0000: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
0010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0020: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0030: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0040: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
0050: 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 ** This file is
0060: 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 an amalgamation
0070: 6f 66 20 6d 61 6e 79 20 73 65 70 61 72 61 74 65 of many separate
0080: 20 43 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20 C source files
0090: 66 72 6f 6d 20 53 51 4c 69 74 65 0a 2a 2a 20 76 from SQLite.** v
00a0: 65 72 73 69 6f 6e 20 33 2e 36 2e 32 30 2e 20 20 ersion 3.6.20.
00b0: 42 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61 6c 6c By combining all
00c0: 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 the individual
00d0: 43 20 63 6f 64 65 20 66 69 6c 65 73 20 69 6e 74 C code files int
00e0: 6f 20 74 68 69 73 20 0a 2a 2a 20 73 69 6e 67 6c o this .** singl
00f0: 65 20 6c 61 72 67 65 20 66 69 6c 65 2c 20 74 68 e large file, th
0100: 65 20 65 6e 74 69 72 65 20 63 6f 64 65 20 63 61 e entire code ca
0110: 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 61 73 n be compiled as
0120: 20 61 20 6f 6e 65 20 74 72 61 6e 73 6c 61 74 69 a one translati
0130: 6f 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 54 68 69 on.** unit. Thi
0140: 73 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 20 63 6f s allows many co
0150: 6d 70 69 6c 65 72 73 20 74 6f 20 64 6f 20 6f 70 mpilers to do op
0160: 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74 68 61 74 timizations that
0170: 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a would not be.**
0180: 20 70 6f 73 73 69 62 6c 65 20 69 66 20 74 68 65 possible if the
0190: 20 66 69 6c 65 73 20 77 65 72 65 20 63 6f 6d 70 files were comp
01a0: 69 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e iled separately.
01b0: 20 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d Performance im
01c0: 70 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a 20 6f 66 provements.** of
01d0: 20 35 25 20 61 72 65 20 6d 6f 72 65 20 61 72 65 5% are more are
01e0: 20 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e 20 77 commonly seen w
01f0: 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f hen SQLite is co
0200: 6d 70 69 6c 65 64 20 61 73 20 61 20 73 69 6e 67 mpiled as a sing
0210: 6c 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f le.** translatio
0220: 6e 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 n unit..**.** Th
0230: 69 73 20 66 69 6c 65 20 69 73 20 61 6c 6c 20 79 is file is all y
0240: 6f 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 69 ou need to compi
0250: 6c 65 20 53 51 4c 69 74 65 2e 20 20 54 6f 20 75 le SQLite. To u
0260: 73 65 20 53 51 4c 69 74 65 20 69 6e 20 6f 74 68 se SQLite in oth
0270: 65 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 2c 20 er.** programs,
0280: 79 6f 75 20 6e 65 65 64 20 74 68 69 73 20 66 69 you need this fi
0290: 6c 65 20 61 6e 64 20 74 68 65 20 22 73 71 6c 69 le and the "sqli
02a0: 74 65 33 2e 68 22 20 68 65 61 64 65 72 20 66 69 te3.h" header fi
02b0: 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 0a le that defines.
02c0: 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 6d 69 ** the programmi
02d0: 6e 67 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 ng interface to
02e0: 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 the SQLite libra
02f0: 72 79 2e 20 20 28 49 66 20 79 6f 75 20 64 6f 20 ry. (If you do
0300: 6e 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74 68 65 not have .** the
0310: 20 22 73 71 6c 69 74 65 33 2e 68 22 20 68 65 61 "sqlite3.h" hea
0320: 64 65 72 20 66 69 6c 65 20 61 74 20 68 61 6e 64 der file at hand
0330: 2c 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e 64 20 , you will find
0340: 61 20 63 6f 70 79 20 65 6d 62 65 64 64 65 64 20 a copy embedded
0350: 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 74 65 within.** the te
0360: 78 74 20 6f 66 20 74 68 69 73 20 66 69 6c 65 2e xt of this file.
0370: 20 20 53 65 61 72 63 68 20 66 6f 72 20 22 42 65 Search for "Be
0380: 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 33 gin file sqlite3
0390: 2e 68 22 20 74 6f 20 66 69 6e 64 20 74 68 65 20 .h" to find the
03a0: 73 74 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20 start.** of the
03b0: 65 6d 62 65 64 64 65 64 20 73 71 6c 69 74 65 33 embedded sqlite3
03c0: 2e 68 20 68 65 61 64 65 72 20 66 69 6c 65 2e 29 .h header file.)
03d0: 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 64 65 Additional code
03e0: 20 66 69 6c 65 73 20 6d 61 79 20 62 65 20 6e 65 files may be ne
03f0: 65 64 65 64 0a 2a 2a 20 69 66 20 79 6f 75 20 77 eded.** if you w
0400: 61 6e 74 20 61 20 77 72 61 70 70 65 72 20 74 6f ant a wrapper to
0410: 20 69 6e 74 65 72 66 61 63 65 20 53 51 4c 69 74 interface SQLit
0420: 65 20 77 69 74 68 20 79 6f 75 72 20 63 68 6f 69 e with your choi
0430: 63 65 20 6f 66 20 70 72 6f 67 72 61 6d 6d 69 6e ce of programmin
0440: 67 0a 2a 2a 20 6c 61 6e 67 75 61 67 65 2e 20 54 g.** language. T
0450: 68 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 he code for the
0460: 22 73 71 6c 69 74 65 33 22 20 63 6f 6d 6d 61 6e "sqlite3" comman
0470: 64 2d 6c 69 6e 65 20 73 68 65 6c 6c 20 69 73 20 d-line shell is
0480: 61 6c 73 6f 20 69 6e 20 61 0a 2a 2a 20 73 65 70 also in a.** sep
0490: 61 72 61 74 65 20 66 69 6c 65 2e 20 54 68 69 73 arate file. This
04a0: 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6f file contains o
04b0: 6e 6c 79 20 63 6f 64 65 20 66 6f 72 20 74 68 65 nly code for the
04c0: 20 63 6f 72 65 20 53 51 4c 69 74 65 20 6c 69 62 core SQLite lib
04d0: 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rary..**.** This
04e0: 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 77 61 amalgamation wa
04f0: 73 20 67 65 6e 65 72 61 74 65 64 20 6f 6e 20 32 s generated on 2
0500: 30 30 39 2d 31 31 2d 30 31 20 31 39 3a 32 32 3a 009-11-01 19:22:
0510: 30 33 20 55 54 43 2e 0a 2a 2f 0a 23 64 65 66 69 03 UTC..*/.#defi
0520: 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 31 ne SQLITE_CORE 1
0530: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
0540: 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 31 0a 23 AMALGAMATION 1.#
0550: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 52 ifndef SQLITE_PR
0560: 49 56 41 54 45 0a 23 20 64 65 66 69 6e 65 20 53 IVATE.# define S
0570: 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 74 QLITE_PRIVATE st
0580: 61 74 69 63 0a 23 65 6e 64 69 66 0a 23 69 66 6e atic.#endif.#ifn
0590: 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0a 23 def SQLITE_API.#
05a0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 define SQLITE_A
05b0: 50 49 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a PI.#endif./*****
05c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
05d0: 66 69 6c 65 20 73 71 6c 69 74 65 49 6e 74 2e 68 file sqliteInt.h
05e0: 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
05f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0600: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
0610: 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
0620: 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
0630: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
0640: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
0650: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
0660: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
0670: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
0680: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
0690: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
06a0: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
06b0: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
06c0: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
06d0: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
06e0: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
06f0: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
0700: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
0710: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
0720: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
0730: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
0740: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0750: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0760: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0770: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0780: 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 69 6e 74 .** Internal int
0790: 65 72 66 61 63 65 20 64 65 66 69 6e 69 74 69 6f erface definitio
07a0: 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a ns for SQLite..*
07b0: 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 *.*/.#ifndef _SQ
07c0: 4c 49 54 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69 LITEINT_H_.#defi
07d0: 6e 65 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48 5f ne _SQLITEINT_H_
07e0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 ../*.** These #d
07f0: 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e efines should en
0800: 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73 able >2GB file s
0810: 75 70 70 6f 72 74 20 6f 6e 20 50 4f 53 49 58 20 upport on POSIX
0820: 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c if the.** underl
0830: 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 ying operating s
0840: 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69 ystem supports i
0850: 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61 t. If the OS la
0860: 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c cks.** large fil
0870: 65 20 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66 e support, or if
0880: 20 74 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f the OS is windo
0890: 77 73 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64 ws, these should
08a0: 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a be no-ops..**.*
08b0: 2a 20 54 69 63 6b 65 74 20 23 32 37 33 39 3a 20 * Ticket #2739:
08c0: 20 54 68 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f The _LARGEFILE_
08d0: 53 4f 55 52 43 45 20 6d 61 63 72 6f 20 6d 75 73 SOURCE macro mus
08e0: 74 20 61 70 70 65 61 72 20 62 65 66 6f 72 65 20 t appear before
08f0: 61 6e 79 0a 2a 2a 20 73 79 73 74 65 6d 20 23 69 any.** system #i
0900: 6e 63 6c 75 64 65 73 2e 20 20 48 65 6e 63 65 2c ncludes. Hence,
0910: 20 74 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 this block of c
0920: 6f 64 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 ode must be the
0930: 76 65 72 79 20 66 69 72 73 74 0a 2a 2a 20 63 6f very first.** co
0940: 64 65 20 69 6e 20 61 6c 6c 20 73 6f 75 72 63 65 de in all source
0950: 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 files..**.** La
0960: 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 rge file support
0970: 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 can be disabled
0980: 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c using the -DSQL
0990: 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 ITE_DISABLE_LFS
09a0: 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 switch.** on the
09b0: 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e compiler comman
09c0: 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 d line. This is
09d0: 20 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f necessary if yo
09e0: 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a u are compiling.
09f0: 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d ** on a recent m
0a00: 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 20 achine (ex: Red
0a10: 48 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 Hat 7.2) but you
0a20: 20 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 want your code
0a30: 74 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e to work.** on an
0a40: 20 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 older machine (
0a50: 65 78 3a 20 52 65 64 20 48 61 74 20 36 2e 30 29 ex: Red Hat 6.0)
0a60: 2e 20 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c . If you compil
0a70: 65 20 6f 6e 20 52 65 64 20 48 61 74 20 37 2e 32 e on Red Hat 7.2
0a80: 0a 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73 .** without this
0a90: 20 6f 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 option, LFS is
0aa0: 65 6e 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 enable. But LFS
0ab0: 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 does not exist
0ac0: 69 6e 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a in the kernel.**
0ad0: 20 69 6e 20 52 65 64 20 48 61 74 20 36 2e 30 2c in Red Hat 6.0,
0ae0: 20 73 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e so the code won
0af0: 27 74 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 't work. Hence,
0b00: 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e for maximum bin
0b10: 61 72 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 ary.** portabili
0b20: 74 79 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d ty you should om
0b30: 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 53 69 it LFS..**.** Si
0b40: 6d 69 6c 61 72 20 69 73 20 74 72 75 65 20 66 6f milar is true fo
0b50: 72 20 4d 61 63 20 4f 53 20 58 2e 20 20 4c 46 53 r Mac OS X. LFS
0b60: 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 is only support
0b70: 65 64 20 6f 6e 20 4d 61 63 20 4f 53 20 58 20 39 ed on Mac OS X 9
0b80: 20 61 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23 and later..*/.#
0b90: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 ifndef SQLITE_DI
0ba0: 53 41 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69 SABLE_LFS.# defi
0bb0: 6e 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20 ne _LARGE_FILE
0bc0: 20 20 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20 1.# ifndef
0bd0: 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 _FILE_OFFSET_BIT
0be0: 53 0a 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49 S.# define _FI
0bf0: 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36 LE_OFFSET_BITS 6
0c00: 34 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 4.# endif.# defi
0c10: 6e 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f ne _LARGEFILE_SO
0c20: 55 52 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f URCE 1.#endif../
0c30: 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 74 68 65 *.** Include the
0c40: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 68 configuration h
0c50: 65 61 64 65 72 20 6f 75 74 70 75 74 20 62 79 20 eader output by
0c60: 27 63 6f 6e 66 69 67 75 72 65 27 20 69 66 20 77 'configure' if w
0c70: 65 27 72 65 20 75 73 69 6e 67 20 74 68 65 0a 2a e're using the.*
0c80: 2a 20 61 75 74 6f 63 6f 6e 66 2d 62 61 73 65 64 * autoconf-based
0c90: 20 62 75 69 6c 64 0a 2a 2f 0a 23 69 66 64 65 66 build.*/.#ifdef
0ca0: 20 5f 48 41 56 45 5f 53 51 4c 49 54 45 5f 43 4f _HAVE_SQLITE_CO
0cb0: 4e 46 49 47 5f 48 0a 23 69 6e 63 6c 75 64 65 20 NFIG_H.#include
0cc0: 22 63 6f 6e 66 69 67 2e 68 22 0a 23 65 6e 64 69 "config.h".#endi
0cd0: 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f../************
0ce0: 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 ** Include sqlit
0cf0: 65 4c 69 6d 69 74 2e 68 20 69 6e 20 74 68 65 20 eLimit.h in the
0d00: 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 middle of sqlite
0d10: 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
0d20: 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
0d30: 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 ** Begin file sq
0d40: 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a liteLimit.h ****
0d50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0d60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0d70: 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 */./*.** 2007 Ma
0d80: 79 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 y 7.**.** The au
0d90: 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
0da0: 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
0db0: 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
0dc0: 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
0dd0: 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
0de0: 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
0df0: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
0e00: 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
0e10: 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
0e20: 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
0e30: 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
0e40: 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
0e50: 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
0e60: 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
0e70: 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
0e80: 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
0e90: 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
0ea0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0eb0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0ec0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0ed0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0ee0: 2a 2a 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 66 **.** .** This f
0ef0: 69 6c 65 20 64 65 66 69 6e 65 73 20 76 61 72 69 ile defines vari
0f00: 6f 75 73 20 6c 69 6d 69 74 73 20 6f 66 20 77 68 ous limits of wh
0f10: 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 70 72 at SQLite can pr
0f20: 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 ocess..**.** @(#
0f30: 29 20 24 49 64 3a 20 73 71 6c 69 74 65 4c 69 6d ) $Id: sqliteLim
0f40: 69 74 2e 68 2c 76 20 31 2e 31 30 20 32 30 30 39 it.h,v 1.10 2009
0f50: 2f 30 31 2f 31 30 20 31 36 3a 31 35 3a 30 39 20 /01/10 16:15:09
0f60: 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 danielk1977 Exp
0f70: 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 $.*/../*.** The
0f80: 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f maximum length o
0f90: 66 20 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 f a TEXT or BLOB
0fa0: 20 69 6e 20 62 79 74 65 73 2e 20 20 20 54 68 69 in bytes. Thi
0fb0: 73 20 61 6c 73 6f 0a 2a 2a 20 6c 69 6d 69 74 73 s also.** limits
0fc0: 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 72 the size of a r
0fd0: 6f 77 20 69 6e 20 61 20 74 61 62 6c 65 20 6f 72 ow in a table or
0fe0: 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 index..**.** Th
0ff0: 65 20 68 61 72 64 20 6c 69 6d 69 74 20 69 73 20 e hard limit is
1000: 74 68 65 20 61 62 69 6c 69 74 79 20 6f 66 20 61 the ability of a
1010: 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 32-bit signed i
1020: 6e 74 65 67 65 72 0a 2a 2a 20 74 6f 20 63 6f 75 nteger.** to cou
1030: 6e 74 20 74 68 65 20 73 69 7a 65 3a 20 32 5e 33 nt the size: 2^3
1040: 31 2d 31 20 6f 72 20 32 31 34 37 34 38 33 36 34 1-1 or 214748364
1050: 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 7..*/.#ifndef SQ
1060: 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a LITE_MAX_LENGTH.
1070: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
1080: 4d 41 58 5f 4c 45 4e 47 54 48 20 31 30 30 30 30 MAX_LENGTH 10000
1090: 30 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 00000.#endif../*
10a0: 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 .** This is the
10b0: 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
10c0: 66 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c f.**.** * Col
10d0: 75 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 0a umns in a table.
10e0: 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 ** * Columns
10f0: 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 20 in an index.**
1100: 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 * Columns in a
1110: 20 76 69 65 77 0a 2a 2a 20 20 20 20 2a 20 54 65 view.** * Te
1120: 72 6d 73 20 69 6e 20 74 68 65 20 53 45 54 20 63 rms in the SET c
1130: 6c 61 75 73 65 20 6f 66 20 61 6e 20 55 50 44 41 lause of an UPDA
1140: 54 45 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 TE statement.**
1150: 20 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 * Terms in th
1160: 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 e result set of
1170: 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 a SELECT stateme
1180: 6e 74 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 nt.** * Terms
1190: 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20 42 59 in the GROUP BY
11a0: 20 6f 72 20 4f 52 44 45 52 20 42 59 20 63 6c 61 or ORDER BY cla
11b0: 75 73 65 73 20 6f 66 20 61 20 53 45 4c 45 43 54 uses of a SELECT
11c0: 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 20 statement..**
11d0: 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65 * Terms in the
11e0: 20 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 6f VALUES clause o
11f0: 66 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 f an INSERT stat
1200: 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ement.**.** The
1210: 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d 69 74 hard upper limit
1220: 20 68 65 72 65 20 69 73 20 33 32 36 37 36 2e 20 here is 32676.
1230: 20 4d 6f 73 74 20 64 61 74 61 62 61 73 65 20 70 Most database p
1240: 65 6f 70 6c 65 20 77 69 6c 6c 0a 2a 2a 20 74 65 eople will.** te
1250: 6c 6c 20 79 6f 75 20 74 68 61 74 20 69 6e 20 61 ll you that in a
1260: 20 77 65 6c 6c 2d 6e 6f 72 6d 61 6c 69 7a 65 64 well-normalized
1270: 20 64 61 74 61 62 61 73 65 2c 20 79 6f 75 20 75 database, you u
1280: 73 75 61 6c 6c 79 20 73 68 6f 75 6c 64 0a 2a 2a sually should.**
1290: 20 6e 6f 74 20 68 61 76 65 20 6d 6f 72 65 20 74 not have more t
12a0: 68 61 6e 20 61 20 64 6f 7a 65 6e 20 6f 72 20 73 han a dozen or s
12b0: 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 79 o columns in any
12c0: 20 74 61 62 6c 65 2e 20 20 41 6e 64 20 69 66 0a table. And if.
12d0: 2a 2a 20 74 68 61 74 20 69 73 20 74 68 65 20 63 ** that is the c
12e0: 61 73 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f ase, there is no
12f0: 20 70 6f 69 6e 74 20 69 6e 20 68 61 76 69 6e 67 point in having
1300: 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 66 65 77 more than a few
1310: 0a 2a 2a 20 64 6f 7a 65 6e 20 76 61 6c 75 65 73 .** dozen values
1320: 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f in any of the o
1330: 74 68 65 72 20 73 69 74 75 61 74 69 6f 6e 73 20 ther situations
1340: 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e described above.
1350: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
1360: 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 23 20 TE_MAX_COLUMN.#
1370: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
1380: 58 5f 43 4f 4c 55 4d 4e 20 32 30 30 30 0a 23 65 X_COLUMN 2000.#e
1390: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
13a0: 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f maximum length o
13b0: 66 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 f a single SQL s
13c0: 74 61 74 65 6d 65 6e 74 20 69 6e 20 62 79 74 65 tatement in byte
13d0: 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 75 73 65 64 s..**.** It used
13e0: 20 74 6f 20 62 65 20 74 68 65 20 63 61 73 65 20 to be the case
13f0: 74 68 61 74 20 73 65 74 74 69 6e 67 20 74 68 69 that setting thi
1400: 73 20 76 61 6c 75 65 20 74 6f 20 7a 65 72 6f 20 s value to zero
1410: 77 6f 75 6c 64 0a 2a 2a 20 74 75 72 6e 20 74 68 would.** turn th
1420: 65 20 6c 69 6d 69 74 20 6f 66 66 2e 20 20 54 68 e limit off. Th
1430: 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 at is no longer
1440: 74 72 75 65 2e 20 20 49 74 20 69 73 20 6e 6f 74 true. It is not
1450: 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 possible.** to
1460: 74 75 72 6e 20 74 68 69 73 20 6c 69 6d 69 74 20 turn this limit
1470: 6f 66 66 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 off..*/.#ifndef
1480: 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c SQLITE_MAX_SQL_L
1490: 45 4e 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 ENGTH.# define S
14a0: 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 QLITE_MAX_SQL_LE
14b0: 4e 47 54 48 20 31 30 30 30 30 30 30 30 30 30 0a NGTH 1000000000.
14c0: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
14d0: 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 e maximum depth
14e0: 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e of an expression
14f0: 20 74 72 65 65 2e 20 54 68 69 73 20 69 73 20 6c tree. This is l
1500: 69 6d 69 74 65 64 20 74 6f 20 0a 2a 2a 20 73 6f imited to .** so
1510: 6d 65 20 65 78 74 65 6e 74 20 62 79 20 53 51 4c me extent by SQL
1520: 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 ITE_MAX_SQL_LENG
1530: 54 48 2e 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 TH. But sometime
1540: 20 79 6f 75 20 6d 69 67 68 74 20 0a 2a 2a 20 77 you might .** w
1550: 61 6e 74 20 74 6f 20 70 6c 61 63 65 20 6d 6f 72 ant to place mor
1560: 65 20 73 65 76 65 72 65 20 6c 69 6d 69 74 73 20 e severe limits
1570: 6f 6e 20 74 68 65 20 63 6f 6d 70 6c 65 78 69 74 on the complexit
1580: 79 20 6f 66 20 61 6e 20 0a 2a 2a 20 65 78 70 72 y of an .** expr
1590: 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 ession..**.** A
15a0: 76 61 6c 75 65 20 6f 66 20 30 20 75 73 65 64 20 value of 0 used
15b0: 74 6f 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 to mean that the
15c0: 20 6c 69 6d 69 74 20 77 61 73 20 6e 6f 74 20 65 limit was not e
15d0: 6e 66 6f 72 63 65 64 2e 0a 2a 2a 20 42 75 74 20 nforced..** But
15e0: 74 68 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 that is no longe
15f0: 72 20 74 72 75 65 2e 20 20 54 68 65 20 6c 69 6d r true. The lim
1600: 69 74 20 69 73 20 6e 6f 77 20 73 74 72 69 63 74 it is now strict
1610: 6c 79 20 65 6e 66 6f 72 63 65 64 0a 2a 2a 20 61 ly enforced.** a
1620: 74 20 61 6c 6c 20 74 69 6d 65 73 2e 0a 2a 2f 0a t all times..*/.
1630: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d #ifndef SQLITE_M
1640: 41 58 5f 45 58 50 52 5f 44 45 50 54 48 0a 23 20 AX_EXPR_DEPTH.#
1650: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
1660: 58 5f 45 58 50 52 5f 44 45 50 54 48 20 31 30 30 X_EXPR_DEPTH 100
1670: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 0.#endif../*.**
1680: 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 The maximum numb
1690: 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61 er of terms in a
16a0: 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 compound SELECT
16b0: 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54 statement..** T
16c0: 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f he code generato
16d0: 72 20 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 20 53 r for compound S
16e0: 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 ELECT statements
16f0: 20 64 6f 65 73 20 6f 6e 65 0a 2a 2a 20 6c 65 76 does one.** lev
1700: 65 6c 20 6f 66 20 72 65 63 75 72 73 69 6f 6e 20 el of recursion
1710: 66 6f 72 20 65 61 63 68 20 74 65 72 6d 2e 20 20 for each term.
1720: 41 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77 A stack overflow
1730: 20 63 61 6e 20 72 65 73 75 6c 74 0a 2a 2a 20 69 can result.** i
1740: 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 f the number of
1750: 74 65 72 6d 73 20 69 73 20 74 6f 6f 20 6c 61 72 terms is too lar
1760: 67 65 2e 20 20 49 6e 20 70 72 61 63 74 69 63 65 ge. In practice
1770: 2c 20 6d 6f 73 74 20 53 51 4c 0a 2a 2a 20 6e 65 , most SQL.** ne
1780: 76 65 72 20 68 61 73 20 6d 6f 72 65 20 74 68 61 ver has more tha
1790: 6e 20 33 20 6f 72 20 34 20 74 65 72 6d 73 2e 20 n 3 or 4 terms.
17a0: 20 55 73 65 20 61 20 76 61 6c 75 65 20 6f 66 20 Use a value of
17b0: 30 20 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20 0 to disable.**
17c0: 61 6e 79 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 any limit on the
17d0: 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 number of terms
17e0: 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 74 20 53 in a compount S
17f0: 45 4c 45 43 54 2e 0a 2a 2f 0a 23 69 66 6e 64 65 ELECT..*/.#ifnde
1800: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d f SQLITE_MAX_COM
1810: 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 23 20 64 POUND_SELECT.# d
1820: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 efine SQLITE_MAX
1830: 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 _COMPOUND_SELECT
1840: 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 500.#endif../*.
1850: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e ** The maximum n
1860: 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73 umber of opcodes
1870: 20 69 6e 20 61 20 56 44 42 45 20 70 72 6f 67 72 in a VDBE progr
1880: 61 6d 2e 0a 2a 2a 20 4e 6f 74 20 63 75 72 72 65 am..** Not curre
1890: 6e 74 6c 79 20 65 6e 66 6f 72 63 65 64 2e 0a 2a ntly enforced..*
18a0: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
18b0: 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 0a 23 20 64 _MAX_VDBE_OP.# d
18c0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 efine SQLITE_MAX
18d0: 5f 56 44 42 45 5f 4f 50 20 32 35 30 30 30 0a 23 _VDBE_OP 25000.#
18e0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
18f0: 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 maximum number
1900: 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 of arguments to
1910: 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e an SQL function.
1920: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
1930: 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f TE_MAX_FUNCTION_
1940: 41 52 47 0a 23 20 64 65 66 69 6e 65 20 53 51 4c ARG.# define SQL
1950: 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e ITE_MAX_FUNCTION
1960: 5f 41 52 47 20 31 32 37 0a 23 65 6e 64 69 66 0a _ARG 127.#endif.
1970: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d ./*.** The maxim
1980: 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d um number of in-
1990: 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 74 6f 20 memory pages to
19a0: 75 73 65 20 66 6f 72 20 74 68 65 20 6d 61 69 6e use for the main
19b0: 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 database.** tab
19c0: 6c 65 20 61 6e 64 20 66 6f 72 20 74 65 6d 70 6f le and for tempo
19d0: 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 54 68 rary tables. Th
19e0: 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 e SQLITE_DEFAULT
19f0: 5f 43 41 43 48 45 5f 53 49 5a 45 0a 2a 2f 0a 23 _CACHE_SIZE.*/.#
1a00: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 ifndef SQLITE_DE
1a10: 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 FAULT_CACHE_SIZE
1a20: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
1a30: 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 _DEFAULT_CACHE_S
1a40: 49 5a 45 20 20 32 30 30 30 0a 23 65 6e 64 69 66 IZE 2000.#endif
1a50: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1a60: 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 DEFAULT_TEMP_CAC
1a70: 48 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 HE_SIZE.# define
1a80: 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
1a90: 54 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45 20 TEMP_CACHE_SIZE
1aa0: 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 500.#endif../*.
1ab0: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e ** The maximum n
1ac0: 75 6d 62 65 72 20 6f 66 20 61 74 74 61 63 68 65 umber of attache
1ad0: 64 20 64 61 74 61 62 61 73 65 73 2e 20 20 54 68 d databases. Th
1ae0: 69 73 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 is must be betwe
1af0: 65 6e 20 30 0a 2a 2a 20 61 6e 64 20 33 30 2e 20 en 0.** and 30.
1b00: 20 54 68 65 20 75 70 70 65 72 20 62 6f 75 6e 64 The upper bound
1b10: 20 6f 6e 20 33 30 20 69 73 20 62 65 63 61 75 73 on 30 is becaus
1b20: 65 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 e a 32-bit integ
1b30: 65 72 20 62 69 74 6d 61 70 0a 2a 2a 20 69 73 20 er bitmap.** is
1b40: 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 used internally
1b50: 74 6f 20 74 72 61 63 6b 20 61 74 74 61 63 68 65 to track attache
1b60: 64 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2f 0a d databases..*/.
1b70: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d #ifndef SQLITE_M
1b80: 41 58 5f 41 54 54 41 43 48 45 44 0a 23 20 64 65 AX_ATTACHED.# de
1b90: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f fine SQLITE_MAX_
1ba0: 41 54 54 41 43 48 45 44 20 31 30 0a 23 65 6e 64 ATTACHED 10.#end
1bb0: 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d if.../*.** The m
1bc0: 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 aximum value of
1bd0: 61 20 3f 6e 6e 6e 20 77 69 6c 64 63 61 72 64 20 a ?nnn wildcard
1be0: 74 68 61 74 20 74 68 65 20 70 61 72 73 65 72 20 that the parser
1bf0: 77 69 6c 6c 20 61 63 63 65 70 74 2e 0a 2a 2f 0a will accept..*/.
1c00: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d #ifndef SQLITE_M
1c10: 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 AX_VARIABLE_NUMB
1c20: 45 52 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 ER.# define SQLI
1c30: 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f TE_MAX_VARIABLE_
1c40: 4e 55 4d 42 45 52 20 39 39 39 0a 23 65 6e 64 69 NUMBER 999.#endi
1c50: 66 0a 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 70 61 f../* Maximum pa
1c60: 67 65 20 73 69 7a 65 2e 20 20 54 68 65 20 75 70 ge size. The up
1c70: 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 69 per bound on thi
1c80: 73 20 76 61 6c 75 65 20 69 73 20 33 32 37 36 38 s value is 32768
1c90: 2e 20 20 54 68 69 73 20 61 20 6c 69 6d 69 74 0a . This a limit.
1ca0: 2a 2a 20 69 6d 70 6f 73 65 64 20 62 79 20 74 68 ** imposed by th
1cb0: 65 20 6e 65 63 65 73 73 69 74 79 20 6f 66 20 73 e necessity of s
1cc0: 74 6f 72 69 6e 67 20 74 68 65 20 76 61 6c 75 65 toring the value
1cd0: 20 69 6e 20 61 20 32 2d 62 79 74 65 20 75 6e 73 in a 2-byte uns
1ce0: 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a igned integer.**
1cf0: 20 61 6e 64 20 74 68 65 20 66 61 63 74 20 74 68 and the fact th
1d00: 61 74 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 at the page size
1d10: 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 must be a power
1d20: 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 of 2..**.** If
1d30: 74 68 69 73 20 6c 69 6d 69 74 20 69 73 20 63 68 this limit is ch
1d40: 61 6e 67 65 64 2c 20 74 68 65 6e 20 74 68 65 20 anged, then the
1d50: 63 6f 6d 70 69 6c 65 64 20 6c 69 62 72 61 72 79 compiled library
1d60: 20 69 73 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a is technically.
1d70: 2a 2a 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 ** incompatible
1d80: 77 69 74 68 20 61 6e 20 53 51 4c 69 74 65 20 6c with an SQLite l
1d90: 69 62 72 61 72 79 20 63 6f 6d 70 69 6c 65 64 20 ibrary compiled
1da0: 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 with a different
1db0: 20 6c 69 6d 69 74 2e 20 49 66 0a 2a 2a 20 61 20 limit. If.** a
1dc0: 70 72 6f 63 65 73 73 20 6f 70 65 72 61 74 69 6e process operatin
1dd0: 67 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 g on a database
1de0: 77 69 74 68 20 61 20 70 61 67 65 2d 73 69 7a 65 with a page-size
1df0: 20 6f 66 20 36 35 35 33 36 20 62 79 74 65 73 20 of 65536 bytes
1e00: 0a 2a 2a 20 63 72 61 73 68 65 73 2c 20 74 68 65 .** crashes, the
1e10: 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 n an instance of
1e20: 20 53 51 4c 69 74 65 20 63 6f 6d 70 69 6c 65 64 SQLite compiled
1e30: 20 77 69 74 68 20 74 68 65 20 64 65 66 61 75 6c with the defaul
1e40: 74 20 70 61 67 65 2d 73 69 7a 65 20 0a 2a 2a 20 t page-size .**
1e50: 6c 69 6d 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 limit will not b
1e60: 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 e able to rollba
1e70: 63 6b 20 74 68 65 20 61 62 6f 72 74 65 64 20 74 ck the aborted t
1e80: 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 ransaction. This
1e90: 20 63 6f 75 6c 64 0a 2a 2a 20 6c 65 61 64 20 74 could.** lead t
1ea0: 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 o database corru
1eb0: 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 ption..*/.#ifnde
1ec0: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 f SQLITE_MAX_PAG
1ed0: 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 E_SIZE.# define
1ee0: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
1ef0: 53 49 5a 45 20 33 32 37 36 38 0a 23 65 6e 64 69 SIZE 32768.#endi
1f00: 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 f.../*.** The de
1f10: 66 61 75 6c 74 20 73 69 7a 65 20 6f 66 20 61 20 fault size of a
1f20: 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 0a 2a database page..*
1f30: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1f40: 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 _DEFAULT_PAGE_SI
1f50: 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 ZE.# define SQLI
1f60: 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f TE_DEFAULT_PAGE_
1f70: 53 49 5a 45 20 31 30 32 34 0a 23 65 6e 64 69 66 SIZE 1024.#endif
1f80: 0a 23 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41 .#if SQLITE_DEFA
1f90: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3e 53 51 ULT_PAGE_SIZE>SQ
1fa0: 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 LITE_MAX_PAGE_SI
1fb0: 5a 45 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 ZE.# undef SQLIT
1fc0: 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 E_DEFAULT_PAGE_S
1fd0: 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c IZE.# define SQL
1fe0: 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 ITE_DEFAULT_PAGE
1ff0: 5f 53 49 5a 45 20 53 51 4c 49 54 45 5f 4d 41 58 _SIZE SQLITE_MAX
2000: 5f 50 41 47 45 5f 53 49 5a 45 0a 23 65 6e 64 69 _PAGE_SIZE.#endi
2010: 66 0a 0a 2f 2a 0a 2a 2a 20 4f 72 64 69 6e 61 72 f../*.** Ordinar
2020: 69 6c 79 2c 20 69 66 20 6e 6f 20 76 61 6c 75 65 ily, if no value
2030: 20 69 73 20 65 78 70 6c 69 63 69 74 6c 79 20 70 is explicitly p
2040: 72 6f 76 69 64 65 64 2c 20 53 51 4c 69 74 65 20 rovided, SQLite
2050: 63 72 65 61 74 65 73 20 64 61 74 61 62 61 73 65 creates database
2060: 73 0a 2a 2a 20 77 69 74 68 20 70 61 67 65 20 73 s.** with page s
2070: 69 7a 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 ize SQLITE_DEFAU
2080: 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2e 20 48 6f LT_PAGE_SIZE. Ho
2090: 77 65 76 65 72 2c 20 62 61 73 65 64 20 6f 6e 20 wever, based on
20a0: 63 65 72 74 61 69 6e 0a 2a 2a 20 64 65 76 69 63 certain.** devic
20b0: 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 e characteristic
20c0: 73 20 28 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 s (sector-size a
20d0: 6e 64 20 61 74 6f 6d 69 63 20 77 72 69 74 65 28 nd atomic write(
20e0: 29 20 73 75 70 70 6f 72 74 29 2c 0a 2a 2a 20 53 ) support),.** S
20f0: 51 4c 69 74 65 20 6d 61 79 20 63 68 6f 6f 73 65 QLite may choose
2100: 20 61 20 6c 61 72 67 65 72 20 76 61 6c 75 65 2e a larger value.
2110: 20 54 68 69 73 20 63 6f 6e 73 74 61 6e 74 20 69 This constant i
2120: 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 s the maximum va
2130: 6c 75 65 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 lue.** SQLite wi
2140: 6c 6c 20 63 68 6f 6f 73 65 20 6f 6e 20 69 74 73 ll choose on its
2150: 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 own..*/.#ifndef
2160: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 SQLITE_MAX_DEFA
2170: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 ULT_PAGE_SIZE.#
2180: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
2190: 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 X_DEFAULT_PAGE_S
21a0: 49 5a 45 20 38 31 39 32 0a 23 65 6e 64 69 66 0a IZE 8192.#endif.
21b0: 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 #if SQLITE_MAX_D
21c0: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
21d0: 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 >SQLITE_MAX_PAGE
21e0: 5f 53 49 5a 45 0a 23 20 75 6e 64 65 66 20 53 51 _SIZE.# undef SQ
21f0: 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 LITE_MAX_DEFAULT
2200: 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 _PAGE_SIZE.# def
2210: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 ine SQLITE_MAX_D
2220: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
2230: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 SQLITE_MAX_PAGE
2240: 5f 53 49 5a 45 0a 23 65 6e 64 69 66 0a 0a 0a 2f _SIZE.#endif.../
2250: 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d *.** Maximum num
2260: 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
2270: 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c one database fil
2280: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 e..**.** This is
2290: 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 74 68 65 really just the
22a0: 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 default value f
22b0: 6f 72 20 74 68 65 20 6d 61 78 5f 70 61 67 65 5f or the max_page_
22c0: 63 6f 75 6e 74 20 70 72 61 67 6d 61 2e 0a 2a 2a count pragma..**
22d0: 20 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e 20 This value can
22e0: 62 65 20 6c 6f 77 65 72 65 64 20 28 6f 72 20 72 be lowered (or r
22f0: 61 69 73 65 64 29 20 61 74 20 72 75 6e 2d 74 69 aised) at run-ti
2300: 6d 65 20 75 73 69 6e 67 20 74 68 61 74 20 74 68 me using that th
2310: 65 0a 2a 2a 20 6d 61 78 5f 70 61 67 65 5f 63 6f e.** max_page_co
2320: 75 6e 74 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 unt macro..*/.#i
2330: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
2340: 5f 50 41 47 45 5f 43 4f 55 4e 54 0a 23 20 64 65 _PAGE_COUNT.# de
2350: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f fine SQLITE_MAX_
2360: 50 41 47 45 5f 43 4f 55 4e 54 20 31 30 37 33 37 PAGE_COUNT 10737
2370: 34 31 38 32 33 0a 23 65 6e 64 69 66 0a 0a 2f 2a 41823.#endif../*
2380: 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 .** Maximum leng
2390: 74 68 20 28 69 6e 20 62 79 74 65 73 29 20 6f 66 th (in bytes) of
23a0: 20 74 68 65 20 70 61 74 74 65 72 6e 20 69 6e 20 the pattern in
23b0: 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a a LIKE or GLOB.*
23c0: 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23 * operator..*/.#
23d0: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 ifndef SQLITE_MA
23e0: 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c X_LIKE_PATTERN_L
23f0: 45 4e 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 ENGTH.# define S
2400: 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 QLITE_MAX_LIKE_P
2410: 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20 35 30 ATTERN_LENGTH 50
2420: 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 000.#endif../*.*
2430: 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 * Maximum depth
2440: 6f 66 20 72 65 63 75 72 73 69 6f 6e 20 66 6f 72 of recursion for
2450: 20 74 72 69 67 67 65 72 73 2e 0a 2a 2a 0a 2a 2a triggers..**.**
2460: 20 41 20 76 61 6c 75 65 20 6f 66 20 31 20 6d 65 A value of 1 me
2470: 61 6e 73 20 74 68 61 74 20 61 20 74 72 69 67 67 ans that a trigg
2480: 65 72 20 70 72 6f 67 72 61 6d 20 77 69 6c 6c 20 er program will
2490: 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 69 not be able to i
24a0: 74 73 65 6c 66 0a 2a 2a 20 66 69 72 65 20 61 6e tself.** fire an
24b0: 79 20 74 72 69 67 67 65 72 73 2e 20 41 20 76 61 y triggers. A va
24c0: 6c 75 65 20 6f 66 20 30 20 6d 65 61 6e 73 20 74 lue of 0 means t
24d0: 68 61 74 20 6e 6f 20 74 72 69 67 67 65 72 20 70 hat no trigger p
24e0: 72 6f 67 72 61 6d 73 20 61 74 20 61 6c 6c 20 0a rograms at all .
24f0: 2a 2a 20 6d 61 79 20 62 65 20 65 78 65 63 75 74 ** may be execut
2500: 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ed..*/.#ifndef S
2510: 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 QLITE_MAX_TRIGGE
2520: 52 5f 44 45 50 54 48 0a 23 69 66 20 64 65 66 69 R_DEPTH.#if defi
2530: 6e 65 64 28 53 51 4c 49 54 45 5f 53 4d 41 4c 4c ned(SQLITE_SMALL
2540: 5f 53 54 41 43 4b 29 0a 23 20 64 65 66 69 6e 65 _STACK).# define
2550: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47 SQLITE_MAX_TRIG
2560: 47 45 52 5f 44 45 50 54 48 20 31 30 0a 23 65 6c GER_DEPTH 10.#el
2570: 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 se.# define SQLI
2580: 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44 TE_MAX_TRIGGER_D
2590: 45 50 54 48 20 31 30 30 30 0a 23 65 6e 64 69 66 EPTH 1000.#endif
25a0: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a .#endif../******
25b0: 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
25c0: 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a sqliteLimit.h **
25d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
25e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
25f0: 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
2600: 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 ******** Continu
2610: 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 ing where we lef
2620: 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 t off in sqliteI
2630: 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
2640: 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 44 69 73 *******/../* Dis
2650: 61 62 6c 65 20 6e 75 69 73 61 6e 63 65 20 77 61 able nuisance wa
2660: 72 6e 69 6e 67 73 20 6f 6e 20 42 6f 72 6c 61 6e rnings on Borlan
2670: 64 20 63 6f 6d 70 69 6c 65 72 73 20 2a 2f 0a 23 d compilers */.#
2680: 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 if defined(__BOR
2690: 4c 41 4e 44 43 5f 5f 29 0a 23 70 72 61 67 6d 61 LANDC__).#pragma
26a0: 20 77 61 72 6e 20 2d 72 63 68 20 2f 2a 20 75 6e warn -rch /* un
26b0: 72 65 61 63 68 61 62 6c 65 20 63 6f 64 65 20 2a reachable code *
26c0: 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d /.#pragma warn -
26d0: 63 63 63 20 2f 2a 20 43 6f 6e 64 69 74 69 6f 6e ccc /* Condition
26e0: 20 69 73 20 61 6c 77 61 79 73 20 74 72 75 65 20 is always true
26f0: 6f 72 20 66 61 6c 73 65 20 2a 2f 0a 23 70 72 61 or false */.#pra
2700: 67 6d 61 20 77 61 72 6e 20 2d 61 75 73 20 2f 2a gma warn -aus /*
2710: 20 41 73 73 69 67 6e 65 64 20 76 61 6c 75 65 20 Assigned value
2720: 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 2a 2f is never used */
2730: 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 63 .#pragma warn -c
2740: 73 75 20 2f 2a 20 43 6f 6d 70 61 72 69 6e 67 20 su /* Comparing
2750: 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73 69 67 signed and unsig
2760: 6e 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20 77 ned */.#pragma w
2770: 61 72 6e 20 2d 73 70 61 20 2f 2a 20 53 75 73 70 arn -spa /* Susp
2780: 69 63 69 6f 75 73 20 70 6f 69 6e 74 65 72 20 61 icious pointer a
2790: 72 69 74 68 6d 65 74 69 63 20 2a 2f 0a 23 65 6e rithmetic */.#en
27a0: 64 69 66 0a 0a 2f 2a 20 4e 65 65 64 65 64 20 66 dif../* Needed f
27b0: 6f 72 20 76 61 72 69 6f 75 73 20 64 65 66 69 6e or various defin
27c0: 69 74 69 6f 6e 73 2e 2e 2e 20 2a 2f 0a 23 69 66 itions... */.#if
27d0: 6e 64 65 66 20 5f 47 4e 55 5f 53 4f 55 52 43 45 ndef _GNU_SOURCE
27e0: 0a 23 20 64 65 66 69 6e 65 20 5f 47 4e 55 5f 53 .# define _GNU_S
27f0: 4f 55 52 43 45 0a 23 65 6e 64 69 66 0a 0a 2f 2a OURCE.#endif../*
2800: 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 74 61 6e .** Include stan
2810: 64 61 72 64 20 68 65 61 64 65 72 20 66 69 6c 65 dard header file
2820: 73 20 61 73 20 6e 65 63 65 73 73 61 72 79 0a 2a s as necessary.*
2830: 2f 0a 23 69 66 64 65 66 20 48 41 56 45 5f 53 54 /.#ifdef HAVE_ST
2840: 44 49 4e 54 5f 48 0a 23 69 6e 63 6c 75 64 65 20 DINT_H.#include
2850: 3c 73 74 64 69 6e 74 2e 68 3e 0a 23 65 6e 64 69 <stdint.h>.#endi
2860: 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f 49 4e f.#ifdef HAVE_IN
2870: 54 54 59 50 45 53 5f 48 0a 23 69 6e 63 6c 75 64 TTYPES_H.#includ
2880: 65 20 3c 69 6e 74 74 79 70 65 73 2e 68 3e 0a 23 e <inttypes.h>.#
2890: 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 53 endif..#define S
28a0: 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 QLITE_INDEX_SAMP
28b0: 4c 45 53 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 54 68 LES 10../*.** Th
28c0: 69 73 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 is macro is used
28d0: 20 74 6f 20 22 68 69 64 65 22 20 73 6f 6d 65 20 to "hide" some
28e0: 75 67 6c 69 6e 65 73 73 20 69 6e 20 63 61 73 74 ugliness in cast
28f0: 69 6e 67 20 61 6e 20 69 6e 74 0a 2a 2a 20 76 61 ing an int.** va
2900: 6c 75 65 20 74 6f 20 61 20 70 74 72 20 76 61 6c lue to a ptr val
2910: 75 65 20 75 6e 64 65 72 20 74 68 65 20 4d 53 56 ue under the MSV
2920: 43 20 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65 C 64-bit compile
2930: 72 2e 20 20 20 43 61 73 74 69 6e 67 0a 2a 2a 20 r. Casting.**
2940: 6e 6f 6e 20 36 34 2d 62 69 74 20 76 61 6c 75 65 non 64-bit value
2950: 73 20 74 6f 20 70 74 72 20 74 79 70 65 73 20 72 s to ptr types r
2960: 65 73 75 6c 74 73 20 69 6e 20 61 20 22 68 61 72 esults in a "har
2970: 64 22 20 65 72 72 6f 72 20 77 69 74 68 20 0a 2a d" error with .*
2980: 2a 20 74 68 65 20 4d 53 56 43 20 36 34 2d 62 69 * the MSVC 64-bi
2990: 74 20 63 6f 6d 70 69 6c 65 72 20 77 68 69 63 68 t compiler which
29a0: 20 74 68 69 73 20 61 74 74 65 6d 70 74 73 20 74 this attempts t
29b0: 6f 20 61 76 6f 69 64 2e 20 20 0a 2a 2a 0a 2a 2a o avoid. .**.**
29c0: 20 41 20 73 69 6d 70 6c 65 20 63 6f 6d 70 69 6c A simple compil
29d0: 65 72 20 70 72 61 67 6d 61 20 6f 72 20 63 61 73 er pragma or cas
29e0: 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f ting sequence co
29f0: 75 6c 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e 64 uld not be found
2a00: 0a 2a 2a 20 74 6f 20 63 6f 72 72 65 63 74 20 74 .** to correct t
2a10: 68 69 73 20 69 6e 20 61 6c 6c 20 73 69 74 75 61 his in all situa
2a20: 74 69 6f 6e 73 2c 20 73 6f 20 74 68 69 73 20 6d tions, so this m
2a30: 61 63 72 6f 20 77 61 73 20 69 6e 74 72 6f 64 75 acro was introdu
2a40: 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 63 6f ced..**.** It co
2a50: 75 6c 64 20 62 65 20 61 72 67 75 65 64 20 74 68 uld be argued th
2a60: 61 74 20 74 68 65 20 69 6e 74 70 74 72 5f 74 20 at the intptr_t
2a70: 74 79 70 65 20 63 6f 75 6c 64 20 62 65 20 75 73 type could be us
2a80: 65 64 20 69 6e 20 74 68 69 73 0a 2a 2a 20 63 61 ed in this.** ca
2a90: 73 65 2c 20 62 75 74 20 74 68 61 74 20 74 79 70 se, but that typ
2aa0: 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 e is not availab
2ab0: 6c 65 20 6f 6e 20 61 6c 6c 20 63 6f 6d 70 69 6c le on all compil
2ac0: 65 72 73 2c 20 6f 72 20 0a 2a 2a 20 72 65 71 75 ers, or .** requ
2ad0: 69 72 65 73 20 74 68 65 20 23 69 6e 63 6c 75 64 ires the #includ
2ae0: 65 20 6f 66 20 73 70 65 63 69 66 69 63 20 68 65 e of specific he
2af0: 61 64 65 72 73 20 77 68 69 63 68 20 64 69 66 66 aders which diff
2b00: 65 72 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 70 ers between.** p
2b10: 6c 61 74 66 6f 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 latforms..**.**
2b20: 54 69 63 6b 65 74 20 23 33 38 36 30 3a 20 20 54 Ticket #3860: T
2b30: 68 65 20 6c 6c 76 6d 2d 67 63 63 2d 34 2e 32 20 he llvm-gcc-4.2
2b40: 63 6f 6d 70 69 6c 65 72 20 66 72 6f 6d 20 41 70 compiler from Ap
2b50: 70 6c 65 20 63 68 6f 6b 65 73 20 6f 6e 0a 2a 2a ple chokes on.**
2b60: 20 74 68 65 20 28 28 76 6f 69 64 2a 29 26 28 28 the ((void*)&((
2b70: 63 68 61 72 2a 29 30 29 5b 58 5d 29 20 63 6f 6e char*)0)[X]) con
2b80: 73 74 72 75 63 74 2e 20 20 42 75 74 20 4d 53 56 struct. But MSV
2b90: 43 20 63 68 6f 6b 65 73 20 6f 6e 20 28 28 76 6f C chokes on ((vo
2ba0: 69 64 2a 29 28 58 29 29 2e 0a 2a 2a 20 53 6f 20 id*)(X))..** So
2bb0: 77 65 20 68 61 76 65 20 74 6f 20 64 65 66 69 6e we have to defin
2bc0: 65 20 74 68 65 20 6d 61 63 72 6f 73 20 69 6e 20 e the macros in
2bd0: 64 69 66 66 65 72 65 6e 74 20 77 61 79 73 20 64 different ways d
2be0: 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 0a epending on the.
2bf0: 2a 2a 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2f 0a ** compiler..*/.
2c00: 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e #if defined(__GN
2c10: 55 43 5f 5f 29 0a 23 20 69 66 20 64 65 66 69 6e UC__).# if defin
2c20: 65 64 28 48 41 56 45 5f 53 54 44 49 4e 54 5f 48 ed(HAVE_STDINT_H
2c30: 29 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c ).# define SQL
2c40: 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 58 ITE_INT_TO_PTR(X
2c50: 29 20 20 28 28 76 6f 69 64 2a 29 28 69 6e 74 70 ) ((void*)(intp
2c60: 74 72 5f 74 29 28 58 29 29 0a 23 20 20 20 64 65 tr_t)(X)).# de
2c70: 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 52 5f fine SQLITE_PTR_
2c80: 54 4f 5f 49 4e 54 28 58 29 20 20 28 28 69 6e 74 TO_INT(X) ((int
2c90: 29 28 69 6e 74 70 74 72 5f 74 29 28 58 29 29 0a )(intptr_t)(X)).
2ca0: 23 20 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e # else.# defin
2cb0: 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f e SQLITE_INT_TO_
2cc0: 50 54 52 28 58 29 20 20 28 28 76 6f 69 64 2a 29 PTR(X) ((void*)
2cd0: 28 58 29 29 0a 23 20 20 20 64 65 66 69 6e 65 20 (X)).# define
2ce0: 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e SQLITE_PTR_TO_IN
2cf0: 54 28 58 29 20 20 28 28 69 6e 74 29 28 58 29 29 T(X) ((int)(X))
2d00: 0a 23 20 65 6e 64 69 66 0a 23 65 6c 73 65 0a 23 .# endif.#else.#
2d10: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 define SQLITE_I
2d20: 4e 54 5f 54 4f 5f 50 54 52 28 58 29 20 20 20 28 NT_TO_PTR(X) (
2d30: 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 2a 29 (void*)&((char*)
2d40: 30 29 5b 58 5d 29 0a 23 20 64 65 66 69 6e 65 20 0)[X]).# define
2d50: 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e SQLITE_PTR_TO_IN
2d60: 54 28 58 29 20 20 20 28 28 69 6e 74 29 28 28 28 T(X) ((int)(((
2d70: 63 68 61 72 2a 29 58 29 2d 28 63 68 61 72 2a 29 char*)X)-(char*)
2d80: 30 29 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 0)).#endif.../*.
2d90: 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 54 48 ** The SQLITE_TH
2da0: 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 20 6d READSAFE macro m
2db0: 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 61 ust be defined a
2dc0: 73 20 65 69 74 68 65 72 20 30 20 6f 72 20 31 2e s either 0 or 1.
2dd0: 0a 2a 2a 20 4f 6c 64 65 72 20 76 65 72 73 69 6f .** Older versio
2de0: 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 75 73 65 ns of SQLite use
2df0: 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 54 48 d an optional TH
2e00: 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 2e 0a READSAFE macro..
2e10: 2a 2a 20 57 65 20 73 75 70 70 6f 72 74 20 74 68 ** We support th
2e20: 61 74 20 66 6f 72 20 6c 65 67 61 63 79 0a 2a 2f at for legacy.*/
2e30: 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
2e40: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 29 LITE_THREADSAFE)
2e50: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48 52 .#if defined(THR
2e60: 45 41 44 53 41 46 45 29 0a 23 20 64 65 66 69 6e EADSAFE).# defin
2e70: 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 e SQLITE_THREADS
2e80: 41 46 45 20 54 48 52 45 41 44 53 41 46 45 0a 23 AFE THREADSAFE.#
2e90: 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 else.# define SQ
2ea0: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 LITE_THREADSAFE
2eb0: 31 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 1.#endif.#endif.
2ec0: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 ./*.** The SQLIT
2ed0: 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 E_DEFAULT_MEMSTA
2ee0: 54 55 53 20 6d 61 63 72 6f 20 6d 75 73 74 20 62 TUS macro must b
2ef0: 65 20 64 65 66 69 6e 65 64 20 61 73 20 65 69 74 e defined as eit
2f00: 68 65 72 20 30 20 6f 72 20 31 2e 0a 2a 2a 20 49 her 0 or 1..** I
2f10: 74 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 t determines whe
2f20: 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 ther or not the
2f30: 66 65 61 74 75 72 65 73 20 72 65 6c 61 74 65 64 features related
2f40: 20 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 to .** SQLITE_C
2f50: 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20 ONFIG_MEMSTATUS
2f60: 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 62 79 are available by
2f70: 20 64 65 66 61 75 6c 74 20 6f 72 20 6e 6f 74 2e default or not.
2f80: 20 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e 0a This value can.
2f90: 2a 2a 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e ** be overridden
2fa0: 20 61 74 20 72 75 6e 74 69 6d 65 20 75 73 69 6e at runtime usin
2fb0: 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f g the sqlite3_co
2fc0: 6e 66 69 67 28 29 20 41 50 49 2e 0a 2a 2f 0a 23 nfig() API..*/.#
2fd0: 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
2fe0: 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 TE_DEFAULT_MEMST
2ff0: 41 54 55 53 29 0a 23 20 64 65 66 69 6e 65 20 53 ATUS).# define S
3000: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 QLITE_DEFAULT_ME
3010: 4d 53 54 41 54 55 53 20 31 0a 23 65 6e 64 69 66 MSTATUS 1.#endif
3020: 0a 0a 2f 2a 0a 2a 2a 20 45 78 61 63 74 6c 79 20 ../*.** Exactly
3030: 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f one of the follo
3040: 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 75 73 74 wing macros must
3050: 20 62 65 20 64 65 66 69 6e 65 64 20 69 6e 20 6f be defined in o
3060: 72 64 65 72 20 74 6f 0a 2a 2a 20 73 70 65 63 69 rder to.** speci
3070: 66 79 20 77 68 69 63 68 20 6d 65 6d 6f 72 79 20 fy which memory
3080: 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
3090: 73 74 65 6d 20 74 6f 20 75 73 65 2e 0a 2a 2a 0a stem to use..**.
30a0: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 53 59 ** SQLITE_SY
30b0: 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 STEM_MALLOC
30c0: 20 20 20 20 20 2f 2f 20 55 73 65 20 6e 6f 72 6d // Use norm
30d0: 61 6c 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 al system malloc
30e0: 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 ().** SQLITE
30f0: 5f 4d 45 4d 44 45 42 55 47 20 20 20 20 20 20 20 _MEMDEBUG
3100: 20 20 20 20 20 20 20 20 2f 2f 20 44 65 62 75 67 // Debug
3110: 67 69 6e 67 20 76 65 72 73 69 6f 6e 20 6f 66 20 ging version of
3120: 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 0a system malloc().
3130: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 45 ** SQLITE_ME
3140: 4d 4f 52 59 5f 53 49 5a 45 20 20 20 20 20 20 20 MORY_SIZE
3150: 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c // internal
3160: 20 61 6c 6c 6f 63 61 74 6f 72 20 23 31 0a 2a 2a allocator #1.**
3170: 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 4d 41 50 SQLITE_MMAP
3180: 5f 48 45 41 50 5f 53 49 5a 45 20 20 20 20 20 20 _HEAP_SIZE
3190: 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20 6d // internal m
31a0: 6d 61 70 28 29 20 61 6c 6c 6f 63 61 74 6f 72 0a map() allocator.
31b0: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 50 4f ** SQLITE_PO
31c0: 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 20 W2_MEMORY_SIZE
31d0: 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c // internal
31e0: 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61 6c power-of-two al
31f0: 6c 6f 63 61 74 6f 72 0a 2a 2a 0a 2a 2a 20 49 66 locator.**.** If
3200: 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 6f none of the abo
3210: 76 65 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 ve are defined,
3220: 74 68 65 6e 20 73 65 74 20 53 51 4c 49 54 45 5f then set SQLITE_
3230: 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 61 73 SYSTEM_MALLOC as
3240: 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e .** the default.
3250: 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .*/.#if defined(
3260: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 SQLITE_SYSTEM_MA
3270: 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51 LLOC)+defined(SQ
3280: 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c LITE_MEMDEBUG)+\
3290: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c . defined(SQL
32a0: 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 ITE_MEMORY_SIZE)
32b0: 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f +defined(SQLITE_
32c0: 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b MMAP_HEAP_SIZE)+
32d0: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 \. defined(SQ
32e0: 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 LITE_POW2_MEMORY
32f0: 5f 53 49 5a 45 29 3e 31 0a 23 20 65 72 72 6f 72 _SIZE)>1.# error
3300: 20 22 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66 "At most one of
3310: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 the following c
3320: 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 63 6f 6e 66 ompile-time conf
3330: 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e iguration option
3340: 73 5c 0a 20 69 73 20 61 6c 6c 6f 77 73 3a 20 53 s\. is allows: S
3350: 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c QLITE_SYSTEM_MAL
3360: 4c 4f 43 2c 20 53 51 4c 49 54 45 5f 4d 45 4d 44 LOC, SQLITE_MEMD
3370: 45 42 55 47 2c 20 53 51 4c 49 54 45 5f 4d 45 4d EBUG, SQLITE_MEM
3380: 4f 52 59 5f 53 49 5a 45 2c 5c 0a 20 53 51 4c 49 ORY_SIZE,\. SQLI
3390: 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a TE_MMAP_HEAP_SIZ
33a0: 45 2c 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d E, SQLITE_POW2_M
33b0: 45 4d 4f 52 59 5f 53 49 5a 45 22 0a 23 65 6e 64 EMORY_SIZE".#end
33c0: 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 if.#if defined(S
33d0: 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c QLITE_SYSTEM_MAL
33e0: 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51 4c LOC)+defined(SQL
33f0: 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c 0a ITE_MEMDEBUG)+\.
3400: 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c 49 defined(SQLI
3410: 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 2b TE_MEMORY_SIZE)+
3420: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d defined(SQLITE_M
3430: 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b 5c MAP_HEAP_SIZE)+\
3440: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c . defined(SQL
3450: 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f ITE_POW2_MEMORY_
3460: 53 49 5a 45 29 3d 3d 30 0a 23 20 64 65 66 69 6e SIZE)==0.# defin
3470: 65 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f e SQLITE_SYSTEM_
3480: 4d 41 4c 4c 4f 43 20 31 0a 23 65 6e 64 69 66 0a MALLOC 1.#endif.
3490: 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 ./*.** If SQLITE
34a0: 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d _MALLOC_SOFT_LIM
34b0: 49 54 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 IT is not zero,
34c0: 74 68 65 6e 20 74 72 79 20 74 6f 20 6b 65 65 70 then try to keep
34d0: 20 74 68 65 0a 2a 2a 20 73 69 7a 65 73 20 6f 66 the.** sizes of
34e0: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
34f0: 6f 6e 73 20 62 65 6c 6f 77 20 74 68 69 73 20 76 ons below this v
3500: 61 6c 75 65 20 77 68 65 72 65 20 70 6f 73 73 69 alue where possi
3510: 62 6c 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 ble..*/.#if !def
3520: 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 41 4c 4c ined(SQLITE_MALL
3530: 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 29 0a 23 OC_SOFT_LIMIT).#
3540: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d define SQLITE_M
3550: 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 ALLOC_SOFT_LIMIT
3560: 20 31 30 32 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 1024.#endif../*
3570: 0a 2a 2a 20 57 65 20 6e 65 65 64 20 74 6f 20 64 .** We need to d
3580: 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 efine _XOPEN_SOU
3590: 52 43 45 20 61 73 20 66 6f 6c 6c 6f 77 73 20 69 RCE as follows i
35a0: 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e 61 62 6c n order to enabl
35b0: 65 0a 2a 2a 20 72 65 63 75 72 73 69 76 65 20 6d e.** recursive m
35c0: 75 74 65 78 65 73 20 6f 6e 20 6d 6f 73 74 20 55 utexes on most U
35d0: 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 42 75 nix systems. Bu
35e0: 74 20 4d 61 63 20 4f 53 20 58 20 69 73 20 64 69 t Mac OS X is di
35f0: 66 66 65 72 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 fferent..** The
3600: 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 64 65 _XOPEN_SOURCE de
3610: 66 69 6e 65 20 63 61 75 73 65 73 20 70 72 6f 62 fine causes prob
3620: 6c 65 6d 73 20 66 6f 72 20 4d 61 63 20 4f 53 20 lems for Mac OS
3630: 58 20 77 65 20 61 72 65 20 74 6f 6c 64 2c 0a 2a X we are told,.*
3640: 2a 20 73 6f 20 69 74 20 69 73 20 6f 6d 69 74 74 * so it is omitt
3650: 65 64 20 74 68 65 72 65 2e 20 20 53 65 65 20 74 ed there. See t
3660: 69 63 6b 65 74 20 23 32 36 37 33 2e 0a 2a 2a 0a icket #2673..**.
3670: 2a 2a 20 4c 61 74 65 72 20 77 65 20 6c 65 61 72 ** Later we lear
3680: 6e 20 74 68 61 74 20 5f 58 4f 50 45 4e 5f 53 4f n that _XOPEN_SO
3690: 55 52 43 45 20 69 73 20 70 6f 6f 72 6c 79 20 6f URCE is poorly o
36a0: 72 20 69 6e 63 6f 72 72 65 63 74 6c 79 0a 2a 2a r incorrectly.**
36b0: 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 6f 6e 20 implemented on
36c0: 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e 20 20 53 some systems. S
36d0: 6f 20 77 65 20 61 76 6f 69 64 20 64 65 66 69 6e o we avoid defin
36e0: 69 6e 67 20 69 74 20 61 74 20 61 6c 6c 0a 2a 2a ing it at all.**
36f0: 20 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 if it is alread
3700: 79 20 64 65 66 69 6e 65 64 20 6f 72 20 69 66 20 y defined or if
3710: 69 74 20 69 73 20 75 6e 6e 65 65 64 65 64 20 62 it is unneeded b
3720: 65 63 61 75 73 65 20 77 65 20 61 72 65 0a 2a 2a ecause we are.**
3730: 20 6e 6f 74 20 64 6f 69 6e 67 20 61 20 74 68 72 not doing a thr
3740: 65 61 64 73 61 66 65 20 62 75 69 6c 64 2e 20 20 eadsafe build.
3750: 54 69 63 6b 65 74 20 23 32 36 38 31 2e 0a 2a 2a Ticket #2681..**
3760: 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 69 63 .** See also tic
3770: 6b 65 74 20 23 32 37 34 31 2e 0a 2a 2f 0a 23 69 ket #2741..*/.#i
3780: 66 20 21 64 65 66 69 6e 65 64 28 5f 58 4f 50 45 f !defined(_XOPE
3790: 4e 5f 53 4f 55 52 43 45 29 20 26 26 20 21 64 65 N_SOURCE) && !de
37a0: 66 69 6e 65 64 28 5f 5f 44 41 52 57 49 4e 5f 5f fined(__DARWIN__
37b0: 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f ) && !defined(__
37c0: 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 APPLE__) && SQLI
37d0: 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20 TE_THREADSAFE.#
37e0: 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 define _XOPEN_S
37f0: 4f 55 52 43 45 20 35 30 30 20 20 2f 2a 20 4e 65 OURCE 500 /* Ne
3800: 65 64 65 64 20 74 6f 20 65 6e 61 62 6c 65 20 70 eded to enable p
3810: 74 68 72 65 61 64 20 72 65 63 75 72 73 69 76 65 thread recursive
3820: 20 6d 75 74 65 78 65 73 20 2a 2f 0a 23 65 6e 64 mutexes */.#end
3830: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 43 if../*.** The TC
3840: 4c 20 68 65 61 64 65 72 73 20 61 72 65 20 6f 6e L headers are on
3850: 6c 79 20 6e 65 65 64 65 64 20 77 68 65 6e 20 63 ly needed when c
3860: 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 54 43 4c ompiling the TCL
3870: 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2f 0a 23 69 bindings..*/.#i
3880: 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
3890: 5f 54 43 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 _TCL) || defined
38a0: 28 54 43 4c 53 48 29 0a 23 20 69 6e 63 6c 75 64 (TCLSH).# includ
38b0: 65 20 3c 74 63 6c 2e 68 3e 0a 23 65 6e 64 69 66 e <tcl.h>.#endif
38c0: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e 79 20 70 65 6f ../*.** Many peo
38d0: 70 6c 65 20 61 72 65 20 66 61 69 6c 69 6e 67 20 ple are failing
38e0: 74 6f 20 73 65 74 20 2d 44 4e 44 45 42 55 47 3d to set -DNDEBUG=
38f0: 31 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 1 when compiling
3900: 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 53 65 74 74 SQLite..** Sett
3910: 69 6e 67 20 4e 44 45 42 55 47 20 6d 61 6b 65 73 ing NDEBUG makes
3920: 20 74 68 65 20 63 6f 64 65 20 73 6d 61 6c 6c 65 the code smalle
3930: 72 20 61 6e 64 20 72 75 6e 20 66 61 73 74 65 72 r and run faster
3940: 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 . So the follow
3950: 69 6e 67 0a 2a 2a 20 6c 69 6e 65 73 20 61 72 65 ing.** lines are
3960: 20 61 64 64 65 64 20 74 6f 20 61 75 74 6f 6d 61 added to automa
3970: 74 69 63 61 6c 6c 79 20 73 65 74 20 4e 44 45 42 tically set NDEB
3980: 55 47 20 75 6e 6c 65 73 73 20 74 68 65 20 2d 44 UG unless the -D
3990: 53 51 4c 49 54 45 5f 44 45 42 55 47 3d 31 0a 2a SQLITE_DEBUG=1.*
39a0: 2a 20 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 2e * option is set.
39b0: 20 20 54 68 75 73 20 4e 44 45 42 55 47 20 62 65 Thus NDEBUG be
39c0: 63 6f 6d 65 73 20 61 6e 20 6f 70 74 2d 69 6e 20 comes an opt-in
39d0: 72 61 74 68 65 72 20 74 68 61 6e 20 61 6e 20 6f rather than an o
39e0: 70 74 2d 6f 75 74 0a 2a 2a 20 66 65 61 74 75 72 pt-out.** featur
39f0: 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e e..*/.#if !defin
3a00: 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 21 64 ed(NDEBUG) && !d
3a10: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
3a20: 42 55 47 29 20 0a 23 20 64 65 66 69 6e 65 20 4e BUG) .# define N
3a30: 44 45 42 55 47 20 31 0a 23 65 6e 64 69 66 0a 0a DEBUG 1.#endif..
3a40: 2f 2a 0a 2a 2a 20 54 68 65 20 74 65 73 74 63 61 /*.** The testca
3a50: 73 65 28 29 20 6d 61 63 72 6f 20 69 73 20 75 73 se() macro is us
3a60: 65 64 20 74 6f 20 61 69 64 20 69 6e 20 63 6f 76 ed to aid in cov
3a70: 65 72 61 67 65 20 74 65 73 74 69 6e 67 2e 20 20 erage testing.
3a80: 57 68 65 6e 20 0a 2a 2a 20 64 6f 69 6e 67 20 63 When .** doing c
3a90: 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 2c overage testing,
3aa0: 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 69 the condition i
3ab0: 6e 73 69 64 65 20 74 68 65 20 61 72 67 75 6d 65 nside the argume
3ac0: 6e 74 20 74 6f 0a 2a 2a 20 74 65 73 74 63 61 73 nt to.** testcas
3ad0: 65 28 29 20 6d 75 73 74 20 62 65 20 65 76 61 6c e() must be eval
3ae0: 75 61 74 65 64 20 62 6f 74 68 20 74 72 75 65 20 uated both true
3af0: 61 6e 64 20 66 61 6c 73 65 20 69 6e 20 6f 72 64 and false in ord
3b00: 65 72 20 74 6f 0a 2a 2a 20 67 65 74 20 66 75 6c er to.** get ful
3b10: 6c 20 62 72 61 6e 63 68 20 63 6f 76 65 72 61 67 l branch coverag
3b20: 65 2e 20 20 54 68 65 20 74 65 73 74 63 61 73 65 e. The testcase
3b30: 28 29 20 6d 61 63 72 6f 20 69 73 20 69 6e 73 65 () macro is inse
3b40: 72 74 65 64 0a 2a 2a 20 74 6f 20 68 65 6c 70 20 rted.** to help
3b50: 65 6e 73 75 72 65 20 61 64 65 71 75 61 74 65 20 ensure adequate
3b60: 74 65 73 74 20 63 6f 76 65 72 61 67 65 20 69 6e test coverage in
3b70: 20 70 6c 61 63 65 73 20 77 68 65 72 65 20 73 69 places where si
3b80: 6d 70 6c 65 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f mple.** conditio
3b90: 6e 2f 64 65 63 69 73 69 6f 6e 20 63 6f 76 65 72 n/decision cover
3ba0: 61 67 65 20 69 73 20 69 6e 61 64 65 71 75 61 74 age is inadequat
3bb0: 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c e. For example,
3bc0: 20 74 65 73 74 63 61 73 65 28 29 0a 2a 2a 20 63 testcase().** c
3bd0: 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6d 61 an be used to ma
3be0: 6b 65 20 73 75 72 65 20 62 6f 75 6e 64 61 72 79 ke sure boundary
3bf0: 20 76 61 6c 75 65 73 20 61 72 65 20 74 65 73 74 values are test
3c00: 65 64 2e 20 20 46 6f 72 0a 2a 2a 20 62 69 74 6d ed. For.** bitm
3c10: 61 73 6b 20 74 65 73 74 73 2c 20 74 65 73 74 63 ask tests, testc
3c20: 61 73 65 28 29 20 63 61 6e 20 62 65 20 75 73 65 ase() can be use
3c30: 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 65 d to make sure e
3c40: 61 63 68 20 62 69 74 0a 2a 2a 20 69 73 20 73 69 ach bit.** is si
3c50: 67 6e 69 66 69 63 61 6e 74 20 61 6e 64 20 75 73 gnificant and us
3c60: 65 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 63 65 ed at least once
3c70: 2e 20 20 4f 6e 20 73 77 69 74 63 68 20 73 74 61 . On switch sta
3c80: 74 65 6d 65 6e 74 73 0a 2a 2a 20 77 68 65 72 65 tements.** where
3c90: 20 6d 75 6c 74 69 70 6c 65 20 63 61 73 65 73 20 multiple cases
3ca0: 67 6f 20 74 6f 20 74 68 65 20 73 61 6d 65 20 62 go to the same b
3cb0: 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 2c 20 74 65 lock of code, te
3cc0: 73 74 63 61 73 65 28 29 0a 2a 2a 20 63 61 6e 20 stcase().** can
3cd0: 69 6e 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20 insure that all
3ce0: 63 61 73 65 73 20 61 72 65 20 65 76 61 6c 75 61 cases are evalua
3cf0: 74 65 64 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 64 65 ted..**.*/.#ifde
3d00: 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 f SQLITE_COVERAG
3d10: 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 E_TEST.SQLITE_PR
3d20: 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
3d30: 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e 74 ite3Coverage(int
3d40: 29 3b 0a 23 20 64 65 66 69 6e 65 20 74 65 73 74 );.# define test
3d50: 63 61 73 65 28 58 29 20 20 69 66 28 20 58 20 29 case(X) if( X )
3d60: 7b 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67 { sqlite3Coverag
3d70: 65 28 5f 5f 4c 49 4e 45 5f 5f 29 3b 20 7d 0a 23 e(__LINE__); }.#
3d80: 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 74 65 else.# define te
3d90: 73 74 63 61 73 65 28 58 29 0a 23 65 6e 64 69 66 stcase(X).#endif
3da0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 45 53 54 ../*.** The TEST
3db0: 4f 4e 4c 59 20 6d 61 63 72 6f 20 69 73 20 75 73 ONLY macro is us
3dc0: 65 64 20 74 6f 20 65 6e 63 6c 6f 73 65 20 76 61 ed to enclose va
3dd0: 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74 69 riable declarati
3de0: 6f 6e 73 20 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 ons or.** other
3df0: 62 69 74 73 20 6f 66 20 63 6f 64 65 20 74 68 61 bits of code tha
3e00: 74 20 61 72 65 20 6e 65 65 64 65 64 20 74 6f 20 t are needed to
3e10: 73 75 70 70 6f 72 74 20 74 68 65 20 61 72 67 75 support the argu
3e20: 6d 65 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 ments.** within
3e30: 74 65 73 74 63 61 73 65 28 29 20 61 6e 64 20 61 testcase() and a
3e40: 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 2e 0a ssert() macros..
3e50: 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 */.#if !defined(
3e60: 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e NDEBUG) || defin
3e70: 65 64 28 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 ed(SQLITE_COVERA
3e80: 47 45 5f 54 45 53 54 29 0a 23 20 64 65 66 69 6e GE_TEST).# defin
3e90: 65 20 54 45 53 54 4f 4e 4c 59 28 58 29 20 20 58 e TESTONLY(X) X
3ea0: 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
3eb0: 54 45 53 54 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 TESTONLY(X).#end
3ec0: 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 69 if../*.** Someti
3ed0: 6d 65 73 20 77 65 20 6e 65 65 64 20 61 20 73 6d mes we need a sm
3ee0: 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 63 6f all amount of co
3ef0: 64 65 20 73 75 63 68 20 61 73 20 61 20 76 61 72 de such as a var
3f00: 69 61 62 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 iable initializa
3f10: 74 69 6f 6e 0a 2a 2a 20 74 6f 20 73 65 74 75 70 tion.** to setup
3f20: 20 66 6f 72 20 61 20 6c 61 74 65 72 20 61 73 73 for a later ass
3f30: 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 2e ert() statement.
3f40: 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 We do not want
3f50: 20 74 68 69 73 20 63 6f 64 65 20 74 6f 0a 2a 2a this code to.**
3f60: 20 61 70 70 65 61 72 20 77 68 65 6e 20 61 73 73 appear when ass
3f70: 65 72 74 28 29 20 69 73 20 64 69 73 61 62 6c 65 ert() is disable
3f80: 64 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e d. The followin
3f90: 67 20 6d 61 63 72 6f 20 69 73 20 74 68 65 72 65 g macro is there
3fa0: 66 6f 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 fore.** used to
3fb0: 63 6f 6e 74 61 69 6e 20 74 68 61 74 20 73 65 74 contain that set
3fc0: 75 70 20 63 6f 64 65 2e 20 20 54 68 65 20 22 56 up code. The "V
3fd0: 56 41 22 20 61 63 72 6f 6e 79 6d 20 73 74 61 6e VA" acronym stan
3fe0: 64 73 20 66 6f 72 0a 2a 2a 20 22 56 65 72 69 66 ds for.** "Verif
3ff0: 69 63 61 74 69 6f 6e 2c 20 56 61 6c 69 64 61 74 ication, Validat
4000: 69 6f 6e 2c 20 61 6e 64 20 41 63 63 72 65 64 69 ion, and Accredi
4010: 74 61 74 69 6f 6e 22 2e 20 20 49 6e 20 6f 74 68 tation". In oth
4020: 65 72 20 77 6f 72 64 73 2c 20 74 68 65 0a 2a 2a er words, the.**
4030: 20 63 6f 64 65 20 77 69 74 68 69 6e 20 56 56 41 code within VVA
4040: 5f 4f 4e 4c 59 28 29 20 77 69 6c 6c 20 6f 6e 6c _ONLY() will onl
4050: 79 20 72 75 6e 20 64 75 72 69 6e 67 20 76 65 72 y run during ver
4060: 69 66 69 63 61 74 69 6f 6e 20 70 72 6f 63 65 73 ification proces
4070: 73 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ses..*/.#ifndef
4080: 4e 44 45 42 55 47 0a 23 20 64 65 66 69 6e 65 20 NDEBUG.# define
4090: 56 56 41 5f 4f 4e 4c 59 28 58 29 20 20 58 0a 23 VVA_ONLY(X) X.#
40a0: 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56 56 else.# define VV
40b0: 41 5f 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69 66 A_ONLY(X).#endif
40c0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 41 4c 57 41 ../*.** The ALWA
40d0: 59 53 20 61 6e 64 20 4e 45 56 45 52 20 6d 61 63 YS and NEVER mac
40e0: 72 6f 73 20 73 75 72 72 6f 75 6e 64 20 62 6f 6f ros surround boo
40f0: 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 lean expressions
4100: 20 77 68 69 63 68 20 0a 2a 2a 20 61 72 65 20 69 which .** are i
4110: 6e 74 65 6e 64 65 64 20 74 6f 20 61 6c 77 61 79 ntended to alway
4120: 73 20 62 65 20 74 72 75 65 20 6f 72 20 66 61 6c s be true or fal
4130: 73 65 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 se, respectively
4140: 2e 20 20 53 75 63 68 0a 2a 2a 20 65 78 70 72 65 . Such.** expre
4150: 73 73 69 6f 6e 73 20 63 6f 75 6c 64 20 62 65 20 ssions could be
4160: 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65 omitted from the
4170: 20 63 6f 64 65 20 63 6f 6d 70 6c 65 74 65 6c 79 code completely
4180: 2e 20 20 42 75 74 20 74 68 65 79 0a 2a 2a 20 61 . But they.** a
4190: 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 61 re included in a
41a0: 20 66 65 77 20 63 61 73 65 73 20 69 6e 20 6f 72 few cases in or
41b0: 64 65 72 20 74 6f 20 65 6e 68 61 6e 63 65 20 74 der to enhance t
41c0: 68 65 20 72 65 73 69 6c 69 65 6e 63 65 0a 2a 2a he resilience.**
41d0: 20 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 75 6e of SQLite to un
41e0: 65 78 70 65 63 74 65 64 20 62 65 68 61 76 69 6f expected behavio
41f0: 72 20 2d 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 r - to make the
4200: 63 6f 64 65 20 22 73 65 6c 66 2d 68 65 61 6c 69 code "self-heali
4210: 6e 67 22 0a 2a 2a 20 6f 72 20 22 64 75 63 74 69 ng".** or "ducti
4220: 6c 65 22 20 72 61 74 68 65 72 20 74 68 61 6e 20 le" rather than
4230: 62 65 69 6e 67 20 22 62 72 69 74 74 6c 65 22 20 being "brittle"
4240: 61 6e 64 20 63 72 61 73 68 69 6e 67 20 61 74 20 and crashing at
4250: 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 68 69 6e the first.** hin
4260: 74 20 6f 66 20 75 6e 70 6c 61 6e 6e 65 64 20 62 t of unplanned b
4270: 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 ehavior..**.** I
4280: 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 41 n other words, A
4290: 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 LWAYS and NEVER
42a0: 61 72 65 20 61 64 64 65 64 20 66 6f 72 20 64 65 are added for de
42b0: 66 65 6e 73 69 76 65 20 63 6f 64 65 2e 0a 2a 2a fensive code..**
42c0: 0a 2a 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 63 .** When doing c
42d0: 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 20 overage testing
42e0: 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52 ALWAYS and NEVER
42f0: 20 61 72 65 20 68 61 72 64 2d 63 6f 64 65 64 20 are hard-coded
4300: 74 6f 0a 2a 2a 20 62 65 20 74 72 75 65 20 61 6e to.** be true an
4310: 64 20 66 61 6c 73 65 20 73 6f 20 74 68 61 74 20 d false so that
4320: 74 68 65 20 75 6e 72 65 61 63 68 61 62 6c 65 20 the unreachable
4330: 63 6f 64 65 20 74 68 65 6e 20 73 70 65 63 69 66 code then specif
4340: 79 20 77 69 6c 6c 0a 2a 2a 20 6e 6f 74 20 62 65 y will.** not be
4350: 20 63 6f 75 6e 74 65 64 20 61 73 20 75 6e 74 65 counted as unte
4360: 73 74 65 64 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 sted code..*/.#i
4370: 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
4380: 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29 0a _COVERAGE_TEST).
4390: 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 # define ALWAYS(
43a0: 58 29 20 20 20 20 20 20 28 31 29 0a 23 20 64 65 X) (1).# de
43b0: 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 fine NEVER(X)
43c0: 20 20 20 20 28 30 29 0a 23 65 6c 69 66 20 21 64 (0).#elif !d
43d0: 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 23 efined(NDEBUG).#
43e0: 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58 define ALWAYS(X
43f0: 29 20 20 20 20 20 20 28 28 58 29 3f 31 3a 28 61 ) ((X)?1:(a
4400: 73 73 65 72 74 28 30 29 2c 30 29 29 0a 23 20 64 ssert(0),0)).# d
4410: 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 efine NEVER(X)
4420: 20 20 20 20 20 28 28 58 29 3f 28 61 73 73 65 72 ((X)?(asser
4430: 74 28 30 29 2c 31 29 3a 30 29 0a 23 65 6c 73 65 t(0),1):0).#else
4440: 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 .# define ALWAYS
4450: 28 58 29 20 20 20 20 20 20 28 58 29 0a 23 20 64 (X) (X).# d
4460: 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 efine NEVER(X)
4470: 20 20 20 20 20 28 58 29 0a 23 65 6e 64 69 66 0a (X).#endif.
4480: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f ./*.** The macro
4490: 20 75 6e 6c 69 6b 65 6c 79 28 29 20 69 73 20 61 unlikely() is a
44a0: 20 68 69 6e 74 20 74 68 61 74 20 73 75 72 72 6f hint that surro
44b0: 75 6e 64 73 20 61 20 62 6f 6f 6c 65 61 6e 0a 2a unds a boolean.*
44c0: 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 * expression tha
44d0: 74 20 69 73 20 75 73 75 61 6c 6c 79 20 66 61 6c t is usually fal
44e0: 73 65 2e 20 20 4d 61 63 72 6f 20 6c 69 6b 65 6c se. Macro likel
44f0: 79 28 29 20 73 75 72 72 6f 75 6e 64 73 0a 2a 2a y() surrounds.**
4500: 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 a boolean expre
4510: 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 75 73 ssion that is us
4520: 75 61 6c 6c 79 20 74 72 75 65 2e 20 20 47 43 43 ually true. GCC
4530: 20 69 73 20 61 62 6c 65 20 74 6f 0a 2a 2a 20 75 is able to.** u
4540: 73 65 20 74 68 65 73 65 20 68 69 6e 74 73 20 74 se these hints t
4550: 6f 20 67 65 6e 65 72 61 74 65 20 62 65 74 74 65 o generate bette
4560: 72 20 63 6f 64 65 2c 20 73 6f 6d 65 74 69 6d 65 r code, sometime
4570: 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 s..*/.#if define
4580: 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 30 d(__GNUC__) && 0
4590: 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c 79 .# define likely
45a0: 28 58 29 20 20 20 20 5f 5f 62 75 69 6c 74 69 6e (X) __builtin
45b0: 5f 65 78 70 65 63 74 28 28 58 29 2c 31 29 0a 23 _expect((X),1).#
45c0: 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 define unlikely
45d0: 28 58 29 20 20 5f 5f 62 75 69 6c 74 69 6e 5f 65 (X) __builtin_e
45e0: 78 70 65 63 74 28 28 58 29 2c 30 29 0a 23 65 6c xpect((X),0).#el
45f0: 73 65 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 se.# define like
4600: 6c 79 28 58 29 20 20 20 20 21 21 28 58 29 0a 23 ly(X) !!(X).#
4610: 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 define unlikely
4620: 28 58 29 20 20 21 21 28 58 29 0a 23 65 6e 64 69 (X) !!(X).#endi
4630: 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f../************
4640: 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 ** Include sqlit
4650: 65 33 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 e3.h in the midd
4660: 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e le of sqliteInt.
4670: 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
4680: 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
4690: 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 ** Begin file sq
46a0: 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a lite3.h ********
46b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46d0: 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 */./*.** 2001 Se
46e0: 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a ptember 15.**.**
46f0: 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
4700: 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
4710: 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
4720: 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
4730: 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
4740: 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
4750: 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
4760: 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
4770: 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
4780: 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
4790: 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
47a0: 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
47b0: 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
47c0: 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
47d0: 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
47e0: 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
47f0: 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
4800: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4810: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4820: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4830: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4840: 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
4850: 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 s header file de
4860: 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 fines the interf
4870: 61 63 65 20 74 68 61 74 20 74 68 65 20 53 51 4c ace that the SQL
4880: 69 74 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 70 ite library.** p
4890: 72 65 73 65 6e 74 73 20 74 6f 20 63 6c 69 65 6e resents to clien
48a0: 74 20 70 72 6f 67 72 61 6d 73 2e 20 20 49 66 20 t programs. If
48b0: 61 20 43 2d 66 75 6e 63 74 69 6f 6e 2c 20 73 74 a C-function, st
48c0: 72 75 63 74 75 72 65 2c 20 64 61 74 61 74 79 70 ructure, datatyp
48d0: 65 2c 0a 2a 2a 20 6f 72 20 63 6f 6e 73 74 61 6e e,.** or constan
48e0: 74 20 64 65 66 69 6e 69 74 69 6f 6e 20 64 6f 65 t definition doe
48f0: 73 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e 20 s not appear in
4900: 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e 20 this file, then
4910: 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 70 it is.** not a p
4920: 75 62 6c 69 73 68 65 64 20 41 50 49 20 6f 66 20 ublished API of
4930: 53 51 4c 69 74 65 2c 20 69 73 20 73 75 62 6a 65 SQLite, is subje
4940: 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 ct to change wit
4950: 68 6f 75 74 0a 2a 2a 20 6e 6f 74 69 63 65 2c 20 hout.** notice,
4960: 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 and should not b
4970: 65 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 e referenced by
4980: 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20 75 73 programs that us
4990: 65 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 e SQLite..**.**
49a0: 53 6f 6d 65 20 6f 66 20 74 68 65 20 64 65 66 69 Some of the defi
49b0: 6e 69 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 nitions that are
49c0: 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 61 72 in this file ar
49d0: 65 20 6d 61 72 6b 65 64 20 61 73 0a 2a 2a 20 22 e marked as.** "
49e0: 65 78 70 65 72 69 6d 65 6e 74 61 6c 22 2e 20 20 experimental".
49f0: 45 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 Experimental int
4a00: 65 72 66 61 63 65 73 20 61 72 65 20 6e 6f 72 6d erfaces are norm
4a10: 61 6c 6c 79 20 6e 65 77 0a 2a 2a 20 66 65 61 74 ally new.** feat
4a20: 75 72 65 73 20 72 65 63 65 6e 74 6c 79 20 61 64 ures recently ad
4a30: 64 65 64 20 74 6f 20 53 51 4c 69 74 65 2e 20 20 ded to SQLite.
4a40: 57 65 20 64 6f 20 6e 6f 74 20 61 6e 74 69 63 69 We do not antici
4a50: 70 61 74 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 pate changes.**
4a60: 74 6f 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 to experimental
4a70: 69 6e 74 65 72 66 61 63 65 73 20 62 75 74 20 72 interfaces but r
4a80: 65 73 65 72 76 65 20 74 68 65 20 72 69 67 68 74 eserve the right
4a90: 20 74 6f 20 6d 61 6b 65 20 6d 69 6e 6f 72 20 63 to make minor c
4aa0: 68 61 6e 67 65 73 0a 2a 2a 20 69 66 20 65 78 70 hanges.** if exp
4ab0: 65 72 69 65 6e 63 65 20 66 72 6f 6d 20 75 73 65 erience from use
4ac0: 20 22 69 6e 20 74 68 65 20 77 69 6c 64 22 20 73 "in the wild" s
4ad0: 75 67 67 65 73 74 20 73 75 63 68 20 63 68 61 6e uggest such chan
4ae0: 67 65 73 20 61 72 65 20 70 72 75 64 65 6e 74 2e ges are prudent.
4af0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 66 66 69 63 .**.** The offic
4b00: 69 61 6c 20 43 2d 6c 61 6e 67 75 61 67 65 20 41 ial C-language A
4b10: 50 49 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e PI documentation
4b20: 20 66 6f 72 20 53 51 4c 69 74 65 20 69 73 20 64 for SQLite is d
4b30: 65 72 69 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 63 erived.** from c
4b40: 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 69 73 20 omments in this
4b50: 66 69 6c 65 2e 20 20 54 68 69 73 20 66 69 6c 65 file. This file
4b60: 20 69 73 20 74 68 65 20 61 75 74 68 6f 72 69 74 is the authorit
4b70: 61 74 69 76 65 20 73 6f 75 72 63 65 0a 2a 2a 20 ative source.**
4b80: 6f 6e 20 68 6f 77 20 53 51 4c 69 74 65 20 69 6e on how SQLite in
4b90: 74 65 72 66 61 63 65 73 20 61 72 65 20 73 75 70 terfaces are sup
4ba0: 70 6f 73 65 20 74 6f 20 6f 70 65 72 61 74 65 2e pose to operate.
4bb0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 .**.** The name
4bc0: 6f 66 20 74 68 69 73 20 66 69 6c 65 20 75 6e 64 of this file und
4bd0: 65 72 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e er configuration
4be0: 20 6d 61 6e 61 67 65 6d 65 6e 74 20 69 73 20 22 management is "
4bf0: 73 71 6c 69 74 65 2e 68 2e 69 6e 22 2e 0a 2a 2a sqlite.h.in"..**
4c00: 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 6d 61 The makefile ma
4c10: 6b 65 73 20 73 6f 6d 65 20 6d 69 6e 6f 72 20 63 kes some minor c
4c20: 68 61 6e 67 65 73 20 74 6f 20 74 68 69 73 20 66 hanges to this f
4c30: 69 6c 65 20 28 73 75 63 68 20 61 73 20 69 6e 73 ile (such as ins
4c40: 65 72 74 69 6e 67 0a 2a 2a 20 74 68 65 20 76 65 erting.** the ve
4c50: 72 73 69 6f 6e 20 6e 75 6d 62 65 72 29 20 61 6e rsion number) an
4c60: 64 20 63 68 61 6e 67 65 73 20 69 74 73 20 6e 61 d changes its na
4c70: 6d 65 20 74 6f 20 22 73 71 6c 69 74 65 33 2e 68 me to "sqlite3.h
4c80: 22 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 " as.** part of
4c90: 74 68 65 20 62 75 69 6c 64 20 70 72 6f 63 65 73 the build proces
4ca0: 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 s..*/.#ifndef _S
4cb0: 51 4c 49 54 45 33 5f 48 5f 0a 23 64 65 66 69 6e QLITE3_H_.#defin
4cc0: 65 20 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23 69 e _SQLITE3_H_.#i
4cd0: 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 67 2e 68 nclude <stdarg.h
4ce0: 3e 20 20 20 20 20 2f 2a 20 4e 65 65 64 65 64 20 > /* Needed
4cf0: 66 6f 72 20 74 68 65 20 64 65 66 69 6e 69 74 69 for the definiti
4d00: 6f 6e 20 6f 66 20 76 61 5f 6c 69 73 74 20 2a 2f on of va_list */
4d10: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 ../*.** Make sur
4d20: 65 20 77 65 20 63 61 6e 20 63 61 6c 6c 20 74 68 e we can call th
4d30: 69 73 20 73 74 75 66 66 20 66 72 6f 6d 20 43 2b is stuff from C+
4d40: 2b 2e 0a 2a 2f 0a 23 69 66 20 30 0a 65 78 74 65 +..*/.#if 0.exte
4d50: 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 0a rn "C" {.#endif.
4d60: 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 ../*.** Add the
4d70: 61 62 69 6c 69 74 79 20 74 6f 20 6f 76 65 72 72 ability to overr
4d80: 69 64 65 20 27 65 78 74 65 72 6e 27 0a 2a 2f 0a ide 'extern'.*/.
4d90: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 45 #ifndef SQLITE_E
4da0: 58 54 45 52 4e 0a 23 20 64 65 66 69 6e 65 20 53 XTERN.# define S
4db0: 51 4c 49 54 45 5f 45 58 54 45 52 4e 20 65 78 74 QLITE_EXTERN ext
4dc0: 65 72 6e 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e ern.#endif..#ifn
4dd0: 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0a 23 def SQLITE_API.#
4de0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 define SQLITE_A
4df0: 50 49 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a PI.#endif.../*.*
4e00: 2a 20 54 68 65 73 65 20 6e 6f 2d 6f 70 20 6d 61 * These no-op ma
4e10: 63 72 6f 73 20 61 72 65 20 75 73 65 64 20 69 6e cros are used in
4e20: 20 66 72 6f 6e 74 20 6f 66 20 69 6e 74 65 72 66 front of interf
4e30: 61 63 65 73 20 74 6f 20 6d 61 72 6b 20 74 68 6f aces to mark tho
4e40: 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 se.** interfaces
4e50: 20 61 73 20 65 69 74 68 65 72 20 64 65 70 72 65 as either depre
4e60: 63 61 74 65 64 20 6f 72 20 65 78 70 65 72 69 6d cated or experim
4e70: 65 6e 74 61 6c 2e 20 20 4e 65 77 20 61 70 70 6c ental. New appl
4e80: 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 ications.** shou
4e90: 6c 64 20 6e 6f 74 20 75 73 65 20 64 65 70 72 65 ld not use depre
4ea0: 63 61 74 65 64 20 69 6e 74 65 72 66 61 63 65 73 cated interfaces
4eb0: 20 2d 20 74 68 65 79 20 61 72 65 20 73 75 70 70 - they are supp
4ec0: 6f 72 74 20 66 6f 72 20 62 61 63 6b 77 61 72 64 ort for backward
4ed0: 73 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 s.** compatibili
4ee0: 74 79 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 ty only. Applic
4ef0: 61 74 69 6f 6e 20 77 72 69 74 65 72 73 20 73 68 ation writers sh
4f00: 6f 75 6c 64 20 62 65 20 61 77 61 72 65 20 74 68 ould be aware th
4f10: 61 74 0a 2a 2a 20 65 78 70 65 72 69 6d 65 6e 74 at.** experiment
4f20: 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 al interfaces ar
4f30: 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 e subject to cha
4f40: 6e 67 65 20 69 6e 20 70 6f 69 6e 74 20 72 65 6c nge in point rel
4f50: 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 eases..**.** The
4f60: 73 65 20 6d 61 63 72 6f 73 20 75 73 65 64 20 74 se macros used t
4f70: 6f 20 72 65 73 6f 6c 76 65 20 74 6f 20 76 61 72 o resolve to var
4f80: 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66 20 63 6f ious kinds of co
4f90: 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 74 68 61 mpiler magic tha
4fa0: 74 0a 2a 2a 20 77 6f 75 6c 64 20 67 65 6e 65 72 t.** would gener
4fb0: 61 74 65 20 77 61 72 6e 69 6e 67 20 6d 65 73 73 ate warning mess
4fc0: 61 67 65 73 20 77 68 65 6e 20 74 68 65 79 20 77 ages when they w
4fd0: 65 72 65 20 75 73 65 64 2e 20 20 42 75 74 20 74 ere used. But t
4fe0: 68 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 hat.** compiler
4ff0: 6d 61 67 69 63 20 65 6e 64 65 64 20 75 70 20 67 magic ended up g
5000: 65 6e 65 72 61 74 69 6e 67 20 73 75 63 68 20 61 enerating such a
5010: 20 66 6c 75 72 72 79 20 6f 66 20 62 75 67 20 72 flurry of bug r
5020: 65 70 6f 72 74 73 0a 2a 2a 20 74 68 61 74 20 77 eports.** that w
5030: 65 20 68 61 76 65 20 74 61 6b 65 6e 20 69 74 20 e have taken it
5040: 61 6c 6c 20 6f 75 74 20 61 6e 64 20 67 6f 6e 65 all out and gone
5050: 20 62 61 63 6b 20 74 6f 20 75 73 69 6e 67 20 73 back to using s
5060: 69 6d 70 6c 65 0a 2a 2a 20 6e 6f 6f 70 20 6d 61 imple.** noop ma
5070: 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 cros..*/.#define
5080: 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 SQLITE_DEPRECAT
5090: 45 44 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 ED.#define SQLIT
50a0: 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 0a E_EXPERIMENTAL..
50b0: 2f 2a 0a 2a 2a 20 45 6e 73 75 72 65 20 74 68 65 /*.** Ensure the
50c0: 73 65 20 73 79 6d 62 6f 6c 73 20 77 65 72 65 20 se symbols were
50d0: 6e 6f 74 20 64 65 66 69 6e 65 64 20 62 79 20 73 not defined by s
50e0: 6f 6d 65 20 70 72 65 76 69 6f 75 73 20 68 65 61 ome previous hea
50f0: 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 der file..*/.#if
5100: 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49 def SQLITE_VERSI
5110: 4f 4e 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 ON.# undef SQLIT
5120: 45 5f 56 45 52 53 49 4f 4e 0a 23 65 6e 64 69 66 E_VERSION.#endif
5130: 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 56 .#ifdef SQLITE_V
5140: 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 20 ERSION_NUMBER.#
5150: 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 undef SQLITE_VER
5160: 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 65 6e 64 SION_NUMBER.#end
5170: 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 if../*.** CAPI3R
5180: 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 EF: Compile-Time
5190: 20 4c 69 62 72 61 72 79 20 56 65 72 73 69 6f 6e Library Version
51a0: 20 4e 75 6d 62 65 72 73 20 7b 48 31 30 30 31 30 Numbers {H10010
51b0: 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 2a } <S60100>.**.**
51c0: 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53 The SQLITE_VERS
51d0: 49 4f 4e 20 61 6e 64 20 53 51 4c 49 54 45 5f 56 ION and SQLITE_V
51e0: 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 23 64 ERSION_NUMBER #d
51f0: 65 66 69 6e 65 73 20 69 6e 0a 2a 2a 20 74 68 65 efines in.** the
5200: 20 73 71 6c 69 74 65 33 2e 68 20 66 69 6c 65 20 sqlite3.h file
5210: 73 70 65 63 69 66 79 20 74 68 65 20 76 65 72 73 specify the vers
5220: 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 77 69 ion of SQLite wi
5230: 74 68 20 77 68 69 63 68 0a 2a 2a 20 74 68 61 74 th which.** that
5240: 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20 header file is
5250: 61 73 73 6f 63 69 61 74 65 64 2e 0a 2a 2a 0a 2a associated..**.*
5260: 2a 20 54 68 65 20 22 76 65 72 73 69 6f 6e 22 20 * The "version"
5270: 6f 66 20 53 51 4c 69 74 65 20 69 73 20 61 20 73 of SQLite is a s
5280: 74 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6f 72 tring of the for
5290: 6d 20 22 57 2e 58 2e 59 22 20 6f 72 20 22 57 2e m "W.X.Y" or "W.
52a0: 58 2e 59 2e 5a 22 2e 0a 2a 2a 20 54 68 65 20 57 X.Y.Z"..** The W
52b0: 20 76 61 6c 75 65 20 69 73 20 6d 61 6a 6f 72 20 value is major
52c0: 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 61 version number a
52d0: 6e 64 20 69 73 20 61 6c 77 61 79 73 20 33 20 69 nd is always 3 i
52e0: 6e 20 53 51 4c 69 74 65 33 2e 0a 2a 2a 20 54 68 n SQLite3..** Th
52f0: 65 20 57 20 76 61 6c 75 65 20 6f 6e 6c 79 20 63 e W value only c
5300: 68 61 6e 67 65 73 20 77 68 65 6e 20 62 61 63 6b hanges when back
5310: 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c wards compatibil
5320: 69 74 79 20 69 73 0a 2a 2a 20 62 72 6f 6b 65 6e ity is.** broken
5330: 20 61 6e 64 20 77 65 20 69 6e 74 65 6e 64 20 74 and we intend t
5340: 6f 20 6e 65 76 65 72 20 62 72 65 61 6b 20 62 61 o never break ba
5350: 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 ckwards compatib
5360: 69 6c 69 74 79 2e 0a 2a 2a 20 54 68 65 20 58 20 ility..** The X
5370: 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d 69 6e value is the min
5380: 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 or version numbe
5390: 72 20 61 6e 64 20 6f 6e 6c 79 20 63 68 61 6e 67 r and only chang
53a0: 65 73 20 77 68 65 6e 0a 2a 2a 20 74 68 65 72 65 es when.** there
53b0: 20 61 72 65 20 6d 61 6a 6f 72 20 66 65 61 74 75 are major featu
53c0: 72 65 20 65 6e 68 61 6e 63 65 6d 65 6e 74 73 20 re enhancements
53d0: 74 68 61 74 20 61 72 65 20 66 6f 72 77 61 72 64 that are forward
53e0: 73 20 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 s compatible.**
53f0: 62 75 74 20 6e 6f 74 20 62 61 63 6b 77 61 72 64 but not backward
5400: 73 20 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a 2a s compatible..**
5410: 20 54 68 65 20 59 20 76 61 6c 75 65 20 69 73 20 The Y value is
5420: 74 68 65 20 72 65 6c 65 61 73 65 20 6e 75 6d 62 the release numb
5430: 65 72 20 61 6e 64 20 69 73 20 69 6e 63 72 65 6d er and is increm
5440: 65 6e 74 65 64 20 77 69 74 68 0a 2a 2a 20 65 61 ented with.** ea
5450: 63 68 20 72 65 6c 65 61 73 65 20 62 75 74 20 72 ch release but r
5460: 65 73 65 74 73 20 62 61 63 6b 20 74 6f 20 30 20 esets back to 0
5470: 77 68 65 6e 65 76 65 72 20 58 20 69 73 20 69 6e whenever X is in
5480: 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a 20 54 68 cremented..** Th
5490: 65 20 5a 20 76 61 6c 75 65 20 6f 6e 6c 79 20 61 e Z value only a
54a0: 70 70 65 61 72 73 20 6f 6e 20 62 72 61 6e 63 68 ppears on branch
54b0: 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a releases..**.**
54c0: 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53 The SQLITE_VERS
54d0: 49 4f 4e 5f 4e 55 4d 42 45 52 20 69 73 20 61 6e ION_NUMBER is an
54e0: 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 integer that is
54f0: 20 63 6f 6d 70 75 74 65 64 20 61 73 0a 2a 2a 20 computed as.**
5500: 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c follows:.**.** <
5510: 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e blockquote><pre>
5520: 0a 2a 2a 20 53 51 4c 49 54 45 5f 56 45 52 53 49 .** SQLITE_VERSI
5530: 4f 4e 5f 4e 55 4d 42 45 52 20 3d 20 57 2a 31 30 ON_NUMBER = W*10
5540: 30 30 30 30 30 20 2b 20 58 2a 31 30 30 30 20 2b 00000 + X*1000 +
5550: 20 59 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c Y.** </pre></bl
5560: 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 ockquote>.**.**
5570: 53 69 6e 63 65 20 76 65 72 73 69 6f 6e 20 33 2e Since version 3.
5580: 36 2e 31 38 2c 20 53 51 4c 69 74 65 20 73 6f 75 6.18, SQLite sou
5590: 72 63 65 20 63 6f 64 65 20 68 61 73 20 62 65 65 rce code has bee
55a0: 6e 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a n stored in the.
55b0: 2a 2a 20 3c 61 20 68 72 65 66 3d 22 68 74 74 70 ** <a href="http
55c0: 3a 2f 2f 77 77 77 2e 66 6f 73 73 69 6c 2d 73 63 ://www.fossil-sc
55d0: 6d 2e 6f 72 67 2f 22 3e 66 6f 73 73 69 6c 20 63 m.org/">fossil c
55e0: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d 61 6e onfiguration man
55f0: 61 67 65 6d 65 6e 74 0a 2a 2a 20 73 79 73 74 65 agement.** syste
5600: 6d 3c 2f 61 3e 2e 20 20 54 68 65 20 53 51 4c 49 m</a>. The SQLI
5610: 54 45 5f 53 4f 55 52 43 45 5f 49 44 0a 2a 2a 20 TE_SOURCE_ID.**
5620: 6d 61 63 72 6f 20 69 73 20 61 20 73 74 72 69 6e macro is a strin
5630: 67 20 77 68 69 63 68 20 69 64 65 6e 74 69 66 69 g which identifi
5640: 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 es a particular
5650: 63 68 65 63 6b 2d 69 6e 20 6f 66 20 53 51 4c 69 check-in of SQLi
5660: 74 65 0a 2a 2a 20 77 69 74 68 69 6e 20 69 74 73 te.** within its
5670: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d configuration m
5680: 61 6e 61 67 65 6d 65 6e 74 20 73 79 73 74 65 6d anagement system
5690: 2e 20 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f . The string co
56a0: 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 64 61 ntains the.** da
56b0: 74 65 20 61 6e 64 20 74 69 6d 65 20 6f 66 20 74 te and time of t
56c0: 68 65 20 63 68 65 63 6b 2d 69 6e 20 28 55 54 43 he check-in (UTC
56d0: 29 20 61 6e 64 20 61 6e 20 53 48 41 31 20 68 61 ) and an SHA1 ha
56e0: 73 68 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65 sh of the entire
56f0: 0a 2a 2a 20 73 6f 75 72 63 65 20 74 72 65 65 2e .** source tree.
5700: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
5710: 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 [sqlite3_libver
5720: 73 69 6f 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c sion()],.** [sql
5730: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f ite3_libversion_
5740: 6e 75 6d 62 65 72 28 29 5d 2c 20 5b 73 71 6c 69 number()], [sqli
5750: 74 65 33 5f 73 6f 75 72 63 65 69 64 28 29 5d 2c te3_sourceid()],
5760: 0a 2a 2a 20 5b 73 71 6c 69 74 65 5f 76 65 72 73 .** [sqlite_vers
5770: 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 ion()] and [sqli
5780: 74 65 5f 73 6f 75 72 63 65 5f 69 64 28 29 5d 2e te_source_id()].
5790: 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
57a0: 6e 74 73 3a 20 5b 48 31 30 30 31 31 5d 20 5b 48 nts: [H10011] [H
57b0: 31 30 30 31 34 5d 0a 2a 2f 0a 23 64 65 66 69 6e 10014].*/.#defin
57c0: 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e e SQLITE_VERSION
57d0: 20 20 20 20 20 20 20 20 22 33 2e 36 2e 32 30 22 "3.6.20"
57e0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
57f0: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 33 VERSION_NUMBER 3
5800: 30 30 36 30 32 30 0a 23 64 65 66 69 6e 65 20 53 006020.#define S
5810: 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 20 QLITE_SOURCE_ID
5820: 20 20 20 20 20 22 32 30 30 39 2d 31 30 2d 33 30 "2009-10-30
5830: 20 31 34 3a 32 37 3a 31 35 20 36 31 32 39 35 32 14:27:15 612952
5840: 37 34 33 64 61 32 38 65 36 35 31 35 31 32 35 34 743da28e65151254
5850: 37 66 63 30 64 33 39 32 35 66 34 63 36 39 38 65 7fc0d3925f4c698e
5860: 62 34 22 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 b4"../*.** CAPI3
5870: 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 69 REF: Run-Time Li
5880: 62 72 61 72 79 20 56 65 72 73 69 6f 6e 20 4e 75 brary Version Nu
5890: 6d 62 65 72 73 20 7b 48 31 30 30 32 30 7d 20 3c mbers {H10020} <
58a0: 53 36 30 31 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f S60100>.** KEYWO
58b0: 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 65 72 RDS: sqlite3_ver
58c0: 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 sion.**.** These
58d0: 20 69 6e 74 65 72 66 61 63 65 73 20 70 72 6f 76 interfaces prov
58e0: 69 64 65 20 74 68 65 20 73 61 6d 65 20 69 6e 66 ide the same inf
58f0: 6f 72 6d 61 74 69 6f 6e 20 61 73 20 74 68 65 20 ormation as the
5900: 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5d [SQLITE_VERSION]
5910: 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 56 45 52 ,.** [SQLITE_VER
5920: 53 49 4f 4e 5f 4e 55 4d 42 45 52 5d 2c 20 61 6e SION_NUMBER], an
5930: 64 20 5b 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 d [SQLITE_SOURCE
5940: 5f 49 44 5d 20 23 64 65 66 69 6e 65 73 20 69 6e _ID] #defines in
5950: 20 74 68 65 20 68 65 61 64 65 72 2c 0a 2a 2a 20 the header,.**
5960: 62 75 74 20 61 72 65 20 61 73 73 6f 63 69 61 74 but are associat
5970: 65 64 20 77 69 74 68 20 74 68 65 20 6c 69 62 72 ed with the libr
5980: 61 72 79 20 69 6e 73 74 65 61 64 20 6f 66 20 74 ary instead of t
5990: 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e 20 he header file.
59a0: 20 43 61 75 74 69 6f 75 73 0a 2a 2a 20 70 72 6f Cautious.** pro
59b0: 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 69 grammers might i
59c0: 6e 63 6c 75 64 65 20 61 73 73 65 72 74 28 29 20 nclude assert()
59d0: 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 statements in th
59e0: 65 69 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 eir application
59f0: 74 6f 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 61 to.** verify tha
5a00: 74 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 t values returne
5a10: 64 20 62 79 20 74 68 65 73 65 20 69 6e 74 65 72 d by these inter
5a20: 66 61 63 65 73 20 6d 61 74 63 68 20 74 68 65 20 faces match the
5a30: 6d 61 63 72 6f 73 20 69 6e 0a 2a 2a 20 74 68 65 macros in.** the
5a40: 20 68 65 61 64 65 72 2c 20 61 6e 64 20 74 68 75 header, and thu
5a50: 73 20 69 6e 73 75 72 65 20 74 68 61 74 20 74 68 s insure that th
5a60: 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 e application is
5a70: 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 .** compiled wit
5a80: 68 20 6d 61 74 63 68 69 6e 67 20 6c 69 62 72 61 h matching libra
5a90: 72 79 20 61 6e 64 20 68 65 61 64 65 72 20 66 69 ry and header fi
5aa0: 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 les..**.** <bloc
5ab0: 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 kquote><pre>.**
5ac0: 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
5ad0: 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 libversion_numbe
5ae0: 72 28 29 3d 3d 53 51 4c 49 54 45 5f 56 45 52 53 r()==SQLITE_VERS
5af0: 49 4f 4e 5f 4e 55 4d 42 45 52 20 29 3b 0a 2a 2a ION_NUMBER );.**
5b00: 20 61 73 73 65 72 74 28 20 73 74 72 63 6d 70 28 assert( strcmp(
5b10: 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 sqlite3_sourceid
5b20: 28 29 2c 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 (),SQLITE_SOURCE
5b30: 5f 49 44 29 3d 3d 30 20 29 3b 0a 2a 2a 20 61 73 _ID)==0 );.** as
5b40: 73 65 72 74 28 20 73 74 72 63 6d 70 28 73 71 6c sert( strcmp(sql
5b50: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 2c ite3_libversion,
5b60: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 29 3d SQLITE_VERSION)=
5b70: 3d 30 20 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c =0 );.** </pre><
5b80: 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a /blockquote>.**.
5b90: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c ** The sqlite3_l
5ba0: 69 62 76 65 72 73 69 6f 6e 28 29 20 66 75 6e 63 ibversion() func
5bb0: 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 tion returns the
5bc0: 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f same informatio
5bd0: 6e 20 61 73 20 69 73 0a 2a 2a 20 69 6e 20 74 68 n as is.** in th
5be0: 65 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f e sqlite3_versio
5bf0: 6e 5b 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 74 n[] string const
5c00: 61 6e 74 2e 20 20 54 68 65 20 66 75 6e 63 74 69 ant. The functi
5c10: 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 0a 2a on is provided.*
5c20: 2a 20 66 6f 72 20 75 73 65 20 69 6e 20 44 4c 4c * for use in DLL
5c30: 73 20 73 69 6e 63 65 20 44 4c 4c 20 75 73 65 72 s since DLL user
5c40: 73 20 75 73 75 61 6c 6c 79 20 64 6f 20 6e 6f 74 s usually do not
5c50: 20 68 61 76 65 20 64 69 72 65 63 74 20 61 63 63 have direct acc
5c60: 65 73 73 20 74 6f 20 73 74 72 69 6e 67 0a 2a 2a ess to string.**
5c70: 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 74 68 69 constants withi
5c80: 6e 20 74 68 65 20 44 4c 4c 2e 20 20 53 69 6d 69 n the DLL. Simi
5c90: 6c 61 72 6c 79 2c 20 74 68 65 20 73 71 6c 69 74 larly, the sqlit
5ca0: 65 33 5f 73 6f 75 72 63 65 69 64 28 29 20 66 75 e3_sourceid() fu
5cb0: 6e 63 74 69 6f 6e 0a 2a 2a 20 72 65 74 75 72 6e nction.** return
5cc0: 73 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72 s the same infor
5cd0: 6d 61 74 69 6f 6e 20 61 73 20 69 73 20 69 6e 20 mation as is in
5ce0: 74 68 65 20 5b 53 51 4c 49 54 45 5f 53 4f 55 52 the [SQLITE_SOUR
5cf0: 43 45 5f 49 44 5d 20 23 64 65 66 69 6e 65 20 6f CE_ID] #define o
5d00: 66 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20 f.** the header
5d10: 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 file..**.** See
5d20: 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 5f 76 65 also: [sqlite_ve
5d30: 72 73 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 rsion()] and [sq
5d40: 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69 64 28 29 lite_source_id()
5d50: 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 ]..**.** Require
5d60: 6d 65 6e 74 73 3a 20 5b 48 31 30 30 32 31 5d 20 ments: [H10021]
5d70: 5b 48 31 30 30 32 32 5d 20 5b 48 31 30 30 32 33 [H10022] [H10023
5d80: 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
5d90: 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 const char sqlit
5da0: 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53 e3_version[] = S
5db0: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a 53 QLITE_VERSION;.S
5dc0: 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
5dd0: 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69 char *sqlite3_li
5de0: 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29 3b 0a bversion(void);.
5df0: 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
5e00: 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 char *sqlite3_s
5e10: 6f 75 72 63 65 69 64 28 76 6f 69 64 29 3b 0a 53 ourceid(void);.S
5e20: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
5e30: 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e lite3_libversion
5e40: 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29 3b 0a 0a _number(void);..
5e50: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
5e60: 54 65 73 74 20 54 6f 20 53 65 65 20 49 66 20 54 Test To See If T
5e70: 68 65 20 4c 69 62 72 61 72 79 20 49 73 20 54 68 he Library Is Th
5e80: 72 65 61 64 73 61 66 65 20 7b 48 31 30 31 30 30 readsafe {H10100
5e90: 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 2a } <S60100>.**.**
5ea0: 20 53 51 4c 69 74 65 20 63 61 6e 20 62 65 20 63 SQLite can be c
5eb0: 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 6f 72 20 ompiled with or
5ec0: 77 69 74 68 6f 75 74 20 6d 75 74 65 78 65 73 2e without mutexes.
5ed0: 20 20 57 68 65 6e 0a 2a 2a 20 74 68 65 20 5b 53 When.** the [S
5ee0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
5ef0: 5d 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f 72 ] C preprocessor
5f00: 20 6d 61 63 72 6f 20 69 73 20 31 20 6f 72 20 32 macro is 1 or 2
5f10: 2c 20 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65 , mutexes.** are
5f20: 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 53 51 4c enabled and SQL
5f30: 69 74 65 20 69 73 20 74 68 72 65 61 64 73 61 66 ite is threadsaf
5f40: 65 2e 20 20 57 68 65 6e 20 74 68 65 0a 2a 2a 20 e. When the.**
5f50: 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 [SQLITE_THREADSA
5f60: 46 45 5d 20 6d 61 63 72 6f 20 69 73 20 30 2c 20 FE] macro is 0,
5f70: 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 65 73 20 .** the mutexes
5f80: 61 72 65 20 6f 6d 69 74 74 65 64 2e 20 20 57 69 are omitted. Wi
5f90: 74 68 6f 75 74 20 74 68 65 20 6d 75 74 65 78 65 thout the mutexe
5fa0: 73 2c 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66 s, it is not saf
5fb0: 65 0a 2a 2a 20 74 6f 20 75 73 65 20 53 51 4c 69 e.** to use SQLi
5fc0: 74 65 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 20 te concurrently
5fd0: 66 72 6f 6d 20 6d 6f 72 65 20 74 68 61 6e 20 6f from more than o
5fe0: 6e 65 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a ne thread..**.**
5ff0: 20 45 6e 61 62 6c 69 6e 67 20 6d 75 74 65 78 65 Enabling mutexe
6000: 73 20 69 6e 63 75 72 73 20 61 20 6d 65 61 73 75 s incurs a measu
6010: 72 61 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e 63 rable performanc
6020: 65 20 70 65 6e 61 6c 74 79 2e 0a 2a 2a 20 53 6f e penalty..** So
6030: 20 69 66 20 73 70 65 65 64 20 69 73 20 6f 66 20 if speed is of
6040: 75 74 6d 6f 73 74 20 69 6d 70 6f 72 74 61 6e 63 utmost importanc
6050: 65 2c 20 69 74 20 6d 61 6b 65 73 20 73 65 6e 73 e, it makes sens
6060: 65 20 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20 e to disable.**
6070: 74 68 65 20 6d 75 74 65 78 65 73 2e 20 20 42 75 the mutexes. Bu
6080: 74 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 73 61 t for maximum sa
6090: 66 65 74 79 2c 20 6d 75 74 65 78 65 73 20 73 68 fety, mutexes sh
60a0: 6f 75 6c 64 20 62 65 20 65 6e 61 62 6c 65 64 2e ould be enabled.
60b0: 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 .** The default
60c0: 62 65 68 61 76 69 6f 72 20 69 73 20 66 6f 72 20 behavior is for
60d0: 6d 75 74 65 78 65 73 20 74 6f 20 62 65 20 65 6e mutexes to be en
60e0: 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 abled..**.** Thi
60f0: 73 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 s interface can
6100: 62 65 20 75 73 65 64 20 62 79 20 61 6e 20 61 70 be used by an ap
6110: 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20 6d 61 6b plication to mak
6120: 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 0a e sure that the.
6130: 2a 2a 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 ** version of SQ
6140: 4c 69 74 65 20 74 68 61 74 20 69 74 20 69 73 20 Lite that it is
6150: 6c 69 6e 6b 69 6e 67 20 61 67 61 69 6e 73 74 20 linking against
6160: 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 was compiled wit
6170: 68 0a 2a 2a 20 74 68 65 20 64 65 73 69 72 65 64 h.** the desired
6180: 20 73 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20 setting of the
6190: 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 [SQLITE_THREADSA
61a0: 46 45 5d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a FE] macro..**.**
61b0: 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 This interface
61c0: 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 6f 6e 20 only reports on
61d0: 74 68 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 the compile-time
61e0: 20 6d 75 74 65 78 20 73 65 74 74 69 6e 67 0a 2a mutex setting.*
61f0: 2a 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45 * of the [SQLITE
6200: 5f 54 48 52 45 41 44 53 41 46 45 5d 20 66 6c 61 _THREADSAFE] fla
6210: 67 2e 20 20 49 66 20 53 51 4c 69 74 65 20 69 73 g. If SQLite is
6220: 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a compiled with.*
6230: 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 * SQLITE_THREADS
6240: 41 46 45 3d 31 20 74 68 65 6e 20 6d 75 74 65 78 AFE=1 then mutex
6250: 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20 62 es are enabled b
6260: 79 20 64 65 66 61 75 6c 74 20 62 75 74 0a 2a 2a y default but.**
6270: 20 63 61 6e 20 62 65 20 66 75 6c 6c 79 20 6f 72 can be fully or
6280: 20 70 61 72 74 69 61 6c 6c 79 20 64 69 73 61 62 partially disab
6290: 6c 65 64 20 75 73 69 6e 67 20 61 20 63 61 6c 6c led using a call
62a0: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e to [sqlite3_con
62b0: 66 69 67 28 29 5d 0a 2a 2a 20 77 69 74 68 20 74 fig()].** with t
62c0: 68 65 20 76 65 72 62 73 20 5b 53 51 4c 49 54 45 he verbs [SQLITE
62d0: 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 _CONFIG_SINGLETH
62e0: 52 45 41 44 5d 2c 20 5b 53 51 4c 49 54 45 5f 43 READ], [SQLITE_C
62f0: 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 ONFIG_MULTITHREA
6300: 44 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 D],.** or [SQLIT
6310: 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 2e E_CONFIG_MUTEX].
6320: 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c The return val
6330: 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 ue of this funct
6340: 69 6f 6e 20 73 68 6f 77 73 0a 2a 2a 20 6f 6e 6c ion shows.** onl
6350: 79 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f y the default co
6360: 6d 70 69 6c 65 2d 74 69 6d 65 20 73 65 74 74 69 mpile-time setti
6370: 6e 67 2c 20 6e 6f 74 20 61 6e 79 20 72 75 6e 2d ng, not any run-
6380: 74 69 6d 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 time changes.**
6390: 74 6f 20 74 68 61 74 20 73 65 74 74 69 6e 67 2e to that setting.
63a0: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 74 68 65 20 5b .**.** See the [
63b0: 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 threading mode]
63c0: 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f documentation fo
63d0: 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 r additional inf
63e0: 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 ormation..**.**
63f0: 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 Requirements: [H
6400: 31 30 31 30 31 5d 20 5b 48 31 30 31 30 32 5d 0a 10101] [H10102].
6410: 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
6420: 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 t sqlite3_thread
6430: 73 61 66 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a safe(void);../*.
6440: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 ** CAPI3REF: Dat
6450: 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e abase Connection
6460: 20 48 61 6e 64 6c 65 20 7b 48 31 32 30 30 30 7d Handle {H12000}
6470: 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 <S40200>.** KEY
6480: 57 4f 52 44 53 3a 20 7b 64 61 74 61 62 61 73 65 WORDS: {database
6490: 20 63 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b 64 61 connection} {da
64a0: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
64b0: 6e 73 7d 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 6f ns}.**.** Each o
64c0: 70 65 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 pen SQLite datab
64d0: 61 73 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 ase is represent
64e0: 65 64 20 62 79 20 61 20 70 6f 69 6e 74 65 72 20 ed by a pointer
64f0: 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f to an instance o
6500: 66 0a 2a 2a 20 74 68 65 20 6f 70 61 71 75 65 20 f.** the opaque
6510: 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 structure named
6520: 22 73 71 6c 69 74 65 33 22 2e 20 20 49 74 20 69 "sqlite3". It i
6530: 73 20 75 73 65 66 75 6c 20 74 6f 20 74 68 69 6e s useful to thin
6540: 6b 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 0a k of an sqlite3.
6550: 2a 2a 20 70 6f 69 6e 74 65 72 20 61 73 20 61 6e ** pointer as an
6560: 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 5b 73 object. The [s
6570: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 qlite3_open()],
6580: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 [sqlite3_open16(
6590: 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 )], and.** [sqli
65a0: 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 te3_open_v2()] i
65b0: 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 69 74 nterfaces are it
65c0: 73 20 63 6f 6e 73 74 72 75 63 74 6f 72 73 2c 20 s constructors,
65d0: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f and [sqlite3_clo
65e0: 73 65 28 29 5d 0a 2a 2a 20 69 73 20 69 74 73 20 se()].** is its
65f0: 64 65 73 74 72 75 63 74 6f 72 2e 20 20 54 68 65 destructor. The
6600: 72 65 20 61 72 65 20 6d 61 6e 79 20 6f 74 68 65 re are many othe
6610: 72 20 69 6e 74 65 72 66 61 63 65 73 20 28 73 75 r interfaces (su
6620: 63 68 20 61 73 0a 2a 2a 20 5b 73 71 6c 69 74 65 ch as.** [sqlite
6630: 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 3_prepare_v2()],
6640: 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
6650: 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 2c 20 61 6e _function()], an
6660: 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 75 d.** [sqlite3_bu
6670: 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 20 74 6f sy_timeout()] to
6680: 20 6e 61 6d 65 20 62 75 74 20 74 68 72 65 65 29 name but three)
6690: 20 74 68 61 74 20 61 72 65 20 6d 65 74 68 6f 64 that are method
66a0: 73 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 s on an.** sqlit
66b0: 65 33 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79 e3 object..*/.ty
66c0: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
66d0: 69 74 65 33 20 73 71 6c 69 74 65 33 3b 0a 0a 2f ite3 sqlite3;../
66e0: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 36 *.** CAPI3REF: 6
66f0: 34 2d 42 69 74 20 49 6e 74 65 67 65 72 20 54 79 4-Bit Integer Ty
6700: 70 65 73 20 7b 48 31 30 32 30 30 7d 20 3c 53 31 pes {H10200} <S1
6710: 30 31 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0110>.** KEYWORD
6720: 53 3a 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 S: sqlite_int64
6730: 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 0a 2a 2a sqlite_uint64.**
6740: 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 72 .** Because ther
6750: 65 20 69 73 20 6e 6f 20 63 72 6f 73 73 2d 70 6c e is no cross-pl
6760: 61 74 66 6f 72 6d 20 77 61 79 20 74 6f 20 73 70 atform way to sp
6770: 65 63 69 66 79 20 36 34 2d 62 69 74 20 69 6e 74 ecify 64-bit int
6780: 65 67 65 72 20 74 79 70 65 73 0a 2a 2a 20 53 51 eger types.** SQ
6790: 4c 69 74 65 20 69 6e 63 6c 75 64 65 73 20 74 79 Lite includes ty
67a0: 70 65 64 65 66 73 20 66 6f 72 20 36 34 2d 62 69 pedefs for 64-bi
67b0: 74 20 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73 t signed and uns
67c0: 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a igned integers..
67d0: 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
67e0: 33 5f 69 6e 74 36 34 20 61 6e 64 20 73 71 6c 69 3_int64 and sqli
67f0: 74 65 33 5f 75 69 6e 74 36 34 20 61 72 65 20 74 te3_uint64 are t
6800: 68 65 20 70 72 65 66 65 72 72 65 64 20 74 79 70 he preferred typ
6810: 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 2a e definitions..*
6820: 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 69 6e 74 * The sqlite_int
6830: 36 34 20 61 6e 64 20 73 71 6c 69 74 65 5f 75 69 64 and sqlite_ui
6840: 6e 74 36 34 20 74 79 70 65 73 20 61 72 65 20 73 nt64 types are s
6850: 75 70 70 6f 72 74 65 64 20 66 6f 72 20 62 61 63 upported for bac
6860: 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 61 74 kwards.** compat
6870: 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 0a 2a 2a ibility only..**
6880: 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
6890: 3a 20 5b 48 31 30 32 30 31 5d 20 5b 48 31 30 32 : [H10201] [H102
68a0: 30 32 5d 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 02].*/.#ifdef SQ
68b0: 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 0a LITE_INT64_TYPE.
68c0: 20 20 74 79 70 65 64 65 66 20 53 51 4c 49 54 45 typedef SQLITE
68d0: 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c 69 _INT64_TYPE sqli
68e0: 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 te_int64;. type
68f0: 64 65 66 20 75 6e 73 69 67 6e 65 64 20 53 51 4c def unsigned SQL
6900: 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 ITE_INT64_TYPE s
6910: 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 qlite_uint64;.#e
6920: 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 lif defined(_MSC
6930: 5f 56 45 52 29 20 7c 7c 20 64 65 66 69 6e 65 64 _VER) || defined
6940: 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 20 (__BORLANDC__).
6950: 20 74 79 70 65 64 65 66 20 5f 5f 69 6e 74 36 34 typedef __int64
6960: 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 sqlite_int64;.
6970: 20 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 typedef unsigne
6980: 64 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 d __int64 sqlite
6990: 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a 20 _uint64;.#else.
69a0: 20 74 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f typedef long lo
69b0: 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 69 6e ng int sqlite_in
69c0: 74 36 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75 t64;. typedef u
69d0: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
69e0: 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 75 69 6e g int sqlite_uin
69f0: 74 36 34 3b 0a 23 65 6e 64 69 66 0a 74 79 70 65 t64;.#endif.type
6a00: 64 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 def sqlite_int64
6a10: 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 3b 0a sqlite3_int64;.
6a20: 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75 typedef sqlite_u
6a30: 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 75 69 int64 sqlite3_ui
6a40: 6e 74 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 nt64;../*.** If
6a50: 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20 compiling for a
6a60: 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c processor that l
6a70: 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f acks floating po
6a80: 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20 int support,.**
6a90: 73 75 62 73 74 69 74 75 74 65 20 69 6e 74 65 67 substitute integ
6aa0: 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d er for floating-
6ab0: 70 6f 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 point..*/.#ifdef
6ac0: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
6ad0: 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 ATING_POINT.# de
6ae0: 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 fine double sqli
6af0: 74 65 33 5f 69 6e 74 36 34 0a 23 65 6e 64 69 66 te3_int64.#endif
6b00: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
6b10: 3a 20 43 6c 6f 73 69 6e 67 20 41 20 44 61 74 61 : Closing A Data
6b20: 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 base Connection
6b30: 7b 48 31 32 30 31 30 7d 20 3c 53 33 30 31 30 30 {H12010} <S30100
6b40: 3e 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 ><S40200>.**.**
6b50: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
6b60: 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 the destructor f
6b70: 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d or the [sqlite3]
6b80: 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 object..**.** A
6b90: 70 70 6c 69 63 61 74 69 6f 6e 73 20 6d 75 73 74 pplications must
6ba0: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 [sqlite3_finali
6bb0: 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 5d 20 61 ze | finalize] a
6bc0: 6c 6c 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 ll [prepared sta
6bd0: 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 61 6e 64 20 tements].** and
6be0: 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c [sqlite3_blob_cl
6bf0: 6f 73 65 20 7c 20 63 6c 6f 73 65 5d 20 61 6c 6c ose | close] all
6c00: 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 5d 20 [BLOB handles]
6c10: 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a associated with.
6c20: 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d ** the [sqlite3]
6c30: 20 6f 62 6a 65 63 74 20 70 72 69 6f 72 20 74 6f object prior to
6c40: 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 63 attempting to c
6c50: 6c 6f 73 65 20 74 68 65 20 6f 62 6a 65 63 74 2e lose the object.
6c60: 0a 2a 2a 0a 2a 2a 20 49 66 20 5b 73 71 6c 69 74 .**.** If [sqlit
6c70: 65 33 5f 63 6c 6f 73 65 28 29 5d 20 69 73 20 69 e3_close()] is i
6c80: 6e 76 6f 6b 65 64 20 77 68 69 6c 65 20 61 20 74 nvoked while a t
6c90: 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 ransaction is op
6ca0: 65 6e 2c 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 en,.** the trans
6cb0: 61 63 74 69 6f 6e 20 69 73 20 61 75 74 6f 6d 61 action is automa
6cc0: 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 tically rolled b
6cd0: 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 ack..**.** The C
6ce0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 parameter to [s
6cf0: 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d qlite3_close(C)]
6d00: 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 must be either
6d10: 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 a NULL.** pointe
6d20: 72 20 6f 72 20 61 6e 20 5b 73 71 6c 69 74 65 33 r or an [sqlite3
6d30: 5d 20 6f 62 6a 65 63 74 20 70 6f 69 6e 74 65 72 ] object pointer
6d40: 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f obtained.** fro
6d50: 6d 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 m [sqlite3_open(
6d60: 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 )], [sqlite3_ope
6d70: 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 n16()], or.** [s
6d80: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
6d90: 5d 2c 20 61 6e 64 20 6e 6f 74 20 70 72 65 76 69 ], and not previ
6da0: 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e 0a 2a 2a ously closed..**
6db0: 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
6dc0: 3a 0a 2a 2a 20 5b 48 31 32 30 31 31 5d 20 5b 48 :.** [H12011] [H
6dd0: 31 32 30 31 32 5d 20 5b 48 31 32 30 31 33 5d 20 12012] [H12013]
6de0: 5b 48 31 32 30 31 34 5d 20 5b 48 31 32 30 31 35 [H12014] [H12015
6df0: 5d 20 5b 48 31 32 30 31 39 5d 0a 2a 2f 0a 53 51 ] [H12019].*/.SQ
6e00: 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
6e10: 69 74 65 33 5f 63 6c 6f 73 65 28 73 71 6c 69 74 ite3_close(sqlit
6e20: 65 33 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 e3 *);../*.** Th
6e30: 65 20 74 79 70 65 20 66 6f 72 20 61 20 63 61 6c e type for a cal
6e40: 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a lback function..
6e50: 2a 2a 20 54 68 69 73 20 69 73 20 6c 65 67 61 63 ** This is legac
6e60: 79 20 61 6e 64 20 64 65 70 72 65 63 61 74 65 64 y and deprecated
6e70: 2e 20 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65 . It is include
6e80: 64 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c d for historical
6e90: 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 .** compatibilit
6ea0: 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 64 6f 63 y and is not doc
6eb0: 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 74 79 70 65 umented..*/.type
6ec0: 64 65 66 20 69 6e 74 20 28 2a 73 71 6c 69 74 65 def int (*sqlite
6ed0: 33 5f 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 3_callback)(void
6ee0: 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 20 63 68 *,int,char**, ch
6ef0: 61 72 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ar**);../*.** CA
6f00: 50 49 33 52 45 46 3a 20 4f 6e 65 2d 53 74 65 70 PI3REF: One-Step
6f10: 20 51 75 65 72 79 20 45 78 65 63 75 74 69 6f 6e Query Execution
6f20: 20 49 6e 74 65 72 66 61 63 65 20 7b 48 31 32 31 Interface {H121
6f30: 30 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 00} <S10000>.**.
6f40: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 ** The sqlite3_e
6f50: 78 65 63 28 29 20 69 6e 74 65 72 66 61 63 65 20 xec() interface
6f60: 69 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 is a convenient
6f70: 77 61 79 20 6f 66 20 72 75 6e 6e 69 6e 67 20 6f way of running o
6f80: 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 53 51 ne or more.** SQ
6f90: 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 77 69 74 L statements wit
6fa0: 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 77 hout having to w
6fb0: 72 69 74 65 20 61 20 6c 6f 74 20 6f 66 20 43 20 rite a lot of C
6fc0: 63 6f 64 65 2e 20 20 54 68 65 20 55 54 46 2d 38 code. The UTF-8
6fd0: 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 53 51 4c 20 encoded.** SQL
6fe0: 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 70 statements are p
6ff0: 61 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 assed in as the
7000: 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
7010: 20 74 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 to sqlite3_exec
7020: 28 29 2e 0a 2a 2a 20 54 68 65 20 73 74 61 74 65 ()..** The state
7030: 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61 ments are evalua
7040: 74 65 64 20 6f 6e 65 20 62 79 20 6f 6e 65 20 75 ted one by one u
7050: 6e 74 69 6c 20 65 69 74 68 65 72 20 61 6e 20 65 ntil either an e
7060: 72 72 6f 72 20 6f 72 0a 2a 2a 20 61 6e 20 69 6e rror or.** an in
7070: 74 65 72 72 75 70 74 20 69 73 20 65 6e 63 6f 75 terrupt is encou
7080: 6e 74 65 72 65 64 2c 20 6f 72 20 75 6e 74 69 6c ntered, or until
7090: 20 74 68 65 79 20 61 72 65 20 61 6c 6c 20 64 6f they are all do
70a0: 6e 65 2e 20 20 54 68 65 20 33 72 64 20 70 61 72 ne. The 3rd par
70b0: 61 6d 65 74 65 72 0a 2a 2a 20 69 73 20 61 6e 20 ameter.** is an
70c0: 6f 70 74 69 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 optional callbac
70d0: 6b 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 k that is invoke
70e0: 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 d once for each
70f0: 72 6f 77 20 6f 66 20 61 6e 79 20 71 75 65 72 79 row of any query
7100: 0a 2a 2a 20 72 65 73 75 6c 74 73 20 70 72 6f 64 .** results prod
7110: 75 63 65 64 20 62 79 20 74 68 65 20 53 51 4c 20 uced by the SQL
7120: 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 statements. The
7130: 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 5th parameter t
7140: 65 6c 6c 73 20 77 68 65 72 65 0a 2a 2a 20 74 6f ells where.** to
7150: 20 77 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 write any error
7160: 20 6d 65 73 73 61 67 65 73 2e 0a 2a 2a 0a 2a 2a messages..**.**
7170: 20 54 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 The error messa
7180: 67 65 20 70 61 73 73 65 64 20 62 61 63 6b 20 74 ge passed back t
7190: 68 72 6f 75 67 68 20 74 68 65 20 35 74 68 20 70 hrough the 5th p
71a0: 61 72 61 6d 65 74 65 72 20 69 73 20 68 65 6c 64 arameter is held
71b0: 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 .** in memory ob
71c0: 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c tained from [sql
71d0: 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 ite3_malloc()].
71e0: 20 54 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 6f To avoid a memo
71f0: 72 79 20 6c 65 61 6b 2c 0a 2a 2a 20 74 68 65 20 ry leak,.** the
7200: 63 61 6c 6c 69 6e 67 20 61 70 70 6c 69 63 61 74 calling applicat
7210: 69 6f 6e 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 ion should call
7220: 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d [sqlite3_free()]
7230: 20 6f 6e 20 61 6e 79 20 65 72 72 6f 72 0a 2a 2a on any error.**
7240: 20 6d 65 73 73 61 67 65 20 72 65 74 75 72 6e 65 message returne
7250: 64 20 74 68 72 6f 75 67 68 20 74 68 65 20 35 74 d through the 5t
7260: 68 20 70 61 72 61 6d 65 74 65 72 20 77 68 65 6e h parameter when
7270: 20 69 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 it has finished
7280: 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 65 72 using.** the er
7290: 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a ror message..**.
72a0: 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 ** If the SQL st
72b0: 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 32 atement in the 2
72c0: 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 nd parameter is
72d0: 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74 79 NULL or an empty
72e0: 20 73 74 72 69 6e 67 0a 2a 2a 20 6f 72 20 61 20 string.** or a
72f0: 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e string containin
7300: 67 20 6f 6e 6c 79 20 77 68 69 74 65 73 70 61 63 g only whitespac
7310: 65 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 2c 20 e and comments,
7320: 74 68 65 6e 20 6e 6f 20 53 51 4c 0a 2a 2a 20 73 then no SQL.** s
7330: 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76 tatements are ev
7340: 61 6c 75 61 74 65 64 20 61 6e 64 20 74 68 65 20 aluated and the
7350: 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 database is not
7360: 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 changed..**.** T
7370: 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 he sqlite3_exec(
7380: 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 69 ) interface is i
7390: 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 65 mplemented in te
73a0: 72 6d 73 20 6f 66 0a 2a 2a 20 5b 73 71 6c 69 74 rms of.** [sqlit
73b0: 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d e3_prepare_v2()]
73c0: 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 , [sqlite3_step(
73d0: 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 )], and [sqlite3
73e0: 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a _finalize()]..**
73f0: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 The sqlite3_exe
7400: 63 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 c() routine does
7410: 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 65 20 nothing to the
7420: 64 61 74 61 62 61 73 65 20 74 68 61 74 20 63 61 database that ca
7430: 6e 6e 6f 74 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 nnot be done.**
7440: 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 by [sqlite3_prep
7450: 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c 69 are_v2()], [sqli
7460: 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e 64 te3_step()], and
7470: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 [sqlite3_finali
7480: 7a 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ze()]..**.** The
7490: 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 first parameter
74a0: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 to [sqlite3_exe
74b0: 63 28 29 5d 20 6d 75 73 74 20 62 65 20 61 6e 20 c()] must be an
74c0: 76 61 6c 69 64 20 61 6e 64 20 6f 70 65 6e 0a 2a valid and open.*
74d0: 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e * [database conn
74e0: 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 54 ection]..**.** T
74f0: 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
7500: 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 ection must not
7510: 62 65 20 63 6c 6f 73 65 64 20 77 68 69 6c 65 0a be closed while.
7520: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 ** [sqlite3_exec
7530: 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a ()] is running..
7540: 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e **.** The callin
7550: 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c g function shoul
7560: 64 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f 66 d use [sqlite3_f
7570: 72 65 65 28 29 5d 20 74 6f 20 66 72 65 65 0a 2a ree()] to free.*
7580: 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61 * the memory tha
7590: 74 20 2a 65 72 72 6d 73 67 20 69 73 20 6c 65 66 t *errmsg is lef
75a0: 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 6f 6e t pointing at on
75b0: 63 65 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 ce the error.**
75c0: 6d 65 73 73 61 67 65 20 69 73 20 6e 6f 20 6c 6f message is no lo
75d0: 6e 67 65 72 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a nger needed..**.
75e0: 2a 2a 20 54 68 65 20 53 51 4c 20 73 74 61 74 65 ** The SQL state
75f0: 6d 65 6e 74 20 74 65 78 74 20 69 6e 20 74 68 65 ment text in the
7600: 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 2nd parameter t
7610: 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 o [sqlite3_exec(
7620: 29 5d 0a 2a 2a 20 6d 75 73 74 20 72 65 6d 61 69 )].** must remai
7630: 6e 20 75 6e 63 68 61 6e 67 65 64 20 77 68 69 6c n unchanged whil
7640: 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 e [sqlite3_exec(
7650: 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a )] is running..*
7660: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
7670: 73 3a 0a 2a 2a 20 5b 48 31 32 31 30 31 5d 20 5b s:.** [H12101] [
7680: 48 31 32 31 30 32 5d 20 5b 48 31 32 31 30 34 5d H12102] [H12104]
7690: 20 5b 48 31 32 31 30 35 5d 20 5b 48 31 32 31 30 [H12105] [H1210
76a0: 37 5d 20 5b 48 31 32 31 31 30 5d 20 5b 48 31 32 7] [H12110] [H12
76b0: 31 31 33 5d 20 5b 48 31 32 31 31 36 5d 0a 2a 2a 113] [H12116].**
76c0: 20 5b 48 31 32 31 31 39 5d 20 5b 48 31 32 31 32 [H12119] [H1212
76d0: 32 5d 20 5b 48 31 32 31 32 35 5d 20 5b 48 31 32 2] [H12125] [H12
76e0: 31 33 31 5d 20 5b 48 31 32 31 33 34 5d 20 5b 48 131] [H12134] [H
76f0: 31 32 31 33 37 5d 20 5b 48 31 32 31 33 38 5d 0a 12137] [H12138].
7700: 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
7710: 74 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 0a t sqlite3_exec(.
7720: 20 20 73 71 6c 69 74 65 33 2a 2c 20 20 20 20 20 sqlite3*,
7730: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7740: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
7750: 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 An open database
7760: 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
7770: 20 2a 73 71 6c 2c 20 20 20 20 20 20 20 20 20 20 *sql,
7780: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7790: 20 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20 65 76 /* SQL to be ev
77a0: 61 6c 75 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 aluated */. int
77b0: 20 28 2a 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69 (*callback)(voi
77c0: 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 63 68 d*,int,char**,ch
77d0: 61 72 2a 2a 29 2c 20 20 2f 2a 20 43 61 6c 6c 62 ar**), /* Callb
77e0: 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a ack function */.
77f0: 20 20 76 6f 69 64 20 2a 2c 20 20 20 20 20 20 20 void *,
7800: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7810: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
7820: 31 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 1st argument to
7830: 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 63 68 callback */. ch
7840: 61 72 20 2a 2a 65 72 72 6d 73 67 20 20 20 20 20 ar **errmsg
7850: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7860: 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f /* Erro
7870: 72 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68 65 r msg written he
7880: 72 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 re */.);../*.**
7890: 43 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c 74 CAPI3REF: Result
78a0: 20 43 6f 64 65 73 20 7b 48 31 30 32 31 30 7d 20 Codes {H10210}
78b0: 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 <S10700>.** KEYW
78c0: 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f 4f 4b 20 ORDS: SQLITE_OK
78d0: 7b 65 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65 72 {error code} {er
78e0: 72 6f 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45 ror codes}.** KE
78f0: 59 57 4f 52 44 53 3a 20 7b 72 65 73 75 6c 74 20 YWORDS: {result
7900: 63 6f 64 65 7d 20 7b 72 65 73 75 6c 74 20 63 6f code} {result co
7910: 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 des}.**.** Many
7920: 53 51 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e 73 SQLite functions
7930: 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 return an integ
7940: 65 72 20 72 65 73 75 6c 74 20 63 6f 64 65 20 66 er result code f
7950: 72 6f 6d 20 74 68 65 20 73 65 74 20 73 68 6f 77 rom the set show
7960: 6e 0a 2a 2a 20 68 65 72 65 20 69 6e 20 6f 72 64 n.** here in ord
7970: 65 72 20 74 6f 20 69 6e 64 69 63 61 74 65 73 20 er to indicates
7980: 73 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75 success or failu
7990: 72 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 65 72 re..**.** New er
79a0: 72 6f 72 20 63 6f 64 65 73 20 6d 61 79 20 62 65 ror codes may be
79b0: 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 added in future
79c0: 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
79d0: 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 ite..**.** See a
79e0: 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 lso: [SQLITE_IOE
79f0: 52 52 5f 52 45 41 44 20 7c 20 65 78 74 65 6e 64 RR_READ | extend
7a00: 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d ed result codes]
7a10: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
7a20: 54 45 5f 4f 4b 20 20 20 20 20 20 20 20 20 20 20 TE_OK
7a30: 30 20 20 20 2f 2a 20 53 75 63 63 65 73 73 66 75 0 /* Successfu
7a40: 6c 20 72 65 73 75 6c 74 20 2a 2f 0a 2f 2a 20 62 l result */./* b
7a50: 65 67 69 6e 6e 69 6e 67 2d 6f 66 2d 65 72 72 6f eginning-of-erro
7a60: 72 2d 63 6f 64 65 73 20 2a 2f 0a 23 64 65 66 69 r-codes */.#defi
7a70: 6e 65 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 ne SQLITE_ERROR
7a80: 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20 53 51 1 /* SQ
7a90: 4c 20 65 72 72 6f 72 20 6f 72 20 6d 69 73 73 69 L error or missi
7aa0: 6e 67 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 ng database */.#
7ab0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e define SQLITE_IN
7ac0: 54 45 52 4e 41 4c 20 20 20 20 20 32 20 20 20 2f TERNAL 2 /
7ad0: 2a 20 49 6e 74 65 72 6e 61 6c 20 6c 6f 67 69 63 * Internal logic
7ae0: 20 65 72 72 6f 72 20 69 6e 20 53 51 4c 69 74 65 error in SQLite
7af0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
7b00: 54 45 5f 50 45 52 4d 20 20 20 20 20 20 20 20 20 TE_PERM
7b10: 33 20 20 20 2f 2a 20 41 63 63 65 73 73 20 70 65 3 /* Access pe
7b20: 72 6d 69 73 73 69 6f 6e 20 64 65 6e 69 65 64 20 rmission denied
7b30: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
7b40: 45 5f 41 42 4f 52 54 20 20 20 20 20 20 20 20 34 E_ABORT 4
7b50: 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 72 /* Callback r
7b60: 6f 75 74 69 6e 65 20 72 65 71 75 65 73 74 65 64 outine requested
7b70: 20 61 6e 20 61 62 6f 72 74 20 2a 2f 0a 23 64 65 an abort */.#de
7b80: 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 55 53 59 fine SQLITE_BUSY
7b90: 20 20 20 20 20 20 20 20 20 35 20 20 20 2f 2a 20 5 /*
7ba0: 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c The database fil
7bb0: 65 20 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23 e is locked */.#
7bc0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f define SQLITE_LO
7bd0: 43 4b 45 44 20 20 20 20 20 20 20 36 20 20 20 2f CKED 6 /
7be0: 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65 * A table in the
7bf0: 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 database is loc
7c00: 6b 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ked */.#define S
7c10: 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 20 20 20 20 QLITE_NOMEM
7c20: 20 20 20 37 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 7 /* A mall
7c30: 6f 63 28 29 20 66 61 69 6c 65 64 20 2a 2f 0a 23 oc() failed */.#
7c40: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 define SQLITE_RE
7c50: 41 44 4f 4e 4c 59 20 20 20 20 20 38 20 20 20 2f ADONLY 8 /
7c60: 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 77 72 69 * Attempt to wri
7c70: 74 65 20 61 20 72 65 61 64 6f 6e 6c 79 20 64 61 te a readonly da
7c80: 74 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e tabase */.#defin
7c90: 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 e SQLITE_INTERRU
7ca0: 50 54 20 20 20 20 39 20 20 20 2f 2a 20 4f 70 65 PT 9 /* Ope
7cb0: 72 61 74 69 6f 6e 20 74 65 72 6d 69 6e 61 74 65 ration terminate
7cc0: 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 74 d by sqlite3_int
7cd0: 65 72 72 75 70 74 28 29 2a 2f 0a 23 64 65 66 69 errupt()*/.#defi
7ce0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 ne SQLITE_IOERR
7cf0: 20 20 20 20 20 20 31 30 20 20 20 2f 2a 20 53 6f 10 /* So
7d00: 6d 65 20 6b 69 6e 64 20 6f 66 20 64 69 73 6b 20 me kind of disk
7d10: 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 72 I/O error occurr
7d20: 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ed */.#define SQ
7d30: 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 20 20 20 LITE_CORRUPT
7d40: 20 31 31 20 20 20 2f 2a 20 54 68 65 20 64 61 74 11 /* The dat
7d50: 61 62 61 73 65 20 64 69 73 6b 20 69 6d 61 67 65 abase disk image
7d60: 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64 20 2a 2f is malformed */
7d70: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
7d80: 4e 4f 54 46 4f 55 4e 44 20 20 20 20 31 32 20 20 NOTFOUND 12
7d90: 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20 54 61 /* NOT USED. Ta
7da0: 62 6c 65 20 6f 72 20 72 65 63 6f 72 64 20 6e 6f ble or record no
7db0: 74 20 66 6f 75 6e 64 20 2a 2f 0a 23 64 65 66 69 t found */.#defi
7dc0: 6e 65 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 20 ne SQLITE_FULL
7dd0: 20 20 20 20 20 20 31 33 20 20 20 2f 2a 20 49 6e 13 /* In
7de0: 73 65 72 74 69 6f 6e 20 66 61 69 6c 65 64 20 62 sertion failed b
7df0: 65 63 61 75 73 65 20 64 61 74 61 62 61 73 65 20 ecause database
7e00: 69 73 20 66 75 6c 6c 20 2a 2f 0a 23 64 65 66 69 is full */.#defi
7e10: 6e 65 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 ne SQLITE_CANTOP
7e20: 45 4e 20 20 20 20 31 34 20 20 20 2f 2a 20 55 6e EN 14 /* Un
7e30: 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 able to open the
7e40: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
7e50: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
7e60: 5f 50 52 4f 54 4f 43 4f 4c 20 20 20 20 31 35 20 _PROTOCOL 15
7e70: 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20 44 /* NOT USED. D
7e80: 61 74 61 62 61 73 65 20 6c 6f 63 6b 20 70 72 6f atabase lock pro
7e90: 74 6f 63 6f 6c 20 65 72 72 6f 72 20 2a 2f 0a 23 tocol error */.#
7ea0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4d define SQLITE_EM
7eb0: 50 54 59 20 20 20 20 20 20 20 31 36 20 20 20 2f PTY 16 /
7ec0: 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 65 6d * Database is em
7ed0: 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 pty */.#define S
7ee0: 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 20 20 20 QLITE_SCHEMA
7ef0: 20 20 31 37 20 20 20 2f 2a 20 54 68 65 20 64 61 17 /* The da
7f00: 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 63 68 tabase schema ch
7f10: 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 anged */.#define
7f20: 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 20 SQLITE_TOOBIG
7f30: 20 20 20 20 31 38 20 20 20 2f 2a 20 53 74 72 69 18 /* Stri
7f40: 6e 67 20 6f 72 20 42 4c 4f 42 20 65 78 63 65 65 ng or BLOB excee
7f50: 64 73 20 73 69 7a 65 20 6c 69 6d 69 74 20 2a 2f ds size limit */
7f60: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
7f70: 43 4f 4e 53 54 52 41 49 4e 54 20 20 31 39 20 20 CONSTRAINT 19
7f80: 20 2f 2a 20 41 62 6f 72 74 20 64 75 65 20 74 6f /* Abort due to
7f90: 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c constraint viol
7fa0: 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 ation */.#define
7fb0: 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 SQLITE_MISMATCH
7fc0: 20 20 20 20 32 30 20 20 20 2f 2a 20 44 61 74 61 20 /* Data
7fd0: 20 74 79 70 65 20 6d 69 73 6d 61 74 63 68 20 2a type mismatch *
7fe0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
7ff0: 5f 4d 49 53 55 53 45 20 20 20 20 20 20 32 31 20 _MISUSE 21
8000: 20 20 2f 2a 20 4c 69 62 72 61 72 79 20 75 73 65 /* Library use
8010: 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 2a 2f d incorrectly */
8020: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8030: 4e 4f 4c 46 53 20 20 20 20 20 20 20 32 32 20 20 NOLFS 22
8040: 20 2f 2a 20 55 73 65 73 20 4f 53 20 66 65 61 74 /* Uses OS feat
8050: 75 72 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 ures not support
8060: 65 64 20 6f 6e 20 68 6f 73 74 20 2a 2f 0a 23 64 ed on host */.#d
8070: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 55 54 efine SQLITE_AUT
8080: 48 20 20 20 20 20 20 20 20 32 33 20 20 20 2f 2a H 23 /*
8090: 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 64 Authorization d
80a0: 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 enied */.#define
80b0: 20 53 51 4c 49 54 45 5f 46 4f 52 4d 41 54 20 20 SQLITE_FORMAT
80c0: 20 20 20 20 32 34 20 20 20 2f 2a 20 41 75 78 69 24 /* Auxi
80d0: 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 liary database f
80e0: 6f 72 6d 61 74 20 65 72 72 6f 72 20 2a 2f 0a 23 ormat error */.#
80f0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 41 define SQLITE_RA
8100: 4e 47 45 20 20 20 20 20 20 20 32 35 20 20 20 2f NGE 25 /
8110: 2a 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 * 2nd parameter
8120: 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 20 to sqlite3_bind
8130: 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f 0a out of range */.
8140: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e #define SQLITE_N
8150: 4f 54 41 44 42 20 20 20 20 20 20 32 36 20 20 20 OTADB 26
8160: 2f 2a 20 46 69 6c 65 20 6f 70 65 6e 65 64 20 74 /* File opened t
8170: 68 61 74 20 69 73 20 6e 6f 74 20 61 20 64 61 74 hat is not a dat
8180: 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 23 64 abase file */.#d
8190: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 4f 57 efine SQLITE_ROW
81a0: 20 20 20 20 20 20 20 20 20 31 30 30 20 20 2f 2a 100 /*
81b0: 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 sqlite3_step()
81c0: 68 61 73 20 61 6e 6f 74 68 65 72 20 72 6f 77 20 has another row
81d0: 72 65 61 64 79 20 2a 2f 0a 23 64 65 66 69 6e 65 ready */.#define
81e0: 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20 20 SQLITE_DONE
81f0: 20 20 20 20 31 30 31 20 20 2f 2a 20 73 71 6c 69 101 /* sqli
8200: 74 65 33 5f 73 74 65 70 28 29 20 68 61 73 20 66 te3_step() has f
8210: 69 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 6e inished executin
8220: 67 20 2a 2f 0a 2f 2a 20 65 6e 64 2d 6f 66 2d 65 g */./* end-of-e
8230: 72 72 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 0a 2f rror-codes */../
8240: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 *.** CAPI3REF: E
8250: 78 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20 43 xtended Result C
8260: 6f 64 65 73 20 7b 48 31 30 32 32 30 7d 20 3c 53 odes {H10220} <S
8270: 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 10700>.** KEYWOR
8280: 44 53 3a 20 7b 65 78 74 65 6e 64 65 64 20 65 72 DS: {extended er
8290: 72 6f 72 20 63 6f 64 65 7d 20 7b 65 78 74 65 6e ror code} {exten
82a0: 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 73 7d ded error codes}
82b0: 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 65 .** KEYWORDS: {e
82c0: 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 xtended result c
82d0: 6f 64 65 7d 20 7b 65 78 74 65 6e 64 65 64 20 72 ode} {extended r
82e0: 65 73 75 6c 74 20 63 6f 64 65 73 7d 0a 2a 2a 0a esult codes}.**.
82f0: 2a 2a 20 49 6e 20 69 74 73 20 64 65 66 61 75 6c ** In its defaul
8300: 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c t configuration,
8310: 20 53 51 4c 69 74 65 20 41 50 49 20 72 6f 75 74 SQLite API rout
8320: 69 6e 65 73 20 72 65 74 75 72 6e 20 6f 6e 65 20 ines return one
8330: 6f 66 20 32 36 20 69 6e 74 65 67 65 72 0a 2a 2a of 26 integer.**
8340: 20 5b 53 51 4c 49 54 45 5f 4f 4b 20 7c 20 72 65 [SQLITE_OK | re
8350: 73 75 6c 74 20 63 6f 64 65 73 5d 2e 20 20 48 6f sult codes]. Ho
8360: 77 65 76 65 72 2c 20 65 78 70 65 72 69 65 6e 63 wever, experienc
8370: 65 20 68 61 73 20 73 68 6f 77 6e 20 74 68 61 74 e has shown that
8380: 20 6d 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 73 many of.** thes
8390: 65 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 e result codes a
83a0: 72 65 20 74 6f 6f 20 63 6f 61 72 73 65 2d 67 72 re too coarse-gr
83b0: 61 69 6e 65 64 2e 20 20 54 68 65 79 20 64 6f 20 ained. They do
83c0: 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 73 0a 2a not provide as.*
83d0: 2a 20 6d 75 63 68 20 69 6e 66 6f 72 6d 61 74 69 * much informati
83e0: 6f 6e 20 61 62 6f 75 74 20 70 72 6f 62 6c 65 6d on about problem
83f0: 73 20 61 73 20 70 72 6f 67 72 61 6d 6d 65 72 73 s as programmers
8400: 20 6d 69 67 68 74 20 6c 69 6b 65 2e 20 20 49 6e might like. In
8410: 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 0a 2a 2a an effort to.**
8420: 20 61 64 64 72 65 73 73 20 74 68 69 73 2c 20 6e address this, n
8430: 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 ewer versions of
8440: 20 53 51 4c 69 74 65 20 28 76 65 72 73 69 6f 6e SQLite (version
8450: 20 33 2e 33 2e 38 20 61 6e 64 20 6c 61 74 65 72 3.3.8 and later
8460: 29 20 69 6e 63 6c 75 64 65 0a 2a 2a 20 73 75 70 ) include.** sup
8470: 70 6f 72 74 20 66 6f 72 20 61 64 64 69 74 69 6f port for additio
8480: 6e 61 6c 20 72 65 73 75 6c 74 20 63 6f 64 65 73 nal result codes
8490: 20 74 68 61 74 20 70 72 6f 76 69 64 65 20 6d 6f that provide mo
84a0: 72 65 20 64 65 74 61 69 6c 65 64 20 69 6e 66 6f re detailed info
84b0: 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 rmation.** about
84c0: 20 65 72 72 6f 72 73 2e 20 54 68 65 20 65 78 74 errors. The ext
84d0: 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 ended result cod
84e0: 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20 6f es are enabled o
84f0: 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 6f 6e r disabled.** on
8500: 20 61 20 70 65 72 20 64 61 74 61 62 61 73 65 20 a per database
8510: 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 73 connection basis
8520: 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 using the.** [s
8530: 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f qlite3_extended_
8540: 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 5d 20 result_codes()]
8550: 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 API..**.** Some
8560: 6f 66 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 of the available
8570: 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 extended result
8580: 20 63 6f 64 65 73 20 61 72 65 20 6c 69 73 74 65 codes are liste
8590: 64 20 68 65 72 65 2e 0a 2a 2a 20 4f 6e 65 20 6d d here..** One m
85a0: 61 79 20 65 78 70 65 63 74 20 74 68 65 20 6e 75 ay expect the nu
85b0: 6d 62 65 72 20 6f 66 20 65 78 74 65 6e 64 65 64 mber of extended
85c0: 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77 69 result codes wi
85d0: 6c 6c 20 62 65 20 65 78 70 61 6e 64 0a 2a 2a 20 ll be expand.**
85e0: 6f 76 65 72 20 74 69 6d 65 2e 20 20 53 6f 66 74 over time. Soft
85f0: 77 61 72 65 20 74 68 61 74 20 75 73 65 73 20 65 ware that uses e
8600: 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 xtended result c
8610: 6f 64 65 73 20 73 68 6f 75 6c 64 20 65 78 70 65 odes should expe
8620: 63 74 0a 2a 2a 20 74 6f 20 73 65 65 20 6e 65 77 ct.** to see new
8630: 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 69 6e result codes in
8640: 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 future releases
8650: 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a of SQLite..**.*
8660: 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4f 4b 20 * The SQLITE_OK
8670: 72 65 73 75 6c 74 20 63 6f 64 65 20 77 69 6c 6c result code will
8680: 20 6e 65 76 65 72 20 62 65 20 65 78 74 65 6e 64 never be extend
8690: 65 64 2e 20 20 49 74 20 77 69 6c 6c 20 61 6c 77 ed. It will alw
86a0: 61 79 73 0a 2a 2a 20 62 65 20 65 78 61 63 74 6c ays.** be exactl
86b0: 79 20 7a 65 72 6f 2e 0a 2a 2f 0a 23 64 65 66 69 y zero..*/.#defi
86c0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f ne SQLITE_IOERR_
86d0: 52 45 41 44 20 20 20 20 20 20 20 20 20 20 20 20 READ
86e0: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 (SQLITE_IOERR
86f0: 7c 20 28 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e | (1<<8)).#defin
8700: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 e SQLITE_IOERR_S
8710: 48 4f 52 54 5f 52 45 41 44 20 20 20 20 20 20 20 HORT_READ
8720: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c (SQLITE_IOERR |
8730: 20 28 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 (2<<8)).#define
8740: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 SQLITE_IOERR_WR
8750: 49 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ITE
8760: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 (SQLITE_IOERR |
8770: 28 33 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 (3<<8)).#define
8780: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 SQLITE_IOERR_FSY
8790: 4e 43 20 20 20 20 20 20 20 20 20 20 20 20 20 28 NC (
87a0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
87b0: 34 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 4<<8)).#define S
87c0: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f QLITE_IOERR_DIR_
87d0: 46 53 59 4e 43 20 20 20 20 20 20 20 20 20 28 53 FSYNC (S
87e0: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 35 QLITE_IOERR | (5
87f0: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 <<8)).#define SQ
8800: 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 LITE_IOERR_TRUNC
8810: 41 54 45 20 20 20 20 20 20 20 20 20 20 28 53 51 ATE (SQ
8820: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 36 3c LITE_IOERR | (6<
8830: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c <8)).#define SQL
8840: 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 ITE_IOERR_FSTAT
8850: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c (SQL
8860: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 37 3c 3c ITE_IOERR | (7<<
8870: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 8)).#define SQLI
8880: 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 20 TE_IOERR_UNLOCK
8890: 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 (SQLI
88a0: 54 45 5f 49 4f 45 52 52 20 7c 20 28 38 3c 3c 38 TE_IOERR | (8<<8
88b0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
88c0: 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 20 20 E_IOERR_RDLOCK
88d0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
88e0: 45 5f 49 4f 45 52 52 20 7c 20 28 39 3c 3c 38 29 E_IOERR | (9<<8)
88f0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ).#define SQLITE
8900: 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 20 20 _IOERR_DELETE
8910: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 (SQLITE
8920: 5f 49 4f 45 52 52 20 7c 20 28 31 30 3c 3c 38 29 _IOERR | (10<<8)
8930: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ).#define SQLITE
8940: 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 20 _IOERR_BLOCKED
8950: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 (SQLITE
8960: 5f 49 4f 45 52 52 20 7c 20 28 31 31 3c 3c 38 29 _IOERR | (11<<8)
8970: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ).#define SQLITE
8980: 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 20 20 20 _IOERR_NOMEM
8990: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 (SQLITE
89a0: 5f 49 4f 45 52 52 20 7c 20 28 31 32 3c 3c 38 29 _IOERR | (12<<8)
89b0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ).#define SQLITE
89c0: 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20 20 20 _IOERR_ACCESS
89d0: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 (SQLITE
89e0: 5f 49 4f 45 52 52 20 7c 20 28 31 33 3c 3c 38 29 _IOERR | (13<<8)
89f0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ).#define SQLITE
8a00: 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 _IOERR_CHECKRESE
8a10: 52 56 45 44 4c 4f 43 4b 20 28 53 51 4c 49 54 45 RVEDLOCK (SQLITE
8a20: 5f 49 4f 45 52 52 20 7c 20 28 31 34 3c 3c 38 29 _IOERR | (14<<8)
8a30: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ).#define SQLITE
8a40: 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 20 20 20 20 _IOERR_LOCK
8a50: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 (SQLITE
8a60: 5f 49 4f 45 52 52 20 7c 20 28 31 35 3c 3c 38 29 _IOERR | (15<<8)
8a70: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ).#define SQLITE
8a80: 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 20 20 20 20 _IOERR_CLOSE
8a90: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 (SQLITE
8aa0: 5f 49 4f 45 52 52 20 7c 20 28 31 36 3c 3c 38 29 _IOERR | (16<<8)
8ab0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ).#define SQLITE
8ac0: 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 _IOERR_DIR_CLOSE
8ad0: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 (SQLITE
8ae0: 5f 49 4f 45 52 52 20 7c 20 28 31 37 3c 3c 38 29 _IOERR | (17<<8)
8af0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ).#define SQLITE
8b00: 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 _LOCKED_SHAREDCA
8b10: 43 48 45 20 20 20 20 20 20 28 53 51 4c 49 54 45 CHE (SQLITE
8b20: 5f 4c 4f 43 4b 45 44 20 7c 20 28 31 3c 3c 38 29 _LOCKED | (1<<8)
8b30: 20 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 )../*.** CAPI3R
8b40: 45 46 3a 20 46 6c 61 67 73 20 46 6f 72 20 46 69 EF: Flags For Fi
8b50: 6c 65 20 4f 70 65 6e 20 4f 70 65 72 61 74 69 6f le Open Operatio
8b60: 6e 73 20 7b 48 31 30 32 33 30 7d 20 3c 48 31 31 ns {H10230} <H11
8b70: 31 32 30 3e 20 3c 48 31 32 37 30 30 3e 0a 2a 2a 120> <H12700>.**
8b80: 0a 2a 2a 20 54 68 65 73 65 20 62 69 74 20 76 61 .** These bit va
8b90: 6c 75 65 73 20 61 72 65 20 69 6e 74 65 6e 64 65 lues are intende
8ba0: 64 20 66 6f 72 20 75 73 65 20 69 6e 20 74 68 65 d for use in the
8bb0: 0a 2a 2a 20 33 72 64 20 70 61 72 61 6d 65 74 65 .** 3rd paramete
8bc0: 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 r to the [sqlite
8bd0: 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 6e 74 3_open_v2()] int
8be0: 65 72 66 61 63 65 20 61 6e 64 0a 2a 2a 20 69 6e erface and.** in
8bf0: 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 the 4th paramet
8c00: 65 72 20 74 6f 20 74 68 65 20 78 4f 70 65 6e 20 er to the xOpen
8c10: 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a 2a method of the.**
8c20: 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f [sqlite3_vfs] o
8c30: 62 6a 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e bject..*/.#defin
8c40: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 e SQLITE_OPEN_RE
8c50: 41 44 4f 4e 4c 59 20 20 20 20 20 20 20 20 20 30 ADONLY 0
8c60: 78 30 30 30 30 30 30 30 31 20 20 2f 2a 20 4f 6b x00000001 /* Ok
8c70: 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 for sqlite3_ope
8c80: 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e n_v2() */.#defin
8c90: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 e SQLITE_OPEN_RE
8ca0: 41 44 57 52 49 54 45 20 20 20 20 20 20 20 20 30 ADWRITE 0
8cb0: 78 30 30 30 30 30 30 30 32 20 20 2f 2a 20 4f 6b x00000002 /* Ok
8cc0: 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 for sqlite3_ope
8cd0: 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e n_v2() */.#defin
8ce0: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 e SQLITE_OPEN_CR
8cf0: 45 41 54 45 20 20 20 20 20 20 20 20 20 20 20 30 EATE 0
8d00: 78 30 30 30 30 30 30 30 34 20 20 2f 2a 20 4f 6b x00000004 /* Ok
8d10: 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 for sqlite3_ope
8d20: 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e n_v2() */.#defin
8d30: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 e SQLITE_OPEN_DE
8d40: 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 20 20 20 30 LETEONCLOSE 0
8d50: 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20 56 46 x00000008 /* VF
8d60: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e S only */.#defin
8d70: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 e SQLITE_OPEN_EX
8d80: 43 4c 55 53 49 56 45 20 20 20 20 20 20 20 20 30 CLUSIVE 0
8d90: 78 30 30 30 30 30 30 31 30 20 20 2f 2a 20 56 46 x00000010 /* VF
8da0: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e S only */.#defin
8db0: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 e SQLITE_OPEN_MA
8dc0: 49 4e 5f 44 42 20 20 20 20 20 20 20 20 20 20 30 IN_DB 0
8dd0: 78 30 30 30 30 30 31 30 30 20 20 2f 2a 20 56 46 x00000100 /* VF
8de0: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e S only */.#defin
8df0: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 e SQLITE_OPEN_TE
8e00: 4d 50 5f 44 42 20 20 20 20 20 20 20 20 20 20 30 MP_DB 0
8e10: 78 30 30 30 30 30 32 30 30 20 20 2f 2a 20 56 46 x00000200 /* VF
8e20: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e S only */.#defin
8e30: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 e SQLITE_OPEN_TR
8e40: 41 4e 53 49 45 4e 54 5f 44 42 20 20 20 20 20 30 ANSIENT_DB 0
8e50: 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20 56 46 x00000400 /* VF
8e60: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e S only */.#defin
8e70: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 e SQLITE_OPEN_MA
8e80: 49 4e 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20 30 IN_JOURNAL 0
8e90: 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20 56 46 x00000800 /* VF
8ea0: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e S only */.#defin
8eb0: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 e SQLITE_OPEN_TE
8ec0: 4d 50 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20 30 MP_JOURNAL 0
8ed0: 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20 56 46 x00001000 /* VF
8ee0: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e S only */.#defin
8ef0: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 e SQLITE_OPEN_SU
8f00: 42 4a 4f 55 52 4e 41 4c 20 20 20 20 20 20 20 30 BJOURNAL 0
8f10: 78 30 30 30 30 32 30 30 30 20 20 2f 2a 20 56 46 x00002000 /* VF
8f20: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e S only */.#defin
8f30: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 e SQLITE_OPEN_MA
8f40: 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 20 20 30 STER_JOURNAL 0
8f50: 78 30 30 30 30 34 30 30 30 20 20 2f 2a 20 56 46 x00004000 /* VF
8f60: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e S only */.#defin
8f70: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f e SQLITE_OPEN_NO
8f80: 4d 55 54 45 58 20 20 20 20 20 20 20 20 20 20 30 MUTEX 0
8f90: 78 30 30 30 30 38 30 30 30 20 20 2f 2a 20 4f 6b x00008000 /* Ok
8fa0: 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 for sqlite3_ope
8fb0: 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e n_v2() */.#defin
8fc0: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 e SQLITE_OPEN_FU
8fd0: 4c 4c 4d 55 54 45 58 20 20 20 20 20 20 20 20 30 LLMUTEX 0
8fe0: 78 30 30 30 31 30 30 30 30 20 20 2f 2a 20 4f 6b x00010000 /* Ok
8ff0: 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 for sqlite3_ope
9000: 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e n_v2() */.#defin
9010: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 e SQLITE_OPEN_SH
9020: 41 52 45 44 43 41 43 48 45 20 20 20 20 20 20 30 AREDCACHE 0
9030: 78 30 30 30 32 30 30 30 30 20 20 2f 2a 20 4f 6b x00020000 /* Ok
9040: 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 for sqlite3_ope
9050: 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e n_v2() */.#defin
9060: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 e SQLITE_OPEN_PR
9070: 49 56 41 54 45 43 41 43 48 45 20 20 20 20 20 30 IVATECACHE 0
9080: 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20 4f 6b x00040000 /* Ok
9090: 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 for sqlite3_ope
90a0: 6e 5f 76 32 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a n_v2() */../*.**
90b0: 20 43 41 50 49 33 52 45 46 3a 20 44 65 76 69 63 CAPI3REF: Devic
90c0: 65 20 43 68 61 72 61 63 74 65 72 69 73 74 69 63 e Characteristic
90d0: 73 20 7b 48 31 30 32 34 30 7d 20 3c 48 31 31 31 s {H10240} <H111
90e0: 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 44 20>.**.** The xD
90f0: 65 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 65 eviceCapabilitie
9100: 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 s method of the
9110: 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 [sqlite3_io_meth
9120: 6f 64 73 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 72 ods].** object r
9130: 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 65 eturns an intege
9140: 72 20 77 68 69 63 68 20 69 73 20 61 20 76 65 63 r which is a vec
9150: 74 6f 72 20 6f 66 20 74 68 65 20 74 68 65 73 65 tor of the these
9160: 0a 2a 2a 20 62 69 74 20 76 61 6c 75 65 73 20 65 .** bit values e
9170: 78 70 72 65 73 73 69 6e 67 20 49 2f 4f 20 63 68 xpressing I/O ch
9180: 61 72 61 63 74 65 72 69 73 74 69 63 73 20 6f 66 aracteristics of
9190: 20 74 68 65 20 6d 61 73 73 20 73 74 6f 72 61 67 the mass storag
91a0: 65 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61 74 e.** device that
91b0: 20 68 6f 6c 64 73 20 74 68 65 20 66 69 6c 65 20 holds the file
91c0: 74 68 61 74 20 74 68 65 20 5b 73 71 6c 69 74 65 that the [sqlite
91d0: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 3_io_methods].**
91e0: 20 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a refers to..**.*
91f0: 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 * The SQLITE_IOC
9200: 41 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65 72 AP_ATOMIC proper
9210: 74 79 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6c ty means that al
9220: 6c 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20 61 l writes of.** a
9230: 6e 79 20 73 69 7a 65 20 61 72 65 20 61 74 6f 6d ny size are atom
9240: 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f ic. The SQLITE_
9250: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 IOCAP_ATOMICnnn
9260: 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 74 values.** mean t
9270: 68 61 74 20 77 72 69 74 65 73 20 6f 66 20 62 6c hat writes of bl
9280: 6f 63 6b 73 20 74 68 61 74 20 61 72 65 20 6e 6e ocks that are nn
9290: 6e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 n bytes in size
92a0: 61 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e and.** are align
92b0: 65 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73 73 ed to an address
92c0: 20 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e 74 which is an int
92d0: 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 eger multiple of
92e0: 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d .** nnn are atom
92f0: 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f ic. The SQLITE_
9300: 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e IOCAP_SAFE_APPEN
9310: 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a D value means.**
9320: 20 74 68 61 74 20 77 68 65 6e 20 64 61 74 61 20 that when data
9330: 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 61 is appended to a
9340: 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 20 file, the data
9350: 69 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 66 is appended.** f
9360: 69 72 73 74 20 74 68 65 6e 20 74 68 65 20 73 69 irst then the si
9370: 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 ze of the file i
9380: 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 76 65 s extended, neve
9390: 72 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 77 r the other.** w
93a0: 61 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 65 20 ay around. The
93b0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 SQLITE_IOCAP_SEQ
93c0: 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 79 UENTIAL property
93d0: 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 69 means that.** i
93e0: 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77 72 nformation is wr
93f0: 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 69 6e itten to disk in
9400: 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 20 the same order
9410: 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78 as calls.** to x
9420: 57 72 69 74 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 Write()..*/.#def
9430: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 ine SQLITE_IOCAP
9440: 5f 41 54 4f 4d 49 43 20 20 20 20 20 20 20 20 20 _ATOMIC
9450: 20 30 78 30 30 30 30 30 30 30 31 0a 23 64 65 66 0x00000001.#def
9460: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 ine SQLITE_IOCAP
9470: 5f 41 54 4f 4d 49 43 35 31 32 20 20 20 20 20 20 _ATOMIC512
9480: 20 30 78 30 30 30 30 30 30 30 32 0a 23 64 65 66 0x00000002.#def
9490: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 ine SQLITE_IOCAP
94a0: 5f 41 54 4f 4d 49 43 31 4b 20 20 20 20 20 20 20 _ATOMIC1K
94b0: 20 30 78 30 30 30 30 30 30 30 34 0a 23 64 65 66 0x00000004.#def
94c0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 ine SQLITE_IOCAP
94d0: 5f 41 54 4f 4d 49 43 32 4b 20 20 20 20 20 20 20 _ATOMIC2K
94e0: 20 30 78 30 30 30 30 30 30 30 38 0a 23 64 65 66 0x00000008.#def
94f0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 ine SQLITE_IOCAP
9500: 5f 41 54 4f 4d 49 43 34 4b 20 20 20 20 20 20 20 _ATOMIC4K
9510: 20 30 78 30 30 30 30 30 30 31 30 0a 23 64 65 66 0x00000010.#def
9520: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 ine SQLITE_IOCAP
9530: 5f 41 54 4f 4d 49 43 38 4b 20 20 20 20 20 20 20 _ATOMIC8K
9540: 20 30 78 30 30 30 30 30 30 32 30 0a 23 64 65 66 0x00000020.#def
9550: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 ine SQLITE_IOCAP
9560: 5f 41 54 4f 4d 49 43 31 36 4b 20 20 20 20 20 20 _ATOMIC16K
9570: 20 30 78 30 30 30 30 30 30 34 30 0a 23 64 65 66 0x00000040.#def
9580: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 ine SQLITE_IOCAP
9590: 5f 41 54 4f 4d 49 43 33 32 4b 20 20 20 20 20 20 _ATOMIC32K
95a0: 20 30 78 30 30 30 30 30 30 38 30 0a 23 64 65 66 0x00000080.#def
95b0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 ine SQLITE_IOCAP
95c0: 5f 41 54 4f 4d 49 43 36 34 4b 20 20 20 20 20 20 _ATOMIC64K
95d0: 20 30 78 30 30 30 30 30 31 30 30 0a 23 64 65 66 0x00000100.#def
95e0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 ine SQLITE_IOCAP
95f0: 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 20 20 20 _SAFE_APPEND
9600: 20 30 78 30 30 30 30 30 32 30 30 0a 23 64 65 66 0x00000200.#def
9610: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 ine SQLITE_IOCAP
9620: 5f 53 45 51 55 45 4e 54 49 41 4c 20 20 20 20 20 _SEQUENTIAL
9630: 20 30 78 30 30 30 30 30 34 30 30 0a 0a 2f 2a 0a 0x00000400../*.
9640: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 6c ** CAPI3REF: Fil
9650: 65 20 4c 6f 63 6b 69 6e 67 20 4c 65 76 65 6c 73 e Locking Levels
9660: 20 7b 48 31 30 32 35 30 7d 20 3c 48 31 31 31 32 {H10250} <H1112
9670: 30 3e 20 3c 48 31 31 33 31 30 3e 0a 2a 2a 0a 2a 0> <H11310>.**.*
9680: 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 6f 6e * SQLite uses on
9690: 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 e of these integ
96a0: 65 72 20 76 61 6c 75 65 73 20 61 73 20 74 68 65 er values as the
96b0: 20 73 65 63 6f 6e 64 0a 2a 2a 20 61 72 67 75 6d second.** argum
96c0: 65 6e 74 20 74 6f 20 63 61 6c 6c 73 20 69 74 20 ent to calls it
96d0: 6d 61 6b 65 73 20 74 6f 20 74 68 65 20 78 4c 6f makes to the xLo
96e0: 63 6b 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63 6b ck() and xUnlock
96f0: 28 29 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 () methods.** of
9700: 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f an [sqlite3_io_
9710: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e methods] object.
9720: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
9730: 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 20 20 20 20 TE_LOCK_NONE
9740: 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 0.#define
9750: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 SQLITE_LOCK_SHAR
9760: 45 44 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 ED 1.#def
9770: 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f ine SQLITE_LOCK_
9780: 52 45 53 45 52 56 45 44 20 20 20 20 20 20 32 0a RESERVED 2.
9790: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
97a0: 4f 43 4b 5f 50 45 4e 44 49 4e 47 20 20 20 20 20 OCK_PENDING
97b0: 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 3.#define SQLI
97c0: 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 TE_LOCK_EXCLUSIV
97d0: 45 20 20 20 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 E 4../*.** C
97e0: 41 50 49 33 52 45 46 3a 20 53 79 6e 63 68 72 6f API3REF: Synchro
97f0: 6e 69 7a 61 74 69 6f 6e 20 54 79 70 65 20 46 6c nization Type Fl
9800: 61 67 73 20 7b 48 31 30 32 36 30 7d 20 3c 48 31 ags {H10260} <H1
9810: 31 31 32 30 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 1120>.**.** When
9820: 20 53 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 SQLite invokes
9830: 74 68 65 20 78 53 79 6e 63 28 29 20 6d 65 74 68 the xSync() meth
9840: 6f 64 20 6f 66 20 61 6e 0a 2a 2a 20 5b 73 71 6c od of an.** [sql
9850: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d ite3_io_methods]
9860: 20 6f 62 6a 65 63 74 20 69 74 20 75 73 65 73 20 object it uses
9870: 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 a combination of
9880: 0a 2a 2a 20 74 68 65 73 65 20 69 6e 74 65 67 65 .** these intege
9890: 72 20 76 61 6c 75 65 73 20 61 73 20 74 68 65 20 r values as the
98a0: 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e second argument.
98b0: 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 .**.** When the
98c0: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 SQLITE_SYNC_DATA
98d0: 4f 4e 4c 59 20 66 6c 61 67 20 69 73 20 75 73 65 ONLY flag is use
98e0: 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 d, it means that
98f0: 20 74 68 65 0a 2a 2a 20 73 79 6e 63 20 6f 70 65 the.** sync ope
9900: 72 61 74 69 6f 6e 20 6f 6e 6c 79 20 6e 65 65 64 ration only need
9910: 73 20 74 6f 20 66 6c 75 73 68 20 64 61 74 61 20 s to flush data
9920: 74 6f 20 6d 61 73 73 20 73 74 6f 72 61 67 65 2e to mass storage.
9930: 20 20 49 6e 6f 64 65 0a 2a 2a 20 69 6e 66 6f 72 Inode.** infor
9940: 6d 61 74 69 6f 6e 20 6e 65 65 64 20 6e 6f 74 20 mation need not
9950: 62 65 20 66 6c 75 73 68 65 64 2e 20 49 66 20 74 be flushed. If t
9960: 68 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 he lower four bi
9970: 74 73 20 6f 66 20 74 68 65 20 66 6c 61 67 0a 2a ts of the flag.*
9980: 2a 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f 53 * equal SQLITE_S
9990: 59 4e 43 5f 4e 4f 52 4d 41 4c 2c 20 74 68 61 74 YNC_NORMAL, that
99a0: 20 6d 65 61 6e 73 20 74 6f 20 75 73 65 20 6e 6f means to use no
99b0: 72 6d 61 6c 20 66 73 79 6e 63 28 29 20 73 65 6d rmal fsync() sem
99c0: 61 6e 74 69 63 73 2e 0a 2a 2a 20 49 66 20 74 68 antics..** If th
99d0: 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 74 e lower four bit
99e0: 73 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f 53 s equal SQLITE_S
99f0: 59 4e 43 5f 46 55 4c 4c 2c 20 74 68 61 74 20 6d YNC_FULL, that m
9a00: 65 61 6e 73 0a 2a 2a 20 74 6f 20 75 73 65 20 4d eans.** to use M
9a10: 61 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66 75 ac OS X style fu
9a20: 6c 6c 73 79 6e 63 20 69 6e 73 74 65 61 64 20 6f llsync instead o
9a30: 66 20 66 73 79 6e 63 28 29 2e 0a 2a 2f 0a 23 64 f fsync()..*/.#d
9a40: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e efine SQLITE_SYN
9a50: 43 5f 4e 4f 52 4d 41 4c 20 20 20 20 20 20 20 20 C_NORMAL
9a60: 30 78 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20 0x00002.#define
9a70: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c SQLITE_SYNC_FULL
9a80: 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 0x0000
9a90: 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 3.#define SQLITE
9aa0: 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 20 _SYNC_DATAONLY
9ab0: 20 20 20 20 30 78 30 30 30 31 30 0a 0a 2f 2a 0a 0x00010../*.
9ac0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 ** CAPI3REF: OS
9ad0: 49 6e 74 65 72 66 61 63 65 20 4f 70 65 6e 20 46 Interface Open F
9ae0: 69 6c 65 20 48 61 6e 64 6c 65 20 7b 48 31 31 31 ile Handle {H111
9af0: 31 30 7d 20 3c 53 32 30 31 31 30 3e 0a 2a 2a 0a 10} <S20110>.**.
9b00: 2a 2a 20 41 6e 20 5b 73 71 6c 69 74 65 33 5f 66 ** An [sqlite3_f
9b10: 69 6c 65 5d 20 6f 62 6a 65 63 74 20 72 65 70 72 ile] object repr
9b20: 65 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20 66 esents an open f
9b30: 69 6c 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 5b ile in the .** [
9b40: 73 71 6c 69 74 65 33 5f 76 66 73 20 7c 20 4f 53 sqlite3_vfs | OS
9b50: 20 69 6e 74 65 72 66 61 63 65 20 6c 61 79 65 72 interface layer
9b60: 5d 2e 20 20 49 6e 64 69 76 69 64 75 61 6c 20 4f ]. Individual O
9b70: 53 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 69 S interface.** i
9b80: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 mplementations w
9b90: 69 6c 6c 0a 2a 2a 20 77 61 6e 74 20 74 6f 20 73 ill.** want to s
9ba0: 75 62 63 6c 61 73 73 20 74 68 69 73 20 6f 62 6a ubclass this obj
9bb0: 65 63 74 20 62 79 20 61 70 70 65 6e 64 69 6e 67 ect by appending
9bc0: 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c additional fiel
9bd0: 64 73 0a 2a 2a 20 66 6f 72 20 74 68 65 69 72 20 ds.** for their
9be0: 6f 77 6e 20 75 73 65 2e 20 20 54 68 65 20 70 4d own use. The pM
9bf0: 65 74 68 6f 64 73 20 65 6e 74 72 79 20 69 73 20 ethods entry is
9c00: 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a a pointer to an.
9c10: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d ** [sqlite3_io_m
9c20: 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 74 ethods] object t
9c30: 68 61 74 20 64 65 66 69 6e 65 73 20 6d 65 74 68 hat defines meth
9c40: 6f 64 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 69 ods for performi
9c50: 6e 67 0a 2a 2a 20 49 2f 4f 20 6f 70 65 72 61 74 ng.** I/O operat
9c60: 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6f 70 65 6e ions on the open
9c70: 20 66 69 6c 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 file..*/.typede
9c80: 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 f struct sqlite3
9c90: 5f 66 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 69 _file sqlite3_fi
9ca0: 6c 65 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 le;.struct sqlit
9cb0: 65 33 5f 66 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 e3_file {. cons
9cc0: 74 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 t struct sqlite3
9cd0: 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 _io_methods *pMe
9ce0: 74 68 6f 64 73 3b 20 20 2f 2a 20 4d 65 74 68 6f thods; /* Metho
9cf0: 64 73 20 66 6f 72 20 61 6e 20 6f 70 65 6e 20 66 ds for an open f
9d00: 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ile */.};../*.**
9d10: 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e CAPI3REF: OS In
9d20: 74 65 72 66 61 63 65 20 46 69 6c 65 20 56 69 72 terface File Vir
9d30: 74 75 61 6c 20 4d 65 74 68 6f 64 73 20 4f 62 6a tual Methods Obj
9d40: 65 63 74 20 7b 48 31 31 31 32 30 7d 20 3c 53 32 ect {H11120} <S2
9d50: 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 45 76 65 72 0110>.**.** Ever
9d60: 79 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 62 79 y file opened by
9d70: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 the [sqlite3_vf
9d80: 73 5d 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 s] xOpen method
9d90: 70 6f 70 75 6c 61 74 65 73 20 61 6e 0a 2a 2a 20 populates an.**
9da0: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f [sqlite3_file] o
9db0: 62 6a 65 63 74 20 28 6f 72 2c 20 6d 6f 72 65 20 bject (or, more
9dc0: 63 6f 6d 6d 6f 6e 6c 79 2c 20 61 20 73 75 62 63 commonly, a subc
9dd0: 6c 61 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20 5b lass of the.** [
9de0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 sqlite3_file] ob
9df0: 6a 65 63 74 29 20 77 69 74 68 20 61 20 70 6f 69 ject) with a poi
9e00: 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 nter to an insta
9e10: 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 nce of this obje
9e20: 63 74 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 ct..** This obje
9e30: 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 6d ct defines the m
9e40: 65 74 68 6f 64 73 20 75 73 65 64 20 74 6f 20 70 ethods used to p
9e50: 65 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 20 6f erform various o
9e60: 70 65 72 61 74 69 6f 6e 73 0a 2a 2a 20 61 67 61 perations.** aga
9e70: 69 6e 73 74 20 74 68 65 20 6f 70 65 6e 20 66 69 inst the open fi
9e80: 6c 65 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 le represented b
9e90: 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 y the [sqlite3_f
9ea0: 69 6c 65 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a ile] object..**.
9eb0: 2a 2a 20 49 66 20 74 68 65 20 78 4f 70 65 6e 20 ** If the xOpen
9ec0: 6d 65 74 68 6f 64 20 73 65 74 73 20 74 68 65 20 method sets the
9ed0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65 sqlite3_file.pMe
9ee0: 74 68 6f 64 73 20 65 6c 65 6d 65 6e 74 20 0a 2a thods element .*
9ef0: 2a 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 * to a non-NULL
9f00: 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 74 68 pointer, then th
9f10: 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 e sqlite3_io_met
9f20: 68 6f 64 73 2e 78 43 6c 6f 73 65 20 6d 65 74 68 hods.xClose meth
9f30: 6f 64 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e 76 od.** may be inv
9f40: 6f 6b 65 64 20 65 76 65 6e 20 69 66 20 74 68 65 oked even if the
9f50: 20 78 4f 70 65 6e 20 72 65 70 6f 72 74 65 64 20 xOpen reported
9f60: 74 68 61 74 20 69 74 20 66 61 69 6c 65 64 2e 20 that it failed.
9f70: 20 54 68 65 0a 2a 2a 20 6f 6e 6c 79 20 77 61 79 The.** only way
9f80: 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 63 61 to prevent a ca
9f90: 6c 6c 20 74 6f 20 78 43 6c 6f 73 65 20 66 6f 6c ll to xClose fol
9fa0: 6c 6f 77 69 6e 67 20 61 20 66 61 69 6c 65 64 20 lowing a failed
9fb0: 78 4f 70 65 6e 0a 2a 2a 20 69 73 20 66 6f 72 20 xOpen.** is for
9fc0: 74 68 65 20 78 4f 70 65 6e 20 74 6f 20 73 65 74 the xOpen to set
9fd0: 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c the sqlite3_fil
9fe0: 65 2e 70 4d 65 74 68 6f 64 73 20 65 6c 65 6d 65 e.pMethods eleme
9ff0: 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a nt to NULL..**.*
a000: 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 * The flags argu
a010: 6d 65 6e 74 20 74 6f 20 78 53 79 6e 63 20 6d 61 ment to xSync ma
a020: 79 20 62 65 20 6f 6e 65 20 6f 66 20 5b 53 51 4c y be one of [SQL
a030: 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 5d ITE_SYNC_NORMAL]
a040: 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 53 or.** [SQLITE_S
a050: 59 4e 43 5f 46 55 4c 4c 5d 2e 20 20 54 68 65 20 YNC_FULL]. The
a060: 66 69 72 73 74 20 63 68 6f 69 63 65 20 69 73 20 first choice is
a070: 74 68 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 the normal fsync
a080: 28 29 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e ()..** The secon
a090: 64 20 63 68 6f 69 63 65 20 69 73 20 61 20 4d 61 d choice is a Ma
a0a0: 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66 75 6c c OS X style ful
a0b0: 6c 73 79 6e 63 2e 20 20 54 68 65 20 5b 53 51 4c lsync. The [SQL
a0c0: 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c ITE_SYNC_DATAONL
a0d0: 59 5d 0a 2a 2a 20 66 6c 61 67 20 6d 61 79 20 62 Y].** flag may b
a0e0: 65 20 4f 52 65 64 20 69 6e 20 74 6f 20 69 6e 64 e ORed in to ind
a0f0: 69 63 61 74 65 20 74 68 61 74 20 6f 6e 6c 79 20 icate that only
a100: 74 68 65 20 64 61 74 61 20 6f 66 20 74 68 65 20 the data of the
a110: 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 6e 6f 74 20 file.** and not
a120: 69 74 73 20 69 6e 6f 64 65 20 6e 65 65 64 73 20 its inode needs
a130: 74 6f 20 62 65 20 73 79 6e 63 65 64 2e 0a 2a 2a to be synced..**
a140: 0a 2a 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20 .** The integer
a150: 76 61 6c 75 65 73 20 74 6f 20 78 4c 6f 63 6b 28 values to xLock(
a160: 29 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 ) and xUnlock()
a170: 61 72 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20 3c 75 are one of.** <u
a180: 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 l>.** <li> [SQLI
a190: 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 0a 2a TE_LOCK_NONE],.*
a1a0: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c * <li> [SQLITE_L
a1b0: 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a 20 OCK_SHARED],.**
a1c0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 <li> [SQLITE_LOC
a1d0: 4b 5f 52 45 53 45 52 56 45 44 5d 2c 0a 2a 2a 20 K_RESERVED],.**
a1e0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 <li> [SQLITE_LOC
a1f0: 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 0a 2a K_PENDING], or.*
a200: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c * <li> [SQLITE_L
a210: 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 5d 2e 0a OCK_EXCLUSIVE]..
a220: 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 20 78 4c 6f 63 ** </ul>.** xLoc
a230: 6b 28 29 20 69 6e 63 72 65 61 73 65 73 20 74 68 k() increases th
a240: 65 20 6c 6f 63 6b 2e 20 78 55 6e 6c 6f 63 6b 28 e lock. xUnlock(
a250: 29 20 64 65 63 72 65 61 73 65 73 20 74 68 65 20 ) decreases the
a260: 6c 6f 63 6b 2e 0a 2a 2a 20 54 68 65 20 78 43 68 lock..** The xCh
a270: 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
a280: 29 20 6d 65 74 68 6f 64 20 63 68 65 63 6b 73 20 ) method checks
a290: 77 68 65 74 68 65 72 20 61 6e 79 20 64 61 74 61 whether any data
a2a0: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c base connection,
a2b0: 0a 2a 2a 20 65 69 74 68 65 72 20 69 6e 20 74 68 .** either in th
a2c0: 69 73 20 70 72 6f 63 65 73 73 20 6f 72 20 69 6e is process or in
a2d0: 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 some other proc
a2e0: 65 73 73 2c 20 69 73 20 68 6f 6c 64 69 6e 67 20 ess, is holding
a2f0: 61 20 52 45 53 45 52 56 45 44 2c 0a 2a 2a 20 50 a RESERVED,.** P
a300: 45 4e 44 49 4e 47 2c 20 6f 72 20 45 58 43 4c 55 ENDING, or EXCLU
a310: 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 SIVE lock on the
a320: 20 66 69 6c 65 2e 20 20 49 74 20 72 65 74 75 72 file. It retur
a330: 6e 73 20 74 72 75 65 0a 2a 2a 20 69 66 20 73 75 ns true.** if su
a340: 63 68 20 61 20 6c 6f 63 6b 20 65 78 69 73 74 73 ch a lock exists
a350: 20 61 6e 64 20 66 61 6c 73 65 20 6f 74 68 65 72 and false other
a360: 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 wise..**.** The
a370: 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20 6d xFileControl() m
a380: 65 74 68 6f 64 20 69 73 20 61 20 67 65 6e 65 72 ethod is a gener
a390: 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 ic interface tha
a3a0: 74 20 61 6c 6c 6f 77 73 20 63 75 73 74 6f 6d 0a t allows custom.
a3b0: 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 ** VFS implement
a3c0: 61 74 69 6f 6e 73 20 74 6f 20 64 69 72 65 63 74 ations to direct
a3d0: 6c 79 20 63 6f 6e 74 72 6f 6c 20 61 6e 20 6f 70 ly control an op
a3e0: 65 6e 20 66 69 6c 65 20 75 73 69 6e 67 20 74 68 en file using th
a3f0: 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 e.** [sqlite3_fi
a400: 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e le_control()] in
a410: 74 65 72 66 61 63 65 2e 20 20 54 68 65 20 73 65 terface. The se
a420: 63 6f 6e 64 20 22 6f 70 22 20 61 72 67 75 6d 65 cond "op" argume
a430: 6e 74 20 69 73 20 61 6e 0a 2a 2a 20 69 6e 74 65 nt is an.** inte
a440: 67 65 72 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 ger opcode. The
a450: 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 third argument
a460: 69 73 20 61 20 67 65 6e 65 72 69 63 20 70 6f 69 is a generic poi
a470: 6e 74 65 72 20 69 6e 74 65 6e 64 65 64 20 74 6f nter intended to
a480: 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 .** point to a s
a490: 74 72 75 63 74 75 72 65 20 74 68 61 74 20 6d 61 tructure that ma
a4a0: 79 20 63 6f 6e 74 61 69 6e 20 61 72 67 75 6d 65 y contain argume
a4b0: 6e 74 73 20 6f 72 20 73 70 61 63 65 20 69 6e 20 nts or space in
a4c0: 77 68 69 63 68 20 74 6f 0a 2a 2a 20 77 72 69 74 which to.** writ
a4d0: 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 2e e return values.
a4e0: 20 20 50 6f 74 65 6e 74 69 61 6c 20 75 73 65 73 Potential uses
a4f0: 20 66 6f 72 20 78 46 69 6c 65 43 6f 6e 74 72 6f for xFileContro
a500: 6c 28 29 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20 l() might be.**
a510: 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 65 6e 61 functions to ena
a520: 62 6c 65 20 62 6c 6f 63 6b 69 6e 67 20 6c 6f 63 ble blocking loc
a530: 6b 73 20 77 69 74 68 20 74 69 6d 65 6f 75 74 73 ks with timeouts
a540: 2c 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 0a , to change the.
a550: 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 ** locking strat
a560: 65 67 79 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 egy (for example
a570: 20 74 6f 20 75 73 65 20 64 6f 74 2d 66 69 6c 65 to use dot-file
a580: 20 6c 6f 63 6b 73 29 2c 20 74 6f 20 69 6e 71 75 locks), to inqu
a590: 69 72 65 0a 2a 2a 20 61 62 6f 75 74 20 74 68 65 ire.** about the
a5a0: 20 73 74 61 74 75 73 20 6f 66 20 61 20 6c 6f 63 status of a loc
a5b0: 6b 2c 20 6f 72 20 74 6f 20 62 72 65 61 6b 20 73 k, or to break s
a5c0: 74 61 6c 65 20 6c 6f 63 6b 73 2e 20 20 54 68 65 tale locks. The
a5d0: 20 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65 20 SQLite.** core
a5e0: 72 65 73 65 72 76 65 73 20 61 6c 6c 20 6f 70 63 reserves all opc
a5f0: 6f 64 65 73 20 6c 65 73 73 20 74 68 61 6e 20 31 odes less than 1
a600: 30 30 20 66 6f 72 20 69 74 73 20 6f 77 6e 20 75 00 for its own u
a610: 73 65 2e 0a 2a 2a 20 41 20 5b 53 51 4c 49 54 45 se..** A [SQLITE
a620: 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 _FCNTL_LOCKSTATE
a630: 20 7c 20 6c 69 73 74 20 6f 66 20 6f 70 63 6f 64 | list of opcod
a640: 65 73 5d 20 6c 65 73 73 20 74 68 61 6e 20 31 30 es] less than 10
a650: 30 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 0 is available..
a660: 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 ** Applications
a670: 74 68 61 74 20 64 65 66 69 6e 65 20 61 20 63 75 that define a cu
a680: 73 74 6f 6d 20 78 46 69 6c 65 43 6f 6e 74 72 6f stom xFileContro
a690: 6c 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20 l method should
a6a0: 75 73 65 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 67 use opcodes.** g
a6b0: 72 65 61 74 65 72 20 74 68 61 6e 20 31 30 30 20 reater than 100
a6c0: 74 6f 20 61 76 6f 69 64 20 63 6f 6e 66 6c 69 63 to avoid conflic
a6d0: 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 ts..**.** The xS
a6e0: 65 63 74 6f 72 53 69 7a 65 28 29 20 6d 65 74 68 ectorSize() meth
a6f0: 6f 64 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 od returns the s
a700: 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 74 68 ector size of th
a710: 65 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61 74 e.** device that
a720: 20 75 6e 64 65 72 6c 69 65 73 20 74 68 65 20 66 underlies the f
a730: 69 6c 65 2e 20 20 54 68 65 20 73 65 63 74 6f 72 ile. The sector
a740: 20 73 69 7a 65 20 69 73 20 74 68 65 0a 2a 2a 20 size is the.**
a750: 6d 69 6e 69 6d 75 6d 20 77 72 69 74 65 20 74 68 minimum write th
a760: 61 74 20 63 61 6e 20 62 65 20 70 65 72 66 6f 72 at can be perfor
a770: 6d 65 64 20 77 69 74 68 6f 75 74 20 64 69 73 74 med without dist
a780: 75 72 62 69 6e 67 0a 2a 2a 20 6f 74 68 65 72 20 urbing.** other
a790: 62 79 74 65 73 20 69 6e 20 74 68 65 20 66 69 6c bytes in the fil
a7a0: 65 2e 20 20 54 68 65 20 78 44 65 76 69 63 65 43 e. The xDeviceC
a7b0: 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 29 haracteristics()
a7c0: 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 65 74 75 72 .** method retur
a7d0: 6e 73 20 61 20 62 69 74 20 76 65 63 74 6f 72 20 ns a bit vector
a7e0: 64 65 73 63 72 69 62 69 6e 67 20 62 65 68 61 76 describing behav
a7f0: 69 6f 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 75 iors of the.** u
a800: 6e 64 65 72 6c 79 69 6e 67 20 64 65 76 69 63 65 nderlying device
a810: 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 :.**.** <ul>.**
a820: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 <li> [SQLITE_IOC
a830: 41 50 5f 41 54 4f 4d 49 43 5d 0a 2a 2a 20 3c 6c AP_ATOMIC].** <l
a840: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 i> [SQLITE_IOCAP
a850: 5f 41 54 4f 4d 49 43 35 31 32 5d 0a 2a 2a 20 3c _ATOMIC512].** <
a860: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 li> [SQLITE_IOCA
a870: 50 5f 41 54 4f 4d 49 43 31 4b 5d 0a 2a 2a 20 3c P_ATOMIC1K].** <
a880: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 li> [SQLITE_IOCA
a890: 50 5f 41 54 4f 4d 49 43 32 4b 5d 0a 2a 2a 20 3c P_ATOMIC2K].** <
a8a0: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 li> [SQLITE_IOCA
a8b0: 50 5f 41 54 4f 4d 49 43 34 4b 5d 0a 2a 2a 20 3c P_ATOMIC4K].** <
a8c0: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 li> [SQLITE_IOCA
a8d0: 50 5f 41 54 4f 4d 49 43 38 4b 5d 0a 2a 2a 20 3c P_ATOMIC8K].** <
a8e0: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 li> [SQLITE_IOCA
a8f0: 50 5f 41 54 4f 4d 49 43 31 36 4b 5d 0a 2a 2a 20 P_ATOMIC16K].**
a900: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 <li> [SQLITE_IOC
a910: 41 50 5f 41 54 4f 4d 49 43 33 32 4b 5d 0a 2a 2a AP_ATOMIC32K].**
a920: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f <li> [SQLITE_IO
a930: 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 5d 0a 2a CAP_ATOMIC64K].*
a940: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 * <li> [SQLITE_I
a950: 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 OCAP_SAFE_APPEND
a960: 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 ].** <li> [SQLIT
a970: 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 E_IOCAP_SEQUENTI
a980: 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a AL].** </ul>.**.
a990: 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f ** The SQLITE_IO
a9a0: 43 41 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65 CAP_ATOMIC prope
a9b0: 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 20 61 rty means that a
a9c0: 6c 6c 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20 ll writes of.**
a9d0: 61 6e 79 20 73 69 7a 65 20 61 72 65 20 61 74 6f any size are ato
a9e0: 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 mic. The SQLITE
a9f0: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e _IOCAP_ATOMICnnn
aa00: 20 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 values.** mean
aa10: 74 68 61 74 20 77 72 69 74 65 73 20 6f 66 20 62 that writes of b
aa20: 6c 6f 63 6b 73 20 74 68 61 74 20 61 72 65 20 6e locks that are n
aa30: 6e 6e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 nn bytes in size
aa40: 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67 and.** are alig
aa50: 6e 65 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73 ned to an addres
aa60: 73 20 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e s which is an in
aa70: 74 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f teger multiple o
aa80: 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f f.** nnn are ato
aa90: 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 mic. The SQLITE
aaa0: 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 _IOCAP_SAFE_APPE
aab0: 4e 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a ND value means.*
aac0: 2a 20 74 68 61 74 20 77 68 65 6e 20 64 61 74 61 * that when data
aad0: 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 is appended to
aae0: 61 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 a file, the data
aaf0: 20 69 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 is appended.**
ab00: 66 69 72 73 74 20 74 68 65 6e 20 74 68 65 20 73 first then the s
ab10: 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ize of the file
ab20: 69 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 76 is extended, nev
ab30: 65 72 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 er the other.**
ab40: 77 61 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 65 way around. The
ab50: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 SQLITE_IOCAP_SE
ab60: 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 QUENTIAL propert
ab70: 79 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 y means that.**
ab80: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77 information is w
ab90: 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 69 ritten to disk i
aba0: 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 n the same order
abb0: 20 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 as calls.** to
abc0: 78 57 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 xWrite()..**.**
abd0: 49 66 20 78 52 65 61 64 28 29 20 72 65 74 75 72 If xRead() retur
abe0: 6e 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f ns SQLITE_IOERR_
abf0: 53 48 4f 52 54 5f 52 45 41 44 20 69 74 20 6d 75 SHORT_READ it mu
ac00: 73 74 20 61 6c 73 6f 20 66 69 6c 6c 0a 2a 2a 20 st also fill.**
ac10: 69 6e 20 74 68 65 20 75 6e 72 65 61 64 20 70 6f in the unread po
ac20: 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 62 75 rtions of the bu
ac30: 66 66 65 72 20 77 69 74 68 20 7a 65 72 6f 73 2e ffer with zeros.
ac40: 20 20 41 20 56 46 53 20 74 68 61 74 0a 2a 2a 20 A VFS that.**
ac50: 66 61 69 6c 73 20 74 6f 20 7a 65 72 6f 2d 66 69 fails to zero-fi
ac60: 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 73 20 6d ll short reads m
ac70: 69 67 68 74 20 73 65 65 6d 20 74 6f 20 77 6f 72 ight seem to wor
ac80: 6b 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a 20 k. However,.**
ac90: 66 61 69 6c 75 72 65 20 74 6f 20 7a 65 72 6f 2d failure to zero-
aca0: 66 69 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 73 fill short reads
acb0: 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 will eventually
acc0: 20 6c 65 61 64 20 74 6f 0a 2a 2a 20 64 61 74 61 lead to.** data
acd0: 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e base corruption.
ace0: 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
acf0: 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 ct sqlite3_io_me
ad00: 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 69 6f thods sqlite3_io
ad10: 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 _methods;.struct
ad20: 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
ad30: 6f 64 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 72 ods {. int iVer
ad40: 73 69 6f 6e 3b 0a 20 20 69 6e 74 20 28 2a 78 43 sion;. int (*xC
ad50: 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 66 69 lose)(sqlite3_fi
ad60: 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 le*);. int (*xR
ad70: 65 61 64 29 28 73 71 6c 69 74 65 33 5f 66 69 6c ead)(sqlite3_fil
ad80: 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69 e*, void*, int i
ad90: 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 Amt, sqlite3_int
ada0: 36 34 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74 64 iOfst);. int
adb0: 20 28 2a 78 57 72 69 74 65 29 28 73 71 6c 69 74 (*xWrite)(sqlit
adc0: 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 e3_file*, const
add0: 76 6f 69 64 2a 2c 20 69 6e 74 20 69 41 6d 74 2c void*, int iAmt,
ade0: 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 sqlite3_int64 i
adf0: 4f 66 73 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 Ofst);. int (*x
ae00: 54 72 75 6e 63 61 74 65 29 28 73 71 6c 69 74 65 Truncate)(sqlite
ae10: 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 33 3_file*, sqlite3
ae20: 5f 69 6e 74 36 34 20 73 69 7a 65 29 3b 0a 20 20 _int64 size);.
ae30: 69 6e 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c int (*xSync)(sql
ae40: 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 ite3_file*, int
ae50: 66 6c 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a flags);. int (*
ae60: 78 46 69 6c 65 53 69 7a 65 29 28 73 71 6c 69 74 xFileSize)(sqlit
ae70: 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 e3_file*, sqlite
ae80: 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 3b 3_int64 *pSize);
ae90: 0a 20 20 69 6e 74 20 28 2a 78 4c 6f 63 6b 29 28 . int (*xLock)(
aea0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 sqlite3_file*, i
aeb0: 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 55 6e nt);. int (*xUn
aec0: 6c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 lock)(sqlite3_fi
aed0: 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 le*, int);. int
aee0: 20 28 2a 78 43 68 65 63 6b 52 65 73 65 72 76 65 (*xCheckReserve
aef0: 64 4c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 dLock)(sqlite3_f
af00: 69 6c 65 2a 2c 20 69 6e 74 20 2a 70 52 65 73 4f ile*, int *pResO
af10: 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69 ut);. int (*xFi
af20: 6c 65 43 6f 6e 74 72 6f 6c 29 28 73 71 6c 69 74 leControl)(sqlit
af30: 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 6f 70 e3_file*, int op
af40: 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 3b 0a 20 , void *pArg);.
af50: 20 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 69 int (*xSectorSi
af60: 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ze)(sqlite3_file
af70: 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 76 *);. int (*xDev
af80: 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
af90: 63 73 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 cs)(sqlite3_file
afa0: 2a 29 3b 0a 20 20 2f 2a 20 41 64 64 69 74 69 6f *);. /* Additio
afb0: 6e 61 6c 20 6d 65 74 68 6f 64 73 20 6d 61 79 20 nal methods may
afc0: 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 be added in futu
afd0: 72 65 20 72 65 6c 65 61 73 65 73 20 2a 2f 0a 7d re releases */.}
afe0: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
aff0: 46 3a 20 53 74 61 6e 64 61 72 64 20 46 69 6c 65 F: Standard File
b000: 20 43 6f 6e 74 72 6f 6c 20 4f 70 63 6f 64 65 73 Control Opcodes
b010: 20 7b 48 31 31 33 31 30 7d 20 3c 53 33 30 38 30 {H11310} <S3080
b020: 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 0>.**.** These i
b030: 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 nteger constants
b040: 20 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f 72 are opcodes for
b050: 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f the xFileContro
b060: 6c 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 l method.** of t
b070: 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d he [sqlite3_io_m
b080: 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 61 ethods] object a
b090: 6e 64 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 nd for the [sqli
b0a0: 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c te3_file_control
b0b0: 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 ()].** interface
b0c0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c ..**.** The [SQL
b0d0: 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 ITE_FCNTL_LOCKST
b0e0: 41 54 45 5d 20 6f 70 63 6f 64 65 20 69 73 20 75 ATE] opcode is u
b0f0: 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e sed for debuggin
b100: 67 2e 20 20 54 68 69 73 0a 2a 2a 20 6f 70 63 6f g. This.** opco
b110: 64 65 20 63 61 75 73 65 73 20 74 68 65 20 78 46 de causes the xF
b120: 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f ileControl metho
b130: 64 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 d to write the c
b140: 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 0a urrent state of.
b150: 2a 2a 20 74 68 65 20 6c 6f 63 6b 20 28 6f 6e 65 ** the lock (one
b160: 20 6f 66 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b of [SQLITE_LOCK
b170: 5f 4e 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 5f _NONE], [SQLITE_
b180: 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a LOCK_SHARED],.**
b190: 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 [SQLITE_LOCK_RE
b1a0: 53 45 52 56 45 44 5d 2c 20 5b 53 51 4c 49 54 45 SERVED], [SQLITE
b1b0: 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20 _LOCK_PENDING],
b1c0: 6f 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f or [SQLITE_LOCK_
b1d0: 45 58 43 4c 55 53 49 56 45 5d 29 0a 2a 2a 20 69 EXCLUSIVE]).** i
b1e0: 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 74 nto an integer t
b1f0: 68 61 74 20 74 68 65 20 70 41 72 67 20 61 72 67 hat the pArg arg
b200: 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f 2e ument points to.
b210: 20 54 68 69 73 20 63 61 70 61 62 69 6c 69 74 79 This capability
b220: 0a 2a 2a 20 69 73 20 75 73 65 64 20 64 75 72 69 .** is used duri
b230: 6e 67 20 74 65 73 74 69 6e 67 20 61 6e 64 20 6f ng testing and o
b240: 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 62 65 20 nly needs to be
b250: 73 75 70 70 6f 72 74 65 64 20 77 68 65 6e 20 53 supported when S
b260: 51 4c 49 54 45 5f 54 45 53 54 0a 2a 2a 20 69 73 QLITE_TEST.** is
b270: 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 64 65 defined..*/.#de
b280: 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 fine SQLITE_FCNT
b290: 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 20 20 20 20 L_LOCKSTATE
b2a0: 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 1.#define SQL
b2b0: 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 ITE_GET_LOCKPROX
b2c0: 59 46 49 4c 45 20 20 20 20 20 20 32 0a 23 64 65 YFILE 2.#de
b2d0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 45 54 5f fine SQLITE_SET_
b2e0: 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20 20 20 LOCKPROXYFILE
b2f0: 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 3.#define SQL
b300: 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 20 20 ITE_LAST_ERRNO
b310: 20 20 20 20 20 20 20 20 20 20 20 34 0a 0a 2f 2a 4../*
b320: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 .** CAPI3REF: Mu
b330: 74 65 78 20 48 61 6e 64 6c 65 20 7b 48 31 37 31 tex Handle {H171
b340: 31 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 2a 0a 10} <S20130>.**.
b350: 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 6d 6f 64 ** The mutex mod
b360: 75 6c 65 20 77 69 74 68 69 6e 20 53 51 4c 69 74 ule within SQLit
b370: 65 20 64 65 66 69 6e 65 73 20 5b 73 71 6c 69 74 e defines [sqlit
b380: 65 33 5f 6d 75 74 65 78 5d 20 74 6f 20 62 65 20 e3_mutex] to be
b390: 61 6e 0a 2a 2a 20 61 62 73 74 72 61 63 74 20 74 an.** abstract t
b3a0: 79 70 65 20 66 6f 72 20 61 20 6d 75 74 65 78 20 ype for a mutex
b3b0: 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 53 51 4c object. The SQL
b3c0: 69 74 65 20 63 6f 72 65 20 6e 65 76 65 72 20 6c ite core never l
b3d0: 6f 6f 6b 73 0a 2a 2a 20 61 74 20 74 68 65 20 69 ooks.** at the i
b3e0: 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e nternal represen
b3f0: 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 5b 73 71 tation of an [sq
b400: 6c 69 74 65 33 5f 6d 75 74 65 78 5d 2e 20 20 49 lite3_mutex]. I
b410: 74 20 6f 6e 6c 79 0a 2a 2a 20 64 65 61 6c 73 20 t only.** deals
b420: 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f with pointers to
b430: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 the [sqlite3_mu
b440: 74 65 78 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a tex] object..**.
b450: 2a 2a 20 4d 75 74 65 78 65 73 20 61 72 65 20 63 ** Mutexes are c
b460: 72 65 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71 reated using [sq
b470: 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
b480: 63 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 c()]..*/.typedef
b490: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
b4a0: 6d 75 74 65 78 20 73 71 6c 69 74 65 33 5f 6d 75 mutex sqlite3_mu
b4b0: 74 65 78 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 tex;../*.** CAPI
b4c0: 33 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 3REF: OS Interfa
b4d0: 63 65 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 34 ce Object {H1114
b4e0: 30 7d 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 0} <S20100>.**.*
b4f0: 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
b500: 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 the sqlite3_vfs
b510: 20 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20 object defines
b520: 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 62 65 the interface be
b530: 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c tween.** the SQL
b540: 69 74 65 20 63 6f 72 65 20 61 6e 64 20 74 68 65 ite core and the
b550: 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 underlying oper
b560: 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 54 ating system. T
b570: 68 65 20 22 76 66 73 22 0a 2a 2a 20 69 6e 20 74 he "vfs".** in t
b580: 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6f he name of the o
b590: 62 6a 65 63 74 20 73 74 61 6e 64 73 20 66 6f 72 bject stands for
b5a0: 20 22 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 "virtual file s
b5b0: 79 73 74 65 6d 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 ystem"..**.** Th
b5c0: 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 e value of the i
b5d0: 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 69 73 Version field is
b5e0: 20 69 6e 69 74 69 61 6c 6c 79 20 31 20 62 75 74 initially 1 but
b5f0: 20 6d 61 79 20 62 65 20 6c 61 72 67 65 72 20 69 may be larger i
b600: 6e 0a 2a 2a 20 66 75 74 75 72 65 20 76 65 72 73 n.** future vers
b610: 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 ions of SQLite.
b620: 20 41 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c Additional fiel
b630: 64 73 20 6d 61 79 20 62 65 20 61 70 70 65 6e 64 ds may be append
b640: 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20 6f 62 ed to this.** ob
b650: 6a 65 63 74 20 77 68 65 6e 20 74 68 65 20 69 56 ject when the iV
b660: 65 72 73 69 6f 6e 20 76 61 6c 75 65 20 69 73 20 ersion value is
b670: 69 6e 63 72 65 61 73 65 64 2e 20 20 4e 6f 74 65 increased. Note
b680: 20 74 68 61 74 20 74 68 65 20 73 74 72 75 63 74 that the struct
b690: 75 72 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 ure.** of the sq
b6a0: 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 lite3_vfs object
b6b0: 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 changes in the
b6c0: 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 74 77 transaction betw
b6d0: 65 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 76 65 een.** SQLite ve
b6e0: 72 73 69 6f 6e 20 33 2e 35 2e 39 20 61 6e 64 20 rsion 3.5.9 and
b6f0: 33 2e 36 2e 30 20 61 6e 64 20 79 65 74 20 74 68 3.6.0 and yet th
b700: 65 20 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 e iVersion field
b710: 20 77 61 73 20 6e 6f 74 0a 2a 2a 20 6d 6f 64 69 was not.** modi
b720: 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 fied..**.** The
b730: 73 7a 4f 73 46 69 6c 65 20 66 69 65 6c 64 20 69 szOsFile field i
b740: 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 s the size of th
b750: 65 20 73 75 62 63 6c 61 73 73 65 64 20 5b 73 71 e subclassed [sq
b760: 6c 69 74 65 33 5f 66 69 6c 65 5d 0a 2a 2a 20 73 lite3_file].** s
b770: 74 72 75 63 74 75 72 65 20 75 73 65 64 20 62 79 tructure used by
b780: 20 74 68 69 73 20 56 46 53 2e 20 20 6d 78 50 61 this VFS. mxPa
b790: 74 68 6e 61 6d 65 20 69 73 20 74 68 65 20 6d 61 thname is the ma
b7a0: 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 0a ximum length of.
b7b0: 2a 2a 20 61 20 70 61 74 68 6e 61 6d 65 20 69 6e ** a pathname in
b7c0: 20 74 68 69 73 20 56 46 53 2e 0a 2a 2a 0a 2a 2a this VFS..**.**
b7d0: 20 52 65 67 69 73 74 65 72 65 64 20 73 71 6c 69 Registered sqli
b7e0: 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 20 te3_vfs objects
b7f0: 61 72 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 are kept on a li
b800: 6e 6b 65 64 20 6c 69 73 74 20 66 6f 72 6d 65 64 nked list formed
b810: 20 62 79 0a 2a 2a 20 74 68 65 20 70 4e 65 78 74 by.** the pNext
b820: 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 5b pointer. The [
b830: 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 sqlite3_vfs_regi
b840: 73 74 65 72 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b ster()].** and [
b850: 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 sqlite3_vfs_unre
b860: 67 69 73 74 65 72 28 29 5d 20 69 6e 74 65 72 66 gister()] interf
b870: 61 63 65 73 20 6d 61 6e 61 67 65 20 74 68 69 73 aces manage this
b880: 20 6c 69 73 74 0a 2a 2a 20 69 6e 20 61 20 74 68 list.** in a th
b890: 72 65 61 64 2d 73 61 66 65 20 77 61 79 2e 20 20 read-safe way.
b8a0: 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 The [sqlite3_vfs
b8b0: 5f 66 69 6e 64 28 29 5d 20 69 6e 74 65 72 66 61 _find()] interfa
b8c0: 63 65 0a 2a 2a 20 73 65 61 72 63 68 65 73 20 74 ce.** searches t
b8d0: 68 65 20 6c 69 73 74 2e 20 20 4e 65 69 74 68 65 he list. Neithe
b8e0: 72 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f r the applicatio
b8f0: 6e 20 63 6f 64 65 20 6e 6f 72 20 74 68 65 20 56 n code nor the V
b900: 46 53 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 FS.** implementa
b910: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 20 tion should use
b920: 74 68 65 20 70 4e 65 78 74 20 70 6f 69 6e 74 65 the pNext pointe
b930: 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4e 65 r..**.** The pNe
b940: 78 74 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 xt field is the
b950: 6f 6e 6c 79 20 66 69 65 6c 64 20 69 6e 20 74 68 only field in th
b960: 65 20 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a e sqlite3_vfs.**
b970: 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 structure that
b980: 53 51 4c 69 74 65 20 77 69 6c 6c 20 65 76 65 72 SQLite will ever
b990: 20 6d 6f 64 69 66 79 2e 20 20 53 51 4c 69 74 65 modify. SQLite
b9a0: 20 77 69 6c 6c 20 6f 6e 6c 79 20 61 63 63 65 73 will only acces
b9b0: 73 0a 2a 2a 20 6f 72 20 6d 6f 64 69 66 79 20 74 s.** or modify t
b9c0: 68 69 73 20 66 69 65 6c 64 20 77 68 69 6c 65 20 his field while
b9d0: 68 6f 6c 64 69 6e 67 20 61 20 70 61 72 74 69 63 holding a partic
b9e0: 75 6c 61 72 20 73 74 61 74 69 63 20 6d 75 74 65 ular static mute
b9f0: 78 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 x..** The applic
ba00: 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 ation should nev
ba10: 65 72 20 6d 6f 64 69 66 79 20 61 6e 79 74 68 69 er modify anythi
ba20: 6e 67 20 77 69 74 68 69 6e 20 74 68 65 20 73 71 ng within the sq
ba30: 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20 6f 62 6a lite3_vfs.** obj
ba40: 65 63 74 20 6f 6e 63 65 20 74 68 65 20 6f 62 6a ect once the obj
ba50: 65 63 74 20 68 61 73 20 62 65 65 6e 20 72 65 67 ect has been reg
ba60: 69 73 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 istered..**.** T
ba70: 68 65 20 7a 4e 61 6d 65 20 66 69 65 6c 64 20 68 he zName field h
ba80: 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 olds the name of
ba90: 20 74 68 65 20 56 46 53 20 6d 6f 64 75 6c 65 2e the VFS module.
baa0: 20 20 54 68 65 20 6e 61 6d 65 20 6d 75 73 74 0a The name must.
bab0: 2a 2a 20 62 65 20 75 6e 69 71 75 65 20 61 63 72 ** be unique acr
bac0: 6f 73 73 20 61 6c 6c 20 56 46 53 20 6d 6f 64 75 oss all VFS modu
bad0: 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 les..**.** SQLit
bae0: 65 20 77 69 6c 6c 20 67 75 61 72 61 6e 74 65 65 e will guarantee
baf0: 20 74 68 61 74 20 74 68 65 20 7a 46 69 6c 65 6e that the zFilen
bb00: 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f ame parameter to
bb10: 20 78 4f 70 65 6e 0a 2a 2a 20 69 73 20 65 69 74 xOpen.** is eit
bb20: 68 65 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 her a NULL point
bb30: 65 72 20 6f 72 20 73 74 72 69 6e 67 20 6f 62 74 er or string obt
bb40: 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 78 46 ained.** from xF
bb50: 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29 2e 20 20 ullPathname().
bb60: 53 51 4c 69 74 65 20 66 75 72 74 68 65 72 20 67 SQLite further g
bb70: 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a 2a uarantees that.*
bb80: 2a 20 74 68 65 20 73 74 72 69 6e 67 20 77 69 6c * the string wil
bb90: 6c 20 62 65 20 76 61 6c 69 64 20 61 6e 64 20 75 l be valid and u
bba0: 6e 63 68 61 6e 67 65 64 20 75 6e 74 69 6c 20 78 nchanged until x
bbb0: 43 6c 6f 73 65 28 29 20 69 73 0a 2a 2a 20 63 61 Close() is.** ca
bbc0: 6c 6c 65 64 2e 20 42 65 63 61 75 73 65 20 6f 66 lled. Because of
bbd0: 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 the previous se
bbe0: 6e 74 65 6e 63 65 2c 0a 2a 2a 20 74 68 65 20 5b ntence,.** the [
bbf0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63 61 sqlite3_file] ca
bc00: 6e 20 73 61 66 65 6c 79 20 73 74 6f 72 65 20 61 n safely store a
bc10: 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a pointer to the.
bc20: 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 69 66 20 69 ** filename if i
bc30: 74 20 6e 65 65 64 73 20 74 6f 20 72 65 6d 65 6d t needs to remem
bc40: 62 65 72 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 ber the filename
bc50: 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e for some reason
bc60: 2e 0a 2a 2a 20 49 66 20 74 68 65 20 7a 46 69 6c ..** If the zFil
bc70: 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 ename parameter
bc80: 69 73 20 78 4f 70 65 6e 20 69 73 20 61 20 4e 55 is xOpen is a NU
bc90: 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 LL pointer then
bca0: 78 4f 70 65 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e xOpen.** must in
bcb0: 76 65 6e 74 20 69 74 73 20 6f 77 6e 20 74 65 6d vent its own tem
bcc0: 70 6f 72 61 72 79 20 6e 61 6d 65 20 66 6f 72 20 porary name for
bcd0: 74 68 65 20 66 69 6c 65 2e 20 20 57 68 65 6e 65 the file. Whene
bce0: 76 65 72 20 74 68 65 20 0a 2a 2a 20 78 46 69 6c ver the .** xFil
bcf0: 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 ename parameter
bd00: 69 73 20 4e 55 4c 4c 20 69 74 20 77 69 6c 6c 20 is NULL it will
bd10: 61 6c 73 6f 20 62 65 20 74 68 65 20 63 61 73 65 also be the case
bd20: 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 66 6c 61 that the.** fla
bd30: 67 73 20 70 61 72 61 6d 65 74 65 72 20 77 69 6c gs parameter wil
bd40: 6c 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 l include [SQLIT
bd50: 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 E_OPEN_DELETEONC
bd60: 4c 4f 53 45 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 LOSE]..**.** The
bd70: 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 flags argument
bd80: 74 6f 20 78 4f 70 65 6e 28 29 20 69 6e 63 6c 75 to xOpen() inclu
bd90: 64 65 73 20 61 6c 6c 20 62 69 74 73 20 73 65 74 des all bits set
bda0: 20 69 6e 0a 2a 2a 20 74 68 65 20 66 6c 61 67 73 in.** the flags
bdb0: 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 argument to [sq
bdc0: 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d lite3_open_v2()]
bdd0: 2e 20 20 4f 72 20 69 66 20 5b 73 71 6c 69 74 65 . Or if [sqlite
bde0: 33 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 6f 72 20 3_open()].** or
bdf0: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 [sqlite3_open16(
be00: 29 5d 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e )] is used, then
be10: 20 66 6c 61 67 73 20 69 6e 63 6c 75 64 65 73 20 flags includes
be20: 61 74 20 6c 65 61 73 74 0a 2a 2a 20 5b 53 51 4c at least.** [SQL
be30: 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
be40: 54 45 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f 50 TE] | [SQLITE_OP
be50: 45 4e 5f 43 52 45 41 54 45 5d 2e 20 0a 2a 2a 20 EN_CREATE]. .**
be60: 49 66 20 78 4f 70 65 6e 28 29 20 6f 70 65 6e 73 If xOpen() opens
be70: 20 61 20 66 69 6c 65 20 72 65 61 64 2d 6f 6e 6c a file read-onl
be80: 79 20 74 68 65 6e 20 69 74 20 73 65 74 73 20 2a y then it sets *
be90: 70 4f 75 74 46 6c 61 67 73 20 74 6f 0a 2a 2a 20 pOutFlags to.**
bea0: 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f include [SQLITE_
beb0: 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e 20 OPEN_READONLY].
bec0: 20 4f 74 68 65 72 20 62 69 74 73 20 69 6e 20 2a Other bits in *
bed0: 70 4f 75 74 46 6c 61 67 73 20 6d 61 79 20 62 65 pOutFlags may be
bee0: 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 set..**.** SQLi
bef0: 74 65 20 77 69 6c 6c 20 61 6c 73 6f 20 61 64 64 te will also add
bf00: 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c one of the foll
bf10: 6f 77 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 owing flags to t
bf20: 68 65 20 78 4f 70 65 6e 28 29 0a 2a 2a 20 63 61 he xOpen().** ca
bf30: 6c 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e ll, depending on
bf40: 20 74 68 65 20 6f 62 6a 65 63 74 20 62 65 69 6e the object bein
bf50: 67 20 6f 70 65 6e 65 64 3a 0a 2a 2a 0a 2a 2a 20 g opened:.**.**
bf60: 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 <ul>.** <li> [S
bf70: 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
bf80: 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 DB].** <li> [SQ
bf90: 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a LITE_OPEN_MAIN_J
bfa0: 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 OURNAL].** <li>
bfb0: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 [SQLITE_OPEN_TE
bfc0: 4d 50 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 MP_DB].** <li>
bfd0: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d [SQLITE_OPEN_TEM
bfe0: 50 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c P_JOURNAL].** <l
bff0: 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e i> [SQLITE_OPEN
c000: 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 5d 0a 2a _TRANSIENT_DB].*
c010: 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f * <li> [SQLITE_
c020: 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 5d OPEN_SUBJOURNAL]
c030: 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 .** <li> [SQLIT
c040: 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f E_OPEN_MASTER_JO
c050: 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a URNAL].** </ul>.
c060: 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 49 **.** The file I
c070: 2f 4f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f /O implementatio
c080: 6e 20 63 61 6e 20 75 73 65 20 74 68 65 20 6f 62 n can use the ob
c090: 6a 65 63 74 20 74 79 70 65 20 66 6c 61 67 73 20 ject type flags
c0a0: 74 6f 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 65 to.** change the
c0b0: 20 77 61 79 20 69 74 20 64 65 61 6c 73 20 77 69 way it deals wi
c0c0: 74 68 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 65 th files. For e
c0d0: 78 61 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c 69 xample, an appli
c0e0: 63 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 64 cation.** that d
c0f0: 6f 65 73 20 6e 6f 74 20 63 61 72 65 20 61 62 6f oes not care abo
c100: 75 74 20 63 72 61 73 68 20 72 65 63 6f 76 65 72 ut crash recover
c110: 79 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6d 69 y or rollback mi
c120: 67 68 74 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 ght make.** the
c130: 6f 70 65 6e 20 6f 66 20 61 20 6a 6f 75 72 6e 61 open of a journa
c140: 6c 20 66 69 6c 65 20 61 20 6e 6f 2d 6f 70 2e 20 l file a no-op.
c150: 20 57 72 69 74 65 73 20 74 6f 20 74 68 69 73 20 Writes to this
c160: 6a 6f 75 72 6e 61 6c 20 77 6f 75 6c 64 0a 2a 2a journal would.**
c170: 20 61 6c 73 6f 20 62 65 20 6e 6f 2d 6f 70 73 2c also be no-ops,
c180: 20 61 6e 64 20 61 6e 79 20 61 74 74 65 6d 70 74 and any attempt
c190: 20 74 6f 20 72 65 61 64 20 74 68 65 20 6a 6f 75 to read the jou
c1a0: 72 6e 61 6c 20 77 6f 75 6c 64 20 72 65 74 75 72 rnal would retur
c1b0: 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45 52 n.** SQLITE_IOER
c1c0: 52 2e 20 20 4f 72 20 74 68 65 20 69 6d 70 6c 65 R. Or the imple
c1d0: 6d 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 20 mentation might
c1e0: 72 65 63 6f 67 6e 69 7a 65 20 74 68 61 74 20 61 recognize that a
c1f0: 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c database.** fil
c200: 65 20 77 69 6c 6c 20 62 65 20 64 6f 69 6e 67 20 e will be doing
c210: 70 61 67 65 2d 61 6c 69 67 6e 65 64 20 73 65 63 page-aligned sec
c220: 74 6f 72 20 72 65 61 64 73 20 61 6e 64 20 77 72 tor reads and wr
c230: 69 74 65 73 20 69 6e 20 61 20 72 61 6e 64 6f 6d ites in a random
c240: 0a 2a 2a 20 6f 72 64 65 72 20 61 6e 64 20 73 65 .** order and se
c250: 74 20 75 70 20 69 74 73 20 49 2f 4f 20 73 75 62 t up its I/O sub
c260: 73 79 73 74 65 6d 20 61 63 63 6f 72 64 69 6e 67 system according
c270: 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 ly..**.** SQLite
c280: 20 6d 69 67 68 74 20 61 6c 73 6f 20 61 64 64 20 might also add
c290: 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f one of the follo
c2a0: 77 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68 wing flags to th
c2b0: 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 3a 0a e xOpen method:.
c2c0: 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c **.** <ul>.** <l
c2d0: 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f i> [SQLITE_OPEN_
c2e0: 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a DELETEONCLOSE].*
c2f0: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4f * <li> [SQLITE_O
c300: 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 0a 2a PEN_EXCLUSIVE].*
c310: 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 * </ul>.**.** Th
c320: 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 e [SQLITE_OPEN_D
c330: 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 20 66 6c ELETEONCLOSE] fl
c340: 61 67 20 6d 65 61 6e 73 20 74 68 65 20 66 69 6c ag means the fil
c350: 65 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 64 e should be.** d
c360: 65 6c 65 74 65 64 20 77 68 65 6e 20 69 74 20 69 eleted when it i
c370: 73 20 63 6c 6f 73 65 64 2e 20 20 54 68 65 20 5b s closed. The [
c380: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 SQLITE_OPEN_DELE
c390: 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77 69 TEONCLOSE].** wi
c3a0: 6c 6c 20 62 65 20 73 65 74 20 66 6f 72 20 54 45 ll be set for TE
c3b0: 4d 50 20 20 64 61 74 61 62 61 73 65 73 2c 20 6a MP databases, j
c3c0: 6f 75 72 6e 61 6c 73 20 61 6e 64 20 66 6f 72 20 ournals and for
c3d0: 73 75 62 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a 0a subjournals..**.
c3e0: 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f ** The [SQLITE_O
c3f0: 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 20 66 PEN_EXCLUSIVE] f
c400: 6c 61 67 20 69 73 20 61 6c 77 61 79 73 20 75 73 lag is always us
c410: 65 64 20 69 6e 20 63 6f 6e 6a 75 6e 63 74 69 6f ed in conjunctio
c420: 6e 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 53 n.** with the [S
c430: 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 QLITE_OPEN_CREAT
c440: 45 5d 20 66 6c 61 67 2c 20 77 68 69 63 68 20 61 E] flag, which a
c450: 72 65 20 62 6f 74 68 20 64 69 72 65 63 74 6c 79 re both directly
c460: 0a 2a 2a 20 61 6e 61 6c 6f 67 6f 75 73 20 74 6f .** analogous to
c470: 20 74 68 65 20 4f 5f 45 58 43 4c 20 61 6e 64 20 the O_EXCL and
c480: 4f 5f 43 52 45 41 54 20 66 6c 61 67 73 20 6f 66 O_CREAT flags of
c490: 20 74 68 65 20 50 4f 53 49 58 20 6f 70 65 6e 28 the POSIX open(
c4a0: 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 53 ).** API. The S
c4b0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 QLITE_OPEN_EXCLU
c4c0: 53 49 56 45 20 66 6c 61 67 2c 20 77 68 65 6e 20 SIVE flag, when
c4d0: 70 61 69 72 65 64 20 77 69 74 68 20 74 68 65 20 paired with the
c4e0: 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f .** SQLITE_OPEN_
c4f0: 43 52 45 41 54 45 2c 20 69 73 20 75 73 65 64 20 CREATE, is used
c500: 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 to indicate that
c510: 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 61 6c 77 file should alw
c520: 61 79 73 0a 2a 2a 20 62 65 20 63 72 65 61 74 65 ays.** be create
c530: 64 2c 20 61 6e 64 20 74 68 61 74 20 69 74 20 69 d, and that it i
c540: 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 69 74 s an error if it
c550: 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e already exists.
c560: 0a 2a 2a 20 49 74 20 69 73 20 3c 69 3e 6e 6f 74 .** It is <i>not
c570: 3c 2f 69 3e 20 75 73 65 64 20 74 6f 20 69 6e 64 </i> used to ind
c580: 69 63 61 74 65 20 74 68 65 20 66 69 6c 65 20 73 icate the file s
c590: 68 6f 75 6c 64 20 62 65 20 6f 70 65 6e 65 64 20 hould be opened
c5a0: 0a 2a 2a 20 66 6f 72 20 65 78 63 6c 75 73 69 76 .** for exclusiv
c5b0: 65 20 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 e access..**.**
c5c0: 41 74 20 6c 65 61 73 74 20 73 7a 4f 73 46 69 6c At least szOsFil
c5d0: 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 e bytes of memor
c5e0: 79 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 y are allocated
c5f0: 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 74 6f 20 by SQLite.** to
c600: 68 6f 6c 64 20 74 68 65 20 20 5b 73 71 6c 69 74 hold the [sqlit
c610: 65 33 5f 66 69 6c 65 5d 20 73 74 72 75 63 74 75 e3_file] structu
c620: 72 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 re passed as the
c630: 20 74 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 third.** argume
c640: 6e 74 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54 68 nt to xOpen. Th
c650: 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 64 e xOpen method d
c660: 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 0a oes not have to.
c670: 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 ** allocate the
c680: 73 74 72 75 63 74 75 72 65 3b 20 69 74 20 73 68 structure; it sh
c690: 6f 75 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20 69 ould just fill i
c6a0: 74 20 69 6e 2e 20 20 4e 6f 74 65 20 74 68 61 74 t in. Note that
c6b0: 0a 2a 2a 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 .** the xOpen me
c6c0: 74 68 6f 64 20 6d 75 73 74 20 73 65 74 20 74 68 thod must set th
c6d0: 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 e sqlite3_file.p
c6e0: 4d 65 74 68 6f 64 73 20 74 6f 20 65 69 74 68 65 Methods to eithe
c6f0: 72 0a 2a 2a 20 61 20 76 61 6c 69 64 20 5b 73 71 r.** a valid [sq
c700: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
c710: 5d 20 6f 62 6a 65 63 74 20 6f 72 20 74 6f 20 4e ] object or to N
c720: 55 4c 4c 2e 20 20 78 4f 70 65 6e 20 6d 75 73 74 ULL. xOpen must
c730: 20 64 6f 0a 2a 2a 20 74 68 69 73 20 65 76 65 6e do.** this even
c740: 20 69 66 20 74 68 65 20 6f 70 65 6e 20 66 61 69 if the open fai
c750: 6c 73 2e 20 20 53 51 4c 69 74 65 20 65 78 70 65 ls. SQLite expe
c760: 63 74 73 20 74 68 61 74 20 74 68 65 20 73 71 6c cts that the sql
c770: 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68 6f ite3_file.pMetho
c780: 64 73 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 77 69 ds.** element wi
c790: 6c 6c 20 62 65 20 76 61 6c 69 64 20 61 66 74 65 ll be valid afte
c7a0: 72 20 78 4f 70 65 6e 20 72 65 74 75 72 6e 73 20 r xOpen returns
c7b0: 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 regardless of th
c7c0: 65 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 e success.** or
c7d0: 66 61 69 6c 75 72 65 20 6f 66 20 74 68 65 20 78 failure of the x
c7e0: 4f 70 65 6e 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a Open call..**.**
c7f0: 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d The flags argum
c800: 65 6e 74 20 74 6f 20 78 41 63 63 65 73 73 28 29 ent to xAccess()
c810: 20 6d 61 79 20 62 65 20 5b 53 51 4c 49 54 45 5f may be [SQLITE_
c820: 41 43 43 45 53 53 5f 45 58 49 53 54 53 5d 0a 2a ACCESS_EXISTS].*
c830: 2a 20 74 6f 20 74 65 73 74 20 66 6f 72 20 74 68 * to test for th
c840: 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20 61 e existence of a
c850: 20 66 69 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54 file, or [SQLIT
c860: 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 E_ACCESS_READWRI
c870: 54 45 5d 20 74 6f 0a 2a 2a 20 74 65 73 74 20 77 TE] to.** test w
c880: 68 65 74 68 65 72 20 61 20 66 69 6c 65 20 69 73 hether a file is
c890: 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72 readable and wr
c8a0: 69 74 61 62 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 itable, or [SQLI
c8b0: 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 5d 0a TE_ACCESS_READ].
c8c0: 2a 2a 20 74 6f 20 74 65 73 74 20 77 68 65 74 68 ** to test wheth
c8d0: 65 72 20 61 20 66 69 6c 65 20 69 73 20 61 74 20 er a file is at
c8e0: 6c 65 61 73 74 20 72 65 61 64 61 62 6c 65 2e 20 least readable.
c8f0: 20 20 54 68 65 20 66 69 6c 65 20 63 61 6e 20 62 The file can b
c900: 65 20 61 0a 2a 2a 20 64 69 72 65 63 74 6f 72 79 e a.** directory
c910: 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 ..**.** SQLite w
c920: 69 6c 6c 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63 ill always alloc
c930: 61 74 65 20 61 74 20 6c 65 61 73 74 20 6d 78 50 ate at least mxP
c940: 61 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 73 20 athname+1 bytes
c950: 66 6f 72 20 74 68 65 0a 2a 2a 20 6f 75 74 70 75 for the.** outpu
c960: 74 20 62 75 66 66 65 72 20 78 46 75 6c 6c 50 61 t buffer xFullPa
c970: 74 68 6e 61 6d 65 2e 20 20 54 68 65 20 65 78 61 thname. The exa
c980: 63 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6f ct size of the o
c990: 75 74 70 75 74 20 62 75 66 66 65 72 0a 2a 2a 20 utput buffer.**
c9a0: 69 73 20 61 6c 73 6f 20 70 61 73 73 65 64 20 61 is also passed a
c9b0: 73 20 61 20 70 61 72 61 6d 65 74 65 72 20 74 6f s a parameter to
c9c0: 20 62 6f 74 68 20 20 6d 65 74 68 6f 64 73 2e 20 both methods.
c9d0: 49 66 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 If the output bu
c9e0: 66 66 65 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c ffer.** is not l
c9f0: 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 5b 53 51 arge enough, [SQ
ca00: 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 73 LITE_CANTOPEN] s
ca10: 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 hould be returne
ca20: 64 2e 20 53 69 6e 63 65 20 74 68 69 73 20 69 73 d. Since this is
ca30: 0a 2a 2a 20 68 61 6e 64 6c 65 64 20 61 73 20 61 .** handled as a
ca40: 20 66 61 74 61 6c 20 65 72 72 6f 72 20 62 79 20 fatal error by
ca50: 53 51 4c 69 74 65 2c 20 76 66 73 20 69 6d 70 6c SQLite, vfs impl
ca60: 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75 ementations shou
ca70: 6c 64 20 65 6e 64 65 61 76 6f 72 0a 2a 2a 20 74 ld endeavor.** t
ca80: 6f 20 70 72 65 76 65 6e 74 20 74 68 69 73 20 62 o prevent this b
ca90: 79 20 73 65 74 74 69 6e 67 20 6d 78 50 61 74 68 y setting mxPath
caa0: 6e 61 6d 65 20 74 6f 20 61 20 73 75 66 66 69 63 name to a suffic
cab0: 69 65 6e 74 6c 79 20 6c 61 72 67 65 20 76 61 6c iently large val
cac0: 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 ue..**.** The xR
cad0: 61 6e 64 6f 6d 6e 65 73 73 28 29 2c 20 78 53 6c andomness(), xSl
cae0: 65 65 70 28 29 2c 20 61 6e 64 20 78 43 75 72 72 eep(), and xCurr
caf0: 65 6e 74 54 69 6d 65 28 29 20 69 6e 74 65 72 66 entTime() interf
cb00: 61 63 65 73 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 aces.** are not
cb10: 73 74 72 69 63 74 6c 79 20 61 20 70 61 72 74 20 strictly a part
cb20: 6f 66 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 of the filesyste
cb30: 6d 2c 20 62 75 74 20 74 68 65 79 20 61 72 65 0a m, but they are.
cb40: 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 ** included in t
cb50: 68 65 20 56 46 53 20 73 74 72 75 63 74 75 72 65 he VFS structure
cb60: 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 6e 65 73 for completenes
cb70: 73 2e 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f s..** The xRando
cb80: 6d 6e 65 73 73 28 29 20 66 75 6e 63 74 69 6f 6e mness() function
cb90: 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 74 attempts to ret
cba0: 75 72 6e 20 6e 42 79 74 65 73 20 62 79 74 65 73 urn nBytes bytes
cbb0: 0a 2a 2a 20 6f 66 20 67 6f 6f 64 2d 71 75 61 6c .** of good-qual
cbc0: 69 74 79 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 ity randomness i
cbd0: 6e 74 6f 20 7a 4f 75 74 2e 20 20 54 68 65 20 72 nto zOut. The r
cbe0: 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a eturn value is.*
cbf0: 2a 20 74 68 65 20 61 63 74 75 61 6c 20 6e 75 6d * the actual num
cc00: 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
cc10: 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69 randomness obtai
cc20: 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 78 53 6c 65 ned..** The xSle
cc30: 65 70 28 29 20 6d 65 74 68 6f 64 20 63 61 75 73 ep() method caus
cc40: 65 73 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 es the calling t
cc50: 68 72 65 61 64 20 74 6f 20 73 6c 65 65 70 20 66 hread to sleep f
cc60: 6f 72 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20 74 or at.** least t
cc70: 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 he number of mic
cc80: 72 6f 73 65 63 6f 6e 64 73 20 67 69 76 65 6e 2e roseconds given.
cc90: 20 20 54 68 65 20 78 43 75 72 72 65 6e 74 54 69 The xCurrentTi
cca0: 6d 65 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 me().** method r
ccb0: 65 74 75 72 6e 73 20 61 20 4a 75 6c 69 61 6e 20 eturns a Julian
ccc0: 44 61 79 20 4e 75 6d 62 65 72 20 66 6f 72 20 74 Day Number for t
ccd0: 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 65 20 he current date
cce0: 61 6e 64 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2f 0a and time..**.*/.
ccf0: 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 typedef struct s
cd00: 71 6c 69 74 65 33 5f 76 66 73 20 73 71 6c 69 74 qlite3_vfs sqlit
cd10: 65 33 5f 76 66 73 3b 0a 73 74 72 75 63 74 20 73 e3_vfs;.struct s
cd20: 71 6c 69 74 65 33 5f 76 66 73 20 7b 0a 20 20 69 qlite3_vfs {. i
cd30: 6e 74 20 69 56 65 72 73 69 6f 6e 3b 20 20 20 20 nt iVersion;
cd40: 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 75 63 /* Struc
cd50: 74 75 72 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d ture version num
cd60: 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4f ber */. int szO
cd70: 73 46 69 6c 65 3b 20 20 20 20 20 20 20 20 20 20 sFile;
cd80: 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 75 62 /* Size of sub
cd90: 63 6c 61 73 73 65 64 20 73 71 6c 69 74 65 33 5f classed sqlite3_
cda0: 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 file */. int mx
cdb0: 50 61 74 68 6e 61 6d 65 3b 20 20 20 20 20 20 20 Pathname;
cdc0: 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 66 69 /* Maximum fi
cdd0: 6c 65 20 70 61 74 68 6e 61 6d 65 20 6c 65 6e 67 le pathname leng
cde0: 74 68 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f th */. sqlite3_
cdf0: 76 66 73 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 vfs *pNext;
ce00: 20 2f 2a 20 4e 65 78 74 20 72 65 67 69 73 74 65 /* Next registe
ce10: 72 65 64 20 56 46 53 20 2a 2f 0a 20 20 63 6f 6e red VFS */. con
ce20: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 st char *zName;
ce30: 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
ce40: 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 66 69 this virtual fi
ce50: 6c 65 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20 76 le system */. v
ce60: 6f 69 64 20 2a 70 41 70 70 44 61 74 61 3b 20 20 oid *pAppData;
ce70: 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
ce80: 65 72 20 74 6f 20 61 70 70 6c 69 63 61 74 69 6f er to applicatio
ce90: 6e 2d 73 70 65 63 69 66 69 63 20 64 61 74 61 20 n-specific data
cea0: 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e */. int (*xOpen
ceb0: 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 )(sqlite3_vfs*,
cec0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
ced0: 65 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a e, sqlite3_file*
cee0: 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
cef0: 20 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 int flags, int
cf00: 2a 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 69 *pOutFlags);. i
cf10: 6e 74 20 28 2a 78 44 65 6c 65 74 65 29 28 73 71 nt (*xDelete)(sq
cf20: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 lite3_vfs*, cons
cf30: 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 t char *zName, i
cf40: 6e 74 20 73 79 6e 63 44 69 72 29 3b 0a 20 20 69 nt syncDir);. i
cf50: 6e 74 20 28 2a 78 41 63 63 65 73 73 29 28 73 71 nt (*xAccess)(sq
cf60: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 lite3_vfs*, cons
cf70: 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 t char *zName, i
cf80: 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70 nt flags, int *p
cf90: 52 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 ResOut);. int (
cfa0: 2a 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 28 *xFullPathname)(
cfb0: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f sqlite3_vfs*, co
cfc0: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c nst char *zName,
cfd0: 20 69 6e 74 20 6e 4f 75 74 2c 20 63 68 61 72 20 int nOut, char
cfe0: 2a 7a 4f 75 74 29 3b 0a 20 20 76 6f 69 64 20 2a *zOut);. void *
cff0: 28 2a 78 44 6c 4f 70 65 6e 29 28 73 71 6c 69 74 (*xDlOpen)(sqlit
d000: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 e3_vfs*, const c
d010: 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 3b har *zFilename);
d020: 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 45 72 72 . void (*xDlErr
d030: 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a or)(sqlite3_vfs*
d040: 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 , int nByte, cha
d050: 72 20 2a 7a 45 72 72 4d 73 67 29 3b 0a 20 20 76 r *zErrMsg);. v
d060: 6f 69 64 20 28 2a 28 2a 78 44 6c 53 79 6d 29 28 oid (*(*xDlSym)(
d070: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 76 6f 69 sqlite3_vfs*,voi
d080: 64 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a d*, const char *
d090: 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29 3b zSymbol))(void);
d0a0: 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 43 6c 6f . void (*xDlClo
d0b0: 73 65 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a se)(sqlite3_vfs*
d0c0: 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 , void*);. int
d0d0: 28 2a 78 52 61 6e 64 6f 6d 6e 65 73 73 29 28 73 (*xRandomness)(s
d0e0: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 qlite3_vfs*, int
d0f0: 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 4f nByte, char *zO
d100: 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 6c ut);. int (*xSl
d110: 65 65 70 29 28 73 71 6c 69 74 65 33 5f 76 66 73 eep)(sqlite3_vfs
d120: 2a 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 6f *, int microseco
d130: 6e 64 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 nds);. int (*xC
d140: 75 72 72 65 6e 74 54 69 6d 65 29 28 73 71 6c 69 urrentTime)(sqli
d150: 74 65 33 5f 76 66 73 2a 2c 20 64 6f 75 62 6c 65 te3_vfs*, double
d160: 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 47 65 74 *);. int (*xGet
d170: 4c 61 73 74 45 72 72 6f 72 29 28 73 71 6c 69 74 LastError)(sqlit
d180: 65 33 5f 76 66 73 2a 2c 20 69 6e 74 2c 20 63 68 e3_vfs*, int, ch
d190: 61 72 20 2a 29 3b 0a 20 20 2f 2a 20 4e 65 77 20 ar *);. /* New
d1a0: 66 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61 70 fields may be ap
d1b0: 70 65 6e 64 65 64 20 69 6e 20 66 69 67 75 72 65 pended in figure
d1c0: 20 76 65 72 73 69 6f 6e 73 2e 20 20 54 68 65 20 versions. The
d1d0: 69 56 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 76 61 iVersion. ** va
d1e0: 6c 75 65 20 77 69 6c 6c 20 69 6e 63 72 65 6d 65 lue will increme
d1f0: 6e 74 20 77 68 65 6e 65 76 65 72 20 74 68 69 73 nt whenever this
d200: 20 68 61 70 70 65 6e 73 2e 20 2a 2f 0a 7d 3b 0a happens. */.};.
d210: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
d220: 20 46 6c 61 67 73 20 66 6f 72 20 74 68 65 20 78 Flags for the x
d230: 41 63 63 65 73 73 20 56 46 53 20 6d 65 74 68 6f Access VFS metho
d240: 64 20 7b 48 31 31 31 39 30 7d 20 3c 48 31 31 31 d {H11190} <H111
d250: 34 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 40>.**.** These
d260: 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 integer constant
d270: 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 s can be used as
d280: 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d the third param
d290: 65 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 78 eter to.** the x
d2a0: 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 Access method of
d2b0: 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 an [sqlite3_vfs
d2c0: 5d 20 6f 62 6a 65 63 74 2e 20 7b 45 4e 44 7d 20 ] object. {END}
d2d0: 20 54 68 65 79 20 64 65 74 65 72 6d 69 6e 65 0a They determine.
d2e0: 2a 2a 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 ** what kind of
d2f0: 70 65 72 6d 69 73 73 69 6f 6e 73 20 74 68 65 20 permissions the
d300: 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 69 xAccess method i
d310: 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0a 2a s looking for..*
d320: 2a 20 57 69 74 68 20 53 51 4c 49 54 45 5f 41 43 * With SQLITE_AC
d330: 43 45 53 53 5f 45 58 49 53 54 53 2c 20 74 68 65 CESS_EXISTS, the
d340: 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a xAccess method.
d350: 2a 2a 20 73 69 6d 70 6c 79 20 63 68 65 63 6b 73 ** simply checks
d360: 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69 6c whether the fil
d370: 65 20 65 78 69 73 74 73 2e 0a 2a 2a 20 57 69 74 e exists..** Wit
d380: 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f h SQLITE_ACCESS_
d390: 52 45 41 44 57 52 49 54 45 2c 20 74 68 65 20 78 READWRITE, the x
d3a0: 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a Access method.**
d3b0: 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72 20 checks whether
d3c0: 74 68 65 20 66 69 6c 65 20 69 73 20 62 6f 74 68 the file is both
d3d0: 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72 readable and wr
d3e0: 69 74 61 62 6c 65 2e 0a 2a 2a 20 57 69 74 68 20 itable..** With
d3f0: 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 SQLITE_ACCESS_RE
d400: 41 44 2c 20 74 68 65 20 78 41 63 63 65 73 73 20 AD, the xAccess
d410: 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b 73 method.** checks
d420: 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69 6c whether the fil
d430: 65 20 69 73 20 72 65 61 64 61 62 6c 65 2e 0a 2a e is readable..*
d440: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
d450: 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 20 20 _ACCESS_EXISTS
d460: 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 0.#define SQLI
d470: 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 TE_ACCESS_READWR
d480: 49 54 45 20 31 0a 23 64 65 66 69 6e 65 20 53 51 ITE 1.#define SQ
d490: 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 LITE_ACCESS_READ
d4a0: 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 43 2../*.** C
d4b0: 41 50 49 33 52 45 46 3a 20 49 6e 69 74 69 61 6c API3REF: Initial
d4c0: 69 7a 65 20 54 68 65 20 53 51 4c 69 74 65 20 4c ize The SQLite L
d4d0: 69 62 72 61 72 79 20 7b 48 31 30 31 33 30 7d 20 ibrary {H10130}
d4e0: 3c 53 32 30 30 30 30 3e 3c 53 33 30 31 30 30 3e <S20000><S30100>
d4f0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
d500: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
d510: 72 6f 75 74 69 6e 65 20 69 6e 69 74 69 61 6c 69 routine initiali
d520: 7a 65 73 20 74 68 65 0a 2a 2a 20 53 51 4c 69 74 zes the.** SQLit
d530: 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 e library. The
d540: 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e sqlite3_shutdown
d550: 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64 65 () routine.** de
d560: 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20 72 65 allocates any re
d570: 73 6f 75 72 63 65 73 20 74 68 61 74 20 77 65 72 sources that wer
d580: 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 e allocated by s
d590: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
d5a0: 65 28 29 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 e()..** This rou
d5b0: 74 69 6e 65 73 20 61 72 65 20 64 65 73 69 67 6e tines are design
d5c0: 65 64 20 74 6f 20 61 69 64 20 69 6e 20 70 72 6f ed to aid in pro
d5d0: 63 65 73 73 20 69 6e 69 74 69 61 6c 69 7a 61 74 cess initializat
d5e0: 69 6f 6e 20 61 6e 64 0a 2a 2a 20 73 68 75 74 64 ion and.** shutd
d5f0: 6f 77 6e 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 own on embedded
d600: 73 79 73 74 65 6d 73 2e 20 20 57 6f 72 6b 73 74 systems. Workst
d610: 61 74 69 6f 6e 20 61 70 70 6c 69 63 61 74 69 6f ation applicatio
d620: 6e 73 20 75 73 69 6e 67 0a 2a 2a 20 53 51 4c 69 ns using.** SQLi
d630: 74 65 20 6e 6f 72 6d 61 6c 6c 79 20 64 6f 20 6e te normally do n
d640: 6f 74 20 6e 65 65 64 20 74 6f 20 69 6e 76 6f 6b ot need to invok
d650: 65 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 e either of thes
d660: 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a e routines..**.*
d670: 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 * A call to sqli
d680: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
d690: 20 69 73 20 61 6e 20 22 65 66 66 65 63 74 69 76 is an "effectiv
d6a0: 65 22 20 63 61 6c 6c 20 69 66 20 69 74 20 69 73 e" call if it is
d6b0: 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 .** the first ti
d6c0: 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 me sqlite3_initi
d6d0: 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76 6f 6b alize() is invok
d6e0: 65 64 20 64 75 72 69 6e 67 20 74 68 65 20 6c 69 ed during the li
d6f0: 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 fetime of.** the
d700: 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69 66 20 process, or if
d710: 69 74 20 69 73 20 74 68 65 20 66 69 72 73 74 20 it is the first
d720: 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 time sqlite3_ini
d730: 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76 tialize() is inv
d740: 6f 6b 65 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e oked.** followin
d750: 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 g a call to sqli
d760: 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20 te3_shutdown().
d770: 20 4f 6e 6c 79 20 61 6e 20 65 66 66 65 63 74 69 Only an effecti
d780: 76 65 20 63 61 6c 6c 0a 2a 2a 20 6f 66 20 73 71 ve call.** of sq
d790: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
d7a0: 28 29 20 64 6f 65 73 20 61 6e 79 20 69 6e 69 74 () does any init
d7b0: 69 61 6c 69 7a 61 74 69 6f 6e 2e 20 20 41 6c 6c ialization. All
d7c0: 20 6f 74 68 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 other calls.**
d7d0: 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d are harmless no-
d7e0: 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c ops..**.** A cal
d7f0: 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 l to sqlite3_shu
d800: 74 64 6f 77 6e 28 29 20 69 73 20 61 6e 20 22 65 tdown() is an "e
d810: 66 66 65 63 74 69 76 65 22 20 63 61 6c 6c 20 69 ffective" call i
d820: 66 20 69 74 20 69 73 20 74 68 65 20 66 69 72 73 f it is the firs
d830: 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c t.** call to sql
d840: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 ite3_shutdown()
d850: 73 69 6e 63 65 20 74 68 65 20 6c 61 73 74 20 73 since the last s
d860: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
d870: 65 28 29 2e 20 20 4f 6e 6c 79 0a 2a 2a 20 61 6e e(). Only.** an
d880: 20 65 66 66 65 63 74 69 76 65 20 63 61 6c 6c 20 effective call
d890: 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 to sqlite3_shutd
d8a0: 6f 77 6e 28 29 20 64 6f 65 73 20 61 6e 79 20 64 own() does any d
d8b0: 65 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e einitialization.
d8c0: 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 76 61 .** All other va
d8d0: 6c 69 64 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c lid calls to sql
d8e0: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 ite3_shutdown()
d8f0: 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d are harmless no-
d900: 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ops..**.** The s
d910: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
d920: 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 e() interface is
d930: 20 74 68 72 65 61 64 73 61 66 65 2c 20 62 75 74 threadsafe, but
d940: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 sqlite3_shutdow
d950: 6e 28 29 0a 2a 2a 20 69 73 20 6e 6f 74 2e 20 20 n().** is not.
d960: 54 68 65 20 73 71 6c 69 74 65 33 5f 73 68 75 74 The sqlite3_shut
d970: 64 6f 77 6e 28 29 20 69 6e 74 65 72 66 61 63 65 down() interface
d980: 20 6d 75 73 74 20 6f 6e 6c 79 20 62 65 20 63 61 must only be ca
d990: 6c 6c 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 lled from a.** s
d9a0: 69 6e 67 6c 65 20 74 68 72 65 61 64 2e 20 20 41 ingle thread. A
d9b0: 6c 6c 20 6f 70 65 6e 20 5b 64 61 74 61 62 61 73 ll open [databas
d9c0: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 6d e connections] m
d9d0: 75 73 74 20 62 65 20 63 6c 6f 73 65 64 20 61 6e ust be closed an
d9e0: 64 20 61 6c 6c 0a 2a 2a 20 6f 74 68 65 72 20 53 d all.** other S
d9f0: 51 4c 69 74 65 20 72 65 73 6f 75 72 63 65 73 20 QLite resources
da00: 6d 75 73 74 20 62 65 20 64 65 61 6c 6c 6f 63 61 must be dealloca
da10: 74 65 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 ted prior to inv
da20: 6f 6b 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 oking.** sqlite3
da30: 5f 73 68 75 74 64 6f 77 6e 28 29 2e 0a 2a 2a 0a _shutdown()..**.
da40: 2a 2a 20 41 6d 6f 6e 67 20 6f 74 68 65 72 20 74 ** Among other t
da50: 68 69 6e 67 73 2c 20 73 71 6c 69 74 65 33 5f 69 hings, sqlite3_i
da60: 6e 69 74 69 61 6c 69 7a 65 28 29 20 77 69 6c 6c nitialize() will
da70: 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74 invoke.** sqlit
da80: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 20 53 e3_os_init(). S
da90: 69 6d 69 6c 61 72 6c 79 2c 20 73 71 6c 69 74 65 imilarly, sqlite
daa0: 33 5f 73 68 75 74 64 6f 77 6e 28 29 0a 2a 2a 20 3_shutdown().**
dab0: 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 73 71 6c 69 will invoke sqli
dac0: 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a te3_os_end()..**
dad0: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
dae0: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 72 6f 75 initialize() rou
daf0: 74 69 6e 65 20 72 65 74 75 72 6e 73 20 5b 53 51 tine returns [SQ
db00: 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 LITE_OK] on succ
db10: 65 73 73 2e 0a 2a 2a 20 49 66 20 66 6f 72 20 73 ess..** If for s
db20: 6f 6d 65 20 72 65 61 73 6f 6e 2c 20 73 71 6c 69 ome reason, sqli
db30: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
db40: 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 69 6e is unable to in
db50: 69 74 69 61 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 itialize.** the
db60: 6c 69 62 72 61 72 79 20 28 70 65 72 68 61 70 73 library (perhaps
db70: 20 69 74 20 69 73 20 75 6e 61 62 6c 65 20 74 6f it is unable to
db80: 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 65 64 allocate a need
db90: 65 64 20 72 65 73 6f 75 72 63 65 20 73 75 63 68 ed resource such
dba0: 0a 2a 2a 20 61 73 20 61 20 6d 75 74 65 78 29 20 .** as a mutex)
dbb0: 69 74 20 72 65 74 75 72 6e 73 20 61 6e 20 5b 65 it returns an [e
dbc0: 72 72 6f 72 20 63 6f 64 65 5d 20 6f 74 68 65 72 rror code] other
dbd0: 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b than [SQLITE_OK
dbe0: 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c ]..**.** The sql
dbf0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
dc00: 29 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c ) routine is cal
dc10: 6c 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62 led internally b
dc20: 79 20 6d 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 y many other.**
dc30: 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 SQLite interface
dc40: 73 20 73 6f 20 74 68 61 74 20 61 6e 20 61 70 70 s so that an app
dc50: 6c 69 63 61 74 69 6f 6e 20 75 73 75 61 6c 6c 79 lication usually
dc60: 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
dc70: 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c 69 o.** invoke sqli
dc80: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
dc90: 20 64 69 72 65 63 74 6c 79 2e 20 20 46 6f 72 20 directly. For
dca0: 65 78 61 6d 70 6c 65 2c 20 5b 73 71 6c 69 74 65 example, [sqlite
dcb0: 33 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 63 61 6c 3_open()].** cal
dcc0: 6c 73 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 ls sqlite3_initi
dcd0: 61 6c 69 7a 65 28 29 20 73 6f 20 74 68 65 20 53 alize() so the S
dce0: 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 QLite library wi
dcf0: 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 ll be automatica
dd00: 6c 6c 79 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a lly.** initializ
dd10: 65 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 ed when [sqlite3
dd20: 5f 6f 70 65 6e 28 29 5d 20 69 73 20 63 61 6c 6c _open()] is call
dd30: 65 64 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 ed if it has not
dd40: 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 0a be initialized.
dd50: 2a 2a 20 61 6c 72 65 61 64 79 2e 20 20 48 6f 77 ** already. How
dd60: 65 76 65 72 2c 20 69 66 20 53 51 4c 69 74 65 20 ever, if SQLite
dd70: 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 is compiled with
dd80: 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 the [SQLITE_OMI
dd90: 54 5f 41 55 54 4f 49 4e 49 54 5d 0a 2a 2a 20 63 T_AUTOINIT].** c
dda0: 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 ompile-time opti
ddb0: 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 61 75 74 on, then the aut
ddc0: 6f 6d 61 74 69 63 20 63 61 6c 6c 73 20 74 6f 20 omatic calls to
ddd0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
dde0: 7a 65 28 29 0a 2a 2a 20 61 72 65 20 6f 6d 69 74 ze().** are omit
ddf0: 74 65 64 20 61 6e 64 20 74 68 65 20 61 70 70 6c ted and the appl
de00: 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 63 61 6c ication must cal
de10: 6c 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 l sqlite3_initia
de20: 6c 69 7a 65 28 29 20 64 69 72 65 63 74 6c 79 0a lize() directly.
de30: 2a 2a 20 70 72 69 6f 72 20 74 6f 20 75 73 69 6e ** prior to usin
de40: 67 20 61 6e 79 20 6f 74 68 65 72 20 53 51 4c 69 g any other SQLi
de50: 74 65 20 69 6e 74 65 72 66 61 63 65 2e 20 20 46 te interface. F
de60: 6f 72 20 6d 61 78 69 6d 75 6d 20 70 6f 72 74 61 or maximum porta
de70: 62 69 6c 69 74 79 2c 0a 2a 2a 20 69 74 20 69 73 bility,.** it is
de80: 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 recommended tha
de90: 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 61 t applications a
dea0: 6c 77 61 79 73 20 69 6e 76 6f 6b 65 20 73 71 6c lways invoke sql
deb0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
dec0: 29 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 70 72 ).** directly pr
ded0: 69 6f 72 20 74 6f 20 75 73 69 6e 67 20 61 6e 79 ior to using any
dee0: 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 69 6e other SQLite in
def0: 74 65 72 66 61 63 65 2e 20 20 46 75 74 75 72 65 terface. Future
df00: 20 72 65 6c 65 61 73 65 73 0a 2a 2a 20 6f 66 20 releases.** of
df10: 53 51 4c 69 74 65 20 6d 61 79 20 72 65 71 75 69 SQLite may requi
df20: 72 65 20 74 68 69 73 2e 20 20 49 6e 20 6f 74 68 re this. In oth
df30: 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 62 65 er words, the be
df40: 68 61 76 69 6f 72 20 65 78 68 69 62 69 74 65 64 havior exhibited
df50: 0a 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20 .** when SQLite
df60: 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 is compiled with
df70: 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 [SQLITE_OMIT_AU
df80: 54 4f 49 4e 49 54 5d 20 6d 69 67 68 74 20 62 65 TOINIT] might be
df90: 63 6f 6d 65 20 74 68 65 0a 2a 2a 20 64 65 66 61 come the.** defa
dfa0: 75 6c 74 20 62 65 68 61 76 69 6f 72 20 69 6e 20 ult behavior in
dfb0: 73 6f 6d 65 20 66 75 74 75 72 65 20 72 65 6c 65 some future rele
dfc0: 61 73 65 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a ase of SQLite..*
dfd0: 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
dfe0: 5f 6f 73 5f 69 6e 69 74 28 29 20 72 6f 75 74 69 _os_init() routi
dff0: 6e 65 20 64 6f 65 73 20 6f 70 65 72 61 74 69 6e ne does operatin
e000: 67 2d 73 79 73 74 65 6d 20 73 70 65 63 69 66 69 g-system specifi
e010: 63 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 61 74 c.** initializat
e020: 69 6f 6e 20 6f 66 20 74 68 65 20 53 51 4c 69 74 ion of the SQLit
e030: 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 e library. The
e040: 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 sqlite3_os_end()
e050: 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 75 6e 64 6f .** routine undo
e060: 65 73 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 es the effect of
e070: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 sqlite3_os_init
e080: 28 29 2e 20 20 54 79 70 69 63 61 6c 20 74 61 73 (). Typical tas
e090: 6b 73 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 ks.** performed
e0a0: 62 79 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 by these routine
e0b0: 73 20 69 6e 63 6c 75 64 65 20 61 6c 6c 6f 63 61 s include alloca
e0c0: 74 69 6f 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 tion or dealloca
e0d0: 74 69 6f 6e 0a 2a 2a 20 6f 66 20 73 74 61 74 69 tion.** of stati
e0e0: 63 20 72 65 73 6f 75 72 63 65 73 2c 20 69 6e 69 c resources, ini
e0f0: 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 67 tialization of g
e100: 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2c lobal variables,
e110: 0a 2a 2a 20 73 65 74 74 69 6e 67 20 75 70 20 61 .** setting up a
e120: 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 default [sqlite
e130: 33 5f 76 66 73 5d 20 6d 6f 64 75 6c 65 2c 20 6f 3_vfs] module, o
e140: 72 20 73 65 74 74 69 6e 67 20 75 70 0a 2a 2a 20 r setting up.**
e150: 61 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67 a default config
e160: 75 72 61 74 69 6f 6e 20 75 73 69 6e 67 20 5b 73 uration using [s
e170: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d qlite3_config()]
e180: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c ..**.** The appl
e190: 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e ication should n
e1a0: 65 76 65 72 20 69 6e 76 6f 6b 65 20 65 69 74 68 ever invoke eith
e1b0: 65 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e er sqlite3_os_in
e1c0: 69 74 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 it().** or sqlit
e1d0: 65 33 5f 6f 73 5f 65 6e 64 28 29 20 64 69 72 65 e3_os_end() dire
e1e0: 63 74 6c 79 2e 20 20 54 68 65 20 61 70 70 6c 69 ctly. The appli
e1f0: 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e cation should on
e200: 6c 79 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c ly invoke.** sql
e210: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
e220: 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 68 ) and sqlite3_sh
e230: 75 74 64 6f 77 6e 28 29 2e 20 20 54 68 65 20 73 utdown(). The s
e240: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 qlite3_os_init()
e250: 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 .** interface is
e260: 20 63 61 6c 6c 65 64 20 61 75 74 6f 6d 61 74 69 called automati
e270: 63 61 6c 6c 79 20 62 79 20 73 71 6c 69 74 65 33 cally by sqlite3
e280: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 61 6e _initialize() an
e290: 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f d.** sqlite3_os_
e2a0: 65 6e 64 28 29 20 69 73 20 63 61 6c 6c 65 64 20 end() is called
e2b0: 62 79 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 by sqlite3_shutd
e2c0: 6f 77 6e 28 29 2e 20 20 41 70 70 72 6f 70 72 69 own(). Appropri
e2d0: 61 74 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 ate.** implement
e2e0: 61 74 69 6f 6e 73 20 66 6f 72 20 73 71 6c 69 74 ations for sqlit
e2f0: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 e3_os_init() and
e300: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 sqlite3_os_end(
e310: 29 0a 2a 2a 20 61 72 65 20 62 75 69 6c 74 20 69 ).** are built i
e320: 6e 74 6f 20 53 51 4c 69 74 65 20 77 68 65 6e 20 nto SQLite when
e330: 69 74 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 66 it is compiled f
e340: 6f 72 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 or Unix, Windows
e350: 2c 20 6f 72 20 4f 53 2f 32 2e 0a 2a 2a 20 57 68 , or OS/2..** Wh
e360: 65 6e 20 5b 63 75 73 74 6f 6d 20 62 75 69 6c 64 en [custom build
e370: 73 20 7c 20 62 75 69 6c 74 20 66 6f 72 20 6f 74 s | built for ot
e380: 68 65 72 20 70 6c 61 74 66 6f 72 6d 73 5d 0a 2a her platforms].*
e390: 2a 20 28 75 73 69 6e 67 20 74 68 65 20 5b 53 51 * (using the [SQ
e3a0: 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 3d 31 5d LITE_OS_OTHER=1]
e3b0: 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a compile-time.**
e3c0: 20 6f 70 74 69 6f 6e 29 20 74 68 65 20 61 70 70 option) the app
e3d0: 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 73 75 lication must su
e3e0: 70 70 6c 79 20 61 20 73 75 69 74 61 62 6c 65 20 pply a suitable
e3f0: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 implementation f
e400: 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 or.** sqlite3_os
e410: 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c 69 _init() and sqli
e420: 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 20 20 41 te3_os_end(). A
e430: 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75 n application-su
e440: 70 70 6c 69 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d pplied.** implem
e450: 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 entation of sqli
e460: 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 6f 72 te3_os_init() or
e470: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 sqlite3_os_end(
e480: 29 0a 2a 2a 20 6d 75 73 74 20 72 65 74 75 72 6e ).** must return
e490: 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 [SQLITE_OK] on
e4a0: 73 75 63 63 65 73 73 20 61 6e 64 20 73 6f 6d 65 success and some
e4b0: 20 6f 74 68 65 72 20 5b 65 72 72 6f 72 20 63 6f other [error co
e4c0: 64 65 5d 20 75 70 6f 6e 0a 2a 2a 20 66 61 69 6c de] upon.** fail
e4d0: 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ure..*/.SQLITE_A
e4e0: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 PI int sqlite3_i
e4f0: 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 3b nitialize(void);
e500: 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
e510: 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e sqlite3_shutdown
e520: 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 (void);.SQLITE_A
e530: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
e540: 73 5f 69 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51 s_init(void);.SQ
e550: 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
e560: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 ite3_os_end(void
e570: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
e580: 45 46 3a 20 43 6f 6e 66 69 67 75 72 69 6e 67 20 EF: Configuring
e590: 54 68 65 20 53 51 4c 69 74 65 20 4c 69 62 72 61 The SQLite Libra
e5a0: 72 79 20 7b 48 31 34 31 30 30 7d 20 3c 53 32 30 ry {H14100} <S20
e5b0: 30 30 30 3e 3c 53 33 30 32 30 30 3e 0a 2a 2a 20 000><S30200>.**
e5c0: 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
e5d0: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 ** The sqlite3_c
e5e0: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 onfig() interfac
e5f0: 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b e is used to mak
e600: 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 e global configu
e610: 72 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65 ration.** change
e620: 73 20 74 6f 20 53 51 4c 69 74 65 20 69 6e 20 6f s to SQLite in o
e630: 72 64 65 72 20 74 6f 20 74 75 6e 65 20 53 51 4c rder to tune SQL
e640: 69 74 65 20 74 6f 20 74 68 65 20 73 70 65 63 69 ite to the speci
e650: 66 69 63 20 6e 65 65 64 73 20 6f 66 0a 2a 2a 20 fic needs of.**
e660: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e the application.
e670: 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f The default co
e680: 6e 66 69 67 75 72 61 74 69 6f 6e 20 69 73 20 72 nfiguration is r
e690: 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 6d ecommended for m
e6a0: 6f 73 74 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 ost.** applicati
e6b0: 6f 6e 73 20 61 6e 64 20 73 6f 20 74 68 69 73 20 ons and so this
e6c0: 72 6f 75 74 69 6e 65 20 69 73 20 75 73 75 61 6c routine is usual
e6d0: 6c 79 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 ly not necessary
e6e0: 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f 76 . It is.** prov
e6f0: 69 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 ided to support
e700: 72 61 72 65 20 61 70 70 6c 69 63 61 74 69 6f 6e rare application
e710: 73 20 77 69 74 68 20 75 6e 75 73 75 61 6c 20 6e s with unusual n
e720: 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 eeds..**.** The
e730: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
e740: 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6e 6f interface is no
e750: 74 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 54 t threadsafe. T
e760: 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a 2a he application.*
e770: 2a 20 6d 75 73 74 20 69 6e 73 75 72 65 20 74 68 * must insure th
e780: 61 74 20 6e 6f 20 6f 74 68 65 72 20 53 51 4c 69 at no other SQLi
e790: 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 te interfaces ar
e7a0: 65 20 69 6e 76 6f 6b 65 64 20 62 79 20 6f 74 68 e invoked by oth
e7b0: 65 72 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 68 er.** threads wh
e7c0: 69 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 ile sqlite3_conf
e7d0: 69 67 28 29 20 69 73 20 72 75 6e 6e 69 6e 67 2e ig() is running.
e7e0: 20 20 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 73 Furthermore, s
e7f0: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 0a qlite3_config().
e800: 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 69 ** may only be i
e810: 6e 76 6f 6b 65 64 20 70 72 69 6f 72 20 74 6f 20 nvoked prior to
e820: 6c 69 62 72 61 72 79 20 69 6e 69 74 69 61 6c 69 library initiali
e830: 7a 61 74 69 6f 6e 20 75 73 69 6e 67 0a 2a 2a 20 zation using.**
e840: 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c [sqlite3_initial
e850: 69 7a 65 28 29 5d 20 6f 72 20 61 66 74 65 72 20 ize()] or after
e860: 73 68 75 74 64 6f 77 6e 20 62 79 20 5b 73 71 6c shutdown by [sql
e870: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d ite3_shutdown()]
e880: 2e 0a 2a 2a 20 4e 6f 74 65 2c 20 68 6f 77 65 76 ..** Note, howev
e890: 65 72 2c 20 74 68 61 74 20 73 71 6c 69 74 65 33 er, that sqlite3
e8a0: 5f 63 6f 6e 66 69 67 28 29 20 63 61 6e 20 62 65 _config() can be
e8b0: 20 63 61 6c 6c 65 64 20 61 73 20 70 61 72 74 20 called as part
e8c0: 6f 66 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d of the.** implem
e8d0: 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 61 entation of an a
e8e0: 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
e8f0: 65 64 20 5b 73 71 6c 69 74 65 33 5f 6f 73 5f 69 ed [sqlite3_os_i
e900: 6e 69 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 nit()]..**.** Th
e910: 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
e920: 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 to sqlite3_conf
e930: 69 67 28 29 20 69 73 20 61 6e 20 69 6e 74 65 67 ig() is an integ
e940: 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f er.** [SQLITE_CO
e950: 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 NFIG_SINGLETHREA
e960: 44 20 7c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f D | configuratio
e970: 6e 20 6f 70 74 69 6f 6e 5d 20 74 68 61 74 20 64 n option] that d
e980: 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 77 68 61 etermines.** wha
e990: 74 20 70 72 6f 70 65 72 74 79 20 6f 66 20 53 51 t property of SQ
e9a0: 4c 69 74 65 20 69 73 20 74 6f 20 62 65 20 63 6f Lite is to be co
e9b0: 6e 66 69 67 75 72 65 64 2e 20 20 53 75 62 73 65 nfigured. Subse
e9c0: 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74 73 0a quent arguments.
e9d0: 2a 2a 20 76 61 72 79 20 64 65 70 65 6e 64 69 6e ** vary dependin
e9e0: 67 20 6f 6e 20 74 68 65 20 5b 53 51 4c 49 54 45 g on the [SQLITE
e9f0: 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 _CONFIG_SINGLETH
ea00: 52 45 41 44 20 7c 20 63 6f 6e 66 69 67 75 72 61 READ | configura
ea10: 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d 0a 2a 2a 20 tion option].**
ea20: 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67 in the first arg
ea30: 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 ument..**.** Whe
ea40: 6e 20 61 20 63 6f 6e 66 69 67 75 72 61 74 69 6f n a configuratio
ea50: 6e 20 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 2c n option is set,
ea60: 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 sqlite3_config(
ea70: 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 ) returns [SQLIT
ea80: 45 5f 4f 4b 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 E_OK]..** If the
ea90: 20 6f 70 74 69 6f 6e 20 69 73 20 75 6e 6b 6e 6f option is unkno
eaa0: 77 6e 20 6f 72 20 53 51 4c 69 74 65 20 69 73 20 wn or SQLite is
eab0: 75 6e 61 62 6c 65 20 74 6f 20 73 65 74 20 74 68 unable to set th
eac0: 65 20 6f 70 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e e option.** then
ead0: 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 this routine re
eae0: 74 75 72 6e 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f turns a non-zero
eaf0: 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a [error code]..*
eb00: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
eb10: 73 3a 0a 2a 2a 20 5b 48 31 34 31 30 33 5d 20 5b s:.** [H14103] [
eb20: 48 31 34 31 30 36 5d 20 5b 48 31 34 31 32 30 5d H14106] [H14120]
eb30: 20 5b 48 31 34 31 32 33 5d 20 5b 48 31 34 31 32 [H14123] [H1412
eb40: 36 5d 20 5b 48 31 34 31 32 39 5d 20 5b 48 31 34 6] [H14129] [H14
eb50: 31 33 32 5d 20 5b 48 31 34 31 33 35 5d 0a 2a 2a 132] [H14135].**
eb60: 20 5b 48 31 34 31 33 38 5d 20 5b 48 31 34 31 34 [H14138] [H1414
eb70: 31 5d 20 5b 48 31 34 31 34 34 5d 20 5b 48 31 34 1] [H14144] [H14
eb80: 31 34 37 5d 20 5b 48 31 34 31 35 30 5d 20 5b 48 147] [H14150] [H
eb90: 31 34 31 35 33 5d 20 5b 48 31 34 31 35 36 5d 20 14153] [H14156]
eba0: 5b 48 31 34 31 35 39 5d 0a 2a 2a 20 5b 48 31 34 [H14159].** [H14
ebb0: 31 36 32 5d 20 5b 48 31 34 31 36 35 5d 20 5b 48 162] [H14165] [H
ebc0: 31 34 31 36 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 14168].*/.SQLITE
ebd0: 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 _API SQLITE_EXPE
ebe0: 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c RIMENTAL int sql
ebf0: 69 74 65 33 5f 63 6f 6e 66 69 67 28 69 6e 74 2c ite3_config(int,
ec00: 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ...);../*.** CA
ec10: 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 PI3REF: Configur
ec20: 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
ec30: 63 74 69 6f 6e 73 20 20 7b 48 31 34 32 30 30 7d ctions {H14200}
ec40: 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 <S20000>.** EXP
ec50: 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 ERIMENTAL.**.**
ec60: 54 68 65 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 The sqlite3_db_c
ec70: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 onfig() interfac
ec80: 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b e is used to mak
ec90: 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a e configuration.
eca0: 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20 ** changes to a
ecb0: 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
ecc0: 74 69 6f 6e 5d 2e 20 20 54 68 65 20 69 6e 74 65 tion]. The inte
ecd0: 72 66 61 63 65 20 69 73 20 73 69 6d 69 6c 61 72 rface is similar
ece0: 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f to.** [sqlite3_
ecf0: 63 6f 6e 66 69 67 28 29 5d 20 65 78 63 65 70 74 config()] except
ed00: 20 74 68 61 74 20 74 68 65 20 63 68 61 6e 67 65 that the change
ed10: 73 20 61 70 70 6c 79 20 74 6f 20 61 20 73 69 6e s apply to a sin
ed20: 67 6c 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 gle.** [database
ed30: 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 73 70 connection] (sp
ed40: 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 66 ecified in the f
ed50: 69 72 73 74 20 61 72 67 75 6d 65 6e 74 29 2e 20 irst argument).
ed60: 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f The.** sqlite3_
ed70: 64 62 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 db_config() inte
ed80: 72 66 61 63 65 20 63 61 6e 20 6f 6e 6c 79 20 62 rface can only b
ed90: 65 20 75 73 65 64 20 69 6d 6d 65 64 69 61 74 65 e used immediate
eda0: 6c 79 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 ly after.** the
edb0: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
edc0: 69 6f 6e 20 69 73 20 63 72 65 61 74 65 64 20 75 ion is created u
edd0: 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6f 70 sing [sqlite3_op
ede0: 65 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 en()],.** [sqlit
edf0: 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 6f 72 e3_open16()], or
ee00: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 [sqlite3_open_v
ee10: 32 28 29 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 2()]. .**.** Th
ee20: 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
ee30: 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 64 62 5f t to sqlite3_db_
ee40: 63 6f 6e 66 69 67 28 44 2c 56 2c 2e 2e 2e 29 20 config(D,V,...)
ee50: 20 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6e 66 69 is the.** confi
ee60: 67 75 72 61 74 69 6f 6e 20 76 65 72 62 20 2d 20 guration verb -
ee70: 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 an integer code
ee80: 74 68 61 74 20 69 6e 64 69 63 61 74 65 73 20 77 that indicates w
ee90: 68 61 74 0a 2a 2a 20 61 73 70 65 63 74 20 6f 66 hat.** aspect of
eea0: 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
eeb0: 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 62 65 onnection] is be
eec0: 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 2e 0a ing configured..
eed0: 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 63 68 6f 69 ** The only choi
eee0: 63 65 20 66 6f 72 20 74 68 69 73 20 76 61 6c 75 ce for this valu
eef0: 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 43 e is [SQLITE_DBC
ef00: 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d ONFIG_LOOKASIDE]
ef10: 2e 0a 2a 2a 20 4e 65 77 20 76 65 72 62 73 20 61 ..** New verbs a
ef20: 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 re likely to be
ef30: 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 added in future
ef40: 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 releases of SQLi
ef50: 74 65 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 te..** Additiona
ef60: 6c 20 61 72 67 75 6d 65 6e 74 73 20 64 65 70 65 l arguments depe
ef70: 6e 64 20 6f 6e 20 74 68 65 20 76 65 72 62 2e 0a nd on the verb..
ef80: 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
ef90: 74 73 3a 0a 2a 2a 20 5b 48 31 34 32 30 33 5d 20 ts:.** [H14203]
efa0: 5b 48 31 34 32 30 36 5d 20 5b 48 31 34 32 30 39 [H14206] [H14209
efb0: 5d 20 5b 48 31 34 32 31 32 5d 20 5b 48 31 34 32 ] [H14212] [H142
efc0: 31 35 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 15].*/.SQLITE_AP
efd0: 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d I SQLITE_EXPERIM
efe0: 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 ENTAL int sqlite
eff0: 33 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 3_db_config(sqli
f000: 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e te3*, int op, ..
f010: 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 .);../*.** CAPI3
f020: 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f REF: Memory Allo
f030: 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 20 cation Routines
f040: 7b 48 31 30 31 35 35 7d 20 3c 53 32 30 31 32 30 {H10155} <S20120
f050: 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 >.** EXPERIMENTA
f060: 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 L.**.** An insta
f070: 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 nce of this obje
f080: 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 ct defines the i
f090: 6e 74 65 72 66 61 63 65 20 62 65 74 77 65 65 6e nterface between
f0a0: 20 53 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 6c SQLite.** and l
f0b0: 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 ow-level memory
f0c0: 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 allocation routi
f0d0: 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 nes..**.** This
f0e0: 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64 20 69 object is used i
f0f0: 6e 20 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61 63 65 n only one place
f100: 20 69 6e 20 74 68 65 20 53 51 4c 69 74 65 20 69 in the SQLite i
f110: 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 41 20 70 nterface..** A p
f120: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 ointer to an ins
f130: 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 tance of this ob
f140: 6a 65 63 74 20 69 73 20 74 68 65 20 61 72 67 75 ject is the argu
f150: 6d 65 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 ment to.** [sqli
f160: 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 68 te3_config()] wh
f170: 65 6e 20 74 68 65 20 63 6f 6e 66 69 67 75 72 61 en the configura
f180: 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 0a 2a tion option is.*
f190: 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 * [SQLITE_CONFIG
f1a0: 5f 4d 41 4c 4c 4f 43 5d 20 6f 72 20 5b 53 51 4c _MALLOC] or [SQL
f1b0: 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 ITE_CONFIG_GETMA
f1c0: 4c 4c 4f 43 5d 2e 20 20 0a 2a 2a 20 42 79 20 63 LLOC]. .** By c
f1d0: 72 65 61 74 69 6e 67 20 61 6e 20 69 6e 73 74 61 reating an insta
f1e0: 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 nce of this obje
f1f0: 63 74 0a 2a 2a 20 61 6e 64 20 70 61 73 73 69 6e ct.** and passin
f200: 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 g it to [sqlite3
f210: 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 _config]([SQLITE
f220: 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 29 _CONFIG_MALLOC])
f230: 0a 2a 2a 20 64 75 72 69 6e 67 20 63 6f 6e 66 69 .** during confi
f240: 67 75 72 61 74 69 6f 6e 2c 20 61 6e 20 61 70 70 guration, an app
f250: 6c 69 63 61 74 69 6f 6e 20 63 61 6e 20 73 70 65 lication can spe
f260: 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 cify an alternat
f270: 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c ive.** memory al
f280: 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
f290: 65 6d 20 66 6f 72 20 53 51 4c 69 74 65 20 74 6f em for SQLite to
f2a0: 20 75 73 65 20 66 6f 72 20 61 6c 6c 20 6f 66 20 use for all of
f2b0: 69 74 73 0a 2a 2a 20 64 79 6e 61 6d 69 63 20 6d its.** dynamic m
f2c0: 65 6d 6f 72 79 20 6e 65 65 64 73 2e 0a 2a 2a 0a emory needs..**.
f2d0: 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 53 51 4c ** Note that SQL
f2e0: 69 74 65 20 63 6f 6d 65 73 20 77 69 74 68 20 73 ite comes with s
f2f0: 65 76 65 72 61 6c 20 5b 62 75 69 6c 74 2d 69 6e everal [built-in
f300: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
f310: 72 73 5d 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 rs].** that are
f320: 70 65 72 66 65 63 74 6c 79 20 61 64 65 71 75 61 perfectly adequa
f330: 74 65 20 66 6f 72 20 74 68 65 20 6f 76 65 72 77 te for the overw
f340: 68 65 6c 6d 69 6e 67 20 6d 61 6a 6f 72 69 74 79 helming majority
f350: 20 6f 66 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 of applications
f360: 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 68 69 .** and that thi
f370: 73 20 6f 62 6a 65 63 74 20 69 73 20 6f 6e 6c 79 s object is only
f380: 20 75 73 65 66 75 6c 20 74 6f 20 61 20 74 69 6e useful to a tin
f390: 79 20 6d 69 6e 6f 72 69 74 79 20 6f 66 20 61 70 y minority of ap
f3a0: 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 77 69 plications.** wi
f3b0: 74 68 20 73 70 65 63 69 61 6c 69 7a 65 64 20 6d th specialized m
f3c0: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
f3d0: 20 72 65 71 75 69 72 65 6d 65 6e 74 73 2e 20 20 requirements.
f3e0: 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73 0a 2a This object is.*
f3f0: 2a 20 61 6c 73 6f 20 75 73 65 64 20 64 75 72 69 * also used duri
f400: 6e 67 20 74 65 73 74 69 6e 67 20 6f 66 20 53 51 ng testing of SQ
f410: 4c 69 74 65 20 69 6e 20 6f 72 64 65 72 20 74 6f Lite in order to
f420: 20 73 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 specify an alte
f430: 72 6e 61 74 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 rnative.** memor
f440: 79 20 61 6c 6c 6f 63 61 74 6f 72 20 74 68 61 74 y allocator that
f450: 20 73 69 6d 75 6c 61 74 65 73 20 6d 65 6d 6f 72 simulates memor
f460: 79 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 y out-of-memory
f470: 63 6f 6e 64 69 74 69 6f 6e 73 20 69 6e 0a 2a 2a conditions in.**
f480: 20 6f 72 64 65 72 20 74 6f 20 76 65 72 69 66 79 order to verify
f490: 20 74 68 61 74 20 53 51 4c 69 74 65 20 72 65 63 that SQLite rec
f4a0: 6f 76 65 72 73 20 67 72 61 63 65 66 75 6c 6c 79 overs gracefully
f4b0: 20 66 72 6f 6d 20 73 75 63 68 0a 2a 2a 20 63 6f from such.** co
f4c0: 6e 64 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 nditions..**.**
f4d0: 54 68 65 20 78 4d 61 6c 6c 6f 63 20 61 6e 64 20 The xMalloc and
f4e0: 78 46 72 65 65 20 6d 65 74 68 6f 64 73 20 6d 75 xFree methods mu
f4f0: 73 74 20 77 6f 72 6b 20 6c 69 6b 65 20 74 68 65 st work like the
f500: 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 .** malloc() and
f510: 20 66 72 65 65 28 29 20 66 75 6e 63 74 69 6f 6e free() function
f520: 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 s from the stand
f530: 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a ard C library..*
f540: 2a 20 54 68 65 20 78 52 65 61 6c 6c 6f 63 20 6d * The xRealloc m
f550: 65 74 68 6f 64 20 6d 75 73 74 20 77 6f 72 6b 20 ethod must work
f560: 6c 69 6b 65 20 72 65 61 6c 6c 6f 63 28 29 20 66 like realloc() f
f570: 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64 rom the standard
f580: 20 43 20 6c 69 62 72 61 72 79 0a 2a 2a 20 77 69 C library.** wi
f590: 74 68 20 74 68 65 20 65 78 63 65 70 74 69 6f 6e th the exception
f5a0: 20 74 68 61 74 20 69 66 20 74 68 65 20 73 65 63 that if the sec
f5b0: 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 ond argument to
f5c0: 78 52 65 61 6c 6c 6f 63 20 69 73 20 7a 65 72 6f xRealloc is zero
f5d0: 2c 0a 2a 2a 20 78 52 65 61 6c 6c 6f 63 20 6d 75 ,.** xRealloc mu
f5e0: 73 74 20 62 65 20 61 20 6e 6f 2d 6f 70 20 2d 20 st be a no-op -
f5f0: 69 74 20 6d 75 73 74 20 6e 6f 74 20 70 65 72 66 it must not perf
f600: 6f 72 6d 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69 orm any allocati
f610: 6f 6e 20 6f 72 0a 2a 2a 20 64 65 61 6c 6c 6f 63 on or.** dealloc
f620: 61 74 69 6f 6e 2e 20 20 53 51 4c 69 74 65 20 67 ation. SQLite g
f630: 75 61 72 61 6e 74 65 65 64 73 20 74 68 61 74 20 uaranteeds that
f640: 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
f650: 65 6e 74 20 74 6f 0a 2a 2a 20 78 52 65 61 6c 6c ent to.** xReall
f660: 6f 63 20 69 73 20 61 6c 77 61 79 73 20 61 20 76 oc is always a v
f670: 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
f680: 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f a prior call to
f690: 20 78 52 6f 75 6e 64 75 70 2e 0a 2a 2a 20 41 6e xRoundup..** An
f6a0: 64 20 73 6f 20 69 6e 20 63 61 73 65 73 20 77 68 d so in cases wh
f6b0: 65 72 65 20 78 52 6f 75 6e 64 75 70 20 61 6c 77 ere xRoundup alw
f6c0: 61 79 73 20 72 65 74 75 72 6e 73 20 61 20 70 6f ays returns a po
f6d0: 73 69 74 69 76 65 20 6e 75 6d 62 65 72 2c 0a 2a sitive number,.*
f6e0: 2a 20 78 52 65 61 6c 6c 6f 63 20 63 61 6e 20 70 * xRealloc can p
f6f0: 65 72 66 6f 72 6d 20 65 78 61 63 74 6c 79 20 61 erform exactly a
f700: 73 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 6c s the standard l
f710: 69 62 72 61 72 79 20 72 65 61 6c 6c 6f 63 28 29 ibrary realloc()
f720: 20 61 6e 64 0a 2a 2a 20 73 74 69 6c 6c 20 62 65 and.** still be
f730: 20 69 6e 20 63 6f 6d 70 6c 69 61 6e 63 65 20 77 in compliance w
f740: 69 74 68 20 74 68 69 73 20 73 70 65 63 69 66 69 ith this specifi
f750: 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 78 53 cation..**.** xS
f760: 69 7a 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72 ize should retur
f770: 6e 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 n the allocated
f780: 73 69 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79 size of a memory
f790: 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 70 allocation.** p
f7a0: 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e reviously obtain
f7b0: 65 64 20 66 72 6f 6d 20 78 4d 61 6c 6c 6f 63 20 ed from xMalloc
f7c0: 6f 72 20 78 52 65 61 6c 6c 6f 63 2e 20 20 54 68 or xRealloc. Th
f7d0: 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 e allocated size
f7e0: 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 74 .** is always at
f7f0: 20 6c 65 61 73 74 20 61 73 20 62 69 67 20 61 73 least as big as
f800: 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 73 the requested s
f810: 69 7a 65 20 62 75 74 20 6d 61 79 20 62 65 20 6c ize but may be l
f820: 61 72 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 arger..**.** The
f830: 20 78 52 6f 75 6e 64 75 70 20 6d 65 74 68 6f 64 xRoundup method
f840: 20 72 65 74 75 72 6e 73 20 77 68 61 74 20 77 6f returns what wo
f850: 75 6c 64 20 62 65 20 74 68 65 20 61 6c 6c 6f 63 uld be the alloc
f860: 61 74 65 64 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 ated size of.**
f870: 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
f880: 69 6f 6e 20 67 69 76 65 6e 20 61 20 70 61 72 74 ion given a part
f890: 69 63 75 6c 61 72 20 72 65 71 75 65 73 74 65 64 icular requested
f8a0: 20 73 69 7a 65 2e 20 20 4d 6f 73 74 20 6d 65 6d size. Most mem
f8b0: 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 6f 72 ory.** allocator
f8c0: 73 20 72 6f 75 6e 64 20 75 70 20 6d 65 6d 6f 72 s round up memor
f8d0: 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 74 y allocations at
f8e0: 20 6c 65 61 73 74 20 74 6f 20 74 68 65 20 6e 65 least to the ne
f8f0: 78 74 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 6f xt multiple.** o
f900: 66 20 38 2e 20 20 53 6f 6d 65 20 61 6c 6c 6f 63 f 8. Some alloc
f910: 61 74 6f 72 73 20 72 6f 75 6e 64 20 75 70 20 74 ators round up t
f920: 6f 20 61 20 6c 61 72 67 65 72 20 6d 75 6c 74 69 o a larger multi
f930: 70 6c 65 20 6f 72 20 74 6f 20 61 20 70 6f 77 65 ple or to a powe
f940: 72 20 6f 66 20 32 2e 0a 2a 2a 20 45 76 65 72 79 r of 2..** Every
f950: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
f960: 6f 6e 20 72 65 71 75 65 73 74 20 63 6f 6d 69 6e on request comin
f970: 67 20 69 6e 20 74 68 72 6f 75 67 68 20 5b 73 71 g in through [sq
f980: 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 0a lite3_malloc()].
f990: 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 ** or [sqlite3_r
f9a0: 65 61 6c 6c 6f 63 28 29 5d 20 66 69 72 73 74 20 ealloc()] first
f9b0: 63 61 6c 6c 73 20 78 52 6f 75 6e 64 75 70 2e 20 calls xRoundup.
f9c0: 20 49 66 20 78 52 6f 75 6e 64 75 70 20 72 65 74 If xRoundup ret
f9d0: 75 72 6e 73 20 30 2c 20 0a 2a 2a 20 74 68 61 74 urns 0, .** that
f9e0: 20 63 61 75 73 65 73 20 74 68 65 20 63 6f 72 72 causes the corr
f9f0: 65 73 70 6f 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 esponding memory
fa00: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 66 allocation to f
fa10: 61 69 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 ail..**.** The x
fa20: 49 6e 69 74 20 6d 65 74 68 6f 64 20 69 6e 69 74 Init method init
fa30: 69 61 6c 69 7a 65 73 20 74 68 65 20 6d 65 6d 6f ializes the memo
fa40: 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 20 20 28 ry allocator. (
fa50: 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 For example,.**
fa60: 69 74 20 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74 it might allocat
fa70: 65 20 61 6e 79 20 72 65 71 75 69 72 65 20 6d 75 e any require mu
fa80: 74 65 78 65 73 20 6f 72 20 69 6e 69 74 69 61 6c texes or initial
fa90: 69 7a 65 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 ize internal dat
faa0: 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 2e a.** structures.
fab0: 20 20 54 68 65 20 78 53 68 75 74 64 6f 77 6e 20 The xShutdown
fac0: 6d 65 74 68 6f 64 20 69 73 20 69 6e 76 6f 6b 65 method is invoke
fad0: 64 20 28 69 6e 64 69 72 65 63 74 6c 79 29 20 62 d (indirectly) b
fae0: 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 68 y.** [sqlite3_sh
faf0: 75 74 64 6f 77 6e 28 29 5d 20 61 6e 64 20 73 68 utdown()] and sh
fb00: 6f 75 6c 64 20 64 65 61 6c 6c 6f 63 61 74 65 20 ould deallocate
fb10: 61 6e 79 20 72 65 73 6f 75 72 63 65 73 20 61 63 any resources ac
fb20: 71 75 69 72 65 64 0a 2a 2a 20 62 79 20 78 49 6e quired.** by xIn
fb30: 69 74 2e 20 20 54 68 65 20 70 41 70 70 44 61 74 it. The pAppDat
fb40: 61 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 a pointer is use
fb50: 64 20 61 73 20 74 68 65 20 6f 6e 6c 79 20 70 61 d as the only pa
fb60: 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 78 49 rameter to.** xI
fb70: 6e 69 74 20 61 6e 64 20 78 53 68 75 74 64 6f 77 nit and xShutdow
fb80: 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 n..**.** SQLite
fb90: 68 6f 6c 64 73 20 74 68 65 20 5b 53 51 4c 49 54 holds the [SQLIT
fba0: 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
fbb0: 41 53 54 45 52 5d 20 6d 75 74 65 78 20 77 68 65 ASTER] mutex whe
fbc0: 6e 20 69 74 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 n it invokes.**
fbd0: 74 68 65 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 the xInit method
fbe0: 2c 20 73 6f 20 74 68 65 20 78 49 6e 69 74 20 6d , so the xInit m
fbf0: 65 74 68 6f 64 20 6e 65 65 64 20 6e 6f 74 20 62 ethod need not b
fc00: 65 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 54 e threadsafe. T
fc10: 68 65 0a 2a 2a 20 78 53 68 75 74 64 6f 77 6e 20 he.** xShutdown
fc20: 6d 65 74 68 6f 64 20 69 73 20 6f 6e 6c 79 20 63 method is only c
fc30: 61 6c 6c 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 alled from [sqli
fc40: 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 20 te3_shutdown()]
fc50: 73 6f 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f so it does.** no
fc60: 74 20 6e 65 65 64 20 74 6f 20 62 65 20 74 68 72 t need to be thr
fc70: 65 61 64 73 61 66 65 20 65 69 74 68 65 72 2e 20 eadsafe either.
fc80: 20 46 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 6d For all other m
fc90: 65 74 68 6f 64 73 2c 20 53 51 4c 69 74 65 0a 2a ethods, SQLite.*
fca0: 2a 20 68 6f 6c 64 73 20 74 68 65 20 5b 53 51 4c * holds the [SQL
fcb0: 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
fcc0: 5f 4d 45 4d 5d 20 6d 75 74 65 78 20 61 73 20 6c _MEM] mutex as l
fcd0: 6f 6e 67 20 61 73 20 74 68 65 0a 2a 2a 20 5b 53 ong as the.** [S
fce0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d QLITE_CONFIG_MEM
fcf0: 53 54 41 54 55 53 5d 20 63 6f 6e 66 69 67 75 72 STATUS] configur
fd00: 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 20 ation option is
fd10: 74 75 72 6e 65 64 20 6f 6e 20 28 77 68 69 63 68 turned on (which
fd20: 0a 2a 2a 20 69 74 20 69 73 20 62 79 20 64 65 66 .** it is by def
fd30: 61 75 6c 74 29 20 61 6e 64 20 73 6f 20 74 68 65 ault) and so the
fd40: 20 6d 65 74 68 6f 64 73 20 61 72 65 20 61 75 74 methods are aut
fd50: 6f 6d 61 74 69 63 61 6c 6c 79 20 73 65 72 69 61 omatically seria
fd60: 6c 69 7a 65 64 2e 0a 2a 2a 20 48 6f 77 65 76 65 lized..** Howeve
fd70: 72 2c 20 69 66 20 5b 53 51 4c 49 54 45 5f 43 4f r, if [SQLITE_CO
fd80: 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 5d 20 NFIG_MEMSTATUS]
fd90: 69 73 20 64 69 73 61 62 6c 65 64 2c 20 74 68 65 is disabled, the
fda0: 6e 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 6d n the other.** m
fdb0: 65 74 68 6f 64 73 20 6d 75 73 74 20 62 65 20 74 ethods must be t
fdc0: 68 72 65 61 64 73 61 66 65 20 6f 72 20 65 6c 73 hreadsafe or els
fdd0: 65 20 6d 61 6b 65 20 74 68 65 69 72 20 6f 77 6e e make their own
fde0: 20 61 72 72 61 6e 67 65 6d 65 6e 74 73 20 66 6f arrangements fo
fdf0: 72 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 61 74 69 r.** serializati
fe00: 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 on..**.** SQLite
fe10: 20 77 69 6c 6c 20 6e 65 76 65 72 20 69 6e 76 6f will never invo
fe20: 6b 65 20 78 49 6e 69 74 28 29 20 6d 6f 72 65 20 ke xInit() more
fe30: 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68 6f 75 than once withou
fe40: 74 20 61 6e 20 69 6e 74 65 72 76 65 6e 69 6e 67 t an intervening
fe50: 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 53 68 75 .** call to xShu
fe60: 74 64 6f 77 6e 28 29 2e 0a 2a 2f 0a 74 79 70 65 tdown()..*/.type
fe70: 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 def struct sqlit
fe80: 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 73 e3_mem_methods s
fe90: 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
fea0: 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 ds;.struct sqlit
feb0: 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 7b e3_mem_methods {
fec0: 0a 20 20 76 6f 69 64 20 2a 28 2a 78 4d 61 6c 6c . void *(*xMall
fed0: 6f 63 29 28 69 6e 74 29 3b 20 20 20 20 20 20 20 oc)(int);
fee0: 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f /* Memory allo
fef0: 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 cation function
ff00: 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 65 */. void (*xFre
ff10: 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 e)(void*);
ff20: 20 20 20 20 2f 2a 20 46 72 65 65 20 61 20 70 72 /* Free a pr
ff30: 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a ior allocation *
ff40: 2f 0a 20 20 76 6f 69 64 20 2a 28 2a 78 52 65 61 /. void *(*xRea
ff50: 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c 69 6e 74 29 lloc)(void*,int)
ff60: 3b 20 20 2f 2a 20 52 65 73 69 7a 65 20 61 6e 20 ; /* Resize an
ff70: 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 allocation */.
ff80: 69 6e 74 20 28 2a 78 53 69 7a 65 29 28 76 6f 69 int (*xSize)(voi
ff90: 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 2f d*); /
ffa0: 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a * Return the siz
ffb0: 65 20 6f 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69 e of an allocati
ffc0: 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 52 on */. int (*xR
ffd0: 6f 75 6e 64 75 70 29 28 69 6e 74 29 3b 20 20 20 oundup)(int);
ffe0: 20 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64 20 /* Round
fff0: 75 70 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 up request size
10000 74 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 to allocation si
10010 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 49 ze */. int (*xI
10020 6e 69 74 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 nit)(void*);
10030 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 /* Initia
10040 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 lize the memory
10050 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 76 allocator */. v
10060 6f 69 64 20 28 2a 78 53 68 75 74 64 6f 77 6e 29 oid (*xShutdown)
10070 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 2f 2a (void*); /*
10080 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 Deinitialize th
10090 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
100a0 6f 72 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 or */. void *pA
100b0 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 ppData;
100c0 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 /* Argume
100d0 6e 74 20 74 6f 20 78 49 6e 69 74 28 29 20 61 6e nt to xInit() an
100e0 64 20 78 53 68 75 74 64 6f 77 6e 28 29 20 2a 2f d xShutdown() */
100f0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 .};../*.** CAPI3
10100 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 61 74 69 REF: Configurati
10110 6f 6e 20 4f 70 74 69 6f 6e 73 20 7b 48 31 30 31 on Options {H101
10120 36 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 60} <S20000>.**
10130 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
10140 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e ** These constan
10150 74 73 20 61 72 65 20 74 68 65 20 61 76 61 69 6c ts are the avail
10160 61 62 6c 65 20 69 6e 74 65 67 65 72 20 63 6f 6e able integer con
10170 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f figuration optio
10180 6e 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 ns that.** can b
10190 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 e passed as the
101a0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 first argument t
101b0 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 o the [sqlite3_c
101c0 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61 onfig()] interfa
101d0 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f ce..**.** New co
101e0 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 nfiguration opti
101f0 6f 6e 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 ons may be added
10200 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 in future relea
10210 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a ses of SQLite..*
10220 2a 20 45 78 69 73 74 69 6e 67 20 63 6f 6e 66 69 * Existing confi
10230 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 guration options
10240 20 6d 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e might be discon
10250 74 69 6e 75 65 64 2e 20 20 41 70 70 6c 69 63 61 tinued. Applica
10260 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 tions.** should
10270 63 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e check the return
10280 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69 code from [sqli
10290 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 74 6f te3_config()] to
102a0 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a make sure that.
102b0 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f 72 6b ** the call work
102c0 65 64 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 ed. The [sqlite
102d0 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 3_config()] inte
102e0 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74 75 72 rface will retur
102f0 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 n a.** non-zero
10300 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 66 20 [error code] if
10310 61 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f a discontinued o
10320 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 63 6f r unsupported co
10330 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 nfiguration opti
10340 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 on.** is invoked
10350 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 ..**.** <dl>.**
10360 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 <dt>SQLITE_CONFI
10370 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 3c 2f G_SINGLETHREAD</
10380 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 65 dt>.** <dd>There
10390 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65 6e 74 are no argument
103a0 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e s to this option
103b0 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 64 . This option d
103c0 69 73 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c 20 6d isables.** all m
103d0 75 74 65 78 69 6e 67 20 61 6e 64 20 70 75 74 73 utexing and puts
103e0 20 53 51 4c 69 74 65 20 69 6e 74 6f 20 61 20 6d SQLite into a m
103f0 6f 64 65 20 77 68 65 72 65 20 69 74 20 63 61 6e ode where it can
10400 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 0a 2a 2a only be used.**
10410 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74 68 72 by a single thr
10420 65 61 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ead.</dd>.**.**
10430 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 <dt>SQLITE_CONFI
10440 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 3c 2f 64 G_MULTITHREAD</d
10450 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 65 20 t>.** <dd>There
10460 61 72 65 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73 are no arguments
10470 20 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 2e to this option.
10480 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 64 69 This option di
10490 73 61 62 6c 65 73 0a 2a 2a 20 6d 75 74 65 78 69 sables.** mutexi
104a0 6e 67 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 ng on [database
104b0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 20 connection] and
104c0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
104d0 65 6e 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a ent] objects..**
104e0 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e The application
104f0 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 is responsible
10500 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e 67 20 for serializing
10510 61 63 63 65 73 73 20 74 6f 0a 2a 2a 20 5b 64 61 access to.** [da
10520 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
10530 6e 73 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 ns] and [prepare
10540 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 2e 20 20 d statements].
10550 42 75 74 20 6f 74 68 65 72 20 6d 75 74 65 78 65 But other mutexe
10560 73 0a 2a 2a 20 61 72 65 20 65 6e 61 62 6c 65 64 s.** are enabled
10570 20 73 6f 20 74 68 61 74 20 53 51 4c 69 74 65 20 so that SQLite
10580 77 69 6c 6c 20 62 65 20 73 61 66 65 20 74 6f 20 will be safe to
10590 75 73 65 20 69 6e 20 61 20 6d 75 6c 74 69 2d 74 use in a multi-t
105a0 68 72 65 61 64 65 64 0a 2a 2a 20 65 6e 76 69 72 hreaded.** envir
105b0 6f 6e 6d 65 6e 74 20 61 73 20 6c 6f 6e 67 20 61 onment as long a
105c0 73 20 6e 6f 20 74 77 6f 20 74 68 72 65 61 64 73 s no two threads
105d0 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 attempt to use
105e0 74 68 65 20 73 61 6d 65 0a 2a 2a 20 5b 64 61 74 the same.** [dat
105f0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
10600 5d 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 ] at the same ti
10610 6d 65 2e 20 20 53 65 65 20 74 68 65 20 5b 74 68 me. See the [th
10620 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 0a 2a 2a reading mode].**
10630 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 documentation f
10640 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e or additional in
10650 66 6f 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e 0a formation.</dd>.
10660 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
10670 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a _CONFIG_SERIALIZ
10680 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 ED</dt>.** <dd>T
10690 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75 here are no argu
106a0 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 ments to this op
106b0 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 tion. This opti
106c0 6f 6e 20 65 6e 61 62 6c 65 73 0a 2a 2a 20 61 6c on enables.** al
106d0 6c 20 6d 75 74 65 78 65 73 20 69 6e 63 6c 75 64 l mutexes includ
106e0 69 6e 67 20 74 68 65 20 72 65 63 75 72 73 69 76 ing the recursiv
106f0 65 0a 2a 2a 20 6d 75 74 65 78 65 73 20 6f 6e 20 e.** mutexes on
10700 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
10710 74 69 6f 6e 5d 20 61 6e 64 20 5b 70 72 65 70 61 tion] and [prepa
10720 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f red statement] o
10730 62 6a 65 63 74 73 2e 0a 2a 2a 20 49 6e 20 74 68 bjects..** In th
10740 69 73 20 6d 6f 64 65 20 28 77 68 69 63 68 20 69 is mode (which i
10750 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 77 68 s the default wh
10760 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d en SQLite is com
10770 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 5b 53 piled with.** [S
10780 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
10790 3d 31 5d 29 20 74 68 65 20 53 51 4c 69 74 65 20 =1]) the SQLite
107a0 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 69 74 73 library will its
107b0 65 6c 66 20 73 65 72 69 61 6c 69 7a 65 20 61 63 elf serialize ac
107c0 63 65 73 73 0a 2a 2a 20 74 6f 20 5b 64 61 74 61 cess.** to [data
107d0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
107e0 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 ] and [prepared
107f0 73 74 61 74 65 6d 65 6e 74 73 5d 20 73 6f 20 74 statements] so t
10800 68 61 74 20 74 68 65 0a 2a 2a 20 61 70 70 6c 69 hat the.** appli
10810 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 20 74 cation is free t
10820 6f 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 5b o use the same [
10830 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
10840 69 6f 6e 5d 20 6f 72 20 74 68 65 0a 2a 2a 20 73 ion] or the.** s
10850 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 ame [prepared st
10860 61 74 65 6d 65 6e 74 5d 20 69 6e 20 64 69 66 66 atement] in diff
10870 65 72 65 6e 74 20 74 68 72 65 61 64 73 20 61 74 erent threads at
10880 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a the same time..
10890 2a 2a 20 53 65 65 20 74 68 65 20 5b 74 68 72 65 ** See the [thre
108a0 61 64 69 6e 67 20 6d 6f 64 65 5d 20 64 6f 63 75 ading mode] docu
108b0 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 mentation for ad
108c0 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
108d0 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a tion.</dd>.**.**
108e0 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 <dt>SQLITE_CONF
108f0 49 47 5f 4d 41 4c 4c 4f 43 3c 2f 64 74 3e 0a 2a IG_MALLOC</dt>.*
10900 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f * <dd>This optio
10910 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 n takes a single
10920 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 argument which
10930 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
10940 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f an.** instance o
10950 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d f the [sqlite3_m
10960 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 em_methods] stru
10970 63 74 75 72 65 2e 20 20 54 68 65 20 61 72 67 75 cture. The argu
10980 6d 65 6e 74 20 73 70 65 63 69 66 69 65 73 0a 2a ment specifies.*
10990 2a 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6c 6f * alternative lo
109a0 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 w-level memory a
109b0 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e llocation routin
109c0 65 73 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e es to be used in
109d0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 74 68 65 place of.** the
109e0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
109f0 6f 6e 20 72 6f 75 74 69 6e 65 73 20 62 75 69 6c on routines buil
10a00 74 20 69 6e 74 6f 20 53 51 4c 69 74 65 2e 3c 2f t into SQLite.</
10a10 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
10a20 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d LITE_CONFIG_GETM
10a30 41 4c 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 ALLOC</dt>.** <d
10a40 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 d>This option ta
10a50 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 kes a single arg
10a60 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 ument which is a
10a70 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a pointer to an.*
10a80 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 * instance of th
10a90 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d e [sqlite3_mem_m
10aa0 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74 75 72 ethods] structur
10ab0 65 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33 e. The [sqlite3
10ac0 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a _mem_methods].**
10ad0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 66 69 structure is fi
10ae0 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 75 lled with the cu
10af0 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 rrently defined
10b00 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
10b10 6e 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 54 n routines..** T
10b20 68 69 73 20 6f 70 74 69 6f 6e 20 63 61 6e 20 62 his option can b
10b30 65 20 75 73 65 64 20 74 6f 20 6f 76 65 72 6c 6f e used to overlo
10b40 61 64 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d ad the default m
10b50 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
10b60 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77 69 74 .** routines wit
10b70 68 20 61 20 77 72 61 70 70 65 72 20 74 68 61 74 h a wrapper that
10b80 20 73 69 6d 75 6c 61 74 69 6f 6e 73 20 6d 65 6d simulations mem
10b90 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 ory allocation f
10ba0 61 69 6c 75 72 65 20 6f 72 0a 2a 2a 20 74 72 61 ailure or.** tra
10bb0 63 6b 73 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 cks memory usage
10bc0 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 3c 2f , for example.</
10bd0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
10be0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 LITE_CONFIG_MEMS
10bf0 54 41 54 55 53 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 TATUS</dt>.** <d
10c00 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 d>This option ta
10c10 6b 65 73 20 73 69 6e 67 6c 65 20 61 72 67 75 6d kes single argum
10c20 65 6e 74 20 6f 66 20 74 79 70 65 20 69 6e 74 2c ent of type int,
10c30 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 interpreted as
10c40 61 20 0a 2a 2a 20 62 6f 6f 6c 65 61 6e 2c 20 77 a .** boolean, w
10c50 68 69 63 68 20 65 6e 61 62 6c 65 73 20 6f 72 20 hich enables or
10c60 64 69 73 61 62 6c 65 73 20 74 68 65 20 63 6f 6c disables the col
10c70 6c 65 63 74 69 6f 6e 20 6f 66 20 6d 65 6d 6f 72 lection of memor
10c80 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 0a 2a 2a y allocation .**
10c90 20 73 74 61 74 69 73 74 69 63 73 2e 20 57 68 65 statistics. Whe
10ca0 6e 20 64 69 73 61 62 6c 65 64 2c 20 74 68 65 20 n disabled, the
10cb0 66 6f 6c 6c 6f 77 69 6e 67 20 53 51 4c 69 74 65 following SQLite
10cc0 20 69 6e 74 65 72 66 61 63 65 73 20 62 65 63 6f interfaces beco
10cd0 6d 65 20 0a 2a 2a 20 6e 6f 6e 2d 6f 70 65 72 61 me .** non-opera
10ce0 74 69 6f 6e 61 6c 3a 0a 2a 2a 20 20 20 3c 75 6c tional:.** <ul
10cf0 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c >.** <li> [sql
10d00 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 ite3_memory_used
10d10 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 ()].** <li> [s
10d20 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 qlite3_memory_hi
10d30 67 68 77 61 74 65 72 28 29 5d 0a 2a 2a 20 20 20 ghwater()].**
10d40 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f 73 6f <li> [sqlite3_so
10d50 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 5d ft_heap_limit()]
10d60 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 .** <li> [sqli
10d70 74 65 33 5f 73 74 61 74 75 73 28 29 5d 0a 2a 2a te3_status()].**
10d80 20 20 20 3c 2f 75 6c 3e 0a 2a 2a 20 3c 2f 64 64 </ul>.** </dd
10d90 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
10da0 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 TE_CONFIG_SCRATC
10db0 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 H</dt>.** <dd>Th
10dc0 69 73 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 is option specif
10dd0 69 65 73 20 61 20 73 74 61 74 69 63 20 6d 65 6d ies a static mem
10de0 6f 72 79 20 62 75 66 66 65 72 20 74 68 61 74 20 ory buffer that
10df0 53 51 4c 69 74 65 20 63 61 6e 20 75 73 65 20 66 SQLite can use f
10e00 6f 72 0a 2a 2a 20 73 63 72 61 74 63 68 20 6d 65 or.** scratch me
10e10 6d 6f 72 79 2e 20 20 54 68 65 72 65 20 61 72 65 mory. There are
10e20 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 three arguments
10e30 3a 20 20 41 20 70 6f 69 6e 74 65 72 20 61 6e 20 : A pointer an
10e40 38 2d 62 79 74 65 0a 2a 2a 20 61 6c 69 67 6e 65 8-byte.** aligne
10e50 64 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 d memory buffer
10e60 66 72 6f 6d 20 77 68 69 63 68 20 74 68 65 20 73 from which the s
10e70 63 72 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e crach allocation
10e80 73 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 72 61 s will be.** dra
10e90 77 6e 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 wn, the size of
10ea0 65 61 63 68 20 73 63 72 61 74 63 68 20 61 6c 6c each scratch all
10eb0 6f 63 61 74 69 6f 6e 20 28 73 7a 29 2c 0a 2a 2a ocation (sz),.**
10ec0 20 61 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d and the maximum
10ed0 20 6e 75 6d 62 65 72 20 6f 66 20 73 63 72 61 74 number of scrat
10ee0 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 28 ch allocations (
10ef0 4e 29 2e 20 20 54 68 65 20 73 7a 0a 2a 2a 20 61 N). The sz.** a
10f00 72 67 75 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 rgument must be
10f10 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 31 36 a multiple of 16
10f20 2e 20 54 68 65 20 73 7a 20 70 61 72 61 6d 65 74 . The sz paramet
10f30 65 72 20 73 68 6f 75 6c 64 20 62 65 20 61 20 66 er should be a f
10f40 65 77 20 62 79 74 65 73 0a 2a 2a 20 6c 61 72 67 ew bytes.** larg
10f50 65 72 20 74 68 61 6e 20 74 68 65 20 61 63 74 75 er than the actu
10f60 61 6c 20 73 63 72 61 74 63 68 20 73 70 61 63 65 al scratch space
10f70 20 72 65 71 75 69 72 65 64 20 64 75 65 20 74 6f required due to
10f80 20 69 6e 74 65 72 6e 61 6c 20 6f 76 65 72 68 65 internal overhe
10f90 61 64 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 ad..** The first
10fa0 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 argument should
10fb0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 38 pointer to an 8
10fc0 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 20 62 75 -byte aligned bu
10fd0 66 66 65 72 0a 2a 2a 20 6f 66 20 61 74 20 6c 65 ffer.** of at le
10fe0 61 73 74 20 73 7a 2a 4e 20 62 79 74 65 73 20 6f ast sz*N bytes o
10ff0 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 53 51 4c f memory..** SQL
11000 69 74 65 20 77 69 6c 6c 20 75 73 65 20 6e 6f 20 ite will use no
11010 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 73 63 more than one sc
11020 72 61 74 63 68 20 62 75 66 66 65 72 20 61 74 20 ratch buffer at
11030 6f 6e 63 65 20 70 65 72 20 74 68 72 65 61 64 2c once per thread,
11040 20 73 6f 0a 2a 2a 20 4e 20 73 68 6f 75 6c 64 20 so.** N should
11050 62 65 20 73 65 74 20 74 6f 20 74 68 65 20 65 78 be set to the ex
11060 70 65 63 74 65 64 20 6d 61 78 69 6d 75 6d 20 6e pected maximum n
11070 75 6d 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 umber of threads
11080 2e 20 20 54 68 65 20 73 7a 0a 2a 2a 20 70 61 72 . The sz.** par
11090 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 ameter should be
110a0 20 36 20 74 69 6d 65 73 20 74 68 65 20 73 69 7a 6 times the siz
110b0 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 e of the largest
110c0 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 73 database page s
110d0 69 7a 65 2e 0a 2a 2a 20 53 63 72 61 74 63 68 20 ize..** Scratch
110e0 62 75 66 66 65 72 73 20 61 72 65 20 75 73 65 64 buffers are used
110f0 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 as part of the
11100 62 74 72 65 65 20 62 61 6c 61 6e 63 65 20 6f 70 btree balance op
11110 65 72 61 74 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 eration. If.**
11120 54 68 65 20 62 74 72 65 65 20 62 61 6c 61 6e 63 The btree balanc
11130 65 72 20 6e 65 65 64 73 20 61 64 64 69 74 69 6f er needs additio
11140 6e 61 6c 20 6d 65 6d 6f 72 79 20 62 65 79 6f 6e nal memory beyon
11150 64 20 77 68 61 74 20 69 73 20 70 72 6f 76 69 64 d what is provid
11160 65 64 20 62 79 0a 2a 2a 20 73 63 72 61 74 63 68 ed by.** scratch
11170 20 62 75 66 66 65 72 73 20 6f 72 20 69 66 20 6e buffers or if n
11180 6f 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72 o scratch buffer
11190 20 73 70 61 63 65 20 69 73 20 73 70 65 63 69 66 space is specif
111a0 69 65 64 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 ied, then SQLite
111b0 0a 2a 2a 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c .** goes to [sql
111c0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 74 ite3_malloc()] t
111d0 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d o obtain the mem
111e0 6f 72 79 20 69 74 20 6e 65 65 64 73 2e 3c 2f 64 ory it needs.</d
111f0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
11200 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 ITE_CONFIG_PAGEC
11210 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 ACHE</dt>.** <dd
11220 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 73 70 65 >This option spe
11230 63 69 66 69 65 73 20 61 20 73 74 61 74 69 63 20 cifies a static
11240 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 68 memory buffer th
11250 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 75 73 at SQLite can us
11260 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 64 61 74 e for.** the dat
11270 61 62 61 73 65 20 70 61 67 65 20 63 61 63 68 65 abase page cache
11280 20 77 69 74 68 20 74 68 65 20 64 65 66 61 75 6c with the defaul
11290 74 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 t page cache imp
112a0 6c 65 6d 65 6e 61 74 69 6f 6e 2e 20 20 0a 2a 2a lemenation. .**
112b0 20 54 68 69 73 20 63 6f 6e 66 69 67 75 72 61 74 This configurat
112c0 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 ion should not b
112d0 65 20 75 73 65 64 20 69 66 20 61 6e 20 61 70 70 e used if an app
112e0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 20 lication-define
112f0 70 61 67 65 0a 2a 2a 20 63 61 63 68 65 20 69 6d page.** cache im
11300 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 plementation is
11310 6c 6f 61 64 65 64 20 75 73 69 6e 67 20 74 68 65 loaded using the
11320 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 SQLITE_CONFIG_P
11330 43 41 43 48 45 20 6f 70 74 69 6f 6e 2e 0a 2a 2a CACHE option..**
11340 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65 There are three
11350 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 arguments to th
11360 69 73 20 6f 70 74 69 6f 6e 3a 20 41 20 70 6f 69 is option: A poi
11370 6e 74 65 72 20 74 6f 20 38 2d 62 79 74 65 20 61 nter to 8-byte a
11380 6c 69 67 6e 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79 ligned.** memory
11390 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61 , the size of ea
113a0 63 68 20 70 61 67 65 20 62 75 66 66 65 72 20 28 ch page buffer (
113b0 73 7a 29 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d sz), and the num
113c0 62 65 72 20 6f 66 20 70 61 67 65 73 20 28 4e 29 ber of pages (N)
113d0 2e 0a 2a 2a 20 54 68 65 20 73 7a 20 61 72 67 75 ..** The sz argu
113e0 6d 65 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 74 ment should be t
113f0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c he size of the l
11400 61 72 67 65 73 74 20 64 61 74 61 62 61 73 65 20 argest database
11410 70 61 67 65 0a 2a 2a 20 28 61 20 70 6f 77 65 72 page.** (a power
11420 20 6f 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20 of two between
11430 35 31 32 20 61 6e 64 20 33 32 37 36 38 29 20 70 512 and 32768) p
11440 6c 75 73 20 61 20 6c 69 74 74 6c 65 20 65 78 74 lus a little ext
11450 72 61 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 70 ra for each.** p
11460 61 67 65 20 68 65 61 64 65 72 2e 20 20 54 68 65 age header. The
11470 20 70 61 67 65 20 68 65 61 64 65 72 20 73 69 7a page header siz
11480 65 20 69 73 20 32 30 20 74 6f 20 34 30 20 62 79 e is 20 to 40 by
11490 74 65 73 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e tes depending on
114a0 0a 2a 2a 20 74 68 65 20 68 6f 73 74 20 61 72 63 .** the host arc
114b0 68 69 74 65 63 74 75 72 65 2e 20 20 49 74 20 69 hitecture. It i
114c0 73 20 68 61 72 6d 6c 65 73 73 2c 20 61 70 61 72 s harmless, apar
114d0 74 20 66 72 6f 6d 20 74 68 65 20 77 61 73 74 65 t from the waste
114e0 64 20 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 74 6f 20 d memory,.** to
114f0 6d 61 6b 65 20 73 7a 20 61 20 6c 69 74 74 6c 65 make sz a little
11500 20 74 6f 6f 20 6c 61 72 67 65 2e 20 20 54 68 65 too large. The
11510 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 first.** argume
11520 6e 74 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74 20 nt should point
11530 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e to an allocation
11540 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 7a 2a of at least sz*
11550 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 N bytes of memor
11560 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c y..** SQLite wil
11570 6c 20 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 l use the memory
11580 20 70 72 6f 76 69 64 65 64 20 62 79 20 74 68 65 provided by the
11590 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
115a0 74 6f 20 73 61 74 69 73 66 79 20 69 74 73 0a 2a to satisfy its.*
115b0 2a 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73 20 66 * memory needs f
115c0 6f 72 20 74 68 65 20 66 69 72 73 74 20 4e 20 70 or the first N p
115d0 61 67 65 73 20 74 68 61 74 20 69 74 20 61 64 64 ages that it add
115e0 73 20 74 6f 20 63 61 63 68 65 2e 20 20 49 66 20 s to cache. If
115f0 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 70 61 additional.** pa
11600 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 ge cache memory
11610 69 73 20 6e 65 65 64 65 64 20 62 65 79 6f 6e 64 is needed beyond
11620 20 77 68 61 74 20 69 73 20 70 72 6f 76 69 64 65 what is provide
11630 64 20 62 79 20 74 68 69 73 20 6f 70 74 69 6f 6e d by this option
11640 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 , then.** SQLite
11650 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74 65 goes to [sqlite
11660 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f 72 20 3_malloc()] for
11670 74 68 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 the additional s
11680 74 6f 72 61 67 65 20 73 70 61 63 65 2e 0a 2a 2a torage space..**
11690 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 The implementat
116a0 69 6f 6e 20 6d 69 67 68 74 20 75 73 65 20 6f 6e ion might use on
116b0 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 e or more of the
116c0 20 4e 20 62 75 66 66 65 72 73 20 74 6f 20 68 6f N buffers to ho
116d0 6c 64 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 63 ld .** memory ac
116e0 63 6f 75 6e 74 69 6e 67 20 69 6e 66 6f 72 6d 61 counting informa
116f0 74 69 6f 6e 2e 20 54 68 65 20 70 6f 69 6e 74 65 tion. The pointe
11700 72 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 r in the first a
11710 72 67 75 6d 65 6e 74 20 6d 75 73 74 0a 2a 2a 20 rgument must.**
11720 62 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e be aligned to an
11730 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 8-byte boundary
11740 20 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 62 or subsequent b
11750 65 68 61 76 69 6f 72 20 6f 66 20 53 51 4c 69 74 ehavior of SQLit
11760 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 75 6e 64 e.** will be und
11770 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a efined.</dd>.**.
11780 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f ** <dt>SQLITE_CO
11790 4e 46 49 47 5f 48 45 41 50 3c 2f 64 74 3e 0a 2a NFIG_HEAP</dt>.*
117a0 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f * <dd>This optio
117b0 6e 20 73 70 65 63 69 66 69 65 73 20 61 20 73 74 n specifies a st
117c0 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 atic memory buff
117d0 65 72 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 er that SQLite w
117e0 69 6c 6c 20 75 73 65 0a 2a 2a 20 66 6f 72 20 61 ill use.** for a
117f0 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 ll of its dynami
11800 63 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 c memory allocat
11810 69 6f 6e 20 6e 65 65 64 73 20 62 65 79 6f 6e 64 ion needs beyond
11820 20 74 68 6f 73 65 20 70 72 6f 76 69 64 65 64 0a those provided.
11830 2a 2a 20 66 6f 72 20 62 79 20 5b 53 51 4c 49 54 ** for by [SQLIT
11840 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 E_CONFIG_SCRATCH
11850 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 43 4f ] and [SQLITE_CO
11860 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2e NFIG_PAGECACHE].
11870 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 .** There are th
11880 72 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 20 41 ree arguments: A
11890 6e 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 n 8-byte aligned
118a0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
118b0 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 74 68 65 20 6e memory,.** the n
118c0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
118d0 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 62 75 66 n the memory buf
118e0 66 65 72 2c 20 61 6e 64 20 74 68 65 20 6d 69 6e fer, and the min
118f0 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 imum allocation
11900 73 69 7a 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 size..** If the
11910 66 69 72 73 74 20 70 6f 69 6e 74 65 72 20 28 74 first pointer (t
11920 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 he memory pointe
11930 72 29 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e r) is NULL, then
11940 20 53 51 4c 69 74 65 20 72 65 76 65 72 74 73 0a SQLite reverts.
11950 2a 2a 20 74 6f 20 75 73 69 6e 67 20 69 74 73 20 ** to using its
11960 64 65 66 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61 default memory a
11970 6c 6c 6f 63 61 74 6f 72 20 28 74 68 65 20 73 79 llocator (the sy
11980 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 69 6d stem malloc() im
11990 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 2c 0a 2a plementation),.*
119a0 2a 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20 70 72 * undoing any pr
119b0 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f ior invocation o
119c0 66 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 f [SQLITE_CONFIG
119d0 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 49 66 20 74 68 _MALLOC]. If th
119e0 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f 69 6e e.** memory poin
119f0 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 ter is not NULL
11a00 61 6e 64 20 65 69 74 68 65 72 20 5b 53 51 4c 49 and either [SQLI
11a10 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 TE_ENABLE_MEMSYS
11a20 33 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 3] or.** [SQLITE
11a30 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 5d _ENABLE_MEMSYS5]
11a40 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68 are defined, th
11a50 65 6e 20 74 68 65 20 61 6c 74 65 72 6e 61 74 69 en the alternati
11a60 76 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c ve memory.** all
11a70 6f 63 61 74 6f 72 20 69 73 20 65 6e 67 61 67 65 ocator is engage
11a80 64 20 74 6f 20 68 61 6e 64 6c 65 20 61 6c 6c 20 d to handle all
11a90 6f 66 20 53 51 4c 69 74 65 73 20 6d 65 6d 6f 72 of SQLites memor
11aa0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 y allocation nee
11ab0 64 73 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 ds..** The first
11ac0 20 70 6f 69 6e 74 65 72 20 28 74 68 65 20 6d 65 pointer (the me
11ad0 6d 6f 72 79 20 70 6f 69 6e 74 65 72 29 20 6d 75 mory pointer) mu
11ae0 73 74 20 62 65 20 61 6c 69 67 6e 65 64 20 74 6f st be aligned to
11af0 20 61 6e 20 38 2d 62 79 74 65 0a 2a 2a 20 62 6f an 8-byte.** bo
11b00 75 6e 64 61 72 79 20 6f 72 20 73 75 62 73 65 71 undary or subseq
11b10 75 65 6e 74 20 62 65 68 61 76 69 6f 72 20 6f 66 uent behavior of
11b20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 SQLite will be
11b30 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a undefined.</dd>.
11b40 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
11b50 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 3c 2f 64 _CONFIG_MUTEX</d
11b60 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f t>.** <dd>This o
11b70 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 ption takes a si
11b80 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 ngle argument wh
11b90 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ich is a pointer
11ba0 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e to an.** instan
11bb0 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 ce of the [sqlit
11bc0 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
11bd0 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 ] structure. Th
11be0 65 20 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69 e argument speci
11bf0 66 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 fies.** alternat
11c00 69 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 75 ive low-level mu
11c10 74 65 78 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 tex routines to
11c20 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 be used in place
11c30 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20 72 6f .** the mutex ro
11c40 75 74 69 6e 65 73 20 62 75 69 6c 74 20 69 6e 74 utines built int
11c50 6f 20 53 51 4c 69 74 65 2e 3c 2f 64 64 3e 0a 2a o SQLite.</dd>.*
11c60 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
11c70 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 3c CONFIG_GETMUTEX<
11c80 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
11c90 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 option takes a
11ca0 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 single argument
11cb0 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 which is a point
11cc0 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 er to an.** inst
11cd0 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c ance of the [sql
11ce0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
11cf0 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 ds] structure.
11d00 54 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f The.** [sqlite3_
11d10 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 0a 2a mutex_methods].*
11d20 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 66 * structure is f
11d30 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 illed with the c
11d40 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 urrently defined
11d50 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 2e mutex routines.
11d60 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 .** This option
11d70 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f can be used to o
11d80 76 65 72 6c 6f 61 64 20 74 68 65 20 64 65 66 61 verload the defa
11d90 75 6c 74 20 6d 75 74 65 78 20 61 6c 6c 6f 63 61 ult mutex alloca
11da0 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 tion.** routines
11db0 20 77 69 74 68 20 61 20 77 72 61 70 70 65 72 20 with a wrapper
11dc0 75 73 65 64 20 74 6f 20 74 72 61 63 6b 20 6d 75 used to track mu
11dd0 74 65 78 20 75 73 61 67 65 20 66 6f 72 20 70 65 tex usage for pe
11de0 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 70 72 6f rformance.** pro
11df0 66 69 6c 69 6e 67 20 6f 72 20 74 65 73 74 69 6e filing or testin
11e00 67 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 3c g, for example.<
11e10 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
11e20 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f QLITE_CONFIG_LOO
11e30 4b 41 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c KASIDE</dt>.** <
11e40 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 dd>This option t
11e50 61 6b 65 73 20 74 77 6f 20 61 72 67 75 6d 65 6e akes two argumen
11e60 74 73 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e ts that determin
11e70 65 20 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a e the default.**
11e80 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
11e90 6f 6e 20 6c 6f 6f 6b 61 73 69 64 65 20 6f 70 74 on lookaside opt
11ea0 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 imization. The
11eb0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 first argument i
11ec0 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 s the.** size of
11ed0 20 65 61 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 each lookaside
11ee0 62 75 66 66 65 72 20 73 6c 6f 74 20 61 6e 64 20 buffer slot and
11ef0 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68 the second is th
11f00 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 e number of.** s
11f10 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 74 lots allocated t
11f20 6f 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 o each database
11f30 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69 connection. Thi
11f40 73 20 6f 70 74 69 6f 6e 20 73 65 74 73 20 74 68 s option sets th
11f50 65 0a 2a 2a 20 3c 69 3e 64 65 66 61 75 6c 74 3c e.** <i>default<
11f60 2f 69 3e 20 6c 6f 6f 6b 61 73 69 64 65 20 73 69 /i> lookaside si
11f70 7a 65 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 45 ze. The [SQLITE
11f80 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 _DBCONFIG_LOOKAS
11f90 49 44 45 5d 0a 2a 2a 20 76 65 72 62 20 74 6f 20 IDE].** verb to
11fa0 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 [sqlite3_db_conf
11fb0 69 67 28 29 5d 20 63 61 6e 20 62 65 20 75 73 65 ig()] can be use
11fc0 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 d to change the
11fd0 6c 6f 6f 6b 61 73 69 64 65 0a 2a 2a 20 63 6f 6e lookaside.** con
11fe0 66 69 67 75 72 61 74 69 6f 6e 20 6f 6e 20 69 6e figuration on in
11ff0 64 69 76 69 64 75 61 6c 20 63 6f 6e 6e 65 63 74 dividual connect
12000 69 6f 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a ions.</dd>.**.**
12010 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 <dt>SQLITE_CONF
12020 49 47 5f 50 43 41 43 48 45 3c 2f 64 74 3e 0a 2a IG_PCACHE</dt>.*
12030 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f * <dd>This optio
12040 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 n takes a single
12050 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 argument which
12060 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a is a pointer to.
12070 2a 2a 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 70 ** an [sqlite3_p
12080 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 5d 20 6f cache_methods] o
12090 62 6a 65 63 74 2e 20 20 54 68 69 73 20 6f 62 6a bject. This obj
120a0 65 63 74 20 73 70 65 63 69 66 69 65 73 20 74 68 ect specifies th
120b0 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 e interface.** t
120c0 6f 20 61 20 63 75 73 74 6f 6d 20 70 61 67 65 20 o a custom page
120d0 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 cache implementa
120e0 74 69 6f 6e 2e 20 20 53 51 4c 69 74 65 20 6d 61 tion. SQLite ma
120f0 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 kes a copy of th
12100 65 0a 2a 2a 20 6f 62 6a 65 63 74 20 61 6e 64 20 e.** object and
12110 75 73 65 73 20 69 74 20 66 6f 72 20 70 61 67 65 uses it for page
12120 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c cache memory al
12130 6c 6f 63 61 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a locations.</dd>.
12140 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
12150 5f 43 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48 _CONFIG_GETPCACH
12160 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 E</dt>.** <dd>Th
12170 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 is option takes
12180 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e a single argumen
12190 74 20 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 t which is a poi
121a0 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 nter to an.** [s
121b0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 qlite3_pcache_me
121c0 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 20 20 thods] object.
121d0 53 51 4c 69 74 65 20 63 6f 70 69 65 73 20 6f 66 SQLite copies of
121e0 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 the current.**
121f0 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 page cache imple
12200 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 mentation into t
12210 68 61 74 20 6f 62 6a 65 63 74 2e 3c 2f 64 64 3e hat object.</dd>
12220 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a .**.** </dl>.*/.
12230 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
12240 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 ONFIG_SINGLETHRE
12250 41 44 20 20 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f AD 1 /* nil */
12260 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
12270 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 CONFIG_MULTITHRE
12280 41 44 20 20 20 32 20 20 2f 2a 20 6e 69 6c 20 2a AD 2 /* nil *
12290 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
122a0 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a _CONFIG_SERIALIZ
122b0 45 44 20 20 20 20 33 20 20 2f 2a 20 6e 69 6c 20 ED 3 /* nil
122c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
122d0 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 E_CONFIG_MALLOC
122e0 20 20 20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c 4 /* sql
122f0 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
12300 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c * */.#define SQL
12310 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 ITE_CONFIG_GETMA
12320 4c 4c 4f 43 20 20 20 20 20 35 20 20 2f 2a 20 73 LLOC 5 /* s
12330 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
12340 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ds* */.#define S
12350 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 QLITE_CONFIG_SCR
12360 41 54 43 48 20 20 20 20 20 20 20 36 20 20 2f 2a ATCH 6 /*
12370 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 void*, int sz,
12380 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 int N */.#define
12390 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 SQLITE_CONFIG_P
123a0 41 47 45 43 41 43 48 45 20 20 20 20 20 37 20 20 AGECACHE 7
123b0 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a /* void*, int sz
123c0 2c 20 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 , int N */.#defi
123d0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ne SQLITE_CONFIG
123e0 5f 48 45 41 50 20 20 20 20 20 20 20 20 20 20 38 _HEAP 8
123f0 20 20 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 /* void*, int
12400 6e 42 79 74 65 2c 20 69 6e 74 20 6d 69 6e 20 2a nByte, int min *
12410 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
12420 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 _CONFIG_MEMSTATU
12430 53 20 20 20 20 20 39 20 20 2f 2a 20 62 6f 6f 6c S 9 /* bool
12440 65 61 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ean */.#define S
12450 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 QLITE_CONFIG_MUT
12460 45 58 20 20 20 20 20 20 20 20 31 30 20 20 2f 2a EX 10 /*
12470 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
12480 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 ethods* */.#defi
12490 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ne SQLITE_CONFIG
124a0 5f 47 45 54 4d 55 54 45 58 20 20 20 20 20 31 31 _GETMUTEX 11
124b0 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 /* sqlite3_mut
124c0 65 78 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f ex_methods* */./
124d0 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 53 51 4c * previously SQL
124e0 49 54 45 5f 43 4f 4e 46 49 47 5f 43 48 55 4e 4b ITE_CONFIG_CHUNK
124f0 41 4c 4c 4f 43 20 31 32 20 77 68 69 63 68 20 69 ALLOC 12 which i
12500 73 20 6e 6f 77 20 75 6e 75 73 65 64 2e 20 2a 2f s now unused. */
12510 20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .#define SQLITE
12520 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 _CONFIG_LOOKASID
12530 45 20 20 20 20 31 33 20 20 2f 2a 20 69 6e 74 20 E 13 /* int
12540 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 int */.#define S
12550 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 QLITE_CONFIG_PCA
12560 43 48 45 20 20 20 20 20 20 20 31 34 20 20 2f 2a CHE 14 /*
12570 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f sqlite3_pcache_
12580 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 methods* */.#def
12590 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ine SQLITE_CONFI
125a0 47 5f 47 45 54 50 43 41 43 48 45 20 20 20 20 31 G_GETPCACHE 1
125b0 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63 5 /* sqlite3_pc
125c0 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f ache_methods* */
125d0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
125e0 3a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 : Configuration
125f0 4f 70 74 69 6f 6e 73 20 7b 48 31 30 31 37 30 7d Options {H10170}
12600 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 <S20000>.** EXP
12610 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 ERIMENTAL.**.**
12620 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 These constants
12630 61 72 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c are the availabl
12640 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 66 69 67 e integer config
12650 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 uration options
12660 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 70 that.** can be p
12670 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 assed as the sec
12680 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 ond argument to
12690 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f the [sqlite3_db_
126a0 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 config()] interf
126b0 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 ace..**.** New c
126c0 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 onfiguration opt
126d0 69 6f 6e 73 20 6d 61 79 20 62 65 20 61 64 64 65 ions may be adde
126e0 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 d in future rele
126f0 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a ases of SQLite..
12700 2a 2a 20 45 78 69 73 74 69 6e 67 20 63 6f 6e 66 ** Existing conf
12710 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e iguration option
12720 73 20 6d 69 67 68 74 20 62 65 20 64 69 73 63 6f s might be disco
12730 6e 74 69 6e 75 65 64 2e 20 20 41 70 70 6c 69 63 ntinued. Applic
12740 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 ations.** should
12750 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 check the retur
12760 6e 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c n code from [sql
12770 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 ite3_db_config()
12780 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 ] to make sure t
12790 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 hat.** the call
127a0 77 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 worked. The [sq
127b0 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 lite3_db_config(
127c0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c )] interface wil
127d0 6c 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f l return a.** no
127e0 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f n-zero [error co
127f0 64 65 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74 de] if a discont
12800 69 6e 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f inued or unsuppo
12810 72 74 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 rted configurati
12820 6f 6e 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 on option.** is
12830 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c invoked..**.** <
12840 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 dl>.** <dt>SQLIT
12850 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 E_DBCONFIG_LOOKA
12860 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 SIDE</dt>.** <dd
12870 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b >This option tak
12880 65 73 20 74 68 72 65 65 20 61 64 64 69 74 69 6f es three additio
12890 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 20 74 68 nal arguments th
128a0 61 74 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 at determine the
128b0 20 0a 2a 2a 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 .** [lookaside
128c0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
128d0 5d 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 ] configuration
128e0 66 6f 72 20 74 68 65 20 5b 64 61 74 61 62 61 73 for the [databas
128f0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a e connection]..*
12900 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 * The first argu
12910 6d 65 6e 74 20 28 74 68 65 20 74 68 69 72 64 20 ment (the third
12920 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 parameter to [sq
12930 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 lite3_db_config(
12940 29 5d 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 )] is a.** point
12950 65 72 20 74 6f 20 61 6e 20 6d 65 6d 6f 72 79 20 er to an memory
12960 62 75 66 66 65 72 20 74 6f 20 75 73 65 20 66 6f buffer to use fo
12970 72 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f r lookaside memo
12980 72 79 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 ry..** The first
12990 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 20 62 65 argument may be
129a0 20 4e 55 4c 4c 20 69 6e 20 77 68 69 63 68 20 63 NULL in which c
129b0 61 73 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 ase SQLite will
129c0 61 6c 6c 6f 63 61 74 65 20 74 68 65 0a 2a 2a 20 allocate the.**
129d0 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 lookaside buffer
129e0 20 69 74 73 65 6c 66 20 75 73 69 6e 67 20 5b 73 itself using [s
129f0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
12a00 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 . The second ar
12a10 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a gument is the.**
12a20 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f size of each lo
12a30 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 73 okaside buffer s
12a40 6c 6f 74 20 61 6e 64 20 74 68 65 20 74 68 69 72 lot and the thir
12a50 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 d argument is th
12a60 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 e number of.** s
12a70 6c 6f 74 73 2e 20 20 54 68 65 20 73 69 7a 65 20 lots. The size
12a80 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 69 6e of the buffer in
12a90 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
12aa0 65 6e 74 20 6d 75 73 74 20 62 65 20 67 72 65 61 ent must be grea
12ab0 74 65 72 20 74 68 61 6e 0a 2a 2a 20 6f 72 20 65 ter than.** or e
12ac0 71 75 61 6c 20 74 6f 20 74 68 65 20 70 72 6f 64 qual to the prod
12ad0 75 63 74 20 6f 66 20 74 68 65 20 73 65 63 6f 6e uct of the secon
12ae0 64 20 61 6e 64 20 74 68 69 72 64 20 61 72 67 75 d and third argu
12af0 6d 65 6e 74 73 2e 20 20 54 68 65 20 62 75 66 66 ments. The buff
12b00 65 72 0a 2a 2a 20 6d 75 73 74 20 62 65 20 61 6c er.** must be al
12b10 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 79 igned to an 8-by
12b20 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 20 49 66 te boundary. If
12b30 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
12b40 6d 65 6e 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 ment is not.** a
12b50 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 2c 20 multiple of 8,
12b60 69 74 20 69 73 20 69 6e 74 65 72 6e 61 6c 6c 79 it is internally
12b70 20 72 6f 75 6e 64 65 64 20 64 6f 77 6e 20 74 6f rounded down to
12b80 20 74 68 65 20 6e 65 78 74 20 73 6d 61 6c 6c 65 the next smalle
12b90 72 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 6f 66 r.** multiple of
12ba0 20 38 2e 20 20 53 65 65 20 61 6c 73 6f 3a 20 5b 8. See also: [
12bb0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f SQLITE_CONFIG_LO
12bc0 4f 4b 41 53 49 44 45 5d 3c 2f 64 64 3e 0a 2a 2a OKASIDE]</dd>.**
12bd0 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 .** </dl>.*/.#de
12be0 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 43 4f fine SQLITE_DBCO
12bf0 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 NFIG_LOOKASIDE
12c00 20 20 31 30 30 31 20 20 2f 2a 20 76 6f 69 64 2a 1001 /* void*
12c10 20 69 6e 74 20 69 6e 74 20 2a 2f 0a 0a 0a 2f 2a int int */.../*
12c20 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e .** CAPI3REF: En
12c30 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 able Or Disable
12c40 45 78 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20 Extended Result
12c50 43 6f 64 65 73 20 7b 48 31 32 32 30 30 7d 20 3c Codes {H12200} <
12c60 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S10700>.**.** Th
12c70 65 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 e sqlite3_extend
12c80 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 ed_result_codes(
12c90 29 20 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 ) routine enable
12ca0 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68 s or disables th
12cb0 65 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 e.** [extended r
12cc0 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 66 65 61 esult codes] fea
12cd0 74 75 72 65 20 6f 66 20 53 51 4c 69 74 65 2e 20 ture of SQLite.
12ce0 54 68 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 The extended res
12cf0 75 6c 74 0a 2a 2a 20 63 6f 64 65 73 20 61 72 65 ult.** codes are
12d00 20 64 69 73 61 62 6c 65 64 20 62 79 20 64 65 66 disabled by def
12d10 61 75 6c 74 20 66 6f 72 20 68 69 73 74 6f 72 69 ault for histori
12d20 63 61 6c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 cal compatibilit
12d30 79 20 63 6f 6e 73 69 64 65 72 61 74 69 6f 6e 73 y considerations
12d40 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
12d50 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 30 31 ents:.** [H12201
12d60 5d 20 5b 48 31 32 32 30 32 5d 0a 2a 2f 0a 53 51 ] [H12202].*/.SQ
12d70 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
12d80 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 ite3_extended_re
12d90 73 75 6c 74 5f 63 6f 64 65 73 28 73 71 6c 69 74 sult_codes(sqlit
12da0 65 33 2a 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 3b e3*, int onoff);
12db0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
12dc0 3a 20 4c 61 73 74 20 49 6e 73 65 72 74 20 52 6f : Last Insert Ro
12dd0 77 69 64 20 7b 48 31 32 32 32 30 7d 20 3c 53 31 wid {H12220} <S1
12de0 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 0700>.**.** Each
12df0 20 65 6e 74 72 79 20 69 6e 20 61 6e 20 53 51 4c entry in an SQL
12e00 69 74 65 20 74 61 62 6c 65 20 68 61 73 20 61 20 ite table has a
12e10 75 6e 69 71 75 65 20 36 34 2d 62 69 74 20 73 69 unique 64-bit si
12e20 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 gned.** integer
12e30 6b 65 79 20 63 61 6c 6c 65 64 20 74 68 65 20 5b key called the [
12e40 52 4f 57 49 44 20 7c 20 22 72 6f 77 69 64 22 5d ROWID | "rowid"]
12e50 2e 20 54 68 65 20 72 6f 77 69 64 20 69 73 20 61 . The rowid is a
12e60 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c 65 0a lways available.
12e70 2a 2a 20 61 73 20 61 6e 20 75 6e 64 65 63 6c 61 ** as an undecla
12e80 72 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 red column named
12e90 20 52 4f 57 49 44 2c 20 4f 49 44 2c 20 6f 72 20 ROWID, OID, or
12ea0 5f 52 4f 57 49 44 5f 20 61 73 20 6c 6f 6e 67 20 _ROWID_ as long
12eb0 61 73 20 74 68 6f 73 65 0a 2a 2a 20 6e 61 6d 65 as those.** name
12ec0 73 20 61 72 65 20 6e 6f 74 20 61 6c 73 6f 20 75 s are not also u
12ed0 73 65 64 20 62 79 20 65 78 70 6c 69 63 69 74 6c sed by explicitl
12ee0 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d y declared colum
12ef0 6e 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20 74 61 ns. If.** the ta
12f00 62 6c 65 20 68 61 73 20 61 20 63 6f 6c 75 6d 6e ble has a column
12f10 20 6f 66 20 74 79 70 65 20 5b 49 4e 54 45 47 45 of type [INTEGE
12f20 52 20 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 74 R PRIMARY KEY] t
12f30 68 65 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0a hen that column.
12f40 2a 2a 20 69 73 20 61 6e 6f 74 68 65 72 20 61 6c ** is another al
12f50 69 61 73 20 66 6f 72 20 74 68 65 20 72 6f 77 69 ias for the rowi
12f60 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f d..**.** This ro
12f70 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 utine returns th
12f80 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 e [rowid] of the
12f90 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 most recent.**
12fa0 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 successful [INSE
12fb0 52 54 5d 20 69 6e 74 6f 20 74 68 65 20 64 61 74 RT] into the dat
12fc0 61 62 61 73 65 20 66 72 6f 6d 20 74 68 65 20 5b abase from the [
12fd0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
12fe0 69 6f 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 ion].** in the f
12ff0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 20 irst argument.
13000 49 66 20 6e 6f 20 73 75 63 63 65 73 73 66 75 6c If no successful
13010 20 5b 49 4e 53 45 52 54 5d 73 0a 2a 2a 20 68 61 [INSERT]s.** ha
13020 76 65 20 65 76 65 72 20 6f 63 63 75 72 72 65 64 ve ever occurred
13030 20 6f 6e 20 74 68 61 74 20 64 61 74 61 62 61 73 on that databas
13040 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65 e connection, ze
13050 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a ro is returned..
13060 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 5b 49 4e 53 **.** If an [INS
13070 45 52 54 5d 20 6f 63 63 75 72 73 20 77 69 74 68 ERT] occurs with
13080 69 6e 20 61 20 74 72 69 67 67 65 72 2c 20 74 68 in a trigger, th
13090 65 6e 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f en the [rowid] o
130a0 66 20 74 68 65 20 69 6e 73 65 72 74 65 64 0a 2a f the inserted.*
130b0 2a 20 72 6f 77 20 69 73 20 72 65 74 75 72 6e 65 * row is returne
130c0 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e d by this routin
130d0 65 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 e as long as the
130e0 20 74 72 69 67 67 65 72 20 69 73 20 72 75 6e 6e trigger is runn
130f0 69 6e 67 2e 0a 2a 2a 20 42 75 74 20 6f 6e 63 65 ing..** But once
13100 20 74 68 65 20 74 72 69 67 67 65 72 20 74 65 72 the trigger ter
13110 6d 69 6e 61 74 65 73 2c 20 74 68 65 20 76 61 6c minates, the val
13120 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 ue returned by t
13130 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 his routine.** r
13140 65 76 65 72 74 73 20 74 6f 20 74 68 65 20 6c 61 everts to the la
13150 73 74 20 76 61 6c 75 65 20 69 6e 73 65 72 74 65 st value inserte
13160 64 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 69 d before the tri
13170 67 67 65 72 20 66 69 72 65 64 2e 0a 2a 2a 0a 2a gger fired..**.*
13180 2a 20 41 6e 20 5b 49 4e 53 45 52 54 5d 20 74 68 * An [INSERT] th
13190 61 74 20 66 61 69 6c 73 20 64 75 65 20 74 6f 20 at fails due to
131a0 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f a constraint vio
131b0 6c 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 0a lation is not a.
131c0 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 ** successful [I
131d0 4e 53 45 52 54 5d 20 61 6e 64 20 64 6f 65 73 20 NSERT] and does
131e0 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 not change the v
131f0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
13200 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 this.** routine
13210 2e 20 20 54 68 75 73 20 49 4e 53 45 52 54 20 4f . Thus INSERT O
13220 52 20 46 41 49 4c 2c 20 49 4e 53 45 52 54 20 4f R FAIL, INSERT O
13230 52 20 49 47 4e 4f 52 45 2c 20 49 4e 53 45 52 54 R IGNORE, INSERT
13240 20 4f 52 20 52 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a OR ROLLBACK,.**
13250 20 61 6e 64 20 49 4e 53 45 52 54 20 4f 52 20 41 and INSERT OR A
13260 42 4f 52 54 20 6d 61 6b 65 20 6e 6f 20 63 68 61 BORT make no cha
13270 6e 67 65 73 20 74 6f 20 74 68 65 20 72 65 74 75 nges to the retu
13280 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 rn value of this
13290 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 68 65 6e .** routine when
132a0 20 74 68 65 69 72 20 69 6e 73 65 72 74 69 6f 6e their insertion
132b0 20 66 61 69 6c 73 2e 20 20 57 68 65 6e 20 49 4e fails. When IN
132c0 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45 0a SERT OR REPLACE.
132d0 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 20 ** encounters a
132e0 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 constraint viola
132f0 74 69 6f 6e 2c 20 69 74 20 64 6f 65 73 20 6e 6f tion, it does no
13300 74 20 66 61 69 6c 2e 20 20 54 68 65 0a 2a 2a 20 t fail. The.**
13310 49 4e 53 45 52 54 20 63 6f 6e 74 69 6e 75 65 73 INSERT continues
13320 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 61 to completion a
13330 66 74 65 72 20 64 65 6c 65 74 69 6e 67 20 72 6f fter deleting ro
13340 77 73 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a ws that caused.*
13350 2a 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 * the constraint
13360 20 70 72 6f 62 6c 65 6d 20 73 6f 20 49 4e 53 45 problem so INSE
13370 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20 77 69 RT OR REPLACE wi
13380 6c 6c 20 61 6c 77 61 79 73 20 63 68 61 6e 67 65 ll always change
13390 0a 2a 2a 20 74 68 65 20 72 65 74 75 72 6e 20 76 .** the return v
133a0 61 6c 75 65 20 6f 66 20 74 68 69 73 20 69 6e 74 alue of this int
133b0 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f erface..**.** Fo
133c0 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f r the purposes o
133d0 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 f this routine,
133e0 61 6e 20 5b 49 4e 53 45 52 54 5d 20 69 73 20 63 an [INSERT] is c
133f0 6f 6e 73 69 64 65 72 65 64 20 74 6f 0a 2a 2a 20 onsidered to.**
13400 62 65 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 be successful ev
13410 65 6e 20 69 66 20 69 74 20 69 73 20 73 75 62 73 en if it is subs
13420 65 71 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 equently rolled
13430 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 back..**.** Requ
13440 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
13450 32 32 32 31 5d 20 5b 48 31 32 32 32 33 5d 0a 2a 2221] [H12223].*
13460 2a 0a 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61 *.** If a separa
13470 74 65 20 74 68 72 65 61 64 20 70 65 72 66 6f 72 te thread perfor
13480 6d 73 20 61 20 6e 65 77 20 5b 49 4e 53 45 52 54 ms a new [INSERT
13490 5d 20 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a ] on the same.**
134a0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
134b0 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 65 20 5b tion while the [
134c0 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 sqlite3_last_ins
134d0 65 72 74 5f 72 6f 77 69 64 28 29 5d 0a 2a 2a 20 ert_rowid()].**
134e0 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e function is runn
134f0 69 6e 67 20 61 6e 64 20 74 68 75 73 20 63 68 61 ing and thus cha
13500 6e 67 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e nges the last in
13510 73 65 72 74 20 5b 72 6f 77 69 64 5d 2c 0a 2a 2a sert [rowid],.**
13520 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 then the value
13530 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c returned by [sql
13540 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 ite3_last_insert
13550 5f 72 6f 77 69 64 28 29 5d 20 69 73 0a 2a 2a 20 _rowid()] is.**
13560 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20 61 6e unpredictable an
13570 64 20 6d 69 67 68 74 20 6e 6f 74 20 65 71 75 61 d might not equa
13580 6c 20 65 69 74 68 65 72 20 74 68 65 20 6f 6c 64 l either the old
13590 20 6f 72 20 74 68 65 20 6e 65 77 0a 2a 2a 20 6c or the new.** l
135a0 61 73 74 20 69 6e 73 65 72 74 20 5b 72 6f 77 69 ast insert [rowi
135b0 64 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 d]..*/.SQLITE_AP
135c0 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 I sqlite3_int64
135d0 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 sqlite3_last_ins
135e0 65 72 74 5f 72 6f 77 69 64 28 73 71 6c 69 74 65 ert_rowid(sqlite
135f0 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 3*);../*.** CAPI
13600 33 52 45 46 3a 20 43 6f 75 6e 74 20 54 68 65 20 3REF: Count The
13610 4e 75 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d Number Of Rows M
13620 6f 64 69 66 69 65 64 20 7b 48 31 32 32 34 30 7d odified {H12240}
13630 20 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S10600>.**.**
13640 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 This function re
13650 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 turns the number
13660 20 6f 66 20 64 61 74 61 62 61 73 65 20 72 6f 77 of database row
13670 73 20 74 68 61 74 20 77 65 72 65 20 63 68 61 6e s that were chan
13680 67 65 64 0a 2a 2a 20 6f 72 20 69 6e 73 65 72 74 ged.** or insert
13690 65 64 20 6f 72 20 64 65 6c 65 74 65 64 20 62 79 ed or deleted by
136a0 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
136b0 6c 79 20 63 6f 6d 70 6c 65 74 65 64 20 53 51 4c ly completed SQL
136c0 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6f 6e statement.** on
136d0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
136e0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 onnection] speci
136f0 66 69 65 64 20 62 79 20 74 68 65 20 66 69 72 73 fied by the firs
13700 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 t parameter..**
13710 4f 6e 6c 79 20 63 68 61 6e 67 65 73 20 74 68 61 Only changes tha
13720 74 20 61 72 65 20 64 69 72 65 63 74 6c 79 20 73 t are directly s
13730 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 pecified by the
13740 5b 49 4e 53 45 52 54 5d 2c 20 5b 55 50 44 41 54 [INSERT], [UPDAT
13750 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 44 45 4c 45 54 E],.** or [DELET
13760 45 5d 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65 E] statement are
13770 20 63 6f 75 6e 74 65 64 2e 20 20 41 75 78 69 6c counted. Auxil
13780 69 61 72 79 20 63 68 61 6e 67 65 73 20 63 61 75 iary changes cau
13790 73 65 64 20 62 79 0a 2a 2a 20 74 72 69 67 67 65 sed by.** trigge
137a0 72 73 20 6f 72 20 5b 66 6f 72 65 69 67 6e 20 6b rs or [foreign k
137b0 65 79 20 61 63 74 69 6f 6e 73 5d 20 61 72 65 20 ey actions] are
137c0 6e 6f 74 20 63 6f 75 6e 74 65 64 2e 20 55 73 65 not counted. Use
137d0 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 the.** [sqlite3
137e0 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 _total_changes()
137f0 5d 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 69 ] function to fi
13800 6e 64 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d nd the total num
13810 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73 0a 2a ber of changes.*
13820 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 68 61 6e * including chan
13830 67 65 73 20 63 61 75 73 65 64 20 62 79 20 74 72 ges caused by tr
13840 69 67 67 65 72 73 20 61 6e 64 20 66 6f 72 65 69 iggers and forei
13850 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 73 2e 0a gn key actions..
13860 2a 2a 0a 2a 2a 20 43 68 61 6e 67 65 73 20 74 6f **.** Changes to
13870 20 61 20 76 69 65 77 20 74 68 61 74 20 61 72 65 a view that are
13880 20 73 69 6d 75 6c 61 74 65 64 20 62 79 20 61 6e simulated by an
13890 20 5b 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 [INSTEAD OF tri
138a0 67 67 65 72 5d 0a 2a 2a 20 61 72 65 20 6e 6f 74 gger].** are not
138b0 20 63 6f 75 6e 74 65 64 2e 20 20 4f 6e 6c 79 20 counted. Only
138c0 72 65 61 6c 20 74 61 62 6c 65 20 63 68 61 6e 67 real table chang
138d0 65 73 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 0a es are counted..
138e0 2a 2a 0a 2a 2a 20 41 20 22 72 6f 77 20 63 68 61 **.** A "row cha
138f0 6e 67 65 22 20 69 73 20 61 20 63 68 61 6e 67 65 nge" is a change
13900 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 to a single row
13910 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 of a single tab
13920 6c 65 0a 2a 2a 20 63 61 75 73 65 64 20 62 79 20 le.** caused by
13930 61 6e 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54 an INSERT, DELET
13940 45 2c 20 6f 72 20 55 50 44 41 54 45 20 73 74 61 E, or UPDATE sta
13950 74 65 6d 65 6e 74 2e 20 20 52 6f 77 73 20 74 68 tement. Rows th
13960 61 74 0a 2a 2a 20 61 72 65 20 63 68 61 6e 67 65 at.** are change
13970 64 20 61 73 20 73 69 64 65 20 65 66 66 65 63 74 d as side effect
13980 73 20 6f 66 20 5b 52 45 50 4c 41 43 45 5d 20 63 s of [REPLACE] c
13990 6f 6e 73 74 72 61 69 6e 74 20 72 65 73 6f 6c 75 onstraint resolu
139a0 74 69 6f 6e 2c 0a 2a 2a 20 72 6f 6c 6c 62 61 63 tion,.** rollbac
139b0 6b 2c 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 k, ABORT process
139c0 69 6e 67 2c 20 5b 44 52 4f 50 20 54 41 42 4c 45 ing, [DROP TABLE
139d0 5d 2c 20 6f 72 20 62 79 20 61 6e 79 20 6f 74 68 ], or by any oth
139e0 65 72 0a 2a 2a 20 6d 65 63 68 61 6e 69 73 6d 73 er.** mechanisms
139f0 20 64 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 do not count as
13a00 20 64 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e direct row chan
13a10 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 74 72 ges..**.** A "tr
13a20 69 67 67 65 72 20 63 6f 6e 74 65 78 74 22 20 69 igger context" i
13a30 73 20 61 20 73 63 6f 70 65 20 6f 66 20 65 78 65 s a scope of exe
13a40 63 75 74 69 6f 6e 20 74 68 61 74 20 62 65 67 69 cution that begi
13a50 6e 73 20 61 6e 64 0a 2a 2a 20 65 6e 64 73 20 77 ns and.** ends w
13a60 69 74 68 20 74 68 65 20 73 63 72 69 70 74 20 6f ith the script o
13a70 66 20 61 20 5b 43 52 45 41 54 45 20 54 52 49 47 f a [CREATE TRIG
13a80 47 45 52 20 7c 20 74 72 69 67 67 65 72 5d 2e 20 GER | trigger].
13a90 0a 2a 2a 20 4d 6f 73 74 20 53 51 4c 20 73 74 61 .** Most SQL sta
13aa0 74 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 65 tements are.** e
13ab0 76 61 6c 75 61 74 65 64 20 6f 75 74 73 69 64 65 valuated outside
13ac0 20 6f 66 20 61 6e 79 20 74 72 69 67 67 65 72 2e of any trigger.
13ad0 20 20 54 68 69 73 20 69 73 20 74 68 65 20 22 74 This is the "t
13ae0 6f 70 20 6c 65 76 65 6c 22 0a 2a 2a 20 74 72 69 op level".** tri
13af0 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 49 gger context. I
13b00 66 20 61 20 74 72 69 67 67 65 72 20 66 69 72 65 f a trigger fire
13b10 73 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 6c s from the top l
13b20 65 76 65 6c 2c 20 61 0a 2a 2a 20 6e 65 77 20 74 evel, a.** new t
13b30 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 20 69 rigger context i
13b40 73 20 65 6e 74 65 72 65 64 20 66 6f 72 20 74 68 s entered for th
13b50 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68 e duration of th
13b60 61 74 20 6f 6e 65 0a 2a 2a 20 74 72 69 67 67 65 at one.** trigge
13b70 72 2e 20 20 53 75 62 74 72 69 67 67 65 72 73 20 r. Subtriggers
13b80 63 72 65 61 74 65 20 73 75 62 63 6f 6e 74 65 78 create subcontex
13b90 74 73 20 66 6f 72 20 74 68 65 69 72 20 64 75 72 ts for their dur
13ba0 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c ation..**.** Cal
13bb0 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 65 78 ling [sqlite3_ex
13bc0 65 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 ec()] or [sqlite
13bd0 33 5f 73 74 65 70 28 29 5d 20 72 65 63 75 72 73 3_step()] recurs
13be0 69 76 65 6c 79 20 64 6f 65 73 0a 2a 2a 20 6e 6f ively does.** no
13bf0 74 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 74 t create a new t
13c00 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 0a rigger context..
13c10 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
13c20 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 ion returns the
13c30 6e 75 6d 62 65 72 20 6f 66 20 64 69 72 65 63 74 number of direct
13c40 20 72 6f 77 20 63 68 61 6e 67 65 73 20 69 6e 20 row changes in
13c50 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 72 65 63 65 the.** most rece
13c60 6e 74 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 nt INSERT, UPDAT
13c70 45 2c 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 E, or DELETE sta
13c80 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 74 68 tement within th
13c90 65 20 73 61 6d 65 0a 2a 2a 20 74 72 69 67 67 65 e same.** trigge
13ca0 72 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a r context..**.**
13cb0 20 54 68 75 73 2c 20 77 68 65 6e 20 63 61 6c 6c Thus, when call
13cc0 65 64 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 ed from the top
13cd0 6c 65 76 65 6c 2c 20 74 68 69 73 20 66 75 6e 63 level, this func
13ce0 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 tion returns the
13cf0 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 .** number of ch
13d00 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 anges in the mos
13d10 74 20 72 65 63 65 6e 74 20 49 4e 53 45 52 54 2c t recent INSERT,
13d20 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 UPDATE, or DELE
13d30 54 45 0a 2a 2a 20 74 68 61 74 20 61 6c 73 6f 20 TE.** that also
13d40 6f 63 63 75 72 72 65 64 20 61 74 20 74 68 65 20 occurred at the
13d50 74 6f 70 20 6c 65 76 65 6c 2e 20 20 57 69 74 68 top level. With
13d60 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 in the body of a
13d70 20 74 72 69 67 67 65 72 2c 0a 2a 2a 20 74 68 65 trigger,.** the
13d80 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 sqlite3_changes
13d90 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e () interface can
13da0 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 66 69 be called to fi
13db0 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 nd the number of
13dc0 0a 2a 2a 20 63 68 61 6e 67 65 73 20 69 6e 20 74 .** changes in t
13dd0 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 he most recently
13de0 20 63 6f 6d 70 6c 65 74 65 64 20 49 4e 53 45 52 completed INSER
13df0 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 T, UPDATE, or DE
13e00 4c 45 54 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e LETE.** statemen
13e10 74 20 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 t within the bod
13e20 79 20 6f 66 20 74 68 65 20 73 61 6d 65 20 74 72 y of the same tr
13e30 69 67 67 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65 igger..** Howeve
13e40 72 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 72 65 r, the number re
13e50 74 75 72 6e 65 64 20 64 6f 65 73 20 6e 6f 74 20 turned does not
13e60 69 6e 63 6c 75 64 65 20 63 68 61 6e 67 65 73 0a include changes.
13e70 2a 2a 20 63 61 75 73 65 64 20 62 79 20 73 75 62 ** caused by sub
13e80 74 72 69 67 67 65 72 73 20 73 69 6e 63 65 20 74 triggers since t
13e90 68 6f 73 65 20 68 61 76 65 20 74 68 65 69 72 20 hose have their
13ea0 6f 77 6e 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a own context..**.
13eb0 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 ** See also the
13ec0 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 [sqlite3_total_c
13ed0 68 61 6e 67 65 73 28 29 5d 20 69 6e 74 65 72 66 hanges()] interf
13ee0 61 63 65 20 61 6e 64 20 74 68 65 0a 2a 2a 20 5b ace and the.** [
13ef0 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 20 70 72 count_changes pr
13f00 61 67 6d 61 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 agma]..**.** Req
13f10 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
13f20 31 32 32 34 31 5d 20 5b 48 31 32 32 34 33 5d 0a 12241] [H12243].
13f30 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 65 70 61 72 **.** If a separ
13f40 61 74 65 20 74 68 72 65 61 64 20 6d 61 6b 65 73 ate thread makes
13f50 20 63 68 61 6e 67 65 73 20 6f 6e 20 74 68 65 20 changes on the
13f60 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f same database co
13f70 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 68 69 6c nnection.** whil
13f80 65 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 e [sqlite3_chang
13f90 65 73 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 es()] is running
13fa0 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 then the value
13fb0 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 75 returned.** is u
13fc0 6e 70 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 npredictable and
13fd0 20 6e 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c 2e not meaningful.
13fe0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
13ff0 6e 74 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 nt sqlite3_chang
14000 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f es(sqlite3*);../
14010 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 *.** CAPI3REF: T
14020 6f 74 61 6c 20 4e 75 6d 62 65 72 20 4f 66 20 52 otal Number Of R
14030 6f 77 73 20 4d 6f 64 69 66 69 65 64 20 7b 48 31 ows Modified {H1
14040 32 32 36 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a 2260} <S10600>.*
14050 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
14060 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e on returns the n
14070 75 6d 62 65 72 20 6f 66 20 72 6f 77 20 63 68 61 umber of row cha
14080 6e 67 65 73 20 63 61 75 73 65 64 20 62 79 20 5b nges caused by [
14090 49 4e 53 45 52 54 5d 2c 0a 2a 2a 20 5b 55 50 44 INSERT],.** [UPD
140a0 41 54 45 5d 20 6f 72 20 5b 44 45 4c 45 54 45 5d ATE] or [DELETE]
140b0 20 73 74 61 74 65 6d 65 6e 74 73 20 73 69 6e 63 statements sinc
140c0 65 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 e the [database
140d0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 77 61 73 20 connection] was
140e0 6f 70 65 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 63 opened..** The c
140f0 6f 75 6e 74 20 69 6e 63 6c 75 64 65 73 20 61 6c ount includes al
14100 6c 20 63 68 61 6e 67 65 73 20 66 72 6f 6d 20 61 l changes from a
14110 6c 6c 20 5b 43 52 45 41 54 45 20 54 52 49 47 47 ll [CREATE TRIGG
14120 45 52 20 7c 20 74 72 69 67 67 65 72 5d 20 0a 2a ER | trigger] .*
14130 2a 20 63 6f 6e 74 65 78 74 73 20 61 6e 64 20 63 * contexts and c
14140 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 5b hanges made by [
14150 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63 74 69 foreign key acti
14160 6f 6e 73 5d 2e 20 48 6f 77 65 76 65 72 2c 0a 2a ons]. However,.*
14170 2a 20 74 68 65 20 63 6f 75 6e 74 20 64 6f 65 73 * the count does
14180 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 68 61 not include cha
14190 6e 67 65 73 20 75 73 65 64 20 74 6f 20 69 6d 70 nges used to imp
141a0 6c 65 6d 65 6e 74 20 5b 52 45 50 4c 41 43 45 5d lement [REPLACE]
141b0 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 0a 2a 2a constraints,.**
141c0 20 64 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 6f 72 do rollbacks or
141d0 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e ABORT processin
141e0 67 2c 20 6f 72 20 5b 44 52 4f 50 20 54 41 42 4c g, or [DROP TABL
141f0 45 5d 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20 E] processing.
14200 54 68 65 0a 2a 2a 20 63 6f 75 6e 74 20 64 6f 65 The.** count doe
14210 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 72 6f s not include ro
14220 77 73 20 6f 66 20 76 69 65 77 73 20 74 68 61 74 ws of views that
14230 20 66 69 72 65 20 61 6e 20 5b 49 4e 53 54 45 41 fire an [INSTEA
14240 44 20 4f 46 20 74 72 69 67 67 65 72 5d 2c 0a 2a D OF trigger],.*
14250 2a 20 74 68 6f 75 67 68 20 69 66 20 74 68 65 20 * though if the
14260 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 INSTEAD OF trigg
14270 65 72 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 er makes changes
14280 20 6f 66 20 69 74 73 20 6f 77 6e 2c 20 74 68 6f of its own, tho
14290 73 65 20 63 68 61 6e 67 65 73 20 0a 2a 2a 20 61 se changes .** a
142a0 72 65 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a 20 54 re counted..** T
142b0 68 65 20 63 68 61 6e 67 65 73 20 61 72 65 20 63 he changes are c
142c0 6f 75 6e 74 65 64 20 61 73 20 73 6f 6f 6e 20 61 ounted as soon a
142d0 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 s the statement
142e0 74 68 61 74 20 6d 61 6b 65 73 20 74 68 65 6d 20 that makes them
142f0 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 64 20 is.** completed
14300 28 77 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d (when the statem
14310 65 6e 74 20 68 61 6e 64 6c 65 20 69 73 20 70 61 ent handle is pa
14320 73 73 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 ssed to [sqlite3
14330 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 _reset()] or.**
14340 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a [sqlite3_finaliz
14350 65 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 e()])..**.** See
14360 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 also the [sqlit
14370 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 6e e3_changes()] in
14380 74 65 72 66 61 63 65 20 61 6e 64 20 74 68 65 0a terface and the.
14390 2a 2a 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67 65 ** [count_change
143a0 73 20 70 72 61 67 6d 61 5d 2e 0a 2a 2a 0a 2a 2a s pragma]..**.**
143b0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
143c0 2a 20 5b 48 31 32 32 36 31 5d 20 5b 48 31 32 32 * [H12261] [H122
143d0 36 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 63].**.** If a s
143e0 65 70 61 72 61 74 65 20 74 68 72 65 61 64 20 6d eparate thread m
143f0 61 6b 65 73 20 63 68 61 6e 67 65 73 20 6f 6e 20 akes changes on
14400 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
14410 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 e connection.**
14420 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 74 while [sqlite3_t
14430 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 otal_changes()]
14440 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 is running then
14450 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 72 65 74 the value.** ret
14460 75 72 6e 65 64 20 69 73 20 75 6e 70 72 65 64 69 urned is unpredi
14470 63 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d ctable and not m
14480 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 eaningful..*/.SQ
14490 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
144a0 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 ite3_total_chang
144b0 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f es(sqlite3*);../
144c0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 *.** CAPI3REF: I
144d0 6e 74 65 72 72 75 70 74 20 41 20 4c 6f 6e 67 2d nterrupt A Long-
144e0 52 75 6e 6e 69 6e 67 20 51 75 65 72 79 20 7b 48 Running Query {H
144f0 31 32 32 37 30 7d 20 3c 53 33 30 35 30 30 3e 0a 12270} <S30500>.
14500 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
14510 69 6f 6e 20 63 61 75 73 65 73 20 61 6e 79 20 70 ion causes any p
14520 65 6e 64 69 6e 67 20 64 61 74 61 62 61 73 65 20 ending database
14530 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 61 62 6f operation to abo
14540 72 74 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e rt and.** return
14550 20 61 74 20 69 74 73 20 65 61 72 6c 69 65 73 74 at its earliest
14560 20 6f 70 70 6f 72 74 75 6e 69 74 79 2e 20 54 68 opportunity. Th
14570 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 79 is routine is ty
14580 70 69 63 61 6c 6c 79 0a 2a 2a 20 63 61 6c 6c 65 pically.** calle
14590 64 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f d in response to
145a0 20 61 20 75 73 65 72 20 61 63 74 69 6f 6e 20 73 a user action s
145b0 75 63 68 20 61 73 20 70 72 65 73 73 69 6e 67 20 uch as pressing
145c0 22 43 61 6e 63 65 6c 22 0a 2a 2a 20 6f 72 20 43 "Cancel".** or C
145d0 74 72 6c 2d 43 20 77 68 65 72 65 20 74 68 65 20 trl-C where the
145e0 75 73 65 72 20 77 61 6e 74 73 20 61 20 6c 6f 6e user wants a lon
145f0 67 20 71 75 65 72 79 20 6f 70 65 72 61 74 69 6f g query operatio
14600 6e 20 74 6f 20 68 61 6c 74 0a 2a 2a 20 69 6d 6d n to halt.** imm
14610 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 ediately..**.**
14620 49 74 20 69 73 20 73 61 66 65 20 74 6f 20 63 61 It is safe to ca
14630 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ll this routine
14640 66 72 6f 6d 20 61 20 74 68 72 65 61 64 20 64 69 from a thread di
14650 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 fferent from the
14660 0a 2a 2a 20 74 68 72 65 61 64 20 74 68 61 74 20 .** thread that
14670 69 73 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e is currently run
14680 6e 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 ning the databas
14690 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 e operation. Bu
146a0 74 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 73 t it.** is not s
146b0 61 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 afe to call this
146c0 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 61 20 routine with a
146d0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
146e0 74 69 6f 6e 5d 20 74 68 61 74 0a 2a 2a 20 69 73 tion] that.** is
146f0 20 63 6c 6f 73 65 64 20 6f 72 20 6d 69 67 68 74 closed or might
14700 20 63 6c 6f 73 65 20 62 65 66 6f 72 65 20 73 71 close before sq
14710 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 lite3_interrupt(
14720 29 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a ) returns..**.**
14730 20 49 66 20 61 6e 20 53 51 4c 20 6f 70 65 72 61 If an SQL opera
14740 74 69 6f 6e 20 69 73 20 76 65 72 79 20 6e 65 61 tion is very nea
14750 72 6c 79 20 66 69 6e 69 73 68 65 64 20 61 74 20 rly finished at
14760 74 68 65 20 74 69 6d 65 20 77 68 65 6e 0a 2a 2a the time when.**
14770 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
14780 70 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2c 20 pt() is called,
14790 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20 6e 6f then it might no
147a0 74 20 68 61 76 65 20 61 6e 20 6f 70 70 6f 72 74 t have an opport
147b0 75 6e 69 74 79 0a 2a 2a 20 74 6f 20 62 65 20 69 unity.** to be i
147c0 6e 74 65 72 72 75 70 74 65 64 20 61 6e 64 20 6d nterrupted and m
147d0 69 67 68 74 20 63 6f 6e 74 69 6e 75 65 20 74 6f ight continue to
147e0 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a completion..**.
147f0 2a 2a 20 41 6e 20 53 51 4c 20 6f 70 65 72 61 74 ** An SQL operat
14800 69 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 74 65 ion that is inte
14810 72 72 75 70 74 65 64 20 77 69 6c 6c 20 72 65 74 rrupted will ret
14820 75 72 6e 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 urn [SQLITE_INTE
14830 52 52 55 50 54 5d 2e 0a 2a 2a 20 49 66 20 74 68 RRUPT]..** If th
14840 65 20 69 6e 74 65 72 72 75 70 74 65 64 20 53 51 e interrupted SQ
14850 4c 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 L operation is a
14860 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 n INSERT, UPDATE
14870 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 74 , or DELETE.** t
14880 68 61 74 20 69 73 20 69 6e 73 69 64 65 20 61 6e hat is inside an
14890 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73 61 explicit transa
148a0 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 ction, then the
148b0 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 entire transacti
148c0 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 72 6f on.** will be ro
148d0 6c 6c 65 64 20 62 61 63 6b 20 61 75 74 6f 6d 61 lled back automa
148e0 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 tically..**.** T
148f0 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 he sqlite3_inter
14900 72 75 70 74 28 44 29 20 63 61 6c 6c 20 69 73 20 rupt(D) call is
14910 69 6e 20 65 66 66 65 63 74 20 75 6e 74 69 6c 20 in effect until
14920 61 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20 72 75 all currently ru
14930 6e 6e 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 nning.** SQL sta
14940 74 65 6d 65 6e 74 73 20 6f 6e 20 5b 64 61 74 61 tements on [data
14950 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
14960 20 44 20 63 6f 6d 70 6c 65 74 65 2e 20 20 41 6e D complete. An
14970 79 20 6e 65 77 20 53 51 4c 20 73 74 61 74 65 6d y new SQL statem
14980 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 ents.** that are
14990 20 73 74 61 72 74 65 64 20 61 66 74 65 72 20 74 started after t
149a0 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 he sqlite3_inter
149b0 72 75 70 74 28 29 20 63 61 6c 6c 20 61 6e 64 20 rupt() call and
149c0 62 65 66 6f 72 65 20 74 68 65 20 0a 2a 2a 20 72 before the .** r
149d0 75 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 unning statement
149e0 73 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61 s reaches zero a
149f0 72 65 20 69 6e 74 65 72 72 75 70 74 65 64 20 61 re interrupted a
14a00 73 20 69 66 20 74 68 65 79 20 68 61 64 20 62 65 s if they had be
14a10 65 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 70 72 en.** running pr
14a20 69 6f 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 ior to the sqlit
14a30 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 63 e3_interrupt() c
14a40 61 6c 6c 2e 20 20 4e 65 77 20 53 51 4c 20 73 74 all. New SQL st
14a50 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 atements.** that
14a60 20 61 72 65 20 73 74 61 72 74 65 64 20 61 66 74 are started aft
14a70 65 72 20 74 68 65 20 72 75 6e 6e 69 6e 67 20 73 er the running s
14a80 74 61 74 65 6d 65 6e 74 20 63 6f 75 6e 74 20 72 tatement count r
14a90 65 61 63 68 65 73 20 7a 65 72 6f 20 61 72 65 0a eaches zero are.
14aa0 2a 2a 20 6e 6f 74 20 65 66 66 65 63 74 65 64 20 ** not effected
14ab0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 by the sqlite3_i
14ac0 6e 74 65 72 72 75 70 74 28 29 2e 0a 2a 2a 20 41 nterrupt()..** A
14ad0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
14ae0 5f 69 6e 74 65 72 72 75 70 74 28 44 29 20 74 68 _interrupt(D) th
14af0 61 74 20 6f 63 63 75 72 73 20 77 68 65 6e 20 74 at occurs when t
14b00 68 65 72 65 20 61 72 65 20 6e 6f 20 72 75 6e 6e here are no runn
14b10 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 ing.** SQL state
14b20 6d 65 6e 74 73 20 69 73 20 61 20 6e 6f 2d 6f 70 ments is a no-op
14b30 20 61 6e 64 20 68 61 73 20 6e 6f 20 65 66 66 65 and has no effe
14b40 63 74 20 6f 6e 20 53 51 4c 20 73 74 61 74 65 6d ct on SQL statem
14b50 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 ents.** that are
14b60 20 73 74 61 72 74 65 64 20 61 66 74 65 72 20 74 started after t
14b70 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 he sqlite3_inter
14b80 72 75 70 74 28 29 20 63 61 6c 6c 20 72 65 74 75 rupt() call retu
14b90 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 rns..**.** Requi
14ba0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 rements:.** [H12
14bb0 32 37 31 5d 20 5b 48 31 32 32 37 32 5d 0a 2a 2a 271] [H12272].**
14bc0 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 .** If the datab
14bd0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 ase connection c
14be0 6c 6f 73 65 73 20 77 68 69 6c 65 20 5b 73 71 6c loses while [sql
14bf0 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 ite3_interrupt()
14c00 5d 0a 2a 2a 20 69 73 20 72 75 6e 6e 69 6e 67 20 ].** is running
14c10 74 68 65 6e 20 62 61 64 20 74 68 69 6e 67 73 20 then bad things
14c20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 68 61 70 70 will likely happ
14c30 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 en..*/.SQLITE_AP
14c40 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 69 I void sqlite3_i
14c50 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74 65 33 nterrupt(sqlite3
14c60 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 *);../*.** CAPI3
14c70 52 45 46 3a 20 44 65 74 65 72 6d 69 6e 65 20 49 REF: Determine I
14c80 66 20 41 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 f An SQL Stateme
14c90 6e 74 20 49 73 20 43 6f 6d 70 6c 65 74 65 20 7b nt Is Complete {
14ca0 48 31 30 35 31 30 7d 20 3c 53 37 30 32 30 30 3e H10510} <S70200>
14cb0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 .**.** These rou
14cc0 74 69 6e 65 73 20 61 72 65 20 75 73 65 66 75 6c tines are useful
14cd0 20 64 75 72 69 6e 67 20 63 6f 6d 6d 61 6e 64 2d during command-
14ce0 6c 69 6e 65 20 69 6e 70 75 74 20 74 6f 20 64 65 line input to de
14cf0 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 0a 2a termine if the.*
14d00 2a 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 * currently ente
14d10 72 65 64 20 74 65 78 74 20 73 65 65 6d 73 20 74 red text seems t
14d20 6f 20 66 6f 72 6d 20 61 20 63 6f 6d 70 6c 65 74 o form a complet
14d30 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 e SQL statement
14d40 6f 72 0a 2a 2a 20 69 66 20 61 64 64 69 74 69 6f or.** if additio
14d50 6e 61 6c 20 69 6e 70 75 74 20 69 73 20 6e 65 65 nal input is nee
14d60 64 65 64 20 62 65 66 6f 72 65 20 73 65 6e 64 69 ded before sendi
14d70 6e 67 20 74 68 65 20 74 65 78 74 20 69 6e 74 6f ng the text into
14d80 0a 2a 2a 20 53 51 4c 69 74 65 20 66 6f 72 20 70 .** SQLite for p
14d90 61 72 73 69 6e 67 2e 20 20 54 68 65 73 65 20 72 arsing. These r
14da0 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 31 outines return 1
14db0 20 69 66 20 74 68 65 20 69 6e 70 75 74 20 73 74 if the input st
14dc0 72 69 6e 67 0a 2a 2a 20 61 70 70 65 61 72 73 20 ring.** appears
14dd0 74 6f 20 62 65 20 61 20 63 6f 6d 70 6c 65 74 65 to be a complete
14de0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 SQL statement.
14df0 20 41 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 A statement is
14e00 6a 75 64 67 65 64 20 74 6f 20 62 65 0a 2a 2a 20 judged to be.**
14e10 63 6f 6d 70 6c 65 74 65 20 69 66 20 69 74 20 65 complete if it e
14e20 6e 64 73 20 77 69 74 68 20 61 20 73 65 6d 69 63 nds with a semic
14e30 6f 6c 6f 6e 20 74 6f 6b 65 6e 20 61 6e 64 20 69 olon token and i
14e40 73 20 6e 6f 74 20 61 20 70 72 65 66 69 78 20 6f s not a prefix o
14e50 66 20 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d f a.** well-form
14e60 65 64 20 43 52 45 41 54 45 20 54 52 49 47 47 45 ed CREATE TRIGGE
14e70 52 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 53 65 R statement. Se
14e80 6d 69 63 6f 6c 6f 6e 73 20 74 68 61 74 20 61 72 micolons that ar
14e90 65 20 65 6d 62 65 64 64 65 64 20 77 69 74 68 69 e embedded withi
14ea0 6e 0a 2a 2a 20 73 74 72 69 6e 67 20 6c 69 74 65 n.** string lite
14eb0 72 61 6c 73 20 6f 72 20 71 75 6f 74 65 64 20 69 rals or quoted i
14ec0 64 65 6e 74 69 66 69 65 72 20 6e 61 6d 65 73 20 dentifier names
14ed0 6f 72 20 63 6f 6d 6d 65 6e 74 73 20 61 72 65 20 or comments are
14ee0 6e 6f 74 0a 2a 2a 20 69 6e 64 65 70 65 6e 64 65 not.** independe
14ef0 6e 74 20 74 6f 6b 65 6e 73 20 28 74 68 65 79 20 nt tokens (they
14f00 61 72 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 are part of the
14f10 74 6f 6b 65 6e 20 69 6e 20 77 68 69 63 68 20 74 token in which t
14f20 68 65 79 20 61 72 65 0a 2a 2a 20 65 6d 62 65 64 hey are.** embed
14f30 64 65 64 29 20 61 6e 64 20 74 68 75 73 20 64 6f ded) and thus do
14f40 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20 61 20 not count as a
14f50 73 74 61 74 65 6d 65 6e 74 20 74 65 72 6d 69 6e statement termin
14f60 61 74 6f 72 2e 20 20 57 68 69 74 65 73 70 61 63 ator. Whitespac
14f70 65 0a 2a 2a 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 e.** and comment
14f80 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 74 68 s that follow th
14f90 65 20 66 69 6e 61 6c 20 73 65 6d 69 63 6f 6c 6f e final semicolo
14fa0 6e 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a n are ignored..*
14fb0 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
14fc0 6e 65 73 20 72 65 74 75 72 6e 20 30 20 69 66 20 nes return 0 if
14fd0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 the statement is
14fe0 20 69 6e 63 6f 6d 70 6c 65 74 65 2e 20 20 49 66 incomplete. If
14ff0 20 61 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c a.** memory all
15000 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 ocation fails, t
15010 68 65 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d hen SQLITE_NOMEM
15020 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
15030 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e .** These routin
15040 65 73 20 64 6f 20 6e 6f 74 20 70 61 72 73 65 20 es do not parse
15050 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
15060 74 73 20 74 68 75 73 0a 2a 2a 20 77 69 6c 6c 20 ts thus.** will
15070 6e 6f 74 20 64 65 74 65 63 74 20 73 79 6e 74 61 not detect synta
15080 63 74 69 63 61 6c 6c 79 20 69 6e 63 6f 72 72 65 ctically incorre
15090 63 74 20 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 ct SQL..**.** If
150a0 20 53 51 4c 69 74 65 20 68 61 73 20 6e 6f 74 20 SQLite has not
150b0 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 been initialized
150c0 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
150d0 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20 70 72 initialize()] pr
150e0 69 6f 72 20 0a 2a 2a 20 74 6f 20 69 6e 76 6f 6b ior .** to invok
150f0 69 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 ing sqlite3_comp
15100 6c 65 74 65 31 36 28 29 20 74 68 65 6e 20 73 71 lete16() then sq
15110 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
15120 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a () is invoked.**
15130 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 automatically b
15140 79 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 y sqlite3_comple
15150 74 65 31 36 28 29 2e 20 20 49 66 20 74 68 61 74 te16(). If that
15160 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
15170 66 61 69 6c 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 fails,.** then t
15180 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
15190 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f 6d from sqlite3_com
151a0 70 6c 65 74 65 31 36 28 29 20 77 69 6c 6c 20 62 plete16() will b
151b0 65 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 72 65 e non-zero.** re
151c0 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 gardless of whet
151d0 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 her or not the i
151e0 6e 70 75 74 20 53 51 4c 20 69 73 20 63 6f 6d 70 nput SQL is comp
151f0 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 lete..**.** Requ
15200 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 35 31 irements: [H1051
15210 31 5d 20 5b 48 31 30 35 31 32 5d 0a 2a 2a 0a 2a 1] [H10512].**.*
15220 2a 20 54 68 65 20 69 6e 70 75 74 20 74 6f 20 5b * The input to [
15230 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 sqlite3_complete
15240 28 29 5d 20 6d 75 73 74 20 62 65 20 61 20 7a 65 ()] must be a ze
15250 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a ro-terminated.**
15260 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a UTF-8 string..*
15270 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 74 *.** The input t
15280 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c o [sqlite3_compl
15290 65 74 65 31 36 28 29 5d 20 6d 75 73 74 20 62 65 ete16()] must be
152a0 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 a zero-terminat
152b0 65 64 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 ed.** UTF-16 str
152c0 69 6e 67 20 69 6e 20 6e 61 74 69 76 65 20 62 79 ing in native by
152d0 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 53 51 4c te order..*/.SQL
152e0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
152f0 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f 6e te3_complete(con
15300 73 74 20 63 68 61 72 20 2a 73 71 6c 29 3b 0a 53 st char *sql);.S
15310 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
15320 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 lite3_complete16
15330 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c (const void *sql
15340 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
15350 45 46 3a 20 52 65 67 69 73 74 65 72 20 41 20 43 EF: Register A C
15360 61 6c 6c 62 61 63 6b 20 54 6f 20 48 61 6e 64 6c allback To Handl
15370 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20 45 72 e SQLITE_BUSY Er
15380 72 6f 72 73 20 7b 48 31 32 33 31 30 7d 20 3c 53 rors {H12310} <S
15390 34 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 40400>.**.** Thi
153a0 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61 s routine sets a
153b0 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 callback functi
153c0 6f 6e 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 on that might be
153d0 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 invoked wheneve
153e0 72 0a 2a 2a 20 61 6e 20 61 74 74 65 6d 70 74 20 r.** an attempt
153f0 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 is made to open
15400 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 a database table
15410 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 that another th
15420 72 65 61 64 0a 2a 2a 20 6f 72 20 70 72 6f 63 65 read.** or proce
15430 73 73 20 68 61 73 20 6c 6f 63 6b 65 64 2e 0a 2a ss has locked..*
15440 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 73 79 *.** If the busy
15450 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 4c callback is NUL
15460 4c 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f L, then [SQLITE_
15470 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 BUSY] or [SQLITE
15480 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a _IOERR_BLOCKED].
15490 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 ** is returned i
154a0 6d 6d 65 64 69 61 74 65 6c 79 20 75 70 6f 6e 20 mmediately upon
154b0 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 74 68 65 encountering the
154c0 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 62 75 lock. If the bu
154d0 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 sy callback.** i
154e0 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e s not NULL, then
154f0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 77 69 the callback wi
15500 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 69 ll be invoked wi
15510 74 68 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 th two arguments
15520 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 ..**.** The firs
15530 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 t argument to th
15540 65 20 68 61 6e 64 6c 65 72 20 69 73 20 61 20 63 e handler is a c
15550 6f 70 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a opy of the void*
15560 20 70 6f 69 6e 74 65 72 20 77 68 69 63 68 0a 2a pointer which.*
15570 2a 20 69 73 20 74 68 65 20 74 68 69 72 64 20 61 * is the third a
15580 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 rgument to sqlit
15590 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 e3_busy_handler(
155a0 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 ). The second a
155b0 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 rgument to.** th
155c0 65 20 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 e handler callba
155d0 63 6b 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ck is the number
155e0 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 74 of times that t
155f0 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 he busy handler
15600 68 61 73 0a 2a 2a 20 62 65 65 6e 20 69 6e 76 6f has.** been invo
15610 6b 65 64 20 66 6f 72 20 74 68 69 73 20 6c 6f 63 ked for this loc
15620 6b 69 6e 67 20 65 76 65 6e 74 2e 20 20 49 66 20 king event. If
15630 74 68 65 0a 2a 2a 20 62 75 73 79 20 63 61 6c 6c the.** busy call
15640 62 61 63 6b 20 72 65 74 75 72 6e 73 20 30 2c 20 back returns 0,
15650 74 68 65 6e 20 6e 6f 20 61 64 64 69 74 69 6f 6e then no addition
15660 61 6c 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 al attempts are
15670 6d 61 64 65 20 74 6f 0a 2a 2a 20 61 63 63 65 73 made to.** acces
15680 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 s the database a
15690 6e 64 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d nd [SQLITE_BUSY]
156a0 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 or [SQLITE_IOER
156b0 52 5f 42 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 R_BLOCKED] is re
156c0 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 74 68 turned..** If th
156d0 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 e callback retur
156e0 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 ns non-zero, the
156f0 6e 20 61 6e 6f 74 68 65 72 20 61 74 74 65 6d 70 n another attemp
15700 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 t.** is made to
15710 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 open the databas
15720 65 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e e for reading an
15730 64 20 74 68 65 20 63 79 63 6c 65 20 72 65 70 65 d the cycle repe
15740 61 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ats..**.** The p
15750 72 65 73 65 6e 63 65 20 6f 66 20 61 20 62 75 73 resence of a bus
15760 79 20 68 61 6e 64 6c 65 72 20 64 6f 65 73 20 6e y handler does n
15770 6f 74 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 ot guarantee tha
15780 74 20 69 74 20 77 69 6c 6c 20 62 65 20 69 6e 76 t it will be inv
15790 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 oked.** when the
157a0 72 65 20 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 re is lock conte
157b0 6e 74 69 6f 6e 2e 20 49 66 20 53 51 4c 69 74 65 ntion. If SQLite
157c0 20 64 65 74 65 72 6d 69 6e 65 73 20 74 68 61 74 determines that
157d0 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 invoking the bu
157e0 73 79 0a 2a 2a 20 68 61 6e 64 6c 65 72 20 63 6f sy.** handler co
157f0 75 6c 64 20 72 65 73 75 6c 74 20 69 6e 20 61 20 uld result in a
15800 64 65 61 64 6c 6f 63 6b 2c 20 69 74 20 77 69 6c deadlock, it wil
15810 6c 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 72 l go ahead and r
15820 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 42 55 eturn [SQLITE_BU
15830 53 59 5d 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 SY].** or [SQLIT
15840 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d E_IOERR_BLOCKED]
15850 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f instead of invo
15860 6b 69 6e 67 20 74 68 65 20 62 75 73 79 20 68 61 king the busy ha
15870 6e 64 6c 65 72 2e 0a 2a 2a 20 43 6f 6e 73 69 64 ndler..** Consid
15880 65 72 20 61 20 73 63 65 6e 61 72 69 6f 20 77 68 er a scenario wh
15890 65 72 65 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 ere one process
158a0 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 61 is holding a rea
158b0 64 20 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a 20 69 d lock that.** i
158c0 74 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20 70 t is trying to p
158d0 72 6f 6d 6f 74 65 20 74 6f 20 61 20 72 65 73 65 romote to a rese
158e0 72 76 65 64 20 6c 6f 63 6b 20 61 6e 64 0a 2a 2a rved lock and.**
158f0 20 61 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 a second proces
15900 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 s is holding a r
15910 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 74 68 61 eserved lock tha
15920 74 20 69 74 20 69 73 20 74 72 79 69 6e 67 0a 2a t it is trying.*
15930 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 * to promote to
15940 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
15950 6b 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 72 k. The first pr
15960 6f 63 65 73 73 20 63 61 6e 6e 6f 74 20 70 72 6f ocess cannot pro
15970 63 65 65 64 0a 2a 2a 20 62 65 63 61 75 73 65 20 ceed.** because
15980 69 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 it is blocked by
15990 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 the second and
159a0 74 68 65 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 the second proce
159b0 73 73 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70 72 6f ss cannot.** pro
159c0 63 65 65 64 20 62 65 63 61 75 73 65 20 69 74 20 ceed because it
159d0 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 74 68 is blocked by th
159e0 65 20 66 69 72 73 74 2e 20 20 49 66 20 62 6f 74 e first. If bot
159f0 68 20 70 72 6f 63 65 73 73 65 73 0a 2a 2a 20 69 h processes.** i
15a00 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 nvoke the busy h
15a10 61 6e 64 6c 65 72 73 2c 20 6e 65 69 74 68 65 72 andlers, neither
15a20 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 20 70 will make any p
15a30 72 6f 67 72 65 73 73 2e 20 20 54 68 65 72 65 66 rogress. Theref
15a40 6f 72 65 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 72 ore,.** SQLite r
15a50 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 42 eturns [SQLITE_B
15a60 55 53 59 5d 20 66 6f 72 20 74 68 65 20 66 69 72 USY] for the fir
15a70 73 74 20 70 72 6f 63 65 73 73 2c 20 68 6f 70 69 st process, hopi
15a80 6e 67 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 ng that this.**
15a90 77 69 6c 6c 20 69 6e 64 75 63 65 20 74 68 65 20 will induce the
15aa0 66 69 72 73 74 20 70 72 6f 63 65 73 73 20 74 6f first process to
15ab0 20 72 65 6c 65 61 73 65 20 69 74 73 20 72 65 61 release its rea
15ac0 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c 6f 77 d lock and allow
15ad0 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 70 .** the second p
15ae0 72 6f 63 65 73 73 20 74 6f 20 70 72 6f 63 65 65 rocess to procee
15af0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 d..**.** The def
15b00 61 75 6c 74 20 62 75 73 79 20 63 61 6c 6c 62 61 ault busy callba
15b10 63 6b 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a ck is NULL..**.*
15b20 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 42 55 * The [SQLITE_BU
15b30 53 59 5d 20 65 72 72 6f 72 20 69 73 20 63 6f 6e SY] error is con
15b40 76 65 72 74 65 64 20 74 6f 20 5b 53 51 4c 49 54 verted to [SQLIT
15b50 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d E_IOERR_BLOCKED]
15b60 0a 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20 .** when SQLite
15b70 69 73 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 is in the middle
15b80 20 6f 66 20 61 20 6c 61 72 67 65 20 74 72 61 6e of a large tran
15b90 73 61 63 74 69 6f 6e 20 77 68 65 72 65 20 61 6c saction where al
15ba0 6c 20 74 68 65 0a 2a 2a 20 63 68 61 6e 67 65 73 l the.** changes
15bb0 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 69 6e will not fit in
15bc0 74 6f 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 to the in-memory
15bd0 20 63 61 63 68 65 2e 20 20 53 51 4c 69 74 65 20 cache. SQLite
15be0 77 69 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64 79 20 will.** already
15bf0 68 6f 6c 64 20 61 20 52 45 53 45 52 56 45 44 20 hold a RESERVED
15c00 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
15c10 62 61 73 65 20 66 69 6c 65 2c 20 62 75 74 20 69 base file, but i
15c20 74 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 70 72 t needs.** to pr
15c30 6f 6d 6f 74 65 20 74 68 69 73 20 6c 6f 63 6b 20 omote this lock
15c40 74 6f 20 45 58 43 4c 55 53 49 56 45 20 73 6f 20 to EXCLUSIVE so
15c50 74 68 61 74 20 69 74 20 63 61 6e 20 73 70 69 6c that it can spil
15c60 6c 20 63 61 63 68 65 0a 2a 2a 20 70 61 67 65 73 l cache.** pages
15c70 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 into the databa
15c80 73 65 20 66 69 6c 65 20 77 69 74 68 6f 75 74 20 se file without
15c90 68 61 72 6d 20 74 6f 20 63 6f 6e 63 75 72 72 65 harm to concurre
15ca0 6e 74 0a 2a 2a 20 72 65 61 64 65 72 73 2e 20 20 nt.** readers.
15cb0 49 66 20 69 74 20 69 73 20 75 6e 61 62 6c 65 20 If it is unable
15cc0 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68 65 20 6c to promote the l
15cd0 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 69 6e ock, then the in
15ce0 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 63 61 63 68 65 -memory.** cache
15cf0 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 69 6e will be left in
15d00 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 an inconsistent
15d10 20 73 74 61 74 65 20 61 6e 64 20 73 6f 20 74 68 state and so th
15d20 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 e error.** code
15d30 69 73 20 70 72 6f 6d 6f 74 65 64 20 66 72 6f 6d is promoted from
15d40 20 74 68 65 20 72 65 6c 61 74 69 76 65 6c 79 20 the relatively
15d50 62 65 6e 69 67 6e 20 5b 53 51 4c 49 54 45 5f 42 benign [SQLITE_B
15d60 55 53 59 5d 20 74 6f 0a 2a 2a 20 74 68 65 20 6d USY] to.** the m
15d70 6f 72 65 20 73 65 76 65 72 65 20 5b 53 51 4c 49 ore severe [SQLI
15d80 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 TE_IOERR_BLOCKED
15d90 5d 2e 20 20 54 68 69 73 20 65 72 72 6f 72 20 63 ]. This error c
15da0 6f 64 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a 2a 2a ode promotion.**
15db0 20 66 6f 72 63 65 73 20 61 6e 20 61 75 74 6f 6d forces an autom
15dc0 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 atic rollback of
15dd0 20 74 68 65 20 63 68 61 6e 67 65 73 2e 20 20 53 the changes. S
15de0 65 65 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 ee the.** <a hre
15df0 66 3d 22 2f 63 76 73 74 72 61 63 2f 77 69 6b 69 f="/cvstrac/wiki
15e00 3f 70 3d 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c ?p=CorruptionFol
15e10 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f 72 22 lowingBusyError"
15e20 3e 0a 2a 2a 20 43 6f 72 72 75 70 74 69 6f 6e 46 >.** CorruptionF
15e30 6f 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f ollowingBusyErro
15e40 72 3c 2f 61 3e 20 77 69 6b 69 20 70 61 67 65 20 r</a> wiki page
15e50 66 6f 72 20 61 20 64 69 73 63 75 73 73 69 6f 6e for a discussion
15e60 20 6f 66 20 77 68 79 0a 2a 2a 20 74 68 69 73 20 of why.** this
15e70 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 0a 2a 2a is important..**
15e80 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e .** There can on
15e90 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 ly be a single b
15ea0 75 73 79 20 68 61 6e 64 6c 65 72 20 64 65 66 69 usy handler defi
15eb0 6e 65 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 ned for each.**
15ec0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
15ed0 74 69 6f 6e 5d 2e 20 20 53 65 74 74 69 6e 67 20 tion]. Setting
15ee0 61 20 6e 65 77 20 62 75 73 79 20 68 61 6e 64 6c a new busy handl
15ef0 65 72 20 63 6c 65 61 72 73 20 61 6e 79 0a 2a 2a er clears any.**
15f00 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20 previously set
15f10 68 61 6e 64 6c 65 72 2e 20 20 4e 6f 74 65 20 74 handler. Note t
15f20 68 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c hat calling [sql
15f30 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 ite3_busy_timeou
15f40 74 28 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61 6c 73 t()].** will als
15f50 6f 20 73 65 74 20 6f 72 20 63 6c 65 61 72 20 74 o set or clear t
15f60 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e he busy handler.
15f70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 75 73 79 20 .**.** The busy
15f80 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 20 callback should
15f90 6e 6f 74 20 74 61 6b 65 20 61 6e 79 20 61 63 74 not take any act
15fa0 69 6f 6e 73 20 77 68 69 63 68 20 6d 6f 64 69 66 ions which modif
15fb0 79 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 y the.** databas
15fc0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 e connection tha
15fd0 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 t invoked the bu
15fe0 73 79 20 68 61 6e 64 6c 65 72 2e 20 20 41 6e 79 sy handler. Any
15ff0 20 73 75 63 68 20 61 63 74 69 6f 6e 73 0a 2a 2a such actions.**
16000 20 72 65 73 75 6c 74 20 69 6e 20 75 6e 64 65 66 result in undef
16010 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a ined behavior..*
16020 2a 20 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e * .** Requiremen
16030 74 73 3a 0a 2a 2a 20 5b 48 31 32 33 31 31 5d 20 ts:.** [H12311]
16040 5b 48 31 32 33 31 32 5d 20 5b 48 31 32 33 31 34 [H12312] [H12314
16050 5d 20 5b 48 31 32 33 31 36 5d 20 5b 48 31 32 33 ] [H12316] [H123
16060 31 38 5d 0a 2a 2a 0a 2a 2a 20 41 20 62 75 73 79 18].**.** A busy
16070 20 68 61 6e 64 6c 65 72 20 6d 75 73 74 20 6e 6f handler must no
16080 74 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 61 t close the data
16090 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a base connection.
160a0 2a 2a 20 6f 72 20 5b 70 72 65 70 61 72 65 64 20 ** or [prepared
160b0 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 statement] that
160c0 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 73 79 invoked the busy
160d0 20 68 61 6e 64 6c 65 72 2e 0a 2a 2f 0a 53 51 4c handler..*/.SQL
160e0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
160f0 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 te3_busy_handler
16100 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a (sqlite3*, int(*
16110 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 20 76 6f )(void*,int), vo
16120 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 id*);../*.** CAP
16130 49 33 52 45 46 3a 20 53 65 74 20 41 20 42 75 73 I3REF: Set A Bus
16140 79 20 54 69 6d 65 6f 75 74 20 7b 48 31 32 33 34 y Timeout {H1234
16150 30 7d 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 0} <S40410>.**.*
16160 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 * This routine s
16170 65 74 73 20 61 20 5b 73 71 6c 69 74 65 33 5f 62 ets a [sqlite3_b
16180 75 73 79 5f 68 61 6e 64 6c 65 72 20 7c 20 62 75 usy_handler | bu
16190 73 79 20 68 61 6e 64 6c 65 72 5d 20 74 68 61 74 sy handler] that
161a0 20 73 6c 65 65 70 73 0a 2a 2a 20 66 6f 72 20 61 sleeps.** for a
161b0 20 73 70 65 63 69 66 69 65 64 20 61 6d 6f 75 6e specified amoun
161c0 74 20 6f 66 20 74 69 6d 65 20 77 68 65 6e 20 61 t of time when a
161d0 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 table is locked
161e0 2e 20 20 54 68 65 20 68 61 6e 64 6c 65 72 0a 2a . The handler.*
161f0 2a 20 77 69 6c 6c 20 73 6c 65 65 70 20 6d 75 6c * will sleep mul
16200 74 69 70 6c 65 20 74 69 6d 65 73 20 75 6e 74 69 tiple times unti
16210 6c 20 61 74 20 6c 65 61 73 74 20 22 6d 73 22 20 l at least "ms"
16220 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 milliseconds of
16230 73 6c 65 65 70 69 6e 67 0a 2a 2a 20 68 61 76 65 sleeping.** have
16240 20 61 63 63 75 6d 75 6c 61 74 65 64 2e 20 7b 48 accumulated. {H
16250 31 32 33 34 33 7d 20 41 66 74 65 72 20 22 6d 73 12343} After "ms
16260 22 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f " milliseconds o
16270 66 20 73 6c 65 65 70 69 6e 67 2c 0a 2a 2a 20 74 f sleeping,.** t
16280 68 65 20 68 61 6e 64 6c 65 72 20 72 65 74 75 72 he handler retur
16290 6e 73 20 30 20 77 68 69 63 68 20 63 61 75 73 65 ns 0 which cause
162a0 73 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 s [sqlite3_step(
162b0 29 5d 20 74 6f 20 72 65 74 75 72 6e 0a 2a 2a 20 )] to return.**
162c0 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 [SQLITE_BUSY] or
162d0 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 [SQLITE_IOERR_B
162e0 4c 4f 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a 2a 20 43 LOCKED]..**.** C
162f0 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 alling this rout
16300 69 6e 65 20 77 69 74 68 20 61 6e 20 61 72 67 75 ine with an argu
16310 6d 65 6e 74 20 6c 65 73 73 20 74 68 61 6e 20 6f ment less than o
16320 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 0a r equal to zero.
16330 2a 2a 20 74 75 72 6e 73 20 6f 66 66 20 61 6c 6c ** turns off all
16340 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2e 0a busy handlers..
16350 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 **.** There can
16360 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 only be a single
16370 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 6f busy handler fo
16380 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a r a particular.*
16390 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e * [database conn
163a0 65 63 74 69 6f 6e 5d 20 61 6e 79 20 61 6e 79 20 ection] any any
163b0 67 69 76 65 6e 20 6d 6f 6d 65 6e 74 2e 20 20 49 given moment. I
163c0 66 20 61 6e 6f 74 68 65 72 20 62 75 73 79 20 68 f another busy h
163d0 61 6e 64 6c 65 72 0a 2a 2a 20 77 61 73 20 64 65 andler.** was de
163e0 66 69 6e 65 64 20 20 28 75 73 69 6e 67 20 5b 73 fined (using [s
163f0 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 qlite3_busy_hand
16400 6c 65 72 28 29 5d 29 20 70 72 69 6f 72 20 74 6f ler()]) prior to
16410 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 calling.** this
16420 20 72 6f 75 74 69 6e 65 2c 20 74 68 61 74 20 6f routine, that o
16430 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c 65 ther busy handle
16440 72 20 69 73 20 63 6c 65 61 72 65 64 2e 0a 2a 2a r is cleared..**
16450 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
16460 3a 0a 2a 2a 20 5b 48 31 32 33 34 31 5d 20 5b 48 :.** [H12341] [H
16470 31 32 33 34 33 5d 20 5b 48 31 32 33 34 34 5d 0a 12343] [H12344].
16480 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
16490 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 t sqlite3_busy_t
164a0 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33 2a 2c imeout(sqlite3*,
164b0 20 69 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a 2a 2a int ms);../*.**
164c0 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 76 65 CAPI3REF: Conve
164d0 6e 69 65 6e 63 65 20 52 6f 75 74 69 6e 65 73 20 nience Routines
164e0 46 6f 72 20 52 75 6e 6e 69 6e 67 20 51 75 65 72 For Running Quer
164f0 69 65 73 20 7b 48 31 32 33 37 30 7d 20 3c 53 31 ies {H12370} <S1
16500 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 44 65 66 69 0000>.**.** Defi
16510 6e 69 74 69 6f 6e 3a 20 41 20 3c 62 3e 72 65 73 nition: A <b>res
16520 75 6c 74 20 74 61 62 6c 65 3c 2f 62 3e 20 69 73 ult table</b> is
16530 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74 72 memory data str
16540 75 63 74 75 72 65 20 63 72 65 61 74 65 64 20 62 ucture created b
16550 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 y the.** [sqlite
16560 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 20 69 3_get_table()] i
16570 6e 74 65 72 66 61 63 65 2e 20 20 41 20 72 65 73 nterface. A res
16580 75 6c 74 20 74 61 62 6c 65 20 72 65 63 6f 72 64 ult table record
16590 73 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 s the.** complet
165a0 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 20 e query results
165b0 66 72 6f 6d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 from one or more
165c0 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 queries..**.**
165d0 54 68 65 20 74 61 62 6c 65 20 63 6f 6e 63 65 70 The table concep
165e0 74 75 61 6c 6c 79 20 68 61 73 20 61 20 6e 75 6d tually has a num
165f0 62 65 72 20 6f 66 20 72 6f 77 73 20 61 6e 64 20 ber of rows and
16600 63 6f 6c 75 6d 6e 73 2e 20 20 42 75 74 0a 2a 2a columns. But.**
16610 20 74 68 65 73 65 20 6e 75 6d 62 65 72 73 20 61 these numbers a
16620 72 65 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 re not part of t
16630 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 he result table
16640 69 74 73 65 6c 66 2e 20 20 54 68 65 73 65 0a 2a itself. These.*
16650 2a 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f 62 * numbers are ob
16660 74 61 69 6e 65 64 20 73 65 70 61 72 61 74 65 6c tained separatel
16670 79 2e 20 20 4c 65 74 20 4e 20 62 65 20 74 68 65 y. Let N be the
16680 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 0a number of rows.
16690 2a 2a 20 61 6e 64 20 4d 20 62 65 20 74 68 65 20 ** and M be the
166a0 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e number of column
166b0 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 6c s..**.** A resul
166c0 74 20 74 61 62 6c 65 20 69 73 20 61 6e 20 61 72 t table is an ar
166d0 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 ray of pointers
166e0 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 to zero-terminat
166f0 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73 ed UTF-8 strings
16700 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 28 ..** There are (
16710 4e 2b 31 29 2a 4d 20 65 6c 65 6d 65 6e 74 73 20 N+1)*M elements
16720 69 6e 20 74 68 65 20 61 72 72 61 79 2e 20 20 54 in the array. T
16730 68 65 20 66 69 72 73 74 20 4d 20 70 6f 69 6e 74 he first M point
16740 65 72 73 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20 ers point.** to
16750 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 zero-terminated
16760 73 74 72 69 6e 67 73 20 74 68 61 74 20 20 63 6f strings that co
16770 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 73 20 ntain the names
16780 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 2e 0a of the columns..
16790 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 ** The remaining
167a0 20 65 6e 74 72 69 65 73 20 61 6c 6c 20 70 6f 69 entries all poi
167b0 6e 74 20 74 6f 20 71 75 65 72 79 20 72 65 73 75 nt to query resu
167c0 6c 74 73 2e 20 20 4e 55 4c 4c 20 76 61 6c 75 65 lts. NULL value
167d0 73 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 4e s result.** in N
167e0 55 4c 4c 20 70 6f 69 6e 74 65 72 73 2e 20 20 41 ULL pointers. A
167f0 6c 6c 20 6f 74 68 65 72 20 76 61 6c 75 65 73 20 ll other values
16800 61 72 65 20 69 6e 20 74 68 65 69 72 20 55 54 46 are in their UTF
16810 2d 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 -8 zero-terminat
16820 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 72 65 70 ed.** string rep
16830 72 65 73 65 6e 74 61 74 69 6f 6e 20 61 73 20 72 resentation as r
16840 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 eturned by [sqli
16850 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 te3_column_text(
16860 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 )]..**.** A resu
16870 6c 74 20 74 61 62 6c 65 20 6d 69 67 68 74 20 63 lt table might c
16880 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 6f 72 onsist of one or
16890 20 6d 6f 72 65 20 6d 65 6d 6f 72 79 20 61 6c 6c more memory all
168a0 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2a 20 49 74 20 ocations..** It
168b0 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 70 is not safe to p
168c0 61 73 73 20 61 20 72 65 73 75 6c 74 20 74 61 62 ass a result tab
168d0 6c 65 20 64 69 72 65 63 74 6c 79 20 74 6f 20 5b le directly to [
168e0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e sqlite3_free()].
168f0 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 61 62 .** A result tab
16900 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 64 65 61 le should be dea
16910 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 5b llocated using [
16920 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 sqlite3_free_tab
16930 6c 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 le()]..**.** As
16940 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 74 68 an example of th
16950 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 66 e result table f
16960 6f 72 6d 61 74 2c 20 73 75 70 70 6f 73 65 20 61 ormat, suppose a
16970 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a 2a 2a query result.**
16980 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a is as follows:.
16990 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 **.** <blockquot
169a0 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 20 e><pre>.**
169b0 20 20 4e 61 6d 65 20 20 20 20 20 20 20 20 7c 20 Name |
169c0 41 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 2d 2d Age.** --
169d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
169e0 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 20 20 -----.**
169f0 41 6c 69 63 65 20 20 20 20 20 20 20 7c 20 34 33 Alice | 43
16a00 0a 2a 2a 20 20 20 20 20 20 20 20 42 6f 62 20 20 .** Bob
16a10 20 20 20 20 20 20 20 7c 20 32 38 0a 2a 2a 20 20 | 28.**
16a20 20 20 20 20 20 20 43 69 6e 64 79 20 20 20 20 20 Cindy
16a30 20 20 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72 65 3e | 21.** </pre>
16a40 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a </blockquote>.**
16a50 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 77 .** There are tw
16a60 6f 20 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32 29 20 o column (M==2)
16a70 61 6e 64 20 74 68 72 65 65 20 72 6f 77 73 20 28 and three rows (
16a80 4e 3d 3d 33 29 2e 20 20 54 68 75 73 20 74 68 65 N==3). Thus the
16a90 0a 2a 2a 20 72 65 73 75 6c 74 20 74 61 62 6c 65 .** result table
16aa0 20 68 61 73 20 38 20 65 6e 74 72 69 65 73 2e 20 has 8 entries.
16ab0 20 53 75 70 70 6f 73 65 20 74 68 65 20 72 65 73 Suppose the res
16ac0 75 6c 74 20 74 61 62 6c 65 20 69 73 20 73 74 6f ult table is sto
16ad0 72 65 64 0a 2a 2a 20 69 6e 20 61 6e 20 61 72 72 red.** in an arr
16ae0 61 79 20 6e 61 6d 65 73 20 61 7a 52 65 73 75 6c ay names azResul
16af0 74 2e 20 20 54 68 65 6e 20 61 7a 52 65 73 75 6c t. Then azResul
16b00 74 20 68 6f 6c 64 73 20 74 68 69 73 20 63 6f 6e t holds this con
16b10 74 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f tent:.**.** <blo
16b20 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a ckquote><pre>.**
16b30 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 azResult
16b40 26 23 39 31 3b 30 5d 20 3d 20 22 4e 61 6d 65 22 [0] = "Name"
16b50 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 ;.** azRe
16b60 73 75 6c 74 26 23 39 31 3b 31 5d 20 3d 20 22 41 sult[1] = "A
16b70 67 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 ge";.** a
16b80 7a 52 65 73 75 6c 74 26 23 39 31 3b 32 5d 20 3d zResult[2] =
16b90 20 22 41 6c 69 63 65 22 3b 0a 2a 2a 20 20 20 20 "Alice";.**
16ba0 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 azResult[
16bb0 3b 33 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a 20 20 ;3] = "43";.**
16bc0 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 azResult&#
16bd0 39 31 3b 34 5d 20 3d 20 22 42 6f 62 22 3b 0a 2a 91;4] = "Bob";.*
16be0 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c * azResul
16bf0 74 26 23 39 31 3b 35 5d 20 3d 20 22 32 38 22 3b t[5] = "28";
16c00 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 .** azRes
16c10 75 6c 74 26 23 39 31 3b 36 5d 20 3d 20 22 43 69 ult[6] = "Ci
16c20 6e 64 79 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 ndy";.**
16c30 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 37 5d 20 azResult[7]
16c40 3d 20 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70 72 65 = "21";.** </pre
16c50 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a ></blockquote>.*
16c60 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
16c70 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 66 75 6e _get_table() fun
16c80 63 74 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20 ction evaluates
16c90 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 73 one or more.** s
16ca0 65 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72 61 74 emicolon-separat
16cb0 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 ed SQL statement
16cc0 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d 74 65 s in the zero-te
16cd0 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 0a 2a rminated UTF-8.*
16ce0 2a 20 73 74 72 69 6e 67 20 6f 66 20 69 74 73 20 * string of its
16cf0 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 2nd parameter.
16d00 49 74 20 72 65 74 75 72 6e 73 20 61 20 72 65 73 It returns a res
16d10 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 74 68 65 ult table to the
16d20 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 67 69 76 65 .** pointer give
16d30 6e 20 69 6e 20 69 74 73 20 33 72 64 20 70 61 72 n in its 3rd par
16d40 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 66 ameter..**.** Af
16d50 74 65 72 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 ter the calling
16d60 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 66 69 6e function has fin
16d70 69 73 68 65 64 20 75 73 69 6e 67 20 74 68 65 20 ished using the
16d80 72 65 73 75 6c 74 2c 20 69 74 20 73 68 6f 75 6c result, it shoul
16d90 64 0a 2a 2a 20 70 61 73 73 20 74 68 65 20 70 6f d.** pass the po
16da0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 65 73 inter to the res
16db0 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 73 71 6c ult table to sql
16dc0 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 ite3_free_table(
16dd0 29 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a ) in order to.**
16de0 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d 65 6d release the mem
16df0 6f 72 79 20 74 68 61 74 20 77 61 73 20 6d 61 6c ory that was mal
16e00 6c 6f 63 65 64 2e 20 20 42 65 63 61 75 73 65 20 loced. Because
16e10 6f 66 20 74 68 65 20 77 61 79 20 74 68 65 0a 2a of the way the.*
16e20 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f * [sqlite3_mallo
16e30 63 28 29 5d 20 68 61 70 70 65 6e 73 20 77 69 74 c()] happens wit
16e40 68 69 6e 20 73 71 6c 69 74 65 33 5f 67 65 74 5f hin sqlite3_get_
16e50 74 61 62 6c 65 28 29 2c 20 74 68 65 20 63 61 6c table(), the cal
16e60 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e ling.** function
16e70 20 6d 75 73 74 20 6e 6f 74 20 74 72 79 20 74 6f must not try to
16e80 20 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33 5f 66 call [sqlite3_f
16e90 72 65 65 28 29 5d 20 64 69 72 65 63 74 6c 79 2e ree()] directly.
16ea0 20 20 4f 6e 6c 79 0a 2a 2a 20 5b 73 71 6c 69 74 Only.** [sqlit
16eb0 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 5d e3_free_table()]
16ec0 20 69 73 20 61 62 6c 65 20 74 6f 20 72 65 6c 65 is able to rele
16ed0 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 70 ase the memory p
16ee0 72 6f 70 65 72 6c 79 20 61 6e 64 20 73 61 66 65 roperly and safe
16ef0 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 ly..**.** The sq
16f00 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 lite3_get_table(
16f10 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 69 ) interface is i
16f20 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 mplemented as a
16f30 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 0a 2a wrapper around.*
16f40 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 * [sqlite3_exec(
16f50 29 5d 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 )]. The sqlite3
16f60 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 72 6f 75 _get_table() rou
16f70 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 68 61 tine does not ha
16f80 76 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f 20 ve access.** to
16f90 61 6e 79 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 any internal dat
16fa0 61 20 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 a structures of
16fb0 53 51 4c 69 74 65 2e 20 20 49 74 20 75 73 65 73 SQLite. It uses
16fc0 20 6f 6e 6c 79 20 74 68 65 20 70 75 62 6c 69 63 only the public
16fd0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 64 65 .** interface de
16fe0 66 69 6e 65 64 20 68 65 72 65 2e 20 20 41 73 20 fined here. As
16ff0 61 20 63 6f 6e 73 65 71 75 65 6e 63 65 2c 20 65 a consequence, e
17000 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63 75 72 rrors that occur
17010 20 69 6e 20 74 68 65 0a 2a 2a 20 77 72 61 70 70 in the.** wrapp
17020 65 72 20 6c 61 79 65 72 20 6f 75 74 73 69 64 65 er layer outside
17030 20 6f 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c of the internal
17040 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 [sqlite3_exec()
17050 5d 20 63 61 6c 6c 20 61 72 65 20 6e 6f 74 0a 2a ] call are not.*
17060 2a 20 72 65 66 6c 65 63 74 65 64 20 69 6e 20 73 * reflected in s
17070 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 ubsequent calls
17080 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 to [sqlite3_errc
17090 6f 64 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 ode()] or [sqlit
170a0 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a e3_errmsg()]..**
170b0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
170c0 3a 0a 2a 2a 20 5b 48 31 32 33 37 31 5d 20 5b 48 :.** [H12371] [H
170d0 31 32 33 37 33 5d 20 5b 48 31 32 33 37 34 5d 20 12373] [H12374]
170e0 5b 48 31 32 33 37 36 5d 20 5b 48 31 32 33 37 39 [H12376] [H12379
170f0 5d 20 5b 48 31 32 33 38 32 5d 0a 2a 2f 0a 53 51 ] [H12382].*/.SQ
17100 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
17110 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 0a ite3_get_table(.
17120 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
17130 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 /* An op
17140 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 en database */.
17150 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 const char *zSq
17160 6c 2c 20 20 20 20 20 2f 2a 20 53 51 4c 20 74 6f l, /* SQL to
17170 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 2a 2f be evaluated */
17180 0a 20 20 63 68 61 72 20 2a 2a 2a 70 61 7a 52 65 . char ***pazRe
17190 73 75 6c 74 2c 20 20 20 20 2f 2a 20 52 65 73 75 sult, /* Resu
171a0 6c 74 73 20 6f 66 20 74 68 65 20 71 75 65 72 79 lts of the query
171b0 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 6f 77 */. int *pnRow
171c0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e , /* N
171d0 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 umber of result
171e0 72 6f 77 73 20 77 72 69 74 74 65 6e 20 68 65 72 rows written her
171f0 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f e */. int *pnCo
17200 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20 lumn, /*
17210 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 Number of result
17220 20 63 6f 6c 75 6d 6e 73 20 77 72 69 74 74 65 6e columns written
17230 20 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 here */. char
17240 2a 2a 70 7a 45 72 72 6d 73 67 20 20 20 20 20 20 **pzErrmsg
17250 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72 /* Error msg wr
17260 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 3b itten here */.);
17270 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
17280 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 sqlite3_free_ta
17290 62 6c 65 28 63 68 61 72 20 2a 2a 72 65 73 75 6c ble(char **resul
172a0 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 t);../*.** CAPI3
172b0 52 45 46 3a 20 46 6f 72 6d 61 74 74 65 64 20 53 REF: Formatted S
172c0 74 72 69 6e 67 20 50 72 69 6e 74 69 6e 67 20 46 tring Printing F
172d0 75 6e 63 74 69 6f 6e 73 20 7b 48 31 37 34 30 30 unctions {H17400
172e0 7d 20 3c 53 37 30 30 30 30 3e 3c 53 32 30 30 30 } <S70000><S2000
172f0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 0>.**.** These r
17300 6f 75 74 69 6e 65 73 20 61 72 65 20 77 6f 72 6b outines are work
17310 2d 61 6c 69 6b 65 73 20 6f 66 20 74 68 65 20 22 -alikes of the "
17320 70 72 69 6e 74 66 28 29 22 20 66 61 6d 69 6c 79 printf()" family
17330 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a of functions.**
17340 20 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 from the standa
17350 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a rd C library..**
17360 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
17370 6d 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73 71 mprintf() and sq
17380 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 29 lite3_vmprintf()
17390 20 72 6f 75 74 69 6e 65 73 20 77 72 69 74 65 20 routines write
173a0 74 68 65 69 72 0a 2a 2a 20 72 65 73 75 6c 74 73 their.** results
173b0 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 into memory obt
173c0 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 ained from [sqli
173d0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a 2a te3_malloc()]..*
173e0 2a 20 54 68 65 20 73 74 72 69 6e 67 73 20 72 65 * The strings re
173f0 74 75 72 6e 65 64 20 62 79 20 74 68 65 73 65 20 turned by these
17400 74 77 6f 20 72 6f 75 74 69 6e 65 73 20 73 68 6f two routines sho
17410 75 6c 64 20 62 65 0a 2a 2a 20 72 65 6c 65 61 73 uld be.** releas
17420 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 66 ed by [sqlite3_f
17430 72 65 65 28 29 5d 2e 20 20 42 6f 74 68 20 72 6f ree()]. Both ro
17440 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 61 0a utines return a.
17450 2a 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 ** NULL pointer
17460 69 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c if [sqlite3_mall
17470 6f 63 28 29 5d 20 69 73 20 75 6e 61 62 6c 65 20 oc()] is unable
17480 74 6f 20 61 6c 6c 6f 63 61 74 65 20 65 6e 6f 75 to allocate enou
17490 67 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 74 6f 20 gh.** memory to
174a0 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 69 hold the resulti
174b0 6e 67 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a ng string..**.**
174c0 20 49 6e 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 In sqlite3_snpr
174d0 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 20 69 intf() routine i
174e0 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 22 73 6e s similar to "sn
174f0 70 72 69 6e 74 66 28 29 22 20 66 72 6f 6d 0a 2a printf()" from.*
17500 2a 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 * the standard C
17510 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 72 library. The r
17520 65 73 75 6c 74 20 69 73 20 77 72 69 74 74 65 6e esult is written
17530 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 62 75 66 into the.** buf
17540 66 65 72 20 73 75 70 70 6c 69 65 64 20 61 73 20 fer supplied as
17550 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d the second param
17560 65 74 65 72 20 77 68 6f 73 65 20 73 69 7a 65 20 eter whose size
17570 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20 74 is given by.** t
17580 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 he first paramet
17590 65 72 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68 er. Note that th
175a0 65 20 6f 72 64 65 72 20 6f 66 20 74 68 65 0a 2a e order of the.*
175b0 2a 20 66 69 72 73 74 20 74 77 6f 20 70 61 72 61 * first two para
175c0 6d 65 74 65 72 73 20 69 73 20 72 65 76 65 72 73 meters is revers
175d0 65 64 20 66 72 6f 6d 20 73 6e 70 72 69 6e 74 66 ed from snprintf
175e0 28 29 2e 20 20 54 68 69 73 20 69 73 20 61 6e 0a (). This is an.
175f0 2a 2a 20 68 69 73 74 6f 72 69 63 61 6c 20 61 63 ** historical ac
17600 63 69 64 65 6e 74 20 74 68 61 74 20 63 61 6e 6e cident that cann
17610 6f 74 20 62 65 20 66 69 78 65 64 20 77 69 74 68 ot be fixed with
17620 6f 75 74 20 62 72 65 61 6b 69 6e 67 0a 2a 2a 20 out breaking.**
17630 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 backwards compat
17640 69 62 69 6c 69 74 79 2e 20 20 4e 6f 74 65 20 61 ibility. Note a
17650 6c 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 lso that sqlite3
17660 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 72 _snprintf().** r
17670 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
17680 20 74 6f 20 69 74 73 20 62 75 66 66 65 72 20 69 to its buffer i
17690 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6e 75 nstead of the nu
176a0 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72 61 mber of.** chara
176b0 63 74 65 72 73 20 61 63 74 75 61 6c 6c 79 20 77 cters actually w
176c0 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
176d0 62 75 66 66 65 72 2e 20 20 57 65 20 61 64 6d 69 buffer. We admi
176e0 74 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6e 75 t that.** the nu
176f0 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 mber of characte
17700 72 73 20 77 72 69 74 74 65 6e 20 77 6f 75 6c 64 rs written would
17710 20 62 65 20 61 20 6d 6f 72 65 20 75 73 65 66 75 be a more usefu
17720 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 l return.** valu
17730 65 20 62 75 74 20 77 65 20 63 61 6e 6e 6f 74 20 e but we cannot
17740 63 68 61 6e 67 65 20 74 68 65 20 69 6d 70 6c 65 change the imple
17750 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c mentation of sql
17760 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a ite3_snprintf().
17770 2a 2a 20 6e 6f 77 20 77 69 74 68 6f 75 74 20 62 ** now without b
17780 72 65 61 6b 69 6e 67 20 63 6f 6d 70 61 74 69 62 reaking compatib
17790 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 ility..**.** As
177a0 6c 6f 6e 67 20 61 73 20 74 68 65 20 62 75 66 66 long as the buff
177b0 65 72 20 73 69 7a 65 20 69 73 20 67 72 65 61 74 er size is great
177c0 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 73 71 er than zero, sq
177d0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 lite3_snprintf()
177e0 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 73 20 74 .** guarantees t
177f0 68 61 74 20 74 68 65 20 62 75 66 66 65 72 20 69 hat the buffer i
17800 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74 65 s always zero-te
17810 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65 20 66 rminated. The f
17820 69 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 irst.** paramete
17830 72 20 22 6e 22 20 69 73 20 74 68 65 20 74 6f 74 r "n" is the tot
17840 61 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 al size of the b
17850 75 66 66 65 72 2c 20 69 6e 63 6c 75 64 69 6e 67 uffer, including
17860 20 73 70 61 63 65 20 66 6f 72 0a 2a 2a 20 74 68 space for.** th
17870 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f e zero terminato
17880 72 2e 20 20 53 6f 20 74 68 65 20 6c 6f 6e 67 65 r. So the longe
17890 73 74 20 73 74 72 69 6e 67 20 74 68 61 74 20 63 st string that c
178a0 61 6e 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 an be completely
178b0 0a 2a 2a 20 77 72 69 74 74 65 6e 20 77 69 6c 6c .** written will
178c0 20 62 65 20 6e 2d 31 20 63 68 61 72 61 63 74 65 be n-1 characte
178d0 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 rs..**.** These
178e0 72 6f 75 74 69 6e 65 73 20 61 6c 6c 20 69 6d 70 routines all imp
178f0 6c 65 6d 65 6e 74 20 73 6f 6d 65 20 61 64 64 69 lement some addi
17900 74 69 6f 6e 61 6c 20 66 6f 72 6d 61 74 74 69 6e tional formattin
17910 67 0a 2a 2a 20 6f 70 74 69 6f 6e 73 20 74 68 61 g.** options tha
17920 74 20 61 72 65 20 75 73 65 66 75 6c 20 66 6f 72 t are useful for
17930 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 53 51 constructing SQ
17940 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a L statements..**
17950 20 41 6c 6c 20 6f 66 20 74 68 65 20 75 73 75 61 All of the usua
17960 6c 20 70 72 69 6e 74 66 28 29 20 66 6f 72 6d 61 l printf() forma
17970 74 74 69 6e 67 20 6f 70 74 69 6f 6e 73 20 61 70 tting options ap
17980 70 6c 79 2e 20 20 49 6e 20 61 64 64 69 74 69 6f ply. In additio
17990 6e 2c 20 74 68 65 72 65 0a 2a 2a 20 69 73 20 61 n, there.** is a
179a0 72 65 20 22 25 71 22 2c 20 22 25 51 22 2c 20 61 re "%q", "%Q", a
179b0 6e 64 20 22 25 7a 22 20 6f 70 74 69 6f 6e 73 2e nd "%z" options.
179c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 25 71 20 6f 70 .**.** The %q op
179d0 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 tion works like
179e0 25 73 20 69 6e 20 74 68 61 74 20 69 74 20 73 75 %s in that it su
179f0 62 73 74 69 74 75 74 65 73 20 61 20 6e 75 6c 6c bstitutes a null
17a00 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 73 -terminated.** s
17a10 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 61 tring from the a
17a20 72 67 75 6d 65 6e 74 20 6c 69 73 74 2e 20 20 42 rgument list. B
17a30 75 74 20 25 71 20 61 6c 73 6f 20 64 6f 75 62 6c ut %q also doubl
17a40 65 73 20 65 76 65 72 79 20 27 5c 27 27 20 63 68 es every '\'' ch
17a50 61 72 61 63 74 65 72 2e 0a 2a 2a 20 25 71 20 69 aracter..** %q i
17a60 73 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75 s designed for u
17a70 73 65 20 69 6e 73 69 64 65 20 61 20 73 74 72 69 se inside a stri
17a80 6e 67 20 6c 69 74 65 72 61 6c 2e 20 20 42 79 20 ng literal. By
17a90 64 6f 75 62 6c 69 6e 67 20 65 61 63 68 20 27 5c doubling each '\
17aa0 27 27 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 20 ''.** character
17ab0 69 74 20 65 73 63 61 70 65 73 20 74 68 61 74 20 it escapes that
17ac0 63 68 61 72 61 63 74 65 72 20 61 6e 64 20 61 6c character and al
17ad0 6c 6f 77 73 20 69 74 20 74 6f 20 62 65 20 69 6e lows it to be in
17ae0 73 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 serted into.** t
17af0 68 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a he string..**.**
17b00 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 73 For example, as
17b10 73 75 6d 65 20 74 68 65 20 73 74 72 69 6e 67 20 sume the string
17b20 76 61 72 69 61 62 6c 65 20 7a 54 65 78 74 20 63 variable zText c
17b30 6f 6e 74 61 69 6e 73 20 74 65 78 74 20 61 73 20 ontains text as
17b40 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c follows:.**.** <
17b50 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e blockquote><pre>
17b60 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 54 65 78 74 .** char *zText
17b70 20 3d 20 22 49 74 27 73 20 61 20 68 61 70 70 79 = "It's a happy
17b80 20 64 61 79 21 22 3b 0a 2a 2a 20 3c 2f 70 72 65 day!";.** </pre
17b90 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a ></blockquote>.*
17ba0 2a 0a 2a 2a 20 4f 6e 65 20 63 61 6e 20 75 73 65 *.** One can use
17bb0 20 74 68 69 73 20 74 65 78 74 20 69 6e 20 61 6e this text in an
17bc0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 SQL statement a
17bd0 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
17be0 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 <blockquote><pr
17bf0 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53 51 e>.** char *zSQ
17c00 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 L = sqlite3_mpri
17c10 6e 74 66 28 22 49 4e 53 45 52 54 20 49 4e 54 4f ntf("INSERT INTO
17c20 20 74 61 62 6c 65 20 56 41 4c 55 45 53 28 27 25 table VALUES('%
17c30 71 27 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a 2a q')", zText);.**
17c40 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 sqlite3_exec(d
17c50 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20 30 b, zSQL, 0, 0, 0
17c60 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 );.** sqlite3_f
17c70 72 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c 2f ree(zSQL);.** </
17c80 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 pre></blockquote
17c90 3e 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65 20 >.**.** Because
17ca0 74 68 65 20 25 71 20 66 6f 72 6d 61 74 20 73 74 the %q format st
17cb0 72 69 6e 67 20 69 73 20 75 73 65 64 2c 20 74 68 ring is used, th
17cc0 65 20 27 5c 27 27 20 63 68 61 72 61 63 74 65 72 e '\'' character
17cd0 20 69 6e 20 7a 54 65 78 74 0a 2a 2a 20 69 73 20 in zText.** is
17ce0 65 73 63 61 70 65 64 20 61 6e 64 20 74 68 65 20 escaped and the
17cf0 53 51 4c 20 67 65 6e 65 72 61 74 65 64 20 69 73 SQL generated is
17d00 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a as follows:.**.
17d10 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c ** <blockquote><
17d20 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 54 20 pre>.** INSERT
17d30 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41 4c 55 INTO table1 VALU
17d40 45 53 28 27 49 74 27 27 73 20 61 20 68 61 70 70 ES('It''s a happ
17d50 79 20 64 61 79 21 27 29 0a 2a 2a 20 3c 2f 70 72 y day!').** </pr
17d60 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a e></blockquote>.
17d70 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 6f **.** This is co
17d80 72 72 65 63 74 2e 20 20 48 61 64 20 77 65 20 75 rrect. Had we u
17d90 73 65 64 20 25 73 20 69 6e 73 74 65 61 64 20 6f sed %s instead o
17da0 66 20 25 71 2c 20 74 68 65 20 67 65 6e 65 72 61 f %q, the genera
17db0 74 65 64 20 53 51 4c 0a 2a 2a 20 77 6f 75 6c 64 ted SQL.** would
17dc0 20 68 61 76 65 20 6c 6f 6f 6b 65 64 20 6c 69 6b have looked lik
17dd0 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 e this:.**.** <b
17de0 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a lockquote><pre>.
17df0 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 ** INSERT INTO
17e00 74 61 62 6c 65 31 20 56 41 4c 55 45 53 28 27 49 table1 VALUES('I
17e10 74 27 73 20 61 20 68 61 70 70 79 20 64 61 79 21 t's a happy day!
17e20 27 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 ');.** </pre></b
17e30 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a lockquote>.**.**
17e40 20 54 68 69 73 20 73 65 63 6f 6e 64 20 65 78 61 This second exa
17e50 6d 70 6c 65 20 69 73 20 61 6e 20 53 51 4c 20 73 mple is an SQL s
17e60 79 6e 74 61 78 20 65 72 72 6f 72 2e 20 20 41 73 yntax error. As
17e70 20 61 20 67 65 6e 65 72 61 6c 20 72 75 6c 65 20 a general rule
17e80 79 6f 75 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 6c you should.** al
17e90 77 61 79 73 20 75 73 65 20 25 71 20 69 6e 73 74 ways use %q inst
17ea0 65 61 64 20 6f 66 20 25 73 20 77 68 65 6e 20 69 ead of %s when i
17eb0 6e 73 65 72 74 69 6e 67 20 74 65 78 74 20 69 6e nserting text in
17ec0 74 6f 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 to a string lite
17ed0 72 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 25 ral..**.** The %
17ee0 51 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20 6c Q option works l
17ef0 69 6b 65 20 25 71 20 65 78 63 65 70 74 20 69 74 ike %q except it
17f00 20 61 6c 73 6f 20 61 64 64 73 20 73 69 6e 67 6c also adds singl
17f10 65 20 71 75 6f 74 65 73 20 61 72 6f 75 6e 64 0a e quotes around.
17f20 2a 2a 20 74 68 65 20 6f 75 74 73 69 64 65 20 6f ** the outside o
17f30 66 20 74 68 65 20 74 6f 74 61 6c 20 73 74 72 69 f the total stri
17f40 6e 67 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 6c ng. Additionall
17f50 79 2c 20 69 66 20 74 68 65 20 70 61 72 61 6d 65 y, if the parame
17f60 74 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72 ter in the.** ar
17f70 67 75 6d 65 6e 74 20 6c 69 73 74 20 69 73 20 61 gument list is a
17f80 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 25 NULL pointer, %
17f90 51 20 73 75 62 73 74 69 74 75 74 65 73 20 74 68 Q substitutes th
17fa0 65 20 74 65 78 74 20 22 4e 55 4c 4c 22 20 28 77 e text "NULL" (w
17fb0 69 74 68 6f 75 74 0a 2a 2a 20 73 69 6e 67 6c 65 ithout.** single
17fc0 20 71 75 6f 74 65 73 29 20 69 6e 20 70 6c 61 63 quotes) in plac
17fd0 65 20 6f 66 20 74 68 65 20 25 51 20 6f 70 74 69 e of the %Q opti
17fe0 6f 6e 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 61 on. So, for exa
17ff0 6d 70 6c 65 2c 20 6f 6e 65 20 63 6f 75 6c 64 20 mple, one could
18000 73 61 79 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 say:.**.** <bloc
18010 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 kquote><pre>.**
18020 20 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71 char *zSQL = sq
18030 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 49 lite3_mprintf("I
18040 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 NSERT INTO table
18050 20 56 41 4c 55 45 53 28 25 51 29 22 2c 20 7a 54 VALUES(%Q)", zT
18060 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 ext);.** sqlite
18070 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c 2c 3_exec(db, zSQL,
18080 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 73 0, 0, 0);.** s
18090 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51 4c qlite3_free(zSQL
180a0 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c );.** </pre></bl
180b0 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 ockquote>.**.**
180c0 54 68 65 20 63 6f 64 65 20 61 62 6f 76 65 20 77 The code above w
180d0 69 6c 6c 20 72 65 6e 64 65 72 20 61 20 63 6f 72 ill render a cor
180e0 72 65 63 74 20 53 51 4c 20 73 74 61 74 65 6d 65 rect SQL stateme
180f0 6e 74 20 69 6e 20 74 68 65 20 7a 53 51 4c 0a 2a nt in the zSQL.*
18100 2a 20 76 61 72 69 61 62 6c 65 20 65 76 65 6e 20 * variable even
18110 69 66 20 74 68 65 20 7a 54 65 78 74 20 76 61 72 if the zText var
18120 69 61 62 6c 65 20 69 73 20 61 20 4e 55 4c 4c 20 iable is a NULL
18130 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 pointer..**.** T
18140 68 65 20 22 25 7a 22 20 66 6f 72 6d 61 74 74 69 he "%z" formatti
18150 6e 67 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20 ng option works
18160 65 78 61 63 74 6c 79 20 6c 69 6b 65 20 22 25 73 exactly like "%s
18170 22 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 61 64 " with the.** ad
18180 64 69 74 69 6f 6e 20 74 68 61 74 20 61 66 74 65 dition that afte
18190 72 20 74 68 65 20 73 74 72 69 6e 67 20 68 61 73 r the string has
181a0 20 62 65 65 6e 20 72 65 61 64 20 61 6e 64 20 63 been read and c
181b0 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 opied into.** th
181c0 65 20 72 65 73 75 6c 74 2c 20 5b 73 71 6c 69 74 e result, [sqlit
181d0 65 33 5f 66 72 65 65 28 29 5d 20 69 73 20 63 61 e3_free()] is ca
181e0 6c 6c 65 64 20 6f 6e 20 74 68 65 20 69 6e 70 75 lled on the inpu
181f0 74 20 73 74 72 69 6e 67 2e 20 7b 45 4e 44 7d 0a t string. {END}.
18200 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
18210 74 73 3a 0a 2a 2a 20 5b 48 31 37 34 30 33 5d 20 ts:.** [H17403]
18220 5b 48 31 37 34 30 36 5d 20 5b 48 31 37 34 30 37 [H17406] [H17407
18230 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
18240 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d 70 char *sqlite3_mp
18250 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 rintf(const char
18260 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 41 *,...);.SQLITE_A
18270 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 PI char *sqlite3
18280 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 _vmprintf(const
18290 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b char*, va_list);
182a0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 .SQLITE_API char
182b0 20 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e *sqlite3_snprin
182c0 74 66 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f 6e tf(int,char*,con
182d0 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a st char*, ...);.
182e0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
182f0 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 Memory Allocati
18300 6f 6e 20 53 75 62 73 79 73 74 65 6d 20 7b 48 31 on Subsystem {H1
18310 37 33 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 7300} <S20000>.*
18320 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 *.** The SQLite
18330 63 6f 72 65 20 20 75 73 65 73 20 74 68 65 73 65 core uses these
18340 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73 20 three routines
18350 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 6f for all of its o
18360 77 6e 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 6d wn.** internal m
18370 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
18380 20 6e 65 65 64 73 2e 20 22 43 6f 72 65 22 20 69 needs. "Core" i
18390 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 n the previous s
183a0 65 6e 74 65 6e 63 65 0a 2a 2a 20 64 6f 65 73 20 entence.** does
183b0 6e 6f 74 20 69 6e 63 6c 75 64 65 20 6f 70 65 72 not include oper
183c0 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 73 70 65 ating-system spe
183d0 63 69 66 69 63 20 56 46 53 20 69 6d 70 6c 65 6d cific VFS implem
183e0 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 0a 2a entation. The.*
183f0 2a 20 57 69 6e 64 6f 77 73 20 56 46 53 20 75 73 * Windows VFS us
18400 65 73 20 6e 61 74 69 76 65 20 6d 61 6c 6c 6f 63 es native malloc
18410 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 66 6f () and free() fo
18420 72 20 73 6f 6d 65 20 6f 70 65 72 61 74 69 6f 6e r some operation
18430 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c s..**.** The sql
18440 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 6f ite3_malloc() ro
18450 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 utine returns a
18460 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f pointer to a blo
18470 63 6b 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 20 ck.** of memory
18480 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73 at least N bytes
18490 20 69 6e 20 6c 65 6e 67 74 68 2c 20 77 68 65 72 in length, wher
184a0 65 20 4e 20 69 73 20 74 68 65 20 70 61 72 61 6d e N is the param
184b0 65 74 65 72 2e 0a 2a 2a 20 49 66 20 73 71 6c 69 eter..** If sqli
184c0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73 20 te3_malloc() is
184d0 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 6e unable to obtain
184e0 20 73 75 66 66 69 63 69 65 6e 74 20 66 72 65 65 sufficient free
184f0 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 69 74 20 72 .** memory, it r
18500 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f eturns a NULL po
18510 69 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 70 inter. If the p
18520 61 72 61 6d 65 74 65 72 20 4e 20 74 6f 0a 2a 2a arameter N to.**
18530 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
18540 29 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67 ) is zero or neg
18550 61 74 69 76 65 20 74 68 65 6e 20 73 71 6c 69 74 ative then sqlit
18560 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 65 74 75 e3_malloc() retu
18570 72 6e 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f rns.** a NULL po
18580 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c inter..**.** Cal
18590 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 65 ling sqlite3_fre
185a0 65 28 29 20 77 69 74 68 20 61 20 70 6f 69 6e 74 e() with a point
185b0 65 72 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 er previously re
185c0 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 6c turned.** by sql
185d0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 ite3_malloc() or
185e0 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 sqlite3_realloc
185f0 28 29 20 72 65 6c 65 61 73 65 73 20 74 68 61 74 () releases that
18600 20 6d 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20 74 68 memory so.** th
18610 61 74 20 69 74 20 6d 69 67 68 74 20 62 65 20 72 at it might be r
18620 65 75 73 65 64 2e 20 20 54 68 65 20 73 71 6c 69 eused. The sqli
18630 74 65 33 5f 66 72 65 65 28 29 20 72 6f 75 74 69 te3_free() routi
18640 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 ne is.** a no-op
18650 20 69 66 20 69 73 20 63 61 6c 6c 65 64 20 77 69 if is called wi
18660 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 th a NULL pointe
18670 72 2e 20 20 50 61 73 73 69 6e 67 20 61 20 4e 55 r. Passing a NU
18680 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f LL pointer.** to
18690 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 sqlite3_free()
186a0 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 41 66 is harmless. Af
186b0 74 65 72 20 62 65 69 6e 67 20 66 72 65 65 64 2c ter being freed,
186c0 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f 75 6c memory.** shoul
186d0 64 20 6e 65 69 74 68 65 72 20 62 65 20 72 65 61 d neither be rea
186e0 64 20 6e 6f 72 20 77 72 69 74 74 65 6e 2e 20 20 d nor written.
186f0 45 76 65 6e 20 72 65 61 64 69 6e 67 20 70 72 65 Even reading pre
18700 76 69 6f 75 73 6c 79 20 66 72 65 65 64 0a 2a 2a viously freed.**
18710 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 72 65 memory might re
18720 73 75 6c 74 20 69 6e 20 61 20 73 65 67 6d 65 6e sult in a segmen
18730 74 61 74 69 6f 6e 20 66 61 75 6c 74 20 6f 72 20 tation fault or
18740 6f 74 68 65 72 20 73 65 76 65 72 65 20 65 72 72 other severe err
18750 6f 72 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63 6f or..** Memory co
18760 72 72 75 70 74 69 6f 6e 2c 20 61 20 73 65 67 6d rruption, a segm
18770 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 2c 20 entation fault,
18780 6f 72 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 or other severe
18790 65 72 72 6f 72 0a 2a 2a 20 6d 69 67 68 74 20 72 error.** might r
187a0 65 73 75 6c 74 20 69 66 20 73 71 6c 69 74 65 33 esult if sqlite3
187b0 5f 66 72 65 65 28 29 20 69 73 20 63 61 6c 6c 65 _free() is calle
187c0 64 20 77 69 74 68 20 61 20 6e 6f 6e 2d 4e 55 4c d with a non-NUL
187d0 4c 20 70 6f 69 6e 74 65 72 20 74 68 61 74 0a 2a L pointer that.*
187e0 2a 20 77 61 73 20 6e 6f 74 20 6f 62 74 61 69 6e * was not obtain
187f0 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f ed from sqlite3_
18800 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 malloc() or sqli
18810 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a te3_realloc()..*
18820 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
18830 5f 72 65 61 6c 6c 6f 63 28 29 20 69 6e 74 65 72 _realloc() inter
18840 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f face attempts to
18850 20 72 65 73 69 7a 65 20 61 0a 2a 2a 20 70 72 69 resize a.** pri
18860 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 or memory alloca
18870 74 69 6f 6e 20 74 6f 20 62 65 20 61 74 20 6c 65 tion to be at le
18880 61 73 74 20 4e 20 62 79 74 65 73 2c 20 77 68 65 ast N bytes, whe
18890 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20 73 re N is the.** s
188a0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e econd parameter.
188b0 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c The memory all
188c0 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 72 65 ocation to be re
188d0 73 69 7a 65 64 20 69 73 20 74 68 65 20 66 69 72 sized is the fir
188e0 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 2e st.** parameter.
188f0 20 20 49 66 20 74 68 65 20 66 69 72 73 74 20 70 If the first p
18900 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 arameter to sqli
18910 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a te3_realloc().**
18920 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 is a NULL point
18930 65 72 20 74 68 65 6e 20 69 74 73 20 62 65 68 61 er then its beha
18940 76 69 6f 72 20 69 73 20 69 64 65 6e 74 69 63 61 vior is identica
18950 6c 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 l to calling.**
18960 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 4e sqlite3_malloc(N
18970 29 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 ) where N is the
18980 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
18990 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 r to sqlite3_rea
189a0 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 74 68 lloc()..** If th
189b0 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
189c0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 er to sqlite3_re
189d0 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f 20 alloc() is zero
189e0 6f 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 74 or.** negative t
189f0 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 hen the behavior
18a00 20 69 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 is exactly the
18a10 73 61 6d 65 20 61 73 20 63 61 6c 6c 69 6e 67 0a same as calling.
18a20 2a 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 ** sqlite3_free(
18a30 50 29 20 77 68 65 72 65 20 50 20 69 73 20 74 68 P) where P is th
18a40 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
18a50 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 r to sqlite3_rea
18a60 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 73 71 6c 69 74 lloc()..** sqlit
18a70 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 74 e3_realloc() ret
18a80 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 urns a pointer t
18a90 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 o a memory alloc
18aa0 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 74 20 6c ation.** of at l
18ab0 65 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e 20 east N bytes in
18ac0 73 69 7a 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20 size or NULL if
18ad0 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f 72 sufficient memor
18ae0 79 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c 65 y is unavailable
18af0 2e 0a 2a 2a 20 49 66 20 4d 20 69 73 20 74 68 65 ..** If M is the
18b00 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 72 69 size of the pri
18b10 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 74 or allocation, t
18b20 68 65 6e 20 6d 69 6e 28 4e 2c 4d 29 20 62 79 74 hen min(N,M) byt
18b30 65 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 72 69 es.** of the pri
18b40 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 72 or allocation ar
18b50 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 e copied into th
18b60 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 62 e beginning of b
18b70 75 66 66 65 72 20 72 65 74 75 72 6e 65 64 0a 2a uffer returned.*
18b80 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 72 65 61 * by sqlite3_rea
18b90 6c 6c 6f 63 28 29 20 61 6e 64 20 74 68 65 20 70 lloc() and the p
18ba0 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 rior allocation
18bb0 69 73 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66 20 is freed..** If
18bc0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
18bd0 29 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2c 20 ) returns NULL,
18be0 74 68 65 6e 20 74 68 65 20 70 72 69 6f 72 20 61 then the prior a
18bf0 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 llocation.** is
18c00 6e 6f 74 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a not freed..**.**
18c10 20 54 68 65 20 6d 65 6d 6f 72 79 20 72 65 74 75 The memory retu
18c20 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f rned by sqlite3_
18c30 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 73 71 6c malloc() and sql
18c40 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a ite3_realloc().*
18c50 2a 20 69 73 20 61 6c 77 61 79 73 20 61 6c 69 67 * is always alig
18c60 6e 65 64 20 74 6f 20 61 74 20 6c 65 61 73 74 20 ned to at least
18c70 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e 64 61 an 8 byte bounda
18c80 72 79 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 ry. {END}.**.**
18c90 54 68 65 20 64 65 66 61 75 6c 74 20 69 6d 70 6c The default impl
18ca0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
18cb0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
18cc0 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 75 73 ion subsystem us
18cd0 65 73 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 es.** the malloc
18ce0 28 29 2c 20 72 65 61 6c 6c 6f 63 28 29 20 61 6e (), realloc() an
18cf0 64 20 66 72 65 65 28 29 20 70 72 6f 76 69 64 65 d free() provide
18d00 64 20 62 79 20 74 68 65 20 73 74 61 6e 64 61 72 d by the standar
18d10 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 20 d C library..**
18d20 7b 48 31 37 33 38 32 7d 20 48 6f 77 65 76 65 72 {H17382} However
18d30 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 63 , if SQLite is c
18d40 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 ompiled with the
18d50 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 .** SQLITE_MEMOR
18d60 59 5f 53 49 5a 45 3d 3c 69 3e 4e 4e 4e 3c 2f 69 Y_SIZE=<i>NNN</i
18d70 3e 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f 72 > C preprocessor
18d80 20 6d 61 63 72 6f 20 28 77 68 65 72 65 20 3c 69 macro (where <i
18d90 3e 4e 4e 4e 3c 2f 69 3e 0a 2a 2a 20 69 73 20 61 >NNN</i>.** is a
18da0 6e 20 69 6e 74 65 67 65 72 29 2c 20 74 68 65 6e n integer), then
18db0 20 53 51 4c 69 74 65 20 63 72 65 61 74 65 20 61 SQLite create a
18dc0 20 73 74 61 74 69 63 20 61 72 72 61 79 20 6f 66 static array of
18dd0 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 3c 69 3e at least.** <i>
18de0 4e 4e 4e 3c 2f 69 3e 20 62 79 74 65 73 20 69 6e NNN</i> bytes in
18df0 20 73 69 7a 65 20 61 6e 64 20 75 73 65 73 20 74 size and uses t
18e00 68 61 74 20 61 72 72 61 79 20 66 6f 72 20 61 6c hat array for al
18e10 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 63 l of its dynamic
18e20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 .** memory alloc
18e30 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 7b 45 4e ation needs. {EN
18e40 44 7d 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 6d D} Additional m
18e50 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 emory allocator
18e60 6f 70 74 69 6f 6e 73 0a 2a 2a 20 6d 61 79 20 62 options.** may b
18e70 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 e added in futur
18e80 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a e releases..**.*
18e90 2a 20 49 6e 20 53 51 4c 69 74 65 20 76 65 72 73 * In SQLite vers
18ea0 69 6f 6e 20 33 2e 35 2e 30 20 61 6e 64 20 33 2e ion 3.5.0 and 3.
18eb0 35 2e 31 2c 20 69 74 20 77 61 73 20 70 6f 73 73 5.1, it was poss
18ec0 69 62 6c 65 20 74 6f 20 64 65 66 69 6e 65 0a 2a ible to define.*
18ed0 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49 * the SQLITE_OMI
18ee0 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43 41 54 T_MEMORY_ALLOCAT
18ef0 49 4f 4e 20 77 68 69 63 68 20 77 6f 75 6c 64 20 ION which would
18f00 63 61 75 73 65 20 74 68 65 20 62 75 69 6c 74 2d cause the built-
18f10 69 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 in.** implementa
18f20 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 72 6f tion of these ro
18f30 75 74 69 6e 65 73 20 74 6f 20 62 65 20 6f 6d 69 utines to be omi
18f40 74 74 65 64 2e 20 20 54 68 61 74 20 63 61 70 61 tted. That capa
18f50 62 69 6c 69 74 79 0a 2a 2a 20 69 73 20 6e 6f 20 bility.** is no
18f60 6c 6f 6e 67 65 72 20 70 72 6f 76 69 64 65 64 2e longer provided.
18f70 20 20 4f 6e 6c 79 20 62 75 69 6c 74 2d 69 6e 20 Only built-in
18f80 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
18f90 73 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a 2a s can be used..*
18fa0 2a 0a 2a 2a 20 54 68 65 20 57 69 6e 64 6f 77 73 *.** The Windows
18fb0 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 6c 61 OS interface la
18fc0 79 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 74 68 65 yer calls.** the
18fd0 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 system malloc()
18fe0 20 61 6e 64 20 66 72 65 65 28 29 20 64 69 72 65 and free() dire
18ff0 63 74 6c 79 20 77 68 65 6e 20 63 6f 6e 76 65 72 ctly when conver
19000 74 69 6e 67 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 ting.** filename
19010 73 20 62 65 74 77 65 65 6e 20 74 68 65 20 55 54 s between the UT
19020 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 F-8 encoding use
19030 64 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 61 d by SQLite.** a
19040 6e 64 20 77 68 61 74 65 76 65 72 20 66 69 6c 65 nd whatever file
19050 6e 61 6d 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 name encoding is
19060 20 75 73 65 64 20 62 79 20 74 68 65 20 70 61 72 used by the par
19070 74 69 63 75 6c 61 72 20 57 69 6e 64 6f 77 73 0a ticular Windows.
19080 2a 2a 20 69 6e 73 74 61 6c 6c 61 74 69 6f 6e 2e ** installation.
19090 20 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 Memory allocat
190a0 69 6f 6e 20 65 72 72 6f 72 73 20 61 72 65 20 64 ion errors are d
190b0 65 74 65 63 74 65 64 2c 20 62 75 74 0a 2a 2a 20 etected, but.**
190c0 74 68 65 79 20 61 72 65 20 72 65 70 6f 72 74 65 they are reporte
190d0 64 20 62 61 63 6b 20 61 73 20 5b 53 51 4c 49 54 d back as [SQLIT
190e0 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 6f 72 0a 2a E_CANTOPEN] or.*
190f0 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5d * [SQLITE_IOERR]
19100 20 72 61 74 68 65 72 20 74 68 61 6e 20 5b 53 51 rather than [SQ
19110 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a 0a LITE_NOMEM]..**.
19120 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
19130 0a 2a 2a 20 5b 48 31 37 33 30 33 5d 20 5b 48 31 .** [H17303] [H1
19140 37 33 30 34 5d 20 5b 48 31 37 33 30 35 5d 20 5b 7304] [H17305] [
19150 48 31 37 33 30 36 5d 20 5b 48 31 37 33 31 30 5d H17306] [H17310]
19160 20 5b 48 31 37 33 31 32 5d 20 5b 48 31 37 33 31 [H17312] [H1731
19170 35 5d 20 5b 48 31 37 33 31 38 5d 0a 2a 2a 20 5b 5] [H17318].** [
19180 48 31 37 33 32 31 5d 20 5b 48 31 37 33 32 32 5d H17321] [H17322]
19190 20 5b 48 31 37 33 32 33 5d 0a 2a 2a 0a 2a 2a 20 [H17323].**.**
191a0 54 68 65 20 70 6f 69 6e 74 65 72 20 61 72 67 75 The pointer argu
191b0 6d 65 6e 74 73 20 74 6f 20 5b 73 71 6c 69 74 65 ments to [sqlite
191c0 33 5f 66 72 65 65 28 29 5d 20 61 6e 64 20 5b 73 3_free()] and [s
191d0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 qlite3_realloc()
191e0 5d 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 ].** must be eit
191f0 68 65 72 20 4e 55 4c 4c 20 6f 72 20 65 6c 73 65 her NULL or else
19200 20 70 6f 69 6e 74 65 72 73 20 6f 62 74 61 69 6e pointers obtain
19210 65 64 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a ed from a prior.
19220 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 ** invocation of
19230 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
19240 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ()] or [sqlite3_
19250 72 65 61 6c 6c 6f 63 28 29 5d 20 74 68 61 74 20 realloc()] that
19260 68 61 76 65 0a 2a 2a 20 6e 6f 74 20 79 65 74 20 have.** not yet
19270 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a 2a been released..*
19280 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 *.** The applica
19290 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 72 65 tion must not re
192a0 61 64 20 6f 72 20 77 72 69 74 65 20 61 6e 79 20 ad or write any
192b0 70 61 72 74 20 6f 66 0a 2a 2a 20 61 20 62 6c 6f part of.** a blo
192c0 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 61 66 74 ck of memory aft
192d0 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20 72 er it has been r
192e0 65 6c 65 61 73 65 64 20 75 73 69 6e 67 0a 2a 2a eleased using.**
192f0 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 [sqlite3_free()
19300 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 ] or [sqlite3_re
19310 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 53 51 4c alloc()]..*/.SQL
19320 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 ITE_API void *sq
19330 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 lite3_malloc(int
19340 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f );.SQLITE_API vo
19350 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 65 61 6c id *sqlite3_real
19360 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29 3b loc(void*, int);
19370 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
19380 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 6f sqlite3_free(vo
19390 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 id*);../*.** CAP
193a0 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c I3REF: Memory Al
193b0 6c 6f 63 61 74 6f 72 20 53 74 61 74 69 73 74 69 locator Statisti
193c0 63 73 20 7b 48 31 37 33 37 30 7d 20 3c 53 33 30 cs {H17370} <S30
193d0 32 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 210>.**.** SQLit
193e0 65 20 70 72 6f 76 69 64 65 73 20 74 68 65 73 65 e provides these
193f0 20 74 77 6f 20 69 6e 74 65 72 66 61 63 65 73 20 two interfaces
19400 66 6f 72 20 72 65 70 6f 72 74 69 6e 67 20 6f 6e for reporting on
19410 20 74 68 65 20 73 74 61 74 75 73 0a 2a 2a 20 6f the status.** o
19420 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d f the [sqlite3_m
19430 61 6c 6c 6f 63 28 29 5d 2c 20 5b 73 71 6c 69 74 alloc()], [sqlit
19440 65 33 5f 66 72 65 65 28 29 5d 2c 20 61 6e 64 20 e3_free()], and
19450 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 [sqlite3_realloc
19460 28 29 5d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2c ()].** routines,
19470 20 77 68 69 63 68 20 66 6f 72 6d 20 74 68 65 20 which form the
19480 62 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 built-in memory
19490 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
194a0 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 stem..**.** Requ
194b0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
194c0 37 33 37 31 5d 20 5b 48 31 37 33 37 33 5d 20 5b 7371] [H17373] [
194d0 48 31 37 33 37 34 5d 20 5b 48 31 37 33 37 35 5d H17374] [H17375]
194e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 .*/.SQLITE_API s
194f0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c qlite3_int64 sql
19500 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 ite3_memory_used
19510 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 (void);.SQLITE_A
19520 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 PI sqlite3_int64
19530 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f sqlite3_memory_
19540 68 69 67 68 77 61 74 65 72 28 69 6e 74 20 72 65 highwater(int re
19550 73 65 74 46 6c 61 67 29 3b 0a 0a 2f 2a 0a 2a 2a setFlag);../*.**
19560 20 43 41 50 49 33 52 45 46 3a 20 50 73 65 75 64 CAPI3REF: Pseud
19570 6f 2d 52 61 6e 64 6f 6d 20 4e 75 6d 62 65 72 20 o-Random Number
19580 47 65 6e 65 72 61 74 6f 72 20 7b 48 31 37 33 39 Generator {H1739
19590 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 0} <S20000>.**.*
195a0 2a 20 53 51 4c 69 74 65 20 63 6f 6e 74 61 69 6e * SQLite contain
195b0 73 20 61 20 68 69 67 68 2d 71 75 61 6c 69 74 79 s a high-quality
195c0 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e pseudo-random n
195d0 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 umber generator
195e0 28 50 52 4e 47 29 20 75 73 65 64 20 74 6f 0a 2a (PRNG) used to.*
195f0 2a 20 73 65 6c 65 63 74 20 72 61 6e 64 6f 6d 20 * select random
19600 5b 52 4f 57 49 44 20 7c 20 52 4f 57 49 44 73 5d [ROWID | ROWIDs]
19610 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 when inserting
19620 6e 65 77 20 72 65 63 6f 72 64 73 20 69 6e 74 6f new records into
19630 20 61 20 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a a table that.**
19640 20 61 6c 72 65 61 64 79 20 75 73 65 73 20 74 68 already uses th
19650 65 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62 e largest possib
19660 6c 65 20 5b 52 4f 57 49 44 5d 2e 20 20 54 68 65 le [ROWID]. The
19670 20 50 52 4e 47 20 69 73 20 61 6c 73 6f 20 75 73 PRNG is also us
19680 65 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 62 75 ed for.** the bu
19690 69 6c 64 2d 69 6e 20 72 61 6e 64 6f 6d 28 29 20 ild-in random()
196a0 61 6e 64 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 29 and randomblob()
196b0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 SQL functions.
196c0 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 This interface
196d0 61 6c 6c 6f 77 73 0a 2a 2a 20 61 70 70 6c 69 63 allows.** applic
196e0 61 74 69 6f 6e 73 20 74 6f 20 61 63 63 65 73 73 ations to access
196f0 20 74 68 65 20 73 61 6d 65 20 50 52 4e 47 20 66 the same PRNG f
19700 6f 72 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 or other purpose
19710 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 s..**.** A call
19720 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 to this routine
19730 73 74 6f 72 65 73 20 4e 20 62 79 74 65 73 20 6f stores N bytes o
19740 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 f randomness int
19750 6f 20 62 75 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a o buffer P..**.*
19760 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 * The first time
19770 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
19780 20 69 6e 76 6f 6b 65 64 20 28 65 69 74 68 65 72 invoked (either
19790 20 69 6e 74 65 72 6e 61 6c 6c 79 20 6f 72 20 62 internally or b
197a0 79 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 y.** the applica
197b0 74 69 6f 6e 29 20 74 68 65 20 50 52 4e 47 20 69 tion) the PRNG i
197c0 73 20 73 65 65 64 65 64 20 75 73 69 6e 67 20 72 s seeded using r
197d0 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e andomness obtain
197e0 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 78 ed.** from the x
197f0 52 61 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f Randomness metho
19800 64 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 d of the default
19810 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f [sqlite3_vfs] o
19820 62 6a 65 63 74 2e 0a 2a 2a 20 4f 6e 20 61 6c 6c bject..** On all
19830 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f subsequent invo
19840 63 61 74 69 6f 6e 73 2c 20 74 68 65 20 70 73 65 cations, the pse
19850 75 64 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69 udo-randomness i
19860 73 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 69 s generated.** i
19870 6e 74 65 72 6e 61 6c 6c 79 20 61 6e 64 20 77 69 nternally and wi
19880 74 68 6f 75 74 20 72 65 63 6f 75 72 73 65 20 74 thout recourse t
19890 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 o the [sqlite3_v
198a0 66 73 5d 20 78 52 61 6e 64 6f 6d 6e 65 73 73 0a fs] xRandomness.
198b0 2a 2a 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a ** method..**.**
198c0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
198d0 2a 20 5b 48 31 37 33 39 32 5d 0a 2a 2f 0a 53 51 * [H17392].*/.SQ
198e0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
198f0 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 lite3_randomness
19900 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 50 29 (int N, void *P)
19910 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
19920 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 20 F: Compile-Time
19930 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 43 61 Authorization Ca
19940 6c 6c 62 61 63 6b 73 20 7b 48 31 32 35 30 30 7d llbacks {H12500}
19950 20 3c 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S70100>.**.**
19960 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67 This routine reg
19970 69 73 74 65 72 73 20 61 20 61 75 74 68 6f 72 69 isters a authori
19980 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 zer callback wit
19990 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a h a particular.*
199a0 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e * [database conn
199b0 65 63 74 69 6f 6e 5d 2c 20 73 75 70 70 6c 69 65 ection], supplie
199c0 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 d in the first a
199d0 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 rgument..** The
199e0 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 authorizer callb
199f0 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 ack is invoked a
19a00 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 s SQL statements
19a10 20 61 72 65 20 62 65 69 6e 67 20 63 6f 6d 70 69 are being compi
19a20 6c 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c 69 74 led.** by [sqlit
19a30 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72 e3_prepare()] or
19a40 20 69 74 73 20 76 61 72 69 61 6e 74 73 20 5b 73 its variants [s
19a50 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
19a60 32 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 2()],.** [sqlite
19a70 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 61 3_prepare16()] a
19a80 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 nd [sqlite3_prep
19a90 61 72 65 31 36 5f 76 32 28 29 5d 2e 20 20 41 74 are16_v2()]. At
19aa0 20 76 61 72 69 6f 75 73 0a 2a 2a 20 70 6f 69 6e various.** poin
19ab0 74 73 20 64 75 72 69 6e 67 20 74 68 65 20 63 6f ts during the co
19ac0 6d 70 69 6c 61 74 69 6f 6e 20 70 72 6f 63 65 73 mpilation proces
19ad0 73 2c 20 61 73 20 6c 6f 67 69 63 20 69 73 20 62 s, as logic is b
19ae0 65 69 6e 67 20 63 72 65 61 74 65 64 0a 2a 2a 20 eing created.**
19af0 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 69 6f to perform vario
19b00 75 73 20 61 63 74 69 6f 6e 73 2c 20 74 68 65 20 us actions, the
19b10 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 authorizer callb
19b20 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 ack is invoked t
19b30 6f 0a 2a 2a 20 73 65 65 20 69 66 20 74 68 6f 73 o.** see if thos
19b40 65 20 61 63 74 69 6f 6e 73 20 61 72 65 20 61 6c e actions are al
19b50 6c 6f 77 65 64 2e 20 20 54 68 65 20 61 75 74 68 lowed. The auth
19b60 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 orizer callback
19b70 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72 6e should.** return
19b80 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 74 6f 20 [SQLITE_OK] to
19b90 61 6c 6c 6f 77 20 74 68 65 20 61 63 74 69 6f 6e allow the action
19ba0 2c 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 , [SQLITE_IGNORE
19bb0 5d 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20 74 68 ] to disallow th
19bc0 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 61 63 e.** specific ac
19bd0 74 69 6f 6e 20 62 75 74 20 61 6c 6c 6f 77 20 74 tion but allow t
19be0 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
19bf0 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 to continue to
19c00 62 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2c 20 be.** compiled,
19c10 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d or [SQLITE_DENY]
19c20 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 65 6e to cause the en
19c30 74 69 72 65 20 53 51 4c 20 73 74 61 74 65 6d 65 tire SQL stateme
19c40 6e 74 20 74 6f 20 62 65 0a 2a 2a 20 72 65 6a 65 nt to be.** reje
19c50 63 74 65 64 20 77 69 74 68 20 61 6e 20 65 72 72 cted with an err
19c60 6f 72 2e 20 20 49 66 20 74 68 65 20 61 75 74 68 or. If the auth
19c70 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 orizer callback
19c80 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 79 20 76 returns.** any v
19c90 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e 20 alue other than
19ca0 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 2c [SQLITE_IGNORE],
19cb0 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 6f 72 [SQLITE_OK], or
19cc0 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d 0a 2a [SQLITE_DENY].*
19cd0 2a 20 74 68 65 6e 20 74 68 65 20 5b 73 71 6c 69 * then the [sqli
19ce0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
19cf0 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 20 ] or equivalent
19d00 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67 67 65 call that trigge
19d10 72 65 64 0a 2a 2a 20 74 68 65 20 61 75 74 68 6f red.** the autho
19d20 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c 20 rizer will fail
19d30 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 with an error me
19d40 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 ssage..**.** Whe
19d50 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 n the callback r
19d60 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f eturns [SQLITE_O
19d70 4b 5d 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 K], that means t
19d80 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 he operation.**
19d90 72 65 71 75 65 73 74 65 64 20 69 73 20 6f 6b 2e requested is ok.
19da0 20 20 57 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 When the callb
19db0 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c ack returns [SQL
19dc0 49 54 45 5f 44 45 4e 59 5d 2c 20 74 68 65 0a 2a ITE_DENY], the.*
19dd0 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 * [sqlite3_prepa
19de0 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 75 69 re_v2()] or equi
19df0 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61 74 valent call that
19e00 20 74 72 69 67 67 65 72 65 64 20 74 68 65 0a 2a triggered the.*
19e10 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 77 69 6c * authorizer wil
19e20 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 20 65 l fail with an e
19e30 72 72 6f 72 20 6d 65 73 73 61 67 65 20 65 78 70 rror message exp
19e40 6c 61 69 6e 69 6e 67 20 74 68 61 74 0a 2a 2a 20 laining that.**
19e50 61 63 63 65 73 73 20 69 73 20 64 65 6e 69 65 64 access is denied
19e60 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 . .**.** The fir
19e70 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 st parameter to
19e80 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 the authorizer c
19e90 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 63 6f 70 allback is a cop
19ea0 79 20 6f 66 20 74 68 65 20 74 68 69 72 64 0a 2a y of the third.*
19eb0 2a 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 * parameter to t
19ec0 68 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 he sqlite3_set_a
19ed0 75 74 68 6f 72 69 7a 65 72 28 29 20 69 6e 74 65 uthorizer() inte
19ee0 72 66 61 63 65 2e 20 54 68 65 20 73 65 63 6f 6e rface. The secon
19ef0 64 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 d parameter.** t
19f00 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 o the callback i
19f10 73 20 61 6e 20 69 6e 74 65 67 65 72 20 5b 53 51 s an integer [SQ
19f20 4c 49 54 45 5f 43 4f 50 59 20 7c 20 61 63 74 69 LITE_COPY | acti
19f30 6f 6e 20 63 6f 64 65 5d 20 74 68 61 74 20 73 70 on code] that sp
19f40 65 63 69 66 69 65 73 0a 2a 2a 20 74 68 65 20 70 ecifies.** the p
19f50 61 72 74 69 63 75 6c 61 72 20 61 63 74 69 6f 6e articular action
19f60 20 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65 to be authorize
19f70 64 2e 20 54 68 65 20 74 68 69 72 64 20 74 68 72 d. The third thr
19f80 6f 75 67 68 20 73 69 78 74 68 20 70 61 72 61 6d ough sixth param
19f90 65 74 65 72 73 0a 2a 2a 20 74 6f 20 74 68 65 20 eters.** to the
19fa0 63 61 6c 6c 62 61 63 6b 20 61 72 65 20 7a 65 72 callback are zer
19fb0 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 o-terminated str
19fc0 69 6e 67 73 20 74 68 61 74 20 63 6f 6e 74 61 69 ings that contai
19fd0 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 n additional.**
19fe0 64 65 74 61 69 6c 73 20 61 62 6f 75 74 20 74 68 details about th
19ff0 65 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 e action to be a
1a000 75 74 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a uthorized..**.**
1a010 20 49 66 20 74 68 65 20 61 63 74 69 6f 6e 20 63 If the action c
1a020 6f 64 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 52 ode is [SQLITE_R
1a030 45 41 44 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 EAD].** and the
1a040 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 callback returns
1a050 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d [SQLITE_IGNORE]
1a060 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 70 72 then the.** [pr
1a070 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
1a080 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 63 ] statement is c
1a090 6f 6e 73 74 72 75 63 74 65 64 20 74 6f 20 73 75 onstructed to su
1a0a0 62 73 74 69 74 75 74 65 0a 2a 2a 20 61 20 4e 55 bstitute.** a NU
1a0b0 4c 4c 20 76 61 6c 75 65 20 69 6e 20 70 6c 61 63 LL value in plac
1a0c0 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 e of the table c
1a0d0 6f 6c 75 6d 6e 20 74 68 61 74 20 77 6f 75 6c 64 olumn that would
1a0e0 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 72 65 have.** been re
1a0f0 61 64 20 69 66 20 5b 53 51 4c 49 54 45 5f 4f 4b ad if [SQLITE_OK
1a100 5d 20 68 61 64 20 62 65 65 6e 20 72 65 74 75 72 ] had been retur
1a110 6e 65 64 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 ned. The [SQLIT
1a120 45 5f 49 47 4e 4f 52 45 5d 0a 2a 2a 20 72 65 74 E_IGNORE].** ret
1a130 75 72 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 urn can be used
1a140 74 6f 20 64 65 6e 79 20 61 6e 20 75 6e 74 72 75 to deny an untru
1a150 73 74 65 64 20 75 73 65 72 20 61 63 63 65 73 73 sted user access
1a160 20 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a to individual.*
1a170 2a 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 61 20 74 * columns of a t
1a180 61 62 6c 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 able..** If the
1a190 61 63 74 69 6f 6e 20 63 6f 64 65 20 69 73 20 5b action code is [
1a1a0 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 5d 20 61 SQLITE_DELETE] a
1a1b0 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 nd the callback
1a1c0 72 65 74 75 72 6e 73 0a 2a 2a 20 5b 53 51 4c 49 returns.** [SQLI
1a1d0 54 45 5f 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 TE_IGNORE] then
1a1e0 74 68 65 20 5b 44 45 4c 45 54 45 5d 20 6f 70 65 the [DELETE] ope
1a1f0 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 73 20 ration proceeds
1a200 62 75 74 20 74 68 65 0a 2a 2a 20 5b 74 72 75 6e but the.** [trun
1a210 63 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f cate optimizatio
1a220 6e 5d 20 69 73 20 64 69 73 61 62 6c 65 64 20 61 n] is disabled a
1a230 6e 64 20 61 6c 6c 20 72 6f 77 73 20 61 72 65 20 nd all rows are
1a240 64 65 6c 65 74 65 64 20 69 6e 64 69 76 69 64 75 deleted individu
1a250 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 ally..**.** An a
1a260 75 74 68 6f 72 69 7a 65 72 20 69 73 20 75 73 65 uthorizer is use
1a270 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f d when [sqlite3_
1a280 70 72 65 70 61 72 65 20 7c 20 70 72 65 70 61 72 prepare | prepar
1a290 69 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73 74 61 74 ing].** SQL stat
1a2a0 65 6d 65 6e 74 73 20 66 72 6f 6d 20 61 6e 20 75 ements from an u
1a2b0 6e 74 72 75 73 74 65 64 20 73 6f 75 72 63 65 2c ntrusted source,
1a2c0 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 to ensure that
1a2d0 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
1a2e0 74 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74 72 79 ts.** do not try
1a2f0 20 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 20 to access data
1a300 74 68 65 79 20 61 72 65 20 6e 6f 74 20 61 6c 6c they are not all
1a310 6f 77 65 64 20 74 6f 20 73 65 65 2c 20 6f 72 20 owed to see, or
1a320 74 68 61 74 20 74 68 65 79 20 64 6f 20 6e 6f 74 that they do not
1a330 0a 2a 2a 20 74 72 79 20 74 6f 20 65 78 65 63 75 .** try to execu
1a340 74 65 20 6d 61 6c 69 63 69 6f 75 73 20 73 74 61 te malicious sta
1a350 74 65 6d 65 6e 74 73 20 74 68 61 74 20 64 61 6d tements that dam
1a360 61 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65 age the database
1a370 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c . For.** exampl
1a380 65 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f e, an applicatio
1a390 6e 20 6d 61 79 20 61 6c 6c 6f 77 20 61 20 75 73 n may allow a us
1a3a0 65 72 20 74 6f 20 65 6e 74 65 72 20 61 72 62 69 er to enter arbi
1a3b0 74 72 61 72 79 0a 2a 2a 20 53 51 4c 20 71 75 65 trary.** SQL que
1a3c0 72 69 65 73 20 66 6f 72 20 65 76 61 6c 75 61 74 ries for evaluat
1a3d0 69 6f 6e 20 62 79 20 61 20 64 61 74 61 62 61 73 ion by a databas
1a3e0 65 2e 20 20 42 75 74 20 74 68 65 20 61 70 70 6c e. But the appl
1a3f0 69 63 61 74 69 6f 6e 20 64 6f 65 73 0a 2a 2a 20 ication does.**
1a400 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 75 73 65 not want the use
1a410 72 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 r to be able to
1a420 6d 61 6b 65 20 61 72 62 69 74 72 61 72 79 20 63 make arbitrary c
1a430 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a hanges to the.**
1a440 20 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20 61 database. An a
1a450 75 74 68 6f 72 69 7a 65 72 20 63 6f 75 6c 64 20 uthorizer could
1a460 74 68 65 6e 20 62 65 20 70 75 74 20 69 6e 20 70 then be put in p
1a470 6c 61 63 65 20 77 68 69 6c 65 20 74 68 65 0a 2a lace while the.*
1a480 2a 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53 * user-entered S
1a490 51 4c 20 69 73 20 62 65 69 6e 67 20 5b 73 71 6c QL is being [sql
1a4a0 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70 ite3_prepare | p
1a4b0 72 65 70 61 72 65 64 5d 20 74 68 61 74 0a 2a 2a repared] that.**
1a4c0 20 64 69 73 61 6c 6c 6f 77 73 20 65 76 65 72 79 disallows every
1a4d0 74 68 69 6e 67 20 65 78 63 65 70 74 20 5b 53 45 thing except [SE
1a4e0 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 73 LECT] statements
1a4f0 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74 ..**.** Applicat
1a500 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 ions that need t
1a510 6f 20 70 72 6f 63 65 73 73 20 53 51 4c 20 66 72 o process SQL fr
1a520 6f 6d 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75 om untrusted sou
1a530 72 63 65 73 0a 2a 2a 20 6d 69 67 68 74 20 61 6c rces.** might al
1a540 73 6f 20 63 6f 6e 73 69 64 65 72 20 6c 6f 77 65 so consider lowe
1a550 72 69 6e 67 20 72 65 73 6f 75 72 63 65 20 6c 69 ring resource li
1a560 6d 69 74 73 20 75 73 69 6e 67 20 5b 73 71 6c 69 mits using [sqli
1a570 74 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 te3_limit()].**
1a580 61 6e 64 20 6c 69 6d 69 74 69 6e 67 20 64 61 74 and limiting dat
1a590 61 62 61 73 65 20 73 69 7a 65 20 75 73 69 6e 67 abase size using
1a5a0 20 74 68 65 20 5b 6d 61 78 5f 70 61 67 65 5f 63 the [max_page_c
1a5b0 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 0a 2a ount] [PRAGMA].*
1a5c0 2a 20 69 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f * in addition to
1a5d0 20 75 73 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 using an author
1a5e0 69 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 izer..**.** Only
1a5f0 20 61 20 73 69 6e 67 6c 65 20 61 75 74 68 6f 72 a single author
1a600 69 7a 65 72 20 63 61 6e 20 62 65 20 69 6e 20 70 izer can be in p
1a610 6c 61 63 65 20 6f 6e 20 61 20 64 61 74 61 62 61 lace on a databa
1a620 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a se connection.**
1a630 20 61 74 20 61 20 74 69 6d 65 2e 20 20 45 61 63 at a time. Eac
1a640 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 h call to sqlite
1a650 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 3_set_authorizer
1a660 20 6f 76 65 72 72 69 64 65 73 20 74 68 65 0a 2a overrides the.*
1a670 2a 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2e * previous call.
1a680 20 20 44 69 73 61 62 6c 65 20 74 68 65 20 61 75 Disable the au
1a690 74 68 6f 72 69 7a 65 72 20 62 79 20 69 6e 73 74 thorizer by inst
1a6a0 61 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 63 61 alling a NULL ca
1a6b0 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 20 61 llback..** The a
1a6c0 75 74 68 6f 72 69 7a 65 72 20 69 73 20 64 69 73 uthorizer is dis
1a6d0 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 abled by default
1a6e0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 ..**.** The auth
1a6f0 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 orizer callback
1a700 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 must not do anyt
1a710 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d hing that will m
1a720 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 odify.** the dat
1a730 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1a740 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 that invoked th
1a750 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c e authorizer cal
1a760 6c 62 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65 20 74 lback..** Note t
1a770 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 hat [sqlite3_pre
1a780 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b pare_v2()] and [
1a790 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
1a7a0 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69 both modify thei
1a7b0 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f r.** database co
1a7c0 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 nnections for th
1a7d0 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f e meaning of "mo
1a7e0 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61 dify" in this pa
1a7f0 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 57 ragraph..**.** W
1a800 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 hen [sqlite3_pre
1a810 70 61 72 65 5f 76 32 28 29 5d 20 69 73 20 75 73 pare_v2()] is us
1a820 65 64 20 74 6f 20 70 72 65 70 61 72 65 20 61 20 ed to prepare a
1a830 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 0a 2a statement, the.*
1a840 2a 20 73 74 61 74 65 6d 65 6e 74 20 6d 69 67 68 * statement migh
1a850 74 20 62 65 20 72 65 2d 70 72 65 70 61 72 65 64 t be re-prepared
1a860 20 64 75 72 69 6e 67 20 5b 73 71 6c 69 74 65 33 during [sqlite3
1a870 5f 73 74 65 70 28 29 5d 20 64 75 65 20 74 6f 20 _step()] due to
1a880 61 20 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 a .** schema cha
1a890 6e 67 65 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 nge. Hence, the
1a8a0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f application sho
1a8b0 75 6c 64 20 65 6e 73 75 72 65 20 74 68 61 74 20 uld ensure that
1a8c0 74 68 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 the.** correct a
1a8d0 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 uthorizer callba
1a8e0 63 6b 20 72 65 6d 61 69 6e 73 20 69 6e 20 70 6c ck remains in pl
1a8f0 61 63 65 20 64 75 72 69 6e 67 20 74 68 65 20 5b ace during the [
1a900 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e sqlite3_step()].
1a910 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 .**.** Note that
1a920 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 the authorizer
1a930 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f callback is invo
1a940 6b 65 64 20 6f 6e 6c 79 20 64 75 72 69 6e 67 0a ked only during.
1a950 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ** [sqlite3_prep
1a960 61 72 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 are()] or its va
1a970 72 69 61 6e 74 73 2e 20 20 41 75 74 68 6f 72 69 riants. Authori
1a980 7a 61 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a zation is not.**
1a990 20 70 65 72 66 6f 72 6d 65 64 20 64 75 72 69 6e performed durin
1a9a0 67 20 73 74 61 74 65 6d 65 6e 74 20 65 76 61 6c g statement eval
1a9b0 75 61 74 69 6f 6e 20 69 6e 20 5b 73 71 6c 69 74 uation in [sqlit
1a9c0 65 33 5f 73 74 65 70 28 29 5d 2c 20 75 6e 6c 65 e3_step()], unle
1a9d0 73 73 0a 2a 2a 20 61 73 20 73 74 61 74 65 64 20 ss.** as stated
1a9e0 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 in the previous
1a9f0 70 61 72 61 67 72 61 70 68 2c 20 73 71 6c 69 74 paragraph, sqlit
1aa00 65 33 5f 73 74 65 70 28 29 20 69 6e 76 6f 6b 65 e3_step() invoke
1aa10 73 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 65 s.** sqlite3_pre
1aa20 70 61 72 65 5f 76 32 28 29 20 74 6f 20 72 65 70 pare_v2() to rep
1aa30 72 65 70 61 72 65 20 61 20 73 74 61 74 65 6d 65 repare a stateme
1aa40 6e 74 20 61 66 74 65 72 20 61 20 73 63 68 65 6d nt after a schem
1aa50 61 20 63 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 a change..**.**
1aa60 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
1aa70 20 5b 48 31 32 35 30 31 5d 20 5b 48 31 32 35 30 [H12501] [H1250
1aa80 32 5d 20 5b 48 31 32 35 30 33 5d 20 5b 48 31 32 2] [H12503] [H12
1aa90 35 30 34 5d 20 5b 48 31 32 35 30 35 5d 20 5b 48 504] [H12505] [H
1aaa0 31 32 35 30 36 5d 20 5b 48 31 32 35 30 37 5d 20 12506] [H12507]
1aab0 5b 48 31 32 35 31 30 5d 0a 2a 2a 20 5b 48 31 32 [H12510].** [H12
1aac0 35 31 31 5d 20 5b 48 31 32 35 31 32 5d 20 5b 48 511] [H12512] [H
1aad0 31 32 35 32 30 5d 20 5b 48 31 32 35 32 31 5d 20 12520] [H12521]
1aae0 5b 48 31 32 35 32 32 5d 0a 2a 2f 0a 53 51 4c 49 [H12522].*/.SQLI
1aaf0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1ab00 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 e3_set_authorize
1ab10 72 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 0a 20 r(. sqlite3*,.
1ab20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f int (*xAuth)(vo
1ab30 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 id*,int,const ch
1ab40 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c ar*,const char*,
1ab50 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 const char*,cons
1ab60 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69 64 t char*),. void
1ab70 20 2a 70 55 73 65 72 44 61 74 61 0a 29 3b 0a 0a *pUserData.);..
1ab80 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
1ab90 41 75 74 68 6f 72 69 7a 65 72 20 52 65 74 75 72 Authorizer Retur
1aba0 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 39 30 7d n Codes {H12590}
1abb0 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 <H12500>.**.**
1abc0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 The [sqlite3_set
1abd0 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75 _authorizer | au
1abe0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1abf0 6b 20 66 75 6e 63 74 69 6f 6e 5d 20 6d 75 73 74 k function] must
1ac00 0a 2a 2a 20 72 65 74 75 72 6e 20 65 69 74 68 65 .** return eithe
1ac10 72 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 72 r [SQLITE_OK] or
1ac20 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 74 77 one of these tw
1ac30 6f 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e 20 6f o constants in o
1ac40 72 64 65 72 0a 2a 2a 20 74 6f 20 73 69 67 6e 61 rder.** to signa
1ac50 6c 20 53 51 4c 69 74 65 20 77 68 65 74 68 65 72 l SQLite whether
1ac60 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 63 74 69 or not the acti
1ac70 6f 6e 20 69 73 20 70 65 72 6d 69 74 74 65 64 2e on is permitted.
1ac80 20 20 53 65 65 20 74 68 65 0a 2a 2a 20 5b 73 71 See the.** [sq
1ac90 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 lite3_set_author
1aca0 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 izer | authorize
1acb0 72 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 5d r documentation]
1acc0 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a for additional.
1acd0 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a ** information..
1ace0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1acf0 45 5f 44 45 4e 59 20 20 20 31 20 20 20 2f 2a 20 E_DENY 1 /*
1ad00 41 62 6f 72 74 20 74 68 65 20 53 51 4c 20 73 74 Abort the SQL st
1ad10 61 74 65 6d 65 6e 74 20 77 69 74 68 20 61 6e 20 atement with an
1ad20 65 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 error */.#define
1ad30 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 32 SQLITE_IGNORE 2
1ad40 20 20 20 2f 2a 20 44 6f 6e 27 74 20 61 6c 6c 6f /* Don't allo
1ad50 77 20 61 63 63 65 73 73 2c 20 62 75 74 20 64 6f w access, but do
1ad60 6e 27 74 20 67 65 6e 65 72 61 74 65 20 61 6e 20 n't generate an
1ad70 65 72 72 6f 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 error */../*.**
1ad80 43 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72 CAPI3REF: Author
1ad90 69 7a 65 72 20 41 63 74 69 6f 6e 20 43 6f 64 65 izer Action Code
1ada0 73 20 7b 48 31 32 35 35 30 7d 20 3c 48 31 32 35 s {H12550} <H125
1adb0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 00>.**.** The [s
1adc0 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f qlite3_set_autho
1add0 72 69 7a 65 72 28 29 5d 20 69 6e 74 65 72 66 61 rizer()] interfa
1ade0 63 65 20 72 65 67 69 73 74 65 72 73 20 61 20 63 ce registers a c
1adf0 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e allback function
1ae00 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 76 6f .** that is invo
1ae10 6b 65 64 20 74 6f 20 61 75 74 68 6f 72 69 7a 65 ked to authorize
1ae20 20 63 65 72 74 61 69 6e 20 53 51 4c 20 73 74 61 certain SQL sta
1ae30 74 65 6d 65 6e 74 20 61 63 74 69 6f 6e 73 2e 20 tement actions.
1ae40 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 The.** second p
1ae50 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 arameter to the
1ae60 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 callback is an i
1ae70 6e 74 65 67 65 72 20 63 6f 64 65 20 74 68 61 74 nteger code that
1ae80 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 68 specifies.** wh
1ae90 61 74 20 61 63 74 69 6f 6e 20 69 73 20 62 65 69 at action is bei
1aea0 6e 67 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20 ng authorized.
1aeb0 54 68 65 73 65 20 61 72 65 20 74 68 65 20 69 6e These are the in
1aec0 74 65 67 65 72 20 61 63 74 69 6f 6e 20 63 6f 64 teger action cod
1aed0 65 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 61 es that.** the a
1aee0 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 uthorizer callba
1aef0 63 6b 20 6d 61 79 20 62 65 20 70 61 73 73 65 64 ck may be passed
1af00 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 63 ..**.** These ac
1af10 74 69 6f 6e 20 63 6f 64 65 20 76 61 6c 75 65 73 tion code values
1af20 20 73 69 67 6e 69 66 79 20 77 68 61 74 20 6b 69 signify what ki
1af30 6e 64 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 20 nd of operation
1af40 69 73 20 74 6f 20 62 65 0a 2a 2a 20 61 75 74 68 is to be.** auth
1af50 6f 72 69 7a 65 64 2e 20 20 54 68 65 20 33 72 64 orized. The 3rd
1af60 20 61 6e 64 20 34 74 68 20 70 61 72 61 6d 65 74 and 4th paramet
1af70 65 72 73 20 74 6f 20 74 68 65 20 61 75 74 68 6f ers to the autho
1af80 72 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6c 6c rization.** call
1af90 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 77 69 back function wi
1afa0 6c 6c 20 62 65 20 70 61 72 61 6d 65 74 65 72 73 ll be parameters
1afb0 20 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e 64 69 or NULL dependi
1afc0 6e 67 20 6f 6e 20 77 68 69 63 68 20 6f 66 20 74 ng on which of t
1afd0 68 65 73 65 0a 2a 2a 20 63 6f 64 65 73 20 69 73 hese.** codes is
1afe0 20 75 73 65 64 20 61 73 20 74 68 65 20 73 65 63 used as the sec
1aff0 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 ond parameter.
1b000 54 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 The 5th paramete
1b010 72 20 74 6f 20 74 68 65 0a 2a 2a 20 61 75 74 68 r to the.** auth
1b020 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 orizer callback
1b030 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 is the name of t
1b040 68 65 20 64 61 74 61 62 61 73 65 20 28 22 6d 61 he database ("ma
1b050 69 6e 22 2c 20 22 74 65 6d 70 22 2c 0a 2a 2a 20 in", "temp",.**
1b060 65 74 63 2e 29 20 69 66 20 61 70 70 6c 69 63 61 etc.) if applica
1b070 62 6c 65 2e 20 20 54 68 65 20 36 74 68 20 70 61 ble. The 6th pa
1b080 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 rameter to the a
1b090 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 uthorizer callba
1b0a0 63 6b 0a 2a 2a 20 69 73 20 74 68 65 20 6e 61 6d ck.** is the nam
1b0b0 65 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 2d 6d e of the inner-m
1b0c0 6f 73 74 20 74 72 69 67 67 65 72 20 6f 72 20 76 ost trigger or v
1b0d0 69 65 77 20 74 68 61 74 20 69 73 20 72 65 73 70 iew that is resp
1b0e0 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 74 onsible for.** t
1b0f0 68 65 20 61 63 63 65 73 73 20 61 74 74 65 6d 70 he access attemp
1b100 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 69 t or NULL if thi
1b110 73 20 61 63 63 65 73 73 20 61 74 74 65 6d 70 74 s access attempt
1b120 20 69 73 20 64 69 72 65 63 74 6c 79 20 66 72 6f is directly fro
1b130 6d 0a 2a 2a 20 74 6f 70 2d 6c 65 76 65 6c 20 53 m.** top-level S
1b140 51 4c 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 52 QL code..**.** R
1b150 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
1b160 5b 48 31 32 35 35 31 5d 20 5b 48 31 32 35 35 32 [H12551] [H12552
1b170 5d 20 5b 48 31 32 35 35 33 5d 20 5b 48 31 32 35 ] [H12553] [H125
1b180 35 34 5d 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 54].*/./********
1b190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b1b0 2a 2a 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a 2a 2a *** 3rd ********
1b1c0 2a 2a 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a 2a 2a **** 4th *******
1b1d0 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ****/.#define SQ
1b1e0 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 LITE_CREATE_INDE
1b1f0 58 20 20 20 20 20 20 20 20 20 20 31 20 20 20 2f X 1 /
1b200 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 * Index Name
1b210 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 Table Name
1b220 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1b230 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c 45 ITE_CREATE_TABLE
1b240 20 20 20 20 20 20 20 20 20 20 32 20 20 20 2f 2a 2 /*
1b250 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
1b260 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 NULL
1b270 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1b280 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 TE_CREATE_TEMP_I
1b290 4e 44 45 58 20 20 20 20 20 33 20 20 20 2f 2a 20 NDEX 3 /*
1b2a0 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 Index Name
1b2b0 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
1b2c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1b2d0 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 E_CREATE_TEMP_TA
1b2e0 42 4c 45 20 20 20 20 20 34 20 20 20 2f 2a 20 54 BLE 4 /* T
1b2f0 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e able Name N
1b300 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a ULL *
1b310 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1b320 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49 _CREATE_TEMP_TRI
1b330 47 47 45 52 20 20 20 35 20 20 20 2f 2a 20 54 72 GGER 5 /* Tr
1b340 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 igger Name Ta
1b350 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f ble Name */
1b360 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1b370 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 CREATE_TEMP_VIEW
1b380 20 20 20 20 20 20 36 20 20 20 2f 2a 20 56 69 65 6 /* Vie
1b390 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c w Name NUL
1b3a0 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a L */.
1b3b0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
1b3c0 52 45 41 54 45 5f 54 52 49 47 47 45 52 20 20 20 REATE_TRIGGER
1b3d0 20 20 20 20 20 37 20 20 20 2f 2a 20 54 72 69 67 7 /* Trig
1b3e0 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c ger Name Tabl
1b3f0 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 e Name */.#
1b400 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 define SQLITE_CR
1b410 45 41 54 45 5f 56 49 45 57 20 20 20 20 20 20 20 EATE_VIEW
1b420 20 20 20 20 38 20 20 20 2f 2a 20 56 69 65 77 20 8 /* View
1b430 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 Name NULL
1b440 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
1b450 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 4c efine SQLITE_DEL
1b460 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ETE
1b470 20 20 20 39 20 20 20 2f 2a 20 54 61 62 6c 65 20 9 /* Table
1b480 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 Name NULL
1b490 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 */.#de
1b4a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 fine SQLITE_DROP
1b4b0 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 _INDEX
1b4c0 20 31 30 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 10 /* Index N
1b4d0 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e ame Table N
1b4e0 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 ame */.#def
1b4f0 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f ine SQLITE_DROP_
1b500 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 TABLE
1b510 31 31 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 11 /* Table Na
1b520 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 me NULL
1b530 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 */.#defi
1b540 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 ne SQLITE_DROP_T
1b550 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 20 31 EMP_INDEX 1
1b560 32 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 2 /* Index Nam
1b570 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d e Table Nam
1b580 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e e */.#defin
1b590 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 e SQLITE_DROP_TE
1b5a0 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 20 31 33 MP_TABLE 13
1b5b0 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 /* Table Name
1b5c0 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 NULL
1b5d0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
1b5e0 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d SQLITE_DROP_TEM
1b5f0 50 5f 54 52 49 47 47 45 52 20 20 20 20 31 34 20 P_TRIGGER 14
1b600 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d /* Trigger Nam
1b610 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 e Table Name
1b620 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
1b630 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 SQLITE_DROP_TEMP
1b640 5f 56 49 45 57 20 20 20 20 20 20 20 31 35 20 20 _VIEW 15
1b650 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 /* View Name
1b660 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 NULL
1b670 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
1b680 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 QLITE_DROP_TRIGG
1b690 45 52 20 20 20 20 20 20 20 20 20 31 36 20 20 20 ER 16
1b6a0 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20 /* Trigger Name
1b6b0 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 Table Name
1b6c0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1b6d0 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 20 20 LITE_DROP_VIEW
1b6e0 20 20 20 20 20 20 20 20 20 20 31 37 20 20 20 2f 17 /
1b6f0 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20 * View Name
1b700 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 NULL
1b710 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1b720 49 54 45 5f 49 4e 53 45 52 54 20 20 20 20 20 20 ITE_INSERT
1b730 20 20 20 20 20 20 20 20 20 31 38 20 20 20 2f 2a 18 /*
1b740 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
1b750 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 NULL
1b760 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1b770 54 45 5f 50 52 41 47 4d 41 20 20 20 20 20 20 20 TE_PRAGMA
1b780 20 20 20 20 20 20 20 20 31 39 20 20 20 2f 2a 20 19 /*
1b790 50 72 61 67 6d 61 20 4e 61 6d 65 20 20 20 20 20 Pragma Name
1b7a0 31 73 74 20 61 72 67 20 6f 72 20 4e 55 4c 4c 20 1st arg or NULL
1b7b0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1b7c0 45 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 20 E_READ
1b7d0 20 20 20 20 20 20 20 32 30 20 20 20 2f 2a 20 54 20 /* T
1b7e0 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43 able Name C
1b7f0 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a olumn Name *
1b800 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1b810 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20 20 _SELECT
1b820 20 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4e 55 21 /* NU
1b830 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 LL NU
1b840 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f LL */
1b850 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1b860 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20 TRANSACTION
1b870 20 20 20 20 20 32 32 20 20 20 2f 2a 20 4f 70 65 22 /* Ope
1b880 72 61 74 69 6f 6e 20 20 20 20 20 20 20 4e 55 4c ration NUL
1b890 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a L */.
1b8a0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 #define SQLITE_U
1b8b0 50 44 41 54 45 20 20 20 20 20 20 20 20 20 20 20 PDATE
1b8c0 20 20 20 20 32 33 20 20 20 2f 2a 20 54 61 62 6c 23 /* Tabl
1b8d0 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c 75 e Name Colu
1b8e0 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 mn Name */.#
1b8f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 54 define SQLITE_AT
1b900 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 TACH
1b910 20 20 20 32 34 20 20 20 2f 2a 20 46 69 6c 65 6e 24 /* Filen
1b920 61 6d 65 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 ame NULL
1b930 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
1b940 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 54 efine SQLITE_DET
1b950 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20 ACH
1b960 20 20 32 35 20 20 20 2f 2a 20 44 61 74 61 62 61 25 /* Databa
1b970 73 65 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20 20 se Name NULL
1b980 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 */.#de
1b990 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4c 54 45 fine SQLITE_ALTE
1b9a0 52 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 R_TABLE
1b9b0 20 32 36 20 20 20 2f 2a 20 44 61 74 61 62 61 73 26 /* Databas
1b9c0 65 20 4e 61 6d 65 20 20 20 54 61 62 6c 65 20 4e e Name Table N
1b9d0 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 ame */.#def
1b9e0 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 49 4e 44 ine SQLITE_REIND
1b9f0 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 EX
1ba00 32 37 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 27 /* Index Na
1ba10 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 me NULL
1ba20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 */.#defi
1ba30 6e 65 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 5a ne SQLITE_ANALYZ
1ba40 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 E 2
1ba50 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 8 /* Table Nam
1ba60 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 e NULL
1ba70 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
1ba80 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f e SQLITE_CREATE_
1ba90 56 54 41 42 4c 45 20 20 20 20 20 20 20 20 32 39 VTABLE 29
1baa0 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 /* Table Name
1bab0 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d Module Nam
1bac0 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 e */.#define
1bad0 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 41 SQLITE_DROP_VTA
1bae0 42 4c 45 20 20 20 20 20 20 20 20 20 20 33 30 20 BLE 30
1baf0 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 /* Table Name
1bb00 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d 65 Module Name
1bb10 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
1bb20 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e 20 SQLITE_FUNCTION
1bb30 20 20 20 20 20 20 20 20 20 20 20 20 33 31 20 20 31
1bb40 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 /* NULL
1bb50 20 20 20 20 46 75 6e 63 74 69 6f 6e 20 4e 61 6d Function Nam
1bb60 65 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 e */.#define S
1bb70 51 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e 54 20 QLITE_SAVEPOINT
1bb80 20 20 20 20 20 20 20 20 20 20 20 33 32 20 20 20 32
1bb90 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20 20 20 /* Operation
1bba0 20 20 20 53 61 76 65 70 6f 69 6e 74 20 4e 61 6d Savepoint Nam
1bbb0 65 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 e */.#define SQ
1bbc0 4c 49 54 45 5f 43 4f 50 59 20 20 20 20 20 20 20 LITE_COPY
1bbd0 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 2f 0 /
1bbe0 2a 20 4e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 64 * No longer used
1bbf0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 */../*.** CAPI3
1bc00 52 45 46 3a 20 54 72 61 63 69 6e 67 20 41 6e 64 REF: Tracing And
1bc10 20 50 72 6f 66 69 6c 69 6e 67 20 46 75 6e 63 74 Profiling Funct
1bc20 69 6f 6e 73 20 7b 48 31 32 32 38 30 7d 20 3c 53 ions {H12280} <S
1bc30 36 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 60400>.** EXPERI
1bc40 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 MENTAL.**.** The
1bc50 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 67 69 se routines regi
1bc60 73 74 65 72 20 63 61 6c 6c 62 61 63 6b 20 66 75 ster callback fu
1bc70 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e nctions that can
1bc80 20 62 65 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 be used for.**
1bc90 74 72 61 63 69 6e 67 20 61 6e 64 20 70 72 6f 66 tracing and prof
1bca0 69 6c 69 6e 67 20 74 68 65 20 65 78 65 63 75 74 iling the execut
1bcb0 69 6f 6e 20 6f 66 20 53 51 4c 20 73 74 61 74 65 ion of SQL state
1bcc0 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ments..**.** The
1bcd0 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 callback functi
1bce0 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62 79 on registered by
1bcf0 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 29 sqlite3_trace()
1bd00 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 0a 2a is invoked at.*
1bd10 2a 20 76 61 72 69 6f 75 73 20 74 69 6d 65 73 20 * various times
1bd20 77 68 65 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 when an SQL stat
1bd30 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 20 72 ement is being r
1bd40 75 6e 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 un by [sqlite3_s
1bd50 74 65 70 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 63 tep()]..** The c
1bd60 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 allback returns
1bd70 61 20 55 54 46 2d 38 20 72 65 6e 64 65 72 69 6e a UTF-8 renderin
1bd80 67 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 g of the SQL sta
1bd90 74 65 6d 65 6e 74 20 74 65 78 74 0a 2a 2a 20 61 tement text.** a
1bda0 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 s the statement
1bdb0 66 69 72 73 74 20 62 65 67 69 6e 73 20 65 78 65 first begins exe
1bdc0 63 75 74 69 6e 67 2e 20 20 41 64 64 69 74 69 6f cuting. Additio
1bdd0 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 6f 63 nal callbacks oc
1bde0 63 75 72 0a 2a 2a 20 61 73 20 65 61 63 68 20 74 cur.** as each t
1bdf0 72 69 67 67 65 72 65 64 20 73 75 62 70 72 6f 67 riggered subprog
1be00 72 61 6d 20 69 73 20 65 6e 74 65 72 65 64 2e 20 ram is entered.
1be10 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 73 20 66 The callbacks f
1be20 6f 72 20 74 72 69 67 67 65 72 73 0a 2a 2a 20 63 or triggers.** c
1be30 6f 6e 74 61 69 6e 20 61 20 55 54 46 2d 38 20 53 ontain a UTF-8 S
1be40 51 4c 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20 QL comment that
1be50 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 74 identifies the t
1be60 72 69 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 rigger..**.** Th
1be70 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 e callback funct
1be80 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62 ion registered b
1be90 79 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c y sqlite3_profil
1bea0 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a e() is invoked.*
1beb0 2a 20 61 73 20 65 61 63 68 20 53 51 4c 20 73 74 * as each SQL st
1bec0 61 74 65 6d 65 6e 74 20 66 69 6e 69 73 68 65 73 atement finishes
1bed0 2e 20 20 54 68 65 20 70 72 6f 66 69 6c 65 20 63 . The profile c
1bee0 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 73 allback contains
1bef0 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c .** the original
1bf00 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74 20 statement text
1bf10 61 6e 64 20 61 6e 20 65 73 74 69 6d 61 74 65 20 and an estimate
1bf20 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74 69 of wall-clock ti
1bf30 6d 65 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c 6f 6e me.** of how lon
1bf40 67 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 g that statement
1bf50 20 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a 2a 2a took to run..**
1bf60 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
1bf70 3a 0a 2a 2a 20 5b 48 31 32 32 38 31 5d 20 5b 48 :.** [H12281] [H
1bf80 31 32 32 38 32 5d 20 5b 48 31 32 32 38 33 5d 20 12282] [H12283]
1bf90 5b 48 31 32 32 38 34 5d 20 5b 48 31 32 32 38 35 [H12284] [H12285
1bfa0 5d 20 5b 48 31 32 32 38 37 5d 20 5b 48 31 32 32 ] [H12287] [H122
1bfb0 38 38 5d 20 5b 48 31 32 32 38 39 5d 0a 2a 2a 20 88] [H12289].**
1bfc0 5b 48 31 32 32 39 30 5d 0a 2a 2f 0a 53 51 4c 49 [H12290].*/.SQLI
1bfd0 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 TE_API SQLITE_EX
1bfe0 50 45 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20 PERIMENTAL void
1bff0 2a 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73 *sqlite3_trace(s
1c000 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 78 qlite3*, void(*x
1c010 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e Trace)(void*,con
1c020 73 74 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 2a st char*), void*
1c030 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 );.SQLITE_API SQ
1c040 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 LITE_EXPERIMENTA
1c050 4c 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f L void *sqlite3_
1c060 70 72 6f 66 69 6c 65 28 73 71 6c 69 74 65 33 2a profile(sqlite3*
1c070 2c 0a 20 20 20 76 6f 69 64 28 2a 78 50 72 6f 66 ,. void(*xProf
1c080 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 ile)(void*,const
1c090 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 75 char*,sqlite3_u
1c0a0 69 6e 74 36 34 29 2c 20 76 6f 69 64 2a 29 3b 0a int64), void*);.
1c0b0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
1c0c0 20 51 75 65 72 79 20 50 72 6f 67 72 65 73 73 20 Query Progress
1c0d0 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 31 Callbacks {H1291
1c0e0 30 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 0} <S60400>.**.*
1c0f0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
1c100 6f 6e 66 69 67 75 72 65 73 20 61 20 63 61 6c 6c onfigures a call
1c110 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2d 20 back function -
1c120 74 68 65 0a 2a 2a 20 70 72 6f 67 72 65 73 73 20 the.** progress
1c130 63 61 6c 6c 62 61 63 6b 20 2d 20 74 68 61 74 20 callback - that
1c140 69 73 20 69 6e 76 6f 6b 65 64 20 70 65 72 69 6f is invoked perio
1c150 64 69 63 61 6c 6c 79 20 64 75 72 69 6e 67 20 6c dically during l
1c160 6f 6e 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 63 ong.** running c
1c170 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 alls to [sqlite3
1c180 5f 65 78 65 63 28 29 5d 2c 20 5b 73 71 6c 69 74 _exec()], [sqlit
1c190 65 33 5f 73 74 65 70 28 29 5d 20 61 6e 64 0a 2a e3_step()] and.*
1c1a0 2a 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 * [sqlite3_get_t
1c1b0 61 62 6c 65 28 29 5d 2e 20 20 41 6e 20 65 78 61 able()]. An exa
1c1c0 6d 70 6c 65 20 75 73 65 20 66 6f 72 20 74 68 69 mple use for thi
1c1d0 73 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 s.** interface i
1c1e0 73 20 74 6f 20 6b 65 65 70 20 61 20 47 55 49 20 s to keep a GUI
1c1f0 75 70 64 61 74 65 64 20 64 75 72 69 6e 67 20 61 updated during a
1c200 20 6c 61 72 67 65 20 71 75 65 72 79 2e 0a 2a 2a large query..**
1c210 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72 .** If the progr
1c220 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 ess callback ret
1c230 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 urns non-zero, t
1c240 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a he operation is.
1c250 2a 2a 20 69 6e 74 65 72 72 75 70 74 65 64 2e 20 ** interrupted.
1c260 20 54 68 69 73 20 66 65 61 74 75 72 65 20 63 61 This feature ca
1c270 6e 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 n be used to imp
1c280 6c 65 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61 6e lement a.** "Can
1c290 63 65 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20 61 cel" button on a
1c2a0 20 47 55 49 20 70 72 6f 67 72 65 73 73 20 64 69 GUI progress di
1c2b0 61 6c 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a 20 alog box..**.**
1c2c0 54 68 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e The progress han
1c2d0 64 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20 64 6f dler must not do
1c2e0 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 anything that w
1c2f0 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 ill modify.** th
1c300 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
1c310 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b ction that invok
1c320 65 64 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 ed the progress
1c330 68 61 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f 74 65 handler..** Note
1c340 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 that [sqlite3_p
1c350 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 repare_v2()] and
1c360 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
1c370 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 ] both modify th
1c380 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 eir.** database
1c390 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 connections for
1c3a0 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 the meaning of "
1c3b0 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 modify" in this
1c3c0 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a paragraph..**.**
1c3d0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
1c3e0 2a 20 5b 48 31 32 39 31 31 5d 20 5b 48 31 32 39 * [H12911] [H129
1c3f0 31 32 5d 20 5b 48 31 32 39 31 33 5d 20 5b 48 31 12] [H12913] [H1
1c400 32 39 31 34 5d 20 5b 48 31 32 39 31 35 5d 20 5b 2914] [H12915] [
1c410 48 31 32 39 31 36 5d 20 5b 48 31 32 39 31 37 5d H12916] [H12917]
1c420 20 5b 48 31 32 39 31 38 5d 0a 2a 2a 0a 2a 2f 0a [H12918].**.*/.
1c430 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
1c440 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 sqlite3_progress
1c450 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33 _handler(sqlite3
1c460 2a 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28 76 *, int, int(*)(v
1c470 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a oid*), void*);..
1c480 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
1c490 4f 70 65 6e 69 6e 67 20 41 20 4e 65 77 20 44 61 Opening A New Da
1c4a0 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f tabase Connectio
1c4b0 6e 20 7b 48 31 32 37 30 30 7d 20 3c 53 34 30 32 n {H12700} <S402
1c4c0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 00>.**.** These
1c4d0 72 6f 75 74 69 6e 65 73 20 6f 70 65 6e 20 61 6e routines open an
1c4e0 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 SQLite database
1c4f0 20 66 69 6c 65 20 77 68 6f 73 65 20 6e 61 6d 65 file whose name
1c500 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65 is given by the
1c510 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 .** filename arg
1c520 75 6d 65 6e 74 2e 20 54 68 65 20 66 69 6c 65 6e ument. The filen
1c530 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ame argument is
1c540 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 55 interpreted as U
1c550 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 TF-8 for.** sqli
1c560 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 te3_open() and s
1c570 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
1c580 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 20 69 and as UTF-16 i
1c590 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 n the native byt
1c5a0 65 0a 2a 2a 20 6f 72 64 65 72 20 66 6f 72 20 73 e.** order for s
1c5b0 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e qlite3_open16().
1c5c0 20 41 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e A [database con
1c5d0 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 20 nection] handle
1c5e0 69 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72 65 is usually.** re
1c5f0 74 75 72 6e 65 64 20 69 6e 20 2a 70 70 44 62 2c turned in *ppDb,
1c600 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f even if an erro
1c610 72 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20 6f r occurs. The o
1c620 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 20 69 73 nly exception is
1c630 20 74 68 61 74 0a 2a 2a 20 69 66 20 53 51 4c 69 that.** if SQLi
1c640 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 te is unable to
1c650 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 allocate memory
1c660 74 6f 20 68 6f 6c 64 20 74 68 65 20 5b 73 71 6c to hold the [sql
1c670 69 74 65 33 5d 20 6f 62 6a 65 63 74 2c 0a 2a 2a ite3] object,.**
1c680 20 61 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 20 a NULL will be
1c690 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 70 written into *pp
1c6a0 44 62 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 Db instead of a
1c6b0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b pointer to the [
1c6c0 73 71 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62 6a 65 sqlite3].** obje
1c6d0 63 74 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 ct. If the datab
1c6e0 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 28 61 ase is opened (a
1c6f0 6e 64 2f 6f 72 20 63 72 65 61 74 65 64 29 20 73 nd/or created) s
1c700 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 74 68 65 uccessfully, the
1c710 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d n.** [SQLITE_OK]
1c720 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f is returned. O
1c730 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 therwise an [err
1c740 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 or code] is retu
1c750 72 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 5b 73 rned. The.** [s
1c760 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d qlite3_errmsg()]
1c770 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 or [sqlite3_err
1c780 6d 73 67 31 36 28 29 5d 20 72 6f 75 74 69 6e 65 msg16()] routine
1c790 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f s can be used to
1c7a0 20 6f 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 45 6e obtain.** an En
1c7b0 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20 64 glish language d
1c7c0 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 escription of th
1c7d0 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 e error..**.** T
1c7e0 68 65 20 64 65 66 61 75 6c 74 20 65 6e 63 6f 64 he default encod
1c7f0 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61 ing for the data
1c800 62 61 73 65 20 77 69 6c 6c 20 62 65 20 55 54 46 base will be UTF
1c810 2d 38 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 -8 if.** sqlite3
1c820 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 _open() or sqlit
1c830 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 e3_open_v2() is
1c840 63 61 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 55 54 called and.** UT
1c850 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 69 F-16 in the nati
1c860 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 69 66 ve byte order if
1c870 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 sqlite3_open16(
1c880 29 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a ) is used..**.**
1c890 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 Whether or not
1c8a0 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 an error occurs
1c8b0 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65 when it is opene
1c8c0 64 2c 20 72 65 73 6f 75 72 63 65 73 0a 2a 2a 20 d, resources.**
1c8d0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
1c8e0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f the [database co
1c8f0 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 nnection] handle
1c900 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c 65 61 should be relea
1c910 73 65 64 20 62 79 0a 2a 2a 20 70 61 73 73 69 6e sed by.** passin
1c920 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 g it to [sqlite3
1c930 5f 63 6c 6f 73 65 28 29 5d 20 77 68 65 6e 20 69 _close()] when i
1c940 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 t is no longer r
1c950 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 equired..**.** T
1c960 68 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f he sqlite3_open_
1c970 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 20 77 v2() interface w
1c980 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 orks like sqlite
1c990 33 5f 6f 70 65 6e 28 29 0a 2a 2a 20 65 78 63 65 3_open().** exce
1c9a0 70 74 20 74 68 61 74 20 69 74 20 61 63 63 65 70 pt that it accep
1c9b0 74 73 20 74 77 6f 20 61 64 64 69 74 69 6f 6e 61 ts two additiona
1c9c0 6c 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72 l parameters for
1c9d0 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6e 74 additional cont
1c9e0 72 6f 6c 0a 2a 2a 20 6f 76 65 72 20 74 68 65 20 rol.** over the
1c9f0 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e new database con
1ca00 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 6c nection. The fl
1ca10 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 63 61 ags parameter ca
1ca20 6e 20 74 61 6b 65 20 6f 6e 65 20 6f 66 0a 2a 2a n take one of.**
1ca30 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 the following t
1ca40 68 72 65 65 20 76 61 6c 75 65 73 2c 20 6f 70 74 hree values, opt
1ca50 69 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e 65 64 ionally combined
1ca60 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 5b 53 with the .** [S
1ca70 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 QLITE_OPEN_NOMUT
1ca80 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 EX], [SQLITE_OPE
1ca90 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c 20 5b 53 N_FULLMUTEX], [S
1caa0 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 QLITE_OPEN_SHARE
1cab0 44 43 41 43 48 45 5d 2c 0a 2a 2a 20 61 6e 64 2f DCACHE],.** and/
1cac0 6f 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f or [SQLITE_OPEN_
1cad0 50 52 49 56 41 54 45 43 41 43 48 45 5d 20 66 6c PRIVATECACHE] fl
1cae0 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a ags:.**.** <dl>.
1caf0 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f ** <dt>[SQLITE_O
1cb00 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c 2f 64 PEN_READONLY]</d
1cb10 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 t>.** <dd>The da
1cb20 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 tabase is opened
1cb30 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f in read-only mo
1cb40 64 65 2e 20 20 49 66 20 74 68 65 20 64 61 74 61 de. If the data
1cb50 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a base does not.**
1cb60 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2c 20 already exist,
1cb70 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 an error is retu
1cb80 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a rned.</dd>.**.**
1cb90 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 <dt>[SQLITE_OPE
1cba0 4e 5f 52 45 41 44 57 52 49 54 45 5d 3c 2f 64 74 N_READWRITE]</dt
1cbb0 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 74 >.** <dd>The dat
1cbc0 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 abase is opened
1cbd0 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20 for reading and
1cbe0 77 72 69 74 69 6e 67 20 69 66 20 70 6f 73 73 69 writing if possi
1cbf0 62 6c 65 2c 20 6f 72 20 72 65 61 64 69 6e 67 0a ble, or reading.
1cc00 2a 2a 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 66 ** only if the f
1cc10 69 6c 65 20 69 73 20 77 72 69 74 65 20 70 72 6f ile is write pro
1cc20 74 65 63 74 65 64 20 62 79 20 74 68 65 20 6f 70 tected by the op
1cc30 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 erating system.
1cc40 20 49 6e 20 65 69 74 68 65 72 0a 2a 2a 20 63 61 In either.** ca
1cc50 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 se the database
1cc60 6d 75 73 74 20 61 6c 72 65 61 64 79 20 65 78 69 must already exi
1cc70 73 74 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e st, otherwise an
1cc80 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e error is return
1cc90 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c ed.</dd>.**.** <
1cca0 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f dt>[SQLITE_OPEN_
1ccb0 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b 53 51 READWRITE] | [SQ
1ccc0 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 LITE_OPEN_CREATE
1ccd0 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 ]</dt>.** <dd>Th
1cce0 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 e database is op
1ccf0 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 ened for reading
1cd00 20 61 6e 64 20 77 72 69 74 69 6e 67 2c 20 61 6e and writing, an
1cd10 64 20 69 73 20 63 72 65 61 74 65 73 20 69 74 20 d is creates it
1cd20 69 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e 6f if.** it does no
1cd30 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e t already exist.
1cd40 20 54 68 69 73 20 69 73 20 74 68 65 20 62 65 68 This is the beh
1cd50 61 76 69 6f 72 20 74 68 61 74 20 69 73 20 61 6c avior that is al
1cd60 77 61 79 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a ways used for.**
1cd70 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 sqlite3_open()
1cd80 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e and sqlite3_open
1cd90 31 36 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 16().</dd>.** </
1cda0 64 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 dl>.**.** If the
1cdb0 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 3rd parameter t
1cdc0 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 o sqlite3_open_v
1cdd0 32 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65 20 6f 2() is not one o
1cde0 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 6e 61 f the.** combina
1cdf0 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f 76 tions shown abov
1ce00 65 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 e or one of the
1ce10 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68 6f combinations sho
1ce20 77 6e 20 61 62 6f 76 65 20 63 6f 6d 62 69 6e 65 wn above combine
1ce30 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 53 d.** with the [S
1ce40 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 QLITE_OPEN_NOMUT
1ce50 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 EX], [SQLITE_OPE
1ce60 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c 0a 2a 2a N_FULLMUTEX],.**
1ce70 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 [SQLITE_OPEN_SH
1ce80 41 52 45 44 43 41 43 48 45 5d 20 61 6e 64 2f 6f AREDCACHE] and/o
1ce90 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 r [SQLITE_OPEN_S
1cea0 48 41 52 45 44 43 41 43 48 45 5d 20 66 6c 61 67 HAREDCACHE] flag
1ceb0 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 62 s,.** then the b
1cec0 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 ehavior is undef
1ced0 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ined..**.** If t
1cee0 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f he [SQLITE_OPEN_
1cef0 4e 4f 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73 NOMUTEX] flag is
1cf00 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 64 set, then the d
1cf10 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1cf20 6f 6e 0a 2a 2a 20 6f 70 65 6e 73 20 69 6e 20 74 on.** opens in t
1cf30 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 20 he multi-thread
1cf40 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d [threading mode]
1cf50 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 as long as the
1cf60 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 0a 2a 2a single-thread.**
1cf70 20 6d 6f 64 65 20 68 61 73 20 6e 6f 74 20 62 65 mode has not be
1cf80 65 6e 20 73 65 74 20 61 74 20 63 6f 6d 70 69 6c en set at compil
1cf90 65 2d 74 69 6d 65 20 6f 72 20 73 74 61 72 74 2d e-time or start-
1cfa0 74 69 6d 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a time. If the.**
1cfb0 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 [SQLITE_OPEN_FU
1cfc0 4c 4c 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73 LLMUTEX] flag is
1cfd0 20 73 65 74 20 74 68 65 6e 20 74 68 65 20 64 61 set then the da
1cfe0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1cff0 6e 20 6f 70 65 6e 73 0a 2a 2a 20 69 6e 20 74 68 n opens.** in th
1d000 65 20 73 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 e serialized [th
1d010 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 75 6e reading mode] un
1d020 6c 65 73 73 20 73 69 6e 67 6c 65 2d 74 68 72 65 less single-thre
1d030 61 64 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f ad was.** previo
1d040 75 73 6c 79 20 73 65 6c 65 63 74 65 64 20 61 74 usly selected at
1d050 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 compile-time or
1d060 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 20 start-time..**
1d070 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e The [SQLITE_OPEN
1d080 5f 53 48 41 52 45 44 43 41 43 48 45 5d 20 66 6c _SHAREDCACHE] fl
1d090 61 67 20 63 61 75 73 65 73 20 74 68 65 20 64 61 ag causes the da
1d0a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1d0b0 6e 20 74 6f 20 62 65 0a 2a 2a 20 65 6c 69 67 69 n to be.** eligi
1d0c0 62 6c 65 20 74 6f 20 75 73 65 20 5b 73 68 61 72 ble to use [shar
1d0d0 65 64 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20 ed cache mode],
1d0e0 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 regardless of wh
1d0f0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 68 61 ether or not sha
1d100 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69 73 20 red.** cache is
1d110 65 6e 61 62 6c 65 64 20 75 73 69 6e 67 20 5b 73 enabled using [s
1d120 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 qlite3_enable_sh
1d130 61 72 65 64 5f 63 61 63 68 65 28 29 5d 2e 20 20 ared_cache()].
1d140 54 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f The.** [SQLITE_O
1d150 50 45 4e 5f 50 52 49 56 41 54 45 43 41 43 48 45 PEN_PRIVATECACHE
1d160 5d 20 66 6c 61 67 20 63 61 75 73 65 73 20 74 68 ] flag causes th
1d170 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
1d180 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 0a 2a 2a 20 ction to not.**
1d190 70 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 5b participate in [
1d1a0 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 shared cache mod
1d1b0 65 5d 20 65 76 65 6e 20 69 66 20 69 74 20 69 73 e] even if it is
1d1c0 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 enabled..**.**
1d1d0 49 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 If the filename
1d1e0 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 2c 20 74 is ":memory:", t
1d1f0 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c 20 74 hen a private, t
1d200 65 6d 70 6f 72 61 72 79 20 69 6e 2d 6d 65 6d 6f emporary in-memo
1d210 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 ry database.** i
1d220 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 74 68 s created for th
1d230 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 e connection. T
1d240 68 69 73 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 his in-memory da
1d250 74 61 62 61 73 65 20 77 69 6c 6c 20 76 61 6e 69 tabase will vani
1d260 73 68 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 64 sh when.** the d
1d270 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1d280 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 46 on is closed. F
1d290 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f uture versions o
1d2a0 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 0a 2a f SQLite might.*
1d2b0 2a 20 6d 61 6b 65 20 75 73 65 20 6f 66 20 61 64 * make use of ad
1d2c0 64 69 74 69 6f 6e 61 6c 20 73 70 65 63 69 61 6c ditional special
1d2d0 20 66 69 6c 65 6e 61 6d 65 73 20 74 68 61 74 20 filenames that
1d2e0 62 65 67 69 6e 20 77 69 74 68 20 74 68 65 20 22 begin with the "
1d2f0 3a 22 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a :" character..**
1d300 20 49 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 It is recommend
1d310 65 64 20 74 68 61 74 20 77 68 65 6e 20 61 20 64 ed that when a d
1d320 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 atabase filename
1d330 20 61 63 74 75 61 6c 6c 79 20 64 6f 65 73 20 62 actually does b
1d340 65 67 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20 22 egin with.** a "
1d350 3a 22 20 63 68 61 72 61 63 74 65 72 20 79 6f 75 :" character you
1d360 20 73 68 6f 75 6c 64 20 70 72 65 66 69 78 20 74 should prefix t
1d370 68 65 20 66 69 6c 65 6e 61 6d 65 20 77 69 74 68 he filename with
1d380 20 61 20 70 61 74 68 6e 61 6d 65 20 73 75 63 68 a pathname such
1d390 20 61 73 0a 2a 2a 20 22 2e 2f 22 20 74 6f 20 61 as.** "./" to a
1d3a0 76 6f 69 64 20 61 6d 62 69 67 75 69 74 79 2e 0a void ambiguity..
1d3b0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6c **.** If the fil
1d3c0 65 6e 61 6d 65 20 69 73 20 61 6e 20 65 6d 70 74 ename is an empt
1d3d0 79 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 61 y string, then a
1d3e0 20 70 72 69 76 61 74 65 2c 20 74 65 6d 70 6f 72 private, tempor
1d3f0 61 72 79 0a 2a 2a 20 6f 6e 2d 64 69 73 6b 20 64 ary.** on-disk d
1d400 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 atabase will be
1d410 63 72 65 61 74 65 64 2e 20 20 54 68 69 73 20 70 created. This p
1d420 72 69 76 61 74 65 20 64 61 74 61 62 61 73 65 20 rivate database
1d430 77 69 6c 6c 20 62 65 0a 2a 2a 20 61 75 74 6f 6d will be.** autom
1d440 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 atically deleted
1d450 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 as soon as the
1d460 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1d470 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a ion is closed..*
1d480 2a 0a 2a 2a 20 54 68 65 20 66 6f 75 72 74 68 20 *.** The fourth
1d490 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c parameter to sql
1d4a0 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 ite3_open_v2() i
1d4b0 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 s the name of th
1d4c0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66 e.** [sqlite3_vf
1d4d0 73 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20 64 s] object that d
1d4e0 65 66 69 6e 65 73 20 74 68 65 20 6f 70 65 72 61 efines the opera
1d4f0 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 ting system inte
1d500 72 66 61 63 65 20 74 68 61 74 0a 2a 2a 20 74 68 rface that.** th
1d510 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 e new database c
1d520 6f 6e 6e 65 63 74 69 6f 6e 20 73 68 6f 75 6c 64 onnection should
1d530 20 75 73 65 2e 20 20 49 66 20 74 68 65 20 66 6f use. If the fo
1d540 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 urth parameter i
1d550 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e s.** a NULL poin
1d560 74 65 72 20 74 68 65 6e 20 74 68 65 20 64 65 66 ter then the def
1d570 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 ault [sqlite3_vf
1d580 73 5d 20 6f 62 6a 65 63 74 20 69 73 20 75 73 65 s] object is use
1d590 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f 74 65 d..**.** <b>Note
1d5a0 20 74 6f 20 57 69 6e 64 6f 77 73 20 75 73 65 72 to Windows user
1d5b0 73 3a 3c 2f 62 3e 20 20 54 68 65 20 65 6e 63 6f s:</b> The enco
1d5c0 64 69 6e 67 20 75 73 65 64 20 66 6f 72 20 74 68 ding used for th
1d5d0 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d e filename argum
1d5e0 65 6e 74 0a 2a 2a 20 6f 66 20 73 71 6c 69 74 65 ent.** of sqlite
1d5f0 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 3_open() and sql
1d600 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 6d ite3_open_v2() m
1d610 75 73 74 20 62 65 20 55 54 46 2d 38 2c 20 6e 6f ust be UTF-8, no
1d620 74 20 77 68 61 74 65 76 65 72 0a 2a 2a 20 63 6f t whatever.** co
1d630 64 65 70 61 67 65 20 69 73 20 63 75 72 72 65 6e depage is curren
1d640 74 6c 79 20 64 65 66 69 6e 65 64 2e 20 20 46 69 tly defined. Fi
1d650 6c 65 6e 61 6d 65 73 20 63 6f 6e 74 61 69 6e 69 lenames containi
1d660 6e 67 20 69 6e 74 65 72 6e 61 74 69 6f 6e 61 6c ng international
1d670 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 6d .** characters m
1d680 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 ust be converted
1d690 20 74 6f 20 55 54 46 2d 38 20 70 72 69 6f 72 20 to UTF-8 prior
1d6a0 74 6f 20 70 61 73 73 69 6e 67 20 74 68 65 6d 20 to passing them
1d6b0 69 6e 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f into.** sqlite3_
1d6c0 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 open() or sqlite
1d6d0 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a 0a 3_open_v2()..**.
1d6e0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
1d6f0 0a 2a 2a 20 5b 48 31 32 37 30 31 5d 20 5b 48 31 .** [H12701] [H1
1d700 32 37 30 32 5d 20 5b 48 31 32 37 30 33 5d 20 5b 2702] [H12703] [
1d710 48 31 32 37 30 34 5d 20 5b 48 31 32 37 30 36 5d H12704] [H12706]
1d720 20 5b 48 31 32 37 30 37 5d 20 5b 48 31 32 37 30 [H12707] [H1270
1d730 39 5d 20 5b 48 31 32 37 31 31 5d 0a 2a 2a 20 5b 9] [H12711].** [
1d740 48 31 32 37 31 32 5d 20 5b 48 31 32 37 31 33 5d H12712] [H12713]
1d750 20 5b 48 31 32 37 31 34 5d 20 5b 48 31 32 37 31 [H12714] [H1271
1d760 37 5d 20 5b 48 31 32 37 31 39 5d 20 5b 48 31 32 7] [H12719] [H12
1d770 37 32 31 5d 20 5b 48 31 32 37 32 33 5d 0a 2a 2f 721] [H12723].*/
1d780 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1d790 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 0a 20 20 sqlite3_open(.
1d7a0 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 const char *file
1d7b0 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 name, /* Datab
1d7c0 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 ase filename (UT
1d7d0 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 F-8) */. sqlite
1d7e0 33 20 2a 2a 70 70 44 62 20 20 20 20 20 20 20 20 3 **ppDb
1d7f0 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 /* OUT: SQLite
1d800 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 29 3b db handle */.);
1d810 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1d820 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 0a sqlite3_open16(.
1d830 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 66 69 const void *fi
1d840 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 lename, /* Dat
1d850 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 abase filename (
1d860 55 54 46 2d 31 36 29 20 2a 2f 0a 20 20 73 71 6c UTF-16) */. sql
1d870 69 74 65 33 20 2a 2a 70 70 44 62 20 20 20 20 20 ite3 **ppDb
1d880 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c /* OUT: SQL
1d890 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f ite db handle */
1d8a0 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .);.SQLITE_API i
1d8b0 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f nt sqlite3_open_
1d8c0 76 32 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 v2(. const char
1d8d0 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a *filename, /*
1d8e0 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 Database filena
1d8f0 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 me (UTF-8) */.
1d900 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c 20 sqlite3 **ppDb,
1d910 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 /* OUT:
1d920 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 SQLite db handle
1d930 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c */. int flags,
1d940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d950 20 46 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 Flags */. cons
1d960 74 20 63 68 61 72 20 2a 7a 56 66 73 20 20 20 20 t char *zVfs
1d970 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 56 /* Name of V
1d980 46 53 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65 FS module to use
1d990 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 */.);../*.** CA
1d9a0 50 49 33 52 45 46 3a 20 45 72 72 6f 72 20 43 6f PI3REF: Error Co
1d9b0 64 65 73 20 41 6e 64 20 4d 65 73 73 61 67 65 73 des And Messages
1d9c0 20 7b 48 31 32 38 30 30 7d 20 3c 53 36 30 32 30 {H12800} <S6020
1d9d0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 0>.**.** The sql
1d9e0 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 20 69 ite3_errcode() i
1d9f0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
1da00 20 74 68 65 20 6e 75 6d 65 72 69 63 20 5b 72 65 the numeric [re
1da10 73 75 6c 74 20 63 6f 64 65 5d 20 6f 72 0a 2a 2a sult code] or.**
1da20 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c [extended resul
1da30 74 20 63 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 t code] for the
1da40 6d 6f 73 74 20 72 65 63 65 6e 74 20 66 61 69 6c most recent fail
1da50 65 64 20 73 71 6c 69 74 65 33 5f 2a 20 41 50 49 ed sqlite3_* API
1da60 20 63 61 6c 6c 0a 2a 2a 20 61 73 73 6f 63 69 61 call.** associa
1da70 74 65 64 20 77 69 74 68 20 61 20 5b 64 61 74 61 ted with a [data
1da80 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
1da90 2e 20 49 66 20 61 20 70 72 69 6f 72 20 41 50 49 . If a prior API
1daa0 20 63 61 6c 6c 20 66 61 69 6c 65 64 0a 2a 2a 20 call failed.**
1dab0 62 75 74 20 74 68 65 20 6d 6f 73 74 20 72 65 63 but the most rec
1dac0 65 6e 74 20 41 50 49 20 63 61 6c 6c 20 73 75 63 ent API call suc
1dad0 63 65 65 64 65 64 2c 20 74 68 65 20 72 65 74 75 ceeded, the retu
1dae0 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 0a 2a 2a rn value from.**
1daf0 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 sqlite3_errcode
1db00 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e () is undefined.
1db10 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 The sqlite3_ex
1db20 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 29 tended_errcode()
1db30 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 .** interface is
1db40 20 74 68 65 20 73 61 6d 65 20 65 78 63 65 70 74 the same except
1db50 20 74 68 61 74 20 69 74 20 61 6c 77 61 79 73 20 that it always
1db60 72 65 74 75 72 6e 73 20 74 68 65 20 0a 2a 2a 20 returns the .**
1db70 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 [extended result
1db80 20 63 6f 64 65 5d 20 65 76 65 6e 20 77 68 65 6e code] even when
1db90 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 extended result
1dba0 20 63 6f 64 65 73 20 61 72 65 0a 2a 2a 20 64 69 codes are.** di
1dbb0 73 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 sabled..**.** Th
1dbc0 65 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 e sqlite3_errmsg
1dbd0 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 () and sqlite3_e
1dbe0 72 72 6d 73 67 31 36 28 29 20 72 65 74 75 72 6e rrmsg16() return
1dbf0 20 45 6e 67 6c 69 73 68 2d 6c 61 6e 67 75 61 67 English-languag
1dc00 65 0a 2a 2a 20 74 65 78 74 20 74 68 61 74 20 64 e.** text that d
1dc10 65 73 63 72 69 62 65 73 20 74 68 65 20 65 72 72 escribes the err
1dc20 6f 72 2c 20 61 73 20 65 69 74 68 65 72 20 55 54 or, as either UT
1dc30 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 72 65 F-8 or UTF-16 re
1dc40 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 4d spectively..** M
1dc50 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 emory to hold th
1dc60 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 e error message
1dc70 73 74 72 69 6e 67 20 69 73 20 6d 61 6e 61 67 65 string is manage
1dc80 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a 2a d internally..**
1dc90 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e The application
1dca0 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
1dcb0 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 66 72 o worry about fr
1dcc0 65 65 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 eeing the result
1dcd0 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 ..** However, th
1dce0 65 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 6d e error string m
1dcf0 69 67 68 74 20 62 65 20 6f 76 65 72 77 72 69 74 ight be overwrit
1dd00 74 65 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 ten or deallocat
1dd10 65 64 20 62 79 0a 2a 2a 20 73 75 62 73 65 71 75 ed by.** subsequ
1dd20 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 6f 74 68 ent calls to oth
1dd30 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 er SQLite interf
1dd40 61 63 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a ace functions..*
1dd50 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 73 65 *.** When the se
1dd60 72 69 61 6c 69 7a 65 64 20 5b 74 68 72 65 61 64 rialized [thread
1dd70 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 69 6e 20 ing mode] is in
1dd80 75 73 65 2c 20 69 74 20 6d 69 67 68 74 20 62 65 use, it might be
1dd90 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 the.** case tha
1dda0 74 20 61 20 73 65 63 6f 6e 64 20 65 72 72 6f 72 t a second error
1ddb0 20 6f 63 63 75 72 73 20 6f 6e 20 61 20 73 65 70 occurs on a sep
1ddc0 61 72 61 74 65 20 74 68 72 65 61 64 20 69 6e 20 arate thread in
1ddd0 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 74 between.** the t
1dde0 69 6d 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 ime of the first
1ddf0 20 65 72 72 6f 72 20 61 6e 64 20 74 68 65 20 63 error and the c
1de00 61 6c 6c 20 74 6f 20 74 68 65 73 65 20 69 6e 74 all to these int
1de10 65 72 66 61 63 65 73 2e 0a 2a 2a 20 57 68 65 6e erfaces..** When
1de20 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c 20 74 that happens, t
1de30 68 65 20 73 65 63 6f 6e 64 20 65 72 72 6f 72 20 he second error
1de40 77 69 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 64 will be reported
1de50 20 73 69 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 since these.**
1de60 69 6e 74 65 72 66 61 63 65 73 20 61 6c 77 61 79 interfaces alway
1de70 73 20 72 65 70 6f 72 74 20 74 68 65 20 6d 6f 73 s report the mos
1de80 74 20 72 65 63 65 6e 74 20 72 65 73 75 6c 74 2e t recent result.
1de90 20 20 54 6f 20 61 76 6f 69 64 0a 2a 2a 20 74 68 To avoid.** th
1dea0 69 73 2c 20 65 61 63 68 20 74 68 72 65 61 64 20 is, each thread
1deb0 63 61 6e 20 6f 62 74 61 69 6e 20 65 78 63 6c 75 can obtain exclu
1dec0 73 69 76 65 20 75 73 65 20 6f 66 20 74 68 65 20 sive use of the
1ded0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
1dee0 74 69 6f 6e 5d 20 44 0a 2a 2a 20 62 79 20 69 6e tion] D.** by in
1def0 76 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f voking [sqlite3_
1df00 6d 75 74 65 78 5f 65 6e 74 65 72 5d 28 5b 73 71 mutex_enter]([sq
1df10 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28 lite3_db_mutex](
1df20 44 29 29 20 62 65 66 6f 72 65 20 62 65 67 69 6e D)) before begin
1df30 6e 69 6e 67 0a 2a 2a 20 74 6f 20 75 73 65 20 44 ning.** to use D
1df40 20 61 6e 64 20 69 6e 76 6f 6b 69 6e 67 20 5b 73 and invoking [s
1df50 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1df60 76 65 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62 5f ve]([sqlite3_db_
1df70 6d 75 74 65 78 5d 28 44 29 29 20 61 66 74 65 72 mutex](D)) after
1df80 0a 2a 2a 20 61 6c 6c 20 63 61 6c 6c 73 20 74 6f .** all calls to
1df90 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 73 20 the interfaces
1dfa0 6c 69 73 74 65 64 20 68 65 72 65 20 61 72 65 20 listed here are
1dfb0 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a completed..**.**
1dfc0 20 49 66 20 61 6e 20 69 6e 74 65 72 66 61 63 65 If an interface
1dfd0 20 66 61 69 6c 73 20 77 69 74 68 20 53 51 4c 49 fails with SQLI
1dfe0 54 45 5f 4d 49 53 55 53 45 2c 20 74 68 61 74 20 TE_MISUSE, that
1dff0 6d 65 61 6e 73 20 74 68 65 20 69 6e 74 65 72 66 means the interf
1e000 61 63 65 0a 2a 2a 20 77 61 73 20 69 6e 76 6f 6b ace.** was invok
1e010 65 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 62 ed incorrectly b
1e020 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f y the applicatio
1e030 6e 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 n. In that case
1e040 2c 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 63 , the.** error c
1e050 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65 20 ode and message
1e060 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 may or may not b
1e070 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 e set..**.** Req
1e080 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
1e090 31 32 38 30 31 5d 20 5b 48 31 32 38 30 32 5d 20 12801] [H12802]
1e0a0 5b 48 31 32 38 30 33 5d 20 5b 48 31 32 38 30 37 [H12803] [H12807
1e0b0 5d 20 5b 48 31 32 38 30 38 5d 20 5b 48 31 32 38 ] [H12808] [H128
1e0c0 30 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 09].*/.SQLITE_AP
1e0d0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 I int sqlite3_er
1e0e0 72 63 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64 rcode(sqlite3 *d
1e0f0 62 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 b);.SQLITE_API i
1e100 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e nt sqlite3_exten
1e110 64 65 64 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 ded_errcode(sqli
1e120 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 te3 *db);.SQLITE
1e130 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 _API const char
1e140 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 *sqlite3_errmsg(
1e150 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 sqlite3*);.SQLIT
1e160 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
1e170 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 *sqlite3_errmsg
1e180 31 36 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 16(sqlite3*);../
1e190 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 *.** CAPI3REF: S
1e1a0 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a QL Statement Obj
1e1b0 65 63 74 20 7b 48 31 33 30 30 30 7d 20 3c 48 31 ect {H13000} <H1
1e1c0 33 30 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 3010>.** KEYWORD
1e1d0 53 3a 20 7b 70 72 65 70 61 72 65 64 20 73 74 61 S: {prepared sta
1e1e0 74 65 6d 65 6e 74 7d 20 7b 70 72 65 70 61 72 65 tement} {prepare
1e1f0 64 20 73 74 61 74 65 6d 65 6e 74 73 7d 0a 2a 2a d statements}.**
1e200 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
1e210 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 of this object r
1e220 65 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 epresents a sing
1e230 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 le SQL statement
1e240 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 ..** This object
1e250 20 69 73 20 76 61 72 69 6f 75 73 6c 79 20 6b 6e is variously kn
1e260 6f 77 6e 20 61 73 20 61 20 22 70 72 65 70 61 72 own as a "prepar
1e270 65 64 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 ed statement" or
1e280 20 61 0a 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 20 a.** "compiled
1e290 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 22 20 6f SQL statement" o
1e2a0 72 20 73 69 6d 70 6c 79 20 61 73 20 61 20 22 73 r simply as a "s
1e2b0 74 61 74 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a 2a tatement"..**.**
1e2c0 20 54 68 65 20 6c 69 66 65 20 6f 66 20 61 20 73 The life of a s
1e2d0 74 61 74 65 6d 65 6e 74 20 6f 62 6a 65 63 74 20 tatement object
1e2e0 67 6f 65 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c goes something l
1e2f0 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 ike this:.**.**
1e300 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 65 <ol>.** <li> Cre
1e310 61 74 65 20 74 68 65 20 6f 62 6a 65 63 74 20 75 ate the object u
1e320 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 sing [sqlite3_pr
1e330 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 61 epare_v2()] or a
1e340 20 72 65 6c 61 74 65 64 0a 2a 2a 20 20 20 20 20 related.**
1e350 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c 6c function..** <l
1e360 69 3e 20 42 69 6e 64 20 76 61 6c 75 65 73 20 74 i> Bind values t
1e370 6f 20 5b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 o [host paramete
1e380 72 73 5d 20 75 73 69 6e 67 20 74 68 65 20 73 71 rs] using the sq
1e390 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a lite3_bind_*().*
1e3a0 2a 20 20 20 20 20 20 69 6e 74 65 72 66 61 63 65 * interface
1e3b0 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 74 s..** <li> Run t
1e3c0 68 65 20 53 51 4c 20 62 79 20 63 61 6c 6c 69 6e he SQL by callin
1e3d0 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 g [sqlite3_step(
1e3e0 29 5d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 )] one or more t
1e3f0 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 65 imes..** <li> Re
1e400 73 65 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e set the statemen
1e410 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 t using [sqlite3
1e420 5f 72 65 73 65 74 28 29 5d 20 74 68 65 6e 20 67 _reset()] then g
1e430 6f 20 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 74 o back.** t
1e440 6f 20 73 74 65 70 20 32 2e 20 20 44 6f 20 74 68 o step 2. Do th
1e450 69 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 is zero or more
1e460 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 44 times..** <li> D
1e470 65 73 74 72 6f 79 20 74 68 65 20 6f 62 6a 65 63 estroy the objec
1e480 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 t using [sqlite3
1e490 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a _finalize()]..**
1e4a0 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 66 </ol>.**.** Ref
1e4b0 65 72 20 74 6f 20 64 6f 63 75 6d 65 6e 74 61 74 er to documentat
1e4c0 69 6f 6e 20 6f 6e 20 69 6e 64 69 76 69 64 75 61 ion on individua
1e4d0 6c 20 6d 65 74 68 6f 64 73 20 61 62 6f 76 65 20 l methods above
1e4e0 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a for additional.*
1e4f0 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a * information..*
1e500 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
1e510 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 73 71 sqlite3_stmt sq
1e520 6c 69 74 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a 0a lite3_stmt;../*.
1e530 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e ** CAPI3REF: Run
1e540 2d 74 69 6d 65 20 4c 69 6d 69 74 73 20 7b 48 31 -time Limits {H1
1e550 32 37 36 30 7d 20 3c 53 32 30 36 30 30 3e 0a 2a 2760} <S20600>.*
1e560 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 *.** This interf
1e570 61 63 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 73 ace allows the s
1e580 69 7a 65 20 6f 66 20 76 61 72 69 6f 75 73 20 63 ize of various c
1e590 6f 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65 20 onstructs to be
1e5a0 6c 69 6d 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 20 limited.** on a
1e5b0 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 79 20 63 6f connection by co
1e5c0 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 73 2e 20 nnection basis.
1e5d0 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d The first param
1e5e0 65 74 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 5b eter is the.** [
1e5f0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1e600 69 6f 6e 5d 20 77 68 6f 73 65 20 6c 69 6d 69 74 ion] whose limit
1e610 20 69 73 20 74 6f 20 62 65 20 73 65 74 20 6f 72 is to be set or
1e620 20 71 75 65 72 69 65 64 2e 20 20 54 68 65 0a 2a queried. The.*
1e630 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 * second paramet
1e640 65 72 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 er is one of the
1e650 20 5b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 [limit categori
1e660 65 73 5d 20 74 68 61 74 20 64 65 66 69 6e 65 20 es] that define
1e670 61 0a 2a 2a 20 63 6c 61 73 73 20 6f 66 20 63 6f a.** class of co
1e680 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65 20 73 nstructs to be s
1e690 69 7a 65 20 6c 69 6d 69 74 65 64 2e 20 20 54 68 ize limited. Th
1e6a0 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 e third paramete
1e6b0 72 20 69 73 20 74 68 65 0a 2a 2a 20 6e 65 77 20 r is the.** new
1e6c0 6c 69 6d 69 74 20 66 6f 72 20 74 68 61 74 20 63 limit for that c
1e6d0 6f 6e 73 74 72 75 63 74 2e 20 20 54 68 65 20 66 onstruct. The f
1e6e0 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
1e6f0 74 68 65 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a 2a the old limit..*
1e700 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 65 77 20 *.** If the new
1e710 6c 69 6d 69 74 20 69 73 20 61 20 6e 65 67 61 74 limit is a negat
1e720 69 76 65 20 6e 75 6d 62 65 72 2c 20 74 68 65 20 ive number, the
1e730 6c 69 6d 69 74 20 69 73 20 75 6e 63 68 61 6e 67 limit is unchang
1e740 65 64 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6c ed..** For the l
1e750 69 6d 69 74 20 63 61 74 65 67 6f 72 79 20 6f 66 imit category of
1e760 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 58 59 SQLITE_LIMIT_XY
1e770 5a 20 74 68 65 72 65 20 69 73 20 61 20 0a 2a 2a Z there is a .**
1e780 20 5b 6c 69 6d 69 74 73 20 7c 20 68 61 72 64 20 [limits | hard
1e790 75 70 70 65 72 20 62 6f 75 6e 64 5d 0a 2a 2a 20 upper bound].**
1e7a0 73 65 74 20 62 79 20 61 20 63 6f 6d 70 69 6c 65 set by a compile
1e7b0 2d 74 69 6d 65 20 43 20 70 72 65 70 72 6f 63 65 -time C preproce
1e7c0 73 73 6f 72 20 6d 61 63 72 6f 20 6e 61 6d 65 64 ssor macro named
1e7d0 20 0a 2a 2a 20 5b 6c 69 6d 69 74 73 20 7c 20 53 .** [limits | S
1e7e0 51 4c 49 54 45 5f 4d 41 58 5f 58 59 5a 5d 2e 0a QLITE_MAX_XYZ]..
1e7f0 2a 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 54 5f ** (The "_LIMIT_
1e800 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 69 73 " in the name is
1e810 20 63 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d 41 changed to "_MA
1e820 58 5f 22 2e 29 0a 2a 2a 20 41 74 74 65 6d 70 74 X_".).** Attempt
1e830 73 20 74 6f 20 69 6e 63 72 65 61 73 65 20 61 20 s to increase a
1e840 6c 69 6d 69 74 20 61 62 6f 76 65 20 69 74 73 20 limit above its
1e850 68 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64 hard upper bound
1e860 20 61 72 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c 79 are.** silently
1e870 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 74 68 truncated to th
1e880 65 20 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d e hard upper lim
1e890 69 74 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74 69 it..**.** Run ti
1e8a0 6d 65 20 6c 69 6d 69 74 73 20 61 72 65 20 69 6e me limits are in
1e8b0 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 tended for use i
1e8c0 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 n applications t
1e8d0 68 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 62 6f hat manage.** bo
1e8e0 74 68 20 74 68 65 69 72 20 6f 77 6e 20 69 6e 74 th their own int
1e8f0 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 20 61 ernal database a
1e900 6e 64 20 61 6c 73 6f 20 64 61 74 61 62 61 73 65 nd also database
1e910 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74 72 s that are contr
1e920 6f 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e 74 72 olled.** by untr
1e930 75 73 74 65 64 20 65 78 74 65 72 6e 61 6c 20 73 usted external s
1e940 6f 75 72 63 65 73 2e 20 20 41 6e 20 65 78 61 6d ources. An exam
1e950 70 6c 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 ple application
1e960 6d 69 67 68 74 20 62 65 20 61 0a 2a 2a 20 77 65 might be a.** we
1e970 62 20 62 72 6f 77 73 65 72 20 74 68 61 74 20 68 b browser that h
1e980 61 73 20 69 74 73 20 6f 77 6e 20 64 61 74 61 62 as its own datab
1e990 61 73 65 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 ases for storing
1e9a0 20 68 69 73 74 6f 72 79 20 61 6e 64 0a 2a 2a 20 history and.**
1e9b0 73 65 70 61 72 61 74 65 20 64 61 74 61 62 61 73 separate databas
1e9c0 65 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 es controlled by
1e9d0 20 4a 61 76 61 53 63 72 69 70 74 20 61 70 70 6c JavaScript appl
1e9e0 69 63 61 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f 61 ications downloa
1e9f0 64 65 64 0a 2a 2a 20 6f 66 66 20 74 68 65 20 49 ded.** off the I
1ea00 6e 74 65 72 6e 65 74 2e 20 20 54 68 65 20 69 6e nternet. The in
1ea10 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 73 ternal databases
1ea20 20 63 61 6e 20 62 65 20 67 69 76 65 6e 20 74 68 can be given th
1ea30 65 0a 2a 2a 20 6c 61 72 67 65 2c 20 64 65 66 61 e.** large, defa
1ea40 75 6c 74 20 6c 69 6d 69 74 73 2e 20 20 44 61 74 ult limits. Dat
1ea50 61 62 61 73 65 73 20 6d 61 6e 61 67 65 64 20 62 abases managed b
1ea60 79 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 y external sourc
1ea70 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20 67 69 76 es can.** be giv
1ea80 65 6e 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 en much smaller
1ea90 6c 69 6d 69 74 73 20 64 65 73 69 67 6e 65 64 20 limits designed
1eaa0 74 6f 20 70 72 65 76 65 6e 74 20 61 20 64 65 6e to prevent a den
1eab0 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65 0a 2a ial of service.*
1eac0 2a 20 61 74 74 61 63 6b 2e 20 20 44 65 76 65 6c * attack. Devel
1ead0 6f 70 65 72 73 20 6d 69 67 68 74 20 61 6c 73 6f opers might also
1eae0 20 77 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 want to use the
1eaf0 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 [sqlite3_set_au
1eb00 74 68 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a 20 69 thorizer()].** i
1eb10 6e 74 65 72 66 61 63 65 20 74 6f 20 66 75 72 74 nterface to furt
1eb20 68 65 72 20 63 6f 6e 74 72 6f 6c 20 75 6e 74 72 her control untr
1eb30 75 73 74 65 64 20 53 51 4c 2e 20 20 54 68 65 20 usted SQL. The
1eb40 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 size of the data
1eb50 62 61 73 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 base.** created
1eb60 62 79 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20 by an untrusted
1eb70 73 63 72 69 70 74 20 63 61 6e 20 62 65 20 63 6f script can be co
1eb80 6e 74 61 69 6e 65 64 20 75 73 69 6e 67 20 74 68 ntained using th
1eb90 65 0a 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 5f 63 e.** [max_page_c
1eba0 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 2e 0a ount] [PRAGMA]..
1ebb0 2a 2a 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d 74 69 **.** New run-ti
1ebc0 6d 65 20 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 me limit categor
1ebd0 69 65 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 ies may be added
1ebe0 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 in future relea
1ebf0 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ses..**.** Requi
1ec00 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 rements:.** [H12
1ec10 37 36 32 5d 20 5b 48 31 32 37 36 36 5d 20 5b 48 762] [H12766] [H
1ec20 31 32 37 36 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 12769].*/.SQLITE
1ec30 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1ec40 5f 6c 69 6d 69 74 28 73 71 6c 69 74 65 33 2a 2c _limit(sqlite3*,
1ec50 20 69 6e 74 20 69 64 2c 20 69 6e 74 20 6e 65 77 int id, int new
1ec60 56 61 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 Val);../*.** CAP
1ec70 49 33 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20 I3REF: Run-Time
1ec80 4c 69 6d 69 74 20 43 61 74 65 67 6f 72 69 65 73 Limit Categories
1ec90 20 7b 48 31 32 37 39 30 7d 20 3c 48 31 32 37 36 {H12790} <H1276
1eca0 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 0>.** KEYWORDS:
1ecb0 7b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 79 7d {limit category}
1ecc0 20 7b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 {limit categori
1ecd0 65 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 es}.**.** These
1ece0 63 6f 6e 73 74 61 6e 74 73 20 64 65 66 69 6e 65 constants define
1ecf0 20 76 61 72 69 6f 75 73 20 70 65 72 66 6f 72 6d various perform
1ed00 61 6e 63 65 20 6c 69 6d 69 74 73 0a 2a 2a 20 74 ance limits.** t
1ed10 68 61 74 20 63 61 6e 20 62 65 20 6c 6f 77 65 72 hat can be lower
1ed20 65 64 20 61 74 20 72 75 6e 2d 74 69 6d 65 20 75 ed at run-time u
1ed30 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69 sing [sqlite3_li
1ed40 6d 69 74 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 mit()]..** The s
1ed50 79 6e 6f 70 73 69 73 20 6f 66 20 74 68 65 20 6d ynopsis of the m
1ed60 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76 eanings of the v
1ed70 61 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 69 73 arious limits is
1ed80 20 73 68 6f 77 6e 20 62 65 6c 6f 77 2e 0a 2a 2a shown below..**
1ed90 20 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f Additional info
1eda0 72 6d 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c rmation is avail
1edb0 61 62 6c 65 20 61 74 20 5b 6c 69 6d 69 74 73 20 able at [limits
1edc0 7c 20 4c 69 6d 69 74 73 20 69 6e 20 53 51 4c 69 | Limits in SQLi
1edd0 74 65 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a te]..**.** <dl>.
1ede0 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 ** <dt>SQLITE_LI
1edf0 4d 49 54 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a MIT_LENGTH</dt>.
1ee00 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d ** <dd>The maxim
1ee10 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 73 um size of any s
1ee20 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 6f 72 tring or BLOB or
1ee30 20 74 61 62 6c 65 20 72 6f 77 2e 3c 64 64 3e 0a table row.<dd>.
1ee40 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
1ee50 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 _LIMIT_SQL_LENGT
1ee60 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 H</dt>.** <dd>Th
1ee70 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 e maximum length
1ee80 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 65 of an SQL state
1ee90 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a ment.</dd>.**.**
1eea0 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 <dt>SQLITE_LIMI
1eeb0 54 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a 2a 2a T_COLUMN</dt>.**
1eec0 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d <dd>The maximum
1eed0 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
1eee0 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 20 64 65 ns in a table de
1eef0 66 69 6e 69 74 69 6f 6e 20 6f 72 20 69 6e 20 74 finition or in t
1ef00 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 he.** result set
1ef10 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 6f of a [SELECT] o
1ef20 72 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 r the maximum nu
1ef30 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
1ef40 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 6f in an index.** o
1ef50 72 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 r in an ORDER BY
1ef60 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 or GROUP BY cla
1ef70 75 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 use.</dd>.**.**
1ef80 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 <dt>SQLITE_LIMIT
1ef90 5f 45 58 50 52 5f 44 45 50 54 48 3c 2f 64 74 3e _EXPR_DEPTH</dt>
1efa0 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 .** <dd>The maxi
1efb0 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65 mum depth of the
1efc0 20 70 61 72 73 65 20 74 72 65 65 20 6f 6e 20 61 parse tree on a
1efd0 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 3c 2f ny expression.</
1efe0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
1eff0 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f LITE_LIMIT_COMPO
1f000 55 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e 0a UND_SELECT</dt>.
1f010 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d ** <dd>The maxim
1f020 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 um number of ter
1f030 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 ms in a compound
1f040 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
1f050 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 t.</dd>.**.** <d
1f060 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 t>SQLITE_LIMIT_V
1f070 44 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20 3c DBE_OP</dt>.** <
1f080 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e dd>The maximum n
1f090 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 63 umber of instruc
1f0a0 74 69 6f 6e 73 20 69 6e 20 61 20 76 69 72 74 75 tions in a virtu
1f0b0 61 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67 72 al machine progr
1f0c0 61 6d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6d am.** used to im
1f0d0 70 6c 65 6d 65 6e 74 20 61 6e 20 53 51 4c 20 73 plement an SQL s
1f0e0 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a tatement.</dd>.*
1f0f0 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
1f100 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 LIMIT_FUNCTION_A
1f110 52 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 RG</dt>.** <dd>T
1f120 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 he maximum numbe
1f130 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6f r of arguments o
1f140 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f 64 n a function.</d
1f150 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
1f160 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 ITE_LIMIT_ATTACH
1f170 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 ED</dt>.** <dd>T
1f180 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 he maximum numbe
1f190 72 20 6f 66 20 5b 41 54 54 41 43 48 20 7c 20 61 r of [ATTACH | a
1f1a0 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 ttached database
1f1b0 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c s].</dd>.**.** <
1f1c0 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f dt>SQLITE_LIMIT_
1f1d0 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e LIKE_PATTERN_LEN
1f1e0 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e GTH</dt>.** <dd>
1f1f0 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 The maximum leng
1f200 74 68 20 6f 66 20 74 68 65 20 70 61 74 74 65 72 th of the patter
1f210 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 n argument to th
1f220 65 20 5b 4c 49 4b 45 5d 20 6f 72 0a 2a 2a 20 5b e [LIKE] or.** [
1f230 47 4c 4f 42 5d 20 6f 70 65 72 61 74 6f 72 73 2e GLOB] operators.
1f240 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
1f250 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 SQLITE_LIMIT_VAR
1f260 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64 74 IABLE_NUMBER</dt
1f270 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 >.** <dd>The max
1f280 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 76 imum number of v
1f290 61 72 69 61 62 6c 65 73 20 69 6e 20 61 6e 20 53 ariables in an S
1f2a0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 QL statement tha
1f2b0 74 20 63 61 6e 0a 2a 2a 20 62 65 20 62 6f 75 6e t can.** be boun
1f2c0 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 d.</dd>.**.** <d
1f2d0 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 t>SQLITE_LIMIT_T
1f2e0 52 49 47 47 45 52 5f 44 45 50 54 48 3c 2f 64 74 RIGGER_DEPTH</dt
1f2f0 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 >.** <dd>The max
1f300 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 72 65 imum depth of re
1f310 63 75 72 73 69 6f 6e 20 66 6f 72 20 74 72 69 67 cursion for trig
1f320 67 65 72 73 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f gers.</dd>.** </
1f330 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 dl>.*/.#define S
1f340 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
1f350 54 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 TH
1f360 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 0.#define
1f370 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c SQLITE_LIMIT_SQL
1f380 5f 4c 45 4e 47 54 48 20 20 20 20 20 20 20 20 20 _LENGTH
1f390 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 1.#define
1f3a0 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f SQLITE_LIMIT_CO
1f3b0 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20 LUMN
1f3c0 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 2.#defin
1f3d0 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 e SQLITE_LIMIT_E
1f3e0 58 50 52 5f 44 45 50 54 48 20 20 20 20 20 20 20 XPR_DEPTH
1f3f0 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 3.#defi
1f400 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f ne SQLITE_LIMIT_
1f410 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 COMPOUND_SELECT
1f420 20 20 20 20 20 20 20 20 20 20 34 0a 23 64 65 66 4.#def
1f430 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 ine SQLITE_LIMIT
1f440 5f 56 44 42 45 5f 4f 50 20 20 20 20 20 20 20 20 _VDBE_OP
1f450 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 5.#de
1f460 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 fine SQLITE_LIMI
1f470 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 20 T_FUNCTION_ARG
1f480 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 6.#d
1f490 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d efine SQLITE_LIM
1f4a0 49 54 5f 41 54 54 41 43 48 45 44 20 20 20 20 20 IT_ATTACHED
1f4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 7.#
1f4c0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 define SQLITE_LI
1f4d0 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e MIT_LIKE_PATTERN
1f4e0 5f 4c 45 4e 47 54 48 20 20 20 20 20 20 20 38 0a _LENGTH 8.
1f4f0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
1f500 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 IMIT_VARIABLE_NU
1f510 4d 42 45 52 20 20 20 20 20 20 20 20 20 20 20 39 MBER 9
1f520 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1f530 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 LIMIT_TRIGGER_DE
1f540 50 54 48 20 20 20 20 20 20 20 20 20 20 20 20 31 PTH 1
1f550 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 0../*.** CAPI3RE
1f560 46 3a 20 43 6f 6d 70 69 6c 69 6e 67 20 41 6e 20 F: Compiling An
1f570 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 SQL Statement {H
1f580 31 33 30 31 30 7d 20 3c 53 31 30 30 30 30 3e 0a 13010} <S10000>.
1f590 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 ** KEYWORDS: {SQ
1f5a0 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 L statement comp
1f5b0 69 6c 65 72 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20 65 iler}.**.** To e
1f5c0 78 65 63 75 74 65 20 61 6e 20 53 51 4c 20 71 75 xecute an SQL qu
1f5d0 65 72 79 2c 20 69 74 20 6d 75 73 74 20 66 69 72 ery, it must fir
1f5e0 73 74 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 69 st be compiled i
1f5f0 6e 74 6f 20 61 20 62 79 74 65 2d 63 6f 64 65 0a nto a byte-code.
1f600 2a 2a 20 70 72 6f 67 72 61 6d 20 75 73 69 6e 67 ** program using
1f610 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 72 6f one of these ro
1f620 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 utines..**.** Th
1f630 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
1f640 2c 20 22 64 62 22 2c 20 69 73 20 61 20 5b 64 61 , "db", is a [da
1f650 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1f660 6e 5d 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d n] obtained from
1f670 20 61 0a 2a 2a 20 70 72 69 6f 72 20 73 75 63 63 a.** prior succ
1f680 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b essful call to [
1f690 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c sqlite3_open()],
1f6a0 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 [sqlite3_open_v
1f6b0 32 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 2()] or.** [sqli
1f6c0 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 20 20 te3_open16()].
1f6d0 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e The database con
1f6e0 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 nection must not
1f6f0 20 68 61 76 65 20 62 65 65 6e 20 63 6c 6f 73 65 have been close
1f700 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 d..**.** The sec
1f710 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 22 7a ond argument, "z
1f720 53 71 6c 22 2c 20 69 73 20 74 68 65 20 73 74 61 Sql", is the sta
1f730 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 63 6f 6d tement to be com
1f740 70 69 6c 65 64 2c 20 65 6e 63 6f 64 65 64 0a 2a piled, encoded.*
1f750 2a 20 61 73 20 65 69 74 68 65 72 20 55 54 46 2d * as either UTF-
1f760 38 20 6f 72 20 55 54 46 2d 31 36 2e 20 20 54 68 8 or UTF-16. Th
1f770 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 e sqlite3_prepar
1f780 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f e() and sqlite3_
1f790 70 72 65 70 61 72 65 5f 76 32 28 29 0a 2a 2a 20 prepare_v2().**
1f7a0 69 6e 74 65 72 66 61 63 65 73 20 75 73 65 20 55 interfaces use U
1f7b0 54 46 2d 38 2c 20 61 6e 64 20 73 71 6c 69 74 65 TF-8, and sqlite
1f7c0 33 5f 70 72 65 70 61 72 65 31 36 28 29 20 61 6e 3_prepare16() an
1f7d0 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 d sqlite3_prepar
1f7e0 65 31 36 5f 76 32 28 29 0a 2a 2a 20 75 73 65 20 e16_v2().** use
1f7f0 55 54 46 2d 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 UTF-16..**.** If
1f800 20 74 68 65 20 6e 42 79 74 65 20 61 72 67 75 6d the nByte argum
1f810 65 6e 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e ent is less than
1f820 20 7a 65 72 6f 2c 20 74 68 65 6e 20 7a 53 71 6c zero, then zSql
1f830 20 69 73 20 72 65 61 64 20 75 70 20 74 6f 20 74 is read up to t
1f840 68 65 0a 2a 2a 20 66 69 72 73 74 20 7a 65 72 6f he.** first zero
1f850 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 49 66 20 terminator. If
1f860 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e 65 67 nByte is non-neg
1f870 61 74 69 76 65 2c 20 74 68 65 6e 20 69 74 20 69 ative, then it i
1f880 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a s the maximum.**
1f890 20 6e 75 6d 62 65 72 20 6f 66 20 20 62 79 74 65 number of byte
1f8a0 73 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c s read from zSql
1f8b0 2e 20 20 57 68 65 6e 20 6e 42 79 74 65 20 69 73 . When nByte is
1f8c0 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 non-negative, t
1f8d0 68 65 0a 2a 2a 20 7a 53 71 6c 20 73 74 72 69 6e he.** zSql strin
1f8e0 67 20 65 6e 64 73 20 61 74 20 65 69 74 68 65 72 g ends at either
1f8f0 20 74 68 65 20 66 69 72 73 74 20 27 5c 30 30 30 the first '\000
1f900 27 20 6f 72 20 27 5c 75 30 30 30 30 27 20 63 68 ' or '\u0000' ch
1f910 61 72 61 63 74 65 72 20 6f 72 0a 2a 2a 20 74 68 aracter or.** th
1f920 65 20 6e 42 79 74 65 2d 74 68 20 62 79 74 65 2c e nByte-th byte,
1f930 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 whichever comes
1f940 20 66 69 72 73 74 2e 20 49 66 20 74 68 65 20 63 first. If the c
1f950 61 6c 6c 65 72 20 6b 6e 6f 77 73 0a 2a 2a 20 74 aller knows.** t
1f960 68 61 74 20 74 68 65 20 73 75 70 70 6c 69 65 64 hat the supplied
1f970 20 73 74 72 69 6e 67 20 69 73 20 6e 75 6c 2d 74 string is nul-t
1f980 65 72 6d 69 6e 61 74 65 64 2c 20 74 68 65 6e 20 erminated, then
1f990 74 68 65 72 65 20 69 73 20 61 20 73 6d 61 6c 6c there is a small
1f9a0 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 .** performance
1f9b0 61 64 76 61 6e 74 61 67 65 20 74 6f 20 62 65 20 advantage to be
1f9c0 67 61 69 6e 65 64 20 62 79 20 70 61 73 73 69 6e gained by passin
1f9d0 67 20 61 6e 20 6e 42 79 74 65 20 70 61 72 61 6d g an nByte param
1f9e0 65 74 65 72 20 74 68 61 74 0a 2a 2a 20 69 73 20 eter that.** is
1f9f0 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6e 75 6d equal to the num
1fa00 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
1fa10 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 the input string
1fa20 20 3c 69 3e 69 6e 63 6c 75 64 69 6e 67 3c 2f 69 <i>including</i
1fa30 3e 0a 2a 2a 20 74 68 65 20 6e 75 6c 2d 74 65 72 >.** the nul-ter
1fa40 6d 69 6e 61 74 6f 72 20 62 79 74 65 73 2e 0a 2a minator bytes..*
1fa50 2a 0a 2a 2a 20 49 66 20 70 7a 54 61 69 6c 20 69 *.** If pzTail i
1fa60 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 s not NULL then
1fa70 2a 70 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20 *pzTail is made
1fa80 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 to point to the
1fa90 66 69 72 73 74 20 62 79 74 65 0a 2a 2a 20 70 61 first byte.** pa
1faa0 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 st the end of th
1fab0 65 20 66 69 72 73 74 20 53 51 4c 20 73 74 61 74 e first SQL stat
1fac0 65 6d 65 6e 74 20 69 6e 20 7a 53 71 6c 2e 20 20 ement in zSql.
1fad0 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f These routines o
1fae0 6e 6c 79 0a 2a 2a 20 63 6f 6d 70 69 6c 65 20 74 nly.** compile t
1faf0 68 65 20 66 69 72 73 74 20 73 74 61 74 65 6d 65 he first stateme
1fb00 6e 74 20 69 6e 20 7a 53 71 6c 2c 20 73 6f 20 2a nt in zSql, so *
1fb10 70 7a 54 61 69 6c 20 69 73 20 6c 65 66 74 20 70 pzTail is left p
1fb20 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 77 68 ointing to.** wh
1fb30 61 74 20 72 65 6d 61 69 6e 73 20 75 6e 63 6f 6d at remains uncom
1fb40 70 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 piled..**.** *pp
1fb50 53 74 6d 74 20 69 73 20 6c 65 66 74 20 70 6f 69 Stmt is left poi
1fb60 6e 74 69 6e 67 20 74 6f 20 61 20 63 6f 6d 70 69 nting to a compi
1fb70 6c 65 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 led [prepared st
1fb80 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 63 61 atement] that ca
1fb90 6e 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 n be.** executed
1fba0 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
1fbb0 73 74 65 70 28 29 5d 2e 20 20 49 66 20 74 68 65 step()]. If the
1fbc0 72 65 20 69 73 20 61 6e 20 65 72 72 6f 72 2c 20 re is an error,
1fbd0 2a 70 70 53 74 6d 74 20 69 73 20 73 65 74 0a 2a *ppStmt is set.*
1fbe0 2a 20 74 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 74 * to NULL. If t
1fbf0 68 65 20 69 6e 70 75 74 20 74 65 78 74 20 63 6f he input text co
1fc00 6e 74 61 69 6e 73 20 6e 6f 20 53 51 4c 20 28 69 ntains no SQL (i
1fc10 66 20 74 68 65 20 69 6e 70 75 74 20 69 73 20 61 f the input is a
1fc20 6e 20 65 6d 70 74 79 0a 2a 2a 20 73 74 72 69 6e n empty.** strin
1fc30 67 20 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 29 20 g or a comment)
1fc40 74 68 65 6e 20 2a 70 70 53 74 6d 74 20 69 73 20 then *ppStmt is
1fc50 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 set to NULL..**
1fc60 54 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 The calling proc
1fc70 65 64 75 72 65 20 69 73 20 72 65 73 70 6f 6e 73 edure is respons
1fc80 69 62 6c 65 20 66 6f 72 20 64 65 6c 65 74 69 6e ible for deletin
1fc90 67 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 0a 2a g the compiled.*
1fca0 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 * SQL statement
1fcb0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 using [sqlite3_f
1fcc0 69 6e 61 6c 69 7a 65 28 29 5d 20 61 66 74 65 72 inalize()] after
1fcd0 20 69 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 it has finished
1fce0 20 77 69 74 68 20 69 74 2e 0a 2a 2a 20 70 70 53 with it..** ppS
1fcf0 74 6d 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 4e tmt may not be N
1fd00 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 ULL..**.** On su
1fd10 63 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f ccess, [SQLITE_O
1fd20 4b 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 K] is returned,
1fd30 6f 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 otherwise an [er
1fd40 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 ror code] is ret
1fd50 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 urned..**.** The
1fd60 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
1fd70 5f 76 32 28 29 20 61 6e 64 20 73 71 6c 69 74 65 _v2() and sqlite
1fd80 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 3_prepare16_v2()
1fd90 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 0a interfaces are.
1fda0 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 ** recommended f
1fdb0 6f 72 20 61 6c 6c 20 6e 65 77 20 70 72 6f 67 72 or all new progr
1fdc0 61 6d 73 2e 20 54 68 65 20 74 77 6f 20 6f 6c 64 ams. The two old
1fdd0 65 72 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 er interfaces ar
1fde0 65 20 72 65 74 61 69 6e 65 64 0a 2a 2a 20 66 6f e retained.** fo
1fdf0 72 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 r backwards comp
1fe00 61 74 69 62 69 6c 69 74 79 2c 20 62 75 74 20 74 atibility, but t
1fe10 68 65 69 72 20 75 73 65 20 69 73 20 64 69 73 63 heir use is disc
1fe20 6f 75 72 61 67 65 64 2e 0a 2a 2a 20 49 6e 20 74 ouraged..** In t
1fe30 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 he "v2" interfac
1fe40 65 73 2c 20 74 68 65 20 70 72 65 70 61 72 65 64 es, the prepared
1fe50 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 68 statement.** th
1fe60 61 74 20 69 73 20 72 65 74 75 72 6e 65 64 20 28 at is returned (
1fe70 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d the [sqlite3_stm
1fe80 74 5d 20 6f 62 6a 65 63 74 29 20 63 6f 6e 74 61 t] object) conta
1fe90 69 6e 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 ins a copy of th
1fea0 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 53 51 e.** original SQ
1feb0 4c 20 74 65 78 74 2e 20 54 68 69 73 20 63 61 75 L text. This cau
1fec0 73 65 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33 ses the [sqlite3
1fed0 5f 73 74 65 70 28 29 5d 20 69 6e 74 65 72 66 61 _step()] interfa
1fee0 63 65 20 74 6f 0a 2a 2a 20 62 65 68 61 76 65 20 ce to.** behave
1fef0 61 20 64 69 66 66 65 72 65 6e 74 6c 79 20 69 6e a differently in
1ff00 20 74 68 72 65 65 20 77 61 79 73 3a 0a 2a 2a 0a three ways:.**.
1ff10 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 0a ** <ol>.** <li>.
1ff20 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 ** If the databa
1ff30 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 se schema change
1ff40 73 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65 s, instead of re
1ff50 74 75 72 6e 69 6e 67 20 5b 53 51 4c 49 54 45 5f turning [SQLITE_
1ff60 53 43 48 45 4d 41 5d 20 61 73 20 69 74 0a 2a 2a SCHEMA] as it.**
1ff70 20 61 6c 77 61 79 73 20 75 73 65 64 20 74 6f 20 always used to
1ff80 64 6f 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 do, [sqlite3_ste
1ff90 70 28 29 5d 20 77 69 6c 6c 20 61 75 74 6f 6d 61 p()] will automa
1ffa0 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69 6c tically recompil
1ffb0 65 20 74 68 65 20 53 51 4c 0a 2a 2a 20 73 74 61 e the SQL.** sta
1ffc0 74 65 6d 65 6e 74 20 61 6e 64 20 74 72 79 20 74 tement and try t
1ffd0 6f 20 72 75 6e 20 69 74 20 61 67 61 69 6e 2e 20 o run it again.
1ffe0 20 49 66 20 74 68 65 20 73 63 68 65 6d 61 20 68 If the schema h
1fff0 61 73 20 63 68 61 6e 67 65 64 20 69 6e 0a 2a 2a as changed in.**
20000 20 61 20 77 61 79 20 74 68 61 74 20 6d 61 6b 65 a way that make
20010 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 s the statement
20020 6e 6f 20 6c 6f 6e 67 65 72 20 76 61 6c 69 64 2c no longer valid,
20030 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
20040 5d 20 77 69 6c 6c 20 73 74 69 6c 6c 0a 2a 2a 20 ] will still.**
20050 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 53 return [SQLITE_S
20060 43 48 45 4d 41 5d 2e 20 20 42 75 74 20 75 6e 6c CHEMA]. But unl
20070 69 6b 65 20 74 68 65 20 6c 65 67 61 63 79 20 62 ike the legacy b
20080 65 68 61 76 69 6f 72 2c 20 5b 53 51 4c 49 54 45 ehavior, [SQLITE
20090 5f 53 43 48 45 4d 41 5d 20 69 73 0a 2a 2a 20 6e _SCHEMA] is.** n
200a0 6f 77 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72 ow a fatal error
200b0 2e 20 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 . Calling [sqli
200c0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
200d0 5d 20 61 67 61 69 6e 20 77 69 6c 6c 20 6e 6f 74 ] again will not
200e0 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 65 72 72 make the.** err
200f0 6f 72 20 67 6f 20 61 77 61 79 2e 20 20 4e 6f 74 or go away. Not
20100 65 3a 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f e: use [sqlite3_
20110 65 72 72 6d 73 67 28 29 5d 20 74 6f 20 66 69 6e errmsg()] to fin
20120 64 20 74 68 65 20 74 65 78 74 0a 2a 2a 20 6f 66 d the text.** of
20130 20 74 68 65 20 70 61 72 73 69 6e 67 20 65 72 72 the parsing err
20140 6f 72 20 74 68 61 74 20 72 65 73 75 6c 74 73 20 or that results
20150 69 6e 20 61 6e 20 5b 53 51 4c 49 54 45 5f 53 43 in an [SQLITE_SC
20160 48 45 4d 41 5d 20 72 65 74 75 72 6e 2e 0a 2a 2a HEMA] return..**
20170 20 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 </li>.**.** <li
20180 3e 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 65 72 72 >.** When an err
20190 6f 72 20 6f 63 63 75 72 73 2c 20 5b 73 71 6c 69 or occurs, [sqli
201a0 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c te3_step()] will
201b0 20 72 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74 return one of t
201c0 68 65 20 64 65 74 61 69 6c 65 64 0a 2a 2a 20 5b he detailed.** [
201d0 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 6f 72 20 error codes] or
201e0 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 [extended error
201f0 63 6f 64 65 73 5d 2e 20 20 54 68 65 20 6c 65 67 codes]. The leg
20200 61 63 79 20 62 65 68 61 76 69 6f 72 20 77 61 73 acy behavior was
20210 20 74 68 61 74 0a 2a 2a 20 5b 73 71 6c 69 74 65 that.** [sqlite
20220 33 5f 73 74 65 70 28 29 5d 20 77 6f 75 6c 64 20 3_step()] would
20230 6f 6e 6c 79 20 72 65 74 75 72 6e 20 61 20 67 65 only return a ge
20240 6e 65 72 69 63 20 5b 53 51 4c 49 54 45 5f 45 52 neric [SQLITE_ER
20250 52 4f 52 5d 20 72 65 73 75 6c 74 20 63 6f 64 65 ROR] result code
20260 0a 2a 2a 20 61 6e 64 20 79 6f 75 20 77 6f 75 6c .** and you woul
20270 64 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61 d have to make a
20280 20 73 65 63 6f 6e 64 20 63 61 6c 6c 20 74 6f 20 second call to
20290 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 [sqlite3_reset()
202a0 5d 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f ] in order.** to
202b0 20 66 69 6e 64 20 74 68 65 20 75 6e 64 65 72 6c find the underl
202c0 79 69 6e 67 20 63 61 75 73 65 20 6f 66 20 74 68 ying cause of th
202d0 65 20 70 72 6f 62 6c 65 6d 2e 20 57 69 74 68 20 e problem. With
202e0 74 68 65 20 22 76 32 22 20 70 72 65 70 61 72 65 the "v2" prepare
202f0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 2c 20 .** interfaces,
20300 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 72 the underlying r
20310 65 61 73 6f 6e 20 66 6f 72 20 74 68 65 20 65 72 eason for the er
20320 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 ror is returned
20330 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 20 immediately..**
20340 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e </li>.**.** <li>
20350 0a 2a 2a 20 5e 49 66 20 74 68 65 20 76 61 6c 75 .** ^If the valu
20360 65 20 6f 66 20 61 20 5b 70 61 72 61 6d 65 74 65 e of a [paramete
20370 72 20 7c 20 68 6f 73 74 20 70 61 72 61 6d 65 74 r | host paramet
20380 65 72 5d 20 69 6e 20 74 68 65 20 57 48 45 52 45 er] in the WHERE
20390 20 63 6c 61 75 73 65 20 6d 69 67 68 74 0a 2a 2a clause might.**
203a0 20 63 68 61 6e 67 65 20 74 68 65 20 71 75 65 72 change the quer
203b0 79 20 70 6c 61 6e 20 66 6f 72 20 61 20 73 74 61 y plan for a sta
203c0 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68 65 tement, then the
203d0 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 79 20 62 statement may b
203e0 65 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c e.** automatical
203f0 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 20 28 61 ly recompiled (a
20400 73 20 69 66 20 74 68 65 72 65 20 68 61 64 20 62 s if there had b
20410 65 65 6e 20 61 20 73 63 68 65 6d 61 20 63 68 61 een a schema cha
20420 6e 67 65 29 20 6f 6e 20 74 68 65 20 66 69 72 73 nge) on the firs
20430 74 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 t .** [sqlite3_s
20440 74 65 70 28 29 5d 20 63 61 6c 6c 20 66 6f 6c 6c tep()] call foll
20450 6f 77 69 6e 67 20 61 6e 79 20 63 68 61 6e 67 65 owing any change
20460 20 74 6f 20 74 68 65 20 0a 2a 2a 20 5b 73 71 6c to the .** [sql
20470 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 20 7c ite3_bind_text |
20480 20 62 69 6e 64 69 6e 67 73 5d 20 6f 66 20 74 68 bindings] of th
20490 65 20 5b 70 61 72 61 6d 65 74 65 72 5d 2e 20 0a e [parameter]. .
204a0 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 6f 6c ** </li>.** </ol
204b0 3e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d >.**.** Requirem
204c0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 30 31 31 ents:.** [H13011
204d0 5d 20 5b 48 31 33 30 31 32 5d 20 5b 48 31 33 30 ] [H13012] [H130
204e0 31 33 5d 20 5b 48 31 33 30 31 34 5d 20 5b 48 31 13] [H13014] [H1
204f0 33 30 31 35 5d 20 5b 48 31 33 30 31 36 5d 20 5b 3015] [H13016] [
20500 48 31 33 30 31 39 5d 20 5b 48 31 33 30 32 31 5d H13019] [H13021]
20510 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 .**.*/.SQLITE_AP
20520 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 I int sqlite3_pr
20530 65 70 61 72 65 28 0a 20 20 73 71 6c 69 74 65 33 epare(. sqlite3
20540 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
20550 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e /* Database han
20560 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 dle */. const c
20570 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 har *zSql,
20580 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e /* SQL statemen
20590 74 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 t, UTF-8 encoded
205a0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c */. int nByte,
205b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
205c0 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 Maximum length
205d0 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 of zSql in bytes
205e0 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 . */. sqlite3_s
205f0 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f tmt **ppStmt, /
20600 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 * OUT: Statement
20610 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e handle */. con
20620 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c st char **pzTail
20630 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 /* OUT: Poi
20640 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 nter to unused p
20650 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a ortion of zSql *
20660 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 /.);.SQLITE_API
20670 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 int sqlite3_prep
20680 61 72 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 are_v2(. sqlite
20690 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
206a0 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 /* Database ha
206b0 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ndle */. const
206c0 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 char *zSql,
206d0 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 /* SQL stateme
206e0 6e 74 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 nt, UTF-8 encode
206f0 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 d */. int nByte
20700 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
20710 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 * Maximum length
20720 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 of zSql in byte
20730 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f s. */. sqlite3_
20740 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 stmt **ppStmt,
20750 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e /* OUT: Statemen
20760 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f t handle */. co
20770 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 nst char **pzTai
20780 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f l /* OUT: Po
20790 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 inter to unused
207a0 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 portion of zSql
207b0 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 */.);.SQLITE_API
207c0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 int sqlite3_pre
207d0 70 61 72 65 31 36 28 0a 20 20 73 71 6c 69 74 65 pare16(. sqlite
207e0 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
207f0 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 /* Database ha
20800 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ndle */. const
20810 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 void *zSql,
20820 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 /* SQL stateme
20830 6e 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 nt, UTF-16 encod
20840 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 ed */. int nByt
20850 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
20860 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 /* Maximum lengt
20870 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 h of zSql in byt
20880 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 es. */. sqlite3
20890 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 _stmt **ppStmt,
208a0 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 /* OUT: Stateme
208b0 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 nt handle */. c
208c0 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 onst void **pzTa
208d0 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 il /* OUT: P
208e0 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 ointer to unused
208f0 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c portion of zSql
20900 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 */.);.SQLITE_AP
20910 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 I int sqlite3_pr
20920 65 70 61 72 65 31 36 5f 76 32 28 0a 20 20 73 71 epare16_v2(. sq
20930 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 lite3 *db,
20940 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
20950 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f e handle */. co
20960 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 nst void *zSql,
20970 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 /* SQL sta
20980 74 65 6d 65 6e 74 2c 20 55 54 46 2d 31 36 20 65 tement, UTF-16 e
20990 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 ncoded */. int
209a0 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 nByte,
209b0 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c /* Maximum l
209c0 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e ength of zSql in
209d0 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c bytes. */. sql
209e0 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 ite3_stmt **ppSt
209f0 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 mt, /* OUT: Sta
20a00 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f tement handle */
20a10 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a . const void **
20a20 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 pzTail /* OU
20a30 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e T: Pointer to un
20a40 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 used portion of
20a50 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a zSql */.);../*.*
20a60 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 72 * CAPI3REF: Retr
20a70 69 65 76 69 6e 67 20 53 74 61 74 65 6d 65 6e 74 ieving Statement
20a80 20 53 51 4c 20 7b 48 31 33 31 30 30 7d 20 3c 48 SQL {H13100} <H
20a90 31 33 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 13000>.**.** Thi
20aa0 73 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 s interface can
20ab0 62 65 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 be used to retri
20ac0 65 76 65 20 61 20 73 61 76 65 64 20 63 6f 70 79 eve a saved copy
20ad0 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c of the original
20ae0 0a 2a 2a 20 53 51 4c 20 74 65 78 74 20 75 73 65 .** SQL text use
20af0 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 5b 70 d to create a [p
20b00 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
20b10 74 5d 20 69 66 20 74 68 61 74 20 73 74 61 74 65 t] if that state
20b20 6d 65 6e 74 20 77 61 73 0a 2a 2a 20 63 6f 6d 70 ment was.** comp
20b30 69 6c 65 64 20 75 73 69 6e 67 20 65 69 74 68 65 iled using eithe
20b40 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 r [sqlite3_prepa
20b50 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c re_v2()] or [sql
20b60 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 ite3_prepare16_v
20b70 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 2()]..**.** Requ
20b80 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
20b90 33 31 30 31 5d 20 5b 48 31 33 31 30 32 5d 20 5b 3101] [H13102] [
20ba0 48 31 33 31 30 33 5d 0a 2a 2f 0a 53 51 4c 49 54 H13103].*/.SQLIT
20bb0 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
20bc0 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 73 71 *sqlite3_sql(sq
20bd0 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
20be0 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 t);../*.** CAPI3
20bf0 52 45 46 3a 20 44 79 6e 61 6d 69 63 61 6c 6c 79 REF: Dynamically
20c00 20 54 79 70 65 64 20 56 61 6c 75 65 20 4f 62 6a Typed Value Obj
20c10 65 63 74 20 7b 48 31 35 30 30 30 7d 20 3c 53 32 ect {H15000} <S2
20c20 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0200>.** KEYWORD
20c30 53 3a 20 7b 70 72 6f 74 65 63 74 65 64 20 73 71 S: {protected sq
20c40 6c 69 74 65 33 5f 76 61 6c 75 65 7d 20 7b 75 6e lite3_value} {un
20c50 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
20c60 33 5f 76 61 6c 75 65 7d 0a 2a 2a 0a 2a 2a 20 53 3_value}.**.** S
20c70 51 4c 69 74 65 20 75 73 65 73 20 74 68 65 20 73 QLite uses the s
20c80 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a qlite3_value obj
20c90 65 63 74 20 74 6f 20 72 65 70 72 65 73 65 6e 74 ect to represent
20ca0 20 61 6c 6c 20 76 61 6c 75 65 73 0a 2a 2a 20 74 all values.** t
20cb0 68 61 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65 hat can be store
20cc0 64 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 d in a database
20cd0 74 61 62 6c 65 2e 20 53 51 4c 69 74 65 20 75 73 table. SQLite us
20ce0 65 73 20 64 79 6e 61 6d 69 63 20 74 79 70 69 6e es dynamic typin
20cf0 67 0a 2a 2a 20 66 6f 72 20 74 68 65 20 76 61 6c g.** for the val
20d00 75 65 73 20 69 74 20 73 74 6f 72 65 73 2e 20 56 ues it stores. V
20d10 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 alues stored in
20d20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 sqlite3_value ob
20d30 6a 65 63 74 73 0a 2a 2a 20 63 61 6e 20 62 65 20 jects.** can be
20d40 69 6e 74 65 67 65 72 73 2c 20 66 6c 6f 61 74 69 integers, floati
20d50 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 2c ng point values,
20d60 20 73 74 72 69 6e 67 73 2c 20 42 4c 4f 42 73 2c strings, BLOBs,
20d70 20 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 or NULL..**.**
20d80 41 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 An sqlite3_value
20d90 20 6f 62 6a 65 63 74 20 6d 61 79 20 62 65 20 65 object may be e
20da0 69 74 68 65 72 20 22 70 72 6f 74 65 63 74 65 64 ither "protected
20db0 22 20 6f 72 20 22 75 6e 70 72 6f 74 65 63 74 65 " or "unprotecte
20dc0 64 22 2e 0a 2a 2a 20 53 6f 6d 65 20 69 6e 74 65 d"..** Some inte
20dd0 72 66 61 63 65 73 20 72 65 71 75 69 72 65 20 61 rfaces require a
20de0 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 protected sqlit
20df0 65 33 5f 76 61 6c 75 65 2e 20 20 4f 74 68 65 72 e3_value. Other
20e00 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 77 interfaces.** w
20e10 69 6c 6c 20 61 63 63 65 70 74 20 65 69 74 68 65 ill accept eithe
20e20 72 20 61 20 70 72 6f 74 65 63 74 65 64 20 6f 72 r a protected or
20e30 20 61 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 20 an unprotected
20e40 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 0a 2a sqlite3_value..*
20e50 2a 20 45 76 65 72 79 20 69 6e 74 65 72 66 61 63 * Every interfac
20e60 65 20 74 68 61 74 20 61 63 63 65 70 74 73 20 73 e that accepts s
20e70 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 61 72 67 qlite3_value arg
20e80 75 6d 65 6e 74 73 20 73 70 65 63 69 66 69 65 73 uments specifies
20e90 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e .** whether or n
20ea0 6f 74 20 69 74 20 72 65 71 75 69 72 65 73 20 61 ot it requires a
20eb0 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 protected sqlit
20ec0 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 e3_value..**.**
20ed0 54 68 65 20 74 65 72 6d 73 20 22 70 72 6f 74 65 The terms "prote
20ee0 63 74 65 64 22 20 61 6e 64 20 22 75 6e 70 72 6f cted" and "unpro
20ef0 74 65 63 74 65 64 22 20 72 65 66 65 72 20 74 6f tected" refer to
20f00 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a whether or not.
20f10 2a 2a 20 61 20 6d 75 74 65 78 20 69 73 20 68 65 ** a mutex is he
20f20 6c 64 2e 20 20 41 20 69 6e 74 65 72 6e 61 6c 20 ld. A internal
20f30 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 66 6f mutex is held fo
20f40 72 20 61 20 70 72 6f 74 65 63 74 65 64 0a 2a 2a r a protected.**
20f50 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f sqlite3_value o
20f60 62 6a 65 63 74 20 62 75 74 20 6e 6f 20 6d 75 74 bject but no mut
20f70 65 78 20 69 73 20 68 65 6c 64 20 66 6f 72 20 61 ex is held for a
20f80 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a n unprotected.**
20f90 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f sqlite3_value o
20fa0 62 6a 65 63 74 2e 20 20 49 66 20 53 51 4c 69 74 bject. If SQLit
20fb0 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f e is compiled to
20fc0 20 62 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 be single-threa
20fd0 64 65 64 0a 2a 2a 20 28 77 69 74 68 20 5b 53 51 ded.** (with [SQ
20fe0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d LITE_THREADSAFE=
20ff0 30 5d 20 61 6e 64 20 77 69 74 68 20 5b 73 71 6c 0] and with [sql
21000 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 28 ite3_threadsafe(
21010 29 5d 20 72 65 74 75 72 6e 69 6e 67 20 30 29 0a )] returning 0).
21020 2a 2a 20 6f 72 20 69 66 20 53 51 4c 69 74 65 20 ** or if SQLite
21030 69 73 20 72 75 6e 20 69 6e 20 6f 6e 65 20 6f 66 is run in one of
21040 20 72 65 64 75 63 65 64 20 6d 75 74 65 78 20 6d reduced mutex m
21050 6f 64 65 73 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 odes .** [SQLITE
21060 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 _CONFIG_SINGLETH
21070 52 45 41 44 5d 20 6f 72 20 5b 53 51 4c 49 54 45 READ] or [SQLITE
21080 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 _CONFIG_MULTITHR
21090 45 41 44 5d 0a 2a 2a 20 74 68 65 6e 20 74 68 65 EAD].** then the
210a0 72 65 20 69 73 20 6e 6f 20 64 69 73 74 69 6e 63 re is no distinc
210b0 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 70 72 6f tion between pro
210c0 74 65 63 74 65 64 20 61 6e 64 20 75 6e 70 72 6f tected and unpro
210d0 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 tected.** sqlite
210e0 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 3_value objects
210f0 61 6e 64 20 74 68 65 79 20 63 61 6e 20 62 65 20 and they can be
21100 75 73 65 64 20 69 6e 74 65 72 63 68 61 6e 67 65 used interchange
21110 61 62 6c 79 2e 20 20 48 6f 77 65 76 65 72 2c 0a ably. However,.
21120 2a 2a 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 63 ** for maximum c
21130 6f 64 65 20 70 6f 72 74 61 62 69 6c 69 74 79 20 ode portability
21140 69 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 it is recommende
21150 64 20 74 68 61 74 20 61 70 70 6c 69 63 61 74 69 d that applicati
21160 6f 6e 73 0a 2a 2a 20 73 74 69 6c 6c 20 6d 61 6b ons.** still mak
21170 65 20 74 68 65 20 64 69 73 74 69 6e 63 74 69 6f e the distinctio
21180 6e 20 62 65 74 77 65 65 6e 20 62 65 74 77 65 65 n between betwee
21190 6e 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64 20 n protected and
211a0 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 unprotected.** s
211b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a qlite3_value obj
211c0 65 63 74 73 20 65 76 65 6e 20 77 68 65 6e 20 6e ects even when n
211d0 6f 74 20 73 74 72 69 63 74 6c 79 20 72 65 71 75 ot strictly requ
211e0 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ired..**.** The
211f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 sqlite3_value ob
21200 6a 65 63 74 73 20 74 68 61 74 20 61 72 65 20 70 jects that are p
21210 61 73 73 65 64 20 61 73 20 70 61 72 61 6d 65 74 assed as paramet
21220 65 72 73 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 ers into the.**
21230 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
21240 66 20 5b 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 f [application-d
21250 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 efined SQL funct
21260 69 6f 6e 73 5d 20 61 72 65 20 70 72 6f 74 65 63 ions] are protec
21270 74 65 64 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ted..** The sqli
21280 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 te3_value object
21290 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 returned by.**
212a0 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f [sqlite3_column_
212b0 76 61 6c 75 65 28 29 5d 20 69 73 20 75 6e 70 72 value()] is unpr
212c0 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 55 6e 70 72 otected..** Unpr
212d0 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
212e0 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 6d 61 value objects ma
212f0 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 77 y only be used w
21300 69 74 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f ith.** [sqlite3_
21310 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 5d 20 result_value()]
21320 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e and [sqlite3_bin
21330 64 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20 54 d_value()]..** T
21340 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 he [sqlite3_valu
21350 65 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33 e_blob | sqlite3
21360 5f 76 61 6c 75 65 5f 74 79 70 65 28 29 5d 20 66 _value_type()] f
21370 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20 69 6e 74 65 amily of.** inte
21380 72 66 61 63 65 73 20 72 65 71 75 69 72 65 20 70 rfaces require p
21390 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 rotected sqlite3
213a0 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 2e 0a _value objects..
213b0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
213c0 74 20 4d 65 6d 20 73 71 6c 69 74 65 33 5f 76 61 t Mem sqlite3_va
213d0 6c 75 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 lue;../*.** CAPI
213e0 33 52 45 46 3a 20 53 51 4c 20 46 75 6e 63 74 69 3REF: SQL Functi
213f0 6f 6e 20 43 6f 6e 74 65 78 74 20 4f 62 6a 65 63 on Context Objec
21400 74 20 7b 48 31 36 30 30 31 7d 20 3c 53 32 30 32 t {H16001} <S202
21410 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 00>.**.** The co
21420 6e 74 65 78 74 20 69 6e 20 77 68 69 63 68 20 61 ntext in which a
21430 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 65 n SQL function e
21440 78 65 63 75 74 65 73 20 69 73 20 73 74 6f 72 65 xecutes is store
21450 64 20 69 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 d in an.** sqlit
21460 65 33 5f 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63 e3_context objec
21470 74 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f t. A pointer to
21480 20 61 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 an sqlite3_cont
21490 65 78 74 20 6f 62 6a 65 63 74 0a 2a 2a 20 69 73 ext object.** is
214a0 20 61 6c 77 61 79 73 20 66 69 72 73 74 20 70 61 always first pa
214b0 72 61 6d 65 74 65 72 20 74 6f 20 5b 61 70 70 6c rameter to [appl
214c0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
214d0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a SQL functions]..
214e0 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 ** The applicati
214f0 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 on-defined SQL f
21500 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e unction implemen
21510 74 61 74 69 6f 6e 20 77 69 6c 6c 20 70 61 73 73 tation will pass
21520 20 74 68 69 73 0a 2a 2a 20 70 6f 69 6e 74 65 72 this.** pointer
21530 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 63 61 through into ca
21540 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f lls to [sqlite3_
21550 72 65 73 75 6c 74 5f 69 6e 74 20 7c 20 73 71 6c result_int | sql
21560 69 74 65 33 5f 72 65 73 75 6c 74 28 29 5d 2c 0a ite3_result()],.
21570 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 61 67 67 72 ** [sqlite3_aggr
21580 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 5d egate_context()]
21590 2c 20 5b 73 71 6c 69 74 65 33 5f 75 73 65 72 5f , [sqlite3_user_
215a0 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c data()],.** [sql
215b0 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f ite3_context_db_
215c0 68 61 6e 64 6c 65 28 29 5d 2c 20 5b 73 71 6c 69 handle()], [sqli
215d0 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 te3_get_auxdata(
215e0 29 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f 72 20 5b 73 )],.** and/or [s
215f0 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 qlite3_set_auxda
21600 74 61 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 ta()]..*/.typede
21610 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 f struct sqlite3
21620 5f 63 6f 6e 74 65 78 74 20 73 71 6c 69 74 65 33 _context sqlite3
21630 5f 63 6f 6e 74 65 78 74 3b 0a 0a 2f 2a 0a 2a 2a _context;../*.**
21640 20 43 41 50 49 33 52 45 46 3a 20 42 69 6e 64 69 CAPI3REF: Bindi
21650 6e 67 20 56 61 6c 75 65 73 20 54 6f 20 50 72 65 ng Values To Pre
21660 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 73 pared Statements
21670 20 7b 48 31 33 35 30 30 7d 20 3c 53 37 30 33 30 {H13500} <S7030
21680 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 0>.** KEYWORDS:
21690 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 7d {host parameter}
216a0 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 {host parameter
216b0 73 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 s} {host paramet
216c0 65 72 20 6e 61 6d 65 7d 0a 2a 2a 20 4b 45 59 57 er name}.** KEYW
216d0 4f 52 44 53 3a 20 7b 53 51 4c 20 70 61 72 61 6d ORDS: {SQL param
216e0 65 74 65 72 7d 20 7b 53 51 4c 20 70 61 72 61 6d eter} {SQL param
216f0 65 74 65 72 73 7d 20 7b 70 61 72 61 6d 65 74 65 eters} {paramete
21700 72 20 62 69 6e 64 69 6e 67 7d 0a 2a 2a 0a 2a 2a r binding}.**.**
21710 20 49 6e 20 74 68 65 20 53 51 4c 20 73 74 72 69 In the SQL stri
21720 6e 67 73 20 69 6e 70 75 74 20 74 6f 20 5b 73 71 ngs input to [sq
21730 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
21740 28 29 5d 20 61 6e 64 20 69 74 73 20 76 61 72 69 ()] and its vari
21750 61 6e 74 73 2c 0a 2a 2a 20 6c 69 74 65 72 61 6c ants,.** literal
21760 73 20 6d 61 79 20 62 65 20 72 65 70 6c 61 63 65 s may be replace
21770 64 20 62 79 20 61 20 5b 70 61 72 61 6d 65 74 65 d by a [paramete
21780 72 5d 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 r] that matches
21790 6f 6e 65 20 6f 66 20 66 6f 6c 6c 6f 77 69 6e 67 one of following
217a0 0a 2a 2a 20 74 65 6d 70 6c 61 74 65 73 3a 0a 2a .** templates:.*
217b0 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 *.** <ul>.** <li
217c0 3e 20 20 3f 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 4e > ?.** <li> ?N
217d0 4e 4e 0a 2a 2a 20 3c 6c 69 3e 20 20 3a 56 56 56 NN.** <li> :VVV
217e0 0a 2a 2a 20 3c 6c 69 3e 20 20 40 56 56 56 0a 2a .** <li> @VVV.*
217f0 2a 20 3c 6c 69 3e 20 20 24 56 56 56 0a 2a 2a 20 * <li> $VVV.**
21800 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 </ul>.**.** In t
21810 68 65 20 74 65 6d 70 6c 61 74 65 73 20 61 62 6f he templates abo
21820 76 65 2c 20 4e 4e 4e 20 72 65 70 72 65 73 65 6e ve, NNN represen
21830 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6c 69 ts an integer li
21840 74 65 72 61 6c 2c 0a 2a 2a 20 61 6e 64 20 56 56 teral,.** and VV
21850 56 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 V represents an
21860 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 69 64 65 alphanumeric ide
21870 6e 74 69 66 65 72 2e 20 20 54 68 65 20 76 61 6c ntifer. The val
21880 75 65 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 ues of these.**
21890 70 61 72 61 6d 65 74 65 72 73 20 28 61 6c 73 6f parameters (also
218a0 20 63 61 6c 6c 65 64 20 22 68 6f 73 74 20 70 61 called "host pa
218b0 72 61 6d 65 74 65 72 20 6e 61 6d 65 73 22 20 6f rameter names" o
218c0 72 20 22 53 51 4c 20 70 61 72 61 6d 65 74 65 72 r "SQL parameter
218d0 73 22 29 0a 2a 2a 20 63 61 6e 20 62 65 20 73 65 s").** can be se
218e0 74 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 t using the sqli
218f0 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 te3_bind_*() rou
21900 74 69 6e 65 73 20 64 65 66 69 6e 65 64 20 68 65 tines defined he
21910 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 re..**.** The fi
21920 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 rst argument to
21930 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 the sqlite3_bind
21940 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 69 73 _*() routines is
21950 20 61 6c 77 61 79 73 0a 2a 2a 20 61 20 70 6f 69 always.** a poi
21960 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 73 71 6c nter to the [sql
21970 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 ite3_stmt] objec
21980 74 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 0a t returned from.
21990 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ** [sqlite3_prep
219a0 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 69 74 73 are_v2()] or its
219b0 20 76 61 72 69 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a variants..**.**
219c0 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 The second argu
219d0 6d 65 6e 74 20 69 73 20 74 68 65 20 69 6e 64 65 ment is the inde
219e0 78 20 6f 66 20 74 68 65 20 53 51 4c 20 70 61 72 x of the SQL par
219f0 61 6d 65 74 65 72 20 74 6f 20 62 65 20 73 65 74 ameter to be set
21a00 2e 0a 2a 2a 20 54 68 65 20 6c 65 66 74 6d 6f 73 ..** The leftmos
21a10 74 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 t SQL parameter
21a20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 has an index of
21a30 31 2e 20 20 57 68 65 6e 20 74 68 65 20 73 61 6d 1. When the sam
21a40 65 20 6e 61 6d 65 64 0a 2a 2a 20 53 51 4c 20 70 e named.** SQL p
21a50 61 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 arameter is used
21a60 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c more than once,
21a70 20 73 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 second and subs
21a80 65 71 75 65 6e 74 0a 2a 2a 20 6f 63 63 75 72 72 equent.** occurr
21a90 65 6e 63 65 73 20 68 61 76 65 20 74 68 65 20 73 ences have the s
21aa0 61 6d 65 20 69 6e 64 65 78 20 61 73 20 74 68 65 ame index as the
21ab0 20 66 69 72 73 74 20 6f 63 63 75 72 72 65 6e 63 first occurrenc
21ac0 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 e..** The index
21ad0 66 6f 72 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 for named parame
21ae0 74 65 72 73 20 63 61 6e 20 62 65 20 6c 6f 6f 6b ters can be look
21af0 65 64 20 75 70 20 75 73 69 6e 67 20 74 68 65 0a ed up using the.
21b00 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 ** [sqlite3_bind
21b10 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 _parameter_index
21b20 28 29 5d 20 41 50 49 20 69 66 20 64 65 73 69 72 ()] API if desir
21b30 65 64 2e 20 20 54 68 65 20 69 6e 64 65 78 0a 2a ed. The index.*
21b40 2a 20 66 6f 72 20 22 3f 4e 4e 4e 22 20 70 61 72 * for "?NNN" par
21b50 61 6d 65 74 65 72 73 20 69 73 20 74 68 65 20 76 ameters is the v
21b60 61 6c 75 65 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a 20 alue of NNN..**
21b70 54 68 65 20 4e 4e 4e 20 76 61 6c 75 65 20 6d 75 The NNN value mu
21b80 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 31 20 st be between 1
21b90 61 6e 64 20 74 68 65 20 5b 73 71 6c 69 74 65 33 and the [sqlite3
21ba0 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 70 61 72 _limit()].** par
21bb0 61 6d 65 74 65 72 20 5b 53 51 4c 49 54 45 5f 4c ameter [SQLITE_L
21bc0 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 IMIT_VARIABLE_NU
21bd0 4d 42 45 52 5d 20 28 64 65 66 61 75 6c 74 20 76 MBER] (default v
21be0 61 6c 75 65 3a 20 39 39 39 29 2e 0a 2a 2a 0a 2a alue: 999)..**.*
21bf0 2a 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 * The third argu
21c00 6d 65 6e 74 20 69 73 20 74 68 65 20 76 61 6c 75 ment is the valu
21c10 65 20 74 6f 20 62 69 6e 64 20 74 6f 20 74 68 65 e to bind to the
21c20 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a parameter..**.*
21c30 2a 20 49 6e 20 74 68 6f 73 65 20 72 6f 75 74 69 * In those routi
21c40 6e 65 73 20 74 68 61 74 20 68 61 76 65 20 61 20 nes that have a
21c50 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 2c fourth argument,
21c60 20 69 74 73 20 76 61 6c 75 65 20 69 73 20 74 68 its value is th
21c70 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 e.** number of b
21c80 79 74 65 73 20 69 6e 20 74 68 65 20 70 61 72 61 ytes in the para
21c90 6d 65 74 65 72 2e 20 20 54 6f 20 62 65 20 63 6c meter. To be cl
21ca0 65 61 72 3a 20 74 68 65 20 76 61 6c 75 65 20 69 ear: the value i
21cb0 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 s the.** number
21cc0 6f 66 20 3c 75 3e 62 79 74 65 73 3c 2f 75 3e 20 of <u>bytes</u>
21cd0 69 6e 20 74 68 65 20 76 61 6c 75 65 2c 20 6e 6f in the value, no
21ce0 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 t the number of
21cf0 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49 characters..** I
21d00 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 f the fourth par
21d10 61 6d 65 74 65 72 20 69 73 20 6e 65 67 61 74 69 ameter is negati
21d20 76 65 2c 20 74 68 65 20 6c 65 6e 67 74 68 20 6f ve, the length o
21d30 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 0a f the string is.
21d40 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ** the number of
21d50 20 62 79 74 65 73 20 75 70 20 74 6f 20 74 68 65 bytes up to the
21d60 20 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d first zero term
21d70 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 inator..**.** Th
21d80 65 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e 74 e fifth argument
21d90 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 to sqlite3_bind
21da0 5f 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65 33 _blob(), sqlite3
21db0 5f 62 69 6e 64 5f 74 65 78 74 28 29 2c 20 61 6e _bind_text(), an
21dc0 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e d.** sqlite3_bin
21dd0 64 5f 74 65 78 74 31 36 28 29 20 69 73 20 61 20 d_text16() is a
21de0 64 65 73 74 72 75 63 74 6f 72 20 75 73 65 64 20 destructor used
21df0 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68 to dispose of th
21e00 65 20 42 4c 4f 42 20 6f 72 0a 2a 2a 20 73 74 72 e BLOB or.** str
21e10 69 6e 67 20 61 66 74 65 72 20 53 51 4c 69 74 65 ing after SQLite
21e20 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 77 69 has finished wi
21e30 74 68 20 69 74 2e 20 49 66 20 74 68 65 20 66 69 th it. If the fi
21e40 66 74 68 20 61 72 67 75 6d 65 6e 74 20 69 73 0a fth argument is.
21e50 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20 76 ** the special v
21e60 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 53 54 41 alue [SQLITE_STA
21e70 54 49 43 5d 2c 20 74 68 65 6e 20 53 51 4c 69 74 TIC], then SQLit
21e80 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 e assumes that t
21e90 68 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f he.** informatio
21ea0 6e 20 69 73 20 69 6e 20 73 74 61 74 69 63 2c 20 n is in static,
21eb0 75 6e 6d 61 6e 61 67 65 64 20 73 70 61 63 65 20 unmanaged space
21ec0 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 and does not nee
21ed0 64 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a d to be freed..*
21ee0 2a 20 49 66 20 74 68 65 20 66 69 66 74 68 20 61 * If the fifth a
21ef0 72 67 75 6d 65 6e 74 20 68 61 73 20 74 68 65 20 rgument has the
21f00 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 54 52 value [SQLITE_TR
21f10 41 4e 53 49 45 4e 54 5d 2c 20 74 68 65 6e 0a 2a ANSIENT], then.*
21f20 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 69 * SQLite makes i
21f30 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20 63 ts own private c
21f40 6f 70 79 20 6f 66 20 74 68 65 20 64 61 74 61 20 opy of the data
21f50 69 6d 6d 65 64 69 61 74 65 6c 79 2c 20 62 65 66 immediately, bef
21f60 6f 72 65 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 ore.** the sqlit
21f70 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 e3_bind_*() rout
21f80 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a ine returns..**.
21f90 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 ** The sqlite3_b
21fa0 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 72 ind_zeroblob() r
21fb0 6f 75 74 69 6e 65 20 62 69 6e 64 73 20 61 20 42 outine binds a B
21fc0 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 20 4e 20 LOB of length N
21fd0 74 68 61 74 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 that.** is fille
21fe0 64 20 77 69 74 68 20 7a 65 72 6f 65 73 2e 20 20 d with zeroes.
21ff0 41 20 7a 65 72 6f 62 6c 6f 62 20 75 73 65 73 20 A zeroblob uses
22000 61 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f a fixed amount o
22010 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 28 6a 75 73 f memory.** (jus
22020 74 20 61 6e 20 69 6e 74 65 67 65 72 20 74 6f 20 t an integer to
22030 68 6f 6c 64 20 69 74 73 20 73 69 7a 65 29 20 77 hold its size) w
22040 68 69 6c 65 20 69 74 20 69 73 20 62 65 69 6e 67 hile it is being
22050 20 70 72 6f 63 65 73 73 65 64 2e 0a 2a 2a 20 5a processed..** Z
22060 65 72 6f 62 6c 6f 62 73 20 61 72 65 20 69 6e 74 eroblobs are int
22070 65 6e 64 65 64 20 74 6f 20 73 65 72 76 65 20 61 ended to serve a
22080 73 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 s placeholders f
22090 6f 72 20 42 4c 4f 42 73 20 77 68 6f 73 65 0a 2a or BLOBs whose.*
220a0 2a 20 63 6f 6e 74 65 6e 74 20 69 73 20 6c 61 74 * content is lat
220b0 65 72 20 77 72 69 74 74 65 6e 20 75 73 69 6e 67 er written using
220c0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f .** [sqlite3_blo
220d0 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 b_open | increme
220e0 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 72 ntal BLOB I/O] r
220f0 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 41 20 6e 65 outines..** A ne
22100 67 61 74 69 76 65 20 76 61 6c 75 65 20 66 6f 72 gative value for
22110 20 74 68 65 20 7a 65 72 6f 62 6c 6f 62 20 72 65 the zeroblob re
22120 73 75 6c 74 73 20 69 6e 20 61 20 7a 65 72 6f 2d sults in a zero-
22130 6c 65 6e 67 74 68 20 42 4c 4f 42 2e 0a 2a 2a 0a length BLOB..**.
22140 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 ** The sqlite3_b
22150 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 ind_*() routines
22160 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 must be called
22170 61 66 74 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 after.** [sqlite
22180 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 3_prepare_v2()]
22190 28 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 (and its variant
221a0 73 29 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 s) or [sqlite3_r
221b0 65 73 65 74 28 29 5d 20 61 6e 64 0a 2a 2a 20 62 eset()] and.** b
221c0 65 66 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 efore [sqlite3_s
221d0 74 65 70 28 29 5d 2e 0a 2a 2a 20 42 69 6e 64 69 tep()]..** Bindi
221e0 6e 67 73 20 61 72 65 20 6e 6f 74 20 63 6c 65 61 ngs are not clea
221f0 72 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69 red by the [sqli
22200 74 65 33 5f 72 65 73 65 74 28 29 5d 20 72 6f 75 te3_reset()] rou
22210 74 69 6e 65 2e 0a 2a 2a 20 55 6e 62 6f 75 6e 64 tine..** Unbound
22220 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 parameters are
22230 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 4e interpreted as N
22240 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 ULL..**.** These
22250 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e routines return
22260 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 [SQLITE_OK] on
22270 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 success or an er
22280 72 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20 61 ror code if.** a
22290 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f nything goes wro
222a0 6e 67 2e 20 20 5b 53 51 4c 49 54 45 5f 52 41 4e ng. [SQLITE_RAN
222b0 47 45 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 GE] is returned
222c0 69 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 if the parameter
222d0 0a 2a 2a 20 69 6e 64 65 78 20 69 73 20 6f 75 74 .** index is out
222e0 20 6f 66 20 72 61 6e 67 65 2e 20 20 5b 53 51 4c of range. [SQL
222f0 49 54 45 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72 65 ITE_NOMEM] is re
22300 74 75 72 6e 65 64 20 69 66 20 6d 61 6c 6c 6f 63 turned if malloc
22310 28 29 20 66 61 69 6c 73 2e 0a 2a 2a 20 5b 53 51 () fails..** [SQ
22320 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20 6d 69 67 LITE_MISUSE] mig
22330 68 74 20 62 65 20 72 65 74 75 72 6e 65 64 20 69 ht be returned i
22340 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 f these routines
22350 20 61 72 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 are called on a
22360 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 .** virtual mach
22370 69 6e 65 20 74 68 61 74 20 69 73 20 74 68 65 20 ine that is the
22380 77 72 6f 6e 67 20 73 74 61 74 65 20 6f 72 20 77 wrong state or w
22390 68 69 63 68 20 68 61 73 20 61 6c 72 65 61 64 79 hich has already
223a0 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65 64 2e been finalized.
223b0 0a 2a 2a 20 44 65 74 65 63 74 69 6f 6e 20 6f 66 .** Detection of
223c0 20 6d 69 73 75 73 65 20 69 73 20 75 6e 72 65 6c misuse is unrel
223d0 69 61 62 6c 65 2e 20 20 41 70 70 6c 69 63 61 74 iable. Applicat
223e0 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 ions should not
223f0 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 53 51 4c depend.** on SQL
22400 49 54 45 5f 4d 49 53 55 53 45 20 72 65 74 75 72 ITE_MISUSE retur
22410 6e 73 2e 20 20 53 51 4c 49 54 45 5f 4d 49 53 55 ns. SQLITE_MISU
22420 53 45 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 SE is intended t
22430 6f 20 69 6e 64 69 63 61 74 65 20 61 0a 2a 2a 20 o indicate a.**
22440 61 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 69 6e a logic error in
22450 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
22460 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69 6f . Future versio
22470 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 ns of SQLite mig
22480 68 74 0a 2a 2a 20 70 61 6e 69 63 20 72 61 74 68 ht.** panic rath
22490 65 72 20 74 68 61 6e 20 72 65 74 75 72 6e 20 53 er than return S
224a0 51 4c 49 54 45 5f 4d 49 53 55 53 45 2e 0a 2a 2a QLITE_MISUSE..**
224b0 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 .** See also: [s
224c0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 qlite3_bind_para
224d0 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 0a meter_count()],.
224e0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 ** [sqlite3_bind
224f0 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 _parameter_name(
22500 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 )], and [sqlite3
22510 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f _bind_parameter_
22520 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 index()]..**.**
22530 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
22540 20 5b 48 31 33 35 30 36 5d 20 5b 48 31 33 35 30 [H13506] [H1350
22550 39 5d 20 5b 48 31 33 35 31 32 5d 20 5b 48 31 33 9] [H13512] [H13
22560 35 31 35 5d 20 5b 48 31 33 35 31 38 5d 20 5b 48 515] [H13518] [H
22570 31 33 35 32 31 5d 20 5b 48 31 33 35 32 34 5d 20 13521] [H13524]
22580 5b 48 31 33 35 32 37 5d 0a 2a 2a 20 5b 48 31 33 [H13527].** [H13
22590 35 33 30 5d 20 5b 48 31 33 35 33 33 5d 20 5b 48 530] [H13533] [H
225a0 31 33 35 33 36 5d 20 5b 48 31 33 35 33 39 5d 20 13536] [H13539]
225b0 5b 48 31 33 35 34 32 5d 20 5b 48 31 33 35 34 35 [H13542] [H13545
225c0 5d 20 5b 48 31 33 35 34 38 5d 20 5b 48 31 33 35 ] [H13548] [H135
225d0 35 31 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 51].**.*/.SQLITE
225e0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
225f0 5f 62 69 6e 64 5f 62 6c 6f 62 28 73 71 6c 69 74 _bind_blob(sqlit
22600 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 e3_stmt*, int, c
22610 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 onst void*, int
22620 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a n, void(*)(void*
22630 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 ));.SQLITE_API i
22640 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
22650 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 double(sqlite3_s
22660 74 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 75 62 6c tmt*, int, doubl
22670 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 e);.SQLITE_API i
22680 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
22690 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 int(sqlite3_stmt
226a0 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 *, int, int);.SQ
226b0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
226c0 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 ite3_bind_int64(
226d0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
226e0 6e 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 nt, sqlite3_int6
226f0 34 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 4);.SQLITE_API i
22700 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
22710 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d null(sqlite3_stm
22720 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 t*, int);.SQLITE
22730 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
22740 5f 62 69 6e 64 5f 74 65 78 74 28 73 71 6c 69 74 _bind_text(sqlit
22750 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 e3_stmt*, int, c
22760 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 20 onst char*, int
22770 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a n, void(*)(void*
22780 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 ));.SQLITE_API i
22790 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
227a0 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 text16(sqlite3_s
227b0 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 tmt*, int, const
227c0 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 void*, int, voi
227d0 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 d(*)(void*));.SQ
227e0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
227f0 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 ite3_bind_value(
22800 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
22810 6e 74 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 nt, const sqlite
22820 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 3_value*);.SQLIT
22830 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
22840 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 3_bind_zeroblob(
22850 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
22860 6e 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a nt, int n);../*.
22870 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d ** CAPI3REF: Num
22880 62 65 72 20 4f 66 20 53 51 4c 20 50 61 72 61 6d ber Of SQL Param
22890 65 74 65 72 73 20 7b 48 31 33 36 30 30 7d 20 3c eters {H13600} <
228a0 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S70300>.**.** Th
228b0 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 is routine can b
228c0 65 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20 74 e used to find t
228d0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 5b 53 51 he number of [SQ
228e0 4c 20 70 61 72 61 6d 65 74 65 72 73 5d 0a 2a 2a L parameters].**
228f0 20 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 in a [prepared
22900 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 53 51 4c statement]. SQL
22910 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 parameters are
22920 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65 0a 2a 2a tokens of the.**
22930 20 66 6f 72 6d 20 22 3f 22 2c 20 22 3f 4e 4e 4e form "?", "?NNN
22940 22 2c 20 22 3a 41 41 41 22 2c 20 22 24 41 41 41 ", ":AAA", "$AAA
22950 22 2c 20 6f 72 20 22 40 41 41 41 22 20 74 68 61 ", or "@AAA" tha
22960 74 20 73 65 72 76 65 20 61 73 0a 2a 2a 20 70 6c t serve as.** pl
22970 61 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 76 aceholders for v
22980 61 6c 75 65 73 20 74 68 61 74 20 61 72 65 20 5b alues that are [
22990 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f sqlite3_bind_blo
229a0 62 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a 20 74 6f b | bound].** to
229b0 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 the parameters
229c0 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2e at a later time.
229d0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
229e0 69 6e 65 20 61 63 74 75 61 6c 6c 79 20 72 65 74 ine actually ret
229f0 75 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f urns the index o
22a00 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 28 72 f the largest (r
22a10 69 67 68 74 6d 6f 73 74 29 0a 2a 2a 20 70 61 72 ightmost).** par
22a20 61 6d 65 74 65 72 2e 20 46 6f 72 20 61 6c 6c 20 ameter. For all
22a30 66 6f 72 6d 73 20 65 78 63 65 70 74 20 3f 4e 4e forms except ?NN
22a40 4e 2c 20 74 68 69 73 20 77 69 6c 6c 20 63 6f 72 N, this will cor
22a50 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 0a 2a respond to the.*
22a60 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 71 * number of uniq
22a70 75 65 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 ue parameters.
22a80 49 66 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 If parameters of
22a90 20 74 68 65 20 3f 4e 4e 4e 20 61 72 65 20 75 73 the ?NNN are us
22aa0 65 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 61 79 ed,.** there may
22ab0 20 62 65 20 67 61 70 73 20 69 6e 20 74 68 65 20 be gaps in the
22ac0 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 list..**.** See
22ad0 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 also: [sqlite3_b
22ae0 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 ind_blob|sqlite3
22af0 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 _bind()],.** [sq
22b00 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d lite3_bind_param
22b10 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e eter_name()], an
22b20 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 d.** [sqlite3_bi
22b30 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 nd_parameter_ind
22b40 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 ex()]..**.** Req
22b50 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
22b60 31 33 36 30 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 13601].*/.SQLITE
22b70 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
22b80 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f _bind_parameter_
22b90 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 count(sqlite3_st
22ba0 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 mt*);../*.** CAP
22bb0 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66 20 41 I3REF: Name Of A
22bc0 20 48 6f 73 74 20 50 61 72 61 6d 65 74 65 72 20 Host Parameter
22bd0 7b 48 31 33 36 32 30 7d 20 3c 53 37 30 33 30 30 {H13620} <S70300
22be0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 >.**.** This rou
22bf0 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 70 tine returns a p
22c00 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 61 ointer to the na
22c10 6d 65 20 6f 66 20 74 68 65 20 6e 2d 74 68 0a 2a me of the n-th.*
22c20 2a 20 5b 53 51 4c 20 70 61 72 61 6d 65 74 65 72 * [SQL parameter
22c30 5d 20 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 ] in a [prepared
22c40 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 statement]..**
22c50 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 20 6f SQL parameters o
22c60 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 4e 4e 4e f the form "?NNN
22c70 22 20 6f 72 20 22 3a 41 41 41 22 20 6f 72 20 22 " or ":AAA" or "
22c80 40 41 41 41 22 20 6f 72 20 22 24 41 41 41 22 0a @AAA" or "$AAA".
22c90 2a 2a 20 68 61 76 65 20 61 20 6e 61 6d 65 20 77 ** have a name w
22ca0 68 69 63 68 20 69 73 20 74 68 65 20 73 74 72 69 hich is the stri
22cb0 6e 67 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41 ng "?NNN" or ":A
22cc0 41 41 22 20 6f 72 20 22 40 41 41 41 22 20 6f 72 AA" or "@AAA" or
22cd0 20 22 24 41 41 41 22 0a 2a 2a 20 72 65 73 70 65 "$AAA".** respe
22ce0 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 49 6e 20 6f ctively..** In o
22cf0 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 ther words, the
22d00 69 6e 69 74 69 61 6c 20 22 3a 22 20 6f 72 20 22 initial ":" or "
22d10 24 22 20 6f 72 20 22 40 22 20 6f 72 20 22 3f 22 $" or "@" or "?"
22d20 0a 2a 2a 20 69 73 20 69 6e 63 6c 75 64 65 64 20 .** is included
22d30 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e as part of the n
22d40 61 6d 65 2e 0a 2a 2a 20 50 61 72 61 6d 65 74 65 ame..** Paramete
22d50 72 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 rs of the form "
22d60 3f 22 20 77 69 74 68 6f 75 74 20 61 20 66 6f 6c ?" without a fol
22d70 6c 6f 77 69 6e 67 20 69 6e 74 65 67 65 72 20 68 lowing integer h
22d80 61 76 65 20 6e 6f 20 6e 61 6d 65 0a 2a 2a 20 61 ave no name.** a
22d90 6e 64 20 61 72 65 20 61 6c 73 6f 20 72 65 66 65 nd are also refe
22da0 72 72 65 64 20 74 6f 20 61 73 20 22 61 6e 6f 6e rred to as "anon
22db0 79 6d 6f 75 73 20 70 61 72 61 6d 65 74 65 72 73 ymous parameters
22dc0 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 "..**.** The fir
22dd0 73 74 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 st host paramete
22de0 72 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f r has an index o
22df0 66 20 31 2c 20 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a f 1, not 0..**.*
22e00 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6e * If the value n
22e10 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 is out of range
22e20 20 6f 72 20 69 66 20 74 68 65 20 6e 2d 74 68 20 or if the n-th
22e30 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 parameter is.**
22e40 6e 61 6d 65 6c 65 73 73 2c 20 74 68 65 6e 20 4e nameless, then N
22e50 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e ULL is returned.
22e60 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 The returned s
22e70 74 72 69 6e 67 20 69 73 0a 2a 2a 20 61 6c 77 61 tring is.** alwa
22e80 79 73 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f ys in UTF-8 enco
22e90 64 69 6e 67 20 65 76 65 6e 20 69 66 20 74 68 65 ding even if the
22ea0 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65 72 named parameter
22eb0 20 77 61 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c was.** original
22ec0 6c 79 20 73 70 65 63 69 66 69 65 64 20 61 73 20 ly specified as
22ed0 55 54 46 2d 31 36 20 69 6e 20 5b 73 71 6c 69 74 UTF-16 in [sqlit
22ee0 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 e3_prepare16()]
22ef0 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 or.** [sqlite3_p
22f00 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a repare16_v2()]..
22f10 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
22f20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c [sqlite3_bind_bl
22f30 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 ob|sqlite3_bind(
22f40 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f )],.** [sqlite3_
22f50 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 bind_parameter_c
22f60 6f 75 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 ount()], and.**
22f70 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 [sqlite3_bind_pa
22f80 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d rameter_index()]
22f90 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
22fa0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 32 31 ents:.** [H13621
22fb0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
22fc0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
22fd0 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 te3_bind_paramet
22fe0 65 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f er_name(sqlite3_
22ff0 73 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a stmt*, int);../*
23000 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6e .** CAPI3REF: In
23010 64 65 78 20 4f 66 20 41 20 50 61 72 61 6d 65 74 dex Of A Paramet
23020 65 72 20 57 69 74 68 20 41 20 47 69 76 65 6e 20 er With A Given
23030 4e 61 6d 65 20 7b 48 31 33 36 34 30 7d 20 3c 53 Name {H13640} <S
23040 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 70300>.**.** Ret
23050 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 urn the index of
23060 20 61 6e 20 53 51 4c 20 70 61 72 61 6d 65 74 65 an SQL paramete
23070 72 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d 65 r given its name
23080 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 64 65 78 20 . The.** index
23090 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 value returned i
230a0 73 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75 s suitable for u
230b0 73 65 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 se as the second
230c0 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 74 6f .** parameter to
230d0 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 [sqlite3_bind_b
230e0 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 lob|sqlite3_bind
230f0 28 29 5d 2e 20 20 41 20 7a 65 72 6f 0a 2a 2a 20 ()]. A zero.**
23100 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 6e is returned if n
23110 6f 20 6d 61 74 63 68 69 6e 67 20 70 61 72 61 6d o matching param
23120 65 74 65 72 20 69 73 20 66 6f 75 6e 64 2e 20 20 eter is found.
23130 54 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a The parameter.**
23140 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 67 69 name must be gi
23150 76 65 6e 20 69 6e 20 55 54 46 2d 38 20 65 76 65 ven in UTF-8 eve
23160 6e 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61 n if the origina
23170 6c 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 77 l statement.** w
23180 61 73 20 70 72 65 70 61 72 65 64 20 66 72 6f 6d as prepared from
23190 20 55 54 46 2d 31 36 20 74 65 78 74 20 75 73 69 UTF-16 text usi
231a0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ng [sqlite3_prep
231b0 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a are16_v2()]..**.
231c0 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 ** See also: [sq
231d0 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c lite3_bind_blob|
231e0 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c sqlite3_bind()],
231f0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e .** [sqlite3_bin
23200 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e d_parameter_coun
23210 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 t()], and.** [sq
23220 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d lite3_bind_param
23230 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a eter_index()]..*
23240 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
23250 73 3a 0a 2a 2a 20 5b 48 31 33 36 34 31 5d 0a 2a s:.** [H13641].*
23260 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
23270 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 sqlite3_bind_pa
23280 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 73 71 rameter_index(sq
23290 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 63 6f 6e lite3_stmt*, con
232a0 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 3b st char *zName);
232b0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
232c0 3a 20 52 65 73 65 74 20 41 6c 6c 20 42 69 6e 64 : Reset All Bind
232d0 69 6e 67 73 20 4f 6e 20 41 20 50 72 65 70 61 72 ings On A Prepar
232e0 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 ed Statement {H1
232f0 33 36 36 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 3660} <S70300>.*
23300 2a 0a 2a 2a 20 43 6f 6e 74 72 61 72 79 20 74 6f *.** Contrary to
23310 20 74 68 65 20 69 6e 74 75 69 74 69 6f 6e 20 6f the intuition o
23320 66 20 6d 61 6e 79 2c 20 5b 73 71 6c 69 74 65 33 f many, [sqlite3
23330 5f 72 65 73 65 74 28 29 5d 20 64 6f 65 73 20 6e _reset()] does n
23340 6f 74 20 72 65 73 65 74 0a 2a 2a 20 74 68 65 20 ot reset.** the
23350 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c [sqlite3_bind_bl
23360 6f 62 20 7c 20 62 69 6e 64 69 6e 67 73 5d 20 6f ob | bindings] o
23370 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 n a [prepared st
23380 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 55 73 65 atement]..** Use
23390 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f this routine to
233a0 20 72 65 73 65 74 20 61 6c 6c 20 68 6f 73 74 20 reset all host
233b0 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 4e 55 parameters to NU
233c0 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 LL..**.** Requir
233d0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 ements:.** [H136
233e0 36 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 61].*/.SQLITE_AP
233f0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c I int sqlite3_cl
23400 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c ear_bindings(sql
23410 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a ite3_stmt*);../*
23420 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 .** CAPI3REF: Nu
23430 6d 62 65 72 20 4f 66 20 43 6f 6c 75 6d 6e 73 20 mber Of Columns
23440 49 6e 20 41 20 52 65 73 75 6c 74 20 53 65 74 20 In A Result Set
23450 7b 48 31 33 37 31 30 7d 20 3c 53 31 30 37 30 30 {H13710} <S10700
23460 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 >.**.** Return t
23470 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c he number of col
23480 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 umns in the resu
23490 6c 74 20 73 65 74 20 72 65 74 75 72 6e 65 64 20 lt set returned
234a0 62 79 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 by the.** [prepa
234b0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 red statement].
234c0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 This routine ret
234d0 75 72 6e 73 20 30 20 69 66 20 70 53 74 6d 74 20 urns 0 if pStmt
234e0 69 73 20 61 6e 20 53 51 4c 0a 2a 2a 20 73 74 61 is an SQL.** sta
234f0 74 65 6d 65 6e 74 20 74 68 61 74 20 64 6f 65 73 tement that does
23500 20 6e 6f 74 20 72 65 74 75 72 6e 20 64 61 74 61 not return data
23510 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 61 6e (for example an
23520 20 5b 55 50 44 41 54 45 5d 29 2e 0a 2a 2a 0a 2a [UPDATE])..**.*
23530 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
23540 2a 2a 20 5b 48 31 33 37 31 31 5d 0a 2a 2f 0a 53 ** [H13711].*/.S
23550 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
23560 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 lite3_column_cou
23570 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 nt(sqlite3_stmt
23580 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 *pStmt);../*.**
23590 43 41 50 49 33 52 45 46 3a 20 43 6f 6c 75 6d 6e CAPI3REF: Column
235a0 20 4e 61 6d 65 73 20 49 6e 20 41 20 52 65 73 75 Names In A Resu
235b0 6c 74 20 53 65 74 20 7b 48 31 33 37 32 30 7d 20 lt Set {H13720}
235c0 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S10700>.**.** T
235d0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 hese routines re
235e0 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 61 73 turn the name as
235f0 73 69 67 6e 65 64 20 74 6f 20 61 20 70 61 72 74 signed to a part
23600 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 0a 2a 2a icular column.**
23610 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
23620 65 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d et of a [SELECT]
23630 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 statement. The
23640 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
23650 6e 61 6d 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 name().** interf
23660 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f ace returns a po
23670 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d inter to a zero-
23680 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 terminated UTF-8
23690 20 73 74 72 69 6e 67 0a 2a 2a 20 61 6e 64 20 73 string.** and s
236a0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 qlite3_column_na
236b0 6d 65 31 36 28 29 20 72 65 74 75 72 6e 73 20 61 me16() returns a
236c0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 pointer to a ze
236d0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a ro-terminated.**
236e0 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 UTF-16 string.
236f0 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d The first param
23700 65 74 65 72 20 69 73 20 74 68 65 20 5b 70 72 65 eter is the [pre
23710 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
23720 0a 2a 2a 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 .** that impleme
23730 6e 74 73 20 74 68 65 20 5b 53 45 4c 45 43 54 5d nts the [SELECT]
23740 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 statement. The
23750 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
23760 20 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d is the.** colum
23770 6e 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 6c n number. The l
23780 65 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 eftmost column i
23790 73 20 6e 75 6d 62 65 72 20 30 2e 0a 2a 2a 0a 2a s number 0..**.*
237a0 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 * The returned s
237b0 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 69 73 tring pointer is
237c0 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 65 69 74 valid until eit
237d0 68 65 72 20 74 68 65 20 5b 70 72 65 70 61 72 65 her the [prepare
237e0 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 d statement].**
237f0 69 73 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 is destroyed by
23800 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a [sqlite3_finaliz
23810 65 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20 74 68 e()] or until th
23820 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 0a 2a e next call to.*
23830 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e * sqlite3_column
23840 5f 6e 61 6d 65 28 29 20 6f 72 20 73 71 6c 69 74 _name() or sqlit
23850 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 e3_column_name16
23860 28 29 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 63 () on the same c
23870 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 olumn..**.** If
23880 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
23890 20 66 61 69 6c 73 20 64 75 72 69 6e 67 20 74 68 fails during th
238a0 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 e processing of
238b0 65 69 74 68 65 72 20 72 6f 75 74 69 6e 65 0a 2a either routine.*
238c0 2a 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 64 * (for example d
238d0 75 72 69 6e 67 20 61 20 63 6f 6e 76 65 72 73 69 uring a conversi
238e0 6f 6e 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f on from UTF-8 to
238f0 20 55 54 46 2d 31 36 29 20 74 68 65 6e 20 61 0a UTF-16) then a.
23900 2a 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 ** NULL pointer
23910 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
23920 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 ** The name of a
23930 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 result column i
23940 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 s the value of t
23950 68 65 20 22 41 53 22 20 63 6c 61 75 73 65 20 66 he "AS" clause f
23960 6f 72 0a 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d or.** that colum
23970 6e 2c 20 69 66 20 74 68 65 72 65 20 69 73 20 61 n, if there is a
23980 6e 20 41 53 20 63 6c 61 75 73 65 2e 20 20 49 66 n AS clause. If
23990 20 74 68 65 72 65 20 69 73 20 6e 6f 20 41 53 20 there is no AS
239a0 63 6c 61 75 73 65 0a 2a 2a 20 74 68 65 6e 20 74 clause.** then t
239b0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 63 he name of the c
239c0 6f 6c 75 6d 6e 20 69 73 20 75 6e 73 70 65 63 69 olumn is unspeci
239d0 66 69 65 64 20 61 6e 64 20 6d 61 79 20 63 68 61 fied and may cha
239e0 6e 67 65 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 nge from.** one
239f0 72 65 6c 65 61 73 65 20 6f 66 20 53 51 4c 69 74 release of SQLit
23a00 65 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a e to the next..*
23a10 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
23a20 73 3a 0a 2a 2a 20 5b 48 31 33 37 32 31 5d 20 5b s:.** [H13721] [
23a30 48 31 33 37 32 33 5d 20 5b 48 31 33 37 32 34 5d H13723] [H13724]
23a40 20 5b 48 31 33 37 32 35 5d 20 5b 48 31 33 37 32 [H13725] [H1372
23a50 36 5d 20 5b 48 31 33 37 32 37 5d 0a 2a 2f 0a 53 6] [H13727].*/.S
23a60 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
23a70 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f char *sqlite3_co
23a80 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65 lumn_name(sqlite
23a90 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 3_stmt*, int N);
23aa0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
23ab0 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
23ac0 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 71 column_name16(sq
23ad0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 lite3_stmt*, int
23ae0 20 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 N);../*.** CAPI
23af0 33 52 45 46 3a 20 53 6f 75 72 63 65 20 4f 66 20 3REF: Source Of
23b00 44 61 74 61 20 49 6e 20 41 20 51 75 65 72 79 20 Data In A Query
23b10 52 65 73 75 6c 74 20 7b 48 31 33 37 34 30 7d 20 Result {H13740}
23b20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S10700>.**.** T
23b30 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 70 72 hese routines pr
23b40 6f 76 69 64 65 20 61 20 6d 65 61 6e 73 20 74 6f ovide a means to
23b50 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20 determine what
23b60 63 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 74 0a 2a column of what.*
23b70 2a 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 68 * table in which
23b80 20 64 61 74 61 62 61 73 65 20 61 20 72 65 73 75 database a resu
23b90 6c 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d lt of a [SELECT]
23ba0 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73 statement comes
23bb0 20 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65 20 6e 61 from..** The na
23bc0 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
23bd0 73 65 20 6f 72 20 74 61 62 6c 65 20 6f 72 20 63 se or table or c
23be0 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 72 65 74 olumn can be ret
23bf0 75 72 6e 65 64 20 61 73 0a 2a 2a 20 65 69 74 68 urned as.** eith
23c00 65 72 20 61 20 55 54 46 2d 38 20 6f 72 20 55 54 er a UTF-8 or UT
23c10 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 68 F-16 string. Th
23c20 65 20 5f 64 61 74 61 62 61 73 65 5f 20 72 6f 75 e _database_ rou
23c30 74 69 6e 65 73 20 72 65 74 75 72 6e 0a 2a 2a 20 tines return.**
23c40 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d the database nam
23c50 65 2c 20 74 68 65 20 5f 74 61 62 6c 65 5f 20 72 e, the _table_ r
23c60 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 outines return t
23c70 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2c 20 61 he table name, a
23c80 6e 64 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e nd.** the origin
23c90 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 _ routines retur
23ca0 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d n the column nam
23cb0 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e e..** The return
23cc0 65 64 20 73 74 72 69 6e 67 20 69 73 20 76 61 6c ed string is val
23cd0 69 64 20 75 6e 74 69 6c 20 74 68 65 20 5b 70 72 id until the [pr
23ce0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
23cf0 5d 20 69 73 20 64 65 73 74 72 6f 79 65 64 0a 2a ] is destroyed.*
23d00 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 * using [sqlite3
23d10 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 _finalize()] or
23d20 75 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 20 69 until the same i
23d30 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72 65 nformation is re
23d40 71 75 65 73 74 65 64 0a 2a 2a 20 61 67 61 69 6e quested.** again
23d50 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 in a different
23d60 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 encoding..**.**
23d70 54 68 65 20 6e 61 6d 65 73 20 72 65 74 75 72 6e The names return
23d80 65 64 20 61 72 65 20 74 68 65 20 6f 72 69 67 69 ed are the origi
23d90 6e 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64 20 6e nal un-aliased n
23da0 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 ames of the.** d
23db0 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 2c 20 atabase, table,
23dc0 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a and column..**.*
23dd0 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 * The first argu
23de0 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c ment to the foll
23df0 6f 77 69 6e 67 20 63 61 6c 6c 73 20 69 73 20 61 owing calls is a
23e00 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
23e10 6d 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65 73 65 20 ment]..** These
23e20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e functions return
23e30 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
23e40 75 74 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d ut the Nth colum
23e50 6e 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a n returned by.**
23e60 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 the statement,
23e70 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 where N is the s
23e80 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 econd function a
23e90 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 rgument..**.** I
23ea0 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e f the Nth column
23eb0 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 returned by the
23ec0 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 6e statement is an
23ed0 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 0a 2a expression or.*
23ee0 2a 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 69 * subquery and i
23ef0 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 76 s not a column v
23f00 61 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c 20 6f alue, then all o
23f10 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e f these function
23f20 73 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c s return.** NULL
23f30 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 . These routine
23f40 20 6d 69 67 68 74 20 61 6c 73 6f 20 72 65 74 75 might also retu
23f50 72 6e 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d rn NULL if a mem
23f60 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 ory allocation e
23f70 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 rror.** occurs.
23f80 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 79 Otherwise, they
23f90 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 return the name
23fa0 20 6f 66 20 74 68 65 20 61 74 74 61 63 68 65 64 of the attached
23fb0 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 database, table
23fc0 0a 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 74 .** and column t
23fd0 68 61 74 20 71 75 65 72 79 20 72 65 73 75 6c 74 hat query result
23fe0 20 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78 74 72 column was extr
23ff0 61 63 74 65 64 20 66 72 6f 6d 2e 0a 2a 2a 0a 2a acted from..**.*
24000 2a 20 41 73 20 77 69 74 68 20 61 6c 6c 20 6f 74 * As with all ot
24010 68 65 72 20 53 51 4c 69 74 65 20 41 50 49 73 2c her SQLite APIs,
24020 20 74 68 6f 73 65 20 70 6f 73 74 66 69 78 65 64 those postfixed
24030 20 77 69 74 68 20 22 31 36 22 20 72 65 74 75 72 with "16" retur
24040 6e 0a 2a 2a 20 55 54 46 2d 31 36 20 65 6e 63 6f n.** UTF-16 enco
24050 64 65 64 20 73 74 72 69 6e 67 73 2c 20 74 68 65 ded strings, the
24060 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73 other functions
24070 20 72 65 74 75 72 6e 20 55 54 46 2d 38 2e 20 7b return UTF-8. {
24080 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 END}.**.** These
24090 20 41 50 49 73 20 61 72 65 20 6f 6e 6c 79 20 61 APIs are only a
240a0 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20 vailable if the
240b0 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 library was comp
240c0 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a iled with the.**
240d0 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f [SQLITE_ENABLE_
240e0 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 5d COLUMN_METADATA]
240f0 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20 C-preprocessor
24100 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a symbol defined..
24110 2a 2a 0a 2a 2a 20 7b 41 31 33 37 35 31 7d 0a 2a **.** {A13751}.*
24120 2a 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 * If two or more
24130 20 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e threads call on
24140 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 e or more of the
24150 73 65 20 72 6f 75 74 69 6e 65 73 20 61 67 61 69 se routines agai
24160 6e 73 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 nst the same.**
24170 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
24180 6e 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 61 74 nt and column at
24190 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74 the same time t
241a0 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 hen the results
241b0 61 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64 are.** undefined
241c0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
241d0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 34 31 ents:.** [H13741
241e0 5d 20 5b 48 31 33 37 34 32 5d 20 5b 48 31 33 37 ] [H13742] [H137
241f0 34 33 5d 20 5b 48 31 33 37 34 34 5d 20 5b 48 31 43] [H13744] [H1
24200 33 37 34 35 5d 20 5b 48 31 33 37 34 36 5d 20 5b 3745] [H13746] [
24210 48 31 33 37 34 38 5d 0a 2a 2a 0a 2a 2a 20 49 66 H13748].**.** If
24220 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 74 68 72 two or more thr
24230 65 61 64 73 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 eads call one or
24240 20 6d 6f 72 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 more.** [sqlite
24250 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 3_column_databas
24260 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d 6e 20 e_name | column
24270 6d 65 74 61 64 61 74 61 20 69 6e 74 65 72 66 61 metadata interfa
24280 63 65 73 5d 0a 2a 2a 20 66 6f 72 20 74 68 65 20 ces].** for the
24290 73 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20 73 same [prepared s
242a0 74 61 74 65 6d 65 6e 74 5d 20 61 6e 64 20 72 65 tatement] and re
242b0 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 61 sult column.** a
242c0 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 t the same time
242d0 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 then the results
242e0 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a are undefined..
242f0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f */.SQLITE_API co
24300 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
24310 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 3_column_databas
24320 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 e_name(sqlite3_s
24330 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 tmt*,int);.SQLIT
24340 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
24350 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
24360 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 _database_name16
24370 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 (sqlite3_stmt*,i
24380 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 nt);.SQLITE_API
24390 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
243a0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 te3_column_table
243b0 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 _name(sqlite3_st
243c0 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 mt*,int);.SQLITE
243d0 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
243e0 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
243f0 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c table_name16(sql
24400 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b ite3_stmt*,int);
24410 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
24420 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f t char *sqlite3_
24430 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 column_origin_na
24440 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a me(sqlite3_stmt*
24450 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 ,int);.SQLITE_AP
24460 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
24470 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 lite3_column_ori
24480 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 gin_name16(sqlit
24490 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a e3_stmt*,int);..
244a0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
244b0 44 65 63 6c 61 72 65 64 20 44 61 74 61 74 79 70 Declared Datatyp
244c0 65 20 4f 66 20 41 20 51 75 65 72 79 20 52 65 73 e Of A Query Res
244d0 75 6c 74 20 7b 48 31 33 37 36 30 7d 20 3c 53 31 ult {H13760} <S1
244e0 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0700>.**.** The
244f0 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 first parameter
24500 69 73 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 is a [prepared s
24510 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 66 tatement]..** If
24520 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 this statement
24530 69 73 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 is a [SELECT] st
24540 61 74 65 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 atement and the
24550 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 Nth column of th
24560 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 72 65 e.** returned re
24570 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 61 74 sult set of that
24580 20 5b 53 45 4c 45 43 54 5d 20 69 73 20 61 20 74 [SELECT] is a t
24590 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 28 6e 6f 74 able column (not
245a0 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f an.** expressio
245b0 6e 20 6f 72 20 73 75 62 71 75 65 72 79 29 20 74 n or subquery) t
245c0 68 65 6e 20 74 68 65 20 64 65 63 6c 61 72 65 64 hen the declared
245d0 20 74 79 70 65 20 6f 66 20 74 68 65 20 74 61 62 type of the tab
245e0 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 le.** column is
245f0 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68 returned. If th
24600 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 e Nth column of
24610 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 69 the result set i
24620 73 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 s an.** expressi
24630 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79 2c 20 on or subquery,
24640 74 68 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e then a NULL poin
24650 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e ter is returned.
24660 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 .** The returned
24670 20 73 74 72 69 6e 67 20 69 73 20 61 6c 77 61 79 string is alway
24680 73 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 2e s UTF-8 encoded.
24690 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 46 6f 72 {END}.**.** For
246a0 20 65 78 61 6d 70 6c 65 2c 20 67 69 76 65 6e 20 example, given
246b0 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 the database sch
246c0 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 ema:.**.** CREAT
246d0 45 20 54 41 42 4c 45 20 74 31 28 63 31 20 56 41 E TABLE t1(c1 VA
246e0 52 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a 20 61 6e RIANT);.**.** an
246f0 64 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 d the following
24700 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 statement to be
24710 63 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a 2a 2a 20 compiled:.**.**
24720 53 45 4c 45 43 54 20 63 31 20 2b 20 31 2c 20 63 SELECT c1 + 1, c
24730 31 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a 0a 2a 2a 1 FROM t1;.**.**
24740 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f this routine wo
24750 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 73 uld return the s
24760 74 72 69 6e 67 20 22 56 41 52 49 41 4e 54 22 20 tring "VARIANT"
24770 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 72 for the second r
24780 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 esult.** column
24790 28 69 3d 3d 31 29 2c 20 61 6e 64 20 61 20 4e 55 (i==1), and a NU
247a0 4c 4c 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 74 LL pointer for t
247b0 68 65 20 66 69 72 73 74 20 72 65 73 75 6c 74 20 he first result
247c0 63 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29 2e 0a 2a column (i==0)..*
247d0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 *.** SQLite uses
247e0 20 64 79 6e 61 6d 69 63 20 72 75 6e 2d 74 69 6d dynamic run-tim
247f0 65 20 74 79 70 69 6e 67 2e 20 20 53 6f 20 6a 75 e typing. So ju
24800 73 74 20 62 65 63 61 75 73 65 20 61 20 63 6f 6c st because a col
24810 75 6d 6e 0a 2a 2a 20 69 73 20 64 65 63 6c 61 72 umn.** is declar
24820 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 ed to contain a
24830 70 61 72 74 69 63 75 6c 61 72 20 74 79 70 65 20 particular type
24840 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20 74 68 does not mean th
24850 61 74 20 74 68 65 0a 2a 2a 20 64 61 74 61 20 73 at the.** data s
24860 74 6f 72 65 64 20 69 6e 20 74 68 61 74 20 63 6f tored in that co
24870 6c 75 6d 6e 20 69 73 20 6f 66 20 74 68 65 20 64 lumn is of the d
24880 65 63 6c 61 72 65 64 20 74 79 70 65 2e 20 20 53 eclared type. S
24890 51 4c 69 74 65 20 69 73 0a 2a 2a 20 73 74 72 6f QLite is.** stro
248a0 6e 67 6c 79 20 74 79 70 65 64 2c 20 62 75 74 20 ngly typed, but
248b0 74 68 65 20 74 79 70 69 6e 67 20 69 73 20 64 79 the typing is dy
248c0 6e 61 6d 69 63 20 6e 6f 74 20 73 74 61 74 69 63 namic not static
248d0 2e 20 20 54 79 70 65 0a 2a 2a 20 69 73 20 61 73 . Type.** is as
248e0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 69 6e sociated with in
248f0 64 69 76 69 64 75 61 6c 20 76 61 6c 75 65 73 2c dividual values,
24900 20 6e 6f 74 20 77 69 74 68 20 74 68 65 20 63 6f not with the co
24910 6e 74 61 69 6e 65 72 73 0a 2a 2a 20 75 73 65 64 ntainers.** used
24920 20 74 6f 20 68 6f 6c 64 20 74 68 6f 73 65 20 76 to hold those v
24930 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 alues..**.** Req
24940 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
24950 31 33 37 36 31 5d 20 5b 48 31 33 37 36 32 5d 20 13761] [H13762]
24960 5b 48 31 33 37 36 33 5d 0a 2a 2f 0a 53 51 4c 49 [H13763].*/.SQLI
24970 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 TE_API const cha
24980 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d r *sqlite3_colum
24990 6e 5f 64 65 63 6c 74 79 70 65 28 73 71 6c 69 74 n_decltype(sqlit
249a0 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 e3_stmt*,int);.S
249b0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
249c0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f void *sqlite3_co
249d0 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 lumn_decltype16(
249e0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e sqlite3_stmt*,in
249f0 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 t);../*.** CAPI3
24a00 52 45 46 3a 20 45 76 61 6c 75 61 74 65 20 41 6e REF: Evaluate An
24a10 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 7b SQL Statement {
24a20 48 31 33 32 30 30 7d 20 3c 53 31 30 30 30 30 3e H13200} <S10000>
24a30 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 20 5b .**.** After a [
24a40 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
24a50 6e 74 5d 20 68 61 73 20 62 65 65 6e 20 70 72 65 nt] has been pre
24a60 70 61 72 65 64 20 75 73 69 6e 67 20 65 69 74 68 pared using eith
24a70 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 er.** [sqlite3_p
24a80 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 repare_v2()] or
24a90 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
24aa0 31 36 5f 76 32 28 29 5d 20 6f 72 20 6f 6e 65 20 16_v2()] or one
24ab0 6f 66 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a of the legacy.**
24ac0 20 69 6e 74 65 72 66 61 63 65 73 20 5b 73 71 6c interfaces [sql
24ad0 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 ite3_prepare()]
24ae0 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 or [sqlite3_prep
24af0 61 72 65 31 36 28 29 5d 2c 20 74 68 69 73 20 66 are16()], this f
24b00 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 unction.** must
24b10 62 65 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 be called one or
24b20 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 65 more times to e
24b30 76 61 6c 75 61 74 65 20 74 68 65 20 73 74 61 74 valuate the stat
24b40 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ement..**.** The
24b50 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 details of the
24b60 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 20 behavior of the
24b70 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69 sqlite3_step() i
24b80 6e 74 65 72 66 61 63 65 20 64 65 70 65 6e 64 0a nterface depend.
24b90 2a 2a 20 6f 6e 20 77 68 65 74 68 65 72 20 74 68 ** on whether th
24ba0 65 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 e statement was
24bb0 70 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 74 prepared using t
24bc0 68 65 20 6e 65 77 65 72 20 22 76 32 22 20 69 6e he newer "v2" in
24bd0 74 65 72 66 61 63 65 0a 2a 2a 20 5b 73 71 6c 69 terface.** [sqli
24be0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
24bf0 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 ] and [sqlite3_p
24c00 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 6f repare16_v2()] o
24c10 72 20 74 68 65 20 6f 6c 64 65 72 20 6c 65 67 61 r the older lega
24c20 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 cy.** interface
24c30 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
24c40 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
24c50 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2e 20 20 _prepare16()].
24c60 54 68 65 20 75 73 65 20 6f 66 20 74 68 65 0a 2a The use of the.*
24c70 2a 20 6e 65 77 20 22 76 32 22 20 69 6e 74 65 72 * new "v2" inter
24c80 66 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e face is recommen
24c90 64 65 64 20 66 6f 72 20 6e 65 77 20 61 70 70 6c ded for new appl
24ca0 69 63 61 74 69 6f 6e 73 20 62 75 74 20 74 68 65 ications but the
24cb0 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 legacy.** inter
24cc0 66 61 63 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e face will contin
24cd0 75 65 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 ue to be support
24ce0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 ed..**.** In the
24cf0 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 63 legacy interfac
24d00 65 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 e, the return va
24d10 6c 75 65 20 77 69 6c 6c 20 62 65 20 65 69 74 68 lue will be eith
24d20 65 72 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d er [SQLITE_BUSY]
24d30 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e ,.** [SQLITE_DON
24d40 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d E], [SQLITE_ROW]
24d50 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d , [SQLITE_ERROR]
24d60 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d 49 53 , or [SQLITE_MIS
24d70 55 53 45 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 68 USE]..** With th
24d80 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 e "v2" interface
24d90 2c 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68 , any of the oth
24da0 65 72 20 5b 72 65 73 75 6c 74 20 63 6f 64 65 73 er [result codes
24db0 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 ] or.** [extende
24dc0 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 d result codes]
24dd0 6d 69 67 68 74 20 62 65 20 72 65 74 75 72 6e 65 might be returne
24de0 64 20 61 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a d as well..**.**
24df0 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6d [SQLITE_BUSY] m
24e00 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 64 61 eans that the da
24e10 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20 77 61 tabase engine wa
24e20 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 63 71 75 s unable to acqu
24e30 69 72 65 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 ire the.** datab
24e40 61 73 65 20 6c 6f 63 6b 73 20 69 74 20 6e 65 65 ase locks it nee
24e50 64 73 20 74 6f 20 64 6f 20 69 74 73 20 6a 6f 62 ds to do its job
24e60 2e 20 20 49 66 20 74 68 65 20 73 74 61 74 65 6d . If the statem
24e70 65 6e 74 20 69 73 20 61 20 5b 43 4f 4d 4d 49 54 ent is a [COMMIT
24e80 5d 0a 2a 2a 20 6f 72 20 6f 63 63 75 72 73 20 6f ].** or occurs o
24e90 75 74 73 69 64 65 20 6f 66 20 61 6e 20 65 78 70 utside of an exp
24ea0 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f licit transactio
24eb0 6e 2c 20 74 68 65 6e 20 79 6f 75 20 63 61 6e 20 n, then you can
24ec0 72 65 74 72 79 20 74 68 65 0a 2a 2a 20 73 74 61 retry the.** sta
24ed0 74 65 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 20 tement. If the
24ee0 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f 74 statement is not
24ef0 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 61 6e 64 20 a [COMMIT] and
24f00 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 61 0a occurs within a.
24f10 2a 2a 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e ** explicit tran
24f20 73 61 63 74 69 6f 6e 20 74 68 65 6e 20 79 6f 75 saction then you
24f30 20 73 68 6f 75 6c 64 20 72 6f 6c 6c 62 61 63 6b should rollback
24f40 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
24f50 20 62 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 before.** conti
24f60 6e 75 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 nuing..**.** [SQ
24f70 4c 49 54 45 5f 44 4f 4e 45 5d 20 6d 65 61 6e 73 LITE_DONE] means
24f80 20 74 68 61 74 20 74 68 65 20 73 74 61 74 65 6d that the statem
24f90 65 6e 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 ent has finished
24fa0 20 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20 73 75 executing.** su
24fb0 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 73 71 6c ccessfully. sql
24fc0 69 74 65 33 5f 73 74 65 70 28 29 20 73 68 6f 75 ite3_step() shou
24fd0 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 ld not be called
24fe0 20 61 67 61 69 6e 20 6f 6e 20 74 68 69 73 20 76 again on this v
24ff0 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e irtual.** machin
25000 65 20 77 69 74 68 6f 75 74 20 66 69 72 73 74 20 e without first
25010 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 calling [sqlite3
25020 5f 72 65 73 65 74 28 29 5d 20 74 6f 20 72 65 73 _reset()] to res
25030 65 74 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a et the virtual.*
25040 2a 20 6d 61 63 68 69 6e 65 20 62 61 63 6b 20 74 * machine back t
25050 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20 73 74 o its initial st
25060 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ate..**.** If th
25070 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 e SQL statement
25080 62 65 69 6e 67 20 65 78 65 63 75 74 65 64 20 72 being executed r
25090 65 74 75 72 6e 73 20 61 6e 79 20 64 61 74 61 2c eturns any data,
250a0 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 52 4f then [SQLITE_RO
250b0 57 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 W].** is returne
250c0 64 20 65 61 63 68 20 74 69 6d 65 20 61 20 6e 65 d each time a ne
250d0 77 20 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73 w row of data is
250e0 20 72 65 61 64 79 20 66 6f 72 20 70 72 6f 63 65 ready for proce
250f0 73 73 69 6e 67 20 62 79 20 74 68 65 0a 2a 2a 20 ssing by the.**
25100 63 61 6c 6c 65 72 2e 20 54 68 65 20 76 61 6c 75 caller. The valu
25110 65 73 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 es may be access
25120 65 64 20 75 73 69 6e 67 20 74 68 65 20 5b 63 6f ed using the [co
25130 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e 63 lumn access func
25140 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 73 71 6c 69 74 tions]..** sqlit
25150 65 33 5f 73 74 65 70 28 29 20 69 73 20 63 61 6c e3_step() is cal
25160 6c 65 64 20 61 67 61 69 6e 20 74 6f 20 72 65 74 led again to ret
25170 72 69 65 76 65 20 74 68 65 20 6e 65 78 74 20 72 rieve the next r
25180 6f 77 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a ow of data..**.*
25190 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d * [SQLITE_ERROR]
251a0 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 72 75 means that a ru
251b0 6e 2d 74 69 6d 65 20 65 72 72 6f 72 20 28 73 75 n-time error (su
251c0 63 68 20 61 73 20 61 20 63 6f 6e 73 74 72 61 69 ch as a constrai
251d0 6e 74 0a 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e 29 nt.** violation)
251e0 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 20 has occurred.
251f0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 73 sqlite3_step() s
25200 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c hould not be cal
25210 6c 65 64 20 61 67 61 69 6e 20 6f 6e 0a 2a 2a 20 led again on.**
25220 74 68 65 20 56 4d 2e 20 4d 6f 72 65 20 69 6e 66 the VM. More inf
25230 6f 72 6d 61 74 69 6f 6e 20 6d 61 79 20 62 65 20 ormation may be
25240 66 6f 75 6e 64 20 62 79 20 63 61 6c 6c 69 6e 67 found by calling
25250 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 [sqlite3_errmsg
25260 28 29 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 68 65 ()]..** With the
25270 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 63 legacy interfac
25280 65 2c 20 61 20 6d 6f 72 65 20 73 70 65 63 69 66 e, a more specif
25290 69 63 20 65 72 72 6f 72 20 63 6f 64 65 20 28 66 ic error code (f
252a0 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 5b or example,.** [
252b0 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 SQLITE_INTERRUPT
252c0 5d 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d ], [SQLITE_SCHEM
252d0 41 5d 2c 20 5b 53 51 4c 49 54 45 5f 43 4f 52 52 A], [SQLITE_CORR
252e0 55 50 54 5d 2c 20 61 6e 64 20 73 6f 20 66 6f 72 UPT], and so for
252f0 74 68 29 0a 2a 2a 20 63 61 6e 20 62 65 20 6f 62 th).** can be ob
25300 74 61 69 6e 65 64 20 62 79 20 63 61 6c 6c 69 6e tained by callin
25310 67 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 g [sqlite3_reset
25320 28 29 5d 20 6f 6e 20 74 68 65 0a 2a 2a 20 5b 70 ()] on the.** [p
25330 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
25340 74 5d 2e 20 20 49 6e 20 74 68 65 20 22 76 32 22 t]. In the "v2"
25350 20 69 6e 74 65 72 66 61 63 65 2c 0a 2a 2a 20 74 interface,.** t
25360 68 65 20 6d 6f 72 65 20 73 70 65 63 69 66 69 63 he more specific
25370 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
25380 65 74 75 72 6e 65 64 20 64 69 72 65 63 74 6c 79 eturned directly
25390 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 70 by sqlite3_step
253a0 28 29 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 ()..**.** [SQLIT
253b0 45 5f 4d 49 53 55 53 45 5d 20 6d 65 61 6e 73 20 E_MISUSE] means
253c0 74 68 61 74 20 74 68 65 20 74 68 69 73 20 72 6f that the this ro
253d0 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 utine was called
253e0 20 69 6e 61 70 70 72 6f 70 72 69 61 74 65 6c 79 inappropriately
253f0 2e 0a 2a 2a 20 50 65 72 68 61 70 73 20 69 74 20 ..** Perhaps it
25400 77 61 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 was called on a
25410 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
25420 65 6e 74 5d 20 74 68 61 74 20 68 61 73 0a 2a 2a ent] that has.**
25430 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 5b 73 already been [s
25440 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 qlite3_finalize
25450 7c 20 66 69 6e 61 6c 69 7a 65 64 5d 20 6f 72 20 | finalized] or
25460 6f 6e 20 6f 6e 65 20 74 68 61 74 20 68 61 64 0a on one that had.
25470 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 ** previously re
25480 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 45 turned [SQLITE_E
25490 52 52 4f 52 5d 20 6f 72 20 5b 53 51 4c 49 54 45 RROR] or [SQLITE
254a0 5f 44 4f 4e 45 5d 2e 20 20 4f 72 20 69 74 20 63 _DONE]. Or it c
254b0 6f 75 6c 64 0a 2a 2a 20 62 65 20 74 68 65 20 63 ould.** be the c
254c0 61 73 65 20 74 68 61 74 20 74 68 65 20 73 61 6d ase that the sam
254d0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
254e0 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 75 ction is being u
254f0 73 65 64 20 62 79 20 74 77 6f 20 6f 72 0a 2a 2a sed by two or.**
25500 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20 61 74 more threads at
25510 20 74 68 65 20 73 61 6d 65 20 6d 6f 6d 65 6e 74 the same moment
25520 20 69 6e 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 in time..**.**
25530 3c 62 3e 47 6f 6f 66 79 20 49 6e 74 65 72 66 61 <b>Goofy Interfa
25540 63 65 20 41 6c 65 72 74 3a 3c 2f 62 3e 20 49 6e ce Alert:</b> In
25550 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e 74 65 the legacy inte
25560 72 66 61 63 65 2c 20 74 68 65 20 73 71 6c 69 74 rface, the sqlit
25570 65 33 5f 73 74 65 70 28 29 0a 2a 2a 20 41 50 49 e3_step().** API
25580 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 always returns
25590 61 20 67 65 6e 65 72 69 63 20 65 72 72 6f 72 20 a generic error
255a0 63 6f 64 65 2c 20 5b 53 51 4c 49 54 45 5f 45 52 code, [SQLITE_ER
255b0 52 4f 52 5d 2c 20 66 6f 6c 6c 6f 77 69 6e 67 20 ROR], following
255c0 61 6e 79 0a 2a 2a 20 65 72 72 6f 72 20 6f 74 68 any.** error oth
255d0 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f er than [SQLITE_
255e0 42 55 53 59 5d 20 61 6e 64 20 5b 53 51 4c 49 54 BUSY] and [SQLIT
255f0 45 5f 4d 49 53 55 53 45 5d 2e 20 20 59 6f 75 20 E_MISUSE]. You
25600 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a 20 5b 73 71 must call.** [sq
25610 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f lite3_reset()] o
25620 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c r [sqlite3_final
25630 69 7a 65 28 29 5d 20 69 6e 20 6f 72 64 65 72 20 ize()] in order
25640 74 6f 20 66 69 6e 64 20 6f 6e 65 20 6f 66 20 74 to find one of t
25650 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 5b he.** specific [
25660 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 74 68 61 error codes] tha
25670 74 20 62 65 74 74 65 72 20 64 65 73 63 72 69 62 t better describ
25680 65 73 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2a es the error..**
25690 20 57 65 20 61 64 6d 69 74 20 74 68 61 74 20 74 We admit that t
256a0 68 69 73 20 69 73 20 61 20 67 6f 6f 66 79 20 64 his is a goofy d
256b0 65 73 69 67 6e 2e 20 20 54 68 65 20 70 72 6f 62 esign. The prob
256c0 6c 65 6d 20 68 61 73 20 62 65 65 6e 20 66 69 78 lem has been fix
256d0 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 22 ed.** with the "
256e0 76 32 22 20 69 6e 74 65 72 66 61 63 65 2e 20 20 v2" interface.
256f0 49 66 20 79 6f 75 20 70 72 65 70 61 72 65 20 61 If you prepare a
25700 6c 6c 20 6f 66 20 79 6f 75 72 20 53 51 4c 20 73 ll of your SQL s
25710 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 75 73 69 tatements.** usi
25720 6e 67 20 65 69 74 68 65 72 20 5b 73 71 6c 69 74 ng either [sqlit
25730 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d e3_prepare_v2()]
25740 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 or [sqlite3_pre
25750 70 61 72 65 31 36 5f 76 32 28 29 5d 20 69 6e 73 pare16_v2()] ins
25760 74 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6c tead.** of the l
25770 65 67 61 63 79 20 5b 73 71 6c 69 74 65 33 5f 70 egacy [sqlite3_p
25780 72 65 70 61 72 65 28 29 5d 20 61 6e 64 20 5b 73 repare()] and [s
25790 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 qlite3_prepare16
257a0 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 2c 0a ()] interfaces,.
257b0 2a 2a 20 74 68 65 6e 20 74 68 65 20 6d 6f 72 65 ** then the more
257c0 20 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f 72 specific [error
257d0 20 63 6f 64 65 73 5d 20 61 72 65 20 72 65 74 75 codes] are retu
257e0 72 6e 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a rned directly.**
257f0 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 70 by sqlite3_step
25800 28 29 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20 (). The use of
25810 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 the "v2" interfa
25820 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 ce is recommende
25830 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 d..**.** Require
25840 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 32 30 ments:.** [H1320
25850 32 5d 20 5b 48 31 35 33 30 34 5d 20 5b 48 31 35 2] [H15304] [H15
25860 33 30 36 5d 20 5b 48 31 35 33 30 38 5d 20 5b 48 306] [H15308] [H
25870 31 35 33 31 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 15310].*/.SQLITE
25880 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
25890 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 _step(sqlite3_st
258a0 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 mt*);../*.** CAP
258b0 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 6f 66 I3REF: Number of
258c0 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 72 65 columns in a re
258d0 73 75 6c 74 20 73 65 74 20 7b 48 31 33 37 37 30 sult set {H13770
258e0 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a } <S10700>.**.**
258f0 20 52 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d Returns the num
25900 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 69 6e ber of values in
25910 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 the current row
25920 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 of the result s
25930 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 et..**.** Requir
25940 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 ements:.** [H137
25950 37 31 5d 20 5b 48 31 33 37 37 32 5d 0a 2a 2f 0a 71] [H13772].*/.
25960 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
25970 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e qlite3_data_coun
25980 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a t(sqlite3_stmt *
25990 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 pStmt);../*.** C
259a0 41 50 49 33 52 45 46 3a 20 46 75 6e 64 61 6d 65 API3REF: Fundame
259b0 6e 74 61 6c 20 44 61 74 61 74 79 70 65 73 20 7b ntal Datatypes {
259c0 48 31 30 32 36 35 7d 20 3c 53 31 30 31 31 30 3e H10265} <S10110>
259d0 3c 53 31 30 31 32 30 3e 0a 2a 2a 20 4b 45 59 57 <S10120>.** KEYW
259e0 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f 54 45 58 ORDS: SQLITE_TEX
259f0 54 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 36 36 7d T.**.** {H10266}
25a00 20 45 76 65 72 79 20 76 61 6c 75 65 20 69 6e 20 Every value in
25a10 53 51 4c 69 74 65 20 68 61 73 20 6f 6e 65 20 6f SQLite has one o
25a20 66 20 66 69 76 65 20 66 75 6e 64 61 6d 65 6e 74 f five fundament
25a30 61 6c 20 64 61 74 61 74 79 70 65 73 3a 0a 2a 2a al datatypes:.**
25a40 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e .** <ul>.** <li>
25a50 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 64-bit signed i
25a60 6e 74 65 67 65 72 0a 2a 2a 20 3c 6c 69 3e 20 36 nteger.** <li> 6
25a70 34 2d 62 69 74 20 49 45 45 45 20 66 6c 6f 61 74 4-bit IEEE float
25a80 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 ing point number
25a90 0a 2a 2a 20 3c 6c 69 3e 20 73 74 72 69 6e 67 0a .** <li> string.
25aa0 2a 2a 20 3c 6c 69 3e 20 42 4c 4f 42 0a 2a 2a 20 ** <li> BLOB.**
25ab0 3c 6c 69 3e 20 4e 55 4c 4c 0a 2a 2a 20 3c 2f 75 <li> NULL.** </u
25ac0 6c 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 l> {END}.**.** T
25ad0 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 hese constants a
25ae0 72 65 20 63 6f 64 65 73 20 66 6f 72 20 65 61 63 re codes for eac
25af0 68 20 6f 66 20 74 68 6f 73 65 20 74 79 70 65 73 h of those types
25b00 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
25b10 74 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 58 t the SQLITE_TEX
25b20 54 20 63 6f 6e 73 74 61 6e 74 20 77 61 73 20 61 T constant was a
25b30 6c 73 6f 20 75 73 65 64 20 69 6e 20 53 51 4c 69 lso used in SQLi
25b40 74 65 20 76 65 72 73 69 6f 6e 20 32 0a 2a 2a 20 te version 2.**
25b50 66 6f 72 20 61 20 63 6f 6d 70 6c 65 74 65 6c 79 for a completely
25b60 20 64 69 66 66 65 72 65 6e 74 20 6d 65 61 6e 69 different meani
25b70 6e 67 2e 20 20 53 6f 66 74 77 61 72 65 20 74 68 ng. Software th
25b80 61 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e 73 74 at links against
25b90 20 62 6f 74 68 0a 2a 2a 20 53 51 4c 69 74 65 20 both.** SQLite
25ba0 76 65 72 73 69 6f 6e 20 32 20 61 6e 64 20 53 51 version 2 and SQ
25bb0 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 20 73 Lite version 3 s
25bc0 68 6f 75 6c 64 20 75 73 65 20 53 51 4c 49 54 45 hould use SQLITE
25bd0 33 5f 54 45 58 54 2c 20 6e 6f 74 0a 2a 2a 20 53 3_TEXT, not.** S
25be0 51 4c 49 54 45 5f 54 45 58 54 2e 0a 2a 2f 0a 23 QLITE_TEXT..*/.#
25bf0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e define SQLITE_IN
25c00 54 45 47 45 52 20 20 31 0a 23 64 65 66 69 6e 65 TEGER 1.#define
25c10 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 20 20 SQLITE_FLOAT
25c20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 2.#define SQLIT
25c30 45 5f 42 4c 4f 42 20 20 20 20 20 34 0a 23 64 65 E_BLOB 4.#de
25c40 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c fine SQLITE_NULL
25c50 20 20 20 20 20 35 0a 23 69 66 64 65 66 20 53 51 5.#ifdef SQ
25c60 4c 49 54 45 5f 54 45 58 54 0a 23 20 75 6e 64 65 LITE_TEXT.# unde
25c70 66 20 53 51 4c 49 54 45 5f 54 45 58 54 0a 23 65 f SQLITE_TEXT.#e
25c80 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c lse.# define SQL
25c90 49 54 45 5f 54 45 58 54 20 20 20 20 20 33 0a 23 ITE_TEXT 3.#
25ca0 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 53 51 endif.#define SQ
25cb0 4c 49 54 45 33 5f 54 45 58 54 20 20 20 20 20 33 LITE3_TEXT 3
25cc0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
25cd0 3a 20 52 65 73 75 6c 74 20 56 61 6c 75 65 73 20 : Result Values
25ce0 46 72 6f 6d 20 41 20 51 75 65 72 79 20 7b 48 31 From A Query {H1
25cf0 33 38 30 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 3800} <S10700>.*
25d00 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 63 6f 6c * KEYWORDS: {col
25d10 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e 63 74 umn access funct
25d20 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 ions}.**.** Thes
25d30 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 6d 20 e routines form
25d40 74 68 65 20 22 72 65 73 75 6c 74 20 73 65 74 20 the "result set
25d50 71 75 65 72 79 22 20 69 6e 74 65 72 66 61 63 65 query" interface
25d60 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f ..**.** These ro
25d70 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 69 6e utines return in
25d80 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
25d90 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 a single column
25da0 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a of the current.*
25db0 2a 20 72 65 73 75 6c 74 20 72 6f 77 20 6f 66 20 * result row of
25dc0 61 20 71 75 65 72 79 2e 20 20 49 6e 20 65 76 65 a query. In eve
25dd0 72 79 20 63 61 73 65 20 74 68 65 20 66 69 72 73 ry case the firs
25de0 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 t argument is a
25df0 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 pointer.** to th
25e00 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 e [prepared stat
25e10 65 6d 65 6e 74 5d 20 74 68 61 74 20 69 73 20 62 ement] that is b
25e20 65 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20 28 eing evaluated (
25e30 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d the [sqlite3_stm
25e40 74 2a 5d 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 t*].** that was
25e50 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 5b 73 returned from [s
25e60 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
25e70 32 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 69 2()] or one of i
25e80 74 73 20 76 61 72 69 61 6e 74 73 29 0a 2a 2a 20 ts variants).**
25e90 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 61 and the second a
25ea0 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 69 rgument is the i
25eb0 6e 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 ndex of the colu
25ec0 6d 6e 20 66 6f 72 20 77 68 69 63 68 20 69 6e 66 mn for which inf
25ed0 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 ormation.** shou
25ee0 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 ld be returned.
25ef0 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 63 6f The leftmost co
25f00 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 lumn of the resu
25f10 6c 74 20 73 65 74 20 68 61 73 20 74 68 65 20 69 lt set has the i
25f20 6e 64 65 78 20 30 2e 0a 2a 2a 20 54 68 65 20 6e ndex 0..** The n
25f30 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
25f40 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 63 in the result c
25f50 61 6e 20 62 65 20 64 65 74 65 72 6d 69 6e 65 64 an be determined
25f60 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 using.** [sqlit
25f70 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 e3_column_count(
25f80 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 )]..**.** If the
25f90 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 64 SQL statement d
25fa0 6f 65 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c oes not currentl
25fb0 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c y point to a val
25fc0 69 64 20 72 6f 77 2c 20 6f 72 20 69 66 20 74 68 id row, or if th
25fd0 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 e.** column inde
25fe0 78 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 x is out of rang
25ff0 65 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 e, the result is
26000 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 54 undefined..** T
26010 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 61 hese routines ma
26020 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 y only be called
26030 20 77 68 65 6e 20 74 68 65 20 6d 6f 73 74 20 72 when the most r
26040 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a ecent call to.**
26050 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
26060 5d 20 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b ] has returned [
26070 53 51 4c 49 54 45 5f 52 4f 57 5d 20 61 6e 64 20 SQLITE_ROW] and
26080 6e 65 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c 69 neither.** [sqli
26090 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6e 6f 72 te3_reset()] nor
260a0 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 [sqlite3_finali
260b0 7a 65 28 29 5d 20 68 61 76 65 20 62 65 65 6e 20 ze()] have been
260c0 63 61 6c 6c 65 64 20 73 75 62 73 65 71 75 65 6e called subsequen
260d0 74 6c 79 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 6f tly..** If any o
260e0 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 f these routines
260f0 20 61 72 65 20 63 61 6c 6c 65 64 20 61 66 74 65 are called afte
26100 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 r [sqlite3_reset
26110 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 ()] or.** [sqlit
26120 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f e3_finalize()] o
26130 72 20 61 66 74 65 72 20 5b 73 71 6c 69 74 65 33 r after [sqlite3
26140 5f 73 74 65 70 28 29 5d 20 68 61 73 20 72 65 74 _step()] has ret
26150 75 72 6e 65 64 0a 2a 2a 20 73 6f 6d 65 74 68 69 urned.** somethi
26160 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 ng other than [S
26170 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 74 68 65 20 QLITE_ROW], the
26180 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65 results are unde
26190 66 69 6e 65 64 2e 0a 2a 2a 20 49 66 20 5b 73 71 fined..** If [sq
261a0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 lite3_step()] or
261b0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 [sqlite3_reset(
261c0 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 )] or [sqlite3_f
261d0 69 6e 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 61 72 inalize()].** ar
261e0 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 20 e called from a
261f0 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 different thread
26200 20 77 68 69 6c 65 20 61 6e 79 20 6f 66 20 74 68 while any of th
26210 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 ese routines.**
26220 61 72 65 20 70 65 6e 64 69 6e 67 2c 20 74 68 65 are pending, the
26230 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 n the results ar
26240 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a e undefined..**.
26250 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 ** The sqlite3_c
26260 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 72 6f 75 olumn_type() rou
26270 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 tine returns the
26280 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 .** [SQLITE_INTE
26290 47 45 52 20 7c 20 64 61 74 61 74 79 70 65 20 63 GER | datatype c
262a0 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 69 6e 69 ode] for the ini
262b0 74 69 61 6c 20 64 61 74 61 20 74 79 70 65 0a 2a tial data type.*
262c0 2a 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 * of the result
262d0 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 72 65 74 column. The ret
262e0 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 6f urned value is o
262f0 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e ne of [SQLITE_IN
26300 54 45 47 45 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 TEGER],.** [SQLI
26310 54 45 5f 46 4c 4f 41 54 5d 2c 20 5b 53 51 4c 49 TE_FLOAT], [SQLI
26320 54 45 5f 54 45 58 54 5d 2c 20 5b 53 51 4c 49 54 TE_TEXT], [SQLIT
26330 45 5f 42 4c 4f 42 5d 2c 20 6f 72 20 5b 53 51 4c E_BLOB], or [SQL
26340 49 54 45 5f 4e 55 4c 4c 5d 2e 20 20 54 68 65 20 ITE_NULL]. The
26350 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65 value.** returne
26360 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c d by sqlite3_col
26370 75 6d 6e 5f 74 79 70 65 28 29 20 69 73 20 6f 6e umn_type() is on
26380 6c 79 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 ly meaningful if
26390 20 6e 6f 20 74 79 70 65 0a 2a 2a 20 63 6f 6e 76 no type.** conv
263a0 65 72 73 69 6f 6e 73 20 68 61 76 65 20 6f 63 63 ersions have occ
263b0 75 72 72 65 64 20 61 73 20 64 65 73 63 72 69 62 urred as describ
263c0 65 64 20 62 65 6c 6f 77 2e 20 20 41 66 74 65 72 ed below. After
263d0 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 a type conversi
263e0 6f 6e 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 on,.** the value
263f0 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c returned by sql
26400 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 ite3_column_type
26410 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e () is undefined.
26420 20 20 46 75 74 75 72 65 0a 2a 2a 20 76 65 72 73 Future.** vers
26430 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d ions of SQLite m
26440 61 79 20 63 68 61 6e 67 65 20 74 68 65 20 62 65 ay change the be
26450 68 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 havior of sqlite
26460 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 0a 3_column_type().
26470 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 74 ** following a t
26480 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a ype conversion..
26490 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 **.** If the res
264a0 75 6c 74 20 69 73 20 61 20 42 4c 4f 42 20 6f 72 ult is a BLOB or
264b0 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74 68 UTF-8 string th
264c0 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 en the sqlite3_c
264d0 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a olumn_bytes().**
264e0 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
264f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
26500 79 74 65 73 20 69 6e 20 74 68 61 74 20 42 4c 4f ytes in that BLO
26510 42 20 6f 72 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 B or string..**
26520 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 If the result is
26530 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 a UTF-16 string
26540 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 , then sqlite3_c
26550 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 63 6f olumn_bytes() co
26560 6e 76 65 72 74 73 0a 2a 2a 20 74 68 65 20 73 74 nverts.** the st
26570 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 20 61 6e ring to UTF-8 an
26580 64 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20 74 d then returns t
26590 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
265a0 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 es..** If the re
265b0 73 75 6c 74 20 69 73 20 61 20 6e 75 6d 65 72 69 sult is a numeri
265c0 63 20 76 61 6c 75 65 20 74 68 65 6e 20 73 71 6c c value then sql
265d0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
265e0 73 28 29 20 75 73 65 73 0a 2a 2a 20 5b 73 71 6c s() uses.** [sql
265f0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d ite3_snprintf()]
26600 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 61 74 to convert that
26610 20 76 61 6c 75 65 20 74 6f 20 61 20 55 54 46 2d value to a UTF-
26620 38 20 73 74 72 69 6e 67 20 61 6e 64 20 72 65 74 8 string and ret
26630 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 urns.** the numb
26640 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 er of bytes in t
26650 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 54 hat string..** T
26660 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
26670 64 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 d does not inclu
26680 64 65 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d de the zero term
26690 69 6e 61 74 6f 72 20 61 74 20 74 68 65 20 65 6e inator at the en
266a0 64 0a 2a 2a 20 6f 66 20 74 68 65 20 73 74 72 69 d.** of the stri
266b0 6e 67 2e 20 20 46 6f 72 20 63 6c 61 72 69 74 79 ng. For clarity
266c0 3a 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 : the value retu
266d0 72 6e 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62 rned is the numb
266e0 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69 er of.** bytes i
266f0 6e 20 74 68 65 20 73 74 72 69 6e 67 2c 20 6e 6f n the string, no
26700 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 t the number of
26710 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a characters..**.*
26720 2a 20 53 74 72 69 6e 67 73 20 72 65 74 75 72 6e * Strings return
26730 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f ed by sqlite3_co
26740 6c 75 6d 6e 5f 74 65 78 74 28 29 20 61 6e 64 20 lumn_text() and
26750 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
26760 65 78 74 31 36 28 29 2c 0a 2a 2a 20 65 76 65 6e ext16(),.** even
26770 20 65 6d 70 74 79 20 73 74 72 69 6e 67 73 2c 20 empty strings,
26780 61 72 65 20 61 6c 77 61 79 73 20 7a 65 72 6f 20 are always zero
26790 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65 terminated. The
267a0 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 return.** value
267b0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f from sqlite3_co
267c0 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 72 20 lumn_blob() for
267d0 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42 4c a zero-length BL
267e0 4f 42 20 69 73 20 61 6e 20 61 72 62 69 74 72 61 OB is an arbitra
267f0 72 79 0a 2a 2a 20 70 6f 69 6e 74 65 72 2c 20 70 ry.** pointer, p
26800 6f 73 73 69 62 6c 79 20 65 76 65 6e 20 61 20 4e ossibly even a N
26810 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a ULL pointer..**.
26820 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 ** The sqlite3_c
26830 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 20 olumn_bytes16()
26840 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c routine is simil
26850 61 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f ar to sqlite3_co
26860 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 lumn_bytes().**
26870 62 75 74 20 6c 65 61 76 65 73 20 74 68 65 20 72 but leaves the r
26880 65 73 75 6c 74 20 69 6e 20 55 54 46 2d 31 36 20 esult in UTF-16
26890 69 6e 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f in native byte o
268a0 72 64 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 rder instead of
268b0 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 7a 65 UTF-8..** The ze
268c0 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 73 ro terminator is
268d0 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e not included in
268e0 20 74 68 69 73 20 63 6f 75 6e 74 2e 0a 2a 2a 0a this count..**.
268f0 2a 2a 20 54 68 65 20 6f 62 6a 65 63 74 20 72 65 ** The object re
26900 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 turned by [sqlit
26910 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 e3_column_value(
26920 29 5d 20 69 73 20 61 6e 0a 2a 2a 20 5b 75 6e 70 )] is an.** [unp
26930 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 rotected sqlite3
26940 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 2e 20 _value] object.
26950 20 41 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 20 An unprotected
26960 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 sqlite3_value ob
26970 6a 65 63 74 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 ject.** may only
26980 20 62 65 20 75 73 65 64 20 77 69 74 68 20 5b 73 be used with [s
26990 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 qlite3_bind_valu
269a0 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 e()] and [sqlite
269b0 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 3_result_value()
269c0 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 5b 75 6e ]..** If the [un
269d0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
269e0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 3_value] object
269f0 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 5b returned by.** [
26a00 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 sqlite3_column_v
26a10 61 6c 75 65 28 29 5d 20 69 73 20 75 73 65 64 20 alue()] is used
26a20 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 77 61 79 in any other way
26a30 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c , including call
26a40 73 0a 2a 2a 20 74 6f 20 72 6f 75 74 69 6e 65 73 s.** to routines
26a50 20 6c 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f 76 like [sqlite3_v
26a60 61 6c 75 65 5f 69 6e 74 28 29 5d 2c 20 5b 73 71 alue_int()], [sq
26a70 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
26a80 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 ()],.** or [sqli
26a90 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 te3_value_bytes(
26aa0 29 5d 2c 20 74 68 65 6e 20 74 68 65 20 62 65 68 )], then the beh
26ab0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
26ac0 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 ed..**.** These
26ad0 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 routines attempt
26ae0 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 to convert the
26af0 76 61 6c 75 65 20 77 68 65 72 65 20 61 70 70 72 value where appr
26b00 6f 70 72 69 61 74 65 2e 20 20 46 6f 72 0a 2a 2a opriate. For.**
26b10 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 example, if the
26b20 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 internal repres
26b30 65 6e 74 61 74 69 6f 6e 20 69 73 20 46 4c 4f 41 entation is FLOA
26b40 54 20 61 6e 64 20 61 20 74 65 78 74 20 72 65 73 T and a text res
26b50 75 6c 74 0a 2a 2a 20 69 73 20 72 65 71 75 65 73 ult.** is reques
26b60 74 65 64 2c 20 5b 73 71 6c 69 74 65 33 5f 73 6e ted, [sqlite3_sn
26b70 70 72 69 6e 74 66 28 29 5d 20 69 73 20 75 73 65 printf()] is use
26b80 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20 d internally to
26b90 70 65 72 66 6f 72 6d 20 74 68 65 0a 2a 2a 20 63 perform the.** c
26ba0 6f 6e 76 65 72 73 69 6f 6e 20 61 75 74 6f 6d 61 onversion automa
26bb0 74 69 63 61 6c 6c 79 2e 20 20 54 68 65 20 66 6f tically. The fo
26bc0 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65 llowing table de
26bd0 74 61 69 6c 73 20 74 68 65 20 63 6f 6e 76 65 72 tails the conver
26be0 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 61 72 sions.** that ar
26bf0 65 20 61 70 70 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a e applied:.**.**
26c00 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a <blockquote>.**
26c10 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 22 <table border="
26c20 31 22 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 1">.** <tr><th>
26c30 49 6e 74 65 72 6e 61 6c 3c 62 72 3e 54 79 70 65 Internal<br>Type
26c40 20 3c 74 68 3e 20 52 65 71 75 65 73 74 65 64 3c <th> Requested<
26c50 62 72 3e 54 79 70 65 20 3c 74 68 3e 20 20 43 6f br>Type <th> Co
26c60 6e 76 65 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c nversion.**.** <
26c70 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 tr><td> NULL
26c80 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 <td> INTEGER
26c90 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 30 <td> Result is 0
26ca0 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 .** <tr><td> NU
26cb0 4c 4c 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 LL <td> FLOA
26cc0 54 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 T <td> Result
26cd0 20 69 73 20 30 2e 30 0a 2a 2a 20 3c 74 72 3e 3c is 0.0.** <tr><
26ce0 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 td> NULL <td
26cf0 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e > TEXT <td>
26d00 20 52 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 Result is NULL
26d10 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c pointer.** <tr><
26d20 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 td> NULL <td
26d30 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e > BLOB <td>
26d40 20 52 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 Result is NULL
26d50 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c pointer.** <tr><
26d60 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 td> INTEGER <td
26d70 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e > FLOAT <td>
26d80 20 43 6f 6e 76 65 72 74 20 66 72 6f 6d 20 69 6e Convert from in
26d90 74 65 67 65 72 20 74 6f 20 66 6c 6f 61 74 0a 2a teger to float.*
26da0 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47 * <tr><td> INTEG
26db0 45 52 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 ER <td> TEXT
26dc0 20 20 20 3c 74 64 3e 20 41 53 43 49 49 20 72 65 <td> ASCII re
26dd0 6e 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 69 ndering of the i
26de0 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 nteger.** <tr><t
26df0 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e d> INTEGER <td>
26e00 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 BLOB <td>
26e10 53 61 6d 65 20 61 73 20 49 4e 54 45 47 45 52 2d Same as INTEGER-
26e20 3e 54 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64 >TEXT.** <tr><td
26e30 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 > FLOAT <td>
26e40 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 43 INTEGER <td> C
26e50 6f 6e 76 65 72 74 20 66 72 6f 6d 20 66 6c 6f 61 onvert from floa
26e60 74 20 74 6f 20 69 6e 74 65 67 65 72 0a 2a 2a 20 t to integer.**
26e70 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 <tr><td> FLOAT
26e80 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 <td> TEXT
26e90 20 3c 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64 <td> ASCII rend
26ea0 65 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6c 6f ering of the flo
26eb0 61 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 at.** <tr><td>
26ec0 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20 20 42 FLOAT <td> B
26ed0 4c 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 LOB <td> Same
26ee0 20 61 73 20 46 4c 4f 41 54 2d 3e 54 45 58 54 0a as FLOAT->TEXT.
26ef0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 ** <tr><td> TEX
26f00 54 20 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 T <td> INTEGE
26f10 52 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f R <td> Use ato
26f20 69 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 i().** <tr><td>
26f30 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20 46 TEXT <td> F
26f40 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 55 73 65 LOAT <td> Use
26f50 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c atof().** <tr><
26f60 74 64 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64 td> TEXT <td
26f70 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e > BLOB <td>
26f80 20 4e 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 3c 74 No change.** <t
26f90 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 r><td> BLOB
26fa0 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c <td> INTEGER <
26fb0 74 64 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20 54 td> Convert to T
26fc0 45 58 54 20 74 68 65 6e 20 75 73 65 20 61 74 6f EXT then use ato
26fd0 69 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 i().** <tr><td>
26fe0 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 46 BLOB <td> F
26ff0 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e LOAT <td> Con
27000 76 65 72 74 20 74 6f 20 54 45 58 54 20 74 68 65 vert to TEXT the
27010 6e 20 75 73 65 20 61 74 6f 66 28 29 0a 2a 2a 20 n use atof().**
27020 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 <tr><td> BLOB
27030 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 <td> TEXT
27040 20 3c 74 64 3e 20 41 64 64 20 61 20 7a 65 72 6f <td> Add a zero
27050 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 66 20 6e terminator if n
27060 65 65 64 65 64 0a 2a 2a 20 3c 2f 74 61 62 6c 65 eeded.** </table
27070 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 >.** </blockquot
27080 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 e>.**.** The tab
27090 6c 65 20 61 62 6f 76 65 20 6d 61 6b 65 73 20 72 le above makes r
270a0 65 66 65 72 65 6e 63 65 20 74 6f 20 73 74 61 6e eference to stan
270b0 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 20 66 dard C library f
270c0 75 6e 63 74 69 6f 6e 73 20 61 74 6f 69 28 29 0a unctions atoi().
270d0 2a 2a 20 61 6e 64 20 61 74 6f 66 28 29 2e 20 20 ** and atof().
270e0 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 SQLite does not
270f0 72 65 61 6c 6c 79 20 75 73 65 20 74 68 65 73 65 really use these
27100 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 49 74 20 functions. It
27110 68 61 73 20 69 74 73 0a 2a 2a 20 6f 77 6e 20 65 has its.** own e
27120 71 75 69 76 61 6c 65 6e 74 20 69 6e 74 65 72 6e quivalent intern
27130 61 6c 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 al routines. Th
27140 65 20 61 74 6f 69 28 29 20 61 6e 64 20 61 74 6f e atoi() and ato
27150 66 28 29 20 6e 61 6d 65 73 20 61 72 65 0a 2a 2a f() names are.**
27160 20 75 73 65 64 20 69 6e 20 74 68 65 20 74 61 62 used in the tab
27170 6c 65 20 66 6f 72 20 62 72 65 76 69 74 79 20 61 le for brevity a
27180 6e 64 20 62 65 63 61 75 73 65 20 74 68 65 79 20 nd because they
27190 61 72 65 20 66 61 6d 69 6c 69 61 72 20 74 6f 20 are familiar to
271a0 6d 6f 73 74 0a 2a 2a 20 43 20 70 72 6f 67 72 61 most.** C progra
271b0 6d 6d 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 mmers..**.** Not
271c0 65 20 74 68 61 74 20 77 68 65 6e 20 74 79 70 65 e that when type
271d0 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 6f 63 63 conversions occ
271e0 75 72 2c 20 70 6f 69 6e 74 65 72 73 20 72 65 74 ur, pointers ret
271f0 75 72 6e 65 64 20 62 79 20 70 72 69 6f 72 0a 2a urned by prior.*
27200 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 * calls to sqlit
27210 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 e3_column_blob()
27220 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e , sqlite3_column
27230 5f 74 65 78 74 28 29 2c 20 61 6e 64 2f 6f 72 0a _text(), and/or.
27240 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d ** sqlite3_colum
27250 6e 5f 74 65 78 74 31 36 28 29 20 6d 61 79 20 62 n_text16() may b
27260 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a e invalidated..*
27270 2a 20 54 79 70 65 20 63 6f 6e 76 65 72 73 69 6f * Type conversio
27280 6e 73 20 61 6e 64 20 70 6f 69 6e 74 65 72 20 69 ns and pointer i
27290 6e 76 61 6c 69 64 61 74 69 6f 6e 73 20 6d 69 67 nvalidations mig
272a0 68 74 20 6f 63 63 75 72 0a 2a 2a 20 69 6e 20 74 ht occur.** in t
272b0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 73 he following cas
272c0 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a es:.**.** <ul>.*
272d0 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 * <li> The initi
272e0 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 20 al content is a
272f0 42 4c 4f 42 20 61 6e 64 20 73 71 6c 69 74 65 33 BLOB and sqlite3
27300 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f _column_text() o
27310 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 r.** sqlite
27320 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 3_column_text16(
27330 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 41 20 ) is called. A
27340 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 20 zero-terminator
27350 6d 69 67 68 74 0a 2a 2a 20 20 20 20 20 20 6e 65 might.** ne
27360 65 64 20 74 6f 20 62 65 20 61 64 64 65 64 20 74 ed to be added t
27370 6f 20 74 68 65 20 73 74 72 69 6e 67 2e 3c 2f 6c o the string.</l
27380 69 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 i>.** <li> The i
27390 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 nitial content i
273a0 73 20 55 54 46 2d 38 20 74 65 78 74 20 61 6e 64 s UTF-8 text and
273b0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
273c0 62 79 74 65 73 31 36 28 29 20 6f 72 0a 2a 2a 20 bytes16() or.**
273d0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c sqlite3_col
273e0 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 69 73 20 umn_text16() is
273f0 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f 6e called. The con
27400 74 65 6e 74 20 6d 75 73 74 20 62 65 20 63 6f 6e tent must be con
27410 76 65 72 74 65 64 0a 2a 2a 20 20 20 20 20 20 74 verted.** t
27420 6f 20 55 54 46 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a o UTF-16.</li>.*
27430 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 * <li> The initi
27440 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54 al content is UT
27450 46 2d 31 36 20 74 65 78 74 20 61 6e 64 20 73 71 F-16 text and sq
27460 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
27470 65 73 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 es() or.**
27480 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
27490 65 78 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2e ext() is called.
274a0 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 The content mu
274b0 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a st be converted.
274c0 2a 2a 20 20 20 20 20 20 74 6f 20 55 54 46 2d 38 ** to UTF-8
274d0 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a .</li>.** </ul>.
274e0 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f 6e **.** Conversion
274f0 73 20 62 65 74 77 65 65 6e 20 55 54 46 2d 31 36 s between UTF-16
27500 62 65 20 61 6e 64 20 55 54 46 2d 31 36 6c 65 20 be and UTF-16le
27510 61 72 65 20 61 6c 77 61 79 73 20 64 6f 6e 65 20 are always done
27520 69 6e 20 70 6c 61 63 65 20 61 6e 64 20 64 6f 0a in place and do.
27530 2a 2a 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 ** not invalidat
27540 65 20 61 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 e a prior pointe
27550 72 2c 20 74 68 6f 75 67 68 20 6f 66 20 63 6f 75 r, though of cou
27560 72 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 rse the content
27570 6f 66 20 74 68 65 20 62 75 66 66 65 72 0a 2a 2a of the buffer.**
27580 20 74 68 61 74 20 74 68 65 20 70 72 69 6f 72 20 that the prior
27590 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 74 pointer points t
275a0 6f 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e o will have been
275b0 20 6d 6f 64 69 66 69 65 64 2e 20 20 4f 74 68 65 modified. Othe
275c0 72 20 6b 69 6e 64 73 0a 2a 2a 20 6f 66 20 63 6f r kinds.** of co
275d0 6e 76 65 72 73 69 6f 6e 20 61 72 65 20 64 6f 6e nversion are don
275e0 65 20 69 6e 20 70 6c 61 63 65 20 77 68 65 6e 20 e in place when
275f0 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 2c 20 it is possible,
27600 62 75 74 20 73 6f 6d 65 74 69 6d 65 73 20 74 68 but sometimes th
27610 65 79 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 70 6f ey.** are not po
27620 73 73 69 62 6c 65 20 61 6e 64 20 69 6e 20 74 68 ssible and in th
27630 6f 73 65 20 63 61 73 65 73 20 70 72 69 6f 72 20 ose cases prior
27640 70 6f 69 6e 74 65 72 73 20 61 72 65 20 69 6e 76 pointers are inv
27650 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 alidated..**.**
27660 54 68 65 20 73 61 66 65 73 74 20 61 6e 64 20 65 The safest and e
27670 61 73 69 65 73 74 20 74 6f 20 72 65 6d 65 6d 62 asiest to rememb
27680 65 72 20 70 6f 6c 69 63 79 20 69 73 20 74 6f 20 er policy is to
27690 69 6e 76 6f 6b 65 20 74 68 65 73 65 20 72 6f 75 invoke these rou
276a0 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 6e 65 20 tines.** in one
276b0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
276c0 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c ways:.**.** <ul
276d0 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 >.** <li>sqlite
276e0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 3_column_text()
276f0 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 followed by sqli
27700 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 te3_column_bytes
27710 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e ()</li>.** <li>
27720 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
27730 6c 6f 62 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 lob() followed b
27740 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e y sqlite3_column
27750 5f 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a _bytes()</li>.**
27760 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f <li>sqlite3_co
27770 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 6f lumn_text16() fo
27780 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 llowed by sqlite
27790 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 3_column_bytes16
277a0 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e ()</li>.** </ul>
277b0 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 .**.** In other
277c0 77 6f 72 64 73 2c 20 79 6f 75 20 73 68 6f 75 6c words, you shoul
277d0 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 63 d call sqlite3_c
277e0 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 0a 2a 2a olumn_text(),.**
277f0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
27800 62 6c 6f 62 28 29 2c 20 6f 72 20 73 71 6c 69 74 blob(), or sqlit
27810 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 e3_column_text16
27820 28 29 20 66 69 72 73 74 20 74 6f 20 66 6f 72 63 () first to forc
27830 65 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 e the result.**
27840 69 6e 74 6f 20 74 68 65 20 64 65 73 69 72 65 64 into the desired
27850 20 66 6f 72 6d 61 74 2c 20 74 68 65 6e 20 69 6e format, then in
27860 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c voke sqlite3_col
27870 75 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a 2a umn_bytes() or.*
27880 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e * sqlite3_column
27890 5f 62 79 74 65 73 31 36 28 29 20 74 6f 20 66 69 _bytes16() to fi
278a0 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 nd the size of t
278b0 68 65 20 72 65 73 75 6c 74 2e 20 20 44 6f 20 6e he result. Do n
278c0 6f 74 20 6d 69 78 20 63 61 6c 6c 73 0a 2a 2a 20 ot mix calls.**
278d0 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d to sqlite3_colum
278e0 6e 5f 74 65 78 74 28 29 20 6f 72 20 73 71 6c 69 n_text() or sqli
278f0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 te3_column_blob(
27900 29 20 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 0a ) with calls to.
27910 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d ** sqlite3_colum
27920 6e 5f 62 79 74 65 73 31 36 28 29 2c 20 61 6e 64 n_bytes16(), and
27930 20 64 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c 6c do not mix call
27940 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c s to sqlite3_col
27950 75 6d 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20 umn_text16().**
27960 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 20 73 71 with calls to sq
27970 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
27980 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 es()..**.** The
27990 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 pointers returne
279a0 64 20 61 72 65 20 76 61 6c 69 64 20 75 6e 74 69 d are valid unti
279b0 6c 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 l a type convers
279c0 69 6f 6e 20 6f 63 63 75 72 73 20 61 73 0a 2a 2a ion occurs as.**
279d0 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 described above
279e0 2c 20 6f 72 20 75 6e 74 69 6c 20 5b 73 71 6c 69 , or until [sqli
279f0 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b te3_step()] or [
27a00 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d sqlite3_reset()]
27a10 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f or.** [sqlite3_
27a20 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69 73 20 63 finalize()] is c
27a30 61 6c 6c 65 64 2e 20 20 54 68 65 20 6d 65 6d 6f alled. The memo
27a40 72 79 20 73 70 61 63 65 20 75 73 65 64 20 74 6f ry space used to
27a50 20 68 6f 6c 64 20 73 74 72 69 6e 67 73 0a 2a 2a hold strings.**
27a60 20 61 6e 64 20 42 4c 4f 42 73 20 69 73 20 66 72 and BLOBs is fr
27a70 65 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c eed automaticall
27a80 79 2e 20 20 44 6f 20 3c 62 3e 6e 6f 74 3c 2f 62 y. Do <b>not</b
27a90 3e 20 70 61 73 73 20 74 68 65 20 70 6f 69 6e 74 > pass the point
27aa0 65 72 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 ers returned.**
27ab0 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f [sqlite3_column_
27ac0 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 blob()], [sqlite
27ad0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 3_column_text()]
27ae0 2c 20 65 74 63 2e 20 69 6e 74 6f 0a 2a 2a 20 5b , etc. into.** [
27af0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e sqlite3_free()].
27b00 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f .**.** If a memo
27b10 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 ry allocation er
27b20 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e ror occurs durin
27b30 67 20 74 68 65 20 65 76 61 6c 75 61 74 69 6f 6e g the evaluation
27b40 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 of any.** of th
27b50 65 73 65 20 72 6f 75 74 69 6e 65 73 2c 20 61 20 ese routines, a
27b60 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 default value is
27b70 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20 returned. The
27b80 64 65 66 61 75 6c 74 20 76 61 6c 75 65 0a 2a 2a default value.**
27b90 20 69 73 20 65 69 74 68 65 72 20 74 68 65 20 69 is either the i
27ba0 6e 74 65 67 65 72 20 30 2c 20 74 68 65 20 66 6c nteger 0, the fl
27bb0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d oating point num
27bc0 62 65 72 20 30 2e 30 2c 20 6f 72 20 61 20 4e 55 ber 0.0, or a NU
27bd0 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20 LL.** pointer.
27be0 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 Subsequent calls
27bf0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 to [sqlite3_err
27c00 63 6f 64 65 28 29 5d 20 77 69 6c 6c 20 72 65 74 code()] will ret
27c10 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e urn.** [SQLITE_N
27c20 4f 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 OMEM]..**.** Req
27c30 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
27c40 31 33 38 30 33 5d 20 5b 48 31 33 38 30 36 5d 20 13803] [H13806]
27c50 5b 48 31 33 38 30 39 5d 20 5b 48 31 33 38 31 32 [H13809] [H13812
27c60 5d 20 5b 48 31 33 38 31 35 5d 20 5b 48 31 33 38 ] [H13815] [H138
27c70 31 38 5d 20 5b 48 31 33 38 32 31 5d 20 5b 48 31 18] [H13821] [H1
27c80 33 38 32 34 5d 0a 2a 2a 20 5b 48 31 33 38 32 37 3824].** [H13827
27c90 5d 20 5b 48 31 33 38 33 30 5d 0a 2a 2f 0a 53 51 ] [H13830].*/.SQ
27ca0 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
27cb0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c oid *sqlite3_col
27cc0 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 umn_blob(sqlite3
27cd0 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c _stmt*, int iCol
27ce0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
27cf0 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e t sqlite3_column
27d00 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73 _bytes(sqlite3_s
27d10 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b tmt*, int iCol);
27d20 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
27d30 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
27d40 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 ytes16(sqlite3_s
27d50 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b tmt*, int iCol);
27d60 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 .SQLITE_API doub
27d70 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d le sqlite3_colum
27d80 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 n_double(sqlite3
27d90 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c _stmt*, int iCol
27da0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
27db0 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e t sqlite3_column
27dc0 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d _int(sqlite3_stm
27dd0 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 t*, int iCol);.S
27de0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
27df0 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 3_int64 sqlite3_
27e00 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c column_int64(sql
27e10 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 ite3_stmt*, int
27e20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 iCol);.SQLITE_AP
27e30 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 I const unsigned
27e40 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
27e50 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74 olumn_text(sqlit
27e60 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 e3_stmt*, int iC
27e70 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 ol);.SQLITE_API
27e80 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
27e90 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 te3_column_text1
27ea0 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 6(sqlite3_stmt*,
27eb0 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 int iCol);.SQLI
27ec0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
27ed0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 e3_column_type(s
27ee0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
27ef0 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f t iCol);.SQLITE_
27f00 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 API sqlite3_valu
27f10 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d e *sqlite3_colum
27f20 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f n_value(sqlite3_
27f30 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 stmt*, int iCol)
27f40 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
27f50 46 3a 20 44 65 73 74 72 6f 79 20 41 20 50 72 65 F: Destroy A Pre
27f60 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 pared Statement
27f70 4f 62 6a 65 63 74 20 7b 48 31 33 33 30 30 7d 20 Object {H13300}
27f80 3c 53 37 30 33 30 30 3e 3c 53 33 30 31 30 30 3e <S70300><S30100>
27f90 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
27fa0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 75 e3_finalize() fu
27fb0 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
27fc0 20 74 6f 20 64 65 6c 65 74 65 20 61 20 5b 70 72 to delete a [pr
27fd0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
27fe0 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 61 ]..** If the sta
27ff0 74 65 6d 65 6e 74 20 77 61 73 20 65 78 65 63 75 tement was execu
28000 74 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 ted successfully
28010 20 6f 72 20 6e 6f 74 20 65 78 65 63 75 74 65 64 or not executed
28020 20 61 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a 2a at all, then.**
28030 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
28040 74 75 72 6e 65 64 2e 20 49 66 20 65 78 65 63 75 turned. If execu
28050 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74 tion of the stat
28060 65 6d 65 6e 74 20 66 61 69 6c 65 64 20 74 68 65 ement failed the
28070 6e 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 n an.** [error c
28080 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 ode] or [extende
28090 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 d error code] is
280a0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
280b0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 This routine ca
280c0 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61 n be called at a
280d0 6e 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 20 ny point during
280e0 74 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 the execution of
280f0 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 the.** [prepare
28100 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49 d statement]. I
28110 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 f the virtual ma
28120 63 68 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a 2a chine has not.**
28130 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 75 completed execu
28140 74 69 6f 6e 20 77 68 65 6e 20 74 68 69 73 20 72 tion when this r
28150 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
28160 2c 20 74 68 61 74 20 69 73 20 6c 69 6b 65 0a 2a , that is like.*
28170 2a 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 61 * encountering a
28180 6e 20 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b 73 n error or an [s
28190 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
281a0 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a 2a | interrupt]..*
281b0 2a 20 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70 64 * Incomplete upd
281c0 61 74 65 73 20 6d 61 79 20 62 65 20 72 6f 6c 6c ates may be roll
281d0 65 64 20 62 61 63 6b 20 61 6e 64 20 74 72 61 6e ed back and tran
281e0 73 61 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c 65 sactions cancele
281f0 64 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 d,.** depending
28200 6f 6e 20 74 68 65 20 63 69 72 63 75 6d 73 74 61 on the circumsta
28210 6e 63 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a nces, and the.**
28220 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 72 65 [error code] re
28230 74 75 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 5b turned will be [
28240 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a SQLITE_ABORT]..*
28250 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
28260 73 3a 0a 2a 2a 20 5b 48 31 31 33 30 32 5d 20 5b s:.** [H11302] [
28270 48 31 31 33 30 34 5d 0a 2a 2f 0a 53 51 4c 49 54 H11304].*/.SQLIT
28280 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
28290 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 3_finalize(sqlit
282a0 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b e3_stmt *pStmt);
282b0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
282c0 3a 20 52 65 73 65 74 20 41 20 50 72 65 70 61 72 : Reset A Prepar
282d0 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a ed Statement Obj
282e0 65 63 74 20 7b 48 31 33 33 33 30 7d 20 3c 53 37 ect {H13330} <S7
282f0 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0300>.**.** The
28300 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 sqlite3_reset()
28310 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
28320 65 64 20 74 6f 20 72 65 73 65 74 20 61 20 5b 70 ed to reset a [p
28330 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
28340 74 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 62 61 63 t].** object bac
28350 6b 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c k to its initial
28360 20 73 74 61 74 65 2c 20 72 65 61 64 79 20 74 6f state, ready to
28370 20 62 65 20 72 65 2d 65 78 65 63 75 74 65 64 2e be re-executed.
28380 0a 2a 2a 20 41 6e 79 20 53 51 4c 20 73 74 61 74 .** Any SQL stat
28390 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 73 20 ement variables
283a0 74 68 61 74 20 68 61 64 20 76 61 6c 75 65 73 20 that had values
283b0 62 6f 75 6e 64 20 74 6f 20 74 68 65 6d 20 75 73 bound to them us
283c0 69 6e 67 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 ing.** the [sqli
283d0 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 te3_bind_blob |
283e0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 sqlite3_bind_*()
283f0 20 41 50 49 5d 20 72 65 74 61 69 6e 20 74 68 65 API] retain the
28400 69 72 20 76 61 6c 75 65 73 2e 0a 2a 2a 20 55 73 ir values..** Us
28410 65 20 5b 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 e [sqlite3_clear
28420 5f 62 69 6e 64 69 6e 67 73 28 29 5d 20 74 6f 20 _bindings()] to
28430 72 65 73 65 74 20 74 68 65 20 62 69 6e 64 69 6e reset the bindin
28440 67 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 gs..**.** {H1133
28450 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 2} The [sqlite3_
28460 72 65 73 65 74 28 53 29 5d 20 69 6e 74 65 72 66 reset(S)] interf
28470 61 63 65 20 72 65 73 65 74 73 20 74 68 65 20 5b ace resets the [
28480 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
28490 6e 74 5d 20 53 0a 2a 2a 20 20 20 20 20 20 20 20 nt] S.**
284a0 20 20 62 61 63 6b 20 74 6f 20 74 68 65 20 62 65 back to the be
284b0 67 69 6e 6e 69 6e 67 20 6f 66 20 69 74 73 20 70 ginning of its p
284c0 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 rogram..**.** {H
284d0 31 31 33 33 34 7d 20 49 66 20 74 68 65 20 6d 6f 11334} If the mo
284e0 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 st recent call t
284f0 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 o [sqlite3_step(
28500 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 S)] for the.**
28510 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 [prepare
28520 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 72 d statement] S r
28530 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f eturned [SQLITE_
28540 52 4f 57 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f ROW] or [SQLITE_
28550 44 4f 4e 45 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 DONE],.**
28560 20 20 20 6f 72 20 69 66 20 5b 73 71 6c 69 74 65 or if [sqlite
28570 33 5f 73 74 65 70 28 53 29 5d 20 68 61 73 20 6e 3_step(S)] has n
28580 65 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e ever before been
28590 20 63 61 6c 6c 65 64 20 6f 6e 20 53 2c 0a 2a 2a called on S,.**
285a0 20 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 5b then [
285b0 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 sqlite3_reset(S)
285c0 5d 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 ] returns [SQLIT
285d0 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 E_OK]..**.** {H1
285e0 31 33 33 36 7d 20 49 66 20 74 68 65 20 6d 6f 73 1336} If the mos
285f0 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f t recent call to
28600 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 [sqlite3_step(S
28610 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 )] for the.**
28620 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 [prepared
28630 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 69 6e statement] S in
28640 64 69 63 61 74 65 64 20 61 6e 20 65 72 72 6f 72 dicated an error
28650 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 , then.**
28660 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 [sqlite3_rese
28670 74 28 53 29 5d 20 72 65 74 75 72 6e 73 20 61 6e t(S)] returns an
28680 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b 65 72 appropriate [er
28690 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a ror code]..**.**
286a0 20 7b 48 31 31 33 33 38 7d 20 54 68 65 20 5b 73 {H11338} The [s
286b0 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d qlite3_reset(S)]
286c0 20 69 6e 74 65 72 66 61 63 65 20 64 6f 65 73 20 interface does
286d0 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 not change the v
286e0 61 6c 75 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 alues.**
286f0 20 20 6f 66 20 61 6e 79 20 5b 73 71 6c 69 74 65 of any [sqlite
28700 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 62 69 6e 64 3_bind_blob|bind
28710 69 6e 67 73 5d 20 6f 6e 20 74 68 65 20 5b 70 72 ings] on the [pr
28720 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
28730 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ] S..*/.SQLITE_A
28740 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 PI int sqlite3_r
28750 65 73 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d eset(sqlite3_stm
28760 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a t *pStmt);../*.*
28770 2a 20 43 41 50 49 33 52 45 46 3a 20 43 72 65 61 * CAPI3REF: Crea
28780 74 65 20 4f 72 20 52 65 64 65 66 69 6e 65 20 53 te Or Redefine S
28790 51 4c 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 QL Functions {H1
287a0 36 31 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 6100} <S20200>.*
287b0 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 66 75 6e * KEYWORDS: {fun
287c0 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 ction creation r
287d0 6f 75 74 69 6e 65 73 7d 0a 2a 2a 20 4b 45 59 57 outines}.** KEYW
287e0 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69 ORDS: {applicati
287f0 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 on-defined SQL f
28800 75 6e 63 74 69 6f 6e 7d 0a 2a 2a 20 4b 45 59 57 unction}.** KEYW
28810 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69 ORDS: {applicati
28820 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 on-defined SQL f
28830 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 unctions}.**.**
28840 54 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 These two functi
28850 6f 6e 73 20 28 63 6f 6c 6c 65 63 74 69 76 65 6c ons (collectivel
28860 79 20 6b 6e 6f 77 6e 20 61 73 20 22 66 75 6e 63 y known as "func
28870 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 6f tion creation ro
28880 75 74 69 6e 65 73 22 29 0a 2a 2a 20 61 72 65 20 utines").** are
28890 75 73 65 64 20 74 6f 20 61 64 64 20 53 51 4c 20 used to add SQL
288a0 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 functions or agg
288b0 72 65 67 61 74 65 73 20 6f 72 20 74 6f 20 72 65 regates or to re
288c0 64 65 66 69 6e 65 20 74 68 65 20 62 65 68 61 76 define the behav
288d0 69 6f 72 0a 2a 2a 20 6f 66 20 65 78 69 73 74 69 ior.** of existi
288e0 6e 67 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 ng SQL functions
288f0 20 6f 72 20 61 67 67 72 65 67 61 74 65 73 2e 20 or aggregates.
28900 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 The only differ
28910 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 65 ence between the
28920 0a 2a 2a 20 74 77 6f 20 69 73 20 74 68 61 74 20 .** two is that
28930 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d the second param
28940 65 74 65 72 2c 20 74 68 65 20 6e 61 6d 65 20 6f eter, the name o
28950 66 20 74 68 65 20 28 73 63 61 6c 61 72 29 20 66 f the (scalar) f
28960 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 unction or.** ag
28970 67 72 65 67 61 74 65 2c 20 69 73 20 65 6e 63 6f gregate, is enco
28980 64 65 64 20 69 6e 20 55 54 46 2d 38 20 66 6f 72 ded in UTF-8 for
28990 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
289a0 66 75 6e 63 74 69 6f 6e 28 29 20 61 6e 64 20 55 function() and U
289b0 54 46 2d 31 36 0a 2a 2a 20 66 6f 72 20 73 71 6c TF-16.** for sql
289c0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 ite3_create_func
289d0 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a 2a 20 tion16()..**.**
289e0 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 The first parame
289f0 74 65 72 20 69 73 20 74 68 65 20 5b 64 61 74 61 ter is the [data
28a00 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
28a10 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 53 51 to which the SQ
28a20 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 L.** function is
28a30 20 74 6f 20 62 65 20 61 64 64 65 64 2e 20 20 49 to be added. I
28a40 66 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 67 72 f a single progr
28a50 61 6d 20 75 73 65 73 20 6d 6f 72 65 20 74 68 61 am uses more tha
28a60 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 0a 2a n one database.*
28a70 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 74 * connection int
28a80 65 72 6e 61 6c 6c 79 2c 20 74 68 65 6e 20 53 51 ernally, then SQ
28a90 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6d 75 73 74 L functions must
28aa0 20 62 65 20 61 64 64 65 64 20 69 6e 64 69 76 69 be added indivi
28ab0 64 75 61 6c 6c 79 20 74 6f 0a 2a 2a 20 65 61 63 dually to.** eac
28ac0 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 h database conne
28ad0 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ction..**.** The
28ae0 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
28af0 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 r is the name of
28b00 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f the SQL functio
28b10 6e 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 n to be created
28b20 6f 72 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e or.** redefined.
28b30 20 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 The length of
28b40 74 68 65 20 6e 61 6d 65 20 69 73 20 6c 69 6d 69 the name is limi
28b50 74 65 64 20 74 6f 20 32 35 35 20 62 79 74 65 73 ted to 255 bytes
28b60 2c 20 65 78 63 6c 75 73 69 76 65 20 6f 66 0a 2a , exclusive of.*
28b70 2a 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 * the zero-termi
28b80 6e 61 74 6f 72 2e 20 20 4e 6f 74 65 20 74 68 61 nator. Note tha
28b90 74 20 74 68 65 20 6e 61 6d 65 20 6c 65 6e 67 74 t the name lengt
28ba0 68 20 6c 69 6d 69 74 20 69 73 20 69 6e 20 62 79 h limit is in by
28bb0 74 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68 61 72 tes, not.** char
28bc0 61 63 74 65 72 73 2e 20 20 41 6e 79 20 61 74 74 acters. Any att
28bd0 65 6d 70 74 20 74 6f 20 63 72 65 61 74 65 20 61 empt to create a
28be0 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 function with a
28bf0 20 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a 2a 20 longer name.**
28c00 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 5b will result in [
28c10 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 62 65 SQLITE_ERROR] be
28c20 69 6e 67 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a ing returned..**
28c30 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 70 61 .** The third pa
28c40 72 61 6d 65 74 65 72 20 28 6e 41 72 67 29 0a 2a rameter (nArg).*
28c50 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 * is the number
28c60 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 of arguments tha
28c70 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 t the SQL functi
28c80 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 on or.** aggrega
28c90 74 65 20 74 61 6b 65 73 2e 20 49 66 20 74 68 69 te takes. If thi
28ca0 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 2d s parameter is -
28cb0 31 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c 20 1, then the SQL
28cc0 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 function or.** a
28cd0 67 67 72 65 67 61 74 65 20 6d 61 79 20 74 61 6b ggregate may tak
28ce0 65 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 e any number of
28cf0 61 72 67 75 6d 65 6e 74 73 20 62 65 74 77 65 65 arguments betwee
28d00 6e 20 30 20 61 6e 64 20 74 68 65 20 6c 69 6d 69 n 0 and the limi
28d10 74 0a 2a 2a 20 73 65 74 20 62 79 20 5b 73 71 6c t.** set by [sql
28d20 69 74 65 33 5f 6c 69 6d 69 74 5d 28 5b 53 51 4c ite3_limit]([SQL
28d30 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49 ITE_LIMIT_FUNCTI
28d40 4f 4e 5f 41 52 47 5d 29 2e 20 20 49 66 20 74 68 ON_ARG]). If th
28d50 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 6d e third.** param
28d60 65 74 65 72 20 69 73 20 6c 65 73 73 20 74 68 61 eter is less tha
28d70 6e 20 2d 31 20 6f 72 20 67 72 65 61 74 65 72 20 n -1 or greater
28d80 74 68 61 6e 20 31 32 37 20 74 68 65 6e 20 74 68 than 127 then th
28d90 65 20 62 65 68 61 76 69 6f 72 20 69 73 0a 2a 2a e behavior is.**
28da0 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a undefined..**.*
28db0 2a 20 54 68 65 20 66 6f 75 72 74 68 20 70 61 72 * The fourth par
28dc0 61 6d 65 74 65 72 2c 20 65 54 65 78 74 52 65 70 ameter, eTextRep
28dd0 2c 20 73 70 65 63 69 66 69 65 73 20 77 68 61 74 , specifies what
28de0 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46 38 .** [SQLITE_UTF8
28df0 20 7c 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 | text encoding
28e00 5d 20 74 68 69 73 20 53 51 4c 20 66 75 6e 63 74 ] this SQL funct
28e10 69 6f 6e 20 70 72 65 66 65 72 73 20 66 6f 72 0a ion prefers for.
28e20 2a 2a 20 69 74 73 20 70 61 72 61 6d 65 74 65 72 ** its parameter
28e30 73 2e 20 20 41 6e 79 20 53 51 4c 20 66 75 6e 63 s. Any SQL func
28e40 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 tion implementat
28e50 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 61 62 ion should be ab
28e60 6c 65 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 77 6f le to work.** wo
28e70 72 6b 20 77 69 74 68 20 55 54 46 2d 38 2c 20 55 rk with UTF-8, U
28e80 54 46 2d 31 36 6c 65 2c 20 6f 72 20 55 54 46 2d TF-16le, or UTF-
28e90 31 36 62 65 2e 20 20 42 75 74 20 73 6f 6d 65 20 16be. But some
28ea0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
28eb0 6d 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 65 may be.** more e
28ec0 66 66 69 63 69 65 6e 74 20 77 69 74 68 20 6f 6e fficient with on
28ed0 65 20 65 6e 63 6f 64 69 6e 67 20 74 68 61 6e 20 e encoding than
28ee0 61 6e 6f 74 68 65 72 2e 20 20 41 6e 20 61 70 70 another. An app
28ef0 6c 69 63 61 74 69 6f 6e 20 6d 61 79 0a 2a 2a 20 lication may.**
28f00 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63 invoke sqlite3_c
28f10 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 reate_function()
28f20 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 or sqlite3_crea
28f30 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 20 te_function16()
28f40 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 74 69 6d 65 multiple.** time
28f50 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 s with the same
28f60 66 75 6e 63 74 69 6f 6e 20 62 75 74 20 77 69 74 function but wit
28f70 68 20 64 69 66 66 65 72 65 6e 74 20 76 61 6c 75 h different valu
28f80 65 73 20 6f 66 20 65 54 65 78 74 52 65 70 2e 0a es of eTextRep..
28f90 2a 2a 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65 ** When multiple
28fa0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
28fb0 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e of the same fun
28fc0 63 74 69 6f 6e 20 61 72 65 20 61 76 61 69 6c 61 ction are availa
28fd0 62 6c 65 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 77 ble, SQLite.** w
28fe0 69 6c 6c 20 70 69 63 6b 20 74 68 65 20 6f 6e 65 ill pick the one
28ff0 20 74 68 61 74 20 69 6e 76 6f 6c 76 65 73 20 74 that involves t
29000 68 65 20 6c 65 61 73 74 20 61 6d 6f 75 6e 74 20 he least amount
29010 6f 66 20 64 61 74 61 20 63 6f 6e 76 65 72 73 69 of data conversi
29020 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 on..** If there
29030 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 is only a single
29040 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
29050 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 63 which does not c
29060 61 72 65 20 77 68 61 74 20 74 65 78 74 0a 2a 2a are what text.**
29070 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 75 73 65 encoding is use
29080 64 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 75 72 d, then the four
29090 74 68 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 th argument shou
290a0 6c 64 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 4e ld be [SQLITE_AN
290b0 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 Y]..**.** The fi
290c0 66 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 fth parameter is
290d0 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 70 6f an arbitrary po
290e0 69 6e 74 65 72 2e 20 20 54 68 65 20 69 6d 70 6c inter. The impl
290f0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
29100 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 63 61 e.** function ca
29110 6e 20 67 61 69 6e 20 61 63 63 65 73 73 20 74 6f n gain access to
29120 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 75 73 this pointer us
29130 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 75 73 65 ing [sqlite3_use
29140 72 5f 64 61 74 61 28 29 5d 2e 0a 2a 2a 0a 2a 2a r_data()]..**.**
29150 20 54 68 65 20 73 65 76 65 6e 74 68 2c 20 65 69 The seventh, ei
29160 67 68 74 68 20 61 6e 64 20 6e 69 6e 74 68 20 70 ghth and ninth p
29170 61 72 61 6d 65 74 65 72 73 2c 20 78 46 75 6e 63 arameters, xFunc
29180 2c 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e , xStep and xFin
29190 61 6c 2c 20 61 72 65 0a 2a 2a 20 70 6f 69 6e 74 al, are.** point
291a0 65 72 73 20 74 6f 20 43 2d 6c 61 6e 67 75 61 67 ers to C-languag
291b0 65 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 e functions that
291c0 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 implement the S
291d0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a QL function or.*
291e0 2a 20 61 67 67 72 65 67 61 74 65 2e 20 41 20 73 * aggregate. A s
291f0 63 61 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69 calar SQL functi
29200 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69 on requires an i
29210 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
29220 20 74 68 65 20 78 46 75 6e 63 0a 2a 2a 20 63 61 the xFunc.** ca
29230 6c 6c 62 61 63 6b 20 6f 6e 6c 79 2c 20 4e 55 4c llback only, NUL
29240 4c 20 70 6f 69 6e 74 65 72 73 20 73 68 6f 75 6c L pointers shoul
29250 64 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 d be passed as t
29260 68 65 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 he xStep and xFi
29270 6e 61 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 nal.** parameter
29280 73 2e 20 41 6e 20 61 67 67 72 65 67 61 74 65 20 s. An aggregate
29290 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 SQL function req
292a0 75 69 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65 uires an impleme
292b0 6e 74 61 74 69 6f 6e 20 6f 66 20 78 53 74 65 70 ntation of xStep
292c0 0a 2a 2a 20 61 6e 64 20 78 46 69 6e 61 6c 20 61 .** and xFinal a
292d0 6e 64 20 4e 55 4c 4c 20 73 68 6f 75 6c 64 20 62 nd NULL should b
292e0 65 20 70 61 73 73 65 64 20 66 6f 72 20 78 46 75 e passed for xFu
292f0 6e 63 2e 20 54 6f 20 64 65 6c 65 74 65 20 61 6e nc. To delete an
29300 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 53 51 4c existing.** SQL
29310 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 function or agg
29320 72 65 67 61 74 65 2c 20 70 61 73 73 20 4e 55 4c regate, pass NUL
29330 4c 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 65 20 L for all three
29340 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 62 61 63 function callbac
29350 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 ks..**.** It is
29360 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 67 permitted to reg
29370 69 73 74 65 72 20 6d 75 6c 74 69 70 6c 65 20 69 ister multiple i
29380 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f mplementations o
29390 66 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 66 75 f the same.** fu
293a0 6e 63 74 69 6f 6e 73 20 77 69 74 68 20 74 68 65 nctions with the
293b0 20 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77 same name but w
293c0 69 74 68 20 65 69 74 68 65 72 20 64 69 66 66 65 ith either diffe
293d0 72 69 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 0a ring numbers of.
293e0 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 ** arguments or
293f0 64 69 66 66 65 72 69 6e 67 20 70 72 65 66 65 72 differing prefer
29400 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e red text encodin
29410 67 73 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c gs. SQLite will
29420 20 75 73 65 0a 2a 2a 20 74 68 65 20 69 6d 70 6c use.** the impl
29430 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 ementation that
29440 6d 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61 74 most closely mat
29450 63 68 65 73 20 74 68 65 20 77 61 79 20 69 6e 20 ches the way in
29460 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51 4c which the.** SQL
29470 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
29480 64 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 69 d. A function i
29490 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 mplementation wi
294a0 74 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 th a non-negativ
294b0 65 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d 65 e.** nArg parame
294c0 74 65 72 20 69 73 20 61 20 62 65 74 74 65 72 20 ter is a better
294d0 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e match than a fun
294e0 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 ction implementa
294f0 74 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20 6e tion with.** a n
29500 65 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20 41 egative nArg. A
29510 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 function where
29520 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74 65 the preferred te
29530 78 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 6d xt encoding.** m
29540 61 74 63 68 65 73 20 74 68 65 20 64 61 74 61 62 atches the datab
29550 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 ase encoding is
29560 61 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74 63 a better.** matc
29570 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 6f h than a functio
29580 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f n where the enco
29590 64 69 6e 67 20 69 73 20 64 69 66 66 65 72 65 6e ding is differen
295a0 74 2e 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74 69 t. .** A functi
295b0 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 on where the enc
295c0 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63 65 oding difference
295d0 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 31 is between UTF1
295e0 36 6c 65 20 61 6e 64 20 55 54 46 31 36 62 65 0a 6le and UTF16be.
295f0 2a 2a 20 69 73 20 61 20 63 6c 6f 73 65 72 20 6d ** is a closer m
29600 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 atch than a func
29610 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 tion where the e
29620 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e ncoding differen
29630 63 65 20 69 73 0a 2a 2a 20 62 65 74 77 65 65 6e ce is.** between
29640 20 55 54 46 38 20 61 6e 64 20 55 54 46 31 36 2e UTF8 and UTF16.
29650 0a 2a 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e 20 .**.** Built-in
29660 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62 65 functions may be
29670 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 6e overloaded by n
29680 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 ew application-d
29690 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 efined functions
296a0 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 ..** The first a
296b0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
296c0 65 64 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 ed function with
296d0 20 61 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f 76 a given name ov
296e0 65 72 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20 62 errides all.** b
296f0 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e uilt-in function
29700 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 5b 64 s in the same [d
29710 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
29720 6f 6e 5d 20 77 69 74 68 20 74 68 65 20 73 61 6d on] with the sam
29730 65 20 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73 65 e name..** Subse
29740 71 75 65 6e 74 20 61 70 70 6c 69 63 61 74 69 6f quent applicatio
29750 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 n-defined functi
29760 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 ons of the same
29770 6e 61 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72 69 name only overri
29780 64 65 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70 70 de .** prior app
29790 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
297a0 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 functions that
297b0 61 72 65 20 61 6e 20 65 78 61 63 74 20 6d 61 74 are an exact mat
297c0 63 68 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e 75 ch for the.** nu
297d0 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65 mber of paramete
297e0 72 73 20 61 6e 64 20 70 72 65 66 65 72 72 65 64 rs and preferred
297f0 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a encoding..**.**
29800 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d An application-
29810 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
29820 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 6f is permitted to
29830 20 63 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 53 call other.** S
29840 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 QLite interfaces
29850 2e 20 20 48 6f 77 65 76 65 72 2c 20 73 75 63 68 . However, such
29860 20 63 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74 0a calls must not.
29870 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 ** close the dat
29880 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
29890 20 6e 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f 72 nor finalize or
298a0 20 72 65 73 65 74 20 74 68 65 20 70 72 65 70 61 reset the prepa
298b0 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 red.** statement
298c0 20 69 6e 20 77 68 69 63 68 20 74 68 65 20 66 75 in which the fu
298d0 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e nction is runnin
298e0 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 g..**.** Require
298f0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 31 30 ments:.** [H1610
29900 33 5d 20 5b 48 31 36 31 30 36 5d 20 5b 48 31 36 3] [H16106] [H16
29910 31 30 39 5d 20 5b 48 31 36 31 31 32 5d 20 5b 48 109] [H16112] [H
29920 31 36 31 31 38 5d 20 5b 48 31 36 31 32 31 5d 20 16118] [H16121]
29930 5b 48 31 36 31 32 37 5d 0a 2a 2a 20 5b 48 31 36 [H16127].** [H16
29940 31 33 30 5d 20 5b 48 31 36 31 33 33 5d 20 5b 48 130] [H16133] [H
29950 31 36 31 33 36 5d 20 5b 48 31 36 31 33 39 5d 20 16136] [H16139]
29960 5b 48 31 36 31 34 32 5d 0a 2a 2f 0a 53 51 4c 49 [H16142].*/.SQLI
29970 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
29980 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
29990 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 on(. sqlite3 *d
299a0 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 b,. const char
299b0 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a *zFunctionName,.
299c0 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e int nArg,. in
299d0 74 20 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f t eTextRep,. vo
299e0 69 64 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 id *pApp,. void
299f0 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 (*xFunc)(sqlite
29a00 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 3_context*,int,s
29a10 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c qlite3_value**),
29a20 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 . void (*xStep)
29a30 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
29a40 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 *,int,sqlite3_va
29a50 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 lue**),. void (
29a60 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 *xFinal)(sqlite3
29a70 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a 53 51 _context*).);.SQ
29a80 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
29a90 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 ite3_create_func
29aa0 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74 65 tion16(. sqlite
29ab0 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 76 3 *db,. const v
29ac0 6f 69 64 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 oid *zFunctionNa
29ad0 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a me,. int nArg,.
29ae0 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 0a int eTextRep,.
29af0 20 20 76 6f 69 64 20 2a 70 41 70 70 2c 0a 20 20 void *pApp,.
29b00 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 void (*xFunc)(sq
29b10 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 lite3_context*,i
29b20 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 nt,sqlite3_value
29b30 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 **),. void (*xS
29b40 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e tep)(sqlite3_con
29b50 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 text*,int,sqlite
29b60 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 3_value**),. vo
29b70 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c id (*xFinal)(sql
29b80 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 ite3_context*).)
29b90 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
29ba0 46 3a 20 54 65 78 74 20 45 6e 63 6f 64 69 6e 67 F: Text Encoding
29bb0 73 20 7b 48 31 30 32 36 37 7d 20 3c 53 35 30 32 s {H10267} <S502
29bc0 30 30 3e 20 3c 48 31 36 31 30 30 3e 0a 2a 2a 0a 00> <H16100>.**.
29bd0 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e ** These constan
29be0 74 20 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72 t define integer
29bf0 20 63 6f 64 65 73 20 74 68 61 74 20 72 65 70 72 codes that repr
29c00 65 73 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75 esent the variou
29c10 73 0a 2a 2a 20 74 65 78 74 20 65 6e 63 6f 64 69 s.** text encodi
29c20 6e 67 73 20 73 75 70 70 6f 72 74 65 64 20 62 79 ngs supported by
29c30 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 23 64 65 66 SQLite..*/.#def
29c40 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 38 20 ine SQLITE_UTF8
29c50 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 1.#def
29c60 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 ine SQLITE_UTF16
29c70 4c 45 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 LE 2.#def
29c80 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 ine SQLITE_UTF16
29c90 42 45 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 BE 3.#def
29ca0 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 ine SQLITE_UTF16
29cb0 20 20 20 20 20 20 20 20 20 20 34 20 20 20 20 2f 4 /
29cc0 2a 20 55 73 65 20 6e 61 74 69 76 65 20 62 79 74 * Use native byt
29cd0 65 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69 e order */.#defi
29ce0 6e 65 20 53 51 4c 49 54 45 5f 41 4e 59 20 20 20 ne SQLITE_ANY
29cf0 20 20 20 20 20 20 20 20 20 35 20 20 20 20 2f 2a 5 /*
29d00 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
29d10 66 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f function only */
29d20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
29d30 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 20 20 38 UTF16_ALIGNED 8
29d40 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63 /* sqlite3_c
29d50 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 20 reate_collation
29d60 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 only */../*.** C
29d70 41 50 49 33 52 45 46 3a 20 44 65 70 72 65 63 61 API3REF: Depreca
29d80 74 65 64 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a ted Functions.**
29d90 20 44 45 50 52 45 43 41 54 45 44 0a 2a 2a 0a 2a DEPRECATED.**.*
29da0 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e * These function
29db0 73 20 61 72 65 20 5b 64 65 70 72 65 63 61 74 65 s are [deprecate
29dc0 64 5d 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 6f d]. In order to
29dd0 20 6d 61 69 6e 74 61 69 6e 0a 2a 2a 20 62 61 63 maintain.** bac
29de0 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 kwards compatibi
29df0 6c 69 74 79 20 77 69 74 68 20 6f 6c 64 65 72 20 lity with older
29e00 63 6f 64 65 2c 20 74 68 65 73 65 20 66 75 6e 63 code, these func
29e10 74 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 0a tions continue .
29e20 2a 2a 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 ** to be support
29e30 65 64 2e 20 20 48 6f 77 65 76 65 72 2c 20 6e 65 ed. However, ne
29e40 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 w applications s
29e50 68 6f 75 6c 64 20 61 76 6f 69 64 0a 2a 2a 20 74 hould avoid.** t
29e60 68 65 20 75 73 65 20 6f 66 20 74 68 65 73 65 20 he use of these
29e70 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 6f 20 68 functions. To h
29e80 65 6c 70 20 65 6e 63 6f 75 72 61 67 65 20 70 65 elp encourage pe
29e90 6f 70 6c 65 20 74 6f 20 61 76 6f 69 64 0a 2a 2a ople to avoid.**
29ea0 20 75 73 69 6e 67 20 74 68 65 73 65 20 66 75 6e using these fun
29eb0 63 74 69 6f 6e 73 2c 20 77 65 20 61 72 65 20 6e ctions, we are n
29ec0 6f 74 20 67 6f 69 6e 67 20 74 6f 20 74 65 6c 6c ot going to tell
29ed0 20 79 6f 75 20 77 68 61 74 20 74 68 65 79 20 64 you what they d
29ee0 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 o..*/.#ifndef SQ
29ef0 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 LITE_OMIT_DEPREC
29f00 41 54 45 44 0a 53 51 4c 49 54 45 5f 41 50 49 20 ATED.SQLITE_API
29f10 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 SQLITE_DEPRECATE
29f20 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67 D int sqlite3_ag
29f30 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71 gregate_count(sq
29f40 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b lite3_context*);
29f50 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
29f60 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e TE_DEPRECATED in
29f70 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 t sqlite3_expire
29f80 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 d(sqlite3_stmt*)
29f90 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c ;.SQLITE_API SQL
29fa0 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 ITE_DEPRECATED i
29fb0 6e 74 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 nt sqlite3_trans
29fc0 66 65 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c fer_bindings(sql
29fd0 69 74 65 33 5f 73 74 6d 74 2a 2c 20 73 71 6c 69 ite3_stmt*, sqli
29fe0 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 te3_stmt*);.SQLI
29ff0 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 TE_API SQLITE_DE
2a000 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c PRECATED int sql
2a010 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f ite3_global_reco
2a020 76 65 72 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 ver(void);.SQLIT
2a030 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 E_API SQLITE_DEP
2a040 52 45 43 41 54 45 44 20 76 6f 69 64 20 73 71 6c RECATED void sql
2a050 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 ite3_thread_clea
2a060 6e 75 70 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 nup(void);.SQLIT
2a070 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 E_API SQLITE_DEP
2a080 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 RECATED int sqli
2a090 74 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d te3_memory_alarm
2a0a0 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73 (void(*)(void*,s
2a0b0 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 qlite3_int64,int
2a0c0 29 2c 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f ),void*,sqlite3_
2a0d0 69 6e 74 36 34 29 3b 0a 23 65 6e 64 69 66 0a 0a int64);.#endif..
2a0e0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
2a0f0 4f 62 74 61 69 6e 69 6e 67 20 53 51 4c 20 46 75 Obtaining SQL Fu
2a100 6e 63 74 69 6f 6e 20 50 61 72 61 6d 65 74 65 72 nction Parameter
2a110 20 56 61 6c 75 65 73 20 7b 48 31 35 31 30 30 7d Values {H15100}
2a120 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S20200>.**.**
2a130 54 68 65 20 43 2d 6c 61 6e 67 75 61 67 65 20 69 The C-language i
2a140 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
2a150 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 SQL functions a
2a160 6e 64 20 61 67 67 72 65 67 61 74 65 73 20 75 73 nd aggregates us
2a170 65 73 0a 2a 2a 20 74 68 69 73 20 73 65 74 20 6f es.** this set o
2a180 66 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 f interface rout
2a190 69 6e 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 ines to access t
2a1a0 68 65 20 70 61 72 61 6d 65 74 65 72 20 76 61 6c he parameter val
2a1b0 75 65 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 66 75 ues on.** the fu
2a1c0 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 nction or aggreg
2a1d0 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 ate..**.** The x
2a1e0 46 75 6e 63 20 28 66 6f 72 20 73 63 61 6c 61 72 Func (for scalar
2a1f0 20 66 75 6e 63 74 69 6f 6e 73 29 20 6f 72 20 78 functions) or x
2a200 53 74 65 70 20 28 66 6f 72 20 61 67 67 72 65 67 Step (for aggreg
2a210 61 74 65 73 29 20 70 61 72 61 6d 65 74 65 72 73 ates) parameters
2a220 0a 2a 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f .** to [sqlite3_
2a230 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 create_function(
2a240 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
2a250 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 create_function1
2a260 36 28 29 5d 0a 2a 2a 20 64 65 66 69 6e 65 20 63 6()].** define c
2a270 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 69 6d allbacks that im
2a280 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 plement the SQL
2a290 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 functions and ag
2a2a0 67 72 65 67 61 74 65 73 2e 0a 2a 2a 20 54 68 65 gregates..** The
2a2b0 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 4th parameter t
2a2c0 6f 20 74 68 65 73 65 20 63 61 6c 6c 62 61 63 6b o these callback
2a2d0 73 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 s is an array of
2a2e0 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 pointers to.**
2a2f0 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 [protected sqlit
2a300 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 e3_value] object
2a310 73 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 s. There is one
2a320 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d [sqlite3_value]
2a330 20 6f 62 6a 65 63 74 20 66 6f 72 0a 2a 2a 20 65 object for.** e
2a340 61 63 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f ach parameter to
2a350 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f the SQL functio
2a360 6e 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e n. These routin
2a370 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a es are used to.*
2a380 2a 20 65 78 74 72 61 63 74 20 76 61 6c 75 65 73 * extract values
2a390 20 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74 from the [sqlit
2a3a0 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 e3_value] object
2a3b0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 s..**.** These r
2a3c0 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6e 6c outines work onl
2a3d0 79 20 77 69 74 68 20 5b 70 72 6f 74 65 63 74 65 y with [protecte
2a3e0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d d sqlite3_value]
2a3f0 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 41 6e 79 objects..** Any
2a400 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 attempt to use
2a410 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f these routines o
2a420 6e 20 61 6e 20 5b 75 6e 70 72 6f 74 65 63 74 65 n an [unprotecte
2a430 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d d sqlite3_value]
2a440 0a 2a 2a 20 6f 62 6a 65 63 74 20 72 65 73 75 6c .** object resul
2a450 74 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 ts in undefined
2a460 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 behavior..**.**
2a470 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77 These routines w
2a480 6f 72 6b 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 ork just like th
2a490 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
2a4a0 5b 63 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 [column access f
2a4b0 75 6e 63 74 69 6f 6e 73 5d 0a 2a 2a 20 65 78 63 unctions].** exc
2a4c0 65 70 74 20 74 68 61 74 20 20 74 68 65 73 65 20 ept that these
2a4d0 72 6f 75 74 69 6e 65 73 20 74 61 6b 65 20 61 20 routines take a
2a4e0 73 69 6e 67 6c 65 20 5b 70 72 6f 74 65 63 74 65 single [protecte
2a4f0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d d sqlite3_value]
2a500 20 6f 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e 74 object.** point
2a510 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 er instead of a
2a520 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 20 [sqlite3_stmt*]
2a530 70 6f 69 6e 74 65 72 20 61 6e 64 20 61 6e 20 69 pointer and an i
2a540 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75 nteger column nu
2a550 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 mber..**.** The
2a560 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
2a570 78 74 31 36 28 29 20 69 6e 74 65 72 66 61 63 65 xt16() interface
2a580 20 65 78 74 72 61 63 74 73 20 61 20 55 54 46 2d extracts a UTF-
2a590 31 36 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 16 string.** in
2a5a0 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 2d the native byte-
2a5b0 6f 72 64 65 72 20 6f 66 20 74 68 65 20 68 6f 73 order of the hos
2a5c0 74 20 6d 61 63 68 69 6e 65 2e 20 20 54 68 65 0a t machine. The.
2a5d0 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ** sqlite3_value
2a5e0 5f 74 65 78 74 31 36 62 65 28 29 20 61 6e 64 20 _text16be() and
2a5f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
2a600 78 74 31 36 6c 65 28 29 20 69 6e 74 65 72 66 61 xt16le() interfa
2a610 63 65 73 0a 2a 2a 20 65 78 74 72 61 63 74 20 55 ces.** extract U
2a620 54 46 2d 31 36 20 73 74 72 69 6e 67 73 20 61 73 TF-16 strings as
2a630 20 62 69 67 2d 65 6e 64 69 61 6e 20 61 6e 64 20 big-endian and
2a640 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 72 65 little-endian re
2a650 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a spectively..**.*
2a660 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 * The sqlite3_va
2a670 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 lue_numeric_type
2a680 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 () interface att
2a690 65 6d 70 74 73 20 74 6f 20 61 70 70 6c 79 0a 2a empts to apply.*
2a6a0 2a 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 * numeric affini
2a6b0 74 79 20 74 6f 20 74 68 65 20 76 61 6c 75 65 2e ty to the value.
2a6c0 20 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 This means tha
2a6d0 74 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 0a t an attempt is.
2a6e0 2a 2a 20 6d 61 64 65 20 74 6f 20 63 6f 6e 76 65 ** made to conve
2a6f0 72 74 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 rt the value to
2a700 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 66 6c an integer or fl
2a710 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 49 oating point. I
2a720 66 0a 2a 2a 20 73 75 63 68 20 61 20 63 6f 6e 76 f.** such a conv
2a730 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 ersion is possib
2a740 6c 65 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 le without loss
2a750 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28 of information (
2a760 69 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 in other.** word
2a770 73 2c 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 s, if the value
2a780 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 is a string that
2a790 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 looks like a nu
2a7a0 6d 62 65 72 29 0a 2a 2a 20 74 68 65 6e 20 74 68 mber).** then th
2a7b0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 e conversion is
2a7c0 70 65 72 66 6f 72 6d 65 64 2e 20 20 4f 74 68 65 performed. Othe
2a7d0 72 77 69 73 65 20 6e 6f 20 63 6f 6e 76 65 72 73 rwise no convers
2a7e0 69 6f 6e 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 54 ion occurs..** T
2a7f0 68 65 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 he [SQLITE_INTEG
2a800 45 52 20 7c 20 64 61 74 61 74 79 70 65 5d 20 61 ER | datatype] a
2a810 66 74 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e 20 fter conversion
2a820 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
2a830 2a 2a 20 50 6c 65 61 73 65 20 70 61 79 20 70 61 ** Please pay pa
2a840 72 74 69 63 75 6c 61 72 20 61 74 74 65 6e 74 69 rticular attenti
2a850 6f 6e 20 74 6f 20 74 68 65 20 66 61 63 74 20 74 on to the fact t
2a860 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 hat the pointer
2a870 72 65 74 75 72 6e 65 64 0a 2a 2a 20 66 72 6f 6d returned.** from
2a880 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f [sqlite3_value_
2a890 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 blob()], [sqlite
2a8a0 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 3_value_text()],
2a8b0 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f or.** [sqlite3_
2a8c0 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 5d 20 value_text16()]
2a8d0 63 61 6e 20 62 65 20 69 6e 76 61 6c 69 64 61 74 can be invalidat
2a8e0 65 64 20 62 79 20 61 20 73 75 62 73 65 71 75 65 ed by a subseque
2a8f0 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 nt call to.** [s
2a900 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 qlite3_value_byt
2a910 65 73 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f es()], [sqlite3_
2a920 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 29 5d value_bytes16()]
2a930 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 , [sqlite3_value
2a940 5f 74 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20 _text()],.** or
2a950 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 [sqlite3_value_t
2a960 65 78 74 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 ext16()]..**.**
2a970 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d These routines m
2a980 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 ust be called fr
2a990 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 om the same thre
2a9a0 61 64 20 61 73 0a 2a 2a 20 74 68 65 20 53 51 4c ad as.** the SQL
2a9b0 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 73 function that s
2a9c0 75 70 70 6c 69 65 64 20 74 68 65 20 5b 73 71 6c upplied the [sql
2a9d0 69 74 65 33 5f 76 61 6c 75 65 2a 5d 20 70 61 72 ite3_value*] par
2a9e0 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 ameters..**.** R
2a9f0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
2aa00 5b 48 31 35 31 30 33 5d 20 5b 48 31 35 31 30 36 [H15103] [H15106
2aa10 5d 20 5b 48 31 35 31 30 39 5d 20 5b 48 31 35 31 ] [H15109] [H151
2aa20 31 32 5d 20 5b 48 31 35 31 31 35 5d 20 5b 48 31 12] [H15115] [H1
2aa30 35 31 31 38 5d 20 5b 48 31 35 31 32 31 5d 20 5b 5118] [H15121] [
2aa40 48 31 35 31 32 34 5d 0a 2a 2a 20 5b 48 31 35 31 H15124].** [H151
2aa50 32 37 5d 20 5b 48 31 35 31 33 30 5d 20 5b 48 31 27] [H15130] [H1
2aa60 35 31 33 33 5d 20 5b 48 31 35 31 33 36 5d 0a 2a 5133] [H15136].*
2aa70 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
2aa80 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
2aa90 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c 69 _value_blob(sqli
2aaa0 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c te3_value*);.SQL
2aab0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
2aac0 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 te3_value_bytes(
2aad0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b sqlite3_value*);
2aae0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
2aaf0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 sqlite3_value_by
2ab00 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 76 61 tes16(sqlite3_va
2ab10 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 lue*);.SQLITE_AP
2ab20 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 I double sqlite3
2ab30 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73 71 _value_double(sq
2ab40 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 lite3_value*);.S
2ab50 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
2ab60 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 lite3_value_int(
2ab70 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b sqlite3_value*);
2ab80 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
2ab90 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 te3_int64 sqlite
2aba0 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 71 3_value_int64(sq
2abb0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 lite3_value*);.S
2abc0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
2abd0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 unsigned char *s
2abe0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
2abf0 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a t(sqlite3_value*
2ac00 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f );.SQLITE_API co
2ac10 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
2ac20 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73 3_value_text16(s
2ac30 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a qlite3_value*);.
2ac40 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
2ac50 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 void *sqlite3_v
2ac60 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 73 71 alue_text16le(sq
2ac70 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 lite3_value*);.S
2ac80 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
2ac90 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 void *sqlite3_va
2aca0 6c 75 65 5f 74 65 78 74 31 36 62 65 28 73 71 6c lue_text16be(sql
2acb0 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 ite3_value*);.SQ
2acc0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
2acd0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
2ace0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b sqlite3_value*);
2acf0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
2ad00 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 sqlite3_value_nu
2ad10 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74 meric_type(sqlit
2ad20 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 0a 2f 2a 0a e3_value*);../*.
2ad30 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62 74 ** CAPI3REF: Obt
2ad40 61 69 6e 20 41 67 67 72 65 67 61 74 65 20 46 75 ain Aggregate Fu
2ad50 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 7b nction Context {
2ad60 48 31 36 32 31 30 7d 20 3c 53 32 30 32 30 30 3e H16210} <S20200>
2ad70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 .**.** The imple
2ad80 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 67 67 mentation of agg
2ad90 72 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 regate SQL funct
2ada0 69 6f 6e 73 20 75 73 65 20 74 68 69 73 20 72 6f ions use this ro
2adb0 75 74 69 6e 65 20 74 6f 20 61 6c 6c 6f 63 61 74 utine to allocat
2adc0 65 0a 2a 2a 20 61 20 73 74 72 75 63 74 75 72 65 e.** a structure
2add0 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 for storing the
2ade0 69 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 ir state..**.**
2adf0 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 The first time t
2ae00 68 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 he sqlite3_aggre
2ae10 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 20 72 gate_context() r
2ae20 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
2ae30 20 66 6f 72 20 61 0a 2a 2a 20 70 61 72 74 69 63 for a.** partic
2ae40 75 6c 61 72 20 61 67 67 72 65 67 61 74 65 2c 20 ular aggregate,
2ae50 53 51 4c 69 74 65 20 61 6c 6c 6f 63 61 74 65 73 SQLite allocates
2ae60 20 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 nBytes of memor
2ae70 79 2c 20 7a 65 72 6f 65 73 20 6f 75 74 20 74 68 y, zeroes out th
2ae80 61 74 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 61 6e at.** memory, an
2ae90 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e d returns a poin
2aea0 74 65 72 20 74 6f 20 69 74 2e 20 4f 6e 20 73 65 ter to it. On se
2aeb0 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 cond and subsequ
2aec0 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 ent calls to.**
2aed0 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 sqlite3_aggregat
2aee0 65 5f 63 6f 6e 74 65 78 74 28 29 20 66 6f 72 20 e_context() for
2aef0 74 68 65 20 73 61 6d 65 20 61 67 67 72 65 67 61 the same aggrega
2af00 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64 65 te function inde
2af10 78 2c 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 62 x,.** the same b
2af20 75 66 66 65 72 20 69 73 20 72 65 74 75 72 6e 65 uffer is returne
2af30 64 2e 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 d. The implement
2af40 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 67 67 ation of the agg
2af50 72 65 67 61 74 65 20 63 61 6e 20 75 73 65 0a 2a regate can use.*
2af60 2a 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 62 * the returned b
2af70 75 66 66 65 72 20 74 6f 20 61 63 63 75 6d 75 6c uffer to accumul
2af80 61 74 65 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 ate data..**.**
2af90 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63 SQLite automatic
2afa0 61 6c 6c 79 20 66 72 65 65 73 20 74 68 65 20 61 ally frees the a
2afb0 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 llocated buffer
2afc0 77 68 65 6e 20 74 68 65 20 61 67 67 72 65 67 61 when the aggrega
2afd0 74 65 0a 2a 2a 20 71 75 65 72 79 20 63 6f 6e 63 te.** query conc
2afe0 6c 75 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ludes..**.** The
2aff0 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 first parameter
2b000 20 73 68 6f 75 6c 64 20 62 65 20 61 20 63 6f 70 should be a cop
2b010 79 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c y of the.** [sql
2b020 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 7c 20 53 ite3_context | S
2b030 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 QL function cont
2b040 65 78 74 5d 20 74 68 61 74 20 69 73 20 74 68 65 ext] that is the
2b050 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 first parameter
2b060 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 .** to the callb
2b070 61 63 6b 20 72 6f 75 74 69 6e 65 20 74 68 61 74 ack routine that
2b080 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 implements the
2b090 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
2b0a0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 on..**.** This r
2b0b0 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 outine must be c
2b0c0 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 alled from the s
2b0d0 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 ame thread in wh
2b0e0 69 63 68 0a 2a 2a 20 74 68 65 20 61 67 67 72 65 ich.** the aggre
2b0f0 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f gate SQL functio
2b100 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a n is running..**
2b110 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
2b120 3a 0a 2a 2a 20 5b 48 31 36 32 31 31 5d 20 5b 48 :.** [H16211] [H
2b130 31 36 32 31 33 5d 20 5b 48 31 36 32 31 35 5d 20 16213] [H16215]
2b140 5b 48 31 36 32 31 37 5d 0a 2a 2f 0a 53 51 4c 49 [H16217].*/.SQLI
2b150 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
2b160 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 ite3_aggregate_c
2b170 6f 6e 74 65 78 74 28 73 71 6c 69 74 65 33 5f 63 ontext(sqlite3_c
2b180 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 6e 42 79 ontext*, int nBy
2b190 74 65 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 tes);../*.** CAP
2b1a0 49 33 52 45 46 3a 20 55 73 65 72 20 44 61 74 61 I3REF: User Data
2b1b0 20 46 6f 72 20 46 75 6e 63 74 69 6f 6e 73 20 7b For Functions {
2b1c0 48 31 36 32 34 30 7d 20 3c 53 32 30 32 30 30 3e H16240} <S20200>
2b1d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
2b1e0 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 69 e3_user_data() i
2b1f0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
2b200 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 a copy of.** th
2b210 65 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 77 e pointer that w
2b220 61 73 20 74 68 65 20 70 55 73 65 72 44 61 74 61 as the pUserData
2b230 20 70 61 72 61 6d 65 74 65 72 20 28 74 68 65 20 parameter (the
2b240 35 74 68 20 70 61 72 61 6d 65 74 65 72 29 0a 2a 5th parameter).*
2b250 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 * of the [sqlite
2b260 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 3_create_functio
2b270 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c n()].** and [sql
2b280 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 ite3_create_func
2b290 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 6e tion16()] routin
2b2a0 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c es that original
2b2b0 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 ly.** registered
2b2c0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
2b2d0 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f defined functio
2b2e0 6e 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 n. {END}.**.** T
2b2f0 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 his routine must
2b300 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 be called from
2b310 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 the same thread
2b320 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 in which.** the
2b330 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
2b340 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 ned function is
2b350 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 running..**.** R
2b360 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
2b370 5b 48 31 36 32 34 33 5d 0a 2a 2f 0a 53 51 4c 49 [H16243].*/.SQLI
2b380 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
2b390 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 ite3_user_data(s
2b3a0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 qlite3_context*)
2b3b0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
2b3c0 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e F: Database Conn
2b3d0 65 63 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74 ection For Funct
2b3e0 69 6f 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c 53 ions {H16250} <S
2b3f0 36 30 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a 2a 60600><S20200>.*
2b400 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
2b410 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 _context_db_hand
2b420 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 le() interface r
2b430 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 eturns a copy of
2b440 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 .** the pointer
2b450 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 to the [database
2b460 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68 connection] (th
2b470 65 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 29 e 1st parameter)
2b480 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 .** of the [sqli
2b490 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 te3_create_funct
2b4a0 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 ion()].** and [s
2b4b0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
2b4c0 6e 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 nction16()] rout
2b4d0 69 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e ines that origin
2b4e0 61 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 ally.** register
2b4f0 65 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 ed the applicati
2b500 6f 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 on defined funct
2b510 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ion..**.** Requi
2b520 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 rements:.** [H16
2b530 32 35 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 253].*/.SQLITE_A
2b540 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 PI sqlite3 *sqli
2b550 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
2b560 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f andle(sqlite3_co
2b570 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 ntext*);../*.**
2b580 43 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69 CAPI3REF: Functi
2b590 6f 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61 74 on Auxiliary Dat
2b5a0 61 20 7b 48 31 36 32 37 30 7d 20 3c 53 32 30 32 a {H16270} <S202
2b5b0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 00>.**.** The fo
2b5c0 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63 llowing two func
2b5d0 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65 tions may be use
2b5e0 64 20 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20 d by scalar SQL
2b5f0 66 75 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 functions to.**
2b600 61 73 73 6f 63 69 61 74 65 20 6d 65 74 61 64 61 associate metada
2b610 74 61 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 ta with argument
2b620 20 76 61 6c 75 65 73 2e 20 49 66 20 74 68 65 20 values. If the
2b630 73 61 6d 65 20 76 61 6c 75 65 20 69 73 20 70 61 same value is pa
2b640 73 73 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 69 ssed to.** multi
2b650 70 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 ple invocations
2b660 6f 66 20 74 68 65 20 73 61 6d 65 20 53 51 4c 20 of the same SQL
2b670 66 75 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 20 function during
2b680 71 75 65 72 79 20 65 78 65 63 75 74 69 6f 6e 2c query execution,
2b690 20 75 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 63 under.** some c
2b6a0 69 72 63 75 6d 73 74 61 6e 63 65 73 20 74 68 65 ircumstances the
2b6b0 20 61 73 73 6f 63 69 61 74 65 64 20 6d 65 74 61 associated meta
2b6c0 64 61 74 61 20 6d 61 79 20 62 65 20 70 72 65 73 data may be pres
2b6d0 65 72 76 65 64 2e 20 54 68 69 73 20 6d 61 79 0a erved. This may.
2b6e0 2a 2a 20 62 65 20 75 73 65 64 2c 20 66 6f 72 20 ** be used, for
2b6f0 65 78 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 20 example, to add
2b700 61 20 72 65 67 75 6c 61 72 2d 65 78 70 72 65 73 a regular-expres
2b710 73 69 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 63 sion matching sc
2b720 61 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e alar.** function
2b730 2e 20 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 76 . The compiled v
2b740 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 65 ersion of the re
2b750 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e gular expression
2b760 20 69 73 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a is stored as.**
2b770 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63 69 metadata associ
2b780 61 74 65 64 20 77 69 74 68 20 74 68 65 20 53 51 ated with the SQ
2b790 4c 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 L value passed a
2b7a0 73 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 s the regular ex
2b7b0 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 74 pression.** patt
2b7c0 65 72 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 6c ern. The compil
2b7d0 65 64 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 ed regular expre
2b7e0 73 73 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 75 ssion can be reu
2b7f0 73 65 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 0a sed on multiple.
2b800 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f ** invocations o
2b810 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 f the same funct
2b820 69 6f 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20 ion so that the
2b830 6f 72 69 67 69 6e 61 6c 20 70 61 74 74 65 72 6e original pattern
2b840 20 73 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 20 string.** does
2b850 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72 not need to be r
2b860 65 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 63 ecompiled on eac
2b870 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a 2a h invocation..**
2b880 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2b890 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e get_auxdata() in
2b8a0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
2b8b0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
2b8c0 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 73 metadata.** ass
2b8d0 6f 63 69 61 74 65 64 20 62 79 20 74 68 65 20 73 ociated by the s
2b8e0 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 qlite3_set_auxda
2b8f0 74 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 69 ta() function wi
2b900 74 68 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d th the Nth argum
2b910 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20 ent.** value to
2b920 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d the application-
2b930 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
2b940 2e 20 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 61 . If no metadata
2b950 20 68 61 73 20 62 65 65 6e 20 65 76 65 72 0a 2a has been ever.*
2b960 2a 20 62 65 65 6e 20 73 65 74 20 66 6f 72 20 74 * been set for t
2b970 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 20 he Nth argument
2b980 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c of the function,
2b990 20 6f 72 20 69 66 20 74 68 65 20 63 6f 72 72 65 or if the corre
2b9a0 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63 sponding.** func
2b9b0 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 68 tion parameter h
2b9c0 61 73 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65 as changed since
2b9d0 20 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77 the meta-data w
2b9e0 61 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 as set,.** then
2b9f0 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 sqlite3_get_auxd
2ba00 61 74 61 28 29 20 72 65 74 75 72 6e 73 20 61 20 ata() returns a
2ba10 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a NULL pointer..**
2ba20 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2ba30 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e set_auxdata() in
2ba40 74 65 72 66 61 63 65 20 73 61 76 65 73 20 74 68 terface saves th
2ba50 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70 6f e metadata.** po
2ba60 69 6e 74 65 64 20 74 6f 20 62 79 20 69 74 73 20 inted to by its
2ba70 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 3rd parameter as
2ba80 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 66 6f the metadata fo
2ba90 72 20 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 72 r the N-th.** ar
2baa0 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 70 gument of the ap
2bab0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
2bac0 64 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 62 d function. Sub
2bad0 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 sequent.** calls
2bae0 20 74 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 5f to sqlite3_get_
2baf0 61 75 78 64 61 74 61 28 29 20 6d 69 67 68 74 20 auxdata() might
2bb00 72 65 74 75 72 6e 20 74 68 69 73 20 64 61 74 61 return this data
2bb10 2c 20 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 6e , if it has.** n
2bb20 6f 74 20 62 65 65 6e 20 64 65 73 74 72 6f 79 65 ot been destroye
2bb30 64 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e d..** If it is n
2bb40 6f 74 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65 20 ot NULL, SQLite
2bb50 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 will invoke the
2bb60 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66 75 destructor.** fu
2bb70 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 62 79 20 nction given by
2bb80 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 the 4th paramete
2bb90 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74 r to sqlite3_set
2bba0 5f 61 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a 2a _auxdata() on.**
2bbb0 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 77 68 the metadata wh
2bbc0 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e en the correspon
2bbd0 64 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 61 ding function pa
2bbe0 72 61 6d 65 74 65 72 20 63 68 61 6e 67 65 73 0a rameter changes.
2bbf0 2a 2a 20 6f 72 20 77 68 65 6e 20 74 68 65 20 53 ** or when the S
2bc00 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d QL statement com
2bc10 70 6c 65 74 65 73 2c 20 77 68 69 63 68 65 76 65 pletes, whicheve
2bc20 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a 2a r comes first..*
2bc30 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73 20 66 *.** SQLite is f
2bc40 72 65 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 ree to call the
2bc50 64 65 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 destructor and d
2bc60 72 6f 70 20 6d 65 74 61 64 61 74 61 20 6f 6e 20 rop metadata on
2bc70 61 6e 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 any.** parameter
2bc80 20 6f 66 20 61 6e 79 20 66 75 6e 63 74 69 6f 6e of any function
2bc90 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 20 20 54 at any time. T
2bca0 68 65 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65 he only guarante
2bcb0 65 20 69 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 e is that.** the
2bcc0 20 64 65 73 74 72 75 63 74 6f 72 20 77 69 6c 6c destructor will
2bcd0 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 be called befor
2bce0 65 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 69 e the metadata i
2bcf0 73 20 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a 2a s dropped..**.**
2bd00 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 65 In practice, me
2bd10 74 61 64 61 74 61 20 69 73 20 70 72 65 73 65 72 tadata is preser
2bd20 76 65 64 20 62 65 74 77 65 65 6e 20 66 75 6e 63 ved between func
2bd30 74 69 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a 2a tion calls for.*
2bd40 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 * expressions th
2bd50 61 74 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 at are constant
2bd60 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e at compile time.
2bd70 20 54 68 69 73 20 69 6e 63 6c 75 64 65 73 20 6c This includes l
2bd80 69 74 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65 73 iteral.** values
2bd90 20 61 6e 64 20 53 51 4c 20 76 61 72 69 61 62 6c and SQL variabl
2bda0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 es..**.** These
2bdb0 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 routines must be
2bdc0 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 called from the
2bdd0 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 same thread in
2bde0 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 53 51 4c which.** the SQL
2bdf0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e function is run
2be00 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 ning..**.** Requ
2be10 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
2be20 36 32 37 32 5d 20 5b 48 31 36 32 37 34 5d 20 5b 6272] [H16274] [
2be30 48 31 36 32 37 36 5d 20 5b 48 31 36 32 37 37 5d H16276] [H16277]
2be40 20 5b 48 31 36 32 37 38 5d 20 5b 48 31 36 32 37 [H16278] [H1627
2be50 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 9].*/.SQLITE_API
2be60 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 67 void *sqlite3_g
2be70 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74 et_auxdata(sqlit
2be80 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 e3_context*, int
2be90 20 4e 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 N);.SQLITE_API
2bea0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 65 74 void sqlite3_set
2beb0 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74 65 33 _auxdata(sqlite3
2bec0 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 4e _context*, int N
2bed0 2c 20 76 6f 69 64 2a 2c 20 76 6f 69 64 20 28 2a , void*, void (*
2bee0 29 28 76 6f 69 64 2a 29 29 3b 0a 0a 0a 2f 2a 0a )(void*));.../*.
2bef0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e ** CAPI3REF: Con
2bf00 73 74 61 6e 74 73 20 44 65 66 69 6e 69 6e 67 20 stants Defining
2bf10 53 70 65 63 69 61 6c 20 44 65 73 74 72 75 63 74 Special Destruct
2bf20 6f 72 20 42 65 68 61 76 69 6f 72 20 7b 48 31 30 or Behavior {H10
2bf30 32 38 30 7d 20 3c 53 33 30 31 30 30 3e 0a 2a 2a 280} <S30100>.**
2bf40 0a 2a 2a 20 54 68 65 73 65 20 61 72 65 20 73 70 .** These are sp
2bf50 65 63 69 61 6c 20 76 61 6c 75 65 73 20 66 6f 72 ecial values for
2bf60 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 the destructor
2bf70 74 68 61 74 20 69 73 20 70 61 73 73 65 64 20 69 that is passed i
2bf80 6e 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 6e 61 n as the.** fina
2bf90 6c 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 72 6f l argument to ro
2bfa0 75 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71 6c utines like [sql
2bfb0 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 ite3_result_blob
2bfc0 28 29 5d 2e 20 20 49 66 20 74 68 65 20 64 65 73 ()]. If the des
2bfd0 74 72 75 63 74 6f 72 0a 2a 2a 20 61 72 67 75 6d tructor.** argum
2bfe0 65 6e 74 20 69 73 20 53 51 4c 49 54 45 5f 53 54 ent is SQLITE_ST
2bff0 41 54 49 43 2c 20 69 74 20 6d 65 61 6e 73 20 74 ATIC, it means t
2c000 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 hat the content
2c010 70 6f 69 6e 74 65 72 20 69 73 20 63 6f 6e 73 74 pointer is const
2c020 61 6e 74 0a 2a 2a 20 61 6e 64 20 77 69 6c 6c 20 ant.** and will
2c030 6e 65 76 65 72 20 63 68 61 6e 67 65 2e 20 20 49 never change. I
2c040 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 t does not need
2c050 74 6f 20 62 65 20 64 65 73 74 72 6f 79 65 64 2e to be destroyed.
2c060 20 20 54 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f The.** SQLITE_
2c070 54 52 41 4e 53 49 45 4e 54 20 76 61 6c 75 65 20 TRANSIENT value
2c080 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 63 means that the c
2c090 6f 6e 74 65 6e 74 20 77 69 6c 6c 20 6c 69 6b 65 ontent will like
2c0a0 6c 79 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 ly change in.**
2c0b0 74 68 65 20 6e 65 61 72 20 66 75 74 75 72 65 20 the near future
2c0c0 61 6e 64 20 74 68 61 74 20 53 51 4c 69 74 65 20 and that SQLite
2c0d0 73 68 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73 20 should make its
2c0e0 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79 own private copy
2c0f0 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 of.** the conte
2c100 6e 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e nt before return
2c110 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 ing..**.** The t
2c120 79 70 65 64 65 66 20 69 73 20 6e 65 63 65 73 73 ypedef is necess
2c130 61 72 79 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 ary to work arou
2c140 6e 64 20 70 72 6f 62 6c 65 6d 73 20 69 6e 20 63 nd problems in c
2c150 65 72 74 61 69 6e 0a 2a 2a 20 43 2b 2b 20 63 6f ertain.** C++ co
2c160 6d 70 69 6c 65 72 73 2e 20 20 53 65 65 20 74 69 mpilers. See ti
2c170 63 6b 65 74 20 23 32 31 39 31 2e 0a 2a 2f 0a 74 cket #2191..*/.t
2c180 79 70 65 64 65 66 20 76 6f 69 64 20 28 2a 73 71 ypedef void (*sq
2c190 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 lite3_destructor
2c1a0 5f 74 79 70 65 29 28 76 6f 69 64 2a 29 3b 0a 23 _type)(void*);.#
2c1b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 define SQLITE_ST
2c1c0 41 54 49 43 20 20 20 20 20 20 28 28 73 71 6c 69 ATIC ((sqli
2c1d0 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 te3_destructor_t
2c1e0 79 70 65 29 30 29 0a 23 64 65 66 69 6e 65 20 53 ype)0).#define S
2c1f0 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 QLITE_TRANSIENT
2c200 20 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 ((sqlite3_dest
2c210 72 75 63 74 6f 72 5f 74 79 70 65 29 2d 31 29 0a ructor_type)-1).
2c220 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
2c230 20 53 65 74 74 69 6e 67 20 54 68 65 20 52 65 73 Setting The Res
2c240 75 6c 74 20 4f 66 20 41 6e 20 53 51 4c 20 46 75 ult Of An SQL Fu
2c250 6e 63 74 69 6f 6e 20 7b 48 31 36 34 30 30 7d 20 nction {H16400}
2c260 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S20200>.**.** T
2c270 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 hese routines ar
2c280 65 20 75 73 65 64 20 62 79 20 74 68 65 20 78 46 e used by the xF
2c290 75 6e 63 20 6f 72 20 78 46 69 6e 61 6c 20 63 61 unc or xFinal ca
2c2a0 6c 6c 62 61 63 6b 73 20 74 68 61 74 0a 2a 2a 20 llbacks that.**
2c2b0 69 6d 70 6c 65 6d 65 6e 74 20 53 51 4c 20 66 75 implement SQL fu
2c2c0 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 nctions and aggr
2c2d0 65 67 61 74 65 73 2e 20 20 53 65 65 0a 2a 2a 20 egates. See.**
2c2e0 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
2c2f0 66 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20 function()] and
2c300 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
2c310 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a function16()].**
2c320 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 for additional
2c330 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a information..**.
2c340 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f ** These functio
2c350 6e 73 20 77 6f 72 6b 20 76 65 72 79 20 6d 75 63 ns work very muc
2c360 68 20 6c 69 6b 65 20 74 68 65 20 5b 70 61 72 61 h like the [para
2c370 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 5d 20 66 meter binding] f
2c380 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20 66 75 6e 63 amily of.** func
2c390 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 62 69 tions used to bi
2c3a0 6e 64 20 76 61 6c 75 65 73 20 74 6f 20 68 6f 73 nd values to hos
2c3b0 74 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 t parameters in
2c3c0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
2c3d0 6e 74 73 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f nts..** Refer to
2c3e0 20 74 68 65 20 5b 53 51 4c 20 70 61 72 61 6d 65 the [SQL parame
2c3f0 74 65 72 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 ter] documentati
2c400 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 on for additiona
2c410 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a l information..*
2c420 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
2c430 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 29 20 69 _result_blob() i
2c440 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 nterface sets th
2c450 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a e result from.**
2c460 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d an application-
2c470 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
2c480 20 74 6f 20 62 65 20 74 68 65 20 42 4c 4f 42 20 to be the BLOB
2c490 77 68 6f 73 65 20 63 6f 6e 74 65 6e 74 20 69 73 whose content is
2c4a0 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 pointed.** to b
2c4b0 79 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 y the second par
2c4c0 61 6d 65 74 65 72 20 61 6e 64 20 77 68 69 63 68 ameter and which
2c4d0 20 69 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67 is N bytes long
2c4e0 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a where N is the.
2c4f0 2a 2a 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 ** third paramet
2c500 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 er..**.** The sq
2c510 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 lite3_result_zer
2c520 6f 62 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 63 oblob() interfac
2c530 65 73 20 73 65 74 20 74 68 65 20 72 65 73 75 6c es set the resul
2c540 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c t of.** the appl
2c550 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
2c560 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 function to be a
2c570 20 42 4c 4f 42 20 63 6f 6e 74 61 69 6e 69 6e 67 BLOB containing
2c580 20 61 6c 6c 20 7a 65 72 6f 0a 2a 2a 20 62 79 74 all zero.** byt
2c590 65 73 20 61 6e 64 20 4e 20 62 79 74 65 73 20 69 es and N bytes i
2c5a0 6e 20 73 69 7a 65 2c 20 77 68 65 72 65 20 4e 20 n size, where N
2c5b0 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 is the value of
2c5c0 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 the 2nd paramete
2c5d0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c r..**.** The sql
2c5e0 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 ite3_result_doub
2c5f0 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 le() interface s
2c600 65 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 66 ets the result f
2c610 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 rom.** an applic
2c620 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 ation-defined fu
2c630 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 66 nction to be a f
2c640 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 loating point va
2c650 6c 75 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a lue specified.**
2c660 20 62 79 20 69 74 73 20 32 6e 64 20 61 72 67 75 by its 2nd argu
2c670 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ment..**.** The
2c680 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
2c690 72 72 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 rror() and sqlit
2c6a0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 e3_result_error1
2c6b0 36 28 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 6() functions.**
2c6c0 20 63 61 75 73 65 20 74 68 65 20 69 6d 70 6c 65 cause the imple
2c6d0 6d 65 6e 74 65 64 20 53 51 4c 20 66 75 6e 63 74 mented SQL funct
2c6e0 69 6f 6e 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 ion to throw an
2c6f0 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 20 53 51 exception..** SQ
2c700 4c 69 74 65 20 75 73 65 73 20 74 68 65 20 73 74 Lite uses the st
2c710 72 69 6e 67 20 70 6f 69 6e 74 65 64 20 74 6f 20 ring pointed to
2c720 62 79 20 74 68 65 0a 2a 2a 20 32 6e 64 20 70 61 by the.** 2nd pa
2c730 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c 69 74 rameter of sqlit
2c740 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
2c750 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 ) or sqlite3_res
2c760 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a ult_error16().**
2c770 20 61 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 as the text of
2c780 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
2c790 2e 20 20 53 51 4c 69 74 65 20 69 6e 74 65 72 70 . SQLite interp
2c7a0 72 65 74 73 20 74 68 65 20 65 72 72 6f 72 0a 2a rets the error.*
2c7b0 2a 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 * message string
2c7c0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65 from sqlite3_re
2c7d0 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 61 73 20 sult_error() as
2c7e0 55 54 46 2d 38 2e 20 53 51 4c 69 74 65 0a 2a 2a UTF-8. SQLite.**
2c7f0 20 69 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 interprets the
2c800 73 74 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 string from sqli
2c810 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
2c820 31 36 28 29 20 61 73 20 55 54 46 2d 31 36 20 69 16() as UTF-16 i
2c830 6e 20 6e 61 74 69 76 65 0a 2a 2a 20 62 79 74 65 n native.** byte
2c840 20 6f 72 64 65 72 2e 20 20 49 66 20 74 68 65 20 order. If the
2c850 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 third parameter
2c860 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c to sqlite3_resul
2c870 74 5f 65 72 72 6f 72 28 29 0a 2a 2a 20 6f 72 20 t_error().** or
2c880 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
2c890 72 72 6f 72 31 36 28 29 20 69 73 20 6e 65 67 61 rror16() is nega
2c8a0 74 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65 tive then SQLite
2c8b0 20 74 61 6b 65 73 20 61 73 20 74 68 65 20 65 72 takes as the er
2c8c0 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 61 ror.** message a
2c8d0 6c 6c 20 74 65 78 74 20 75 70 20 74 68 72 6f 75 ll text up throu
2c8e0 67 68 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 gh the first zer
2c8f0 6f 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 o character..**
2c900 49 66 20 74 68 65 20 74 68 69 72 64 20 70 61 72 If the third par
2c910 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 ameter to sqlite
2c920 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 3_result_error()
2c930 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 or.** sqlite3_r
2c940 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 esult_error16()
2c950 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 is non-negative
2c960 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 then SQLite take
2c970 73 20 74 68 61 74 20 6d 61 6e 79 0a 2a 2a 20 62 s that many.** b
2c980 79 74 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63 ytes (not charac
2c990 74 65 72 73 29 20 66 72 6f 6d 20 74 68 65 20 32 ters) from the 2
2c9a0 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 20 nd parameter as
2c9b0 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 the error messag
2c9c0 65 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 e..** The sqlite
2c9d0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 3_result_error()
2c9e0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73 and sqlite3_res
2c9f0 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a ult_error16().**
2ca00 20 72 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 61 routines make a
2ca10 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 private copy of
2ca20 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
2ca30 67 65 20 74 65 78 74 20 62 65 66 6f 72 65 0a 2a ge text before.*
2ca40 2a 20 74 68 65 79 20 72 65 74 75 72 6e 2e 20 20 * they return.
2ca50 48 65 6e 63 65 2c 20 74 68 65 20 63 61 6c 6c 69 Hence, the calli
2ca60 6e 67 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 ng function can
2ca70 64 65 61 6c 6c 6f 63 61 74 65 20 6f 72 0a 2a 2a deallocate or.**
2ca80 20 6d 6f 64 69 66 79 20 74 68 65 20 74 65 78 74 modify the text
2ca90 20 61 66 74 65 72 20 74 68 65 79 20 72 65 74 75 after they retu
2caa0 72 6e 20 77 69 74 68 6f 75 74 20 68 61 72 6d 2e rn without harm.
2cab0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2cac0 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 result_error_cod
2cad0 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 e() function cha
2cae0 6e 67 65 73 20 74 68 65 20 65 72 72 6f 72 20 63 nges the error c
2caf0 6f 64 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 ode.** returned
2cb00 62 79 20 53 51 4c 69 74 65 20 61 73 20 61 20 72 by SQLite as a r
2cb10 65 73 75 6c 74 20 6f 66 20 61 6e 20 65 72 72 6f esult of an erro
2cb20 72 20 69 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e r in a function.
2cb30 20 20 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a 2a By default,.**
2cb40 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 the error code
2cb50 69 73 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e is SQLITE_ERROR.
2cb60 20 20 41 20 73 75 62 73 65 71 75 65 6e 74 20 63 A subsequent c
2cb70 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 all to sqlite3_r
2cb80 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a 2a esult_error().**
2cb90 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 or sqlite3_resu
2cba0 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 72 65 73 lt_error16() res
2cbb0 65 74 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f ets the error co
2cbc0 64 65 20 74 6f 20 53 51 4c 49 54 45 5f 45 52 52 de to SQLITE_ERR
2cbd0 4f 52 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 OR..**.** The sq
2cbe0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 6f 6f lite3_result_too
2cbf0 62 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 big() interface
2cc00 63 61 75 73 65 73 20 53 51 4c 69 74 65 20 74 6f causes SQLite to
2cc10 20 74 68 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a throw an error.
2cc20 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 ** indicating th
2cc30 61 74 20 61 20 73 74 72 69 6e 67 20 6f 72 20 42 at a string or B
2cc40 4c 4f 42 20 69 73 20 74 6f 20 6c 6f 6e 67 20 74 LOB is to long t
2cc50 6f 20 72 65 70 72 65 73 65 6e 74 2e 0a 2a 2a 0a o represent..**.
2cc60 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 ** The sqlite3_r
2cc70 65 73 75 6c 74 5f 6e 6f 6d 65 6d 28 29 20 69 6e esult_nomem() in
2cc80 74 65 72 66 61 63 65 20 63 61 75 73 65 73 20 53 terface causes S
2cc90 51 4c 69 74 65 20 74 6f 20 74 68 72 6f 77 20 61 QLite to throw a
2cca0 6e 20 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 n error.** indic
2ccb0 61 74 69 6e 67 20 74 68 61 74 20 61 20 6d 65 6d ating that a mem
2ccc0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 ory allocation f
2ccd0 61 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ailed..**.** The
2cce0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2ccf0 69 6e 74 28 29 20 69 6e 74 65 72 66 61 63 65 20 int() interface
2cd00 73 65 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 sets the return
2cd10 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 value.** of the
2cd20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
2cd30 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 ned function to
2cd40 62 65 20 74 68 65 20 33 32 2d 62 69 74 20 73 69 be the 32-bit si
2cd50 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 gned integer.**
2cd60 76 61 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74 value given in t
2cd70 68 65 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e he 2nd argument.
2cd80 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2cd90 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 29 20 69 result_int64() i
2cda0 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 nterface sets th
2cdb0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a e return value.*
2cdc0 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 * of the applica
2cdd0 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e tion-defined fun
2cde0 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 ction to be the
2cdf0 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 64-bit signed in
2ce00 74 65 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20 67 teger.** value g
2ce10 69 76 65 6e 20 69 6e 20 74 68 65 20 32 6e 64 20 iven in the 2nd
2ce20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 argument..**.**
2ce30 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 The sqlite3_resu
2ce40 6c 74 5f 6e 75 6c 6c 28 29 20 69 6e 74 65 72 66 lt_null() interf
2ce50 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 74 ace sets the ret
2ce60 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 urn value.** of
2ce70 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d the application-
2ce80 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
2ce90 20 74 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a to be NULL..**.
2cea0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 ** The sqlite3_r
2ceb0 65 73 75 6c 74 5f 74 65 78 74 28 29 2c 20 73 71 esult_text(), sq
2cec0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
2ced0 74 31 36 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 t16(),.** sqlite
2cee0 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 3_result_text16l
2cef0 65 28 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 e(), and sqlite3
2cf00 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 _result_text16be
2cf10 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a () interfaces.**
2cf20 20 73 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 set the return
2cf30 76 61 6c 75 65 20 6f 66 20 74 68 65 20 61 70 70 value of the app
2cf40 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
2cf50 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 0a function to be.
2cf60 2a 2a 20 61 20 74 65 78 74 20 73 74 72 69 6e 67 ** a text string
2cf70 20 77 68 69 63 68 20 69 73 20 72 65 70 72 65 73 which is repres
2cf80 65 6e 74 65 64 20 61 73 20 55 54 46 2d 38 2c 20 ented as UTF-8,
2cf90 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 UTF-16 native by
2cfa0 74 65 20 6f 72 64 65 72 2c 0a 2a 2a 20 55 54 46 te order,.** UTF
2cfb0 2d 31 36 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 -16 little endia
2cfc0 6e 2c 20 6f 72 20 55 54 46 2d 31 36 20 62 69 67 n, or UTF-16 big
2cfd0 20 65 6e 64 69 61 6e 2c 20 72 65 73 70 65 63 74 endian, respect
2cfe0 69 76 65 6c 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 ively..** SQLite
2cff0 20 74 61 6b 65 73 20 74 68 65 20 74 65 78 74 20 takes the text
2d000 72 65 73 75 6c 74 20 66 72 6f 6d 20 74 68 65 20 result from the
2d010 61 70 70 6c 69 63 61 74 69 6f 6e 20 66 72 6f 6d application from
2d020 0a 2a 2a 20 74 68 65 20 32 6e 64 20 70 61 72 61 .** the 2nd para
2d030 6d 65 74 65 72 20 6f 66 20 74 68 65 20 73 71 6c meter of the sql
2d040 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
2d050 2a 20 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a * interfaces..**
2d060 20 49 66 20 74 68 65 20 33 72 64 20 70 61 72 61 If the 3rd para
2d070 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c meter to the sql
2d080 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
2d090 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 * interfaces.**
2d0a0 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 is negative, the
2d0b0 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20 72 n SQLite takes r
2d0c0 65 73 75 6c 74 20 74 65 78 74 20 66 72 6f 6d 20 esult text from
2d0d0 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 the 2nd paramete
2d0e0 72 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 r.** through the
2d0f0 20 66 69 72 73 74 20 7a 65 72 6f 20 63 68 61 72 first zero char
2d100 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 acter..** If the
2d110 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 3rd parameter t
2d120 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 o the sqlite3_re
2d130 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 sult_text* inter
2d140 66 61 63 65 73 0a 2a 2a 20 69 73 20 6e 6f 6e 2d faces.** is non-
2d150 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 61 negative, then a
2d160 73 20 6d 61 6e 79 20 62 79 74 65 73 20 28 6e 6f s many bytes (no
2d170 74 20 63 68 61 72 61 63 74 65 72 73 29 20 6f 66 t characters) of
2d180 20 74 68 65 20 74 65 78 74 0a 2a 2a 20 70 6f 69 the text.** poi
2d190 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 32 nted to by the 2
2d1a0 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 72 65 nd parameter are
2d1b0 20 74 61 6b 65 6e 20 61 73 20 74 68 65 20 61 70 taken as the ap
2d1c0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
2d1d0 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 d.** function re
2d1e0 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 sult..** If the
2d1f0 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 4th parameter to
2d200 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 the sqlite3_res
2d210 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 ult_text* interf
2d220 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 aces.** or sqlit
2d230 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 e3_result_blob i
2d240 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 s a non-NULL poi
2d250 6e 74 65 72 2c 20 74 68 65 6e 20 53 51 4c 69 74 nter, then SQLit
2d260 65 20 63 61 6c 6c 73 20 74 68 61 74 0a 2a 2a 20 e calls that.**
2d270 66 75 6e 63 74 69 6f 6e 20 61 73 20 74 68 65 20 function as the
2d280 64 65 73 74 72 75 63 74 6f 72 20 6f 6e 20 74 68 destructor on th
2d290 65 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 72 e text or BLOB r
2d2a0 65 73 75 6c 74 20 77 68 65 6e 20 69 74 20 68 61 esult when it ha
2d2b0 73 0a 2a 2a 20 66 69 6e 69 73 68 65 64 20 75 73 s.** finished us
2d2c0 69 6e 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e ing that result.
2d2d0 0a 2a 2a 20 49 66 20 74 68 65 20 34 74 68 20 70 .** If the 4th p
2d2e0 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 arameter to the
2d2f0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
2d300 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 20 ext* interfaces
2d310 6f 72 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 or to.** sqlite3
2d320 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20 _result_blob is
2d330 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f 6e 73 the special cons
2d340 74 61 6e 74 20 53 51 4c 49 54 45 5f 53 54 41 54 tant SQLITE_STAT
2d350 49 43 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 0a IC, then SQLite.
2d360 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 ** assumes that
2d370 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 the text or BLOB
2d380 20 72 65 73 75 6c 74 20 69 73 20 69 6e 20 63 6f result is in co
2d390 6e 73 74 61 6e 74 20 73 70 61 63 65 20 61 6e 64 nstant space and
2d3a0 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 6f 70 does not.** cop
2d3b0 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 y the content of
2d3c0 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 6e the parameter n
2d3d0 6f 72 20 63 61 6c 6c 20 61 20 64 65 73 74 72 75 or call a destru
2d3e0 63 74 6f 72 20 6f 6e 20 74 68 65 20 63 6f 6e 74 ctor on the cont
2d3f0 65 6e 74 0a 2a 2a 20 77 68 65 6e 20 69 74 20 68 ent.** when it h
2d400 61 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e as finished usin
2d410 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e 0a 2a g that result..*
2d420 2a 20 49 66 20 74 68 65 20 34 74 68 20 70 61 72 * If the 4th par
2d430 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 ameter to the sq
2d440 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
2d450 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a t* interfaces.**
2d460 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 or sqlite3_resu
2d470 6c 74 5f 62 6c 6f 62 20 69 73 20 74 68 65 20 73 lt_blob is the s
2d480 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 pecial constant
2d490 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 SQLITE_TRANSIENT
2d4a0 0a 2a 2a 20 74 68 65 6e 20 53 51 4c 69 74 65 20 .** then SQLite
2d4b0 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20 makes a copy of
2d4c0 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 the result into
2d4d0 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66 space obtained f
2d4e0 72 6f 6d 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c rom.** from [sql
2d4f0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62 ite3_malloc()] b
2d500 65 66 6f 72 65 20 69 74 20 72 65 74 75 72 6e 73 efore it returns
2d510 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
2d520 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 te3_result_value
2d530 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 () interface set
2d540 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a s the result of.
2d550 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 ** the applicati
2d560 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 on-defined funct
2d570 69 6f 6e 20 74 6f 20 62 65 20 61 20 63 6f 70 79 ion to be a copy
2d580 20 74 68 65 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 the.** [unprote
2d590 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c cted sqlite3_val
2d5a0 75 65 5d 20 6f 62 6a 65 63 74 20 73 70 65 63 69 ue] object speci
2d5b0 66 69 65 64 20 62 79 20 74 68 65 20 32 6e 64 20 fied by the 2nd
2d5c0 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65 0a parameter. The.
2d5d0 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c ** sqlite3_resul
2d5e0 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72 66 t_value() interf
2d5f0 61 63 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79 ace makes a copy
2d600 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 of the [sqlite3
2d610 5f 76 61 6c 75 65 5d 0a 2a 2a 20 73 6f 20 74 68 _value].** so th
2d620 61 74 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f at the [sqlite3_
2d630 76 61 6c 75 65 5d 20 73 70 65 63 69 66 69 65 64 value] specified
2d640 20 69 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 in the paramete
2d650 72 20 6d 61 79 20 63 68 61 6e 67 65 20 6f 72 0a r may change or.
2d660 2a 2a 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 ** be deallocate
2d670 64 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f d after sqlite3_
2d680 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 20 72 result_value() r
2d690 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74 20 68 eturns without h
2d6a0 61 72 6d 2e 0a 2a 2a 20 41 20 5b 70 72 6f 74 65 arm..** A [prote
2d6b0 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c cted sqlite3_val
2d6c0 75 65 5d 20 6f 62 6a 65 63 74 20 6d 61 79 20 61 ue] object may a
2d6d0 6c 77 61 79 73 20 62 65 20 75 73 65 64 20 77 68 lways be used wh
2d6e0 65 72 65 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f ere an.** [unpro
2d6f0 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
2d700 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 69 73 20 alue] object is
2d710 72 65 71 75 69 72 65 64 2c 20 73 6f 20 65 69 74 required, so eit
2d720 68 65 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 5b her.** kind of [
2d730 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f sqlite3_value] o
2d740 62 6a 65 63 74 20 63 61 6e 20 62 65 20 75 73 65 bject can be use
2d750 64 20 77 69 74 68 20 74 68 69 73 20 69 6e 74 65 d with this inte
2d760 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 rface..**.** If
2d770 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 these routines a
2d780 72 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 re called from w
2d790 69 74 68 69 6e 20 74 68 65 20 64 69 66 66 65 72 ithin the differ
2d7a0 65 6e 74 20 74 68 72 65 61 64 0a 2a 2a 20 74 68 ent thread.** th
2d7b0 61 6e 20 74 68 65 20 6f 6e 65 20 63 6f 6e 74 61 an the one conta
2d7c0 69 6e 69 6e 67 20 74 68 65 20 61 70 70 6c 69 63 ining the applic
2d7d0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 ation-defined fu
2d7e0 6e 63 74 69 6f 6e 20 74 68 61 74 20 72 65 63 65 nction that rece
2d7f0 69 76 65 64 0a 2a 2a 20 74 68 65 20 5b 73 71 6c ived.** the [sql
2d800 69 74 65 33 5f 63 6f 6e 74 65 78 74 5d 20 70 6f ite3_context] po
2d810 69 6e 74 65 72 2c 20 74 68 65 20 72 65 73 75 6c inter, the resul
2d820 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 ts are undefined
2d830 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
2d840 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 34 30 33 ents:.** [H16403
2d850 5d 20 5b 48 31 36 34 30 36 5d 20 5b 48 31 36 34 ] [H16406] [H164
2d860 30 39 5d 20 5b 48 31 36 34 31 32 5d 20 5b 48 31 09] [H16412] [H1
2d870 36 34 31 35 5d 20 5b 48 31 36 34 31 38 5d 20 5b 6415] [H16418] [
2d880 48 31 36 34 32 31 5d 20 5b 48 31 36 34 32 34 5d H16421] [H16424]
2d890 0a 2a 2a 20 5b 48 31 36 34 32 37 5d 20 5b 48 31 .** [H16427] [H1
2d8a0 36 34 33 30 5d 20 5b 48 31 36 34 33 33 5d 20 5b 6430] [H16433] [
2d8b0 48 31 36 34 33 36 5d 20 5b 48 31 36 34 33 39 5d H16436] [H16439]
2d8c0 20 5b 48 31 36 34 34 32 5d 20 5b 48 31 36 34 34 [H16442] [H1644
2d8d0 35 5d 20 5b 48 31 36 34 34 38 5d 0a 2a 2a 20 5b 5] [H16448].** [
2d8e0 48 31 36 34 35 31 5d 20 5b 48 31 36 34 35 34 5d H16451] [H16454]
2d8f0 20 5b 48 31 36 34 35 37 5d 20 5b 48 31 36 34 36 [H16457] [H1646
2d900 30 5d 20 5b 48 31 36 34 36 33 5d 0a 2a 2f 0a 53 0] [H16463].*/.S
2d910 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
2d920 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c qlite3_result_bl
2d930 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ob(sqlite3_conte
2d940 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a xt*, const void*
2d950 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 , int, void(*)(v
2d960 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 oid*));.SQLITE_A
2d970 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
2d980 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 result_double(sq
2d990 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 lite3_context*,
2d9a0 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f double);.SQLITE_
2d9b0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
2d9c0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 _result_error(sq
2d9d0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 lite3_context*,
2d9e0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 const char*, int
2d9f0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f );.SQLITE_API vo
2da00 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
2da10 74 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 t_error16(sqlite
2da20 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 3_context*, cons
2da30 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 t void*, int);.S
2da40 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
2da50 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
2da60 72 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 ror_toobig(sqlit
2da70 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 e3_context*);.SQ
2da80 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
2da90 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
2daa0 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 or_nomem(sqlite3
2dab0 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 _context*);.SQLI
2dac0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
2dad0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
2dae0 5f 63 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f _code(sqlite3_co
2daf0 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 ntext*, int);.SQ
2db00 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
2db10 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 lite3_result_int
2db20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
2db30 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
2db40 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
2db50 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 _result_int64(sq
2db60 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 lite3_context*,
2db70 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a sqlite3_int64);.
2db80 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
2db90 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e sqlite3_result_n
2dba0 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ull(sqlite3_cont
2dbb0 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 ext*);.SQLITE_AP
2dbc0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
2dbd0 65 73 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74 esult_text(sqlit
2dbe0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e e3_context*, con
2dbf0 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76 st char*, int, v
2dc00 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a oid(*)(void*));.
2dc10 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
2dc20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
2dc30 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 63 6f ext16(sqlite3_co
2dc40 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f ntext*, const vo
2dc50 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a id*, int, void(*
2dc60 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 )(void*));.SQLIT
2dc70 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
2dc80 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 e3_result_text16
2dc90 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 le(sqlite3_conte
2dca0 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a xt*, const void*
2dcb0 2c 20 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f , int,void(*)(vo
2dcc0 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 id*));.SQLITE_AP
2dcd0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
2dce0 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73 esult_text16be(s
2dcf0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
2dd00 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e const void*, in
2dd10 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 t,void(*)(void*)
2dd20 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f );.SQLITE_API vo
2dd30 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
2dd40 74 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f t_value(sqlite3_
2dd50 63 6f 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 context*, sqlite
2dd60 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 3_value*);.SQLIT
2dd70 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
2dd80 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c e3_result_zerobl
2dd90 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ob(sqlite3_conte
2dda0 78 74 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a xt*, int n);../*
2ddb0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 .** CAPI3REF: De
2ddc0 66 69 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69 fine New Collati
2ddd0 6e 67 20 53 65 71 75 65 6e 63 65 73 20 7b 48 31 ng Sequences {H1
2dde0 36 36 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a 6600} <S20300>.*
2ddf0 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 *.** These funct
2de00 69 6f 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f ions are used to
2de10 20 61 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 69 add new collati
2de20 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 74 6f 20 on sequences to
2de30 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 the.** [database
2de40 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 connection] spe
2de50 63 69 66 69 65 64 20 61 73 20 74 68 65 20 66 69 cified as the fi
2de60 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a rst argument..**
2de70 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 .** The name of
2de80 74 68 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f the new collatio
2de90 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 73 70 n sequence is sp
2dea0 65 63 69 66 69 65 64 20 61 73 20 61 20 55 54 46 ecified as a UTF
2deb0 2d 38 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 -8 string.** for
2dec0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
2ded0 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20 collation() and
2dee0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 sqlite3_create_c
2def0 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a ollation_v2().**
2df00 20 61 6e 64 20 61 20 55 54 46 2d 31 36 20 73 74 and a UTF-16 st
2df10 72 69 6e 67 20 66 6f 72 20 73 71 6c 69 74 65 33 ring for sqlite3
2df20 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f _create_collatio
2df30 6e 31 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63 61 n16(). In all ca
2df40 73 65 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 ses.** the name
2df50 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 is passed as the
2df60 20 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e second function
2df70 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
2df80 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d The third argum
2df90 65 6e 74 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f ent may be one o
2dfa0 66 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 f the constants
2dfb0 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a [SQLITE_UTF8],.*
2dfc0 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c * [SQLITE_UTF16L
2dfd0 45 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 E], or [SQLITE_U
2dfe0 54 46 31 36 42 45 5d 2c 20 69 6e 64 69 63 61 74 TF16BE], indicat
2dff0 69 6e 67 20 74 68 61 74 20 74 68 65 20 75 73 65 ing that the use
2e000 72 2d 73 75 70 70 6c 69 65 64 0a 2a 2a 20 72 6f r-supplied.** ro
2e010 75 74 69 6e 65 20 65 78 70 65 63 74 73 20 74 6f utine expects to
2e020 20 62 65 20 70 61 73 73 65 64 20 70 6f 69 6e 74 be passed point
2e030 65 72 73 20 74 6f 20 73 74 72 69 6e 67 73 20 65 ers to strings e
2e040 6e 63 6f 64 65 64 20 75 73 69 6e 67 20 55 54 46 ncoded using UTF
2e050 2d 38 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 -8,.** UTF-16 li
2e060 74 74 6c 65 2d 65 6e 64 69 61 6e 2c 20 6f 72 20 ttle-endian, or
2e070 55 54 46 2d 31 36 20 62 69 67 2d 65 6e 64 69 61 UTF-16 big-endia
2e080 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e n, respectively.
2e090 20 54 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 The.** third ar
2e0a0 67 75 6d 65 6e 74 20 6d 69 67 68 74 20 61 6c 73 gument might als
2e0b0 6f 20 62 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 o be [SQLITE_UTF
2e0c0 31 36 5d 20 74 6f 20 69 6e 64 69 63 61 74 65 20 16] to indicate
2e0d0 74 68 61 74 20 74 68 65 20 72 6f 75 74 69 6e 65 that the routine
2e0e0 0a 2a 2a 20 65 78 70 65 63 74 73 20 70 6f 69 6e .** expects poin
2e0f0 74 65 72 73 20 74 6f 20 62 65 20 55 54 46 2d 31 ters to be UTF-1
2e100 36 20 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65 6 strings in the
2e110 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 native byte ord
2e120 65 72 2c 20 6f 72 20 74 68 65 0a 2a 2a 20 61 72 er, or the.** ar
2e130 67 75 6d 65 6e 74 20 63 61 6e 20 62 65 20 5b 53 gument can be [S
2e140 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 QLITE_UTF16_ALIG
2e150 4e 45 44 5d 20 69 66 20 74 68 65 0a 2a 2a 20 74 NED] if the.** t
2e160 68 65 20 72 6f 75 74 69 6e 65 20 65 78 70 65 63 he routine expec
2e170 74 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 31 ts pointers to 1
2e180 36 2d 62 69 74 20 77 6f 72 64 20 61 6c 69 67 6e 6-bit word align
2e190 65 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f 66 ed strings.** of
2e1a0 20 55 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e UTF-16 in the n
2e1b0 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 ative byte order
2e1c0 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 ..**.** A pointe
2e1d0 72 20 74 6f 20 74 68 65 20 75 73 65 72 20 73 75 r to the user su
2e1e0 70 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 6d pplied routine m
2e1f0 75 73 74 20 62 65 20 70 61 73 73 65 64 20 61 73 ust be passed as
2e200 20 74 68 65 20 66 69 66 74 68 0a 2a 2a 20 61 72 the fifth.** ar
2e210 67 75 6d 65 6e 74 2e 20 20 49 66 20 69 74 20 69 gument. If it i
2e220 73 20 4e 55 4c 4c 2c 20 74 68 69 73 20 69 73 20 s NULL, this is
2e230 74 68 65 20 73 61 6d 65 20 61 73 20 64 65 6c 65 the same as dele
2e240 74 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 ting the collati
2e250 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 28 on.** sequence (
2e260 73 6f 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 so that SQLite c
2e270 61 6e 6e 6f 74 20 63 61 6c 6c 20 69 74 20 61 6e annot call it an
2e280 79 6d 6f 72 65 29 2e 0a 2a 2a 20 45 61 63 68 20 ymore)..** Each
2e290 74 69 6d 65 20 74 68 65 20 61 70 70 6c 69 63 61 time the applica
2e2a0 74 69 6f 6e 20 73 75 70 70 6c 69 65 64 20 66 75 tion supplied fu
2e2b0 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 nction is invoke
2e2c0 64 2c 20 69 74 20 69 73 20 70 61 73 73 65 64 0a d, it is passed.
2e2d0 2a 2a 20 61 73 20 69 74 73 20 66 69 72 73 74 20 ** as its first
2e2e0 70 61 72 61 6d 65 74 65 72 20 61 20 63 6f 70 79 parameter a copy
2e2f0 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 70 61 of the void* pa
2e300 73 73 65 64 20 61 73 20 74 68 65 20 66 6f 75 72 ssed as the four
2e310 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 th argument.** t
2e320 6f 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 o sqlite3_create
2e330 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 6f 72 20 _collation() or
2e340 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 sqlite3_create_c
2e350 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a ollation16()..**
2e360 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e .** The remainin
2e370 67 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 g arguments to t
2e380 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 he application-s
2e390 75 70 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 upplied routine
2e3a0 61 72 65 20 74 77 6f 20 73 74 72 69 6e 67 73 2c are two strings,
2e3b0 0a 2a 2a 20 65 61 63 68 20 72 65 70 72 65 73 65 .** each represe
2e3c0 6e 74 65 64 20 62 79 20 61 20 28 6c 65 6e 67 74 nted by a (lengt
2e3d0 68 2c 20 64 61 74 61 29 20 70 61 69 72 20 61 6e h, data) pair an
2e3e0 64 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 d encoded in the
2e3f0 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 68 61 encoding.** tha
2e400 74 20 77 61 73 20 70 61 73 73 65 64 20 61 73 20 t was passed as
2e410 74 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 the third argume
2e420 6e 74 20 77 68 65 6e 20 74 68 65 20 63 6f 6c 6c nt when the coll
2e430 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 ation sequence w
2e440 61 73 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 as.** registered
2e450 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 61 70 70 . {END} The app
2e460 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 lication defined
2e470 20 63 6f 6c 6c 61 74 69 6f 6e 20 72 6f 75 74 69 collation routi
2e480 6e 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 ne should.** ret
2e490 75 72 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 urn negative, ze
2e4a0 72 6f 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 ro or positive i
2e4b0 66 20 74 68 65 20 66 69 72 73 74 20 73 74 72 69 f the first stri
2e4c0 6e 67 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c ng is less than,
2e4d0 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 .** equal to, or
2e4e0 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 greater than th
2e4f0 65 20 73 65 63 6f 6e 64 20 73 74 72 69 6e 67 2e e second string.
2e500 20 69 2e 65 2e 20 28 53 54 52 49 4e 47 31 20 2d i.e. (STRING1 -
2e510 20 53 54 52 49 4e 47 32 29 2e 0a 2a 2a 0a 2a 2a STRING2)..**.**
2e520 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 The sqlite3_cre
2e530 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 ate_collation_v2
2e540 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71 () works like sq
2e550 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c lite3_create_col
2e560 6c 61 74 69 6f 6e 28 29 0a 2a 2a 20 65 78 63 65 lation().** exce
2e570 70 74 20 74 68 61 74 20 69 74 20 74 61 6b 65 73 pt that it takes
2e580 20 61 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65 an extra argume
2e590 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 64 65 nt which is a de
2e5a0 73 74 72 75 63 74 6f 72 20 66 6f 72 0a 2a 2a 20 structor for.**
2e5b0 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 2e 20 20 the collation.
2e5c0 54 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 69 The destructor i
2e5d0 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 s called when th
2e5e0 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 0a 2a e collation is.*
2e5f0 2a 20 64 65 73 74 72 6f 79 65 64 20 61 6e 64 20 * destroyed and
2e600 69 73 20 70 61 73 73 65 64 20 61 20 63 6f 70 79 is passed a copy
2e610 20 6f 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 of the fourth p
2e620 61 72 61 6d 65 74 65 72 20 76 6f 69 64 2a 20 70 arameter void* p
2e630 6f 69 6e 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 ointer.** of the
2e640 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
2e650 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 2e 0a collation_v2()..
2e660 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 73 20 61 72 ** Collations ar
2e670 65 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e e destroyed when
2e680 20 74 68 65 79 20 61 72 65 20 6f 76 65 72 72 69 they are overri
2e690 64 64 65 6e 20 62 79 20 6c 61 74 65 72 20 63 61 dden by later ca
2e6a0 6c 6c 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f lls to the.** co
2e6b0 6c 6c 61 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e llation creation
2e6c0 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 77 68 functions or wh
2e6d0 65 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 en the [database
2e6e0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 connection] is
2e6f0 63 6c 6f 73 65 64 0a 2a 2a 20 75 73 69 6e 67 20 closed.** using
2e700 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 [sqlite3_close()
2e710 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 ]..**.** See als
2e720 6f 3a 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c o: [sqlite3_col
2e730 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 5d lation_needed()]
2e740 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f and [sqlite3_co
2e750 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 llation_needed16
2e760 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ()]..**.** Requi
2e770 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 rements:.** [H16
2e780 36 30 33 5d 20 5b 48 31 36 36 30 34 5d 20 5b 48 603] [H16604] [H
2e790 31 36 36 30 36 5d 20 5b 48 31 36 36 30 39 5d 20 16606] [H16609]
2e7a0 5b 48 31 36 36 31 32 5d 20 5b 48 31 36 36 31 35 [H16612] [H16615
2e7b0 5d 20 5b 48 31 36 36 31 38 5d 20 5b 48 31 36 36 ] [H16618] [H166
2e7c0 32 31 5d 0a 2a 2a 20 5b 48 31 36 36 32 34 5d 20 21].** [H16624]
2e7d0 5b 48 31 36 36 32 37 5d 20 5b 48 31 36 36 33 30 [H16627] [H16630
2e7e0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
2e7f0 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 int sqlite3_crea
2e800 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 te_collation(.
2e810 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e sqlite3*, . con
2e820 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
2e830 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c . int eTextRep,
2e840 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 . void*,. int
2e850 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 (*xCompare)(void
2e860 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 *,int,const void
2e870 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 *,int,const void
2e880 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 *).);.SQLITE_API
2e890 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 int sqlite3_cre
2e8a0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 ate_collation_v2
2e8b0 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 (. sqlite3*, .
2e8c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
2e8d0 6d 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74 me, . int eText
2e8e0 52 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 Rep, . void*,.
2e8f0 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 int(*xCompare)(
2e900 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 void*,int,const
2e910 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 void*,int,const
2e920 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a void*),. void(*
2e930 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 xDestroy)(void*)
2e940 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .);.SQLITE_API i
2e950 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 nt sqlite3_creat
2e960 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 e_collation16(.
2e970 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f sqlite3*, . co
2e980 6e 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c nst void *zName,
2e990 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c . int eTextRep,
2e9a0 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 . void*,. int
2e9b0 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 (*xCompare)(void
2e9c0 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 *,int,const void
2e9d0 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 *,int,const void
2e9e0 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 *).);../*.** CAP
2e9f0 49 33 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e I3REF: Collation
2ea00 20 4e 65 65 64 65 64 20 43 61 6c 6c 62 61 63 6b Needed Callback
2ea10 73 20 7b 48 31 36 37 30 30 7d 20 3c 53 32 30 33 s {H16700} <S203
2ea20 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 00>.**.** To avo
2ea30 69 64 20 68 61 76 69 6e 67 20 74 6f 20 72 65 67 id having to reg
2ea40 69 73 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 ister all collat
2ea50 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 62 65 ion sequences be
2ea60 66 6f 72 65 20 61 20 64 61 74 61 62 61 73 65 0a fore a database.
2ea70 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 ** can be used,
2ea80 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 63 a single callbac
2ea90 6b 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 k function may b
2eaa0 65 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74 e registered wit
2eab0 68 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 h the.** [databa
2eac0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 se connection] t
2ead0 6f 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e o be called when
2eae0 65 76 65 72 20 61 6e 20 75 6e 64 65 66 69 6e 65 ever an undefine
2eaf0 64 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 d collation.** s
2eb00 65 71 75 65 6e 63 65 20 69 73 20 72 65 71 75 69 equence is requi
2eb10 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 red..**.** If th
2eb20 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 e function is re
2eb30 67 69 73 74 65 72 65 64 20 75 73 69 6e 67 20 74 gistered using t
2eb40 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 he sqlite3_colla
2eb50 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41 50 tion_needed() AP
2eb60 49 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 I,.** then it is
2eb70 20 70 61 73 73 65 64 20 74 68 65 20 6e 61 6d 65 passed the name
2eb80 73 20 6f 66 20 75 6e 64 65 66 69 6e 65 64 20 63 s of undefined c
2eb90 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
2eba0 65 73 20 61 73 20 73 74 72 69 6e 67 73 0a 2a 2a es as strings.**
2ebb0 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d encoded in UTF-
2ebc0 38 2e 20 7b 48 31 36 37 30 33 7d 20 49 66 20 73 8. {H16703} If s
2ebd0 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e qlite3_collation
2ebe0 5f 6e 65 65 64 65 64 31 36 28 29 20 69 73 20 75 _needed16() is u
2ebf0 73 65 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 sed,.** the name
2ec00 73 20 61 72 65 20 70 61 73 73 65 64 20 61 73 20 s are passed as
2ec10 55 54 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e UTF-16 in machin
2ec20 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 e native byte or
2ec30 64 65 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 der..** A call t
2ec40 6f 20 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f o either functio
2ec50 6e 20 72 65 70 6c 61 63 65 73 20 61 6e 79 20 65 n replaces any e
2ec60 78 69 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 6b xisting callback
2ec70 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 ..**.** When the
2ec80 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 callback is inv
2ec90 6f 6b 65 64 2c 20 74 68 65 20 66 69 72 73 74 20 oked, the first
2eca0 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 argument passed
2ecb0 69 73 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 is a copy.** of
2ecc0 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
2ecd0 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 ent to sqlite3_c
2ece0 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 ollation_needed(
2ecf0 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f ) or.** sqlite3_
2ed00 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 collation_needed
2ed10 31 36 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 16(). The secon
2ed20 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 d argument is th
2ed30 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f e database.** co
2ed40 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 nnection. The t
2ed50 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 hird argument is
2ed60 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f one of [SQLITE_
2ed70 55 54 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f 55 UTF8], [SQLITE_U
2ed80 54 46 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b TF16BE],.** or [
2ed90 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2c SQLITE_UTF16LE],
2eda0 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 indicating the
2edb0 6d 6f 73 74 20 64 65 73 69 72 61 62 6c 65 20 66 most desirable f
2edc0 6f 72 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 orm of the colla
2edd0 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 tion.** sequence
2ede0 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 function requir
2edf0 65 64 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 ed. The fourth
2ee00 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 parameter is the
2ee10 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 name of the.**
2ee20 72 65 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 69 required collati
2ee30 6f 6e 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a on sequence..**.
2ee40 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 ** The callback
2ee50 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 function should
2ee60 72 65 67 69 73 74 65 72 20 74 68 65 20 64 65 73 register the des
2ee70 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75 ired collation u
2ee80 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 sing.** [sqlite3
2ee90 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f _create_collatio
2eea0 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 n()], [sqlite3_c
2eeb0 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 reate_collation1
2eec0 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 6()], or.** [sql
2eed0 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c ite3_create_coll
2eee0 61 74 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a ation_v2()]..**.
2eef0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
2ef00 0a 2a 2a 20 5b 48 31 36 37 30 32 5d 20 5b 48 31 .** [H16702] [H1
2ef10 36 37 30 34 5d 20 5b 48 31 36 37 30 36 5d 0a 2a 6704] [H16706].*
2ef20 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
2ef30 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 sqlite3_collati
2ef40 6f 6e 5f 6e 65 65 64 65 64 28 0a 20 20 73 71 6c on_needed(. sql
2ef50 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c ite3*, . void*,
2ef60 20 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 . void(*)(void
2ef70 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 *,sqlite3*,int e
2ef80 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 TextRep,const ch
2ef90 61 72 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 ar*).);.SQLITE_A
2efa0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
2efb0 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 ollation_needed1
2efc0 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 6(. sqlite3*, .
2efd0 20 20 76 6f 69 64 2a 2c 0a 20 20 76 6f 69 64 28 void*,. void(
2efe0 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 *)(void*,sqlite3
2eff0 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 *,int eTextRep,c
2f000 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a onst void*).);..
2f010 2f 2a 0a 2a 2a 20 53 70 65 63 69 66 79 20 74 68 /*.** Specify th
2f020 65 20 6b 65 79 20 66 6f 72 20 61 6e 20 65 6e 63 e key for an enc
2f030 72 79 70 74 65 64 20 64 61 74 61 62 61 73 65 2e rypted database.
2f040 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 This routine s
2f050 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 63 61 6c 6c hould be.** call
2f060 65 64 20 72 69 67 68 74 20 61 66 74 65 72 20 73 ed right after s
2f070 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a 2a qlite3_open()..*
2f080 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f *.** The code to
2f090 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 implement this
2f0a0 41 50 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c API is not avail
2f0b0 61 62 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c able in the publ
2f0c0 69 63 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 ic release.** of
2f0d0 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 SQLite..*/.SQLI
2f0e0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
2f0f0 65 33 5f 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 e3_key(. sqlite
2f100 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
2f110 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
2f120 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79 base to be rekey
2f130 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f ed */. const vo
2f140 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b id *pKey, int nK
2f150 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 ey /* The ke
2f160 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 y */.);../*.** C
2f170 68 61 6e 67 65 20 74 68 65 20 6b 65 79 20 6f 6e hange the key on
2f180 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 an open databas
2f190 65 2e 20 20 49 66 20 74 68 65 20 63 75 72 72 65 e. If the curre
2f1a0 6e 74 20 64 61 74 61 62 61 73 65 20 69 73 20 6e nt database is n
2f1b0 6f 74 0a 2a 2a 20 65 6e 63 72 79 70 74 65 64 2c ot.** encrypted,
2f1c0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 this routine wi
2f1d0 6c 6c 20 65 6e 63 72 79 70 74 20 69 74 2e 20 20 ll encrypt it.
2f1e0 49 66 20 70 4e 65 77 3d 3d 30 20 6f 72 20 6e 4e If pNew==0 or nN
2f1f0 65 77 3d 3d 30 2c 20 74 68 65 0a 2a 2a 20 64 61 ew==0, the.** da
2f200 74 61 62 61 73 65 20 69 73 20 64 65 63 72 79 70 tabase is decryp
2f210 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ted..**.** The c
2f220 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 ode to implement
2f230 20 74 68 69 73 20 41 50 49 20 69 73 20 6e 6f 74 this API is not
2f240 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 available in th
2f250 65 20 70 75 62 6c 69 63 20 72 65 6c 65 61 73 65 e public release
2f260 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a .** of SQLite..*
2f270 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
2f280 20 73 71 6c 69 74 65 33 5f 72 65 6b 65 79 28 0a sqlite3_rekey(.
2f290 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
2f2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2f2b0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 /* Database to
2f2c0 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20 be rekeyed */.
2f2d0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 const void *pKey
2f2e0 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f , int nKey /
2f2f0 2a 20 54 68 65 20 6e 65 77 20 6b 65 79 20 2a 2f * The new key */
2f300 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 .);../*.** CAPI3
2f310 52 45 46 3a 20 53 75 73 70 65 6e 64 20 45 78 65 REF: Suspend Exe
2f320 63 75 74 69 6f 6e 20 46 6f 72 20 41 20 53 68 6f cution For A Sho
2f330 72 74 20 54 69 6d 65 20 7b 48 31 30 35 33 30 7d rt Time {H10530}
2f340 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 <S40410>.**.**
2f350 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 The sqlite3_slee
2f360 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 p() function cau
2f370 73 65 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 ses the current
2f380 74 68 72 65 61 64 20 74 6f 20 73 75 73 70 65 6e thread to suspen
2f390 64 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 66 d execution.** f
2f3a0 6f 72 20 61 74 20 6c 65 61 73 74 20 61 20 6e 75 or at least a nu
2f3b0 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 mber of millisec
2f3c0 6f 6e 64 73 20 73 70 65 63 69 66 69 65 64 20 69 onds specified i
2f3d0 6e 20 69 74 73 20 70 61 72 61 6d 65 74 65 72 2e n its parameter.
2f3e0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 .**.** If the op
2f3f0 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 64 erating system d
2f400 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 oes not support
2f410 73 6c 65 65 70 20 72 65 71 75 65 73 74 73 20 77 sleep requests w
2f420 69 74 68 0a 2a 2a 20 6d 69 6c 6c 69 73 65 63 6f ith.** milliseco
2f430 6e 64 20 74 69 6d 65 20 72 65 73 6f 6c 75 74 69 nd time resoluti
2f440 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74 69 6d on, then the tim
2f450 65 20 77 69 6c 6c 20 62 65 20 72 6f 75 6e 64 65 e will be rounde
2f460 64 20 75 70 20 74 6f 0a 2a 2a 20 74 68 65 20 6e d up to.** the n
2f470 65 61 72 65 73 74 20 73 65 63 6f 6e 64 2e 20 54 earest second. T
2f480 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c he number of mil
2f490 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 liseconds of sle
2f4a0 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 ep actually.** r
2f4b0 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 equested from th
2f4c0 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 e operating syst
2f4d0 65 6d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a em is returned..
2f4e0 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6d 70 **.** SQLite imp
2f4f0 6c 65 6d 65 6e 74 73 20 74 68 69 73 20 69 6e 74 lements this int
2f500 65 72 66 61 63 65 20 62 79 20 63 61 6c 6c 69 6e erface by callin
2f510 67 20 74 68 65 20 78 53 6c 65 65 70 28 29 0a 2a g the xSleep().*
2f520 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 * method of the
2f530 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 default [sqlite3
2f540 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a _vfs] object..**
2f550 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
2f560 3a 20 5b 48 31 30 35 33 33 5d 20 5b 48 31 30 35 : [H10533] [H105
2f570 33 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 36].*/.SQLITE_AP
2f580 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c I int sqlite3_sl
2f590 65 65 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a eep(int);../*.**
2f5a0 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 CAPI3REF: Name
2f5b0 4f 66 20 54 68 65 20 46 6f 6c 64 65 72 20 48 6f Of The Folder Ho
2f5c0 6c 64 69 6e 67 20 54 65 6d 70 6f 72 61 72 79 20 lding Temporary
2f5d0 46 69 6c 65 73 20 7b 48 31 30 33 31 30 7d 20 3c Files {H10310} <
2f5e0 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 66 S20000>.**.** If
2f5f0 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 this global var
2f600 69 61 62 6c 65 20 69 73 20 6d 61 64 65 20 74 6f iable is made to
2f610 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 69 point to a stri
2f620 6e 67 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 ng which is.** t
2f630 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f 6c he name of a fol
2f640 64 65 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72 65 der (a.k.a. dire
2f650 63 74 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c 6c ctory), then all
2f660 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 temporary files
2f670 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 53 .** created by S
2f680 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 70 6c QLite will be pl
2f690 61 63 65 64 20 69 6e 20 74 68 61 74 20 64 69 72 aced in that dir
2f6a0 65 63 74 6f 72 79 2e 20 20 49 66 20 74 68 69 73 ectory. If this
2f6b0 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 variable.** is
2f6c0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 a NULL pointer,
2f6d0 74 68 65 6e 20 53 51 4c 69 74 65 20 70 65 72 66 then SQLite perf
2f6e0 6f 72 6d 73 20 61 20 73 65 61 72 63 68 20 66 6f orms a search fo
2f6f0 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 r an appropriate
2f700 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 .** temporary fi
2f710 6c 65 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a le directory..**
2f720 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61 .** It is not sa
2f730 66 65 20 74 6f 20 72 65 61 64 20 6f 72 20 6d 6f fe to read or mo
2f740 64 69 66 79 20 74 68 69 73 20 76 61 72 69 61 62 dify this variab
2f750 6c 65 20 69 6e 20 6d 6f 72 65 20 74 68 61 6e 20 le in more than
2f760 6f 6e 65 0a 2a 2a 20 74 68 72 65 61 64 20 61 74 one.** thread at
2f770 20 61 20 74 69 6d 65 2e 20 20 49 74 20 69 73 20 a time. It is
2f780 6e 6f 74 20 73 61 66 65 20 74 6f 20 72 65 61 64 not safe to read
2f790 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 69 73 20 or modify this
2f7a0 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 66 20 61 variable.** if a
2f7b0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
2f7c0 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20 ction] is being
2f7d0 75 73 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 used at the same
2f7e0 20 74 69 6d 65 20 69 6e 20 61 20 73 65 70 61 72 time in a separ
2f7f0 61 74 65 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a ate.** thread..*
2f800 2a 20 49 74 20 69 73 20 69 6e 74 65 6e 64 65 64 * It is intended
2f810 20 74 68 61 74 20 74 68 69 73 20 76 61 72 69 61 that this varia
2f820 62 6c 65 20 62 65 20 73 65 74 20 6f 6e 63 65 0a ble be set once.
2f830 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 70 72 ** as part of pr
2f840 6f 63 65 73 73 20 69 6e 69 74 69 61 6c 69 7a 61 ocess initializa
2f850 74 69 6f 6e 20 61 6e 64 20 62 65 66 6f 72 65 20 tion and before
2f860 61 6e 79 20 53 51 4c 69 74 65 20 69 6e 74 65 72 any SQLite inter
2f870 66 61 63 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 face.** routines
2f880 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65 have been calle
2f890 64 20 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 d and that this
2f8a0 76 61 72 69 61 62 6c 65 20 72 65 6d 61 69 6e 20 variable remain
2f8b0 75 6e 63 68 61 6e 67 65 64 0a 2a 2a 20 74 68 65 unchanged.** the
2f8c0 72 65 61 66 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 reafter..**.** T
2f8d0 68 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 he [temp_store_d
2f8e0 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d irectory pragma]
2f8f0 20 6d 61 79 20 6d 6f 64 69 66 79 20 74 68 69 73 may modify this
2f900 20 76 61 72 69 61 62 6c 65 20 61 6e 64 20 63 61 variable and ca
2f910 75 73 65 0a 2a 2a 20 69 74 20 74 6f 20 70 6f 69 use.** it to poi
2f920 6e 74 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 nt to memory obt
2f930 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 ained from [sqli
2f940 74 65 33 5f 6d 61 6c 6c 6f 63 5d 2e 20 20 46 75 te3_malloc]. Fu
2f950 72 74 68 65 72 6d 6f 72 65 2c 0a 2a 2a 20 74 68 rthermore,.** th
2f960 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 e [temp_store_di
2f970 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 rectory pragma]
2f980 61 6c 77 61 79 73 20 61 73 73 75 6d 65 73 20 74 always assumes t
2f990 68 61 74 20 61 6e 79 20 73 74 72 69 6e 67 0a 2a hat any string.*
2f9a0 2a 20 74 68 61 74 20 74 68 69 73 20 76 61 72 69 * that this vari
2f9b0 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 able points to i
2f9c0 73 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 s held in memory
2f9d0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 0a obtained from .
2f9e0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c ** [sqlite3_mall
2f9f0 6f 63 5d 20 61 6e 64 20 74 68 65 20 70 72 61 67 oc] and the prag
2fa00 6d 61 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74 ma may attempt t
2fa10 6f 20 66 72 65 65 20 74 68 61 74 20 6d 65 6d 6f o free that memo
2fa20 72 79 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c ry.** using [sql
2fa30 69 74 65 33 5f 66 72 65 65 5d 2e 0a 2a 2a 20 48 ite3_free]..** H
2fa40 65 6e 63 65 2c 20 69 66 20 74 68 69 73 20 76 61 ence, if this va
2fa50 72 69 61 62 6c 65 20 69 73 20 6d 6f 64 69 66 69 riable is modifi
2fa60 65 64 20 64 69 72 65 63 74 6c 79 2c 20 65 69 74 ed directly, eit
2fa70 68 65 72 20 69 74 20 73 68 6f 75 6c 64 20 62 65 her it should be
2fa80 0a 2a 2a 20 6d 61 64 65 20 4e 55 4c 4c 20 6f 72 .** made NULL or
2fa90 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 made to point t
2faa0 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 o memory obtaine
2fab0 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f d from [sqlite3_
2fac0 6d 61 6c 6c 6f 63 5d 0a 2a 2a 20 6f 72 20 65 6c malloc].** or el
2fad0 73 65 20 74 68 65 20 75 73 65 20 6f 66 20 74 68 se the use of th
2fae0 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 e [temp_store_di
2faf0 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 rectory pragma]
2fb00 73 68 6f 75 6c 64 20 62 65 20 61 76 6f 69 64 65 should be avoide
2fb10 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 d..*/.SQLITE_API
2fb20 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 74 char *sqlite3_t
2fb30 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 0a emp_directory;..
2fb40 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
2fb50 54 65 73 74 20 46 6f 72 20 41 75 74 6f 2d 43 6f Test For Auto-Co
2fb60 6d 6d 69 74 20 4d 6f 64 65 20 7b 48 31 32 39 33 mmit Mode {H1293
2fb70 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 4b 0} <S60200>.** K
2fb80 45 59 57 4f 52 44 53 3a 20 7b 61 75 74 6f 63 6f EYWORDS: {autoco
2fb90 6d 6d 69 74 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a 2a mmit mode}.**.**
2fba0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 The sqlite3_get
2fbb0 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 29 20 69 6e _autocommit() in
2fbc0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
2fbd0 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a 20 7a non-zero or.** z
2fbe0 65 72 6f 20 69 66 20 74 68 65 20 67 69 76 65 6e ero if the given
2fbf0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
2fc00 74 69 6f 6e 20 69 73 20 6f 72 20 69 73 20 6e 6f tion is or is no
2fc10 74 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 t in autocommit
2fc20 6d 6f 64 65 2c 0a 2a 2a 20 72 65 73 70 65 63 74 mode,.** respect
2fc30 69 76 65 6c 79 2e 20 20 41 75 74 6f 63 6f 6d 6d ively. Autocomm
2fc40 69 74 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 79 it mode is on by
2fc50 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 20 41 75 74 default..** Aut
2fc60 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 ocommit mode is
2fc70 64 69 73 61 62 6c 65 64 20 62 79 20 61 20 5b 42 disabled by a [B
2fc80 45 47 49 4e 5d 20 73 74 61 74 65 6d 65 6e 74 2e EGIN] statement.
2fc90 0a 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d .** Autocommit m
2fca0 6f 64 65 20 69 73 20 72 65 2d 65 6e 61 62 6c 65 ode is re-enable
2fcb0 64 20 62 79 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 d by a [COMMIT]
2fcc0 6f 72 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a or [ROLLBACK]..*
2fcd0 2a 0a 2a 2a 20 49 66 20 63 65 72 74 61 69 6e 20 *.** If certain
2fce0 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20 kinds of errors
2fcf0 6f 63 63 75 72 20 6f 6e 20 61 20 73 74 61 74 65 occur on a state
2fd00 6d 65 6e 74 20 77 69 74 68 69 6e 20 61 20 6d 75 ment within a mu
2fd10 6c 74 69 2d 73 74 61 74 65 6d 65 6e 74 0a 2a 2a lti-statement.**
2fd20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 65 72 transaction (er
2fd30 72 6f 72 73 20 69 6e 63 6c 75 64 69 6e 67 20 5b rors including [
2fd40 53 51 4c 49 54 45 5f 46 55 4c 4c 5d 2c 20 5b 53 SQLITE_FULL], [S
2fd50 51 4c 49 54 45 5f 49 4f 45 52 52 5d 2c 0a 2a 2a QLITE_IOERR],.**
2fd60 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2c [SQLITE_NOMEM],
2fd70 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c 20 [SQLITE_BUSY],
2fd80 61 6e 64 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 and [SQLITE_INTE
2fd90 52 52 55 50 54 5d 29 20 74 68 65 6e 20 74 68 65 RRUPT]) then the
2fda0 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 .** transaction
2fdb0 6d 69 67 68 74 20 62 65 20 72 6f 6c 6c 65 64 20 might be rolled
2fdc0 62 61 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c back automatical
2fdd0 6c 79 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77 61 ly. The only wa
2fde0 79 20 74 6f 0a 2a 2a 20 66 69 6e 64 20 6f 75 74 y to.** find out
2fdf0 20 77 68 65 74 68 65 72 20 53 51 4c 69 74 65 20 whether SQLite
2fe00 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 6f automatically ro
2fe10 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20 74 72 lled back the tr
2fe20 61 6e 73 61 63 74 69 6f 6e 20 61 66 74 65 72 0a ansaction after.
2fe30 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 74 ** an error is t
2fe40 6f 20 75 73 65 20 74 68 69 73 20 66 75 6e 63 74 o use this funct
2fe50 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ion..**.** If an
2fe60 6f 74 68 65 72 20 74 68 72 65 61 64 20 63 68 61 other thread cha
2fe70 6e 67 65 73 20 74 68 65 20 61 75 74 6f 63 6f 6d nges the autocom
2fe80 6d 69 74 20 73 74 61 74 75 73 20 6f 66 20 74 68 mit status of th
2fe90 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f e database.** co
2fea0 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 nnection while t
2feb0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 his routine is r
2fec0 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 unning, then the
2fed0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a return value.**
2fee0 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a is undefined..*
2fef0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
2ff00 73 3a 20 5b 48 31 32 39 33 31 5d 20 5b 48 31 32 s: [H12931] [H12
2ff10 39 33 32 5d 20 5b 48 31 32 39 33 33 5d 20 5b 48 932] [H12933] [H
2ff20 31 32 39 33 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 12934].*/.SQLITE
2ff30 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
2ff40 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 _get_autocommit(
2ff50 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a sqlite3*);../*.*
2ff60 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 6e 64 * CAPI3REF: Find
2ff70 20 54 68 65 20 44 61 74 61 62 61 73 65 20 48 61 The Database Ha
2ff80 6e 64 6c 65 20 4f 66 20 41 20 50 72 65 70 61 72 ndle Of A Prepar
2ff90 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 ed Statement {H1
2ffa0 33 31 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a 2a 3120} <S60600>.*
2ffb0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
2ffc0 5f 64 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 72 _db_handle inter
2ffd0 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 face returns the
2ffe0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
2fff0 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a ction] handle.**
30000 20 74 6f 20 77 68 69 63 68 20 61 20 5b 70 72 65 to which a [pre
30010 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
30020 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20 5b belongs. The [
30030 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
30040 69 6f 6e 5d 0a 2a 2a 20 72 65 74 75 72 6e 65 64 ion].** returned
30050 20 62 79 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 by sqlite3_db_h
30060 61 6e 64 6c 65 20 69 73 20 74 68 65 20 73 61 6d andle is the sam
30070 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
30080 65 63 74 69 6f 6e 5d 20 74 68 61 74 20 77 61 73 ection] that was
30090 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
300a0 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 73 ent.** to the [s
300b0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
300c0 32 28 29 5d 20 63 61 6c 6c 20 28 6f 72 20 69 74 2()] call (or it
300d0 73 20 76 61 72 69 61 6e 74 73 29 20 74 68 61 74 s variants) that
300e0 20 77 61 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 was used to.**
300f0 63 72 65 61 74 65 20 74 68 65 20 73 74 61 74 65 create the state
30100 6d 65 6e 74 20 69 6e 20 74 68 65 20 66 69 72 73 ment in the firs
30110 74 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 52 t place..**.** R
30120 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 equirements: [H1
30130 33 31 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 3123].*/.SQLITE_
30140 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c API sqlite3 *sql
30150 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 ite3_db_handle(s
30160 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a qlite3_stmt*);..
30170 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
30180 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 72 Find the next pr
30190 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
301a0 20 7b 48 31 33 31 34 30 7d 20 3c 53 36 30 36 30 {H13140} <S6060
301b0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 0>.**.** This in
301c0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
301d0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
301e0 20 6e 65 78 74 20 5b 70 72 65 70 61 72 65 64 20 next [prepared
301f0 73 74 61 74 65 6d 65 6e 74 5d 20 61 66 74 65 72 statement] after
30200 0a 2a 2a 20 70 53 74 6d 74 20 61 73 73 6f 63 69 .** pStmt associ
30210 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 ated with the [d
30220 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
30230 6f 6e 5d 20 70 44 62 2e 20 20 49 66 20 70 53 74 on] pDb. If pSt
30240 6d 74 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 mt is NULL.** th
30250 65 6e 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 en this interfac
30260 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e e returns a poin
30270 74 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 ter to the first
30280 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
30290 65 6e 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 ent.** associate
302a0 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 d with the datab
302b0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 ase connection p
302c0 44 62 2e 20 20 49 66 20 6e 6f 20 70 72 65 70 61 Db. If no prepa
302d0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a red statement.**
302e0 20 73 61 74 69 73 66 69 65 73 20 74 68 65 20 63 satisfies the c
302f0 6f 6e 64 69 74 69 6f 6e 73 20 6f 66 20 74 68 69 onditions of thi
30300 73 20 72 6f 75 74 69 6e 65 2c 20 69 74 20 72 65 s routine, it re
30310 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a turns NULL..**.*
30320 2a 20 54 68 65 20 5b 64 61 74 61 62 61 73 65 20 * The [database
30330 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 6f 69 6e connection] poin
30340 74 65 72 20 44 20 69 6e 20 61 20 63 61 6c 6c 20 ter D in a call
30350 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6e to.** [sqlite3_n
30360 65 78 74 5f 73 74 6d 74 28 44 2c 53 29 5d 20 6d ext_stmt(D,S)] m
30370 75 73 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20 ust refer to an
30380 6f 70 65 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a open database.**
30390 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6e 64 20 connection and
303a0 69 6e 20 70 61 72 74 69 63 75 6c 61 72 20 6d 75 in particular mu
303b0 73 74 20 6e 6f 74 20 62 65 20 61 20 4e 55 4c 4c st not be a NULL
303c0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 pointer..**.**
303d0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 Requirements: [H
303e0 31 33 31 34 33 5d 20 5b 48 31 33 31 34 36 5d 20 13143] [H13146]
303f0 5b 48 31 33 31 34 39 5d 20 5b 48 31 33 31 35 32 [H13149] [H13152
30400 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
30410 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 71 sqlite3_stmt *sq
30420 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 lite3_next_stmt(
30430 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 73 71 sqlite3 *pDb, sq
30440 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
30450 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 t);../*.** CAPI3
30460 52 45 46 3a 20 43 6f 6d 6d 69 74 20 41 6e 64 20 REF: Commit And
30470 52 6f 6c 6c 62 61 63 6b 20 4e 6f 74 69 66 69 63 Rollback Notific
30480 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 ation Callbacks
30490 7b 48 31 32 39 35 30 7d 20 3c 53 36 30 34 30 30 {H12950} <S60400
304a0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 >.**.** The sqli
304b0 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 te3_commit_hook(
304c0 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 ) interface regi
304d0 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b sters a callback
304e0 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 .** function to
304f0 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 be invoked whene
30500 76 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f ver a transactio
30510 6e 20 69 73 20 5b 43 4f 4d 4d 49 54 20 7c 20 63 n is [COMMIT | c
30520 6f 6d 6d 69 74 74 65 64 5d 2e 0a 2a 2a 20 41 6e ommitted]..** An
30530 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 y callback set b
30540 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c y a previous cal
30550 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d l to sqlite3_com
30560 6d 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f mit_hook().** fo
30570 72 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 r the same datab
30580 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 ase connection i
30590 73 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a s overridden..**
305a0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 6f 6c The sqlite3_rol
305b0 6c 62 61 63 6b 5f 68 6f 6f 6b 28 29 20 69 6e 74 lback_hook() int
305c0 65 72 66 61 63 65 20 72 65 67 69 73 74 65 72 73 erface registers
305d0 20 61 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 a callback.** f
305e0 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e unction to be in
305f0 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20 61 voked whenever a
30600 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
30610 5b 52 4f 4c 4c 42 41 43 4b 20 7c 20 72 6f 6c 6c [ROLLBACK | roll
30620 65 64 20 62 61 63 6b 5d 2e 0a 2a 2a 20 41 6e 79 ed back]..** Any
30630 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79 callback set by
30640 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c a previous call
30650 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d to sqlite3_comm
30660 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f 72 it_hook().** for
30670 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
30680 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 se connection is
30690 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 20 overridden..**
306a0 54 68 65 20 70 41 72 67 20 61 72 67 75 6d 65 6e The pArg argumen
306b0 74 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f t is passed thro
306c0 75 67 68 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 ugh to the callb
306d0 61 63 6b 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63 ack..** If the c
306e0 61 6c 6c 62 61 63 6b 20 6f 6e 20 61 20 63 6f 6d allback on a com
306f0 6d 69 74 20 68 6f 6f 6b 20 66 75 6e 63 74 69 6f mit hook functio
30700 6e 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 n returns non-ze
30710 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 ro,.** then the
30720 63 6f 6d 6d 69 74 20 69 73 20 63 6f 6e 76 65 72 commit is conver
30730 74 65 64 20 69 6e 74 6f 20 61 20 72 6f 6c 6c 62 ted into a rollb
30740 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ack..**.** If an
30750 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 77 other function w
30760 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 as previously re
30770 67 69 73 74 65 72 65 64 2c 20 69 74 73 0a 2a 2a gistered, its.**
30780 20 70 41 72 67 20 76 61 6c 75 65 20 69 73 20 72 pArg value is r
30790 65 74 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 eturned. Otherw
307a0 69 73 65 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 ise NULL is retu
307b0 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 rned..**.** The
307c0 63 61 6c 6c 62 61 63 6b 20 69 6d 70 6c 65 6d 65 callback impleme
307d0 6e 74 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 ntation must not
307e0 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61 do anything tha
307f0 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a t will modify.**
30800 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
30810 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e nnection that in
30820 76 6f 6b 65 64 20 74 68 65 20 63 61 6c 6c 62 61 voked the callba
30830 63 6b 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73 ck. Any actions
30840 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 .** to modify th
30850 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
30860 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65 ction must be de
30870 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74 ferred until aft
30880 65 72 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 er the.** comple
30890 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c tion of the [sql
308a0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c ite3_step()] cal
308b0 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 65 64 l that triggered
308c0 20 74 68 65 20 63 6f 6d 6d 69 74 0a 2a 2a 20 6f the commit.** o
308d0 72 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20 r rollback hook
308e0 69 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c 61 in the first pla
308f0 63 65 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 ce..** Note that
30900 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
30910 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c e_v2()] and [sql
30920 69 74 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 ite3_step()] bot
30930 68 20 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a h modify their.*
30940 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * database conne
30950 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d ctions for the m
30960 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 eaning of "modif
30970 79 22 20 69 6e 20 74 68 69 73 20 70 61 72 61 67 y" in this parag
30980 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69 raph..**.** Regi
30990 73 74 65 72 69 6e 67 20 61 20 4e 55 4c 4c 20 66 stering a NULL f
309a0 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 unction disables
309b0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a the callback..*
309c0 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 6f *.** When the co
309d0 6d 6d 69 74 20 68 6f 6f 6b 20 63 61 6c 6c 62 61 mmit hook callba
309e0 63 6b 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 ck routine retur
309f0 6e 73 20 7a 65 72 6f 2c 20 74 68 65 20 5b 43 4f ns zero, the [CO
30a00 4d 4d 49 54 5d 0a 2a 2a 20 6f 70 65 72 61 74 69 MMIT].** operati
30a10 6f 6e 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f on is allowed to
30a20 20 63 6f 6e 74 69 6e 75 65 20 6e 6f 72 6d 61 6c continue normal
30a30 6c 79 2e 20 20 49 66 20 74 68 65 20 63 6f 6d 6d ly. If the comm
30a40 69 74 20 68 6f 6f 6b 0a 2a 2a 20 72 65 74 75 72 it hook.** retur
30a50 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 ns non-zero, the
30a60 6e 20 74 68 65 20 5b 43 4f 4d 4d 49 54 5d 20 69 n the [COMMIT] i
30a70 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f s converted into
30a80 20 61 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a a [ROLLBACK]..*
30a90 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 68 * The rollback h
30aa0 6f 6f 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f ook is invoked o
30ab0 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 20 74 68 61 n a rollback tha
30ac0 74 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 61 t results from a
30ad0 20 63 6f 6d 6d 69 74 0a 2a 2a 20 68 6f 6f 6b 20 commit.** hook
30ae0 72 65 74 75 72 6e 69 6e 67 20 6e 6f 6e 2d 7a 65 returning non-ze
30af0 72 6f 2c 20 6a 75 73 74 20 61 73 20 69 74 20 77 ro, just as it w
30b00 6f 75 6c 64 20 62 65 20 77 69 74 68 20 61 6e 79 ould be with any
30b10 20 6f 74 68 65 72 20 72 6f 6c 6c 62 61 63 6b 2e other rollback.
30b20 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 .**.** For the p
30b30 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 urposes of this
30b40 41 50 49 2c 20 61 20 74 72 61 6e 73 61 63 74 69 API, a transacti
30b50 6f 6e 20 69 73 20 73 61 69 64 20 74 6f 20 68 61 on is said to ha
30b60 76 65 20 62 65 65 6e 0a 2a 2a 20 72 6f 6c 6c 65 ve been.** rolle
30b70 64 20 62 61 63 6b 20 69 66 20 61 6e 20 65 78 70 d back if an exp
30b80 6c 69 63 69 74 20 22 52 4f 4c 4c 42 41 43 4b 22 licit "ROLLBACK"
30b90 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 65 78 statement is ex
30ba0 65 63 75 74 65 64 2c 20 6f 72 0a 2a 2a 20 61 6e ecuted, or.** an
30bb0 20 65 72 72 6f 72 20 6f 72 20 63 6f 6e 73 74 72 error or constr
30bc0 61 69 6e 74 20 63 61 75 73 65 73 20 61 6e 20 69 aint causes an i
30bd0 6d 70 6c 69 63 69 74 20 72 6f 6c 6c 62 61 63 6b mplicit rollback
30be0 20 74 6f 20 6f 63 63 75 72 2e 0a 2a 2a 20 54 68 to occur..** Th
30bf0 65 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 e rollback callb
30c00 61 63 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b ack is not invok
30c10 65 64 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 ed if a transact
30c20 69 6f 6e 20 69 73 0a 2a 2a 20 61 75 74 6f 6d 61 ion is.** automa
30c30 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 tically rolled b
30c40 61 63 6b 20 62 65 63 61 75 73 65 20 74 68 65 20 ack because the
30c50 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
30c60 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a ion is closed..*
30c70 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 * The rollback c
30c80 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 69 allback is not i
30c90 6e 76 6f 6b 65 64 20 69 66 20 61 20 74 72 61 6e nvoked if a tran
30ca0 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 72 6f saction is.** ro
30cb0 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61 75 73 lled back becaus
30cc0 65 20 61 20 63 6f 6d 6d 69 74 20 63 61 6c 6c 62 e a commit callb
30cd0 61 63 6b 20 72 65 74 75 72 6e 65 64 20 6e 6f 6e ack returned non
30ce0 2d 7a 65 72 6f 2e 0a 2a 2a 20 3c 74 6f 64 6f 3e -zero..** <todo>
30cf0 20 43 68 65 63 6b 20 6f 6e 20 74 68 69 73 20 3c Check on this <
30d00 2f 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 53 65 65 /todo>.**.** See
30d10 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 also the [sqlit
30d20 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29 e3_update_hook()
30d30 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a ] interface..**.
30d40 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
30d50 0a 2a 2a 20 5b 48 31 32 39 35 31 5d 20 5b 48 31 .** [H12951] [H1
30d60 32 39 35 32 5d 20 5b 48 31 32 39 35 33 5d 20 5b 2952] [H12953] [
30d70 48 31 32 39 35 34 5d 20 5b 48 31 32 39 35 35 5d H12954] [H12955]
30d80 0a 2a 2a 20 5b 48 31 32 39 36 31 5d 20 5b 48 31 .** [H12961] [H1
30d90 32 39 36 32 5d 20 5b 48 31 32 39 36 33 5d 20 5b 2962] [H12963] [
30da0 48 31 32 39 36 34 5d 0a 2a 2f 0a 53 51 4c 49 54 H12964].*/.SQLIT
30db0 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
30dc0 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 te3_commit_hook(
30dd0 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29 sqlite3*, int(*)
30de0 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b (void*), void*);
30df0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
30e00 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 *sqlite3_rollba
30e10 63 6b 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33 2a ck_hook(sqlite3*
30e20 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a , void(*)(void *
30e30 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a ), void*);../*.*
30e40 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 * CAPI3REF: Data
30e50 20 43 68 61 6e 67 65 20 4e 6f 74 69 66 69 63 61 Change Notifica
30e60 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b tion Callbacks {
30e70 48 31 32 39 37 30 7d 20 3c 53 36 30 34 30 30 3e H12970} <S60400>
30e80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
30e90 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29 e3_update_hook()
30ea0 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 interface regis
30eb0 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 ters a callback
30ec0 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77 69 74 68 function.** with
30ed0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
30ee0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 64 65 6e 74 onnection] ident
30ef0 69 66 69 65 64 20 62 79 20 74 68 65 20 66 69 72 ified by the fir
30f00 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 st argument.** t
30f10 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 o be invoked whe
30f20 6e 65 76 65 72 20 61 20 72 6f 77 20 69 73 20 75 never a row is u
30f30 70 64 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64 pdated, inserted
30f40 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 or deleted..**
30f50 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 Any callback set
30f60 20 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 by a previous c
30f70 61 6c 6c 20 74 6f 20 74 68 69 73 20 66 75 6e 63 all to this func
30f80 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20 tion.** for the
30f90 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f same database co
30fa0 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 nnection is over
30fb0 72 69 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 ridden..**.** Th
30fc0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
30fd0 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 t is a pointer t
30fe0 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 o the function t
30ff0 6f 20 69 6e 76 6f 6b 65 20 77 68 65 6e 20 61 0a o invoke when a.
31000 2a 2a 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 ** row is update
31010 64 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 d, inserted or d
31020 65 6c 65 74 65 64 2e 0a 2a 2a 20 54 68 65 20 66 eleted..** The f
31030 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
31040 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 the callback is
31050 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 74 a copy of the t
31060 68 69 72 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a hird argument.**
31070 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 70 64 61 to sqlite3_upda
31080 74 65 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a 20 54 68 te_hook()..** Th
31090 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c 62 61 63 e second callbac
310a0 6b 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e k argument is on
310b0 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 53 e of [SQLITE_INS
310c0 45 52 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 44 45 ERT], [SQLITE_DE
310d0 4c 45 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 LETE],.** or [SQ
310e0 4c 49 54 45 5f 55 50 44 41 54 45 5d 2c 20 64 65 LITE_UPDATE], de
310f0 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f pending on the o
31100 70 65 72 61 74 69 6f 6e 20 74 68 61 74 20 63 61 peration that ca
31110 75 73 65 64 20 74 68 65 20 63 61 6c 6c 62 61 63 used the callbac
31120 6b 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b k.** to be invok
31130 65 64 2e 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 ed..** The third
31140 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75 and fourth argu
31150 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 63 61 6c ments to the cal
31160 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 20 70 6f lback contain po
31170 69 6e 74 65 72 73 20 74 6f 20 74 68 65 0a 2a 2a inters to the.**
31180 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 61 database and ta
31190 62 6c 65 20 6e 61 6d 65 20 63 6f 6e 74 61 69 6e ble name contain
311a0 69 6e 67 20 74 68 65 20 61 66 66 65 63 74 65 64 ing the affected
311b0 20 72 6f 77 2e 0a 2a 2a 20 54 68 65 20 66 69 6e row..** The fin
311c0 61 6c 20 63 61 6c 6c 62 61 63 6b 20 70 61 72 61 al callback para
311d0 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 72 6f meter is the [ro
311e0 77 69 64 5d 20 6f 66 20 74 68 65 20 72 6f 77 2e wid] of the row.
311f0 0a 2a 2a 20 49 6e 20 74 68 65 20 63 61 73 65 20 .** In the case
31200 6f 66 20 61 6e 20 75 70 64 61 74 65 2c 20 74 68 of an update, th
31210 69 73 20 69 73 20 74 68 65 20 5b 72 6f 77 69 64 is is the [rowid
31220 5d 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61 ] after the upda
31230 74 65 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a te takes place..
31240 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 **.** The update
31250 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e 76 hook is not inv
31260 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74 65 72 6e oked when intern
31270 61 6c 20 73 79 73 74 65 6d 20 74 61 62 6c 65 73 al system tables
31280 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 are.** modified
31290 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61 (i.e. sqlite_ma
312a0 73 74 65 72 20 61 6e 64 20 73 71 6c 69 74 65 5f ster and sqlite_
312b0 73 65 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a sequence)..**.**
312c0 20 49 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 In the current
312d0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 implementation,
312e0 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 0a the update hook.
312f0 2a 2a 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 ** is not invoke
31300 64 20 77 68 65 6e 20 64 75 70 6c 69 63 61 74 69 d when duplicati
31310 6f 6e 20 72 6f 77 73 20 61 72 65 20 64 65 6c 65 on rows are dele
31320 74 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 61 ted because of a
31330 6e 0a 2a 2a 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43 n.** [ON CONFLIC
31340 54 20 7c 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 T | ON CONFLICT
31350 52 45 50 4c 41 43 45 5d 20 63 6c 61 75 73 65 2e REPLACE] clause.
31360 20 20 4e 6f 72 20 69 73 20 74 68 65 20 75 70 64 Nor is the upd
31370 61 74 65 20 68 6f 6f 6b 0a 2a 2a 20 69 6e 76 6f ate hook.** invo
31380 6b 65 64 20 77 68 65 6e 20 72 6f 77 73 20 61 72 ked when rows ar
31390 65 20 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20 e deleted using
313a0 74 68 65 20 5b 74 72 75 6e 63 61 74 65 20 6f 70 the [truncate op
313b0 74 69 6d 69 7a 61 74 69 6f 6e 5d 2e 0a 2a 2a 20 timization]..**
313c0 54 68 65 20 65 78 63 65 70 74 69 6f 6e 73 20 64 The exceptions d
313d0 65 66 69 6e 65 64 20 69 6e 20 74 68 69 73 20 70 efined in this p
313e0 61 72 61 67 72 61 70 68 20 6d 69 67 68 74 20 63 aragraph might c
313f0 68 61 6e 67 65 20 69 6e 20 61 20 66 75 74 75 72 hange in a futur
31400 65 0a 2a 2a 20 72 65 6c 65 61 73 65 20 6f 66 20 e.** release of
31410 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 SQLite..**.** Th
31420 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 69 6d e update hook im
31430 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 73 plementation mus
31440 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e t not do anythin
31450 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 g that will modi
31460 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 fy.** the databa
31470 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 se connection th
31480 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 75 at invoked the u
31490 70 64 61 74 65 20 68 6f 6f 6b 2e 20 20 41 6e 79 pdate hook. Any
314a0 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d actions.** to m
314b0 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61 odify the databa
314c0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 se connection mu
314d0 73 74 20 62 65 20 64 65 66 65 72 72 65 64 20 75 st be deferred u
314e0 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a ntil after the.*
314f0 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20 * completion of
31500 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 the [sqlite3_ste
31510 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74 p()] call that t
31520 72 69 67 67 65 72 65 64 20 74 68 65 20 75 70 64 riggered the upd
31530 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 20 4e 6f 74 ate hook..** Not
31540 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f e that [sqlite3_
31550 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e prepare_v2()] an
31560 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 d [sqlite3_step(
31570 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 )] both modify t
31580 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 heir.** database
31590 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 connections for
315a0 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 the meaning of
315b0 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 "modify" in this
315c0 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a paragraph..**.*
315d0 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75 6e * If another fun
315e0 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69 6f ction was previo
315f0 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 2c usly registered,
31600 20 69 74 73 20 70 41 72 67 20 76 61 6c 75 65 0a its pArg value.
31610 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 ** is returned.
31620 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 20 Otherwise NULL
31630 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
31640 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 ** See also the
31650 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f [sqlite3_commit_
31660 68 6f 6f 6b 28 29 5d 20 61 6e 64 20 5b 73 71 6c hook()] and [sql
31670 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f ite3_rollback_ho
31680 6f 6b 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 ok()].** interfa
31690 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ces..**.** Requi
316a0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 rements:.** [H12
316b0 39 37 31 5d 20 5b 48 31 32 39 37 33 5d 20 5b 48 971] [H12973] [H
316c0 31 32 39 37 35 5d 20 5b 48 31 32 39 37 37 5d 20 12975] [H12977]
316d0 5b 48 31 32 39 37 39 5d 20 5b 48 31 32 39 38 31 [H12979] [H12981
316e0 5d 20 5b 48 31 32 39 38 33 5d 20 5b 48 31 32 39 ] [H12983] [H129
316f0 38 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 86].*/.SQLITE_AP
31700 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f I void *sqlite3_
31710 75 70 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20 73 update_hook(. s
31720 71 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 qlite3*, . void
31730 28 2a 29 28 76 6f 69 64 20 2a 2c 69 6e 74 20 2c (*)(void *,int ,
31740 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68 61 char const *,cha
31750 72 20 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74 65 r const *,sqlite
31760 33 5f 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64 3_int64),. void
31770 2a 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 *.);../*.** CAPI
31780 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20 3REF: Enable Or
31790 44 69 73 61 62 6c 65 20 53 68 61 72 65 64 20 50 Disable Shared P
317a0 61 67 65 72 20 43 61 63 68 65 20 7b 48 31 30 33 ager Cache {H103
317b0 33 30 7d 20 3c 53 33 30 39 30 30 3e 0a 2a 2a 20 30} <S30900>.**
317c0 4b 45 59 57 4f 52 44 53 3a 20 7b 73 68 61 72 65 KEYWORDS: {share
317d0 64 20 63 61 63 68 65 7d 0a 2a 2a 0a 2a 2a 20 54 d cache}.**.** T
317e0 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 61 62 his routine enab
317f0 6c 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 les or disables
31800 74 68 65 20 73 68 61 72 69 6e 67 20 6f 66 20 74 the sharing of t
31810 68 65 20 64 61 74 61 62 61 73 65 20 63 61 63 68 he database cach
31820 65 0a 2a 2a 20 61 6e 64 20 73 63 68 65 6d 61 20 e.** and schema
31830 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 data structures
31840 62 65 74 77 65 65 6e 20 5b 64 61 74 61 62 61 73 between [databas
31850 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7c 20 63 e connection | c
31860 6f 6e 6e 65 63 74 69 6f 6e 73 5d 0a 2a 2a 20 74 onnections].** t
31870 6f 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 o the same datab
31880 61 73 65 2e 20 53 68 61 72 69 6e 67 20 69 73 20 ase. Sharing is
31890 65 6e 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 enabled if the a
318a0 72 67 75 6d 65 6e 74 20 69 73 20 74 72 75 65 0a rgument is true.
318b0 2a 2a 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 ** and disabled
318c0 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 if the argument
318d0 69 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 is false..**.**
318e0 43 61 63 68 65 20 73 68 61 72 69 6e 67 20 69 73 Cache sharing is
318f0 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 64 69 73 enabled and dis
31900 61 62 6c 65 64 20 66 6f 72 20 61 6e 20 65 6e 74 abled for an ent
31910 69 72 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 20 ire process..**
31920 54 68 69 73 20 69 73 20 61 20 63 68 61 6e 67 65 This is a change
31930 20 61 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65 as of SQLite ve
31940 72 73 69 6f 6e 20 33 2e 35 2e 30 2e 20 49 6e 20 rsion 3.5.0. In
31950 70 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 20 6f prior versions o
31960 66 20 53 51 4c 69 74 65 2c 0a 2a 2a 20 73 68 61 f SQLite,.** sha
31970 72 69 6e 67 20 77 61 73 20 65 6e 61 62 6c 65 64 ring was enabled
31980 20 6f 72 20 64 69 73 61 62 6c 65 64 20 66 6f 72 or disabled for
31990 20 65 61 63 68 20 74 68 72 65 61 64 20 73 65 70 each thread sep
319a0 61 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 arately..**.** T
319b0 68 65 20 63 61 63 68 65 20 73 68 61 72 69 6e 67 he cache sharing
319c0 20 6d 6f 64 65 20 73 65 74 20 62 79 20 74 68 69 mode set by thi
319d0 73 20 69 6e 74 65 72 66 61 63 65 20 65 66 66 65 s interface effe
319e0 63 74 73 20 61 6c 6c 20 73 75 62 73 65 71 75 65 cts all subseque
319f0 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 5b nt.** calls to [
31a00 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c sqlite3_open()],
31a10 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 [sqlite3_open_v
31a20 32 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 2()], and [sqlit
31a30 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a e3_open16()]..**
31a40 20 45 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 Existing databa
31a50 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 se connections c
31a60 6f 6e 74 69 6e 75 65 20 75 73 65 20 74 68 65 20 ontinue use the
31a70 73 68 61 72 69 6e 67 20 6d 6f 64 65 0a 2a 2a 20 sharing mode.**
31a80 74 68 61 74 20 77 61 73 20 69 6e 20 65 66 66 65 that was in effe
31a90 63 74 20 61 74 20 74 68 65 20 74 69 6d 65 20 74 ct at the time t
31aa0 68 65 79 20 77 65 72 65 20 6f 70 65 6e 65 64 2e hey were opened.
31ab0 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 .**.** Virtual t
31ac0 61 62 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20 ables cannot be
31ad0 75 73 65 64 20 77 69 74 68 20 61 20 73 68 61 72 used with a shar
31ae0 65 64 20 63 61 63 68 65 2e 20 20 57 68 65 6e 20 ed cache. When
31af0 73 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 shared.** cache
31b00 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 is enabled, the
31b10 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
31b20 6d 6f 64 75 6c 65 28 29 5d 20 41 50 49 20 75 73 module()] API us
31b30 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a ed to register.*
31b40 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 * virtual tables
31b50 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 will always ret
31b60 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a urn an error..**
31b70 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
31b80 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
31b90 5f 4f 4b 5d 20 69 66 20 73 68 61 72 65 64 20 63 _OK] if shared c
31ba0 61 63 68 65 20 77 61 73 20 65 6e 61 62 6c 65 64 ache was enabled
31bb0 20 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 or disabled.**
31bc0 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 41 successfully. A
31bd0 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 n [error code] i
31be0 73 20 72 65 74 75 72 6e 65 64 20 6f 74 68 65 72 s returned other
31bf0 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 wise..**.** Shar
31c00 65 64 20 63 61 63 68 65 20 69 73 20 64 69 73 61 ed cache is disa
31c10 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e bled by default.
31c20 20 42 75 74 20 74 68 69 73 20 6d 69 67 68 74 20 But this might
31c30 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 66 75 74 change in.** fut
31c40 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 ure releases of
31c50 53 51 4c 69 74 65 2e 20 20 41 70 70 6c 69 63 61 SQLite. Applica
31c60 74 69 6f 6e 73 20 74 68 61 74 20 63 61 72 65 20 tions that care
31c70 61 62 6f 75 74 20 73 68 61 72 65 64 0a 2a 2a 20 about shared.**
31c80 63 61 63 68 65 20 73 65 74 74 69 6e 67 20 73 68 cache setting sh
31c90 6f 75 6c 64 20 73 65 74 20 69 74 20 65 78 70 6c ould set it expl
31ca0 69 63 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 65 icitly..**.** Se
31cb0 65 20 41 6c 73 6f 3a 20 20 5b 53 51 4c 69 74 65 e Also: [SQLite
31cc0 20 53 68 61 72 65 64 2d 43 61 63 68 65 20 4d 6f Shared-Cache Mo
31cd0 64 65 5d 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 de].**.** Requir
31ce0 65 6d 65 6e 74 73 3a 20 5b 48 31 30 33 33 31 5d ements: [H10331]
31cf0 20 5b 48 31 30 33 33 36 5d 20 5b 48 31 30 33 33 [H10336] [H1033
31d00 37 5d 20 5b 48 31 30 33 33 39 5d 0a 2a 2f 0a 53 7] [H10339].*/.S
31d10 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
31d20 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 lite3_enable_sha
31d30 72 65 64 5f 63 61 63 68 65 28 69 6e 74 29 3b 0a red_cache(int);.
31d40 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
31d50 20 41 74 74 65 6d 70 74 20 54 6f 20 46 72 65 65 Attempt To Free
31d60 20 48 65 61 70 20 4d 65 6d 6f 72 79 20 7b 48 31 Heap Memory {H1
31d70 37 33 34 30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 7340} <S30220>.*
31d80 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
31d90 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 _release_memory(
31da0 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 ) interface atte
31db0 6d 70 74 73 20 74 6f 20 66 72 65 65 20 4e 20 62 mpts to free N b
31dc0 79 74 65 73 0a 2a 2a 20 6f 66 20 68 65 61 70 20 ytes.** of heap
31dd0 6d 65 6d 6f 72 79 20 62 79 20 64 65 61 6c 6c 6f memory by deallo
31de0 63 61 74 69 6e 67 20 6e 6f 6e 2d 65 73 73 65 6e cating non-essen
31df0 74 69 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f tial memory allo
31e00 63 61 74 69 6f 6e 73 0a 2a 2a 20 68 65 6c 64 20 cations.** held
31e10 62 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 by the database
31e20 6c 69 62 72 61 72 79 2e 20 7b 45 4e 44 7d 20 20 library. {END}
31e30 4d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 20 63 Memory used to c
31e40 61 63 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a ache database.**
31e50 20 70 61 67 65 73 20 74 6f 20 69 6d 70 72 6f 76 pages to improv
31e60 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 73 e performance is
31e70 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e an example of n
31e80 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d on-essential mem
31e90 6f 72 79 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f ory..** sqlite3_
31ea0 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 release_memory()
31eb0 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
31ec0 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 ber of bytes act
31ed0 75 61 6c 6c 79 20 66 72 65 65 64 2c 0a 2a 2a 20 ually freed,.**
31ee0 77 68 69 63 68 20 6d 69 67 68 74 20 62 65 20 6d which might be m
31ef0 6f 72 65 20 6f 72 20 6c 65 73 73 20 74 68 61 6e ore or less than
31f00 20 74 68 65 20 61 6d 6f 75 6e 74 20 72 65 71 75 the amount requ
31f10 65 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 ested..**.** Req
31f20 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 37 33 uirements: [H173
31f30 34 31 5d 20 5b 48 31 37 33 34 32 5d 0a 2a 2f 0a 41] [H17342].*/.
31f40 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
31f50 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d qlite3_release_m
31f60 65 6d 6f 72 79 28 69 6e 74 29 3b 0a 0a 2f 2a 0a emory(int);../*.
31f70 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6d 70 ** CAPI3REF: Imp
31f80 6f 73 65 20 41 20 4c 69 6d 69 74 20 4f 6e 20 48 ose A Limit On H
31f90 65 61 70 20 53 69 7a 65 20 7b 48 31 37 33 35 30 eap Size {H17350
31fa0 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 2a 2a } <S30220>.**.**
31fb0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6f 66 The sqlite3_sof
31fc0 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 20 69 t_heap_limit() i
31fd0 6e 74 65 72 66 61 63 65 20 70 6c 61 63 65 73 20 nterface places
31fe0 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 0a 2a a "soft" limit.*
31ff0 2a 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 * on the amount
32000 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 74 of heap memory t
32010 68 61 74 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 hat may be alloc
32020 61 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a ated by SQLite..
32030 2a 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 6e 61 ** If an interna
32040 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 l allocation is
32050 72 65 71 75 65 73 74 65 64 20 74 68 61 74 20 77 requested that w
32060 6f 75 6c 64 20 65 78 63 65 65 64 20 74 68 65 0a ould exceed the.
32070 2a 2a 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d ** soft heap lim
32080 69 74 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c it, [sqlite3_rel
32090 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 69 ease_memory()] i
320a0 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 65 20 6f 72 s invoked one or
320b0 0a 2a 2a 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 .** more times t
320c0 6f 20 66 72 65 65 20 75 70 20 73 6f 6d 65 20 73 o free up some s
320d0 70 61 63 65 20 62 65 66 6f 72 65 20 74 68 65 20 pace before the
320e0 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 70 65 allocation is pe
320f0 72 66 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 rformed..**.** T
32100 68 65 20 6c 69 6d 69 74 20 69 73 20 63 61 6c 6c he limit is call
32110 65 64 20 22 73 6f 66 74 22 2c 20 62 65 63 61 75 ed "soft", becau
32120 73 65 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 72 se if [sqlite3_r
32130 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d elease_memory()]
32140 0a 2a 2a 20 63 61 6e 6e 6f 74 20 66 72 65 65 20 .** cannot free
32150 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f 72 sufficient memor
32160 79 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65 y to prevent the
32170 20 6c 69 6d 69 74 20 66 72 6f 6d 20 62 65 69 6e limit from bein
32180 67 20 65 78 63 65 65 64 65 64 2c 0a 2a 2a 20 74 g exceeded,.** t
32190 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c he memory is all
321a0 6f 63 61 74 65 64 20 61 6e 79 77 61 79 20 61 6e ocated anyway an
321b0 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70 d the current op
321c0 65 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 73 eration proceeds
321d0 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 67 61 74 69 ..**.** A negati
321e0 76 65 20 6f 72 20 7a 65 72 6f 20 76 61 6c 75 65 ve or zero value
321f0 20 66 6f 72 20 4e 20 6d 65 61 6e 73 20 74 68 61 for N means tha
32200 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 6f t there is no so
32210 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 61 6e ft heap limit an
32220 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 d.** [sqlite3_re
32230 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 lease_memory()]
32240 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 20 63 61 6c will only be cal
32250 6c 65 64 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20 led when memory
32260 69 73 20 65 78 68 61 75 73 74 65 64 2e 0a 2a 2a is exhausted..**
32270 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c The default val
32280 75 65 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20 ue for the soft
32290 68 65 61 70 20 6c 69 6d 69 74 20 69 73 20 7a 65 heap limit is ze
322a0 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 ro..**.** SQLite
322b0 20 6d 61 6b 65 73 20 61 20 62 65 73 74 20 65 66 makes a best ef
322c0 66 6f 72 74 20 74 6f 20 68 6f 6e 6f 72 20 74 68 fort to honor th
322d0 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 e soft heap limi
322e0 74 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65 t..** But if the
322f0 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 soft heap limit
32300 20 63 61 6e 6e 6f 74 20 62 65 20 68 6f 6e 6f 72 cannot be honor
32310 65 64 2c 20 65 78 65 63 75 74 69 6f 6e 20 77 69 ed, execution wi
32320 6c 6c 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 77 ll.** continue w
32330 69 74 68 6f 75 74 20 65 72 72 6f 72 20 6f 72 20 ithout error or
32340 6e 6f 74 69 66 69 63 61 74 69 6f 6e 2e 20 20 54 notification. T
32350 68 69 73 20 69 73 20 77 68 79 20 74 68 65 20 6c his is why the l
32360 69 6d 69 74 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 imit is.** calle
32370 64 20 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 d a "soft" limit
32380 2e 20 20 49 74 20 69 73 20 61 64 76 69 73 6f 72 . It is advisor
32390 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 72 y only..**.** Pr
323a0 69 6f 72 20 74 6f 20 53 51 4c 69 74 65 20 76 65 ior to SQLite ve
323b0 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 69 rsion 3.5.0, thi
323c0 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 s routine only c
323d0 6f 6e 73 74 72 61 69 6e 65 64 20 74 68 65 20 6d onstrained the m
323e0 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 emory.** allocat
323f0 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74 ed by a single t
32400 68 72 65 61 64 20 2d 20 74 68 65 20 73 61 6d 65 hread - the same
32410 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 thread in which
32420 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a this routine.**
32430 20 72 75 6e 73 2e 20 20 42 65 67 69 6e 6e 69 6e runs. Beginnin
32440 67 20 77 69 74 68 20 53 51 4c 69 74 65 20 76 65 g with SQLite ve
32450 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 65 rsion 3.5.0, the
32460 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 soft heap limit
32470 20 69 73 0a 2a 2a 20 61 70 70 6c 69 65 64 20 74 is.** applied t
32480 6f 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 54 o all threads. T
32490 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 he value specifi
324a0 65 64 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20 ed for the soft
324b0 68 65 61 70 20 6c 69 6d 69 74 0a 2a 2a 20 69 73 heap limit.** is
324c0 20 61 6e 20 75 70 70 65 72 20 62 6f 75 6e 64 20 an upper bound
324d0 6f 6e 20 74 68 65 20 74 6f 74 61 6c 20 6d 65 6d on the total mem
324e0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 ory allocation f
324f0 6f 72 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 or all threads.
32500 49 6e 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e In.** version 3.
32510 35 2e 30 20 74 68 65 72 65 20 69 73 20 6e 6f 20 5.0 there is no
32520 6d 65 63 68 61 6e 69 73 6d 20 66 6f 72 20 6c 69 mechanism for li
32530 6d 69 74 69 6e 67 20 74 68 65 20 68 65 61 70 20 miting the heap
32540 75 73 61 67 65 20 66 6f 72 0a 2a 2a 20 69 6e 64 usage for.** ind
32550 69 76 69 64 75 61 6c 20 74 68 72 65 61 64 73 2e ividual threads.
32560 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
32570 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 33 35 31 5d nts:.** [H16351]
32580 20 5b 48 31 36 33 35 32 5d 20 5b 48 31 36 33 35 [H16352] [H1635
32590 33 5d 20 5b 48 31 36 33 35 34 5d 20 5b 48 31 36 3] [H16354] [H16
325a0 33 35 35 5d 20 5b 48 31 36 33 35 38 5d 0a 2a 2f 355] [H16358].*/
325b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
325c0 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 sqlite3_soft_he
325d0 61 70 5f 6c 69 6d 69 74 28 69 6e 74 29 3b 0a 0a ap_limit(int);..
325e0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
325f0 45 78 74 72 61 63 74 20 4d 65 74 61 64 61 74 61 Extract Metadata
32600 20 41 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e 20 About A Column
32610 4f 66 20 41 20 54 61 62 6c 65 20 7b 48 31 32 38 Of A Table {H128
32620 35 30 7d 20 3c 53 36 30 33 30 30 3e 0a 2a 2a 0a 50} <S60300>.**.
32630 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
32640 72 65 74 75 72 6e 73 20 6d 65 74 61 64 61 74 61 returns metadata
32650 20 61 62 6f 75 74 20 61 20 73 70 65 63 69 66 69 about a specifi
32660 63 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73 70 c column of a sp
32670 65 63 69 66 69 63 0a 2a 2a 20 64 61 74 61 62 61 ecific.** databa
32680 73 65 20 74 61 62 6c 65 20 61 63 63 65 73 73 69 se table accessi
32690 62 6c 65 20 75 73 69 6e 67 20 74 68 65 20 5b 64 ble using the [d
326a0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
326b0 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 70 61 on] handle.** pa
326c0 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 ssed as the firs
326d0 74 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d t function argum
326e0 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ent..**.** The c
326f0 6f 6c 75 6d 6e 20 69 73 20 69 64 65 6e 74 69 66 olumn is identif
32700 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e ied by the secon
32710 64 2c 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 d, third and fou
32720 72 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 rth parameters t
32730 6f 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 o.** this functi
32740 6f 6e 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 on. The second p
32750 61 72 61 6d 65 74 65 72 20 69 73 20 65 69 74 68 arameter is eith
32760 65 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 er the name of t
32770 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 28 he database.** (
32780 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 i.e. "main", "te
32790 6d 70 22 20 6f 72 20 61 6e 20 61 74 74 61 63 68 mp" or an attach
327a0 65 64 20 64 61 74 61 62 61 73 65 29 20 63 6f 6e ed database) con
327b0 74 61 69 6e 69 6e 67 20 74 68 65 20 73 70 65 63 taining the spec
327c0 69 66 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20 6f ified.** table o
327d0 72 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 r NULL. If it is
327e0 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 NULL, then all
327f0 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 attached databas
32800 65 73 20 61 72 65 20 73 65 61 72 63 68 65 64 0a es are searched.
32810 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 ** for the table
32820 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 using the same
32830 61 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 20 62 algorithm used b
32840 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 65 y the database e
32850 6e 67 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 6f ngine to.** reso
32860 6c 76 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 lve unqualified
32870 74 61 62 6c 65 20 72 65 66 65 72 65 6e 63 65 73 table references
32880 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 ..**.** The thir
32890 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 d and fourth par
328a0 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 ameters to this
328b0 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 function are the
328c0 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d table and colum
328d0 6e 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 n.** name of the
328e0 20 64 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e 2c desired column,
328f0 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 4e respectively. N
32900 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 either of these
32910 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d 61 parameters.** ma
32920 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a y be NULL..**.**
32930 20 4d 65 74 61 64 61 74 61 20 69 73 20 72 65 74 Metadata is ret
32940 75 72 6e 65 64 20 62 79 20 77 72 69 74 69 6e 67 urned by writing
32950 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c to the memory l
32960 6f 63 61 74 69 6f 6e 73 20 70 61 73 73 65 64 20 ocations passed
32970 61 73 20 74 68 65 20 35 74 68 0a 2a 2a 20 61 6e as the 5th.** an
32980 64 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 d subsequent par
32990 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 ameters to this
329a0 66 75 6e 63 74 69 6f 6e 2e 20 41 6e 79 20 6f 66 function. Any of
329b0 20 74 68 65 73 65 20 61 72 67 75 6d 65 6e 74 73 these arguments
329c0 20 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2c may be.** NULL,
329d0 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 in which case t
329e0 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
329f0 20 65 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 61 element of meta
32a00 64 61 74 61 20 69 73 20 6f 6d 69 74 74 65 64 2e data is omitted.
32a10 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f .**.** <blockquo
32a20 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f te>.** <table bo
32a30 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72 rder="1">.** <tr
32a40 3e 3c 74 68 3e 20 50 61 72 61 6d 65 74 65 72 20 ><th> Parameter
32a50 3c 74 68 3e 20 4f 75 74 70 75 74 3c 62 72 3e 54 <th> Output<br>T
32a60 79 70 65 20 3c 74 68 3e 20 20 44 65 73 63 72 69 ype <th> Descri
32a70 70 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e ption.**.** <tr>
32a80 3c 74 64 3e 20 35 74 68 20 3c 74 64 3e 20 63 6f <td> 5th <td> co
32a90 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 44 nst char* <td> D
32aa0 61 74 61 20 74 79 70 65 0a 2a 2a 20 3c 74 72 3e ata type.** <tr>
32ab0 3c 74 64 3e 20 36 74 68 20 3c 74 64 3e 20 63 6f <td> 6th <td> co
32ac0 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 4e nst char* <td> N
32ad0 61 6d 65 20 6f 66 20 64 65 66 61 75 6c 74 20 63 ame of default c
32ae0 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
32af0 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 37 74 e.** <tr><td> 7t
32b00 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 h <td> int
32b10 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 <td> True if
32b20 63 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e 4f 54 column has a NOT
32b30 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 NULL constraint
32b40 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 74 68 .** <tr><td> 8th
32b50 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 <td> int
32b60 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 <td> True if c
32b70 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 olumn is part of
32b80 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 the PRIMARY KEY
32b90 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 74 68 .** <tr><td> 9th
32ba0 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 <td> int
32bb0 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 <td> True if c
32bc0 6f 6c 75 6d 6e 20 69 73 20 5b 41 55 54 4f 49 4e olumn is [AUTOIN
32bd0 43 52 45 4d 45 4e 54 5d 0a 2a 2a 20 3c 2f 74 61 CREMENT].** </ta
32be0 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 ble>.** </blockq
32bf0 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 uote>.**.** The
32c00 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 memory pointed t
32c10 6f 20 62 79 20 74 68 65 20 63 68 61 72 61 63 74 o by the charact
32c20 65 72 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 er pointers retu
32c30 72 6e 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 rned for the.**
32c40 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 declaration type
32c50 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 and collation s
32c60 65 71 75 65 6e 63 65 20 69 73 20 76 61 6c 69 64 equence is valid
32c70 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 20 only until the
32c80 6e 65 78 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 next.** call to
32c90 61 6e 79 20 53 51 4c 69 74 65 20 41 50 49 20 66 any SQLite API f
32ca0 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 unction..**.** I
32cb0 66 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 f the specified
32cc0 74 61 62 6c 65 20 69 73 20 61 63 74 75 61 6c 6c table is actuall
32cd0 79 20 61 20 76 69 65 77 2c 20 61 6e 20 5b 65 72 y a view, an [er
32ce0 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 ror code] is ret
32cf0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 urned..**.** If
32d00 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f the specified co
32d10 6c 75 6d 6e 20 69 73 20 22 72 6f 77 69 64 22 2c lumn is "rowid",
32d20 20 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 69 "oid" or "_rowi
32d30 64 5f 22 20 61 6e 64 20 61 6e 0a 2a 2a 20 5b 49 d_" and an.** [I
32d40 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K
32d50 45 59 5d 20 63 6f 6c 75 6d 6e 20 68 61 73 20 62 EY] column has b
32d60 65 65 6e 20 65 78 70 6c 69 63 69 74 6c 79 20 64 een explicitly d
32d70 65 63 6c 61 72 65 64 2c 20 74 68 65 6e 20 74 68 eclared, then th
32d80 65 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 e output.** para
32d90 6d 65 74 65 72 73 20 61 72 65 20 73 65 74 20 66 meters are set f
32da0 6f 72 20 74 68 65 20 65 78 70 6c 69 63 69 74 6c or the explicitl
32db0 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d y declared colum
32dc0 6e 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e n. If there is n
32dd0 6f 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20 o.** explicitly
32de0 64 65 63 6c 61 72 65 64 20 5b 49 4e 54 45 47 45 declared [INTEGE
32df0 52 20 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 63 R PRIMARY KEY] c
32e00 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 20 olumn, then the
32e10 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d 65 output.** parame
32e20 74 65 72 73 20 61 72 65 20 73 65 74 20 61 73 20 ters are set as
32e30 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c follows:.**.** <
32e40 70 72 65 3e 0a 2a 2a 20 20 20 20 20 64 61 74 61 pre>.** data
32e50 20 74 79 70 65 3a 20 22 49 4e 54 45 47 45 52 22 type: "INTEGER"
32e60 0a 2a 2a 20 20 20 20 20 63 6f 6c 6c 61 74 69 6f .** collatio
32e70 6e 20 73 65 71 75 65 6e 63 65 3a 20 22 42 49 4e n sequence: "BIN
32e80 41 52 59 22 0a 2a 2a 20 20 20 20 20 6e 6f 74 20 ARY".** not
32e90 6e 75 6c 6c 3a 20 30 0a 2a 2a 20 20 20 20 20 70 null: 0.** p
32ea0 72 69 6d 61 72 79 20 6b 65 79 3a 20 31 0a 2a 2a rimary key: 1.**
32eb0 20 20 20 20 20 61 75 74 6f 20 69 6e 63 72 65 6d auto increm
32ec0 65 6e 74 3a 20 30 0a 2a 2a 20 3c 2f 70 72 65 3e ent: 0.** </pre>
32ed0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
32ee0 74 69 6f 6e 20 6d 61 79 20 6c 6f 61 64 20 6f 6e tion may load on
32ef0 65 20 6f 72 20 6d 6f 72 65 20 73 63 68 65 6d 61 e or more schema
32f00 73 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 s from database
32f10 66 69 6c 65 73 2e 20 49 66 20 61 6e 0a 2a 2a 20 files. If an.**
32f20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 error occurs dur
32f30 69 6e 67 20 74 68 69 73 20 70 72 6f 63 65 73 73 ing this process
32f40 2c 20 6f 72 20 69 66 20 74 68 65 20 72 65 71 75 , or if the requ
32f50 65 73 74 65 64 20 74 61 62 6c 65 20 6f 72 20 63 ested table or c
32f60 6f 6c 75 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 olumn.** cannot
32f70 62 65 20 66 6f 75 6e 64 2c 20 61 6e 20 5b 65 72 be found, an [er
32f80 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 ror code] is ret
32f90 75 72 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 urned and an err
32fa0 6f 72 20 6d 65 73 73 61 67 65 20 6c 65 66 74 0a or message left.
32fb0 2a 2a 20 69 6e 20 74 68 65 20 5b 64 61 74 61 62 ** in the [datab
32fc0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
32fd0 28 74 6f 20 62 65 20 72 65 74 72 69 65 76 65 64 (to be retrieved
32fe0 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 65 using sqlite3_e
32ff0 72 72 6d 73 67 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 rrmsg())..**.**
33000 54 68 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 This API is only
33010 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 available if th
33020 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f e library was co
33030 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a mpiled with the.
33040 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c ** [SQLITE_ENABL
33050 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 E_COLUMN_METADAT
33060 41 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f A] C-preprocesso
33070 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 r symbol defined
33080 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
33090 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c int sqlite3_tabl
330a0 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 e_column_metadat
330b0 61 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 a(. sqlite3 *db
330c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
330d0 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68 /* Connection h
330e0 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 andle */. const
330f0 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 char *zDbName,
33100 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
33110 73 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20 se name or NULL
33120 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
33130 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20 *zTableName,
33140 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a /* Table name *
33150 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
33160 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20 zColumnName,
33170 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a /* Column name *
33180 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a /. char const *
33190 2a 70 7a 44 61 74 61 54 79 70 65 2c 20 20 20 20 *pzDataType,
331a0 2f 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 6c 61 /* OUTPUT: Decla
331b0 72 65 64 20 64 61 74 61 20 74 79 70 65 20 2a 2f red data type */
331c0 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a . char const **
331d0 70 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f pzCollSeq, /
331e0 2a 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74 * OUTPUT: Collat
331f0 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d ion sequence nam
33200 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74 e */. int *pNot
33210 4e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 Null,
33220 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 /* OUTPUT: Tr
33230 75 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63 ue if NOT NULL c
33240 6f 6e 73 74 72 61 69 6e 74 20 65 78 69 73 74 73 onstraint exists
33250 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d */. int *pPrim
33260 61 72 79 4b 65 79 2c 20 20 20 20 20 20 20 20 20 aryKey,
33270 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 /* OUTPUT: Tru
33280 65 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74 e if column part
33290 20 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20 of PK */. int
332a0 2a 70 41 75 74 6f 69 6e 63 20 20 20 20 20 20 20 *pAutoinc
332b0 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 /* OUTPU
332c0 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d T: True if colum
332d0 6e 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d n is auto-increm
332e0 65 6e 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a ent */.);../*.**
332f0 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 61 64 20 CAPI3REF: Load
33300 41 6e 20 45 78 74 65 6e 73 69 6f 6e 20 7b 48 31 An Extension {H1
33310 32 36 30 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2600} <S20500>.*
33320 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 *.** This interf
33330 61 63 65 20 6c 6f 61 64 73 20 61 6e 20 53 51 4c ace loads an SQL
33340 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69 ite extension li
33350 62 72 61 72 79 20 66 72 6f 6d 20 74 68 65 20 6e brary from the n
33360 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a amed file..**.**
33370 20 7b 48 31 32 36 30 31 7d 20 54 68 65 20 73 71 {H12601} The sq
33380 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e lite3_load_exten
33390 73 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 sion() interface
333a0 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f 61 attempts to loa
333b0 64 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 d an.**
333c0 20 53 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f SQLite extensio
333d0 6e 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 n library contai
333e0 6e 65 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 ned in the file
333f0 7a 46 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 zFile..**.** {H1
33400 32 36 30 32 7d 20 54 68 65 20 65 6e 74 72 79 20 2602} The entry
33410 70 6f 69 6e 74 20 69 73 20 7a 50 72 6f 63 2e 0a point is zProc..
33420 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 33 7d 20 7a **.** {H12603} z
33430 50 72 6f 63 20 6d 61 79 20 62 65 20 30 2c 20 69 Proc may be 0, i
33440 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 n which case the
33450 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 65 6e 74 name of the ent
33460 72 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20 ry point.**
33470 20 20 20 20 20 64 65 66 61 75 6c 74 73 20 74 6f defaults to
33480 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 "sqlite3_extens
33490 69 6f 6e 5f 69 6e 69 74 22 2e 0a 2a 2a 0a 2a 2a ion_init"..**.**
334a0 20 7b 48 31 32 36 30 34 7d 20 54 68 65 20 73 71 {H12604} The sq
334b0 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e lite3_load_exten
334c0 73 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 sion() interface
334d0 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a shall return.**
334e0 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 [SQLIT
334f0 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 E_OK] on success
33500 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 45 52 52 and [SQLITE_ERR
33510 4f 52 5d 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 OR] if something
33520 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a goes wrong..**.
33530 2a 2a 20 7b 48 31 32 36 30 35 7d 20 49 66 20 61 ** {H12605} If a
33540 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 n error occurs a
33550 6e 64 20 70 7a 45 72 72 4d 73 67 20 69 73 20 6e nd pzErrMsg is n
33560 6f 74 20 30 2c 20 74 68 65 6e 20 74 68 65 0a 2a ot 0, then the.*
33570 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
33580 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 te3_load_extensi
33590 6f 6e 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 on()] interface
335a0 73 68 61 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f shall attempt to
335b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69 6c .** fil
335c0 6c 20 2a 70 7a 45 72 72 4d 73 67 20 77 69 74 68 l *pzErrMsg with
335d0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 error message t
335e0 65 78 74 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 ext stored in me
335f0 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 mory.**
33600 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b obtained from [
33610 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
33620 5d 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 63 61 ]. {END} The ca
33630 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a lling function.*
33640 2a 20 20 20 20 20 20 20 20 20 20 73 68 6f 75 6c * shoul
33650 64 20 66 72 65 65 20 74 68 69 73 20 6d 65 6d 6f d free this memo
33660 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 ry by calling [s
33670 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a qlite3_free()]..
33680 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 36 7d 20 45 **.** {H12606} E
33690 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 xtension loading
336a0 20 6d 75 73 74 20 62 65 20 65 6e 61 62 6c 65 64 must be enabled
336b0 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 using.**
336c0 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 [sqlite3_enab
336d0 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f le_load_extensio
336e0 6e 28 29 5d 20 70 72 69 6f 72 20 74 6f 20 63 61 n()] prior to ca
336f0 6c 6c 69 6e 67 20 74 68 69 73 20 41 50 49 2c 0a lling this API,.
33700 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 74 68 65 ** othe
33710 72 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 77 rwise an error w
33720 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e ill be returned.
33730 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
33740 6e 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f nt sqlite3_load_
33750 65 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c extension(. sql
33760 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 ite3 *db,
33770 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 /* Load the e
33780 78 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68 xtension into th
33790 69 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e is database conn
337a0 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 ection */. cons
337b0 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 t char *zFile,
337c0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
337d0 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 shared library
337e0 63 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e containing exten
337f0 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 sion */. const
33800 63 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 char *zProc,
33810 2f 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 /* Entry point.
33820 20 44 65 72 69 76 65 64 20 66 72 6f 6d 20 7a 46 Derived from zF
33830 69 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 ile if 0 */. ch
33840 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 ar **pzErrMsg
33850 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 /* Put error
33860 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 69 66 message here if
33870 20 6e 6f 74 20 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a not 0 */.);../*
33880 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e .** CAPI3REF: En
33890 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 able Or Disable
338a0 45 78 74 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e Extension Loadin
338b0 67 20 7b 48 31 32 36 32 30 7d 20 3c 53 32 30 35 g {H12620} <S205
338c0 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 6f 20 61 73 20 00>.**.** So as
338d0 6e 6f 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75 not to open secu
338e0 72 69 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c rity holes in ol
338f0 64 65 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 der applications
33900 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 75 6e 70 that are.** unp
33910 72 65 70 61 72 65 64 20 74 6f 20 64 65 61 6c 20 repared to deal
33920 77 69 74 68 20 65 78 74 65 6e 73 69 6f 6e 20 6c with extension l
33930 6f 61 64 69 6e 67 2c 20 61 6e 64 20 61 73 20 61 oading, and as a
33940 20 6d 65 61 6e 73 20 6f 66 20 64 69 73 61 62 6c means of disabl
33950 69 6e 67 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e ing.** extension
33960 20 6c 6f 61 64 69 6e 67 20 77 68 69 6c 65 20 65 loading while e
33970 76 61 6c 75 61 74 69 6e 67 20 75 73 65 72 2d 65 valuating user-e
33980 6e 74 65 72 65 64 20 53 51 4c 2c 20 74 68 65 20 ntered SQL, the
33990 66 6f 6c 6c 6f 77 69 6e 67 20 41 50 49 0a 2a 2a following API.**
339a0 20 69 73 20 70 72 6f 76 69 64 65 64 20 74 6f 20 is provided to
339b0 74 75 72 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 turn the [sqlite
339c0 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 3_load_extension
339d0 28 29 5d 20 6d 65 63 68 61 6e 69 73 6d 20 6f 6e ()] mechanism on
339e0 20 61 6e 64 20 6f 66 66 2e 0a 2a 2a 0a 2a 2a 20 and off..**.**
339f0 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e Extension loadin
33a00 67 20 69 73 20 6f 66 66 20 62 79 20 64 65 66 61 g is off by defa
33a10 75 6c 74 2e 20 53 65 65 20 74 69 63 6b 65 74 20 ult. See ticket
33a20 23 31 38 36 33 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 #1863..**.** {H1
33a30 32 36 32 31 7d 20 43 61 6c 6c 20 74 68 65 20 73 2621} Call the s
33a40 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f qlite3_enable_lo
33a50 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 72 ad_extension() r
33a60 6f 75 74 69 6e 65 20 77 69 74 68 20 6f 6e 6f 66 outine with onof
33a70 66 3d 3d 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 f==1.**
33a80 20 74 6f 20 74 75 72 6e 20 65 78 74 65 6e 73 69 to turn extensi
33a90 6f 6e 20 6c 6f 61 64 69 6e 67 20 6f 6e 20 61 6e on loading on an
33aa0 64 20 63 61 6c 6c 20 69 74 20 77 69 74 68 20 6f d call it with o
33ab0 6e 6f 66 66 3d 3d 30 20 74 6f 20 74 75 72 6e 0a noff==0 to turn.
33ac0 2a 2a 20 20 20 20 20 20 20 20 20 20 69 74 20 62 ** it b
33ad0 61 63 6b 20 6f 66 66 20 61 67 61 69 6e 2e 0a 2a ack off again..*
33ae0 2a 0a 2a 2a 20 7b 48 31 32 36 32 32 7d 20 45 78 *.** {H12622} Ex
33af0 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 tension loading
33b00 69 73 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c is off by defaul
33b10 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 t..*/.SQLITE_API
33b20 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 int sqlite3_ena
33b30 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 ble_load_extensi
33b40 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 on(sqlite3 *db,
33b50 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a int onoff);../*.
33b60 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 75 74 ** CAPI3REF: Aut
33b70 6f 6d 61 74 69 63 61 6c 6c 79 20 4c 6f 61 64 20 omatically Load
33b80 41 6e 20 45 78 74 65 6e 73 69 6f 6e 73 20 7b 48 An Extensions {H
33b90 31 32 36 34 30 7d 20 3c 53 32 30 35 30 30 3e 0a 12640} <S20500>.
33ba0 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 63 **.** This API c
33bb0 61 6e 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 74 an be invoked at
33bc0 20 70 72 6f 67 72 61 6d 20 73 74 61 72 74 75 70 program startup
33bd0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 72 65 67 in order to reg
33be0 69 73 74 65 72 0a 2a 2a 20 6f 6e 65 20 6f 72 20 ister.** one or
33bf0 6d 6f 72 65 20 73 74 61 74 69 63 61 6c 6c 79 20 more statically
33c00 6c 69 6e 6b 65 64 20 65 78 74 65 6e 73 69 6f 6e linked extension
33c10 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 61 s that will be a
33c20 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 vailable.** to a
33c30 6c 6c 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65 ll new [database
33c40 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 2e 20 7b connections]. {
33c50 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 END}.**.** This
33c60 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 61 routine stores a
33c70 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
33c80 65 78 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e 20 extension in an
33c90 61 72 72 61 79 20 74 68 61 74 20 69 73 0a 2a 2a array that is.**
33ca0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b obtained from [
33cb0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
33cc0 5d 2e 20 20 49 66 20 79 6f 75 20 72 75 6e 20 61 ]. If you run a
33cd0 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 63 68 65 memory leak che
33ce0 63 6b 65 72 0a 2a 2a 20 6f 6e 20 79 6f 75 72 20 cker.** on your
33cf0 70 72 6f 67 72 61 6d 20 61 6e 64 20 69 74 20 72 program and it r
33d00 65 70 6f 72 74 73 20 61 20 6c 65 61 6b 20 62 65 eports a leak be
33d10 63 61 75 73 65 20 6f 66 20 74 68 69 73 20 61 72 cause of this ar
33d20 72 61 79 2c 20 69 6e 76 6f 6b 65 0a 2a 2a 20 5b ray, invoke.** [
33d30 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 sqlite3_reset_au
33d40 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 to_extension()]
33d50 70 72 69 6f 72 20 74 6f 20 73 68 75 74 64 6f 77 prior to shutdow
33d60 6e 20 74 6f 20 66 72 65 65 20 74 68 65 20 6d 65 n to free the me
33d70 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 mory..**.** {H12
33d80 36 34 31 7d 20 54 68 69 73 20 66 75 6e 63 74 69 641} This functi
33d90 6f 6e 20 72 65 67 69 73 74 65 72 73 20 61 6e 20 on registers an
33da0 65 78 74 65 6e 73 69 6f 6e 20 65 6e 74 72 79 20 extension entry
33db0 70 6f 69 6e 74 20 74 68 61 74 20 69 73 0a 2a 2a point that is.**
33dc0 20 20 20 20 20 20 20 20 20 20 61 75 74 6f 6d 61 automa
33dd0 74 69 63 61 6c 6c 79 20 69 6e 76 6f 6b 65 64 20 tically invoked
33de0 77 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20 5b whenever a new [
33df0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
33e00 69 6f 6e 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 ion].**
33e10 20 69 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 is opened using
33e20 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 [sqlite3_open()
33e30 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e ], [sqlite3_open
33e40 31 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 16()],.**
33e50 20 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f or [sqlite3_o
33e60 70 65 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a pen_v2()]..**.**
33e70 20 7b 48 31 32 36 34 32 7d 20 44 75 70 6c 69 63 {H12642} Duplic
33e80 61 74 65 20 65 78 74 65 6e 73 69 6f 6e 73 20 61 ate extensions a
33e90 72 65 20 64 65 74 65 63 74 65 64 20 73 6f 20 63 re detected so c
33ea0 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 alling this rout
33eb0 69 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ine.**
33ec0 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77 multiple times w
33ed0 69 74 68 20 74 68 65 20 73 61 6d 65 20 65 78 74 ith the same ext
33ee0 65 6e 73 69 6f 6e 20 69 73 20 68 61 72 6d 6c 65 ension is harmle
33ef0 73 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 ss..**.** {H1264
33f00 33 7d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 3} This routine
33f10 73 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 stores a pointer
33f20 20 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f to the extensio
33f30 6e 20 69 6e 20 61 6e 20 61 72 72 61 79 0a 2a 2a n in an array.**
33f40 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20 69 that i
33f50 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 s obtained from
33f60 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 [sqlite3_malloc(
33f70 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 )]..**.** {H1264
33f80 34 7d 20 41 75 74 6f 6d 61 74 69 63 20 65 78 74 4} Automatic ext
33f90 65 6e 73 69 6f 6e 73 20 61 70 70 6c 79 20 61 63 ensions apply ac
33fa0 72 6f 73 73 20 61 6c 6c 20 74 68 72 65 61 64 73 ross all threads
33fb0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
33fc0 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 75 74 6f int sqlite3_auto
33fd0 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20 _extension(void
33fe0 28 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29 28 76 (*xEntryPoint)(v
33ff0 6f 69 64 29 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 oid));../*.** CA
34000 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 75 PI3REF: Reset Au
34010 74 6f 6d 61 74 69 63 20 45 78 74 65 6e 73 69 6f tomatic Extensio
34020 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 36 n Loading {H1266
34030 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 0} <S20500>.**.*
34040 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
34050 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 70 72 65 disables all pre
34060 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 viously register
34070 65 64 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 ed automatic.**
34080 65 78 74 65 6e 73 69 6f 6e 73 2e 20 7b 45 4e 44 extensions. {END
34090 7d 20 20 49 74 20 75 6e 64 6f 65 73 20 74 68 65 } It undoes the
340a0 20 65 66 66 65 63 74 20 6f 66 20 61 6c 6c 20 70 effect of all p
340b0 72 69 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 rior.** [sqlite3
340c0 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 _auto_extension(
340d0 29 5d 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 )] calls..**.**
340e0 7b 48 31 32 36 36 31 7d 20 54 68 69 73 20 66 75 {H12661} This fu
340f0 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 nction disables
34100 61 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79 20 72 all previously r
34110 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20 20 egistered.**
34120 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 20 automatic
34130 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a extensions..**.*
34140 2a 20 7b 48 31 32 36 36 32 7d 20 54 68 69 73 20 * {H12662} This
34150 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 function disable
34160 73 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 s automatic exte
34170 6e 73 69 6f 6e 73 20 69 6e 20 61 6c 6c 20 74 68 nsions in all th
34180 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 reads..*/.SQLITE
34190 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
341a0 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 3_reset_auto_ext
341b0 65 6e 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 0a 2f ension(void);../
341c0 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d *.****** EXPERIM
341d0 45 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 ENTAL - subject
341e0 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 to change withou
341f0 74 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a t notice *******
34200 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
34210 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 e interface to t
34220 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 he virtual-table
34230 20 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 63 75 mechanism is cu
34240 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 rrently consider
34250 65 64 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 ed.** to be expe
34260 72 69 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 rimental. The i
34270 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74 20 63 nterface might c
34280 68 61 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 hange in incompa
34290 74 69 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 tible ways..** I
342a0 66 20 74 68 69 73 20 69 73 20 61 20 70 72 6f 62 f this is a prob
342b0 6c 65 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 lem for you, do
342c0 6e 6f 74 20 75 73 65 20 74 68 65 20 69 6e 74 65 not use the inte
342d0 72 66 61 63 65 20 61 74 20 74 68 69 73 20 74 69 rface at this ti
342e0 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 me..**.** When t
342f0 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 he virtual-table
34300 20 6d 65 63 68 61 6e 69 73 6d 20 73 74 61 62 69 mechanism stabi
34310 6c 69 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 lizes, we will d
34320 65 63 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e eclare the.** in
34330 74 65 72 66 61 63 65 20 66 69 78 65 64 2c 20 73 terface fixed, s
34340 75 70 70 6f 72 74 20 69 74 20 69 6e 64 65 66 69 upport it indefi
34350 6e 69 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f nitely, and remo
34360 76 65 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e ve this comment.
34370 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 .*/../*.** Struc
34380 74 75 72 65 73 20 75 73 65 64 20 62 79 20 74 68 tures used by th
34390 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 e virtual table
343a0 69 6e 74 65 72 66 61 63 65 0a 2a 2f 0a 74 79 70 interface.*/.typ
343b0 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 edef struct sqli
343c0 74 65 33 5f 76 74 61 62 20 73 71 6c 69 74 65 33 te3_vtab sqlite3
343d0 5f 76 74 61 62 3b 0a 74 79 70 65 64 65 66 20 73 _vtab;.typedef s
343e0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e truct sqlite3_in
343f0 64 65 78 5f 69 6e 66 6f 20 73 71 6c 69 74 65 33 dex_info sqlite3
34400 5f 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74 79 70 _index_info;.typ
34410 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 edef struct sqli
34420 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 te3_vtab_cursor
34430 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 sqlite3_vtab_cur
34440 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 sor;.typedef str
34450 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 uct sqlite3_modu
34460 6c 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c le sqlite3_modul
34470 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 e;../*.** CAPI3R
34480 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c EF: Virtual Tabl
34490 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 30 30 e Object {H18000
344a0 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 } <S20400>.** KE
344b0 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f YWORDS: sqlite3_
344c0 6d 6f 64 75 6c 65 20 7b 76 69 72 74 75 61 6c 20 module {virtual
344d0 74 61 62 6c 65 20 6d 6f 64 75 6c 65 7d 0a 2a 2a table module}.**
344e0 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
344f0 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 .** This structu
34500 72 65 2c 20 73 6f 6d 65 74 69 6d 65 73 20 63 61 re, sometimes ca
34510 6c 6c 65 64 20 61 20 61 20 22 76 69 72 74 75 61 lled a a "virtua
34520 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 22 2c l table module",
34530 20 0a 2a 2a 20 64 65 66 69 6e 65 73 20 74 68 65 .** defines the
34540 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
34550 6f 66 20 61 20 5b 76 69 72 74 75 61 6c 20 74 61 of a [virtual ta
34560 62 6c 65 73 5d 2e 20 20 0a 2a 2a 20 54 68 69 73 bles]. .** This
34570 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 73 69 structure consi
34580 73 74 73 20 6d 6f 73 74 6c 79 20 6f 66 20 6d 65 sts mostly of me
34590 74 68 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f thods for the mo
345a0 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 dule..**.** A vi
345b0 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 rtual table modu
345c0 6c 65 20 69 73 20 63 72 65 61 74 65 64 20 62 79 le is created by
345d0 20 66 69 6c 6c 69 6e 67 20 69 6e 20 61 20 70 65 filling in a pe
345e0 72 73 69 73 74 65 6e 74 0a 2a 2a 20 69 6e 73 74 rsistent.** inst
345f0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 ance of this str
34600 75 63 74 75 72 65 20 61 6e 64 20 70 61 73 73 69 ucture and passi
34610 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ng a pointer to
34620 74 68 61 74 20 69 6e 73 74 61 6e 63 65 0a 2a 2a that instance.**
34630 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 to [sqlite3_cre
34640 61 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 6f 72 ate_module()] or
34650 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
34660 5f 6d 6f 64 75 6c 65 5f 76 32 28 29 5d 2e 0a 2a _module_v2()]..*
34670 2a 20 54 68 65 20 72 65 67 69 73 74 72 61 74 69 * The registrati
34680 6f 6e 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 on remains valid
34690 20 75 6e 74 69 6c 20 69 74 20 69 73 20 72 65 70 until it is rep
346a0 6c 61 63 65 64 20 62 79 20 61 20 64 69 66 66 65 laced by a diffe
346b0 72 65 6e 74 0a 2a 2a 20 6d 6f 64 75 6c 65 20 6f rent.** module o
346c0 72 20 75 6e 74 69 6c 20 74 68 65 20 5b 64 61 74 r until the [dat
346d0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
346e0 5d 20 63 6c 6f 73 65 73 2e 20 20 54 68 65 20 63 ] closes. The c
346f0 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 69 ontent.** of thi
34700 73 20 73 74 72 75 63 74 75 72 65 20 6d 75 73 74 s structure must
34710 20 6e 6f 74 20 63 68 61 6e 67 65 20 77 68 69 6c not change whil
34720 65 20 69 74 20 69 73 20 72 65 67 69 73 74 65 72 e it is register
34730 65 64 20 77 69 74 68 0a 2a 2a 20 61 6e 79 20 64 ed with.** any d
34740 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
34750 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 on..*/.struct sq
34760 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 lite3_module {.
34770 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 int iVersion;.
34780 20 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28 int (*xCreate)(
34790 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a sqlite3*, void *
347a0 70 41 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20 pAux,.
347b0 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 int argc, c
347c0 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 onst char *const
347d0 2a 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 *argv,.
347e0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 sqlite3_vt
347f0 61 62 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 ab **ppVTab, cha
34800 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 r**);. int (*xC
34810 6f 6e 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a onnect)(sqlite3*
34820 2c 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 , void *pAux,.
34830 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 int
34840 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 argc, const cha
34850 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 r *const*argv,.
34860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 sq
34870 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 lite3_vtab **ppV
34880 54 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20 Tab, char**);.
34890 69 6e 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78 int (*xBestIndex
348a0 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a )(sqlite3_vtab *
348b0 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 pVTab, sqlite3_i
348c0 6e 64 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 ndex_info*);. i
348d0 6e 74 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74 nt (*xDisconnect
348e0 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a )(sqlite3_vtab *
348f0 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a pVTab);. int (*
34900 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 xDestroy)(sqlite
34910 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 3_vtab *pVTab);.
34920 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 int (*xOpen)(s
34930 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 qlite3_vtab *pVT
34940 61 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 ab, sqlite3_vtab
34950 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 _cursor **ppCurs
34960 6f 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c or);. int (*xCl
34970 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 ose)(sqlite3_vta
34980 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e b_cursor*);. in
34990 74 20 28 2a 78 46 69 6c 74 65 72 29 28 73 71 6c t (*xFilter)(sql
349a0 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
349b0 2a 2c 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 *, int idxNum, c
349c0 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 onst char *idxSt
349d0 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 r,.
349e0 20 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c int argc, sql
349f0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
34a00 76 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 v);. int (*xNex
34a10 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f t)(sqlite3_vtab_
34a20 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 cursor*);. int
34a30 28 2a 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f (*xEof)(sqlite3_
34a40 76 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 vtab_cursor*);.
34a50 20 69 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 int (*xColumn)(
34a60 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 sqlite3_vtab_cur
34a70 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f sor*, sqlite3_co
34a80 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 ntext*, int);.
34a90 69 6e 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71 int (*xRowid)(sq
34aa0 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
34ab0 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 r*, sqlite3_int6
34ac0 34 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e 4 *pRowid);. in
34ad0 74 20 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c t (*xUpdate)(sql
34ae0 69 74 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74 ite3_vtab *, int
34af0 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 , sqlite3_value
34b00 2a 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 **, sqlite3_int6
34b10 34 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42 4 *);. int (*xB
34b20 65 67 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 egin)(sqlite3_vt
34b30 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e ab *pVTab);. in
34b40 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74 t (*xSync)(sqlit
34b50 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b e3_vtab *pVTab);
34b60 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 . int (*xCommit
34b70 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a )(sqlite3_vtab *
34b80 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a pVTab);. int (*
34b90 78 52 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74 xRollback)(sqlit
34ba0 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b e3_vtab *pVTab);
34bb0 0a 20 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75 . int (*xFindFu
34bc0 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f nction)(sqlite3_
34bd0 76 74 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 vtab *pVtab, int
34be0 20 6e 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61 nArg, const cha
34bf0 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 r *zName,.
34c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
34c10 20 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 void (**pxFunc)
34c20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
34c30 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 *,int,sqlite3_va
34c40 6c 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 lue**),.
34c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76 v
34c60 6f 69 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20 20 oid **ppArg);.
34c70 69 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73 int (*xRename)(s
34c80 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 qlite3_vtab *pVt
34c90 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ab, const char *
34ca0 7a 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a zNew);.};../*.**
34cb0 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 CAPI3REF: Virtu
34cc0 61 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e al Table Indexin
34cd0 67 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b 48 g Information {H
34ce0 31 38 31 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 18100} <S20400>.
34cf0 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c ** KEYWORDS: sql
34d00 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a ite3_index_info.
34d10 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
34d20 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
34d30 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 3_index_info str
34d40 75 63 74 75 72 65 20 61 6e 64 20 69 74 73 20 73 ucture and its s
34d50 75 62 73 74 72 75 63 74 75 72 65 73 20 69 73 20 ubstructures is
34d60 75 73 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 20 used to.** pass
34d70 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f information into
34d80 20 61 6e 64 20 72 65 63 65 69 76 65 20 74 68 65 and receive the
34d90 20 72 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20 reply from the
34da0 5b 78 42 65 73 74 49 6e 64 65 78 5d 0a 2a 2a 20 [xBestIndex].**
34db0 6d 65 74 68 6f 64 20 6f 66 20 61 20 5b 76 69 72 method of a [vir
34dc0 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c tual table modul
34dd0 65 5d 2e 20 20 54 68 65 20 66 69 65 6c 64 73 20 e]. The fields
34de0 75 6e 64 65 72 20 2a 2a 49 6e 70 75 74 73 2a 2a under **Inputs**
34df0 20 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 70 75 are the.** inpu
34e00 74 73 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78 ts to xBestIndex
34e10 20 61 6e 64 20 61 72 65 20 72 65 61 64 2d 6f 6e and are read-on
34e20 6c 79 2e 20 20 78 42 65 73 74 49 6e 64 65 78 20 ly. xBestIndex
34e30 69 6e 73 65 72 74 73 20 69 74 73 0a 2a 2a 20 72 inserts its.** r
34e40 65 73 75 6c 74 73 20 69 6e 74 6f 20 74 68 65 20 esults into the
34e50 2a 2a 4f 75 74 70 75 74 73 2a 2a 20 66 69 65 6c **Outputs** fiel
34e60 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 43 ds..**.** The aC
34e70 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 onstraint[] arra
34e80 79 20 72 65 63 6f 72 64 73 20 57 48 45 52 45 20 y records WHERE
34e90 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e clause constrain
34ea0 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a ts of the form:.
34eb0 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 63 6f 6c 75 6d **.** <pre>colum
34ec0 6e 20 4f 50 20 65 78 70 72 3c 2f 70 72 65 3e 0a n OP expr</pre>.
34ed0 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 4f 50 20 69 **.** where OP i
34ee0 73 20 3d 2c 20 26 6c 74 3b 2c 20 26 6c 74 3b 3d s =, <, <=
34ef0 2c 20 26 67 74 3b 2c 20 6f 72 20 26 67 74 3b 3d , >, or >=
34f00 2e 20 20 54 68 65 20 70 61 72 74 69 63 75 6c 61 . The particula
34f10 72 20 6f 70 65 72 61 74 6f 72 20 69 73 0a 2a 2a r operator is.**
34f20 20 73 74 6f 72 65 64 20 69 6e 20 61 43 6f 6e 73 stored in aCons
34f30 74 72 61 69 6e 74 5b 5d 2e 6f 70 2e 20 20 54 68 traint[].op. Th
34f40 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 e index of the c
34f50 6f 6c 75 6d 6e 20 69 73 20 73 74 6f 72 65 64 20 olumn is stored
34f60 69 6e 0a 2a 2a 20 61 43 6f 6e 73 74 72 61 69 6e in.** aConstrain
34f70 74 5b 5d 2e 69 43 6f 6c 75 6d 6e 2e 20 20 61 43 t[].iColumn. aC
34f80 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 onstraint[].usab
34f90 6c 65 20 69 73 20 54 52 55 45 20 69 66 20 74 68 le is TRUE if th
34fa0 65 0a 2a 2a 20 65 78 70 72 20 6f 6e 20 74 68 65 e.** expr on the
34fb0 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 right-hand side
34fc0 20 63 61 6e 20 62 65 20 65 76 61 6c 75 61 74 65 can be evaluate
34fd0 64 20 28 61 6e 64 20 74 68 75 73 20 74 68 65 20 d (and thus the
34fe0 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 69 73 constraint.** is
34ff0 20 75 73 61 62 6c 65 29 20 61 6e 64 20 66 61 6c usable) and fal
35000 73 65 20 69 66 20 69 74 20 63 61 6e 6e 6f 74 2e se if it cannot.
35010 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d .**.** The optim
35020 69 7a 65 72 20 61 75 74 6f 6d 61 74 69 63 61 6c izer automatical
35030 6c 79 20 69 6e 76 65 72 74 73 20 74 65 72 6d 73 ly inverts terms
35040 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 65 78 of the form "ex
35050 70 72 20 4f 50 20 63 6f 6c 75 6d 6e 22 0a 2a 2a pr OP column".**
35060 20 61 6e 64 20 6d 61 6b 65 73 20 6f 74 68 65 72 and makes other
35070 20 73 69 6d 70 6c 69 66 69 63 61 74 69 6f 6e 73 simplifications
35080 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c to the WHERE cl
35090 61 75 73 65 20 69 6e 20 61 6e 20 61 74 74 65 6d ause in an attem
350a0 70 74 20 74 6f 0a 2a 2a 20 67 65 74 20 61 73 20 pt to.** get as
350b0 6d 61 6e 79 20 57 48 45 52 45 20 63 6c 61 75 73 many WHERE claus
350c0 65 20 74 65 72 6d 73 20 69 6e 74 6f 20 74 68 65 e terms into the
350d0 20 66 6f 72 6d 20 73 68 6f 77 6e 20 61 62 6f 76 form shown abov
350e0 65 20 61 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a e as possible..*
350f0 2a 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e * The aConstrain
35100 74 5b 5d 20 61 72 72 61 79 20 6f 6e 6c 79 20 72 t[] array only r
35110 65 70 6f 72 74 73 20 57 48 45 52 45 20 63 6c 61 eports WHERE cla
35120 75 73 65 20 74 65 72 6d 73 20 69 6e 20 74 68 65 use terms in the
35130 20 63 6f 72 72 65 63 74 0a 2a 2a 20 66 6f 72 6d correct.** form
35140 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 that refer to t
35150 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 76 69 he particular vi
35160 72 74 75 61 6c 20 74 61 62 6c 65 20 62 65 69 6e rtual table bein
35170 67 20 71 75 65 72 69 65 64 2e 0a 2a 2a 0a 2a 2a g queried..**.**
35180 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f Information abo
35190 75 74 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 ut the ORDER BY
351a0 63 6c 61 75 73 65 20 69 73 20 73 74 6f 72 65 64 clause is stored
351b0 20 69 6e 20 61 4f 72 64 65 72 42 79 5b 5d 2e 0a in aOrderBy[]..
351c0 2a 2a 20 45 61 63 68 20 74 65 72 6d 20 6f 66 20 ** Each term of
351d0 61 4f 72 64 65 72 42 79 20 72 65 63 6f 72 64 73 aOrderBy records
351e0 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 a column of the
351f0 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 ORDER BY clause
35200 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 42 65 ..**.** The [xBe
35210 73 74 49 6e 64 65 78 5d 20 6d 65 74 68 6f 64 20 stIndex] method
35220 6d 75 73 74 20 66 69 6c 6c 20 61 43 6f 6e 73 74 must fill aConst
35230 72 61 69 6e 74 55 73 61 67 65 5b 5d 20 77 69 74 raintUsage[] wit
35240 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a h information.**
35250 20 61 62 6f 75 74 20 77 68 61 74 20 70 61 72 61 about what para
35260 6d 65 74 65 72 73 20 74 6f 20 70 61 73 73 20 74 meters to pass t
35270 6f 20 78 46 69 6c 74 65 72 2e 20 20 49 66 20 61 o xFilter. If a
35280 72 67 76 49 6e 64 65 78 3e 30 20 74 68 65 6e 0a rgvIndex>0 then.
35290 2a 2a 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e ** the right-han
352a0 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 63 6f d side of the co
352b0 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 43 6f 6e rresponding aCon
352c0 73 74 72 61 69 6e 74 5b 5d 20 69 73 20 65 76 61 straint[] is eva
352d0 6c 75 61 74 65 64 0a 2a 2a 20 61 6e 64 20 62 65 luated.** and be
352e0 63 6f 6d 65 73 20 74 68 65 20 61 72 67 76 49 6e comes the argvIn
352f0 64 65 78 2d 74 68 20 65 6e 74 72 79 20 69 6e 20 dex-th entry in
35300 61 72 67 76 2e 20 20 49 66 20 61 43 6f 6e 73 74 argv. If aConst
35310 72 61 69 6e 74 55 73 61 67 65 5b 5d 2e 6f 6d 69 raintUsage[].omi
35320 74 0a 2a 2a 20 69 73 20 74 72 75 65 2c 20 74 68 t.** is true, th
35330 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e en the constrain
35340 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 t is assumed to
35350 62 65 20 66 75 6c 6c 79 20 68 61 6e 64 6c 65 64 be fully handled
35360 20 62 79 20 74 68 65 0a 2a 2a 20 76 69 72 74 75 by the.** virtu
35370 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 69 73 20 al table and is
35380 6e 6f 74 20 63 68 65 63 6b 65 64 20 61 67 61 69 not checked agai
35390 6e 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a n by SQLite..**.
353a0 2a 2a 20 54 68 65 20 69 64 78 4e 75 6d 20 61 6e ** The idxNum an
353b0 64 20 69 64 78 50 74 72 20 76 61 6c 75 65 73 20 d idxPtr values
353c0 61 72 65 20 72 65 63 6f 72 64 65 64 20 61 6e 64 are recorded and
353d0 20 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 passed into the
353e0 0a 2a 2a 20 5b 78 46 69 6c 74 65 72 5d 20 6d 65 .** [xFilter] me
353f0 74 68 6f 64 2e 0a 2a 2a 20 5b 73 71 6c 69 74 65 thod..** [sqlite
35400 33 5f 66 72 65 65 28 29 5d 20 69 73 20 75 73 65 3_free()] is use
35410 64 20 74 6f 20 66 72 65 65 20 69 64 78 50 74 72 d to free idxPtr
35420 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 66 if and only iff
35430 0a 2a 2a 20 6e 65 65 64 54 6f 46 72 65 65 49 64 .** needToFreeId
35440 78 50 74 72 20 69 73 20 74 72 75 65 2e 0a 2a 2a xPtr is true..**
35450 0a 2a 2a 20 54 68 65 20 6f 72 64 65 72 42 79 43 .** The orderByC
35460 6f 6e 73 75 6d 65 64 20 6d 65 61 6e 73 20 74 68 onsumed means th
35470 61 74 20 6f 75 74 70 75 74 20 66 72 6f 6d 20 5b at output from [
35480 78 46 69 6c 74 65 72 5d 2f 5b 78 4e 65 78 74 5d xFilter]/[xNext]
35490 20 77 69 6c 6c 20 6f 63 63 75 72 20 69 6e 0a 2a will occur in.*
354a0 2a 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 72 * the correct or
354b0 64 65 72 20 74 6f 20 73 61 74 69 73 66 79 20 74 der to satisfy t
354c0 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 he ORDER BY clau
354d0 73 65 20 73 6f 20 74 68 61 74 20 6e 6f 20 73 65 se so that no se
354e0 70 61 72 61 74 65 0a 2a 2a 20 73 6f 72 74 69 6e parate.** sortin
354f0 67 20 73 74 65 70 20 69 73 20 72 65 71 75 69 72 g step is requir
35500 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 ed..**.** The es
35510 74 69 6d 61 74 65 64 43 6f 73 74 20 76 61 6c 75 timatedCost valu
35520 65 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 e is an estimate
35530 20 6f 66 20 74 68 65 20 63 6f 73 74 20 6f 66 20 of the cost of
35540 64 6f 69 6e 67 20 74 68 65 0a 2a 2a 20 70 61 72 doing the.** par
35550 74 69 63 75 6c 61 72 20 6c 6f 6f 6b 75 70 2e 20 ticular lookup.
35560 20 41 20 66 75 6c 6c 20 73 63 61 6e 20 6f 66 20 A full scan of
35570 61 20 74 61 62 6c 65 20 77 69 74 68 20 4e 20 65 a table with N e
35580 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61 ntries should ha
35590 76 65 0a 2a 2a 20 61 20 63 6f 73 74 20 6f 66 20 ve.** a cost of
355a0 4e 2e 20 20 41 20 62 69 6e 61 72 79 20 73 65 61 N. A binary sea
355b0 72 63 68 20 6f 66 20 61 20 74 61 62 6c 65 20 6f rch of a table o
355c0 66 20 4e 20 65 6e 74 72 69 65 73 20 73 68 6f 75 f N entries shou
355d0 6c 64 20 68 61 76 65 20 61 0a 2a 2a 20 63 6f 73 ld have a.** cos
355e0 74 20 6f 66 20 61 70 70 72 6f 78 69 6d 61 74 65 t of approximate
355f0 6c 79 20 6c 6f 67 28 4e 29 2e 0a 2a 2f 0a 73 74 ly log(N)..*/.st
35600 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 ruct sqlite3_ind
35610 65 78 5f 69 6e 66 6f 20 7b 0a 20 20 2f 2a 20 49 ex_info {. /* I
35620 6e 70 75 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e nputs */. int n
35630 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 Constraint;
35640 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
35650 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 of entries in aC
35660 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 73 onstraint */. s
35670 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e truct sqlite3_in
35680 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 7b dex_constraint {
35690 0a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d . int iColum
356a0 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
356b0 2f 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c 65 66 /* Column on lef
356c0 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 63 t-hand side of c
356d0 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20 onstraint */.
356e0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
356f0 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 op; /* C
35700 6f 6e 73 74 72 61 69 6e 74 20 6f 70 65 72 61 74 onstraint operat
35710 6f 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 or */. unsig
35720 6e 65 64 20 63 68 61 72 20 75 73 61 62 6c 65 3b ned char usable;
35730 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
35740 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 this constraint
35750 69 73 20 75 73 61 62 6c 65 20 2a 2f 0a 20 20 20 is usable */.
35760 20 20 69 6e 74 20 69 54 65 72 6d 4f 66 66 73 65 int iTermOffse
35770 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 t; /* U
35780 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2d sed internally -
35790 20 78 42 65 73 74 49 6e 64 65 78 20 73 68 6f 75 xBestIndex shou
357a0 6c 64 20 69 67 6e 6f 72 65 20 2a 2f 0a 20 20 7d ld ignore */. }
357b0 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 *aConstraint;
357c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 /* Tab
357d0 6c 65 20 6f 66 20 57 48 45 52 45 20 63 6c 61 75 le of WHERE clau
357e0 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a se constraints *
357f0 2f 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 /. int nOrderBy
35800 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
35810 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d * Number of term
35820 73 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 s in the ORDER B
35830 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 Y clause */. st
35840 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 ruct sqlite3_ind
35850 65 78 5f 6f 72 64 65 72 62 79 20 7b 0a 20 20 20 ex_orderby {.
35860 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 int iColumn;
35870 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
35880 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a olumn number */.
35890 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 unsigned ch
358a0 61 72 20 64 65 73 63 3b 20 20 20 20 20 20 20 2f ar desc; /
358b0 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 2e * True for DESC.
358c0 20 20 46 61 6c 73 65 20 66 6f 72 20 41 53 43 2e False for ASC.
358d0 20 2a 2f 0a 20 20 7d 20 2a 61 4f 72 64 65 72 42 */. } *aOrderB
358e0 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y;
358f0 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 /* The ORDER BY
35900 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 2f 2a 20 clause */. /*
35910 4f 75 74 70 75 74 73 20 2a 2f 0a 20 20 73 74 72 Outputs */. str
35920 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 uct sqlite3_inde
35930 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 x_constraint_usa
35940 67 65 20 7b 0a 20 20 20 20 69 6e 74 20 61 72 67 ge {. int arg
35950 76 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20 vIndex;
35960 20 20 2f 2a 20 69 66 20 3e 30 2c 20 63 6f 6e 73 /* if >0, cons
35970 74 72 61 69 6e 74 20 69 73 20 70 61 72 74 20 6f traint is part o
35980 66 20 61 72 67 76 20 74 6f 20 78 46 69 6c 74 65 f argv to xFilte
35990 72 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 r */. unsigne
359a0 64 20 63 68 61 72 20 6f 6d 69 74 3b 20 20 20 20 d char omit;
359b0 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 64 65 /* Do not code
359c0 20 61 20 74 65 73 74 20 66 6f 72 20 74 68 69 73 a test for this
359d0 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 constraint */.
359e0 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 55 } *aConstraintU
359f0 73 61 67 65 3b 0a 20 20 69 6e 74 20 69 64 78 4e sage;. int idxN
35a00 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 um;
35a10 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 75 73 65 /* Number use
35a20 64 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 d to identify th
35a30 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 68 61 e index */. cha
35a40 72 20 2a 69 64 78 53 74 72 3b 20 20 20 20 20 20 r *idxStr;
35a50 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e /* Strin
35a60 67 2c 20 70 6f 73 73 69 62 6c 79 20 6f 62 74 61 g, possibly obta
35a70 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
35a80 33 5f 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 69 6e 3_malloc */. in
35a90 74 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 t needToFreeIdxS
35aa0 74 72 3b 20 20 20 20 20 20 2f 2a 20 46 72 65 65 tr; /* Free
35ab0 20 69 64 78 53 74 72 20 75 73 69 6e 67 20 73 71 idxStr using sq
35ac0 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 66 20 lite3_free() if
35ad0 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 true */. int or
35ae0 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 3b 20 20 derByConsumed;
35af0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
35b00 6f 75 74 70 75 74 20 69 73 20 61 6c 72 65 61 64 output is alread
35b10 79 20 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 64 y ordered */. d
35b20 6f 75 62 6c 65 20 65 73 74 69 6d 61 74 65 64 43 ouble estimatedC
35b30 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 45 73 74 ost; /* Est
35b40 69 6d 61 74 65 64 20 63 6f 73 74 20 6f 66 20 75 imated cost of u
35b50 73 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20 sing this index
35b60 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 53 51 */.};.#define SQ
35b70 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 LITE_INDEX_CONST
35b80 52 41 49 4e 54 5f 45 51 20 20 20 20 32 0a 23 64 RAINT_EQ 2.#d
35b90 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 efine SQLITE_IND
35ba0 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54 EX_CONSTRAINT_GT
35bb0 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4.#define SQ
35bc0 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 LITE_INDEX_CONST
35bd0 52 41 49 4e 54 5f 4c 45 20 20 20 20 38 0a 23 64 RAINT_LE 8.#d
35be0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 efine SQLITE_IND
35bf0 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 EX_CONSTRAINT_LT
35c00 20 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20 53 16.#define S
35c10 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 QLITE_INDEX_CONS
35c20 54 52 41 49 4e 54 5f 47 45 20 20 20 20 33 32 0a TRAINT_GE 32.
35c30 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
35c40 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f NDEX_CONSTRAINT_
35c50 4d 41 54 43 48 20 36 34 0a 0a 2f 2a 0a 2a 2a 20 MATCH 64../*.**
35c60 43 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 74 CAPI3REF: Regist
35c70 65 72 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 er A Virtual Tab
35c80 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f le Implementatio
35c90 6e 20 7b 48 31 38 32 30 30 7d 20 3c 53 32 30 34 n {H18200} <S204
35ca0 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 00>.** EXPERIMEN
35cb0 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 TAL.**.** This r
35cc0 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 outine is used t
35cd0 6f 20 72 65 67 69 73 74 65 72 20 61 20 6e 65 77 o register a new
35ce0 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 [virtual table
35cf0 6d 6f 64 75 6c 65 5d 20 6e 61 6d 65 2e 0a 2a 2a module] name..**
35d00 20 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d 75 Module names mu
35d10 73 74 20 62 65 20 72 65 67 69 73 74 65 72 65 64 st be registered
35d20 20 62 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61 74 before.** creat
35d30 69 6e 67 20 61 20 6e 65 77 20 5b 76 69 72 74 75 ing a new [virtu
35d40 61 6c 20 74 61 62 6c 65 5d 20 75 73 69 6e 67 20 al table] using
35d50 74 68 65 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 62 the module, or b
35d60 65 66 6f 72 65 20 75 73 69 6e 67 20 61 0a 2a 2a efore using a.**
35d70 20 70 72 65 65 78 69 73 74 69 6e 67 20 5b 76 69 preexisting [vi
35d80 72 74 75 61 6c 20 74 61 62 6c 65 5d 20 66 6f 72 rtual table] for
35d90 20 74 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a the module..**.
35da0 2a 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 ** The module na
35db0 6d 65 20 69 73 20 72 65 67 69 73 74 65 72 65 64 me is registered
35dc0 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 on the [databas
35dd0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 e connection] sp
35de0 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 ecified.** by th
35df0 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
35e00 72 2e 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 r. The name of
35e10 74 68 65 20 6d 6f 64 75 6c 65 20 69 73 20 67 69 the module is gi
35e20 76 65 6e 20 62 79 20 74 68 65 20 0a 2a 2a 20 73 ven by the .** s
35e30 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e econd parameter.
35e40 20 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61 The third para
35e50 6d 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 meter is a point
35e60 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 69 6d 70 er to.** the imp
35e70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
35e80 68 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c he [virtual tabl
35e90 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 20 54 68 65 e module]. The
35ea0 20 66 6f 75 72 74 68 0a 2a 2a 20 70 61 72 61 6d fourth.** param
35eb0 65 74 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 eter is an arbit
35ec0 72 61 72 79 20 63 6c 69 65 6e 74 20 64 61 74 61 rary client data
35ed0 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 pointer that is
35ee0 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 0a passed through.
35ef0 2a 2a 20 69 6e 74 6f 20 74 68 65 20 5b 78 43 72 ** into the [xCr
35f00 65 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e eate] and [xConn
35f10 65 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66 20 ect] methods of
35f20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c the virtual tabl
35f30 65 20 6d 6f 64 75 6c 65 0a 2a 2a 20 77 68 65 6e e module.** when
35f40 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 a new virtual t
35f50 61 62 6c 65 20 69 73 20 62 65 20 62 65 69 6e 67 able is be being
35f60 20 63 72 65 61 74 65 64 20 6f 72 20 72 65 69 6e created or rein
35f70 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a itialized..**.**
35f80 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 This interface
35f90 68 61 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 has exactly the
35fa0 73 61 6d 65 20 65 66 66 65 63 74 20 61 73 20 63 same effect as c
35fb0 61 6c 6c 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 alling.** [sqlit
35fc0 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 e3_create_module
35fd0 5f 76 32 28 29 5d 20 77 69 74 68 20 61 20 4e 55 _v2()] with a NU
35fe0 4c 4c 20 63 6c 69 65 6e 74 20 64 61 74 61 20 64 LL client data d
35ff0 65 73 74 72 75 63 74 6f 72 2e 0a 2a 2f 0a 53 51 estructor..*/.SQ
36000 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f LITE_API SQLITE_
36010 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 EXPERIMENTAL int
36020 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
36030 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 module(. sqlite
36040 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
36050 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 /* SQLite c
36060 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 onnection to reg
36070 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 ister module wit
36080 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 h */. const cha
36090 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 r *zName,
360a0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
360b0 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e module */. con
360c0 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c st sqlite3_modul
360d0 65 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f e *p, /* Metho
360e0 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c ds for the modul
360f0 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c e */. void *pCl
36100 69 65 6e 74 44 61 74 61 20 20 20 20 20 20 20 20 ientData
36110 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 /* Client data
36120 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f for xCreate/xCo
36130 6e 6e 65 63 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a nnect */.);../*.
36140 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 ** CAPI3REF: Reg
36150 69 73 74 65 72 20 41 20 56 69 72 74 75 61 6c 20 ister A Virtual
36160 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 Table Implementa
36170 74 69 6f 6e 20 7b 48 31 38 32 31 30 7d 20 3c 53 tion {H18210} <S
36180 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 20400>.** EXPERI
36190 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 MENTAL.**.** Thi
361a0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 64 65 s routine is ide
361b0 6e 74 69 63 61 6c 20 74 6f 20 74 68 65 20 5b 73 ntical to the [s
361c0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f qlite3_create_mo
361d0 64 75 6c 65 28 29 5d 20 6d 65 74 68 6f 64 2c 0a dule()] method,.
361e0 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69 ** except that i
361f0 74 20 68 61 73 20 61 6e 20 65 78 74 72 61 20 70 t has an extra p
36200 61 72 61 6d 65 74 65 72 20 74 6f 20 73 70 65 63 arameter to spec
36210 69 66 79 20 0a 2a 2a 20 61 20 64 65 73 74 72 75 ify .** a destru
36220 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f ctor function fo
36230 72 20 74 68 65 20 63 6c 69 65 6e 74 20 64 61 74 r the client dat
36240 61 20 70 6f 69 6e 74 65 72 2e 20 20 53 51 4c 69 a pointer. SQLi
36250 74 65 20 77 69 6c 6c 0a 2a 2a 20 69 6e 76 6f 6b te will.** invok
36260 65 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 e the destructor
36270 20 66 75 6e 63 74 69 6f 6e 20 28 69 66 20 69 74 function (if it
36280 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 29 20 77 68 is not NULL) wh
36290 65 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 6e 6f 20 en SQLite.** no
362a0 6c 6f 6e 67 65 72 20 6e 65 65 64 73 20 74 68 65 longer needs the
362b0 20 70 43 6c 69 65 6e 74 44 61 74 61 20 70 6f 69 pClientData poi
362c0 6e 74 65 72 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 nter. .*/.SQLIT
362d0 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 E_API SQLITE_EXP
362e0 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 ERIMENTAL int sq
362f0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 lite3_create_mod
36300 75 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 ule_v2(. sqlite
36310 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
36320 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 /* SQLite c
36330 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 onnection to reg
36340 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 ister module wit
36350 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 h */. const cha
36360 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 r *zName,
36370 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
36380 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e module */. con
36390 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c st sqlite3_modul
363a0 65 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f e *p, /* Metho
363b0 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c ds for the modul
363c0 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c e */. void *pCl
363d0 69 65 6e 74 44 61 74 61 2c 20 20 20 20 20 20 20 ientData,
363e0 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 /* Client data
363f0 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f for xCreate/xCo
36400 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 28 nnect */. void(
36410 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a *xDestroy)(void*
36420 29 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 ) /* Module
36430 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 destructor funct
36440 69 6f 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a ion */.);../*.**
36450 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 CAPI3REF: Virtu
36460 61 6c 20 54 61 62 6c 65 20 49 6e 73 74 61 6e 63 al Table Instanc
36470 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 31 30 e Object {H18010
36480 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 } <S20400>.** KE
36490 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f YWORDS: sqlite3_
364a0 76 74 61 62 0a 2a 2a 20 45 58 50 45 52 49 4d 45 vtab.** EXPERIME
364b0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 NTAL.**.** Every
364c0 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 [virtual table
364d0 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e module] implemen
364e0 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 tation uses a su
364f0 62 63 6c 61 73 73 0a 2a 2a 20 6f 66 20 74 68 65 bclass.** of the
36500 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
36510 74 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 65 ture to describe
36520 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e a particular in
36530 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 stance.** of the
36540 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d [virtual table]
36550 2e 20 20 45 61 63 68 20 73 75 62 63 6c 61 73 73 . Each subclass
36560 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 6c will.** be tail
36570 6f 72 65 64 20 74 6f 20 74 68 65 20 73 70 65 63 ored to the spec
36580 69 66 69 63 20 6e 65 65 64 73 20 6f 66 20 74 68 ific needs of th
36590 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 e module impleme
365a0 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 ntation..** The
365b0 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 purpose of this
365c0 73 75 70 65 72 63 6c 61 73 73 20 69 73 20 74 6f superclass is to
365d0 20 64 65 66 69 6e 65 20 63 65 72 74 61 69 6e 20 define certain
365e0 66 69 65 6c 64 73 20 74 68 61 74 20 61 72 65 0a fields that are.
365f0 2a 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c ** common to all
36600 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e module implemen
36610 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 tations..**.** V
36620 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 65 irtual tables me
36630 74 68 6f 64 73 20 63 61 6e 20 73 65 74 20 61 6e thods can set an
36640 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 error message b
36650 79 20 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a 2a y assigning a.**
36660 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 string obtained
36670 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d from [sqlite3_m
36680 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 7a 45 72 printf()] to zEr
36690 72 4d 73 67 2e 20 20 54 68 65 20 6d 65 74 68 6f rMsg. The metho
366a0 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 d should.** take
366b0 20 63 61 72 65 20 74 68 61 74 20 61 6e 79 20 70 care that any p
366c0 72 69 6f 72 20 73 74 72 69 6e 67 20 69 73 20 66 rior string is f
366d0 72 65 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 reed by a call t
366e0 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 o [sqlite3_free(
366f0 29 5d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61 )].** prior to a
36700 73 73 69 67 6e 69 6e 67 20 61 20 6e 65 77 20 73 ssigning a new s
36710 74 72 69 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67 tring to zErrMsg
36720 2e 20 20 41 66 74 65 72 20 74 68 65 20 65 72 72 . After the err
36730 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 or message.** is
36740 20 64 65 6c 69 76 65 72 65 64 20 75 70 20 74 6f delivered up to
36750 20 74 68 65 20 63 6c 69 65 6e 74 20 61 70 70 6c the client appl
36760 69 63 61 74 69 6f 6e 2c 20 74 68 65 20 73 74 72 ication, the str
36770 69 6e 67 20 77 69 6c 6c 20 62 65 20 61 75 74 6f ing will be auto
36780 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 matically.** fre
36790 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 66 72 ed by sqlite3_fr
367a0 65 65 28 29 20 61 6e 64 20 74 68 65 20 7a 45 72 ee() and the zEr
367b0 72 4d 73 67 20 66 69 65 6c 64 20 77 69 6c 6c 20 rMsg field will
367c0 62 65 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 73 74 be zeroed..*/.st
367d0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61 ruct sqlite3_vta
367e0 62 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 b {. const sqli
367f0 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 te3_module *pMod
36800 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64 ule; /* The mod
36810 75 6c 65 20 66 6f 72 20 74 68 69 73 20 76 69 72 ule for this vir
36820 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 tual table */.
36830 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 int nRef;
36840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
36850 2f 2a 20 4e 4f 20 4c 4f 4e 47 45 52 20 55 53 45 /* NO LONGER USE
36860 44 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 D */. char *zEr
36870 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 20 rMsg;
36880 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 /* Error
36890 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 73 71 6c message from sql
368a0 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a ite3_mprintf() *
368b0 2f 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 /. /* Virtual t
368c0 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 able implementat
368d0 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69 63 61 ions will typica
368e0 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e lly add addition
368f0 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a al fields */.};.
36900 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
36910 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 43 Virtual Table C
36920 75 72 73 6f 72 20 4f 62 6a 65 63 74 20 20 7b 48 ursor Object {H
36930 31 38 30 32 30 7d 20 3c 53 32 30 34 30 30 3e 0a 18020} <S20400>.
36940 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c ** KEYWORDS: sql
36950 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
36960 20 7b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 {virtual table
36970 63 75 72 73 6f 72 7d 0a 2a 2a 20 45 58 50 45 52 cursor}.** EXPER
36980 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 IMENTAL.**.** Ev
36990 65 72 79 20 5b 76 69 72 74 75 61 6c 20 74 61 62 ery [virtual tab
369a0 6c 65 20 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 le module] imple
369b0 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61 mentation uses a
369c0 20 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65 subclass of the
369d0 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 .** following st
369e0 72 75 63 74 75 72 65 20 74 6f 20 64 65 73 63 72 ructure to descr
369f0 69 62 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 ibe cursors that
36a00 20 70 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 0a point into the.
36a10 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c ** [virtual tabl
36a20 65 5d 20 61 6e 64 20 61 72 65 20 75 73 65 64 0a e] and are used.
36a30 2a 2a 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 ** to loop throu
36a40 67 68 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 gh the virtual t
36a50 61 62 6c 65 2e 20 20 43 75 72 73 6f 72 73 20 61 able. Cursors a
36a60 72 65 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 re created using
36a70 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 the.** [sqlite3
36a80 5f 6d 6f 64 75 6c 65 2e 78 4f 70 65 6e 20 7c 20 _module.xOpen |
36a90 78 4f 70 65 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 xOpen] method of
36aa0 20 74 68 65 20 6d 6f 64 75 6c 65 20 61 6e 64 20 the module and
36ab0 61 72 65 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a are destroyed.**
36ac0 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 by the [sqlite3
36ad0 5f 6d 6f 64 75 6c 65 2e 78 43 6c 6f 73 65 20 7c _module.xClose |
36ae0 20 78 43 6c 6f 73 65 5d 20 6d 65 74 68 6f 64 2e xClose] method.
36af0 20 20 43 75 73 73 6f 72 73 20 61 72 65 20 75 73 Cussors are us
36b00 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 5b 78 46 ed.** by the [xF
36b10 69 6c 74 65 72 5d 2c 20 5b 78 4e 65 78 74 5d 2c ilter], [xNext],
36b20 20 5b 78 45 6f 66 5d 2c 20 5b 78 43 6f 6c 75 6d [xEof], [xColum
36b30 6e 5d 2c 20 61 6e 64 20 5b 78 52 6f 77 69 64 5d n], and [xRowid]
36b40 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 methods.** of t
36b50 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 68 he module. Each
36b60 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e module implemen
36b70 74 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 69 tation will defi
36b80 6e 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e ne.** the conten
36b90 74 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73 74 t of a cursor st
36ba0 72 75 63 74 75 72 65 20 74 6f 20 73 75 69 74 20 ructure to suit
36bb0 69 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a its own needs..*
36bc0 2a 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72 63 *.** This superc
36bd0 6c 61 73 73 20 65 78 69 73 74 73 20 69 6e 20 6f lass exists in o
36be0 72 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20 66 rder to define f
36bf0 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72 ields of the cur
36c00 73 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 sor that.** are
36c10 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d common to all im
36c20 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a plementations..*
36c30 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 /.struct sqlite3
36c40 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 7b 0a 20 _vtab_cursor {.
36c50 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
36c60 56 74 61 62 3b 20 20 20 20 20 20 2f 2a 20 56 69 Vtab; /* Vi
36c70 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 66 20 74 rtual table of t
36c80 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 his cursor */.
36c90 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 /* Virtual table
36ca0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
36cb0 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 will typically
36cc0 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 add additional f
36cd0 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ields */.};../*.
36ce0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 63 ** CAPI3REF: Dec
36cf0 6c 61 72 65 20 54 68 65 20 53 63 68 65 6d 61 20 lare The Schema
36d00 4f 66 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 Of A Virtual Tab
36d10 6c 65 20 7b 48 31 38 32 38 30 7d 20 3c 53 32 30 le {H18280} <S20
36d20 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 400>.** EXPERIME
36d30 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b NTAL.**.** The [
36d40 78 43 72 65 61 74 65 5d 20 61 6e 64 20 5b 78 43 xCreate] and [xC
36d50 6f 6e 6e 65 63 74 5d 20 6d 65 74 68 6f 64 73 20 onnect] methods
36d60 6f 66 20 61 0a 2a 2a 20 5b 76 69 72 74 75 61 6c of a.** [virtual
36d70 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 63 table module] c
36d80 61 6c 6c 20 74 68 69 73 20 69 6e 74 65 72 66 61 all this interfa
36d90 63 65 0a 2a 2a 20 74 6f 20 64 65 63 6c 61 72 65 ce.** to declare
36da0 20 74 68 65 20 66 6f 72 6d 61 74 20 28 74 68 65 the format (the
36db0 20 6e 61 6d 65 73 20 61 6e 64 20 64 61 74 61 74 names and datat
36dc0 79 70 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 ypes of the colu
36dd0 6d 6e 73 29 20 6f 66 0a 2a 2a 20 74 68 65 20 76 mns) of.** the v
36de0 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74 68 irtual tables th
36df0 65 79 20 69 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f ey implement..*/
36e00 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
36e10 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 TE_EXPERIMENTAL
36e20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c int sqlite3_decl
36e30 61 72 65 5f 76 74 61 62 28 73 71 6c 69 74 65 33 are_vtab(sqlite3
36e40 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a *, const char *z
36e50 53 51 4c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 SQL);../*.** CAP
36e60 49 33 52 45 46 3a 20 4f 76 65 72 6c 6f 61 64 20 I3REF: Overload
36e70 41 20 46 75 6e 63 74 69 6f 6e 20 46 6f 72 20 41 A Function For A
36e80 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b Virtual Table {
36e90 48 31 38 33 30 30 7d 20 3c 53 32 30 34 30 30 3e H18300} <S20400>
36ea0 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
36eb0 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 .**.** Virtual t
36ec0 61 62 6c 65 73 20 63 61 6e 20 70 72 6f 76 69 64 ables can provid
36ed0 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69 6d e alternative im
36ee0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 plementations of
36ef0 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 75 73 functions.** us
36f00 69 6e 67 20 74 68 65 20 5b 78 46 69 6e 64 46 75 ing the [xFindFu
36f10 6e 63 74 69 6f 6e 5d 20 6d 65 74 68 6f 64 20 6f nction] method o
36f20 66 20 74 68 65 20 5b 76 69 72 74 75 61 6c 20 74 f the [virtual t
36f30 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 0a able module]. .
36f40 2a 2a 20 42 75 74 20 67 6c 6f 62 61 6c 20 76 65 ** But global ve
36f50 72 73 69 6f 6e 73 20 6f 66 20 74 68 6f 73 65 20 rsions of those
36f60 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73 functions.** mus
36f70 74 20 65 78 69 73 74 20 69 6e 20 6f 72 64 65 72 t exist in order
36f80 20 74 6f 20 62 65 20 6f 76 65 72 6c 6f 61 64 65 to be overloade
36f90 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 d..**.** This AP
36fa0 49 20 6d 61 6b 65 73 20 73 75 72 65 20 61 20 67 I makes sure a g
36fb0 6c 6f 62 61 6c 20 76 65 72 73 69 6f 6e 20 6f 66 lobal version of
36fc0 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 a function with
36fd0 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a a particular.**
36fe0 20 6e 61 6d 65 20 61 6e 64 20 6e 75 6d 62 65 72 name and number
36ff0 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 65 of parameters e
37000 78 69 73 74 73 2e 20 20 49 66 20 6e 6f 20 73 75 xists. If no su
37010 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 ch function exis
37020 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 69 ts.** before thi
37030 73 20 41 50 49 20 69 73 20 63 61 6c 6c 65 64 2c s API is called,
37040 20 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 a new function
37050 69 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 is created. The
37060 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a implementation.
37070 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20 66 75 ** of the new fu
37080 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 63 61 nction always ca
37090 75 73 65 73 20 61 6e 20 65 78 63 65 70 74 69 6f uses an exceptio
370a0 6e 20 74 6f 20 62 65 20 74 68 72 6f 77 6e 2e 20 n to be thrown.
370b0 20 53 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 66 So.** the new f
370c0 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67 unction is not g
370d0 6f 6f 64 20 66 6f 72 20 61 6e 79 74 68 69 6e 67 ood for anything
370e0 20 62 79 20 69 74 73 65 6c 66 2e 20 20 49 74 73 by itself. Its
370f0 20 6f 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f 73 65 only.** purpose
37100 20 69 73 20 74 6f 20 62 65 20 61 20 70 6c 61 63 is to be a plac
37110 65 68 6f 6c 64 65 72 20 66 75 6e 63 74 69 6f 6e eholder function
37120 20 74 68 61 74 20 63 61 6e 20 62 65 20 6f 76 65 that can be ove
37130 72 6c 6f 61 64 65 64 0a 2a 2a 20 62 79 20 61 20 rloaded.** by a
37140 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e [virtual table].
37150 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 .*/.SQLITE_API S
37160 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 QLITE_EXPERIMENT
37170 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f AL int sqlite3_o
37180 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e verload_function
37190 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 (sqlite3*, const
371a0 20 63 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 char *zFuncName
371b0 2c 20 69 6e 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a , int nArg);../*
371c0 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 .** The interfac
371d0 65 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c e to the virtual
371e0 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d -table mechanism
371f0 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 20 28 defined above (
37200 62 61 63 6b 20 75 70 0a 2a 2a 20 74 6f 20 61 20 back up.** to a
37210 63 6f 6d 6d 65 6e 74 20 72 65 6d 61 72 6b 61 62 comment remarkab
37220 6c 79 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 ly similar to th
37230 69 73 20 6f 6e 65 29 20 69 73 20 63 75 72 72 65 is one) is curre
37240 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a ntly considered.
37250 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d ** to be experim
37260 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65 ental. The inte
37270 72 66 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e rface might chan
37280 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62 ge in incompatib
37290 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74 le ways..** If t
372a0 68 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d his is a problem
372b0 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 for you, do not
372c0 20 75 73 65 20 74 68 65 20 69 6e 74 65 72 66 61 use the interfa
372d0 63 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e ce at this time.
372e0 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 .**.** When the
372f0 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 virtual-table me
37300 63 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a chanism stabiliz
37310 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c es, we will decl
37320 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 are the.** inter
37330 66 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 70 face fixed, supp
37340 6f 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74 ort it indefinit
37350 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20 ely, and remove
37360 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a this comment..**
37370 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45 .****** EXPERIME
37380 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74 NTAL - subject t
37390 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 o change without
373a0 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a notice ********
373b0 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a ******.*/../*.**
373c0 20 43 41 50 49 33 52 45 46 3a 20 41 20 48 61 6e CAPI3REF: A Han
373d0 64 6c 65 20 54 6f 20 41 6e 20 4f 70 65 6e 20 42 dle To An Open B
373e0 4c 4f 42 20 7b 48 31 37 38 30 30 7d 20 3c 53 33 LOB {H17800} <S3
373f0 30 32 33 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0230>.** KEYWORD
37400 53 3a 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 7d S: {BLOB handle}
37410 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 7d 0a {BLOB handles}.
37420 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 **.** An instanc
37430 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 e of this object
37440 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 6f represents an o
37450 70 65 6e 20 42 4c 4f 42 20 6f 6e 20 77 68 69 63 pen BLOB on whic
37460 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c h.** [sqlite3_bl
37470 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d ob_open | increm
37480 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 ental BLOB I/O]
37490 63 61 6e 20 62 65 20 70 65 72 66 6f 72 6d 65 64 can be performed
374a0 2e 0a 2a 2a 20 4f 62 6a 65 63 74 73 20 6f 66 20 ..** Objects of
374b0 74 68 69 73 20 74 79 70 65 20 61 72 65 20 63 72 this type are cr
374c0 65 61 74 65 64 20 62 79 20 5b 73 71 6c 69 74 65 eated by [sqlite
374d0 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a 3_blob_open()].*
374e0 2a 20 61 6e 64 20 64 65 73 74 72 6f 79 65 64 20 * and destroyed
374f0 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 by [sqlite3_blob
37500 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 20 54 68 _close()]..** Th
37510 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f e [sqlite3_blob_
37520 72 65 61 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c read()] and [sql
37530 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 ite3_blob_write(
37540 29 5d 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a )] interfaces.**
37550 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 can be used to
37560 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 73 6d read or write sm
37570 61 6c 6c 20 73 75 62 73 65 63 74 69 6f 6e 73 20 all subsections
37580 6f 66 20 74 68 65 20 42 4c 4f 42 2e 0a 2a 2a 20 of the BLOB..**
37590 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f The [sqlite3_blo
375a0 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 b_bytes()] inter
375b0 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 face returns the
375c0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 42 4c 4f size of the BLO
375d0 42 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 74 B in bytes..*/.t
375e0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 ypedef struct sq
375f0 6c 69 74 65 33 5f 62 6c 6f 62 20 73 71 6c 69 74 lite3_blob sqlit
37600 65 33 5f 62 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20 e3_blob;../*.**
37610 43 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 20 41 CAPI3REF: Open A
37620 20 42 4c 4f 42 20 46 6f 72 20 49 6e 63 72 65 6d BLOB For Increm
37630 65 6e 74 61 6c 20 49 2f 4f 20 7b 48 31 37 38 31 ental I/O {H1781
37640 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 0} <S30230>.**.*
37650 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 * This interface
37660 73 20 6f 70 65 6e 73 20 61 20 5b 42 4c 4f 42 20 s opens a [BLOB
37670 68 61 6e 64 6c 65 20 7c 20 68 61 6e 64 6c 65 5d handle | handle]
37680 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 6c 6f 63 to the BLOB loc
37690 61 74 65 64 0a 2a 2a 20 69 6e 20 72 6f 77 20 69 ated.** in row i
376a0 52 6f 77 2c 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c Row, column zCol
376b0 75 6d 6e 2c 20 74 61 62 6c 65 20 7a 54 61 62 6c umn, table zTabl
376c0 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 7a 44 e in database zD
376d0 62 3b 0a 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77 b;.** in other w
376e0 6f 72 64 73 2c 20 74 68 65 20 73 61 6d 65 20 42 ords, the same B
376f0 4c 4f 42 20 74 68 61 74 20 77 6f 75 6c 64 20 62 LOB that would b
37700 65 20 73 65 6c 65 63 74 65 64 20 62 79 3a 0a 2a e selected by:.*
37710 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 *.** <pre>.**
37720 20 20 53 45 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e SELECT zColumn
37730 20 46 52 4f 4d 20 7a 44 62 2e 7a 54 61 62 6c 65 FROM zDb.zTable
37740 20 57 48 45 52 45 20 5b 72 6f 77 69 64 5d 20 3d WHERE [rowid] =
37750 20 69 52 6f 77 3b 0a 2a 2a 20 3c 2f 70 72 65 3e iRow;.** </pre>
37760 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 {END}.**.** If
37770 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 the flags parame
37780 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c ter is non-zero,
37790 20 74 68 65 6e 20 74 68 65 20 42 4c 4f 42 20 69 then the BLOB i
377a0 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 s opened for rea
377b0 64 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 61 d.** and write a
377c0 63 63 65 73 73 2e 20 49 66 20 69 74 20 69 73 20 ccess. If it is
377d0 7a 65 72 6f 2c 20 74 68 65 20 42 4c 4f 42 20 69 zero, the BLOB i
377e0 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 s opened for rea
377f0 64 20 61 63 63 65 73 73 2e 0a 2a 2a 20 49 74 20 d access..** It
37800 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
37810 74 6f 20 6f 70 65 6e 20 61 20 63 6f 6c 75 6d 6e to open a column
37820 20 74 68 61 74 20 69 73 20 70 61 72 74 20 6f 66 that is part of
37830 20 61 6e 20 69 6e 64 65 78 20 6f 72 20 70 72 69 an index or pri
37840 6d 61 72 79 20 0a 2a 2a 20 6b 65 79 20 66 6f 72 mary .** key for
37850 20 77 72 69 74 69 6e 67 2e 20 5e 49 66 20 5b 66 writing. ^If [f
37860 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 oreign key const
37870 72 61 69 6e 74 73 5d 20 61 72 65 20 65 6e 61 62 raints] are enab
37880 6c 65 64 2c 20 69 74 20 69 73 20 0a 2a 2a 20 6e led, it is .** n
37890 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6f ot possible to o
378a0 70 65 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 pen a column tha
378b0 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 5b t is part of a [
378c0 63 68 69 6c 64 20 6b 65 79 5d 20 66 6f 72 20 77 child key] for w
378d0 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 4e 6f riting..**.** No
378e0 74 65 20 74 68 61 74 20 74 68 65 20 64 61 74 61 te that the data
378f0 62 61 73 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 base name is not
37900 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 68 the filename th
37910 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 at contains.** t
37920 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20 he database but
37930 72 61 74 68 65 72 20 74 68 65 20 73 79 6d 62 6f rather the symbo
37940 6c 69 63 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 lic name of the
37950 64 61 74 61 62 61 73 65 20 74 68 61 74 0a 2a 2a database that.**
37960 20 69 73 20 61 73 73 69 67 6e 65 64 20 77 68 65 is assigned whe
37970 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 n the database i
37980 73 20 63 6f 6e 6e 65 63 74 65 64 20 75 73 69 6e s connected usin
37990 67 20 5b 41 54 54 41 43 48 5d 2e 0a 2a 2a 20 46 g [ATTACH]..** F
379a0 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 or the main data
379b0 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 20 64 base file, the d
379c0 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 atabase name is
379d0 22 6d 61 69 6e 22 2e 0a 2a 2a 20 46 6f 72 20 54 "main"..** For T
379e0 45 4d 50 20 74 61 62 6c 65 73 2c 20 74 68 65 20 EMP tables, the
379f0 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 database name is
37a00 20 22 74 65 6d 70 22 2e 0a 2a 2a 0a 2a 2a 20 4f "temp"..**.** O
37a10 6e 20 73 75 63 63 65 73 73 2c 20 5b 53 51 4c 49 n success, [SQLI
37a20 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e TE_OK] is return
37a30 65 64 20 61 6e 64 20 74 68 65 20 6e 65 77 20 5b ed and the new [
37a40 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 73 20 BLOB handle] is
37a50 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 written.** to *p
37a60 70 42 6c 6f 62 2e 20 4f 74 68 65 72 77 69 73 65 pBlob. Otherwise
37a70 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d an [error code]
37a80 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 is returned and
37a90 20 2a 70 70 42 6c 6f 62 20 69 73 20 73 65 74 0a *ppBlob is set.
37aa0 2a 2a 20 74 6f 20 62 65 20 61 20 6e 75 6c 6c 20 ** to be a null
37ab0 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 54 68 69 73 pointer..** This
37ac0 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74 function sets t
37ad0 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
37ae0 6e 65 63 74 69 6f 6e 5d 20 65 72 72 6f 72 20 63 nection] error c
37af0 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65 0a ode and message.
37b00 2a 2a 20 61 63 63 65 73 73 69 62 6c 65 20 76 69 ** accessible vi
37b10 61 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f a [sqlite3_errco
37b20 64 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 de()] and [sqlit
37b30 65 33 5f 65 72 72 6d 73 67 28 29 5d 20 61 6e 64 e3_errmsg()] and
37b40 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63 related.** func
37b50 74 69 6f 6e 73 2e 20 20 4e 6f 74 65 20 74 68 61 tions. Note tha
37b60 74 20 74 68 65 20 2a 70 70 42 6c 6f 62 20 76 61 t the *ppBlob va
37b70 72 69 61 62 6c 65 20 69 73 20 61 6c 77 61 79 73 riable is always
37b80 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 6e 20 initialized in
37b90 61 0a 2a 2a 20 77 61 79 20 74 68 61 74 20 6d 61 a.** way that ma
37ba0 6b 65 73 20 69 74 20 73 61 66 65 20 74 6f 20 69 kes it safe to i
37bb0 6e 76 6f 6b 65 20 5b 73 71 6c 69 74 65 33 5f 62 nvoke [sqlite3_b
37bc0 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 20 6f 6e 20 lob_close()] on
37bd0 2a 70 70 42 6c 6f 62 0a 2a 2a 20 72 65 67 61 72 *ppBlob.** regar
37be0 64 6c 65 73 73 20 6f 66 20 74 68 65 20 73 75 63 dless of the suc
37bf0 63 65 73 73 20 6f 72 20 66 61 69 6c 75 72 65 20 cess or failure
37c00 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e of this routine.
37c10 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f .**.** If the ro
37c20 77 20 74 68 61 74 20 61 20 42 4c 4f 42 20 68 61 w that a BLOB ha
37c30 6e 64 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 ndle points to i
37c40 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e s modified by an
37c50 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 2c 20 5b 44 .** [UPDATE], [D
37c60 45 4c 45 54 45 5d 2c 20 6f 72 20 62 79 20 5b 4f ELETE], or by [O
37c70 4e 20 43 4f 4e 46 4c 49 43 54 5d 20 73 69 64 65 N CONFLICT] side
37c80 2d 65 66 66 65 63 74 73 0a 2a 2a 20 74 68 65 6e -effects.** then
37c90 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 the BLOB handle
37ca0 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65 is marked as "e
37cb0 78 70 69 72 65 64 22 2e 0a 2a 2a 20 54 68 69 73 xpired"..** This
37cc0 20 69 73 20 74 72 75 65 20 69 66 20 61 6e 79 20 is true if any
37cd0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 6f column of the ro
37ce0 77 20 69 73 20 63 68 61 6e 67 65 64 2c 20 65 76 w is changed, ev
37cf0 65 6e 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f en a column.** o
37d00 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e ther than the on
37d10 65 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c e the BLOB handl
37d20 65 20 69 73 20 6f 70 65 6e 20 6f 6e 2e 0a 2a 2a e is open on..**
37d30 20 43 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 Calls to [sqlit
37d40 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20 e3_blob_read()]
37d50 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f and [sqlite3_blo
37d60 62 5f 77 72 69 74 65 28 29 5d 20 66 6f 72 0a 2a b_write()] for.*
37d70 2a 20 61 20 65 78 70 69 72 65 64 20 42 4c 4f 42 * a expired BLOB
37d80 20 68 61 6e 64 6c 65 20 66 61 69 6c 20 77 69 74 handle fail wit
37d90 68 20 61 6e 20 72 65 74 75 72 6e 20 63 6f 64 65 h an return code
37da0 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 of [SQLITE_ABOR
37db0 54 5d 2e 0a 2a 2a 20 43 68 61 6e 67 65 73 20 77 T]..** Changes w
37dc0 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 42 4c ritten into a BL
37dd0 4f 42 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 OB prior to the
37de0 42 4c 4f 42 20 65 78 70 69 72 69 6e 67 20 61 72 BLOB expiring ar
37df0 65 20 6e 6f 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 e not.** rollbac
37e00 6b 20 62 79 20 74 68 65 20 65 78 70 69 72 61 74 k by the expirat
37e10 69 6f 6e 20 6f 66 20 74 68 65 20 42 4c 4f 42 2e ion of the BLOB.
37e20 20 20 53 75 63 68 20 63 68 61 6e 67 65 73 20 77 Such changes w
37e30 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a ill eventually.*
37e40 2a 20 63 6f 6d 6d 69 74 20 69 66 20 74 68 65 20 * commit if the
37e50 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6e 74 transaction cont
37e60 69 6e 75 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 inues to complet
37e70 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 ion..**.** Use t
37e80 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 he [sqlite3_blob
37e90 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66 _bytes()] interf
37ea0 61 63 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 ace to determine
37eb0 20 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 the size of.**
37ec0 74 68 65 20 6f 70 65 6e 65 64 20 62 6c 6f 62 2e the opened blob.
37ed0 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 61 20 The size of a
37ee0 62 6c 6f 62 20 6d 61 79 20 6e 6f 74 20 62 65 20 blob may not be
37ef0 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 0a changed by this.
37f00 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 20 20 55 ** interface. U
37f10 73 65 20 74 68 65 20 5b 55 50 44 41 54 45 5d 20 se the [UPDATE]
37f20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 63 SQL command to c
37f30 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f hange the size o
37f40 66 20 61 0a 2a 2a 20 62 6c 6f 62 2e 0a 2a 2a 0a f a.** blob..**.
37f50 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f ** The [sqlite3_
37f60 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d bind_zeroblob()]
37f70 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 and [sqlite3_re
37f80 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d sult_zeroblob()]
37f90 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 61 interfaces.** a
37fa0 6e 64 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 nd the built-in
37fb0 5b 7a 65 72 6f 62 6c 6f 62 5d 20 53 51 4c 20 66 [zeroblob] SQL f
37fc0 75 6e 63 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 unction can be u
37fd0 73 65 64 2c 20 69 66 20 64 65 73 69 72 65 64 2c sed, if desired,
37fe0 0a 2a 2a 20 74 6f 20 63 72 65 61 74 65 20 61 6e .** to create an
37ff0 20 65 6d 70 74 79 2c 20 7a 65 72 6f 2d 66 69 6c empty, zero-fil
38000 6c 65 64 20 62 6c 6f 62 20 69 6e 20 77 68 69 63 led blob in whic
38010 68 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 h to read or wri
38020 74 65 20 75 73 69 6e 67 0a 2a 2a 20 74 68 69 73 te using.** this
38030 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a interface..**.*
38040 2a 20 54 6f 20 61 76 6f 69 64 20 61 20 72 65 73 * To avoid a res
38050 6f 75 72 63 65 20 6c 65 61 6b 2c 20 65 76 65 72 ource leak, ever
38060 79 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e y open [BLOB han
38070 64 6c 65 5d 20 73 68 6f 75 6c 64 20 65 76 65 6e dle] should even
38080 74 75 61 6c 6c 79 0a 2a 2a 20 62 65 20 72 65 6c tually.** be rel
38090 65 61 73 65 64 20 62 79 20 61 20 63 61 6c 6c 20 eased by a call
380a0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 to [sqlite3_blob
380b0 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a _close()]..**.**
380c0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
380d0 2a 20 5b 48 31 37 38 31 33 5d 20 5b 48 31 37 38 * [H17813] [H178
380e0 31 34 5d 20 5b 48 31 37 38 31 36 5d 20 5b 48 31 14] [H17816] [H1
380f0 37 38 31 39 5d 20 5b 48 31 37 38 32 31 5d 20 5b 7819] [H17821] [
38100 48 31 37 38 32 34 5d 0a 2a 2f 0a 53 51 4c 49 54 H17824].*/.SQLIT
38110 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
38120 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 3_blob_open(. s
38130 71 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e 73 74 qlite3*,. const
38140 20 63 68 61 72 20 2a 7a 44 62 2c 0a 20 20 63 6f char *zDb,. co
38150 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 nst char *zTable
38160 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ,. const char *
38170 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c 69 74 zColumn,. sqlit
38180 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c 0a 20 e3_int64 iRow,.
38190 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 73 71 int flags,. sq
381a0 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 lite3_blob **ppB
381b0 6c 6f 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 lob.);../*.** CA
381c0 50 49 33 52 45 46 3a 20 43 6c 6f 73 65 20 41 20 PI3REF: Close A
381d0 42 4c 4f 42 20 48 61 6e 64 6c 65 20 7b 48 31 37 BLOB Handle {H17
381e0 38 33 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 830} <S30230>.**
381f0 0a 2a 2a 20 43 6c 6f 73 65 73 20 61 6e 20 6f 70 .** Closes an op
38200 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d en [BLOB handle]
38210 2e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 ..**.** Closing
38220 61 20 42 4c 4f 42 20 73 68 61 6c 6c 20 63 61 75 a BLOB shall cau
38230 73 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 se the current t
38240 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f ransaction to co
38250 6d 6d 69 74 0a 2a 2a 20 69 66 20 74 68 65 72 65 mmit.** if there
38260 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 42 4c are no other BL
38270 4f 42 73 2c 20 6e 6f 20 70 65 6e 64 69 6e 67 20 OBs, no pending
38280 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
38290 6e 74 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20 nts, and the.**
382a0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
382b0 69 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74 6f 63 ion is in [autoc
382c0 6f 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a 2a 20 ommit mode]..**
382d0 49 66 20 61 6e 79 20 77 72 69 74 65 73 20 77 65 If any writes we
382e0 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 42 re made to the B
382f0 4c 4f 42 2c 20 74 68 65 79 20 6d 69 67 68 74 20 LOB, they might
38300 62 65 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 be held in cache
38310 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 6c .** until the cl
38320 6f 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 66 ose operation if
38330 20 74 68 65 79 20 77 69 6c 6c 20 66 69 74 2e 0a they will fit..
38340 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 74 68 **.** Closing th
38350 65 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66 6f 72 e BLOB often for
38360 63 65 73 20 74 68 65 20 63 68 61 6e 67 65 73 0a ces the changes.
38370 2a 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b 20 61 ** out to disk a
38380 6e 64 20 73 6f 20 69 66 20 61 6e 79 20 49 2f 4f nd so if any I/O
38390 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 74 errors occur, t
383a0 68 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 hey will likely
383b0 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 20 occur.** at the
383c0 74 69 6d 65 20 77 68 65 6e 20 74 68 65 20 42 4c time when the BL
383d0 4f 42 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 41 OB is closed. A
383e0 6e 79 20 65 72 72 6f 72 73 20 74 68 61 74 20 6f ny errors that o
383f0 63 63 75 72 20 64 75 72 69 6e 67 0a 2a 2a 20 63 ccur during.** c
38400 6c 6f 73 69 6e 67 20 61 72 65 20 72 65 70 6f 72 losing are repor
38410 74 65 64 20 61 73 20 61 20 6e 6f 6e 2d 7a 65 72 ted as a non-zer
38420 6f 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e 0a o return value..
38430 2a 2a 0a 2a 2a 20 54 68 65 20 42 4c 4f 42 20 69 **.** The BLOB i
38440 73 20 63 6c 6f 73 65 64 20 75 6e 63 6f 6e 64 69 s closed uncondi
38450 74 69 6f 6e 61 6c 6c 79 2e 20 20 45 76 65 6e 20 tionally. Even
38460 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 if this routine
38470 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 20 65 72 returns.** an er
38480 72 6f 72 20 63 6f 64 65 2c 20 74 68 65 20 42 4c ror code, the BL
38490 4f 42 20 69 73 20 73 74 69 6c 6c 20 63 6c 6f 73 OB is still clos
384a0 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e ed..**.** Callin
384b0 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 g this routine w
384c0 69 74 68 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 ith a null point
384d0 65 72 20 28 77 68 69 63 68 20 61 73 20 77 6f 75 er (which as wou
384e0 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 0a 2a ld be returned.*
384f0 2a 20 62 79 20 66 61 69 6c 65 64 20 63 61 6c 6c * by failed call
38500 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f to [sqlite3_blo
38510 62 5f 6f 70 65 6e 28 29 5d 29 20 69 73 20 61 20 b_open()]) is a
38520 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a harmless no-op..
38530 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
38540 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 33 33 5d 20 ts:.** [H17833]
38550 5b 48 31 37 38 33 36 5d 20 5b 48 31 37 38 33 39 [H17836] [H17839
38560 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
38570 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 int sqlite3_blob
38580 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 62 _close(sqlite3_b
38590 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 lob *);../*.** C
385a0 41 50 49 33 52 45 46 3a 20 52 65 74 75 72 6e 20 API3REF: Return
385b0 54 68 65 20 53 69 7a 65 20 4f 66 20 41 6e 20 4f The Size Of An O
385c0 70 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38 34 30 pen BLOB {H17840
385d0 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a } <S30230>.**.**
385e0 20 52 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a Returns the siz
385f0 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 e in bytes of th
38600 65 20 42 4c 4f 42 20 61 63 63 65 73 73 69 62 6c e BLOB accessibl
38610 65 20 76 69 61 20 74 68 65 20 0a 2a 2a 20 73 75 e via the .** su
38620 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 ccessfully opene
38630 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 d [BLOB handle]
38640 69 6e 20 69 74 73 20 6f 6e 6c 79 20 61 72 67 75 in its only argu
38650 6d 65 6e 74 2e 20 20 54 68 65 0a 2a 2a 20 69 6e ment. The.** in
38660 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 49 cremental blob I
38670 2f 4f 20 72 6f 75 74 69 6e 65 73 20 63 61 6e 20 /O routines can
38680 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20 6f 76 65 only read or ove
38690 72 77 72 69 74 69 6e 67 20 65 78 69 73 74 69 6e rwriting existin
386a0 67 0a 2a 2a 20 62 6c 6f 62 20 63 6f 6e 74 65 6e g.** blob conten
386b0 74 3b 20 74 68 65 79 20 63 61 6e 6e 6f 74 20 63 t; they cannot c
386c0 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f hange the size o
386d0 66 20 61 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 f a blob..**.**
386e0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c This routine onl
386f0 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c y works on a [BL
38700 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 OB handle] which
38710 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 has been create
38720 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20 d.** by a prior
38730 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 successful call
38740 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 to [sqlite3_blob
38750 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 _open()] and whi
38760 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 ch has not.** be
38770 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 en closed by [sq
38780 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 lite3_blob_close
38790 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e ()]. Passing an
387a0 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20 y other pointer
387b0 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f in.** to this ro
387c0 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e utine results in
387d0 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70 undefined and p
387e0 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61 robably undesira
387f0 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a ble behavior..**
38800 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
38810 3a 0a 2a 2a 20 5b 48 31 37 38 34 33 5d 0a 2a 2f :.** [H17843].*/
38820 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
38830 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 sqlite3_blob_byt
38840 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 es(sqlite3_blob
38850 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 *);../*.** CAPI3
38860 52 45 46 3a 20 52 65 61 64 20 44 61 74 61 20 46 REF: Read Data F
38870 72 6f 6d 20 41 20 42 4c 4f 42 20 49 6e 63 72 65 rom A BLOB Incre
38880 6d 65 6e 74 61 6c 6c 79 20 7b 48 31 37 38 35 30 mentally {H17850
38890 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a } <S30230>.**.**
388a0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
388b0 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 64 s used to read d
388c0 61 74 61 20 66 72 6f 6d 20 61 6e 20 6f 70 65 6e ata from an open
388d0 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 [BLOB handle] i
388e0 6e 74 6f 20 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d nto a.** caller-
388f0 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2e supplied buffer.
38900 20 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 N bytes of data
38910 20 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f are copied into
38920 20 62 75 66 66 65 72 20 5a 0a 2a 2a 20 66 72 6f buffer Z.** fro
38930 6d 20 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c m the open BLOB,
38940 20 73 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66 starting at off
38950 73 65 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a set iOffset..**.
38960 2a 2a 20 49 66 20 6f 66 66 73 65 74 20 69 4f 66 ** If offset iOf
38970 66 73 65 74 20 69 73 20 6c 65 73 73 20 74 68 61 fset is less tha
38980 6e 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 n N bytes from t
38990 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 42 4c he end of the BL
389a0 4f 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 OB,.** [SQLITE_E
389b0 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 RROR] is returne
389c0 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 d and no data is
389d0 20 72 65 61 64 2e 20 20 49 66 20 4e 20 6f 72 20 read. If N or
389e0 69 4f 66 66 73 65 74 20 69 73 0a 2a 2a 20 6c 65 iOffset is.** le
389f0 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 5b 53 ss than zero, [S
38a00 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 QLITE_ERROR] is
38a10 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 returned and no
38a20 64 61 74 61 20 69 73 20 72 65 61 64 2e 0a 2a 2a data is read..**
38a30 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 The size of the
38a40 20 62 6c 6f 62 20 28 61 6e 64 20 68 65 6e 63 65 blob (and hence
38a50 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c the maximum val
38a60 75 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65 74 29 ue of N+iOffset)
38a70 0a 2a 2a 20 63 61 6e 20 62 65 20 64 65 74 65 72 .** can be deter
38a80 6d 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 20 mined using the
38a90 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 [sqlite3_blob_by
38aa0 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 tes()] interface
38ab0 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 74 65 6d ..**.** An attem
38ac0 70 74 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 pt to read from
38ad0 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c 4f 42 an expired [BLOB
38ae0 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73 20 77 handle] fails w
38af0 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 ith an.** error
38b00 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f code of [SQLITE_
38b10 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 4f 6e ABORT]..**.** On
38b20 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54 45 success, SQLITE
38b30 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e _OK is returned.
38b40 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 .** Otherwise, a
38b50 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f n [error code] o
38b60 72 20 61 6e 20 5b 65 78 74 65 6e 64 65 64 20 65 r an [extended e
38b70 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 rror code] is re
38b80 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 turned..**.** Th
38b90 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 is routine only
38ba0 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42 works on a [BLOB
38bb0 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 68 handle] which h
38bc0 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 0a as been created.
38bd0 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20 73 75 ** by a prior su
38be0 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f ccessful call to
38bf0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f [sqlite3_blob_o
38c00 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 68 pen()] and which
38c10 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e has not.** been
38c20 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c 69 closed by [sqli
38c30 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 te3_blob_close()
38c40 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e 79 20 ]. Passing any
38c50 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20 69 6e other pointer in
38c60 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f 75 74 .** to this rout
38c70 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e 20 75 ine results in u
38c80 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70 72 6f ndefined and pro
38c90 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61 62 6c bably undesirabl
38ca0 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a e behavior..**.*
38cb0 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c * See also: [sql
38cc0 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 ite3_blob_write(
38cd0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 )]..**.** Requir
38ce0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 ements:.** [H178
38cf0 35 33 5d 20 5b 48 31 37 38 35 36 5d 20 5b 48 31 53] [H17856] [H1
38d00 37 38 35 39 5d 20 5b 48 31 37 38 36 32 5d 20 5b 7859] [H17862] [
38d10 48 31 37 38 36 33 5d 20 5b 48 31 37 38 36 35 5d H17863] [H17865]
38d20 20 5b 48 31 37 38 36 38 5d 0a 2a 2f 0a 53 51 4c [H17868].*/.SQL
38d30 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
38d40 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71 te3_blob_read(sq
38d50 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 76 6f lite3_blob *, vo
38d60 69 64 20 2a 5a 2c 20 69 6e 74 20 4e 2c 20 69 6e id *Z, int N, in
38d70 74 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a t iOffset);../*.
38d80 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 57 72 69 ** CAPI3REF: Wri
38d90 74 65 20 44 61 74 61 20 49 6e 74 6f 20 41 20 42 te Data Into A B
38da0 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c LOB Incrementall
38db0 79 20 7b 48 31 37 38 37 30 7d 20 3c 53 33 30 32 y {H17870} <S302
38dc0 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 30>.**.** This f
38dd0 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 unction is used
38de0 74 6f 20 77 72 69 74 65 20 64 61 74 61 20 69 6e to write data in
38df0 74 6f 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 to an open [BLOB
38e00 20 68 61 6e 64 6c 65 5d 20 66 72 6f 6d 20 61 0a handle] from a.
38e10 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c 69 ** caller-suppli
38e20 65 64 20 62 75 66 66 65 72 2e 20 4e 20 62 79 74 ed buffer. N byt
38e30 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 es of data are c
38e40 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20 62 opied from the b
38e50 75 66 66 65 72 20 5a 0a 2a 2a 20 69 6e 74 6f 20 uffer Z.** into
38e60 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 the open BLOB, s
38e70 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65 tarting at offse
38e80 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a t iOffset..**.**
38e90 20 49 66 20 74 68 65 20 5b 42 4c 4f 42 20 68 61 If the [BLOB ha
38ea0 6e 64 6c 65 5d 20 70 61 73 73 65 64 20 61 73 20 ndle] passed as
38eb0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
38ec0 6e 74 20 77 61 73 20 6e 6f 74 20 6f 70 65 6e 65 nt was not opene
38ed0 64 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 d for.** writing
38ee0 20 28 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 (the flags para
38ef0 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 meter to [sqlite
38f00 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 77 3_blob_open()] w
38f10 61 73 20 7a 65 72 6f 29 2c 0a 2a 2a 20 74 68 69 as zero),.** thi
38f20 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
38f30 6e 73 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f ns [SQLITE_READO
38f40 4e 4c 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 NLY]..**.** This
38f50 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e function may on
38f60 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20 63 6f ly modify the co
38f70 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 42 4c ntents of the BL
38f80 4f 42 3b 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 OB; it is.** not
38f90 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e 63 possible to inc
38fa0 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f rease the size o
38fb0 66 20 61 20 42 4c 4f 42 20 75 73 69 6e 67 20 74 f a BLOB using t
38fc0 68 69 73 20 41 50 49 2e 0a 2a 2a 20 49 66 20 6f his API..** If o
38fd0 66 66 73 65 74 20 69 4f 66 66 73 65 74 20 69 73 ffset iOffset is
38fe0 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79 74 less than N byt
38ff0 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 es from the end
39000 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20 of the BLOB,.**
39010 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 [SQLITE_ERROR] i
39020 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e s returned and n
39030 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65 o data is writte
39040 6e 2e 20 20 49 66 20 4e 20 69 73 0a 2a 2a 20 6c n. If N is.** l
39050 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 20 5b 53 ess than zero [S
39060 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 QLITE_ERROR] is
39070 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 returned and no
39080 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 2e data is written.
39090 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 .** The size of
390a0 74 68 65 20 42 4c 4f 42 20 28 61 6e 64 20 68 65 the BLOB (and he
390b0 6e 63 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 nce the maximum
390c0 76 61 6c 75 65 20 6f 66 20 4e 2b 69 4f 66 66 73 value of N+iOffs
390d0 65 74 29 0a 2a 2a 20 63 61 6e 20 62 65 20 64 65 et).** can be de
390e0 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 20 74 termined using t
390f0 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 he [sqlite3_blob
39100 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66 _bytes()] interf
39110 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 ace..**.** An at
39120 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 74 tempt to write t
39130 6f 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c o an expired [BL
39140 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73 OB handle] fails
39150 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f with an.** erro
39160 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 r code of [SQLIT
39170 45 5f 41 42 4f 52 54 5d 2e 20 20 57 72 69 74 65 E_ABORT]. Write
39180 73 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 74 68 s to the BLOB th
39190 61 74 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20 62 at occurred.** b
391a0 65 66 6f 72 65 20 74 68 65 20 5b 42 4c 4f 42 20 efore the [BLOB
391b0 68 61 6e 64 6c 65 5d 20 65 78 70 69 72 65 64 20 handle] expired
391c0 61 72 65 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 are not rolled b
391d0 61 63 6b 20 62 79 20 74 68 65 0a 2a 2a 20 65 78 ack by the.** ex
391e0 70 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 piration of the
391f0 68 61 6e 64 6c 65 2c 20 74 68 6f 75 67 68 20 6f handle, though o
39200 66 20 63 6f 75 72 73 65 20 74 68 6f 73 65 20 63 f course those c
39210 68 61 6e 67 65 73 20 6d 69 67 68 74 0a 2a 2a 20 hanges might.**
39220 68 61 76 65 20 62 65 65 6e 20 6f 76 65 72 77 72 have been overwr
39230 69 74 74 65 6e 20 62 79 20 74 68 65 20 73 74 61 itten by the sta
39240 74 65 6d 65 6e 74 20 74 68 61 74 20 65 78 70 69 tement that expi
39250 72 65 64 20 74 68 65 20 42 4c 4f 42 20 68 61 6e red the BLOB han
39260 64 6c 65 0a 2a 2a 20 6f 72 20 62 79 20 6f 74 68 dle.** or by oth
39270 65 72 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 73 er independent s
39280 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a tatements..**.**
39290 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c On success, SQL
392a0 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
392b0 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 ed..** Otherwise
392c0 2c 20 61 6e 20 20 5b 65 72 72 6f 72 20 63 6f 64 , an [error cod
392d0 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64 e] or an [extend
392e0 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 ed error code] i
392f0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
39300 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f * This routine o
39310 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b nly works on a [
39320 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 69 BLOB handle] whi
39330 63 68 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 ch has been crea
39340 74 65 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f ted.** by a prio
39350 72 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c r successful cal
39360 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c l to [sqlite3_bl
39370 6f 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 ob_open()] and w
39380 68 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 hich has not.**
39390 62 65 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b been closed by [
393a0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f sqlite3_blob_clo
393b0 73 65 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 se()]. Passing
393c0 61 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 any other pointe
393d0 72 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20 r in.** to this
393e0 72 6f 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20 routine results
393f0 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 in undefined and
39400 20 70 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 probably undesi
39410 72 61 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a rable behavior..
39420 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
39430 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 [sqlite3_blob_re
39440 61 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 ad()]..**.** Req
39450 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
39460 31 37 38 37 33 5d 20 5b 48 31 37 38 37 34 5d 20 17873] [H17874]
39470 5b 48 31 37 38 37 35 5d 20 5b 48 31 37 38 37 36 [H17875] [H17876
39480 5d 20 5b 48 31 37 38 37 37 5d 20 5b 48 31 37 38 ] [H17877] [H178
39490 37 39 5d 20 5b 48 31 37 38 38 32 5d 20 5b 48 31 79] [H17882] [H1
394a0 37 38 38 35 5d 0a 2a 2a 20 5b 48 31 37 38 38 38 7885].** [H17888
394b0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
394c0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 int sqlite3_blob
394d0 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62 _write(sqlite3_b
394e0 6c 6f 62 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 lob *, const voi
394f0 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 d *z, int n, int
39500 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a iOffset);../*.*
39510 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 * CAPI3REF: Virt
39520 75 61 6c 20 46 69 6c 65 20 53 79 73 74 65 6d 20 ual File System
39530 4f 62 6a 65 63 74 73 20 7b 48 31 31 32 30 30 7d Objects {H11200}
39540 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S20100>.**.**
39550 41 20 76 69 72 74 75 61 6c 20 66 69 6c 65 73 79 A virtual filesy
39560 73 74 65 6d 20 28 56 46 53 29 20 69 73 20 61 6e stem (VFS) is an
39570 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f [sqlite3_vfs] o
39580 62 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 53 51 bject.** that SQ
39590 4c 69 74 65 20 75 73 65 73 20 74 6f 20 69 6e 74 Lite uses to int
395a0 65 72 61 63 74 0a 2a 2a 20 77 69 74 68 20 74 68 eract.** with th
395b0 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 e underlying ope
395c0 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 rating system.
395d0 4d 6f 73 74 20 53 51 4c 69 74 65 20 62 75 69 6c Most SQLite buil
395e0 64 73 20 63 6f 6d 65 20 77 69 74 68 20 61 0a 2a ds come with a.*
395f0 2a 20 73 69 6e 67 6c 65 20 64 65 66 61 75 6c 74 * single default
39600 20 56 46 53 20 74 68 61 74 20 69 73 20 61 70 70 VFS that is app
39610 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 ropriate for the
39620 20 68 6f 73 74 20 63 6f 6d 70 75 74 65 72 2e 0a host computer..
39630 2a 2a 20 4e 65 77 20 56 46 53 65 73 20 63 61 6e ** New VFSes can
39640 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20 61 be registered a
39650 6e 64 20 65 78 69 73 74 69 6e 67 20 56 46 53 65 nd existing VFSe
39660 73 20 63 61 6e 20 62 65 20 75 6e 72 65 67 69 73 s can be unregis
39670 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 6f tered..** The fo
39680 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 63 llowing interfac
39690 65 73 20 61 72 65 20 70 72 6f 76 69 64 65 64 2e es are provided.
396a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
396b0 65 33 5f 76 66 73 5f 66 69 6e 64 28 29 20 69 6e e3_vfs_find() in
396c0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
396d0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 56 a pointer to a V
396e0 46 53 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d FS given its nam
396f0 65 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20 e..** Names are
39700 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e 0a case sensitive..
39710 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20 7a 65 72 ** Names are zer
39720 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 o-terminated UTF
39730 2d 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 49 -8 strings..** I
39740 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 f there is no ma
39750 74 63 68 2c 20 61 20 4e 55 4c 4c 20 70 6f 69 6e tch, a NULL poin
39760 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e ter is returned.
39770 0a 2a 2a 20 49 66 20 7a 56 66 73 4e 61 6d 65 20 .** If zVfsName
39780 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 is NULL then the
39790 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 20 default VFS is
397a0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
397b0 4e 65 77 20 56 46 53 65 73 20 61 72 65 20 72 65 New VFSes are re
397c0 67 69 73 74 65 72 65 64 20 77 69 74 68 20 73 71 gistered with sq
397d0 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 lite3_vfs_regist
397e0 65 72 28 29 2e 0a 2a 2a 20 45 61 63 68 20 6e 65 er()..** Each ne
397f0 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 68 w VFS becomes th
39800 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 66 e default VFS if
39810 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66 6c the makeDflt fl
39820 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 54 68 ag is set..** Th
39830 65 20 73 61 6d 65 20 56 46 53 20 63 61 6e 20 62 e same VFS can b
39840 65 20 72 65 67 69 73 74 65 72 65 64 20 6d 75 6c e registered mul
39850 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74 68 tiple times with
39860 6f 75 74 20 69 6e 6a 75 72 79 2e 0a 2a 2a 20 54 out injury..** T
39870 6f 20 6d 61 6b 65 20 61 6e 20 65 78 69 73 74 69 o make an existi
39880 6e 67 20 56 46 53 20 69 6e 74 6f 20 74 68 65 20 ng VFS into the
39890 64 65 66 61 75 6c 74 20 56 46 53 2c 20 72 65 67 default VFS, reg
398a0 69 73 74 65 72 20 69 74 20 61 67 61 69 6e 0a 2a ister it again.*
398b0 2a 20 77 69 74 68 20 74 68 65 20 6d 61 6b 65 44 * with the makeD
398c0 66 6c 74 20 66 6c 61 67 20 73 65 74 2e 20 20 49 flt flag set. I
398d0 66 20 74 77 6f 20 64 69 66 66 65 72 65 6e 74 20 f two different
398e0 56 46 53 65 73 20 77 69 74 68 20 74 68 65 0a 2a VFSes with the.*
398f0 2a 20 73 61 6d 65 20 6e 61 6d 65 20 61 72 65 20 * same name are
39900 72 65 67 69 73 74 65 72 65 64 2c 20 74 68 65 20 registered, the
39910 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 behavior is unde
39920 66 69 6e 65 64 2e 20 20 49 66 20 61 0a 2a 2a 20 fined. If a.**
39930 56 46 53 20 69 73 20 72 65 67 69 73 74 65 72 65 VFS is registere
39940 64 20 77 69 74 68 20 61 20 6e 61 6d 65 20 74 68 d with a name th
39950 61 74 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61 6e at is NULL or an
39960 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 0a 2a empty string,.*
39970 2a 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 * then the behav
39980 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 ior is undefined
39990 2e 0a 2a 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 74 ..**.** Unregist
399a0 65 72 20 61 20 56 46 53 20 77 69 74 68 20 74 68 er a VFS with th
399b0 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e e sqlite3_vfs_un
399c0 72 65 67 69 73 74 65 72 28 29 20 69 6e 74 65 72 register() inter
399d0 66 61 63 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 face..** If the
399e0 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 20 75 default VFS is u
399f0 6e 72 65 67 69 73 74 65 72 65 64 2c 20 61 6e 6f nregistered, ano
39a00 74 68 65 72 20 56 46 53 20 69 73 20 63 68 6f 73 ther VFS is chos
39a10 65 6e 20 61 73 0a 2a 2a 20 74 68 65 20 64 65 66 en as.** the def
39a20 61 75 6c 74 2e 20 20 54 68 65 20 63 68 6f 69 63 ault. The choic
39a30 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 56 46 e for the new VF
39a40 53 20 69 73 20 61 72 62 69 74 72 61 72 79 2e 0a S is arbitrary..
39a50 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
39a60 74 73 3a 0a 2a 2a 20 5b 48 31 31 32 30 33 5d 20 ts:.** [H11203]
39a70 5b 48 31 31 32 30 36 5d 20 5b 48 31 31 32 30 39 [H11206] [H11209
39a80 5d 20 5b 48 31 31 32 31 32 5d 20 5b 48 31 31 32 ] [H11212] [H112
39a90 31 35 5d 20 5b 48 31 31 32 31 38 5d 0a 2a 2f 0a 15] [H11218].*/.
39aa0 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 SQLITE_API sqlit
39ab0 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f e3_vfs *sqlite3_
39ac0 76 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 vfs_find(const c
39ad0 68 61 72 20 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a har *zVfsName);.
39ae0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
39af0 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 qlite3_vfs_regis
39b00 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a ter(sqlite3_vfs*
39b10 2c 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 3b , int makeDflt);
39b20 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
39b30 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 sqlite3_vfs_unre
39b40 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 gister(sqlite3_v
39b50 66 73 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 fs*);../*.** CAP
39b60 49 33 52 45 46 3a 20 4d 75 74 65 78 65 73 20 7b I3REF: Mutexes {
39b70 48 31 37 30 30 30 7d 20 3c 53 32 30 30 30 30 3e H17000} <S20000>
39b80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 .**.** The SQLit
39b90 65 20 63 6f 72 65 20 75 73 65 73 20 74 68 65 73 e core uses thes
39ba0 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 74 e routines for t
39bb0 68 72 65 61 64 0a 2a 2a 20 73 79 6e 63 68 72 6f hread.** synchro
39bc0 6e 69 7a 61 74 69 6f 6e 2e 20 54 68 6f 75 67 68 nization. Though
39bd0 20 74 68 65 79 20 61 72 65 20 69 6e 74 65 6e 64 they are intend
39be0 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a ed for internal.
39bf0 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 ** use by SQLite
39c00 2c 20 63 6f 64 65 20 74 68 61 74 20 6c 69 6e 6b , code that link
39c10 73 20 61 67 61 69 6e 73 74 20 53 51 4c 69 74 65 s against SQLite
39c20 20 69 73 0a 2a 2a 20 70 65 72 6d 69 74 74 65 64 is.** permitted
39c30 20 74 6f 20 75 73 65 20 61 6e 79 20 6f 66 20 74 to use any of t
39c40 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a hese routines..*
39c50 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 *.** The SQLite
39c60 73 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6e 74 source code cont
39c70 61 69 6e 73 20 6d 75 6c 74 69 70 6c 65 20 69 6d ains multiple im
39c80 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a plementations.**
39c90 20 6f 66 20 74 68 65 73 65 20 6d 75 74 65 78 20 of these mutex
39ca0 72 6f 75 74 69 6e 65 73 2e 20 20 41 6e 20 61 70 routines. An ap
39cb0 70 72 6f 70 72 69 61 74 65 20 69 6d 70 6c 65 6d propriate implem
39cc0 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 entation.** is s
39cd0 65 6c 65 63 74 65 64 20 61 75 74 6f 6d 61 74 69 elected automati
39ce0 63 61 6c 6c 79 20 61 74 20 63 6f 6d 70 69 6c 65 cally at compile
39cf0 2d 74 69 6d 65 2e 20 20 54 68 65 20 66 6f 6c 6c -time. The foll
39d00 6f 77 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65 6d 65 owing.** impleme
39d10 6e 74 61 74 69 6f 6e 73 20 61 72 65 20 61 76 61 ntations are ava
39d20 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 53 51 ilable in the SQ
39d30 4c 69 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a Lite core:.**.**
39d40 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 20 <ul>.** <li>
39d50 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 SQLITE_MUTEX_OS2
39d60 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 .** <li> SQLIT
39d70 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 0a E_MUTEX_PTHREAD.
39d80 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 ** <li> SQLITE
39d90 5f 4d 55 54 45 58 5f 57 33 32 0a 2a 2a 20 3c 6c _MUTEX_W32.** <l
39da0 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 i> SQLITE_MUTE
39db0 58 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a X_NOOP.** </ul>.
39dc0 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 **.** The SQLITE
39dd0 5f 4d 55 54 45 58 5f 4e 4f 4f 50 20 69 6d 70 6c _MUTEX_NOOP impl
39de0 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 20 ementation is a
39df0 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 0a set of routines.
39e00 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 ** that does no
39e10 72 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 real locking and
39e20 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 is appropriate
39e30 66 6f 72 20 75 73 65 20 69 6e 0a 2a 2a 20 61 20 for use in.** a
39e40 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 single-threaded
39e50 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 54 68 application. Th
39e60 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f e SQLITE_MUTEX_O
39e70 53 32 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 S2,.** SQLITE_MU
39e80 54 45 58 5f 50 54 48 52 45 41 44 2c 20 61 6e 64 TEX_PTHREAD, and
39e90 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 SQLITE_MUTEX_W3
39ea0 32 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2 implementation
39eb0 73 0a 2a 2a 20 61 72 65 20 61 70 70 72 6f 70 72 s.** are appropr
39ec0 69 61 74 65 20 66 6f 72 20 75 73 65 20 6f 6e 20 iate for use on
39ed0 4f 53 2f 32 2c 20 55 6e 69 78 2c 20 61 6e 64 20 OS/2, Unix, and
39ee0 57 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 Windows..**.** I
39ef0 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 f SQLite is comp
39f00 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 51 iled with the SQ
39f10 4c 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 45 LITE_MUTEX_APPDE
39f20 46 20 70 72 65 70 72 6f 63 65 73 73 6f 72 0a 2a F preprocessor.*
39f30 2a 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 * macro defined
39f40 28 77 69 74 68 20 22 2d 44 53 51 4c 49 54 45 5f (with "-DSQLITE_
39f50 4d 55 54 45 58 5f 41 50 50 44 45 46 3d 31 22 29 MUTEX_APPDEF=1")
39f60 2c 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 0a , then no mutex.
39f70 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
39f80 6e 20 69 73 20 69 6e 63 6c 75 64 65 64 20 77 69 n is included wi
39f90 74 68 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 th the library.
39fa0 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
39fb0 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 .** application
39fc0 6d 75 73 74 20 73 75 70 70 6c 79 20 61 20 63 75 must supply a cu
39fd0 73 74 6f 6d 20 6d 75 74 65 78 20 69 6d 70 6c 65 stom mutex imple
39fe0 6d 65 6e 74 61 74 69 6f 6e 20 75 73 69 6e 67 20 mentation using
39ff0 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 the.** [SQLITE_C
3a000 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 74 ONFIG_MUTEX] opt
3a010 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ion of the sqlit
3a020 65 33 5f 63 6f 6e 66 69 67 28 29 20 66 75 6e 63 e3_config() func
3a030 74 69 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65 20 63 tion.** before c
3a040 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 alling sqlite3_i
3a050 6e 69 74 69 61 6c 69 7a 65 28 29 20 6f 72 20 61 nitialize() or a
3a060 6e 79 20 6f 74 68 65 72 20 70 75 62 6c 69 63 20 ny other public
3a070 73 71 6c 69 74 65 33 5f 0a 2a 2a 20 66 75 6e 63 sqlite3_.** func
3a080 74 69 6f 6e 20 74 68 61 74 20 63 61 6c 6c 73 20 tion that calls
3a090 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
3a0a0 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 ze()..**.** {H17
3a0b0 30 31 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 011} The sqlite3
3a0c0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 _mutex_alloc() r
3a0d0 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 outine allocates
3a0e0 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 a new.** mutex
3a0f0 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f and returns a po
3a100 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 7b 48 31 inter to it. {H1
3a110 37 30 31 32 7d 20 49 66 20 69 74 20 72 65 74 75 7012} If it retu
3a120 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 rns NULL.** that
3a130 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 means that a mu
3a140 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 tex could not be
3a150 20 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 48 31 37 allocated. {H17
3a160 30 31 33 7d 20 53 51 4c 69 74 65 0a 2a 2a 20 77 013} SQLite.** w
3a170 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 ill unwind its s
3a180 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 tack and return
3a190 61 6e 20 65 72 72 6f 72 2e 20 7b 48 31 37 30 31 an error. {H1701
3a1a0 34 7d 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 4} The argument.
3a1b0 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 ** to sqlite3_mu
3a1c0 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f tex_alloc() is o
3a1d0 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 ne of these inte
3a1e0 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a ger constants:.*
3a1f0 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 *.** <ul>.** <li
3a200 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
3a210 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 FAST.** <li> SQ
3a220 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
3a230 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 SIVE.** <li> SQ
3a240 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
3a250 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e C_MASTER.** <li>
3a260 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
3a270 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 TATIC_MEM.** <li
3a280 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
3a290 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c STATIC_MEM2.** <
3a2a0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
3a2b0 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a X_STATIC_PRNG.**
3a2c0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
3a2d0 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a TEX_STATIC_LRU.*
3a2e0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
3a2f0 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 UTEX_STATIC_LRU2
3a300 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 .** </ul>.**.**
3a310 7b 48 31 37 30 31 35 7d 20 54 68 65 20 66 69 72 {H17015} The fir
3a320 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 st two constants
3a330 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d cause sqlite3_m
3a340 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 utex_alloc() to
3a350 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 create.** a new
3a360 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 mutex. The new
3a370 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 mutex is recursi
3a380 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d ve when SQLITE_M
3a390 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a UTEX_RECURSIVE.*
3a3a0 2a 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f * is used but no
3a3b0 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f t necessarily so
3a3c0 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 when SQLITE_MUT
3a3d0 45 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e EX_FAST is used.
3a3e0 20 7b 45 4e 44 7d 0a 2a 2a 20 54 68 65 20 6d 75 {END}.** The mu
3a3f0 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 tex implementati
3a400 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 on does not need
3a410 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 to make a disti
3a420 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 nction.** betwee
3a430 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 n SQLITE_MUTEX_R
3a440 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c ECURSIVE and SQL
3a450 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 ITE_MUTEX_FAST i
3a460 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 f it does.** not
3a470 20 77 61 6e 74 20 74 6f 2e 20 20 7b 48 31 37 30 want to. {H170
3a480 31 36 7d 20 42 75 74 20 53 51 4c 69 74 65 20 77 16} But SQLite w
3a490 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 ill only request
3a4a0 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 a recursive mut
3a4b0 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 ex in.** cases w
3a4c0 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e here it really n
3a4d0 65 65 64 73 20 6f 6e 65 2e 20 20 7b 45 4e 44 7d eeds one. {END}
3a4e0 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e If a faster non
3a4f0 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 -recursive mutex
3a500 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
3a510 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 on is available
3a520 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 on the host plat
3a530 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 form, the mutex
3a540 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 subsystem.** mig
3a550 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 ht return such a
3a560 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e mutex in respon
3a570 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 se to SQLITE_MUT
3a580 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 7b EX_FAST..**.** {
3a590 48 31 37 30 31 37 7d 20 54 68 65 20 6f 74 68 65 H17017} The othe
3a5a0 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 r allowed parame
3a5b0 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f ters to sqlite3_
3a5c0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 mutex_alloc() ea
3a5d0 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 ch return.** a p
3a5e0 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 ointer to a stat
3a5f0 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d ic preexisting m
3a600 75 74 65 78 2e 20 7b 45 4e 44 7d 20 20 53 69 78 utex. {END} Six
3a610 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 static mutexes
3a620 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 are.** used by t
3a630 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69 he current versi
3a640 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 on of SQLite. F
3a650 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f uture versions o
3a660 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 f SQLite.** may
3a670 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 add additional s
3a680 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20 tatic mutexes.
3a690 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 Static mutexes a
3a6a0 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a re for internal.
3a6b0 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 ** use by SQLite
3a6c0 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 only. Applicat
3a6d0 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51 ions that use SQ
3a6e0 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f Lite mutexes sho
3a6f0 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 uld.** use only
3a700 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 the dynamic mute
3a710 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 xes returned by
3a720 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
3a730 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d T or.** SQLITE_M
3a740 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a UTEX_RECURSIVE..
3a750 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 38 7d 20 4e **.** {H17018} N
3a760 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 ote that if one
3a770 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d of the dynamic m
3a780 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 utex parameters
3a790 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 (SQLITE_MUTEX_FA
3a7a0 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f ST.** or SQLITE_
3a7b0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 MUTEX_RECURSIVE)
3a7c0 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 is used then sq
3a7d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
3a7e0 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 c().** returns a
3a7f0 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 different mutex
3a800 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 on every call.
3a810 20 7b 48 31 37 30 33 34 7d 20 42 75 74 20 66 6f {H17034} But fo
3a820 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20 r the static.**
3a830 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 mutex types, the
3a840 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 same mutex is r
3a850 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 eturned on every
3a860 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a call that has.*
3a870 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 * the same type
3a880 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 number..**.** {H
3a890 31 37 30 31 39 7d 20 54 68 65 20 73 71 6c 69 74 17019} The sqlit
3a8a0 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 29 20 e3_mutex_free()
3a8b0 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 routine dealloca
3a8c0 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 tes a previously
3a8d0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 64 79 .** allocated dy
3a8e0 6e 61 6d 69 63 20 6d 75 74 65 78 2e 20 7b 48 31 namic mutex. {H1
3a8f0 37 30 32 30 7d 20 53 51 4c 69 74 65 20 69 73 20 7020} SQLite is
3a900 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c careful to deall
3a910 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 64 ocate every.** d
3a920 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 74 68 61 ynamic mutex tha
3a930 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 20 t it allocates.
3a940 7b 41 31 37 30 32 31 7d 20 54 68 65 20 64 79 6e {A17021} The dyn
3a950 61 6d 69 63 20 6d 75 74 65 78 65 73 20 6d 75 73 amic mutexes mus
3a960 74 20 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a 20 75 t not be in.** u
3a970 73 65 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 se when they are
3a980 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 41 deallocated. {A
3a990 31 37 30 32 32 7d 20 41 74 74 65 6d 70 74 69 6e 17022} Attemptin
3a9a0 67 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 g to deallocate
3a9b0 61 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65 a static.** mute
3a9c0 78 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 x results in und
3a9d0 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e efined behavior.
3a9e0 20 7b 48 31 37 30 32 33 7d 20 53 51 4c 69 74 65 {H17023} SQLite
3a9f0 20 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61 74 never deallocat
3aa00 65 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 20 6d es.** a static m
3aa10 75 74 65 78 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a utex. {END}.**.*
3aa20 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
3aa30 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 tex_enter() and
3aa40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
3aa50 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 y() routines att
3aa60 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 empt.** to enter
3aa70 20 61 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 32 a mutex. {H1702
3aa80 34 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 4} If another th
3aa90 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 read is already
3aaa0 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 within the mutex
3aab0 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 ,.** sqlite3_mut
3aac0 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 ex_enter() will
3aad0 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 block and sqlite
3aae0 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 3_mutex_try() wi
3aaf0 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c ll return.** SQL
3ab00 49 54 45 5f 42 55 53 59 2e 20 7b 48 31 37 30 32 ITE_BUSY. {H1702
3ab10 35 7d 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 5} The sqlite3_
3ab20 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 mutex_try() inte
3ab30 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 5b 53 rface returns [S
3ab40 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f QLITE_OK].** upo
3ab50 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 n successful ent
3ab60 72 79 2e 20 20 7b 48 31 37 30 32 36 7d 20 4d 75 ry. {H17026} Mu
3ab70 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73 texes created us
3ab80 69 6e 67 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 ing.** SQLITE_MU
3ab90 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 TEX_RECURSIVE ca
3aba0 6e 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c n be entered mul
3abb0 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 tiple times by t
3abc0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 0a he same thread..
3abd0 2a 2a 20 7b 48 31 37 30 32 37 7d 20 49 6e 20 73 ** {H17027} In s
3abe0 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a uch cases the,.*
3abf0 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 * mutex must be
3ac00 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 exited an equal
3ac10 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 number of times
3ac20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 before another t
3ac30 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 hread.** can ent
3ac40 65 72 2e 20 20 7b 41 31 37 30 32 38 7d 20 49 66 er. {A17028} If
3ac50 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
3ac60 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 tries to enter
3ac70 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e any other.** kin
3ac80 64 20 6f 66 20 6d 75 74 65 78 20 6d 6f 72 65 20 d of mutex more
3ac90 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 than once, the b
3aca0 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 ehavior is undef
3acb0 69 6e 65 64 2e 0a 2a 2a 20 7b 48 31 37 30 32 39 ined..** {H17029
3acc0 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 } SQLite will ne
3acd0 76 65 72 20 65 78 68 69 62 69 74 0a 2a 2a 20 73 ver exhibit.** s
3ace0 75 63 68 20 62 65 68 61 76 69 6f 72 20 69 6e 20 uch behavior in
3acf0 69 74 73 20 6f 77 6e 20 75 73 65 20 6f 66 20 6d its own use of m
3ad00 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f utexes..**.** So
3ad10 6d 65 20 73 79 73 74 65 6d 73 20 28 66 6f 72 20 me systems (for
3ad20 65 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f 77 73 example, Windows
3ad30 20 39 35 29 20 64 6f 20 6e 6f 74 20 73 75 70 70 95) do not supp
3ad40 6f 72 74 20 74 68 65 20 6f 70 65 72 61 74 69 6f ort the operatio
3ad50 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 n.** implemented
3ad60 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 by sqlite3_mute
3ad70 78 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74 68 6f x_try(). On tho
3ad80 73 65 20 73 79 73 74 65 6d 73 2c 20 73 71 6c 69 se systems, sqli
3ad90 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 0a te3_mutex_try().
3ada0 2a 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 ** will always r
3adb0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 eturn SQLITE_BUS
3adc0 59 2e 20 20 7b 48 31 37 30 33 30 7d 20 54 68 65 Y. {H17030} The
3add0 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6f 6e 6c SQLite core onl
3ade0 79 20 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 73 y ever uses.** s
3adf0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
3ae00 28 29 20 61 73 20 61 6e 20 6f 70 74 69 6d 69 7a () as an optimiz
3ae10 61 74 69 6f 6e 20 73 6f 20 74 68 69 73 20 69 73 ation so this is
3ae20 20 61 63 63 65 70 74 61 62 6c 65 20 62 65 68 61 acceptable beha
3ae30 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 vior..**.** {H17
3ae40 30 33 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 031} The sqlite3
3ae50 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 _mutex_leave() r
3ae60 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d outine exits a m
3ae70 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a utex that was.**
3ae80 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 previously ente
3ae90 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 red by the same
3aea0 74 68 72 65 61 64 2e 20 20 7b 41 31 37 30 33 32 thread. {A17032
3aeb0 7d 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a } The behavior.*
3aec0 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 * is undefined i
3aed0 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e f the mutex is n
3aee0 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 ot currently ent
3aef0 65 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 ered by the.** c
3af00 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 6f 72 alling thread or
3af10 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
3af20 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 7b 48 y allocated. {H
3af30 31 37 30 33 33 7d 20 53 51 4c 69 74 65 20 77 69 17033} SQLite wi
3af40 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 64 6f 20 65 ll.** never do e
3af50 69 74 68 65 72 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a ither. {END}.**.
3af60 2a 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 ** If the argume
3af70 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 nt to sqlite3_mu
3af80 74 65 78 5f 65 6e 74 65 72 28 29 2c 20 73 71 6c tex_enter(), sql
3af90 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 ite3_mutex_try()
3afa0 2c 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f , or.** sqlite3_
3afb0 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 69 73 mutex_leave() is
3afc0 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c a NULL pointer,
3afd0 20 74 68 65 6e 20 61 6c 6c 20 74 68 72 65 65 20 then all three
3afe0 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65 68 61 routines.** beha
3aff0 76 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a ve as no-ops..**
3b000 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 .** See also: [s
3b010 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
3b020 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 d()] and [sqlite
3b030 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 3_mutex_notheld(
3b040 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 )]..*/.SQLITE_AP
3b050 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 I sqlite3_mutex
3b060 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 *sqlite3_mutex_a
3b070 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 lloc(int);.SQLIT
3b080 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
3b090 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71 e3_mutex_free(sq
3b0a0 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 lite3_mutex*);.S
3b0b0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
3b0c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
3b0d0 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 er(sqlite3_mutex
3b0e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 *);.SQLITE_API i
3b0f0 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 nt sqlite3_mutex
3b100 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 _try(sqlite3_mut
3b110 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ex*);.SQLITE_API
3b120 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 void sqlite3_mu
3b130 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 tex_leave(sqlite
3b140 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 3_mutex*);../*.*
3b150 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 * CAPI3REF: Mute
3b160 78 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74 x Methods Object
3b170 20 7b 48 31 37 31 32 30 7d 20 3c 53 32 30 31 33 {H17120} <S2013
3b180 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
3b190 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 AL.**.** An inst
3b1a0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 ance of this str
3b1b0 75 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 ucture defines t
3b1c0 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 he low-level rou
3b1d0 74 69 6e 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f tines.** used to
3b1e0 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 75 73 allocate and us
3b1f0 65 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a e mutexes..**.**
3b200 20 55 73 75 61 6c 6c 79 2c 20 74 68 65 20 64 65 Usually, the de
3b210 66 61 75 6c 74 20 6d 75 74 65 78 20 69 6d 70 6c fault mutex impl
3b220 65 6d 65 6e 74 61 74 69 6f 6e 73 20 70 72 6f 76 ementations prov
3b230 69 64 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 ided by SQLite a
3b240 72 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 re.** sufficient
3b250 2c 20 68 6f 77 65 76 65 72 20 74 68 65 20 75 73 , however the us
3b260 65 72 20 68 61 73 20 74 68 65 20 6f 70 74 69 6f er has the optio
3b270 6e 20 6f 66 20 73 75 62 73 74 69 74 75 74 69 6e n of substitutin
3b280 67 20 61 20 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d g a custom.** im
3b290 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 plementation for
3b2a0 20 73 70 65 63 69 61 6c 69 7a 65 64 20 64 65 70 specialized dep
3b2b0 6c 6f 79 6d 65 6e 74 73 20 6f 72 20 73 79 73 74 loyments or syst
3b2c0 65 6d 73 20 66 6f 72 20 77 68 69 63 68 20 53 51 ems for which SQ
3b2d0 4c 69 74 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 Lite.** does not
3b2e0 20 70 72 6f 76 69 64 65 20 61 20 73 75 69 74 61 provide a suita
3b2f0 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ble implementati
3b300 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 on. In this case
3b310 2c 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 63 72 , the user.** cr
3b320 65 61 74 65 73 20 61 6e 64 20 70 6f 70 75 6c 61 eates and popula
3b330 74 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 tes an instance
3b340 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
3b350 65 20 74 6f 20 70 61 73 73 0a 2a 2a 20 74 6f 20 e to pass.** to
3b360 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
3b370 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 along with the
3b380 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d [SQLITE_CONFIG_M
3b390 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a UTEX] option..**
3b3a0 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 61 Additionally, a
3b3b0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
3b3c0 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e is structure can
3b3d0 20 62 65 20 75 73 65 64 20 61 73 20 61 6e 0a 2a be used as an.*
3b3e0 2a 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c * output variabl
3b3f0 65 20 77 68 65 6e 20 71 75 65 72 79 69 6e 67 20 e when querying
3b400 74 68 65 20 73 79 73 74 65 6d 20 66 6f 72 20 74 the system for t
3b410 68 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78 he current mutex
3b420 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
3b430 6f 6e 2c 20 75 73 69 6e 67 20 74 68 65 20 5b 53 on, using the [S
3b440 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 QLITE_CONFIG_GET
3b450 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a MUTEX] option..*
3b460 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 49 *.** The xMutexI
3b470 6e 69 74 20 6d 65 74 68 6f 64 20 64 65 66 69 6e nit method defin
3b480 65 64 20 62 79 20 74 68 69 73 20 73 74 72 75 63 ed by this struc
3b490 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 ture is invoked
3b4a0 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79 as.** part of sy
3b4b0 73 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a 61 74 stem initializat
3b4c0 69 6f 6e 20 62 79 20 74 68 65 20 73 71 6c 69 74 ion by the sqlit
3b4d0 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
3b4e0 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 31 function..** {H1
3b4f0 37 30 30 31 7d 20 54 68 65 20 78 4d 75 74 65 78 7001} The xMutex
3b500 49 6e 69 74 20 72 6f 75 74 69 6e 65 20 73 68 61 Init routine sha
3b510 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20 ll be called by
3b520 53 51 4c 69 74 65 20 6f 6e 63 65 20 66 6f 72 20 SQLite once for
3b530 65 61 63 68 0a 2a 2a 20 65 66 66 65 63 74 69 76 each.** effectiv
3b540 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 e call to [sqlit
3b550 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d e3_initialize()]
3b560 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 ..**.** The xMut
3b570 65 78 45 6e 64 20 6d 65 74 68 6f 64 20 64 65 66 exEnd method def
3b580 69 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72 ined by this str
3b590 75 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 ucture is invoke
3b5a0 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 d as.** part of
3b5b0 73 79 73 74 65 6d 20 73 68 75 74 64 6f 77 6e 20 system shutdown
3b5c0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 by the sqlite3_s
3b5d0 68 75 74 64 6f 77 6e 28 29 20 66 75 6e 63 74 69 hutdown() functi
3b5e0 6f 6e 2e 20 54 68 65 0a 2a 2a 20 69 6d 70 6c 65 on. The.** imple
3b5f0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 mentation of thi
3b600 73 20 6d 65 74 68 6f 64 20 69 73 20 65 78 70 65 s method is expe
3b610 63 74 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20 cted to release
3b620 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a all outstanding.
3b630 2a 2a 20 72 65 73 6f 75 72 63 65 73 20 6f 62 74 ** resources obt
3b640 61 69 6e 65 64 20 62 79 20 74 68 65 20 6d 75 74 ained by the mut
3b650 65 78 20 6d 65 74 68 6f 64 73 20 69 6d 70 6c 65 ex methods imple
3b660 6d 65 6e 74 61 74 69 6f 6e 2c 20 65 73 70 65 63 mentation, espec
3b670 69 61 6c 6c 79 0a 2a 2a 20 74 68 6f 73 65 20 6f ially.** those o
3b680 62 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 78 btained by the x
3b690 4d 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f 64 MutexInit method
3b6a0 2e 20 7b 48 31 37 30 30 33 7d 20 54 68 65 20 78 . {H17003} The x
3b6b0 4d 75 74 65 78 45 6e 64 28 29 0a 2a 2a 20 69 6e MutexEnd().** in
3b6c0 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65 terface shall be
3b6d0 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f invoked once fo
3b6e0 72 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b r each call to [
3b6f0 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e sqlite3_shutdown
3b700 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 ()]..**.** The r
3b710 65 6d 61 69 6e 69 6e 67 20 73 65 76 65 6e 20 6d emaining seven m
3b720 65 74 68 6f 64 73 20 64 65 66 69 6e 65 64 20 62 ethods defined b
3b730 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 y this structure
3b740 20 28 78 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 2a (xMutexAlloc,.*
3b750 2a 20 78 4d 75 74 65 78 46 72 65 65 2c 20 78 4d * xMutexFree, xM
3b760 75 74 65 78 45 6e 74 65 72 2c 20 78 4d 75 74 65 utexEnter, xMute
3b770 78 54 72 79 2c 20 78 4d 75 74 65 78 4c 65 61 76 xTry, xMutexLeav
3b780 65 2c 20 78 4d 75 74 65 78 48 65 6c 64 20 61 6e e, xMutexHeld an
3b790 64 0a 2a 2a 20 78 4d 75 74 65 78 4e 6f 74 68 65 d.** xMutexNothe
3b7a0 6c 64 29 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 ld) implement th
3b7b0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 e following inte
3b7c0 72 66 61 63 65 73 20 28 72 65 73 70 65 63 74 69 rfaces (respecti
3b7d0 76 65 6c 79 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c vely):.**.** <ul
3b7e0 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 >.** <li> [sq
3b7f0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
3b800 63 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 c()] </li>.**
3b810 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d <li> [sqlite3_m
3b820 75 74 65 78 5f 66 72 65 65 28 29 5d 20 3c 2f 6c utex_free()] </l
3b830 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 i>.** <li> [s
3b840 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
3b850 65 72 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 er()] </li>.**
3b860 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f <li> [sqlite3_
3b870 6d 75 74 65 78 5f 74 72 79 28 29 5d 20 3c 2f 6c mutex_try()] </l
3b880 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 i>.** <li> [s
3b890 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
3b8a0 76 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 ve()] </li>.**
3b8b0 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f <li> [sqlite3_
3b8c0 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d 20 3c 2f mutex_held()] </
3b8d0 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b li>.** <li> [
3b8e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f sqlite3_mutex_no
3b8f0 74 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a theld()] </li>.*
3b900 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 * </ul>.**.** Th
3b910 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 e only differenc
3b920 65 20 69 73 20 74 68 61 74 20 74 68 65 20 70 75 e is that the pu
3b930 62 6c 69 63 20 73 71 6c 69 74 65 33 5f 58 58 58 blic sqlite3_XXX
3b940 20 66 75 6e 63 74 69 6f 6e 73 20 65 6e 75 6d 65 functions enume
3b950 72 61 74 65 64 0a 2a 2a 20 61 62 6f 76 65 20 73 rated.** above s
3b960 69 6c 65 6e 74 6c 79 20 69 67 6e 6f 72 65 20 61 ilently ignore a
3b970 6e 79 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 74 ny invocations t
3b980 68 61 74 20 70 61 73 73 20 61 20 4e 55 4c 4c 20 hat pass a NULL
3b990 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 0a pointer instead.
3b9a0 2a 2a 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 ** of a valid mu
3b9b0 74 65 78 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 tex handle. The
3b9c0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
3b9d0 6f 66 20 74 68 65 20 6d 65 74 68 6f 64 73 20 64 of the methods d
3b9e0 65 66 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 69 efined.** by thi
3b9f0 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 s structure are
3ba00 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 not required to
3ba10 68 61 6e 64 6c 65 20 74 68 69 73 20 63 61 73 65 handle this case
3ba20 2c 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a , the results.**
3ba30 20 6f 66 20 70 61 73 73 69 6e 67 20 61 20 4e 55 of passing a NU
3ba40 4c 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 LL pointer inste
3ba50 61 64 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 ad of a valid mu
3ba60 74 65 78 20 68 61 6e 64 6c 65 20 61 72 65 20 75 tex handle are u
3ba70 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 28 69 2e 65 ndefined.** (i.e
3ba80 2e 20 69 74 20 69 73 20 61 63 63 65 70 74 61 62 . it is acceptab
3ba90 6c 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e le to provide an
3baa0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
3bab0 74 68 61 74 20 73 65 67 66 61 75 6c 74 73 20 69 that segfaults i
3bac0 66 0a 2a 2a 20 69 74 20 69 73 20 70 61 73 73 65 f.** it is passe
3bad0 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 d a NULL pointer
3bae0 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 )..**.** The xMu
3baf0 74 65 78 49 6e 69 74 28 29 20 6d 65 74 68 6f 64 texInit() method
3bb00 20 6d 75 73 74 20 62 65 20 74 68 72 65 61 64 73 must be threads
3bb10 61 66 65 2e 20 20 49 74 20 6d 75 73 74 20 62 65 afe. It must be
3bb20 20 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20 harmless to.**
3bb30 69 6e 76 6f 6b 65 20 78 4d 75 74 65 78 49 6e 69 invoke xMutexIni
3bb40 74 28 29 20 6d 75 74 69 70 6c 65 20 74 69 6d 65 t() mutiple time
3bb50 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d s within the sam
3bb60 65 20 70 72 6f 63 65 73 73 20 61 6e 64 20 77 69 e process and wi
3bb70 74 68 6f 75 74 0a 2a 2a 20 69 6e 74 65 72 76 65 thout.** interve
3bb80 6e 69 6e 67 20 63 61 6c 6c 73 20 74 6f 20 78 4d ning calls to xM
3bb90 75 74 65 78 45 6e 64 28 29 2e 20 20 53 65 63 6f utexEnd(). Seco
3bba0 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e nd and subsequen
3bbb0 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 78 4d t calls to.** xM
3bbc0 75 74 65 78 49 6e 69 74 28 29 20 6d 75 73 74 20 utexInit() must
3bbd0 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a be no-ops..**.**
3bbe0 20 78 4d 75 74 65 78 49 6e 69 74 28 29 20 6d 75 xMutexInit() mu
3bbf0 73 74 20 6e 6f 74 20 75 73 65 20 53 51 4c 69 74 st not use SQLit
3bc00 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
3bc10 69 6f 6e 20 28 5b 73 71 6c 69 74 65 33 5f 6d 61 ion ([sqlite3_ma
3bc20 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 61 6e 64 20 69 lloc()].** and i
3bc30 74 73 20 61 73 73 6f 63 69 61 74 65 73 29 2e 20 ts associates).
3bc40 20 53 69 6d 69 6c 61 72 6c 79 2c 20 78 4d 75 74 Similarly, xMut
3bc50 65 78 41 6c 6c 6f 63 28 29 20 6d 75 73 74 20 6e exAlloc() must n
3bc60 6f 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 65 ot use SQLite me
3bc70 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 mory.** allocati
3bc80 6f 6e 20 66 6f 72 20 61 20 73 74 61 74 69 63 20 on for a static
3bc90 6d 75 74 65 78 2e 20 20 48 6f 77 65 76 65 72 20 mutex. However
3bca0 78 4d 75 74 65 78 41 6c 6c 6f 63 28 29 20 6d 61 xMutexAlloc() ma
3bcb0 79 20 75 73 65 20 53 51 4c 69 74 65 0a 2a 2a 20 y use SQLite.**
3bcc0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
3bcd0 6e 20 66 6f 72 20 61 20 66 61 73 74 20 6f 72 20 n for a fast or
3bce0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 2e recursive mutex.
3bcf0 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 .**.** SQLite wi
3bd00 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 4d ll invoke the xM
3bd10 75 74 65 78 45 6e 64 28 29 20 6d 65 74 68 6f 64 utexEnd() method
3bd20 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 73 when [sqlite3_s
3bd30 68 75 74 64 6f 77 6e 28 29 5d 20 69 73 0a 2a 2a hutdown()] is.**
3bd40 20 63 61 6c 6c 65 64 2c 20 62 75 74 20 6f 6e 6c called, but onl
3bd50 79 20 69 66 20 74 68 65 20 70 72 69 6f 72 20 63 y if the prior c
3bd60 61 6c 6c 20 74 6f 20 78 4d 75 74 65 78 49 6e 69 all to xMutexIni
3bd70 74 20 72 65 74 75 72 6e 65 64 20 53 51 4c 49 54 t returned SQLIT
3bd80 45 5f 4f 4b 2e 0a 2a 2a 20 49 66 20 78 4d 75 74 E_OK..** If xMut
3bd90 65 78 49 6e 69 74 20 66 61 69 6c 73 20 69 6e 20 exInit fails in
3bda0 61 6e 79 20 77 61 79 2c 20 69 74 20 69 73 20 65 any way, it is e
3bdb0 78 70 65 63 74 65 64 20 74 6f 20 63 6c 65 61 6e xpected to clean
3bdc0 20 75 70 20 61 66 74 65 72 20 69 74 73 65 6c 66 up after itself
3bdd0 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65 74 .** prior to ret
3bde0 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 74 79 70 65 64 urning..*/.typed
3bdf0 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 ef struct sqlite
3be00 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 3_mutex_methods
3be10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
3be20 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 thods;.struct sq
3be30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
3be40 6f 64 73 20 7b 0a 20 20 69 6e 74 20 28 2a 78 4d ods {. int (*xM
3be50 75 74 65 78 49 6e 69 74 29 28 76 6f 69 64 29 3b utexInit)(void);
3be60 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 45 . int (*xMutexE
3be70 6e 64 29 28 76 6f 69 64 29 3b 0a 20 20 73 71 6c nd)(void);. sql
3be80 69 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a 78 4d ite3_mutex *(*xM
3be90 75 74 65 78 41 6c 6c 6f 63 29 28 69 6e 74 29 3b utexAlloc)(int);
3bea0 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 . void (*xMutex
3beb0 46 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d 75 Free)(sqlite3_mu
3bec0 74 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28 tex *);. void (
3bed0 2a 78 4d 75 74 65 78 45 6e 74 65 72 29 28 73 71 *xMutexEnter)(sq
3bee0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a lite3_mutex *);.
3bef0 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 54 72 int (*xMutexTr
3bf00 79 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 y)(sqlite3_mutex
3bf10 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d *);. void (*xM
3bf20 75 74 65 78 4c 65 61 76 65 29 28 73 71 6c 69 74 utexLeave)(sqlit
3bf30 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 e3_mutex *);. i
3bf40 6e 74 20 28 2a 78 4d 75 74 65 78 48 65 6c 64 29 nt (*xMutexHeld)
3bf50 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
3bf60 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 );. int (*xMute
3bf70 78 4e 6f 74 68 65 6c 64 29 28 73 71 6c 69 74 65 xNotheld)(sqlite
3bf80 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 7d 3b 0a 0a 3_mutex *);.};..
3bf90 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
3bfa0 4d 75 74 65 78 20 56 65 72 69 66 69 63 61 74 69 Mutex Verificati
3bfb0 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48 31 37 on Routines {H17
3bfc0 30 38 30 7d 20 3c 53 32 30 31 33 30 3e 20 3c 53 080} <S20130> <S
3bfd0 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 30800>.**.** The
3bfe0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
3bff0 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 eld() and sqlite
3c000 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 3_mutex_notheld(
3c010 29 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 ) routines.** ar
3c020 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 e intended for u
3c030 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 se inside assert
3c040 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 7b () statements. {
3c050 48 31 37 30 38 31 7d 20 54 68 65 20 53 51 4c 69 H17081} The SQLi
3c060 74 65 20 63 6f 72 65 0a 2a 2a 20 6e 65 76 65 72 te core.** never
3c070 20 75 73 65 73 20 74 68 65 73 65 20 72 6f 75 74 uses these rout
3c080 69 6e 65 73 20 65 78 63 65 70 74 20 69 6e 73 69 ines except insi
3c090 64 65 20 61 6e 20 61 73 73 65 72 74 28 29 20 61 de an assert() a
3c0a0 6e 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a nd applications.
3c0b0 2a 2a 20 61 72 65 20 61 64 76 69 73 65 64 20 74 ** are advised t
3c0c0 6f 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6c 65 61 o follow the lea
3c0d0 64 20 6f 66 20 74 68 65 20 63 6f 72 65 2e 20 20 d of the core.
3c0e0 7b 48 31 37 30 38 32 7d 20 54 68 65 20 63 6f 72 {H17082} The cor
3c0f0 65 20 6f 6e 6c 79 0a 2a 2a 20 70 72 6f 76 69 64 e only.** provid
3c100 65 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f es implementatio
3c110 6e 73 20 66 6f 72 20 74 68 65 73 65 20 72 6f 75 ns for these rou
3c120 74 69 6e 65 73 20 77 68 65 6e 20 69 74 20 69 73 tines when it is
3c130 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77 69 74 compiled.** wit
3c140 68 20 74 68 65 20 53 51 4c 49 54 45 5f 44 45 42 h the SQLITE_DEB
3c150 55 47 20 66 6c 61 67 2e 20 20 7b 41 31 37 30 38 UG flag. {A1708
3c160 37 7d 20 45 78 74 65 72 6e 61 6c 20 6d 75 74 65 7} External mute
3c170 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e x implementation
3c180 73 0a 2a 2a 20 61 72 65 20 6f 6e 6c 79 20 72 65 s.** are only re
3c190 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69 64 quired to provid
3c1a0 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 e these routines
3c1b0 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 if SQLITE_DEBUG
3c1c0 20 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 20 61 is.** defined a
3c1d0 6e 64 20 69 66 20 4e 44 45 42 55 47 20 69 73 20 nd if NDEBUG is
3c1e0 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a not defined..**.
3c1f0 2a 2a 20 7b 48 31 37 30 38 33 7d 20 54 68 65 73 ** {H17083} Thes
3c200 65 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c e routines shoul
3c210 64 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66 d return true if
3c220 20 74 68 65 20 6d 75 74 65 78 20 69 6e 20 74 68 the mutex in th
3c230 65 69 72 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 eir argument.**
3c240 69 73 20 68 65 6c 64 20 6f 72 20 6e 6f 74 20 68 is held or not h
3c250 65 6c 64 2c 20 72 65 73 70 65 63 74 69 76 65 6c eld, respectivel
3c260 79 2c 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e y, by the callin
3c270 67 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 g thread..**.**
3c280 7b 58 31 37 30 38 34 7d 20 54 68 65 20 69 6d 70 {X17084} The imp
3c290 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6e lementation is n
3c2a0 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 70 ot required to p
3c2b0 72 6f 76 69 64 65 64 20 76 65 72 73 69 6f 6e 73 rovided versions
3c2c0 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 72 6f 75 of these.** rou
3c2d0 74 69 6e 65 73 20 74 68 61 74 20 61 63 74 75 61 tines that actua
3c2e0 6c 6c 79 20 77 6f 72 6b 2e 20 49 66 20 74 68 65 lly work. If the
3c2f0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
3c300 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 64 65 does not provide
3c310 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 76 65 72 73 working.** vers
3c320 69 6f 6e 73 20 6f 66 20 74 68 65 73 65 20 72 6f ions of these ro
3c330 75 74 69 6e 65 73 2c 20 69 74 20 73 68 6f 75 6c utines, it shoul
3c340 64 20 61 74 20 6c 65 61 73 74 20 70 72 6f 76 69 d at least provi
3c350 64 65 20 73 74 75 62 73 20 74 68 61 74 20 61 6c de stubs that al
3c360 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e 20 74 ways.** return t
3c370 72 75 65 20 73 6f 20 74 68 61 74 20 6f 6e 65 20 rue so that one
3c380 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 73 70 75 does not get spu
3c390 72 69 6f 75 73 20 61 73 73 65 72 74 69 6f 6e 20 rious assertion
3c3a0 66 61 69 6c 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 failures..**.**
3c3b0 7b 48 31 37 30 38 35 7d 20 49 66 20 74 68 65 20 {H17085} If the
3c3c0 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 argument to sqli
3c3d0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 te3_mutex_held()
3c3e0 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 is a NULL point
3c3f0 65 72 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 72 er then.** the r
3c400 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65 outine should re
3c410 74 75 72 6e 20 31 2e 20 20 7b 45 4e 44 7d 20 54 turn 1. {END} T
3c420 68 69 73 20 73 65 65 6d 73 20 63 6f 75 6e 74 65 his seems counte
3c430 72 2d 69 6e 74 75 69 74 69 76 65 20 73 69 6e 63 r-intuitive sinc
3c440 65 0a 2a 2a 20 63 6c 65 61 72 6c 79 20 74 68 65 e.** clearly the
3c450 20 6d 75 74 65 78 20 63 61 6e 6e 6f 74 20 62 65 mutex cannot be
3c460 20 68 65 6c 64 20 69 66 20 69 74 20 64 6f 65 73 held if it does
3c470 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 42 75 74 not exist. But
3c480 20 74 68 65 0a 2a 2a 20 74 68 65 20 72 65 61 73 the.** the reas
3c490 6f 6e 20 74 68 65 20 6d 75 74 65 78 20 64 6f 65 on the mutex doe
3c4a0 73 20 6e 6f 74 20 65 78 69 73 74 20 69 73 20 62 s not exist is b
3c4b0 65 63 61 75 73 65 20 74 68 65 20 62 75 69 6c 64 ecause the build
3c4c0 20 69 73 20 6e 6f 74 0a 2a 2a 20 75 73 69 6e 67 is not.** using
3c4d0 20 6d 75 74 65 78 65 73 2e 20 20 41 6e 64 20 77 mutexes. And w
3c4e0 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 e do not want th
3c4f0 65 20 61 73 73 65 72 74 28 29 20 63 6f 6e 74 61 e assert() conta
3c500 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 63 61 6c ining the.** cal
3c510 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 l to sqlite3_mut
3c520 65 78 5f 68 65 6c 64 28 29 20 74 6f 20 66 61 69 ex_held() to fai
3c530 6c 2c 20 73 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f l, so a non-zero
3c540 20 72 65 74 75 72 6e 20 69 73 0a 2a 2a 20 74 68 return is.** th
3c550 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 68 e appropriate th
3c560 69 6e 67 20 74 6f 20 64 6f 2e 20 20 7b 48 31 37 ing to do. {H17
3c570 30 38 36 7d 20 54 68 65 20 73 71 6c 69 74 65 33 086} The sqlite3
3c580 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 _mutex_notheld()
3c590 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 73 68 .** interface sh
3c5a0 6f 75 6c 64 20 61 6c 73 6f 20 72 65 74 75 72 6e ould also return
3c5b0 20 31 20 77 68 65 6e 20 67 69 76 65 6e 20 61 20 1 when given a
3c5c0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f NULL pointer..*/
3c5d0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
3c5e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
3c5f0 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
3c600 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 *);.SQLITE_API i
3c610 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 nt sqlite3_mutex
3c620 5f 6e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 _notheld(sqlite3
3c630 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a _mutex*);../*.**
3c640 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 CAPI3REF: Mutex
3c650 20 54 79 70 65 73 20 7b 48 31 37 30 30 31 7d 20 Types {H17001}
3c660 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <H17000>.**.** T
3c670 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 he [sqlite3_mute
3c680 78 5f 61 6c 6c 6f 63 28 29 5d 20 69 6e 74 65 72 x_alloc()] inter
3c690 66 61 63 65 20 74 61 6b 65 73 20 61 20 73 69 6e face takes a sin
3c6a0 67 6c 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 gle argument.**
3c6b0 77 68 69 63 68 20 69 73 20 6f 6e 65 20 6f 66 20 which is one of
3c6c0 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f these integer co
3c6d0 6e 73 74 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 nstants..**.** T
3c6e0 68 65 20 73 65 74 20 6f 66 20 73 74 61 74 69 63 he set of static
3c6f0 20 6d 75 74 65 78 65 73 20 6d 61 79 20 63 68 61 mutexes may cha
3c700 6e 67 65 20 66 72 6f 6d 20 6f 6e 65 20 53 51 4c nge from one SQL
3c710 69 74 65 20 72 65 6c 65 61 73 65 20 74 6f 20 74 ite release to t
3c720 68 65 0a 2a 2a 20 6e 65 78 74 2e 20 20 41 70 70 he.** next. App
3c730 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f lications that o
3c740 76 65 72 72 69 64 65 20 74 68 65 20 62 75 69 6c verride the buil
3c750 74 2d 69 6e 20 6d 75 74 65 78 20 6c 6f 67 69 63 t-in mutex logic
3c760 20 6d 75 73 74 20 62 65 0a 2a 2a 20 70 72 65 70 must be.** prep
3c770 61 72 65 64 20 74 6f 20 61 63 63 6f 6d 6d 6f 64 ared to accommod
3c780 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 ate additional s
3c790 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 0a 2a tatic mutexes..*
3c7a0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
3c7b0 5f 4d 55 54 45 58 5f 46 41 53 54 20 20 20 20 20 _MUTEX_FAST
3c7c0 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 0.#defin
3c7d0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 e SQLITE_MUTEX_R
3c7e0 45 43 55 52 53 49 56 45 20 20 20 20 20 20 20 20 ECURSIVE
3c7f0 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 1.#define SQLITE
3c800 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 _MUTEX_STATIC_MA
3c810 53 54 45 52 20 20 20 20 32 0a 23 64 65 66 69 6e STER 2.#defin
3c820 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 e SQLITE_MUTEX_S
3c830 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 TATIC_MEM
3c840 33 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 61 3 /* sqlite3_ma
3c850 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e lloc() */.#defin
3c860 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 e SQLITE_MUTEX_S
3c870 54 41 54 49 43 5f 4d 45 4d 32 20 20 20 20 20 20 TATIC_MEM2
3c880 34 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 20 2a 4 /* NOT USED *
3c890 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
3c8a0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4f 50 _MUTEX_STATIC_OP
3c8b0 45 4e 20 20 20 20 20 20 34 20 20 2f 2a 20 73 71 EN 4 /* sq
3c8c0 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 29 lite3BtreeOpen()
3c8d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
3c8e0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
3c8f0 50 52 4e 47 20 20 20 20 20 20 35 20 20 2f 2a 20 PRNG 5 /*
3c900 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 28 29 sqlite3_random()
3c910 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
3c920 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
3c930 4c 52 55 20 20 20 20 20 20 20 36 20 20 2f 2a 20 LRU 6 /*
3c940 6c 72 75 20 70 61 67 65 20 6c 69 73 74 20 2a 2f lru page list */
3c950 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
3c960 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 MUTEX_STATIC_LRU
3c970 32 20 20 20 20 20 20 37 20 20 2f 2a 20 6c 72 75 2 7 /* lru
3c980 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 0a 2f page list */../
3c990 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 *.** CAPI3REF: R
3c9a0 65 74 72 69 65 76 65 20 74 68 65 20 6d 75 74 65 etrieve the mute
3c9b0 78 20 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 x for a database
3c9c0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 37 connection {H17
3c9d0 30 30 32 7d 20 3c 48 31 37 30 30 30 3e 0a 2a 2a 002} <H17000>.**
3c9e0 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 .** This interfa
3c9f0 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 ce returns a poi
3ca00 6e 74 65 72 20 74 68 65 20 5b 73 71 6c 69 74 65 nter the [sqlite
3ca10 33 5f 6d 75 74 65 78 5d 20 6f 62 6a 65 63 74 20 3_mutex] object
3ca20 74 68 61 74 20 0a 2a 2a 20 73 65 72 69 61 6c 69 that .** seriali
3ca30 7a 65 73 20 61 63 63 65 73 73 20 74 6f 20 74 68 zes access to th
3ca40 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
3ca50 65 63 74 69 6f 6e 5d 20 67 69 76 65 6e 20 69 6e ection] given in
3ca60 20 74 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a the argument.**
3ca70 20 77 68 65 6e 20 74 68 65 20 5b 74 68 72 65 61 when the [threa
3ca80 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 53 65 ding mode] is Se
3ca90 72 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 20 rialized..** If
3caa0 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d the [threading m
3cab0 6f 64 65 5d 20 69 73 20 53 69 6e 67 6c 65 2d 74 ode] is Single-t
3cac0 68 72 65 61 64 20 6f 72 20 4d 75 6c 74 69 2d 74 hread or Multi-t
3cad0 68 72 65 61 64 20 74 68 65 6e 20 74 68 69 73 0a hread then this.
3cae0 2a 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 ** routine retur
3caf0 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 ns a NULL pointe
3cb00 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 r..*/.SQLITE_API
3cb10 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
3cb20 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 sqlite3_db_mutex
3cb30 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a (sqlite3*);../*.
3cb40 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 77 ** CAPI3REF: Low
3cb50 2d 4c 65 76 65 6c 20 43 6f 6e 74 72 6f 6c 20 4f -Level Control O
3cb60 66 20 44 61 74 61 62 61 73 65 20 46 69 6c 65 73 f Database Files
3cb70 20 7b 48 31 31 33 30 30 7d 20 3c 53 33 30 38 30 {H11300} <S3080
3cb80 30 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 31 0>.**.** {H11301
3cb90 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 } The [sqlite3_f
3cba0 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 ile_control()] i
3cbb0 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 73 20 61 nterface makes a
3cbc0 20 64 69 72 65 63 74 20 63 61 6c 6c 20 74 6f 20 direct call to
3cbd0 74 68 65 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 the.** xFileCont
3cbe0 72 6f 6c 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 rol method for t
3cbf0 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d he [sqlite3_io_m
3cc00 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 61 ethods] object a
3cc10 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 ssociated.** wit
3cc20 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 h a particular d
3cc30 61 74 61 62 61 73 65 20 69 64 65 6e 74 69 66 69 atabase identifi
3cc40 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 ed by the second
3cc50 20 61 72 67 75 6d 65 6e 74 2e 20 7b 48 31 31 33 argument. {H113
3cc60 30 32 7d 20 54 68 65 0a 2a 2a 20 6e 61 6d 65 20 02} The.** name
3cc70 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
3cc80 69 73 20 74 68 65 20 6e 61 6d 65 20 61 73 73 69 is the name assi
3cc90 67 6e 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 gned to the data
3cca0 62 61 73 65 20 62 79 20 74 68 65 0a 2a 2a 20 3c base by the.** <
3ccb0 61 20 68 72 65 66 3d 22 6c 61 6e 67 5f 61 74 74 a href="lang_att
3ccc0 61 63 68 2e 68 74 6d 6c 22 3e 41 54 54 41 43 48 ach.html">ATTACH
3ccd0 3c 2f 61 3e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 </a> SQL command
3cce0 20 74 68 61 74 20 6f 70 65 6e 65 64 20 74 68 65 that opened the
3ccf0 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 7b 48 .** database. {H
3cd00 31 31 33 30 33 7d 20 54 6f 20 63 6f 6e 74 72 6f 11303} To contro
3cd10 6c 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 l the main datab
3cd20 61 73 65 20 66 69 6c 65 2c 20 75 73 65 20 74 68 ase file, use th
3cd30 65 20 6e 61 6d 65 20 22 6d 61 69 6e 22 0a 2a 2a e name "main".**
3cd40 20 6f 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 or a NULL point
3cd50 65 72 2e 20 7b 48 31 31 33 30 34 7d 20 54 68 65 er. {H11304} The
3cd60 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 third and fourt
3cd70 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 h parameters to
3cd80 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
3cd90 61 72 65 20 70 61 73 73 65 64 20 64 69 72 65 63 are passed direc
3cda0 74 6c 79 20 74 68 72 6f 75 67 68 20 74 6f 20 74 tly through to t
3cdb0 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68 he second and th
3cdc0 69 72 64 20 70 61 72 61 6d 65 74 65 72 73 20 6f ird parameters o
3cdd0 66 0a 2a 2a 20 74 68 65 20 78 46 69 6c 65 43 6f f.** the xFileCo
3cde0 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 20 7b ntrol method. {
3cdf0 48 31 31 33 30 35 7d 20 54 68 65 20 72 65 74 75 H11305} The retu
3ce00 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 rn value of the
3ce10 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 0a 2a 2a 20 xFileControl.**
3ce20 6d 65 74 68 6f 64 20 62 65 63 6f 6d 65 73 20 74 method becomes t
3ce30 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
3ce40 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e of this routine.
3ce50 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 36 7d 20 .**.** {H11306}
3ce60 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 If the second pa
3ce70 72 61 6d 65 74 65 72 20 28 7a 44 62 4e 61 6d 65 rameter (zDbName
3ce80 29 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 ) does not match
3ce90 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 79 the name of any
3cea0 0a 2a 2a 20 6f 70 65 6e 20 64 61 74 61 62 61 73 .** open databas
3ceb0 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51 4c e file, then SQL
3cec0 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74 ITE_ERROR is ret
3ced0 75 72 6e 65 64 2e 20 7b 48 31 31 33 30 37 7d 20 urned. {H11307}
3cee0 54 68 69 73 20 65 72 72 6f 72 0a 2a 2a 20 63 6f This error.** co
3cef0 64 65 20 69 73 20 6e 6f 74 20 72 65 6d 65 6d 62 de is not rememb
3cf00 65 72 65 64 20 61 6e 64 20 77 69 6c 6c 20 6e 6f ered and will no
3cf10 74 20 62 65 20 72 65 63 61 6c 6c 65 64 20 62 79 t be recalled by
3cf20 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 [sqlite3_errcod
3cf30 65 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 e()].** or [sqli
3cf40 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 20 7b te3_errmsg()]. {
3cf50 41 31 31 33 30 38 7d 20 54 68 65 20 75 6e 64 65 A11308} The unde
3cf60 72 6c 79 69 6e 67 20 78 46 69 6c 65 43 6f 6e 74 rlying xFileCont
3cf70 72 6f 6c 20 6d 65 74 68 6f 64 20 6d 69 67 68 74 rol method might
3cf80 0a 2a 2a 20 61 6c 73 6f 20 72 65 74 75 72 6e 20 .** also return
3cf90 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20 7b SQLITE_ERROR. {
3cfa0 41 31 31 33 30 39 7d 20 54 68 65 72 65 20 69 73 A11309} There is
3cfb0 20 6e 6f 20 77 61 79 20 74 6f 20 64 69 73 74 69 no way to disti
3cfc0 6e 67 75 69 73 68 20 62 65 74 77 65 65 6e 0a 2a nguish between.*
3cfd0 2a 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 7a * an incorrect z
3cfe0 44 62 4e 61 6d 65 20 61 6e 64 20 61 6e 20 53 51 DbName and an SQ
3cff0 4c 49 54 45 5f 45 52 52 4f 52 20 72 65 74 75 72 LITE_ERROR retur
3d000 6e 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72 n from the under
3d010 6c 79 69 6e 67 0a 2a 2a 20 78 46 69 6c 65 43 6f lying.** xFileCo
3d020 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 7b 45 ntrol method. {E
3d030 4e 44 7d 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c ND}.**.** See al
3d040 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 so: [SQLITE_FCNT
3d050 4c 5f 4c 4f 43 4b 53 54 41 54 45 5d 0a 2a 2f 0a L_LOCKSTATE].*/.
3d060 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
3d070 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 qlite3_file_cont
3d080 72 6f 6c 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f rol(sqlite3*, co
3d090 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d nst char *zDbNam
3d0a0 65 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 2a e, int op, void*
3d0b0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
3d0c0 45 46 3a 20 54 65 73 74 69 6e 67 20 49 6e 74 65 EF: Testing Inte
3d0d0 72 66 61 63 65 20 7b 48 31 31 34 30 30 7d 20 3c rface {H11400} <
3d0e0 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S30800>.**.** Th
3d0f0 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 e sqlite3_test_c
3d100 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 61 ontrol() interfa
3d110 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 ce is used to re
3d120 61 64 20 6f 75 74 20 69 6e 74 65 72 6e 61 6c 0a ad out internal.
3d130 2a 2a 20 73 74 61 74 65 20 6f 66 20 53 51 4c 69 ** state of SQLi
3d140 74 65 20 61 6e 64 20 74 6f 20 69 6e 6a 65 63 74 te and to inject
3d150 20 66 61 75 6c 74 73 20 69 6e 74 6f 20 53 51 4c faults into SQL
3d160 69 74 65 20 66 6f 72 20 74 65 73 74 69 6e 67 0a ite for testing.
3d170 2a 2a 20 70 75 72 70 6f 73 65 73 2e 20 20 54 68 ** purposes. Th
3d180 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
3d190 72 20 69 73 20 61 6e 20 6f 70 65 72 61 74 69 6f r is an operatio
3d1a0 6e 20 63 6f 64 65 20 74 68 61 74 20 64 65 74 65 n code that dete
3d1b0 72 6d 69 6e 65 73 0a 2a 2a 20 74 68 65 20 6e 75 rmines.** the nu
3d1c0 6d 62 65 72 2c 20 6d 65 61 6e 69 6e 67 2c 20 61 mber, meaning, a
3d1d0 6e 64 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 nd operation of
3d1e0 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 70 all subsequent p
3d1f0 61 72 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a arameters..**.**
3d200 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 This interface
3d210 69 73 20 6e 6f 74 20 66 6f 72 20 75 73 65 20 62 is not for use b
3d220 79 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 20 y applications.
3d230 20 49 74 20 65 78 69 73 74 73 20 73 6f 6c 65 6c It exists solel
3d240 79 0a 2a 2a 20 66 6f 72 20 76 65 72 69 66 79 69 y.** for verifyi
3d250 6e 67 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f ng the correct o
3d260 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 peration of the
3d270 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20 SQLite library.
3d280 20 44 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e Depending.** on
3d290 20 68 6f 77 20 74 68 65 20 53 51 4c 69 74 65 20 how the SQLite
3d2a0 6c 69 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69 library is compi
3d2b0 6c 65 64 2c 20 74 68 69 73 20 69 6e 74 65 72 66 led, this interf
3d2c0 61 63 65 20 6d 69 67 68 74 20 6e 6f 74 20 65 78 ace might not ex
3d2d0 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 ist..**.** The d
3d2e0 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 6f 70 etails of the op
3d2f0 65 72 61 74 69 6f 6e 20 63 6f 64 65 73 2c 20 74 eration codes, t
3d300 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 2c 20 74 heir meanings, t
3d310 68 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a he parameters.**
3d320 20 74 68 65 79 20 74 61 6b 65 2c 20 61 6e 64 20 they take, and
3d330 77 68 61 74 20 74 68 65 79 20 64 6f 20 61 72 65 what they do are
3d340 20 61 6c 6c 20 73 75 62 6a 65 63 74 20 74 6f 20 all subject to
3d350 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e change without n
3d360 6f 74 69 63 65 2e 0a 2a 2a 20 55 6e 6c 69 6b 65 otice..** Unlike
3d370 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 53 51 4c most of the SQL
3d380 69 74 65 20 41 50 49 2c 20 74 68 69 73 20 66 75 ite API, this fu
3d390 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67 75 nction is not gu
3d3a0 61 72 61 6e 74 65 65 64 20 74 6f 0a 2a 2a 20 6f aranteed to.** o
3d3b0 70 65 72 61 74 65 20 63 6f 6e 73 69 73 74 65 6e perate consisten
3d3c0 74 6c 79 20 66 72 6f 6d 20 6f 6e 65 20 72 65 6c tly from one rel
3d3d0 65 61 73 65 20 74 6f 20 74 68 65 20 6e 65 78 74 ease to the next
3d3e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
3d3f0 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 65 73 74 int sqlite3_test
3d400 5f 63 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f 70 2c _control(int op,
3d410 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ...);../*.** CA
3d420 50 49 33 52 45 46 3a 20 54 65 73 74 69 6e 67 20 PI3REF: Testing
3d430 49 6e 74 65 72 66 61 63 65 20 4f 70 65 72 61 74 Interface Operat
3d440 69 6f 6e 20 43 6f 64 65 73 20 7b 48 31 31 34 31 ion Codes {H1141
3d450 30 7d 20 3c 48 31 31 34 30 30 3e 0a 2a 2a 0a 2a 0} <H11400>.**.*
3d460 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 * These constant
3d470 73 20 61 72 65 20 74 68 65 20 76 61 6c 69 64 20 s are the valid
3d480 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 20 70 operation code p
3d490 61 72 61 6d 65 74 65 72 73 20 75 73 65 64 0a 2a arameters used.*
3d4a0 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 * as the first a
3d4b0 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69 rgument to [sqli
3d4c0 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c te3_test_control
3d4d0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 ()]..**.** These
3d4e0 20 70 61 72 61 6d 65 74 65 72 73 20 61 6e 64 20 parameters and
3d4f0 74 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 20 61 their meanings a
3d500 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 re subject to ch
3d510 61 6e 67 65 0a 2a 2a 20 77 69 74 68 6f 75 74 20 ange.** without
3d520 6e 6f 74 69 63 65 2e 20 20 54 68 65 73 65 20 76 notice. These v
3d530 61 6c 75 65 73 20 61 72 65 20 66 6f 72 20 74 65 alues are for te
3d540 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f sting purposes o
3d550 6e 6c 79 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74 nly..** Applicat
3d560 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 ions should not
3d570 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 use any of these
3d580 20 70 61 72 61 6d 65 74 65 72 73 20 6f 72 20 74 parameters or t
3d590 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 74 he.** [sqlite3_t
3d5a0 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 est_control()] i
3d5b0 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64 65 nterface..*/.#de
3d5c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 fine SQLITE_TEST
3d5d0 43 54 52 4c 5f 50 52 4e 47 5f 53 41 56 45 20 20 CTRL_PRNG_SAVE
3d5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 5.
3d5f0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 #define SQLITE_T
3d600 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 ESTCTRL_PRNG_RES
3d610 54 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20 TORE
3d620 20 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 6.#define SQLIT
3d630 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f E_TESTCTRL_PRNG_
3d640 52 45 53 45 54 20 20 20 20 20 20 20 20 20 20 20 RESET
3d650 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51 7.#define SQ
3d660 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 49 LITE_TESTCTRL_BI
3d670 54 56 45 43 5f 54 45 53 54 20 20 20 20 20 20 20 TVEC_TEST
3d680 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 8.#define
3d690 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c SQLITE_TESTCTRL
3d6a0 5f 46 41 55 4c 54 5f 49 4e 53 54 41 4c 4c 20 20 _FAULT_INSTALL
3d6b0 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 9.#def
3d6c0 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 ine SQLITE_TESTC
3d6d0 54 52 4c 5f 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f TRL_BENIGN_MALLO
3d6e0 43 5f 48 4f 4f 4b 53 20 20 20 20 20 31 30 0a 23 C_HOOKS 10.#
3d6f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 define SQLITE_TE
3d700 53 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47 5f 42 STCTRL_PENDING_B
3d710 59 54 45 20 20 20 20 20 20 20 20 20 20 20 20 31 YTE 1
3d720 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 1.#define SQLITE
3d730 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45 52 54 _TESTCTRL_ASSERT
3d740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3d750 20 20 31 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 12.#define SQL
3d760 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 4c 57 ITE_TESTCTRL_ALW
3d770 41 59 53 20 20 20 20 20 20 20 20 20 20 20 20 20 AYS
3d780 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20 13.#define
3d790 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f SQLITE_TESTCTRL_
3d7a0 52 45 53 45 52 56 45 20 20 20 20 20 20 20 20 20 RESERVE
3d7b0 20 20 20 20 20 20 20 20 31 34 0a 0a 2f 2a 0a 2a 14../*.*
3d7c0 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c 69 * CAPI3REF: SQLi
3d7d0 74 65 20 52 75 6e 74 69 6d 65 20 53 74 61 74 75 te Runtime Statu
3d7e0 73 20 7b 48 31 37 32 30 30 7d 20 3c 53 36 30 32 s {H17200} <S602
3d7f0 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 00>.** EXPERIMEN
3d800 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 TAL.**.** This i
3d810 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 nterface is used
3d820 20 74 6f 20 72 65 74 72 69 65 76 65 20 72 75 6e to retrieve run
3d830 74 69 6d 65 20 73 74 61 74 75 73 20 69 6e 66 6f time status info
3d840 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 rmation.** about
3d850 20 74 68 65 20 70 72 65 66 6f 72 6d 61 6e 63 65 the preformance
3d860 20 6f 66 20 53 51 4c 69 74 65 2c 20 61 6e 64 20 of SQLite, and
3d870 6f 70 74 69 6f 6e 61 6c 6c 79 20 74 6f 20 72 65 optionally to re
3d880 73 65 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 68 set various.** h
3d890 69 67 68 77 61 74 65 72 20 6d 61 72 6b 73 2e 20 ighwater marks.
3d8a0 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d The first argum
3d8b0 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 67 65 ent is an intege
3d8c0 72 20 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 74 68 r code for.** th
3d8d0 65 20 73 70 65 63 69 66 69 63 20 70 61 72 61 6d e specific param
3d8e0 65 74 65 72 20 74 6f 20 6d 65 61 73 75 72 65 2e eter to measure.
3d8f0 20 20 52 65 63 6f 67 6e 69 7a 65 64 20 69 6e 74 Recognized int
3d900 65 67 65 72 20 63 6f 64 65 73 0a 2a 2a 20 61 72 eger codes.** ar
3d910 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5b 53 e of the form [S
3d920 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d QLITE_STATUS_MEM
3d930 4f 52 59 5f 55 53 45 44 20 7c 20 53 51 4c 49 54 ORY_USED | SQLIT
3d940 45 5f 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e 0a 2a E_STATUS_...]..*
3d950 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 76 61 * The current va
3d960 6c 75 65 20 6f 66 20 74 68 65 20 70 61 72 61 6d lue of the param
3d970 65 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 eter is returned
3d980 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e into *pCurrent.
3d990 0a 2a 2a 20 54 68 65 20 68 69 67 68 65 73 74 20 .** The highest
3d9a0 72 65 63 6f 72 64 65 64 20 76 61 6c 75 65 20 69 recorded value i
3d9b0 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 s returned in *p
3d9c0 48 69 67 68 77 61 74 65 72 2e 20 20 49 66 20 74 Highwater. If t
3d9d0 68 65 0a 2a 2a 20 72 65 73 65 74 46 6c 61 67 20 he.** resetFlag
3d9e0 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 is true, then th
3d9f0 65 20 68 69 67 68 65 73 74 20 72 65 63 6f 72 64 e highest record
3da00 20 76 61 6c 75 65 20 69 73 20 72 65 73 65 74 20 value is reset
3da10 61 66 74 65 72 0a 2a 2a 20 2a 70 48 69 67 68 77 after.** *pHighw
3da20 61 74 65 72 20 69 73 20 77 72 69 74 74 65 6e 2e ater is written.
3da30 20 53 6f 6d 65 20 70 61 72 61 6d 65 74 65 72 73 Some parameters
3da40 20 64 6f 20 6e 6f 74 20 72 65 63 6f 72 64 20 74 do not record t
3da50 68 65 20 68 69 67 68 65 73 74 0a 2a 2a 20 76 61 he highest.** va
3da60 6c 75 65 2e 20 20 46 6f 72 20 74 68 6f 73 65 20 lue. For those
3da70 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6e 6f parameters.** no
3da80 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e thing is written
3da90 20 69 6e 74 6f 20 2a 70 48 69 67 68 77 61 74 65 into *pHighwate
3daa0 72 20 61 6e 64 20 74 68 65 20 72 65 73 65 74 46 r and the resetF
3dab0 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a lag is ignored..
3dac0 2a 2a 20 4f 74 68 65 72 20 70 61 72 61 6d 65 74 ** Other paramet
3dad0 65 72 73 20 72 65 63 6f 72 64 20 6f 6e 6c 79 20 ers record only
3dae0 74 68 65 20 68 69 67 68 77 61 74 65 72 20 6d 61 the highwater ma
3daf0 72 6b 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 63 rk and not the c
3db00 75 72 72 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e urrent.** value.
3db10 20 20 46 6f 72 20 74 68 65 73 65 20 6c 61 74 74 For these latt
3db20 65 72 20 70 61 72 61 6d 65 74 65 72 73 20 6e 6f er parameters no
3db30 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e thing is written
3db40 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e into *pCurrent.
3db50 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
3db60 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 ine returns SQLI
3db70 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 TE_OK on success
3db80 20 61 6e 64 20 61 20 6e 6f 6e 2d 7a 65 72 6f 0a and a non-zero.
3db90 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 ** [error code]
3dba0 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a on failure..**.*
3dbb0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
3dbc0 73 20 74 68 72 65 61 64 73 61 66 65 20 62 75 74 s threadsafe but
3dbd0 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2e 20 is not atomic.
3dbe0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 This routine ca
3dbf0 6e 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 n be.** called w
3dc00 68 69 6c 65 20 6f 74 68 65 72 20 74 68 72 65 61 hile other threa
3dc10 64 73 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 74 ds are running t
3dc20 68 65 20 73 61 6d 65 20 6f 72 20 64 69 66 66 65 he same or diffe
3dc30 72 65 6e 74 20 53 51 4c 69 74 65 0a 2a 2a 20 69 rent SQLite.** i
3dc40 6e 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77 65 nterfaces. Howe
3dc50 76 65 72 20 74 68 65 20 76 61 6c 75 65 73 20 72 ver the values r
3dc60 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 43 75 72 eturned in *pCur
3dc70 72 65 6e 74 20 61 6e 64 0a 2a 2a 20 2a 70 48 69 rent and.** *pHi
3dc80 67 68 77 61 74 65 72 20 72 65 66 6c 65 63 74 20 ghwater reflect
3dc90 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 53 51 the status of SQ
3dca0 4c 69 74 65 20 61 74 20 64 69 66 66 65 72 65 6e Lite at differen
3dcb0 74 20 70 6f 69 6e 74 73 20 69 6e 20 74 69 6d 65 t points in time
3dcc0 0a 2a 2a 20 61 6e 64 20 69 74 20 69 73 20 70 6f .** and it is po
3dcd0 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 ssible that anot
3dce0 68 65 72 20 74 68 72 65 61 64 20 6d 69 67 68 74 her thread might
3dcf0 20 63 68 61 6e 67 65 20 74 68 65 20 70 61 72 61 change the para
3dd00 6d 65 74 65 72 0a 2a 2a 20 69 6e 20 62 65 74 77 meter.** in betw
3dd10 65 65 6e 20 74 68 65 20 74 69 6d 65 73 20 77 68 een the times wh
3dd20 65 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e 64 en *pCurrent and
3dd30 20 2a 70 48 69 67 68 77 61 74 65 72 20 61 72 65 *pHighwater are
3dd40 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 written..**.**
3dd50 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 See also: [sqlit
3dd60 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 0a e3_db_status()].
3dd70 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 */.SQLITE_API SQ
3dd80 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 LITE_EXPERIMENTA
3dd90 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 L int sqlite3_st
3dda0 61 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 atus(int op, int
3ddb0 20 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 *pCurrent, int
3ddc0 2a 70 48 69 67 68 77 61 74 65 72 2c 20 69 6e 74 *pHighwater, int
3ddd0 20 72 65 73 65 74 46 6c 61 67 29 3b 0a 0a 0a 2f resetFlag);.../
3dde0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 *.** CAPI3REF: S
3ddf0 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 73 tatus Parameters
3de00 20 7b 48 31 37 32 35 30 7d 20 3c 48 31 37 32 30 {H17250} <H1720
3de10 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
3de20 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 AL.**.** These i
3de30 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 nteger constants
3de40 20 64 65 73 69 67 6e 61 74 65 20 76 61 72 69 6f designate vario
3de50 75 73 20 72 75 6e 2d 74 69 6d 65 20 73 74 61 74 us run-time stat
3de60 75 73 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a us parameters.**
3de70 20 74 68 61 74 20 63 61 6e 20 62 65 20 72 65 74 that can be ret
3de80 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 urned by [sqlite
3de90 33 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a 3_status()]..**.
3dea0 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 ** <dl>.** <dt>S
3deb0 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d QLITE_STATUS_MEM
3dec0 4f 52 59 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a ORY_USED</dt>.**
3ded0 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 <dd>This parame
3dee0 74 65 72 20 69 73 20 74 68 65 20 63 75 72 72 65 ter is the curre
3def0 6e 74 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d nt amount of mem
3df00 6f 72 79 20 63 68 65 63 6b 65 64 20 6f 75 74 0a ory checked out.
3df10 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 ** using [sqlite
3df20 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 65 69 74 3_malloc()], eit
3df30 68 65 72 20 64 69 72 65 63 74 6c 79 20 6f 72 20 her directly or
3df40 69 6e 64 69 72 65 63 74 6c 79 2e 20 20 54 68 65 indirectly. The
3df50 0a 2a 2a 20 66 69 67 75 72 65 20 69 6e 63 6c 75 .** figure inclu
3df60 64 65 73 20 63 61 6c 6c 73 20 6d 61 64 65 20 74 des calls made t
3df70 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f o [sqlite3_mallo
3df80 63 28 29 5d 20 62 79 20 74 68 65 20 61 70 70 6c c()] by the appl
3df90 69 63 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 69 ication.** and i
3dfa0 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 75 nternal memory u
3dfb0 73 61 67 65 20 62 79 20 74 68 65 20 53 51 4c 69 sage by the SQLi
3dfc0 74 65 20 6c 69 62 72 61 72 79 2e 20 20 53 63 72 te library. Scr
3dfd0 61 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 63 atch memory.** c
3dfe0 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b 53 51 ontrolled by [SQ
3dff0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 LITE_CONFIG_SCRA
3e000 54 43 48 5d 20 61 6e 64 20 61 75 78 69 6c 69 61 TCH] and auxilia
3e010 72 79 20 70 61 67 65 2d 63 61 63 68 65 0a 2a 2a ry page-cache.**
3e020 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 72 6f 6c 6c memory controll
3e030 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f ed by [SQLITE_CO
3e040 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 20 NFIG_PAGECACHE]
3e050 69 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 is not included
3e060 69 6e 0a 2a 2a 20 74 68 69 73 20 70 61 72 61 6d in.** this param
3e070 65 74 65 72 2e 20 20 54 68 65 20 61 6d 6f 75 6e eter. The amoun
3e080 74 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 t returned is th
3e090 65 20 73 75 6d 20 6f 66 20 74 68 65 20 61 6c 6c e sum of the all
3e0a0 6f 63 61 74 69 6f 6e 0a 2a 2a 20 73 69 7a 65 73 ocation.** sizes
3e0b0 20 61 73 20 72 65 70 6f 72 74 65 64 20 62 79 20 as reported by
3e0c0 74 68 65 20 78 53 69 7a 65 20 6d 65 74 68 6f 64 the xSize method
3e0d0 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d in [sqlite3_mem
3e0e0 5f 6d 65 74 68 6f 64 73 5d 2e 3c 2f 64 64 3e 0a _methods].</dd>.
3e0f0 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
3e100 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 _STATUS_MALLOC_S
3e110 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e IZE</dt>.** <dd>
3e120 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 This parameter r
3e130 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 ecords the large
3e140 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 st memory alloca
3e150 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 tion request.**
3e160 68 61 6e 64 65 64 20 74 6f 20 5b 73 71 6c 69 74 handed to [sqlit
3e170 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 e3_malloc()] or
3e180 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 [sqlite3_realloc
3e190 28 29 5d 20 28 6f 72 20 74 68 65 69 72 0a 2a 2a ()] (or their.**
3e1a0 20 69 6e 74 65 72 6e 61 6c 20 65 71 75 69 76 61 internal equiva
3e1b0 6c 65 6e 74 73 29 2e 20 20 4f 6e 6c 79 20 74 68 lents). Only th
3e1c0 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
3e1d0 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 in the.** *pHig
3e1e0 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 72 hwater parameter
3e1f0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 to [sqlite3_sta
3e200 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 tus()] is of int
3e210 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 erest. .** The
3e220 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e value written in
3e230 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74 to the *pCurrent
3e240 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e parameter is un
3e250 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a defined.</dd>.**
3e260 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 .** <dt>SQLITE_S
3e270 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f TATUS_PAGECACHE_
3e280 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 USED</dt>.** <dd
3e290 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 >This parameter
3e2a0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 returns the numb
3e2b0 65 72 20 6f 66 20 70 61 67 65 73 20 75 73 65 64 er of pages used
3e2c0 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 5b out of the.** [
3e2d0 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79 pagecache memory
3e2e0 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 74 68 61 74 allocator] that
3e2f0 20 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 20 was configured
3e300 75 73 69 6e 67 20 0a 2a 2a 20 5b 53 51 4c 49 54 using .** [SQLIT
3e310 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 E_CONFIG_PAGECAC
3e320 48 45 5d 2e 20 20 54 68 65 0a 2a 2a 20 76 61 6c HE]. The.** val
3e330 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 69 ue returned is i
3e340 6e 20 70 61 67 65 73 2c 20 6e 6f 74 20 69 6e 20 n pages, not in
3e350 62 79 74 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a bytes.</dd>.**.*
3e360 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 * <dt>SQLITE_STA
3e370 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 TUS_PAGECACHE_OV
3e380 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c ERFLOW</dt>.** <
3e390 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 dd>This paramete
3e3a0 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 r returns the nu
3e3b0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
3e3c0 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 61 page cache.** a
3e3d0 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 20 llocation which
3e3e0 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 61 could not be sta
3e3f0 74 69 73 66 69 65 64 20 62 79 20 74 68 65 20 5b tisfied by the [
3e400 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 SQLITE_CONFIG_PA
3e410 47 45 43 41 43 48 45 5d 0a 2a 2a 20 62 75 66 66 GECACHE].** buff
3e420 65 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f 72 er and where for
3e430 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 ced to overflow
3e440 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c to [sqlite3_mall
3e450 6f 63 28 29 5d 2e 20 20 54 68 65 0a 2a 2a 20 72 oc()]. The.** r
3e460 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 6e eturned value in
3e470 63 6c 75 64 65 73 20 61 6c 6c 6f 63 61 74 69 6f cludes allocatio
3e480 6e 73 20 74 68 61 74 20 6f 76 65 72 66 6c 6f 77 ns that overflow
3e490 65 64 20 62 65 63 61 75 73 65 20 74 68 65 79 0a ed because they.
3e4a0 2a 2a 20 77 68 65 72 65 20 74 6f 6f 20 6c 61 72 ** where too lar
3e4b0 67 65 20 28 74 68 65 79 20 77 65 72 65 20 6c 61 ge (they were la
3e4c0 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 22 73 rger than the "s
3e4d0 7a 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a z" parameter to.
3e4e0 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ** [SQLITE_CONFI
3e4f0 47 5f 50 41 47 45 43 41 43 48 45 5d 29 20 61 6e G_PAGECACHE]) an
3e500 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68 d allocations th
3e510 61 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65 at overflowed be
3e520 63 61 75 73 65 0a 2a 2a 20 6e 6f 20 73 70 61 63 cause.** no spac
3e530 65 20 77 61 73 20 6c 65 66 74 20 69 6e 20 74 68 e was left in th
3e540 65 20 70 61 67 65 20 63 61 63 68 65 2e 3c 2f 64 e page cache.</d
3e550 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
3e560 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 ITE_STATUS_PAGEC
3e570 41 43 48 45 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a ACHE_SIZE</dt>.*
3e580 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d * <dd>This param
3e590 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 65 eter records the
3e5a0 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 largest memory
3e5b0 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 allocation reque
3e5c0 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 st.** handed to
3e5d0 5b 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 [pagecache memor
3e5e0 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f y allocator]. O
3e5f0 6e 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72 65 nly the value re
3e600 74 75 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a turned in the.**
3e610 20 2a 70 48 69 67 68 77 61 74 65 72 20 70 61 72 *pHighwater par
3e620 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 ameter to [sqlit
3e630 65 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73 20 e3_status()] is
3e640 6f 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a of interest. .*
3e650 2a 20 54 68 65 20 76 61 6c 75 65 20 77 72 69 74 * The value writ
3e660 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70 43 ten into the *pC
3e670 75 72 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72 urrent parameter
3e680 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f is undefined.</
3e690 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
3e6a0 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 LITE_STATUS_SCRA
3e6b0 54 43 48 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a TCH_USED</dt>.**
3e6c0 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 <dd>This parame
3e6d0 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 ter returns the
3e6e0 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 number of alloca
3e6f0 74 69 6f 6e 73 20 75 73 65 64 20 6f 75 74 20 6f tions used out o
3e700 66 20 74 68 65 0a 2a 2a 20 5b 73 63 72 61 74 63 f the.** [scratc
3e710 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 h memory allocat
3e720 6f 72 5d 20 63 6f 6e 66 69 67 75 72 65 64 20 75 or] configured u
3e730 73 69 6e 67 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f sing.** [SQLITE_
3e740 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 2e CONFIG_SCRATCH].
3e750 20 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 The value retu
3e760 72 6e 65 64 20 69 73 20 69 6e 20 61 6c 6c 6f 63 rned is in alloc
3e770 61 74 69 6f 6e 73 2c 20 6e 6f 74 0a 2a 2a 20 69 ations, not.** i
3e780 6e 20 62 79 74 65 73 2e 20 20 53 69 6e 63 65 20 n bytes. Since
3e790 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 20 a single thread
3e7a0 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 6f 6e may only have on
3e7b0 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 e scratch alloca
3e7c0 74 69 6f 6e 0a 2a 2a 20 6f 75 74 73 74 61 6e 64 tion.** outstand
3e7d0 69 6e 67 20 61 74 20 74 69 6d 65 2c 20 74 68 69 ing at time, thi
3e7e0 73 20 70 61 72 61 6d 65 74 65 72 20 61 6c 73 6f s parameter also
3e7f0 20 72 65 70 6f 72 74 73 20 74 68 65 20 6e 75 6d reports the num
3e800 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 0a 2a ber of threads.*
3e810 2a 20 75 73 69 6e 67 20 73 63 72 61 74 63 68 20 * using scratch
3e820 6d 65 6d 6f 72 79 20 61 74 20 74 68 65 20 73 61 memory at the sa
3e830 6d 65 20 74 69 6d 65 2e 3c 2f 64 64 3e 0a 2a 2a me time.</dd>.**
3e840 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 .** <dt>SQLITE_S
3e850 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 TATUS_SCRATCH_OV
3e860 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c ERFLOW</dt>.** <
3e870 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 dd>This paramete
3e880 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 r returns the nu
3e890 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
3e8a0 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 0a scratch memory.
3e8b0 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 ** allocation wh
3e8c0 69 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 ich could not be
3e8d0 20 73 74 61 74 69 73 66 69 65 64 20 62 79 20 74 statisfied by t
3e8e0 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 he [SQLITE_CONFI
3e8f0 47 5f 53 43 52 41 54 43 48 5d 0a 2a 2a 20 62 75 G_SCRATCH].** bu
3e900 66 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66 ffer and where f
3e910 6f 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f orced to overflo
3e920 77 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 w to [sqlite3_ma
3e930 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20 76 61 lloc()]. The va
3e940 6c 75 65 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 lues.** returned
3e950 20 69 6e 63 6c 75 64 65 20 6f 76 65 72 66 6c 6f include overflo
3e960 77 73 20 62 65 63 61 75 73 65 20 74 68 65 20 72 ws because the r
3e970 65 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 equested allocat
3e980 69 6f 6e 20 77 61 73 20 74 6f 6f 0a 2a 2a 20 6c ion was too.** l
3e990 61 72 67 65 72 20 28 74 68 61 74 20 69 73 2c 20 arger (that is,
3e9a0 62 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75 because the requ
3e9b0 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e ested allocation
3e9c0 20 77 61 73 20 6c 61 72 67 65 72 20 74 68 61 6e was larger than
3e9d0 20 74 68 65 0a 2a 2a 20 22 73 7a 22 20 70 61 72 the.** "sz" par
3e9e0 61 6d 65 74 65 72 20 74 6f 20 5b 53 51 4c 49 54 ameter to [SQLIT
3e9f0 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 E_CONFIG_SCRATCH
3ea00 5d 29 20 61 6e 64 20 62 65 63 61 75 73 65 20 6e ]) and because n
3ea10 6f 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72 o scratch buffer
3ea20 0a 2a 2a 20 73 6c 6f 74 73 20 77 65 72 65 20 61 .** slots were a
3ea30 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 3c 2f 64 vailable..** </d
3ea40 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
3ea50 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 ITE_STATUS_SCRAT
3ea60 43 48 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 CH_SIZE</dt>.**
3ea70 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 <dd>This paramet
3ea80 65 72 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c er records the l
3ea90 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c argest memory al
3eaa0 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 location request
3eab0 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b 73 .** handed to [s
3eac0 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61 6c cratch memory al
3ead0 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 locator]. Only
3eae0 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
3eaf0 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 ed in the.** *pH
3eb00 69 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 ighwater paramet
3eb10 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 er to [sqlite3_s
3eb20 74 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 tatus()] is of i
3eb30 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 nterest. .** Th
3eb40 65 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 e value written
3eb50 69 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 into the *pCurre
3eb60 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 nt parameter is
3eb70 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a undefined.</dd>.
3eb80 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
3eb90 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53 _STATUS_PARSER_S
3eba0 54 41 43 4b 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 TACK</dt>.** <dd
3ebb0 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 >This parameter
3ebc0 72 65 63 6f 72 64 73 20 74 68 65 20 64 65 65 70 records the deep
3ebd0 65 73 74 20 70 61 72 73 65 72 20 73 74 61 63 6b est parser stack
3ebe0 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 0a 2a 2a . It is only.**
3ebf0 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 53 meaningful if S
3ec00 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 QLite is compile
3ec10 64 20 77 69 74 68 20 5b 59 59 54 52 41 43 4b 4d d with [YYTRACKM
3ec20 41 58 53 54 41 43 4b 44 45 50 54 48 5d 2e 3c 2f AXSTACKDEPTH].</
3ec30 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a dd>.** </dl>.**.
3ec40 2a 2a 20 4e 65 77 20 73 74 61 74 75 73 20 70 61 ** New status pa
3ec50 72 61 6d 65 74 65 72 73 20 6d 61 79 20 62 65 20 rameters may be
3ec60 61 64 64 65 64 20 66 72 6f 6d 20 74 69 6d 65 20 added from time
3ec70 74 6f 20 74 69 6d 65 2e 0a 2a 2f 0a 23 64 65 66 to time..*/.#def
3ec80 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 ine SQLITE_STATU
3ec90 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 20 20 20 S_MEMORY_USED
3eca0 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 0.#define
3ecb0 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 SQLITE_STATUS_P
3ecc0 41 47 45 43 41 43 48 45 5f 55 53 45 44 20 20 20 AGECACHE_USED
3ecd0 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 1.#define SQ
3ece0 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 LITE_STATUS_PAGE
3ecf0 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 20 20 CACHE_OVERFLOW
3ed00 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 2.#define SQLIT
3ed10 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 E_STATUS_SCRATCH
3ed20 5f 55 53 45 44 20 20 20 20 20 20 20 20 20 33 0a _USED 3.
3ed30 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
3ed40 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 TATUS_SCRATCH_OV
3ed50 45 52 46 4c 4f 57 20 20 20 20 20 34 0a 23 64 65 ERFLOW 4.#de
3ed60 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 fine SQLITE_STAT
3ed70 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 20 20 US_MALLOC_SIZE
3ed80 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 5.#defin
3ed90 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f e SQLITE_STATUS_
3eda0 50 41 52 53 45 52 5f 53 54 41 43 4b 20 20 20 20 PARSER_STACK
3edb0 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 53 6.#define S
3edc0 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 QLITE_STATUS_PAG
3edd0 45 43 41 43 48 45 5f 53 49 5a 45 20 20 20 20 20 ECACHE_SIZE
3ede0 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 7.#define SQLI
3edf0 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 TE_STATUS_SCRATC
3ee00 48 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20 38 H_SIZE 8
3ee10 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
3ee20 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 : Database Conne
3ee30 63 74 69 6f 6e 20 53 74 61 74 75 73 20 7b 48 31 ction Status {H1
3ee40 37 35 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 7500} <S60200>.*
3ee50 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
3ee60 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 *.** This interf
3ee70 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 ace is used to r
3ee80 65 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65 20 etrieve runtime
3ee90 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 status informati
3eea0 6f 6e 20 0a 2a 2a 20 61 62 6f 75 74 20 61 20 73 on .** about a s
3eeb0 69 6e 67 6c 65 20 5b 64 61 74 61 62 61 73 65 20 ingle [database
3eec0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54 68 connection]. Th
3eed0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
3eee0 20 69 73 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 is the.** datab
3eef0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f ase connection o
3ef00 62 6a 65 63 74 20 74 6f 20 62 65 20 69 6e 74 65 bject to be inte
3ef10 72 72 6f 67 61 74 65 64 2e 20 20 54 68 65 20 73 rrogated. The s
3ef20 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a econd argument.*
3ef30 2a 20 69 73 20 74 68 65 20 70 61 72 61 6d 65 74 * is the paramet
3ef40 65 72 20 74 6f 20 69 6e 74 65 72 72 6f 67 61 74 er to interrogat
3ef50 65 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 e. Currently, t
3ef60 68 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 he only allowed
3ef70 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65 value.** for the
3ef80 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
3ef90 72 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 53 r is [SQLITE_DBS
3efa0 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f TATUS_LOOKASIDE_
3efb0 55 53 45 44 5d 2e 0a 2a 2a 20 41 64 64 69 74 69 USED]..** Additi
3efc0 6f 6e 61 6c 20 6f 70 74 69 6f 6e 73 20 77 69 6c onal options wil
3efd0 6c 20 6c 69 6b 65 6c 79 20 61 70 70 65 61 72 20 l likely appear
3efe0 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 in future releas
3eff0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a es of SQLite..**
3f000 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 .** The current
3f010 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65 71 value of the req
3f020 75 65 73 74 65 64 20 70 61 72 61 6d 65 74 65 72 uested parameter
3f030 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f is written into
3f040 20 2a 70 43 75 72 0a 2a 2a 20 61 6e 64 20 74 68 *pCur.** and th
3f050 65 20 68 69 67 68 65 73 74 20 69 6e 73 74 61 6e e highest instan
3f060 74 61 6e 65 6f 75 73 20 76 61 6c 75 65 20 69 73 taneous value is
3f070 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 written into *p
3f080 48 69 77 74 72 2e 20 20 49 66 0a 2a 2a 20 74 68 Hiwtr. If.** th
3f090 65 20 72 65 73 65 74 46 6c 67 20 69 73 20 74 72 e resetFlg is tr
3f0a0 75 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69 67 ue, then the hig
3f0b0 68 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f hest instantaneo
3f0c0 75 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 72 us value is.** r
3f0d0 65 73 65 74 20 62 61 63 6b 20 64 6f 77 6e 20 74 eset back down t
3f0e0 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 o the current va
3f0f0 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 lue..**.** See a
3f100 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 lso: [sqlite3_st
3f110 61 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c atus()] and [sql
3f120 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 ite3_stmt_status
3f130 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ()]..*/.SQLITE_A
3f140 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 PI SQLITE_EXPERI
3f150 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 MENTAL int sqlit
3f160 65 33 5f 64 62 5f 73 74 61 74 75 73 28 73 71 6c e3_db_status(sql
3f170 69 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 69 ite3*, int op, i
3f180 6e 74 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 nt *pCur, int *p
3f190 48 69 77 74 72 2c 20 69 6e 74 20 72 65 73 65 74 Hiwtr, int reset
3f1a0 46 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 Flg);../*.** CAP
3f1b0 49 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 61 I3REF: Status Pa
3f1c0 72 61 6d 65 74 65 72 73 20 66 6f 72 20 64 61 74 rameters for dat
3f1d0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
3f1e0 73 20 7b 48 31 37 35 32 30 7d 20 3c 48 31 37 35 s {H17520} <H175
3f1f0 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 00>.** EXPERIMEN
3f200 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 TAL.**.** These
3f210 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68 constants are th
3f220 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 e available inte
3f230 67 65 72 20 22 76 65 72 62 73 22 20 74 68 61 74 ger "verbs" that
3f240 20 63 61 6e 20 62 65 20 70 61 73 73 65 64 20 61 can be passed a
3f250 73 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 s.** the second
3f260 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
3f270 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 [sqlite3_db_stat
3f280 75 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e us()] interface.
3f290 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 76 65 72 62 73 .**.** New verbs
3f2a0 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e may be added in
3f2b0 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 future releases
3f2c0 20 6f 66 20 53 51 4c 69 74 65 2e 20 45 78 69 73 of SQLite. Exis
3f2d0 74 69 6e 67 20 76 65 72 62 73 0a 2a 2a 20 6d 69 ting verbs.** mi
3f2e0 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 6e ght be discontin
3f2f0 75 65 64 2e 20 41 70 70 6c 69 63 61 74 69 6f 6e ued. Application
3f300 73 20 73 68 6f 75 6c 64 20 63 68 65 63 6b 20 74 s should check t
3f310 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66 he return code f
3f320 72 6f 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f rom.** [sqlite3_
3f330 64 62 5f 73 74 61 74 75 73 28 29 5d 20 74 6f 20 db_status()] to
3f340 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 make sure that t
3f350 68 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e 0a he call worked..
3f360 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f ** The [sqlite3_
3f370 64 62 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 db_status()] int
3f380 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74 75 erface will retu
3f390 72 6e 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 65 72 rn a non-zero er
3f3a0 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 66 20 61 ror code.** if a
3f3b0 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72 discontinued or
3f3c0 20 75 6e 73 75 70 70 6f 72 74 65 64 20 76 65 72 unsupported ver
3f3d0 62 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a b is invoked..**
3f3e0 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e .** <dl>.** <dt>
3f3f0 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f SQLITE_DBSTATUS_
3f400 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3c 2f LOOKASIDE_USED</
3f410 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 dt>.** <dd>This
3f420 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e parameter return
3f430 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
3f440 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 lookaside memory
3f450 20 73 6c 6f 74 73 20 63 75 72 72 65 6e 74 6c 79 slots currently
3f460 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74 2e .** checked out.
3f470 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a </dd>.** </dl>.*
3f480 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
3f490 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 _DBSTATUS_LOOKAS
3f4a0 49 44 45 5f 55 53 45 44 20 20 20 20 20 30 0a 0a IDE_USED 0..
3f4b0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
3f4c0 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d Prepared Statem
3f4d0 65 6e 74 20 53 74 61 74 75 73 20 7b 48 31 37 35 ent Status {H175
3f4e0 35 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 50} <S60200>.**
3f4f0 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
3f500 2a 2a 20 45 61 63 68 20 70 72 65 70 61 72 65 64 ** Each prepared
3f510 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 69 6e 74 statement maint
3f520 61 69 6e 73 20 76 61 72 69 6f 75 73 0a 2a 2a 20 ains various.**
3f530 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 [SQLITE_STMTSTAT
3f540 55 53 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 US_SORT | counte
3f550 72 73 5d 20 74 68 61 74 20 6d 65 61 73 75 72 65 rs] that measure
3f560 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f the number.** o
3f570 66 20 74 69 6d 65 73 20 69 74 20 68 61 73 20 70 f times it has p
3f580 65 72 66 6f 72 6d 65 64 20 73 70 65 63 69 66 69 erformed specifi
3f590 63 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 54 c operations. T
3f5a0 68 65 73 65 20 63 6f 75 6e 74 65 72 73 20 63 61 hese counters ca
3f5b0 6e 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 n.** be used to
3f5c0 6d 6f 6e 69 74 6f 72 20 74 68 65 20 70 65 72 66 monitor the perf
3f5d0 6f 72 6d 61 6e 63 65 20 63 68 61 72 61 63 74 65 ormance characte
3f5e0 72 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 70 ristics of the p
3f5f0 72 65 70 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 repared.** state
3f600 6d 65 6e 74 73 2e 20 20 46 6f 72 20 65 78 61 6d ments. For exam
3f610 70 6c 65 2c 20 69 66 20 74 68 65 20 6e 75 6d 62 ple, if the numb
3f620 65 72 20 6f 66 20 74 61 62 6c 65 20 73 74 65 70 er of table step
3f630 73 20 67 72 65 61 74 6c 79 20 65 78 63 65 65 64 s greatly exceed
3f640 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 s.** the number
3f650 6f 66 20 74 61 62 6c 65 20 73 65 61 72 63 68 65 of table searche
3f660 73 20 6f 72 20 72 65 73 75 6c 74 20 72 6f 77 73 s or result rows
3f670 2c 20 74 68 61 74 20 77 6f 75 6c 64 20 74 65 6e , that would ten
3f680 64 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a d to indicate.**
3f690 20 74 68 61 74 20 74 68 65 20 70 72 65 70 61 72 that the prepar
3f6a0 65 64 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 ed statement is
3f6b0 75 73 69 6e 67 20 61 20 66 75 6c 6c 20 74 61 62 using a full tab
3f6c0 6c 65 20 73 63 61 6e 20 72 61 74 68 65 72 20 74 le scan rather t
3f6d0 68 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e han.** an index.
3f6e0 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e .**.** This in
3f6f0 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 terface is used
3f700 74 6f 20 72 65 74 72 69 65 76 65 20 61 6e 64 20 to retrieve and
3f710 72 65 73 65 74 20 63 6f 75 6e 74 65 72 20 76 61 reset counter va
3f720 6c 75 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 20 5b lues from.** a [
3f730 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
3f740 6e 74 5d 2e 20 20 54 68 65 20 66 69 72 73 74 20 nt]. The first
3f750 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
3f760 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
3f770 6e 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 6f 20 nt.** object to
3f780 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e be interrogated.
3f790 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 The second arg
3f7a0 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 69 ument.** is an i
3f7b0 6e 74 65 67 65 72 20 63 6f 64 65 20 66 6f 72 20 nteger code for
3f7c0 61 20 73 70 65 63 69 66 69 63 20 5b 53 51 4c 49 a specific [SQLI
3f7d0 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f TE_STMTSTATUS_SO
3f7e0 52 54 20 7c 20 63 6f 75 6e 74 65 72 5d 0a 2a 2a RT | counter].**
3f7f0 20 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 to be interroga
3f800 74 65 64 2e 20 0a 2a 2a 20 54 68 65 20 63 75 72 ted. .** The cur
3f810 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 rent value of th
3f820 65 20 72 65 71 75 65 73 74 65 64 20 63 6f 75 6e e requested coun
3f830 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e ter is returned.
3f840 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 65 74 .** If the reset
3f850 46 6c 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 Flg is true, the
3f860 6e 20 74 68 65 20 63 6f 75 6e 74 65 72 20 69 73 n the counter is
3f870 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f 20 61 reset to zero a
3f880 66 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 fter this.** int
3f890 65 72 66 61 63 65 20 63 61 6c 6c 20 72 65 74 75 erface call retu
3f8a0 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 rns..**.** See a
3f8b0 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 lso: [sqlite3_st
3f8c0 61 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c atus()] and [sql
3f8d0 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 ite3_db_status()
3f8e0 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 ]..*/.SQLITE_API
3f8f0 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 SQLITE_EXPERIME
3f900 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 NTAL int sqlite3
3f910 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71 6c _stmt_status(sql
3f920 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 ite3_stmt*, int
3f930 6f 70 2c 69 6e 74 20 72 65 73 65 74 46 6c 67 29 op,int resetFlg)
3f940 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
3f950 46 3a 20 53 74 61 74 75 73 20 50 61 72 61 6d 65 F: Status Parame
3f960 74 65 72 73 20 66 6f 72 20 70 72 65 70 61 72 65 ters for prepare
3f970 64 20 73 74 61 74 65 6d 65 6e 74 73 20 7b 48 31 d statements {H1
3f980 37 35 37 30 7d 20 3c 48 31 37 35 35 30 3e 0a 2a 7570} <H17550>.*
3f990 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
3f9a0 2a 0a 2a 2a 20 54 68 65 73 65 20 70 72 65 70 72 *.** These prepr
3f9b0 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 73 20 64 ocessor macros d
3f9c0 65 66 69 6e 65 20 69 6e 74 65 67 65 72 20 63 6f efine integer co
3f9d0 64 65 73 20 74 68 61 74 20 6e 61 6d 65 20 63 6f des that name co
3f9e0 75 6e 74 65 72 0a 2a 2a 20 76 61 6c 75 65 73 20 unter.** values
3f9f0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
3fa00 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d the [sqlite3_stm
3fa10 74 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 65 t_status()] inte
3fa20 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65 20 6d 65 rface..** The me
3fa30 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61 anings of the va
3fa40 72 69 6f 75 73 20 63 6f 75 6e 74 65 72 73 20 61 rious counters a
3fa50 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a re as follows:.*
3fa60 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 *.** <dl>.** <dt
3fa70 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 >SQLITE_STMTSTAT
3fa80 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 US_FULLSCAN_STEP
3fa90 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
3faa0 73 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 s is the number
3fab0 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 53 51 of times that SQ
3fac0 4c 69 74 65 20 68 61 73 20 73 74 65 70 70 65 64 Lite has stepped
3fad0 20 66 6f 72 77 61 72 64 20 69 6e 0a 2a 2a 20 61 forward in.** a
3fae0 20 74 61 62 6c 65 20 61 73 20 70 61 72 74 20 6f table as part o
3faf0 66 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 f a full table s
3fb00 63 61 6e 2e 20 20 4c 61 72 67 65 20 6e 75 6d 62 can. Large numb
3fb10 65 72 73 20 66 6f 72 20 74 68 69 73 20 63 6f 75 ers for this cou
3fb20 6e 74 65 72 0a 2a 2a 20 6d 61 79 20 69 6e 64 69 nter.** may indi
3fb30 63 61 74 65 20 6f 70 70 6f 72 74 75 6e 69 74 69 cate opportuniti
3fb40 65 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e es for performan
3fb50 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 74 ce improvement t
3fb60 68 72 6f 75 67 68 20 0a 2a 2a 20 63 61 72 65 66 hrough .** caref
3fb70 75 6c 20 75 73 65 20 6f 66 20 69 6e 64 69 63 65 ul use of indice
3fb80 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 s.</dd>.**.** <d
3fb90 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 t>SQLITE_STMTSTA
3fba0 54 55 53 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a TUS_SORT</dt>.**
3fbb0 20 3c 64 64 3e 54 68 69 73 20 69 73 20 74 68 65 <dd>This is the
3fbc0 20 6e 75 6d 62 65 72 20 6f 66 20 73 6f 72 74 20 number of sort
3fbd0 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74 20 operations that
3fbe0 68 61 76 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a have occurred..*
3fbf0 2a 20 41 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c * A non-zero val
3fc00 75 65 20 69 6e 20 74 68 69 73 20 63 6f 75 6e 74 ue in this count
3fc10 65 72 20 6d 61 79 20 69 6e 64 69 63 61 74 65 20 er may indicate
3fc20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 an opportunity t
3fc30 6f 0a 2a 2a 20 69 6d 70 72 6f 76 65 6d 65 6e 74 o.** improvement
3fc40 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 68 72 performance thr
3fc50 6f 75 67 68 20 63 61 72 65 66 75 6c 20 75 73 65 ough careful use
3fc60 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c 2f 64 64 of indices.</dd
3fc70 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f >.**.** </dl>.*/
3fc80 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
3fc90 53 54 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 STMTSTATUS_FULLS
3fca0 43 41 4e 5f 53 54 45 50 20 20 20 20 20 31 0a 23 CAN_STEP 1.#
3fcb0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 define SQLITE_ST
3fcc0 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 20 20 20 MTSTATUS_SORT
3fcd0 20 20 20 20 20 20 20 20 20 20 20 32 0a 0a 2f 2a 2../*
3fce0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 75 .** CAPI3REF: Cu
3fcf0 73 74 6f 6d 20 50 61 67 65 20 43 61 63 68 65 20 stom Page Cache
3fd00 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 49 Object.** EXPERI
3fd10 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 MENTAL.**.** The
3fd20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 sqlite3_pcache
3fd30 74 79 70 65 20 69 73 20 6f 70 61 71 75 65 2e 20 type is opaque.
3fd40 20 49 74 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 It is implement
3fd50 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 6c 75 ed by.** the plu
3fd60 67 67 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 ggable module.
3fd70 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 The SQLite core
3fd80 68 61 73 20 6e 6f 20 6b 6e 6f 77 6c 65 64 67 65 has no knowledge
3fd90 20 6f 66 0a 2a 2a 20 69 74 73 20 73 69 7a 65 20 of.** its size
3fda0 6f 72 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 or internal stru
3fdb0 63 74 75 72 65 20 61 6e 64 20 6e 65 76 65 72 20 cture and never
3fdc0 64 65 61 6c 73 20 77 69 74 68 20 74 68 65 0a 2a deals with the.*
3fdd0 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 * sqlite3_pcache
3fde0 20 6f 62 6a 65 63 74 20 65 78 63 65 70 74 20 62 object except b
3fdf0 79 20 68 6f 6c 64 69 6e 67 20 61 6e 64 20 70 61 y holding and pa
3fe00 73 73 69 6e 67 20 70 6f 69 6e 74 65 72 73 0a 2a ssing pointers.*
3fe10 2a 20 74 6f 20 74 68 65 20 6f 62 6a 65 63 74 2e * to the object.
3fe20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 5b 73 71 6c 69 .**.** See [sqli
3fe30 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f te3_pcache_metho
3fe40 64 73 5d 20 66 6f 72 20 61 64 64 69 74 69 6f 6e ds] for addition
3fe50 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a al information..
3fe60 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
3fe70 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 t sqlite3_pcache
3fe80 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 3b sqlite3_pcache;
3fe90 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
3fea0 3a 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 44 65 : Application De
3feb0 66 69 6e 65 64 20 50 61 67 65 20 43 61 63 68 65 fined Page Cache
3fec0 2e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b ..** KEYWORDS: {
3fed0 70 61 67 65 20 63 61 63 68 65 7d 0a 2a 2a 20 45 page cache}.** E
3fee0 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a XPERIMENTAL.**.*
3fef0 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 * The [sqlite3_c
3ff00 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 onfig]([SQLITE_C
3ff10 4f 4e 46 49 47 5f 50 43 41 43 48 45 5d 2c 20 2e ONFIG_PCACHE], .
3ff20 2e 2e 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 ..) interface ca
3ff30 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 61 6e n.** register an
3ff40 20 61 6c 74 65 72 6e 61 74 69 76 65 20 70 61 67 alternative pag
3ff50 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e e cache implemen
3ff60 74 61 74 69 6f 6e 20 62 79 20 70 61 73 73 69 6e tation by passin
3ff70 67 20 69 6e 20 61 6e 20 0a 2a 2a 20 69 6e 73 74 g in an .** inst
3ff80 61 6e 63 65 20 6f 66 20 74 68 65 20 73 71 6c 69 ance of the sqli
3ff90 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f te3_pcache_metho
3ffa0 64 73 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 ds structure. Th
3ffb0 65 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20 74 68 e majority of th
3ffc0 65 20 0a 2a 2a 20 68 65 61 70 20 6d 65 6d 6f 72 e .** heap memor
3ffd0 79 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 y used by SQLite
3ffe0 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 is used by the
3fff0 70 61 67 65 20 63 61 63 68 65 20 74 6f 20 63 61 page cache to ca
40000 63 68 65 20 64 61 74 61 20 72 65 61 64 20 0a 2a che data read .*
40010 2a 20 66 72 6f 6d 2c 20 6f 72 20 72 65 61 64 79 * from, or ready
40020 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 74 to be written t
40030 6f 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 o, the database
40040 66 69 6c 65 2e 20 42 79 20 69 6d 70 6c 65 6d 65 file. By impleme
40050 6e 74 69 6e 67 20 61 20 0a 2a 2a 20 63 75 73 74 nting a .** cust
40060 6f 6d 20 70 61 67 65 20 63 61 63 68 65 20 75 73 om page cache us
40070 69 6e 67 20 74 68 69 73 20 41 50 49 2c 20 61 6e ing this API, an
40080 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e application can
40090 20 63 6f 6e 74 72 6f 6c 20 6d 6f 72 65 20 0a 2a control more .*
400a0 2a 20 70 72 65 63 69 73 65 6c 79 20 74 68 65 20 * precisely the
400b0 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 amount of memory
400c0 20 63 6f 6e 73 75 6d 65 64 20 62 79 20 53 51 4c consumed by SQL
400d0 69 74 65 2c 20 74 68 65 20 77 61 79 20 69 6e 20 ite, the way in
400e0 77 68 69 63 68 20 0a 2a 2a 20 74 68 61 74 20 6d which .** that m
400f0 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74 emory is allocat
40100 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65 64 2c ed and released,
40110 20 61 6e 64 20 74 68 65 20 70 6f 6c 69 63 69 65 and the policie
40120 73 20 75 73 65 64 20 74 6f 20 0a 2a 2a 20 64 65 s used to .** de
40130 74 65 72 6d 69 6e 65 20 65 78 61 63 74 6c 79 20 termine exactly
40140 77 68 69 63 68 20 70 61 72 74 73 20 6f 66 20 61 which parts of a
40150 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
40160 72 65 20 63 61 63 68 65 64 20 61 6e 64 20 66 6f re cached and fo
40170 72 20 0a 2a 2a 20 68 6f 77 20 6c 6f 6e 67 2e 0a r .** how long..
40180 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e **.** The conten
40190 74 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 ts of the sqlite
401a0 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 3_pcache_methods
401b0 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 63 structure are c
401c0 6f 70 69 65 64 20 74 6f 20 61 6e 0a 2a 2a 20 69 opied to an.** i
401d0 6e 74 65 72 6e 61 6c 20 62 75 66 66 65 72 20 62 nternal buffer b
401e0 79 20 53 51 4c 69 74 65 20 77 69 74 68 69 6e 20 y SQLite within
401f0 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c the call to [sql
40200 69 74 65 33 5f 63 6f 6e 66 69 67 5d 2e 20 20 48 ite3_config]. H
40210 65 6e 63 65 0a 2a 2a 20 74 68 65 20 61 70 70 6c ence.** the appl
40220 69 63 61 74 69 6f 6e 20 6d 61 79 20 64 69 73 63 ication may disc
40230 61 72 64 20 74 68 65 20 70 61 72 61 6d 65 74 65 ard the paramete
40240 72 20 61 66 74 65 72 20 74 68 65 20 63 61 6c 6c r after the call
40250 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f to.** [sqlite3_
40260 63 6f 6e 66 69 67 28 29 5d 20 72 65 74 75 72 6e config()] return
40270 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e s..**.** The xIn
40280 69 74 28 29 20 6d 65 74 68 6f 64 20 69 73 20 63 it() method is c
40290 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 alled once for e
402a0 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c ach call to [sql
402b0 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
402c0 29 5d 0a 2a 2a 20 28 75 73 75 61 6c 6c 79 20 6f )].** (usually o
402d0 6e 6c 79 20 6f 6e 63 65 20 64 75 72 69 6e 67 20 nly once during
402e0 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 the lifetime of
402f0 74 68 65 20 70 72 6f 63 65 73 73 29 2e 20 49 74 the process). It
40300 20 69 73 20 70 61 73 73 65 64 0a 2a 2a 20 61 20 is passed.** a
40310 63 6f 70 79 20 6f 66 20 74 68 65 20 73 71 6c 69 copy of the sqli
40320 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f te3_pcache_metho
40330 64 73 2e 70 41 72 67 20 76 61 6c 75 65 2e 20 49 ds.pArg value. I
40340 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f t can be used to
40350 20 73 65 74 0a 2a 2a 20 75 70 20 67 6c 6f 62 61 set.** up globa
40360 6c 20 73 74 72 75 63 74 75 72 65 73 20 61 6e 64 l structures and
40370 20 6d 75 74 65 78 65 73 20 72 65 71 75 69 72 65 mutexes require
40380 64 20 62 79 20 74 68 65 20 63 75 73 74 6f 6d 20 d by the custom
40390 70 61 67 65 20 63 61 63 68 65 20 0a 2a 2a 20 69 page cache .** i
403a0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 0a mplementation. .
403b0 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 68 75 74 64 **.** The xShutd
403c0 6f 77 6e 28 29 20 6d 65 74 68 6f 64 20 69 73 20 own() method is
403d0 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 called from with
403e0 69 6e 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 in [sqlite3_shut
403f0 64 6f 77 6e 28 29 5d 2c 20 0a 2a 2a 20 69 66 20 down()], .** if
40400 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
40410 69 6e 76 6f 6b 65 73 20 74 68 69 73 20 41 50 49 invokes this API
40420 2e 20 49 74 20 63 61 6e 20 62 65 20 75 73 65 64 . It can be used
40430 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 0a 2a 2a to clean up .**
40440 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 any outstanding
40450 20 72 65 73 6f 75 72 63 65 73 20 62 65 66 6f 72 resources befor
40460 65 20 70 72 6f 63 65 73 73 20 73 68 75 74 64 6f e process shutdo
40470 77 6e 2c 20 69 66 20 72 65 71 75 69 72 65 64 2e wn, if required.
40480 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 68 6f .**.** SQLite ho
40490 6c 64 73 20 61 20 5b 53 51 4c 49 54 45 5f 4d 55 lds a [SQLITE_MU
404a0 54 45 58 5f 52 45 43 55 52 53 49 56 45 5d 20 6d TEX_RECURSIVE] m
404b0 75 74 65 78 20 77 68 65 6e 20 69 74 20 69 6e 76 utex when it inv
404c0 6f 6b 65 73 0a 2a 2a 20 74 68 65 20 78 49 6e 69 okes.** the xIni
404d0 74 20 6d 65 74 68 6f 64 2c 20 73 6f 20 74 68 65 t method, so the
404e0 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 6e 65 xInit method ne
404f0 65 64 20 6e 6f 74 20 62 65 20 74 68 72 65 61 64 ed not be thread
40500 73 61 66 65 2e 20 20 54 68 65 0a 2a 2a 20 78 53 safe. The.** xS
40510 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 20 69 hutdown method i
40520 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 s only called fr
40530 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 om [sqlite3_shut
40540 64 6f 77 6e 28 29 5d 20 73 6f 20 69 74 20 64 6f down()] so it do
40550 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 es.** not need t
40560 6f 20 62 65 20 74 68 72 65 61 64 73 61 66 65 20 o be threadsafe
40570 65 69 74 68 65 72 2e 20 20 41 6c 6c 20 6f 74 68 either. All oth
40580 65 72 20 6d 65 74 68 6f 64 73 20 6d 75 73 74 20 er methods must
40590 62 65 20 74 68 72 65 61 64 73 61 66 65 0a 2a 2a be threadsafe.**
405a0 20 69 6e 20 6d 75 6c 74 69 74 68 72 65 61 64 65 in multithreade
405b0 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 0a d applications..
405c0 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c **.** SQLite wil
405d0 6c 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 20 78 l never invoke x
405e0 49 6e 69 74 28 29 20 6d 6f 72 65 20 74 68 61 6e Init() more than
405f0 20 6f 6e 63 65 20 77 69 74 68 6f 75 74 20 61 6e once without an
40600 20 69 6e 74 65 72 76 65 6e 69 6e 67 0a 2a 2a 20 intervening.**
40610 63 61 6c 6c 20 74 6f 20 78 53 68 75 74 64 6f 77 call to xShutdow
40620 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 n()..**.** The x
40630 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f 64 20 Create() method
40640 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 73 74 is used to const
40650 72 75 63 74 20 61 20 6e 65 77 20 63 61 63 68 65 ruct a new cache
40660 20 69 6e 73 74 61 6e 63 65 2e 20 20 53 51 4c 69 instance. SQLi
40670 74 65 0a 2a 2a 20 77 69 6c 6c 20 74 79 70 69 63 te.** will typic
40680 61 6c 6c 79 20 63 72 65 61 74 65 20 6f 6e 65 20 ally create one
40690 63 61 63 68 65 20 69 6e 73 74 61 6e 63 65 20 66 cache instance f
406a0 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 64 61 74 or each open dat
406b0 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a 20 74 abase file,.** t
406c0 68 6f 75 67 68 20 74 68 69 73 20 69 73 20 6e 6f hough this is no
406d0 74 20 67 75 61 72 61 6e 74 65 65 64 2e 20 54 68 t guaranteed. Th
406e0 65 0a 2a 2a 20 66 69 72 73 74 20 70 61 72 61 6d e.** first param
406f0 65 74 65 72 2c 20 73 7a 50 61 67 65 2c 20 69 73 eter, szPage, is
40700 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74 the size in byt
40710 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 es of the pages
40720 74 68 61 74 20 6d 75 73 74 0a 2a 2a 20 62 65 20 that must.** be
40730 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 allocated by the
40740 20 63 61 63 68 65 2e 20 20 73 7a 50 61 67 65 20 cache. szPage
40750 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 20 70 6f will not be a po
40760 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 73 7a 50 wer of two. szP
40770 61 67 65 0a 2a 2a 20 77 69 6c 6c 20 74 68 65 20 age.** will the
40780 70 61 67 65 20 73 69 7a 65 20 6f 66 20 74 68 65 page size of the
40790 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
407a0 68 61 74 20 69 73 20 74 6f 20 62 65 20 63 61 63 hat is to be cac
407b0 68 65 64 20 70 6c 75 73 20 61 6e 0a 2a 2a 20 69 hed plus an.** i
407c0 6e 63 72 65 6d 65 6e 74 20 28 68 65 72 65 20 63 ncrement (here c
407d0 61 6c 6c 65 64 20 22 52 22 29 20 6f 66 20 61 62 alled "R") of ab
407e0 6f 75 74 20 31 30 30 20 6f 72 20 32 30 30 2e 20 out 100 or 200.
407f0 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 SQLite will use
40800 20 74 68 65 0a 2a 2a 20 65 78 74 72 61 20 52 20 the.** extra R
40810 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61 bytes on each pa
40820 67 65 20 74 6f 20 73 74 6f 72 65 20 6d 65 74 61 ge to store meta
40830 64 61 74 61 20 61 62 6f 75 74 20 74 68 65 20 75 data about the u
40840 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 64 61 74 nderlying.** dat
40850 61 62 61 73 65 20 70 61 67 65 20 6f 6e 20 64 69 abase page on di
40860 73 6b 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6f sk. The value o
40870 66 20 52 20 64 65 70 65 6e 64 73 0a 2a 2a 20 6f f R depends.** o
40880 6e 20 74 68 65 20 53 51 4c 69 74 65 20 76 65 72 n the SQLite ver
40890 73 69 6f 6e 2c 20 74 68 65 20 74 61 72 67 65 74 sion, the target
408a0 20 70 6c 61 74 66 6f 72 6d 2c 20 61 6e 64 20 68 platform, and h
408b0 6f 77 20 53 51 4c 69 74 65 20 77 61 73 20 63 6f ow SQLite was co
408c0 6d 70 69 6c 65 64 2e 0a 2a 2a 20 52 20 69 73 20 mpiled..** R is
408d0 63 6f 6e 73 74 61 6e 74 20 66 6f 72 20 61 20 70 constant for a p
408e0 61 72 74 69 63 75 6c 61 72 20 62 75 69 6c 64 20 articular build
408f0 6f 66 20 53 51 4c 69 74 65 2e 20 20 54 68 65 20 of SQLite. The
40900 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
40910 74 6f 0a 2a 2a 20 78 43 72 65 61 74 65 28 29 2c to.** xCreate(),
40920 20 62 50 75 72 67 65 61 62 6c 65 2c 20 69 73 20 bPurgeable, is
40930 74 72 75 65 20 69 66 20 74 68 65 20 63 61 63 68 true if the cach
40940 65 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 20 e being created
40950 77 69 6c 6c 0a 2a 2a 20 62 65 20 75 73 65 64 20 will.** be used
40960 74 6f 20 63 61 63 68 65 20 64 61 74 61 62 61 73 to cache databas
40970 65 20 70 61 67 65 73 20 6f 66 20 61 20 66 69 6c e pages of a fil
40980 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b e stored on disk
40990 2c 20 6f 72 0a 2a 2a 20 66 61 6c 73 65 20 69 66 , or.** false if
409a0 20 69 74 20 69 73 20 75 73 65 64 20 66 6f 72 20 it is used for
409b0 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 an in-memory dat
409c0 61 62 61 73 65 2e 20 54 68 65 20 63 61 63 68 65 abase. The cache
409d0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a implementation.
409e0 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 ** does not have
409f0 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 to do anything
40a00 73 70 65 63 69 61 6c 20 62 61 73 65 64 20 77 69 special based wi
40a10 74 68 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 th the value of
40a20 62 50 75 72 67 65 61 62 6c 65 3b 0a 2a 2a 20 69 bPurgeable;.** i
40a30 74 20 69 73 20 70 75 72 65 6c 79 20 61 64 76 69 t is purely advi
40a40 73 6f 72 79 2e 20 20 4f 6e 20 61 20 63 61 63 68 sory. On a cach
40a50 65 20 77 68 65 72 65 20 62 50 75 72 67 65 61 62 e where bPurgeab
40a60 6c 65 20 69 73 20 66 61 6c 73 65 2c 20 53 51 4c le is false, SQL
40a70 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 ite will.** neve
40a80 72 20 69 6e 76 6f 6b 65 20 78 55 6e 70 69 6e 28 r invoke xUnpin(
40a90 29 20 65 78 63 65 70 74 20 74 6f 20 64 65 6c 69 ) except to deli
40aa0 62 65 72 61 74 65 6c 79 20 64 65 6c 65 74 65 20 berately delete
40ab0 61 20 70 61 67 65 2e 0a 2a 2a 20 49 6e 20 6f 74 a page..** In ot
40ac0 68 65 72 20 77 6f 72 64 73 2c 20 61 20 63 61 63 her words, a cac
40ad0 68 65 20 63 72 65 61 74 65 64 20 77 69 74 68 20 he created with
40ae0 62 50 75 72 67 65 61 62 6c 65 20 73 65 74 20 74 bPurgeable set t
40af0 6f 20 66 61 6c 73 65 20 77 69 6c 6c 0a 2a 2a 20 o false will.**
40b00 6e 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 61 6e never contain an
40b10 79 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 y unpinned pages
40b20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 61 63 ..**.** The xCac
40b30 68 65 73 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 hesize() method
40b40 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 61 74 may be called at
40b50 20 61 6e 79 20 74 69 6d 65 20 62 79 20 53 51 4c any time by SQL
40b60 69 74 65 20 74 6f 20 73 65 74 20 74 68 65 0a 2a ite to set the.*
40b70 2a 20 73 75 67 67 65 73 74 65 64 20 6d 61 78 69 * suggested maxi
40b80 6d 75 6d 20 63 61 63 68 65 2d 73 69 7a 65 20 28 mum cache-size (
40b90 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
40ba0 73 74 6f 72 65 64 20 62 79 29 20 74 68 65 20 63 stored by) the c
40bb0 61 63 68 65 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 ache.** instance
40bc0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 passed as the f
40bd0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 irst argument. T
40be0 68 69 73 20 69 73 20 74 68 65 20 76 61 6c 75 65 his is the value
40bf0 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e configured usin
40c00 67 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 g.** the SQLite
40c10 22 5b 50 52 41 47 4d 41 20 63 61 63 68 65 5f 73 "[PRAGMA cache_s
40c20 69 7a 65 5d 22 20 63 6f 6d 6d 61 6e 64 2e 20 41 ize]" command. A
40c30 73 20 77 69 74 68 20 74 68 65 20 62 50 75 72 67 s with the bPurg
40c40 65 61 62 6c 65 20 70 61 72 61 6d 65 74 65 72 2c eable parameter,
40c50 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e .** the implemen
40c60 74 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 65 tation is not re
40c70 71 75 69 72 65 64 20 74 6f 20 64 6f 20 61 6e 79 quired to do any
40c80 74 68 69 6e 67 20 77 69 74 68 20 74 68 69 73 0a thing with this.
40c90 2a 2a 20 76 61 6c 75 65 3b 20 69 74 20 69 73 20 ** value; it is
40ca0 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a advisory only..*
40cb0 2a 0a 2a 2a 20 54 68 65 20 78 50 61 67 65 63 6f *.** The xPageco
40cc0 75 6e 74 28 29 20 6d 65 74 68 6f 64 20 73 68 6f unt() method sho
40cd0 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 6e uld return the n
40ce0 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 63 umber of pages c
40cf0 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 73 74 6f 72 urrently.** stor
40d00 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e ed in the cache.
40d10 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 78 46 65 74 .** .** The xFet
40d20 63 68 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75 ch() method is u
40d30 73 65 64 20 74 6f 20 66 65 74 63 68 20 61 20 70 sed to fetch a p
40d40 61 67 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 age and return a
40d50 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 pointer to it.
40d60 0a 2a 2a 20 41 20 27 70 61 67 65 27 2c 20 69 6e .** A 'page', in
40d70 20 74 68 69 73 20 63 6f 6e 74 65 78 74 2c 20 69 this context, i
40d80 73 20 61 20 62 75 66 66 65 72 20 6f 66 20 73 7a s a buffer of sz
40d90 50 61 67 65 20 62 79 74 65 73 20 61 6c 69 67 6e Page bytes align
40da0 65 64 20 61 74 20 61 6e 0a 2a 2a 20 38 2d 62 79 ed at an.** 8-by
40db0 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 54 68 65 te boundary. The
40dc0 20 70 61 67 65 20 74 6f 20 62 65 20 66 65 74 63 page to be fetc
40dd0 68 65 64 20 69 73 20 64 65 74 65 72 6d 69 6e 65 hed is determine
40de0 64 20 62 79 20 74 68 65 20 6b 65 79 2e 20 54 68 d by the key. Th
40df0 65 0a 2a 2a 20 6d 69 6d 69 6d 75 6d 20 6b 65 79 e.** mimimum key
40e00 20 76 61 6c 75 65 20 69 73 20 31 2e 20 41 66 74 value is 1. Aft
40e10 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20 72 er it has been r
40e20 65 74 72 69 65 76 65 64 20 75 73 69 6e 67 20 78 etrieved using x
40e30 46 65 74 63 68 2c 20 74 68 65 20 70 61 67 65 20 Fetch, the page
40e40 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 .** is considere
40e50 64 20 74 6f 20 62 65 20 22 70 69 6e 6e 65 64 22 d to be "pinned"
40e60 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 ..**.** If the r
40e70 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73 equested page is
40e80 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
40e90 70 61 67 65 20 63 61 63 68 65 2c 20 74 68 65 6e page cache, then
40ea0 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 0a the page cache.
40eb0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
40ec0 6e 20 6d 75 73 74 20 72 65 74 75 72 6e 20 61 20 n must return a
40ed0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 pointer to the p
40ee0 61 67 65 20 62 75 66 66 65 72 20 77 69 74 68 20 age buffer with
40ef0 69 74 73 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 its content.** i
40f00 6e 74 61 63 74 2e 20 20 49 66 20 74 68 65 20 72 ntact. If the r
40f10 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73 equested page is
40f20 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 not already in
40f30 74 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e 20 the cache, then
40f40 74 68 65 0a 2a 2a 20 62 65 68 61 76 69 6f 72 20 the.** behavior
40f50 6f 66 20 74 68 65 20 63 61 63 68 65 20 69 6d 70 of the cache imp
40f60 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 64 lementation is d
40f70 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 etermined by the
40f80 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a value of the.**
40f90 20 63 72 65 61 74 65 46 6c 61 67 20 70 61 72 61 createFlag para
40fa0 6d 65 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 meter passed to
40fb0 78 46 65 74 63 68 2c 20 61 63 63 6f 72 64 69 6e xFetch, accordin
40fc0 67 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 g to the followi
40fd0 6e 67 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 ng table:.**.**
40fe0 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 31 20 <table border=1
40ff0 77 69 64 74 68 3d 38 35 25 20 61 6c 69 67 6e 3d width=85% align=
41000 63 65 6e 74 65 72 3e 0a 2a 2a 20 3c 74 72 3e 3c center>.** <tr><
41010 74 68 3e 20 63 72 65 61 74 65 46 6c 61 67 20 3c th> createFlag <
41020 74 68 3e 20 42 65 68 61 76 69 6f 75 72 20 77 68 th> Behaviour wh
41030 65 6e 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 en page is not a
41040 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68 65 0a lready in cache.
41050 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 30 20 3c 74 ** <tr><td> 0 <t
41060 64 3e 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 d> Do not alloca
41070 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 20 te a new page.
41080 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20 Return NULL..**
41090 3c 74 72 3e 3c 74 64 3e 20 31 20 3c 74 64 3e 20 <tr><td> 1 <td>
410a0 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 Allocate a new p
410b0 61 67 65 20 69 66 20 69 74 20 65 61 73 79 20 61 age if it easy a
410c0 6e 64 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f nd convenient to
410d0 20 64 6f 20 73 6f 2e 0a 2a 2a 20 20 20 20 20 20 do so..**
410e0 20 20 20 20 20 20 20 20 20 20 20 4f 74 68 65 72 Other
410f0 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c wise return NULL
41100 2e 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 32 20 ..** <tr><td> 2
41110 3c 74 64 3e 20 4d 61 6b 65 20 65 76 65 72 79 20 <td> Make every
41120 65 66 66 6f 72 74 20 74 6f 20 61 6c 6c 6f 63 61 effort to alloca
41130 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 20 te a new page.
41140 4f 6e 6c 79 20 72 65 74 75 72 6e 0a 2a 2a 20 20 Only return.**
41150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e N
41160 55 4c 4c 20 69 66 20 61 6c 6c 6f 63 61 74 69 6e ULL if allocatin
41170 67 20 61 20 6e 65 77 20 70 61 67 65 20 69 73 20 g a new page is
41180 65 66 66 65 63 74 69 76 65 6c 79 20 69 6d 70 6f effectively impo
41190 73 73 69 62 6c 65 2e 0a 2a 2a 20 3c 2f 74 61 62 ssible..** </tab
411a0 6c 65 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 le>.**.** SQLite
411b0 20 77 69 6c 6c 20 6e 6f 72 6d 61 6c 6c 79 20 69 will normally i
411c0 6e 76 6f 6b 65 20 78 46 65 74 63 68 28 29 20 77 nvoke xFetch() w
411d0 69 74 68 20 61 20 63 72 65 61 74 65 46 6c 61 67 ith a createFlag
411e0 20 6f 66 20 30 20 6f 72 20 31 2e 20 20 49 66 0a of 0 or 1. If.
411f0 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 78 46 65 ** a call to xFe
41200 74 63 68 28 29 20 77 69 74 68 20 63 72 65 61 74 tch() with creat
41210 65 46 6c 61 67 3d 3d 31 20 72 65 74 75 72 6e 73 eFlag==1 returns
41220 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53 51 4c 69 NULL, then SQLi
41230 74 65 20 77 69 6c 6c 0a 2a 2a 20 61 74 74 65 6d te will.** attem
41240 70 74 20 74 6f 20 75 6e 70 69 6e 20 6f 6e 65 20 pt to unpin one
41250 6f 72 20 6d 6f 72 65 20 63 61 63 68 65 20 70 61 or more cache pa
41260 67 65 73 20 62 79 20 73 70 69 6c 6c 69 6e 67 20 ges by spilling
41270 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a the content of.*
41280 2a 20 70 69 6e 6e 65 64 20 70 61 67 65 73 20 74 * pinned pages t
41290 6f 20 64 69 73 6b 20 61 6e 64 20 73 79 6e 63 68 o disk and synch
412a0 69 6e 67 20 74 68 65 20 6f 70 65 72 61 74 69 6e ing the operatin
412b0 67 20 73 79 73 74 65 6d 20 64 69 73 6b 20 63 61 g system disk ca
412c0 63 68 65 2e 20 41 66 74 65 72 0a 2a 2a 20 61 74 che. After.** at
412d0 74 65 6d 70 74 69 6e 67 20 74 6f 20 75 6e 70 69 tempting to unpi
412e0 6e 20 70 61 67 65 73 2c 20 74 68 65 20 78 46 65 n pages, the xFe
412f0 74 63 68 28 29 20 6d 65 74 68 6f 64 20 77 69 6c tch() method wil
41300 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 67 61 l be invoked aga
41310 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20 63 72 65 in with.** a cre
41320 61 74 65 46 6c 61 67 20 6f 66 20 32 2e 0a 2a 2a ateFlag of 2..**
41330 0a 2a 2a 20 78 55 6e 70 69 6e 28 29 20 69 73 20 .** xUnpin() is
41340 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 65 called by SQLite
41350 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20 with a pointer
41360 74 6f 20 61 20 63 75 72 72 65 6e 74 6c 79 20 70 to a currently p
41370 69 6e 6e 65 64 20 70 61 67 65 0a 2a 2a 20 61 73 inned page.** as
41380 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 its second argu
41390 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 74 68 69 ment. If the thi
413a0 72 64 20 70 61 72 61 6d 65 74 65 72 2c 20 64 69 rd parameter, di
413b0 73 63 61 72 64 2c 20 69 73 20 6e 6f 6e 2d 7a 65 scard, is non-ze
413c0 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 ro,.** then the
413d0 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 65 page should be e
413e0 76 69 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20 victed from the
413f0 63 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20 63 cache. In this c
41400 61 73 65 20 53 51 4c 69 74 65 20 0a 2a 2a 20 61 ase SQLite .** a
41410 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 ssumes that the
41420 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20 70 61 next time the pa
41430 67 65 20 69 73 20 72 65 74 72 69 65 76 65 64 20 ge is retrieved
41440 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 75 from the cache u
41450 73 69 6e 67 0a 2a 2a 20 74 68 65 20 78 46 65 74 sing.** the xFet
41460 63 68 28 29 20 6d 65 74 68 6f 64 2c 20 69 74 20 ch() method, it
41470 77 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e 20 will be zeroed.
41480 49 66 20 74 68 65 20 64 69 73 63 61 72 64 20 70 If the discard p
41490 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 7a arameter is.** z
414a0 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 61 ero, then the pa
414b0 67 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 ge is considered
414c0 20 74 6f 20 62 65 20 75 6e 70 69 6e 6e 65 64 2e to be unpinned.
414d0 20 54 68 65 20 63 61 63 68 65 20 69 6d 70 6c 65 The cache imple
414e0 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6d 61 79 mentation.** may
414f0 20 63 68 6f 6f 73 65 20 74 6f 20 65 76 69 63 74 choose to evict
41500 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 unpinned pages
41510 61 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a 2a 0a at any time..**.
41520 2a 2a 20 54 68 65 20 63 61 63 68 65 20 69 73 20 ** The cache is
41530 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 not required to
41540 70 65 72 66 6f 72 6d 20 61 6e 79 20 72 65 66 65 perform any refe
41550 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 2e 20 rence counting.
41560 41 20 73 69 6e 67 6c 65 20 0a 2a 2a 20 63 61 6c A single .** cal
41570 6c 20 74 6f 20 78 55 6e 70 69 6e 28 29 20 75 6e l to xUnpin() un
41580 70 69 6e 73 20 74 68 65 20 70 61 67 65 20 72 65 pins the page re
41590 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 gardless of the
415a0 6e 75 6d 62 65 72 20 6f 66 20 70 72 69 6f 72 20 number of prior
415b0 63 61 6c 6c 73 20 0a 2a 2a 20 74 6f 20 78 46 65 calls .** to xFe
415c0 74 63 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tch()..**.** The
415d0 20 78 52 65 6b 65 79 28 29 20 6d 65 74 68 6f 64 xRekey() method
415e0 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e is used to chan
415f0 67 65 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 ge the key value
41600 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
41610 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 70 61 73 the.** page pas
41620 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e sed as the secon
41630 64 20 61 72 67 75 6d 65 6e 74 20 66 72 6f 6d 20 d argument from
41640 6f 6c 64 4b 65 79 20 74 6f 20 6e 65 77 4b 65 79 oldKey to newKey
41650 2e 20 49 66 20 74 68 65 20 63 61 63 68 65 0a 2a . If the cache.*
41660 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e * previously con
41670 74 61 69 6e 73 20 61 6e 20 65 6e 74 72 79 20 61 tains an entry a
41680 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e ssociated with n
41690 65 77 4b 65 79 2c 20 69 74 20 73 68 6f 75 6c 64 ewKey, it should
416a0 20 62 65 0a 2a 2a 20 64 69 73 63 61 72 64 65 64 be.** discarded
416b0 2e 20 41 6e 79 20 70 72 69 6f 72 20 63 61 63 68 . Any prior cach
416c0 65 20 65 6e 74 72 79 20 61 73 73 6f 63 69 61 74 e entry associat
416d0 65 64 20 77 69 74 68 20 6e 65 77 4b 65 79 20 69 ed with newKey i
416e0 73 20 67 75 61 72 61 6e 74 65 65 64 20 6e 6f 74 s guaranteed not
416f0 0a 2a 2a 20 74 6f 20 62 65 20 70 69 6e 6e 65 64 .** to be pinned
41700 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c ..**.** When SQL
41710 69 74 65 20 63 61 6c 6c 73 20 74 68 65 20 78 54 ite calls the xT
41720 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68 6f 64 runcate() method
41730 2c 20 74 68 65 20 63 61 63 68 65 20 6d 75 73 74 , the cache must
41740 20 64 69 73 63 61 72 64 20 61 6c 6c 0a 2a 2a 20 discard all.**
41750 65 78 69 73 74 69 6e 67 20 63 61 63 68 65 20 65 existing cache e
41760 6e 74 72 69 65 73 20 77 69 74 68 20 70 61 67 65 ntries with page
41770 20 6e 75 6d 62 65 72 73 20 28 6b 65 79 73 29 20 numbers (keys)
41780 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 greater than or
41790 65 71 75 61 6c 0a 2a 2a 20 74 6f 20 74 68 65 20 equal.** to the
417a0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 4c 69 value of the iLi
417b0 6d 69 74 20 70 61 72 61 6d 65 74 65 72 20 70 61 mit parameter pa
417c0 73 73 65 64 20 74 6f 20 78 54 72 75 6e 63 61 74 ssed to xTruncat
417d0 65 28 29 2e 20 49 66 20 61 6e 79 0a 2a 2a 20 6f e(). If any.** o
417e0 66 20 74 68 65 73 65 20 70 61 67 65 73 20 61 72 f these pages ar
417f0 65 20 70 69 6e 6e 65 64 2c 20 74 68 65 79 20 61 e pinned, they a
41800 72 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 75 6e re implicitly un
41810 70 69 6e 6e 65 64 2c 20 6d 65 61 6e 69 6e 67 20 pinned, meaning
41820 74 68 61 74 0a 2a 2a 20 74 68 65 79 20 63 61 6e that.** they can
41830 20 62 65 20 73 61 66 65 6c 79 20 64 69 73 63 61 be safely disca
41840 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 rded..**.** The
41850 78 44 65 73 74 72 6f 79 28 29 20 6d 65 74 68 6f xDestroy() metho
41860 64 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 6c d is used to del
41870 65 74 65 20 61 20 63 61 63 68 65 20 61 6c 6c 6f ete a cache allo
41880 63 61 74 65 64 20 62 79 20 78 43 72 65 61 74 65 cated by xCreate
41890 28 29 2e 0a 2a 2a 20 41 6c 6c 20 72 65 73 6f 75 ()..** All resou
418a0 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 rces associated
418b0 77 69 74 68 20 74 68 65 20 73 70 65 63 69 66 69 with the specifi
418c0 65 64 20 63 61 63 68 65 20 73 68 6f 75 6c 64 20 ed cache should
418d0 62 65 20 66 72 65 65 64 2e 20 41 66 74 65 72 0a be freed. After.
418e0 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 78 ** calling the x
418f0 44 65 73 74 72 6f 79 28 29 20 6d 65 74 68 6f 64 Destroy() method
41900 2c 20 53 51 4c 69 74 65 20 63 6f 6e 73 69 64 65 , SQLite conside
41910 72 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f rs the [sqlite3_
41920 70 63 61 63 68 65 2a 5d 0a 2a 2a 20 68 61 6e 64 pcache*].** hand
41930 6c 65 20 69 6e 76 61 6c 69 64 2c 20 61 6e 64 20 le invalid, and
41940 77 69 6c 6c 20 6e 6f 74 20 75 73 65 20 69 74 20 will not use it
41950 77 69 74 68 20 61 6e 79 20 6f 74 68 65 72 20 73 with any other s
41960 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 qlite3_pcache_me
41970 74 68 6f 64 73 0a 2a 2a 20 66 75 6e 63 74 69 6f thods.** functio
41980 6e 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 ns..*/.typedef s
41990 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63 truct sqlite3_pc
419a0 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 73 71 6c ache_methods sql
419b0 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 ite3_pcache_meth
419c0 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 ods;.struct sqli
419d0 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f te3_pcache_metho
419e0 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a 70 41 72 ds {. void *pAr
419f0 67 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 74 g;. int (*xInit
41a00 29 28 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 )(void*);. void
41a10 20 28 2a 78 53 68 75 74 64 6f 77 6e 29 28 76 6f (*xShutdown)(vo
41a20 69 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f id*);. sqlite3_
41a30 70 63 61 63 68 65 20 2a 28 2a 78 43 72 65 61 74 pcache *(*xCreat
41a40 65 29 28 69 6e 74 20 73 7a 50 61 67 65 2c 20 69 e)(int szPage, i
41a50 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29 3b 0a nt bPurgeable);.
41a60 20 20 76 6f 69 64 20 28 2a 78 43 61 63 68 65 73 void (*xCaches
41a70 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 70 63 61 ize)(sqlite3_pca
41a80 63 68 65 2a 2c 20 69 6e 74 20 6e 43 61 63 68 65 che*, int nCache
41a90 73 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 size);. int (*x
41aa0 50 61 67 65 63 6f 75 6e 74 29 28 73 71 6c 69 74 Pagecount)(sqlit
41ab0 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a 20 20 76 e3_pcache*);. v
41ac0 6f 69 64 20 2a 28 2a 78 46 65 74 63 68 29 28 73 oid *(*xFetch)(s
41ad0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 qlite3_pcache*,
41ae0 75 6e 73 69 67 6e 65 64 20 6b 65 79 2c 20 69 6e unsigned key, in
41af0 74 20 63 72 65 61 74 65 46 6c 61 67 29 3b 0a 20 t createFlag);.
41b00 20 76 6f 69 64 20 28 2a 78 55 6e 70 69 6e 29 28 void (*xUnpin)(
41b10 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c sqlite3_pcache*,
41b20 20 76 6f 69 64 2a 2c 20 69 6e 74 20 64 69 73 63 void*, int disc
41b30 61 72 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 ard);. void (*x
41b40 52 65 6b 65 79 29 28 73 71 6c 69 74 65 33 5f 70 Rekey)(sqlite3_p
41b50 63 61 63 68 65 2a 2c 20 76 6f 69 64 2a 2c 20 75 cache*, void*, u
41b60 6e 73 69 67 6e 65 64 20 6f 6c 64 4b 65 79 2c 20 nsigned oldKey,
41b70 75 6e 73 69 67 6e 65 64 20 6e 65 77 4b 65 79 29 unsigned newKey)
41b80 3b 0a 20 20 76 6f 69 64 20 28 2a 78 54 72 75 6e ;. void (*xTrun
41b90 63 61 74 65 29 28 73 71 6c 69 74 65 33 5f 70 63 cate)(sqlite3_pc
41ba0 61 63 68 65 2a 2c 20 75 6e 73 69 67 6e 65 64 20 ache*, unsigned
41bb0 69 4c 69 6d 69 74 29 3b 0a 20 20 76 6f 69 64 20 iLimit);. void
41bc0 28 2a 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 (*xDestroy)(sqli
41bd0 74 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a 7d 3b te3_pcache*);.};
41be0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
41bf0 3a 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 : Online Backup
41c00 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 49 Object.** EXPERI
41c10 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 MENTAL.**.** The
41c20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 sqlite3_backup
41c30 6f 62 6a 65 63 74 20 72 65 63 6f 72 64 73 20 73 object records s
41c40 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e tate information
41c50 20 61 62 6f 75 74 20 61 6e 20 6f 6e 67 6f 69 6e about an ongoin
41c60 67 0a 2a 2a 20 6f 6e 6c 69 6e 65 20 62 61 63 6b g.** online back
41c70 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 54 up operation. T
41c80 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 he sqlite3_backu
41c90 70 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 p object is crea
41ca0 74 65 64 20 62 79 0a 2a 2a 20 61 20 63 61 6c 6c ted by.** a call
41cb0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 to [sqlite3_bac
41cc0 6b 75 70 5f 69 6e 69 74 28 29 5d 20 61 6e 64 20 kup_init()] and
41cd0 69 73 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 is destroyed by
41ce0 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 a call to.** [sq
41cf0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e lite3_backup_fin
41d00 69 73 68 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 ish()]..**.** Se
41d10 65 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 e Also: [Using t
41d20 68 65 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 he SQLite Online
41d30 20 42 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2f 0a Backup API].*/.
41d40 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 typedef struct s
41d50 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 73 71 qlite3_backup sq
41d60 6c 69 74 65 33 5f 62 61 63 6b 75 70 3b 0a 0a 2f lite3_backup;../
41d70 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f *.** CAPI3REF: O
41d80 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50 49 nline Backup API
41d90 2e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 ..** EXPERIMENTA
41da0 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 L.**.** This API
41db0 20 69 73 20 75 73 65 64 20 74 6f 20 6f 76 65 72 is used to over
41dc0 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e write the conten
41dd0 74 73 20 6f 66 20 6f 6e 65 20 64 61 74 61 62 61 ts of one databa
41de0 73 65 20 77 69 74 68 20 74 68 61 74 0a 2a 2a 20 se with that.**
41df0 6f 66 20 61 6e 6f 74 68 65 72 2e 20 49 74 20 69 of another. It i
41e00 73 20 75 73 65 66 75 6c 20 65 69 74 68 65 72 20 s useful either
41e10 66 6f 72 20 63 72 65 61 74 69 6e 67 20 62 61 63 for creating bac
41e20 6b 75 70 73 20 6f 66 20 64 61 74 61 62 61 73 65 kups of database
41e30 73 20 6f 72 0a 2a 2a 20 66 6f 72 20 63 6f 70 79 s or.** for copy
41e40 69 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 ing in-memory da
41e50 74 61 62 61 73 65 73 20 74 6f 20 6f 72 20 66 72 tabases to or fr
41e60 6f 6d 20 70 65 72 73 69 73 74 65 6e 74 20 66 69 om persistent fi
41e70 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 les. .**.** See
41e80 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 Also: [Using the
41e90 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 20 42 SQLite Online B
41ea0 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2a 0a 2a 2a ackup API].**.**
41eb0 20 45 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 Exclusive acces
41ec0 73 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f s is required to
41ed0 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
41ee0 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 74 68 database for th
41ef0 65 20 0a 2a 2a 20 64 75 72 61 74 69 6f 6e 20 6f e .** duration o
41f00 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 2e f the operation.
41f10 20 48 6f 77 65 76 65 72 20 74 68 65 20 73 6f 75 However the sou
41f20 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 rce database is
41f30 6f 6e 6c 79 0a 2a 2a 20 72 65 61 64 2d 6c 6f 63 only.** read-loc
41f40 6b 65 64 20 77 68 69 6c 65 20 69 74 20 69 73 20 ked while it is
41f50 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67 20 72 actually being r
41f60 65 61 64 2c 20 69 74 20 69 73 20 6e 6f 74 20 6c ead, it is not l
41f70 6f 63 6b 65 64 0a 2a 2a 20 63 6f 6e 74 69 6e 75 ocked.** continu
41f80 6f 75 73 6c 79 20 66 6f 72 20 74 68 65 20 65 6e ously for the en
41f90 74 69 72 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 tire operation.
41fa0 54 68 75 73 2c 20 74 68 65 20 62 61 63 6b 75 70 Thus, the backup
41fb0 20 6d 61 79 20 62 65 0a 2a 2a 20 70 65 72 66 6f may be.** perfo
41fc0 72 6d 65 64 20 6f 6e 20 61 20 6c 69 76 65 20 64 rmed on a live d
41fd0 61 74 61 62 61 73 65 20 77 69 74 68 6f 75 74 20 atabase without
41fe0 70 72 65 76 65 6e 74 69 6e 67 20 6f 74 68 65 72 preventing other
41ff0 20 75 73 65 72 73 20 66 72 6f 6d 0a 2a 2a 20 77 users from.** w
42000 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 riting to the da
42010 74 61 62 61 73 65 20 66 6f 72 20 61 6e 20 65 78 tabase for an ex
42020 74 65 6e 64 65 64 20 70 65 72 69 6f 64 20 6f 66 tended period of
42030 20 74 69 6d 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 6f time..** .** To
42040 20 70 65 72 66 6f 72 6d 20 61 20 62 61 63 6b 75 perform a backu
42050 70 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 2a 2a p operation: .**
42060 20 20 20 3c 6f 6c 3e 0a 2a 2a 20 20 20 20 20 3c <ol>.** <
42070 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 li><b>sqlite3_ba
42080 63 6b 75 70 5f 69 6e 69 74 28 29 3c 2f 62 3e 20 ckup_init()</b>
42090 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 74 is called once t
420a0 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 o initialize the
420b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 62 61 63 6b .** back
420c0 75 70 2c 20 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e up, .** <li>
420d0 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 <b>sqlite3_backu
420e0 70 5f 73 74 65 70 28 29 3c 2f 62 3e 20 69 73 20 p_step()</b> is
420f0 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 6d 6f called one or mo
42100 72 65 20 74 69 6d 65 73 20 74 6f 20 74 72 61 6e re times to tran
42110 73 66 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 sfer .**
42120 20 74 68 65 20 64 61 74 61 20 62 65 74 77 65 65 the data betwee
42130 6e 20 74 68 65 20 74 77 6f 20 64 61 74 61 62 61 n the two databa
42140 73 65 73 2c 20 61 6e 64 20 66 69 6e 61 6c 6c 79 ses, and finally
42150 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c 62 3e 73 .** <li><b>s
42160 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 qlite3_backup_fi
42170 6e 69 73 68 28 29 3c 2f 62 3e 20 69 73 20 63 61 nish()</b> is ca
42180 6c 6c 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20 lled to release
42190 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 0a 2a all resources .*
421a0 2a 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69 * associ
421b0 61 74 65 64 20 77 69 74 68 20 74 68 65 20 62 61 ated with the ba
421c0 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 ckup operation.
421d0 0a 2a 2a 20 20 20 3c 2f 6f 6c 3e 0a 2a 2a 20 54 .** </ol>.** T
421e0 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 65 here should be e
421f0 78 61 63 74 6c 79 20 6f 6e 65 20 63 61 6c 6c 20 xactly one call
42200 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
42210 70 5f 66 69 6e 69 73 68 28 29 20 66 6f 72 20 65 p_finish() for e
42220 61 63 68 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 ach.** successfu
42230 6c 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 l call to sqlite
42240 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2e 3_backup_init().
42250 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 .**.** <b>sqlite
42260 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 3c 3_backup_init()<
42270 2f 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 /b>.**.** The fi
42280 72 73 74 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 rst two argument
42290 73 20 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c s passed to [sql
422a0 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 ite3_backup_init
422b0 28 29 5d 20 61 72 65 20 74 68 65 20 64 61 74 61 ()] are the data
422c0 62 61 73 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 61 base.** handle a
422d0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
422e0 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 he destination d
422f0 61 74 61 62 61 73 65 20 61 6e 64 20 74 68 65 20 atabase and the
42300 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 0a 2a database name .*
42310 2a 20 75 73 65 64 20 74 6f 20 61 74 74 61 63 68 * used to attach
42320 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
42330 20 64 61 74 61 62 61 73 65 20 74 6f 20 74 68 65 database to the
42340 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 64 61 74 handle. The dat
42350 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 69 73 abase name.** is
42360 20 22 6d 61 69 6e 22 20 66 6f 72 20 74 68 65 20 "main" for the
42370 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 22 main database, "
42380 74 65 6d 70 22 20 66 6f 72 20 74 68 65 20 74 65 temp" for the te
42390 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 mporary database
423a0 2c 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 , or.** the name
423b0 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61 specified as pa
423c0 72 74 20 6f 66 20 74 68 65 20 5b 41 54 54 41 43 rt of the [ATTAC
423d0 48 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20 H] statement if
423e0 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
423f0 69 73 0a 2a 2a 20 61 6e 20 61 74 74 61 63 68 65 is.** an attache
42400 64 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 d database. The
42410 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 third and fourth
42420 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65 arguments passe
42430 64 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 65 33 d to .** sqlite3
42440 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 20 69 _backup_init() i
42450 64 65 6e 74 69 66 79 20 74 68 65 20 5b 64 61 74 dentify the [dat
42460 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
42470 5d 0a 2a 2a 20 61 6e 64 20 64 61 74 61 62 61 73 ].** and databas
42480 65 20 6e 61 6d 65 20 75 73 65 64 0a 2a 2a 20 74 e name used.** t
42490 6f 20 61 63 63 65 73 73 20 74 68 65 20 73 6f 75 o access the sou
424a0 72 63 65 20 64 61 74 61 62 61 73 65 2e 20 54 68 rce database. Th
424b0 65 20 76 61 6c 75 65 73 20 70 61 73 73 65 64 20 e values passed
424c0 66 6f 72 20 74 68 65 20 73 6f 75 72 63 65 20 61 for the source a
424d0 6e 64 20 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 nd .** destinati
424e0 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e on [database con
424f0 6e 65 63 74 69 6f 6e 5d 20 70 61 72 61 6d 65 74 nection] paramet
42500 65 72 73 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 ers must not be
42510 74 68 65 20 73 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 the same..**.**
42520 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
42530 72 73 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65 rs within sqlite
42540 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2c 3_backup_init(),
42550 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 20 72 65 then NULL is re
42560 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 61 6e turned.** and an
42570 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 error code and
42580 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72 error message wr
42590 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 5b itten into the [
425a0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
425b0 69 6f 6e 5d 20 0a 2a 2a 20 70 61 73 73 65 64 20 ion] .** passed
425c0 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 as the first arg
425d0 75 6d 65 6e 74 2e 20 54 68 65 79 20 6d 61 79 20 ument. They may
425e0 62 65 20 72 65 74 72 69 65 76 65 64 20 75 73 69 be retrieved usi
425f0 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 ng the.** [sqlit
42600 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20 5b e3_errcode()], [
42610 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 sqlite3_errmsg()
42620 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f ], and [sqlite3_
42630 65 72 72 6d 73 67 31 36 28 29 5d 20 66 75 6e 63 errmsg16()] func
42640 74 69 6f 6e 73 2e 0a 2a 2a 20 4f 74 68 65 72 77 tions..** Otherw
42650 69 73 65 2c 20 69 66 20 73 75 63 63 65 73 73 66 ise, if successf
42660 75 6c 2c 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ul, a pointer to
42670 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 an [sqlite3_bac
42680 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 73 0a 2a kup] object is.*
42690 2a 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 * returned. This
426a0 20 70 6f 69 6e 74 65 72 20 6d 61 79 20 62 65 20 pointer may be
426b0 75 73 65 64 20 77 69 74 68 20 74 68 65 20 73 71 used with the sq
426c0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
426d0 70 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 p() and.** sqlit
426e0 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 e3_backup_finish
426f0 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 () functions to
42700 70 65 72 66 6f 72 6d 20 74 68 65 20 73 70 65 63 perform the spec
42710 69 66 69 65 64 20 62 61 63 6b 75 70 20 0a 2a 2a ified backup .**
42720 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a operation..**.*
42730 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 * <b>sqlite3_bac
42740 6b 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e 0a 2a kup_step()</b>.*
42750 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 5b 73 *.** Function [s
42760 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
42770 65 70 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f ep()] is used to
42780 20 63 6f 70 79 20 75 70 20 74 6f 20 6e 50 61 67 copy up to nPag
42790 65 20 70 61 67 65 73 20 62 65 74 77 65 65 6e 20 e pages between
427a0 0a 2a 2a 20 74 68 65 20 73 6f 75 72 63 65 20 61 .** the source a
427b0 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 nd destination d
427c0 61 74 61 62 61 73 65 73 2c 20 77 68 65 72 65 20 atabases, where
427d0 6e 50 61 67 65 20 69 73 20 74 68 65 20 76 61 6c nPage is the val
427e0 75 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 73 65 ue of the .** se
427f0 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 70 cond parameter p
42800 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 assed to sqlite3
42810 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 _backup_step().
42820 49 66 20 6e 50 61 67 65 20 69 73 20 61 20 6e 65 If nPage is a ne
42830 67 61 74 69 76 65 0a 2a 2a 20 76 61 6c 75 65 2c gative.** value,
42840 20 61 6c 6c 20 72 65 6d 61 69 6e 69 6e 67 20 73 all remaining s
42850 6f 75 72 63 65 20 70 61 67 65 73 20 61 72 65 20 ource pages are
42860 63 6f 70 69 65 64 2e 20 49 66 20 74 68 65 20 72 copied. If the r
42870 65 71 75 69 72 65 64 20 70 61 67 65 73 20 61 72 equired pages ar
42880 65 20 0a 2a 2a 20 73 75 63 63 65 73 66 75 6c 6c e .** succesfull
42890 79 20 63 6f 70 69 65 64 2c 20 62 75 74 20 74 68 y copied, but th
428a0 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 6f ere are still mo
428b0 72 65 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79 re pages to copy
428c0 20 62 65 66 6f 72 65 20 74 68 65 20 0a 2a 2a 20 before the .**
428d0 62 61 63 6b 75 70 20 69 73 20 63 6f 6d 70 6c 65 backup is comple
428e0 74 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 5b te, it returns [
428f0 53 51 4c 49 54 45 5f 4f 4b 5d 2e 20 49 66 20 6e SQLITE_OK]. If n
42900 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 o error occured
42910 61 6e 64 20 74 68 65 72 65 20 0a 2a 2a 20 61 72 and there .** ar
42920 65 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20 e no more pages
42930 74 6f 20 63 6f 70 79 2c 20 74 68 65 6e 20 5b 53 to copy, then [S
42940 51 4c 49 54 45 5f 44 4f 4e 45 5d 20 69 73 20 72 QLITE_DONE] is r
42950 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 eturned. If an e
42960 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c rror .** occurs,
42970 20 74 68 65 6e 20 61 6e 20 53 51 4c 69 74 65 20 then an SQLite
42980 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
42990 74 75 72 6e 65 64 2e 20 41 73 20 77 65 6c 6c 20 turned. As well
429a0 61 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 61 as [SQLITE_OK] a
429b0 6e 64 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f nd.** [SQLITE_DO
429c0 4e 45 5d 2c 20 61 20 63 61 6c 6c 20 74 6f 20 73 NE], a call to s
429d0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
429e0 65 70 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 ep() may return
429f0 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 [SQLITE_READONLY
42a00 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f ],.** [SQLITE_NO
42a10 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55 MEM], [SQLITE_BU
42a20 53 59 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 SY], [SQLITE_LOC
42a30 4b 45 44 5d 2c 20 6f 72 20 61 6e 0a 2a 2a 20 5b KED], or an.** [
42a40 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 SQLITE_IOERR_ACC
42a50 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45 ESS | SQLITE_IOE
42a60 52 52 5f 58 58 58 5d 20 65 78 74 65 6e 64 65 64 RR_XXX] extended
42a70 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a error code..**.
42a80 2a 2a 20 41 73 20 77 65 6c 6c 20 61 73 20 74 68 ** As well as th
42a90 65 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65 e case where the
42aa0 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 destination dat
42ab0 61 62 61 73 65 20 66 69 6c 65 20 77 61 73 20 6f abase file was o
42ac0 70 65 6e 65 64 20 66 6f 72 0a 2a 2a 20 72 65 61 pened for.** rea
42ad0 64 2d 6f 6e 6c 79 20 61 63 63 65 73 73 2c 20 73 d-only access, s
42ae0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
42af0 65 70 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 ep() may return
42b00 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 [SQLITE_READONLY
42b10 5d 20 69 66 0a 2a 2a 20 74 68 65 20 64 65 73 74 ] if.** the dest
42b20 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e ination is an in
42b30 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
42b40 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e with a differen
42b50 74 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a 20 66 t page size.** f
42b60 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 64 rom the source d
42b70 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 atabase..**.** I
42b80 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 f sqlite3_backup
42b90 5f 73 74 65 70 28 29 20 63 61 6e 6e 6f 74 20 6f _step() cannot o
42ba0 62 74 61 69 6e 20 61 20 72 65 71 75 69 72 65 64 btain a required
42bb0 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 6c 6f 63 file-system loc
42bc0 6b 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 5b k, then.** the [
42bd0 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e sqlite3_busy_han
42be0 64 6c 65 72 20 7c 20 62 75 73 79 2d 68 61 6e 64 dler | busy-hand
42bf0 6c 65 72 20 66 75 6e 63 74 69 6f 6e 5d 0a 2a 2a ler function].**
42c00 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69 66 20 is invoked (if
42c10 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64 one is specified
42c20 29 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 62 75 ). If the .** bu
42c30 73 79 2d 68 61 6e 64 6c 65 72 20 72 65 74 75 72 sy-handler retur
42c40 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 62 65 66 6f ns non-zero befo
42c50 72 65 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 re the lock is a
42c60 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20 0a vailable, then .
42c70 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d ** [SQLITE_BUSY]
42c80 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 is returned to
42c90 74 68 65 20 63 61 6c 6c 65 72 2e 20 49 6e 20 74 the caller. In t
42ca0 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61 6c his case the cal
42cb0 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f l to.** sqlite3_
42cc0 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 backup_step() ca
42cd0 6e 20 62 65 20 72 65 74 72 69 65 64 20 6c 61 74 n be retried lat
42ce0 65 72 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 er. If the sourc
42cf0 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 e.** [database c
42d00 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 69 73 onnection].** is
42d10 20 62 65 69 6e 67 20 75 73 65 64 20 74 6f 20 77 being used to w
42d20 72 69 74 65 20 74 6f 20 74 68 65 20 73 6f 75 72 rite to the sour
42d30 63 65 20 64 61 74 61 62 61 73 65 20 77 68 65 6e ce database when
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 0a 2a 2a 20 69 73 20 63 61 6c step().** is cal
42d60 6c 65 64 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 led, then [SQLIT
42d70 45 5f 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 74 E_LOCKED] is ret
42d80 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c urned immediatel
42d90 79 2e 20 41 67 61 69 6e 2c 20 69 6e 20 74 68 69 y. Again, in thi
42da0 73 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 63 61 s.** case the ca
42db0 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 ll to sqlite3_ba
42dc0 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 6e 20 ckup_step() can
42dd0 62 65 20 72 65 74 72 69 65 64 20 6c 61 74 65 72 be retried later
42de0 20 6f 6e 2e 20 49 66 0a 2a 2a 20 5b 53 51 4c 49 on. If.** [SQLI
42df0 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20 TE_IOERR_ACCESS
42e00 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58 | SQLITE_IOERR_X
42e10 58 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d XX], [SQLITE_NOM
42e20 45 4d 5d 2c 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 EM], or.** [SQLI
42e30 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 20 69 73 20 TE_READONLY] is
42e40 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 0a returned, then .
42e50 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70 ** there is no p
42e60 6f 69 6e 74 20 69 6e 20 72 65 74 72 79 69 6e 67 oint in retrying
42e70 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c the call to sql
42e80 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
42e90 28 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 65 72 (). These .** er
42ea0 72 6f 72 73 20 61 72 65 20 63 6f 6e 73 69 64 65 rors are conside
42eb0 72 65 64 20 66 61 74 61 6c 2e 20 41 74 20 74 68 red fatal. At th
42ec0 69 73 20 70 6f 69 6e 74 20 74 68 65 20 61 70 70 is point the app
42ed0 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 61 63 lication must ac
42ee0 63 65 70 74 20 0a 2a 2a 20 74 68 61 74 20 74 68 cept .** that th
42ef0 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 e backup operati
42f00 6f 6e 20 68 61 73 20 66 61 69 6c 65 64 20 61 6e on has failed an
42f10 64 20 70 61 73 73 20 74 68 65 20 62 61 63 6b 75 d pass the backu
42f20 70 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 6e 64 p operation hand
42f30 6c 65 20 0a 2a 2a 20 74 6f 20 74 68 65 20 73 71 le .** to the sq
42f40 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e lite3_backup_fin
42f50 69 73 68 28 29 20 74 6f 20 72 65 6c 65 61 73 65 ish() to release
42f60 20 61 73 73 6f 63 69 61 74 65 64 20 72 65 73 6f associated reso
42f70 75 72 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 6c urces..**.** Fol
42f80 6c 6f 77 69 6e 67 20 74 68 65 20 66 69 72 73 74 lowing the first
42f90 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
42fa0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2c 20 _backup_step(),
42fb0 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
42fc0 6b 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 k is.** obtained
42fd0 20 6f 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 on the destinat
42fe0 69 6f 6e 20 66 69 6c 65 2e 20 49 74 20 69 73 20 ion file. It is
42ff0 6e 6f 74 20 72 65 6c 65 61 73 65 64 20 75 6e 74 not released unt
43000 69 6c 20 65 69 74 68 65 72 20 0a 2a 2a 20 73 71 il either .** sq
43010 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e lite3_backup_fin
43020 69 73 68 28 29 20 69 73 20 63 61 6c 6c 65 64 20 ish() is called
43030 6f 72 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 or the backup op
43040 65 72 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 6c eration is compl
43050 65 74 65 20 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 ete .** and sqli
43060 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 te3_backup_step(
43070 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 ) returns [SQLIT
43080 45 5f 44 4f 4e 45 5d 2e 20 41 64 64 69 74 69 6f E_DONE]. Additio
43090 6e 61 6c 6c 79 2c 20 65 61 63 68 20 74 69 6d 65 nally, each time
430a0 20 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 73 .** a call to s
430b0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
430c0 65 70 28 29 20 69 73 20 6d 61 64 65 20 61 20 5b ep() is made a [
430d0 73 68 61 72 65 64 20 6c 6f 63 6b 5d 20 69 73 20 shared lock] is
430e0 6f 62 74 61 69 6e 65 64 20 6f 6e 0a 2a 2a 20 74 obtained on.** t
430f0 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
43100 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 6c 6f se file. This lo
43110 63 6b 20 69 73 20 72 65 6c 65 61 73 65 64 20 62 ck is released b
43120 65 66 6f 72 65 20 74 68 65 0a 2a 2a 20 73 71 6c efore the.** sql
43130 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
43140 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e () call returns.
43150 20 42 65 63 61 75 73 65 20 74 68 65 20 73 6f 75 Because the sou
43160 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 rce database is
43170 6e 6f 74 0a 2a 2a 20 6c 6f 63 6b 65 64 20 62 65 not.** locked be
43180 74 77 65 65 6e 20 63 61 6c 6c 73 20 74 6f 20 73 tween calls to s
43190 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
431a0 65 70 28 29 2c 20 69 74 20 6d 61 79 20 62 65 20 ep(), it may be
431b0 6d 6f 64 69 66 69 65 64 20 6d 69 64 2d 77 61 79 modified mid-way
431c0 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 .** through the
431d0 62 61 63 6b 75 70 20 70 72 6f 63 65 64 75 72 65 backup procedure
431e0 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 . If the source
431f0 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f 64 69 database is modi
43200 66 69 65 64 20 62 79 20 61 6e 0a 2a 2a 20 65 78 fied by an.** ex
43210 74 65 72 6e 61 6c 20 70 72 6f 63 65 73 73 20 6f ternal process o
43220 72 20 76 69 61 20 61 20 64 61 74 61 62 61 73 65 r via a database
43230 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 74 68 65 connection othe
43240 72 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 20 62 r than the one b
43250 65 69 6e 67 0a 2a 2a 20 75 73 65 64 20 62 79 20 eing.** used by
43260 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 the backup opera
43270 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 62 tion, then the b
43280 61 63 6b 75 70 20 77 69 6c 6c 20 62 65 20 74 72 ackup will be tr
43290 61 6e 73 70 61 72 65 6e 74 6c 79 0a 2a 2a 20 72 ansparently.** r
432a0 65 73 74 61 72 74 65 64 20 62 79 20 74 68 65 20 estarted by the
432b0 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c next call to sql
432c0 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
432d0 28 29 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 (). If the sourc
432e0 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69 e .** database i
432f0 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 74 68 s modified by th
43300 65 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 e using the same
43310 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
43320 74 69 6f 6e 20 61 73 20 69 73 20 75 73 65 64 0a tion as is used.
43330 2a 2a 20 62 79 20 74 68 65 20 62 61 63 6b 75 70 ** by the backup
43340 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e operation, then
43350 20 74 68 65 20 62 61 63 6b 75 70 20 64 61 74 61 the backup data
43360 62 61 73 65 20 69 73 20 74 72 61 6e 73 70 61 72 base is transpar
43370 65 6e 74 6c 79 20 0a 2a 2a 20 75 70 64 61 74 65 ently .** update
43380 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 d at the same ti
43390 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c me..**.** <b>sql
433a0 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 ite3_backup_fini
433b0 73 68 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 4f sh()</b>.**.** O
433c0 6e 63 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b nce sqlite3_back
433d0 75 70 5f 73 74 65 70 28 29 20 68 61 73 20 72 65 up_step() has re
433e0 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 44 turned [SQLITE_D
433f0 4f 4e 45 5d 2c 20 6f 72 20 77 68 65 6e 20 74 68 ONE], or when th
43400 65 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f e .** applicatio
43410 6e 20 77 69 73 68 65 73 20 74 6f 20 61 62 61 6e n wishes to aban
43420 64 6f 6e 20 74 68 65 20 62 61 63 6b 75 70 20 6f don the backup o
43430 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 20 5b 73 peration, the [s
43440 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 0a 2a qlite3_backup].*
43450 2a 20 6f 62 6a 65 63 74 20 73 68 6f 75 6c 64 20 * object should
43460 62 65 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c be passed to sql
43470 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 ite3_backup_fini
43480 73 68 28 29 2e 20 54 68 69 73 20 72 65 6c 65 61 sh(). This relea
43490 73 65 73 20 61 6c 6c 0a 2a 2a 20 72 65 73 6f 75 ses all.** resou
434a0 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 rces associated
434b0 77 69 74 68 20 74 68 65 20 62 61 63 6b 75 70 20 with the backup
434c0 6f 70 65 72 61 74 69 6f 6e 2e 20 49 66 20 73 71 operation. If sq
434d0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
434e0 70 28 29 0a 2a 2a 20 68 61 73 20 6e 6f 74 20 79 p().** has not y
434f0 65 74 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c et returned [SQL
43500 49 54 45 5f 44 4f 4e 45 5d 2c 20 74 68 65 6e 20 ITE_DONE], then
43510 61 6e 79 20 61 63 74 69 76 65 20 77 72 69 74 65 any active write
43520 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 -transaction on
43530 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 the.** destinati
43540 6f 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 72 on database is r
43550 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 65 20 olled back. The
43560 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d [sqlite3_backup]
43570 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 76 61 6c object is inval
43580 69 64 0a 2a 2a 20 61 6e 64 20 6d 61 79 20 6e 6f id.** and may no
43590 74 20 62 65 20 75 73 65 64 20 66 6f 6c 6c 6f 77 t be used follow
435a0 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 ing a call to sq
435b0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e lite3_backup_fin
435c0 69 73 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ish()..**.** The
435d0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
435e0 62 79 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 by sqlite3_backu
435f0 70 5f 66 69 6e 69 73 68 20 69 73 20 5b 53 51 4c p_finish is [SQL
43600 49 54 45 5f 4f 4b 5d 20 69 66 20 6e 6f 20 65 72 ITE_OK] if no er
43610 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 72 65 64 2c ror.** occurred,
43620 20 72 65 67 61 72 64 6c 65 73 73 20 6f 72 20 77 regardless or w
43630 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 71 hether or not sq
43640 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
43650 70 28 29 20 77 61 73 20 63 61 6c 6c 65 64 0a 2a p() was called.*
43660 2a 20 61 20 73 75 66 66 69 63 69 65 6e 74 20 6e * a sufficient n
43670 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 umber of times t
43680 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 62 o complete the b
43690 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e ackup operation.
436a0 20 4f 72 2c 20 69 66 0a 2a 2a 20 61 6e 20 6f 75 Or, if.** an ou
436b0 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64 t-of-memory cond
436c0 69 74 69 6f 6e 20 6f 72 20 49 4f 20 65 72 72 6f ition or IO erro
436d0 72 20 6f 63 63 75 72 65 64 20 64 75 72 69 6e 67 r occured during
436e0 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 a call to.** sq
436f0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
43700 70 28 29 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 p() then [SQLITE
43710 5f 4e 4f 4d 45 4d 5d 20 6f 72 20 61 6e 0a 2a 2a _NOMEM] or an.**
43720 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 [SQLITE_IOERR_A
43730 43 43 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 CCESS | SQLITE_I
43740 4f 45 52 52 5f 58 58 58 5d 20 65 72 72 6f 72 20 OERR_XXX] error
43750 63 6f 64 65 0a 2a 2a 20 69 73 20 72 65 74 75 72 code.** is retur
43760 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ned. In this cas
43770 65 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 e the error code
43780 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 and an error me
43790 73 73 61 67 65 20 61 72 65 0a 2a 2a 20 77 72 69 ssage are.** wri
437a0 74 74 65 6e 20 74 6f 20 74 68 65 20 64 65 73 74 tten to the dest
437b0 69 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 73 ination [databas
437c0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a e connection]..*
437d0 2a 0a 2a 2a 20 41 20 72 65 74 75 72 6e 20 6f 66 *.** A return of
437e0 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f [SQLITE_BUSY] o
437f0 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 r [SQLITE_LOCKED
43800 5d 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 62 ] from sqlite3_b
43810 61 63 6b 75 70 5f 73 74 65 70 28 29 20 69 73 0a ackup_step() is.
43820 2a 2a 20 6e 6f 74 20 61 20 70 65 72 6d 61 6e 65 ** not a permane
43830 6e 74 20 65 72 72 6f 72 20 61 6e 64 20 64 6f 65 nt error and doe
43840 73 20 6e 6f 74 20 61 66 66 65 63 74 20 74 68 65 s not affect the
43850 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 return value of
43860 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b .** sqlite3_back
43870 75 70 5f 66 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a up_finish()..**.
43880 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 ** <b>sqlite3_ba
43890 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 ckup_remaining()
438a0 2c 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 , sqlite3_backup
438b0 5f 70 61 67 65 63 6f 75 6e 74 28 29 3c 2f 62 3e _pagecount()</b>
438c0 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61 6c 6c .**.** Each call
438d0 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b to sqlite3_back
438e0 75 70 5f 73 74 65 70 28 29 20 73 65 74 73 20 74 up_step() sets t
438f0 77 6f 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 wo values stored
43900 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62 internally.** b
43910 79 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61 y an [sqlite3_ba
43920 63 6b 75 70 5d 20 6f 62 6a 65 63 74 2e 20 54 68 ckup] object. Th
43930 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
43940 73 20 73 74 69 6c 6c 20 74 6f 20 62 65 20 62 61 s still to be ba
43950 63 6b 65 64 0a 2a 2a 20 75 70 2c 20 77 68 69 63 cked.** up, whic
43960 68 20 6d 61 79 20 62 65 20 71 75 65 72 69 65 64 h may be queried
43970 20 62 79 20 73 71 6c 69 74 65 33 5f 62 61 63 6b by sqlite3_back
43980 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 2c 20 up_remaining(),
43990 61 6e 64 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a and the total.**
439a0 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
439b0 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 in the source d
439c0 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77 68 atabase file, wh
439d0 69 63 68 20 6d 61 79 20 62 65 20 71 75 65 72 69 ich may be queri
439e0 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 ed by.** sqlite3
439f0 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e _backup_pagecoun
43a00 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 t()..**.** The v
43a10 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 62 alues returned b
43a20 79 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e y these function
43a30 73 20 61 72 65 20 6f 6e 6c 79 20 75 70 64 61 74 s are only updat
43a40 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 ed by.** sqlite3
43a50 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 _backup_step().
43a60 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 If the source da
43a70 74 61 62 61 73 65 20 69 73 20 6d 6f 64 69 66 69 tabase is modifi
43a80 65 64 20 64 75 72 69 6e 67 20 61 20 62 61 63 6b ed during a back
43a90 75 70 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2c up.** operation,
43aa0 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 73 then the values
43ab0 20 61 72 65 20 6e 6f 74 20 75 70 64 61 74 65 64 are not updated
43ac0 20 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72 20 to account for
43ad0 61 6e 79 20 65 78 74 72 61 0a 2a 2a 20 70 61 67 any extra.** pag
43ae0 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 es that need to
43af0 62 65 20 75 70 64 61 74 65 64 20 6f 72 20 74 68 be updated or th
43b00 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 73 6f e size of the so
43b10 75 72 63 65 20 64 61 74 61 62 61 73 65 20 66 69 urce database fi
43b20 6c 65 0a 2a 2a 20 63 68 61 6e 67 69 6e 67 2e 0a le.** changing..
43b30 2a 2a 0a 2a 2a 20 3c 62 3e 43 6f 6e 63 75 72 72 **.** <b>Concurr
43b40 65 6e 74 20 55 73 61 67 65 20 6f 66 20 44 61 74 ent Usage of Dat
43b50 61 62 61 73 65 20 48 61 6e 64 6c 65 73 3c 2f 62 abase Handles</b
43b60 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 75 72 >.**.** The sour
43b70 63 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e ce [database con
43b80 6e 65 63 74 69 6f 6e 5d 20 6d 61 79 20 62 65 20 nection] may be
43b90 75 73 65 64 20 62 79 20 74 68 65 20 61 70 70 6c used by the appl
43ba0 69 63 61 74 69 6f 6e 20 66 6f 72 20 6f 74 68 65 ication for othe
43bb0 72 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 77 68 r.** purposes wh
43bc0 69 6c 65 20 61 20 62 61 63 6b 75 70 20 6f 70 65 ile a backup ope
43bd0 72 61 74 69 6f 6e 20 69 73 20 75 6e 64 65 72 77 ration is underw
43be0 61 79 20 6f 72 20 62 65 69 6e 67 20 69 6e 69 74 ay or being init
43bf0 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 20 53 ialized..** If S
43c00 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 QLite is compile
43c10 64 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 65 64 d and configured
43c20 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 72 65 to support thre
43c30 61 64 73 61 66 65 20 64 61 74 61 62 61 73 65 0a adsafe database.
43c40 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20 ** connections,
43c50 74 68 65 6e 20 74 68 65 20 73 6f 75 72 63 65 20 then the source
43c60 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
43c70 69 6f 6e 20 6d 61 79 20 62 65 20 75 73 65 64 20 ion may be used
43c80 63 6f 6e 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 concurrently.**
43c90 66 72 6f 6d 20 77 69 74 68 69 6e 20 6f 74 68 65 from within othe
43ca0 72 20 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a r threads..**.**
43cb0 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 61 70 However, the ap
43cc0 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 67 plication must g
43cd0 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 uarantee that th
43ce0 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 e destination da
43cf0 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 tabase.** connec
43d00 74 69 6f 6e 20 68 61 6e 64 6c 65 20 69 73 20 6e tion handle is n
43d10 6f 74 20 70 61 73 73 65 64 20 74 6f 20 61 6e 79 ot passed to any
43d20 20 6f 74 68 65 72 20 41 50 49 20 28 62 79 20 61 other API (by a
43d30 6e 79 20 74 68 72 65 61 64 29 20 61 66 74 65 72 ny thread) after
43d40 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 .** sqlite3_bac
43d50 6b 75 70 5f 69 6e 69 74 28 29 20 69 73 20 63 61 kup_init() is ca
43d60 6c 6c 65 64 20 61 6e 64 20 62 65 66 6f 72 65 20 lled and before
43d70 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
43d80 67 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c g call to.** sql
43d90 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 ite3_backup_fini
43da0 73 68 28 29 2e 20 55 6e 66 6f 72 74 75 6e 61 74 sh(). Unfortunat
43db0 65 6c 79 20 53 51 4c 69 74 65 20 64 6f 65 73 20 ely SQLite does
43dc0 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 63 68 not currently ch
43dd0 65 63 6b 0a 2a 2a 20 66 6f 72 20 74 68 69 73 2c eck.** for this,
43de0 20 69 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 if the applicat
43df0 69 6f 6e 20 64 6f 65 73 20 75 73 65 20 74 68 65 ion does use the
43e00 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b 64 61 destination [da
43e10 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
43e20 6e 5d 0a 2a 2a 20 66 6f 72 20 73 6f 6d 65 20 6f n].** for some o
43e30 74 68 65 72 20 70 75 72 70 6f 73 65 20 64 75 72 ther purpose dur
43e40 69 6e 67 20 61 20 62 61 63 6b 75 70 20 6f 70 65 ing a backup ope
43e50 72 61 74 69 6f 6e 2c 20 74 68 69 6e 67 73 20 6d ration, things m
43e60 61 79 20 61 70 70 65 61 72 20 74 6f 0a 2a 2a 20 ay appear to.**
43e70 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 62 work correctly b
43e80 75 74 20 69 6e 20 66 61 63 74 20 62 65 20 73 75 ut in fact be su
43e90 62 74 6c 79 20 6d 61 6c 66 75 6e 63 74 69 6f 6e btly malfunction
43ea0 69 6e 67 2e 20 20 55 73 65 20 6f 66 20 74 68 65 ing. Use of the
43eb0 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 .** destination
43ec0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
43ed0 69 6f 6e 20 77 68 69 6c 65 20 61 20 62 61 63 6b ion while a back
43ee0 75 70 20 69 73 20 69 6e 20 70 72 6f 67 72 65 73 up is in progres
43ef0 73 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 6f 20 s might.** also
43f00 63 61 75 73 65 20 61 20 6d 75 74 65 78 20 64 65 cause a mutex de
43f10 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 75 adlock..**.** Fu
43f20 72 74 68 65 72 6d 6f 72 65 2c 20 69 66 20 72 75 rthermore, if ru
43f30 6e 6e 69 6e 67 20 69 6e 20 5b 73 68 61 72 65 64 nning in [shared
43f40 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20 74 68 cache mode], th
43f50 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 e application mu
43f60 73 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 st.** guarantee
43f70 74 68 61 74 20 74 68 65 20 73 68 61 72 65 64 20 that the shared
43f80 63 61 63 68 65 20 75 73 65 64 20 62 79 20 74 68 cache used by th
43f90 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 e destination da
43fa0 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 6e 6f 74 tabase.** is not
43fb0 20 61 63 63 65 73 73 65 64 20 77 68 69 6c 65 20 accessed while
43fc0 74 68 65 20 62 61 63 6b 75 70 20 69 73 20 72 75 the backup is ru
43fd0 6e 6e 69 6e 67 2e 20 49 6e 20 70 72 61 63 74 69 nning. In practi
43fe0 63 65 20 74 68 69 73 20 6d 65 61 6e 73 0a 2a 2a ce this means.**
43ff0 20 74 68 61 74 20 74 68 65 20 61 70 70 6c 69 63 that the applic
44000 61 74 69 6f 6e 20 6d 75 73 74 20 67 75 61 72 61 ation must guara
44010 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 66 69 ntee that the fi
44020 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 20 62 le-system file b
44030 65 69 6e 67 20 0a 2a 2a 20 62 61 63 6b 65 64 20 eing .** backed
44040 75 70 20 74 6f 20 69 73 20 6e 6f 74 20 61 63 63 up to is not acc
44050 65 73 73 65 64 20 62 79 20 61 6e 79 20 63 6f 6e essed by any con
44060 6e 65 63 74 69 6f 6e 20 77 69 74 68 69 6e 20 74 nection within t
44070 68 65 20 70 72 6f 63 65 73 73 2c 0a 2a 2a 20 6e he process,.** n
44080 6f 74 20 6a 75 73 74 20 74 68 65 20 73 70 65 63 ot just the spec
44090 69 66 69 63 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 ific connection
440a0 74 68 61 74 20 77 61 73 20 70 61 73 73 65 64 20 that was passed
440b0 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
440c0 70 5f 69 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 p_init()..**.**
440d0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 The [sqlite3_bac
440e0 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 74 73 65 kup] object itse
440f0 6c 66 20 69 73 20 70 61 72 74 69 61 6c 6c 79 20 lf is partially
44100 74 68 72 65 61 64 73 61 66 65 2e 20 4d 75 6c 74 threadsafe. Mult
44110 69 70 6c 65 20 0a 2a 2a 20 74 68 72 65 61 64 73 iple .** threads
44120 20 6d 61 79 20 73 61 66 65 6c 79 20 6d 61 6b 65 may safely make
44130 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 63 75 72 multiple concur
44140 72 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 rent calls to sq
44150 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
44160 70 28 29 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c p()..** However,
44170 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 the sqlite3_bac
44180 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 kup_remaining()
44190 61 6e 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b and sqlite3_back
441a0 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 0a 2a up_pagecount().*
441b0 2a 20 41 50 49 73 20 61 72 65 20 6e 6f 74 20 73 * APIs are not s
441c0 74 72 69 63 74 6c 79 20 73 70 65 61 6b 69 6e 67 trictly speaking
441d0 20 74 68 72 65 61 64 73 61 66 65 2e 20 49 66 20 threadsafe. If
441e0 74 68 65 79 20 61 72 65 20 69 6e 76 6f 6b 65 64 they are invoked
441f0 20 61 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 at the.** same
44200 74 69 6d 65 20 61 73 20 61 6e 6f 74 68 65 72 20 time as another
44210 74 68 72 65 61 64 20 69 73 20 69 6e 76 6f 6b 69 thread is invoki
44220 6e 67 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ng sqlite3_backu
44230 70 5f 73 74 65 70 28 29 20 69 74 20 69 73 0a 2a p_step() it is.*
44240 2a 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 * possible that
44250 74 68 65 79 20 72 65 74 75 72 6e 20 69 6e 76 61 they return inva
44260 6c 69 64 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53 lid values..*/.S
44270 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
44280 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c 69 74 65 3_backup *sqlite
44290 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 0a 20 3_backup_init(.
442a0 20 73 71 6c 69 74 65 33 20 2a 70 44 65 73 74 2c sqlite3 *pDest,
442b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
442c0 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69 /* Desti
442d0 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 nation database
442e0 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 handle */. cons
442f0 74 20 63 68 61 72 20 2a 7a 44 65 73 74 4e 61 6d t char *zDestNam
44300 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
44310 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f /* Destinatio
44320 6e 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 n database name
44330 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 53 */. sqlite3 *pS
44340 6f 75 72 63 65 2c 20 20 20 20 20 20 20 20 20 20 ource,
44350 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
44360 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68 ource database h
44370 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 andle */. const
44380 20 63 68 61 72 20 2a 7a 53 6f 75 72 63 65 4e 61 char *zSourceNa
44390 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 me
443a0 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 61 /* Source data
443b0 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a base name */.);.
443c0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
443d0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
443e0 65 70 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ep(sqlite3_backu
443f0 70 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65 29 p *p, int nPage)
44400 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
44410 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
44420 66 69 6e 69 73 68 28 73 71 6c 69 74 65 33 5f 62 finish(sqlite3_b
44430 61 63 6b 75 70 20 2a 70 29 3b 0a 53 51 4c 49 54 ackup *p);.SQLIT
44440 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
44450 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 3_backup_remaini
44460 6e 67 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ng(sqlite3_backu
44470 70 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 41 50 p *p);.SQLITE_AP
44480 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 I int sqlite3_ba
44490 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 73 ckup_pagecount(s
444a0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 qlite3_backup *p
444b0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
444c0 45 46 3a 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66 EF: Unlock Notif
444d0 69 63 61 74 69 6f 6e 0a 2a 2a 20 45 58 50 45 52 ication.** EXPER
444e0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 57 68 IMENTAL.**.** Wh
444f0 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 73 68 en running in sh
44500 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c ared-cache mode,
44510 20 61 20 64 61 74 61 62 61 73 65 20 6f 70 65 72 a database oper
44520 61 74 69 6f 6e 20 6d 61 79 20 66 61 69 6c 20 77 ation may fail w
44530 69 74 68 0a 2a 2a 20 61 6e 20 5b 53 51 4c 49 54 ith.** an [SQLIT
44540 45 5f 4c 4f 43 4b 45 44 5d 20 65 72 72 6f 72 20 E_LOCKED] error
44550 69 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20 if the required
44560 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 73 68 61 locks on the sha
44570 72 65 64 2d 63 61 63 68 65 20 6f 72 0a 2a 2a 20 red-cache or.**
44580 69 6e 64 69 76 69 64 75 61 6c 20 74 61 62 6c 65 individual table
44590 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 s within the sha
445a0 72 65 64 2d 63 61 63 68 65 20 63 61 6e 6e 6f 74 red-cache cannot
445b0 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 20 53 65 be obtained. Se
445c0 65 0a 2a 2a 20 5b 53 51 4c 69 74 65 20 53 68 61 e.** [SQLite Sha
445d0 72 65 64 2d 43 61 63 68 65 20 4d 6f 64 65 5d 20 red-Cache Mode]
445e0 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 69 6f for a descriptio
445f0 6e 20 6f 66 20 73 68 61 72 65 64 2d 63 61 63 68 n of shared-cach
44600 65 20 6c 6f 63 6b 69 6e 67 2e 20 0a 2a 2a 20 54 e locking. .** T
44610 68 69 73 20 41 50 49 20 6d 61 79 20 62 65 20 75 his API may be u
44620 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 20 sed to register
44630 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 a callback that
44640 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76 6f SQLite will invo
44650 6b 65 20 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 ke .** when the
44660 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 65 connection curre
44670 6e 74 6c 79 20 68 6f 6c 64 69 6e 67 20 74 68 65 ntly holding the
44680 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 20 72 required lock r
44690 65 6c 69 6e 71 75 69 73 68 65 73 20 69 74 2e 0a elinquishes it..
446a0 2a 2a 20 54 68 69 73 20 41 50 49 20 69 73 20 6f ** This API is o
446b0 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 nly available if
446c0 20 74 68 65 20 6c 69 62 72 61 72 79 20 77 61 73 the library was
446d0 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 compiled with t
446e0 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e he.** [SQLITE_EN
446f0 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 ABLE_UNLOCK_NOTI
44700 46 59 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 FY] C-preprocess
44710 6f 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 or symbol define
44720 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 d..**.** See Als
44730 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53 51 o: [Using the SQ
44740 4c 69 74 65 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69 Lite Unlock Noti
44750 66 69 63 61 74 69 6f 6e 20 46 65 61 74 75 72 65 fication Feature
44760 5d 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 64 2d ]..**.** Shared-
44770 63 61 63 68 65 20 6c 6f 63 6b 73 20 61 72 65 20 cache locks are
44780 72 65 6c 65 61 73 65 64 20 77 68 65 6e 20 61 20 released when a
44790 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
447a0 69 6f 6e 20 63 6f 6e 63 6c 75 64 65 73 0a 2a 2a ion concludes.**
447b0 20 69 74 73 20 63 75 72 72 65 6e 74 20 74 72 61 its current tra
447c0 6e 73 61 63 74 69 6f 6e 2c 20 65 69 74 68 65 72 nsaction, either
447d0 20 62 79 20 63 6f 6d 6d 69 74 74 69 6e 67 20 69 by committing i
447e0 74 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 69 74 20 t or rolling it
447f0 62 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 back. .**.** Whe
44800 6e 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 n a connection (
44810 6b 6e 6f 77 6e 20 61 73 20 74 68 65 20 62 6c 6f known as the blo
44820 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 cked connection)
44830 20 66 61 69 6c 73 20 74 6f 20 6f 62 74 61 69 6e fails to obtain
44840 20 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 a.** shared-cac
44850 68 65 20 6c 6f 63 6b 20 61 6e 64 20 53 51 4c 49 he lock and SQLI
44860 54 45 5f 4c 4f 43 4b 45 44 20 69 73 20 72 65 74 TE_LOCKED is ret
44870 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c urned to the cal
44880 6c 65 72 2c 20 74 68 65 0a 2a 2a 20 69 64 65 6e ler, the.** iden
44890 74 69 74 79 20 6f 66 20 74 68 65 20 64 61 74 61 tity of the data
448a0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
448b0 28 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f (the blocking co
448c0 6e 6e 65 63 74 69 6f 6e 29 20 74 68 61 74 0a 2a nnection) that.*
448d0 2a 20 68 61 73 20 6c 6f 63 6b 65 64 20 74 68 65 * has locked the
448e0 20 72 65 71 75 69 72 65 64 20 72 65 73 6f 75 72 required resour
448f0 63 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 74 ce is stored int
44900 65 72 6e 61 6c 6c 79 2e 20 41 66 74 65 72 20 61 ernally. After a
44910 6e 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f n .** applicatio
44920 6e 20 72 65 63 65 69 76 65 73 20 61 6e 20 53 51 n receives an SQ
44930 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 65 72 72 6f LITE_LOCKED erro
44940 72 2c 20 69 74 20 6d 61 79 20 63 61 6c 6c 20 74 r, it may call t
44950 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 75 6e he.** sqlite3_un
44960 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 6d 65 lock_notify() me
44970 74 68 6f 64 20 77 69 74 68 20 74 68 65 20 62 6c thod with the bl
44980 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e ocked connection
44990 20 68 61 6e 64 6c 65 20 61 73 20 0a 2a 2a 20 74 handle as .** t
449a0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
449b0 74 20 74 6f 20 72 65 67 69 73 74 65 72 20 66 6f t to register fo
449c0 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 r a callback tha
449d0 74 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 t will be invoke
449e0 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 62 6c d.** when the bl
449f0 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f ocking connectio
44a00 6e 73 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 ns current trans
44a10 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 action is conclu
44a20 64 65 64 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c ded. The.** call
44a30 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 back is invoked
44a40 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 from within the
44a50 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 5d 20 6f [sqlite3_step] o
44a60 72 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 r [sqlite3_close
44a70 5d 0a 2a 2a 20 63 61 6c 6c 20 74 68 61 74 20 63 ].** call that c
44a80 6f 6e 63 6c 75 64 65 73 20 74 68 65 20 62 6c 6f oncludes the blo
44a90 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e cking connection
44aa0 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a s transaction..*
44ab0 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f *.** If sqlite3_
44ac0 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 unlock_notify()
44ad0 69 73 20 63 61 6c 6c 65 64 20 69 6e 20 61 20 6d is called in a m
44ae0 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 ulti-threaded ap
44af0 70 6c 69 63 61 74 69 6f 6e 2c 0a 2a 2a 20 74 68 plication,.** th
44b00 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 20 ere is a chance
44b10 74 68 61 74 20 74 68 65 20 62 6c 6f 63 6b 69 6e that the blockin
44b20 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 6c g connection wil
44b30 6c 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 2a l have already.*
44b40 2a 20 63 6f 6e 63 6c 75 64 65 64 20 69 74 73 20 * concluded its
44b50 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 79 20 74 transaction by t
44b60 68 65 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f he time sqlite3_
44b70 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 unlock_notify()
44b80 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 49 is invoked..** I
44b90 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 2c 20 f this happens,
44ba0 74 68 65 6e 20 74 68 65 20 73 70 65 63 69 66 69 then the specifi
44bb0 65 64 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 ed callback is i
44bc0 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 65 nvoked immediate
44bd0 6c 79 2c 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 ly,.** from with
44be0 69 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 in the call to s
44bf0 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f qlite3_unlock_no
44c00 74 69 66 79 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 tify()..**.** If
44c10 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e the blocked con
44c20 6e 65 63 74 69 6f 6e 20 69 73 20 61 74 74 65 6d nection is attem
44c30 70 74 69 6e 67 20 74 6f 20 6f 62 74 61 69 6e 20 pting to obtain
44c40 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 a write-lock on
44c50 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 a.** shared-cach
44c60 65 20 74 61 62 6c 65 2c 20 61 6e 64 20 6d 6f 72 e table, and mor
44c70 65 20 74 68 61 6e 20 6f 6e 65 20 6f 74 68 65 72 e than one other
44c80 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 connection curr
44c90 65 6e 74 6c 79 20 68 6f 6c 64 73 0a 2a 2a 20 61 ently holds.** a
44ca0 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 read-lock on th
44cb0 65 20 73 61 6d 65 20 74 61 62 6c 65 2c 20 74 68 e same table, th
44cc0 65 6e 20 53 51 4c 69 74 65 20 61 72 62 69 74 72 en SQLite arbitr
44cd0 61 72 69 6c 79 20 73 65 6c 65 63 74 73 20 6f 6e arily selects on
44ce0 65 20 6f 66 20 0a 2a 2a 20 74 68 65 20 6f 74 68 e of .** the oth
44cf0 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 er connections t
44d00 6f 20 75 73 65 20 61 73 20 74 68 65 20 62 6c 6f o use as the blo
44d10 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e cking connection
44d20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 61 ..**.** There ma
44d30 79 20 62 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65 y be at most one
44d40 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 unlock-notify c
44d50 61 6c 6c 62 61 63 6b 20 72 65 67 69 73 74 65 72 allback register
44d60 65 64 20 62 79 20 61 20 0a 2a 2a 20 62 6c 6f 63 ed by a .** bloc
44d70 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 ked connection.
44d80 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 If sqlite3_unloc
44d90 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 63 61 k_notify() is ca
44da0 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a lled when the.**
44db0 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 blocked connect
44dc0 69 6f 6e 20 61 6c 72 65 61 64 79 20 68 61 73 20 ion already has
44dd0 61 20 72 65 67 69 73 74 65 72 65 64 20 75 6e 6c a registered unl
44de0 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 ock-notify callb
44df0 61 63 6b 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 ack,.** then the
44e00 20 6e 65 77 20 63 61 6c 6c 62 61 63 6b 20 72 65 new callback re
44e10 70 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 2e 20 places the old.
44e20 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 If sqlite3_unloc
44e30 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 0a 2a 2a k_notify() is.**
44e40 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e called with a N
44e50 55 4c 4c 20 70 6f 69 6e 74 65 72 20 61 73 20 69 ULL pointer as i
44e60 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 ts second argume
44e70 6e 74 2c 20 74 68 65 6e 20 61 6e 79 20 65 78 69 nt, then any exi
44e80 73 74 69 6e 67 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d sting.** unlock-
44e90 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 notify callback
44ea0 69 73 20 63 61 6e 63 65 6c 6c 65 64 2e 20 54 68 is cancelled. Th
44eb0 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 e blocked connec
44ec0 74 69 6f 6e 73 20 0a 2a 2a 20 75 6e 6c 6f 63 6b tions .** unlock
44ed0 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b -notify callback
44ee0 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 63 61 6e may also be can
44ef0 63 65 6c 65 64 20 62 79 20 63 6c 6f 73 69 6e 67 celed by closing
44f00 20 74 68 65 20 62 6c 6f 63 6b 65 64 0a 2a 2a 20 the blocked.**
44f10 63 6f 6e 6e 65 63 74 69 6f 6e 20 75 73 69 6e 67 connection using
44f20 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 [sqlite3_close(
44f30 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 75 6e )]..**.** The un
44f40 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c lock-notify call
44f50 62 61 63 6b 20 69 73 20 6e 6f 74 20 72 65 65 6e back is not reen
44f60 74 72 61 6e 74 2e 20 49 66 20 61 6e 20 61 70 70 trant. If an app
44f70 6c 69 63 61 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 lication invokes
44f80 0a 2a 2a 20 61 6e 79 20 73 71 6c 69 74 65 33 5f .** any sqlite3_
44f90 78 78 78 20 41 50 49 20 66 75 6e 63 74 69 6f 6e xxx API function
44fa0 73 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 6e s from within an
44fb0 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 unlock-notify c
44fc0 61 6c 6c 62 61 63 6b 2c 20 61 0a 2a 2a 20 63 72 allback, a.** cr
44fd0 61 73 68 20 6f 72 20 64 65 61 64 6c 6f 63 6b 20 ash or deadlock
44fe0 6d 61 79 20 62 65 20 74 68 65 20 72 65 73 75 6c may be the resul
44ff0 74 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 t..**.** Unless
45000 64 65 61 64 6c 6f 63 6b 20 69 73 20 64 65 74 65 deadlock is dete
45010 63 74 65 64 20 28 73 65 65 20 62 65 6c 6f 77 29 cted (see below)
45020 2c 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b , sqlite3_unlock
45030 5f 6e 6f 74 69 66 79 28 29 20 61 6c 77 61 79 73 _notify() always
45040 0a 2a 2a 20 72 65 74 75 72 6e 73 20 53 51 4c 49 .** returns SQLI
45050 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e TE_OK..**.** <b>
45060 43 61 6c 6c 62 61 63 6b 20 49 6e 76 6f 63 61 74 Callback Invocat
45070 69 6f 6e 20 44 65 74 61 69 6c 73 3c 2f 62 3e 0a ion Details</b>.
45080 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 75 6e **.** When an un
45090 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c lock-notify call
450a0 62 61 63 6b 20 69 73 20 72 65 67 69 73 74 65 72 back is register
450b0 65 64 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74 ed, the applicat
450c0 69 6f 6e 20 70 72 6f 76 69 64 65 73 20 61 20 0a ion provides a .
450d0 2a 2a 20 73 69 6e 67 6c 65 20 76 6f 69 64 2a 20 ** single void*
450e0 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20 pointer that is
450f0 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 63 61 passed to the ca
45100 6c 6c 62 61 63 6b 20 77 68 65 6e 20 69 74 20 69 llback when it i
45110 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 48 6f s invoked..** Ho
45120 77 65 76 65 72 2c 20 74 68 65 20 73 69 67 6e 61 wever, the signa
45130 74 75 72 65 20 6f 66 20 74 68 65 20 63 61 6c 6c ture of the call
45140 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 61 6c back function al
45150 6c 6f 77 73 20 53 51 4c 69 74 65 20 74 6f 20 70 lows SQLite to p
45160 61 73 73 0a 2a 2a 20 69 74 20 61 6e 20 61 72 72 ass.** it an arr
45170 61 79 20 6f 66 20 76 6f 69 64 2a 20 63 6f 6e 74 ay of void* cont
45180 65 78 74 20 70 6f 69 6e 74 65 72 73 2e 20 54 68 ext pointers. Th
45190 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
451a0 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 61 6e passed to.** an
451b0 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 unlock-notify c
451c0 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 70 6f 69 allback is a poi
451d0 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 nter to an array
451e0 20 6f 66 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65 of void* pointe
451f0 72 73 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 rs,.** and the s
45200 65 63 6f 6e 64 20 69 73 20 74 68 65 20 6e 75 6d econd is the num
45210 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
45220 6e 20 74 68 65 20 61 72 72 61 79 2e 0a 2a 2a 0a n the array..**.
45230 2a 2a 20 57 68 65 6e 20 61 20 62 6c 6f 63 6b 69 ** When a blocki
45240 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 ng connections t
45250 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f ransaction is co
45260 6e 63 6c 75 64 65 64 2c 20 74 68 65 72 65 20 6d ncluded, there m
45270 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 74 68 ay be.** more th
45280 61 6e 20 6f 6e 65 20 62 6c 6f 63 6b 65 64 20 63 an one blocked c
45290 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 68 onnection that h
452a0 61 73 20 72 65 67 69 73 74 65 72 65 64 20 66 6f as registered fo
452b0 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 r an unlock-noti
452c0 66 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 fy.** callback.
452d0 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73 If two or more s
452e0 75 63 68 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e uch blocked conn
452f0 65 63 74 69 6f 6e 73 20 68 61 76 65 20 73 70 65 ections have spe
45300 63 69 66 69 65 64 20 74 68 65 0a 2a 2a 20 73 61 cified the.** sa
45310 6d 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 me callback func
45320 74 69 6f 6e 2c 20 74 68 65 6e 20 69 6e 73 74 65 tion, then inste
45330 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 74 ad of invoking t
45340 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 he callback func
45350 74 69 6f 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 tion.** multiple
45360 20 74 69 6d 65 73 2c 20 69 74 20 69 73 20 69 6e times, it is in
45370 76 6f 6b 65 64 20 6f 6e 63 65 20 77 69 74 68 20 voked once with
45380 74 68 65 20 73 65 74 20 6f 66 20 76 6f 69 64 2a the set of void*
45390 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 context pointer
453a0 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 s.** specified b
453b0 79 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f y the blocked co
453c0 6e 6e 65 63 74 69 6f 6e 73 20 62 75 6e 64 6c 65 nnections bundle
453d0 64 20 74 6f 67 65 74 68 65 72 20 69 6e 74 6f 20 d together into
453e0 61 6e 20 61 72 72 61 79 2e 0a 2a 2a 20 54 68 69 an array..** Thi
453f0 73 20 67 69 76 65 73 20 74 68 65 20 61 70 70 6c s gives the appl
45400 69 63 61 74 69 6f 6e 20 61 6e 20 6f 70 70 6f 72 ication an oppor
45410 74 75 6e 69 74 79 20 74 6f 20 70 72 69 6f 72 69 tunity to priori
45420 74 69 7a 65 20 61 6e 79 20 61 63 74 69 6f 6e 73 tize any actions
45430 20 0a 2a 2a 20 72 65 6c 61 74 65 64 20 74 6f 20 .** related to
45440 74 68 65 20 73 65 74 20 6f 66 20 75 6e 62 6c 6f the set of unblo
45450 63 6b 65 64 20 64 61 74 61 62 61 73 65 20 63 6f cked database co
45460 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a nnections..**.**
45470 20 3c 62 3e 44 65 61 64 6c 6f 63 6b 20 44 65 74 <b>Deadlock Det
45480 65 63 74 69 6f 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a ection</b>.**.**
45490 20 41 73 73 75 6d 69 6e 67 20 74 68 61 74 20 61 Assuming that a
454a0 66 74 65 72 20 72 65 67 69 73 74 65 72 69 6e 67 fter registering
454b0 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e for an unlock-n
454c0 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 61 otify callback a
454d0 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77 61 .** database wa
454e0 69 74 73 20 66 6f 72 20 74 68 65 20 63 61 6c 6c its for the call
454f0 62 61 63 6b 20 74 6f 20 62 65 20 69 73 73 75 65 back to be issue
45500 64 20 62 65 66 6f 72 65 20 74 61 6b 69 6e 67 20 d before taking
45510 61 6e 79 20 66 75 72 74 68 65 72 0a 2a 2a 20 61 any further.** a
45520 63 74 69 6f 6e 20 28 61 20 72 65 61 73 6f 6e 61 ction (a reasona
45530 62 6c 65 20 61 73 73 75 6d 70 74 69 6f 6e 29 2c ble assumption),
45540 20 74 68 65 6e 20 75 73 69 6e 67 20 74 68 69 73 then using this
45550 20 41 50 49 20 6d 61 79 20 63 61 75 73 65 20 74 API may cause t
45560 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f he.** applicatio
45570 6e 20 74 6f 20 64 65 61 64 6c 6f 63 6b 2e 20 46 n to deadlock. F
45580 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 63 or example, if c
45590 6f 6e 6e 65 63 74 69 6f 6e 20 58 20 69 73 20 77 onnection X is w
455a0 61 69 74 69 6e 67 20 66 6f 72 0a 2a 2a 20 63 6f aiting for.** co
455b0 6e 6e 65 63 74 69 6f 6e 20 59 27 73 20 74 72 61 nnection Y's tra
455c0 6e 73 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 nsaction to be c
455d0 6f 6e 63 6c 75 64 65 64 2c 20 61 6e 64 20 73 69 oncluded, and si
455e0 6d 69 6c 61 72 6c 79 20 63 6f 6e 6e 65 63 74 69 milarly connecti
455f0 6f 6e 0a 2a 2a 20 59 20 69 73 20 77 61 69 74 69 on.** Y is waiti
45600 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e ng on connection
45610 20 58 27 73 20 74 72 61 6e 73 61 63 74 69 6f 6e X's transaction
45620 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20 63 , then neither c
45630 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c onnection.** wil
45640 6c 20 70 72 6f 63 65 65 64 20 61 6e 64 20 74 68 l proceed and th
45650 65 20 73 79 73 74 65 6d 20 6d 61 79 20 72 65 6d e system may rem
45660 61 69 6e 20 64 65 61 64 6c 6f 63 6b 65 64 20 69 ain deadlocked i
45670 6e 64 65 66 69 6e 69 74 65 6c 79 2e 0a 2a 2a 0a ndefinitely..**.
45680 2a 2a 20 54 6f 20 61 76 6f 69 64 20 74 68 69 73 ** To avoid this
45690 20 73 63 65 6e 61 72 69 6f 2c 20 74 68 65 20 73 scenario, the s
456a0 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f qlite3_unlock_no
456b0 74 69 66 79 28 29 20 70 65 72 66 6f 72 6d 73 20 tify() performs
456c0 64 65 61 64 6c 6f 63 6b 0a 2a 2a 20 64 65 74 65 deadlock.** dete
456d0 63 74 69 6f 6e 2e 20 49 66 20 61 20 67 69 76 65 ction. If a give
456e0 6e 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 n call to sqlite
456f0 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 3_unlock_notify(
45700 29 20 77 6f 75 6c 64 20 70 75 74 20 74 68 65 0a ) would put the.
45710 2a 2a 20 73 79 73 74 65 6d 20 69 6e 20 61 20 64 ** system in a d
45720 65 61 64 6c 6f 63 6b 65 64 20 73 74 61 74 65 2c eadlocked state,
45730 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 then SQLITE_LOC
45740 4b 45 44 20 69 73 20 72 65 74 75 72 6e 65 64 20 KED is returned
45750 61 6e 64 20 6e 6f 0a 2a 2a 20 75 6e 6c 6f 63 6b and no.** unlock
45760 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b -notify callback
45770 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2e 20 is registered.
45780 54 68 65 20 73 79 73 74 65 6d 20 69 73 20 73 61 The system is sa
45790 69 64 20 74 6f 20 62 65 20 69 6e 0a 2a 2a 20 61 id to be in.** a
457a0 20 64 65 61 64 6c 6f 63 6b 65 64 20 73 74 61 74 deadlocked stat
457b0 65 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 e if connection
457c0 41 20 68 61 73 20 72 65 67 69 73 74 65 72 65 64 A has registered
457d0 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e for an unlock-n
457e0 6f 74 69 66 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 otify.** callbac
457f0 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c 75 73 k on the conclus
45800 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f ion of connectio
45810 6e 20 42 27 73 20 74 72 61 6e 73 61 63 74 69 6f n B's transactio
45820 6e 2c 20 61 6e 64 20 63 6f 6e 6e 65 63 74 69 6f n, and connectio
45830 6e 0a 2a 2a 20 42 20 68 61 73 20 69 74 73 65 6c n.** B has itsel
45840 66 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 f registered for
45850 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 an unlock-notif
45860 79 20 63 61 6c 6c 62 61 63 6b 20 77 68 65 6e 20 y callback when
45870 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 41 27 connection.** A'
45880 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 s transaction is
45890 20 63 6f 6e 63 6c 75 64 65 64 2e 20 49 6e 64 69 concluded. Indi
458a0 72 65 63 74 20 64 65 61 64 6c 6f 63 6b 20 69 73 rect deadlock is
458b0 20 61 6c 73 6f 20 64 65 74 65 63 74 65 64 2c 20 also detected,
458c0 73 6f 0a 2a 2a 20 74 68 65 20 73 79 73 74 65 6d so.** the system
458d0 20 69 73 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 is also conside
458e0 72 65 64 20 74 6f 20 62 65 20 64 65 61 64 6c 6f red to be deadlo
458f0 63 6b 65 64 20 69 66 20 63 6f 6e 6e 65 63 74 69 cked if connecti
45900 6f 6e 20 42 20 68 61 73 0a 2a 2a 20 72 65 67 69 on B has.** regi
45910 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75 6e stered for an un
45920 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c lock-notify call
45930 62 61 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 back on the conc
45940 6c 75 73 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 lusion of connec
45950 74 69 6f 6e 0a 2a 2a 20 43 27 73 20 74 72 61 6e tion.** C's tran
45960 73 61 63 74 69 6f 6e 2c 20 77 68 65 72 65 20 63 saction, where c
45970 6f 6e 6e 65 63 74 69 6f 6e 20 43 20 69 73 20 77 onnection C is w
45980 61 69 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 aiting on connec
45990 74 69 6f 6e 20 41 2e 20 41 6e 79 0a 2a 2a 20 6e tion A. Any.** n
459a0 75 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20 umber of levels
459b0 6f 66 20 69 6e 64 69 72 65 63 74 69 6f 6e 20 61 of indirection a
459c0 72 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2a 0a 2a re allowed..**.*
459d0 2a 20 3c 62 3e 54 68 65 20 22 44 52 4f 50 20 54 * <b>The "DROP T
459e0 41 42 4c 45 22 20 45 78 63 65 70 74 69 6f 6e 3c ABLE" Exception<
459f0 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 /b>.**.** When a
45a00 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
45a10 33 5f 73 74 65 70 28 29 5d 20 72 65 74 75 72 6e 3_step()] return
45a20 73 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2c s SQLITE_LOCKED,
45a30 20 69 74 20 69 73 20 61 6c 6d 6f 73 74 20 0a 2a it is almost .*
45a40 2a 20 61 6c 77 61 79 73 20 61 70 70 72 6f 70 72 * always appropr
45a50 69 61 74 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c iate to call sql
45a60 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 ite3_unlock_noti
45a70 66 79 28 29 2e 20 54 68 65 72 65 20 69 73 20 68 fy(). There is h
45a80 6f 77 65 76 65 72 2c 0a 2a 2a 20 6f 6e 65 20 65 owever,.** one e
45a90 78 63 65 70 74 69 6f 6e 2e 20 57 68 65 6e 20 65 xception. When e
45aa0 78 65 63 75 74 69 6e 67 20 61 20 22 44 52 4f 50 xecuting a "DROP
45ab0 20 54 41 42 4c 45 22 20 6f 72 20 22 44 52 4f 50 TABLE" or "DROP
45ac0 20 49 4e 44 45 58 22 20 73 74 61 74 65 6d 65 6e INDEX" statemen
45ad0 74 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 63 68 65 t,.** SQLite che
45ae0 63 6b 73 20 69 66 20 74 68 65 72 65 20 61 72 65 cks if there are
45af0 20 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 65 any currently e
45b00 78 65 63 75 74 69 6e 67 20 53 45 4c 45 43 54 20 xecuting SELECT
45b10 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 statements.** th
45b20 61 74 20 62 65 6c 6f 6e 67 20 74 6f 20 74 68 65 at belong to the
45b30 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e same connection
45b40 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 2c 20 . If there are,
45b50 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 73 SQLITE_LOCKED is
45b60 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 49 6e .** returned. In
45b70 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 this case there
45b80 20 69 73 20 6e 6f 20 22 62 6c 6f 63 6b 69 6e 67 is no "blocking
45b90 20 63 6f 6e 6e 65 63 74 69 6f 6e 22 2c 20 73 6f connection", so
45ba0 20 69 6e 76 6f 6b 69 6e 67 0a 2a 2a 20 73 71 6c invoking.** sql
45bb0 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 ite3_unlock_noti
45bc0 66 79 28 29 20 72 65 73 75 6c 74 73 20 69 6e 20 fy() results in
45bd0 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 the unlock-notif
45be0 79 20 63 61 6c 6c 62 61 63 6b 20 62 65 69 6e 67 y callback being
45bf0 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 .** invoked imme
45c00 64 69 61 74 65 6c 79 2e 20 49 66 20 74 68 65 20 diately. If the
45c10 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 68 65 6e application then
45c20 20 72 65 2d 61 74 74 65 6d 70 74 73 20 74 68 65 re-attempts the
45c30 20 22 44 52 4f 50 20 54 41 42 4c 45 22 0a 2a 2a "DROP TABLE".**
45c40 20 6f 72 20 22 44 52 4f 50 20 49 4e 44 45 58 22 or "DROP INDEX"
45c50 20 71 75 65 72 79 2c 20 61 6e 20 69 6e 66 69 6e query, an infin
45c60 69 74 65 20 6c 6f 6f 70 20 6d 69 67 68 74 20 62 ite loop might b
45c70 65 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a e the result..**
45c80 0a 2a 2a 20 4f 6e 65 20 77 61 79 20 61 72 6f 75 .** One way arou
45c90 6e 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 nd this problem
45ca0 69 73 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 is to check the
45cb0 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 extended error c
45cc0 6f 64 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 ode returned.**
45cd0 62 79 20 61 6e 20 73 71 6c 69 74 65 33 5f 73 74 by an sqlite3_st
45ce0 65 70 28 29 20 63 61 6c 6c 2e 20 49 66 20 74 68 ep() call. If th
45cf0 65 72 65 20 69 73 20 61 20 62 6c 6f 63 6b 69 6e ere is a blockin
45d00 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 g connection, th
45d10 65 6e 20 74 68 65 0a 2a 2a 20 65 78 74 65 6e 64 en the.** extend
45d20 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 ed error code is
45d30 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4c set to SQLITE_L
45d40 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 OCKED_SHAREDCACH
45d50 45 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 6e E. Otherwise, in
45d60 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20 .** the special
45d70 22 44 52 4f 50 20 54 41 42 4c 45 2f 49 4e 44 45 "DROP TABLE/INDE
45d80 58 22 20 63 61 73 65 2c 20 74 68 65 20 65 78 74 X" case, the ext
45d90 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 ended error code
45da0 20 69 73 20 6a 75 73 74 20 0a 2a 2a 20 53 51 4c is just .** SQL
45db0 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 53 ITE_LOCKED..*/.S
45dc0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
45dd0 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 lite3_unlock_not
45de0 69 66 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a ify(. sqlite3 *
45df0 70 42 6c 6f 63 6b 65 64 2c 20 20 20 20 20 20 20 pBlocked,
45e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45e10 20 20 20 2f 2a 20 57 61 69 74 69 6e 67 20 63 6f /* Waiting co
45e20 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f nnection */. vo
45e30 69 64 20 28 2a 78 4e 6f 74 69 66 79 29 28 76 6f id (*xNotify)(vo
45e40 69 64 20 2a 2a 61 70 41 72 67 2c 20 69 6e 74 20 id **apArg, int
45e50 6e 41 72 67 29 2c 20 20 20 20 2f 2a 20 43 61 6c nArg), /* Cal
45e60 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 74 lback function t
45e70 6f 20 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20 76 6f o invoke */. vo
45e80 69 64 20 2a 70 4e 6f 74 69 66 79 41 72 67 20 20 id *pNotifyArg
45e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45ea0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 /* Arg
45eb0 75 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f ument to pass to
45ec0 20 78 4e 6f 74 69 66 79 20 2a 2f 0a 29 3b 0a 0a xNotify */.);..
45ed0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
45ee0 20 53 74 72 69 6e 67 20 43 6f 6d 70 61 72 69 73 String Comparis
45ef0 6f 6e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 on.** EXPERIMENT
45f00 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 AL.**.** The [sq
45f10 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28 29 lite3_strnicmp()
45f20 5d 20 41 50 49 20 61 6c 6c 6f 77 73 20 61 70 70 ] API allows app
45f30 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 65 78 lications and ex
45f40 74 65 6e 73 69 6f 6e 73 20 74 6f 0a 2a 2a 20 63 tensions to.** c
45f50 6f 6d 70 61 72 65 20 74 68 65 20 63 6f 6e 74 65 ompare the conte
45f60 6e 74 73 20 6f 66 20 74 77 6f 20 62 75 66 66 65 nts of two buffe
45f70 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 55 54 rs containing UT
45f80 46 2d 38 20 73 74 72 69 6e 67 73 20 69 6e 20 61 F-8 strings in a
45f90 0a 2a 2a 20 63 61 73 65 2d 69 6e 64 65 6e 64 65 .** case-indende
45fa0 6e 74 20 66 61 73 68 69 6f 6e 2c 20 75 73 69 6e nt fashion, usin
45fb0 67 20 74 68 65 20 73 61 6d 65 20 64 65 66 69 6e g the same defin
45fc0 69 74 69 6f 6e 20 6f 66 20 63 61 73 65 20 69 6e ition of case in
45fd0 64 65 70 65 6e 64 65 6e 63 65 20 0a 2a 2a 20 74 dependence .** t
45fe0 68 61 74 20 53 51 4c 69 74 65 20 75 73 65 73 20 hat SQLite uses
45ff0 69 6e 74 65 72 6e 61 6c 6c 79 20 77 68 65 6e 20 internally when
46000 63 6f 6d 70 61 72 69 6e 67 20 69 64 65 6e 74 69 comparing identi
46010 66 69 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 fiers..*/.SQLITE
46020 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
46030 5f 73 74 72 6e 69 63 6d 70 28 63 6f 6e 73 74 20 _strnicmp(const
46040 63 68 61 72 20 2a 2c 20 63 6f 6e 73 74 20 63 68 char *, const ch
46050 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a ar *, int);../*.
46060 2a 2a 20 55 6e 64 6f 20 74 68 65 20 68 61 63 6b ** Undo the hack
46070 20 74 68 61 74 20 63 6f 6e 76 65 72 74 73 20 66 that converts f
46080 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 74 79 loating point ty
46090 70 65 73 20 74 6f 20 69 6e 74 65 67 65 72 20 66 pes to integer f
460a0 6f 72 0a 2a 2a 20 62 75 69 6c 64 73 20 6f 6e 20 or.** builds on
460b0 70 72 6f 63 65 73 73 6f 72 73 20 77 69 74 68 6f processors witho
460c0 75 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e ut floating poin
460d0 74 20 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a 23 69 t support..*/.#i
460e0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
460f0 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a _FLOATING_POINT.
46100 23 20 75 6e 64 65 66 20 64 6f 75 62 6c 65 0a 23 # undef double.#
46110 65 6e 64 69 66 0a 0a 23 69 66 20 30 0a 7d 20 20 endif..#if 0.}
46120 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 27 65 /* End of the 'e
46130 78 74 65 72 6e 20 22 43 22 27 20 62 6c 6f 63 6b xtern "C"' block
46140 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 */.#endif.#endi
46150 66 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f.../***********
46160 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 *** End of sqlit
46170 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e3.h ***********
46180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
461a0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
461b0 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
461c0 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
461d0 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 in sqliteInt.h
461e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
461f0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
46200 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 61 73 68 *** Include hash
46210 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
46220 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 of sqliteInt.h
46230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46240 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
46250 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 *** Begin file h
46260 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ash.h **********
46270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46290 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 **/./*.** 2001 S
462a0 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a eptember 22.**.*
462b0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
462c0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
462d0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
462e0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
462f0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
46300 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
46310 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
46320 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
46330 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
46340 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
46350 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
46360 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
46370 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
46380 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
46390 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
463a0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
463b0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
463c0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
463d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
463e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
463f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
46410 69 73 20 69 73 20 74 68 65 20 68 65 61 64 65 72 is is the header
46420 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 67 65 file for the ge
46430 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c 65 neric hash-table
46440 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a implemenation.*
46450 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 * used in SQLite
46460 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 61 73 ..**.** $Id: has
46470 68 2e 68 2c 76 20 31 2e 31 35 20 32 30 30 39 2f h.h,v 1.15 2009/
46480 30 35 2f 30 32 20 31 33 3a 32 39 3a 33 38 20 64 05/02 13:29:38 d
46490 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e rh Exp $.*/.#ifn
464a0 64 65 66 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 def _SQLITE_HASH
464b0 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c _H_.#define _SQL
464c0 49 54 45 5f 48 41 53 48 5f 48 5f 0a 0a 2f 2a 20 ITE_HASH_H_../*
464d0 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 Forward declarat
464e0 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72 ions of structur
464f0 65 73 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 es. */.typedef s
46500 74 72 75 63 74 20 48 61 73 68 20 48 61 73 68 3b truct Hash Hash;
46510 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
46520 48 61 73 68 45 6c 65 6d 20 48 61 73 68 45 6c 65 HashElem HashEle
46530 6d 3b 0a 0a 2f 2a 20 41 20 63 6f 6d 70 6c 65 74 m;../* A complet
46540 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 e hash table is
46550 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
46560 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
46570 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 69 ucture..** The i
46580 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 69 73 nternals of this
46590 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 69 structure are i
465a0 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 ntended to be op
465b0 61 71 75 65 20 2d 2d 20 63 6c 69 65 6e 74 0a 2a aque -- client.*
465c0 2a 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 6f * code should no
465d0 74 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63 63 t attempt to acc
465e0 65 73 73 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 ess or modify th
465f0 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73 e fields of this
46600 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 69 structure.** di
46610 72 65 63 74 6c 79 2e 20 20 43 68 61 6e 67 65 20 rectly. Change
46620 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6f this structure o
46630 6e 6c 79 20 62 79 20 75 73 69 6e 67 20 74 68 65 nly by using the
46640 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e routines below.
46650 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 73 6f 6d .** However, som
46660 65 20 6f 66 20 74 68 65 20 22 70 72 6f 63 65 64 e of the "proced
46670 75 72 65 73 22 20 61 6e 64 20 22 66 75 6e 63 74 ures" and "funct
46680 69 6f 6e 73 22 20 66 6f 72 20 6d 6f 64 69 66 79 ions" for modify
46690 69 6e 67 20 61 6e 64 0a 2a 2a 20 61 63 63 65 73 ing and.** acces
466a0 73 69 6e 67 20 74 68 69 73 20 73 74 72 75 63 74 sing this struct
466b0 75 72 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6d ure are really m
466c0 61 63 72 6f 73 2c 20 73 6f 20 77 65 20 63 61 6e acros, so we can
466d0 27 74 20 72 65 61 6c 6c 79 20 6d 61 6b 65 0a 2a 't really make.*
466e0 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 * this structure
466f0 20 6f 70 61 71 75 65 2e 0a 2a 2a 0a 2a 2a 20 41 opaque..**.** A
46700 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 ll elements of t
46710 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 61 72 he hash table ar
46720 65 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f e on a single do
46730 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 ubly-linked list
46740 2e 0a 2a 2a 20 48 61 73 68 2e 66 69 72 73 74 20 ..** Hash.first
46750 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 68 65 points to the he
46760 61 64 20 6f 66 20 74 68 69 73 20 6c 69 73 74 2e ad of this list.
46770 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 .**.** There are
46780 20 48 61 73 68 2e 68 74 73 69 7a 65 20 62 75 63 Hash.htsize buc
46790 6b 65 74 73 2e 20 20 45 61 63 68 20 62 75 63 6b kets. Each buck
467a0 65 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 et points to a s
467b0 70 6f 74 20 69 6e 0a 2a 2a 20 74 68 65 20 67 6c pot in.** the gl
467c0 6f 62 61 6c 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b obal doubly-link
467d0 65 64 20 6c 69 73 74 2e 20 20 54 68 65 20 63 6f ed list. The co
467e0 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 75 ntents of the bu
467f0 63 6b 65 74 20 61 72 65 20 74 68 65 0a 2a 2a 20 cket are the.**
46800 65 6c 65 6d 65 6e 74 20 70 6f 69 6e 74 65 64 20 element pointed
46810 74 6f 20 70 6c 75 73 20 74 68 65 20 6e 65 78 74 to plus the next
46820 20 5f 68 74 2e 63 6f 75 6e 74 2d 31 20 65 6c 65 _ht.count-1 ele
46830 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 6c 69 73 ments in the lis
46840 74 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 2e 68 74 t..**.** Hash.ht
46850 73 69 7a 65 20 61 6e 64 20 48 61 73 68 2e 68 74 size and Hash.ht
46860 20 6d 61 79 20 62 65 20 7a 65 72 6f 2e 20 20 49 may be zero. I
46870 6e 20 74 68 61 74 20 63 61 73 65 20 6c 6f 6f 6b n that case look
46880 75 70 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 62 79 up is done.** by
46890 20 61 20 6c 69 6e 65 61 72 20 73 65 61 72 63 68 a linear search
468a0 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 6c of the global l
468b0 69 73 74 2e 20 20 46 6f 72 20 73 6d 61 6c 6c 20 ist. For small
468c0 74 61 62 6c 65 73 2c 20 74 68 65 20 0a 2a 2a 20 tables, the .**
468d0 48 61 73 68 2e 68 74 20 74 61 62 6c 65 20 69 73 Hash.ht table is
468e0 20 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74 65 64 never allocated
468f0 20 62 65 63 61 75 73 65 20 69 66 20 74 68 65 72 because if ther
46900 65 20 61 72 65 20 66 65 77 20 65 6c 65 6d 65 6e e are few elemen
46910 74 73 0a 2a 2a 20 69 6e 20 74 68 65 20 74 61 62 ts.** in the tab
46920 6c 65 2c 20 69 74 20 69 73 20 66 61 73 74 65 72 le, it is faster
46930 20 74 6f 20 64 6f 20 61 20 6c 69 6e 65 61 72 20 to do a linear
46940 73 65 61 72 63 68 20 74 68 61 6e 20 74 6f 20 6d search than to m
46950 61 6e 61 67 65 0a 2a 2a 20 74 68 65 20 68 61 73 anage.** the has
46960 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 h table..*/.stru
46970 63 74 20 48 61 73 68 20 7b 0a 20 20 75 6e 73 69 ct Hash {. unsi
46980 67 6e 65 64 20 69 6e 74 20 68 74 73 69 7a 65 3b gned int htsize;
46990 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
469a0 6f 66 20 62 75 63 6b 65 74 73 20 69 6e 20 74 68 of buckets in th
469b0 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a e hash table */.
469c0 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 unsigned int c
469d0 6f 75 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 4e ount; /* N
469e0 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
469f0 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a in this table *
46a00 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 66 69 /. HashElem *fi
46a10 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a rst; /*
46a20 20 54 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 The first eleme
46a30 6e 74 20 6f 66 20 74 68 65 20 61 72 72 61 79 20 nt of the array
46a40 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20 */. struct _ht
46a50 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f { /
46a60 2a 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 * the hash table
46a70 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 6f 75 6e */. int coun
46a80 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
46a90 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
46aa0 65 6e 74 72 69 65 73 20 77 69 74 68 20 74 68 69 entries with thi
46ab0 73 20 68 61 73 68 20 2a 2f 0a 20 20 20 20 48 61 s hash */. Ha
46ac0 73 68 45 6c 65 6d 20 2a 63 68 61 69 6e 3b 20 20 shElem *chain;
46ad0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
46ae0 74 65 72 20 74 6f 20 66 69 72 73 74 20 65 6e 74 ter to first ent
46af0 72 79 20 77 69 74 68 20 74 68 69 73 20 68 61 73 ry with this has
46b00 68 20 2a 2f 0a 20 20 7d 20 2a 68 74 3b 0a 7d 3b h */. } *ht;.};
46b10 0a 0a 2f 2a 20 45 61 63 68 20 65 6c 65 6d 65 6e ../* Each elemen
46b20 74 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 t in the hash ta
46b30 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e ble is an instan
46b40 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
46b50 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 ing .** structur
46b60 65 2e 20 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 e. All elements
46b70 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61 are stored on a
46b80 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c single doubly-l
46b90 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a inked list..**.*
46ba0 2a 20 41 67 61 69 6e 2c 20 74 68 69 73 20 73 74 * Again, this st
46bb0 72 75 63 74 75 72 65 20 69 73 20 69 6e 74 65 6e ructure is inten
46bc0 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 ded to be opaque
46bd0 2c 20 62 75 74 20 69 74 20 63 61 6e 27 74 20 72 , but it can't r
46be0 65 61 6c 6c 79 0a 2a 2a 20 62 65 20 6f 70 61 71 eally.** be opaq
46bf0 75 65 20 62 65 63 61 75 73 65 20 69 74 20 69 73 ue because it is
46c00 20 75 73 65 64 20 62 79 20 6d 61 63 72 6f 73 2e used by macros.
46c10 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 73 68 45 .*/.struct HashE
46c20 6c 65 6d 20 7b 0a 20 20 48 61 73 68 45 6c 65 6d lem {. HashElem
46c30 20 2a 6e 65 78 74 2c 20 2a 70 72 65 76 3b 20 20 *next, *prev;
46c40 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 61 6e 64 /* Next and
46c50 20 70 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e previous elemen
46c60 74 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 ts in the table
46c70 2a 2f 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 3b */. void *data;
46c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46c90 20 20 2f 2a 20 44 61 74 61 20 61 73 73 6f 63 69 /* Data associ
46ca0 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65 ated with this e
46cb0 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 lement */. cons
46cc0 74 20 63 68 61 72 20 2a 70 4b 65 79 3b 20 69 6e t char *pKey; in
46cd0 74 20 6e 4b 65 79 3b 20 20 2f 2a 20 4b 65 79 20 t nKey; /* Key
46ce0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
46cf0 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a this element */.
46d00 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 };../*.** Access
46d10 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 6f 20 64 routines. To d
46d20 65 6c 65 74 65 2c 20 69 6e 73 65 72 74 20 61 20 elete, insert a
46d30 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f NULL pointer..*/
46d40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
46d50 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 void sqlite3Hash
46d60 49 6e 69 74 28 48 61 73 68 2a 29 3b 0a 53 51 4c Init(Hash*);.SQL
46d70 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
46d80 20 2a 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 *sqlite3HashIns
46d90 65 72 74 28 48 61 73 68 2a 2c 20 63 6f 6e 73 74 ert(Hash*, const
46da0 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e 74 char *pKey, int
46db0 20 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 70 44 61 nKey, void *pDa
46dc0 74 61 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ta);.SQLITE_PRIV
46dd0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
46de0 33 48 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20 3HashFind(const
46df0 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 63 68 61 Hash*, const cha
46e00 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 r *pKey, int nKe
46e10 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 y);.SQLITE_PRIVA
46e20 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 TE void sqlite3H
46e30 61 73 68 43 6c 65 61 72 28 48 61 73 68 2a 29 3b ashClear(Hash*);
46e40 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 ../*.** Macros f
46e50 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 or looping over
46e60 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 all elements of
46e70 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 a hash table. T
46e80 68 65 20 69 64 69 6f 6d 20 69 73 0a 2a 2a 20 6c he idiom is.** l
46e90 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 ike this:.**.**
46ea0 20 20 48 61 73 68 20 68 3b 0a 2a 2a 20 20 20 48 Hash h;.** H
46eb0 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 2a 2a 20 20 ashElem *p;.**
46ec0 20 2e 2e 2e 0a 2a 2a 20 20 20 66 6f 72 28 70 3d ....** for(p=
46ed0 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 sqliteHashFirst(
46ee0 26 68 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 &h); p; p=sqlite
46ef0 48 61 73 68 4e 65 78 74 28 70 29 29 7b 0a 2a 2a HashNext(p)){.**
46f00 20 20 20 20 20 53 6f 6d 65 53 74 72 75 63 74 75 SomeStructu
46f10 72 65 20 2a 70 44 61 74 61 20 3d 20 73 71 6c 69 re *pData = sqli
46f20 74 65 48 61 73 68 44 61 74 61 28 70 29 3b 0a 2a teHashData(p);.*
46f30 2a 20 20 20 20 20 2f 2f 20 64 6f 20 73 6f 6d 65 * // do some
46f40 74 68 69 6e 67 20 77 69 74 68 20 70 44 61 74 61 thing with pData
46f50 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a 23 64 65 66 69 .** }.*/.#defi
46f60 6e 65 20 73 71 6c 69 74 65 48 61 73 68 46 69 72 ne sqliteHashFir
46f70 73 74 28 48 29 20 20 28 28 48 29 2d 3e 66 69 72 st(H) ((H)->fir
46f80 73 74 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 st).#define sqli
46f90 74 65 48 61 73 68 4e 65 78 74 28 45 29 20 20 20 teHashNext(E)
46fa0 28 28 45 29 2d 3e 6e 65 78 74 29 0a 23 64 65 66 ((E)->next).#def
46fb0 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 44 61 ine sqliteHashDa
46fc0 74 61 28 45 29 20 20 20 28 28 45 29 2d 3e 64 61 ta(E) ((E)->da
46fd0 74 61 29 0a 2f 2a 20 23 64 65 66 69 6e 65 20 73 ta)./* #define s
46fe0 71 6c 69 74 65 48 61 73 68 4b 65 79 28 45 29 20 qliteHashKey(E)
46ff0 20 20 20 28 28 45 29 2d 3e 70 4b 65 79 29 20 2f ((E)->pKey) /
47000 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 2f 2a / NOT USED */./*
47010 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 #define sqliteH
47020 61 73 68 4b 65 79 73 69 7a 65 28 45 29 20 28 28 ashKeysize(E) ((
47030 45 29 2d 3e 6e 4b 65 79 29 20 20 2f 2f 20 4e 4f E)->nKey) // NO
47040 54 20 55 53 45 44 20 2a 2f 0a 0a 2f 2a 0a 2a 2a T USED */../*.**
47050 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
47060 65 73 20 69 6e 20 61 20 68 61 73 68 20 74 61 62 es in a hash tab
47070 6c 65 0a 2a 2f 0a 2f 2a 20 23 64 65 66 69 6e 65 le.*/./* #define
47080 20 73 71 6c 69 74 65 48 61 73 68 43 6f 75 6e 74 sqliteHashCount
47090 28 48 29 20 20 28 28 48 29 2d 3e 63 6f 75 6e 74 (H) ((H)->count
470a0 29 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f ) // NOT USED */
470b0 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c ..#endif /* _SQL
470c0 49 54 45 5f 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a ITE_HASH_H_ */..
470d0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
470e0 45 6e 64 20 6f 66 20 68 61 73 68 2e 68 20 2a 2a End of hash.h **
470f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
47100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
47110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
47120 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
47130 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
47140 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
47150 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
47160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
47170 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
47180 49 6e 63 6c 75 64 65 20 70 61 72 73 65 2e 68 20 Include parse.h
47190 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
471a0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a sqliteInt.h ***
471b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
471c0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
471d0 42 65 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65 Begin file parse
471e0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
471f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
47200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
47210 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 4d 49 20 #define TK_SEMI
47220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47230 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 1.#de
47240 66 69 6e 65 20 54 4b 5f 45 58 50 4c 41 49 4e 20 fine TK_EXPLAIN
47250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47260 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 2.#defin
47270 65 20 54 4b 5f 51 55 45 52 59 20 20 20 20 20 20 e TK_QUERY
47280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47290 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 54 3.#define T
472a0 4b 5f 50 4c 41 4e 20 20 20 20 20 20 20 20 20 20 K_PLAN
472b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
472c0 20 20 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 4.#define TK_B
472d0 45 47 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 EGIN
472e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
472f0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 52 41 4e .#define TK_TRAN
47300 53 41 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 SACTION
47310 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 6.#d
47320 65 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 52 45 efine TK_DEFERRE
47330 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 D
47340 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 7.#defi
47350 6e 65 20 54 4b 5f 49 4d 4d 45 44 49 41 54 45 20 ne TK_IMMEDIATE
47360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47370 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20 8.#define
47380 54 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 TK_EXCLUSIVE
47390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
473a0 20 20 20 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 9.#define TK_
473b0 43 4f 4d 4d 49 54 20 20 20 20 20 20 20 20 20 20 COMMIT
473c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
473d0 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4e 44 0.#define TK_END
473e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
473f0 20 20 20 20 20 20 20 20 20 20 20 20 31 31 0a 23 11.#
47400 64 65 66 69 6e 65 20 54 4b 5f 52 4f 4c 4c 42 41 define TK_ROLLBA
47410 43 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 CK
47420 20 20 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 12.#def
47430 69 6e 65 20 54 4b 5f 53 41 56 45 50 4f 49 4e 54 ine TK_SAVEPOINT
47440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47450 20 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65 13.#define
47460 20 54 4b 5f 52 45 4c 45 41 53 45 20 20 20 20 20 TK_RELEASE
47470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47480 20 20 20 31 34 0a 23 64 65 66 69 6e 65 20 54 4b 14.#define TK
47490 5f 54 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 _TO
474a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
474b0 31 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 41 15.#define TK_TA
474c0 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 BLE
474d0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 36 0a 16.
474e0 23 64 65 66 69 6e 65 20 54 4b 5f 43 52 45 41 54 #define TK_CREAT
474f0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
47500 20 20 20 20 20 20 20 20 20 20 31 37 0a 23 64 65 17.#de
47510 66 69 6e 65 20 54 4b 5f 49 46 20 20 20 20 20 20 fine TK_IF
47520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47530 20 20 20 20 20 20 20 31 38 0a 23 64 65 66 69 6e 18.#defin
47540 65 20 54 4b 5f 4e 4f 54 20 20 20 20 20 20 20 20 e TK_NOT
47550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47560 20 20 20 20 31 39 0a 23 64 65 66 69 6e 65 20 54 19.#define T
47570 4b 5f 45 58 49 53 54 53 20 20 20 20 20 20 20 20 K_EXISTS
47580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47590 20 32 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 20.#define TK_T
475a0 45 4d 50 20 20 20 20 20 20 20 20 20 20 20 20 20 EMP
475b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 31 21
475c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 50 20 20 .#define TK_LP
475d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
475e0 20 20 20 20 20 20 20 20 20 20 20 32 32 0a 23 64 22.#d
475f0 65 66 69 6e 65 20 54 4b 5f 52 50 20 20 20 20 20 efine TK_RP
47600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47610 20 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 69 23.#defi
47620 6e 65 20 54 4b 5f 41 53 20 20 20 20 20 20 20 20 ne TK_AS
47630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47640 20 20 20 20 20 32 34 0a 23 64 65 66 69 6e 65 20 24.#define
47650 54 4b 5f 43 4f 4d 4d 41 20 20 20 20 20 20 20 20 TK_COMMA
47660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47670 20 20 32 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 25.#define TK_
47680 49 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ID
47690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2
476a0 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 44 6.#define TK_IND
476b0 45 58 45 44 20 20 20 20 20 20 20 20 20 20 20 20 EXED
476c0 20 20 20 20 20 20 20 20 20 20 20 20 32 37 0a 23 27.#
476d0 64 65 66 69 6e 65 20 54 4b 5f 41 42 4f 52 54 20 define TK_ABORT
476e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
476f0 20 20 20 20 20 20 20 20 20 32 38 0a 23 64 65 66 28.#def
47700 69 6e 65 20 54 4b 5f 41 43 54 49 4f 4e 20 20 20 ine TK_ACTION
47710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47720 20 20 20 20 20 20 32 39 0a 23 64 65 66 69 6e 65 29.#define
47730 20 54 4b 5f 41 46 54 45 52 20 20 20 20 20 20 20 TK_AFTER
47740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47750 20 20 20 33 30 0a 23 64 65 66 69 6e 65 20 54 4b 30.#define TK
47760 5f 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 _ANALYZE
47770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47780 33 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 31.#define TK_AS
47790 43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 C
477a0 20 20 20 20 20 20 20 20 20 20 20 20 20 33 32 0a 32.
477b0 23 64 65 66 69 6e 65 20 54 4b 5f 41 54 54 41 43 #define TK_ATTAC
477c0 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 H
477d0 20 20 20 20 20 20 20 20 20 20 33 33 0a 23 64 65 33.#de
477e0 66 69 6e 65 20 54 4b 5f 42 45 46 4f 52 45 20 20 fine TK_BEFORE
477f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47800 20 20 20 20 20 20 20 33 34 0a 23 64 65 66 69 6e 34.#defin
47810 65 20 54 4b 5f 42 59 20 20 20 20 20 20 20 20 20 e TK_BY
47820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47830 20 20 20 20 33 35 0a 23 64 65 66 69 6e 65 20 54 35.#define T
47840 4b 5f 43 41 53 43 41 44 45 20 20 20 20 20 20 20 K_CASCADE
47850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47860 20 33 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 36.#define TK_C
47870 41 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20 AST
47880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 37 37
47890 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 .#define TK_COLU
478a0 4d 4e 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 MNKW
478b0 20 20 20 20 20 20 20 20 20 20 20 33 38 0a 23 64 38.#d
478c0 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 46 4c 49 43 efine TK_CONFLIC
478d0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
478e0 20 20 20 20 20 20 20 20 33 39 0a 23 64 65 66 69 39.#defi
478f0 6e 65 20 54 4b 5f 44 41 54 41 42 41 53 45 20 20 ne TK_DATABASE
47900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47910 20 20 20 20 20 34 30 0a 23 64 65 66 69 6e 65 20 40.#define
47920 54 4b 5f 44 45 53 43 20 20 20 20 20 20 20 20 20 TK_DESC
47930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47940 20 20 34 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41.#define TK_
47950 44 45 54 41 43 48 20 20 20 20 20 20 20 20 20 20 DETACH
47960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
47970 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 41 43 2.#define TK_EAC
47980 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 H
47990 20 20 20 20 20 20 20 20 20 20 20 20 34 33 0a 23 43.#
479a0 64 65 66 69 6e 65 20 54 4b 5f 46 41 49 4c 20 20 define TK_FAIL
479b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
479c0 20 20 20 20 20 20 20 20 20 34 34 0a 23 64 65 66 44.#def
479d0 69 6e 65 20 54 4b 5f 46 4f 52 20 20 20 20 20 20 ine TK_FOR
479e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
479f0 20 20 20 20 20 20 34 35 0a 23 64 65 66 69 6e 65 45.#define
47a00 20 54 4b 5f 49 47 4e 4f 52 45 20 20 20 20 20 20 TK_IGNORE
47a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47a20 20 20 20 34 36 0a 23 64 65 66 69 6e 65 20 54 4b 46.#define TK
47a30 5f 49 4e 49 54 49 41 4c 4c 59 20 20 20 20 20 20 _INITIALLY
47a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47a50 34 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 47.#define TK_IN
47a60 53 54 45 41 44 20 20 20 20 20 20 20 20 20 20 20 STEAD
47a70 20 20 20 20 20 20 20 20 20 20 20 20 20 34 38 0a 48.
47a80 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4b 45 5f #define TK_LIKE_
47a90 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 KW
47aa0 20 20 20 20 20 20 20 20 20 20 34 39 0a 23 64 65 49.#de
47ab0 66 69 6e 65 20 54 4b 5f 4d 41 54 43 48 20 20 20 fine TK_MATCH
47ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ad0 20 20 20 20 20 20 20 35 30 0a 23 64 65 66 69 6e 50.#defin
47ae0 65 20 54 4b 5f 4e 4f 20 20 20 20 20 20 20 20 20 e TK_NO
47af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47b00 20 20 20 20 35 31 0a 23 64 65 66 69 6e 65 20 54 51.#define T
47b10 4b 5f 4b 45 59 20 20 20 20 20 20 20 20 20 20 20 K_KEY
47b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47b30 20 35 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52.#define TK_O
47b40 46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 F
47b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 33 53
47b60 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 46 46 53 .#define TK_OFFS
47b70 45 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ET
47b80 20 20 20 20 20 20 20 20 20 20 20 35 34 0a 23 64 54.#d
47b90 65 66 69 6e 65 20 54 4b 5f 50 52 41 47 4d 41 20 efine TK_PRAGMA
47ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47bb0 20 20 20 20 20 20 20 20 35 35 0a 23 64 65 66 69 55.#defi
47bc0 6e 65 20 54 4b 5f 52 41 49 53 45 20 20 20 20 20 ne TK_RAISE
47bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47be0 20 20 20 20 20 35 36 0a 23 64 65 66 69 6e 65 20 56.#define
47bf0 54 4b 5f 52 45 50 4c 41 43 45 20 20 20 20 20 20 TK_REPLACE
47c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c10 20 20 35 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 57.#define TK_
47c20 52 45 53 54 52 49 43 54 20 20 20 20 20 20 20 20 RESTRICT
47c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
47c40 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 4f 57 8.#define TK_ROW
47c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c60 20 20 20 20 20 20 20 20 20 20 20 20 35 39 0a 23 59.#
47c70 64 65 66 69 6e 65 20 54 4b 5f 54 52 49 47 47 45 define TK_TRIGGE
47c80 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 R
47c90 20 20 20 20 20 20 20 20 20 36 30 0a 23 64 65 66 60.#def
47ca0 69 6e 65 20 54 4b 5f 56 41 43 55 55 4d 20 20 20 ine TK_VACUUM
47cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47cc0 20 20 20 20 20 20 36 31 0a 23 64 65 66 69 6e 65 61.#define
47cd0 20 54 4b 5f 56 49 45 57 20 20 20 20 20 20 20 20 TK_VIEW
47ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47cf0 20 20 20 36 32 0a 23 64 65 66 69 6e 65 20 54 4b 62.#define TK
47d00 5f 56 49 52 54 55 41 4c 20 20 20 20 20 20 20 20 _VIRTUAL
47d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d20 36 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 63.#define TK_RE
47d30 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20 INDEX
47d40 20 20 20 20 20 20 20 20 20 20 20 20 20 36 34 0a 64.
47d50 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 4e 41 4d #define TK_RENAM
47d60 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
47d70 20 20 20 20 20 20 20 20 20 20 36 35 0a 23 64 65 65.#de
47d80 66 69 6e 65 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 fine TK_CTIME_KW
47d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47da0 20 20 20 20 20 20 20 36 36 0a 23 64 65 66 69 6e 66.#defin
47db0 65 20 54 4b 5f 41 4e 59 20 20 20 20 20 20 20 20 e TK_ANY
47dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47dd0 20 20 20 20 36 37 0a 23 64 65 66 69 6e 65 20 54 67.#define T
47de0 4b 5f 4f 52 20 20 20 20 20 20 20 20 20 20 20 20 K_OR
47df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e00 20 36 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 68.#define TK_A
47e10 4e 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ND
47e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 39 69
47e30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 20 20 .#define TK_IS
47e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e50 20 20 20 20 20 20 20 20 20 20 20 37 30 0a 23 64 70.#d
47e60 65 66 69 6e 65 20 54 4b 5f 42 45 54 57 45 45 4e efine TK_BETWEEN
47e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e80 20 20 20 20 20 20 20 20 37 31 0a 23 64 65 66 69 71.#defi
47e90 6e 65 20 54 4b 5f 49 4e 20 20 20 20 20 20 20 20 ne TK_IN
47ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47eb0 20 20 20 20 20 37 32 0a 23 64 65 66 69 6e 65 20 72.#define
47ec0 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 20 20 20 TK_ISNULL
47ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ee0 20 20 37 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 73.#define TK_
47ef0 4e 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 NOTNULL
47f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
47f10 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 45 20 4.#define TK_NE
47f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f30 20 20 20 20 20 20 20 20 20 20 20 20 37 35 0a 23 75.#
47f40 64 65 66 69 6e 65 20 54 4b 5f 45 51 20 20 20 20 define TK_EQ
47f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f60 20 20 20 20 20 20 20 20 20 37 36 0a 23 64 65 66 76.#def
47f70 69 6e 65 20 54 4b 5f 47 54 20 20 20 20 20 20 20 ine TK_GT
47f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f90 20 20 20 20 20 20 37 37 0a 23 64 65 66 69 6e 65 77.#define
47fa0 20 54 4b 5f 4c 45 20 20 20 20 20 20 20 20 20 20 TK_LE
47fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47fc0 20 20 20 37 38 0a 23 64 65 66 69 6e 65 20 54 4b 78.#define TK
47fd0 5f 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20 _LT
47fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ff0 37 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 45 79.#define TK_GE
48000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48010 20 20 20 20 20 20 20 20 20 20 20 20 20 38 30 0a 80.
48020 23 64 65 66 69 6e 65 20 54 4b 5f 45 53 43 41 50 #define TK_ESCAP
48030 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
48040 20 20 20 20 20 20 20 20 20 20 38 31 0a 23 64 65 81.#de
48050 66 69 6e 65 20 54 4b 5f 42 49 54 41 4e 44 20 20 fine TK_BITAND
48060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48070 20 20 20 20 20 20 20 38 32 0a 23 64 65 66 69 6e 82.#defin
48080 65 20 54 4b 5f 42 49 54 4f 52 20 20 20 20 20 20 e TK_BITOR
48090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
480a0 20 20 20 20 38 33 0a 23 64 65 66 69 6e 65 20 54 83.#define T
480b0 4b 5f 4c 53 48 49 46 54 20 20 20 20 20 20 20 20 K_LSHIFT
480c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
480d0 20 38 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 84.#define TK_R
480e0 53 48 49 46 54 20 20 20 20 20 20 20 20 20 20 20 SHIFT
480f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 35 85
48100 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 4c 55 53 .#define TK_PLUS
48110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48120 20 20 20 20 20 20 20 20 20 20 20 38 36 0a 23 64 86.#d
48130 65 66 69 6e 65 20 54 4b 5f 4d 49 4e 55 53 20 20 efine TK_MINUS
48140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48150 20 20 20 20 20 20 20 20 38 37 0a 23 64 65 66 69 87.#defi
48160 6e 65 20 54 4b 5f 53 54 41 52 20 20 20 20 20 20 ne TK_STAR
48170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48180 20 20 20 20 20 38 38 0a 23 64 65 66 69 6e 65 20 88.#define
48190 54 4b 5f 53 4c 41 53 48 20 20 20 20 20 20 20 20 TK_SLASH
481a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
481b0 20 20 38 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 89.#define TK_
481c0 52 45 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 REM
481d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
481e0 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 0.#define TK_CON
481f0 43 41 54 20 20 20 20 20 20 20 20 20 20 20 20 20 CAT
48200 20 20 20 20 20 20 20 20 20 20 20 20 39 31 0a 23 91.#
48210 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 4c 41 54 define TK_COLLAT
48220 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
48230 20 20 20 20 20 20 20 20 20 39 32 0a 23 64 65 66 92.#def
48240 69 6e 65 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 ine TK_BITNOT
48250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48260 20 20 20 20 20 20 39 33 0a 23 64 65 66 69 6e 65 93.#define
48270 20 54 4b 5f 53 54 52 49 4e 47 20 20 20 20 20 20 TK_STRING
48280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48290 20 20 20 39 34 0a 23 64 65 66 69 6e 65 20 54 4b 94.#define TK
482a0 5f 4a 4f 49 4e 5f 4b 57 20 20 20 20 20 20 20 20 _JOIN_KW
482b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
482c0 39 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 95.#define TK_CO
482d0 4e 53 54 52 41 49 4e 54 20 20 20 20 20 20 20 20 NSTRAINT
482e0 20 20 20 20 20 20 20 20 20 20 20 20 20 39 36 0a 96.
482f0 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 41 55 #define TK_DEFAU
48300 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 LT
48310 20 20 20 20 20 20 20 20 20 20 39 37 0a 23 64 65 97.#de
48320 66 69 6e 65 20 54 4b 5f 4e 55 4c 4c 20 20 20 20 fine TK_NULL
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 39 38 0a 23 64 65 66 69 6e 98.#defin
48350 65 20 54 4b 5f 50 52 49 4d 41 52 59 20 20 20 20 e TK_PRIMARY
48360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48370 20 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20 54 99.#define T
48380 4b 5f 55 4e 49 51 55 45 20 20 20 20 20 20 20 20 K_UNIQUE
48390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
483a0 20 31 30 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 100.#define TK_
483b0 43 48 45 43 4b 20 20 20 20 20 20 20 20 20 20 20 CHECK
483c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
483d0 30 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 01.#define TK_RE
483e0 46 45 52 45 4e 43 45 53 20 20 20 20 20 20 20 20 FERENCES
483f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 32 102
48400 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 55 54 4f .#define TK_AUTO
48410 49 4e 43 52 20 20 20 20 20 20 20 20 20 20 20 20 INCR
48420 20 20 20 20 20 20 20 20 20 20 20 31 30 33 0a 23 103.#
48430 64 65 66 69 6e 65 20 54 4b 5f 4f 4e 20 20 20 20 define TK_ON
48440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48450 20 20 20 20 20 20 20 20 20 31 30 34 0a 23 64 65 104.#de
48460 66 69 6e 65 20 54 4b 5f 44 45 4c 45 54 45 20 20 fine TK_DELETE
48470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48480 20 20 20 20 20 20 20 31 30 35 0a 23 64 65 66 69 105.#defi
48490 6e 65 20 54 4b 5f 55 50 44 41 54 45 20 20 20 20 ne TK_UPDATE
484a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
484b0 20 20 20 20 20 31 30 36 0a 23 64 65 66 69 6e 65 106.#define
484c0 20 54 4b 5f 53 45 54 20 20 20 20 20 20 20 20 20 TK_SET
484d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
484e0 20 20 20 31 30 37 0a 23 64 65 66 69 6e 65 20 54 107.#define T
484f0 4b 5f 44 45 46 45 52 52 41 42 4c 45 20 20 20 20 K_DEFERRABLE
48500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48510 20 31 30 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 108.#define TK_
48520 46 4f 52 45 49 47 4e 20 20 20 20 20 20 20 20 20 FOREIGN
48530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48540 30 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 52 09.#define TK_DR
48550 4f 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OP
48560 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 30 110
48570 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49 4f .#define TK_UNIO
48580 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 N
48590 20 20 20 20 20 20 20 20 20 20 20 31 31 31 0a 23 111.#
485a0 64 65 66 69 6e 65 20 54 4b 5f 41 4c 4c 20 20 20 define TK_ALL
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 31 31 32 0a 23 64 65 112.#de
485d0 66 69 6e 65 20 54 4b 5f 45 58 43 45 50 54 20 20 fine TK_EXCEPT
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 31 31 33 0a 23 64 65 66 69 113.#defi
48600 6e 65 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 ne TK_INTERSECT
48610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48620 20 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e 65 114.#define
48630 20 54 4b 5f 53 45 4c 45 43 54 20 20 20 20 20 20 TK_SELECT
48640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48650 20 20 20 31 31 35 0a 23 64 65 66 69 6e 65 20 54 115.#define T
48660 4b 5f 44 49 53 54 49 4e 43 54 20 20 20 20 20 20 K_DISTINCT
48670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48680 20 31 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 116.#define TK_
48690 44 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20 DOT
486a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
486b0 31 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 52 17.#define TK_FR
486c0 4f 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OM
486d0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 38 118
486e0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e .#define TK_JOIN
486f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48700 20 20 20 20 20 20 20 20 20 20 20 31 31 39 0a 23 119.#
48710 64 65 66 69 6e 65 20 54 4b 5f 55 53 49 4e 47 20 define TK_USING
48720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48730 20 20 20 20 20 20 20 20 20 31 32 30 0a 23 64 65 120.#de
48740 66 69 6e 65 20 54 4b 5f 4f 52 44 45 52 20 20 20 fine TK_ORDER
48750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48760 20 20 20 20 20 20 20 31 32 31 0a 23 64 65 66 69 121.#defi
48770 6e 65 20 54 4b 5f 47 52 4f 55 50 20 20 20 20 20 ne TK_GROUP
48780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48790 20 20 20 20 20 31 32 32 0a 23 64 65 66 69 6e 65 122.#define
487a0 20 54 4b 5f 48 41 56 49 4e 47 20 20 20 20 20 20 TK_HAVING
487b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
487c0 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65 20 54 123.#define T
487d0 4b 5f 4c 49 4d 49 54 20 20 20 20 20 20 20 20 20 K_LIMIT
487e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
487f0 20 31 32 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 124.#define TK_
48800 57 48 45 52 45 20 20 20 20 20 20 20 20 20 20 20 WHERE
48810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48820 32 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 25.#define TK_IN
48830 54 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 TO
48840 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 36 126
48850 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 4c 55 .#define TK_VALU
48860 45 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ES
48870 20 20 20 20 20 20 20 20 20 20 20 31 32 37 0a 23 127.#
48880 64 65 66 69 6e 65 20 54 4b 5f 49 4e 53 45 52 54 define TK_INSERT
48890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
488a0 20 20 20 20 20 20 20 20 20 31 32 38 0a 23 64 65 128.#de
488b0 66 69 6e 65 20 54 4b 5f 49 4e 54 45 47 45 52 20 fine TK_INTEGER
488c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
488d0 20 20 20 20 20 20 20 31 32 39 0a 23 64 65 66 69 129.#defi
488e0 6e 65 20 54 4b 5f 46 4c 4f 41 54 20 20 20 20 20 ne TK_FLOAT
488f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48900 20 20 20 20 20 31 33 30 0a 23 64 65 66 69 6e 65 130.#define
48910 20 54 4b 5f 42 4c 4f 42 20 20 20 20 20 20 20 20 TK_BLOB
48920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48930 20 20 20 31 33 31 0a 23 64 65 66 69 6e 65 20 54 131.#define T
48940 4b 5f 52 45 47 49 53 54 45 52 20 20 20 20 20 20 K_REGISTER
48950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48960 20 31 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 132.#define TK_
48970 56 41 52 49 41 42 4c 45 20 20 20 20 20 20 20 20 VARIABLE
48980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48990 33 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 33.#define TK_CA
489a0 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 SE
489b0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34 134
489c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 57 48 45 4e .#define TK_WHEN
489d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
489e0 20 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23 135.#
489f0 64 65 66 69 6e 65 20 54 4b 5f 54 48 45 4e 20 20 define TK_THEN
48a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48a10 20 20 20 20 20 20 20 20 20 31 33 36 0a 23 64 65 136.#de
48a20 66 69 6e 65 20 54 4b 5f 45 4c 53 45 20 20 20 20 fine TK_ELSE
48a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48a40 20 20 20 20 20 20 20 31 33 37 0a 23 64 65 66 69 137.#defi
48a50 6e 65 20 54 4b 5f 49 4e 44 45 58 20 20 20 20 20 ne TK_INDEX
48a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48a70 20 20 20 20 20 31 33 38 0a 23 64 65 66 69 6e 65 138.#define
48a80 20 54 4b 5f 41 4c 54 45 52 20 20 20 20 20 20 20 TK_ALTER
48a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48aa0 20 20 20 31 33 39 0a 23 64 65 66 69 6e 65 20 54 139.#define T
48ab0 4b 5f 41 44 44 20 20 20 20 20 20 20 20 20 20 20 K_ADD
48ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48ad0 20 31 34 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 140.#define TK_
48ae0 54 4f 5f 54 45 58 54 20 20 20 20 20 20 20 20 20 TO_TEXT
48af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48b00 34 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 41.#define TK_TO
48b10 5f 42 4c 4f 42 20 20 20 20 20 20 20 20 20 20 20 _BLOB
48b20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 32 142
48b30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 4e .#define TK_TO_N
48b40 55 4d 45 52 49 43 20 20 20 20 20 20 20 20 20 20 UMERIC
48b50 20 20 20 20 20 20 20 20 20 20 20 31 34 33 0a 23 143.#
48b60 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 49 4e 54 define TK_TO_INT
48b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48b80 20 20 20 20 20 20 20 20 20 31 34 34 0a 23 64 65 144.#de
48b90 66 69 6e 65 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 fine TK_TO_REAL
48ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48bb0 20 20 20 20 20 20 20 31 34 35 0a 23 64 65 66 69 145.#defi
48bc0 6e 65 20 54 4b 5f 49 53 4e 4f 54 20 20 20 20 20 ne TK_ISNOT
48bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48be0 20 20 20 20 20 31 34 36 0a 23 64 65 66 69 6e 65 146.#define
48bf0 20 54 4b 5f 45 4e 44 5f 4f 46 5f 46 49 4c 45 20 TK_END_OF_FILE
48c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48c10 20 20 20 31 34 37 0a 23 64 65 66 69 6e 65 20 54 147.#define T
48c20 4b 5f 49 4c 4c 45 47 41 4c 20 20 20 20 20 20 20 K_ILLEGAL
48c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48c40 20 31 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 148.#define TK_
48c50 53 50 41 43 45 20 20 20 20 20 20 20 20 20 20 20 SPACE
48c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48c70 34 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49.#define TK_UN
48c80 43 4c 4f 53 45 44 5f 53 54 52 49 4e 47 20 20 20 CLOSED_STRING
48c90 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 30 150
48ca0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 55 4e 43 .#define TK_FUNC
48cb0 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 TION
48cc0 20 20 20 20 20 20 20 20 20 20 20 31 35 31 0a 23 151.#
48cd0 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e define TK_COLUMN
48ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48cf0 20 20 20 20 20 20 20 20 20 31 35 32 0a 23 64 65 152.#de
48d00 66 69 6e 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 fine TK_AGG_FUNC
48d10 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 TION
48d20 20 20 20 20 20 20 20 31 35 33 0a 23 64 65 66 69 153.#defi
48d30 6e 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e ne TK_AGG_COLUMN
48d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48d50 20 20 20 20 20 31 35 34 0a 23 64 65 66 69 6e 65 154.#define
48d60 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 20 TK_CONST_FUNC
48d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48d80 20 20 20 31 35 35 0a 23 64 65 66 69 6e 65 20 54 155.#define T
48d90 4b 5f 55 4d 49 4e 55 53 20 20 20 20 20 20 20 20 K_UMINUS
48da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48db0 20 31 35 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 156.#define TK_
48dc0 55 50 4c 55 53 20 20 20 20 20 20 20 20 20 20 20 UPLUS
48dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48de0 35 37 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 57../***********
48df0 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 *** End of parse
48e00 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
48e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48e30 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
48e40 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
48e50 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
48e60 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 in sqliteInt.h
48e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48e80 2a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 **/.#include <st
48e90 64 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 dio.h>.#include
48ea0 3c 73 74 64 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c <stdlib.h>.#incl
48eb0 75 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 ude <string.h>.#
48ec0 69 6e 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e include <assert.
48ed0 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 h>.#include <std
48ee0 64 65 66 2e 68 3e 0a 0a 2f 2a 0a 2a 2a 20 49 66 def.h>../*.** If
48ef0 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 compiling for a
48f00 20 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74 20 processor that
48f10 6c 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 lacks floating p
48f20 6f 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a oint support,.**
48f30 20 73 75 62 73 74 69 74 75 74 65 20 69 6e 74 65 substitute inte
48f40 67 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 ger for floating
48f50 2d 70 6f 69 6e 74 0a 2a 2f 0a 23 69 66 64 65 66 -point.*/.#ifdef
48f60 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
48f70 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 ATING_POINT.# de
48f80 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 fine double sqli
48f90 74 65 5f 69 6e 74 36 34 0a 23 20 64 65 66 69 6e te_int64.# defin
48fa0 65 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 e LONGDOUBLE_TYP
48fb0 45 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23 E sqlite_int64.#
48fc0 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 42 ifndef SQLITE_B
48fd0 49 47 5f 44 42 4c 0a 23 20 20 20 64 65 66 69 6e IG_DBL.# defin
48fe0 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c e SQLITE_BIG_DBL
48ff0 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 (((sqlite3_int6
49000 34 29 31 29 3c 3c 35 30 29 0a 23 20 65 6e 64 69 4)1)<<50).# endi
49010 66 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 f.# define SQLIT
49020 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f E_OMIT_DATETIME_
49030 46 55 4e 43 53 20 31 0a 23 20 64 65 66 69 6e 65 FUNCS 1.# define
49040 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 SQLITE_OMIT_TRA
49050 43 45 20 31 0a 23 20 75 6e 64 65 66 20 53 51 4c CE 1.# undef SQL
49060 49 54 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e ITE_MIXED_ENDIAN
49070 5f 36 34 42 49 54 5f 46 4c 4f 41 54 0a 23 20 75 _64BIT_FLOAT.# u
49080 6e 64 65 66 20 53 51 4c 49 54 45 5f 48 41 56 45 ndef SQLITE_HAVE
49090 5f 49 53 4e 41 4e 0a 23 65 6e 64 69 66 0a 23 69 _ISNAN.#endif.#i
490a0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 42 49 47 fndef SQLITE_BIG
490b0 5f 44 42 4c 0a 23 20 64 65 66 69 6e 65 20 53 51 _DBL.# define SQ
490c0 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20 28 31 65 LITE_BIG_DBL (1e
490d0 39 39 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 99).#endif../*.*
490e0 2a 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 69 73 * OMIT_TEMPDB is
490f0 20 73 65 74 20 74 6f 20 31 20 69 66 20 53 51 4c set to 1 if SQL
49100 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 20 ITE_OMIT_TEMPDB
49110 69 73 20 64 65 66 69 6e 65 64 2c 20 6f 72 20 30 is defined, or 0
49120 0a 2a 2a 20 61 66 74 65 72 77 61 72 64 2e 20 48 .** afterward. H
49130 61 76 69 6e 67 20 74 68 69 73 20 6d 61 63 72 6f aving this macro
49140 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f 20 63 61 allows us to ca
49150 75 73 65 20 74 68 65 20 43 20 63 6f 6d 70 69 6c use the C compil
49160 65 72 20 0a 2a 2a 20 74 6f 20 6f 6d 69 74 20 63 er .** to omit c
49170 6f 64 65 20 75 73 65 64 20 62 79 20 54 45 4d 50 ode used by TEMP
49180 20 74 61 62 6c 65 73 20 77 69 74 68 6f 75 74 20 tables without
49190 6d 65 73 73 79 20 23 69 66 6e 64 65 66 20 73 74 messy #ifndef st
491a0 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 69 66 atements..*/.#if
491b0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
491c0 54 45 4d 50 44 42 0a 23 64 65 66 69 6e 65 20 4f TEMPDB.#define O
491d0 4d 49 54 5f 54 45 4d 50 44 42 20 31 0a 23 65 6c MIT_TEMPDB 1.#el
491e0 73 65 0a 23 64 65 66 69 6e 65 20 4f 4d 49 54 5f se.#define OMIT_
491f0 54 45 4d 50 44 42 20 30 0a 23 65 6e 64 69 66 0a TEMPDB 0.#endif.
49200 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f ./*.** If the fo
49210 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 llowing macro is
49220 20 73 65 74 20 74 6f 20 31 2c 20 74 68 65 6e 20 set to 1, then
49230 4e 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20 NULL values are
49240 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 64 69 considered.** di
49250 73 74 69 6e 63 74 20 77 68 65 6e 20 64 65 74 65 stinct when dete
49260 72 6d 69 6e 69 6e 67 20 77 68 65 74 68 65 72 20 rmining whether
49270 6f 72 20 6e 6f 74 20 74 77 6f 20 65 6e 74 72 69 or not two entri
49280 65 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 0a es are the same.
49290 2a 2a 20 69 6e 20 61 20 55 4e 49 51 55 45 20 69 ** in a UNIQUE i
492a0 6e 64 65 78 2e 20 20 54 68 69 73 20 69 73 20 74 ndex. This is t
492b0 68 65 20 77 61 79 20 50 6f 73 74 67 72 65 53 51 he way PostgreSQ
492c0 4c 2c 20 4f 72 61 63 6c 65 2c 20 44 42 32 2c 20 L, Oracle, DB2,
492d0 4d 79 53 51 4c 2c 0a 2a 2a 20 4f 43 45 4c 4f 54 MySQL,.** OCELOT
492e0 2c 20 61 6e 64 20 46 69 72 65 62 69 72 64 20 61 , and Firebird a
492f0 6c 6c 20 77 6f 72 6b 2e 20 20 54 68 65 20 53 51 ll work. The SQ
49300 4c 39 32 20 73 70 65 63 20 65 78 70 6c 69 63 69 L92 spec explici
49310 74 6c 79 20 73 61 79 73 20 74 68 69 73 0a 2a 2a tly says this.**
49320 20 69 73 20 74 68 65 20 77 61 79 20 74 68 69 6e is the way thin
49330 67 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74 gs are suppose t
49340 6f 20 77 6f 72 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 o work..**.** If
49350 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d the following m
49360 61 63 72 6f 20 69 73 20 73 65 74 20 74 6f 20 30 acro is set to 0
49370 2c 20 74 68 65 20 4e 55 4c 4c 73 20 61 72 65 20 , the NULLs are
49380 69 6e 64 69 73 74 69 6e 63 74 20 66 6f 72 0a 2a indistinct for.*
49390 2a 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78 * a UNIQUE index
493a0 2e 20 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 2c . In this mode,
493b0 20 79 6f 75 20 63 61 6e 20 6f 6e 6c 79 20 68 61 you can only ha
493c0 76 65 20 61 20 73 69 6e 67 6c 65 20 4e 55 4c 4c ve a single NULL
493d0 20 65 6e 74 72 79 0a 2a 2a 20 66 6f 72 20 61 20 entry.** for a
493e0 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 65 64 20 column declared
493f0 55 4e 49 51 55 45 2e 20 20 54 68 69 73 20 69 73 UNIQUE. This is
49400 20 74 68 65 20 77 61 79 20 49 6e 66 6f 72 6d 69 the way Informi
49410 78 20 61 6e 64 20 53 51 4c 20 53 65 72 76 65 72 x and SQL Server
49420 0a 2a 2a 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 64 65 .** work..*/.#de
49430 66 69 6e 65 20 4e 55 4c 4c 5f 44 49 53 54 49 4e fine NULL_DISTIN
49440 43 54 5f 46 4f 52 5f 55 4e 49 51 55 45 20 31 0a CT_FOR_UNIQUE 1.
49450 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 66 69 6c 65 ./*.** The "file
49460 20 66 6f 72 6d 61 74 22 20 6e 75 6d 62 65 72 20 format" number
49470 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 is an integer th
49480 61 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 at is incremente
49490 64 20 77 68 65 6e 65 76 65 72 0a 2a 2a 20 74 68 d whenever.** th
494a0 65 20 56 44 42 45 2d 6c 65 76 65 6c 20 66 69 6c e VDBE-level fil
494b0 65 20 66 6f 72 6d 61 74 20 63 68 61 6e 67 65 73 e format changes
494c0 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
494d0 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74 macros define t
494e0 68 65 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c he.** the defaul
494f0 74 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f t file format fo
49500 72 20 6e 65 77 20 64 61 74 61 62 61 73 65 73 20 r new databases
49510 61 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 and the maximum
49520 66 69 6c 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 74 file format.** t
49530 68 61 74 20 74 68 65 20 6c 69 62 72 61 72 79 20 hat the library
49540 63 61 6e 20 72 65 61 64 2e 0a 2a 2f 0a 23 64 65 can read..*/.#de
49550 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f fine SQLITE_MAX_
49560 46 49 4c 45 5f 46 4f 52 4d 41 54 20 34 0a 23 69 FILE_FORMAT 4.#i
49570 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 fndef SQLITE_DEF
49580 41 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 AULT_FILE_FORMAT
49590 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
495a0 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f _DEFAULT_FILE_FO
495b0 52 4d 41 54 20 31 0a 23 65 6e 64 69 66 0a 0a 23 RMAT 1.#endif..#
495c0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 ifndef SQLITE_DE
495d0 46 41 55 4c 54 5f 52 45 43 55 52 53 49 56 45 5f FAULT_RECURSIVE_
495e0 54 52 49 47 47 45 52 53 0a 23 20 64 65 66 69 6e TRIGGERS.# defin
495f0 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 e SQLITE_DEFAULT
49600 5f 52 45 43 55 52 53 49 56 45 5f 54 52 49 47 47 _RECURSIVE_TRIGG
49610 45 52 53 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a ERS 0.#endif../*
49620 0a 2a 2a 20 50 72 6f 76 69 64 65 20 61 20 64 65 .** Provide a de
49630 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 fault value for
49640 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 SQLITE_TEMP_STOR
49650 45 20 69 6e 20 63 61 73 65 20 69 74 20 69 73 20 E in case it is
49660 6e 6f 74 20 73 70 65 63 69 66 69 65 64 0a 2a 2a not specified.**
49670 20 6f 6e 20 74 68 65 20 63 6f 6d 6d 61 6e 64 2d on the command-
49680 6c 69 6e 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 line.*/.#ifndef
49690 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 SQLITE_TEMP_STOR
496a0 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 E.# define SQLIT
496b0 45 5f 54 45 4d 50 5f 53 54 4f 52 45 20 31 0a 23 E_TEMP_STORE 1.#
496c0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 43 43 endif../*.** GCC
496d0 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 does not define
496e0 20 74 68 65 20 6f 66 66 73 65 74 6f 66 28 29 20 the offsetof()
496f0 6d 61 63 72 6f 20 73 6f 20 77 65 27 6c 6c 20 68 macro so we'll h
49700 61 76 65 20 74 6f 20 64 6f 20 69 74 0a 2a 2a 20 ave to do it.**
49710 6f 75 72 73 65 6c 76 65 73 2e 0a 2a 2f 0a 23 69 ourselves..*/.#i
49720 66 6e 64 65 66 20 6f 66 66 73 65 74 6f 66 0a 23 fndef offsetof.#
49730 64 65 66 69 6e 65 20 6f 66 66 73 65 74 6f 66 28 define offsetof(
49740 53 54 52 55 43 54 55 52 45 2c 46 49 45 4c 44 29 STRUCTURE,FIELD)
49750 20 28 28 69 6e 74 29 28 28 63 68 61 72 2a 29 26 ((int)((char*)&
49760 28 28 53 54 52 55 43 54 55 52 45 2a 29 30 29 2d ((STRUCTURE*)0)-
49770 3e 46 49 45 4c 44 29 29 0a 23 65 6e 64 69 66 0a >FIELD)).#endif.
49780 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 ./*.** Check to
49790 73 65 65 20 69 66 20 74 68 69 73 20 6d 61 63 68 see if this mach
497a0 69 6e 65 20 75 73 65 73 20 45 42 43 44 49 43 2e ine uses EBCDIC.
497b0 20 20 28 59 65 73 2c 20 62 65 6c 69 65 76 65 20 (Yes, believe
497c0 69 74 20 6f 72 0a 2a 2a 20 6e 6f 74 2c 20 74 68 it or.** not, th
497d0 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 61 ere are still ma
497e0 63 68 69 6e 65 73 20 6f 75 74 20 74 68 65 72 65 chines out there
497f0 20 74 68 61 74 20 75 73 65 20 45 42 43 44 49 43 that use EBCDIC
49800 2e 29 0a 2a 2f 0a 23 69 66 20 27 41 27 20 3d 3d .).*/.#if 'A' ==
49810 20 27 5c 33 30 31 27 0a 23 20 64 65 66 69 6e 65 '\301'.# define
49820 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 20 31 SQLITE_EBCDIC 1
49830 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
49840 53 51 4c 49 54 45 5f 41 53 43 49 49 20 31 0a 23 SQLITE_ASCII 1.#
49850 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 endif../*.** Int
49860 65 67 65 72 73 20 6f 66 20 6b 6e 6f 77 6e 20 73 egers of known s
49870 69 7a 65 73 2e 20 20 54 68 65 73 65 20 74 79 70 izes. These typ
49880 65 64 65 66 73 20 6d 69 67 68 74 20 63 68 61 6e edefs might chan
49890 67 65 20 66 6f 72 20 61 72 63 68 69 74 65 63 74 ge for architect
498a0 75 72 65 73 0a 2a 2a 20 77 68 65 72 65 20 74 68 ures.** where th
498b0 65 20 73 69 7a 65 73 20 76 65 72 79 2e 20 20 50 e sizes very. P
498c0 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 reprocessor macr
498d0 6f 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 os are available
498e0 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 so that the.**
498f0 74 79 70 65 73 20 63 61 6e 20 62 65 20 63 6f 6e types can be con
49900 76 65 6e 69 65 6e 74 6c 79 20 72 65 64 65 66 69 veniently redefi
49910 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 ned at compile-t
49920 79 70 65 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a ype. Like this:
49930 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 .**.** c
49940 63 20 27 2d 44 55 49 4e 54 50 54 52 5f 54 59 50 c '-DUINTPTR_TYP
49950 45 3d 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 27 E=long long int'
49960 20 2e 2e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ....*/.#ifndef
49970 55 49 4e 54 33 32 5f 54 59 50 45 0a 23 20 69 66 UINT32_TYPE.# if
49980 64 65 66 20 48 41 56 45 5f 55 49 4e 54 33 32 5f def HAVE_UINT32_
49990 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 T.# define UINT
499a0 33 32 5f 54 59 50 45 20 75 69 6e 74 33 32 5f 74 32_TYPE uint32_t
499b0 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e .# else.# defin
499c0 65 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 6e e UINT32_TYPE un
499d0 73 69 67 6e 65 64 20 69 6e 74 0a 23 20 65 6e 64 signed int.# end
499e0 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 if.#endif.#ifnde
499f0 66 20 55 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 f UINT16_TYPE.#
49a00 69 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54 31 ifdef HAVE_UINT1
49a10 36 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49 6_T.# define UI
49a20 4e 54 31 36 5f 54 59 50 45 20 75 69 6e 74 31 36 NT16_TYPE uint16
49a30 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 _t.# else.# def
49a40 69 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20 ine UINT16_TYPE
49a50 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69 unsigned short i
49a60 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 nt.# endif.#endi
49a70 66 0a 23 69 66 6e 64 65 66 20 49 4e 54 31 36 5f f.#ifndef INT16_
49a80 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 TYPE.# ifdef HAV
49a90 45 5f 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66 E_INT16_T.# def
49aa0 69 6e 65 20 49 4e 54 31 36 5f 54 59 50 45 20 69 ine INT16_TYPE i
49ab0 6e 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a 23 20 nt16_t.# else.#
49ac0 20 64 65 66 69 6e 65 20 49 4e 54 31 36 5f 54 59 define INT16_TY
49ad0 50 45 20 73 68 6f 72 74 20 69 6e 74 0a 23 20 65 PE short int.# e
49ae0 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e ndif.#endif.#ifn
49af0 64 65 66 20 55 49 4e 54 38 5f 54 59 50 45 0a 23 def UINT8_TYPE.#
49b00 20 69 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54 ifdef HAVE_UINT
49b10 38 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49 8_T.# define UI
49b20 4e 54 38 5f 54 59 50 45 20 75 69 6e 74 38 5f 74 NT8_TYPE uint8_t
49b30 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e .# else.# defin
49b40 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75 6e 73 e UINT8_TYPE uns
49b50 69 67 6e 65 64 20 63 68 61 72 0a 23 20 65 6e 64 igned char.# end
49b60 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 if.#endif.#ifnde
49b70 66 20 49 4e 54 38 5f 54 59 50 45 0a 23 20 69 66 f INT8_TYPE.# if
49b80 64 65 66 20 48 41 56 45 5f 49 4e 54 38 5f 54 0a def HAVE_INT8_T.
49b90 23 20 20 64 65 66 69 6e 65 20 49 4e 54 38 5f 54 # define INT8_T
49ba0 59 50 45 20 69 6e 74 38 5f 74 0a 23 20 65 6c 73 YPE int8_t.# els
49bb0 65 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 38 e.# define INT8
49bc0 5f 54 59 50 45 20 73 69 67 6e 65 64 20 63 68 61 _TYPE signed cha
49bd0 72 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 r.# endif.#endif
49be0 0a 23 69 66 6e 64 65 66 20 4c 4f 4e 47 44 4f 55 .#ifndef LONGDOU
49bf0 42 4c 45 5f 54 59 50 45 0a 23 20 64 65 66 69 6e BLE_TYPE.# defin
49c00 65 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 e LONGDOUBLE_TYP
49c10 45 20 6c 6f 6e 67 20 64 6f 75 62 6c 65 0a 23 65 E long double.#e
49c20 6e 64 69 66 0a 74 79 70 65 64 65 66 20 73 71 6c ndif.typedef sql
49c30 69 74 65 5f 69 6e 74 36 34 20 69 36 34 3b 20 20 ite_int64 i64;
49c40 20 20 20 20 20 20 20 20 2f 2a 20 38 2d 62 79 74 /* 8-byt
49c50 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 e signed integer
49c60 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 71 6c 69 */.typedef sqli
49c70 74 65 5f 75 69 6e 74 36 34 20 75 36 34 3b 20 20 te_uint64 u64;
49c80 20 20 20 20 20 20 20 2f 2a 20 38 2d 62 79 74 65 /* 8-byte
49c90 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 unsigned intege
49ca0 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 4e r */.typedef UIN
49cb0 54 33 32 5f 54 59 50 45 20 75 33 32 3b 20 20 20 T32_TYPE u32;
49cc0 20 20 20 20 20 20 20 20 2f 2a 20 34 2d 62 79 74 /* 4-byt
49cd0 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 e unsigned integ
49ce0 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 er */.typedef UI
49cf0 4e 54 31 36 5f 54 59 50 45 20 75 31 36 3b 20 20 NT16_TYPE u16;
49d00 20 20 20 20 20 20 20 20 20 2f 2a 20 32 2d 62 79 /* 2-by
49d10 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 te unsigned inte
49d20 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 49 ger */.typedef I
49d30 4e 54 31 36 5f 54 59 50 45 20 69 31 36 3b 20 20 NT16_TYPE i16;
49d40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 2d 62 /* 2-b
49d50 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 yte signed integ
49d60 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 er */.typedef UI
49d70 4e 54 38 5f 54 59 50 45 20 75 38 3b 20 20 20 20 NT8_TYPE u8;
49d80 20 20 20 20 20 20 20 20 20 2f 2a 20 31 2d 62 79 /* 1-by
49d90 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 te unsigned inte
49da0 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 49 ger */.typedef I
49db0 4e 54 38 5f 54 59 50 45 20 69 38 3b 20 20 20 20 NT8_TYPE i8;
49dc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 2d 62 /* 1-b
49dd0 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 yte signed integ
49de0 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c er */../*.** SQL
49df0 49 54 45 5f 4d 41 58 5f 55 33 32 20 69 73 20 61 ITE_MAX_U32 is a
49e00 20 75 36 34 20 63 6f 6e 73 74 61 6e 74 20 74 68 u64 constant th
49e10 61 74 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 at is the maximu
49e20 6d 20 75 36 34 20 76 61 6c 75 65 0a 2a 2a 20 74 m u64 value.** t
49e30 68 61 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65 hat can be store
49e40 64 20 69 6e 20 61 20 75 33 32 20 77 69 74 68 6f d in a u32 witho
49e50 75 74 20 6c 6f 73 73 20 6f 66 20 64 61 74 61 2e ut loss of data.
49e60 20 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 The value.** i
49e70 73 20 30 78 30 30 30 30 30 30 30 30 66 66 66 66 s 0x00000000ffff
49e80 66 66 66 66 2e 20 20 42 75 74 20 62 65 63 61 75 ffff. But becau
49e90 73 65 20 6f 66 20 71 75 69 72 6b 73 20 6f 66 20 se of quirks of
49ea0 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20 some compilers,
49eb0 77 65 0a 2a 2a 20 68 61 76 65 20 74 6f 20 73 70 we.** have to sp
49ec0 65 63 69 66 79 20 74 68 65 20 76 61 6c 75 65 20 ecify the value
49ed0 69 6e 20 74 68 65 20 6c 65 73 73 20 69 6e 74 75 in the less intu
49ee0 69 74 69 76 65 20 6d 61 6e 6e 65 72 20 73 68 6f itive manner sho
49ef0 77 6e 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 wn:.*/.#define S
49f00 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32 20 20 28 QLITE_MAX_U32 (
49f10 28 28 28 75 36 34 29 31 29 3c 3c 33 32 29 2d 31 (((u64)1)<<32)-1
49f20 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 )../*.** Macros
49f30 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 to determine whe
49f40 74 68 65 72 20 74 68 65 20 6d 61 63 68 69 6e 65 ther the machine
49f50 20 69 73 20 62 69 67 20 6f 72 20 6c 69 74 74 6c is big or littl
49f60 65 20 65 6e 64 69 61 6e 2c 0a 2a 2a 20 65 76 61 e endian,.** eva
49f70 6c 75 61 74 65 64 20 61 74 20 72 75 6e 74 69 6d luated at runtim
49f80 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c e..*/.#ifdef SQL
49f90 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e ITE_AMALGAMATION
49fa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
49fb0 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65 const int sqlite
49fc0 33 6f 6e 65 20 3d 20 31 3b 0a 23 65 6c 73 65 0a 3one = 1;.#else.
49fd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
49fe0 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65 33 onst int sqlite3
49ff0 6f 6e 65 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 one;.#endif.#if
4a000 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c defined(i386) ||
4a010 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f defined(__i386_
4a020 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d _) || defined(_M
4a030 5f 49 58 38 36 29 5c 0a 20 20 20 20 20 20 20 20 _IX86)\.
4a040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4a050 20 20 20 20 20 7c 7c 20 64 65 66 69 6e 65 64 28 || defined(
4a060 5f 5f 78 38 36 5f 36 34 29 20 7c 7c 20 64 65 66 __x86_64) || def
4a070 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 ined(__x86_64__)
4a080 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
4a090 5f 42 49 47 45 4e 44 49 41 4e 20 20 20 20 30 0a _BIGENDIAN 0.
4a0a0 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
4a0b0 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 31 0a 23 LITTLEENDIAN 1.#
4a0c0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 define SQLITE_U
4a0d0 54 46 31 36 4e 41 54 49 56 45 20 20 53 51 4c 49 TF16NATIVE SQLI
4a0e0 54 45 5f 55 54 46 31 36 4c 45 0a 23 65 6c 73 65 TE_UTF16LE.#else
4a0f0 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
4a100 5f 42 49 47 45 4e 44 49 41 4e 20 20 20 20 28 2a _BIGENDIAN (*
4a110 28 63 68 61 72 20 2a 29 28 26 73 71 6c 69 74 65 (char *)(&sqlite
4a120 33 6f 6e 65 29 3d 3d 30 29 0a 23 20 64 65 66 69 3one)==0).# defi
4a130 6e 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 ne SQLITE_LITTLE
4a140 45 4e 44 49 41 4e 20 28 2a 28 63 68 61 72 20 2a ENDIAN (*(char *
4a150 29 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d )(&sqlite3one)==
4a160 31 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 1).# define SQLI
4a170 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20 28 TE_UTF16NATIVE (
4a180 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e SQLITE_BIGENDIAN
4a190 3f 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3a ?SQLITE_UTF16BE:
4a1a0 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29 0a SQLITE_UTF16LE).
4a1b0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f #endif../*.** Co
4a1c0 6e 73 74 61 6e 74 73 20 66 6f 72 20 74 68 65 20 nstants for the
4a1d0 6c 61 72 67 65 73 74 20 61 6e 64 20 73 6d 61 6c largest and smal
4a1e0 6c 65 73 74 20 70 6f 73 73 69 62 6c 65 20 36 34 lest possible 64
4a1f0 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 -bit signed inte
4a200 67 65 72 73 2e 0a 2a 2a 20 54 68 65 73 65 20 6d gers..** These m
4a210 61 63 72 6f 73 20 61 72 65 20 64 65 73 69 67 6e acros are design
4a220 65 64 20 74 6f 20 77 6f 72 6b 20 63 6f 72 72 65 ed to work corre
4a230 63 74 6c 79 20 6f 6e 20 62 6f 74 68 20 33 32 2d ctly on both 32-
4a240 62 69 74 20 61 6e 64 20 36 34 2d 62 69 74 0a 2a bit and 64-bit.*
4a250 2a 20 63 6f 6d 70 69 6c 65 72 73 2e 0a 2a 2f 0a * compilers..*/.
4a260 23 64 65 66 69 6e 65 20 4c 41 52 47 45 53 54 5f #define LARGEST_
4a270 49 4e 54 36 34 20 20 28 30 78 66 66 66 66 66 66 INT64 (0xffffff
4a280 66 66 7c 28 28 28 69 36 34 29 30 78 37 66 66 66 ff|(((i64)0x7fff
4a290 66 66 66 66 29 3c 3c 33 32 29 29 0a 23 64 65 66 ffff)<<32)).#def
4a2a0 69 6e 65 20 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 ine SMALLEST_INT
4a2b0 36 34 20 28 28 28 69 36 34 29 2d 31 29 20 2d 20 64 (((i64)-1) -
4a2c0 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 29 0a 0a LARGEST_INT64)..
4a2d0 2f 2a 20 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 /* .** Round up
4a2e0 61 20 6e 75 6d 62 65 72 20 74 6f 20 74 68 65 20 a number to the
4a2f0 6e 65 78 74 20 6c 61 72 67 65 72 20 6d 75 6c 74 next larger mult
4a300 69 70 6c 65 20 6f 66 20 38 2e 20 20 54 68 69 73 iple of 8. This
4a310 20 69 73 20 75 73 65 64 0a 2a 2a 20 74 6f 20 66 is used.** to f
4a320 6f 72 63 65 20 38 2d 62 79 74 65 20 61 6c 69 67 orce 8-byte alig
4a330 6e 6d 65 6e 74 20 6f 6e 20 36 34 2d 62 69 74 20 nment on 64-bit
4a340 61 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a 2a architectures..*
4a350 2f 0a 23 64 65 66 69 6e 65 20 52 4f 55 4e 44 38 /.#define ROUND8
4a360 28 78 29 20 20 20 20 20 28 28 28 78 29 2b 37 29 (x) (((x)+7)
4a370 26 7e 37 29 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e &~7)../*.** Roun
4a380 64 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 6e 65 d down to the ne
4a390 61 72 65 73 74 20 6d 75 6c 74 69 70 6c 65 20 6f arest multiple o
4a3a0 66 20 38 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 f 8.*/.#define R
4a3b0 4f 55 4e 44 44 4f 57 4e 38 28 78 29 20 28 28 78 OUNDDOWN8(x) ((x
4a3c0 29 26 7e 37 29 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 )&~7)../*.** Ass
4a3d0 65 72 74 20 74 68 61 74 20 74 68 65 20 70 6f 69 ert that the poi
4a3e0 6e 74 65 72 20 58 20 69 73 20 61 6c 69 67 6e 65 nter X is aligne
4a3f0 64 20 74 6f 20 61 6e 20 38 2d 62 79 74 65 20 62 d to an 8-byte b
4a400 6f 75 6e 64 61 72 79 2e 0a 2a 2f 0a 23 64 65 66 oundary..*/.#def
4a410 69 6e 65 20 45 49 47 48 54 5f 42 59 54 45 5f 41 ine EIGHT_BYTE_A
4a420 4c 49 47 4e 4d 45 4e 54 28 58 29 20 20 20 28 28 LIGNMENT(X) ((
4a430 28 28 63 68 61 72 2a 29 28 58 29 20 2d 20 28 63 ((char*)(X) - (c
4a440 68 61 72 2a 29 30 29 26 37 29 3d 3d 30 29 0a 0a har*)0)&7)==0)..
4a450 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e ./*.** An instan
4a460 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
4a470 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 ing structure is
4a480 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 used to store t
4a490 68 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a he busy-handler.
4a4a0 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 ** callback for
4a4b0 61 20 67 69 76 65 6e 20 73 71 6c 69 74 65 20 68 a given sqlite h
4a4c0 61 6e 64 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 andle. .**.** Th
4a4d0 65 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e e sqlite.busyHan
4a4e0 64 6c 65 72 20 6d 65 6d 62 65 72 20 6f 66 20 74 dler member of t
4a4f0 68 65 20 73 71 6c 69 74 65 20 73 74 72 75 63 74 he sqlite struct
4a500 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 62 75 contains the bu
4a510 73 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 sy.** callback f
4a520 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 or the database
4a530 68 61 6e 64 6c 65 2e 20 45 61 63 68 20 70 61 67 handle. Each pag
4a540 65 72 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68 er opened via th
4a550 65 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 e sqlite.** hand
4a560 6c 65 20 69 73 20 70 61 73 73 65 64 20 61 20 70 le is passed a p
4a570 6f 69 6e 74 65 72 20 74 6f 20 73 71 6c 69 74 65 ointer to sqlite
4a580 2e 62 75 73 79 48 61 6e 64 6c 65 72 2e 20 54 68 .busyHandler. Th
4a590 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a e busy-handler.*
4a5a0 2a 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 75 * callback is cu
4a5b0 72 72 65 6e 74 6c 79 20 69 6e 76 6f 6b 65 64 20 rrently invoked
4a5c0 6f 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e only from within
4a5d0 20 70 61 67 65 72 2e 63 2e 0a 2a 2f 0a 74 79 70 pager.c..*/.typ
4a5e0 65 64 65 66 20 73 74 72 75 63 74 20 42 75 73 79 edef struct Busy
4a5f0 48 61 6e 64 6c 65 72 20 42 75 73 79 48 61 6e 64 Handler BusyHand
4a600 6c 65 72 3b 0a 73 74 72 75 63 74 20 42 75 73 79 ler;.struct Busy
4a610 48 61 6e 64 6c 65 72 20 7b 0a 20 20 69 6e 74 20 Handler {. int
4a620 28 2a 78 46 75 6e 63 29 28 76 6f 69 64 20 2a 2c (*xFunc)(void *,
4a630 69 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 62 75 int); /* The bu
4a640 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 sy callback */.
4a650 20 76 6f 69 64 20 2a 70 41 72 67 3b 20 20 20 20 void *pArg;
4a660 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
4a670 69 72 73 74 20 61 72 67 20 74 6f 20 62 75 73 79 irst arg to busy
4a680 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 callback */. i
4a690 6e 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20 20 nt nBusy;
4a6a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 /* Inc
4a6b0 72 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61 remented with ea
4a6c0 63 68 20 62 75 73 79 20 63 61 6c 6c 20 2a 2f 0a ch busy call */.
4a6d0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f };../*.** Name o
4a6e0 66 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 74 f the master dat
4a6f0 61 62 61 73 65 20 74 61 62 6c 65 2e 20 20 54 68 abase table. Th
4a700 65 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 e master databas
4a710 65 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20 e table.** is a
4a720 73 70 65 63 69 61 6c 20 74 61 62 6c 65 20 74 68 special table th
4a730 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d at holds the nam
4a740 65 73 20 61 6e 64 20 61 74 74 72 69 62 75 74 65 es and attribute
4a750 73 20 6f 66 20 61 6c 6c 0a 2a 2a 20 75 73 65 72 s of all.** user
4a760 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 tables and indi
4a770 63 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ces..*/.#define
4a780 4d 41 53 54 45 52 5f 4e 41 4d 45 20 20 20 20 20 MASTER_NAME
4a790 20 20 22 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 "sqlite_master
4a7a0 22 0a 23 64 65 66 69 6e 65 20 54 45 4d 50 5f 4d ".#define TEMP_M
4a7b0 41 53 54 45 52 5f 4e 41 4d 45 20 20 22 73 71 6c ASTER_NAME "sql
4a7c0 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22 ite_temp_master"
4a7d0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 ../*.** The root
4a7e0 2d 70 61 67 65 20 6f 66 20 74 68 65 20 6d 61 73 -page of the mas
4a7f0 74 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 ter database tab
4a800 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d le..*/.#define M
4a810 41 53 54 45 52 5f 52 4f 4f 54 20 20 20 20 20 20 ASTER_ROOT
4a820 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 1../*.** The na
4a830 6d 65 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 me of the schema
4a840 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 table..*/.#defi
4a850 6e 65 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 ne SCHEMA_TABLE(
4a860 78 29 20 20 28 28 21 4f 4d 49 54 5f 54 45 4d 50 x) ((!OMIT_TEMP
4a870 44 42 29 26 26 28 78 3d 3d 31 29 3f 54 45 4d 50 DB)&&(x==1)?TEMP
4a880 5f 4d 41 53 54 45 52 5f 4e 41 4d 45 3a 4d 41 53 _MASTER_NAME:MAS
4a890 54 45 52 5f 4e 41 4d 45 29 0a 0a 2f 2a 0a 2a 2a TER_NAME)../*.**
4a8a0 20 41 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 6d A convenience m
4a8b0 61 63 72 6f 20 74 68 61 74 20 72 65 74 75 72 6e acro that return
4a8c0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
4a8d0 65 6c 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 61 elements in.** a
4a8e0 6e 20 61 72 72 61 79 2e 0a 2a 2f 0a 23 64 65 66 n array..*/.#def
4a8f0 69 6e 65 20 41 72 72 61 79 53 69 7a 65 28 58 29 ine ArraySize(X)
4a900 20 20 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f ((int)(sizeo
4a910 66 28 58 29 2f 73 69 7a 65 6f 66 28 58 5b 30 5d f(X)/sizeof(X[0]
4a920 29 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 )))../*.** The f
4a930 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 61 ollowing value a
4a940 73 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 6d s a destructor m
4a950 65 61 6e 73 20 74 6f 20 75 73 65 20 73 71 6c 69 eans to use sqli
4a960 74 65 33 44 62 46 72 65 65 28 29 2e 0a 2a 2a 20 te3DbFree()..**
4a970 54 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65 72 This is an inter
4a980 6e 61 6c 20 65 78 74 65 6e 73 69 6f 6e 20 74 6f nal extension to
4a990 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 20 61 SQLITE_STATIC a
4a9a0 6e 64 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 nd SQLITE_TRANSI
4a9b0 45 4e 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ENT..*/.#define
4a9c0 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 20 SQLITE_DYNAMIC
4a9d0 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 ((sqlite3_destr
4a9e0 75 63 74 6f 72 5f 74 79 70 65 29 73 71 6c 69 74 uctor_type)sqlit
4a9f0 65 33 44 62 46 72 65 65 29 0a 0a 2f 2a 0a 2a 2a e3DbFree)../*.**
4aa00 20 57 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4d 49 When SQLITE_OMI
4aa10 54 5f 57 53 44 20 69 73 20 64 65 66 69 6e 65 64 T_WSD is defined
4aa20 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 , it means that
4aa30 74 68 65 20 74 61 72 67 65 74 20 70 6c 61 74 66 the target platf
4aa40 6f 72 6d 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 orm does.** not
4aa50 73 75 70 70 6f 72 74 20 57 72 69 74 61 62 6c 65 support Writable
4aa60 20 53 74 61 74 69 63 20 44 61 74 61 20 28 57 53 Static Data (WS
4aa70 44 29 20 73 75 63 68 20 61 73 20 67 6c 6f 62 61 D) such as globa
4aa80 6c 20 61 6e 64 20 73 74 61 74 69 63 20 76 61 72 l and static var
4aa90 69 61 62 6c 65 73 2e 0a 2a 2a 20 41 6c 6c 20 76 iables..** All v
4aaa0 61 72 69 61 62 6c 65 73 20 6d 75 73 74 20 65 69 ariables must ei
4aab0 74 68 65 72 20 62 65 20 6f 6e 20 74 68 65 20 73 ther be on the s
4aac0 74 61 63 6b 20 6f 72 20 64 79 6e 61 6d 69 63 61 tack or dynamica
4aad0 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 66 72 lly allocated fr
4aae0 6f 6d 0a 2a 2a 20 74 68 65 20 68 65 61 70 2e 20 om.** the heap.
4aaf0 20 57 68 65 6e 20 57 53 44 20 69 73 20 75 6e 73 When WSD is uns
4ab00 75 70 70 6f 72 74 65 64 2c 20 74 68 65 20 76 61 upported, the va
4ab10 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74 69 riable declarati
4ab20 6f 6e 73 20 73 63 61 74 74 65 72 65 64 0a 2a 2a ons scattered.**
4ab30 20 74 68 72 6f 75 67 68 6f 75 74 20 74 68 65 20 throughout the
4ab40 53 51 4c 69 74 65 20 63 6f 64 65 20 6d 75 73 74 SQLite code must
4ab50 20 62 65 63 6f 6d 65 20 63 6f 6e 73 74 61 6e 74 become constant
4ab60 73 20 69 6e 73 74 65 61 64 2e 20 20 54 68 65 20 s instead. The
4ab70 53 51 4c 49 54 45 5f 57 53 44 0a 2a 2a 20 6d 61 SQLITE_WSD.** ma
4ab80 63 72 6f 20 69 73 20 75 73 65 64 20 66 6f 72 20 cro is used for
4ab90 74 68 69 73 20 70 75 72 70 6f 73 65 2e 20 20 41 this purpose. A
4aba0 6e 64 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65 nd instead of re
4abb0 66 65 72 65 6e 63 69 6e 67 20 74 68 65 20 76 61 ferencing the va
4abc0 72 69 61 62 6c 65 0a 2a 2a 20 64 69 72 65 63 74 riable.** direct
4abd0 6c 79 2c 20 77 65 20 75 73 65 20 69 74 73 20 63 ly, we use its c
4abe0 6f 6e 73 74 61 6e 74 20 61 73 20 61 20 6b 65 79 onstant as a key
4abf0 20 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 65 20 72 to lookup the r
4ac00 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65 un-time allocate
4ac10 64 0a 2a 2a 20 62 75 66 66 65 72 20 74 68 61 74 d.** buffer that
4ac20 20 68 6f 6c 64 73 20 72 65 61 6c 20 76 61 72 69 holds real vari
4ac30 61 62 6c 65 2e 20 20 54 68 65 20 63 6f 6e 73 74 able. The const
4ac40 61 6e 74 20 69 73 20 61 6c 73 6f 20 74 68 65 20 ant is also the
4ac50 69 6e 69 74 69 61 6c 69 7a 65 72 0a 2a 2a 20 66 initializer.** f
4ac60 6f 72 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20 or the run-time
4ac70 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 allocated buffer
4ac80 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 75 ..**.** In the u
4ac90 73 75 61 6c 20 63 61 73 65 20 77 68 65 72 65 20 sual case where
4aca0 57 53 44 20 69 73 20 73 75 70 70 6f 72 74 65 64 WSD is supported
4acb0 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 57 53 44 , the SQLITE_WSD
4acc0 20 61 6e 64 20 47 4c 4f 42 41 4c 0a 2a 2a 20 6d and GLOBAL.** m
4acd0 61 63 72 6f 73 20 62 65 63 6f 6d 65 20 6e 6f 2d acros become no-
4ace0 6f 70 73 20 61 6e 64 20 68 61 76 65 20 7a 65 72 ops and have zer
4acf0 6f 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d o performance im
4ad00 70 61 63 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 pact..*/.#ifdef
4ad10 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a SQLITE_OMIT_WSD.
4ad20 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 #define SQLITE
4ad30 5f 57 53 44 20 63 6f 6e 73 74 0a 20 20 23 64 65 _WSD const. #de
4ad40 66 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c 76 29 fine GLOBAL(t,v)
4ad50 20 28 2a 28 74 2a 29 73 71 6c 69 74 65 33 5f 77 (*(t*)sqlite3_w
4ad60 73 64 5f 66 69 6e 64 28 28 76 6f 69 64 2a 29 26 sd_find((void*)&
4ad70 28 76 29 2c 20 73 69 7a 65 6f 66 28 76 29 29 29 (v), sizeof(v)))
4ad80 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 . #define sqlit
4ad90 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 47 e3GlobalConfig G
4ada0 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 53 71 6c LOBAL(struct Sql
4adb0 69 74 65 33 43 6f 6e 66 69 67 2c 20 73 71 6c 69 ite3Config, sqli
4adc0 74 65 33 43 6f 6e 66 69 67 29 0a 53 51 4c 49 54 te3Config).SQLIT
4add0 45 5f 41 50 49 20 20 20 69 6e 74 20 73 71 6c 69 E_API int sqli
4ade0 74 65 33 5f 77 73 64 5f 69 6e 69 74 28 69 6e 74 te3_wsd_init(int
4adf0 20 4e 2c 20 69 6e 74 20 4a 29 3b 0a 53 51 4c 49 N, int J);.SQLI
4ae00 54 45 5f 41 50 49 20 20 20 76 6f 69 64 20 2a 73 TE_API void *s
4ae10 71 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e 64 28 qlite3_wsd_find(
4ae20 76 6f 69 64 20 2a 4b 2c 20 69 6e 74 20 4c 29 3b void *K, int L);
4ae30 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 .#else. #define
4ae40 20 53 51 4c 49 54 45 5f 57 53 44 20 0a 20 20 23 SQLITE_WSD . #
4ae50 64 65 66 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c define GLOBAL(t,
4ae60 76 29 20 76 0a 20 20 23 64 65 66 69 6e 65 20 73 v) v. #define s
4ae70 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
4ae80 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 ig sqlite3Config
4ae90 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
4aea0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 he following mac
4aeb0 72 6f 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 ros are used to
4aec0 73 75 70 70 72 65 73 73 20 63 6f 6d 70 69 6c 65 suppress compile
4aed0 72 20 77 61 72 6e 69 6e 67 73 20 61 6e 64 20 74 r warnings and t
4aee0 6f 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 63 6c 65 o.** make it cle
4aef0 61 72 20 74 6f 20 68 75 6d 61 6e 20 72 65 61 64 ar to human read
4af00 65 72 73 20 77 68 65 6e 20 61 20 66 75 6e 63 74 ers when a funct
4af10 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 ion parameter is
4af20 20 64 65 6c 69 62 65 72 61 74 65 6c 79 20 0a 2a deliberately .*
4af30 2a 20 6c 65 66 74 20 75 6e 75 73 65 64 20 77 69 * left unused wi
4af40 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 thin the body of
4af50 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 69 a function. Thi
4af60 73 20 75 73 75 61 6c 6c 79 20 68 61 70 70 65 6e s usually happen
4af70 73 20 77 68 65 6e 0a 2a 2a 20 61 20 66 75 6e 63 s when.** a func
4af80 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 76 tion is called v
4af90 69 61 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f ia a function po
4afa0 69 6e 74 65 72 2e 20 46 6f 72 20 65 78 61 6d 70 inter. For examp
4afb0 6c 65 20 74 68 65 20 0a 2a 2a 20 69 6d 70 6c 65 le the .** imple
4afc0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 mentation of an
4afd0 53 51 4c 20 61 67 67 72 65 67 61 74 65 20 73 74 SQL aggregate st
4afe0 65 70 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 ep callback may
4aff0 6e 6f 74 20 75 73 65 20 74 68 65 0a 2a 2a 20 70 not use the.** p
4b000 61 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61 74 arameter indicat
4b010 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ing the number o
4b020 66 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 f arguments pass
4b030 65 64 20 74 6f 20 74 68 65 20 61 67 67 72 65 67 ed to the aggreg
4b040 61 74 65 2c 0a 2a 2a 20 69 66 20 69 74 20 6b 6e ate,.** if it kn
4b050 6f 77 73 20 74 68 61 74 20 74 68 69 73 20 69 73 ows that this is
4b060 20 65 6e 66 6f 72 63 65 64 20 65 6c 73 65 77 68 enforced elsewh
4b070 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 ere..**.** When
4b080 61 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d a function param
4b090 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65 64 eter is not used
4b0a0 20 61 74 20 61 6c 6c 20 77 69 74 68 69 6e 20 74 at all within t
4b0b0 68 65 20 62 6f 64 79 20 6f 66 20 61 20 66 75 6e he body of a fun
4b0c0 63 74 69 6f 6e 2c 0a 2a 2a 20 69 74 20 69 73 20 ction,.** it is
4b0d0 67 65 6e 65 72 61 6c 6c 79 20 6e 61 6d 65 64 20 generally named
4b0e0 22 4e 6f 74 55 73 65 64 22 20 6f 72 20 22 4e 6f "NotUsed" or "No
4b0f0 74 55 73 65 64 32 22 20 74 6f 20 6d 61 6b 65 20 tUsed2" to make
4b100 74 68 69 6e 67 73 20 65 76 65 6e 20 63 6c 65 61 things even clea
4b110 72 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c rer..** However,
4b120 20 74 68 65 73 65 20 6d 61 63 72 6f 73 20 6d 61 these macros ma
4b130 79 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 74 y also be used t
4b140 6f 20 73 75 70 70 72 65 73 73 20 77 61 72 6e 69 o suppress warni
4b150 6e 67 73 20 72 65 6c 61 74 65 64 20 74 6f 0a 2a ngs related to.*
4b160 2a 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 * parameters tha
4b170 74 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 t may or may not
4b180 20 62 65 20 75 73 65 64 20 64 65 70 65 6e 64 69 be used dependi
4b190 6e 67 20 6f 6e 20 63 6f 6d 70 69 6c 61 74 69 6f ng on compilatio
4b1a0 6e 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 20 46 6f n options..** Fo
4b1b0 72 20 65 78 61 6d 70 6c 65 20 74 68 6f 73 65 20 r example those
4b1c0 70 61 72 61 6d 65 74 65 72 73 20 6f 6e 6c 79 20 parameters only
4b1d0 75 73 65 64 20 69 6e 20 61 73 73 65 72 74 28 29 used in assert()
4b1e0 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 6e 20 statements. In
4b1f0 74 68 65 73 65 0a 2a 2a 20 63 61 73 65 73 20 74 these.** cases t
4b200 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 he parameters ar
4b210 65 20 6e 61 6d 65 64 20 61 73 20 70 65 72 20 74 e named as per t
4b220 68 65 20 75 73 75 61 6c 20 63 6f 6e 76 65 6e 74 he usual convent
4b230 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ions..*/.#define
4b240 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
4b250 52 28 78 29 20 28 76 6f 69 64 29 28 78 29 0a 23 R(x) (void)(x).#
4b260 64 65 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41 define UNUSED_PA
4b270 52 41 4d 45 54 45 52 32 28 78 2c 79 29 20 55 4e RAMETER2(x,y) UN
4b280 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 78 USED_PARAMETER(x
4b290 29 2c 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 ),UNUSED_PARAMET
4b2a0 45 52 28 79 29 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 ER(y)../*.** For
4b2b0 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 73 20 ward references
4b2c0 74 6f 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2f to structures.*/
4b2d0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b2e0 41 67 67 49 6e 66 6f 20 41 67 67 49 6e 66 6f 3b AggInfo AggInfo;
4b2f0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b300 41 75 74 68 43 6f 6e 74 65 78 74 20 41 75 74 68 AuthContext Auth
4b310 43 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64 65 66 Context;.typedef
4b320 20 73 74 72 75 63 74 20 41 75 74 6f 69 6e 63 49 struct AutoincI
4b330 6e 66 6f 20 41 75 74 6f 69 6e 63 49 6e 66 6f 3b nfo AutoincInfo;
4b340 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b350 42 69 74 76 65 63 20 42 69 74 76 65 63 3b 0a 74 Bitvec Bitvec;.t
4b360 79 70 65 64 65 66 20 73 74 72 75 63 74 20 52 6f ypedef struct Ro
4b370 77 53 65 74 20 52 6f 77 53 65 74 3b 0a 74 79 70 wSet RowSet;.typ
4b380 65 64 65 66 20 73 74 72 75 63 74 20 43 6f 6c 6c edef struct Coll
4b390 53 65 71 20 43 6f 6c 6c 53 65 71 3b 0a 74 79 70 Seq CollSeq;.typ
4b3a0 65 64 65 66 20 73 74 72 75 63 74 20 43 6f 6c 75 edef struct Colu
4b3b0 6d 6e 20 43 6f 6c 75 6d 6e 3b 0a 74 79 70 65 64 mn Column;.typed
4b3c0 65 66 20 73 74 72 75 63 74 20 44 62 20 44 62 3b ef struct Db Db;
4b3d0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b3e0 53 63 68 65 6d 61 20 53 63 68 65 6d 61 3b 0a 74 Schema Schema;.t
4b3f0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78 ypedef struct Ex
4b400 70 72 20 45 78 70 72 3b 0a 74 79 70 65 64 65 66 pr Expr;.typedef
4b410 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 struct ExprList
4b420 20 45 78 70 72 4c 69 73 74 3b 0a 74 79 70 65 64 ExprList;.typed
4b430 65 66 20 73 74 72 75 63 74 20 45 78 70 72 53 70 ef struct ExprSp
4b440 61 6e 20 45 78 70 72 53 70 61 6e 3b 0a 74 79 70 an ExprSpan;.typ
4b450 65 64 65 66 20 73 74 72 75 63 74 20 46 4b 65 79 edef struct FKey
4b460 20 46 4b 65 79 3b 0a 74 79 70 65 64 65 66 20 73 FKey;.typedef s
4b470 74 72 75 63 74 20 46 75 6e 63 44 65 66 20 46 75 truct FuncDef Fu
4b480 6e 63 44 65 66 3b 0a 74 79 70 65 64 65 66 20 73 ncDef;.typedef s
4b490 74 72 75 63 74 20 46 75 6e 63 44 65 66 48 61 73 truct FuncDefHas
4b4a0 68 20 46 75 6e 63 44 65 66 48 61 73 68 3b 0a 74 h FuncDefHash;.t
4b4b0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 64 ypedef struct Id
4b4c0 4c 69 73 74 20 49 64 4c 69 73 74 3b 0a 74 79 70 List IdList;.typ
4b4d0 65 64 65 66 20 73 74 72 75 63 74 20 49 6e 64 65 edef struct Inde
4b4e0 78 20 49 6e 64 65 78 3b 0a 74 79 70 65 64 65 66 x Index;.typedef
4b4f0 20 73 74 72 75 63 74 20 49 6e 64 65 78 53 61 6d struct IndexSam
4b500 70 6c 65 20 49 6e 64 65 78 53 61 6d 70 6c 65 3b ple IndexSample;
4b510 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b520 4b 65 79 43 6c 61 73 73 20 4b 65 79 43 6c 61 73 KeyClass KeyClas
4b530 73 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 s;.typedef struc
4b540 74 20 4b 65 79 49 6e 66 6f 20 4b 65 79 49 6e 66 t KeyInfo KeyInf
4b550 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 o;.typedef struc
4b560 74 20 4c 6f 6f 6b 61 73 69 64 65 20 4c 6f 6f 6b t Lookaside Look
4b570 61 73 69 64 65 3b 0a 74 79 70 65 64 65 66 20 73 aside;.typedef s
4b580 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 53 truct LookasideS
4b590 6c 6f 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f lot LookasideSlo
4b5a0 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 t;.typedef struc
4b5b0 74 20 4d 6f 64 75 6c 65 20 4d 6f 64 75 6c 65 3b t Module Module;
4b5c0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b5d0 4e 61 6d 65 43 6f 6e 74 65 78 74 20 4e 61 6d 65 NameContext Name
4b5e0 43 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64 65 66 Context;.typedef
4b5f0 20 73 74 72 75 63 74 20 50 61 72 73 65 20 50 61 struct Parse Pa
4b600 72 73 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 rse;.typedef str
4b610 75 63 74 20 53 61 76 65 70 6f 69 6e 74 20 53 61 uct Savepoint Sa
4b620 76 65 70 6f 69 6e 74 3b 0a 74 79 70 65 64 65 66 vepoint;.typedef
4b630 20 73 74 72 75 63 74 20 53 65 6c 65 63 74 20 53 struct Select S
4b640 65 6c 65 63 74 3b 0a 74 79 70 65 64 65 66 20 73 elect;.typedef s
4b650 74 72 75 63 74 20 53 72 63 4c 69 73 74 20 53 72 truct SrcList Sr
4b660 63 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 cList;.typedef s
4b670 74 72 75 63 74 20 53 74 72 41 63 63 75 6d 20 53 truct StrAccum S
4b680 74 72 41 63 63 75 6d 3b 0a 74 79 70 65 64 65 66 trAccum;.typedef
4b690 20 73 74 72 75 63 74 20 54 61 62 6c 65 20 54 61 struct Table Ta
4b6a0 62 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ble;.typedef str
4b6b0 75 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20 54 61 uct TableLock Ta
4b6c0 62 6c 65 4c 6f 63 6b 3b 0a 74 79 70 65 64 65 66 bleLock;.typedef
4b6d0 20 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 54 6f struct Token To
4b6e0 6b 65 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ken;.typedef str
4b6f0 75 63 74 20 54 72 69 67 67 65 72 50 72 67 20 54 uct TriggerPrg T
4b700 72 69 67 67 65 72 50 72 67 3b 0a 74 79 70 65 64 riggerPrg;.typed
4b710 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 ef struct Trigge
4b720 72 53 74 65 70 20 54 72 69 67 67 65 72 53 74 65 rStep TriggerSte
4b730 70 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 p;.typedef struc
4b740 74 20 54 72 69 67 67 65 72 20 54 72 69 67 67 65 t Trigger Trigge
4b750 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 r;.typedef struc
4b760 74 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 t UnpackedRecord
4b770 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 3b UnpackedRecord;
4b780 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b790 56 54 61 62 6c 65 20 56 54 61 62 6c 65 3b 0a 74 VTable VTable;.t
4b7a0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57 61 ypedef struct Wa
4b7b0 6c 6b 65 72 20 57 61 6c 6b 65 72 3b 0a 74 79 70 lker Walker;.typ
4b7c0 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72 edef struct Wher
4b7d0 65 50 6c 61 6e 20 57 68 65 72 65 50 6c 61 6e 3b ePlan WherePlan;
4b7e0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b7f0 57 68 65 72 65 49 6e 66 6f 20 57 68 65 72 65 49 WhereInfo WhereI
4b800 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 nfo;.typedef str
4b810 75 63 74 20 57 68 65 72 65 4c 65 76 65 6c 20 57 uct WhereLevel W
4b820 68 65 72 65 4c 65 76 65 6c 3b 0a 0a 2f 2a 0a 2a hereLevel;../*.*
4b830 2a 20 44 65 66 65 72 20 73 6f 75 72 63 69 6e 67 * Defer sourcing
4b840 20 76 64 62 65 2e 68 20 61 6e 64 20 62 74 72 65 vdbe.h and btre
4b850 65 2e 68 20 75 6e 74 69 6c 20 61 66 74 65 72 20 e.h until after
4b860 74 68 65 20 22 75 38 22 20 61 6e 64 20 0a 2a 2a the "u8" and .**
4b870 20 22 42 75 73 79 48 61 6e 64 6c 65 72 22 20 74 "BusyHandler" t
4b880 79 70 65 64 65 66 73 2e 20 76 64 62 65 2e 68 20 ypedefs. vdbe.h
4b890 61 6c 73 6f 20 72 65 71 75 69 72 65 73 20 61 20 also requires a
4b8a0 66 65 77 20 6f 66 20 74 68 65 20 6f 70 61 71 75 few of the opaqu
4b8b0 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 79 70 e.** pointer typ
4b8c0 65 73 20 28 69 2e 65 2e 20 46 75 6e 63 44 65 66 es (i.e. FuncDef
4b8d0 29 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 2e ) defined above.
4b8e0 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .*/./***********
4b8f0 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 62 74 72 65 *** Include btre
4b900 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c e.h in the middl
4b910 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 e of sqliteInt.h
4b920 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
4b930 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
4b940 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 *** Begin file b
4b950 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a tree.h *********
4b960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b980 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 **/./*.** 2001 S
4b990 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a eptember 15.**.*
4b9a0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
4b9b0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
4b9c0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
4b9d0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
4b9e0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
4b9f0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
4ba00 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
4ba10 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
4ba20 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
4ba30 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
4ba40 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
4ba50 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
4ba60 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
4ba70 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
4ba80 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
4ba90 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
4baa0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
4bab0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
4bac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4bad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4bae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4baf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
4bb00 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 is header file d
4bb10 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 efines the inter
4bb20 66 61 63 65 20 74 68 61 74 20 74 68 65 20 73 71 face that the sq
4bb30 6c 69 74 65 20 42 2d 54 72 65 65 20 66 69 6c 65 lite B-Tree file
4bb40 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 20 .** subsystem.
4bb50 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 See comments in
4bb60 74 68 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20 the source code
4bb70 66 6f 72 20 61 20 64 65 74 61 69 6c 65 64 20 64 for a detailed d
4bb80 65 73 63 72 69 70 74 69 6f 6e 0a 2a 2a 20 6f 66 escription.** of
4bb90 20 77 68 61 74 20 65 61 63 68 20 69 6e 74 65 72 what each inter
4bba0 66 61 63 65 20 72 6f 75 74 69 6e 65 20 64 6f 65 face routine doe
4bbb0 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 s..**.** @(#) $I
4bbc0 64 3a 20 62 74 72 65 65 2e 68 2c 76 20 31 2e 31 d: btree.h,v 1.1
4bbd0 32 30 20 32 30 30 39 2f 30 37 2f 32 32 20 30 30 20 2009/07/22 00
4bbe0 3a 33 35 3a 32 34 20 64 72 68 20 45 78 70 20 24 :35:24 drh Exp $
4bbf0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 42 54 52 .*/.#ifndef _BTR
4bc00 45 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 42 EE_H_.#define _B
4bc10 54 52 45 45 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f TREE_H_../* TODO
4bc20 3a 20 54 68 69 73 20 64 65 66 69 6e 69 74 69 6f : This definitio
4bc30 6e 20 69 73 20 6a 75 73 74 20 69 6e 63 6c 75 64 n is just includ
4bc40 65 64 20 73 6f 20 6f 74 68 65 72 20 6d 6f 64 75 ed so other modu
4bc50 6c 65 73 20 63 6f 6d 70 69 6c 65 2e 20 49 74 0a les compile. It.
4bc60 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 ** needs to be r
4bc70 65 76 69 73 69 74 65 64 2e 0a 2a 2f 0a 23 64 65 evisited..*/.#de
4bc80 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 42 54 fine SQLITE_N_BT
4bc90 52 45 45 5f 4d 45 54 41 20 31 30 0a 0a 2f 2a 0a REE_META 10../*.
4bca0 2a 2a 20 49 66 20 64 65 66 69 6e 65 64 20 61 73 ** If defined as
4bcb0 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d non-zero, auto-
4bcc0 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 vacuum is enable
4bcd0 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20 4f 74 d by default. Ot
4bce0 68 65 72 77 69 73 65 0a 2a 2a 20 69 74 20 6d 75 herwise.** it mu
4bcf0 73 74 20 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 st be turned on
4bd00 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73 for each databas
4bd10 65 20 75 73 69 6e 67 20 22 50 52 41 47 4d 41 20 e using "PRAGMA
4bd20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 31 22 auto_vacuum = 1"
4bd30 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
4bd40 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f ITE_DEFAULT_AUTO
4bd50 56 41 43 55 55 4d 0a 20 20 23 64 65 66 69 6e 65 VACUUM. #define
4bd60 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
4bd70 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e AUTOVACUUM 0.#en
4bd80 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 42 54 52 dif..#define BTR
4bd90 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f EE_AUTOVACUUM_NO
4bda0 4e 45 20 30 20 20 20 20 20 20 20 20 2f 2a 20 44 NE 0 /* D
4bdb0 6f 20 6e 6f 74 20 64 6f 20 61 75 74 6f 2d 76 61 o not do auto-va
4bdc0 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 cuum */.#define
4bdd0 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d BTREE_AUTOVACUUM
4bde0 5f 46 55 4c 4c 20 31 20 20 20 20 20 20 20 20 2f _FULL 1 /
4bdf0 2a 20 44 6f 20 66 75 6c 6c 20 61 75 74 6f 2d 76 * Do full auto-v
4be00 61 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 acuum */.#define
4be10 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 BTREE_AUTOVACUU
4be20 4d 5f 49 4e 43 52 20 32 20 20 20 20 20 20 20 20 M_INCR 2
4be30 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76 /* Incremental v
4be40 61 63 75 75 6d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 acuum */../*.**
4be50 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 Forward declarat
4be60 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72 ions of structur
4be70 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 e.*/.typedef str
4be80 75 63 74 20 42 74 72 65 65 20 42 74 72 65 65 3b uct Btree Btree;
4be90 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4bea0 42 74 43 75 72 73 6f 72 20 42 74 43 75 72 73 6f BtCursor BtCurso
4beb0 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 r;.typedef struc
4bec0 74 20 42 74 53 68 61 72 65 64 20 42 74 53 68 61 t BtShared BtSha
4bed0 72 65 64 3b 0a 74 79 70 65 64 65 66 20 73 74 72 red;.typedef str
4bee0 75 63 74 20 42 74 72 65 65 4d 75 74 65 78 41 72 uct BtreeMutexAr
4bef0 72 61 79 20 42 74 72 65 65 4d 75 74 65 78 41 72 ray BtreeMutexAr
4bf00 72 61 79 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ray;../*.** This
4bf10 20 73 74 72 75 63 74 75 72 65 20 72 65 63 6f 72 structure recor
4bf20 64 73 20 61 6c 6c 20 6f 66 20 74 68 65 20 42 74 ds all of the Bt
4bf30 72 65 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 rees that need t
4bf40 6f 20 68 6f 6c 64 0a 2a 2a 20 61 20 6d 75 74 65 o hold.** a mute
4bf50 78 20 62 65 66 6f 72 65 20 77 65 20 65 6e 74 65 x before we ente
4bf60 72 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 r sqlite3VdbeExe
4bf70 63 28 29 2e 20 20 54 68 65 20 42 74 72 65 65 73 c(). The Btrees
4bf80 20 61 72 65 0a 2a 2a 20 61 72 65 20 70 6c 61 63 are.** are plac
4bf90 65 64 20 69 6e 20 61 42 74 72 65 65 5b 5d 20 69 ed in aBtree[] i
4bfa0 6e 20 6f 72 64 65 72 20 6f 66 20 61 42 74 72 65 n order of aBtre
4bfb0 65 5b 5d 2d 3e 70 42 74 2e 20 20 54 68 61 74 20 e[]->pBt. That
4bfc0 77 61 79 2c 0a 2a 2a 20 77 65 20 63 61 6e 20 61 way,.** we can a
4bfd0 6c 77 61 79 73 20 6c 6f 63 6b 20 61 6e 64 20 75 lways lock and u
4bfe0 6e 6c 6f 63 6b 20 74 68 65 6d 20 61 6c 6c 20 71 nlock them all q
4bff0 75 69 63 6b 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63 uickly..*/.struc
4c000 74 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 t BtreeMutexArra
4c010 79 20 7b 0a 20 20 69 6e 74 20 6e 4d 75 74 65 78 y {. int nMutex
4c020 3b 0a 20 20 42 74 72 65 65 20 2a 61 42 74 72 65 ;. Btree *aBtre
4c030 65 5b 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 e[SQLITE_MAX_ATT
4c040 41 43 48 45 44 2b 31 5d 3b 0a 7d 3b 0a 0a 0a 53 ACHED+1];.};...S
4c050 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4c060 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 t sqlite3BtreeOp
4c070 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 en(. const char
4c080 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f *zFilename, /
4c090 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 * Name of databa
4c0a0 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 se file to open
4c0b0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
4c0c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
4c0d0 20 41 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 Associated data
4c0e0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
4c0f0 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42 */. Btree **ppB
4c100 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 2f 2a tree, /*
4c110 20 52 65 74 75 72 6e 20 6f 70 65 6e 20 42 74 72 Return open Btr
4c120 65 65 2a 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e ee* here */. in
4c130 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 t flags,
4c140 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
4c150 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 */. int vfsFlag
4c160 73 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a s /*
4c170 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 Flags passed th
4c180 72 6f 75 67 68 20 74 6f 20 56 46 53 20 6f 70 65 rough to VFS ope
4c190 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 54 68 65 20 n */.);../* The
4c1a0 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 flags parameter
4c1b0 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f to sqlite3BtreeO
4c1c0 70 65 6e 20 63 61 6e 20 62 65 20 74 68 65 20 62 pen can be the b
4c1d0 69 74 77 69 73 65 20 6f 72 20 6f 66 20 74 68 65 itwise or of the
4c1e0 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 .** following va
4c1f0 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 lues..**.** NOTE
4c200 3a 20 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 : These values
4c210 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65 20 63 must match the c
4c220 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 50 41 47 orresponding PAG
4c230 45 52 5f 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a ER_ values in.**
4c240 20 70 61 67 65 72 2e 68 2e 0a 2a 2f 0a 23 64 65 pager.h..*/.#de
4c250 66 69 6e 65 20 42 54 52 45 45 5f 4f 4d 49 54 5f fine BTREE_OMIT_
4c260 4a 4f 55 52 4e 41 4c 20 20 31 20 20 2f 2a 20 44 JOURNAL 1 /* D
4c270 6f 20 6e 6f 74 20 75 73 65 20 6a 6f 75 72 6e 61 o not use journa
4c280 6c 2e 20 20 4e 6f 20 61 72 67 75 6d 65 6e 74 20 l. No argument
4c290 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 */.#define BTREE
4c2a0 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 32 _NO_READLOCK 2
4c2b0 20 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c 6f /* Omit readlo
4c2c0 63 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 20 cks on readonly
4c2d0 66 69 6c 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 files */.#define
4c2e0 20 42 54 52 45 45 5f 4d 45 4d 4f 52 59 20 20 20 BTREE_MEMORY
4c2f0 20 20 20 20 20 34 20 20 2f 2a 20 49 6e 2d 6d 65 4 /* In-me
4c300 6d 6f 72 79 20 44 42 2e 20 20 4e 6f 20 61 72 67 mory DB. No arg
4c310 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 ument */.#define
4c320 20 42 54 52 45 45 5f 52 45 41 44 4f 4e 4c 59 20 BTREE_READONLY
4c330 20 20 20 20 20 38 20 20 2f 2a 20 4f 70 65 6e 20 8 /* Open
4c340 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 20 the database in
4c350 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 2a read-only mode *
4c360 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f /.#define BTREE_
4c370 52 45 41 44 57 52 49 54 45 20 20 20 20 31 36 20 READWRITE 16
4c380 20 2f 2a 20 4f 70 65 6e 20 66 6f 72 20 62 6f 74 /* Open for bot
4c390 68 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 h reading and wr
4c3a0 69 74 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 iting */.#define
4c3b0 20 42 54 52 45 45 5f 43 52 45 41 54 45 20 20 20 BTREE_CREATE
4c3c0 20 20 20 20 33 32 20 20 2f 2a 20 43 72 65 61 74 32 /* Creat
4c3d0 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 e the database i
4c3e0 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 f it does not ex
4c3f0 69 73 74 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 ist */..SQLITE_P
4c400 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4c410 65 33 42 74 72 65 65 43 6c 6f 73 65 28 42 74 72 e3BtreeClose(Btr
4c420 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ee*);.SQLITE_PRI
4c430 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4c440 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a BtreeSetCacheSiz
4c450 65 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 e(Btree*,int);.S
4c460 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4c470 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 t sqlite3BtreeSe
4c480 74 53 61 66 65 74 79 4c 65 76 65 6c 28 42 74 72 tSafetyLevel(Btr
4c490 65 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 ee*,int,int);.SQ
4c4a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4c4b0 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e sqlite3BtreeSyn
4c4c0 63 44 69 73 61 62 6c 65 64 28 42 74 72 65 65 2a cDisabled(Btree*
4c4d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4c4e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c4f0 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 42 74 eeSetPageSize(Bt
4c500 72 65 65 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 ree *p, int nPag
4c510 65 73 69 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65 esize, int nRese
4c520 72 76 65 2c 20 69 6e 74 20 65 46 69 78 29 3b 0a rve, int eFix);.
4c530 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4c540 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 nt sqlite3BtreeG
4c550 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 etPageSize(Btree
4c560 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
4c570 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
4c580 72 65 65 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 reeMaxPageCount(
4c590 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c Btree*,int);.SQL
4c5a0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4c5b0 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52 sqlite3BtreeGetR
4c5c0 65 73 65 72 76 65 28 42 74 72 65 65 2a 29 3b 0a eserve(Btree*);.
4c5d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4c5e0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 nt sqlite3BtreeS
4c5f0 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 etAutoVacuum(Btr
4c600 65 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 ee *, int);.SQLI
4c610 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4c620 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 41 75 qlite3BtreeGetAu
4c630 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a toVacuum(Btree *
4c640 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4c650 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c660 65 65 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72 eeBeginTrans(Btr
4c670 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 ee*,int);.SQLITE
4c680 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4c690 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 ite3BtreeCommitP
4c6a0 68 61 73 65 4f 6e 65 28 42 74 72 65 65 2a 2c 20 haseOne(Btree*,
4c6b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 const char *zMas
4c6c0 74 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ter);.SQLITE_PRI
4c6d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4c6e0 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
4c6f0 54 77 6f 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c Two(Btree*);.SQL
4c700 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4c710 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
4c720 69 74 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 it(Btree*);.SQLI
4c730 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4c740 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 qlite3BtreeRollb
4c750 61 63 6b 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c ack(Btree*);.SQL
4c760 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4c770 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 sqlite3BtreeBegi
4c780 6e 53 74 6d 74 28 42 74 72 65 65 2a 2c 69 6e 74 nStmt(Btree*,int
4c790 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4c7a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c7b0 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 eeCreateTable(Bt
4c7c0 72 65 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 20 ree*, int*, int
4c7d0 66 6c 61 67 73 29 3b 0a 53 51 4c 49 54 45 5f 50 flags);.SQLITE_P
4c7e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4c7f0 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 e3BtreeIsInTrans
4c800 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
4c810 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4c820 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 ite3BtreeIsInRea
4c830 64 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a dTrans(Btree*);.
4c840 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4c850 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 nt sqlite3BtreeI
4c860 73 49 6e 42 61 63 6b 75 70 28 42 74 72 65 65 2a sInBackup(Btree*
4c870 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4c880 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 E void *sqlite3B
4c890 74 72 65 65 53 63 68 65 6d 61 28 42 74 72 65 65 treeSchema(Btree
4c8a0 20 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 *, int, void(*)
4c8b0 28 76 6f 69 64 20 2a 29 29 3b 0a 53 51 4c 49 54 (void *));.SQLIT
4c8c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4c8d0 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 lite3BtreeSchema
4c8e0 4c 6f 63 6b 65 64 28 42 74 72 65 65 20 2a 70 42 Locked(Btree *pB
4c8f0 74 72 65 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 tree);.SQLITE_PR
4c900 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4c910 33 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 3BtreeLockTable(
4c920 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 69 Btree *pBtree, i
4c930 6e 74 20 69 54 61 62 2c 20 75 38 20 69 73 57 72 nt iTab, u8 isWr
4c940 69 74 65 4c 6f 63 6b 29 3b 0a 53 51 4c 49 54 45 iteLock);.SQLITE
4c950 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4c960 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 ite3BtreeSavepoi
4c970 6e 74 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c nt(Btree *, int,
4c980 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 int);..SQLITE_P
4c990 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 RIVATE const cha
4c9a0 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 r *sqlite3BtreeG
4c9b0 65 74 46 69 6c 65 6e 61 6d 65 28 42 74 72 65 65 etFilename(Btree
4c9c0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
4c9d0 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
4c9e0 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a sqlite3BtreeGetJ
4c9f0 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65 ournalname(Btree
4ca00 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
4ca10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4ca20 74 72 65 65 43 6f 70 79 46 69 6c 65 28 42 74 72 treeCopyFile(Btr
4ca30 65 65 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a ee *, Btree *);.
4ca40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4ca50 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4ca60 49 6e 63 72 56 61 63 75 75 6d 28 42 74 72 65 65 IncrVacuum(Btree
4ca70 20 2a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 *);../* The fla
4ca80 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 gs parameter to
4ca90 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 sqlite3BtreeCrea
4caa0 74 65 54 61 62 6c 65 20 63 61 6e 20 62 65 20 74 teTable can be t
4cab0 68 65 20 62 69 74 77 69 73 65 20 4f 52 0a 2a 2a he bitwise OR.**
4cac0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
4cad0 67 20 66 6c 61 67 73 3a 0a 2a 2f 0a 23 64 65 66 g flags:.*/.#def
4cae0 69 6e 65 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 ine BTREE_INTKEY
4caf0 20 20 20 20 20 31 20 20 20 20 2f 2a 20 54 61 62 1 /* Tab
4cb00 6c 65 20 68 61 73 20 6f 6e 6c 79 20 36 34 2d 62 le has only 64-b
4cb10 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 it signed intege
4cb20 72 20 6b 65 79 73 20 2a 2f 0a 23 64 65 66 69 6e r keys */.#defin
4cb30 65 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 e BTREE_ZERODATA
4cb40 20 20 20 32 20 20 20 20 2f 2a 20 54 61 62 6c 65 2 /* Table
4cb50 20 68 61 73 20 6b 65 79 73 20 6f 6e 6c 79 20 2d has keys only -
4cb60 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 23 64 65 66 no data */.#def
4cb70 69 6e 65 20 42 54 52 45 45 5f 4c 45 41 46 44 41 ine BTREE_LEAFDA
4cb80 54 41 20 20 20 34 20 20 20 20 2f 2a 20 44 61 74 TA 4 /* Dat
4cb90 61 20 73 74 6f 72 65 64 20 69 6e 20 6c 65 61 76 a stored in leav
4cba0 65 73 20 6f 6e 6c 79 2e 20 20 49 6d 70 6c 69 65 es only. Implie
4cbb0 73 20 49 4e 54 4b 45 59 20 2a 2f 0a 0a 53 51 4c s INTKEY */..SQL
4cbc0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4cbd0 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 sqlite3BtreeDrop
4cbe0 54 61 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e Table(Btree*, in
4cbf0 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 t, int*);.SQLITE
4cc00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4cc10 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 ite3BtreeClearTa
4cc20 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c ble(Btree*, int,
4cc30 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 int*);.SQLITE_P
4cc40 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
4cc50 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 te3BtreeTripAllC
4cc60 75 72 73 6f 72 73 28 42 74 72 65 65 2a 2c 20 69 ursors(Btree*, i
4cc70 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 nt);..SQLITE_PRI
4cc80 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
4cc90 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 42 74 3BtreeGetMeta(Bt
4cca0 72 65 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 ree *pBtree, int
4ccb0 20 69 64 78 2c 20 75 33 32 20 2a 70 56 61 6c 75 idx, u32 *pValu
4ccc0 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 e);.SQLITE_PRIVA
4ccd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
4cce0 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 42 74 reeUpdateMeta(Bt
4ccf0 72 65 65 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75 ree*, int idx, u
4cd00 33 32 20 76 61 6c 75 65 29 3b 0a 0a 2f 2a 0a 2a 32 value);../*.*
4cd10 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 * The second par
4cd20 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 ameter to sqlite
4cd30 33 42 74 72 65 65 47 65 74 4d 65 74 61 20 6f 72 3BtreeGetMeta or
4cd40 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 sqlite3BtreeUpd
4cd50 61 74 65 4d 65 74 61 0a 2a 2a 20 73 68 6f 75 6c ateMeta.** shoul
4cd60 64 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 d be one of the
4cd70 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 following values
4cd80 2e 20 54 68 65 20 69 6e 74 65 67 65 72 20 76 61 . The integer va
4cd90 6c 75 65 73 20 61 72 65 20 61 73 73 69 67 6e 65 lues are assigne
4cda0 64 20 0a 2a 2a 20 74 6f 20 63 6f 6e 73 74 61 6e d .** to constan
4cdb0 74 73 20 73 6f 20 74 68 61 74 20 74 68 65 20 6f ts so that the o
4cdc0 66 66 73 65 74 20 6f 66 20 74 68 65 20 63 6f 72 ffset of the cor
4cdd0 72 65 73 70 6f 6e 64 69 6e 67 20 66 69 65 6c 64 responding field
4cde0 20 69 6e 20 61 6e 0a 2a 2a 20 53 51 4c 69 74 65 in an.** SQLite
4cdf0 20 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72 database header
4ce00 20 6d 61 79 20 62 65 20 66 6f 75 6e 64 20 75 73 may be found us
4ce10 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e ing the followin
4ce20 67 20 66 6f 72 6d 75 6c 61 3a 0a 2a 2a 0a 2a 2a g formula:.**.**
4ce30 20 20 20 6f 66 66 73 65 74 20 3d 20 33 36 20 2b offset = 36 +
4ce40 20 28 69 64 78 20 2a 20 34 29 0a 2a 2a 0a 2a 2a (idx * 4).**.**
4ce50 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 For example, th
4ce60 65 20 66 72 65 65 2d 70 61 67 65 2d 63 6f 75 6e e free-page-coun
4ce70 74 20 66 69 65 6c 64 20 69 73 20 6c 6f 63 61 74 t field is locat
4ce80 65 64 20 61 74 20 62 79 74 65 20 6f 66 66 73 65 ed at byte offse
4ce90 74 20 33 36 20 6f 66 0a 2a 2a 20 74 68 65 20 64 t 36 of.** the d
4cea0 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 65 61 atabase file hea
4ceb0 64 65 72 2e 20 54 68 65 20 69 6e 63 72 2d 76 61 der. The incr-va
4cec0 63 75 75 6d 2d 66 6c 61 67 20 66 69 65 6c 64 20 cuum-flag field
4ced0 69 73 20 6c 6f 63 61 74 65 64 20 61 74 0a 2a 2a is located at.**
4cee0 20 62 79 74 65 20 6f 66 66 73 65 74 20 36 34 20 byte offset 64
4cef0 28 3d 3d 20 33 36 2b 34 2a 37 29 2e 0a 2a 2f 0a (== 36+4*7)..*/.
4cf00 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 46 52 #define BTREE_FR
4cf10 45 45 5f 50 41 47 45 5f 43 4f 55 4e 54 20 20 20 EE_PAGE_COUNT
4cf20 20 20 30 0a 23 64 65 66 69 6e 65 20 42 54 52 45 0.#define BTRE
4cf30 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e E_SCHEMA_VERSION
4cf40 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 1.#define
4cf50 42 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 BTREE_FILE_FORMA
4cf60 54 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 T 2.#def
4cf70 69 6e 65 20 42 54 52 45 45 5f 44 45 46 41 55 4c ine BTREE_DEFAUL
4cf80 54 5f 43 41 43 48 45 5f 53 49 5a 45 20 20 33 0a T_CACHE_SIZE 3.
4cf90 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4c 41 #define BTREE_LA
4cfa0 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 20 RGEST_ROOT_PAGE
4cfb0 20 20 34 0a 23 64 65 66 69 6e 65 20 42 54 52 45 4.#define BTRE
4cfc0 45 5f 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47 20 E_TEXT_ENCODING
4cfd0 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 5.#define
4cfe0 42 54 52 45 45 5f 55 53 45 52 5f 56 45 52 53 49 BTREE_USER_VERSI
4cff0 4f 4e 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 ON 6.#def
4d000 69 6e 65 20 42 54 52 45 45 5f 49 4e 43 52 5f 56 ine BTREE_INCR_V
4d010 41 43 55 55 4d 20 20 20 20 20 20 20 20 20 37 0a ACUUM 7.
4d020 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4d030 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4d040 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 2a Cursor(. Btree*
4d050 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
4d060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
4d070 2a 20 42 54 72 65 65 20 63 6f 6e 74 61 69 6e 69 * BTree containi
4d080 6e 67 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e ng table to open
4d090 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 */. int iTable
4d0a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
4d0b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
4d0c0 64 65 78 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 dex of root page
4d0d0 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 */. int wrFlag
4d0e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
4d0f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 /* 1
4d100 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 20 30 20 for writing. 0
4d110 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f for read-only */
4d120 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 . struct KeyInf
4d130 6f 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 o*,
4d140 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
4d150 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 6f 6d argument to com
4d160 70 61 72 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f pare function */
4d170 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 . BtCursor *pCu
4d180 72 73 6f 72 20 20 20 20 20 20 20 20 20 20 20 20 rsor
4d190 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 /* Space
4d1a0 20 74 6f 20 77 72 69 74 65 20 63 75 72 73 6f 72 to write cursor
4d1b0 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 3b structure */.);
4d1c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4d1d0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4d1e0 43 75 72 73 6f 72 53 69 7a 65 28 76 6f 69 64 29 CursorSize(void)
4d1f0 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ;..SQLITE_PRIVAT
4d200 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4d210 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 42 74 eeCloseCursor(Bt
4d220 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 Cursor*);.SQLITE
4d230 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4d240 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 ite3BtreeMovetoU
4d250 6e 70 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72 npacked(. BtCur
4d260 73 6f 72 2a 2c 0a 20 20 55 6e 70 61 63 6b 65 64 sor*,. Unpacked
4d270 52 65 63 6f 72 64 20 2a 70 55 6e 4b 65 79 2c 0a Record *pUnKey,.
4d280 20 20 69 36 34 20 69 6e 74 4b 65 79 2c 0a 20 20 i64 intKey,.
4d290 69 6e 74 20 62 69 61 73 2c 0a 20 20 69 6e 74 20 int bias,. int
4d2a0 2a 70 52 65 73 0a 29 3b 0a 53 51 4c 49 54 45 5f *pRes.);.SQLITE_
4d2b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4d2c0 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 te3BtreeCursorHa
4d2d0 73 4d 6f 76 65 64 28 42 74 43 75 72 73 6f 72 2a sMoved(BtCursor*
4d2e0 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f , int*);.SQLITE_
4d2f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4d300 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 42 te3BtreeDelete(B
4d310 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 tCursor*);.SQLIT
4d320 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4d330 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 lite3BtreeInsert
4d340 28 42 74 43 75 72 73 6f 72 2a 2c 20 63 6f 6e 73 (BtCursor*, cons
4d350 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 t void *pKey, i6
4d360 34 20 6e 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 4 nKey,.
4d370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d380 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 const
4d390 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 void *pData, int
4d3a0 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 nData,.
4d3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d3c0 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 5a int nZ
4d3d0 65 72 6f 2c 20 69 6e 74 20 62 69 61 73 2c 20 69 ero, int bias, i
4d3e0 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 29 3b 0a nt seekResult);.
4d3f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4d400 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 nt sqlite3BtreeF
4d410 69 72 73 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 irst(BtCursor*,
4d420 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 int *pRes);.SQLI
4d430 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4d440 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 qlite3BtreeLast(
4d450 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a BtCursor*, int *
4d460 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 pRes);.SQLITE_PR
4d470 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4d480 33 42 74 72 65 65 4e 65 78 74 28 42 74 43 75 72 3BtreeNext(BtCur
4d490 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 sor*, int *pRes)
4d4a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4d4b0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4d4c0 65 45 6f 66 28 42 74 43 75 72 73 6f 72 2a 29 3b eEof(BtCursor*);
4d4d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4d4e0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4d4f0 50 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f Previous(BtCurso
4d500 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a r*, int *pRes);.
4d510 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4d520 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b nt sqlite3BtreeK
4d530 65 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a eySize(BtCursor*
4d540 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 , i64 *pSize);.S
4d550 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4d560 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 t sqlite3BtreeKe
4d570 79 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 y(BtCursor*, u32
4d580 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 offset, u32 amt
4d590 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 , void*);.SQLITE
4d5a0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 _PRIVATE const v
4d5b0 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 oid *sqlite3Btre
4d5c0 65 4b 65 79 46 65 74 63 68 28 42 74 43 75 72 73 eKeyFetch(BtCurs
4d5d0 6f 72 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b or*, int *pAmt);
4d5e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4d5f0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
4d600 74 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 te3BtreeDataFetc
4d610 68 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 h(BtCursor*, int
4d620 20 2a 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f *pAmt);.SQLITE_
4d630 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4d640 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 te3BtreeDataSize
4d650 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 (BtCursor*, u32
4d660 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f *pSize);.SQLITE_
4d670 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4d680 74 65 33 42 74 72 65 65 44 61 74 61 28 42 74 43 te3BtreeData(BtC
4d690 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 ursor*, u32 offs
4d6a0 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 et, u32 amt, voi
4d6b0 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 d*);.SQLITE_PRIV
4d6c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
4d6d0 42 74 72 65 65 53 65 74 43 61 63 68 65 64 52 6f BtreeSetCachedRo
4d6e0 77 69 64 28 42 74 43 75 72 73 6f 72 2a 2c 20 73 wid(BtCursor*, s
4d6f0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 qlite3_int64);.S
4d700 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 QLITE_PRIVATE sq
4d710 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 lite3_int64 sqli
4d720 74 65 33 42 74 72 65 65 47 65 74 43 61 63 68 65 te3BtreeGetCache
4d730 64 52 6f 77 69 64 28 42 74 43 75 72 73 6f 72 2a dRowid(BtCursor*
4d740 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 );..SQLITE_PRIVA
4d750 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
4d760 42 74 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 BtreeIntegrityCh
4d770 65 63 6b 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 eck(Btree*, int
4d780 2a 61 52 6f 6f 74 2c 20 69 6e 74 20 6e 52 6f 6f *aRoot, int nRoo
4d790 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 t, int, int*);.S
4d7a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 74 QLITE_PRIVATE st
4d7b0 72 75 63 74 20 50 61 67 65 72 20 2a 73 71 6c 69 ruct Pager *sqli
4d7c0 74 65 33 42 74 72 65 65 50 61 67 65 72 28 42 74 te3BtreePager(Bt
4d7d0 72 65 65 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 ree*);..SQLITE_P
4d7e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4d7f0 65 33 42 74 72 65 65 50 75 74 44 61 74 61 28 42 e3BtreePutData(B
4d800 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 tCursor*, u32 of
4d810 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 fset, u32 amt, v
4d820 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 oid*);.SQLITE_PR
4d830 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
4d840 65 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 e3BtreeCacheOver
4d850 66 6c 6f 77 28 42 74 43 75 72 73 6f 72 20 2a 29 flow(BtCursor *)
4d860 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4d870 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
4d880 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74 eeClearCursor(Bt
4d890 43 75 72 73 6f 72 20 2a 29 3b 0a 0a 23 69 66 6e Cursor *);..#ifn
4d8a0 64 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 def NDEBUG.SQLIT
4d8b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4d8c0 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 lite3BtreeCursor
4d8d0 49 73 56 61 6c 69 64 28 42 74 43 75 72 73 6f 72 IsValid(BtCursor
4d8e0 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e *);.#endif..#ifn
4d8f0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
4d900 42 54 52 45 45 43 4f 55 4e 54 0a 53 51 4c 49 54 BTREECOUNT.SQLIT
4d910 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4d920 6c 69 74 65 33 42 74 72 65 65 43 6f 75 6e 74 28 lite3BtreeCount(
4d930 42 74 43 75 72 73 6f 72 20 2a 2c 20 69 36 34 20 BtCursor *, i64
4d940 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 *);.#endif..#ifd
4d950 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
4d960 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4d970 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 t sqlite3BtreeCu
4d980 72 73 6f 72 49 6e 66 6f 28 42 74 43 75 72 73 6f rsorInfo(BtCurso
4d990 72 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 29 3b 0a r*, int*, int);.
4d9a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
4d9b0 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
4d9c0 43 75 72 73 6f 72 4c 69 73 74 28 42 74 72 65 65 CursorList(Btree
4d9d0 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a *);.#endif../*.*
4d9e0 2a 20 49 66 20 77 65 20 61 72 65 20 6e 6f 74 20 * If we are not
4d9f0 75 73 69 6e 67 20 73 68 61 72 65 64 20 63 61 63 using shared cac
4da00 68 65 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 he, then there i
4da10 73 20 6e 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 s no need to.**
4da20 75 73 65 20 6d 75 74 65 78 65 73 20 74 6f 20 61 use mutexes to a
4da30 63 63 65 73 73 20 74 68 65 20 42 74 53 68 61 72 ccess the BtShar
4da40 65 64 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 ed structures.
4da50 53 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 45 So make the.** E
4da60 6e 74 65 72 20 61 6e 64 20 4c 65 61 76 65 20 70 nter and Leave p
4da70 72 6f 63 65 64 75 72 65 73 20 6e 6f 2d 6f 70 73 rocedures no-ops
4da80 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
4da90 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
4daa0 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49 CACHE.SQLITE_PRI
4dab0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
4dac0 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 42 74 te3BtreeEnter(Bt
4dad0 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ree*);.SQLITE_PR
4dae0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
4daf0 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c ite3BtreeEnterAl
4db00 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c l(sqlite3*);.#el
4db10 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 se.# define sqli
4db20 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 58 29 te3BtreeEnter(X)
4db30 20 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 .# define sqlit
4db40 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 e3BtreeEnterAll(
4db50 58 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 X).#endif..#if !
4db60 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
4db70 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
4db80 29 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 ) && SQLITE_THRE
4db90 41 44 53 41 46 45 0a 53 51 4c 49 54 45 5f 50 52 ADSAFE.SQLITE_PR
4dba0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
4dbb0 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 42 ite3BtreeLeave(B
4dbc0 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 tree*);.SQLITE_P
4dbd0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
4dbe0 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 43 lite3BtreeEnterC
4dbf0 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 ursor(BtCursor*)
4dc00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4dc10 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 void sqlite3B
4dc20 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 treeLeaveCursor(
4dc30 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 BtCursor*);.SQLI
4dc40 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
4dc50 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 d sqlite3BtreeLe
4dc60 61 76 65 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 aveAll(sqlite3*)
4dc70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4dc80 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 void sqlite3B
4dc90 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e treeMutexArrayEn
4dca0 74 65 72 28 42 74 72 65 65 4d 75 74 65 78 41 72 ter(BtreeMutexAr
4dcb0 72 61 79 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ray*);.SQLITE_PR
4dcc0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
4dcd0 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 ite3BtreeMutexAr
4dce0 72 61 79 4c 65 61 76 65 28 42 74 72 65 65 4d 75 rayLeave(BtreeMu
4dcf0 74 65 78 41 72 72 61 79 2a 29 3b 0a 53 51 4c 49 texArray*);.SQLI
4dd00 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
4dd10 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 d sqlite3BtreeMu
4dd20 74 65 78 41 72 72 61 79 49 6e 73 65 72 74 28 42 texArrayInsert(B
4dd30 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 2a 2c treeMutexArray*,
4dd40 20 42 74 72 65 65 2a 29 3b 0a 23 69 66 6e 64 65 Btree*);.#ifnde
4dd50 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 f NDEBUG. /* Th
4dd60 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 ese routines are
4dd70 20 75 73 65 64 20 69 6e 73 69 64 65 20 61 73 73 used inside ass
4dd80 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 ert() statements
4dd90 20 6f 6e 6c 79 2e 20 2a 2f 0a 53 51 4c 49 54 45 only. */.SQLITE
4dda0 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
4ddb0 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
4ddc0 4d 75 74 65 78 28 42 74 72 65 65 2a 29 3b 0a 53 Mutex(Btree*);.S
4ddd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
4dde0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4ddf0 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 HoldsAllMutexes(
4de00 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6e 64 69 sqlite3*);.#endi
4de10 66 0a 23 65 6c 73 65 0a 0a 23 20 64 65 66 69 6e f.#else..# defin
4de20 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 e sqlite3BtreeLe
4de30 61 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20 ave(X).# define
4de40 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
4de50 72 43 75 72 73 6f 72 28 58 29 0a 23 20 64 65 66 rCursor(X).# def
4de60 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 ine sqlite3Btree
4de70 4c 65 61 76 65 43 75 72 73 6f 72 28 58 29 0a 23 LeaveCursor(X).#
4de80 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 define sqlite3B
4de90 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 58 29 0a treeLeaveAll(X).
4dea0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
4deb0 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 BtreeMutexArrayE
4dec0 6e 74 65 72 28 58 29 0a 23 20 64 65 66 69 6e 65 nter(X).# define
4ded0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 sqlite3BtreeMut
4dee0 65 78 41 72 72 61 79 4c 65 61 76 65 28 58 29 0a exArrayLeave(X).
4def0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
4df00 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 BtreeMutexArrayI
4df10 6e 73 65 72 74 28 58 2c 59 29 0a 0a 23 20 64 65 nsert(X,Y)..# de
4df20 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 fine sqlite3Btre
4df30 65 48 6f 6c 64 73 4d 75 74 65 78 28 58 29 20 31 eHoldsMutex(X) 1
4df40 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
4df50 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 3BtreeHoldsAllMu
4df60 74 65 78 65 73 28 58 29 20 31 0a 23 65 6e 64 69 texes(X) 1.#endi
4df70 66 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 42 f...#endif /* _B
4df80 54 52 45 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a TREE_H_ */../***
4df90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
4dfa0 6f 66 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a of btree.h *****
4dfb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
4dfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
4dff0 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
4e000 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 left off in sqli
4e010 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
4e020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
4e030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
4e040 75 64 65 20 76 64 62 65 2e 68 20 69 6e 20 74 68 ude vdbe.h in th
4e050 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 e middle of sqli
4e060 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
4e070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
4e080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
4e090 6e 20 66 69 6c 65 20 76 64 62 65 2e 68 20 2a 2a n file vdbe.h **
4e0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
4e0d0 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 * 2001 September
4e0e0 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 15.**.** The au
4e0f0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
4e100 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
4e110 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
4e120 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
4e130 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
4e140 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
4e150 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
4e160 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
4e170 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
4e180 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
4e190 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
4e1a0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
4e1b0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
4e1c0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
4e1d0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
4e1e0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
4e1f0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
4e200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e240 2a 2a 0a 2a 2a 20 48 65 61 64 65 72 20 66 69 6c **.** Header fil
4e250 65 20 66 6f 72 20 74 68 65 20 56 69 72 74 75 61 e for the Virtua
4e260 6c 20 44 61 74 61 42 61 73 65 20 45 6e 67 69 6e l DataBase Engin
4e270 65 20 28 56 44 42 45 29 0a 2a 2a 0a 2a 2a 20 54 e (VDBE).**.** T
4e280 68 69 73 20 68 65 61 64 65 72 20 64 65 66 69 6e his header defin
4e290 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 es the interface
4e2a0 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20 to the virtual
4e2b0 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 0a database engine.
4e2c0 2a 2a 20 6f 72 20 56 44 42 45 2e 20 20 54 68 65 ** or VDBE. The
4e2d0 20 56 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74 73 VDBE implements
4e2e0 20 61 6e 20 61 62 73 74 72 61 63 74 20 6d 61 63 an abstract mac
4e2f0 68 69 6e 65 20 74 68 61 74 20 72 75 6e 73 20 61 hine that runs a
4e300 0a 2a 2a 20 73 69 6d 70 6c 65 20 70 72 6f 67 72 .** simple progr
4e310 61 6d 20 74 6f 20 61 63 63 65 73 73 20 61 6e 64 am to access and
4e320 20 6d 6f 64 69 66 79 20 74 68 65 20 75 6e 64 65 modify the unde
4e330 72 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 2e rlying database.
4e340 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 .**.** $Id: vdbe
4e350 2e 68 2c 76 20 31 2e 31 34 32 20 32 30 30 39 2f .h,v 1.142 2009/
4e360 30 37 2f 32 34 20 31 37 3a 35 38 3a 35 33 20 64 07/24 17:58:53 d
4e370 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 anielk1977 Exp $
4e380 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c .*/.#ifndef _SQL
4e390 49 54 45 5f 56 44 42 45 5f 48 5f 0a 23 64 65 66 ITE_VDBE_H_.#def
4e3a0 69 6e 65 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 ine _SQLITE_VDBE
4e3b0 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e _H_../*.** A sin
4e3c0 67 6c 65 20 56 44 42 45 20 69 73 20 61 6e 20 6f gle VDBE is an o
4e3d0 70 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20 paque structure
4e3e0 6e 61 6d 65 64 20 22 56 64 62 65 22 2e 20 20 4f named "Vdbe". O
4e3f0 6e 6c 79 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 nly routines.**
4e400 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 66 69 in the source fi
4e410 6c 65 20 73 71 6c 69 74 65 56 64 62 65 2e 63 20 le sqliteVdbe.c
4e420 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73 are allowed to s
4e430 65 65 20 74 68 65 20 69 6e 73 69 64 65 73 0a 2a ee the insides.*
4e440 2a 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 * of this struct
4e450 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ure..*/.typedef
4e460 73 74 72 75 63 74 20 56 64 62 65 20 56 64 62 65 struct Vdbe Vdbe
4e470 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d ;../*.** The nam
4e480 65 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 es of the follow
4e490 69 6e 67 20 74 79 70 65 73 20 64 65 63 6c 61 72 ing types declar
4e4a0 65 64 20 69 6e 20 76 64 62 65 49 6e 74 2e 68 20 ed in vdbeInt.h
4e4b0 61 72 65 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 are required.**
4e4c0 66 6f 72 20 74 68 65 20 56 64 62 65 4f 70 20 64 for the VdbeOp d
4e4d0 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 efinition..*/.ty
4e4e0 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 pedef struct Vdb
4e4f0 65 46 75 6e 63 20 56 64 62 65 46 75 6e 63 3b 0a eFunc VdbeFunc;.
4e500 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d typedef struct M
4e510 65 6d 20 4d 65 6d 3b 0a 74 79 70 65 64 65 66 20 em Mem;.typedef
4e520 73 74 72 75 63 74 20 53 75 62 50 72 6f 67 72 61 struct SubProgra
4e530 6d 20 53 75 62 50 72 6f 67 72 61 6d 3b 0a 0a 2f m SubProgram;../
4e540 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e *.** A single in
4e550 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65 struction of the
4e560 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
4e570 20 68 61 73 20 61 6e 20 6f 70 63 6f 64 65 0a 2a has an opcode.*
4e580 2a 20 61 6e 64 20 61 73 20 6d 61 6e 79 20 61 73 * and as many as
4e590 20 74 68 72 65 65 20 6f 70 65 72 61 6e 64 73 2e three operands.
4e5a0 20 20 54 68 65 20 69 6e 73 74 72 75 63 74 69 6f The instructio
4e5b0 6e 20 69 73 20 72 65 63 6f 72 64 65 64 0a 2a 2a n is recorded.**
4e5c0 20 61 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 as an instance
4e5d0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
4e5e0 20 73 74 72 75 63 74 75 72 65 3a 0a 2a 2f 0a 73 structure:.*/.s
4e5f0 74 72 75 63 74 20 56 64 62 65 4f 70 20 7b 0a 20 truct VdbeOp {.
4e600 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 u8 opcode;
4e610 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 /* What ope
4e620 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 ration to perfor
4e630 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 m */. signed ch
4e640 61 72 20 70 34 74 79 70 65 3b 20 2f 2a 20 4f 6e ar p4type; /* On
4e650 65 20 6f 66 20 74 68 65 20 50 34 5f 78 78 78 20 e of the P4_xxx
4e660 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 70 34 constants for p4
4e670 20 2a 2f 0a 20 20 75 38 20 6f 70 66 6c 61 67 73 */. u8 opflags
4e680 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 ; /* Not
4e690 20 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64 20 currently used
4e6a0 2a 2f 0a 20 20 75 38 20 70 35 3b 20 20 20 20 20 */. u8 p5;
4e6b0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 66 74 /* Fift
4e6c0 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 h parameter is a
4e6d0 6e 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61 n unsigned chara
4e6e0 63 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 31 cter */. int p1
4e6f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
4e700 20 46 69 72 73 74 20 6f 70 65 72 61 6e 64 20 2a First operand *
4e710 2f 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 /. int p2;
4e720 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e /* Secon
4e730 64 20 70 61 72 61 6d 65 74 65 72 20 28 6f 66 74 d parameter (oft
4e740 65 6e 20 74 68 65 20 6a 75 6d 70 20 64 65 73 74 en the jump dest
4e750 69 6e 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 69 6e ination) */. in
4e760 74 20 70 33 3b 20 20 20 20 20 20 20 20 20 20 20 t p3;
4e770 20 20 2f 2a 20 54 68 65 20 74 68 69 72 64 20 70 /* The third p
4e780 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 75 6e arameter */. un
4e790 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20 ion {
4e7a0 20 20 2f 2a 20 66 6f 75 72 74 68 20 70 61 72 61 /* fourth para
4e7b0 6d 65 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 meter */. int
4e7c0 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
4e7d0 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 /* Integer v
4e7e0 61 6c 75 65 20 69 66 20 70 34 74 79 70 65 3d 3d alue if p4type==
4e7f0 50 34 5f 49 4e 54 33 32 20 2a 2f 0a 20 20 20 20 P4_INT32 */.
4e800 76 6f 69 64 20 2a 70 3b 20 20 20 20 20 20 20 20 void *p;
4e810 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 69 /* Generi
4e820 63 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 c pointer */.
4e830 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 char *z;
4e840 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
4e850 65 72 20 74 6f 20 64 61 74 61 20 66 6f 72 20 73 er to data for s
4e860 74 72 69 6e 67 20 28 63 68 61 72 20 61 72 72 61 tring (char arra
4e870 79 29 20 74 79 70 65 73 20 2a 2f 0a 20 20 20 20 y) types */.
4e880 69 36 34 20 2a 70 49 36 34 3b 20 20 20 20 20 20 i64 *pI64;
4e890 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 /* Used w
4e8a0 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 hen p4type is P4
4e8b0 5f 49 4e 54 36 34 20 2a 2f 0a 20 20 20 20 64 6f _INT64 */. do
4e8c0 75 62 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 uble *pReal;
4e8d0 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 /* Used whe
4e8e0 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 52 n p4type is P4_R
4e8f0 45 41 4c 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 EAL */. FuncD
4e900 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 ef *pFunc;
4e910 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 /* Used when p
4e920 34 74 79 70 65 20 69 73 20 50 34 5f 46 55 4e 43 4type is P4_FUNC
4e930 44 45 46 20 2a 2f 0a 20 20 20 20 56 64 62 65 46 DEF */. VdbeF
4e940 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 20 unc *pVdbeFunc;
4e950 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 /* Used when p
4e960 34 74 79 70 65 20 69 73 20 50 34 5f 56 44 42 45 4type is P4_VDBE
4e970 46 55 4e 43 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c FUNC */. Coll
4e980 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 Seq *pColl;
4e990 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 /* Used when
4e9a0 70 34 74 79 70 65 20 69 73 20 50 34 5f 43 4f 4c p4type is P4_COL
4e9b0 4c 53 45 51 20 2a 2f 0a 20 20 20 20 4d 65 6d 20 LSEQ */. Mem
4e9c0 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 *pMem;
4e9d0 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 /* Used when
4e9e0 70 34 74 79 70 65 20 69 73 20 50 34 5f 4d 45 4d p4type is P4_MEM
4e9f0 20 2a 2f 0a 20 20 20 20 56 54 61 62 6c 65 20 2a */. VTable *
4ea00 70 56 74 61 62 3b 20 20 20 20 20 20 20 20 20 2f pVtab; /
4ea10 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 * Used when p4ty
4ea20 70 65 20 69 73 20 50 34 5f 56 54 41 42 20 2a 2f pe is P4_VTAB */
4ea30 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b . KeyInfo *pK
4ea40 65 79 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 55 eyInfo; /* U
4ea50 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 sed when p4type
4ea60 69 73 20 50 34 5f 4b 45 59 49 4e 46 4f 20 2a 2f is P4_KEYINFO */
4ea70 0a 20 20 20 20 69 6e 74 20 2a 61 69 3b 20 20 20 . int *ai;
4ea80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 /* U
4ea90 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 sed when p4type
4eaa0 69 73 20 50 34 5f 49 4e 54 41 52 52 41 59 20 2a is P4_INTARRAY *
4eab0 2f 0a 20 20 20 20 53 75 62 50 72 6f 67 72 61 6d /. SubProgram
4eac0 20 2a 70 50 72 6f 67 72 61 6d 3b 20 20 2f 2a 20 *pProgram; /*
4ead0 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 Used when p4type
4eae0 20 69 73 20 50 34 5f 53 55 42 50 52 4f 47 52 41 is P4_SUBPROGRA
4eaf0 4d 20 2a 2f 0a 20 20 7d 20 70 34 3b 0a 23 69 66 M */. } p4;.#if
4eb00 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
4eb10 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d 65 6e . char *zCommen
4eb20 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 t; /* C
4eb30 6f 6d 6d 65 6e 74 20 74 6f 20 69 6d 70 72 6f 76 omment to improv
4eb40 65 20 72 65 61 64 61 62 69 6c 69 74 79 20 2a 2f e readability */
4eb50 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 56 .#endif.#ifdef V
4eb60 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 69 6e DBE_PROFILE. in
4eb70 74 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 t cnt;
4eb80 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
4eb90 20 6f 66 20 74 69 6d 65 73 20 74 68 69 73 20 69 of times this i
4eba0 6e 73 74 72 75 63 74 69 6f 6e 20 77 61 73 20 65 nstruction was e
4ebb0 78 65 63 75 74 65 64 20 2a 2f 0a 20 20 75 36 34 xecuted */. u64
4ebc0 20 63 79 63 6c 65 73 3b 20 20 20 20 20 20 20 20 cycles;
4ebd0 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 74 /* Total t
4ebe0 69 6d 65 20 73 70 65 6e 74 20 65 78 65 63 75 74 ime spent execut
4ebf0 69 6e 67 20 74 68 69 73 20 69 6e 73 74 72 75 63 ing this instruc
4ec00 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d tion */.#endif.}
4ec10 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4ec20 20 56 64 62 65 4f 70 20 56 64 62 65 4f 70 3b 0a VdbeOp VdbeOp;.
4ec30 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 75 62 2d 72 6f ../*.** A sub-ro
4ec40 75 74 69 6e 65 20 75 73 65 64 20 74 6f 20 69 6d utine used to im
4ec50 70 6c 65 6d 65 6e 74 20 61 20 74 72 69 67 67 65 plement a trigge
4ec60 72 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 73 74 r program..*/.st
4ec70 72 75 63 74 20 53 75 62 50 72 6f 67 72 61 6d 20 ruct SubProgram
4ec80 7b 0a 20 20 56 64 62 65 4f 70 20 2a 61 4f 70 3b {. VdbeOp *aOp;
4ec90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4eca0 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 6f 70 /* Array of op
4ecb0 63 6f 64 65 73 20 66 6f 72 20 73 75 62 2d 70 72 codes for sub-pr
4ecc0 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e ogram */. int n
4ecd0 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Op;
4ece0 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6c 65 6d /* Elem
4ecf0 65 6e 74 73 20 69 6e 20 61 4f 70 5b 5d 20 2a 2f ents in aOp[] */
4ed00 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 . int nMem;
4ed10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ed20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 /* Number of me
4ed30 6d 6f 72 79 20 63 65 6c 6c 73 20 72 65 71 75 69 mory cells requi
4ed40 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 73 red */. int nCs
4ed50 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
4ed60 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
4ed70 20 6f 66 20 63 75 72 73 6f 72 73 20 72 65 71 75 of cursors requ
4ed80 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 ired */. int nR
4ed90 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ef;
4eda0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
4edb0 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f r of pointers to
4edc0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
4edd0 2a 2f 0a 20 20 76 6f 69 64 20 2a 74 6f 6b 65 6e */. void *token
4ede0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
4edf0 20 20 20 2f 2a 20 69 64 20 74 68 61 74 20 6d 61 /* id that ma
4ee00 79 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 63 y be used to rec
4ee10 75 72 73 69 76 65 20 74 72 69 67 67 65 72 73 20 ursive triggers
4ee20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 */.};../*.** A s
4ee30 6d 61 6c 6c 65 72 20 76 65 72 73 69 6f 6e 20 6f maller version o
4ee40 66 20 56 64 62 65 4f 70 20 75 73 65 64 20 66 6f f VdbeOp used fo
4ee50 72 20 74 68 65 20 56 64 62 65 41 64 64 4f 70 4c r the VdbeAddOpL
4ee60 69 73 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 ist() function b
4ee70 65 63 61 75 73 65 0a 2a 2a 20 69 74 20 74 61 6b ecause.** it tak
4ee80 65 73 20 75 70 20 6c 65 73 73 20 73 70 61 63 65 es up less space
4ee90 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 ..*/.struct Vdbe
4eea0 4f 70 4c 69 73 74 20 7b 0a 20 20 75 38 20 6f 70 OpList {. u8 op
4eeb0 63 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f code; /
4eec0 2a 20 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e * What operation
4eed0 20 74 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 to perform */.
4eee0 20 73 69 67 6e 65 64 20 63 68 61 72 20 70 31 3b signed char p1;
4eef0 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 70 /* First op
4ef00 65 72 61 6e 64 20 2a 2f 0a 20 20 73 69 67 6e 65 erand */. signe
4ef10 64 20 63 68 61 72 20 70 32 3b 20 20 20 20 20 2f d char p2; /
4ef20 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 * Second paramet
4ef30 65 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a 75 er (often the ju
4ef40 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 20 mp destination)
4ef50 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 */. signed char
4ef60 20 70 33 3b 20 20 20 20 20 2f 2a 20 54 68 69 72 p3; /* Thir
4ef70 64 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 7d d parameter */.}
4ef80 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4ef90 20 56 64 62 65 4f 70 4c 69 73 74 20 56 64 62 65 VdbeOpList Vdbe
4efa0 4f 70 4c 69 73 74 3b 0a 0a 2f 2a 0a 2a 2a 20 41 OpList;../*.** A
4efb0 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 llowed values of
4efc0 20 56 64 62 65 4f 70 2e 70 34 74 79 70 65 0a 2a VdbeOp.p4type.*
4efd0 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 4e 4f 54 /.#define P4_NOT
4efe0 55 53 45 44 20 20 20 20 30 20 20 20 2f 2a 20 54 USED 0 /* T
4eff0 68 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20 he P4 parameter
4f000 69 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f 0a 23 is not used */.#
4f010 64 65 66 69 6e 65 20 50 34 5f 44 59 4e 41 4d 49 define P4_DYNAMI
4f020 43 20 20 28 2d 31 29 20 20 2f 2a 20 50 6f 69 6e C (-1) /* Poin
4f030 74 65 72 20 74 6f 20 61 20 73 74 72 69 6e 67 20 ter to a string
4f040 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
4f050 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a liteMalloc() */.
4f060 23 64 65 66 69 6e 65 20 50 34 5f 53 54 41 54 49 #define P4_STATI
4f070 43 20 20 20 28 2d 32 29 20 20 2f 2a 20 50 6f 69 C (-2) /* Poi
4f080 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 nter to a static
4f090 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 string */.#defi
4f0a0 6e 65 20 50 34 5f 43 4f 4c 4c 53 45 51 20 20 28 ne P4_COLLSEQ (
4f0b0 2d 34 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 -4) /* P4 is a
4f0c0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 43 6f 6c pointer to a Col
4f0d0 6c 53 65 71 20 73 74 72 75 63 74 75 72 65 20 2a lSeq structure *
4f0e0 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 46 55 4e /.#define P4_FUN
4f0f0 43 44 45 46 20 20 28 2d 35 29 20 20 2f 2a 20 50 CDEF (-5) /* P
4f100 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 4 is a pointer t
4f110 6f 20 61 20 46 75 6e 63 44 65 66 20 73 74 72 75 o a FuncDef stru
4f120 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 cture */.#define
4f130 20 50 34 5f 4b 45 59 49 4e 46 4f 20 20 28 2d 36 P4_KEYINFO (-6
4f140 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f ) /* P4 is a po
4f150 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e inter to a KeyIn
4f160 66 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a fo structure */.
4f170 23 64 65 66 69 6e 65 20 50 34 5f 56 44 42 45 46 #define P4_VDBEF
4f180 55 4e 43 20 28 2d 37 29 20 20 2f 2a 20 50 34 20 UNC (-7) /* P4
4f190 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
4f1a0 61 20 56 64 62 65 46 75 6e 63 20 73 74 72 75 63 a VdbeFunc struc
4f1b0 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ture */.#define
4f1c0 50 34 5f 4d 45 4d 20 20 20 20 20 20 28 2d 38 29 P4_MEM (-8)
4f1d0 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 /* P4 is a poi
4f1e0 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 2a 20 20 nter to a Mem*
4f1f0 20 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 structure */.#
4f200 64 65 66 69 6e 65 20 50 34 5f 54 52 41 4e 53 49 define P4_TRANSI
4f210 45 4e 54 20 28 2d 39 29 20 2f 2a 20 50 34 20 69 ENT (-9) /* P4 i
4f220 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
4f230 20 74 72 61 6e 73 69 65 6e 74 20 73 74 72 69 6e transient strin
4f240 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f g */.#define P4_
4f250 56 54 41 42 20 20 20 20 20 28 2d 31 30 29 20 2f VTAB (-10) /
4f260 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 * P4 is a pointe
4f270 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f r to an sqlite3_
4f280 76 74 61 62 20 73 74 72 75 63 74 75 72 65 20 2a vtab structure *
4f290 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 4d 50 52 /.#define P4_MPR
4f2a0 49 4e 54 46 20 20 28 2d 31 31 29 20 2f 2a 20 50 INTF (-11) /* P
4f2b0 34 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 62 4 is a string ob
4f2c0 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 tained from sqli
4f2d0 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f te3_mprintf() */
4f2e0 0a 23 64 65 66 69 6e 65 20 50 34 5f 52 45 41 4c .#define P4_REAL
4f2f0 20 20 20 20 20 28 2d 31 32 29 20 2f 2a 20 50 34 (-12) /* P4
4f300 20 69 73 20 61 20 36 34 2d 62 69 74 20 66 6c 6f is a 64-bit flo
4f310 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 ating point valu
4f320 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f e */.#define P4_
4f330 49 4e 54 36 34 20 20 20 20 28 2d 31 33 29 20 2f INT64 (-13) /
4f340 2a 20 50 34 20 69 73 20 61 20 36 34 2d 62 69 74 * P4 is a 64-bit
4f350 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 signed integer
4f360 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e */.#define P4_IN
4f370 54 33 32 20 20 20 20 28 2d 31 34 29 20 2f 2a 20 T32 (-14) /*
4f380 50 34 20 69 73 20 61 20 33 32 2d 62 69 74 20 73 P4 is a 32-bit s
4f390 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f igned integer */
4f3a0 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54 41 .#define P4_INTA
4f3b0 52 52 41 59 20 28 2d 31 35 29 20 2f 2a 20 50 34 RRAY (-15) /* P4
4f3c0 20 69 73 20 61 20 76 65 63 74 6f 72 20 6f 66 20 is a vector of
4f3d0 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 73 20 32-bit integers
4f3e0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 53 55 */.#define P4_SU
4f3f0 42 50 52 4f 47 52 41 4d 20 20 28 2d 31 38 29 20 BPROGRAM (-18)
4f400 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 /* P4 is a point
4f410 65 72 20 74 6f 20 61 20 53 75 62 50 72 6f 67 72 er to a SubProgr
4f420 61 6d 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a am structure */.
4f430 0a 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e 67 20 ./* When adding
4f440 61 20 50 34 20 61 72 67 75 6d 65 6e 74 20 75 73 a P4 argument us
4f450 69 6e 67 20 50 34 5f 4b 45 59 49 4e 46 4f 2c 20 ing P4_KEYINFO,
4f460 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 4b 65 a copy of the Ke
4f470 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a yInfo structure.
4f480 2a 2a 20 69 73 20 6d 61 64 65 2e 20 20 54 68 61 ** is made. Tha
4f490 74 20 63 6f 70 79 20 69 73 20 66 72 65 65 64 20 t copy is freed
4f4a0 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 69 73 when the Vdbe is
4f4b0 20 66 69 6e 61 6c 69 7a 65 64 2e 20 20 42 75 74 finalized. But
4f4c0 20 69 66 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d if the.** argum
4f4d0 65 6e 74 20 69 73 20 50 34 5f 4b 45 59 49 4e 46 ent is P4_KEYINF
4f4e0 4f 5f 48 41 4e 44 4f 46 46 2c 20 74 68 65 20 70 O_HANDOFF, the p
4f4f0 61 73 73 65 64 20 69 6e 20 70 6f 69 6e 74 65 72 assed in pointer
4f500 20 69 73 20 75 73 65 64 2e 20 20 49 74 20 73 74 is used. It st
4f510 69 6c 6c 0a 2a 2a 20 67 65 74 73 20 66 72 65 65 ill.** gets free
4f520 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 d when the Vdbe
4f530 69 73 20 66 69 6e 61 6c 69 7a 65 64 20 73 6f 20 is finalized so
4f540 69 74 20 73 74 69 6c 6c 20 73 68 6f 75 6c 64 20 it still should
4f550 62 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 be obtained.** f
4f560 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 73 71 6c rom a single sql
4f570 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 42 75 iteMalloc(). Bu
4f580 74 20 6e 6f 20 63 6f 70 79 20 69 73 20 6d 61 64 t no copy is mad
4f590 65 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 6e e and the callin
4f5a0 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 73 68 g.** function sh
4f5b0 6f 75 6c 64 20 2a 6e 6f 74 2a 20 74 72 79 20 74 ould *not* try t
4f5c0 6f 20 66 72 65 65 20 74 68 65 20 4b 65 79 49 6e o free the KeyIn
4f5d0 66 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 fo..*/.#define P
4f5e0 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 4_KEYINFO_HANDOF
4f5f0 46 20 28 2d 31 36 29 0a 23 64 65 66 69 6e 65 20 F (-16).#define
4f600 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 54 49 P4_KEYINFO_STATI
4f610 43 20 20 28 2d 31 37 29 0a 0a 2f 2a 0a 2a 2a 20 C (-17)../*.**
4f620 54 68 65 20 56 64 62 65 2e 61 43 6f 6c 4e 61 6d The Vdbe.aColNam
4f630 65 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 e array contains
4f640 20 35 6e 20 4d 65 6d 20 73 74 72 75 63 74 75 72 5n Mem structur
4f650 65 73 2c 20 77 68 65 72 65 20 6e 20 69 73 20 74 es, where n is t
4f660 68 65 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 he .** number of
4f670 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74 61 columns of data
4f680 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 returned by the
4f690 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 statement..*/.#
4f6a0 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e define COLNAME_N
4f6b0 41 4d 45 20 20 20 20 20 30 0a 23 64 65 66 69 6e AME 0.#defin
4f6c0 65 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 e COLNAME_DECLTY
4f6d0 50 45 20 31 0a 23 64 65 66 69 6e 65 20 43 4f 4c PE 1.#define COL
4f6e0 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 20 32 0a NAME_DATABASE 2.
4f6f0 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f #define COLNAME_
4f700 54 41 42 4c 45 20 20 20 20 33 0a 23 64 65 66 69 TABLE 3.#defi
4f710 6e 65 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d ne COLNAME_COLUM
4f720 4e 20 20 20 34 0a 23 69 66 64 65 66 20 53 51 4c N 4.#ifdef SQL
4f730 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d ITE_ENABLE_COLUM
4f740 4e 5f 4d 45 54 41 44 41 54 41 0a 23 20 64 65 66 N_METADATA.# def
4f750 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 ine COLNAME_N
4f760 20 20 20 20 20 35 20 20 20 20 20 20 2f 2a 20 4e 5 /* N
4f770 75 6d 62 65 72 20 6f 66 20 43 4f 4c 4e 41 4d 45 umber of COLNAME
4f780 5f 78 78 78 20 73 79 6d 62 6f 6c 73 20 2a 2f 0a _xxx symbols */.
4f790 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 53 51 #else.# ifdef SQ
4f7a0 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 LITE_OMIT_DECLTY
4f7b0 50 45 0a 23 20 20 20 64 65 66 69 6e 65 20 43 4f PE.# define CO
4f7c0 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20 31 20 20 LNAME_N 1
4f7d0 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 6f 6e 6c /* Store onl
4f7e0 79 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 23 20 y the name */.#
4f7f0 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e 65 20 else.# define
4f800 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20 32 COLNAME_N 2
4f810 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 /* Store t
4f820 68 65 20 6e 61 6d 65 20 61 6e 64 20 64 65 63 6c he name and decl
4f830 74 79 70 65 20 2a 2f 0a 23 20 65 6e 64 69 66 0a type */.# endif.
4f840 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
4f850 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 e following macr
4f860 6f 20 63 6f 6e 76 65 72 74 73 20 61 20 72 65 6c o converts a rel
4f870 61 74 69 76 65 20 61 64 64 72 65 73 73 20 69 6e ative address in
4f880 20 74 68 65 20 70 32 20 66 69 65 6c 64 0a 2a 2a the p2 field.**
4f890 20 6f 66 20 61 20 56 64 62 65 4f 70 20 73 74 72 of a VdbeOp str
4f8a0 75 63 74 75 72 65 20 69 6e 74 6f 20 61 20 6e 65 ucture into a ne
4f8b0 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20 73 6f gative number so
4f8c0 20 74 68 61 74 20 0a 2a 2a 20 73 71 6c 69 74 65 that .** sqlite
4f8d0 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 29 3VdbeAddOpList()
4f8e0 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 68 65 20 knows that the
4f8f0 61 64 64 72 65 73 73 20 69 73 20 72 65 6c 61 74 address is relat
4f900 69 76 65 2e 20 20 43 61 6c 6c 69 6e 67 0a 2a 2a ive. Calling.**
4f910 20 74 68 65 20 6d 61 63 72 6f 20 61 67 61 69 6e the macro again
4f920 20 72 65 73 74 6f 72 65 73 20 74 68 65 20 61 64 restores the ad
4f930 64 72 65 73 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e dress..*/.#defin
4f940 65 20 41 44 44 52 28 58 29 20 20 28 2d 31 2d 28 e ADDR(X) (-1-(
4f950 58 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d X))../*.** The m
4f960 61 6b 65 66 69 6c 65 20 73 63 61 6e 73 20 74 68 akefile scans th
4f970 65 20 76 64 62 65 2e 63 20 73 6f 75 72 63 65 20 e vdbe.c source
4f980 66 69 6c 65 20 61 6e 64 20 63 72 65 61 74 65 73 file and creates
4f990 20 74 68 65 20 22 6f 70 63 6f 64 65 73 2e 68 22 the "opcodes.h"
4f9a0 0a 2a 2a 20 68 65 61 64 65 72 20 66 69 6c 65 20 .** header file
4f9b0 74 68 61 74 20 64 65 66 69 6e 65 73 20 61 20 6e that defines a n
4f9c0 75 6d 62 65 72 20 66 6f 72 20 65 61 63 68 20 6f umber for each o
4f9d0 70 63 6f 64 65 20 75 73 65 64 20 62 79 20 74 68 pcode used by th
4f9e0 65 20 56 44 42 45 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a e VDBE..*/./****
4f9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
4fa00 64 65 20 6f 70 63 6f 64 65 73 2e 68 20 69 6e 20 de opcodes.h in
4fa10 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 76 64 the middle of vd
4fa20 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a be.h ***********
4fa30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
4fa40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
4fa50 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e 68 20 file opcodes.h
4fa60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4fa70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4fa80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 41 75 *********/./* Au
4fa90 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 tomatically gene
4faa0 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 65 rated. Do not e
4fab0 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 20 74 68 dit */./* See th
4fac0 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 e mkopcodeh.awk
4fad0 73 63 72 69 70 74 20 66 6f 72 20 64 65 74 61 69 script for detai
4fae0 6c 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 ls */.#define OP
4faf0 5f 52 65 61 64 43 6f 6f 6b 69 65 20 20 20 20 20 _ReadCookie
4fb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb10 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 1.#define
4fb20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 20 20 20 OP_AutoCommit
4fb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb40 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 2.#defin
4fb50 65 20 4f 50 5f 46 6f 75 6e 64 20 20 20 20 20 20 e OP_Found
4fb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb70 20 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 3.#def
4fb80 69 6e 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 20 20 ine OP_NullRow
4fb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fba0 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 23 64 4.#d
4fbb0 65 66 69 6e 65 20 4f 50 5f 4c 74 20 20 20 20 20 efine OP_Lt
4fbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 37 39 20 79
4fbe0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
4fbf0 4c 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 LT */.#def
4fc00 69 6e 65 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 ine OP_RowSetTes
4fc10 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
4fc20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 5.#d
4fc30 65 66 69 6e 65 20 4f 50 5f 56 61 72 69 61 62 6c efine OP_Variabl
4fc40 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
4fc50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 6.
4fc60 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 41 #define OP_RealA
4fc70 66 66 69 6e 69 74 79 20 20 20 20 20 20 20 20 20 ffinity
4fc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc90 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72 7.#define OP_Sor
4fca0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
4fcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fcc0 20 20 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 8.#define OP_A
4fcd0 66 66 69 6e 69 74 79 20 20 20 20 20 20 20 20 20 ffinity
4fce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fcf0 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20 4f 50 9.#define OP
4fd00 5f 49 66 4e 6f 74 20 20 20 20 20 20 20 20 20 20 _IfNot
4fd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd20 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e 65 20 10.#define
4fd30 4f 50 5f 47 6f 73 75 62 20 20 20 20 20 20 20 20 OP_Gosub
4fd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd50 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 6e 11.#defin
4fd60 65 20 4f 50 5f 41 64 64 20 20 20 20 20 20 20 20 e OP_Add
4fd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd80 20 20 20 20 20 20 20 20 20 38 36 20 20 20 2f 2a 86 /*
4fd90 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 same as TK_PLUS
4fda0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
4fdb0 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 20 20 20 20 OP_NotFound
4fdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fdd0 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 12.#defin
4fde0 65 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 20 20 e OP_ResultRow
4fdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fe00 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 65 66 13.#def
4fe10 69 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 20 20 ine OP_IsNull
4fe20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fe30 20 20 20 20 20 20 20 20 20 20 20 37 33 20 20 20 73
4fe40 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 /* same as TK_IS
4fe50 4e 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 66 69 6e NULL */.#defin
4fe60 65 20 4f 50 5f 53 65 65 6b 4c 65 20 20 20 20 20 e OP_SeekLe
4fe70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fe80 20 20 20 20 20 20 20 20 20 31 34 0a 23 64 65 66 14.#def
4fe90 69 6e 65 20 4f 50 5f 52 6f 77 69 64 20 20 20 20 ine OP_Rowid
4fea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4feb0 20 20 20 20 20 20 20 20 20 20 20 31 35 0a 23 64 15.#d
4fec0 65 66 69 6e 65 20 4f 50 5f 43 72 65 61 74 65 49 efine OP_CreateI
4fed0 6e 64 65 78 20 20 20 20 20 20 20 20 20 20 20 20 ndex
4fee0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 36 0a 16.
4fef0 23 64 65 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61 #define OP_Expla
4ff00 69 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 in
4ff10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4ff20 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 7.#define OP_Dro
4ff30 70 49 6e 64 65 78 20 20 20 20 20 20 20 20 20 20 pIndex
4ff40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff50 20 31 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 18.#define OP_N
4ff60 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 ull
4ff70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff80 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 20.#define OP
4ff90 5f 50 72 6f 67 72 61 6d 20 20 20 20 20 20 20 20 _Program
4ffa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ffb0 20 20 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20 21.#define
4ffc0 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 20 20 20 OP_ToInt
4ffd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ffe0 20 20 20 20 20 20 31 34 34 20 20 20 2f 2a 20 73 144 /* s
4fff0 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 ame as TK_TO_INT
50000 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
50010 5f 49 6e 74 36 34 20 20 20 20 20 20 20 20 20 20 _Int64
50020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50030 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20 22.#define
50040 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 OP_LoadAnalysis
50050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50060 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 69 6e 23.#defin
50070 65 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 20 20 e OP_IdxInsert
50080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50090 20 20 20 20 20 20 20 20 20 32 34 0a 23 64 65 66 24.#def
500a0 69 6e 65 20 4f 50 5f 56 55 70 64 61 74 65 20 20 ine OP_VUpdate
500b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
500c0 20 20 20 20 20 20 20 20 20 20 20 32 35 0a 23 64 25.#d
500d0 65 66 69 6e 65 20 4f 50 5f 4e 65 78 74 20 20 20 efine OP_Next
500e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
500f0 20 20 20 20 20 20 20 20 20 20 20 20 20 32 36 0a 26.
50100 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 4e 75 6d #define OP_ToNum
50110 65 72 69 63 20 20 20 20 20 20 20 20 20 20 20 20 eric
50120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 14
50130 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 3 /* same as T
50140 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2a 2f 0a 23 K_TO_NUMERIC*/.#
50150 64 65 66 69 6e 65 20 4f 50 5f 47 65 20 20 20 20 define OP_Ge
50160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 30 80
50180 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
50190 5f 47 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 _GE */.#de
501a0 66 69 6e 65 20 4f 50 5f 42 69 74 4e 6f 74 20 20 fine OP_BitNot
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 20 39 33 20 20 93
501d0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 /* same as TK_B
501e0 49 54 4e 4f 54 20 20 20 2a 2f 0a 23 64 65 66 69 ITNOT */.#defi
501f0 6e 65 20 4f 50 5f 53 65 65 6b 4c 74 20 20 20 20 ne OP_SeekLt
50200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50210 20 20 20 20 20 20 20 20 20 20 32 37 0a 23 64 65 27.#de
50220 66 69 6e 65 20 4f 50 5f 52 65 77 69 6e 64 20 20 fine OP_Rewind
50230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50240 20 20 20 20 20 20 20 20 20 20 20 20 32 38 0a 23 28.#
50250 64 65 66 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70 define OP_Multip
50260 6c 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ly
50270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 38 88
50280 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
50290 5f 53 54 41 52 20 20 20 20 20 2a 2f 0a 23 64 65 _STAR */.#de
502a0 66 69 6e 65 20 4f 50 5f 54 6f 52 65 61 6c 20 20 fine OP_ToReal
502b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
502c0 20 20 20 20 20 20 20 20 20 20 20 31 34 35 20 20 145
502d0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 /* same as TK_T
502e0 4f 5f 52 45 41 4c 20 20 2a 2f 0a 23 64 65 66 69 O_REAL */.#defi
502f0 6e 65 20 4f 50 5f 47 74 20 20 20 20 20 20 20 20 ne OP_Gt
50300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50310 20 20 20 20 20 20 20 20 20 20 37 37 20 20 20 2f 77 /
50320 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 20 * same as TK_GT
50330 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
50340 20 4f 50 5f 52 6f 77 53 65 74 52 65 61 64 20 20 OP_RowSetRead
50350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50360 20 20 20 20 20 20 20 20 32 39 0a 23 64 65 66 69 29.#defi
50370 6e 65 20 4f 50 5f 4c 61 73 74 20 20 20 20 20 20 ne OP_Last
50380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50390 20 20 20 20 20 20 20 20 20 20 33 30 0a 23 64 65 30.#de
503a0 66 69 6e 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e fine OP_MustBeIn
503b0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
503c0 20 20 20 20 20 20 20 20 20 20 20 20 33 31 0a 23 31.#
503d0 64 65 66 69 6e 65 20 4f 50 5f 4e 65 20 20 20 20 define OP_Ne
503e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
503f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 35 75
50400 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
50410 5f 4e 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 _NE */.#de
50420 66 69 6e 65 20 4f 50 5f 49 6e 63 72 56 61 63 75 fine OP_IncrVacu
50430 75 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 um
50440 20 20 20 20 20 20 20 20 20 20 20 20 33 32 0a 23 32.#
50450 64 65 66 69 6e 65 20 4f 50 5f 53 74 72 69 6e 67 define OP_String
50460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 33 33
50480 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 46 69 6c .#define OP_VFil
50490 74 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 ter
504a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
504b0 33 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 34.#define OP_Co
504c0 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 unt
504d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
504e0 20 20 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 35.#define OP_
504f0 43 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20 20 Close
50500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50510 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20 4f 36.#define O
50520 50 5f 41 67 67 46 69 6e 61 6c 20 20 20 20 20 20 P_AggFinal
50530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50540 20 20 20 20 20 20 33 37 0a 23 64 65 66 69 6e 65 37.#define
50550 20 4f 50 5f 52 6f 77 44 61 74 61 20 20 20 20 20 OP_RowData
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 33 38 0a 23 64 65 66 69 38.#defi
50580 6e 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 20 20 ne OP_IdxRowid
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 33 39 0a 23 64 65 39.#de
505b0 66 69 6e 65 20 4f 50 5f 50 61 72 61 6d 20 20 20 fine OP_Param
505c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
505d0 20 20 20 20 20 20 20 20 20 20 20 20 34 30 0a 23 40.#
505e0 64 65 66 69 6e 65 20 4f 50 5f 50 61 67 65 63 6f define OP_Pageco
505f0 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 unt
50600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 31 41
50610 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 4f .#define OP_BitO
50620 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r
50630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50640 38 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 83 /* same as
50650 54 4b 5f 42 49 54 4f 52 20 20 20 20 2a 2f 0a 23 TK_BITOR */.#
50660 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 4e 75 6c define OP_NotNul
50670 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l
50680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 34 74
50690 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
506a0 5f 4e 4f 54 4e 55 4c 4c 20 20 2a 2f 0a 23 64 65 _NOTNULL */.#de
506b0 66 69 6e 65 20 4f 50 5f 53 65 65 6b 47 65 20 20 fine OP_SeekGe
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 34 32 0a 23 42.#
506e0 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 20 20 20 define OP_Not
506f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 39 19
50710 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
50720 5f 4e 4f 54 20 20 20 20 20 20 2a 2f 0a 23 64 65 _NOT */.#de
50730 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75 fine OP_OpenPseu
50740 64 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 do
50750 20 20 20 20 20 20 20 20 20 20 20 20 34 33 0a 23 43.#
50760 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74 20 20 define OP_Halt
50770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 34 44
50790 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6d 70 .#define OP_Comp
507a0 61 72 65 20 20 20 20 20 20 20 20 20 20 20 20 20 are
507b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507c0 34 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 45.#define OP_Ne
507d0 77 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 wRowid
507e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507f0 20 20 34 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46.#define OP_
50800 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 20 20 Real
50810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50820 20 20 20 31 33 30 20 20 20 2f 2a 20 73 61 6d 65 130 /* same
50830 20 61 73 20 54 4b 5f 46 4c 4f 41 54 20 20 20 20 as TK_FLOAT
50840 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 */.#define OP_Id
50850 78 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20 xLT
50860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50870 20 20 34 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47.#define OP_
50880 53 65 65 6b 47 74 20 20 20 20 20 20 20 20 20 20 SeekGt
50890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
508a0 20 20 20 20 34 38 0a 23 64 65 66 69 6e 65 20 4f 48.#define O
508b0 50 5f 4d 65 6d 4d 61 78 20 20 20 20 20 20 20 20 P_MemMax
508c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
508d0 20 20 20 20 20 20 34 39 0a 23 64 65 66 69 6e 65 49.#define
508e0 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 20 20 20 OP_Function
508f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50900 20 20 20 20 20 20 20 20 35 30 0a 23 64 65 66 69 50.#defi
50910 6e 65 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 ne OP_IntegrityC
50920 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k
50930 20 20 20 20 20 20 20 20 20 20 35 31 0a 23 64 65 51.#de
50940 66 69 6e 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 fine OP_Remainde
50950 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r
50960 20 20 20 20 20 20 20 20 20 20 20 20 39 30 20 20 90
50970 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 /* same as TK_R
50980 45 4d 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 EM */.#defi
50990 6e 65 20 4f 50 5f 46 6b 43 6f 75 6e 74 65 72 20 ne OP_FkCounter
509a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
509b0 20 20 20 20 20 20 20 20 20 20 35 32 0a 23 64 65 52.#de
509c0 66 69 6e 65 20 4f 50 5f 53 43 6f 70 79 20 20 20 fine OP_SCopy
509d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
509e0 20 20 20 20 20 20 20 20 20 20 20 20 35 33 0a 23 53.#
509f0 64 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 4c define OP_ShiftL
50a00 65 66 74 20 20 20 20 20 20 20 20 20 20 20 20 20 eft
50a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 34 84
50a20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
50a30 5f 4c 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 _LSHIFT */.#de
50a40 66 69 6e 65 20 4f 50 5f 49 66 4e 65 67 20 20 20 fine OP_IfNeg
50a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a60 20 20 20 20 20 20 20 20 20 20 20 20 35 34 0a 23 54.#
50a70 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 41 6e 64 define OP_BitAnd
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 20 20 20 20 20 20 38 32 82
50aa0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
50ab0 5f 42 49 54 41 4e 44 20 20 20 2a 2f 0a 23 64 65 _BITAND */.#de
50ac0 66 69 6e 65 20 4f 50 5f 4f 72 20 20 20 20 20 20 fine OP_Or
50ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ae0 20 20 20 20 20 20 20 20 20 20 20 20 36 38 20 20 68
50af0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4f /* same as TK_O
50b00 52 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 R */.#defi
50b10 6e 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 ne OP_NotExists
50b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b30 20 20 20 20 20 20 20 20 20 20 35 35 0a 23 64 65 55.#de
50b40 66 69 6e 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 fine OP_VDestroy
50b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b60 20 20 20 20 20 20 20 20 20 20 20 20 35 36 0a 23 56.#
50b70 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 44 65 6c define OP_IdxDel
50b80 65 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ete
50b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 37 57
50ba0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 61 63 75 .#define OP_Vacu
50bb0 75 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 um
50bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50bd0 35 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 58.#define OP_Co
50be0 70 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 py
50bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c00 20 20 35 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 59.#define OP_
50c10 49 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 If
50c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c30 20 20 20 20 36 30 0a 23 64 65 66 69 6e 65 20 4f 60.#define O
50c40 50 5f 4a 75 6d 70 20 20 20 20 20 20 20 20 20 20 P_Jump
50c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c60 20 20 20 20 20 20 36 31 0a 23 64 65 66 69 6e 65 61.#define
50c70 20 4f 50 5f 44 65 73 74 72 6f 79 20 20 20 20 20 OP_Destroy
50c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c90 20 20 20 20 20 20 20 20 36 32 0a 23 64 65 66 69 62.#defi
50ca0 6e 65 20 4f 50 5f 41 67 67 53 74 65 70 20 20 20 ne OP_AggStep
50cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50cc0 20 20 20 20 20 20 20 20 20 20 36 33 0a 23 64 65 63.#de
50cd0 66 69 6e 65 20 4f 50 5f 43 6c 65 61 72 20 20 20 fine OP_Clear
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 36 34 0a 23 64.#
50d00 64 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74 define OP_Insert
50d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 35 65
50d30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 65 72 6d .#define OP_Perm
50d40 75 74 61 74 69 6f 6e 20 20 20 20 20 20 20 20 20 utation
50d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d60 36 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 42 66.#define OP_VB
50d70 65 67 69 6e 20 20 20 20 20 20 20 20 20 20 20 20 egin
50d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d90 20 20 36 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 67.#define OP_
50da0 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 20 20 OpenEphemeral
50db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50dc0 20 20 20 20 37 30 0a 23 64 65 66 69 6e 65 20 4f 70.#define O
50dd0 50 5f 49 64 78 47 45 20 20 20 20 20 20 20 20 20 P_IdxGE
50de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50df0 20 20 20 20 20 20 37 31 0a 23 64 65 66 69 6e 65 71.#define
50e00 20 4f 50 5f 54 72 61 63 65 20 20 20 20 20 20 20 OP_Trace
50e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e20 20 20 20 20 20 20 20 20 37 32 0a 23 64 65 66 69 72.#defi
50e30 6e 65 20 4f 50 5f 44 69 76 69 64 65 20 20 20 20 ne OP_Divide
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 38 39 20 20 20 2f 89 /
50e60 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41 * same as TK_SLA
50e70 53 48 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 SH */.#define
50e80 20 4f 50 5f 53 74 72 69 6e 67 38 20 20 20 20 20 OP_String8
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 39 34 20 20 20 2f 2a 20 94 /*
50eb0 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 49 4e same as TK_STRIN
50ec0 47 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f G */.#define O
50ed0 50 5f 43 6f 6e 63 61 74 20 20 20 20 20 20 20 20 P_Concat
50ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ef0 20 20 20 20 20 20 39 31 20 20 20 2f 2a 20 73 61 91 /* sa
50f00 6d 65 20 61 73 20 54 4b 5f 43 4f 4e 43 41 54 20 me as TK_CONCAT
50f10 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
50f20 4d 61 6b 65 52 65 63 6f 72 64 20 20 20 20 20 20 MakeRecord
50f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f40 20 20 20 20 38 31 0a 23 64 65 66 69 6e 65 20 4f 81.#define O
50f50 50 5f 59 69 65 6c 64 20 20 20 20 20 20 20 20 20 P_Yield
50f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f70 20 20 20 20 20 20 39 32 0a 23 64 65 66 69 6e 65 92.#define
50f80 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 20 20 20 OP_SetCookie
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 20 39 35 0a 23 64 65 66 69 95.#defi
50fb0 6e 65 20 4f 50 5f 50 72 65 76 20 20 20 20 20 20 ne OP_Prev
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 20 39 36 0a 23 64 65 96.#de
50fe0 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 72 69 67 fine OP_DropTrig
50ff0 67 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 ger
51000 20 20 20 20 20 20 20 20 20 20 20 20 39 37 0a 23 97.#
51010 64 65 66 69 6e 65 20 4f 50 5f 46 6b 49 66 5a 65 define OP_FkIfZe
51020 72 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ro
51030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 38 98
51040 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 6e 64 20 .#define OP_And
51050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51070 36 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 69 /* same as
51080 54 4b 5f 41 4e 44 20 20 20 20 20 20 2a 2f 0a 23 TK_AND */.#
51090 64 65 66 69 6e 65 20 4f 50 5f 56 43 6f 6c 75 6d define OP_VColum
510a0 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n
510b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 39 99
510c0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 74 75 .#define OP_Retu
510d0 72 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 rn
510e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
510f0 30 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 00.#define OP_Op
51100 65 6e 57 72 69 74 65 20 20 20 20 20 20 20 20 20 enWrite
51110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51120 20 31 30 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 101.#define OP_
51130 49 6e 74 65 67 65 72 20 20 20 20 20 20 20 20 20 Integer
51140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51150 20 20 20 31 30 32 0a 23 64 65 66 69 6e 65 20 4f 102.#define O
51160 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 20 20 P_Transaction
51170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51180 20 20 20 20 20 31 30 33 0a 23 64 65 66 69 6e 65 103.#define
51190 20 4f 50 5f 49 66 50 6f 73 20 20 20 20 20 20 20 OP_IfPos
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 30 34 0a 23 64 65 66 69 104.#defi
511c0 6e 65 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 20 ne OP_RowSetAdd
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 30 35 0a 23 64 65 105.#de
511f0 66 69 6e 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 20 fine OP_CollSeq
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 30 36 0a 23 106.#
51220 64 65 66 69 6e 65 20 4f 50 5f 53 61 76 65 70 6f define OP_Savepo
51230 69 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 int
51240 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 37 107
51250 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 52 65 6e .#define OP_VRen
51260 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ame
51270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51280 30 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 08.#define OP_To
51290 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 Blob
512a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
512b0 20 31 34 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 142 /* same a
512c0 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 2a 2f s TK_TO_BLOB */
512d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 71 75 .#define OP_Sequ
512e0 65 6e 63 65 20 20 20 20 20 20 20 20 20 20 20 20 ence
512f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51300 30 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 68 09.#define OP_Sh
51310 69 66 74 52 69 67 68 74 20 20 20 20 20 20 20 20 iftRight
51320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51330 20 20 38 35 20 20 20 2f 2a 20 73 61 6d 65 20 61 85 /* same a
51340 73 20 54 4b 5f 52 53 48 49 46 54 20 20 20 2a 2f s TK_RSHIFT */
51350 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74 .#define OP_Halt
51360 49 66 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 IfNull
51370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51380 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 10.#define OP_VC
51390 72 65 61 74 65 20 20 20 20 20 20 20 20 20 20 20 reate
513a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
513b0 20 31 31 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 111.#define OP_
513c0 43 72 65 61 74 65 54 61 62 6c 65 20 20 20 20 20 CreateTable
513d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
513e0 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65 20 4f 112.#define O
513f0 50 5f 41 64 64 49 6d 6d 20 20 20 20 20 20 20 20 P_AddImm
51400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51410 20 20 20 20 20 31 31 33 0a 23 64 65 66 69 6e 65 113.#define
51420 20 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 20 20 OP_ToText
51430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51440 20 20 20 20 20 20 20 31 34 31 20 20 20 2f 2a 20 141 /*
51450 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 same as TK_TO_TE
51460 58 54 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f XT */.#define O
51470 50 5f 44 72 6f 70 54 61 62 6c 65 20 20 20 20 20 P_DropTable
51480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51490 20 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e 65 114.#define
514a0 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 20 20 20 OP_IsUnique
514b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
514c0 20 20 20 20 20 20 20 31 31 35 0a 23 64 65 66 69 115.#defi
514d0 6e 65 20 4f 50 5f 56 4f 70 65 6e 20 20 20 20 20 ne OP_VOpen
514e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
514f0 20 20 20 20 20 20 20 20 20 31 31 36 0a 23 64 65 116.#de
51500 66 69 6e 65 20 4f 50 5f 49 66 5a 65 72 6f 20 20 fine OP_IfZero
51510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51520 20 20 20 20 20 20 20 20 20 20 20 31 31 37 0a 23 117.#
51530 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 6f 70 20 20 define OP_Noop
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 20 20 20 20 20 20 31 31 38 118
51560 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 .#define OP_Inse
51570 72 74 49 6e 74 20 20 20 20 20 20 20 20 20 20 20 rtInt
51580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51590 31 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 19.#define OP_Ro
515a0 77 4b 65 79 20 20 20 20 20 20 20 20 20 20 20 20 wKey
515b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
515c0 20 31 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 120.#define OP_
515d0 45 78 70 69 72 65 20 20 20 20 20 20 20 20 20 20 Expire
515e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
515f0 20 20 20 31 32 31 0a 23 64 65 66 69 6e 65 20 4f 121.#define O
51600 50 5f 44 65 6c 65 74 65 20 20 20 20 20 20 20 20 P_Delete
51610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51620 20 20 20 20 20 31 32 32 0a 23 64 65 66 69 6e 65 122.#define
51630 20 4f 50 5f 53 75 62 74 72 61 63 74 20 20 20 20 OP_Subtract
51640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51650 20 20 20 20 20 20 20 20 38 37 20 20 20 2f 2a 20 87 /*
51660 73 61 6d 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53 same as TK_MINUS
51670 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f */.#define O
51680 50 5f 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20 P_Blob
51690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
516a0 20 20 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65 123.#define
516b0 20 4f 50 5f 4d 6f 76 65 20 20 20 20 20 20 20 20 OP_Move
516c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
516d0 20 20 20 20 20 20 20 31 32 34 0a 23 64 65 66 69 124.#defi
516e0 6e 65 20 4f 50 5f 47 6f 74 6f 20 20 20 20 20 20 ne OP_Goto
516f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51700 20 20 20 20 20 20 20 20 20 31 32 35 0a 23 64 65 125.#de
51710 66 69 6e 65 20 4f 50 5f 50 61 72 73 65 53 63 68 fine OP_ParseSch
51720 65 6d 61 20 20 20 20 20 20 20 20 20 20 20 20 20 ema
51730 20 20 20 20 20 20 20 20 20 20 20 31 32 36 0a 23 126.#
51740 64 65 66 69 6e 65 20 4f 50 5f 45 71 20 20 20 20 define OP_Eq
51750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 36 76
51770 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
51780 5f 45 51 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 _EQ */.#de
51790 66 69 6e 65 20 4f 50 5f 56 4e 65 78 74 20 20 20 fine OP_VNext
517a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
517b0 20 20 20 20 20 20 20 20 20 20 20 31 32 37 0a 23 127.#
517c0 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 20 20 define OP_Seek
517d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
517e0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 38 128
517f0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 65 20 20 .#define OP_Le
51800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51820 37 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 78 /* same as
51830 54 4b 5f 4c 45 20 20 20 20 20 20 20 2a 2f 0a 23 TK_LE */.#
51840 64 65 66 69 6e 65 20 4f 50 5f 54 61 62 6c 65 4c define OP_TableL
51850 6f 63 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 ock
51860 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 39 129
51870 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 65 72 69 .#define OP_Veri
51880 66 79 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 fyCookie
51890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
518a0 33 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 31.#define OP_Co
518b0 6c 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20 lumn
518c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
518d0 20 31 33 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 132.#define OP_
518e0 4f 70 65 6e 52 65 61 64 20 20 20 20 20 20 20 20 OpenRead
518f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51900 20 20 20 31 33 33 0a 23 64 65 66 69 6e 65 20 4f 133.#define O
51910 50 5f 52 65 73 65 74 43 6f 75 6e 74 20 20 20 20 P_ResetCount
51920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51930 20 20 20 20 20 31 33 34 0a 0a 2f 2a 20 54 68 65 134../* The
51940 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 63 6f 64 following opcod
51950 65 20 76 61 6c 75 65 73 20 61 72 65 20 6e 65 76 e values are nev
51960 65 72 20 75 73 65 64 20 2a 2f 0a 23 64 65 66 69 er used */.#defi
51970 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 ne OP_NotUsed_13
51980 35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5
51990 20 20 20 20 20 20 20 20 20 31 33 35 0a 23 64 65 135.#de
519a0 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f fine OP_NotUsed_
519b0 31 33 36 20 20 20 20 20 20 20 20 20 20 20 20 20 136
519c0 20 20 20 20 20 20 20 20 20 20 20 31 33 36 0a 23 136.#
519d0 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 define OP_NotUse
519e0 64 5f 31 33 37 20 20 20 20 20 20 20 20 20 20 20 d_137
519f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 37 137
51a00 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 .#define OP_NotU
51a10 73 65 64 5f 31 33 38 20 20 20 20 20 20 20 20 20 sed_138
51a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51a30 33 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 38.#define OP_No
51a40 74 55 73 65 64 5f 31 33 39 20 20 20 20 20 20 20 tUsed_139
51a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51a60 20 31 33 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 139.#define OP_
51a70 4e 6f 74 55 73 65 64 5f 31 34 30 20 20 20 20 20 NotUsed_140
51a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51a90 20 20 20 31 34 30 0a 0a 0a 2f 2a 20 50 72 6f 70 140.../* Prop
51aa0 65 72 74 69 65 73 20 73 75 63 68 20 61 73 20 22 erties such as "
51ab0 6f 75 74 32 22 20 6f 72 20 22 6a 75 6d 70 22 20 out2" or "jump"
51ac0 74 68 61 74 20 61 72 65 20 73 70 65 63 69 66 69 that are specifi
51ad0 65 64 20 69 6e 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 ed in.** comment
51ae0 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 s following the
51af0 22 63 61 73 65 22 20 66 6f 72 20 65 61 63 68 20 "case" for each
51b00 6f 70 63 6f 64 65 20 69 6e 20 74 68 65 20 76 64 opcode in the vd
51b10 62 65 2e 63 0a 2a 2a 20 61 72 65 20 65 6e 63 6f be.c.** are enco
51b20 64 65 64 20 69 6e 74 6f 20 62 69 74 76 65 63 74 ded into bitvect
51b30 6f 72 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a ors as follows:.
51b40 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 */.#define OPFLG
51b50 5f 4a 55 4d 50 20 20 20 20 20 20 20 20 20 20 20 _JUMP
51b60 20 30 78 30 30 30 31 20 20 2f 2a 20 6a 75 6d 70 0x0001 /* jump
51b70 3a 20 20 50 32 20 68 6f 6c 64 73 20 6a 6d 70 20 : P2 holds jmp
51b80 74 61 72 67 65 74 20 2a 2f 0a 23 64 65 66 69 6e target */.#defin
51b90 65 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 e OPFLG_OUT2_PRE
51ba0 52 45 4c 45 41 53 45 20 30 78 30 30 30 32 20 20 RELEASE 0x0002
51bb0 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 /* out2-prerelea
51bc0 73 65 3a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f se: */.#define O
51bd0 50 46 4c 47 5f 49 4e 31 20 20 20 20 20 20 20 20 PFLG_IN1
51be0 20 20 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 0x0004 /*
51bf0 69 6e 31 3a 20 20 20 50 31 20 69 73 20 61 6e 20 in1: P1 is an
51c00 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 input */.#define
51c10 20 4f 50 46 4c 47 5f 49 4e 32 20 20 20 20 20 20 OPFLG_IN2
51c20 20 20 20 20 20 20 20 30 78 30 30 30 38 20 20 2f 0x0008 /
51c30 2a 20 69 6e 32 3a 20 20 20 50 32 20 69 73 20 61 * in2: P2 is a
51c40 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 n input */.#defi
51c50 6e 65 20 4f 50 46 4c 47 5f 49 4e 33 20 20 20 20 ne OPFLG_IN3
51c60 20 20 20 20 20 20 20 20 20 30 78 30 30 31 30 20 0x0010
51c70 20 2f 2a 20 69 6e 33 3a 20 20 20 50 33 20 69 73 /* in3: P3 is
51c80 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 an input */.#de
51c90 66 69 6e 65 20 4f 50 46 4c 47 5f 4f 55 54 33 20 fine OPFLG_OUT3
51ca0 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 32 0x002
51cb0 30 20 20 2f 2a 20 6f 75 74 33 3a 20 20 50 33 20 0 /* out3: P3
51cc0 69 73 20 61 6e 20 6f 75 74 70 75 74 20 2a 2f 0a is an output */.
51cd0 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e #define OPFLG_IN
51ce0 49 54 49 41 4c 49 5a 45 52 20 7b 5c 0a 2f 2a 20 ITIALIZER {\./*
51cf0 20 20 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 0 */ 0x00, 0x0
51d00 32 2c 20 30 78 30 30 2c 20 30 78 31 31 2c 20 30 2, 0x00, 0x11, 0
51d10 78 30 30 2c 20 30 78 31 35 2c 20 30 78 30 30 2c x00, 0x15, 0x00,
51d20 20 30 78 30 34 2c 5c 0a 2f 2a 20 20 20 38 20 2a 0x04,\./* 8 *
51d30 2f 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 / 0x01, 0x00, 0x
51d40 30 35 2c 20 30 78 30 31 2c 20 30 78 31 31 2c 20 05, 0x01, 0x11,
51d50 30 78 30 30 2c 20 30 78 31 31 2c 20 30 78 30 32 0x00, 0x11, 0x02
51d60 2c 5c 0a 2f 2a 20 20 31 36 20 2a 2f 20 30 78 30 ,\./* 16 */ 0x0
51d70 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 2, 0x00, 0x00, 0
51d80 78 30 34 2c 20 30 78 30 32 2c 20 30 78 30 31 2c x04, 0x02, 0x01,
51d90 20 30 78 30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a 0x02, 0x00,\./*
51da0 20 20 32 34 20 2a 2f 20 30 78 30 38 2c 20 30 78 24 */ 0x08, 0x
51db0 30 30 2c 20 30 78 30 31 2c 20 30 78 31 31 2c 20 00, 0x01, 0x11,
51dc0 30 78 30 31 2c 20 30 78 32 31 2c 20 30 78 30 31 0x01, 0x21, 0x01
51dd0 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20 20 33 32 20 , 0x05,\./* 32
51de0 2a 2f 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 */ 0x01, 0x02, 0
51df0 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 30 2c x01, 0x02, 0x00,
51e00 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
51e10 32 2c 5c 0a 2f 2a 20 20 34 30 20 2a 2f 20 30 78 2,\./* 40 */ 0x
51e20 30 32 2c 20 30 78 30 32 2c 20 30 78 31 31 2c 20 02, 0x02, 0x11,
51e30 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
51e40 2c 20 30 78 30 32 2c 20 30 78 31 31 2c 5c 0a 2f , 0x02, 0x11,\./
51e50 2a 20 20 34 38 20 2a 2f 20 30 78 31 31 2c 20 30 * 48 */ 0x11, 0
51e60 78 30 38 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x08, 0x00, 0x00,
51e70 20 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 0x00, 0x04, 0x0
51e80 35 2c 20 30 78 31 31 2c 5c 0a 2f 2a 20 20 35 36 5, 0x11,\./* 56
51e90 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 */ 0x00, 0x00,
51ea0 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 35 0x00, 0x04, 0x05
51eb0 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 , 0x01, 0x02, 0x
51ec0 30 30 2c 5c 0a 2f 2a 20 20 36 34 20 2a 2f 20 30 00,\./* 64 */ 0
51ed0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
51ee0 20 30 78 30 30 2c 20 30 78 32 63 2c 20 30 78 32 0x00, 0x2c, 0x2
51ef0 63 2c 20 30 78 30 30 2c 20 30 78 31 31 2c 5c 0a c, 0x00, 0x11,\.
51f00 2f 2a 20 20 37 32 20 2a 2f 20 30 78 30 30 2c 20 /* 72 */ 0x00,
51f10 30 78 30 35 2c 20 30 78 30 35 2c 20 30 78 31 35 0x05, 0x05, 0x15
51f20 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78 , 0x15, 0x15, 0x
51f30 31 35 2c 20 30 78 31 35 2c 5c 0a 2f 2a 20 20 38 15, 0x15,\./* 8
51f40 30 20 2a 2f 20 30 78 31 35 2c 20 30 78 30 30 2c 0 */ 0x15, 0x00,
51f50 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 0x2c, 0x2c, 0x2
51f60 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 c, 0x2c, 0x2c, 0
51f70 78 32 63 2c 5c 0a 2f 2a 20 20 38 38 20 2a 2f 20 x2c,\./* 88 */
51f80 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63 0x2c, 0x2c, 0x2c
51f90 2c 20 30 78 32 63 2c 20 30 78 30 34 2c 20 30 78 , 0x2c, 0x04, 0x
51fa0 30 34 2c 20 30 78 30 32 2c 20 30 78 31 30 2c 5c 04, 0x02, 0x10,\
51fb0 0a 2f 2a 20 20 39 36 20 2a 2f 20 30 78 30 31 2c ./* 96 */ 0x01,
51fc0 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 0x00, 0x01, 0x0
51fd0 30 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 20 30 0, 0x04, 0x00, 0
51fe0 78 30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 31 x02, 0x00,\./* 1
51ff0 30 34 20 2a 2f 20 30 78 30 35 2c 20 30 78 30 38 04 */ 0x05, 0x08
52000 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
52010 30 30 2c 20 30 78 30 32 2c 20 30 78 31 30 2c 20 00, 0x02, 0x10,
52020 30 78 30 30 2c 5c 0a 2f 2a 20 31 31 32 20 2a 2f 0x00,\./* 112 */
52030 20 30 78 30 32 2c 20 30 78 30 34 2c 20 30 78 30 0x02, 0x04, 0x0
52040 30 2c 20 30 78 31 31 2c 20 30 78 30 30 2c 20 30 0, 0x11, 0x00, 0
52050 78 30 35 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x05, 0x00, 0x00,
52060 5c 0a 2f 2a 20 31 32 30 20 2a 2f 20 30 78 30 30 \./* 120 */ 0x00
52070 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
52080 30 32 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20 02, 0x00, 0x01,
52090 30 78 30 30 2c 20 30 78 30 31 2c 5c 0a 2f 2a 20 0x00, 0x01,\./*
520a0 31 32 38 20 2a 2f 20 30 78 30 38 2c 20 30 78 30 128 */ 0x08, 0x0
520b0 30 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 0, 0x02, 0x00, 0
520c0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
520d0 20 30 78 30 30 2c 5c 0a 2f 2a 20 31 33 36 20 2a 0x00,\./* 136 *
520e0 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 / 0x00, 0x00, 0x
520f0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
52100 30 78 30 34 2c 20 30 78 30 34 2c 20 30 78 30 34 0x04, 0x04, 0x04
52110 2c 5c 0a 2f 2a 20 31 34 34 20 2a 2f 20 30 78 30 ,\./* 144 */ 0x0
52120 34 2c 20 30 78 30 34 2c 7d 0a 0a 2f 2a 2a 2a 2a 4, 0x04,}../****
52130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
52140 66 20 6f 70 63 6f 64 65 73 2e 68 20 2a 2a 2a 2a f opcodes.h ****
52150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
52180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 ********** Conti
52190 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c nuing where we l
521a0 65 66 74 20 6f 66 66 20 69 6e 20 76 64 62 65 2e eft off in vdbe.
521b0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
521c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a *********/../*.*
521d0 2a 20 50 72 6f 74 6f 74 79 70 65 73 20 66 6f 72 * Prototypes for
521e0 20 74 68 65 20 56 44 42 45 20 69 6e 74 65 72 66 the VDBE interf
521f0 61 63 65 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e ace. See commen
52200 74 73 20 6f 6e 20 74 68 65 20 69 6d 70 6c 65 6d ts on the implem
52210 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 entation.** for
52220 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 a description of
52230 20 77 68 61 74 20 65 61 63 68 20 6f 66 20 74 68 what each of th
52240 65 73 65 20 72 6f 75 74 69 6e 65 73 20 64 6f 65 ese routines doe
52250 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
52260 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69 74 VATE Vdbe *sqlit
52270 65 33 56 64 62 65 43 72 65 61 74 65 28 73 71 6c e3VdbeCreate(sql
52280 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ite3*);.SQLITE_P
52290 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
522a0 65 33 56 64 62 65 41 64 64 4f 70 30 28 56 64 62 e3VdbeAddOp0(Vdb
522b0 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f e*,int);.SQLITE_
522c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
522d0 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 56 64 te3VdbeAddOp1(Vd
522e0 62 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 be*,int,int);.SQ
522f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
52300 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
52310 70 32 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 p2(Vdbe*,int,int
52320 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
52330 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
52340 33 56 64 62 65 41 64 64 4f 70 33 28 56 64 62 65 3VdbeAddOp3(Vdbe
52350 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e *,int,int,int,in
52360 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
52370 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
52380 62 65 41 64 64 4f 70 34 28 56 64 62 65 2a 2c 69 beAddOp4(Vdbe*,i
52390 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 63 nt,int,int,int,c
523a0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 2c 69 onst char *zP4,i
523b0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
523c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
523d0 64 62 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62 dbeAddOpList(Vdb
523e0 65 2a 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 e*, int nOp, Vdb
523f0 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 eOpList const *a
52400 4f 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 Op);.SQLITE_PRIV
52410 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
52420 56 64 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 VdbeChangeP1(Vdb
52430 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e e*, int addr, in
52440 74 20 50 31 29 3b 0a 53 51 4c 49 54 45 5f 50 52 t P1);.SQLITE_PR
52450 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
52460 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 56 e3VdbeChangeP2(V
52470 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 dbe*, int addr,
52480 69 6e 74 20 50 32 29 3b 0a 53 51 4c 49 54 45 5f int P2);.SQLITE_
52490 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
524a0 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 ite3VdbeChangeP3
524b0 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 (Vdbe*, int addr
524c0 2c 20 69 6e 74 20 50 33 29 3b 0a 53 51 4c 49 54 , int P3);.SQLIT
524d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
524e0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
524f0 50 35 28 56 64 62 65 2a 2c 20 75 38 20 50 35 29 P5(Vdbe*, u8 P5)
52500 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
52510 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
52520 65 4a 75 6d 70 48 65 72 65 28 56 64 62 65 2a 2c eJumpHere(Vdbe*,
52530 20 69 6e 74 20 61 64 64 72 29 3b 0a 53 51 4c 49 int addr);.SQLI
52540 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
52550 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
52560 65 54 6f 4e 6f 6f 70 28 56 64 62 65 2a 2c 20 69 eToNoop(Vdbe*, i
52570 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 4e 29 3b nt addr, int N);
52580 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
52590 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
525a0 43 68 61 6e 67 65 50 34 28 56 64 62 65 2a 2c 20 ChangeP4(Vdbe*,
525b0 69 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20 int addr, const
525c0 63 68 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 4e char *zP4, int N
525d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
525e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
525f0 62 65 55 73 65 73 42 74 72 65 65 28 56 64 62 65 beUsesBtree(Vdbe
52600 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
52610 50 52 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a PRIVATE VdbeOp *
52620 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 sqlite3VdbeGetOp
52630 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 (Vdbe*, int);.SQ
52640 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
52650 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
52660 4c 61 62 65 6c 28 56 64 62 65 2a 29 3b 0a 53 51 Label(Vdbe*);.SQ
52670 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
52680 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c d sqlite3VdbeDel
52690 65 74 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 ete(Vdbe*);.SQLI
526a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
526b0 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 sqlite3VdbeMakeR
526c0 65 61 64 79 28 56 64 62 65 2a 2c 69 6e 74 2c 69 eady(Vdbe*,int,i
526d0 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 nt,int,int,int,i
526e0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
526f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
52700 64 62 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 dbeFinalize(Vdbe
52710 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
52720 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
52730 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 dbeResolveLabel(
52740 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c Vdbe*, int);.SQL
52750 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
52760 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 sqlite3VdbeCurre
52770 6e 74 41 64 64 72 28 56 64 62 65 2a 29 3b 0a 23 ntAddr(Vdbe*);.#
52780 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
52790 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
527a0 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 E int sqlite3V
527b0 64 62 65 41 73 73 65 72 74 4d 61 79 41 62 6f 72 dbeAssertMayAbor
527c0 74 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a t(Vdbe *, int);.
527d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
527e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
527f0 65 54 72 61 63 65 28 56 64 62 65 2a 2c 46 49 4c eTrace(Vdbe*,FIL
52800 45 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 E*);.#endif.SQLI
52810 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
52820 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 sqlite3VdbeReset
52830 53 74 65 70 52 65 73 75 6c 74 28 56 64 62 65 2a StepResult(Vdbe*
52840 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
52850 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
52860 65 52 65 73 65 74 28 56 64 62 65 2a 29 3b 0a 53 eReset(Vdbe*);.S
52870 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
52880 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 id sqlite3VdbeSe
52890 74 4e 75 6d 43 6f 6c 73 28 56 64 62 65 2a 2c 69 tNumCols(Vdbe*,i
528a0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
528b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
528c0 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 56 64 dbeSetColName(Vd
528d0 62 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 63 be*, int, int, c
528e0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 76 6f 69 onst char *, voi
528f0 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 d(*)(void*));.SQ
52900 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
52910 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 d sqlite3VdbeCou
52920 6e 74 43 68 61 6e 67 65 73 28 56 64 62 65 2a 29 ntChanges(Vdbe*)
52930 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
52940 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 sqlite3 *sqlite
52950 33 56 64 62 65 44 62 28 56 64 62 65 2a 29 3b 0a 3VdbeDb(Vdbe*);.
52960 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
52970 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 oid sqlite3VdbeS
52980 65 74 53 71 6c 28 56 64 62 65 2a 2c 20 63 6f 6e etSql(Vdbe*, con
52990 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 st char *z, int
529a0 6e 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f n, int);.SQLITE_
529b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
529c0 69 74 65 33 56 64 62 65 53 77 61 70 28 56 64 62 ite3VdbeSwap(Vdb
529d0 65 2a 2c 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 e*,Vdbe*);.SQLIT
529e0 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 4f 70 E_PRIVATE VdbeOp
529f0 20 2a 73 71 6c 69 74 65 33 56 64 62 65 54 61 6b *sqlite3VdbeTak
52a00 65 4f 70 41 72 72 61 79 28 56 64 62 65 2a 2c 20 eOpArray(Vdbe*,
52a10 69 6e 74 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c int*, int*);.SQL
52a20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
52a30 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 6f 67 sqlite3VdbeProg
52a40 72 61 6d 44 65 6c 65 74 65 28 73 71 6c 69 74 65 ramDelete(sqlite
52a50 33 20 2a 2c 20 53 75 62 50 72 6f 67 72 61 6d 20 3 *, SubProgram
52a60 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
52a70 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f PRIVATE sqlite3_
52a80 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 64 value *sqlite3Vd
52a90 62 65 47 65 74 56 61 6c 75 65 28 56 64 62 65 2a beGetValue(Vdbe*
52aa0 2c 20 69 6e 74 2c 20 75 38 29 3b 0a 53 51 4c 49 , int, u8);.SQLI
52ab0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
52ac0 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 56 61 sqlite3VdbeSetVa
52ad0 72 6d 61 73 6b 28 56 64 62 65 2a 2c 20 69 6e 74 rmask(Vdbe*, int
52ae0 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 );..SQLITE_PRIVA
52af0 54 45 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 TE UnpackedRecor
52b00 64 20 2a 73 71 6c 69 74 65 33 56 64 62 65 52 65 d *sqlite3VdbeRe
52b10 63 6f 72 64 55 6e 70 61 63 6b 28 4b 65 79 49 6e cordUnpack(KeyIn
52b20 66 6f 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f fo*,int,const vo
52b30 69 64 2a 2c 63 68 61 72 2a 2c 69 6e 74 29 3b 0a id*,char*,int);.
52b40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
52b50 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 oid sqlite3VdbeD
52b60 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 eleteUnpackedRec
52b70 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f ord(UnpackedReco
52b80 72 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 rd*);.SQLITE_PRI
52b90 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
52ba0 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 VdbeRecordCompar
52bb0 65 28 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 e(int,const void
52bc0 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 *,UnpackedRecord
52bd0 2a 29 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 4e 44 *);...#ifndef ND
52be0 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 EBUG.SQLITE_PRIV
52bf0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
52c00 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 e3VdbeComment(Vd
52c10 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a be*, const char*
52c20 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 , ...);.# define
52c30 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 20 VdbeComment(X)
52c40 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d sqlite3VdbeComm
52c50 65 6e 74 20 58 0a 53 51 4c 49 54 45 5f 50 52 49 ent X.SQLITE_PRI
52c60 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
52c70 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 te3VdbeNoopComme
52c80 6e 74 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 nt(Vdbe*, const
52c90 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64 char*, ...);.# d
52ca0 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f efine VdbeNoopCo
52cb0 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 74 65 mment(X) sqlite
52cc0 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 3VdbeNoopComment
52cd0 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e X.#else.# defin
52ce0 65 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 e VdbeComment(X)
52cf0 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 4e 6f .# define VdbeNo
52d00 6f 70 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 65 6e opComment(X).#en
52d10 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a dif..#endif../**
52d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
52d30 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a of vdbe.h *****
52d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
52d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e ************ Con
52d80 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 tinuing where we
52d90 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c left off in sql
52da0 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
52db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
52dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 ************ Inc
52dd0 6c 75 64 65 20 70 61 67 65 72 2e 68 20 69 6e 20 lude pager.h in
52de0 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 the middle of sq
52df0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a liteInt.h ******
52e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
52e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
52e20 69 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 68 20 in file pager.h
52e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
52e60 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 ** 2001 Septembe
52e70 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 15.**.** The a
52e80 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
52e90 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
52ea0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
52eb0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
52ec0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
52ed0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
52ee0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
52ef0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
52f00 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
52f10 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
52f20 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
52f30 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
52f40 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
52f50 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
52f60 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
52f70 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
52f80 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
52f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52fd0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 ***.** This head
52fe0 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 er file defines
52ff0 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 68 the interface th
53000 61 74 20 74 68 65 20 73 71 6c 69 74 65 20 70 61 at the sqlite pa
53010 67 65 20 63 61 63 68 65 0a 2a 2a 20 73 75 62 73 ge cache.** subs
53020 79 73 74 65 6d 2e 20 20 54 68 65 20 70 61 67 65 ystem. The page
53030 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d cache subsystem
53040 20 72 65 61 64 73 20 61 6e 64 20 77 72 69 74 65 reads and write
53050 73 20 61 20 66 69 6c 65 20 61 20 70 61 67 65 0a s a file a page.
53060 2a 2a 20 61 74 20 61 20 74 69 6d 65 20 61 6e 64 ** at a time and
53070 20 70 72 6f 76 69 64 65 73 20 61 20 6a 6f 75 72 provides a jour
53080 6e 61 6c 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b nal for rollback
53090 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 ..**.** @(#) $Id
530a0 3a 20 70 61 67 65 72 2e 68 2c 76 20 31 2e 31 30 : pager.h,v 1.10
530b0 34 20 32 30 30 39 2f 30 37 2f 32 34 20 31 39 3a 4 2009/07/24 19:
530c0 30 31 3a 31 39 20 64 72 68 20 45 78 70 20 24 0a 01:19 drh Exp $.
530d0 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41 47 */..#ifndef _PAG
530e0 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50 ER_H_.#define _P
530f0 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44 AGER_H_../*.** D
53100 65 66 61 75 6c 74 20 6d 61 78 69 6d 75 6d 20 73 efault maximum s
53110 69 7a 65 20 66 6f 72 20 70 65 72 73 69 73 74 65 ize for persiste
53120 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 nt journal files
53130 2e 20 41 20 6e 65 67 61 74 69 76 65 20 0a 2a 2a . A negative .**
53140 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 6e 6f 20 value means no
53150 6c 69 6d 69 74 2e 20 54 68 69 73 20 76 61 6c 75 limit. This valu
53160 65 20 6d 61 79 20 62 65 20 6f 76 65 72 72 69 64 e may be overrid
53170 64 65 6e 20 75 73 69 6e 67 20 74 68 65 20 0a 2a den using the .*
53180 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f * sqlite3PagerJo
53190 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 29 urnalSizeLimit()
531a0 20 41 50 49 2e 20 53 65 65 20 61 6c 73 6f 20 22 API. See also "
531b0 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 PRAGMA journal_s
531c0 69 7a 65 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23 ize_limit"..*/.#
531d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 ifndef SQLITE_DE
531e0 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 FAULT_JOURNAL_SI
531f0 5a 45 5f 4c 49 4d 49 54 0a 20 20 23 64 65 66 69 ZE_LIMIT. #defi
53200 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ne SQLITE_DEFAUL
53210 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c T_JOURNAL_SIZE_L
53220 49 4d 49 54 20 2d 31 0a 23 65 6e 64 69 66 0a 0a IMIT -1.#endif..
53230 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 75 /*.** The type u
53240 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74 sed to represent
53250 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 a page number.
53260 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20 The first page
53270 69 6e 20 61 20 66 69 6c 65 0a 2a 2a 20 69 73 20 in a file.** is
53280 63 61 6c 6c 65 64 20 70 61 67 65 20 31 2e 20 20 called page 1.
53290 30 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 70 0 is used to rep
532a0 72 65 73 65 6e 74 20 22 6e 6f 74 20 61 20 70 61 resent "not a pa
532b0 67 65 22 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ge"..*/.typedef
532c0 75 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a u32 Pgno;../*.**
532d0 20 45 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 20 Each open file
532e0 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 61 20 is managed by a
532f0 73 65 70 61 72 61 74 65 20 69 6e 73 74 61 6e 63 separate instanc
53300 65 20 6f 66 20 74 68 65 20 22 50 61 67 65 72 22 e of the "Pager"
53310 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 structure..*/.t
53320 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61 ypedef struct Pa
53330 67 65 72 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a ger Pager;../*.*
53340 2a 20 48 61 6e 64 6c 65 20 74 79 70 65 20 66 6f * Handle type fo
53350 72 20 70 61 67 65 73 2e 0a 2a 2f 0a 74 79 70 65 r pages..*/.type
53360 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72 def struct PgHdr
53370 20 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20 DbPage;../*.**
53380 50 61 67 65 20 6e 75 6d 62 65 72 20 50 41 47 45 Page number PAGE
53390 52 5f 4d 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 76 R_MJ_PGNO is nev
533a0 65 72 20 75 73 65 64 20 69 6e 20 61 6e 20 53 51 er used in an SQ
533b0 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 28 69 Lite database (i
533c0 74 20 69 73 0a 2a 2a 20 72 65 73 65 72 76 65 64 t is.** reserved
533d0 20 66 6f 72 20 77 6f 72 6b 69 6e 67 20 61 72 6f for working aro
533e0 75 6e 64 20 61 20 77 69 6e 64 6f 77 73 2f 70 6f und a windows/po
533f0 73 69 78 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c six incompatibil
53400 69 74 79 29 2e 20 49 74 20 69 73 0a 2a 2a 20 75 ity). It is.** u
53410 73 65 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e sed in the journ
53420 61 6c 20 74 6f 20 73 69 67 6e 69 66 79 20 74 68 al to signify th
53430 61 74 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72 at the remainder
53440 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
53450 66 69 6c 65 20 0a 2a 2a 20 69 73 20 64 65 76 6f file .** is devo
53460 74 65 64 20 74 6f 20 73 74 6f 72 69 6e 67 20 61 ted to storing a
53470 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
53480 6e 61 6d 65 20 2d 20 74 68 65 72 65 20 61 72 65 name - there are
53490 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20 74 no more pages t
534a0 6f 0a 2a 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 o.** roll back.
534b0 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 See comments for
534c0 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4d function writeM
534d0 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 69 asterJournal() i
534e0 6e 20 70 61 67 65 72 2e 63 20 0a 2a 2a 20 66 6f n pager.c .** fo
534f0 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 r details..*/.#d
53500 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 efine PAGER_MJ_P
53510 47 4e 4f 28 78 29 20 28 28 50 67 6e 6f 29 28 28 GNO(x) ((Pgno)((
53520 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 PENDING_BYTE/((x
53530 29 2d 3e 70 61 67 65 53 69 7a 65 29 29 2b 31 29 )->pageSize))+1)
53540 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 )../*.** Allowed
53550 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 values for the
53560 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 flags parameter
53570 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f to sqlite3PagerO
53580 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 pen()..**.** NOT
53590 45 3a 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 E: These values
535a0 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65 20 63 must match the c
535b0 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 42 54 52 orresponding BTR
535c0 45 45 5f 20 76 61 6c 75 65 73 20 69 6e 20 62 74 EE_ values in bt
535d0 72 65 65 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e ree.h..*/.#defin
535e0 65 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 e PAGER_OMIT_JOU
535f0 52 4e 41 4c 20 20 30 78 30 30 30 31 20 20 20 20 RNAL 0x0001
53600 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20 61 20 /* Do not use a
53610 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
53620 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 */.#define PAGE
53630 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 R_NO_READLOCK
53640 30 78 30 30 30 32 20 20 20 20 2f 2a 20 4f 6d 69 0x0002 /* Omi
53650 74 20 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 t readlocks on r
53660 65 61 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f eadonly files */
53670 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 ../*.** Valid va
53680 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 65 63 lues for the sec
53690 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 ond argument to
536a0 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b sqlite3PagerLock
536b0 69 6e 67 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 ingMode()..*/.#d
536c0 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b efine PAGER_LOCK
536d0 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 INGMODE_QUERY
536e0 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 -1.#define PA
536f0 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
53700 4e 4f 52 4d 41 4c 20 20 20 20 20 20 30 0a 23 64 NORMAL 0.#d
53710 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b efine PAGER_LOCK
53720 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 INGMODE_EXCLUSIV
53730 45 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c E 1../*.** Val
53740 69 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 id values for th
53750 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
53760 74 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 t to sqlite3Page
53770 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 29 2e 0a rJournalMode()..
53780 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 */.#define PAGER
53790 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 _JOURNALMODE_QUE
537a0 52 59 20 20 20 20 20 20 2d 31 0a 23 64 65 66 69 RY -1.#defi
537b0 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c ne PAGER_JOURNAL
537c0 4d 4f 44 45 5f 44 45 4c 45 54 45 20 20 20 20 20 MODE_DELETE
537d0 20 30 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 0 /* Commit b
537e0 79 20 64 65 6c 65 74 69 6e 67 20 6a 6f 75 72 6e y deleting journ
537f0 61 6c 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 al file */.#defi
53800 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c ne PAGER_JOURNAL
53810 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 20 20 20 MODE_PERSIST
53820 20 31 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 1 /* Commit b
53830 79 20 7a 65 72 6f 69 6e 67 20 6a 6f 75 72 6e 61 y zeroing journa
53840 6c 20 68 65 61 64 65 72 20 2a 2f 0a 23 64 65 66 l header */.#def
53850 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 ine PAGER_JOURNA
53860 4c 4d 4f 44 45 5f 4f 46 46 20 20 20 20 20 20 20 LMODE_OFF
53870 20 20 32 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 2 /* Journal
53880 20 6f 6d 69 74 74 65 64 2e 20 20 2a 2f 0a 23 64 omitted. */.#d
53890 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 efine PAGER_JOUR
538a0 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 NALMODE_TRUNCATE
538b0 20 20 20 20 33 20 20 20 2f 2a 20 43 6f 6d 6d 69 3 /* Commi
538c0 74 20 62 79 20 74 72 75 6e 63 61 74 69 6e 67 20 t by truncating
538d0 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 journal */.#defi
538e0 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c ne PAGER_JOURNAL
538f0 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 MODE_MEMORY
53900 20 34 20 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 4 /* In-memor
53910 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a y journal file *
53920 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 6d /../*.** The rem
53930 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 66 ainder of this f
53940 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ile contains the
53950 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 declarations of
53960 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a the functions.*
53970 2a 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 74 * that make up t
53980 68 65 20 50 61 67 65 72 20 73 75 62 2d 73 79 73 he Pager sub-sys
53990 74 65 6d 20 41 50 49 2e 20 53 65 65 20 73 6f 75 tem API. See sou
539a0 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 rce code comment
539b0 73 20 66 6f 72 20 0a 2a 2a 20 61 20 64 65 74 61 s for .** a deta
539c0 69 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e iled description
539d0 20 6f 66 20 65 61 63 68 20 72 6f 75 74 69 6e 65 of each routine
539e0 2e 0a 2a 2f 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e ..*/../* Open an
539f0 64 20 63 6c 6f 73 65 20 61 20 50 61 67 65 72 20 d close a Pager
53a00 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a connection. */ .
53a10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
53a20 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f nt sqlite3PagerO
53a30 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 pen(. sqlite3_v
53a40 66 73 2a 2c 0a 20 20 50 61 67 65 72 20 2a 2a 70 fs*,. Pager **p
53a50 70 50 61 67 65 72 2c 0a 20 20 63 6f 6e 73 74 20 pPager,. const
53a60 63 68 61 72 2a 2c 0a 20 20 69 6e 74 2c 0a 20 20 char*,. int,.
53a70 69 6e 74 2c 0a 20 20 69 6e 74 2c 0a 20 20 76 6f int,. int,. vo
53a80 69 64 28 2a 29 28 44 62 50 61 67 65 2a 29 0a 29 id(*)(DbPage*).)
53a90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
53aa0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
53ab0 72 43 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50 rClose(Pager *pP
53ac0 61 67 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ager);.SQLITE_PR
53ad0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
53ae0 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 3PagerReadFilehe
53af0 61 64 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 ader(Pager*, int
53b00 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a , unsigned char*
53b10 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 );../* Functions
53b20 20 75 73 65 64 20 74 6f 20 63 6f 6e 66 69 67 75 used to configu
53b30 72 65 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63 re a Pager objec
53b40 74 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t. */.SQLITE_PRI
53b50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
53b60 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 3PagerSetBusyhan
53b70 64 6c 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 dler(Pager*, int
53b80 28 2a 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 (*)(void *), voi
53b90 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 d *);.SQLITE_PRI
53ba0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
53bb0 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 PagerSetPagesize
53bc0 28 50 61 67 65 72 2a 2c 20 75 31 36 2a 2c 20 69 (Pager*, u16*, i
53bd0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
53be0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
53bf0 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 agerMaxPageCount
53c00 28 50 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 (Pager*, int);.S
53c10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
53c20 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 id sqlite3PagerS
53c30 65 74 43 61 63 68 65 73 69 7a 65 28 50 61 67 65 etCachesize(Page
53c40 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 r*, int);.SQLITE
53c50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
53c60 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66 lite3PagerSetSaf
53c70 65 74 79 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c etyLevel(Pager*,
53c80 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 int,int);.SQLITE
53c90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
53ca0 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 ite3PagerLocking
53cb0 4d 6f 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e Mode(Pager *, in
53cc0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
53cd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
53ce0 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 gerJournalMode(P
53cf0 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 ager *, int);.SQ
53d00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 LITE_PRIVATE i64
53d10 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 sqlite3PagerJou
53d20 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61 rnalSizeLimit(Pa
53d30 67 65 72 20 2a 2c 20 69 36 34 29 3b 0a 53 51 4c ger *, i64);.SQL
53d40 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 ITE_PRIVATE sqli
53d50 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 73 71 6c te3_backup **sql
53d60 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 ite3PagerBackupP
53d70 74 72 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 tr(Pager*);../*
53d80 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 Functions used t
53d90 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c o obtain and rel
53da0 65 61 73 65 20 70 61 67 65 20 72 65 66 65 72 65 ease page refere
53db0 6e 63 65 73 2e 20 2a 2f 20 0a 53 51 4c 49 54 45 nces. */ .SQLITE
53dc0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
53dd0 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65 ite3PagerAcquire
53de0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
53df0 50 67 6e 6f 20 70 67 6e 6f 2c 20 44 62 50 61 67 Pgno pgno, DbPag
53e00 65 20 2a 2a 70 70 50 61 67 65 2c 20 69 6e 74 20 e **ppPage, int
53e10 63 6c 72 46 6c 61 67 29 3b 0a 23 64 65 66 69 6e clrFlag);.#defin
53e20 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 e sqlite3PagerGe
53e30 74 28 41 2c 42 2c 43 29 20 73 71 6c 69 74 65 33 t(A,B,C) sqlite3
53e40 50 61 67 65 72 41 63 71 75 69 72 65 28 41 2c 42 PagerAcquire(A,B
53e50 2c 43 2c 30 29 0a 53 51 4c 49 54 45 5f 50 52 49 ,C,0).SQLITE_PRI
53e60 56 41 54 45 20 44 62 50 61 67 65 20 2a 73 71 6c VATE DbPage *sql
53e70 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 ite3PagerLookup(
53e80 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 Pager *pPager, P
53e90 67 6e 6f 20 70 67 6e 6f 29 3b 0a 53 51 4c 49 54 gno pgno);.SQLIT
53ea0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
53eb0 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 44 qlite3PagerRef(D
53ec0 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f bPage*);.SQLITE_
53ed0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
53ee0 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44 ite3PagerUnref(D
53ef0 62 50 61 67 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65 bPage*);../* Ope
53f00 72 61 74 69 6f 6e 73 20 6f 6e 20 70 61 67 65 20 rations on page
53f10 72 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f 0a 53 references. */.S
53f20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
53f30 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 t sqlite3PagerWr
53f40 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 ite(DbPage*);.SQ
53f50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
53f60 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f d sqlite3PagerDo
53f70 6e 74 57 72 69 74 65 28 44 62 50 61 67 65 2a 29 ntWrite(DbPage*)
53f80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
53f90 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
53fa0 72 4d 6f 76 65 70 61 67 65 28 50 61 67 65 72 2a rMovepage(Pager*
53fb0 2c 44 62 50 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e ,DbPage*,Pgno,in
53fc0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
53fd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
53fe0 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 gerPageRefcount(
53ff0 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 DbPage*);.SQLITE
54000 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
54010 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
54020 74 61 28 44 62 50 61 67 65 20 2a 29 3b 20 0a 53 ta(DbPage *); .S
54030 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
54040 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 id *sqlite3Pager
54050 47 65 74 45 78 74 72 61 28 44 62 50 61 67 65 20 GetExtra(DbPage
54060 2a 29 3b 20 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f *); ../* Functio
54070 6e 73 20 75 73 65 64 20 74 6f 20 6d 61 6e 61 67 ns used to manag
54080 65 20 70 61 67 65 72 20 74 72 61 6e 73 61 63 74 e pager transact
54090 69 6f 6e 73 20 61 6e 64 20 73 61 76 65 70 6f 69 ions and savepoi
540a0 6e 74 73 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 nts. */.SQLITE_P
540b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
540c0 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
540d0 28 50 61 67 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a (Pager*, int*);.
540e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
540f0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 nt sqlite3PagerB
54100 65 67 69 6e 28 50 61 67 65 72 2a 2c 20 69 6e 74 egin(Pager*, int
54110 20 65 78 46 6c 61 67 2c 20 69 6e 74 29 3b 0a 53 exFlag, int);.S
54120 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
54130 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f t sqlite3PagerCo
54140 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 50 61 67 mmitPhaseOne(Pag
54150 65 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a er*,const char *
54160 7a 4d 61 73 74 65 72 2c 20 69 6e 74 29 3b 0a 53 zMaster, int);.S
54170 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
54180 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 t sqlite3PagerSy
54190 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 nc(Pager *pPager
541a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
541b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
541c0 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f erCommitPhaseTwo
541d0 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 (Pager*);.SQLITE
541e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
541f0 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 ite3PagerRollbac
54200 6b 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 k(Pager*);.SQLIT
54210 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
54220 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 lite3PagerOpenSa
54230 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 vepoint(Pager *p
54240 50 61 67 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53 Pager, int n);.S
54250 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
54260 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 t sqlite3PagerSa
54270 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 vepoint(Pager *p
54280 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 Pager, int op, i
54290 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a nt iSavepoint);.
542a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
542b0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 nt sqlite3PagerS
542c0 68 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 haredLock(Pager
542d0 2a 70 50 61 67 65 72 29 3b 0a 0a 2f 2a 20 46 75 *pPager);../* Fu
542e0 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 nctions used to
542f0 71 75 65 72 79 20 70 61 67 65 72 20 73 74 61 74 query pager stat
54300 65 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 61 74 e and configurat
54310 69 6f 6e 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion. */.SQLITE_P
54320 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 RIVATE u8 sqlite
54330 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 3PagerIsreadonly
54340 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 (Pager*);.SQLITE
54350 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
54360 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e ite3PagerRefcoun
54370 74 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 t(Pager*);.SQLIT
54380 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
54390 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 char *sqlite3Pag
543a0 65 72 46 69 6c 65 6e 61 6d 65 28 50 61 67 65 72 erFilename(Pager
543b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
543c0 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 TE const sqlite3
543d0 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67 _vfs *sqlite3Pag
543e0 65 72 56 66 73 28 50 61 67 65 72 2a 29 3b 0a 53 erVfs(Pager*);.S
543f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 QLITE_PRIVATE sq
54400 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69 lite3_file *sqli
54410 74 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 67 te3PagerFile(Pag
54420 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 er*);.SQLITE_PRI
54430 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 VATE const char
54440 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 *sqlite3PagerJou
54450 72 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 2a 29 rnalname(Pager*)
54460 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
54470 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
54480 72 4e 6f 73 79 6e 63 28 50 61 67 65 72 2a 29 3b rNosync(Pager*);
54490 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
544a0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 void *sqlite3Pag
544b0 65 72 54 65 6d 70 53 70 61 63 65 28 50 61 67 65 erTempSpace(Page
544c0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
544d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
544e0 61 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65 agerIsMemdb(Page
544f0 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f r*);../* Functio
54500 6e 73 20 75 73 65 64 20 74 6f 20 74 72 75 6e 63 ns used to trunc
54510 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ate the database
54520 20 66 69 6c 65 2e 20 2a 2f 0a 53 51 4c 49 54 45 file. */.SQLITE
54530 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
54540 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 lite3PagerTrunca
54550 74 65 49 6d 61 67 65 28 50 61 67 65 72 2a 2c 50 teImage(Pager*,P
54560 67 6e 6f 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 gno);../* Functi
54570 6f 6e 73 20 74 6f 20 73 75 70 70 6f 72 74 20 74 ons to support t
54580 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 esting and debug
54590 67 69 6e 67 2e 20 2a 2f 0a 23 69 66 20 21 64 65 ging. */.#if !de
545a0 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c fined(NDEBUG) ||
545b0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
545c0 54 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 TEST).SQLITE_PRI
545d0 56 41 54 45 20 20 20 50 67 6e 6f 20 73 71 6c 69 VATE Pgno sqli
545e0 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 te3PagerPagenumb
545f0 65 72 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c er(DbPage*);.SQL
54600 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e ITE_PRIVATE in
54610 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 t sqlite3PagerIs
54620 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65 writeable(DbPage
54630 2a 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 *);.#endif.#ifde
54640 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
54650 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
54660 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 nt *sqlite3Pager
54670 53 74 61 74 73 28 50 61 67 65 72 2a 29 3b 0a 53 Stats(Pager*);.S
54680 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
54690 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
546a0 72 52 65 66 64 75 6d 70 28 50 61 67 65 72 2a 29 rRefdump(Pager*)
546b0 3b 0a 20 20 76 6f 69 64 20 64 69 73 61 62 6c 65 ;. void disable
546c0 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 _simulated_io_er
546d0 72 6f 72 73 28 76 6f 69 64 29 3b 0a 20 20 76 6f rors(void);. vo
546e0 69 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 id enable_simula
546f0 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f ted_io_errors(vo
54700 69 64 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 id);.#else.# def
54710 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 ine disable_simu
54720 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 lated_io_errors(
54730 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c ).# define enabl
54740 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 e_simulated_io_e
54750 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a rrors().#endif..
54760 23 65 6e 64 69 66 20 2f 2a 20 5f 50 41 47 45 52 #endif /* _PAGER
54770 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a _H_ */../*******
54780 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 ******* End of p
54790 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ager.h *********
547a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
547b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
547c0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
547d0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
547e0 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
547f0 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e off in sqliteIn
54800 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
54810 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
54820 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 ******* Include
54830 70 63 61 63 68 65 2e 68 20 69 6e 20 74 68 65 20 pcache.h in the
54840 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 middle of sqlite
54850 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
54860 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
54870 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
54880 6c 65 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a le pcache.h ****
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 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
548c0 30 38 20 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a 08 August 05.**.
548d0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
548e0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
548f0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
54900 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
54910 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
54920 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
54930 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
54940 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
54950 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
54960 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
54970 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
54980 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
54990 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
549a0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
549b0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
549c0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
549d0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
549e0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
549f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
54a30 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 his header file
54a40 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 defines the inte
54a50 72 66 61 63 65 20 74 68 61 74 20 74 68 65 20 73 rface that the s
54a60 71 6c 69 74 65 20 70 61 67 65 20 63 61 63 68 65 qlite page cache
54a70 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 0a .** subsystem. .
54a80 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 **.** @(#) $Id:
54a90 70 63 61 63 68 65 2e 68 2c 76 20 31 2e 32 30 20 pcache.h,v 1.20
54aa0 32 30 30 39 2f 30 37 2f 32 35 20 31 31 3a 34 36 2009/07/25 11:46
54ab0 3a 34 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :49 danielk1977
54ac0 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 Exp $.*/..#ifnde
54ad0 66 20 5f 50 43 41 43 48 45 5f 48 5f 0a 0a 74 79 f _PCACHE_H_..ty
54ae0 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48 pedef struct PgH
54af0 64 72 20 50 67 48 64 72 3b 0a 74 79 70 65 64 65 dr PgHdr;.typede
54b00 66 20 73 74 72 75 63 74 20 50 43 61 63 68 65 20 f struct PCache
54b10 50 43 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 PCache;../*.** E
54b20 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 very page in the
54b30 20 63 61 63 68 65 20 69 73 20 63 6f 6e 74 72 6f cache is contro
54b40 6c 6c 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 lled by an insta
54b50 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
54b60 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 wing.** structur
54b70 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67 48 e..*/.struct PgH
54b80 64 72 20 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61 dr {. void *pDa
54b90 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ta;
54ba0 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 /* Content
54bb0 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f of this page */
54bc0 0a 20 20 76 6f 69 64 20 2a 70 45 78 74 72 61 3b . void *pExtra;
54bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54be0 20 20 2f 2a 20 45 78 74 72 61 20 63 6f 6e 74 65 /* Extra conte
54bf0 6e 74 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 nt */. PgHdr *p
54c00 44 69 72 74 79 3b 20 20 20 20 20 20 20 20 20 20 Dirty;
54c10 20 20 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 69 /* Transi
54c20 65 6e 74 20 6c 69 73 74 20 6f 66 20 64 69 72 74 ent list of dirt
54c30 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 6e y pages */. Pgn
54c40 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 o pgno;
54c50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
54c60 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 age number for t
54c70 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 61 his page */. Pa
54c80 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 ger *pPager;
54c90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
54ca0 54 68 65 20 70 61 67 65 72 20 74 68 69 73 20 70 The pager this p
54cb0 61 67 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a age is part of *
54cc0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
54cd0 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 75 33 CHECK_PAGES. u3
54ce0 32 20 70 61 67 65 48 61 73 68 3b 20 20 20 20 20 2 pageHash;
54cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
54d00 48 61 73 68 20 6f 66 20 70 61 67 65 20 63 6f 6e Hash of page con
54d10 74 65 6e 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 tent */.#endif.
54d20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 u16 flags;
54d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54d40 2f 2a 20 50 47 48 44 52 20 66 6c 61 67 73 20 64 /* PGHDR flags d
54d50 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a efined below */.
54d60 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a . /************
54d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 **********. **
54db0 45 6c 65 6d 65 6e 74 73 20 61 62 6f 76 65 20 61 Elements above a
54dc0 72 65 20 70 75 62 6c 69 63 2e 20 20 41 6c 6c 20 re public. All
54dd0 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20 that follows is
54de0 70 72 69 76 61 74 65 20 74 6f 20 70 63 61 63 68 private to pcach
54df0 65 2e 63 0a 20 20 2a 2a 20 61 6e 64 20 73 68 6f e.c. ** and sho
54e00 75 6c 64 20 6e 6f 74 20 62 65 20 61 63 63 65 73 uld not be acces
54e10 73 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 sed by other mod
54e20 75 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 31 36 ules.. */. i16
54e30 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 nRef;
54e40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
54e50 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73 20 6f umber of users o
54e60 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 f this page */.
54e70 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b PCache *pCache;
54e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54e90 2f 2a 20 43 61 63 68 65 20 74 68 61 74 20 6f 77 /* Cache that ow
54ea0 6e 73 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a ns this page */.
54eb0 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 . PgHdr *pDirty
54ec0 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 Next;
54ed0 20 20 2f 2a 20 4e 65 78 74 20 65 6c 65 6d 65 6e /* Next elemen
54ee0 74 20 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 t in list of dir
54ef0 74 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 ty pages */. Pg
54f00 48 64 72 20 2a 70 44 69 72 74 79 50 72 65 76 3b Hdr *pDirtyPrev;
54f10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
54f20 50 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 Previous element
54f30 20 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74 in list of dirt
54f40 79 20 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f y pages */.};../
54f50 2a 20 42 69 74 20 76 61 6c 75 65 73 20 66 6f 72 * Bit values for
54f60 20 50 67 48 64 72 2e 66 6c 61 67 73 20 2a 2f 0a PgHdr.flags */.
54f70 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44 49 #define PGHDR_DI
54f80 52 54 59 20 20 20 20 20 20 20 20 20 20 20 20 20 RTY
54f90 30 78 30 30 32 20 20 2f 2a 20 50 61 67 65 20 68 0x002 /* Page h
54fa0 61 73 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 as changed */.#d
54fb0 65 66 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 efine PGHDR_NEED
54fc0 5f 53 59 4e 43 20 20 20 20 20 20 20 20 20 30 78 _SYNC 0x
54fd0 30 30 34 20 20 2f 2a 20 46 73 79 6e 63 20 74 68 004 /* Fsync th
54fe0 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
54ff0 61 6c 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20 al before.
55000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
55010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
55020 20 2a 2a 20 77 72 69 74 69 6e 67 20 74 68 69 73 ** writing this
55030 20 70 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 page to the dat
55040 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 abase */.#define
55050 20 50 47 48 44 52 5f 4e 45 45 44 5f 52 45 41 44 PGHDR_NEED_READ
55060 20 20 20 20 20 20 20 20 20 30 78 30 30 38 20 20 0x008
55070 2f 2a 20 43 6f 6e 74 65 6e 74 20 69 73 20 75 6e /* Content is un
55080 72 65 61 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 read */.#define
55090 50 47 48 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49 PGHDR_REUSE_UNLI
550a0 4b 45 4c 59 20 20 20 20 30 78 30 31 30 20 20 2f KELY 0x010 /
550b0 2a 20 41 20 68 69 6e 74 20 74 68 61 74 20 72 65 * A hint that re
550c0 75 73 65 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20 use is unlikely
550d0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 */.#define PGHDR
550e0 5f 44 4f 4e 54 5f 57 52 49 54 45 20 20 20 20 20 _DONT_WRITE
550f0 20 20 20 30 78 30 32 30 20 20 2f 2a 20 44 6f 20 0x020 /* Do
55100 6e 6f 74 20 77 72 69 74 65 20 63 6f 6e 74 65 6e not write conten
55110 74 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a t to disk */../*
55120 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 Initialize and
55130 73 68 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67 shutdown the pag
55140 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 e cache subsyste
55150 6d 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 m */.SQLITE_PRIV
55160 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
55170 63 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 cacheInitialize(
55180 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 void);.SQLITE_PR
55190 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
551a0 65 33 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e e3PcacheShutdown
551b0 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 50 61 67 65 (void);../* Page
551c0 20 63 61 63 68 65 20 62 75 66 66 65 72 20 6d 61 cache buffer ma
551d0 6e 61 67 65 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65 nagement:.** The
551e0 73 65 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c se routines impl
551f0 65 6d 65 6e 74 20 53 51 4c 49 54 45 5f 43 4f 4e ement SQLITE_CON
55200 46 49 47 5f 50 41 47 45 43 41 43 48 45 2e 0a 2a FIG_PAGECACHE..*
55210 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
55220 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 void sqlite3PCa
55230 63 68 65 42 75 66 66 65 72 53 65 74 75 70 28 76 cheBufferSetup(v
55240 6f 69 64 20 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 oid *, int sz, i
55250 6e 74 20 6e 29 3b 0a 0a 2f 2a 20 43 72 65 61 74 nt n);../* Creat
55260 65 20 61 20 6e 65 77 20 70 61 67 65 72 20 63 61 e a new pager ca
55270 63 68 65 2e 0a 2a 2a 20 55 6e 64 65 72 20 6d 65 che..** Under me
55280 6d 6f 72 79 20 73 74 72 65 73 73 2c 20 69 6e 76 mory stress, inv
55290 6f 6b 65 20 78 53 74 72 65 73 73 20 74 6f 20 74 oke xStress to t
552a0 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 ry to make pages
552b0 20 63 6c 65 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20 clean..** Only
552c0 63 6c 65 61 6e 20 61 6e 64 20 75 6e 70 69 6e 6e clean and unpinn
552d0 65 64 20 70 61 67 65 73 20 63 61 6e 20 62 65 20 ed pages can be
552e0 72 65 63 6c 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51 reclaimed..*/.SQ
552f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
55300 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f d sqlite3PcacheO
55310 70 65 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 67 pen(. int szPag
55320 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
55330 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
55340 20 65 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 every page */.
55350 20 69 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 int szExtra,
55360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
55370 2f 2a 20 45 78 74 72 61 20 73 70 61 63 65 20 61 /* Extra space a
55380 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 ssociated with e
55390 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e ach page */. in
553a0 74 20 62 50 75 72 67 65 61 62 6c 65 2c 20 20 20 t bPurgeable,
553b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
553c0 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72 True if pages ar
553d0 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f e on backing sto
553e0 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 re */. int (*xS
553f0 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 20 50 67 tress)(void*, Pg
55400 48 64 72 2a 29 2c 20 2f 2a 20 43 61 6c 6c 20 74 Hdr*), /* Call t
55410 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 o try to make pa
55420 67 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 ges clean */. v
55430 6f 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 oid *pStress,
55440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
55450 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 Argument to xSt
55460 72 65 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 ress */. PCache
55470 20 2a 70 54 6f 49 6e 69 74 20 20 20 20 20 20 20 *pToInit
55480 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 /* Prea
55490 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 llocated space f
554a0 6f 72 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f or the PCache */
554b0 0a 29 3b 0a 0a 2f 2a 20 4d 6f 64 69 66 79 20 74 .);../* Modify t
554c0 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 66 74 he page-size aft
554d0 65 72 20 74 68 65 20 63 61 63 68 65 20 68 61 73 er the cache has
554e0 20 62 65 65 6e 20 63 72 65 61 74 65 64 2e 20 2a been created. *
554f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
55500 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
55510 63 68 65 53 65 74 50 61 67 65 53 69 7a 65 28 50 cheSetPageSize(P
55520 43 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a Cache *, int);..
55530 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 /* Return the si
55540 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 61 ze in bytes of a
55550 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 PCache object.
55560 20 55 73 65 64 20 74 6f 20 70 72 65 61 6c 6c 6f Used to preallo
55570 63 61 74 65 0a 2a 2a 20 73 74 6f 72 61 67 65 20 cate.** storage
55580 73 70 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 space..*/.SQLITE
55590 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
555a0 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 76 ite3PcacheSize(v
555b0 6f 69 64 29 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65 oid);../* One re
555c0 6c 65 61 73 65 20 70 65 72 20 73 75 63 63 65 73 lease per succes
555d0 73 66 75 6c 20 66 65 74 63 68 2e 20 20 50 61 67 sful fetch. Pag
555e0 65 20 69 73 20 70 69 6e 6e 65 64 20 75 6e 74 69 e is pinned unti
555f0 6c 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 20 52 l released..** R
55600 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 65 64 eference counted
55610 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 . .*/.SQLITE_PRI
55620 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
55630 50 63 61 63 68 65 46 65 74 63 68 28 50 43 61 63 PcacheFetch(PCac
55640 68 65 2a 2c 20 50 67 6e 6f 2c 20 69 6e 74 20 63 he*, Pgno, int c
55650 72 65 61 74 65 46 6c 61 67 2c 20 50 67 48 64 72 reateFlag, PgHdr
55660 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
55670 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
55680 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 PcacheRelease(Pg
55690 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 Hdr*);..SQLITE_P
556a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
556b0 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 50 67 te3PcacheDrop(Pg
556c0 48 64 72 2a 29 3b 20 20 20 20 20 20 20 20 20 2f Hdr*); /
556d0 2a 20 52 65 6d 6f 76 65 20 70 61 67 65 20 66 72 * Remove page fr
556e0 6f 6d 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 om cache */.SQLI
556f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
55700 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b sqlite3PcacheMak
55710 65 44 69 72 74 79 28 50 67 48 64 72 2a 29 3b 20 eDirty(PgHdr*);
55720 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 /* Make sure
55730 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 64 page is marked d
55740 69 72 74 79 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 irty */.SQLITE_P
55750 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
55760 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 te3PcacheMakeCle
55770 61 6e 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f an(PgHdr*); /
55780 2a 20 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 * Mark a single
55790 70 61 67 65 20 61 73 20 63 6c 65 61 6e 20 2a 2f page as clean */
557a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
557b0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
557c0 68 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 heCleanAll(PCach
557d0 65 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20 e*); /* Mark
557e0 61 6c 6c 20 64 69 72 74 79 20 6c 69 73 74 20 70 all dirty list p
557f0 61 67 65 73 20 61 73 20 63 6c 65 61 6e 20 2a 2f ages as clean */
55800 0a 0a 2f 2a 20 43 68 61 6e 67 65 20 61 20 70 61 ../* Change a pa
55810 67 65 20 6e 75 6d 62 65 72 2e 20 20 55 73 65 64 ge number. Used
55820 20 62 79 20 69 6e 63 72 2d 76 61 63 75 75 6d 2e by incr-vacuum.
55830 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
55840 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
55850 63 61 63 68 65 4d 6f 76 65 28 50 67 48 64 72 2a cacheMove(PgHdr*
55860 2c 20 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d , Pgno);../* Rem
55870 6f 76 65 20 61 6c 6c 20 70 61 67 65 73 20 77 69 ove all pages wi
55880 74 68 20 70 67 6e 6f 3e 78 2e 20 20 52 65 73 65 th pgno>x. Rese
55890 74 20 74 68 65 20 63 61 63 68 65 20 69 66 20 78 t the cache if x
558a0 3d 3d 30 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ==0 */.SQLITE_PR
558b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
558c0 65 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 e3PcacheTruncate
558d0 28 50 43 61 63 68 65 2a 2c 20 50 67 6e 6f 20 78 (PCache*, Pgno x
558e0 29 3b 0a 0a 2f 2a 20 47 65 74 20 61 20 6c 69 73 );../* Get a lis
558f0 74 20 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 70 t of all dirty p
55900 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 ages in the cach
55910 65 2c 20 73 6f 72 74 65 64 20 62 79 20 70 61 67 e, sorted by pag
55920 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 53 51 4c 49 e number */.SQLI
55930 54 45 5f 50 52 49 56 41 54 45 20 50 67 48 64 72 TE_PRIVATE PgHdr
55940 20 2a 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 *sqlite3PcacheD
55950 69 72 74 79 4c 69 73 74 28 50 43 61 63 68 65 2a irtyList(PCache*
55960 29 3b 0a 0a 2f 2a 20 52 65 73 65 74 20 61 6e 64 );../* Reset and
55970 20 63 6c 6f 73 65 20 74 68 65 20 63 61 63 68 65 close the cache
55980 20 6f 62 6a 65 63 74 20 2a 2f 0a 53 51 4c 49 54 object */.SQLIT
55990 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
559a0 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 qlite3PcacheClos
559b0 65 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 e(PCache*);../*
559c0 43 6c 65 61 72 20 66 6c 61 67 73 20 66 72 6f 6d Clear flags from
559d0 20 70 61 67 65 73 20 6f 66 20 74 68 65 20 70 61 pages of the pa
559e0 67 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 ge cache */.SQLI
559f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
55a00 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 sqlite3PcacheCle
55a10 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43 61 63 arSyncFlags(PCac
55a20 68 65 20 2a 29 3b 0a 0a 2f 2a 20 44 69 73 63 61 he *);../* Disca
55a30 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 rd the contents
55a40 6f 66 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a of the cache */.
55a50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
55a60 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
55a70 65 43 6c 65 61 72 28 50 43 61 63 68 65 2a 29 3b eClear(PCache*);
55a80 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 ../* Return the
55a90 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
55aa0 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 outstanding page
55ab0 20 72 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a 53 references */.S
55ac0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
55ad0 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 t sqlite3PcacheR
55ae0 65 66 43 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 efCount(PCache*)
55af0 3b 0a 0a 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 ;../* Increment
55b00 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f the reference co
55b10 75 6e 74 20 6f 66 20 61 6e 20 65 78 69 73 74 69 unt of an existi
55b20 6e 67 20 70 61 67 65 20 2a 2f 0a 53 51 4c 49 54 ng page */.SQLIT
55b30 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
55b40 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 28 qlite3PcacheRef(
55b50 50 67 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 PgHdr*);..SQLITE
55b60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
55b70 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65 ite3PcachePageRe
55b80 66 63 6f 75 6e 74 28 50 67 48 64 72 2a 29 3b 0a fcount(PgHdr*);.
55b90 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 ./* Return the t
55ba0 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 otal number of p
55bb0 61 67 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74 ages stored in t
55bc0 68 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 he cache */.SQLI
55bd0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
55be0 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 qlite3PcachePage
55bf0 63 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a count(PCache*);.
55c00 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
55c10 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 ITE_CHECK_PAGES)
55c20 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 || defined(SQLI
55c30 54 45 5f 44 45 42 55 47 29 0a 2f 2a 20 49 74 65 TE_DEBUG)./* Ite
55c40 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 6c 6c rate through all
55c50 20 64 69 72 74 79 20 70 61 67 65 73 20 63 75 72 dirty pages cur
55c60 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e rently stored in
55c70 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 69 73 the cache. This
55c80 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 .** interface is
55c90 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 only available
55ca0 69 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f if SQLITE_CHECK_
55cb0 50 41 47 45 53 20 69 73 20 64 65 66 69 6e 65 64 PAGES is defined
55cc0 20 77 68 65 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 when the .** li
55cd0 62 72 61 72 79 20 69 73 20 62 75 69 6c 74 2e 0a brary is built..
55ce0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
55cf0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
55d00 61 63 68 65 49 74 65 72 61 74 65 44 69 72 74 79 acheIterateDirty
55d10 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c (PCache *pCache,
55d20 20 76 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50 void (*xIter)(P
55d30 67 48 64 72 20 2a 29 29 3b 0a 23 65 6e 64 69 66 gHdr *));.#endif
55d40 0a 0a 2f 2a 20 53 65 74 20 61 6e 64 20 67 65 74 ../* Set and get
55d50 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63 the suggested c
55d60 61 63 68 65 2d 73 69 7a 65 20 66 6f 72 20 74 68 ache-size for th
55d70 65 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 e specified page
55d80 72 2d 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 49 r-cache..**.** I
55d90 66 20 6e 6f 20 67 6c 6f 62 61 6c 20 6d 61 78 69 f no global maxi
55da0 6d 75 6d 20 69 73 20 63 6f 6e 66 69 67 75 72 65 mum is configure
55db0 64 2c 20 74 68 65 6e 20 74 68 65 20 73 79 73 74 d, then the syst
55dc0 65 6d 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c em attempts to l
55dd0 69 6d 69 74 0a 2a 2a 20 74 68 65 20 74 6f 74 61 imit.** the tota
55de0 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 l number of page
55df0 73 20 63 61 63 68 65 64 20 62 79 20 70 75 72 67 s cached by purg
55e00 65 61 62 6c 65 20 70 61 67 65 72 2d 63 61 63 68 eable pager-cach
55e10 65 73 20 74 6f 20 74 68 65 20 73 75 6d 0a 2a 2a es to the sum.**
55e20 20 6f 66 20 74 68 65 20 73 75 67 67 65 73 74 65 of the suggeste
55e30 64 20 63 61 63 68 65 2d 73 69 7a 65 73 2e 0a 2a d cache-sizes..*
55e40 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
55e50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
55e60 63 68 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 cheSetCachesize(
55e70 50 43 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a PCache *, int);.
55e80 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
55e90 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ST.SQLITE_PRIVAT
55ea0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 E int sqlite3Pca
55eb0 63 68 65 47 65 74 43 61 63 68 65 73 69 7a 65 28 cheGetCachesize(
55ec0 50 43 61 63 68 65 20 2a 29 3b 0a 23 65 6e 64 69 PCache *);.#endi
55ed0 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 f..#ifdef SQLITE
55ee0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d _ENABLE_MEMORY_M
55ef0 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 20 54 72 79 ANAGEMENT./* Try
55f00 20 74 6f 20 72 65 74 75 72 6e 20 6d 65 6d 6f 72 to return memor
55f10 79 20 75 73 65 64 20 62 79 20 74 68 65 20 70 63 y used by the pc
55f20 61 63 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20 74 ache module to t
55f30 68 65 20 6d 61 69 6e 20 6d 65 6d 6f 72 79 20 68 he main memory h
55f40 65 61 70 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 eap */.SQLITE_PR
55f50 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
55f60 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 4d 65 3PcacheReleaseMe
55f70 6d 6f 72 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69 mory(int);.#endi
55f80 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 f..#ifdef SQLITE
55f90 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 _TEST.SQLITE_PRI
55fa0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
55fb0 33 50 63 61 63 68 65 53 74 61 74 73 28 69 6e 74 3PcacheStats(int
55fc0 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a *,int*,int*,int*
55fd0 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 );.#endif..SQLIT
55fe0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
55ff0 71 6c 69 74 65 33 50 43 61 63 68 65 53 65 74 44 qlite3PCacheSetD
56000 65 66 61 75 6c 74 28 76 6f 69 64 29 3b 0a 0a 23 efault(void);..#
56010 65 6e 64 69 66 20 2f 2a 20 5f 50 43 41 43 48 45 endif /* _PCACHE
56020 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a _H_ */../*******
56030 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 ******* End of p
56040 63 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a cache.h ********
56050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56070 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
56080 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
56090 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
560a0 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e off in sqliteIn
560b0 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
560c0 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a ******/../******
560d0 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 ******** Include
560e0 20 6f 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 os.h in the mid
560f0 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 dle of sqliteInt
56100 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
56110 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
56120 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
56130 69 6c 65 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a ile os.h *******
56140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56160 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
56170 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 36 001 September 16
56180 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
56190 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
561a0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
561b0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
561c0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
561d0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
561e0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
561f0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
56200 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
56210 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
56220 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
56230 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
56240 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
56250 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
56260 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
56270 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
56280 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
56290 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
562a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
562b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
562c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
562d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
562e0 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
562f0 68 65 61 64 65 72 20 66 69 6c 65 20 28 74 6f 67 header file (tog
56300 65 74 68 65 72 20 77 69 74 68 20 69 73 20 63 6f ether with is co
56310 6d 70 61 6e 69 6f 6e 20 43 20 73 6f 75 72 63 65 mpanion C source
56320 2d 63 6f 64 65 20 66 69 6c 65 0a 2a 2a 20 22 6f -code file.** "o
56330 73 2e 63 22 29 20 61 74 74 65 6d 70 74 20 74 6f s.c") attempt to
56340 20 61 62 73 74 72 61 63 74 20 74 68 65 20 75 6e abstract the un
56350 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 derlying operati
56360 6e 67 20 73 79 73 74 65 6d 20 73 6f 20 74 68 61 ng system so tha
56370 74 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 t.** the SQLite
56380 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 77 6f 72 library will wor
56390 6b 20 6f 6e 20 62 6f 74 68 20 50 4f 53 49 58 20 k on both POSIX
563a0 61 6e 64 20 77 69 6e 64 6f 77 73 20 73 79 73 74 and windows syst
563b0 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ems..**.** This
563c0 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20 23 header file is #
563d0 69 6e 63 6c 75 64 65 2d 65 64 20 62 79 20 73 71 include-ed by sq
563e0 6c 69 74 65 49 6e 74 2e 68 20 61 6e 64 20 74 68 liteInt.h and th
563f0 75 73 20 65 6e 64 73 20 75 70 0a 2a 2a 20 62 65 us ends up.** be
56400 69 6e 67 20 69 6e 63 6c 75 64 65 64 20 62 79 20 ing included by
56410 65 76 65 72 79 20 73 6f 75 72 63 65 20 66 69 6c every source fil
56420 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 e..**.** $Id: os
56430 2e 68 2c 76 20 31 2e 31 30 38 20 32 30 30 39 2f .h,v 1.108 2009/
56440 30 32 2f 30 35 20 31 36 3a 33 31 3a 34 36 20 64 02/05 16:31:46 d
56450 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e rh Exp $.*/.#ifn
56460 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 def _SQLITE_OS_H
56470 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 _.#define _SQLIT
56480 45 5f 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 46 E_OS_H_../*.** F
56490 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 igure out if we
564a0 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 are dealing with
564b0 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20 Unix, Windows,
564c0 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a or some other.**
564d0 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 operating syste
564e0 6d 2e 20 20 41 66 74 65 72 20 74 68 65 20 66 6f m. After the fo
564f0 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 llowing block of
56500 20 70 72 65 70 72 6f 63 65 73 73 20 6d 61 63 72 preprocess macr
56510 6f 73 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20 53 51 os,.** all of SQ
56520 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 2c 20 53 51 LITE_OS_UNIX, SQ
56530 4c 49 54 45 5f 4f 53 5f 57 49 4e 2c 20 53 51 4c LITE_OS_WIN, SQL
56540 49 54 45 5f 4f 53 5f 4f 53 32 2c 20 61 6e 64 20 ITE_OS_OS2, and
56550 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 20 SQLITE_OS_OTHER
56560 0a 2a 2a 20 77 69 6c 6c 20 64 65 66 69 6e 65 64 .** will defined
56570 20 74 6f 20 65 69 74 68 65 72 20 31 20 6f 72 20 to either 1 or
56580 30 2e 20 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 0. One of the f
56590 6f 75 72 20 77 69 6c 6c 20 62 65 20 31 2e 20 20 our will be 1.
565a0 54 68 65 20 6f 74 68 65 72 20 0a 2a 2a 20 74 68 The other .** th
565b0 72 65 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a 2a ree will be 0..*
565c0 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 /.#if defined(SQ
565d0 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 LITE_OS_OTHER).#
565e0 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 if SQLITE_OS_OT
565f0 48 45 52 3d 3d 31 0a 23 20 20 20 75 6e 64 65 66 HER==1.# undef
56600 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a SQLITE_OS_UNIX.
56610 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 # define SQLIT
56620 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 E_OS_UNIX 0.#
56630 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f undef SQLITE_OS_
56640 57 49 4e 0a 23 20 20 20 64 65 66 69 6e 65 20 53 WIN.# define S
56650 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 QLITE_OS_WIN 0.#
56660 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f undef SQLITE_
56670 4f 53 5f 4f 53 32 0a 23 20 20 20 64 65 66 69 6e OS_OS2.# defin
56680 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 e SQLITE_OS_OS2
56690 30 0a 23 20 65 6c 73 65 0a 23 20 20 20 75 6e 64 0.# else.# und
566a0 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 ef SQLITE_OS_OTH
566b0 45 52 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 ER.# endif.#endi
566c0 66 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 f.#if !defined(S
566d0 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 29 20 26 QLITE_OS_UNIX) &
566e0 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 & !defined(SQLIT
566f0 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 64 65 E_OS_OTHER).# de
56700 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f fine SQLITE_OS_O
56710 54 48 45 52 20 30 0a 23 20 69 66 6e 64 65 66 20 THER 0.# ifndef
56720 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 SQLITE_OS_WIN.#
56730 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49 if defined(_WI
56740 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 N32) || defined(
56750 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 WIN32) || define
56760 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 20 7c 7c d(__CYGWIN__) ||
56770 20 64 65 66 69 6e 65 64 28 5f 5f 4d 49 4e 47 57 defined(__MINGW
56780 33 32 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 32__) || defined
56790 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 23 (__BORLANDC__).#
567a0 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 define SQLI
567b0 54 45 5f 4f 53 5f 57 49 4e 20 31 0a 23 20 20 20 TE_OS_WIN 1.#
567c0 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
567d0 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20 OS_UNIX 0.#
567e0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
567f0 5f 4f 53 32 20 30 0a 23 20 20 20 65 6c 69 66 20 _OS2 0.# elif
56800 64 65 66 69 6e 65 64 28 5f 5f 45 4d 58 5f 5f 29 defined(__EMX__)
56810 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 32 || defined(_OS2
56820 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 4f 53 32 ) || defined(OS2
56830 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 ) || defined(_OS
56840 32 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 2_) || defined(_
56850 5f 4f 53 32 5f 5f 29 0a 23 20 20 20 20 20 64 65 _OS2__).# de
56860 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 fine SQLITE_OS_W
56870 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e IN 0.# defin
56880 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 e SQLITE_OS_UNIX
56890 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 0.# define
568a0 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 31 0a SQLITE_OS_OS2 1.
568b0 23 20 20 20 65 6c 73 65 0a 23 20 20 20 20 20 64 # else.# d
568c0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
568d0 57 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 WIN 0.# defi
568e0 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 ne SQLITE_OS_UNI
568f0 58 20 31 0a 23 20 20 20 20 20 64 65 66 69 6e 65 X 1.# define
56900 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 SQLITE_OS_OS2 0
56910 0a 23 20 20 65 6e 64 69 66 0a 23 20 65 6c 73 65 .# endif.# else
56920 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 .# define SQLIT
56930 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 64 E_OS_UNIX 0.# d
56940 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
56950 4f 53 32 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 OS2 0.# endif.#e
56960 6c 73 65 0a 23 20 69 66 6e 64 65 66 20 53 51 4c lse.# ifndef SQL
56970 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 64 65 ITE_OS_WIN.# de
56980 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 fine SQLITE_OS_W
56990 49 4e 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e IN 0.# endif.#en
569a0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 dif../*.** Deter
569b0 6d 69 6e 65 20 69 66 20 77 65 20 61 72 65 20 64 mine if we are d
569c0 65 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e 64 ealing with Wind
569d0 6f 77 73 43 45 20 2d 20 77 68 69 63 68 20 68 61 owsCE - which ha
569e0 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64 75 s a much.** redu
569f0 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 ced API..*/.#if
56a00 64 65 66 69 6e 65 64 28 5f 57 49 4e 33 32 5f 57 defined(_WIN32_W
56a10 43 45 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c CE).# define SQL
56a20 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 31 0a 23 ITE_OS_WINCE 1.#
56a30 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 else.# define SQ
56a40 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 30 0a LITE_OS_WINCE 0.
56a50 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 #endif.../*.** D
56a60 65 66 69 6e 65 20 74 68 65 20 6d 61 78 69 6d 75 efine the maximu
56a70 6d 20 73 69 7a 65 20 6f 66 20 61 20 74 65 6d 70 m size of a temp
56a80 6f 72 61 72 79 20 66 69 6c 65 6e 61 6d 65 0a 2a orary filename.*
56a90 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
56aa0 57 49 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 77 WIN.# include <w
56ab0 69 6e 64 6f 77 73 2e 68 3e 0a 23 20 64 65 66 69 indows.h>.# defi
56ac0 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 ne SQLITE_TEMPNA
56ad0 4d 45 5f 53 49 5a 45 20 28 4d 41 58 5f 50 41 54 ME_SIZE (MAX_PAT
56ae0 48 2b 35 30 29 0a 23 65 6c 69 66 20 53 51 4c 49 H+50).#elif SQLI
56af0 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 69 66 20 28 TE_OS_OS2.# if (
56b00 5f 5f 47 4e 55 43 5f 5f 20 3e 20 33 20 7c 7c 20 __GNUC__ > 3 ||
56b10 5f 5f 47 4e 55 43 5f 5f 20 3d 3d 20 33 20 26 26 __GNUC__ == 3 &&
56b20 20 5f 5f 47 4e 55 43 5f 4d 49 4e 4f 52 5f 5f 20 __GNUC_MINOR__
56b30 3e 3d 20 33 29 20 26 26 20 64 65 66 69 6e 65 64 >= 3) && defined
56b40 28 4f 53 32 5f 48 49 47 48 5f 4d 45 4d 4f 52 59 (OS2_HIGH_MEMORY
56b50 29 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6f 73 ).# include <os
56b60 32 73 61 66 65 2e 68 3e 20 2f 2a 20 68 61 73 20 2safe.h> /* has
56b70 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 62 to be included b
56b80 65 66 6f 72 65 20 6f 73 32 2e 68 20 66 6f 72 20 efore os2.h for
56b90 6c 69 6e 6b 69 6e 67 20 74 6f 20 77 6f 72 6b 20 linking to work
56ba0 2a 2f 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 */.# endif.# def
56bb0 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 44 41 54 45 ine INCL_DOSDATE
56bc0 54 49 4d 45 0a 23 20 64 65 66 69 6e 65 20 49 4e TIME.# define IN
56bd0 43 4c 5f 44 4f 53 46 49 4c 45 4d 47 52 0a 23 20 CL_DOSFILEMGR.#
56be0 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 45 define INCL_DOSE
56bf0 52 52 4f 52 53 0a 23 20 64 65 66 69 6e 65 20 49 RRORS.# define I
56c00 4e 43 4c 5f 44 4f 53 4d 49 53 43 0a 23 20 64 65 NCL_DOSMISC.# de
56c10 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 50 52 4f fine INCL_DOSPRO
56c20 43 45 53 53 0a 23 20 64 65 66 69 6e 65 20 49 4e CESS.# define IN
56c30 43 4c 5f 44 4f 53 4d 4f 44 55 4c 45 4d 47 52 0a CL_DOSMODULEMGR.
56c40 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f # define INCL_DO
56c50 53 53 45 4d 41 50 48 4f 52 45 53 0a 23 20 69 6e SSEMAPHORES.# in
56c60 63 6c 75 64 65 20 3c 6f 73 32 2e 68 3e 0a 23 20 clude <os2.h>.#
56c70 69 6e 63 6c 75 64 65 20 3c 75 63 6f 6e 76 2e 68 include <uconv.h
56c80 3e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 >.# define SQLIT
56c90 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 E_TEMPNAME_SIZE
56ca0 28 43 43 48 4d 41 58 50 41 54 48 43 4f 4d 50 29 (CCHMAXPATHCOMP)
56cb0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
56cc0 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f SQLITE_TEMPNAME_
56cd0 53 49 5a 45 20 32 30 30 0a 23 65 6e 64 69 66 0a SIZE 200.#endif.
56ce0 0a 2f 2a 20 49 66 20 74 68 65 20 53 45 54 5f 46 ./* If the SET_F
56cf0 55 4c 4c 53 59 4e 43 20 6d 61 63 72 6f 20 69 73 ULLSYNC macro is
56d00 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 61 62 6f not defined abo
56d10 76 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 69 74 ve, then make it
56d20 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 0a 2a 2f 0a 23 .** a no-op.*/.#
56d30 69 66 6e 64 65 66 20 53 45 54 5f 46 55 4c 4c 53 ifndef SET_FULLS
56d40 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 53 45 54 YNC.# define SET
56d50 5f 46 55 4c 4c 53 59 4e 43 28 78 2c 79 29 0a 23 _FULLSYNC(x,y).#
56d60 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
56d70 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f 66 default size of
56d80 20 61 20 64 69 73 6b 20 73 65 63 74 6f 72 0a 2a a disk sector.*
56d90 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
56da0 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f _DEFAULT_SECTOR_
56db0 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 SIZE.# define SQ
56dc0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 LITE_DEFAULT_SEC
56dd0 54 4f 52 5f 53 49 5a 45 20 35 31 32 0a 23 65 6e TOR_SIZE 512.#en
56de0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 6d 70 6f dif../*.** Tempo
56df0 72 61 72 79 20 66 69 6c 65 73 20 61 72 65 20 6e rary files are n
56e00 61 6d 65 64 20 73 74 61 72 74 69 6e 67 20 77 69 amed starting wi
56e10 74 68 20 74 68 69 73 20 70 72 65 66 69 78 20 66 th this prefix f
56e20 6f 6c 6c 6f 77 65 64 20 62 79 20 31 36 20 72 61 ollowed by 16 ra
56e30 6e 64 6f 6d 0a 2a 2a 20 61 6c 70 68 61 6e 75 6d ndom.** alphanum
56e40 65 72 69 63 20 63 68 61 72 61 63 74 65 72 73 2c eric characters,
56e50 20 61 6e 64 20 6e 6f 20 66 69 6c 65 20 65 78 74 and no file ext
56e60 65 6e 73 69 6f 6e 2e 20 54 68 65 79 20 61 72 65 ension. They are
56e70 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a 2a stored in the.*
56e80 2a 20 4f 53 27 73 20 73 74 61 6e 64 61 72 64 20 * OS's standard
56e90 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 64 temporary file d
56ea0 69 72 65 63 74 6f 72 79 2c 20 61 6e 64 20 61 72 irectory, and ar
56eb0 65 20 64 65 6c 65 74 65 64 20 70 72 69 6f 72 20 e deleted prior
56ec0 74 6f 20 65 78 69 74 2e 0a 2a 2a 20 49 66 20 73 to exit..** If s
56ed0 71 6c 69 74 65 20 69 73 20 62 65 69 6e 67 20 65 qlite is being e
56ee0 6d 62 65 64 64 65 64 20 69 6e 20 61 6e 6f 74 68 mbedded in anoth
56ef0 65 72 20 70 72 6f 67 72 61 6d 2c 20 79 6f 75 20 er program, you
56f00 6d 61 79 20 77 69 73 68 20 74 6f 20 63 68 61 6e may wish to chan
56f10 67 65 20 74 68 65 0a 2a 2a 20 70 72 65 66 69 78 ge the.** prefix
56f20 20 74 6f 20 72 65 66 6c 65 63 74 20 79 6f 75 72 to reflect your
56f30 20 70 72 6f 67 72 61 6d 27 73 20 6e 61 6d 65 2c program's name,
56f40 20 73 6f 20 74 68 61 74 20 69 66 20 79 6f 75 72 so that if your
56f50 20 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a program exits.*
56f60 2a 20 70 72 65 6d 61 74 75 72 65 6c 79 2c 20 6f * prematurely, o
56f70 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c ld temporary fil
56f80 65 73 20 63 61 6e 20 62 65 20 65 61 73 69 6c 79 es can be easily
56f90 20 69 64 65 6e 74 69 66 69 65 64 2e 20 54 68 69 identified. Thi
56fa0 73 20 63 61 6e 20 62 65 20 64 6f 6e 65 0a 2a 2a s can be done.**
56fb0 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f using -DSQLITE_
56fc0 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 TEMP_FILE_PREFIX
56fd0 3d 6d 79 70 72 65 66 69 78 5f 20 6f 6e 20 74 68 =myprefix_ on th
56fe0 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 e compiler comma
56ff0 6e 64 20 6c 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 32 nd line..**.** 2
57000 30 30 36 2d 31 30 2d 33 31 3a 20 20 54 68 65 20 006-10-31: The
57010 64 65 66 61 75 6c 74 20 70 72 65 66 69 78 20 75 default prefix u
57020 73 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69 74 sed to be "sqlit
57030 65 5f 22 2e 20 20 42 75 74 20 74 68 65 6e 0a 2a e_". But then.*
57040 2a 20 4d 63 61 66 65 65 20 73 74 61 72 74 65 64 * Mcafee started
57050 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e using SQLite in
57060 20 74 68 65 69 72 20 61 6e 74 69 2d 76 69 72 75 their anti-viru
57070 73 20 70 72 6f 64 75 63 74 20 61 6e 64 20 69 74 s product and it
57080 0a 2a 2a 20 73 74 61 72 74 65 64 20 70 75 74 74 .** started putt
57090 69 6e 67 20 66 69 6c 65 73 20 77 69 74 68 20 74 ing files with t
570a0 68 65 20 22 73 71 6c 69 74 65 22 20 6e 61 6d 65 he "sqlite" name
570b0 20 69 6e 20 74 68 65 20 63 3a 2f 74 65 6d 70 20 in the c:/temp
570c0 66 6f 6c 64 65 72 2e 0a 2a 2a 20 54 68 69 73 20 folder..** This
570d0 61 6e 6e 6f 79 65 64 20 6d 61 6e 79 20 77 69 6e annoyed many win
570e0 64 6f 77 73 20 75 73 65 72 73 2e 20 20 54 68 6f dows users. Tho
570f0 73 65 20 75 73 65 72 73 20 77 6f 75 6c 64 20 74 se users would t
57100 68 65 6e 20 64 6f 20 61 20 0a 2a 2a 20 47 6f 6f hen do a .** Goo
57110 67 6c 65 20 73 65 61 72 63 68 20 66 6f 72 20 22 gle search for "
57120 73 71 6c 69 74 65 22 2c 20 66 69 6e 64 20 74 68 sqlite", find th
57130 65 20 74 65 6c 65 70 68 6f 6e 65 20 6e 75 6d 62 e telephone numb
57140 65 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65 ers of the.** de
57150 76 65 6c 6f 70 65 72 73 20 61 6e 64 20 63 61 6c velopers and cal
57160 6c 20 74 6f 20 77 61 6b 65 20 74 68 65 6d 20 75 l to wake them u
57170 70 20 61 74 20 6e 69 67 68 74 20 61 6e 64 20 63 p at night and c
57180 6f 6d 70 6c 61 69 6e 2e 0a 2a 2a 20 46 6f 72 20 omplain..** For
57190 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 65 this reason, the
571a0 20 64 65 66 61 75 6c 74 20 6e 61 6d 65 20 70 72 default name pr
571b0 65 66 69 78 20 69 73 20 63 68 61 6e 67 65 64 20 efix is changed
571c0 74 6f 20 62 65 20 22 73 71 6c 69 74 65 22 20 0a to be "sqlite" .
571d0 2a 2a 20 73 70 65 6c 6c 65 64 20 62 61 63 6b 77 ** spelled backw
571e0 61 72 64 73 2e 20 20 53 6f 20 74 68 65 20 74 65 ards. So the te
571f0 6d 70 20 66 69 6c 65 73 20 61 72 65 20 73 74 69 mp files are sti
57200 6c 6c 20 69 64 65 6e 74 69 66 69 65 64 2c 20 62 ll identified, b
57210 75 74 0a 2a 2a 20 61 6e 79 62 6f 64 79 20 73 6d ut.** anybody sm
57220 61 72 74 20 65 6e 6f 75 67 68 20 74 6f 20 66 69 art enough to fi
57230 67 75 72 65 20 6f 75 74 20 74 68 65 20 63 6f 64 gure out the cod
57240 65 20 69 73 20 61 6c 73 6f 20 6c 69 6b 65 6c 79 e is also likely
57250 20 73 6d 61 72 74 0a 2a 2a 20 65 6e 6f 75 67 68 smart.** enough
57260 20 74 6f 20 6b 6e 6f 77 20 74 68 61 74 20 63 61 to know that ca
57270 6c 6c 69 6e 67 20 74 68 65 20 64 65 76 65 6c 6f lling the develo
57280 70 65 72 20 77 69 6c 6c 20 6e 6f 74 20 68 65 6c per will not hel
57290 70 20 67 65 74 20 72 69 64 0a 2a 2a 20 6f 66 20 p get rid.** of
572a0 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 the file..*/.#if
572b0 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 ndef SQLITE_TEMP
572c0 5f 46 49 4c 45 5f 50 52 45 46 49 58 0a 23 20 64 _FILE_PREFIX.# d
572d0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d efine SQLITE_TEM
572e0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 20 22 65 P_FILE_PREFIX "e
572f0 74 69 6c 71 73 5f 22 0a 23 65 6e 64 69 66 0a 0a tilqs_".#endif..
57300 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
57310 69 6e 67 20 76 61 6c 75 65 73 20 6d 61 79 20 62 ing values may b
57320 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 e passed as the
57330 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
57340 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4c to.** sqlite3OsL
57350 6f 63 6b 28 29 2e 20 54 68 65 20 76 61 72 69 6f ock(). The vario
57360 75 73 20 6c 6f 63 6b 73 20 65 78 68 69 62 69 74 us locks exhibit
57370 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
57380 65 6d 61 6e 74 69 63 73 3a 0a 2a 2a 0a 2a 2a 20 emantics:.**.**
57390 53 48 41 52 45 44 3a 20 20 20 20 41 6e 79 20 6e SHARED: Any n
573a0 75 6d 62 65 72 20 6f 66 20 70 72 6f 63 65 73 73 umber of process
573b0 65 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 53 48 es may hold a SH
573c0 41 52 45 44 20 6c 6f 63 6b 20 73 69 6d 75 6c 74 ARED lock simult
573d0 61 6e 65 6f 75 73 6c 79 2e 0a 2a 2a 20 52 45 53 aneously..** RES
573e0 45 52 56 45 44 3a 20 20 41 20 73 69 6e 67 6c 65 ERVED: A single
573f0 20 70 72 6f 63 65 73 73 20 6d 61 79 20 68 6f 6c process may hol
57400 64 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 d a RESERVED loc
57410 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a 2a k on a file at.*
57420 2a 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 79 * any
57430 20 74 69 6d 65 2e 20 4f 74 68 65 72 20 70 72 6f time. Other pro
57440 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64 20 cesses may hold
57450 61 6e 64 20 6f 62 74 61 69 6e 20 6e 65 77 20 53 and obtain new S
57460 48 41 52 45 44 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 HARED locks..**
57470 50 45 4e 44 49 4e 47 3a 20 20 20 41 20 73 69 6e PENDING: A sin
57480 67 6c 65 20 70 72 6f 63 65 73 73 20 6d 61 79 20 gle process may
57490 68 6f 6c 64 20 61 20 50 45 4e 44 49 4e 47 20 6c hold a PENDING l
574a0 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 ock on a file at
574b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 61 .** a
574c0 6e 79 20 6f 6e 65 20 74 69 6d 65 2e 20 45 78 69 ny one time. Exi
574d0 73 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 sting SHARED loc
574e0 6b 73 20 6d 61 79 20 70 65 72 73 69 73 74 2c 20 ks may persist,
574f0 62 75 74 20 6e 6f 20 6e 65 77 0a 2a 2a 20 20 20 but no new.**
57500 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 20 SHARED
57510 6c 6f 63 6b 73 20 6d 61 79 20 62 65 20 6f 62 74 locks may be obt
57520 61 69 6e 65 64 20 62 79 20 6f 74 68 65 72 20 70 ained by other p
57530 72 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 45 58 43 rocesses..** EXC
57540 4c 55 53 49 56 45 3a 20 41 6e 20 45 58 43 4c 55 LUSIVE: An EXCLU
57550 53 49 56 45 20 6c 6f 63 6b 20 70 72 65 63 6c 75 SIVE lock preclu
57560 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 6c 6f des all other lo
57570 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 50 45 4e 44 49 cks..**.** PENDI
57580 4e 47 5f 4c 4f 43 4b 20 6d 61 79 20 6e 6f 74 20 NG_LOCK may not
57590 62 65 20 70 61 73 73 65 64 20 64 69 72 65 63 74 be passed direct
575a0 6c 79 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4c ly to sqlite3OsL
575b0 6f 63 6b 28 29 2e 20 49 6e 73 74 65 61 64 2c 20 ock(). Instead,
575c0 61 0a 2a 2a 20 70 72 6f 63 65 73 73 20 74 68 61 a.** process tha
575d0 74 20 72 65 71 75 65 73 74 73 20 61 6e 20 45 58 t requests an EX
575e0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6d 61 79 CLUSIVE lock may
575f0 20 61 63 74 75 61 6c 6c 79 20 6f 62 74 61 69 6e actually obtain
57600 20 61 20 50 45 4e 44 49 4e 47 0a 2a 2a 20 6c 6f a PENDING.** lo
57610 63 6b 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 ck. This can be
57620 75 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 upgraded to an E
57630 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 62 79 XCLUSIVE lock by
57640 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 a subsequent ca
57650 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 ll to.** sqlite3
57660 4f 73 4c 6f 63 6b 28 29 2e 0a 2a 2f 0a 23 64 65 OsLock()..*/.#de
57670 66 69 6e 65 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 fine NO_LOCK
57680 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 0.#define S
57690 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 31 HARED_LOCK 1
576a0 0a 23 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 .#define RESERVE
576b0 44 5f 4c 4f 43 4b 20 20 20 32 0a 23 64 65 66 69 D_LOCK 2.#defi
576c0 6e 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 ne PENDING_LOCK
576d0 20 20 20 33 0a 23 64 65 66 69 6e 65 20 45 58 43 3.#define EXC
576e0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 34 0a 0a LUSIVE_LOCK 4..
576f0 2f 2a 0a 2a 2a 20 46 69 6c 65 20 4c 6f 63 6b 69 /*.** File Locki
57700 6e 67 20 4e 6f 74 65 73 3a 20 20 28 4d 6f 73 74 ng Notes: (Most
57710 6c 79 20 61 62 6f 75 74 20 77 69 6e 64 6f 77 73 ly about windows
57720 20 62 75 74 20 61 6c 73 6f 20 73 6f 6d 65 20 69 but also some i
57730 6e 66 6f 20 66 6f 72 20 55 6e 69 78 29 0a 2a 2a nfo for Unix).**
57740 0a 2a 2a 20 57 65 20 63 61 6e 6e 6f 74 20 75 73 .** We cannot us
57750 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 6f e LockFileEx() o
57760 72 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 r UnlockFileEx()
57770 20 6f 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 on Win95/98/ME
57780 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 6f 73 65 because.** those
57790 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e functions are n
577a0 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 53 ot available. S
577b0 6f 20 77 65 20 75 73 65 20 6f 6e 6c 79 20 4c 6f o we use only Lo
577c0 63 6b 46 69 6c 65 28 29 20 61 6e 64 0a 2a 2a 20 ckFile() and.**
577d0 55 6e 6c 6f 63 6b 46 69 6c 65 28 29 2e 0a 2a 2a UnlockFile()..**
577e0 0a 2a 2a 20 4c 6f 63 6b 46 69 6c 65 28 29 20 70 .** LockFile() p
577f0 72 65 76 65 6e 74 73 20 6e 6f 74 20 6a 75 73 74 revents not just
57800 20 77 72 69 74 69 6e 67 20 62 75 74 20 61 6c 73 writing but als
57810 6f 20 72 65 61 64 69 6e 67 20 62 79 20 6f 74 68 o reading by oth
57820 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2a er processes..**
57830 20 41 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 69 A SHARED_LOCK i
57840 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f s obtained by lo
57850 63 6b 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 72 cking a single r
57860 61 6e 64 6f 6d 6c 79 2d 63 68 6f 73 65 6e 20 0a andomly-chosen .
57870 2a 2a 20 62 79 74 65 20 6f 75 74 20 6f 66 20 61 ** byte out of a
57880 20 73 70 65 63 69 66 69 63 20 72 61 6e 67 65 20 specific range
57890 6f 66 20 62 79 74 65 73 2e 20 54 68 65 20 6c 6f of bytes. The lo
578a0 63 6b 20 62 79 74 65 20 69 73 20 6f 62 74 61 69 ck byte is obtai
578b0 6e 65 64 20 61 74 20 0a 2a 2a 20 72 61 6e 64 6f ned at .** rando
578c0 6d 20 73 6f 20 74 77 6f 20 73 65 70 61 72 61 74 m so two separat
578d0 65 20 72 65 61 64 65 72 73 20 63 61 6e 20 70 72 e readers can pr
578e0 6f 62 61 62 6c 79 20 61 63 63 65 73 73 20 74 68 obably access th
578f0 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 0a 2a e file at the .*
57900 2a 20 73 61 6d 65 20 74 69 6d 65 2c 20 75 6e 6c * same time, unl
57910 65 73 73 20 74 68 65 79 20 61 72 65 20 75 6e 6c ess they are unl
57920 75 63 6b 79 20 61 6e 64 20 63 68 6f 6f 73 65 20 ucky and choose
57930 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 20 62 79 the same lock by
57940 74 65 2e 0a 2a 2a 20 41 6e 20 45 58 43 4c 55 53 te..** An EXCLUS
57950 49 56 45 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 IVE_LOCK is obta
57960 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 ined by locking
57970 61 6c 6c 20 62 79 74 65 73 20 69 6e 20 74 68 65 all bytes in the
57980 20 72 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 72 65 range..** There
57990 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 6f 6e 65 can only be one
579a0 20 77 72 69 74 65 72 2e 20 20 41 20 52 45 53 45 writer. A RESE
579b0 52 56 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 RVED_LOCK is obt
579c0 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 ained by locking
579d0 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 62 79 74 .** a single byt
579e0 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 68 e of the file th
579f0 61 74 20 69 73 20 64 65 73 69 67 6e 61 74 65 64 at is designated
57a00 20 61 73 20 74 68 65 20 72 65 73 65 72 76 65 64 as the reserved
57a10 20 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 lock byte..** A
57a20 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 69 73 PENDING_LOCK is
57a30 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 obtained by loc
57a40 6b 69 6e 67 20 61 20 64 65 73 69 67 6e 61 74 65 king a designate
57a50 64 20 62 79 74 65 20 64 69 66 66 65 72 65 6e 74 d byte different
57a60 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 52 45 53 from.** the RES
57a70 45 52 56 45 44 5f 4c 4f 43 4b 20 62 79 74 65 2e ERVED_LOCK byte.
57a80 0a 2a 2a 0a 2a 2a 20 4f 6e 20 57 69 6e 4e 54 2f .**.** On WinNT/
57a90 32 4b 2f 58 50 20 73 79 73 74 65 6d 73 2c 20 4c 2K/XP systems, L
57aa0 6f 63 6b 46 69 6c 65 45 78 28 29 20 61 6e 64 20 ockFileEx() and
57ab0 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20 61 UnlockFileEx() a
57ac0 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 0a 2a 2a re available,.**
57ad0 20 77 68 69 63 68 20 6d 65 61 6e 73 20 77 65 20 which means we
57ae0 63 61 6e 20 75 73 65 20 72 65 61 64 65 72 2f 77 can use reader/w
57af0 72 69 74 65 72 20 6c 6f 63 6b 73 2e 20 20 57 68 riter locks. Wh
57b00 65 6e 20 72 65 61 64 65 72 2f 77 72 69 74 65 72 en reader/writer
57b10 20 6c 6f 63 6b 73 0a 2a 2a 20 61 72 65 20 75 73 locks.** are us
57b20 65 64 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 ed, the lock is
57b30 70 6c 61 63 65 64 20 6f 6e 20 74 68 65 20 73 61 placed on the sa
57b40 6d 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 me range of byte
57b50 73 20 74 68 61 74 20 69 73 20 75 73 65 64 0a 2a s that is used.*
57b60 2a 20 66 6f 72 20 70 72 6f 62 61 62 69 6c 69 73 * for probabilis
57b70 74 69 63 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 57 tic locking in W
57b80 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 48 65 6e in95/98/ME. Hen
57b90 63 65 2c 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 ce, the locking
57ba0 73 63 68 65 6d 65 0a 2a 2a 20 77 69 6c 6c 20 73 scheme.** will s
57bb0 75 70 70 6f 72 74 20 74 77 6f 20 6f 72 20 6d 6f upport two or mo
57bc0 72 65 20 57 69 6e 39 35 20 72 65 61 64 65 72 73 re Win95 readers
57bd0 20 6f 72 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 or two or more
57be0 57 69 6e 4e 54 20 72 65 61 64 65 72 73 2e 0a 2a WinNT readers..*
57bf0 2a 20 42 75 74 20 61 20 73 69 6e 67 6c 65 20 57 * But a single W
57c00 69 6e 39 35 20 72 65 61 64 65 72 20 77 69 6c 6c in95 reader will
57c10 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 57 69 lock out all Wi
57c20 6e 4e 54 20 72 65 61 64 65 72 73 20 61 6e 64 20 nNT readers and
57c30 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 57 69 6e 4e a single.** WinN
57c40 54 20 72 65 61 64 65 72 20 77 69 6c 6c 20 6c 6f T reader will lo
57c50 63 6b 20 6f 75 74 20 61 6c 6c 20 6f 74 68 65 72 ck out all other
57c60 20 57 69 6e 39 35 20 72 65 61 64 65 72 73 2e 0a Win95 readers..
57c70 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 **.** The follow
57c80 69 6e 67 20 23 64 65 66 69 6e 65 73 20 73 70 65 ing #defines spe
57c90 63 69 66 79 20 74 68 65 20 72 61 6e 67 65 20 6f cify the range o
57ca0 66 20 62 79 74 65 73 20 75 73 65 64 20 66 6f 72 f bytes used for
57cb0 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 53 48 41 locking..** SHA
57cc0 52 45 44 5f 53 49 5a 45 20 69 73 20 74 68 65 20 RED_SIZE is the
57cd0 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
57ce0 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 available in the
57cf0 20 70 6f 6f 6c 20 66 72 6f 6d 20 77 68 69 63 68 pool from which
57d00 0a 2a 2a 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 .** a random byt
57d10 65 20 69 73 20 73 65 6c 65 63 74 65 64 20 66 6f e is selected fo
57d20 72 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e r a shared lock.
57d30 20 20 54 68 65 20 70 6f 6f 6c 20 6f 66 20 62 79 The pool of by
57d40 74 65 73 20 66 6f 72 0a 2a 2a 20 73 68 61 72 65 tes for.** share
57d50 64 20 6c 6f 63 6b 73 20 62 65 67 69 6e 73 20 61 d locks begins a
57d60 74 20 53 48 41 52 45 44 5f 46 49 52 53 54 2e 20 t SHARED_FIRST.
57d70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6d 65 20 .**.** The same
57d80 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 locking strategy
57d90 20 61 6e 64 0a 2a 2a 20 62 79 74 65 20 72 61 6e and.** byte ran
57da0 67 65 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 ges are used for
57db0 20 55 6e 69 78 2e 20 20 54 68 69 73 20 6c 65 61 Unix. This lea
57dc0 76 65 73 20 6f 70 65 6e 20 74 68 65 20 70 6f 73 ves open the pos
57dd0 73 69 62 6c 69 74 79 20 6f 66 20 68 61 76 69 6e siblity of havin
57de0 67 0a 2a 2a 20 63 6c 69 65 6e 74 73 20 6f 6e 20 g.** clients on
57df0 77 69 6e 39 35 2c 20 77 69 6e 4e 54 2c 20 61 6e win95, winNT, an
57e00 64 20 75 6e 69 78 20 61 6c 6c 20 74 61 6c 6b 69 d unix all talki
57e10 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 73 ng to the same s
57e20 68 61 72 65 64 20 66 69 6c 65 0a 2a 2a 20 61 6e hared file.** an
57e30 64 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f d all locking co
57e40 72 72 65 63 74 6c 79 2e 20 20 54 6f 20 64 6f 20 rrectly. To do
57e50 73 6f 20 77 6f 75 6c 64 20 72 65 71 75 69 72 65 so would require
57e60 20 74 68 61 74 20 73 61 6d 62 61 20 28 6f 72 20 that samba (or
57e70 77 68 61 74 65 76 65 72 0a 2a 2a 20 74 6f 6f 6c whatever.** tool
57e80 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 66 is being used f
57e90 6f 72 20 66 69 6c 65 20 73 68 61 72 69 6e 67 29 or file sharing)
57ea0 20 69 6d 70 6c 65 6d 65 6e 74 73 20 6c 6f 63 6b implements lock
57eb0 73 20 63 6f 72 72 65 63 74 6c 79 20 62 65 74 77 s correctly betw
57ec0 65 65 6e 0a 2a 2a 20 77 69 6e 64 6f 77 73 20 61 een.** windows a
57ed0 6e 64 20 75 6e 69 78 2e 20 20 49 27 6d 20 67 75 nd unix. I'm gu
57ee0 65 73 73 69 6e 67 20 74 68 61 74 20 69 73 6e 27 essing that isn'
57ef0 74 20 6c 69 6b 65 6c 79 20 74 6f 20 68 61 70 70 t likely to happ
57f00 65 6e 2c 20 62 75 74 20 62 79 0a 2a 2a 20 75 73 en, but by.** us
57f10 69 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 ing the same loc
57f20 6b 69 6e 67 20 72 61 6e 67 65 20 77 65 20 61 72 king range we ar
57f30 65 20 61 74 20 6c 65 61 73 74 20 6f 70 65 6e 20 e at least open
57f40 74 6f 20 74 68 65 20 70 6f 73 73 69 62 69 6c 69 to the possibili
57f50 74 79 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 69 6e ty..**.** Lockin
57f60 67 20 69 6e 20 77 69 6e 64 6f 77 73 20 69 73 20 g in windows is
57f70 6d 61 6e 64 69 74 6f 72 79 2e 20 20 46 6f 72 20 manditory. For
57f80 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 77 65 20 this reason, we
57f90 63 61 6e 6e 6f 74 20 73 74 6f 72 65 0a 2a 2a 20 cannot store.**
57fa0 61 63 74 75 61 6c 20 64 61 74 61 20 69 6e 20 74 actual data in t
57fb0 68 65 20 62 79 74 65 73 20 75 73 65 64 20 66 6f he bytes used fo
57fc0 72 20 6c 6f 63 6b 69 6e 67 2e 20 20 54 68 65 20 r locking. The
57fd0 70 61 67 65 72 20 6e 65 76 65 72 20 61 6c 6c 6f pager never allo
57fe0 63 61 74 65 73 0a 2a 2a 20 74 68 65 20 70 61 67 cates.** the pag
57ff0 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 6c es involved in l
58000 6f 63 6b 69 6e 67 20 74 68 65 72 65 66 6f 72 65 ocking therefore
58010 2e 20 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69 . SHARED_SIZE i
58020 73 20 73 65 6c 65 63 74 65 64 20 73 6f 0a 2a 2a s selected so.**
58030 20 74 68 61 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 that all locks
58040 77 69 6c 6c 20 66 69 74 20 6f 6e 20 61 20 73 69 will fit on a si
58050 6e 67 6c 65 20 70 61 67 65 20 65 76 65 6e 20 61 ngle page even a
58060 74 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 70 61 t the minimum pa
58070 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 50 45 4e 44 ge size..** PEND
58080 49 4e 47 5f 42 59 54 45 20 64 65 66 69 6e 65 73 ING_BYTE defines
58090 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
580a0 66 20 74 68 65 20 6c 6f 63 6b 73 2e 20 20 42 79 f the locks. By
580b0 20 64 65 66 61 75 6c 74 20 50 45 4e 44 49 4e 47 default PENDING
580c0 5f 42 59 54 45 0a 2a 2a 20 69 73 20 73 65 74 20 _BYTE.** is set
580d0 68 69 67 68 20 73 6f 20 74 68 61 74 20 77 65 20 high so that we
580e0 64 6f 6e 27 74 20 68 61 76 65 20 74 6f 20 61 6c don't have to al
580f0 6c 6f 63 61 74 65 20 61 6e 20 75 6e 75 73 65 64 locate an unused
58100 20 70 61 67 65 20 65 78 63 65 70 74 0a 2a 2a 20 page except.**
58110 66 6f 72 20 76 65 72 79 20 6c 61 72 67 65 20 64 for very large d
58120 61 74 61 62 61 73 65 73 2e 20 20 42 75 74 20 6f atabases. But o
58130 6e 65 20 73 68 6f 75 6c 64 20 74 65 73 74 20 74 ne should test t
58140 68 65 20 70 61 67 65 20 73 6b 69 70 70 69 6e 67 he page skipping
58150 20 6c 6f 67 69 63 20 0a 2a 2a 20 62 79 20 73 65 logic .** by se
58160 74 74 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42 59 tting PENDING_BY
58170 54 45 20 6c 6f 77 20 61 6e 64 20 72 75 6e 6e 69 TE low and runni
58180 6e 67 20 74 68 65 20 65 6e 74 69 72 65 20 72 65 ng the entire re
58190 67 72 65 73 73 69 6f 6e 20 73 75 69 74 65 2e 0a gression suite..
581a0 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 **.** Changing t
581b0 68 65 20 76 61 6c 75 65 20 6f 66 20 50 45 4e 44 he value of PEND
581c0 49 4e 47 5f 42 59 54 45 20 72 65 73 75 6c 74 73 ING_BYTE results
581d0 20 69 6e 20 61 20 73 75 62 74 6c 79 20 69 6e 63 in a subtly inc
581e0 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 66 69 6c ompatible.** fil
581f0 65 20 66 6f 72 6d 61 74 2e 20 20 44 65 70 65 6e e format. Depen
58200 64 69 6e 67 20 6f 6e 20 68 6f 77 20 69 74 20 69 ding on how it i
58210 73 20 63 68 61 6e 67 65 64 2c 20 79 6f 75 20 6d s changed, you m
58220 69 67 68 74 20 6e 6f 74 20 6e 6f 74 69 63 65 0a ight not notice.
58230 2a 2a 20 74 68 65 20 69 6e 63 6f 6d 70 61 74 69 ** the incompati
58240 62 69 6c 69 74 79 20 72 69 67 68 74 20 61 77 61 bility right awa
58250 79 2c 20 65 76 65 6e 20 72 75 6e 6e 69 6e 67 20 y, even running
58260 61 20 66 75 6c 6c 20 72 65 67 72 65 73 73 69 6f a full regressio
58270 6e 20 74 65 73 74 2e 0a 2a 2a 20 54 68 65 20 64 n test..** The d
58280 65 66 61 75 6c 74 20 6c 6f 63 61 74 69 6f 6e 20 efault location
58290 6f 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 of PENDING_BYTE
582a0 69 73 20 74 68 65 20 66 69 72 73 74 20 62 79 74 is the first byt
582b0 65 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 47 e past the.** 1G
582c0 42 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2a 0a 2a B boundary..**.*
582d0 2f 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e /.#define PENDIN
582e0 47 5f 42 59 54 45 20 20 20 20 20 20 73 71 6c 69 G_BYTE sqli
582f0 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 0a 23 te3PendingByte.#
58300 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 44 5f define RESERVED_
58310 42 59 54 45 20 20 20 20 20 28 50 45 4e 44 49 4e BYTE (PENDIN
58320 47 5f 42 59 54 45 2b 31 29 0a 23 64 65 66 69 6e G_BYTE+1).#defin
58330 65 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 20 e SHARED_FIRST
58340 20 20 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 54 (PENDING_BYT
58350 45 2b 32 29 0a 23 64 65 66 69 6e 65 20 53 48 41 E+2).#define SHA
58360 52 45 44 5f 53 49 5a 45 20 20 20 20 20 20 20 35 RED_SIZE 5
58370 31 30 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 10../*.** Wrappe
58380 72 20 61 72 6f 75 6e 64 20 4f 53 20 73 70 65 63 r around OS spec
58390 69 66 69 63 20 73 71 6c 69 74 65 33 5f 6f 73 5f ific sqlite3_os_
583a0 69 6e 69 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e init() function.
583b0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
583c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
583d0 49 6e 69 74 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 Init(void);../*
583e0 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f .** Functions fo
583f0 72 20 61 63 63 65 73 73 69 6e 67 20 73 71 6c 69 r accessing sqli
58400 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 te3_file methods
58410 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 .*/.SQLITE_PRIV
58420 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
58430 73 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 sClose(sqlite3_f
58440 69 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ile*);.SQLITE_PR
58450 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
58460 33 4f 73 52 65 61 64 28 73 71 6c 69 74 65 33 5f 3OsRead(sqlite3_
58470 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e file*, void*, in
58480 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 t amt, i64 offse
58490 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
584a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
584b0 57 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69 Write(sqlite3_fi
584c0 6c 65 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a le*, const void*
584d0 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f , int amt, i64 o
584e0 66 66 73 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50 ffset);.SQLITE_P
584f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
58500 65 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c e3OsTruncate(sql
58510 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34 20 ite3_file*, i64
58520 73 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 size);.SQLITE_PR
58530 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
58540 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 3OsSync(sqlite3_
58550 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c file*, int);.SQL
58560 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
58570 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a sqlite3OsFileSiz
58580 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c e(sqlite3_file*,
58590 20 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 i64 *pSize);.SQ
585a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
585b0 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 sqlite3OsLock(s
585c0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e qlite3_file*, in
585d0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
585e0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
585f0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 Unlock(sqlite3_f
58600 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 ile*, int);.SQLI
58610 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
58620 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 qlite3OsCheckRes
58630 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 ervedLock(sqlite
58640 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
58650 2a 70 52 65 73 4f 75 74 29 3b 0a 53 51 4c 49 54 *pResOut);.SQLIT
58660 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
58670 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 lite3OsFileContr
58680 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a ol(sqlite3_file*
58690 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 64 65 ,int,void*);.#de
586a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 fine SQLITE_FCNT
586b0 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 30 L_DB_UNCHANGED 0
586c0 78 63 61 30 39 33 66 61 30 0a 53 51 4c 49 54 45 xca093fa0.SQLITE
586d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
586e0 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 ite3OsSectorSize
586f0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
58700 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 d);.SQLITE_PRIVA
58710 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
58720 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
58730 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 stics(sqlite3_fi
58740 6c 65 20 2a 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a le *id);../* .**
58750 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 Functions for a
58760 63 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33 ccessing sqlite3
58770 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 0a 2a 2f _vfs methods .*/
58780 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
58790 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 int sqlite3OsOpe
587a0 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c n(sqlite3_vfs *,
587b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 const char *, s
587c0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e qlite3_file*, in
587d0 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 t, int *);.SQLIT
587e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
587f0 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71 lite3OsDelete(sq
58800 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e lite3_vfs *, con
58810 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b st char *, int);
58820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
58830 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63 int sqlite3OsAcc
58840 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ess(sqlite3_vfs
58850 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c *, const char *,
58860 20 69 6e 74 2c 20 69 6e 74 20 2a 70 52 65 73 4f int, int *pResO
58870 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ut);.SQLITE_PRIV
58880 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
58890 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 73 71 sFullPathname(sq
588a0 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e lite3_vfs *, con
588b0 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 st char *, int,
588c0 63 68 61 72 20 2a 29 3b 0a 23 69 66 6e 64 65 66 char *);.#ifndef
588d0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 SQLITE_OMIT_LOA
588e0 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 D_EXTENSION.SQLI
588f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
58900 2a 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e *sqlite3OsDlOpen
58910 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 (sqlite3_vfs *,
58920 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 const char *);.S
58930 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
58940 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 id sqlite3OsDlEr
58950 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ror(sqlite3_vfs
58960 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b *, int, char *);
58970 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
58980 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 4f 73 void (*sqlite3Os
58990 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 DlSym(sqlite3_vf
589a0 73 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 6f 6e s *, void *, con
589b0 73 74 20 63 68 61 72 20 2a 29 29 28 76 6f 69 64 st char *))(void
589c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
589d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 E void sqlite3Os
589e0 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f DlClose(sqlite3_
589f0 76 66 73 20 2a 2c 20 76 6f 69 64 20 2a 29 3b 0a vfs *, void *);.
58a00 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
58a10 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e _OMIT_LOAD_EXTEN
58a20 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 SION */.SQLITE_P
58a30 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
58a40 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 e3OsRandomness(s
58a50 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e qlite3_vfs *, in
58a60 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 t, char *);.SQLI
58a70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
58a80 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71 qlite3OsSleep(sq
58a90 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 lite3_vfs *, int
58aa0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
58ab0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 E int sqlite3OsC
58ac0 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 urrentTime(sqlit
58ad0 65 33 5f 76 66 73 20 2a 2c 20 64 6f 75 62 6c 65 e3_vfs *, double
58ae0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 *);../*.** Conve
58af0 6e 69 65 6e 63 65 20 66 75 6e 63 74 69 6f 6e 73 nience functions
58b00 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 6e 64 for opening and
58b10 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 75 closing files u
58b20 73 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 33 sing .** sqlite3
58b30 5f 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 6f 62 74 _malloc() to obt
58b40 61 69 6e 20 73 70 61 63 65 20 66 6f 72 20 74 68 ain space for th
58b50 65 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 73 74 e file-handle st
58b60 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 ructure..*/.SQLI
58b70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
58b80 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c qlite3OsOpenMall
58b90 6f 63 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a oc(sqlite3_vfs *
58ba0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 , const char *,
58bb0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2a 2c sqlite3_file **,
58bc0 20 69 6e 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 int,int*);.SQLI
58bd0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
58be0 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 qlite3OsCloseFre
58bf0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
58c00 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 );..#endif /* _S
58c10 51 4c 49 54 45 5f 4f 53 5f 48 5f 20 2a 2f 0a 0a QLITE_OS_H_ */..
58c20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
58c30 45 6e 64 20 6f 66 20 6f 73 2e 68 20 2a 2a 2a 2a End of os.h ****
58c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
58c70 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
58c80 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
58c90 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
58ca0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
58cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
58cc0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
58cd0 49 6e 63 6c 75 64 65 20 6d 75 74 65 78 2e 68 20 Include mutex.h
58ce0 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
58cf0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a sqliteInt.h ***
58d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
58d10 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
58d20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 Begin file mutex
58d30 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
58d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
58d60 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 /*.** 2007 Augus
58d70 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 t 28.**.** The a
58d80 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
58d90 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
58da0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
58db0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
58dc0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
58dd0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
58de0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
58df0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
58e00 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
58e10 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
58e20 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
58e30 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
58e40 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
58e50 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
58e60 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
58e70 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
58e80 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
58e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58ed0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ***.**.** This f
58ee0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ile contains the
58ef0 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65 72 20 66 common header f
58f00 6f 72 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70 or all mutex imp
58f10 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a lementations..**
58f20 20 54 68 65 20 73 71 6c 69 74 65 49 6e 74 2e 68 The sqliteInt.h
58f30 20 68 65 61 64 65 72 20 23 69 6e 63 6c 75 64 65 header #include
58f40 73 20 74 68 69 73 20 66 69 6c 65 20 73 6f 20 74 s this file so t
58f50 68 61 74 20 69 74 20 69 73 20 61 76 61 69 6c 61 hat it is availa
58f60 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 73 6f ble.** to all so
58f70 75 72 63 65 20 66 69 6c 65 73 2e 20 20 57 65 20 urce files. We
58f80 62 72 65 61 6b 20 69 74 20 6f 75 74 20 69 6e 20 break it out in
58f90 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 65 an effort to kee
58fa0 70 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65 p the code.** be
58fb0 74 74 65 72 20 6f 72 67 61 6e 69 7a 65 64 2e 0a tter organized..
58fc0 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 73 6f 75 **.** NOTE: sou
58fd0 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 rce files should
58fe0 20 2a 6e 6f 74 2a 20 23 69 6e 63 6c 75 64 65 20 *not* #include
58ff0 74 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 this header file
59000 20 64 69 72 65 63 74 6c 79 2e 0a 2a 2a 20 53 6f directly..** So
59010 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c urce files shoul
59020 64 20 23 69 6e 63 6c 75 64 65 20 74 68 65 20 73 d #include the s
59030 71 6c 69 74 65 49 6e 74 2e 68 20 66 69 6c 65 20 qliteInt.h file
59040 61 6e 64 20 6c 65 74 20 74 68 61 74 20 66 69 6c and let that fil
59050 65 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 74 68 69 e.** include thi
59060 73 20 6f 6e 65 20 69 6e 64 69 72 65 63 74 6c 79 s one indirectly
59070 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 ..**.** $Id: mut
59080 65 78 2e 68 2c 76 20 31 2e 39 20 32 30 30 38 2f ex.h,v 1.9 2008/
59090 31 30 2f 30 37 20 31 35 3a 32 35 3a 34 38 20 64 10/07 15:25:48 d
590a0 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a rh Exp $.*/.../*
590b0 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77 .** Figure out w
590c0 68 61 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 hat version of t
590d0 68 65 20 63 6f 64 65 20 74 6f 20 75 73 65 2e 20 he code to use.
590e0 20 54 68 65 20 63 68 6f 69 63 65 73 20 61 72 65 The choices are
590f0 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f .**.** SQLITE_
59100 4d 55 54 45 58 5f 4f 4d 49 54 20 20 20 20 20 20 MUTEX_OMIT
59110 20 20 20 4e 6f 20 6d 75 74 65 78 20 6c 6f 67 69 No mutex logi
59120 63 2e 20 20 4e 6f 74 20 65 76 65 6e 20 73 74 75 c. Not even stu
59130 62 73 2e 20 20 54 68 65 0a 2a 2a 20 20 20 20 20 bs. The.**
59140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59150 20 20 20 20 20 20 20 20 6d 75 74 65 78 65 73 20 mutexes
59160 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 63 61 6e implemention can
59170 6e 6f 74 20 62 65 20 6f 76 65 72 72 69 64 64 65 not be overridde
59180 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 n.**
59190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
591a0 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a at start-time..
591b0 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d **.** SQLITE_M
591c0 55 54 45 58 5f 4e 4f 4f 50 20 20 20 20 20 20 20 UTEX_NOOP
591d0 20 20 46 6f 72 20 73 69 6e 67 6c 65 2d 74 68 72 For single-thr
591e0 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f eaded applicatio
591f0 6e 73 2e 20 20 4e 6f 0a 2a 2a 20 20 20 20 20 20 ns. No.**
59200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59210 20 20 20 20 20 20 20 6d 75 74 75 61 6c 20 65 78 mutual ex
59220 63 6c 75 73 69 6f 6e 20 69 73 20 70 72 6f 76 69 clusion is provi
59230 64 65 64 2e 20 20 42 75 74 20 74 68 69 73 0a 2a ded. But this.*
59240 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
59250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6d im
59260 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e plementation can
59270 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 61 be overridden a
59280 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
59290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
592a0 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a start-time..**.
592b0 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 ** SQLITE_MUTE
592c0 58 5f 50 54 48 52 45 41 44 53 20 20 20 20 20 46 X_PTHREADS F
592d0 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 or multi-threade
592e0 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f d applications o
592f0 6e 20 55 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 n Unix..**.**
59300 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 SQLITE_MUTEX_W32
59310 20 20 20 20 20 20 20 20 20 20 46 6f 72 20 6d 75 For mu
59320 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 70 lti-threaded app
59330 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 57 69 6e lications on Win
59340 33 32 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 32..**.** SQLI
59350 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 20 20 20 TE_MUTEX_OS2
59360 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d For multi-
59370 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 threaded applica
59380 74 69 6f 6e 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a tions on OS/2..*
59390 2f 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 54 48 /.#if !SQLITE_TH
593a0 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e READSAFE.# defin
593b0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f e SQLITE_MUTEX_O
593c0 4d 49 54 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 MIT.#endif.#if S
593d0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
593e0 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c && !defined(SQL
593f0 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a ITE_MUTEX_NOOP).
59400 23 20 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f # if SQLITE_OS_
59410 55 4e 49 58 0a 23 20 20 20 20 64 65 66 69 6e 65 UNIX.# define
59420 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 SQLITE_MUTEX_PT
59430 48 52 45 41 44 53 0a 23 20 20 65 6c 69 66 20 53 HREADS.# elif S
59440 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 QLITE_OS_WIN.#
59450 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
59460 4d 55 54 45 58 5f 57 33 32 0a 23 20 20 65 6c 69 MUTEX_W32.# eli
59470 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a f SQLITE_OS_OS2.
59480 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 # define SQLI
59490 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 23 20 20 TE_MUTEX_OS2.#
594a0 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65 else.# define
594b0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f SQLITE_MUTEX_NO
594c0 4f 50 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 OP.# endif.#end
594d0 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
594e0 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a E_MUTEX_OMIT./*.
594f0 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 ** If this is a
59500 6e 6f 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e 74 61 no-op implementa
59510 74 69 6f 6e 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 tion, implement
59520 65 76 65 72 79 74 68 69 6e 67 20 61 73 20 6d 61 everything as ma
59530 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 cros..*/.#define
59540 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
59550 6c 6c 6f 63 28 58 29 20 20 20 20 28 28 73 71 6c lloc(X) ((sql
59560 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23 ite3_mutex*)8).#
59570 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d define sqlite3_m
59580 75 74 65 78 5f 66 72 65 65 28 58 29 0a 23 64 65 utex_free(X).#de
59590 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 fine sqlite3_mut
595a0 65 78 5f 65 6e 74 65 72 28 58 29 0a 23 64 65 66 ex_enter(X).#def
595b0 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ine sqlite3_mute
595c0 78 5f 74 72 79 28 58 29 20 20 20 20 20 20 53 51 x_try(X) SQ
595d0 4c 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 LITE_OK.#define
595e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
595f0 61 76 65 28 58 29 0a 23 64 65 66 69 6e 65 20 73 ave(X).#define s
59600 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
59610 64 28 58 29 20 20 20 20 20 31 0a 23 64 65 66 69 d(X) 1.#defi
59620 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ne sqlite3_mutex
59630 5f 6e 6f 74 68 65 6c 64 28 58 29 20 20 31 0a 23 _notheld(X) 1.#
59640 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 define sqlite3Mu
59650 74 65 78 41 6c 6c 6f 63 28 58 29 20 20 20 20 20 texAlloc(X)
59660 20 28 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ((sqlite3_mutex
59670 2a 29 38 29 0a 23 64 65 66 69 6e 65 20 73 71 6c *)8).#define sql
59680 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29 20 ite3MutexInit()
59690 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b SQLITE_OK
596a0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 .#define sqlite3
596b0 4d 75 74 65 78 45 6e 64 28 29 0a 23 65 6e 64 69 MutexEnd().#endi
596c0 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c f /* defined(SQL
596d0 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 29 20 ITE_MUTEX_OMIT)
596e0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
596f0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 *** End of mutex
59700 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
59710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59730 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
59740 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
59750 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
59760 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 in sqliteInt.h
59770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59780 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 **/.../*.** Each
59790 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
597a0 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79 o be accessed by
597b0 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 61 the system is a
597c0 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 n instance.** of
597d0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
597e0 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 72 65 tructure. There
597f0 20 61 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 74 77 are normally tw
59800 6f 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 o of these struc
59810 74 75 72 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 tures.** in the
59820 73 71 6c 69 74 65 2e 61 44 62 5b 5d 20 61 72 72 sqlite.aDb[] arr
59830 61 79 2e 20 20 61 44 62 5b 30 5d 20 69 73 20 74 ay. aDb[0] is t
59840 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
59850 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 61 44 62 file and.** aDb
59860 5b 31 5d 20 69 73 20 74 68 65 20 64 61 74 61 62 [1] is the datab
59870 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 74 6f ase file used to
59880 20 68 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 hold temporary
59890 74 61 62 6c 65 73 2e 20 20 41 64 64 69 74 69 6f tables. Additio
598a0 6e 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 73 nal.** databases
598b0 20 6d 61 79 20 62 65 20 61 74 74 61 63 68 65 64 may be attached
598c0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 44 62 20 7b ..*/.struct Db {
598d0 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 . char *zName;
598e0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
598f0 6f 66 20 74 68 69 73 20 64 61 74 61 62 61 73 65 of this database
59900 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 */. Btree *pBt
59910 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ; /* Th
59920 65 20 42 2a 54 72 65 65 20 73 74 72 75 63 74 75 e B*Tree structu
59930 72 65 20 66 6f 72 20 74 68 69 73 20 64 61 74 61 re for this data
59940 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 base file */. u
59950 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 8 inTrans;
59960 20 20 20 20 2f 2a 20 30 3a 20 6e 6f 74 20 77 72 /* 0: not wr
59970 69 74 61 62 6c 65 2e 20 20 31 3a 20 54 72 61 6e itable. 1: Tran
59980 73 61 63 74 69 6f 6e 2e 20 20 32 3a 20 43 68 65 saction. 2: Che
59990 63 6b 70 6f 69 6e 74 20 2a 2f 0a 20 20 75 38 20 ckpoint */. u8
599a0 73 61 66 65 74 79 5f 6c 65 76 65 6c 3b 20 20 20 safety_level;
599b0 20 20 2f 2a 20 48 6f 77 20 61 67 67 72 65 73 73 /* How aggress
599c0 69 76 65 20 61 74 20 73 79 6e 63 69 6e 67 20 64 ive at syncing d
599d0 61 74 61 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 ata to disk */.
599e0 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 Schema *pSchema
599f0 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 ; /* Pointer
59a00 20 74 6f 20 64 61 74 61 62 61 73 65 20 73 63 68 to database sch
59a10 65 6d 61 20 28 70 6f 73 73 69 62 6c 79 20 73 68 ema (possibly sh
59a20 61 72 65 64 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ared) */.};../*.
59a30 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
59a40 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
59a50 73 74 72 75 63 74 75 72 65 20 73 74 6f 72 65 73 structure stores
59a60 20 61 20 64 61 74 61 62 61 73 65 20 73 63 68 65 a database sche
59a70 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ma..**.** If the
59a80 72 65 20 61 72 65 20 6e 6f 20 76 69 72 74 75 61 re are no virtua
59a90 6c 20 74 61 62 6c 65 73 20 63 6f 6e 66 69 67 75 l tables configu
59aa0 72 65 64 20 69 6e 20 74 68 69 73 20 73 63 68 65 red in this sche
59ab0 6d 61 2c 20 74 68 65 0a 2a 2a 20 53 63 68 65 6d ma, the.** Schem
59ac0 61 2e 64 62 20 76 61 72 69 61 62 6c 65 20 69 73 a.db variable is
59ad0 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 41 66 set to NULL. Af
59ae0 74 65 72 20 74 68 65 20 66 69 72 73 74 20 76 69 ter the first vi
59af0 72 74 75 61 6c 20 74 61 62 6c 65 0a 2a 2a 20 68 rtual table.** h
59b00 61 73 20 62 65 65 6e 20 61 64 64 65 64 2c 20 69 as been added, i
59b10 74 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e t is set to poin
59b20 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 t to the databas
59b30 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a e connection .**
59b40 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 used to create
59b50 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 the connection.
59b60 4f 6e 63 65 20 61 20 76 69 72 74 75 61 6c 20 74 Once a virtual t
59b70 61 62 6c 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a able has been.**
59b80 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 53 63 added to the Sc
59b90 68 65 6d 61 20 73 74 72 75 63 74 75 72 65 20 61 hema structure a
59ba0 6e 64 20 74 68 65 20 53 63 68 65 6d 61 2e 64 62 nd the Schema.db
59bb0 20 76 61 72 69 61 62 6c 65 20 70 6f 70 75 6c 61 variable popula
59bc0 74 65 64 2c 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68 ted, .** only th
59bd0 61 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e at database conn
59be0 65 63 74 69 6f 6e 20 6d 61 79 20 75 73 65 20 74 ection may use t
59bf0 68 65 20 53 63 68 65 6d 61 20 74 6f 20 70 72 65 he Schema to pre
59c00 70 61 72 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 pare .** stateme
59c10 6e 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 nts..*/.struct S
59c20 63 68 65 6d 61 20 7b 0a 20 20 69 6e 74 20 73 63 chema {. int sc
59c30 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f hema_cookie; /
59c40 2a 20 44 61 74 61 62 61 73 65 20 73 63 68 65 6d * Database schem
59c50 61 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 a version number
59c60 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a for this file *
59c70 2f 0a 20 20 48 61 73 68 20 74 62 6c 48 61 73 68 /. Hash tblHash
59c80 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 ; /* All
59c90 74 61 62 6c 65 73 20 69 6e 64 65 78 65 64 20 62 tables indexed b
59ca0 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 y name */. Hash
59cb0 20 69 64 78 48 61 73 68 3b 20 20 20 20 20 20 20 idxHash;
59cc0 20 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65 64 29 20 /* All (named)
59cd0 69 6e 64 69 63 65 73 20 69 6e 64 65 78 65 64 20 indices indexed
59ce0 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 by name */. Has
59cf0 68 20 74 72 69 67 48 61 73 68 3b 20 20 20 20 20 h trigHash;
59d00 20 20 2f 2a 20 41 6c 6c 20 74 72 69 67 67 65 72 /* All trigger
59d10 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d s indexed by nam
59d20 65 20 2a 2f 0a 20 20 48 61 73 68 20 66 6b 65 79 e */. Hash fkey
59d30 48 61 73 68 3b 20 20 20 20 20 20 20 2f 2a 20 41 Hash; /* A
59d40 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 ll foreign keys
59d50 62 79 20 72 65 66 65 72 65 6e 63 65 64 20 74 61 by referenced ta
59d60 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 61 ble name */. Ta
59d70 62 6c 65 20 2a 70 53 65 71 54 61 62 3b 20 20 20 ble *pSeqTab;
59d80 20 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 /* The sqlite
59d90 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 _sequence table
59da0 75 73 65 64 20 62 79 20 41 55 54 4f 49 4e 43 52 used by AUTOINCR
59db0 45 4d 45 4e 54 20 2a 2f 0a 20 20 75 38 20 66 69 EMENT */. u8 fi
59dc0 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20 20 le_format;
59dd0 2f 2a 20 53 63 68 65 6d 61 20 66 6f 72 6d 61 74 /* Schema format
59de0 20 76 65 72 73 69 6f 6e 20 66 6f 72 20 74 68 69 version for thi
59df0 73 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 65 s file */. u8 e
59e00 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nc;
59e10 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 6e /* Text encodin
59e20 67 20 75 73 65 64 20 62 79 20 74 68 69 73 20 64 g used by this d
59e30 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 31 36 atabase */. u16
59e40 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 flags;
59e50 20 20 2f 2a 20 46 6c 61 67 73 20 61 73 73 6f 63 /* Flags assoc
59e60 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 iated with this
59e70 73 63 68 65 6d 61 20 2a 2f 0a 20 20 69 6e 74 20 schema */. int
59e80 63 61 63 68 65 5f 73 69 7a 65 3b 20 20 20 20 20 cache_size;
59e90 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
59ea0 67 65 73 20 74 6f 20 75 73 65 20 69 6e 20 74 68 ges to use in th
59eb0 65 20 63 61 63 68 65 20 2a 2f 0a 23 69 66 6e 64 e cache */.#ifnd
59ec0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
59ed0 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 73 71 IRTUALTABLE. sq
59ee0 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 lite3 *db;
59ef0 20 20 20 2f 2a 20 22 4f 77 6e 65 72 22 20 63 6f /* "Owner" co
59f00 6e 6e 65 63 74 69 6f 6e 2e 20 53 65 65 20 63 6f nnection. See co
59f10 6d 6d 65 6e 74 20 61 62 6f 76 65 20 2a 2f 0a 23 mment above */.#
59f20 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 endif.};../*.**
59f30 54 68 65 73 65 20 6d 61 63 72 6f 73 20 63 61 6e These macros can
59f40 20 62 65 20 75 73 65 64 20 74 6f 20 74 65 73 74 be used to test
59f50 2c 20 73 65 74 2c 20 6f 72 20 63 6c 65 61 72 20 , set, or clear
59f60 62 69 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 bits in the .**
59f70 44 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a Db.flags field..
59f80 2a 2f 0a 23 64 65 66 69 6e 65 20 44 62 48 61 73 */.#define DbHas
59f90 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 Property(D,I,P)
59fa0 20 20 20 20 28 28 28 44 29 2d 3e 61 44 62 5b 49 (((D)->aDb[I
59fb0 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 ].pSchema->flags
59fc0 26 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 &(P))==(P)).#def
59fd0 69 6e 65 20 44 62 48 61 73 41 6e 79 50 72 6f 70 ine DbHasAnyProp
59fe0 65 72 74 79 28 44 2c 49 2c 50 29 20 20 28 28 28 erty(D,I,P) (((
59ff0 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 D)->aDb[I].pSche
5a000 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 3d ma->flags&(P))!=
5a010 30 29 0a 23 64 65 66 69 6e 65 20 44 62 53 65 74 0).#define DbSet
5a020 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 Property(D,I,P)
5a030 20 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e (D)->aDb[I].
5a040 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 7c 3d pSchema->flags|=
5a050 28 50 29 0a 23 64 65 66 69 6e 65 20 44 62 43 6c (P).#define DbCl
5a060 65 61 72 50 72 6f 70 65 72 74 79 28 44 2c 49 2c earProperty(D,I,
5a070 50 29 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d P) (D)->aDb[I]
5a080 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 .pSchema->flags&
5a090 3d 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c =~(P)../*.** All
5a0a0 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 owed values for
5a0b0 74 68 65 20 44 42 2e 66 6c 61 67 73 20 66 69 65 the DB.flags fie
5a0c0 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 44 42 ld..**.** The DB
5a0d0 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 66 6c _SchemaLoaded fl
5a0e0 61 67 20 69 73 20 73 65 74 20 61 66 74 65 72 20 ag is set after
5a0f0 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 the database sch
5a100 65 6d 61 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 ema has been.**
5a110 72 65 61 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e read into intern
5a120 61 6c 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a al hash tables..
5a130 2a 2a 0a 2a 2a 20 44 42 5f 55 6e 72 65 73 65 74 **.** DB_Unreset
5a140 56 69 65 77 73 20 6d 65 61 6e 73 20 74 68 61 74 Views means that
5a150 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 76 69 65 one or more vie
5a160 77 73 20 68 61 76 65 20 63 6f 6c 75 6d 6e 20 6e ws have column n
5a170 61 6d 65 73 20 74 68 61 74 0a 2a 2a 20 68 61 76 ames that.** hav
5a180 65 20 62 65 65 6e 20 66 69 6c 6c 65 64 20 6f 75 e been filled ou
5a190 74 2e 20 20 49 66 20 74 68 65 20 73 63 68 65 6d t. If the schem
5a1a0 61 20 63 68 61 6e 67 65 73 2c 20 74 68 65 73 65 a changes, these
5a1b0 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6d 69 column names mi
5a1c0 67 68 74 0a 2a 2a 20 63 68 61 6e 67 65 73 20 61 ght.** changes a
5a1d0 6e 64 20 73 6f 20 74 68 65 20 76 69 65 77 20 77 nd so the view w
5a1e0 69 6c 6c 20 6e 65 65 64 20 74 6f 20 62 65 20 72 ill need to be r
5a1f0 65 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 eset..*/.#define
5a200 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 DB_SchemaLoaded
5a210 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 54 0x0001 /* T
5a220 68 65 20 73 63 68 65 6d 61 20 68 61 73 20 62 65 he schema has be
5a230 65 6e 20 6c 6f 61 64 65 64 20 2a 2f 0a 23 64 65 en loaded */.#de
5a240 66 69 6e 65 20 44 42 5f 55 6e 72 65 73 65 74 56 fine DB_UnresetV
5a250 69 65 77 73 20 20 20 20 30 78 30 30 30 32 20 20 iews 0x0002
5a260 2f 2a 20 53 6f 6d 65 20 76 69 65 77 73 20 68 61 /* Some views ha
5a270 76 65 20 64 65 66 69 6e 65 64 20 63 6f 6c 75 6d ve defined colum
5a280 6e 20 6e 61 6d 65 73 20 2a 2f 0a 23 64 65 66 69 n names */.#defi
5a290 6e 65 20 44 42 5f 45 6d 70 74 79 20 20 20 20 20 ne DB_Empty
5a2a0 20 20 20 20 20 20 30 78 30 30 30 34 20 20 2f 2a 0x0004 /*
5a2b0 20 54 68 65 20 66 69 6c 65 20 69 73 20 65 6d 70 The file is emp
5a2c0 74 79 20 28 6c 65 6e 67 74 68 20 30 20 62 79 74 ty (length 0 byt
5a2d0 65 73 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 es) */../*.** Th
5a2e0 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 66 e number of diff
5a2f0 65 72 65 6e 74 20 6b 69 6e 64 73 20 6f 66 20 74 erent kinds of t
5a300 68 69 6e 67 73 20 74 68 61 74 20 63 61 6e 20 62 hings that can b
5a310 65 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 75 73 69 e limited.** usi
5a320 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6c ng the sqlite3_l
5a330 69 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 imit() interface
5a340 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
5a350 49 54 45 5f 4e 5f 4c 49 4d 49 54 20 28 53 51 4c ITE_N_LIMIT (SQL
5a360 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 ITE_LIMIT_TRIGGE
5a370 52 5f 44 45 50 54 48 2b 31 29 0a 0a 2f 2a 0a 2a R_DEPTH+1)../*.*
5a380 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c * Lookaside mall
5a390 6f 63 20 69 73 20 61 20 73 65 74 20 6f 66 20 66 oc is a set of f
5a3a0 69 78 65 64 2d 73 69 7a 65 20 62 75 66 66 65 72 ixed-size buffer
5a3b0 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 s that can be us
5a3c0 65 64 0a 2a 2a 20 74 6f 20 73 61 74 69 73 66 79 ed.** to satisfy
5a3d0 20 73 6d 61 6c 6c 20 74 72 61 6e 73 69 65 6e 74 small transient
5a3e0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
5a3f0 6f 6e 20 72 65 71 75 65 73 74 73 20 66 6f 72 20 on requests for
5a400 6f 62 6a 65 63 74 73 0a 2a 2a 20 61 73 73 6f 63 objects.** assoc
5a410 69 61 74 65 64 20 77 69 74 68 20 61 20 70 61 72 iated with a par
5a420 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 ticular database
5a430 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 connection. Th
5a440 65 20 75 73 65 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b e use of.** look
5a450 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 70 72 6f aside malloc pro
5a460 76 69 64 65 73 20 61 20 73 69 67 6e 69 66 69 63 vides a signific
5a470 61 6e 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 ant performance
5a480 65 6e 68 61 6e 63 65 6d 65 6e 74 0a 2a 2a 20 28 enhancement.** (
5a490 61 70 70 72 6f 78 20 31 30 25 29 20 62 79 20 61 approx 10%) by a
5a4a0 76 6f 69 64 69 6e 67 20 6e 75 6d 65 72 6f 75 73 voiding numerous
5a4b0 20 6d 61 6c 6c 6f 63 2f 66 72 65 65 20 72 65 71 malloc/free req
5a4c0 75 65 73 74 73 20 77 68 69 6c 65 20 70 61 72 73 uests while pars
5a4d0 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 ing.** SQL state
5a4e0 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ments..**.** The
5a4f0 20 4c 6f 6f 6b 61 73 69 64 65 20 73 74 72 75 63 Lookaside struc
5a500 74 75 72 65 20 68 6f 6c 64 73 20 63 6f 6e 66 69 ture holds confi
5a510 67 75 72 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 guration informa
5a520 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 0a 2a tion about the.*
5a530 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c * lookaside mall
5a540 6f 63 20 73 75 62 73 79 73 74 65 6d 2e 20 20 45 oc subsystem. E
5a550 61 63 68 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 ach available me
5a560 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
5a570 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 in.** the lookas
5a580 69 64 65 20 73 75 62 73 79 73 74 65 6d 20 69 73 ide subsystem is
5a590 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 6c 69 6e stored on a lin
5a5a0 6b 65 64 20 6c 69 73 74 20 6f 66 20 4c 6f 6f 6b ked list of Look
5a5b0 61 73 69 64 65 53 6c 6f 74 0a 2a 2a 20 6f 62 6a asideSlot.** obj
5a5c0 65 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 6f 6b ects..**.** Look
5a5d0 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e aside allocation
5a5e0 73 20 61 72 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 s are only allow
5a5f0 65 64 20 66 6f 72 20 6f 62 6a 65 63 74 73 20 74 ed for objects t
5a600 68 61 74 20 61 72 65 20 61 73 73 6f 63 69 61 74 hat are associat
5a610 65 64 0a 2a 2a 20 77 69 74 68 20 61 20 70 61 72 ed.** with a par
5a620 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 ticular database
5a630 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 48 65 connection. He
5a640 6e 63 65 2c 20 73 63 68 65 6d 61 20 69 6e 66 6f nce, schema info
5a650 72 6d 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a rmation cannot.*
5a660 2a 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 6c * be stored in l
5a670 6f 6f 6b 61 73 69 64 65 20 62 65 63 61 75 73 65 ookaside because
5a680 20 69 6e 20 73 68 61 72 65 64 20 63 61 63 68 65 in shared cache
5a690 20 6d 6f 64 65 20 74 68 65 20 73 63 68 65 6d 61 mode the schema
5a6a0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 information.**
5a6b0 69 73 20 73 68 61 72 65 64 20 62 79 20 6d 75 6c is shared by mul
5a6c0 74 69 70 6c 65 20 64 61 74 61 62 61 73 65 20 63 tiple database c
5a6d0 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 54 68 65 onnections. The
5a6e0 72 65 66 6f 72 65 2c 20 77 68 69 6c 65 20 70 61 refore, while pa
5a6f0 72 73 69 6e 67 0a 2a 2a 20 73 63 68 65 6d 61 20 rsing.** schema
5a700 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65 information, the
5a710 20 4c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 Lookaside.bEnab
5a720 6c 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 led flag is clea
5a730 72 65 64 20 73 6f 20 74 68 61 74 0a 2a 2a 20 6c red so that.** l
5a740 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 ookaside allocat
5a750 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 75 73 65 ions are not use
5a760 64 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 74 d to construct t
5a770 68 65 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 he schema object
5a780 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4c 6f 6f s..*/.struct Loo
5a790 6b 61 73 69 64 65 20 7b 0a 20 20 75 31 36 20 73 kaside {. u16 s
5a7a0 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 z;
5a7b0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 /* Size of ea
5a7c0 63 68 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 ch buffer in byt
5a7d0 65 73 20 2a 2f 0a 20 20 75 38 20 62 45 6e 61 62 es */. u8 bEnab
5a7e0 6c 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 led;
5a7f0 2f 2a 20 46 61 6c 73 65 20 74 6f 20 64 69 73 61 /* False to disa
5a800 62 6c 65 20 6e 65 77 20 6c 6f 6f 6b 61 73 69 64 ble new lookasid
5a810 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f e allocations */
5a820 0a 20 20 75 38 20 62 4d 61 6c 6c 6f 63 65 64 3b . u8 bMalloced;
5a830 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
5a840 75 65 20 69 66 20 70 53 74 61 72 74 20 6f 62 74 ue if pStart obt
5a850 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
5a860 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 e3_malloc() */.
5a870 20 69 6e 74 20 6e 4f 75 74 3b 20 20 20 20 20 20 int nOut;
5a880 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
5a890 65 72 20 6f 66 20 62 75 66 66 65 72 73 20 63 75 er of buffers cu
5a8a0 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 rrently checked
5a8b0 6f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 4f out */. int mxO
5a8c0 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ut;
5a8d0 20 2f 2a 20 48 69 67 68 77 61 74 65 72 20 6d 61 /* Highwater ma
5a8e0 72 6b 20 66 6f 72 20 6e 4f 75 74 20 2a 2f 0a 20 rk for nOut */.
5a8f0 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a LookasideSlot *
5a900 70 46 72 65 65 3b 20 20 20 2f 2a 20 4c 69 73 74 pFree; /* List
5a910 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 62 75 of available bu
5a920 66 66 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20 ffers */. void
5a930 2a 70 53 74 61 72 74 3b 20 20 20 20 20 20 20 20 *pStart;
5a940 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 /* First byte
5a950 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 of available me
5a960 6d 6f 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 mory space */.
5a970 76 6f 69 64 20 2a 70 45 6e 64 3b 20 20 20 20 20 void *pEnd;
5a980 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
5a990 20 62 79 74 65 20 70 61 73 74 20 65 6e 64 20 6f byte past end o
5a9a0 66 20 61 76 61 69 6c 61 62 6c 65 20 73 70 61 63 f available spac
5a9b0 65 20 2a 2f 0a 7d 3b 0a 73 74 72 75 63 74 20 4c e */.};.struct L
5a9c0 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 7b 0a 20 ookasideSlot {.
5a9d0 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a LookasideSlot *
5a9e0 70 4e 65 78 74 3b 20 20 20 20 2f 2a 20 4e 65 78 pNext; /* Nex
5a9f0 74 20 62 75 66 66 65 72 20 69 6e 20 74 68 65 20 t buffer in the
5aa00 6c 69 73 74 20 6f 66 20 66 72 65 65 20 62 75 66 list of free buf
5aa10 66 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a fers */.};../*.*
5aa20 2a 20 41 20 68 61 73 68 20 74 61 62 6c 65 20 66 * A hash table f
5aa30 6f 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 or function defi
5aa40 6e 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 48 nitions..**.** H
5aa50 61 73 68 20 65 61 63 68 20 46 75 6e 63 44 65 66 ash each FuncDef
5aa60 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 structure into
5aa70 6f 6e 65 20 6f 66 20 74 68 65 20 46 75 6e 63 44 one of the FuncD
5aa80 65 66 48 61 73 68 2e 61 5b 5d 20 73 6c 6f 74 73 efHash.a[] slots
5aa90 2e 0a 2a 2a 20 43 6f 6c 6c 69 73 69 6f 6e 73 20 ..** Collisions
5aaa0 61 72 65 20 6f 6e 20 74 68 65 20 46 75 6e 63 44 are on the FuncD
5aab0 65 66 2e 70 48 61 73 68 20 63 68 61 69 6e 2e 0a ef.pHash chain..
5aac0 2a 2f 0a 73 74 72 75 63 74 20 46 75 6e 63 44 65 */.struct FuncDe
5aad0 66 48 61 73 68 20 7b 0a 20 20 46 75 6e 63 44 65 fHash {. FuncDe
5aae0 66 20 2a 61 5b 32 33 5d 3b 20 20 20 20 20 20 20 f *a[23];
5aaf0 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f /* Hash table fo
5ab00 72 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d r functions */.}
5ab10 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 ;../*.** Each da
5ab20 74 61 62 61 73 65 20 69 73 20 61 6e 20 69 6e 73 tabase is an ins
5ab30 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
5ab40 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
5ab50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
5ab60 74 65 2e 6c 61 73 74 52 6f 77 69 64 20 72 65 63 te.lastRowid rec
5ab70 6f 72 64 73 20 74 68 65 20 6c 61 73 74 20 69 6e ords the last in
5ab80 73 65 72 74 20 72 6f 77 69 64 20 67 65 6e 65 72 sert rowid gener
5ab90 61 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e ated by an.** in
5aba0 73 65 72 74 20 73 74 61 74 65 6d 65 6e 74 2e 20 sert statement.
5abb0 20 49 6e 73 65 72 74 73 20 6f 6e 20 76 69 65 77 Inserts on view
5abc0 73 20 64 6f 20 6e 6f 74 20 61 66 66 65 63 74 20 s do not affect
5abd0 69 74 73 20 76 61 6c 75 65 2e 20 20 45 61 63 68 its value. Each
5abe0 0a 2a 2a 20 74 72 69 67 67 65 72 20 68 61 73 20 .** trigger has
5abf0 69 74 73 20 6f 77 6e 20 63 6f 6e 74 65 78 74 2c its own context,
5ac00 20 73 6f 20 74 68 61 74 20 6c 61 73 74 52 6f 77 so that lastRow
5ac10 69 64 20 63 61 6e 20 62 65 20 75 70 64 61 74 65 id can be update
5ac20 64 20 69 6e 73 69 64 65 0a 2a 2a 20 74 72 69 67 d inside.** trig
5ac30 67 65 72 73 20 61 73 20 75 73 75 61 6c 2e 20 20 gers as usual.
5ac40 54 68 65 20 70 72 65 76 69 6f 75 73 20 76 61 6c The previous val
5ac50 75 65 20 77 69 6c 6c 20 62 65 20 72 65 73 74 6f ue will be resto
5ac60 72 65 64 20 6f 6e 63 65 20 74 68 65 20 74 72 69 red once the tri
5ac70 67 67 65 72 0a 2a 2a 20 65 78 69 74 73 2e 20 20 gger.** exits.
5ac80 55 70 6f 6e 20 65 6e 74 65 72 69 6e 67 20 61 20 Upon entering a
5ac90 62 65 66 6f 72 65 20 6f 72 20 69 6e 73 74 65 61 before or instea
5aca0 64 20 6f 66 20 74 72 69 67 67 65 72 2c 20 6c 61 d of trigger, la
5acb0 73 74 52 6f 77 69 64 20 69 73 20 6e 6f 0a 2a 2a stRowid is no.**
5acc0 20 6c 6f 6e 67 65 72 20 28 73 69 6e 63 65 20 61 longer (since a
5acd0 66 74 65 72 20 76 65 72 73 69 6f 6e 20 32 2e 38 fter version 2.8
5ace0 2e 31 32 29 20 72 65 73 65 74 20 74 6f 20 2d 31 .12) reset to -1
5acf0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
5ad00 74 65 2e 6e 43 68 61 6e 67 65 20 64 6f 65 73 20 te.nChange does
5ad10 6e 6f 74 20 63 6f 75 6e 74 20 63 68 61 6e 67 65 not count change
5ad20 73 20 77 69 74 68 69 6e 20 74 72 69 67 67 65 72 s within trigger
5ad30 73 20 61 6e 64 20 6b 65 65 70 73 20 6e 6f 0a 2a s and keeps no.*
5ad40 2a 20 63 6f 6e 74 65 78 74 2e 20 20 49 74 20 69 * context. It i
5ad50 73 20 72 65 73 65 74 20 61 74 20 73 74 61 72 74 s reset at start
5ad60 20 6f 66 20 73 71 6c 69 74 65 33 5f 65 78 65 63 of sqlite3_exec
5ad70 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e ..** The sqlite.
5ad80 6c 73 43 68 61 6e 67 65 20 72 65 70 72 65 73 65 lsChange represe
5ad90 6e 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f nts the number o
5ada0 66 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 f changes made b
5adb0 79 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 69 6e y the last.** in
5adc0 73 65 72 74 2c 20 75 70 64 61 74 65 2c 20 6f 72 sert, update, or
5add0 20 64 65 6c 65 74 65 20 73 74 61 74 65 6d 65 6e delete statemen
5ade0 74 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 63 t. It remains c
5adf0 6f 6e 73 74 61 6e 74 20 74 68 72 6f 75 67 68 6f onstant througho
5ae00 75 74 20 74 68 65 0a 2a 2a 20 6c 65 6e 67 74 68 ut the.** length
5ae10 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 of a statement
5ae20 61 6e 64 20 69 73 20 74 68 65 6e 20 75 70 64 61 and is then upda
5ae30 74 65 64 20 62 79 20 4f 50 5f 53 65 74 43 6f 75 ted by OP_SetCou
5ae40 6e 74 73 2e 20 20 49 74 20 6b 65 65 70 73 20 61 nts. It keeps a
5ae50 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 73 74 61 63 .** context stac
5ae60 6b 20 6a 75 73 74 20 6c 69 6b 65 20 6c 61 73 74 k just like last
5ae70 52 6f 77 69 64 20 73 6f 20 74 68 61 74 20 74 68 Rowid so that th
5ae80 65 20 63 6f 75 6e 74 20 6f 66 20 63 68 61 6e 67 e count of chang
5ae90 65 73 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 74 es.** within a t
5aea0 72 69 67 67 65 72 20 69 73 20 6e 6f 74 20 73 65 rigger is not se
5aeb0 65 6e 20 6f 75 74 73 69 64 65 20 74 68 65 20 74 en outside the t
5aec0 72 69 67 67 65 72 2e 20 20 43 68 61 6e 67 65 73 rigger. Changes
5aed0 20 74 6f 20 76 69 65 77 73 20 64 6f 20 6e 6f 74 to views do not
5aee0 0a 2a 2a 20 61 66 66 65 63 74 20 74 68 65 20 76 .** affect the v
5aef0 61 6c 75 65 20 6f 66 20 6c 73 43 68 61 6e 67 65 alue of lsChange
5af00 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e ..** The sqlite.
5af10 63 73 43 68 61 6e 67 65 20 6b 65 65 70 73 20 74 csChange keeps t
5af20 72 61 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 rack of the numb
5af30 65 72 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68 er of current ch
5af40 61 6e 67 65 73 20 28 73 69 6e 63 65 0a 2a 2a 20 anges (since.**
5af50 74 68 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 the last stateme
5af60 6e 74 29 20 61 6e 64 20 69 73 20 75 73 65 64 20 nt) and is used
5af70 74 6f 20 75 70 64 61 74 65 20 73 71 6c 69 74 65 to update sqlite
5af80 5f 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a _lsChange..**.**
5af90 20 54 68 65 20 6d 65 6d 62 65 72 20 76 61 72 69 The member vari
5afa0 61 62 6c 65 73 20 73 71 6c 69 74 65 2e 65 72 72 ables sqlite.err
5afb0 43 6f 64 65 2c 20 73 71 6c 69 74 65 2e 7a 45 72 Code, sqlite.zEr
5afc0 72 4d 73 67 20 61 6e 64 20 73 71 6c 69 74 65 2e rMsg and sqlite.
5afd0 7a 45 72 72 4d 73 67 31 36 0a 2a 2a 20 73 74 6f zErrMsg16.** sto
5afe0 72 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 re the most rece
5aff0 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e nt error code an
5b000 64 2c 20 69 66 20 61 70 70 6c 69 63 61 62 6c 65 d, if applicable
5b010 2c 20 73 74 72 69 6e 67 2e 20 54 68 65 0a 2a 2a , string. The.**
5b020 20 69 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 internal functi
5b030 6f 6e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 on sqlite3Error(
5b040 29 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 ) is used to set
5b050 20 74 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 these variables
5b060 0a 2a 2a 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 .** consistently
5b070 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 ..*/.struct sqli
5b080 74 65 33 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f te3 {. sqlite3_
5b090 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 vfs *pVfs;
5b0a0 20 20 20 20 20 20 2f 2a 20 4f 53 20 49 6e 74 65 /* OS Inte
5b0b0 72 66 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e rface */. int n
5b0c0 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Db;
5b0d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
5b0e0 65 72 20 6f 66 20 62 61 63 6b 65 6e 64 73 20 63 er of backends c
5b0f0 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 urrently in use
5b100 2a 2f 0a 20 20 44 62 20 2a 61 44 62 3b 20 20 20 */. Db *aDb;
5b110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b120 20 20 20 2f 2a 20 41 6c 6c 20 62 61 63 6b 65 6e /* All backen
5b130 64 73 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 ds */. int flag
5b140 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
5b150 20 20 20 20 20 20 2f 2a 20 4d 69 73 63 65 6c 6c /* Miscell
5b160 61 6e 65 6f 75 73 20 66 6c 61 67 73 2e 20 53 65 aneous flags. Se
5b170 65 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 e below */. int
5b180 20 6f 70 65 6e 46 6c 61 67 73 3b 20 20 20 20 20 openFlags;
5b190 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c /* Fl
5b1a0 61 67 73 20 70 61 73 73 65 64 20 74 6f 20 73 71 ags passed to sq
5b1b0 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 lite3_vfs.xOpen(
5b1c0 29 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f ) */. int errCo
5b1d0 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 de;
5b1e0 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 /* Most rec
5b1f0 65 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 20 28 ent error code (
5b200 53 51 4c 49 54 45 5f 2a 29 20 2a 2f 0a 20 20 69 SQLITE_*) */. i
5b210 6e 74 20 65 72 72 4d 61 73 6b 3b 20 20 20 20 20 nt errMask;
5b220 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5b230 26 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77 & result codes w
5b240 69 74 68 20 74 68 69 73 20 62 65 66 6f 72 65 20 ith this before
5b250 72 65 74 75 72 6e 69 6e 67 20 2a 2f 0a 20 20 75 returning */. u
5b260 38 20 61 75 74 6f 43 6f 6d 6d 69 74 3b 20 20 20 8 autoCommit;
5b270 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5b280 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 The auto-commit
5b290 66 6c 61 67 2e 20 2a 2f 0a 20 20 75 38 20 74 65 flag. */. u8 te
5b2a0 6d 70 5f 73 74 6f 72 65 3b 20 20 20 20 20 20 20 mp_store;
5b2b0 20 20 20 20 20 20 20 20 20 2f 2a 20 31 3a 20 66 /* 1: f
5b2c0 69 6c 65 20 32 3a 20 6d 65 6d 6f 72 79 20 30 3a ile 2: memory 0:
5b2d0 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 default */. u8
5b2e0 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 mallocFailed;
5b2f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
5b300 72 75 65 20 69 66 20 77 65 20 68 61 76 65 20 73 rue if we have s
5b310 65 65 6e 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 een a malloc fai
5b320 6c 75 72 65 20 2a 2f 0a 20 20 75 38 20 64 66 6c lure */. u8 dfl
5b330 74 4c 6f 63 6b 4d 6f 64 65 3b 20 20 20 20 20 20 tLockMode;
5b340 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 /* Defau
5b350 6c 74 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 lt locking-mode
5b360 66 6f 72 20 61 74 74 61 63 68 65 64 20 64 62 73 for attached dbs
5b370 20 2a 2f 0a 20 20 75 38 20 64 66 6c 74 4a 6f 75 */. u8 dfltJou
5b380 72 6e 61 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 rnalMode;
5b390 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6a /* Default j
5b3a0 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 66 6f 72 20 ournal mode for
5b3b0 61 74 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a attached dbs */.
5b3c0 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 6e 65 signed char ne
5b3d0 78 74 41 75 74 6f 76 61 63 3b 20 20 20 20 20 20 xtAutovac;
5b3e0 2f 2a 20 41 75 74 6f 76 61 63 20 73 65 74 74 69 /* Autovac setti
5b3f0 6e 67 20 61 66 74 65 72 20 56 41 43 55 55 4d 20 ng after VACUUM
5b400 69 66 20 3e 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 if >=0 */. int
5b410 6e 65 78 74 50 61 67 65 73 69 7a 65 3b 20 20 20 nextPagesize;
5b420 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
5b430 65 73 69 7a 65 20 61 66 74 65 72 20 56 41 43 55 esize after VACU
5b440 55 4d 20 69 66 20 3e 30 20 2a 2f 0a 20 20 69 6e UM if >0 */. in
5b450 74 20 6e 54 61 62 6c 65 3b 20 20 20 20 20 20 20 t nTable;
5b460 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
5b470 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 umber of tables
5b480 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
5b490 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 44 */. CollSeq *pD
5b4a0 66 6c 74 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 fltColl;
5b4b0 20 20 20 2f 2a 20 54 68 65 20 64 65 66 61 75 6c /* The defaul
5b4c0 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 t collating sequ
5b4d0 65 6e 63 65 20 28 42 49 4e 41 52 59 29 20 2a 2f ence (BINARY) */
5b4e0 0a 20 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64 . i64 lastRowid
5b4f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5b500 20 2f 2a 20 52 4f 57 49 44 20 6f 66 20 6d 6f 73 /* ROWID of mos
5b510 74 20 72 65 63 65 6e 74 20 69 6e 73 65 72 74 20 t recent insert
5b520 28 73 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 (see above) */.
5b530 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20 20 u32 magic;
5b540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5b550 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65 72 20 66 * Magic number f
5b560 6f 72 20 64 65 74 65 63 74 20 6c 69 62 72 61 72 or detect librar
5b570 79 20 6d 69 73 75 73 65 20 2a 2f 0a 20 20 69 6e y misuse */. in
5b580 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 t nChange;
5b590 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 /* V
5b5a0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
5b5b0 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 sqlite3_changes
5b5c0 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 74 () */. int nTot
5b5d0 61 6c 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 alChange;
5b5e0 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 /* Value r
5b5f0 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 eturned by sqlit
5b600 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 e3_total_changes
5b610 28 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f () */. sqlite3_
5b620 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 mutex *mutex;
5b630 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 /* Connect
5b640 69 6f 6e 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69 ion mutex */. i
5b650 6e 74 20 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 nt aLimit[SQLITE
5b660 5f 4e 5f 4c 49 4d 49 54 5d 3b 20 20 20 2f 2a 20 _N_LIMIT]; /*
5b670 4c 69 6d 69 74 73 20 2a 2f 0a 20 20 73 74 72 75 Limits */. stru
5b680 63 74 20 73 71 6c 69 74 65 33 49 6e 69 74 49 6e ct sqlite3InitIn
5b690 66 6f 20 7b 20 20 20 20 20 20 2f 2a 20 49 6e 66 fo { /* Inf
5b6a0 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 64 75 ormation used du
5b6b0 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 ring initializat
5b6c0 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 ion */. int i
5b6d0 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Db;
5b6e0 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 62 /* When b
5b6f0 61 63 6b 20 69 73 20 62 65 69 6e 67 20 69 6e 69 ack is being ini
5b700 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 20 tialized */.
5b710 69 6e 74 20 6e 65 77 54 6e 75 6d 3b 20 20 20 20 int newTnum;
5b720 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
5b730 6f 6f 74 70 61 67 65 20 6f 66 20 74 61 62 6c 65 ootpage of table
5b740 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a being initializ
5b750 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 62 75 73 ed */. u8 bus
5b760 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y;
5b770 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 69 66 /* TRUE if
5b780 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 69 74 69 currently initi
5b790 61 6c 69 7a 69 6e 67 20 2a 2f 0a 20 20 20 20 75 alizing */. u
5b7a0 38 20 6f 72 70 68 61 6e 54 72 69 67 67 65 72 3b 8 orphanTrigger;
5b7b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 /* La
5b7c0 73 74 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 st statement is
5b7d0 6f 72 70 68 61 6e 65 64 20 54 45 4d 50 20 74 72 orphaned TEMP tr
5b7e0 69 67 67 65 72 20 2a 2f 0a 20 20 7d 20 69 6e 69 igger */. } ini
5b7f0 74 3b 0a 20 20 69 6e 74 20 6e 45 78 74 65 6e 73 t;. int nExtens
5b800 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 ion;
5b810 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
5b820 6c 6f 61 64 65 64 20 65 78 74 65 6e 73 69 6f 6e loaded extension
5b830 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 45 s */. void **aE
5b840 78 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 xtension;
5b850 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 /* Array of
5b860 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 shared library
5b870 68 61 6e 64 6c 65 73 20 2a 2f 0a 20 20 73 74 72 handles */. str
5b880 75 63 74 20 56 64 62 65 20 2a 70 56 64 62 65 3b uct Vdbe *pVdbe;
5b890 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 /* Li
5b8a0 73 74 20 6f 66 20 61 63 74 69 76 65 20 76 69 72 st of active vir
5b8b0 74 75 61 6c 20 6d 61 63 68 69 6e 65 73 20 2a 2f tual machines */
5b8c0 0a 20 20 69 6e 74 20 61 63 74 69 76 65 56 64 62 . int activeVdb
5b8d0 65 43 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 eCnt;
5b8e0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 56 44 /* Number of VD
5b8f0 42 45 73 20 63 75 72 72 65 6e 74 6c 79 20 65 78 BEs currently ex
5b900 65 63 75 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 ecuting */. int
5b910 20 77 72 69 74 65 56 64 62 65 43 6e 74 3b 20 20 writeVdbeCnt;
5b920 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
5b930 6d 62 65 72 20 6f 66 20 61 63 74 69 76 65 20 56 mber of active V
5b940 44 42 45 73 20 74 68 61 74 20 61 72 65 20 77 72 DBEs that are wr
5b950 69 74 69 6e 67 20 2a 2f 0a 20 20 76 6f 69 64 20 iting */. void
5b960 28 2a 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c (*xTrace)(void*,
5b970 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 20 20 20 const char*);
5b980 20 20 20 20 20 2f 2a 20 54 72 61 63 65 20 66 75 /* Trace fu
5b990 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 nction */. void
5b9a0 20 2a 70 54 72 61 63 65 41 72 67 3b 20 20 20 20 *pTraceArg;
5b9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b9c0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e /* Argumen
5b9d0 74 20 74 6f 20 74 68 65 20 74 72 61 63 65 20 66 t to the trace f
5b9e0 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 unction */. voi
5b9f0 64 20 28 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f d (*xProfile)(vo
5ba00 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c id*,const char*,
5ba10 75 36 34 29 3b 20 20 2f 2a 20 50 72 6f 66 69 6c u64); /* Profil
5ba20 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a ing function */.
5ba30 20 20 76 6f 69 64 20 2a 70 50 72 6f 66 69 6c 65 void *pProfile
5ba40 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 Arg;
5ba50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
5ba60 72 67 75 6d 65 6e 74 20 74 6f 20 70 72 6f 66 69 rgument to profi
5ba70 6c 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 le function */.
5ba80 20 76 6f 69 64 20 2a 70 43 6f 6d 6d 69 74 41 72 void *pCommitAr
5ba90 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
5baa0 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 /* Argument t
5bab0 6f 20 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 o xCommitCallbac
5bac0 6b 28 29 20 2a 2f 20 20 20 0a 20 20 69 6e 74 20 k() */ . int
5bad0 28 2a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 (*xCommitCallbac
5bae0 6b 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 2f 2a k)(void*); /*
5baf0 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72 Invoked at ever
5bb00 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 y commit. */. v
5bb10 6f 69 64 20 2a 70 52 6f 6c 6c 62 61 63 6b 41 72 oid *pRollbackAr
5bb20 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
5bb30 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 /* Argument to
5bb40 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 xRollbackCallbac
5bb50 6b 28 29 20 2a 2f 20 20 20 0a 20 20 76 6f 69 64 k() */ . void
5bb60 20 28 2a 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c (*xRollbackCall
5bb70 62 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 2f 2a back)(void*); /*
5bb80 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72 Invoked at ever
5bb90 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 y commit. */. v
5bba0 6f 69 64 20 2a 70 55 70 64 61 74 65 41 72 67 3b oid *pUpdateArg;
5bbb0 0a 20 20 76 6f 69 64 20 28 2a 78 55 70 64 61 74 . void (*xUpdat
5bbc0 65 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a eCallback)(void*
5bbd0 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 ,int, const char
5bbe0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 *,const char*,sq
5bbf0 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20 20 76 lite_int64);. v
5bc00 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 oid(*xCollNeeded
5bc10 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a )(void*,sqlite3*
5bc20 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f ,int eTextRep,co
5bc30 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 76 6f nst char*);. vo
5bc40 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 31 id(*xCollNeeded1
5bc50 36 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 6)(void*,sqlite3
5bc60 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 *,int eTextRep,c
5bc70 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76 onst void*);. v
5bc80 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 oid *pCollNeeded
5bc90 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 Arg;. sqlite3_v
5bca0 61 6c 75 65 20 2a 70 45 72 72 3b 20 20 20 20 20 alue *pErr;
5bcb0 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 /* Most rec
5bcc0 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 ent error messag
5bcd0 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 e */. char *zEr
5bce0 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 20 rMsg;
5bcf0 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 /* Most rec
5bd00 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 ent error messag
5bd10 65 20 28 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 e (UTF-8 encoded
5bd20 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 ) */. char *zEr
5bd30 72 4d 73 67 31 36 3b 20 20 20 20 20 20 20 20 20 rMsg16;
5bd40 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 /* Most rec
5bd50 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 ent error messag
5bd60 65 20 28 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 e (UTF-16 encode
5bd70 64 29 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a d) */. union {.
5bd80 20 20 20 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 volatile int
5bd90 20 69 73 49 6e 74 65 72 72 75 70 74 65 64 3b 20 isInterrupted;
5bda0 2f 2a 20 54 72 75 65 20 69 66 20 73 71 6c 69 74 /* True if sqlit
5bdb0 65 33 5f 69 6e 74 65 72 72 75 70 74 20 68 61 73 e3_interrupt has
5bdc0 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a been called */.
5bdd0 20 20 20 20 64 6f 75 62 6c 65 20 6e 6f 74 55 73 double notUs
5bde0 65 64 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 ed1;
5bdf0 2f 2a 20 53 70 61 63 65 72 20 2a 2f 0a 20 20 7d /* Spacer */. }
5be00 20 75 31 3b 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 u1;. Lookaside
5be10 20 6c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 lookaside;
5be20 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b 61 73 69 64 /* Lookasid
5be30 65 20 6d 61 6c 6c 6f 63 20 63 6f 6e 66 69 67 75 e malloc configu
5be40 72 61 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 ration */.#ifnde
5be50 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
5be60 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 6e THORIZATION. in
5be70 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69 64 2a t (*xAuth)(void*
5be80 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a ,int,const char*
5be90 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e ,const char*,con
5bea0 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 st char*,const c
5beb0 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 20 har*);.
5bec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bed0 20 20 20 20 20 20 20 2f 2a 20 41 63 63 65 73 73 /* Access
5bee0 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 authorization f
5bef0 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 unction */. voi
5bf00 64 20 2a 70 41 75 74 68 41 72 67 3b 20 20 20 20 d *pAuthArg;
5bf10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73 /* 1s
5bf20 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 t argument to th
5bf30 65 20 61 63 63 65 73 73 20 61 75 74 68 20 66 75 e access auth fu
5bf40 6e 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 nction */.#endif
5bf50 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
5bf60 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 OMIT_PROGRESS_CA
5bf70 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20 28 2a 78 LLBACK. int (*x
5bf80 50 72 6f 67 72 65 73 73 29 28 76 6f 69 64 20 2a Progress)(void *
5bf90 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 ); /* The pr
5bfa0 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 ogress callback
5bfb0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72 6f 67 */. void *pProg
5bfc0 72 65 73 73 41 72 67 3b 20 20 20 20 20 20 20 20 ressArg;
5bfd0 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 /* Argument t
5bfe0 6f 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 o the progress c
5bff0 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 allback */. int
5c000 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 3b 20 20 nProgressOps;
5c010 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
5c020 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73 20 mber of opcodes
5c030 66 6f 72 20 70 72 6f 67 72 65 73 73 20 63 61 6c for progress cal
5c040 6c 62 61 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a lback */.#endif.
5c050 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
5c060 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
5c070 0a 20 20 48 61 73 68 20 61 4d 6f 64 75 6c 65 3b . Hash aModule;
5c080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c090 20 2f 2a 20 70 6f 70 75 6c 61 74 65 64 20 62 79 /* populated by
5c0a0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
5c0b0 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 54 61 module() */. Ta
5c0c0 62 6c 65 20 2a 70 56 54 61 62 3b 20 20 20 20 20 ble *pVTab;
5c0d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 76 /* v
5c0e0 74 61 62 20 77 69 74 68 20 61 63 74 69 76 65 20 tab with active
5c0f0 43 6f 6e 6e 65 63 74 2f 43 72 65 61 74 65 20 6d Connect/Create m
5c100 65 74 68 6f 64 20 2a 2f 0a 20 20 56 54 61 62 6c ethod */. VTabl
5c110 65 20 2a 2a 61 56 54 72 61 6e 73 3b 20 20 20 20 e **aVTrans;
5c120 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 /* Virt
5c130 75 61 6c 20 74 61 62 6c 65 73 20 77 69 74 68 20 ual tables with
5c140 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e open transaction
5c150 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 54 72 61 s */. int nVTra
5c160 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ns;
5c170 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 /* Allocate
5c180 64 20 73 69 7a 65 20 6f 66 20 61 56 54 72 61 6e d size of aVTran
5c190 73 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20 2a 70 s */. VTable *p
5c1a0 44 69 73 63 6f 6e 6e 65 63 74 3b 20 20 20 20 2f Disconnect; /
5c1b0 2a 20 44 69 73 63 6f 6e 6e 65 63 74 20 74 68 65 * Disconnect the
5c1c0 73 65 20 69 6e 20 6e 65 78 74 20 73 71 6c 69 74 se in next sqlit
5c1d0 65 33 5f 70 72 65 70 61 72 65 28 29 20 2a 2f 0a e3_prepare() */.
5c1e0 23 65 6e 64 69 66 0a 20 20 46 75 6e 63 44 65 66 #endif. FuncDef
5c1f0 48 61 73 68 20 61 46 75 6e 63 3b 20 20 20 20 20 Hash aFunc;
5c200 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 /* Hash t
5c210 61 62 6c 65 20 6f 66 20 63 6f 6e 6e 65 63 74 69 able of connecti
5c220 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a on functions */.
5c230 20 20 48 61 73 68 20 61 43 6f 6c 6c 53 65 71 3b Hash aCollSeq;
5c240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c250 2f 2a 20 41 6c 6c 20 63 6f 6c 6c 61 74 69 6e 67 /* All collating
5c260 20 73 65 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20 sequences */.
5c270 42 75 73 79 48 61 6e 64 6c 65 72 20 62 75 73 79 BusyHandler busy
5c280 48 61 6e 64 6c 65 72 3b 20 20 20 20 20 20 2f 2a Handler; /*
5c290 20 42 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a Busy callback *
5c2a0 2f 0a 20 20 69 6e 74 20 62 75 73 79 54 69 6d 65 /. int busyTime
5c2b0 6f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 out;
5c2c0 20 20 2f 2a 20 42 75 73 79 20 68 61 6e 64 6c 65 /* Busy handle
5c2d0 72 20 74 69 6d 65 6f 75 74 2c 20 69 6e 20 6d 73 r timeout, in ms
5c2e0 65 63 20 2a 2f 0a 20 20 44 62 20 61 44 62 53 74 ec */. Db aDbSt
5c2f0 61 74 69 63 5b 32 5d 3b 20 20 20 20 20 20 20 20 atic[2];
5c300 20 20 20 20 20 20 2f 2a 20 53 74 61 74 69 63 20 /* Static
5c310 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 32 20 space for the 2
5c320 64 65 66 61 75 6c 74 20 62 61 63 6b 65 6e 64 73 default backends
5c330 20 2a 2f 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 */. Savepoint
5c340 2a 70 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 *pSavepoint;
5c350 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 /* List of a
5c360 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 ctive savepoints
5c370 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 */. int nSavep
5c380 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 oint;
5c390 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
5c3a0 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e non-transaction
5c3b0 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 savepoints */.
5c3c0 20 69 6e 74 20 6e 53 74 61 74 65 6d 65 6e 74 3b int nStatement;
5c3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5c3e0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 * Number of nest
5c3f0 65 64 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 ed statement-tra
5c400 6e 73 61 63 74 69 6f 6e 73 20 20 2a 2f 0a 20 20 nsactions */.
5c410 75 38 20 69 73 54 72 61 6e 73 61 63 74 69 6f 6e u8 isTransaction
5c420 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 2f 2a Savepoint; /*
5c430 20 54 72 75 65 20 69 66 20 74 68 65 20 6f 75 74 True if the out
5c440 65 72 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 ermost savepoint
5c450 20 69 73 20 61 20 54 53 20 2a 2f 0a 20 20 69 36 is a TS */. i6
5c460 34 20 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 4 nDeferredCons;
5c470 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
5c480 65 74 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 et deferred cons
5c490 74 72 61 69 6e 74 73 20 74 68 69 73 20 74 72 61 traints this tra
5c4a0 6e 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 0a 23 69 nsaction. */..#i
5c4b0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
5c4c0 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 LE_UNLOCK_NOTIFY
5c4d0 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 . /* The follow
5c4e0 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20 61 72 ing variables ar
5c4f0 65 20 61 6c 6c 20 70 72 6f 74 65 63 74 65 64 20 e all protected
5c500 62 79 20 74 68 65 20 53 54 41 54 49 43 5f 4d 41 by the STATIC_MA
5c510 53 54 45 52 20 0a 20 20 2a 2a 20 6d 75 74 65 78 STER . ** mutex
5c520 2c 20 6e 6f 74 20 62 79 20 73 71 6c 69 74 65 33 , not by sqlite3
5c530 2e 6d 75 74 65 78 2e 20 54 68 65 79 20 61 72 65 .mutex. They are
5c540 20 75 73 65 64 20 62 79 20 63 6f 64 65 20 69 6e used by code in
5c550 20 6e 6f 74 69 66 79 2e 63 2e 20 0a 20 20 2a 2a notify.c. . **
5c560 0a 20 20 2a 2a 20 57 68 65 6e 20 58 2e 70 55 6e . ** When X.pUn
5c570 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d lockConnection==
5c580 59 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 Y, that means th
5c590 61 74 20 58 20 69 73 20 77 61 69 74 69 6e 67 20 at X is waiting
5c5a0 66 6f 72 20 59 20 74 6f 0a 20 20 2a 2a 20 75 6e for Y to. ** un
5c5b0 6c 6f 63 6b 20 73 6f 20 74 68 61 74 20 69 74 20 lock so that it
5c5c0 63 61 6e 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a can proceed.. *
5c5d0 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 58 2e 70 42 *. ** When X.pB
5c5e0 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f lockingConnectio
5c5f0 6e 3d 3d 59 2c 20 74 68 61 74 20 6d 65 61 6e 73 n==Y, that means
5c600 20 74 68 61 74 20 73 6f 6d 65 74 68 69 6e 67 20 that something
5c610 74 68 61 74 20 58 20 74 72 69 65 64 0a 20 20 2a that X tried. *
5c620 2a 20 74 72 69 65 64 20 74 6f 20 64 6f 20 72 65 * tried to do re
5c630 63 65 6e 74 6c 79 20 66 61 69 6c 65 64 20 77 69 cently failed wi
5c640 74 68 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 th an SQLITE_LOC
5c650 4b 45 44 20 65 72 72 6f 72 20 64 75 65 20 74 6f KED error due to
5c660 20 6c 6f 63 6b 73 0a 20 20 2a 2a 20 68 65 6c 64 locks. ** held
5c670 20 62 79 20 59 2e 0a 20 20 2a 2f 0a 20 20 73 71 by Y.. */. sq
5c680 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b 69 6e 67 lite3 *pBlocking
5c690 43 6f 6e 6e 65 63 74 69 6f 6e 3b 20 2f 2a 20 43 Connection; /* C
5c6a0 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 63 onnection that c
5c6b0 61 75 73 65 64 20 53 51 4c 49 54 45 5f 4c 4f 43 aused SQLITE_LOC
5c6c0 4b 45 44 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 KED */. sqlite3
5c6d0 20 2a 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 *pUnlockConnect
5c6e0 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f ion; /
5c6f0 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 * Connection to
5c700 77 61 74 63 68 20 66 6f 72 20 75 6e 6c 6f 63 6b watch for unlock
5c710 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 6e 6c */. void *pUnl
5c720 6f 63 6b 41 72 67 3b 20 20 20 20 20 20 20 20 20 ockArg;
5c730 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
5c740 72 67 75 6d 65 6e 74 20 74 6f 20 78 55 6e 6c 6f rgument to xUnlo
5c750 63 6b 4e 6f 74 69 66 79 20 2a 2f 0a 20 20 76 6f ckNotify */. vo
5c760 69 64 20 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69 id (*xUnlockNoti
5c770 66 79 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 fy)(void **, int
5c780 29 3b 20 20 2f 2a 20 55 6e 6c 6f 63 6b 20 6e 6f ); /* Unlock no
5c790 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f tify callback */
5c7a0 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 4e 65 78 . sqlite3 *pNex
5c7b0 74 42 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 tBlocked;
5c7c0 20 2f 2a 20 4e 65 78 74 20 69 6e 20 6c 69 73 74 /* Next in list
5c7d0 20 6f 66 20 61 6c 6c 20 62 6c 6f 63 6b 65 64 20 of all blocked
5c7e0 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 23 connections */.#
5c7f0 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 endif.};../*.**
5c800 41 20 6d 61 63 72 6f 20 74 6f 20 64 69 73 63 6f A macro to disco
5c810 76 65 72 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 ver the encoding
5c820 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e 0a of a database..
5c830 2a 2f 0a 23 64 65 66 69 6e 65 20 45 4e 43 28 64 */.#define ENC(d
5c840 62 29 20 28 28 64 62 29 2d 3e 61 44 62 5b 30 5d b) ((db)->aDb[0]
5c850 2e 70 53 63 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a .pSchema->enc)..
5c860 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 /*.** Possible v
5c870 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 71 alues for the sq
5c880 6c 69 74 65 2e 66 6c 61 67 73 20 61 6e 64 20 6f lite.flags and o
5c890 72 20 44 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 r Db.flags field
5c8a0 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69 s..**.** On sqli
5c8b0 74 65 2e 66 6c 61 67 73 2c 20 74 68 65 20 53 51 te.flags, the SQ
5c8c0 4c 49 54 45 5f 49 6e 54 72 61 6e 73 20 76 61 6c LITE_InTrans val
5c8d0 75 65 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 ue means that we
5c8e0 20 68 61 76 65 0a 2a 2a 20 65 78 65 63 75 74 65 have.** execute
5c8f0 64 20 61 20 42 45 47 49 4e 2e 20 20 4f 6e 20 44 d a BEGIN. On D
5c900 62 2e 66 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f b.flags, SQLITE_
5c910 49 6e 54 72 61 6e 73 20 6d 65 61 6e 73 20 61 20 InTrans means a
5c920 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 statement.** tra
5c930 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 nsaction is acti
5c940 76 65 20 6f 6e 20 74 68 61 74 20 70 61 72 74 69 ve on that parti
5c950 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 66 cular database f
5c960 69 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ile..*/.#define
5c970 53 51 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65 SQLITE_VdbeTrace
5c980 20 20 20 20 20 20 30 78 30 30 30 30 30 30 30 31 0x00000001
5c990 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61 /* True to tra
5c9a0 63 65 20 56 44 42 45 20 65 78 65 63 75 74 69 6f ce VDBE executio
5c9b0 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c n */.#define SQL
5c9c0 49 54 45 5f 49 6e 54 72 61 6e 73 20 20 20 20 20 ITE_InTrans
5c9d0 20 20 20 30 78 30 30 30 30 30 30 30 38 20 20 2f 0x00000008 /
5c9e0 2a 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 74 * True if in a t
5c9f0 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 ransaction */.#d
5ca00 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 6e 74 efine SQLITE_Int
5ca10 65 72 6e 43 68 61 6e 67 65 73 20 20 30 78 30 30 ernChanges 0x00
5ca20 30 30 30 30 31 30 20 20 2f 2a 20 55 6e 63 6f 6d 000010 /* Uncom
5ca30 6d 69 74 74 65 64 20 48 61 73 68 20 74 61 62 6c mitted Hash tabl
5ca40 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 e changes */.#de
5ca50 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c fine SQLITE_Full
5ca60 43 6f 6c 4e 61 6d 65 73 20 20 20 30 78 30 30 30 ColNames 0x000
5ca70 30 30 30 32 30 20 20 2f 2a 20 53 68 6f 77 20 66 00020 /* Show f
5ca80 75 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 ull column names
5ca90 20 6f 6e 20 53 45 4c 45 43 54 20 2a 2f 0a 23 64 on SELECT */.#d
5caa0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 68 6f efine SQLITE_Sho
5cab0 72 74 43 6f 6c 4e 61 6d 65 73 20 20 30 78 30 30 rtColNames 0x00
5cac0 30 30 30 30 34 30 20 20 2f 2a 20 53 68 6f 77 20 000040 /* Show
5cad0 73 68 6f 72 74 20 63 6f 6c 75 6d 6e 73 20 6e 61 short columns na
5cae0 6d 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 mes */.#define S
5caf0 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 QLITE_CountRows
5cb00 20 20 20 20 20 30 78 30 30 30 30 30 30 38 30 20 0x00000080
5cb10 20 2f 2a 20 43 6f 75 6e 74 20 72 6f 77 73 20 63 /* Count rows c
5cb20 68 61 6e 67 65 64 20 62 79 20 49 4e 53 45 52 54 hanged by INSERT
5cb30 2c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 , */.
5cb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5cb60 2a 20 20 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 * DELETE, or U
5cb70 50 44 41 54 45 20 61 6e 64 20 72 65 74 75 72 6e PDATE and return
5cb80 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
5cb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5cbb0 20 20 20 74 68 65 20 63 6f 75 6e 74 20 75 73 69 the count usi
5cbc0 6e 67 20 61 20 63 61 6c 6c 62 61 63 6b 2e 20 2a ng a callback. *
5cbd0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5cbe0 5f 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 20 _NullCallback
5cbf0 30 78 30 30 30 30 30 31 30 30 20 20 2f 2a 20 49 0x00000100 /* I
5cc00 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 nvoke the callba
5cc10 63 6b 20 6f 6e 63 65 20 69 66 20 74 68 65 20 2a ck once if the *
5cc20 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /.
5cc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cc40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 /*
5cc50 20 72 65 73 75 6c 74 20 73 65 74 20 69 73 20 65 result set is e
5cc60 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 mpty */.#define
5cc70 53 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 20 SQLITE_SqlTrace
5cc80 20 20 20 20 20 20 30 78 30 30 30 30 30 32 30 30 0x00000200
5cc90 20 20 2f 2a 20 44 65 62 75 67 20 70 72 69 6e 74 /* Debug print
5cca0 20 53 51 4c 20 61 73 20 69 74 20 65 78 65 63 75 SQL as it execu
5ccb0 74 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 tes */.#define S
5ccc0 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e QLITE_VdbeListin
5ccd0 67 20 20 20 20 30 78 30 30 30 30 30 34 30 30 20 g 0x00000400
5cce0 20 2f 2a 20 44 65 62 75 67 20 6c 69 73 74 69 6e /* Debug listin
5ccf0 67 73 20 6f 66 20 56 44 42 45 20 70 72 6f 67 72 gs of VDBE progr
5cd00 61 6d 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ams */.#define S
5cd10 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d QLITE_WriteSchem
5cd20 61 20 20 20 20 30 78 30 30 30 30 30 38 30 30 20 a 0x00000800
5cd30 20 2f 2a 20 4f 4b 20 74 6f 20 75 70 64 61 74 65 /* OK to update
5cd40 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 2a SQLITE_MASTER *
5cd50 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5cd60 5f 4e 6f 52 65 61 64 6c 6f 63 6b 20 20 20 20 20 _NoReadlock
5cd70 30 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20 52 0x00001000 /* R
5cd80 65 61 64 6c 6f 63 6b 73 20 61 72 65 20 6f 6d 69 eadlocks are omi
5cd90 74 74 65 64 20 77 68 65 6e 20 0a 20 20 20 20 20 tted when .
5cda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cdc0 20 20 20 20 20 2a 2a 20 61 63 63 65 73 73 69 6e ** accessin
5cdd0 67 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 g read-only data
5cde0 62 61 73 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 bases */.#define
5cdf0 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 SQLITE_IgnoreCh
5ce00 65 63 6b 73 20 20 20 30 78 30 30 30 30 32 30 30 ecks 0x0000200
5ce10 30 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 65 6e 66 0 /* Do not enf
5ce20 6f 72 63 65 20 63 68 65 63 6b 20 63 6f 6e 73 74 orce check const
5ce30 72 61 69 6e 74 73 20 2a 2f 0a 23 64 65 66 69 6e raints */.#defin
5ce40 65 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 e SQLITE_ReadUnc
5ce50 6f 6d 6d 69 74 74 65 64 20 30 78 30 30 30 30 34 ommitted 0x00004
5ce60 30 30 30 20 2f 2a 20 46 6f 72 20 73 68 61 72 65 000 /* For share
5ce70 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a d-cache mode */.
5ce80 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
5ce90 65 67 61 63 79 46 69 6c 65 46 6d 74 20 20 30 78 egacyFileFmt 0x
5cea0 30 30 30 30 38 30 30 30 20 20 2f 2a 20 43 72 65 00008000 /* Cre
5ceb0 61 74 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 ate new database
5cec0 73 20 69 6e 20 66 6f 72 6d 61 74 20 31 20 2a 2f s in format 1 */
5ced0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5cee0 46 75 6c 6c 46 53 79 6e 63 20 20 20 20 20 20 30 FullFSync 0
5cef0 78 30 30 30 31 30 30 30 30 20 20 2f 2a 20 55 73 x00010000 /* Us
5cf00 65 20 66 75 6c 6c 20 66 73 79 6e 63 20 6f 6e 20 e full fsync on
5cf10 74 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 23 the backend */.#
5cf20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 6f define SQLITE_Lo
5cf30 61 64 45 78 74 65 6e 73 69 6f 6e 20 20 30 78 30 adExtension 0x0
5cf40 30 30 32 30 30 30 30 20 20 2f 2a 20 45 6e 61 62 0020000 /* Enab
5cf50 6c 65 20 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f le load_extensio
5cf60 6e 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51 n */..#define SQ
5cf70 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 LITE_RecoveryMod
5cf80 65 20 20 20 30 78 30 30 30 34 30 30 30 30 20 20 e 0x00040000
5cf90 2f 2a 20 49 67 6e 6f 72 65 20 73 63 68 65 6d 61 /* Ignore schema
5cfa0 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66 69 errors */.#defi
5cfb0 6e 65 20 53 51 4c 49 54 45 5f 52 65 76 65 72 73 ne SQLITE_Revers
5cfc0 65 4f 72 64 65 72 20 20 20 30 78 30 30 31 30 30 eOrder 0x00100
5cfd0 30 30 30 20 20 2f 2a 20 52 65 76 65 72 73 65 20 000 /* Reverse
5cfe0 75 6e 6f 72 64 65 72 65 64 20 53 45 4c 45 43 54 unordered SELECT
5cff0 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c s */.#define SQL
5d000 49 54 45 5f 52 65 63 54 72 69 67 67 65 72 73 20 ITE_RecTriggers
5d010 20 20 20 30 78 30 30 32 30 30 30 30 30 20 20 2f 0x00200000 /
5d020 2a 20 45 6e 61 62 6c 65 20 72 65 63 75 72 73 69 * Enable recursi
5d030 76 65 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 23 ve triggers */.#
5d040 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 6f define SQLITE_Fo
5d050 72 65 69 67 6e 4b 65 79 73 20 20 20 20 30 78 30 reignKeys 0x0
5d060 30 34 30 30 30 30 30 20 20 2f 2a 20 45 6e 66 6f 0400000 /* Enfo
5d070 72 63 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 rce foreign key
5d080 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 2a 2f 0a constraints */.
5d090 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 ./*.** Possible
5d0a0 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 values for the s
5d0b0 71 6c 69 74 65 2e 6d 61 67 69 63 20 66 69 65 6c qlite.magic fiel
5d0c0 64 2e 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 d..** The number
5d0d0 73 20 61 72 65 20 6f 62 74 61 69 6e 65 64 20 61 s are obtained a
5d0e0 74 20 72 61 6e 64 6f 6d 20 61 6e 64 20 68 61 76 t random and hav
5d0f0 65 20 6e 6f 20 73 70 65 63 69 61 6c 20 6d 65 61 e no special mea
5d100 6e 69 6e 67 2c 20 6f 74 68 65 72 0a 2a 2a 20 74 ning, other.** t
5d110 68 61 6e 20 62 65 69 6e 67 20 64 69 73 74 69 6e han being distin
5d120 63 74 20 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 ct from one anot
5d130 68 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 her..*/.#define
5d140 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 SQLITE_MAGIC_OPE
5d150 4e 20 20 20 20 20 30 78 61 30 32 39 61 36 39 37 N 0xa029a697
5d160 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73 /* Database is
5d170 20 6f 70 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 open */.#define
5d180 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c SQLITE_MAGIC_CL
5d190 4f 53 45 44 20 20 20 30 78 39 66 33 63 32 64 33 OSED 0x9f3c2d3
5d1a0 33 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 3 /* Database i
5d1b0 73 20 63 6c 6f 73 65 64 20 2a 2f 0a 23 64 65 66 s closed */.#def
5d1c0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 ine SQLITE_MAGIC
5d1d0 5f 53 49 43 4b 20 20 20 20 20 30 78 34 62 37 37 _SICK 0x4b77
5d1e0 31 32 39 30 20 20 2f 2a 20 45 72 72 6f 72 20 61 1290 /* Error a
5d1f0 6e 64 20 61 77 61 69 74 69 6e 67 20 63 6c 6f 73 nd awaiting clos
5d200 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c e */.#define SQL
5d210 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 20 ITE_MAGIC_BUSY
5d220 20 20 20 30 78 66 30 33 62 37 39 30 36 20 20 2f 0xf03b7906 /
5d230 2a 20 44 61 74 61 62 61 73 65 20 63 75 72 72 65 * Database curre
5d240 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 23 ntly in use */.#
5d250 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
5d260 47 49 43 5f 45 52 52 4f 52 20 20 20 20 30 78 62 GIC_ERROR 0xb
5d270 35 33 35 37 39 33 30 20 20 2f 2a 20 41 6e 20 53 5357930 /* An S
5d280 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 65 72 72 QLITE_MISUSE err
5d290 6f 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 0a or occurred */..
5d2a0 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 66 /*.** Each SQL f
5d2b0 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 66 69 6e unction is defin
5d2c0 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 ed by an instanc
5d2d0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
5d2e0 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e ng.** structure.
5d2f0 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 A pointer to t
5d300 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 his structure is
5d310 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73 stored in the s
5d320 71 6c 69 74 65 2e 61 46 75 6e 63 0a 2a 2a 20 68 qlite.aFunc.** h
5d330 61 73 68 20 74 61 62 6c 65 2e 20 20 57 68 65 6e ash table. When
5d340 20 6d 75 6c 74 69 70 6c 65 20 66 75 6e 63 74 69 multiple functi
5d350 6f 6e 73 20 68 61 76 65 20 74 68 65 20 73 61 6d ons have the sam
5d360 65 20 6e 61 6d 65 2c 20 74 68 65 20 68 61 73 68 e name, the hash
5d370 20 74 61 62 6c 65 0a 2a 2a 20 70 6f 69 6e 74 73 table.** points
5d380 20 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 to a linked lis
5d390 74 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 t of these struc
5d3a0 74 75 72 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 tures..*/.struct
5d3b0 20 46 75 6e 63 44 65 66 20 7b 0a 20 20 69 31 36 FuncDef {. i16
5d3c0 20 6e 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 nArg;
5d3d0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 /* Number of a
5d3e0 72 67 75 6d 65 6e 74 73 2e 20 20 2d 31 20 6d 65 rguments. -1 me
5d3f0 61 6e 73 20 75 6e 6c 69 6d 69 74 65 64 20 2a 2f ans unlimited */
5d400 0a 20 20 75 38 20 69 50 72 65 66 45 6e 63 3b 20 . u8 iPrefEnc;
5d410 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 65 /* Prefe
5d420 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 rred text encodi
5d430 6e 67 20 28 53 51 4c 49 54 45 5f 55 54 46 38 2c ng (SQLITE_UTF8,
5d440 20 31 36 4c 45 2c 20 31 36 42 45 29 20 2a 2f 0a 16LE, 16BE) */.
5d450 20 20 75 38 20 66 6c 61 67 73 3b 20 20 20 20 20 u8 flags;
5d460 20 20 20 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 /* Some c
5d470 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 53 51 ombination of SQ
5d480 4c 49 54 45 5f 46 55 4e 43 5f 2a 20 2a 2f 0a 20 LITE_FUNC_* */.
5d490 20 76 6f 69 64 20 2a 70 55 73 65 72 44 61 74 61 void *pUserData
5d4a0 3b 20 20 20 20 20 2f 2a 20 55 73 65 72 20 64 61 ; /* User da
5d4b0 74 61 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a ta parameter */.
5d4c0 20 20 46 75 6e 63 44 65 66 20 2a 70 4e 65 78 74 FuncDef *pNext
5d4d0 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 66 ; /* Next f
5d4e0 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 73 61 6d unction with sam
5d4f0 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 e name */. void
5d500 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 (*xFunc)(sqlite
5d510 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 3_context*,int,s
5d520 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b qlite3_value**);
5d530 20 2f 2a 20 52 65 67 75 6c 61 72 20 66 75 6e 63 /* Regular func
5d540 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 tion */. void (
5d550 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f *xStep)(sqlite3_
5d560 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c context*,int,sql
5d570 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f ite3_value**); /
5d580 2a 20 41 67 67 72 65 67 61 74 65 20 73 74 65 70 * Aggregate step
5d590 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 */. void (*xFi
5d5a0 6e 61 6c 69 7a 65 29 28 73 71 6c 69 74 65 33 5f nalize)(sqlite3_
5d5b0 63 6f 6e 74 65 78 74 2a 29 3b 20 20 20 20 20 20 context*);
5d5c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67 67 /* Agg
5d5d0 72 65 67 61 74 65 20 66 69 6e 61 6c 69 7a 65 72 regate finalizer
5d5e0 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d */. char *zNam
5d5f0 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 e; /* SQ
5d600 4c 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75 L name of the fu
5d610 6e 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 46 75 6e nction. */. Fun
5d620 63 44 65 66 20 2a 70 48 61 73 68 3b 20 20 20 20 cDef *pHash;
5d630 20 20 2f 2a 20 4e 65 78 74 20 77 69 74 68 20 61 /* Next with a
5d640 20 64 69 66 66 65 72 65 6e 74 20 6e 61 6d 65 20 different name
5d650 62 75 74 20 74 68 65 20 73 61 6d 65 20 68 61 73 but the same has
5d660 68 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 h */.};../*.** P
5d670 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 ossible values f
5d680 6f 72 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 or FuncDef.flags
5d690 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
5d6a0 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 20 20 20 TE_FUNC_LIKE
5d6b0 20 30 78 30 31 20 2f 2a 20 43 61 6e 64 69 64 61 0x01 /* Candida
5d6c0 74 65 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20 te for the LIKE
5d6d0 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0a optimization */.
5d6e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 #define SQLITE_F
5d6f0 55 4e 43 5f 43 41 53 45 20 20 20 20 20 30 78 30 UNC_CASE 0x0
5d700 32 20 2f 2a 20 43 61 73 65 2d 73 65 6e 73 69 74 2 /* Case-sensit
5d710 69 76 65 20 4c 49 4b 45 2d 74 79 70 65 20 66 75 ive LIKE-type fu
5d720 6e 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e nction */.#defin
5d730 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 e SQLITE_FUNC_EP
5d740 48 45 4d 20 20 20 20 30 78 30 34 20 2f 2a 20 45 HEM 0x04 /* E
5d750 70 68 65 6d 65 72 61 6c 2e 20 20 44 65 6c 65 74 phemeral. Delet
5d760 65 20 77 69 74 68 20 56 44 42 45 20 2a 2f 0a 23 e with VDBE */.#
5d770 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 define SQLITE_FU
5d780 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 30 78 30 38 NC_NEEDCOLL 0x08
5d790 20 2f 2a 20 73 71 6c 69 74 65 33 47 65 74 46 75 /* sqlite3GetFu
5d7a0 6e 63 43 6f 6c 6c 53 65 71 28 29 20 6d 69 67 68 ncCollSeq() migh
5d7b0 74 20 62 65 20 63 61 6c 6c 65 64 20 2a 2f 0a 23 t be called */.#
5d7c0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 define SQLITE_FU
5d7d0 4e 43 5f 50 52 49 56 41 54 45 20 20 30 78 31 30 NC_PRIVATE 0x10
5d7e0 20 2f 2a 20 41 6c 6c 6f 77 65 64 20 66 6f 72 20 /* Allowed for
5d7f0 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c internal use onl
5d800 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c y */.#define SQL
5d810 49 54 45 5f 46 55 4e 43 5f 43 4f 55 4e 54 20 20 ITE_FUNC_COUNT
5d820 20 20 30 78 32 30 20 2f 2a 20 42 75 69 6c 74 2d 0x20 /* Built-
5d830 69 6e 20 63 6f 75 6e 74 28 2a 29 20 61 67 67 72 in count(*) aggr
5d840 65 67 61 74 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 egate */../*.**
5d850 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 The following th
5d860 72 65 65 20 6d 61 63 72 6f 73 2c 20 46 55 4e 43 ree macros, FUNC
5d870 54 49 4f 4e 28 29 2c 20 4c 49 4b 45 46 55 4e 43 TION(), LIKEFUNC
5d880 28 29 20 61 6e 64 20 41 47 47 52 45 47 41 54 45 () and AGGREGATE
5d890 28 29 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 74 () are.** used t
5d8a0 6f 20 63 72 65 61 74 65 20 74 68 65 20 69 6e 69 o create the ini
5d8b0 74 69 61 6c 69 7a 65 72 73 20 66 6f 72 20 74 68 tializers for th
5d8c0 65 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 e FuncDef struct
5d8d0 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 55 ures..**.** FU
5d8e0 4e 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 NCTION(zName, nA
5d8f0 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 rg, iArg, bNC, x
5d900 46 75 6e 63 29 0a 2a 2a 20 20 20 20 20 55 73 65 Func).** Use
5d910 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 63 d to create a sc
5d920 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 alar function de
5d930 66 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66 75 finition of a fu
5d940 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a nction zName .**
5d950 20 20 20 20 20 69 6d 70 6c 65 6d 65 6e 74 65 64 implemented
5d960 20 62 79 20 43 20 66 75 6e 63 74 69 6f 6e 20 78 by C function x
5d970 46 75 6e 63 20 74 68 61 74 20 61 63 63 65 70 74 Func that accept
5d980 73 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73 s nArg arguments
5d990 2e 20 54 68 65 0a 2a 2a 20 20 20 20 20 76 61 6c . The.** val
5d9a0 75 65 20 70 61 73 73 65 64 20 61 73 20 69 41 72 ue passed as iAr
5d9b0 67 20 69 73 20 63 61 73 74 20 74 6f 20 61 20 28 g is cast to a (
5d9c0 76 6f 69 64 2a 29 20 61 6e 64 20 6d 61 64 65 20 void*) and made
5d9d0 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 20 20 20 available.**
5d9e0 20 61 73 20 74 68 65 20 75 73 65 72 2d 64 61 74 as the user-dat
5d9f0 61 20 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f a (sqlite3_user_
5da00 64 61 74 61 28 29 29 20 66 6f 72 20 74 68 65 20 data()) for the
5da10 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 0a 2a 2a function. If .**
5da20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 20 62 4e argument bN
5da30 43 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 C is true, then
5da40 74 68 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f the SQLITE_FUNC_
5da50 4e 45 45 44 43 4f 4c 4c 20 66 6c 61 67 20 69 73 NEEDCOLL flag is
5da60 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 41 47 set..**.** AG
5da70 47 52 45 47 41 54 45 28 7a 4e 61 6d 65 2c 20 6e GREGATE(zName, n
5da80 41 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 Arg, iArg, bNC,
5da90 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 0a 2a xStep, xFinal).*
5daa0 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 * Used to cr
5dab0 65 61 74 65 20 61 6e 20 61 67 67 72 65 67 61 74 eate an aggregat
5dac0 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e e function defin
5dad0 69 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 ition implemente
5dae0 64 20 62 79 0a 2a 2a 20 20 20 20 20 74 68 65 20 d by.** the
5daf0 43 20 66 75 6e 63 74 69 6f 6e 73 20 78 53 74 65 C functions xSte
5db00 70 20 61 6e 64 20 78 46 69 6e 61 6c 2e 20 54 68 p and xFinal. Th
5db10 65 20 66 69 72 73 74 20 66 6f 75 72 20 70 61 72 e first four par
5db20 61 6d 65 74 65 72 73 0a 2a 2a 20 20 20 20 20 61 ameters.** a
5db30 72 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 69 re interpreted i
5db40 6e 20 74 68 65 20 73 61 6d 65 20 77 61 79 20 61 n the same way a
5db50 73 20 74 68 65 20 66 69 72 73 74 20 34 20 70 61 s the first 4 pa
5db60 72 61 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20 20 rameters to.**
5db70 20 20 20 46 55 4e 43 54 49 4f 4e 28 29 2e 0a 2a FUNCTION()..*
5db80 2a 0a 2a 2a 20 20 20 4c 49 4b 45 46 55 4e 43 28 *.** LIKEFUNC(
5db90 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 zName, nArg, pAr
5dba0 67 2c 20 66 6c 61 67 73 29 0a 2a 2a 20 20 20 20 g, flags).**
5dbb0 20 55 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 Used to create
5dbc0 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f a scalar functio
5dbd0 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 n definition of
5dbe0 61 20 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65 a function zName
5dbf0 20 0a 2a 2a 20 20 20 20 20 74 68 61 74 20 61 63 .** that ac
5dc00 63 65 70 74 73 20 6e 41 72 67 20 61 72 67 75 6d cepts nArg argum
5dc10 65 6e 74 73 20 61 6e 64 20 69 73 20 69 6d 70 6c ents and is impl
5dc20 65 6d 65 6e 74 65 64 20 62 79 20 61 20 63 61 6c emented by a cal
5dc30 6c 20 74 6f 20 43 20 0a 2a 2a 20 20 20 20 20 66 l to C .** f
5dc40 75 6e 63 74 69 6f 6e 20 6c 69 6b 65 46 75 6e 63 unction likeFunc
5dc50 2e 20 41 72 67 75 6d 65 6e 74 20 70 41 72 67 20 . Argument pArg
5dc60 69 73 20 63 61 73 74 20 74 6f 20 61 20 28 76 6f is cast to a (vo
5dc70 69 64 20 2a 29 20 61 6e 64 20 6d 61 64 65 0a 2a id *) and made.*
5dc80 2a 20 20 20 20 20 61 76 61 69 6c 61 62 6c 65 20 * available
5dc90 61 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 as the function
5dca0 75 73 65 72 2d 64 61 74 61 20 28 73 71 6c 69 74 user-data (sqlit
5dcb0 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 29 2e e3_user_data()).
5dcc0 20 54 68 65 0a 2a 2a 20 20 20 20 20 46 75 6e 63 The.** Func
5dcd0 44 65 66 2e 66 6c 61 67 73 20 76 61 72 69 61 62 Def.flags variab
5dce0 6c 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 le is set to the
5dcf0 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 value passed as
5dd00 20 74 68 65 20 66 6c 61 67 73 0a 2a 2a 20 20 20 the flags.**
5dd10 20 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a parameter..*/.
5dd20 23 64 65 66 69 6e 65 20 46 55 4e 43 54 49 4f 4e #define FUNCTION
5dd30 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 (zName, nArg, iA
5dd40 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 rg, bNC, xFunc)
5dd50 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 \. {nArg, SQLIT
5dd60 45 5f 55 54 46 38 2c 20 62 4e 43 2a 53 51 4c 49 E_UTF8, bNC*SQLI
5dd70 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c TE_FUNC_NEEDCOLL
5dd80 2c 20 5c 0a 20 20 20 53 51 4c 49 54 45 5f 49 4e , \. SQLITE_IN
5dd90 54 5f 54 4f 5f 50 54 52 28 69 41 72 67 29 2c 20 T_TO_PTR(iArg),
5dda0 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20 0, xFunc, 0, 0,
5ddb0 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 #zName, 0}.#defi
5ddc0 6e 65 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28 ne STR_FUNCTION(
5ddd0 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 zName, nArg, pAr
5dde0 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c g, bNC, xFunc) \
5ddf0 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 . {nArg, SQLITE
5de00 5f 55 54 46 38 2c 20 62 4e 43 2a 53 51 4c 49 54 _UTF8, bNC*SQLIT
5de10 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c E_FUNC_NEEDCOLL,
5de20 20 5c 0a 20 20 20 70 41 72 67 2c 20 30 2c 20 78 \. pArg, 0, x
5de30 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 Func, 0, 0, #zNa
5de40 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 4c me, 0}.#define L
5de50 49 4b 45 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e IKEFUNC(zName, n
5de60 41 72 67 2c 20 61 72 67 2c 20 66 6c 61 67 73 29 Arg, arg, flags)
5de70 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 \. {nArg, SQLI
5de80 54 45 5f 55 54 46 38 2c 20 66 6c 61 67 73 2c 20 TE_UTF8, flags,
5de90 28 76 6f 69 64 20 2a 29 61 72 67 2c 20 30 2c 20 (void *)arg, 0,
5dea0 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 2c 20 likeFunc, 0, 0,
5deb0 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 #zName, 0}.#defi
5dec0 6e 65 20 41 47 47 52 45 47 41 54 45 28 7a 4e 61 ne AGGREGATE(zNa
5ded0 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67 2c 20 6e me, nArg, arg, n
5dee0 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c c, xStep, xFinal
5def0 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c ) \. {nArg, SQL
5df00 49 54 45 5f 55 54 46 38 2c 20 6e 63 2a 53 51 4c ITE_UTF8, nc*SQL
5df10 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c ITE_FUNC_NEEDCOL
5df20 4c 2c 20 5c 0a 20 20 20 53 51 4c 49 54 45 5f 49 L, \. SQLITE_I
5df30 4e 54 5f 54 4f 5f 50 54 52 28 61 72 67 29 2c 20 NT_TO_PTR(arg),
5df40 30 2c 20 30 2c 20 78 53 74 65 70 2c 78 46 69 6e 0, 0, xStep,xFin
5df50 61 6c 2c 23 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a al,#zName,0}../*
5df60 0a 2a 2a 20 41 6c 6c 20 63 75 72 72 65 6e 74 20 .** All current
5df70 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73 savepoints are s
5df80 74 6f 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b 65 tored in a linke
5df90 64 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20 d list starting
5dfa0 61 74 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 70 53 at.** sqlite3.pS
5dfb0 61 76 65 70 6f 69 6e 74 2e 20 54 68 65 20 66 69 avepoint. The fi
5dfc0 72 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 rst element in t
5dfd0 68 65 20 6c 69 73 74 20 69 73 20 74 68 65 20 6d he list is the m
5dfe0 6f 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 ost recently.**
5dff0 6f 70 65 6e 65 64 20 73 61 76 65 70 6f 69 6e 74 opened savepoint
5e000 2e 20 53 61 76 65 70 6f 69 6e 74 73 20 61 72 65 . Savepoints are
5e010 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6c 69 added to the li
5e020 73 74 20 62 79 20 74 68 65 20 76 64 62 65 0a 2a st by the vdbe.*
5e030 2a 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 * OP_Savepoint i
5e040 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 nstruction..*/.s
5e050 74 72 75 63 74 20 53 61 76 65 70 6f 69 6e 74 20 truct Savepoint
5e060 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b {. char *zName;
5e070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e080 20 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 70 /* Savep
5e090 6f 69 6e 74 20 6e 61 6d 65 20 28 6e 75 6c 2d 74 oint name (nul-t
5e0a0 65 72 6d 69 6e 61 74 65 64 29 20 2a 2f 0a 20 20 erminated) */.
5e0b0 69 36 34 20 6e 44 65 66 65 72 72 65 64 43 6f 6e i64 nDeferredCon
5e0c0 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
5e0d0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
5e0e0 20 64 65 66 65 72 72 65 64 20 66 6b 20 76 69 6f deferred fk vio
5e0f0 6c 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 53 61 76 lations */. Sav
5e100 65 70 6f 69 6e 74 20 2a 70 4e 65 78 74 3b 20 20 epoint *pNext;
5e110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e120 20 2f 2a 20 50 61 72 65 6e 74 20 73 61 76 65 70 /* Parent savep
5e130 6f 69 6e 74 20 28 69 66 20 61 6e 79 29 20 2a 2f oint (if any) */
5e140 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 .};../*.** The f
5e150 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 75 73 65 ollowing are use
5e160 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 d as the second
5e170 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c parameter to sql
5e180 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28 29 2c ite3Savepoint(),
5e190 0a 2a 2a 20 61 6e 64 20 61 73 20 74 68 65 20 50 .** and as the P
5e1a0 31 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 1 argument to th
5e1b0 65 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 e OP_Savepoint i
5e1c0 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 nstruction..*/.#
5e1d0 64 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 define SAVEPOINT
5e1e0 5f 42 45 47 49 4e 20 20 20 20 20 20 30 0a 23 64 _BEGIN 0.#d
5e1f0 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f efine SAVEPOINT_
5e200 52 45 4c 45 41 53 45 20 20 20 20 31 0a 23 64 65 RELEASE 1.#de
5e210 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 52 fine SAVEPOINT_R
5e220 4f 4c 4c 42 41 43 4b 20 20 20 32 0a 0a 0a 2f 2a OLLBACK 2.../*
5e230 0a 2a 2a 20 45 61 63 68 20 53 51 4c 69 74 65 20 .** Each SQLite
5e240 6d 6f 64 75 6c 65 20 28 76 69 72 74 75 61 6c 20 module (virtual
5e250 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e table definition
5e260 29 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 ) is defined by
5e270 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f an.** instance o
5e280 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
5e290 73 74 72 75 63 74 75 72 65 2c 20 73 74 6f 72 65 structure, store
5e2a0 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 d in the sqlite3
5e2b0 2e 61 4d 6f 64 75 6c 65 0a 2a 2a 20 68 61 73 68 .aModule.** hash
5e2c0 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 table..*/.struc
5e2d0 74 20 4d 6f 64 75 6c 65 20 7b 0a 20 20 63 6f 6e t Module {. con
5e2e0 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c st sqlite3_modul
5e2f0 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20 e *pModule;
5e300 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 70 6f /* Callback po
5e310 69 6e 74 65 72 73 20 2a 2f 0a 20 20 63 6f 6e 73 inters */. cons
5e320 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 t char *zName;
5e330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e340 20 2f 2a 20 4e 61 6d 65 20 70 61 73 73 65 64 20 /* Name passed
5e350 74 6f 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 to create_module
5e360 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 () */. void *pA
5e370 75 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ux;
5e380 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5e390 70 41 75 78 20 70 61 73 73 65 64 20 74 6f 20 63 pAux passed to c
5e3a0 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a reate_module() *
5e3b0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74 /. void (*xDest
5e3c0 72 6f 79 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 roy)(void *);
5e3d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 /* Modu
5e3e0 6c 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 le destructor fu
5e3f0 6e 63 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a nction */.};../*
5e400 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 .** information
5e410 61 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d about each colum
5e420 6e 20 6f 66 20 61 6e 20 53 51 4c 20 74 61 62 6c n of an SQL tabl
5e430 65 20 69 73 20 68 65 6c 64 20 69 6e 20 61 6e 20 e is held in an
5e440 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 instance.** of t
5e450 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a his structure..*
5e460 2f 0a 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 /.struct Column
5e470 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b {. char *zName;
5e480 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
5e490 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 this column */.
5e4a0 20 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 Expr *pDflt;
5e4b0 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 6c /* Default val
5e4c0 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d ue of this colum
5e4d0 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 66 n */. char *zDf
5e4e0 6c 74 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 lt; /* Origi
5e4f0 6e 61 6c 20 74 65 78 74 20 6f 66 20 74 68 65 20 nal text of the
5e500 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 2a 2f default value */
5e510 0a 20 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 . char *zType;
5e520 20 20 20 20 2f 2a 20 44 61 74 61 20 74 79 70 65 /* Data type
5e530 20 66 6f 72 20 74 68 69 73 20 63 6f 6c 75 6d 6e for this column
5e540 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c */. char *zCol
5e550 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 l; /* Collat
5e560 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 20 20 49 ing sequence. I
5e570 66 20 4e 55 4c 4c 2c 20 75 73 65 20 74 68 65 20 f NULL, use the
5e580 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20 default */. u8
5e590 6e 6f 74 4e 75 6c 6c 3b 20 20 20 20 20 20 2f 2a notNull; /*
5e5a0 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20 69 True if there i
5e5b0 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e s a NOT NULL con
5e5c0 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 75 38 20 straint */. u8
5e5d0 69 73 50 72 69 6d 4b 65 79 3b 20 20 20 20 2f 2a isPrimKey; /*
5e5e0 20 54 72 75 65 20 69 66 20 74 68 69 73 20 63 6f True if this co
5e5f0 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 20 lumn is part of
5e600 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 the PRIMARY KEY
5e610 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 */. char affini
5e620 74 79 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 ty; /* One of
5e630 74 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 2e the SQLITE_AFF_.
5e640 2e 2e 20 76 61 6c 75 65 73 20 2a 2f 0a 23 69 66 .. values */.#if
5e650 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
5e660 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
5e670 75 38 20 69 73 48 69 64 64 65 6e 3b 20 20 20 20 u8 isHidden;
5e680 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 /* True if this
5e690 20 63 6f 6c 75 6d 6e 20 69 73 20 27 68 69 64 64 column is 'hidd
5e6a0 65 6e 27 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b en' */.#endif.};
5e6b0 0a 0a 2f 2a 0a 2a 2a 20 41 20 22 43 6f 6c 6c 61 ../*.** A "Colla
5e6c0 74 69 6e 67 20 53 65 71 75 65 6e 63 65 22 20 69 ting Sequence" i
5e6d0 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20 s defined by an
5e6e0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
5e6f0 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 following.** str
5e700 75 63 74 75 72 65 2e 20 43 6f 6e 63 65 70 74 75 ucture. Conceptu
5e710 61 6c 6c 79 2c 20 61 20 63 6f 6c 6c 61 74 69 6e ally, a collatin
5e720 67 20 73 65 71 75 65 6e 63 65 20 63 6f 6e 73 69 g sequence consi
5e730 73 74 73 20 6f 66 20 61 20 6e 61 6d 65 20 61 6e sts of a name an
5e740 64 0a 2a 2a 20 61 20 63 6f 6d 70 61 72 69 73 6f d.** a compariso
5e750 6e 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 64 n routine that d
5e760 65 66 69 6e 65 73 20 74 68 65 20 6f 72 64 65 72 efines the order
5e770 20 6f 66 20 74 68 61 74 20 73 65 71 75 65 6e 63 of that sequenc
5e780 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d e..**.** There m
5e790 61 79 20 74 77 6f 20 73 65 70 61 72 61 74 65 20 ay two separate
5e7a0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
5e7b0 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e of the collation
5e7c0 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 6e 65 0a 2a function, one.*
5e7d0 2a 20 74 68 61 74 20 70 72 6f 63 65 73 73 65 73 * that processes
5e7e0 20 74 65 78 74 20 69 6e 20 55 54 46 2d 38 20 65 text in UTF-8 e
5e7f0 6e 63 6f 64 69 6e 67 20 28 43 6f 6c 6c 53 65 71 ncoding (CollSeq
5e800 2e 78 43 6d 70 29 20 61 6e 64 20 61 6e 6f 74 68 .xCmp) and anoth
5e810 65 72 20 74 68 61 74 0a 2a 2a 20 70 72 6f 63 65 er that.** proce
5e820 73 73 65 73 20 74 65 78 74 20 65 6e 63 6f 64 65 sses text encode
5e830 64 20 69 6e 20 55 54 46 2d 31 36 20 28 43 6f 6c d in UTF-16 (Col
5e840 6c 53 65 71 2e 78 43 6d 70 31 36 29 2c 20 75 73 lSeq.xCmp16), us
5e850 69 6e 67 20 74 68 65 20 6d 61 63 68 69 6e 65 0a ing the machine.
5e860 2a 2a 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f ** native byte o
5e870 72 64 65 72 2e 20 57 68 65 6e 20 61 20 63 6f 6c rder. When a col
5e880 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
5e890 69 73 20 69 6e 76 6f 6b 65 64 2c 20 53 51 4c 69 is invoked, SQLi
5e8a0 74 65 20 73 65 6c 65 63 74 73 0a 2a 2a 20 74 68 te selects.** th
5e8b0 65 20 76 65 72 73 69 6f 6e 20 74 68 61 74 20 77 e version that w
5e8c0 69 6c 6c 20 72 65 71 75 69 72 65 20 74 68 65 20 ill require the
5e8d0 6c 65 61 73 74 20 65 78 70 65 6e 73 69 76 65 20 least expensive
5e8e0 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 72 61 6e encoding.** tran
5e8f0 73 6c 61 74 69 6f 6e 73 2c 20 69 66 20 61 6e 79 slations, if any
5e900 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 6f 6c 6c ..**.** The Coll
5e910 53 65 71 2e 70 55 73 65 72 20 6d 65 6d 62 65 72 Seq.pUser member
5e920 20 76 61 72 69 61 62 6c 65 20 69 73 20 61 6e 20 variable is an
5e930 65 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 20 extra parameter
5e940 74 68 61 74 20 70 61 73 73 65 64 20 69 6e 0a 2a that passed in.*
5e950 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 * as the first a
5e960 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 55 rgument to the U
5e970 54 46 2d 38 20 63 6f 6d 70 61 72 69 73 6f 6e 20 TF-8 comparison
5e980 66 75 6e 63 74 69 6f 6e 2c 20 78 43 6d 70 2e 0a function, xCmp..
5e990 2a 2a 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 72 ** CollSeq.pUser
5e9a0 31 36 20 69 73 20 74 68 65 20 65 71 75 69 76 61 16 is the equiva
5e9b0 6c 65 6e 74 20 66 6f 72 20 74 68 65 20 55 54 46 lent for the UTF
5e9c0 2d 31 36 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 -16 comparison f
5e9d0 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 78 43 6d 70 unction,.** xCmp
5e9e0 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f 74 16..**.** If bot
5e9f0 68 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 20 61 h CollSeq.xCmp a
5ea00 6e 64 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 nd CollSeq.xCmp1
5ea10 36 20 61 72 65 20 4e 55 4c 4c 2c 20 69 74 20 6d 6 are NULL, it m
5ea20 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a eans that the.**
5ea30 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
5ea40 6e 63 65 20 69 73 20 75 6e 64 65 66 69 6e 65 64 nce is undefined
5ea50 2e 20 20 49 6e 64 69 63 65 73 20 62 75 69 6c 74 . Indices built
5ea60 20 6f 6e 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 on an undefined
5ea70 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 .** collating se
5ea80 71 75 65 6e 63 65 20 6d 61 79 20 6e 6f 74 20 62 quence may not b
5ea90 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 e read or writte
5eaa0 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f 6c n..*/.struct Col
5eab0 6c 53 65 71 20 7b 0a 20 20 63 68 61 72 20 2a 7a lSeq {. char *z
5eac0 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f Name; /
5ead0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f * Name of the co
5eae0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
5eaf0 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 , UTF-8 encoded
5eb00 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 */. u8 enc;
5eb10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 /* Te
5eb20 78 74 20 65 6e 63 6f 64 69 6e 67 20 68 61 6e 64 xt encoding hand
5eb30 6c 65 64 20 62 79 20 78 43 6d 70 28 29 20 2a 2f led by xCmp() */
5eb40 0a 20 20 75 38 20 74 79 70 65 3b 20 20 20 20 20 . u8 type;
5eb50 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 /* One
5eb60 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f of the SQLITE_CO
5eb70 4c 4c 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 62 65 LL_... values be
5eb80 6c 6f 77 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 low */. void *p
5eb90 55 73 65 72 3b 20 20 20 20 20 20 20 20 20 20 2f User; /
5eba0 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 * First argument
5ebb0 20 74 6f 20 78 43 6d 70 28 29 20 2a 2f 0a 20 20 to xCmp() */.
5ebc0 69 6e 74 20 28 2a 78 43 6d 70 29 28 76 6f 69 64 int (*xCmp)(void
5ebd0 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 *,int, const voi
5ebe0 64 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 d*, int, const v
5ebf0 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a oid*);. void (*
5ec00 78 44 65 6c 29 28 76 6f 69 64 2a 29 3b 20 20 2f xDel)(void*); /
5ec10 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 * Destructor for
5ec20 20 70 55 73 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a pUser */.};../*
5ec30 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 .** Allowed valu
5ec40 65 73 20 6f 66 20 43 6f 6c 6c 53 65 71 2e 74 79 es of CollSeq.ty
5ec50 70 65 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 pe:.*/.#define S
5ec60 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 QLITE_COLL_BINAR
5ec70 59 20 20 31 20 20 2f 2a 20 54 68 65 20 64 65 66 Y 1 /* The def
5ec80 61 75 6c 74 20 6d 65 6d 63 6d 70 28 29 20 63 6f ault memcmp() co
5ec90 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
5eca0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5ecb0 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 20 20 TE_COLL_NOCASE
5ecc0 32 20 20 2f 2a 20 54 68 65 20 62 75 69 6c 74 2d 2 /* The built-
5ecd0 69 6e 20 4e 4f 43 41 53 45 20 63 6f 6c 6c 61 74 in NOCASE collat
5ece0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a ing sequence */.
5ecf0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
5ed00 4f 4c 4c 5f 52 45 56 45 52 53 45 20 33 20 20 2f OLL_REVERSE 3 /
5ed10 2a 20 54 68 65 20 62 75 69 6c 74 2d 69 6e 20 52 * The built-in R
5ed20 45 56 45 52 53 45 20 63 6f 6c 6c 61 74 69 6e 67 EVERSE collating
5ed30 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 sequence */.#de
5ed40 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c fine SQLITE_COLL
5ed50 5f 55 53 45 52 20 20 20 20 30 20 20 2f 2a 20 41 _USER 0 /* A
5ed60 6e 79 20 6f 74 68 65 72 20 75 73 65 72 2d 64 65 ny other user-de
5ed70 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6e 67 20 fined collating
5ed80 73 65 71 75 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a sequence */../*.
5ed90 2a 2a 20 41 20 73 6f 72 74 20 6f 72 64 65 72 20 ** A sort order
5eda0 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 41 53 can be either AS
5edb0 43 20 6f 72 20 44 45 53 43 2e 0a 2a 2f 0a 23 64 C or DESC..*/.#d
5edc0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 5f efine SQLITE_SO_
5edd0 41 53 43 20 20 20 20 20 20 20 30 20 20 2f 2a 20 ASC 0 /*
5ede0 53 6f 72 74 20 69 6e 20 61 73 63 65 6e 64 69 6e Sort in ascendin
5edf0 67 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69 g order */.#defi
5ee00 6e 65 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45 53 ne SQLITE_SO_DES
5ee10 43 20 20 20 20 20 20 31 20 20 2f 2a 20 53 6f 72 C 1 /* Sor
5ee20 74 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f t in ascending o
5ee30 72 64 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 rder */../*.** C
5ee40 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 olumn affinity t
5ee50 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 ypes..**.** Thes
5ee60 65 20 75 73 65 64 20 74 6f 20 68 61 76 65 20 6d e used to have m
5ee70 6e 65 6d 6f 6e 69 63 20 6e 61 6d 65 20 6c 69 6b nemonic name lik
5ee80 65 20 27 69 27 20 66 6f 72 20 53 51 4c 49 54 45 e 'i' for SQLITE
5ee90 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 61 6e 64 _AFF_INTEGER and
5eea0 0a 2a 2a 20 27 74 27 20 66 6f 72 20 53 51 4c 49 .** 't' for SQLI
5eeb0 54 45 5f 41 46 46 5f 54 45 58 54 2e 20 20 42 75 TE_AFF_TEXT. Bu
5eec0 74 20 77 65 20 63 61 6e 20 73 61 76 65 20 61 20 t we can save a
5eed0 6c 69 74 74 6c 65 20 73 70 61 63 65 20 61 6e 64 little space and
5eee0 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 74 68 65 20 improve.** the
5eef0 73 70 65 65 64 20 61 20 6c 69 74 74 6c 65 20 62 speed a little b
5ef00 79 20 6e 75 6d 62 65 72 69 6e 67 20 74 68 65 20 y numbering the
5ef10 76 61 6c 75 65 73 20 63 6f 6e 73 65 63 75 74 69 values consecuti
5ef20 76 65 6c 79 2e 20 20 0a 2a 2a 0a 2a 2a 20 42 75 vely. .**.** Bu
5ef30 74 20 72 61 74 68 65 72 20 74 68 61 6e 20 73 74 t rather than st
5ef40 61 72 74 20 77 69 74 68 20 30 20 6f 72 20 31 2c art with 0 or 1,
5ef50 20 77 65 20 62 65 67 69 6e 20 77 69 74 68 20 27 we begin with '
5ef60 61 27 2e 20 20 54 68 61 74 20 77 61 79 2c 0a 2a a'. That way,.*
5ef70 2a 20 77 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20 * when multiple
5ef80 61 66 66 69 6e 69 74 79 20 74 79 70 65 73 20 61 affinity types a
5ef90 72 65 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 20 re concatenated
5efa0 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 61 6e into a string an
5efb0 64 0a 2a 2a 20 75 73 65 64 20 61 73 20 74 68 65 d.** used as the
5efc0 20 50 34 20 6f 70 65 72 61 6e 64 2c 20 74 68 65 P4 operand, the
5efd0 79 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20 72 y will be more r
5efe0 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4e eadable..**.** N
5eff0 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 74 68 ote also that th
5f000 65 20 6e 75 6d 65 72 69 63 20 74 79 70 65 73 20 e numeric types
5f010 61 72 65 20 67 72 6f 75 70 65 64 20 74 6f 67 65 are grouped toge
5f020 74 68 65 72 20 73 6f 20 74 68 61 74 20 74 65 73 ther so that tes
5f030 74 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 20 6e 75 ting.** for a nu
5f040 6d 65 72 69 63 20 74 79 70 65 20 69 73 20 61 20 meric type is a
5f050 73 69 6e 67 6c 65 20 63 6f 6d 70 61 72 69 73 6f single compariso
5f060 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 n..*/.#define SQ
5f070 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 20 20 LITE_AFF_TEXT
5f080 20 20 27 61 27 0a 23 64 65 66 69 6e 65 20 53 51 'a'.#define SQ
5f090 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 LITE_AFF_NONE
5f0a0 20 20 27 62 27 0a 23 64 65 66 69 6e 65 20 53 51 'b'.#define SQ
5f0b0 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 LITE_AFF_NUMERIC
5f0c0 20 20 27 63 27 0a 23 64 65 66 69 6e 65 20 53 51 'c'.#define SQ
5f0d0 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 LITE_AFF_INTEGER
5f0e0 20 20 27 64 27 0a 23 64 65 66 69 6e 65 20 53 51 'd'.#define SQ
5f0f0 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 20 20 LITE_AFF_REAL
5f100 20 20 27 65 27 0a 0a 23 64 65 66 69 6e 65 20 73 'e'..#define s
5f110 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63 41 qlite3IsNumericA
5f120 66 66 69 6e 69 74 79 28 58 29 20 20 28 28 58 29 ffinity(X) ((X)
5f130 3e 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d >=SQLITE_AFF_NUM
5f140 45 52 49 43 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ERIC)../*.** The
5f150 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b SQLITE_AFF_MASK
5f160 20 76 61 6c 75 65 73 20 6d 61 73 6b 73 20 6f 66 values masks of
5f170 66 20 74 68 65 20 73 69 67 6e 69 66 69 63 61 6e f the significan
5f180 74 20 62 69 74 73 20 6f 66 20 61 6e 0a 2a 2a 20 t bits of an.**
5f190 61 66 66 69 6e 69 74 79 20 76 61 6c 75 65 2e 20 affinity value.
5f1a0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
5f1b0 54 45 5f 41 46 46 5f 4d 41 53 4b 20 20 20 20 20 TE_AFF_MASK
5f1c0 30 78 36 37 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 69 0x67../*.** Addi
5f1d0 74 69 6f 6e 61 6c 20 62 69 74 20 76 61 6c 75 65 tional bit value
5f1e0 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 4f 52 s that can be OR
5f1f0 65 64 20 77 69 74 68 20 61 6e 20 61 66 66 69 6e ed with an affin
5f200 69 74 79 20 77 69 74 68 6f 75 74 0a 2a 2a 20 63 ity without.** c
5f210 68 61 6e 67 69 6e 67 20 74 68 65 20 61 66 66 69 hanging the affi
5f220 6e 69 74 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 nity..*/.#define
5f230 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 SQLITE_JUMPIFNU
5f240 4c 4c 20 20 20 30 78 30 38 20 20 2f 2a 20 6a 75 LL 0x08 /* ju
5f250 6d 70 73 20 69 66 20 65 69 74 68 65 72 20 6f 70 mps if either op
5f260 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a 2f erand is NULL */
5f270 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5f280 53 54 4f 52 45 50 32 20 20 20 20 20 20 30 78 31 STOREP2 0x1
5f290 30 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 0 /* Store resu
5f2a0 6c 74 20 69 6e 20 72 65 67 5b 50 32 5d 20 72 61 lt in reg[P2] ra
5f2b0 74 68 65 72 20 74 68 61 6e 20 6a 75 6d 70 20 2a ther than jump *
5f2c0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5f2d0 5f 4e 55 4c 4c 45 51 20 20 20 20 20 20 20 30 78 _NULLEQ 0x
5f2e0 38 30 20 20 2f 2a 20 4e 55 4c 4c 3d 4e 55 4c 4c 80 /* NULL=NULL
5f2f0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 */../*.** An ob
5f300 6a 65 63 74 20 6f 66 20 74 68 69 73 20 74 79 70 ject of this typ
5f310 65 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 e is created for
5f320 20 65 61 63 68 20 76 69 72 74 75 61 6c 20 74 61 each virtual ta
5f330 62 6c 65 20 70 72 65 73 65 6e 74 20 69 6e 0a 2a ble present in.*
5f340 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 * the database s
5f350 63 68 65 6d 61 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 chema. .**.** If
5f360 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 the database sc
5f370 68 65 6d 61 20 69 73 20 73 68 61 72 65 64 2c 20 hema is shared,
5f380 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e then there is on
5f390 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 e instance of th
5f3a0 69 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 is.** structure
5f3b0 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73 for each databas
5f3c0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 73 71 e connection (sq
5f3d0 6c 69 74 65 33 2a 29 20 74 68 61 74 20 75 73 65 lite3*) that use
5f3e0 73 20 74 68 65 20 73 68 61 72 65 64 0a 2a 2a 20 s the shared.**
5f3f0 73 63 68 65 6d 61 2e 20 54 68 69 73 20 69 73 20 schema. This is
5f400 62 65 63 61 75 73 65 20 65 61 63 68 20 64 61 74 because each dat
5f410 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
5f420 20 72 65 71 75 69 72 65 73 20 69 74 73 20 6f 77 requires its ow
5f430 6e 20 75 6e 69 71 75 65 0a 2a 2a 20 69 6e 73 74 n unique.** inst
5f440 61 6e 63 65 20 6f 66 20 74 68 65 20 73 71 6c 69 ance of the sqli
5f450 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 te3_vtab* handle
5f460 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 used to access
5f470 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c the virtual tabl
5f480 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 e .** implementa
5f490 74 69 6f 6e 2e 20 73 71 6c 69 74 65 33 5f 76 74 tion. sqlite3_vt
5f4a0 61 62 2a 20 68 61 6e 64 6c 65 73 20 63 61 6e 20 ab* handles can
5f4b0 6e 6f 74 20 62 65 20 73 68 61 72 65 64 20 62 65 not be shared be
5f4c0 74 77 65 65 6e 20 0a 2a 2a 20 64 61 74 61 62 61 tween .** databa
5f4d0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20 se connections,
5f4e0 65 76 65 6e 20 77 68 65 6e 20 74 68 65 20 72 65 even when the re
5f4f0 73 74 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d st of the in-mem
5f500 6f 72 79 20 64 61 74 61 62 61 73 65 20 0a 2a 2a ory database .**
5f510 20 73 63 68 65 6d 61 20 69 73 20 73 68 61 72 65 schema is share
5f520 64 2c 20 61 73 20 74 68 65 20 69 6d 70 6c 65 6d d, as the implem
5f530 65 6e 74 61 74 69 6f 6e 20 6f 66 74 65 6e 20 73 entation often s
5f540 74 6f 72 65 73 20 74 68 65 20 64 61 74 61 62 61 tores the databa
5f550 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e se.** connection
5f560 20 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 74 handle passed t
5f570 6f 20 69 74 20 76 69 61 20 74 68 65 20 78 43 6f o it via the xCo
5f580 6e 6e 65 63 74 28 29 20 6f 72 20 78 43 72 65 61 nnect() or xCrea
5f590 74 65 28 29 20 6d 65 74 68 6f 64 0a 2a 2a 20 64 te() method.** d
5f5a0 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 uring initializa
5f5b0 74 69 6f 6e 20 69 6e 74 65 72 6e 61 6c 6c 79 2e tion internally.
5f5c0 20 54 68 69 73 20 64 61 74 61 62 61 73 65 20 63 This database c
5f5d0 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 onnection handle
5f5e0 20 6d 61 79 0a 2a 2a 20 74 68 65 6e 20 75 73 65 may.** then use
5f5f0 64 20 62 79 20 74 68 65 20 76 69 72 74 75 61 6c d by the virtual
5f600 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 table implement
5f610 61 74 69 6f 6e 20 74 6f 20 61 63 63 65 73 73 20 ation to access
5f620 72 65 61 6c 20 74 61 62 6c 65 73 20 0a 2a 2a 20 real tables .**
5f630 77 69 74 68 69 6e 20 74 68 65 20 64 61 74 61 62 within the datab
5f640 61 73 65 2e 20 53 6f 20 74 68 61 74 20 74 68 65 ase. So that the
5f650 79 20 61 70 70 65 61 72 20 61 73 20 70 61 72 74 y appear as part
5f660 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 73 20 of the callers
5f670 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c .** transaction,
5f680 20 74 68 65 73 65 20 61 63 63 65 73 73 65 73 20 these accesses
5f690 6e 65 65 64 20 74 6f 20 62 65 20 6d 61 64 65 20 need to be made
5f6a0 76 69 61 20 74 68 65 20 73 61 6d 65 20 64 61 74 via the same dat
5f6b0 61 62 61 73 65 20 0a 2a 2a 20 63 6f 6e 6e 65 63 abase .** connec
5f6c0 74 69 6f 6e 20 61 73 20 74 68 61 74 20 75 73 65 tion as that use
5f6d0 64 20 74 6f 20 65 78 65 63 75 74 65 20 53 51 4c d to execute SQL
5f6e0 20 6f 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20 74 operations on t
5f6f0 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 he virtual table
5f700 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 56 54 61 62 ..**.** All VTab
5f710 6c 65 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 le objects that
5f720 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 61 20 correspond to a
5f730 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 single table in
5f740 61 20 73 68 61 72 65 64 0a 2a 2a 20 64 61 74 61 a shared.** data
5f750 62 61 73 65 20 73 63 68 65 6d 61 20 61 72 65 20 base schema are
5f760 69 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72 65 64 initially stored
5f770 20 69 6e 20 61 20 6c 69 6e 6b 65 64 2d 6c 69 73 in a linked-lis
5f780 74 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 0a t pointed to by.
5f790 2a 2a 20 74 68 65 20 54 61 62 6c 65 2e 70 56 54 ** the Table.pVT
5f7a0 61 62 6c 65 20 6d 65 6d 62 65 72 20 76 61 72 69 able member vari
5f7b0 61 62 6c 65 20 6f 66 20 74 68 65 20 63 6f 72 72 able of the corr
5f7c0 65 73 70 6f 6e 64 69 6e 67 20 54 61 62 6c 65 20 esponding Table
5f7d0 6f 62 6a 65 63 74 2e 0a 2a 2a 20 57 68 65 6e 20 object..** When
5f7e0 61 6e 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 an sqlite3_prepa
5f7f0 72 65 28 29 20 6f 70 65 72 61 74 69 6f 6e 20 69 re() operation i
5f800 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 61 63 s required to ac
5f810 63 65 73 73 20 74 68 65 20 76 69 72 74 75 61 6c cess the virtual
5f820 0a 2a 2a 20 74 61 62 6c 65 2c 20 69 74 20 73 65 .** table, it se
5f830 61 72 63 68 65 73 20 74 68 65 20 6c 69 73 74 20 arches the list
5f840 66 6f 72 20 74 68 65 20 56 54 61 62 6c 65 20 74 for the VTable t
5f850 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 hat corresponds
5f860 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 to the.** databa
5f870 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 6f se connection do
5f880 69 6e 67 20 74 68 65 20 70 72 65 70 61 72 69 6e ing the preparin
5f890 67 20 73 6f 20 61 73 20 74 6f 20 75 73 65 20 74 g so as to use t
5f8a0 68 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20 73 71 he correct.** sq
5f8b0 6c 69 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 lite3_vtab* hand
5f8c0 6c 65 20 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c le in the compil
5f8d0 65 64 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 ed query..**.**
5f8e0 57 68 65 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 When an in-memor
5f8f0 79 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 69 y Table object i
5f900 73 20 64 65 6c 65 74 65 64 20 28 66 6f 72 20 65 s deleted (for e
5f910 78 61 6d 70 6c 65 20 77 68 65 6e 20 74 68 65 0a xample when the.
5f920 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 62 65 69 ** schema is bei
5f930 6e 67 20 72 65 6c 6f 61 64 65 64 20 66 6f 72 20 ng reloaded for
5f940 73 6f 6d 65 20 72 65 61 73 6f 6e 29 2c 20 74 68 some reason), th
5f950 65 20 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 e VTable objects
5f960 20 61 72 65 20 6e 6f 74 20 0a 2a 2a 20 64 65 6c are not .** del
5f970 65 74 65 64 20 61 6e 64 20 74 68 65 20 73 71 6c eted and the sql
5f980 69 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c ite3_vtab* handl
5f990 65 73 20 61 72 65 20 6e 6f 74 20 78 44 69 73 63 es are not xDisc
5f9a0 6f 6e 6e 65 63 74 28 29 65 64 20 0a 2a 2a 20 69 onnect()ed .** i
5f9b0 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 6e 73 74 mmediately. Inst
5f9c0 65 61 64 2c 20 74 68 65 79 20 61 72 65 20 6d 6f ead, they are mo
5f9d0 76 65 64 20 66 72 6f 6d 20 74 68 65 20 54 61 62 ved from the Tab
5f9e0 6c 65 2e 70 56 54 61 62 6c 65 20 6c 69 73 74 20 le.pVTable list
5f9f0 74 6f 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 6c 69 to.** another li
5fa00 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 64 nked list headed
5fa10 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 2e by the sqlite3.
5fa20 70 44 69 73 63 6f 6e 6e 65 63 74 20 6d 65 6d 62 pDisconnect memb
5fa30 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 72 er of the.** cor
5fa40 72 65 73 70 6f 6e 64 69 6e 67 20 73 71 6c 69 74 responding sqlit
5fa50 65 33 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 e3 structure. Th
5fa60 65 79 20 61 72 65 20 74 68 65 6e 20 64 65 6c 65 ey are then dele
5fa70 74 65 64 2f 78 44 69 73 63 6f 6e 6e 65 63 74 65 ted/xDisconnecte
5fa80 64 20 0a 2a 2a 20 6e 65 78 74 20 74 69 6d 65 20 d .** next time
5fa90 61 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 a statement is p
5faa0 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 73 61 repared using sa
5fab0 69 64 20 73 71 6c 69 74 65 33 2a 2e 20 54 68 69 id sqlite3*. Thi
5fac0 73 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20 s is done.** to
5fad0 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 20 69 avoid deadlock i
5fae0 73 73 75 65 73 20 69 6e 76 6f 6c 76 69 6e 67 20 ssues involving
5faf0 6d 75 6c 74 69 70 6c 65 20 73 71 6c 69 74 65 33 multiple sqlite3
5fb00 2e 6d 75 74 65 78 20 6d 75 74 65 78 65 73 2e 0a .mutex mutexes..
5fb10 2a 2a 20 52 65 66 65 72 20 74 6f 20 63 6f 6d 6d ** Refer to comm
5fb20 65 6e 74 73 20 61 62 6f 76 65 20 66 75 6e 63 74 ents above funct
5fb30 69 6f 6e 20 73 71 6c 69 74 65 33 56 74 61 62 55 ion sqlite3VtabU
5fb40 6e 6c 6f 63 6b 4c 69 73 74 28 29 20 66 6f 72 20 nlockList() for
5fb50 61 6e 0a 2a 2a 20 65 78 70 6c 61 6e 61 74 69 6f an.** explanatio
5fb60 6e 20 61 73 20 74 6f 20 77 68 79 20 69 74 20 69 n as to why it i
5fb70 73 20 73 61 66 65 20 74 6f 20 61 64 64 20 61 6e s safe to add an
5fb80 20 65 6e 74 72 79 20 74 6f 20 61 6e 20 73 71 6c entry to an sql
5fb90 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 ite3.pDisconnect
5fba0 0a 2a 2a 20 6c 69 73 74 20 77 69 74 68 6f 75 74 .** list without
5fbb0 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 63 6f 72 holding the cor
5fbc0 72 65 73 70 6f 6e 64 69 6e 67 20 73 71 6c 69 74 responding sqlit
5fbd0 65 33 2e 6d 75 74 65 78 20 6d 75 74 65 78 2e 0a e3.mutex mutex..
5fbe0 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 **.** The memory
5fbf0 20 66 6f 72 20 6f 62 6a 65 63 74 73 20 6f 66 20 for objects of
5fc00 74 68 69 73 20 74 79 70 65 20 69 73 20 61 6c 77 this type is alw
5fc10 61 79 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 ays allocated by
5fc20 20 0a 2a 2a 20 73 71 6c 69 74 65 33 44 62 4d 61 .** sqlite3DbMa
5fc30 6c 6c 6f 63 28 29 2c 20 75 73 69 6e 67 20 74 68 lloc(), using th
5fc40 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e e connection han
5fc50 64 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 56 54 dle stored in VT
5fc60 61 62 6c 65 2e 64 62 20 61 73 20 0a 2a 2a 20 74 able.db as .** t
5fc70 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
5fc80 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 54 61 t..*/.struct VTa
5fc90 62 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 ble {. sqlite3
5fca0 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 *db;
5fcb0 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f /* Database co
5fcc0 6e 6e 65 63 74 69 6f 6e 20 61 73 73 6f 63 69 61 nnection associa
5fcd0 74 65 64 20 77 69 74 68 20 74 68 69 73 20 74 61 ted with this ta
5fce0 62 6c 65 20 2a 2f 0a 20 20 4d 6f 64 75 6c 65 20 ble */. Module
5fcf0 2a 70 4d 6f 64 3b 20 20 20 20 20 20 20 20 20 20 *pMod;
5fd00 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
5fd10 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e module implemen
5fd20 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 tation */. sqli
5fd30 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b te3_vtab *pVtab;
5fd40 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
5fd50 20 74 6f 20 76 74 61 62 20 69 6e 73 74 61 6e 63 to vtab instanc
5fd60 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b e */. int nRef;
5fd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5fd80 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f /* Number of po
5fd90 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 inters to this s
5fda0 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 56 54 tructure */. VT
5fdb0 61 62 6c 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 able *pNext;
5fdc0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 /* Next
5fdd0 69 6e 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 in linked list (
5fde0 73 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 7d 3b see above) */.};
5fdf0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c ../*.** Each SQL
5fe00 20 74 61 62 6c 65 20 69 73 20 72 65 70 72 65 73 table is repres
5fe10 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 ented in memory
5fe20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f by an instance o
5fe30 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 f the.** followi
5fe40 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a ng structure..**
5fe50 0a 2a 2a 20 54 61 62 6c 65 2e 7a 4e 61 6d 65 20 .** Table.zName
5fe60 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 is the name of t
5fe70 68 65 20 74 61 62 6c 65 2e 20 20 54 68 65 20 63 he table. The c
5fe80 61 73 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69 ase of the origi
5fe90 6e 61 6c 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 nal.** CREATE TA
5fea0 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73 BLE statement is
5feb0 20 73 74 6f 72 65 64 2c 20 62 75 74 20 63 61 73 stored, but cas
5fec0 65 20 69 73 20 6e 6f 74 20 73 69 67 6e 69 66 69 e is not signifi
5fed0 63 61 6e 74 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 cant for.** comp
5fee0 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 arisons..**.** T
5fef0 61 62 6c 65 2e 6e 43 6f 6c 20 69 73 20 74 68 65 able.nCol is the
5ff00 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
5ff10 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 ns in this table
5ff20 2e 20 20 54 61 62 6c 65 2e 61 43 6f 6c 20 69 73 . Table.aCol is
5ff30 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f a.** pointer to
5ff40 20 61 6e 20 61 72 72 61 79 20 6f 66 20 43 6f 6c an array of Col
5ff50 75 6d 6e 20 73 74 72 75 63 74 75 72 65 73 2c 20 umn structures,
5ff60 6f 6e 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c one for each col
5ff70 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 umn..**.** If th
5ff80 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 49 e table has an I
5ff90 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K
5ffa0 45 59 2c 20 74 68 65 6e 20 54 61 62 6c 65 2e 69 EY, then Table.i
5ffb0 50 4b 65 79 20 69 73 20 74 68 65 20 69 6e 64 65 PKey is the inde
5ffc0 78 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 x of.** the colu
5ffd0 6d 6e 20 74 68 61 74 20 69 73 20 74 68 61 74 20 mn that is that
5ffe0 6b 65 79 2e 20 20 20 4f 74 68 65 72 77 69 73 65 key. Otherwise
5fff0 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20 Table.iPKey is
60000 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 74 65 0a negative. Note.
60010 2a 2a 20 74 68 61 74 20 74 68 65 20 64 61 74 61 ** that the data
60020 74 79 70 65 20 6f 66 20 74 68 65 20 50 52 49 4d type of the PRIM
60030 41 52 59 20 4b 45 59 20 6d 75 73 74 20 62 65 20 ARY KEY must be
60040 49 4e 54 45 47 45 52 20 66 6f 72 20 74 68 69 73 INTEGER for this
60050 20 66 69 65 6c 64 20 74 6f 0a 2a 2a 20 62 65 20 field to.** be
60060 73 65 74 2e 20 20 41 6e 20 49 4e 54 45 47 45 52 set. An INTEGER
60070 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 73 20 PRIMARY KEY is
60080 75 73 65 64 20 61 73 20 74 68 65 20 72 6f 77 69 used as the rowi
60090 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f d for each row o
600a0 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e 20 f.** the table.
600b0 20 49 66 20 61 20 74 61 62 6c 65 20 68 61 73 20 If a table has
600c0 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 no INTEGER PRIMA
600d0 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 61 20 72 RY KEY, then a r
600e0 61 6e 64 6f 6d 20 72 6f 77 69 64 0a 2a 2a 20 69 andom rowid.** i
600f0 73 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20 s generated for
60100 65 61 63 68 20 72 6f 77 20 6f 66 20 74 68 65 20 each row of the
60110 74 61 62 6c 65 2e 20 20 54 46 5f 48 61 73 50 72 table. TF_HasPr
60120 69 6d 61 72 79 4b 65 79 20 69 73 20 73 65 74 20 imaryKey is set
60130 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 if.** the table
60140 68 61 73 20 61 6e 79 20 50 52 49 4d 41 52 59 20 has any PRIMARY
60150 4b 45 59 2c 20 49 4e 54 45 47 45 52 20 6f 72 20 KEY, INTEGER or
60160 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a otherwise..**.**
60170 20 54 61 62 6c 65 2e 74 6e 75 6d 20 69 73 20 74 Table.tnum is t
60180 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 he page number f
60190 6f 72 20 74 68 65 20 72 6f 6f 74 20 42 54 72 65 or the root BTre
601a0 65 20 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 e page of the ta
601b0 62 6c 65 20 69 6e 20 74 68 65 0a 2a 2a 20 64 61 ble in the.** da
601c0 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 tabase file. If
601d0 20 54 61 62 6c 65 2e 69 44 62 20 69 73 20 74 68 Table.iDb is th
601e0 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 e index of the d
601f0 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 62 61 atabase table ba
60200 63 6b 65 6e 64 0a 2a 2a 20 69 6e 20 73 71 6c 69 ckend.** in sqli
60210 74 65 2e 61 44 62 5b 5d 2e 20 20 30 20 69 73 20 te.aDb[]. 0 is
60220 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 for the main dat
60230 61 62 61 73 65 20 61 6e 64 20 31 20 69 73 20 66 abase and 1 is f
60240 6f 72 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 or the file that
60250 0a 2a 2a 20 68 6f 6c 64 73 20 74 65 6d 70 6f 72 .** holds tempor
60260 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64 20 69 ary tables and i
60270 6e 64 69 63 65 73 2e 20 20 49 66 20 54 46 5f 45 ndices. If TF_E
60280 70 68 65 6d 65 72 61 6c 20 69 73 20 73 65 74 0a phemeral is set.
60290 2a 2a 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c ** then the tabl
602a0 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 e is stored in a
602b0 20 66 69 6c 65 20 74 68 61 74 20 69 73 20 61 75 file that is au
602c0 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 tomatically dele
602d0 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 ted.** when the
602e0 56 44 42 45 20 63 75 72 73 6f 72 20 74 6f 20 74 VDBE cursor to t
602f0 68 65 20 74 61 62 6c 65 20 69 73 20 63 6c 6f 73 he table is clos
60300 65 64 2e 20 20 49 6e 20 74 68 69 73 20 63 61 73 ed. In this cas
60310 65 20 54 61 62 6c 65 2e 74 6e 75 6d 20 0a 2a 2a e Table.tnum .**
60320 20 72 65 66 65 72 73 20 56 44 42 45 20 63 75 72 refers VDBE cur
60330 73 6f 72 20 6e 75 6d 62 65 72 20 74 68 61 74 20 sor number that
60340 68 6f 6c 64 73 20 74 68 65 20 74 61 62 6c 65 20 holds the table
60350 6f 70 65 6e 2c 20 6e 6f 74 20 74 6f 20 74 68 65 open, not to the
60360 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 6e 75 root.** page nu
60370 6d 62 65 72 2e 20 20 54 72 61 6e 73 69 65 6e 74 mber. Transient
60380 20 74 61 62 6c 65 73 20 61 72 65 20 75 73 65 64 tables are used
60390 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73 to hold the res
603a0 75 6c 74 73 20 6f 66 20 61 0a 2a 2a 20 73 75 62 ults of a.** sub
603b0 2d 71 75 65 72 79 20 74 68 61 74 20 61 70 70 65 -query that appe
603c0 61 72 73 20 69 6e 73 74 65 61 64 20 6f 66 20 61 ars instead of a
603d0 20 72 65 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65 real table name
603e0 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 in the FROM cla
603f0 75 73 65 20 0a 2a 2a 20 6f 66 20 61 20 53 45 4c use .** of a SEL
60400 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a ECT statement..*
60410 2f 0a 73 74 72 75 63 74 20 54 61 62 6c 65 20 7b /.struct Table {
60420 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4d 65 . sqlite3 *dbMe
60430 6d 3b 20 20 20 20 20 20 2f 2a 20 44 42 20 63 6f m; /* DB co
60440 6e 6e 65 63 74 69 6f 6e 20 75 73 65 64 20 66 6f nnection used fo
60450 72 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f r lookaside allo
60460 63 61 74 69 6f 6e 73 2e 20 2a 2f 0a 20 20 63 68 cations. */. ch
60470 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 ar *zName;
60480 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
60490 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 e table or view
604a0 2a 2f 0a 20 20 69 6e 74 20 69 50 4b 65 79 3b 20 */. int iPKey;
604b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 /* If
604c0 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 75 73 not negative, us
604d0 65 20 61 43 6f 6c 5b 69 50 4b 65 79 5d 20 61 73 e aCol[iPKey] as
604e0 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 the primary key
604f0 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 */. int nCol;
60500 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
60510 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
60520 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f in this table */
60530 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 3b . Column *aCol;
60540 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 /* Infor
60550 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 mation about eac
60560 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 6e h column */. In
60570 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 dex *pIndex;
60580 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 53 51 /* List of SQ
60590 4c 20 69 6e 64 65 78 65 73 20 6f 6e 20 74 68 69 L indexes on thi
605a0 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 69 6e s table. */. in
605b0 74 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 t tnum;
605c0 20 20 20 2f 2a 20 52 6f 6f 74 20 42 54 72 65 65 /* Root BTree
605d0 20 6e 6f 64 65 20 66 6f 72 20 74 68 69 73 20 74 node for this t
605e0 61 62 6c 65 20 28 73 65 65 20 6e 6f 74 65 20 61 able (see note a
605f0 62 6f 76 65 29 20 2a 2f 0a 20 20 53 65 6c 65 63 bove) */. Selec
60600 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 20 t *pSelect;
60610 2f 2a 20 4e 55 4c 4c 20 66 6f 72 20 74 61 62 6c /* NULL for tabl
60620 65 73 2e 20 20 50 6f 69 6e 74 73 20 74 6f 20 64 es. Points to d
60630 65 66 69 6e 69 74 69 6f 6e 20 69 66 20 61 20 76 efinition if a v
60640 69 65 77 2e 20 2a 2f 0a 20 20 75 31 36 20 6e 52 iew. */. u16 nR
60650 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ef; /
60660 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e * Number of poin
60670 74 65 72 73 20 74 6f 20 74 68 69 73 20 54 61 62 ters to this Tab
60680 6c 65 20 2a 2f 0a 20 20 75 38 20 74 61 62 46 6c le */. u8 tabFl
60690 61 67 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ags; /*
606a0 4d 61 73 6b 20 6f 66 20 54 46 5f 2a 20 76 61 6c Mask of TF_* val
606b0 75 65 73 20 2a 2f 0a 20 20 75 38 20 6b 65 79 43 ues */. u8 keyC
606c0 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a onf; /*
606d0 20 57 68 61 74 20 74 6f 20 64 6f 20 69 6e 20 63 What to do in c
606e0 61 73 65 20 6f 66 20 75 6e 69 71 75 65 6e 65 73 ase of uniquenes
606f0 73 20 63 6f 6e 66 6c 69 63 74 20 6f 6e 20 69 50 s conflict on iP
60700 4b 65 79 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 Key */. FKey *p
60710 46 4b 65 79 3b 20 20 20 20 20 20 20 20 20 2f 2a FKey; /*
60720 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 Linked list of
60730 61 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 all foreign keys
60740 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a in this table *
60750 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 /. char *zColAf
60760 66 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 f; /* Stri
60770 6e 67 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 ng defining the
60780 61 66 66 69 6e 69 74 79 20 6f 66 20 65 61 63 68 affinity of each
60790 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 69 66 6e 64 column */.#ifnd
607a0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 ef SQLITE_OMIT_C
607b0 48 45 43 4b 0a 20 20 45 78 70 72 20 2a 70 43 68 HECK. Expr *pCh
607c0 65 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 eck; /* T
607d0 68 65 20 41 4e 44 20 6f 66 20 61 6c 6c 20 43 48 he AND of all CH
607e0 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 ECK constraints
607f0 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 */.#endif.#ifnde
60800 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c f SQLITE_OMIT_AL
60810 54 45 52 54 41 42 4c 45 0a 20 20 69 6e 74 20 61 TERTABLE. int a
60820 64 64 43 6f 6c 4f 66 66 73 65 74 3b 20 20 20 20 ddColOffset;
60830 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20 43 52 45 /* Offset in CRE
60840 41 54 45 20 54 41 42 4c 45 20 73 74 6d 74 20 74 ATE TABLE stmt t
60850 6f 20 61 64 64 20 61 20 6e 65 77 20 63 6f 6c 75 o add a new colu
60860 6d 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 mn */.#endif.#if
60870 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
60880 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
60890 56 54 61 62 6c 65 20 2a 70 56 54 61 62 6c 65 3b VTable *pVTable;
608a0 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 /* List of
608b0 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 2e 20 VTable objects.
608c0 2a 2f 0a 20 20 69 6e 74 20 6e 4d 6f 64 75 6c 65 */. int nModule
608d0 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d Arg; /* Num
608e0 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 ber of arguments
608f0 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a to the module *
60900 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 4d 6f 64 /. char **azMod
60910 75 6c 65 41 72 67 3b 20 20 2f 2a 20 54 65 78 74 uleArg; /* Text
60920 20 6f 66 20 61 6c 6c 20 6d 6f 64 75 6c 65 20 61 of all module a
60930 72 67 73 2e 20 5b 30 5d 20 69 73 20 6d 6f 64 75 rgs. [0] is modu
60940 6c 65 20 6e 61 6d 65 20 2a 2f 0a 23 65 6e 64 69 le name */.#endi
60950 66 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 f. Trigger *pTr
60960 69 67 67 65 72 3b 20 20 20 2f 2a 20 4c 69 73 74 igger; /* List
60970 20 6f 66 20 74 72 69 67 67 65 72 73 20 73 74 6f of triggers sto
60980 72 65 64 20 69 6e 20 70 53 63 68 65 6d 61 20 2a red in pSchema *
60990 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 /. Schema *pSch
609a0 65 6d 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 65 ema; /* Sche
609b0 6d 61 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 ma that contains
609c0 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 this table */.
609d0 20 54 61 62 6c 65 20 2a 70 4e 65 78 74 5a 6f 6d Table *pNextZom
609e0 62 69 65 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 6e bie; /* Next on
609f0 20 74 68 65 20 50 61 72 73 65 2e 70 5a 6f 6d 62 the Parse.pZomb
60a00 69 65 54 61 62 20 6c 69 73 74 20 2a 2f 0a 7d 3b ieTab list */.};
60a10 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 ../*.** Allowed
60a20 76 61 6c 75 65 73 20 66 6f 72 20 54 61 62 65 2e values for Tabe.
60a30 74 61 62 46 6c 61 67 73 2e 0a 2a 2f 0a 23 64 65 tabFlags..*/.#de
60a40 66 69 6e 65 20 54 46 5f 52 65 61 64 6f 6e 6c 79 fine TF_Readonly
60a50 20 20 20 20 20 20 20 20 30 78 30 31 20 20 20 20 0x01
60a60 2f 2a 20 52 65 61 64 2d 6f 6e 6c 79 20 73 79 73 /* Read-only sys
60a70 74 65 6d 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 tem table */.#de
60a80 66 69 6e 65 20 54 46 5f 45 70 68 65 6d 65 72 61 fine TF_Ephemera
60a90 6c 20 20 20 20 20 20 20 30 78 30 32 20 20 20 20 l 0x02
60aa0 2f 2a 20 41 6e 20 65 70 68 65 6d 65 72 61 6c 20 /* An ephemeral
60ab0 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 table */.#define
60ac0 20 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 TF_HasPrimaryKe
60ad0 79 20 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54 y 0x04 /* T
60ae0 61 62 6c 65 20 68 61 73 20 61 20 70 72 69 6d 61 able has a prima
60af0 72 79 20 6b 65 79 20 2a 2f 0a 23 64 65 66 69 6e ry key */.#defin
60b00 65 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 e TF_Autoincreme
60b10 6e 74 20 20 20 30 78 30 38 20 20 20 20 2f 2a 20 nt 0x08 /*
60b20 49 6e 74 65 67 65 72 20 70 72 69 6d 61 72 79 20 Integer primary
60b30 6b 65 79 20 69 73 20 61 75 74 6f 69 6e 63 72 65 key is autoincre
60b40 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ment */.#define
60b50 54 46 5f 56 69 72 74 75 61 6c 20 20 20 20 20 20 TF_Virtual
60b60 20 20 20 30 78 31 30 20 20 20 20 2f 2a 20 49 73 0x10 /* Is
60b70 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 a virtual table
60b80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 4e */.#define TF_N
60b90 65 65 64 4d 65 74 61 64 61 74 61 20 20 20 20 30 eedMetadata 0
60ba0 78 32 30 20 20 20 20 2f 2a 20 61 43 6f 6c 5b 5d x20 /* aCol[]
60bb0 2e 7a 54 79 70 65 20 61 6e 64 20 61 43 6f 6c 5b .zType and aCol[
60bc0 5d 2e 70 43 6f 6c 6c 20 6d 69 73 73 69 6e 67 20 ].pColl missing
60bd0 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 */..../*.** Test
60be0 20 74 6f 20 73 65 65 20 77 68 65 74 68 65 72 20 to see whether
60bf0 6f 72 20 6e 6f 74 20 61 20 74 61 62 6c 65 20 69 or not a table i
60c00 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c s a virtual tabl
60c10 65 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 64 e. This is.** d
60c20 6f 6e 65 20 61 73 20 61 20 6d 61 63 72 6f 20 73 one as a macro s
60c30 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 o that it will b
60c40 65 20 6f 70 74 69 6d 69 7a 65 64 20 6f 75 74 20 e optimized out
60c50 77 68 65 6e 20 76 69 72 74 75 61 6c 0a 2a 2a 20 when virtual.**
60c60 74 61 62 6c 65 20 73 75 70 70 6f 72 74 20 69 73 table support is
60c70 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68 omitted from th
60c80 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 6e e build..*/.#ifn
60c90 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
60ca0 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 20 VIRTUALTABLE.#
60cb0 64 65 66 69 6e 65 20 49 73 56 69 72 74 75 61 6c define IsVirtual
60cc0 28 58 29 20 20 20 20 20 20 28 28 28 58 29 2d 3e (X) (((X)->
60cd0 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 tabFlags & TF_Vi
60ce0 72 74 75 61 6c 29 21 3d 30 29 0a 23 20 20 64 65 rtual)!=0).# de
60cf0 66 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f 6c fine IsHiddenCol
60d00 75 6d 6e 28 58 29 20 28 28 58 29 2d 3e 69 73 48 umn(X) ((X)->isH
60d10 69 64 64 65 6e 29 0a 23 65 6c 73 65 0a 23 20 20 idden).#else.#
60d20 64 65 66 69 6e 65 20 49 73 56 69 72 74 75 61 6c define IsVirtual
60d30 28 58 29 20 20 20 20 20 20 30 0a 23 20 20 64 65 (X) 0.# de
60d40 66 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f 6c fine IsHiddenCol
60d50 75 6d 6e 28 58 29 20 30 0a 23 65 6e 64 69 66 0a umn(X) 0.#endif.
60d60 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 66 6f 72 65 ./*.** Each fore
60d70 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 ign key constrai
60d80 6e 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 nt is an instanc
60d90 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
60da0 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a ng structure..**
60db0 0a 2a 2a 20 41 20 66 6f 72 65 69 67 6e 20 6b 65 .** A foreign ke
60dc0 79 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 y is associated
60dd0 77 69 74 68 20 74 77 6f 20 74 61 62 6c 65 73 2e with two tables.
60de0 20 20 54 68 65 20 22 66 72 6f 6d 22 20 74 61 62 The "from" tab
60df0 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 74 61 62 le is.** the tab
60e00 6c 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 le that contains
60e10 20 74 68 65 20 52 45 46 45 52 45 4e 43 45 53 20 the REFERENCES
60e20 63 6c 61 75 73 65 20 74 68 61 74 20 63 72 65 61 clause that crea
60e30 74 65 73 20 74 68 65 20 66 6f 72 65 69 67 6e 0a tes the foreign.
60e40 2a 2a 20 6b 65 79 2e 20 20 54 68 65 20 22 74 6f ** key. The "to
60e50 22 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 74 " table is the t
60e60 61 62 6c 65 20 74 68 61 74 20 69 73 20 6e 61 6d able that is nam
60e70 65 64 20 69 6e 20 74 68 65 20 52 45 46 45 52 45 ed in the REFERE
60e80 4e 43 45 53 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 NCES clause..**
60e90 43 6f 6e 73 69 64 65 72 20 74 68 69 73 20 65 78 Consider this ex
60ea0 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ample:.**.**
60eb0 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 65 78 CREATE TABLE ex
60ec0 31 28 0a 2a 2a 20 20 20 20 20 20 20 61 20 49 4e 1(.** a IN
60ed0 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE
60ee0 59 2c 0a 2a 2a 20 20 20 20 20 20 20 62 20 49 4e Y,.** b IN
60ef0 54 45 47 45 52 20 43 4f 4e 53 54 52 41 49 4e 54 TEGER CONSTRAINT
60f00 20 66 6b 31 20 52 45 46 45 52 45 4e 43 45 53 20 fk1 REFERENCES
60f10 65 78 32 28 78 29 0a 2a 2a 20 20 20 20 20 29 3b ex2(x).** );
60f20 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66 6f 72 65 69 .**.** For forei
60f30 67 6e 20 6b 65 79 20 22 66 6b 31 22 2c 20 74 68 gn key "fk1", th
60f40 65 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 e from-table is
60f50 22 65 78 31 22 20 61 6e 64 20 74 68 65 20 74 6f "ex1" and the to
60f60 2d 74 61 62 6c 65 20 69 73 20 22 65 78 32 22 2e -table is "ex2".
60f70 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 52 45 46 45 .**.** Each REFE
60f80 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 67 65 RENCES clause ge
60f90 6e 65 72 61 74 65 73 20 61 6e 20 69 6e 73 74 61 nerates an insta
60fa0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
60fb0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a wing structure.*
60fc0 2a 20 77 68 69 63 68 20 69 73 20 61 74 74 61 63 * which is attac
60fd0 68 65 64 20 74 6f 20 74 68 65 20 66 72 6f 6d 2d hed to the from-
60fe0 74 61 62 6c 65 2e 20 20 54 68 65 20 74 6f 2d 74 table. The to-t
60ff0 61 62 6c 65 20 6e 65 65 64 20 6e 6f 74 20 65 78 able need not ex
61000 69 73 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 ist when.** the
61010 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 63 72 from-table is cr
61020 65 61 74 65 64 2e 20 20 54 68 65 20 65 78 69 73 eated. The exis
61030 74 65 6e 63 65 20 6f 66 20 74 68 65 20 74 6f 2d tence of the to-
61040 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 63 68 65 table is not che
61050 63 6b 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 cked..*/.struct
61060 46 4b 65 79 20 7b 0a 20 20 54 61 62 6c 65 20 2a FKey {. Table *
61070 70 46 72 6f 6d 3b 20 20 20 20 20 2f 2a 20 54 61 pFrom; /* Ta
61080 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ble containing t
61090 68 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c he REFERENCES cl
610a0 61 75 73 65 20 28 61 6b 61 3a 20 43 68 69 6c 64 ause (aka: Child
610b0 29 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 ) */. FKey *pNe
610c0 78 74 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 xtFrom; /* Next
610d0 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20 foreign key in
610e0 70 46 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 20 pFrom */. char
610f0 2a 7a 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 *zTo; /*
61100 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 68 Name of table th
61110 61 74 20 74 68 65 20 6b 65 79 20 70 6f 69 6e 74 at the key point
61120 73 20 74 6f 20 28 61 6b 61 3a 20 50 61 72 65 6e s to (aka: Paren
61130 74 29 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e t) */. FKey *pN
61140 65 78 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65 78 extTo; /* Nex
61150 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e t foreign key on
61160 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 6f table named zTo
61170 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 50 72 65 */. FKey *pPre
61180 76 54 6f 3b 20 20 20 20 2f 2a 20 50 72 65 76 69 vTo; /* Previ
61190 6f 75 73 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 ous foreign key
611a0 6f 6e 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a on table named z
611b0 54 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c To */. int nCol
611c0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d ; /* Num
611d0 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
611e0 6e 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20 n this key */.
611f0 2f 2a 20 45 56 3a 20 52 2d 33 30 33 32 33 2d 32 /* EV: R-30323-2
61200 31 39 31 37 20 2a 2f 0a 20 20 75 38 20 69 73 44 1917 */. u8 isD
61210 65 66 65 72 72 65 64 3b 20 20 20 20 2f 2a 20 54 eferred; /* T
61220 72 75 65 20 69 66 20 63 6f 6e 73 74 72 61 69 6e rue if constrain
61230 74 20 63 68 65 63 6b 69 6e 67 20 69 73 20 64 65 t checking is de
61240 66 65 72 72 65 64 20 74 69 6c 6c 20 43 4f 4d 4d ferred till COMM
61250 49 54 20 2a 2f 0a 20 20 75 38 20 61 41 63 74 69 IT */. u8 aActi
61260 6f 6e 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 on[2];
61270 2f 2a 20 4f 4e 20 44 45 4c 45 54 45 20 61 6e 64 /* ON DELETE and
61280 20 4f 4e 20 55 50 44 41 54 45 20 61 63 74 69 6f ON UPDATE actio
61290 6e 73 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 ns, respectively
612a0 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 61 */. Trigger *a
612b0 70 54 72 69 67 67 65 72 5b 32 5d 3b 20 20 2f 2a pTrigger[2]; /*
612c0 20 54 72 69 67 67 65 72 73 20 66 6f 72 20 61 41 Triggers for aA
612d0 63 74 69 6f 6e 5b 5d 20 61 63 74 69 6f 6e 73 20 ction[] actions
612e0 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 43 6f 6c */. struct sCol
612f0 4d 61 70 20 7b 20 20 2f 2a 20 4d 61 70 70 69 6e Map { /* Mappin
61300 67 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 g of columns in
61310 70 46 72 6f 6d 20 74 6f 20 63 6f 6c 75 6d 6e 73 pFrom to columns
61320 20 69 6e 20 7a 54 6f 20 2a 2f 0a 20 20 20 20 69 in zTo */. i
61330 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 20 nt iFrom;
61340 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f /* Index of co
61350 6c 75 6d 6e 20 69 6e 20 70 46 72 6f 6d 20 2a 2f lumn in pFrom */
61360 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 3b . char *zCol;
61370 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
61380 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 7a 54 6f of column in zTo
61390 2e 20 20 49 66 20 30 20 75 73 65 20 50 52 49 4d . If 0 use PRIM
613a0 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 7d 20 61 ARY KEY */. } a
613b0 43 6f 6c 5b 31 5d 3b 20 20 20 20 20 20 20 20 2f Col[1]; /
613c0 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 * One entry for
613d0 65 61 63 68 20 6f 66 20 6e 43 6f 6c 20 63 6f 6c each of nCol col
613e0 75 6d 6e 20 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a umn s */.};../*.
613f0 2a 2a 20 53 51 4c 69 74 65 20 73 75 70 70 6f 72 ** SQLite suppor
61400 74 73 20 6d 61 6e 79 20 64 69 66 66 65 72 65 6e ts many differen
61410 74 20 77 61 79 73 20 74 6f 20 72 65 73 6f 6c 76 t ways to resolv
61420 65 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a e a constraint.*
61430 2a 20 65 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 41 * error. ROLLBA
61440 43 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 CK processing me
61450 61 6e 73 20 74 68 61 74 20 61 20 63 6f 6e 73 74 ans that a const
61460 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 0a raint violation.
61470 2a 2a 20 63 61 75 73 65 73 20 74 68 65 20 6f 70 ** causes the op
61480 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 eration in proce
61490 73 73 20 74 6f 20 66 61 69 6c 20 61 6e 64 20 66 ss to fail and f
614a0 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 or the current t
614b0 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f ransaction.** to
614c0 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e be rolled back.
614d0 20 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 ABORT processi
614e0 6e 67 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 ng means the ope
614f0 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 ration in proces
61500 73 0a 2a 2a 20 66 61 69 6c 73 20 61 6e 64 20 61 s.** fails and a
61510 6e 79 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73 ny prior changes
61520 20 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 6f from that one o
61530 70 65 72 61 74 69 6f 6e 20 61 72 65 20 62 61 63 peration are bac
61540 6b 65 64 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 20 ked out,.** but
61550 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
61560 69 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 is not rolled ba
61570 63 6b 2e 20 20 46 41 49 4c 20 70 72 6f 63 65 73 ck. FAIL proces
61580 73 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 0a sing means that.
61590 2a 2a 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e ** the operation
615a0 20 69 6e 20 70 72 6f 67 72 65 73 73 20 73 74 6f in progress sto
615b0 70 73 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 ps and returns a
615c0 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 42 n error code. B
615d0 75 74 20 70 72 69 6f 72 0a 2a 2a 20 63 68 61 6e ut prior.** chan
615e0 67 65 73 20 64 75 65 20 74 6f 20 74 68 65 20 73 ges due to the s
615f0 61 6d 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 ame operation ar
61600 65 20 6e 6f 74 20 62 61 63 6b 65 64 20 6f 75 74 e not backed out
61610 20 61 6e 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b and no rollback
61620 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 49 47 4e .** occurs. IGN
61630 4f 52 45 20 6d 65 61 6e 73 20 74 68 61 74 20 74 ORE means that t
61640 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 72 6f he particular ro
61650 77 20 74 68 61 74 20 63 61 75 73 65 64 20 74 68 w that caused th
61660 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 e constraint.**
61670 65 72 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e 73 error is not ins
61680 65 72 74 65 64 20 6f 72 20 75 70 64 61 74 65 64 erted or updated
61690 2e 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f . Processing co
616a0 6e 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 ntinues and no e
616b0 72 72 6f 72 0a 2a 2a 20 69 73 20 72 65 74 75 72 rror.** is retur
616c0 6e 65 64 2e 20 20 52 45 50 4c 41 43 45 20 6d 65 ned. REPLACE me
616d0 61 6e 73 20 74 68 61 74 20 70 72 65 65 78 69 73 ans that preexis
616e0 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 72 6f ting database ro
616f0 77 73 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a ws that caused.*
61700 2a 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 * a UNIQUE const
61710 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 raint violation
61720 61 72 65 20 72 65 6d 6f 76 65 64 20 73 6f 20 74 are removed so t
61730 68 61 74 20 74 68 65 20 6e 65 77 20 69 6e 73 65 hat the new inse
61740 72 74 20 6f 72 0a 2a 2a 20 75 70 64 61 74 65 20 rt or.** update
61750 63 61 6e 20 70 72 6f 63 65 65 64 2e 20 20 50 72 can proceed. Pr
61760 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75 ocessing continu
61770 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 20 es and no error
61780 69 73 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a is reported..**.
61790 2a 2a 20 52 45 53 54 52 49 43 54 2c 20 53 45 54 ** RESTRICT, SET
617a0 4e 55 4c 4c 2c 20 61 6e 64 20 43 41 53 43 41 44 NULL, and CASCAD
617b0 45 20 61 63 74 69 6f 6e 73 20 61 70 70 6c 79 20 E actions apply
617c0 6f 6e 6c 79 20 74 6f 20 66 6f 72 65 69 67 6e 20 only to foreign
617d0 6b 65 79 73 2e 0a 2a 2a 20 52 45 53 54 52 49 43 keys..** RESTRIC
617e0 54 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 T is the same as
617f0 20 41 42 4f 52 54 20 66 6f 72 20 49 4d 4d 45 44 ABORT for IMMED
61800 49 41 54 45 20 66 6f 72 65 69 67 6e 20 6b 65 79 IATE foreign key
61810 73 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 61 6d s and the.** sam
61820 65 20 61 73 20 52 4f 4c 4c 42 41 43 4b 20 66 6f e as ROLLBACK fo
61830 72 20 44 45 46 45 52 52 45 44 20 6b 65 79 73 2e r DEFERRED keys.
61840 20 20 53 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 20 SETNULL means
61850 74 68 61 74 20 74 68 65 20 66 6f 72 65 69 67 6e that the foreign
61860 0a 2a 2a 20 6b 65 79 20 69 73 20 73 65 74 20 74 .** key is set t
61870 6f 20 4e 55 4c 4c 2e 20 20 43 41 53 43 41 44 45 o NULL. CASCADE
61880 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 44 45 means that a DE
61890 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 6f LETE or UPDATE o
618a0 66 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e f the.** referen
618b0 63 65 64 20 74 61 62 6c 65 20 72 6f 77 20 69 73 ced table row is
618c0 20 70 72 6f 70 61 67 61 74 65 64 20 69 6e 74 6f propagated into
618d0 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 68 6f the row that ho
618e0 6c 64 73 20 74 68 65 0a 2a 2a 20 66 6f 72 65 69 lds the.** forei
618f0 67 6e 20 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 gn key..** .** T
61900 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d he following sym
61910 62 6f 6c 69 63 20 76 61 6c 75 65 73 20 61 72 65 bolic values are
61920 20 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 used to record
61930 77 68 69 63 68 20 74 79 70 65 0a 2a 2a 20 6f 66 which type.** of
61940 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 2e action to take.
61950 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 4e .*/.#define OE_N
61960 6f 6e 65 20 20 20 20 20 30 20 20 20 2f 2a 20 54 one 0 /* T
61970 68 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 74 here is no const
61980 72 61 69 6e 74 20 74 6f 20 63 68 65 63 6b 20 2a raint to check *
61990 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f 6c /.#define OE_Rol
619a0 6c 62 61 63 6b 20 31 20 20 20 2f 2a 20 46 61 69 lback 1 /* Fai
619b0 6c 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 l the operation
619c0 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 and rollback the
619d0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a transaction */.
619e0 23 64 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72 74 #define OE_Abort
619f0 20 20 20 20 32 20 20 20 2f 2a 20 42 61 63 6b 20 2 /* Back
61a00 6f 75 74 20 63 68 61 6e 67 65 73 20 62 75 74 20 out changes but
61a10 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 do no rollback t
61a20 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 ransaction */.#d
61a30 65 66 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20 20 efine OE_Fail
61a40 20 20 33 20 20 20 2f 2a 20 53 74 6f 70 20 74 68 3 /* Stop th
61a50 65 20 6f 70 65 72 61 74 69 6f 6e 20 62 75 74 20 e operation but
61a60 6c 65 61 76 65 20 61 6c 6c 20 70 72 69 6f 72 20 leave all prior
61a70 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 changes */.#defi
61a80 6e 65 20 4f 45 5f 49 67 6e 6f 72 65 20 20 20 34 ne OE_Ignore 4
61a90 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 /* Ignore the
61aa0 20 65 72 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 64 error. Do not d
61ab0 6f 20 74 68 65 20 49 4e 53 45 52 54 20 6f 72 20 o the INSERT or
61ac0 55 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e UPDATE */.#defin
61ad0 65 20 4f 45 5f 52 65 70 6c 61 63 65 20 20 35 20 e OE_Replace 5
61ae0 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 78 69 73 /* Delete exis
61af0 74 69 6e 67 20 72 65 63 6f 72 64 2c 20 74 68 65 ting record, the
61b00 6e 20 64 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 n do INSERT or U
61b10 50 44 41 54 45 20 2a 2f 0a 0a 23 64 65 66 69 6e PDATE */..#defin
61b20 65 20 4f 45 5f 52 65 73 74 72 69 63 74 20 36 20 e OE_Restrict 6
61b30 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 20 66 6f /* OE_Abort fo
61b40 72 20 49 4d 4d 45 44 49 41 54 45 2c 20 4f 45 5f r IMMEDIATE, OE_
61b50 52 6f 6c 6c 62 61 63 6b 20 66 6f 72 20 44 45 46 Rollback for DEF
61b60 45 52 52 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 ERRED */.#define
61b70 20 4f 45 5f 53 65 74 4e 75 6c 6c 20 20 37 20 20 OE_SetNull 7
61b80 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 /* Set the fore
61b90 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f ign key value to
61ba0 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 NULL */.#define
61bb0 20 4f 45 5f 53 65 74 44 66 6c 74 20 20 38 20 20 OE_SetDflt 8
61bc0 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 /* Set the fore
61bd0 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f ign key value to
61be0 20 69 74 73 20 64 65 66 61 75 6c 74 20 2a 2f 0a its default */.
61bf0 23 64 65 66 69 6e 65 20 4f 45 5f 43 61 73 63 61 #define OE_Casca
61c00 64 65 20 20 39 20 20 20 2f 2a 20 43 61 73 63 61 de 9 /* Casca
61c10 64 65 20 74 68 65 20 63 68 61 6e 67 65 73 20 2a de the changes *
61c20 2f 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f 44 65 /..#define OE_De
61c30 66 61 75 6c 74 20 20 39 39 20 20 2f 2a 20 44 6f fault 99 /* Do
61c40 20 77 68 61 74 65 76 65 72 20 74 68 65 20 64 65 whatever the de
61c50 66 61 75 6c 74 20 61 63 74 69 6f 6e 20 69 73 20 fault action is
61c60 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e */.../*.** An in
61c70 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
61c80 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
61c90 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 e is passed as t
61ca0 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 he first.** argu
61cb0 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 56 ment to sqlite3V
61cc0 64 62 65 4b 65 79 43 6f 6d 70 61 72 65 20 61 6e dbeKeyCompare an
61cd0 64 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e d is used to con
61ce0 74 72 6f 6c 20 74 68 65 20 0a 2a 2a 20 63 6f 6d trol the .** com
61cf0 70 61 72 69 73 6f 6e 20 6f 66 20 74 68 65 20 74 parison of the t
61d00 77 6f 20 69 6e 64 65 78 20 6b 65 79 73 2e 0a 2a wo index keys..*
61d10 2f 0a 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f /.struct KeyInfo
61d20 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 {. sqlite3 *db
61d30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ; /* The
61d40 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
61d50 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b ion */. u8 enc;
61d60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
61d70 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2d 20 Text encoding -
61d80 6f 6e 65 20 6f 66 20 74 68 65 20 54 45 58 54 5f one of the TEXT_
61d90 55 74 66 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 Utf* values */.
61da0 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 u16 nField;
61db0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
61dc0 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f f entries in aCo
61dd0 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53 ll[] */. u8 *aS
61de0 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 20 2f 2a ortOrder; /*
61df0 20 49 66 20 64 65 66 69 6e 65 64 20 61 6e 20 61 If defined an a
61e00 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 69 73 20 SortOrder[i] is
61e10 74 72 75 65 2c 20 73 6f 72 74 20 44 45 53 43 20 true, sort DESC
61e20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 */. CollSeq *aC
61e30 6f 6c 6c 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c 6c oll[1]; /* Coll
61e40 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 ating sequence f
61e50 6f 72 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 or each term of
61e60 74 68 65 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f the key */.};../
61e70 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
61e80 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
61e90 67 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 g structure hold
61ea0 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 s information ab
61eb0 6f 75 74 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 out a.** single
61ec0 69 6e 64 65 78 20 72 65 63 6f 72 64 20 74 68 61 index record tha
61ed0 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 t has already be
61ee0 65 6e 20 70 61 72 73 65 64 20 6f 75 74 20 69 6e en parsed out in
61ef0 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a to individual.**
61f00 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 values..**.** A
61f10 20 72 65 63 6f 72 64 20 69 73 20 61 6e 20 6f 62 record is an ob
61f20 6a 65 63 74 20 74 68 61 74 20 63 6f 6e 74 61 69 ject that contai
61f30 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66 ns one or more f
61f40 69 65 6c 64 73 20 6f 66 20 64 61 74 61 2e 0a 2a ields of data..*
61f50 2a 20 52 65 63 6f 72 64 73 20 61 72 65 20 75 73 * Records are us
61f60 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 ed to store the
61f70 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 74 61 62 content of a tab
61f80 6c 65 20 72 6f 77 20 61 6e 64 20 74 6f 20 73 74 le row and to st
61f90 6f 72 65 0a 2a 2a 20 74 68 65 20 6b 65 79 20 6f ore.** the key o
61fa0 66 20 61 6e 20 69 6e 64 65 78 2e 20 20 41 20 62 f an index. A b
61fb0 6c 6f 62 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 lob encoding of
61fc0 61 20 72 65 63 6f 72 64 20 69 73 20 63 72 65 61 a record is crea
61fd0 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 4f 50 ted by.** the OP
61fe0 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f _MakeRecord opco
61ff0 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 20 61 de of the VDBE a
62000 6e 64 20 69 73 20 64 69 73 61 73 73 65 6d 62 6c nd is disassembl
62010 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50 5f ed by the.** OP_
62020 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a Column opcode..*
62030 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 *.** This struct
62040 75 72 65 20 68 6f 6c 64 73 20 61 20 72 65 63 6f ure holds a reco
62050 72 64 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 rd that has alre
62060 61 64 79 20 62 65 65 6e 20 64 69 73 61 73 73 65 ady been disasse
62070 6d 62 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69 74 mbled.** into it
62080 73 20 63 6f 6e 73 74 69 74 75 65 6e 74 20 66 69 s constituent fi
62090 65 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 elds..*/.struct
620a0 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 7b UnpackedRecord {
620b0 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 . KeyInfo *pKey
620c0 49 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 Info; /* Collat
620d0 69 6f 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72 64 ion and sort-ord
620e0 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a er information *
620f0 2f 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 /. u16 nField;
62100 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
62110 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 r of entries in
62120 61 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31 36 apMem[] */. u16
62130 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 flags;
62140 20 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74 74 /* Boolean sett
62150 69 6e 67 73 2e 20 20 55 4e 50 41 43 4b 45 44 5f ings. UNPACKED_
62160 2e 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 ... below */. i
62170 36 34 20 72 6f 77 69 64 3b 20 20 20 20 20 20 20 64 rowid;
62180 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 55 4e /* Used by UN
62190 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 PACKED_PREFIX_SE
621a0 41 52 43 48 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 ARCH */. Mem *a
621b0 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a Mem; /*
621c0 20 56 61 6c 75 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f Values */.};../
621d0 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c *.** Allowed val
621e0 75 65 73 20 6f 66 20 55 6e 70 61 63 6b 65 64 52 ues of UnpackedR
621f0 65 63 6f 72 64 2e 66 6c 61 67 73 0a 2a 2f 0a 23 ecord.flags.*/.#
62200 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f define UNPACKED_
62210 4e 45 45 44 5f 46 52 45 45 20 20 20 20 20 30 78 NEED_FREE 0x
62220 30 30 30 31 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 0001 /* Memory
62230 69 73 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d is from sqlite3M
62240 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 alloc() */.#defi
62250 6e 65 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 ne UNPACKED_NEED
62260 5f 44 45 53 54 52 4f 59 20 20 30 78 30 30 30 32 _DESTROY 0x0002
62270 20 20 2f 2a 20 61 70 4d 65 6d 5b 5d 73 20 73 68 /* apMem[]s sh
62280 6f 75 6c 64 20 61 6c 6c 20 62 65 20 64 65 73 74 ould all be dest
62290 72 6f 79 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 royed */.#define
622a0 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 UNPACKED_IGNORE
622b0 5f 52 4f 57 49 44 20 20 30 78 30 30 30 34 20 20 _ROWID 0x0004
622c0 2f 2a 20 49 67 6e 6f 72 65 20 74 72 61 69 6c 69 /* Ignore traili
622d0 6e 67 20 72 6f 77 69 64 20 6f 6e 20 6b 65 79 31 ng rowid on key1
622e0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 */.#define UNPA
622f0 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 20 20 20 CKED_INCRKEY
62300 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 4d 61 0x0008 /* Ma
62310 6b 65 20 74 68 69 73 20 6b 65 79 20 61 6e 20 65 ke this key an e
62320 70 73 69 6c 6f 6e 20 6c 61 72 67 65 72 20 2a 2f psilon larger */
62330 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 .#define UNPACKE
62340 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20 20 D_PREFIX_MATCH
62350 30 78 30 30 31 30 20 20 2f 2a 20 41 20 70 72 65 0x0010 /* A pre
62360 66 69 78 20 6d 61 74 63 68 20 69 73 20 63 6f 6e fix match is con
62370 73 69 64 65 72 65 64 20 4f 4b 20 2a 2f 0a 23 64 sidered OK */.#d
62380 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 50 efine UNPACKED_P
62390 52 45 46 49 58 5f 53 45 41 52 43 48 20 30 78 30 REFIX_SEARCH 0x0
623a0 30 32 30 20 20 2f 2a 20 41 20 70 72 65 66 69 78 020 /* A prefix
623b0 20 6d 61 74 63 68 20 69 73 20 63 6f 6e 73 69 64 match is consid
623c0 65 72 65 64 20 4f 4b 20 2a 2f 0a 0a 2f 2a 0a 2a ered OK */../*.*
623d0 2a 20 45 61 63 68 20 53 51 4c 20 69 6e 64 65 78 * Each SQL index
623e0 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 is represented
623f0 69 6e 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e 0a in memory by an.
62400 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 ** instance of t
62410 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
62420 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 ucture..**.** Th
62430 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 e columns of the
62440 20 74 61 62 6c 65 20 74 68 61 74 20 61 72 65 20 table that are
62450 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 61 72 to be indexed ar
62460 65 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 62 e described.** b
62470 79 20 74 68 65 20 61 69 43 6f 6c 75 6d 6e 5b 5d y the aiColumn[]
62480 20 66 69 65 6c 64 20 6f 66 20 74 68 69 73 20 73 field of this s
62490 74 72 75 63 74 75 72 65 2e 20 20 46 6f 72 20 65 tructure. For e
624a0 78 61 6d 70 6c 65 2c 20 73 75 70 70 6f 73 65 0a xample, suppose.
624b0 2a 2a 20 77 65 20 68 61 76 65 20 74 68 65 20 66 ** we have the f
624c0 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 61 ollowing table a
624d0 6e 64 20 69 6e 64 65 78 3a 0a 2a 2a 0a 2a 2a 20 nd index:.**.**
624e0 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 CREATE TABLE
624f0 20 45 78 31 28 63 31 20 69 6e 74 2c 20 63 32 20 Ex1(c1 int, c2
62500 69 6e 74 2c 20 63 33 20 74 65 78 74 29 3b 0a 2a int, c3 text);.*
62510 2a 20 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 * CREATE IND
62520 45 58 20 45 78 32 20 4f 4e 20 45 78 31 28 63 33 EX Ex2 ON Ex1(c3
62530 2c 63 31 29 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 ,c1);.**.** In t
62540 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 he Table structu
62550 72 65 20 64 65 73 63 72 69 62 69 6e 67 20 45 78 re describing Ex
62560 31 2c 20 6e 43 6f 6c 3d 3d 33 20 62 65 63 61 75 1, nCol==3 becau
62570 73 65 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20 se there are.**
62580 74 68 72 65 65 20 63 6f 6c 75 6d 6e 73 20 69 6e three columns in
62590 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49 6e 20 the table. In
625a0 74 68 65 20 49 6e 64 65 78 20 73 74 72 75 63 74 the Index struct
625b0 75 72 65 20 64 65 73 63 72 69 62 69 6e 67 0a 2a ure describing.*
625c0 2a 20 45 78 32 2c 20 6e 43 6f 6c 75 6d 6e 3d 3d * Ex2, nColumn==
625d0 32 20 73 69 6e 63 65 20 32 20 6f 66 20 74 68 65 2 since 2 of the
625e0 20 33 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 45 78 3 columns of Ex
625f0 31 20 61 72 65 20 69 6e 64 65 78 65 64 2e 0a 2a 1 are indexed..*
62600 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 61 * The value of a
62610 69 43 6f 6c 75 6d 6e 20 69 73 20 7b 32 2c 20 30 iColumn is {2, 0
62620 7d 2e 20 20 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d }. aiColumn[0]=
62630 3d 32 20 62 65 63 61 75 73 65 20 74 68 65 20 0a =2 because the .
62640 2a 2a 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 ** first column
62650 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28 63 to be indexed (c
62660 33 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 3) has an index
62670 6f 66 20 32 20 69 6e 20 45 78 31 2e 61 43 6f 6c of 2 in Ex1.aCol
62680 5b 5d 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e []..** The secon
62690 64 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 69 d column to be i
626a0 6e 64 65 78 65 64 20 28 63 31 29 20 68 61 73 20 ndexed (c1) has
626b0 61 6e 20 69 6e 64 65 78 20 6f 66 20 30 20 69 6e an index of 0 in
626c0 0a 2a 2a 20 45 78 31 2e 61 43 6f 6c 5b 5d 2c 20 .** Ex1.aCol[],
626d0 68 65 6e 63 65 20 45 78 32 2e 61 69 43 6f 6c 75 hence Ex2.aiColu
626e0 6d 6e 5b 31 5d 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 mn[1]==0..**.**
626f0 54 68 65 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f The Index.onErro
62700 72 20 66 69 65 6c 64 20 64 65 74 65 72 6d 69 6e r field determin
62710 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f es whether or no
62720 74 20 74 68 65 20 69 6e 64 65 78 65 64 20 63 6f t the indexed co
62730 6c 75 6d 6e 73 0a 2a 2a 20 6d 75 73 74 20 62 65 lumns.** must be
62740 20 75 6e 69 71 75 65 20 61 6e 64 20 77 68 61 74 unique and what
62750 20 74 6f 20 64 6f 20 69 66 20 74 68 65 79 20 61 to do if they a
62760 72 65 20 6e 6f 74 2e 20 20 57 68 65 6e 20 49 6e re not. When In
62770 64 65 78 2e 6f 6e 45 72 72 6f 72 3d 4f 45 5f 4e dex.onError=OE_N
62780 6f 6e 65 2c 0a 2a 2a 20 69 74 20 6d 65 61 6e 73 one,.** it means
62790 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20 75 this is not a u
627a0 6e 69 71 75 65 20 69 6e 64 65 78 2e 20 20 4f 74 nique index. Ot
627b0 68 65 72 77 69 73 65 20 69 74 20 69 73 20 61 20 herwise it is a
627c0 75 6e 69 71 75 65 20 69 6e 64 65 78 0a 2a 2a 20 unique index.**
627d0 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 and the value of
627e0 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20 69 Index.onError i
627f0 6e 64 69 63 61 74 65 20 74 68 65 20 77 68 69 63 ndicate the whic
62800 68 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c h conflict resol
62810 75 74 69 6f 6e 20 0a 2a 2a 20 61 6c 67 6f 72 69 ution .** algori
62820 74 68 6d 20 74 6f 20 65 6d 70 6c 6f 79 20 77 68 thm to employ wh
62830 65 6e 65 76 65 72 20 61 6e 20 61 74 74 65 6d 70 enever an attemp
62840 74 20 69 73 20 6d 61 64 65 20 74 6f 20 69 6e 73 t is made to ins
62850 65 72 74 20 61 20 6e 6f 6e 2d 75 6e 69 71 75 65 ert a non-unique
62860 0a 2a 2a 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a .** element..*/.
62870 73 74 72 75 63 74 20 49 6e 64 65 78 20 7b 0a 20 struct Index {.
62880 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 char *zName;
62890 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 /* Name of thi
628a0 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 s index */. int
628b0 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f 2a nColumn; /*
628c0 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d Number of colum
628d0 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 ns in the table
628e0 75 73 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 used by this ind
628f0 65 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43 ex */. int *aiC
62900 6f 6c 75 6d 6e 3b 20 20 20 2f 2a 20 57 68 69 63 olumn; /* Whic
62910 68 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75 73 h columns are us
62920 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 78 ed by this index
62930 2e 20 20 31 73 74 20 69 73 20 30 20 2a 2f 0a 20 . 1st is 0 */.
62940 20 75 6e 73 69 67 6e 65 64 20 2a 61 69 52 6f 77 unsigned *aiRow
62950 45 73 74 3b 20 2f 2a 20 52 65 73 75 6c 74 20 6f Est; /* Result o
62960 66 20 41 4e 41 4c 59 5a 45 3a 20 45 73 74 2e 20 f ANALYZE: Est.
62970 72 6f 77 73 20 73 65 6c 65 63 74 65 64 20 62 79 rows selected by
62980 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a each column */.
62990 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b Table *pTable;
629a0 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 20 74 61 /* The SQL ta
629b0 62 6c 65 20 62 65 69 6e 67 20 69 6e 64 65 78 65 ble being indexe
629c0 64 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b d */. int tnum;
629d0 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
629e0 63 6f 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74 20 containing root
629f0 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 69 6e of this index in
62a00 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
62a10 2f 0a 20 20 75 38 20 6f 6e 45 72 72 6f 72 3b 20 /. u8 onError;
62a20 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 /* OE_Abort
62a30 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45 5f , OE_Ignore, OE_
62a40 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f 4e Replace, or OE_N
62a50 6f 6e 65 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f one */. u8 auto
62a60 49 6e 64 65 78 3b 20 20 20 20 2f 2a 20 54 72 75 Index; /* Tru
62a70 65 20 69 66 20 69 73 20 61 75 74 6f 6d 61 74 69 e if is automati
62a80 63 61 6c 6c 79 20 63 72 65 61 74 65 64 20 28 65 cally created (e
62a90 78 3a 20 62 79 20 55 4e 49 51 55 45 29 20 2a 2f x: by UNIQUE) */
62aa0 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66 . char *zColAff
62ab0 3b 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 64 65 ; /* String de
62ac0 66 69 6e 69 6e 67 20 74 68 65 20 61 66 66 69 6e fining the affin
62ad0 69 74 79 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 ity of each colu
62ae0 6d 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 mn */. Index *p
62af0 4e 65 78 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 Next; /* The
62b00 6e 65 78 74 20 69 6e 64 65 78 20 61 73 73 6f 63 next index assoc
62b10 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 iated with the s
62b20 61 6d 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 53 ame table */. S
62b30 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 chema *pSchema;
62b40 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 /* Schema contai
62b50 6e 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20 ning this index
62b60 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72 74 4f 72 */. u8 *aSortOr
62b70 64 65 72 3b 20 20 2f 2a 20 41 72 72 61 79 20 6f der; /* Array o
62b80 66 20 73 69 7a 65 20 49 6e 64 65 78 2e 6e 43 6f f size Index.nCo
62b90 6c 75 6d 6e 2e 20 54 72 75 65 3d 3d 44 45 53 43 lumn. True==DESC
62ba0 2c 20 46 61 6c 73 65 3d 3d 41 53 43 20 2a 2f 0a , False==ASC */.
62bb0 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 6c 3b char **azColl;
62bc0 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 63 /* Array of c
62bd0 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
62be0 65 20 6e 61 6d 65 73 20 66 6f 72 20 69 6e 64 65 e names for inde
62bf0 78 20 2a 2f 0a 20 20 49 6e 64 65 78 53 61 6d 70 x */. IndexSamp
62c00 6c 65 20 2a 61 53 61 6d 70 6c 65 3b 20 20 20 20 le *aSample;
62c10 2f 2a 20 41 72 72 61 79 20 6f 66 20 53 51 4c 49 /* Array of SQLI
62c20 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 TE_INDEX_SAMPLES
62c30 20 73 61 6d 70 6c 65 73 20 2a 2f 0a 7d 3b 0a 0a samples */.};..
62c40 2f 2a 0a 2a 2a 20 45 61 63 68 20 73 61 6d 70 6c /*.** Each sampl
62c50 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 e stored in the
62c60 73 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61 62 sqlite_stat2 tab
62c70 6c 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 le is represente
62c80 64 20 69 6e 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 d in memory .**
62c90 75 73 69 6e 67 20 61 20 73 74 72 75 63 74 75 72 using a structur
62ca0 65 20 6f 66 20 74 68 69 73 20 74 79 70 65 2e 0a e of this type..
62cb0 2a 2f 0a 73 74 72 75 63 74 20 49 6e 64 65 78 53 */.struct IndexS
62cc0 61 6d 70 6c 65 20 7b 0a 20 20 75 6e 69 6f 6e 20 ample {. union
62cd0 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 20 20 {. char *z;
62ce0 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 /* Value i
62cf0 66 20 65 54 79 70 65 20 69 73 20 53 51 4c 49 54 f eType is SQLIT
62d00 45 5f 54 45 58 54 20 6f 72 20 53 51 4c 49 54 45 E_TEXT or SQLITE
62d10 5f 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 64 6f 75 _BLOB */. dou
62d20 62 6c 65 20 72 3b 20 20 20 20 20 20 20 2f 2a 20 ble r; /*
62d30 56 61 6c 75 65 20 69 66 20 65 54 79 70 65 20 69 Value if eType i
62d40 73 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 6f s SQLITE_FLOAT o
62d50 72 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 r SQLITE_INTEGER
62d60 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 75 38 20 */. } u;. u8
62d70 65 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 2f eType; /
62d80 2a 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 53 * SQLITE_NULL, S
62d90 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 2e 2e QLITE_INTEGER ..
62da0 2e 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 6e . etc. */. u8 n
62db0 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 2f 2a Byte; /*
62dc0 20 53 69 7a 65 20 69 6e 20 62 79 74 65 20 6f 66 Size in byte of
62dd0 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2e 20 2a text or blob. *
62de0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 /.};../*.** Each
62df0 20 74 6f 6b 65 6e 20 63 6f 6d 69 6e 67 20 6f 75 token coming ou
62e00 74 20 6f 66 20 74 68 65 20 6c 65 78 65 72 20 69 t of the lexer i
62e10 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 s an instance of
62e20 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 .** this structu
62e30 72 65 2e 20 20 54 6f 6b 65 6e 73 20 61 72 65 20 re. Tokens are
62e40 61 6c 73 6f 20 75 73 65 64 20 61 73 20 70 61 72 also used as par
62e50 74 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 t of an expressi
62e60 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 69 on..**.** Note i
62e70 66 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 20 74 68 65 f Token.z==0 the
62e80 6e 20 54 6f 6b 65 6e 2e 64 79 6e 20 61 6e 64 20 n Token.dyn and
62e90 54 6f 6b 65 6e 2e 6e 20 61 72 65 20 75 6e 64 65 Token.n are unde
62ea0 66 69 6e 65 64 20 61 6e 64 0a 2a 2a 20 6d 61 79 fined and.** may
62eb0 20 63 6f 6e 74 61 69 6e 20 72 61 6e 64 6f 6d 20 contain random
62ec0 76 61 6c 75 65 73 2e 20 20 44 6f 20 6e 6f 74 20 values. Do not
62ed0 6d 61 6b 65 20 61 6e 79 20 61 73 73 75 6d 70 74 make any assumpt
62ee0 69 6f 6e 73 20 61 62 6f 75 74 20 54 6f 6b 65 6e ions about Token
62ef0 2e 64 79 6e 0a 2a 2a 20 61 6e 64 20 54 6f 6b 65 .dyn.** and Toke
62f00 6e 2e 6e 20 77 68 65 6e 20 54 6f 6b 65 6e 2e 7a n.n when Token.z
62f10 3d 3d 30 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 ==0..*/.struct T
62f20 6f 6b 65 6e 20 7b 0a 20 20 63 6f 6e 73 74 20 63 oken {. const c
62f30 68 61 72 20 2a 7a 3b 20 20 20 20 20 2f 2a 20 54 har *z; /* T
62f40 65 78 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e ext of the token
62f50 2e 20 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d . Not NULL-term
62f60 69 6e 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73 inated! */. uns
62f70 69 67 6e 65 64 20 69 6e 74 20 6e 3b 20 20 20 20 igned int n;
62f80 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 /* Number of cha
62f90 72 61 63 74 65 72 73 20 69 6e 20 74 68 69 73 20 racters in this
62fa0 74 6f 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a token */.};../*.
62fb0 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
62fc0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
62fd0 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d contains inform
62fe0 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 ation needed to
62ff0 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 generate.** code
63000 20 66 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68 for a SELECT th
63010 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 at contains aggr
63020 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e egate functions.
63030 0a 2a 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f .**.** If Expr.o
63040 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e p==TK_AGG_COLUMN
63050 20 6f 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 or TK_AGG_FUNCT
63060 49 4f 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41 ION then Expr.pA
63070 67 67 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70 ggInfo is a.** p
63080 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 ointer to this s
63090 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 45 tructure. The E
630a0 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c xpr.iColumn fiel
630b0 64 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 69 d is the index i
630c0 6e 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f n.** AggInfo.aCo
630d0 6c 5b 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61 l[] or AggInfo.a
630e0 46 75 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d Func[] of inform
630f0 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 ation needed to
63100 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 generate.** code
63110 20 66 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a for that node..
63120 2a 2a 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47 **.** AggInfo.pG
63130 72 6f 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e roupBy and AggIn
63140 66 6f 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70 fo.aFunc.pExpr p
63150 6f 69 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77 oint to fields w
63160 69 74 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 ithin the.** ori
63170 67 69 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72 ginal Select str
63180 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 ucture that desc
63190 72 69 62 65 73 20 74 68 65 20 53 45 4c 45 43 54 ribes the SELECT
631a0 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 statement. The
631b0 73 65 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20 se.** fields do
631c0 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66 not need to be f
631d0 72 65 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f reed when deallo
631e0 63 61 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e cating the AggIn
631f0 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f fo structure..*/
63200 0a 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 .struct AggInfo
63210 7b 0a 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64 {. u8 directMod
63220 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 e; /* D
63230 69 72 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20 irect rendering
63240 6d 6f 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20 mode means take
63250 64 61 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20 data directly.
63260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63270 20 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 ** from
63280 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20 72 61 source tables ra
63290 74 68 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61 ther than from a
632a0 63 63 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 ccumulators */.
632b0 20 75 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64 u8 useSortingId
632c0 78 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 x; /* In d
632d0 69 72 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65 irect mode, refe
632e0 72 65 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e rence the sortin
632f0 67 20 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20 g index rather.
63300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63310 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e ** than
63320 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c the source tabl
63330 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69 e */. int sorti
63340 6e 67 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f ngIdx; /
63350 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 * Cursor number
63360 6f 66 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 of the sorting i
63370 6e 64 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69 ndex */. ExprLi
63380 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 st *pGroupBy;
63390 20 20 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62 /* The group b
633a0 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e y clause */. in
633b0 74 20 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e t nSortingColumn
633c0 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 ; /* Number
633d0 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 of columns in th
633e0 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 e sorting index
633f0 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 */. struct AggI
63400 6e 66 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 nfo_col { /*
63410 46 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 For each column
63420 75 73 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74 used in source t
63430 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62 ables */. Tab
63440 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 le *pTab;
63450 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 /* Source
63460 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 table */. int
63470 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 iTable;
63480 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 /* Cursor
63490 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f number of the so
634a0 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 urce table */.
634b0 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 int iColumn;
634c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
634d0 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 lumn number with
634e0 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 in the source ta
634f0 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 ble */. int i
63500 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 SorterColumn;
63510 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 /* Column nu
63520 6d 62 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74 mber in the sort
63530 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 ing index */.
63540 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 int iMem;
63550 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d /* Mem
63560 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 ory location tha
63570 74 20 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 t acts as accumu
63580 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 lator */. Exp
63590 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 r *pExpr;
635a0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 /* The ori
635b0 67 69 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e ginal expression
635c0 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 */. } *aCol;.
635d0 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 int nColumn;
635e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
635f0 65 72 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69 er of used entri
63600 65 73 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a es in aCol[] */.
63610 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c int nColumnAll
63620 6f 63 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d oc; /* Num
63630 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c ber of slots all
63640 6f 63 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b ocated for aCol[
63650 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75 ] */. int nAccu
63660 6d 75 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f mulator; /
63670 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 * Number of colu
63680 6d 6e 73 20 74 68 61 74 20 73 68 6f 77 20 74 68 mns that show th
63690 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74 rough to the out
636a0 70 75 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 put..
636b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
636c0 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c * Additional col
636d0 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e umns are used on
636e0 6c 79 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 ly as parameters
636f0 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 to.
63700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a **
63710 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
63720 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 ions */. struct
63730 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 AggInfo_func {
63740 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67 /* For each ag
63750 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
63760 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 */. Expr *pE
63770 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 xpr;
63780 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65 /* Expression e
63790 6e 63 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63 ncoding the func
637a0 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63 tion */. Func
637b0 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 Def *pFunc;
637c0 20 20 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72 /* The aggr
637d0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 egate function i
637e0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f mplementation */
637f0 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 . int iMem;
63800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
63810 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e Memory location
63820 20 74 68 61 74 20 61 63 74 73 20 61 73 20 61 63 that acts as ac
63830 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 cumulator */.
63840 20 69 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20 int iDistinct;
63850 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68 /* Eph
63860 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65 emeral table use
63870 64 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53 d to enforce DIS
63880 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 TINCT */. } *aF
63890 75 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63 unc;. int nFunc
638a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
638b0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
638c0 69 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a ies in aFunc[] *
638d0 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c /. int nFuncAll
638e0 6f 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e oc; /* N
638f0 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 umber of slots a
63900 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75 llocated for aFu
63910 6e 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a nc[] */.};../*.*
63920 2a 20 45 61 63 68 20 6e 6f 64 65 20 6f 66 20 61 * Each node of a
63930 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 n expression in
63940 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20 69 the parse tree i
63950 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a s an instance.**
63960 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
63970 72 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f re..**.** Expr.o
63980 70 20 69 73 20 74 68 65 20 6f 70 63 6f 64 65 2e p is the opcode.
63990 20 54 68 65 20 69 6e 74 65 67 65 72 20 70 61 72 The integer par
639a0 73 65 72 20 74 6f 6b 65 6e 20 63 6f 64 65 73 20 ser token codes
639b0 61 72 65 20 72 65 75 73 65 64 0a 2a 2a 20 61 73 are reused.** as
639c0 20 6f 70 63 6f 64 65 73 20 68 65 72 65 2e 20 46 opcodes here. F
639d0 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 or example, the
639e0 70 61 72 73 65 72 20 64 65 66 69 6e 65 73 20 54 parser defines T
639f0 4b 5f 47 45 20 74 6f 20 62 65 20 61 6e 20 69 6e K_GE to be an in
63a00 74 65 67 65 72 0a 2a 2a 20 63 6f 64 65 20 72 65 teger.** code re
63a10 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 22 presenting the "
63a20 3e 3d 22 20 6f 70 65 72 61 74 6f 72 2e 20 54 68 >=" operator. Th
63a30 69 73 20 73 61 6d 65 20 69 6e 74 65 67 65 72 20 is same integer
63a40 63 6f 64 65 20 69 73 20 72 65 75 73 65 64 0a 2a code is reused.*
63a50 2a 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 74 * to represent t
63a60 68 65 20 67 72 65 61 74 65 72 2d 74 68 61 6e 2d he greater-than-
63a70 6f 72 2d 65 71 75 61 6c 2d 74 6f 20 6f 70 65 72 or-equal-to oper
63a80 61 74 6f 72 20 69 6e 20 74 68 65 20 65 78 70 72 ator in the expr
63a90 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e 0a ession.** tree..
63aa0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 **.** If the exp
63ab0 72 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 ression is an SQ
63ac0 4c 20 6c 69 74 65 72 61 6c 20 28 54 4b 5f 49 4e L literal (TK_IN
63ad0 54 45 47 45 52 2c 20 54 4b 5f 46 4c 4f 41 54 2c TEGER, TK_FLOAT,
63ae0 20 54 4b 5f 42 4c 4f 42 2c 20 0a 2a 2a 20 6f 72 TK_BLOB, .** or
63af0 20 54 4b 5f 53 54 52 49 4e 47 29 2c 20 74 68 65 TK_STRING), the
63b00 6e 20 45 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e n Expr.token con
63b10 74 61 69 6e 73 20 74 68 65 20 74 65 78 74 20 6f tains the text o
63b20 66 20 74 68 65 20 53 51 4c 20 6c 69 74 65 72 61 f the SQL litera
63b30 6c 2e 20 49 66 0a 2a 2a 20 74 68 65 20 65 78 70 l. If.** the exp
63b40 72 65 73 73 69 6f 6e 20 69 73 20 61 20 76 61 72 ression is a var
63b50 69 61 62 6c 65 20 28 54 4b 5f 56 41 52 49 41 42 iable (TK_VARIAB
63b60 4c 45 29 2c 20 74 68 65 6e 20 45 78 70 72 2e 74 LE), then Expr.t
63b70 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 oken contains th
63b80 65 20 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e e .** variable n
63b90 61 6d 65 2e 20 46 69 6e 61 6c 6c 79 2c 20 69 66 ame. Finally, if
63ba0 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
63bb0 69 73 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 is an SQL functi
63bc0 6f 6e 20 28 54 4b 5f 46 55 4e 43 54 49 4f 4e 29 on (TK_FUNCTION)
63bd0 2c 0a 2a 2a 20 74 68 65 6e 20 45 78 70 72 2e 74 ,.** then Expr.t
63be0 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 oken contains th
63bf0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75 e name of the fu
63c00 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78 nction..**.** Ex
63c10 70 72 2e 70 52 69 67 68 74 20 61 6e 64 20 45 78 pr.pRight and Ex
63c20 70 72 2e 70 4c 65 66 74 20 61 72 65 20 74 68 65 pr.pLeft are the
63c30 20 6c 65 66 74 20 61 6e 64 20 72 69 67 68 74 20 left and right
63c40 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 6f subexpressions o
63c50 66 20 61 0a 2a 2a 20 62 69 6e 61 72 79 20 6f 70 f a.** binary op
63c60 65 72 61 74 6f 72 2e 20 45 69 74 68 65 72 20 6f erator. Either o
63c70 72 20 62 6f 74 68 20 6d 61 79 20 62 65 20 4e 55 r both may be NU
63c80 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 78 LL..**.** Expr.x
63c90 2e 70 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74 .pList is a list
63ca0 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 69 66 of arguments if
63cb0 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
63cc0 69 73 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 is an SQL functi
63cd0 6f 6e 2c 0a 2a 2a 20 61 20 43 41 53 45 20 65 78 on,.** a CASE ex
63ce0 70 72 65 73 73 69 6f 6e 20 6f 72 20 61 6e 20 49 pression or an I
63cf0 4e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 N expression of
63d00 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68 73 3e 20 the form "<lhs>
63d10 49 4e 20 28 3c 79 3e 2c 20 3c 7a 3e 2e 2e 2e 29 IN (<y>, <z>...)
63d20 22 2e 0a 2a 2a 20 45 78 70 72 2e 78 2e 70 53 65 "..** Expr.x.pSe
63d30 6c 65 63 74 20 69 73 20 75 73 65 64 20 69 66 20 lect is used if
63d40 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
63d50 73 20 61 20 73 75 62 2d 73 65 6c 65 63 74 20 6f s a sub-select o
63d60 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 r an expression
63d70 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 72 6d 20 22 of.** the form "
63d80 3c 6c 68 73 3e 20 49 4e 20 28 53 45 4c 45 43 54 <lhs> IN (SELECT
63d90 20 2e 2e 2e 29 22 2e 20 49 66 20 74 68 65 20 45 ...)". If the E
63da0 50 5f 78 49 73 53 65 6c 65 63 74 20 62 69 74 20 P_xIsSelect bit
63db0 69 73 20 73 65 74 20 69 6e 20 74 68 65 0a 2a 2a is set in the.**
63dc0 20 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b Expr.flags mask
63dd0 2c 20 74 68 65 6e 20 45 78 70 72 2e 78 2e 70 53 , then Expr.x.pS
63de0 65 6c 65 63 74 20 69 73 20 76 61 6c 69 64 2e 20 elect is valid.
63df0 4f 74 68 65 72 77 69 73 65 2c 20 45 78 70 72 2e Otherwise, Expr.
63e00 78 2e 70 4c 69 73 74 20 69 73 20 0a 2a 2a 20 76 x.pList is .** v
63e10 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 alid..**.** An e
63e20 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 xpression of the
63e30 20 66 6f 72 6d 20 49 44 20 6f 72 20 49 44 2e 49 form ID or ID.I
63e40 44 20 72 65 66 65 72 73 20 74 6f 20 61 20 63 6f D refers to a co
63e50 6c 75 6d 6e 20 69 6e 20 61 20 74 61 62 6c 65 2e lumn in a table.
63e60 0a 2a 2a 20 46 6f 72 20 73 75 63 68 20 65 78 70 .** For such exp
63e70 72 65 73 73 69 6f 6e 73 2c 20 45 78 70 72 2e 6f ressions, Expr.o
63e80 70 20 69 73 20 73 65 74 20 74 6f 20 54 4b 5f 43 p is set to TK_C
63e90 4f 4c 55 4d 4e 20 61 6e 64 20 45 78 70 72 2e 69 OLUMN and Expr.i
63ea0 54 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 Table is.** the
63eb0 69 6e 74 65 67 65 72 20 63 75 72 73 6f 72 20 6e integer cursor n
63ec0 75 6d 62 65 72 20 6f 66 20 61 20 56 44 42 45 20 umber of a VDBE
63ed0 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 cursor pointing
63ee0 74 6f 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e to that table an
63ef0 64 0a 2a 2a 20 45 78 70 72 2e 69 43 6f 6c 75 6d d.** Expr.iColum
63f00 6e 20 69 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 n is the column
63f10 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 73 number for the s
63f20 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e 2e 20 pecific column.
63f30 20 49 66 20 74 68 65 0a 2a 2a 20 65 78 70 72 65 If the.** expre
63f40 73 73 69 6f 6e 20 69 73 20 75 73 65 64 20 61 73 ssion is used as
63f50 20 61 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 a result in an
63f60 61 67 67 72 65 67 61 74 65 20 53 45 4c 45 43 54 aggregate SELECT
63f70 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 76 61 , then the.** va
63f80 6c 75 65 20 69 73 20 61 6c 73 6f 20 73 74 6f 72 lue is also stor
63f90 65 64 20 69 6e 20 74 68 65 20 45 78 70 72 2e 69 ed in the Expr.i
63fa0 41 67 67 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 Agg column in th
63fb0 65 20 61 67 67 72 65 67 61 74 65 20 73 6f 20 74 e aggregate so t
63fc0 68 61 74 0a 2a 2a 20 69 74 20 63 61 6e 20 62 65 hat.** it can be
63fd0 20 61 63 63 65 73 73 65 64 20 61 66 74 65 72 20 accessed after
63fe0 61 6c 6c 20 61 67 67 72 65 67 61 74 65 73 20 61 all aggregates a
63ff0 72 65 20 63 6f 6d 70 75 74 65 64 2e 0a 2a 2a 0a re computed..**.
64000 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 ** If the expres
64010 73 69 6f 6e 20 69 73 20 61 6e 20 75 6e 62 6f 75 sion is an unbou
64020 6e 64 20 76 61 72 69 61 62 6c 65 20 6d 61 72 6b nd variable mark
64030 65 72 20 28 61 20 71 75 65 73 74 69 6f 6e 20 6d er (a question m
64040 61 72 6b 20 0a 2a 2a 20 63 68 61 72 61 63 74 65 ark .** characte
64050 72 20 27 3f 27 20 69 6e 20 74 68 65 20 6f 72 69 r '?' in the ori
64060 67 69 6e 61 6c 20 53 51 4c 29 20 74 68 65 6e 20 ginal SQL) then
64070 74 68 65 20 45 78 70 72 2e 69 54 61 62 6c 65 20 the Expr.iTable
64080 68 6f 6c 64 73 20 74 68 65 20 69 6e 64 65 78 20 holds the index
64090 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 .** number for t
640a0 68 61 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a hat variable..**
640b0 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 .** If the expre
640c0 73 73 69 6f 6e 20 69 73 20 61 20 73 75 62 71 75 ssion is a subqu
640d0 65 72 79 20 74 68 65 6e 20 45 78 70 72 2e 69 43 ery then Expr.iC
640e0 6f 6c 75 6d 6e 20 68 6f 6c 64 73 20 61 6e 20 69 olumn holds an i
640f0 6e 74 65 67 65 72 0a 2a 2a 20 72 65 67 69 73 74 nteger.** regist
64100 65 72 20 6e 75 6d 62 65 72 20 63 6f 6e 74 61 69 er number contai
64110 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 20 ning the result
64120 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79 2e of the subquery.
64130 20 20 49 66 20 74 68 65 0a 2a 2a 20 73 75 62 71 If the.** subq
64140 75 65 72 79 20 67 69 76 65 73 20 61 20 63 6f 6e uery gives a con
64150 73 74 61 6e 74 20 72 65 73 75 6c 74 2c 20 74 68 stant result, th
64160 65 6e 20 69 54 61 62 6c 65 20 69 73 20 2d 31 2e en iTable is -1.
64170 20 20 49 66 20 74 68 65 20 73 75 62 71 75 65 72 If the subquer
64180 79 0a 2a 2a 20 67 69 76 65 73 20 61 20 64 69 66 y.** gives a dif
64190 66 65 72 65 6e 74 20 61 6e 73 77 65 72 20 61 74 ferent answer at
641a0 20 64 69 66 66 65 72 65 6e 74 20 74 69 6d 65 73 different times
641b0 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e during statemen
641c0 74 20 70 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 t processing.**
641d0 74 68 65 6e 20 69 54 61 62 6c 65 20 69 73 20 74 then iTable is t
641e0 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 61 20 he address of a
641f0 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20 subroutine that
64200 63 6f 6d 70 75 74 65 73 20 74 68 65 20 73 75 62 computes the sub
64210 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 query..**.** If
64220 74 68 65 20 45 78 70 72 20 69 73 20 6f 66 20 74 the Expr is of t
64230 79 70 65 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 61 ype OP_Column, a
64240 6e 64 20 74 68 65 20 74 61 62 6c 65 20 69 74 20 nd the table it
64250 69 73 20 73 65 6c 65 63 74 69 6e 67 20 66 72 6f is selecting fro
64260 6d 0a 2a 2a 20 69 73 20 61 20 64 69 73 6b 20 74 m.** is a disk t
64270 61 62 6c 65 20 6f 72 20 74 68 65 20 22 6f 6c 64 able or the "old
64280 2e 2a 22 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 .*" pseudo-table
64290 2c 20 74 68 65 6e 20 70 54 61 62 20 70 6f 69 6e , then pTab poin
642a0 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 72 ts to the.** cor
642b0 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65 responding table
642c0 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a definition..**.
642d0 2a 2a 20 41 4c 4c 4f 43 41 54 49 4f 4e 20 4e 4f ** ALLOCATION NO
642e0 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 45 78 70 72 20 TES:.**.** Expr
642f0 6f 62 6a 65 63 74 73 20 63 61 6e 20 75 73 65 20 objects can use
64300 61 20 6c 6f 74 20 6f 66 20 6d 65 6d 6f 72 79 20 a lot of memory
64310 73 70 61 63 65 20 69 6e 20 64 61 74 61 62 61 73 space in databas
64320 65 20 73 63 68 65 6d 61 2e 20 20 54 6f 0a 2a 2a e schema. To.**
64330 20 68 65 6c 70 20 72 65 64 75 63 65 20 6d 65 6d help reduce mem
64340 6f 72 79 20 72 65 71 75 69 72 65 6d 65 6e 74 73 ory requirements
64350 2c 20 73 6f 6d 65 74 69 6d 65 73 20 61 6e 20 45 , sometimes an E
64360 78 70 72 20 6f 62 6a 65 63 74 20 77 69 6c 6c 20 xpr object will
64370 62 65 0a 2a 2a 20 74 72 75 6e 63 61 74 65 64 2e be.** truncated.
64380 20 20 41 6e 64 20 74 6f 20 72 65 64 75 63 65 20 And to reduce
64390 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 65 the number of me
643a0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 mory allocations
643b0 2c 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 74 , sometimes.** t
643c0 77 6f 20 6f 72 20 6d 6f 72 65 20 45 78 70 72 20 wo or more Expr
643d0 6f 62 6a 65 63 74 73 20 77 69 6c 6c 20 62 65 20 objects will be
643e0 73 74 6f 72 65 64 20 69 6e 20 61 20 73 69 6e 67 stored in a sing
643f0 6c 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 le memory alloca
64400 74 69 6f 6e 2c 0a 2a 2a 20 74 6f 67 65 74 68 65 tion,.** togethe
64410 72 20 77 69 74 68 20 45 78 70 72 2e 7a 54 6f 6b r with Expr.zTok
64420 65 6e 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 0a 2a en strings..**.*
64430 2a 20 49 66 20 74 68 65 20 45 50 5f 52 65 64 75 * If the EP_Redu
64440 63 65 64 20 61 6e 64 20 45 50 5f 54 6f 6b 65 6e ced and EP_Token
64450 4f 6e 6c 79 20 66 6c 61 67 73 20 61 72 65 20 73 Only flags are s
64460 65 74 20 77 68 65 6e 0a 2a 2a 20 61 6e 20 45 78 et when.** an Ex
64470 70 72 20 6f 62 6a 65 63 74 20 69 73 20 74 72 75 pr object is tru
64480 6e 63 61 74 65 64 2e 20 20 57 68 65 6e 20 45 50 ncated. When EP
64490 5f 52 65 64 75 63 65 64 20 69 73 20 73 65 74 2c _Reduced is set,
644a0 20 74 68 65 6e 20 61 6c 6c 0a 2a 2a 20 74 68 65 then all.** the
644b0 20 63 68 69 6c 64 20 45 78 70 72 20 6f 62 6a 65 child Expr obje
644c0 63 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e cts in the Expr.
644d0 70 4c 65 66 74 20 61 6e 64 20 45 78 70 72 2e 70 pLeft and Expr.p
644e0 52 69 67 68 74 20 73 75 62 74 72 65 65 73 0a 2a Right subtrees.*
644f0 2a 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 * are contained
64500 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20 within the same
64510 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
64520 6e 2e 20 20 4e 6f 74 65 2c 20 68 6f 77 65 76 65 n. Note, howeve
64530 72 2c 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 73 r, that.** the s
64540 75 62 74 72 65 65 73 20 69 6e 20 45 78 70 72 2e ubtrees in Expr.
64550 78 2e 70 4c 69 73 74 20 6f 72 20 45 78 70 72 2e x.pList or Expr.
64560 78 2e 70 53 65 6c 65 63 74 20 61 72 65 20 61 6c x.pSelect are al
64570 77 61 79 73 20 73 65 70 61 72 61 74 65 6c 79 0a ways separately.
64580 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 72 65 ** allocated, re
64590 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 gardless of whet
645a0 68 65 72 20 6f 72 20 6e 6f 74 20 45 50 5f 52 65 her or not EP_Re
645b0 64 75 63 65 64 20 69 73 20 73 65 74 2e 0a 2a 2f duced is set..*/
645c0 0a 73 74 72 75 63 74 20 45 78 70 72 20 7b 0a 20 .struct Expr {.
645d0 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 u8 op;
645e0 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 72 61 /* Opera
645f0 74 69 6f 6e 20 70 65 72 66 6f 72 6d 65 64 20 62 tion performed b
64600 79 20 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 20 y this node */.
64610 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20 char affinity;
64620 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 /* The a
64630 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 63 ffinity of the c
64640 6f 6c 75 6d 6e 20 6f 72 20 30 20 69 66 20 6e 6f olumn or 0 if no
64650 74 20 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 t a column */.
64660 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 u16 flags;
64670 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 /* Variou
64680 73 20 66 6c 61 67 73 2e 20 20 45 50 5f 2a 20 53 s flags. EP_* S
64690 65 65 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 75 6e ee below */. un
646a0 69 6f 6e 20 7b 0a 20 20 20 20 63 68 61 72 20 2a ion {. char *
646b0 7a 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 zToken;
646c0 20 2f 2a 20 54 6f 6b 65 6e 20 76 61 6c 75 65 2e /* Token value.
646d0 20 5a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64 Zero terminated
646e0 20 61 6e 64 20 64 65 71 75 6f 74 65 64 20 2a 2f and dequoted */
646f0 0a 20 20 20 20 69 6e 74 20 69 56 61 6c 75 65 3b . int iValue;
64700 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
64710 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 66 20 nteger value if
64720 45 50 5f 49 6e 74 56 61 6c 75 65 20 2a 2f 0a 20 EP_IntValue */.
64730 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 } u;.. /* If t
64740 68 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 he EP_TokenOnly
64750 66 6c 61 67 20 69 73 20 73 65 74 20 69 6e 20 74 flag is set in t
64760 68 65 20 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 he Expr.flags ma
64770 73 6b 2c 20 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a sk, then no. **
64780 20 73 70 61 63 65 20 69 73 20 61 6c 6c 6f 63 61 space is alloca
64790 74 65 64 20 66 6f 72 20 74 68 65 20 66 69 65 6c ted for the fiel
647a0 64 73 20 62 65 6c 6f 77 20 74 68 69 73 20 70 6f ds below this po
647b0 69 6e 74 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 int. An attempt
647c0 74 6f 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 74 to. ** access t
647d0 68 65 6d 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 hem will result
647e0 69 6e 20 61 20 73 65 67 66 61 75 6c 74 20 6f 72 in a segfault or
647f0 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 20 0a 20 malfunction. .
64800 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
64810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64840 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 45 78 70 72 20 ******/.. Expr
64850 2a 70 4c 65 66 74 3b 20 20 20 20 20 20 20 20 20 *pLeft;
64860 20 20 2f 2a 20 4c 65 66 74 20 73 75 62 6e 6f 64 /* Left subnod
64870 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 e */. Expr *pRi
64880 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ght; /*
64890 20 52 69 67 68 74 20 73 75 62 6e 6f 64 65 20 2a Right subnode *
648a0 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 /. union {.
648b0 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b ExprList *pList;
648c0 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e /* Function
648d0 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 69 6e arguments or in
648e0 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 3c 65 78 "<expr> IN (<ex
648f0 70 72 2d 6c 69 73 74 29 22 20 2a 2f 0a 20 20 20 pr-list)" */.
64900 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 Select *pSelect
64910 3b 20 20 20 20 20 2f 2a 20 55 73 65 64 20 66 6f ; /* Used fo
64920 72 20 73 75 62 2d 73 65 6c 65 63 74 73 20 61 6e r sub-selects an
64930 64 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 3c 73 d "<expr> IN (<s
64940 65 6c 65 63 74 3e 29 22 20 2a 2f 0a 20 20 7d 20 elect>)" */. }
64950 78 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 x;. CollSeq *pC
64960 6f 6c 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 oll; /* T
64970 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 he collation typ
64980 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 e of the column
64990 6f 72 20 30 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 or 0 */.. /* If
649a0 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 20 the EP_Reduced
649b0 66 6c 61 67 20 69 73 20 73 65 74 20 69 6e 20 74 flag is set in t
649c0 68 65 20 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 he Expr.flags ma
649d0 73 6b 2c 20 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a sk, then no. **
649e0 20 73 70 61 63 65 20 69 73 20 61 6c 6c 6f 63 61 space is alloca
649f0 74 65 64 20 66 6f 72 20 74 68 65 20 66 69 65 6c ted for the fiel
64a00 64 73 20 62 65 6c 6f 77 20 74 68 69 73 20 70 6f ds below this po
64a10 69 6e 74 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 int. An attempt
64a20 74 6f 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 74 to. ** access t
64a30 68 65 6d 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 hem will result
64a40 69 6e 20 61 20 73 65 67 66 61 75 6c 74 20 6f 72 in a segfault or
64a50 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 malfunction..
64a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64aa0 2a 2a 2a 2a 2a 2f 0a 0a 20 20 69 6e 74 20 69 54 *****/.. int iT
64ab0 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 able;
64ac0 20 2f 2a 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 /* TK_COLUMN: c
64ad0 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 ursor number of
64ae0 74 61 62 6c 65 20 68 6f 6c 64 69 6e 67 20 63 6f table holding co
64af0 6c 75 6d 6e 0a 20 20 20 20 20 20 20 20 20 20 20 lumn.
64b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a **
64b10 20 54 4b 5f 52 45 47 49 53 54 45 52 3a 20 72 65 TK_REGISTER: re
64b20 67 69 73 74 65 72 20 6e 75 6d 62 65 72 0a 20 20 gister number.
64b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64b40 20 20 20 20 20 20 20 2a 2a 20 54 4b 5f 54 52 49 ** TK_TRI
64b50 47 47 45 52 3a 20 31 20 2d 3e 20 6e 65 77 2c 20 GGER: 1 -> new,
64b60 30 20 2d 3e 20 6f 6c 64 20 2a 2f 0a 20 20 69 31 0 -> old */. i1
64b70 36 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 6 iColumn;
64b80 20 20 20 20 20 2f 2a 20 54 4b 5f 43 4f 4c 55 4d /* TK_COLUM
64b90 4e 3a 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 2e N: column index.
64ba0 20 20 2d 31 20 66 6f 72 20 72 6f 77 69 64 2e 0a -1 for rowid..
64bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64bc0 20 20 20 20 20 20 20 20 20 2a 2a 20 54 4b 5f 56 ** TK_V
64bd0 41 52 49 41 42 4c 45 3a 20 76 61 72 69 61 62 6c ARIABLE: variabl
64be0 65 20 6e 75 6d 62 65 72 20 28 61 6c 77 61 79 73 e number (always
64bf0 20 3e 3d 20 31 29 2e 20 2a 2f 0a 20 20 69 31 36 >= 1). */. i16
64c00 20 69 41 67 67 3b 20 20 20 20 20 20 20 20 20 20 iAgg;
64c10 20 20 20 20 2f 2a 20 57 68 69 63 68 20 65 6e 74 /* Which ent
64c20 72 79 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e ry in pAggInfo->
64c30 61 43 6f 6c 5b 5d 20 6f 72 20 2d 3e 61 46 75 6e aCol[] or ->aFun
64c40 63 5b 5d 20 2a 2f 0a 20 20 69 31 36 20 69 52 69 c[] */. i16 iRi
64c50 67 68 74 4a 6f 69 6e 54 61 62 6c 65 3b 20 20 20 ghtJoinTable;
64c60 2f 2a 20 49 66 20 45 50 5f 46 72 6f 6d 4a 6f 69 /* If EP_FromJoi
64c70 6e 2c 20 74 68 65 20 72 69 67 68 74 20 74 61 62 n, the right tab
64c80 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 6e 20 2a le of the join *
64c90 2f 0a 20 20 75 38 20 66 6c 61 67 73 32 3b 20 20 /. u8 flags2;
64ca0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 /* Se
64cb0 63 6f 6e 64 20 73 65 74 20 6f 66 20 66 6c 61 67 cond set of flag
64cc0 73 2e 20 20 45 50 32 5f 2e 2e 2e 20 2a 2f 0a 20 s. EP2_... */.
64cd0 20 75 38 20 6f 70 32 3b 20 20 20 20 20 20 20 20 u8 op2;
64ce0 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20 /* If a
64cf0 54 4b 5f 52 45 47 49 53 54 45 52 2c 20 74 68 65 TK_REGISTER, the
64d00 20 6f 72 69 67 69 6e 61 6c 20 76 61 6c 75 65 20 original value
64d10 6f 66 20 45 78 70 72 2e 6f 70 20 2a 2f 0a 20 20 of Expr.op */.
64d20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 AggInfo *pAggInf
64d30 6f 3b 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 o; /* Used b
64d40 79 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 y TK_AGG_COLUMN
64d50 61 6e 64 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 and TK_AGG_FUNCT
64d60 49 4f 4e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a ION */. Table *
64d70 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 pTab;
64d80 2f 2a 20 54 61 62 6c 65 20 66 6f 72 20 54 4b 5f /* Table for TK_
64d90 43 4f 4c 55 4d 4e 20 65 78 70 72 65 73 73 69 6f COLUMN expressio
64da0 6e 73 2e 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 ns. */.#if SQLIT
64db0 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 E_MAX_EXPR_DEPTH
64dc0 3e 30 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 >0. int nHeight
64dd0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 ; /* H
64de0 65 69 67 68 74 20 6f 66 20 74 68 65 20 74 72 65 eight of the tre
64df0 65 20 68 65 61 64 65 64 20 62 79 20 74 68 69 73 e headed by this
64e00 20 6e 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a node */.#endif.
64e10 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f };../*.** The fo
64e20 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68 65 20 llowing are the
64e30 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62 69 74 73 meanings of bits
64e40 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61 in the Expr.fla
64e50 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 gs field..*/.#de
64e60 66 69 6e 65 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e fine EP_FromJoin
64e70 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4f 72 0x0001 /* Or
64e80 69 67 69 6e 61 74 65 64 20 69 6e 20 4f 4e 20 6f iginated in ON o
64e90 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f r USING clause o
64ea0 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 f a join */.#def
64eb0 69 6e 65 20 45 50 5f 41 67 67 20 20 20 20 20 20 ine EP_Agg
64ec0 20 20 30 78 30 30 30 32 20 20 2f 2a 20 43 6f 6e 0x0002 /* Con
64ed0 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 tains one or mor
64ee0 65 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 e aggregate func
64ef0 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 tions */.#define
64f00 20 45 50 5f 52 65 73 6f 6c 76 65 64 20 20 20 30 EP_Resolved 0
64f10 78 30 30 30 34 20 20 2f 2a 20 49 44 73 20 68 61 x0004 /* IDs ha
64f20 76 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 ve been resolved
64f30 20 74 6f 20 43 4f 4c 55 4d 4e 73 20 2a 2f 0a 23 to COLUMNs */.#
64f40 64 65 66 69 6e 65 20 45 50 5f 45 72 72 6f 72 20 define EP_Error
64f50 20 20 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 0x0008 /*
64f60 45 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e 74 61 Expression conta
64f70 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 ins one or more
64f80 65 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e errors */.#defin
64f90 65 20 45 50 5f 44 69 73 74 69 6e 63 74 20 20 20 e EP_Distinct
64fa0 30 78 30 30 31 30 20 20 2f 2a 20 41 67 67 72 65 0x0010 /* Aggre
64fb0 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 77 69 gate function wi
64fc0 74 68 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 th DISTINCT keyw
64fd0 6f 72 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 ord */.#define E
64fe0 50 5f 56 61 72 53 65 6c 65 63 74 20 20 30 78 30 P_VarSelect 0x0
64ff0 30 32 30 20 20 2f 2a 20 70 53 65 6c 65 63 74 20 020 /* pSelect
65000 69 73 20 63 6f 72 72 65 6c 61 74 65 64 2c 20 6e is correlated, n
65010 6f 74 20 63 6f 6e 73 74 61 6e 74 20 2a 2f 0a 23 ot constant */.#
65020 64 65 66 69 6e 65 20 45 50 5f 44 62 6c 51 75 6f define EP_DblQuo
65030 74 65 64 20 20 30 78 30 30 34 30 20 20 2f 2a 20 ted 0x0040 /*
65040 74 6f 6b 65 6e 2e 7a 20 77 61 73 20 6f 72 69 67 token.z was orig
65050 69 6e 61 6c 6c 79 20 69 6e 20 22 2e 2e 2e 22 20 inally in "..."
65060 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 49 6e */.#define EP_In
65070 66 69 78 46 75 6e 63 20 20 30 78 30 30 38 30 20 fixFunc 0x0080
65080 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 6e 20 /* True for an
65090 69 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e 3a 20 infix function:
650a0 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 65 74 63 20 LIKE, GLOB, etc
650b0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 45 78 */.#define EP_Ex
650c0 70 43 6f 6c 6c 61 74 65 20 30 78 30 31 30 30 20 pCollate 0x0100
650d0 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 /* Collating se
650e0 71 75 65 6e 63 65 20 73 70 65 63 69 66 69 65 64 quence specified
650f0 20 65 78 70 6c 69 63 69 74 6c 79 20 2a 2f 0a 23 explicitly */.#
65100 64 65 66 69 6e 65 20 45 50 5f 41 6e 79 41 66 66 define EP_AnyAff
65110 20 20 20 20 20 30 78 30 32 30 30 20 20 2f 2a 20 0x0200 /*
65120 43 61 6e 20 74 61 6b 65 20 61 20 63 61 63 68 65 Can take a cache
65130 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 6e 79 20 d column of any
65140 61 66 66 69 6e 69 74 79 20 2a 2f 0a 23 64 65 66 affinity */.#def
65150 69 6e 65 20 45 50 5f 46 69 78 65 64 44 65 73 74 ine EP_FixedDest
65160 20 20 30 78 30 34 30 30 20 20 2f 2a 20 52 65 73 0x0400 /* Res
65170 75 6c 74 20 6e 65 65 64 65 64 20 69 6e 20 61 20 ult needed in a
65180 73 70 65 63 69 66 69 63 20 72 65 67 69 73 74 65 specific registe
65190 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f r */.#define EP_
651a0 49 6e 74 56 61 6c 75 65 20 20 20 30 78 30 38 30 IntValue 0x080
651b0 30 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 0 /* Integer va
651c0 6c 75 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e lue contained in
651d0 20 75 2e 69 56 61 6c 75 65 20 2a 2f 0a 23 64 65 u.iValue */.#de
651e0 66 69 6e 65 20 45 50 5f 78 49 73 53 65 6c 65 63 fine EP_xIsSelec
651f0 74 20 20 30 78 31 30 30 30 20 20 2f 2a 20 78 2e t 0x1000 /* x.
65200 70 53 65 6c 65 63 74 20 69 73 20 76 61 6c 69 64 pSelect is valid
65210 20 28 6f 74 68 65 72 77 69 73 65 20 78 2e 70 4c (otherwise x.pL
65220 69 73 74 20 69 73 29 20 2a 2f 0a 0a 23 64 65 66 ist is) */..#def
65230 69 6e 65 20 45 50 5f 52 65 64 75 63 65 64 20 20 ine EP_Reduced
65240 20 20 30 78 32 30 30 30 20 20 2f 2a 20 45 78 70 0x2000 /* Exp
65250 72 20 73 74 72 75 63 74 20 69 73 20 45 58 50 52 r struct is EXPR
65260 5f 52 45 44 55 43 45 44 53 49 5a 45 20 62 79 74 _REDUCEDSIZE byt
65270 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 es only */.#defi
65280 6e 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 ne EP_TokenOnly
65290 20 30 78 34 30 30 30 20 20 2f 2a 20 45 78 70 72 0x4000 /* Expr
652a0 20 73 74 72 75 63 74 20 69 73 20 45 58 50 52 5f struct is EXPR_
652b0 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 62 79 TOKENONLYSIZE by
652c0 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 tes only */.#def
652d0 69 6e 65 20 45 50 5f 53 74 61 74 69 63 20 20 20 ine EP_Static
652e0 20 20 30 78 38 30 30 30 20 20 2f 2a 20 48 65 6c 0x8000 /* Hel
652f0 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6e 6f 74 20 d in memory not
65300 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 obtained from ma
65310 6c 6c 6f 63 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a lloc() */../*.**
65320 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 The following a
65330 72 65 20 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 re the meanings
65340 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 45 of bits in the E
65350 78 70 72 2e 66 6c 61 67 73 32 20 66 69 65 6c 64 xpr.flags2 field
65360 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 32 ..*/.#define EP2
65370 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 20 20 _MallocedToken
65380 30 78 30 30 30 31 20 20 2f 2a 20 4e 65 65 64 20 0x0001 /* Need
65390 74 6f 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 to sqlite3DbFree
653a0 28 29 20 45 78 70 72 2e 7a 54 6f 6b 65 6e 20 2a () Expr.zToken *
653b0 2f 0a 23 64 65 66 69 6e 65 20 45 50 32 5f 49 72 /.#define EP2_Ir
653c0 72 65 64 75 63 69 62 6c 65 20 20 20 20 30 78 30 reducible 0x0
653d0 30 30 32 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 45 002 /* Cannot E
653e0 58 50 52 44 55 50 5f 52 45 44 55 43 45 20 74 68 XPRDUP_REDUCE th
653f0 69 73 20 45 78 70 72 20 2a 2f 0a 0a 2f 2a 0a 2a is Expr */../*.*
65400 2a 20 54 68 65 20 70 73 65 75 64 6f 2d 72 6f 75 * The pseudo-rou
65410 74 69 6e 65 20 73 71 6c 69 74 65 33 45 78 70 72 tine sqlite3Expr
65420 53 65 74 49 72 72 65 64 75 63 69 62 6c 65 20 73 SetIrreducible s
65430 65 74 73 20 74 68 65 20 45 50 32 5f 49 72 72 65 ets the EP2_Irre
65440 64 75 63 69 62 6c 65 0a 2a 2a 20 66 6c 61 67 20 ducible.** flag
65450 6f 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e on an expression
65460 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 69 structure. Thi
65470 73 20 66 6c 61 67 20 69 73 20 75 73 65 64 20 66 s flag is used f
65480 6f 72 20 56 56 26 41 20 6f 6e 6c 79 2e 20 20 54 or VV&A only. T
65490 68 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 he.** routine is
654a0 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 implemented as
654b0 61 20 6d 61 63 72 6f 20 74 68 61 74 20 6f 6e 6c a macro that onl
654c0 79 20 77 6f 72 6b 73 20 77 68 65 6e 20 69 6e 20 y works when in
654d0 64 65 62 75 67 67 69 6e 67 20 6d 6f 64 65 2c 0a debugging mode,.
654e0 2a 2a 20 73 6f 20 61 73 20 6e 6f 74 20 74 6f 20 ** so as not to
654f0 62 75 72 64 65 6e 20 70 72 6f 64 75 63 74 69 6f burden productio
65500 6e 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64 65 n code..*/.#ifde
65510 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 f SQLITE_DEBUG.#
65520 20 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 49 define ExprSetI
65530 72 72 65 64 75 63 69 62 6c 65 28 58 29 20 20 28 rreducible(X) (
65540 58 29 2d 3e 66 6c 61 67 73 32 20 7c 3d 20 45 50 X)->flags2 |= EP
65550 32 5f 49 72 72 65 64 75 63 69 62 6c 65 0a 23 65 2_Irreducible.#e
65560 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 45 78 70 lse.# define Exp
65570 72 53 65 74 49 72 72 65 64 75 63 69 62 6c 65 28 rSetIrreducible(
65580 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a X).#endif../*.**
65590 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 63 61 These macros ca
655a0 6e 20 62 65 20 75 73 65 64 20 74 6f 20 74 65 73 n be used to tes
655b0 74 2c 20 73 65 74 2c 20 6f 72 20 63 6c 65 61 72 t, set, or clear
655c0 20 62 69 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a bits in the .**
655d0 20 45 78 70 72 2e 66 6c 61 67 73 20 66 69 65 6c Expr.flags fiel
655e0 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 78 d..*/.#define Ex
655f0 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 45 2c prHasProperty(E,
65600 50 29 20 20 20 20 20 28 28 28 45 29 2d 3e 66 6c P) (((E)->fl
65610 61 67 73 26 28 50 29 29 3d 3d 28 50 29 29 0a 23 ags&(P))==(P)).#
65620 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 41 6e define ExprHasAn
65630 79 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 yProperty(E,P)
65640 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 29 (((E)->flags&(P)
65650 29 21 3d 30 29 0a 23 64 65 66 69 6e 65 20 45 78 )!=0).#define Ex
65660 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 45 2c prSetProperty(E,
65670 50 29 20 20 20 20 20 28 45 29 2d 3e 66 6c 61 67 P) (E)->flag
65680 73 7c 3d 28 50 29 0a 23 64 65 66 69 6e 65 20 45 s|=(P).#define E
65690 78 70 72 43 6c 65 61 72 50 72 6f 70 65 72 74 79 xprClearProperty
656a0 28 45 2c 50 29 20 20 20 28 45 29 2d 3e 66 6c 61 (E,P) (E)->fla
656b0 67 73 26 3d 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 gs&=~(P)../*.**
656c0 4d 61 63 72 6f 73 20 74 6f 20 64 65 74 65 72 6d Macros to determ
656d0 69 6e 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ine the number o
656e0 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 f bytes required
656f0 20 62 79 20 61 20 6e 6f 72 6d 61 6c 20 45 78 70 by a normal Exp
65700 72 20 0a 2a 2a 20 73 74 72 75 63 74 2c 20 61 6e r .** struct, an
65710 20 45 78 70 72 20 73 74 72 75 63 74 20 77 69 74 Expr struct wit
65720 68 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 h the EP_Reduced
65730 20 66 6c 61 67 20 73 65 74 20 69 6e 20 45 78 70 flag set in Exp
65740 72 2e 66 6c 61 67 73 20 0a 2a 2a 20 61 6e 64 20 r.flags .** and
65750 61 6e 20 45 78 70 72 20 73 74 72 75 63 74 20 77 an Expr struct w
65760 69 74 68 20 74 68 65 20 45 50 5f 54 6f 6b 65 6e ith the EP_Token
65770 4f 6e 6c 79 20 66 6c 61 67 20 73 65 74 2e 0a 2a Only flag set..*
65780 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f 46 /.#define EXPR_F
65790 55 4c 4c 53 49 5a 45 20 20 20 20 20 20 20 20 20 ULLSIZE
657a0 20 20 73 69 7a 65 6f 66 28 45 78 70 72 29 20 20 sizeof(Expr)
657b0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6c 6c /* Full
657c0 20 73 69 7a 65 20 2a 2f 0a 23 64 65 66 69 6e 65 size */.#define
657d0 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a EXPR_REDUCEDSIZ
657e0 45 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 6f E offseto
657f0 66 28 45 78 70 72 2c 69 54 61 62 6c 65 29 20 20 f(Expr,iTable)
65800 2f 2a 20 43 6f 6d 6d 6f 6e 20 66 65 61 74 75 72 /* Common featur
65810 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 es */.#define EX
65820 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 PR_TOKENONLYSIZE
65830 20 20 20 20 20 20 6f 66 66 73 65 74 6f 66 28 45 offsetof(E
65840 78 70 72 2c 70 4c 65 66 74 29 20 20 20 2f 2a 20 xpr,pLeft) /*
65850 46 65 77 65 72 20 66 65 61 74 75 72 65 73 20 2a Fewer features *
65860 2f 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67 73 20 70 /../*.** Flags p
65870 61 73 73 65 64 20 74 6f 20 74 68 65 20 73 71 6c assed to the sql
65880 69 74 65 33 45 78 70 72 44 75 70 28 29 20 66 75 ite3ExprDup() fu
65890 6e 63 74 69 6f 6e 2e 20 53 65 65 20 74 68 65 20 nction. See the
658a0 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 0a header comment .
658b0 2a 2a 20 61 62 6f 76 65 20 73 71 6c 69 74 65 33 ** above sqlite3
658c0 45 78 70 72 44 75 70 28 29 20 66 6f 72 20 64 65 ExprDup() for de
658d0 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e tails..*/.#defin
658e0 65 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 e EXPRDUP_REDUCE
658f0 20 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20 0x0001
65900 20 2f 2a 20 55 73 65 64 20 72 65 64 75 63 65 64 /* Used reduced
65910 2d 73 69 7a 65 20 45 78 70 72 20 6e 6f 64 65 73 -size Expr nodes
65920 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 */../*.** A lis
65930 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 t of expressions
65940 2e 20 20 45 61 63 68 20 65 78 70 72 65 73 73 69 . Each expressi
65950 6f 6e 20 6d 61 79 20 6f 70 74 69 6f 6e 61 6c 6c on may optionall
65960 79 20 68 61 76 65 20 61 0a 2a 2a 20 6e 61 6d 65 y have a.** name
65970 2e 20 20 41 6e 20 65 78 70 72 2f 6e 61 6d 65 20 . An expr/name
65980 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 63 61 6e 20 combination can
65990 62 65 20 75 73 65 64 20 69 6e 20 73 65 76 65 72 be used in sever
659a0 61 6c 20 77 61 79 73 2c 20 73 75 63 68 0a 2a 2a al ways, such.**
659b0 20 61 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20 as the list of
659c0 22 65 78 70 72 20 41 53 20 49 44 22 20 66 69 65 "expr AS ID" fie
659d0 6c 64 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 lds following a
659e0 22 53 45 4c 45 43 54 22 20 6f 72 20 69 6e 20 74 "SELECT" or in t
659f0 68 65 0a 2a 2a 20 6c 69 73 74 20 6f 66 20 22 49 he.** list of "I
65a00 44 20 3d 20 65 78 70 72 22 20 69 74 65 6d 73 20 D = expr" items
65a10 69 6e 20 61 6e 20 55 50 44 41 54 45 2e 20 20 41 in an UPDATE. A
65a20 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 list of express
65a30 69 6f 6e 73 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f ions can.** also
65a40 20 62 65 20 75 73 65 64 20 61 73 20 74 68 65 20 be used as the
65a50 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 20 66 75 argument to a fu
65a60 6e 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 nction, in which
65a70 20 63 61 73 65 20 74 68 65 20 61 2e 7a 4e 61 6d case the a.zNam
65a80 65 0a 2a 2a 20 66 69 65 6c 64 20 69 73 20 6e 6f e.** field is no
65a90 74 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 t used..*/.struc
65aa0 74 20 45 78 70 72 4c 69 73 74 20 7b 0a 20 20 69 t ExprList {. i
65ab0 6e 74 20 6e 45 78 70 72 3b 20 20 20 20 20 20 20 nt nExpr;
65ac0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
65ad0 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f of expressions o
65ae0 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 n the list */.
65af0 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 int nAlloc;
65b00 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
65b10 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f of entries allo
65b20 63 61 74 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 cated below */.
65b30 20 69 6e 74 20 69 45 43 75 72 73 6f 72 3b 20 20 int iECursor;
65b40 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 /* VDBE
65b50 43 75 72 73 6f 72 20 61 73 73 6f 63 69 61 74 65 Cursor associate
65b60 64 20 77 69 74 68 20 74 68 69 73 20 45 78 70 72 d with this Expr
65b70 4c 69 73 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 List */. struct
65b80 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 7b ExprList_item {
65b90 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 . Expr *pExpr
65ba0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
65bb0 68 65 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 he list of expre
65bc0 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 20 20 63 68 ssions */. ch
65bd0 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 ar *zName;
65be0 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 73 /* Token as
65bf0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
65c00 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f is expression */
65c10 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 70 61 6e . char *zSpan
65c20 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f ; /* O
65c30 72 69 67 69 6e 61 6c 20 74 65 78 74 20 6f 66 20 riginal text of
65c40 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a the expression *
65c50 2f 0a 20 20 20 20 75 38 20 73 6f 72 74 4f 72 64 /. u8 sortOrd
65c60 65 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 er; /*
65c70 31 20 66 6f 72 20 44 45 53 43 20 6f 72 20 30 20 1 for DESC or 0
65c80 66 6f 72 20 41 53 43 20 2a 2f 0a 20 20 20 20 75 for ASC */. u
65c90 38 20 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 8 done;
65ca0 20 20 20 20 20 20 2f 2a 20 41 20 66 6c 61 67 20 /* A flag
65cb0 74 6f 20 69 6e 64 69 63 61 74 65 20 77 68 65 6e to indicate when
65cc0 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 66 processing is f
65cd0 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20 20 20 75 inished */. u
65ce0 31 36 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 16 iCol;
65cf0 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 4f 52 44 /* For ORD
65d00 45 52 20 42 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75 ER BY, column nu
65d10 6d 62 65 72 20 69 6e 20 72 65 73 75 6c 74 20 73 mber in result s
65d20 65 74 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 41 et */. u16 iA
65d30 6c 69 61 73 3b 20 20 20 20 20 20 20 20 20 20 20 lias;
65d40 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 50 /* Index into P
65d50 61 72 73 65 2e 61 41 6c 69 61 73 5b 5d 20 66 6f arse.aAlias[] fo
65d60 72 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a r zName */. } *
65d70 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 a;
65d80 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 /* One entry
65d90 20 66 6f 72 20 65 61 63 68 20 65 78 70 72 65 73 for each expres
65da0 73 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a sion */.};../*.*
65db0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
65dc0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
65dd0 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70 is used by the p
65de0 61 72 73 65 72 20 74 6f 20 72 65 63 6f 72 64 20 arser to record
65df0 62 6f 74 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 both.** the pars
65e00 65 20 74 72 65 65 20 66 6f 72 20 61 6e 20 65 78 e tree for an ex
65e10 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 74 68 65 pression and the
65e20 20 73 70 61 6e 20 6f 66 20 69 6e 70 75 74 20 74 span of input t
65e30 65 78 74 20 66 6f 72 20 61 6e 0a 2a 2a 20 65 78 ext for an.** ex
65e40 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 pression..*/.str
65e50 75 63 74 20 45 78 70 72 53 70 61 6e 20 7b 0a 20 uct ExprSpan {.
65e60 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 Expr *pExpr;
65e70 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 /* The ex
65e80 70 72 65 73 73 69 6f 6e 20 70 61 72 73 65 20 74 pression parse t
65e90 72 65 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ree */. const c
65ea0 68 61 72 20 2a 7a 53 74 61 72 74 3b 20 20 20 2f har *zStart; /
65eb0 2a 20 46 69 72 73 74 20 63 68 61 72 61 63 74 65 * First characte
65ec0 72 20 6f 66 20 69 6e 70 75 74 20 74 65 78 74 20 r of input text
65ed0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
65ee0 2a 7a 45 6e 64 3b 20 20 20 20 20 2f 2a 20 4f 6e *zEnd; /* On
65ef0 65 20 63 68 61 72 61 63 74 65 72 20 70 61 73 74 e character past
65f00 20 74 68 65 20 65 6e 64 20 6f 66 20 69 6e 70 75 the end of inpu
65f10 74 20 74 65 78 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a t text */.};../*
65f20 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
65f30 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
65f40 65 20 63 61 6e 20 68 6f 6c 64 20 61 20 73 69 6d e can hold a sim
65f50 70 6c 65 20 6c 69 73 74 20 6f 66 20 69 64 65 6e ple list of iden
65f60 74 69 66 69 65 72 73 2c 0a 2a 2a 20 73 75 63 68 tifiers,.** such
65f70 20 61 73 20 74 68 65 20 6c 69 73 74 20 22 61 2c as the list "a,
65f80 62 2c 63 22 20 69 6e 20 74 68 65 20 66 6f 6c 6c b,c" in the foll
65f90 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 owing statements
65fa0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 49 4e 53 :.**.** INS
65fb0 45 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 ERT INTO t(a,b,c
65fc0 29 20 56 41 4c 55 45 53 20 2e 2e 2e 3b 0a 2a 2a ) VALUES ...;.**
65fd0 20 20 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 CREATE IND
65fe0 45 58 20 69 64 78 20 4f 4e 20 74 28 61 2c 62 2c EX idx ON t(a,b,
65ff0 63 29 3b 0a 2a 2a 20 20 20 20 20 20 43 52 45 41 c);.** CREA
66000 54 45 20 54 52 49 47 47 45 52 20 74 72 69 67 20 TE TRIGGER trig
66010 42 45 46 4f 52 45 20 55 50 44 41 54 45 20 4f 4e BEFORE UPDATE ON
66020 20 74 28 61 2c 62 2c 63 29 20 2e 2e 2e 3b 0a 2a t(a,b,c) ...;.*
66030 2a 0a 2a 2a 20 54 68 65 20 49 64 4c 69 73 74 2e *.** The IdList.
66040 61 2e 69 64 78 20 66 69 65 6c 64 20 69 73 20 75 a.idx field is u
66050 73 65 64 20 77 68 65 6e 20 74 68 65 20 49 64 4c sed when the IdL
66060 69 73 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 ist represents t
66070 68 65 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 63 6f he list of.** co
66080 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 66 74 65 72 lumn names after
66090 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e a table name in
660a0 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65 an INSERT state
660b0 6d 65 6e 74 2e 20 20 49 6e 20 74 68 65 20 73 74 ment. In the st
660c0 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 atement.**.**
660d0 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 28 INSERT INTO t(
660e0 61 2c 62 2c 63 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a a,b,c) ....**.**
660f0 20 49 66 20 22 61 22 20 69 73 20 74 68 65 20 6b If "a" is the k
66100 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 -th column of ta
66110 62 6c 65 20 22 74 22 2c 20 74 68 65 6e 20 49 64 ble "t", then Id
66120 4c 69 73 74 2e 61 5b 30 5d 2e 69 64 78 3d 3d 6b List.a[0].idx==k
66130 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 64 4c 69 ..*/.struct IdLi
66140 73 74 20 7b 0a 20 20 73 74 72 75 63 74 20 49 64 st {. struct Id
66150 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 List_item {.
66160 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
66170 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
66180 20 69 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a 20 identifier */.
66190 20 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 int idx;
661a0 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e /* Index in
661b0 20 73 6f 6d 65 20 54 61 62 6c 65 2e 61 43 6f 6c some Table.aCol
661c0 5b 5d 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6e [] of a column n
661d0 61 6d 65 64 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 amed zName */.
661e0 7d 20 2a 61 3b 0a 20 20 69 6e 74 20 6e 49 64 3b } *a;. int nId;
661f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
66200 65 72 20 6f 66 20 69 64 65 6e 74 69 66 69 65 72 er of identifier
66210 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f s on the list */
66220 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 . int nAlloc;
66230 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
66240 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 entries allocat
66250 65 64 20 66 6f 72 20 61 5b 5d 20 62 65 6c 6f 77 ed for a[] below
66260 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 */.};../*.** Th
66270 65 20 62 69 74 6d 61 73 6b 20 64 61 74 61 74 79 e bitmask dataty
66280 70 65 20 64 65 66 69 6e 65 64 20 62 65 6c 6f 77 pe defined below
66290 20 69 73 20 75 73 65 64 20 66 6f 72 20 76 61 72 is used for var
662a0 69 6f 75 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f ious optimizatio
662b0 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 ns..**.** Changi
662c0 6e 67 20 74 68 69 73 20 66 72 6f 6d 20 61 20 36 ng this from a 6
662d0 34 2d 62 69 74 20 74 6f 20 61 20 33 32 2d 62 69 4-bit to a 32-bi
662e0 74 20 74 79 70 65 20 6c 69 6d 69 74 73 20 74 68 t type limits th
662f0 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 e number of.** t
66300 61 62 6c 65 73 20 69 6e 20 61 20 6a 6f 69 6e 20 ables in a join
66310 74 6f 20 33 32 20 69 6e 73 74 65 61 64 20 6f 66 to 32 instead of
66320 20 36 34 2e 20 20 42 75 74 20 69 74 20 61 6c 73 64. But it als
66330 6f 20 72 65 64 75 63 65 73 20 74 68 65 20 73 69 o reduces the si
66340 7a 65 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 69 62 ze.** of the lib
66350 72 61 72 79 20 62 79 20 37 33 38 20 62 79 74 65 rary by 738 byte
66360 73 20 6f 6e 20 69 78 38 36 2e 0a 2a 2f 0a 74 79 s on ix86..*/.ty
66370 70 65 64 65 66 20 75 36 34 20 42 69 74 6d 61 73 pedef u64 Bitmas
66380 6b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 k;../*.** The nu
66390 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e 20 mber of bits in
663a0 61 20 42 69 74 6d 61 73 6b 2e 20 20 22 42 4d 53 a Bitmask. "BMS
663b0 22 20 6d 65 61 6e 73 20 22 42 69 74 4d 61 73 6b " means "BitMask
663c0 20 53 69 7a 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 Size"..*/.#defi
663d0 6e 65 20 42 4d 53 20 20 28 28 69 6e 74 29 28 73 ne BMS ((int)(s
663e0 69 7a 65 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 izeof(Bitmask)*8
663f0 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f ))../*.** The fo
66400 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
66410 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 e describes the
66420 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 FROM clause of a
66430 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
66440 74 2e 0a 2a 2a 20 45 61 63 68 20 74 61 62 6c 65 t..** Each table
66450 20 6f 72 20 73 75 62 71 75 65 72 79 20 69 6e 20 or subquery in
66460 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 the FROM clause
66470 69 73 20 61 20 73 65 70 61 72 61 74 65 20 65 6c is a separate el
66480 65 6d 65 6e 74 20 6f 66 0a 2a 2a 20 74 68 65 20 ement of.** the
66490 53 72 63 4c 69 73 74 2e 61 5b 5d 20 61 72 72 61 SrcList.a[] arra
664a0 79 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68 y..**.** With th
664b0 65 20 61 64 64 69 74 69 6f 6e 20 6f 66 20 6d 75 e addition of mu
664c0 6c 74 69 70 6c 65 20 64 61 74 61 62 61 73 65 20 ltiple database
664d0 73 75 70 70 6f 72 74 2c 20 74 68 65 20 66 6f 6c support, the fol
664e0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
664f0 0a 2a 2a 20 63 61 6e 20 61 6c 73 6f 20 62 65 20 .** can also be
66500 75 73 65 64 20 74 6f 20 64 65 73 63 72 69 62 65 used to describe
66510 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 a particular ta
66520 62 6c 65 20 73 75 63 68 20 61 73 20 74 68 65 20 ble such as the
66530 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 table that.** is
66540 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e 20 modified by an
66550 49 4e 53 45 52 54 2c 20 44 45 4c 45 54 45 2c 20 INSERT, DELETE,
66560 6f 72 20 55 50 44 41 54 45 20 73 74 61 74 65 6d or UPDATE statem
66570 65 6e 74 2e 20 20 49 6e 20 73 74 61 6e 64 61 72 ent. In standar
66580 64 20 53 51 4c 2c 0a 2a 2a 20 73 75 63 68 20 61 d SQL,.** such a
66590 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61 table must be a
665a0 20 73 69 6d 70 6c 65 20 6e 61 6d 65 3a 20 49 44 simple name: ID
665b0 2e 20 20 42 75 74 20 69 6e 20 53 51 4c 69 74 65 . But in SQLite
665c0 2c 20 74 68 65 20 74 61 62 6c 65 20 63 61 6e 0a , the table can.
665d0 2a 2a 20 6e 6f 77 20 62 65 20 69 64 65 6e 74 69 ** now be identi
665e0 66 69 65 64 20 62 79 20 61 20 64 61 74 61 62 61 fied by a databa
665f0 73 65 20 6e 61 6d 65 2c 20 61 20 64 6f 74 2c 20 se name, a dot,
66600 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 6e then the table n
66610 61 6d 65 3a 20 49 44 2e 49 44 2e 0a 2a 2a 0a 2a ame: ID.ID..**.*
66620 2a 20 54 68 65 20 6a 6f 69 6e 74 79 70 65 20 73 * The jointype s
66630 74 61 72 74 73 20 6f 75 74 20 73 68 6f 77 69 6e tarts out showin
66640 67 20 74 68 65 20 6a 6f 69 6e 20 74 79 70 65 20 g the join type
66650 62 65 74 77 65 65 6e 20 74 68 65 20 63 75 72 72 between the curr
66660 65 6e 74 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 ent table.** and
66670 20 74 68 65 20 6e 65 78 74 20 74 61 62 6c 65 20 the next table
66680 6f 6e 20 74 68 65 20 6c 69 73 74 2e 20 20 54 68 on the list. Th
66690 65 20 70 61 72 73 65 72 20 62 75 69 6c 64 73 20 e parser builds
666a0 74 68 65 20 6c 69 73 74 20 74 68 69 73 20 77 61 the list this wa
666b0 79 2e 0a 2a 2a 20 42 75 74 20 73 71 6c 69 74 65 y..** But sqlite
666c0 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 3SrcListShiftJoi
666d0 6e 54 79 70 65 28 29 20 6c 61 74 65 72 20 73 68 nType() later sh
666e0 69 66 74 73 20 74 68 65 20 6a 6f 69 6e 74 79 70 ifts the jointyp
666f0 65 73 20 73 6f 20 74 68 61 74 20 65 61 63 68 0a es so that each.
66700 2a 2a 20 6a 6f 69 6e 74 79 70 65 20 65 78 70 72 ** jointype expr
66710 65 73 73 65 73 20 74 68 65 20 6a 6f 69 6e 20 62 esses the join b
66720 65 74 77 65 65 6e 20 74 68 65 20 74 61 62 6c 65 etween the table
66730 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 and the previou
66740 73 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 s table..*/.stru
66750 63 74 20 53 72 63 4c 69 73 74 20 7b 0a 20 20 69 ct SrcList {. i
66760 31 36 20 6e 53 72 63 3b 20 20 20 20 20 20 20 20 16 nSrc;
66770 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 /* Number of tab
66780 6c 65 73 20 6f 72 20 73 75 62 71 75 65 72 69 65 les or subquerie
66790 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c s in the FROM cl
667a0 61 75 73 65 20 2a 2f 0a 20 20 69 31 36 20 6e 41 ause */. i16 nA
667b0 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 4e 75 lloc; /* Nu
667c0 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
667d0 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61 5b 5d allocated in a[]
667e0 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 73 74 72 75 below */. stru
667f0 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
66800 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 61 74 {. char *zDat
66810 61 62 61 73 65 3b 20 20 2f 2a 20 4e 61 6d 65 20 abase; /* Name
66820 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64 of database hold
66830 69 6e 67 20 74 68 69 73 20 74 61 62 6c 65 20 2a ing this table *
66840 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d /. char *zNam
66850 65 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 e; /* Name
66860 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a of the table */.
66870 20 20 20 20 63 68 61 72 20 2a 7a 41 6c 69 61 73 char *zAlias
66880 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 22 42 22 ; /* The "B"
66890 20 70 61 72 74 20 6f 66 20 61 20 22 41 20 41 53 part of a "A AS
668a0 20 42 22 20 70 68 72 61 73 65 2e 20 20 7a 4e 61 B" phrase. zNa
668b0 6d 65 20 69 73 20 74 68 65 20 22 41 22 20 2a 2f me is the "A" */
668c0 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 . Table *pTab
668d0 3b 20 20 20 20 20 20 2f 2a 20 41 6e 20 53 51 4c ; /* An SQL
668e0 20 74 61 62 6c 65 20 63 6f 72 72 65 73 70 6f 6e table correspon
668f0 64 69 6e 67 20 74 6f 20 7a 4e 61 6d 65 20 2a 2f ding to zName */
66900 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65 . Select *pSe
66910 6c 65 63 74 3b 20 20 2f 2a 20 41 20 53 45 4c 45 lect; /* A SELE
66920 43 54 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65 CT statement use
66930 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 d in place of a
66940 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 table name */.
66950 20 20 75 38 20 69 73 50 6f 70 75 6c 61 74 65 64 u8 isPopulated
66960 3b 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 ; /* Temporary
66970 20 74 61 62 6c 65 20 61 73 73 6f 63 69 61 74 65 table associate
66980 64 20 77 69 74 68 20 53 45 4c 45 43 54 20 69 73 d with SELECT is
66990 20 70 6f 70 75 6c 61 74 65 64 20 2a 2f 0a 20 20 populated */.
669a0 20 20 75 38 20 6a 6f 69 6e 74 79 70 65 3b 20 20 u8 jointype;
669b0 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 6a /* Type of j
669c0 6f 69 6e 20 62 65 74 77 65 65 6e 20 74 68 69 73 oin between this
669d0 20 61 62 6c 65 20 61 6e 64 20 74 68 65 20 70 72 able and the pr
669e0 65 76 69 6f 75 73 20 2a 2f 0a 20 20 20 20 75 38 evious */. u8
669f0 20 6e 6f 74 49 6e 64 65 78 65 64 3b 20 20 20 20 notIndexed;
66a00 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65 /* True if there
66a10 20 69 73 20 61 20 4e 4f 54 20 49 4e 44 45 58 45 is a NOT INDEXE
66a20 44 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 D clause */.
66a30 69 6e 74 20 69 43 75 72 73 6f 72 3b 20 20 20 20 int iCursor;
66a40 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 /* The VDBE cu
66a50 72 73 6f 72 20 6e 75 6d 62 65 72 20 75 73 65 64 rsor number used
66a60 20 74 6f 20 61 63 63 65 73 73 20 74 68 69 73 20 to access this
66a70 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 45 78 70 table */. Exp
66a80 72 20 2a 70 4f 6e 3b 20 20 20 20 20 20 20 20 2f r *pOn; /
66a90 2a 20 54 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 * The ON clause
66aa0 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20 of a join */.
66ab0 20 49 64 4c 69 73 74 20 2a 70 55 73 69 6e 67 3b IdList *pUsing;
66ac0 20 20 20 2f 2a 20 54 68 65 20 55 53 49 4e 47 20 /* The USING
66ad0 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e clause of a join
66ae0 20 2a 2f 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 */. Bitmask
66af0 63 6f 6c 55 73 65 64 3b 20 20 2f 2a 20 42 69 74 colUsed; /* Bit
66b00 20 4e 20 28 31 3c 3c 4e 29 20 73 65 74 20 69 66 N (1<<N) set if
66b10 20 63 6f 6c 75 6d 6e 20 4e 20 6f 66 20 70 54 61 column N of pTa
66b20 62 20 69 73 20 75 73 65 64 20 2a 2f 0a 20 20 20 b is used */.
66b30 20 63 68 61 72 20 2a 7a 49 6e 64 65 78 3b 20 20 char *zIndex;
66b40 20 20 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 72 /* Identifier
66b50 20 66 72 6f 6d 20 22 49 4e 44 45 58 45 44 20 42 from "INDEXED B
66b60 59 20 3c 7a 49 6e 64 65 78 3e 22 20 63 6c 61 75 Y <zIndex>" clau
66b70 73 65 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 se */. Index
66b80 2a 70 49 6e 64 65 78 3b 20 20 20 20 2f 2a 20 49 *pIndex; /* I
66b90 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20 63 ndex structure c
66ba0 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 orresponding to
66bb0 7a 49 6e 64 65 78 2c 20 69 66 20 61 6e 79 20 2a zIndex, if any *
66bc0 2f 0a 20 20 7d 20 61 5b 31 5d 3b 20 20 20 20 20 /. } a[1];
66bd0 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 /* One e
66be0 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 69 64 ntry for each id
66bf0 65 6e 74 69 66 69 65 72 20 6f 6e 20 74 68 65 20 entifier on the
66c00 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a list */.};../*.*
66c10 2a 20 50 65 72 6d 69 74 74 65 64 20 76 61 6c 75 * Permitted valu
66c20 65 73 20 6f 66 20 74 68 65 20 53 72 63 4c 69 73 es of the SrcLis
66c30 74 2e 61 2e 6a 6f 69 6e 74 79 70 65 20 66 69 65 t.a.jointype fie
66c40 6c 64 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 ld.*/.#define JT
66c50 5f 49 4e 4e 45 52 20 20 20 20 20 30 78 30 30 30 _INNER 0x000
66c60 31 20 20 20 20 2f 2a 20 41 6e 79 20 6b 69 6e 64 1 /* Any kind
66c70 20 6f 66 20 69 6e 6e 65 72 20 6f 72 20 63 72 6f of inner or cro
66c80 73 73 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 ss join */.#defi
66c90 6e 65 20 4a 54 5f 43 52 4f 53 53 20 20 20 20 20 ne JT_CROSS
66ca0 30 78 30 30 30 32 20 20 20 20 2f 2a 20 45 78 70 0x0002 /* Exp
66cb0 6c 69 63 69 74 20 75 73 65 20 6f 66 20 74 68 65 licit use of the
66cc0 20 43 52 4f 53 53 20 6b 65 79 77 6f 72 64 20 2a CROSS keyword *
66cd0 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4e 41 54 /.#define JT_NAT
66ce0 55 52 41 4c 20 20 20 30 78 30 30 30 34 20 20 20 URAL 0x0004
66cf0 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 22 /* True for a "
66d00 6e 61 74 75 72 61 6c 22 20 6a 6f 69 6e 20 2a 2f natural" join */
66d10 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4c 45 46 54 .#define JT_LEFT
66d20 20 20 20 20 20 20 30 78 30 30 30 38 20 20 20 20 0x0008
66d30 2f 2a 20 4c 65 66 74 20 6f 75 74 65 72 20 6a 6f /* Left outer jo
66d40 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 in */.#define JT
66d50 5f 52 49 47 48 54 20 20 20 20 20 30 78 30 30 31 _RIGHT 0x001
66d60 30 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 75 0 /* Right ou
66d70 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 ter join */.#def
66d80 69 6e 65 20 4a 54 5f 4f 55 54 45 52 20 20 20 20 ine JT_OUTER
66d90 20 30 78 30 30 32 30 20 20 20 20 2f 2a 20 54 68 0x0020 /* Th
66da0 65 20 22 4f 55 54 45 52 22 20 6b 65 79 77 6f 72 e "OUTER" keywor
66db0 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a d is present */.
66dc0 23 64 65 66 69 6e 65 20 4a 54 5f 45 52 52 4f 52 #define JT_ERROR
66dd0 20 20 20 20 20 30 78 30 30 34 30 20 20 20 20 2f 0x0040 /
66de0 2a 20 75 6e 6b 6e 6f 77 6e 20 6f 72 20 75 6e 73 * unknown or uns
66df0 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79 upported join ty
66e00 70 65 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 pe */.../*.** A
66e10 57 68 65 72 65 50 6c 61 6e 20 6f 62 6a 65 63 74 WherePlan object
66e20 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 holds informati
66e30 6f 6e 20 74 68 61 74 20 64 65 73 63 72 69 62 65 on that describe
66e40 73 20 61 20 6c 6f 6f 6b 75 70 0a 2a 2a 20 73 74 s a lookup.** st
66e50 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 rategy..**.** Th
66e60 69 73 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 74 is object is int
66e70 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71 ended to be opaq
66e80 75 65 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 ue outside of th
66e90 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 e where.c module
66ea0 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6e 63 6c 75 ..** It is inclu
66eb0 64 65 64 20 68 65 72 65 20 6f 6e 6c 79 20 73 6f ded here only so
66ec0 20 74 68 61 74 20 74 68 61 74 20 63 6f 6d 70 69 that that compi
66ed0 6c 65 72 20 77 69 6c 6c 20 6b 6e 6f 77 20 68 6f ler will know ho
66ee0 77 20 62 69 67 20 69 74 0a 2a 2a 20 69 73 2e 20 w big it.** is.
66ef0 20 4e 6f 6e 65 20 6f 66 20 74 68 65 20 66 69 65 None of the fie
66f00 6c 64 73 20 69 6e 20 74 68 69 73 20 6f 62 6a 65 lds in this obje
66f10 63 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 ct should be use
66f20 64 20 6f 75 74 73 69 64 65 20 6f 66 0a 2a 2a 20 d outside of.**
66f30 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75 the where.c modu
66f40 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69 6e le..**.** Within
66f50 20 74 68 65 20 75 6e 69 6f 6e 2c 20 70 49 64 78 the union, pIdx
66f60 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 is only used wh
66f70 65 6e 20 77 73 46 6c 61 67 73 26 57 48 45 52 45 en wsFlags&WHERE
66f80 5f 49 4e 44 45 58 45 44 20 69 73 20 74 72 75 65 _INDEXED is true
66f90 2e 0a 2a 2a 20 70 54 65 72 6d 20 69 73 20 6f 6e ..** pTerm is on
66fa0 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 46 ly used when wsF
66fb0 6c 61 67 73 26 57 48 45 52 45 5f 4d 55 4c 54 49 lags&WHERE_MULTI
66fc0 5f 4f 52 20 69 73 20 74 72 75 65 2e 20 20 41 6e _OR is true. An
66fd0 64 20 70 56 74 61 62 49 64 78 0a 2a 2a 20 69 73 d pVtabIdx.** is
66fe0 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 only used when
66ff0 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 56 49 wsFlags&WHERE_VI
67000 52 54 55 41 4c 54 41 42 4c 45 20 69 73 20 74 72 RTUALTABLE is tr
67010 75 65 2e 20 20 49 74 20 69 73 20 6e 65 76 65 72 ue. It is never
67020 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 the.** case tha
67030 74 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 t more than one
67040 6f 66 20 74 68 65 73 65 20 63 6f 6e 64 69 74 69 of these conditi
67050 6f 6e 73 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a ons is true..*/.
67060 73 74 72 75 63 74 20 57 68 65 72 65 50 6c 61 6e struct WherePlan
67070 20 7b 0a 20 20 75 33 32 20 77 73 46 6c 61 67 73 {. u32 wsFlags
67080 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
67090 20 20 20 20 2f 2a 20 57 48 45 52 45 5f 2a 20 66 /* WHERE_* f
670a0 6c 61 67 73 20 74 68 61 74 20 64 65 73 63 72 69 lags that descri
670b0 62 65 20 74 68 65 20 73 74 72 61 74 65 67 79 20 be the strategy
670c0 2a 2f 0a 20 20 75 33 32 20 6e 45 71 3b 20 20 20 */. u32 nEq;
670d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
670e0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
670f0 20 3d 3d 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 == constraints
67100 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 */. union {.
67110 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 Index *pIdx;
67120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67130 2f 2a 20 49 6e 64 65 78 20 77 68 65 6e 20 57 48 /* Index when WH
67140 45 52 45 5f 49 4e 44 45 58 45 44 20 69 73 20 74 ERE_INDEXED is t
67150 72 75 65 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 rue */. struc
67160 74 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 t WhereTerm *pTe
67170 72 6d 3b 20 20 20 20 20 20 20 2f 2a 20 57 48 45 rm; /* WHE
67180 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 20 66 RE clause term f
67190 6f 72 20 4f 52 2d 73 65 61 72 63 68 20 2a 2f 0a or OR-search */.
671a0 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 sqlite3_inde
671b0 78 5f 69 6e 66 6f 20 2a 70 56 74 61 62 49 64 78 x_info *pVtabIdx
671c0 3b 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 ; /* Virtual ta
671d0 62 6c 65 20 69 6e 64 65 78 20 74 6f 20 75 73 65 ble index to use
671e0 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f */. } u;.};../
671f0 2a 0a 2a 2a 20 46 6f 72 20 65 61 63 68 20 6e 65 *.** For each ne
67200 73 74 65 64 20 6c 6f 6f 70 20 69 6e 20 61 20 57 sted loop in a W
67210 48 45 52 45 20 63 6c 61 75 73 65 20 69 6d 70 6c HERE clause impl
67220 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 74 68 65 20 ementation, the
67230 57 68 65 72 65 49 6e 66 6f 0a 2a 2a 20 73 74 72 WhereInfo.** str
67240 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 ucture contains
67250 61 20 73 69 6e 67 6c 65 20 69 6e 73 74 61 6e 63 a single instanc
67260 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 e of this struct
67270 75 72 65 2e 20 20 54 68 69 73 20 73 74 72 75 63 ure. This struc
67280 74 75 72 65 0a 2a 2a 20 69 73 20 69 6e 74 65 6e ture.** is inten
67290 64 65 64 20 74 6f 20 62 65 20 70 72 69 76 61 74 ded to be privat
672a0 65 20 74 68 65 20 74 68 65 20 77 68 65 72 65 2e e the the where.
672b0 63 20 6d 6f 64 75 6c 65 20 61 6e 64 20 73 68 6f c module and sho
672c0 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 63 uld not be.** ac
672d0 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 69 65 64 cess or modified
672e0 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 by other module
672f0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 49 64 s..**.** The pId
67300 78 49 6e 66 6f 20 66 69 65 6c 64 20 69 73 20 75 xInfo field is u
67310 73 65 64 20 74 6f 20 68 65 6c 70 20 70 69 63 6b sed to help pick
67320 20 74 68 65 20 62 65 73 74 20 69 6e 64 65 78 20 the best index
67330 6f 6e 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 on a.** virtual
67340 74 61 62 6c 65 2e 20 20 54 68 65 20 70 49 64 78 table. The pIdx
67350 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20 63 6f 6e Info pointer con
67360 74 61 69 6e 73 20 69 6e 64 65 78 69 6e 67 0a 2a tains indexing.*
67370 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f * information fo
67380 72 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c 65 r the i-th table
67390 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 in the FROM cla
673a0 75 73 65 20 62 65 66 6f 72 65 20 72 65 6f 72 64 use before reord
673b0 65 72 69 6e 67 2e 0a 2a 2a 20 41 6c 6c 20 74 68 ering..** All th
673c0 65 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74 e pIdxInfo point
673d0 65 72 73 20 61 72 65 20 66 72 65 65 64 20 62 79 ers are freed by
673e0 20 77 68 65 72 65 49 6e 66 6f 46 72 65 65 28 29 whereInfoFree()
673f0 20 69 6e 20 77 68 65 72 65 2e 63 2e 0a 2a 2a 20 in where.c..**
67400 41 6c 6c 20 6f 74 68 65 72 20 69 6e 66 6f 72 6d All other inform
67410 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 69 2d 74 ation in the i-t
67420 68 20 57 68 65 72 65 4c 65 76 65 6c 20 6f 62 6a h WhereLevel obj
67430 65 63 74 20 66 6f 72 20 74 68 65 20 69 2d 74 68 ect for the i-th
67440 20 74 61 62 6c 65 0a 2a 2a 20 61 66 74 65 72 20 table.** after
67450 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 72 64 65 FROM clause orde
67460 72 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ring..*/.struct
67470 57 68 65 72 65 4c 65 76 65 6c 20 7b 0a 20 20 57 WhereLevel {. W
67480 68 65 72 65 50 6c 61 6e 20 70 6c 61 6e 3b 20 20 herePlan plan;
67490 20 20 20 20 20 2f 2a 20 71 75 65 72 79 20 70 6c /* query pl
674a0 61 6e 20 66 6f 72 20 74 68 69 73 20 65 6c 65 6d an for this elem
674b0 65 6e 74 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 ent of the FROM
674c0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 clause */. int
674d0 69 4c 65 66 74 4a 6f 69 6e 3b 20 20 20 20 20 20 iLeftJoin;
674e0 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c /* Memory cell
674f0 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 used to impleme
67500 6e 74 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f nt LEFT OUTER JO
67510 49 4e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 IN */. int iTab
67520 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a Cur; /*
67530 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 The VDBE cursor
67540 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 used to access
67550 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 the table */. i
67560 6e 74 20 69 49 64 78 43 75 72 3b 20 20 20 20 20 nt iIdxCur;
67570 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 /* The VDBE
67580 20 63 75 72 73 6f 72 20 75 73 65 64 20 74 6f 20 cursor used to
67590 61 63 63 65 73 73 20 70 49 64 78 20 2a 2f 0a 20 access pIdx */.
675a0 20 69 6e 74 20 61 64 64 72 42 72 6b 3b 20 20 20 int addrBrk;
675b0 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 /* Jump h
675c0 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 ere to break out
675d0 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a of the loop */.
675e0 20 20 69 6e 74 20 61 64 64 72 4e 78 74 3b 20 20 int addrNxt;
675f0 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 /* Jump
67600 68 65 72 65 20 74 6f 20 73 74 61 72 74 20 74 68 here to start th
67610 65 20 6e 65 78 74 20 49 4e 20 63 6f 6d 62 69 6e e next IN combin
67620 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 ation */. int a
67630 64 64 72 43 6f 6e 74 3b 20 20 20 20 20 20 20 20 ddrCont;
67640 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f /* Jump here to
67650 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 74 continue with t
67660 68 65 20 6e 65 78 74 20 6c 6f 6f 70 20 63 79 63 he next loop cyc
67670 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 le */. int addr
67680 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 2f 2a First; /*
67690 20 46 69 72 73 74 20 69 6e 73 74 72 75 63 74 69 First instructi
676a0 6f 6e 20 6f 66 20 69 6e 74 65 72 69 6f 72 20 6f on of interior o
676b0 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 f the loop */.
676c0 75 38 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 20 u8 iFrom;
676d0 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20 65 /* Which e
676e0 6e 74 72 79 20 69 6e 20 74 68 65 20 46 52 4f 4d ntry in the FROM
676f0 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 75 38 20 clause */. u8
67700 6f 70 2c 20 70 35 3b 20 20 20 20 20 20 20 20 20 op, p5;
67710 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20 61 6e 64 /* Opcode and
67720 20 50 35 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 P5 of the opcod
67730 65 20 74 68 61 74 20 65 6e 64 73 20 74 68 65 20 e that ends the
67740 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 70 31 loop */. int p1
67750 2c 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 , p2;
67760 2f 2a 20 4f 70 65 72 61 6e 64 73 20 6f 66 20 74 /* Operands of t
67770 68 65 20 6f 70 63 6f 64 65 20 75 73 65 64 20 74 he opcode used t
67780 6f 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70 20 o ends the loop
67790 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 */. union {
677a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
677b0 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 64 formation that d
677c0 65 70 65 6e 64 73 20 6f 6e 20 70 6c 61 6e 2e 77 epends on plan.w
677d0 73 46 6c 61 67 73 20 2a 2f 0a 20 20 20 20 73 74 sFlags */. st
677e0 72 75 63 74 20 7b 0a 20 20 20 20 20 20 69 6e 74 ruct {. int
677f0 20 6e 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20 nIn;
67800 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
67810 65 6e 74 72 69 65 73 20 69 6e 20 61 49 6e 4c 6f entries in aInLo
67820 6f 70 5b 5d 20 2a 2f 0a 20 20 20 20 20 20 73 74 op[] */. st
67830 72 75 63 74 20 49 6e 4c 6f 6f 70 20 7b 0a 20 20 ruct InLoop {.
67840 20 20 20 20 20 20 69 6e 74 20 69 43 75 72 3b 20 int iCur;
67850 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
67860 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 The VDBE cursor
67870 75 73 65 64 20 62 79 20 74 68 69 73 20 49 4e 20 used by this IN
67880 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 operator */.
67890 20 20 20 20 69 6e 74 20 61 64 64 72 49 6e 54 6f int addrInTo
678a0 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f p; /* To
678b0 70 20 6f 66 20 74 68 65 20 49 4e 20 6c 6f 6f 70 p of the IN loop
678c0 20 2a 2f 0a 20 20 20 20 20 20 7d 20 2a 61 49 6e */. } *aIn
678d0 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 Loop;
678e0 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 /* Information a
678f0 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 65 64 bout each nested
67900 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a IN operator */.
67910 20 20 20 20 7d 20 69 6e 3b 20 20 20 20 20 20 20 } in;
67920 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 /* Use
67930 64 20 77 68 65 6e 20 70 6c 61 6e 2e 77 73 46 6c d when plan.wsFl
67940 61 67 73 26 57 48 45 52 45 5f 49 4e 5f 41 42 4c ags&WHERE_IN_ABL
67950 45 20 2a 2f 0a 20 20 7d 20 75 3b 0a 0a 20 20 2f E */. } u;.. /
67960 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
67970 66 69 65 6c 64 20 69 73 20 72 65 61 6c 6c 79 20 field is really
67980 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 not part of the
67990 63 75 72 72 65 6e 74 20 6c 65 76 65 6c 2e 20 20 current level.
679a0 42 75 74 0a 20 20 2a 2a 20 77 65 20 6e 65 65 64 But. ** we need
679b0 20 61 20 70 6c 61 63 65 20 74 6f 20 63 61 63 68 a place to cach
679c0 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 e virtual table
679d0 69 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74 69 6f index informatio
679e0 6e 20 66 6f 72 20 65 61 63 68 0a 20 20 2a 2a 20 n for each. **
679f0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e virtual table in
67a00 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 the FROM clause
67a10 20 61 6e 64 20 74 68 65 20 57 68 65 72 65 4c 65 and the WhereLe
67a20 76 65 6c 20 73 74 72 75 63 74 75 72 65 20 69 73 vel structure is
67a30 0a 20 20 2a 2a 20 61 20 63 6f 6e 76 65 6e 69 65 . ** a convenie
67a40 6e 74 20 70 6c 61 63 65 20 73 69 6e 63 65 20 74 nt place since t
67a50 68 65 72 65 20 69 73 20 6f 6e 65 20 57 68 65 72 here is one Wher
67a60 65 4c 65 76 65 6c 20 66 6f 72 20 65 61 63 68 20 eLevel for each
67a70 46 52 4f 4d 20 63 6c 61 75 73 65 0a 20 20 2a 2a FROM clause. **
67a80 20 65 6c 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 element.. */.
67a90 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 sqlite3_index_i
67aa0 6e 66 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 20 20 nfo *pIdxInfo;
67ab0 2f 2a 20 49 6e 64 65 78 20 69 6e 66 6f 20 66 6f /* Index info fo
67ac0 72 20 6e 2d 74 68 20 73 6f 75 72 63 65 20 74 61 r n-th source ta
67ad0 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ble */.};../*.**
67ae0 20 46 6c 61 67 73 20 61 70 70 72 6f 70 72 69 61 Flags appropria
67af0 74 65 20 66 6f 72 20 74 68 65 20 77 63 74 72 6c te for the wctrl
67b00 46 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 Flags parameter
67b10 6f 66 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 of sqlite3WhereB
67b20 65 67 69 6e 28 29 0a 2a 2a 20 61 6e 64 20 74 68 egin().** and th
67b30 65 20 57 68 65 72 65 49 6e 66 6f 2e 77 63 74 72 e WhereInfo.wctr
67b40 6c 46 6c 61 67 73 20 6d 65 6d 62 65 72 2e 0a 2a lFlags member..*
67b50 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f /.#define WHERE_
67b60 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 20 20 ORDERBY_NORMAL
67b70 20 30 78 30 30 30 30 20 2f 2a 20 4e 6f 2d 6f 70 0x0000 /* No-op
67b80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 */.#define WHER
67b90 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 20 20 20 E_ORDERBY_MIN
67ba0 20 20 20 30 78 30 30 30 31 20 2f 2a 20 4f 52 44 0x0001 /* ORD
67bb0 45 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e 67 ER BY processing
67bc0 20 66 6f 72 20 6d 69 6e 28 29 20 66 75 6e 63 20 for min() func
67bd0 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 */.#define WHERE
67be0 5f 4f 52 44 45 52 42 59 5f 4d 41 58 20 20 20 20 _ORDERBY_MAX
67bf0 20 20 30 78 30 30 30 32 20 2f 2a 20 4f 52 44 45 0x0002 /* ORDE
67c00 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e 67 20 R BY processing
67c10 66 6f 72 20 6d 61 78 28 29 20 66 75 6e 63 20 2a for max() func *
67c20 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f /.#define WHERE_
67c30 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44 20 ONEPASS_DESIRED
67c40 20 30 78 30 30 30 34 20 2f 2a 20 57 61 6e 74 20 0x0004 /* Want
67c50 74 6f 20 64 6f 20 6f 6e 65 2d 70 61 73 73 20 55 to do one-pass U
67c60 50 44 41 54 45 2f 44 45 4c 45 54 45 20 2a 2f 0a PDATE/DELETE */.
67c70 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 44 55 #define WHERE_DU
67c80 50 4c 49 43 41 54 45 53 5f 4f 4b 20 20 20 20 30 PLICATES_OK 0
67c90 78 30 30 30 38 20 2f 2a 20 4f 6b 20 74 6f 20 72 x0008 /* Ok to r
67ca0 65 74 75 72 6e 20 61 20 72 6f 77 20 6d 6f 72 65 eturn a row more
67cb0 20 74 68 61 6e 20 6f 6e 63 65 20 2a 2f 0a 23 64 than once */.#d
67cc0 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 4d 49 54 efine WHERE_OMIT
67cd0 5f 4f 50 45 4e 20 20 20 20 20 20 20 20 30 78 30 _OPEN 0x0
67ce0 30 31 30 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 010 /* Table cur
67cf0 73 6f 72 20 61 72 65 20 61 6c 72 65 61 64 79 20 sor are already
67d00 6f 70 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 open */.#define
67d10 57 48 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 WHERE_OMIT_CLOSE
67d20 20 20 20 20 20 20 20 30 78 30 30 32 30 20 2f 2a 0x0020 /*
67d30 20 4f 6d 69 74 20 63 6c 6f 73 65 20 6f 66 20 74 Omit close of t
67d40 61 62 6c 65 20 26 20 69 6e 64 65 78 20 63 75 72 able & index cur
67d50 73 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 sors */.#define
67d60 57 48 45 52 45 5f 46 4f 52 43 45 5f 54 41 42 4c WHERE_FORCE_TABL
67d70 45 20 20 20 20 20 20 30 78 30 30 34 30 20 2f 2a E 0x0040 /*
67d80 20 44 6f 20 6e 6f 74 20 75 73 65 20 61 6e 20 69 Do not use an i
67d90 6e 64 65 78 2d 6f 6e 6c 79 20 73 65 61 72 63 68 ndex-only search
67da0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 57 */../*.** The W
67db0 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63 HERE clause proc
67dc0 65 73 73 69 6e 67 20 72 6f 75 74 69 6e 65 20 68 essing routine h
67dd0 61 73 20 74 77 6f 20 68 61 6c 76 65 73 2e 20 20 as two halves.
67de0 54 68 65 0a 2a 2a 20 66 69 72 73 74 20 70 61 72 The.** first par
67df0 74 20 64 6f 65 73 20 74 68 65 20 73 74 61 72 74 t does the start
67e00 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c 6f of the WHERE lo
67e10 6f 70 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e op and the secon
67e20 64 0a 2a 2a 20 68 61 6c 66 20 64 6f 65 73 20 74 d.** half does t
67e30 68 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20 57 he tail of the W
67e40 48 45 52 45 20 6c 6f 6f 70 2e 20 20 41 6e 20 69 HERE loop. An i
67e50 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 nstance of.** th
67e60 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 is structure is
67e70 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 returned by the
67e80 66 69 72 73 74 20 68 61 6c 66 20 61 6e 64 20 70 first half and p
67e90 61 73 73 65 64 0a 2a 2a 20 69 6e 74 6f 20 74 68 assed.** into th
67ea0 65 20 73 65 63 6f 6e 64 20 68 61 6c 66 20 74 6f e second half to
67eb0 20 67 69 76 65 20 73 6f 6d 65 20 63 6f 6e 74 69 give some conti
67ec0 6e 75 69 74 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 nuity..*/.struct
67ed0 20 57 68 65 72 65 49 6e 66 6f 20 7b 0a 20 20 50 WhereInfo {. P
67ee0 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 arse *pParse;
67ef0 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 /* Parsing a
67f00 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 nd code generati
67f10 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
67f20 75 31 36 20 77 63 74 72 6c 46 6c 61 67 73 3b 20 u16 wctrlFlags;
67f30 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 6f 72 /* Flags or
67f40 69 67 69 6e 61 6c 6c 79 20 70 61 73 73 65 64 20 iginally passed
67f50 74 6f 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 to sqlite3WhereB
67f60 65 67 69 6e 28 29 20 2a 2f 0a 20 20 75 38 20 6f egin() */. u8 o
67f70 6b 4f 6e 65 50 61 73 73 3b 20 20 20 20 20 20 20 kOnePass;
67f80 20 2f 2a 20 4f 6b 20 74 6f 20 75 73 65 20 6f 6e /* Ok to use on
67f90 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d e-pass algorithm
67fa0 20 66 6f 72 20 55 50 44 41 54 45 20 6f 72 20 44 for UPDATE or D
67fb0 45 4c 45 54 45 20 2a 2f 0a 20 20 53 72 63 4c 69 ELETE */. SrcLi
67fc0 73 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20 st *pTabList;
67fd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 /* Lis
67fe0 74 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 t of tables in t
67ff0 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 69 6e 74 he join */. int
68000 20 69 54 6f 70 3b 20 20 20 20 20 20 20 20 20 20 iTop;
68010 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
68020 68 65 20 76 65 72 79 20 62 65 67 69 6e 6e 69 6e he very beginnin
68030 67 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c g of the WHERE l
68040 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f oop */. int iCo
68050 6e 74 69 6e 75 65 3b 20 20 20 20 20 20 20 20 20 ntinue;
68060 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 /* Jump
68070 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 here to continue
68080 20 77 69 74 68 20 6e 65 78 74 20 72 65 63 6f 72 with next recor
68090 64 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 61 d */. int iBrea
680a0 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k;
680b0 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 /* Jump he
680c0 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 re to break out
680d0 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 of the loop */.
680e0 20 69 6e 74 20 6e 4c 65 76 65 6c 3b 20 20 20 20 int nLevel;
680f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68100 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 /* Number of nes
68110 74 65 64 20 6c 6f 6f 70 20 2a 2f 0a 20 20 73 74 ted loop */. st
68120 72 75 63 74 20 57 68 65 72 65 43 6c 61 75 73 65 ruct WhereClause
68130 20 2a 70 57 43 3b 20 20 20 20 20 20 20 2f 2a 20 *pWC; /*
68140 44 65 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 Decomposition of
68150 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 the WHERE claus
68160 65 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 e */. WhereLeve
68170 6c 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 l a[1];
68180 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 /* Informa
68190 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 tion about each
681a0 6e 65 73 74 20 6c 6f 6f 70 20 69 6e 20 57 48 45 nest loop in WHE
681b0 52 45 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 RE */.};../*.**
681c0 41 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 64 65 A NameContext de
681d0 66 69 6e 65 73 20 61 20 63 6f 6e 74 65 78 74 20 fines a context
681e0 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65 73 6f in which to reso
681f0 6c 76 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f lve table and co
68200 6c 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 73 2e 20 20 lumn.** names.
68210 54 68 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 73 The context cons
68220 69 73 74 73 20 6f 66 20 61 20 6c 69 73 74 20 6f ists of a list o
68230 66 20 74 61 62 6c 65 73 20 28 74 68 65 20 70 53 f tables (the pS
68240 72 63 4c 69 73 74 29 20 66 69 65 6c 64 20 61 6e rcList) field an
68250 64 0a 2a 2a 20 61 20 6c 69 73 74 20 6f 66 20 6e d.** a list of n
68260 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 amed expression
68270 28 70 45 4c 69 73 74 29 2e 20 20 54 68 65 20 6e (pEList). The n
68280 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 amed expression
68290 6c 69 73 74 20 6d 61 79 0a 2a 2a 20 62 65 20 4e list may.** be N
682a0 55 4c 4c 2e 20 20 54 68 65 20 70 53 72 63 20 63 ULL. The pSrc c
682b0 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 orresponds to th
682c0 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 e FROM clause of
682d0 20 61 20 53 45 4c 45 43 54 20 6f 72 0a 2a 2a 20 a SELECT or.**
682e0 74 6f 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 to the table bei
682f0 6e 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 20 62 ng operated on b
68300 79 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 y INSERT, UPDATE
68310 2c 20 6f 72 20 44 45 4c 45 54 45 2e 20 20 54 68 , or DELETE. Th
68320 65 0a 2a 2a 20 70 45 4c 69 73 74 20 63 6f 72 72 e.** pEList corr
68330 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 72 esponds to the r
68340 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53 esult set of a S
68350 45 4c 45 43 54 20 61 6e 64 20 69 73 20 4e 55 4c ELECT and is NUL
68360 4c 20 66 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 73 L for.** other s
68370 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a tatements..**.**
68380 20 4e 61 6d 65 43 6f 6e 74 65 78 74 73 20 63 61 NameContexts ca
68390 6e 20 62 65 20 6e 65 73 74 65 64 2e 20 20 57 68 n be nested. Wh
683a0 65 6e 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d en resolving nam
683b0 65 73 2c 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f es, the inner-mo
683c0 73 74 20 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 st .** context i
683d0 73 20 73 65 61 72 63 68 65 64 20 66 69 72 73 74 s searched first
683e0 2e 20 20 49 66 20 6e 6f 20 6d 61 74 63 68 20 69 . If no match i
683f0 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 65 78 s found, the nex
68400 74 20 6f 75 74 65 72 0a 2a 2a 20 63 6f 6e 74 65 t outer.** conte
68410 78 74 20 69 73 20 63 68 65 63 6b 65 64 2e 20 20 xt is checked.
68420 49 66 20 74 68 65 72 65 20 69 73 20 73 74 69 6c If there is stil
68430 6c 20 6e 6f 20 6d 61 74 63 68 2c 20 74 68 65 20 l no match, the
68440 6e 65 78 74 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 next context.**
68450 69 73 20 63 68 65 63 6b 65 64 2e 20 20 54 68 69 is checked. Thi
68460 73 20 70 72 6f 63 65 73 73 20 63 6f 6e 74 69 6e s process contin
68470 75 65 73 20 75 6e 74 69 6c 20 65 69 74 68 65 72 ues until either
68480 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e a match is foun
68490 64 0a 2a 2a 20 6f 72 20 61 6c 6c 20 63 6f 6e 74 d.** or all cont
684a0 65 78 74 73 20 61 72 65 20 63 68 65 63 6b 2e 20 exts are check.
684b0 20 57 68 65 6e 20 61 20 6d 61 74 63 68 20 69 73 When a match is
684c0 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 52 65 66 found, the nRef
684d0 20 6d 65 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 68 member of.** th
684e0 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 74 61 69 e context contai
684f0 6e 69 6e 67 20 74 68 65 20 6d 61 74 63 68 20 69 ning the match i
68500 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 20 0a s incremented. .
68510 2a 2a 0a 2a 2a 20 45 61 63 68 20 73 75 62 71 75 **.** Each subqu
68520 65 72 79 20 67 65 74 73 20 61 20 6e 65 77 20 4e ery gets a new N
68530 61 6d 65 43 6f 6e 74 65 78 74 2e 20 20 54 68 65 ameContext. The
68540 20 70 4e 65 78 74 20 66 69 65 6c 64 20 70 6f 69 pNext field poi
68550 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 4e 61 nts to the.** Na
68560 6d 65 43 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 meContext in the
68570 20 70 61 72 65 6e 74 20 71 75 65 72 79 2e 20 20 parent query.
68580 54 68 75 73 20 74 68 65 20 70 72 6f 63 65 73 73 Thus the process
68590 20 6f 66 20 73 63 61 6e 6e 69 6e 67 20 74 68 65 of scanning the
685a0 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 .** NameContext
685b0 6c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 list corresponds
685c0 20 74 6f 20 73 65 61 72 63 68 69 6e 67 20 74 68 to searching th
685d0 72 6f 75 67 68 20 73 75 63 63 65 73 73 69 76 65 rough successive
685e0 6c 79 20 6f 75 74 65 72 0a 2a 2a 20 73 75 62 71 ly outer.** subq
685f0 75 65 72 69 65 73 20 6c 6f 6f 6b 69 6e 67 20 66 ueries looking f
68600 6f 72 20 61 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 or a match..*/.s
68610 74 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78 truct NameContex
68620 74 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 t {. Parse *pPa
68630 72 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 rse; /* Th
68640 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 53 72 e parser */. Sr
68650 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 3b cList *pSrcList;
68660 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 /* One or mor
68670 65 20 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f e tables used to
68680 20 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 2a resolve names *
68690 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 /. ExprList *pE
686a0 4c 69 73 74 3b 20 20 20 20 2f 2a 20 4f 70 74 69 List; /* Opti
686b0 6f 6e 61 6c 20 6c 69 73 74 20 6f 66 20 6e 61 6d onal list of nam
686c0 65 64 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a ed expressions *
686d0 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 /. int nRef;
686e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
686f0 65 72 20 6f 66 20 6e 61 6d 65 73 20 72 65 73 6f er of names reso
68700 6c 76 65 64 20 62 79 20 74 68 69 73 20 63 6f 6e lved by this con
68710 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 text */. int nE
68720 72 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f rr; /
68730 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f * Number of erro
68740 72 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 rs encountered w
68750 68 69 6c 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e hile resolving n
68760 61 6d 65 73 20 2a 2f 0a 20 20 75 38 20 61 6c 6c ames */. u8 all
68770 6f 77 41 67 67 3b 20 20 20 20 20 20 20 20 20 2f owAgg; /
68780 2a 20 41 67 67 72 65 67 61 74 65 20 66 75 6e 63 * Aggregate func
68790 74 69 6f 6e 73 20 61 6c 6c 6f 77 65 64 20 68 65 tions allowed he
687a0 72 65 20 2a 2f 0a 20 20 75 38 20 68 61 73 41 67 re */. u8 hasAg
687b0 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 g; /*
687c0 54 72 75 65 20 69 66 20 61 67 67 72 65 67 61 74 True if aggregat
687d0 65 73 20 61 72 65 20 73 65 65 6e 20 2a 2f 0a 20 es are seen */.
687e0 20 75 38 20 69 73 43 68 65 63 6b 3b 20 20 20 20 u8 isCheck;
687f0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
68800 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 resolving names
68810 20 69 6e 20 61 20 43 48 45 43 4b 20 63 6f 6e 73 in a CHECK cons
68820 74 72 61 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 traint */. int
68830 6e 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 nDepth;
68840 20 2f 2a 20 44 65 70 74 68 20 6f 66 20 73 75 62 /* Depth of sub
68850 71 75 65 72 79 20 72 65 63 75 72 73 69 6f 6e 2e query recursion.
68860 20 31 20 66 6f 72 20 6e 6f 20 72 65 63 75 72 73 1 for no recurs
68870 69 6f 6e 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f ion */. AggInfo
68880 20 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 2f 2a *pAggInfo; /*
68890 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f Information abo
688a0 75 74 20 61 67 67 72 65 67 61 74 65 73 20 61 74 ut aggregates at
688b0 20 74 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 this level */.
688c0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e NameContext *pN
688d0 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 75 ext; /* Next ou
688e0 74 65 72 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 ter name context
688f0 2e 20 20 4e 55 4c 4c 20 66 6f 72 20 6f 75 74 65 . NULL for oute
68900 72 6d 6f 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a rmost */.};../*.
68910 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
68920 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
68930 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 structure contai
68940 6e 73 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 ns all informati
68950 6f 6e 0a 2a 2a 20 6e 65 65 64 65 64 20 74 6f 20 on.** needed to
68960 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f generate code fo
68970 72 20 61 20 73 69 6e 67 6c 65 20 53 45 4c 45 43 r a single SELEC
68980 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a T statement..**.
68990 2a 2a 20 6e 4c 69 6d 69 74 20 69 73 20 73 65 74 ** nLimit is set
689a0 20 74 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20 to -1 if there
689b0 69 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 is no LIMIT clau
689c0 73 65 2e 20 20 6e 4f 66 66 73 65 74 20 69 73 20 se. nOffset is
689d0 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 49 66 20 set to 0..** If
689e0 74 68 65 72 65 20 69 73 20 61 20 4c 49 4d 49 54 there is a LIMIT
689f0 20 63 6c 61 75 73 65 2c 20 74 68 65 20 70 61 72 clause, the par
68a00 73 65 72 20 73 65 74 73 20 6e 4c 69 6d 69 74 20 ser sets nLimit
68a10 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 to the value of
68a20 74 68 65 0a 2a 2a 20 6c 69 6d 69 74 20 61 6e 64 the.** limit and
68a30 20 6e 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 nOffset to the
68a40 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 66 66 value of the off
68a50 73 65 74 20 28 6f 72 20 30 20 69 66 20 74 68 65 set (or 0 if the
68a60 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 6f 66 66 re is not.** off
68a70 73 65 74 29 2e 20 20 42 75 74 20 6c 61 74 65 72 set). But later
68a80 20 6f 6e 2c 20 6e 4c 69 6d 69 74 20 61 6e 64 20 on, nLimit and
68a90 6e 4f 66 66 73 65 74 20 62 65 63 6f 6d 65 20 74 nOffset become t
68aa0 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 he memory locati
68ab0 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 56 44 ons.** in the VD
68ac0 42 45 20 74 68 61 74 20 72 65 63 6f 72 64 20 74 BE that record t
68ad0 68 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66 he limit and off
68ae0 73 65 74 20 63 6f 75 6e 74 65 72 73 2e 0a 2a 2a set counters..**
68af0 0a 2a 2a 20 61 64 64 72 4f 70 65 6e 45 70 68 6d .** addrOpenEphm
68b00 5b 5d 20 65 6e 74 72 69 65 73 20 63 6f 6e 74 61 [] entries conta
68b10 69 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 6f in the address o
68b20 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 f OP_OpenEphemer
68b30 61 6c 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a 20 54 al opcodes..** T
68b40 68 65 73 65 20 61 64 64 72 65 73 73 65 73 20 6d hese addresses m
68b50 75 73 74 20 62 65 20 73 74 6f 72 65 64 20 73 6f ust be stored so
68b60 20 74 68 61 74 20 77 65 20 63 61 6e 20 67 6f 20 that we can go
68b70 62 61 63 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e back and fill in
68b80 0a 2a 2a 20 74 68 65 20 50 34 5f 4b 45 59 49 4e .** the P4_KEYIN
68b90 46 4f 20 61 6e 64 20 50 32 20 70 61 72 61 6d 65 FO and P2 parame
68ba0 74 65 72 73 20 6c 61 74 65 72 2e 20 20 4e 65 69 ters later. Nei
68bb0 74 68 65 72 20 74 68 65 20 4b 65 79 49 6e 66 6f ther the KeyInfo
68bc0 20 6e 6f 72 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 nor.** the numb
68bd0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
68be0 20 50 32 20 63 61 6e 20 62 65 20 63 6f 6d 70 75 P2 can be compu
68bf0 74 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 ted at the same
68c00 74 69 6d 65 0a 2a 2a 20 61 73 20 74 68 65 20 4f time.** as the O
68c10 50 5f 4f 70 65 6e 45 70 68 6d 20 69 6e 73 74 72 P_OpenEphm instr
68c20 75 63 74 69 6f 6e 20 69 73 20 63 6f 64 65 64 20 uction is coded
68c30 62 65 63 61 75 73 65 20 6e 6f 74 0a 2a 2a 20 65 because not.** e
68c40 6e 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f nough informatio
68c50 6e 20 61 62 6f 75 74 20 74 68 65 20 63 6f 6d 70 n about the comp
68c60 6f 75 6e 64 20 71 75 65 72 79 20 69 73 20 6b 6e ound query is kn
68c70 6f 77 6e 20 61 74 20 74 68 61 74 20 70 6f 69 6e own at that poin
68c80 74 2e 0a 2a 2a 20 54 68 65 20 4b 65 79 49 6e 66 t..** The KeyInf
68c90 6f 20 66 6f 72 20 61 64 64 72 4f 70 65 6e 54 72 o for addrOpenTr
68ca0 61 6e 5b 30 5d 20 61 6e 64 20 5b 31 5d 20 63 6f an[0] and [1] co
68cb0 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 ntains collating
68cc0 20 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20 66 6f sequences.** fo
68cd0 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 r the result set
68ce0 2e 20 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 . The KeyInfo f
68cf0 6f 72 20 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b or addrOpenTran[
68d00 32 5d 20 63 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 2] contains coll
68d10 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 ating.** sequenc
68d20 65 73 20 66 6f 72 20 74 68 65 20 4f 52 44 45 52 es for the ORDER
68d30 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 BY clause..*/.s
68d40 74 72 75 63 74 20 53 65 6c 65 63 74 20 7b 0a 20 truct Select {.
68d50 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 ExprList *pELis
68d60 74 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 t; /* The f
68d70 69 65 6c 64 73 20 6f 66 20 74 68 65 20 72 65 73 ields of the res
68d80 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 ult */. u8 op;
68d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68da0 2f 2a 20 4f 6e 65 20 6f 66 3a 20 54 4b 5f 55 4e /* One of: TK_UN
68db0 49 4f 4e 20 54 4b 5f 41 4c 4c 20 54 4b 5f 49 4e ION TK_ALL TK_IN
68dc0 54 45 52 53 45 43 54 20 54 4b 5f 45 58 43 45 50 TERSECT TK_EXCEP
68dd0 54 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 T */. char affi
68de0 6e 69 74 79 3b 20 20 20 20 20 20 20 20 20 2f 2a nity; /*
68df0 20 4d 61 6b 65 52 65 63 6f 72 64 20 77 69 74 68 MakeRecord with
68e00 20 74 68 69 73 20 61 66 66 69 6e 69 74 79 20 66 this affinity f
68e10 6f 72 20 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20 or SRT_Set */.
68e20 75 31 36 20 73 65 6c 46 6c 61 67 73 3b 20 20 20 u16 selFlags;
68e30 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 /* Variou
68e40 73 20 53 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f s SF_* values */
68e50 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 . SrcList *pSrc
68e60 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 ; /* The
68e70 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a FROM clause */.
68e80 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 Expr *pWhere;
68e90 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
68ea0 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a WHERE clause */.
68eb0 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f ExprList *pGro
68ec0 75 70 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 upBy; /* The
68ed0 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 GROUP BY clause
68ee0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 48 61 76 69 */. Expr *pHavi
68ef0 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ng; /* T
68f00 68 65 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 he HAVING clause
68f10 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
68f20 70 4f 72 64 65 72 42 79 3b 20 20 20 20 2f 2a 20 pOrderBy; /*
68f30 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 The ORDER BY cla
68f40 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 use */. Select
68f50 2a 70 50 72 69 6f 72 3b 20 20 20 20 20 20 20 20 *pPrior;
68f60 2f 2a 20 50 72 69 6f 72 20 73 65 6c 65 63 74 20 /* Prior select
68f70 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 in a compound se
68f80 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a lect statement *
68f90 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 78 /. Select *pNex
68fa0 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 t; /* Ne
68fb0 78 74 20 73 65 6c 65 63 74 20 74 6f 20 74 68 65 xt select to the
68fc0 20 6c 65 66 74 20 69 6e 20 61 20 63 6f 6d 70 6f left in a compo
68fd0 75 6e 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 und */. Select
68fe0 2a 70 52 69 67 68 74 6d 6f 73 74 3b 20 20 20 20 *pRightmost;
68ff0 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74 20 73 65 /* Right-most se
69000 6c 65 63 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 lect in a compou
69010 6e 64 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d nd select statem
69020 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ent */. Expr *p
69030 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20 Limit;
69040 2f 2a 20 4c 49 4d 49 54 20 65 78 70 72 65 73 73 /* LIMIT express
69050 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 ion. NULL means
69060 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 45 not used. */. E
69070 78 70 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20 xpr *pOffset;
69080 20 20 20 20 20 20 2f 2a 20 4f 46 46 53 45 54 20 /* OFFSET
69090 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c expression. NULL
690a0 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 2e means not used.
690b0 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 */. int iLimit
690c0 2c 20 69 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20 , iOffset; /*
690d0 4d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 Memory registers
690e0 20 68 6f 6c 64 69 6e 67 20 4c 49 4d 49 54 20 26 holding LIMIT &
690f0 20 4f 46 46 53 45 54 20 63 6f 75 6e 74 65 72 73 OFFSET counters
69100 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f 70 */. int addrOp
69110 65 6e 45 70 68 6d 5b 33 5d 3b 20 20 20 2f 2a 20 enEphm[3]; /*
69120 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 20 6f 70 63 OP_OpenEphem opc
69130 6f 64 65 73 20 72 65 6c 61 74 65 64 20 74 6f 20 odes related to
69140 74 68 69 73 20 73 65 6c 65 63 74 20 2a 2f 0a 7d this select */.}
69150 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 ;../*.** Allowed
69160 20 76 61 6c 75 65 73 20 66 6f 72 20 53 65 6c 65 values for Sele
69170 63 74 2e 73 65 6c 46 6c 61 67 73 2e 20 20 54 68 ct.selFlags. Th
69180 65 20 22 53 46 22 20 70 72 65 66 69 78 20 73 74 e "SF" prefix st
69190 61 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 53 65 6c ands for.** "Sel
691a0 65 63 74 20 46 6c 61 67 22 2e 0a 2a 2f 0a 23 64 ect Flag"..*/.#d
691b0 65 66 69 6e 65 20 53 46 5f 44 69 73 74 69 6e 63 efine SF_Distinc
691c0 74 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20 t 0x0001
691d0 20 2f 2a 20 4f 75 74 70 75 74 20 73 68 6f 75 6c /* Output shoul
691e0 64 20 62 65 20 44 49 53 54 49 4e 43 54 20 2a 2f d be DISTINCT */
691f0 0a 23 64 65 66 69 6e 65 20 53 46 5f 52 65 73 6f .#define SF_Reso
69200 6c 76 65 64 20 20 20 20 20 20 20 20 30 78 30 30 lved 0x00
69210 30 32 20 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 02 /* Identifie
69220 72 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 rs have been res
69230 6f 6c 76 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 olved */.#define
69240 20 53 46 5f 41 67 67 72 65 67 61 74 65 20 20 20 SF_Aggregate
69250 20 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 43 0x0004 /* C
69260 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74 ontains aggregat
69270 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 e functions */.#
69280 64 65 66 69 6e 65 20 53 46 5f 55 73 65 73 45 70 define SF_UsesEp
69290 68 65 6d 65 72 61 6c 20 20 20 30 78 30 30 30 38 hemeral 0x0008
692a0 20 20 2f 2a 20 55 73 65 73 20 74 68 65 20 4f 70 /* Uses the Op
692b0 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f enEphemeral opco
692c0 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 de */.#define SF
692d0 5f 45 78 70 61 6e 64 65 64 20 20 20 20 20 20 20 _Expanded
692e0 20 30 78 30 30 31 30 20 20 2f 2a 20 73 71 6c 69 0x0010 /* sqli
692f0 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 te3SelectExpand(
69300 29 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 69 73 ) called on this
69310 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 48 */.#define SF_H
69320 61 73 54 79 70 65 49 6e 66 6f 20 20 20 20 20 30 asTypeInfo 0
69330 78 30 30 32 30 20 20 2f 2a 20 46 52 4f 4d 20 73 x0020 /* FROM s
69340 75 62 71 75 65 72 69 65 73 20 68 61 76 65 20 54 ubqueries have T
69350 61 62 6c 65 20 6d 65 74 61 64 61 74 61 20 2a 2f able metadata */
69360 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 73 .../*.** The res
69370 75 6c 74 73 20 6f 66 20 61 20 73 65 6c 65 63 74 ults of a select
69380 20 63 61 6e 20 62 65 20 64 69 73 74 72 69 62 75 can be distribu
69390 74 65 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77 ted in several w
693a0 61 79 73 2e 20 20 54 68 65 0a 2a 2a 20 22 53 52 ays. The.** "SR
693b0 54 22 20 70 72 65 66 69 78 20 6d 65 61 6e 73 20 T" prefix means
693c0 22 53 45 4c 45 43 54 20 52 65 73 75 6c 74 20 54 "SELECT Result T
693d0 79 70 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ype"..*/.#define
693e0 20 53 52 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20 SRT_Union
693f0 20 20 31 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 1 /* Store re
69400 73 75 6c 74 20 61 73 20 6b 65 79 73 20 69 6e 20 sult as keys in
69410 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 an index */.#def
69420 69 6e 65 20 53 52 54 5f 45 78 63 65 70 74 20 20 ine SRT_Except
69430 20 20 20 20 20 32 20 20 2f 2a 20 52 65 6d 6f 76 2 /* Remov
69440 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 20 61 20 e result from a
69450 55 4e 49 4f 4e 20 69 6e 64 65 78 20 2a 2f 0a 23 UNION index */.#
69460 64 65 66 69 6e 65 20 53 52 54 5f 45 78 69 73 74 define SRT_Exist
69470 73 20 20 20 20 20 20 20 33 20 20 2f 2a 20 53 74 s 3 /* St
69480 6f 72 65 20 31 20 69 66 20 74 68 65 20 72 65 73 ore 1 if the res
69490 75 6c 74 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 ult is not empty
694a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f */.#define SRT_
694b0 44 69 73 63 61 72 64 20 20 20 20 20 20 34 20 20 Discard 4
694c0 2f 2a 20 44 6f 20 6e 6f 74 20 73 61 76 65 20 74 /* Do not save t
694d0 68 65 20 72 65 73 75 6c 74 73 20 61 6e 79 77 68 he results anywh
694e0 65 72 65 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 4f ere */../* The O
694f0 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 RDER BY clause i
69500 73 20 69 67 6e 6f 72 65 64 20 66 6f 72 20 61 6c s ignored for al
69510 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 2a l of the above *
69520 2f 0a 23 64 65 66 69 6e 65 20 49 67 6e 6f 72 61 /.#define Ignora
69530 62 6c 65 4f 72 64 65 72 62 79 28 58 29 20 28 28 bleOrderby(X) ((
69540 58 2d 3e 65 44 65 73 74 29 3c 3d 53 52 54 5f 44 X->eDest)<=SRT_D
69550 69 73 63 61 72 64 29 0a 0a 23 64 65 66 69 6e 65 iscard)..#define
69560 20 53 52 54 5f 4f 75 74 70 75 74 20 20 20 20 20 SRT_Output
69570 20 20 35 20 20 2f 2a 20 4f 75 74 70 75 74 20 65 5 /* Output e
69580 61 63 68 20 72 6f 77 20 6f 66 20 72 65 73 75 6c ach row of resul
69590 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 t */.#define SRT
695a0 5f 4d 65 6d 20 20 20 20 20 20 20 20 20 20 36 20 _Mem 6
695b0 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 /* Store result
695c0 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c in a memory cel
695d0 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 l */.#define SRT
695e0 5f 53 65 74 20 20 20 20 20 20 20 20 20 20 37 20 _Set 7
695f0 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 /* Store result
69600 73 20 61 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 s as keys in an
69610 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 index */.#define
69620 20 53 52 54 5f 54 61 62 6c 65 20 20 20 20 20 20 SRT_Table
69630 20 20 38 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 8 /* Store re
69640 73 75 6c 74 20 61 73 20 64 61 74 61 20 77 69 74 sult as data wit
69650 68 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 h an automatic r
69660 6f 77 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 owid */.#define
69670 53 52 54 5f 45 70 68 65 6d 54 61 62 20 20 20 20 SRT_EphemTab
69680 20 39 20 20 2f 2a 20 43 72 65 61 74 65 20 74 72 9 /* Create tr
69690 61 6e 73 69 65 6e 74 20 74 61 62 20 61 6e 64 20 ansient tab and
696a0 73 74 6f 72 65 20 6c 69 6b 65 20 53 52 54 5f 54 store like SRT_T
696b0 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 able */.#define
696c0 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 20 20 SRT_Coroutine
696d0 31 30 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 10 /* Generate
696e0 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 a single row of
696f0 72 65 73 75 6c 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a result */../*.**
69700 20 41 20 73 74 72 75 63 74 75 72 65 20 75 73 65 A structure use
69710 64 20 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74 d to customize t
69720 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 73 he behavior of s
69730 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 2e 20 qlite3Select().
69740 53 65 65 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 See.** comments
69750 61 62 6f 76 65 20 73 71 6c 69 74 65 33 53 65 6c above sqlite3Sel
69760 65 63 74 28 29 20 66 6f 72 20 64 65 74 61 69 6c ect() for detail
69770 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 s..*/.typedef st
69780 72 75 63 74 20 53 65 6c 65 63 74 44 65 73 74 20 ruct SelectDest
69790 53 65 6c 65 63 74 44 65 73 74 3b 0a 73 74 72 75 SelectDest;.stru
697a0 63 74 20 53 65 6c 65 63 74 44 65 73 74 20 7b 0a ct SelectDest {.
697b0 20 20 75 38 20 65 44 65 73 74 3b 20 20 20 20 20 u8 eDest;
697c0 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 /* How to di
697d0 73 70 6f 73 65 20 6f 66 20 74 68 65 20 72 65 73 spose of the res
697e0 75 6c 74 73 20 2a 2f 0a 20 20 75 38 20 61 66 66 ults */. u8 aff
697f0 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 inity; /* A
69800 66 66 69 6e 69 74 79 20 75 73 65 64 20 77 68 65 ffinity used whe
69810 6e 20 65 44 65 73 74 3d 3d 53 52 54 5f 53 65 74 n eDest==SRT_Set
69820 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 3b */. int iParm;
69830 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72 /* A par
69840 61 6d 65 74 65 72 20 75 73 65 64 20 62 79 20 74 ameter used by t
69850 68 65 20 65 44 65 73 74 20 64 69 73 70 6f 73 61 he eDest disposa
69860 6c 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e l method */. in
69870 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 t iMem;
69880 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74 65 72 /* Base register
69890 20 77 68 65 72 65 20 72 65 73 75 6c 74 73 20 61 where results a
698a0 72 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 re written */.
698b0 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 int nMem;
698c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 /* Number of r
698d0 65 67 69 73 74 65 72 73 20 61 6c 6c 6f 63 61 74 egisters allocat
698e0 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ed */.};../*.**
698f0 44 75 72 69 6e 67 20 63 6f 64 65 20 67 65 6e 65 During code gene
69900 72 61 74 69 6f 6e 20 6f 66 20 73 74 61 74 65 6d ration of statem
69910 65 6e 74 73 20 74 68 61 74 20 64 6f 20 69 6e 73 ents that do ins
69920 65 72 74 73 20 69 6e 74 6f 20 41 55 54 4f 49 4e erts into AUTOIN
69930 43 52 45 4d 45 4e 54 20 0a 2a 2a 20 74 61 62 6c CREMENT .** tabl
69940 65 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e es, the followin
69950 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 g information is
69960 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 attached to the
69970 20 54 61 62 6c 65 2e 75 2e 61 75 74 6f 49 6e 63 Table.u.autoInc
69980 2e 70 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 66 .p.** pointer of
69990 20 65 61 63 68 20 61 75 74 6f 69 6e 63 72 65 6d each autoincrem
699a0 65 6e 74 20 74 61 62 6c 65 20 74 6f 20 72 65 63 ent table to rec
699b0 6f 72 64 20 73 6f 6d 65 20 73 69 64 65 20 69 6e ord some side in
699c0 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 0a 2a formation that.*
699d0 2a 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 * the code gener
699e0 61 74 6f 72 20 6e 65 65 64 73 2e 20 20 57 65 20 ator needs. We
699f0 68 61 76 65 20 74 6f 20 6b 65 65 70 20 70 65 72 have to keep per
69a00 2d 74 61 62 6c 65 20 61 75 74 6f 69 6e 63 72 65 -table autoincre
69a10 6d 65 6e 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 ment.** informat
69a20 69 6f 6e 20 69 6e 20 63 61 73 65 20 69 6e 73 65 ion in case inse
69a30 72 74 73 20 61 72 65 20 64 6f 77 6e 20 77 69 74 rts are down wit
69a40 68 69 6e 20 74 72 69 67 67 65 72 73 2e 20 20 54 hin triggers. T
69a50 72 69 67 67 65 72 73 20 64 6f 20 6e 6f 74 0a 2a riggers do not.*
69a60 2a 20 6e 6f 72 6d 61 6c 6c 79 20 63 6f 6f 72 64 * normally coord
69a70 69 6e 61 74 65 20 74 68 65 69 72 20 61 63 74 69 inate their acti
69a80 76 69 74 69 65 73 2c 20 62 75 74 20 77 65 20 64 vities, but we d
69a90 6f 20 6e 65 65 64 20 74 6f 20 63 6f 6f 72 64 69 o need to coordi
69aa0 6e 61 74 65 20 74 68 65 0a 2a 2a 20 6c 6f 61 64 nate the.** load
69ab0 69 6e 67 20 61 6e 64 20 73 61 76 69 6e 67 20 6f ing and saving o
69ac0 66 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 f autoincrement
69ad0 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a information..*/.
69ae0 73 74 72 75 63 74 20 41 75 74 6f 69 6e 63 49 6e struct AutoincIn
69af0 66 6f 20 7b 0a 20 20 41 75 74 6f 69 6e 63 49 6e fo {. AutoincIn
69b00 66 6f 20 2a 70 4e 65 78 74 3b 20 20 20 2f 2a 20 fo *pNext; /*
69b10 4e 65 78 74 20 69 6e 66 6f 20 62 6c 6f 63 6b 20 Next info block
69b20 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 74 68 65 in a list of the
69b30 6d 20 61 6c 6c 20 2a 2f 0a 20 20 54 61 62 6c 65 m all */. Table
69b40 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 *pTab;
69b50 20 2f 2a 20 54 61 62 6c 65 20 74 68 69 73 20 69 /* Table this i
69b60 6e 66 6f 20 62 6c 6f 63 6b 20 72 65 66 65 72 73 nfo block refers
69b70 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 to */. int iDb
69b80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
69b90 2a 20 49 6e 64 65 78 20 69 6e 20 73 71 6c 69 74 * Index in sqlit
69ba0 65 33 2e 61 44 62 5b 5d 20 6f 66 20 64 61 74 61 e3.aDb[] of data
69bb0 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 base holding pTa
69bc0 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 43 74 b */. int regCt
69bd0 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 r; /*
69be0 4d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 20 Memory register
69bf0 68 6f 6c 64 69 6e 67 20 74 68 65 20 72 6f 77 69 holding the rowi
69c00 64 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 7d 3b 0a d counter */.};.
69c10 0a 2f 2a 0a 2a 2a 20 53 69 7a 65 20 6f 66 20 74 ./*.** Size of t
69c20 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 0a he column cache.
69c30 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
69c40 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 0a 23 20 64 E_N_COLCACHE.# d
69c50 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 43 efine SQLITE_N_C
69c60 4f 4c 43 41 43 48 45 20 31 30 0a 23 65 6e 64 69 OLCACHE 10.#endi
69c70 66 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 f../*.** At leas
69c80 74 20 6f 6e 65 20 69 6e 73 74 61 6e 63 65 20 6f t one instance o
69c90 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
69ca0 73 74 72 75 63 74 75 72 65 20 69 73 20 63 72 65 structure is cre
69cb0 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 0a 2a ated for each .*
69cc0 2a 20 74 72 69 67 67 65 72 20 74 68 61 74 20 6d * trigger that m
69cd0 61 79 20 62 65 20 66 69 72 65 64 20 77 68 69 6c ay be fired whil
69ce0 65 20 70 61 72 73 69 6e 67 20 61 6e 20 49 4e 53 e parsing an INS
69cf0 45 52 54 2c 20 55 50 44 41 54 45 20 6f 72 20 44 ERT, UPDATE or D
69d00 45 4c 45 54 45 0a 2a 2a 20 73 74 61 74 65 6d 65 ELETE.** stateme
69d10 6e 74 2e 20 41 6c 6c 20 73 75 63 68 20 6f 62 6a nt. All such obj
69d20 65 63 74 73 20 61 72 65 20 73 74 6f 72 65 64 20 ects are stored
69d30 69 6e 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 in the linked li
69d40 73 74 20 68 65 61 64 65 64 20 61 74 0a 2a 2a 20 st headed at.**
69d50 50 61 72 73 65 2e 70 54 72 69 67 67 65 72 50 72 Parse.pTriggerPr
69d60 67 20 61 6e 64 20 64 65 6c 65 74 65 64 20 6f 6e g and deleted on
69d70 63 65 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d ce statement com
69d80 70 69 6c 61 74 69 6f 6e 20 68 61 73 20 62 65 65 pilation has bee
69d90 6e 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 64 2e 0a n.** completed..
69da0 2a 2a 0a 2a 2a 20 41 20 56 64 62 65 20 73 75 62 **.** A Vdbe sub
69db0 2d 70 72 6f 67 72 61 6d 20 74 68 61 74 20 69 6d -program that im
69dc0 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 62 6f 64 plements the bod
69dd0 79 20 61 6e 64 20 57 48 45 4e 20 63 6c 61 75 73 y and WHEN claus
69de0 65 20 6f 66 20 74 72 69 67 67 65 72 0a 2a 2a 20 e of trigger.**
69df0 54 72 69 67 67 65 72 50 72 67 2e 70 54 72 69 67 TriggerPrg.pTrig
69e00 67 65 72 2c 20 61 73 73 75 6d 69 6e 67 20 61 20 ger, assuming a
69e10 64 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c default ON CONFL
69e20 49 43 54 20 63 6c 61 75 73 65 20 6f 66 0a 2a 2a ICT clause of.**
69e30 20 54 72 69 67 67 65 72 50 72 67 2e 6f 72 63 6f TriggerPrg.orco
69e40 6e 66 2c 20 69 73 20 73 74 6f 72 65 64 20 69 6e nf, is stored in
69e50 20 74 68 65 20 54 72 69 67 67 65 72 50 72 67 2e the TriggerPrg.
69e60 70 50 72 6f 67 72 61 6d 20 76 61 72 69 61 62 6c pProgram variabl
69e70 65 2e 0a 2a 2a 20 54 68 65 20 50 61 72 73 65 2e e..** The Parse.
69e80 70 54 72 69 67 67 65 72 50 72 67 20 6c 69 73 74 pTriggerPrg list
69e90 20 6e 65 76 65 72 20 63 6f 6e 74 61 69 6e 73 20 never contains
69ea0 74 77 6f 20 65 6e 74 72 69 65 73 20 77 69 74 68 two entries with
69eb0 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 76 61 6c the same.** val
69ec0 75 65 73 20 66 6f 72 20 62 6f 74 68 20 70 54 72 ues for both pTr
69ed0 69 67 67 65 72 20 61 6e 64 20 6f 72 63 6f 6e 66 igger and orconf
69ee0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 72 69 67 ..**.** The Trig
69ef0 67 65 72 50 72 67 2e 6f 6c 64 6d 61 73 6b 20 76 gerPrg.oldmask v
69f00 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 ariable is set t
69f10 6f 20 61 20 6d 61 73 6b 20 6f 66 20 6f 6c 64 2e o a mask of old.
69f20 2a 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 61 63 63 * columns.** acc
69f30 65 73 73 65 64 20 28 6f 72 20 73 65 74 20 74 6f essed (or set to
69f40 20 30 20 66 6f 72 20 74 72 69 67 67 65 72 73 20 0 for triggers
69f50 66 69 72 65 64 20 61 73 20 61 20 72 65 73 75 6c fired as a resul
69f60 74 20 6f 66 20 49 4e 53 45 52 54 20 0a 2a 2a 20 t of INSERT .**
69f70 73 74 61 74 65 6d 65 6e 74 73 29 2e 0a 2a 2f 0a statements)..*/.
69f80 73 74 72 75 63 74 20 54 72 69 67 67 65 72 50 72 struct TriggerPr
69f90 67 20 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 g {. Trigger *p
69fa0 54 72 69 67 67 65 72 3b 20 20 20 20 20 20 2f 2a Trigger; /*
69fb0 20 54 72 69 67 67 65 72 20 74 68 69 73 20 70 72 Trigger this pr
69fc0 6f 67 72 61 6d 20 77 61 73 20 63 6f 64 65 64 20 ogram was coded
69fd0 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 from */. int or
69fe0 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 20 20 conf;
69ff0 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20 /* Default ON
6a000 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 CONFLICT policy
6a010 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20 */. SubProgram
6a020 2a 70 50 72 6f 67 72 61 6d 3b 20 20 20 2f 2a 20 *pProgram; /*
6a030 50 72 6f 67 72 61 6d 20 69 6d 70 6c 65 6d 65 6e Program implemen
6a040 74 69 6e 67 20 70 54 72 69 67 67 65 72 2f 6f 72 ting pTrigger/or
6a050 63 6f 6e 66 20 2a 2f 0a 20 20 75 33 32 20 6f 6c conf */. u32 ol
6a060 64 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 dmask;
6a070 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 6f 6c 64 /* Mask of old
6a080 2e 2a 20 63 6f 6c 75 6d 6e 73 20 61 63 63 65 73 .* columns acces
6a090 73 65 64 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 sed */. Trigger
6a0a0 50 72 67 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 Prg *pNext;
6a0b0 20 2f 2a 20 4e 65 78 74 20 65 6e 74 72 79 20 69 /* Next entry i
6a0c0 6e 20 50 61 72 73 65 2e 70 54 72 69 67 67 65 72 n Parse.pTrigger
6a0d0 50 72 67 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a Prg list */.};..
6a0e0 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 70 61 72 /*.** An SQL par
6a0f0 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 41 20 ser context. A
6a100 63 6f 70 79 20 6f 66 20 74 68 69 73 20 73 74 72 copy of this str
6a110 75 63 74 75 72 65 20 69 73 20 70 61 73 73 65 64 ucture is passed
6a120 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68 65 20 through.** the
6a130 70 61 72 73 65 72 20 61 6e 64 20 64 6f 77 6e 20 parser and down
6a140 69 6e 74 6f 20 61 6c 6c 20 74 68 65 20 70 61 72 into all the par
6a150 73 65 72 20 61 63 74 69 6f 6e 20 72 6f 75 74 69 ser action routi
6a160 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a ne in order to.*
6a170 2a 20 63 61 72 72 79 20 61 72 6f 75 6e 64 20 69 * carry around i
6a180 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 nformation that
6a190 69 73 20 67 6c 6f 62 61 6c 20 74 6f 20 74 68 65 is global to the
6a1a0 20 65 6e 74 69 72 65 20 70 61 72 73 65 2e 0a 2a entire parse..*
6a1b0 2a 0a 2a 2a 20 54 68 65 20 73 74 72 75 63 74 75 *.** The structu
6a1c0 72 65 20 69 73 20 64 69 76 69 64 65 64 20 69 6e re is divided in
6a1d0 74 6f 20 74 77 6f 20 70 61 72 74 73 2e 20 20 57 to two parts. W
6a1e0 68 65 6e 20 74 68 65 20 70 61 72 73 65 72 20 61 hen the parser a
6a1f0 6e 64 20 63 6f 64 65 0a 2a 2a 20 67 65 6e 65 72 nd code.** gener
6a200 61 74 65 20 63 61 6c 6c 20 74 68 65 6d 73 65 6c ate call themsel
6a210 76 65 73 20 72 65 63 75 72 73 69 76 65 6c 79 2c ves recursively,
6a220 20 74 68 65 20 66 69 72 73 74 20 70 61 72 74 20 the first part
6a230 6f 66 20 74 68 65 20 73 74 72 75 63 74 75 72 65 of the structure
6a240 0a 2a 2a 20 69 73 20 63 6f 6e 73 74 61 6e 74 20 .** is constant
6a250 62 75 74 20 74 68 65 20 73 65 63 6f 6e 64 20 70 but the second p
6a260 61 72 74 20 69 73 20 72 65 73 65 74 20 61 74 20 art is reset at
6a270 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e the beginning an
6a280 64 20 65 6e 64 20 6f 66 0a 2a 2a 20 65 61 63 68 d end of.** each
6a290 20 72 65 63 75 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a recursion..**.*
6a2a0 2a 20 54 68 65 20 6e 54 61 62 6c 65 4c 6f 63 6b * The nTableLock
6a2b0 20 61 6e 64 20 61 54 61 62 6c 65 4c 6f 63 6b 20 and aTableLock
6a2c0 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 6f 6e variables are on
6a2d0 6c 79 20 75 73 65 64 20 69 66 20 74 68 65 20 73 ly used if the s
6a2e0 68 61 72 65 64 2d 63 61 63 68 65 20 0a 2a 2a 20 hared-cache .**
6a2f0 66 65 61 74 75 72 65 20 69 73 20 65 6e 61 62 6c feature is enabl
6a300 65 64 20 28 69 66 20 73 71 6c 69 74 65 33 54 73 ed (if sqlite3Ts
6a310 64 28 29 2d 3e 75 73 65 53 68 61 72 65 64 44 61 d()->useSharedDa
6a320 74 61 20 69 73 20 74 72 75 65 29 2e 20 54 68 65 ta is true). The
6a330 79 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f y are.** used to
6a340 20 73 74 6f 72 65 20 74 68 65 20 73 65 74 20 6f store the set o
6a350 66 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 72 65 f table-locks re
6a360 71 75 69 72 65 64 20 62 79 20 74 68 65 20 73 74 quired by the st
6a370 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 0a 2a 2a atement being.**
6a380 20 63 6f 6d 70 69 6c 65 64 2e 20 46 75 6e 63 74 compiled. Funct
6a390 69 6f 6e 20 73 71 6c 69 74 65 33 54 61 62 6c 65 ion sqlite3Table
6a3a0 4c 6f 63 6b 28 29 20 69 73 20 75 73 65 64 20 74 Lock() is used t
6a3b0 6f 20 61 64 64 20 65 6e 74 72 69 65 73 20 74 6f o add entries to
6a3c0 20 74 68 65 0a 2a 2a 20 6c 69 73 74 2e 0a 2a 2f the.** list..*/
6a3d0 0a 73 74 72 75 63 74 20 50 61 72 73 65 20 7b 0a .struct Parse {.
6a3e0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
6a3f0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 /* The ma
6a400 69 6e 20 64 61 74 61 62 61 73 65 20 73 74 72 75 in database stru
6a410 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72 cture */. int r
6a420 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
6a430 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 /* Return code f
6a440 72 6f 6d 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f rom execution */
6a450 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 . char *zErrMsg
6a460 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 72 ; /* An er
6a470 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20 ror message */.
6a480 20 56 64 62 65 20 2a 70 56 64 62 65 3b 20 20 20 Vdbe *pVdbe;
6a490 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 6e 67 69 /* An engi
6a4a0 6e 65 20 66 6f 72 20 65 78 65 63 75 74 69 6e 67 ne for executing
6a4b0 20 64 61 74 61 62 61 73 65 20 62 79 74 65 63 6f database byteco
6a4c0 64 65 20 2a 2f 0a 20 20 75 38 20 63 6f 6c 4e 61 de */. u8 colNa
6a4d0 6d 65 73 53 65 74 3b 20 20 20 20 20 20 2f 2a 20 mesSet; /*
6a4e0 54 52 55 45 20 61 66 74 65 72 20 4f 50 5f 43 6f TRUE after OP_Co
6a4f0 6c 75 6d 6e 4e 61 6d 65 20 68 61 73 20 62 65 65 lumnName has bee
6a500 6e 20 69 73 73 75 65 64 20 74 6f 20 70 56 64 62 n issued to pVdb
6a510 65 20 2a 2f 0a 20 20 75 38 20 6e 61 6d 65 43 6c e */. u8 nameCl
6a520 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 ash; /* A
6a530 20 70 65 72 6d 61 6e 65 6e 74 20 74 61 62 6c 65 permanent table
6a540 20 6e 61 6d 65 20 63 6c 61 73 68 65 73 20 77 69 name clashes wi
6a550 74 68 20 74 65 6d 70 20 74 61 62 6c 65 20 6e 61 th temp table na
6a560 6d 65 20 2a 2f 0a 20 20 75 38 20 63 68 65 63 6b me */. u8 check
6a570 53 63 68 65 6d 61 3b 20 20 20 20 20 20 2f 2a 20 Schema; /*
6a580 43 61 75 73 65 73 20 73 63 68 65 6d 61 20 63 6f Causes schema co
6a590 6f 6b 69 65 20 63 68 65 63 6b 20 61 66 74 65 72 okie check after
6a5a0 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 75 an error */. u
6a5b0 38 20 6e 65 73 74 65 64 3b 20 20 20 20 20 20 20 8 nested;
6a5c0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
6a5d0 20 6e 65 73 74 65 64 20 63 61 6c 6c 73 20 74 6f nested calls to
6a5e0 20 74 68 65 20 70 61 72 73 65 72 2f 63 6f 64 65 the parser/code
6a5f0 20 67 65 6e 65 72 61 74 6f 72 20 2a 2f 0a 20 20 generator */.
6a600 75 38 20 70 61 72 73 65 45 72 72 6f 72 3b 20 20 u8 parseError;
6a610 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 /* True aft
6a620 65 72 20 61 20 70 61 72 73 69 6e 67 20 65 72 72 er a parsing err
6a630 6f 72 2e 20 20 54 69 63 6b 65 74 20 23 31 37 39 or. Ticket #179
6a640 34 20 2a 2f 0a 20 20 75 38 20 6e 54 65 6d 70 52 4 */. u8 nTempR
6a650 65 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e eg; /* N
6a660 75 6d 62 65 72 20 6f 66 20 74 65 6d 70 6f 72 61 umber of tempora
6a670 72 79 20 72 65 67 69 73 74 65 72 73 20 69 6e 20 ry registers in
6a680 61 54 65 6d 70 52 65 67 5b 5d 20 2a 2f 0a 20 20 aTempReg[] */.
6a690 75 38 20 6e 54 65 6d 70 49 6e 55 73 65 3b 20 20 u8 nTempInUse;
6a6a0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
6a6b0 66 20 61 54 65 6d 70 52 65 67 5b 5d 20 63 75 72 f aTempReg[] cur
6a6c0 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f rently checked o
6a6d0 75 74 20 2a 2f 0a 20 20 69 6e 74 20 61 54 65 6d ut */. int aTem
6a6e0 70 52 65 67 5b 38 5d 3b 20 20 20 20 20 2f 2a 20 pReg[8]; /*
6a6f0 48 6f 6c 64 69 6e 67 20 61 72 65 61 20 66 6f 72 Holding area for
6a700 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 temporary regis
6a710 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 ters */. int nR
6a720 61 6e 67 65 52 65 67 3b 20 20 20 20 20 20 20 2f angeReg; /
6a730 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 74 65 * Size of the te
6a740 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 mporary register
6a750 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 block */. int
6a760 69 52 61 6e 67 65 52 65 67 3b 20 20 20 20 20 20 iRangeReg;
6a770 20 2f 2a 20 46 69 72 73 74 20 72 65 67 69 73 74 /* First regist
6a780 65 72 20 69 6e 20 74 65 6d 70 6f 72 61 72 79 20 er in temporary
6a790 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b 20 2a register block *
6a7a0 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 /. int nErr;
6a7b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
6a7c0 65 72 20 6f 66 20 65 72 72 6f 72 73 20 73 65 65 er of errors see
6a7d0 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 62 3b n */. int nTab;
6a7e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
6a7f0 75 6d 62 65 72 20 6f 66 20 70 72 65 76 69 6f 75 umber of previou
6a800 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 56 44 sly allocated VD
6a810 42 45 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 BE cursors */.
6a820 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 int nMem;
6a830 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
6a840 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 75 f memory cells u
6a850 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 sed so far */.
6a860 69 6e 74 20 6e 53 65 74 3b 20 20 20 20 20 20 20 int nSet;
6a870 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
6a880 66 20 73 65 74 73 20 75 73 65 64 20 73 6f 20 66 f sets used so f
6a890 61 72 20 2a 2f 0a 20 20 69 6e 74 20 63 6b 42 61 ar */. int ckBa
6a8a0 73 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 se; /*
6a8b0 42 61 73 65 20 72 65 67 69 73 74 65 72 20 6f 66 Base register of
6a8c0 20 64 61 74 61 20 64 75 72 69 6e 67 20 63 68 65 data during che
6a8d0 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a ck constraints *
6a8e0 2f 0a 20 20 69 6e 74 20 69 43 61 63 68 65 4c 65 /. int iCacheLe
6a8f0 76 65 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 43 vel; /* ColC
6a900 61 63 68 65 20 76 61 6c 69 64 20 77 68 65 6e 20 ache valid when
6a910 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 69 4c 65 76 aColCache[].iLev
6a920 65 6c 3c 3d 69 43 61 63 68 65 4c 65 76 65 6c 20 el<=iCacheLevel
6a930 2a 2f 0a 20 20 69 6e 74 20 69 43 61 63 68 65 43 */. int iCacheC
6a940 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 nt; /* Cou
6a950 6e 74 65 72 20 75 73 65 64 20 74 6f 20 67 65 6e nter used to gen
6a960 65 72 61 74 65 20 61 43 6f 6c 43 61 63 68 65 5b erate aColCache[
6a970 5d 2e 6c 72 75 20 76 61 6c 75 65 73 20 2a 2f 0a ].lru values */.
6a980 20 20 75 38 20 6e 43 6f 6c 43 61 63 68 65 3b 20 u8 nColCache;
6a990 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6a9a0 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 of entries in t
6a9b0 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 he column cache
6a9c0 2a 2f 0a 20 20 75 38 20 69 43 6f 6c 43 61 63 68 */. u8 iColCach
6a9d0 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 e; /* Nex
6a9e0 74 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63 t entry of the c
6a9f0 61 63 68 65 20 74 6f 20 72 65 70 6c 61 63 65 20 ache to replace
6aa00 2a 2f 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c */. struct yCol
6aa10 43 61 63 68 65 20 7b 0a 20 20 20 20 69 6e 74 20 Cache {. int
6aa20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 iTable;
6aa30 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f /* Table curso
6aa40 72 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 r number */.
6aa50 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 int iColumn;
6aa60 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 /* Table c
6aa70 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a olumn number */.
6aa80 20 20 20 20 75 38 20 61 66 66 43 68 61 6e 67 65 u8 affChange
6aa90 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ; /* Tru
6aaa0 65 20 69 66 20 74 68 69 73 20 72 65 67 69 73 74 e if this regist
6aab0 65 72 20 68 61 73 20 68 61 64 20 61 6e 20 61 66 er has had an af
6aac0 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 2a 2f finity change */
6aad0 0a 20 20 20 20 75 38 20 74 65 6d 70 52 65 67 3b . u8 tempReg;
6aae0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 52 /* iR
6aaf0 65 67 20 69 73 20 61 20 74 65 6d 70 20 72 65 67 eg is a temp reg
6ab00 69 73 74 65 72 20 74 68 61 74 20 6e 65 65 64 73 ister that needs
6ab10 20 74 6f 20 62 65 20 66 72 65 65 64 20 2a 2f 0a to be freed */.
6ab20 20 20 20 20 69 6e 74 20 69 4c 65 76 65 6c 3b 20 int iLevel;
6ab30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 73 /* Nes
6ab40 74 69 6e 67 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 ting level */.
6ab50 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20 20 20 int iReg;
6ab60 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 20 77 /* Reg w
6ab70 69 74 68 20 76 61 6c 75 65 20 6f 66 20 74 68 69 ith value of thi
6ab80 73 20 63 6f 6c 75 6d 6e 2e 20 30 20 6d 65 61 6e s column. 0 mean
6ab90 73 20 6e 6f 6e 65 2e 20 2a 2f 0a 20 20 20 20 69 s none. */. i
6aba0 6e 74 20 6c 72 75 3b 20 20 20 20 20 20 20 20 20 nt lru;
6abb0 20 20 20 20 20 2f 2a 20 4c 65 61 73 74 20 72 65 /* Least re
6abc0 63 65 6e 74 6c 79 20 75 73 65 64 20 65 6e 74 72 cently used entr
6abd0 79 20 68 61 73 20 74 68 65 20 73 6d 61 6c 6c 65 y has the smalle
6abe0 73 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 st value */. }
6abf0 61 43 6f 6c 43 61 63 68 65 5b 53 51 4c 49 54 45 aColCache[SQLITE
6ac00 5f 4e 5f 43 4f 4c 43 41 43 48 45 5d 3b 20 20 2f _N_COLCACHE]; /
6ac10 2a 20 4f 6e 65 20 66 6f 72 20 65 61 63 68 20 63 * One for each c
6ac20 6f 6c 75 6d 6e 20 63 61 63 68 65 20 65 6e 74 72 olumn cache entr
6ac30 79 20 2a 2f 0a 20 20 75 33 32 20 77 72 69 74 65 y */. u32 write
6ac40 4d 61 73 6b 3b 20 20 20 20 20 20 20 2f 2a 20 53 Mask; /* S
6ac50 74 61 72 74 20 61 20 77 72 69 74 65 20 74 72 61 tart a write tra
6ac60 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 73 nsaction on thes
6ac70 65 20 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 20 e databases */.
6ac80 20 75 33 32 20 63 6f 6f 6b 69 65 4d 61 73 6b 3b u32 cookieMask;
6ac90 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b /* Bitmask
6aca0 20 6f 66 20 73 63 68 65 6d 61 20 76 65 72 69 66 of schema verif
6acb0 69 65 64 20 64 61 74 61 62 61 73 65 73 20 2a 2f ied databases */
6acc0 0a 20 20 75 38 20 69 73 4d 75 6c 74 69 57 72 69 . u8 isMultiWri
6acd0 74 65 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 te; /* True
6ace0 69 66 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 79 if statement may
6acf0 20 61 66 66 65 63 74 2f 69 6e 73 65 72 74 20 6d affect/insert m
6ad00 75 6c 74 69 70 6c 65 20 72 6f 77 73 20 2a 2f 0a ultiple rows */.
6ad10 20 20 75 38 20 6d 61 79 41 62 6f 72 74 3b 20 20 u8 mayAbort;
6ad20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
6ad30 66 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 79 20 f statement may
6ad40 74 68 72 6f 77 20 61 6e 20 41 42 4f 52 54 20 65 throw an ABORT e
6ad50 78 63 65 70 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e xception */. in
6ad60 74 20 63 6f 6f 6b 69 65 47 6f 74 6f 3b 20 20 20 t cookieGoto;
6ad70 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 /* Address of
6ad80 20 4f 50 5f 47 6f 74 6f 20 74 6f 20 63 6f 6f 6b OP_Goto to cook
6ad90 69 65 20 76 65 72 69 66 69 65 72 20 73 75 62 72 ie verifier subr
6ada0 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 outine */. int
6adb0 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 53 51 4c 49 cookieValue[SQLI
6adc0 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 2b TE_MAX_ATTACHED+
6add0 32 5d 3b 20 20 2f 2a 20 56 61 6c 75 65 73 20 6f 2]; /* Values o
6ade0 66 20 63 6f 6f 6b 69 65 73 20 74 6f 20 76 65 72 f cookies to ver
6adf0 69 66 79 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ify */.#ifndef S
6ae00 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
6ae10 44 5f 43 41 43 48 45 0a 20 20 69 6e 74 20 6e 54 D_CACHE. int nT
6ae20 61 62 6c 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 ableLock;
6ae30 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 6f /* Number of lo
6ae40 63 6b 73 20 69 6e 20 61 54 61 62 6c 65 4c 6f 63 cks in aTableLoc
6ae50 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 4c 6f 63 6b k */. TableLock
6ae60 20 2a 61 54 61 62 6c 65 4c 6f 63 6b 3b 20 2f 2a *aTableLock; /*
6ae70 20 52 65 71 75 69 72 65 64 20 74 61 62 6c 65 20 Required table
6ae80 6c 6f 63 6b 73 20 66 6f 72 20 73 68 61 72 65 64 locks for shared
6ae90 2d 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 -cache mode */.#
6aea0 65 6e 64 69 66 0a 20 20 69 6e 74 20 72 65 67 52 endif. int regR
6aeb0 6f 77 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 owid; /*
6aec0 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 Register holding
6aed0 20 72 6f 77 69 64 20 6f 66 20 43 52 45 41 54 45 rowid of CREATE
6aee0 20 54 41 42 4c 45 20 65 6e 74 72 79 20 2a 2f 0a TABLE entry */.
6aef0 20 20 69 6e 74 20 72 65 67 52 6f 6f 74 3b 20 20 int regRoot;
6af00 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
6af10 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 6f 74 20 er holding root
6af20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 page number for
6af30 6e 65 77 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 new objects */.
6af40 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 41 AutoincInfo *pA
6af50 69 6e 63 3b 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 inc; /* Informa
6af60 74 69 6f 6e 20 61 62 6f 75 74 20 41 55 54 4f 49 tion about AUTOI
6af70 4e 43 52 45 4d 45 4e 54 20 63 6f 75 6e 74 65 72 NCREMENT counter
6af80 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 78 41 s */. int nMaxA
6af90 72 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d rg; /* M
6afa0 61 78 20 61 72 67 73 20 70 61 73 73 65 64 20 74 ax args passed t
6afb0 6f 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 o user function
6afc0 62 79 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a by sub-program *
6afd0 2f 0a 0a 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 /.. /* Informat
6afe0 69 6f 6e 20 75 73 65 64 20 77 68 69 6c 65 20 63 ion used while c
6aff0 6f 64 69 6e 67 20 74 72 69 67 67 65 72 20 70 72 oding trigger pr
6b000 6f 67 72 61 6d 73 2e 20 2a 2f 0a 20 20 50 61 72 ograms. */. Par
6b010 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 3b 20 20 se *pToplevel;
6b020 20 20 2f 2a 20 50 61 72 73 65 20 73 74 72 75 63 /* Parse struc
6b030 74 75 72 65 20 66 6f 72 20 6d 61 69 6e 20 70 72 ture for main pr
6b040 6f 67 72 61 6d 20 28 6f 72 20 4e 55 4c 4c 29 20 ogram (or NULL)
6b050 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 72 69 */. Table *pTri
6b060 67 67 65 72 54 61 62 3b 20 20 2f 2a 20 54 61 62 ggerTab; /* Tab
6b070 6c 65 20 74 72 69 67 67 65 72 73 20 61 72 65 20 le triggers are
6b080 62 65 69 6e 67 20 63 6f 64 65 64 20 66 6f 72 20 being coded for
6b090 2a 2f 0a 20 20 75 33 32 20 6f 6c 64 6d 61 73 6b */. u32 oldmask
6b0a0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 ; /* Mas
6b0b0 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c 75 6d k of old.* colum
6b0c0 6e 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f ns referenced */
6b0d0 0a 20 20 75 38 20 65 54 72 69 67 67 65 72 4f 70 . u8 eTriggerOp
6b0e0 3b 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 55 50 ; /* TK_UP
6b0f0 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 20 DATE, TK_INSERT
6b100 6f 72 20 54 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a or TK_DELETE */.
6b110 20 20 75 38 20 65 4f 72 63 6f 6e 66 3b 20 20 20 u8 eOrconf;
6b120 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c /* Defaul
6b130 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f t ON CONFLICT po
6b140 6c 69 63 79 20 66 6f 72 20 74 72 69 67 67 65 72 licy for trigger
6b150 20 73 74 65 70 73 20 2a 2f 0a 20 20 75 38 20 64 steps */. u8 d
6b160 69 73 61 62 6c 65 54 72 69 67 67 65 72 73 3b 20 isableTriggers;
6b170 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 69 73 61 /* True to disa
6b180 62 6c 65 20 74 72 69 67 67 65 72 73 20 2a 2f 0a ble triggers */.
6b190 0a 20 20 2f 2a 20 41 62 6f 76 65 20 69 73 20 63 . /* Above is c
6b1a0 6f 6e 73 74 61 6e 74 20 62 65 74 77 65 65 6e 20 onstant between
6b1b0 72 65 63 75 72 73 69 6f 6e 73 2e 20 20 42 65 6c recursions. Bel
6b1c0 6f 77 20 69 73 20 72 65 73 65 74 20 62 65 66 6f ow is reset befo
6b1d0 72 65 20 61 6e 64 20 61 66 74 65 72 0a 20 20 2a re and after. *
6b1e0 2a 20 65 61 63 68 20 72 65 63 75 72 73 69 6f 6e * each recursion
6b1f0 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 56 61 72 3b */.. int nVar;
6b200 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
6b210 75 6d 62 65 72 20 6f 66 20 27 3f 27 20 76 61 72 umber of '?' var
6b220 69 61 62 6c 65 73 20 73 65 65 6e 20 69 6e 20 74 iables seen in t
6b230 68 65 20 53 51 4c 20 73 6f 20 66 61 72 20 2a 2f he SQL so far */
6b240 0a 20 20 69 6e 74 20 6e 56 61 72 45 78 70 72 3b . int nVarExpr;
6b250 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
6b260 72 20 6f 66 20 75 73 65 64 20 73 6c 6f 74 73 20 r of used slots
6b270 69 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a in apVarExpr[] *
6b280 2f 0a 20 20 69 6e 74 20 6e 56 61 72 45 78 70 72 /. int nVarExpr
6b290 41 6c 6c 6f 63 3b 20 20 20 2f 2a 20 4e 75 6d 62 Alloc; /* Numb
6b2a0 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 er of allocated
6b2b0 73 6c 6f 74 73 20 69 6e 20 61 70 56 61 72 45 78 slots in apVarEx
6b2c0 70 72 5b 5d 20 2a 2f 0a 20 20 45 78 70 72 20 2a pr[] */. Expr *
6b2d0 2a 61 70 56 61 72 45 78 70 72 3b 20 20 20 20 2f *apVarExpr; /
6b2e0 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 3a 61 * Pointers to :a
6b2f0 61 61 20 61 6e 64 20 24 61 61 61 61 20 77 69 6c aa and $aaaa wil
6b300 64 63 61 72 64 20 65 78 70 72 65 73 73 69 6f 6e dcard expression
6b310 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 52 65 s */. Vdbe *pRe
6b320 70 72 65 70 61 72 65 3b 20 20 20 20 2f 2a 20 56 prepare; /* V
6b330 4d 20 62 65 69 6e 67 20 72 65 70 72 65 70 61 72 M being reprepar
6b340 65 64 20 28 73 71 6c 69 74 65 33 52 65 70 72 65 ed (sqlite3Repre
6b350 70 61 72 65 28 29 29 20 2a 2f 0a 20 20 69 6e 74 pare()) */. int
6b360 20 6e 41 6c 69 61 73 3b 20 20 20 20 20 20 20 20 nAlias;
6b370 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 /* Number of a
6b380 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20 73 65 liased result se
6b390 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 t columns */. i
6b3a0 6e 74 20 6e 41 6c 69 61 73 41 6c 6c 6f 63 3b 20 nt nAliasAlloc;
6b3b0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
6b3c0 20 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 allocated slots
6b3d0 20 66 6f 72 20 61 41 6c 69 61 73 5b 5d 20 2a 2f for aAlias[] */
6b3e0 0a 20 20 69 6e 74 20 2a 61 41 6c 69 61 73 3b 20 . int *aAlias;
6b3f0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 /* Regis
6b400 74 65 72 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 ter used to hold
6b410 20 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20 aliased result
6b420 2a 2f 0a 20 20 75 38 20 65 78 70 6c 61 69 6e 3b */. u8 explain;
6b430 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
6b440 65 20 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e e if the EXPLAIN
6b450 20 66 6c 61 67 20 69 73 20 66 6f 75 6e 64 20 6f flag is found o
6b460 6e 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 n the query */.
6b470 20 54 6f 6b 65 6e 20 73 4e 61 6d 65 54 6f 6b 65 Token sNameToke
6b480 6e 3b 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 77 n; /* Token w
6b490 69 74 68 20 75 6e 71 75 61 6c 69 66 69 65 64 20 ith unqualified
6b4a0 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 6e 61 schema object na
6b4b0 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4c me */. Token sL
6b4c0 61 73 74 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20 astToken; /*
6b4d0 54 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 20 70 The last token p
6b4e0 61 72 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 arsed */. const
6b4f0 20 63 68 61 72 20 2a 7a 54 61 69 6c 3b 20 20 20 char *zTail;
6b500 2f 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78 74 20 /* All SQL text
6b510 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 73 65 past the last se
6b520 6d 69 63 6f 6c 6f 6e 20 70 61 72 73 65 64 20 2a micolon parsed *
6b530 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 54 /. Table *pNewT
6b540 61 62 6c 65 3b 20 20 20 20 2f 2a 20 41 20 74 61 able; /* A ta
6b550 62 6c 65 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 ble being constr
6b560 75 63 74 65 64 20 62 79 20 43 52 45 41 54 45 20 ucted by CREATE
6b570 54 41 42 4c 45 20 2a 2f 0a 20 20 54 72 69 67 67 TABLE */. Trigg
6b580 65 72 20 2a 70 4e 65 77 54 72 69 67 67 65 72 3b er *pNewTrigger;
6b590 20 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 /* Trigger
6b5a0 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 20 under construct
6b5b0 62 79 20 61 20 43 52 45 41 54 45 20 54 52 49 47 by a CREATE TRIG
6b5c0 47 45 52 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 GER */. const c
6b5d0 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 har *zAuthContex
6b5e0 74 3b 20 2f 2a 20 54 68 65 20 36 74 68 20 70 61 t; /* The 6th pa
6b5f0 72 61 6d 65 74 65 72 20 74 6f 20 64 62 2d 3e 78 rameter to db->x
6b600 41 75 74 68 20 63 61 6c 6c 62 61 63 6b 73 20 2a Auth callbacks *
6b610 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
6b620 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
6b630 4c 45 0a 20 20 54 6f 6b 65 6e 20 73 41 72 67 3b LE. Token sArg;
6b640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6b650 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 /* Complete text
6b660 20 6f 66 20 61 20 6d 6f 64 75 6c 65 20 61 72 67 of a module arg
6b670 75 6d 65 6e 74 20 2a 2f 0a 20 20 75 38 20 64 65 ument */. u8 de
6b680 63 6c 61 72 65 56 74 61 62 3b 20 20 20 20 20 20 clareVtab;
6b690 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
6b6a0 20 69 6e 73 69 64 65 20 73 71 6c 69 74 65 33 5f inside sqlite3_
6b6b0 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 2a declare_vtab() *
6b6c0 2f 0a 20 20 69 6e 74 20 6e 56 74 61 62 4c 6f 63 /. int nVtabLoc
6b6d0 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f k; /
6b6e0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 69 72 74 * Number of virt
6b6f0 75 61 6c 20 74 61 62 6c 65 73 20 74 6f 20 6c 6f ual tables to lo
6b700 63 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 2a ck */. Table **
6b710 61 70 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 20 apVtabLock;
6b720 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
6b730 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 virtual tables
6b740 6e 65 65 64 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 needing locking
6b750 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 */.#endif. int
6b760 6e 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 nHeight;
6b770 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f /* Expressio
6b780 6e 20 74 72 65 65 20 68 65 69 67 68 74 20 6f 66 n tree height of
6b790 20 63 75 72 72 65 6e 74 20 73 75 62 2d 73 65 6c current sub-sel
6b7a0 65 63 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a ect */. Table *
6b7b0 70 5a 6f 6d 62 69 65 54 61 62 3b 20 20 20 20 20 pZombieTab;
6b7c0 20 2f 2a 20 4c 69 73 74 20 6f 66 20 54 61 62 6c /* List of Tabl
6b7d0 65 20 6f 62 6a 65 63 74 73 20 74 6f 20 64 65 6c e objects to del
6b7e0 65 74 65 20 61 66 74 65 72 20 63 6f 64 65 20 67 ete after code g
6b7f0 65 6e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 50 en */. TriggerP
6b800 72 67 20 2a 70 54 72 69 67 67 65 72 50 72 67 3b rg *pTriggerPrg;
6b810 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 /* Linked li
6b820 73 74 20 6f 66 20 63 6f 64 65 64 20 74 72 69 67 st of coded trig
6b830 67 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 gers */.};..#ifd
6b840 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
6b850 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 23 64 IRTUALTABLE. #d
6b860 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 efine IN_DECLARE
6b870 5f 56 54 41 42 20 30 0a 23 65 6c 73 65 0a 20 20 _VTAB 0.#else.
6b880 23 64 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 #define IN_DECLA
6b890 52 45 5f 56 54 41 42 20 28 70 50 61 72 73 65 2d RE_VTAB (pParse-
6b8a0 3e 64 65 63 6c 61 72 65 56 74 61 62 29 0a 23 65 >declareVtab).#e
6b8b0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 ndif../*.** An i
6b8c0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
6b8d0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
6b8e0 72 65 20 63 61 6e 20 62 65 20 64 65 63 6c 61 72 re can be declar
6b8f0 65 64 20 6f 6e 20 61 20 73 74 61 63 6b 20 61 6e ed on a stack an
6b900 64 20 75 73 65 64 0a 2a 2a 20 74 6f 20 73 61 76 d used.** to sav
6b910 65 20 74 68 65 20 50 61 72 73 65 2e 7a 41 75 74 e the Parse.zAut
6b920 68 43 6f 6e 74 65 78 74 20 76 61 6c 75 65 20 73 hContext value s
6b930 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62 65 o that it can be
6b940 20 72 65 73 74 6f 72 65 64 20 6c 61 74 65 72 2e restored later.
6b950 0a 2a 2f 0a 73 74 72 75 63 74 20 41 75 74 68 43 .*/.struct AuthC
6b960 6f 6e 74 65 78 74 20 7b 0a 20 20 63 6f 6e 73 74 ontext {. const
6b970 20 63 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 char *zAuthCont
6b980 65 78 74 3b 20 20 20 2f 2a 20 50 75 74 20 73 61 ext; /* Put sa
6b990 76 65 64 20 50 61 72 73 65 2e 7a 41 75 74 68 43 ved Parse.zAuthC
6b9a0 6f 6e 74 65 78 74 20 68 65 72 65 20 2a 2f 0a 20 ontext here */.
6b9b0 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 Parse *pParse;
6b9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6b9d0 54 68 65 20 50 61 72 73 65 20 73 74 72 75 63 74 The Parse struct
6b9e0 75 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ure */.};../*.**
6b9f0 20 42 69 74 66 69 65 6c 64 20 66 6c 61 67 73 20 Bitfield flags
6ba00 66 6f 72 20 50 35 20 76 61 6c 75 65 20 69 6e 20 for P5 value in
6ba10 4f 50 5f 49 6e 73 65 72 74 20 61 6e 64 20 4f 50 OP_Insert and OP
6ba20 5f 44 65 6c 65 74 65 0a 2a 2f 0a 23 64 65 66 69 _Delete.*/.#defi
6ba30 6e 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 ne OPFLAG_NCHANG
6ba40 45 20 20 20 20 20 20 20 30 78 30 31 20 20 20 20 E 0x01
6ba50 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 /* Set to update
6ba60 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a db->nChange */.
6ba70 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 4c #define OPFLAG_L
6ba80 41 53 54 52 4f 57 49 44 20 20 20 20 20 30 78 30 ASTROWID 0x0
6ba90 32 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 75 2 /* Set to u
6baa0 70 64 61 74 65 20 64 62 2d 3e 6c 61 73 74 52 6f pdate db->lastRo
6bab0 77 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f wid */.#define O
6bac0 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45 20 20 PFLAG_ISUPDATE
6bad0 20 20 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54 0x04 /* T
6bae0 68 69 73 20 4f 50 5f 49 6e 73 65 72 74 20 69 73 his OP_Insert is
6baf0 20 61 6e 20 73 71 6c 20 55 50 44 41 54 45 20 2a an sql UPDATE *
6bb00 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 /.#define OPFLAG
6bb10 5f 41 50 50 45 4e 44 20 20 20 20 20 20 20 20 30 _APPEND 0
6bb20 78 30 38 20 20 20 20 2f 2a 20 54 68 69 73 20 69 x08 /* This i
6bb30 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 s likely to be a
6bb40 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 23 64 65 66 n append */.#def
6bb50 69 6e 65 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 ine OPFLAG_USESE
6bb60 45 4b 52 45 53 55 4c 54 20 30 78 31 30 20 20 20 EKRESULT 0x10
6bb70 20 2f 2a 20 54 72 79 20 74 6f 20 61 76 6f 69 64 /* Try to avoid
6bb80 20 61 20 73 65 65 6b 20 69 6e 20 42 74 72 65 65 a seek in Btree
6bb90 49 6e 73 65 72 74 28 29 20 2a 2f 0a 23 64 65 66 Insert() */.#def
6bba0 69 6e 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52 ine OPFLAG_CLEAR
6bbb0 43 41 43 48 45 20 20 20 20 30 78 32 30 20 20 20 CACHE 0x20
6bbc0 20 2f 2a 20 43 6c 65 61 72 20 70 73 65 75 64 6f /* Clear pseudo
6bbd0 2d 74 61 62 6c 65 20 63 61 63 68 65 20 69 6e 20 -table cache in
6bbe0 4f 50 5f 43 6f 6c 75 6d 6e 20 2a 2f 0a 0a 2f 2a OP_Column */../*
6bbf0 0a 20 2a 20 45 61 63 68 20 74 72 69 67 67 65 72 . * Each trigger
6bc00 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 present in the
6bc10 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 database schema
6bc20 69 73 20 73 74 6f 72 65 64 20 61 73 20 61 6e 20 is stored as an
6bc30 69 6e 73 74 61 6e 63 65 20 6f 66 0a 20 2a 20 73 instance of. * s
6bc40 74 72 75 63 74 20 54 72 69 67 67 65 72 2e 20 0a truct Trigger. .
6bc50 20 2a 0a 20 2a 20 50 6f 69 6e 74 65 72 73 20 74 *. * Pointers t
6bc60 6f 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 o instances of s
6bc70 74 72 75 63 74 20 54 72 69 67 67 65 72 20 61 72 truct Trigger ar
6bc80 65 20 73 74 6f 72 65 64 20 69 6e 20 74 77 6f 20 e stored in two
6bc90 77 61 79 73 2e 0a 20 2a 20 31 2e 20 49 6e 20 74 ways.. * 1. In t
6bca0 68 65 20 22 74 72 69 67 48 61 73 68 22 20 68 61 he "trigHash" ha
6bcb0 73 68 20 74 61 62 6c 65 20 28 70 61 72 74 20 6f sh table (part o
6bcc0 66 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 74 f the sqlite3* t
6bcd0 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 hat represents t
6bce0 68 65 20 0a 20 2a 20 20 20 20 64 61 74 61 62 61 he . * databa
6bcf0 73 65 29 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 se). This allows
6bd00 20 54 72 69 67 67 65 72 20 73 74 72 75 63 74 75 Trigger structu
6bd10 72 65 73 20 74 6f 20 62 65 20 72 65 74 72 69 65 res to be retrie
6bd20 76 65 64 20 62 79 20 6e 61 6d 65 2e 0a 20 2a 20 ved by name.. *
6bd30 32 2e 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20 2. All triggers
6bd40 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
6bd50 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 66 a single table f
6bd60 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 orm a linked lis
6bd70 74 2c 20 75 73 69 6e 67 20 74 68 65 0a 20 2a 20 t, using the. *
6bd80 20 20 20 70 4e 65 78 74 20 6d 65 6d 62 65 72 20 pNext member
6bd90 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 of struct Trigge
6bda0 72 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 r. A pointer to
6bdb0 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e the first elemen
6bdc0 74 20 6f 66 20 74 68 65 0a 20 2a 20 20 20 20 6c t of the. * l
6bdd0 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 73 74 inked list is st
6bde0 6f 72 65 64 20 61 73 20 74 68 65 20 22 70 54 72 ored as the "pTr
6bdf0 69 67 67 65 72 22 20 6d 65 6d 62 65 72 20 6f 66 igger" member of
6be00 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 0a the associated.
6be10 20 2a 20 20 20 20 73 74 72 75 63 74 20 54 61 62 * struct Tab
6be20 6c 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 22 73 le.. *. * The "s
6be30 74 65 70 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 tep_list" member
6be40 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 points to the f
6be50 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 irst element of
6be60 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 20 2a a linked list. *
6be70 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
6be80 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 73 SQL statements s
6be90 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65 20 pecified as the
6bea0 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e trigger program.
6beb0 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 . */.struct Trig
6bec0 67 65 72 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e ger {. char *zN
6bed0 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ame;
6bee0 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 /* The name of t
6bef0 68 65 20 74 72 69 67 67 65 72 20 20 20 20 20 20 he trigger
6bf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6bf10 20 20 2a 2f 0a 20 20 63 68 61 72 20 2a 74 61 62 */. char *tab
6bf20 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f le; /
6bf30 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76 * The table or v
6bf40 69 65 77 20 74 6f 20 77 68 69 63 68 20 74 68 65 iew to which the
6bf50 20 74 72 69 67 67 65 72 20 61 70 70 6c 69 65 73 trigger applies
6bf60 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 */. u8 op;
6bf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6bf80 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 One of TK_DELET
6bf90 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b E, TK_UPDATE, TK
6bfa0 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 _INSERT
6bfb0 2a 2f 0a 20 20 75 38 20 74 72 5f 74 6d 3b 20 20 */. u8 tr_tm;
6bfc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6bfd0 4f 6e 65 20 6f 66 20 54 52 49 47 47 45 52 5f 42 One of TRIGGER_B
6bfe0 45 46 4f 52 45 2c 20 54 52 49 47 47 45 52 5f 41 EFORE, TRIGGER_A
6bff0 46 54 45 52 20 2a 2f 0a 20 20 45 78 70 72 20 2a FTER */. Expr *
6c000 70 57 68 65 6e 3b 20 20 20 20 20 20 20 20 20 20 pWhen;
6c010 20 20 2f 2a 20 54 68 65 20 57 48 45 4e 20 63 6c /* The WHEN cl
6c020 61 75 73 65 20 6f 66 20 74 68 65 20 65 78 70 72 ause of the expr
6c030 65 73 73 69 6f 6e 20 28 6d 61 79 20 62 65 20 4e ession (may be N
6c040 55 4c 4c 29 20 2a 2f 0a 20 20 49 64 4c 69 73 74 ULL) */. IdList
6c050 20 2a 70 43 6f 6c 75 6d 6e 73 3b 20 20 20 20 20 *pColumns;
6c060 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
6c070 61 6e 20 55 50 44 41 54 45 20 4f 46 20 3c 63 6f an UPDATE OF <co
6c080 6c 75 6d 6e 2d 6c 69 73 74 3e 20 74 72 69 67 67 lumn-list> trigg
6c090 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 er,.
6c0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c0b0 20 74 68 65 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73 the <column-lis
6c0c0 74 3e 20 69 73 20 73 74 6f 72 65 64 20 68 65 72 t> is stored her
6c0d0 65 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 e */. Schema *p
6c0e0 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f Schema; /
6c0f0 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e * Schema contain
6c100 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72 20 ing the trigger
6c110 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 54 61 */. Schema *pTa
6c120 62 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20 bSchema; /*
6c130 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e Schema containin
6c140 67 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 g the table */.
6c150 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 74 TriggerStep *st
6c160 65 70 5f 6c 69 73 74 3b 20 2f 2a 20 4c 69 6e 6b ep_list; /* Link
6c170 20 6c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 list of trigger
6c180 20 70 72 6f 67 72 61 6d 20 73 74 65 70 73 20 20 program steps
6c190 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 */.
6c1a0 54 72 69 67 67 65 72 20 2a 70 4e 65 78 74 3b 20 Trigger *pNext;
6c1b0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 /* Next
6c1c0 74 72 69 67 67 65 72 20 61 73 73 6f 63 69 61 74 trigger associat
6c1d0 65 64 20 77 69 74 68 20 74 68 65 20 74 61 62 6c ed with the tabl
6c1e0 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 e */.};../*.** A
6c1f0 20 74 72 69 67 67 65 72 20 69 73 20 65 69 74 68 trigger is eith
6c200 65 72 20 61 20 42 45 46 4f 52 45 20 6f 72 20 61 er a BEFORE or a
6c210 6e 20 41 46 54 45 52 20 74 72 69 67 67 65 72 2e n AFTER trigger.
6c220 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
6c230 63 6f 6e 73 74 61 6e 74 73 0a 2a 2a 20 64 65 74 constants.** det
6c240 65 72 6d 69 6e 65 20 77 68 69 63 68 2e 20 0a 2a ermine which. .*
6c250 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 *.** If there ar
6c260 65 20 6d 75 6c 74 69 70 6c 65 20 74 72 69 67 67 e multiple trigg
6c270 65 72 73 2c 20 79 6f 75 20 6d 69 67 68 74 20 6f ers, you might o
6c280 66 20 73 6f 6d 65 20 42 45 46 4f 52 45 20 61 6e f some BEFORE an
6c290 64 20 73 6f 6d 65 20 41 46 54 45 52 2e 0a 2a 2a d some AFTER..**
6c2a0 20 49 6e 20 74 68 61 74 20 63 61 73 65 73 2c 20 In that cases,
6c2b0 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 the constants be
6c2c0 6c 6f 77 20 63 61 6e 20 62 65 20 4f 52 65 64 20 low can be ORed
6c2d0 74 6f 67 65 74 68 65 72 2e 0a 2a 2f 0a 23 64 65 together..*/.#de
6c2e0 66 69 6e 65 20 54 52 49 47 47 45 52 5f 42 45 46 fine TRIGGER_BEF
6c2f0 4f 52 45 20 20 31 0a 23 64 65 66 69 6e 65 20 54 ORE 1.#define T
6c300 52 49 47 47 45 52 5f 41 46 54 45 52 20 20 20 32 RIGGER_AFTER 2
6c310 0a 0a 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61 ../*. * An insta
6c320 6e 63 65 20 6f 66 20 73 74 72 75 63 74 20 54 72 nce of struct Tr
6c330 69 67 67 65 72 53 74 65 70 20 69 73 20 75 73 65 iggerStep is use
6c340 64 20 74 6f 20 73 74 6f 72 65 20 61 20 73 69 6e d to store a sin
6c350 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e gle SQL statemen
6c360 74 0a 20 2a 20 74 68 61 74 20 69 73 20 61 20 70 t. * that is a p
6c370 61 72 74 20 6f 66 20 61 20 74 72 69 67 67 65 72 art of a trigger
6c380 2d 70 72 6f 67 72 61 6d 2e 20 0a 20 2a 0a 20 2a -program. . *. *
6c390 20 49 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 74 Instances of st
6c3a0 72 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70 ruct TriggerStep
6c3b0 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61 are stored in a
6c3c0 20 73 69 6e 67 6c 79 20 6c 69 6e 6b 65 64 20 6c singly linked l
6c3d0 69 73 74 20 28 6c 69 6e 6b 65 64 0a 20 2a 20 75 ist (linked. * u
6c3e0 73 69 6e 67 20 74 68 65 20 22 70 4e 65 78 74 22 sing the "pNext"
6c3f0 20 6d 65 6d 62 65 72 29 20 72 65 66 65 72 65 6e member) referen
6c400 63 65 64 20 62 79 20 74 68 65 20 22 73 74 65 70 ced by the "step
6c410 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 20 6f 66 _list" member of
6c420 20 74 68 65 20 0a 20 2a 20 61 73 73 6f 63 69 61 the . * associa
6c430 74 65 64 20 73 74 72 75 63 74 20 54 72 69 67 67 ted struct Trigg
6c440 65 72 20 69 6e 73 74 61 6e 63 65 2e 20 54 68 65 er instance. The
6c450 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f first element o
6c460 66 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 f the linked lis
6c470 74 20 69 73 0a 20 2a 20 74 68 65 20 66 69 72 73 t is. * the firs
6c480 74 20 73 74 65 70 20 6f 66 20 74 68 65 20 74 72 t step of the tr
6c490 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 2e 0a 20 igger-program..
6c4a0 2a 20 0a 20 2a 20 54 68 65 20 22 6f 70 22 20 6d * . * The "op" m
6c4b0 65 6d 62 65 72 20 69 6e 64 69 63 61 74 65 73 20 ember indicates
6c4c0 77 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20 whether this is
6c4d0 61 20 22 44 45 4c 45 54 45 22 2c 20 22 49 4e 53 a "DELETE", "INS
6c4e0 45 52 54 22 2c 20 22 55 50 44 41 54 45 22 20 6f ERT", "UPDATE" o
6c4f0 72 0a 20 2a 20 22 53 45 4c 45 43 54 22 20 73 74 r. * "SELECT" st
6c500 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 6d 65 61 atement. The mea
6c510 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 6f 74 68 nings of the oth
6c520 65 72 20 6d 65 6d 62 65 72 73 20 69 73 20 64 65 er members is de
6c530 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 termined by the
6c540 0a 20 2a 20 76 61 6c 75 65 20 6f 66 20 22 6f 70 . * value of "op
6c550 22 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 2a " as follows:. *
6c560 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 49 4e . * (op == TK_IN
6c570 53 45 52 54 29 0a 20 2a 20 6f 72 63 6f 6e 66 20 SERT). * orconf
6c580 20 20 20 2d 3e 20 73 74 6f 72 65 73 20 74 68 65 -> stores the
6c590 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 ON CONFLICT alg
6c5a0 6f 72 69 74 68 6d 0a 20 2a 20 70 53 65 6c 65 63 orithm. * pSelec
6c5b0 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20 69 t -> If this i
6c5c0 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f s an INSERT INTO
6c5d0 20 2e 2e 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 ... SELECT ...
6c5e0 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a statement, then.
6c5f0 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
6c600 74 68 69 73 20 73 74 6f 72 65 73 20 61 20 70 6f this stores a po
6c610 69 6e 74 65 72 20 74 6f 20 74 68 65 20 53 45 4c inter to the SEL
6c620 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 4f ECT statement. O
6c630 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 therwise NULL..
6c640 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 * target -> A
6c650 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 token holding t
6c660 68 65 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f he quoted name o
6c670 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 69 f the table to i
6c680 6e 73 65 72 74 20 69 6e 74 6f 2e 0a 20 2a 20 70 nsert into.. * p
6c690 45 78 70 72 4c 69 73 74 20 2d 3e 20 49 66 20 74 ExprList -> If t
6c6a0 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 his is an INSERT
6c6b0 20 49 4e 54 4f 20 2e 2e 2e 20 56 41 4c 55 45 53 INTO ... VALUES
6c6c0 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c 20 ... statement,
6c6d0 74 68 65 6e 0a 20 2a 20 20 20 20 20 20 20 20 20 then. *
6c6e0 20 20 20 20 20 74 68 69 73 20 73 74 6f 72 65 73 this stores
6c6f0 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 69 6e values to be in
6c700 73 65 72 74 65 64 2e 20 4f 74 68 65 72 77 69 73 serted. Otherwis
6c710 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 49 64 4c 69 e NULL.. * pIdLi
6c720 73 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20 st -> If this
6c730 69 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 is an INSERT INT
6c740 4f 20 2e 2e 2e 20 28 3c 63 6f 6c 75 6d 6e 2d 6e O ... (<column-n
6c750 61 6d 65 73 3e 29 20 56 41 4c 55 45 53 20 2e 2e ames>) VALUES ..
6c760 2e 20 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 . . *
6c770 20 20 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 statement, th
6c780 65 6e 20 74 68 69 73 20 73 74 6f 72 65 73 20 74 en this stores t
6c790 68 65 20 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 20 he column-names
6c7a0 74 6f 20 62 65 0a 20 2a 20 20 20 20 20 20 20 20 to be. *
6c7b0 20 20 20 20 20 20 69 6e 73 65 72 74 65 64 20 69 inserted i
6c7c0 6e 74 6f 2e 0a 20 2a 0a 20 2a 20 28 6f 70 20 3d nto.. *. * (op =
6c7d0 3d 20 54 4b 5f 44 45 4c 45 54 45 29 0a 20 2a 20 = TK_DELETE). *
6c7e0 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 target -> A t
6c7f0 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 oken holding the
6c800 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 quoted name of
6c810 74 68 65 20 74 61 62 6c 65 20 74 6f 20 64 65 6c the table to del
6c820 65 74 65 20 66 72 6f 6d 2e 0a 20 2a 20 70 57 68 ete from.. * pWh
6c830 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20 57 48 ere -> The WH
6c840 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68 ERE clause of th
6c850 65 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 e DELETE stateme
6c860 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73 70 65 nt if one is spe
6c870 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20 20 20 cified.. *
6c880 20 20 20 20 20 20 20 20 4f 74 68 65 72 77 69 73 Otherwis
6c890 65 20 4e 55 4c 4c 2e 0a 20 2a 20 0a 20 2a 20 28 e NULL.. * . * (
6c8a0 6f 70 20 3d 3d 20 54 4b 5f 55 50 44 41 54 45 29 op == TK_UPDATE)
6c8b0 0a 20 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e . * target ->
6c8c0 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 A token holding
6c8d0 20 74 68 65 20 71 75 6f 74 65 64 20 6e 61 6d 65 the quoted name
6c8e0 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f of the table to
6c8f0 20 75 70 64 61 74 65 20 72 6f 77 73 20 6f 66 2e update rows of.
6c900 0a 20 2a 20 70 57 68 65 72 65 20 20 20 20 2d 3e . * pWhere ->
6c910 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 The WHERE claus
6c920 65 20 6f 66 20 74 68 65 20 55 50 44 41 54 45 20 e of the UPDATE
6c930 73 74 61 74 65 6d 65 6e 74 20 69 66 20 6f 6e 65 statement if one
6c940 20 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a 20 is specified..
6c950 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f * O
6c960 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 therwise NULL..
6c970 2a 20 70 45 78 70 72 4c 69 73 74 20 2d 3e 20 41 * pExprList -> A
6c980 20 6c 69 73 74 20 6f 66 20 74 68 65 20 63 6f 6c list of the col
6c990 75 6d 6e 73 20 74 6f 20 75 70 64 61 74 65 20 61 umns to update a
6c9a0 6e 64 20 74 68 65 20 65 78 70 72 65 73 73 69 6f nd the expressio
6c9b0 6e 73 20 74 6f 20 75 70 64 61 74 65 0a 20 2a 20 ns to update. *
6c9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 the
6c9d0 6d 20 74 6f 2e 20 53 65 65 20 73 71 6c 69 74 65 m to. See sqlite
6c9e0 33 55 70 64 61 74 65 28 29 20 64 6f 63 75 6d 65 3Update() docume
6c9f0 6e 74 61 74 69 6f 6e 20 6f 66 20 22 70 43 68 61 ntation of "pCha
6ca00 6e 67 65 73 22 0a 20 2a 20 20 20 20 20 20 20 20 nges". *
6ca10 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 0a argument..
6ca20 20 2a 20 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 * . */.struct T
6ca30 72 69 67 67 65 72 53 74 65 70 20 7b 0a 20 20 75 riggerStep {. u
6ca40 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 8 op;
6ca50 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b /* One of TK
6ca60 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 55 50 44 41 _DELETE, TK_UPDA
6ca70 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 TE, TK_INSERT, T
6ca80 4b 5f 53 45 4c 45 43 54 20 2a 2f 0a 20 20 75 38 K_SELECT */. u8
6ca90 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 orconf;
6caa0 20 20 20 2f 2a 20 4f 45 5f 52 6f 6c 6c 62 61 63 /* OE_Rollbac
6cab0 6b 20 65 74 63 2e 20 2a 2f 0a 20 20 54 72 69 67 k etc. */. Trig
6cac0 67 65 72 20 2a 70 54 72 69 67 3b 20 20 20 20 20 ger *pTrig;
6cad0 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 20 /* The trigger
6cae0 74 68 61 74 20 74 68 69 73 20 73 74 65 70 20 69 that this step i
6caf0 73 20 61 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 s a part of */.
6cb00 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 Select *pSelect
6cb10 3b 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 ; /* SELECT
6cb20 73 74 61 74 6d 65 6e 74 20 6f 72 20 52 48 53 20 statment or RHS
6cb30 6f 66 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e of INSERT INTO .
6cb40 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 2a 2f 0a . SELECT ... */.
6cb50 20 20 54 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20 Token target;
6cb60 20 20 20 20 20 20 20 2f 2a 20 54 61 72 67 65 74 /* Target
6cb70 20 74 61 62 6c 65 20 66 6f 72 20 44 45 4c 45 54 table for DELET
6cb80 45 2c 20 55 50 44 41 54 45 2c 20 49 4e 53 45 52 E, UPDATE, INSER
6cb90 54 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 T */. Expr *pWh
6cba0 65 72 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 ere; /* T
6cbb0 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
6cbc0 66 6f 72 20 44 45 4c 45 54 45 20 6f 72 20 55 50 for DELETE or UP
6cbd0 44 41 54 45 20 73 74 65 70 73 20 2a 2f 0a 20 20 DATE steps */.
6cbe0 45 78 70 72 4c 69 73 74 20 2a 70 45 78 70 72 4c ExprList *pExprL
6cbf0 69 73 74 3b 20 2f 2a 20 53 45 54 20 63 6c 61 75 ist; /* SET clau
6cc00 73 65 20 66 6f 72 20 55 50 44 41 54 45 2e 20 20 se for UPDATE.
6cc10 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 66 6f VALUES clause fo
6cc20 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 49 64 r INSERT */. Id
6cc30 4c 69 73 74 20 2a 70 49 64 4c 69 73 74 3b 20 20 List *pIdList;
6cc40 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d /* Column nam
6cc50 65 73 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f es for INSERT */
6cc60 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a . TriggerStep *
6cc70 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 pNext; /* Next
6cc80 69 6e 20 74 68 65 20 6c 69 6e 6b 2d 6c 69 73 74 in the link-list
6cc90 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 */. TriggerSte
6cca0 70 20 2a 70 4c 61 73 74 3b 20 20 2f 2a 20 4c 61 p *pLast; /* La
6ccb0 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 st element in li
6ccc0 6e 6b 2d 6c 69 73 74 2e 20 56 61 6c 69 64 20 66 nk-list. Valid f
6ccd0 6f 72 20 31 73 74 20 65 6c 65 6d 20 6f 6e 6c 79 or 1st elem only
6cce0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 */.};../*.** Th
6ccf0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
6cd00 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 69 cture contains i
6cd10 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 nformation used
6cd20 62 79 20 74 68 65 20 73 71 6c 69 74 65 46 69 78 by the sqliteFix
6cd30 2e 2e 2e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 ....** routines
6cd40 61 73 20 74 68 65 79 20 77 61 6c 6b 20 74 68 65 as they walk the
6cd50 20 70 61 72 73 65 20 74 72 65 65 20 74 6f 20 6d parse tree to m
6cd60 61 6b 65 20 64 61 74 61 62 61 73 65 20 72 65 66 ake database ref
6cd70 65 72 65 6e 63 65 73 0a 2a 2a 20 65 78 70 6c 69 erences.** expli
6cd80 63 69 74 2e 20 20 0a 2a 2f 0a 74 79 70 65 64 65 cit. .*/.typede
6cd90 66 20 73 74 72 75 63 74 20 44 62 46 69 78 65 72 f struct DbFixer
6cda0 20 44 62 46 69 78 65 72 3b 0a 73 74 72 75 63 74 DbFixer;.struct
6cdb0 20 44 62 46 69 78 65 72 20 7b 0a 20 20 50 61 72 DbFixer {. Par
6cdc0 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 se *pParse;
6cdd0 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 /* The parsing
6cde0 63 6f 6e 74 65 78 74 2e 20 20 45 72 72 6f 72 20 context. Error
6cdf0 6d 65 73 73 61 67 65 73 20 77 72 69 74 74 65 6e messages written
6ce00 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 here */. const
6ce10 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 2f char *zDb; /
6ce20 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 * Make sure all
6ce30 6f 62 6a 65 63 74 73 20 61 72 65 20 63 6f 6e 74 objects are cont
6ce40 61 69 6e 65 64 20 69 6e 20 74 68 69 73 20 64 61 ained in this da
6ce50 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 tabase */. cons
6ce60 74 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 t char *zType;
6ce70 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20 63 /* Type of the c
6ce80 6f 6e 74 61 69 6e 65 72 20 2d 20 75 73 65 64 20 ontainer - used
6ce90 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 for error messag
6cea0 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f es */. const To
6ceb0 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 2f 2a 20 4e ken *pName; /* N
6cec0 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 ame of the conta
6ced0 69 6e 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 iner - used for
6cee0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a error messages *
6cef0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f /.};../*.** An o
6cf00 62 6a 65 63 74 65 64 20 75 73 65 64 20 74 6f 20 bjected used to
6cf10 61 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 74 accumulate the t
6cf20 65 78 74 20 6f 66 20 61 20 73 74 72 69 6e 67 20 ext of a string
6cf30 77 68 65 72 65 20 77 65 0a 2a 2a 20 64 6f 20 6e where we.** do n
6cf40 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 6b ot necessarily k
6cf50 6e 6f 77 20 68 6f 77 20 62 69 67 20 74 68 65 20 now how big the
6cf60 73 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 69 string will be i
6cf70 6e 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 n the end..*/.st
6cf80 72 75 63 74 20 53 74 72 41 63 63 75 6d 20 7b 0a ruct StrAccum {.
6cf90 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
6cfa0 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e /* Option
6cfb0 61 6c 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 al database for
6cfc0 6c 6f 6f 6b 61 73 69 64 65 2e 20 20 43 61 6e 20 lookaside. Can
6cfd0 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68 61 be NULL */. cha
6cfe0 72 20 2a 7a 42 61 73 65 3b 20 20 20 20 20 20 20 r *zBase;
6cff0 20 20 2f 2a 20 41 20 62 61 73 65 20 61 6c 6c 6f /* A base allo
6d000 63 61 74 69 6f 6e 2e 20 20 4e 6f 74 20 66 72 6f cation. Not fro
6d010 6d 20 6d 61 6c 6c 6f 63 2e 20 2a 2f 0a 20 20 63 m malloc. */. c
6d020 68 61 72 20 2a 7a 54 65 78 74 3b 20 20 20 20 20 har *zText;
6d030 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e /* The strin
6d040 67 20 63 6f 6c 6c 65 63 74 65 64 20 73 6f 20 66 g collected so f
6d050 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 43 68 ar */. int nCh
6d060 61 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ar; /*
6d070 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74 Length of the st
6d080 72 69 6e 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20 ring so far */.
6d090 20 69 6e 74 20 20 6e 41 6c 6c 6f 63 3b 20 20 20 int nAlloc;
6d0a0 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 /* Amount
6d0b0 6f 66 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 of space allocat
6d0c0 65 64 20 69 6e 20 7a 54 65 78 74 20 2a 2f 0a 20 ed in zText */.
6d0d0 20 69 6e 74 20 20 6d 78 41 6c 6c 6f 63 3b 20 20 int mxAlloc;
6d0e0 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d /* Maximum
6d0f0 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 allowed string
6d100 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 75 38 20 20 length */. u8
6d110 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 mallocFailed;
6d120 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65 /* Becomes true
6d130 20 69 66 20 61 6e 79 20 6d 65 6d 6f 72 79 20 61 if any memory a
6d140 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 20 llocation fails
6d150 2a 2f 0a 20 20 75 38 20 20 20 75 73 65 4d 61 6c */. u8 useMal
6d160 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 loc; /* Tru
6d170 65 20 69 66 20 7a 54 65 78 74 20 69 73 20 65 6e e if zText is en
6d180 6c 61 72 67 65 61 62 6c 65 20 75 73 69 6e 67 20 largeable using
6d190 72 65 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 38 20 realloc */. u8
6d1a0 20 20 74 6f 6f 42 69 67 3b 20 20 20 20 20 20 20 tooBig;
6d1b0 20 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 /* Becomes tru
6d1c0 65 20 69 66 20 73 74 72 69 6e 67 20 73 69 7a 65 e if string size
6d1d0 20 65 78 63 65 65 64 73 20 6c 69 6d 69 74 73 20 exceeds limits
6d1e0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 70 */.};../*.** A p
6d1f0 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 ointer to this s
6d200 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64 tructure is used
6d210 20 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20 to communicate
6d220 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 information.** f
6d230 72 6f 6d 20 73 71 6c 69 74 65 33 49 6e 69 74 20 rom sqlite3Init
6d240 61 6e 64 20 4f 50 5f 50 61 72 73 65 53 63 68 65 and OP_ParseSche
6d250 6d 61 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 ma into the sqli
6d260 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2e te3InitCallback.
6d270 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
6d280 63 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a ct {. sqlite3 *
6d290 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 db; /* Th
6d2a0 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 e database being
6d2b0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a initialized */.
6d2c0 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 int iDb;
6d2d0 20 20 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d /* 0 for m
6d2e0 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 20 31 ain database. 1
6d2f0 20 66 6f 72 20 54 45 4d 50 2c 20 32 2e 2e 20 66 for TEMP, 2.. f
6d300 6f 72 20 41 54 54 41 43 48 65 64 20 2a 2f 0a 20 or ATTACHed */.
6d310 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 char **pzErrMsg
6d320 3b 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 ; /* Error me
6d330 73 73 61 67 65 20 73 74 6f 72 65 64 20 68 65 72 ssage stored her
6d340 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 e */. int rc;
6d350 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
6d360 73 75 6c 74 20 63 6f 64 65 20 73 74 6f 72 65 64 sult code stored
6d370 20 68 65 72 65 20 2a 2f 0a 7d 20 49 6e 69 74 44 here */.} InitD
6d380 61 74 61 3b 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 ata;../*.** Stru
6d390 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 cture containing
6d3a0 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 global configur
6d3b0 61 74 69 6f 6e 20 64 61 74 61 20 66 6f 72 20 74 ation data for t
6d3c0 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 he SQLite librar
6d3d0 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 y..**.** This st
6d3e0 72 75 63 74 75 72 65 20 61 6c 73 6f 20 63 6f 6e ructure also con
6d3f0 74 61 69 6e 73 20 73 6f 6d 65 20 73 74 61 74 65 tains some state
6d400 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f information..*/
6d410 0a 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 .struct Sqlite3C
6d420 6f 6e 66 69 67 20 7b 0a 20 20 69 6e 74 20 62 4d onfig {. int bM
6d430 65 6d 73 74 61 74 3b 20 20 20 20 20 20 20 20 20 emstat;
6d440 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
6d450 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 6d 65 rue to enable me
6d460 6d 6f 72 79 20 73 74 61 74 75 73 20 2a 2f 0a 20 mory status */.
6d470 20 69 6e 74 20 62 43 6f 72 65 4d 75 74 65 78 3b int bCoreMutex;
6d480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d490 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e /* True to en
6d4a0 61 62 6c 65 20 63 6f 72 65 20 6d 75 74 65 78 69 able core mutexi
6d4b0 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 46 75 6c ng */. int bFul
6d4c0 6c 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 lMutex;
6d4d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
6d4e0 65 20 74 6f 20 65 6e 61 62 6c 65 20 66 75 6c 6c e to enable full
6d4f0 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 69 mutexing */. i
6d500 6e 74 20 6d 78 53 74 72 6c 65 6e 3b 20 20 20 20 nt mxStrlen;
6d510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d520 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 74 72 69 /* Maximum stri
6d530 6e 67 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 69 ng length */. i
6d540 6e 74 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 3b 20 nt szLookaside;
6d550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d560 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f 6b /* Default look
6d570 61 73 69 64 65 20 62 75 66 66 65 72 20 73 69 7a aside buffer siz
6d580 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 6f 6b e */. int nLook
6d590 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 aside;
6d5a0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 /* Defa
6d5b0 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 ult lookaside bu
6d5c0 66 66 65 72 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 ffer count */.
6d5d0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
6d5e0 6f 64 73 20 6d 3b 20 20 20 20 20 20 20 20 20 20 ods m;
6d5f0 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d /* Low-level m
6d600 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
6d610 20 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 interface */.
6d620 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
6d630 74 68 6f 64 73 20 6d 75 74 65 78 3b 20 20 20 20 thods mutex;
6d640 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d /* Low-level m
6d650 75 74 65 78 20 69 6e 74 65 72 66 61 63 65 20 2a utex interface *
6d660 2f 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 /. sqlite3_pcac
6d670 68 65 5f 6d 65 74 68 6f 64 73 20 70 63 61 63 68 he_methods pcach
6d680 65 3b 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 e; /* Low-lev
6d690 65 6c 20 70 61 67 65 2d 63 61 63 68 65 20 69 6e el page-cache in
6d6a0 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 76 6f 69 terface */. voi
6d6b0 64 20 2a 70 48 65 61 70 3b 20 20 20 20 20 20 20 d *pHeap;
6d6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6d6d0 2a 20 48 65 61 70 20 73 74 6f 72 61 67 65 20 73 * Heap storage s
6d6e0 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 pace */. int nH
6d6f0 65 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 eap;
6d700 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
6d710 69 7a 65 20 6f 66 20 70 48 65 61 70 5b 5d 20 2a ize of pHeap[] *
6d720 2f 0a 20 20 69 6e 74 20 6d 6e 52 65 71 2c 20 6d /. int mnReq, m
6d730 78 52 65 71 3b 20 20 20 20 20 20 20 20 20 20 20 xReq;
6d740 20 20 20 20 20 20 2f 2a 20 4d 69 6e 20 61 6e 64 /* Min and
6d750 20 6d 61 78 20 68 65 61 70 20 72 65 71 75 65 73 max heap reques
6d760 74 73 20 73 69 7a 65 73 20 2a 2f 0a 20 20 76 6f ts sizes */. vo
6d770 69 64 20 2a 70 53 63 72 61 74 63 68 3b 20 20 20 id *pScratch;
6d780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d790 2f 2a 20 53 63 72 61 74 63 68 20 6d 65 6d 6f 72 /* Scratch memor
6d7a0 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 y */. int szScr
6d7b0 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 atch;
6d7c0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
6d7d0 20 6f 66 20 65 61 63 68 20 73 63 72 61 74 63 68 of each scratch
6d7e0 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 buffer */. int
6d7f0 20 6e 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 nScratch;
6d800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6d810 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 63 72 61 * Number of scra
6d820 74 63 68 20 62 75 66 66 65 72 73 20 2a 2f 0a 20 tch buffers */.
6d830 20 76 6f 69 64 20 2a 70 50 61 67 65 3b 20 20 20 void *pPage;
6d840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d850 20 20 20 2f 2a 20 50 61 67 65 20 63 61 63 68 65 /* Page cache
6d860 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 memory */. int
6d870 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20 szPage;
6d880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6d890 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 70 * Size of each p
6d8a0 61 67 65 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a age in pPage[] *
6d8b0 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 /. int nPage;
6d8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d8d0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
6d8e0 6f 66 20 70 61 67 65 73 20 69 6e 20 70 50 61 67 of pages in pPag
6d8f0 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 e[] */. int mxP
6d900 61 72 73 65 72 53 74 61 63 6b 3b 20 20 20 20 20 arserStack;
6d910 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 /* ma
6d920 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 ximum depth of t
6d930 68 65 20 70 61 72 73 65 72 20 73 74 61 63 6b 20 he parser stack
6d940 2a 2f 0a 20 20 69 6e 74 20 73 68 61 72 65 64 43 */. int sharedC
6d950 61 63 68 65 45 6e 61 62 6c 65 64 3b 20 20 20 20 acheEnabled;
6d960 20 20 20 20 20 20 20 2f 2a 20 74 72 75 65 20 69 /* true i
6d970 66 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d f shared-cache m
6d980 6f 64 65 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 ode enabled */.
6d990 20 2f 2a 20 54 68 65 20 61 62 6f 76 65 20 6d 69 /* The above mi
6d9a0 67 68 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a ght be initializ
6d9b0 65 64 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 ed to non-zero.
6d9c0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e The following n
6d9d0 65 65 64 20 74 6f 20 61 6c 77 61 79 73 0a 20 20 eed to always.
6d9e0 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 62 65 20 ** initially be
6d9f0 7a 65 72 6f 2c 20 68 6f 77 65 76 65 72 2e 20 2a zero, however. *
6da00 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74 3b 20 /. int isInit;
6da10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6da20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 /* True af
6da30 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 ter initializati
6da40 6f 6e 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 on has finished
6da50 2a 2f 0a 20 20 69 6e 74 20 69 6e 50 72 6f 67 72 */. int inProgr
6da60 65 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ess;
6da70 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77 /* True w
6da80 68 69 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 hile initializat
6da90 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20 ion in progress
6daa0 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 75 74 65 78 */. int isMutex
6dab0 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 Init;
6dac0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 /* True a
6dad0 66 74 65 72 20 6d 75 74 65 78 65 73 20 61 72 65 fter mutexes are
6dae0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a initialized */.
6daf0 20 20 69 6e 74 20 69 73 4d 61 6c 6c 6f 63 49 6e int isMallocIn
6db00 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 it;
6db10 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65 /* True afte
6db20 72 20 6d 61 6c 6c 6f 63 20 69 73 20 69 6e 69 74 r malloc is init
6db30 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 ialized */. int
6db40 20 69 73 50 43 61 63 68 65 49 6e 69 74 3b 20 20 isPCacheInit;
6db50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6db60 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c * True after mal
6db70 6c 6f 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a loc is initializ
6db80 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f ed */. sqlite3_
6db90 6d 75 74 65 78 20 2a 70 49 6e 69 74 4d 75 74 65 mutex *pInitMute
6dba0 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 x; /* Mut
6dbb0 65 78 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 ex used by sqlit
6dbc0 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
6dbd0 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 49 6e 69 */. int nRefIni
6dbe0 74 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 tMutex;
6dbf0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6dc00 20 6f 66 20 75 73 65 72 73 20 6f 66 20 70 49 6e of users of pIn
6dc10 69 74 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f itMutex */.};../
6dc20 2a 0a 2a 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 *.** Context poi
6dc30 6e 74 65 72 20 70 61 73 73 65 64 20 64 6f 77 6e nter passed down
6dc40 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 72 65 through the tre
6dc50 65 2d 77 61 6c 6b 2e 0a 2a 2f 0a 73 74 72 75 63 e-walk..*/.struc
6dc60 74 20 57 61 6c 6b 65 72 20 7b 0a 20 20 69 6e 74 t Walker {. int
6dc70 20 28 2a 78 45 78 70 72 43 61 6c 6c 62 61 63 6b (*xExprCallback
6dc80 29 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a )(Walker*, Expr*
6dc90 29 3b 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 ); /* Callba
6dca0 63 6b 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f ck for expressio
6dcb0 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 ns */. int (*xS
6dcc0 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 29 28 57 electCallback)(W
6dcd0 61 6c 6b 65 72 2a 2c 53 65 6c 65 63 74 2a 29 3b alker*,Select*);
6dce0 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f /* Callback fo
6dcf0 72 20 53 45 4c 45 43 54 73 20 2a 2f 0a 20 20 50 r SELECTs */. P
6dd00 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 arse *pParse;
6dd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6dd20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 /* Pars
6dd30 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 2a 2f 0a er context. */.
6dd40 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 union {
6dd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6dd60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
6dd70 78 74 72 61 20 64 61 74 61 20 66 6f 72 20 63 61 xtra data for ca
6dd80 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61 llback */. Na
6dd90 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20 meContext *pNC;
6dda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ddb0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69 /* Nami
6ddc0 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
6ddd0 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
6dde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ddf0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6de00 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 2a 2f Integer value */
6de10 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46 . } u;.};../* F
6de20 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 orward declarati
6de30 6f 6e 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ons */.SQLITE_PR
6de40 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6de50 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 3WalkExpr(Walker
6de60 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 *, Expr*);.SQLIT
6de70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6de80 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 lite3WalkExprLis
6de90 74 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c t(Walker*, ExprL
6dea0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ist*);.SQLITE_PR
6deb0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6dec0 33 57 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 3WalkSelect(Walk
6ded0 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 er*, Select*);.S
6dee0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6def0 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c t sqlite3WalkSel
6df00 65 63 74 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c ectExpr(Walker*,
6df10 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 Select*);.SQLIT
6df20 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6df30 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 lite3WalkSelectF
6df40 72 6f 6d 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c rom(Walker*, Sel
6df50 65 63 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 ect*);../*.** Re
6df60 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 turn code from t
6df70 68 65 20 70 61 72 73 65 2d 74 72 65 65 20 77 61 he parse-tree wa
6df80 6c 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73 lking primitives
6df90 20 61 6e 64 20 74 68 65 69 72 0a 2a 2a 20 63 61 and their.** ca
6dfa0 6c 6c 62 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66 llbacks..*/.#def
6dfb0 69 6e 65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 ine WRC_Continue
6dfc0 20 20 20 20 30 20 20 20 2f 2a 20 43 6f 6e 74 69 0 /* Conti
6dfd0 6e 75 65 20 64 6f 77 6e 20 69 6e 74 6f 20 63 68 nue down into ch
6dfe0 69 6c 64 72 65 6e 20 2a 2f 0a 23 64 65 66 69 6e ildren */.#defin
6dff0 65 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 e WRC_Prune
6e000 20 20 31 20 20 20 2f 2a 20 4f 6d 69 74 20 63 68 1 /* Omit ch
6e010 69 6c 64 72 65 6e 20 62 75 74 20 63 6f 6e 74 69 ildren but conti
6e020 6e 75 65 20 77 61 6c 6b 69 6e 67 20 73 69 62 6c nue walking sibl
6e030 69 6e 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ings */.#define
6e040 57 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 WRC_Abort
6e050 32 20 20 20 2f 2a 20 41 62 61 6e 64 6f 6e 20 74 2 /* Abandon t
6e060 68 65 20 74 72 65 65 20 77 61 6c 6b 20 2a 2f 0a he tree walk */.
6e070 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 ./*.** Assuming
6e080 7a 49 6e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 zIn points to th
6e090 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 e first byte of
6e0a0 61 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 a UTF-8 characte
6e0b0 72 2c 0a 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49 r,.** advance zI
6e0c0 6e 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 n to point to th
6e0d0 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 e first byte of
6e0e0 74 68 65 20 6e 65 78 74 20 55 54 46 2d 38 20 63 the next UTF-8 c
6e0f0 68 61 72 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65 haracter..*/.#de
6e100 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4b 49 50 fine SQLITE_SKIP
6e110 5f 55 54 46 38 28 7a 49 6e 29 20 7b 20 20 20 20 _UTF8(zIn) {
6e120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e130 20 20 20 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a \. if( (*(z
6e140 49 6e 2b 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20 In++))>=0xc0 ){
6e150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e160 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
6e170 20 20 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 while( (*zIn
6e180 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b & 0xc0)==0x80 ){
6e190 20 7a 49 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20 zIn++; }
6e1a0 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 \. }
6e1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
6e1e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 .}../*.** The SQ
6e1f0 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
6e200 54 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 65 T macro can be e
6e210 69 74 68 65 72 20 61 20 63 6f 6e 73 74 61 6e 74 ither a constant
6e220 20 28 66 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e (for production
6e230 0a 2a 2a 20 62 75 69 6c 64 73 29 20 6f 72 20 61 .** builds) or a
6e240 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 28 function call (
6e250 66 6f 72 20 64 65 62 75 67 67 69 6e 67 29 2e 20 for debugging).
6e260 20 49 66 20 69 74 20 69 73 20 61 20 66 75 6e 63 If it is a func
6e270 74 69 6f 6e 20 63 61 6c 6c 2c 0a 2a 2a 20 69 74 tion call,.** it
6e280 20 61 6c 6c 6f 77 73 20 74 68 65 20 6f 70 65 72 allows the oper
6e290 61 74 6f 72 20 74 6f 20 73 65 74 20 61 20 62 72 ator to set a br
6e2a0 65 61 6b 70 6f 69 6e 74 20 61 74 20 74 68 65 20 eakpoint at the
6e2b0 73 70 6f 74 20 77 68 65 72 65 20 64 61 74 61 62 spot where datab
6e2c0 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f ase.** corruptio
6e2d0 6e 20 69 73 20 66 69 72 73 74 20 64 65 74 65 63 n is first detec
6e2e0 74 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ted..*/.#ifdef S
6e2f0 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 QLITE_DEBUG.SQLI
6e300 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
6e310 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28 sqlite3Corrupt(
6e320 76 6f 69 64 29 3b 0a 23 20 64 65 66 69 6e 65 20 void);.# define
6e330 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
6e340 4b 50 54 20 73 71 6c 69 74 65 33 43 6f 72 72 75 KPT sqlite3Corru
6e350 70 74 28 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 pt().#else.# def
6e360 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 ine SQLITE_CORRU
6e370 50 54 5f 42 4b 50 54 20 53 51 4c 49 54 45 5f 43 PT_BKPT SQLITE_C
6e380 4f 52 52 55 50 54 0a 23 65 6e 64 69 66 0a 0a 2f ORRUPT.#endif../
6e390 2a 0a 2a 2a 20 54 68 65 20 63 74 79 70 65 2e 68 *.** The ctype.h
6e3a0 20 68 65 61 64 65 72 20 69 73 20 6e 65 65 64 65 header is neede
6e3b0 64 20 66 6f 72 20 6e 6f 6e 2d 41 53 43 49 49 20 d for non-ASCII
6e3c0 73 79 73 74 65 6d 73 2e 20 20 49 74 20 69 73 20 systems. It is
6e3d0 61 6c 73 6f 0a 2a 2a 20 6e 65 65 64 65 64 20 62 also.** needed b
6e3e0 79 20 46 54 53 33 20 77 68 65 6e 20 46 54 53 33 y FTS3 when FTS3
6e3f0 20 69 73 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 is included in
6e400 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e the amalgamation
6e410 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 ..*/.#if !define
6e420 64 28 53 51 4c 49 54 45 5f 41 53 43 49 49 29 20 d(SQLITE_ASCII)
6e430 7c 7c 20 5c 0a 20 20 20 20 28 64 65 66 69 6e 65 || \. (define
6e440 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d(SQLITE_ENABLE_
6e450 46 54 53 33 29 20 26 26 20 64 65 66 69 6e 65 64 FTS3) && defined
6e460 28 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 (SQLITE_AMALGAMA
6e470 54 49 4f 4e 29 29 0a 23 20 69 6e 63 6c 75 64 65 TION)).# include
6e480 20 3c 63 74 79 70 65 2e 68 3e 0a 23 65 6e 64 69 <ctype.h>.#endi
6e490 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c f../*.** The fol
6e4a0 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 69 lowing macros mi
6e4b0 6d 69 63 20 74 68 65 20 73 74 61 6e 64 61 72 64 mic the standard
6e4c0 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f library functio
6e4d0 6e 73 20 74 6f 75 70 70 65 72 28 29 2c 0a 2a 2a ns toupper(),.**
6e4e0 20 69 73 73 70 61 63 65 28 29 2c 20 69 73 61 6c isspace(), isal
6e4f0 6e 75 6d 28 29 2c 20 69 73 64 69 67 69 74 28 29 num(), isdigit()
6e500 20 61 6e 64 20 69 73 78 64 69 67 69 74 28 29 2c and isxdigit(),
6e510 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54 respectively. T
6e520 68 65 0a 2a 2a 20 73 71 6c 69 74 65 20 76 65 72 he.** sqlite ver
6e530 73 69 6f 6e 73 20 6f 6e 6c 79 20 77 6f 72 6b 20 sions only work
6e540 66 6f 72 20 41 53 43 49 49 20 63 68 61 72 61 63 for ASCII charac
6e550 74 65 72 73 2c 20 72 65 67 61 72 64 6c 65 73 73 ters, regardless
6e560 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 23 of locale..*/.#
6e570 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 ifdef SQLITE_ASC
6e580 49 49 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 II.# define sqli
6e590 74 65 33 54 6f 75 70 70 65 72 28 78 29 20 20 28 te3Toupper(x) (
6e5a0 28 78 29 26 7e 28 73 71 6c 69 74 65 33 43 74 79 (x)&~(sqlite3Cty
6e5b0 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 peMap[(unsigned
6e5c0 63 68 61 72 29 28 78 29 5d 26 30 78 32 30 29 29 char)(x)]&0x20))
6e5d0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6e5e0 33 49 73 73 70 61 63 65 28 78 29 20 20 20 28 73 3Isspace(x) (s
6e5f0 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 qlite3CtypeMap[(
6e600 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 unsigned char)(x
6e610 29 5d 26 30 78 30 31 29 0a 23 20 64 65 66 69 6e )]&0x01).# defin
6e620 65 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d e sqlite3Isalnum
6e630 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 43 74 (x) (sqlite3Ct
6e640 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 ypeMap[(unsigned
6e650 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 36 29 char)(x)]&0x06)
6e660 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6e670 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 28 73 3Isalpha(x) (s
6e680 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 qlite3CtypeMap[(
6e690 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 unsigned char)(x
6e6a0 29 5d 26 30 78 30 32 29 0a 23 20 64 65 66 69 6e )]&0x02).# defin
6e6b0 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 e sqlite3Isdigit
6e6c0 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 43 74 (x) (sqlite3Ct
6e6d0 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 ypeMap[(unsigned
6e6e0 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 34 29 char)(x)]&0x04)
6e6f0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6e700 33 49 73 78 64 69 67 69 74 28 78 29 20 20 28 73 3Isxdigit(x) (s
6e710 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 qlite3CtypeMap[(
6e720 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 unsigned char)(x
6e730 29 5d 26 30 78 30 38 29 0a 23 20 64 65 66 69 6e )]&0x08).# defin
6e740 65 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 e sqlite3Tolower
6e750 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 55 70 (x) (sqlite3Up
6e760 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 perToLower[(unsi
6e770 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 29 0a gned char)(x)]).
6e780 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 #else.# define s
6e790 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 78 29 qlite3Toupper(x)
6e7a0 20 20 20 74 6f 75 70 70 65 72 28 28 75 6e 73 69 toupper((unsi
6e7b0 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 gned char)(x)).#
6e7c0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 define sqlite3I
6e7d0 73 73 70 61 63 65 28 78 29 20 20 20 69 73 73 70 sspace(x) issp
6e7e0 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20 63 68 ace((unsigned ch
6e7f0 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 ar)(x)).# define
6e800 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 sqlite3Isalnum(
6e810 78 29 20 20 20 69 73 61 6c 6e 75 6d 28 28 75 6e x) isalnum((un
6e820 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 signed char)(x))
6e830 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6e840 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 69 73 3Isalpha(x) is
6e850 61 6c 70 68 61 28 28 75 6e 73 69 67 6e 65 64 20 alpha((unsigned
6e860 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 char)(x)).# defi
6e870 6e 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 ne sqlite3Isdigi
6e880 74 28 78 29 20 20 20 69 73 64 69 67 69 74 28 28 t(x) isdigit((
6e890 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 unsigned char)(x
6e8a0 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 )).# define sqli
6e8b0 74 65 33 49 73 78 64 69 67 69 74 28 78 29 20 20 te3Isxdigit(x)
6e8c0 69 73 78 64 69 67 69 74 28 28 75 6e 73 69 67 6e isxdigit((unsign
6e8d0 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 ed char)(x)).# d
6e8e0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 6c efine sqlite3Tol
6e8f0 6f 77 65 72 28 78 29 20 20 20 74 6f 6c 6f 77 65 ower(x) tolowe
6e900 72 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 r((unsigned char
6e910 29 28 78 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a )(x)).#endif../*
6e920 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e .** Internal fun
6e930 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 ction prototypes
6e940 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
6e950 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 TE int sqlite3St
6e960 72 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 rICmp(const char
6e970 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *, const char *
6e980 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6e990 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e E int sqlite3IsN
6e9a0 75 6d 62 65 72 28 63 6f 6e 73 74 20 63 68 61 72 umber(const char
6e9b0 2a 2c 20 69 6e 74 2a 2c 20 75 38 29 3b 0a 53 51 *, int*, u8);.SQ
6e9c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6e9d0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
6e9e0 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 (const char*);.#
6e9f0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 define sqlite3St
6ea00 72 4e 49 43 6d 70 20 73 71 6c 69 74 65 33 5f 73 rNICmp sqlite3_s
6ea10 74 72 6e 69 63 6d 70 0a 0a 53 51 4c 49 54 45 5f trnicmp..SQLITE_
6ea20 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6ea30 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f te3MallocInit(vo
6ea40 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 id);.SQLITE_PRIV
6ea50 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6ea60 4d 61 6c 6c 6f 63 45 6e 64 28 76 6f 69 64 29 3b MallocEnd(void);
6ea70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ea80 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c void *sqlite3Mal
6ea90 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 loc(int);.SQLITE
6eaa0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
6eab0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
6eac0 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 (int);.SQLITE_PR
6ead0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
6eae0 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 te3DbMallocZero(
6eaf0 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a sqlite3*, int);.
6eb00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6eb10 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 oid *sqlite3DbMa
6eb20 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33 2a llocRaw(sqlite3*
6eb30 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
6eb40 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
6eb50 69 74 65 33 44 62 53 74 72 44 75 70 28 73 71 6c ite3DbStrDup(sql
6eb60 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 ite3*,const char
6eb70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6eb80 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
6eb90 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69 74 65 DbStrNDup(sqlite
6eba0 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 3*,const char*,
6ebb0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6ebc0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
6ebd0 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c e3Realloc(void*,
6ebe0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
6ebf0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
6ec00 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 te3DbReallocOrFr
6ec10 65 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f ee(sqlite3 *, vo
6ec20 69 64 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 id *, int);.SQLI
6ec30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6ec40 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f *sqlite3DbReallo
6ec50 63 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 c(sqlite3 *, voi
6ec60 64 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 d *, int);.SQLIT
6ec70 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6ec80 71 6c 69 74 65 33 44 62 46 72 65 65 28 73 71 6c qlite3DbFree(sql
6ec90 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 ite3*, void*);.S
6eca0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6ecb0 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 t sqlite3MallocS
6ecc0 69 7a 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 ize(void*);.SQLI
6ecd0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6ece0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 qlite3DbMallocSi
6ecf0 7a 65 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 ze(sqlite3*, voi
6ed00 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 d*);.SQLITE_PRIV
6ed10 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
6ed20 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69 3ScratchMalloc(i
6ed30 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6ed40 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6ed50 53 63 72 61 74 63 68 46 72 65 65 28 76 6f 69 64 ScratchFree(void
6ed60 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6ed70 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
6ed80 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b PageMalloc(int);
6ed90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6eda0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
6edb0 46 72 65 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c Free(void*);.SQL
6edc0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6edd0 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 sqlite3MemSetDe
6ede0 66 61 75 6c 74 28 76 6f 69 64 29 3b 0a 53 51 4c fault(void);.SQL
6edf0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6ee00 20 73 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 sqlite3BenignMa
6ee10 6c 6c 6f 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28 llocHooks(void (
6ee20 2a 29 28 76 6f 69 64 29 2c 20 76 6f 69 64 20 28 *)(void), void (
6ee30 2a 29 28 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54 *)(void));.SQLIT
6ee40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6ee50 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d lite3MemoryAlarm
6ee60 28 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 2c (void (*)(void*,
6ee70 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 sqlite3_int64,
6ee80 69 6e 74 29 2c 20 76 6f 69 64 2a 2c 20 73 71 6c int), void*, sql
6ee90 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 0a 2f 2a ite3_int64);../*
6eea0 0a 2a 2a 20 4f 6e 20 73 79 73 74 65 6d 73 20 77 .** On systems w
6eeb0 69 74 68 20 61 6d 70 6c 65 20 73 74 61 63 6b 20 ith ample stack
6eec0 73 70 61 63 65 20 61 6e 64 20 74 68 61 74 20 73 space and that s
6eed0 75 70 70 6f 72 74 20 61 6c 6c 6f 63 61 28 29 2c upport alloca(),
6eee0 20 6d 61 6b 65 0a 2a 2a 20 75 73 65 20 6f 66 20 make.** use of
6eef0 61 6c 6c 6f 63 61 28 29 20 74 6f 20 6f 62 74 61 alloca() to obta
6ef00 69 6e 20 73 70 61 63 65 20 66 6f 72 20 6c 61 72 in space for lar
6ef10 67 65 20 61 75 74 6f 6d 61 74 69 63 20 6f 62 6a ge automatic obj
6ef20 65 63 74 73 2e 20 20 42 79 20 64 65 66 61 75 6c ects. By defaul
6ef30 74 2c 0a 2a 2a 20 6f 62 74 61 69 6e 20 73 70 61 t,.** obtain spa
6ef40 63 65 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 ce from malloc()
6ef50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f ..**.** The allo
6ef60 63 61 28 29 20 72 6f 75 74 69 6e 65 20 6e 65 76 ca() routine nev
6ef70 65 72 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e er returns NULL.
6ef80 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73 This will caus
6ef90 65 20 63 6f 64 65 20 70 61 74 68 73 0a 2a 2a 20 e code paths.**
6efa0 74 68 61 74 20 64 65 61 6c 20 77 69 74 68 20 73 that deal with s
6efb0 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 qlite3StackAlloc
6efc0 28 29 20 66 61 69 6c 75 72 65 73 20 74 6f 20 62 () failures to b
6efd0 65 20 75 6e 72 65 61 63 68 61 62 6c 65 2e 0a 2a e unreachable..*
6efe0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
6eff0 55 53 45 5f 41 4c 4c 4f 43 41 0a 23 20 64 65 66 USE_ALLOCA.# def
6f000 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b ine sqlite3Stack
6f010 41 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 20 20 20 AllocRaw(D,N)
6f020 61 6c 6c 6f 63 61 28 4e 29 0a 23 20 64 65 66 69 alloca(N).# defi
6f030 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 ne sqlite3StackA
6f040 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 20 20 6d llocZero(D,N) m
6f050 65 6d 73 65 74 28 61 6c 6c 6f 63 61 28 4e 29 2c emset(alloca(N),
6f060 20 30 2c 20 4e 29 0a 23 20 64 65 66 69 6e 65 20 0, N).# define
6f070 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 sqlite3StackFree
6f080 28 44 2c 50 29 20 20 20 20 20 20 20 0a 23 65 6c (D,P) .#el
6f090 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 se.# define sqli
6f0a0 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77 te3StackAllocRaw
6f0b0 28 44 2c 4e 29 20 20 20 73 71 6c 69 74 65 33 44 (D,N) sqlite3D
6f0c0 62 4d 61 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 0a bMallocRaw(D,N).
6f0d0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
6f0e0 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 44 StackAllocZero(D
6f0f0 2c 4e 29 20 20 73 71 6c 69 74 65 33 44 62 4d 61 ,N) sqlite3DbMa
6f100 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 0a 23 20 llocZero(D,N).#
6f110 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 define sqlite3St
6f120 61 63 6b 46 72 65 65 28 44 2c 50 29 20 20 20 20 ackFree(D,P)
6f130 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
6f140 28 44 2c 50 29 0a 23 65 6e 64 69 66 0a 0a 23 69 (D,P).#endif..#i
6f150 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
6f160 4c 45 5f 4d 45 4d 53 59 53 33 0a 53 51 4c 49 54 LE_MEMSYS3.SQLIT
6f170 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
6f180 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
6f190 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 ods *sqlite3MemG
6f1a0 65 74 4d 65 6d 73 79 73 33 28 76 6f 69 64 29 3b etMemsys3(void);
6f1b0 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
6f1c0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
6f1d0 53 59 53 35 0a 53 51 4c 49 54 45 5f 50 52 49 56 SYS5.SQLITE_PRIV
6f1e0 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 ATE const sqlite
6f1f0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 3_mem_methods *s
6f200 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 qlite3MemGetMems
6f210 79 73 35 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 ys5(void);.#endi
6f220 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 f...#ifndef SQLI
6f230 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 53 51 TE_MUTEX_OMIT.SQ
6f240 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 LITE_PRIVATE s
6f250 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
6f260 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 hods *sqlite3Def
6f270 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 3b aultMutex(void);
6f280 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f290 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
6f2a0 2a 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c *sqlite3MutexAll
6f2b0 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f oc(int);.SQLITE_
6f2c0 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
6f2d0 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76 lite3MutexInit(v
6f2e0 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 oid);.SQLITE_PRI
6f2f0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
6f300 65 33 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 e3MutexEnd(void)
6f310 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 ;.#endif..SQLITE
6f320 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6f330 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 ite3StatusValue(
6f340 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6f350 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6f360 33 53 74 61 74 75 73 41 64 64 28 69 6e 74 2c 20 3StatusAdd(int,
6f370 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6f380 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6f390 33 53 74 61 74 75 73 53 65 74 28 69 6e 74 2c 20 3StatusSet(int,
6f3a0 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 int);..SQLITE_PR
6f3b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6f3c0 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65 29 3b 0a 3IsNaN(double);.
6f3d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f3e0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 58 50 72 void sqlite3VXPr
6f3f0 69 6e 74 66 28 53 74 72 41 63 63 75 6d 2a 2c 20 intf(StrAccum*,
6f400 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a int, const char*
6f410 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 , va_list);.SQLI
6f420 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 TE_PRIVATE char
6f430 2a 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 *sqlite3MPrintf(
6f440 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 sqlite3*,const c
6f450 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 har*, ...);.SQLI
6f460 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 TE_PRIVATE char
6f470 2a 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 *sqlite3VMPrintf
6f480 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 (sqlite3*,const
6f490 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b char*, va_list);
6f4a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f4b0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 char *sqlite3MAp
6f4c0 70 65 6e 64 66 28 73 71 6c 69 74 65 33 2a 2c 63 pendf(sqlite3*,c
6f4d0 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a har*,const char*
6f4e0 2c 2e 2e 2e 29 3b 0a 23 69 66 20 64 65 66 69 6e ,...);.#if defin
6f4f0 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 ed(SQLITE_TEST)
6f500 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 || defined(SQLIT
6f510 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 45 5f E_DEBUG).SQLITE_
6f520 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
6f530 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
6f540 66 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e f(const char*, .
6f550 2e 2e 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 ..);.#endif.#if
6f560 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 defined(SQLITE_T
6f570 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 EST).SQLITE_PRIV
6f580 41 54 45 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 ATE void *sqli
6f590 74 65 33 54 65 73 74 54 65 78 74 54 6f 50 74 72 te3TestTextToPtr
6f5a0 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 (const char*);.#
6f5b0 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 endif.SQLITE_PRI
6f5c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6f5d0 33 53 65 74 53 74 72 69 6e 67 28 63 68 61 72 20 3SetString(char
6f5e0 2a 2a 2c 20 73 71 6c 69 74 65 33 2a 2c 20 63 6f **, sqlite3*, co
6f5f0 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b nst char*, ...);
6f600 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f610 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f void sqlite3Erro
6f620 72 4d 73 67 28 50 61 72 73 65 2a 2c 20 63 6f 6e rMsg(Parse*, con
6f630 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a st char*, ...);.
6f640 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6f650 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 oid sqlite3Error
6f660 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b 0a 53 Clear(Parse*);.S
6f670 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6f680 74 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 t sqlite3Dequote
6f690 28 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f (char*);.SQLITE_
6f6a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6f6b0 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 63 te3KeywordCode(c
6f6c0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
6f6d0 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 ar*, int);.SQLIT
6f6e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6f6f0 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28 50 lite3RunParser(P
6f700 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 arse*, const cha
6f710 72 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 r*, char **);.SQ
6f720 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6f730 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43 d sqlite3FinishC
6f740 6f 64 69 6e 67 28 50 61 72 73 65 2a 29 3b 0a 53 oding(Parse*);.S
6f750 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6f760 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 t sqlite3GetTemp
6f770 52 65 67 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c Reg(Parse*);.SQL
6f780 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6f790 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 sqlite3ReleaseT
6f7a0 65 6d 70 52 65 67 28 50 61 72 73 65 2a 2c 69 6e empReg(Parse*,in
6f7b0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6f7c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 TE int sqlite3Ge
6f7d0 74 54 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 tTempRange(Parse
6f7e0 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 *,int);.SQLITE_P
6f7f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6f800 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 te3ReleaseTempRa
6f810 6e 67 65 28 50 61 72 73 65 2a 2c 69 6e 74 2c 69 nge(Parse*,int,i
6f820 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6f830 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
6f840 33 45 78 70 72 41 6c 6c 6f 63 28 73 71 6c 69 74 3ExprAlloc(sqlit
6f850 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 54 6f e3*,int,const To
6f860 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ken*,int);.SQLIT
6f870 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a E_PRIVATE Expr *
6f880 73 71 6c 69 74 65 33 45 78 70 72 28 73 71 6c 69 sqlite3Expr(sqli
6f890 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 te3*,int,const c
6f8a0 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
6f8b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6f8c0 65 33 45 78 70 72 41 74 74 61 63 68 53 75 62 74 e3ExprAttachSubt
6f8d0 72 65 65 73 28 73 71 6c 69 74 65 33 2a 2c 45 78 rees(sqlite3*,Ex
6f8e0 70 72 2a 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 pr*,Expr*,Expr*)
6f8f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6f900 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 50 45 Expr *sqlite3PE
6f910 78 70 72 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c xpr(Parse*, int,
6f920 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c 20 63 Expr*, Expr*, c
6f930 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 onst Token*);.SQ
6f940 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
6f950 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e r *sqlite3ExprAn
6f960 64 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a d(sqlite3*,Expr*
6f970 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
6f980 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 _PRIVATE Expr *s
6f990 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 qlite3ExprFuncti
6f9a0 6f 6e 28 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 on(Parse*,ExprLi
6f9b0 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 st*, Token*);.SQ
6f9c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6f9d0 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 73 73 d sqlite3ExprAss
6f9e0 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 50 61 72 ignVarNumber(Par
6f9f0 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c se*, Expr*);.SQL
6fa00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6fa10 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 sqlite3ExprClea
6fa20 72 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 r(sqlite3*, Expr
6fa30 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6fa40 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
6fa50 78 70 72 44 65 6c 65 74 65 28 73 71 6c 69 74 65 xprDelete(sqlite
6fa60 33 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 3*, Expr*);.SQLI
6fa70 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c TE_PRIVATE ExprL
6fa80 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 ist *sqlite3Expr
6fa90 4c 69 73 74 41 70 70 65 6e 64 28 50 61 72 73 65 ListAppend(Parse
6faa0 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 45 78 70 72 *,ExprList*,Expr
6fab0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6fac0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
6fad0 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 50 xprListSetName(P
6fae0 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c arse*,ExprList*,
6faf0 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c Token*,int);.SQL
6fb00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6fb10 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
6fb20 53 65 74 53 70 61 6e 28 50 61 72 73 65 2a 2c 45 SetSpan(Parse*,E
6fb30 78 70 72 4c 69 73 74 2a 2c 45 78 70 72 53 70 61 xprList*,ExprSpa
6fb40 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
6fb50 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6fb60 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73 ExprListDelete(s
6fb70 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73 qlite3*, ExprLis
6fb80 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
6fb90 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 ATE int sqlite3I
6fba0 6e 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 63 68 nit(sqlite3*, ch
6fbb0 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ar**);.SQLITE_PR
6fbc0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6fbd0 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 3InitCallback(vo
6fbe0 69 64 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a id*, int, char**
6fbf0 2c 20 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 , char**);.SQLIT
6fc00 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6fc10 71 6c 69 74 65 33 50 72 61 67 6d 61 28 50 61 72 qlite3Pragma(Par
6fc20 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e se*,Token*,Token
6fc30 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 *,Token*,int);.S
6fc40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6fc50 69 64 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 id sqlite3ResetI
6fc60 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 nternalSchema(sq
6fc70 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 lite3*, int);.SQ
6fc80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6fc90 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 d sqlite3BeginPa
6fca0 72 73 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b rse(Parse*,int);
6fcb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6fcc0 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d void sqlite3Comm
6fcd0 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 itInternalChange
6fce0 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c s(sqlite3*);.SQL
6fcf0 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c ITE_PRIVATE Tabl
6fd00 65 20 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74 e *sqlite3Result
6fd10 53 65 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73 SetOfSelect(Pars
6fd20 65 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c e*,Select*);.SQL
6fd30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6fd40 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 sqlite3OpenMast
6fd50 65 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 2c erTable(Parse *,
6fd60 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
6fd70 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6fd80 65 33 53 74 61 72 74 54 61 62 6c 65 28 50 61 72 e3StartTable(Par
6fd90 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e se*,Token*,Token
6fda0 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e *,int,int,int,in
6fdb0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6fdc0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
6fdd0 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c ddColumn(Parse*,
6fde0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
6fdf0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6fe00 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 ite3AddNotNull(P
6fe10 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c arse*, int);.SQL
6fe20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6fe30 20 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 sqlite3AddPrima
6fe40 72 79 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 ryKey(Parse*, Ex
6fe50 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e prList*, int, in
6fe60 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
6fe70 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6fe80 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 ite3AddCheckCons
6fe90 74 72 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 45 traint(Parse*, E
6fea0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
6feb0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6fec0 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 e3AddColumnType(
6fed0 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a Parse*,Token*);.
6fee0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6fef0 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 65 oid sqlite3AddDe
6ff00 66 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65 faultValue(Parse
6ff10 2a 2c 45 78 70 72 53 70 61 6e 2a 29 3b 0a 53 51 *,ExprSpan*);.SQ
6ff20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6ff30 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c d sqlite3AddColl
6ff40 61 74 65 54 79 70 65 28 50 61 72 73 65 2a 2c 20 ateType(Parse*,
6ff50 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
6ff60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6ff70 69 74 65 33 45 6e 64 54 61 62 6c 65 28 50 61 72 ite3EndTable(Par
6ff80 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e se*,Token*,Token
6ff90 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 0a 53 51 4c *,Select*);..SQL
6ffa0 49 54 45 5f 50 52 49 56 41 54 45 20 42 69 74 76 ITE_PRIVATE Bitv
6ffb0 65 63 20 2a 73 71 6c 69 74 65 33 42 69 74 76 65 ec *sqlite3Bitve
6ffc0 63 43 72 65 61 74 65 28 75 33 32 29 3b 0a 53 51 cCreate(u32);.SQ
6ffd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6ffe0 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 sqlite3BitvecTe
6fff0 73 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 st(Bitvec*, u32)
70000 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70010 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 int sqlite3Bitv
70020 65 63 53 65 74 28 42 69 74 76 65 63 2a 2c 20 75 ecSet(Bitvec*, u
70030 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 32);.SQLITE_PRIV
70040 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
70050 42 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 76 BitvecClear(Bitv
70060 65 63 2a 2c 20 75 33 32 2c 20 76 6f 69 64 2a 29 ec*, u32, void*)
70070 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70080 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 void sqlite3Bit
70090 76 65 63 44 65 73 74 72 6f 79 28 42 69 74 76 65 vecDestroy(Bitve
700a0 63 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 c*);.SQLITE_PRIV
700b0 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 42 ATE u32 sqlite3B
700c0 69 74 76 65 63 53 69 7a 65 28 42 69 74 76 65 63 itvecSize(Bitvec
700d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
700e0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 TE int sqlite3Bi
700f0 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74 28 tvecBuiltinTest(
70100 69 6e 74 2c 69 6e 74 2a 29 3b 0a 0a 53 51 4c 49 int,int*);..SQLI
70110 54 45 5f 50 52 49 56 41 54 45 20 52 6f 77 53 65 TE_PRIVATE RowSe
70120 74 20 2a 73 71 6c 69 74 65 33 52 6f 77 53 65 74 t *sqlite3RowSet
70130 49 6e 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 76 Init(sqlite3*, v
70140 6f 69 64 2a 2c 20 75 6e 73 69 67 6e 65 64 20 69 oid*, unsigned i
70150 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
70160 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
70170 52 6f 77 53 65 74 43 6c 65 61 72 28 52 6f 77 53 RowSetClear(RowS
70180 65 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 et*);.SQLITE_PRI
70190 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
701a0 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 52 6f 3RowSetInsert(Ro
701b0 77 53 65 74 2a 2c 20 69 36 34 29 3b 0a 53 51 4c wSet*, i64);.SQL
701c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
701d0 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73 sqlite3RowSetTes
701e0 74 28 52 6f 77 53 65 74 2a 2c 20 75 38 20 69 42 t(RowSet*, u8 iB
701f0 61 74 63 68 2c 20 69 36 34 29 3b 0a 53 51 4c 49 atch, i64);.SQLI
70200 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
70210 71 6c 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74 qlite3RowSetNext
70220 28 52 6f 77 53 65 74 2a 2c 20 69 36 34 2a 29 3b (RowSet*, i64*);
70230 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
70240 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 void sqlite3Cre
70250 61 74 65 56 69 65 77 28 50 61 72 73 65 2a 2c 54 ateView(Parse*,T
70260 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b oken*,Token*,Tok
70270 65 6e 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e 74 2c en*,Select*,int,
70280 69 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 int);..#if !defi
70290 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
702a0 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 VIEW) || !define
702b0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 d(SQLITE_OMIT_VI
702c0 52 54 55 41 4c 54 41 42 4c 45 29 0a 53 51 4c 49 RTUALTABLE).SQLI
702d0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
702e0 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 sqlite3ViewGetC
702f0 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65 olumnNames(Parse
70300 2a 2c 54 61 62 6c 65 2a 29 3b 0a 23 65 6c 73 65 *,Table*);.#else
70310 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
70320 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 3ViewGetColumnNa
70330 6d 65 73 28 41 2c 42 29 20 30 0a 23 65 6e 64 69 mes(A,B) 0.#endi
70340 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 f..SQLITE_PRIVAT
70350 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 E void sqlite3Dr
70360 6f 70 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 opTable(Parse*,
70370 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 SrcList*, int, i
70380 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
70390 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
703a0 44 65 6c 65 74 65 54 61 62 6c 65 28 54 61 62 6c DeleteTable(Tabl
703b0 65 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c e*);.#ifndef SQL
703c0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 ITE_OMIT_AUTOINC
703d0 52 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 REMENT.SQLITE_PR
703e0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
703f0 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e ite3Autoincremen
70400 74 42 65 67 69 6e 28 50 61 72 73 65 20 2a 70 50 tBegin(Parse *pP
70410 61 72 73 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 arse);.SQLITE_PR
70420 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
70430 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e ite3Autoincremen
70440 74 45 6e 64 28 50 61 72 73 65 20 2a 70 50 61 72 tEnd(Parse *pPar
70450 73 65 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 se);.#else.# def
70460 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 6f 69 ine sqlite3Autoi
70470 6e 63 72 65 6d 65 6e 74 42 65 67 69 6e 28 58 29 ncrementBegin(X)
70480 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
70490 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 3AutoincrementEn
704a0 64 28 58 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 d(X).#endif.SQLI
704b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
704c0 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 50 61 sqlite3Insert(Pa
704d0 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 rse*, SrcList*,
704e0 45 78 70 72 4c 69 73 74 2a 2c 20 53 65 6c 65 63 ExprList*, Selec
704f0 74 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 69 6e 74 t*, IdList*, int
70500 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
70510 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 E void *sqlite3A
70520 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 73 71 6c rrayAllocate(sql
70530 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c ite3*,void*,int,
70540 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e int,int*,int*,in
70550 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
70560 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 ATE IdList *sqli
70570 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28 te3IdListAppend(
70580 73 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 sqlite3*, IdList
70590 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
705a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
705b0 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65 qlite3IdListInde
705c0 78 28 49 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20 x(IdList*,const
705d0 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 char*);.SQLITE_P
705e0 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a RIVATE SrcList *
705f0 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e sqlite3SrcListEn
70600 6c 61 72 67 65 28 73 71 6c 69 74 65 33 2a 2c 20 large(sqlite3*,
70610 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 SrcList*, int, i
70620 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
70630 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c ATE SrcList *sql
70640 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e ite3SrcListAppen
70650 64 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c d(sqlite3*, SrcL
70660 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f ist*, Token*, To
70670 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
70680 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 IVATE SrcList *s
70690 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 qlite3SrcListApp
706a0 65 6e 64 46 72 6f 6d 54 65 72 6d 28 50 61 72 73 endFromTerm(Pars
706b0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f e*, SrcList*, To
706c0 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 20 ken*, Token*,.
706d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
706e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
706f0 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c 65 Token*, Sele
70700 63 74 2a 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69 ct*, Expr*, IdLi
70710 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
70720 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70730 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 3SrcListIndexedB
70740 79 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 y(Parse *, SrcLi
70750 73 74 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a st *, Token *);.
70760 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
70770 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78 65 nt sqlite3Indexe
70780 64 42 79 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 dByLookup(Parse
70790 2a 2c 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 *, struct SrcLis
707a0 74 5f 69 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54 t_item *);.SQLIT
707b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
707c0 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 qlite3SrcListShi
707d0 66 74 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 ftJoinType(SrcLi
707e0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
707f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70800 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 3SrcListAssignCu
70810 72 73 6f 72 73 28 50 61 72 73 65 2a 2c 20 53 72 rsors(Parse*, Sr
70820 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f cList*);.SQLITE_
70830 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
70840 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 ite3IdListDelete
70850 28 73 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 (sqlite3*, IdLis
70860 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
70870 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
70880 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 73 71 SrcListDelete(sq
70890 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a lite3*, SrcList*
708a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
708b0 45 20 49 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 E Index *sqlite3
708c0 43 72 65 61 74 65 49 6e 64 65 78 28 50 61 72 73 CreateIndex(Pars
708d0 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a e*,Token*,Token*
708e0 2c 53 72 63 4c 69 73 74 2a 2c 45 78 70 72 4c 69 ,SrcList*,ExprLi
708f0 73 74 2a 2c 69 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a st*,int,Token*,.
70900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70910 20 20 20 20 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 Token*,
70920 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
70930 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
70940 71 6c 69 74 65 33 44 72 6f 70 49 6e 64 65 78 28 qlite3DropIndex(
70950 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a Parse*, SrcList*
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 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
70980 65 33 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c e3Select(Parse*,
70990 20 53 65 6c 65 63 74 2a 2c 20 53 65 6c 65 63 74 Select*, Select
709a0 44 65 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Dest*);.SQLITE_P
709b0 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 RIVATE Select *s
709c0 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 qlite3SelectNew(
709d0 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a Parse*,ExprList*
709e0 2c 53 72 63 4c 69 73 74 2a 2c 45 78 70 72 2a 2c ,SrcList*,Expr*,
709f0 45 78 70 72 4c 69 73 74 2a 2c 0a 20 20 20 20 20 ExprList*,.
70a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70a10 20 20 20 20 45 78 70 72 2a 2c 45 78 70 72 4c 69 Expr*,ExprLi
70a20 73 74 2a 2c 69 6e 74 2c 45 78 70 72 2a 2c 45 78 st*,int,Expr*,Ex
70a30 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
70a40 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70a50 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 73 71 3SelectDelete(sq
70a60 6c 69 74 65 33 2a 2c 20 53 65 6c 65 63 74 2a 29 lite3*, Select*)
70a70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70a80 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 53 Table *sqlite3S
70a90 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72 rcListLookup(Par
70aa0 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a se*, SrcList*);.
70ab0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
70ac0 6e 74 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 nt sqlite3IsRead
70ad0 4f 6e 6c 79 28 50 61 72 73 65 2a 2c 20 54 61 62 Only(Parse*, Tab
70ae0 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 le*, int);.SQLIT
70af0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
70b00 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 qlite3OpenTable(
70b10 50 61 72 73 65 2a 2c 20 69 6e 74 20 69 43 75 72 Parse*, int iCur
70b20 2c 20 69 6e 74 20 69 44 62 2c 20 54 61 62 6c 65 , int iDb, Table
70b30 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 20 64 65 66 *, int);.#if def
70b40 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 ined(SQLITE_ENAB
70b50 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 LE_UPDATE_DELETE
70b60 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 _LIMIT) && !defi
70b70 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
70b80 53 55 42 51 55 45 52 59 29 0a 53 51 4c 49 54 45 SUBQUERY).SQLITE
70b90 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 _PRIVATE Expr *s
70ba0 71 6c 69 74 65 33 4c 69 6d 69 74 57 68 65 72 65 qlite3LimitWhere
70bb0 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 (Parse *, SrcLis
70bc0 74 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 t *, Expr *, Exp
70bd0 72 4c 69 73 74 20 2a 2c 20 45 78 70 72 20 2a 2c rList *, Expr *,
70be0 20 45 78 70 72 20 2a 2c 20 63 68 61 72 20 2a 29 Expr *, char *)
70bf0 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f ;.#endif.SQLITE_
70c00 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
70c10 69 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 50 ite3DeleteFrom(P
70c20 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c arse*, SrcList*,
70c30 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
70c40 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
70c50 69 74 65 33 55 70 64 61 74 65 28 50 61 72 73 65 ite3Update(Parse
70c60 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 *, SrcList*, Exp
70c70 72 4c 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 69 rList*, Expr*, i
70c80 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
70c90 41 54 45 20 57 68 65 72 65 49 6e 66 6f 20 2a 73 ATE WhereInfo *s
70ca0 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e qlite3WhereBegin
70cb0 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 (Parse*, SrcList
70cc0 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 4c 69 *, Expr*, ExprLi
70cd0 73 74 2a 2a 2c 20 75 31 36 29 3b 0a 53 51 4c 49 st**, u16);.SQLI
70ce0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
70cf0 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 sqlite3WhereEnd(
70d00 57 68 65 72 65 49 6e 66 6f 2a 29 3b 0a 53 51 4c WhereInfo*);.SQL
70d10 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
70d20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 sqlite3ExprCodeG
70d30 65 74 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c etColumn(Parse*,
70d40 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e Table*, int, in
70d50 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 t, int, int);.SQ
70d60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
70d70 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 d sqlite3ExprCod
70d80 65 4d 6f 76 65 28 50 61 72 73 65 2a 2c 20 69 6e eMove(Parse*, in
70d90 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 t, int, int);.SQ
70da0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
70db0 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 d sqlite3ExprCod
70dc0 65 43 6f 70 79 28 50 61 72 73 65 2a 2c 20 69 6e eCopy(Parse*, in
70dd0 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 t, int, int);.SQ
70de0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
70df0 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 d sqlite3ExprCac
70e00 68 65 53 74 6f 72 65 28 50 61 72 73 65 2a 2c 20 heStore(Parse*,
70e10 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a int, int, int);.
70e20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70e30 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
70e40 61 63 68 65 50 75 73 68 28 50 61 72 73 65 2a 29 achePush(Parse*)
70e50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70e60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
70e70 72 43 61 63 68 65 50 6f 70 28 50 61 72 73 65 2a rCachePop(Parse*
70e80 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
70e90 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
70ea0 74 65 33 45 78 70 72 43 61 63 68 65 52 65 6d 6f te3ExprCacheRemo
70eb0 76 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b ve(Parse*, int);
70ec0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70ed0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
70ee0 43 61 63 68 65 43 6c 65 61 72 28 50 61 72 73 65 CacheClear(Parse
70ef0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
70f00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
70f10 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 xprCacheAffinity
70f20 43 68 61 6e 67 65 28 50 61 72 73 65 2a 2c 20 69 Change(Parse*, i
70f30 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
70f40 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
70f50 6c 69 74 65 33 45 78 70 72 48 61 72 64 43 6f 70 lite3ExprHardCop
70f60 79 28 50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 y(Parse*,int,int
70f70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
70f80 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
70f90 72 43 6f 64 65 28 50 61 72 73 65 2a 2c 20 45 78 rCode(Parse*, Ex
70fa0 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 pr*, int);.SQLIT
70fb0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
70fc0 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d lite3ExprCodeTem
70fd0 70 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c p(Parse*, Expr*,
70fe0 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 int*);.SQLITE_P
70ff0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
71000 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 e3ExprCodeTarget
71010 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 (Parse*, Expr*,
71020 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
71030 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
71040 45 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65 ExprCodeAndCache
71050 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 (Parse*, Expr*,
71060 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
71070 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
71080 33 45 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 3ExprCodeConstan
71090 74 73 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a ts(Parse*, Expr*
710a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
710b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
710c0 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 50 61 rCodeExprList(Pa
710d0 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c rse*, ExprList*,
710e0 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
710f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
71100 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 sqlite3ExprIfTru
71110 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c e(Parse*, Expr*,
71120 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
71130 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
71140 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c sqlite3ExprIfFal
71150 73 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a se(Parse*, Expr*
71160 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
71170 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c ITE_PRIVATE Tabl
71180 65 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 54 61 e *sqlite3FindTa
71190 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e ble(sqlite3*,con
711a0 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 st char*, const
711b0 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 char*);.SQLITE_P
711c0 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 RIVATE Table *sq
711d0 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 lite3LocateTable
711e0 28 50 61 72 73 65 2a 2c 69 6e 74 20 69 73 56 69 (Parse*,int isVi
711f0 65 77 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 ew,const char*,
71200 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 const char*);.SQ
71210 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64 LITE_PRIVATE Ind
71220 65 78 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 49 ex *sqlite3FindI
71230 6e 64 65 78 28 73 71 6c 69 74 65 33 2a 2c 63 6f ndex(sqlite3*,co
71240 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 nst char*, const
71250 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f char*);.SQLITE_
71260 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
71270 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c ite3UnlinkAndDel
71280 65 74 65 54 61 62 6c 65 28 73 71 6c 69 74 65 33 eteTable(sqlite3
71290 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 *,int,const char
712a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
712b0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 TE void sqlite3U
712c0 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e nlinkAndDeleteIn
712d0 64 65 78 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 dex(sqlite3*,int
712e0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 ,const char*);.S
712f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
71300 69 64 20 73 71 6c 69 74 65 33 56 61 63 75 75 6d id sqlite3Vacuum
71310 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 (Parse*);.SQLITE
71320 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
71330 69 74 65 33 52 75 6e 56 61 63 75 75 6d 28 63 68 ite3RunVacuum(ch
71340 61 72 2a 2a 2c 20 73 71 6c 69 74 65 33 2a 29 3b ar**, sqlite3*);
71350 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71360 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4e 61 6d char *sqlite3Nam
71370 65 46 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 eFromToken(sqlit
71380 65 33 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 e3*, Token*);.SQ
71390 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
713a0 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 sqlite3ExprComp
713b0 61 72 65 28 45 78 70 72 2a 2c 20 45 78 70 72 2a are(Expr*, Expr*
713c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
713d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
713e0 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 prAnalyzeAggrega
713f0 74 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a tes(NameContext*
71400 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
71410 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
71420 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 lite3ExprAnalyze
71430 41 67 67 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 AggList(NameCont
71440 65 78 74 2a 2c 45 78 70 72 4c 69 73 74 2a 29 3b ext*,ExprList*);
71450 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71460 56 64 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74 Vdbe *sqlite3Get
71470 56 64 62 65 28 50 61 72 73 65 2a 29 3b 0a 53 51 Vdbe(Parse*);.SQ
71480 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
71490 64 20 73 71 6c 69 74 65 33 50 72 6e 67 53 61 76 d sqlite3PrngSav
714a0 65 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 eState(void);.SQ
714b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
714c0 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 d sqlite3PrngRes
714d0 74 6f 72 65 53 74 61 74 65 28 76 6f 69 64 29 3b toreState(void);
714e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
714f0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 void sqlite3Prng
71500 52 65 73 65 74 53 74 61 74 65 28 76 6f 69 64 29 ResetState(void)
71510 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
71520 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6c void sqlite3Rol
71530 6c 62 61 63 6b 41 6c 6c 28 73 71 6c 69 74 65 33 lbackAll(sqlite3
71540 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
71550 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 TE void sqlite3C
71560 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 odeVerifySchema(
71570 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 Parse*, int);.SQ
71580 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
71590 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 d sqlite3BeginTr
715a0 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a ansaction(Parse*
715b0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
715c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
715d0 74 65 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 te3CommitTransac
715e0 74 69 6f 6e 28 50 61 72 73 65 2a 29 3b 0a 53 51 tion(Parse*);.SQ
715f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
71600 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 d sqlite3Rollbac
71610 6b 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 kTransaction(Par
71620 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 se*);.SQLITE_PRI
71630 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
71640 33 53 61 76 65 70 6f 69 6e 74 28 50 61 72 73 65 3Savepoint(Parse
71650 2a 2c 20 69 6e 74 2c 20 54 6f 6b 65 6e 2a 29 3b *, int, Token*);
71660 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71670 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 void sqlite3Clos
71680 65 53 61 76 65 70 6f 69 6e 74 73 28 73 71 6c 69 eSavepoints(sqli
71690 74 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 te3 *);.SQLITE_P
716a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
716b0 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 e3ExprIsConstant
716c0 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f (Expr*);.SQLITE_
716d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
716e0 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e te3ExprIsConstan
716f0 74 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 2a 29 3b tNotJoin(Expr*);
71700 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71710 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 int sqlite3ExprI
71720 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 sConstantOrFunct
71730 69 6f 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 ion(Expr*);.SQLI
71740 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
71750 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 qlite3ExprIsInte
71760 67 65 72 28 45 78 70 72 2a 2c 20 69 6e 74 2a 29 ger(Expr*, int*)
71770 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
71780 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 52 6f int sqlite3IsRo
71790 77 69 64 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 wid(const char*)
717a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
717b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e void sqlite3Gen
717c0 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28 50 erateRowDelete(P
717d0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 arse*, Table*, i
717e0 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 54 72 nt, int, int, Tr
717f0 69 67 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 igger *, int);.S
71800 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
71810 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 id sqlite3Genera
71820 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 teRowIndexDelete
71830 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c (Parse*, Table*,
71840 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c int, int*);.SQL
71850 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
71860 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49 sqlite3GenerateI
71870 6e 64 65 78 4b 65 79 28 50 61 72 73 65 2a 2c 20 ndexKey(Parse*,
71880 49 6e 64 65 78 2a 2c 20 69 6e 74 2c 20 69 6e 74 Index*, int, int
71890 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
718a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
718b0 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 te3GenerateConst
718c0 72 61 69 6e 74 43 68 65 63 6b 73 28 50 61 72 73 raintChecks(Pars
718d0 65 2a 2c 54 61 62 6c 65 2a 2c 69 6e 74 2c 69 6e e*,Table*,int,in
718e0 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 t,.
718f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71900 20 20 20 20 20 20 20 20 69 6e 74 2a 2c 69 6e 74 int*,int
71910 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 ,int,int,int,int
71920 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
71930 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 TE void sqlite3C
71940 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e ompleteInsertion
71950 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c (Parse*, Table*,
71960 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c int, int, int*,
71970 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b int, int, int);
71980 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71990 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 int sqlite3OpenT
719a0 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 50 ableAndIndices(P
719b0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 arse*, Table*, i
719c0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
719d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
719e0 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f lite3BeginWriteO
719f0 70 65 72 61 74 69 6f 6e 28 50 61 72 73 65 2a 2c peration(Parse*,
71a00 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
71a10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
71a20 73 71 6c 69 74 65 33 4d 75 6c 74 69 57 72 69 74 sqlite3MultiWrit
71a30 65 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 e(Parse*);.SQLIT
71a40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
71a50 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 50 qlite3MayAbort(P
71a60 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 arse*);.SQLITE_P
71a70 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
71a80 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e te3HaltConstrain
71a90 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 63 t(Parse*, int, c
71aa0 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 har*, int);.SQLI
71ab0 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
71ac0 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 *sqlite3ExprDup(
71ad0 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69 sqlite3*,Expr*,i
71ae0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
71af0 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71 ATE ExprList *sq
71b00 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 lite3ExprListDup
71b10 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 4c 69 (sqlite3*,ExprLi
71b20 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 st*,int);.SQLITE
71b30 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 _PRIVATE SrcList
71b40 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 *sqlite3SrcList
71b50 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 72 63 Dup(sqlite3*,Src
71b60 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 List*,int);.SQLI
71b70 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 TE_PRIVATE IdLis
71b80 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 t *sqlite3IdList
71b90 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 49 64 4c Dup(sqlite3*,IdL
71ba0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ist*);.SQLITE_PR
71bb0 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 IVATE Select *sq
71bc0 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 73 lite3SelectDup(s
71bd0 71 6c 69 74 65 33 2a 2c 53 65 6c 65 63 74 2a 2c qlite3*,Select*,
71be0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
71bf0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
71c00 33 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28 46 3FuncDefInsert(F
71c10 75 6e 63 44 65 66 48 61 73 68 2a 2c 20 46 75 6e uncDefHash*, Fun
71c20 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 cDef*);.SQLITE_P
71c30 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a RIVATE FuncDef *
71c40 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 sqlite3FindFunct
71c50 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e ion(sqlite3*,con
71c60 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69 6e 74 st char*,int,int
71c70 2c 75 38 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 ,u8,int);.SQLITE
71c80 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
71c90 6c 69 74 65 33 52 65 67 69 73 74 65 72 42 75 69 lite3RegisterBui
71ca0 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28 73 71 ltinFunctions(sq
71cb0 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f lite3*);.SQLITE_
71cc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
71cd0 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74 65 ite3RegisterDate
71ce0 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 76 6f TimeFunctions(vo
71cf0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 id);.SQLITE_PRIV
71d00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
71d10 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75 RegisterGlobalFu
71d20 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 3b 0a 23 nctions(void);.#
71d30 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
71d40 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
71d50 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53 E int sqlite3S
71d60 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 65 33 2a afetyOn(sqlite3*
71d70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71d80 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53 E int sqlite3S
71d90 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 65 33 afetyOff(sqlite3
71da0 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 *);.#else.# defi
71db0 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 ne sqlite3Safety
71dc0 4f 6e 28 41 29 20 30 0a 23 20 64 65 66 69 6e 65 On(A) 0.# define
71dd0 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
71de0 66 28 41 29 20 30 0a 23 65 6e 64 69 66 0a 53 51 f(A) 0.#endif.SQ
71df0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
71e00 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 sqlite3SafetyCh
71e10 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b eckOk(sqlite3*);
71e20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71e30 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 int sqlite3Safet
71e40 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73 yCheckSickOrOk(s
71e50 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 qlite3*);.SQLITE
71e60 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
71e70 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 lite3ChangeCooki
71e80 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a e(Parse*, int);.
71e90 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
71ea0 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 LITE_OMIT_VIEW)
71eb0 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 && !defined(SQLI
71ec0 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 TE_OMIT_TRIGGER)
71ed0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71ee0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 74 65 void sqlite3Mate
71ef0 72 69 61 6c 69 7a 65 56 69 65 77 28 50 61 72 73 rializeView(Pars
71f00 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 70 72 e*, Table*, Expr
71f10 2a 2c 20 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a *, int);.#endif.
71f20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
71f30 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53 51 4c OMIT_TRIGGER.SQL
71f40 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
71f50 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 id sqlite3BeginT
71f60 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54 rigger(Parse*, T
71f70 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 oken*,Token*,int
71f80 2c 69 6e 74 2c 49 64 4c 69 73 74 2a 2c 53 72 63 ,int,IdList*,Src
71f90 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 List*,.
71fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71fb0 20 20 45 78 70 72 2a 2c 69 6e 74 2c 20 69 6e 74 Expr*,int, int
71fc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71fd0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
71fe0 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 50 61 FinishTrigger(Pa
71ff0 72 73 65 2a 2c 20 54 72 69 67 67 65 72 53 74 65 rse*, TriggerSte
72000 70 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c p*, Token*);.SQL
72010 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
72020 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 id sqlite3DropTr
72030 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 53 72 igger(Parse*, Sr
72040 63 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 cList*, int);.SQ
72050 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
72060 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 oid sqlite3DropT
72070 72 69 67 67 65 72 50 74 72 28 50 61 72 73 65 2a riggerPtr(Parse*
72080 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c , Trigger*);.SQL
72090 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 ITE_PRIVATE Tr
720a0 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33 54 72 igger *sqlite3Tr
720b0 69 67 67 65 72 73 45 78 69 73 74 28 50 61 72 73 iggersExist(Pars
720c0 65 20 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 e *, Table*, int
720d0 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 , ExprList*, int
720e0 20 2a 70 4d 61 73 6b 29 3b 0a 53 51 4c 49 54 45 *pMask);.SQLITE
720f0 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 _PRIVATE Trigg
72100 65 72 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 er *sqlite3Trigg
72110 65 72 4c 69 73 74 28 50 61 72 73 65 20 2a 2c 20 erList(Parse *,
72120 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 Table *);.SQLITE
72130 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
72140 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 sqlite3CodeRowTr
72150 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54 72 igger(Parse*, Tr
72160 69 67 67 65 72 20 2a 2c 20 69 6e 74 2c 20 45 78 igger *, int, Ex
72170 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 54 61 prList*, int, Ta
72180 62 6c 65 20 2a 2c 0a 20 20 20 20 20 20 20 20 20 ble *,.
72190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
721a0 20 20 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 int, int, int
721b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
721c0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
721d0 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 44 69 CodeRowTriggerDi
721e0 72 65 63 74 28 50 61 72 73 65 20 2a 2c 20 54 72 rect(Parse *, Tr
721f0 69 67 67 65 72 20 2a 2c 20 54 61 62 6c 65 20 2a igger *, Table *
72200 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 , int, int, int)
72210 3b 0a 20 20 76 6f 69 64 20 73 71 6c 69 74 65 56 ;. void sqliteV
72220 69 65 77 54 72 69 67 67 65 72 73 28 50 61 72 73 iewTriggers(Pars
72230 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 70 72 e*, Table*, Expr
72240 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 *, int, ExprList
72250 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
72260 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
72270 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 53 74 3DeleteTriggerSt
72280 65 70 28 73 71 6c 69 74 65 33 2a 2c 20 54 72 69 ep(sqlite3*, Tri
72290 67 67 65 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 ggerStep*);.SQLI
722a0 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 TE_PRIVATE Tri
722b0 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 ggerStep *sqlite
722c0 33 54 72 69 67 67 65 72 53 65 6c 65 63 74 53 74 3TriggerSelectSt
722d0 65 70 28 73 71 6c 69 74 65 33 2a 2c 53 65 6c 65 ep(sqlite3*,Sele
722e0 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ct*);.SQLITE_PRI
722f0 56 41 54 45 20 20 20 54 72 69 67 67 65 72 53 74 VATE TriggerSt
72300 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 ep *sqlite3Trigg
72310 65 72 49 6e 73 65 72 74 53 74 65 70 28 73 71 6c erInsertStep(sql
72320 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 49 64 ite3*,Token*, Id
72330 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 List*,.
72340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45 E
72360 78 70 72 4c 69 73 74 2a 2c 53 65 6c 65 63 74 2a xprList*,Select*
72370 2c 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ,u8);.SQLITE_PRI
72380 56 41 54 45 20 20 20 54 72 69 67 67 65 72 53 74 VATE TriggerSt
72390 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 ep *sqlite3Trigg
723a0 65 72 55 70 64 61 74 65 53 74 65 70 28 73 71 6c erUpdateStep(sql
723b0 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 45 78 70 ite3*,Token*,Exp
723c0 72 4c 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 75 rList*, Expr*, u
723d0 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 8);.SQLITE_PRIVA
723e0 54 45 20 20 20 54 72 69 67 67 65 72 53 74 65 70 TE TriggerStep
723f0 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 *sqlite3Trigger
72400 44 65 6c 65 74 65 53 74 65 70 28 73 71 6c 69 74 DeleteStep(sqlit
72410 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 e3*,Token*, Expr
72420 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
72430 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
72440 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 3DeleteTrigger(s
72450 71 6c 69 74 65 33 2a 2c 20 54 72 69 67 67 65 72 qlite3*, Trigger
72460 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
72470 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
72480 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 3UnlinkAndDelete
72490 54 72 69 67 67 65 72 28 73 71 6c 69 74 65 33 2a Trigger(sqlite3*
724a0 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a ,int,const char*
724b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
724c0 45 20 20 20 75 33 32 20 73 71 6c 69 74 65 33 54 E u32 sqlite3T
724d0 72 69 67 67 65 72 4f 6c 64 6d 61 73 6b 28 50 61 riggerOldmask(Pa
724e0 72 73 65 2a 2c 54 72 69 67 67 65 72 2a 2c 45 78 rse*,Trigger*,Ex
724f0 70 72 4c 69 73 74 2a 2c 54 61 62 6c 65 2a 2c 69 prList*,Table*,i
72500 6e 74 29 3b 0a 23 20 64 65 66 69 6e 65 20 73 71 nt);.# define sq
72510 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 lite3ParseToplev
72520 65 6c 28 70 29 20 28 28 70 29 2d 3e 70 54 6f 70 el(p) ((p)->pTop
72530 6c 65 76 65 6c 20 3f 20 28 70 29 2d 3e 70 54 6f level ? (p)->pTo
72540 70 6c 65 76 65 6c 20 3a 20 28 70 29 29 0a 23 65 plevel : (p)).#e
72550 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c lse.# define sql
72560 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73 ite3TriggersExis
72570 74 28 42 2c 43 2c 44 2c 45 2c 46 29 20 30 0a 23 t(B,C,D,E,F) 0.#
72580 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 define sqlite3D
72590 65 6c 65 74 65 54 72 69 67 67 65 72 28 41 2c 42 eleteTrigger(A,B
725a0 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
725b0 65 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 e3DropTriggerPtr
725c0 28 41 2c 42 29 0a 23 20 64 65 66 69 6e 65 20 73 (A,B).# define s
725d0 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 qlite3UnlinkAndD
725e0 65 6c 65 74 65 54 72 69 67 67 65 72 28 41 2c 42 eleteTrigger(A,B
725f0 2c 43 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c ,C).# define sql
72600 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 ite3CodeRowTrigg
72610 65 72 28 41 2c 42 2c 43 2c 44 2c 45 2c 46 2c 47 er(A,B,C,D,E,F,G
72620 2c 48 2c 49 29 0a 23 20 64 65 66 69 6e 65 20 73 ,H,I).# define s
72630 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 qlite3CodeRowTri
72640 67 67 65 72 44 69 72 65 63 74 28 41 2c 42 2c 43 ggerDirect(A,B,C
72650 2c 44 2c 45 2c 46 29 0a 23 20 64 65 66 69 6e 65 ,D,E,F).# define
72660 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c sqlite3TriggerL
72670 69 73 74 28 58 2c 20 59 29 20 30 0a 23 20 64 65 ist(X, Y) 0.# de
72680 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73 fine sqlite3Pars
72690 65 54 6f 70 6c 65 76 65 6c 28 70 29 20 70 0a 23 eToplevel(p) p.#
726a0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 define sqlite3T
726b0 72 69 67 67 65 72 4f 6c 64 6d 61 73 6b 28 41 2c riggerOldmask(A,
726c0 42 2c 43 2c 44 2c 45 29 20 30 0a 23 65 6e 64 69 B,C,D,E) 0.#endi
726d0 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 f..SQLITE_PRIVAT
726e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 69 E int sqlite3Joi
726f0 6e 54 79 70 65 28 50 61 72 73 65 2a 2c 20 54 6f nType(Parse*, To
72700 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f ken*, Token*, To
72710 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
72720 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
72730 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b e3CreateForeignK
72740 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c ey(Parse*, ExprL
72750 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 45 78 ist*, Token*, Ex
72760 70 72 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 prList*, int);.S
72770 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
72780 69 64 20 73 71 6c 69 74 65 33 44 65 66 65 72 46 id sqlite3DeferF
72790 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 73 65 2a oreignKey(Parse*
727a0 2c 20 69 6e 74 29 3b 0a 23 69 66 6e 64 65 66 20 , int);.#ifndef
727b0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 SQLITE_OMIT_AUTH
727c0 4f 52 49 5a 41 54 49 4f 4e 0a 53 51 4c 49 54 45 ORIZATION.SQLITE
727d0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
727e0 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 28 sqlite3AuthRead(
727f0 50 61 72 73 65 2a 2c 45 78 70 72 2a 2c 53 63 68 Parse*,Expr*,Sch
72800 65 6d 61 2a 2c 53 72 63 4c 69 73 74 2a 29 3b 0a ema*,SrcList*);.
72810 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
72820 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74 68 int sqlite3Auth
72830 43 68 65 63 6b 28 50 61 72 73 65 2a 2c 69 6e 74 Check(Parse*,int
72840 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 , const char*, c
72850 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 onst char*, cons
72860 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 t char*);.SQLITE
72870 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
72880 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 sqlite3AuthConte
72890 78 74 50 75 73 68 28 50 61 72 73 65 2a 2c 20 41 xtPush(Parse*, A
728a0 75 74 68 43 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e uthContext*, con
728b0 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 st char*);.SQLIT
728c0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
728d0 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 sqlite3AuthCont
728e0 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e 74 65 extPop(AuthConte
728f0 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 xt*);.SQLITE_PRI
72900 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
72910 65 33 41 75 74 68 52 65 61 64 43 6f 6c 28 50 61 e3AuthReadCol(Pa
72920 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 rse*, const char
72930 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *, const char *
72940 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20 , int);.#else.#
72950 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 define sqlite3Au
72960 74 68 52 65 61 64 28 61 2c 62 2c 63 2c 64 29 0a thRead(a,b,c,d).
72970 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
72980 41 75 74 68 43 68 65 63 6b 28 61 2c 62 2c 63 2c AuthCheck(a,b,c,
72990 64 2c 65 29 20 20 20 20 53 51 4c 49 54 45 5f 4f d,e) SQLITE_O
729a0 4b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 K.# define sqlit
729b0 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 e3AuthContextPus
729c0 68 28 61 2c 62 2c 63 29 0a 23 20 64 65 66 69 6e h(a,b,c).# defin
729d0 65 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e e sqlite3AuthCon
729e0 74 65 78 74 50 6f 70 28 61 29 20 20 28 28 76 6f textPop(a) ((vo
729f0 69 64 29 28 61 29 29 0a 23 65 6e 64 69 66 0a 53 id)(a)).#endif.S
72a00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
72a10 69 64 20 73 71 6c 69 74 65 33 41 74 74 61 63 68 id sqlite3Attach
72a20 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 (Parse*, Expr*,
72a30 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 Expr*, Expr*);.S
72a40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
72a50 69 64 20 73 71 6c 69 74 65 33 44 65 74 61 63 68 id sqlite3Detach
72a60 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b (Parse*, Expr*);
72a70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72a80 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
72a90 46 61 63 74 6f 72 79 28 63 6f 6e 73 74 20 73 71 Factory(const sq
72aa0 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 lite3 *db, const
72ab0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
72ac0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
72ad0 20 20 20 20 20 20 20 20 20 69 6e 74 20 6f 6d 69 int omi
72ae0 74 4a 6f 75 72 6e 61 6c 2c 20 69 6e 74 20 6e 43 tJournal, int nC
72af0 61 63 68 65 2c 20 69 6e 74 20 66 6c 61 67 73 2c ache, int flags,
72b00 20 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65 65 Btree **ppBtree
72b10 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
72b20 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 E int sqlite3Fix
72b30 49 6e 69 74 28 44 62 46 69 78 65 72 2a 2c 20 50 Init(DbFixer*, P
72b40 61 72 73 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 arse*, int, cons
72b50 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 54 t char*, const T
72b60 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
72b70 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
72b80 65 33 46 69 78 53 72 63 4c 69 73 74 28 44 62 46 e3FixSrcList(DbF
72b90 69 78 65 72 2a 2c 20 53 72 63 4c 69 73 74 2a 29 ixer*, SrcList*)
72ba0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72bb0 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 int sqlite3FixS
72bc0 65 6c 65 63 74 28 44 62 46 69 78 65 72 2a 2c 20 elect(DbFixer*,
72bd0 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 Select*);.SQLITE
72be0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
72bf0 69 74 65 33 46 69 78 45 78 70 72 28 44 62 46 69 ite3FixExpr(DbFi
72c00 78 65 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 xer*, Expr*);.SQ
72c10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
72c20 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c sqlite3FixExprL
72c30 69 73 74 28 44 62 46 69 78 65 72 2a 2c 20 45 78 ist(DbFixer*, Ex
72c40 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 prList*);.SQLITE
72c50 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
72c60 69 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74 ite3FixTriggerSt
72c70 65 70 28 44 62 46 69 78 65 72 2a 2c 20 54 72 69 ep(DbFixer*, Tri
72c80 67 67 65 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 ggerStep*);.SQLI
72c90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
72ca0 71 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 qlite3AtoF(const
72cb0 20 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 char *z, double
72cc0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
72cd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 TE int sqlite3Ge
72ce0 74 49 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 61 tInt32(const cha
72cf0 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 r *, int*);.SQLI
72d00 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
72d10 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 69 qlite3FitsIn64Bi
72d20 74 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c ts(const char *,
72d30 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
72d40 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
72d50 33 55 74 66 31 36 42 79 74 65 4c 65 6e 28 63 6f 3Utf16ByteLen(co
72d60 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c nst void *pData,
72d70 20 69 6e 74 20 6e 43 68 61 72 29 3b 0a 53 51 4c int nChar);.SQL
72d80 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
72d90 73 71 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c sqlite3Utf8CharL
72da0 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 en(const char *p
72db0 44 61 74 61 2c 20 69 6e 74 20 6e 42 79 74 65 29 Data, int nByte)
72dc0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72dd0 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 int sqlite3Utf8
72de0 52 65 61 64 28 63 6f 6e 73 74 20 75 38 2a 2c 20 Read(const u8*,
72df0 63 6f 6e 73 74 20 75 38 2a 2a 29 3b 0a 0a 2f 2a const u8**);../*
72e00 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 .** Routines to
72e10 72 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 76 read and write v
72e20 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 ariable-length i
72e30 6e 74 65 67 65 72 73 2e 20 20 54 68 65 73 65 20 ntegers. These
72e40 75 73 65 64 20 74 6f 0a 2a 2a 20 62 65 20 64 65 used to.** be de
72e50 66 69 6e 65 64 20 6c 6f 63 61 6c 6c 79 2c 20 62 fined locally, b
72e60 75 74 20 6e 6f 77 20 77 65 20 75 73 65 20 74 68 ut now we use th
72e70 65 20 76 61 72 69 6e 74 20 72 6f 75 74 69 6e 65 e varint routine
72e80 73 20 69 6e 20 74 68 65 20 75 74 69 6c 2e 63 0a s in the util.c.
72e90 2a 2a 20 66 69 6c 65 2e 20 20 43 6f 64 65 20 73 ** file. Code s
72ea0 68 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41 hould use the MA
72eb0 43 52 4f 20 66 6f 72 6d 73 20 62 65 6c 6f 77 2c CRO forms below,
72ec0 20 61 73 20 74 68 65 20 56 61 72 69 6e 74 33 32 as the Varint32
72ed0 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 versions.** are
72ee0 20 63 6f 64 65 64 20 74 6f 20 61 73 73 75 6d 65 coded to assume
72ef0 20 74 68 65 20 73 69 6e 67 6c 65 20 62 79 74 65 the single byte
72f00 20 63 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 case is already
72f10 20 68 61 6e 64 6c 65 64 20 28 77 68 69 63 68 20 handled (which
72f20 0a 2a 2a 20 74 68 65 20 4d 41 43 52 4f 20 66 6f .** the MACRO fo
72f30 72 6d 20 64 6f 65 73 29 2e 0a 2a 2f 0a 53 51 4c rm does)..*/.SQL
72f40 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
72f50 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 sqlite3PutVarint
72f60 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c (unsigned char*,
72f70 20 75 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 u64);.SQLITE_PR
72f80 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
72f90 33 50 75 74 56 61 72 69 6e 74 33 32 28 75 6e 73 3PutVarint32(uns
72fa0 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32 igned char*, u32
72fb0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
72fc0 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 E u8 sqlite3GetV
72fd0 61 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69 arint(const unsi
72fe0 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 75 36 34 gned char *, u64
72ff0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
73000 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 ATE u8 sqlite3Ge
73010 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 tVarint32(const
73020 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c unsigned char *,
73030 20 75 33 32 20 2a 29 3b 0a 53 51 4c 49 54 45 5f u32 *);.SQLITE_
73040 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
73050 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 te3VarintLen(u64
73060 20 76 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 v);../*.** The
73070 68 65 61 64 65 72 20 6f 66 20 61 20 72 65 63 6f header of a reco
73080 72 64 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 rd consists of a
73090 20 73 65 71 75 65 6e 63 65 20 76 61 72 69 61 62 sequence variab
730a0 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 le-length intege
730b0 72 73 2e 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 rs..** These int
730c0 65 67 65 72 73 20 61 72 65 20 61 6c 6d 6f 73 74 egers are almost
730d0 20 61 6c 77 61 79 73 20 73 6d 61 6c 6c 20 61 6e always small an
730e0 64 20 61 72 65 20 65 6e 63 6f 64 65 64 20 61 73 d are encoded as
730f0 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a a single byte..
73100 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
73110 20 6d 61 63 72 6f 73 20 74 61 6b 65 20 61 64 76 macros take adv
73120 61 6e 74 61 67 65 20 74 68 69 73 20 66 61 63 74 antage this fact
73130 20 74 6f 20 70 72 6f 76 69 64 65 20 61 20 66 61 to provide a fa
73140 73 74 20 65 6e 63 6f 64 65 0a 2a 2a 20 61 6e 64 st encode.** and
73150 20 64 65 63 6f 64 65 20 6f 66 20 74 68 65 20 69 decode of the i
73160 6e 74 65 67 65 72 73 20 69 6e 20 61 20 72 65 63 ntegers in a rec
73170 6f 72 64 20 68 65 61 64 65 72 2e 20 20 49 74 20 ord header. It
73180 69 73 20 66 61 73 74 65 72 20 66 6f 72 20 74 68 is faster for th
73190 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 e common.** case
731a0 20 77 68 65 72 65 20 74 68 65 20 69 6e 74 65 67 where the integ
731b0 65 72 20 69 73 20 61 20 73 69 6e 67 6c 65 20 62 er is a single b
731c0 79 74 65 2e 20 20 49 74 20 69 73 20 61 20 6c 69 yte. It is a li
731d0 74 74 6c 65 20 73 6c 6f 77 65 72 20 77 68 65 6e ttle slower when
731e0 20 74 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 the.** integer
731f0 69 73 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 is two or more b
73200 79 74 65 73 2e 20 20 42 75 74 20 6f 76 65 72 61 ytes. But overa
73210 6c 6c 20 69 74 20 69 73 20 66 61 73 74 65 72 2e ll it is faster.
73220 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f .**.** The follo
73230 77 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 73 wing expressions
73240 20 61 72 65 20 65 71 75 69 76 61 6c 65 6e 74 3a are equivalent:
73250 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 .**.** x = s
73260 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 qlite3GetVarint3
73270 32 28 20 41 2c 20 26 42 20 29 3b 0a 2a 2a 20 20 2( A, &B );.**
73280 20 20 20 78 20 3d 20 73 71 6c 69 74 65 33 50 75 x = sqlite3Pu
73290 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 tVarint32( A, B
732a0 29 3b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d );.**.** x =
732b0 20 67 65 74 56 61 72 69 6e 74 33 32 28 20 41 2c getVarint32( A,
732c0 20 42 20 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d B );.** x =
732d0 20 70 75 74 56 61 72 69 6e 74 33 32 28 20 41 2c putVarint32( A,
732e0 20 42 20 29 3b 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 B );.**.*/.#def
732f0 69 6e 65 20 67 65 74 56 61 72 69 6e 74 33 32 28 ine getVarint32(
73300 41 2c 42 29 20 20 28 75 38 29 28 28 2a 28 41 29 A,B) (u8)((*(A)
73310 3c 28 75 38 29 30 78 38 30 29 20 3f 20 28 28 42 <(u8)0x80) ? ((B
73320 29 20 3d 20 28 75 33 32 29 2a 28 41 29 29 2c 31 ) = (u32)*(A)),1
73330 20 3a 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 : sqlite3GetVar
73340 69 6e 74 33 32 28 28 41 29 2c 20 28 75 33 32 20 int32((A), (u32
73350 2a 29 26 28 42 29 29 29 0a 23 64 65 66 69 6e 65 *)&(B))).#define
73360 20 70 75 74 56 61 72 69 6e 74 33 32 28 41 2c 42 putVarint32(A,B
73370 29 20 20 28 75 38 29 28 28 28 75 33 32 29 28 42 ) (u8)(((u32)(B
73380 29 3c 28 75 33 32 29 30 78 38 30 29 20 3f 20 28 )<(u32)0x80) ? (
73390 2a 28 41 29 20 3d 20 28 75 6e 73 69 67 6e 65 64 *(A) = (unsigned
733a0 20 63 68 61 72 29 28 42 29 29 2c 31 20 3a 20 73 char)(B)),1 : s
733b0 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 qlite3PutVarint3
733c0 32 28 28 41 29 2c 20 28 42 29 29 29 0a 23 64 65 2((A), (B))).#de
733d0 66 69 6e 65 20 67 65 74 56 61 72 69 6e 74 20 20 fine getVarint
733e0 20 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 sqlite3GetVari
733f0 6e 74 0a 23 64 65 66 69 6e 65 20 70 75 74 56 61 nt.#define putVa
73400 72 69 6e 74 20 20 20 20 73 71 6c 69 74 65 33 50 rint sqlite3P
73410 75 74 56 61 72 69 6e 74 0a 0a 0a 53 51 4c 49 54 utVarint...SQLIT
73420 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
73430 63 68 61 72 20 2a 73 71 6c 69 74 65 33 49 6e 64 char *sqlite3Ind
73440 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 exAffinityStr(Vd
73450 62 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a be *, Index *);.
73460 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
73470 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65 oid sqlite3Table
73480 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 62 65 AffinityStr(Vdbe
73490 20 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53 51 *, Table *);.SQ
734a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
734b0 72 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 r sqlite3Compare
734c0 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70 Affinity(Expr *p
734d0 45 78 70 72 2c 20 63 68 61 72 20 61 66 66 32 29 Expr, char aff2)
734e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
734f0 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 int sqlite3Inde
73500 78 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 72 xAffinityOk(Expr
73510 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69 64 *pExpr, char id
73520 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 53 51 4c x_affinity);.SQL
73530 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
73540 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 sqlite3ExprAffi
73550 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 nity(Expr *pExpr
73560 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73570 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f E int sqlite3Ato
73580 69 36 34 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c i64(const char*,
73590 20 69 36 34 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 i64*);.SQLITE_P
735a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
735b0 74 65 33 45 72 72 6f 72 28 73 71 6c 69 74 65 33 te3Error(sqlite3
735c0 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 *, int, const ch
735d0 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 ar*,...);.SQLITE
735e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
735f0 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 qlite3HexToBlob(
73600 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 sqlite3*, const
73610 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 3b char *z, int n);
73620 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73630 69 6e 74 20 73 71 6c 69 74 65 33 54 77 6f 50 61 int sqlite3TwoPa
73640 72 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20 rtName(Parse *,
73650 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a Token *, Token *
73660 2c 20 54 6f 6b 65 6e 20 2a 2a 29 3b 0a 53 51 4c , Token **);.SQL
73670 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
73680 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 45 t char *sqlite3E
73690 72 72 53 74 72 28 69 6e 74 29 3b 0a 53 51 4c 49 rrStr(int);.SQLI
736a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
736b0 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 qlite3ReadSchema
736c0 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b (Parse *pParse);
736d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
736e0 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 CollSeq *sqlite3
736f0 46 69 6e 64 43 6f 6c 6c 53 65 71 28 73 71 6c 69 FindCollSeq(sqli
73700 74 65 33 2a 2c 75 38 20 65 6e 63 2c 20 63 6f 6e te3*,u8 enc, con
73710 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 st char*,int);.S
73720 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f QLITE_PRIVATE Co
73730 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 4c 6f llSeq *sqlite3Lo
73740 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 cateCollSeq(Pars
73750 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 e *pParse, const
73760 20 63 68 61 72 2a 7a 4e 61 6d 65 29 3b 0a 53 51 char*zName);.SQ
73770 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c LITE_PRIVATE Col
73780 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 45 78 70 lSeq *sqlite3Exp
73790 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a rCollSeq(Parse *
737a0 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 pParse, Expr *pE
737b0 78 70 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 xpr);.SQLITE_PRI
737c0 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 VATE Expr *sqlit
737d0 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 e3ExprSetColl(Pa
737e0 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 rse *pParse, Exp
737f0 72 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 r *, Token *);.S
73800 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
73810 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f t sqlite3CheckCo
73820 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 2c 20 43 llSeq(Parse *, C
73830 6f 6c 6c 53 65 71 20 2a 29 3b 0a 53 51 4c 49 54 ollSeq *);.SQLIT
73840 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
73850 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 lite3CheckObject
73860 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20 63 6f Name(Parse *, co
73870 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c nst char *);.SQL
73880 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
73890 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 sqlite3VdbeSetC
738a0 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a hanges(sqlite3 *
738b0 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f , int);..SQLITE_
738c0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f PRIVATE const vo
738d0 69 64 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 id *sqlite3Value
738e0 54 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c Text(sqlite3_val
738f0 75 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 ue*, u8);.SQLITE
73900 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
73910 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 73 ite3ValueBytes(s
73920 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 75 qlite3_value*, u
73930 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 8);.SQLITE_PRIVA
73940 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
73950 61 6c 75 65 53 65 74 53 74 72 28 73 71 6c 69 74 alueSetStr(sqlit
73960 65 33 5f 76 61 6c 75 65 2a 2c 20 69 6e 74 2c 20 e3_value*, int,
73970 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 75 38 2c const void *,u8,
73980 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
73990 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 28 2a void(*
739a0 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 )(void*));.SQLIT
739b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
739c0 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 qlite3ValueFree(
739d0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b sqlite3_value*);
739e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
739f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 sqlite3_value *s
73a00 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 73 qlite3ValueNew(s
73a10 71 6c 69 74 65 33 20 2a 29 3b 0a 53 51 4c 49 54 qlite3 *);.SQLIT
73a20 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a E_PRIVATE char *
73a30 73 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38 28 sqlite3Utf16to8(
73a40 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 sqlite3 *, const
73a50 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 23 69 void*, int);.#i
73a60 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
73a70 4c 45 5f 53 54 41 54 32 0a 53 51 4c 49 54 45 5f LE_STAT2.SQLITE_
73a80 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 PRIVATE char *sq
73a90 6c 69 74 65 33 55 74 66 38 74 6f 31 36 28 73 71 lite3Utf8to16(sq
73aa0 6c 69 74 65 33 20 2a 2c 20 75 38 2c 20 63 68 61 lite3 *, u8, cha
73ab0 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 r *, int, int *)
73ac0 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f ;.#endif.SQLITE_
73ad0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
73ae0 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 te3ValueFromExpr
73af0 28 73 71 6c 69 74 65 33 20 2a 2c 20 45 78 70 72 (sqlite3 *, Expr
73b00 20 2a 2c 20 75 38 2c 20 75 38 2c 20 73 71 6c 69 *, u8, u8, sqli
73b10 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 3b 0a 53 te3_value **);.S
73b20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
73b30 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 id sqlite3ValueA
73b40 70 70 6c 79 41 66 66 69 6e 69 74 79 28 73 71 6c pplyAffinity(sql
73b50 69 74 65 33 5f 76 61 6c 75 65 20 2a 2c 20 75 38 ite3_value *, u8
73b60 2c 20 75 38 29 3b 0a 23 69 66 6e 64 65 66 20 53 , u8);.#ifndef S
73b70 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 QLITE_AMALGAMATI
73b80 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ON.SQLITE_PRIVAT
73b90 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 E const unsigned
73ba0 20 63 68 61 72 20 73 71 6c 69 74 65 33 55 70 70 char sqlite3Upp
73bb0 65 72 54 6f 4c 6f 77 65 72 5b 5d 3b 0a 53 51 4c erToLower[];.SQL
73bc0 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
73bd0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
73be0 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b sqlite3CtypeMap[
73bf0 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ];.SQLITE_PRIVAT
73c00 45 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 E SQLITE_WSD str
73c10 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 uct Sqlite3Confi
73c20 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 3b g sqlite3Config;
73c30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73c40 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44 SQLITE_WSD FuncD
73c50 65 66 48 61 73 68 20 73 71 6c 69 74 65 33 47 6c efHash sqlite3Gl
73c60 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a 53 obalFunctions;.S
73c70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
73c80 74 20 73 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 t sqlite3Pending
73c90 42 79 74 65 3b 0a 23 65 6e 64 69 66 0a 53 51 4c Byte;.#endif.SQL
73ca0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
73cb0 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61 67 65 sqlite3RootPage
73cc0 4d 6f 76 65 64 28 44 62 2a 2c 20 69 6e 74 2c 20 Moved(Db*, int,
73cd0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
73ce0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
73cf0 33 52 65 69 6e 64 65 78 28 50 61 72 73 65 2a 2c 3Reindex(Parse*,
73d00 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 Token*, Token*)
73d10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73d20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 void sqlite3Alt
73d30 65 72 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 erFunctions(sqli
73d40 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 te3*);.SQLITE_PR
73d50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
73d60 65 33 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 e3AlterRenameTab
73d70 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 le(Parse*, SrcLi
73d80 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 st*, Token*);.SQ
73d90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
73da0 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e sqlite3GetToken
73db0 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 (const unsigned
73dc0 63 68 61 72 20 2a 2c 20 69 6e 74 20 2a 29 3b 0a char *, int *);.
73dd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
73de0 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73 74 65 oid sqlite3Neste
73df0 64 50 61 72 73 65 28 50 61 72 73 65 2a 2c 20 63 dParse(Parse*, c
73e00 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 onst char*, ...)
73e10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73e20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
73e30 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65 irePreparedState
73e40 6d 65 6e 74 73 28 73 71 6c 69 74 65 33 2a 29 3b ments(sqlite3*);
73e50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73e60 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 65 53 int sqlite3CodeS
73e70 75 62 73 65 6c 65 63 74 28 50 61 72 73 65 20 2a ubselect(Parse *
73e80 2c 20 45 78 70 72 20 2a 2c 20 69 6e 74 2c 20 69 , Expr *, int, i
73e90 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
73ea0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
73eb0 53 65 6c 65 63 74 50 72 65 70 28 50 61 72 73 65 SelectPrep(Parse
73ec0 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 4e 61 6d 65 *, Select*, Name
73ed0 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 Context*);.SQLIT
73ee0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
73ef0 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 lite3ResolveExpr
73f00 4e 61 6d 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78 Names(NameContex
73f10 74 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 t*, Expr*);.SQLI
73f20 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
73f30 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 sqlite3ResolveSe
73f40 6c 65 63 74 4e 61 6d 65 73 28 50 61 72 73 65 2a lectNames(Parse*
73f50 2c 20 53 65 6c 65 63 74 2a 2c 20 4e 61 6d 65 43 , Select*, NameC
73f60 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 ontext*);.SQLITE
73f70 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
73f80 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 ite3ResolveOrder
73f90 47 72 6f 75 70 42 79 28 50 61 72 73 65 2a 2c 20 GroupBy(Parse*,
73fa0 53 65 6c 65 63 74 2a 2c 20 45 78 70 72 4c 69 73 Select*, ExprLis
73fb0 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 t*, const char*)
73fc0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73fd0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6c void sqlite3Col
73fe0 75 6d 6e 44 65 66 61 75 6c 74 28 56 64 62 65 20 umnDefault(Vdbe
73ff0 2a 2c 20 54 61 62 6c 65 20 2a 2c 20 69 6e 74 2c *, Table *, int,
74000 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
74010 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
74020 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64 e3AlterFinishAdd
74030 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20 Column(Parse *,
74040 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 Token *);.SQLITE
74050 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
74060 6c 69 74 65 33 41 6c 74 65 72 42 65 67 69 6e 41 lite3AlterBeginA
74070 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a ddColumn(Parse *
74080 2c 20 53 72 63 4c 69 73 74 20 2a 29 3b 0a 53 51 , SrcList *);.SQ
74090 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c LITE_PRIVATE Col
740a0 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 47 65 74 lSeq *sqlite3Get
740b0 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a CollSeq(sqlite3*
740c0 2c 20 75 38 2c 20 43 6f 6c 6c 53 65 71 20 2a 2c , u8, CollSeq *,
740d0 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 const char*);.S
740e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
740f0 61 72 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 ar sqlite3Affini
74100 74 79 54 79 70 65 28 63 6f 6e 73 74 20 63 68 61 tyType(const cha
74110 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
74120 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
74130 41 6e 61 6c 79 7a 65 28 50 61 72 73 65 2a 2c 20 Analyze(Parse*,
74140 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b Token*, Token*);
74150 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74160 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b int sqlite3Invok
74170 65 42 75 73 79 48 61 6e 64 6c 65 72 28 42 75 73 eBusyHandler(Bus
74180 79 48 61 6e 64 6c 65 72 2a 29 3b 0a 53 51 4c 49 yHandler*);.SQLI
74190 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
741a0 71 6c 69 74 65 33 46 69 6e 64 44 62 28 73 71 6c qlite3FindDb(sql
741b0 69 74 65 33 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a ite3*, Token*);.
741c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
741d0 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 nt sqlite3FindDb
741e0 4e 61 6d 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 Name(sqlite3 *,
741f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 const char *);.S
74200 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
74210 74 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73 69 t sqlite3Analysi
74220 73 4c 6f 61 64 28 73 71 6c 69 74 65 33 2a 2c 69 sLoad(sqlite3*,i
74230 6e 74 20 69 44 42 29 3b 0a 53 51 4c 49 54 45 5f nt iDB);.SQLITE_
74240 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
74250 69 74 65 33 44 65 6c 65 74 65 49 6e 64 65 78 53 ite3DeleteIndexS
74260 61 6d 70 6c 65 73 28 49 6e 64 65 78 2a 29 3b 0a amples(Index*);.
74270 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
74280 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 61 75 oid sqlite3Defau
74290 6c 74 52 6f 77 45 73 74 28 49 6e 64 65 78 2a 29 ltRowEst(Index*)
742a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
742b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 void sqlite3Reg
742c0 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f isterLikeFunctio
742d0 6e 73 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 ns(sqlite3*, int
742e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
742f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4c E int sqlite3IsL
74300 69 6b 65 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 ikeFunction(sqli
74310 74 65 33 2a 2c 45 78 70 72 2a 2c 69 6e 74 2a 2c te3*,Expr*,int*,
74320 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 char*);.SQLITE_P
74330 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
74340 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f te3MinimumFileFo
74350 72 6d 61 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 rmat(Parse*, int
74360 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
74370 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
74380 74 65 33 53 63 68 65 6d 61 46 72 65 65 28 76 6f te3SchemaFree(vo
74390 69 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 id *);.SQLITE_PR
743a0 49 56 41 54 45 20 53 63 68 65 6d 61 20 2a 73 71 IVATE Schema *sq
743b0 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74 28 73 lite3SchemaGet(s
743c0 71 6c 69 74 65 33 20 2a 2c 20 42 74 72 65 65 20 qlite3 *, Btree
743d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
743e0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 63 TE int sqlite3Sc
743f0 68 65 6d 61 54 6f 49 6e 64 65 78 28 73 71 6c 69 hemaToIndex(sqli
74400 74 65 33 20 2a 64 62 2c 20 53 63 68 65 6d 61 20 te3 *db, Schema
74410 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
74420 54 45 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c 69 TE KeyInfo *sqli
74430 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 te3IndexKeyinfo(
74440 50 61 72 73 65 20 2a 2c 20 49 6e 64 65 78 20 2a Parse *, Index *
74450 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
74460 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 72 65 E int sqlite3Cre
74470 61 74 65 46 75 6e 63 28 73 71 6c 69 74 65 33 20 ateFunc(sqlite3
74480 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c *, const char *,
74490 20 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64 20 int, int, void
744a0 2a 2c 20 0a 20 20 76 6f 69 64 20 28 2a 29 28 73 *, . void (*)(s
744b0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
744c0 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 int,sqlite3_valu
744d0 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a e **),. void (*
744e0 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 )(sqlite3_contex
744f0 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 t*,int,sqlite3_v
74500 61 6c 75 65 20 2a 2a 29 2c 20 76 6f 69 64 20 28 alue **), void (
74510 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 *)(sqlite3_conte
74520 78 74 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xt*));.SQLITE_PR
74530 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
74540 33 41 70 69 45 78 69 74 28 73 71 6c 69 74 65 33 3ApiExit(sqlite3
74550 20 2a 64 62 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 *db, int);.SQLI
74560 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
74570 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 qlite3OpenTempDa
74580 74 61 62 61 73 65 28 50 61 72 73 65 20 2a 29 3b tabase(Parse *);
74590 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
745a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 void sqlite3Str
745b0 41 63 63 75 6d 49 6e 69 74 28 53 74 72 41 63 63 AccumInit(StrAcc
745c0 75 6d 2a 2c 20 63 68 61 72 2a 2c 20 69 6e 74 2c um*, char*, int,
745d0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
745e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
745f0 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 e3StrAccumAppend
74600 28 53 74 72 41 63 63 75 6d 2a 2c 63 6f 6e 73 74 (StrAccum*,const
74610 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c char*,int);.SQL
74620 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
74630 20 2a 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 *sqlite3StrAccu
74640 6d 46 69 6e 69 73 68 28 53 74 72 41 63 63 75 6d mFinish(StrAccum
74650 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
74660 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
74670 74 72 41 63 63 75 6d 52 65 73 65 74 28 53 74 72 trAccumReset(Str
74680 41 63 63 75 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f Accum*);.SQLITE_
74690 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
746a0 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e ite3SelectDestIn
746b0 69 74 28 53 65 6c 65 63 74 44 65 73 74 2a 2c 69 it(SelectDest*,i
746c0 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f nt,int);.SQLITE_
746d0 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 PRIVATE Expr *sq
746e0 6c 69 74 65 33 43 72 65 61 74 65 43 6f 6c 75 6d lite3CreateColum
746f0 6e 45 78 70 72 28 73 71 6c 69 74 65 33 20 2a 2c nExpr(sqlite3 *,
74700 20 53 72 63 4c 69 73 74 20 2a 2c 20 69 6e 74 2c SrcList *, int,
74710 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 int);..SQLITE_P
74720 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
74730 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74 te3BackupRestart
74740 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
74750 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
74760 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
74770 61 63 6b 75 70 55 70 64 61 74 65 28 73 71 6c 69 ackupUpdate(sqli
74780 74 65 33 5f 62 61 63 6b 75 70 20 2a 2c 20 50 67 te3_backup *, Pg
74790 6e 6f 2c 20 63 6f 6e 73 74 20 75 38 20 2a 29 3b no, const u8 *);
747a0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 ../*.** The inte
747b0 72 66 61 63 65 20 74 6f 20 74 68 65 20 4c 45 4d rface to the LEM
747c0 4f 4e 2d 67 65 6e 65 72 61 74 65 64 20 70 61 72 ON-generated par
747d0 73 65 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ser.*/.SQLITE_PR
747e0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
747f0 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28 76 te3ParserAlloc(v
74800 6f 69 64 2a 28 2a 29 28 73 69 7a 65 5f 74 29 29 oid*(*)(size_t))
74810 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74820 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 void sqlite3Par
74830 73 65 72 46 72 65 65 28 76 6f 69 64 2a 2c 20 76 serFree(void*, v
74840 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a oid(*)(void*));.
74850 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
74860 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 oid sqlite3Parse
74870 72 28 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 54 6f r(void*, int, To
74880 6b 65 6e 2c 20 50 61 72 73 65 2a 29 3b 0a 23 69 ken, Parse*);.#i
74890 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53 fdef YYTRACKMAXS
748a0 54 41 43 4b 44 45 50 54 48 0a 53 51 4c 49 54 45 TACKDEPTH.SQLITE
748b0 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
748c0 71 6c 69 74 65 33 50 61 72 73 65 72 53 74 61 63 qlite3ParserStac
748d0 6b 50 65 61 6b 28 76 6f 69 64 2a 29 3b 0a 23 65 kPeak(void*);.#e
748e0 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 ndif..SQLITE_PRI
748f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
74900 33 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69 3AutoLoadExtensi
74910 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 ons(sqlite3*);.#
74920 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
74930 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f IT_LOAD_EXTENSIO
74940 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 N.SQLITE_PRIVATE
74950 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 void sqlite3C
74960 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 73 loseExtensions(s
74970 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a qlite3*);.#else.
74980 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
74990 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 CloseExtensions(
749a0 58 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 X).#endif..#ifnd
749b0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
749c0 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 HARED_CACHE.SQLI
749d0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
749e0 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f d sqlite3TableLo
749f0 63 6b 28 50 61 72 73 65 20 2a 2c 20 69 6e 74 2c ck(Parse *, int,
74a00 20 69 6e 74 2c 20 75 38 2c 20 63 6f 6e 73 74 20 int, u8, const
74a10 63 68 61 72 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 char *);.#else.
74a20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
74a30 54 61 62 6c 65 4c 6f 63 6b 28 76 2c 77 2c 78 2c TableLock(v,w,x,
74a40 79 2c 7a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 y,z).#endif..#if
74a50 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
74a60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
74a70 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 int sqlite3Utf8
74a80 54 6f 38 28 75 6e 73 69 67 6e 65 64 20 63 68 61 To8(unsigned cha
74a90 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 r*);.#endif..#if
74aa0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
74ab0 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 20 VIRTUALTABLE.#
74ac0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 define sqlite3Vt
74ad0 61 62 43 6c 65 61 72 28 59 29 0a 23 20 20 64 65 abClear(Y).# de
74ae0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 fine sqlite3Vtab
74af0 53 79 6e 63 28 58 2c 59 29 20 53 51 4c 49 54 45 Sync(X,Y) SQLITE
74b00 5f 4f 4b 0a 23 20 20 64 65 66 69 6e 65 20 73 71 _OK.# define sq
74b10 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 lite3VtabRollbac
74b20 6b 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 k(X).# define s
74b30 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 qlite3VtabCommit
74b40 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 (X).# define sq
74b50 6c 69 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28 lite3VtabInSync(
74b60 64 62 29 20 30 0a 23 20 20 64 65 66 69 6e 65 20 db) 0.# define
74b70 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 sqlite3VtabLock(
74b80 58 29 20 0a 23 20 20 64 65 66 69 6e 65 20 73 71 X) .# define sq
74b90 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 lite3VtabUnlock(
74ba0 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c X).# define sql
74bb0 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 ite3VtabUnlockLi
74bc0 73 74 28 58 29 0a 23 65 6c 73 65 0a 53 51 4c 49 st(X).#else.SQLI
74bd0 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f TE_PRIVATE vo
74be0 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c id sqlite3VtabCl
74bf0 65 61 72 28 54 61 62 6c 65 2a 29 3b 0a 53 51 4c ear(Table*);.SQL
74c00 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69 ITE_PRIVATE i
74c10 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 nt sqlite3VtabSy
74c20 6e 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 nc(sqlite3 *db,
74c30 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 char **);.SQLITE
74c40 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e 74 20 _PRIVATE int
74c50 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 sqlite3VtabRollb
74c60 61 63 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 ack(sqlite3 *db)
74c70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74c80 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 int sqlite3V
74c90 74 61 62 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 tabCommit(sqlite
74ca0 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 50 3 *db);.SQLITE_P
74cb0 52 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 73 RIVATE void s
74cc0 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 56 qlite3VtabLock(V
74cd0 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 Table *);.SQLITE
74ce0 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f 69 64 _PRIVATE void
74cf0 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f sqlite3VtabUnlo
74d00 63 6b 28 56 54 61 62 6c 65 20 2a 29 3b 0a 53 51 ck(VTable *);.SQ
74d10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 LITE_PRIVATE
74d20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 void sqlite3Vtab
74d30 55 6e 6c 6f 63 6b 4c 69 73 74 28 73 71 6c 69 74 UnlockList(sqlit
74d40 65 33 2a 29 3b 0a 23 20 20 64 65 66 69 6e 65 20 e3*);.# define
74d50 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53 79 6e sqlite3VtabInSyn
74d60 63 28 64 62 29 20 28 28 64 62 29 2d 3e 6e 56 54 c(db) ((db)->nVT
74d70 72 61 6e 73 3e 30 20 26 26 20 28 64 62 29 2d 3e rans>0 && (db)->
74d80 61 56 54 72 61 6e 73 3d 3d 30 29 0a 23 65 6e 64 aVTrans==0).#end
74d90 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 if.SQLITE_PRIVAT
74da0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 E void sqlite3Vt
74db0 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 50 abMakeWritable(P
74dc0 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 53 arse*,Table*);.S
74dd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
74de0 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 id sqlite3VtabBe
74df0 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 2a 2c ginParse(Parse*,
74e00 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c Token*, Token*,
74e10 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 Token*);.SQLITE
74e20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
74e30 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73 68 50 lite3VtabFinishP
74e40 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b arse(Parse*, Tok
74e50 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 en*);.SQLITE_PRI
74e60 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
74e70 33 56 74 61 62 41 72 67 49 6e 69 74 28 50 61 72 3VtabArgInit(Par
74e80 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 se*);.SQLITE_PRI
74e90 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
74ea0 33 56 74 61 62 41 72 67 45 78 74 65 6e 64 28 50 3VtabArgExtend(P
74eb0 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a arse*, Token*);.
74ec0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
74ed0 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 nt sqlite3VtabCa
74ee0 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 llCreate(sqlite3
74ef0 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 *, int, const ch
74f00 61 72 20 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a ar *, char **);.
74f10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
74f20 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 nt sqlite3VtabCa
74f30 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73 65 2a llConnect(Parse*
74f40 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 , Table*);.SQLIT
74f50 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
74f60 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44 65 73 lite3VtabCallDes
74f70 74 72 6f 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 troy(sqlite3*, i
74f80 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a nt, const char *
74f90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
74fa0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 E int sqlite3Vta
74fb0 62 42 65 67 69 6e 28 73 71 6c 69 74 65 33 20 2a bBegin(sqlite3 *
74fc0 2c 20 56 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c , VTable *);.SQL
74fd0 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63 ITE_PRIVATE Func
74fe0 44 65 66 20 2a 73 71 6c 69 74 65 33 56 74 61 62 Def *sqlite3Vtab
74ff0 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e OverloadFunction
75000 28 73 71 6c 69 74 65 33 20 2a 2c 46 75 6e 63 44 (sqlite3 *,FuncD
75010 65 66 2a 2c 20 69 6e 74 20 6e 41 72 67 2c 20 45 ef*, int nArg, E
75020 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
75030 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
75040 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f e3InvalidFunctio
75050 6e 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 n(sqlite3_contex
75060 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 t*,int,sqlite3_v
75070 61 6c 75 65 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f alue**);.SQLITE_
75080 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
75090 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 te3TransferBindi
750a0 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ngs(sqlite3_stmt
750b0 20 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 *, sqlite3_stmt
750c0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
750d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 ATE int sqlite3R
750e0 65 70 72 65 70 61 72 65 28 56 64 62 65 2a 29 3b eprepare(Vdbe*);
750f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
75100 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
75110 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 ListCheckLength(
75120 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 Parse*, ExprList
75130 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b *, const char*);
75140 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
75150 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 CollSeq *sqlite3
75160 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c BinaryCompareCol
75170 6c 53 65 71 28 50 61 72 73 65 20 2a 2c 20 45 78 lSeq(Parse *, Ex
75180 70 72 20 2a 2c 20 45 78 70 72 20 2a 29 3b 0a 53 pr *, Expr *);.S
75190 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
751a0 74 20 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d t sqlite3TempInM
751b0 65 6d 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c 69 emory(const sqli
751c0 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 te3*);.SQLITE_PR
751d0 49 56 41 54 45 20 56 54 61 62 6c 65 20 2a 73 71 IVATE VTable *sq
751e0 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 73 lite3GetVTable(s
751f0 71 6c 69 74 65 33 2a 2c 20 54 61 62 6c 65 2a 29 qlite3*, Table*)
75200 3b 0a 0a 2f 2a 20 44 65 63 6c 61 72 61 74 69 6f ;../* Declaratio
75210 6e 73 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 73 ns for functions
75220 20 69 6e 20 66 6b 65 79 2e 63 2e 20 41 6c 6c 20 in fkey.c. All
75230 6f 66 20 74 68 65 73 65 20 61 72 65 20 72 65 70 of these are rep
75240 6c 61 63 65 64 20 62 79 0a 2a 2a 20 6e 6f 2d 6f laced by.** no-o
75250 70 20 6d 61 63 72 6f 73 20 69 66 20 4f 4d 49 54 p macros if OMIT
75260 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 20 69 73 20 _FOREIGN_KEY is
75270 64 65 66 69 6e 65 64 2e 20 49 6e 20 74 68 69 73 defined. In this
75280 20 63 61 73 65 20 6e 6f 20 66 6f 72 65 69 67 6e case no foreign
75290 0a 2a 2a 20 6b 65 79 20 66 75 6e 63 74 69 6f 6e .** key function
752a0 61 6c 69 74 79 20 69 73 20 61 76 61 69 6c 61 62 ality is availab
752b0 6c 65 2e 20 49 66 20 4f 4d 49 54 5f 54 52 49 47 le. If OMIT_TRIG
752c0 47 45 52 20 69 73 20 64 65 66 69 6e 65 64 20 62 GER is defined b
752d0 75 74 0a 2a 2a 20 4f 4d 49 54 5f 46 4f 52 45 49 ut.** OMIT_FOREI
752e0 47 4e 5f 4b 45 59 20 69 73 20 6e 6f 74 2c 20 6f GN_KEY is not, o
752f0 6e 6c 79 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 nly some of the
75300 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f functions are no
75310 2d 6f 70 65 64 2e 20 49 6e 0a 2a 2a 20 74 68 69 -oped. In.** thi
75320 73 20 63 61 73 65 20 66 6f 72 65 69 67 6e 20 6b s case foreign k
75330 65 79 73 20 61 72 65 20 70 61 72 73 65 64 2c 20 eys are parsed,
75340 62 75 74 20 6e 6f 20 6f 74 68 65 72 20 66 75 6e but no other fun
75350 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 0a 2a ctionality is .*
75360 2a 20 70 72 6f 76 69 64 65 64 20 28 65 6e 66 6f * provided (enfo
75370 72 63 65 6d 65 6e 74 20 6f 66 20 46 4b 20 63 6f rcement of FK co
75380 6e 73 74 72 61 69 6e 74 73 20 72 65 71 75 69 72 nstraints requir
75390 65 73 20 74 68 65 20 74 72 69 67 67 65 72 73 20 es the triggers
753a0 73 75 62 2d 73 79 73 74 65 6d 29 2e 0a 2a 2f 0a sub-system)..*/.
753b0 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
753c0 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e ITE_OMIT_FOREIGN
753d0 5f 4b 45 59 29 20 26 26 20 21 64 65 66 69 6e 65 _KEY) && !define
753e0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 d(SQLITE_OMIT_TR
753f0 49 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52 IGGER).SQLITE_PR
75400 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
75410 69 74 65 33 46 6b 43 68 65 63 6b 28 50 61 72 73 ite3FkCheck(Pars
75420 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c e*, Table*, int,
75430 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
75440 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
75450 69 74 65 33 46 6b 44 72 6f 70 54 61 62 6c 65 28 ite3FkDropTable(
75460 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 20 Parse*, SrcList
75470 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 *, Table*);.SQLI
75480 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
75490 64 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69 6f d sqlite3FkActio
754a0 6e 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 ns(Parse*, Table
754b0 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e *, ExprList*, in
754c0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
754d0 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 TE int sqlite3
754e0 46 6b 52 65 71 75 69 72 65 64 28 50 61 72 73 65 FkRequired(Parse
754f0 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2a 2c *, Table*, int*,
75500 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
75510 49 56 41 54 45 20 20 20 75 33 32 20 73 71 6c 69 IVATE u32 sqli
75520 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 50 61 72 te3FkOldmask(Par
75530 73 65 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 se*, Table*);.SQ
75540 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 46 LITE_PRIVATE F
75550 4b 65 79 20 2a 73 71 6c 69 74 65 33 46 6b 52 65 Key *sqlite3FkRe
75560 66 65 72 65 6e 63 65 73 28 54 61 62 6c 65 20 2a ferences(Table *
75570 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 );.#else. #defi
75580 6e 65 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69 ne sqlite3FkActi
75590 6f 6e 73 28 61 2c 62 2c 63 2c 64 29 0a 20 20 23 ons(a,b,c,d). #
755a0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 6b define sqlite3Fk
755b0 43 68 65 63 6b 28 61 2c 62 2c 63 2c 64 29 0a 20 Check(a,b,c,d).
755c0 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
755d0 46 6b 44 72 6f 70 54 61 62 6c 65 28 61 2c 62 2c FkDropTable(a,b,
755e0 63 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c c). #define sql
755f0 69 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 61 2c ite3FkOldmask(a,
75600 62 29 20 20 20 20 20 20 30 0a 20 20 23 64 65 66 b) 0. #def
75610 69 6e 65 20 73 71 6c 69 74 65 33 46 6b 52 65 71 ine sqlite3FkReq
75620 75 69 72 65 64 28 61 2c 62 2c 63 2c 64 29 20 30 uired(a,b,c,d) 0
75630 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
75640 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 SQLITE_OMIT_FORE
75650 49 47 4e 5f 4b 45 59 0a 53 51 4c 49 54 45 5f 50 IGN_KEY.SQLITE_P
75660 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
75670 6c 69 74 65 33 46 6b 44 65 6c 65 74 65 28 54 61 lite3FkDelete(Ta
75680 62 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 ble*);.#else. #
75690 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 6b define sqlite3Fk
756a0 44 65 6c 65 74 65 28 61 29 0a 23 65 6e 64 69 66 Delete(a).#endif
756b0 0a 0a 0a 2f 2a 0a 2a 2a 20 41 76 61 69 6c 61 62 .../*.** Availab
756c0 6c 65 20 66 61 75 6c 74 20 69 6e 6a 65 63 74 6f le fault injecto
756d0 72 73 2e 20 20 53 68 6f 75 6c 64 20 62 65 20 6e rs. Should be n
756e0 75 6d 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e umbered beginnin
756f0 67 20 77 69 74 68 20 30 2e 0a 2a 2f 0a 23 64 65 g with 0..*/.#de
75700 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 41 55 4c fine SQLITE_FAUL
75710 54 49 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c 4f 43 TINJECTOR_MALLOC
75720 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 0.#define S
75730 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 QLITE_FAULTINJEC
75740 54 4f 52 5f 43 4f 55 4e 54 20 20 20 20 20 20 31 TOR_COUNT 1
75750 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 ../*.** The inte
75760 72 66 61 63 65 20 74 6f 20 74 68 65 20 63 6f 64 rface to the cod
75770 65 20 69 6e 20 66 61 75 6c 74 2e 63 20 75 73 65 e in fault.c use
75780 64 20 66 6f 72 20 69 64 65 6e 74 69 66 79 69 6e d for identifyin
75790 67 20 22 62 65 6e 69 67 6e 22 0a 2a 2a 20 6d 61 g "benign".** ma
757a0 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 2e 20 54 lloc failures. T
757b0 68 69 73 20 69 73 20 6f 6e 6c 79 20 70 72 65 73 his is only pres
757c0 65 6e 74 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d ent if SQLITE_OM
757d0 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a IT_BUILTIN_TEST.
757e0 2a 2a 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 ** is not define
757f0 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 d..*/.#ifndef SQ
75800 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 LITE_OMIT_BUILTI
75810 4e 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 N_TEST.SQLITE_PR
75820 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
75830 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d ite3BeginBenignM
75840 61 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a 53 51 4c alloc(void);.SQL
75850 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
75860 69 64 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e id sqlite3EndBen
75870 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 3b ignMalloc(void);
75880 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 .#else. #define
75890 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e sqlite3BeginBen
758a0 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 20 20 23 64 ignMalloc(). #d
758b0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 6e 64 efine sqlite3End
758c0 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 23 BenignMalloc().#
758d0 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 49 endif..#define I
758e0 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 20 20 N_INDEX_ROWID
758f0 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 1.#defin
75900 65 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20 20 e IN_INDEX_EPH
75910 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 2.#de
75920 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 49 4e fine IN_INDEX_IN
75930 44 45 58 20 20 20 20 20 20 20 20 20 20 20 33 0a DEX 3.
75940 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
75950 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e nt sqlite3FindIn
75960 49 6e 64 65 78 28 50 61 72 73 65 20 2a 2c 20 45 Index(Parse *, E
75970 78 70 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a 0a 23 xpr *, int*);..#
75980 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
75990 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 BLE_ATOMIC_WRITE
759a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
759b0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 int sqlite3Jou
759c0 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 rnalOpen(sqlite3
759d0 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 _vfs *, const ch
759e0 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 ar *, sqlite3_fi
759f0 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b le *, int, int);
75a00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
75a10 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 int sqlite3Jou
75a20 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 33 rnalSize(sqlite3
75a30 5f 76 66 73 20 2a 29 3b 0a 53 51 4c 49 54 45 5f _vfs *);.SQLITE_
75a40 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
75a50 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 lite3JournalCrea
75a60 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 te(sqlite3_file
75a70 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 *);.#else. #def
75a80 69 6e 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e ine sqlite3Journ
75a90 61 6c 53 69 7a 65 28 70 56 66 73 29 20 28 28 70 alSize(pVfs) ((p
75aa0 56 66 73 29 2d 3e 73 7a 4f 73 46 69 6c 65 29 0a Vfs)->szOsFile).
75ab0 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 #endif..SQLITE_P
75ac0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
75ad0 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 te3MemJournalOpe
75ae0 6e 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a n(sqlite3_file *
75af0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
75b00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d E int sqlite3Mem
75b10 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64 JournalSize(void
75b20 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
75b30 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d E int sqlite3IsM
75b40 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 emJournal(sqlite
75b50 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 69 66 20 3_file *);..#if
75b60 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f SQLITE_MAX_EXPR_
75b70 44 45 50 54 48 3e 30 0a 53 51 4c 49 54 45 5f 50 DEPTH>0.SQLITE_P
75b80 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
75b90 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 lite3ExprSetHeig
75ba0 68 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ht(Parse *pParse
75bb0 2c 20 45 78 70 72 20 2a 70 29 3b 0a 53 51 4c 49 , Expr *p);.SQLI
75bc0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
75bd0 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 sqlite3SelectEx
75be0 70 72 48 65 69 67 68 74 28 53 65 6c 65 63 74 20 prHeight(Select
75bf0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
75c00 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 TE int sqlite3
75c10 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 ExprCheckHeight(
75c20 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 65 Parse*, int);.#e
75c30 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 lse. #define sq
75c40 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 lite3ExprSetHeig
75c50 68 74 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e ht(x,y). #defin
75c60 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 e sqlite3SelectE
75c70 78 70 72 48 65 69 67 68 74 28 78 29 20 30 0a 20 xprHeight(x) 0.
75c80 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
75c90 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 ExprCheckHeight(
75ca0 78 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c x,y).#endif..SQL
75cb0 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 ITE_PRIVATE u32
75cc0 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 sqlite3Get4byte(
75cd0 63 6f 6e 73 74 20 75 38 2a 29 3b 0a 53 51 4c 49 const u8*);.SQLI
75ce0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
75cf0 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 sqlite3Put4byte(
75d00 75 38 2a 2c 20 75 33 32 29 3b 0a 0a 23 69 66 64 u8*, u32);..#ifd
75d10 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
75d20 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 53 _UNLOCK_NOTIFY.S
75d30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
75d40 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e void sqlite3Conn
75d50 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 73 71 ectionBlocked(sq
75d60 6c 69 74 65 33 20 2a 2c 20 73 71 6c 69 74 65 33 lite3 *, sqlite3
75d70 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
75d80 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
75d90 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f e3ConnectionUnlo
75da0 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 cked(sqlite3 *db
75db0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
75dc0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
75dd0 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 ConnectionClosed
75de0 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23 (sqlite3 *db);.#
75df0 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 else. #define s
75e00 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e qlite3Connection
75e10 42 6c 6f 63 6b 65 64 28 78 2c 79 29 0a 20 20 23 Blocked(x,y). #
75e20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f define sqlite3Co
75e30 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 nnectionUnlocked
75e40 28 78 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 (x). #define sq
75e50 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 lite3ConnectionC
75e60 6c 6f 73 65 64 28 78 29 0a 23 65 6e 64 69 66 0a losed(x).#endif.
75e70 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
75e80 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 EBUG.SQLITE_PRIV
75e90 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
75ea0 65 33 50 61 72 73 65 72 54 72 61 63 65 28 46 49 e3ParserTrace(FI
75eb0 4c 45 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 LE*, char *);.#e
75ec0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 ndif../*.** If t
75ed0 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 he SQLITE_ENABLE
75ee0 20 49 4f 54 52 41 43 45 20 65 78 69 73 74 73 20 IOTRACE exists
75ef0 74 68 65 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 then the global
75f00 76 61 72 69 61 62 6c 65 0a 2a 2a 20 73 71 6c 69 variable.** sqli
75f10 74 65 33 49 6f 54 72 61 63 65 20 69 73 20 61 20 te3IoTrace is a
75f20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 72 69 pointer to a pri
75f30 6e 74 66 2d 6c 69 6b 65 20 72 6f 75 74 69 6e 65 ntf-like routine
75f40 20 75 73 65 64 20 74 6f 0a 2a 2a 20 70 72 69 6e used to.** prin
75f50 74 20 49 2f 4f 20 74 72 61 63 69 6e 67 20 6d 65 t I/O tracing me
75f60 73 73 61 67 65 73 2e 20 0a 2a 2f 0a 23 69 66 64 ssages. .*/.#ifd
75f70 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
75f80 5f 49 4f 54 52 41 43 45 0a 23 20 64 65 66 69 6e _IOTRACE.# defin
75f90 65 20 49 4f 54 52 41 43 45 28 41 29 20 20 69 66 e IOTRACE(A) if
75fa0 28 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 ( sqlite3IoTrace
75fb0 20 29 7b 20 73 71 6c 69 74 65 33 49 6f 54 72 61 ){ sqlite3IoTra
75fc0 63 65 20 41 3b 20 7d 0a 53 51 4c 49 54 45 5f 50 ce A; }.SQLITE_P
75fd0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
75fe0 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 lite3VdbeIOTrace
75ff0 53 71 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 Sql(Vdbe*);.SQLI
76000 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
76010 28 2a 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 (*sqlite3IoTrace
76020 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e )(const char*,..
76030 2e 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 .);.#else.# defi
76040 6e 65 20 49 4f 54 52 41 43 45 28 41 29 0a 23 20 ne IOTRACE(A).#
76050 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64 define sqlite3Vd
76060 62 65 49 4f 54 72 61 63 65 53 71 6c 28 58 29 0a beIOTraceSql(X).
76070 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a #endif..#endif..
76080 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
76090 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 End of sqliteInt
760a0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
760b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
760c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
760d0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
760e0 42 65 67 69 6e 20 66 69 6c 65 20 67 6c 6f 62 61 Begin file globa
760f0 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a l.c ************
76100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
76120 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 /*.** 2008 June
76130 31 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 13.**.** The aut
76140 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
76150 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
76160 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
76170 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
76180 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
76190 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
761a0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
761b0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
761c0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
761d0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
761e0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
761f0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
76200 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
76210 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
76220 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
76230 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
76240 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
76250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76290 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
762a0 65 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e e contains defin
762b0 69 74 69 6f 6e 73 20 6f 66 20 67 6c 6f 62 61 6c itions of global
762c0 20 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20 63 variables and c
762d0 6f 6e 74 61 6e 74 73 2e 0a 2a 2f 0a 0a 0a 2f 2a ontants..*/.../*
762e0 20 41 6e 20 61 72 72 61 79 20 74 6f 20 6d 61 70 An array to map
762f0 20 61 6c 6c 20 75 70 70 65 72 2d 63 61 73 65 20 all upper-case
76300 63 68 61 72 61 63 74 65 72 73 20 69 6e 74 6f 20 characters into
76310 74 68 65 69 72 20 63 6f 72 72 65 73 70 6f 6e 64 their correspond
76320 69 6e 67 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61 73 ing.** lower-cas
76330 65 20 63 68 61 72 61 63 74 65 72 2e 20 0a 2a 2a e character. .**
76340 0a 2a 2a 20 53 51 4c 69 74 65 20 6f 6e 6c 79 20 .** SQLite only
76350 63 6f 6e 73 69 64 65 72 73 20 55 53 2d 41 53 43 considers US-ASC
76360 49 49 20 28 6f 72 20 45 42 43 44 49 43 29 20 63 II (or EBCDIC) c
76370 68 61 72 61 63 74 65 72 73 2e 20 20 57 65 20 64 haracters. We d
76380 6f 20 6e 6f 74 0a 2a 2a 20 68 61 6e 64 6c 65 20 o not.** handle
76390 63 61 73 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 case conversions
763a0 20 66 6f 72 20 74 68 65 20 55 54 46 20 63 68 61 for the UTF cha
763b0 72 61 63 74 65 72 20 73 65 74 20 73 69 6e 63 65 racter set since
763c0 20 74 68 65 20 74 61 62 6c 65 73 0a 2a 2a 20 69 the tables.** i
763d0 6e 76 6f 6c 76 65 64 20 61 72 65 20 6e 65 61 72 nvolved are near
763e0 6c 79 20 61 73 20 62 69 67 20 6f 72 20 62 69 67 ly as big or big
763f0 67 65 72 20 74 68 61 6e 20 53 51 4c 69 74 65 20 ger than SQLite
76400 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54 itself..*/.SQLIT
76410 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
76420 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 unsigned char sq
76430 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 lite3UpperToLowe
76440 72 5b 5d 20 3d 20 7b 0a 23 69 66 64 65 66 20 53 r[] = {.#ifdef S
76450 51 4c 49 54 45 5f 41 53 43 49 49 0a 20 20 20 20 QLITE_ASCII.
76460 20 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c 0, 1, 2, 3,
76470 20 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c 4, 5, 6, 7,
76480 20 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c 8, 9, 10, 11,
76490 20 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c 12, 13, 14, 15,
764a0 20 31 36 2c 20 31 37 2c 0a 20 20 20 20 20 31 38 16, 17,. 18
764b0 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 , 19, 20, 21, 22
764c0 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 , 23, 24, 25, 26
764d0 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 , 27, 28, 29, 30
764e0 2c 20 33 31 2c 20 33 32 2c 20 33 33 2c 20 33 34 , 31, 32, 33, 34
764f0 2c 20 33 35 2c 0a 20 20 20 20 20 33 36 2c 20 33 , 35,. 36, 3
76500 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c 20 34 7, 38, 39, 40, 4
76510 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c 20 34 1, 42, 43, 44, 4
76520 35 2c 20 34 36 2c 20 34 37 2c 20 34 38 2c 20 34 5, 46, 47, 48, 4
76530 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 35 9, 50, 51, 52, 5
76540 33 2c 0a 20 20 20 20 20 35 34 2c 20 35 35 2c 20 3,. 54, 55,
76550 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c 20 56, 57, 58, 59,
76560 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c 20 60, 61, 62, 63,
76570 36 34 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 64, 97, 98, 99,1
76580 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 0a 00,101,102,103,.
76590 20 20 20 20 31 30 34 2c 31 30 35 2c 31 30 36 2c 104,105,106,
765a0 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 107,108,109,110,
765b0 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 111,112,113,114,
765c0 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 115,116,117,118,
765d0 31 31 39 2c 31 32 30 2c 31 32 31 2c 0a 20 20 20 119,120,121,.
765e0 20 31 32 32 2c 20 39 31 2c 20 39 32 2c 20 39 33 122, 91, 92, 93
765f0 2c 20 39 34 2c 20 39 35 2c 20 39 36 2c 20 39 37 , 94, 95, 96, 97
76600 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 , 98, 99,100,101
76610 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30 35 ,102,103,104,105
76620 2c 31 30 36 2c 31 30 37 2c 0a 20 20 20 20 31 30 ,106,107,. 10
76630 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 8,109,110,111,11
76640 32 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 31 2,113,114,115,11
76650 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 6,117,118,119,12
76660 30 2c 31 32 31 2c 31 32 32 2c 31 32 33 2c 31 32 0,121,122,123,12
76670 34 2c 31 32 35 2c 0a 20 20 20 20 31 32 36 2c 31 4,125,. 126,1
76680 32 37 2c 31 32 38 2c 31 32 39 2c 31 33 30 2c 31 27,128,129,130,1
76690 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 31,132,133,134,1
766a0 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c 31 35,136,137,138,1
766b0 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 39,140,141,142,1
766c0 34 33 2c 0a 20 20 20 20 31 34 34 2c 31 34 35 2c 43,. 144,145,
766d0 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 146,147,148,149,
766e0 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 150,151,152,153,
766f0 31 35 34 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c 154,155,156,157,
76700 31 35 38 2c 31 35 39 2c 31 36 30 2c 31 36 31 2c 158,159,160,161,
76710 0a 20 20 20 20 31 36 32 2c 31 36 33 2c 31 36 34 . 162,163,164
76720 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 ,165,166,167,168
76730 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c 31 37 32 ,169,170,171,172
76740 2c 31 37 33 2c 31 37 34 2c 31 37 35 2c 31 37 36 ,173,174,175,176
76750 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 0a 20 20 ,177,178,179,.
76760 20 20 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 180,181,182,18
76770 33 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 3,184,185,186,18
76780 37 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 7,188,189,190,19
76790 31 2c 31 39 32 2c 31 39 33 2c 31 39 34 2c 31 39 1,192,193,194,19
767a0 35 2c 31 39 36 2c 31 39 37 2c 0a 20 20 20 20 31 5,196,197,. 1
767b0 39 38 2c 31 39 39 2c 32 30 30 2c 32 30 31 2c 32 98,199,200,201,2
767c0 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 02,203,204,205,2
767d0 30 36 2c 32 30 37 2c 32 30 38 2c 32 30 39 2c 32 06,207,208,209,2
767e0 31 30 2c 32 31 31 2c 32 31 32 2c 32 31 33 2c 32 10,211,212,213,2
767f0 31 34 2c 32 31 35 2c 0a 20 20 20 20 32 31 36 2c 14,215,. 216,
76800 32 31 37 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c 217,218,219,220,
76810 32 32 31 2c 32 32 32 2c 32 32 33 2c 32 32 34 2c 221,222,223,224,
76820 32 32 35 2c 32 32 36 2c 32 32 37 2c 32 32 38 2c 225,226,227,228,
76830 32 32 39 2c 32 33 30 2c 32 33 31 2c 32 33 32 2c 229,230,231,232,
76840 32 33 33 2c 0a 20 20 20 20 32 33 34 2c 32 33 35 233,. 234,235
76850 2c 32 33 36 2c 32 33 37 2c 32 33 38 2c 32 33 39 ,236,237,238,239
76860 2c 32 34 30 2c 32 34 31 2c 32 34 32 2c 32 34 33 ,240,241,242,243
76870 2c 32 34 34 2c 32 34 35 2c 32 34 36 2c 32 34 37 ,244,245,246,247
76880 2c 32 34 38 2c 32 34 39 2c 32 35 30 2c 32 35 31 ,248,249,250,251
76890 2c 0a 20 20 20 20 32 35 32 2c 32 35 33 2c 32 35 ,. 252,253,25
768a0 34 2c 32 35 35 0a 23 65 6e 64 69 66 0a 23 69 66 4,255.#endif.#if
768b0 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 def SQLITE_EBCDI
768c0 43 0a 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20 C. 0, 1,
768d0 32 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c 20 20 2, 3, 4, 5,
768e0 36 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c 20 31 6, 7, 8, 9, 1
768f0 30 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c 20 31 0, 11, 12, 13, 1
76900 34 2c 20 31 35 2c 20 2f 2a 20 30 78 20 2a 2f 0a 4, 15, /* 0x */.
76910 20 20 20 20 20 31 36 2c 20 31 37 2c 20 31 38 2c 16, 17, 18,
76920 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c 19, 20, 21, 22,
76930 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c 23, 24, 25, 26,
76940 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c 27, 28, 29, 30,
76950 20 33 31 2c 20 2f 2a 20 31 78 20 2a 2f 0a 20 20 31, /* 1x */.
76960 20 20 20 33 32 2c 20 33 33 2c 20 33 34 2c 20 33 32, 33, 34, 3
76970 35 2c 20 33 36 2c 20 33 37 2c 20 33 38 2c 20 33 5, 36, 37, 38, 3
76980 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c 20 34 9, 40, 41, 42, 4
76990 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c 20 34 3, 44, 45, 46, 4
769a0 37 2c 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 20 7, /* 2x */.
769b0 20 34 38 2c 20 34 39 2c 20 35 30 2c 20 35 31 2c 48, 49, 50, 51,
769c0 20 35 32 2c 20 35 33 2c 20 35 34 2c 20 35 35 2c 52, 53, 54, 55,
769d0 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c 56, 57, 58, 59,
769e0 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c 60, 61, 62, 63,
769f0 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20 20 36 /* 3x */. 6
76a00 34 2c 20 36 35 2c 20 36 36 2c 20 36 37 2c 20 36 4, 65, 66, 67, 6
76a10 38 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c 20 37 8, 69, 70, 71, 7
76a20 32 2c 20 37 33 2c 20 37 34 2c 20 37 35 2c 20 37 2, 73, 74, 75, 7
76a30 36 2c 20 37 37 2c 20 37 38 2c 20 37 39 2c 20 2f 6, 77, 78, 79, /
76a40 2a 20 34 78 20 2a 2f 0a 20 20 20 20 20 38 30 2c * 4x */. 80,
76a50 20 38 31 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c 81, 82, 83, 84,
76a60 20 38 35 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c 85, 86, 87, 88,
76a70 20 38 39 2c 20 39 30 2c 20 39 31 2c 20 39 32 2c 89, 90, 91, 92,
76a80 20 39 33 2c 20 39 34 2c 20 39 35 2c 20 2f 2a 20 93, 94, 95, /*
76a90 35 78 20 2a 2f 0a 20 20 20 20 20 39 36 2c 20 39 5x */. 96, 9
76aa0 37 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c 20 36 7, 66, 67, 68, 6
76ab0 39 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c 20 37 9, 70, 71, 72, 7
76ac0 33 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c 31 30 3,106,107,108,10
76ad0 39 2c 31 31 30 2c 31 31 31 2c 20 2f 2a 20 36 78 9,110,111, /* 6x
76ae0 20 2a 2f 0a 20 20 20 20 31 31 32 2c 20 38 31 2c */. 112, 81,
76af0 20 38 32 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c 82, 83, 84, 85,
76b00 20 38 36 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c 86, 87, 88, 89,
76b10 31 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 2c 122,123,124,125,
76b20 31 32 36 2c 31 32 37 2c 20 2f 2a 20 37 78 20 2a 126,127, /* 7x *
76b30 2f 0a 20 20 20 20 31 32 38 2c 31 32 39 2c 31 33 /. 128,129,13
76b40 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 0,131,132,133,13
76b50 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 4,135,136,137,13
76b60 38 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 8,139,140,141,14
76b70 32 2c 31 34 33 2c 20 2f 2a 20 38 78 20 2a 2f 0a 2,143, /* 8x */.
76b80 20 20 20 20 31 34 34 2c 31 34 35 2c 31 34 36 2c 144,145,146,
76b90 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 147,148,149,150,
76ba0 31 35 31 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c 151,152,153,154,
76bb0 31 35 35 2c 31 35 36 2c 31 35 37 2c 31 35 36 2c 155,156,157,156,
76bc0 31 35 39 2c 20 2f 2a 20 39 78 20 2a 2f 0a 20 20 159, /* 9x */.
76bd0 20 20 31 36 30 2c 31 36 31 2c 31 36 32 2c 31 36 160,161,162,16
76be0 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 3,164,165,166,16
76bf0 37 2c 31 36 38 2c 31 36 39 2c 31 37 30 2c 31 37 7,168,169,170,17
76c00 31 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 37 1,140,141,142,17
76c10 35 2c 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 20 5, /* Ax */.
76c20 31 37 36 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 176,177,178,179,
76c30 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c 180,181,182,183,
76c40 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c 184,185,186,187,
76c50 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c 188,189,190,191,
76c60 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 20 31 39 /* Bx */. 19
76c70 32 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 2,129,130,131,13
76c80 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 2,133,134,135,13
76c90 36 2c 31 33 37 2c 32 30 32 2c 32 30 33 2c 32 30 6,137,202,203,20
76ca0 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f 4,205,206,207, /
76cb0 2a 20 43 78 20 2a 2f 0a 20 20 20 20 32 30 38 2c * Cx */. 208,
76cc0 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 145,146,147,148,
76cd0 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 149,150,151,152,
76ce0 31 35 33 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c 153,218,219,220,
76cf0 32 32 31 2c 32 32 32 2c 32 32 33 2c 20 2f 2a 20 221,222,223, /*
76d00 44 78 20 2a 2f 0a 20 20 20 20 32 32 34 2c 32 32 Dx */. 224,22
76d10 35 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 5,162,163,164,16
76d20 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 5,166,167,168,16
76d30 39 2c 32 33 32 2c 32 30 33 2c 32 30 34 2c 32 30 9,232,203,204,20
76d40 35 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 45 78 5,206,207, /* Ex
76d50 20 2a 2f 0a 20 20 20 20 32 33 39 2c 32 34 30 2c */. 239,240,
76d60 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 241,242,243,244,
76d70 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 245,246,247,248,
76d80 32 34 39 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 249,219,220,221,
76d90 32 32 32 2c 32 35 35 2c 20 2f 2a 20 46 78 20 2a 222,255, /* Fx *
76da0 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a /.#endif.};../*.
76db0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
76dc0 20 32 35 36 20 62 79 74 65 20 6c 6f 6f 6b 75 70 256 byte lookup
76dd0 20 74 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 table is used t
76de0 6f 20 73 75 70 70 6f 72 74 20 53 51 4c 69 74 65 o support SQLite
76df0 73 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 65 71 s built-in.** eq
76e00 75 69 76 61 6c 65 6e 74 73 20 74 6f 20 74 68 65 uivalents to the
76e10 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 6e 64 following stand
76e20 61 72 64 20 6c 69 62 72 61 72 79 20 66 75 6e 63 ard library func
76e30 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 tions:.**.** i
76e40 73 73 70 61 63 65 28 29 20 20 20 20 20 20 20 20 sspace()
76e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76e60 30 78 30 31 0a 2a 2a 20 20 20 69 73 61 6c 70 68 0x01.** isalph
76e70 61 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20 a()
76e80 20 20 20 20 20 20 20 20 20 20 20 30 78 30 32 0a 0x02.
76e90 2a 2a 20 20 20 69 73 64 69 67 69 74 28 29 20 20 ** isdigit()
76ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76eb0 20 20 20 20 20 20 30 78 30 34 0a 2a 2a 20 20 20 0x04.**
76ec0 69 73 61 6c 6e 75 6d 28 29 20 20 20 20 20 20 20 isalnum()
76ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76ee0 20 30 78 30 36 0a 2a 2a 20 20 20 69 73 78 64 69 0x06.** isxdi
76ef0 67 69 74 28 29 20 20 20 20 20 20 20 20 20 20 20 git()
76f00 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 38 0x08
76f10 0a 2a 2a 20 20 20 74 6f 75 70 70 65 72 28 29 20 .** toupper()
76f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76f30 20 20 20 20 20 20 20 30 78 32 30 0a 2a 2a 0a 2a 0x20.**.*
76f40 2a 20 42 69 74 20 30 78 32 30 20 69 73 20 73 65 * Bit 0x20 is se
76f50 74 20 69 66 20 74 68 65 20 6d 61 70 70 65 64 20 t if the mapped
76f60 63 68 61 72 61 63 74 65 72 20 72 65 71 75 69 72 character requir
76f70 65 73 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74 es translation t
76f80 6f 20 75 70 70 65 72 0a 2a 2a 20 63 61 73 65 2e o upper.** case.
76f90 20 69 2e 65 2e 20 69 66 20 74 68 65 20 63 68 61 i.e. if the cha
76fa0 72 61 63 74 65 72 20 69 73 20 61 20 6c 6f 77 65 racter is a lowe
76fb0 72 2d 63 61 73 65 20 41 53 43 49 49 20 63 68 61 r-case ASCII cha
76fc0 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 78 20 racter..** If x
76fd0 69 73 20 61 20 6c 6f 77 65 72 2d 63 61 73 65 20 is a lower-case
76fe0 41 53 43 49 49 20 63 68 61 72 61 63 74 65 72 2c ASCII character,
76ff0 20 74 68 65 6e 20 69 74 73 20 75 70 70 65 72 2d then its upper-
77000 63 61 73 65 20 65 71 75 69 76 61 6c 65 6e 74 0a case equivalent.
77010 2a 2a 20 69 73 20 28 78 20 2d 20 30 78 32 30 29 ** is (x - 0x20)
77020 2e 20 54 68 65 72 65 66 6f 72 65 20 74 6f 75 70 . Therefore toup
77030 70 65 72 28 29 20 63 61 6e 20 62 65 20 69 6d 70 per() can be imp
77040 6c 65 6d 65 6e 74 65 64 20 61 73 3a 0a 2a 2a 0a lemented as:.**.
77050 2a 2a 20 20 20 28 78 20 26 20 7e 28 6d 61 70 5b ** (x & ~(map[
77060 78 5d 26 30 78 32 30 29 29 0a 2a 2a 0a 2a 2a 20 x]&0x20)).**.**
77070 53 74 61 6e 64 61 72 64 20 66 75 6e 63 74 69 6f Standard functio
77080 6e 20 74 6f 6c 6f 77 65 72 28 29 20 69 73 20 69 n tolower() is i
77090 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 mplemented using
770a0 20 74 68 65 20 73 71 6c 69 74 65 33 55 70 70 65 the sqlite3Uppe
770b0 72 54 6f 4c 6f 77 65 72 5b 5d 0a 2a 2a 20 61 72 rToLower[].** ar
770c0 72 61 79 2e 20 74 6f 6c 6f 77 65 72 28 29 20 69 ray. tolower() i
770d0 73 20 75 73 65 64 20 6d 6f 72 65 20 6f 66 74 65 s used more ofte
770e0 6e 20 74 68 61 6e 20 74 6f 75 70 70 65 72 28 29 n than toupper()
770f0 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a by SQLite..**.*
77100 2a 20 53 51 4c 69 74 65 27 73 20 76 65 72 73 69 * SQLite's versi
77110 6f 6e 73 20 61 72 65 20 69 64 65 6e 74 69 63 61 ons are identica
77120 6c 20 74 6f 20 74 68 65 20 73 74 61 6e 64 61 72 l to the standar
77130 64 20 76 65 72 73 69 6f 6e 73 20 61 73 73 75 6d d versions assum
77140 69 6e 67 20 61 0a 2a 2a 20 6c 6f 63 61 6c 65 20 ing a.** locale
77150 6f 66 20 22 43 22 2e 20 54 68 65 79 20 61 72 65 of "C". They are
77160 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 implemented as
77170 6d 61 63 72 6f 73 20 69 6e 20 73 71 6c 69 74 65 macros in sqlite
77180 49 6e 74 2e 68 2e 0a 2a 2f 0a 23 69 66 64 65 66 Int.h..*/.#ifdef
77190 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 53 51 SQLITE_ASCII.SQ
771a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
771b0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
771c0 20 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 sqlite3CtypeMap
771d0 5b 32 35 36 5d 20 3d 20 7b 0a 20 20 30 78 30 30 [256] = {. 0x00
771e0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
771f0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77200 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 0x00, 0x00, /*
77210 30 30 2e 2e 30 37 20 20 20 20 2e 2e 2e 2e 2e 2e 00..07 ......
77220 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 .. */. 0x00, 0x
77230 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20 01, 0x01, 0x01,
77240 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 30 0x01, 0x01, 0x00
77250 2c 20 30 78 30 30 2c 20 20 2f 2a 20 30 38 2e 2e , 0x00, /* 08..
77260 30 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 0f ........ *
77270 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 /. 0x00, 0x00,
77280 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
77290 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
772a0 30 30 2c 20 20 2f 2a 20 31 30 2e 2e 31 37 20 20 00, /* 10..17
772b0 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 ........ */.
772c0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
772d0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
772e0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
772f0 20 2f 2a 20 31 38 2e 2e 31 66 20 20 20 20 2e 2e /* 18..1f ..
77300 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 31 ...... */. 0x01
77310 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77320 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77330 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 0x00, 0x00, /*
77340 32 30 2e 2e 32 37 20 20 20 20 20 21 22 23 24 25 20..27 !"#$%
77350 26 27 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 &' */. 0x00, 0x
77360 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77370 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
77380 2c 20 30 78 30 30 2c 20 20 2f 2a 20 32 38 2e 2e , 0x00, /* 28..
77390 32 66 20 20 20 20 28 29 2a 2b 2c 2d 2e 2f 20 2a 2f ()*+,-./ *
773a0 2f 0a 20 20 30 78 30 63 2c 20 30 78 30 63 2c 20 /. 0x0c, 0x0c,
773b0 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 0x0c, 0x0c, 0x0c
773c0 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 , 0x0c, 0x0c, 0x
773d0 30 63 2c 20 20 2f 2a 20 33 30 2e 2e 33 37 20 20 0c, /* 30..37
773e0 20 20 30 31 32 33 34 35 36 37 20 2a 2f 0a 20 20 01234567 */.
773f0 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 30 0x0c, 0x0c, 0x00
77400 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77410 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77420 20 2f 2a 20 33 38 2e 2e 33 66 20 20 20 20 38 39 /* 38..3f 89
77430 3a 3b 3c 3d 3e 3f 20 2a 2f 0a 0a 20 20 30 78 30 :;<=>? */.. 0x0
77440 30 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30 0, 0x0a, 0x0a, 0
77450 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c x0a, 0x0a, 0x0a,
77460 20 30 78 30 61 2c 20 30 78 30 32 2c 20 20 2f 2a 0x0a, 0x02, /*
77470 20 34 30 2e 2e 34 37 20 20 20 20 40 41 42 43 44 40..47 @ABCD
77480 45 46 47 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30 EFG */. 0x02, 0
77490 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c x02, 0x02, 0x02,
774a0 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 0x02, 0x02, 0x0
774b0 32 2c 20 30 78 30 32 2c 20 20 2f 2a 20 34 38 2e 2, 0x02, /* 48.
774c0 2e 34 66 20 20 20 20 48 49 4a 4b 4c 4d 4e 4f 20 .4f HIJKLMNO
774d0 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32 2c */. 0x02, 0x02,
774e0 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 0x02, 0x02, 0x0
774f0 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 2, 0x02, 0x02, 0
77500 78 30 32 2c 20 20 2f 2a 20 35 30 2e 2e 35 37 20 x02, /* 50..57
77510 20 20 20 50 51 52 53 54 55 56 57 20 2a 2f 0a 20 PQRSTUVW */.
77520 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 0x02, 0x02, 0x0
77530 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 2, 0x00, 0x00, 0
77540 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77550 20 20 2f 2a 20 35 38 2e 2e 35 66 20 20 20 20 58 /* 58..5f X
77560 59 5a 5b 5c 5d 5e 5f 20 2a 2f 0a 20 20 30 78 30 YZ[\]^_ */. 0x0
77570 30 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30 0, 0x2a, 0x2a, 0
77580 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c x2a, 0x2a, 0x2a,
77590 20 30 78 32 61 2c 20 30 78 32 32 2c 20 20 2f 2a 0x2a, 0x22, /*
775a0 20 36 30 2e 2e 36 37 20 20 20 20 60 61 62 63 64 60..67 `abcd
775b0 65 66 67 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30 efg */. 0x22, 0
775c0 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c x22, 0x22, 0x22,
775d0 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 0x22, 0x22, 0x2
775e0 32 2c 20 30 78 32 32 2c 20 20 2f 2a 20 36 38 2e 2, 0x22, /* 68.
775f0 2e 36 66 20 20 20 20 68 69 6a 6b 6c 6d 6e 6f 20 .6f hijklmno
77600 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32 2c */. 0x22, 0x22,
77610 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 0x22, 0x22, 0x2
77620 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 2, 0x22, 0x22, 0
77630 78 32 32 2c 20 20 2f 2a 20 37 30 2e 2e 37 37 20 x22, /* 70..77
77640 20 20 20 70 71 72 73 74 75 76 77 20 2a 2f 0a 20 pqrstuvw */.
77650 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 0x22, 0x22, 0x2
77660 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 2, 0x00, 0x00, 0
77670 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77680 20 20 2f 2a 20 37 38 2e 2e 37 66 20 20 20 20 78 /* 78..7f x
77690 79 7a 7b 7c 7d 7e 2e 20 2a 2f 0a 0a 20 20 30 78 yz{|}~. */.. 0x
776a0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
776b0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
776c0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f , 0x00, 0x00, /
776d0 2a 20 38 30 2e 2e 38 37 20 20 20 20 2e 2e 2e 2e * 80..87 ....
776e0 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 .... */. 0x00,
776f0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
77700 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77710 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 38 38 00, 0x00, /* 88
77720 2e 2e 38 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e ..8f ........
77730 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 */. 0x00, 0x00
77740 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77750 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77760 30 78 30 30 2c 20 20 2f 2a 20 39 30 2e 2e 39 37 0x00, /* 90..97
77770 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a ........ */.
77780 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 0x00, 0x00, 0x
77790 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
777a0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
777b0 2c 20 20 2f 2a 20 39 38 2e 2e 39 66 20 20 20 20 , /* 98..9f
777c0 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 ........ */. 0x
777d0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
777e0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
777f0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f , 0x00, 0x00, /
77800 2a 20 61 30 2e 2e 61 37 20 20 20 20 2e 2e 2e 2e * a0..a7 ....
77810 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 .... */. 0x00,
77820 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
77830 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77840 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 61 38 00, 0x00, /* a8
77850 2e 2e 61 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e ..af ........
77860 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 */. 0x00, 0x00
77870 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77880 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77890 30 78 30 30 2c 20 20 2f 2a 20 62 30 2e 2e 62 37 0x00, /* b0..b7
778a0 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a ........ */.
778b0 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 0x00, 0x00, 0x
778c0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
778d0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
778e0 2c 20 20 2f 2a 20 62 38 2e 2e 62 66 20 20 20 20 , /* b8..bf
778f0 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 0a 20 20 30 ........ */.. 0
77900 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77910 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77920 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 0, 0x00, 0x00,
77930 2f 2a 20 63 30 2e 2e 63 37 20 20 20 20 2e 2e 2e /* c0..c7 ...
77940 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c ..... */. 0x00,
77950 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77960 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77970 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 63 x00, 0x00, /* c
77980 38 2e 2e 63 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 8..cf .......
77990 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 . */. 0x00, 0x0
779a0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
779b0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
779c0 20 30 78 30 30 2c 20 20 2f 2a 20 64 30 2e 2e 64 0x00, /* d0..d
779d0 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 7 ........ */
779e0 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 . 0x00, 0x00, 0
779f0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77a00 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77a10 30 2c 20 20 2f 2a 20 64 38 2e 2e 64 66 20 20 20 0, /* d8..df
77a20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 ........ */. 0
77a30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77a40 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77a50 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 0, 0x00, 0x00,
77a60 2f 2a 20 65 30 2e 2e 65 37 20 20 20 20 2e 2e 2e /* e0..e7 ...
77a70 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c ..... */. 0x00,
77a80 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77a90 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77aa0 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 65 x00, 0x00, /* e
77ab0 38 2e 2e 65 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 8..ef .......
77ac0 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 . */. 0x00, 0x0
77ad0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77ae0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77af0 20 30 78 30 30 2c 20 20 2f 2a 20 66 30 2e 2e 66 0x00, /* f0..f
77b00 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 7 ........ */
77b10 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 . 0x00, 0x00, 0
77b20 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77b30 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77b40 30 20 20 20 2f 2a 20 66 38 2e 2e 66 66 20 20 20 0 /* f8..ff
77b50 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 7d 3b 0a ........ */.};.
77b60 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 #endif..../*.**
77b70 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 69 The following si
77b80 6e 67 6c 65 74 6f 6e 20 63 6f 6e 74 61 69 6e 73 ngleton contains
77b90 20 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 the global conf
77ba0 69 67 75 72 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a iguration for.**
77bb0 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 the SQLite libr
77bc0 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ary..*/.SQLITE_P
77bd0 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 RIVATE SQLITE_WS
77be0 44 20 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 D struct Sqlite3
77bf0 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f Config sqlite3Co
77c00 6e 66 69 67 20 3d 20 7b 0a 20 20 20 53 51 4c 49 nfig = {. SQLI
77c10 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 TE_DEFAULT_MEMST
77c20 41 54 55 53 2c 20 20 2f 2a 20 62 4d 65 6d 73 74 ATUS, /* bMemst
77c30 61 74 20 2a 2f 0a 20 20 20 31 2c 20 20 20 20 20 at */. 1,
77c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77c50 20 20 20 20 2f 2a 20 62 43 6f 72 65 4d 75 74 65 /* bCoreMute
77c60 78 20 2a 2f 0a 20 20 20 53 51 4c 49 54 45 5f 54 x */. SQLITE_T
77c70 48 52 45 41 44 53 41 46 45 3d 3d 31 2c 20 20 20 HREADSAFE==1,
77c80 20 20 20 2f 2a 20 62 46 75 6c 6c 4d 75 74 65 78 /* bFullMutex
77c90 20 2a 2f 0a 20 20 20 30 78 37 66 66 66 66 66 66 */. 0x7ffffff
77ca0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
77cb0 20 20 2f 2a 20 6d 78 53 74 72 6c 65 6e 20 2a 2f /* mxStrlen */
77cc0 0a 20 20 20 31 30 30 2c 20 20 20 20 20 20 20 20 . 100,
77cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
77ce0 2a 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f * szLookaside */
77cf0 0a 20 20 20 35 30 30 2c 20 20 20 20 20 20 20 20 . 500,
77d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
77d10 2a 20 6e 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a * nLookaside */.
77d20 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c {0,0,0,0,0,0,
77d30 30 2c 30 7d 2c 20 20 20 20 20 20 20 20 20 2f 2a 0,0}, /*
77d40 20 6d 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c m */. {0,0,0,
77d50 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 0,0,0,0,0,0},
77d60 20 20 20 20 2f 2a 20 6d 75 74 65 78 20 2a 2f 0a /* mutex */.
77d70 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c {0,0,0,0,0,0,
77d80 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 2f 2a 0,0,0,0,0}, /*
77d90 20 70 63 61 63 68 65 20 2a 2f 0a 20 20 20 28 76 pcache */. (v
77da0 6f 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 oid*)0,
77db0 20 20 20 20 20 20 20 20 20 2f 2a 20 70 48 65 61 /* pHea
77dc0 70 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 p */. 0,
77dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77de0 20 20 20 2f 2a 20 6e 48 65 61 70 20 2a 2f 0a 20 /* nHeap */.
77df0 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 20 0, 0,
77e00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
77e10 6d 6e 48 65 61 70 2c 20 6d 78 48 65 61 70 20 2a mnHeap, mxHeap *
77e20 2f 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 /. (void*)0,
77e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77e40 2f 2a 20 70 53 63 72 61 74 63 68 20 2a 2f 0a 20 /* pScratch */.
77e50 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
77e60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
77e70 73 7a 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 szScratch */.
77e80 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
77e90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 53 /* nS
77ea0 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 28 76 6f cratch */. (vo
77eb0 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 id*)0,
77ec0 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 /* pPage
77ed0 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
77ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77ef0 20 20 2f 2a 20 73 7a 50 61 67 65 20 2a 2f 0a 20 /* szPage */.
77f00 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
77f10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
77f20 6e 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20 nPage */. 0,
77f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77f40 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 72 73 /* mxPars
77f50 65 72 53 74 61 63 6b 20 2a 2f 0a 20 20 20 30 2c erStack */. 0,
77f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77f70 20 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61 72 /* shar
77f80 65 64 43 61 63 68 65 45 6e 61 62 6c 65 64 20 2a edCacheEnabled *
77f90 2f 0a 20 20 20 2f 2a 20 41 6c 6c 20 74 68 65 20 /. /* All the
77fa0 72 65 73 74 20 73 68 6f 75 6c 64 20 61 6c 77 61 rest should alwa
77fb0 79 73 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 ys be initialize
77fc0 64 20 74 6f 20 7a 65 72 6f 20 2a 2f 0a 20 20 20 d to zero */.
77fd0 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
77fe0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73 /* is
77ff0 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 Init */. 0,
78000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78010 20 20 20 20 20 20 2f 2a 20 69 6e 50 72 6f 67 72 /* inProgr
78020 65 73 73 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 ess */. 0,
78030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78040 20 20 20 20 20 2f 2a 20 69 73 4d 75 74 65 78 49 /* isMutexI
78050 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 nit */. 0,
78060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78070 20 20 20 20 20 2f 2a 20 69 73 4d 61 6c 6c 6f 63 /* isMalloc
78080 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 Init */. 0,
78090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
780a0 20 20 20 20 20 20 2f 2a 20 69 73 50 43 61 63 68 /* isPCach
780b0 65 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 eInit */. 0,
780c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
780d0 20 20 20 20 20 20 20 2f 2a 20 70 49 6e 69 74 4d /* pInitM
780e0 75 74 65 78 20 2a 2f 0a 20 20 20 30 2c 20 20 20 utex */. 0,
780f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78100 20 20 20 20 20 20 2f 2a 20 6e 52 65 66 49 6e 69 /* nRefIni
78110 74 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 0a 2f tMutex */.};.../
78120 2a 0a 2a 2a 20 48 61 73 68 20 74 61 62 6c 65 20 *.** Hash table
78130 66 6f 72 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 for global funct
78140 69 6f 6e 73 20 2d 20 66 75 6e 63 74 69 6f 6e 73 ions - functions
78150 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a common to all.*
78160 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * database conne
78170 63 74 69 6f 6e 73 2e 20 20 41 66 74 65 72 20 69 ctions. After i
78180 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2c 20 74 nitialization, t
78190 68 69 73 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 his table is.**
781a0 72 65 61 64 2d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 read-only..*/.SQ
781b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c LITE_PRIVATE SQL
781c0 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48 ITE_WSD FuncDefH
781d0 61 73 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 ash sqlite3Globa
781e0 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a 0a 2f 2a 0a lFunctions;../*.
781f0 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 ** The value of
78200 74 68 65 20 22 70 65 6e 64 69 6e 67 22 20 62 79 the "pending" by
78210 74 65 20 6d 75 73 74 20 62 65 20 30 78 34 30 30 te must be 0x400
78220 30 30 30 30 30 20 28 31 20 62 79 74 65 20 70 61 00000 (1 byte pa
78230 73 74 20 74 68 65 0a 2a 2a 20 31 2d 67 69 62 61 st the.** 1-giba
78240 62 79 74 65 20 62 6f 75 6e 64 61 72 79 29 20 69 byte boundary) i
78250 6e 20 61 20 63 6f 6d 70 61 74 69 62 6c 65 20 64 n a compatible d
78260 61 74 61 62 61 73 65 2e 20 20 53 51 4c 69 74 65 atabase. SQLite
78270 20 6e 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 74 never uses.** t
78280 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 he database page
78290 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 that contains t
782a0 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 2e he pending byte.
782b0 20 20 49 74 20 6e 65 76 65 72 20 61 74 74 65 6d It never attem
782c0 70 74 73 0a 2a 2a 20 74 6f 20 72 65 61 64 20 6f pts.** to read o
782d0 72 20 77 72 69 74 65 20 74 68 61 74 20 70 61 67 r write that pag
782e0 65 2e 20 20 54 68 65 20 70 65 6e 64 69 6e 67 20 e. The pending
782f0 62 79 74 65 20 70 61 67 65 20 69 73 20 73 65 74 byte page is set
78300 20 61 73 73 69 67 6e 0a 2a 2a 20 66 6f 72 20 75 assign.** for u
78310 73 65 20 62 79 20 74 68 65 20 56 46 53 20 6c 61 se by the VFS la
78320 79 65 72 73 20 61 73 20 73 70 61 63 65 20 66 6f yers as space fo
78330 72 20 6d 61 6e 61 67 69 6e 67 20 66 69 6c 65 20 r managing file
78340 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 locks..**.** Dur
78350 69 6e 67 20 74 65 73 74 69 6e 67 2c 20 69 74 20 ing testing, it
78360 69 73 20 6f 66 74 65 6e 20 64 65 73 69 72 61 62 is often desirab
78370 6c 65 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 70 le to move the p
78380 65 6e 64 69 6e 67 20 62 79 74 65 20 74 6f 0a 2a ending byte to.*
78390 2a 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 6f * a different po
783a0 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 66 69 sition in the fi
783b0 6c 65 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 le. This allows
783c0 20 63 6f 64 65 20 74 68 61 74 20 68 61 73 20 74 code that has t
783d0 6f 0a 2a 2a 20 64 65 61 6c 20 77 69 74 68 20 74 o.** deal with t
783e0 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 he pending byte
783f0 74 6f 20 72 75 6e 20 6f 6e 20 66 69 6c 65 73 20 to run on files
78400 74 68 61 74 20 61 72 65 20 6d 75 63 68 20 73 6d that are much sm
78410 61 6c 6c 65 72 0a 2a 2a 20 74 68 61 6e 20 31 20 aller.** than 1
78420 47 69 42 2e 20 20 54 68 65 20 73 71 6c 69 74 65 GiB. The sqlite
78430 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 3_test_control()
78440 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 interface can b
78450 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 6d 6f 76 e used to.** mov
78460 65 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 e the pending by
78470 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 4f 52 54 te..**.** IMPORT
78480 41 4e 54 3a 20 20 43 68 61 6e 67 69 6e 67 20 74 ANT: Changing t
78490 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 he pending byte
784a0 74 6f 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 to any value oth
784b0 65 72 20 74 68 61 6e 0a 2a 2a 20 30 78 34 30 30 er than.** 0x400
784c0 30 30 30 30 30 20 72 65 73 75 6c 74 73 20 69 6e 00000 results in
784d0 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 an incompatible
784e0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 database file f
784f0 6f 72 6d 61 74 21 0a 2a 2a 20 43 68 61 6e 67 69 ormat!.** Changi
78500 6e 67 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 ng the pending b
78510 79 74 65 20 64 75 72 69 6e 67 20 6f 70 65 72 61 yte during opera
78520 74 69 6e 67 20 72 65 73 75 6c 74 73 20 69 6e 20 ting results in
78530 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 61 6e 64 undefined.** and
78540 20 64 69 6c 65 74 65 72 69 6f 75 73 20 62 65 68 dileterious beh
78550 61 76 69 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 avior..*/.SQLITE
78560 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
78570 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 20 ite3PendingByte
78580 3d 20 30 78 34 30 30 30 30 30 30 30 3b 0a 0a 2f = 0x40000000;../
78590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
785a0 6e 64 20 6f 66 20 67 6c 6f 62 61 6c 2e 63 20 2a nd of global.c *
785b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
785c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
785d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
785e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
785f0 65 67 69 6e 20 66 69 6c 65 20 73 74 61 74 75 73 egin file status
78600 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
78610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
78630 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 31 *.** 2008 June 1
78640 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 8.**.** The auth
78650 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
78660 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
78670 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
78680 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
78690 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
786a0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
786b0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
786c0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
786d0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
786e0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
786f0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
78700 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
78710 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
78720 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
78730 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
78740 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
78750 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
78760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
787a0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 .**.** This modu
787b0 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 le implements th
787c0 65 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 e sqlite3_status
787d0 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64 () interface and
787e0 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63 related.** func
787f0 74 69 6f 6e 61 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a tionality..**.**
78800 20 24 49 64 3a 20 73 74 61 74 75 73 2e 63 2c 76 $Id: status.c,v
78810 20 31 2e 39 20 32 30 30 38 2f 30 39 2f 30 32 20 1.9 2008/09/02
78820 30 30 3a 35 32 3a 35 32 20 64 72 68 20 45 78 70 00:52:52 drh Exp
78830 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 72 $.*/../*.** Var
78840 69 61 62 6c 65 73 20 69 6e 20 77 68 69 63 68 20 iables in which
78850 74 6f 20 72 65 63 6f 72 64 20 73 74 61 74 75 73 to record status
78860 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f information..*/
78870 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
78880 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 20 sqlite3StatType
78890 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 3b sqlite3StatType;
788a0 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 .static SQLITE_W
788b0 53 44 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 SD struct sqlite
788c0 33 53 74 61 74 54 79 70 65 20 7b 0a 20 20 69 6e 3StatType {. in
788d0 74 20 6e 6f 77 56 61 6c 75 65 5b 39 5d 3b 20 20 t nowValue[9];
788e0 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e /* Curren
788f0 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 t value */. int
78900 20 6d 78 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 mxValue[9];
78910 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d /* Maximum
78920 20 76 61 6c 75 65 20 2a 2f 0a 7d 20 73 71 6c 69 value */.} sqli
78930 74 65 33 53 74 61 74 20 3d 20 7b 20 7b 30 2c 7d te3Stat = { {0,}
78940 2c 20 7b 30 2c 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54 , {0,} };.../* T
78950 68 65 20 22 77 73 64 53 74 61 74 22 20 6d 61 63 he "wsdStat" mac
78960 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 ro will resolve
78970 74 6f 20 74 68 65 20 73 74 61 74 75 73 20 69 6e to the status in
78980 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 74 61 formation.** sta
78990 74 65 20 76 65 63 74 6f 72 2e 20 20 49 66 20 77 te vector. If w
789a0 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 ritable static d
789b0 61 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 ata is unsupport
789c0 65 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 ed on the target
789d0 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 ,.** we have to
789e0 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65 locate the state
789f0 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 vector at run-t
78a00 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 ime. In the mor
78a10 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 e common.** case
78a20 20 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 20 where writable
78a30 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 73 static data is s
78a40 75 70 70 6f 72 74 65 64 2c 20 77 73 64 53 74 61 upported, wsdSta
78a50 74 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 65 t can refer dire
78a60 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 ctly.** to the "
78a70 73 71 6c 69 74 65 33 53 74 61 74 22 20 73 74 61 sqlite3Stat" sta
78a80 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 te vector declar
78a90 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 ed above..*/.#if
78aa0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
78ab0 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77 73 64 WSD.# define wsd
78ac0 53 74 61 74 49 6e 69 74 20 20 73 71 6c 69 74 65 StatInit sqlite
78ad0 33 53 74 61 74 54 79 70 65 20 2a 78 20 3d 20 26 3StatType *x = &
78ae0 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 53 74 GLOBAL(sqlite3St
78af0 61 74 54 79 70 65 2c 73 71 6c 69 74 65 33 53 74 atType,sqlite3St
78b00 61 74 29 0a 23 20 64 65 66 69 6e 65 20 77 73 64 at).# define wsd
78b10 53 74 61 74 20 78 5b 30 5d 0a 23 65 6c 73 65 0a Stat x[0].#else.
78b20 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 # define wsdStat
78b30 49 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 77 73 Init.# define ws
78b40 64 53 74 61 74 20 73 71 6c 69 74 65 33 53 74 61 dStat sqlite3Sta
78b50 74 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 t.#endif../*.**
78b60 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 Return the curre
78b70 6e 74 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74 nt value of a st
78b80 61 74 75 73 20 70 61 72 61 6d 65 74 65 72 2e 0a atus parameter..
78b90 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
78ba0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 61 E int sqlite3Sta
78bb0 74 75 73 56 61 6c 75 65 28 69 6e 74 20 6f 70 29 tusValue(int op)
78bc0 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b {. wsdStatInit;
78bd0 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 . assert( op>=0
78be0 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 && op<ArraySize
78bf0 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 (wsdStat.nowValu
78c00 65 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 77 e) );. return w
78c10 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b sdStat.nowValue[
78c20 6f 70 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 op];.}../*.** Ad
78c30 64 20 4e 20 74 6f 20 74 68 65 20 76 61 6c 75 65 d N to the value
78c40 20 6f 66 20 61 20 73 74 61 74 75 73 20 72 65 63 of a status rec
78c50 6f 72 64 2e 20 20 49 74 20 69 73 20 61 73 73 75 ord. It is assu
78c60 6d 65 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 med that the.**
78c70 63 61 6c 6c 65 72 20 68 6f 6c 64 73 20 61 70 70 caller holds app
78c80 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 73 2e 0a ropriate locks..
78c90 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
78ca0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 E void sqlite3St
78cb0 61 74 75 73 41 64 64 28 69 6e 74 20 6f 70 2c 20 atusAdd(int op,
78cc0 69 6e 74 20 4e 29 7b 0a 20 20 77 73 64 53 74 61 int N){. wsdSta
78cd0 74 49 6e 69 74 3b 0a 20 20 61 73 73 65 72 74 28 tInit;. assert(
78ce0 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 op>=0 && op<Arr
78cf0 61 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e aySize(wsdStat.n
78d00 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20 20 77 73 owValue) );. ws
78d10 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f dStat.nowValue[o
78d20 70 5d 20 2b 3d 20 4e 3b 0a 20 20 69 66 28 20 77 p] += N;. if( w
78d30 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b sdStat.nowValue[
78d40 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d 78 56 61 op]>wsdStat.mxVa
78d50 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 20 20 77 lue[op] ){. w
78d60 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f sdStat.mxValue[o
78d70 70 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 p] = wsdStat.now
78d80 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d Value[op];. }.}
78d90 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
78da0 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 value of a statu
78db0 73 20 74 6f 20 58 2e 0a 2a 2f 0a 53 51 4c 49 54 s to X..*/.SQLIT
78dc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
78dd0 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 qlite3StatusSet(
78de0 69 6e 74 20 6f 70 2c 20 69 6e 74 20 58 29 7b 0a int op, int X){.
78df0 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 wsdStatInit;.
78e00 20 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 assert( op>=0 &
78e10 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77 & op<ArraySize(w
78e20 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 sdStat.nowValue)
78e30 20 29 3b 0a 20 20 77 73 64 53 74 61 74 2e 6e 6f );. wsdStat.no
78e40 77 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 58 3b 0a wValue[op] = X;.
78e50 20 20 69 66 28 20 77 73 64 53 74 61 74 2e 6e 6f if( wsdStat.no
78e60 77 56 61 6c 75 65 5b 6f 70 5d 3e 77 73 64 53 74 wValue[op]>wsdSt
78e70 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 29 at.mxValue[op] )
78e80 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78 {. wsdStat.mx
78e90 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 Value[op] = wsdS
78ea0 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d tat.nowValue[op]
78eb0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 ;. }.}../*.** Q
78ec0 75 65 72 79 20 73 74 61 74 75 73 20 69 6e 66 6f uery status info
78ed0 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 rmation..**.** T
78ee0 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 his implementati
78ef0 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 on assumes that
78f00 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 reading or writi
78f10 6e 67 20 61 6e 20 61 6c 69 67 6e 65 64 0a 2a 2a ng an aligned.**
78f20 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 32-bit integer
78f30 69 73 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 is an atomic ope
78f40 72 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 61 74 ration. If that
78f50 20 61 73 73 75 6d 70 74 69 6f 6e 20 69 73 20 6e assumption is n
78f60 6f 74 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e ot true,.** then
78f70 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
78f80 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e not threadsafe.
78f90 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
78fa0 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 nt sqlite3_statu
78fb0 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 s(int op, int *p
78fc0 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 Current, int *pH
78fd0 69 67 68 77 61 74 65 72 2c 20 69 6e 74 20 72 65 ighwater, int re
78fe0 73 65 74 46 6c 61 67 29 7b 0a 20 20 77 73 64 53 setFlag){. wsdS
78ff0 74 61 74 49 6e 69 74 3b 0a 20 20 69 66 28 20 6f tatInit;. if( o
79000 70 3c 30 20 7c 7c 20 6f 70 3e 3d 41 72 72 61 79 p<0 || op>=Array
79010 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 Size(wsdStat.now
79020 56 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 72 65 Value) ){. re
79030 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 turn SQLITE_MISU
79040 53 45 3b 0a 20 20 7d 0a 20 20 2a 70 43 75 72 72 SE;. }. *pCurr
79050 65 6e 74 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f ent = wsdStat.no
79060 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 2a 70 wValue[op];. *p
79070 48 69 67 68 77 61 74 65 72 20 3d 20 77 73 64 53 Highwater = wsdS
79080 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 3b tat.mxValue[op];
79090 0a 20 20 69 66 28 20 72 65 73 65 74 46 6c 61 67 . if( resetFlag
790a0 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e ){. wsdStat.
790b0 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 mxValue[op] = ws
790c0 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f dStat.nowValue[o
790d0 70 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e p];. }. return
790e0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
790f0 2a 0a 2a 2a 20 51 75 65 72 79 20 73 74 61 74 75 *.** Query statu
79100 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f s information fo
79110 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 r a single datab
79120 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a ase connection.*
79130 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
79140 20 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 sqlite3_db_stat
79150 75 73 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 us(. sqlite3 *d
79160 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 b, /* T
79170 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
79180 65 63 74 69 6f 6e 20 77 68 6f 73 65 20 73 74 61 ection whose sta
79190 74 75 73 20 69 73 20 64 65 73 69 72 65 64 20 2a tus is desired *
791a0 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 /. int op,
791b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 /* Sta
791c0 74 75 73 20 76 65 72 62 20 2a 2f 0a 20 20 69 6e tus verb */. in
791d0 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 20 20 20 t *pCurrent,
791e0 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 75 72 /* Write cur
791f0 72 65 6e 74 20 76 61 6c 75 65 20 68 65 72 65 20 rent value here
79200 2a 2f 0a 20 20 69 6e 74 20 2a 70 48 69 67 68 77 */. int *pHighw
79210 61 74 65 72 2c 20 20 20 20 20 20 2f 2a 20 57 72 ater, /* Wr
79220 69 74 65 20 68 69 67 68 2d 77 61 74 65 72 20 6d ite high-water m
79230 61 72 6b 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e ark here */. in
79240 74 20 72 65 73 65 74 46 6c 61 67 20 20 20 20 20 t resetFlag
79250 20 20 20 20 2f 2a 20 52 65 73 65 74 20 68 69 67 /* Reset hig
79260 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 69 66 20 h-water mark if
79270 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 true */.){. swi
79280 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 tch( op ){. c
79290 61 73 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41 ase SQLITE_DBSTA
792a0 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 TUS_LOOKASIDE_US
792b0 45 44 3a 20 7b 0a 20 20 20 20 20 20 2a 70 43 75 ED: {. *pCu
792c0 72 72 65 6e 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b rrent = db->look
792d0 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 aside.nOut;.
792e0 20 20 2a 70 48 69 67 68 77 61 74 65 72 20 3d 20 *pHighwater =
792f0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 db->lookaside.mx
79300 4f 75 74 3b 0a 20 20 20 20 20 20 69 66 28 20 72 Out;. if( r
79310 65 73 65 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 esetFlag ){.
79320 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 db->lookasid
79330 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f e.mxOut = db->lo
79340 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 okaside.nOut;.
79350 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
79360 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 k;. }. def
79370 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 65 ault: {. re
79380 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
79390 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 R;. }. }. r
793a0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
793b0 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}../***********
793c0 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 74 61 74 75 *** End of statu
793d0 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s.c ************
793e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
793f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79400 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
79410 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 64 *** Begin file d
79420 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ate.c **********
79430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79450 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4f **/./*.** 2003 O
79460 63 74 6f 62 65 72 20 33 31 0a 2a 2a 0a 2a 2a 20 ctober 31.**.**
79470 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
79480 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
79490 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
794a0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
794b0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
794c0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
794d0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
794e0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
794f0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
79500 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
79510 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
79520 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
79530 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
79540 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
79550 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
79560 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
79570 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
79580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
795a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
795b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
795c0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
795d0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 file contains t
795e0 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 he C functions t
795f0 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 64 61 hat implement da
79600 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 66 te and time.** f
79610 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 53 51 4c unctions for SQL
79620 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 ite. .**.** The
79630 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65 re is only one e
79640 78 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20 69 xported symbol i
79650 6e 20 74 68 69 73 20 66 69 6c 65 20 2d 20 74 68 n this file - th
79660 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71 e function.** sq
79670 6c 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74 lite3RegisterDat
79680 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 29 eTimeFunctions()
79690 20 66 6f 75 6e 64 20 61 74 20 74 68 65 20 62 6f found at the bo
796a0 74 74 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c 65 ttom of the file
796b0 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63 ..** All other c
796c0 6f 64 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f ode has file sco
796d0 70 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64 pe..**.** $Id: d
796e0 61 74 65 2e 63 2c 76 20 31 2e 31 30 37 20 32 30 ate.c,v 1.107 20
796f0 30 39 2f 30 35 2f 30 33 20 32 30 3a 32 33 3a 35 09/05/03 20:23:5
79700 33 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 3 drh Exp $.**.*
79710 2a 20 53 51 4c 69 74 65 20 70 72 6f 63 65 73 73 * SQLite process
79720 65 73 20 61 6c 6c 20 74 69 6d 65 73 20 61 6e 64 es all times and
79730 20 64 61 74 65 73 20 61 73 20 4a 75 6c 69 61 6e dates as Julian
79740 20 44 61 79 20 6e 75 6d 62 65 72 73 2e 20 20 54 Day numbers. T
79750 68 65 0a 2a 2a 20 64 61 74 65 73 20 61 6e 64 20 he.** dates and
79760 74 69 6d 65 73 20 61 72 65 20 73 74 6f 72 65 64 times are stored
79770 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f as the number o
79780 66 20 64 61 79 73 20 73 69 6e 63 65 20 6e 6f 6f f days since noo
79790 6e 0a 2a 2a 20 69 6e 20 47 72 65 65 6e 77 69 63 n.** in Greenwic
797a0 68 20 6f 6e 20 4e 6f 76 65 6d 62 65 72 20 32 34 h on November 24
797b0 2c 20 34 37 31 34 20 42 2e 43 2e 20 61 63 63 6f , 4714 B.C. acco
797c0 72 64 69 6e 67 20 74 6f 20 74 68 65 20 47 72 65 rding to the Gre
797d0 67 6f 72 69 61 6e 0a 2a 2a 20 63 61 6c 65 6e 64 gorian.** calend
797e0 61 72 20 73 79 73 74 65 6d 2e 20 0a 2a 2a 0a 2a ar system. .**.*
797f0 2a 20 31 39 37 30 2d 30 31 2d 30 31 20 30 30 3a * 1970-01-01 00:
79800 30 30 3a 30 30 20 69 73 20 4a 44 20 32 34 34 30 00:00 is JD 2440
79810 35 38 37 2e 35 0a 2a 2a 20 32 30 30 30 2d 30 31 587.5.** 2000-01
79820 2d 30 31 20 30 30 3a 30 30 3a 30 30 20 69 73 20 -01 00:00:00 is
79830 4a 44 20 32 34 35 31 35 34 34 2e 35 0a 2a 2a 0a JD 2451544.5.**.
79840 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e ** This implemen
79850 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 79 65 tion requires ye
79860 61 72 73 20 74 6f 20 62 65 20 65 78 70 72 65 73 ars to be expres
79870 73 65 64 20 61 73 20 61 20 34 2d 64 69 67 69 74 sed as a 4-digit
79880 20 6e 75 6d 62 65 72 0a 2a 2a 20 77 68 69 63 68 number.** which
79890 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 6c 79 means that only
798a0 20 64 61 74 65 73 20 62 65 74 77 65 65 6e 20 30 dates between 0
798b0 30 30 30 2d 30 31 2d 30 31 20 61 6e 64 20 39 39 000-01-01 and 99
798c0 39 39 2d 31 32 2d 33 31 20 63 61 6e 0a 2a 2a 20 99-12-31 can.**
798d0 62 65 20 72 65 70 72 65 73 65 6e 74 65 64 2c 20 be represented,
798e0 65 76 65 6e 20 74 68 6f 75 67 68 20 6a 75 6c 69 even though juli
798f0 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 73 20 61 an day numbers a
79900 6c 6c 6f 77 20 61 20 6d 75 63 68 20 77 69 64 65 llow a much wide
79910 72 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 r.** range of da
79920 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 47 tes..**.** The G
79930 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 regorian calenda
79940 72 20 73 79 73 74 65 6d 20 69 73 20 75 73 65 64 r system is used
79950 20 66 6f 72 20 61 6c 6c 20 64 61 74 65 73 20 61 for all dates a
79960 6e 64 20 74 69 6d 65 73 2c 0a 2a 2a 20 65 76 65 nd times,.** eve
79970 6e 20 74 68 6f 73 65 20 74 68 61 74 20 70 72 65 n those that pre
79980 64 61 74 65 20 74 68 65 20 47 72 65 67 6f 72 69 date the Gregori
79990 61 6e 20 63 61 6c 65 6e 64 61 72 2e 20 20 48 69 an calendar. Hi
799a0 73 74 6f 72 69 61 6e 73 20 75 73 75 61 6c 6c 79 storians usually
799b0 0a 2a 2a 20 75 73 65 20 74 68 65 20 4a 75 6c 69 .** use the Juli
799c0 61 6e 20 63 61 6c 65 6e 64 61 72 20 66 6f 72 20 an calendar for
799d0 64 61 74 65 73 20 70 72 69 6f 72 20 74 6f 20 31 dates prior to 1
799e0 35 38 32 2d 31 30 2d 31 35 20 61 6e 64 20 66 6f 582-10-15 and fo
799f0 72 20 73 6f 6d 65 0a 2a 2a 20 64 61 74 65 73 20 r some.** dates
79a00 61 66 74 65 72 77 61 72 64 73 2c 20 64 65 70 65 afterwards, depe
79a10 6e 64 69 6e 67 20 6f 6e 20 6c 6f 63 61 6c 65 2e nding on locale.
79a20 20 20 42 65 77 61 72 65 20 6f 66 20 74 68 69 73 Beware of this
79a30 20 64 69 66 66 65 72 65 6e 63 65 2e 0a 2a 2a 0a difference..**.
79a40 2a 2a 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f ** The conversio
79a50 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72 65 n algorithms are
79a60 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 61 73 implemented bas
79a70 65 64 20 6f 6e 20 64 65 73 63 72 69 70 74 69 6f ed on descriptio
79a80 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c ns.** in the fol
79a90 6c 6f 77 69 6e 67 20 74 65 78 74 3a 0a 2a 2a 0a lowing text:.**.
79aa0 2a 2a 20 20 20 20 20 20 4a 65 61 6e 20 4d 65 65 ** Jean Mee
79ab0 75 73 0a 2a 2a 20 20 20 20 20 20 41 73 74 72 6f us.** Astro
79ac0 6e 6f 6d 69 63 61 6c 20 41 6c 67 6f 72 69 74 68 nomical Algorith
79ad0 6d 73 2c 20 32 6e 64 20 45 64 69 74 69 6f 6e 2c ms, 2nd Edition,
79ae0 20 31 39 39 38 0a 2a 2a 20 20 20 20 20 20 49 53 1998.** IS
79af0 42 4d 20 30 2d 39 34 33 33 39 36 2d 36 31 2d 31 BM 0-943396-61-1
79b00 0a 2a 2a 20 20 20 20 20 20 57 69 6c 6c 6d 61 6e .** Willman
79b10 6e 2d 42 65 6c 6c 2c 20 49 6e 63 0a 2a 2a 20 20 n-Bell, Inc.**
79b20 20 20 20 20 52 69 63 68 6d 6f 6e 64 2c 20 56 69 Richmond, Vi
79b30 72 67 69 6e 69 61 20 28 55 53 41 29 0a 2a 2f 0a rginia (USA).*/.
79b40 23 69 6e 63 6c 75 64 65 20 3c 74 69 6d 65 2e 68 #include <time.h
79b50 3e 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 >..#ifndef SQLIT
79b60 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f E_OMIT_DATETIME_
79b70 46 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 FUNCS../*.** On
79b80 72 65 63 65 6e 74 20 57 69 6e 64 6f 77 73 20 70 recent Windows p
79b90 6c 61 74 66 6f 72 6d 73 2c 20 74 68 65 20 6c 6f latforms, the lo
79ba0 63 61 6c 74 69 6d 65 5f 73 28 29 20 66 75 6e 63 caltime_s() func
79bb0 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c tion is availabl
79bc0 65 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 e.** as part of
79bd0 74 68 65 20 22 53 65 63 75 72 65 20 43 52 54 22 the "Secure CRT"
79be0 2e 20 49 74 20 69 73 20 65 73 73 65 6e 74 69 61 . It is essentia
79bf0 6c 6c 79 20 65 71 75 69 76 61 6c 65 6e 74 20 74 lly equivalent t
79c00 6f 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f o .** localtime_
79c10 72 28 29 20 61 76 61 69 6c 61 62 6c 65 20 75 6e r() available un
79c20 64 65 72 20 6d 6f 73 74 20 50 4f 53 49 58 20 70 der most POSIX p
79c30 6c 61 74 66 6f 72 6d 73 2c 20 65 78 63 65 70 74 latforms, except
79c40 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 6f 72 that the .** or
79c50 64 65 72 20 6f 66 20 74 68 65 20 70 61 72 61 6d der of the param
79c60 65 74 65 72 73 20 69 73 20 72 65 76 65 72 73 65 eters is reverse
79c70 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 68 74 74 d..**.** See htt
79c80 70 3a 2f 2f 6d 73 64 6e 2e 6d 69 63 72 6f 73 6f p://msdn.microso
79c90 66 74 2e 63 6f 6d 2f 65 6e 2d 75 73 2f 6c 69 62 ft.com/en-us/lib
79ca0 72 61 72 79 2f 61 34 34 32 78 33 79 65 28 56 53 rary/a442x3ye(VS
79cb0 2e 38 30 29 2e 61 73 70 78 2e 0a 2a 2a 0a 2a 2a .80).aspx..**.**
79cc0 20 49 66 20 74 68 65 20 75 73 65 72 20 68 61 73 If the user has
79cd0 20 6e 6f 74 20 69 6e 64 69 63 61 74 65 64 20 74 not indicated t
79ce0 6f 20 75 73 65 20 6c 6f 63 61 6c 74 69 6d 65 5f o use localtime_
79cf0 72 28 29 20 6f 72 20 6c 6f 63 61 6c 74 69 6d 65 r() or localtime
79d00 5f 73 28 29 0a 2a 2a 20 61 6c 72 65 61 64 79 2c _s().** already,
79d10 20 63 68 65 63 6b 20 66 6f 72 20 61 6e 20 4d 53 check for an MS
79d20 56 43 20 62 75 69 6c 64 20 65 6e 76 69 72 6f 6e VC build environ
79d30 6d 65 6e 74 20 74 68 61 74 20 70 72 6f 76 69 64 ment that provid
79d40 65 73 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 es .** localtime
79d50 5f 73 28 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 _s()..*/.#if !de
79d60 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c fined(HAVE_LOCAL
79d70 54 49 4d 45 5f 52 29 20 26 26 20 21 64 65 66 69 TIME_R) && !defi
79d80 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 ned(HAVE_LOCALTI
79d90 4d 45 5f 53 29 20 26 26 20 5c 0a 20 20 20 20 20 ME_S) && \.
79da0 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 defined(_MSC_VER
79db0 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 43 52 ) && defined(_CR
79dc0 54 5f 49 4e 53 45 43 55 52 45 5f 44 45 50 52 45 T_INSECURE_DEPRE
79dd0 43 41 54 45 29 0a 23 64 65 66 69 6e 65 20 48 41 CATE).#define HA
79de0 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 20 31 VE_LOCALTIME_S 1
79df0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 .#endif../*.** A
79e00 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 68 structure for h
79e10 6f 6c 64 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 olding a single
79e20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 2e 0a 2a date and time..*
79e30 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
79e40 20 44 61 74 65 54 69 6d 65 20 44 61 74 65 54 69 DateTime DateTi
79e50 6d 65 3b 0a 73 74 72 75 63 74 20 44 61 74 65 54 me;.struct DateT
79e60 69 6d 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f ime {. sqlite3_
79e70 69 6e 74 36 34 20 69 4a 44 3b 20 2f 2a 20 54 68 int64 iJD; /* Th
79e80 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d e julian day num
79e90 62 65 72 20 74 69 6d 65 73 20 38 36 34 30 30 30 ber times 864000
79ea0 30 30 20 2a 2f 0a 20 20 69 6e 74 20 59 2c 20 4d 00 */. int Y, M
79eb0 2c 20 44 3b 20 20 20 20 20 20 20 2f 2a 20 59 65 , D; /* Ye
79ec0 61 72 2c 20 6d 6f 6e 74 68 2c 20 61 6e 64 20 64 ar, month, and d
79ed0 61 79 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 6d ay */. int h, m
79ee0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f ; /* Ho
79ef0 75 72 20 61 6e 64 20 6d 69 6e 75 74 65 73 20 2a ur and minutes *
79f00 2f 0a 20 20 69 6e 74 20 74 7a 3b 20 20 20 20 20 /. int tz;
79f10 20 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 7a 6f /* Timezo
79f20 6e 65 20 6f 66 66 73 65 74 20 69 6e 20 6d 69 6e ne offset in min
79f30 75 74 65 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 utes */. double
79f40 20 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s; /*
79f50 53 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 63 68 61 Seconds */. cha
79f60 72 20 76 61 6c 69 64 59 4d 44 3b 20 20 20 20 20 r validYMD;
79f70 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 59 /* True (1) if Y
79f80 2c 4d 2c 44 20 61 72 65 20 76 61 6c 69 64 20 2a ,M,D are valid *
79f90 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 48 4d /. char validHM
79fa0 53 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 S; /* True (
79fb0 31 29 20 69 66 20 68 2c 6d 2c 73 20 61 72 65 20 1) if h,m,s are
79fc0 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 valid */. char
79fd0 76 61 6c 69 64 4a 44 3b 20 20 20 20 20 20 2f 2a validJD; /*
79fe0 20 54 72 75 65 20 28 31 29 20 69 66 20 69 4a 44 True (1) if iJD
79ff0 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 is valid */. c
7a000 68 61 72 20 76 61 6c 69 64 54 5a 3b 20 20 20 20 har validTZ;
7a010 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 /* True (1) if
7a020 20 74 7a 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a tz is valid */.
7a030 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 };.../*.** Conve
7a040 72 74 20 7a 44 61 74 65 20 69 6e 74 6f 20 6f 6e rt zDate into on
7a050 65 20 6f 72 20 6d 6f 72 65 20 69 6e 74 65 67 65 e or more intege
7a060 72 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 rs. Additional
7a070 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 63 6f 6d arguments.** com
7a080 65 20 69 6e 20 67 72 6f 75 70 73 20 6f 66 20 35 e in groups of 5
7a090 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a as follows:.**.
7a0a0 2a 2a 20 20 20 20 20 20 20 4e 20 20 20 20 20 20 ** N
7a0b0 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 67 69 74 number of digit
7a0c0 73 20 69 6e 20 74 68 65 20 69 6e 74 65 67 65 72 s in the integer
7a0d0 0a 2a 2a 20 20 20 20 20 20 20 6d 69 6e 20 20 20 .** min
7a0e0 20 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 65 minimum allowe
7a0f0 64 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 d value of the i
7a100 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 nteger.**
7a110 6d 61 78 20 20 20 20 20 6d 61 78 69 6d 75 6d 20 max maximum
7a120 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 allowed value of
7a130 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 the integer.**
7a140 20 20 20 20 20 20 6e 65 78 74 43 20 20 20 66 69 nextC fi
7a150 72 73 74 20 63 68 61 72 61 63 74 65 72 20 61 66 rst character af
7a160 74 65 72 20 74 68 65 20 69 6e 74 65 67 65 72 0a ter the integer.
7a170 2a 2a 20 20 20 20 20 20 20 70 56 61 6c 20 20 20 ** pVal
7a180 20 77 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 where to write
7a190 74 68 65 20 69 6e 74 65 67 65 72 73 20 76 61 6c the integers val
7a1a0 75 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 ue..**.** Conver
7a1b0 73 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75 sions continue u
7a1c0 6e 74 69 6c 20 6f 6e 65 20 77 69 74 68 20 6e 65 ntil one with ne
7a1d0 78 74 43 3d 3d 30 20 69 73 20 65 6e 63 6f 75 6e xtC==0 is encoun
7a1e0 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 75 tered..** The fu
7a1f0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 nction returns t
7a200 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 75 63 he number of suc
7a210 63 65 73 73 66 75 6c 20 63 6f 6e 76 65 72 73 69 cessful conversi
7a220 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ons..*/.static i
7a230 6e 74 20 67 65 74 44 69 67 69 74 73 28 63 6f 6e nt getDigits(con
7a240 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 st char *zDate,
7a250 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 ...){. va_list
7a260 61 70 3b 0a 20 20 69 6e 74 20 76 61 6c 3b 0a 20 ap;. int val;.
7a270 20 69 6e 74 20 4e 3b 0a 20 20 69 6e 74 20 6d 69 int N;. int mi
7a280 6e 3b 0a 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 n;. int max;.
7a290 69 6e 74 20 6e 65 78 74 43 3b 0a 20 20 69 6e 74 int nextC;. int
7a2a0 20 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 20 63 6e *pVal;. int cn
7a2b0 74 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 t = 0;. va_star
7a2c0 74 28 61 70 2c 20 7a 44 61 74 65 29 3b 0a 20 20 t(ap, zDate);.
7a2d0 64 6f 7b 0a 20 20 20 20 4e 20 3d 20 76 61 5f 61 do{. N = va_a
7a2e0 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 rg(ap, int);.
7a2f0 20 6d 69 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 min = va_arg(ap
7a300 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6d 61 78 20 , int);. max
7a310 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 = va_arg(ap, int
7a320 29 3b 0a 20 20 20 20 6e 65 78 74 43 20 3d 20 76 );. nextC = v
7a330 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a a_arg(ap, int);.
7a340 20 20 20 20 70 56 61 6c 20 3d 20 76 61 5f 61 72 pVal = va_ar
7a350 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 g(ap, int*);.
7a360 20 76 61 6c 20 3d 20 30 3b 0a 20 20 20 20 77 68 val = 0;. wh
7a370 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 ile( N-- ){.
7a380 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 if( !sqlite3Is
7a390 64 69 67 69 74 28 2a 7a 44 61 74 65 29 20 29 7b digit(*zDate) ){
7a3a0 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e . goto en
7a3b0 64 5f 67 65 74 44 69 67 69 74 73 3b 0a 20 20 20 d_getDigits;.
7a3c0 20 20 20 7d 0a 20 20 20 20 20 20 76 61 6c 20 3d }. val =
7a3d0 20 76 61 6c 2a 31 30 20 2b 20 2a 7a 44 61 74 65 val*10 + *zDate
7a3e0 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 7a 44 - '0';. zD
7a3f0 61 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 ate++;. }.
7a400 20 69 66 28 20 76 61 6c 3c 6d 69 6e 20 7c 7c 20 if( val<min ||
7a410 76 61 6c 3e 6d 61 78 20 7c 7c 20 28 6e 65 78 74 val>max || (next
7a420 43 21 3d 30 20 26 26 20 6e 65 78 74 43 21 3d 2a C!=0 && nextC!=*
7a430 7a 44 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20 zDate) ){.
7a440 67 6f 74 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 goto end_getDigi
7a450 74 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 ts;. }. *p
7a460 56 61 6c 20 3d 20 76 61 6c 3b 0a 20 20 20 20 7a Val = val;. z
7a470 44 61 74 65 2b 2b 3b 0a 20 20 20 20 63 6e 74 2b Date++;. cnt+
7a480 2b 3b 0a 20 20 7d 77 68 69 6c 65 28 20 6e 65 78 +;. }while( nex
7a490 74 43 20 29 3b 0a 65 6e 64 5f 67 65 74 44 69 67 tC );.end_getDig
7a4a0 69 74 73 3a 0a 20 20 76 61 5f 65 6e 64 28 61 70 its:. va_end(ap
7a4b0 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6e 74 3b );. return cnt;
7a4c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 .}../*.** Read t
7a4d0 65 78 74 20 66 72 6f 6d 20 7a 5b 5d 20 61 6e 64 ext from z[] and
7a4e0 20 63 6f 6e 76 65 72 74 20 69 6e 74 6f 20 61 20 convert into a
7a4f0 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e floating point n
7a500 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a umber. Return.*
7a510 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * the number of
7a520 64 69 67 69 74 73 20 63 6f 6e 76 65 72 74 65 64 digits converted
7a530 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 ..*/.#define get
7a540 56 61 6c 75 65 20 73 71 6c 69 74 65 33 41 74 6f Value sqlite3Ato
7a550 46 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61 F../*.** Parse a
7a560 20 74 69 6d 65 7a 6f 6e 65 20 65 78 74 65 6e 73 timezone extens
7a570 69 6f 6e 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f ion on the end o
7a580 66 20 61 20 64 61 74 65 2d 74 69 6d 65 2e 0a 2a f a date-time..*
7a590 2a 20 54 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 * The extension
7a5a0 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a is of the form:.
7a5b0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 28 2b 2f **.** (+/
7a5c0 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a 2a 20 4f 72 -)HH:MM.**.** Or
7a5d0 20 74 68 65 20 22 7a 75 6c 75 22 20 6e 6f 74 61 the "zulu" nota
7a5e0 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 tion:.**.**
7a5f0 20 20 20 5a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 Z.**.** If th
7a600 65 20 70 61 72 73 65 20 69 73 20 73 75 63 63 65 e parse is succe
7a610 73 73 66 75 6c 2c 20 77 72 69 74 65 20 74 68 65 ssful, write the
7a620 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6e 75 74 number of minut
7a630 65 73 0a 2a 2a 20 6f 66 20 63 68 61 6e 67 65 20 es.** of change
7a640 69 6e 20 70 2d 3e 74 7a 20 61 6e 64 20 72 65 74 in p->tz and ret
7a650 75 72 6e 20 30 2e 20 20 49 66 20 61 20 70 61 72 urn 0. If a par
7a660 73 65 72 20 65 72 72 6f 72 20 6f 63 63 75 72 73 ser error occurs
7a670 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f 6e 2d ,.** return non-
7a680 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 69 zero..**.** A mi
7a690 73 73 69 6e 67 20 73 70 65 63 69 66 69 65 72 20 ssing specifier
7a6a0 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 is not considere
7a6b0 64 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 d an error..*/.s
7a6c0 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 54 tatic int parseT
7a6d0 69 6d 65 7a 6f 6e 65 28 63 6f 6e 73 74 20 63 68 imezone(const ch
7a6e0 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 ar *zDate, DateT
7a6f0 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 73 ime *p){. int s
7a700 67 6e 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 48 gn = 0;. int nH
7a710 72 2c 20 6e 4d 6e 3b 0a 20 20 69 6e 74 20 63 3b r, nMn;. int c;
7a720 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 . while( sqlite
7a730 33 49 73 73 70 61 63 65 28 2a 7a 44 61 74 65 29 3Isspace(*zDate)
7a740 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 ){ zDate++; }.
7a750 20 70 2d 3e 74 7a 20 3d 20 30 3b 0a 20 20 63 20 p->tz = 0;. c
7a760 3d 20 2a 7a 44 61 74 65 3b 0a 20 20 69 66 28 20 = *zDate;. if(
7a770 63 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 73 67 c=='-' ){. sg
7a780 6e 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 20 n = -1;. }else
7a790 69 66 28 20 63 3d 3d 27 2b 27 20 29 7b 0a 20 20 if( c=='+' ){.
7a7a0 20 20 73 67 6e 20 3d 20 2b 31 3b 0a 20 20 7d 65 sgn = +1;. }e
7a7b0 6c 73 65 20 69 66 28 20 63 3d 3d 27 5a 27 20 7c lse if( c=='Z' |
7a7c0 7c 20 63 3d 3d 27 7a 27 20 29 7b 0a 20 20 20 20 | c=='z' ){.
7a7d0 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 67 6f 74 zDate++;. got
7a7e0 6f 20 7a 75 6c 75 5f 74 69 6d 65 3b 0a 20 20 7d o zulu_time;. }
7a7f0 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
7a800 20 63 21 3d 30 3b 0a 20 20 7d 0a 20 20 7a 44 61 c!=0;. }. zDa
7a810 74 65 2b 2b 3b 0a 20 20 69 66 28 20 67 65 74 44 te++;. if( getD
7a820 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 igits(zDate, 2,
7a830 30 2c 20 31 34 2c 20 27 3a 27 2c 20 26 6e 48 72 0, 14, ':', &nHr
7a840 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 , 2, 0, 59, 0, &
7a850 6e 4d 6e 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 nMn)!=2 ){. r
7a860 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a eturn 1;. }. z
7a870 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20 70 2d 3e Date += 5;. p->
7a880 74 7a 20 3d 20 73 67 6e 2a 28 6e 4d 6e 20 2b 20 tz = sgn*(nMn +
7a890 6e 48 72 2a 36 30 29 3b 0a 7a 75 6c 75 5f 74 69 nHr*60);.zulu_ti
7a8a0 6d 65 3a 0a 20 20 77 68 69 6c 65 28 20 73 71 6c me:. while( sql
7a8b0 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 44 61 ite3Isspace(*zDa
7a8c0 74 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 te) ){ zDate++;
7a8d0 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 44 61 74 }. return *zDat
7a8e0 65 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 e!=0;.}../*.** P
7a8f0 61 72 73 65 20 74 69 6d 65 73 20 6f 66 20 74 68 arse times of th
7a900 65 20 66 6f 72 6d 20 48 48 3a 4d 4d 20 6f 72 20 e form HH:MM or
7a910 48 48 3a 4d 4d 3a 53 53 20 6f 72 20 48 48 3a 4d HH:MM:SS or HH:M
7a920 4d 3a 53 53 2e 46 46 46 46 2e 0a 2a 2a 20 54 68 M:SS.FFFF..** Th
7a930 65 20 48 48 2c 20 4d 4d 2c 20 61 6e 64 20 53 53 e HH, MM, and SS
7a940 20 6d 75 73 74 20 65 61 63 68 20 62 65 20 65 78 must each be ex
7a950 61 63 74 6c 79 20 32 20 64 69 67 69 74 73 2e 20 actly 2 digits.
7a960 20 54 68 65 0a 2a 2a 20 66 72 61 63 74 69 6f 6e The.** fraction
7a970 61 6c 20 73 65 63 6f 6e 64 73 20 46 46 46 46 20 al seconds FFFF
7a980 63 61 6e 20 62 65 20 6f 6e 65 20 6f 72 20 6d 6f can be one or mo
7a990 72 65 20 64 69 67 69 74 73 2e 0a 2a 2a 0a 2a 2a re digits..**.**
7a9a0 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 Return 1 if the
7a9b0 72 65 20 69 73 20 61 20 70 61 72 73 69 6e 67 20 re is a parsing
7a9c0 65 72 72 6f 72 20 61 6e 64 20 30 20 6f 6e 20 73 error and 0 on s
7a9d0 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 uccess..*/.stati
7a9e0 63 20 69 6e 74 20 70 61 72 73 65 48 68 4d 6d 53 c int parseHhMmS
7a9f0 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 s(const char *zD
7aa00 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 ate, DateTime *p
7aa10 29 7b 0a 20 20 69 6e 74 20 68 2c 20 6d 2c 20 73 ){. int h, m, s
7aa20 3b 0a 20 20 64 6f 75 62 6c 65 20 6d 73 20 3d 20 ;. double ms =
7aa30 30 2e 30 3b 0a 20 20 69 66 28 20 67 65 74 44 69 0.0;. if( getDi
7aa40 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 gits(zDate, 2, 0
7aa50 2c 20 32 34 2c 20 27 3a 27 2c 20 26 68 2c 20 32 , 24, ':', &h, 2
7aa60 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 6d 29 21 , 0, 59, 0, &m)!
7aa70 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =2 ){. return
7aa80 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20 1;. }. zDate
7aa90 2b 3d 20 35 3b 0a 20 20 69 66 28 20 2a 7a 44 61 += 5;. if( *zDa
7aaa0 74 65 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 7a te==':' ){. z
7aab0 44 61 74 65 2b 2b 3b 0a 20 20 20 20 69 66 28 20 Date++;. if(
7aac0 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c getDigits(zDate,
7aad0 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 73 2, 0, 59, 0, &s
7aae0 29 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 65 )!=1 ){. re
7aaf0 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 turn 1;. }.
7ab00 20 20 7a 44 61 74 65 20 2b 3d 20 32 3b 0a 20 20 zDate += 2;.
7ab10 20 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 27 2e if( *zDate=='.
7ab20 27 20 26 26 20 73 71 6c 69 74 65 33 49 73 64 69 ' && sqlite3Isdi
7ab30 67 69 74 28 7a 44 61 74 65 5b 31 5d 29 20 29 7b git(zDate[1]) ){
7ab40 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 53 . double rS
7ab50 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20 20 20 cale = 1.0;.
7ab60 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 20 zDate++;.
7ab70 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 while( sqlite3I
7ab80 73 64 69 67 69 74 28 2a 7a 44 61 74 65 29 20 29 sdigit(*zDate) )
7ab90 7b 0a 20 20 20 20 20 20 20 20 6d 73 20 3d 20 6d {. ms = m
7aba0 73 2a 31 30 2e 30 20 2b 20 2a 7a 44 61 74 65 20 s*10.0 + *zDate
7abb0 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 72 - '0';. r
7abc0 53 63 61 6c 65 20 2a 3d 20 31 30 2e 30 3b 0a 20 Scale *= 10.0;.
7abd0 20 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a zDate++;.
7abe0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 73 }. ms
7abf0 20 2f 3d 20 72 53 63 61 6c 65 3b 0a 20 20 20 20 /= rScale;.
7ac00 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 }. }else{. s
7ac10 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 = 0;. }. p->v
7ac20 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 70 2d alidJD = 0;. p-
7ac30 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 >validHMS = 1;.
7ac40 20 70 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 2d 3e p->h = h;. p->
7ac50 6d 20 3d 20 6d 3b 0a 20 20 70 2d 3e 73 20 3d 20 m = m;. p->s =
7ac60 73 20 2b 20 6d 73 3b 0a 20 20 69 66 28 20 70 61 s + ms;. if( pa
7ac70 72 73 65 54 69 6d 65 7a 6f 6e 65 28 7a 44 61 74 rseTimezone(zDat
7ac80 65 2c 20 70 29 20 29 20 72 65 74 75 72 6e 20 31 e, p) ) return 1
7ac90 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d ;. p->validTZ =
7aca0 20 28 70 2d 3e 74 7a 21 3d 30 29 3f 31 3a 30 3b (p->tz!=0)?1:0;
7acb0 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
7acc0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 66 72 /*.** Convert fr
7acd0 6f 6d 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 om YYYY-MM-DD HH
7ace0 3a 4d 4d 3a 53 53 20 74 6f 20 6a 75 6c 69 61 6e :MM:SS to julian
7acf0 20 64 61 79 2e 20 20 57 65 20 61 6c 77 61 79 73 day. We always
7ad00 20 61 73 73 75 6d 65 0a 2a 2a 20 74 68 61 74 20 assume.** that
7ad10 74 68 65 20 59 59 59 59 2d 4d 4d 2d 44 44 20 69 the YYYY-MM-DD i
7ad20 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 s according to t
7ad30 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c he Gregorian cal
7ad40 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 endar..**.** Ref
7ad50 65 72 65 6e 63 65 3a 20 20 4d 65 65 75 73 20 70 erence: Meeus p
7ad60 61 67 65 20 36 31 0a 2a 2f 0a 73 74 61 74 69 63 age 61.*/.static
7ad70 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 4a 44 28 void computeJD(
7ad80 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 DateTime *p){.
7ad90 69 6e 74 20 59 2c 20 4d 2c 20 44 2c 20 41 2c 20 int Y, M, D, A,
7ada0 42 2c 20 58 31 2c 20 58 32 3b 0a 0a 20 20 69 66 B, X1, X2;.. if
7adb0 28 20 70 2d 3e 76 61 6c 69 64 4a 44 20 29 20 72 ( p->validJD ) r
7adc0 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e eturn;. if( p->
7add0 76 61 6c 69 64 59 4d 44 20 29 7b 0a 20 20 20 20 validYMD ){.
7ade0 59 20 3d 20 70 2d 3e 59 3b 0a 20 20 20 20 4d 20 Y = p->Y;. M
7adf0 3d 20 70 2d 3e 4d 3b 0a 20 20 20 20 44 20 3d 20 = p->M;. D =
7ae00 70 2d 3e 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 p->D;. }else{.
7ae10 20 20 20 59 20 3d 20 32 30 30 30 3b 20 20 2f 2a Y = 2000; /*
7ae20 20 49 66 20 6e 6f 20 59 4d 44 20 73 70 65 63 69 If no YMD speci
7ae30 66 69 65 64 2c 20 61 73 73 75 6d 65 20 32 30 30 fied, assume 200
7ae40 30 2d 4a 61 6e 2d 30 31 20 2a 2f 0a 20 20 20 20 0-Jan-01 */.
7ae50 4d 20 3d 20 31 3b 0a 20 20 20 20 44 20 3d 20 31 M = 1;. D = 1
7ae60 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4d 3c 3d 32 ;. }. if( M<=2
7ae70 20 29 7b 0a 20 20 20 20 59 2d 2d 3b 0a 20 20 20 ){. Y--;.
7ae80 20 4d 20 2b 3d 20 31 32 3b 0a 20 20 7d 0a 20 20 M += 12;. }.
7ae90 41 20 3d 20 59 2f 31 30 30 3b 0a 20 20 42 20 3d A = Y/100;. B =
7aea0 20 32 20 2d 20 41 20 2b 20 28 41 2f 34 29 3b 0a 2 - A + (A/4);.
7aeb0 20 20 58 31 20 3d 20 33 36 35 32 35 2a 28 59 2b X1 = 36525*(Y+
7aec0 34 37 31 36 29 2f 31 30 30 3b 0a 20 20 58 32 20 4716)/100;. X2
7aed0 3d 20 33 30 36 30 30 31 2a 28 4d 2b 31 29 2f 31 = 306001*(M+1)/1
7aee0 30 30 30 30 3b 0a 20 20 70 2d 3e 69 4a 44 20 3d 0000;. p->iJD =
7aef0 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 (sqlite3_int64)
7af00 28 28 58 31 20 2b 20 58 32 20 2b 20 44 20 2b 20 ((X1 + X2 + D +
7af10 42 20 2d 20 31 35 32 34 2e 35 20 29 20 2a 20 38 B - 1524.5 ) * 8
7af20 36 34 30 30 30 30 30 29 3b 0a 20 20 70 2d 3e 76 6400000);. p->v
7af30 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 69 66 alidJD = 1;. if
7af40 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 7b ( p->validHMS ){
7af50 0a 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 70 . p->iJD += p
7af60 2d 3e 68 2a 33 36 30 30 30 30 30 20 2b 20 70 2d ->h*3600000 + p-
7af70 3e 6d 2a 36 30 30 30 30 20 2b 20 28 73 71 6c 69 >m*60000 + (sqli
7af80 74 65 33 5f 69 6e 74 36 34 29 28 70 2d 3e 73 2a te3_int64)(p->s*
7af90 31 30 30 30 29 3b 0a 20 20 20 20 69 66 28 20 70 1000);. if( p
7afa0 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20 ->validTZ ){.
7afb0 20 20 20 70 2d 3e 69 4a 44 20 2d 3d 20 70 2d 3e p->iJD -= p->
7afc0 74 7a 2a 36 30 30 30 30 3b 0a 20 20 20 20 20 20 tz*60000;.
7afd0 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b p->validYMD = 0;
7afe0 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 . p->validH
7aff0 4d 53 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d MS = 0;. p-
7b000 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 >validTZ = 0;.
7b010 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
7b020 20 50 61 72 73 65 20 64 61 74 65 73 20 6f 66 20 Parse dates of
7b030 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 the form.**.**
7b040 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 YYYY-MM-DD HH
7b050 3a 4d 4d 3a 53 53 2e 46 46 46 0a 2a 2a 20 20 20 :MM:SS.FFF.**
7b060 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a YYYY-MM-DD HH:
7b070 4d 4d 3a 53 53 0a 2a 2a 20 20 20 20 20 59 59 59 MM:SS.** YYY
7b080 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 0a 2a 2a Y-MM-DD HH:MM.**
7b090 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 0a YYYY-MM-DD.
7b0a0 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 **.** Write the
7b0b0 72 65 73 75 6c 74 20 69 6e 74 6f 20 74 68 65 20 result into the
7b0c0 44 61 74 65 54 69 6d 65 20 73 74 72 75 63 74 75 DateTime structu
7b0d0 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 30 0a re and return 0.
7b0e0 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e ** on success an
7b0f0 64 20 31 20 69 66 20 74 68 65 20 69 6e 70 75 74 d 1 if the input
7b100 20 73 74 72 69 6e 67 20 69 73 20 6e 6f 74 20 61 string is not a
7b110 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 well-formed.**
7b120 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 date..*/.static
7b130 69 6e 74 20 70 61 72 73 65 59 79 79 79 4d 6d 44 int parseYyyyMmD
7b140 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 d(const char *zD
7b150 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 ate, DateTime *p
7b160 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 ){. int Y, M, D
7b170 2c 20 6e 65 67 3b 0a 0a 20 20 69 66 28 20 7a 44 , neg;.. if( zD
7b180 61 74 65 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20 ate[0]=='-' ){.
7b190 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 zDate++;.
7b1a0 6e 65 67 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 neg = 1;. }else
7b1b0 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 {. neg = 0;.
7b1c0 20 7d 0a 20 20 69 66 28 20 67 65 74 44 69 67 69 }. if( getDigi
7b1d0 74 73 28 7a 44 61 74 65 2c 34 2c 30 2c 39 39 39 ts(zDate,4,0,999
7b1e0 39 2c 27 2d 27 2c 26 59 2c 32 2c 31 2c 31 32 2c 9,'-',&Y,2,1,12,
7b1f0 27 2d 27 2c 26 4d 2c 32 2c 31 2c 33 31 2c 30 2c '-',&M,2,1,31,0,
7b200 26 44 29 21 3d 33 20 29 7b 0a 20 20 20 20 72 65 &D)!=3 ){. re
7b210 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 turn 1;. }. zD
7b220 61 74 65 20 2b 3d 20 31 30 3b 0a 20 20 77 68 69 ate += 10;. whi
7b230 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 le( sqlite3Isspa
7b240 63 65 28 2a 7a 44 61 74 65 29 20 7c 7c 20 27 54 ce(*zDate) || 'T
7b250 27 3d 3d 2a 28 75 38 2a 29 7a 44 61 74 65 20 29 '==*(u8*)zDate )
7b260 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 69 { zDate++; }. i
7b270 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a f( parseHhMmSs(z
7b280 44 61 74 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 Date, p)==0 ){.
7b290 20 20 20 2f 2a 20 57 65 20 67 6f 74 20 74 68 65 /* We got the
7b2a0 20 74 69 6d 65 20 2a 2f 0a 20 20 7d 65 6c 73 65 time */. }else
7b2b0 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 30 20 29 if( *zDate==0 )
7b2c0 7b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d {. p->validHM
7b2d0 53 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a S = 0;. }else{.
7b2e0 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
7b2f0 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d }. p->validJD =
7b300 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 0;. p->validYM
7b310 44 20 3d 20 31 3b 0a 20 20 70 2d 3e 59 20 3d 20 D = 1;. p->Y =
7b320 6e 65 67 20 3f 20 2d 59 20 3a 20 59 3b 0a 20 20 neg ? -Y : Y;.
7b330 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 20 70 2d 3e 44 p->M = M;. p->D
7b340 20 3d 20 44 3b 0a 20 20 69 66 28 20 70 2d 3e 76 = D;. if( p->v
7b350 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20 20 63 6f alidTZ ){. co
7b360 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 7d 0a mputeJD(p);. }.
7b370 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
7b380 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74 69 6d *.** Set the tim
7b390 65 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 e to the current
7b3a0 20 74 69 6d 65 20 72 65 70 6f 72 74 65 64 20 62 time reported b
7b3b0 79 20 74 68 65 20 56 46 53 0a 2a 2f 0a 73 74 61 y the VFS.*/.sta
7b3c0 74 69 63 20 76 6f 69 64 20 73 65 74 44 61 74 65 tic void setDate
7b3d0 54 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 73 71 TimeToCurrent(sq
7b3e0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
7b3f0 6f 6e 74 65 78 74 2c 20 44 61 74 65 54 69 6d 65 ontext, DateTime
7b400 20 2a 70 29 7b 0a 20 20 64 6f 75 62 6c 65 20 72 *p){. double r
7b410 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
7b420 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 = sqlite3_contex
7b430 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 t_db_handle(cont
7b440 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f ext);. sqlite3O
7b450 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d sCurrentTime(db-
7b460 3e 70 56 66 73 2c 20 26 72 29 3b 0a 20 20 70 2d >pVfs, &r);. p-
7b470 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f >iJD = (sqlite3_
7b480 69 6e 74 36 34 29 28 72 2a 38 36 34 30 30 30 30 int64)(r*8640000
7b490 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 70 2d 0.0 + 0.5);. p-
7b4a0 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 7d 0a >validJD = 1;.}.
7b4b0 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 ./*.** Attempt t
7b4c0 6f 20 70 61 72 73 65 20 74 68 65 20 67 69 76 65 o parse the give
7b4d0 6e 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 n string into a
7b4e0 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75 6d 62 65 Julian Day Numbe
7b4f0 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 r. Return.** th
7b500 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f e number of erro
7b510 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f rs..**.** The fo
7b520 6c 6c 6f 77 69 6e 67 20 61 72 65 20 61 63 63 65 llowing are acce
7b530 70 74 61 62 6c 65 20 66 6f 72 6d 73 20 66 6f 72 ptable forms for
7b540 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e the input strin
7b550 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 59 g:.**.** YY
7b560 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 YY-MM-DD HH:MM:S
7b570 53 2e 46 46 46 20 20 2b 2f 2d 48 48 3a 4d 4d 0a S.FFF +/-HH:MM.
7b580 2a 2a 20 20 20 20 20 20 44 44 44 44 2e 44 44 20 ** DDDD.DD
7b590 0a 2a 2a 20 20 20 20 20 20 6e 6f 77 0a 2a 2a 0a .** now.**.
7b5a0 2a 2a 20 49 6e 20 74 68 65 20 66 69 72 73 74 20 ** In the first
7b5b0 66 6f 72 6d 2c 20 74 68 65 20 2b 2f 2d 48 48 3a form, the +/-HH:
7b5c0 4d 4d 20 69 73 20 61 6c 77 61 79 73 20 6f 70 74 MM is always opt
7b5d0 69 6f 6e 61 6c 2e 20 20 54 68 65 20 66 72 61 63 ional. The frac
7b5e0 74 69 6f 6e 61 6c 0a 2a 2a 20 73 65 63 6f 6e 64 tional.** second
7b5f0 73 20 65 78 74 65 6e 73 69 6f 6e 20 28 74 68 65 s extension (the
7b600 20 22 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69 ".FFF") is opti
7b610 6f 6e 61 6c 2e 20 20 54 68 65 20 73 65 63 6f 6e onal. The secon
7b620 64 73 20 70 6f 72 74 69 6f 6e 0a 2a 2a 20 28 22 ds portion.** ("
7b630 3a 53 53 2e 46 46 46 22 29 20 69 73 20 6f 70 74 :SS.FFF") is opt
7b640 69 6f 6e 2e 20 20 54 68 65 20 79 65 61 72 20 61 ion. The year a
7b650 6e 64 20 64 61 74 65 20 63 61 6e 20 62 65 20 6f nd date can be o
7b660 6d 69 74 74 65 64 20 61 73 20 6c 6f 6e 67 0a 2a mitted as long.*
7b670 2a 20 61 73 20 74 68 65 72 65 20 69 73 20 61 20 * as there is a
7b680 74 69 6d 65 20 73 74 72 69 6e 67 2e 20 20 54 68 time string. Th
7b690 65 20 74 69 6d 65 20 73 74 72 69 6e 67 20 63 61 e time string ca
7b6a0 6e 20 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20 n be omitted as
7b6b0 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 long.** as there
7b6c0 20 69 73 20 61 20 79 65 61 72 20 61 6e 64 20 64 is a year and d
7b6d0 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ate..*/.static i
7b6e0 6e 74 20 70 61 72 73 65 44 61 74 65 4f 72 54 69 nt parseDateOrTi
7b6f0 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f me(. sqlite3_co
7b700 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 ntext *context,
7b710 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
7b720 44 61 74 65 2c 20 0a 20 20 44 61 74 65 54 69 6d Date, . DateTim
7b730 65 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 69 73 e *p.){. int is
7b740 52 65 61 6c 4e 75 6d 3b 20 20 20 20 2f 2a 20 52 RealNum; /* R
7b750 65 74 75 72 6e 20 66 72 6f 6d 20 73 71 6c 69 74 eturn from sqlit
7b760 65 33 49 73 4e 75 6d 62 65 72 28 29 2e 20 20 4e e3IsNumber(). N
7b770 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 69 66 28 ot used */. if(
7b780 20 70 61 72 73 65 59 79 79 79 4d 6d 44 64 28 7a parseYyyyMmDd(z
7b790 44 61 74 65 2c 70 29 3d 3d 30 20 29 7b 0a 20 20 Date,p)==0 ){.
7b7a0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 return 0;. }e
7b7b0 6c 73 65 20 69 66 28 20 70 61 72 73 65 48 68 4d lse if( parseHhM
7b7c0 6d 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 mSs(zDate, p)==0
7b7d0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
7b7e0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 ;. }else if( sq
7b7f0 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 61 lite3StrICmp(zDa
7b800 74 65 2c 22 6e 6f 77 22 29 3d 3d 30 29 7b 0a 20 te,"now")==0){.
7b810 20 20 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f setDateTimeTo
7b820 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c Current(context,
7b830 20 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 p);. return
7b840 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 0;. }else if( s
7b850 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 7a qlite3IsNumber(z
7b860 44 61 74 65 2c 20 26 69 73 52 65 61 6c 4e 75 6d Date, &isRealNum
7b870 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 20 29 , SQLITE_UTF8) )
7b880 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 3b 0a {. double r;.
7b890 20 20 20 20 67 65 74 56 61 6c 75 65 28 7a 44 61 getValue(zDa
7b8a0 74 65 2c 20 26 72 29 3b 0a 20 20 20 20 70 2d 3e te, &r);. p->
7b8b0 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 iJD = (sqlite3_i
7b8c0 6e 74 36 34 29 28 72 2a 38 36 34 30 30 30 30 30 nt64)(r*86400000
7b8d0 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 20 20 70 .0 + 0.5);. p
7b8e0 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 ->validJD = 1;.
7b8f0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
7b900 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a . return 1;.}..
7b910 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 /*.** Compute th
7b920 65 20 59 65 61 72 2c 20 4d 6f 6e 74 68 2c 20 61 e Year, Month, a
7b930 6e 64 20 44 61 79 20 66 72 6f 6d 20 74 68 65 20 nd Day from the
7b940 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 julian day numbe
7b950 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 r..*/.static voi
7b960 64 20 63 6f 6d 70 75 74 65 59 4d 44 28 44 61 74 d computeYMD(Dat
7b970 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 eTime *p){. int
7b980 20 5a 2c 20 41 2c 20 42 2c 20 43 2c 20 44 2c 20 Z, A, B, C, D,
7b990 45 2c 20 58 31 3b 0a 20 20 69 66 28 20 70 2d 3e E, X1;. if( p->
7b9a0 76 61 6c 69 64 59 4d 44 20 29 20 72 65 74 75 72 validYMD ) retur
7b9b0 6e 3b 0a 20 20 69 66 28 20 21 70 2d 3e 76 61 6c n;. if( !p->val
7b9c0 69 64 4a 44 20 29 7b 0a 20 20 20 20 70 2d 3e 59 idJD ){. p->Y
7b9d0 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 70 2d 3e = 2000;. p->
7b9e0 4d 20 3d 20 31 3b 0a 20 20 20 20 70 2d 3e 44 20 M = 1;. p->D
7b9f0 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 1;. }else{.
7ba00 20 20 5a 20 3d 20 28 69 6e 74 29 28 28 70 2d 3e Z = (int)((p->
7ba10 69 4a 44 20 2b 20 34 33 32 30 30 30 30 30 29 2f iJD + 43200000)/
7ba20 38 36 34 30 30 30 30 30 29 3b 0a 20 20 20 20 41 86400000);. A
7ba30 20 3d 20 28 69 6e 74 29 28 28 5a 20 2d 20 31 38 = (int)((Z - 18
7ba40 36 37 32 31 36 2e 32 35 29 2f 33 36 35 32 34 2e 67216.25)/36524.
7ba50 32 35 29 3b 0a 20 20 20 20 41 20 3d 20 5a 20 2b 25);. A = Z +
7ba60 20 31 20 2b 20 41 20 2d 20 28 41 2f 34 29 3b 0a 1 + A - (A/4);.
7ba70 20 20 20 20 42 20 3d 20 41 20 2b 20 31 35 32 34 B = A + 1524
7ba80 3b 0a 20 20 20 20 43 20 3d 20 28 69 6e 74 29 28 ;. C = (int)(
7ba90 28 42 20 2d 20 31 32 32 2e 31 29 2f 33 36 35 2e (B - 122.1)/365.
7baa0 32 35 29 3b 0a 20 20 20 20 44 20 3d 20 28 33 36 25);. D = (36
7bab0 35 32 35 2a 43 29 2f 31 30 30 3b 0a 20 20 20 20 525*C)/100;.
7bac0 45 20 3d 20 28 69 6e 74 29 28 28 42 2d 44 29 2f E = (int)((B-D)/
7bad0 33 30 2e 36 30 30 31 29 3b 0a 20 20 20 20 58 31 30.6001);. X1
7bae0 20 3d 20 28 69 6e 74 29 28 33 30 2e 36 30 30 31 = (int)(30.6001
7baf0 2a 45 29 3b 0a 20 20 20 20 70 2d 3e 44 20 3d 20 *E);. p->D =
7bb00 42 20 2d 20 44 20 2d 20 58 31 3b 0a 20 20 20 20 B - D - X1;.
7bb10 70 2d 3e 4d 20 3d 20 45 3c 31 34 20 3f 20 45 2d p->M = E<14 ? E-
7bb20 31 20 3a 20 45 2d 31 33 3b 0a 20 20 20 20 70 2d 1 : E-13;. p-
7bb30 3e 59 20 3d 20 70 2d 3e 4d 3e 32 20 3f 20 43 20 >Y = p->M>2 ? C
7bb40 2d 20 34 37 31 36 20 3a 20 43 20 2d 20 34 37 31 - 4716 : C - 471
7bb50 35 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 5;. }. p->vali
7bb60 64 59 4d 44 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a dYMD = 1;.}../*.
7bb70 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 48 ** Compute the H
7bb80 6f 75 72 2c 20 4d 69 6e 75 74 65 2c 20 61 6e 64 our, Minute, and
7bb90 20 53 65 63 6f 6e 64 73 20 66 72 6f 6d 20 74 68 Seconds from th
7bba0 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d e julian day num
7bbb0 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ber..*/.static v
7bbc0 6f 69 64 20 63 6f 6d 70 75 74 65 48 4d 53 28 44 oid computeHMS(D
7bbd0 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 ateTime *p){. i
7bbe0 6e 74 20 73 3b 0a 20 20 69 66 28 20 70 2d 3e 76 nt s;. if( p->v
7bbf0 61 6c 69 64 48 4d 53 20 29 20 72 65 74 75 72 6e alidHMS ) return
7bc00 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 ;. computeJD(p)
7bc10 3b 0a 20 20 73 20 3d 20 28 69 6e 74 29 28 28 70 ;. s = (int)((p
7bc20 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 30 30 30 ->iJD + 43200000
7bc30 29 20 25 20 38 36 34 30 30 30 30 30 29 3b 0a 20 ) % 86400000);.
7bc40 20 70 2d 3e 73 20 3d 20 73 2f 31 30 30 30 2e 30 p->s = s/1000.0
7bc50 3b 0a 20 20 73 20 3d 20 28 69 6e 74 29 70 2d 3e ;. s = (int)p->
7bc60 73 3b 0a 20 20 70 2d 3e 73 20 2d 3d 20 73 3b 0a s;. p->s -= s;.
7bc70 20 20 70 2d 3e 68 20 3d 20 73 2f 33 36 30 30 3b p->h = s/3600;
7bc80 0a 20 20 73 20 2d 3d 20 70 2d 3e 68 2a 33 36 30 . s -= p->h*360
7bc90 30 3b 0a 20 20 70 2d 3e 6d 20 3d 20 73 2f 36 30 0;. p->m = s/60
7bca0 3b 0a 20 20 70 2d 3e 73 20 2b 3d 20 73 20 2d 20 ;. p->s += s -
7bcb0 70 2d 3e 6d 2a 36 30 3b 0a 20 20 70 2d 3e 76 61 p->m*60;. p->va
7bcc0 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 7d 0a 0a 2f lidHMS = 1;.}../
7bcd0 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 62 6f 74 *.** Compute bot
7bce0 68 20 59 4d 44 20 61 6e 64 20 48 4d 53 0a 2a 2f h YMD and HMS.*/
7bcf0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d .static void com
7bd00 70 75 74 65 59 4d 44 5f 48 4d 53 28 44 61 74 65 puteYMD_HMS(Date
7bd10 54 69 6d 65 20 2a 70 29 7b 0a 20 20 63 6f 6d 70 Time *p){. comp
7bd20 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 63 6f 6d uteYMD(p);. com
7bd30 70 75 74 65 48 4d 53 28 70 29 3b 0a 7d 0a 0a 2f puteHMS(p);.}../
7bd40 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 59 *.** Clear the Y
7bd50 4d 44 20 61 6e 64 20 48 4d 53 20 61 6e 64 20 74 MD and HMS and t
7bd60 68 65 20 54 5a 0a 2a 2f 0a 73 74 61 74 69 63 20 he TZ.*/.static
7bd70 76 6f 69 64 20 63 6c 65 61 72 59 4d 44 5f 48 4d void clearYMD_HM
7bd80 53 5f 54 5a 28 44 61 74 65 54 69 6d 65 20 2a 70 S_TZ(DateTime *p
7bd90 29 7b 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 ){. p->validYMD
7bda0 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 = 0;. p->valid
7bdb0 48 4d 53 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 HMS = 0;. p->va
7bdc0 6c 69 64 54 5a 20 3d 20 30 3b 0a 7d 0a 0a 23 69 lidTZ = 0;.}..#i
7bdd0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
7bde0 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 2f 2a 0a 2a T_LOCALTIME./*.*
7bdf0 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 64 69 * Compute the di
7be00 66 66 65 72 65 6e 63 65 20 28 69 6e 20 6d 69 6c fference (in mil
7be10 6c 69 73 65 63 6f 6e 64 73 29 0a 2a 2a 20 62 65 liseconds).** be
7be20 74 77 65 65 6e 20 6c 6f 63 61 6c 74 69 6d 65 20 tween localtime
7be30 61 6e 64 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 and UTC (a.k.a.
7be40 47 4d 54 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 GMT).** for the
7be50 74 69 6d 65 20 76 61 6c 75 65 20 70 20 77 68 65 time value p whe
7be60 72 65 20 70 20 69 73 20 69 6e 20 55 54 43 2e 0a re p is in UTC..
7be70 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 */.static sqlite
7be80 33 5f 69 6e 74 36 34 20 6c 6f 63 61 6c 74 69 6d 3_int64 localtim
7be90 65 4f 66 66 73 65 74 28 44 61 74 65 54 69 6d 65 eOffset(DateTime
7bea0 20 2a 70 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 *p){. DateTime
7beb0 20 78 2c 20 79 3b 0a 20 20 74 69 6d 65 5f 74 20 x, y;. time_t
7bec0 74 3b 0a 20 20 78 20 3d 20 2a 70 3b 0a 20 20 63 t;. x = *p;. c
7bed0 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 omputeYMD_HMS(&x
7bee0 29 3b 0a 20 20 69 66 28 20 78 2e 59 3c 31 39 37 );. if( x.Y<197
7bef0 31 20 7c 7c 20 78 2e 59 3e 3d 32 30 33 38 20 29 1 || x.Y>=2038 )
7bf00 7b 0a 20 20 20 20 78 2e 59 20 3d 20 32 30 30 30 {. x.Y = 2000
7bf10 3b 0a 20 20 20 20 78 2e 4d 20 3d 20 31 3b 0a 20 ;. x.M = 1;.
7bf20 20 20 20 78 2e 44 20 3d 20 31 3b 0a 20 20 20 20 x.D = 1;.
7bf30 78 2e 68 20 3d 20 30 3b 0a 20 20 20 20 78 2e 6d x.h = 0;. x.m
7bf40 20 3d 20 30 3b 0a 20 20 20 20 78 2e 73 20 3d 20 = 0;. x.s =
7bf50 30 2e 30 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 0.0;. } else {.
7bf60 20 20 20 20 69 6e 74 20 73 20 3d 20 28 69 6e 74 int s = (int
7bf70 29 28 78 2e 73 20 2b 20 30 2e 35 29 3b 0a 20 20 )(x.s + 0.5);.
7bf80 20 20 78 2e 73 20 3d 20 73 3b 0a 20 20 7d 0a 20 x.s = s;. }.
7bf90 20 78 2e 74 7a 20 3d 20 30 3b 0a 20 20 78 2e 76 x.tz = 0;. x.v
7bfa0 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 63 6f alidJD = 0;. co
7bfb0 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 74 mputeJD(&x);. t
7bfc0 20 3d 20 28 74 69 6d 65 5f 74 29 28 78 2e 69 4a = (time_t)(x.iJ
7bfd0 44 2f 31 30 30 30 20 2d 20 32 31 30 38 36 36 37 D/1000 - 2108667
7bfe0 36 2a 28 69 36 34 29 31 30 30 30 30 29 3b 0a 23 6*(i64)10000);.#
7bff0 69 66 64 65 66 20 48 41 56 45 5f 4c 4f 43 41 4c ifdef HAVE_LOCAL
7c000 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73 TIME_R. {. s
7c010 74 72 75 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b truct tm sLocal;
7c020 0a 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 . localtime_r
7c030 28 26 74 2c 20 26 73 4c 6f 63 61 6c 29 3b 0a 20 (&t, &sLocal);.
7c040 20 20 20 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e y.Y = sLocal.
7c050 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a tm_year + 1900;.
7c060 20 20 20 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c y.M = sLocal
7c070 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 .tm_mon + 1;.
7c080 20 79 2e 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d y.D = sLocal.tm
7c090 5f 6d 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d _mday;. y.h =
7c0a0 20 73 4c 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b sLocal.tm_hour;
7c0b0 0a 20 20 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 . y.m = sLoca
7c0c0 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e l.tm_min;. y.
7c0d0 73 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 s = sLocal.tm_se
7c0e0 63 3b 0a 20 20 7d 0a 23 65 6c 69 66 20 64 65 66 c;. }.#elif def
7c0f0 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 ined(HAVE_LOCALT
7c100 49 4d 45 5f 53 29 20 26 26 20 48 41 56 45 5f 4c IME_S) && HAVE_L
7c110 4f 43 41 4c 54 49 4d 45 5f 53 0a 20 20 7b 0a 20 OCALTIME_S. {.
7c120 20 20 20 73 74 72 75 63 74 20 74 6d 20 73 4c 6f struct tm sLo
7c130 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 6c 74 69 cal;. localti
7c140 6d 65 5f 73 28 26 73 4c 6f 63 61 6c 2c 20 26 74 me_s(&sLocal, &t
7c150 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 73 4c 6f );. y.Y = sLo
7c160 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b 20 31 39 cal.tm_year + 19
7c170 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 73 4c 00;. y.M = sL
7c180 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b ocal.tm_mon + 1;
7c190 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c 6f 63 61 . y.D = sLoca
7c1a0 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 79 l.tm_mday;. y
7c1b0 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 68 .h = sLocal.tm_h
7c1c0 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 73 our;. y.m = s
7c1d0 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 20 Local.tm_min;.
7c1e0 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e 74 y.s = sLocal.t
7c1f0 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 m_sec;. }.#else
7c200 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 . {. struct
7c210 74 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71 6c tm *pTm;. sql
7c220 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
7c230 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c (sqlite3MutexAll
7c240 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
7c250 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b STATIC_MASTER));
7c260 0a 20 20 20 20 70 54 6d 20 3d 20 6c 6f 63 61 6c . pTm = local
7c270 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 79 2e time(&t);. y.
7c280 59 20 3d 20 70 54 6d 2d 3e 74 6d 5f 79 65 61 72 Y = pTm->tm_year
7c290 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d + 1900;. y.M
7c2a0 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 6f 6e 20 2b = pTm->tm_mon +
7c2b0 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20 70 54 1;. y.D = pT
7c2c0 6d 2d 3e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 m->tm_mday;.
7c2d0 79 2e 68 20 3d 20 70 54 6d 2d 3e 74 6d 5f 68 6f y.h = pTm->tm_ho
7c2e0 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 70 54 ur;. y.m = pT
7c2f0 6d 2d 3e 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 m->tm_min;. y
7c300 2e 73 20 3d 20 70 54 6d 2d 3e 74 6d 5f 73 65 63 .s = pTm->tm_sec
7c310 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
7c320 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 tex_leave(sqlite
7c330 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
7c340 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
7c350 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d 0a 23 65 MASTER));. }.#e
7c360 6e 64 69 66 0a 20 20 79 2e 76 61 6c 69 64 59 4d ndif. y.validYM
7c370 44 20 3d 20 31 3b 0a 20 20 79 2e 76 61 6c 69 64 D = 1;. y.valid
7c380 48 4d 53 20 3d 20 31 3b 0a 20 20 79 2e 76 61 6c HMS = 1;. y.val
7c390 69 64 4a 44 20 3d 20 30 3b 0a 20 20 79 2e 76 61 idJD = 0;. y.va
7c3a0 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 63 6f 6d lidTZ = 0;. com
7c3b0 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 20 72 65 puteJD(&y);. re
7c3c0 74 75 72 6e 20 79 2e 69 4a 44 20 2d 20 78 2e 69 turn y.iJD - x.i
7c3d0 4a 44 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 JD;.}.#endif /*
7c3e0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 SQLITE_OMIT_LOCA
7c3f0 4c 54 49 4d 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 LTIME */../*.**
7c400 50 72 6f 63 65 73 73 20 61 20 6d 6f 64 69 66 69 Process a modifi
7c410 65 72 20 74 6f 20 61 20 64 61 74 65 2d 74 69 6d er to a date-tim
7c420 65 20 73 74 61 6d 70 2e 20 20 54 68 65 20 6d 6f e stamp. The mo
7c430 64 69 66 69 65 72 73 20 61 72 65 0a 2a 2a 20 61 difiers are.** a
7c440 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
7c450 20 20 20 20 20 4e 4e 4e 20 64 61 79 73 0a 2a 2a NNN days.**
7c460 20 20 20 20 20 4e 4e 4e 20 68 6f 75 72 73 0a 2a NNN hours.*
7c470 2a 20 20 20 20 20 4e 4e 4e 20 6d 69 6e 75 74 65 * NNN minute
7c480 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 2e 4e 4e 4e s.** NNN.NNN
7c490 4e 20 73 65 63 6f 6e 64 73 0a 2a 2a 20 20 20 20 N seconds.**
7c4a0 20 4e 4e 4e 20 6d 6f 6e 74 68 73 0a 2a 2a 20 20 NNN months.**
7c4b0 20 20 20 4e 4e 4e 20 79 65 61 72 73 0a 2a 2a 20 NNN years.**
7c4c0 20 20 20 20 73 74 61 72 74 20 6f 66 20 6d 6f 6e start of mon
7c4d0 74 68 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 th.** start
7c4e0 6f 66 20 79 65 61 72 0a 2a 2a 20 20 20 20 20 73 of year.** s
7c4f0 74 61 72 74 20 6f 66 20 77 65 65 6b 0a 2a 2a 20 tart of week.**
7c500 20 20 20 20 73 74 61 72 74 20 6f 66 20 64 61 79 start of day
7c510 0a 2a 2a 20 20 20 20 20 77 65 65 6b 64 61 79 20 .** weekday
7c520 4e 0a 2a 2a 20 20 20 20 20 75 6e 69 78 65 70 6f N.** unixepo
7c530 63 68 0a 2a 2a 20 20 20 20 20 6c 6f 63 61 6c 74 ch.** localt
7c540 69 6d 65 0a 2a 2a 20 20 20 20 20 75 74 63 0a 2a ime.** utc.*
7c550 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 6f 6e *.** Return 0 on
7c560 20 73 75 63 63 65 73 73 20 61 6e 64 20 31 20 69 success and 1 i
7c570 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b f there is any k
7c580 69 6e 64 20 6f 66 20 65 72 72 6f 72 2e 0a 2a 2f ind of error..*/
7c590 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 .static int pars
7c5a0 65 4d 6f 64 69 66 69 65 72 28 63 6f 6e 73 74 20 eModifier(const
7c5b0 63 68 61 72 20 2a 7a 4d 6f 64 2c 20 44 61 74 65 char *zMod, Date
7c5c0 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 Time *p){. int
7c5d0 72 63 20 3d 20 31 3b 0a 20 20 69 6e 74 20 6e 3b rc = 1;. int n;
7c5e0 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a 20 20 63 . double r;. c
7c5f0 68 61 72 20 2a 7a 2c 20 7a 42 75 66 5b 33 30 5d har *z, zBuf[30]
7c600 3b 0a 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 ;. z = zBuf;.
7c610 66 6f 72 28 6e 3d 30 3b 20 6e 3c 41 72 72 61 79 for(n=0; n<Array
7c620 53 69 7a 65 28 7a 42 75 66 29 2d 31 20 26 26 20 Size(zBuf)-1 &&
7c630 7a 4d 6f 64 5b 6e 5d 3b 20 6e 2b 2b 29 7b 0a 20 zMod[n]; n++){.
7c640 20 20 20 7a 5b 6e 5d 20 3d 20 28 63 68 61 72 29 z[n] = (char)
7c650 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f sqlite3UpperToLo
7c660 77 65 72 5b 28 75 38 29 7a 4d 6f 64 5b 6e 5d 5d wer[(u8)zMod[n]]
7c670 3b 0a 20 20 7d 0a 20 20 7a 5b 6e 5d 20 3d 20 30 ;. }. z[n] = 0
7c680 3b 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 30 5d ;. switch( z[0]
7c690 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ){.#ifndef SQLI
7c6a0 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d TE_OMIT_LOCALTIM
7c6b0 45 0a 20 20 20 20 63 61 73 65 20 27 6c 27 3a 20 E. case 'l':
7c6c0 7b 0a 20 20 20 20 20 20 2f 2a 20 20 20 20 6c 6f {. /* lo
7c6d0 63 61 6c 74 69 6d 65 0a 20 20 20 20 20 20 2a 2a caltime. **
7c6e0 0a 20 20 20 20 20 20 2a 2a 20 41 73 73 75 6d 69 . ** Assumi
7c6f0 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 ng the current t
7c700 69 6d 65 20 76 61 6c 75 65 20 69 73 20 55 54 43 ime value is UTC
7c710 20 28 61 2e 6b 2e 61 2e 20 47 4d 54 29 2c 20 73 (a.k.a. GMT), s
7c720 68 69 66 74 20 69 74 20 74 6f 0a 20 20 20 20 20 hift it to.
7c730 20 2a 2a 20 73 68 6f 77 20 6c 6f 63 61 6c 20 74 ** show local t
7c740 69 6d 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ime.. */.
7c750 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a if( strcmp(z
7c760 2c 20 22 6c 6f 63 61 6c 74 69 6d 65 22 29 3d 3d , "localtime")==
7c770 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6d 0 ){. com
7c780 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 puteJD(p);.
7c790 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 6c 6f 63 p->iJD += loc
7c7a0 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b altimeOffset(p);
7c7b0 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d . clearYM
7c7c0 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 D_HMS_TZ(p);.
7c7d0 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 rc = 0;.
7c7e0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
7c7f0 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
7c800 20 20 20 63 61 73 65 20 27 75 27 3a 20 7b 0a 20 case 'u': {.
7c810 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a /*. **
7c820 20 20 20 20 75 6e 69 78 65 70 6f 63 68 0a 20 20 unixepoch.
7c830 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
7c840 54 72 65 61 74 20 74 68 65 20 63 75 72 72 65 6e Treat the curren
7c850 74 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69 4a t value of p->iJ
7c860 44 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20 D as the number
7c870 6f 66 0a 20 20 20 20 20 20 2a 2a 20 73 65 63 6f of. ** seco
7c880 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 2e 20 nds since 1970.
7c890 20 43 6f 6e 76 65 72 74 20 74 6f 20 61 20 72 65 Convert to a re
7c8a0 61 6c 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 al julian day nu
7c8b0 6d 62 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 mber.. */.
7c8c0 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 if( strcmp(
7c8d0 7a 2c 20 22 75 6e 69 78 65 70 6f 63 68 22 29 3d z, "unixepoch")=
7c8e0 3d 30 20 26 26 20 70 2d 3e 76 61 6c 69 64 4a 44 =0 && p->validJD
7c8f0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 ){. p->i
7c900 4a 44 20 3d 20 28 70 2d 3e 69 4a 44 20 2b 20 34 JD = (p->iJD + 4
7c910 33 32 30 30 29 2f 38 36 34 30 30 20 2b 20 32 31 3200)/86400 + 21
7c920 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 30 30 086676*(i64)1000
7c930 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 63 6c 0000;. cl
7c940 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 earYMD_HMS_TZ(p)
7c950 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 ;. rc = 0
7c960 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 ;. }.#ifnde
7c970 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
7c980 43 41 4c 54 49 4d 45 0a 20 20 20 20 20 20 65 6c CALTIME. el
7c990 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c se if( strcmp(z,
7c9a0 20 22 75 74 63 22 29 3d 3d 30 20 29 7b 0a 20 20 "utc")==0 ){.
7c9b0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e sqlite3_in
7c9c0 74 36 34 20 63 31 3b 0a 20 20 20 20 20 20 20 20 t64 c1;.
7c9d0 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 computeJD(p);.
7c9e0 20 20 20 20 20 20 63 31 20 3d 20 6c 6f 63 61 6c c1 = local
7c9f0 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 timeOffset(p);.
7ca00 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d p->iJD -=
7ca10 20 63 31 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 c1;. cle
7ca20 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b arYMD_HMS_TZ(p);
7ca30 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 . p->iJD
7ca40 2b 3d 20 63 31 20 2d 20 6c 6f 63 61 6c 74 69 6d += c1 - localtim
7ca50 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20 eOffset(p);.
7ca60 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 rc = 0;.
7ca70 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 }.#endif.
7ca80 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
7ca90 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20 20 case 'w': {.
7caa0 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a 20 /*. **
7cab0 20 20 20 77 65 65 6b 64 61 79 20 4e 0a 20 20 20 weekday N.
7cac0 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d **. ** M
7cad0 6f 76 65 20 74 68 65 20 64 61 74 65 20 74 6f 20 ove the date to
7cae0 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 6f 6e the same time on
7caf0 20 74 68 65 20 6e 65 78 74 20 6f 63 63 75 72 72 the next occurr
7cb00 65 6e 63 65 20 6f 66 0a 20 20 20 20 20 20 2a 2a ence of. **
7cb10 20 77 65 65 6b 64 61 79 20 4e 20 77 68 65 72 65 weekday N where
7cb20 20 30 3d 3d 53 75 6e 64 61 79 2c 20 31 3d 3d 4d 0==Sunday, 1==M
7cb30 6f 6e 64 61 79 2c 20 61 6e 64 20 73 6f 20 66 6f onday, and so fo
7cb40 72 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20 20 rth. If the.
7cb50 20 20 20 2a 2a 20 64 61 74 65 20 69 73 20 61 6c ** date is al
7cb60 72 65 61 64 79 20 6f 6e 20 74 68 65 20 61 70 70 ready on the app
7cb70 72 6f 70 72 69 61 74 65 20 77 65 65 6b 64 61 79 ropriate weekday
7cb80 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f , this is a no-o
7cb90 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 p.. */.
7cba0 20 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c if( strncmp(z,
7cbb0 20 22 77 65 65 6b 64 61 79 20 22 2c 20 38 29 3d "weekday ", 8)=
7cbc0 3d 30 20 26 26 20 67 65 74 56 61 6c 75 65 28 26 =0 && getValue(&
7cbd0 7a 5b 38 5d 2c 26 72 29 3e 30 0a 20 20 20 20 20 z[8],&r)>0.
7cbe0 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 && (
7cbf0 6e 3d 28 69 6e 74 29 72 29 3d 3d 72 20 26 26 20 n=(int)r)==r &&
7cc00 6e 3e 3d 30 20 26 26 20 72 3c 37 20 29 7b 0a 20 n>=0 && r<7 ){.
7cc10 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 sqlite3_i
7cc20 6e 74 36 34 20 5a 3b 0a 20 20 20 20 20 20 20 20 nt64 Z;.
7cc30 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 computeYMD_HMS(p
7cc40 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 );. p->va
7cc50 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20 lidTZ = 0;.
7cc60 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 p->validJD =
7cc70 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 0;. compu
7cc80 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 teJD(p);.
7cc90 20 5a 20 3d 20 28 28 70 2d 3e 69 4a 44 20 2b 20 Z = ((p->iJD +
7cca0 31 32 39 36 30 30 30 30 30 29 2f 38 36 34 30 30 129600000)/86400
7ccb0 30 30 30 29 20 25 20 37 3b 0a 20 20 20 20 20 20 000) % 7;.
7ccc0 20 20 69 66 28 20 5a 3e 6e 20 29 20 5a 20 2d 3d if( Z>n ) Z -=
7ccd0 20 37 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 7;. p->i
7cce0 4a 44 20 2b 3d 20 28 6e 20 2d 20 5a 29 2a 38 36 JD += (n - Z)*86
7ccf0 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 400000;.
7cd00 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 clearYMD_HMS_TZ(
7cd10 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d p);. rc =
7cd20 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
7cd30 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
7cd40 20 20 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20 case 's': {.
7cd50 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a /*. **
7cd60 20 20 20 20 73 74 61 72 74 20 6f 66 20 54 54 54 start of TTT
7cd70 54 54 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 TT. **.
7cd80 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61 ** Move the da
7cd90 74 65 20 62 61 63 6b 77 61 72 64 73 20 74 6f 20 te backwards to
7cda0 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
7cdb0 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 79 the current day
7cdc0 2c 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d 6f ,. ** or mo
7cdd0 6e 74 68 20 6f 72 20 79 65 61 72 2e 0a 20 20 20 nth or year..
7cde0 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
7cdf0 73 74 72 6e 63 6d 70 28 7a 2c 20 22 73 74 61 72 strncmp(z, "star
7ce00 74 20 6f 66 20 22 2c 20 39 29 21 3d 30 20 29 20 t of ", 9)!=0 )
7ce10 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7a 20 2b break;. z +
7ce20 3d 20 39 3b 0a 20 20 20 20 20 20 63 6f 6d 70 75 = 9;. compu
7ce30 74 65 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 20 teYMD(p);.
7ce40 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b p->validHMS = 1;
7ce50 0a 20 20 20 20 20 20 70 2d 3e 68 20 3d 20 70 2d . p->h = p-
7ce60 3e 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d >m = 0;. p-
7ce70 3e 73 20 3d 20 30 2e 30 3b 0a 20 20 20 20 20 20 >s = 0.0;.
7ce80 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a p->validTZ = 0;.
7ce90 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 p->validJD
7cea0 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 = 0;. if(
7ceb0 73 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22 strcmp(z,"month"
7cec0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
7ced0 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20 p->D = 1;.
7cee0 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 rc = 0;.
7cef0 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 }else if( strcmp
7cf00 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 7b (z,"year")==0 ){
7cf10 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 . compute
7cf20 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 YMD(p);.
7cf30 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20 p->M = 1;.
7cf40 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20 p->D = 1;.
7cf50 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 rc = 0;.
7cf60 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 }else if( strc
7cf70 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20 29 mp(z,"day")==0 )
7cf80 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 {. rc = 0
7cf90 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
7cfa0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
7cfb0 20 63 61 73 65 20 27 2b 27 3a 0a 20 20 20 20 63 case '+':. c
7cfc0 61 73 65 20 27 2d 27 3a 0a 20 20 20 20 63 61 73 ase '-':. cas
7cfd0 65 20 27 30 27 3a 0a 20 20 20 20 63 61 73 65 20 e '0':. case
7cfe0 27 31 27 3a 0a 20 20 20 20 63 61 73 65 20 27 32 '1':. case '2
7cff0 27 3a 0a 20 20 20 20 63 61 73 65 20 27 33 27 3a ':. case '3':
7d000 0a 20 20 20 20 63 61 73 65 20 27 34 27 3a 0a 20 . case '4':.
7d010 20 20 20 63 61 73 65 20 27 35 27 3a 0a 20 20 20 case '5':.
7d020 20 63 61 73 65 20 27 36 27 3a 0a 20 20 20 20 63 case '6':. c
7d030 61 73 65 20 27 37 27 3a 0a 20 20 20 20 63 61 73 ase '7':. cas
7d040 65 20 27 38 27 3a 0a 20 20 20 20 63 61 73 65 20 e '8':. case
7d050 27 39 27 3a 20 7b 0a 20 20 20 20 20 20 64 6f 75 '9': {. dou
7d060 62 6c 65 20 72 52 6f 75 6e 64 65 72 3b 0a 20 20 ble rRounder;.
7d070 20 20 20 20 6e 20 3d 20 67 65 74 56 61 6c 75 65 n = getValue
7d080 28 7a 2c 20 26 72 29 3b 0a 20 20 20 20 20 20 61 (z, &r);. a
7d090 73 73 65 72 74 28 20 6e 3e 3d 31 20 29 3b 0a 20 ssert( n>=1 );.
7d0a0 20 20 20 20 20 69 66 28 20 7a 5b 6e 5d 3d 3d 27 if( z[n]=='
7d0b0 3a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a :' ){. /*
7d0c0 20 41 20 6d 6f 64 69 66 69 65 72 20 6f 66 20 74 A modifier of t
7d0d0 68 65 20 66 6f 72 6d 20 28 2b 7c 2d 29 48 48 3a he form (+|-)HH:
7d0e0 4d 4d 3a 53 53 2e 46 46 46 20 61 64 64 73 20 28 MM:SS.FFF adds (
7d0f0 6f 72 20 73 75 62 74 72 61 63 74 73 29 20 74 68 or subtracts) th
7d100 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 70 65 e. ** spe
7d110 63 69 66 69 65 64 20 6e 75 6d 62 65 72 20 6f 66 cified number of
7d120 20 68 6f 75 72 73 2c 20 6d 69 6e 75 74 65 73 2c hours, minutes,
7d130 20 73 65 63 6f 6e 64 73 2c 20 61 6e 64 20 66 72 seconds, and fr
7d140 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 actional seconds
7d150 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 . ** to t
7d160 68 65 20 74 69 6d 65 2e 20 20 54 68 65 20 22 2e he time. The ".
7d170 46 46 46 22 20 6d 61 79 20 62 65 20 6f 6d 69 74 FFF" may be omit
7d180 74 65 64 2e 20 20 54 68 65 20 22 3a 53 53 2e 46 ted. The ":SS.F
7d190 46 46 22 20 6d 61 79 20 62 65 0a 20 20 20 20 20 FF" may be.
7d1a0 20 20 20 2a 2a 20 6f 6d 69 74 74 65 64 2e 0a 20 ** omitted..
7d1b0 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
7d1c0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 const char *z2
7d1d0 20 3d 20 7a 3b 0a 20 20 20 20 20 20 20 20 44 61 = z;. Da
7d1e0 74 65 54 69 6d 65 20 74 78 3b 0a 20 20 20 20 20 teTime tx;.
7d1f0 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 sqlite3_int64
7d200 20 64 61 79 3b 0a 20 20 20 20 20 20 20 20 69 66 day;. if
7d210 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 69 ( !sqlite3Isdigi
7d220 74 28 2a 7a 32 29 20 29 20 7a 32 2b 2b 3b 0a 20 t(*z2) ) z2++;.
7d230 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 74 memset(&t
7d240 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 74 78 29 x, 0, sizeof(tx)
7d250 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
7d260 61 72 73 65 48 68 4d 6d 53 73 28 7a 32 2c 20 26 arseHhMmSs(z2, &
7d270 74 78 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 tx) ) break;.
7d280 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 computeJD(&
7d290 74 78 29 3b 0a 20 20 20 20 20 20 20 20 74 78 2e tx);. tx.
7d2a0 69 4a 44 20 2d 3d 20 34 33 32 30 30 30 30 30 3b iJD -= 43200000;
7d2b0 0a 20 20 20 20 20 20 20 20 64 61 79 20 3d 20 74 . day = t
7d2c0 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 3b 0a x.iJD/86400000;.
7d2d0 20 20 20 20 20 20 20 20 74 78 2e 69 4a 44 20 2d tx.iJD -
7d2e0 3d 20 64 61 79 2a 38 36 34 30 30 30 30 30 3b 0a = day*86400000;.
7d2f0 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b 30 5d if( z[0]
7d300 3d 3d 27 2d 27 20 29 20 74 78 2e 69 4a 44 20 3d =='-' ) tx.iJD =
7d310 20 2d 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20 20 -tx.iJD;.
7d320 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a computeJD(p);.
7d330 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 clearYMD
7d340 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 _HMS_TZ(p);.
7d350 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 74 78 p->iJD += tx
7d360 2e 69 4a 44 3b 0a 20 20 20 20 20 20 20 20 72 63 .iJD;. rc
7d370 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72 = 0;. br
7d380 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
7d390 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 z += n;.
7d3a0 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 while( sqlite3I
7d3b0 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b sspace(*z) ) z++
7d3c0 3b 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69 ;. n = sqli
7d3d0 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a te3Strlen30(z);.
7d3e0 20 20 20 20 20 20 69 66 28 20 6e 3e 31 30 20 7c if( n>10 |
7d3f0 7c 20 6e 3c 33 20 29 20 62 72 65 61 6b 3b 0a 20 | n<3 ) break;.
7d400 20 20 20 20 20 69 66 28 20 7a 5b 6e 2d 31 5d 3d if( z[n-1]=
7d410 3d 27 73 27 20 29 7b 20 7a 5b 6e 2d 31 5d 20 3d ='s' ){ z[n-1] =
7d420 20 30 3b 20 6e 2d 2d 3b 20 7d 0a 20 20 20 20 20 0; n--; }.
7d430 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 computeJD(p);.
7d440 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 rc = 0;.
7d450 20 20 20 72 52 6f 75 6e 64 65 72 20 3d 20 72 3c rRounder = r<
7d460 30 20 3f 20 2d 30 2e 35 20 3a 20 2b 30 2e 35 3b 0 ? -0.5 : +0.5;
7d470 0a 20 20 20 20 20 20 69 66 28 20 6e 3d 3d 33 20 . if( n==3
7d480 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 79 && strcmp(z,"day
7d490 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ")==0 ){.
7d4a0 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 p->iJD += (sqli
7d4b0 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 te3_int64)(r*864
7d4c0 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64 00000.0 + rRound
7d4d0 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 er);. }else
7d4e0 20 69 66 28 20 6e 3d 3d 34 20 26 26 20 73 74 72 if( n==4 && str
7d4f0 63 6d 70 28 7a 2c 22 68 6f 75 72 22 29 3d 3d 30 cmp(z,"hour")==0
7d500 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 ){. p->i
7d510 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 JD += (sqlite3_i
7d520 6e 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30 nt64)(r*(8640000
7d530 30 2e 30 2f 32 34 2e 30 29 20 2b 20 72 52 6f 75 0.0/24.0) + rRou
7d540 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c nder);. }el
7d550 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 73 se if( n==6 && s
7d560 74 72 63 6d 70 28 7a 2c 22 6d 69 6e 75 74 65 22 trcmp(z,"minute"
7d570 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
7d580 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 p->iJD += (sqlit
7d590 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 34 e3_int64)(r*(864
7d5a0 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 30 00000.0/(24.0*60
7d5b0 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72 29 .0)) + rRounder)
7d5c0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
7d5d0 28 20 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d 70 ( n==6 && strcmp
7d5e0 28 7a 2c 22 73 65 63 6f 6e 64 22 29 3d 3d 30 20 (z,"second")==0
7d5f0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a ){. p->iJ
7d600 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e D += (sqlite3_in
7d610 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30 30 t64)(r*(86400000
7d620 2e 30 2f 28 32 34 2e 30 2a 36 30 2e 30 2a 36 30 .0/(24.0*60.0*60
7d630 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72 29 .0)) + rRounder)
7d640 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
7d650 28 20 6e 3d 3d 35 20 26 26 20 73 74 72 63 6d 70 ( n==5 && strcmp
7d660 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 29 (z,"month")==0 )
7d670 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 2c {. int x,
7d680 20 79 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 y;. comp
7d690 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 uteYMD_HMS(p);.
7d6a0 20 20 20 20 20 20 20 70 2d 3e 4d 20 2b 3d 20 28 p->M += (
7d6b0 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 78 int)r;. x
7d6c0 20 3d 20 70 2d 3e 4d 3e 30 20 3f 20 28 70 2d 3e = p->M>0 ? (p->
7d6d0 4d 2d 31 29 2f 31 32 20 3a 20 28 70 2d 3e 4d 2d M-1)/12 : (p->M-
7d6e0 31 32 29 2f 31 32 3b 0a 20 20 20 20 20 20 20 20 12)/12;.
7d6f0 70 2d 3e 59 20 2b 3d 20 78 3b 0a 20 20 20 20 20 p->Y += x;.
7d700 20 20 20 70 2d 3e 4d 20 2d 3d 20 78 2a 31 32 3b p->M -= x*12;
7d710 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 . p->vali
7d720 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 dJD = 0;.
7d730 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 computeJD(p);.
7d740 20 20 20 20 20 20 20 79 20 3d 20 28 69 6e 74 29 y = (int)
7d750 72 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 79 r;. if( y
7d760 21 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 !=r ){.
7d770 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 p->iJD += (sqli
7d780 74 65 33 5f 69 6e 74 36 34 29 28 28 72 20 2d 20 te3_int64)((r -
7d790 79 29 2a 33 30 2e 30 2a 38 36 34 30 30 30 30 30 y)*30.0*86400000
7d7a0 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a .0 + rRounder);.
7d7b0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
7d7c0 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 26 }else if( n==4 &
7d7d0 26 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61 72 & strcmp(z,"year
7d7e0 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ")==0 ){.
7d7f0 20 69 6e 74 20 79 20 3d 20 28 69 6e 74 29 72 3b int y = (int)r;
7d800 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 . compute
7d810 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20 YMD_HMS(p);.
7d820 20 20 20 20 70 2d 3e 59 20 2b 3d 20 79 3b 0a 20 p->Y += y;.
7d830 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a p->validJ
7d840 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 63 D = 0;. c
7d850 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 omputeJD(p);.
7d860 20 20 20 20 20 69 66 28 20 79 21 3d 72 20 29 7b if( y!=r ){
7d870 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 4a . p->iJ
7d880 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e D += (sqlite3_in
7d890 74 36 34 29 28 28 72 20 2d 20 79 29 2a 33 36 35 t64)((r - y)*365
7d8a0 2e 30 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 .0*86400000.0 +
7d8b0 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 rRounder);.
7d8c0 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
7d8d0 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 31 {. rc = 1
7d8e0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
7d8f0 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 clearYMD_HMS_TZ(
7d900 70 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b p);. break;
7d910 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 . }. defau
7d920 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62 72 65 61 lt: {. brea
7d930 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 k;. }. }. r
7d940 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
7d950 2a 2a 20 50 72 6f 63 65 73 73 20 74 69 6d 65 20 ** Process time
7d960 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e function argumen
7d970 74 73 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20 ts. argv[0] is
7d980 61 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d a date-time stam
7d990 70 2e 0a 2a 2a 20 61 72 67 76 5b 31 5d 20 61 6e p..** argv[1] an
7d9a0 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 d following are
7d9b0 6d 6f 64 69 66 69 65 72 73 2e 20 20 50 61 72 73 modifiers. Pars
7d9c0 65 20 74 68 65 6d 20 61 6c 6c 20 61 6e 64 20 77 e them all and w
7d9d0 72 69 74 65 0a 2a 2a 20 74 68 65 20 72 65 73 75 rite.** the resu
7d9e0 6c 74 69 6e 67 20 74 69 6d 65 20 69 6e 74 6f 20 lting time into
7d9f0 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 72 the DateTime str
7da00 75 63 74 75 72 65 20 70 2e 20 20 52 65 74 75 72 ucture p. Retur
7da10 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 n 0.** on succes
7da20 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 65 s and 1 if there
7da30 20 61 72 65 20 61 6e 79 20 65 72 72 6f 72 73 2e are any errors.
7da40 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 .**.** If there
7da50 61 72 65 20 7a 65 72 6f 20 70 61 72 61 6d 65 74 are zero paramet
7da60 65 72 73 20 28 69 66 20 65 76 65 6e 20 61 72 67 ers (if even arg
7da70 76 5b 30 5d 20 69 73 20 75 6e 64 65 66 69 6e 65 v[0] is undefine
7da80 64 29 0a 2a 2a 20 74 68 65 6e 20 61 73 73 75 6d d).** then assum
7da90 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 e a default valu
7daa0 65 20 6f 66 20 22 6e 6f 77 22 20 66 6f 72 20 61 e of "now" for a
7dab0 72 67 76 5b 30 5d 2e 0a 2a 2f 0a 73 74 61 74 69 rgv[0]..*/.stati
7dac0 63 20 69 6e 74 20 69 73 44 61 74 65 28 0a 20 20 c int isDate(.
7dad0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
7dae0 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74 *context, . int
7daf0 20 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74 65 argc, . sqlite
7db00 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 2c 20 3_value **argv,
7db10 0a 20 20 44 61 74 65 54 69 6d 65 20 2a 70 0a 29 . DateTime *p.)
7db20 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e {. int i;. con
7db30 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
7db40 20 2a 7a 3b 0a 20 20 69 6e 74 20 65 54 79 70 65 *z;. int eType
7db50 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c ;. memset(p, 0,
7db60 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 sizeof(*p));.
7db70 69 66 28 20 61 72 67 63 3d 3d 30 20 29 7b 0a 20 if( argc==0 ){.
7db80 20 20 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f setDateTimeTo
7db90 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c Current(context,
7dba0 20 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 p);. }else if(
7dbb0 20 28 65 54 79 70 65 20 3d 20 73 71 6c 69 74 65 (eType = sqlite
7dbc0 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 3_value_type(arg
7dbd0 76 5b 30 5d 29 29 3d 3d 53 51 4c 49 54 45 5f 46 v[0]))==SQLITE_F
7dbe0 4c 4f 41 54 0a 20 20 20 20 20 20 20 20 20 20 20 LOAT.
7dbf0 20 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 || eType
7dc00 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 ==SQLITE_INTEGER
7dc10 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 3d ){. p->iJD =
7dc20 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 (sqlite3_int64)
7dc30 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 (sqlite3_value_d
7dc40 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 2a 38 ouble(argv[0])*8
7dc50 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 6400000.0 + 0.5)
7dc60 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 ;. p->validJD
7dc70 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 1;. }else{.
7dc80 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 z = sqlite3_v
7dc90 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 alue_text(argv[0
7dca0 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 20 7c ]);. if( !z |
7dcb0 7c 20 70 61 72 73 65 44 61 74 65 4f 72 54 69 6d | parseDateOrTim
7dcc0 65 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 e(context, (char
7dcd0 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 20 20 20 *)z, p) ){.
7dce0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
7dcf0 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 3b 20 . }. for(i=1;
7dd00 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 i<argc; i++){.
7dd10 20 20 69 66 28 20 28 7a 20 3d 20 73 71 6c 69 74 if( (z = sqlit
7dd20 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
7dd30 67 76 5b 69 5d 29 29 3d 3d 30 20 7c 7c 20 70 61 gv[i]))==0 || pa
7dd40 72 73 65 4d 6f 64 69 66 69 65 72 28 28 63 68 61 rseModifier((cha
7dd50 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 20 20 r*)z, p) ){.
7dd60 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
7dd70 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 }. }. return 0
7dd80 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ;.}.../*.** The
7dd90 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e following routin
7dda0 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 es implement the
7ddb0 20 76 61 72 69 6f 75 73 20 64 61 74 65 20 61 6e various date an
7ddc0 64 20 74 69 6d 65 20 66 75 6e 63 74 69 6f 6e 73 d time functions
7ddd0 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a .** of SQLite..*
7dde0 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 6a 75 6c 69 /../*.** juli
7ddf0 61 6e 64 61 79 28 20 54 49 4d 45 53 54 52 49 4e anday( TIMESTRIN
7de00 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e G, MOD, MOD, ...
7de10 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ).**.** Return t
7de20 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 he julian day nu
7de30 6d 62 65 72 20 6f 66 20 74 68 65 20 64 61 74 65 mber of the date
7de40 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 specified in th
7de50 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2f 0a 73 e arguments.*/.s
7de60 74 61 74 69 63 20 76 6f 69 64 20 6a 75 6c 69 61 tatic void julia
7de70 6e 64 61 79 46 75 6e 63 28 0a 20 20 73 71 6c 69 ndayFunc(. sqli
7de80 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
7de90 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 text,. int argc
7dea0 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
7deb0 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 e **argv.){. Da
7dec0 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 teTime x;. if(
7ded0 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 isDate(context,
7dee0 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d argc, argv, &x)=
7def0 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6d 70 75 74 =0 ){. comput
7df00 65 4a 44 28 26 78 29 3b 0a 20 20 20 20 73 71 6c eJD(&x);. sql
7df10 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 ite3_result_doub
7df20 6c 65 28 63 6f 6e 74 65 78 74 2c 20 78 2e 69 4a le(context, x.iJ
7df30 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20 D/86400000.0);.
7df40 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 64 }.}../*.** d
7df50 61 74 65 74 69 6d 65 28 20 54 49 4d 45 53 54 52 atetime( TIMESTR
7df60 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e ING, MOD, MOD, .
7df70 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e ..).**.** Return
7df80 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d YYYY-MM-DD HH:M
7df90 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 M:SS.*/.static v
7dfa0 6f 69 64 20 64 61 74 65 74 69 6d 65 46 75 6e 63 oid datetimeFunc
7dfb0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
7dfc0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
7dfd0 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 int argc,. sqli
7dfe0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
7dff0 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 .){. DateTime x
7e000 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63 ;. if( isDate(c
7e010 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 ontext, argc, ar
7e020 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 gv, &x)==0 ){.
7e030 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d char zBuf[100]
7e040 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 ;. computeYMD
7e050 5f 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73 71 _HMS(&x);. sq
7e060 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
7e070 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 izeof(zBuf), zBu
7e080 66 2c 20 22 25 30 34 64 2d 25 30 32 64 2d 25 30 f, "%04d-%02d-%0
7e090 32 64 20 25 30 32 64 3a 25 30 32 64 3a 25 30 32 2d %02d:%02d:%02
7e0a0 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 d",.
7e0b0 20 20 20 20 20 20 20 20 20 78 2e 59 2c 20 78 2e x.Y, x.
7e0c0 4d 2c 20 78 2e 44 2c 20 78 2e 68 2c 20 78 2e 6d M, x.D, x.h, x.m
7e0d0 2c 20 28 69 6e 74 29 28 78 2e 73 29 29 3b 0a 20 , (int)(x.s));.
7e0e0 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
7e0f0 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 t_text(context,
7e100 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 zBuf, -1, SQLITE
7e110 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d _TRANSIENT);. }
7e120 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 74 69 6d .}../*.** tim
7e130 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d e( TIMESTRING, M
7e140 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a OD, MOD, ...).**
7e150 0a 2a 2a 20 52 65 74 75 72 6e 20 48 48 3a 4d 4d .** Return HH:MM
7e160 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f :SS.*/.static vo
7e170 69 64 20 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 id timeFunc(. s
7e180 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
7e190 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 context,. int a
7e1a0 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 rgc,. sqlite3_v
7e1b0 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 alue **argv.){.
7e1c0 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 DateTime x;. i
7e1d0 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 f( isDate(contex
7e1e0 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 t, argc, argv, &
7e1f0 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 x)==0 ){. cha
7e200 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 r zBuf[100];.
7e210 20 63 6f 6d 70 75 74 65 48 4d 53 28 26 78 29 3b computeHMS(&x);
7e220 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
7e230 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 rintf(sizeof(zBu
7e240 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 32 64 3a f), zBuf, "%02d:
7e250 25 30 32 64 3a 25 30 32 64 22 2c 20 78 2e 68 2c %02d:%02d", x.h,
7e260 20 78 2e 6d 2c 20 28 69 6e 74 29 78 2e 73 29 3b x.m, (int)x.s);
7e270 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 . sqlite3_res
7e280 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 ult_text(context
7e290 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 , zBuf, -1, SQLI
7e2a0 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 TE_TRANSIENT);.
7e2b0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 64 }.}../*.** d
7e2c0 61 74 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c ate( TIMESTRING,
7e2d0 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a MOD, MOD, ...).
7e2e0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 59 59 59 **.** Return YYY
7e2f0 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a 73 74 61 74 69 Y-MM-DD.*/.stati
7e300 63 20 76 6f 69 64 20 64 61 74 65 46 75 6e 63 28 c void dateFunc(
7e310 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
7e320 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
7e330 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 nt argc,. sqlit
7e340 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a e3_value **argv.
7e350 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b ){. DateTime x;
7e360 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63 6f . if( isDate(co
7e370 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 ntext, argc, arg
7e380 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 v, &x)==0 ){.
7e390 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b char zBuf[100];
7e3a0 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 . computeYMD(
7e3b0 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 &x);. sqlite3
7e3c0 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 _snprintf(sizeof
7e3d0 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 (zBuf), zBuf, "%
7e3e0 30 34 64 2d 25 30 32 64 2d 25 30 32 64 22 2c 20 04d-%02d-%02d",
7e3f0 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 29 3b 0a x.Y, x.M, x.D);.
7e400 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
7e410 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c lt_text(context,
7e420 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 zBuf, -1, SQLIT
7e430 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 E_TRANSIENT);.
7e440 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 73 74 }.}../*.** st
7e450 72 66 74 69 6d 65 28 20 46 4f 52 4d 41 54 2c 20 rftime( FORMAT,
7e460 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c TIMESTRING, MOD,
7e470 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a MOD, ...).**.**
7e480 20 52 65 74 75 72 6e 20 61 20 73 74 72 69 6e 67 Return a string
7e490 20 64 65 73 63 72 69 62 65 64 20 62 79 20 46 4f described by FO
7e4a0 52 4d 41 54 2e 20 20 43 6f 6e 76 65 72 73 69 6f RMAT. Conversio
7e4b0 6e 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a ns as follows:.*
7e4c0 2a 0a 2a 2a 20 20 20 25 64 20 20 64 61 79 20 6f *.** %d day o
7e4d0 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 25 66 20 f month.** %f
7e4e0 20 2a 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 ** fractional s
7e4f0 65 63 6f 6e 64 73 20 20 53 53 2e 53 53 53 0a 2a econds SS.SSS.*
7e500 2a 20 20 20 25 48 20 20 68 6f 75 72 20 30 30 2d * %H hour 00-
7e510 32 34 0a 2a 2a 20 20 20 25 6a 20 20 64 61 79 20 24.** %j day
7e520 6f 66 20 79 65 61 72 20 30 30 30 2d 33 36 36 0a of year 000-366.
7e530 2a 2a 20 20 20 25 4a 20 20 2a 2a 20 4a 75 6c 69 ** %J ** Juli
7e540 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 0a 2a 2a an day number.**
7e550 20 20 20 25 6d 20 20 6d 6f 6e 74 68 20 30 31 2d %m month 01-
7e560 31 32 0a 2a 2a 20 20 20 25 4d 20 20 6d 69 6e 75 12.** %M minu
7e570 74 65 20 30 30 2d 35 39 0a 2a 2a 20 20 20 25 73 te 00-59.** %s
7e580 20 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 seconds since
7e590 31 39 37 30 2d 30 31 2d 30 31 0a 2a 2a 20 20 20 1970-01-01.**
7e5a0 25 53 20 20 73 65 63 6f 6e 64 73 20 30 30 2d 35 %S seconds 00-5
7e5b0 39 0a 2a 2a 20 20 20 25 77 20 20 64 61 79 20 6f 9.** %w day o
7e5c0 66 20 77 65 65 6b 20 30 2d 36 20 20 73 75 6e 64 f week 0-6 sund
7e5d0 61 79 3d 3d 30 0a 2a 2a 20 20 20 25 57 20 20 77 ay==0.** %W w
7e5e0 65 65 6b 20 6f 66 20 79 65 61 72 20 30 30 2d 35 eek of year 00-5
7e5f0 33 0a 2a 2a 20 20 20 25 59 20 20 79 65 61 72 20 3.** %Y year
7e600 30 30 30 30 2d 39 39 39 39 0a 2a 2a 20 20 20 25 0000-9999.** %
7e610 25 20 20 25 0a 2a 2f 0a 73 74 61 74 69 63 20 76 % %.*/.static v
7e620 6f 69 64 20 73 74 72 66 74 69 6d 65 46 75 6e 63 oid strftimeFunc
7e630 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
7e640 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
7e650 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 int argc,. sqli
7e660 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
7e670 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 .){. DateTime x
7e680 3b 0a 20 20 75 36 34 20 6e 3b 0a 20 20 73 69 7a ;. u64 n;. siz
7e690 65 5f 74 20 69 2c 6a 3b 0a 20 20 63 68 61 72 20 e_t i,j;. char
7e6a0 2a 7a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 *z;. sqlite3 *d
7e6b0 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 b;. const char
7e6c0 2a 7a 46 6d 74 20 3d 20 28 63 6f 6e 73 74 20 63 *zFmt = (const c
7e6d0 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c har*)sqlite3_val
7e6e0 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 ue_text(argv[0])
7e6f0 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 ;. char zBuf[10
7e700 30 5d 3b 0a 20 20 69 66 28 20 7a 46 6d 74 3d 3d 0];. if( zFmt==
7e710 30 20 7c 7c 20 69 73 44 61 74 65 28 63 6f 6e 74 0 || isDate(cont
7e720 65 78 74 2c 20 61 72 67 63 2d 31 2c 20 61 72 67 ext, argc-1, arg
7e730 76 2b 31 2c 20 26 78 29 20 29 20 72 65 74 75 72 v+1, &x) ) retur
7e740 6e 3b 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 n;. db = sqlite
7e750 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
7e760 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 dle(context);.
7e770 66 6f 72 28 69 3d 30 2c 20 6e 3d 31 3b 20 7a 46 for(i=0, n=1; zF
7e780 6d 74 5b 69 5d 3b 20 69 2b 2b 2c 20 6e 2b 2b 29 mt[i]; i++, n++)
7e790 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 {. if( zFmt[i
7e7a0 5d 3d 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 ]=='%' ){.
7e7b0 73 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 2b 31 switch( zFmt[i+1
7e7c0 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 ] ){. cas
7e7d0 65 20 27 64 27 3a 0a 20 20 20 20 20 20 20 20 63 e 'd':. c
7e7e0 61 73 65 20 27 48 27 3a 0a 20 20 20 20 20 20 20 ase 'H':.
7e7f0 20 63 61 73 65 20 27 6d 27 3a 0a 20 20 20 20 20 case 'm':.
7e800 20 20 20 63 61 73 65 20 27 4d 27 3a 0a 20 20 20 case 'M':.
7e810 20 20 20 20 20 63 61 73 65 20 27 53 27 3a 0a 20 case 'S':.
7e820 20 20 20 20 20 20 20 63 61 73 65 20 27 57 27 3a case 'W':
7e830 0a 20 20 20 20 20 20 20 20 20 20 6e 2b 2b 3b 0a . n++;.
7e840 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 61 6c /* fal
7e850 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20 20 l thru */.
7e860 20 20 63 61 73 65 20 27 77 27 3a 0a 20 20 20 20 case 'w':.
7e870 20 20 20 20 63 61 73 65 20 27 25 27 3a 0a 20 20 case '%':.
7e880 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
7e890 20 20 20 20 20 20 20 63 61 73 65 20 27 66 27 3a case 'f':
7e8a0 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 . n +=
7e8b0 38 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 8;. bre
7e8c0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
7e8d0 20 27 6a 27 3a 0a 20 20 20 20 20 20 20 20 20 20 'j':.
7e8e0 6e 20 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 n += 3;.
7e8f0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
7e900 20 63 61 73 65 20 27 59 27 3a 0a 20 20 20 20 20 case 'Y':.
7e910 20 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20 n += 8;.
7e920 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
7e930 20 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 0a case 's':.
7e940 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4a 27 case 'J'
7e950 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d :. n +=
7e960 20 35 30 3b 0a 20 20 20 20 20 20 20 20 20 20 62 50;. b
7e970 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 reak;. de
7e980 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 20 fault:.
7e990 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 45 52 52 return; /* ERR
7e9a0 4f 52 2e 20 20 72 65 74 75 72 6e 20 61 20 4e 55 OR. return a NU
7e9b0 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 LL */. }.
7e9c0 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 7d 0a 20 i++;. }.
7e9d0 20 7d 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e }. testcase( n
7e9e0 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66 29 2d 31 ==sizeof(zBuf)-1
7e9f0 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
7ea00 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66 29 20 n==sizeof(zBuf)
7ea10 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e );. testcase( n
7ea20 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69 ==(u64)db->aLimi
7ea30 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
7ea40 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20 20 74 65 ENGTH]+1 );. te
7ea50 73 74 63 61 73 65 28 20 6e 3d 3d 28 75 36 34 29 stcase( n==(u64)
7ea60 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
7ea70 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 E_LIMIT_LENGTH]
7ea80 29 3b 0a 20 20 69 66 28 20 6e 3c 73 69 7a 65 6f );. if( n<sizeo
7ea90 66 28 7a 42 75 66 29 20 29 7b 0a 20 20 20 20 7a f(zBuf) ){. z
7eaa0 20 3d 20 7a 42 75 66 3b 0a 20 20 7d 65 6c 73 65 = zBuf;. }else
7eab0 20 69 66 28 20 6e 3e 28 75 36 34 29 64 62 2d 3e if( n>(u64)db->
7eac0 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
7ead0 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 MIT_LENGTH] ){.
7eae0 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
7eaf0 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 t_error_toobig(c
7eb00 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 72 65 74 ontext);. ret
7eb10 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 urn;. }else{.
7eb20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 4d z = sqlite3DbM
7eb30 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 28 69 6e allocRaw(db, (in
7eb40 74 29 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d t)n);. if( z=
7eb50 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
7eb60 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
7eb70 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b _nomem(context);
7eb80 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 . return;.
7eb90 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6d 70 75 }. }. compu
7eba0 74 65 4a 44 28 26 78 29 3b 0a 20 20 63 6f 6d 70 teJD(&x);. comp
7ebb0 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a uteYMD_HMS(&x);.
7ebc0 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 7a 46 6d for(i=j=0; zFm
7ebd0 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 t[i]; i++){.
7ebe0 69 66 28 20 7a 46 6d 74 5b 69 5d 21 3d 27 25 27 if( zFmt[i]!='%'
7ebf0 20 29 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d ){. z[j++]
7ec00 20 3d 20 7a 46 6d 74 5b 69 5d 3b 0a 20 20 20 20 = zFmt[i];.
7ec10 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 2b 2b }else{. i++
7ec20 3b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 20 ;. switch(
7ec30 7a 46 6d 74 5b 69 5d 20 29 7b 0a 20 20 20 20 20 zFmt[i] ){.
7ec40 20 20 20 63 61 73 65 20 27 64 27 3a 20 20 73 71 case 'd': sq
7ec50 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 lite3_snprintf(3
7ec60 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 , &z[j],"%02d",x
7ec70 2e 44 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b .D); j+=2; break
7ec80 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 ;. case '
7ec90 66 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 f': {.
7eca0 64 6f 75 62 6c 65 20 73 20 3d 20 78 2e 73 3b 0a double s = x.s;.
7ecb0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 3e if( s>
7ecc0 35 39 2e 39 39 39 20 29 20 73 20 3d 20 35 39 2e 59.999 ) s = 59.
7ecd0 39 39 39 3b 0a 20 20 20 20 20 20 20 20 20 20 73 999;. s
7ece0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
7ecf0 37 2c 20 26 7a 5b 6a 5d 2c 22 25 30 36 2e 33 66 7, &z[j],"%06.3f
7ed00 22 2c 20 73 29 3b 0a 20 20 20 20 20 20 20 20 20 ", s);.
7ed10 20 6a 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 j += sqlite3Str
7ed20 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 len30(&z[j]);.
7ed30 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
7ed40 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
7ed50 20 63 61 73 65 20 27 48 27 3a 20 20 73 71 6c 69 case 'H': sqli
7ed60 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 te3_snprintf(3,
7ed70 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 68 &z[j],"%02d",x.h
7ed80 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a ); j+=2; break;.
7ed90 20 20 20 20 20 20 20 20 63 61 73 65 20 27 57 27 case 'W'
7eda0 3a 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 2a : /* Fall thru *
7edb0 2f 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 /. case '
7edc0 6a 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 j': {.
7edd0 69 6e 74 20 6e 44 61 79 3b 20 20 20 20 20 20 20 int nDay;
7ede0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
7edf0 6f 66 20 64 61 79 73 20 73 69 6e 63 65 20 31 73 of days since 1s
7ee00 74 20 64 61 79 20 6f 66 20 79 65 61 72 20 2a 2f t day of year */
7ee10 0a 20 20 20 20 20 20 20 20 20 20 44 61 74 65 54 . DateT
7ee20 69 6d 65 20 79 20 3d 20 78 3b 0a 20 20 20 20 20 ime y = x;.
7ee30 20 20 20 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d y.validJD =
7ee40 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e 0;. y.
7ee50 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 M = 1;.
7ee60 20 79 2e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20 y.D = 1;.
7ee70 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 79 computeJD(&y
7ee80 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 44 61 );. nDa
7ee90 79 20 3d 20 28 69 6e 74 29 28 28 78 2e 69 4a 44 y = (int)((x.iJD
7eea0 2d 79 2e 69 4a 44 2b 34 33 32 30 30 30 30 30 29 -y.iJD+43200000)
7eeb0 2f 38 36 34 30 30 30 30 30 29 3b 0a 20 20 20 20 /86400000);.
7eec0 20 20 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 if( zFmt[i
7eed0 5d 3d 3d 27 57 27 20 29 7b 0a 20 20 20 20 20 20 ]=='W' ){.
7eee0 20 20 20 20 20 20 69 6e 74 20 77 64 3b 20 20 20 int wd;
7eef0 2f 2a 20 30 3d 4d 6f 6e 64 61 79 2c 20 31 3d 54 /* 0=Monday, 1=T
7ef00 75 65 73 64 61 79 2c 20 2e 2e 2e 20 36 3d 53 75 uesday, ... 6=Su
7ef10 6e 64 61 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 nday */.
7ef20 20 20 20 20 77 64 20 3d 20 28 69 6e 74 29 28 28 wd = (int)((
7ef30 28 78 2e 69 4a 44 2b 34 33 32 30 30 30 30 30 29 (x.iJD+43200000)
7ef40 2f 38 36 34 30 30 30 30 30 29 25 37 29 3b 0a 20 /86400000)%7);.
7ef50 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
7ef60 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 e3_snprintf(3, &
7ef70 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28 6e 44 61 z[j],"%02d",(nDa
7ef80 79 2b 37 2d 77 64 29 2f 37 29 3b 0a 20 20 20 20 y+7-wd)/7);.
7ef90 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 32 3b 0a j += 2;.
7efa0 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
7efb0 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
7efc0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 34 2c ite3_snprintf(4,
7efd0 20 26 7a 5b 6a 5d 2c 22 25 30 33 64 22 2c 6e 44 &z[j],"%03d",nD
7efe0 61 79 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 ay+1);.
7eff0 20 20 20 6a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 j += 3;.
7f000 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
7f010 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
7f020 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 }. case '
7f030 4a 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 J': {.
7f040 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
7f050 28 32 30 2c 20 26 7a 5b 6a 5d 2c 22 25 2e 31 36 (20, &z[j],"%.16
7f060 67 22 2c 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 g",x.iJD/8640000
7f070 30 2e 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 0.0);.
7f080 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e j+=sqlite3Strlen
7f090 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 30(&z[j]);.
7f0a0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
7f0b0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 }. ca
7f0c0 73 65 20 27 6d 27 3a 20 20 73 71 6c 69 74 65 33 se 'm': sqlite3
7f0d0 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b _snprintf(3, &z[
7f0e0 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 4d 29 3b 20 j],"%02d",x.M);
7f0f0 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 j+=2; break;.
7f100 20 20 20 20 20 63 61 73 65 20 27 4d 27 3a 20 20 case 'M':
7f110 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
7f120 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 (3, &z[j],"%02d"
7f130 2c 78 2e 6d 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 ,x.m); j+=2; bre
7f140 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
7f150 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 's': {.
7f160 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
7f170 74 66 28 33 30 2c 26 7a 5b 6a 5d 2c 22 25 6c 6c tf(30,&z[j],"%ll
7f180 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 d",.
7f190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
7f1a0 69 36 34 29 28 78 2e 69 4a 44 2f 31 30 30 30 20 i64)(x.iJD/1000
7f1b0 2d 20 32 31 30 38 36 36 37 36 2a 28 69 36 34 29 - 21086676*(i64)
7f1c0 31 30 30 30 30 29 29 3b 0a 20 20 20 20 20 20 20 10000));.
7f1d0 20 20 20 6a 20 2b 3d 20 73 71 6c 69 74 65 33 53 j += sqlite3S
7f1e0 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a trlen30(&z[j]);.
7f1f0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
7f200 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
7f210 20 20 20 63 61 73 65 20 27 53 27 3a 20 20 73 71 case 'S': sq
7f220 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 lite3_snprintf(3
7f230 2c 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28 69 ,&z[j],"%02d",(i
7f240 6e 74 29 78 2e 73 29 3b 20 6a 2b 3d 32 3b 20 62 nt)x.s); j+=2; b
7f250 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 reak;. ca
7f260 73 65 20 27 77 27 3a 20 7b 0a 20 20 20 20 20 20 se 'w': {.
7f270 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 28 63 68 z[j++] = (ch
7f280 61 72 29 28 28 28 78 2e 69 4a 44 2b 31 32 39 36 ar)(((x.iJD+1296
7f290 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 00000)/86400000)
7f2a0 20 25 20 37 29 20 2b 20 27 30 27 3b 0a 20 20 20 % 7) + '0';.
7f2b0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
7f2c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
7f2d0 63 61 73 65 20 27 59 27 3a 20 7b 0a 20 20 20 20 case 'Y': {.
7f2e0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
7f2f0 70 72 69 6e 74 66 28 35 2c 26 7a 5b 6a 5d 2c 22 printf(5,&z[j],"
7f300 25 30 34 64 22 2c 78 2e 59 29 3b 20 6a 2b 3d 73 %04d",x.Y); j+=s
7f310 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 qlite3Strlen30(&
7f320 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 z[j]);.
7f330 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
7f340 7d 0a 20 20 20 20 20 20 20 20 64 65 66 61 75 6c }. defaul
7f350 74 3a 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 25 t: z[j++] = '%
7f360 27 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 '; break;.
7f370 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 7a 5b }. }. }. z[
7f380 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 j] = 0;. sqlite
7f390 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 3_result_text(co
7f3a0 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c 0a 20 20 ntext, z, -1,.
7f3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7f3c0 20 20 20 20 7a 3d 3d 7a 42 75 66 20 3f 20 53 51 z==zBuf ? SQ
7f3d0 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 3a LITE_TRANSIENT :
7f3e0 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 SQLITE_DYNAMIC)
7f3f0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 ;.}../*.** curre
7f400 6e 74 5f 74 69 6d 65 28 29 0a 2a 2a 0a 2a 2a 20 nt_time().**.**
7f410 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 This function re
7f420 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 turns the same v
7f430 61 6c 75 65 20 61 73 20 74 69 6d 65 28 27 6e 6f alue as time('no
7f440 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 w')..*/.static v
7f450 6f 69 64 20 63 74 69 6d 65 46 75 6e 63 28 0a 20 oid ctimeFunc(.
7f460 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
7f470 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 *context,. int
7f480 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 NotUsed,. sqli
7f490 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 te3_value **NotU
7f4a0 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 sed2.){. UNUSED
7f4b0 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 _PARAMETER2(NotU
7f4c0 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a sed, NotUsed2);.
7f4d0 20 20 74 69 6d 65 46 75 6e 63 28 63 6f 6e 74 65 timeFunc(conte
7f4e0 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a xt, 0, 0);.}../*
7f4f0 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 64 61 74 65 .** current_date
7f500 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 ().**.** This fu
7f510 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 nction returns t
7f520 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 he same value as
7f530 20 64 61 74 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f date('now')..*/
7f540 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 64 61 .static void cda
7f550 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 teFunc(. sqlite
7f560 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
7f570 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 xt,. int NotUse
7f580 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c d,. sqlite3_val
7f590 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b ue **NotUsed2.){
7f5a0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
7f5b0 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f TER2(NotUsed, No
7f5c0 74 55 73 65 64 32 29 3b 0a 20 20 64 61 74 65 46 tUsed2);. dateF
7f5d0 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 unc(context, 0,
7f5e0 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 0);.}../*.** cur
7f5f0 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28 29 rent_timestamp()
7f600 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
7f610 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 tion returns the
7f620 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20 64 same value as d
7f630 61 74 65 74 69 6d 65 28 27 6e 6f 77 27 29 2e 0a atetime('now')..
7f640 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
7f650 74 69 6d 65 73 74 61 6d 70 46 75 6e 63 28 0a 20 timestampFunc(.
7f660 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
7f670 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 *context,. int
7f680 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 NotUsed,. sqli
7f690 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 te3_value **NotU
7f6a0 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 sed2.){. UNUSED
7f6b0 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 _PARAMETER2(NotU
7f6c0 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a sed, NotUsed2);.
7f6d0 20 20 64 61 74 65 74 69 6d 65 46 75 6e 63 28 63 datetimeFunc(c
7f6e0 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d ontext, 0, 0);.}
7f6f0 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 .#endif /* !defi
7f700 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
7f710 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 29 20 DATETIME_FUNCS)
7f720 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */..#ifdef SQLIT
7f730 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f E_OMIT_DATETIME_
7f740 46 55 4e 43 53 0a 2f 2a 0a 2a 2a 20 49 66 20 74 FUNCS./*.** If t
7f750 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f he library is co
7f760 6d 70 69 6c 65 64 20 74 6f 20 6f 6d 69 74 20 74 mpiled to omit t
7f770 68 65 20 66 75 6c 6c 2d 73 63 61 6c 65 20 64 61 he full-scale da
7f780 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 68 te and time.** h
7f790 61 6e 64 6c 69 6e 67 20 28 74 6f 20 67 65 74 20 andling (to get
7f7a0 61 20 73 6d 61 6c 6c 65 72 20 62 69 6e 61 72 79 a smaller binary
7f7b0 29 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ), the following
7f7c0 20 6d 69 6e 69 6d 61 6c 20 76 65 72 73 69 6f 6e minimal version
7f7d0 0a 2a 2a 20 6f 66 20 74 68 65 20 66 75 6e 63 74 .** of the funct
7f7e0 69 6f 6e 73 20 63 75 72 72 65 6e 74 5f 74 69 6d ions current_tim
7f7f0 65 28 29 2c 20 63 75 72 72 65 6e 74 5f 64 61 74 e(), current_dat
7f800 65 28 29 20 61 6e 64 20 63 75 72 72 65 6e 74 5f e() and current_
7f810 74 69 6d 65 73 74 61 6d 70 28 29 0a 2a 2a 20 61 timestamp().** a
7f820 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 73 74 re included inst
7f830 65 61 64 2e 20 54 68 69 73 20 69 73 20 74 6f 20 ead. This is to
7f840 73 75 70 70 6f 72 74 20 63 6f 6c 75 6d 6e 20 64 support column d
7f850 65 63 6c 61 72 61 74 69 6f 6e 73 20 74 68 61 74 eclarations that
7f860 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 22 44 45 46 .** include "DEF
7f870 41 55 4c 54 20 43 55 52 52 45 4e 54 5f 54 49 4d AULT CURRENT_TIM
7f880 45 22 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 E" etc..**.** Th
7f890 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 73 is function uses
7f8a0 20 74 68 65 20 43 2d 6c 69 62 72 61 72 79 20 66 the C-library f
7f8b0 75 6e 63 74 69 6f 6e 73 20 74 69 6d 65 28 29 2c unctions time(),
7f8c0 20 67 6d 74 69 6d 65 28 29 0a 2a 2a 20 61 6e 64 gmtime().** and
7f8d0 20 73 74 72 66 74 69 6d 65 28 29 2e 20 54 68 65 strftime(). The
7f8e0 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 74 format string t
7f8f0 6f 20 70 61 73 73 20 74 6f 20 73 74 72 66 74 69 o pass to strfti
7f900 6d 65 28 29 20 69 73 20 73 75 70 70 6c 69 65 64 me() is supplied
7f910 0a 2a 2a 20 61 73 20 74 68 65 20 75 73 65 72 2d .** as the user-
7f920 64 61 74 61 20 66 6f 72 20 74 68 65 20 66 75 6e data for the fun
7f930 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ction..*/.static
7f940 20 76 6f 69 64 20 63 75 72 72 65 6e 74 54 69 6d void currentTim
7f950 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 eFunc(. sqlite3
7f960 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
7f970 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
7f980 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
7f990 2a 61 72 67 76 0a 29 7b 0a 20 20 74 69 6d 65 5f *argv.){. time_
7f9a0 74 20 74 3b 0a 20 20 63 68 61 72 20 2a 7a 46 6f t t;. char *zFo
7f9b0 72 6d 61 74 20 3d 20 28 63 68 61 72 20 2a 29 73 rmat = (char *)s
7f9c0 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 qlite3_user_data
7f9d0 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c (context);. sql
7f9e0 69 74 65 33 20 2a 64 62 3b 0a 20 20 64 6f 75 62 ite3 *db;. doub
7f9f0 6c 65 20 72 54 3b 0a 20 20 63 68 61 72 20 7a 42 le rT;. char zB
7fa00 75 66 5b 32 30 5d 3b 0a 0a 20 20 55 4e 55 53 45 uf[20];.. UNUSE
7fa10 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 D_PARAMETER(argc
7fa20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
7fa30 4d 45 54 45 52 28 61 72 67 76 29 3b 0a 0a 20 20 METER(argv);..
7fa40 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e db = sqlite3_con
7fa50 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 text_db_handle(c
7fa60 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 ontext);. sqlit
7fa70 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 e3OsCurrentTime(
7fa80 64 62 2d 3e 70 56 66 73 2c 20 26 72 54 29 3b 0a db->pVfs, &rT);.
7fa90 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
7faa0 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
7fab0 4e 54 0a 20 20 74 20 3d 20 38 36 34 30 30 2e 30 NT. t = 86400.0
7fac0 2a 28 72 54 20 2d 20 32 34 34 30 35 38 37 2e 35 *(rT - 2440587.5
7fad0 29 20 2b 20 30 2e 35 3b 0a 23 65 6c 73 65 0a 20 ) + 0.5;.#else.
7fae0 20 2f 2a 20 77 69 74 68 6f 75 74 20 66 6c 6f 61 /* without floa
7faf0 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f ting point suppo
7fb00 72 74 2c 20 72 54 20 77 69 6c 6c 20 68 61 76 65 rt, rT will have
7fb10 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 6c 6f . ** already lo
7fb20 73 74 20 66 72 61 63 74 69 6f 6e 61 6c 20 64 61 st fractional da
7fb30 79 20 70 72 65 63 69 73 69 6f 6e 2e 0a 20 20 2a y precision.. *
7fb40 2f 0a 20 20 74 20 3d 20 38 36 34 30 30 20 2a 20 /. t = 86400 *
7fb50 28 72 54 20 2d 20 32 34 34 30 35 38 37 29 20 2d (rT - 2440587) -
7fb60 20 34 33 32 30 30 3b 0a 23 65 6e 64 69 66 0a 23 43200;.#endif.#
7fb70 69 66 64 65 66 20 48 41 56 45 5f 47 4d 54 49 4d ifdef HAVE_GMTIM
7fb80 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 E_R. {. stru
7fb90 63 74 20 74 6d 20 73 4e 6f 77 3b 0a 20 20 20 20 ct tm sNow;.
7fba0 67 6d 74 69 6d 65 5f 72 28 26 74 2c 20 26 73 4e gmtime_r(&t, &sN
7fbb0 6f 77 29 3b 0a 20 20 20 20 73 74 72 66 74 69 6d ow);. strftim
7fbc0 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46 6f 72 e(zBuf, 20, zFor
7fbd0 6d 61 74 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 7d mat, &sNow);. }
7fbe0 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73 .#else. {. s
7fbf0 74 72 75 63 74 20 74 6d 20 2a 70 54 6d 3b 0a 20 truct tm *pTm;.
7fc00 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
7fc10 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 _enter(sqlite3Mu
7fc20 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
7fc30 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
7fc40 54 45 52 29 29 3b 0a 20 20 20 20 70 54 6d 20 3d TER));. pTm =
7fc50 20 67 6d 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 gmtime(&t);.
7fc60 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66 2c 20 strftime(zBuf,
7fc70 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 70 54 6d 20, zFormat, pTm
7fc80 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
7fc90 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 utex_leave(sqlit
7fca0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
7fcb0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
7fcc0 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d 0a 23 _MASTER));. }.#
7fcd0 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 endif.. sqlite3
7fce0 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e _result_text(con
7fcf0 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 text, zBuf, -1,
7fd00 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 SQLITE_TRANSIENT
7fd10 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a );.}.#endif../*.
7fd20 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
7fd30 20 72 65 67 69 73 74 65 72 65 64 20 61 6c 6c 20 registered all
7fd40 6f 66 20 74 68 65 20 61 62 6f 76 65 20 43 20 66 of the above C f
7fd50 75 6e 63 74 69 6f 6e 73 20 61 73 20 53 51 4c 0a unctions as SQL.
7fd60 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 ** functions. T
7fd70 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 74 68 his should be th
7fd80 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 e only routine i
7fd90 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 n this file with
7fda0 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e .** external lin
7fdb0 6b 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f kage..*/.SQLITE_
7fdc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
7fdd0 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74 65 ite3RegisterDate
7fde0 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 76 6f TimeFunctions(vo
7fdf0 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 53 51 id){. static SQ
7fe00 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 LITE_WSD FuncDef
7fe10 20 61 44 61 74 65 54 69 6d 65 46 75 6e 63 73 5b aDateTimeFuncs[
7fe20 5d 20 3d 20 7b 0a 23 69 66 6e 64 65 66 20 53 51 ] = {.#ifndef SQ
7fe30 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 LITE_OMIT_DATETI
7fe40 4d 45 5f 46 55 4e 43 53 0a 20 20 20 20 46 55 4e ME_FUNCS. FUN
7fe50 43 54 49 4f 4e 28 6a 75 6c 69 61 6e 64 61 79 2c CTION(julianday,
7fe60 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 -1, 0, 0
7fe70 2c 20 6a 75 6c 69 61 6e 64 61 79 46 75 6e 63 20 , juliandayFunc
7fe80 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 ),. FUNCTION(
7fe90 64 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 date,
7fea0 20 20 2d 31 2c 20 30 2c 20 30 2c 20 64 61 74 65 -1, 0, 0, date
7feb0 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 Func ),.
7fec0 20 46 55 4e 43 54 49 4f 4e 28 74 69 6d 65 2c 20 FUNCTION(time,
7fed0 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 -1,
7fee0 30 2c 20 30 2c 20 74 69 6d 65 46 75 6e 63 20 20 0, 0, timeFunc
7fef0 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
7ff00 49 4f 4e 28 64 61 74 65 74 69 6d 65 2c 20 20 20 ION(datetime,
7ff10 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 -1, 0, 0,
7ff20 64 61 74 65 74 69 6d 65 46 75 6e 63 20 20 29 2c datetimeFunc ),
7ff30 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 74 . FUNCTION(st
7ff40 72 66 74 69 6d 65 2c 20 20 20 20 20 20 20 20 20 rftime,
7ff50 2d 31 2c 20 30 2c 20 30 2c 20 73 74 72 66 74 69 -1, 0, 0, strfti
7ff60 6d 65 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 46 meFunc ),. F
7ff70 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f UNCTION(current_
7ff80 74 69 6d 65 2c 20 20 20 20 20 20 30 2c 20 30 2c time, 0, 0,
7ff90 20 30 2c 20 63 74 69 6d 65 46 75 6e 63 20 20 20 0, ctimeFunc
7ffa0 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
7ffb0 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 N(current_timest
7ffc0 61 6d 70 2c 20 30 2c 20 30 2c 20 30 2c 20 63 74 amp, 0, 0, 0, ct
7ffd0 69 6d 65 73 74 61 6d 70 46 75 6e 63 29 2c 0a 20 imestampFunc),.
7ffe0 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 FUNCTION(curr
7fff0 65 6e 74 5f 64 61 74 65 2c 20 20 20 20 20 20 30 ent_date, 0
80000 2c 20 30 2c 20 30 2c 20 63 64 61 74 65 46 75 6e , 0, 0, cdateFun
80010 63 20 20 20 20 20 29 2c 0a 23 65 6c 73 65 0a 20 c ),.#else.
80020 20 20 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28 STR_FUNCTION(
80030 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c 20 20 20 current_time,
80040 20 20 20 30 2c 20 22 25 48 3a 25 4d 3a 25 53 22 0, "%H:%M:%S"
80050 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 63 75 , 0, cu
80060 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 29 2c 0a rrentTimeFunc),.
80070 20 20 20 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e STR_FUNCTION
80080 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 (current_timesta
80090 6d 70 2c 20 30 2c 20 22 25 59 2d 25 6d 2d 25 64 mp, 0, "%Y-%m-%d
800a0 22 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 63 ", 0, c
800b0 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 29 2c urrentTimeFunc),
800c0 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 54 49 4f . STR_FUNCTIO
800d0 4e 28 63 75 72 72 65 6e 74 5f 64 61 74 65 2c 20 N(current_date,
800e0 20 20 20 20 20 30 2c 20 22 25 59 2d 25 6d 2d 25 0, "%Y-%m-%
800f0 64 20 25 48 3a 25 4d 3a 25 53 22 2c 20 30 2c 20 d %H:%M:%S", 0,
80100 63 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 29 currentTimeFunc)
80110 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 20 20 ,.#endif. };.
80120 69 6e 74 20 69 3b 0a 20 20 46 75 6e 63 44 65 66 int i;. FuncDef
80130 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 47 Hash *pHash = &G
80140 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 61 73 LOBAL(FuncDefHas
80150 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c h, sqlite3Global
80160 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 46 75 Functions);. Fu
80170 6e 63 44 65 66 20 2a 61 46 75 6e 63 20 3d 20 28 ncDef *aFunc = (
80180 46 75 6e 63 44 65 66 2a 29 26 47 4c 4f 42 41 4c FuncDef*)&GLOBAL
80190 28 46 75 6e 63 44 65 66 2c 20 61 44 61 74 65 54 (FuncDef, aDateT
801a0 69 6d 65 46 75 6e 63 73 29 3b 0a 0a 20 20 66 6f imeFuncs);.. fo
801b0 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 r(i=0; i<ArraySi
801c0 7a 65 28 61 44 61 74 65 54 69 6d 65 46 75 6e 63 ze(aDateTimeFunc
801d0 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 s); i++){. sq
801e0 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73 65 lite3FuncDefInse
801f0 72 74 28 70 48 61 73 68 2c 20 26 61 46 75 6e 63 rt(pHash, &aFunc
80200 5b 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a [i]);. }.}../**
80210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
80220 20 6f 66 20 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a of date.c *****
80230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
80260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
80270 69 6e 20 66 69 6c 65 20 6f 73 2e 63 20 2a 2a 2a in file os.c ***
80280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
802a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
802b0 2a 2a 20 32 30 30 35 20 4e 6f 76 65 6d 62 65 72 ** 2005 November
802c0 20 32 39 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 29.**.** The au
802d0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
802e0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
802f0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
80300 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
80310 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
80320 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
80330 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
80340 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
80350 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
80360 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
80370 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
80380 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
80390 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
803a0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
803b0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
803c0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
803d0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
803e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
803f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80420 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
80430 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
80440 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 63 6f OS interface co
80450 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f de that is commo
80460 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 61 72 63 68 n to all.** arch
80470 69 74 65 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a itectures..**.**
80480 20 24 49 64 3a 20 6f 73 2e 63 2c 76 20 31 2e 31 $Id: os.c,v 1.1
80490 32 37 20 32 30 30 39 2f 30 37 2f 32 37 20 31 31 27 2009/07/27 11
804a0 3a 34 31 3a 32 31 20 64 61 6e 69 65 6c 6b 31 39 :41:21 danielk19
804b0 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 64 65 66 77 Exp $.*/.#def
804c0 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43 ine _SQLITE_OS_C
804d0 5f 20 31 0a 23 75 6e 64 65 66 20 5f 53 51 4c 49 _ 1.#undef _SQLI
804e0 54 45 5f 4f 53 5f 43 5f 0a 0a 2f 2a 0a 2a 2a 20 TE_OS_C_../*.**
804f0 54 68 65 20 64 65 66 61 75 6c 74 20 53 51 4c 69 The default SQLi
80500 74 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 69 te sqlite3_vfs i
80510 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64 mplementations d
80520 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 0a 2a o not allocate.*
80530 2a 20 6d 65 6d 6f 72 79 20 28 61 63 74 75 61 6c * memory (actual
80540 6c 79 2c 20 6f 73 5f 75 6e 69 78 2e 63 20 61 6c ly, os_unix.c al
80550 6c 6f 63 61 74 65 73 20 61 20 73 6d 61 6c 6c 20 locates a small
80560 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 amount of memory
80570 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 .** from within
80580 4f 73 4f 70 65 6e 28 29 29 2c 20 62 75 74 20 73 OsOpen()), but s
80590 6f 6d 65 20 74 68 69 72 64 2d 70 61 72 74 79 20 ome third-party
805a0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
805b0 6d 61 79 2e 0a 2a 2a 20 53 6f 20 77 65 20 74 65 may..** So we te
805c0 73 74 20 74 68 65 20 65 66 66 65 63 74 73 20 6f st the effects o
805d0 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 f a malloc() fai
805e0 6c 69 6e 67 20 61 6e 64 20 74 68 65 20 73 71 6c ling and the sql
805f0 69 74 65 33 4f 73 58 58 58 28 29 0a 2a 2a 20 66 ite3OsXXX().** f
80600 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 69 6e unction returnin
80610 67 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e g SQLITE_IOERR_N
80620 4f 4d 45 4d 20 75 73 69 6e 67 20 74 68 65 20 44 OMEM using the D
80630 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
80640 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 macro..**.** Th
80650 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 e following func
80660 74 69 6f 6e 73 20 61 72 65 20 69 6e 73 74 72 75 tions are instru
80670 6d 65 6e 74 65 64 20 66 6f 72 20 6d 61 6c 6c 6f mented for mallo
80680 63 28 29 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20 c() failure .**
80690 74 65 73 74 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 testing:.**.**
806a0 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e sqlite3OsOpen
806b0 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 ().** sqlite
806c0 33 4f 73 52 65 61 64 28 29 0a 2a 2a 20 20 20 20 3OsRead().**
806d0 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
806e0 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 ).** sqlite3
806f0 4f 73 53 79 6e 63 28 29 0a 2a 2a 20 20 20 20 20 OsSync().**
80700 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 0a sqlite3OsLock().
80710 2a 2a 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 **.*/.#if define
80720 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 d(SQLITE_TEST) &
80730 26 20 28 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e & (SQLITE_OS_WIN
80740 3d 3d 30 29 0a 20 20 23 64 65 66 69 6e 65 20 44 ==0). #define D
80750 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
80760 28 78 29 20 69 66 20 28 21 78 20 7c 7c 20 21 73 (x) if (!x || !s
80770 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e qlite3IsMemJourn
80780 61 6c 28 78 29 29 20 7b 20 20 20 20 20 5c 0a 20 al(x)) { \.
80790 20 20 20 76 6f 69 64 20 2a 70 54 73 74 41 6c 6c void *pTstAll
807a0 6f 63 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c oc = sqlite3Mall
807b0 6f 63 28 31 30 29 3b 20 20 20 20 20 20 20 20 20 oc(10);
807c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
807d0 20 20 20 20 5c 0a 20 20 20 20 69 66 20 28 21 70 \. if (!p
807e0 54 73 74 41 6c 6c 6f 63 29 20 72 65 74 75 72 6e TstAlloc) return
807f0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f SQLITE_IOERR_NO
80800 4d 45 4d 3b 20 20 20 20 20 20 20 20 20 20 20 20 MEM;
80810 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
80820 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 sqlite3_free(pT
80830 73 74 41 6c 6c 6f 63 29 3b 20 20 20 20 20 20 20 stAlloc);
80840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80860 20 20 5c 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 \. }.#else.
80870 23 64 65 66 69 6e 65 20 44 4f 5f 4f 53 5f 4d 41 #define DO_OS_MA
80880 4c 4c 4f 43 5f 54 45 53 54 28 78 29 0a 23 65 6e LLOC_TEST(x).#en
80890 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 dif../*.** The f
808a0 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 ollowing routine
808b0 73 20 61 72 65 20 63 6f 6e 76 65 6e 69 65 6e 63 s are convenienc
808c0 65 20 77 72 61 70 70 65 72 73 20 61 72 6f 75 6e e wrappers aroun
808d0 64 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 d methods.** of
808e0 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 the sqlite3_file
808f0 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 69 object. This i
80900 73 20 6d 6f 73 74 6c 79 20 6a 75 73 74 20 73 79 s mostly just sy
80910 6e 74 61 63 74 69 63 20 73 75 67 61 72 2e 20 41 ntactic sugar. A
80920 6c 6c 0a 2a 2a 20 6f 66 20 74 68 69 73 20 77 6f ll.** of this wo
80930 75 6c 64 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c uld be completel
80940 79 20 61 75 74 6f 6d 61 74 69 63 20 69 66 20 53 y automatic if S
80950 51 4c 69 74 65 20 77 65 72 65 20 63 6f 64 65 64 QLite were coded
80960 20 75 73 69 6e 67 0a 2a 2a 20 43 2b 2b 20 69 6e using.** C++ in
80970 73 74 65 61 64 20 6f 66 20 70 6c 61 69 6e 20 6f stead of plain o
80980 6c 64 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ld C..*/.SQLITE_
80990 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
809a0 74 65 33 4f 73 43 6c 6f 73 65 28 73 71 6c 69 74 te3OsClose(sqlit
809b0 65 33 5f 66 69 6c 65 20 2a 70 49 64 29 7b 0a 20 e3_file *pId){.
809c0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
809d0 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 49 64 2d 3e _OK;. if( pId->
809e0 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 pMethods ){.
809f0 72 63 20 3d 20 70 49 64 2d 3e 70 4d 65 74 68 6f rc = pId->pMetho
80a00 64 73 2d 3e 78 43 6c 6f 73 65 28 70 49 64 29 3b ds->xClose(pId);
80a10 0a 20 20 20 20 70 49 64 2d 3e 70 4d 65 74 68 6f . pId->pMetho
80a20 64 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 ds = 0;. }. re
80a30 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 turn rc;.}.SQLIT
80a40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
80a50 6c 69 74 65 33 4f 73 52 65 61 64 28 73 71 6c 69 lite3OsRead(sqli
80a60 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 76 6f te3_file *id, vo
80a70 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d id *pBuf, int am
80a80 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 7b 0a t, i64 offset){.
80a90 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 DO_OS_MALLOC_T
80aa0 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 EST(id);. retur
80ab0 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e n id->pMethods->
80ac0 78 52 65 61 64 28 69 64 2c 20 70 42 75 66 2c 20 xRead(id, pBuf,
80ad0 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a amt, offset);.}.
80ae0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
80af0 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 nt sqlite3OsWrit
80b00 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
80b10 69 64 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a id, const void *
80b20 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 69 pBuf, int amt, i
80b30 36 34 20 6f 66 66 73 65 74 29 7b 0a 20 20 44 4f 64 offset){. DO
80b40 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 _OS_MALLOC_TEST(
80b50 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 id);. return id
80b60 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 57 72 69 ->pMethods->xWri
80b70 74 65 28 69 64 2c 20 70 42 75 66 2c 20 61 6d 74 te(id, pBuf, amt
80b80 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51 4c , offset);.}.SQL
80b90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
80ba0 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 sqlite3OsTruncat
80bb0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
80bc0 69 64 2c 20 69 36 34 20 73 69 7a 65 29 7b 0a 20 id, i64 size){.
80bd0 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 return id->pMet
80be0 68 6f 64 73 2d 3e 78 54 72 75 6e 63 61 74 65 28 hods->xTruncate(
80bf0 69 64 2c 20 73 69 7a 65 29 3b 0a 7d 0a 53 51 4c id, size);.}.SQL
80c00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
80c10 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 73 71 sqlite3OsSync(sq
80c20 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
80c30 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 44 4f int flags){. DO
80c40 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 _OS_MALLOC_TEST(
80c50 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 id);. return id
80c60 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 79 6e ->pMethods->xSyn
80c70 63 28 69 64 2c 20 66 6c 61 67 73 29 3b 0a 7d 0a c(id, flags);.}.
80c80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
80c90 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 nt sqlite3OsFile
80ca0 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c Size(sqlite3_fil
80cb0 65 20 2a 69 64 2c 20 69 36 34 20 2a 70 53 69 7a e *id, i64 *pSiz
80cc0 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c e){. DO_OS_MALL
80cd0 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72 OC_TEST(id);. r
80ce0 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f eturn id->pMetho
80cf0 64 73 2d 3e 78 46 69 6c 65 53 69 7a 65 28 69 64 ds->xFileSize(id
80d00 2c 20 70 53 69 7a 65 29 3b 0a 7d 0a 53 51 4c 49 , pSize);.}.SQLI
80d10 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
80d20 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 6c qlite3OsLock(sql
80d30 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
80d40 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20 nt lockType){.
80d50 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 DO_OS_MALLOC_TES
80d60 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 T(id);. return
80d70 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 4c id->pMethods->xL
80d80 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 70 65 ock(id, lockType
80d90 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
80da0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
80db0 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f sUnlock(sqlite3_
80dc0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f file *id, int lo
80dd0 63 6b 54 79 70 65 29 7b 0a 20 20 72 65 74 75 72 ckType){. retur
80de0 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e n id->pMethods->
80df0 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b xUnlock(id, lock
80e00 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f Type);.}.SQLITE_
80e10 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
80e20 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 te3OsCheckReserv
80e30 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 edLock(sqlite3_f
80e40 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 ile *id, int *pR
80e50 65 73 4f 75 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f esOut){. DO_OS_
80e60 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b MALLOC_TEST(id);
80e70 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d . return id->pM
80e80 65 74 68 6f 64 73 2d 3e 78 43 68 65 63 6b 52 65 ethods->xCheckRe
80e90 73 65 72 76 65 64 4c 6f 63 6b 28 69 64 2c 20 70 servedLock(id, p
80ea0 52 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 ResOut);.}.SQLIT
80eb0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
80ec0 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 lite3OsFileContr
80ed0 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ol(sqlite3_file
80ee0 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 *id, int op, voi
80ef0 64 20 2a 70 41 72 67 29 7b 0a 20 20 72 65 74 75 d *pArg){. retu
80f00 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d rn id->pMethods-
80f10 3e 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64 >xFileControl(id
80f20 2c 20 6f 70 2c 20 70 41 72 67 29 3b 0a 7d 0a 53 , op, pArg);.}.S
80f30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
80f40 74 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f t sqlite3OsSecto
80f50 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 rSize(sqlite3_fi
80f60 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 28 le *id){. int (
80f70 2a 78 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71 *xSectorSize)(sq
80f80 6c 69 74 65 33 5f 66 69 6c 65 2a 29 20 3d 20 69 lite3_file*) = i
80f90 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 65 d->pMethods->xSe
80fa0 63 74 6f 72 53 69 7a 65 3b 0a 20 20 72 65 74 75 ctorSize;. retu
80fb0 72 6e 20 28 78 53 65 63 74 6f 72 53 69 7a 65 20 rn (xSectorSize
80fc0 3f 20 78 53 65 63 74 6f 72 53 69 7a 65 28 69 64 ? xSectorSize(id
80fd0 29 20 3a 20 53 51 4c 49 54 45 5f 44 45 46 41 55 ) : SQLITE_DEFAU
80fe0 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 29 3b LT_SECTOR_SIZE);
80ff0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
81000 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 E int sqlite3OsD
81010 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
81020 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c tics(sqlite3_fil
81030 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e e *id){. return
81040 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 id->pMethods->x
81050 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
81060 73 74 69 63 73 28 69 64 29 3b 0a 7d 0a 0a 2f 2a stics(id);.}../*
81070 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 72 6f .** The next gro
81080 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 up of routines a
81090 72 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 re convenience w
810a0 72 61 70 70 65 72 73 20 61 72 6f 75 6e 64 20 74 rappers around t
810b0 68 65 0a 2a 2a 20 56 46 53 20 6d 65 74 68 6f 64 he.** VFS method
810c0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
810d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
810e0 4f 73 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 OsOpen(. sqlite
810f0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 3_vfs *pVfs, .
81100 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 const char *zPat
81110 68 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 h, . sqlite3_fi
81120 6c 65 20 2a 70 46 69 6c 65 2c 20 0a 20 20 69 6e le *pFile, . in
81130 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20 t flags, . int
81140 2a 70 46 6c 61 67 73 4f 75 74 0a 29 7b 0a 20 20 *pFlagsOut.){.
81150 69 6e 74 20 72 63 3b 0a 20 20 44 4f 5f 4f 53 5f int rc;. DO_OS_
81160 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 30 29 3b 0a MALLOC_TEST(0);.
81170 20 20 2f 2a 20 30 78 37 66 31 66 20 69 73 20 61 /* 0x7f1f is a
81180 20 6d 61 73 6b 20 6f 66 20 53 51 4c 49 54 45 5f mask of SQLITE_
81190 4f 50 45 4e 5f 20 66 6c 61 67 73 20 74 68 61 74 OPEN_ flags that
811a0 20 61 72 65 20 76 61 6c 69 64 20 74 6f 20 62 65 are valid to be
811b0 20 70 61 73 73 65 64 0a 20 20 2a 2a 20 64 6f 77 passed. ** dow
811c0 6e 20 69 6e 74 6f 20 74 68 65 20 56 46 53 20 6c n into the VFS l
811d0 61 79 65 72 2e 20 20 53 6f 6d 65 20 53 51 4c 49 ayer. Some SQLI
811e0 54 45 5f 4f 50 45 4e 5f 20 66 6c 61 67 73 20 28 TE_OPEN_ flags (
811f0 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 20 20 2a for example,. *
81200 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 * SQLITE_OPEN_FU
81210 4c 4c 4d 55 54 45 58 20 6f 72 20 53 51 4c 49 54 LLMUTEX or SQLIT
81220 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 E_OPEN_SHAREDCAC
81230 48 45 29 20 61 72 65 20 62 6c 6f 63 6b 65 64 20 HE) are blocked
81240 62 65 66 6f 72 65 0a 20 20 2a 2a 20 72 65 61 63 before. ** reac
81250 68 69 6e 67 20 74 68 65 20 56 46 53 2e 20 2a 2f hing the VFS. */
81260 0a 20 20 72 63 20 3d 20 70 56 66 73 2d 3e 78 4f . rc = pVfs->xO
81270 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 2c pen(pVfs, zPath,
81280 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 20 26 20 pFile, flags &
81290 30 78 37 66 31 66 2c 20 70 46 6c 61 67 73 4f 75 0x7f1f, pFlagsOu
812a0 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 t);. assert( rc
812b0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 ==SQLITE_OK || p
812c0 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d File->pMethods==
812d0 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 0 );. return rc
812e0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
812f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
81300 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 5f 76 Delete(sqlite3_v
81310 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 fs *pVfs, const
81320 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 69 6e 74 char *zPath, int
81330 20 64 69 72 53 79 6e 63 29 7b 0a 20 20 72 65 74 dirSync){. ret
81340 75 72 6e 20 70 56 66 73 2d 3e 78 44 65 6c 65 74 urn pVfs->xDelet
81350 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 64 e(pVfs, zPath, d
81360 69 72 53 79 6e 63 29 3b 0a 7d 0a 53 51 4c 49 54 irSync);.}.SQLIT
81370 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
81380 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 0a 20 lite3OsAccess(.
81390 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
813a0 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 fs, . const cha
813b0 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 69 6e 74 r *zPath, . int
813c0 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20 2a flags, . int *
813d0 70 52 65 73 4f 75 74 0a 29 7b 0a 20 20 44 4f 5f pResOut.){. DO_
813e0 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 30 OS_MALLOC_TEST(0
813f0 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 );. return pVfs
81400 2d 3e 78 41 63 63 65 73 73 28 70 56 66 73 2c 20 ->xAccess(pVfs,
81410 7a 50 61 74 68 2c 20 66 6c 61 67 73 2c 20 70 52 zPath, flags, pR
81420 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 esOut);.}.SQLITE
81430 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
81440 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 ite3OsFullPathna
81450 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 me(. sqlite3_vf
81460 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 s *pVfs, . cons
81470 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a t char *zPath, .
81480 20 20 69 6e 74 20 6e 50 61 74 68 4f 75 74 2c 20 int nPathOut,
81490 0a 20 20 63 68 61 72 20 2a 7a 50 61 74 68 4f 75 . char *zPathOu
814a0 74 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 t.){. return pV
814b0 66 73 2d 3e 78 46 75 6c 6c 50 61 74 68 6e 61 6d fs->xFullPathnam
814c0 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 6e e(pVfs, zPath, n
814d0 50 61 74 68 4f 75 74 2c 20 7a 50 61 74 68 4f 75 PathOut, zPathOu
814e0 74 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 t);.}.#ifndef SQ
814f0 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 LITE_OMIT_LOAD_E
81500 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f XTENSION.SQLITE_
81510 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
81520 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 lite3OsDlOpen(sq
81530 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
81540 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 const char *zPa
81550 74 68 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 th){. return pV
81560 66 73 2d 3e 78 44 6c 4f 70 65 6e 28 70 56 66 73 fs->xDlOpen(pVfs
81570 2c 20 7a 50 61 74 68 29 3b 0a 7d 0a 53 51 4c 49 , zPath);.}.SQLI
81580 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
81590 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 sqlite3OsDlError
815a0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
815b0 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 fs, int nByte, c
815c0 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 har *zBufOut){.
815d0 20 70 56 66 73 2d 3e 78 44 6c 45 72 72 6f 72 28 pVfs->xDlError(
815e0 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75 pVfs, nByte, zBu
815f0 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f fOut);.}.SQLITE_
81600 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 PRIVATE void (*s
81610 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 73 71 qlite3OsDlSym(sq
81620 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
81630 20 76 6f 69 64 20 2a 70 48 64 6c 65 2c 20 63 6f void *pHdle, co
81640 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 29 29 nst char *zSym))
81650 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e (void){. return
81660 20 70 56 66 73 2d 3e 78 44 6c 53 79 6d 28 70 56 pVfs->xDlSym(pV
81670 66 73 2c 20 70 48 64 6c 65 2c 20 7a 53 79 6d 29 fs, pHdle, zSym)
81680 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
81690 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f TE void sqlite3O
816a0 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 sDlClose(sqlite3
816b0 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 _vfs *pVfs, void
816c0 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 70 56 *pHandle){. pV
816d0 66 73 2d 3e 78 44 6c 43 6c 6f 73 65 28 70 56 66 fs->xDlClose(pVf
816e0 73 2c 20 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 s, pHandle);.}.#
816f0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
81700 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 OMIT_LOAD_EXTENS
81710 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ION */.SQLITE_PR
81720 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
81730 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 3OsRandomness(sq
81740 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
81750 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72 int nByte, char
81760 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 72 65 *zBufOut){. re
81770 74 75 72 6e 20 70 56 66 73 2d 3e 78 52 61 6e 64 turn pVfs->xRand
81780 6f 6d 6e 65 73 73 28 70 56 66 73 2c 20 6e 42 79 omness(pVfs, nBy
81790 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a te, zBufOut);.}.
817a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
817b0 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 nt sqlite3OsSlee
817c0 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 p(sqlite3_vfs *p
817d0 56 66 73 2c 20 69 6e 74 20 6e 4d 69 63 72 6f 29 Vfs, int nMicro)
817e0 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d {. return pVfs-
817f0 3e 78 53 6c 65 65 70 28 70 56 66 73 2c 20 6e 4d >xSleep(pVfs, nM
81800 69 63 72 6f 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f icro);.}.SQLITE_
81810 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
81820 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 te3OsCurrentTime
81830 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
81840 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 54 69 6d fs, double *pTim
81850 65 4f 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 eOut){. return
81860 70 56 66 73 2d 3e 78 43 75 72 72 65 6e 74 54 69 pVfs->xCurrentTi
81870 6d 65 28 70 56 66 73 2c 20 70 54 69 6d 65 4f 75 me(pVfs, pTimeOu
81880 74 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 t);.}..SQLITE_PR
81890 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
818a0 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 0a 20 3OsOpenMalloc(.
818b0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
818c0 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 fs, . const cha
818d0 72 20 2a 7a 46 69 6c 65 2c 20 0a 20 20 73 71 6c r *zFile, . sql
818e0 69 74 65 33 5f 66 69 6c 65 20 2a 2a 70 70 46 69 ite3_file **ppFi
818f0 6c 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 le, . int flags
81900 2c 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 ,. int *pOutFla
81910 67 73 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d gs.){. int rc =
81920 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
81930 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
81940 46 69 6c 65 3b 0a 20 20 70 46 69 6c 65 20 3d 20 File;. pFile =
81950 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 (sqlite3_file *)
81960 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 56 sqlite3Malloc(pV
81970 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20 fs->szOsFile);.
81980 20 69 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20 if( pFile ){.
81990 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
819a0 4f 70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65 Open(pVfs, zFile
819b0 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 , pFile, flags,
819c0 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20 pOutFlags);.
819d0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
819e0 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 K ){. sqlit
819f0 65 33 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a e3_free(pFile);.
81a00 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
81a10 20 2a 70 70 46 69 6c 65 20 3d 20 70 46 69 6c 65 *ppFile = pFile
81a20 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
81a30 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 turn rc;.}.SQLIT
81a40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
81a50 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 lite3OsCloseFree
81a60 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
81a70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 File){. int rc
81a80 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 = SQLITE_OK;. a
81a90 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a ssert( pFile );.
81aa0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
81ab0 43 6c 6f 73 65 28 70 46 69 6c 65 29 3b 0a 20 20 Close(pFile);.
81ac0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 sqlite3_free(pFi
81ad0 6c 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 le);. return rc
81ae0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
81af0 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 77 72 function is a wr
81b00 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65 apper around the
81b10 20 4f 53 20 73 70 65 63 69 66 69 63 20 69 6d 70 OS specific imp
81b20 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 0a 2a lementation of.*
81b30 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 * sqlite3_os_ini
81b40 74 28 29 2e 20 54 68 65 20 70 75 72 70 6f 73 65 t(). The purpose
81b50 20 6f 66 20 74 68 65 20 77 72 61 70 70 65 72 20 of the wrapper
81b60 69 73 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 is to provide th
81b70 65 0a 2a 2a 20 61 62 69 6c 69 74 79 20 74 6f 20 e.** ability to
81b80 73 69 6d 75 6c 61 74 65 20 61 20 6d 61 6c 6c 6f simulate a mallo
81b90 63 20 66 61 69 6c 75 72 65 2c 20 73 6f 20 74 68 c failure, so th
81ba0 61 74 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 20 at the handling
81bb0 6f 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 69 of an.** error i
81bc0 6e 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 n sqlite3_os_ini
81bd0 74 28 29 20 62 79 20 74 68 65 20 75 70 70 65 72 t() by the upper
81be0 20 6c 61 79 65 72 73 20 63 61 6e 20 62 65 20 74 layers can be t
81bf0 65 73 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ested..*/.SQLITE
81c00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
81c10 69 74 65 33 4f 73 49 6e 69 74 28 76 6f 69 64 29 ite3OsInit(void)
81c20 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 71 {. void *p = sq
81c30 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 31 30 29 lite3_malloc(10)
81c40 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
81c50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
81c60 45 4d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 EM;. sqlite3_fr
81c70 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ee(p);. return
81c80 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 sqlite3_os_init(
81c90 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 );.}../*.** The
81ca0 6c 69 73 74 20 6f 66 20 61 6c 6c 20 72 65 67 69 list of all regi
81cb0 73 74 65 72 65 64 20 56 46 53 20 69 6d 70 6c 65 stered VFS imple
81cc0 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 mentations..*/.s
81cd0 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66 tatic sqlite3_vf
81ce0 73 20 2a 20 53 51 4c 49 54 45 5f 57 53 44 20 76 s * SQLITE_WSD v
81cf0 66 73 4c 69 73 74 20 3d 20 30 3b 0a 23 64 65 66 fsList = 0;.#def
81d00 69 6e 65 20 76 66 73 4c 69 73 74 20 47 4c 4f 42 ine vfsList GLOB
81d10 41 4c 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a AL(sqlite3_vfs *
81d20 2c 20 76 66 73 4c 69 73 74 29 0a 0a 2f 2a 0a 2a , vfsList)../*.*
81d30 2a 20 4c 6f 63 61 74 65 20 61 20 56 46 53 20 62 * Locate a VFS b
81d40 79 20 6e 61 6d 65 2e 20 20 49 66 20 6e 6f 20 6e y name. If no n
81d50 61 6d 65 20 69 73 20 67 69 76 65 6e 2c 20 73 69 ame is given, si
81d60 6d 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65 0a mply return the.
81d70 2a 2a 20 66 69 72 73 74 20 56 46 53 20 6f 6e 20 ** first VFS on
81d80 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c the list..*/.SQL
81d90 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
81da0 76 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 73 vfs *sqlite3_vfs
81db0 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 72 _find(const char
81dc0 20 2a 7a 56 66 73 29 7b 0a 20 20 73 71 6c 69 74 *zVfs){. sqlit
81dd0 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 30 e3_vfs *pVfs = 0
81de0 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 ;.#if SQLITE_THR
81df0 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 EADSAFE. sqlite
81e00 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 3_mutex *mutex;.
81e10 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 #endif.#ifndef S
81e20 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 QLITE_OMIT_AUTOI
81e30 4e 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 NIT. int rc = s
81e40 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
81e50 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 e();. if( rc )
81e60 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 return 0;.#endif
81e70 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
81e80 41 44 53 41 46 45 0a 20 20 6d 75 74 65 78 20 3d ADSAFE. mutex =
81e90 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c sqlite3MutexAll
81ea0 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
81eb0 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a STATIC_MASTER);.
81ec0 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 #endif. sqlite3
81ed0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 _mutex_enter(mut
81ee0 65 78 29 3b 0a 20 20 66 6f 72 28 70 56 66 73 20 ex);. for(pVfs
81ef0 3d 20 76 66 73 4c 69 73 74 3b 20 70 56 66 73 3b = vfsList; pVfs;
81f00 20 70 56 66 73 3d 70 56 66 73 2d 3e 70 4e 65 78 pVfs=pVfs->pNex
81f10 74 29 7b 0a 20 20 20 20 69 66 28 20 7a 56 66 73 t){. if( zVfs
81f20 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 ==0 ) break;.
81f30 20 69 66 28 20 73 74 72 63 6d 70 28 7a 56 66 73 if( strcmp(zVfs
81f40 2c 20 70 56 66 73 2d 3e 7a 4e 61 6d 65 29 3d 3d , pVfs->zName)==
81f50 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 0 ) break;. }.
81f60 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
81f70 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 eave(mutex);. r
81f80 65 74 75 72 6e 20 70 56 66 73 3b 0a 7d 0a 0a 2f eturn pVfs;.}../
81f90 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 61 20 56 46 *.** Unlink a VF
81fa0 53 20 66 72 6f 6d 20 74 68 65 20 6c 69 6e 6b 65 S from the linke
81fb0 64 20 6c 69 73 74 0a 2a 2f 0a 73 74 61 74 69 63 d list.*/.static
81fc0 20 76 6f 69 64 20 76 66 73 55 6e 6c 69 6e 6b 28 void vfsUnlink(
81fd0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
81fe0 73 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 s){. assert( sq
81ff0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
82000 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c (sqlite3MutexAll
82010 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
82020 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 20 STATIC_MASTER))
82030 29 3b 0a 20 20 69 66 28 20 70 56 66 73 3d 3d 30 );. if( pVfs==0
82040 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70 ){. /* No-op
82050 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 */. }else if(
82060 76 66 73 4c 69 73 74 3d 3d 70 56 66 73 20 29 7b vfsList==pVfs ){
82070 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 70 . vfsList = p
82080 56 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 Vfs->pNext;. }e
82090 6c 73 65 20 69 66 28 20 76 66 73 4c 69 73 74 20 lse if( vfsList
820a0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 ){. sqlite3_v
820b0 66 73 20 2a 70 20 3d 20 76 66 73 4c 69 73 74 3b fs *p = vfsList;
820c0 0a 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 . while( p->p
820d0 4e 65 78 74 20 26 26 20 70 2d 3e 70 4e 65 78 74 Next && p->pNext
820e0 21 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 !=pVfs ){.
820f0 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 p = p->pNext;.
82100 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 }. if( p->p
82110 4e 65 78 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 Next==pVfs ){.
82120 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 p->pNext = p
82130 56 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 Vfs->pNext;.
82140 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 }. }.}../*.** R
82150 65 67 69 73 74 65 72 20 61 20 56 46 53 20 77 69 egister a VFS wi
82160 74 68 20 74 68 65 20 73 79 73 74 65 6d 2e 20 20 th the system.
82170 49 74 20 69 73 20 68 61 72 6d 6c 65 73 73 20 74 It is harmless t
82180 6f 20 72 65 67 69 73 74 65 72 20 74 68 65 20 73 o register the s
82190 61 6d 65 0a 2a 2a 20 56 46 53 20 6d 75 6c 74 69 ame.** VFS multi
821a0 70 6c 65 20 74 69 6d 65 73 2e 20 20 54 68 65 20 ple times. The
821b0 6e 65 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 new VFS becomes
821c0 74 68 65 20 64 65 66 61 75 6c 74 20 69 66 20 6d the default if m
821d0 61 6b 65 44 66 6c 74 20 69 73 0a 2a 2a 20 74 72 akeDflt is.** tr
821e0 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ue..*/.SQLITE_AP
821f0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 I int sqlite3_vf
82200 73 5f 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 s_register(sqlit
82210 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e e3_vfs *pVfs, in
82220 74 20 6d 61 6b 65 44 66 6c 74 29 7b 0a 20 20 73 t makeDflt){. s
82230 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
82240 74 65 78 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 tex = 0;.#ifndef
82250 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
82260 4f 49 4e 49 54 0a 20 20 69 6e 74 20 72 63 20 3d OINIT. int rc =
82270 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
82280 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 ize();. if( rc
82290 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e ) return rc;.#en
822a0 64 69 66 0a 20 20 6d 75 74 65 78 20 3d 20 73 71 dif. mutex = sq
822b0 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
822c0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
822d0 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 TIC_MASTER);. s
822e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
822f0 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 er(mutex);. vfs
82300 55 6e 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 Unlink(pVfs);.
82310 69 66 28 20 6d 61 6b 65 44 66 6c 74 20 7c 7c 20 if( makeDflt ||
82320 76 66 73 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 vfsList==0 ){.
82330 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 pVfs->pNext =
82340 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 76 66 73 vfsList;. vfs
82350 4c 69 73 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d List = pVfs;. }
82360 65 6c 73 65 7b 0a 20 20 20 20 70 56 66 73 2d 3e else{. pVfs->
82370 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74 2d pNext = vfsList-
82380 3e 70 4e 65 78 74 3b 0a 20 20 20 20 76 66 73 4c >pNext;. vfsL
82390 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66 ist->pNext = pVf
823a0 73 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 s;. }. assert(
823b0 76 66 73 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 vfsList);. sqli
823c0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
823d0 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e mutex);. return
823e0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
823f0 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20 *.** Unregister
82400 61 20 56 46 53 20 73 6f 20 74 68 61 74 20 69 74 a VFS so that it
82410 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 61 63 is no longer ac
82420 63 65 73 73 69 62 6c 65 2e 0a 2a 2f 0a 53 51 4c cessible..*/.SQL
82430 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
82440 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 te3_vfs_unregist
82450 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a er(sqlite3_vfs *
82460 70 56 66 73 29 7b 0a 23 69 66 20 53 51 4c 49 54 pVfs){.#if SQLIT
82470 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 E_THREADSAFE. s
82480 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
82490 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 tex = sqlite3Mut
824a0 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
824b0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
824c0 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 ER);.#endif. sq
824d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
824e0 72 28 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 55 r(mutex);. vfsU
824f0 6e 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 73 nlink(pVfs);. s
82500 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
82510 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 ve(mutex);. ret
82520 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
82530 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
82540 2a 20 45 6e 64 20 6f 66 20 6f 73 2e 63 20 2a 2a * End of os.c **
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82580 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
82590 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 61 75 * Begin file fau
825a0 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a lt.c ***********
825b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
825c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
825d0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 61 6e /./*.** 2008 Jan
825e0 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 22.**.** The au
825f0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
82600 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
82610 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
82620 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
82630 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
82640 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
82650 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
82660 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
82670 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
82680 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
82690 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
826a0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
826b0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
826c0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
826d0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
826e0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
826f0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
82700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82740 2a 2a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 66 61 **.**.** $Id: fa
82750 75 6c 74 2e 63 2c 76 20 31 2e 31 31 20 32 30 30 ult.c,v 1.11 200
82760 38 2f 30 39 2f 30 32 20 30 30 3a 35 32 3a 35 32 8/09/02 00:52:52
82770 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f drh Exp $.*/../
82780 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
82790 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 ontains code to
827a0 73 75 70 70 6f 72 74 20 74 68 65 20 63 6f 6e 63 support the conc
827b0 65 70 74 20 6f 66 20 22 62 65 6e 69 67 6e 22 20 ept of "benign"
827c0 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 .** malloc failu
827d0 72 65 73 20 28 77 68 65 6e 20 74 68 65 20 78 4d res (when the xM
827e0 61 6c 6c 6f 63 28 29 20 6f 72 20 78 52 65 61 6c alloc() or xReal
827f0 6c 6f 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 loc() method of
82800 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d the.** sqlite3_m
82810 65 6d 5f 6d 65 74 68 6f 64 73 20 73 74 72 75 63 em_methods struc
82820 74 75 72 65 20 66 61 69 6c 73 20 74 6f 20 61 6c ture fails to al
82830 6c 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b 20 6f locate a block o
82840 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6e 64 20 f memory.** and
82850 72 65 74 75 72 6e 73 20 30 29 2e 20 0a 2a 2a 0a returns 0). .**.
82860 2a 2a 20 4d 6f 73 74 20 6d 61 6c 6c 6f 63 20 66 ** Most malloc f
82870 61 69 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d ailures are non-
82880 62 65 6e 69 67 6e 2e 20 41 66 74 65 72 20 74 68 benign. After th
82890 65 79 20 6f 63 63 75 72 2c 20 53 51 4c 69 74 65 ey occur, SQLite
828a0 0a 2a 2a 20 61 62 61 6e 64 6f 6e 73 20 74 68 65 .** abandons the
828b0 20 63 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69 current operati
828c0 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 on and returns a
828d0 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 28 75 73 n error code (us
828e0 75 61 6c 6c 79 0a 2a 2a 20 53 51 4c 49 54 45 5f ually.** SQLITE_
828f0 4e 4f 4d 45 4d 29 20 74 6f 20 74 68 65 20 75 73 NOMEM) to the us
82900 65 72 2e 20 48 6f 77 65 76 65 72 2c 20 73 6f 6d er. However, som
82910 65 74 69 6d 65 73 20 61 20 66 61 75 6c 74 20 69 etimes a fault i
82920 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c s not necessaril
82930 79 0a 2a 2a 20 66 61 74 61 6c 2e 20 46 6f 72 20 y.** fatal. For
82940 65 78 61 6d 70 6c 65 2c 20 69 66 20 61 20 6d 61 example, if a ma
82950 6c 6c 6f 63 20 66 61 69 6c 73 20 77 68 69 6c 65 lloc fails while
82960 20 72 65 73 69 7a 69 6e 67 20 61 20 68 61 73 68 resizing a hash
82970 20 74 61 62 6c 65 2c 20 74 68 69 73 20 0a 2a 2a table, this .**
82980 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 72 is completely r
82990 65 63 6f 76 65 72 61 62 6c 65 20 73 69 6d 70 6c ecoverable simpl
829a0 79 20 62 79 20 6e 6f 74 20 63 61 72 72 79 69 6e y by not carryin
829b0 67 20 6f 75 74 20 74 68 65 20 72 65 73 69 7a 65 g out the resize
829c0 2e 20 54 68 65 20 0a 2a 2a 20 68 61 73 68 20 74 . The .** hash t
829d0 61 62 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e able will contin
829e0 75 65 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 6e ue to function n
829f0 6f 72 6d 61 6c 6c 79 2e 20 20 53 6f 20 61 20 6d ormally. So a m
82a00 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 0a 2a alloc failure .*
82a10 2a 20 64 75 72 69 6e 67 20 61 20 68 61 73 68 20 * during a hash
82a20 74 61 62 6c 65 20 72 65 73 69 7a 65 20 69 73 20 table resize is
82a30 61 20 62 65 6e 69 67 6e 20 66 61 75 6c 74 2e 0a a benign fault..
82a40 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */...#ifndef SQL
82a50 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e ITE_OMIT_BUILTIN
82a60 5f 54 45 53 54 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f _TEST../*.** Glo
82a70 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a bal variables..*
82a80 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
82a90 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f BenignMallocHoo
82aa0 6b 73 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 ks BenignMallocH
82ab0 6f 6f 6b 73 3b 0a 73 74 61 74 69 63 20 53 51 4c ooks;.static SQL
82ac0 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 42 ITE_WSD struct B
82ad0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 enignMallocHooks
82ae0 20 7b 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e {. void (*xBen
82af0 69 67 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 3b ignBegin)(void);
82b00 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 . void (*xBenig
82b10 6e 45 6e 64 29 28 76 6f 69 64 29 3b 0a 7d 20 73 nEnd)(void);.} s
82b20 71 6c 69 74 65 33 48 6f 6f 6b 73 20 3d 20 7b 20 qlite3Hooks = {
82b30 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 20 54 68 65 20 0, 0 };../* The
82b40 22 77 73 64 48 6f 6f 6b 73 22 20 6d 61 63 72 6f "wsdHooks" macro
82b50 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f will resolve to
82b60 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 the appropriate
82b70 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f BenignMallocHoo
82b80 6b 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e ks.** structure.
82b90 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 73 74 If writable st
82ba0 61 74 69 63 20 64 61 74 61 20 69 73 20 75 6e 73 atic data is uns
82bb0 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 upported on the
82bc0 74 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61 target,.** we ha
82bd0 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 ve to locate the
82be0 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 74 state vector at
82bf0 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 run-time. In t
82c00 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a he more common.*
82c10 2a 20 63 61 73 65 20 77 68 65 72 65 20 77 72 69 * case where wri
82c20 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 table static dat
82c30 61 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c 20 a is supported,
82c40 77 73 64 48 6f 6f 6b 73 20 63 61 6e 20 72 65 66 wsdHooks can ref
82c50 65 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74 er directly.** t
82c60 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33 48 6f o the "sqlite3Ho
82c70 6f 6b 73 22 20 73 74 61 74 65 20 76 65 63 74 6f oks" state vecto
82c80 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f 76 65 r declared above
82c90 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
82ca0 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65 TE_OMIT_WSD.# de
82cb0 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69 fine wsdHooksIni
82cc0 74 20 5c 0a 20 20 42 65 6e 69 67 6e 4d 61 6c 6c t \. BenignMall
82cd0 6f 63 48 6f 6f 6b 73 20 2a 78 20 3d 20 26 47 4c ocHooks *x = &GL
82ce0 4f 42 41 4c 28 42 65 6e 69 67 6e 4d 61 6c 6c 6f OBAL(BenignMallo
82cf0 63 48 6f 6f 6b 73 2c 73 71 6c 69 74 65 33 48 6f cHooks,sqlite3Ho
82d00 6f 6b 73 29 0a 23 20 64 65 66 69 6e 65 20 77 73 oks).# define ws
82d10 64 48 6f 6f 6b 73 20 78 5b 30 5d 0a 23 65 6c 73 dHooks x[0].#els
82d20 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f e.# define wsdHo
82d30 6f 6b 73 49 6e 69 74 0a 23 20 64 65 66 69 6e 65 oksInit.# define
82d40 20 77 73 64 48 6f 6f 6b 73 20 73 71 6c 69 74 65 wsdHooks sqlite
82d50 33 48 6f 6f 6b 73 0a 23 65 6e 64 69 66 0a 0a 0a 3Hooks.#endif...
82d60 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 68 /*.** Register h
82d70 6f 6f 6b 73 20 74 6f 20 63 61 6c 6c 20 77 68 65 ooks to call whe
82d80 6e 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 n sqlite3BeginBe
82d90 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 nignMalloc() and
82da0 0a 2a 2a 20 73 71 6c 69 74 65 33 45 6e 64 42 65 .** sqlite3EndBe
82db0 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 72 65 nignMalloc() are
82dc0 20 63 61 6c 6c 65 64 2c 20 72 65 73 70 65 63 74 called, respect
82dd0 69 76 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ively..*/.SQLITE
82de0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
82df0 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f lite3BenignMallo
82e00 63 48 6f 6f 6b 73 28 0a 20 20 76 6f 69 64 20 28 cHooks(. void (
82e10 2a 78 42 65 6e 69 67 6e 42 65 67 69 6e 29 28 76 *xBenignBegin)(v
82e20 6f 69 64 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 oid),. void (*x
82e30 42 65 6e 69 67 6e 45 6e 64 29 28 76 6f 69 64 29 BenignEnd)(void)
82e40 0a 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e .){. wsdHooksIn
82e50 69 74 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 2e 78 it;. wsdHooks.x
82e60 42 65 6e 69 67 6e 42 65 67 69 6e 20 3d 20 78 42 BenignBegin = xB
82e70 65 6e 69 67 6e 42 65 67 69 6e 3b 0a 20 20 77 73 enignBegin;. ws
82e80 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e dHooks.xBenignEn
82e90 64 20 3d 20 78 42 65 6e 69 67 6e 45 6e 64 3b 0a d = xBenignEnd;.
82ea0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 28 73 }../*.** This (s
82eb0 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d qlite3EndBenignM
82ec0 61 6c 6c 6f 63 28 29 29 20 69 73 20 63 61 6c 6c alloc()) is call
82ed0 65 64 20 62 79 20 53 51 4c 69 74 65 20 63 6f 64 ed by SQLite cod
82ee0 65 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 e to indicate th
82ef0 61 74 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 at.** subsequent
82f00 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 malloc failures
82f10 20 61 72 65 20 62 65 6e 69 67 6e 2e 20 41 20 63 are benign. A c
82f20 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 45 6e all to sqlite3En
82f30 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a dBenignMalloc().
82f40 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 ** indicates tha
82f50 74 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c t subsequent mal
82f60 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72 65 loc failures are
82f70 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 0a 2a 2f 0a non-benign..*/.
82f80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
82f90 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e oid sqlite3Begin
82fa0 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 BenignMalloc(voi
82fb0 64 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e d){. wsdHooksIn
82fc0 69 74 3b 0a 20 20 69 66 28 20 77 73 64 48 6f 6f it;. if( wsdHoo
82fd0 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e 20 ks.xBenignBegin
82fe0 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e ){. wsdHooks.
82ff0 78 42 65 6e 69 67 6e 42 65 67 69 6e 28 29 3b 0a xBenignBegin();.
83000 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 }.}.SQLITE_PRI
83010 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
83020 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 3EndBenignMalloc
83030 28 76 6f 69 64 29 7b 0a 20 20 77 73 64 48 6f 6f (void){. wsdHoo
83040 6b 73 49 6e 69 74 3b 0a 20 20 69 66 28 20 77 73 ksInit;. if( ws
83050 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e dHooks.xBenignEn
83060 64 20 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b d ){. wsdHook
83070 73 2e 78 42 65 6e 69 67 6e 45 6e 64 28 29 3b 0a s.xBenignEnd();.
83080 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 20 20 }.}..#endif
83090 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /* #ifndef SQLIT
830a0 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 E_OMIT_BUILTIN_T
830b0 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a EST */../*******
830c0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 ******* End of f
830d0 61 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ault.c *********
830e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
830f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83100 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
83110 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
83120 6c 65 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a le mem0.c ******
83130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83150 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
83160 30 38 20 4f 63 74 6f 62 65 72 20 32 38 0a 2a 2a 08 October 28.**
83170 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
83180 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
83190 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
831a0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
831b0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
831c0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
831d0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
831e0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
831f0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
83200 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
83210 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
83220 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
83230 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
83240 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
83250 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
83260 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
83270 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
83280 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
83290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
832a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
832b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
832c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
832d0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
832e0 74 61 69 6e 73 20 61 20 6e 6f 2d 6f 70 20 6d 65 tains a no-op me
832f0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
83300 64 72 69 76 65 72 73 20 66 6f 72 20 75 73 65 20 drivers for use
83310 77 68 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 5a when.** SQLITE_Z
83320 45 52 4f 5f 4d 41 4c 4c 4f 43 20 69 73 20 64 65 ERO_MALLOC is de
83330 66 69 6e 65 64 2e 20 20 54 68 65 20 61 6c 6c 6f fined. The allo
83340 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 69 cation drivers i
83350 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 68 65 mplemented.** he
83360 72 65 20 61 6c 77 61 79 73 20 66 61 69 6c 2e 20 re always fail.
83370 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 SQLite will not
83380 20 6f 70 65 72 61 74 65 20 77 69 74 68 20 74 68 operate with th
83390 65 73 65 20 64 72 69 76 65 72 73 2e 20 20 54 68 ese drivers. Th
833a0 65 73 65 0a 2a 2a 20 61 72 65 20 6d 65 72 65 6c ese.** are merel
833b0 79 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 2e 20 y placeholders.
833c0 20 52 65 61 6c 20 64 72 69 76 65 72 73 20 6d 75 Real drivers mu
833d0 73 74 20 62 65 20 73 75 62 73 74 69 74 75 74 65 st be substitute
833e0 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 d using.** sqlit
833f0 65 33 5f 63 6f 6e 66 69 67 28 29 20 62 65 66 6f e3_config() befo
83400 72 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f re SQLite will o
83410 70 65 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 perate..**.** $I
83420 64 3a 20 6d 65 6d 30 2e 63 2c 76 20 31 2e 31 20 d: mem0.c,v 1.1
83430 32 30 30 38 2f 31 30 2f 32 38 20 31 38 3a 35 38 2008/10/28 18:58
83440 3a 32 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :20 drh Exp $.*/
83450 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 ../*.** This ver
83460 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f sion of the memo
83470 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 ry allocator is
83480 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 49 74 the default. It
83490 20 69 73 0a 2a 2a 20 75 73 65 64 20 77 68 65 6e is.** used when
834a0 20 6e 6f 20 6f 74 68 65 72 20 6d 65 6d 6f 72 79 no other memory
834b0 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 73 70 allocator is sp
834c0 65 63 69 66 69 65 64 20 75 73 69 6e 67 20 63 6f ecified using co
834d0 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d 61 mpile-time.** ma
834e0 63 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 cros..*/.#ifdef
834f0 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c SQLITE_ZERO_MALL
83500 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20 OC../*.** No-op
83510 76 65 72 73 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 versions of all
83520 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
83530 6e 20 72 6f 75 74 69 6e 65 73 0a 2a 2f 0a 73 74 n routines.*/.st
83540 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 atic void *sqlit
83550 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 e3MemMalloc(int
83560 6e 42 79 74 65 29 7b 20 72 65 74 75 72 6e 20 30 nByte){ return 0
83570 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ; }.static void
83580 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 76 sqlite3MemFree(v
83590 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 72 65 oid *pPrior){ re
835a0 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63 20 76 turn; }.static v
835b0 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 oid *sqlite3MemR
835c0 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 ealloc(void *pPr
835d0 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b ior, int nByte){
835e0 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 return 0; }.sta
835f0 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d tic int sqlite3M
83600 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 50 72 emSize(void *pPr
83610 69 6f 72 29 7b 20 72 65 74 75 72 6e 20 30 3b 20 ior){ return 0;
83620 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c }.static int sql
83630 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 ite3MemRoundup(i
83640 6e 74 20 6e 29 7b 20 72 65 74 75 72 6e 20 6e 3b nt n){ return n;
83650 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 }.static int sq
83660 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69 lite3MemInit(voi
83670 64 20 2a 4e 6f 74 55 73 65 64 29 7b 20 72 65 74 d *NotUsed){ ret
83680 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d urn SQLITE_OK; }
83690 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c .static void sql
836a0 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 ite3MemShutdown(
836b0 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 20 void *NotUsed){
836c0 72 65 74 75 72 6e 3b 20 7d 0a 0a 2f 2a 0a 2a 2a return; }../*.**
836d0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
836e0 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e the only routin
836f0 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 e in this file w
83700 69 74 68 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e ith external lin
83710 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 kage..**.** Popu
83720 6c 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 late the low-lev
83730 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 el memory alloca
83740 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f tion function po
83750 69 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c inters in.** sql
83760 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
83770 2e 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 .m with pointers
83780 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 to the routines
83790 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a in this file..*
837a0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
837b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
837c0 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 SetDefault(void)
837d0 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 {. static const
837e0 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 sqlite3_mem_met
837f0 68 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 hods defaultMeth
83800 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c ods = {. sql
83810 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 ite3MemMalloc,.
83820 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 sqlite3MemFr
83830 65 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 ee,. sqlite3
83840 4d 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 MemRealloc,.
83850 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c sqlite3MemSize,
83860 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
83870 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 Roundup,. sq
83880 6c 69 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 lite3MemInit,.
83890 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 sqlite3MemShu
838a0 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 tdown,. 0.
838b0 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e };. sqlite3_con
838c0 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 fig(SQLITE_CONFI
838d0 47 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 G_MALLOC, &defau
838e0 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 ltMethods);.}..#
838f0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
83900 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a ZERO_MALLOC */..
83910 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
83920 45 6e 64 20 6f 66 20 6d 65 6d 30 2e 63 20 2a 2a End of mem0.c **
83930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
83960 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
83970 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 31 2e Begin file mem1.
83980 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
83990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
839a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
839b0 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 /*.** 2007 Augus
839c0 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 t 14.**.** The a
839d0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
839e0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
839f0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
83a00 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
83a10 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
83a20 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
83a30 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
83a40 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
83a50 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
83a60 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
83a70 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
83a80 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
83a90 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
83aa0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
83ab0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
83ac0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
83ad0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
83ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
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 2a ****************
83b20 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ***.**.** This f
83b30 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f 77 ile contains low
83b40 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c -level memory al
83b50 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 location drivers
83b60 20 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 53 51 4c for when.** SQL
83b70 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 ite will use the
83b80 20 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62 72 standard C-libr
83b90 61 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c ary malloc/reall
83ba0 6f 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61 63 oc/free interfac
83bb0 65 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 74 e.** to obtain t
83bc0 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 he memory it nee
83bd0 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ds..**.** This f
83be0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 ile contains imp
83bf0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 lementations of
83c00 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 the low-level me
83c10 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a mory allocation.
83c20 2a 2a 20 72 6f 75 74 69 6e 65 73 20 73 70 65 63 ** routines spec
83c30 69 66 69 65 64 20 69 6e 20 74 68 65 20 73 71 6c ified in the sql
83c40 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
83c50 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 object..**.** $
83c60 49 64 3a 20 6d 65 6d 31 2e 63 2c 76 20 31 2e 33 Id: mem1.c,v 1.3
83c70 30 20 32 30 30 39 2f 30 33 2f 32 33 20 30 34 3a 0 2009/03/23 04:
83c80 33 33 3a 33 33 20 64 61 6e 69 65 6c 6b 31 39 37 33:33 danielk197
83c90 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 7 Exp $.*/../*.*
83ca0 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f * This version o
83cb0 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c f the memory all
83cc0 6f 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 65 ocator is the de
83cd0 66 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a fault. It is.**
83ce0 20 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 used when no ot
83cf0 68 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 her memory alloc
83d00 61 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 65 ator is specifie
83d10 64 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d d using compile-
83d20 74 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a time.** macros..
83d30 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
83d40 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0a 0a _SYSTEM_MALLOC..
83d50 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 6d 61 6c 6c 6f /*.** Like mallo
83d60 63 28 29 2c 20 62 75 74 20 72 65 6d 65 6d 62 65 c(), but remembe
83d70 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 r the size of th
83d80 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 e allocation.**
83d90 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 66 so that we can f
83da0 69 6e 64 20 69 74 20 6c 61 74 65 72 20 75 73 69 ind it later usi
83db0 6e 67 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a ng sqlite3MemSiz
83dc0 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 e()..**.** For t
83dd0 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f his low-level ro
83de0 75 74 69 6e 65 2c 20 77 65 20 61 72 65 20 67 75 utine, we are gu
83df0 61 72 61 6e 74 65 65 64 20 74 68 61 74 20 6e 42 aranteed that nB
83e00 79 74 65 3e 30 20 62 65 63 61 75 73 65 0a 2a 2a yte>0 because.**
83e10 20 63 61 73 65 73 20 6f 66 20 6e 42 79 74 65 3c cases of nByte<
83e20 3d 30 20 77 69 6c 6c 20 62 65 20 69 6e 74 65 72 =0 will be inter
83e30 63 65 70 74 65 64 20 61 6e 64 20 64 65 61 6c 74 cepted and dealt
83e40 20 77 69 74 68 20 62 79 20 68 69 67 68 65 72 20 with by higher
83e50 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 level.** routine
83e60 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 s..*/.static voi
83e70 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c d *sqlite3MemMal
83e80 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a loc(int nByte){.
83e90 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
83ea0 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 42 *p;. assert( nB
83eb0 79 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74 65 yte>0 );. nByte
83ec0 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 = ROUND8(nByte)
83ed0 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 ;. p = malloc(
83ee0 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 nByte+8 );. if(
83ef0 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d p ){. p[0] =
83f00 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b nByte;. p++;
83f10 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 . }. return (v
83f20 6f 69 64 20 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a oid *)p;.}../*.*
83f30 2a 20 4c 69 6b 65 20 66 72 65 65 28 29 20 62 75 * Like free() bu
83f40 74 20 77 6f 72 6b 73 20 66 6f 72 20 61 6c 6c 6f t works for allo
83f50 63 61 74 69 6f 6e 73 20 6f 62 74 61 69 6e 65 64 cations obtained
83f60 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 65 6d from sqlite3Mem
83f70 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 73 Malloc().** or s
83f80 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 qlite3MemRealloc
83f90 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 ()..**.** For th
83fa0 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 is low-level rou
83fb0 74 69 6e 65 2c 20 77 65 20 61 6c 72 65 61 64 79 tine, we already
83fc0 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69 6f know that pPrio
83fd0 72 21 3d 30 20 73 69 6e 63 65 0a 2a 2a 20 63 61 r!=0 since.** ca
83fe0 73 65 73 20 77 68 65 72 65 20 70 50 72 69 6f 72 ses where pPrior
83ff0 3d 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62 65 ==0 will have be
84000 65 6e 20 69 6e 74 65 63 65 70 74 65 64 20 61 6e en intecepted an
84010 64 20 64 65 61 6c 74 20 77 69 74 68 0a 2a 2a 20 d dealt with.**
84020 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 by higher-level
84030 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 routines..*/.sta
84040 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 tic void sqlite3
84050 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50 MemFree(void *pP
84060 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 rior){. sqlite3
84070 5f 69 6e 74 36 34 20 2a 70 20 3d 20 28 73 71 6c _int64 *p = (sql
84080 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 ite3_int64*)pPri
84090 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 or;. assert( pP
840a0 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 70 2d 2d rior!=0 );. p--
840b0 3b 0a 20 20 66 72 65 65 28 70 29 3b 0a 7d 0a 0a ;. free(p);.}..
840c0 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 72 65 61 6c 6c /*.** Like reall
840d0 6f 63 28 29 2e 20 20 52 65 73 69 7a 65 20 61 6e oc(). Resize an
840e0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 allocation prev
840f0 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 iously obtained
84100 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d from.** sqlite3M
84110 65 6d 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a emMalloc()..**.*
84120 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c * For this low-l
84130 65 76 65 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 evel interface,
84140 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 we know that pPr
84150 69 6f 72 21 3d 30 2e 20 20 43 61 73 65 73 20 77 ior!=0. Cases w
84160 68 65 72 65 0a 2a 2a 20 70 50 72 69 6f 72 3d 3d here.** pPrior==
84170 30 20 77 68 69 6c 65 20 68 61 76 65 20 62 65 65 0 while have bee
84180 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20 62 79 n intercepted by
84190 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f higher-level ro
841a0 75 74 69 6e 65 20 61 6e 64 0a 2a 2a 20 72 65 64 utine and.** red
841b0 69 72 65 63 74 65 64 20 74 6f 20 78 4d 61 6c 6c irected to xMall
841c0 6f 63 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 oc. Similarly,
841d0 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 6e 42 79 we know that nBy
841e0 74 65 3e 30 20 62 65 63 61 75 73 65 73 0a 2a 2a te>0 becauses.**
841f0 20 63 61 73 65 73 20 77 68 65 72 65 20 6e 42 79 cases where nBy
84200 74 65 3c 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 te<=0 will have
84210 62 65 65 6e 20 69 6e 74 65 72 63 65 70 74 65 64 been intercepted
84220 20 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c by higher-level
84230 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 6e 64 .** routines and
84240 20 72 65 64 69 72 65 63 74 65 64 20 74 6f 20 78 redirected to x
84250 46 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 Free..*/.static
84260 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d void *sqlite3Mem
84270 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 Realloc(void *pP
84280 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 rior, int nByte)
84290 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 {. sqlite3_int6
842a0 34 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 4 *p = (sqlite3_
842b0 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 int64*)pPrior;.
842c0 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 assert( pPrior!
842d0 3d 30 20 26 26 20 6e 42 79 74 65 3e 30 20 29 3b =0 && nByte>0 );
842e0 0a 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 . nByte = ROUND
842f0 38 28 6e 42 79 74 65 29 3b 0a 20 20 70 20 3d 20 8(nByte);. p =
84300 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 (sqlite3_int64*)
84310 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 pPrior;. p--;.
84320 20 70 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2c 20 p = realloc(p,
84330 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 nByte+8 );. if(
84340 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d p ){. p[0] =
84350 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b nByte;. p++;
84360 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 . }. return (v
84370 6f 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a oid*)p;.}../*.**
84380 20 52 65 70 6f 72 74 20 74 68 65 20 61 6c 6c 6f Report the allo
84390 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 20 cated size of a
843a0 70 72 69 6f 72 20 72 65 74 75 72 6e 20 66 72 6f prior return fro
843b0 6d 20 78 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f m xMalloc().** o
843c0 72 20 78 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2f r xRealloc()..*/
843d0 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 .static int sqli
843e0 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 te3MemSize(void
843f0 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 *pPrior){. sqli
84400 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 te3_int64 *p;.
84410 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 20 if( pPrior==0 )
84420 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 return 0;. p =
84430 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 (sqlite3_int64*)
84440 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 pPrior;. p--;.
84450 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 5b 30 return (int)p[0
84460 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e ];.}../*.** Roun
84470 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20 73 d up a request s
84480 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ize to the next
84490 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e valid allocation
844a0 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 size..*/.static
844b0 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 int sqlite3MemR
844c0 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 oundup(int n){.
844d0 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e return ROUND8(n
844e0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 );.}../*.** Init
844f0 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 ialize this modu
84500 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
84510 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 t sqlite3MemInit
84520 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b (void *NotUsed){
84530 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
84540 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
84550 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
84560 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 ;.}../*.** Deini
84570 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 tialize this mod
84580 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ule..*/.static v
84590 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 oid sqlite3MemSh
845a0 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 utdown(void *Not
845b0 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f Used){. UNUSED_
845c0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
845d0 64 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a d);. return;.}.
845e0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
845f0 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 ine is the only
84600 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 routine in this
84610 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e file with extern
84620 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a al linkage..**.*
84630 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c * Populate the l
84640 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 ow-level memory
84650 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 allocation funct
84660 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a ion pointers in.
84670 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c ** sqlite3Global
84680 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f Config.m with po
84690 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f inters to the ro
846a0 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 utines in this f
846b0 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ile..*/.SQLITE_P
846c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
846d0 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 te3MemSetDefault
846e0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 (void){. static
846f0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d const sqlite3_m
84700 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 em_methods defau
84710 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 ltMethods = {.
84720 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c sqlite3MemMal
84730 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 loc,. sqlite
84740 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 3MemFree,. s
84750 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 qlite3MemRealloc
84760 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ,. sqlite3Me
84770 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 mSize,. sqli
84780 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 te3MemRoundup,.
84790 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e sqlite3MemIn
847a0 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 it,. sqlite3
847b0 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 MemShutdown,.
847c0 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 0. };. sqlit
847d0 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 e3_config(SQLITE
847e0 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 _CONFIG_MALLOC,
847f0 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 &defaultMethods)
84800 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 ;.}..#endif /* S
84810 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c QLITE_SYSTEM_MAL
84820 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a LOC */../*******
84830 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d ******* End of m
84840 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a em1.c **********
84850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84870 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
84880 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
84890 6c 65 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a le mem2.c ******
848a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
848b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
848c0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
848d0 30 37 20 41 75 67 75 73 74 20 31 35 0a 2a 2a 0a 07 August 15.**.
848e0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
848f0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
84900 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
84910 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
84920 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
84930 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
84940 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
84950 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
84960 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
84970 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
84980 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
84990 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
849a0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
849b0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
849c0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
849d0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
849e0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
849f0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
84a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
84a40 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
84a50 61 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d ains low-level m
84a60 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
84a70 20 64 72 69 76 65 72 73 20 66 6f 72 20 77 68 65 drivers for whe
84a80 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c n.** SQLite will
84a90 20 75 73 65 20 74 68 65 20 73 74 61 6e 64 61 72 use the standar
84aa0 64 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c d C-library mall
84ab0 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 oc/realloc/free
84ac0 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 interface.** to
84ad0 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 obtain the memor
84ae0 79 20 69 74 20 6e 65 65 64 73 20 77 68 69 6c 65 y it needs while
84af0 20 61 64 64 69 6e 67 20 6c 6f 74 73 20 6f 66 20 adding lots of
84b00 61 64 64 69 74 69 6f 6e 61 6c 20 64 65 62 75 67 additional debug
84b10 67 69 6e 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 ging.** informat
84b20 69 6f 6e 20 74 6f 20 65 61 63 68 20 61 6c 6c 6f ion to each allo
84b30 63 61 74 69 6f 6e 20 69 6e 20 6f 72 64 65 72 20 cation in order
84b40 74 6f 20 68 65 6c 70 20 64 65 74 65 63 74 20 61 to help detect a
84b50 6e 64 20 66 69 78 20 6d 65 6d 6f 72 79 0a 2a 2a nd fix memory.**
84b60 20 6c 65 61 6b 73 20 61 6e 64 20 6d 65 6d 6f 72 leaks and memor
84b70 79 20 75 73 61 67 65 20 65 72 72 6f 72 73 2e 0a y usage errors..
84b80 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
84b90 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 contains impleme
84ba0 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 ntations of the
84bb0 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 low-level memory
84bc0 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 allocation.** r
84bd0 6f 75 74 69 6e 65 73 20 73 70 65 63 69 66 69 65 outines specifie
84be0 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 d in the sqlite3
84bf0 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a _mem_methods obj
84c00 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 ect..**.** $Id:
84c10 6d 65 6d 32 2e 63 2c 76 20 31 2e 34 35 20 32 30 mem2.c,v 1.45 20
84c20 30 39 2f 30 33 2f 32 33 20 30 34 3a 33 33 3a 33 09/03/23 04:33:3
84c30 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 3 danielk1977 Ex
84c40 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 p $.*/../*.** Th
84c50 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 is version of th
84c60 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
84c70 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 or is used only
84c80 69 66 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 if the.** SQLITE
84c90 5f 4d 45 4d 44 45 42 55 47 20 6d 61 63 72 6f 20 _MEMDEBUG macro
84ca0 69 73 20 64 65 66 69 6e 65 64 0a 2a 2f 0a 23 69 is defined.*/.#i
84cb0 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 45 4d 44 fdef SQLITE_MEMD
84cc0 45 42 55 47 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 EBUG../*.** The
84cd0 62 61 63 6b 74 72 61 63 65 20 66 75 6e 63 74 69 backtrace functi
84ce0 6f 6e 61 6c 69 74 79 20 69 73 20 6f 6e 6c 79 20 onality is only
84cf0 61 76 61 69 6c 61 62 6c 65 20 77 69 74 68 20 47 available with G
84d00 4c 49 42 43 0a 2a 2f 0a 23 69 66 64 65 66 20 5f LIBC.*/.#ifdef _
84d10 5f 47 4c 49 42 43 5f 5f 0a 20 20 65 78 74 65 72 _GLIBC__. exter
84d20 6e 20 69 6e 74 20 62 61 63 6b 74 72 61 63 65 28 n int backtrace(
84d30 76 6f 69 64 2a 2a 2c 69 6e 74 29 3b 0a 20 20 65 void**,int);. e
84d40 78 74 65 72 6e 20 76 6f 69 64 20 62 61 63 6b 74 xtern void backt
84d50 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 race_symbols_fd(
84d60 76 6f 69 64 2a 63 6f 6e 73 74 2a 2c 69 6e 74 2c void*const*,int,
84d70 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 int);.#else.# de
84d80 66 69 6e 65 20 62 61 63 6b 74 72 61 63 65 28 41 fine backtrace(A
84d90 2c 42 29 20 31 0a 23 20 64 65 66 69 6e 65 20 62 ,B) 1.# define b
84da0 61 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 acktrace_symbols
84db0 5f 66 64 28 41 2c 42 2c 43 29 0a 23 65 6e 64 69 _fd(A,B,C).#endi
84dc0 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6d 65 f../*.** Each me
84dd0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
84de0 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a looks like this:
84df0 0a 2a 2a 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d .**.** --------
84e00 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84e10 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84e20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84e30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84e40 0a 2a 2a 20 20 7c 20 54 69 74 6c 65 20 7c 20 20 .** | Title |
84e50 62 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 backtrace pointe
84e60 72 73 20 7c 20 20 4d 65 6d 42 6c 6f 63 6b 48 64 rs | MemBlockHd
84e70 72 20 7c 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 r | allocation
84e80 7c 20 20 45 6e 64 47 75 61 72 64 20 7c 0a 2a 2a | EndGuard |.**
84e90 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d --------------
84ea0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84eb0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84ec0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84ed0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a ----------.**.**
84ee0 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e The application
84ef0 20 63 6f 64 65 20 73 65 65 73 20 6f 6e 6c 79 20 code sees only
84f00 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
84f10 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 57 65 allocation. We
84f20 20 68 61 76 65 0a 2a 2a 20 74 6f 20 62 61 63 6b have.** to back
84f30 20 75 70 20 66 72 6f 6d 20 74 68 65 20 61 6c 6c up from the all
84f40 6f 63 61 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 ocation pointer
84f50 74 6f 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 to find the MemB
84f60 6c 6f 63 6b 48 64 72 2e 20 20 54 68 65 0a 2a 2a lockHdr. The.**
84f70 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 74 65 6c MemBlockHdr tel
84f80 6c 73 20 75 73 20 74 68 65 20 73 69 7a 65 20 6f ls us the size o
84f90 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e f the allocation
84fa0 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 and the number
84fb0 6f 66 0a 2a 2a 20 62 61 63 6b 74 72 61 63 65 20 of.** backtrace
84fc0 70 6f 69 6e 74 65 72 73 2e 20 20 54 68 65 72 65 pointers. There
84fd0 20 69 73 20 61 6c 73 6f 20 61 20 67 75 61 72 64 is also a guard
84fe0 20 77 6f 72 64 20 61 74 20 74 68 65 20 65 6e 64 word at the end
84ff0 20 6f 66 20 74 68 65 0a 2a 2a 20 4d 65 6d 42 6c of the.** MemBl
85000 6f 63 6b 48 64 72 2e 0a 2a 2f 0a 73 74 72 75 63 ockHdr..*/.struc
85010 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7b 0a t MemBlockHdr {.
85020 20 20 69 36 34 20 69 53 69 7a 65 3b 20 20 20 20 i64 iSize;
85030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
85040 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
85050 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e this allocation
85060 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d */. struct Mem
85070 42 6c 6f 63 6b 48 64 72 20 2a 70 4e 65 78 74 2c BlockHdr *pNext,
85080 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 4c 69 6e *pPrev; /* Lin
85090 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 ked list of all
850a0 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 2a unfreed memory *
850b0 2f 0a 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72 /. char nBacktr
850c0 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ace;
850d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
850e0 72 20 6f 66 20 62 61 63 6b 74 72 61 63 65 73 20 r of backtraces
850f0 6f 6e 20 74 68 69 73 20 61 6c 6c 6f 63 20 2a 2f on this alloc */
85100 0a 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61 . char nBacktra
85110 63 65 53 6c 6f 74 73 3b 20 20 20 20 20 20 20 20 ceSlots;
85120 20 20 20 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 /* Availa
85130 62 6c 65 20 62 61 63 6b 74 72 61 63 65 20 73 6c ble backtrace sl
85140 6f 74 73 20 2a 2f 0a 20 20 73 68 6f 72 74 20 6e ots */. short n
85150 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20 20 20 Title;
85160 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
85170 42 79 74 65 73 20 6f 66 20 74 69 74 6c 65 3b 20 Bytes of title;
85180 69 6e 63 6c 75 64 65 73 20 27 5c 30 27 20 2a 2f includes '\0' */
85190 0a 20 20 69 6e 74 20 69 46 6f 72 65 47 75 61 72 . int iForeGuar
851a0 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
851b0 20 20 20 20 20 20 20 2f 2a 20 47 75 61 72 64 20 /* Guard
851c0 77 6f 72 64 20 66 6f 72 20 73 61 6e 69 74 79 20 word for sanity
851d0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 75 61 */.};../*.** Gua
851e0 72 64 20 77 6f 72 64 73 0a 2a 2f 0a 23 64 65 66 rd words.*/.#def
851f0 69 6e 65 20 46 4f 52 45 47 55 41 52 44 20 30 78 ine FOREGUARD 0x
85200 38 30 46 35 45 31 35 33 0a 23 64 65 66 69 6e 65 80F5E153.#define
85210 20 52 45 41 52 47 55 41 52 44 20 30 78 45 34 36 REARGUARD 0xE46
85220 37 36 42 35 33 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 76B53../*.** Num
85230 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 73 69 ber of malloc si
85240 7a 65 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 6f ze increments to
85250 20 74 72 61 63 6b 2e 0a 2a 2f 0a 23 64 65 66 69 track..*/.#defi
85260 6e 65 20 4e 43 53 49 5a 45 20 20 31 30 30 30 0a ne NCSIZE 1000.
85270 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 ./*.** All of th
85280 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c e static variabl
85290 65 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20 es used by this
852a0 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 module are colle
852b0 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 cted.** into a s
852c0 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 65 20 ingle structure
852d0 6e 61 6d 65 64 20 22 6d 65 6d 22 2e 20 20 54 68 named "mem". Th
852e0 69 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68 is is to keep th
852f0 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69 e.** static vari
85300 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 ables organized
85310 61 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61 and to reduce na
85320 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f mespace pollutio
85330 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d n.** when this m
85340 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 odule is combine
85350 64 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20 d with other in
85360 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e the amalgamation
85370 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 ..*/.static stru
85380 63 74 20 7b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a ct {. . /*. *
85390 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 * Mutex to contr
853a0 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 ol access to the
853b0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
853c0 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 on subsystem..
853d0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 */. sqlite3_mut
853e0 65 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a ex *mutex;.. /*
853f0 0a 20 20 2a 2a 20 48 65 61 64 20 61 6e 64 20 74 . ** Head and t
85400 61 69 6c 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 ail of a linked
85410 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f 75 74 73 list of all outs
85420 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 tanding allocati
85430 6f 6e 73 0a 20 20 2a 2f 0a 20 20 73 74 72 75 63 ons. */. struc
85440 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 t MemBlockHdr *p
85450 46 69 72 73 74 3b 0a 20 20 73 74 72 75 63 74 20 First;. struct
85460 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4c 61 MemBlockHdr *pLa
85470 73 74 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a st;. . /*. **
85480 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c The number of l
85490 65 76 65 6c 73 20 6f 66 20 62 61 63 6b 74 72 61 evels of backtra
854a0 63 65 20 74 6f 20 73 61 76 65 20 69 6e 20 6e 65 ce to save in ne
854b0 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 w allocations..
854c0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b 74 */. int nBackt
854d0 72 61 63 65 3b 0a 20 20 76 6f 69 64 20 28 2a 78 race;. void (*x
854e0 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 Backtrace)(int,
854f0 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 3b 0a 0a int, void **);..
85500 20 20 2f 2a 0a 20 20 2a 2a 20 54 69 74 6c 65 20 /*. ** Title
85510 74 65 78 74 20 74 6f 20 69 6e 73 65 72 74 20 69 text to insert i
85520 6e 20 66 72 6f 6e 74 20 6f 66 20 65 61 63 68 20 n front of each
85530 62 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 6e 74 block. */. int
85540 20 6e 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20 nTitle;
85550 2f 2a 20 42 79 74 65 73 20 6f 66 20 7a 54 69 74 /* Bytes of zTit
85560 6c 65 20 74 6f 20 73 61 76 65 2e 20 20 49 6e 63 le to save. Inc
85570 6c 75 64 65 73 20 27 5c 30 27 20 61 6e 64 20 70 ludes '\0' and p
85580 61 64 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 adding */. char
85590 20 7a 54 69 74 6c 65 5b 31 30 30 5d 3b 20 20 2f zTitle[100]; /
855a0 2a 20 54 68 65 20 74 69 74 6c 65 20 74 65 78 74 * The title text
855b0 20 2a 2f 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 */.. /* . **
855c0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 44 69 73 sqlite3MallocDis
855d0 61 6c 6c 6f 77 28 29 20 69 6e 63 72 65 6d 65 6e allow() incremen
855e0 74 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ts the following
855f0 20 63 6f 75 6e 74 65 72 2e 0a 20 20 2a 2a 20 73 counter.. ** s
85600 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f qlite3MallocAllo
85610 77 28 29 20 64 65 63 72 65 6d 65 6e 74 73 20 69 w() decrements i
85620 74 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 64 69 t.. */. int di
85630 73 61 6c 6c 6f 77 3b 20 2f 2a 20 44 6f 20 6e 6f sallow; /* Do no
85640 74 20 61 6c 6c 6f 77 20 6d 65 6d 6f 72 79 20 61 t allow memory a
85650 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 llocation */..
85660 2f 2a 0a 20 20 2a 2a 20 47 61 74 68 65 72 20 73 /*. ** Gather s
85670 74 61 74 69 73 74 69 63 73 20 6f 6e 20 74 68 65 tatistics on the
85680 20 73 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79 sizes of memory
85690 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 allocations..
856a0 2a 2a 20 6e 41 6c 6c 6f 63 5b 69 5d 20 69 73 20 ** nAlloc[i] is
856b0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c the number of al
856c0 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 location attempt
856d0 73 20 6f 66 20 69 2a 38 0a 20 20 2a 2a 20 62 79 s of i*8. ** by
856e0 74 65 73 2e 20 20 69 3d 3d 4e 43 53 49 5a 45 20 tes. i==NCSIZE
856f0 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
85700 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 allocation atte
85710 6d 70 74 73 20 66 6f 72 0a 20 20 2a 2a 20 73 69 mpts for. ** si
85720 7a 65 73 20 6d 6f 72 65 20 74 68 61 6e 20 4e 43 zes more than NC
85730 53 49 5a 45 2a 38 20 62 79 74 65 73 2e 0a 20 20 SIZE*8 bytes..
85740 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 5b */. int nAlloc[
85750 4e 43 53 49 5a 45 5d 3b 20 20 20 20 20 20 2f 2a NCSIZE]; /*
85760 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 Total number of
85770 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a allocations */.
85780 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 5b 4e int nCurrent[N
85790 43 53 49 5a 45 5d 3b 20 20 20 20 2f 2a 20 43 75 CSIZE]; /* Cu
857a0 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 rrent number of
857b0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 allocations */.
857c0 20 69 6e 74 20 6d 78 43 75 72 72 65 6e 74 5b 4e int mxCurrent[N
857d0 43 53 49 5a 45 5d 3b 20 20 20 2f 2a 20 48 69 67 CSIZE]; /* Hig
857e0 68 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 20 hwater mark for
857f0 6e 43 75 72 72 65 6e 74 20 2a 2f 0a 0a 7d 20 6d nCurrent */..} m
85800 65 6d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 em;.../*.** Adju
85810 73 74 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 st memory usage
85820 73 74 61 74 69 73 74 69 63 73 0a 2a 2f 0a 73 74 statistics.*/.st
85830 61 74 69 63 20 76 6f 69 64 20 61 64 6a 75 73 74 atic void adjust
85840 53 74 61 74 73 28 69 6e 74 20 69 53 69 7a 65 2c Stats(int iSize,
85850 20 69 6e 74 20 69 6e 63 72 65 6d 65 6e 74 29 7b int increment){
85860 0a 20 20 69 6e 74 20 69 20 3d 20 52 4f 55 4e 44 . int i = ROUND
85870 38 28 69 53 69 7a 65 29 2f 38 3b 0a 20 20 69 66 8(iSize)/8;. if
85880 28 20 69 3e 4e 43 53 49 5a 45 2d 31 20 29 7b 0a ( i>NCSIZE-1 ){.
85890 20 20 20 20 69 20 3d 20 4e 43 53 49 5a 45 20 2d i = NCSIZE -
858a0 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 6e 1;. }. if( in
858b0 63 72 65 6d 65 6e 74 3e 30 20 29 7b 0a 20 20 20 crement>0 ){.
858c0 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2b 2b mem.nAlloc[i]++
858d0 3b 0a 20 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65 ;. mem.nCurre
858e0 6e 74 5b 69 5d 2b 2b 3b 0a 20 20 20 20 69 66 28 nt[i]++;. if(
858f0 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d mem.nCurrent[i]
85900 3e 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 >mem.mxCurrent[i
85910 5d 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 6d ] ){. mem.m
85920 78 43 75 72 72 65 6e 74 5b 69 5d 20 3d 20 6d 65 xCurrent[i] = me
85930 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3b 0a 20 m.nCurrent[i];.
85940 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
85950 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 mem.nCurrent[i
85960 5d 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ]--;. assert(
85970 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d mem.nCurrent[i]
85980 3e 3d 30 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a >=0 );. }.}../*
85990 0a 2a 2a 20 47 69 76 65 6e 20 61 6e 20 61 6c 6c .** Given an all
859a0 6f 63 61 74 69 6f 6e 2c 20 66 69 6e 64 20 74 68 ocation, find th
859b0 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 66 6f e MemBlockHdr fo
859c0 72 20 74 68 61 74 20 61 6c 6c 6f 63 61 74 69 6f r that allocatio
859d0 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f n..**.** This ro
859e0 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 65 utine checks the
859f0 20 67 75 61 72 64 73 20 61 74 20 65 69 74 68 65 guards at eithe
85a00 72 20 65 6e 64 20 6f 66 20 74 68 65 20 61 6c 6c r end of the all
85a10 6f 63 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 69 ocation and.** i
85a20 66 20 74 68 65 79 20 61 72 65 20 69 6e 63 6f 72 f they are incor
85a30 72 65 63 74 20 69 74 20 61 73 73 65 72 74 73 2e rect it asserts.
85a40 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 .*/.static struc
85a50 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 73 t MemBlockHdr *s
85a60 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 qlite3MemsysGetH
85a70 65 61 64 65 72 28 76 6f 69 64 20 2a 70 41 6c 6c eader(void *pAll
85a80 6f 63 61 74 69 6f 6e 29 7b 0a 20 20 73 74 72 75 ocation){. stru
85a90 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a ct MemBlockHdr *
85aa0 70 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74 3b 0a p;. int *pInt;.
85ab0 20 20 75 38 20 2a 70 55 38 3b 0a 20 20 69 6e 74 u8 *pU8;. int
85ac0 20 6e 52 65 73 65 72 76 65 3b 0a 0a 20 20 70 20 nReserve;.. p
85ad0 3d 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f = (struct MemBlo
85ae0 63 6b 48 64 72 2a 29 70 41 6c 6c 6f 63 61 74 69 ckHdr*)pAllocati
85af0 6f 6e 3b 0a 20 20 70 2d 2d 3b 0a 20 20 61 73 73 on;. p--;. ass
85b00 65 72 74 28 20 70 2d 3e 69 46 6f 72 65 47 75 61 ert( p->iForeGua
85b10 72 64 3d 3d 28 69 6e 74 29 46 4f 52 45 47 55 41 rd==(int)FOREGUA
85b20 52 44 20 29 3b 0a 20 20 6e 52 65 73 65 72 76 65 RD );. nReserve
85b30 20 3d 20 52 4f 55 4e 44 38 28 70 2d 3e 69 53 69 = ROUND8(p->iSi
85b40 7a 65 29 3b 0a 20 20 70 49 6e 74 20 3d 20 28 69 ze);. pInt = (i
85b50 6e 74 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b nt*)pAllocation;
85b60 0a 20 20 70 55 38 20 3d 20 28 75 38 2a 29 70 41 . pU8 = (u8*)pA
85b70 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 61 73 73 llocation;. ass
85b80 65 72 74 28 20 70 49 6e 74 5b 6e 52 65 73 65 72 ert( pInt[nReser
85b90 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 3d ve/sizeof(int)]=
85ba0 3d 28 69 6e 74 29 52 45 41 52 47 55 41 52 44 20 =(int)REARGUARD
85bb0 29 3b 0a 20 20 2f 2a 20 54 68 69 73 20 63 68 65 );. /* This che
85bc0 63 6b 73 20 61 6e 79 20 6f 66 20 74 68 65 20 22 cks any of the "
85bd0 65 78 74 72 61 22 20 62 79 74 65 73 20 61 6c 6c extra" bytes all
85be0 6f 63 61 74 65 64 20 64 75 65 0a 20 20 2a 2a 20 ocated due. **
85bf0 74 6f 20 72 6f 75 6e 64 69 6e 67 20 75 70 20 74 to rounding up t
85c00 6f 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e o an 8 byte boun
85c10 64 61 72 79 20 74 6f 20 65 6e 73 75 72 65 20 0a dary to ensure .
85c20 20 20 2a 2a 20 74 68 65 79 20 68 61 76 65 6e 27 ** they haven'
85c30 74 20 62 65 65 6e 20 6f 76 65 72 77 72 69 74 74 t been overwritt
85c40 65 6e 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 en.. */. while
85c50 28 20 6e 52 65 73 65 72 76 65 2d 2d 20 3e 20 70 ( nReserve-- > p
85c60 2d 3e 69 53 69 7a 65 20 29 20 61 73 73 65 72 74 ->iSize ) assert
85c70 28 20 70 55 38 5b 6e 52 65 73 65 72 76 65 5d 3d ( pU8[nReserve]=
85c80 3d 30 78 36 35 20 29 3b 0a 20 20 72 65 74 75 72 =0x65 );. retur
85c90 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 n p;.}../*.** Re
85ca0 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
85cb0 6f 66 20 62 79 74 65 73 20 63 75 72 72 65 6e 74 of bytes current
85cc0 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 ly allocated at
85cd0 61 64 64 72 65 73 73 20 70 2e 0a 2a 2f 0a 73 74 address p..*/.st
85ce0 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 atic int sqlite3
85cf0 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 MemSize(void *p)
85d00 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c {. struct MemBl
85d10 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 ockHdr *pHdr;.
85d20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 if( !p ){. re
85d30 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 48 turn 0;. }. pH
85d40 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 dr = sqlite3Mems
85d50 79 73 47 65 74 48 65 61 64 65 72 28 70 29 3b 0a ysGetHeader(p);.
85d60 20 20 72 65 74 75 72 6e 20 70 48 64 72 2d 3e 69 return pHdr->i
85d70 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 Size;.}../*.** I
85d80 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 nitialize the me
85d90 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
85da0 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 subsystem..*/.st
85db0 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 atic int sqlite3
85dc0 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f MemInit(void *No
85dd0 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 tUsed){. UNUSED
85de0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
85df0 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 ed);. assert( (
85e00 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 4d 65 sizeof(struct Me
85e10 6d 42 6c 6f 63 6b 48 64 72 29 26 37 29 20 3d 3d mBlockHdr)&7) ==
85e20 20 30 20 29 3b 0a 20 20 69 66 28 20 21 73 71 6c 0 );. if( !sql
85e30 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
85e40 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 .bMemstat ){.
85e50 20 2f 2a 20 49 66 20 6d 65 6d 6f 72 79 20 73 74 /* If memory st
85e60 61 74 75 73 20 69 73 20 65 6e 61 62 6c 65 64 2c atus is enabled,
85e70 20 74 68 65 6e 20 74 68 65 20 6d 61 6c 6c 6f 63 then the malloc
85e80 2e 63 20 77 72 61 70 70 65 72 20 77 69 6c 6c 20 .c wrapper will
85e90 61 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 68 already. ** h
85ea0 6f 6c 64 20 74 68 65 20 53 54 41 54 49 43 5f 4d old the STATIC_M
85eb0 45 4d 20 6d 75 74 65 78 20 77 68 65 6e 20 74 68 EM mutex when th
85ec0 65 20 72 6f 75 74 69 6e 65 73 20 68 65 72 65 20 e routines here
85ed0 61 72 65 20 69 6e 76 6f 6b 65 64 2e 20 2a 2f 0a are invoked. */.
85ee0 20 20 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 mem.mutex =
85ef0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
85f00 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
85f10 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a TATIC_MEM);. }.
85f20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
85f30 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 OK;.}../*.** Dei
85f40 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 nitialize the me
85f50 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
85f60 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 subsystem..*/.st
85f70 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 atic void sqlite
85f80 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 3MemShutdown(voi
85f90 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 d *NotUsed){. U
85fa0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
85fb0 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 2e NotUsed);. mem.
85fc0 6d 75 74 65 78 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a mutex = 0;.}../*
85fd0 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 .** Round up a r
85fe0 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 equest size to t
85ff0 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c he next valid al
86000 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a location size..*
86010 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c /.static int sql
86020 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 ite3MemRoundup(i
86030 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 nt n){. return
86040 52 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a ROUND8(n);.}../*
86050 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 .** Allocate nBy
86060 74 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f te bytes of memo
86070 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ry..*/.static vo
86080 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 id *sqlite3MemMa
86090 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b lloc(int nByte){
860a0 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f . struct MemBlo
860b0 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 76 ckHdr *pHdr;. v
860c0 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68 61 oid **pBt;. cha
860d0 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 2a 70 49 6e r *z;. int *pIn
860e0 74 3b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 30 t;. void *p = 0
860f0 3b 0a 20 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a ;. int totalSiz
86100 65 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76 e;. int nReserv
86110 65 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 e;. sqlite3_mut
86120 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 ex_enter(mem.mut
86130 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d ex);. assert( m
86140 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 29 em.disallow==0 )
86150 3b 0a 20 20 6e 52 65 73 65 72 76 65 20 3d 20 52 ;. nReserve = R
86160 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 OUND8(nByte);.
86170 74 6f 74 61 6c 53 69 7a 65 20 3d 20 6e 52 65 73 totalSize = nRes
86180 65 72 76 65 20 2b 20 73 69 7a 65 6f 66 28 2a 70 erve + sizeof(*p
86190 48 64 72 29 20 2b 20 73 69 7a 65 6f 66 28 69 6e Hdr) + sizeof(in
861a0 74 29 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 t) +.
861b0 20 20 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 mem.nBacktra
861c0 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 ce*sizeof(void*)
861d0 20 2b 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20 + mem.nTitle;.
861e0 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 74 6f 74 61 p = malloc(tota
861f0 6c 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 20 lSize);. if( p
86200 29 7b 0a 20 20 20 20 7a 20 3d 20 70 3b 0a 20 20 ){. z = p;.
86210 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 pBt = (void**)
86220 26 7a 5b 6d 65 6d 2e 6e 54 69 74 6c 65 5d 3b 0a &z[mem.nTitle];.
86230 20 20 20 20 70 48 64 72 20 3d 20 28 73 74 72 75 pHdr = (stru
86240 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 ct MemBlockHdr*)
86250 26 70 42 74 5b 6d 65 6d 2e 6e 42 61 63 6b 74 72 &pBt[mem.nBacktr
86260 61 63 65 5d 3b 0a 20 20 20 20 70 48 64 72 2d 3e ace];. pHdr->
86270 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 pNext = 0;. p
86280 48 64 72 2d 3e 70 50 72 65 76 20 3d 20 6d 65 6d Hdr->pPrev = mem
86290 2e 70 4c 61 73 74 3b 0a 20 20 20 20 69 66 28 20 .pLast;. if(
862a0 6d 65 6d 2e 70 4c 61 73 74 20 29 7b 0a 20 20 20 mem.pLast ){.
862b0 20 20 20 6d 65 6d 2e 70 4c 61 73 74 2d 3e 70 4e mem.pLast->pN
862c0 65 78 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 ext = pHdr;.
862d0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d }else{. mem
862e0 2e 70 46 69 72 73 74 20 3d 20 70 48 64 72 3b 0a .pFirst = pHdr;.
862f0 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 2e 70 4c }. mem.pL
86300 61 73 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 ast = pHdr;.
86310 70 48 64 72 2d 3e 69 46 6f 72 65 47 75 61 72 64 pHdr->iForeGuard
86320 20 3d 20 46 4f 52 45 47 55 41 52 44 3b 0a 20 20 = FOREGUARD;.
86330 20 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 pHdr->nBacktra
86340 63 65 53 6c 6f 74 73 20 3d 20 6d 65 6d 2e 6e 42 ceSlots = mem.nB
86350 61 63 6b 74 72 61 63 65 3b 0a 20 20 20 20 70 48 acktrace;. pH
86360 64 72 2d 3e 6e 54 69 74 6c 65 20 3d 20 6d 65 6d dr->nTitle = mem
86370 2e 6e 54 69 74 6c 65 3b 0a 20 20 20 20 69 66 28 .nTitle;. if(
86380 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20 mem.nBacktrace
86390 29 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 61 ){. void *a
863a0 41 64 64 72 5b 34 30 5d 3b 0a 20 20 20 20 20 20 Addr[40];.
863b0 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
863c0 20 3d 20 62 61 63 6b 74 72 61 63 65 28 61 41 64 = backtrace(aAd
863d0 64 72 2c 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 dr, mem.nBacktra
863e0 63 65 2b 31 29 2d 31 3b 0a 20 20 20 20 20 20 6d ce+1)-1;. m
863f0 65 6d 63 70 79 28 70 42 74 2c 20 26 61 41 64 64 emcpy(pBt, &aAdd
86400 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e 42 61 63 r[1], pHdr->nBac
86410 6b 74 72 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f ktrace*sizeof(vo
86420 69 64 2a 29 29 3b 0a 20 20 20 20 20 20 61 73 73 id*));. ass
86430 65 72 74 28 70 42 74 5b 30 5d 29 3b 0a 20 20 20 ert(pBt[0]);.
86440 20 20 20 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b if( mem.xBack
86450 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 trace ){.
86460 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 mem.xBacktrace(
86470 6e 42 79 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61 nByte, pHdr->nBa
86480 63 6b 74 72 61 63 65 2d 31 2c 20 26 61 41 64 64 cktrace-1, &aAdd
86490 72 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 r[1]);. }.
864a0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
864b0 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
864c0 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
864d0 69 66 28 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29 if( mem.nTitle )
864e0 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a {. memcpy(z
864f0 2c 20 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65 , mem.zTitle, me
86500 6d 2e 6e 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d m.nTitle);. }
86510 0a 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 . pHdr->iSize
86520 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 64 = nByte;. ad
86530 6a 75 73 74 53 74 61 74 73 28 6e 42 79 74 65 2c justStats(nByte,
86540 20 2b 31 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d +1);. pInt =
86550 20 28 69 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b (int*)&pHdr[1];
86560 0a 20 20 20 20 70 49 6e 74 5b 6e 52 65 73 65 72 . pInt[nReser
86570 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20 ve/sizeof(int)]
86580 3d 20 52 45 41 52 47 55 41 52 44 3b 0a 20 20 20 = REARGUARD;.
86590 20 6d 65 6d 73 65 74 28 70 49 6e 74 2c 20 30 78 memset(pInt, 0x
865a0 36 35 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 65, nReserve);.
865b0 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 70 49 p = (void*)pI
865c0 6e 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 nt;. }. sqlite
865d0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
865e0 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 m.mutex);. retu
865f0 72 6e 20 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn p; .}../*.**
86600 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a Free memory..*/.
86610 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 static void sqli
86620 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 te3MemFree(void
86630 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 74 72 75 *pPrior){. stru
86640 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a ct MemBlockHdr *
86650 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 pHdr;. void **p
86660 42 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 Bt;. char *z;.
86670 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
86680 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
86690 6d 73 74 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75 74 mstat || mem.mut
866a0 65 78 21 3d 30 20 29 3b 0a 20 20 70 48 64 72 20 ex!=0 );. pHdr
866b0 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 = sqlite3MemsysG
866c0 65 74 48 65 61 64 65 72 28 70 50 72 69 6f 72 29 etHeader(pPrior)
866d0 3b 0a 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a ;. pBt = (void*
866e0 2a 29 70 48 64 72 3b 0a 20 20 70 42 74 20 2d 3d *)pHdr;. pBt -=
866f0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 pHdr->nBacktrac
86700 65 53 6c 6f 74 73 3b 0a 20 20 73 71 6c 69 74 65 eSlots;. sqlite
86710 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 3_mutex_enter(me
86720 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 m.mutex);. if(
86730 70 48 64 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 pHdr->pPrev ){.
86740 20 20 20 61 73 73 65 72 74 28 20 70 48 64 72 2d assert( pHdr-
86750 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 >pPrev->pNext==p
86760 48 64 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d Hdr );. pHdr-
86770 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 >pPrev->pNext =
86780 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d pHdr->pNext;. }
86790 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
867a0 28 20 6d 65 6d 2e 70 46 69 72 73 74 3d 3d 70 48 ( mem.pFirst==pH
867b0 64 72 20 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 46 dr );. mem.pF
867c0 69 72 73 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65 irst = pHdr->pNe
867d0 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 xt;. }. if( pH
867e0 64 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 dr->pNext ){.
867f0 20 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70 assert( pHdr->p
86800 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 48 64 Next->pPrev==pHd
86810 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 r );. pHdr->p
86820 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 48 Next->pPrev = pH
86830 64 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 65 6c dr->pPrev;. }el
86840 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
86850 6d 65 6d 2e 70 4c 61 73 74 3d 3d 70 48 64 72 20 mem.pLast==pHdr
86860 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 );. mem.pLast
86870 20 3d 20 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a = pHdr->pPrev;.
86880 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a }. z = (char*
86890 29 70 42 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 64 )pBt;. z -= pHd
868a0 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 61 64 6a r->nTitle;. adj
868b0 75 73 74 53 74 61 74 73 28 70 48 64 72 2d 3e 69 ustStats(pHdr->i
868c0 53 69 7a 65 2c 20 2d 31 29 3b 0a 20 20 6d 65 6d Size, -1);. mem
868d0 73 65 74 28 7a 2c 20 30 78 32 62 2c 20 73 69 7a set(z, 0x2b, siz
868e0 65 6f 66 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d eof(void*)*pHdr-
868f0 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 >nBacktraceSlots
86900 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 + sizeof(*pHdr)
86910 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 +.
86920 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 pHdr->iSize
86930 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b + sizeof(int) +
86940 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0a pHdr->nTitle);.
86950 20 20 66 72 65 65 28 7a 29 3b 0a 20 20 73 71 6c free(z);. sql
86960 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
86970 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d (mem.mutex); .}
86980 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
86990 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 he size of an ex
869a0 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c isting memory al
869b0 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 location..**.**
869c0 46 6f 72 20 74 68 69 73 20 64 65 62 75 67 67 69 For this debuggi
869d0 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ng implementatio
869e0 6e 2c 20 77 65 20 2a 61 6c 77 61 79 73 2a 20 6d n, we *always* m
869f0 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 ake a copy of th
86a00 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 e.** allocation
86a10 69 6e 74 6f 20 61 20 6e 65 77 20 70 6c 61 63 65 into a new place
86a20 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 49 6e 20 in memory. In
86a30 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 65 this way, if the
86a40 20 0a 2a 2a 20 68 69 67 68 65 72 20 6c 65 76 65 .** higher leve
86a50 6c 20 63 6f 64 65 20 69 73 20 75 73 69 6e 67 20 l code is using
86a60 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f pointer to the o
86a70 6c 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 ld allocation, i
86a80 74 20 69 73 20 0a 2a 2a 20 6d 75 63 68 20 6d 6f t is .** much mo
86a90 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 72 65 re likely to bre
86aa0 61 6b 20 61 6e 64 20 77 65 20 61 72 65 20 6d 75 ak and we are mu
86ab0 63 68 20 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 74 ch more liking t
86ac0 6f 20 66 69 6e 64 0a 2a 2a 20 74 68 65 20 65 72 o find.** the er
86ad0 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ror..*/.static v
86ae0 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 oid *sqlite3MemR
86af0 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 ealloc(void *pPr
86b00 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b ior, int nByte){
86b10 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f . struct MemBlo
86b20 63 6b 48 64 72 20 2a 70 4f 6c 64 48 64 72 3b 0a ckHdr *pOldHdr;.
86b30 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 void *pNew;.
86b40 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 assert( mem.disa
86b50 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 70 4f 6c llow==0 );. pOl
86b60 64 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 dHdr = sqlite3Me
86b70 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 70 50 msysGetHeader(pP
86b80 72 69 6f 72 29 3b 0a 20 20 70 4e 65 77 20 3d 20 rior);. pNew =
86b90 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 sqlite3MemMalloc
86ba0 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 70 (nByte);. if( p
86bb0 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 New ){. memcp
86bc0 79 28 70 4e 65 77 2c 20 70 50 72 69 6f 72 2c 20 y(pNew, pPrior,
86bd0 6e 42 79 74 65 3c 70 4f 6c 64 48 64 72 2d 3e 69 nByte<pOldHdr->i
86be0 53 69 7a 65 20 3f 20 6e 42 79 74 65 20 3a 20 70 Size ? nByte : p
86bf0 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a OldHdr->iSize);.
86c00 20 20 20 20 69 66 28 20 6e 42 79 74 65 3e 70 4f if( nByte>pO
86c10 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 29 7b 0a ldHdr->iSize ){.
86c20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 memset(&((
86c30 63 68 61 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64 char*)pNew)[pOld
86c40 48 64 72 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 32 Hdr->iSize], 0x2
86c50 62 2c 20 6e 42 79 74 65 20 2d 20 70 4f 6c 64 48 b, nByte - pOldH
86c60 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 dr->iSize);.
86c70 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d }. sqlite3Mem
86c80 46 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 Free(pPrior);.
86c90 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b }. return pNew;
86ca0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61 .}../*.** Popula
86cb0 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c te the low-level
86cc0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
86cd0 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e on function poin
86ce0 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 ters in.** sqlit
86cf0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
86d00 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 with pointers t
86d10 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 o the routines i
86d20 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a n this file..*/.
86d30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
86d40 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 oid sqlite3MemSe
86d50 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a tDefault(void){.
86d60 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 static const s
86d70 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
86d80 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 ds defaultMethod
86d90 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 s = {. sqlit
86da0 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 e3MemMalloc,.
86db0 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 sqlite3MemFree
86dc0 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ,. sqlite3Me
86dd0 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 mRealloc,. s
86de0 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 qlite3MemSize,.
86df0 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f sqlite3MemRo
86e00 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 undup,. sqli
86e10 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 te3MemInit,.
86e20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 sqlite3MemShutd
86e30 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b own,. 0. };
86e40 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 . sqlite3_confi
86e50 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f g(SQLITE_CONFIG_
86e60 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 MALLOC, &default
86e70 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a 0a Methods);.}../*.
86e80 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 ** Set the numbe
86e90 72 20 6f 66 20 62 61 63 6b 74 72 61 63 65 20 6c r of backtrace l
86ea0 65 76 65 6c 73 20 6b 65 70 74 20 66 6f 72 20 65 evels kept for e
86eb0 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a ach allocation..
86ec0 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 7a 65 ** A value of ze
86ed0 72 6f 20 74 75 72 6e 73 20 6f 66 66 20 62 61 63 ro turns off bac
86ee0 6b 74 72 61 63 69 6e 67 2e 20 20 54 68 65 20 6e ktracing. The n
86ef0 75 6d 62 65 72 20 69 73 20 61 6c 77 61 79 73 20 umber is always
86f00 72 6f 75 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f rounded.** up to
86f10 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32 a multiple of 2
86f20 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
86f30 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
86f40 4d 65 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 MemdebugBacktrac
86f50 65 28 69 6e 74 20 64 65 70 74 68 29 7b 0a 20 20 e(int depth){.
86f60 69 66 28 20 64 65 70 74 68 3c 30 20 29 7b 20 64 if( depth<0 ){ d
86f70 65 70 74 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66 epth = 0; }. if
86f80 28 20 64 65 70 74 68 3e 32 30 20 29 7b 20 64 65 ( depth>20 ){ de
86f90 70 74 68 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65 pth = 20; }. de
86fa0 70 74 68 20 3d 20 28 64 65 70 74 68 2b 31 29 26 pth = (depth+1)&
86fb0 30 78 66 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63 0xfe;. mem.nBac
86fc0 6b 74 72 61 63 65 20 3d 20 64 65 70 74 68 3b 0a ktrace = depth;.
86fd0 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 }..SQLITE_PRIVAT
86fe0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 E void sqlite3Me
86ff0 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 43 mdebugBacktraceC
87000 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20 28 2a 78 allback(void (*x
87010 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 Backtrace)(int,
87020 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a int, void **)){.
87030 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 mem.xBacktrace
87040 20 3d 20 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d = xBacktrace;.}
87050 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
87060 74 69 74 6c 65 20 73 74 72 69 6e 67 20 66 6f 72 title string for
87070 20 73 75 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f subsequent allo
87080 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 cations..*/.SQLI
87090 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
870a0 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 sqlite3MemdebugS
870b0 65 74 74 69 74 6c 65 28 63 6f 6e 73 74 20 63 68 ettitle(const ch
870c0 61 72 20 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 75 ar *zTitle){. u
870d0 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 20 3d 20 nsigned int n =
870e0 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
870f0 7a 54 69 74 6c 65 29 20 2b 20 31 3b 0a 20 20 73 zTitle) + 1;. s
87100 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
87110 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 er(mem.mutex);.
87120 20 69 66 28 20 6e 3e 3d 73 69 7a 65 6f 66 28 6d if( n>=sizeof(m
87130 65 6d 2e 7a 54 69 74 6c 65 29 20 29 20 6e 20 3d em.zTitle) ) n =
87140 20 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 sizeof(mem.zTit
87150 6c 65 29 2d 31 3b 0a 20 20 6d 65 6d 63 70 79 28 le)-1;. memcpy(
87160 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 7a 54 69 74 mem.zTitle, zTit
87170 6c 65 2c 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a 54 le, n);. mem.zT
87180 69 74 6c 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20 6d itle[n] = 0;. m
87190 65 6d 2e 6e 54 69 74 6c 65 20 3d 20 52 4f 55 4e em.nTitle = ROUN
871a0 44 38 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 D8(n);. sqlite3
871b0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d _mutex_leave(mem
871c0 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 .mutex);.}..SQLI
871d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
871e0 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 sqlite3MemdebugS
871f0 79 6e 63 28 29 7b 0a 20 20 73 74 72 75 63 74 20 ync(){. struct
87200 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 MemBlockHdr *pHd
87210 72 3b 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 r;. for(pHdr=me
87220 6d 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20 m.pFirst; pHdr;
87230 70 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 pHdr=pHdr->pNext
87240 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 2a 70 42 ){. void **pB
87250 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 t = (void**)pHdr
87260 3b 0a 20 20 20 20 70 42 74 20 2d 3d 20 70 48 64 ;. pBt -= pHd
87270 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f r->nBacktraceSlo
87280 74 73 3b 0a 20 20 20 20 6d 65 6d 2e 78 42 61 63 ts;. mem.xBac
87290 6b 74 72 61 63 65 28 70 48 64 72 2d 3e 69 53 69 ktrace(pHdr->iSi
872a0 7a 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 ze, pHdr->nBackt
872b0 72 61 63 65 2d 31 2c 20 26 70 42 74 5b 31 5d 29 race-1, &pBt[1])
872c0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f ;. }.}../*.** O
872d0 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 pen the file ind
872e0 69 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 icated and write
872f0 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e a log of all un
87300 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a freed memory .**
87310 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 allocations int
87320 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 o that log..*/.S
87330 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
87340 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 id sqlite3Memdeb
87350 75 67 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 ugDump(const cha
87360 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 r *zFilename){.
87370 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 73 74 FILE *out;. st
87380 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 ruct MemBlockHdr
87390 20 2a 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a *pHdr;. void *
873a0 2a 70 42 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 *pBt;. int i;.
873b0 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 out = fopen(zFi
873c0 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 lename, "w");.
873d0 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 if( out==0 ){.
873e0 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 fprintf(stderr
873f0 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 , "** Unable to
87400 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 output memory de
87410 62 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 bug output log:
87420 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 %s **\n",.
87430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 zF
87440 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 ilename);. re
87450 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 turn;. }. for(
87460 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b pHdr=mem.pFirst;
87470 20 70 48 64 72 3b 20 70 48 64 72 3d 70 48 64 72 pHdr; pHdr=pHdr
87480 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 63 68 ->pNext){. ch
87490 61 72 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29 70 ar *z = (char*)p
874a0 48 64 72 3b 0a 20 20 20 20 7a 20 2d 3d 20 70 48 Hdr;. z -= pH
874b0 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c dr->nBacktraceSl
874c0 6f 74 73 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a ots*sizeof(void*
874d0 29 20 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 ) + pHdr->nTitle
874e0 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 ;. fprintf(ou
874f0 74 2c 20 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62 79 t, "**** %lld by
87500 74 65 73 20 61 74 20 25 70 20 66 72 6f 6d 20 25 tes at %p from %
87510 73 20 2a 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 20 s ****\n", .
87520 20 20 20 20 20 20 20 20 70 48 64 72 2d 3e 69 53 pHdr->iS
87530 69 7a 65 2c 20 26 70 48 64 72 5b 31 5d 2c 20 70 ize, &pHdr[1], p
87540 48 64 72 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a 20 Hdr->nTitle ? z
87550 3a 20 22 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 66 : "???");. if
87560 28 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 ( pHdr->nBacktra
87570 63 65 20 29 7b 0a 20 20 20 20 20 20 66 66 6c 75 ce ){. fflu
87580 73 68 28 6f 75 74 29 3b 0a 20 20 20 20 20 20 70 sh(out);. p
87590 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 Bt = (void**)pHd
875a0 72 3b 0a 20 20 20 20 20 20 70 42 74 20 2d 3d 20 r;. pBt -=
875b0 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
875c0 53 6c 6f 74 73 3b 0a 20 20 20 20 20 20 62 61 63 Slots;. bac
875d0 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 ktrace_symbols_f
875e0 64 28 70 42 74 2c 20 70 48 64 72 2d 3e 6e 42 61 d(pBt, pHdr->nBa
875f0 63 6b 74 72 61 63 65 2c 20 66 69 6c 65 6e 6f 28 cktrace, fileno(
87600 6f 75 74 29 29 3b 0a 20 20 20 20 20 20 66 70 72 out));. fpr
87610 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b intf(out, "\n");
87620 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 70 72 . }. }. fpr
87630 69 6e 74 66 28 6f 75 74 2c 20 22 43 4f 55 4e 54 intf(out, "COUNT
87640 53 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d S:\n");. for(i=
87650 30 3b 20 69 3c 4e 43 53 49 5a 45 2d 31 3b 20 69 0; i<NCSIZE-1; i
87660 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d ++){. if( mem
87670 2e 6e 41 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20 20 .nAlloc[i] ){.
87680 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c fprintf(out,
87690 20 22 20 20 20 25 35 64 3a 20 25 31 30 64 20 25 " %5d: %10d %
876a0 31 30 64 20 25 31 30 64 5c 6e 22 2c 20 0a 20 20 10d %10d\n", .
876b0 20 20 20 20 20 20 20 20 20 20 69 2a 38 2c 20 6d i*8, m
876c0 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d 65 em.nAlloc[i], me
876d0 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2c 20 6d m.nCurrent[i], m
876e0 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d 29 em.mxCurrent[i])
876f0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
87700 28 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 ( mem.nAlloc[NCS
87710 49 5a 45 2d 31 5d 20 29 7b 0a 20 20 20 20 66 70 IZE-1] ){. fp
87720 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 rintf(out, " %
87730 35 64 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 5d: %10d %10d %1
87740 30 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 0d\n",.
87750 20 20 20 20 4e 43 53 49 5a 45 2a 38 2d 38 2c 20 NCSIZE*8-8,
87760 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a mem.nAlloc[NCSIZ
87770 45 2d 31 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 E-1],.
87780 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b mem.nCurrent[
87790 4e 43 53 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e 6d NCSIZE-1], mem.m
877a0 78 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d xCurrent[NCSIZE-
877b0 31 5d 29 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f 73 1]);. }. fclos
877c0 65 28 6f 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a e(out);.}../*.**
877d0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
877e0 65 72 20 6f 66 20 74 69 6d 65 73 20 73 71 6c 69 er of times sqli
877f0 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20 68 te3MemMalloc() h
87800 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a as been called..
87810 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
87820 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d E int sqlite3Mem
87830 64 65 62 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e 74 debugMallocCount
87840 28 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 (){. int i;. i
87850 6e 74 20 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 nt nTotal = 0;.
87860 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49 for(i=0; i<NCSI
87870 5a 45 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 54 ZE; i++){. nT
87880 6f 74 61 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c 6c otal += mem.nAll
87890 6f 63 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 oc[i];. }. ret
878a0 75 72 6e 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 0a urn nTotal;.}...
878b0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
878c0 5f 4d 45 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a _MEMDEBUG */../*
878d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
878e0 64 20 6f 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a d of mem2.c ****
878f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
87920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
87930 67 69 6e 20 66 69 6c 65 20 6d 65 6d 33 2e 63 20 gin file mem3.c
87940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
87970 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72 .** 2007 October
87980 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 14.**.** The au
87990 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
879a0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
879b0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
879c0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
879d0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
879e0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
879f0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
87a00 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
87a10 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
87a20 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
87a30 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
87a40 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
87a50 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
87a60 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
87a70 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
87a80 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
87a90 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
87aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87ae0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
87af0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 contains the C f
87b00 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d unctions that im
87b10 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 plement a memory
87b20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 .** allocation s
87b30 75 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65 ubsystem for use
87b40 20 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a by SQLite. .**.
87b50 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 ** This version
87b60 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c of the memory al
87b70 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
87b80 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 em omits all.**
87b90 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e use of malloc().
87ba0 20 54 68 65 20 53 51 4c 69 74 65 20 75 73 65 72 The SQLite user
87bb0 20 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f 63 supplies a bloc
87bc0 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 k of memory.** b
87bd0 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 efore calling sq
87be0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
87bf0 28 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c () from which al
87c00 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 locations.** are
87c10 20 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e made and return
87c20 65 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f ed by the xMallo
87c30 63 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 c() and xRealloc
87c40 28 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 () .** implement
87c50 61 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c ations. Once sql
87c60 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
87c70 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 ) has been calle
87c80 64 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 d,.** the amount
87c90 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c of memory avail
87ca0 61 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69 able to SQLite i
87cb0 73 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e s fixed and cann
87cc0 6f 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 ot.** be changed
87cd0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 ..**.** This ver
87ce0 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f sion of the memo
87cf0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 ry allocation su
87d00 62 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 bsystem is inclu
87d10 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 ded.** in the bu
87d20 69 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 ild only if SQLI
87d30 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 TE_ENABLE_MEMSYS
87d40 33 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 3 is defined..**
87d50 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 33 2e 63 2c .** $Id: mem3.c,
87d60 76 20 31 2e 32 35 20 32 30 30 38 2f 31 31 2f 31 v 1.25 2008/11/1
87d70 39 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69 65 9 16:52:44 danie
87d80 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
87d90 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 ./*.** This vers
87da0 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ion of the memor
87db0 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 6f y allocator is o
87dc0 6e 6c 79 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 nly built into t
87dd0 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 53 51 he library.** SQ
87de0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 LITE_ENABLE_MEMS
87df0 59 53 33 20 69 73 20 64 65 66 69 6e 65 64 2e 20 YS3 is defined.
87e00 44 65 66 69 6e 69 6e 67 20 74 68 69 73 20 73 79 Defining this sy
87e10 6d 62 6f 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a mbol does not.**
87e20 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c mean that the l
87e30 69 62 72 61 72 79 20 77 69 6c 6c 20 75 73 65 20 ibrary will use
87e40 61 20 6d 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62 79 a memory-pool by
87e50 20 64 65 66 61 75 6c 74 2c 20 6a 75 73 74 20 74 default, just t
87e60 68 61 74 0a 2a 2a 20 69 74 20 69 73 20 61 76 61 hat.** it is ava
87e70 69 6c 61 62 6c 65 2e 20 54 68 65 20 6d 65 6d 70 ilable. The memp
87e80 6f 6f 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 ool allocator is
87e90 20 61 63 74 69 76 61 74 65 64 20 62 79 20 63 61 activated by ca
87ea0 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 lling.** sqlite3
87eb0 5f 63 6f 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23 69 _config()..*/.#i
87ec0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
87ed0 4c 45 5f 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a 2a LE_MEMSYS3../*.*
87ee0 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 28 * Maximum size (
87ef0 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 6f in Mem3Blocks) o
87f00 66 20 61 20 22 73 6d 61 6c 6c 22 20 63 68 75 6e f a "small" chun
87f10 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 k..*/.#define MX
87f20 5f 53 4d 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a _SMALL 10.../*.*
87f30 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 * Number of free
87f40 6c 69 73 74 20 68 61 73 68 20 73 6c 6f 74 73 0a list hash slots.
87f50 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 48 41 53 */.#define N_HAS
87f60 48 20 20 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d H 61../*.** A m
87f70 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
87f80 20 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20 61 20 (also called a
87f90 22 63 68 75 6e 6b 22 29 20 63 6f 6e 73 69 73 74 "chunk") consist
87fa0 73 20 6f 66 20 74 77 6f 20 6f 72 20 0a 2a 2a 20 s of two or .**
87fb0 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 77 68 65 72 more blocks wher
87fc0 65 20 65 61 63 68 20 62 6c 6f 63 6b 20 69 73 20 e each block is
87fd0 38 20 62 79 74 65 73 2e 20 20 54 68 65 20 66 69 8 bytes. The fi
87fe0 72 73 74 20 38 20 62 79 74 65 73 20 61 72 65 20 rst 8 bytes are
87ff0 0a 2a 2a 20 61 20 68 65 61 64 65 72 20 74 68 61 .** a header tha
88000 74 20 69 73 20 6e 6f 74 20 72 65 74 75 72 6e 65 t is not returne
88010 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 2a d to the user..*
88020 2a 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 73 20 *.** A chunk is
88030 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 6c 6f 63 two or more bloc
88040 6b 73 20 74 68 61 74 20 69 73 20 65 69 74 68 65 ks that is eithe
88050 72 20 63 68 65 63 6b 65 64 20 6f 75 74 20 6f 72 r checked out or
88060 0a 2a 2a 20 66 72 65 65 2e 20 20 54 68 65 20 66 .** free. The f
88070 69 72 73 74 20 62 6c 6f 63 6b 20 68 61 73 20 66 irst block has f
88080 6f 72 6d 61 74 20 75 2e 68 64 72 2e 20 20 75 2e ormat u.hdr. u.
88090 68 64 72 2e 73 69 7a 65 34 78 20 69 73 20 34 20 hdr.size4x is 4
880a0 74 69 6d 65 73 20 74 68 65 0a 2a 2a 20 73 69 7a times the.** siz
880b0 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 e of the allocat
880c0 69 6f 6e 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 ion in blocks if
880d0 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 the allocation
880e0 69 73 20 66 72 65 65 2e 0a 2a 2a 20 54 68 65 20 is free..** The
880f0 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 20 62 u.hdr.size4x&1 b
88100 69 74 20 69 73 20 74 72 75 65 20 69 66 20 74 68 it is true if th
88110 65 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b e chunk is check
88120 65 64 20 6f 75 74 20 61 6e 64 0a 2a 2a 20 66 61 ed out and.** fa
88130 6c 73 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b lse if the chunk
88140 20 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c is on the freel
88150 69 73 74 2e 20 20 54 68 65 20 75 2e 68 64 72 2e ist. The u.hdr.
88160 73 69 7a 65 34 78 26 32 20 62 69 74 0a 2a 2a 20 size4x&2 bit.**
88170 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 70 is true if the p
88180 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 revious chunk is
88190 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 checked out and
881a0 20 66 61 6c 73 65 20 69 66 20 74 68 65 0a 2a 2a false if the.**
881b0 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 previous chunk
881c0 69 73 20 66 72 65 65 2e 20 20 54 68 65 20 75 2e is free. The u.
881d0 68 64 72 2e 70 72 65 76 53 69 7a 65 20 66 69 65 hdr.prevSize fie
881e0 6c 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f ld is the size o
881f0 66 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 f.** the previou
88200 73 20 63 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 6b s chunk in block
88210 73 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 s if the previou
88220 73 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68 s chunk is on th
88230 65 0a 2a 2a 20 66 72 65 65 6c 69 73 74 2e 20 49 e.** freelist. I
88240 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 f the previous c
88250 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 hunk is checked
88260 6f 75 74 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e 68 out, then.** u.h
88270 64 72 2e 70 72 65 76 53 69 7a 65 20 63 61 6e 20 dr.prevSize can
88280 62 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 be part of the d
88290 61 74 61 20 66 6f 72 20 74 68 61 74 20 63 68 75 ata for that chu
882a0 6e 6b 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a nk and should.**
882b0 20 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20 not be read or
882c0 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57 written..**.** W
882d0 65 20 6f 66 74 65 6e 20 69 64 65 6e 74 69 66 79 e often identify
882e0 20 61 20 63 68 75 6e 6b 20 62 79 20 69 74 73 20 a chunk by its
882f0 69 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 index in mem3.aP
88300 6f 6f 6c 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a 20 ool[]. When.**
88310 74 68 69 73 20 69 73 20 64 6f 6e 65 2c 20 74 68 this is done, th
88320 65 20 63 68 75 6e 6b 20 69 6e 64 65 78 20 72 65 e chunk index re
88330 66 65 72 73 20 74 6f 20 74 68 65 20 73 65 63 6f fers to the seco
88340 6e 64 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 74 nd block of.** t
88350 68 65 20 63 68 75 6e 6b 2e 20 20 49 6e 20 74 68 he chunk. In th
88360 69 73 20 77 61 79 2c 20 74 68 65 20 66 69 72 73 is way, the firs
88370 74 20 63 68 75 6e 6b 20 68 61 73 20 61 6e 20 69 t chunk has an i
88380 6e 64 65 78 20 6f 66 20 31 2e 0a 2a 2a 20 41 20 ndex of 1..** A
88390 63 68 75 6e 6b 20 69 6e 64 65 78 20 6f 66 20 30 chunk index of 0
883a0 20 6d 65 61 6e 73 20 22 6e 6f 20 73 75 63 68 20 means "no such
883b0 63 68 75 6e 6b 22 20 61 6e 64 20 69 73 20 74 68 chunk" and is th
883c0 65 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20 e equivalent.**
883d0 6f 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 of a NULL pointe
883e0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 r..**.** The sec
883f0 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 66 72 65 ond block of fre
88400 65 20 63 68 75 6e 6b 73 20 69 73 20 6f 66 20 74 e chunks is of t
88410 68 65 20 66 6f 72 6d 20 75 2e 6c 69 73 74 2e 20 he form u.list.
88420 20 54 68 65 0a 2a 2a 20 74 77 6f 20 66 69 65 6c The.** two fiel
88430 64 73 20 66 6f 72 6d 20 61 20 64 6f 75 62 6c 65 ds form a double
88440 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 -linked list of
88450 63 68 75 6e 6b 73 20 6f 66 20 72 65 6c 61 74 65 chunks of relate
88460 64 20 73 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69 6e d sizes..** Poin
88470 74 65 72 73 20 74 6f 20 74 68 65 20 68 65 61 64 ters to the head
88480 20 6f 66 20 74 68 65 20 6c 69 73 74 20 61 72 65 of the list are
88490 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 33 2e stored in mem3.
884a0 61 69 53 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f aiSmall[] .** fo
884b0 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 r smaller chunks
884c0 20 61 6e 64 20 6d 65 6d 33 2e 61 69 48 61 73 68 and mem3.aiHash
884d0 5b 5d 20 66 6f 72 20 6c 61 72 67 65 72 20 63 68 [] for larger ch
884e0 75 6e 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 unks..**.** The
884f0 73 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 second block of
88500 61 20 63 68 75 6e 6b 20 69 73 20 75 73 65 72 20 a chunk is user
88510 64 61 74 61 20 69 66 20 74 68 65 20 63 68 75 6e data if the chun
88520 6b 20 69 73 20 63 68 65 63 6b 65 64 20 0a 2a 2a k is checked .**
88530 20 6f 75 74 2e 20 20 49 66 20 61 20 63 68 75 6e out. If a chun
88540 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 k is checked out
88550 2c 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20 , the user data
88560 6d 61 79 20 65 78 74 65 6e 64 20 69 6e 74 6f 0a may extend into.
88570 2a 2a 20 74 68 65 20 75 2e 68 64 72 2e 70 72 65 ** the u.hdr.pre
88580 76 53 69 7a 65 20 76 61 6c 75 65 20 6f 66 20 74 vSize value of t
88590 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 75 he following chu
885a0 6e 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 nk..*/.typedef s
885b0 74 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 truct Mem3Block
885c0 4d 65 6d 33 42 6c 6f 63 6b 3b 0a 73 74 72 75 63 Mem3Block;.struc
885d0 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20 20 t Mem3Block {.
885e0 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75 union {. stru
885f0 63 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 70 ct {. u32 p
88600 72 65 76 53 69 7a 65 3b 20 20 20 2f 2a 20 53 69 revSize; /* Si
88610 7a 65 20 6f 66 20 70 72 65 76 69 6f 75 73 20 63 ze of previous c
88620 68 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 hunk in Mem3Bloc
88630 6b 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 k elements */.
88640 20 20 20 20 75 33 32 20 73 69 7a 65 34 78 3b 20 u32 size4x;
88650 20 20 20 20 2f 2a 20 34 78 20 74 68 65 20 73 69 /* 4x the si
88660 7a 65 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68 ze of current ch
88670 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b unk in Mem3Block
88680 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 elements */.
88690 20 7d 20 68 64 72 3b 0a 20 20 20 20 73 74 72 75 } hdr;. stru
886a0 63 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 6e ct {. u32 n
886b0 65 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e ext; /* In
886c0 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f dex in mem3.aPoo
886d0 6c 5b 5d 20 6f 66 20 6e 65 78 74 20 66 72 65 65 l[] of next free
886e0 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 chunk */.
886f0 75 33 32 20 70 72 65 76 3b 20 20 20 20 20 20 20 u32 prev;
88700 2f 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33 /* Index in mem3
88710 2e 61 50 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76 .aPool[] of prev
88720 69 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b 20 ious free chunk
88730 2a 2f 0a 20 20 20 20 7d 20 6c 69 73 74 3b 0a 20 */. } list;.
88740 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 } u;.};../*.**
88750 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 All of the stati
88760 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 c variables used
88770 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 by this module
88780 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a are collected.**
88790 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 into a single s
887a0 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 tructure named "
887b0 6d 65 6d 33 22 2e 20 20 54 68 69 73 20 69 73 20 mem3". This is
887c0 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 to keep the.** s
887d0 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 tatic variables
887e0 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f organized and to
887f0 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 reduce namespac
88800 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 e pollution.** w
88810 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 hen this module
88820 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 is combined with
88830 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d other in the am
88840 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 algamation..*/.s
88850 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 tatic SQLITE_WSD
88860 20 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 struct Mem3Glob
88870 61 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d al {. /*. ** M
88880 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 emory available
88890 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 for allocation.
888a0 6e 50 6f 6f 6c 20 69 73 20 74 68 65 20 73 69 7a nPool is the siz
888b0 65 20 6f 66 20 74 68 65 20 61 72 72 61 79 0a 20 e of the array.
888c0 20 2a 2a 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 ** (in Mem3Bloc
888d0 6b 73 29 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 ks) pointed to b
888e0 79 20 61 50 6f 6f 6c 20 6c 65 73 73 20 32 2e 0a y aPool less 2..
888f0 20 20 2a 2f 0a 20 20 75 33 32 20 6e 50 6f 6f 6c */. u32 nPool
88900 3b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 61 ;. Mem3Block *a
88910 50 6f 6f 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a Pool;.. /*. **
88920 20 54 72 75 65 20 69 66 20 77 65 20 61 72 65 20 True if we are
88930 65 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 6f 75 evaluating an ou
88940 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 61 6c 6c t-of-memory call
88950 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 back.. */. int
88960 20 61 6c 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20 alarmBusy;. .
88970 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 /*. ** Mutex t
88980 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 o control access
88990 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 to the memory a
889a0 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
889b0 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 tem.. */. sqli
889c0 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
889d0 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 ;. . /*. ** T
889e0 68 65 20 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e he minimum amoun
889f0 74 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 20 t of free space
88a00 74 68 61 74 20 77 65 20 68 61 76 65 20 73 65 65 that we have see
88a10 6e 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 6d 6e n.. */. u32 mn
88a20 4d 61 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 Master;.. /*.
88a30 2a 2a 20 69 4d 61 73 74 65 72 20 69 73 20 74 68 ** iMaster is th
88a40 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d e index of the m
88a50 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 4d 6f aster chunk. Mo
88a60 73 74 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f st new allocatio
88a70 6e 73 0a 20 20 2a 2a 20 6f 63 63 75 72 20 6f 66 ns. ** occur of
88a80 66 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 2e f of this chunk.
88a90 20 20 73 7a 4d 61 73 74 65 72 20 69 73 20 74 68 szMaster is th
88aa0 65 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42 e size (in Mem3B
88ab0 6c 6f 63 6b 73 29 0a 20 20 2a 2a 20 6f 66 20 74 locks). ** of t
88ac0 68 65 20 63 75 72 72 65 6e 74 20 6d 61 73 74 65 he current maste
88ad0 72 2e 20 20 69 4d 61 73 74 65 72 20 69 73 20 30 r. iMaster is 0
88ae0 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 if there is not
88af0 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 0a 20 master chunk..
88b00 20 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 63 ** The master c
88b10 68 75 6e 6b 20 69 73 20 6e 6f 74 20 69 6e 20 65 hunk is not in e
88b20 69 74 68 65 72 20 74 68 65 20 61 69 48 61 73 68 ither the aiHash
88b30 5b 5d 20 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d 2e [] or aiSmall[].
88b40 0a 20 20 2a 2f 0a 20 20 75 33 32 20 69 4d 61 73 . */. u32 iMas
88b50 74 65 72 3b 0a 20 20 75 33 32 20 73 7a 4d 61 73 ter;. u32 szMas
88b60 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 ter;.. /*. **
88b70 41 72 72 61 79 20 6f 66 20 6c 69 73 74 73 20 6f Array of lists o
88b80 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 61 63 f free blocks ac
88b90 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 62 cording to the b
88ba0 6c 6f 63 6b 20 73 69 7a 65 20 0a 20 20 2a 2a 20 lock size . **
88bb0 66 6f 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e for smaller chun
88bc0 6b 73 2c 20 6f 72 20 61 20 68 61 73 68 20 6f 6e ks, or a hash on
88bd0 20 74 68 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20 the block size
88be0 66 6f 72 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 for larger. **
88bf0 63 68 75 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20 75 chunks.. */. u
88c00 33 32 20 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 4d 32 aiSmall[MX_SM
88c10 41 4c 4c 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f 72 ALL-1]; /* For
88c20 20 73 69 7a 65 73 20 32 20 74 68 72 6f 75 67 68 sizes 2 through
88c30 20 4d 58 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c 75 MX_SMALL, inclu
88c40 73 69 76 65 20 2a 2f 0a 20 20 75 33 32 20 61 69 sive */. u32 ai
88c50 48 61 73 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 20 Hash[N_HASH];
88c60 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 /* For size
88c70 73 20 4d 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e 64 s MX_SMALL+1 and
88c80 20 6c 61 72 67 65 72 20 2a 2f 0a 7d 20 6d 65 6d larger */.} mem
88c90 33 20 3d 20 7b 20 39 37 35 33 35 35 37 35 20 7d 3 = { 97535575 }
88ca0 3b 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 33 20 ;..#define mem3
88cb0 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65 GLOBAL(struct Me
88cc0 6d 33 47 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29 0a m3Global, mem3).
88cd0 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 ./*.** Unlink th
88ce0 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e e chunk at mem3.
88cf0 61 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 aPool[i] from li
88d00 73 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 st it is current
88d10 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f 6f ly.** on. *pRoo
88d20 74 20 69 73 20 74 68 65 20 6c 69 73 74 20 74 68 t is the list th
88d30 61 74 20 69 20 69 73 20 61 20 6d 65 6d 62 65 72 at i is a member
88d40 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 of..*/.static v
88d50 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e oid memsys3Unlin
88d60 6b 46 72 6f 6d 4c 69 73 74 28 75 33 32 20 69 2c kFromList(u32 i,
88d70 20 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 u32 *pRoot){.
88d80 75 33 32 20 6e 65 78 74 20 3d 20 6d 65 6d 33 2e u32 next = mem3.
88d90 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e aPool[i].u.list.
88da0 6e 65 78 74 3b 0a 20 20 75 33 32 20 70 72 65 76 next;. u32 prev
88db0 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d = mem3.aPool[i]
88dc0 2e 75 2e 6c 69 73 74 2e 70 72 65 76 3b 0a 20 20 .u.list.prev;.
88dd0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
88de0 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e mutex_held(mem3.
88df0 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
88e00 70 72 65 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2a prev==0 ){. *
88e10 70 52 6f 6f 74 20 3d 20 6e 65 78 74 3b 0a 20 20 pRoot = next;.
88e20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 33 2e }else{. mem3.
88e30 61 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 aPool[prev].u.li
88e40 73 74 2e 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a st.next = next;.
88e50 20 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 20 29 }. if( next )
88e60 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c {. mem3.aPool
88e70 5b 6e 65 78 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 [next].u.list.pr
88e80 65 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 20 ev = prev;. }.
88e90 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 mem3.aPool[i].u
88ea0 2e 6c 69 73 74 2e 6e 65 78 74 20 3d 20 30 3b 0a .list.next = 0;.
88eb0 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e mem3.aPool[i].
88ec0 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30 3b u.list.prev = 0;
88ed0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b .}../*.** Unlink
88ee0 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 6e the chunk at in
88ef0 64 65 78 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 77 dex i from .** w
88f00 68 61 74 65 76 65 72 20 6c 69 73 74 20 69 73 20 hatever list is
88f10 63 75 72 72 65 6e 74 6c 79 20 61 20 6d 65 6d 62 currently a memb
88f20 65 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 er of..*/.static
88f30 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c void memsys3Unl
88f40 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 33 ink(u32 i){. u3
88f50 32 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 2 size, hash;.
88f60 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
88f70 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e mutex_held(mem3.
88f80 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
88f90 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b rt( (mem3.aPool[
88fa0 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 i-1].u.hdr.size4
88fb0 78 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 61 x & 1)==0 );. a
88fc0 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 ssert( i>=1 );.
88fd0 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f size = mem3.aPo
88fe0 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ol[i-1].u.hdr.si
88ff0 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 ze4x/4;. assert
89000 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f ( size==mem3.aPo
89010 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 ol[i+size-1].u.h
89020 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 dr.prevSize );.
89030 20 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 assert( size>=2
89040 20 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 3c );. if( size <
89050 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 = MX_SMALL ){.
89060 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 memsys3UnlinkF
89070 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 romList(i, &mem3
89080 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d .aiSmall[size-2]
89090 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
890a0 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f hash = size % N_
890b0 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 HASH;. memsys
890c0 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 3UnlinkFromList(
890d0 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b i, &mem3.aiHash[
890e0 68 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f hash]);. }.}../
890f0 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 *.** Link the ch
89100 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f unk at mem3.aPoo
89110 6c 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20 l[i] so that is
89120 6f 6e 20 74 68 65 20 6c 69 73 74 20 72 6f 6f 74 on the list root
89130 65 64 0a 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 2e ed.** at *pRoot.
89140 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
89150 6d 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c memsys3LinkIntoL
89160 69 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a ist(u32 i, u32 *
89170 70 52 6f 6f 74 29 7b 0a 20 20 61 73 73 65 72 74 pRoot){. assert
89180 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
89190 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 held(mem3.mutex)
891a0 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c );. mem3.aPool
891b0 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 [i].u.list.next
891c0 3d 20 2a 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d 33 = *pRoot;. mem3
891d0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 .aPool[i].u.list
891e0 2e 70 72 65 76 20 3d 20 30 3b 0a 20 20 69 66 28 .prev = 0;. if(
891f0 20 2a 70 52 6f 6f 74 20 29 7b 0a 20 20 20 20 6d *pRoot ){. m
89200 65 6d 33 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f 74 em3.aPool[*pRoot
89210 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 ].u.list.prev =
89220 69 3b 0a 20 20 7d 0a 20 20 2a 70 52 6f 6f 74 20 i;. }. *pRoot
89230 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 = i;.}../*.** Li
89240 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 nk the chunk at
89250 69 6e 64 65 78 20 69 20 69 6e 74 6f 20 65 69 74 index i into eit
89260 68 65 72 20 74 68 65 20 61 70 70 72 6f 70 72 69 her the appropri
89270 61 74 65 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68 75 ate.** small chu
89280 6e 6b 20 6c 69 73 74 2c 20 6f 72 20 69 6e 74 6f nk list, or into
89290 20 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b the large chunk
892a0 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a hash table..*/.
892b0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
892c0 79 73 33 4c 69 6e 6b 28 75 33 32 20 69 29 7b 0a ys3Link(u32 i){.
892d0 20 20 75 33 32 20 73 69 7a 65 2c 20 68 61 73 68 u32 size, hash
892e0 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
892f0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d te3_mutex_held(m
89300 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 em3.mutex) );.
89310 61 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a assert( i>=1 );.
89320 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e assert( (mem3.
89330 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 aPool[i-1].u.hdr
89340 2e 73 69 7a 65 34 78 20 26 20 31 29 3d 3d 30 20 .size4x & 1)==0
89350 29 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 );. size = mem3
89360 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 .aPool[i-1].u.hd
89370 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 r.size4x/4;. as
89380 73 65 72 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 sert( size==mem3
89390 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d .aPool[i+size-1]
893a0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 .u.hdr.prevSize
893b0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a );. assert( siz
893c0 65 3e 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 69 e>=2 );. if( si
893d0 7a 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 ze <= MX_SMALL )
893e0 7b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e {. memsys3Lin
893f0 6b 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 kIntoList(i, &me
89400 6d 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d m3.aiSmall[size-
89410 32 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 2]);. }else{.
89420 20 20 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20 hash = size %
89430 4e 5f 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 N_HASH;. mems
89440 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 ys3LinkIntoList(
89450 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b i, &mem3.aiHash[
89460 68 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f hash]);. }.}../
89470 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54 *.** If the STAT
89480 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 IC_MEM mutex is
89490 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 not already held
894a0 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e , obtain it now.
894b0 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 The mutex.** wi
894c0 6c 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65 ll already be he
894d0 6c 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20 ld (obtained by
894e0 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 code in malloc.c
894f0 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 ) if.** sqlite3G
89500 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d lobalConfig.bMem
89510 53 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f Stat is true..*/
89520 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
89530 73 79 73 33 45 6e 74 65 72 28 76 6f 69 64 29 7b sys3Enter(void){
89540 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c . if( sqlite3Gl
89550 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 obalConfig.bMems
89560 74 61 74 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 6d tat==0 && mem3.m
89570 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d utex==0 ){. m
89580 65 6d 33 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 em3.mutex = sqli
89590 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
895a0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
895b0 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 C_MEM);. }. sq
895c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
895d0 72 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d r(mem3.mutex);.}
895e0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
895f0 73 79 73 33 4c 65 61 76 65 28 76 6f 69 64 29 7b sys3Leave(void){
89600 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
89610 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 _leave(mem3.mute
89620 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c x);.}../*.** Cal
89630 6c 65 64 20 77 68 65 6e 20 77 65 20 61 72 65 20 led when we are
89640 75 6e 61 62 6c 65 20 74 6f 20 73 61 74 69 73 66 unable to satisf
89650 79 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 y an allocation
89660 6f 66 20 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73 74 of nBytes..*/.st
89670 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 atic void memsys
89680 33 4f 75 74 4f 66 4d 65 6d 6f 72 79 28 69 6e 74 3OutOfMemory(int
89690 20 6e 42 79 74 65 29 7b 0a 20 20 69 66 28 20 21 nByte){. if( !
896a0 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 29 mem3.alarmBusy )
896b0 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d {. mem3.alarm
896c0 42 75 73 79 20 3d 20 31 3b 0a 20 20 20 20 61 73 Busy = 1;. as
896d0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
896e0 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 tex_held(mem3.mu
896f0 74 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 tex) );. sqli
89700 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
89710 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20 mem3.mutex);.
89720 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 sqlite3_release
89730 5f 6d 65 6d 6f 72 79 28 6e 42 79 74 65 29 3b 0a _memory(nByte);.
89740 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
89750 78 5f 65 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74 x_enter(mem3.mut
89760 65 78 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c ex);. mem3.al
89770 61 72 6d 42 75 73 79 20 3d 20 30 3b 0a 20 20 7d armBusy = 0;. }
89780 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e 6b .}.../*.** Chunk
89790 20 69 20 69 73 20 61 20 66 72 65 65 20 63 68 75 i is a free chu
897a0 6e 6b 20 74 68 61 74 20 68 61 73 20 62 65 65 6e nk that has been
897b0 20 75 6e 6c 69 6e 6b 65 64 2e 20 20 41 64 6a 75 unlinked. Adju
897c0 73 74 20 69 74 73 20 0a 2a 2a 20 73 69 7a 65 20 st its .** size
897d0 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 63 parameters for c
897e0 68 65 63 6b 2d 6f 75 74 20 61 6e 64 20 72 65 74 heck-out and ret
897f0 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
89800 20 74 68 65 20 0a 2a 2a 20 75 73 65 72 20 70 6f the .** user po
89810 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68 75 rtion of the chu
89820 6e 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f nk..*/.static vo
89830 69 64 20 2a 6d 65 6d 73 79 73 33 43 68 65 63 6b id *memsys3Check
89840 6f 75 74 28 75 33 32 20 69 2c 20 75 33 32 20 6e out(u32 i, u32 n
89850 42 6c 6f 63 6b 29 7b 0a 20 20 75 33 32 20 78 3b Block){. u32 x;
89860 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
89870 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 e3_mutex_held(me
89880 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 m3.mutex) );. a
89890 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 ssert( i>=1 );.
898a0 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 assert( mem3.aP
898b0 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 ool[i-1].u.hdr.s
898c0 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 ize4x/4==nBlock
898d0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d );. assert( mem
898e0 33 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 3.aPool[i+nBlock
898f0 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 -1].u.hdr.prevSi
89900 7a 65 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 ze==nBlock );.
89910 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 x = mem3.aPool[i
89920 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
89930 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 ;. mem3.aPool[i
89940 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
89950 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 20 = nBlock*4 | 1
89960 7c 20 28 78 26 32 29 3b 0a 20 20 6d 65 6d 33 2e | (x&2);. mem3.
89970 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 aPool[i+nBlock-1
89980 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 ].u.hdr.prevSize
89990 20 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 6d = nBlock;. mem
899a0 33 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 3.aPool[i+nBlock
899b0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
899c0 20 7c 3d 20 32 3b 0a 20 20 72 65 74 75 72 6e 20 |= 2;. return
899d0 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a &mem3.aPool[i];.
899e0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 72 76 65 20 61 }../*.** Carve a
899f0 20 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68 piece off of th
89a00 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 65 6d e end of the mem
89a10 33 2e 69 4d 61 73 74 65 72 20 66 72 65 65 20 63 3.iMaster free c
89a20 68 75 6e 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 hunk..** Return
89a30 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
89a40 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e new allocation.
89a50 20 20 4f 72 2c 20 69 66 20 74 68 65 20 6d 61 73 Or, if the mas
89a60 74 65 72 20 63 68 75 6e 6b 0a 2a 2a 20 69 73 20 ter chunk.** is
89a70 6e 6f 74 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 not large enough
89a80 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 , return 0..*/.s
89a90 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 tatic void *mems
89aa0 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 75 33 ys3FromMaster(u3
89ab0 32 20 6e 42 6c 6f 63 6b 29 7b 0a 20 20 61 73 73 2 nBlock){. ass
89ac0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
89ad0 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 ex_held(mem3.mut
89ae0 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
89af0 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d mem3.szMaster>=
89b00 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 69 66 28 20 nBlock );. if(
89b10 6e 42 6c 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d nBlock>=mem3.szM
89b20 61 73 74 65 72 2d 31 20 29 7b 0a 20 20 20 20 2f aster-1 ){. /
89b30 2a 20 55 73 65 20 74 68 65 20 65 6e 74 69 72 65 * Use the entire
89b40 20 6d 61 73 74 65 72 20 2a 2f 0a 20 20 20 20 76 master */. v
89b50 6f 69 64 20 2a 70 20 3d 20 6d 65 6d 73 79 73 33 oid *p = memsys3
89b60 43 68 65 63 6b 6f 75 74 28 6d 65 6d 33 2e 69 4d Checkout(mem3.iM
89b70 61 73 74 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d 61 aster, mem3.szMa
89b80 73 74 65 72 29 3b 0a 20 20 20 20 6d 65 6d 33 2e ster);. mem3.
89b90 69 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 iMaster = 0;.
89ba0 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d mem3.szMaster =
89bb0 20 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d 0;. mem3.mnM
89bc0 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 72 aster = 0;. r
89bd0 65 74 75 72 6e 20 70 3b 0a 20 20 7d 65 6c 73 65 eturn p;. }else
89be0 7b 0a 20 20 20 20 2f 2a 20 53 70 6c 69 74 20 74 {. /* Split t
89bf0 68 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e he master block.
89c00 20 20 52 65 74 75 72 6e 20 74 68 65 20 74 61 69 Return the tai
89c10 6c 2e 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 65 l. */. u32 ne
89c20 77 69 2c 20 78 3b 0a 20 20 20 20 6e 65 77 69 20 wi, x;. newi
89c30 3d 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 2b = mem3.iMaster +
89c40 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d mem3.szMaster -
89c50 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 61 73 73 nBlock;. ass
89c60 65 72 74 28 20 6e 65 77 69 20 3e 20 6d 65 6d 33 ert( newi > mem3
89c70 2e 69 4d 61 73 74 65 72 2b 31 20 29 3b 0a 20 20 .iMaster+1 );.
89c80 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d mem3.aPool[mem
89c90 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 3.iMaster+mem3.s
89ca0 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 zMaster-1].u.hdr
89cb0 2e 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f .prevSize = nBlo
89cc0 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f ck;. mem3.aPo
89cd0 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b ol[mem3.iMaster+
89ce0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d mem3.szMaster-1]
89cf0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d .u.hdr.size4x |=
89d00 20 32 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 2;. mem3.aPo
89d10 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 ol[newi-1].u.hdr
89d20 2e 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b .size4x = nBlock
89d30 2a 34 20 2b 20 31 3b 0a 20 20 20 20 6d 65 6d 33 *4 + 1;. mem3
89d40 2e 73 7a 4d 61 73 74 65 72 20 2d 3d 20 6e 42 6c .szMaster -= nBl
89d50 6f 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 ock;. mem3.aP
89d60 6f 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 ool[newi-1].u.hd
89d70 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d r.prevSize = mem
89d80 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 3.szMaster;.
89d90 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d x = mem3.aPool[m
89da0 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 em3.iMaster-1].u
89db0 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b .hdr.size4x & 2;
89dc0 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b . mem3.aPool[
89dd0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e mem3.iMaster-1].
89de0 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d u.hdr.size4x = m
89df0 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c em3.szMaster*4 |
89e00 20 78 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 x;. if( mem3
89e10 2e 73 7a 4d 61 73 74 65 72 20 3c 20 6d 65 6d 33 .szMaster < mem3
89e20 2e 6d 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 .mnMaster ){.
89e30 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 mem3.mnMaster
89e40 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 = mem3.szMaster
89e50 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
89e60 72 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d 33 2e rn (void*)&mem3.
89e70 61 50 6f 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20 7d aPool[newi];. }
89e80 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f 74 .}../*.** *pRoot
89e90 20 69 73 20 74 68 65 20 68 65 61 64 20 6f 66 20 is the head of
89ea0 61 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 63 a list of free c
89eb0 68 75 6e 6b 73 20 6f 66 20 74 68 65 20 73 61 6d hunks of the sam
89ec0 65 20 73 69 7a 65 0a 2a 2a 20 6f 72 20 73 61 6d e size.** or sam
89ed0 65 20 73 69 7a 65 20 68 61 73 68 2e 20 20 49 6e e size hash. In
89ee0 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 2a 70 other words, *p
89ef0 52 6f 6f 74 20 69 73 20 61 6e 20 65 6e 74 72 79 Root is an entry
89f00 20 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 6d 65 in either.** me
89f10 6d 33 2e 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72 20 m3.aiSmall[] or
89f20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 5d 2e 20 20 mem3.aiHash[].
89f30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
89f40 69 6e 65 20 65 78 61 6d 69 6e 65 73 20 61 6c 6c ine examines all
89f50 20 65 6e 74 72 69 65 73 20 6f 6e 20 74 68 65 20 entries on the
89f60 67 69 76 65 6e 20 6c 69 73 74 20 61 6e 64 20 74 given list and t
89f70 72 69 65 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c 65 ries.** to coale
89f80 73 63 65 20 65 61 63 68 20 65 6e 74 72 69 65 73 sce each entries
89f90 20 77 69 74 68 20 61 64 6a 61 63 65 6e 74 20 66 with adjacent f
89fa0 72 65 65 20 63 68 75 6e 6b 73 2e 20 20 0a 2a 2a ree chunks. .**
89fb0 0a 2a 2a 20 49 66 20 69 74 20 73 65 65 73 20 61 .** If it sees a
89fc0 20 63 68 75 6e 6b 20 74 68 61 74 20 69 73 20 6c chunk that is l
89fd0 61 72 67 65 72 20 74 68 61 6e 20 6d 65 6d 33 2e arger than mem3.
89fe0 69 4d 61 73 74 65 72 2c 20 69 74 20 72 65 70 6c iMaster, it repl
89ff0 61 63 65 73 20 0a 2a 2a 20 74 68 65 20 63 75 72 aces .** the cur
8a000 72 65 6e 74 20 6d 65 6d 33 2e 69 4d 61 73 74 65 rent mem3.iMaste
8a010 72 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 6c r with the new l
8a020 61 72 67 65 72 20 63 68 75 6e 6b 2e 20 20 49 6e arger chunk. In
8a030 20 6f 72 64 65 72 20 66 6f 72 0a 2a 2a 20 74 68 order for.** th
8a040 69 73 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 is mem3.iMaster
8a050 72 65 70 6c 61 63 65 6d 65 6e 74 20 74 6f 20 77 replacement to w
8a060 6f 72 6b 2c 20 74 68 65 20 6d 61 73 74 65 72 20 ork, the master
8a070 63 68 75 6e 6b 20 6d 75 73 74 20 62 65 0a 2a 2a chunk must be.**
8a080 20 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65 linked into the
8a090 20 68 61 73 68 20 74 61 62 6c 65 73 2e 20 20 54 hash tables. T
8a0a0 68 61 74 20 69 73 20 6e 6f 74 20 74 68 65 20 6e hat is not the n
8a0b0 6f 72 6d 61 6c 20 73 74 61 74 65 20 6f 66 0a 2a ormal state of.*
8a0c0 2a 20 61 66 66 61 69 72 73 2c 20 6f 66 20 63 6f * affairs, of co
8a0d0 75 72 73 65 2e 20 20 54 68 65 20 63 61 6c 6c 69 urse. The calli
8a0e0 6e 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 ng routine must
8a0f0 6c 69 6e 6b 20 74 68 65 20 6d 61 73 74 65 72 0a link the master.
8a100 2a 2a 20 63 68 75 6e 6b 20 62 65 66 6f 72 65 20 ** chunk before
8a110 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f invoking this ro
8a120 75 74 69 6e 65 2c 20 74 68 65 6e 20 6d 75 73 74 utine, then must
8a130 20 75 6e 6c 69 6e 6b 20 74 68 65 20 28 70 6f 73 unlink the (pos
8a140 73 69 62 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 64 sibly.** changed
8a150 29 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6f ) master chunk o
8a160 6e 63 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 nce this routine
8a170 20 68 61 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a has finished..*
8a180 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 /.static void me
8a190 6d 73 79 73 33 4d 65 72 67 65 28 75 33 32 20 2a msys3Merge(u32 *
8a1a0 70 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 69 4e pRoot){. u32 iN
8a1b0 65 78 74 2c 20 70 72 65 76 2c 20 73 69 7a 65 2c ext, prev, size,
8a1c0 20 69 2c 20 78 3b 0a 0a 20 20 61 73 73 65 72 74 i, x;.. assert
8a1d0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
8a1e0 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 held(mem3.mutex)
8a1f0 20 29 3b 0a 20 20 66 6f 72 28 69 3d 2a 70 52 6f );. for(i=*pRo
8a200 6f 74 3b 20 69 3e 30 3b 20 69 3d 69 4e 65 78 74 ot; i>0; i=iNext
8a210 29 7b 0a 20 20 20 20 69 4e 65 78 74 20 3d 20 6d ){. iNext = m
8a220 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c em3.aPool[i].u.l
8a230 69 73 74 2e 6e 65 78 74 3b 0a 20 20 20 20 73 69 ist.next;. si
8a240 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b ze = mem3.aPool[
8a250 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 i-1].u.hdr.size4
8a260 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 x;. assert( (
8a270 73 69 7a 65 26 31 29 3d 3d 30 20 29 3b 0a 20 20 size&1)==0 );.
8a280 20 20 69 66 28 20 28 73 69 7a 65 26 32 29 3d 3d if( (size&2)==
8a290 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 0 ){. memsy
8a2a0 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 s3UnlinkFromList
8a2b0 28 69 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 (i, pRoot);.
8a2c0 20 20 61 73 73 65 72 74 28 20 69 20 3e 20 6d 65 assert( i > me
8a2d0 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e m3.aPool[i-1].u.
8a2e0 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a hdr.prevSize );.
8a2f0 20 20 20 20 20 20 70 72 65 76 20 3d 20 69 20 2d prev = i -
8a300 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d mem3.aPool[i-1]
8a310 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b .u.hdr.prevSize;
8a320 0a 20 20 20 20 20 20 69 66 28 20 70 72 65 76 3d . if( prev=
8a330 3d 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 =iNext ){.
8a340 20 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 iNext = mem3.a
8a350 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 Pool[prev].u.lis
8a360 74 2e 6e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a t.next;. }.
8a370 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c memsys3Unl
8a380 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 ink(prev);.
8a390 20 73 69 7a 65 20 3d 20 69 20 2b 20 73 69 7a 65 size = i + size
8a3a0 2f 34 20 2d 20 70 72 65 76 3b 0a 20 20 20 20 20 /4 - prev;.
8a3b0 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b x = mem3.aPool[
8a3c0 70 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 prev-1].u.hdr.si
8a3d0 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 ze4x & 2;.
8a3e0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d mem3.aPool[prev-
8a3f0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 1].u.hdr.size4x
8a400 3d 20 73 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 20 = size*4 | x;.
8a410 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 mem3.aPool[p
8a420 72 65 76 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 rev+size-1].u.hd
8a430 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 73 69 7a r.prevSize = siz
8a440 65 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 e;. memsys3
8a450 4c 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 Link(prev);.
8a460 20 20 69 20 3d 20 70 72 65 76 3b 0a 20 20 20 20 i = prev;.
8a470 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 69 7a }else{. siz
8a480 65 20 2f 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 e /= 4;. }.
8a490 20 20 69 66 28 20 73 69 7a 65 3e 6d 65 6d 33 2e if( size>mem3.
8a4a0 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 szMaster ){.
8a4b0 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d mem3.iMaster =
8a4c0 20 69 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 i;. mem3.s
8a4d0 7a 4d 61 73 74 65 72 20 3d 20 73 69 7a 65 3b 0a zMaster = size;.
8a4e0 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
8a4f0 2a 2a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 63 ** Return a bloc
8a500 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61 k of memory of a
8a510 74 20 6c 65 61 73 74 20 6e 42 79 74 65 73 20 69 t least nBytes i
8a520 6e 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 n size..** Retur
8a530 6e 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65 n NULL if unable
8a540 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
8a550 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 ction assumes th
8a560 61 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 at the necessary
8a570 20 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 mutexes, if any
8a580 2c 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 , are.** already
8a590 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c held by the cal
8a5a0 6c 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 ler. Hence "Unsa
8a5b0 66 65 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 fe"..*/.static v
8a5c0 6f 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c oid *memsys3Mall
8a5d0 6f 63 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 ocUnsafe(int nBy
8a5e0 74 65 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20 te){. u32 i;.
8a5f0 75 33 32 20 6e 42 6c 6f 63 6b 3b 0a 20 20 75 33 u32 nBlock;. u3
8a600 32 20 74 6f 46 72 65 65 3b 0a 0a 20 20 61 73 73 2 toFree;.. ass
8a610 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
8a620 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 ex_held(mem3.mut
8a630 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
8a640 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 sizeof(Mem3Bloc
8a650 6b 29 3d 3d 38 20 29 3b 0a 20 20 69 66 28 20 6e k)==8 );. if( n
8a660 42 79 74 65 3c 3d 31 32 20 29 7b 0a 20 20 20 20 Byte<=12 ){.
8a670 6e 42 6c 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d 65 nBlock = 2;. }e
8a680 6c 73 65 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 lse{. nBlock
8a690 3d 20 28 6e 42 79 74 65 20 2b 20 31 31 29 2f 38 = (nByte + 11)/8
8a6a0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
8a6b0 6e 42 6c 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20 20 nBlock>=2 );..
8a6c0 2f 2a 20 53 54 45 50 20 31 3a 0a 20 20 2a 2a 20 /* STEP 1:. **
8a6d0 4c 6f 6f 6b 20 66 6f 72 20 61 6e 20 65 6e 74 72 Look for an entr
8a6e0 79 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74 y of the correct
8a6f0 20 73 69 7a 65 20 69 6e 20 65 69 74 68 65 72 20 size in either
8a700 74 68 65 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20 63 the small. ** c
8a710 68 75 6e 6b 20 74 61 62 6c 65 20 6f 72 20 69 6e hunk table or in
8a720 20 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b the large chunk
8a730 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 hash table. Th
8a740 69 73 20 69 73 0a 20 20 2a 2a 20 73 75 63 63 65 is is. ** succe
8a750 73 73 66 75 6c 20 6d 6f 73 74 20 6f 66 20 74 68 ssful most of th
8a760 65 20 74 69 6d 65 20 28 61 62 6f 75 74 20 39 20 e time (about 9
8a770 74 69 6d 65 73 20 6f 75 74 20 6f 66 20 31 30 29 times out of 10)
8a780 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 6c .. */. if( nBl
8a790 6f 63 6b 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 ock <= MX_SMALL
8a7a0 29 7b 0a 20 20 20 20 69 20 3d 20 6d 65 6d 33 2e ){. i = mem3.
8a7b0 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 aiSmall[nBlock-2
8a7c0 5d 3b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29 ];. if( i>0 )
8a7d0 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 {. memsys3U
8a7e0 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c nlinkFromList(i,
8a7f0 20 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e &mem3.aiSmall[n
8a800 42 6c 6f 63 6b 2d 32 5d 29 3b 0a 20 20 20 20 20 Block-2]);.
8a810 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 return memsys3C
8a820 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 heckout(i, nBloc
8a830 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 k);. }. }els
8a840 65 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 68 20 e{. int hash
8a850 3d 20 6e 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 53 = nBlock % N_HAS
8a860 48 3b 0a 20 20 20 20 66 6f 72 28 69 3d 6d 65 6d H;. for(i=mem
8a870 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 3b 20 3.aiHash[hash];
8a880 69 3e 30 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f 6f i>0; i=mem3.aPoo
8a890 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 l[i].u.list.next
8a8a0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d ){. if( mem
8a8b0 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
8a8c0 64 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c dr.size4x/4==nBl
8a8d0 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 6d ock ){. m
8a8e0 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d emsys3UnlinkFrom
8a8f0 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 List(i, &mem3.ai
8a900 48 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20 20 Hash[hash]);.
8a910 20 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 return mems
8a920 79 73 33 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e ys3Checkout(i, n
8a930 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a Block);. }.
8a940 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
8a950 53 54 45 50 20 32 3a 0a 20 20 2a 2a 20 54 72 79 STEP 2:. ** Try
8a960 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 to satisfy the
8a970 61 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 allocation by ca
8a980 72 76 69 6e 67 20 61 20 70 69 65 63 65 20 6f 66 rving a piece of
8a990 66 20 6f 66 20 74 68 65 20 65 6e 64 0a 20 20 2a f of the end. *
8a9a0 2a 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 * of the master
8a9b0 63 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 chunk. This ste
8a9c0 70 20 75 73 75 61 6c 6c 79 20 77 6f 72 6b 73 20 p usually works
8a9d0 69 66 20 73 74 65 70 20 31 20 66 61 69 6c 73 2e if step 1 fails.
8a9e0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 . */. if( mem3
8a9f0 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 .szMaster>=nBloc
8aa00 6b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 k ){. return
8aa10 6d 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 memsys3FromMaste
8aa20 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a r(nBlock);. }..
8aa30 0a 20 20 2f 2a 20 53 54 45 50 20 33 3a 20 20 0a . /* STEP 3: .
8aa40 20 20 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 ** Loop throug
8aa50 68 20 74 68 65 20 65 6e 74 69 72 65 20 6d 65 6d h the entire mem
8aa60 6f 72 79 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c 65 ory pool. Coale
8aa70 73 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72 65 sce adjacent fre
8aa80 65 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e 20 20 e. ** chunks.
8aa90 52 65 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 Recompute the ma
8aaa0 73 74 65 72 20 63 68 75 6e 6b 20 61 73 20 74 68 ster chunk as th
8aab0 65 20 6c 61 72 67 65 73 74 20 66 72 65 65 20 63 e largest free c
8aac0 68 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e 20 hunk.. ** Then
8aad0 74 72 79 20 61 67 61 69 6e 20 74 6f 20 73 61 74 try again to sat
8aae0 69 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 isfy the allocat
8aaf0 69 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61 ion by carving a
8ab00 20 70 69 65 63 65 20 6f 66 66 0a 20 20 2a 2a 20 piece off. **
8ab10 6f 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 of the end of th
8ab20 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 e master chunk.
8ab30 20 54 68 69 73 20 73 74 65 70 20 68 61 70 70 65 This step happe
8ab40 6e 73 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 ns very. ** rar
8ab50 65 6c 79 20 28 77 65 20 68 6f 70 65 21 29 0a 20 ely (we hope!).
8ab60 20 2a 2f 0a 20 20 66 6f 72 28 74 6f 46 72 65 65 */. for(toFree
8ab70 3d 6e 42 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46 72 =nBlock*16; toFr
8ab80 65 65 3c 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 31 ee<(mem3.nPool*1
8ab90 36 29 3b 20 74 6f 46 72 65 65 20 2a 3d 20 32 29 6); toFree *= 2)
8aba0 7b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4f 75 74 {. memsys3Out
8abb0 4f 66 4d 65 6d 6f 72 79 28 74 6f 46 72 65 65 29 OfMemory(toFree)
8abc0 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 69 ;. if( mem3.i
8abd0 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 Master ){.
8abe0 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 6d 65 6d 33 memsys3Link(mem3
8abf0 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 .iMaster);.
8ac00 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 mem3.iMaster =
8ac10 30 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 0;. mem3.sz
8ac20 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 Master = 0;.
8ac30 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 }. for(i=0; i
8ac40 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 <N_HASH; i++){.
8ac50 20 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72 67 memsys3Merg
8ac60 65 28 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 e(&mem3.aiHash[i
8ac70 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f ]);. }. fo
8ac80 72 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c r(i=0; i<MX_SMAL
8ac90 4c 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 L-1; i++){.
8aca0 20 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26 6d memsys3Merge(&m
8acb0 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29 3b em3.aiSmall[i]);
8acc0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d . }. if( m
8acd0 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a em3.szMaster ){.
8ace0 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c memsys3Unl
8acf0 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ink(mem3.iMaster
8ad00 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d );. if( mem
8ad10 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 3.szMaster>=nBlo
8ad20 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 ck ){. re
8ad30 74 75 72 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d turn memsys3From
8ad40 4d 61 73 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a Master(nBlock);.
8ad50 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
8ad60 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20 }.. /* If none
8ad70 6f 66 20 74 68 65 20 61 62 6f 76 65 20 77 6f 72 of the above wor
8ad80 6b 65 64 2c 20 74 68 65 6e 20 77 65 20 66 61 69 ked, then we fai
8ad90 6c 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 l. */. return 0
8ada0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 ;.}../*.** Free
8adb0 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d an outstanding m
8adc0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
8add0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
8ade0 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 ction assumes th
8adf0 61 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 at the necessary
8ae00 20 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 mutexes, if any
8ae10 2c 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 , are.** already
8ae20 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c held by the cal
8ae30 6c 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 ler. Hence "Unsa
8ae40 66 65 22 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d fe"..*/.void mem
8ae50 73 79 73 33 46 72 65 65 55 6e 73 61 66 65 28 76 sys3FreeUnsafe(v
8ae60 6f 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 4d 65 oid *pOld){. Me
8ae70 6d 33 42 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d 65 m3Block *p = (Me
8ae80 6d 33 42 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0a 20 m3Block*)pOld;.
8ae90 20 69 6e 74 20 69 3b 0a 20 20 75 33 32 20 73 69 int i;. u32 si
8aea0 7a 65 2c 20 78 3b 0a 20 20 61 73 73 65 72 74 28 ze, x;. assert(
8aeb0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
8aec0 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
8aed0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 3e 6d );. assert( p>m
8aee0 65 6d 33 2e 61 50 6f 6f 6c 20 26 26 20 70 3c 26 em3.aPool && p<&
8aef0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e mem3.aPool[mem3.
8af00 6e 50 6f 6f 6c 5d 20 29 3b 0a 20 20 69 20 3d 20 nPool] );. i =
8af10 70 20 2d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 3b 0a p - mem3.aPool;.
8af20 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e assert( (mem3.
8af30 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 aPool[i-1].u.hdr
8af40 2e 73 69 7a 65 34 78 26 31 29 3d 3d 31 20 29 3b .size4x&1)==1 );
8af50 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 . size = mem3.a
8af60 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e Pool[i-1].u.hdr.
8af70 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 size4x/4;. asse
8af80 72 74 28 20 69 2b 73 69 7a 65 3c 3d 6d 65 6d 33 rt( i+size<=mem3
8af90 2e 6e 50 6f 6f 6c 2b 31 20 29 3b 0a 20 20 6d 65 .nPool+1 );. me
8afa0 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e m3.aPool[i-1].u.
8afb0 68 64 72 2e 73 69 7a 65 34 78 20 26 3d 20 7e 31 hdr.size4x &= ~1
8afc0 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 ;. mem3.aPool[i
8afd0 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 +size-1].u.hdr.p
8afe0 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a revSize = size;.
8aff0 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 mem3.aPool[i+s
8b000 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a ize-1].u.hdr.siz
8b010 65 34 78 20 26 3d 20 7e 32 3b 0a 20 20 6d 65 6d e4x &= ~2;. mem
8b020 73 79 73 33 4c 69 6e 6b 28 69 29 3b 0a 0a 20 20 sys3Link(i);..
8b030 2f 2a 20 54 72 79 20 74 6f 20 65 78 70 61 6e 64 /* Try to expand
8b040 20 74 68 65 20 6d 61 73 74 65 72 20 75 73 69 6e the master usin
8b050 67 20 74 68 65 20 6e 65 77 6c 79 20 66 72 65 65 g the newly free
8b060 64 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 66 28 d chunk */. if(
8b070 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 29 7b mem3.iMaster ){
8b080 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d . while( (mem
8b090 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 3.aPool[mem3.iMa
8b0a0 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ster-1].u.hdr.si
8b0b0 7a 65 34 78 26 32 29 3d 3d 30 20 29 7b 0a 20 20 ze4x&2)==0 ){.
8b0c0 20 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e size = mem3.
8b0d0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 aPool[mem3.iMast
8b0e0 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 er-1].u.hdr.prev
8b0f0 53 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 33 Size;. mem3
8b100 2e 69 4d 61 73 74 65 72 20 2d 3d 20 73 69 7a 65 .iMaster -= size
8b110 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d ;. mem3.szM
8b120 61 73 74 65 72 20 2b 3d 20 73 69 7a 65 3b 0a 20 aster += size;.
8b130 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 memsys3Unli
8b140 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 nk(mem3.iMaster)
8b150 3b 0a 20 20 20 20 20 20 78 20 3d 20 6d 65 6d 33 ;. x = mem3
8b160 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 .aPool[mem3.iMas
8b170 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a ter-1].u.hdr.siz
8b180 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d e4x & 2;. m
8b190 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 em3.aPool[mem3.i
8b1a0 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e Master-1].u.hdr.
8b1b0 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a size4x = mem3.sz
8b1c0 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 Master*4 | x;.
8b1d0 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d mem3.aPool[m
8b1e0 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 em3.iMaster+mem3
8b1f0 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 .szMaster-1].u.h
8b200 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 dr.prevSize = me
8b210 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 m3.szMaster;.
8b220 20 7d 0a 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e }. x = mem3.
8b230 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 aPool[mem3.iMast
8b240 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 er-1].u.hdr.size
8b250 34 78 20 26 20 32 3b 0a 20 20 20 20 77 68 69 6c 4x & 2;. whil
8b260 65 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d e( (mem3.aPool[m
8b270 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 em3.iMaster+mem3
8b280 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 .szMaster-1].u.h
8b290 64 72 2e 73 69 7a 65 34 78 26 31 29 3d 3d 30 20 dr.size4x&1)==0
8b2a0 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 ){. memsys3
8b2b0 55 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 Unlink(mem3.iMas
8b2c0 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 ter+mem3.szMaste
8b2d0 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 r);. mem3.s
8b2e0 7a 4d 61 73 74 65 72 20 2b 3d 20 6d 65 6d 33 2e zMaster += mem3.
8b2f0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 aPool[mem3.iMast
8b300 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 er+mem3.szMaster
8b310 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
8b320 2f 34 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 /4;. mem3.a
8b330 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
8b340 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 r-1].u.hdr.size4
8b350 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 x = mem3.szMaste
8b360 72 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d r*4 | x;. m
8b370 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 em3.aPool[mem3.i
8b380 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 Master+mem3.szMa
8b390 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 ster-1].u.hdr.pr
8b3a0 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a evSize = mem3.sz
8b3b0 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 Master;. }.
8b3c0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 }.}../*.** Retur
8b3d0 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e n the size of an
8b3e0 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c outstanding all
8b3f0 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65 ocation, in byte
8b400 73 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20 s. The.** size
8b410 72 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 20 74 returned omits t
8b420 68 65 20 38 2d 62 79 74 65 20 68 65 61 64 65 72 he 8-byte header
8b430 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68 69 73 overhead. This
8b440 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 only.** works f
8b450 6f 72 20 63 68 75 6e 6b 73 20 74 68 61 74 20 61 or chunks that a
8b460 72 65 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 re currently che
8b470 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61 cked out..*/.sta
8b480 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 53 tic int memsys3S
8b490 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 ize(void *p){.
8b4a0 4d 65 6d 33 42 6c 6f 63 6b 20 2a 70 42 6c 6f 63 Mem3Block *pBloc
8b4b0 6b 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 k;. if( p==0 )
8b4c0 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 42 6c 6f return 0;. pBlo
8b4d0 63 6b 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a ck = (Mem3Block*
8b4e0 29 70 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 )p;. assert( (p
8b4f0 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e Block[-1].u.hdr.
8b500 73 69 7a 65 34 78 26 31 29 21 3d 30 20 29 3b 0a size4x&1)!=0 );.
8b510 20 20 72 65 74 75 72 6e 20 28 70 42 6c 6f 63 6b return (pBlock
8b520 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 [-1].u.hdr.size4
8b530 78 26 7e 33 29 2a 32 20 2d 20 34 3b 0a 7d 0a 0a x&~3)*2 - 4;.}..
8b540 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 /*.** Round up a
8b550 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f request size to
8b560 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 the next valid
8b570 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e allocation size.
8b580 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
8b590 65 6d 73 79 73 33 52 6f 75 6e 64 75 70 28 69 6e emsys3Roundup(in
8b5a0 74 20 6e 29 7b 0a 20 20 69 66 28 20 6e 3c 3d 31 t n){. if( n<=1
8b5b0 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 2 ){. return
8b5c0 31 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 12;. }else{.
8b5d0 20 72 65 74 75 72 6e 20 28 28 6e 2b 31 31 29 26 return ((n+11)&
8b5e0 7e 37 29 20 2d 20 34 3b 0a 20 20 7d 0a 7d 0a 0a ~7) - 4;. }.}..
8b5f0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e /*.** Allocate n
8b600 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e Bytes of memory.
8b610 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
8b620 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 28 69 *memsys3Malloc(i
8b630 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 nt nBytes){. sq
8b640 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a lite3_int64 *p;.
8b650 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 73 assert( nBytes
8b660 3e 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 2f >0 ); /
8b670 2a 20 6d 61 6c 6c 6f 63 2e 63 20 66 69 6c 74 65 * malloc.c filte
8b680 72 73 20 6f 75 74 20 30 20 62 79 74 65 20 72 65 rs out 0 byte re
8b690 71 75 65 73 74 73 20 2a 2f 0a 20 20 6d 65 6d 73 quests */. mems
8b6a0 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 ys3Enter();. p
8b6b0 3d 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 = memsys3MallocU
8b6c0 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 nsafe(nBytes);.
8b6d0 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b memsys3Leave();
8b6e0 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a . return (void*
8b6f0 29 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 )p; .}../*.** Fr
8b700 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 76 6f ee memory..*/.vo
8b710 69 64 20 6d 65 6d 73 79 73 33 46 72 65 65 28 76 id memsys3Free(v
8b720 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 oid *pPrior){.
8b730 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 20 29 assert( pPrior )
8b740 3b 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 ;. memsys3Enter
8b750 28 29 3b 0a 20 20 6d 65 6d 73 79 73 33 46 72 65 ();. memsys3Fre
8b760 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b eUnsafe(pPrior);
8b770 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 . memsys3Leave(
8b780 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e );.}../*.** Chan
8b790 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 ge the size of a
8b7a0 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 n existing memor
8b7b0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a y allocation.*/.
8b7c0 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 52 65 61 void *memsys3Rea
8b7d0 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f lloc(void *pPrio
8b7e0 72 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a r, int nBytes){.
8b7f0 20 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f int nOld;. vo
8b800 69 64 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 id *p;. if( pPr
8b810 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ior==0 ){. re
8b820 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c turn sqlite3_mal
8b830 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d loc(nBytes);. }
8b840 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 . if( nBytes<=0
8b850 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
8b860 66 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 free(pPrior);.
8b870 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
8b880 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 33 nOld = memsys3
8b890 53 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 Size(pPrior);.
8b8a0 69 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 if( nBytes<=nOld
8b8b0 20 26 26 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c 64 && nBytes>=nOld
8b8c0 2d 31 32 38 20 29 7b 0a 20 20 20 20 72 65 74 75 -128 ){. retu
8b8d0 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 rn pPrior;. }.
8b8e0 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b memsys3Enter();
8b8f0 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61 . p = memsys3Ma
8b900 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65 llocUnsafe(nByte
8b910 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 s);. if( p ){.
8b920 20 20 20 69 66 28 20 6e 4f 6c 64 3c 6e 42 79 74 if( nOld<nByt
8b930 65 73 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 es ){. memc
8b940 70 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f py(p, pPrior, nO
8b950 6c 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ld);. }else{.
8b960 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 memcpy(p,
8b970 70 50 72 69 6f 72 2c 20 6e 42 79 74 65 73 29 3b pPrior, nBytes);
8b980 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 79 . }. memsy
8b990 73 33 46 72 65 65 55 6e 73 61 66 65 28 70 50 72 s3FreeUnsafe(pPr
8b9a0 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 ior);. }. mems
8b9b0 79 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 ys3Leave();. re
8b9c0 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn p;.}../*.**
8b9d0 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 Initialize this
8b9e0 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 module..*/.stat
8b9f0 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 49 6e ic int memsys3In
8ba00 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 it(void *NotUsed
8ba10 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
8ba20 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
8ba30 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c if( !sqlite3Gl
8ba40 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 obalConfig.pHeap
8ba50 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
8ba60 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d QLITE_ERROR;. }
8ba70 0a 0a 20 20 2f 2a 20 53 74 6f 72 65 20 61 20 70 .. /* Store a p
8ba80 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65 ointer to the me
8ba90 6d 6f 72 79 20 62 6c 6f 63 6b 20 69 6e 20 67 6c mory block in gl
8baa0 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 6d obal structure m
8bab0 65 6d 33 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 em3. */. assert
8bac0 28 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f ( sizeof(Mem3Blo
8bad0 63 6b 29 3d 3d 38 20 29 3b 0a 20 20 6d 65 6d 33 ck)==8 );. mem3
8bae0 2e 61 50 6f 6f 6c 20 3d 20 28 4d 65 6d 33 42 6c .aPool = (Mem3Bl
8baf0 6f 63 6b 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f ock *)sqlite3Glo
8bb00 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b balConfig.pHeap;
8bb10 0a 20 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 20 3d 20 . mem3.nPool =
8bb20 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f (sqlite3GlobalCo
8bb30 6e 66 69 67 2e 6e 48 65 61 70 20 2f 20 73 69 7a nfig.nHeap / siz
8bb40 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 29 20 eof(Mem3Block))
8bb50 2d 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 - 2;.. /* Initi
8bb60 61 6c 69 7a 65 20 74 68 65 20 6d 61 73 74 65 72 alize the master
8bb70 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6d 65 6d block. */. mem
8bb80 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 6d 65 6d 3.szMaster = mem
8bb90 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 3.nPool;. mem3.
8bba0 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e mnMaster = mem3.
8bbb0 73 7a 4d 61 73 74 65 72 3b 0a 20 20 6d 65 6d 33 szMaster;. mem3
8bbc0 2e 69 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 .iMaster = 1;.
8bbd0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 30 5d 2e 75 2e mem3.aPool[0].u.
8bbe0 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 28 6d 65 hdr.size4x = (me
8bbf0 6d 33 2e 73 7a 4d 61 73 74 65 72 3c 3c 32 29 20 m3.szMaster<<2)
8bc00 2b 20 32 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f + 2;. mem3.aPoo
8bc10 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e l[mem3.nPool].u.
8bc20 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d hdr.prevSize = m
8bc30 65 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d em3.nPool;. mem
8bc40 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 3.aPool[mem3.nPo
8bc50 6f 6c 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 ol].u.hdr.size4x
8bc60 20 3d 20 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20 = 1;.. return
8bc70 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
8bc80 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 .** Deinitialize
8bc90 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f this module..*/
8bca0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
8bcb0 73 79 73 33 53 68 75 74 64 6f 77 6e 28 76 6f 69 sys3Shutdown(voi
8bcc0 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 d *NotUsed){. U
8bcd0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
8bce0 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 33 NotUsed);. mem3
8bcf0 2e 6d 75 74 65 78 20 3d 20 30 3b 0a 20 20 72 65 .mutex = 0;. re
8bd00 74 75 72 6e 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a turn;.}..../*.**
8bd10 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69 Open the file i
8bd20 6e 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 69 ndicated and wri
8bd30 74 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 te a log of all
8bd40 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a unfreed memory .
8bd50 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 ** allocations i
8bd60 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f nto that log..*/
8bd70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
8bd80 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 73 void sqlite3Mems
8bd90 79 73 33 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 ys3Dump(const ch
8bda0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a ar *zFilename){.
8bdb0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
8bdc0 42 55 47 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b BUG. FILE *out;
8bdd0 0a 20 20 75 33 32 20 69 2c 20 6a 3b 0a 20 20 75 . u32 i, j;. u
8bde0 33 32 20 73 69 7a 65 3b 0a 20 20 69 66 28 20 7a 32 size;. if( z
8bdf0 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a Filename==0 || z
8be00 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 Filename[0]==0 )
8be10 7b 0a 20 20 20 20 6f 75 74 20 3d 20 73 74 64 6f {. out = stdo
8be20 75 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ut;. }else{.
8be30 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 out = fopen(zFi
8be40 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 lename, "w");.
8be50 20 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a if( out==0 ){.
8be60 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 fprintf(st
8be70 64 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 derr, "** Unable
8be80 20 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 to output memor
8be90 79 20 64 65 62 75 67 20 6f 75 74 70 75 74 20 6c y debug output l
8bea0 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 og: %s **\n",.
8beb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8bec0 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a zFilename);.
8bed0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
8bee0 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 }. }. memsys
8bef0 33 45 6e 74 65 72 28 29 3b 0a 20 20 66 70 72 69 3Enter();. fpri
8bf00 6e 74 66 28 6f 75 74 2c 20 22 43 48 55 4e 4b 53 ntf(out, "CHUNKS
8bf10 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 31 :\n");. for(i=1
8bf20 3b 20 69 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b ; i<=mem3.nPool;
8bf30 20 69 2b 3d 73 69 7a 65 2f 34 29 7b 0a 20 20 20 i+=size/4){.
8bf40 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f size = mem3.aPo
8bf50 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ol[i-1].u.hdr.si
8bf60 7a 65 34 78 3b 0a 20 20 20 20 69 66 28 20 73 69 ze4x;. if( si
8bf70 7a 65 2f 34 3c 3d 31 20 29 7b 0a 20 20 20 20 20 ze/4<=1 ){.
8bf80 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 fprintf(out, "%
8bf90 70 20 73 69 7a 65 20 65 72 72 6f 72 5c 6e 22 2c p size error\n",
8bfa0 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 &mem3.aPool[i])
8bfb0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
8bfc0 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 0 );. break
8bfd0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
8bfe0 28 73 69 7a 65 26 31 29 3d 3d 30 20 26 26 20 6d (size&1)==0 && m
8bff0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 em3.aPool[i+size
8c000 2f 34 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 /4-1].u.hdr.prev
8c010 53 69 7a 65 21 3d 73 69 7a 65 2f 34 20 29 7b 0a Size!=size/4 ){.
8c020 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 fprintf(ou
8c030 74 2c 20 22 25 70 20 74 61 69 6c 20 73 69 7a 65 t, "%p tail size
8c040 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 5c does not match\
8c050 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b n", &mem3.aPool[
8c060 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 i]);. asser
8c070 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72 t( 0 );. br
8c080 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 eak;. }. i
8c090 66 28 20 28 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b f( ((mem3.aPool[
8c0a0 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 64 i+size/4-1].u.hd
8c0b0 72 2e 73 69 7a 65 34 78 26 32 29 3e 3e 31 29 21 r.size4x&2)>>1)!
8c0c0 3d 28 73 69 7a 65 26 31 29 20 29 7b 0a 20 20 20 =(size&1) ){.
8c0d0 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
8c0e0 22 25 70 20 74 61 69 6c 20 63 68 65 63 6b 6f 75 "%p tail checkou
8c0f0 74 20 62 69 74 20 69 73 20 69 6e 63 6f 72 72 65 t bit is incorre
8c100 63 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f ct\n", &mem3.aPo
8c110 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 ol[i]);. as
8c120 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20 20 sert( 0 );.
8c130 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
8c140 20 20 69 66 28 20 73 69 7a 65 26 31 20 29 7b 0a if( size&1 ){.
8c150 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 fprintf(ou
8c160 74 2c 20 22 25 70 20 25 36 64 20 62 79 74 65 73 t, "%p %6d bytes
8c170 20 63 68 65 63 6b 65 64 20 6f 75 74 5c 6e 22 2c checked out\n",
8c180 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c &mem3.aPool[i],
8c190 20 28 73 69 7a 65 2f 34 29 2a 38 2d 38 29 3b 0a (size/4)*8-8);.
8c1a0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
8c1b0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 fprintf(out, "%
8c1c0 70 20 25 36 64 20 62 79 74 65 73 20 66 72 65 65 p %6d bytes free
8c1d0 25 73 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f %s\n", &mem3.aPo
8c1e0 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29 2a ol[i], (size/4)*
8c1f0 38 2d 38 2c 0a 20 20 20 20 20 20 20 20 20 20 20 8-8,.
8c200 20 20 20 20 20 20 20 69 3d 3d 6d 65 6d 33 2e 69 i==mem3.i
8c210 4d 61 73 74 65 72 20 3f 20 22 20 2a 2a 6d 61 73 Master ? " **mas
8c220 74 65 72 2a 2a 22 20 3a 20 22 22 29 3b 0a 20 20 ter**" : "");.
8c230 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d }. }. for(i=
8c240 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 0; i<MX_SMALL-1;
8c250 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d i++){. if( m
8c260 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3d 3d em3.aiSmall[i]==
8c270 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 0 ) continue;.
8c280 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
8c290 73 6d 61 6c 6c 28 25 32 64 29 3a 22 2c 20 69 29 small(%2d):", i)
8c2a0 3b 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d 65 ;. for(j = me
8c2b0 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3b 20 6a m3.aiSmall[i]; j
8c2c0 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c >0; j=mem3.aPool
8c2d0 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 [j].u.list.next)
8c2e0 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 {. fprintf(
8c2f0 6f 75 74 2c 20 22 20 25 70 28 25 64 29 22 2c 20 out, " %p(%d)",
8c300 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a &mem3.aPool[j],.
8c310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6d (m
8c320 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 em3.aPool[j-1].u
8c330 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38 .hdr.size4x/4)*8
8c340 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 -8);. }. f
8c350 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 printf(out, "\n"
8c360 29 3b 20 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d ); . }. for(i=
8c370 30 3b 20 69 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 0; i<N_HASH; i++
8c380 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e ){. if( mem3.
8c390 61 69 48 61 73 68 5b 69 5d 3d 3d 30 20 29 20 63 aiHash[i]==0 ) c
8c3a0 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 70 72 ontinue;. fpr
8c3b0 69 6e 74 66 28 6f 75 74 2c 20 22 68 61 73 68 28 intf(out, "hash(
8c3c0 25 32 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 %2d):", i);.
8c3d0 66 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 48 for(j = mem3.aiH
8c3e0 61 73 68 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d ash[i]; j>0; j=m
8c3f0 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c em3.aPool[j].u.l
8c400 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 ist.next){.
8c410 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 fprintf(out, "
8c420 25 70 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61 %p(%d)", &mem3.a
8c430 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 Pool[j],.
8c440 20 20 20 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f (mem3.aPo
8c450 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ol[j-1].u.hdr.si
8c460 7a 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 ze4x/4)*8-8);.
8c470 20 20 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 }. fprintf(
8c480 6f 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d out, "\n"); . }
8c490 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 . fprintf(out,
8c4a0 22 6d 61 73 74 65 72 3d 25 64 5c 6e 22 2c 20 6d "master=%d\n", m
8c4b0 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 em3.iMaster);.
8c4c0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6e 6f fprintf(out, "no
8c4d0 77 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d wUsed=%d\n", mem
8c4e0 33 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 3.nPool*8 - mem3
8c4f0 2e 73 7a 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 .szMaster*8);.
8c500 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 78 fprintf(out, "mx
8c510 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 Used=%d\n", mem3
8c520 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e .nPool*8 - mem3.
8c530 6d 6e 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 73 mnMaster*8);. s
8c540 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
8c550 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a ve(mem3.mutex);.
8c560 20 20 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 if( out==stdou
8c570 74 20 29 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 t ){. fflush(
8c580 73 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 stdout);. }else
8c590 7b 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 {. fclose(out
8c5a0 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 );. }.#else. U
8c5b0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
8c5c0 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 zFilename);.#end
8c5d0 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 if.}../*.** This
8c5e0 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 routine is the
8c5f0 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 only routine in
8c600 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 this file with e
8c610 78 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b xternal .** link
8c620 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c age..**.** Popul
8c630 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 ate the low-leve
8c640 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 l memory allocat
8c650 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 ion function poi
8c660 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 nters in.** sqli
8c670 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
8c680 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 m with pointers
8c690 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 to the routines
8c6a0 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 54 68 in this file. Th
8c6b0 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 73 e.** arguments s
8c6c0 70 65 63 69 66 79 20 74 68 65 20 62 6c 6f 63 6b pecify the block
8c6d0 20 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 6d 61 of memory to ma
8c6e0 6e 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 nage..**.** This
8c6f0 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 routine is only
8c700 20 63 61 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 called by sqlit
8c710 65 33 5f 63 6f 6e 66 69 67 28 29 2c 20 61 6e 64 e3_config(), and
8c720 20 74 68 65 72 65 66 6f 72 65 0a 2a 2a 20 69 73 therefore.** is
8c730 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f not required to
8c740 20 62 65 20 74 68 72 65 61 64 73 61 66 65 20 28 be threadsafe (
8c750 69 74 20 69 73 20 6e 6f 74 29 2e 0a 2a 2f 0a 53 it is not)..*/.S
8c760 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
8c770 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f nst sqlite3_mem_
8c780 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 methods *sqlite3
8c790 4d 65 6d 47 65 74 4d 65 6d 73 79 73 33 28 76 6f MemGetMemsys3(vo
8c7a0 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f id){. static co
8c7b0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f nst sqlite3_mem_
8c7c0 6d 65 74 68 6f 64 73 20 6d 65 6d 70 6f 6f 6c 4d methods mempoolM
8c7d0 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 ethods = {.
8c7e0 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 2c 0a 20 memsys3Malloc,.
8c7f0 20 20 20 20 6d 65 6d 73 79 73 33 46 72 65 65 2c memsys3Free,
8c800 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 52 65 61 . memsys3Rea
8c810 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 lloc,. memsy
8c820 73 33 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d s3Size,. mem
8c830 73 79 73 33 52 6f 75 6e 64 75 70 2c 0a 20 20 20 sys3Roundup,.
8c840 20 20 6d 65 6d 73 79 73 33 49 6e 69 74 2c 0a 20 memsys3Init,.
8c850 20 20 20 20 6d 65 6d 73 79 73 33 53 68 75 74 64 memsys3Shutd
8c860 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b own,. 0. };
8c870 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 70 6f . return &mempo
8c880 6f 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 olMethods;.}..#e
8c890 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 ndif /* SQLITE_E
8c8a0 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 2a 2f NABLE_MEMSYS3 */
8c8b0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
8c8c0 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 33 2e 63 20 * End of mem3.c
8c8d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c8f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c900 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
8c910 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d * Begin file mem
8c920 35 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 5.c ************
8c930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c950 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4f 63 74 /./*.** 2007 Oct
8c960 6f 62 65 72 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 ober 14.**.** Th
8c970 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
8c980 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
8c990 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
8c9a0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
8c9b0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
8c9c0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
8c9d0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
8c9e0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
8c9f0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
8ca00 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
8ca10 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
8ca20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
8ca30 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
8ca40 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
8ca50 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
8ca60 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
8ca70 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
8ca80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ca90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8caa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cac0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
8cad0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ile contains the
8cae0 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 C functions tha
8caf0 74 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 6d 65 t implement a me
8cb00 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 mory.** allocati
8cb10 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 66 6f 72 on subsystem for
8cb20 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 2e 20 use by SQLite.
8cb30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 .**.** This vers
8cb40 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ion of the memor
8cb50 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 y allocation sub
8cb60 73 79 73 74 65 6d 20 6f 6d 69 74 73 20 61 6c 6c system omits all
8cb70 0a 2a 2a 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f .** use of mallo
8cb80 63 28 29 2e 20 54 68 65 20 61 70 70 6c 69 63 61 c(). The applica
8cb90 74 69 6f 6e 20 67 69 76 65 73 20 53 51 4c 69 74 tion gives SQLit
8cba0 65 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d e a block of mem
8cbb0 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 ory.** before ca
8cbc0 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e lling sqlite3_in
8cbd0 69 74 69 61 6c 69 7a 65 28 29 20 66 72 6f 6d 20 itialize() from
8cbe0 77 68 69 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e which allocation
8cbf0 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 61 6e s.** are made an
8cc00 64 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 d returned by th
8cc10 65 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 e xMalloc() and
8cc20 78 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 69 xRealloc() .** i
8cc30 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 20 mplementations.
8cc40 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 Once sqlite3_ini
8cc50 74 69 61 6c 69 7a 65 28 29 20 68 61 73 20 62 65 tialize() has be
8cc60 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 68 en called,.** th
8cc70 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f e amount of memo
8cc80 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 ry available to
8cc90 53 51 4c 69 74 65 20 69 73 20 66 69 78 65 64 20 SQLite is fixed
8cca0 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 and cannot.** be
8ccb0 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 changed..**.**
8ccc0 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 This version of
8ccd0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
8cce0 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 ation subsystem
8ccf0 69 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 69 is included.** i
8cd00 6e 20 74 68 65 20 62 75 69 6c 64 20 6f 6e 6c 79 n the build only
8cd10 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c if SQLITE_ENABL
8cd20 45 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65 66 E_MEMSYS5 is def
8cd30 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ined..**.** This
8cd40 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
8cd50 72 20 75 73 65 73 20 74 68 65 20 66 6f 6c 6c 6f r uses the follo
8cd60 77 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 3a 0a wing algorithm:.
8cd70 2a 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 6c 6c 20 **.** 1. All
8cd80 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
8cd90 6e 73 20 73 69 7a 65 73 20 61 72 65 20 72 6f 75 ns sizes are rou
8cda0 6e 64 65 64 20 75 70 20 74 6f 20 61 20 70 6f 77 nded up to a pow
8cdb0 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 20 er of 2..**.**
8cdc0 20 32 2e 20 20 49 66 20 74 77 6f 20 61 64 6a 61 2. If two adja
8cdd0 63 65 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b 73 cent free blocks
8cde0 20 61 72 65 20 74 68 65 20 68 61 6c 76 65 73 20 are the halves
8cdf0 6f 66 20 61 20 6c 61 72 67 65 72 20 62 6c 6f 63 of a larger bloc
8ce00 6b 2c 0a 2a 2a 20 20 20 20 20 20 20 74 68 65 6e k,.** then
8ce10 20 74 68 65 20 74 77 6f 20 62 6c 6f 63 6b 73 20 the two blocks
8ce20 61 72 65 20 63 6f 61 6c 65 73 65 64 20 69 6e 74 are coalesed int
8ce30 6f 20 74 68 65 20 73 69 6e 67 6c 65 20 6c 61 72 o the single lar
8ce40 67 65 72 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a ger block..**.**
8ce50 20 20 20 33 2e 20 20 4e 65 77 20 6d 65 6d 6f 72 3. New memor
8ce60 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 y is allocated f
8ce70 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 61 76 rom the first av
8ce80 61 69 6c 61 62 6c 65 20 66 72 65 65 20 62 6c 6f ailable free blo
8ce90 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 ck..**.** This a
8cea0 6c 67 6f 72 69 74 68 6d 20 69 73 20 64 65 73 63 lgorithm is desc
8ceb0 72 69 62 65 64 20 69 6e 3a 20 4a 2e 20 4d 2e 20 ribed in: J. M.
8cec0 52 6f 62 73 6f 6e 2e 20 22 42 6f 75 6e 64 73 20 Robson. "Bounds
8ced0 66 6f 72 20 53 6f 6d 65 20 46 75 6e 63 74 69 6f for Some Functio
8cee0 6e 73 0a 2a 2a 20 43 6f 6e 63 65 72 6e 69 6e 67 ns.** Concerning
8cef0 20 44 79 6e 61 6d 69 63 20 53 74 6f 72 61 67 65 Dynamic Storage
8cf00 20 41 6c 6c 6f 63 61 74 69 6f 6e 22 2e 20 4a 6f Allocation". Jo
8cf10 75 72 6e 61 6c 20 6f 66 20 74 68 65 20 41 73 73 urnal of the Ass
8cf20 6f 63 69 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 ociation for.**
8cf30 43 6f 6d 70 75 74 69 6e 67 20 4d 61 63 68 69 6e Computing Machin
8cf40 65 72 79 2c 20 56 6f 6c 75 6d 65 20 32 31 2c 20 ery, Volume 21,
8cf50 4e 75 6d 62 65 72 20 38 2c 20 4a 75 6c 79 20 31 Number 8, July 1
8cf60 39 37 34 2c 20 70 61 67 65 73 20 34 39 31 2d 34 974, pages 491-4
8cf70 39 39 2e 0a 2a 2a 20 0a 2a 2a 20 4c 65 74 20 6e 99..** .** Let n
8cf80 20 62 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 be the size of
8cf90 74 68 65 20 6c 61 72 67 65 73 74 20 61 6c 6c 6f the largest allo
8cfa0 63 61 74 69 6f 6e 20 64 69 76 69 64 65 64 20 62 cation divided b
8cfb0 79 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 2a 2a y the minimum.**
8cfc0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 allocation size
8cfd0 20 28 61 66 74 65 72 20 72 6f 75 6e 64 69 6e 67 (after rounding
8cfe0 20 61 6c 6c 20 73 69 7a 65 73 20 75 70 20 74 6f all sizes up to
8cff0 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 29 20 a power of 2.)
8d000 20 4c 65 74 20 4d 0a 2a 2a 20 62 65 20 74 68 65 Let M.** be the
8d010 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 maximum amount
8d020 6f 66 20 6d 65 6d 6f 72 79 20 65 76 65 72 20 6f of memory ever o
8d030 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e utstanding at on
8d040 65 20 74 69 6d 65 2e 20 20 4c 65 74 0a 2a 2a 20 e time. Let.**
8d050 4e 20 62 65 20 74 68 65 20 74 6f 74 61 6c 20 61 N be the total a
8d060 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 mount of memory
8d070 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c available for al
8d080 6c 6f 63 61 74 69 6f 6e 2e 20 20 52 6f 62 73 6f location. Robso
8d090 6e 0a 2a 2a 20 70 72 6f 76 65 64 20 74 68 61 74 n.** proved that
8d0a0 20 74 68 69 73 20 6d 65 6d 6f 72 79 20 61 6c 6c this memory all
8d0b0 6f 63 61 74 6f 72 20 77 69 6c 6c 20 6e 65 76 65 ocator will neve
8d0c0 72 20 62 72 65 61 6b 64 6f 77 6e 20 64 75 65 20 r breakdown due
8d0d0 74 6f 20 0a 2a 2a 20 66 72 61 67 6d 65 6e 74 61 to .** fragmenta
8d0e0 74 69 6f 6e 20 61 73 20 6c 6f 6e 67 20 61 73 20 tion as long as
8d0f0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f the following co
8d100 6e 73 74 72 61 69 6e 74 20 68 6f 6c 64 73 3a 0a nstraint holds:.
8d110 2a 2a 0a 2a 2a 20 20 20 20 20 20 4e 20 3e 3d 20 **.** N >=
8d120 20 4d 2a 28 31 20 2b 20 6c 6f 67 32 28 6e 29 2f M*(1 + log2(n)/
8d130 32 29 20 2d 20 6e 20 2b 20 31 0a 2a 2a 0a 2a 2a 2) - n + 1.**.**
8d140 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 74 61 The sqlite3_sta
8d150 74 75 73 28 29 20 6c 6f 67 69 63 20 74 72 61 63 tus() logic trac
8d160 6b 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 ks the maximum v
8d170 61 6c 75 65 73 20 6f 66 20 6e 20 61 6e 64 20 4d alues of n and M
8d180 20 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6e 20 61 so.** that an a
8d190 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 2c 20 pplication can,
8d1a0 61 74 20 61 6e 79 20 74 69 6d 65 2c 20 76 65 72 at any time, ver
8d1b0 69 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61 ify this constra
8d1c0 69 6e 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 int..*/../*.** T
8d1d0 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 his version of t
8d1e0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
8d1f0 74 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 tor is used only
8d200 20 77 68 65 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 when .** SQLITE
8d210 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 _ENABLE_MEMSYS5
8d220 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 is defined..*/.#
8d230 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
8d240 42 4c 45 5f 4d 45 4d 53 59 53 35 0a 0a 2f 2a 0a BLE_MEMSYS5../*.
8d250 2a 2a 20 41 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c ** A minimum all
8d260 6f 63 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e ocation is an in
8d270 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
8d280 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
8d290 65 2e 0a 2a 2a 20 4c 61 72 67 65 72 20 61 6c 6c e..** Larger all
8d2a0 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 61 6e 20 ocations are an
8d2b0 61 72 72 61 79 20 6f 66 20 74 68 65 73 65 20 73 array of these s
8d2c0 74 72 75 63 74 75 72 65 73 20 77 68 65 72 65 20 tructures where
8d2d0 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 the.** size of t
8d2e0 68 65 20 61 72 72 61 79 20 69 73 20 61 20 70 6f he array is a po
8d2f0 77 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 wer of 2..**.**
8d300 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73 The size of this
8d310 20 6f 62 6a 65 63 74 20 6d 75 73 74 20 62 65 20 object must be
8d320 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 a power of two.
8d330 20 54 68 61 74 20 66 61 63 74 20 69 73 0a 2a 2a That fact is.**
8d340 20 76 65 72 69 66 69 65 64 20 69 6e 20 6d 65 6d verified in mem
8d350 73 79 73 35 49 6e 69 74 28 29 2e 0a 2a 2f 0a 74 sys5Init()..*/.t
8d360 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 ypedef struct Me
8d370 6d 35 4c 69 6e 6b 20 4d 65 6d 35 4c 69 6e 6b 3b m5Link Mem5Link;
8d380 0a 73 74 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b .struct Mem5Link
8d390 20 7b 0a 20 20 69 6e 74 20 6e 65 78 74 3b 20 20 {. int next;
8d3a0 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
8d3b0 20 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b next free chunk
8d3c0 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65 76 3b 20 */. int prev;
8d3d0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
8d3e0 66 20 70 72 65 76 69 6f 75 73 20 66 72 65 65 20 f previous free
8d3f0 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a chunk */.};../*.
8d400 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 ** Maximum size
8d410 6f 66 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69 6f of any allocatio
8d420 6e 20 69 73 20 28 28 31 3c 3c 4c 4f 47 4d 41 58 n is ((1<<LOGMAX
8d430 29 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 2e 20 )*mem5.szAtom).
8d440 53 69 6e 63 65 0a 2a 2a 20 6d 65 6d 35 2e 73 7a Since.** mem5.sz
8d450 41 74 6f 6d 20 69 73 20 61 6c 77 61 79 73 20 61 Atom is always a
8d460 74 20 6c 65 61 73 74 20 38 20 61 6e 64 20 33 32 t least 8 and 32
8d470 2d 62 69 74 20 69 6e 74 65 67 65 72 73 20 61 72 -bit integers ar
8d480 65 20 75 73 65 64 2c 0a 2a 2a 20 69 74 20 69 73 e used,.** it is
8d490 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 70 6f not actually po
8d4a0 73 73 69 62 6c 65 20 74 6f 20 72 65 61 63 68 20 ssible to reach
8d4b0 74 68 69 73 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 this limit..*/.#
8d4c0 64 65 66 69 6e 65 20 4c 4f 47 4d 41 58 20 33 30 define LOGMAX 30
8d4d0 0a 0a 2f 2a 0a 2a 2a 20 4d 61 73 6b 73 20 75 73 ../*.** Masks us
8d4e0 65 64 20 66 6f 72 20 6d 65 6d 35 2e 61 43 74 72 ed for mem5.aCtr
8d4f0 6c 5b 5d 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f l[] elements..*/
8d500 0a 23 64 65 66 69 6e 65 20 43 54 52 4c 5f 4c 4f .#define CTRL_LO
8d510 47 53 49 5a 45 20 20 30 78 31 66 20 20 20 20 2f GSIZE 0x1f /
8d520 2a 20 4c 6f 67 32 20 53 69 7a 65 20 6f 66 20 74 * Log2 Size of t
8d530 68 69 73 20 62 6c 6f 63 6b 20 2a 2f 0a 23 64 65 his block */.#de
8d540 66 69 6e 65 20 43 54 52 4c 5f 46 52 45 45 20 20 fine CTRL_FREE
8d550 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 54 72 0x20 /* Tr
8d560 75 65 20 69 66 20 6e 6f 74 20 63 68 65 63 6b 65 ue if not checke
8d570 64 20 6f 75 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 d out */../*.**
8d580 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 All of the stati
8d590 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 c variables used
8d5a0 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 by this module
8d5b0 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a are collected.**
8d5c0 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 into a single s
8d5d0 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 tructure named "
8d5e0 6d 65 6d 35 22 2e 20 20 54 68 69 73 20 69 73 20 mem5". This is
8d5f0 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 to keep the.** s
8d600 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 tatic variables
8d610 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f organized and to
8d620 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 reduce namespac
8d630 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 e pollution.** w
8d640 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 hen this module
8d650 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 is combined with
8d660 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d other in the am
8d670 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 algamation..*/.s
8d680 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 tatic SQLITE_WSD
8d690 20 73 74 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62 struct Mem5Glob
8d6a0 61 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d al {. /*. ** M
8d6b0 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 emory available
8d6c0 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 20 for allocation.
8d6d0 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 41 74 6f 6d */. int szAtom
8d6e0 3b 20 20 20 20 20 20 2f 2a 20 53 6d 61 6c 6c 65 ; /* Smalle
8d6f0 73 74 20 70 6f 73 73 69 62 6c 65 20 61 6c 6c 6f st possible allo
8d700 63 61 74 69 6f 6e 20 69 6e 20 62 79 74 65 73 20 cation in bytes
8d710 2a 2f 0a 20 20 69 6e 74 20 6e 42 6c 6f 63 6b 3b */. int nBlock;
8d720 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
8d730 6f 66 20 73 7a 41 74 6f 6d 20 73 69 7a 65 64 20 of szAtom sized
8d740 62 6c 6f 63 6b 73 20 69 6e 20 7a 50 6f 6f 6c 20 blocks in zPool
8d750 2a 2f 0a 20 20 75 38 20 2a 7a 50 6f 6f 6c 3b 20 */. u8 *zPool;
8d760 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 /* Memory
8d770 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 62 65 20 available to be
8d780 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 0a allocated */. .
8d790 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 /*. ** Mutex
8d7a0 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 to control acces
8d7b0 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 s to the memory
8d7c0 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
8d7d0 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c stem.. */. sql
8d7e0 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 ite3_mutex *mute
8d7f0 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 65 x;.. /*. ** Pe
8d800 72 66 6f 72 6d 61 6e 63 65 20 73 74 61 74 69 73 rformance statis
8d810 74 69 63 73 0a 20 20 2a 2f 0a 20 20 75 36 34 20 tics. */. u64
8d820 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 nAlloc;
8d830 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 /* Total number
8d840 6f 66 20 63 61 6c 6c 73 20 74 6f 20 6d 61 6c 6c of calls to mall
8d850 6f 63 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61 oc */. u64 tota
8d860 6c 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a 20 54 lAlloc; /* T
8d870 6f 74 61 6c 20 6f 66 20 61 6c 6c 20 6d 61 6c 6c otal of all mall
8d880 6f 63 20 63 61 6c 6c 73 20 2d 20 69 6e 63 6c 75 oc calls - inclu
8d890 64 65 73 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 des internal fra
8d8a0 67 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61 6c g */. u64 total
8d8b0 45 78 63 65 73 73 3b 20 20 20 20 2f 2a 20 54 6f Excess; /* To
8d8c0 74 61 6c 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 tal internal fra
8d8d0 67 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 gmentation */.
8d8e0 75 33 32 20 63 75 72 72 65 6e 74 4f 75 74 3b 20 u32 currentOut;
8d8f0 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 63 /* Current c
8d900 68 65 63 6b 6f 75 74 2c 20 69 6e 63 6c 75 64 69 heckout, includi
8d910 6e 67 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67 ng internal frag
8d920 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 mentation */. u
8d930 33 32 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 3b 32 currentCount;
8d940 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 /* Current nu
8d950 6d 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63 74 mber of distinct
8d960 20 63 68 65 63 6b 6f 75 74 73 20 2a 2f 0a 20 20 checkouts */.
8d970 75 33 32 20 6d 61 78 4f 75 74 3b 20 20 20 20 20 u32 maxOut;
8d980 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 /* Maximum i
8d990 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 63 75 72 nstantaneous cur
8d9a0 72 65 6e 74 4f 75 74 20 2a 2f 0a 20 20 75 33 32 rentOut */. u32
8d9b0 20 6d 61 78 43 6f 75 6e 74 3b 20 20 20 20 20 20 maxCount;
8d9c0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 6e 73 74 /* Maximum inst
8d9d0 61 6e 74 61 6e 65 6f 75 73 20 63 75 72 72 65 6e antaneous curren
8d9e0 74 43 6f 75 6e 74 20 2a 2f 0a 20 20 75 33 32 20 tCount */. u32
8d9f0 6d 61 78 52 65 71 75 65 73 74 3b 20 20 20 20 20 maxRequest;
8da00 2f 2a 20 4c 61 72 67 65 73 74 20 61 6c 6c 6f 63 /* Largest alloc
8da10 61 74 69 6f 6e 20 28 65 78 63 6c 75 73 69 76 65 ation (exclusive
8da20 20 6f 66 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 of internal fra
8da30 67 29 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 g) */. . /*.
8da40 2a 2a 20 4c 69 73 74 73 20 6f 66 20 66 72 65 65 ** Lists of free
8da50 20 62 6c 6f 63 6b 73 2e 20 20 61 69 46 72 65 65 blocks. aiFree
8da60 6c 69 73 74 5b 30 5d 20 69 73 20 61 20 6c 69 73 list[0] is a lis
8da70 74 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 t of free blocks
8da80 20 6f 66 0a 20 20 2a 2a 20 73 69 7a 65 20 6d 65 of. ** size me
8da90 6d 35 2e 73 7a 41 74 6f 6d 2e 20 20 61 69 46 72 m5.szAtom. aiFr
8daa0 65 65 6c 69 73 74 5b 31 5d 20 68 6f 6c 64 73 20 eelist[1] holds
8dab0 62 6c 6f 63 6b 73 20 6f 66 20 73 69 7a 65 20 73 blocks of size s
8dac0 7a 41 74 6f 6d 2a 32 2e 0a 20 20 2a 2a 20 61 6e zAtom*2.. ** an
8dad0 64 20 73 6f 20 66 6f 72 74 68 2e 0a 20 20 2a 2f d so forth.. */
8dae0 0a 20 20 69 6e 74 20 61 69 46 72 65 65 6c 69 73 . int aiFreelis
8daf0 74 5b 4c 4f 47 4d 41 58 2b 31 5d 3b 0a 0a 20 20 t[LOGMAX+1];..
8db00 2f 2a 0a 20 20 2a 2a 20 53 70 61 63 65 20 66 6f /*. ** Space fo
8db10 72 20 74 72 61 63 6b 69 6e 67 20 77 68 69 63 68 r tracking which
8db20 20 62 6c 6f 63 6b 73 20 61 72 65 20 63 68 65 63 blocks are chec
8db30 6b 65 64 20 6f 75 74 20 61 6e 64 20 74 68 65 20 ked out and the
8db40 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 65 61 63 size. ** of eac
8db50 68 20 62 6c 6f 63 6b 2e 20 20 4f 6e 65 20 62 79 h block. One by
8db60 74 65 20 70 65 72 20 62 6c 6f 63 6b 2e 0a 20 20 te per block..
8db70 2a 2f 0a 20 20 75 38 20 2a 61 43 74 72 6c 3b 0a */. u8 *aCtrl;.
8db80 0a 7d 20 6d 65 6d 35 20 3d 20 7b 20 30 20 7d 3b .} mem5 = { 0 };
8db90 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74 ../*.** Access t
8dba0 68 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 he static variab
8dbb0 6c 65 20 74 68 72 6f 75 67 68 20 61 20 6d 61 63 le through a mac
8dbc0 72 6f 20 66 6f 72 20 53 51 4c 49 54 45 5f 4f 4d ro for SQLITE_OM
8dbd0 49 54 5f 57 53 44 0a 2a 2f 0a 23 64 65 66 69 6e IT_WSD.*/.#defin
8dbe0 65 20 6d 65 6d 35 20 47 4c 4f 42 41 4c 28 73 74 e mem5 GLOBAL(st
8dbf0 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61 6c 2c ruct Mem5Global,
8dc00 20 6d 65 6d 35 29 0a 0a 2f 2a 0a 2a 2a 20 41 73 mem5)../*.** As
8dc10 73 75 6d 69 6e 67 20 6d 65 6d 35 2e 7a 50 6f 6f suming mem5.zPoo
8dc20 6c 20 69 73 20 64 69 76 69 64 65 64 20 75 70 20 l is divided up
8dc30 69 6e 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 into an array of
8dc40 20 4d 65 6d 35 4c 69 6e 6b 0a 2a 2a 20 73 74 72 Mem5Link.** str
8dc50 75 63 74 75 72 65 73 2c 20 72 65 74 75 72 6e 20 uctures, return
8dc60 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
8dc70 20 69 64 78 2d 74 68 20 73 75 63 68 20 6c 69 6b idx-th such lik
8dc80 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d ..*/.#define MEM
8dc90 35 4c 49 4e 4b 28 69 64 78 29 20 28 28 4d 65 6d 5LINK(idx) ((Mem
8dca0 35 4c 69 6e 6b 20 2a 29 28 26 6d 65 6d 35 2e 7a 5Link *)(&mem5.z
8dcb0 50 6f 6f 6c 5b 28 69 64 78 29 2a 6d 65 6d 35 2e Pool[(idx)*mem5.
8dcc0 73 7a 41 74 6f 6d 5d 29 29 0a 0a 2f 2a 0a 2a 2a szAtom]))../*.**
8dcd0 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e Unlink the chun
8dce0 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b k at mem5.aPool[
8dcf0 69 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20 i] from list it
8dd00 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 is currently.**
8dd10 6f 6e 2e 20 20 49 74 20 73 68 6f 75 6c 64 20 62 on. It should b
8dd20 65 20 66 6f 75 6e 64 20 6f 6e 20 6d 65 6d 35 2e e found on mem5.
8dd30 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 aiFreelist[iLogs
8dd40 69 7a 65 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ize]..*/.static
8dd50 76 6f 69 64 20 6d 65 6d 73 79 73 35 55 6e 6c 69 void memsys5Unli
8dd60 6e 6b 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c nk(int i, int iL
8dd70 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 6e ogsize){. int n
8dd80 65 78 74 2c 20 70 72 65 76 3b 0a 20 20 61 73 73 ext, prev;. ass
8dd90 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d ert( i>=0 && i<m
8dda0 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 em5.nBlock );.
8ddb0 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 assert( iLogsize
8ddc0 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c >=0 && iLogsize<
8ddd0 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 73 =LOGMAX );. ass
8dde0 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c ert( (mem5.aCtrl
8ddf0 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49 [i] & CTRL_LOGSI
8de00 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b ZE)==iLogsize );
8de10 0a 0a 20 20 6e 65 78 74 20 3d 20 4d 45 4d 35 4c .. next = MEM5L
8de20 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 INK(i)->next;.
8de30 70 72 65 76 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 prev = MEM5LINK(
8de40 69 29 2d 3e 70 72 65 76 3b 0a 20 20 69 66 28 20 i)->prev;. if(
8de50 70 72 65 76 3c 30 20 29 7b 0a 20 20 20 20 6d 65 prev<0 ){. me
8de60 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c m5.aiFreelist[iL
8de70 6f 67 73 69 7a 65 5d 20 3d 20 6e 65 78 74 3b 0a ogsize] = next;.
8de80 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 45 4d }else{. MEM
8de90 35 4c 49 4e 4b 28 70 72 65 76 29 2d 3e 6e 65 78 5LINK(prev)->nex
8dea0 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 t = next;. }.
8deb0 69 66 28 20 6e 65 78 74 3e 3d 30 20 29 7b 0a 20 if( next>=0 ){.
8dec0 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 6e 65 78 74 MEM5LINK(next
8ded0 29 2d 3e 70 72 65 76 20 3d 20 70 72 65 76 3b 0a )->prev = prev;.
8dee0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e }.}../*.** Lin
8def0 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 6d k the chunk at m
8df00 65 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f 20 em5.aPool[i] so
8df10 74 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20 69 that is on the i
8df20 4c 6f 67 73 69 7a 65 0a 2a 2a 20 66 72 65 65 20 Logsize.** free
8df30 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 list..*/.static
8df40 76 6f 69 64 20 6d 65 6d 73 79 73 35 4c 69 6e 6b void memsys5Link
8df50 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f 67 (int i, int iLog
8df60 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 78 3b 0a size){. int x;.
8df70 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
8df80 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 3_mutex_held(mem
8df90 35 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 5.mutex) );. as
8dfa0 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
8dfb0 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 mem5.nBlock );.
8dfc0 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a assert( iLogsiz
8dfd0 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 e>=0 && iLogsize
8dfe0 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 <=LOGMAX );. as
8dff0 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 sert( (mem5.aCtr
8e000 6c 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 l[i] & CTRL_LOGS
8e010 49 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 IZE)==iLogsize )
8e020 3b 0a 0a 20 20 78 20 3d 20 4d 45 4d 35 4c 49 4e ;.. x = MEM5LIN
8e030 4b 28 69 29 2d 3e 6e 65 78 74 20 3d 20 6d 65 6d K(i)->next = mem
8e040 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 5.aiFreelist[iLo
8e050 67 73 69 7a 65 5d 3b 0a 20 20 4d 45 4d 35 4c 49 gsize];. MEM5LI
8e060 4e 4b 28 69 29 2d 3e 70 72 65 76 20 3d 20 2d 31 NK(i)->prev = -1
8e070 3b 0a 20 20 69 66 28 20 78 3e 3d 30 20 29 7b 0a ;. if( x>=0 ){.
8e080 20 20 20 20 61 73 73 65 72 74 28 20 78 3c 6d 65 assert( x<me
8e090 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 20 m5.nBlock );.
8e0a0 20 4d 45 4d 35 4c 49 4e 4b 28 78 29 2d 3e 70 72 MEM5LINK(x)->pr
8e0b0 65 76 20 3d 20 69 3b 0a 20 20 7d 0a 20 20 6d 65 ev = i;. }. me
8e0c0 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c m5.aiFreelist[iL
8e0d0 6f 67 73 69 7a 65 5d 20 3d 20 69 3b 0a 7d 0a 0a ogsize] = i;.}..
8e0e0 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 /*.** If the STA
8e0f0 54 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 TIC_MEM mutex is
8e100 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c not already hel
8e110 64 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 d, obtain it now
8e120 2e 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 . The mutex.** w
8e130 69 6c 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 ill already be h
8e140 65 6c 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 eld (obtained by
8e150 20 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e code in malloc.
8e160 63 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 c) if.** sqlite3
8e170 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
8e180 6d 53 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a mStat is true..*
8e190 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 /.static void me
8e1a0 6d 73 79 73 35 45 6e 74 65 72 28 76 6f 69 64 29 msys5Enter(void)
8e1b0 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 {. sqlite3_mute
8e1c0 78 5f 65 6e 74 65 72 28 6d 65 6d 35 2e 6d 75 74 x_enter(mem5.mut
8e1d0 65 78 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f ex);.}.static vo
8e1e0 69 64 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 id memsys5Leave(
8e1f0 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 void){. sqlite3
8e200 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d _mutex_leave(mem
8e210 35 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 5.mutex);.}../*.
8e220 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 ** Return the si
8e230 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e ze of an outstan
8e240 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c ding allocation,
8e250 20 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65 0a in bytes. The.
8e260 2a 2a 20 73 69 7a 65 20 72 65 74 75 72 6e 65 64 ** size returned
8e270 20 6f 6d 69 74 73 20 74 68 65 20 38 2d 62 79 74 omits the 8-byt
8e280 65 20 68 65 61 64 65 72 20 6f 76 65 72 68 65 61 e header overhea
8e290 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a d. This only.**
8e2a0 20 77 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b works for chunk
8e2b0 73 20 74 68 61 74 20 61 72 65 20 63 75 72 72 65 s that are curre
8e2c0 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 ntly checked out
8e2d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
8e2e0 6d 65 6d 73 79 73 35 53 69 7a 65 28 76 6f 69 64 memsys5Size(void
8e2f0 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 53 69 7a *p){. int iSiz
8e300 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29 e = 0;. if( p )
8e310 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 28 28 {. int i = ((
8e320 75 38 20 2a 29 70 2d 6d 65 6d 35 2e 7a 50 6f 6f u8 *)p-mem5.zPoo
8e330 6c 29 2f 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a l)/mem5.szAtom;.
8e340 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 assert( i>=0
8e350 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 && i<mem5.nBloc
8e360 6b 20 29 3b 0a 20 20 20 20 69 53 69 7a 65 20 3d k );. iSize =
8e370 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 2a 20 28 mem5.szAtom * (
8e380 31 20 3c 3c 20 28 6d 65 6d 35 2e 61 43 74 72 6c 1 << (mem5.aCtrl
8e390 5b 69 5d 26 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 [i]&CTRL_LOGSIZE
8e3a0 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ));. }. return
8e3b0 20 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a iSize;.}../*.**
8e3c0 20 46 69 6e 64 20 74 68 65 20 66 69 72 73 74 20 Find the first
8e3d0 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 66 72 65 entry on the fre
8e3e0 65 6c 69 73 74 20 69 4c 6f 67 73 69 7a 65 2e 20 elist iLogsize.
8e3f0 20 55 6e 6c 69 6e 6b 20 74 68 61 74 0a 2a 2a 20 Unlink that.**
8e400 65 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72 6e entry and return
8e410 20 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2f 0a its index. .*/.
8e420 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 static int memsy
8e430 73 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69 6e s5UnlinkFirst(in
8e440 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69 t iLogsize){. i
8e450 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 46 69 72 nt i;. int iFir
8e460 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 st;.. assert( i
8e470 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c Logsize>=0 && iL
8e480 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 ogsize<=LOGMAX )
8e490 3b 0a 20 20 69 20 3d 20 69 46 69 72 73 74 20 3d ;. i = iFirst =
8e4a0 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 mem5.aiFreelist
8e4b0 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 61 73 [iLogsize];. as
8e4c0 73 65 72 74 28 20 69 46 69 72 73 74 3e 3d 30 20 sert( iFirst>=0
8e4d0 29 3b 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20 );. while( i>0
8e4e0 29 7b 0a 20 20 20 20 69 66 28 20 69 3c 69 46 69 ){. if( i<iFi
8e4f0 72 73 74 20 29 20 69 46 69 72 73 74 20 3d 20 69 rst ) iFirst = i
8e500 3b 0a 20 20 20 20 69 20 3d 20 4d 45 4d 35 4c 49 ;. i = MEM5LI
8e510 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 7d NK(i)->next;. }
8e520 0a 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b . memsys5Unlink
8e530 28 69 46 69 72 73 74 2c 20 69 4c 6f 67 73 69 7a (iFirst, iLogsiz
8e540 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46 69 e);. return iFi
8e550 72 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rst;.}../*.** Re
8e560 74 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20 turn a block of
8e570 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 memory of at lea
8e580 73 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a st nBytes in siz
8e590 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c e..** Return NUL
8e5a0 4c 20 69 66 20 75 6e 61 62 6c 65 2e 20 20 52 65 L if unable. Re
8e5b0 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 42 79 turn NULL if nBy
8e5c0 74 65 73 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 tes==0..**.** Th
8e5d0 65 20 63 61 6c 6c 65 72 20 67 75 61 72 61 6e 74 e caller guarant
8e5e0 65 65 73 20 74 68 61 74 20 6e 42 79 74 65 20 70 ees that nByte p
8e5f0 6f 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 ositive..**.** T
8e600 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f 62 he caller has ob
8e610 74 61 69 6e 65 64 20 61 20 6d 75 74 65 78 20 70 tained a mutex p
8e620 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 rior to invoking
8e630 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 this.** routine
8e640 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e 65 76 so there is nev
8e650 65 72 20 61 6e 79 20 63 68 61 6e 63 65 20 74 68 er any chance th
8e660 61 74 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a at two or more.*
8e670 2a 20 74 68 72 65 61 64 73 20 63 61 6e 20 62 65 * threads can be
8e680 20 69 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 in this routine
8e690 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d at the same tim
8e6a0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
8e6b0 64 20 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 d *memsys5Malloc
8e6c0 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74 65 Unsafe(int nByte
8e6d0 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 ){. int i;
8e6e0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
8e6f0 66 20 61 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 5d f a mem5.aPool[]
8e700 20 73 6c 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 69 slot */. int i
8e710 42 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 49 Bin; /* I
8e720 6e 64 65 78 20 69 6e 74 6f 20 6d 65 6d 35 2e 61 ndex into mem5.a
8e730 69 46 72 65 65 6c 69 73 74 5b 5d 20 2a 2f 0a 20 iFreelist[] */.
8e740 20 69 6e 74 20 69 46 75 6c 6c 53 7a 3b 20 20 20 int iFullSz;
8e750 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c /* Size of all
8e760 6f 63 61 74 69 6f 6e 20 72 6f 75 6e 64 65 64 20 ocation rounded
8e770 75 70 20 74 6f 20 70 6f 77 65 72 20 6f 66 20 32 up to power of 2
8e780 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 6f 67 73 69 */. int iLogsi
8e790 7a 65 3b 20 20 20 20 2f 2a 20 4c 6f 67 32 20 6f ze; /* Log2 o
8e7a0 66 20 69 46 75 6c 6c 53 7a 2f 50 4f 57 32 5f 4d f iFullSz/POW2_M
8e7b0 49 4e 20 2a 2f 0a 0a 20 20 2f 2a 20 6e 42 79 74 IN */.. /* nByt
8e7c0 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f 73 69 e must be a posi
8e7d0 74 69 76 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 tive */. assert
8e7e0 28 20 6e 42 79 74 65 3e 30 20 29 3b 0a 0a 20 20 ( nByte>0 );..
8e7f0 2f 2a 20 4b 65 65 70 20 74 72 61 63 6b 20 6f 66 /* Keep track of
8e800 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c the maximum all
8e810 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 2e ocation request.
8e820 20 20 45 76 65 6e 20 75 6e 66 75 6c 66 69 6c 6c Even unfulfill
8e830 65 64 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 73 ed. ** requests
8e840 20 61 72 65 20 63 6f 75 6e 74 65 64 20 2a 2f 0a are counted */.
8e850 20 20 69 66 28 20 28 75 33 32 29 6e 42 79 74 65 if( (u32)nByte
8e860 3e 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 >mem5.maxRequest
8e870 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 61 78 ){. mem5.max
8e880 52 65 71 75 65 73 74 20 3d 20 6e 42 79 74 65 3b Request = nByte;
8e890 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 62 6f 72 74 . }.. /* Abort
8e8a0 20 69 66 20 74 68 65 20 72 65 71 75 65 73 74 65 if the requeste
8e8b0 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a d allocation siz
8e8c0 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e e is larger than
8e8d0 20 74 68 65 20 6c 61 72 67 65 73 74 0a 20 20 2a the largest. *
8e8e0 2a 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 74 * power of two t
8e8f0 68 61 74 20 77 65 20 63 61 6e 20 72 65 70 72 65 hat we can repre
8e900 73 65 6e 74 20 75 73 69 6e 67 20 33 32 2d 62 69 sent using 32-bi
8e910 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 t signed integer
8e920 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 s.. */. if( nB
8e930 79 74 65 20 3e 20 30 78 34 30 30 30 30 30 30 30 yte > 0x40000000
8e940 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
8e950 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 75 6e ;. }.. /* Roun
8e960 64 20 6e 42 79 74 65 20 75 70 20 74 6f 20 74 68 d nByte up to th
8e970 65 20 6e 65 78 74 20 76 61 6c 69 64 20 70 6f 77 e next valid pow
8e980 65 72 20 6f 66 20 74 77 6f 20 2a 2f 0a 20 20 66 er of two */. f
8e990 6f 72 28 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35 2e or(iFullSz=mem5.
8e9a0 73 7a 41 74 6f 6d 2c 20 69 4c 6f 67 73 69 7a 65 szAtom, iLogsize
8e9b0 3d 30 3b 20 69 46 75 6c 6c 53 7a 3c 6e 42 79 74 =0; iFullSz<nByt
8e9c0 65 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20 32 2c e; iFullSz *= 2,
8e9d0 20 69 4c 6f 67 73 69 7a 65 2b 2b 29 7b 7d 0a 0a iLogsize++){}..
8e9e0 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 6d /* Make sure m
8e9f0 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 em5.aiFreelist[i
8ea00 4c 6f 67 73 69 7a 65 5d 20 63 6f 6e 74 61 69 6e Logsize] contain
8ea10 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 66 s at least one f
8ea20 72 65 65 0a 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 ree. ** block.
8ea30 20 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20 73 70 If not, then sp
8ea40 6c 69 74 20 61 20 62 6c 6f 63 6b 20 6f 66 20 74 lit a block of t
8ea50 68 65 20 6e 65 78 74 20 6c 61 72 67 65 72 20 70 he next larger p
8ea60 6f 77 65 72 20 6f 66 0a 20 20 2a 2a 20 74 77 6f ower of. ** two
8ea70 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 in order to cre
8ea80 61 74 65 20 61 20 6e 65 77 20 66 72 65 65 20 62 ate a new free b
8ea90 6c 6f 63 6b 20 6f 66 20 73 69 7a 65 20 69 4c 6f lock of size iLo
8eaa0 67 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f gsize.. */. fo
8eab0 72 28 69 42 69 6e 3d 69 4c 6f 67 73 69 7a 65 3b r(iBin=iLogsize;
8eac0 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 mem5.aiFreelist
8ead0 5b 69 42 69 6e 5d 3c 30 20 26 26 20 69 42 69 6e [iBin]<0 && iBin
8eae0 3c 3d 4c 4f 47 4d 41 58 3b 20 69 42 69 6e 2b 2b <=LOGMAX; iBin++
8eaf0 29 7b 7d 0a 20 20 69 66 28 20 69 42 69 6e 3e 4c ){}. if( iBin>L
8eb00 4f 47 4d 41 58 20 29 20 72 65 74 75 72 6e 20 30 OGMAX ) return 0
8eb10 3b 0a 20 20 69 20 3d 20 6d 65 6d 73 79 73 35 55 ;. i = memsys5U
8eb20 6e 6c 69 6e 6b 46 69 72 73 74 28 69 42 69 6e 29 nlinkFirst(iBin)
8eb30 3b 0a 20 20 77 68 69 6c 65 28 20 69 42 69 6e 3e ;. while( iBin>
8eb40 69 4c 6f 67 73 69 7a 65 20 29 7b 0a 20 20 20 20 iLogsize ){.
8eb50 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 0a 20 20 int newSize;..
8eb60 20 20 69 42 69 6e 2d 2d 3b 0a 20 20 20 20 6e 65 iBin--;. ne
8eb70 77 53 69 7a 65 20 3d 20 31 20 3c 3c 20 69 42 69 wSize = 1 << iBi
8eb80 6e 3b 0a 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 n;. mem5.aCtr
8eb90 6c 5b 69 2b 6e 65 77 53 69 7a 65 5d 20 3d 20 43 l[i+newSize] = C
8eba0 54 52 4c 5f 46 52 45 45 20 7c 20 69 42 69 6e 3b TRL_FREE | iBin;
8ebb0 0a 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b . memsys5Link
8ebc0 28 69 2b 6e 65 77 53 69 7a 65 2c 20 69 42 69 6e (i+newSize, iBin
8ebd0 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 43 );. }. mem5.aC
8ebe0 74 72 6c 5b 69 5d 20 3d 20 69 4c 6f 67 73 69 7a trl[i] = iLogsiz
8ebf0 65 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 e;.. /* Update
8ec00 61 6c 6c 6f 63 61 74 6f 72 20 70 65 72 66 6f 72 allocator perfor
8ec10 6d 61 6e 63 65 20 73 74 61 74 69 73 74 69 63 73 mance statistics
8ec20 2e 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6e 41 6c 6c . */. mem5.nAll
8ec30 6f 63 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 oc++;. mem5.tot
8ec40 61 6c 41 6c 6c 6f 63 20 2b 3d 20 69 46 75 6c 6c alAlloc += iFull
8ec50 53 7a 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c Sz;. mem5.total
8ec60 45 78 63 65 73 73 20 2b 3d 20 69 46 75 6c 6c 53 Excess += iFullS
8ec70 7a 20 2d 20 6e 42 79 74 65 3b 0a 20 20 6d 65 6d z - nByte;. mem
8ec80 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2b 2b 5.currentCount++
8ec90 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 ;. mem5.current
8eca0 4f 75 74 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a Out += iFullSz;.
8ecb0 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 43 6f if( mem5.maxCo
8ecc0 75 6e 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74 unt<mem5.current
8ecd0 43 6f 75 6e 74 20 29 20 6d 65 6d 35 2e 6d 61 78 Count ) mem5.max
8ece0 43 6f 75 6e 74 20 3d 20 6d 65 6d 35 2e 63 75 72 Count = mem5.cur
8ecf0 72 65 6e 74 43 6f 75 6e 74 3b 0a 20 20 69 66 28 rentCount;. if(
8ed00 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 3c 6d 65 6d mem5.maxOut<mem
8ed10 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 29 20 6d 5.currentOut ) m
8ed20 65 6d 35 2e 6d 61 78 4f 75 74 20 3d 20 6d 65 6d em5.maxOut = mem
8ed30 35 2e 63 75 72 72 65 6e 74 4f 75 74 3b 0a 0a 20 5.currentOut;..
8ed40 20 2f 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 /* Return a poi
8ed50 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f nter to the allo
8ed60 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20 2a 2f cated memory. */
8ed70 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a . return (void*
8ed80 29 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 69 2a 6d )&mem5.zPool[i*m
8ed90 65 6d 35 2e 73 7a 41 74 6f 6d 5d 3b 0a 7d 0a 0a em5.szAtom];.}..
8eda0 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 /*.** Free an ou
8edb0 74 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 tstanding memory
8edc0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a allocation..*/.
8edd0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
8ede0 79 73 35 46 72 65 65 55 6e 73 61 66 65 28 76 6f ys5FreeUnsafe(vo
8edf0 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 75 33 32 id *pOld){. u32
8ee00 20 73 69 7a 65 2c 20 69 4c 6f 67 73 69 7a 65 3b size, iLogsize;
8ee10 0a 20 20 69 6e 74 20 69 42 6c 6f 63 6b 3b 0a 0a . int iBlock;..
8ee20 20 20 2f 2a 20 53 65 74 20 69 42 6c 6f 63 6b 20 /* Set iBlock
8ee30 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 to the index of
8ee40 74 68 65 20 62 6c 6f 63 6b 20 70 6f 69 6e 74 65 the block pointe
8ee50 64 20 74 6f 20 62 79 20 70 4f 6c 64 20 69 6e 20 d to by pOld in
8ee60 0a 20 20 2a 2a 20 74 68 65 20 61 72 72 61 79 20 . ** the array
8ee70 6f 66 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 62 of mem5.szAtom b
8ee80 79 74 65 20 62 6c 6f 63 6b 73 20 70 6f 69 6e 74 yte blocks point
8ee90 65 64 20 74 6f 20 62 79 20 6d 65 6d 35 2e 7a 50 ed to by mem5.zP
8eea0 6f 6f 6c 2e 0a 20 20 2a 2f 0a 20 20 69 42 6c 6f ool.. */. iBlo
8eeb0 63 6b 20 3d 20 28 28 75 38 20 2a 29 70 4f 6c 64 ck = ((u8 *)pOld
8eec0 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d -mem5.zPool)/mem
8eed0 35 2e 73 7a 41 74 6f 6d 3b 0a 0a 20 20 2f 2a 20 5.szAtom;.. /*
8eee0 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 Check that the p
8eef0 6f 69 6e 74 65 72 20 70 4f 6c 64 20 70 6f 69 6e ointer pOld poin
8ef00 74 73 20 74 6f 20 61 20 76 61 6c 69 64 2c 20 6e ts to a valid, n
8ef10 6f 6e 2d 66 72 65 65 20 62 6c 6f 63 6b 2e 20 2a on-free block. *
8ef20 2f 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f /. assert( iBlo
8ef30 63 6b 3e 3d 30 20 26 26 20 69 42 6c 6f 63 6b 3c ck>=0 && iBlock<
8ef40 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 mem5.nBlock );.
8ef50 20 61 73 73 65 72 74 28 20 28 28 75 38 20 2a 29 assert( ((u8 *)
8ef60 70 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 pOld-mem5.zPool)
8ef70 25 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3d 3d 30 20 %mem5.szAtom==0
8ef80 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 );. assert( (me
8ef90 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d m5.aCtrl[iBlock]
8efa0 20 26 20 43 54 52 4c 5f 46 52 45 45 29 3d 3d 30 & CTRL_FREE)==0
8efb0 20 29 3b 0a 0a 20 20 69 4c 6f 67 73 69 7a 65 20 );.. iLogsize
8efc0 3d 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c = mem5.aCtrl[iBl
8efd0 6f 63 6b 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 ock] & CTRL_LOGS
8efe0 49 5a 45 3b 0a 20 20 73 69 7a 65 20 3d 20 31 3c IZE;. size = 1<
8eff0 3c 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 61 73 73 <iLogsize;. ass
8f000 65 72 74 28 20 69 42 6c 6f 63 6b 2b 73 69 7a 65 ert( iBlock+size
8f010 2d 31 3c 28 75 33 32 29 6d 65 6d 35 2e 6e 42 6c -1<(u32)mem5.nBl
8f020 6f 63 6b 20 29 3b 0a 0a 20 20 6d 65 6d 35 2e 61 ock );.. mem5.a
8f030 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 7c 3d 20 Ctrl[iBlock] |=
8f040 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 6d 65 6d CTRL_FREE;. mem
8f050 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 2b 73 5.aCtrl[iBlock+s
8f060 69 7a 65 2d 31 5d 20 7c 3d 20 43 54 52 4c 5f 46 ize-1] |= CTRL_F
8f070 52 45 45 3b 0a 20 20 61 73 73 65 72 74 28 20 6d REE;. assert( m
8f080 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 em5.currentCount
8f090 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 >0 );. assert(
8f0a0 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3e mem5.currentOut>
8f0b0 3d 28 73 69 7a 65 2a 6d 65 6d 35 2e 73 7a 41 74 =(size*mem5.szAt
8f0c0 6f 6d 29 20 29 3b 0a 20 20 6d 65 6d 35 2e 63 75 om) );. mem5.cu
8f0d0 72 72 65 6e 74 43 6f 75 6e 74 2d 2d 3b 0a 20 20 rrentCount--;.
8f0e0 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 mem5.currentOut
8f0f0 2d 3d 20 73 69 7a 65 2a 6d 65 6d 35 2e 73 7a 41 -= size*mem5.szA
8f100 74 6f 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 6d tom;. assert( m
8f110 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3e 30 em5.currentOut>0
8f120 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 || mem5.current
8f130 43 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73 Count==0 );. as
8f140 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 sert( mem5.curre
8f150 6e 74 43 6f 75 6e 74 3e 30 20 7c 7c 20 6d 65 6d ntCount>0 || mem
8f160 35 2e 63 75 72 72 65 6e 74 4f 75 74 3d 3d 30 20 5.currentOut==0
8f170 29 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c );.. mem5.aCtrl
8f180 5b 69 42 6c 6f 63 6b 5d 20 3d 20 43 54 52 4c 5f [iBlock] = CTRL_
8f190 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 3b FREE | iLogsize;
8f1a0 0a 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53 . while( ALWAYS
8f1b0 28 69 4c 6f 67 73 69 7a 65 3c 4c 4f 47 4d 41 58 (iLogsize<LOGMAX
8f1c0 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 75 ) ){. int iBu
8f1d0 64 64 79 3b 0a 20 20 20 20 69 66 28 20 28 69 42 ddy;. if( (iB
8f1e0 6c 6f 63 6b 3e 3e 69 4c 6f 67 73 69 7a 65 29 20 lock>>iLogsize)
8f1f0 26 20 31 20 29 7b 0a 20 20 20 20 20 20 69 42 75 & 1 ){. iBu
8f200 64 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2d 20 73 ddy = iBlock - s
8f210 69 7a 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ize;. }else{.
8f220 20 20 20 20 20 20 69 42 75 64 64 79 20 3d 20 69 iBuddy = i
8f230 42 6c 6f 63 6b 20 2b 20 73 69 7a 65 3b 0a 20 20 Block + size;.
8f240 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
8f250 69 42 75 64 64 79 3e 3d 30 20 29 3b 0a 20 20 20 iBuddy>=0 );.
8f260 20 69 66 28 20 28 69 42 75 64 64 79 2b 28 31 3c if( (iBuddy+(1<
8f270 3c 69 4c 6f 67 73 69 7a 65 29 29 3e 6d 65 6d 35 <iLogsize))>mem5
8f280 2e 6e 42 6c 6f 63 6b 20 29 20 62 72 65 61 6b 3b .nBlock ) break;
8f290 0a 20 20 20 20 69 66 28 20 6d 65 6d 35 2e 61 43 . if( mem5.aC
8f2a0 74 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43 54 trl[iBuddy]!=(CT
8f2b0 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 RL_FREE | iLogsi
8f2c0 7a 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 ze) ) break;.
8f2d0 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 memsys5Unlink(i
8f2e0 42 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a 65 29 Buddy, iLogsize)
8f2f0 3b 0a 20 20 20 20 69 4c 6f 67 73 69 7a 65 2b 2b ;. iLogsize++
8f300 3b 0a 20 20 20 20 69 66 28 20 69 42 75 64 64 79 ;. if( iBuddy
8f310 3c 69 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 <iBlock ){.
8f320 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 mem5.aCtrl[iBud
8f330 64 79 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 dy] = CTRL_FREE
8f340 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20 | iLogsize;.
8f350 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c mem5.aCtrl[iBl
8f360 6f 63 6b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 ock] = 0;.
8f370 69 42 6c 6f 63 6b 20 3d 20 69 42 75 64 64 79 3b iBlock = iBuddy;
8f380 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
8f390 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c mem5.aCtrl[iBl
8f3a0 6f 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 ock] = CTRL_FREE
8f3b0 20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 | iLogsize;.
8f3c0 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 mem5.aCtrl[iB
8f3d0 75 64 64 79 5d 20 3d 20 30 3b 0a 20 20 20 20 7d uddy] = 0;. }
8f3e0 0a 20 20 20 20 73 69 7a 65 20 2a 3d 20 32 3b 0a . size *= 2;.
8f3f0 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c 69 6e }. memsys5Lin
8f400 6b 28 69 42 6c 6f 63 6b 2c 20 69 4c 6f 67 73 69 k(iBlock, iLogsi
8f410 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c ze);.}../*.** Al
8f420 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f 66 locate nBytes of
8f430 20 6d 65 6d 6f 72 79 0a 2a 2f 0a 73 74 61 74 69 memory.*/.stati
8f440 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d c void *memsys5M
8f450 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 73 alloc(int nBytes
8f460 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 ){. sqlite3_int
8f470 36 34 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 64 *p = 0;. if(
8f480 20 6e 42 79 74 65 73 3e 30 20 29 7b 0a 20 20 20 nBytes>0 ){.
8f490 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b memsys5Enter();
8f4a0 0a 20 20 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 . p = memsys5
8f4b0 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 MallocUnsafe(nBy
8f4c0 74 65 73 29 3b 0a 20 20 20 20 6d 65 6d 73 79 73 tes);. memsys
8f4d0 35 4c 65 61 76 65 28 29 3b 0a 20 20 7d 0a 20 20 5Leave();. }.
8f4e0 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b return (void*)p;
8f4f0 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 .}../*.** Free
8f500 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 memory..**.** Th
8f510 65 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d 65 e outer layer me
8f520 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 70 mory allocator p
8f530 72 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f 75 revents this rou
8f540 74 69 6e 65 20 66 72 6f 6d 0a 2a 2a 20 62 65 69 tine from.** bei
8f550 6e 67 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70 ng called with p
8f560 50 72 69 6f 72 3d 3d 30 2e 0a 2a 2f 0a 73 74 61 Prior==0..*/.sta
8f570 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 tic void memsys5
8f580 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f Free(void *pPrio
8f590 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 r){. assert( pP
8f5a0 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 6d 65 6d rior!=0 );. mem
8f5b0 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 6d sys5Enter();. m
8f5c0 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66 65 emsys5FreeUnsafe
8f5d0 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d 73 (pPrior);. mems
8f5e0 79 73 35 4c 65 61 76 65 28 29 3b 20 20 0a 7d 0a ys5Leave(); .}.
8f5f0 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
8f600 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 e size of an exi
8f610 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c sting memory all
8f620 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 ocation..**.** T
8f630 68 65 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d he outer layer m
8f640 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 emory allocator
8f650 70 72 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f prevents this ro
8f660 75 74 69 6e 65 20 66 72 6f 6d 0a 2a 2a 20 62 65 utine from.** be
8f670 69 6e 67 20 63 61 6c 6c 65 64 20 77 69 74 68 20 ing called with
8f680 70 50 72 69 6f 72 3d 3d 30 2e 20 20 0a 2a 2a 0a pPrior==0. .**.
8f690 2a 2a 20 6e 42 79 74 65 73 20 69 73 20 61 6c 77 ** nBytes is alw
8f6a0 61 79 73 20 61 20 76 61 6c 75 65 20 6f 62 74 61 ays a value obta
8f6b0 69 6e 65 64 20 66 72 6f 6d 20 61 20 70 72 69 6f ined from a prio
8f6c0 72 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 6d 65 6d r call to.** mem
8f6d0 73 79 73 35 52 6f 75 6e 64 28 29 2e 20 20 48 65 sys5Round(). He
8f6e0 6e 63 65 20 6e 42 79 74 65 73 20 69 73 20 61 6c nce nBytes is al
8f6f0 77 61 79 73 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 ways a non-negat
8f700 69 76 65 20 70 6f 77 65 72 0a 2a 2a 20 6f 66 20 ive power.** of
8f710 74 77 6f 2e 20 20 49 66 20 6e 42 79 74 65 73 3d two. If nBytes=
8f720 3d 30 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 =0 that means th
8f730 61 74 20 61 6e 20 6f 76 65 72 73 69 7a 65 20 61 at an oversize a
8f740 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 28 61 6e llocation.** (an
8f750 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67 allocation larg
8f760 65 72 20 74 68 61 6e 20 30 78 34 30 30 30 30 30 er than 0x400000
8f770 30 30 29 20 77 61 73 20 72 65 71 75 65 73 74 65 00) was requeste
8f780 64 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72 6f d and this.** ro
8f790 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65 74 utine should ret
8f7a0 75 72 6e 20 30 20 77 69 74 68 6f 75 74 20 66 72 urn 0 without fr
8f7b0 65 65 69 6e 67 20 70 50 72 69 6f 72 2e 0a 2a 2f eeing pPrior..*/
8f7c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 .static void *me
8f7d0 6d 73 79 73 35 52 65 61 6c 6c 6f 63 28 76 6f 69 msys5Realloc(voi
8f7e0 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e d *pPrior, int n
8f7f0 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f Bytes){. int nO
8f800 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 ld;. void *p;.
8f810 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 assert( pPrior!
8f820 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
8f830 28 6e 42 79 74 65 73 26 28 6e 42 79 74 65 73 2d (nBytes&(nBytes-
8f840 31 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 1))==0 );. asse
8f850 72 74 28 20 6e 42 79 74 65 73 3e 3d 30 20 29 3b rt( nBytes>=0 );
8f860 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3d 3d 30 . if( nBytes==0
8f870 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
8f880 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d ;. }. nOld = m
8f890 65 6d 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f emsys5Size(pPrio
8f8a0 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 r);. if( nBytes
8f8b0 3c 3d 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65 <=nOld ){. re
8f8c0 74 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d turn pPrior;. }
8f8d0 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 . memsys5Enter(
8f8e0 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 );. p = memsys5
8f8f0 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 MallocUnsafe(nBy
8f900 74 65 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b tes);. if( p ){
8f910 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 . memcpy(p, p
8f920 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 Prior, nOld);.
8f930 20 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 memsys5FreeUns
8f940 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d afe(pPrior);. }
8f950 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 . memsys5Leave(
8f960 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d );. return p;.}
8f970 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 ../*.** Round up
8f980 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 a request size
8f990 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 to the next vali
8f9a0 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a d allocation siz
8f9b0 65 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 61 6c e. If.** the al
8f9c0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 74 6f 6f 20 location is too
8f9d0 6c 61 72 67 65 20 74 6f 20 62 65 20 68 61 6e 64 large to be hand
8f9e0 6c 65 64 20 62 79 20 74 68 69 73 20 61 6c 6c 6f led by this allo
8f9f0 63 61 74 69 6f 6e 20 73 79 73 74 65 6d 2c 0a 2a cation system,.*
8fa00 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a * return 0..**.*
8fa10 2a 20 41 6c 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e * All allocation
8fa20 73 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 s must be a powe
8fa30 72 20 6f 66 20 74 77 6f 20 61 6e 64 20 6d 75 73 r of two and mus
8fa40 74 20 62 65 20 65 78 70 72 65 73 73 65 64 20 62 t be expressed b
8fa50 79 20 61 0a 2a 2a 20 33 32 2d 62 69 74 20 73 69 y a.** 32-bit si
8fa60 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20 48 gned integer. H
8fa70 65 6e 63 65 20 74 68 65 20 6c 61 72 67 65 73 74 ence the largest
8fa80 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 30 allocation is 0
8fa90 78 34 30 30 30 30 30 30 30 0a 2a 2a 20 6f 72 20 x40000000.** or
8faa0 31 30 37 33 37 34 31 38 32 34 20 62 79 74 65 73 1073741824 bytes
8fab0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
8fac0 6d 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28 69 memsys5Roundup(i
8fad0 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46 75 nt n){. int iFu
8fae0 6c 6c 53 7a 3b 0a 20 20 69 66 28 20 6e 20 3e 20 llSz;. if( n >
8faf0 30 78 34 30 30 30 30 30 30 30 20 29 20 72 65 74 0x40000000 ) ret
8fb00 75 72 6e 20 30 3b 0a 20 20 66 6f 72 28 69 46 75 urn 0;. for(iFu
8fb10 6c 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41 74 6f 6d llSz=mem5.szAtom
8fb20 3b 20 69 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75 ; iFullSz<n; iFu
8fb30 6c 6c 53 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65 llSz *= 2);. re
8fb40 74 75 72 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a turn iFullSz;.}.
8fb50 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
8fb60 65 20 63 65 69 6c 69 6e 67 20 6f 66 20 74 68 65 e ceiling of the
8fb70 20 6c 6f 67 61 72 69 74 68 6d 20 62 61 73 65 20 logarithm base
8fb80 32 20 6f 66 20 69 56 61 6c 75 65 2e 0a 2a 2a 0a 2 of iValue..**.
8fb90 2a 2a 20 45 78 61 6d 70 6c 65 73 3a 20 20 20 6d ** Examples: m
8fba0 65 6d 73 79 73 35 4c 6f 67 28 31 29 20 2d 3e 20 emsys5Log(1) ->
8fbb0 30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 0.**
8fbc0 20 6d 65 6d 73 79 73 35 4c 6f 67 28 32 29 20 2d memsys5Log(2) -
8fbd0 3e 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 > 1.**
8fbe0 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 34 29 memsys5Log(4)
8fbf0 20 2d 3e 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 -> 2.**
8fc00 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 memsys5Log(
8fc10 35 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20 20 20 5) -> 3.**
8fc20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f memsys5Lo
8fc30 67 28 38 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20 g(8) -> 3.**
8fc40 20 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 memsys5
8fc50 4c 6f 67 28 39 29 20 2d 3e 20 34 0a 2a 2f 0a 73 Log(9) -> 4.*/.s
8fc60 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 tatic int memsys
8fc70 35 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65 29 5Log(int iValue)
8fc80 7b 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20 20 {. int iLog;.
8fc90 66 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 31 3c 3c for(iLog=0; (1<<
8fca0 69 4c 6f 67 29 3c 69 56 61 6c 75 65 3b 20 69 4c iLog)<iValue; iL
8fcb0 6f 67 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e 20 og++);. return
8fcc0 69 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 iLog;.}../*.** I
8fcd0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 nitialize the me
8fce0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 0a mory allocator..
8fcf0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
8fd00 6e 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 ne is not thread
8fd10 73 61 66 65 2e 20 20 54 68 65 20 63 61 6c 6c 65 safe. The calle
8fd20 72 20 6d 75 73 74 20 62 65 20 68 6f 6c 64 69 6e r must be holdin
8fd30 67 20 61 20 6d 75 74 65 78 0a 2a 2a 20 74 6f 20 g a mutex.** to
8fd40 70 72 65 76 65 6e 74 20 6d 75 6c 74 69 70 6c 65 prevent multiple
8fd50 20 74 68 72 65 61 64 73 20 66 72 6f 6d 20 65 6e threads from en
8fd60 74 65 72 69 6e 67 20 61 74 20 74 68 65 20 73 61 tering at the sa
8fd70 6d 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61 74 me time..*/.stat
8fd80 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 49 6e ic int memsys5In
8fd90 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 it(void *NotUsed
8fda0 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 20 ){. int ii;
8fdb0 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
8fdc0 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 counter */. int
8fdd0 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 nByte;
8fde0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
8fdf0 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 es of memory ava
8fe00 69 6c 61 62 6c 65 20 74 6f 20 74 68 69 73 20 61 ilable to this a
8fe10 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 75 38 llocator */. u8
8fe20 20 2a 7a 42 79 74 65 3b 20 20 20 20 20 20 20 20 *zByte;
8fe30 20 2f 2a 20 4d 65 6d 6f 72 79 20 75 73 61 62 6c /* Memory usabl
8fe40 65 20 62 79 20 74 68 69 73 20 61 6c 6c 6f 63 61 e by this alloca
8fe50 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 69 tor */. int nMi
8fe60 6e 4c 6f 67 3b 20 20 20 20 20 20 20 2f 2a 20 4c nLog; /* L
8fe70 6f 67 20 62 61 73 65 20 32 20 6f 66 20 6d 69 6e og base 2 of min
8fe80 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 imum allocation
8fe90 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 2a 2f size in bytes */
8fea0 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 . int iOffset;
8feb0 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 66 66 73 /* An offs
8fec0 65 74 20 69 6e 74 6f 20 6d 65 6d 35 2e 61 43 74 et into mem5.aCt
8fed0 72 6c 5b 5d 20 2a 2f 0a 0a 20 20 55 4e 55 53 45 rl[] */.. UNUSE
8fee0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
8fef0 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 sed);.. /* For
8ff00 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 the purposes of
8ff10 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 64 69 this routine, di
8ff20 73 61 62 6c 65 20 74 68 65 20 6d 75 74 65 78 20 sable the mutex
8ff30 2a 2f 0a 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 */. mem5.mutex
8ff40 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 73 = 0;.. /* The s
8ff50 69 7a 65 20 6f 66 20 61 20 4d 65 6d 35 4c 69 6e ize of a Mem5Lin
8ff60 6b 20 6f 62 6a 65 63 74 20 6d 75 73 74 20 62 65 k object must be
8ff70 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e a power of two.
8ff80 20 20 56 65 72 69 66 79 20 74 68 61 74 0a 20 20 Verify that.
8ff90 2a 2a 20 74 68 69 73 20 69 73 20 63 61 73 65 2e ** this is case.
8ffa0 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
8ffb0 28 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e 6b (sizeof(Mem5Link
8ffc0 29 26 28 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 )&(sizeof(Mem5Li
8ffd0 6e 6b 29 2d 31 29 29 3d 3d 30 20 29 3b 0a 0a 20 nk)-1))==0 );..
8ffe0 20 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65 33 nByte = sqlite3
8fff0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 GlobalConfig.nHe
90000 61 70 3b 0a 20 20 7a 42 79 74 65 20 3d 20 28 75 ap;. zByte = (u
90010 38 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 8*)sqlite3Global
90020 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a 20 20 Config.pHeap;.
90030 61 73 73 65 72 74 28 20 7a 42 79 74 65 21 3d 30 assert( zByte!=0
90040 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f ); /* sqlite3_
90050 63 6f 6e 66 69 67 28 29 20 64 6f 65 73 20 6e 6f config() does no
90060 74 20 61 6c 6c 6f 77 20 6f 74 68 65 72 77 69 73 t allow otherwis
90070 65 20 2a 2f 0a 0a 20 20 6e 4d 69 6e 4c 6f 67 20 e */.. nMinLog
90080 3d 20 6d 65 6d 73 79 73 35 4c 6f 67 28 73 71 6c = memsys5Log(sql
90090 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
900a0 2e 6d 6e 52 65 71 29 3b 0a 20 20 6d 65 6d 35 2e .mnReq);. mem5.
900b0 73 7a 41 74 6f 6d 20 3d 20 28 31 3c 3c 6e 4d 69 szAtom = (1<<nMi
900c0 6e 4c 6f 67 29 3b 0a 20 20 77 68 69 6c 65 28 20 nLog);. while(
900d0 28 69 6e 74 29 73 69 7a 65 6f 66 28 4d 65 6d 35 (int)sizeof(Mem5
900e0 4c 69 6e 6b 29 3e 6d 65 6d 35 2e 73 7a 41 74 6f Link)>mem5.szAto
900f0 6d 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 73 7a m ){. mem5.sz
90100 41 74 6f 6d 20 3d 20 6d 65 6d 35 2e 73 7a 41 74 Atom = mem5.szAt
90110 6f 6d 20 3c 3c 20 31 3b 0a 20 20 7d 0a 0a 20 20 om << 1;. }..
90120 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 3d 20 28 6e mem5.nBlock = (n
90130 42 79 74 65 20 2f 20 28 6d 65 6d 35 2e 73 7a 41 Byte / (mem5.szA
90140 74 6f 6d 2b 73 69 7a 65 6f 66 28 75 38 29 29 29 tom+sizeof(u8)))
90150 3b 0a 20 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 3d ;. mem5.zPool =
90160 20 7a 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e 61 zByte;. mem5.a
90170 43 74 72 6c 20 3d 20 28 75 38 20 2a 29 26 6d 65 Ctrl = (u8 *)&me
90180 6d 35 2e 7a 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42 m5.zPool[mem5.nB
90190 6c 6f 63 6b 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d lock*mem5.szAtom
901a0 5d 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 ];.. for(ii=0;
901b0 69 69 3c 3d 4c 4f 47 4d 41 58 3b 20 69 69 2b 2b ii<=LOGMAX; ii++
901c0 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 ){. mem5.aiFr
901d0 65 65 6c 69 73 74 5b 69 69 5d 20 3d 20 2d 31 3b eelist[ii] = -1;
901e0 0a 20 20 7d 0a 0a 20 20 69 4f 66 66 73 65 74 20 . }.. iOffset
901f0 3d 20 30 3b 0a 20 20 66 6f 72 28 69 69 3d 4c 4f = 0;. for(ii=LO
90200 47 4d 41 58 3b 20 69 69 3e 3d 30 3b 20 69 69 2d GMAX; ii>=0; ii-
90210 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c -){. int nAll
90220 6f 63 20 3d 20 28 31 3c 3c 69 69 29 3b 0a 20 20 oc = (1<<ii);.
90230 20 20 69 66 28 20 28 69 4f 66 66 73 65 74 2b 6e if( (iOffset+n
90240 41 6c 6c 6f 63 29 3c 3d 6d 65 6d 35 2e 6e 42 6c Alloc)<=mem5.nBl
90250 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d ock ){. mem
90260 35 2e 61 43 74 72 6c 5b 69 4f 66 66 73 65 74 5d 5.aCtrl[iOffset]
90270 20 3d 20 69 69 20 7c 20 43 54 52 4c 5f 46 52 45 = ii | CTRL_FRE
90280 45 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 35 E;. memsys5
90290 4c 69 6e 6b 28 69 4f 66 66 73 65 74 2c 20 69 69 Link(iOffset, ii
902a0 29 3b 0a 20 20 20 20 20 20 69 4f 66 66 73 65 74 );. iOffset
902b0 20 2b 3d 20 6e 41 6c 6c 6f 63 3b 0a 20 20 20 20 += nAlloc;.
902c0 7d 0a 20 20 20 20 61 73 73 65 72 74 28 28 69 4f }. assert((iO
902d0 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 3e 6d 65 ffset+nAlloc)>me
902e0 6d 35 2e 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a m5.nBlock);. }.
902f0 0a 20 20 2f 2a 20 49 66 20 61 20 6d 75 74 65 78 . /* If a mutex
90300 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 is required for
90310 20 6e 6f 72 6d 61 6c 20 6f 70 65 72 61 74 69 6f normal operatio
90320 6e 2c 20 61 6c 6c 6f 63 61 74 65 20 6f 6e 65 20 n, allocate one
90330 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 */. if( sqlite3
90340 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
90350 6d 73 74 61 74 3d 3d 30 20 29 7b 0a 20 20 20 20 mstat==0 ){.
90360 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20 73 71 6c mem5.mutex = sql
90370 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
90380 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
90390 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 0a 20 20 IC_MEM);. }..
903a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
903b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 ;.}../*.** Deini
903c0 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 tialize this mod
903d0 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ule..*/.static v
903e0 6f 69 64 20 6d 65 6d 73 79 73 35 53 68 75 74 64 oid memsys5Shutd
903f0 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 own(void *NotUse
90400 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 d){. UNUSED_PAR
90410 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
90420 0a 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20 . mem5.mutex =
90430 30 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 0;. return;.}..
90440 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
90450 53 54 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 ST./*.** Open th
90460 65 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 e file indicated
90470 20 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 and write a log
90480 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 of all unfreed
90490 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 memory .** alloc
904a0 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 ations into that
904b0 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f log..*/.SQLITE_
904c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
904d0 69 74 65 33 4d 65 6d 73 79 73 35 44 75 6d 70 28 ite3Memsys5Dump(
904e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
904f0 65 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20 2a ename){. FILE *
90500 6f 75 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c out;. int i, j,
90510 20 6e 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f n;. int nMinLo
90520 67 3b 0a 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e g;.. if( zFilen
90530 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e ame==0 || zFilen
90540 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 ame[0]==0 ){.
90550 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 out = stdout;.
90560 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20 }else{. out
90570 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d = fopen(zFilenam
90580 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28 e, "w");. if(
90590 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 out==0 ){.
905a0 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
905b0 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f "** Unable to o
905c0 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 utput memory deb
905d0 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 ug output log: %
905e0 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 s **\n",.
905f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a z
90600 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 Filename);.
90610 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 return;. }.
90620 20 7d 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 }. memsys5Ente
90630 72 28 29 3b 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d r();. nMinLog =
90640 20 6d 65 6d 73 79 73 35 4c 6f 67 28 6d 65 6d 35 memsys5Log(mem5
90650 2e 73 7a 41 74 6f 6d 29 3b 0a 20 20 66 6f 72 28 .szAtom);. for(
90660 69 3d 30 3b 20 69 3c 3d 4c 4f 47 4d 41 58 20 26 i=0; i<=LOGMAX &
90670 26 20 69 2b 6e 4d 69 6e 4c 6f 67 3c 33 32 3b 20 & i+nMinLog<32;
90680 69 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6e 3d i++){. for(n=
90690 30 2c 20 6a 3d 6d 65 6d 35 2e 61 69 46 72 65 65 0, j=mem5.aiFree
906a0 6c 69 73 74 5b 69 5d 3b 20 6a 3e 3d 30 3b 20 6a list[i]; j>=0; j
906b0 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 6a 29 2d 3e = MEM5LINK(j)->
906c0 6e 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 next, n++){}.
906d0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 66 fprintf(out, "f
906e0 72 65 65 6c 69 73 74 20 69 74 65 6d 73 20 6f 66 reelist items of
906f0 20 73 69 7a 65 20 25 64 3a 20 25 64 5c 6e 22 2c size %d: %d\n",
90700 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3c 3c 20 mem5.szAtom <<
90710 69 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 66 70 72 i, n);. }. fpr
90720 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e intf(out, "mem5.
90730 6e 41 6c 6c 6f 63 20 20 20 20 20 20 20 3d 20 25 nAlloc = %
90740 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6e 41 6c llu\n", mem5.nAl
90750 6c 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 loc);. fprintf(
90760 6f 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c out, "mem5.total
90770 41 6c 6c 6f 63 20 20 20 3d 20 25 6c 6c 75 5c 6e Alloc = %llu\n
90780 22 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c ", mem5.totalAll
90790 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f oc);. fprintf(o
907a0 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 45 ut, "mem5.totalE
907b0 78 63 65 73 73 20 20 3d 20 25 6c 6c 75 5c 6e 22 xcess = %llu\n"
907c0 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 , mem5.totalExce
907d0 73 73 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f ss);. fprintf(o
907e0 75 74 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e ut, "mem5.curren
907f0 74 4f 75 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 tOut = %u\n",
90800 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 29 mem5.currentOut)
90810 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c ;. fprintf(out,
90820 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f "mem5.currentCo
90830 75 6e 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d unt = %u\n", mem
90840 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b 5.currentCount);
90850 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 . fprintf(out,
90860 22 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 20 20 20 "mem5.maxOut
90870 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 = %u\n", mem5
90880 2e 6d 61 78 4f 75 74 29 3b 0a 20 20 66 70 72 69 .maxOut);. fpri
90890 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d ntf(out, "mem5.m
908a0 61 78 43 6f 75 6e 74 20 20 20 20 20 3d 20 25 75 axCount = %u
908b0 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 \n", mem5.maxCou
908c0 6e 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f nt);. fprintf(o
908d0 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 52 65 71 ut, "mem5.maxReq
908e0 75 65 73 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 uest = %u\n",
908f0 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 29 mem5.maxRequest)
90900 3b 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 ;. memsys5Leave
90910 28 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73 ();. if( out==s
90920 74 64 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c tdout ){. ffl
90930 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d ush(stdout);. }
90940 65 6c 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65 else{. fclose
90950 28 6f 75 74 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e (out);. }.}.#en
90960 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 dif../*.** This
90970 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f routine is the o
90980 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 nly routine in t
90990 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 his file with ex
909a0 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 ternal .** linka
909b0 67 65 2e 20 49 74 20 72 65 74 75 72 6e 73 20 61 ge. It returns a
909c0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 pointer to a st
909d0 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 65 6d atic sqlite3_mem
909e0 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 73 74 72 75 _methods.** stru
909f0 63 74 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 ct populated wit
90a00 68 20 74 68 65 20 6d 65 6d 73 79 73 35 20 6d 65 h the memsys5 me
90a10 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 thods..*/.SQLITE
90a20 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 _PRIVATE const s
90a30 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
90a40 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 ds *sqlite3MemGe
90a50 74 4d 65 6d 73 79 73 35 28 76 6f 69 64 29 7b 0a tMemsys5(void){.
90a60 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 static const s
90a70 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
90a80 64 73 20 6d 65 6d 73 79 73 35 4d 65 74 68 6f 64 ds memsys5Method
90a90 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79 s = {. memsy
90aa0 73 35 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d s5Malloc,. m
90ab0 65 6d 73 79 73 35 46 72 65 65 2c 0a 20 20 20 20 emsys5Free,.
90ac0 20 6d 65 6d 73 79 73 35 52 65 61 6c 6c 6f 63 2c memsys5Realloc,
90ad0 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 53 69 7a . memsys5Siz
90ae0 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 e,. memsys5R
90af0 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d oundup,. mem
90b00 73 79 73 35 49 6e 69 74 2c 0a 20 20 20 20 20 6d sys5Init,. m
90b10 65 6d 73 79 73 35 53 68 75 74 64 6f 77 6e 2c 0a emsys5Shutdown,.
90b20 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65 0. };. re
90b30 74 75 72 6e 20 26 6d 65 6d 73 79 73 35 4d 65 74 turn &memsys5Met
90b40 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 hods;.}..#endif
90b50 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 /* SQLITE_ENABLE
90b60 5f 4d 45 4d 53 59 53 35 20 2a 2f 0a 0a 2f 2a 2a _MEMSYS5 */../**
90b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
90b80 20 6f 66 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a of mem5.c *****
90b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
90bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
90bd0 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 63 20 in file mutex.c
90be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
90c10 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31 ** 2007 August 1
90c20 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 4.**.** The auth
90c30 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
90c40 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
90c50 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
90c60 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
90c70 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
90c80 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
90c90 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
90ca0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
90cb0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
90cc0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
90cd0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
90ce0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
90cf0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
90d00 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
90d10 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
90d20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
90d30 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
90d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90d80 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
90d90 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e ntains the C fun
90da0 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c ctions that impl
90db0 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a ement mutexes..*
90dc0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
90dd0 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 ontains code tha
90de0 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 61 63 72 6f t is common acro
90df0 73 73 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70 ss all mutex imp
90e00 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 0a 2a lementations...*
90e10 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e *.** $Id: mutex.
90e20 63 2c 76 20 31 2e 33 31 20 32 30 30 39 2f 30 37 c,v 1.31 2009/07
90e30 2f 31 36 20 31 38 3a 32 31 3a 31 38 20 64 72 68 /16 18:21:18 drh
90e40 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 64 Exp $.*/..#if d
90e50 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
90e60 42 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 64 BUG) && !defined
90e70 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d (SQLITE_MUTEX_OM
90e80 49 54 29 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 64 65 IT)./*.** For de
90e90 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73 bugging purposes
90ea0 2c 20 72 65 63 6f 72 64 20 77 68 65 6e 20 74 68 , record when th
90eb0 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 e mutex subsyste
90ec0 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 m is initialized
90ed0 0a 2a 2a 20 61 6e 64 20 75 6e 69 6e 69 74 69 61 .** and uninitia
90ee0 6c 69 7a 65 64 20 73 6f 20 74 68 61 74 20 77 65 lized so that we
90ef0 20 63 61 6e 20 61 73 73 65 72 74 28 29 20 69 66 can assert() if
90f00 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 74 74 there is an att
90f10 65 6d 70 74 20 74 6f 0a 2a 2a 20 61 6c 6c 6f 63 empt to.** alloc
90f20 61 74 65 20 61 20 6d 75 74 65 78 20 77 68 69 6c ate a mutex whil
90f30 65 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 e the system is
90f40 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a uninitialized..*
90f50 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f /.static SQLITE_
90f60 57 53 44 20 69 6e 74 20 6d 75 74 65 78 49 73 49 WSD int mutexIsI
90f70 6e 69 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 20 nit = 0;.#endif
90f80 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 /* SQLITE_DEBUG
90f90 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */...#ifndef SQL
90fa0 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f ITE_MUTEX_OMIT./
90fb0 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
90fc0 74 68 65 20 6d 75 74 65 78 20 73 79 73 74 65 6d the mutex system
90fd0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
90fe0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d ATE int sqlite3M
90ff0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 utexInit(void){
91000 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
91010 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 73 71 6c TE_OK;. if( sql
91020 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
91030 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 .bCoreMutex ){.
91040 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 if( !sqlite3G
91050 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 lobalConfig.mute
91060 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 29 7b x.xMutexAlloc ){
91070 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
91080 20 78 4d 75 74 65 78 41 6c 6c 6f 63 20 6d 65 74 xMutexAlloc met
91090 68 6f 64 20 68 61 73 20 6e 6f 74 20 62 65 65 6e hod has not been
910a0 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 75 set, then the u
910b0 73 65 72 20 64 69 64 20 6e 6f 74 0a 20 20 20 20 ser did not.
910c0 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61 20 6d ** install a m
910d0 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 utex implementat
910e0 69 6f 6e 20 76 69 61 20 73 71 6c 69 74 65 33 5f ion via sqlite3_
910f0 63 6f 6e 66 69 67 28 29 20 70 72 69 6f 72 20 74 config() prior t
91100 6f 20 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 o . ** sqli
91110 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
91120 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 2e 20 54 being called. T
91130 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 65 73 his block copies
91140 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 20 20 20 pointers to.
91150 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61 75 6c ** the defaul
91160 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e t implementation
91170 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 into the sqlite
91180 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73 74 3GlobalConfig st
91190 72 75 63 74 75 72 65 2e 0a 20 20 20 20 20 20 2a ructure.. *
911a0 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f /. sqlite3_
911b0 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 70 mutex_methods *p
911c0 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 44 65 From = sqlite3De
911d0 66 61 75 6c 74 4d 75 74 65 78 28 29 3b 0a 20 20 faultMutex();.
911e0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
911f0 78 5f 6d 65 74 68 6f 64 73 20 2a 70 54 6f 20 3d x_methods *pTo =
91200 20 26 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 &sqlite3GlobalC
91210 6f 6e 66 69 67 2e 6d 75 74 65 78 3b 0a 0a 20 20 onfig.mutex;..
91220 20 20 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 memcpy(pTo,
91230 70 46 72 6f 6d 2c 20 6f 66 66 73 65 74 6f 66 28 pFrom, offsetof(
91240 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
91250 74 68 6f 64 73 2c 20 78 4d 75 74 65 78 41 6c 6c thods, xMutexAll
91260 6f 63 29 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 oc));. memc
91270 70 79 28 26 70 54 6f 2d 3e 78 4d 75 74 65 78 46 py(&pTo->xMutexF
91280 72 65 65 2c 20 26 70 46 72 6f 6d 2d 3e 78 4d 75 ree, &pFrom->xMu
91290 74 65 78 46 72 65 65 2c 0a 20 20 20 20 20 20 20 texFree,.
912a0 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 54 sizeof(*pT
912b0 6f 29 20 2d 20 6f 66 66 73 65 74 6f 66 28 73 71 o) - offsetof(sq
912c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
912d0 6f 64 73 2c 20 78 4d 75 74 65 78 46 72 65 65 29 ods, xMutexFree)
912e0 29 3b 0a 20 20 20 20 20 20 70 54 6f 2d 3e 78 4d );. pTo->xM
912f0 75 74 65 78 41 6c 6c 6f 63 20 3d 20 70 46 72 6f utexAlloc = pFro
91300 6d 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 3b 0a m->xMutexAlloc;.
91310 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 }. rc = s
91320 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
91330 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 49 ig.mutex.xMutexI
91340 6e 69 74 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 64 nit();. }..#ifd
91350 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
91360 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75 GLOBAL(int, mu
91370 74 65 78 49 73 49 6e 69 74 29 20 3d 20 31 3b 0a texIsInit) = 1;.
91380 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e #endif.. return
91390 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 rc;.}../*.** Sh
913a0 75 74 64 6f 77 6e 20 74 68 65 20 6d 75 74 65 78 utdown the mutex
913b0 20 73 79 73 74 65 6d 2e 20 54 68 69 73 20 63 61 system. This ca
913c0 6c 6c 20 66 72 65 65 73 20 72 65 73 6f 75 72 63 ll frees resourc
913d0 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a es allocated by.
913e0 2a 2a 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49 ** sqlite3MutexI
913f0 6e 69 74 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 nit()..*/.SQLITE
91400 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
91410 69 74 65 33 4d 75 74 65 78 45 6e 64 28 76 6f 69 ite3MutexEnd(voi
91420 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 d){. int rc = S
91430 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
91440 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
91450 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 fig.mutex.xMutex
91460 45 6e 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 End ){. rc =
91470 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
91480 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 fig.mutex.xMutex
91490 45 6e 64 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 64 End();. }..#ifd
914a0 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
914b0 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75 GLOBAL(int, mu
914c0 74 65 78 49 73 49 6e 69 74 29 20 3d 20 30 3b 0a texIsInit) = 0;.
914d0 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e #endif.. return
914e0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
914f0 74 72 69 65 76 65 20 61 20 70 6f 69 6e 74 65 72 trieve a pointer
91500 20 74 6f 20 61 20 73 74 61 74 69 63 20 6d 75 74 to a static mut
91510 65 78 20 6f 72 20 61 6c 6c 6f 63 61 74 65 20 61 ex or allocate a
91520 20 6e 65 77 20 64 79 6e 61 6d 69 63 20 6f 6e 65 new dynamic one
91530 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
91540 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 sqlite3_mutex *s
91550 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
91560 6f 63 28 69 6e 74 20 69 64 29 7b 0a 23 69 66 6e oc(int id){.#ifn
91570 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
91580 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 AUTOINIT. if( s
91590 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
915a0 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a e() ) return 0;.
915b0 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
915c0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
915d0 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 fig.mutex.xMutex
915e0 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 53 51 Alloc(id);.}..SQ
915f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
91600 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 ite3_mutex *sqli
91610 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e te3MutexAlloc(in
91620 74 20 69 64 29 7b 0a 20 20 69 66 28 20 21 73 71 t id){. if( !sq
91630 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
91640 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a g.bCoreMutex ){.
91650 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
91660 7d 0a 20 20 61 73 73 65 72 74 28 20 47 4c 4f 42 }. assert( GLOB
91670 41 4c 28 69 6e 74 2c 20 6d 75 74 65 78 49 73 49 AL(int, mutexIsI
91680 6e 69 74 29 20 29 3b 0a 20 20 72 65 74 75 72 6e nit) );. return
91690 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
916a0 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 nfig.mutex.xMute
916b0 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 2f xAlloc(id);.}../
916c0 2a 0a 2a 2a 20 46 72 65 65 20 61 20 64 79 6e 61 *.** Free a dyna
916d0 6d 69 63 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 mic mutex..*/.SQ
916e0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
916f0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 lite3_mutex_free
91700 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
91710 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 p){. if( p ){.
91720 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sqlite3Global
91730 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 Config.mutex.xMu
91740 74 65 78 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a texFree(p);. }.
91750 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 }../*.** Obtain
91760 74 68 65 20 6d 75 74 65 78 20 70 2e 20 49 66 20 the mutex p. If
91770 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65 61 some other threa
91780 64 20 61 6c 72 65 61 64 79 20 68 61 73 20 74 68 d already has th
91790 65 20 6d 75 74 65 78 2c 20 62 6c 6f 63 6b 0a 2a e mutex, block.*
917a0 2a 20 75 6e 74 69 6c 20 69 74 20 63 61 6e 20 62 * until it can b
917b0 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f 0a 53 e obtained..*/.S
917c0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
917d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
917e0 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 er(sqlite3_mutex
917f0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b *p){. if( p ){
91800 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
91810 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
91820 4d 75 74 65 78 45 6e 74 65 72 28 70 29 3b 0a 20 MutexEnter(p);.
91830 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 }.}../*.** Obta
91840 69 6e 20 74 68 65 20 6d 75 74 65 78 20 70 2e 20 in the mutex p.
91850 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 If successful, r
91860 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e eturn SQLITE_OK.
91870 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 Otherwise, if a
91880 6e 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65 61 64 nother.** thread
91890 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 65 78 holds the mutex
918a0 20 61 6e 64 20 69 74 20 63 61 6e 6e 6f 74 20 62 and it cannot b
918b0 65 20 6f 62 74 61 69 6e 65 64 2c 20 72 65 74 75 e obtained, retu
918c0 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a rn SQLITE_BUSY..
918d0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
918e0 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f t sqlite3_mutex_
918f0 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 try(sqlite3_mute
91900 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 x *p){. int rc
91910 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
91920 66 28 20 70 20 29 7b 0a 20 20 20 20 72 65 74 75 f( p ){. retu
91930 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c rn sqlite3Global
91940 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 Config.mutex.xMu
91950 74 65 78 54 72 79 28 70 29 3b 0a 20 20 7d 0a 20 texTry(p);. }.
91960 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
91970 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
91980 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 _mutex_leave() r
91990 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d outine exits a m
919a0 75 74 65 78 20 74 68 61 74 20 77 61 73 20 70 72 utex that was pr
919b0 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 65 6e 74 65 eviously.** ente
919c0 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 red by the same
919d0 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 thread. The beh
919e0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
919f0 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 ed if the mutex
91a00 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 .** is not curre
91a10 6e 74 6c 79 20 65 6e 74 65 72 65 64 2e 20 49 66 ntly entered. If
91a20 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 a NULL pointer
91a30 69 73 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 is passed as an
91a40 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 68 69 73 argument.** this
91a50 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
91a60 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f o-op..*/.SQLITE_
91a70 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
91a80 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c _mutex_leave(sql
91a90 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
91aa0 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 if( p ){. s
91ab0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
91ac0 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 4c ig.mutex.xMutexL
91ad0 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a eave(p);. }.}..
91ae0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f #ifndef NDEBUG./
91af0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
91b00 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e _mutex_held() an
91b10 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
91b20 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e notheld() routin
91b30 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 e are.** intende
91b40 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 d for use inside
91b50 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
91b60 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ents..*/.SQLITE_
91b70 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
91b80 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 mutex_held(sqlit
91b90 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
91ba0 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 73 return p==0 || s
91bb0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
91bc0 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 48 ig.mutex.xMutexH
91bd0 65 6c 64 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 eld(p);.}.SQLITE
91be0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
91bf0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 73 _mutex_notheld(s
91c00 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
91c10 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 {. return p==0
91c20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c || sqlite3Global
91c30 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 Config.mutex.xMu
91c40 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 3b 0a 7d texNotheld(p);.}
91c50 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 .#endif..#endif
91c60 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f /* SQLITE_MUTEX_
91c70 4f 4d 49 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a OMIT */../******
91c80 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
91c90 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a mutex.c ********
91ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91cc0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
91cd0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
91ce0 69 6c 65 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 ile mutex_noop.c
91cf0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
91d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91d10 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
91d20 30 30 38 20 4f 63 74 6f 62 65 72 20 30 37 0a 2a 008 October 07.*
91d30 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
91d40 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
91d50 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
91d60 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
91d70 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
91d80 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
91d90 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
91da0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
91db0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
91dc0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
91dd0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
91de0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
91df0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
91e00 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
91e10 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
91e20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
91e30 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
91e40 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
91e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
91e90 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
91ea0 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 ins the C functi
91eb0 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ons that impleme
91ec0 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a nt mutexes..**.*
91ed0 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 * This implement
91ee0 61 74 69 6f 6e 20 69 6e 20 74 68 69 73 20 66 69 ation in this fi
91ef0 6c 65 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 le does not prov
91f00 69 64 65 20 61 6e 79 20 6d 75 74 75 61 6c 0a 2a ide any mutual.*
91f10 2a 20 65 78 63 6c 75 73 69 6f 6e 20 61 6e 64 20 * exclusion and
91f20 69 73 20 74 68 75 73 20 73 75 69 74 61 62 6c 65 is thus suitable
91f30 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e for use only in
91f40 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a applications.**
91f50 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 that use SQLite
91f60 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 74 68 72 in a single thr
91f70 65 61 64 2e 20 20 54 68 65 20 72 6f 75 74 69 6e ead. The routin
91f80 65 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 68 65 es defined.** he
91f90 72 65 20 61 72 65 20 70 6c 61 63 65 2d 68 6f 6c re are place-hol
91fa0 64 65 72 73 2e 20 20 41 70 70 6c 69 63 61 74 69 ders. Applicati
91fb0 6f 6e 73 20 63 61 6e 20 73 75 62 73 74 69 74 75 ons can substitu
91fc0 74 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 6d 75 te working.** mu
91fd0 74 65 78 20 72 6f 75 74 69 6e 65 73 20 61 74 20 tex routines at
91fe0 73 74 61 72 74 2d 74 69 6d 65 20 75 73 69 6e 67 start-time using
91ff0 20 74 68 65 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 the.**.** s
92000 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 qlite3_config(SQ
92010 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 LITE_CONFIG_MUTE
92020 58 2c 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 69 6e 74 X,...).**.** int
92030 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 erface..**.** If
92040 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 53 compiled with S
92050 51 4c 49 54 45 5f 44 45 42 55 47 2c 20 74 68 65 QLITE_DEBUG, the
92060 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 67 n additional log
92070 69 63 20 69 73 20 69 6e 73 65 72 74 65 64 0a 2a ic is inserted.*
92080 2a 20 74 68 61 74 20 64 6f 65 73 20 65 72 72 6f * that does erro
92090 72 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 6d 75 r checking on mu
920a0 74 65 78 65 73 20 74 6f 20 6d 61 6b 65 20 73 75 texes to make su
920b0 72 65 20 74 68 65 79 20 61 72 65 20 62 65 69 6e re they are bein
920c0 67 0a 2a 2a 20 63 61 6c 6c 65 64 20 63 6f 72 72 g.** called corr
920d0 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 ectly..**.** $Id
920e0 3a 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 2c 76 : mutex_noop.c,v
920f0 20 31 2e 33 20 32 30 30 38 2f 31 32 2f 30 35 20 1.3 2008/12/05
92100 31 37 3a 31 37 3a 30 38 20 64 72 68 20 45 78 70 17:17:08 drh Exp
92110 20 24 0a 2a 2f 0a 0a 0a 23 69 66 20 64 65 66 69 $.*/...#if defi
92120 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 ned(SQLITE_MUTEX
92130 5f 4e 4f 4f 50 29 20 26 26 20 21 64 65 66 69 6e _NOOP) && !defin
92140 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
92150 0a 2f 2a 0a 2a 2a 20 53 74 75 62 20 72 6f 75 74 ./*.** Stub rout
92160 69 6e 65 73 20 66 6f 72 20 61 6c 6c 20 6d 75 74 ines for all mut
92170 65 78 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 0a 2a ex methods..**.*
92180 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 73 20 * This routines
92190 70 72 6f 76 69 64 65 20 6e 6f 20 6d 75 74 75 61 provide no mutua
921a0 6c 20 65 78 63 6c 75 73 69 6f 6e 20 6f 72 20 65 l exclusion or e
921b0 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 2e 0a 2a rror checking..*
921c0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f /.static int noo
921d0 70 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 pMutexHeld(sqlit
921e0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 e3_mutex *p){ re
921f0 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 69 63 turn 1; }.static
92200 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 4e 6f int noopMutexNo
92210 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 theld(sqlite3_mu
92220 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 tex *p){ return
92230 31 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 1; }.static int
92240 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 28 76 6f noopMutexInit(vo
92250 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 id){ return SQLI
92260 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 TE_OK; }.static
92270 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 64 int noopMutexEnd
92280 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 (void){ return S
92290 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 QLITE_OK; }.stat
922a0 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ic sqlite3_mutex
922b0 20 2a 6e 6f 6f 70 4d 75 74 65 78 41 6c 6c 6f 63 *noopMutexAlloc
922c0 28 69 6e 74 20 69 64 29 7b 20 72 65 74 75 72 6e (int id){ return
922d0 20 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a (sqlite3_mutex*
922e0 29 38 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 )8; }.static voi
922f0 64 20 6e 6f 6f 70 4d 75 74 65 78 46 72 65 65 28 d noopMutexFree(
92300 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
92310 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 ){ return; }.sta
92320 74 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 tic void noopMut
92330 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f exEnter(sqlite3_
92340 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 mutex *p){ retur
92350 6e 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 n; }.static int
92360 6e 6f 6f 70 4d 75 74 65 78 54 72 79 28 73 71 6c noopMutexTry(sql
92370 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 ite3_mutex *p){
92380 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
92390 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ; }.static void
923a0 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 76 65 28 73 noopMutexLeave(s
923b0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
923c0 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 0a 53 51 4c { return; }..SQL
923d0 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 ITE_PRIVATE sqli
923e0 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
923f0 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c s *sqlite3Defaul
92400 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 tMutex(void){.
92410 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d static sqlite3_m
92420 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 utex_methods sMu
92430 74 65 78 20 3d 20 7b 0a 20 20 20 20 6e 6f 6f 70 tex = {. noop
92440 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 6e MutexInit,. n
92450 6f 6f 70 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 oopMutexEnd,.
92460 20 6e 6f 6f 70 4d 75 74 65 78 41 6c 6c 6f 63 2c noopMutexAlloc,
92470 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 46 72 . noopMutexFr
92480 65 65 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 ee,. noopMute
92490 78 45 6e 74 65 72 2c 0a 20 20 20 20 6e 6f 6f 70 xEnter,. noop
924a0 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 6e 6f MutexTry,. no
924b0 6f 70 4d 75 74 65 78 4c 65 61 76 65 2c 0a 0a 20 opMutexLeave,..
924c0 20 20 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64 noopMutexHeld
924d0 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4e ,. noopMutexN
924e0 6f 74 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72 otheld. };.. r
924f0 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d eturn &sMutex;.}
92500 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e .#endif /* defin
92510 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ed(SQLITE_MUTEX_
92520 4e 4f 4f 50 29 20 26 26 20 21 64 65 66 69 6e 65 NOOP) && !define
92530 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 d(SQLITE_DEBUG)
92540 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 */..#if defined(
92550 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f SQLITE_MUTEX_NOO
92560 50 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 P) && defined(SQ
92570 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a LITE_DEBUG)./*.*
92580 2a 20 49 6e 20 74 68 69 73 20 69 6d 70 6c 65 6d * In this implem
92590 65 6e 74 61 74 69 6f 6e 2c 20 65 72 72 6f 72 20 entation, error
925a0 63 68 65 63 6b 69 6e 67 20 69 73 20 70 72 6f 76 checking is prov
925b0 69 64 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 ided for testing
925c0 0a 2a 2a 20 61 6e 64 20 64 65 62 75 67 67 69 6e .** and debuggin
925d0 67 20 70 75 72 70 6f 73 65 73 2e 20 20 54 68 65 g purposes. The
925e0 20 6d 75 74 65 78 65 73 20 73 74 69 6c 6c 20 64 mutexes still d
925f0 6f 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 6e o not provide an
92600 79 0a 2a 2a 20 6d 75 74 75 61 6c 20 65 78 63 6c y.** mutual excl
92610 75 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a usion..*/../*.**
92620 20 54 68 65 20 6d 75 74 65 78 20 6f 62 6a 65 63 The mutex objec
92630 74 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 t.*/.struct sqli
92640 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 69 6e te3_mutex {. in
92650 74 20 69 64 3b 20 20 20 20 20 2f 2a 20 54 68 65 t id; /* The
92660 20 6d 75 74 65 78 20 74 79 70 65 20 2a 2f 0a 20 mutex type */.
92670 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 2f 2a 20 int cnt; /*
92680 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 Number of entrie
92690 73 20 77 69 74 68 6f 75 74 20 61 20 6d 61 74 63 s without a matc
926a0 68 69 6e 67 20 6c 65 61 76 65 20 2a 2f 0a 7d 3b hing leave */.};
926b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ../*.** The sqli
926c0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 te3_mutex_held()
926d0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 and sqlite3_mut
926e0 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 ex_notheld() rou
926f0 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 tine are.** inte
92700 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 nded for use ins
92710 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 ide assert() sta
92720 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 tements..*/.stat
92730 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 ic int debugMute
92740 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 xHeld(sqlite3_mu
92750 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 tex *p){. retur
92760 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 63 6e 74 n p==0 || p->cnt
92770 3e 30 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 >0;.}.static int
92780 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 debugMutexNothe
92790 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
927a0 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 *p){. return p
927b0 3d 3d 30 20 7c 7c 20 70 2d 3e 63 6e 74 3d 3d 30 ==0 || p->cnt==0
927c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 ;.}../*.** Initi
927d0 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 alize and deinit
927e0 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 ialize the mutex
927f0 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 subsystem..*/.s
92800 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d tatic int debugM
92810 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 utexInit(void){
92820 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
92830 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 ; }.static int d
92840 65 62 75 67 4d 75 74 65 78 45 6e 64 28 76 6f 69 ebugMutexEnd(voi
92850 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 d){ return SQLIT
92860 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK; }../*.** T
92870 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
92880 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 _alloc() routine
92890 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 allocates a new
928a0 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65 .** mutex and re
928b0 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
928c0 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72 65 to it. If it re
928d0 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 turns NULL.** th
928e0 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 at means that a
928f0 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 mutex could not
92900 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 0a 2a be allocated. .*
92910 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 /.static sqlite3
92920 5f 6d 75 74 65 78 20 2a 64 65 62 75 67 4d 75 74 _mutex *debugMut
92930 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b exAlloc(int id){
92940 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 . static sqlite
92950 33 5f 6d 75 74 65 78 20 61 53 74 61 74 69 63 5b 3_mutex aStatic[
92960 36 5d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 6];. sqlite3_mu
92970 74 65 78 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 tex *pNew = 0;.
92980 20 73 77 69 74 63 68 28 20 69 64 20 29 7b 0a 20 switch( id ){.
92990 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d case SQLITE_M
929a0 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 UTEX_FAST:. c
929b0 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ase SQLITE_MUTEX
929c0 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 _RECURSIVE: {.
929d0 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 pNew = sqlit
929e0 65 33 4d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 e3Malloc(sizeof(
929f0 2a 70 4e 65 77 29 29 3b 0a 20 20 20 20 20 20 69 *pNew));. i
92a00 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 f( pNew ){.
92a10 20 20 20 70 4e 65 77 2d 3e 69 64 20 3d 20 69 64 pNew->id = id
92a20 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e ;. pNew->
92a30 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d cnt = 0;. }
92a40 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
92a50 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a }. default:
92a60 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 {. assert(
92a70 20 69 64 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 id-2 >= 0 );.
92a80 20 20 20 20 61 73 73 65 72 74 28 20 69 64 2d 32 assert( id-2
92a90 20 3c 20 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 < (int)(sizeof(
92aa0 61 53 74 61 74 69 63 29 2f 73 69 7a 65 6f 66 28 aStatic)/sizeof(
92ab0 61 53 74 61 74 69 63 5b 30 5d 29 29 20 29 3b 0a aStatic[0])) );.
92ac0 20 20 20 20 20 20 70 4e 65 77 20 3d 20 26 61 53 pNew = &aS
92ad0 74 61 74 69 63 5b 69 64 2d 32 5d 3b 0a 20 20 20 tatic[id-2];.
92ae0 20 20 20 70 4e 65 77 2d 3e 69 64 20 3d 20 69 64 pNew->id = id
92af0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
92b00 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
92b10 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n pNew;.}../*.**
92b20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 This routine de
92b30 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 allocates a prev
92b40 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 iously allocated
92b50 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 mutex..*/.stati
92b60 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65 c void debugMute
92b70 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 xFree(sqlite3_mu
92b80 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 tex *p){. asser
92b90 74 28 20 70 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a t( p->cnt==0 );.
92ba0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d assert( p->id=
92bb0 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 =SQLITE_MUTEX_FA
92bc0 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c ST || p->id==SQL
92bd0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
92be0 49 56 45 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 IVE );. sqlite3
92bf0 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a _free(p);.}../*.
92c00 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d ** The sqlite3_m
92c10 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 utex_enter() and
92c20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 sqlite3_mutex_t
92c30 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 ry() routines at
92c40 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 tempt.** to ente
92c50 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 r a mutex. If a
92c60 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 nother thread is
92c70 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 already within
92c80 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 the mutex,.** sq
92c90 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
92ca0 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 r() will block a
92cb0 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 nd sqlite3_mutex
92cc0 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 _try() will retu
92cd0 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 rn.** SQLITE_BUS
92ce0 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f Y. The sqlite3_
92cf0 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 mutex_try() inte
92d00 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 rface returns SQ
92d10 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 LITE_OK.** upon
92d20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 successful entry
92d30 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 . Mutexes creat
92d40 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f ed using SQLITE_
92d50 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 MUTEX_RECURSIVE
92d60 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 can.** be entere
92d70 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 d multiple times
92d80 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 by the same thr
92d90 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 ead. In such ca
92da0 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 ses the,.** mute
92db0 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 x must be exited
92dc0 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 an equal number
92dd0 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 of times before
92de0 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a another thread.
92df0 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 ** can enter. I
92e00 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 f the same threa
92e10 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 d tries to enter
92e20 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 any other kind
92e30 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 of mutex.** more
92e40 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 than once, the
92e50 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 behavior is unde
92e60 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 fined..*/.static
92e70 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65 78 void debugMutex
92e80 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 Enter(sqlite3_mu
92e90 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 tex *p){. asser
92ea0 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 t( p->id==SQLITE
92eb0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
92ec0 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 4e 6f || debugMutexNo
92ed0 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d theld(p) );. p-
92ee0 3e 63 6e 74 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 >cnt++;.}.static
92ef0 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 54 int debugMutexT
92f00 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ry(sqlite3_mutex
92f10 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
92f20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
92f30 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c TEX_RECURSIVE ||
92f40 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 debugMutexNothe
92f50 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e ld(p) );. p->cn
92f60 74 2b 2b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 t++;. return SQ
92f70 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
92f80 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
92f90 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 tex_leave() rout
92fa0 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 ine exits a mute
92fb0 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 x that was.** pr
92fc0 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 eviously entered
92fd0 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 by the same thr
92fe0 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 ead. The behavi
92ff0 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e or.** is undefin
93000 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 ed if the mutex
93010 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 is not currently
93020 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 entered or.** i
93030 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 s not currently
93040 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 allocated. SQLi
93050 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f te will never do
93060 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 either..*/.stat
93070 69 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 ic void debugMut
93080 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f exLeave(sqlite3_
93090 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 mutex *p){. ass
930a0 65 72 74 28 20 64 65 62 75 67 4d 75 74 65 78 48 ert( debugMutexH
930b0 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63 eld(p) );. p->c
930c0 6e 74 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 nt--;. assert(
930d0 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
930e0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c TEX_RECURSIVE ||
930f0 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 debugMutexNothe
93100 6c 64 28 70 29 20 29 3b 0a 7d 0a 0a 53 51 4c 49 ld(p) );.}..SQLI
93110 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 TE_PRIVATE sqlit
93120 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
93130 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 *sqlite3Default
93140 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 Mutex(void){. s
93150 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 tatic sqlite3_mu
93160 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 tex_methods sMut
93170 65 78 20 3d 20 7b 0a 20 20 20 20 64 65 62 75 67 ex = {. debug
93180 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 64 MutexInit,. d
93190 65 62 75 67 4d 75 74 65 78 45 6e 64 2c 0a 20 20 ebugMutexEnd,.
931a0 20 20 64 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f debugMutexAllo
931b0 63 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 c,. debugMute
931c0 78 46 72 65 65 2c 0a 20 20 20 20 64 65 62 75 67 xFree,. debug
931d0 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 MutexEnter,.
931e0 64 65 62 75 67 4d 75 74 65 78 54 72 79 2c 0a 20 debugMutexTry,.
931f0 20 20 20 64 65 62 75 67 4d 75 74 65 78 4c 65 61 debugMutexLea
93200 76 65 2c 0a 0a 20 20 20 20 64 65 62 75 67 4d 75 ve,.. debugMu
93210 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 64 65 62 texHeld,. deb
93220 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 ugMutexNotheld.
93230 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 };.. return &s
93240 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 Mutex;.}.#endif
93250 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 /* defined(SQLIT
93260 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 E_MUTEX_NOOP) &&
93270 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
93280 44 45 42 55 47 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a DEBUG) */../****
93290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
932a0 66 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 20 2a f mutex_noop.c *
932b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
932c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
932d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
932e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
932f0 20 66 69 6c 65 20 6d 75 74 65 78 5f 6f 73 32 2e file mutex_os2.
93300 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
93310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
93330 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a 2007 August 28.
93340 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
93350 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
93360 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
93370 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
93380 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
93390 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
933a0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
933b0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
933c0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
933d0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
933e0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
933f0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
93400 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
93410 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
93420 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
93430 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
93440 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
93450 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
93460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
934a0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
934b0 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 ains the C funct
934c0 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d ions that implem
934d0 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 ent mutexes for
934e0 4f 53 2f 32 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 OS/2.**.** $Id:
934f0 6d 75 74 65 78 5f 6f 73 32 2e 63 2c 76 20 31 2e mutex_os2.c,v 1.
93500 31 31 20 32 30 30 38 2f 31 31 2f 32 32 20 31 39 11 2008/11/22 19
93510 3a 35 30 3a 35 34 20 70 77 65 69 6c 62 61 63 68 :50:54 pweilbach
93520 65 72 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a er Exp $.*/../*.
93530 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 ** The code in t
93540 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 his file is only
93550 20 75 73 65 64 20 69 66 20 53 51 4c 49 54 45 5f used if SQLITE_
93560 4d 55 54 45 58 5f 4f 53 32 20 69 73 20 64 65 66 MUTEX_OS2 is def
93570 69 6e 65 64 2e 0a 2a 2a 20 53 65 65 20 74 68 65 ined..** See the
93580 20 6d 75 74 65 78 2e 68 20 66 69 6c 65 20 66 6f mutex.h file fo
93590 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 69 r details..*/.#i
935a0 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 fdef SQLITE_MUTE
935b0 58 5f 4f 53 32 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a X_OS2../********
935c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4f ************** O
935d0 53 2f 32 20 4d 75 74 65 78 20 49 6d 70 6c 65 6d S/2 Mutex Implem
935e0 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a entation *******
935f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
93600 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 **.** This imple
93610 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 75 74 mentation of mut
93620 65 78 65 73 20 69 73 20 62 75 69 6c 74 20 75 73 exes is built us
93630 69 6e 67 20 74 68 65 20 4f 53 2f 32 20 41 50 49 ing the OS/2 API
93640 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ..*/../*.** The
93650 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a 2a 20 mutex object.**
93660 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d Each recursive m
93670 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 utex is an insta
93680 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
93690 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a wing structure..
936a0 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 */.struct sqlite
936b0 33 5f 6d 75 74 65 78 20 7b 0a 20 20 48 4d 54 58 3_mutex {. HMTX
936c0 20 6d 75 74 65 78 3b 20 20 20 20 20 20 20 2f 2a mutex; /*
936d0 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 Mutex controlli
936e0 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 ng the lock */.
936f0 20 69 6e 74 20 20 69 64 3b 20 20 20 20 20 20 20 int id;
93700 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 /* Mutex type
93710 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 52 65 66 3b */. int nRef;
93720 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
93730 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 r of references
93740 2a 2f 0a 20 20 54 49 44 20 20 6f 77 6e 65 72 3b */. TID owner;
93750 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61 64 /* Thread
93760 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6d 75 holding this mu
93770 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 tex */.};..#defi
93780 6e 65 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 ne OS2_MUTEX_INI
93790 54 49 41 4c 49 5a 45 52 20 20 20 30 2c 30 2c 30 TIALIZER 0,0,0
937a0 2c 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 ,0../*.** Initia
937b0 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 lize and deiniti
937c0 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 alize the mutex
937d0 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 subsystem..*/.st
937e0 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 atic int os2Mute
937f0 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 xInit(void){ ret
93800 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d urn SQLITE_OK; }
93810 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d .static int os2M
93820 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 utexEnd(void){ r
93830 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
93840 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 }../*.** The sq
93850 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
93860 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f c() routine allo
93870 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d cates a new.** m
93880 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 utex and returns
93890 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 a pointer to it
938a0 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 . If it returns
938b0 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 NULL.** that me
938c0 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 ans that a mutex
938d0 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c could not be al
938e0 6c 6f 63 61 74 65 64 2e 20 0a 2a 2a 20 53 51 4c located. .** SQL
938f0 69 74 65 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 ite will unwind
93900 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65 its stack and re
93910 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 turn an error.
93920 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 The argument.**
93930 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 to sqlite3_mutex
93940 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 _alloc() is one
93950 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 of these integer
93960 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a constants:.**.*
93970 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 * <ul>.** <li>
93980 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
93990 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
939a0 30 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 0.** <li> SQLIT
939b0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
939c0 45 20 20 20 20 20 20 20 20 20 20 31 0a 2a 2a 20 E 1.**
939d0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
939e0 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
939f0 20 20 20 20 20 20 32 0a 2a 2a 20 3c 6c 69 3e 20 2.** <li>
93a00 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
93a10 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 20 ATIC_MEM
93a20 20 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 3.** <li> SQLI
93a30 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
93a40 50 52 4e 47 20 20 20 20 20 20 20 20 34 0a 2a 2a PRNG 4.**
93a50 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 </ul>.**.** The
93a60 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 first two const
93a70 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 ants cause sqlit
93a80 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
93a90 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 to create.** a
93aa0 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 new mutex. The
93ab0 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 new mutex is rec
93ac0 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 ursive when SQLI
93ad0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
93ae0 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 VE.** is used bu
93af0 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c t not necessaril
93b00 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 y so when SQLITE
93b10 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 _MUTEX_FAST is u
93b20 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 sed..** The mute
93b30 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e x implementation
93b40 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
93b50 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 o make a distinc
93b60 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 tion.** between
93b70 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
93b80 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 URSIVE and SQLIT
93b90 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 E_MUTEX_FAST if
93ba0 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 it does.** not w
93bb0 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c ant to. But SQL
93bc0 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 ite will only re
93bd0 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76 quest a recursiv
93be0 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 e mutex in.** ca
93bf0 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61 ses where it rea
93c00 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 lly needs one.
93c10 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d If a faster non-
93c20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a recursive mutex.
93c30 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
93c40 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f n is available o
93c50 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 n the host platf
93c60 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 orm, the mutex s
93c70 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 ubsystem.** migh
93c80 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 t return such a
93c90 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 mutex in respons
93ca0 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 e to SQLITE_MUTE
93cb0 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 X_FAST..**.** Th
93cc0 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 e other allowed
93cd0 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 parameters to sq
93ce0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
93cf0 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a c() each return.
93d00 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** a pointer to
93d10 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73 a static preexis
93d20 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68 72 ting mutex. Thr
93d30 65 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 ee static mutexe
93d40 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 s are.** used by
93d50 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 the current ver
93d60 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 sion of SQLite.
93d70 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 Future versions
93d80 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 of SQLite.** ma
93d90 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c y add additional
93da0 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e static mutexes.
93db0 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 Static mutexes
93dc0 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 are for interna
93dd0 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 l.** use by SQLi
93de0 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 te only. Applic
93df0 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 ations that use
93e00 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 SQLite mutexes s
93e10 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c hould.** use onl
93e20 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 y the dynamic mu
93e30 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 texes returned b
93e40 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 y SQLITE_MUTEX_F
93e50 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 AST or.** SQLITE
93e60 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
93e70 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
93e80 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 t if one of the
93e90 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 dynamic mutex pa
93ea0 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 rameters (SQLITE
93eb0 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f _MUTEX_FAST.** o
93ec0 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 r SQLITE_MUTEX_R
93ed0 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 ECURSIVE) is use
93ee0 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d d then sqlite3_m
93ef0 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 utex_alloc().**
93f00 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 returns a differ
93f10 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 ent mutex on eve
93f20 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f ry call. But fo
93f30 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20 r the static.**
93f40 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 mutex types, the
93f50 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 same mutex is r
93f60 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 eturned on every
93f70 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a call that has.*
93f80 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 * the same type
93f90 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 number..*/.stati
93fa0 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 c sqlite3_mutex
93fb0 2a 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63 28 69 *os2MutexAlloc(i
93fc0 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 71 6c nt iType){. sql
93fd0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 20 3d 20 ite3_mutex *p =
93fe0 4e 55 4c 4c 3b 0a 20 20 73 77 69 74 63 68 28 20 NULL;. switch(
93ff0 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 iType ){. cas
94000 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 e SQLITE_MUTEX_F
94010 41 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 AST:. case SQ
94020 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
94030 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 20 SIVE: {. p
94040 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a = sqlite3MallocZ
94050 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 ero( sizeof(*p)
94060 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 29 );. if( p )
94070 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 {. p->id
94080 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 20 = iType;.
94090 20 69 66 28 20 44 6f 73 43 72 65 61 74 65 4d 75 if( DosCreateMu
940a0 74 65 78 53 65 6d 28 20 30 2c 20 26 70 2d 3e 6d texSem( 0, &p->m
940b0 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 20 29 utex, 0, FALSE )
940c0 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a != NO_ERROR ){.
940d0 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
940e0 33 5f 66 72 65 65 28 20 70 20 29 3b 0a 20 20 20 3_free( p );.
940f0 20 20 20 20 20 20 20 70 20 3d 20 4e 55 4c 4c 3b p = NULL;
94100 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
94110 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
94120 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
94130 74 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 74 69 t: {. stati
94140 63 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69 c volatile int i
94150 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 sInit = 0;.
94160 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f static sqlite3_
94170 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65 mutex staticMute
94180 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 xes[] = {.
94190 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e { OS2_MUTEX_IN
941a0 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 ITIALIZER, },.
941b0 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 { OS2_MUTE
941c0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d X_INITIALIZER, }
941d0 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f ,. { OS2_
941e0 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 MUTEX_INITIALIZE
941f0 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 R, },. {
94200 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 OS2_MUTEX_INITIA
94210 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 LIZER, },.
94220 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e { OS2_MUTEX_IN
94230 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 ITIALIZER, },.
94240 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 { OS2_MUTE
94250 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d X_INITIALIZER, }
94260 2c 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 ,. };.
94270 20 69 66 20 28 20 21 69 73 49 6e 69 74 20 29 7b if ( !isInit ){
94280 0a 20 20 20 20 20 20 20 20 41 50 49 52 45 54 20 . APIRET
94290 72 63 3b 0a 20 20 20 20 20 20 20 20 50 54 49 42 rc;. PTIB
942a0 20 70 74 69 62 3b 0a 20 20 20 20 20 20 20 20 50 ptib;. P
942b0 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 20 20 PIB ppib;.
942c0 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 0a 20 20 HMTX mutex;.
942d0 20 20 20 20 20 20 63 68 61 72 20 6e 61 6d 65 5b char name[
942e0 33 32 5d 3b 0a 20 20 20 20 20 20 20 20 44 6f 73 32];. Dos
942f0 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 20 26 GetInfoBlocks( &
94300 70 74 69 62 2c 20 26 70 70 69 62 20 29 3b 0a 20 ptib, &ppib );.
94310 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 sqlite3_s
94320 6e 70 72 69 6e 74 66 28 20 73 69 7a 65 6f 66 28 nprintf( sizeof(
94330 6e 61 6d 65 29 2c 20 6e 61 6d 65 2c 20 22 5c 5c name), name, "\\
94340 53 45 4d 33 32 5c 5c 53 51 4c 49 54 45 25 30 34 SEM32\\SQLITE%04
94350 78 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 x",.
94360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 70 pp
94370 69 62 2d 3e 70 69 62 5f 75 6c 70 69 64 20 29 3b ib->pib_ulpid );
94380 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 . while(
94390 21 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 !isInit ){.
943a0 20 20 20 20 20 6d 75 74 65 78 20 3d 20 30 3b 0a mutex = 0;.
943b0 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 44 rc = D
943c0 6f 73 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d osCreateMutexSem
943d0 28 20 6e 61 6d 65 2c 20 26 6d 75 74 65 78 2c 20 ( name, &mutex,
943e0 30 2c 20 46 41 4c 53 45 29 3b 0a 20 20 20 20 20 0, FALSE);.
943f0 20 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20 4e if( rc == N
94400 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 O_ERROR ){.
94410 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 unsigned
94420 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 20 int i;.
94430 20 20 20 69 66 28 20 21 69 73 49 6e 69 74 20 29 if( !isInit )
94440 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
94450 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20 3c 20 for( i = 0; i <
94460 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 sizeof(staticMut
94470 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 exes)/sizeof(sta
94480 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 3b 20 ticMutexes[0]);
94490 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 i++ ){.
944a0 20 20 20 20 20 20 20 44 6f 73 43 72 65 61 74 65 DosCreate
944b0 4d 75 74 65 78 53 65 6d 28 20 30 2c 20 26 73 74 MutexSem( 0, &st
944c0 61 74 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d aticMutexes[i].m
944d0 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 20 29 utex, 0, FALSE )
944e0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
944f0 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }.
94500 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 isInit = 1;.
94510 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
94520 20 20 20 20 20 20 44 6f 73 43 6c 6f 73 65 4d 75 DosCloseMu
94530 74 65 78 53 65 6d 28 20 6d 75 74 65 78 20 29 3b texSem( mutex );
94540 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
94550 20 69 66 28 20 72 63 20 3d 3d 20 45 52 52 4f 52 if( rc == ERROR
94560 5f 44 55 50 4c 49 43 41 54 45 5f 4e 41 4d 45 20 _DUPLICATE_NAME
94570 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 44 ){. D
94580 6f 73 53 6c 65 65 70 28 20 31 20 29 3b 0a 20 20 osSleep( 1 );.
94590 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
945a0 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 retur
945b0 6e 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d n p;. }
945c0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
945d0 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 }. assert(
945e0 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b iType-2 >= 0 );
945f0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 . assert( i
94600 54 79 70 65 2d 32 20 3c 20 73 69 7a 65 6f 66 28 Type-2 < sizeof(
94610 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f 73 staticMutexes)/s
94620 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 izeof(staticMute
94630 78 65 73 5b 30 5d 29 20 29 3b 0a 20 20 20 20 20 xes[0]) );.
94640 20 70 20 3d 20 26 73 74 61 74 69 63 4d 75 74 65 p = &staticMute
94650 78 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 xes[iType-2];.
94660 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 p->id = iTyp
94670 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a e;. break;.
94680 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
94690 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 rn p;.}.../*.**
946a0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61 This routine dea
946b0 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 69 llocates a previ
946c0 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 ously allocated
946d0 6d 75 74 65 78 2e 0a 2a 2a 20 53 51 4c 69 74 65 mutex..** SQLite
946e0 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64 is careful to d
946f0 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 20 eallocate every
94700 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 61 6c mutex that it al
94710 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 locates..*/.stat
94720 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 65 78 ic void os2Mutex
94730 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 Free(sqlite3_mut
94740 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d ex *p){. if( p=
94750 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 =0 ) return;. a
94760 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d ssert( p->nRef==
94770 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
94780 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 ->id==SQLITE_MUT
94790 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 EX_FAST || p->id
947a0 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
947b0 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 44 6f ECURSIVE );. Do
947c0 73 43 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 sCloseMutexSem(
947d0 70 2d 3e 6d 75 74 65 78 20 29 3b 0a 20 20 73 71 p->mutex );. sq
947e0 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20 29 3b lite3_free( p );
947f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 .}../*.** The sq
94800 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
94810 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f r() and sqlite3_
94820 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 mutex_try() rout
94830 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 ines attempt.**
94840 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 to enter a mutex
94850 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 . If another th
94860 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 read is already
94870 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 within the mutex
94880 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 ,.** sqlite3_mut
94890 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 ex_enter() will
948a0 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 block and sqlite
948b0 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 3_mutex_try() wi
948c0 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c ll return.** SQL
948d0 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 ITE_BUSY. The s
948e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
948f0 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 () interface ret
94900 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a urns SQLITE_OK.*
94910 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 * upon successfu
94920 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 l entry. Mutexe
94930 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 s created using
94940 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
94950 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 URSIVE can.** be
94960 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c entered multipl
94970 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 e times by the s
94980 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 ame thread. In
94990 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a such cases the,.
949a0 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 ** mutex must be
949b0 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c exited an equal
949c0 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 number of times
949d0 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 before another
949e0 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e thread.** can en
949f0 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d ter. If the sam
94a00 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 e thread tries t
94a10 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 o enter any othe
94a20 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a r kind of mutex.
94a30 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 ** more than onc
94a40 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 e, the behavior
94a50 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f is undefined..*/
94a60 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 .static void os2
94a70 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 MutexEnter(sqlit
94a80 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
94a90 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68 TID tid;. PID h
94aa0 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 older1;. ULONG
94ab0 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70 holder2;. if( p
94ac0 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
94ad0 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 assert( p->id==S
94ae0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
94af0 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d 75 74 65 RSIVE || os2Mute
94b00 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 xNotheld(p) );.
94b10 20 44 6f 73 52 65 71 75 65 73 74 4d 75 74 65 78 DosRequestMutex
94b20 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 53 45 Sem(p->mutex, SE
94b30 4d 5f 49 4e 44 45 46 49 4e 49 54 45 5f 57 41 49 M_INDEFINITE_WAI
94b40 54 29 3b 0a 20 20 44 6f 73 51 75 65 72 79 4d 75 T);. DosQueryMu
94b50 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c texSem(p->mutex,
94b60 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 64 2c &holder1, &tid,
94b70 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 70 2d &holder2);. p-
94b80 3e 6f 77 6e 65 72 20 3d 20 74 69 64 3b 0a 20 20 >owner = tid;.
94b90 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 p->nRef++;.}.sta
94ba0 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 78 tic int os2Mutex
94bb0 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 Try(sqlite3_mute
94bc0 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b x *p){. int rc;
94bd0 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49 . TID tid;. PI
94be0 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f D holder1;. ULO
94bf0 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 NG holder2;. if
94c00 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( p==0 ) return
94c10 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 SQLITE_OK;. ass
94c20 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 ert( p->id==SQLI
94c30 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
94c40 56 45 20 7c 7c 20 6f 73 32 4d 75 74 65 78 4e 6f VE || os2MutexNo
94c50 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 69 66 theld(p) );. if
94c60 28 20 44 6f 73 52 65 71 75 65 73 74 4d 75 74 65 ( DosRequestMute
94c70 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 53 xSem(p->mutex, S
94c80 45 4d 5f 49 4d 4d 45 44 49 41 54 45 5f 52 45 54 EM_IMMEDIATE_RET
94c90 55 52 4e 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 URN) == NO_ERROR
94ca0 29 20 7b 0a 20 20 20 20 44 6f 73 51 75 65 72 79 ) {. DosQuery
94cb0 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 MutexSem(p->mute
94cc0 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 x, &holder1, &ti
94cd0 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 d, &holder2);.
94ce0 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74 69 64 p->owner = tid
94cf0 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b ;. p->nRef++;
94d00 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
94d10 5f 4f 4b 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a _OK;. } else {.
94d20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
94d30 42 55 53 59 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 BUSY;. }.. ret
94d40 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
94d50 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
94d60 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 ex_leave() routi
94d70 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 ne exits a mutex
94d80 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 that was.** pre
94d90 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 viously entered
94da0 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 by the same thre
94db0 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f ad. The behavio
94dc0 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 r.** is undefine
94dd0 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 d if the mutex i
94de0 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 s not currently
94df0 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 entered or.** is
94e00 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 not currently a
94e10 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 llocated. SQLit
94e20 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 e will never do
94e30 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 either..*/.stati
94e40 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 65 78 4c c void os2MutexL
94e50 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 eave(sqlite3_mut
94e60 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 ex *p){. TID ti
94e70 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 d;. PID holder1
94e80 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 ;. ULONG holder
94e90 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 2;. if( p==0 )
94ea0 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 return;. assert
94eb0 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 ( p->nRef>0 );.
94ec0 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65 DosQueryMutexSe
94ed0 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c m(p->mutex, &hol
94ee0 64 65 72 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c der1, &tid, &hol
94ef0 64 65 72 32 29 3b 0a 20 20 61 73 73 65 72 74 28 der2);. assert(
94f00 20 70 2d 3e 6f 77 6e 65 72 3d 3d 74 69 64 20 29 p->owner==tid )
94f10 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 ;. p->nRef--;.
94f20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 assert( p->nRef
94f30 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 ==0 || p->id==SQ
94f40 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
94f50 53 49 56 45 20 29 3b 0a 20 20 44 6f 73 52 65 6c SIVE );. DosRel
94f60 65 61 73 65 4d 75 74 65 78 53 65 6d 28 70 2d 3e easeMutexSem(p->
94f70 6d 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 64 65 mutex);.}..#ifde
94f80 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f f SQLITE_DEBUG./
94f90 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
94fa0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e _mutex_held() an
94fb0 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
94fc0 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e notheld() routin
94fd0 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 e are.** intende
94fe0 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 d for use inside
94ff0 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
95000 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ents..*/.static
95010 69 6e 74 20 6f 73 32 4d 75 74 65 78 48 65 6c 64 int os2MutexHeld
95020 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
95030 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 p){. TID tid;.
95040 20 50 49 44 20 70 69 64 3b 0a 20 20 55 4c 4f 4e PID pid;. ULON
95050 47 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50 54 49 G ulCount;. PTI
95060 42 20 70 74 69 62 3b 0a 20 20 69 66 28 20 70 21 B ptib;. if( p!
95070 3d 30 20 29 20 7b 0a 20 20 20 20 44 6f 73 51 75 =0 ) {. DosQu
95080 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d eryMutexSem(p->m
95090 75 74 65 78 2c 20 26 70 69 64 2c 20 26 74 69 64 utex, &pid, &tid
950a0 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0a 20 20 7d , &ulCount);. }
950b0 20 65 6c 73 65 20 7b 0a 20 20 20 20 44 6f 73 47 else {. DosG
950c0 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26 70 74 etInfoBlocks(&pt
950d0 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 74 ib, NULL);. t
950e0 69 64 20 3d 20 70 74 69 62 2d 3e 74 69 62 5f 70 id = ptib->tib_p
950f0 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 69 64 tib2->tib2_ultid
95100 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
95110 3d 3d 30 20 7c 7c 20 28 70 2d 3e 6e 52 65 66 21 ==0 || (p->nRef!
95120 3d 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d =0 && p->owner==
95130 74 69 64 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 tid);.}.static i
95140 6e 74 20 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 nt os2MutexNothe
95150 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
95160 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b *p){. TID tid;
95170 0a 20 20 50 49 44 20 70 69 64 3b 0a 20 20 55 4c . PID pid;. UL
95180 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50 ONG ulCount;. P
95190 54 49 42 20 70 74 69 62 3b 0a 20 20 69 66 28 20 TIB ptib;. if(
951a0 70 21 3d 20 30 20 29 20 7b 0a 20 20 20 20 44 6f p!= 0 ) {. Do
951b0 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 sQueryMutexSem(p
951c0 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64 2c 20 26 ->mutex, &pid, &
951d0 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0a tid, &ulCount);.
951e0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 44 } else {. D
951f0 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 osGetInfoBlocks(
95200 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 &ptib, NULL);.
95210 20 20 74 69 64 20 3d 20 70 74 69 62 2d 3e 74 69 tid = ptib->ti
95220 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c b_ptib2->tib2_ul
95230 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 tid;. }. retur
95240 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 65 n p==0 || p->nRe
95250 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72 f==0 || p->owner
95260 21 3d 74 69 64 3b 0a 7d 0a 23 65 6e 64 69 66 0a !=tid;.}.#endif.
95270 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
95280 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
95290 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 thods *sqlite3De
952a0 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 faultMutex(void)
952b0 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 {. static sqlit
952c0 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
952d0 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 sMutex = {.
952e0 6f 73 32 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 os2MutexInit,.
952f0 20 20 6f 73 32 4d 75 74 65 78 45 6e 64 2c 0a 20 os2MutexEnd,.
95300 20 20 20 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63 os2MutexAlloc
95310 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 46 72 ,. os2MutexFr
95320 65 65 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 ee,. os2Mutex
95330 45 6e 74 65 72 2c 0a 20 20 20 20 6f 73 32 4d 75 Enter,. os2Mu
95340 74 65 78 54 72 79 2c 0a 20 20 20 20 6f 73 32 4d texTry,. os2M
95350 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 66 64 65 utexLeave,.#ifde
95360 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
95370 20 20 20 6f 73 32 4d 75 74 65 78 48 65 6c 64 2c os2MutexHeld,
95380 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 4e 6f 74 . os2MutexNot
95390 68 65 6c 64 0a 23 65 6e 64 69 66 0a 20 20 7d 3b held.#endif. };
953a0 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74 .. return &sMut
953b0 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ex;.}.#endif /*
953c0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 SQLITE_MUTEX_OS2
953d0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
953e0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 **** End of mute
953f0 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a x_os2.c ********
95400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95420 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
95430 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
95440 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a 2a 2a mutex_unix.c ***
95450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95470 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 ***/./*.** 2007
95480 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 August 28.**.**
95490 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
954a0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
954b0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
954c0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
954d0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
954e0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
954f0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
95500 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
95510 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
95520 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
95530 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
95540 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
95550 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
95560 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
95570 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
95580 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
95590 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
955a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
955b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
955c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
955d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
955e0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
955f0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 file contains t
95600 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 he C functions t
95610 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 hat implement mu
95620 74 65 78 65 73 20 66 6f 72 20 70 74 68 72 65 61 texes for pthrea
95630 64 73 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 ds.**.** $Id: mu
95640 74 65 78 5f 75 6e 69 78 2e 63 2c 76 20 31 2e 31 tex_unix.c,v 1.1
95650 36 20 32 30 30 38 2f 31 32 2f 30 38 20 31 38 3a 6 2008/12/08 18:
95660 31 39 3a 31 38 20 64 72 68 20 45 78 70 20 24 0a 19:18 drh Exp $.
95670 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f */../*.** The co
95680 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 de in this file
95690 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 is only used if
956a0 77 65 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 we are compiling
956b0 20 74 68 72 65 61 64 73 61 66 65 0a 2a 2a 20 75 threadsafe.** u
956c0 6e 64 65 72 20 75 6e 69 78 20 77 69 74 68 20 70 nder unix with p
956d0 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e threads..**.** N
956e0 6f 74 65 20 74 68 61 74 20 74 68 69 73 20 69 6d ote that this im
956f0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72 65 71 plementation req
95700 75 69 72 65 73 20 61 20 76 65 72 73 69 6f 6e 20 uires a version
95710 6f 66 20 70 74 68 72 65 61 64 73 20 74 68 61 74 of pthreads that
95720 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 72 65 63 .** supports rec
95730 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 2e 0a ursive mutexes..
95740 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
95750 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 53 0a _MUTEX_PTHREADS.
95760 0a 23 69 6e 63 6c 75 64 65 20 3c 70 74 68 72 65 .#include <pthre
95770 61 64 2e 68 3e 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 ad.h>.../*.** Ea
95780 63 68 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 ch recursive mut
95790 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 ex is an instanc
957a0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
957b0 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f ng structure..*/
957c0 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
957d0 6d 75 74 65 78 20 7b 0a 20 20 70 74 68 72 65 61 mutex {. pthrea
957e0 64 5f 6d 75 74 65 78 5f 74 20 6d 75 74 65 78 3b d_mutex_t mutex;
957f0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f /* Mutex co
95800 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f ntrolling the lo
95810 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64 3b 20 ck */. int id;
95820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
95830 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 /* Mutex type
95840 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 */. int nRef;
95850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
95860 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
95870 74 72 61 6e 63 65 73 20 2a 2f 0a 20 20 70 74 68 trances */. pth
95880 72 65 61 64 5f 74 20 6f 77 6e 65 72 3b 20 20 20 read_t owner;
95890 20 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61 /* Threa
958a0 64 20 74 68 61 74 20 69 73 20 77 69 74 68 69 6e d that is within
958b0 20 74 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 23 this mutex */.#
958c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
958d0 55 47 0a 20 20 69 6e 74 20 74 72 61 63 65 3b 20 UG. int trace;
958e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
958f0 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61 63 65 /* True to trace
95900 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 65 6e 64 changes */.#end
95910 69 66 0a 7d 3b 0a 23 69 66 64 65 66 20 53 51 4c if.};.#ifdef SQL
95920 49 54 45 5f 44 45 42 55 47 0a 23 64 65 66 69 6e ITE_DEBUG.#defin
95930 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f e SQLITE3_MUTEX_
95940 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 50 54 INITIALIZER { PT
95950 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 HREAD_MUTEX_INIT
95960 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28 IALIZER, 0, 0, (
95970 70 74 68 72 65 61 64 5f 74 29 30 2c 20 30 20 7d pthread_t)0, 0 }
95980 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 .#else.#define S
95990 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 QLITE3_MUTEX_INI
959a0 54 49 41 4c 49 5a 45 52 20 7b 20 50 54 48 52 45 TIALIZER { PTHRE
959b0 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c AD_MUTEX_INITIAL
959c0 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28 70 74 68 IZER, 0, 0, (pth
959d0 72 65 61 64 5f 74 29 30 20 7d 0a 23 65 6e 64 69 read_t)0 }.#endi
959e0 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c f../*.** The sql
959f0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
95a00 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 ) and sqlite3_mu
95a10 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f tex_notheld() ro
95a20 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 utine are.** int
95a30 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 6f 6e ended for use on
95a40 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 ly inside assert
95a50 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 () statements.
95a60 4f 6e 20 73 6f 6d 65 20 70 6c 61 74 66 6f 72 6d On some platform
95a70 73 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 69 67 68 s,.** there migh
95a80 74 20 62 65 20 72 61 63 65 20 63 6f 6e 64 69 74 t be race condit
95a90 69 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 63 61 ions that can ca
95aa0 75 73 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e use these routin
95ab0 65 73 20 74 6f 0a 2a 2a 20 64 65 6c 69 76 65 72 es to.** deliver
95ac0 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75 6c incorrect resul
95ad0 74 73 2e 20 20 49 6e 20 70 61 72 74 69 63 75 6c ts. In particul
95ae0 61 72 2c 20 69 66 20 70 74 68 72 65 61 64 5f 65 ar, if pthread_e
95af0 71 75 61 6c 28 29 20 69 73 0a 2a 2a 20 6e 6f 74 qual() is.** not
95b00 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 an atomic opera
95b10 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 73 65 tion, then these
95b20 20 72 6f 75 74 69 6e 65 73 20 6d 69 67 68 74 20 routines might
95b30 64 65 6c 69 76 65 72 79 0a 2a 2a 20 69 6e 63 6f delivery.** inco
95b40 72 72 65 63 74 20 72 65 73 75 6c 74 73 2e 20 20 rrect results.
95b50 4f 6e 20 6d 6f 73 74 20 70 6c 61 74 66 6f 72 6d On most platform
95b60 73 2c 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c s, pthread_equal
95b70 28 29 20 69 73 20 61 20 0a 2a 2a 20 63 6f 6d 70 () is a .** comp
95b80 61 72 69 73 6f 6e 20 6f 66 20 74 77 6f 20 69 6e arison of two in
95b90 74 65 67 65 72 73 20 61 6e 64 20 69 73 20 74 68 tegers and is th
95ba0 65 72 65 66 6f 72 65 20 61 74 6f 6d 69 63 2e 20 erefore atomic.
95bb0 20 42 75 74 20 77 65 20 61 72 65 0a 2a 2a 20 74 But we are.** t
95bc0 6f 6c 64 20 74 68 61 74 20 48 50 55 58 20 69 73 old that HPUX is
95bd0 20 6e 6f 74 20 73 75 63 68 20 61 20 70 6c 61 74 not such a plat
95be0 66 6f 72 6d 2e 20 20 49 66 20 73 6f 2c 20 74 68 form. If so, th
95bf0 65 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 en these routine
95c00 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 61 6c s.** will not al
95c10 77 61 79 73 20 77 6f 72 6b 20 63 6f 72 72 65 63 ways work correc
95c20 74 6c 79 20 6f 6e 20 48 50 55 58 2e 0a 2a 2a 0a tly on HPUX..**.
95c30 2a 2a 20 4f 6e 20 74 68 6f 73 65 20 70 6c 61 74 ** On those plat
95c40 66 6f 72 6d 73 20 77 68 65 72 65 20 70 74 68 72 forms where pthr
95c50 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 20 6e ead_equal() is n
95c60 6f 74 20 61 74 6f 6d 69 63 2c 20 53 51 4c 69 74 ot atomic, SQLit
95c70 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 e.** should be c
95c80 6f 6d 70 69 6c 65 64 20 77 69 74 68 6f 75 74 20 ompiled without
95c90 2d 44 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 -DSQLITE_DEBUG a
95ca0 6e 64 20 77 69 74 68 20 2d 44 4e 44 45 42 55 47 nd with -DNDEBUG
95cb0 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 73 75 72 65 to.** make sure
95cc0 20 6e 6f 20 61 73 73 65 72 74 28 29 20 73 74 61 no assert() sta
95cd0 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c tements are eval
95ce0 75 61 74 65 64 20 61 6e 64 20 68 65 6e 63 65 20 uated and hence
95cf0 74 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 these.** routine
95d00 73 20 61 72 65 20 6e 65 76 65 72 20 63 61 6c 6c s are never call
95d10 65 64 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 ed..*/.#if !defi
95d20 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 ned(NDEBUG) || d
95d30 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
95d40 42 55 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20 BUG).static int
95d50 70 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 pthreadMutexHeld
95d60 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
95d70 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d p){. return (p-
95d80 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 74 68 72 >nRef!=0 && pthr
95d90 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e ead_equal(p->own
95da0 65 72 2c 20 70 74 68 72 65 61 64 5f 73 65 6c 66 er, pthread_self
95db0 28 29 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 ()));.}.static i
95dc0 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e nt pthreadMutexN
95dd0 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d otheld(sqlite3_m
95de0 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 utex *p){. retu
95df0 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c rn p->nRef==0 ||
95e00 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 pthread_equal(p
95e10 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 61 64 ->owner, pthread
95e20 5f 73 65 6c 66 28 29 29 3d 3d 30 3b 0a 7d 0a 23 _self())==0;.}.#
95e30 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 endif../*.** Ini
95e40 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e tialize and dein
95e50 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 itialize the mut
95e60 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f ex subsystem..*/
95e70 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 .static int pthr
95e80 65 61 64 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 eadMutexInit(voi
95e90 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 d){ return SQLIT
95ea0 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 E_OK; }.static i
95eb0 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 45 nt pthreadMutexE
95ec0 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e nd(void){ return
95ed0 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f SQLITE_OK; }../
95ee0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
95ef0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 _mutex_alloc() r
95f00 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 outine allocates
95f10 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 a new.** mutex
95f20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f and returns a po
95f30 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 inter to it. If
95f40 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c it returns NULL
95f50 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 .** that means t
95f60 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c hat a mutex coul
95f70 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 d not be allocat
95f80 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 ed. SQLite.** w
95f90 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 ill unwind its s
95fa0 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 tack and return
95fb0 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 61 an error. The a
95fc0 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 rgument.** to sq
95fd0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
95fe0 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 c() is one of th
95ff0 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 ese integer cons
96000 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c tants:.**.** <ul
96010 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 >.** <li> SQLIT
96020 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 E_MUTEX_FAST.**
96030 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
96040 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 EX_RECURSIVE.**
96050 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
96060 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
96070 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 .** <li> SQLITE
96080 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 _MUTEX_STATIC_ME
96090 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 M.** <li> SQLIT
960a0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
960b0 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c EM2.** <li> SQL
960c0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
960d0 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53 _PRNG.** <li> S
960e0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
960f0 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20 20 IC_LRU.** <li>
96100 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
96110 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f 75 6c TIC_LRU2.** </ul
96120 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 >.**.** The firs
96130 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 t two constants
96140 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 cause sqlite3_mu
96150 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 tex_alloc() to c
96160 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d reate.** a new m
96170 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d utex. The new m
96180 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76 utex is recursiv
96190 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 e when SQLITE_MU
961a0 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a TEX_RECURSIVE.**
961b0 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 is used but not
961c0 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 necessarily so
961d0 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 when SQLITE_MUTE
961e0 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a X_FAST is used..
961f0 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70 ** The mutex imp
96200 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 lementation does
96210 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b not need to mak
96220 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a e a distinction.
96230 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54 ** between SQLIT
96240 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
96250 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 E and SQLITE_MUT
96260 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f EX_FAST if it do
96270 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 es.** not want t
96280 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77 o. But SQLite w
96290 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 ill only request
962a0 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 a recursive mut
962b0 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 ex in.** cases w
962c0 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e here it really n
962d0 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 eeds one. If a
962e0 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 faster non-recur
962f0 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d sive mutex.** im
96300 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 plementation is
96310 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 available on the
96320 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 host platform,
96330 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 the mutex subsys
96340 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 tem.** might ret
96350 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78 urn such a mutex
96360 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 in response to
96370 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
96380 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 T..**.** The oth
96390 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d er allowed param
963a0 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 eters to sqlite3
963b0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 _mutex_alloc() e
963c0 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 ach return.** a
963d0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 pointer to a sta
963e0 74 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 tic preexisting
963f0 6d 75 74 65 78 2e 20 20 53 69 78 20 73 74 61 74 mutex. Six stat
96400 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a ic mutexes are.*
96410 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75 * used by the cu
96420 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 rrent version of
96430 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65 SQLite. Future
96440 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
96450 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 ite.** may add a
96460 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 dditional static
96470 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69 mutexes. Stati
96480 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f c mutexes are fo
96490 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 r internal.** us
964a0 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 e by SQLite only
964b0 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 . Applications
964c0 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 that use SQLite
964d0 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a mutexes should.*
964e0 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 * use only the d
964f0 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72 ynamic mutexes r
96500 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54 eturned by SQLIT
96510 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a E_MUTEX_FAST or.
96520 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ** SQLITE_MUTEX_
96530 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a RECURSIVE..**.**
96540 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e Note that if on
96550 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 e of the dynamic
96560 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 mutex parameter
96570 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f s (SQLITE_MUTEX_
96580 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 FAST.** or SQLIT
96590 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
965a0 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 E) is used then
965b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
965c0 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 loc().** returns
965d0 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 a different mut
965e0 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c ex on every call
965f0 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 73 . But for the s
96600 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20 tatic .** mutex
96610 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20 types, the same
96620 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65 mutex is returne
96630 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 d on every call
96640 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 that has.** the
96650 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72 same type number
96660 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 ..*/.static sqli
96670 74 65 33 5f 6d 75 74 65 78 20 2a 70 74 68 72 65 te3_mutex *pthre
96680 61 64 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 adMutexAlloc(int
96690 20 69 54 79 70 65 29 7b 0a 20 20 73 74 61 74 69 iType){. stati
966a0 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 c sqlite3_mutex
966b0 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 5d 20 staticMutexes[]
966c0 3d 20 7b 0a 20 20 20 20 53 51 4c 49 54 45 33 5f = {. SQLITE3_
966d0 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 MUTEX_INITIALIZE
966e0 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d R,. SQLITE3_M
966f0 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 UTEX_INITIALIZER
96700 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 ,. SQLITE3_MU
96710 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c TEX_INITIALIZER,
96720 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 . SQLITE3_MUT
96730 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a EX_INITIALIZER,.
96740 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 SQLITE3_MUTE
96750 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 X_INITIALIZER,.
96760 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 SQLITE3_MUTEX
96770 5f 49 4e 49 54 49 41 4c 49 5a 45 52 0a 20 20 7d _INITIALIZER. }
96780 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
96790 78 20 2a 70 3b 0a 20 20 73 77 69 74 63 68 28 20 x *p;. switch(
967a0 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 iType ){. cas
967b0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 e SQLITE_MUTEX_R
967c0 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 ECURSIVE: {.
967d0 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c p = sqlite3Mal
967e0 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 locZero( sizeof(
967f0 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 *p) );. if(
96800 20 70 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c p ){.#ifdef SQL
96810 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 ITE_HOMEGROWN_RE
96820 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 CURSIVE_MUTEX.
96830 20 20 20 20 20 20 2f 2a 20 49 66 20 72 65 63 75 /* If recu
96840 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 61 72 rsive mutexes ar
96850 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c e not available,
96860 20 77 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f we will have to
96870 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 75 69 6c . ** buil
96880 64 20 6f 75 72 20 6f 77 6e 2e 20 20 53 65 65 20 d our own. See
96890 62 65 6c 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20 below. */.
968a0 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f pthread_mutex_
968b0 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 init(&p->mutex,
968c0 30 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 0);.#else.
968d0 20 20 2f 2a 20 55 73 65 20 61 20 72 65 63 75 72 /* Use a recur
968e0 73 69 76 65 20 6d 75 74 65 78 20 69 66 20 69 74 sive mutex if it
968f0 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f is available */
96900 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 . pthread
96910 5f 6d 75 74 65 78 61 74 74 72 5f 74 20 72 65 63 _mutexattr_t rec
96920 75 72 73 69 76 65 41 74 74 72 3b 0a 20 20 20 20 ursiveAttr;.
96930 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 pthread_mute
96940 78 61 74 74 72 5f 69 6e 69 74 28 26 72 65 63 75 xattr_init(&recu
96950 72 73 69 76 65 41 74 74 72 29 3b 0a 20 20 20 20 rsiveAttr);.
96960 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 pthread_mute
96970 78 61 74 74 72 5f 73 65 74 74 79 70 65 28 26 72 xattr_settype(&r
96980 65 63 75 72 73 69 76 65 41 74 74 72 2c 20 50 54 ecursiveAttr, PT
96990 48 52 45 41 44 5f 4d 55 54 45 58 5f 52 45 43 55 HREAD_MUTEX_RECU
969a0 52 53 49 56 45 29 3b 0a 20 20 20 20 20 20 20 20 RSIVE);.
969b0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e pthread_mutex_in
969c0 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 26 72 it(&p->mutex, &r
969d0 65 63 75 72 73 69 76 65 41 74 74 72 29 3b 0a 20 ecursiveAttr);.
969e0 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d pthread_m
969f0 75 74 65 78 61 74 74 72 5f 64 65 73 74 72 6f 79 utexattr_destroy
96a00 28 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29 (&recursiveAttr)
96a10 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 ;.#endif.
96a20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a p->id = iType;.
96a30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
96a40 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
96a50 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ase SQLITE_MUTEX
96a60 5f 46 41 53 54 3a 20 7b 0a 20 20 20 20 20 20 70 _FAST: {. p
96a70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
96a80 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 Zero( sizeof(*p)
96a90 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 );. if( p
96aa0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 ){. p->id
96ab0 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 = iType;.
96ac0 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f pthread_mutex_
96ad0 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 init(&p->mutex,
96ae0 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0);. }.
96af0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
96b00 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 default: {.
96b10 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70 assert( iTyp
96b20 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 e-2 >= 0 );.
96b30 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d assert( iType-
96b40 32 20 3c 20 41 72 72 61 79 53 69 7a 65 28 73 74 2 < ArraySize(st
96b50 61 74 69 63 4d 75 74 65 78 65 73 29 20 29 3b 0a aticMutexes) );.
96b60 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 74 69 p = &stati
96b70 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32 cMutexes[iType-2
96b80 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d ];. p->id =
96b90 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72 iType;. br
96ba0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
96bb0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f return p;.}.../
96bc0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
96bd0 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 e deallocates a
96be0 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c previously.** al
96bf0 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 20 located mutex.
96c00 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 SQLite is carefu
96c10 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 l to deallocate
96c20 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 every.** mutex t
96c30 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 hat it allocates
96c40 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
96c50 20 70 74 68 72 65 61 64 4d 75 74 65 78 46 72 65 pthreadMutexFre
96c60 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 e(sqlite3_mutex
96c70 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
96c80 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 ->nRef==0 );. a
96c90 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
96ca0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 LITE_MUTEX_FAST
96cb0 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 || p->id==SQLITE
96cc0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
96cd0 20 29 3b 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 );. pthread_mu
96ce0 74 65 78 5f 64 65 73 74 72 6f 79 28 26 70 2d 3e tex_destroy(&p->
96cf0 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 mutex);. sqlite
96d00 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 3_free(p);.}../*
96d10 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
96d20 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e mutex_enter() an
96d30 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
96d40 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 try() routines a
96d50 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 ttempt.** to ent
96d60 65 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 er a mutex. If
96d70 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 another thread i
96d80 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e s already within
96d90 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 the mutex,.** s
96da0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
96db0 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 er() will block
96dc0 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 and sqlite3_mute
96dd0 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 x_try() will ret
96de0 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 urn.** SQLITE_BU
96df0 53 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 SY. The sqlite3
96e00 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 _mutex_try() int
96e10 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 erface returns S
96e20 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e QLITE_OK.** upon
96e30 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 successful entr
96e40 79 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 y. Mutexes crea
96e50 74 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 ted using SQLITE
96e60 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
96e70 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 can.** be enter
96e80 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 ed multiple time
96e90 73 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 s by the same th
96ea0 72 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 read. In such c
96eb0 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 ases the,.** mut
96ec0 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 ex must be exite
96ed0 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 d an equal numbe
96ee0 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 r of times befor
96ef0 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 e another thread
96f00 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 .** can enter.
96f10 49 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 If the same thre
96f20 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 ad tries to ente
96f30 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 r any other kind
96f40 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 of mutex.** mor
96f50 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 e than once, the
96f60 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 behavior is und
96f70 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 efined..*/.stati
96f80 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75 c void pthreadMu
96f90 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33 texEnter(sqlite3
96fa0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 _mutex *p){. as
96fb0 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c sert( p->id==SQL
96fc0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
96fd0 49 56 45 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 IVE || pthreadMu
96fe0 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b texNotheld(p) );
96ff0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
97000 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 HOMEGROWN_RECURS
97010 49 56 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 IVE_MUTEX. /* I
97020 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 f recursive mute
97030 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 xes are not avai
97040 6c 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 lable, then we h
97050 61 76 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a ave to grow. **
97060 20 6f 75 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 our own. This
97070 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 implementation a
97080 73 73 75 6d 65 73 20 74 68 61 74 20 70 74 68 72 ssumes that pthr
97090 65 61 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a ead_equal(). **
970a0 20 69 73 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 is atomic - tha
970b0 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 t it cannot be d
970c0 65 63 65 69 76 65 64 20 69 6e 74 6f 20 74 68 69 eceived into thi
970d0 6e 6b 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 nking self. **
970e0 61 6e 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 and p->owner are
970f0 20 65 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e equal if p->own
97100 65 72 20 63 68 61 6e 67 65 73 20 62 65 74 77 65 er changes betwe
97110 65 6e 20 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 en two values.
97120 2a 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 ** that are not
97130 65 71 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 equal to self wh
97140 69 6c 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 ile the comparis
97150 6f 6e 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 on is taking pla
97160 63 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d ce.. ** This im
97170 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 plementation als
97180 6f 20 61 73 73 75 6d 65 73 20 61 20 63 6f 68 65 o assumes a cohe
97190 72 65 6e 74 20 63 61 63 68 65 20 2d 20 74 68 61 rent cache - tha
971a0 74 20 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 65 t . ** separate
971b0 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f processes canno
971c0 74 20 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 t read different
971d0 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 values from the
971e0 20 73 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 same. ** addre
971f0 73 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 ss at the same t
97200 69 6d 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 ime. If either
97210 6f 66 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e of these two con
97220 64 69 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 ditions. ** are
97230 20 6e 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 not met, then t
97240 68 65 20 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 he mutexes will
97250 66 61 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d fail and problem
97260 73 20 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 s will result..
97270 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 */. {. pthr
97280 65 61 64 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 ead_t self = pth
97290 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 read_self();.
972a0 20 69 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 if( p->nRef>0 &
972b0 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 & pthread_equal(
972c0 70 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 p->owner, self)
972d0 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 ){. p->nRef
972e0 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ++;. }else{.
972f0 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 pthread_mut
97300 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 ex_lock(&p->mute
97310 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 x);. assert
97320 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a ( p->nRef==0 );.
97330 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d p->owner =
97340 20 73 65 6c 66 3b 0a 20 20 20 20 20 20 70 2d 3e self;. p->
97350 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 7d 0a nRef = 1;. }.
97360 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 55 }.#else. /* U
97370 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 se the built-in
97380 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 recursive mutexe
97390 73 20 69 66 20 74 68 65 79 20 61 72 65 20 61 76 s if they are av
973a0 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 ailable.. */.
973b0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f pthread_mutex_lo
973c0 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 ck(&p->mutex);.
973d0 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72 p->owner = pthr
973e0 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 70 2d ead_self();. p-
973f0 3e 6e 52 65 66 2b 2b 3b 0a 23 65 6e 64 69 66 0a >nRef++;.#endif.
97400 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
97410 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e 74 72 EBUG. if( p->tr
97420 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 ace ){. print
97430 66 28 22 65 6e 74 65 72 20 6d 75 74 65 78 20 25 f("enter mutex %
97440 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65 66 p (%d) with nRef
97450 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 =%d\n", p, p->tr
97460 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 ace, p->nRef);.
97470 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74 }.#endif.}.stat
97480 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 ic int pthreadMu
97490 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d texTry(sqlite3_m
974a0 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 utex *p){. int
974b0 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d rc;. assert( p-
974c0 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
974d0 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 70 X_RECURSIVE || p
974e0 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 threadMutexNothe
974f0 6c 64 28 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 ld(p) );..#ifdef
97500 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 SQLITE_HOMEGROW
97510 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 N_RECURSIVE_MUTE
97520 58 0a 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73 X. /* If recurs
97530 69 76 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 ive mutexes are
97540 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 not available, t
97550 68 65 6e 20 77 65 20 68 61 76 65 20 74 6f 20 67 hen we have to g
97560 72 6f 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e row. ** our own
97570 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e . This implemen
97580 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 tation assumes t
97590 68 61 74 20 70 74 68 72 65 61 64 5f 65 71 75 61 hat pthread_equa
975a0 6c 28 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d l(). ** is atom
975b0 69 63 20 2d 20 74 68 61 74 20 69 74 20 63 61 6e ic - that it can
975c0 6e 6f 74 20 62 65 20 64 65 63 65 69 76 65 64 20 not be deceived
975d0 69 6e 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 into thinking se
975e0 6c 66 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f lf. ** and p->o
975f0 77 6e 65 72 20 61 72 65 20 65 71 75 61 6c 20 69 wner are equal i
97600 66 20 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 f p->owner chang
97610 65 73 20 62 65 74 77 65 65 6e 20 74 77 6f 20 76 es between two v
97620 61 6c 75 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 alues. ** that
97630 61 72 65 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f are not equal to
97640 20 73 65 6c 66 20 77 68 69 6c 65 20 74 68 65 20 self while the
97650 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 61 comparison is ta
97660 6b 69 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a king place.. **
97670 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 This implementa
97680 74 69 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 tion also assume
97690 73 20 61 20 63 6f 68 65 72 65 6e 74 20 63 61 63 s a coherent cac
976a0 68 65 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 he - that . **
976b0 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 73 73 separate process
976c0 65 73 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 es cannot read d
976d0 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65 73 20 ifferent values
976e0 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 from the same.
976f0 2a 2a 20 61 64 64 72 65 73 73 20 61 74 20 74 68 ** address at th
97700 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 e same time. If
97710 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 either of these
97720 20 74 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a two conditions.
97730 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 ** are not met
97740 2c 20 74 68 65 6e 20 74 68 65 20 6d 75 74 65 78 , then the mutex
97750 65 73 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 es will fail and
97760 20 70 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 problems will r
97770 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a esult.. */. {.
97780 20 20 20 20 70 74 68 72 65 61 64 5f 74 20 73 65 pthread_t se
97790 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c lf = pthread_sel
977a0 66 28 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e f();. if( p->
977b0 6e 52 65 66 3e 30 20 26 26 20 70 74 68 72 65 61 nRef>0 && pthrea
977c0 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 d_equal(p->owner
977d0 2c 20 73 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 , self) ){.
977e0 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 p->nRef++;.
977f0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
97800 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
97810 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 72 pthread_mutex_tr
97820 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 ylock(&p->mutex)
97830 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 ==0 ){. ass
97840 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 ert( p->nRef==0
97850 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65 );. p->owne
97860 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 20 20 20 r = self;.
97870 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 p->nRef = 1;.
97880 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
97890 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 K;. }else{.
978a0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
978b0 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a BUSY;. }. }.
978c0 23 65 6c 73 65 0a 20 20 2f 2a 20 55 73 65 20 74 #else. /* Use t
978d0 68 65 20 62 75 69 6c 74 2d 69 6e 20 72 65 63 75 he built-in recu
978e0 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 69 66 rsive mutexes if
978f0 20 74 68 65 79 20 61 72 65 20 61 76 61 69 6c 61 they are availa
97900 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ble.. */. if(
97910 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 72 pthread_mutex_tr
97920 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 ylock(&p->mutex)
97930 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6f 77 ==0 ){. p->ow
97940 6e 65 72 20 3d 20 70 74 68 72 65 61 64 5f 73 65 ner = pthread_se
97950 6c 66 28 29 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 lf();. p->nRe
97960 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 f++;. rc = SQ
97970 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 LITE_OK;. }else
97980 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
97990 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64 E_BUSY;. }.#end
979a0 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
979b0 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 72 63 E_DEBUG. if( rc
979c0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 ==SQLITE_OK && p
979d0 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70 ->trace ){. p
979e0 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d 75 74 rintf("enter mut
979f0 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68 20 ex %p (%d) with
97a00 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 nRef=%d\n", p, p
97a10 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 ->trace, p->nRef
97a20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 );. }.#endif.
97a30 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
97a40 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
97a50 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f mutex_leave() ro
97a60 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 utine exits a mu
97a70 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 tex that was.**
97a80 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 previously enter
97a90 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 ed by the same t
97aa0 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 hread. The beha
97ab0 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 vior.** is undef
97ac0 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 ined if the mute
97ad0 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 x is not current
97ae0 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a ly entered or.**
97af0 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
97b00 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 y allocated. SQ
97b10 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 Lite will never
97b20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 do either..*/.st
97b30 61 74 69 63 20 76 6f 69 64 20 70 74 68 72 65 61 atic void pthrea
97b40 64 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 dMutexLeave(sqli
97b50 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
97b60 20 61 73 73 65 72 74 28 20 70 74 68 72 65 61 64 assert( pthread
97b70 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29 3b 0a MutexHeld(p) );.
97b80 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 p->nRef--;. a
97b90 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d ssert( p->nRef==
97ba0 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 0 || p->id==SQLI
97bb0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
97bc0 56 45 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 VE );..#ifdef SQ
97bd0 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 LITE_HOMEGROWN_R
97be0 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 ECURSIVE_MUTEX.
97bf0 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 if( p->nRef==0
97c00 29 7b 0a 20 20 20 20 70 74 68 72 65 61 64 5f 6d ){. pthread_m
97c10 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e utex_unlock(&p->
97c20 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 23 65 6c 73 mutex);. }.#els
97c30 65 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 e. pthread_mute
97c40 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 x_unlock(&p->mut
97c50 65 78 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 ex);.#endif..#if
97c60 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
97c70 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20 . if( p->trace
97c80 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 6c ){. printf("l
97c90 65 61 76 65 20 6d 75 74 65 78 20 25 70 20 28 25 eave mutex %p (%
97ca0 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c d) with nRef=%d\
97cb0 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c n", p, p->trace,
97cc0 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 p->nRef);. }.#
97cd0 65 6e 64 69 66 0a 7d 0a 0a 53 51 4c 49 54 45 5f endif.}..SQLITE_
97ce0 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f PRIVATE sqlite3_
97cf0 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 mutex_methods *s
97d00 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 qlite3DefaultMut
97d10 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 ex(void){. stat
97d20 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ic sqlite3_mutex
97d30 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 _methods sMutex
97d40 3d 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64 4d = {. pthreadM
97d50 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 70 74 utexInit,. pt
97d60 68 72 65 61 64 4d 75 74 65 78 45 6e 64 2c 0a 20 hreadMutexEnd,.
97d70 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 41 pthreadMutexA
97d80 6c 6c 6f 63 2c 0a 20 20 20 20 70 74 68 72 65 61 lloc,. pthrea
97d90 64 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 dMutexFree,.
97da0 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e 74 65 pthreadMutexEnte
97db0 72 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 r,. pthreadMu
97dc0 74 65 78 54 72 79 2c 0a 20 20 20 20 70 74 68 72 texTry,. pthr
97dd0 65 61 64 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 eadMutexLeave,.#
97de0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
97df0 55 47 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 UG. pthreadMu
97e00 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 70 74 68 texHeld,. pth
97e10 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 readMutexNotheld
97e20 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c 0a 20 20 .#else. 0,.
97e30 20 20 30 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 0.#endif. };.
97e40 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65 . return &sMute
97e50 78 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 x;.}..#endif /*
97e60 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 SQLITE_MUTEX_PTH
97e70 52 45 41 44 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a READ */../******
97e80 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
97e90 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a 2a 2a mutex_unix.c ***
97ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97ec0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
97ed0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
97ee0 69 6c 65 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 ile mutex_w32.c
97ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97f10 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
97f20 30 30 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 007 August 14.**
97f30 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
97f40 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
97f50 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
97f60 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
97f70 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
97f80 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
97f90 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
97fa0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
97fb0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
97fc0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
97fd0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
97fe0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
97ff0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
98000 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
98010 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
98020 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
98030 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
98040 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
98050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
98090 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
980a0 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f ns the C functio
980b0 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e ns that implemen
980c0 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 77 69 t mutexes for wi
980d0 6e 33 32 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d n32.**.** $Id: m
980e0 75 74 65 78 5f 77 33 32 2e 63 2c 76 20 31 2e 31 utex_w32.c,v 1.1
980f0 38 20 32 30 30 39 2f 30 38 2f 31 30 20 30 33 3a 8 2009/08/10 03:
98100 32 33 3a 32 31 20 73 68 61 6e 65 20 45 78 70 20 23:21 shane Exp
98110 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 $.*/../*.** The
98120 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c code in this fil
98130 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 e is only used i
98140 66 20 77 65 20 61 72 65 20 63 6f 6d 70 69 6c 69 f we are compili
98150 6e 67 20 6d 75 6c 74 69 74 68 72 65 61 64 65 64 ng multithreaded
98160 0a 2a 2a 20 6f 6e 20 61 20 77 69 6e 33 32 20 73 .** on a win32 s
98170 79 73 74 65 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66 ystem..*/.#ifdef
98180 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 SQLITE_MUTEX_W3
98190 32 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 72 65 2../*.** Each re
981a0 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 73 cursive mutex is
981b0 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
981c0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
981d0 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 ructure..*/.stru
981e0 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ct sqlite3_mutex
981f0 20 7b 0a 20 20 43 52 49 54 49 43 41 4c 5f 53 45 {. CRITICAL_SE
98200 43 54 49 4f 4e 20 6d 75 74 65 78 3b 20 20 20 20 CTION mutex;
98210 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c /* Mutex control
98220 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f ling the lock */
98230 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20 20 . int id;
98240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
98250 20 4d 75 74 65 78 20 74 79 70 65 20 2a 2f 0a 20 Mutex type */.
98260 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 int nRef;
98270 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
98280 75 6d 62 65 72 20 6f 66 20 65 6e 74 65 72 61 6e umber of enteran
98290 63 65 73 20 2a 2f 0a 20 20 44 57 4f 52 44 20 6f ces */. DWORD o
982a0 77 6e 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 wner;
982b0 20 20 20 20 2f 2a 20 54 68 72 65 61 64 20 68 6f /* Thread ho
982c0 6c 64 69 6e 67 20 74 68 69 73 20 6d 75 74 65 78 lding this mutex
982d0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 */.};../*.** Re
982e0 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e 2d 7a turn true (non-z
982f0 65 72 6f 29 20 69 66 20 77 65 20 61 72 65 20 72 ero) if we are r
98300 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57 69 6e unning under Win
98310 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e 58 50 NT, Win2K, WinXP
98320 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e 20 20 ,.** or WinCE.
98330 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28 7a 65 Return false (ze
98340 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c 20 57 ro) for Win95, W
98350 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45 2e 0a in98, or WinME..
98360 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20 61 6e **.** Here is an
98370 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f 62 73 interesting obs
98380 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e 39 35 ervation: Win95
98390 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57 69 6e , Win98, and Win
983a0 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65 20 4c ME lack.** the L
983b0 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e ockFileEx() API.
983c0 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 74 69 But we can sti
983d0 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 ll statically li
983e0 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61 74 0a nk against that.
983f0 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67 20 61 ** API as long a
98400 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c 6c 20 s we don't call
98410 69 74 20 77 69 6e 20 72 75 6e 6e 69 6e 67 20 57 it win running W
98420 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 41 20 63 in95/98/ME. A c
98430 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 73 20 72 all to.** this r
98440 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 outine is used t
98450 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 o determine if t
98460 68 65 20 68 6f 73 74 20 69 73 20 57 69 6e 39 35 he host is Win95
98470 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 57 69 6e /98/ME or.** Win
98480 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 68 61 74 NT/2K/XP so that
98490 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 we will know wh
984a0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 77 65 20 ether or not we
984b0 63 61 6e 20 73 61 66 65 6c 79 20 63 61 6c 6c 0a can safely call.
984c0 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 ** the LockFileE
984d0 78 28 29 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 6d x() API..**.** m
984e0 75 74 65 78 49 73 4e 54 28 29 20 69 73 20 6f 6e utexIsNT() is on
984f0 6c 79 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 ly used for the
98500 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 6c TryEnterCritical
98510 53 65 63 74 69 6f 6e 28 29 20 41 50 49 20 63 61 Section() API ca
98520 6c 6c 2c 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 ll,.** which is
98530 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 only available i
98540 66 20 79 6f 75 72 20 61 70 70 6c 69 63 61 74 69 f your applicati
98550 6f 6e 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 on was compiled
98560 77 69 74 68 20 0a 2a 2a 20 5f 57 49 4e 33 32 5f with .** _WIN32_
98570 57 49 4e 4e 54 20 64 65 66 69 6e 65 64 20 74 6f WINNT defined to
98580 20 61 20 76 61 6c 75 65 20 3e 3d 20 30 78 30 34 a value >= 0x04
98590 30 30 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20 00. Currently,
985a0 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20 63 61 6c 6c the only.** call
985b0 20 74 6f 20 54 72 79 45 6e 74 65 72 43 72 69 74 to TryEnterCrit
985c0 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 69 73 icalSection() is
985d0 20 23 69 66 64 65 66 27 65 64 20 6f 75 74 2c 20 #ifdef'ed out,
985e0 73 6f 20 23 69 66 64 65 66 20 0a 2a 2a 20 74 68 so #ifdef .** th
985f0 69 73 20 6f 75 74 20 61 73 20 77 65 6c 6c 2e 0a is out as well..
98600 2a 2f 0a 23 69 66 20 30 0a 23 69 66 20 53 51 4c */.#if 0.#if SQL
98610 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 20 64 ITE_OS_WINCE.# d
98620 65 66 69 6e 65 20 6d 75 74 65 78 49 73 4e 54 28 efine mutexIsNT(
98630 29 20 20 28 31 29 0a 23 65 6c 73 65 0a 20 20 73 ) (1).#else. s
98640 74 61 74 69 63 20 69 6e 74 20 6d 75 74 65 78 49 tatic int mutexI
98650 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 20 73 sNT(void){. s
98660 74 61 74 69 63 20 69 6e 74 20 6f 73 54 79 70 65 tatic int osType
98670 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 6f 73 = 0;. if( os
98680 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Type==0 ){.
98690 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 OSVERSIONINFO s
986a0 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 Info;. sInf
986b0 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 o.dwOSVersionInf
986c0 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 oSize = sizeof(s
986d0 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 Info);. Get
986e0 56 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f VersionEx(&sInfo
986f0 29 3b 0a 20 20 20 20 20 20 6f 73 54 79 70 65 20 );. osType
98700 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61 74 66 6f = sInfo.dwPlatfo
98710 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41 54 46 4f rmId==VER_PLATFO
98720 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f 20 32 20 RM_WIN32_NT ? 2
98730 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 : 1;. }. r
98740 65 74 75 72 6e 20 6f 73 54 79 70 65 3d 3d 32 3b eturn osType==2;
98750 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 . }.#endif /* S
98760 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 2a QLITE_OS_WINCE *
98770 2f 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 /.#endif..#ifdef
98780 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a SQLITE_DEBUG./*
98790 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
987a0 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 mutex_held() and
987b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e sqlite3_mutex_n
987c0 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 otheld() routine
987d0 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 are.** intended
987e0 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e for use only in
987f0 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 side assert() st
98800 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 atements..*/.sta
98810 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 tic int winMutex
98820 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 Held(sqlite3_mut
98830 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e ex *p){. return
98840 20 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 p->nRef!=0 && p
98850 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 75 72 72 ->owner==GetCurr
98860 65 6e 74 54 68 72 65 61 64 49 64 28 29 3b 0a 7d entThreadId();.}
98870 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d .static int winM
98880 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 utexNotheld(sqli
98890 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
988a0 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d return p->nRef=
988b0 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72 21 3d =0 || p->owner!=
988c0 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 GetCurrentThread
988d0 49 64 28 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a Id();.}.#endif..
988e0 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a ./*.** Initializ
988f0 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 e and deinitiali
98900 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 ze the mutex sub
98910 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 system..*/.stati
98920 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 c sqlite3_mutex
98930 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d winMutex_staticM
98940 75 74 65 78 65 73 5b 36 5d 3b 0a 73 74 61 74 69 utexes[6];.stati
98950 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 5f 69 c int winMutex_i
98960 73 49 6e 69 74 20 3d 20 30 3b 0a 2f 2a 20 41 73 sInit = 0;./* As
98970 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 28 29 20 winMutexInit()
98980 61 6e 64 20 77 69 6e 4d 75 74 65 78 45 6e 64 28 and winMutexEnd(
98990 29 20 61 72 65 20 63 61 6c 6c 65 64 20 61 73 20 ) are called as
989a0 70 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20 73 part.** of the s
989b0 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
989c0 65 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 68 e and sqlite3_sh
989d0 75 74 64 6f 77 6e 28 29 0a 2a 2a 20 70 72 6f 63 utdown().** proc
989e0 65 73 73 69 6e 67 2c 20 74 68 65 20 22 69 6e 74 essing, the "int
989f0 65 72 6c 6f 63 6b 65 64 22 20 6d 61 67 69 63 20 erlocked" magic
98a00 69 73 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 0a is probably not.
98a10 2a 2a 20 73 74 72 69 63 74 6c 79 20 6e 65 63 65 ** strictly nece
98a20 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 ssary..*/.static
98a30 20 6c 6f 6e 67 20 77 69 6e 4d 75 74 65 78 5f 6c long winMutex_l
98a40 6f 63 6b 20 3d 20 30 3b 0a 0a 73 74 61 74 69 63 ock = 0;..static
98a50 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 49 6e 69 int winMutexIni
98a60 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 54 t(void){ . /* T
98a70 68 65 20 66 69 72 73 74 20 74 6f 20 69 6e 63 72 he first to incr
98a80 65 6d 65 6e 74 20 74 6f 20 31 20 64 6f 65 73 20 ement to 1 does
98a90 61 63 74 75 61 6c 20 69 6e 69 74 69 61 6c 69 7a actual initializ
98aa0 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 49 ation */. if( I
98ab0 6e 74 65 72 6c 6f 63 6b 65 64 43 6f 6d 70 61 72 nterlockedCompar
98ac0 65 45 78 63 68 61 6e 67 65 28 26 77 69 6e 4d 75 eExchange(&winMu
98ad0 74 65 78 5f 6c 6f 63 6b 2c 20 31 2c 20 30 29 3d tex_lock, 1, 0)=
98ae0 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b =0 ){. int i;
98af0 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
98b00 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d 75 74 ArraySize(winMut
98b10 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73 ex_staticMutexes
98b20 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 49 ); i++){. I
98b30 6e 69 74 69 61 6c 69 7a 65 43 72 69 74 69 63 61 nitializeCritica
98b40 6c 53 65 63 74 69 6f 6e 28 26 77 69 6e 4d 75 74 lSection(&winMut
98b50 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73 ex_staticMutexes
98b60 5b 69 5d 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 [i].mutex);.
98b70 7d 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 5f 69 }. winMutex_i
98b80 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 65 6c sInit = 1;. }el
98b90 73 65 7b 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 6f se{. /* Someo
98ba0 6e 65 20 65 6c 73 65 20 69 73 20 69 6e 20 74 68 ne else is in th
98bb0 65 20 70 72 6f 63 65 73 73 20 6f 66 20 69 6e 69 e process of ini
98bc0 74 69 6e 67 20 74 68 65 20 73 74 61 74 69 63 20 ting the static
98bd0 6d 75 74 65 78 65 73 20 2a 2f 0a 20 20 20 20 77 mutexes */. w
98be0 68 69 6c 65 28 20 21 77 69 6e 4d 75 74 65 78 5f hile( !winMutex_
98bf0 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 isInit ){.
98c00 53 6c 65 65 70 28 31 29 3b 0a 20 20 20 20 7d 0a Sleep(1);. }.
98c10 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
98c20 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 73 74 61 74 ITE_OK; .}..stat
98c30 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 45 ic int winMutexE
98c40 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 nd(void){ . /*
98c50 54 68 65 20 66 69 72 73 74 20 74 6f 20 64 65 63 The first to dec
98c60 72 65 6d 65 6e 74 20 74 6f 20 30 20 64 6f 65 73 rement to 0 does
98c70 20 61 63 74 75 61 6c 20 73 68 75 74 64 6f 77 6e actual shutdown
98c80 20 0a 20 20 2a 2a 20 28 77 68 69 63 68 20 73 68 . ** (which sh
98c90 6f 75 6c 64 20 62 65 20 74 68 65 20 6c 61 73 74 ould be the last
98ca0 20 74 6f 20 73 68 75 74 64 6f 77 6e 2e 29 20 2a to shutdown.) *
98cb0 2f 0a 20 20 69 66 28 20 49 6e 74 65 72 6c 6f 63 /. if( Interloc
98cc0 6b 65 64 43 6f 6d 70 61 72 65 45 78 63 68 61 6e kedCompareExchan
98cd0 67 65 28 26 77 69 6e 4d 75 74 65 78 5f 6c 6f 63 ge(&winMutex_loc
98ce0 6b 2c 20 30 2c 20 31 29 3d 3d 31 20 29 7b 0a 20 k, 0, 1)==1 ){.
98cf0 20 20 20 69 66 28 20 77 69 6e 4d 75 74 65 78 5f if( winMutex_
98d00 69 73 49 6e 69 74 3d 3d 31 20 29 7b 0a 20 20 20 isInit==1 ){.
98d10 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
98d20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 for(i=0; i<Array
98d30 53 69 7a 65 28 77 69 6e 4d 75 74 65 78 5f 73 74 Size(winMutex_st
98d40 61 74 69 63 4d 75 74 65 78 65 73 29 3b 20 69 2b aticMutexes); i+
98d50 2b 29 7b 0a 20 20 20 20 20 20 20 20 44 65 6c 65 +){. Dele
98d60 74 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f teCriticalSectio
98d70 6e 28 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 n(&winMutex_stat
98d80 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74 icMutexes[i].mut
98d90 65 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ex);. }.
98da0 20 20 20 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e winMutex_isIn
98db0 69 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 it = 0;. }.
98dc0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
98dd0 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 E_OK; .}../*.**
98de0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
98df0 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e x_alloc() routin
98e00 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 e allocates a ne
98e10 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 w.** mutex and r
98e20 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
98e30 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72 to it. If it r
98e40 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 eturns NULL.** t
98e50 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 hat means that a
98e60 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 mutex could not
98e70 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 be allocated.
98e80 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 SQLite.** will u
98e90 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20 nwind its stack
98ea0 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 and return an er
98eb0 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65 ror. The argume
98ec0 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 nt.** to sqlite3
98ed0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 _mutex_alloc() i
98ee0 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 s one of these i
98ef0 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 nteger constants
98f00 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 :.**.** <ul>.**
98f10 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
98f20 45 58 5f 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 EX_FAST.** <li>
98f30 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
98f40 43 55 52 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 CURSIVE.** <li>
98f50 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
98f60 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c ATIC_MASTER.** <
98f70 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
98f80 58 5f 53 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 X_STATIC_MEM.**
98f90 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
98fa0 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a EX_STATIC_MEM2.*
98fb0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
98fc0 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 UTEX_STATIC_PRNG
98fd0 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 .** <li> SQLITE
98fe0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 _MUTEX_STATIC_LR
98ff0 55 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 U.** <li> SQLIT
99000 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c E_MUTEX_STATIC_L
99010 52 55 32 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a RU2.** </ul>.**.
99020 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f ** The first two
99030 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65 constants cause
99040 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
99050 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65 lloc() to create
99060 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e .** a new mutex.
99070 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20 The new mutex
99080 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65 is recursive whe
99090 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 n SQLITE_MUTEX_R
990a0 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 ECURSIVE.** is u
990b0 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 sed but not nece
990c0 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 ssarily so when
990d0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
990e0 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68 T is used..** Th
990f0 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e e mutex implemen
99100 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 tation does not
99110 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 need to make a d
99120 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 istinction.** be
99130 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 tween SQLITE_MUT
99140 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64 EX_RECURSIVE and
99150 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
99160 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a ST if it does.**
99170 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42 not want to. B
99180 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f ut SQLite will o
99190 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65 nly request a re
991a0 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e cursive mutex in
991b0 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 .** cases where
991c0 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 it really needs
991d0 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65 one. If a faste
991e0 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 r non-recursive
991f0 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 mutex.** impleme
99200 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c ntation is avail
99210 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 able on the host
99220 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d platform, the m
99230 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a utex subsystem.*
99240 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73 * might return s
99250 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72 uch a mutex in r
99260 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 esponse to SQLIT
99270 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a E_MUTEX_FAST..**
99280 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c .** The other al
99290 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73 lowed parameters
992a0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 to sqlite3_mute
992b0 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 x_alloc() each r
992c0 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 eturn.** a point
992d0 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 er to a static p
992e0 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 reexisting mutex
992f0 2e 20 20 53 69 78 20 73 74 61 74 69 63 20 6d 75 . Six static mu
99300 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 texes are.** use
99310 64 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 d by the current
99320 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 version of SQLi
99330 74 65 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 te. Future vers
99340 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a ions of SQLite.*
99350 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69 74 69 * may add additi
99360 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 onal static mute
99370 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74 xes. Static mut
99380 65 78 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 exes are for int
99390 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 ernal.** use by
993a0 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 SQLite only. Ap
993b0 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 plications that
993c0 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78 use SQLite mutex
993d0 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 es should.** use
993e0 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 only the dynami
993f0 63 20 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e c mutexes return
99400 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 ed by SQLITE_MUT
99410 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 EX_FAST or.** SQ
99420 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
99430 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 SIVE..**.** Note
99440 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 that if one of
99450 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 the dynamic mute
99460 78 20 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 x parameters (SQ
99470 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a LITE_MUTEX_FAST.
99480 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 ** or SQLITE_MUT
99490 45 58 5f 52 45 43 55 52 53 49 56 45 29 20 69 73 EX_RECURSIVE) is
994a0 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74 used then sqlit
994b0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
994c0 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69 .** returns a di
994d0 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e fferent mutex on
994e0 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 42 75 every call. Bu
994f0 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 69 63 t for the static
99500 20 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 73 .** mutex types
99510 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65 78 , the same mutex
99520 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 is returned on
99530 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74 20 every call that
99540 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 has.** the same
99550 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a type number..*/.
99560 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d static sqlite3_m
99570 75 74 65 78 20 2a 77 69 6e 4d 75 74 65 78 41 6c utex *winMutexAl
99580 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a loc(int iType){.
99590 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
995a0 2a 70 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 69 *p;.. switch( i
995b0 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 Type ){. case
995c0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
995d0 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c ST:. case SQL
995e0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
995f0 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d IVE: {. p =
99600 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 sqlite3MallocZe
99610 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 ro( sizeof(*p) )
99620 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 29 7b ;. if( p ){
99630 20 20 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 . p->id
99640 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 = iType;.
99650 20 20 49 6e 69 74 69 61 6c 69 7a 65 43 72 69 74 InitializeCrit
99660 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e icalSection(&p->
99670 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 7d 0a mutex);. }.
99680 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
99690 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 }. default:
996a0 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
996b0 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 3d winMutex_isInit=
996c0 3d 31 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 =1 );. asse
996d0 72 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 rt( iType-2 >= 0
996e0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
996f0 28 20 69 54 79 70 65 2d 32 20 3c 20 41 72 72 61 ( iType-2 < Arra
99700 79 53 69 7a 65 28 77 69 6e 4d 75 74 65 78 5f 73 ySize(winMutex_s
99710 74 61 74 69 63 4d 75 74 65 78 65 73 29 20 29 3b taticMutexes) );
99720 0a 20 20 20 20 20 20 70 20 3d 20 26 77 69 6e 4d . p = &winM
99730 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 utex_staticMutex
99740 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20 es[iType-2];.
99750 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 p->id = iType
99760 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
99770 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
99780 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 n p;.}.../*.** T
99790 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61 6c his routine deal
997a0 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 69 6f locates a previo
997b0 75 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 usly.** allocate
997c0 64 20 6d 75 74 65 78 2e 20 20 53 51 4c 69 74 65 d mutex. SQLite
997d0 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64 is careful to d
997e0 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 0a eallocate every.
997f0 2a 2a 20 6d 75 74 65 78 20 74 68 61 74 20 69 74 ** mutex that it
99800 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 allocates..*/.s
99810 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d 75 tatic void winMu
99820 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f texFree(sqlite3_
99830 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 mutex *p){. ass
99840 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 ert( p );. asse
99850 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 rt( p->nRef==0 )
99860 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 ;. assert( p->i
99870 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
99880 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 FAST || p->id==S
99890 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
998a0 52 53 49 56 45 20 29 3b 0a 20 20 44 65 6c 65 74 RSIVE );. Delet
998b0 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e eCriticalSection
998c0 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 (&p->mutex);. s
998d0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
998e0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
998f0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
99900 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d () and sqlite3_m
99910 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 utex_try() routi
99920 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 nes attempt.** t
99930 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e o enter a mutex.
99940 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 If another thr
99950 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 ead is already w
99960 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c ithin the mutex,
99970 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .** sqlite3_mute
99980 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 x_enter() will b
99990 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 lock and sqlite3
999a0 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c _mutex_try() wil
999b0 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 l return.** SQLI
999c0 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 TE_BUSY. The sq
999d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
999e0 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 ) interface retu
999f0 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a rns SQLITE_OK.**
99a00 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c upon successful
99a10 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 entry. Mutexes
99a20 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 created using S
99a30 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
99a40 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 RSIVE can.** be
99a50 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 entered multiple
99a60 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 times by the sa
99a70 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 me thread. In s
99a80 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a uch cases the,.*
99a90 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 * mutex must be
99aa0 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 exited an equal
99ab0 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 number of times
99ac0 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 before another t
99ad0 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 hread.** can ent
99ae0 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 er. If the same
99af0 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f thread tries to
99b00 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 enter any other
99b10 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a kind of mutex.*
99b20 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 * more than once
99b30 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 , the behavior i
99b40 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a s undefined..*/.
99b50 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d static void winM
99b60 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 utexEnter(sqlite
99b70 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 3_mutex *p){. a
99b80 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
99b90 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
99ba0 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 SIVE || winMutex
99bb0 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 Notheld(p) );.
99bc0 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 EnterCriticalSec
99bd0 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b tion(&p->mutex);
99be0 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 47 65 . p->owner = Ge
99bf0 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 tCurrentThreadId
99c00 28 29 3b 20 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b (); . p->nRef++
99c10 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 ;.}.static int w
99c20 69 6e 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 inMutexTry(sqlit
99c30 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
99c40 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
99c50 42 55 53 59 3b 0a 20 20 61 73 73 65 72 74 28 20 BUSY;. assert(
99c60 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
99c70 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c TEX_RECURSIVE ||
99c80 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 winMutexNotheld
99c90 28 70 29 20 29 3b 0a 20 20 2f 2a 0a 20 20 2a 2a (p) );. /*. **
99ca0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
99cb0 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65 ex_try() routine
99cc0 20 69 73 20 76 65 72 79 20 72 61 72 65 6c 79 20 is very rarely
99cd0 75 73 65 64 2c 20 61 6e 64 20 77 68 65 6e 20 69 used, and when i
99ce0 74 0a 20 20 2a 2a 20 69 73 20 75 73 65 64 20 69 t. ** is used i
99cf0 74 20 69 73 20 6d 65 72 65 6c 79 20 61 6e 20 6f t is merely an o
99d00 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 53 6f ptimization. So
99d10 20 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 69 74 it is OK for it
99d20 20 74 6f 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 to always. **
99d30 66 61 69 6c 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a fail. . **. *
99d40 2a 20 54 68 65 20 54 72 79 45 6e 74 65 72 43 72 * The TryEnterCr
99d50 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 iticalSection()
99d60 69 6e 74 65 72 66 61 63 65 20 69 73 20 6f 6e 6c interface is onl
99d70 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 57 y available on W
99d80 69 6e 4e 54 2e 0a 20 20 2a 2a 20 41 6e 64 20 73 inNT.. ** And s
99d90 6f 6d 65 20 77 69 6e 64 6f 77 73 20 63 6f 6d 70 ome windows comp
99da0 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 69 ilers complain i
99db0 66 20 79 6f 75 20 74 72 79 20 74 6f 20 75 73 65 f you try to use
99dc0 20 69 74 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a it without. **
99dd0 20 66 69 72 73 74 20 64 6f 69 6e 67 20 73 6f 6d first doing som
99de0 65 20 23 64 65 66 69 6e 65 73 20 74 68 61 74 20 e #defines that
99df0 70 72 65 76 65 6e 74 20 53 51 4c 69 74 65 20 66 prevent SQLite f
99e00 72 6f 6d 20 62 75 69 6c 64 69 6e 67 20 6f 6e 20 rom building on
99e10 57 69 6e 39 38 2e 0a 20 20 2a 2a 20 46 6f 72 20 Win98.. ** For
99e20 74 68 61 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 that reason, we
99e30 77 69 6c 6c 20 6f 6d 69 74 20 74 68 69 73 20 6f will omit this o
99e40 70 74 69 6d 69 7a 61 74 69 6f 6e 20 66 6f 72 20 ptimization for
99e50 6e 6f 77 2e 20 20 53 65 65 0a 20 20 2a 2a 20 74 now. See. ** t
99e60 69 63 6b 65 74 20 23 32 36 38 35 2e 0a 20 20 2a icket #2685.. *
99e70 2f 0a 23 69 66 20 30 0a 20 20 69 66 28 20 6d 75 /.#if 0. if( mu
99e80 74 65 78 49 73 4e 54 28 29 20 26 26 20 54 72 79 texIsNT() && Try
99e90 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 EnterCriticalSec
99ea0 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 20 tion(&p->mutex)
99eb0 29 7b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 ){. p->owner
99ec0 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 = GetCurrentThre
99ed0 61 64 49 64 28 29 3b 0a 20 20 20 20 70 2d 3e 6e adId();. p->n
99ee0 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 Ref++;. rc =
99ef0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 23 SQLITE_OK;. }.#
99f00 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 else. UNUSED_PA
99f10 52 41 4d 45 54 45 52 28 70 29 3b 0a 23 65 6e 64 RAMETER(p);.#end
99f20 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a if. return rc;.
99f30 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
99f40 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
99f50 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 () routine exits
99f60 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 a mutex that wa
99f70 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 s.** previously
99f80 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 entered by the s
99f90 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 ame thread. The
99fa0 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 behavior.** is
99fb0 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 undefined if the
99fc0 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 mutex is not cu
99fd0 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 rrently entered
99fe0 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 or.** is not cur
99ff0 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 rently allocated
9a000 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e . SQLite will n
9a010 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a ever do either..
9a020 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 */.static void w
9a030 69 6e 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c inMutexLeave(sql
9a040 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
9a050 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 assert( p->nRe
9a060 66 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 f>0 );. assert(
9a070 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 75 p->owner==GetCu
9a080 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29 20 rrentThreadId()
9a090 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a );. p->nRef--;.
9a0a0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 assert( p->nRe
9a0b0 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 f==0 || p->id==S
9a0c0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
9a0d0 52 53 49 56 45 20 29 3b 0a 20 20 4c 65 61 76 65 RSIVE );. Leave
9a0e0 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 CriticalSection(
9a0f0 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53 &p->mutex);.}..S
9a100 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 QLITE_PRIVATE sq
9a110 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
9a120 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 ods *sqlite3Defa
9a130 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a ultMutex(void){.
9a140 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 static sqlite3
9a150 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 _mutex_methods s
9a160 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 77 69 Mutex = {. wi
9a170 6e 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 nMutexInit,.
9a180 77 69 6e 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 winMutexEnd,.
9a190 20 77 69 6e 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a winMutexAlloc,.
9a1a0 20 20 20 20 77 69 6e 4d 75 74 65 78 46 72 65 65 winMutexFree
9a1b0 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45 6e ,. winMutexEn
9a1c0 74 65 72 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 ter,. winMute
9a1d0 78 54 72 79 2c 0a 20 20 20 20 77 69 6e 4d 75 74 xTry,. winMut
9a1e0 65 78 4c 65 61 76 65 2c 0a 23 69 66 64 65 66 20 exLeave,.#ifdef
9a1f0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 SQLITE_DEBUG.
9a200 20 77 69 6e 4d 75 74 65 78 48 65 6c 64 2c 0a 20 winMutexHeld,.
9a210 20 20 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 winMutexNothe
9a220 6c 64 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c 0a ld.#else. 0,.
9a230 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 20 20 7d 0.#endif. }
9a240 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 ;.. return &sMu
9a250 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a tex;.}.#endif /*
9a260 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 SQLITE_MUTEX_W3
9a270 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2 */../*********
9a280 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 ***** End of mut
9a290 65 78 5f 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a ex_w32.c *******
9a2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a2c0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
9a2d0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
9a2e0 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a malloc.c ******
9a2f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a310 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 ****/./*.** 2001
9a320 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a September 15.**
9a330 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
9a340 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
9a350 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
9a360 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
9a370 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
9a380 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
9a390 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
9a3a0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
9a3b0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
9a3c0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
9a3d0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
9a3e0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
9a3f0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
9a400 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
9a410 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
9a420 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
9a430 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
9a440 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
9a450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
9a490 2a 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ** Memory alloca
9a4a0 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 75 tion functions u
9a4b0 73 65 64 20 74 68 72 6f 75 67 68 6f 75 74 20 73 sed throughout s
9a4c0 71 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 qlite..**.** $Id
9a4d0 3a 20 6d 61 6c 6c 6f 63 2e 63 2c 76 20 31 2e 36 : malloc.c,v 1.6
9a4e0 36 20 32 30 30 39 2f 30 37 2f 31 37 20 31 31 3a 6 2009/07/17 11:
9a4f0 34 34 3a 30 37 20 64 72 68 20 45 78 70 20 24 0a 44:07 drh Exp $.
9a500 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 */../*.** This r
9a510 6f 75 74 69 6e 65 20 72 75 6e 73 20 77 68 65 6e outine runs when
9a520 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
9a530 63 61 74 6f 72 20 73 65 65 73 20 74 68 61 74 20 cator sees that
9a540 74 68 65 0a 2a 2a 20 74 6f 74 61 6c 20 6d 65 6d the.** total mem
9a550 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 ory allocation i
9a560 73 20 61 62 6f 75 74 20 74 6f 20 65 78 63 65 65 s about to excee
9a570 64 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 0a d the soft heap.
9a580 2a 2a 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 73 74 61 ** limit..*/.sta
9a590 74 69 63 20 76 6f 69 64 20 73 6f 66 74 48 65 61 tic void softHea
9a5a0 70 4c 69 6d 69 74 45 6e 66 6f 72 63 65 72 28 0a pLimitEnforcer(.
9a5b0 20 20 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c void *NotUsed,
9a5c0 20 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 . sqlite3_int6
9a5d0 34 20 4e 6f 74 55 73 65 64 32 2c 0a 20 20 69 6e 4 NotUsed2,. in
9a5e0 74 20 61 6c 6c 6f 63 53 69 7a 65 0a 29 7b 0a 20 t allocSize.){.
9a5f0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
9a600 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 R2(NotUsed, NotU
9a610 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 sed2);. sqlite3
9a620 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 _release_memory(
9a630 61 6c 6c 6f 63 53 69 7a 65 29 3b 0a 7d 0a 0a 2f allocSize);.}../
9a640 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 6f 66 *.** Set the sof
9a650 74 20 68 65 61 70 2d 73 69 7a 65 20 6c 69 6d 69 t heap-size limi
9a660 74 20 66 6f 72 20 74 68 65 20 6c 69 62 72 61 72 t for the librar
9a670 79 2e 20 50 61 73 73 69 6e 67 20 61 20 7a 65 72 y. Passing a zer
9a680 6f 20 6f 72 20 0a 2a 2a 20 6e 65 67 61 74 69 76 o or .** negativ
9a690 65 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 e value indicate
9a6a0 73 20 6e 6f 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53 s no limit..*/.S
9a6b0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
9a6c0 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 qlite3_soft_heap
9a6d0 5f 6c 69 6d 69 74 28 69 6e 74 20 6e 29 7b 0a 20 _limit(int n){.
9a6e0 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 sqlite3_uint64
9a6f0 69 4c 69 6d 69 74 3b 0a 20 20 69 6e 74 20 6f 76 iLimit;. int ov
9a700 65 72 61 67 65 3b 0a 20 20 69 66 28 20 6e 3c 30 erage;. if( n<0
9a710 20 29 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d ){. iLimit =
9a720 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
9a730 20 69 4c 69 6d 69 74 20 3d 20 6e 3b 0a 20 20 7d iLimit = n;. }
9a740 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
9a750 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 OMIT_AUTOINIT.
9a760 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
9a770 7a 65 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 ze();.#endif. i
9a780 66 28 20 69 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 f( iLimit>0 ){.
9a790 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 sqlite3Memory
9a7a0 41 6c 61 72 6d 28 73 6f 66 74 48 65 61 70 4c 69 Alarm(softHeapLi
9a7b0 6d 69 74 45 6e 66 6f 72 63 65 72 2c 20 30 2c 20 mitEnforcer, 0,
9a7c0 69 4c 69 6d 69 74 29 3b 0a 20 20 7d 65 6c 73 65 iLimit);. }else
9a7d0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d {. sqlite3Mem
9a7e0 6f 72 79 41 6c 61 72 6d 28 30 2c 20 30 2c 20 30 oryAlarm(0, 0, 0
9a7f0 29 3b 0a 20 20 7d 0a 20 20 6f 76 65 72 61 67 65 );. }. overage
9a800 20 3d 20 28 69 6e 74 29 28 73 71 6c 69 74 65 33 = (int)(sqlite3
9a810 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 20 2d _memory_used() -
9a820 20 28 69 36 34 29 6e 29 3b 0a 20 20 69 66 28 20 (i64)n);. if(
9a830 6f 76 65 72 61 67 65 3e 30 20 29 7b 0a 20 20 20 overage>0 ){.
9a840 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 sqlite3_release
9a850 5f 6d 65 6d 6f 72 79 28 6f 76 65 72 61 67 65 29 _memory(overage)
9a860 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 ;. }.}../*.** A
9a870 74 74 65 6d 70 74 20 74 6f 20 72 65 6c 65 61 73 ttempt to releas
9a880 65 20 75 70 20 74 6f 20 6e 20 62 79 74 65 73 20 e up to n bytes
9a890 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c of non-essential
9a8a0 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c memory currentl
9a8b0 79 0a 2a 2a 20 68 65 6c 64 20 62 79 20 53 51 4c y.** held by SQL
9a8c0 69 74 65 2e 20 41 6e 20 65 78 61 6d 70 6c 65 20 ite. An example
9a8d0 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c of non-essential
9a8e0 20 6d 65 6d 6f 72 79 20 69 73 20 6d 65 6d 6f 72 memory is memor
9a8f0 79 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 61 63 y used to.** cac
9a900 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 he database page
9a910 73 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 63 s that are not c
9a920 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e urrently in use.
9a930 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
9a940 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 nt sqlite3_relea
9a950 73 65 5f 6d 65 6d 6f 72 79 28 69 6e 74 20 6e 29 se_memory(int n)
9a960 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f {.#ifdef SQLITE_
9a970 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 ENABLE_MEMORY_MA
9a980 4e 41 47 45 4d 45 4e 54 0a 20 20 69 6e 74 20 6e NAGEMENT. int n
9a990 52 65 74 20 3d 20 30 3b 0a 20 20 6e 52 65 74 20 Ret = 0;. nRet
9a9a0 2b 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 += sqlite3Pcache
9a9b0 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 2d ReleaseMemory(n-
9a9c0 6e 52 65 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 nRet);. return
9a9d0 6e 52 65 74 3b 0a 23 65 6c 73 65 0a 20 20 55 4e nRet;.#else. UN
9a9e0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e USED_PARAMETER(n
9a9f0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
9aa00 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 7d 0a TE_OK;.#endif.}.
9aa10 0a 2f 2a 0a 2a 2a 20 53 74 61 74 65 20 69 6e 66 ./*.** State inf
9aa20 6f 72 6d 61 74 69 6f 6e 20 6c 6f 63 61 6c 20 74 ormation local t
9aa30 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c o the memory all
9aa40 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 ocation subsyste
9aa50 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c m..*/.static SQL
9aa60 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d ITE_WSD struct M
9aa70 65 6d 30 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a em0Global {. /*
9aa80 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 Number of free
9aa90 70 61 67 65 73 20 66 6f 72 20 73 63 72 61 74 63 pages for scratc
9aaa0 68 20 61 6e 64 20 70 61 67 65 2d 63 61 63 68 65 h and page-cache
9aab0 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 75 33 32 memory */. u32
9aac0 20 6e 53 63 72 61 74 63 68 46 72 65 65 3b 0a 20 nScratchFree;.
9aad0 20 75 33 32 20 6e 50 61 67 65 46 72 65 65 3b 0a u32 nPageFree;.
9aae0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
9aaf0 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 *mutex;
9ab00 20 2f 2a 20 4d 75 74 65 78 20 74 6f 20 73 65 72 /* Mutex to ser
9ab10 69 61 6c 69 7a 65 20 61 63 63 65 73 73 20 2a 2f ialize access */
9ab20 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 .. /*. ** The
9ab30 61 6c 61 72 6d 20 63 61 6c 6c 62 61 63 6b 20 61 alarm callback a
9ab40 6e 64 20 69 74 73 20 61 72 67 75 6d 65 6e 74 73 nd its arguments
9ab50 2e 20 20 54 68 65 20 6d 65 6d 30 2e 6d 75 74 65 . The mem0.mute
9ab60 78 20 6c 6f 63 6b 20 77 69 6c 6c 0a 20 20 2a 2a x lock will. **
9ab70 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20 74 be held while t
9ab80 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 72 he callback is r
9ab90 75 6e 6e 69 6e 67 2e 20 20 52 65 63 75 72 73 69 unning. Recursi
9aba0 76 65 20 63 61 6c 6c 73 20 69 6e 74 6f 0a 20 20 ve calls into.
9abb0 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 ** the memory su
9abc0 62 73 79 73 74 65 6d 20 61 72 65 20 61 6c 6c 6f bsystem are allo
9abd0 77 65 64 2c 20 62 75 74 20 6e 6f 20 6e 65 77 20 wed, but no new
9abe0 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20 62 callbacks will b
9abf0 65 0a 20 20 2a 2a 20 69 73 73 75 65 64 2e 0a 20 e. ** issued..
9ac00 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e */. sqlite3_in
9ac10 74 36 34 20 61 6c 61 72 6d 54 68 72 65 73 68 6f t64 alarmThresho
9ac20 6c 64 3b 0a 20 20 76 6f 69 64 20 28 2a 61 6c 61 ld;. void (*ala
9ac30 72 6d 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 rmCallback)(void
9ac40 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 *, sqlite3_int64
9ac50 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 2a 61 ,int);. void *a
9ac60 6c 61 72 6d 41 72 67 3b 0a 0a 20 20 2f 2a 0a 20 larmArg;.. /*.
9ac70 20 2a 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 ** Pointers to
9ac80 74 68 65 20 65 6e 64 20 6f 66 20 73 71 6c 69 74 the end of sqlit
9ac90 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
9aca0 53 63 72 61 74 63 68 20 61 6e 64 0a 20 20 2a 2a Scratch and. **
9acb0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
9acc0 6e 66 69 67 2e 70 50 61 67 65 20 74 6f 20 61 20 nfig.pPage to a
9acd0 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 block of memory
9ace0 74 68 61 74 20 72 65 63 6f 72 64 73 0a 20 20 2a that records. *
9acf0 2a 20 77 68 69 63 68 20 70 61 67 65 73 20 61 72 * which pages ar
9ad00 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a e available.. *
9ad10 2f 0a 20 20 75 33 32 20 2a 61 53 63 72 61 74 63 /. u32 *aScratc
9ad20 68 46 72 65 65 3b 0a 20 20 75 33 32 20 2a 61 50 hFree;. u32 *aP
9ad30 61 67 65 46 72 65 65 3b 0a 7d 20 6d 65 6d 30 20 ageFree;.} mem0
9ad40 3d 20 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 = { 0, 0, 0, 0,
9ad50 30 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 23 0, 0, 0, 0 };..#
9ad60 64 65 66 69 6e 65 20 6d 65 6d 30 20 47 4c 4f 42 define mem0 GLOB
9ad70 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 30 47 6c AL(struct Mem0Gl
9ad80 6f 62 61 6c 2c 20 6d 65 6d 30 29 0a 0a 2f 2a 0a obal, mem0)../*.
9ad90 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 ** Initialize th
9ada0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
9adb0 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a ion subsystem..*
9adc0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
9add0 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c int sqlite3Mall
9ade0 6f 63 49 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 ocInit(void){.
9adf0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
9ae00 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f lConfig.m.xMallo
9ae10 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 c==0 ){. sqli
9ae20 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 te3MemSetDefault
9ae30 28 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 ();. }. memset
9ae40 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f (&mem0, 0, sizeo
9ae50 66 28 6d 65 6d 30 29 29 3b 0a 20 20 69 66 28 20 f(mem0));. if(
9ae60 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9ae70 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 fig.bCoreMutex )
9ae80 7b 0a 20 20 20 20 6d 65 6d 30 2e 6d 75 74 65 78 {. mem0.mutex
9ae90 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 = sqlite3MutexA
9aea0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
9aeb0 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 X_STATIC_MEM);.
9aec0 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
9aed0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 GlobalConfig.pSc
9aee0 72 61 74 63 68 20 26 26 20 73 71 6c 69 74 65 33 ratch && sqlite3
9aef0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 GlobalConfig.szS
9af00 63 72 61 74 63 68 3e 3d 31 30 30 0a 20 20 20 20 cratch>=100.
9af10 20 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 && sqlite3Glob
9af20 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 alConfig.nScratc
9af30 68 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 h>=0 ){. int
9af40 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c i;. sqlite3Gl
9af50 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 obalConfig.szScr
9af60 61 74 63 68 20 3d 20 52 4f 55 4e 44 44 4f 57 4e atch = ROUNDDOWN
9af70 38 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 8(sqlite3GlobalC
9af80 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 2d onfig.szScratch-
9af90 34 29 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 53 63 4);. mem0.aSc
9afa0 72 61 74 63 68 46 72 65 65 20 3d 20 28 75 33 32 ratchFree = (u32
9afb0 2a 29 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 *)&((char*)sqlit
9afc0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
9afd0 53 63 72 61 74 63 68 29 0a 20 20 20 20 20 20 20 Scratch).
9afe0 20 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 [sqli
9aff0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9b000 73 7a 53 63 72 61 74 63 68 2a 73 71 6c 69 74 65 szScratch*sqlite
9b010 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 3GlobalConfig.nS
9b020 63 72 61 74 63 68 5d 3b 0a 20 20 20 20 66 6f 72 cratch];. for
9b030 28 69 3d 30 3b 20 69 3c 73 71 6c 69 74 65 33 47 (i=0; i<sqlite3G
9b040 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 lobalConfig.nScr
9b050 61 74 63 68 3b 20 69 2b 2b 29 7b 20 6d 65 6d 30 atch; i++){ mem0
9b060 2e 61 53 63 72 61 74 63 68 46 72 65 65 5b 69 5d .aScratchFree[i]
9b070 20 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 = i; }. mem0
9b080 2e 6e 53 63 72 61 74 63 68 46 72 65 65 20 3d 20 .nScratchFree =
9b090 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9b0a0 66 69 67 2e 6e 53 63 72 61 74 63 68 3b 0a 20 20 fig.nScratch;.
9b0b0 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 }else{. sqlit
9b0c0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
9b0d0 53 63 72 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 Scratch = 0;.
9b0e0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
9b0f0 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 20 3d nfig.szScratch =
9b100 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 0;. }. if( sq
9b110 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b120 67 2e 70 50 61 67 65 20 26 26 20 73 71 6c 69 74 g.pPage && sqlit
9b130 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 e3GlobalConfig.s
9b140 7a 50 61 67 65 3e 3d 35 31 32 0a 20 20 20 20 20 zPage>=512.
9b150 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 && sqlite3Globa
9b160 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3e 3d 31 lConfig.nPage>=1
9b170 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 ){. int i;.
9b180 20 20 20 69 6e 74 20 6f 76 65 72 68 65 61 64 3b int overhead;
9b190 0a 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 52 4f . int sz = RO
9b1a0 55 4e 44 44 4f 57 4e 38 28 73 71 6c 69 74 65 33 UNDDOWN8(sqlite3
9b1b0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 GlobalConfig.szP
9b1c0 61 67 65 29 3b 0a 20 20 20 20 69 6e 74 20 6e 20 age);. int n
9b1d0 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
9b1e0 6f 6e 66 69 67 2e 6e 50 61 67 65 3b 0a 20 20 20 onfig.nPage;.
9b1f0 20 6f 76 65 72 68 65 61 64 20 3d 20 28 34 2a 6e overhead = (4*n
9b200 20 2b 20 73 7a 20 2d 20 31 29 2f 73 7a 3b 0a 20 + sz - 1)/sz;.
9b210 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sqlite3Global
9b220 43 6f 6e 66 69 67 2e 6e 50 61 67 65 20 2d 3d 20 Config.nPage -=
9b230 6f 76 65 72 68 65 61 64 3b 0a 20 20 20 20 6d 65 overhead;. me
9b240 6d 30 2e 61 50 61 67 65 46 72 65 65 20 3d 20 28 m0.aPageFree = (
9b250 75 33 32 2a 29 26 28 28 63 68 61 72 2a 29 73 71 u32*)&((char*)sq
9b260 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b270 67 2e 70 50 61 67 65 29 0a 20 20 20 20 20 20 20 g.pPage).
9b280 20 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 [sqli
9b290 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9b2a0 73 7a 50 61 67 65 2a 73 71 6c 69 74 65 33 47 6c szPage*sqlite3Gl
9b2b0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 obalConfig.nPage
9b2c0 5d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 ];. for(i=0;
9b2d0 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 i<sqlite3GlobalC
9b2e0 6f 6e 66 69 67 2e 6e 50 61 67 65 3b 20 69 2b 2b onfig.nPage; i++
9b2f0 29 7b 20 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 ){ mem0.aPageFre
9b300 65 5b 69 5d 20 3d 20 69 3b 20 7d 0a 20 20 20 20 e[i] = i; }.
9b310 6d 65 6d 30 2e 6e 50 61 67 65 46 72 65 65 20 3d mem0.nPageFree =
9b320 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
9b330 6e 66 69 67 2e 6e 50 61 67 65 3b 0a 20 20 7d 65 nfig.nPage;. }e
9b340 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 lse{. sqlite3
9b350 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 GlobalConfig.pPa
9b360 67 65 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 ge = 0;. sqli
9b370 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9b380 73 7a 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a szPage = 0;. }.
9b390 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
9b3a0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
9b3b0 49 6e 69 74 28 73 71 6c 69 74 65 33 47 6c 6f 62 Init(sqlite3Glob
9b3c0 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 alConfig.m.pAppD
9b3d0 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 ata);.}../*.** D
9b3e0 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 einitialize the
9b3f0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
9b400 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a n subsystem..*/.
9b410 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
9b420 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f oid sqlite3Mallo
9b430 63 45 6e 64 28 76 6f 69 64 29 7b 0a 20 20 69 66 cEnd(void){. if
9b440 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
9b450 6f 6e 66 69 67 2e 6d 2e 78 53 68 75 74 64 6f 77 onfig.m.xShutdow
9b460 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 n ){. sqlite3
9b470 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
9b480 53 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 Shutdown(sqlite3
9b490 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 GlobalConfig.m.p
9b4a0 41 70 70 44 61 74 61 29 3b 0a 20 20 7d 0a 20 20 AppData);. }.
9b4b0 6d 65 6d 73 65 74 28 26 6d 65 6d 30 2c 20 30 2c memset(&mem0, 0,
9b4c0 20 73 69 7a 65 6f 66 28 6d 65 6d 30 29 29 3b 0a sizeof(mem0));.
9b4d0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
9b4e0 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 the amount of me
9b4f0 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 20 63 mory currently c
9b500 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53 hecked out..*/.S
9b510 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
9b520 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 3_int64 sqlite3_
9b530 6d 65 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 memory_used(void
9b540 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a ){. int n, mx;.
9b550 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
9b560 72 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 res;. sqlite3_s
9b570 74 61 74 75 73 28 53 51 4c 49 54 45 5f 53 54 41 tatus(SQLITE_STA
9b580 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c TUS_MEMORY_USED,
9b590 20 26 6e 2c 20 26 6d 78 2c 20 30 29 3b 0a 20 20 &n, &mx, 0);.
9b5a0 72 65 73 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 res = (sqlite3_i
9b5b0 6e 74 36 34 29 6e 3b 20 20 2f 2a 20 57 6f 72 6b nt64)n; /* Work
9b5c0 20 61 72 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 around bug in B
9b5d0 6f 72 6c 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 orland C. Ticket
9b5e0 20 23 33 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 #3216 */. retu
9b5f0 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a rn res;.}../*.**
9b600 20 52 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 Return the maxi
9b610 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 mum amount of me
9b620 6d 6f 72 79 20 74 68 61 74 20 68 61 73 20 65 76 mory that has ev
9b630 65 72 20 62 65 65 6e 0a 2a 2a 20 63 68 65 63 6b er been.** check
9b640 65 64 20 6f 75 74 20 73 69 6e 63 65 20 65 69 74 ed out since eit
9b650 68 65 72 20 74 68 65 20 62 65 67 69 6e 6e 69 6e her the beginnin
9b660 67 20 6f 66 20 74 68 69 73 20 70 72 6f 63 65 73 g of this proces
9b670 73 0a 2a 2a 20 6f 72 20 73 69 6e 63 65 20 74 68 s.** or since th
9b680 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 72 65 e most recent re
9b690 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 set..*/.SQLITE_A
9b6a0 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 PI sqlite3_int64
9b6b0 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f sqlite3_memory_
9b6c0 68 69 67 68 77 61 74 65 72 28 69 6e 74 20 72 65 highwater(int re
9b6d0 73 65 74 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 setFlag){. int
9b6e0 6e 2c 20 6d 78 3b 0a 20 20 73 71 6c 69 74 65 33 n, mx;. sqlite3
9b6f0 5f 69 6e 74 36 34 20 72 65 73 3b 0a 20 20 73 71 _int64 res;. sq
9b700 6c 69 74 65 33 5f 73 74 61 74 75 73 28 53 51 4c lite3_status(SQL
9b710 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 ITE_STATUS_MEMOR
9b720 59 5f 55 53 45 44 2c 20 26 6e 2c 20 26 6d 78 2c Y_USED, &n, &mx,
9b730 20 72 65 73 65 74 46 6c 61 67 29 3b 0a 20 20 72 resetFlag);. r
9b740 65 73 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e es = (sqlite3_in
9b750 74 36 34 29 6d 78 3b 20 20 2f 2a 20 57 6f 72 6b t64)mx; /* Work
9b760 20 61 72 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 around bug in B
9b770 6f 72 6c 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 orland C. Ticket
9b780 20 23 33 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 #3216 */. retu
9b790 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a rn res;.}../*.**
9b7a0 20 43 68 61 6e 67 65 20 74 68 65 20 61 6c 61 72 Change the alar
9b7b0 6d 20 63 61 6c 6c 62 61 63 6b 0a 2a 2f 0a 53 51 m callback.*/.SQ
9b7c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
9b7d0 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c sqlite3MemoryAl
9b7e0 61 72 6d 28 0a 20 20 76 6f 69 64 28 2a 78 43 61 arm(. void(*xCa
9b7f0 6c 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a 70 41 llback)(void *pA
9b800 72 67 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 rg, sqlite3_int6
9b810 34 20 75 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 4 used,int N),.
9b820 20 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20 73 void *pArg,. s
9b830 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 54 68 qlite3_int64 iTh
9b840 72 65 73 68 6f 6c 64 0a 29 7b 0a 20 20 73 71 6c reshold.){. sql
9b850 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
9b860 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
9b870 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 mem0.alarmCallba
9b880 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a ck = xCallback;.
9b890 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 20 mem0.alarmArg
9b8a0 3d 20 70 41 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 = pArg;. mem0.a
9b8b0 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 3d 20 larmThreshold =
9b8c0 69 54 68 72 65 73 68 6f 6c 64 3b 0a 20 20 73 71 iThreshold;. sq
9b8d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
9b8e0 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 e(mem0.mutex);.
9b8f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
9b900 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 K;.}..#ifndef SQ
9b910 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 LITE_OMIT_DEPREC
9b920 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 ATED./*.** Depre
9b930 63 61 74 65 64 20 65 78 74 65 72 6e 61 6c 20 69 cated external i
9b940 6e 74 65 72 66 61 63 65 2e 20 20 49 6e 74 65 72 nterface. Inter
9b950 6e 61 6c 2f 63 6f 72 65 20 53 51 4c 69 74 65 20 nal/core SQLite
9b960 63 6f 64 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 code.** should c
9b970 61 6c 6c 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 all sqlite3Memor
9b980 79 41 6c 61 72 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 yAlarm..*/.SQLIT
9b990 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
9b9a0 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 0a 3_memory_alarm(.
9b9b0 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 63 void(*xCallbac
9b9c0 6b 29 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 73 k)(void *pArg, s
9b9d0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 75 73 65 qlite3_int64 use
9b9e0 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 64 d,int N),. void
9b9f0 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 65 *pArg,. sqlite
9ba00 33 5f 69 6e 74 36 34 20 69 54 68 72 65 73 68 6f 3_int64 iThresho
9ba10 6c 64 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 ld.){. return s
9ba20 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 qlite3MemoryAlar
9ba30 6d 28 78 43 61 6c 6c 62 61 63 6b 2c 20 70 41 72 m(xCallback, pAr
9ba40 67 2c 20 69 54 68 72 65 73 68 6f 6c 64 29 3b 0a g, iThreshold);.
9ba50 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
9ba60 54 72 69 67 67 65 72 20 74 68 65 20 61 6c 61 72 Trigger the alar
9ba70 6d 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 m .*/.static voi
9ba80 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 d sqlite3MallocA
9ba90 6c 61 72 6d 28 69 6e 74 20 6e 42 79 74 65 29 7b larm(int nByte){
9baa0 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 . void (*xCallb
9bab0 61 63 6b 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 ack)(void*,sqlit
9bac0 65 33 5f 69 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 e3_int64,int);.
9bad0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e sqlite3_int64 n
9bae0 6f 77 55 73 65 64 3b 0a 20 20 76 6f 69 64 20 2a owUsed;. void *
9baf0 70 41 72 67 3b 0a 20 20 69 66 28 20 6d 65 6d 30 pArg;. if( mem0
9bb00 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 3d 3d .alarmCallback==
9bb10 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 78 43 0 ) return;. xC
9bb20 61 6c 6c 62 61 63 6b 20 3d 20 6d 65 6d 30 2e 61 allback = mem0.a
9bb30 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 larmCallback;.
9bb40 6e 6f 77 55 73 65 64 20 3d 20 73 71 6c 69 74 65 nowUsed = sqlite
9bb50 33 53 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 3StatusValue(SQL
9bb60 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 ITE_STATUS_MEMOR
9bb70 59 5f 55 53 45 44 29 3b 0a 20 20 70 41 72 67 20 Y_USED);. pArg
9bb80 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 3b = mem0.alarmArg;
9bb90 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c . mem0.alarmCal
9bba0 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 73 71 6c lback = 0;. sql
9bbb0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
9bbc0 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
9bbd0 78 43 61 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20 xCallback(pArg,
9bbe0 6e 6f 77 55 73 65 64 2c 20 6e 42 79 74 65 29 3b nowUsed, nByte);
9bbf0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
9bc00 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 _enter(mem0.mute
9bc10 78 29 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d x);. mem0.alarm
9bc20 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c Callback = xCall
9bc30 62 61 63 6b 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 back;. mem0.ala
9bc40 72 6d 41 72 67 20 3d 20 70 41 72 67 3b 0a 7d 0a rmArg = pArg;.}.
9bc50 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20 6d 65 6d 6f ./*.** Do a memo
9bc60 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 69 ry allocation wi
9bc70 74 68 20 73 74 61 74 69 73 74 69 63 73 20 61 6e th statistics an
9bc80 64 20 61 6c 61 72 6d 73 2e 20 20 41 73 73 75 6d d alarms. Assum
9bc90 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 e the.** lock is
9bca0 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2e 0a 2a already held..*
9bcb0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 61 6c /.static int mal
9bcc0 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 69 6e 74 locWithAlarm(int
9bcd0 20 6e 2c 20 76 6f 69 64 20 2a 2a 70 70 29 7b 0a n, void **pp){.
9bce0 20 20 69 6e 74 20 6e 46 75 6c 6c 3b 0a 20 20 76 int nFull;. v
9bcf0 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 oid *p;. assert
9bd00 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
9bd10 68 65 6c 64 28 6d 65 6d 30 2e 6d 75 74 65 78 29 held(mem0.mutex)
9bd20 20 29 3b 0a 20 20 6e 46 75 6c 6c 20 3d 20 73 71 );. nFull = sq
9bd30 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9bd40 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 28 6e 29 3b g.m.xRoundup(n);
9bd50 0a 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 . sqlite3Status
9bd60 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Set(SQLITE_STATU
9bd70 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e S_MALLOC_SIZE, n
9bd80 29 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61 6c );. if( mem0.al
9bd90 61 72 6d 43 61 6c 6c 62 61 63 6b 21 3d 30 20 29 armCallback!=0 )
9bda0 7b 0a 20 20 20 20 69 6e 74 20 6e 55 73 65 64 20 {. int nUsed
9bdb0 3d 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 = sqlite3StatusV
9bdc0 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 41 54 alue(SQLITE_STAT
9bdd0 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29 3b US_MEMORY_USED);
9bde0 0a 20 20 20 20 69 66 28 20 6e 55 73 65 64 2b 6e . if( nUsed+n
9bdf0 46 75 6c 6c 20 3e 3d 20 6d 65 6d 30 2e 61 6c 61 Full >= mem0.ala
9be00 72 6d 54 68 72 65 73 68 6f 6c 64 20 29 7b 0a 20 rmThreshold ){.
9be10 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c sqlite3Mall
9be20 6f 63 41 6c 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a ocAlarm(nFull);.
9be30 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 }. }. p =
9be40 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9be50 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 46 fig.m.xMalloc(nF
9be60 75 6c 6c 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 ull);. if( p==0
9be70 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 && mem0.alarmCa
9be80 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 73 71 llback ){. sq
9be90 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d lite3MallocAlarm
9bea0 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 70 20 3d (nFull);. p =
9beb0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
9bec0 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e nfig.m.xMalloc(n
9bed0 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 69 66 28 Full);. }. if(
9bee0 20 70 20 29 7b 0a 20 20 20 20 6e 46 75 6c 6c 20 p ){. nFull
9bef0 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 = sqlite3MallocS
9bf00 69 7a 65 28 70 29 3b 0a 20 20 20 20 73 71 6c 69 ize(p);. sqli
9bf10 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c te3StatusAdd(SQL
9bf20 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 ITE_STATUS_MEMOR
9bf30 59 5f 55 53 45 44 2c 20 6e 46 75 6c 6c 29 3b 0a Y_USED, nFull);.
9bf40 20 20 7d 0a 20 20 2a 70 70 20 3d 20 70 3b 0a 20 }. *pp = p;.
9bf50 20 72 65 74 75 72 6e 20 6e 46 75 6c 6c 3b 0a 7d return nFull;.}
9bf60 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 ../*.** Allocate
9bf70 20 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 72 memory. This r
9bf80 6f 75 74 69 6e 65 20 69 73 20 6c 69 6b 65 20 73 outine is like s
9bf90 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
9bfa0 65 78 63 65 70 74 20 74 68 61 74 20 69 74 0a 2a except that it.*
9bfb0 2a 20 61 73 73 75 6d 65 73 20 74 68 65 20 6d 65 * assumes the me
9bfc0 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d 20 68 mory subsystem h
9bfd0 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
9bfe0 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a initialized..*/.
9bff0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
9c000 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c oid *sqlite3Mall
9c010 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 oc(int n){. voi
9c020 64 20 2a 70 3b 0a 20 20 69 66 28 20 6e 3c 3d 30 d *p;. if( n<=0
9c030 20 7c 7c 20 6e 3e 3d 30 78 37 66 66 66 66 66 30 || n>=0x7fffff0
9c040 30 20 29 7b 0a 20 20 20 20 2f 2a 20 41 20 6d 65 0 ){. /* A me
9c050 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
9c060 6f 66 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 62 of a number of b
9c070 79 74 65 73 20 77 68 69 63 68 20 69 73 20 6e 65 ytes which is ne
9c080 61 72 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 20 ar the maximum.
9c090 20 20 20 2a 2a 20 73 69 67 6e 65 64 20 69 6e 74 ** signed int
9c0a0 65 67 65 72 20 76 61 6c 75 65 20 6d 69 67 68 74 eger value might
9c0b0 20 63 61 75 73 65 20 61 6e 20 69 6e 74 65 67 65 cause an intege
9c0c0 72 20 6f 76 65 72 66 6c 6f 77 20 69 6e 73 69 64 r overflow insid
9c0d0 65 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 e of the. **
9c0e0 78 4d 61 6c 6c 6f 63 28 29 2e 20 20 48 65 6e 63 xMalloc(). Henc
9c0f0 65 20 77 65 20 6c 69 6d 69 74 20 74 68 65 20 6d e we limit the m
9c100 61 78 69 6d 75 6d 20 73 69 7a 65 20 74 6f 20 30 aximum size to 0
9c110 78 37 66 66 66 66 66 30 30 2c 20 67 69 76 69 6e x7fffff00, givin
9c120 67 0a 20 20 20 20 2a 2a 20 32 35 35 20 62 79 74 g. ** 255 byt
9c130 65 73 20 6f 66 20 6f 76 65 72 68 65 61 64 2e 20 es of overhead.
9c140 20 53 51 4c 69 74 65 20 69 74 73 65 6c 66 20 77 SQLite itself w
9c150 69 6c 6c 20 6e 65 76 65 72 20 75 73 65 20 61 6e ill never use an
9c160 79 74 68 69 6e 67 20 6e 65 61 72 0a 20 20 20 20 ything near.
9c170 2a 2a 20 74 68 69 73 20 61 6d 6f 75 6e 74 2e 20 ** this amount.
9c180 20 54 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f The only way to
9c190 20 72 65 61 63 68 20 74 68 65 20 6c 69 6d 69 74 reach the limit
9c1a0 20 69 73 20 77 69 74 68 20 73 71 6c 69 74 65 33 is with sqlite3
9c1b0 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 20 _malloc() */.
9c1c0 20 70 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 p = 0;. }else
9c1d0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
9c1e0 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 lConfig.bMemstat
9c1f0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
9c200 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 mutex_enter(mem0
9c210 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 6d 61 6c .mutex);. mal
9c220 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20 locWithAlarm(n,
9c230 26 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 &p);. sqlite3
9c240 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d _mutex_leave(mem
9c250 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 0.mutex);. }els
9c260 65 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 e{. p = sqlit
9c270 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
9c280 2e 78 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d .xMalloc(n);. }
9c290 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
9c2a0 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 /*.** This versi
9c2b0 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 on of the memory
9c2c0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 allocation is f
9c2d0 6f 72 20 75 73 65 20 62 79 20 74 68 65 20 61 70 or use by the ap
9c2e0 70 6c 69 63 61 74 69 6f 6e 2e 0a 2a 2a 20 46 69 plication..** Fi
9c2f0 72 73 74 20 6d 61 6b 65 20 73 75 72 65 20 74 68 rst make sure th
9c300 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 e memory subsyst
9c310 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 em is initialize
9c320 64 2c 20 74 68 65 6e 20 64 6f 20 74 68 65 0a 2a d, then do the.*
9c330 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f * allocation..*/
9c340 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
9c350 20 2a 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 *sqlite3_malloc
9c360 28 69 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65 66 (int n){.#ifndef
9c370 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
9c380 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 OINIT. if( sqli
9c390 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
9c3a0 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e ) return 0;.#en
9c3b0 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c dif. return sql
9c3c0 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 7d ite3Malloc(n);.}
9c3d0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 68 72 ../*.** Each thr
9c3e0 65 61 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76 ead may only hav
9c3f0 65 20 61 20 73 69 6e 67 6c 65 20 6f 75 74 73 74 e a single outst
9c400 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f anding allocatio
9c410 6e 20 66 72 6f 6d 0a 2a 2a 20 78 53 63 72 61 74 n from.** xScrat
9c420 63 68 4d 61 6c 6c 6f 63 28 29 2e 20 20 57 65 20 chMalloc(). We
9c430 76 65 72 69 66 79 20 74 68 69 73 20 63 6f 6e 73 verify this cons
9c440 74 72 61 69 6e 74 20 69 6e 20 74 68 65 20 73 69 traint in the si
9c450 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a ngle-threaded.**
9c460 20 63 61 73 65 20 62 79 20 73 65 74 74 69 6e 67 case by setting
9c470 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 scratchAllocOut
9c480 20 74 6f 20 31 20 77 68 65 6e 20 61 6e 20 61 6c to 1 when an al
9c490 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f location.** is o
9c4a0 75 74 73 74 61 6e 64 69 6e 67 20 63 6c 65 61 72 utstanding clear
9c4b0 69 6e 67 20 69 74 20 77 68 65 6e 20 74 68 65 20 ing it when the
9c4c0 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 allocation is fr
9c4d0 65 65 64 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 eed..*/.#if SQLI
9c4e0 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 TE_THREADSAFE==0
9c4f0 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 && !defined(NDE
9c500 42 55 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20 BUG).static int
9c510 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 scratchAllocOut
9c520 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a = 0;.#endif.../*
9c530 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d .** Allocate mem
9c540 6f 72 79 20 74 68 61 74 20 69 73 20 74 6f 20 62 ory that is to b
9c550 65 20 75 73 65 64 20 61 6e 64 20 72 65 6c 65 61 e used and relea
9c560 73 65 64 20 72 69 67 68 74 20 61 77 61 79 2e 0a sed right away..
9c570 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
9c580 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 61 6c is similar to al
9c590 6c 6f 63 61 28 29 20 69 6e 20 74 68 61 74 20 69 loca() in that i
9c5a0 74 20 69 73 20 6e 6f 74 20 69 6e 74 65 6e 64 65 t is not intende
9c5b0 64 0a 2a 2a 20 66 6f 72 20 73 69 74 75 61 74 69 d.** for situati
9c5c0 6f 6e 73 20 77 68 65 72 65 20 74 68 65 20 6d 65 ons where the me
9c5d0 6d 6f 72 79 20 6d 69 67 68 74 20 62 65 20 68 65 mory might be he
9c5e0 6c 64 20 6c 6f 6e 67 2d 74 65 72 6d 2e 20 20 54 ld long-term. T
9c5f0 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 his.** routine i
9c600 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 67 65 s intended to ge
9c610 74 20 6d 65 6d 6f 72 79 20 74 6f 20 6f 6c 64 20 t memory to old
9c620 6c 61 72 67 65 20 74 72 61 6e 73 69 65 6e 74 20 large transient
9c630 64 61 74 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 data.** structur
9c640 65 73 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 6f es that would no
9c650 74 20 6e 6f 72 6d 61 6c 6c 79 20 66 69 74 20 6f t normally fit o
9c660 6e 20 74 68 65 20 73 74 61 63 6b 20 6f 66 20 61 n the stack of a
9c670 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64 20 70 72 n.** embedded pr
9c680 6f 63 65 73 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 ocessor..*/.SQLI
9c690 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
9c6a0 2a 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d *sqlite3ScratchM
9c6b0 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 alloc(int n){.
9c6c0 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 void *p;. asser
9c6d0 74 28 20 6e 3e 30 20 29 3b 0a 0a 23 69 66 20 53 t( n>0 );..#if S
9c6e0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
9c6f0 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 ==0 && !defined(
9c700 4e 44 45 42 55 47 29 0a 20 20 2f 2a 20 56 65 72 NDEBUG). /* Ver
9c710 69 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 ify that no more
9c720 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 than one scratc
9c730 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 h allocation per
9c740 20 74 68 72 65 61 64 0a 20 20 2a 2a 20 69 73 20 thread. ** is
9c750 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f outstanding at o
9c760 6e 65 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20 ne time. (This
9c770 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 is only checked
9c780 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 69 6e 67 in the. ** sing
9c790 6c 65 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 le-threaded case
9c7a0 20 73 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 since checking
9c7b0 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 in the multi-thr
9c7c0 65 61 64 65 64 20 63 61 73 65 0a 20 20 2a 2a 20 eaded case. **
9c7d0 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f would be much mo
9c7e0 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29 re complicated.)
9c7f0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 63 */. assert( sc
9c800 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 3d 3d 30 ratchAllocOut==0
9c810 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 );.#endif.. if
9c820 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
9c830 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3c onfig.szScratch<
9c840 6e 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 63 n ){. goto sc
9c850 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a ratch_overflow;.
9c860 20 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 73 }else{ . s
9c870 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
9c880 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a er(mem0.mutex);.
9c890 20 20 20 20 69 66 28 20 6d 65 6d 30 2e 6e 53 63 if( mem0.nSc
9c8a0 72 61 74 63 68 46 72 65 65 3d 3d 30 20 29 7b 0a ratchFree==0 ){.
9c8b0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
9c8c0 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d tex_leave(mem0.m
9c8d0 75 74 65 78 29 3b 0a 20 20 20 20 20 20 67 6f 74 utex);. got
9c8e0 6f 20 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c o scratch_overfl
9c8f0 6f 77 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ow;. }else{.
9c900 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 int i;.
9c910 20 20 69 20 3d 20 6d 65 6d 30 2e 61 53 63 72 61 i = mem0.aScra
9c920 74 63 68 46 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e tchFree[--mem0.n
9c930 53 63 72 61 74 63 68 46 72 65 65 5d 3b 0a 20 20 ScratchFree];.
9c940 20 20 20 20 69 20 2a 3d 20 73 71 6c 69 74 65 33 i *= sqlite3
9c950 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 GlobalConfig.szS
9c960 63 72 61 74 63 68 3b 0a 20 20 20 20 20 20 73 71 cratch;. sq
9c970 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 lite3StatusAdd(S
9c980 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 QLITE_STATUS_SCR
9c990 41 54 43 48 5f 55 53 45 44 2c 20 31 29 3b 0a 20 ATCH_USED, 1);.
9c9a0 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 sqlite3Stat
9c9b0 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 usSet(SQLITE_STA
9c9c0 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 TUS_SCRATCH_SIZE
9c9d0 2c 20 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 , n);. sqli
9c9e0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
9c9f0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 mem0.mutex);.
9ca00 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 26 28 p = (void*)&(
9ca10 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c (char*)sqlite3Gl
9ca20 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 obalConfig.pScra
9ca30 74 63 68 29 5b 69 5d 3b 0a 20 20 20 20 20 20 61 tch)[i];. a
9ca40 73 73 65 72 74 28 20 20 28 28 28 75 38 2a 29 70 ssert( (((u8*)p
9ca50 20 2d 20 28 75 38 2a 29 30 29 20 26 20 37 29 3d - (u8*)0) & 7)=
9ca60 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a =0 );. }. }.
9ca70 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
9ca80 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66 DSAFE==0 && !def
9ca90 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20 73 ined(NDEBUG). s
9caa0 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d cratchAllocOut =
9cab0 20 70 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 p!=0;.#endif..
9cac0 20 72 65 74 75 72 6e 20 70 3b 0a 0a 73 63 72 61 return p;..scra
9cad0 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3a 0a 20 20 tch_overflow:.
9cae0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
9caf0 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 lConfig.bMemstat
9cb00 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
9cb10 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 mutex_enter(mem0
9cb20 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c .mutex);. sql
9cb30 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 ite3StatusSet(SQ
9cb40 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 LITE_STATUS_SCRA
9cb50 54 43 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 TCH_SIZE, n);.
9cb60 20 20 6e 20 3d 20 6d 61 6c 6c 6f 63 57 69 74 68 n = mallocWith
9cb70 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a 20 20 Alarm(n, &p);.
9cb80 20 20 69 66 28 20 70 20 29 20 73 71 6c 69 74 65 if( p ) sqlite
9cb90 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 3StatusAdd(SQLIT
9cba0 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 E_STATUS_SCRATCH
9cbb0 5f 4f 56 45 52 46 4c 4f 57 2c 20 6e 29 3b 0a 20 _OVERFLOW, n);.
9cbc0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
9cbd0 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 _leave(mem0.mute
9cbe0 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 x);. }else{.
9cbf0 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 p = sqlite3Glob
9cc00 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c alConfig.m.xMall
9cc10 6f 63 28 6e 29 3b 0a 20 20 7d 0a 23 69 66 20 53 oc(n);. }.#if S
9cc20 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
9cc30 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 ==0 && !defined(
9cc40 4e 44 45 42 55 47 29 0a 20 20 73 63 72 61 74 63 NDEBUG). scratc
9cc50 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d 30 hAllocOut = p!=0
9cc60 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 ;.#endif. retur
9cc70 6e 20 70 3b 20 20 20 20 0a 7d 0a 53 51 4c 49 54 n p; .}.SQLIT
9cc80 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
9cc90 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 72 65 qlite3ScratchFre
9cca0 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66 e(void *p){. if
9ccb0 28 20 70 20 29 7b 0a 0a 23 69 66 20 53 51 4c 49 ( p ){..#if SQLI
9ccc0 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 TE_THREADSAFE==0
9ccd0 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 && !defined(NDE
9cce0 42 55 47 29 0a 20 20 20 20 2f 2a 20 56 65 72 69 BUG). /* Veri
9ccf0 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 fy that no more
9cd00 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 than one scratch
9cd10 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 allocation per
9cd20 74 68 72 65 61 64 0a 20 20 20 20 2a 2a 20 69 73 thread. ** is
9cd30 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20 outstanding at
9cd40 6f 6e 65 20 74 69 6d 65 2e 20 20 28 54 68 69 73 one time. (This
9cd50 20 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 is only checked
9cd60 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 in the. ** s
9cd70 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 63 ingle-threaded c
9cd80 61 73 65 20 73 69 6e 63 65 20 63 68 65 63 6b 69 ase since checki
9cd90 6e 67 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d ng in the multi-
9cda0 74 68 72 65 61 64 65 64 20 63 61 73 65 0a 20 20 threaded case.
9cdb0 20 20 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d 75 ** would be mu
9cdc0 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61 ch more complica
9cdd0 74 65 64 2e 29 20 2a 2f 0a 20 20 20 20 61 73 73 ted.) */. ass
9cde0 65 72 74 28 20 73 63 72 61 74 63 68 41 6c 6c 6f ert( scratchAllo
9cdf0 63 4f 75 74 3d 3d 31 20 29 3b 0a 20 20 20 20 73 cOut==1 );. s
9ce00 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d cratchAllocOut =
9ce10 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 0;.#endif..
9ce20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
9ce30 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 lConfig.pScratch
9ce40 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 7c ==0. |
9ce50 7c 20 70 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 | p<sqlite3Globa
9ce60 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 lConfig.pScratch
9ce70 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 . || p
9ce80 3e 3d 28 76 6f 69 64 2a 29 6d 65 6d 30 2e 61 53 >=(void*)mem0.aS
9ce90 63 72 61 74 63 68 46 72 65 65 20 29 7b 0a 20 20 cratchFree ){.
9cea0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 if( sqlite3G
9ceb0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d lobalConfig.bMem
9cec0 73 74 61 74 20 29 7b 0a 20 20 20 20 20 20 20 20 stat ){.
9ced0 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69 int iSize = sqli
9cee0 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 te3MallocSize(p)
9cef0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
9cf00 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 3_mutex_enter(me
9cf10 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 m0.mutex);.
9cf20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
9cf30 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Add(SQLITE_STATU
9cf40 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c S_SCRATCH_OVERFL
9cf50 4f 57 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 OW, -iSize);.
9cf60 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 sqlite3Stat
9cf70 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 usAdd(SQLITE_STA
9cf80 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c TUS_MEMORY_USED,
9cf90 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 -iSize);.
9cfa0 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
9cfb0 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 onfig.m.xFree(p)
9cfc0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
9cfd0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
9cfe0 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 m0.mutex);.
9cff0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
9d000 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9d010 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a fig.m.xFree(p);.
9d020 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
9d030 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a e{. int i;.
9d040 20 20 20 20 20 20 69 20 3d 20 28 69 6e 74 29 28 i = (int)(
9d050 28 75 38 2a 29 70 20 2d 20 28 75 38 2a 29 73 71 (u8*)p - (u8*)sq
9d060 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9d070 67 2e 70 53 63 72 61 74 63 68 29 3b 0a 20 20 20 g.pScratch);.
9d080 20 20 20 69 20 2f 3d 20 73 71 6c 69 74 65 33 47 i /= sqlite3G
9d090 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 lobalConfig.szSc
9d0a0 72 61 74 63 68 3b 0a 20 20 20 20 20 20 61 73 73 ratch;. ass
9d0b0 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 73 ert( i>=0 && i<s
9d0c0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9d0d0 69 67 2e 6e 53 63 72 61 74 63 68 20 29 3b 0a 20 ig.nScratch );.
9d0e0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
9d0f0 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 ex_enter(mem0.mu
9d100 74 65 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65 tex);. asse
9d110 72 74 28 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 rt( mem0.nScratc
9d120 68 46 72 65 65 3c 28 75 33 32 29 73 71 6c 69 74 hFree<(u32)sqlit
9d130 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e e3GlobalConfig.n
9d140 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20 Scratch );.
9d150 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 mem0.aScratchFr
9d160 65 65 5b 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 ee[mem0.nScratch
9d170 46 72 65 65 2b 2b 5d 20 3d 20 69 3b 0a 20 20 20 Free++] = i;.
9d180 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
9d190 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Add(SQLITE_STATU
9d1a0 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 2c 20 S_SCRATCH_USED,
9d1b0 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 -1);. sqlit
9d1c0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
9d1d0 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 em0.mutex);.
9d1e0 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 }. }.}../*.** T
9d1f0 52 55 45 20 69 66 20 70 20 69 73 20 61 20 6c 6f RUE if p is a lo
9d200 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 okaside memory a
9d210 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 6f 6d 20 64 llocation from d
9d220 62 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c b.*/.#ifndef SQL
9d230 49 54 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 ITE_OMIT_LOOKASI
9d240 44 45 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 DE.static int is
9d250 4c 6f 6f 6b 61 73 69 64 65 28 73 71 6c 69 74 65 Lookaside(sqlite
9d260 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b 3 *db, void *p){
9d270 0a 20 20 72 65 74 75 72 6e 20 64 62 20 26 26 20 . return db &&
9d280 70 20 26 26 20 70 3e 3d 64 62 2d 3e 6c 6f 6f 6b p && p>=db->look
9d290 61 73 69 64 65 2e 70 53 74 61 72 74 20 26 26 20 aside.pStart &&
9d2a0 70 3c 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e p<db->lookaside.
9d2b0 70 45 6e 64 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 pEnd;.}.#else.#d
9d2c0 65 66 69 6e 65 20 69 73 4c 6f 6f 6b 61 73 69 64 efine isLookasid
9d2d0 65 28 41 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a e(A,B) 0.#endif.
9d2e0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
9d2f0 65 20 73 69 7a 65 20 6f 66 20 61 20 6d 65 6d 6f e size of a memo
9d300 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 ry allocation pr
9d310 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 eviously obtaine
9d320 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 d from.** sqlite
9d330 33 4d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 3Malloc() or sql
9d340 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a ite3_malloc()..*
9d350 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
9d360 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c int sqlite3Mall
9d370 6f 63 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b ocSize(void *p){
9d380 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
9d390 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 3GlobalConfig.m.
9d3a0 78 53 69 7a 65 28 70 29 3b 0a 7d 0a 53 51 4c 49 xSize(p);.}.SQLI
9d3b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
9d3c0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 qlite3DbMallocSi
9d3d0 7a 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ze(sqlite3 *db,
9d3e0 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 void *p){. asse
9d3f0 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c rt( db==0 || sql
9d400 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
9d410 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
9d420 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 if( isLookaside(
9d430 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 72 65 db, p) ){. re
9d440 74 75 72 6e 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 turn db->lookasi
9d450 64 65 2e 73 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a de.sz;. }else{.
9d460 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 return sqlit
9d470 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
9d480 2e 78 53 69 7a 65 28 70 29 3b 0a 20 20 7d 0a 7d .xSize(p);. }.}
9d490 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d ../*.** Free mem
9d4a0 6f 72 79 20 70 72 65 76 69 6f 75 73 6c 79 20 6f ory previously o
9d4b0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c btained from sql
9d4c0 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f ite3Malloc()..*/
9d4d0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
9d4e0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 6f sqlite3_free(vo
9d4f0 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d id *p){. if( p=
9d500 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 =0 ) return;. i
9d510 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c f( sqlite3Global
9d520 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 Config.bMemstat
9d530 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d ){. sqlite3_m
9d540 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e utex_enter(mem0.
9d550 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 mutex);. sqli
9d560 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c te3StatusAdd(SQL
9d570 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 ITE_STATUS_MEMOR
9d580 59 5f 55 53 45 44 2c 20 2d 73 71 6c 69 74 65 33 Y_USED, -sqlite3
9d590 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 29 3b 0a MallocSize(p));.
9d5a0 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
9d5b0 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 lConfig.m.xFree(
9d5c0 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f p);. sqlite3_
9d5d0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 mutex_leave(mem0
9d5e0 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 .mutex);. }else
9d5f0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f {. sqlite3Glo
9d600 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 balConfig.m.xFre
9d610 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a e(p);. }.}../*.
9d620 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 74 ** Free memory t
9d630 68 61 74 20 6d 69 67 68 74 20 62 65 20 61 73 73 hat might be ass
9d640 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 70 ociated with a p
9d650 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 articular databa
9d660 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e se.** connection
9d670 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
9d680 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
9d690 44 62 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a DbFree(sqlite3 *
9d6a0 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 db, void *p){.
9d6b0 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c assert( db==0 ||
9d6c0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
9d6d0 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 eld(db->mutex) )
9d6e0 3b 0a 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 ;. if( isLookas
9d6f0 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 ide(db, p) ){.
9d700 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 LookasideSlot
9d710 2a 70 42 75 66 20 3d 20 28 4c 6f 6f 6b 61 73 69 *pBuf = (Lookasi
9d720 64 65 53 6c 6f 74 2a 29 70 3b 0a 20 20 20 20 70 deSlot*)p;. p
9d730 42 75 66 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d Buf->pNext = db-
9d740 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 >lookaside.pFree
9d750 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 ;. db->lookas
9d760 69 64 65 2e 70 46 72 65 65 20 3d 20 70 42 75 66 ide.pFree = pBuf
9d770 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 ;. db->lookas
9d780 69 64 65 2e 6e 4f 75 74 2d 2d 3b 0a 20 20 7d 65 ide.nOut--;. }e
9d790 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 lse{. sqlite3
9d7a0 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a _free(p);. }.}.
9d7b0 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
9d7c0 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 e size of an exi
9d7d0 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c sting memory all
9d7e0 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 ocation.*/.SQLIT
9d7f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
9d800 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 76 sqlite3Realloc(v
9d810 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e oid *pOld, int n
9d820 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f Bytes){. int nO
9d830 6c 64 2c 20 6e 4e 65 77 3b 0a 20 20 76 6f 69 64 ld, nNew;. void
9d840 20 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20 70 4f *pNew;. if( pO
9d850 6c 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 ld==0 ){. ret
9d860 75 72 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f urn sqlite3Mallo
9d870 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20 c(nBytes);. }.
9d880 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 29 if( nBytes<=0 )
9d890 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 {. sqlite3_fr
9d8a0 65 65 28 70 4f 6c 64 29 3b 0a 20 20 20 20 72 65 ee(pOld);. re
9d8b0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 turn 0;. }. if
9d8c0 28 20 6e 42 79 74 65 73 3e 3d 30 78 37 66 66 66 ( nBytes>=0x7fff
9d8d0 66 66 30 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 ff00 ){. /* T
9d8e0 68 65 20 30 78 37 66 66 66 66 30 30 20 6c 69 6d he 0x7ffff00 lim
9d8f0 69 74 20 74 65 72 6d 20 69 73 20 65 78 70 6c 61 it term is expla
9d900 69 6e 65 64 20 69 6e 20 63 6f 6d 6d 65 6e 74 73 ined in comments
9d910 20 6f 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f on sqlite3Mallo
9d920 63 28 29 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 c() */. retur
9d930 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 n 0;. }. nOld
9d940 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 = sqlite3MallocS
9d950 69 7a 65 28 70 4f 6c 64 29 3b 0a 20 20 6e 4e 65 ize(pOld);. nNe
9d960 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 w = sqlite3Globa
9d970 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 lConfig.m.xRound
9d980 75 70 28 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 up(nBytes);. if
9d990 28 20 6e 4f 6c 64 3d 3d 6e 4e 65 77 20 29 7b 0a ( nOld==nNew ){.
9d9a0 20 20 20 20 70 4e 65 77 20 3d 20 70 4f 6c 64 3b pNew = pOld;
9d9b0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c . }else if( sql
9d9c0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9d9d0 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 .bMemstat ){.
9d9e0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
9d9f0 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 nter(mem0.mutex)
9da00 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 ;. sqlite3Sta
9da10 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54 tusSet(SQLITE_ST
9da20 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 ATUS_MALLOC_SIZE
9da30 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69 , nBytes);. i
9da40 66 28 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 f( sqlite3Status
9da50 56 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 41 Value(SQLITE_STA
9da60 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29 TUS_MEMORY_USED)
9da70 2b 6e 4e 65 77 2d 6e 4f 6c 64 20 3e 3d 20 0a 20 +nNew-nOld >= .
9da80 20 20 20 20 20 20 20 20 20 6d 65 6d 30 2e 61 6c mem0.al
9da90 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 29 7b 0a armThreshold ){.
9daa0 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c sqlite3Mal
9dab0 6c 6f 63 41 6c 61 72 6d 28 6e 4e 65 77 2d 6e 4f locAlarm(nNew-nO
9dac0 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 ld);. }. p
9dad0 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f New = sqlite3Glo
9dae0 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 balConfig.m.xRea
9daf0 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 lloc(pOld, nNew)
9db00 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d ;. if( pNew==
9db10 30 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 0 && mem0.alarmC
9db20 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 allback ){.
9db30 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c sqlite3MallocAl
9db40 61 72 6d 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 arm(nBytes);.
9db50 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 pNew = sqlite
9db60 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 3GlobalConfig.m.
9db70 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e xRealloc(pOld, n
9db80 4e 65 77 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 New);. }.
9db90 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 if( pNew ){.
9dba0 20 20 6e 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 nNew = sqlite3
9dbb0 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4e 65 77 29 MallocSize(pNew)
9dbc0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 ;. sqlite3S
9dbd0 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f tatusAdd(SQLITE_
9dbe0 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 STATUS_MEMORY_US
9dbf0 45 44 2c 20 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a ED, nNew-nOld);.
9dc00 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
9dc10 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
9dc20 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c m0.mutex);. }el
9dc30 73 65 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 se{. pNew = s
9dc40 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9dc50 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f ig.m.xRealloc(pO
9dc60 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 7d 0a 20 ld, nNew);. }.
9dc70 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a return pNew;.}.
9dc80 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 75 62 6c 69 ./*.** The publi
9dc90 63 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 73 c interface to s
9dca0 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 2e 20 20 qlite3Realloc.
9dcb0 4d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 Make sure that t
9dcc0 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 75 62 he memory.** sub
9dcd0 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61 system is initia
9dce0 6c 69 7a 65 64 20 70 72 69 6f 72 20 74 6f 20 69 lized prior to i
9dcf0 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65 52 65 nvoking sqliteRe
9dd00 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 alloc..*/.SQLITE
9dd10 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 _API void *sqlit
9dd20 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 64 20 e3_realloc(void
9dd30 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e 29 7b 0a 23 *pOld, int n){.#
9dd40 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
9dd50 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 IT_AUTOINIT. if
9dd60 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 ( sqlite3_initia
9dd70 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 lize() ) return
9dd80 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 0;.#endif. retu
9dd90 72 6e 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f rn sqlite3Reallo
9dda0 63 28 70 4f 6c 64 2c 20 6e 29 3b 0a 7d 0a 0a 0a c(pOld, n);.}...
9ddb0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 /*.** Allocate a
9ddc0 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 0a nd zero memory..
9ddd0 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */ .SQLITE_PRIVA
9dde0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
9ddf0 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 20 6e MallocZero(int n
9de00 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 ){. void *p = s
9de10 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b qlite3Malloc(n);
9de20 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
9de30 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b memset(p, 0, n);
9de40 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
9de50 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 .}../*.** Alloca
9de60 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f te and zero memo
9de70 72 79 2e 20 20 49 66 20 74 68 65 20 61 6c 6c 6f ry. If the allo
9de80 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d 61 cation fails, ma
9de90 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 ke.** the malloc
9dea0 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 Failed flag in t
9deb0 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f he connection po
9dec0 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 inter..*/.SQLITE
9ded0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
9dee0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 qlite3DbMallocZe
9def0 72 6f 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ro(sqlite3 *db,
9df00 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a int n){. void *
9df10 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c p = sqlite3DbMal
9df20 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 locRaw(db, n);.
9df30 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 if( p ){. me
9df40 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 mset(p, 0, n);.
9df50 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d }. return p;.}
9df60 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 ../*.** Allocate
9df70 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 and zero memory
9df80 2e 20 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61 . If the alloca
9df90 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d 61 6b 65 tion fails, make
9dfa0 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 .** the mallocFa
9dfb0 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 iled flag in the
9dfc0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e connection poin
9dfd0 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 62 ter..**.** If db
9dfe0 21 3d 30 20 61 6e 64 20 64 62 2d 3e 6d 61 6c 6c !=0 and db->mall
9dff0 6f 63 46 61 69 6c 65 64 20 69 73 20 74 72 75 65 ocFailed is true
9e000 20 28 69 6e 64 69 63 61 74 69 6e 67 20 61 20 70 (indicating a p
9e010 72 69 6f 72 20 6d 61 6c 6c 6f 63 0a 2a 2a 20 66 rior malloc.** f
9e020 61 69 6c 75 72 65 20 6f 6e 20 74 68 65 20 73 61 ailure on the sa
9e030 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e me database conn
9e040 65 63 74 69 6f 6e 29 20 74 68 65 6e 20 61 6c 77 ection) then alw
9e050 61 79 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a ays return 0..**
9e060 20 48 65 6e 63 65 20 66 6f 72 20 61 20 70 61 72 Hence for a par
9e070 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 ticular database
9e080 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 6f 6e 63 connection, onc
9e090 65 20 6d 61 6c 6c 6f 63 20 73 74 61 72 74 73 0a e malloc starts.
9e0a0 2a 2a 20 66 61 69 6c 69 6e 67 2c 20 69 74 20 66 ** failing, it f
9e0b0 61 69 6c 73 20 63 6f 6e 73 69 73 74 65 6e 74 6c ails consistentl
9e0c0 79 20 75 6e 74 69 6c 20 6d 61 6c 6c 6f 63 46 61 y until mallocFa
9e0d0 69 6c 65 64 20 69 73 20 72 65 73 65 74 2e 0a 2a iled is reset..*
9e0e0 2a 20 54 68 69 73 20 69 73 20 61 6e 20 69 6d 70 * This is an imp
9e0f0 6f 72 74 61 6e 74 20 61 73 73 75 6d 70 74 69 6f ortant assumptio
9e100 6e 2e 20 20 54 68 65 72 65 20 61 72 65 20 6d 61 n. There are ma
9e110 6e 79 20 70 6c 61 63 65 73 20 69 6e 20 74 68 65 ny places in the
9e120 0a 2a 2a 20 63 6f 64 65 20 74 68 61 74 20 64 6f .** code that do
9e130 20 74 68 69 6e 67 73 20 6c 69 6b 65 20 74 68 69 things like thi
9e140 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 s:.**.**
9e150 20 69 6e 74 20 2a 61 20 3d 20 28 69 6e 74 2a 29 int *a = (int*)
9e160 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
9e170 61 77 28 64 62 2c 20 31 30 30 29 3b 0a 2a 2a 20 aw(db, 100);.**
9e180 20 20 20 20 20 20 20 20 69 6e 74 20 2a 62 20 3d int *b =
9e190 20 28 69 6e 74 2a 29 73 71 6c 69 74 65 33 44 62 (int*)sqlite3Db
9e1a0 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 32 30 MallocRaw(db, 20
9e1b0 30 29 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 0);.** i
9e1c0 66 28 20 62 20 29 20 61 5b 31 30 5d 20 3d 20 39 f( b ) a[10] = 9
9e1d0 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 ;.**.** In other
9e1e0 20 77 6f 72 64 73 2c 20 69 66 20 61 20 73 75 62 words, if a sub
9e1f0 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 28 sequent malloc (
9e200 65 78 3a 20 22 62 22 29 20 77 6f 72 6b 65 64 2c ex: "b") worked,
9e210 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 0a 2a it is assumed.*
9e220 2a 20 74 68 61 74 20 61 6c 6c 20 70 72 69 6f 72 * that all prior
9e230 20 6d 61 6c 6c 6f 63 73 20 28 65 78 3a 20 22 61 mallocs (ex: "a
9e240 22 29 20 77 6f 72 6b 65 64 20 74 6f 6f 2e 0a 2a ") worked too..*
9e250 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
9e260 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 void *sqlite3Db
9e270 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 MallocRaw(sqlite
9e280 33 20 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0a 20 3 *db, int n){.
9e290 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 void *p;. asse
9e2a0 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c rt( db==0 || sql
9e2b0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
9e2c0 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 23 69 db->mutex) );.#i
9e2d0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
9e2e0 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 20 20 69 66 T_LOOKASIDE. if
9e2f0 28 20 64 62 20 29 7b 0a 20 20 20 20 4c 6f 6f 6b ( db ){. Look
9e300 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75 66 3b asideSlot *pBuf;
9e310 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c . if( db->mal
9e320 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
9e330 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 return 0;.
9e340 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6c }. if( db->l
9e350 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 ookaside.bEnable
9e360 64 20 26 26 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b d && n<=db->look
9e370 61 73 69 64 65 2e 73 7a 0a 20 20 20 20 20 20 20 aside.sz.
9e380 20 20 26 26 20 28 70 42 75 66 20 3d 20 64 62 2d && (pBuf = db-
9e390 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 >lookaside.pFree
9e3a0 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 62 )!=0 ){. db
9e3b0 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 ->lookaside.pFre
9e3c0 65 20 3d 20 70 42 75 66 2d 3e 70 4e 65 78 74 3b e = pBuf->pNext;
9e3d0 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 . db->looka
9e3e0 73 69 64 65 2e 6e 4f 75 74 2b 2b 3b 0a 20 20 20 side.nOut++;.
9e3f0 20 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 if( db->looka
9e400 73 69 64 65 2e 6e 4f 75 74 3e 64 62 2d 3e 6c 6f side.nOut>db->lo
9e410 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 29 7b okaside.mxOut ){
9e420 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f . db->loo
9e430 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 3d 20 64 kaside.mxOut = d
9e440 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 b->lookaside.nOu
9e450 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 t;. }.
9e460 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 return (void*)p
9e470 42 75 66 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 Buf;. }. }.#
9e480 65 6c 73 65 0a 20 20 69 66 28 20 64 62 20 26 26 else. if( db &&
9e490 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
9e4a0 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 d ){. return
9e4b0 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 0;. }.#endif.
9e4c0 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f p = sqlite3Mallo
9e4d0 63 28 6e 29 3b 0a 20 20 69 66 28 20 21 70 20 26 c(n);. if( !p &
9e4e0 26 20 64 62 20 29 7b 0a 20 20 20 20 64 62 2d 3e & db ){. db->
9e4f0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
9e500 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
9e510 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a ;.}../*.** Resiz
9e520 65 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 6d e the block of m
9e530 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 6f emory pointed to
9e540 20 62 79 20 70 20 74 6f 20 6e 20 62 79 74 65 73 by p to n bytes
9e550 2e 20 49 66 20 74 68 65 0a 2a 2a 20 72 65 73 69 . If the.** resi
9e560 7a 65 20 66 61 69 6c 73 2c 20 73 65 74 20 74 68 ze fails, set th
9e570 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 e mallocFailed f
9e580 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 lag in the conne
9e590 63 74 69 6f 6e 20 6f 62 6a 65 63 74 2e 0a 2a 2f ction object..*/
9e5a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
9e5b0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52 void *sqlite3DbR
9e5c0 65 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 20 2a ealloc(sqlite3 *
9e5d0 64 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69 6e 74 db, void *p, int
9e5e0 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 n){. void *pNe
9e5f0 77 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 w = 0;. assert(
9e600 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 db!=0 );. asse
9e610 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
9e620 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 x_held(db->mutex
9e630 29 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d ) );. if( db->m
9e640 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 allocFailed==0 )
9e650 7b 0a 20 20 20 20 69 66 28 20 70 3d 3d 30 20 29 {. if( p==0 )
9e660 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 73 {. return s
9e670 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
9e680 77 28 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a w(db, n);. }.
9e690 20 20 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 if( isLookas
9e6a0 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 ide(db, p) ){.
9e6b0 20 20 20 20 69 66 28 20 6e 3c 3d 64 62 2d 3e 6c if( n<=db->l
9e6c0 6f 6f 6b 61 73 69 64 65 2e 73 7a 20 29 7b 0a 20 ookaside.sz ){.
9e6d0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 3b return p;
9e6e0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
9e6f0 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d New = sqlite3DbM
9e700 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b allocRaw(db, n);
9e710 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 . if( pNew
9e720 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 ){. memcp
9e730 79 28 70 4e 65 77 2c 20 70 2c 20 64 62 2d 3e 6c y(pNew, p, db->l
9e740 6f 6f 6b 61 73 69 64 65 2e 73 7a 29 3b 0a 20 20 ookaside.sz);.
9e750 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
9e760 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 ree(db, p);.
9e770 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
9e780 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 pNew = sqli
9e790 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 2c 20 6e te3_realloc(p, n
9e7a0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e );. if( !pN
9e7b0 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 ew ){. db
9e7c0 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
9e7d0 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 1;. }.
9e7e0 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 }. }. return p
9e7f0 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 New;.}../*.** At
9e800 74 65 6d 70 74 20 74 6f 20 72 65 61 6c 6c 6f 63 tempt to realloc
9e810 61 74 65 20 70 2e 20 20 49 66 20 74 68 65 20 72 ate p. If the r
9e820 65 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c eallocation fail
9e830 73 2c 20 74 68 65 6e 20 66 72 65 65 20 70 0a 2a s, then free p.*
9e840 2a 20 61 6e 64 20 73 65 74 20 74 68 65 20 6d 61 * and set the ma
9e850 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 llocFailed flag
9e860 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
9e870 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 connection..*/.S
9e880 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
9e890 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52 65 61 id *sqlite3DbRea
9e8a0 6c 6c 6f 63 4f 72 46 72 65 65 28 73 71 6c 69 74 llocOrFree(sqlit
9e8b0 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 2c e3 *db, void *p,
9e8c0 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 int n){. void
9e8d0 2a 70 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20 *pNew;. pNew =
9e8e0 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 sqlite3DbRealloc
9e8f0 28 64 62 2c 20 70 2c 20 6e 29 3b 0a 20 20 69 66 (db, p, n);. if
9e900 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 73 ( !pNew ){. s
9e910 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
9e920 20 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 p);. }. retur
9e930 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n pNew;.}../*.**
9e940 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 Make a copy of
9e950 61 20 73 74 72 69 6e 67 20 69 6e 20 6d 65 6d 6f a string in memo
9e960 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ry obtained from
9e970 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e sqliteMalloc().
9e980 20 54 68 65 73 65 20 0a 2a 2a 20 66 75 6e 63 74 These .** funct
9e990 69 6f 6e 73 20 63 61 6c 6c 20 73 71 6c 69 74 65 ions call sqlite
9e9a0 33 4d 61 6c 6c 6f 63 52 61 77 28 29 20 64 69 72 3MallocRaw() dir
9e9b0 65 63 74 6c 79 20 69 6e 73 74 65 61 64 20 6f 66 ectly instead of
9e9c0 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e sqliteMalloc().
9e9d0 20 54 68 69 73 0a 2a 2a 20 69 73 20 62 65 63 61 This.** is beca
9e9e0 75 73 65 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20 use when memory
9e9f0 64 65 62 75 67 67 69 6e 67 20 69 73 20 74 75 72 debugging is tur
9ea00 6e 65 64 20 6f 6e 2c 20 74 68 65 73 65 20 74 77 ned on, these tw
9ea10 6f 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 o functions are
9ea20 0a 2a 2a 20 63 61 6c 6c 65 64 20 76 69 61 20 6d .** called via m
9ea30 61 63 72 6f 73 20 74 68 61 74 20 72 65 63 6f 72 acros that recor
9ea40 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 69 d the current fi
9ea50 6c 65 20 61 6e 64 20 6c 69 6e 65 20 6e 75 6d 62 le and line numb
9ea60 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 54 68 72 er in the.** Thr
9ea70 65 61 64 44 61 74 61 20 73 74 72 75 63 74 75 72 eadData structur
9ea80 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
9ea90 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
9eaa0 65 33 44 62 53 74 72 44 75 70 28 73 71 6c 69 74 e3DbStrDup(sqlit
9eab0 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 e3 *db, const ch
9eac0 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61 72 20 2a ar *z){. char *
9ead0 7a 4e 65 77 3b 0a 20 20 73 69 7a 65 5f 74 20 6e zNew;. size_t n
9eae0 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a ;. if( z==0 ){.
9eaf0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
9eb00 7d 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 }. n = sqlite3S
9eb10 74 72 6c 65 6e 33 30 28 7a 29 20 2b 20 31 3b 0a trlen30(z) + 1;.
9eb20 20 20 61 73 73 65 72 74 28 20 28 6e 26 30 78 37 assert( (n&0x7
9eb30 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 fffffff)==n );.
9eb40 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 zNew = sqlite3D
9eb50 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 28 bMallocRaw(db, (
9eb60 69 6e 74 29 6e 29 3b 0a 20 20 69 66 28 20 7a 4e int)n);. if( zN
9eb70 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 ew ){. memcpy
9eb80 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 (zNew, z, n);.
9eb90 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b }. return zNew;
9eba0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
9ebb0 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 E char *sqlite3D
9ebc0 62 53 74 72 4e 44 75 70 28 73 71 6c 69 74 65 33 bStrNDup(sqlite3
9ebd0 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 *db, const char
9ebe0 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 63 *z, int n){. c
9ebf0 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 69 66 28 har *zNew;. if(
9ec00 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 z==0 ){. ret
9ec10 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 urn 0;. }. ass
9ec20 65 72 74 28 20 28 6e 26 30 78 37 66 66 66 66 66 ert( (n&0x7fffff
9ec30 66 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a 4e 65 77 ff)==n );. zNew
9ec40 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
9ec50 6f 63 52 61 77 28 64 62 2c 20 6e 2b 31 29 3b 0a ocRaw(db, n+1);.
9ec60 20 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20 if( zNew ){.
9ec70 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a memcpy(zNew, z
9ec80 2c 20 6e 29 3b 0a 20 20 20 20 7a 4e 65 77 5b 6e , n);. zNew[n
9ec90 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 ] = 0;. }. ret
9eca0 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a urn zNew;.}../*.
9ecb0 2a 2a 20 43 72 65 61 74 65 20 61 20 73 74 72 69 ** Create a stri
9ecc0 6e 67 20 66 72 6f 6d 20 74 68 65 20 7a 46 72 6f ng from the zFro
9ecd0 6d 61 74 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 mat argument and
9ece0 20 74 68 65 20 76 61 5f 6c 69 73 74 20 74 68 61 the va_list tha
9ecf0 74 20 66 6f 6c 6c 6f 77 73 2e 0a 2a 2a 20 53 74 t follows..** St
9ed00 6f 72 65 20 74 68 65 20 73 74 72 69 6e 67 20 69 ore the string i
9ed10 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 n memory obtaine
9ed20 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c d from sqliteMal
9ed30 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b 65 20 2a loc() and make *
9ed40 70 7a 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74 pz.** point to t
9ed50 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 hat string..*/.S
9ed60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
9ed70 69 64 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 id sqlite3SetStr
9ed80 69 6e 67 28 63 68 61 72 20 2a 2a 70 7a 2c 20 73 ing(char **pz, s
9ed90 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 qlite3 *db, cons
9eda0 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c t char *zFormat,
9edb0 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 ...){. va_list
9edc0 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a ap;. char *z;.
9edd0 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 . va_start(ap,
9ede0 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 zFormat);. z =
9edf0 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 sqlite3VMPrintf(
9ee00 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 db, zFormat, ap)
9ee10 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a ;. va_end(ap);.
9ee20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
9ee30 64 62 2c 20 2a 70 7a 29 3b 0a 20 20 2a 70 7a 20 db, *pz);. *pz
9ee40 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 = z;.}.../*.** T
9ee50 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 his function mus
9ee60 74 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f t be called befo
9ee70 72 65 20 65 78 69 74 69 6e 67 20 61 6e 79 20 41 re exiting any A
9ee80 50 49 20 66 75 6e 63 74 69 6f 6e 20 28 69 2e 65 PI function (i.e
9ee90 2e 20 0a 2a 2a 20 72 65 74 75 72 6e 69 6e 67 20 . .** returning
9eea0 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 75 control to the u
9eeb0 73 65 72 29 20 74 68 61 74 20 68 61 73 20 63 61 ser) that has ca
9eec0 6c 6c 65 64 20 73 71 6c 69 74 65 33 5f 6d 61 6c lled sqlite3_mal
9eed0 6c 6f 63 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 loc or.** sqlite
9eee0 33 5f 72 65 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 3_realloc..**.**
9eef0 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 The returned va
9ef00 6c 75 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 lue is normally
9ef10 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 65 a copy of the se
9ef20 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f cond argument to
9ef30 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f this.** functio
9ef40 6e 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 61 n. However, if a
9ef50 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 malloc() failur
9ef60 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 20 73 e has occurred s
9ef70 69 6e 63 65 20 74 68 65 20 70 72 65 76 69 6f 75 ince the previou
9ef80 73 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 s.** invocation
9ef90 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 SQLITE_NOMEM is
9efa0 72 65 74 75 72 6e 65 64 20 69 6e 73 74 65 61 64 returned instead
9efb0 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 . .**.** If the
9efc0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 first argument,
9efd0 64 62 2c 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 db, is not NULL
9efe0 61 6e 64 20 61 20 6d 61 6c 6c 6f 63 28 29 20 65 and a malloc() e
9eff0 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 rror has occurre
9f000 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 d,.** then the c
9f010 6f 6e 6e 65 63 74 69 6f 6e 20 65 72 72 6f 72 2d onnection error-
9f020 63 6f 64 65 20 28 74 68 65 20 76 61 6c 75 65 20 code (the value
9f030 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 returned by sqli
9f040 74 65 33 5f 65 72 72 63 6f 64 65 28 29 29 0a 2a te3_errcode()).*
9f050 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 * is set to SQLI
9f060 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 53 51 4c TE_NOMEM..*/.SQL
9f070 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
9f080 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 73 sqlite3ApiExit(s
9f090 71 6c 69 74 65 33 2a 20 64 62 2c 20 69 6e 74 20 qlite3* db, int
9f0a0 72 63 29 7b 0a 20 20 2f 2a 20 49 66 20 74 68 65 rc){. /* If the
9f0b0 20 64 62 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f db handle is no
9f0c0 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 77 65 20 t NULL, then we
9f0d0 6d 75 73 74 20 68 6f 6c 64 20 74 68 65 20 63 6f must hold the co
9f0e0 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 0a nnection handle.
9f0f0 20 20 2a 2a 20 6d 75 74 65 78 20 68 65 72 65 2e ** mutex here.
9f100 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 72 Otherwise the r
9f110 65 61 64 20 28 61 6e 64 20 70 6f 73 73 69 62 6c ead (and possibl
9f120 65 20 77 72 69 74 65 29 20 6f 66 20 64 62 2d 3e e write) of db->
9f130 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 0a 20 20 mallocFailed .
9f140 2a 2a 20 69 73 20 75 6e 73 61 66 65 2c 20 61 73 ** is unsafe, as
9f150 20 69 73 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 is the call to
9f160 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 2e 0a sqlite3Error()..
9f170 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 21 */. assert( !
9f180 64 62 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 db || sqlite3_mu
9f190 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 tex_held(db->mut
9f1a0 65 78 29 20 29 3b 0a 20 20 69 66 28 20 64 62 20 ex) );. if( db
9f1b0 26 26 20 28 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 && (db->mallocFa
9f1c0 69 6c 65 64 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 iled || rc==SQLI
9f1d0 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 29 20 TE_IOERR_NOMEM)
9f1e0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
9f1f0 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4e ror(db, SQLITE_N
9f200 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 20 64 62 OMEM, 0);. db
9f210 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
9f220 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 0;. rc = SQL
9f230 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 ITE_NOMEM;. }.
9f240 20 72 65 74 75 72 6e 20 72 63 20 26 20 28 64 62 return rc & (db
9f250 20 3f 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20 3a ? db->errMask :
9f260 20 30 78 66 66 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 0xff);.}../****
9f270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
9f280 66 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a f malloc.c *****
9f290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
9f2c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
9f2d0 20 66 69 6c 65 20 70 72 69 6e 74 66 2e 63 20 2a file printf.c *
9f2e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f2f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
9f310 20 54 68 65 20 22 70 72 69 6e 74 66 22 20 63 6f The "printf" co
9f320 64 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 de that follows
9f330 64 61 74 65 73 20 66 72 6f 6d 20 74 68 65 20 31 dates from the 1
9f340 39 38 30 27 73 2e 20 20 49 74 20 69 73 20 69 6e 980's. It is in
9f350 0a 2a 2a 20 74 68 65 20 70 75 62 6c 69 63 20 64 .** the public d
9f360 6f 6d 61 69 6e 2e 20 20 54 68 65 20 6f 72 69 67 omain. The orig
9f370 69 6e 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 61 72 inal comments ar
9f380 65 20 69 6e 63 6c 75 64 65 64 20 68 65 72 65 20 e included here
9f390 66 6f 72 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 6e for.** completen
9f3a0 65 73 73 2e 20 20 54 68 65 79 20 61 72 65 20 76 ess. They are v
9f3b0 65 72 79 20 6f 75 74 2d 6f 66 2d 64 61 74 65 20 ery out-of-date
9f3c0 62 75 74 20 6d 69 67 68 74 20 62 65 20 75 73 65 but might be use
9f3d0 66 75 6c 20 61 73 0a 2a 2a 20 61 6e 20 68 69 73 ful as.** an his
9f3e0 74 6f 72 69 63 61 6c 20 72 65 66 65 72 65 6e 63 torical referenc
9f3f0 65 2e 20 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 e. Most of the
9f400 22 65 6e 68 61 6e 63 65 6d 65 6e 74 73 22 20 68 "enhancements" h
9f410 61 76 65 20 62 65 65 6e 20 62 61 63 6b 65 64 0a ave been backed.
9f420 2a 2a 20 6f 75 74 20 73 6f 20 74 68 61 74 20 74 ** out so that t
9f430 68 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 he functionality
9f440 20 69 73 20 6e 6f 77 20 74 68 65 20 73 61 6d 65 is now the same
9f450 20 61 73 20 73 74 61 6e 64 61 72 64 20 70 72 69 as standard pri
9f460 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 ntf()..**.** $Id
9f470 3a 20 70 72 69 6e 74 66 2e 63 2c 76 20 31 2e 31 : printf.c,v 1.1
9f480 30 34 20 32 30 30 39 2f 30 36 2f 30 33 20 30 31 04 2009/06/03 01
9f490 3a 32 34 3a 35 34 20 64 72 68 20 45 78 70 20 24 :24:54 drh Exp $
9f4a0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
9f4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
9f4f0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
9f500 6e 67 20 6d 6f 64 75 6c 65 73 20 69 73 20 61 6e ng modules is an
9f510 20 65 6e 68 61 6e 63 65 64 20 72 65 70 6c 61 63 enhanced replac
9f520 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 22 70 ement for the "p
9f530 72 69 6e 74 66 22 20 73 75 62 72 6f 75 74 69 6e rintf" subroutin
9f540 65 73 0a 2a 2a 20 66 6f 75 6e 64 20 69 6e 20 74 es.** found in t
9f550 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 he standard C li
9f560 62 72 61 72 79 2e 20 20 54 68 65 20 66 6f 6c 6c brary. The foll
9f570 6f 77 69 6e 67 20 65 6e 68 61 6e 63 65 6d 65 6e owing enhancemen
9f580 74 73 20 61 72 65 0a 2a 2a 20 73 75 70 70 6f 72 ts are.** suppor
9f590 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 ted:.**.**
9f5a0 2b 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 66 75 + Additional fu
9f5b0 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 73 74 nctions. The st
9f5c0 61 6e 64 61 72 64 20 73 65 74 20 6f 66 20 22 70 andard set of "p
9f5d0 72 69 6e 74 66 22 20 66 75 6e 63 74 69 6f 6e 73 rintf" functions
9f5e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 63 6c .** incl
9f5f0 75 64 65 73 20 70 72 69 6e 74 66 2c 20 66 70 72 udes printf, fpr
9f600 69 6e 74 66 2c 20 73 70 72 69 6e 74 66 2c 20 76 intf, sprintf, v
9f610 70 72 69 6e 74 66 2c 20 76 66 70 72 69 6e 74 66 printf, vfprintf
9f620 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 , and.**
9f630 20 76 73 70 72 69 6e 74 66 2e 20 20 54 68 69 73 vsprintf. This
9f640 20 6d 6f 64 75 6c 65 20 61 64 64 73 20 74 68 65 module adds the
9f650 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a following:.**.*
9f660 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 73 * * s
9f670 6e 70 72 69 6e 74 66 20 2d 2d 20 57 6f 72 6b 73 nprintf -- Works
9f680 20 6c 69 6b 65 20 73 70 72 69 6e 74 66 2c 20 62 like sprintf, b
9f690 75 74 20 68 61 73 20 61 6e 20 65 78 74 72 61 20 ut has an extra
9f6a0 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 argument.**
9f6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9f6c0 20 20 20 20 20 77 68 69 63 68 20 69 73 20 74 68 which is th
9f6d0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 e size of the bu
9f6e0 66 66 65 72 20 77 72 69 74 74 65 6e 20 74 6f 2e ffer written to.
9f6f0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .**.**
9f700 20 2a 20 20 6d 70 72 69 6e 74 66 20 2d 2d 20 20 * mprintf --
9f710 53 69 6d 69 6c 61 72 20 74 6f 20 73 70 72 69 6e Similar to sprin
9f720 74 66 2e 20 20 57 72 69 74 65 73 20 6f 75 74 70 tf. Writes outp
9f730 75 74 20 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a 20 ut to memory.**
9f740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9f750 20 20 20 20 20 20 20 20 20 6f 62 74 61 69 6e 65 obtaine
9f760 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a d from malloc..*
9f770 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a *.** *
9f780 20 20 78 70 72 69 6e 74 66 20 2d 2d 20 20 43 61 xprintf -- Ca
9f790 6c 6c 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 lls a function t
9f7a0 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 6f 75 74 o dispose of out
9f7b0 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 put..**.**
9f7c0 20 20 20 20 20 2a 20 20 6e 70 72 69 6e 74 66 20 * nprintf
9f7d0 2d 2d 20 20 4e 6f 20 6f 75 74 70 75 74 2c 20 62 -- No output, b
9f7e0 75 74 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e ut returns the n
9f7f0 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
9f800 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ers.**
9f810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9f820 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76 65 20 that would have
9f830 62 65 65 6e 20 6f 75 74 70 75 74 20 62 79 20 70 been output by p
9f840 72 69 6e 74 66 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 rintf..**.**
9f850 20 20 20 20 20 20 20 2a 20 20 41 20 76 2d 20 76 * A v- v
9f860 65 72 73 69 6f 6e 20 28 65 78 3a 20 76 73 6e 70 ersion (ex: vsnp
9f870 72 69 6e 74 66 29 20 6f 66 20 65 76 65 72 79 20 rintf) of every
9f880 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6c 73 6f function is also
9f890 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
9f8a0 20 73 75 70 70 6c 69 65 64 2e 0a 2a 2a 0a 2a 2a supplied..**.**
9f8b0 20 20 20 20 20 20 2b 20 20 41 20 66 65 77 20 65 + A few e
9f8c0 78 74 65 6e 73 69 6f 6e 73 20 74 6f 20 74 68 65 xtensions to the
9f8d0 20 66 6f 72 6d 61 74 74 69 6e 67 20 6e 6f 74 61 formatting nota
9f8e0 74 69 6f 6e 20 61 72 65 20 73 75 70 70 6f 72 74 tion are support
9f8f0 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 ed:.**.**
9f900 20 20 20 20 2a 20 20 54 68 65 20 22 3d 22 20 66 * The "=" f
9f910 6c 61 67 20 28 73 69 6d 69 6c 61 72 20 74 6f 20 lag (similar to
9f920 22 2d 22 29 20 63 61 75 73 65 73 20 74 68 65 20 "-") causes the
9f930 6f 75 74 70 75 74 20 74 6f 20 62 65 0a 2a 2a 20 output to be.**
9f940 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 20 be
9f950 63 65 6e 74 65 72 65 64 20 69 6e 20 74 68 65 20 centered in the
9f960 61 70 70 72 6f 70 72 69 61 74 65 6c 79 20 73 69 appropriately si
9f970 7a 65 64 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a zed field..**.**
9f980 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 54 68 * Th
9f990 65 20 25 62 20 66 69 65 6c 64 20 6f 75 74 70 75 e %b field outpu
9f9a0 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e ts an integer in
9f9b0 20 62 69 6e 61 72 79 20 6e 6f 74 61 74 69 6f 6e binary notation
9f9c0 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 ..**.**
9f9d0 20 20 2a 20 20 54 68 65 20 25 63 20 66 69 65 6c * The %c fiel
9f9e0 64 20 6e 6f 77 20 61 63 63 65 70 74 73 20 61 20 d now accepts a
9f9f0 70 72 65 63 69 73 69 6f 6e 2e 20 20 54 68 65 20 precision. The
9fa00 63 68 61 72 61 63 74 65 72 20 6f 75 74 70 75 74 character output
9fa10 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
9fa20 20 69 73 20 72 65 70 65 61 74 65 64 20 62 79 20 is repeated by
9fa30 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 the number of ti
9fa40 6d 65 73 20 74 68 65 20 70 72 65 63 69 73 69 6f mes the precisio
9fa50 6e 20 73 70 65 63 69 66 69 65 73 2e 0a 2a 2a 0a n specifies..**.
9fa60 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 ** *
9fa70 54 68 65 20 25 27 20 66 69 65 6c 64 20 77 6f 72 The %' field wor
9fa80 6b 73 20 6c 69 6b 65 20 25 63 2c 20 62 75 74 20 ks like %c, but
9fa90 74 61 6b 65 73 20 61 73 20 69 74 73 20 63 68 61 takes as its cha
9faa0 72 61 63 74 65 72 20 74 68 65 0a 2a 2a 20 20 20 racter the.**
9fab0 20 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 20 next
9fac0 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 character of the
9fad0 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 2c 20 format string,
9fae0 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6e instead of the n
9faf0 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ext.**
9fb00 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 20 20 46 argument. F
9fb10 6f 72 20 65 78 61 6d 70 6c 65 2c 20 20 70 72 69 or example, pri
9fb20 6e 74 66 28 22 25 2e 37 38 27 2d 22 29 20 20 70 ntf("%.78'-") p
9fb30 72 69 6e 74 73 20 37 38 20 6d 69 6e 75 73 0a 2a rints 78 minus.*
9fb40 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 * s
9fb50 69 67 6e 73 2c 20 74 68 65 20 73 61 6d 65 20 61 igns, the same a
9fb60 73 20 20 70 72 69 6e 74 66 28 22 25 2e 37 38 63 s printf("%.78c
9fb70 22 2c 27 2d 27 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 ",'-')..**.**
9fb80 20 20 20 2b 20 20 57 68 65 6e 20 63 6f 6d 70 69 + When compi
9fb90 6c 65 64 20 75 73 69 6e 67 20 47 43 43 20 6f 6e led using GCC on
9fba0 20 61 20 53 50 41 52 43 2c 20 74 68 69 73 20 76 a SPARC, this v
9fbb0 65 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 ersion of printf
9fbc0 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 is.** f
9fbd0 61 73 74 65 72 20 74 68 61 6e 20 74 68 65 20 6c aster than the l
9fbe0 69 62 72 61 72 79 20 70 72 69 6e 74 66 20 66 6f ibrary printf fo
9fbf0 72 20 53 55 4e 20 4f 53 20 34 2e 31 2e 0a 2a 2a r SUN OS 4.1..**
9fc00 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 6c 6c 20 .** + All
9fc10 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 66 75 functions are fu
9fc20 6c 6c 79 20 72 65 65 6e 74 72 61 6e 74 2e 0a 2a lly reentrant..*
9fc30 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 *.*/../*.** Conv
9fc40 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66 61 6c ersion types fal
9fc50 6c 20 69 6e 74 6f 20 76 61 72 69 6f 75 73 20 63 l into various c
9fc60 61 74 65 67 6f 72 69 65 73 20 61 73 20 64 65 66 ategories as def
9fc70 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 66 ined by the.** f
9fc80 6f 6c 6c 6f 77 69 6e 67 20 65 6e 75 6d 65 72 61 ollowing enumera
9fc90 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 tion..*/.#define
9fca0 20 65 74 52 41 44 49 58 20 20 20 20 20 20 20 31 etRADIX 1
9fcb0 20 2f 2a 20 49 6e 74 65 67 65 72 20 74 79 70 65 /* Integer type
9fcc0 73 2e 20 20 25 64 2c 20 25 78 2c 20 25 6f 2c 20 s. %d, %x, %o,
9fcd0 61 6e 64 20 73 6f 20 66 6f 72 74 68 20 2a 2f 0a and so forth */.
9fce0 23 64 65 66 69 6e 65 20 65 74 46 4c 4f 41 54 20 #define etFLOAT
9fcf0 20 20 20 20 20 20 32 20 2f 2a 20 46 6c 6f 61 74 2 /* Float
9fd00 69 6e 67 20 70 6f 69 6e 74 2e 20 20 25 66 20 2a ing point. %f *
9fd10 2f 0a 23 64 65 66 69 6e 65 20 65 74 45 58 50 20 /.#define etEXP
9fd20 20 20 20 20 20 20 20 20 33 20 2f 2a 20 45 78 70 3 /* Exp
9fd30 6f 6e 65 6e 74 69 6f 6e 61 6c 20 6e 6f 74 61 74 onentional notat
9fd40 69 6f 6e 2e 20 25 65 20 61 6e 64 20 25 45 20 2a ion. %e and %E *
9fd50 2f 0a 23 64 65 66 69 6e 65 20 65 74 47 45 4e 45 /.#define etGENE
9fd60 52 49 43 20 20 20 20 20 34 20 2f 2a 20 46 6c 6f RIC 4 /* Flo
9fd70 61 74 69 6e 67 20 6f 72 20 65 78 70 6f 6e 65 6e ating or exponen
9fd80 74 69 61 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20 tial, depending
9fd90 6f 6e 20 65 78 70 6f 6e 65 6e 74 2e 20 25 67 20 on exponent. %g
9fda0 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 49 5a */.#define etSIZ
9fdb0 45 20 20 20 20 20 20 20 20 35 20 2f 2a 20 52 65 E 5 /* Re
9fdc0 74 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66 20 63 turn number of c
9fdd0 68 61 72 61 63 74 65 72 73 20 70 72 6f 63 65 73 haracters proces
9fde0 73 65 64 20 73 6f 20 66 61 72 2e 20 25 6e 20 2a sed so far. %n *
9fdf0 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 54 52 49 /.#define etSTRI
9fe00 4e 47 20 20 20 20 20 20 36 20 2f 2a 20 53 74 72 NG 6 /* Str
9fe10 69 6e 67 73 2e 20 25 73 20 2a 2f 0a 23 64 65 66 ings. %s */.#def
9fe20 69 6e 65 20 65 74 44 59 4e 53 54 52 49 4e 47 20 ine etDYNSTRING
9fe30 20 20 37 20 2f 2a 20 44 79 6e 61 6d 69 63 61 6c 7 /* Dynamical
9fe40 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 ly allocated str
9fe50 69 6e 67 73 2e 20 25 7a 20 2a 2f 0a 23 64 65 66 ings. %z */.#def
9fe60 69 6e 65 20 65 74 50 45 52 43 45 4e 54 20 20 20 ine etPERCENT
9fe70 20 20 38 20 2f 2a 20 50 65 72 63 65 6e 74 20 73 8 /* Percent s
9fe80 79 6d 62 6f 6c 2e 20 25 25 20 2a 2f 0a 23 64 65 ymbol. %% */.#de
9fe90 66 69 6e 65 20 65 74 43 48 41 52 58 20 20 20 20 fine etCHARX
9fea0 20 20 20 39 20 2f 2a 20 43 68 61 72 61 63 74 65 9 /* Characte
9feb0 72 73 2e 20 25 63 20 2a 2f 0a 2f 2a 20 54 68 65 rs. %c */./* The
9fec0 20 72 65 73 74 20 61 72 65 20 65 78 74 65 6e 73 rest are extens
9fed0 69 6f 6e 73 2c 20 6e 6f 74 20 6e 6f 72 6d 61 6c ions, not normal
9fee0 6c 79 20 66 6f 75 6e 64 20 69 6e 20 70 72 69 6e ly found in prin
9fef0 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 tf() */.#define
9ff00 65 74 53 51 4c 45 53 43 41 50 45 20 20 31 30 20 etSQLESCAPE 10
9ff10 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68 20 /* Strings with
9ff20 27 5c 27 27 20 64 6f 75 62 6c 65 64 2e 20 20 25 '\'' doubled. %
9ff30 71 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 q */.#define etS
9ff40 51 4c 45 53 43 41 50 45 32 20 31 31 20 2f 2a 20 QLESCAPE2 11 /*
9ff50 53 74 72 69 6e 67 73 20 77 69 74 68 20 27 5c 27 Strings with '\'
9ff60 27 20 64 6f 75 62 6c 65 64 20 61 6e 64 20 65 6e ' doubled and en
9ff70 63 6c 6f 73 65 64 20 69 6e 20 27 27 2c 0a 20 20 closed in '',.
9ff80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9ff90 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 70 6f 69 NULL poi
9ffa0 6e 74 65 72 73 20 72 65 70 6c 61 63 65 64 20 62 nters replaced b
9ffb0 79 20 53 51 4c 20 4e 55 4c 4c 2e 20 20 25 51 20 y SQL NULL. %Q
9ffc0 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 54 4f 4b */.#define etTOK
9ffd0 45 4e 20 20 20 20 20 20 31 32 20 2f 2a 20 61 20 EN 12 /* a
9ffe0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 6f 6b pointer to a Tok
9fff0 65 6e 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a en structure */.
a0000 23 64 65 66 69 6e 65 20 65 74 53 52 43 4c 49 53 #define etSRCLIS
a0010 54 20 20 20 20 31 33 20 2f 2a 20 61 20 70 6f 69 T 13 /* a poi
a0020 6e 74 65 72 20 74 6f 20 61 20 53 72 63 4c 69 73 nter to a SrcLis
a0030 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 50 t */.#define etP
a0040 4f 49 4e 54 45 52 20 20 20 20 31 34 20 2f 2a 20 OINTER 14 /*
a0050 54 68 65 20 25 70 20 63 6f 6e 76 65 72 73 69 6f The %p conversio
a0060 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 n */.#define etS
a0070 51 4c 45 53 43 41 50 45 33 20 31 35 20 2f 2a 20 QLESCAPE3 15 /*
a0080 25 77 20 2d 3e 20 53 74 72 69 6e 67 73 20 77 69 %w -> Strings wi
a0090 74 68 20 27 5c 22 27 20 64 6f 75 62 6c 65 64 20 th '\"' doubled
a00a0 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 4f 52 44 */.#define etORD
a00b0 49 4e 41 4c 20 20 20 20 31 36 20 2f 2a 20 25 72 INAL 16 /* %r
a00c0 20 2d 3e 20 31 73 74 2c 20 32 6e 64 2c 20 33 72 -> 1st, 2nd, 3r
a00d0 64 2c 20 34 74 68 2c 20 65 74 63 2e 20 20 45 6e d, 4th, etc. En
a00e0 67 6c 69 73 68 20 6f 6e 6c 79 20 2a 2f 0a 0a 23 glish only */..#
a00f0 64 65 66 69 6e 65 20 65 74 49 4e 56 41 4c 49 44 define etINVALID
a0100 20 20 20 20 20 30 20 2f 2a 20 41 6e 79 20 75 6e 0 /* Any un
a0110 72 65 63 6f 67 6e 69 7a 65 64 20 63 6f 6e 76 65 recognized conve
a0120 72 73 69 6f 6e 20 74 79 70 65 20 2a 2f 0a 0a 0a rsion type */...
a0130 2f 2a 0a 2a 2a 20 41 6e 20 22 65 74 42 79 74 65 /*.** An "etByte
a0140 22 20 69 73 20 61 6e 20 38 2d 62 69 74 20 75 6e " is an 8-bit un
a0150 73 69 67 6e 65 64 20 76 61 6c 75 65 2e 0a 2a 2f signed value..*/
a0160 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 .typedef unsigne
a0170 64 20 63 68 61 72 20 65 74 42 79 74 65 3b 0a 0a d char etByte;..
a0180 2f 2a 0a 2a 2a 20 45 61 63 68 20 62 75 69 6c 74 /*.** Each built
a0190 69 6e 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68 in conversion ch
a01a0 61 72 61 63 74 65 72 20 28 65 78 3a 20 74 68 65 aracter (ex: the
a01b0 20 27 64 27 20 69 6e 20 22 25 64 22 29 20 69 73 'd' in "%d") is
a01c0 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 62 79 described.** by
a01d0 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
a01e0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
a01f0 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 65 64 ructure.*/.typed
a0200 65 66 20 73 74 72 75 63 74 20 65 74 5f 69 6e 66 ef struct et_inf
a0210 6f 20 7b 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 o { /* Informa
a0220 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 tion about each
a0230 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 2a 2f 0a format field */.
a0240 20 20 63 68 61 72 20 66 6d 74 74 79 70 65 3b 20 char fmttype;
a0250 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
a0260 65 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 63 e format field c
a0270 6f 64 65 20 6c 65 74 74 65 72 20 2a 2f 0a 20 20 ode letter */.
a0280 65 74 42 79 74 65 20 62 61 73 65 3b 20 20 20 20 etByte base;
a0290 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
a02a0 62 61 73 65 20 66 6f 72 20 72 61 64 69 78 20 63 base for radix c
a02b0 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 65 onversion */. e
a02c0 74 42 79 74 65 20 66 6c 61 67 73 3b 20 20 20 20 tByte flags;
a02d0 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f /* One o
a02e0 72 20 6d 6f 72 65 20 6f 66 20 46 4c 41 47 5f 20 r more of FLAG_
a02f0 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77 20 constants below
a0300 2a 2f 0a 20 20 65 74 42 79 74 65 20 74 79 70 65 */. etByte type
a0310 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
a0320 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61 72 61 Conversion para
a0330 64 69 67 6d 20 2a 2f 0a 20 20 65 74 42 79 74 65 digm */. etByte
a0340 20 63 68 61 72 73 65 74 3b 20 20 20 20 20 20 20 charset;
a0350 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 /* Offset int
a0360 6f 20 61 44 69 67 69 74 73 5b 5d 20 6f 66 20 74 o aDigits[] of t
a0370 68 65 20 64 69 67 69 74 73 20 73 74 72 69 6e 67 he digits string
a0380 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 70 72 65 */. etByte pre
a0390 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f fix; /
a03a0 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 50 * Offset into aP
a03b0 72 65 66 69 78 5b 5d 20 6f 66 20 74 68 65 20 70 refix[] of the p
a03c0 72 65 66 69 78 20 73 74 72 69 6e 67 20 2a 2f 0a refix string */.
a03d0 7d 20 65 74 5f 69 6e 66 6f 3b 0a 0a 2f 2a 0a 2a } et_info;../*.*
a03e0 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 * Allowed values
a03f0 20 66 6f 72 20 65 74 5f 69 6e 66 6f 2e 66 6c 61 for et_info.fla
a0400 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c gs.*/.#define FL
a0410 41 47 5f 53 49 47 4e 45 44 20 20 31 20 20 20 20 AG_SIGNED 1
a0420 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 /* True if the
a0430 76 61 6c 75 65 20 74 6f 20 63 6f 6e 76 65 72 74 value to convert
a0440 20 69 73 20 73 69 67 6e 65 64 20 2a 2f 0a 23 64 is signed */.#d
a0450 65 66 69 6e 65 20 46 4c 41 47 5f 49 4e 54 45 52 efine FLAG_INTER
a0460 4e 20 20 32 20 20 20 20 20 2f 2a 20 54 72 75 65 N 2 /* True
a0470 20 69 66 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c if for internal
a0480 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 use only */.#de
a0490 66 69 6e 65 20 46 4c 41 47 5f 53 54 52 49 4e 47 fine FLAG_STRING
a04a0 20 20 34 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 77 4 /* Allow
a04b0 20 69 6e 66 69 6e 69 74 79 20 70 72 65 63 69 73 infinity precis
a04c0 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 ion */.../*.** T
a04d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 he following tab
a04e0 6c 65 20 69 73 20 73 65 61 72 63 68 65 64 20 6c le is searched l
a04f0 69 6e 65 61 72 6c 79 2c 20 73 6f 20 69 74 20 69 inearly, so it i
a0500 73 20 67 6f 6f 64 20 74 6f 20 70 75 74 20 74 68 s good to put th
a0510 65 0a 2a 2a 20 6d 6f 73 74 20 66 72 65 71 75 65 e.** most freque
a0520 6e 74 6c 79 20 75 73 65 64 20 63 6f 6e 76 65 72 ntly used conver
a0530 73 69 6f 6e 20 74 79 70 65 73 20 66 69 72 73 74 sion types first
a0540 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 ..*/.static cons
a0550 74 20 63 68 61 72 20 61 44 69 67 69 74 73 5b 5d t char aDigits[]
a0560 20 3d 20 22 30 31 32 33 34 35 36 37 38 39 41 42 = "0123456789AB
a0570 43 44 45 46 30 31 32 33 34 35 36 37 38 39 61 62 CDEF0123456789ab
a0580 63 64 65 66 22 3b 0a 73 74 61 74 69 63 20 63 6f cdef";.static co
a0590 6e 73 74 20 63 68 61 72 20 61 50 72 65 66 69 78 nst char aPrefix
a05a0 5b 5d 20 3d 20 22 2d 78 30 5c 30 30 30 58 30 22 [] = "-x0\000X0"
a05b0 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 65 ;.static const e
a05c0 74 5f 69 6e 66 6f 20 66 6d 74 69 6e 66 6f 5b 5d t_info fmtinfo[]
a05d0 20 3d 20 7b 0a 20 20 7b 20 20 27 64 27 2c 20 31 = {. { 'd', 1
a05e0 30 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 20 20 0, 1, etRADIX,
a05f0 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 0, 0 },. {
a0600 20 20 27 73 27 2c 20 20 30 2c 20 34 2c 20 65 74 's', 0, 4, et
a0610 53 54 52 49 4e 47 2c 20 20 20 20 20 30 2c 20 20 STRING, 0,
a0620 30 20 7d 2c 0a 20 20 7b 20 20 27 67 27 2c 20 20 0 },. { 'g',
a0630 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43 2c 0, 1, etGENERIC,
a0640 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 30, 0 },. {
a0650 20 20 27 7a 27 2c 20 20 30 2c 20 34 2c 20 65 74 'z', 0, 4, et
a0660 44 59 4e 53 54 52 49 4e 47 2c 20 20 30 2c 20 20 DYNSTRING, 0,
a0670 30 20 7d 2c 0a 20 20 7b 20 20 27 71 27 2c 20 20 0 },. { 'q',
a0680 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 41 50 0, 4, etSQLESCAP
a0690 45 2c 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b E, 0, 0 },. {
a06a0 20 20 27 51 27 2c 20 20 30 2c 20 34 2c 20 65 74 'Q', 0, 4, et
a06b0 53 51 4c 45 53 43 41 50 45 32 2c 20 30 2c 20 20 SQLESCAPE2, 0,
a06c0 30 20 7d 2c 0a 20 20 7b 20 20 27 77 27 2c 20 20 0 },. { 'w',
a06d0 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 41 50 0, 4, etSQLESCAP
a06e0 45 33 2c 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b E3, 0, 0 },. {
a06f0 20 20 27 63 27 2c 20 20 30 2c 20 30 2c 20 65 74 'c', 0, 0, et
a0700 43 48 41 52 58 2c 20 20 20 20 20 20 30 2c 20 20 CHARX, 0,
a0710 30 20 7d 2c 0a 20 20 7b 20 20 27 6f 27 2c 20 20 0 },. { 'o',
a0720 38 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20 20 8, 0, etRADIX,
a0730 20 20 20 20 30 2c 20 20 32 20 7d 2c 0a 20 20 7b 0, 2 },. {
a0740 20 20 27 75 27 2c 20 31 30 2c 20 30 2c 20 65 74 'u', 10, 0, et
a0750 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20 RADIX, 0,
a0760 30 20 7d 2c 0a 20 20 7b 20 20 27 78 27 2c 20 31 0 },. { 'x', 1
a0770 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20 20 6, 0, etRADIX,
a0780 20 20 20 20 31 36 2c 20 31 20 7d 2c 0a 20 20 7b 16, 1 },. {
a0790 20 20 27 58 27 2c 20 31 36 2c 20 30 2c 20 65 74 'X', 16, 0, et
a07a0 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20 RADIX, 0,
a07b0 34 20 7d 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 4 },.#ifndef SQL
a07c0 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
a07d0 47 5f 50 4f 49 4e 54 0a 20 20 7b 20 20 27 66 27 G_POINT. { 'f'
a07e0 2c 20 20 30 2c 20 31 2c 20 65 74 46 4c 4f 41 54 , 0, 1, etFLOAT
a07f0 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a , 0, 0 },.
a0800 20 20 7b 20 20 27 65 27 2c 20 20 30 2c 20 31 2c { 'e', 0, 1,
a0810 20 65 74 45 58 50 2c 20 20 20 20 20 20 20 20 33 etEXP, 3
a0820 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 45 27 0, 0 },. { 'E'
a0830 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 50 2c 20 , 0, 1, etEXP,
a0840 20 20 20 20 20 20 20 31 34 2c 20 30 20 7d 2c 0a 14, 0 },.
a0850 20 20 7b 20 20 27 47 27 2c 20 20 30 2c 20 31 2c { 'G', 0, 1,
a0860 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20 20 31 etGENERIC, 1
a0870 34 2c 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 4, 0 },.#endif.
a0880 20 7b 20 20 27 69 27 2c 20 31 30 2c 20 31 2c 20 { 'i', 10, 1,
a0890 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c etRADIX, 0,
a08a0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6e 27 2c 0 },. { 'n',
a08b0 20 20 30 2c 20 30 2c 20 65 74 53 49 5a 45 2c 20 0, 0, etSIZE,
a08c0 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 0, 0 },.
a08d0 20 7b 20 20 27 25 27 2c 20 20 30 2c 20 30 2c 20 { '%', 0, 0,
a08e0 65 74 50 45 52 43 45 4e 54 2c 20 20 20 20 30 2c etPERCENT, 0,
a08f0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 70 27 2c 0 },. { 'p',
a0900 20 31 36 2c 20 30 2c 20 65 74 50 4f 49 4e 54 45 16, 0, etPOINTE
a0910 52 2c 20 20 20 20 30 2c 20 20 31 20 7d 2c 0a 0a R, 0, 1 },..
a0920 2f 2a 20 41 6c 6c 20 74 68 65 20 72 65 73 74 20 /* All the rest
a0930 68 61 76 65 20 74 68 65 20 46 4c 41 47 5f 49 4e have the FLAG_IN
a0940 54 45 52 4e 20 62 69 74 20 73 65 74 20 61 6e 64 TERN bit set and
a0950 20 61 72 65 20 74 68 75 73 20 66 6f 72 20 69 6e are thus for in
a0960 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 6f 6e ternal.** use on
a0970 6c 79 20 2a 2f 0a 20 20 7b 20 20 27 54 27 2c 20 ly */. { 'T',
a0980 20 30 2c 20 32 2c 20 65 74 54 4f 4b 45 4e 2c 20 0, 2, etTOKEN,
a0990 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 0, 0 },.
a09a0 7b 20 20 27 53 27 2c 20 20 30 2c 20 32 2c 20 65 { 'S', 0, 2, e
a09b0 74 53 52 43 4c 49 53 54 2c 20 20 20 20 30 2c 20 tSRCLIST, 0,
a09c0 20 30 20 7d 2c 0a 20 20 7b 20 20 27 72 27 2c 20 0 },. { 'r',
a09d0 31 30 2c 20 33 2c 20 65 74 4f 52 44 49 4e 41 4c 10, 3, etORDINAL
a09e0 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 7d 3b , 0, 0 },.};
a09f0 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 ../*.** If SQLIT
a0a00 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
a0a10 50 4f 49 4e 54 20 69 73 20 64 65 66 69 6e 65 64 POINT is defined
a0a20 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 , then none of t
a0a30 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e he floating poin
a0a40 74 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 t.** conversions
a0a50 20 77 69 6c 6c 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 will work..*/.#
a0a60 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
a0a70 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
a0a80 54 0a 2f 2a 0a 2a 2a 20 22 2a 76 61 6c 22 20 69 T./*.** "*val" i
a0a90 73 20 61 20 64 6f 75 62 6c 65 20 73 75 63 68 20 s a double such
a0aa0 74 68 61 74 20 30 2e 31 20 3c 3d 20 2a 76 61 6c that 0.1 <= *val
a0ab0 20 3c 20 31 30 2e 30 0a 2a 2a 20 52 65 74 75 72 < 10.0.** Retur
a0ac0 6e 20 74 68 65 20 61 73 63 69 69 20 63 6f 64 65 n the ascii code
a0ad0 20 66 6f 72 20 74 68 65 20 6c 65 61 64 69 6e 67 for the leading
a0ae0 20 64 69 67 69 74 20 6f 66 20 2a 76 61 6c 2c 20 digit of *val,
a0af0 74 68 65 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c 79 then.** multiply
a0b00 20 22 2a 76 61 6c 22 20 62 79 20 31 30 2e 30 20 "*val" by 10.0
a0b10 74 6f 20 72 65 6e 6f 72 6d 61 6c 69 7a 65 2e 0a to renormalize..
a0b20 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 2a **.** Example:.*
a0b30 2a 20 20 20 20 20 69 6e 70 75 74 3a 20 20 20 20 * input:
a0b40 20 2a 76 61 6c 20 3d 20 33 2e 31 34 31 35 39 0a *val = 3.14159.
a0b50 2a 2a 20 20 20 20 20 6f 75 74 70 75 74 3a 20 20 ** output:
a0b60 20 20 2a 76 61 6c 20 3d 20 31 2e 34 31 35 39 20 *val = 1.4159
a0b70 20 20 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 function retu
a0b80 72 6e 20 3d 20 27 33 27 0a 2a 2a 0a 2a 2a 20 54 rn = '3'.**.** T
a0b90 68 65 20 63 6f 75 6e 74 65 72 20 2a 63 6e 74 20 he counter *cnt
a0ba0 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 is incremented e
a0bb0 61 63 68 20 74 69 6d 65 2e 20 20 41 66 74 65 72 ach time. After
a0bc0 20 63 6f 75 6e 74 65 72 20 65 78 63 65 65 64 73 counter exceeds
a0bd0 0a 2a 2a 20 31 36 20 28 74 68 65 20 6e 75 6d 62 .** 16 (the numb
a0be0 65 72 20 6f 66 20 73 69 67 6e 69 66 69 63 61 6e er of significan
a0bf0 74 20 64 69 67 69 74 73 20 69 6e 20 61 20 36 34 t digits in a 64
a0c00 2d 62 69 74 20 66 6c 6f 61 74 29 20 27 30 27 20 -bit float) '0'
a0c10 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 is.** always ret
a0c20 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 urned..*/.static
a0c30 20 63 68 61 72 20 65 74 5f 67 65 74 64 69 67 69 char et_getdigi
a0c40 74 28 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 t(LONGDOUBLE_TYP
a0c50 45 20 2a 76 61 6c 2c 20 69 6e 74 20 2a 63 6e 74 E *val, int *cnt
a0c60 29 7b 0a 20 20 69 6e 74 20 64 69 67 69 74 3b 0a ){. int digit;.
a0c70 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 LONGDOUBLE_TYP
a0c80 45 20 64 3b 0a 20 20 69 66 28 20 28 2a 63 6e 74 E d;. if( (*cnt
a0c90 29 2b 2b 20 3e 3d 20 31 36 20 29 20 72 65 74 75 )++ >= 16 ) retu
a0ca0 72 6e 20 27 30 27 3b 0a 20 20 64 69 67 69 74 20 rn '0';. digit
a0cb0 3d 20 28 69 6e 74 29 2a 76 61 6c 3b 0a 20 20 64 = (int)*val;. d
a0cc0 20 3d 20 64 69 67 69 74 3b 0a 20 20 64 69 67 69 = digit;. digi
a0cd0 74 20 2b 3d 20 27 30 27 3b 0a 20 20 2a 76 61 6c t += '0';. *val
a0ce0 20 3d 20 28 2a 76 61 6c 20 2d 20 64 29 2a 31 30 = (*val - d)*10
a0cf0 2e 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 63 68 .0;. return (ch
a0d00 61 72 29 64 69 67 69 74 3b 0a 7d 0a 23 65 6e 64 ar)digit;.}.#end
a0d10 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
a0d20 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
a0d30 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e */../*.** Appen
a0d40 64 20 4e 20 73 70 61 63 65 20 63 68 61 72 61 63 d N space charac
a0d50 74 65 72 73 20 74 6f 20 74 68 65 20 67 69 76 65 ters to the give
a0d60 6e 20 73 74 72 69 6e 67 20 62 75 66 66 65 72 2e n string buffer.
a0d70 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
a0d80 61 70 70 65 6e 64 53 70 61 63 65 28 53 74 72 41 appendSpace(StrA
a0d90 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20 69 6e ccum *pAccum, in
a0da0 74 20 4e 29 7b 0a 20 20 73 74 61 74 69 63 20 63 t N){. static c
a0db0 6f 6e 73 74 20 63 68 61 72 20 7a 53 70 61 63 65 onst char zSpace
a0dc0 73 5b 5d 20 3d 20 22 20 20 20 20 20 20 20 20 20 s[] = "
a0dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a0de0 20 20 20 20 22 3b 0a 20 20 77 68 69 6c 65 28 20 ";. while(
a0df0 4e 3e 3d 28 69 6e 74 29 73 69 7a 65 6f 66 28 7a N>=(int)sizeof(z
a0e00 53 70 61 63 65 73 29 2d 31 20 29 7b 0a 20 20 20 Spaces)-1 ){.
a0e10 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
a0e20 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a Append(pAccum, z
a0e30 53 70 61 63 65 73 2c 20 73 69 7a 65 6f 66 28 7a Spaces, sizeof(z
a0e40 53 70 61 63 65 73 29 2d 31 29 3b 0a 20 20 20 20 Spaces)-1);.
a0e50 4e 20 2d 3d 20 73 69 7a 65 6f 66 28 7a 53 70 61 N -= sizeof(zSpa
a0e60 63 65 73 29 2d 31 3b 0a 20 20 7d 0a 20 20 69 66 ces)-1;. }. if
a0e70 28 20 4e 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c ( N>0 ){. sql
a0e80 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 ite3StrAccumAppe
a0e90 6e 64 28 70 41 63 63 75 6d 2c 20 7a 53 70 61 63 nd(pAccum, zSpac
a0ea0 65 73 2c 20 4e 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f es, N);. }.}../
a0eb0 2a 0a 2a 2a 20 4f 6e 20 6d 61 63 68 69 6e 65 73 *.** On machines
a0ec0 20 77 69 74 68 20 61 20 73 6d 61 6c 6c 20 73 74 with a small st
a0ed0 61 63 6b 20 73 69 7a 65 2c 20 79 6f 75 20 63 61 ack size, you ca
a0ee0 6e 20 72 65 64 65 66 69 6e 65 20 74 68 65 0a 2a n redefine the.*
a0ef0 2a 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 * SQLITE_PRINT_B
a0f00 55 46 5f 53 49 5a 45 20 74 6f 20 62 65 20 6c 65 UF_SIZE to be le
a0f10 73 73 20 74 68 61 6e 20 33 35 30 2e 0a 2a 2f 0a ss than 350..*/.
a0f20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 #ifndef SQLITE_P
a0f30 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 0a 23 20 RINT_BUF_SIZE.#
a0f40 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
a0f50 45 5f 53 4d 41 4c 4c 5f 53 54 41 43 4b 29 0a 23 E_SMALL_STACK).#
a0f60 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 define SQLITE
a0f70 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 _PRINT_BUF_SIZE
a0f80 35 30 0a 23 20 65 6c 73 65 0a 23 20 20 20 64 65 50.# else.# de
a0f90 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 49 4e fine SQLITE_PRIN
a0fa0 54 5f 42 55 46 5f 53 49 5a 45 20 33 35 30 0a 23 T_BUF_SIZE 350.#
a0fb0 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 64 endif.#endif.#d
a0fc0 65 66 69 6e 65 20 65 74 42 55 46 53 49 5a 45 20 efine etBUFSIZE
a0fd0 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 SQLITE_PRINT_BUF
a0fe0 5f 53 49 5a 45 20 20 2f 2a 20 53 69 7a 65 20 6f _SIZE /* Size o
a0ff0 66 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 f the output buf
a1000 66 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 fer */../*.** Th
a1010 65 20 72 6f 6f 74 20 70 72 6f 67 72 61 6d 2e 20 e root program.
a1020 20 41 6c 6c 20 76 61 72 69 61 74 69 6f 6e 73 20 All variations
a1030 63 61 6c 6c 20 74 68 69 73 20 63 6f 72 65 2e 0a call this core..
a1040 2a 2a 0a 2a 2a 20 49 4e 50 55 54 53 3a 0a 2a 2a **.** INPUTS:.**
a1050 20 20 20 66 75 6e 63 20 20 20 54 68 69 73 20 69 func This i
a1060 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
a1070 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 69 6e 67 function taking
a1080 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 three arguments
a1090 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 31 .** 1
a10a0 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 . A pointer to a
a10b0 6e 79 74 68 69 6e 67 2e 20 20 53 61 6d 65 20 61 nything. Same a
a10c0 73 20 74 68 65 20 22 61 72 67 22 20 70 61 72 61 s the "arg" para
a10d0 6d 65 74 65 72 2e 0a 2a 2a 20 20 20 20 20 20 20 meter..**
a10e0 20 20 20 20 20 32 2e 20 41 20 70 6f 69 6e 74 65 2. A pointe
a10f0 72 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 r to the list of
a1100 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20 62 characters to b
a1110 65 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 e output.**
a1120 20 20 20 20 20 20 20 20 20 20 28 4e 6f 74 65 2c (Note,
a1130 20 74 68 69 73 20 6c 69 73 74 20 69 73 20 4e 4f this list is NO
a1140 54 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74 65 T null terminate
a1150 64 2e 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 d.).**
a1160 20 20 33 2e 20 41 6e 20 69 6e 74 65 67 65 72 20 3. An integer
a1170 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 number of charac
a1180 74 65 72 73 20 74 6f 20 62 65 20 6f 75 74 70 75 ters to be outpu
a1190 74 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 t..**
a11a0 20 20 20 20 28 4e 6f 74 65 3a 20 54 68 69 73 20 (Note: This
a11b0 6e 75 6d 62 65 72 20 6d 69 67 68 74 20 62 65 20 number might be
a11c0 7a 65 72 6f 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 61 zero.).**.** a
a11d0 72 67 20 20 20 20 54 68 69 73 20 69 73 20 74 68 rg This is th
a11e0 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 79 e pointer to any
a11f0 74 68 69 6e 67 20 77 68 69 63 68 20 77 69 6c 6c thing which will
a1200 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 be passed as th
a1210 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69 e.** fi
a1220 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 rst argument to
a1230 22 66 75 6e 63 22 2e 20 20 55 73 65 20 69 74 20 "func". Use it
a1240 66 6f 72 20 77 68 61 74 65 76 65 72 20 79 6f 75 for whatever you
a1250 20 6c 69 6b 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 66 like..**.** f
a1260 6d 74 20 20 20 20 54 68 69 73 20 69 73 20 74 68 mt This is th
a1270 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 2c e format string,
a1280 20 61 73 20 69 6e 20 74 68 65 20 75 73 75 61 6c as in the usual
a1290 20 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 print..**.**
a12a0 61 70 20 20 20 20 20 54 68 69 73 20 69 73 20 61 ap This is a
a12b0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 6c 69 pointer to a li
a12c0 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e st of arguments.
a12d0 20 20 53 61 6d 65 20 61 73 20 69 6e 0a 2a 2a 20 Same as in.**
a12e0 20 20 20 20 20 20 20 20 20 76 66 70 72 69 6e 74 vfprint
a12f0 2e 0a 2a 2a 0a 2a 2a 20 4f 55 54 50 55 54 53 3a ..**.** OUTPUTS:
a1300 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 54 68 65 .** The
a1310 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 return value is
a1320 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 the total numbe
a1330 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 r of characters
a1340 73 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 20 20 20 sent to.**
a1350 20 20 20 20 74 68 65 20 66 75 6e 63 74 69 6f 6e the function
a1360 20 22 66 75 6e 63 22 2e 20 20 52 65 74 75 72 6e "func". Return
a1370 73 20 2d 31 20 6f 6e 20 61 20 65 72 72 6f 72 2e s -1 on a error.
a1380 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 .**.** Note that
a1390 20 74 68 65 20 6f 72 64 65 72 20 69 6e 20 77 68 the order in wh
a13a0 69 63 68 20 61 75 74 6f 6d 61 74 69 63 20 76 61 ich automatic va
a13b0 72 69 61 62 6c 65 73 20 61 72 65 20 64 65 63 6c riables are decl
a13c0 61 72 65 64 20 62 65 6c 6f 77 0a 2a 2a 20 73 65 ared below.** se
a13d0 65 6d 73 20 74 6f 20 6d 61 6b 65 20 61 20 62 69 ems to make a bi
a13e0 67 20 64 69 66 66 65 72 65 6e 63 65 20 69 6e 20 g difference in
a13f0 64 65 74 65 72 6d 69 6e 69 6e 67 20 68 6f 77 20 determining how
a1400 66 61 73 74 20 74 68 69 73 20 62 65 61 73 74 0a fast this beast.
a1410 2a 2a 20 77 69 6c 6c 20 72 75 6e 2e 0a 2a 2f 0a ** will run..*/.
a1420 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
a1430 6f 69 64 20 73 71 6c 69 74 65 33 56 58 50 72 69 oid sqlite3VXPri
a1440 6e 74 66 28 0a 20 20 53 74 72 41 63 63 75 6d 20 ntf(. StrAccum
a1450 2a 70 41 63 63 75 6d 2c 20 20 20 20 20 20 20 20 *pAccum,
a1460 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 63 63 /* Acc
a1470 75 6d 75 6c 61 74 65 20 72 65 73 75 6c 74 73 20 umulate results
a1480 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73 here */. int us
a1490 65 45 78 74 65 6e 64 65 64 2c 20 20 20 20 20 20 eExtended,
a14a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a14b0 41 6c 6c 6f 77 20 65 78 74 65 6e 64 65 64 20 25 Allow extended %
a14c0 2d 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a 2f 0a -conversions */.
a14d0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 6d const char *fm
a14e0 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t,
a14f0 20 20 20 20 20 2f 2a 20 46 6f 72 6d 61 74 20 73 /* Format s
a1500 74 72 69 6e 67 20 2a 2f 0a 20 20 76 61 5f 6c 69 tring */. va_li
a1510 73 74 20 61 70 20 20 20 20 20 20 20 20 20 20 20 st ap
a1520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a1530 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 29 7b arguments */.){
a1540 0a 20 20 69 6e 74 20 63 3b 20 20 20 20 20 20 20 . int c;
a1550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a1560 20 4e 65 78 74 20 63 68 61 72 61 63 74 65 72 20 Next character
a1570 69 6e 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 in the format st
a1580 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 2a ring */. char *
a1590 62 75 66 70 74 3b 20 20 20 20 20 20 20 20 20 20 bufpt;
a15a0 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
a15b0 74 6f 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f to the conversio
a15c0 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e n buffer */. in
a15d0 74 20 70 72 65 63 69 73 69 6f 6e 3b 20 20 20 20 t precision;
a15e0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 63 /* Prec
a15f0 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 63 75 72 ision of the cur
a1600 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a 20 20 rent field */.
a1610 69 6e 74 20 6c 65 6e 67 74 68 3b 20 20 20 20 20 int length;
a1620 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 /* Le
a1630 6e 67 74 68 20 6f 66 20 74 68 65 20 66 69 65 6c ngth of the fiel
a1640 64 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 3b 20 d */. int idx;
a1650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1660 20 20 2f 2a 20 41 20 67 65 6e 65 72 61 6c 20 70 /* A general p
a1670 75 72 70 6f 73 65 20 6c 6f 6f 70 20 63 6f 75 6e urpose loop coun
a1680 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 77 69 64 ter */. int wid
a1690 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 th;
a16a0 20 20 20 20 2f 2a 20 57 69 64 74 68 20 6f 66 20 /* Width of
a16b0 74 68 65 20 63 75 72 72 65 6e 74 20 66 69 65 6c the current fiel
a16c0 64 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c d */. etByte fl
a16d0 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 3b 20 ag_leftjustify;
a16e0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 2d 22 /* True if "-"
a16f0 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 flag is present
a1700 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 */. etByte fla
a1710 67 5f 70 6c 75 73 73 69 67 6e 3b 20 20 20 20 20 g_plussign;
a1720 20 2f 2a 20 54 72 75 65 20 69 66 20 22 2b 22 20 /* True if "+"
a1730 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 flag is present
a1740 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 */. etByte flag
a1750 5f 62 6c 61 6e 6b 73 69 67 6e 3b 20 20 20 20 20 _blanksign;
a1760 2f 2a 20 54 72 75 65 20 69 66 20 22 20 22 20 66 /* True if " " f
a1770 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a lag is present *
a1780 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f /. etByte flag_
a1790 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 3b 20 2f alternateform; /
a17a0 2a 20 54 72 75 65 20 69 66 20 22 23 22 20 66 6c * True if "#" fl
a17b0 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f ag is present */
a17c0 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 61 . etByte flag_a
a17d0 6c 74 66 6f 72 6d 32 3b 20 20 20 20 20 20 2f 2a ltform2; /*
a17e0 20 54 72 75 65 20 69 66 20 22 21 22 20 66 6c 61 True if "!" fla
a17f0 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a g is present */.
a1800 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 7a 65 etByte flag_ze
a1810 72 6f 70 61 64 3b 20 20 20 20 20 20 20 2f 2a 20 ropad; /*
a1820 54 72 75 65 20 69 66 20 66 69 65 6c 64 20 77 69 True if field wi
a1830 64 74 68 20 63 6f 6e 73 74 61 6e 74 20 73 74 61 dth constant sta
a1840 72 74 73 20 77 69 74 68 20 7a 65 72 6f 20 2a 2f rts with zero */
a1850 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 6c . etByte flag_l
a1860 6f 6e 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ong; /*
a1870 20 54 72 75 65 20 69 66 20 22 6c 22 20 66 6c 61 True if "l" fla
a1880 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a g is present */.
a1890 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 6c 6f etByte flag_lo
a18a0 6e 67 6c 6f 6e 67 3b 20 20 20 20 20 20 2f 2a 20 nglong; /*
a18b0 54 72 75 65 20 69 66 20 74 68 65 20 22 6c 6c 22 True if the "ll"
a18c0 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 flag is present
a18d0 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 64 6f 6e */. etByte don
a18e0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
a18f0 20 2f 2a 20 4c 6f 6f 70 20 74 65 72 6d 69 6e 61 /* Loop termina
a1900 74 69 6f 6e 20 66 6c 61 67 20 2a 2f 0a 20 20 73 tion flag */. s
a1910 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 6c 6f 6e qlite_uint64 lon
a1920 67 76 61 6c 75 65 3b 20 20 20 2f 2a 20 56 61 6c gvalue; /* Val
a1930 75 65 20 66 6f 72 20 69 6e 74 65 67 65 72 20 74 ue for integer t
a1940 79 70 65 73 20 2a 2f 0a 20 20 4c 4f 4e 47 44 4f ypes */. LONGDO
a1950 55 42 4c 45 5f 54 59 50 45 20 72 65 61 6c 76 61 UBLE_TYPE realva
a1960 6c 75 65 3b 20 2f 2a 20 56 61 6c 75 65 20 66 6f lue; /* Value fo
a1970 72 20 72 65 61 6c 20 74 79 70 65 73 20 2a 2f 0a r real types */.
a1980 20 20 63 6f 6e 73 74 20 65 74 5f 69 6e 66 6f 20 const et_info
a1990 2a 69 6e 66 6f 70 3b 20 20 20 20 20 20 2f 2a 20 *infop; /*
a19a0 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 Pointer to the a
a19b0 70 70 72 6f 70 72 69 61 74 65 20 69 6e 66 6f 20 ppropriate info
a19c0 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 63 structure */. c
a19d0 68 61 72 20 62 75 66 5b 65 74 42 55 46 53 49 5a har buf[etBUFSIZ
a19e0 45 5d 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e E]; /* Con
a19f0 76 65 72 73 69 6f 6e 20 62 75 66 66 65 72 20 2a version buffer *
a1a00 2f 0a 20 20 63 68 61 72 20 70 72 65 66 69 78 3b /. char prefix;
a1a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
a1a20 2a 20 50 72 65 66 69 78 20 63 68 61 72 61 63 74 * Prefix charact
a1a30 65 72 2e 20 20 22 2b 22 20 6f 72 20 22 2d 22 20 er. "+" or "-"
a1a40 6f 72 20 22 20 22 20 6f 72 20 27 5c 30 27 2e 20 or " " or '\0'.
a1a50 2a 2f 0a 20 20 65 74 42 79 74 65 20 78 74 79 70 */. etByte xtyp
a1a60 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 e = 0;
a1a70 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61 /* Conversion pa
a1a80 72 61 64 69 67 6d 20 2a 2f 0a 20 20 63 68 61 72 radigm */. char
a1a90 20 2a 7a 45 78 74 72 61 3b 20 20 20 20 20 20 20 *zExtra;
a1aa0 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 /* Extra
a1ab0 6d 65 6d 6f 72 79 20 75 73 65 64 20 66 6f 72 20 memory used for
a1ac0 65 74 54 43 4c 45 53 43 41 50 45 20 63 6f 6e 76 etTCLESCAPE conv
a1ad0 65 72 73 69 6f 6e 73 20 2a 2f 0a 23 69 66 6e 64 ersions */.#ifnd
a1ae0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ef SQLITE_OMIT_F
a1af0 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 LOATING_POINT.
a1b00 69 6e 74 20 20 65 78 70 2c 20 65 32 3b 20 20 20 int exp, e2;
a1b10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 65 78 /* ex
a1b20 70 6f 6e 65 6e 74 20 6f 66 20 72 65 61 6c 20 6e ponent of real n
a1b30 75 6d 62 65 72 73 20 2a 2f 0a 20 20 64 6f 75 62 umbers */. doub
a1b40 6c 65 20 72 6f 75 6e 64 65 72 3b 20 20 20 20 20 le rounder;
a1b50 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 66 /* Used f
a1b60 6f 72 20 72 6f 75 6e 64 69 6e 67 20 66 6c 6f 61 or rounding floa
a1b70 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 ting point value
a1b80 73 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c s */. etByte fl
a1b90 61 67 5f 64 70 3b 20 20 20 20 20 20 20 20 20 20 ag_dp;
a1ba0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 64 65 63 /* True if dec
a1bb0 69 6d 61 6c 20 70 6f 69 6e 74 20 73 68 6f 75 6c imal point shoul
a1bc0 64 20 62 65 20 73 68 6f 77 6e 20 2a 2f 0a 20 20 d be shown */.
a1bd0 65 74 42 79 74 65 20 66 6c 61 67 5f 72 74 7a 3b etByte flag_rtz;
a1be0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
a1bf0 75 65 20 69 66 20 74 72 61 69 6c 69 6e 67 20 7a ue if trailing z
a1c00 65 72 6f 73 20 73 68 6f 75 6c 64 20 62 65 20 72 eros should be r
a1c10 65 6d 6f 76 65 64 20 2a 2f 0a 20 20 65 74 42 79 emoved */. etBy
a1c20 74 65 20 66 6c 61 67 5f 65 78 70 3b 20 20 20 20 te flag_exp;
a1c30 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 /* True t
a1c40 6f 20 66 6f 72 63 65 20 64 69 73 70 6c 61 79 20 o force display
a1c50 6f 66 20 74 68 65 20 65 78 70 6f 6e 65 6e 74 20 of the exponent
a1c60 2a 2f 0a 20 20 69 6e 74 20 6e 73 64 3b 20 20 20 */. int nsd;
a1c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1c80 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 69 67 /* Number of sig
a1c90 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 73 20 nificant digits
a1ca0 72 65 74 75 72 6e 65 64 20 2a 2f 0a 23 65 6e 64 returned */.#end
a1cb0 69 66 0a 0a 20 20 6c 65 6e 67 74 68 20 3d 20 30 if.. length = 0
a1cc0 3b 0a 20 20 62 75 66 70 74 20 3d 20 30 3b 0a 20 ;. bufpt = 0;.
a1cd0 20 66 6f 72 28 3b 20 28 63 3d 28 2a 66 6d 74 29 for(; (c=(*fmt)
a1ce0 29 21 3d 30 3b 20 2b 2b 66 6d 74 29 7b 0a 20 20 )!=0; ++fmt){.
a1cf0 20 20 69 66 28 20 63 21 3d 27 25 27 20 29 7b 0a if( c!='%' ){.
a1d00 20 20 20 20 20 20 69 6e 74 20 61 6d 74 3b 0a 20 int amt;.
a1d10 20 20 20 20 20 62 75 66 70 74 20 3d 20 28 63 68 bufpt = (ch
a1d20 61 72 20 2a 29 66 6d 74 3b 0a 20 20 20 20 20 20 ar *)fmt;.
a1d30 61 6d 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 77 amt = 1;. w
a1d40 68 69 6c 65 28 20 28 63 3d 28 2a 2b 2b 66 6d 74 hile( (c=(*++fmt
a1d50 29 29 21 3d 27 25 27 20 26 26 20 63 21 3d 30 20 ))!='%' && c!=0
a1d60 29 20 61 6d 74 2b 2b 3b 0a 20 20 20 20 20 20 73 ) amt++;. s
a1d70 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 qlite3StrAccumAp
a1d80 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 66 pend(pAccum, buf
a1d90 70 74 2c 20 61 6d 74 29 3b 0a 20 20 20 20 20 20 pt, amt);.
a1da0 69 66 28 20 63 3d 3d 30 20 29 20 62 72 65 61 6b if( c==0 ) break
a1db0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
a1dc0 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 3d 3d 30 20 (c=(*++fmt))==0
a1dd0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
a1de0 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 StrAccumAppend(p
a1df0 41 63 63 75 6d 2c 20 22 25 22 2c 20 31 29 3b 0a Accum, "%", 1);.
a1e00 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
a1e10 20 7d 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 6f }. /* Find o
a1e20 75 74 20 77 68 61 74 20 66 6c 61 67 73 20 61 72 ut what flags ar
a1e30 65 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 20 e present */.
a1e40 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 flag_leftjustif
a1e50 79 20 3d 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 y = flag_plussig
a1e60 6e 20 3d 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 n = flag_blanksi
a1e70 67 6e 20 3d 20 0a 20 20 20 20 20 66 6c 61 67 5f gn = . flag_
a1e80 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 alternateform =
a1e90 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 3d 20 flag_altform2 =
a1ea0 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 3d 20 30 flag_zeropad = 0
a1eb0 3b 0a 20 20 20 20 64 6f 6e 65 20 3d 20 30 3b 0a ;. done = 0;.
a1ec0 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 73 77 do{. sw
a1ed0 69 74 63 68 28 20 63 20 29 7b 0a 20 20 20 20 20 itch( c ){.
a1ee0 20 20 20 63 61 73 65 20 27 2d 27 3a 20 20 20 66 case '-': f
a1ef0 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 lag_leftjustify
a1f00 3d 20 31 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a = 1; break;.
a1f10 20 20 20 20 20 20 20 20 63 61 73 65 20 27 2b 27 case '+'
a1f20 3a 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 : flag_plussig
a1f30 6e 20 3d 20 31 3b 20 20 20 20 20 20 20 20 62 72 n = 1; br
a1f40 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
a1f50 65 20 27 20 27 3a 20 20 20 66 6c 61 67 5f 62 6c e ' ': flag_bl
a1f60 61 6e 6b 73 69 67 6e 20 3d 20 31 3b 20 20 20 20 anksign = 1;
a1f70 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a1f80 20 20 63 61 73 65 20 27 23 27 3a 20 20 20 66 6c case '#': fl
a1f90 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d ag_alternateform
a1fa0 20 3d 20 31 3b 20 20 20 62 72 65 61 6b 3b 0a 20 = 1; break;.
a1fb0 20 20 20 20 20 20 20 63 61 73 65 20 27 21 27 3a case '!':
a1fc0 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 flag_altform2
a1fd0 20 3d 20 31 3b 20 20 20 20 20 20 20 20 62 72 65 = 1; bre
a1fe0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
a1ff0 20 27 30 27 3a 20 20 20 66 6c 61 67 5f 7a 65 72 '0': flag_zer
a2000 6f 70 61 64 20 3d 20 31 3b 20 20 20 20 20 20 20 opad = 1;
a2010 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
a2020 20 64 65 66 61 75 6c 74 3a 20 20 20 20 64 6f 6e default: don
a2030 65 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 e = 1;
a2040 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
a2050 20 20 20 20 7d 0a 20 20 20 20 7d 77 68 69 6c 65 }. }while
a2060 28 20 21 64 6f 6e 65 20 26 26 20 28 63 3d 28 2a ( !done && (c=(*
a2070 2b 2b 66 6d 74 29 29 21 3d 30 20 29 3b 0a 20 20 ++fmt))!=0 );.
a2080 20 20 2f 2a 20 47 65 74 20 74 68 65 20 66 69 65 /* Get the fie
a2090 6c 64 20 77 69 64 74 68 20 2a 2f 0a 20 20 20 20 ld width */.
a20a0 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 69 width = 0;. i
a20b0 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0a 20 20 20 f( c=='*' ){.
a20c0 20 20 20 77 69 64 74 68 20 3d 20 76 61 5f 61 72 width = va_ar
a20d0 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 g(ap,int);.
a20e0 20 69 66 28 20 77 69 64 74 68 3c 30 20 29 7b 0a if( width<0 ){.
a20f0 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 65 66 flag_lef
a2100 74 6a 75 73 74 69 66 79 20 3d 20 31 3b 0a 20 20 tjustify = 1;.
a2110 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 2d 77 width = -w
a2120 69 64 74 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20 idth;. }.
a2130 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a c = *++fmt;.
a2140 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
a2150 20 77 68 69 6c 65 28 20 63 3e 3d 27 30 27 20 26 while( c>='0' &
a2160 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 & c<='9' ){.
a2170 20 20 20 20 77 69 64 74 68 20 3d 20 77 69 64 74 width = widt
a2180 68 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a h*10 + c - '0';.
a2190 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 c = *++f
a21a0 6d 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 mt;. }.
a21b0 7d 0a 20 20 20 20 69 66 28 20 77 69 64 74 68 20 }. if( width
a21c0 3e 20 65 74 42 55 46 53 49 5a 45 2d 31 30 20 29 > etBUFSIZE-10 )
a21d0 7b 0a 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 {. width =
a21e0 65 74 42 55 46 53 49 5a 45 2d 31 30 3b 0a 20 20 etBUFSIZE-10;.
a21f0 20 20 7d 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 }. /* Get t
a2200 68 65 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0a he precision */.
a2210 20 20 20 20 69 66 28 20 63 3d 3d 27 2e 27 20 29 if( c=='.' )
a2220 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 69 6f {. precisio
a2230 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 20 3d n = 0;. c =
a2240 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 69 *++fmt;. i
a2250 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0a 20 20 20 f( c=='*' ){.
a2260 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d precision =
a2270 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b va_arg(ap,int);
a2280 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 . if( pre
a2290 63 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 63 69 cision<0 ) preci
a22a0 73 69 6f 6e 20 3d 20 2d 70 72 65 63 69 73 69 6f sion = -precisio
a22b0 6e 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a n;. c = *
a22c0 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 65 6c ++fmt;. }el
a22d0 73 65 7b 0a 20 20 20 20 20 20 20 20 77 68 69 6c se{. whil
a22e0 65 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d e( c>='0' && c<=
a22f0 27 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 '9' ){.
a2300 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 70 72 65 precision = pre
a2310 63 69 73 69 6f 6e 2a 31 30 20 2b 20 63 20 2d 20 cision*10 + c -
a2320 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 63 '0';. c
a2330 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 = *++fmt;.
a2340 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
a2350 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 72 }else{. pr
a2360 65 63 69 73 69 6f 6e 20 3d 20 2d 31 3b 0a 20 20 ecision = -1;.
a2370 20 20 7d 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 }. /* Get t
a2380 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 79 he conversion ty
a2390 70 65 20 6d 6f 64 69 66 69 65 72 20 2a 2f 0a 20 pe modifier */.
a23a0 20 20 20 69 66 28 20 63 3d 3d 27 6c 27 20 29 7b if( c=='l' ){
a23b0 0a 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 . flag_long
a23c0 20 3d 20 31 3b 0a 20 20 20 20 20 20 63 20 3d 20 = 1;. c =
a23d0 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 69 66 *++fmt;. if
a23e0 28 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20 ( c=='l' ){.
a23f0 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e flag_longlon
a2400 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 63 g = 1;. c
a2410 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 = *++fmt;.
a2420 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a2430 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 flag_longlong =
a2440 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0;. }. }
a2450 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6c 61 67 else{. flag
a2460 5f 6c 6f 6e 67 20 3d 20 66 6c 61 67 5f 6c 6f 6e _long = flag_lon
a2470 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 20 20 7d glong = 0;. }
a2480 0a 20 20 20 20 2f 2a 20 46 65 74 63 68 20 74 68 . /* Fetch th
a2490 65 20 69 6e 66 6f 20 65 6e 74 72 79 20 66 6f 72 e info entry for
a24a0 20 74 68 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 the field */.
a24b0 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d 74 69 6e infop = &fmtin
a24c0 66 6f 5b 30 5d 3b 0a 20 20 20 20 78 74 79 70 65 fo[0];. xtype
a24d0 20 3d 20 65 74 49 4e 56 41 4c 49 44 3b 0a 20 20 = etINVALID;.
a24e0 20 20 66 6f 72 28 69 64 78 3d 30 3b 20 69 64 78 for(idx=0; idx
a24f0 3c 41 72 72 61 79 53 69 7a 65 28 66 6d 74 69 6e <ArraySize(fmtin
a2500 66 6f 29 3b 20 69 64 78 2b 2b 29 7b 0a 20 20 20 fo); idx++){.
a2510 20 20 20 69 66 28 20 63 3d 3d 66 6d 74 69 6e 66 if( c==fmtinf
a2520 6f 5b 69 64 78 5d 2e 66 6d 74 74 79 70 65 20 29 o[idx].fmttype )
a2530 7b 0a 20 20 20 20 20 20 20 20 69 6e 66 6f 70 20 {. infop
a2540 3d 20 26 66 6d 74 69 6e 66 6f 5b 69 64 78 5d 3b = &fmtinfo[idx];
a2550 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 73 65 . if( use
a2560 45 78 74 65 6e 64 65 64 20 7c 7c 20 28 69 6e 66 Extended || (inf
a2570 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 op->flags & FLAG
a2580 5f 49 4e 54 45 52 4e 29 3d 3d 30 20 29 7b 0a 20 _INTERN)==0 ){.
a2590 20 20 20 20 20 20 20 20 20 78 74 79 70 65 20 3d xtype =
a25a0 20 69 6e 66 6f 70 2d 3e 74 79 70 65 3b 0a 20 20 infop->type;.
a25b0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
a25c0 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 return;.
a25d0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a25e0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
a25f0 20 20 20 20 7d 0a 20 20 20 20 7a 45 78 74 72 61 }. zExtra
a2600 20 3d 20 30 3b 0a 0a 0a 20 20 20 20 2f 2a 20 4c = 0;... /* L
a2610 69 6d 69 74 20 74 68 65 20 70 72 65 63 69 73 69 imit the precisi
a2620 6f 6e 20 74 6f 20 70 72 65 76 65 6e 74 20 6f 76 on to prevent ov
a2630 65 72 66 6c 6f 77 69 6e 67 20 62 75 66 5b 5d 20 erflowing buf[]
a2640 64 75 72 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f during conversio
a2650 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 72 65 n */. if( pre
a2660 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a 45 cision>etBUFSIZE
a2670 2d 34 30 20 26 26 20 28 69 6e 66 6f 70 2d 3e 66 -40 && (infop->f
a2680 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 54 52 49 lags & FLAG_STRI
a2690 4e 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 NG)==0 ){.
a26a0 70 72 65 63 69 73 69 6f 6e 20 3d 20 65 74 42 55 precision = etBU
a26b0 46 53 49 5a 45 2d 34 30 3b 0a 20 20 20 20 7d 0a FSIZE-40;. }.
a26c0 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 41 . /*. ** A
a26d0 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 76 61 t this point, va
a26e0 72 69 61 62 6c 65 73 20 61 72 65 20 69 6e 69 74 riables are init
a26f0 69 61 6c 69 7a 65 64 20 61 73 20 66 6f 6c 6c 6f ialized as follo
a2700 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a ws:. **. *
a2710 2a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 * flag_alterna
a2720 74 65 66 6f 72 6d 20 20 20 20 20 20 20 20 20 20 teform
a2730 54 52 55 45 20 69 66 20 61 20 27 23 27 20 69 73 TRUE if a '#' is
a2740 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a present.. **
a2750 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 flag_altform2
a2760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 T
a2770 52 55 45 20 69 66 20 61 20 27 21 27 20 69 73 20 RUE if a '!' is
a2780 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 present.. **
a2790 20 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 flag_plussign
a27a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 TR
a27b0 55 45 20 69 66 20 61 20 27 2b 27 20 69 73 20 70 UE if a '+' is p
a27c0 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 resent.. **
a27d0 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 flag_leftjustif
a27e0 79 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55 y TRU
a27f0 45 20 69 66 20 61 20 27 2d 27 20 69 73 20 70 72 E if a '-' is pr
a2800 65 73 65 6e 74 20 6f 72 20 69 66 20 74 68 65 0a esent or if the.
a2810 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 **
a2820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2830 20 20 20 20 20 66 69 65 6c 64 20 77 69 64 74 68 field width
a2840 20 77 61 73 20 6e 65 67 61 74 69 76 65 2e 0a 20 was negative..
a2850 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 7a 65 72 ** flag_zer
a2860 6f 70 61 64 20 20 20 20 20 20 20 20 20 20 20 20 opad
a2870 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65 20 TRUE if the
a2880 77 69 64 74 68 20 62 65 67 61 6e 20 77 69 74 68 width began with
a2890 20 30 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 0.. ** fla
a28a0 67 5f 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20 g_long
a28b0 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 TRUE if
a28c0 20 74 68 65 20 6c 65 74 74 65 72 20 27 6c 27 20 the letter 'l'
a28d0 28 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a 20 (ell) prefixed.
a28e0 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **
a28f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2900 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73 69 the conversi
a2910 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 on character..
a2920 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 ** flag_long
a2930 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20 20 20 long
a2940 20 20 20 54 52 55 45 20 69 66 20 74 68 65 20 6c TRUE if the l
a2950 65 74 74 65 72 20 27 6c 6c 27 20 28 65 6c 6c 20 etter 'll' (ell
a2960 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a 20 20 ell) prefixed.
a2970 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 **
a2980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2990 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f the conversio
a29a0 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20 n character..
a29b0 20 2a 2a 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b ** flag_blank
a29c0 73 69 67 6e 20 20 20 20 20 20 20 20 20 20 20 20 sign
a29d0 20 20 54 52 55 45 20 69 66 20 61 20 27 20 27 20 TRUE if a ' '
a29e0 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 is present..
a29f0 2a 2a 20 20 20 77 69 64 74 68 20 20 20 20 20 20 ** width
a2a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2a10 20 54 68 65 20 73 70 65 63 69 66 69 65 64 20 66 The specified f
a2a20 69 65 6c 64 20 77 69 64 74 68 2e 20 20 54 68 69 ield width. Thi
a2a30 73 20 69 73 0a 20 20 20 20 2a 2a 20 20 20 20 20 s is. **
a2a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2a50 20 20 20 20 20 20 20 20 20 20 61 6c 77 61 79 73 always
a2a60 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 20 non-negative.
a2a70 5a 65 72 6f 20 69 73 20 74 68 65 20 64 65 66 61 Zero is the defa
a2a80 75 6c 74 2e 0a 20 20 20 20 2a 2a 20 20 20 70 72 ult.. ** pr
a2a90 65 63 69 73 69 6f 6e 20 20 20 20 20 20 20 20 20 ecision
a2aa0 20 20 20 20 20 20 20 20 20 20 54 68 65 20 73 70 The sp
a2ab0 65 63 69 66 69 65 64 20 70 72 65 63 69 73 69 6f ecified precisio
a2ac0 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a n. The default.
a2ad0 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 **
a2ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2af0 20 20 20 20 20 69 73 20 2d 31 2e 0a 20 20 20 20 is -1..
a2b00 2a 2a 20 20 20 78 74 79 70 65 20 20 20 20 20 20 ** xtype
a2b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2b20 20 54 68 65 20 63 6c 61 73 73 20 6f 66 20 74 68 The class of th
a2b30 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 20 20 e conversion..
a2b40 20 20 2a 2a 20 20 20 69 6e 66 6f 70 20 20 20 20 ** infop
a2b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2b60 20 20 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 Pointer to th
a2b70 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 69 6e e appropriate in
a2b80 66 6f 20 73 74 72 75 63 74 2e 0a 20 20 20 20 2a fo struct.. *
a2b90 2f 0a 20 20 20 20 73 77 69 74 63 68 28 20 78 74 /. switch( xt
a2ba0 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 ype ){. cas
a2bb0 65 20 65 74 50 4f 49 4e 54 45 52 3a 0a 20 20 20 e etPOINTER:.
a2bc0 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f flag_longlo
a2bd0 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 72 ng = sizeof(char
a2be0 2a 29 3d 3d 73 69 7a 65 6f 66 28 69 36 34 29 3b *)==sizeof(i64);
a2bf0 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f . flag_lo
a2c00 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 72 ng = sizeof(char
a2c10 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c 6f 6e 67 20 *)==sizeof(long
a2c20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a int);. /*
a2c30 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e Fall through in
a2c40 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 to the next case
a2c50 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 65 */. case e
a2c60 74 4f 52 44 49 4e 41 4c 3a 0a 20 20 20 20 20 20 tORDINAL:.
a2c70 63 61 73 65 20 65 74 52 41 44 49 58 3a 0a 20 20 case etRADIX:.
a2c80 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f 70 2d if( infop-
a2c90 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 49 >flags & FLAG_SI
a2ca0 47 4e 45 44 20 29 7b 0a 20 20 20 20 20 20 20 20 GNED ){.
a2cb0 20 20 69 36 34 20 76 3b 0a 20 20 20 20 20 20 20 i64 v;.
a2cc0 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 if( flag_long
a2cd0 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20 long ){.
a2ce0 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28 61 v = va_arg(a
a2cf0 70 2c 69 36 34 29 3b 0a 20 20 20 20 20 20 20 20 p,i64);.
a2d00 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 }else if( flag
a2d10 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 _long ){.
a2d20 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28 v = va_arg(
a2d30 61 70 2c 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 ap,long int);.
a2d40 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
a2d50 20 20 20 20 20 20 20 20 20 20 20 76 20 3d 20 76 v = v
a2d60 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 a_arg(ap,int);.
a2d70 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
a2d80 20 20 20 20 20 69 66 28 20 76 3c 30 20 29 7b 0a if( v<0 ){.
a2d90 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 long
a2da0 76 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20 20 value = -v;.
a2db0 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d prefix =
a2dc0 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 20 20 '-';.
a2dd0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
a2de0 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 longvalue = v
a2df0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 ;. if
a2e00 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 ( flag_plussign
a2e10 29 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 ) prefix
a2e20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 = '+';.
a2e30 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 else if( flag
a2e40 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20 70 72 _blanksign ) pr
a2e50 65 66 69 78 20 3d 20 27 20 27 3b 0a 20 20 20 20 efix = ' ';.
a2e60 20 20 20 20 20 20 20 20 65 6c 73 65 20 20 20 20 else
a2e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2e80 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 prefix = 0;.
a2e90 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
a2ea0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a2eb0 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e if( flag_lon
a2ec0 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 glong ){.
a2ed0 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d longvalue =
a2ee0 20 76 61 5f 61 72 67 28 61 70 2c 75 36 34 29 3b va_arg(ap,u64);
a2ef0 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
a2f00 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 20 29 if( flag_long )
a2f10 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f {. lo
a2f20 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67 ngvalue = va_arg
a2f30 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 6c 6f 6e (ap,unsigned lon
a2f40 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 g int);.
a2f50 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
a2f60 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d longvalue =
a2f70 20 76 61 5f 61 72 67 28 61 70 2c 75 6e 73 69 67 va_arg(ap,unsig
a2f80 6e 65 64 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 ned int);.
a2f90 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
a2fa0 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 prefix = 0;.
a2fb0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
a2fc0 28 20 6c 6f 6e 67 76 61 6c 75 65 3d 3d 30 20 29 ( longvalue==0 )
a2fd0 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 flag_alternatef
a2fe0 6f 72 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 orm = 0;.
a2ff0 20 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61 if( flag_zeropa
a3000 64 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3c 77 d && precision<w
a3010 69 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30 29 idth-(prefix!=0)
a3020 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 ){. pr
a3030 65 63 69 73 69 6f 6e 20 3d 20 77 69 64 74 68 2d ecision = width-
a3040 28 70 72 65 66 69 78 21 3d 30 29 3b 0a 20 20 20 (prefix!=0);.
a3050 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 }. b
a3060 75 66 70 74 20 3d 20 26 62 75 66 5b 65 74 42 55 ufpt = &buf[etBU
a3070 46 53 49 5a 45 2d 31 5d 3b 0a 20 20 20 20 20 20 FSIZE-1];.
a3080 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 4f if( xtype==etO
a3090 52 44 49 4e 41 4c 20 29 7b 0a 20 20 20 20 20 20 RDINAL ){.
a30a0 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 static const
a30b0 20 63 68 61 72 20 7a 4f 72 64 5b 5d 20 3d 20 22 char zOrd[] = "
a30c0 74 68 73 74 6e 64 72 64 22 3b 0a 20 20 20 20 20 thstndrd";.
a30d0 20 20 20 20 20 69 6e 74 20 78 20 3d 20 28 69 6e int x = (in
a30e0 74 29 28 6c 6f 6e 67 76 61 6c 75 65 20 25 20 31 t)(longvalue % 1
a30f0 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 0);. if
a3100 28 20 78 3e 3d 34 20 7c 7c 20 28 6c 6f 6e 67 76 ( x>=4 || (longv
a3110 61 6c 75 65 2f 31 30 29 25 31 30 3d 3d 31 20 29 alue/10)%10==1 )
a3120 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 20 {. x
a3130 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d = 0;. }
a3140 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 5b 65 . buf[e
a3150 74 42 55 46 53 49 5a 45 2d 33 5d 20 3d 20 7a 4f tBUFSIZE-3] = zO
a3160 72 64 5b 78 2a 32 5d 3b 0a 20 20 20 20 20 20 20 rd[x*2];.
a3170 20 20 20 62 75 66 5b 65 74 42 55 46 53 49 5a 45 buf[etBUFSIZE
a3180 2d 32 5d 20 3d 20 7a 4f 72 64 5b 78 2a 32 2b 31 -2] = zOrd[x*2+1
a3190 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 ];. buf
a31a0 70 74 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20 20 pt -= 2;.
a31b0 20 7d 0a 20 20 20 20 20 20 20 20 7b 0a 20 20 20 }. {.
a31c0 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 20 register
a31d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 73 65 74 const char *cset
a31e0 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 20 72 65 ; /* Use re
a31f0 67 69 73 74 65 72 73 20 66 6f 72 20 73 70 65 65 gisters for spee
a3200 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 d */. r
a3210 65 67 69 73 74 65 72 20 69 6e 74 20 62 61 73 65 egister int base
a3220 3b 0a 20 20 20 20 20 20 20 20 20 20 63 73 65 74 ;. cset
a3230 20 3d 20 26 61 44 69 67 69 74 73 5b 69 6e 66 6f = &aDigits[info
a3240 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a 20 20 20 p->charset];.
a3250 20 20 20 20 20 20 20 62 61 73 65 20 3d 20 69 6e base = in
a3260 66 6f 70 2d 3e 62 61 73 65 3b 0a 20 20 20 20 20 fop->base;.
a3270 20 20 20 20 20 64 6f 7b 20 20 20 20 20 20 20 20 do{
a3280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a32a0 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 6f /* Convert to
a32b0 20 61 73 63 69 69 20 2a 2f 0a 20 20 20 20 20 20 ascii */.
a32c0 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29 *(--bufpt)
a32d0 20 3d 20 63 73 65 74 5b 6c 6f 6e 67 76 61 6c 75 = cset[longvalu
a32e0 65 25 62 61 73 65 5d 3b 0a 20 20 20 20 20 20 20 e%base];.
a32f0 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d longvalue =
a3300 20 6c 6f 6e 67 76 61 6c 75 65 2f 62 61 73 65 3b longvalue/base;
a3310 0a 20 20 20 20 20 20 20 20 20 20 7d 77 68 69 6c . }whil
a3320 65 28 20 6c 6f 6e 67 76 61 6c 75 65 3e 30 20 29 e( longvalue>0 )
a3330 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
a3340 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e length = (in
a3350 74 29 28 26 62 75 66 5b 65 74 42 55 46 53 49 5a t)(&buf[etBUFSIZ
a3360 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20 20 E-1]-bufpt);.
a3370 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 65 for(idx=pre
a3380 63 69 73 69 6f 6e 2d 6c 65 6e 67 74 68 3b 20 69 cision-length; i
a3390 64 78 3e 30 3b 20 69 64 78 2d 2d 29 7b 0a 20 20 dx>0; idx--){.
a33a0 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 *(--bufp
a33b0 74 29 20 3d 20 27 30 27 3b 20 20 20 20 20 20 20 t) = '0';
a33c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a33d0 20 20 20 20 20 20 2f 2a 20 5a 65 72 6f 20 70 61 /* Zero pa
a33e0 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 d */. }.
a33f0 20 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69 if( prefi
a3400 78 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d x ) *(--bufpt) =
a3410 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 prefix;
a3420 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 73 69 /* Add si
a3430 67 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 gn */. if
a3440 28 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 ( flag_alternate
a3450 66 6f 72 6d 20 26 26 20 69 6e 66 6f 70 2d 3e 70 form && infop->p
a3460 72 65 66 69 78 20 29 7b 20 20 20 20 20 20 2f 2a refix ){ /*
a3470 20 41 64 64 20 22 30 22 20 6f 72 20 22 30 78 22 Add "0" or "0x"
a3480 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 63 6f */. co
a3490 6e 73 74 20 63 68 61 72 20 2a 70 72 65 3b 0a 20 nst char *pre;.
a34a0 20 20 20 20 20 20 20 20 20 63 68 61 72 20 78 3b char x;
a34b0 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 20 3d . pre =
a34c0 20 26 61 50 72 65 66 69 78 5b 69 6e 66 6f 70 2d &aPrefix[infop-
a34d0 3e 70 72 65 66 69 78 5d 3b 0a 20 20 20 20 20 20 >prefix];.
a34e0 20 20 20 20 66 6f 72 28 3b 20 28 78 3d 28 2a 70 for(; (x=(*p
a34f0 72 65 29 29 21 3d 30 3b 20 70 72 65 2b 2b 29 20 re))!=0; pre++)
a3500 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 78 3b 0a *(--bufpt) = x;.
a3510 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a3520 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e 74 29 length = (int)
a3530 28 26 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d (&buf[etBUFSIZE-
a3540 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20 20 20 20 1]-bufpt);.
a3550 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a3560 63 61 73 65 20 65 74 46 4c 4f 41 54 3a 0a 20 20 case etFLOAT:.
a3570 20 20 20 20 63 61 73 65 20 65 74 45 58 50 3a 0a case etEXP:.
a3580 20 20 20 20 20 20 63 61 73 65 20 65 74 47 45 4e case etGEN
a3590 45 52 49 43 3a 0a 20 20 20 20 20 20 20 20 72 65 ERIC:. re
a35a0 61 6c 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67 alvalue = va_arg
a35b0 28 61 70 2c 64 6f 75 62 6c 65 29 3b 0a 23 69 66 (ap,double);.#if
a35c0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
a35d0 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a _FLOATING_POINT.
a35e0 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 63 if( prec
a35f0 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 63 69 73 ision<0 ) precis
a3600 69 6f 6e 20 3d 20 36 3b 20 20 20 20 20 20 20 20 ion = 6;
a3610 20 2f 2a 20 53 65 74 20 64 65 66 61 75 6c 74 20 /* Set default
a3620 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20 precision */.
a3630 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 if( precisi
a3640 6f 6e 3e 65 74 42 55 46 53 49 5a 45 2f 32 2d 31 on>etBUFSIZE/2-1
a3650 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 0 ) precision =
a3660 65 74 42 55 46 53 49 5a 45 2f 32 2d 31 30 3b 0a etBUFSIZE/2-10;.
a3670 20 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c if( real
a3680 76 61 6c 75 65 3c 30 2e 30 20 29 7b 0a 20 20 20 value<0.0 ){.
a3690 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65 realvalue
a36a0 20 3d 20 2d 72 65 61 6c 76 61 6c 75 65 3b 0a 20 = -realvalue;.
a36b0 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 prefix
a36c0 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 7d = '-';. }
a36d0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
a36e0 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 if( flag_plussig
a36f0 6e 20 29 20 20 20 20 20 20 20 20 20 20 70 72 65 n ) pre
a3700 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 fix = '+';.
a3710 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c else if( fl
a3720 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20 ag_blanksign )
a3730 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b 0a prefix = ' ';.
a3740 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20 else
a3750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3760 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 prefix =
a3770 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 0;. }.
a3780 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d if( xtype==
a3790 65 74 47 45 4e 45 52 49 43 20 26 26 20 70 72 65 etGENERIC && pre
a37a0 63 69 73 69 6f 6e 3e 30 20 29 20 70 72 65 63 69 cision>0 ) preci
a37b0 73 69 6f 6e 2d 2d 3b 0a 23 69 66 20 30 0a 20 20 sion--;.#if 0.
a37c0 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64 69 6e /* Roundin
a37d0 67 20 77 6f 72 6b 73 20 6c 69 6b 65 20 42 53 44 g works like BSD
a37e0 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 61 when the consta
a37f0 6e 74 20 30 2e 34 39 39 39 20 69 73 20 75 73 65 nt 0.4999 is use
a3800 64 2e 20 20 57 69 65 72 64 21 20 2a 2f 0a 20 20 d. Wierd! */.
a3810 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 for(idx=pr
a3820 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 72 ecision, rounder
a3830 3d 30 2e 34 39 39 39 3b 20 69 64 78 3e 30 3b 20 =0.4999; idx>0;
a3840 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d idx--, rounder*=
a3850 30 2e 31 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 0.1);.#else.
a3860 20 20 20 20 2f 2a 20 49 74 20 6d 61 6b 65 73 20 /* It makes
a3870 6d 6f 72 65 20 73 65 6e 73 65 20 74 6f 20 75 73 more sense to us
a3880 65 20 30 2e 35 20 2a 2f 0a 20 20 20 20 20 20 20 e 0.5 */.
a3890 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69 for(idx=precisi
a38a0 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 35 3b on, rounder=0.5;
a38b0 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72 idx>0; idx--, r
a38c0 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 7b 7d 0a 23 ounder*=0.1){}.#
a38d0 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 69 66 endif. if
a38e0 28 20 78 74 79 70 65 3d 3d 65 74 46 4c 4f 41 54 ( xtype==etFLOAT
a38f0 20 29 20 72 65 61 6c 76 61 6c 75 65 20 2b 3d 20 ) realvalue +=
a3900 72 6f 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 20 rounder;.
a3910 20 2f 2a 20 4e 6f 72 6d 61 6c 69 7a 65 20 72 65 /* Normalize re
a3920 61 6c 76 61 6c 75 65 20 74 6f 20 77 69 74 68 69 alvalue to withi
a3930 6e 20 31 30 2e 30 20 3e 20 72 65 61 6c 76 61 6c n 10.0 > realval
a3940 75 65 20 3e 3d 20 31 2e 30 20 2a 2f 0a 20 20 20 ue >= 1.0 */.
a3950 20 20 20 20 20 65 78 70 20 3d 20 30 3b 0a 20 20 exp = 0;.
a3960 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
a3970 33 49 73 4e 61 4e 28 28 64 6f 75 62 6c 65 29 72 3IsNaN((double)r
a3980 65 61 6c 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 ealvalue) ){.
a3990 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 bufpt = "
a39a0 4e 61 4e 22 3b 0a 20 20 20 20 20 20 20 20 20 20 NaN";.
a39b0 6c 65 6e 67 74 68 20 3d 20 33 3b 0a 20 20 20 20 length = 3;.
a39c0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
a39d0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
a39e0 66 28 20 72 65 61 6c 76 61 6c 75 65 3e 30 2e 30 f( realvalue>0.0
a39f0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 ){. wh
a3a00 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d ile( realvalue>=
a3a10 31 65 33 32 20 26 26 20 65 78 70 3c 3d 33 35 30 1e32 && exp<=350
a3a20 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d ){ realvalue *=
a3a30 20 31 65 2d 33 32 3b 20 65 78 70 2b 3d 33 32 3b 1e-32; exp+=32;
a3a40 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 }. whi
a3a50 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 le( realvalue>=1
a3a60 65 38 20 26 26 20 65 78 70 3c 3d 33 35 30 20 29 e8 && exp<=350 )
a3a70 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 { realvalue *= 1
a3a80 65 2d 38 3b 20 65 78 70 2b 3d 38 3b 20 7d 0a 20 e-8; exp+=8; }.
a3a90 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 while(
a3aa0 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20 realvalue>=10.0
a3ab0 26 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 72 && exp<=350 ){ r
a3ac0 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b ealvalue *= 0.1;
a3ad0 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 exp++; }.
a3ae0 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 while( realv
a3af0 61 6c 75 65 3c 31 65 2d 38 20 29 7b 20 72 65 61 alue<1e-8 ){ rea
a3b00 6c 76 61 6c 75 65 20 2a 3d 20 31 65 38 3b 20 65 lvalue *= 1e8; e
a3b10 78 70 2d 3d 38 3b 20 7d 0a 20 20 20 20 20 20 20 xp-=8; }.
a3b20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 while( realva
a3b30 6c 75 65 3c 31 2e 30 20 29 7b 20 72 65 61 6c 76 lue<1.0 ){ realv
a3b40 61 6c 75 65 20 2a 3d 20 31 30 2e 30 3b 20 65 78 alue *= 10.0; ex
a3b50 70 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 p--; }.
a3b60 20 69 66 28 20 65 78 70 3e 33 35 30 20 29 7b 0a if( exp>350 ){.
a3b70 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
a3b80 70 72 65 66 69 78 3d 3d 27 2d 27 20 29 7b 0a 20 prefix=='-' ){.
a3b90 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 buf
a3ba0 70 74 20 3d 20 22 2d 49 6e 66 22 3b 0a 20 20 20 pt = "-Inf";.
a3bb0 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 }else i
a3bc0 66 28 20 70 72 65 66 69 78 3d 3d 27 2b 27 20 29 f( prefix=='+' )
a3bd0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
a3be0 62 75 66 70 74 20 3d 20 22 2b 49 6e 66 22 3b 0a bufpt = "+Inf";.
a3bf0 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 }els
a3c00 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e{.
a3c10 20 62 75 66 70 74 20 3d 20 22 49 6e 66 22 3b 0a bufpt = "Inf";.
a3c20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
a3c30 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 length
a3c40 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
a3c50 33 30 28 62 75 66 70 74 29 3b 0a 20 20 20 20 20 30(bufpt);.
a3c60 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
a3c70 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a3c80 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66 70 }. bufp
a3c90 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20 t = buf;.
a3ca0 20 2f 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 /*. ** I
a3cb0 66 20 74 68 65 20 66 69 65 6c 64 20 74 79 70 65 f the field type
a3cc0 20 69 73 20 65 74 47 45 4e 45 52 49 43 2c 20 74 is etGENERIC, t
a3cd0 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 6f 20 65 hen convert to e
a3ce0 69 74 68 65 72 20 65 74 45 58 50 0a 20 20 20 20 ither etEXP.
a3cf0 20 20 20 20 2a 2a 20 6f 72 20 65 74 46 4c 4f 41 ** or etFLOA
a3d00 54 2c 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 T, as appropriat
a3d10 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 e.. */.
a3d20 20 20 20 20 20 20 66 6c 61 67 5f 65 78 70 20 3d flag_exp =
a3d30 20 78 74 79 70 65 3d 3d 65 74 45 58 50 3b 0a 20 xtype==etEXP;.
a3d40 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 if( xtype
a3d50 21 3d 65 74 46 4c 4f 41 54 20 29 7b 0a 20 20 20 !=etFLOAT ){.
a3d60 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65 realvalue
a3d70 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20 20 += rounder;.
a3d80 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c 76 if( realv
a3d90 61 6c 75 65 3e 3d 31 30 2e 30 20 29 7b 20 72 65 alue>=10.0 ){ re
a3da0 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b 20 alvalue *= 0.1;
a3db0 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 exp++; }.
a3dc0 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 }. if( x
a3dd0 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43 20 type==etGENERIC
a3de0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61 ){. fla
a3df0 67 5f 72 74 7a 20 3d 20 21 66 6c 61 67 5f 61 6c g_rtz = !flag_al
a3e00 74 65 72 6e 61 74 65 66 6f 72 6d 3b 0a 20 20 20 ternateform;.
a3e10 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3c 2d if( exp<-
a3e20 34 20 7c 7c 20 65 78 70 3e 70 72 65 63 69 73 69 4 || exp>precisi
a3e30 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 on ){.
a3e40 20 20 78 74 79 70 65 20 3d 20 65 74 45 58 50 3b xtype = etEXP;
a3e50 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
a3e60 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 72 {. pr
a3e70 65 63 69 73 69 6f 6e 20 3d 20 70 72 65 63 69 73 ecision = precis
a3e80 69 6f 6e 20 2d 20 65 78 70 3b 0a 20 20 20 20 20 ion - exp;.
a3e90 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65 xtype = e
a3ea0 74 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20 20 20 tFLOAT;.
a3eb0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 }. }els
a3ec0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61 e{. fla
a3ed0 67 5f 72 74 7a 20 3d 20 30 3b 0a 20 20 20 20 20 g_rtz = 0;.
a3ee0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
a3ef0 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b xtype==etEXP ){
a3f00 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d 20 . e2 =
a3f10 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0;. }else
a3f20 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d {. e2 =
a3f30 20 65 78 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a exp;. }.
a3f40 20 20 20 20 20 20 20 20 6e 73 64 20 3d 20 30 3b nsd = 0;
a3f50 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 64 70 . flag_dp
a3f60 20 3d 20 28 70 72 65 63 69 73 69 6f 6e 3e 30 20 = (precision>0
a3f70 3f 31 3a 30 29 20 7c 20 66 6c 61 67 5f 61 6c 74 ?1:0) | flag_alt
a3f80 65 72 6e 61 74 65 66 6f 72 6d 20 7c 20 66 6c 61 ernateform | fla
a3f90 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a 20 20 20 20 g_altform2;.
a3fa0 20 20 20 20 2f 2a 20 54 68 65 20 73 69 67 6e 20 /* The sign
a3fb0 69 6e 20 66 72 6f 6e 74 20 6f 66 20 74 68 65 20 in front of the
a3fc0 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 20 number */.
a3fd0 20 20 69 66 28 20 70 72 65 66 69 78 20 29 7b 0a if( prefix ){.
a3fe0 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 *(bufp
a3ff0 74 2b 2b 29 20 3d 20 70 72 65 66 69 78 3b 0a 20 t++) = prefix;.
a4000 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a4010 20 2f 2a 20 44 69 67 69 74 73 20 70 72 69 6f 72 /* Digits prior
a4020 20 74 6f 20 74 68 65 20 64 65 63 69 6d 61 6c 20 to the decimal
a4030 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 point */.
a4040 20 69 66 28 20 65 32 3c 30 20 29 7b 0a 20 20 20 if( e2<0 ){.
a4050 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b *(bufpt++
a4060 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 ) = '0';.
a4070 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a4080 20 20 66 6f 72 28 3b 20 65 32 3e 3d 30 3b 20 65 for(; e2>=0; e
a4090 32 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2--){.
a40a0 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 65 *(bufpt++) = e
a40b0 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 61 6c t_getdigit(&real
a40c0 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 20 20 value,&nsd);.
a40d0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a40e0 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 }. /* Th
a40f0 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 e decimal point
a4100 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 */. if( f
a4110 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 20 20 20 lag_dp ){.
a4120 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d *(bufpt++) =
a4130 20 27 2e 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a '.';. }.
a4140 20 20 20 20 20 20 20 20 2f 2a 20 22 30 22 20 64 /* "0" d
a4150 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65 20 igits after the
a4160 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 62 75 decimal point bu
a4170 74 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 72 t before the fir
a4180 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 69 st. ** si
a4190 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 20 gnificant digit
a41a0 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 2a 2f of the number */
a41b0 0a 20 20 20 20 20 20 20 20 66 6f 72 28 65 32 2b . for(e2+
a41c0 2b 3b 20 65 32 3c 30 3b 20 70 72 65 63 69 73 69 +; e2<0; precisi
a41d0 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b 0a 20 20 20 on--, e2++){.
a41e0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
a41f0 72 65 63 69 73 69 6f 6e 3e 30 20 29 3b 0a 20 20 recision>0 );.
a4200 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b *(bufpt+
a4210 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 +) = '0';.
a4220 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 }. /* S
a4230 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 ignificant digit
a4240 73 20 61 66 74 65 72 20 74 68 65 20 64 65 63 69 s after the deci
a4250 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 mal point */.
a4260 20 20 20 20 20 77 68 69 6c 65 28 20 28 70 72 65 while( (pre
a4270 63 69 73 69 6f 6e 2d 2d 29 3e 30 20 29 7b 0a 20 cision--)>0 ){.
a4280 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 *(bufpt
a4290 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64 69 67 69 ++) = et_getdigi
a42a0 74 28 26 72 65 61 6c 76 61 6c 75 65 2c 26 6e 73 t(&realvalue,&ns
a42b0 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 d);. }.
a42c0 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 /* Remove
a42d0 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 61 trailing zeros a
a42e0 6e 64 20 74 68 65 20 22 2e 22 20 69 66 20 6e 6f nd the "." if no
a42f0 20 64 69 67 69 74 73 20 66 6f 6c 6c 6f 77 20 74 digits follow t
a4300 68 65 20 22 2e 22 20 2a 2f 0a 20 20 20 20 20 20 he "." */.
a4310 20 20 69 66 28 20 66 6c 61 67 5f 72 74 7a 20 26 if( flag_rtz &
a4320 26 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 & flag_dp ){.
a4330 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 62 75 while( bu
a4340 66 70 74 5b 2d 31 5d 3d 3d 27 30 27 20 29 20 2a fpt[-1]=='0' ) *
a4350 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a 20 (--bufpt) = 0;.
a4360 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
a4370 20 62 75 66 70 74 3e 62 75 66 20 29 3b 0a 20 20 bufpt>buf );.
a4380 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70 if( bufp
a4390 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29 7b 0a 20 20 t[-1]=='.' ){.
a43a0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6c if( fl
a43b0 61 67 5f 61 6c 74 66 6f 72 6d 32 20 29 7b 0a 20 ag_altform2 ){.
a43c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62 *(b
a43d0 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 ufpt++) = '0';.
a43e0 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 }else
a43f0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
a4400 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a *(--bufpt) = 0;.
a4410 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
a4420 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a4430 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 }. /* A
a4440 64 64 20 74 68 65 20 22 65 4e 4e 4e 22 20 73 75 dd the "eNNN" su
a4450 66 66 69 78 20 2a 2f 0a 20 20 20 20 20 20 20 20 ffix */.
a4460 69 66 28 20 66 6c 61 67 5f 65 78 70 20 7c 7c 20 if( flag_exp ||
a4470 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b 0a xtype==etEXP ){.
a4480 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 *(bufp
a4490 74 2b 2b 29 20 3d 20 61 44 69 67 69 74 73 5b 69 t++) = aDigits[i
a44a0 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a nfop->charset];.
a44b0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78 if( ex
a44c0 70 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 p<0 ){.
a44d0 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 *(bufpt++) =
a44e0 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78 70 3b '-'; exp = -exp;
a44f0 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
a4500 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 {. *(
a4510 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27 3b 0a bufpt++) = '+';.
a4520 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
a4530 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 3d 31 if( exp>=1
a4540 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 00 ){.
a4550 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28 *(bufpt++) = (
a4560 63 68 61 72 29 28 28 65 78 70 2f 31 30 30 29 2b char)((exp/100)+
a4570 27 30 27 29 3b 20 20 20 20 20 20 20 20 2f 2a 20 '0'); /*
a4580 31 30 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 100's digit */.
a4590 20 20 20 20 20 20 20 20 20 20 20 65 78 70 20 25 exp %
a45a0 3d 20 31 30 30 3b 0a 20 20 20 20 20 20 20 20 20 = 100;.
a45b0 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 }. *(b
a45c0 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 72 29 ufpt++) = (char)
a45d0 28 65 78 70 2f 31 30 2b 27 30 27 29 3b 20 20 20 (exp/10+'0');
a45e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 30 27 /* 10'
a45f0 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 20 s digit */.
a4600 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 *(bufpt++)
a4610 3d 20 28 63 68 61 72 29 28 65 78 70 25 31 30 2b = (char)(exp%10+
a4620 27 30 27 29 3b 20 20 20 20 20 20 20 20 20 20 20 '0');
a4630 20 20 2f 2a 20 31 27 73 20 64 69 67 69 74 20 2a /* 1's digit *
a4640 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 /. }.
a4650 20 20 20 20 2a 62 75 66 70 74 20 3d 20 30 3b 0a *bufpt = 0;.
a4660 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
a4670 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d 62 65 72 converted number
a4680 20 69 73 20 69 6e 20 62 75 66 5b 5d 20 61 6e 64 is in buf[] and
a4690 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64 zero terminated
a46a0 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a 20 20 20 . Output it..
a46b0 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 ** Note tha
a46c0 74 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73 20 t the number is
a46d0 69 6e 20 74 68 65 20 75 73 75 61 6c 20 6f 72 64 in the usual ord
a46e0 65 72 2c 20 6e 6f 74 20 72 65 76 65 72 73 65 64 er, not reversed
a46f0 20 61 73 20 77 69 74 68 0a 20 20 20 20 20 20 20 as with.
a4700 20 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f 6e 76 ** integer conv
a4710 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20 20 ersions. */.
a4720 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e length = (in
a4730 74 29 28 62 75 66 70 74 2d 62 75 66 29 3b 0a 20 t)(bufpt-buf);.
a4740 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 bufpt = b
a4750 75 66 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 uf;.. /*
a4760 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 20 41 Special case: A
a4770 64 64 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 dd leading zeros
a4780 20 69 66 20 74 68 65 20 66 6c 61 67 5f 7a 65 72 if the flag_zer
a4790 6f 70 61 64 20 66 6c 61 67 20 69 73 0a 20 20 20 opad flag is.
a47a0 20 20 20 20 20 2a 2a 20 73 65 74 20 61 6e 64 20 ** set and
a47b0 77 65 20 61 72 65 20 6e 6f 74 20 6c 65 66 74 20 we are not left
a47c0 6a 75 73 74 69 66 69 65 64 20 2a 2f 0a 20 20 20 justified */.
a47d0 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a 65 if( flag_ze
a47e0 72 6f 70 61 64 20 26 26 20 21 66 6c 61 67 5f 6c ropad && !flag_l
a47f0 65 66 74 6a 75 73 74 69 66 79 20 26 26 20 6c 65 eftjustify && le
a4800 6e 67 74 68 20 3c 20 77 69 64 74 68 29 7b 0a 20 ngth < width){.
a4810 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a int i;.
a4820 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 50 int nP
a4830 61 64 20 3d 20 77 69 64 74 68 20 2d 20 6c 65 6e ad = width - len
a4840 67 74 68 3b 0a 20 20 20 20 20 20 20 20 20 20 66 gth;. f
a4850 6f 72 28 69 3d 77 69 64 74 68 3b 20 69 3e 3d 6e or(i=width; i>=n
a4860 50 61 64 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20 Pad; i--){.
a4870 20 20 20 20 20 20 20 62 75 66 70 74 5b 69 5d 20 bufpt[i]
a4880 3d 20 62 75 66 70 74 5b 69 2d 6e 50 61 64 5d 3b = bufpt[i-nPad];
a4890 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
a48a0 20 20 20 20 20 20 20 69 20 3d 20 70 72 65 66 69 i = prefi
a48b0 78 21 3d 30 3b 0a 20 20 20 20 20 20 20 20 20 20 x!=0;.
a48c0 77 68 69 6c 65 28 20 6e 50 61 64 2d 2d 20 29 20 while( nPad-- )
a48d0 62 75 66 70 74 5b 69 2b 2b 5d 20 3d 20 27 30 27 bufpt[i++] = '0'
a48e0 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 ;. leng
a48f0 74 68 20 3d 20 77 69 64 74 68 3b 0a 20 20 20 20 th = width;.
a4900 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 }.#endif.
a4910 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
a4920 20 20 63 61 73 65 20 65 74 53 49 5a 45 3a 0a 20 case etSIZE:.
a4930 20 20 20 20 20 20 20 2a 28 76 61 5f 61 72 67 28 *(va_arg(
a4940 61 70 2c 69 6e 74 2a 29 29 20 3d 20 70 41 63 63 ap,int*)) = pAcc
a4950 75 6d 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20 20 um->nChar;.
a4960 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 length = widt
a4970 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 h = 0;. b
a4980 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 reak;. case
a4990 20 65 74 50 45 52 43 45 4e 54 3a 0a 20 20 20 20 etPERCENT:.
a49a0 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 27 25 27 buf[0] = '%'
a49b0 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20 ;. bufpt
a49c0 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20 20 6c = buf;. l
a49d0 65 6e 67 74 68 20 3d 20 31 3b 0a 20 20 20 20 20 ength = 1;.
a49e0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a49f0 63 61 73 65 20 65 74 43 48 41 52 58 3a 0a 20 20 case etCHARX:.
a4a00 20 20 20 20 20 20 63 20 3d 20 76 61 5f 61 72 67 c = va_arg
a4a10 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 (ap,int);.
a4a20 20 20 62 75 66 5b 30 5d 20 3d 20 28 63 68 61 72 buf[0] = (char
a4a30 29 63 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 )c;. if(
a4a40 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 29 7b 0a precision>=0 ){.
a4a50 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 for(id
a4a60 78 3d 31 3b 20 69 64 78 3c 70 72 65 63 69 73 69 x=1; idx<precisi
a4a70 6f 6e 3b 20 69 64 78 2b 2b 29 20 62 75 66 5b 69 on; idx++) buf[i
a4a80 64 78 5d 20 3d 20 28 63 68 61 72 29 63 3b 0a 20 dx] = (char)c;.
a4a90 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 length
a4aa0 3d 20 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20 = precision;.
a4ab0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a4ac0 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 31 3b length =1;
a4ad0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a4ae0 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a bufpt = buf;.
a4af0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
a4b00 20 20 20 20 20 63 61 73 65 20 65 74 53 54 52 49 case etSTRI
a4b10 4e 47 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 NG:. case e
a4b20 74 44 59 4e 53 54 52 49 4e 47 3a 0a 20 20 20 20 tDYNSTRING:.
a4b30 20 20 20 20 62 75 66 70 74 20 3d 20 76 61 5f 61 bufpt = va_a
a4b40 72 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a 20 20 rg(ap,char*);.
a4b50 20 20 20 20 20 20 69 66 28 20 62 75 66 70 74 3d if( bufpt=
a4b60 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
a4b70 62 75 66 70 74 20 3d 20 22 22 3b 0a 20 20 20 20 bufpt = "";.
a4b80 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 78 74 }else if( xt
a4b90 79 70 65 3d 3d 65 74 44 59 4e 53 54 52 49 4e 47 ype==etDYNSTRING
a4ba0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 45 ){. zE
a4bb0 78 74 72 61 20 3d 20 62 75 66 70 74 3b 0a 20 20 xtra = bufpt;.
a4bc0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a4bd0 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 if( precision>=0
a4be0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f ){. fo
a4bf0 72 28 6c 65 6e 67 74 68 3d 30 3b 20 6c 65 6e 67 r(length=0; leng
a4c00 74 68 3c 70 72 65 63 69 73 69 6f 6e 20 26 26 20 th<precision &&
a4c10 62 75 66 70 74 5b 6c 65 6e 67 74 68 5d 3b 20 6c bufpt[length]; l
a4c20 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20 20 20 20 20 ength++){}.
a4c30 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a4c40 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 73 71 6c length = sql
a4c50 69 74 65 33 53 74 72 6c 65 6e 33 30 28 62 75 66 ite3Strlen30(buf
a4c60 70 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 pt);. }.
a4c70 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
a4c80 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45 53 case etSQLES
a4c90 43 41 50 45 3a 0a 20 20 20 20 20 20 63 61 73 65 CAPE:. case
a4ca0 20 65 74 53 51 4c 45 53 43 41 50 45 32 3a 0a 20 etSQLESCAPE2:.
a4cb0 20 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45 case etSQLE
a4cc0 53 43 41 50 45 33 3a 20 7b 0a 20 20 20 20 20 20 SCAPE3: {.
a4cd0 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 2c 20 69 int i, j, n, i
a4ce0 73 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 snull;. i
a4cf0 6e 74 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20 nt needQuote;.
a4d00 20 20 20 20 20 20 63 68 61 72 20 63 68 3b 0a 20 char ch;.
a4d10 20 20 20 20 20 20 20 63 68 61 72 20 71 20 3d 20 char q =
a4d20 28 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53 ((xtype==etSQLES
a4d30 43 41 50 45 33 29 3f 27 22 27 3a 27 5c 27 27 29 CAPE3)?'"':'\'')
a4d40 3b 20 20 20 2f 2a 20 51 75 6f 74 65 20 63 68 61 ; /* Quote cha
a4d50 72 61 63 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 racter */.
a4d60 20 20 63 68 61 72 20 2a 65 73 63 61 72 67 20 3d char *escarg =
a4d70 20 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a va_arg(ap,char*
a4d80 29 3b 0a 20 20 20 20 20 20 20 20 69 73 6e 75 6c );. isnul
a4d90 6c 20 3d 20 65 73 63 61 72 67 3d 3d 30 3b 0a 20 l = escarg==0;.
a4da0 20 20 20 20 20 20 20 69 66 28 20 69 73 6e 75 6c if( isnul
a4db0 6c 20 29 20 65 73 63 61 72 67 20 3d 20 28 78 74 l ) escarg = (xt
a4dc0 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 ype==etSQLESCAPE
a4dd0 32 20 3f 20 22 4e 55 4c 4c 22 20 3a 20 22 28 4e 2 ? "NULL" : "(N
a4de0 55 4c 4c 29 22 29 3b 0a 20 20 20 20 20 20 20 20 ULL)");.
a4df0 66 6f 72 28 69 3d 6e 3d 30 3b 20 28 63 68 3d 65 for(i=n=0; (ch=e
a4e00 73 63 61 72 67 5b 69 5d 29 21 3d 30 3b 20 69 2b scarg[i])!=0; i+
a4e10 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 +){. if
a4e20 28 20 63 68 3d 3d 71 20 29 20 20 6e 2b 2b 3b 0a ( ch==q ) n++;.
a4e30 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a4e40 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 21 69 needQuote = !i
a4e50 73 6e 75 6c 6c 20 26 26 20 78 74 79 70 65 3d 3d snull && xtype==
a4e60 65 74 53 51 4c 45 53 43 41 50 45 32 3b 0a 20 20 etSQLESCAPE2;.
a4e70 20 20 20 20 20 20 6e 20 2b 3d 20 69 20 2b 20 31 n += i + 1
a4e80 20 2b 20 6e 65 65 64 51 75 6f 74 65 2a 32 3b 0a + needQuote*2;.
a4e90 20 20 20 20 20 20 20 20 69 66 28 20 6e 3e 65 74 if( n>et
a4ea0 42 55 46 53 49 5a 45 20 29 7b 0a 20 20 20 20 20 BUFSIZE ){.
a4eb0 20 20 20 20 20 62 75 66 70 74 20 3d 20 7a 45 78 bufpt = zEx
a4ec0 74 72 61 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c tra = sqlite3Mal
a4ed0 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 20 20 20 20 loc( n );.
a4ee0 20 20 20 20 69 66 28 20 62 75 66 70 74 3d 3d 30 if( bufpt==0
a4ef0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a4f00 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 pAccum->mallocFa
a4f10 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 iled = 1;.
a4f20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
a4f30 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a4f40 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
a4f50 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a bufpt = buf;.
a4f60 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a4f70 20 20 6a 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 j = 0;.
a4f80 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 if( needQuote )
a4f90 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b bufpt[j++] = q;
a4fa0 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 . for(i=0
a4fb0 3b 20 28 63 68 3d 65 73 63 61 72 67 5b 69 5d 29 ; (ch=escarg[i])
a4fc0 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 !=0; i++){.
a4fd0 20 20 20 20 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 bufpt[j++]
a4fe0 3d 20 63 68 3b 0a 20 20 20 20 20 20 20 20 20 20 = ch;.
a4ff0 69 66 28 20 63 68 3d 3d 71 20 29 20 62 75 66 70 if( ch==q ) bufp
a5000 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 t[j++] = ch;.
a5010 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
a5020 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 62 f( needQuote ) b
a5030 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 ufpt[j++] = q;.
a5040 20 20 20 20 20 20 20 62 75 66 70 74 5b 6a 5d 20 bufpt[j]
a5050 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e = 0;. len
a5060 67 74 68 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 gth = j;.
a5070 20 2f 2a 20 54 68 65 20 70 72 65 63 69 73 69 6f /* The precisio
a5080 6e 20 69 73 20 69 67 6e 6f 72 65 64 20 6f 6e 20 n is ignored on
a5090 25 71 20 61 6e 64 20 25 51 20 2a 2f 0a 20 20 20 %q and %Q */.
a50a0 20 20 20 20 20 2f 2a 20 69 66 28 20 70 72 65 63 /* if( prec
a50b0 69 73 69 6f 6e 3e 3d 30 20 26 26 20 70 72 65 63 ision>=0 && prec
a50c0 69 73 69 6f 6e 3c 6c 65 6e 67 74 68 20 29 20 6c ision<length ) l
a50d0 65 6e 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f ength = precisio
a50e0 6e 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 72 n; */. br
a50f0 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
a5100 20 20 20 63 61 73 65 20 65 74 54 4f 4b 45 4e 3a case etTOKEN:
a5110 20 7b 0a 20 20 20 20 20 20 20 20 54 6f 6b 65 6e {. Token
a5120 20 2a 70 54 6f 6b 65 6e 20 3d 20 76 61 5f 61 72 *pToken = va_ar
a5130 67 28 61 70 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 20 g(ap, Token*);.
a5140 20 20 20 20 20 20 20 69 66 28 20 70 54 6f 6b 65 if( pToke
a5150 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 n ){. s
a5160 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 qlite3StrAccumAp
a5170 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 28 63 6f pend(pAccum, (co
a5180 6e 73 74 20 63 68 61 72 2a 29 70 54 6f 6b 65 6e nst char*)pToken
a5190 2d 3e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b ->z, pToken->n);
a51a0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a51b0 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 length = widt
a51c0 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 h = 0;. b
a51d0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
a51e0 20 20 20 20 63 61 73 65 20 65 74 53 52 43 4c 49 case etSRCLI
a51f0 53 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 72 ST: {. Sr
a5200 63 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 76 61 cList *pSrc = va
a5210 5f 61 72 67 28 61 70 2c 20 53 72 63 4c 69 73 74 _arg(ap, SrcList
a5220 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 *);. int
a5230 6b 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 k = va_arg(ap, i
a5240 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73 74 72 nt);. str
a5250 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
a5260 20 2a 70 49 74 65 6d 20 3d 20 26 70 53 72 63 2d *pItem = &pSrc-
a5270 3e 61 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 61 >a[k];. a
a5280 73 73 65 72 74 28 20 6b 3e 3d 30 20 26 26 20 6b ssert( k>=0 && k
a5290 3c 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20 <pSrc->nSrc );.
a52a0 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d if( pItem
a52b0 2d 3e 7a 44 61 74 61 62 61 73 65 20 29 7b 0a 20 ->zDatabase ){.
a52c0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
a52d0 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 StrAccumAppend(p
a52e0 41 63 63 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 44 Accum, pItem->zD
a52f0 61 74 61 62 61 73 65 2c 20 2d 31 29 3b 0a 20 20 atabase, -1);.
a5300 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 sqlite3S
a5310 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 trAccumAppend(pA
a5320 63 63 75 6d 2c 20 22 2e 22 2c 20 31 29 3b 0a 20 ccum, ".", 1);.
a5330 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a5340 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
a5350 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 70 Append(pAccum, p
a5360 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 2d 31 29 Item->zName, -1)
a5370 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 ;. length
a5380 20 3d 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 = width = 0;.
a5390 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
a53a0 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 }. defau
a53b0 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 61 73 lt: {. as
a53c0 73 65 72 74 28 20 78 74 79 70 65 3d 3d 65 74 49 sert( xtype==etI
a53d0 4e 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 NVALID );.
a53e0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 return;.
a53f0 7d 0a 20 20 20 20 7d 2f 2a 20 45 6e 64 20 73 77 }. }/* End sw
a5400 69 74 63 68 20 6f 76 65 72 20 74 68 65 20 66 6f itch over the fo
a5410 72 6d 61 74 20 74 79 70 65 20 2a 2f 0a 20 20 20 rmat type */.
a5420 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 74 /*. ** The t
a5430 65 78 74 20 6f 66 20 74 68 65 20 63 6f 6e 76 65 ext of the conve
a5440 72 73 69 6f 6e 20 69 73 20 70 6f 69 6e 74 65 64 rsion is pointed
a5450 20 74 6f 20 62 79 20 22 62 75 66 70 74 22 20 61 to by "bufpt" a
a5460 6e 64 20 69 73 0a 20 20 20 20 2a 2a 20 22 6c 65 nd is. ** "le
a5470 6e 67 74 68 22 20 63 68 61 72 61 63 74 65 72 73 ngth" characters
a5480 20 6c 6f 6e 67 2e 20 20 54 68 65 20 66 69 65 6c long. The fiel
a5490 64 20 77 69 64 74 68 20 69 73 20 22 77 69 64 74 d width is "widt
a54a0 68 22 2e 20 20 44 6f 0a 20 20 20 20 2a 2a 20 74 h". Do. ** t
a54b0 68 65 20 6f 75 74 70 75 74 2e 0a 20 20 20 20 2a he output.. *
a54c0 2f 0a 20 20 20 20 69 66 28 20 21 66 6c 61 67 5f /. if( !flag_
a54d0 6c 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 leftjustify ){.
a54e0 20 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e register in
a54f0 74 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20 t nspace;.
a5500 6e 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c nspace = width-l
a5510 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28 ength;. if(
a5520 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 nspace>0 ){.
a5530 20 20 20 20 20 61 70 70 65 6e 64 53 70 61 63 65 appendSpace
a5540 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 (pAccum, nspace)
a5550 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
a5560 20 20 20 20 69 66 28 20 6c 65 6e 67 74 68 3e 30 if( length>0
a5570 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
a5580 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 3StrAccumAppend(
a5590 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20 6c pAccum, bufpt, l
a55a0 65 6e 67 74 68 29 3b 0a 20 20 20 20 7d 0a 20 20 ength);. }.
a55b0 20 20 69 66 28 20 66 6c 61 67 5f 6c 65 66 74 6a if( flag_leftj
a55c0 75 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20 20 ustify ){.
a55d0 72 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73 70 register int nsp
a55e0 61 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61 63 ace;. nspac
a55f0 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74 68 e = width-length
a5600 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70 61 ;. if( nspa
a5610 63 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 ce>0 ){.
a5620 61 70 70 65 6e 64 53 70 61 63 65 28 70 41 63 63 appendSpace(pAcc
a5630 75 6d 2c 20 6e 73 70 61 63 65 29 3b 0a 20 20 20 um, nspace);.
a5640 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
a5650 66 28 20 7a 45 78 74 72 61 20 29 7b 0a 20 20 20 f( zExtra ){.
a5660 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
a5670 7a 45 78 74 72 61 29 3b 0a 20 20 20 20 7d 0a 20 zExtra);. }.
a5680 20 7d 2f 2a 20 45 6e 64 20 66 6f 72 20 6c 6f 6f }/* End for loo
a5690 70 20 6f 76 65 72 20 74 68 65 20 66 6f 72 6d 61 p over the forma
a56a0 74 20 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 2f 2a t string */.} /*
a56b0 20 45 6e 64 20 6f 66 20 66 75 6e 63 74 69 6f 6e End of function
a56c0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e */../*.** Appen
a56d0 64 20 4e 20 62 79 74 65 73 20 6f 66 20 74 65 78 d N bytes of tex
a56e0 74 20 66 72 6f 6d 20 7a 20 74 6f 20 74 68 65 20 t from z to the
a56f0 53 74 72 41 63 63 75 6d 20 6f 62 6a 65 63 74 2e StrAccum object.
a5700 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
a5710 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
a5720 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 53 74 trAccumAppend(St
a5730 72 41 63 63 75 6d 20 2a 70 2c 20 63 6f 6e 73 74 rAccum *p, const
a5740 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 4e 29 char *z, int N)
a5750 7b 0a 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30 {. assert( z!=0
a5760 20 7c 7c 20 4e 3d 3d 30 20 29 3b 0a 20 20 69 66 || N==0 );. if
a5770 28 20 70 2d 3e 74 6f 6f 42 69 67 20 7c 20 70 2d ( p->tooBig | p-
a5780 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
a5790 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 70 2d . testcase(p-
a57a0 3e 74 6f 6f 42 69 67 29 3b 0a 20 20 20 20 74 65 >tooBig);. te
a57b0 73 74 63 61 73 65 28 70 2d 3e 6d 61 6c 6c 6f 63 stcase(p->malloc
a57c0 46 61 69 6c 65 64 29 3b 0a 20 20 20 20 72 65 74 Failed);. ret
a57d0 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e urn;. }. if( N
a57e0 3c 30 20 29 7b 0a 20 20 20 20 4e 20 3d 20 73 71 <0 ){. N = sq
a57f0 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 lite3Strlen30(z)
a5800 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3d 3d 30 ;. }. if( N==0
a5810 20 7c 7c 20 4e 45 56 45 52 28 7a 3d 3d 30 29 20 || NEVER(z==0)
a5820 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 ){. return;.
a5830 20 7d 0a 20 20 69 66 28 20 70 2d 3e 6e 43 68 61 }. if( p->nCha
a5840 72 2b 4e 20 3e 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 r+N >= p->nAlloc
a5850 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e ){. char *zN
a5860 65 77 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e ew;. if( !p->
a5870 75 73 65 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 useMalloc ){.
a5880 20 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 31 p->tooBig = 1
a5890 3b 0a 20 20 20 20 20 20 4e 20 3d 20 70 2d 3e 6e ;. N = p->n
a58a0 41 6c 6c 6f 63 20 2d 20 70 2d 3e 6e 43 68 61 72 Alloc - p->nChar
a58b0 20 2d 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20 - 1;. if(
a58c0 4e 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 N<=0 ){.
a58d0 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a return;. }.
a58e0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
a58f0 20 69 36 34 20 73 7a 4e 65 77 20 3d 20 70 2d 3e i64 szNew = p->
a5900 6e 43 68 61 72 3b 0a 20 20 20 20 20 20 73 7a 4e nChar;. szN
a5910 65 77 20 2b 3d 20 4e 20 2b 20 31 3b 0a 20 20 20 ew += N + 1;.
a5920 20 20 20 69 66 28 20 73 7a 4e 65 77 20 3e 20 70 if( szNew > p
a5930 2d 3e 6d 78 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 ->mxAlloc ){.
a5940 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 sqlite3StrA
a5950 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 20 ccumReset(p);.
a5960 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42 69 67 20 p->tooBig
a5970 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 65 74 = 1;. ret
a5980 75 72 6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 urn;. }else
a5990 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 41 6c {. p->nAl
a59a0 6c 6f 63 20 3d 20 28 69 6e 74 29 73 7a 4e 65 77 loc = (int)szNew
a59b0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
a59c0 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 zNew = sqlite3Db
a59d0 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c MallocRaw(p->db,
a59e0 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 p->nAlloc );.
a59f0 20 20 20 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a if( zNew ){.
a5a00 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a memcpy(z
a5a10 4e 65 77 2c 20 70 2d 3e 7a 54 65 78 74 2c 20 70 New, p->zText, p
a5a20 2d 3e 6e 43 68 61 72 29 3b 0a 20 20 20 20 20 20 ->nChar);.
a5a30 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 sqlite3StrAccu
a5a40 6d 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20 20 mReset(p);.
a5a50 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 7a 4e p->zText = zN
a5a60 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ew;. }else{
a5a70 0a 20 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c 6c . p->mall
a5a80 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
a5a90 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 sqlite3Str
a5aa0 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 AccumReset(p);.
a5ab0 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 return;.
a5ac0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
a5ad0 0a 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a 54 . memcpy(&p->zT
a5ae0 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 2c 20 7a ext[p->nChar], z
a5af0 2c 20 4e 29 3b 0a 20 20 70 2d 3e 6e 43 68 61 72 , N);. p->nChar
a5b00 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 += N;.}../*.**
a5b10 46 69 6e 69 73 68 20 6f 66 66 20 61 20 73 74 72 Finish off a str
a5b20 69 6e 67 20 62 79 20 6d 61 6b 69 6e 67 20 73 75 ing by making su
a5b30 72 65 20 69 74 20 69 73 20 7a 65 72 6f 2d 74 65 re it is zero-te
a5b40 72 6d 69 6e 61 74 65 64 2e 0a 2a 2a 20 52 65 74 rminated..** Ret
a5b50 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
a5b60 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 73 the resulting s
a5b70 74 72 69 6e 67 2e 20 20 52 65 74 75 72 6e 20 61 tring. Return a
a5b80 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 NULL.** pointer
a5b90 20 69 66 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 if any kind of
a5ba0 65 72 72 6f 72 20 77 61 73 20 65 6e 63 6f 75 6e error was encoun
a5bb0 74 65 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 tered..*/.SQLITE
a5bc0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
a5bd0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 qlite3StrAccumFi
a5be0 6e 69 73 68 28 53 74 72 41 63 63 75 6d 20 2a 70 nish(StrAccum *p
a5bf0 29 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 ){. if( p->zTex
a5c00 74 20 29 7b 0a 20 20 20 20 70 2d 3e 7a 54 65 78 t ){. p->zTex
a5c10 74 5b 70 2d 3e 6e 43 68 61 72 5d 20 3d 20 30 3b t[p->nChar] = 0;
a5c20 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 73 65 4d . if( p->useM
a5c30 61 6c 6c 6f 63 20 26 26 20 70 2d 3e 7a 54 65 78 alloc && p->zTex
a5c40 74 3d 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a 20 t==p->zBase ){.
a5c50 20 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 p->zText =
a5c60 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
a5c70 61 77 28 70 2d 3e 64 62 2c 20 70 2d 3e 6e 43 68 aw(p->db, p->nCh
a5c80 61 72 2b 31 20 29 3b 0a 20 20 20 20 20 20 69 66 ar+1 );. if
a5c90 28 20 70 2d 3e 7a 54 65 78 74 20 29 7b 0a 20 20 ( p->zText ){.
a5ca0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2d 3e memcpy(p->
a5cb0 7a 54 65 78 74 2c 20 70 2d 3e 7a 42 61 73 65 2c zText, p->zBase,
a5cc0 20 70 2d 3e 6e 43 68 61 72 2b 31 29 3b 0a 20 20 p->nChar+1);.
a5cd0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
a5ce0 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c p->mallocFail
a5cf0 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a ed = 1;. }.
a5d00 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
a5d10 72 6e 20 70 2d 3e 7a 54 65 78 74 3b 0a 7d 0a 0a rn p->zText;.}..
a5d20 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 61 6e 20 53 /*.** Reset an S
a5d30 74 72 41 63 63 75 6d 20 73 74 72 69 6e 67 2e 20 trAccum string.
a5d40 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 61 6c Reclaim all mal
a5d50 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2f loced memory..*/
a5d60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a5d70 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 void sqlite3StrA
a5d80 63 63 75 6d 52 65 73 65 74 28 53 74 72 41 63 63 ccumReset(StrAcc
a5d90 75 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d um *p){. if( p-
a5da0 3e 7a 54 65 78 74 21 3d 70 2d 3e 7a 42 61 73 65 >zText!=p->zBase
a5db0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 ){. sqlite3D
a5dc0 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e bFree(p->db, p->
a5dd0 7a 54 65 78 74 29 3b 0a 20 20 7d 0a 20 20 70 2d zText);. }. p-
a5de0 3e 7a 54 65 78 74 20 3d 20 30 3b 0a 7d 0a 0a 2f >zText = 0;.}../
a5df0 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
a5e00 61 20 73 74 72 69 6e 67 20 61 63 63 75 6d 75 6c a string accumul
a5e10 61 74 6f 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ator.*/.SQLITE_P
a5e20 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
a5e30 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 te3StrAccumInit(
a5e40 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 68 61 StrAccum *p, cha
a5e50 72 20 2a 7a 42 61 73 65 2c 20 69 6e 74 20 6e 2c r *zBase, int n,
a5e60 20 69 6e 74 20 6d 78 29 7b 0a 20 20 70 2d 3e 7a int mx){. p->z
a5e70 54 65 78 74 20 3d 20 70 2d 3e 7a 42 61 73 65 20 Text = p->zBase
a5e80 3d 20 7a 42 61 73 65 3b 0a 20 20 70 2d 3e 64 62 = zBase;. p->db
a5e90 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 43 68 61 72 = 0;. p->nChar
a5ea0 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 41 6c 6c 6f = 0;. p->nAllo
a5eb0 63 20 3d 20 6e 3b 0a 20 20 70 2d 3e 6d 78 41 6c c = n;. p->mxAl
a5ec0 6c 6f 63 20 3d 20 6d 78 3b 0a 20 20 70 2d 3e 75 loc = mx;. p->u
a5ed0 73 65 4d 61 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20 seMalloc = 1;.
a5ee0 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 30 3b 0a 20 p->tooBig = 0;.
a5ef0 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 p->mallocFailed
a5f00 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 = 0;.}../*.** P
a5f10 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 rint into memory
a5f20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
a5f30 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 qliteMalloc().
a5f40 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c Use the internal
a5f50 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e .** %-conversion
a5f60 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a extensions..*/.
a5f70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
a5f80 68 61 72 20 2a 73 71 6c 69 74 65 33 56 4d 50 72 har *sqlite3VMPr
a5f90 69 6e 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62 intf(sqlite3 *db
a5fa0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 , const char *zF
a5fb0 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20 61 ormat, va_list a
a5fc0 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 p){. char *z;.
a5fd0 20 63 68 61 72 20 7a 42 61 73 65 5b 53 51 4c 49 char zBase[SQLI
a5fe0 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a TE_PRINT_BUF_SIZ
a5ff0 45 5d 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 E];. StrAccum a
a6000 63 63 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 cc;. assert( db
a6010 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 !=0 );. sqlite3
a6020 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 StrAccumInit(&ac
a6030 63 2c 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f 66 c, zBase, sizeof
a6040 28 7a 42 61 73 65 29 2c 0a 20 20 20 20 20 20 20 (zBase),.
a6050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 d
a6060 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
a6070 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 29 3b _LIMIT_LENGTH]);
a6080 0a 20 20 61 63 63 2e 64 62 20 3d 20 64 62 3b 0a . acc.db = db;.
a6090 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 sqlite3VXPrint
a60a0 66 28 26 61 63 63 2c 20 31 2c 20 7a 46 6f 72 6d f(&acc, 1, zForm
a60b0 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20 73 at, ap);. z = s
a60c0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 qlite3StrAccumFi
a60d0 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 69 66 nish(&acc);. if
a60e0 28 20 61 63 63 2e 6d 61 6c 6c 6f 63 46 61 69 6c ( acc.mallocFail
a60f0 65 64 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 ed ){. db->ma
a6100 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a llocFailed = 1;.
a6110 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a }. return z;.
a6120 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 }../*.** Print i
a6130 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 nto memory obtai
a6140 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d ned from sqliteM
a6150 61 6c 6c 6f 63 28 29 2e 20 20 55 73 65 20 74 68 alloc(). Use th
a6160 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d e internal.** %-
a6170 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e conversion exten
a6180 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 sions..*/.SQLITE
a6190 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
a61a0 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 73 71 qlite3MPrintf(sq
a61b0 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 lite3 *db, const
a61c0 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 char *zFormat,
a61d0 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 ...){. va_list
a61e0 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 ap;. char *z;.
a61f0 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 va_start(ap, zF
a6200 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 ormat);. z = sq
a6210 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 lite3VMPrintf(db
a6220 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
a6230 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 va_end(ap);.
a6240 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a return z;.}../*.
a6250 2a 2a 20 4c 69 6b 65 20 73 71 6c 69 74 65 33 4d ** Like sqlite3M
a6260 50 72 69 6e 74 66 28 29 2c 20 62 75 74 20 63 61 Printf(), but ca
a6270 6c 6c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 ll sqlite3DbFree
a6280 28 29 20 6f 6e 20 7a 53 74 72 20 61 66 74 65 72 () on zStr after
a6290 20 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 74 formatting.** t
a62a0 68 65 20 73 74 72 69 6e 67 20 61 6e 64 20 62 65 he string and be
a62b0 66 6f 72 65 20 72 65 74 75 72 6e 6e 69 6e 67 2e fore returnning.
a62c0 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 This routine i
a62d0 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 s intended to be
a62e0 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6d 6f 64 69 used.** to modi
a62f0 66 79 20 61 6e 20 65 78 69 73 74 69 6e 67 20 73 fy an existing s
a6300 74 72 69 6e 67 2e 20 20 46 6f 72 20 65 78 61 6d tring. For exam
a6310 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 ple:.**.**
a6320 20 78 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 x = sqlite3MPri
a6330 6e 74 66 28 64 62 2c 20 78 2c 20 22 70 72 65 66 ntf(db, x, "pref
a6340 69 78 20 25 73 20 73 75 66 66 69 78 22 2c 20 78 ix %s suffix", x
a6350 29 3b 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f );.**.*/.SQLITE_
a6360 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 PRIVATE char *sq
a6370 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 73 71 lite3MAppendf(sq
a6380 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20 lite3 *db, char
a6390 2a 7a 53 74 72 2c 20 63 6f 6e 73 74 20 63 68 61 *zStr, const cha
a63a0 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 r *zFormat, ...)
a63b0 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a {. va_list ap;.
a63c0 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f char *z;. va_
a63d0 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 start(ap, zForma
a63e0 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 t);. z = sqlite
a63f0 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 3VMPrintf(db, zF
a6400 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 ormat, ap);. va
a6410 5f 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 _end(ap);. sqli
a6420 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 te3DbFree(db, zS
a6430 74 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b tr);. return z;
a6440 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 .}../*.** Print
a6450 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 into memory obta
a6460 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
a6470 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 4f 6d 69 3_malloc(). Omi
a6480 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a t the internal.*
a6490 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 * %-conversion e
a64a0 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 xtensions..*/.SQ
a64b0 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 LITE_API char *s
a64c0 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 qlite3_vmprintf(
a64d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 const char *zFor
a64e0 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20 61 70 29 mat, va_list ap)
a64f0 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63 {. char *z;. c
a6500 68 61 72 20 7a 42 61 73 65 5b 53 51 4c 49 54 45 har zBase[SQLITE
a6510 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 5d _PRINT_BUF_SIZE]
a6520 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 63 ;. StrAccum acc
a6530 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
a6540 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 _OMIT_AUTOINIT.
a6550 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 if( sqlite3_ini
a6560 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 tialize() ) retu
a6570 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 73 rn 0;.#endif. s
a6580 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e qlite3StrAccumIn
a6590 69 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 it(&acc, zBase,
a65a0 73 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 20 53 sizeof(zBase), S
a65b0 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 QLITE_MAX_LENGTH
a65c0 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 );. sqlite3VXPr
a65d0 69 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 intf(&acc, 0, zF
a65e0 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20 ormat, ap);. z
a65f0 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 = sqlite3StrAccu
a6600 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 mFinish(&acc);.
a6610 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a return z;.}../*
a6620 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d .** Print into m
a6630 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
a6640 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c rom sqlite3_mall
a6650 6f 63 28 29 28 29 2e 20 20 4f 6d 69 74 20 74 68 oc()(). Omit th
a6660 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d e internal.** %-
a6670 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e conversion exten
a6680 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 sions..*/.SQLITE
a6690 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 _API char *sqlit
a66a0 65 33 5f 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 e3_mprintf(const
a66b0 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 char *zFormat,
a66c0 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 ...){. va_list
a66d0 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 23 ap;. char *z;.#
a66e0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
a66f0 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 IT_AUTOINIT. if
a6700 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 ( sqlite3_initia
a6710 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 lize() ) return
a6720 30 3b 0a 23 65 6e 64 69 66 0a 20 20 76 61 5f 73 0;.#endif. va_s
a6730 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 tart(ap, zFormat
a6740 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 );. z = sqlite3
a6750 5f 76 6d 70 72 69 6e 74 66 28 7a 46 6f 72 6d 61 _vmprintf(zForma
a6760 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 t, ap);. va_end
a6770 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a (ap);. return z
a6780 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73 71 6c 69 74 ;.}../*.** sqlit
a6790 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 77 6f e3_snprintf() wo
a67a0 72 6b 73 20 6c 69 6b 65 20 73 6e 70 72 69 6e 74 rks like snprint
a67b0 66 28 29 20 65 78 63 65 70 74 20 74 68 61 74 20 f() except that
a67c0 69 74 20 69 67 6e 6f 72 65 73 20 74 68 65 0a 2a it ignores the.*
a67d0 2a 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 6c 65 * current locale
a67e0 20 73 65 74 74 69 6e 67 73 2e 20 20 54 68 69 73 settings. This
a67f0 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 66 6f is important fo
a6800 72 20 53 51 4c 69 74 65 20 62 65 63 61 75 73 65 r SQLite because
a6810 20 77 65 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 61 we.** are not a
a6820 62 6c 65 20 74 6f 20 75 73 65 20 61 20 22 2c 22 ble to use a ","
a6830 20 61 73 20 74 68 65 20 64 65 63 69 6d 61 6c 20 as the decimal
a6840 70 6f 69 6e 74 20 69 6e 20 70 6c 61 63 65 20 6f point in place o
a6850 66 20 22 2e 22 20 61 73 0a 2a 2a 20 73 70 65 63 f "." as.** spec
a6860 69 66 69 65 64 20 62 79 20 73 6f 6d 65 20 6c 6f ified by some lo
a6870 63 61 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 cales..*/.SQLITE
a6880 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 _API char *sqlit
a6890 65 33 5f 73 6e 70 72 69 6e 74 66 28 69 6e 74 20 e3_snprintf(int
a68a0 6e 2c 20 63 68 61 72 20 2a 7a 42 75 66 2c 20 63 n, char *zBuf, c
a68b0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
a68c0 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 63 68 61 72 at, ...){. char
a68d0 20 2a 7a 3b 0a 20 20 76 61 5f 6c 69 73 74 20 61 *z;. va_list a
a68e0 70 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 p;. StrAccum ac
a68f0 63 3b 0a 0a 20 20 69 66 28 20 6e 3c 3d 30 20 29 c;.. if( n<=0 )
a6900 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 42 75 {. return zBu
a6910 66 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 f;. }. sqlite3
a6920 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 StrAccumInit(&ac
a6930 63 2c 20 7a 42 75 66 2c 20 6e 2c 20 30 29 3b 0a c, zBuf, n, 0);.
a6940 20 20 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 acc.useMalloc
a6950 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 = 0;. va_start(
a6960 61 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73 ap,zFormat);. s
a6970 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 qlite3VXPrintf(&
a6980 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c acc, 0, zFormat,
a6990 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 ap);. va_end(a
a69a0 70 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 p);. z = sqlite
a69b0 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 3StrAccumFinish(
a69c0 26 61 63 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 &acc);. return
a69d0 7a 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 z;.}..#if define
a69e0 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a d(SQLITE_DEBUG).
a69f0 2f 2a 0a 2a 2a 20 41 20 76 65 72 73 69 6f 6e 20 /*.** A version
a6a00 6f 66 20 70 72 69 6e 74 66 28 29 20 74 68 61 74 of printf() that
a6a10 20 75 6e 64 65 72 73 74 61 6e 64 73 20 25 6c 6c understands %ll
a6a20 64 2e 20 20 55 73 65 64 20 66 6f 72 20 64 65 62 d. Used for deb
a6a30 75 67 67 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 70 ugging..** The p
a6a40 72 69 6e 74 66 28 29 20 62 75 69 6c 74 20 69 6e rintf() built in
a6a50 74 6f 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 to some versions
a6a60 20 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 65 73 of windows does
a6a70 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20 not understand
a6a80 25 6c 6c 64 0a 2a 2a 20 61 6e 64 20 73 65 67 66 %lld.** and segf
a6a90 61 75 6c 74 73 20 69 66 20 79 6f 75 20 67 69 76 aults if you giv
a6aa0 65 20 69 74 20 61 20 6c 6f 6e 67 20 6c 6f 6e 67 e it a long long
a6ab0 20 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f int..*/.SQLITE_
a6ac0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
a6ad0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
a6ae0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 const char *zFor
a6af0 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f mat, ...){. va_
a6b00 6c 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63 list ap;. StrAc
a6b10 63 75 6d 20 61 63 63 3b 0a 20 20 63 68 61 72 20 cum acc;. char
a6b20 7a 42 75 66 5b 35 30 30 5d 3b 0a 20 20 73 71 6c zBuf[500];. sql
a6b30 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 ite3StrAccumInit
a6b40 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 73 69 7a (&acc, zBuf, siz
a6b50 65 6f 66 28 7a 42 75 66 29 2c 20 30 29 3b 0a 20 eof(zBuf), 0);.
a6b60 20 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d acc.useMalloc =
a6b70 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 0;. va_start(a
a6b80 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71 p,zFormat);. sq
a6b90 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 61 lite3VXPrintf(&a
a6ba0 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 cc, 0, zFormat,
a6bb0 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 ap);. va_end(ap
a6bc0 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 );. sqlite3StrA
a6bd0 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 ccumFinish(&acc)
a6be0 3b 0a 20 20 66 70 72 69 6e 74 66 28 73 74 64 6f ;. fprintf(stdo
a6bf0 75 74 2c 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a ut,"%s", zBuf);.
a6c00 20 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 fflush(stdout)
a6c10 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a ;.}.#endif../***
a6c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
a6c30 6f 66 20 70 72 69 6e 74 66 2e 63 20 2a 2a 2a 2a of printf.c ****
a6c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
a6c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
a6c80 6e 20 66 69 6c 65 20 72 61 6e 64 6f 6d 2e 63 20 n file random.c
a6c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
a6cc0 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 * 2001 September
a6cd0 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 15.**.** The au
a6ce0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
a6cf0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
a6d00 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
a6d10 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
a6d20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
a6d30 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
a6d40 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
a6d50 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
a6d60 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
a6d70 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
a6d80 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
a6d90 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
a6da0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
a6db0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
a6dc0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
a6dd0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
a6de0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
a6df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6e30 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
a6e40 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f contains code to
a6e50 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 70 73 65 implement a pse
a6e60 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 udo-random numbe
a6e70 72 0a 2a 2a 20 67 65 6e 65 72 61 74 6f 72 20 28 r.** generator (
a6e80 50 52 4e 47 29 20 66 6f 72 20 53 51 4c 69 74 65 PRNG) for SQLite
a6e90 2e 0a 2a 2a 0a 2a 2a 20 52 61 6e 64 6f 6d 20 6e ..**.** Random n
a6ea0 75 6d 62 65 72 73 20 61 72 65 20 75 73 65 64 20 umbers are used
a6eb0 62 79 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 64 by some of the d
a6ec0 61 74 61 62 61 73 65 20 62 61 63 6b 65 6e 64 73 atabase backends
a6ed0 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 in order.** to
a6ee0 67 65 6e 65 72 61 74 65 20 72 61 6e 64 6f 6d 20 generate random
a6ef0 69 6e 74 65 67 65 72 20 6b 65 79 73 20 66 6f 72 integer keys for
a6f00 20 74 61 62 6c 65 73 20 6f 72 20 72 61 6e 64 6f tables or rando
a6f10 6d 20 66 69 6c 65 6e 61 6d 65 73 2e 0a 2a 2a 0a m filenames..**.
a6f20 2a 2a 20 24 49 64 3a 20 72 61 6e 64 6f 6d 2e 63 ** $Id: random.c
a6f30 2c 76 20 31 2e 32 39 20 32 30 30 38 2f 31 32 2f ,v 1.29 2008/12/
a6f40 31 30 20 31 39 3a 32 36 3a 32 34 20 64 72 68 20 10 19:26:24 drh
a6f50 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6c Exp $.*/.../* Al
a6f60 6c 20 74 68 72 65 61 64 73 20 73 68 61 72 65 20 l threads share
a6f70 61 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 20 a single random
a6f80 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 number generator
a6f90 2e 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 ..** This struct
a6fa0 75 72 65 20 69 73 20 74 68 65 20 63 75 72 72 65 ure is the curre
a6fb0 6e 74 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 nt state of the
a6fc0 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 generator..*/.st
a6fd0 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 atic SQLITE_WSD
a6fe0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 struct sqlite3Pr
a6ff0 6e 67 54 79 70 65 20 7b 0a 20 20 75 6e 73 69 67 ngType {. unsig
a7000 6e 65 64 20 63 68 61 72 20 69 73 49 6e 69 74 3b ned char isInit;
a7010 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
a7020 65 20 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 64 e if initialized
a7030 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
a7040 68 61 72 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 har i, j;
a7050 20 20 20 20 20 2f 2a 20 53 74 61 74 65 20 76 61 /* State va
a7060 72 69 61 62 6c 65 73 20 2a 2f 0a 20 20 75 6e 73 riables */. uns
a7070 69 67 6e 65 64 20 63 68 61 72 20 73 5b 32 35 36 igned char s[256
a7080 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 ]; /* S
a7090 74 61 74 65 20 76 61 72 69 61 62 6c 65 73 20 2a tate variables *
a70a0 2f 0a 7d 20 73 71 6c 69 74 65 33 50 72 6e 67 3b /.} sqlite3Prng;
a70b0 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 73 69 ../*.** Get a si
a70c0 6e 67 6c 65 20 38 2d 62 69 74 20 72 61 6e 64 6f ngle 8-bit rando
a70d0 6d 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 m value from the
a70e0 20 52 43 34 20 50 52 4e 47 2e 20 20 54 68 65 20 RC4 PRNG. The
a70f0 4d 75 74 65 78 0a 2a 2a 20 6d 75 73 74 20 62 65 Mutex.** must be
a7100 20 68 65 6c 64 20 77 68 69 6c 65 20 65 78 65 63 held while exec
a7110 75 74 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 uting this routi
a7120 6e 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 79 20 6e 6f ne..**.** Why no
a7130 74 20 6a 75 73 74 20 75 73 65 20 61 20 6c 69 62 t just use a lib
a7140 72 61 72 79 20 72 61 6e 64 6f 6d 20 67 65 6e 65 rary random gene
a7150 72 61 74 6f 72 20 6c 69 6b 65 20 6c 72 61 6e 64 rator like lrand
a7160 34 38 28 29 20 66 6f 72 20 74 68 69 73 3f 0a 2a 48() for this?.*
a7170 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 4f 50 * Because the OP
a7180 5f 4e 65 77 52 6f 77 69 64 20 6f 70 63 6f 64 65 _NewRowid opcode
a7190 20 69 6e 20 74 68 65 20 56 44 42 45 20 64 65 70 in the VDBE dep
a71a0 65 6e 64 73 20 6f 6e 20 68 61 76 69 6e 67 20 61 ends on having a
a71b0 20 76 65 72 79 0a 2a 2a 20 67 6f 6f 64 20 73 6f very.** good so
a71c0 75 72 63 65 20 6f 66 20 72 61 6e 64 6f 6d 20 6e urce of random n
a71d0 75 6d 62 65 72 73 2e 20 20 54 68 65 20 6c 72 61 umbers. The lra
a71e0 6e 64 34 38 28 29 20 6c 69 62 72 61 72 79 20 66 nd48() library f
a71f0 75 6e 63 74 69 6f 6e 20 6d 61 79 0a 2a 2a 20 77 unction may.** w
a7200 65 6c 6c 20 62 65 20 67 6f 6f 64 20 65 6e 6f 75 ell be good enou
a7210 67 68 2e 20 20 42 75 74 20 6d 61 79 62 65 20 6e gh. But maybe n
a7220 6f 74 2e 20 20 4f 72 20 6d 61 79 62 65 20 6c 72 ot. Or maybe lr
a7230 61 6e 64 34 38 28 29 20 68 61 73 20 73 6f 6d 65 and48() has some
a7240 0a 2a 2a 20 73 75 62 74 6c 65 20 70 72 6f 62 6c .** subtle probl
a7250 65 6d 73 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74 ems on some syst
a7260 65 6d 73 20 74 68 61 74 20 63 6f 75 6c 64 20 63 ems that could c
a7270 61 75 73 65 20 70 72 6f 62 6c 65 6d 73 2e 20 20 ause problems.
a7280 49 74 20 69 73 20 68 61 72 64 0a 2a 2a 20 74 6f It is hard.** to
a7290 20 6b 6e 6f 77 2e 20 20 54 6f 20 6d 69 6e 69 6d know. To minim
a72a0 69 7a 65 20 74 68 65 20 72 69 73 6b 20 6f 66 20 ize the risk of
a72b0 70 72 6f 62 6c 65 6d 73 20 64 75 65 20 74 6f 20 problems due to
a72c0 62 61 64 20 6c 72 61 6e 64 34 38 28 29 0a 2a 2a bad lrand48().**
a72d0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
a72e0 2c 20 53 51 4c 69 74 65 20 75 73 65 73 20 74 68 , SQLite uses th
a72f0 69 73 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 is random number
a7300 20 67 65 6e 65 72 61 74 6f 72 20 62 61 73 65 64 generator based
a7310 0a 2a 2a 20 6f 6e 20 52 43 34 2c 20 77 68 69 63 .** on RC4, whic
a7320 68 20 77 65 20 6b 6e 6f 77 20 77 6f 72 6b 73 20 h we know works
a7330 76 65 72 79 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a very well..**.**
a7340 20 28 4c 61 74 65 72 29 3a 20 20 41 63 74 75 61 (Later): Actua
a7350 6c 6c 79 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 lly, OP_NewRowid
a7360 20 64 6f 65 73 20 6e 6f 74 20 64 65 70 65 6e 64 does not depend
a7370 20 6f 6e 20 61 20 67 6f 6f 64 20 73 6f 75 72 63 on a good sourc
a7380 65 20 6f 66 0a 2a 2a 20 72 61 6e 64 6f 6d 6e 65 e of.** randomne
a7390 73 73 20 61 6e 79 20 6d 6f 72 65 2e 20 20 42 75 ss any more. Bu
a73a0 74 20 77 65 20 77 69 6c 6c 20 6c 65 61 76 65 20 t we will leave
a73b0 74 68 69 73 20 63 6f 64 65 20 69 6e 20 61 6c 6c this code in all
a73c0 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2f 0a 73 74 the same..*/.st
a73d0 61 74 69 63 20 75 38 20 72 61 6e 64 6f 6d 42 79 atic u8 randomBy
a73e0 74 65 28 76 6f 69 64 29 7b 0a 20 20 75 6e 73 69 te(void){. unsi
a73f0 67 6e 65 64 20 63 68 61 72 20 74 3b 0a 0a 0a 20 gned char t;...
a7400 20 2f 2a 20 54 68 65 20 22 77 73 64 50 72 6e 67 /* The "wsdPrng
a7410 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65 73 " macro will res
a7420 6f 6c 76 65 20 74 6f 20 74 68 65 20 70 73 65 75 olve to the pseu
a7430 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 do-random number
a7440 20 67 65 6e 65 72 61 74 6f 72 0a 20 20 2a 2a 20 generator. **
a7450 73 74 61 74 65 20 76 65 63 74 6f 72 2e 20 20 49 state vector. I
a7460 66 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 f writable stati
a7470 63 20 64 61 74 61 20 69 73 20 75 6e 73 75 70 70 c data is unsupp
a7480 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72 orted on the tar
a7490 67 65 74 2c 0a 20 20 2a 2a 20 77 65 20 68 61 76 get,. ** we hav
a74a0 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 e to locate the
a74b0 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 74 20 state vector at
a74c0 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 run-time. In th
a74d0 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 20 20 e more common.
a74e0 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 77 72 ** case where wr
a74f0 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 itable static da
a7500 74 61 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c ta is supported,
a7510 20 77 73 64 50 72 6e 67 20 63 61 6e 20 72 65 66 wsdPrng can ref
a7520 65 72 20 64 69 72 65 63 74 6c 79 0a 20 20 2a 2a er directly. **
a7530 20 74 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33 to the "sqlite3
a7540 50 72 6e 67 22 20 73 74 61 74 65 20 76 65 63 74 Prng" state vect
a7550 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f 76 or declared abov
a7560 65 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 e.. */.#ifdef S
a7570 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 20 QLITE_OMIT_WSD.
a7580 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 struct sqlite3P
a7590 72 6e 67 54 79 70 65 20 2a 70 20 3d 20 26 47 4c rngType *p = &GL
a75a0 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 OBAL(struct sqli
a75b0 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c te3PrngType, sql
a75c0 69 74 65 33 50 72 6e 67 29 3b 0a 23 20 64 65 66 ite3Prng);.# def
a75d0 69 6e 65 20 77 73 64 50 72 6e 67 20 70 5b 30 5d ine wsdPrng p[0]
a75e0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
a75f0 77 73 64 50 72 6e 67 20 73 71 6c 69 74 65 33 50 wsdPrng sqlite3P
a7600 72 6e 67 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 2f rng.#endif... /
a7610 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 * Initialize the
a7620 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 72 61 state of the ra
a7630 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 ndom number gene
a7640 72 61 74 6f 72 20 6f 6e 63 65 2c 0a 20 20 2a 2a rator once,. **
a7650 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 the first time
a7660 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
a7670 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 73 65 65 called. The see
a7680 64 20 76 61 6c 75 65 20 64 6f 65 73 0a 20 20 2a d value does. *
a7690 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 63 6f * not need to co
a76a0 6e 74 61 69 6e 20 61 20 6c 6f 74 20 6f 66 20 72 ntain a lot of r
a76b0 61 6e 64 6f 6d 6e 65 73 73 20 73 69 6e 63 65 20 andomness since
a76c0 77 65 20 61 72 65 20 6e 6f 74 0a 20 20 2a 2a 20 we are not. **
a76d0 74 72 79 69 6e 67 20 74 6f 20 64 6f 20 73 65 63 trying to do sec
a76e0 75 72 65 20 65 6e 63 72 79 70 74 69 6f 6e 20 6f ure encryption o
a76f0 72 20 61 6e 79 74 68 69 6e 67 20 6c 69 6b 65 20 r anything like
a7700 74 68 61 74 2e 2e 2e 0a 20 20 2a 2a 0a 20 20 2a that.... **. *
a7710 2a 20 4e 6f 74 68 69 6e 67 20 69 6e 20 74 68 69 * Nothing in thi
a7720 73 20 66 69 6c 65 20 6f 72 20 61 6e 79 77 68 65 s file or anywhe
a7730 72 65 20 65 6c 73 65 20 69 6e 20 53 51 4c 69 74 re else in SQLit
a7740 65 20 64 6f 65 73 20 61 6e 79 20 6b 69 6e 64 20 e does any kind
a7750 6f 66 0a 20 20 2a 2a 20 65 6e 63 72 79 70 74 69 of. ** encrypti
a7760 6f 6e 2e 20 20 54 68 65 20 52 43 34 20 61 6c 67 on. The RC4 alg
a7770 6f 72 69 74 68 6d 20 69 73 20 62 65 69 6e 67 20 orithm is being
a7780 75 73 65 64 20 61 73 20 61 20 50 52 4e 47 20 28 used as a PRNG (
a7790 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 0a 20 20 pseudo-random.
a77a0 2a 2a 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 ** number genera
a77b0 74 6f 72 29 20 6e 6f 74 20 61 73 20 61 6e 20 65 tor) not as an e
a77c0 6e 63 72 79 70 74 69 6f 6e 20 64 65 76 69 63 65 ncryption device
a77d0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 77 73 .. */. if( !ws
a77e0 64 50 72 6e 67 2e 69 73 49 6e 69 74 20 29 7b 0a dPrng.isInit ){.
a77f0 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 int i;. c
a7800 68 61 72 20 6b 5b 32 35 36 5d 3b 0a 20 20 20 20 har k[256];.
a7810 77 73 64 50 72 6e 67 2e 6a 20 3d 20 30 3b 0a 20 wsdPrng.j = 0;.
a7820 20 20 20 77 73 64 50 72 6e 67 2e 69 20 3d 20 30 wsdPrng.i = 0
a7830 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 52 ;. sqlite3OsR
a7840 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 andomness(sqlite
a7850 33 5f 76 66 73 5f 66 69 6e 64 28 30 29 2c 20 32 3_vfs_find(0), 2
a7860 35 36 2c 20 6b 29 3b 0a 20 20 20 20 66 6f 72 28 56, k);. for(
a7870 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b 29 i=0; i<256; i++)
a7880 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e {. wsdPrng.
a7890 73 5b 69 5d 20 3d 20 28 75 38 29 69 3b 0a 20 20 s[i] = (u8)i;.
a78a0 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b }. for(i=0;
a78b0 20 69 3c 32 35 36 3b 20 69 2b 2b 29 7b 0a 20 20 i<256; i++){.
a78c0 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20 2b 3d wsdPrng.j +=
a78d0 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 2b 20 wsdPrng.s[i] +
a78e0 6b 5b 69 5d 3b 0a 20 20 20 20 20 20 74 20 3d 20 k[i];. t =
a78f0 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e wsdPrng.s[wsdPrn
a7900 67 2e 6a 5d 3b 0a 20 20 20 20 20 20 77 73 64 50 g.j];. wsdP
a7910 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d rng.s[wsdPrng.j]
a7920 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 3b = wsdPrng.s[i];
a7930 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e 73 . wsdPrng.s
a7940 5b 69 5d 20 3d 20 74 3b 0a 20 20 20 20 7d 0a 20 [i] = t;. }.
a7950 20 20 20 77 73 64 50 72 6e 67 2e 69 73 49 6e 69 wsdPrng.isIni
a7960 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a t = 1;. }.. /*
a7970 20 47 65 6e 65 72 61 74 65 20 61 6e 64 20 72 65 Generate and re
a7980 74 75 72 6e 20 73 69 6e 67 6c 65 20 72 61 6e 64 turn single rand
a7990 6f 6d 20 62 79 74 65 0a 20 20 2a 2f 0a 20 20 77 om byte. */. w
a79a0 73 64 50 72 6e 67 2e 69 2b 2b 3b 0a 20 20 74 20 sdPrng.i++;. t
a79b0 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 = wsdPrng.s[wsdP
a79c0 72 6e 67 2e 69 5d 3b 0a 20 20 77 73 64 50 72 6e rng.i];. wsdPrn
a79d0 67 2e 6a 20 2b 3d 20 74 3b 0a 20 20 77 73 64 50 g.j += t;. wsdP
a79e0 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 69 5d rng.s[wsdPrng.i]
a79f0 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 = wsdPrng.s[wsd
a7a00 50 72 6e 67 2e 6a 5d 3b 0a 20 20 77 73 64 50 72 Prng.j];. wsdPr
a7a10 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 20 ng.s[wsdPrng.j]
a7a20 3d 20 74 3b 0a 20 20 74 20 2b 3d 20 77 73 64 50 = t;. t += wsdP
a7a30 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 69 5d rng.s[wsdPrng.i]
a7a40 3b 0a 20 20 72 65 74 75 72 6e 20 77 73 64 50 72 ;. return wsdPr
a7a50 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a ng.s[t];.}../*.*
a7a60 2a 20 52 65 74 75 72 6e 20 4e 20 72 61 6e 64 6f * Return N rando
a7a70 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a 53 51 4c 49 m bytes..*/.SQLI
a7a80 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
a7a90 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 69 te3_randomness(i
a7aa0 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 70 42 75 66 nt N, void *pBuf
a7ab0 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 ){. unsigned ch
a7ac0 61 72 20 2a 7a 42 75 66 20 3d 20 70 42 75 66 3b ar *zBuf = pBuf;
a7ad0 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
a7ae0 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 ADSAFE. sqlite3
a7af0 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 _mutex *mutex =
a7b00 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
a7b10 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
a7b20 54 41 54 49 43 5f 50 52 4e 47 29 3b 0a 23 65 6e TATIC_PRNG);.#en
a7b30 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 dif. sqlite3_mu
a7b40 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 tex_enter(mutex)
a7b50 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 ;. while( N-- )
a7b60 7b 0a 20 20 20 20 2a 28 7a 42 75 66 2b 2b 29 20 {. *(zBuf++)
a7b70 3d 20 72 61 6e 64 6f 6d 42 79 74 65 28 29 3b 0a = randomByte();.
a7b80 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 }. sqlite3_mu
a7b90 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 tex_leave(mutex)
a7ba0 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ;.}..#ifndef SQL
a7bb0 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e ITE_OMIT_BUILTIN
a7bc0 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 _TEST./*.** For
a7bd0 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 testing purposes
a7be0 2c 20 77 65 20 73 6f 6d 65 74 69 6d 65 73 20 77 , we sometimes w
a7bf0 61 6e 74 20 74 6f 20 70 72 65 73 65 72 76 65 20 ant to preserve
a7c00 74 68 65 20 73 74 61 74 65 20 6f 66 0a 2a 2a 20 the state of.**
a7c10 50 52 4e 47 20 61 6e 64 20 72 65 73 74 6f 72 65 PRNG and restore
a7c20 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69 74 73 the PRNG to its
a7c30 20 73 61 76 65 64 20 73 74 61 74 65 20 61 74 20 saved state at
a7c40 61 20 6c 61 74 65 72 20 74 69 6d 65 2c 20 6f 72 a later time, or
a7c50 0a 2a 2a 20 74 6f 20 72 65 73 65 74 20 74 68 65 .** to reset the
a7c60 20 50 52 4e 47 20 74 6f 20 69 74 73 20 69 6e 69 PRNG to its ini
a7c70 74 69 61 6c 20 73 74 61 74 65 2e 20 20 54 68 65 tial state. The
a7c80 73 65 20 72 6f 75 74 69 6e 65 73 20 61 63 63 6f se routines acco
a7c90 6d 70 6c 69 73 68 0a 2a 2a 20 74 68 6f 73 65 20 mplish.** those
a7ca0 74 61 73 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tasks..**.** The
a7cb0 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f sqlite3_test_co
a7cc0 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 61 63 ntrol() interfac
a7cd0 65 20 63 61 6c 6c 73 20 74 68 65 73 65 20 72 6f e calls these ro
a7ce0 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 63 6f 6e utines to.** con
a7cf0 74 72 6f 6c 20 74 68 65 20 50 52 4e 47 2e 0a 2a trol the PRNG..*
a7d00 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f /.static SQLITE_
a7d10 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69 74 WSD struct sqlit
a7d20 65 33 50 72 6e 67 54 79 70 65 20 73 71 6c 69 74 e3PrngType sqlit
a7d30 65 33 53 61 76 65 64 50 72 6e 67 3b 0a 53 51 4c e3SavedPrng;.SQL
a7d40 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
a7d50 20 73 71 6c 69 74 65 33 50 72 6e 67 53 61 76 65 sqlite3PrngSave
a7d60 53 74 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 6d State(void){. m
a7d70 65 6d 63 70 79 28 0a 20 20 20 20 26 47 4c 4f 42 emcpy(. &GLOB
a7d80 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 AL(struct sqlite
a7d90 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 3PrngType, sqlit
a7da0 65 33 53 61 76 65 64 50 72 6e 67 29 2c 0a 20 20 e3SavedPrng),.
a7db0 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 &GLOBAL(struct
a7dc0 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 sqlite3PrngType
a7dd0 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 2c 0a , sqlite3Prng),.
a7de0 20 20 20 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 sizeof(sqlit
a7df0 65 33 50 72 6e 67 29 0a 20 20 29 3b 0a 7d 0a 53 e3Prng). );.}.S
a7e00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
a7e10 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 id sqlite3PrngRe
a7e20 73 74 6f 72 65 53 74 61 74 65 28 76 6f 69 64 29 storeState(void)
a7e30 7b 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20 20 20 {. memcpy(.
a7e40 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 &GLOBAL(struct s
a7e50 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 qlite3PrngType,
a7e60 73 71 6c 69 74 65 33 50 72 6e 67 29 2c 0a 20 20 sqlite3Prng),.
a7e70 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 &GLOBAL(struct
a7e80 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 sqlite3PrngType
a7e90 2c 20 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 , sqlite3SavedPr
a7ea0 6e 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66 28 ng),. sizeof(
a7eb0 73 71 6c 69 74 65 33 50 72 6e 67 29 0a 20 20 29 sqlite3Prng). )
a7ec0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
a7ed0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
a7ee0 72 6e 67 52 65 73 65 74 53 74 61 74 65 28 76 6f rngResetState(vo
a7ef0 69 64 29 7b 0a 20 20 47 4c 4f 42 41 4c 28 73 74 id){. GLOBAL(st
a7f00 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 ruct sqlite3Prng
a7f10 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e Type, sqlite3Prn
a7f20 67 29 2e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 7d g).isInit = 0;.}
a7f30 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
a7f40 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 E_OMIT_BUILTIN_T
a7f50 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a EST */../*******
a7f60 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 ******* End of r
a7f70 61 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a andom.c ********
a7f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7fa0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
a7fb0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
a7fc0 6c 65 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a le utf.c *******
a7fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7ff0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
a8000 30 34 20 41 70 72 69 6c 20 31 33 0a 2a 2a 0a 2a 04 April 13.**.*
a8010 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
a8020 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
a8030 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
a8040 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
a8050 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
a8060 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
a8070 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
a8080 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
a8090 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
a80a0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
a80b0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
a80c0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
a80d0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
a80e0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
a80f0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
a8100 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
a8110 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
a8120 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
a8130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
a8170 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
a8180 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74 routines used t
a8190 6f 20 74 72 61 6e 73 6c 61 74 65 20 62 65 74 77 o translate betw
a81a0 65 65 6e 20 55 54 46 2d 38 2c 20 0a 2a 2a 20 55 een UTF-8, .** U
a81b0 54 46 2d 31 36 2c 20 55 54 46 2d 31 36 42 45 2c TF-16, UTF-16BE,
a81c0 20 61 6e 64 20 55 54 46 2d 31 36 4c 45 2e 0a 2a and UTF-16LE..*
a81d0 2a 0a 2a 2a 20 24 49 64 3a 20 75 74 66 2e 63 2c *.** $Id: utf.c,
a81e0 76 20 31 2e 37 33 20 32 30 30 39 2f 30 34 2f 30 v 1.73 2009/04/0
a81f0 31 20 31 38 3a 34 30 3a 33 32 20 64 72 68 20 45 1 18:40:32 drh E
a8200 78 70 20 24 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 xp $.**.** Notes
a8210 20 6f 6e 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a on UTF-8:.**.**
a8220 20 20 20 42 79 74 65 2d 30 20 20 20 20 42 79 74 Byte-0 Byt
a8230 65 2d 31 20 20 20 20 42 79 74 65 2d 32 20 20 20 e-1 Byte-2
a8240 20 42 79 74 65 2d 33 20 20 20 20 56 61 6c 75 65 Byte-3 Value
a8250 0a 2a 2a 20 20 30 78 78 78 78 78 78 78 20 20 20 .** 0xxxxxxx
a8260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a8270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 00
a8280 30 30 30 30 30 30 20 30 30 30 30 30 30 30 30 20 000000 00000000
a8290 30 78 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 30 0xxxxxxx.** 110
a82a0 79 79 79 79 79 20 20 31 30 78 78 78 78 78 78 20 yyyyy 10xxxxxx
a82b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a82c0 20 20 20 20 20 20 30 30 30 30 30 30 30 30 20 30 00000000 0
a82d0 30 30 30 30 79 79 79 20 79 79 78 78 78 78 78 78 0000yyy yyxxxxxx
a82e0 0a 2a 2a 20 20 31 31 31 30 7a 7a 7a 7a 20 20 31 .** 1110zzzz 1
a82f0 30 79 79 79 79 79 79 20 20 31 30 78 78 78 78 78 0yyyyyy 10xxxxx
a8300 78 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 x 00
a8310 30 30 30 30 30 30 20 7a 7a 7a 7a 79 79 79 79 20 000000 zzzzyyyy
a8320 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 yyxxxxxx.** 111
a8330 31 30 75 75 75 20 20 31 30 75 75 7a 7a 7a 7a 20 10uuu 10uuzzzz
a8340 20 31 30 79 79 79 79 79 79 20 20 31 30 78 78 78 10yyyyyy 10xxx
a8350 78 78 78 20 20 20 30 30 30 75 75 75 75 75 20 7a xxx 000uuuuu z
a8360 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 zzzyyyy yyxxxxxx
a8370 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 .**.**.** Notes
a8380 6f 6e 20 55 54 46 2d 31 36 3a 20 20 28 77 69 74 on UTF-16: (wit
a8390 68 20 77 77 77 77 2b 31 3d 3d 75 75 75 75 75 29 h wwww+1==uuuuu)
a83a0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 57 6f 72 64 .**.** Word
a83b0 2d 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 -0
a83c0 20 57 6f 72 64 2d 31 20 20 20 20 20 20 20 20 20 Word-1
a83d0 20 56 61 6c 75 65 0a 2a 2a 20 20 31 31 30 31 31 Value.** 11011
a83e0 30 77 77 20 77 77 7a 7a 7a 7a 79 79 20 20 20 31 0ww wwzzzzyy 1
a83f0 31 30 31 31 31 79 79 20 79 79 78 78 78 78 78 78 10111yy yyxxxxxx
a8400 20 20 20 20 30 30 30 75 75 75 75 75 20 7a 7a 7a 000uuuuu zzz
a8410 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a zyyyy yyxxxxxx.*
a8420 2a 20 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 * zzzzyyyy yyxx
a8430 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20 20 xxxx
a8440 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 0000
a8450 30 30 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79 0000 zzzzyyyy yy
a8460 78 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 xxxxxx.**.**.**
a8470 42 4f 4d 20 6f 72 20 42 79 74 65 20 4f 72 64 65 BOM or Byte Orde
a8480 72 20 4d 61 72 6b 3a 0a 2a 2a 20 20 20 20 20 30 r Mark:.** 0
a8490 78 66 66 20 30 78 66 65 20 20 20 6c 69 74 74 6c xff 0xfe littl
a84a0 65 2d 65 6e 64 69 61 6e 20 75 74 66 2d 31 36 20 e-endian utf-16
a84b0 66 6f 6c 6c 6f 77 73 0a 2a 2a 20 20 20 20 20 30 follows.** 0
a84c0 78 66 65 20 30 78 66 66 20 20 20 62 69 67 2d 65 xfe 0xff big-e
a84d0 6e 64 69 61 6e 20 75 74 66 2d 31 36 20 66 6f 6c ndian utf-16 fol
a84e0 6c 6f 77 73 0a 2a 2a 0a 2a 2f 0a 2f 2a 2a 2a 2a lows.**.*/./****
a84f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
a8500 64 65 20 76 64 62 65 49 6e 74 2e 68 20 69 6e 20 de vdbeInt.h in
a8510 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 75 74 the middle of ut
a8520 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f.c ************
a8530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
a8540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
a8550 20 66 69 6c 65 20 76 64 62 65 49 6e 74 2e 68 20 file vdbeInt.h
a8560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
a8590 20 32 30 30 33 20 53 65 70 74 65 6d 62 65 72 20 2003 September
a85a0 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6.**.** The auth
a85b0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
a85c0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
a85d0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
a85e0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
a85f0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
a8600 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
a8610 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
a8620 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
a8630 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
a8640 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
a8650 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
a8660 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
a8670 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
a8680 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
a8690 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
a86a0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
a86b0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
a86c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a86d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a86e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a86f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8700 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 .** This is the
a8710 68 65 61 64 65 72 20 66 69 6c 65 20 66 6f 72 20 header file for
a8720 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 information that
a8730 20 69 73 20 70 72 69 76 61 74 65 20 74 6f 20 74 is private to t
a8740 68 65 0a 2a 2a 20 56 44 42 45 2e 20 20 54 68 69 he.** VDBE. Thi
a8750 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 s information us
a8760 65 64 20 74 6f 20 61 6c 6c 20 62 65 20 61 74 20 ed to all be at
a8770 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 the top of the s
a8780 69 6e 67 6c 65 0a 2a 2a 20 73 6f 75 72 63 65 20 ingle.** source
a8790 63 6f 64 65 20 66 69 6c 65 20 22 76 64 62 65 2e code file "vdbe.
a87a0 63 22 2e 20 20 57 68 65 6e 20 74 68 61 74 20 66 c". When that f
a87b0 69 6c 65 20 62 65 63 61 6d 65 20 74 6f 6f 20 62 ile became too b
a87c0 69 67 20 28 6f 76 65 72 0a 2a 2a 20 36 30 30 30 ig (over.** 6000
a87d0 20 6c 69 6e 65 73 20 6c 6f 6e 67 29 20 69 74 20 lines long) it
a87e0 77 61 73 20 73 70 6c 69 74 20 75 70 20 69 6e 74 was split up int
a87f0 6f 20 73 65 76 65 72 61 6c 20 73 6d 61 6c 6c 65 o several smalle
a8800 72 20 66 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 74 r files and.** t
a8810 68 69 73 20 68 65 61 64 65 72 20 69 6e 66 6f 72 his header infor
a8820 6d 61 74 69 6f 6e 20 77 61 73 20 66 61 63 74 6f mation was facto
a8830 72 65 64 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 red out..**.** $
a8840 49 64 3a 20 76 64 62 65 49 6e 74 2e 68 2c 76 20 Id: vdbeInt.h,v
a8850 31 2e 31 37 34 20 32 30 30 39 2f 30 36 2f 32 33 1.174 2009/06/23
a8860 20 31 34 3a 31 35 3a 30 34 20 64 72 68 20 45 78 14:15:04 drh Ex
a8870 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f p $.*/.#ifndef _
a8880 56 44 42 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69 VDBEINT_H_.#defi
a8890 6e 65 20 5f 56 44 42 45 49 4e 54 5f 48 5f 0a 0a ne _VDBEINT_H_..
a88a0 2f 2a 0a 2a 2a 20 53 51 4c 20 69 73 20 74 72 61 /*.** SQL is tra
a88b0 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 73 nslated into a s
a88c0 65 71 75 65 6e 63 65 20 6f 66 20 69 6e 73 74 72 equence of instr
a88d0 75 63 74 69 6f 6e 73 20 74 6f 20 62 65 0a 2a 2a uctions to be.**
a88e0 20 65 78 65 63 75 74 65 64 20 62 79 20 61 20 76 executed by a v
a88f0 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 irtual machine.
a8900 20 45 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f Each instructio
a8910 6e 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 n is an instance
a8920 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f .** of the follo
a8930 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a wing structure..
a8940 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
a8950 74 20 56 64 62 65 4f 70 20 4f 70 3b 0a 0a 2f 2a t VdbeOp Op;../*
a8960 0a 2a 2a 20 42 6f 6f 6c 65 61 6e 20 76 61 6c 75 .** Boolean valu
a8970 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e es.*/.typedef un
a8980 73 69 67 6e 65 64 20 63 68 61 72 20 42 6f 6f 6c signed char Bool
a8990 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 73 6f ;../*.** A curso
a89a0 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 69 r is a pointer i
a89b0 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 42 54 72 nto a single BTr
a89c0 65 65 20 77 69 74 68 69 6e 20 61 20 64 61 74 61 ee within a data
a89d0 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 base file..** Th
a89e0 65 20 63 75 72 73 6f 72 20 63 61 6e 20 73 65 65 e cursor can see
a89f0 6b 20 74 6f 20 61 20 42 54 72 65 65 20 65 6e 74 k to a BTree ent
a8a00 72 79 20 77 69 74 68 20 61 20 70 61 72 74 69 63 ry with a partic
a8a10 75 6c 61 72 20 6b 65 79 2c 20 6f 72 0a 2a 2a 20 ular key, or.**
a8a20 6c 6f 6f 70 20 6f 76 65 72 20 61 6c 6c 20 65 6e loop over all en
a8a30 74 72 69 65 73 20 6f 66 20 74 68 65 20 42 74 72 tries of the Btr
a8a40 65 65 2e 20 20 59 6f 75 20 63 61 6e 20 61 6c 73 ee. You can als
a8a50 6f 20 69 6e 73 65 72 74 20 6e 65 77 20 42 54 72 o insert new BTr
a8a60 65 65 0a 2a 2a 20 65 6e 74 72 69 65 73 20 6f 72 ee.** entries or
a8a70 20 72 65 74 72 69 65 76 65 20 74 68 65 20 6b 65 retrieve the ke
a8a80 79 20 6f 72 20 64 61 74 61 20 66 72 6f 6d 20 74 y or data from t
a8a90 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 he entry that th
a8aa0 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 63 e cursor.** is c
a8ab0 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e urrently pointin
a8ac0 67 20 74 6f 2e 0a 2a 2a 20 0a 2a 2a 20 45 76 65 g to..** .** Eve
a8ad0 72 79 20 63 75 72 73 6f 72 20 74 68 61 74 20 74 ry cursor that t
a8ae0 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 he virtual machi
a8af0 6e 65 20 68 61 73 20 6f 70 65 6e 20 69 73 20 72 ne has open is r
a8b00 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 6e epresented by an
a8b10 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 .** instance of
a8b20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
a8b30 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 ructure..**.** I
a8b40 66 20 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 f the VdbeCursor
a8b50 2e 69 73 54 72 69 67 67 65 72 52 6f 77 20 66 6c .isTriggerRow fl
a8b60 61 67 20 69 73 20 73 65 74 20 69 74 20 6d 65 61 ag is set it mea
a8b70 6e 73 20 74 68 61 74 20 74 68 69 73 20 63 75 72 ns that this cur
a8b80 73 6f 72 20 69 73 0a 2a 2a 20 72 65 61 6c 6c 79 sor is.** really
a8b90 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 74 68 a single row th
a8ba0 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 at represents th
a8bb0 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 e NEW or OLD pse
a8bc0 75 64 6f 2d 74 61 62 6c 65 20 6f 66 0a 2a 2a 20 udo-table of.**
a8bd0 61 20 72 6f 77 20 74 72 69 67 67 65 72 2e 20 20 a row trigger.
a8be0 54 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 The data for the
a8bf0 20 72 6f 77 20 69 73 20 73 74 6f 72 65 64 20 69 row is stored i
a8c00 6e 20 56 64 62 65 43 75 72 73 6f 72 2e 70 44 61 n VdbeCursor.pDa
a8c10 74 61 20 61 6e 64 0a 2a 2a 20 74 68 65 20 72 6f ta and.** the ro
a8c20 77 69 64 20 69 73 20 69 6e 20 56 64 62 65 43 75 wid is in VdbeCu
a8c30 72 73 6f 72 2e 69 4b 65 79 2e 0a 2a 2f 0a 73 74 rsor.iKey..*/.st
a8c40 72 75 63 74 20 56 64 62 65 43 75 72 73 6f 72 20 ruct VdbeCursor
a8c50 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 {. BtCursor *pC
a8c60 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20 54 68 65 ursor; /* The
a8c70 20 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 cursor structur
a8c80 65 20 6f 66 20 74 68 65 20 62 61 63 6b 65 6e 64 e of the backend
a8c90 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 */. int iDb;
a8ca0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
a8cb0 6e 64 65 78 20 6f 66 20 63 75 72 73 6f 72 20 64 ndex of cursor d
a8cc0 61 74 61 62 61 73 65 20 69 6e 20 64 62 2d 3e 61 atabase in db->a
a8cd0 44 62 5b 5d 20 28 6f 72 20 2d 31 29 20 2a 2f 0a Db[] (or -1) */.
a8ce0 20 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b i64 lastRowid;
a8cf0 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 /* Last
a8d00 72 6f 77 69 64 20 66 72 6f 6d 20 61 20 4e 65 78 rowid from a Nex
a8d10 74 20 6f 72 20 4e 65 78 74 49 64 78 20 6f 70 65 t or NextIdx ope
a8d20 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 42 6f 6f 6c ration */. Bool
a8d30 20 7a 65 72 6f 65 64 3b 20 20 20 20 20 20 20 20 zeroed;
a8d40 20 20 2f 2a 20 54 72 75 65 20 69 66 20 7a 65 72 /* True if zer
a8d50 6f 65 64 20 6f 75 74 20 61 6e 64 20 72 65 61 64 oed out and read
a8d60 79 20 66 6f 72 20 72 65 75 73 65 20 2a 2f 0a 20 y for reuse */.
a8d70 20 42 6f 6f 6c 20 72 6f 77 69 64 49 73 56 61 6c Bool rowidIsVal
a8d80 69 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 id; /* True i
a8d90 66 20 6c 61 73 74 52 6f 77 69 64 20 69 73 20 76 f lastRowid is v
a8da0 61 6c 69 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 61 alid */. Bool a
a8db0 74 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 tFirst;
a8dc0 2f 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e 74 /* True if point
a8dd0 69 6e 67 20 74 6f 20 66 69 72 73 74 20 65 6e 74 ing to first ent
a8de0 72 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 75 73 65 ry */. Bool use
a8df0 52 61 6e 64 6f 6d 52 6f 77 69 64 3b 20 20 2f 2a RandomRowid; /*
a8e00 20 47 65 6e 65 72 61 74 65 20 6e 65 77 20 72 65 Generate new re
a8e10 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 73 65 6d cord numbers sem
a8e20 69 2d 72 61 6e 64 6f 6d 6c 79 20 2a 2f 0a 20 20 i-randomly */.
a8e30 42 6f 6f 6c 20 6e 75 6c 6c 52 6f 77 3b 20 20 20 Bool nullRow;
a8e40 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
a8e50 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 72 pointing to a r
a8e60 6f 77 20 77 69 74 68 20 6e 6f 20 64 61 74 61 20 ow with no data
a8e70 2a 2f 0a 20 20 42 6f 6f 6c 20 64 65 66 65 72 72 */. Bool deferr
a8e80 65 64 4d 6f 76 65 74 6f 3b 20 20 2f 2a 20 41 20 edMoveto; /* A
a8e90 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 call to sqlite3B
a8ea0 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 69 73 20 treeMoveto() is
a8eb0 6e 65 65 64 65 64 20 2a 2f 0a 20 20 42 6f 6f 6c needed */. Bool
a8ec0 20 69 73 54 61 62 6c 65 3b 20 20 20 20 20 20 20 isTable;
a8ed0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 74 /* True if a t
a8ee0 61 62 6c 65 20 72 65 71 75 69 72 69 6e 67 20 69 able requiring i
a8ef0 6e 74 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a 20 nteger keys */.
a8f00 20 42 6f 6f 6c 20 69 73 49 6e 64 65 78 3b 20 20 Bool isIndex;
a8f10 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
a8f20 66 20 61 6e 20 69 6e 64 65 78 20 63 6f 6e 74 61 f an index conta
a8f30 69 6e 69 6e 67 20 6b 65 79 73 20 6f 6e 6c 79 20 ining keys only
a8f40 2d 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69 - no data */. i
a8f50 36 34 20 6d 6f 76 65 74 6f 54 61 72 67 65 74 3b 64 movetoTarget;
a8f60 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 /* Argument
a8f70 20 74 6f 20 74 68 65 20 64 65 66 65 72 72 65 64 to the deferred
a8f80 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
a8f90 65 74 6f 28 29 20 2a 2f 0a 20 20 42 74 72 65 65 eto() */. Btree
a8fa0 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 *pBt;
a8fb0 20 2f 2a 20 53 65 70 61 72 61 74 65 20 66 69 6c /* Separate fil
a8fc0 65 20 68 6f 6c 64 69 6e 67 20 74 65 6d 70 6f 72 e holding tempor
a8fd0 61 72 79 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 ary table */. i
a8fe0 6e 74 20 70 73 65 75 64 6f 54 61 62 6c 65 52 65 nt pseudoTableRe
a8ff0 67 3b 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 g; /* Register
a9000 20 68 6f 6c 64 69 6e 67 20 70 73 65 75 64 6f 74 holding pseudot
a9010 61 62 6c 65 20 63 6f 6e 74 65 6e 74 2e 20 2a 2f able content. */
a9020 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 . KeyInfo *pKey
a9030 49 6e 66 6f 3b 20 20 20 20 2f 2a 20 49 6e 66 6f Info; /* Info
a9040 20 61 62 6f 75 74 20 69 6e 64 65 78 20 6b 65 79 about index key
a9050 73 20 6e 65 65 64 65 64 20 62 79 20 69 6e 64 65 s needed by inde
a9060 78 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 69 x cursors */. i
a9070 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 nt nField;
a9080 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
a9090 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 f fields in the
a90a0 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20 header */. i64
a90b0 73 65 71 43 6f 75 6e 74 3b 20 20 20 20 20 20 20 seqCount;
a90c0 20 20 2f 2a 20 53 65 71 75 65 6e 63 65 20 63 6f /* Sequence co
a90d0 75 6e 74 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 unter */. sqlit
a90e0 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a e3_vtab_cursor *
a90f0 70 56 74 61 62 43 75 72 73 6f 72 3b 20 20 2f 2a pVtabCursor; /*
a9100 20 54 68 65 20 63 75 72 73 6f 72 20 66 6f 72 20 The cursor for
a9110 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 a virtual table
a9120 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 */. const sqlit
a9130 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 e3_module *pModu
a9140 6c 65 3b 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c le; /* Modul
a9150 65 20 66 6f 72 20 63 75 72 73 6f 72 20 70 56 74 e for cursor pVt
a9160 61 62 43 75 72 73 6f 72 20 2a 2f 0a 0a 20 20 2f abCursor */.. /
a9170 2a 20 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 74 * Result of last
a9180 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
a9190 65 74 6f 28 29 20 64 6f 6e 65 20 62 79 20 61 6e eto() done by an
a91a0 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f 72 OP_NotExists or
a91b0 20 0a 20 20 2a 2a 20 4f 50 5f 49 73 55 6e 69 71 . ** OP_IsUniq
a91c0 75 65 20 6f 70 63 6f 64 65 20 6f 6e 20 74 68 69 ue opcode on thi
a91d0 73 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 69 s cursor. */. i
a91e0 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 3b 0a 0a nt seekResult;..
a91f0 20 20 2f 2a 20 43 61 63 68 65 64 20 69 6e 66 6f /* Cached info
a9200 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 rmation about th
a9210 65 20 68 65 61 64 65 72 20 66 6f 72 20 74 68 65 e header for the
a9220 20 64 61 74 61 20 72 65 63 6f 72 64 20 74 68 61 data record tha
a9230 74 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 73 6f t the. ** curso
a9240 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 r is currently p
a9250 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 4f 6e 6c ointing to. Onl
a9260 79 20 76 61 6c 69 64 20 69 66 20 63 61 63 68 65 y valid if cache
a9270 53 74 61 74 75 73 20 6d 61 74 63 68 65 73 0a 20 Status matches.
a9280 20 2a 2a 20 56 64 62 65 2e 63 61 63 68 65 43 74 ** Vdbe.cacheCt
a9290 72 2e 20 20 56 64 62 65 2e 63 61 63 68 65 43 74 r. Vdbe.cacheCt
a92a0 72 20 77 69 6c 6c 20 6e 65 76 65 72 20 74 61 6b r will never tak
a92b0 65 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f e on the value o
a92c0 66 0a 20 20 2a 2a 20 43 41 43 48 45 5f 53 54 41 f. ** CACHE_STA
a92d0 4c 45 20 61 6e 64 20 73 6f 20 73 65 74 74 69 6e LE and so settin
a92e0 67 20 63 61 63 68 65 53 74 61 74 75 73 3d 43 41 g cacheStatus=CA
a92f0 43 48 45 5f 53 54 41 4c 45 20 67 75 61 72 61 6e CHE_STALE guaran
a9300 74 65 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 74 tees that. ** t
a9310 68 65 20 63 61 63 68 65 20 69 73 20 6f 75 74 20 he cache is out
a9320 6f 66 20 64 61 74 65 2e 0a 20 20 2a 2a 0a 20 20 of date.. **.
a9330 2a 2a 20 61 52 6f 77 20 6d 69 67 68 74 20 70 6f ** aRow might po
a9340 69 6e 74 20 74 6f 20 28 65 70 68 65 6d 65 72 61 int to (ephemera
a9350 6c 29 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 l) data for the
a9360 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 6f 72 20 current row, or
a9370 69 74 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62 65 it might. ** be
a9380 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 75 33 NULL.. */. u3
a9390 32 20 63 61 63 68 65 53 74 61 74 75 73 3b 20 20 2 cacheStatus;
a93a0 20 20 20 20 2f 2a 20 43 61 63 68 65 20 69 73 20 /* Cache is
a93b0 76 61 6c 69 64 20 69 66 20 74 68 69 73 20 6d 61 valid if this ma
a93c0 74 63 68 65 73 20 56 64 62 65 2e 63 61 63 68 65 tches Vdbe.cache
a93d0 43 74 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61 79 Ctr */. int pay
a93e0 6c 6f 61 64 53 69 7a 65 3b 20 20 20 20 20 20 2f loadSize; /
a93f0 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f * Total number o
a9400 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 f bytes in the r
a9410 65 63 6f 72 64 20 2a 2f 0a 20 20 75 33 32 20 2a ecord */. u32 *
a9420 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 aType;
a9430 20 2f 2a 20 54 79 70 65 20 76 61 6c 75 65 73 20 /* Type values
a9440 66 6f 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 for all entries
a9450 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f in the record */
a9460 0a 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74 3b . u32 *aOffset;
a9470 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 /* Cach
a9480 65 64 20 6f 66 66 73 65 74 73 20 74 6f 20 74 68 ed offsets to th
a9490 65 20 73 74 61 72 74 20 6f 66 20 65 61 63 68 20 e start of each
a94a0 63 6f 6c 75 6d 6e 73 20 64 61 74 61 20 2a 2f 0a columns data */.
a94b0 20 20 75 38 20 2a 61 52 6f 77 3b 20 20 20 20 20 u8 *aRow;
a94c0 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 /* Data
a94d0 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 for the current
a94e0 72 6f 77 2c 20 69 66 20 61 6c 6c 20 6f 6e 20 6f row, if all on o
a94f0 6e 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 74 79 ne page */.};.ty
a9500 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 pedef struct Vdb
a9510 65 43 75 72 73 6f 72 20 56 64 62 65 43 75 72 73 eCursor VdbeCurs
a9520 6f 72 3b 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 or;../*.** When
a9530 61 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 69 73 a sub-program is
a9540 20 65 78 65 63 75 74 65 64 20 28 4f 50 5f 50 72 executed (OP_Pr
a9550 6f 67 72 61 6d 29 2c 20 61 20 73 74 72 75 63 74 ogram), a struct
a9560 75 72 65 20 6f 66 20 74 68 69 73 20 74 79 70 65 ure of this type
a9570 0a 2a 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 .** is allocated
a9580 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 75 to store the cu
a9590 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 rrent value of t
a95a0 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 he program count
a95b0 65 72 2c 20 61 73 0a 2a 2a 20 77 65 6c 6c 20 61 er, as.** well a
a95c0 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 65 s the current me
a95d0 6d 6f 72 79 20 63 65 6c 6c 20 61 72 72 61 79 20 mory cell array
a95e0 61 6e 64 20 76 61 72 69 6f 75 73 20 6f 74 68 65 and various othe
a95f0 72 20 66 72 61 6d 65 20 73 70 65 63 69 66 69 63 r frame specific
a9600 0a 2a 2a 20 76 61 6c 75 65 73 20 73 74 6f 72 65 .** values store
a9610 64 20 69 6e 20 74 68 65 20 56 64 62 65 20 73 74 d in the Vdbe st
a9620 72 75 63 74 2e 20 57 68 65 6e 20 74 68 65 20 73 ruct. When the s
a9630 75 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 66 69 ub-program is fi
a9640 6e 69 73 68 65 64 2c 20 0a 2a 2a 20 74 68 65 73 nished, .** thes
a9650 65 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f 70 e values are cop
a9660 69 65 64 20 62 61 63 6b 20 74 6f 20 74 68 65 20 ied back to the
a9670 56 64 62 65 20 66 72 6f 6d 20 74 68 65 20 56 64 Vdbe from the Vd
a9680 62 65 46 72 61 6d 65 20 73 74 72 75 63 74 75 72 beFrame structur
a9690 65 2c 0a 2a 2a 20 72 65 73 74 6f 72 69 6e 67 20 e,.** restoring
a96a0 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 the state of the
a96b0 20 56 4d 20 74 6f 20 61 73 20 69 74 20 77 61 73 VM to as it was
a96c0 20 62 65 66 6f 72 65 20 74 68 65 20 73 75 62 2d before the sub-
a96d0 70 72 6f 67 72 61 6d 0a 2a 2a 20 62 65 67 61 6e program.** began
a96e0 20 65 78 65 63 75 74 69 6e 67 2e 0a 2a 2a 0a 2a executing..**.*
a96f0 2a 20 46 72 61 6d 65 73 20 61 72 65 20 73 74 6f * Frames are sto
a9700 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 red in a linked
a9710 6c 69 73 74 20 68 65 61 64 65 64 20 61 74 20 56 list headed at V
a9720 64 62 65 2e 70 50 61 72 65 6e 74 2e 20 56 64 62 dbe.pParent. Vdb
a9730 65 2e 70 50 61 72 65 6e 74 0a 2a 2a 20 69 73 20 e.pParent.** is
a9740 74 68 65 20 70 61 72 65 6e 74 20 6f 66 20 74 68 the parent of th
a9750 65 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 2c e current frame,
a9760 20 6f 72 20 7a 65 72 6f 20 69 66 20 74 68 65 20 or zero if the
a9770 63 75 72 72 65 6e 74 20 66 72 61 6d 65 0a 2a 2a current frame.**
a9780 20 69 73 20 74 68 65 20 6d 61 69 6e 20 56 64 62 is the main Vdb
a9790 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 74 79 e program..*/.ty
a97a0 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 pedef struct Vdb
a97b0 65 46 72 61 6d 65 20 56 64 62 65 46 72 61 6d 65 eFrame VdbeFrame
a97c0 3b 0a 73 74 72 75 63 74 20 56 64 62 65 46 72 61 ;.struct VdbeFra
a97d0 6d 65 20 7b 0a 20 20 56 64 62 65 20 2a 76 3b 20 me {. Vdbe *v;
a97e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
a97f0 2a 20 56 4d 20 74 68 69 73 20 66 72 61 6d 65 20 * VM this frame
a9800 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a 20 20 belongs to */.
a9810 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 int pc;
a9820 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 /* Progr
a9830 61 6d 20 43 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 am Counter */.
a9840 4f 70 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 Op *aOp;
a9850 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 /* Progr
a9860 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 am instructions
a9870 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 3b 20 20 20 */. int nOp;
a9880 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a9890 53 69 7a 65 20 6f 66 20 61 4f 70 20 61 72 72 61 Size of aOp arra
a98a0 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d y */. Mem *aMem
a98b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
a98c0 2a 20 41 72 72 61 79 20 6f 66 20 6d 65 6d 6f 72 * Array of memor
a98d0 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 69 6e 74 y cells */. int
a98e0 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 nMem;
a98f0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
a9900 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 4d 65 f entries in aMe
a9910 6d 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f m */. VdbeCurso
a9920 72 20 2a 2a 61 70 43 73 72 3b 20 20 20 20 20 2f r **apCsr; /
a9930 2a 20 45 6c 65 6d 65 6e 74 20 6f 66 20 56 64 62 * Element of Vdb
a9940 65 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 75 e cursors */. u
a9950 31 36 20 6e 43 75 72 73 6f 72 3b 20 20 20 20 20 16 nCursor;
a9960 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
a9970 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 of entries in a
a9980 70 43 73 72 20 2a 2f 0a 20 20 76 6f 69 64 20 2a pCsr */. void *
a9990 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 token;
a99a0 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 53 75 62 /* Copy of Sub
a99b0 50 72 6f 67 72 61 6d 2e 74 6f 6b 65 6e 20 2a 2f Program.token */
a99c0 0a 20 20 69 6e 74 20 6e 43 68 69 6c 64 4d 65 6d . int nChildMem
a99d0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 ; /* Nu
a99e0 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63 mber of memory c
a99f0 65 6c 6c 73 20 66 6f 72 20 63 68 69 6c 64 20 66 ells for child f
a9a00 72 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 rame */. int nC
a9a10 68 69 6c 64 43 73 72 3b 20 20 20 20 20 20 20 20 hildCsr;
a9a20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
a9a30 75 72 73 6f 72 73 20 66 6f 72 20 63 68 69 6c 64 ursors for child
a9a40 20 66 72 61 6d 65 20 2a 2f 0a 20 20 69 36 34 20 frame */. i64
a9a50 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20 20 lastRowid;
a9a60 20 20 20 20 2f 2a 20 4c 61 73 74 20 69 6e 73 65 /* Last inse
a9a70 72 74 20 72 6f 77 69 64 20 28 73 71 6c 69 74 65 rt rowid (sqlite
a9a80 33 2e 6c 61 73 74 52 6f 77 69 64 29 20 2a 2f 0a 3.lastRowid) */.
a9a90 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20 20 int nChange;
a9aa0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 /* Sta
a9ab0 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 73 20 28 tement changes (
a9ac0 56 64 62 65 2e 6e 43 68 61 6e 67 65 73 29 20 20 Vdbe.nChanges)
a9ad0 20 20 20 2a 2f 0a 20 20 56 64 62 65 46 72 61 6d */. VdbeFram
a9ae0 65 20 2a 70 50 61 72 65 6e 74 3b 20 20 20 20 20 e *pParent;
a9af0 2f 2a 20 50 61 72 65 6e 74 20 6f 66 20 74 68 69 /* Parent of thi
a9b00 73 20 66 72 61 6d 65 20 2a 2f 0a 7d 3b 0a 0a 23 s frame */.};..#
a9b10 64 65 66 69 6e 65 20 56 64 62 65 46 72 61 6d 65 define VdbeFrame
a9b20 4d 65 6d 28 70 29 20 28 28 4d 65 6d 20 2a 29 26 Mem(p) ((Mem *)&
a9b30 28 28 75 38 20 2a 29 70 29 5b 52 4f 55 4e 44 38 ((u8 *)p)[ROUND8
a9b40 28 73 69 7a 65 6f 66 28 56 64 62 65 46 72 61 6d (sizeof(VdbeFram
a9b50 65 29 29 5d 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 76 e))])../*.** A v
a9b60 61 6c 75 65 20 66 6f 72 20 56 64 62 65 43 75 72 alue for VdbeCur
a9b70 73 6f 72 2e 63 61 63 68 65 56 61 6c 69 64 20 74 sor.cacheValid t
a9b80 68 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 61 hat means the ca
a9b90 63 68 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e che is always in
a9ba0 76 61 6c 69 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e valid..*/.#defin
a9bb0 65 20 43 41 43 48 45 5f 53 54 41 4c 45 20 30 0a e CACHE_STALE 0.
a9bc0 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 6c ./*.** Internall
a9bd0 79 2c 20 74 68 65 20 76 64 62 65 20 6d 61 6e 69 y, the vdbe mani
a9be0 70 75 6c 61 74 65 73 20 6e 65 61 72 6c 79 20 61 pulates nearly a
a9bf0 6c 6c 20 53 51 4c 20 76 61 6c 75 65 73 20 61 73 ll SQL values as
a9c00 20 4d 65 6d 0a 2a 2a 20 73 74 72 75 63 74 75 72 Mem.** structur
a9c10 65 73 2e 20 45 61 63 68 20 4d 65 6d 20 73 74 72 es. Each Mem str
a9c20 75 63 74 20 6d 61 79 20 63 61 63 68 65 20 6d 75 uct may cache mu
a9c30 6c 74 69 70 6c 65 20 72 65 70 72 65 73 65 6e 74 ltiple represent
a9c40 61 74 69 6f 6e 73 20 28 73 74 72 69 6e 67 2c 0a ations (string,.
a9c50 2a 2a 20 69 6e 74 65 67 65 72 20 65 74 63 2e 29 ** integer etc.)
a9c60 20 6f 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c of the same val
a9c70 75 65 2e 20 20 41 20 76 61 6c 75 65 20 28 61 6e ue. A value (an
a9c80 64 20 74 68 65 72 65 66 6f 72 65 20 4d 65 6d 20 d therefore Mem
a9c90 73 74 72 75 63 74 75 72 65 29 0a 2a 2a 20 68 61 structure).** ha
a9ca0 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 s the following
a9cb0 70 72 6f 70 65 72 74 69 65 73 3a 0a 2a 2a 0a 2a properties:.**.*
a9cc0 2a 20 45 61 63 68 20 76 61 6c 75 65 20 68 61 73 * Each value has
a9cd0 20 61 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 a manifest type
a9ce0 2e 20 54 68 65 20 6d 61 6e 69 66 65 73 74 20 74 . The manifest t
a9cf0 79 70 65 20 6f 66 20 74 68 65 20 76 61 6c 75 65 ype of the value
a9d00 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 20 stored.** in a
a9d10 4d 65 6d 20 73 74 72 75 63 74 20 69 73 20 72 65 Mem struct is re
a9d20 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 4d 65 turned by the Me
a9d30 6d 54 79 70 65 28 4d 65 6d 2a 29 20 6d 61 63 72 mType(Mem*) macr
a9d40 6f 2e 20 54 68 65 20 74 79 70 65 20 69 73 0a 2a o. The type is.*
a9d50 2a 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f * one of SQLITE_
a9d60 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 49 4e 54 NULL, SQLITE_INT
a9d70 45 47 45 52 2c 20 53 51 4c 49 54 45 5f 52 45 41 EGER, SQLITE_REA
a9d80 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 20 6f L, SQLITE_TEXT o
a9d90 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 4c 4f 42 r.** SQLITE_BLOB
a9da0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 20 ..*/.struct Mem
a9db0 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 {. union {.
a9dc0 69 36 34 20 69 3b 20 20 20 20 20 20 20 20 20 20 i64 i;
a9dd0 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 /* Integer v
a9de0 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 alue. */. int
a9df0 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 20 nZero;
a9e00 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 62 69 /* Used when bi
a9e10 74 20 4d 45 4d 5f 5a 65 72 6f 20 69 73 20 73 65 t MEM_Zero is se
a9e20 74 20 69 6e 20 66 6c 61 67 73 20 2a 2f 0a 20 20 t in flags */.
a9e30 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b FuncDef *pDef;
a9e40 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e /* Used on
a9e50 6c 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d ly when flags==M
a9e60 45 4d 5f 41 67 67 20 2a 2f 0a 20 20 20 20 52 6f EM_Agg */. Ro
a9e70 77 53 65 74 20 2a 70 52 6f 77 53 65 74 3b 20 20 wSet *pRowSet;
a9e80 20 20 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 /* Used only w
a9e90 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 52 hen flags==MEM_R
a9ea0 6f 77 53 65 74 20 2a 2f 0a 20 20 20 20 56 64 62 owSet */. Vdb
a9eb0 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 eFrame *pFrame;
a9ec0 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 66 6c /* Used when fl
a9ed0 61 67 73 3d 3d 4d 45 4d 5f 46 72 61 6d 65 20 2a ags==MEM_Frame *
a9ee0 2f 0a 20 20 7d 20 75 3b 0a 20 20 64 6f 75 62 6c /. } u;. doubl
a9ef0 65 20 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f e r; /
a9f00 2a 20 52 65 61 6c 20 76 61 6c 75 65 20 2a 2f 0a * Real value */.
a9f10 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
a9f20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73 /* The ass
a9f30 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65 ociated database
a9f40 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 connection */.
a9f50 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 char *z;
a9f60 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f /* String o
a9f70 72 20 42 4c 4f 42 20 76 61 6c 75 65 20 2a 2f 0a r BLOB value */.
a9f80 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 int n;
a9f90 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
a9fa0 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e of characters in
a9fb0 20 73 74 72 69 6e 67 20 76 61 6c 75 65 2c 20 65 string value, e
a9fc0 78 63 6c 75 64 69 6e 67 20 27 5c 30 27 20 2a 2f xcluding '\0' */
a9fd0 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 . u16 flags;
a9fe0 20 20 20 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 /* Some c
a9ff0 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 4d 45 ombination of ME
aa000 4d 5f 4e 75 6c 6c 2c 20 4d 45 4d 5f 53 74 72 2c M_Null, MEM_Str,
aa010 20 4d 45 4d 5f 44 79 6e 2c 20 65 74 63 2e 20 2a MEM_Dyn, etc. *
aa020 2f 0a 20 20 75 38 20 20 74 79 70 65 3b 20 20 20 /. u8 type;
aa030 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f /* One o
aa040 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 53 f SQLITE_NULL, S
aa050 51 4c 49 54 45 5f 54 45 58 54 2c 20 53 51 4c 49 QLITE_TEXT, SQLI
aa060 54 45 5f 49 4e 54 45 47 45 52 2c 20 65 74 63 20 TE_INTEGER, etc
aa070 2a 2f 0a 20 20 75 38 20 20 65 6e 63 3b 20 20 20 */. u8 enc;
aa080 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 /* SQLI
aa090 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f TE_UTF8, SQLITE_
aa0a0 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54 45 5f UTF16BE, SQLITE_
aa0b0 55 54 46 31 36 4c 45 20 2a 2f 0a 20 20 76 6f 69 UTF16LE */. voi
aa0c0 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a d (*xDel)(void *
aa0d0 29 3b 20 20 2f 2a 20 49 66 20 6e 6f 74 20 6e 75 ); /* If not nu
aa0e0 6c 6c 2c 20 63 61 6c 6c 20 74 68 69 73 20 66 75 ll, call this fu
aa0f0 6e 63 74 69 6f 6e 20 74 6f 20 64 65 6c 65 74 65 nction to delete
aa100 20 4d 65 6d 2e 7a 20 2a 2f 0a 20 20 63 68 61 72 Mem.z */. char
aa110 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 20 20 20 20 *zMalloc;
aa120 2f 2a 20 44 79 6e 61 6d 69 63 20 62 75 66 66 65 /* Dynamic buffe
aa130 72 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 r allocated by s
aa140 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
aa150 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 4f 6e 65 20 6f 72 */.};../* One or
aa160 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 66 6f 6c more of the fol
aa170 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 61 72 65 lowing flags are
aa180 20 73 65 74 20 74 6f 20 69 6e 64 69 63 61 74 65 set to indicate
aa190 20 74 68 65 20 76 61 6c 69 64 4f 4b 0a 2a 2a 20 the validOK.**
aa1a0 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 representations
aa1b0 6f 66 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f of the value sto
aa1c0 72 65 64 20 69 6e 20 74 68 65 20 4d 65 6d 20 73 red in the Mem s
aa1d0 74 72 75 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 truct..**.** If
aa1e0 74 68 65 20 4d 45 4d 5f 4e 75 6c 6c 20 66 6c 61 the MEM_Null fla
aa1f0 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 g is set, then t
aa200 68 65 20 76 61 6c 75 65 20 69 73 20 61 6e 20 53 he value is an S
aa210 51 4c 20 4e 55 4c 4c 20 76 61 6c 75 65 2e 0a 2a QL NULL value..*
aa220 2a 20 4e 6f 20 6f 74 68 65 72 20 66 6c 61 67 73 * No other flags
aa230 20 6d 61 79 20 62 65 20 73 65 74 20 69 6e 20 74 may be set in t
aa240 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 his case..**.**
aa250 49 66 20 74 68 65 20 4d 45 4d 5f 53 74 72 20 66 If the MEM_Str f
aa260 6c 61 67 20 69 73 20 73 65 74 20 74 68 65 6e 20 lag is set then
aa270 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 61 74 20 Mem.z points at
aa280 61 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 a string represe
aa290 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 55 73 75 61 ntation..** Usua
aa2a0 6c 6c 79 20 74 68 69 73 20 69 73 20 65 6e 63 6f lly this is enco
aa2b0 64 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 ded in the same
aa2c0 75 6e 69 63 6f 64 65 20 65 6e 63 6f 64 69 6e 67 unicode encoding
aa2d0 20 61 73 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 as the main.**
aa2e0 64 61 74 61 62 61 73 65 20 28 73 65 65 20 62 65 database (see be
aa2f0 6c 6f 77 20 66 6f 72 20 65 78 63 65 70 74 69 6f low for exceptio
aa300 6e 73 29 2e 20 49 66 20 74 68 65 20 4d 45 4d 5f ns). If the MEM_
aa310 54 65 72 6d 20 66 6c 61 67 20 69 73 20 61 6c 73 Term flag is als
aa320 6f 0a 2a 2a 20 73 65 74 2c 20 74 68 65 6e 20 74 o.** set, then t
aa330 68 65 20 73 74 72 69 6e 67 20 69 73 20 6e 75 6c he string is nul
aa340 20 74 65 72 6d 69 6e 61 74 65 64 2e 20 54 68 65 terminated. The
aa350 20 4d 45 4d 5f 49 6e 74 20 61 6e 64 20 4d 45 4d MEM_Int and MEM
aa360 5f 52 65 61 6c 20 0a 2a 2a 20 66 6c 61 67 73 20 _Real .** flags
aa370 6d 61 79 20 63 6f 65 78 69 73 74 20 77 69 74 68 may coexist with
aa380 20 74 68 65 20 4d 45 4d 5f 53 74 72 20 66 6c 61 the MEM_Str fla
aa390 67 2e 0a 2a 2a 0a 2a 2a 20 4d 75 6c 74 69 70 6c g..**.** Multipl
aa3a0 65 20 6f 66 20 74 68 65 73 65 20 76 61 6c 75 65 e of these value
aa3b0 73 20 63 61 6e 20 61 70 70 65 61 72 20 69 6e 20 s can appear in
aa3c0 4d 65 6d 2e 66 6c 61 67 73 2e 20 20 42 75 74 20 Mem.flags. But
aa3d0 6f 6e 6c 79 20 6f 6e 65 0a 2a 2a 20 61 74 20 61 only one.** at a
aa3e0 20 74 69 6d 65 20 63 61 6e 20 61 70 70 65 61 72 time can appear
aa3f0 20 69 6e 20 4d 65 6d 2e 74 79 70 65 2e 0a 2a 2f in Mem.type..*/
aa400 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 4e 75 6c .#define MEM_Nul
aa410 6c 20 20 20 20 20 20 30 78 30 30 30 31 20 20 20 l 0x0001
aa420 2f 2a 20 56 61 6c 75 65 20 69 73 20 4e 55 4c 4c /* Value is NULL
aa430 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f */.#define MEM_
aa440 53 74 72 20 20 20 20 20 20 20 30 78 30 30 30 32 Str 0x0002
aa450 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 /* Value is a
aa460 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 string */.#defi
aa470 6e 65 20 4d 45 4d 5f 49 6e 74 20 20 20 20 20 20 ne MEM_Int
aa480 20 30 78 30 30 30 34 20 20 20 2f 2a 20 56 61 6c 0x0004 /* Val
aa490 75 65 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 ue is an integer
aa4a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f */.#define MEM_
aa4b0 52 65 61 6c 20 20 20 20 20 20 30 78 30 30 30 38 Real 0x0008
aa4c0 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 /* Value is a
aa4d0 20 72 65 61 6c 20 6e 75 6d 62 65 72 20 2a 2f 0a real number */.
aa4e0 23 64 65 66 69 6e 65 20 4d 45 4d 5f 42 6c 6f 62 #define MEM_Blob
aa4f0 20 20 20 20 20 20 30 78 30 30 31 30 20 20 20 2f 0x0010 /
aa500 2a 20 56 61 6c 75 65 20 69 73 20 61 20 42 4c 4f * Value is a BLO
aa510 42 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d B */.#define MEM
aa520 5f 52 6f 77 53 65 74 20 20 20 20 30 78 30 30 32 _RowSet 0x002
aa530 30 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 0 /* Value is
aa540 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 a RowSet object
aa550 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 46 */.#define MEM_F
aa560 72 61 6d 65 20 20 20 20 20 30 78 30 30 34 30 20 rame 0x0040
aa570 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 20 /* Value is a
aa580 56 64 62 65 46 72 61 6d 65 20 6f 62 6a 65 63 74 VdbeFrame object
aa590 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f */.#define MEM_
aa5a0 54 79 70 65 4d 61 73 6b 20 20 30 78 30 30 66 66 TypeMask 0x00ff
aa5b0 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 74 79 /* Mask of ty
aa5c0 70 65 20 62 69 74 73 20 2a 2f 0a 0a 2f 2a 20 57 pe bits */../* W
aa5d0 68 65 6e 65 76 65 72 20 4d 65 6d 20 63 6f 6e 74 henever Mem cont
aa5e0 61 69 6e 73 20 61 20 76 61 6c 69 64 20 73 74 72 ains a valid str
aa5f0 69 6e 67 20 6f 72 20 62 6c 6f 62 20 72 65 70 72 ing or blob repr
aa600 65 73 65 6e 74 61 74 69 6f 6e 2c 20 6f 6e 65 20 esentation, one
aa610 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 of.** the follow
aa620 69 6e 67 20 66 6c 61 67 73 20 6d 75 73 74 20 62 ing flags must b
aa630 65 20 73 65 74 20 74 6f 20 64 65 74 65 72 6d 69 e set to determi
aa640 6e 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 6d 61 ne the memory ma
aa650 6e 61 67 65 6d 65 6e 74 0a 2a 2a 20 70 6f 6c 69 nagement.** poli
aa660 63 79 20 66 6f 72 20 4d 65 6d 2e 7a 2e 20 20 54 cy for Mem.z. T
aa670 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67 he MEM_Term flag
aa680 20 74 65 6c 6c 73 20 75 73 20 77 68 65 74 68 65 tells us whethe
aa690 72 20 6f 72 20 6e 6f 74 20 74 68 65 0a 2a 2a 20 r or not the.**
aa6a0 73 74 72 69 6e 67 20 69 73 20 5c 30 30 30 20 6f string is \000 o
aa6b0 72 20 5c 75 30 30 30 30 20 74 65 72 6d 69 6e 61 r \u0000 termina
aa6c0 74 65 64 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d ted.*/.#define M
aa6d0 45 4d 5f 54 65 72 6d 20 20 20 20 20 20 30 78 30 EM_Term 0x0
aa6e0 32 30 30 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 200 /* String
aa6f0 72 65 70 20 69 73 20 6e 75 6c 20 74 65 72 6d 69 rep is nul termi
aa700 6e 61 74 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 nated */.#define
aa710 20 4d 45 4d 5f 44 79 6e 20 20 20 20 20 20 20 30 MEM_Dyn 0
aa720 78 30 34 30 30 20 20 20 2f 2a 20 4e 65 65 64 20 x0400 /* Need
aa730 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 46 72 to call sqliteFr
aa740 65 65 28 29 20 6f 6e 20 4d 65 6d 2e 7a 20 2a 2f ee() on Mem.z */
aa750 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 53 74 61 .#define MEM_Sta
aa760 74 69 63 20 20 20 20 30 78 30 38 30 30 20 20 20 tic 0x0800
aa770 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 /* Mem.z points
aa780 74 6f 20 61 20 73 74 61 74 69 63 20 73 74 72 69 to a static stri
aa790 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 ng */.#define ME
aa7a0 4d 5f 45 70 68 65 6d 20 20 20 20 20 30 78 31 30 M_Ephem 0x10
aa7b0 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 00 /* Mem.z po
aa7c0 69 6e 74 73 20 74 6f 20 61 6e 20 65 70 68 65 6d ints to an ephem
aa7d0 65 72 61 6c 20 73 74 72 69 6e 67 20 2a 2f 0a 23 eral string */.#
aa7e0 64 65 66 69 6e 65 20 4d 45 4d 5f 41 67 67 20 20 define MEM_Agg
aa7f0 20 20 20 20 20 30 78 32 30 30 30 20 20 20 2f 2a 0x2000 /*
aa800 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f Mem.z points to
aa810 20 61 6e 20 61 67 67 20 66 75 6e 63 74 69 6f 6e an agg function
aa820 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 23 64 65 66 context */.#def
aa830 69 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20 20 20 20 ine MEM_Zero
aa840 20 20 30 78 34 30 30 30 20 20 20 2f 2a 20 4d 65 0x4000 /* Me
aa850 6d 2e 69 20 63 6f 6e 74 61 69 6e 73 20 63 6f 75 m.i contains cou
aa860 6e 74 20 6f 66 20 30 73 20 61 70 70 65 6e 64 65 nt of 0s appende
aa870 64 20 74 6f 20 62 6c 6f 62 20 2a 2f 0a 0a 23 69 d to blob */..#i
aa880 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
aa890 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 23 75 6e 64 _INCRBLOB. #und
aa8a0 65 66 20 4d 45 4d 5f 5a 65 72 6f 0a 20 20 23 64 ef MEM_Zero. #d
aa8b0 65 66 69 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20 30 efine MEM_Zero 0
aa8c0 78 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f x0000.#endif.../
aa8d0 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 79 20 65 *.** Clear any e
aa8e0 78 69 73 74 69 6e 67 20 74 79 70 65 20 66 6c 61 xisting type fla
aa8f0 67 73 20 66 72 6f 6d 20 61 20 4d 65 6d 20 61 6e gs from a Mem an
aa900 64 20 72 65 70 6c 61 63 65 20 74 68 65 6d 20 77 d replace them w
aa910 69 74 68 20 66 0a 2a 2f 0a 23 64 65 66 69 6e 65 ith f.*/.#define
aa920 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 MemSetTypeFlag(
aa930 70 2c 20 66 29 20 5c 0a 20 20 20 28 28 70 29 2d p, f) \. ((p)-
aa940 3e 66 6c 61 67 73 20 3d 20 28 28 70 29 2d 3e 66 >flags = ((p)->f
aa950 6c 61 67 73 26 7e 28 4d 45 4d 5f 54 79 70 65 4d lags&~(MEM_TypeM
aa960 61 73 6b 7c 4d 45 4d 5f 5a 65 72 6f 29 29 7c 66 ask|MEM_Zero))|f
aa970 29 0a 0a 0a 2f 2a 20 41 20 56 64 62 65 46 75 6e ).../* A VdbeFun
aa980 63 20 69 73 20 6a 75 73 74 20 61 20 46 75 6e 63 c is just a Func
aa990 44 65 66 20 28 64 65 66 69 6e 65 64 20 69 6e 20 Def (defined in
aa9a0 73 71 6c 69 74 65 49 6e 74 2e 68 29 20 74 68 61 sqliteInt.h) tha
aa9b0 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 64 t contains.** ad
aa9c0 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
aa9d0 74 69 6f 6e 20 61 62 6f 75 74 20 61 75 78 69 6c tion about auxil
aa9e0 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e iary information
aa9f0 20 62 6f 75 6e 64 20 74 6f 20 61 72 67 75 6d 65 bound to argume
aaa00 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 66 75 nts.** of the fu
aaa10 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 nction. This is
aaa20 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 used to impleme
aaa30 6e 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 67 nt the sqlite3_g
aaa40 65 74 5f 61 75 78 64 61 74 61 28 29 0a 2a 2a 20 et_auxdata().**
aaa50 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 65 74 5f and sqlite3_set_
aaa60 61 75 78 64 61 74 61 28 29 20 41 50 49 73 2e 20 auxdata() APIs.
aaa70 20 54 68 65 20 22 61 75 78 64 61 74 61 22 20 69 The "auxdata" i
aaa80 73 20 73 6f 6d 65 20 61 75 78 69 6c 69 61 72 79 s some auxiliary
aaa90 20 64 61 74 61 0a 2a 2a 20 74 68 61 74 20 63 61 data.** that ca
aaaa0 6e 20 62 65 20 61 73 73 6f 63 69 61 74 65 64 20 n be associated
aaab0 77 69 74 68 20 61 20 63 6f 6e 73 74 61 6e 74 20 with a constant
aaac0 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 20 66 75 argument to a fu
aaad0 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a 2a nction. This.**
aaae0 20 61 6c 6c 6f 77 73 20 66 75 6e 63 74 69 6f 6e allows function
aaaf0 73 20 73 75 63 68 20 61 73 20 22 72 65 67 65 78 s such as "regex
aab00 70 22 20 74 6f 20 63 6f 6d 70 69 6c 65 20 74 68 p" to compile th
aab10 65 69 72 20 63 6f 6e 73 74 61 6e 74 20 72 65 67 eir constant reg
aab20 75 6c 61 72 0a 2a 2a 20 65 78 70 72 65 73 73 69 ular.** expressi
aab30 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 6e 63 65 on argument once
aab40 20 61 6e 64 20 72 65 75 73 65 64 20 74 68 65 20 and reused the
aab50 63 6f 6d 70 69 6c 65 64 20 63 6f 64 65 20 66 6f compiled code fo
aab60 72 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 69 6e r multiple.** in
aab70 76 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 vocations..*/.st
aab80 72 75 63 74 20 56 64 62 65 46 75 6e 63 20 7b 0a ruct VdbeFunc {.
aab90 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 FuncDef *pFunc
aaba0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
aabb0 2f 2a 20 54 68 65 20 64 65 66 69 6e 69 74 69 6f /* The definitio
aabc0 6e 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f n of the functio
aabd0 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 75 78 3b n */. int nAux;
aabe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aabf0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
aac00 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 f entries alloca
aac10 74 65 64 20 66 6f 72 20 61 70 41 75 78 5b 5d 20 ted for apAux[]
aac20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 75 78 44 */. struct AuxD
aac30 61 74 61 20 7b 0a 20 20 20 20 76 6f 69 64 20 2a ata {. void *
aac40 70 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20 pAux;
aac50 20 20 20 20 20 20 20 20 2f 2a 20 41 75 78 20 64 /* Aux d
aac60 61 74 61 20 66 6f 72 20 74 68 65 20 69 2d 74 68 ata for the i-th
aac70 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 20 20 20 argument */.
aac80 20 76 6f 69 64 20 28 2a 78 44 65 6c 65 74 65 29 void (*xDelete)
aac90 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 20 2f (void *); /
aaca0 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 * Destructor for
aacb0 20 74 68 65 20 61 75 78 20 64 61 74 61 20 2a 2f the aux data */
aacc0 0a 20 20 7d 20 61 70 41 75 78 5b 31 5d 3b 20 20 . } apAux[1];
aacd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aace0 20 2f 2a 20 4f 6e 65 20 73 6c 6f 74 20 66 6f 72 /* One slot for
aacf0 20 65 61 63 68 20 66 75 6e 63 74 69 6f 6e 20 61 each function a
aad00 72 67 75 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f rgument */.};../
aad10 2a 0a 2a 2a 20 54 68 65 20 22 63 6f 6e 74 65 78 *.** The "contex
aad20 74 22 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 t" argument for
aad30 61 20 69 6e 73 74 61 6c 6c 61 62 6c 65 20 66 75 a installable fu
aad40 6e 63 74 69 6f 6e 2e 20 20 41 20 70 6f 69 6e 74 nction. A point
aad50 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 er to an.** inst
aad60 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 ance of this str
aad70 75 63 74 75 72 65 20 69 73 20 74 68 65 20 66 69 ucture is the fi
aad80 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 rst argument to
aad90 74 68 65 20 72 6f 75 74 69 6e 65 73 20 75 73 65 the routines use
aada0 64 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 74 d.** implement t
aadb0 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 he SQL functions
aadc0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 ..**.** There is
aadd0 20 61 20 74 79 70 65 64 65 66 20 66 6f 72 20 74 a typedef for t
aade0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 6e his structure in
aadf0 20 73 71 6c 69 74 65 2e 68 2e 20 20 53 6f 20 61 sqlite.h. So a
aae00 6c 6c 20 72 6f 75 74 69 6e 65 73 2c 0a 2a 2a 20 ll routines,.**
aae10 65 76 65 6e 20 74 68 65 20 70 75 62 6c 69 63 20 even the public
aae20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 53 51 4c interface to SQL
aae30 69 74 65 2c 20 63 61 6e 20 75 73 65 20 61 20 70 ite, can use a p
aae40 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 ointer to this s
aae50 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 42 75 74 tructure..** But
aae60 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 74 68 this file is th
aae70 65 20 6f 6e 6c 79 20 70 6c 61 63 65 20 77 68 65 e only place whe
aae80 72 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 re the internal
aae90 64 65 74 61 69 6c 73 20 6f 66 20 74 68 69 73 0a details of this.
aaea0 2a 2a 20 73 74 72 75 63 74 75 72 65 20 61 72 65 ** structure are
aaeb0 20 6b 6e 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 known..**.** Th
aaec0 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 is structure is
aaed0 64 65 66 69 6e 65 64 20 69 6e 73 69 64 65 20 6f defined inside o
aaee0 66 20 76 64 62 65 49 6e 74 2e 68 20 62 65 63 61 f vdbeInt.h beca
aaef0 75 73 65 20 69 74 20 75 73 65 73 20 73 75 62 73 use it uses subs
aaf00 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 28 4d 65 tructures.** (Me
aaf10 6d 29 20 77 68 69 63 68 20 61 72 65 20 6f 6e 6c m) which are onl
aaf20 79 20 64 65 66 69 6e 65 64 20 74 68 65 72 65 2e y defined there.
aaf30 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 .*/.struct sqlit
aaf40 65 33 5f 63 6f 6e 74 65 78 74 20 7b 0a 20 20 46 e3_context {. F
aaf50 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 uncDef *pFunc;
aaf60 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
aaf70 74 6f 20 66 75 6e 63 74 69 6f 6e 20 69 6e 66 6f to function info
aaf80 72 6d 61 74 69 6f 6e 2e 20 20 4d 55 53 54 20 42 rmation. MUST B
aaf90 45 20 46 49 52 53 54 20 2a 2f 0a 20 20 56 64 62 E FIRST */. Vdb
aafa0 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 eFunc *pVdbeFunc
aafb0 3b 20 20 2f 2a 20 41 75 78 69 6c 61 72 79 20 64 ; /* Auxilary d
aafc0 61 74 61 2c 20 69 66 20 63 72 65 61 74 65 64 2e ata, if created.
aafd0 20 2a 2f 0a 20 20 4d 65 6d 20 73 3b 20 20 20 20 */. Mem s;
aafe0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
aaff0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
ab000 69 73 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a is stored here *
ab010 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 /. Mem *pMem;
ab020 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d /* Mem
ab030 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 74 6f ory cell used to
ab040 20 73 74 6f 72 65 20 61 67 67 72 65 67 61 74 65 store aggregate
ab050 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e context */. in
ab060 74 20 69 73 45 72 72 6f 72 3b 20 20 20 20 20 20 t isError;
ab070 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64 /* Error cod
ab080 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 e returned by th
ab090 65 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0a 20 e function. */.
ab0a0 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b CollSeq *pColl;
ab0b0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 /* Collat
ab0c0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a ing sequence */.
ab0d0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 53 65 74 20 };../*.** A Set
ab0e0 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 structure is use
ab0f0 64 20 66 6f 72 20 71 75 69 63 6b 20 74 65 73 74 d for quick test
ab100 69 6e 67 20 74 6f 20 73 65 65 20 69 66 20 61 20 ing to see if a
ab110 76 61 6c 75 65 0a 2a 2a 20 69 73 20 70 61 72 74 value.** is part
ab120 20 6f 66 20 61 20 73 6d 61 6c 6c 20 73 65 74 2e of a small set.
ab130 20 20 53 65 74 73 20 61 72 65 20 75 73 65 64 20 Sets are used
ab140 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 63 6f 64 to implement cod
ab150 65 20 6c 69 6b 65 0a 2a 2a 20 74 68 69 73 3a 0a e like.** this:.
ab160 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 78 2e ** x.
ab170 79 20 49 4e 20 28 27 68 69 27 2c 27 68 6f 6f 27 y IN ('hi','hoo'
ab180 2c 27 68 75 6d 27 29 0a 2a 2f 0a 74 79 70 65 64 ,'hum').*/.typed
ab190 65 66 20 73 74 72 75 63 74 20 53 65 74 20 53 65 ef struct Set Se
ab1a0 74 3b 0a 73 74 72 75 63 74 20 53 65 74 20 7b 0a t;.struct Set {.
ab1b0 20 20 48 61 73 68 20 68 61 73 68 3b 20 20 20 20 Hash hash;
ab1c0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 65 /* A se
ab1d0 74 20 69 73 20 6a 75 73 74 20 61 20 68 61 73 68 t is just a hash
ab1e0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68 table */. Hash
ab1f0 45 6c 65 6d 20 2a 70 72 65 76 3b 20 20 20 20 20 Elem *prev;
ab200 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 6c 79 /* Previously
ab210 20 61 63 63 65 73 73 65 64 20 68 61 73 68 20 65 accessed hash e
ab220 6c 65 6d 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a lemen */.};../*.
ab230 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
ab240 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 f the virtual ma
ab250 63 68 69 6e 65 2e 20 20 54 68 69 73 20 73 74 72 chine. This str
ab260 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 ucture contains
ab270 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 the complete.**
ab280 73 74 61 74 65 20 6f 66 20 74 68 65 20 76 69 72 state of the vir
ab290 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a tual machine..**
ab2a0 0a 2a 2a 20 54 68 65 20 22 73 71 6c 69 74 65 33 .** The "sqlite3
ab2b0 5f 73 74 6d 74 22 20 73 74 72 75 63 74 75 72 65 _stmt" structure
ab2c0 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 pointer that is
ab2d0 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c returned by sql
ab2e0 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29 0a 2a ite3_compile().*
ab2f0 2a 20 69 73 20 72 65 61 6c 6c 79 20 61 20 70 6f * is really a po
ab300 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 inter to an inst
ab310 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 ance of this str
ab320 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 ucture..**.** Th
ab330 65 20 56 64 62 65 2e 69 6e 56 74 61 62 4d 65 74 e Vdbe.inVtabMet
ab340 68 6f 64 20 76 61 72 69 61 62 6c 65 20 69 73 20 hod variable is
ab350 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 set to non-zero
ab360 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e for the duration
ab370 20 6f 66 0a 2a 2a 20 61 6e 79 20 76 69 72 74 75 of.** any virtu
ab380 61 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f 64 20 al table method
ab390 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6d 61 64 65 invocations made
ab3a0 20 62 79 20 74 68 65 20 76 64 62 65 20 70 72 6f by the vdbe pro
ab3b0 67 72 61 6d 2e 20 49 74 20 69 73 0a 2a 2a 20 73 gram. It is.** s
ab3c0 65 74 20 74 6f 20 32 20 66 6f 72 20 78 44 65 73 et to 2 for xDes
ab3d0 74 72 6f 79 20 6d 65 74 68 6f 64 20 63 61 6c 6c troy method call
ab3e0 73 20 61 6e 64 20 31 20 66 6f 72 20 61 6c 6c 20 s and 1 for all
ab3f0 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 2e 20 54 other methods. T
ab400 68 69 73 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 his.** variable
ab410 69 73 20 75 73 65 64 20 66 6f 72 20 74 77 6f 20 is used for two
ab420 70 75 72 70 6f 73 65 73 3a 20 74 6f 20 61 6c 6c purposes: to all
ab430 6f 77 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68 ow xDestroy meth
ab440 6f 64 73 20 74 6f 20 65 78 65 63 75 74 65 0a 2a ods to execute.*
ab450 2a 20 22 44 52 4f 50 20 54 41 42 4c 45 22 20 73 * "DROP TABLE" s
ab460 74 61 74 65 6d 65 6e 74 73 20 61 6e 64 20 74 6f tatements and to
ab470 20 70 72 65 76 65 6e 74 20 73 6f 6d 65 20 6e 61 prevent some na
ab480 73 74 79 20 73 69 64 65 20 65 66 66 65 63 74 73 sty side effects
ab490 20 6f 66 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 of.** malloc fa
ab4a0 69 6c 75 72 65 20 77 68 65 6e 20 53 51 4c 69 74 ilure when SQLit
ab4b0 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 72 65 63 e is invoked rec
ab4c0 75 72 73 69 76 65 6c 79 20 62 79 20 61 20 76 69 ursively by a vi
ab4d0 72 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a 20 rtual table .**
ab4e0 6d 65 74 68 6f 64 20 66 75 6e 63 74 69 6f 6e 2e method function.
ab4f0 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 20 .*/.struct Vdbe
ab500 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b {. sqlite3 *db;
ab510 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
ab520 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
ab530 65 63 74 69 6f 6e 20 74 68 61 74 20 6f 77 6e 73 ection that owns
ab540 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 this statement
ab550 2a 2f 0a 20 20 56 64 62 65 20 2a 70 50 72 65 76 */. Vdbe *pPrev
ab560 2c 2a 70 4e 65 78 74 3b 20 20 20 20 20 2f 2a 20 ,*pNext; /*
ab570 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 56 Linked list of V
ab580 44 42 45 73 20 77 69 74 68 20 74 68 65 20 73 61 DBEs with the sa
ab590 6d 65 20 56 64 62 65 2e 64 62 20 2a 2f 0a 20 20 me Vdbe.db */.
ab5a0 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20 20 int nOp;
ab5b0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
ab5c0 72 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e r of instruction
ab5d0 73 20 69 6e 20 74 68 65 20 70 72 6f 67 72 61 6d s in the program
ab5e0 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 41 6c 6c */. int nOpAll
ab5f0 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a oc; /*
ab600 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 Number of slots
ab610 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 allocated for a
ab620 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70 20 2a 61 4f Op[] */. Op *aO
ab630 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
ab640 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 68 6f /* Space to ho
ab650 6c 64 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d ld the virtual m
ab660 61 63 68 69 6e 65 27 73 20 70 72 6f 67 72 61 6d achine's program
ab670 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c */. int nLabel
ab680 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
ab690 20 4e 75 6d 62 65 72 20 6f 66 20 6c 61 62 65 6c Number of label
ab6a0 73 20 75 73 65 64 20 2a 2f 0a 20 20 69 6e 74 20 s used */. int
ab6b0 6e 4c 61 62 65 6c 41 6c 6c 6f 63 3b 20 20 20 20 nLabelAlloc;
ab6c0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
ab6d0 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 slots allocated
ab6e0 20 69 6e 20 61 4c 61 62 65 6c 5b 5d 20 2a 2f 0a in aLabel[] */.
ab6f0 20 20 69 6e 74 20 2a 61 4c 61 62 65 6c 3b 20 20 int *aLabel;
ab700 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 /* Spa
ab710 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6c ce to hold the l
ab720 61 62 65 6c 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a abels */. Mem *
ab730 2a 61 70 41 72 67 3b 20 20 20 20 20 20 20 20 20 *apArg;
ab740 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20 /* Arguments
ab750 74 6f 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 to currently exe
ab760 63 75 74 69 6e 67 20 75 73 65 72 20 66 75 6e 63 cuting user func
ab770 74 69 6f 6e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 tion */. Mem *a
ab780 43 6f 6c 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 ColName;
ab790 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 /* Column name
ab7a0 73 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 s to return */.
ab7b0 20 4d 65 6d 20 2a 70 52 65 73 75 6c 74 53 65 74 Mem *pResultSet
ab7c0 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e ; /* Poin
ab7d0 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 ter to an array
ab7e0 6f 66 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 of results */.
ab7f0 75 31 36 20 6e 52 65 73 43 6f 6c 75 6d 6e 3b 20 u16 nResColumn;
ab800 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
ab810 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
ab820 6f 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20 72 one row of the r
ab830 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 75 esult set */. u
ab840 31 36 20 6e 43 75 72 73 6f 72 3b 20 20 20 20 20 16 nCursor;
ab850 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
ab860 20 6f 66 20 73 6c 6f 74 73 20 69 6e 20 61 70 43 of slots in apC
ab870 73 72 5b 5d 20 2a 2f 0a 20 20 56 64 62 65 43 75 sr[] */. VdbeCu
ab880 72 73 6f 72 20 2a 2a 61 70 43 73 72 3b 20 20 20 rsor **apCsr;
ab890 20 20 2f 2a 20 4f 6e 65 20 65 6c 65 6d 65 6e 74 /* One element
ab8a0 20 6f 66 20 74 68 69 73 20 61 72 72 61 79 20 66 of this array f
ab8b0 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 63 75 72 or each open cur
ab8c0 73 6f 72 20 2a 2f 0a 20 20 75 38 20 65 72 72 6f sor */. u8 erro
ab8d0 72 41 63 74 69 6f 6e 3b 20 20 20 20 20 20 20 20 rAction;
ab8e0 20 2f 2a 20 52 65 63 6f 76 65 72 79 20 61 63 74 /* Recovery act
ab8f0 69 6f 6e 20 74 6f 20 64 6f 20 69 6e 20 63 61 73 ion to do in cas
ab900 65 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 2a 2f e of an error */
ab910 0a 20 20 75 38 20 6f 6b 56 61 72 3b 20 20 20 20 . u8 okVar;
ab920 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
ab930 75 65 20 69 66 20 61 7a 56 61 72 5b 5d 20 68 61 ue if azVar[] ha
ab940 73 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a s been initializ
ab950 65 64 20 2a 2f 0a 20 20 75 31 36 20 6e 56 61 72 ed */. u16 nVar
ab960 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ab970 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
ab980 72 69 65 73 20 69 6e 20 61 56 61 72 5b 5d 20 2a ries in aVar[] *
ab990 2f 0a 20 20 4d 65 6d 20 2a 61 56 61 72 3b 20 20 /. Mem *aVar;
ab9a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 /* V
ab9b0 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 4f 50 alues for the OP
ab9c0 5f 56 61 72 69 61 62 6c 65 20 6f 70 63 6f 64 65 _Variable opcode
ab9d0 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a . */. char **az
ab9e0 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f Var; /
ab9f0 2a 20 4e 61 6d 65 20 6f 66 20 76 61 72 69 61 62 * Name of variab
aba00 6c 65 73 20 2a 2f 0a 20 20 75 33 32 20 6d 61 67 les */. u32 mag
aba10 69 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ic;
aba20 20 2f 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65 72 /* Magic number
aba30 20 66 6f 72 20 73 61 6e 69 74 79 20 63 68 65 63 for sanity chec
aba40 6b 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d king */. int nM
aba50 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 em;
aba60 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d /* Number of m
aba70 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 emory locations
aba80 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 currently alloca
aba90 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d ted */. Mem *aM
abaa0 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 em;
abab0 20 2f 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 6c /* The memory l
abac0 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 75 33 ocations */. u3
abad0 32 20 63 61 63 68 65 43 74 72 3b 20 20 20 20 20 2 cacheCtr;
abae0 20 20 20 20 20 20 2f 2a 20 56 64 62 65 43 75 72 /* VdbeCur
abaf0 73 6f 72 20 72 6f 77 20 63 61 63 68 65 20 67 65 sor row cache ge
abb00 6e 65 72 61 74 69 6f 6e 20 63 6f 75 6e 74 65 72 neration counter
abb10 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 */. int pc;
abb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
abb30 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 The program cou
abb40 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63 nter */. int rc
abb50 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
abb60 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 /* Value to re
abb70 74 75 72 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a turn */. char *
abb80 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 20 zErrMsg;
abb90 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 /* Error messa
abba0 67 65 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 ge written here
abbb0 2a 2f 0a 20 20 75 38 20 65 78 70 6c 61 69 6e 3b */. u8 explain;
abbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
abbd0 54 72 75 65 20 69 66 20 45 58 50 4c 41 49 4e 20 True if EXPLAIN
abbe0 70 72 65 73 65 6e 74 20 6f 6e 20 53 51 4c 20 63 present on SQL c
abbf0 6f 6d 6d 61 6e 64 20 2a 2f 0a 20 20 75 38 20 63 ommand */. u8 c
abc00 68 61 6e 67 65 43 6e 74 4f 6e 3b 20 20 20 20 20 hangeCntOn;
abc10 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 75 /* True to u
abc20 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 pdate the change
abc30 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 -counter */. u8
abc40 20 65 78 70 69 72 65 64 3b 20 20 20 20 20 20 20 expired;
abc50 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
abc60 20 74 68 65 20 56 4d 20 6e 65 65 64 73 20 74 6f the VM needs to
abc70 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20 2a be recompiled *
abc80 2f 0a 20 20 75 38 20 6d 69 6e 57 72 69 74 65 46 /. u8 minWriteF
abc90 69 6c 65 46 6f 72 6d 61 74 3b 20 20 2f 2a 20 4d ileFormat; /* M
abca0 69 6e 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d inimum file form
abcb0 61 74 20 66 6f 72 20 77 72 69 74 61 62 6c 65 20 at for writable
abcc0 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 2a database files *
abcd0 2f 0a 20 20 75 38 20 69 6e 56 74 61 62 4d 65 74 /. u8 inVtabMet
abce0 68 6f 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 hod; /* S
abcf0 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 ee comments abov
abd00 65 20 2a 2f 0a 20 20 75 38 20 75 73 65 73 53 74 e */. u8 usesSt
abd10 6d 74 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 2f mtJournal; /
abd20 2a 20 54 72 75 65 20 69 66 20 75 73 65 73 20 61 * True if uses a
abd30 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e statement journ
abd40 61 6c 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f al */. u8 readO
abd50 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 nly;
abd60 2f 2a 20 54 72 75 65 20 66 6f 72 20 72 65 61 64 /* True for read
abd70 2d 6f 6e 6c 79 20 73 74 61 74 65 6d 65 6e 74 73 -only statements
abd80 20 2a 2f 0a 20 20 75 38 20 69 73 50 72 65 70 61 */. u8 isPrepa
abd90 72 65 56 32 3b 20 20 20 20 20 20 20 20 20 2f 2a reV2; /*
abda0 20 54 72 75 65 20 69 66 20 70 72 65 70 61 72 65 True if prepare
abdb0 64 20 77 69 74 68 20 70 72 65 70 61 72 65 5f 76 d with prepare_v
abdc0 32 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 2() */. int nCh
abdd0 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 ange;
abde0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 62 /* Number of db
abdf0 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 73 69 changes made si
abe00 6e 63 65 20 6c 61 73 74 20 72 65 73 65 74 20 2a nce last reset *
abe10 2f 0a 20 20 69 6e 74 20 62 74 72 65 65 4d 61 73 /. int btreeMas
abe20 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 k; /* B
abe30 69 74 6d 61 73 6b 20 6f 66 20 64 62 2d 3e 61 44 itmask of db->aD
abe40 62 5b 5d 20 65 6e 74 72 69 65 73 20 72 65 66 65 b[] entries refe
abe50 72 65 6e 63 65 64 20 2a 2f 0a 20 20 69 36 34 20 renced */. i64
abe60 73 74 61 72 74 54 69 6d 65 3b 20 20 20 20 20 20 startTime;
abe70 20 20 20 20 2f 2a 20 54 69 6d 65 20 77 68 65 6e /* Time when
abe80 20 71 75 65 72 79 20 73 74 61 72 74 65 64 20 2d query started -
abe90 20 75 73 65 64 20 66 6f 72 20 70 72 6f 66 69 6c used for profil
abea0 69 6e 67 20 2a 2f 0a 20 20 42 74 72 65 65 4d 75 ing */. BtreeMu
abeb0 74 65 78 41 72 72 61 79 20 61 4d 75 74 65 78 3b texArray aMutex;
abec0 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 20 /* An array of
abed0 42 74 72 65 65 20 75 73 65 64 20 68 65 72 65 20 Btree used here
abee0 61 6e 64 20 6e 65 65 64 69 6e 67 20 6c 6f 63 6b and needing lock
abef0 73 20 2a 2f 0a 20 20 69 6e 74 20 61 43 6f 75 6e s */. int aCoun
abf00 74 65 72 5b 32 5d 3b 20 20 20 20 20 20 20 20 2f ter[2]; /
abf10 2a 20 43 6f 75 6e 74 65 72 73 20 75 73 65 64 20 * Counters used
abf20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f by sqlite3_stmt_
abf30 73 74 61 74 75 73 28 29 20 2a 2f 0a 20 20 63 68 status() */. ch
abf40 61 72 20 2a 7a 53 71 6c 3b 20 20 20 20 20 20 20 ar *zSql;
abf50 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 /* Text of
abf60 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 the SQL stateme
abf70 6e 74 20 74 68 61 74 20 67 65 6e 65 72 61 74 65 nt that generate
abf80 64 20 74 68 69 73 20 2a 2f 0a 20 20 76 6f 69 64 d this */. void
abf90 20 2a 70 46 72 65 65 3b 20 20 20 20 20 20 20 20 *pFree;
abfa0 20 20 20 20 2f 2a 20 46 72 65 65 20 74 68 69 73 /* Free this
abfb0 20 77 68 65 6e 20 64 65 6c 65 74 69 6e 67 20 74 when deleting t
abfc0 68 65 20 76 64 62 65 20 2a 2f 0a 20 20 69 36 34 he vdbe */. i64
abfd0 20 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 3b 20 nFkConstraint;
abfe0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
abff0 66 20 69 6d 6d 2e 20 46 4b 20 63 6f 6e 73 74 72 f imm. FK constr
ac000 61 69 6e 74 73 20 74 68 69 73 20 56 4d 20 2a 2f aints this VM */
ac010 0a 20 20 69 36 34 20 6e 53 74 6d 74 44 65 66 43 . i64 nStmtDefC
ac020 6f 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 ons; /* Nu
ac030 6d 62 65 72 20 6f 66 20 64 65 66 2e 20 63 6f 6e mber of def. con
ac040 73 74 72 61 69 6e 74 73 20 77 68 65 6e 20 73 74 straints when st
ac050 6d 74 20 73 74 61 72 74 65 64 20 2a 2f 0a 20 20 mt started */.
ac060 69 6e 74 20 69 53 74 61 74 65 6d 65 6e 74 3b 20 int iStatement;
ac070 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 /* State
ac080 6d 65 6e 74 20 6e 75 6d 62 65 72 20 28 6f 72 20 ment number (or
ac090 30 20 69 66 20 68 61 73 20 6e 6f 74 20 6f 70 65 0 if has not ope
ac0a0 6e 65 64 20 73 74 6d 74 29 20 2a 2f 0a 23 69 66 ned stmt) */.#if
ac0b0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
ac0c0 0a 20 20 46 49 4c 45 20 2a 74 72 61 63 65 3b 20 . FILE *trace;
ac0d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 /* Wr
ac0e0 69 74 65 20 61 6e 20 65 78 65 63 75 74 69 6f 6e ite an execution
ac0f0 20 74 72 61 63 65 20 68 65 72 65 2c 20 69 66 20 trace here, if
ac100 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a 23 65 6e 64 not NULL */.#end
ac110 69 66 0a 20 20 56 64 62 65 46 72 61 6d 65 20 2a if. VdbeFrame *
ac120 70 46 72 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 pFrame; /*
ac130 50 61 72 65 6e 74 20 66 72 61 6d 65 20 2a 2f 0a Parent frame */.
ac140 20 20 69 6e 74 20 6e 46 72 61 6d 65 3b 20 20 20 int nFrame;
ac150 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
ac160 62 65 72 20 6f 66 20 66 72 61 6d 65 73 20 69 6e ber of frames in
ac170 20 70 46 72 61 6d 65 20 6c 69 73 74 20 2a 2f 0a pFrame list */.
ac180 20 20 75 33 32 20 65 78 70 6d 61 73 6b 3b 20 20 u32 expmask;
ac190 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69 6e /* Bin
ac1a0 64 69 6e 67 20 74 6f 20 74 68 65 73 65 20 76 61 ding to these va
ac1b0 72 73 20 69 6e 76 61 6c 69 64 61 74 65 73 20 56 rs invalidates V
ac1c0 4d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 M */.};../*.** T
ac1d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 he following are
ac1e0 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 allowed values
ac1f0 66 6f 72 20 56 64 62 65 2e 6d 61 67 69 63 0a 2a for Vdbe.magic.*
ac200 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d /.#define VDBE_M
ac210 41 47 49 43 5f 49 4e 49 54 20 20 20 20 20 30 78 AGIC_INIT 0x
ac220 32 36 62 63 65 61 61 35 20 20 20 20 2f 2a 20 42 26bceaa5 /* B
ac230 75 69 6c 64 69 6e 67 20 61 20 56 44 42 45 20 70 uilding a VDBE p
ac240 72 6f 67 72 61 6d 20 2a 2f 0a 23 64 65 66 69 6e rogram */.#defin
ac250 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e e VDBE_MAGIC_RUN
ac260 20 20 20 20 20 20 30 78 62 64 66 32 30 64 61 33 0xbdf20da3
ac270 20 20 20 20 2f 2a 20 56 44 42 45 20 69 73 20 72 /* VDBE is r
ac280 65 61 64 79 20 74 6f 20 65 78 65 63 75 74 65 20 eady to execute
ac290 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f */.#define VDBE_
ac2a0 4d 41 47 49 43 5f 48 41 4c 54 20 20 20 20 20 30 MAGIC_HALT 0
ac2b0 78 35 31 39 63 32 39 37 33 20 20 20 20 2f 2a 20 x519c2973 /*
ac2c0 56 44 42 45 20 68 61 73 20 63 6f 6d 70 6c 65 74 VDBE has complet
ac2d0 65 64 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a ed execution */.
ac2e0 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 #define VDBE_MAG
ac2f0 49 43 5f 44 45 41 44 20 20 20 20 20 30 78 62 36 IC_DEAD 0xb6
ac300 30 36 63 33 63 38 20 20 20 20 2f 2a 20 54 68 65 06c3c8 /* The
ac310 20 56 44 42 45 20 68 61 73 20 62 65 65 6e 20 64 VDBE has been d
ac320 65 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 2f eallocated */../
ac330 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 70 72 *.** Function pr
ac340 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 4c 49 ototypes.*/.SQLI
ac350 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
ac360 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 sqlite3VdbeFreeC
ac370 75 72 73 6f 72 28 56 64 62 65 20 2a 2c 20 56 64 ursor(Vdbe *, Vd
ac380 62 65 43 75 72 73 6f 72 2a 29 3b 0a 76 6f 69 64 beCursor*);.void
ac390 20 73 71 6c 69 74 65 56 64 62 65 50 6f 70 53 74 sqliteVdbePopSt
ac3a0 61 63 6b 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a ack(Vdbe*,int);.
ac3b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
ac3c0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 nt sqlite3VdbeCu
ac3d0 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 43 rsorMoveto(VdbeC
ac3e0 75 72 73 6f 72 2a 29 3b 0a 23 69 66 20 64 65 66 ursor*);.#if def
ac3f0 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
ac400 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 G) || defined(VD
ac410 42 45 5f 50 52 4f 46 49 4c 45 29 0a 53 51 4c 49 BE_PROFILE).SQLI
ac420 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
ac430 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 sqlite3VdbePrint
ac440 4f 70 28 46 49 4c 45 2a 2c 20 69 6e 74 2c 20 4f Op(FILE*, int, O
ac450 70 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 p*);.#endif.SQLI
ac460 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 TE_PRIVATE u32 s
ac470 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
ac480 54 79 70 65 4c 65 6e 28 75 33 32 29 3b 0a 53 51 TypeLen(u32);.SQ
ac490 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 LITE_PRIVATE u32
ac4a0 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
ac4b0 61 6c 54 79 70 65 28 4d 65 6d 2a 2c 20 69 6e 74 alType(Mem*, int
ac4c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
ac4d0 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 E u32 sqlite3Vdb
ac4e0 65 53 65 72 69 61 6c 50 75 74 28 75 6e 73 69 67 eSerialPut(unsig
ac4f0 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 ned char*, int,
ac500 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 Mem*, int);.SQLI
ac510 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 TE_PRIVATE u32 s
ac520 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
ac530 47 65 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e Get(const unsign
ac540 65 64 20 63 68 61 72 2a 2c 20 75 33 32 2c 20 4d ed char*, u32, M
ac550 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 em*);.SQLITE_PRI
ac560 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
ac570 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 3VdbeDeleteAuxDa
ac580 74 61 28 56 64 62 65 46 75 6e 63 2a 2c 20 69 6e ta(VdbeFunc*, in
ac590 74 29 3b 0a 0a 69 6e 74 20 73 71 6c 69 74 65 32 t);..int sqlite2
ac5a0 42 74 72 65 65 4b 65 79 43 6f 6d 70 61 72 65 28 BtreeKeyCompare(
ac5b0 42 74 43 75 72 73 6f 72 20 2a 2c 20 63 6f 6e 73 BtCursor *, cons
ac5c0 74 20 76 6f 69 64 20 2a 2c 20 69 6e 74 2c 20 69 t void *, int, i
ac5d0 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 nt, int *);.SQLI
ac5e0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
ac5f0 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79 qlite3VdbeIdxKey
ac600 43 6f 6d 70 61 72 65 28 56 64 62 65 43 75 72 73 Compare(VdbeCurs
ac610 6f 72 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f or*,UnpackedReco
ac620 72 64 2a 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 rd*,int*);.SQLIT
ac630 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
ac640 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 lite3VdbeIdxRowi
ac650 64 28 73 71 6c 69 74 65 33 2a 2c 20 42 74 43 75 d(sqlite3*, BtCu
ac660 72 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29 3b 0a rsor *, i64 *);.
ac670 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
ac680 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d nt sqlite3MemCom
ac690 70 61 72 65 28 63 6f 6e 73 74 20 4d 65 6d 2a 2c pare(const Mem*,
ac6a0 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e const Mem*, con
ac6b0 73 74 20 43 6f 6c 6c 53 65 71 2a 29 3b 0a 53 51 st CollSeq*);.SQ
ac6c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
ac6d0 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 sqlite3VdbeExec
ac6e0 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f (Vdbe*);.SQLITE_
ac6f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
ac700 74 65 33 56 64 62 65 4c 69 73 74 28 56 64 62 65 te3VdbeList(Vdbe
ac710 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
ac720 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
ac730 62 65 48 61 6c 74 28 56 64 62 65 2a 29 3b 0a 53 beHalt(Vdbe*);.S
ac740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
ac750 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 t sqlite3VdbeCha
ac760 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d 20 ngeEncoding(Mem
ac770 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
ac780 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
ac790 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 te3VdbeMemTooBig
ac7a0 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 (Mem*);.SQLITE_P
ac7b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
ac7c0 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 4d 65 e3VdbeMemCopy(Me
ac7d0 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 29 3b m*, const Mem*);
ac7e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ac7f0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
ac800 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d MemShallowCopy(M
ac810 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c em*, const Mem*,
ac820 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
ac830 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
ac840 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 4d 65 e3VdbeMemMove(Me
ac850 6d 2a 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 m*, Mem*);.SQLIT
ac860 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
ac870 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 lite3VdbeMemNulT
ac880 65 72 6d 69 6e 61 74 65 28 4d 65 6d 2a 29 3b 0a erminate(Mem*);.
ac890 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
ac8a0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
ac8b0 6d 53 65 74 53 74 72 28 4d 65 6d 2a 2c 20 63 6f mSetStr(Mem*, co
ac8c0 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 nst char*, int,
ac8d0 75 38 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 u8, void(*)(void
ac8e0 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *));.SQLITE_PRIV
ac8f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
ac900 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 VdbeMemSetInt64(
ac910 4d 65 6d 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 Mem*, i64);.SQLI
ac920 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
ac930 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
ac940 74 44 6f 75 62 6c 65 28 4d 65 6d 2a 2c 20 64 6f tDouble(Mem*, do
ac950 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 uble);.SQLITE_PR
ac960 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
ac970 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c e3VdbeMemSetNull
ac980 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 (Mem*);.SQLITE_P
ac990 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
ac9a0 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 te3VdbeMemSetZer
ac9b0 6f 42 6c 6f 62 28 4d 65 6d 2a 2c 69 6e 74 29 3b oBlob(Mem*,int);
ac9c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ac9d0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
ac9e0 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 4d 65 6d MemSetRowSet(Mem
ac9f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
aca00 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
aca10 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 beMemMakeWriteab
aca20 6c 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 le(Mem*);.SQLITE
aca30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
aca40 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e ite3VdbeMemStrin
aca50 67 69 66 79 28 4d 65 6d 2a 2c 20 69 6e 74 29 3b gify(Mem*, int);
aca60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
aca70 69 36 34 20 73 71 6c 69 74 65 33 56 64 62 65 49 i64 sqlite3VdbeI
aca80 6e 74 56 61 6c 75 65 28 4d 65 6d 2a 29 3b 0a 53 ntValue(Mem*);.S
aca90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
acaa0 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
acab0 49 6e 74 65 67 65 72 69 66 79 28 4d 65 6d 2a 29 Integerify(Mem*)
acac0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
acad0 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 56 double sqlite3V
acae0 64 62 65 52 65 61 6c 56 61 6c 75 65 28 4d 65 6d dbeRealValue(Mem
acaf0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
acb00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
acb10 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 dbeIntegerAffini
acb20 74 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 ty(Mem*);.SQLITE
acb30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
acb40 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 ite3VdbeMemReali
acb50 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 fy(Mem*);.SQLITE
acb60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
acb70 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 ite3VdbeMemNumer
acb80 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 ify(Mem*);.SQLIT
acb90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
acba0 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d lite3VdbeMemFrom
acbb0 42 74 72 65 65 28 42 74 43 75 72 73 6f 72 2a 2c Btree(BtCursor*,
acbc0 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 4d 65 6d 2a int,int,int,Mem*
acbd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
acbe0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
acbf0 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d 65 6d beMemRelease(Mem
acc00 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 *p);.SQLITE_PRI
acc10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
acc20 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 3VdbeMemReleaseE
acc30 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 29 3b xternal(Mem *p);
acc40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
acc50 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
acc60 65 6d 46 69 6e 61 6c 69 7a 65 28 4d 65 6d 2a 2c emFinalize(Mem*,
acc70 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c 49 FuncDef*);.SQLI
acc80 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
acc90 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4f 70 char *sqlite3Op
acca0 63 6f 64 65 4e 61 6d 65 28 69 6e 74 29 3b 0a 53 codeName(int);.S
accb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
accc0 74 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63 t sqlite3VdbeOpc
accd0 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79 28 69 odeHasProperty(i
acce0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
accf0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
acd00 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
acd10 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e Mem *pMem, int n
acd20 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65 29 3b , int preserve);
acd30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
acd40 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 int sqlite3VdbeC
acd50 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 56 64 loseStatement(Vd
acd60 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 be *, int);.SQLI
acd70 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
acd80 73 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65 sqlite3VdbeFrame
acd90 44 65 6c 65 74 65 28 56 64 62 65 46 72 61 6d 65 Delete(VdbeFrame
acda0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
acdb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
acdc0 62 65 46 72 61 6d 65 52 65 73 74 6f 72 65 28 56 beFrameRestore(V
acdd0 64 62 65 46 72 61 6d 65 20 2a 29 3b 0a 53 51 4c dbeFrame *);.SQL
acde0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
acdf0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
ace00 74 6f 72 65 54 79 70 65 28 4d 65 6d 20 2a 70 4d toreType(Mem *pM
ace10 65 6d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 em);..#ifndef SQ
ace20 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 LITE_OMIT_FOREIG
ace30 4e 5f 4b 45 59 0a 53 51 4c 49 54 45 5f 50 52 49 N_KEY.SQLITE_PRI
ace40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
ace50 56 64 62 65 43 68 65 63 6b 46 6b 28 56 64 62 65 VdbeCheckFk(Vdbe
ace60 20 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a *, int);.#else.
ace70 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
ace80 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c 69 29 VdbeCheckFk(p,i)
ace90 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 0.#endif..#ifnd
acea0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
aceb0 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 HARED_CACHE.SQLI
acec0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
aced0 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 sqlite3VdbeMutex
acee0 41 72 72 61 79 45 6e 74 65 72 28 56 64 62 65 20 ArrayEnter(Vdbe
acef0 2a 70 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 *p);.#else.# def
acf00 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 4d ine sqlite3VdbeM
acf10 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 70 utexArrayEnter(p
acf20 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 ).#endif..SQLITE
acf30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
acf40 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 ite3VdbeMemTrans
acf50 6c 61 74 65 28 4d 65 6d 2a 2c 20 75 38 29 3b 0a late(Mem*, u8);.
acf60 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
acf70 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
acf80 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
acf90 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 56 64 3VdbePrintSql(Vd
acfa0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 be*);.SQLITE_PRI
acfb0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
acfc0 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 te3VdbeMemPretty
acfd0 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c Print(Mem *pMem,
acfe0 20 63 68 61 72 20 2a 7a 42 75 66 29 3b 0a 23 65 char *zBuf);.#e
acff0 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 ndif.SQLITE_PRIV
ad000 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
ad010 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 dbeMemHandleBom(
ad020 4d 65 6d 20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66 Mem *pMem);..#if
ad030 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
ad040 5f 49 4e 43 52 42 4c 4f 42 0a 53 51 4c 49 54 45 _INCRBLOB.SQLITE
ad050 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
ad060 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 qlite3VdbeMemExp
ad070 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a 29 3b 0a andBlob(Mem *);.
ad080 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 #else. #define
ad090 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 sqlite3VdbeMemEx
ad0a0 70 61 6e 64 42 6c 6f 62 28 78 29 20 53 51 4c 49 pandBlob(x) SQLI
ad0b0 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 23 65 TE_OK.#endif..#e
ad0c0 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 ndif /* !defined
ad0d0 28 5f 56 44 42 45 49 4e 54 5f 48 5f 29 20 2a 2f (_VDBEINT_H_) */
ad0e0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
ad0f0 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 49 6e 74 * End of vdbeInt
ad100 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
ad110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ad120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ad130 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
ad140 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
ad150 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
ad160 6e 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a n utf.c ********
ad170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ad180 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
ad190 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 2f E_AMALGAMATION./
ad1a0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
ad1b0 6e 67 20 63 6f 6e 73 74 61 6e 74 20 76 61 6c 75 ng constant valu
ad1c0 65 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 e is used by the
ad1d0 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 SQLITE_BIGENDIA
ad1e0 4e 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f N and.** SQLITE_
ad1f0 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 6d 61 63 LITTLEENDIAN mac
ad200 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ros..*/.SQLITE_P
ad210 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 RIVATE const int
ad220 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b sqlite3one = 1;
ad230 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
ad240 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 2a E_AMALGAMATION *
ad250 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f /../*.** This lo
ad260 6f 6b 75 70 20 74 61 62 6c 65 20 69 73 20 75 73 okup table is us
ad270 65 64 20 74 6f 20 68 65 6c 70 20 64 65 63 6f 64 ed to help decod
ad280 65 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 e the first byte
ad290 20 6f 66 0a 2a 2a 20 61 20 6d 75 6c 74 69 2d 62 of.** a multi-b
ad2a0 79 74 65 20 55 54 46 38 20 63 68 61 72 61 63 74 yte UTF8 charact
ad2b0 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f er..*/.static co
ad2c0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
ad2d0 72 20 73 71 6c 69 74 65 33 55 74 66 38 54 72 61 r sqlite3Utf8Tra
ad2e0 6e 73 31 5b 5d 20 3d 20 7b 0a 20 20 30 78 30 30 ns1[] = {. 0x00
ad2f0 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 , 0x01, 0x02, 0x
ad300 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 03, 0x04, 0x05,
ad310 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 0x06, 0x07,. 0x
ad320 30 38 2c 20 30 78 30 39 2c 20 30 78 30 61 2c 20 08, 0x09, 0x0a,
ad330 30 78 30 62 2c 20 30 78 30 63 2c 20 30 78 30 64 0x0b, 0x0c, 0x0d
ad340 2c 20 30 78 30 65 2c 20 30 78 30 66 2c 0a 20 20 , 0x0e, 0x0f,.
ad350 30 78 31 30 2c 20 30 78 31 31 2c 20 30 78 31 32 0x10, 0x11, 0x12
ad360 2c 20 30 78 31 33 2c 20 30 78 31 34 2c 20 30 78 , 0x13, 0x14, 0x
ad370 31 35 2c 20 30 78 31 36 2c 20 30 78 31 37 2c 0a 15, 0x16, 0x17,.
ad380 20 20 30 78 31 38 2c 20 30 78 31 39 2c 20 30 78 0x18, 0x19, 0x
ad390 31 61 2c 20 30 78 31 62 2c 20 30 78 31 63 2c 20 1a, 0x1b, 0x1c,
ad3a0 30 78 31 64 2c 20 30 78 31 65 2c 20 30 78 31 66 0x1d, 0x1e, 0x1f
ad3b0 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 ,. 0x00, 0x01,
ad3c0 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 0x02, 0x03, 0x04
ad3d0 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 , 0x05, 0x06, 0x
ad3e0 30 37 2c 0a 20 20 30 78 30 38 2c 20 30 78 30 39 07,. 0x08, 0x09
ad3f0 2c 20 30 78 30 61 2c 20 30 78 30 62 2c 20 30 78 , 0x0a, 0x0b, 0x
ad400 30 63 2c 20 30 78 30 64 2c 20 30 78 30 65 2c 20 0c, 0x0d, 0x0e,
ad410 30 78 30 66 2c 0a 20 20 30 78 30 30 2c 20 30 78 0x0f,. 0x00, 0x
ad420 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 01, 0x02, 0x03,
ad430 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 0x04, 0x05, 0x06
ad440 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 30 2c 20 , 0x07,. 0x00,
ad450 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 0x01, 0x02, 0x03
ad460 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 , 0x00, 0x01, 0x
ad470 30 30 2c 20 30 78 30 30 2c 0a 7d 3b 0a 0a 0a 23 00, 0x00,.};...#
ad480 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 define WRITE_UTF
ad490 38 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 8(zOut, c) {
ad4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad4b0 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c \. if( c<
ad4c0 30 78 30 30 30 38 30 20 29 7b 20 20 20 20 20 20 0x00080 ){
ad4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ad4f0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 . *zOut++ = (
ad500 75 38 29 28 63 26 30 78 46 46 29 3b 20 20 20 20 u8)(c&0xFF);
ad510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad520 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 \. }
ad530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad560 20 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c \. else if( c<
ad570 30 78 30 30 38 30 30 20 29 7b 20 20 20 20 20 20 0x00800 ){
ad580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad590 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
ad5a0 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 43 30 20 2b *zOut++ = 0xC0 +
ad5b0 20 28 75 38 29 28 28 63 3e 3e 36 29 26 30 78 31 (u8)((c>>6)&0x1
ad5c0 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 F);
ad5d0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
ad5e0 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 63 = 0x80 + (u8)(c
ad5f0 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 & 0x3F);
ad600 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ad610 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }
ad620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad640 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20 69 66 \. else if
ad650 28 20 63 3c 30 78 31 30 30 30 30 20 29 7b 20 20 ( c<0x10000 ){
ad660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ad680 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 *zOut++ = 0x
ad690 45 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 E0 + (u8)((c>>12
ad6a0 29 26 30 78 30 46 29 3b 20 20 20 20 20 20 20 20 )&0x0F);
ad6b0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f \. *zO
ad6c0 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 ut++ = 0x80 + (u
ad6d0 38 29 28 28 63 3e 3e 36 29 20 26 20 30 78 33 46 8)((c>>6) & 0x3F
ad6e0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
ad6f0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
ad700 30 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 0x80 + (u8)(c &
ad710 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 0x3F);
ad720 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c \. }el
ad730 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 se{
ad740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad760 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
ad770 3d 20 30 78 46 30 20 2b 20 28 75 38 29 28 28 63 = 0xF0 + (u8)((c
ad780 3e 3e 31 38 29 20 26 20 30 78 30 37 29 3b 20 20 >>18) & 0x07);
ad790 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
ad7a0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 *zOut++ = 0x80
ad7b0 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 29 20 26 + (u8)((c>>12) &
ad7c0 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 0x3F);
ad7d0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
ad7e0 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 + = 0x80 + (u8)(
ad7f0 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 20 (c>>6) & 0x3F);
ad800 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ad810 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 *zOut++ = 0x8
ad820 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 33 0 + (u8)(c & 0x3
ad830 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 F);
ad840 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 \. }
ad850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ad880 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54 .}..#define WRIT
ad890 45 5f 55 54 46 31 36 4c 45 28 7a 4f 75 74 2c 20 E_UTF16LE(zOut,
ad8a0 63 29 20 7b 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 20
ad8c0 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 \. if(
ad8d0 63 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 20 20 c<=0xFFFF ){
ad8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ad910 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 *zOut++ = (u
ad920 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 8)(c&0x00FF);
ad930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad950 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
ad960 2b 20 3d 20 28 75 38 29 28 28 63 3e 3e 38 29 26 + = (u8)((c>>8)&
ad970 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 0x00FF);
ad980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad990 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 \. }e
ad9a0 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 lse{
ad9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad9e0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
ad9f0 28 75 38 29 28 28 28 63 3e 3e 31 30 29 26 30 78 (u8)(((c>>10)&0x
ada00 30 30 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31 003F) + (((c-0x1
ada10 30 30 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43 0000)>>10)&0x00C
ada20 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 0)); \. *zOu
ada30 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 t++ = (u8)(0x00D
ada40 38 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 8 + (((c-0x10000
ada50 29 3e 3e 31 38 29 26 30 78 30 33 29 29 3b 20 20 )>>18)&0x03));
ada60 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ada70 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 *zOut++ = (u8)
ada80 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 (c&0x00FF);
ada90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adab0 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
adac0 3d 20 28 75 38 29 28 30 78 30 30 44 43 20 2b 20 = (u8)(0x00DC +
adad0 28 28 63 3e 3e 38 29 26 30 78 30 33 29 29 3b 20 ((c>>8)&0x03));
adae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adaf0 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 \. }
adb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
adb40 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 }..#define WRITE
adb50 5f 55 54 46 31 36 42 45 28 7a 4f 75 74 2c 20 63 _UTF16BE(zOut, c
adb60 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 ) {
adb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb80 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 \. if( c
adb90 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 20 20 20 <=0xFFFF ){
adba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adbb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
adbd0 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 *zOut++ = (u8
adbe0 29 28 28 63 3e 3e 38 29 26 30 78 30 30 46 46 29 )((c>>8)&0x00FF)
adbf0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
adc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc10 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
adc20 20 3d 20 28 75 38 29 28 63 26 30 78 30 30 46 46 = (u8)(c&0x00FF
adc30 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
adc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc50 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c \. }el
adc60 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 se{
adc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
adca0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 . *zOut++ = (
adcb0 75 38 29 28 30 78 30 30 44 38 20 2b 20 28 28 28 u8)(0x00D8 + (((
adcc0 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 29 26 c-0x10000)>>18)&
adcd0 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 0x03));
adce0 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 \. *zOut
adcf0 2b 2b 20 3d 20 28 75 38 29 28 28 28 63 3e 3e 31 ++ = (u8)(((c>>1
add00 30 29 26 30 78 30 30 33 46 29 20 2b 20 28 28 28 0)&0x003F) + (((
add10 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 30 29 26 c-0x10000)>>10)&
add20 30 78 30 30 43 30 29 29 3b 20 20 5c 0a 20 20 20 0x00C0)); \.
add30 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 *zOut++ = (u8)(
add40 30 78 30 30 44 43 20 2b 20 28 28 63 3e 3e 38 29 0x00DC + ((c>>8)
add50 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 &0x03));
add60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add70 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
add80 20 28 75 38 29 28 63 26 30 78 30 30 46 46 29 3b (u8)(c&0x00FF);
add90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
addb0 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 \. }
addc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
addd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
addf0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d \.}
ade00 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55 ..#define READ_U
ade10 54 46 31 36 4c 45 28 7a 49 6e 2c 20 54 45 52 4d TF16LE(zIn, TERM
ade20 2c 20 63 29 7b 20 20 20 20 20 20 20 20 20 20 20 , c){
ade30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade40 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 \. c =
ade50 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 (*zIn++);
ade60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade90 5c 0a 20 20 63 20 2b 3d 20 28 28 2a 7a 49 6e 2b \. c += ((*zIn+
adea0 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 +)<<8);
adeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aded0 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 \. if(
adee0 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30 c>=0xD800 && c<0
adef0 78 45 30 30 30 20 26 26 20 54 45 52 4d 20 29 7b xE000 && TERM ){
adf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf20 5c 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28 \. int c2 = (
adf30 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 *zIn++);
adf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf60 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 32 \. c2
adf70 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 += ((*zIn++)<<8
adf80 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
adf90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adfb0 5c 0a 20 20 20 20 63 20 3d 20 28 63 32 26 30 78 \. c = (c2&0x
adfc0 30 33 46 46 29 20 2b 20 28 28 63 26 30 78 30 30 03FF) + ((c&0x00
adfd0 33 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 3F)<<10) + (((c&
adfe0 30 78 30 33 43 30 29 2b 30 78 30 30 34 30 29 3c 0x03C0)+0x0040)<
adff0 3c 31 30 29 3b 20 20 20 5c 0a 20 20 7d 20 20 20 <10); \. }
ae000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae040 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 \.}..#define REA
ae050 44 5f 55 54 46 31 36 42 45 28 7a 49 6e 2c 20 54 D_UTF16BE(zIn, T
ae060 45 52 4d 2c 20 63 29 7b 20 20 20 20 20 20 20 20 ERM, c){
ae070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae080 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 \. c
ae090 20 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 = ((*zIn++)<<8)
ae0a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ae0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae0d0 20 20 20 5c 0a 20 20 63 20 2b 3d 20 28 2a 7a 49 \. c += (*zI
ae0e0 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 n++);
ae0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae110 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 \. i
ae120 66 28 20 63 3e 3d 30 78 44 38 30 30 20 26 26 20 f( c>=0xD800 &&
ae130 63 3c 30 78 45 30 30 30 20 26 26 20 54 45 52 4d c<0xE000 && TERM
ae140 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 ){
ae150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae160 20 20 20 5c 0a 20 20 20 20 69 6e 74 20 63 32 20 \. int c2
ae170 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b = ((*zIn++)<<8);
ae180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae1a0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
ae1b0 20 63 32 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b c2 += (*zIn++);
ae1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae1f0 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 28 63 32 \. c = (c2
ae200 26 30 78 30 33 46 46 29 20 2b 20 28 28 63 26 30 &0x03FF) + ((c&0
ae210 78 30 30 33 46 29 3c 3c 31 30 29 20 2b 20 28 28 x003F)<<10) + ((
ae220 28 63 26 30 78 30 33 43 30 29 2b 30 78 30 30 34 (c&0x03C0)+0x004
ae230 30 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 20 20 7d 0)<<10); \. }
ae240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae280 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 \.}../*.** Tr
ae290 61 6e 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 anslate a single
ae2a0 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 UTF-8 character
ae2b0 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 75 6e . Return the un
ae2c0 69 63 6f 64 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a icode value..**.
ae2d0 2a 2a 20 44 75 72 69 6e 67 20 74 72 61 6e 73 6c ** During transl
ae2e0 61 74 69 6f 6e 2c 20 61 73 73 75 6d 65 20 74 68 ation, assume th
ae2f0 61 74 20 74 68 65 20 62 79 74 65 20 74 68 61 74 at the byte that
ae300 20 7a 54 65 72 6d 20 70 6f 69 6e 74 73 0a 2a 2a zTerm points.**
ae310 20 69 73 20 61 20 30 78 30 30 2e 0a 2a 2a 0a 2a is a 0x00..**.*
ae320 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65 * Write a pointe
ae330 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 75 6e r to the next un
ae340 72 65 61 64 20 62 79 74 65 20 62 61 63 6b 20 69 read byte back i
ae350 6e 74 6f 20 2a 70 7a 4e 65 78 74 2e 0a 2a 2a 0a nto *pzNext..**.
ae360 2a 2a 20 4e 6f 74 65 73 20 4f 6e 20 49 6e 76 61 ** Notes On Inva
ae370 6c 69 64 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a lid UTF-8:.**.**
ae380 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74 69 6e * This routin
ae390 65 20 6e 65 76 65 72 20 61 6c 6c 6f 77 73 20 61 e never allows a
ae3a0 20 37 2d 62 69 74 20 63 68 61 72 61 63 74 65 72 7-bit character
ae3b0 20 28 30 78 30 30 20 74 68 72 6f 75 67 68 20 30 (0x00 through 0
ae3c0 78 37 66 29 20 74 6f 0a 2a 2a 20 20 20 20 20 62 x7f) to.** b
ae3d0 65 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20 6d e encoded as a m
ae3e0 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63 ulti-byte charac
ae3f0 74 65 72 2e 20 20 41 6e 79 20 6d 75 6c 74 69 2d ter. Any multi-
ae400 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 74 byte character t
ae410 68 61 74 0a 2a 2a 20 20 20 20 20 61 74 74 65 6d hat.** attem
ae420 70 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 pts to encode a
ae430 76 61 6c 75 65 20 62 65 74 77 65 65 6e 20 30 78 value between 0x
ae440 30 30 20 61 6e 64 20 30 78 37 66 20 69 73 20 72 00 and 0x7f is r
ae450 65 6e 64 65 72 65 64 20 61 73 20 30 78 66 66 66 endered as 0xfff
ae460 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 d..**.** * Thi
ae470 73 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 s routine never
ae480 61 6c 6c 6f 77 73 20 61 20 55 54 46 31 36 20 73 allows a UTF16 s
ae490 75 72 72 6f 67 61 74 65 20 76 61 6c 75 65 20 74 urrogate value t
ae4a0 6f 20 62 65 20 65 6e 63 6f 64 65 64 2e 0a 2a 2a o be encoded..**
ae4b0 20 20 20 20 20 49 66 20 61 20 6d 75 6c 74 69 2d If a multi-
ae4c0 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 61 byte character a
ae4d0 74 74 65 6d 70 74 73 20 74 6f 20 65 6e 63 6f 64 ttempts to encod
ae4e0 65 20 61 20 76 61 6c 75 65 20 62 65 74 77 65 65 e a value betwee
ae4f0 6e 0a 2a 2a 20 20 20 20 20 30 78 64 38 30 30 20 n.** 0xd800
ae500 61 6e 64 20 30 78 65 30 30 30 20 74 68 65 6e 20 and 0xe000 then
ae510 69 74 20 69 73 20 72 65 6e 64 65 72 65 64 20 61 it is rendered a
ae520 73 20 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 s 0xfffd..**.**
ae530 20 2a 20 20 42 79 74 65 73 20 69 6e 20 74 68 65 * Bytes in the
ae540 20 72 61 6e 67 65 20 6f 66 20 30 78 38 30 20 74 range of 0x80 t
ae550 68 72 6f 75 67 68 20 30 78 62 66 20 77 68 69 63 hrough 0xbf whic
ae560 68 20 6f 63 63 75 72 20 61 73 20 74 68 65 20 66 h occur as the f
ae570 69 72 73 74 0a 2a 2a 20 20 20 20 20 62 79 74 65 irst.** byte
ae580 20 6f 66 20 61 20 63 68 61 72 61 63 74 65 72 20 of a character
ae590 61 72 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 are interpreted
ae5a0 61 73 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 as single-byte c
ae5b0 68 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 haracters.**
ae5c0 20 61 6e 64 20 72 65 6e 64 65 72 65 64 20 61 73 and rendered as
ae5d0 20 74 68 65 6d 73 65 6c 76 65 73 20 65 76 65 6e themselves even
ae5e0 20 74 68 6f 75 67 68 20 74 68 65 79 20 61 72 65 though they are
ae5f0 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20 technically.**
ae600 20 20 20 20 69 6e 76 61 6c 69 64 20 63 68 61 72 invalid char
ae610 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a acters..**.** *
ae620 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 This routine a
ae630 63 63 65 70 74 73 20 61 6e 20 69 6e 66 69 6e 69 ccepts an infini
ae640 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 te number of dif
ae650 66 65 72 65 6e 74 20 55 54 46 38 20 65 6e 63 6f ferent UTF8 enco
ae660 64 69 6e 67 73 0a 2a 2a 20 20 20 20 20 66 6f 72 dings.** for
ae670 20 75 6e 69 63 6f 64 65 20 76 61 6c 75 65 73 20 unicode values
ae680 30 78 38 30 20 61 6e 64 20 67 72 65 61 74 65 72 0x80 and greater
ae690 2e 20 20 49 74 20 64 6f 20 6e 6f 74 20 63 68 61 . It do not cha
ae6a0 6e 67 65 20 6f 76 65 72 2d 6c 65 6e 67 74 68 0a nge over-length.
ae6b0 2a 2a 20 20 20 20 20 65 6e 63 6f 64 69 6e 67 73 ** encodings
ae6c0 20 74 6f 20 30 78 66 66 66 64 20 61 73 20 73 6f to 0xfffd as so
ae6d0 6d 65 20 73 79 73 74 65 6d 73 20 72 65 63 6f 6d me systems recom
ae6e0 6d 65 6e 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 mend..*/.#define
ae6f0 20 52 45 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20 READ_UTF8(zIn,
ae700 7a 54 65 72 6d 2c 20 63 29 20 20 20 20 20 20 20 zTerm, c)
ae710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae720 20 20 20 20 5c 0a 20 20 63 20 3d 20 2a 28 7a 49 \. c = *(zI
ae730 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 n++);
ae740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae760 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 30 \. if( c>=0xc0
ae770 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 ){
ae780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ae7a0 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 c = sqlite3U
ae7b0 74 66 38 54 72 61 6e 73 31 5b 63 2d 30 78 63 30 tf8Trans1[c-0xc0
ae7c0 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ];
ae7d0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
ae7e0 20 77 68 69 6c 65 28 20 7a 49 6e 21 3d 7a 54 65 while( zIn!=zTe
ae7f0 72 6d 20 26 26 20 28 2a 7a 49 6e 20 26 20 30 78 rm && (*zIn & 0x
ae800 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 20 20 20 c0)==0x80 ){
ae810 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20 \.
ae820 63 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30 78 c = (c<<6) + (0x
ae830 33 66 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 20 3f & *(zIn++));
ae840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae850 20 20 20 20 20 5c 0a 20 20 20 20 7d 20 20 20 20 \. }
ae860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae890 20 20 5c 0a 20 20 20 20 69 66 28 20 63 3c 30 78 \. if( c<0x
ae8a0 38 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 80
ae8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ae8d0 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 . || (c&0
ae8e0 78 46 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38 xFFFFF800)==0xD8
ae8f0 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 00
ae900 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ae910 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 || (c&0xFF
ae920 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20 FFFFFE)==0xFFFE
ae930 29 7b 20 20 63 20 3d 20 30 78 46 46 46 44 3b 20 ){ c = 0xFFFD;
ae940 7d 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 53 } \. }.S
ae950 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
ae960 74 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 t sqlite3Utf8Rea
ae970 64 28 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 d(. const unsig
ae980 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 2c 20 20 ned char *zIn,
ae990 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 /* First by
ae9a0 74 65 20 6f 66 20 55 54 46 2d 38 20 63 68 61 72 te of UTF-8 char
ae9b0 61 63 74 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 acter */. const
ae9c0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
ae9d0 2a 70 7a 4e 65 78 74 20 20 20 20 2f 2a 20 57 72 *pzNext /* Wr
ae9e0 69 74 65 20 66 69 72 73 74 20 62 79 74 65 20 70 ite first byte p
ae9f0 61 73 74 20 55 54 46 2d 38 20 63 68 61 72 20 68 ast UTF-8 char h
aea00 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ere */.){. int
aea10 63 3b 0a 0a 20 20 2f 2a 20 53 61 6d 65 20 61 73 c;.. /* Same as
aea20 20 52 45 41 44 5f 55 54 46 38 28 29 20 61 62 6f READ_UTF8() abo
aea30 76 65 20 62 75 74 20 77 69 74 68 6f 75 74 20 74 ve but without t
aea40 68 65 20 7a 54 65 72 6d 20 70 61 72 61 6d 65 74 he zTerm paramet
aea50 65 72 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69 er.. ** For thi
aea60 73 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 73 s routine, we as
aea70 73 75 6d 65 20 74 68 65 20 55 54 46 38 20 73 74 sume the UTF8 st
aea80 72 69 6e 67 20 69 73 20 61 6c 77 61 79 73 20 7a ring is always z
aea90 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a ero-terminated..
aeaa0 20 20 2a 2f 0a 20 20 63 20 3d 20 2a 28 7a 49 6e */. c = *(zIn
aeab0 2b 2b 29 3b 0a 20 20 69 66 28 20 63 3e 3d 30 78 ++);. if( c>=0x
aeac0 63 30 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71 c0 ){. c = sq
aead0 6c 69 74 65 33 55 74 66 38 54 72 61 6e 73 31 5b lite3Utf8Trans1[
aeae0 63 2d 30 78 63 30 5d 3b 0a 20 20 20 20 77 68 69 c-0xc0];. whi
aeaf0 6c 65 28 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 le( (*zIn & 0xc0
aeb00 29 3d 3d 30 78 38 30 20 29 7b 0a 20 20 20 20 20 )==0x80 ){.
aeb10 20 63 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30 c = (c<<6) + (0
aeb20 78 33 66 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b x3f & *(zIn++));
aeb30 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 . }. if( c
aeb40 3c 30 78 38 30 0a 20 20 20 20 20 20 20 20 7c 7c <0x80. ||
aeb50 20 28 63 26 30 78 46 46 46 46 46 38 30 30 29 3d (c&0xFFFFF800)=
aeb60 3d 30 78 44 38 30 30 0a 20 20 20 20 20 20 20 20 =0xD800.
aeb70 7c 7c 20 28 63 26 30 78 46 46 46 46 46 46 46 45 || (c&0xFFFFFFFE
aeb80 29 3d 3d 30 78 46 46 46 45 20 29 7b 20 20 63 20 )==0xFFFE ){ c
aeb90 3d 20 30 78 46 46 46 44 3b 20 7d 0a 20 20 7d 0a = 0xFFFD; }. }.
aeba0 20 20 2a 70 7a 4e 65 78 74 20 3d 20 7a 49 6e 3b *pzNext = zIn;
aebb0 0a 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a . return c;.}..
aebc0 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 .../*.** If the
aebd0 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 TRANSLATE_TRACE
aebe0 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 macro is defined
aebf0 2c 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 65 , the value of e
aec00 61 63 68 20 4d 65 6d 20 69 73 0a 2a 2a 20 70 72 ach Mem is.** pr
aec10 69 6e 74 65 64 20 6f 6e 20 73 74 64 65 72 72 20 inted on stderr
aec20 6f 6e 20 74 68 65 20 77 61 79 20 69 6e 74 6f 20 on the way into
aec30 61 6e 64 20 6f 75 74 20 6f 66 20 73 71 6c 69 74 and out of sqlit
aec40 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 e3VdbeMemTransla
aec50 74 65 28 29 2e 0a 2a 2f 20 0a 2f 2a 20 23 64 65 te()..*/ ./* #de
aec60 66 69 6e 65 20 54 52 41 4e 53 4c 41 54 45 5f 54 fine TRANSLATE_T
aec70 52 41 43 45 20 31 20 2a 2f 0a 0a 23 69 66 6e 64 RACE 1 */..#ifnd
aec80 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 ef SQLITE_OMIT_U
aec90 54 46 31 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 TF16./*.** This
aeca0 72 6f 75 74 69 6e 65 20 74 72 61 6e 73 66 6f 72 routine transfor
aecb0 6d 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 ms the internal
aecc0 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 text encoding us
aecd0 65 64 20 62 79 20 70 4d 65 6d 20 74 6f 0a 2a 2a ed by pMem to.**
aece0 20 64 65 73 69 72 65 64 45 6e 63 2e 20 49 74 20 desiredEnc. It
aecf0 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74 is an error if t
aed00 68 65 20 73 74 72 69 6e 67 20 69 73 20 61 6c 72 he string is alr
aed10 65 61 64 79 20 6f 66 20 74 68 65 20 64 65 73 69 eady of the desi
aed20 72 65 64 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 2c red.** encoding,
aed30 20 6f 72 20 69 66 20 2a 70 4d 65 6d 20 64 6f 65 or if *pMem doe
aed40 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 s not contain a
aed50 73 74 72 69 6e 67 20 76 61 6c 75 65 2e 0a 2a 2f string value..*/
aed60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
aed70 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
aed80 65 6d 54 72 61 6e 73 6c 61 74 65 28 4d 65 6d 20 emTranslate(Mem
aed90 2a 70 4d 65 6d 2c 20 75 38 20 64 65 73 69 72 65 *pMem, u8 desire
aeda0 64 45 6e 63 29 7b 0a 20 20 69 6e 74 20 6c 65 6e dEnc){. int len
aedb0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
aedc0 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 /* Maximum
aedd0 6c 65 6e 67 74 68 20 6f 66 20 6f 75 74 70 75 74 length of output
aede0 20 73 74 72 69 6e 67 20 69 6e 20 62 79 74 65 73 string in bytes
aedf0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
aee00 68 61 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20 har *zOut;
aee10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
aee20 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a utput buffer */.
aee30 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
aee40 2a 7a 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20 *zIn;
aee50 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 /* Input
aee60 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 iterator */. u
aee70 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 nsigned char *zT
aee80 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 erm;
aee90 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 69 /* End of i
aeea0 6e 70 75 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e nput */. unsign
aeeb0 65 64 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 ed char *z;
aeec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aeed0 2f 2a 20 4f 75 74 70 75 74 20 69 74 65 72 61 74 /* Output iterat
aeee0 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 or */. unsigned
aeef0 20 69 6e 74 20 63 3b 0a 0a 20 20 61 73 73 65 72 int c;.. asser
aef00 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c t( pMem->db==0 |
aef10 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
aef20 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d held(pMem->db->m
aef30 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
aef40 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d t( pMem->flags&M
aef50 45 4d 5f 53 74 72 20 29 3b 0a 20 20 61 73 73 65 EM_Str );. asse
aef60 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 rt( pMem->enc!=d
aef70 65 73 69 72 65 64 45 6e 63 20 29 3b 0a 20 20 61 esiredEnc );. a
aef80 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 ssert( pMem->enc
aef90 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
aefa0 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a pMem->n>=0 );..
aefb0 23 69 66 20 64 65 66 69 6e 65 64 28 54 52 41 4e #if defined(TRAN
aefc0 53 4c 41 54 45 5f 54 52 41 43 45 29 20 26 26 20 SLATE_TRACE) &&
aefd0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
aefe0 45 42 55 47 29 0a 20 20 7b 0a 20 20 20 20 63 68 EBUG). {. ch
aeff0 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 ar zBuf[100];.
af000 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
af010 50 72 65 74 74 79 50 72 69 6e 74 28 70 4d 65 6d PrettyPrint(pMem
af020 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 72 , zBuf);. fpr
af030 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 49 4e intf(stderr, "IN
af040 50 55 54 3a 20 20 25 73 5c 6e 22 2c 20 7a 42 75 PUT: %s\n", zBu
af050 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a f);. }.#endif..
af060 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 61 6e /* If the tran
af070 73 6c 61 74 69 6f 6e 20 69 73 20 62 65 74 77 65 slation is betwe
af080 65 6e 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 en UTF-16 little
af090 20 61 6e 64 20 62 69 67 20 65 6e 64 69 61 6e 2c and big endian,
af0a0 20 74 68 65 6e 20 0a 20 20 2a 2a 20 61 6c 6c 20 then . ** all
af0b0 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65 64 that is required
af0c0 20 69 73 20 74 6f 20 73 77 61 70 20 74 68 65 20 is to swap the
af0d0 62 79 74 65 20 6f 72 64 65 72 2e 20 54 68 69 73 byte order. This
af0e0 20 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 64 case is handled
af0f0 0a 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 6c . ** differentl
af100 79 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 y from the other
af110 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4d s.. */. if( pM
af120 65 6d 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f em->enc!=SQLITE_
af130 55 54 46 38 20 26 26 20 64 65 73 69 72 65 64 45 UTF8 && desiredE
af140 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 nc!=SQLITE_UTF8
af150 29 7b 0a 20 20 20 20 75 38 20 74 65 6d 70 3b 0a ){. u8 temp;.
af160 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 int rc;.
af170 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
af180 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 MemMakeWriteable
af190 28 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 (pMem);. if(
af1a0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
af1b0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 . assert( r
af1c0 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 c==SQLITE_NOMEM
af1d0 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
af1e0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
af1f0 20 20 7d 0a 20 20 20 20 7a 49 6e 20 3d 20 28 75 }. zIn = (u
af200 38 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 8*)pMem->z;.
af210 7a 54 65 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 zTerm = &zIn[pMe
af220 6d 2d 3e 6e 26 7e 31 5d 3b 0a 20 20 20 20 77 68 m->n&~1];. wh
af230 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 ile( zIn<zTerm )
af240 7b 0a 20 20 20 20 20 20 74 65 6d 70 20 3d 20 2a {. temp = *
af250 7a 49 6e 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 20 zIn;. *zIn
af260 3d 20 2a 28 7a 49 6e 2b 31 29 3b 0a 20 20 20 20 = *(zIn+1);.
af270 20 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20 20 20 2a zIn++;. *
af280 7a 49 6e 2b 2b 20 3d 20 74 65 6d 70 3b 0a 20 20 zIn++ = temp;.
af290 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e }. pMem->en
af2a0 63 20 3d 20 64 65 73 69 72 65 64 45 6e 63 3b 0a c = desiredEnc;.
af2b0 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 6c 61 goto transla
af2c0 74 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f te_out;. }.. /
af2d0 2a 20 53 65 74 20 6c 65 6e 20 74 6f 20 74 68 65 * Set len to the
af2e0 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 maximum number
af2f0 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 of bytes require
af300 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20 d in the output
af310 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 69 66 28 buffer. */. if(
af320 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c desiredEnc==SQL
af330 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 ITE_UTF8 ){.
af340 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 /* When converti
af350 6e 67 20 66 72 6f 6d 20 55 54 46 2d 31 36 2c 20 ng from UTF-16,
af360 74 68 65 20 6d 61 78 69 6d 75 6d 20 67 72 6f 77 the maximum grow
af370 74 68 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 0a th results from.
af380 20 20 20 20 2a 2a 20 74 72 61 6e 73 6c 61 74 69 ** translati
af390 6e 67 20 61 20 32 2d 62 79 74 65 20 63 68 61 72 ng a 2-byte char
af3a0 61 63 74 65 72 20 74 6f 20 61 20 34 2d 62 79 74 acter to a 4-byt
af3b0 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 e UTF-8 characte
af3c0 72 2e 0a 20 20 20 20 2a 2a 20 41 20 73 69 6e 67 r.. ** A sing
af3d0 6c 65 20 62 79 74 65 20 69 73 20 72 65 71 75 69 le byte is requi
af3e0 72 65 64 20 66 6f 72 20 74 68 65 20 6f 75 74 70 red for the outp
af3f0 75 74 20 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a ut string. **
af400 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2e nul-terminator.
af410 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4d 65 6d . */. pMem
af420 2d 3e 6e 20 26 3d 20 7e 31 3b 0a 20 20 20 20 6c ->n &= ~1;. l
af430 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32 en = pMem->n * 2
af440 20 2b 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 + 1;. }else{.
af450 20 20 20 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65 /* When conve
af460 72 74 69 6e 67 20 66 72 6f 6d 20 55 54 46 2d 38 rting from UTF-8
af470 20 74 6f 20 55 54 46 2d 31 36 20 74 68 65 20 6d to UTF-16 the m
af480 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 69 73 aximum growth is
af490 20 63 61 75 73 65 64 0a 20 20 20 20 2a 2a 20 77 caused. ** w
af4a0 68 65 6e 20 61 20 31 2d 62 79 74 65 20 55 54 46 hen a 1-byte UTF
af4b0 2d 38 20 63 68 61 72 61 63 74 65 72 20 69 73 20 -8 character is
af4c0 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 translated into
af4d0 61 20 32 2d 62 79 74 65 20 55 54 46 2d 31 36 0a a 2-byte UTF-16.
af4e0 20 20 20 20 2a 2a 20 63 68 61 72 61 63 74 65 72 ** character
af4f0 2e 20 54 77 6f 20 62 79 74 65 73 20 61 72 65 20 . Two bytes are
af500 72 65 71 75 69 72 65 64 20 69 6e 20 74 68 65 20 required in the
af510 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 66 6f output buffer fo
af520 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 75 6c r the. ** nul
af530 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 -terminator..
af540 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d */. len = pM
af550 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 32 3b 0a 20 em->n * 2 + 2;.
af560 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 7a 49 6e }.. /* Set zIn
af570 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 to point at the
af580 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 69 6e start of the in
af590 70 75 74 20 62 75 66 66 65 72 20 61 6e 64 20 7a put buffer and z
af5a0 54 65 72 6d 20 74 6f 20 70 6f 69 6e 74 20 31 0a Term to point 1.
af5b0 20 20 2a 2a 20 62 79 74 65 20 70 61 73 74 20 74 ** byte past t
af5c0 68 65 20 65 6e 64 2e 0a 20 20 2a 2a 0a 20 20 2a he end.. **. *
af5d0 2a 20 56 61 72 69 61 62 6c 65 20 7a 4f 75 74 20 * Variable zOut
af5e0 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 is set to point
af5f0 61 74 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 at the output bu
af600 66 66 65 72 2c 20 73 70 61 63 65 20 6f 62 74 61 ffer, space obta
af610 69 6e 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 73 ined. ** from s
af620 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e qlite3_malloc().
af630 0a 20 20 2a 2f 0a 20 20 7a 49 6e 20 3d 20 28 75 . */. zIn = (u
af640 38 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 7a 54 8*)pMem->z;. zT
af650 65 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d erm = &zIn[pMem-
af660 3e 6e 5d 3b 0a 20 20 7a 4f 75 74 20 3d 20 73 71 >n];. zOut = sq
af670 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
af680 28 70 4d 65 6d 2d 3e 64 62 2c 20 6c 65 6e 29 3b (pMem->db, len);
af690 0a 20 20 69 66 28 20 21 7a 4f 75 74 20 29 7b 0a . if( !zOut ){.
af6a0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
af6b0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 7a E_NOMEM;. }. z
af6c0 20 3d 20 7a 4f 75 74 3b 0a 0a 20 20 69 66 28 20 = zOut;.. if(
af6d0 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 pMem->enc==SQLIT
af6e0 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 69 66 E_UTF8 ){. if
af6f0 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 ( desiredEnc==SQ
af700 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a LITE_UTF16LE ){.
af710 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d /* UTF-8 -
af720 3e 20 55 54 46 2d 31 36 20 4c 69 74 74 6c 65 2d > UTF-16 Little-
af730 65 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20 endian */.
af740 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d while( zIn<zTerm
af750 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 63 ){. /* c
af760 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 = sqlite3Utf8Re
af770 61 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 ad(zIn, zTerm, (
af780 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 const u8**)&zIn)
af790 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 52 45 41 ; */. REA
af7a0 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 D_UTF8(zIn, zTer
af7b0 6d 2c 20 63 29 3b 0a 20 20 20 20 20 20 20 20 57 m, c);. W
af7c0 52 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 RITE_UTF16LE(z,
af7d0 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 c);. }.
af7e0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 }else{. ass
af7f0 65 72 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d ert( desiredEnc=
af800 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 =SQLITE_UTF16BE
af810 29 3b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d );. /* UTF-
af820 38 20 2d 3e 20 55 54 46 2d 31 36 20 42 69 67 2d 8 -> UTF-16 Big-
af830 65 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20 endian */.
af840 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d while( zIn<zTerm
af850 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 63 ){. /* c
af860 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 = sqlite3Utf8Re
af870 61 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 ad(zIn, zTerm, (
af880 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 const u8**)&zIn)
af890 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 52 45 41 ; */. REA
af8a0 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 D_UTF8(zIn, zTer
af8b0 6d 2c 20 63 29 3b 0a 20 20 20 20 20 20 20 20 57 m, c);. W
af8c0 52 49 54 45 5f 55 54 46 31 36 42 45 28 7a 2c 20 RITE_UTF16BE(z,
af8d0 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 c);. }.
af8e0 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 }. pMem->n =
af8f0 28 69 6e 74 29 28 7a 20 2d 20 7a 4f 75 74 29 3b (int)(z - zOut);
af900 0a 20 20 20 20 2a 7a 2b 2b 20 3d 20 30 3b 0a 20 . *z++ = 0;.
af910 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
af920 72 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d rt( desiredEnc==
af930 53 51 4c 49 54 45 5f 55 54 46 38 20 29 3b 0a 20 SQLITE_UTF8 );.
af940 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 if( pMem->enc
af950 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 ==SQLITE_UTF16LE
af960 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 ){. /* UTF
af970 2d 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 -16 Little-endia
af980 6e 20 2d 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20 n -> UTF-8 */.
af990 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a while( zIn<z
af9a0 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 Term ){.
af9b0 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e READ_UTF16LE(zIn
af9c0 2c 20 7a 49 6e 3c 7a 54 65 72 6d 2c 20 63 29 3b , zIn<zTerm, c);
af9d0 20 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f . WRITE_
af9e0 55 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 UTF8(z, c);.
af9f0 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
afa00 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 42 /* UTF-16 B
afa10 69 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 ig-endian -> UTF
afa20 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c -8 */. whil
afa30 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a e( zIn<zTerm ){.
afa40 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 READ_UTF
afa50 31 36 42 45 28 7a 49 6e 2c 20 7a 49 6e 3c 7a 54 16BE(zIn, zIn<zT
afa60 65 72 6d 2c 20 63 29 3b 20 0a 20 20 20 20 20 20 erm, c); .
afa70 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 WRITE_UTF8(z,
afa80 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 c);. }.
afa90 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 }. pMem->n =
afaa0 28 69 6e 74 29 28 7a 20 2d 20 7a 4f 75 74 29 3b (int)(z - zOut);
afab0 0a 20 20 7d 0a 20 20 2a 7a 20 3d 20 30 3b 0a 20 . }. *z = 0;.
afac0 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e assert( (pMem->
afad0 6e 2b 28 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 n+(desiredEnc==S
afae0 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 29 QLITE_UTF8?1:2))
afaf0 3c 3d 6c 65 6e 20 29 3b 0a 0a 20 20 73 71 6c 69 <=len );.. sqli
afb00 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
afb10 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d e(pMem);. pMem-
afb20 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f >flags &= ~(MEM_
afb30 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 7c 4d Static|MEM_Dyn|M
afb40 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 70 4d 65 EM_Ephem);. pMe
afb50 6d 2d 3e 65 6e 63 20 3d 20 64 65 73 69 72 65 64 m->enc = desired
afb60 45 6e 63 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 Enc;. pMem->fla
afb70 67 73 20 7c 3d 20 28 4d 45 4d 5f 54 65 72 6d 7c gs |= (MEM_Term|
afb80 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 70 4d 65 6d MEM_Dyn);. pMem
afb90 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 7a 4f 75 ->z = (char*)zOu
afba0 74 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c t;. pMem->zMall
afbb0 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 0a 74 oc = pMem->z;..t
afbc0 72 61 6e 73 6c 61 74 65 5f 6f 75 74 3a 0a 23 69 ranslate_out:.#i
afbd0 66 20 64 65 66 69 6e 65 64 28 54 52 41 4e 53 4c f defined(TRANSL
afbe0 41 54 45 5f 54 52 41 43 45 29 20 26 26 20 64 65 ATE_TRACE) && de
afbf0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
afc00 55 47 29 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 UG). {. char
afc10 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 zBuf[100];.
afc20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 sqlite3VdbeMemPr
afc30 65 74 74 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20 ettyPrint(pMem,
afc40 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69 6e zBuf);. fprin
afc50 74 66 28 73 74 64 65 72 72 2c 20 22 4f 55 54 50 tf(stderr, "OUTP
afc60 55 54 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 UT: %s\n", zBuf)
afc70 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 ;. }.#endif. r
afc80 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
afc90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
afca0 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 66 6f outine checks fo
afcb0 72 20 61 20 62 79 74 65 2d 6f 72 64 65 72 20 6d r a byte-order m
afcc0 61 72 6b 20 61 74 20 74 68 65 20 62 65 67 69 6e ark at the begin
afcd0 6e 69 6e 67 20 6f 66 20 74 68 65 20 0a 2a 2a 20 ning of the .**
afce0 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 73 74 UTF-16 string st
afcf0 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 2e 20 49 ored in *pMem. I
afd00 66 20 6f 6e 65 20 69 73 20 70 72 65 73 65 6e 74 f one is present
afd10 2c 20 69 74 20 69 73 20 72 65 6d 6f 76 65 64 20 , it is removed
afd20 61 6e 64 0a 2a 2a 20 74 68 65 20 65 6e 63 6f 64 and.** the encod
afd30 69 6e 67 20 6f 66 20 74 68 65 20 4d 65 6d 20 61 ing of the Mem a
afd40 64 6a 75 73 74 65 64 2e 20 54 68 69 73 20 72 6f djusted. This ro
afd50 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 64 utine does not d
afd60 6f 20 61 6e 79 0a 2a 2a 20 62 79 74 65 2d 73 77 o any.** byte-sw
afd70 61 70 70 69 6e 67 2c 20 69 74 20 6a 75 73 74 20 apping, it just
afd80 73 65 74 73 20 4d 65 6d 2e 65 6e 63 20 61 70 70 sets Mem.enc app
afd90 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a ropriately..**.*
afda0 2a 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e * The allocation
afdb0 20 28 73 74 61 74 69 63 2c 20 64 79 6e 61 6d 69 (static, dynami
afdc0 63 20 65 74 63 2e 29 20 61 6e 64 20 65 6e 63 6f c etc.) and enco
afdd0 64 69 6e 67 20 6f 66 20 74 68 65 20 4d 65 6d 20 ding of the Mem
afde0 6d 61 79 20 62 65 0a 2a 2a 20 63 68 61 6e 67 65 may be.** change
afdf0 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 d by this functi
afe00 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
afe10 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
afe20 33 56 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 3VdbeMemHandleBo
afe30 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 m(Mem *pMem){.
afe40 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
afe50 4f 4b 3b 0a 20 20 75 38 20 62 6f 6d 20 3d 20 30 OK;. u8 bom = 0
afe60 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 ;.. assert( pMe
afe70 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 20 20 69 66 28 m->n>=0 );. if(
afe80 20 70 4d 65 6d 2d 3e 6e 3e 31 20 29 7b 0a 20 20 pMem->n>1 ){.
afe90 20 20 75 38 20 62 31 20 3d 20 2a 28 75 38 20 2a u8 b1 = *(u8 *
afea0 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 75 38 )pMem->z;. u8
afeb0 20 62 32 20 3d 20 2a 28 28 28 75 38 20 2a 29 70 b2 = *(((u8 *)p
afec0 4d 65 6d 2d 3e 7a 29 20 2b 20 31 29 3b 0a 20 20 Mem->z) + 1);.
afed0 20 20 69 66 28 20 62 31 3d 3d 30 78 46 45 20 26 if( b1==0xFE &
afee0 26 20 62 32 3d 3d 30 78 46 46 20 29 7b 0a 20 20 & b2==0xFF ){.
afef0 20 20 20 20 62 6f 6d 20 3d 20 53 51 4c 49 54 45 bom = SQLITE
aff00 5f 55 54 46 31 36 42 45 3b 0a 20 20 20 20 7d 0a _UTF16BE;. }.
aff10 20 20 20 20 69 66 28 20 62 31 3d 3d 30 78 46 46 if( b1==0xFF
aff20 20 26 26 20 62 32 3d 3d 30 78 46 45 20 29 7b 0a && b2==0xFE ){.
aff30 20 20 20 20 20 20 62 6f 6d 20 3d 20 53 51 4c 49 bom = SQLI
aff40 54 45 5f 55 54 46 31 36 4c 45 3b 0a 20 20 20 20 TE_UTF16LE;.
aff50 7d 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 62 }. }. . if( b
aff60 6f 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 om ){. rc = s
aff70 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b qlite3VdbeMemMak
aff80 65 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d 29 eWriteable(pMem)
aff90 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
affa0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
affb0 20 70 4d 65 6d 2d 3e 6e 20 2d 3d 20 32 3b 0a 20 pMem->n -= 2;.
affc0 20 20 20 20 20 6d 65 6d 6d 6f 76 65 28 70 4d 65 memmove(pMe
affd0 6d 2d 3e 7a 2c 20 26 70 4d 65 6d 2d 3e 7a 5b 32 m->z, &pMem->z[2
affe0 5d 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 ], pMem->n);.
afff0 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d pMem->z[pMem-
b0000 3e 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 >n] = '\0';.
b0010 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e pMem->z[pMem->
b0020 6e 2b 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 n+1] = '\0';.
b0030 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c pMem->flags |
b0040 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 = MEM_Term;.
b0050 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 62 6f pMem->enc = bo
b0060 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 m;. }. }. r
b0070 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 eturn rc;.}.#end
b0080 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
b0090 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a T_UTF16 */../*.*
b00a0 2a 20 70 5a 20 69 73 20 61 20 55 54 46 2d 38 20 * pZ is a UTF-8
b00b0 65 6e 63 6f 64 65 64 20 75 6e 69 63 6f 64 65 20 encoded unicode
b00c0 73 74 72 69 6e 67 2e 20 49 66 20 6e 42 79 74 65 string. If nByte
b00d0 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 is less than ze
b00e0 72 6f 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 74 68 ro,.** return th
b00f0 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 63 e number of unic
b0100 6f 64 65 20 63 68 61 72 61 63 74 65 72 73 20 69 ode characters i
b0110 6e 20 70 5a 20 75 70 20 74 6f 20 28 62 75 74 20 n pZ up to (but
b0120 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 29 0a 2a not including).*
b0130 2a 20 74 68 65 20 66 69 72 73 74 20 30 78 30 30 * the first 0x00
b0140 20 62 79 74 65 2e 20 49 66 20 6e 42 79 74 65 20 byte. If nByte
b0150 69 73 20 6e 6f 74 20 6c 65 73 73 20 74 68 61 6e is not less than
b0160 20 7a 65 72 6f 2c 20 72 65 74 75 72 6e 20 74 68 zero, return th
b0170 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75 e.** number of u
b0180 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74 65 72 nicode character
b0190 73 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 6e s in the first n
b01a0 42 79 74 65 20 6f 66 20 70 5a 20 28 6f 72 20 75 Byte of pZ (or u
b01b0 70 20 74 6f 20 0a 2a 2a 20 74 68 65 20 66 69 72 p to .** the fir
b01c0 73 74 20 30 78 30 30 2c 20 77 68 69 63 68 65 76 st 0x00, whichev
b01d0 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 29 2e er comes first).
b01e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b01f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 TE int sqlite3Ut
b0200 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e 73 74 20 f8CharLen(const
b0210 63 68 61 72 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e char *zIn, int n
b0220 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 72 20 3d Byte){. int r =
b0230 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 0;. const u8 *
b0240 7a 20 3d 20 28 63 6f 6e 73 74 20 75 38 2a 29 7a z = (const u8*)z
b0250 49 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a In;. const u8 *
b0260 7a 54 65 72 6d 3b 0a 20 20 69 66 28 20 6e 42 79 zTerm;. if( nBy
b0270 74 65 3e 3d 30 20 29 7b 0a 20 20 20 20 7a 54 65 te>=0 ){. zTe
b0280 72 6d 20 3d 20 26 7a 5b 6e 42 79 74 65 5d 3b 0a rm = &z[nByte];.
b0290 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 54 65 }else{. zTe
b02a0 72 6d 20 3d 20 28 63 6f 6e 73 74 20 75 38 2a 29 rm = (const u8*)
b02b0 28 2d 31 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 (-1);. }. asse
b02c0 72 74 28 20 7a 3c 3d 7a 54 65 72 6d 20 29 3b 0a rt( z<=zTerm );.
b02d0 20 20 77 68 69 6c 65 28 20 2a 7a 21 3d 30 20 26 while( *z!=0 &
b02e0 26 20 7a 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 & z<zTerm ){.
b02f0 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 SQLITE_SKIP_UTF
b0300 38 28 7a 29 3b 0a 20 20 20 20 72 2b 2b 3b 0a 20 8(z);. r++;.
b0310 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d }. return r;.}
b0320 0a 0a 2f 2a 20 54 68 69 73 20 74 65 73 74 20 66 ../* This test f
b0330 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 unction is not c
b0340 75 72 72 65 6e 74 6c 79 20 75 73 65 64 20 62 79 urrently used by
b0350 20 74 68 65 20 61 75 74 6f 6d 61 74 65 64 20 74 the automated t
b0360 65 73 74 2d 73 75 69 74 65 2e 20 0a 2a 2a 20 48 est-suite. .** H
b0370 65 6e 63 65 20 69 74 20 69 73 20 6f 6e 6c 79 20 ence it is only
b0380 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 64 65 62 available in deb
b0390 75 67 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 ug builds..*/.#i
b03a0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
b03b0 5f 54 45 53 54 29 20 26 26 20 64 65 66 69 6e 65 _TEST) && define
b03c0 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a d(SQLITE_DEBUG).
b03d0 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20 /*.** Translate
b03e0 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 38 2e 0a UTF-8 to UTF-8..
b03f0 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 61 73 20 74 **.** This has t
b0400 68 65 20 65 66 66 65 63 74 20 6f 66 20 6d 61 6b he effect of mak
b0410 69 6e 67 20 73 75 72 65 20 74 68 61 74 20 74 68 ing sure that th
b0420 65 20 73 74 72 69 6e 67 20 69 73 20 77 65 6c 6c e string is well
b0430 2d 66 6f 72 6d 65 64 0a 2a 2a 20 55 54 46 2d 38 -formed.** UTF-8
b0440 2e 20 20 4d 69 73 63 6f 64 65 64 20 63 68 61 72 . Miscoded char
b0450 61 63 74 65 72 73 20 61 72 65 20 72 65 6d 6f 76 acters are remov
b0460 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 72 ed..**.** The tr
b0470 61 6e 73 6c 61 74 69 6f 6e 20 69 73 20 64 6f 6e anslation is don
b0480 65 20 69 6e 2d 70 6c 61 63 65 20 28 73 69 6e 63 e in-place (sinc
b0490 65 20 69 74 20 69 73 20 69 6d 70 6f 73 73 69 62 e it is impossib
b04a0 6c 65 20 66 6f 72 20 74 68 65 0a 2a 2a 20 63 6f le for the.** co
b04b0 72 72 65 63 74 20 55 54 46 2d 38 20 65 6e 63 6f rrect UTF-8 enco
b04c0 64 69 6e 67 20 74 6f 20 62 65 20 6c 6f 6e 67 65 ding to be longe
b04d0 72 20 74 68 61 6e 20 61 20 6d 61 6c 66 6f 72 6d r than a malform
b04e0 65 64 20 65 6e 63 6f 64 69 6e 67 29 2e 0a 2a 2f ed encoding)..*/
b04f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b0500 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 54 int sqlite3Utf8T
b0510 6f 38 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 o8(unsigned char
b0520 20 2a 7a 49 6e 29 7b 0a 20 20 75 6e 73 69 67 6e *zIn){. unsign
b0530 65 64 20 63 68 61 72 20 2a 7a 4f 75 74 20 3d 20 ed char *zOut =
b0540 7a 49 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 zIn;. unsigned
b0550 63 68 61 72 20 2a 7a 53 74 61 72 74 20 3d 20 7a char *zStart = z
b0560 49 6e 3b 0a 20 20 75 33 32 20 63 3b 0a 0a 20 20 In;. u32 c;..
b0570 77 68 69 6c 65 28 20 7a 49 6e 5b 30 5d 20 29 7b while( zIn[0] ){
b0580 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 . c = sqlite3
b0590 55 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 28 63 Utf8Read(zIn, (c
b05a0 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b onst u8**)&zIn);
b05b0 0a 20 20 20 20 69 66 28 20 63 21 3d 30 78 66 66 . if( c!=0xff
b05c0 66 64 20 29 7b 0a 20 20 20 20 20 20 57 52 49 54 fd ){. WRIT
b05d0 45 5f 55 54 46 38 28 7a 4f 75 74 2c 20 63 29 3b E_UTF8(zOut, c);
b05e0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 7a 4f . }. }. *zO
b05f0 75 74 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e ut = 0;. return
b0600 20 28 69 6e 74 29 28 7a 4f 75 74 20 2d 20 7a 53 (int)(zOut - zS
b0610 74 61 72 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a tart);.}.#endif.
b0620 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
b0630 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a OMIT_UTF16./*.**
b0640 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 31 Convert a UTF-1
b0650 36 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 6 string in the
b0660 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 20 native encoding
b0670 69 6e 74 6f 20 61 20 55 54 46 2d 38 20 73 74 72 into a UTF-8 str
b0680 69 6e 67 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 ing..** Memory t
b0690 6f 20 68 6f 6c 64 20 74 68 65 20 55 54 46 2d 38 o hold the UTF-8
b06a0 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 string is obtai
b06b0 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
b06c0 5f 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 _malloc and must
b06d0 0a 2a 2a 20 62 65 20 66 72 65 65 64 20 62 79 20 .** be freed by
b06e0 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 the calling func
b06f0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 55 4c 4c tion..**.** NULL
b0700 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
b0710 74 68 65 72 65 20 69 73 20 61 6e 20 61 6c 6c 6f there is an allo
b0720 63 61 74 69 6f 6e 20 65 72 72 6f 72 2e 0a 2a 2f cation error..*/
b0730 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b0740 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66 char *sqlite3Utf
b0750 31 36 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 64 16to8(sqlite3 *d
b0760 62 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a b, const void *z
b0770 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 , int nByte){.
b0780 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 65 74 28 Mem m;. memset(
b0790 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29 &m, 0, sizeof(m)
b07a0 29 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b 0a );. m.db = db;.
b07b0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
b07c0 53 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e 42 SetStr(&m, z, nB
b07d0 79 74 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 yte, SQLITE_UTF1
b07e0 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 6NATIVE, SQLITE_
b07f0 53 54 41 54 49 43 29 3b 0a 20 20 73 71 6c 69 74 STATIC);. sqlit
b0800 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f e3VdbeChangeEnco
b0810 64 69 6e 67 28 26 6d 2c 20 53 51 4c 49 54 45 5f ding(&m, SQLITE_
b0820 55 54 46 38 29 3b 0a 20 20 69 66 28 20 64 62 2d UTF8);. if( db-
b0830 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
b0840 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
b0850 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a MemRelease(&m);.
b0860 20 20 20 20 6d 2e 7a 20 3d 20 30 3b 0a 20 20 7d m.z = 0;. }
b0870 0a 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c . assert( (m.fl
b0880 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21 ags & MEM_Term)!
b0890 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 =0 || db->malloc
b08a0 46 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73 65 Failed );. asse
b08b0 72 74 28 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d rt( (m.flags & M
b08c0 45 4d 5f 53 74 72 29 21 3d 30 20 7c 7c 20 64 62 EM_Str)!=0 || db
b08d0 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
b08e0 3b 0a 20 20 72 65 74 75 72 6e 20 28 6d 2e 66 6c ;. return (m.fl
b08f0 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 21 3d ags & MEM_Dyn)!=
b0900 30 20 3f 20 6d 2e 7a 20 3a 20 73 71 6c 69 74 65 0 ? m.z : sqlite
b0910 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 6d 2e 3DbStrDup(db, m.
b0920 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e z);.}../*.** Con
b0930 76 65 72 74 20 61 20 55 54 46 2d 38 20 73 74 72 vert a UTF-8 str
b0940 69 6e 67 20 74 6f 20 74 68 65 20 55 54 46 2d 31 ing to the UTF-1
b0950 36 20 65 6e 63 6f 64 69 6e 67 20 73 70 65 63 69 6 encoding speci
b0960 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 fied by paramete
b0970 72 0a 2a 2a 20 65 6e 63 2e 20 41 20 70 6f 69 6e r.** enc. A poin
b0980 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73 ter to the new s
b0990 74 72 69 6e 67 20 69 73 20 72 65 74 75 72 6e 65 tring is returne
b09a0 64 2c 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 d, and the value
b09b0 20 6f 66 20 2a 70 6e 4f 75 74 0a 2a 2a 20 69 73 of *pnOut.** is
b09c0 20 73 65 74 20 74 6f 20 74 68 65 20 6c 65 6e 67 set to the leng
b09d0 74 68 20 6f 66 20 74 68 65 20 72 65 74 75 72 6e th of the return
b09e0 65 64 20 73 74 72 69 6e 67 20 69 6e 20 62 79 74 ed string in byt
b09f0 65 73 2e 20 54 68 65 20 63 61 6c 6c 20 73 68 6f es. The call sho
b0a00 75 6c 64 0a 2a 2a 20 61 72 72 61 6e 67 65 20 74 uld.** arrange t
b0a10 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 o call sqlite3Db
b0a20 46 72 65 65 28 29 20 6f 6e 20 74 68 65 20 72 65 Free() on the re
b0a30 74 75 72 6e 65 64 20 70 6f 69 6e 74 65 72 20 77 turned pointer w
b0a40 68 65 6e 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 20 hen it is.** no
b0a50 6c 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64 2e longer required.
b0a60 0a 2a 2a 20 0a 2a 2a 20 49 66 20 61 20 6d 61 6c .** .** If a mal
b0a70 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 75 loc failure occu
b0a80 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 rs, NULL is retu
b0a90 72 6e 65 64 20 61 6e 64 20 74 68 65 20 64 62 2e rned and the db.
b0aa0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 0a 2a 2a 20 mallocFailed.**
b0ab0 66 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 69 66 flag set..*/.#if
b0ac0 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
b0ad0 45 5f 53 54 41 54 32 0a 53 51 4c 49 54 45 5f 50 E_STAT2.SQLITE_P
b0ae0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
b0af0 69 74 65 33 55 74 66 38 74 6f 31 36 28 73 71 6c ite3Utf8to16(sql
b0b00 69 74 65 33 20 2a 64 62 2c 20 75 38 20 65 6e 63 ite3 *db, u8 enc
b0b10 2c 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e , char *z, int n
b0b20 2c 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a 20 , int *pnOut){.
b0b30 20 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 65 74 Mem m;. memset
b0b40 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d (&m, 0, sizeof(m
b0b50 29 29 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b ));. m.db = db;
b0b60 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
b0b70 6d 53 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e mSetStr(&m, z, n
b0b80 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 , SQLITE_UTF8, S
b0b90 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 QLITE_STATIC);.
b0ba0 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
b0bb0 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 26 6d 2c MemTranslate(&m,
b0bc0 20 65 6e 63 29 20 29 7b 0a 20 20 20 20 61 73 73 enc) ){. ass
b0bd0 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 ert( db->mallocF
b0be0 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 74 ailed );. ret
b0bf0 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 urn 0;. }. ass
b0c00 65 72 74 28 20 6d 2e 7a 3d 3d 6d 2e 7a 4d 61 6c ert( m.z==m.zMal
b0c10 6c 6f 63 20 29 3b 0a 20 20 2a 70 6e 4f 75 74 20 loc );. *pnOut
b0c20 3d 20 6d 2e 6e 3b 0a 20 20 72 65 74 75 72 6e 20 = m.n;. return
b0c30 6d 2e 7a 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f m.z;.}.#endif../
b0c40 2a 0a 2a 2a 20 7a 49 6e 20 69 73 20 61 20 55 54 *.** zIn is a UT
b0c50 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 75 6e 69 F-16 encoded uni
b0c60 63 6f 64 65 20 73 74 72 69 6e 67 20 61 74 20 6c code string at l
b0c70 65 61 73 74 20 6e 43 68 61 72 20 63 68 61 72 61 east nChar chara
b0c80 63 74 65 72 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52 cters long..** R
b0c90 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
b0ca0 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 of bytes in the
b0cb0 20 66 69 72 73 74 20 6e 43 68 61 72 20 75 6e 69 first nChar uni
b0cc0 63 6f 64 65 20 63 68 61 72 61 63 74 65 72 73 0a code characters.
b0cd0 2a 2a 20 69 6e 20 70 5a 2e 20 20 6e 43 68 61 72 ** in pZ. nChar
b0ce0 20 6d 75 73 74 20 62 65 20 6e 6f 6e 2d 6e 65 67 must be non-neg
b0cf0 61 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ative..*/.SQLITE
b0d00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
b0d10 69 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e ite3Utf16ByteLen
b0d20 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 49 6e (const void *zIn
b0d30 2c 20 69 6e 74 20 6e 43 68 61 72 29 7b 0a 20 20 , int nChar){.
b0d40 69 6e 74 20 63 3b 0a 20 20 75 6e 73 69 67 6e 65 int c;. unsigne
b0d50 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 20 d char const *z
b0d60 3d 20 7a 49 6e 3b 0a 20 20 69 6e 74 20 6e 20 3d = zIn;. int n =
b0d70 20 30 3b 0a 20 20 0a 20 20 69 66 28 20 53 51 4c 0;. . if( SQL
b0d80 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d ITE_UTF16NATIVE=
b0d90 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 =SQLITE_UTF16BE
b0da0 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 3c ){. while( n<
b0db0 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20 52 nChar ){. R
b0dc0 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 31 EAD_UTF16BE(z, 1
b0dd0 2c 20 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b , c);. n++;
b0de0 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
b0df0 20 20 20 20 77 68 69 6c 65 28 20 6e 3c 6e 43 68 while( n<nCh
b0e00 61 72 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44 ar ){. READ
b0e10 5f 55 54 46 31 36 4c 45 28 7a 2c 20 31 2c 20 63 _UTF16LE(z, 1, c
b0e20 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 );. n++;.
b0e30 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
b0e40 20 28 69 6e 74 29 28 7a 2d 28 75 6e 73 69 67 6e (int)(z-(unsign
b0e50 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 29 ed char const *)
b0e60 7a 49 6e 29 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 zIn);.}..#if def
b0e70 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
b0e80 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 )./*.** This rou
b0e90 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 tine is called f
b0ea0 72 6f 6d 20 74 68 65 20 54 43 4c 20 74 65 73 74 rom the TCL test
b0eb0 20 66 75 6e 63 74 69 6f 6e 20 22 74 72 61 6e 73 function "trans
b0ec0 6c 61 74 65 5f 73 65 6c 66 74 65 73 74 22 2e 0a late_selftest"..
b0ed0 2a 2a 20 49 74 20 63 68 65 63 6b 73 20 74 68 61 ** It checks tha
b0ee0 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 73 t the primitives
b0ef0 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e 67 for serializing
b0f00 20 61 6e 64 20 64 65 73 65 72 69 61 6c 69 7a 69 and deserializi
b0f10 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 ng.** characters
b0f20 20 69 6e 20 65 61 63 68 20 65 6e 63 6f 64 69 6e in each encodin
b0f30 67 20 61 72 65 20 69 6e 76 65 72 73 65 73 20 6f g are inverses o
b0f40 66 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f f each other..*/
b0f50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b0f60 76 6f 69 64 20 73 71 6c 69 74 65 33 55 74 66 53 void sqlite3UtfS
b0f70 65 6c 66 54 65 73 74 28 76 6f 69 64 29 7b 0a 20 elfTest(void){.
b0f80 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 2c unsigned int i,
b0f90 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 t;. unsigned c
b0fa0 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 20 20 har zBuf[20];.
b0fb0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
b0fc0 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 ;. int n;. uns
b0fd0 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 20 igned int c;..
b0fe0 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 for(i=0; i<0x001
b0ff0 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 10000; i++){.
b1000 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 z = zBuf;. W
b1010 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 69 29 3b RITE_UTF8(z, i);
b1020 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a . n = (int)(z
b1030 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65 -zBuf);. asse
b1040 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 rt( n>0 && n<=4
b1050 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b );. z[0] = 0;
b1060 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 . z = zBuf;.
b1070 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 c = sqlite3Ut
b1080 66 38 52 65 61 64 28 7a 2c 20 28 63 6f 6e 73 74 f8Read(z, (const
b1090 20 75 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 74 u8**)&z);. t
b10a0 20 3d 20 69 3b 0a 20 20 20 20 69 66 28 20 69 3e = i;. if( i>
b10b0 3d 30 78 44 38 30 30 20 26 26 20 69 3c 3d 30 78 =0xD800 && i<=0x
b10c0 44 46 46 46 20 29 20 74 20 3d 20 30 78 46 46 46 DFFF ) t = 0xFFF
b10d0 44 3b 0a 20 20 20 20 69 66 28 20 28 69 26 30 78 D;. if( (i&0x
b10e0 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 FFFFFFFE)==0xFFF
b10f0 45 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a E ) t = 0xFFFD;.
b1100 20 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d 74 assert( c==t
b1110 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
b1120 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 (z-zBuf)==n );.
b1130 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c }. for(i=0; i<
b1140 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 0x00110000; i++)
b1150 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44 {. if( i>=0xD
b1160 38 30 30 20 26 26 20 69 3c 30 78 45 30 30 30 20 800 && i<0xE000
b1170 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
b1180 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52 z = zBuf;. WR
b1190 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 69 ITE_UTF16LE(z, i
b11a0 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 );. n = (int)
b11b0 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 (z-zBuf);. as
b11c0 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d sert( n>0 && n<=
b11d0 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 4 );. z[0] =
b11e0 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0;. z = zBuf;
b11f0 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 4c . READ_UTF16L
b1200 45 28 7a 2c 20 31 2c 20 63 29 3b 0a 20 20 20 20 E(z, 1, c);.
b1210 61 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a assert( c==i );.
b1220 20 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a assert( (z-z
b1230 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 Buf)==n );. }.
b1240 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 for(i=0; i<0x00
b1250 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 110000; i++){.
b1260 20 20 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 if( i>=0xD800
b1270 26 26 20 69 3c 30 78 45 30 30 30 20 29 20 63 6f && i<0xE000 ) co
b1280 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 ntinue;. z =
b1290 7a 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f zBuf;. WRITE_
b12a0 55 54 46 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20 UTF16BE(z, i);.
b12b0 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a n = (int)(z-z
b12c0 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65 72 74 Buf);. assert
b12d0 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b ( n>0 && n<=4 );
b12e0 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 . z[0] = 0;.
b12f0 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 z = zBuf;.
b1300 20 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c READ_UTF16BE(z,
b1310 20 31 2c 20 63 29 3b 0a 20 20 20 20 61 73 73 65 1, c);. asse
b1320 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20 20 20 rt( c==i );.
b1330 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 assert( (z-zBuf)
b1340 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e ==n );. }.}.#en
b1350 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 dif /* SQLITE_TE
b1360 53 54 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 ST */.#endif /*
b1370 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
b1380 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 6 */../*********
b1390 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 74 66 ***** End of utf
b13a0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
b13b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b13c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b13d0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
b13e0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
b13f0 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a util.c ********
b1400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1420 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 ****/./*.** 2001
b1430 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a September 15.**
b1440 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
b1450 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
b1460 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
b1470 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
b1480 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
b1490 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
b14a0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
b14b0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
b14c0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
b14d0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
b14e0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
b14f0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
b1500 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
b1510 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
b1520 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
b1530 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
b1540 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
b1550 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
b1560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
b15a0 55 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e Utility function
b15b0 73 20 75 73 65 64 20 74 68 72 6f 75 67 68 6f 75 s used throughou
b15c0 74 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 t sqlite..**.**
b15d0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
b15e0 6e 73 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 ns functions for
b15f0 20 61 6c 6c 6f 63 61 74 69 6e 67 20 6d 65 6d 6f allocating memo
b1600 72 79 2c 20 63 6f 6d 70 61 72 69 6e 67 0a 2a 2a ry, comparing.**
b1610 20 73 74 72 69 6e 67 73 2c 20 61 6e 64 20 73 74 strings, and st
b1620 75 66 66 20 6c 69 6b 65 20 74 68 61 74 2e 0a 2a uff like that..*
b1630 2a 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 *.*/.#ifdef SQLI
b1640 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 0a 23 20 TE_HAVE_ISNAN.#
b1650 69 6e 63 6c 75 64 65 20 3c 6d 61 74 68 2e 68 3e include <math.h>
b1660 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
b1670 6f 75 74 69 6e 65 20 6e 65 65 64 65 64 20 74 6f outine needed to
b1680 20 73 75 70 70 6f 72 74 20 74 68 65 20 74 65 73 support the tes
b1690 74 63 61 73 65 28 29 20 6d 61 63 72 6f 2e 0a 2a tcase() macro..*
b16a0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
b16b0 43 4f 56 45 52 41 47 45 5f 54 45 53 54 0a 53 51 COVERAGE_TEST.SQ
b16c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
b16d0 64 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67 d sqlite3Coverag
b16e0 65 28 69 6e 74 20 78 29 7b 0a 20 20 73 74 61 74 e(int x){. stat
b16f0 69 63 20 69 6e 74 20 64 75 6d 6d 79 20 3d 20 30 ic int dummy = 0
b1700 3b 0a 20 20 64 75 6d 6d 79 20 2b 3d 20 78 3b 0a ;. dummy += x;.
b1710 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
b1720 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 Return true if t
b1730 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e he floating poin
b1740 74 20 76 61 6c 75 65 20 69 73 20 4e 6f 74 20 61 t value is Not a
b1750 20 4e 75 6d 62 65 72 20 28 4e 61 4e 29 2e 0a 2a Number (NaN)..*
b1760 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 6d 61 74 *.** Use the mat
b1770 68 20 6c 69 62 72 61 72 79 20 69 73 6e 61 6e 28 h library isnan(
b1780 29 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 63 6f ) function if co
b1790 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49 mpiled with SQLI
b17a0 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 2e 0a 2a TE_HAVE_ISNAN..*
b17b0 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 77 65 20 * Otherwise, we
b17c0 68 61 76 65 20 6f 75 72 20 6f 77 6e 20 69 6d 70 have our own imp
b17d0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 lementation that
b17e0 20 77 6f 72 6b 73 20 6f 6e 20 6d 6f 73 74 20 73 works on most s
b17f0 79 73 74 65 6d 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ystems..*/.SQLIT
b1800 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
b1810 6c 69 74 65 33 49 73 4e 61 4e 28 64 6f 75 62 6c lite3IsNaN(doubl
b1820 65 20 78 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 e x){. int rc;
b1830 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 72 /* The value r
b1840 65 74 75 72 6e 20 2a 2f 0a 23 69 66 20 21 64 65 eturn */.#if !de
b1850 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 56 fined(SQLITE_HAV
b1860 45 5f 49 53 4e 41 4e 29 0a 20 20 2f 2a 0a 20 20 E_ISNAN). /*.
b1870 2a 2a 20 53 79 73 74 65 6d 73 20 74 68 61 74 20 ** Systems that
b1880 73 75 70 70 6f 72 74 20 74 68 65 20 69 73 6e 61 support the isna
b1890 6e 28 29 20 6c 69 62 72 61 72 79 20 66 75 6e 63 n() library func
b18a0 74 69 6f 6e 20 73 68 6f 75 6c 64 20 70 72 6f 62 tion should prob
b18b0 61 62 6c 79 0a 20 20 2a 2a 20 6d 61 6b 65 20 75 ably. ** make u
b18c0 73 65 20 6f 66 20 69 74 20 62 79 20 63 6f 6d 70 se of it by comp
b18d0 69 6c 69 6e 67 20 77 69 74 68 20 2d 44 53 51 4c iling with -DSQL
b18e0 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 2e 20 ITE_HAVE_ISNAN.
b18f0 20 42 75 74 20 77 65 20 68 61 76 65 0a 20 20 2a But we have. *
b1900 2a 20 66 6f 75 6e 64 20 74 68 61 74 20 6d 61 6e * found that man
b1910 79 20 73 79 73 74 65 6d 73 20 64 6f 20 6e 6f 74 y systems do not
b1920 20 68 61 76 65 20 61 20 77 6f 72 6b 69 6e 67 20 have a working
b1930 69 73 6e 61 6e 28 29 20 66 75 6e 63 74 69 6f 6e isnan() function
b1940 20 73 6f 0a 20 20 2a 2a 20 74 68 69 73 20 69 6d so. ** this im
b1950 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 plementation is
b1960 70 72 6f 76 69 64 65 64 20 61 73 20 61 6e 20 61 provided as an a
b1970 6c 74 65 72 6e 61 74 69 76 65 2e 0a 20 20 2a 2a lternative.. **
b1980 0a 20 20 2a 2a 20 54 68 69 73 20 4e 61 4e 20 74 . ** This NaN t
b1990 65 73 74 20 73 6f 6d 65 74 69 6d 65 73 20 66 61 est sometimes fa
b19a0 69 6c 73 20 69 66 20 63 6f 6d 70 69 6c 65 64 20 ils if compiled
b19b0 6f 6e 20 47 43 43 20 77 69 74 68 20 2d 66 66 61 on GCC with -ffa
b19c0 73 74 2d 6d 61 74 68 2e 0a 20 20 2a 2a 20 4f 6e st-math.. ** On
b19d0 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c the other hand,
b19e0 20 74 68 65 20 75 73 65 20 6f 66 20 2d 66 66 61 the use of -ffa
b19f0 73 74 2d 6d 61 74 68 20 63 6f 6d 65 73 20 77 69 st-math comes wi
b1a00 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 th the following
b1a10 0a 20 20 2a 2a 20 77 61 72 6e 69 6e 67 3a 0a 20 . ** warning:.
b1a20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 54 68 **. ** Th
b1a30 69 73 20 6f 70 74 69 6f 6e 20 5b 2d 66 66 61 73 is option [-ffas
b1a40 74 2d 6d 61 74 68 5d 20 73 68 6f 75 6c 64 20 6e t-math] should n
b1a50 65 76 65 72 20 62 65 20 74 75 72 6e 65 64 20 6f ever be turned o
b1a60 6e 20 62 79 20 61 6e 79 0a 20 20 2a 2a 20 20 20 n by any. **
b1a70 20 20 20 2d 4f 20 6f 70 74 69 6f 6e 20 73 69 6e -O option sin
b1a80 63 65 20 69 74 20 63 61 6e 20 72 65 73 75 6c 74 ce it can result
b1a90 20 69 6e 20 69 6e 63 6f 72 72 65 63 74 20 6f 75 in incorrect ou
b1aa0 74 70 75 74 20 66 6f 72 20 70 72 6f 67 72 61 6d tput for program
b1ab0 73 0a 20 20 2a 2a 20 20 20 20 20 20 77 68 69 63 s. ** whic
b1ac0 68 20 64 65 70 65 6e 64 20 6f 6e 20 61 6e 20 65 h depend on an e
b1ad0 78 61 63 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 xact implementat
b1ae0 69 6f 6e 20 6f 66 20 49 45 45 45 20 6f 72 20 49 ion of IEEE or I
b1af0 53 4f 20 0a 20 20 2a 2a 20 20 20 20 20 20 72 75 SO . ** ru
b1b00 6c 65 73 2f 73 70 65 63 69 66 69 63 61 74 69 6f les/specificatio
b1b10 6e 73 20 66 6f 72 20 6d 61 74 68 20 66 75 6e 63 ns for math func
b1b20 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a tions.. **. **
b1b30 20 55 6e 64 65 72 20 4d 53 56 43 2c 20 74 68 69 Under MSVC, thi
b1b40 73 20 4e 61 4e 20 74 65 73 74 20 6d 61 79 20 66 s NaN test may f
b1b50 61 69 6c 20 69 66 20 63 6f 6d 70 69 6c 65 64 20 ail if compiled
b1b60 77 69 74 68 20 61 20 66 6c 6f 61 74 69 6e 67 2d with a floating-
b1b70 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 70 72 65 63 . ** point prec
b1b80 69 73 69 6f 6e 20 6d 6f 64 65 20 6f 74 68 65 72 ision mode other
b1b90 20 74 68 61 6e 20 2f 66 70 3a 70 72 65 63 69 73 than /fp:precis
b1ba0 65 2e 20 20 46 72 6f 6d 20 74 68 65 20 4d 53 44 e. From the MSD
b1bb0 4e 20 0a 20 20 2a 2a 20 64 6f 63 75 6d 65 6e 74 N . ** document
b1bc0 61 74 69 6f 6e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a ation:. **. **
b1bd0 20 20 20 20 20 20 54 68 65 20 63 6f 6d 70 69 6c The compil
b1be0 65 72 20 5b 77 69 74 68 20 2f 66 70 3a 70 72 65 er [with /fp:pre
b1bf0 63 69 73 65 5d 20 77 69 6c 6c 20 70 72 6f 70 65 cise] will prope
b1c00 72 6c 79 20 68 61 6e 64 6c 65 20 63 6f 6d 70 61 rly handle compa
b1c10 72 69 73 6f 6e 73 20 0a 20 20 2a 2a 20 20 20 20 risons . **
b1c20 20 20 69 6e 76 6f 6c 76 69 6e 67 20 4e 61 4e 2e involving NaN.
b1c30 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 78 20 For example, x
b1c40 21 3d 20 78 20 65 76 61 6c 75 61 74 65 73 20 74 != x evaluates t
b1c50 6f 20 74 72 75 65 20 69 66 20 78 20 69 73 20 4e o true if x is N
b1c60 61 4e 20 0a 20 20 2a 2a 20 20 20 20 20 20 2e 2e aN . ** ..
b1c70 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 5f 5f .. */.#ifdef __
b1c80 46 41 53 54 5f 4d 41 54 48 5f 5f 0a 23 20 65 72 FAST_MATH__.# er
b1c90 72 6f 72 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 ror SQLite will
b1ca0 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 not work correct
b1cb0 6c 79 20 77 69 74 68 20 74 68 65 20 2d 66 66 61 ly with the -ffa
b1cc0 73 74 2d 6d 61 74 68 20 6f 70 74 69 6f 6e 20 6f st-math option o
b1cd0 66 20 47 43 43 2e 0a 23 65 6e 64 69 66 0a 20 20 f GCC..#endif.
b1ce0 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c 65 20 volatile double
b1cf0 79 20 3d 20 78 3b 0a 20 20 76 6f 6c 61 74 69 6c y = x;. volatil
b1d00 65 20 64 6f 75 62 6c 65 20 7a 20 3d 20 79 3b 0a e double z = y;.
b1d10 20 20 72 63 20 3d 20 28 79 21 3d 7a 29 3b 0a 23 rc = (y!=z);.#
b1d20 65 6c 73 65 20 20 2f 2a 20 69 66 20 64 65 66 69 else /* if defi
b1d30 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 56 45 5f ned(SQLITE_HAVE_
b1d40 49 53 4e 41 4e 29 20 2a 2f 0a 20 20 72 63 20 3d ISNAN) */. rc =
b1d50 20 69 73 6e 61 6e 28 78 29 3b 0a 23 65 6e 64 69 isnan(x);.#endi
b1d60 66 20 2f 2a 20 53 51 4c 49 54 45 5f 48 41 56 45 f /* SQLITE_HAVE
b1d70 5f 49 53 4e 41 4e 20 2a 2f 0a 20 20 74 65 73 74 _ISNAN */. test
b1d80 63 61 73 65 28 20 72 63 20 29 3b 0a 20 20 72 65 case( rc );. re
b1d90 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
b1da0 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72 69 * Compute a stri
b1db0 6e 67 20 6c 65 6e 67 74 68 20 74 68 61 74 20 69 ng length that i
b1dc0 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 77 68 61 s limited to wha
b1dd0 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64 20 t can be stored
b1de0 69 6e 0a 2a 2a 20 6c 6f 77 65 72 20 33 30 20 62 in.** lower 30 b
b1df0 69 74 73 20 6f 66 20 61 20 33 32 2d 62 69 74 20 its of a 32-bit
b1e00 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 0a signed integer..
b1e10 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 **.** The value
b1e20 72 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 6e 65 returned will ne
b1e30 76 65 72 20 62 65 20 6e 65 67 61 74 69 76 65 2e ver be negative.
b1e40 20 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 65 76 Nor will it ev
b1e50 65 72 20 62 65 20 67 72 65 61 74 65 72 0a 2a 2a er be greater.**
b1e60 20 74 68 61 6e 20 74 68 65 20 61 63 74 75 61 6c than the actual
b1e70 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 length of the s
b1e80 74 72 69 6e 67 2e 20 20 46 6f 72 20 76 65 72 79 tring. For very
b1e90 20 6c 6f 6e 67 20 73 74 72 69 6e 67 73 20 28 67 long strings (g
b1ea0 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 31 reater.** than 1
b1eb0 47 69 42 29 20 74 68 65 20 76 61 6c 75 65 20 72 GiB) the value r
b1ec0 65 74 75 72 6e 65 64 20 6d 69 67 68 74 20 62 65 eturned might be
b1ed0 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 74 less than the t
b1ee0 72 75 65 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 rue string lengt
b1ef0 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 h..*/.SQLITE_PRI
b1f00 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
b1f10 53 74 72 6c 65 6e 33 30 28 63 6f 6e 73 74 20 63 Strlen30(const c
b1f20 68 61 72 20 2a 7a 29 7b 0a 20 20 63 6f 6e 73 74 har *z){. const
b1f30 20 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 char *z2 = z;.
b1f40 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 if( z==0 ) retu
b1f50 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 2a rn 0;. while( *
b1f60 7a 32 20 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 20 z2 ){ z2++; }.
b1f70 72 65 74 75 72 6e 20 30 78 33 66 66 66 66 66 66 return 0x3ffffff
b1f80 66 20 26 20 28 69 6e 74 29 28 7a 32 20 2d 20 7a f & (int)(z2 - z
b1f90 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 );.}../*.** Set
b1fa0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
b1fb0 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 65 error code and e
b1fc0 72 72 6f 72 20 73 74 72 69 6e 67 20 66 6f 72 20 rror string for
b1fd0 74 68 65 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 the sqlite.** ha
b1fe0 6e 64 6c 65 20 22 64 62 22 2e 20 54 68 65 20 65 ndle "db". The e
b1ff0 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 65 74 rror code is set
b2000 20 74 6f 20 22 65 72 72 5f 63 6f 64 65 22 2e 0a to "err_code"..
b2010 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e **.** If it is n
b2020 6f 74 20 4e 55 4c 4c 2c 20 73 74 72 69 6e 67 20 ot NULL, string
b2030 7a 46 6f 72 6d 61 74 20 73 70 65 63 69 66 69 65 zFormat specifie
b2040 73 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 s the format of
b2050 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 73 74 72 the.** error str
b2060 69 6e 67 20 69 6e 20 74 68 65 20 73 74 79 6c 65 ing in the style
b2070 20 6f 66 20 74 68 65 20 70 72 69 6e 74 66 20 66 of the printf f
b2080 75 6e 63 74 69 6f 6e 73 3a 20 54 68 65 20 66 6f unctions: The fo
b2090 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 66 6f 72 6d 61 llowing.** forma
b20a0 74 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 t characters are
b20b0 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 allowed:.**.**
b20c0 20 20 20 20 20 25 73 20 20 20 20 20 20 49 6e 73 %s Ins
b20d0 65 72 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 ert a string.**
b20e0 20 20 20 20 20 25 7a 20 20 20 20 20 20 41 20 73 %z A s
b20f0 74 72 69 6e 67 20 74 68 61 74 20 73 68 6f 75 6c tring that shoul
b2100 64 20 62 65 20 66 72 65 65 64 20 61 66 74 65 72 d be freed after
b2110 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 20 use.** %d
b2120 20 20 20 20 20 49 6e 73 65 72 74 20 61 6e 20 69 Insert an i
b2130 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 25 nteger.** %
b2140 54 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 T Insert a
b2150 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53 token.** %S
b2160 20 20 20 20 20 20 49 6e 73 65 72 74 20 74 68 65 Insert the
b2170 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f first element o
b2180 66 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a f a SrcList.**.*
b2190 2a 20 7a 46 6f 72 6d 61 74 20 61 6e 64 20 61 6e * zFormat and an
b21a0 79 20 73 74 72 69 6e 67 20 74 6f 6b 65 6e 73 20 y string tokens
b21b0 74 68 61 74 20 66 6f 6c 6c 6f 77 20 69 74 20 61 that follow it a
b21c0 72 65 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 re assumed to be
b21d0 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 .** encoded in U
b21e0 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 63 TF-8..**.** To c
b21f0 6c 65 61 72 20 74 68 65 20 6d 6f 73 74 20 72 65 lear the most re
b2200 63 65 6e 74 20 65 72 72 6f 72 20 66 6f 72 20 73 cent error for s
b2210 71 6c 69 74 65 20 68 61 6e 64 6c 65 20 22 64 62 qlite handle "db
b2220 22 2c 20 73 71 6c 69 74 65 33 45 72 72 6f 72 0a ", sqlite3Error.
b2230 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c ** should be cal
b2240 6c 65 64 20 77 69 74 68 20 65 72 72 5f 63 6f 64 led with err_cod
b2250 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f e set to SQLITE_
b2260 4f 4b 20 61 6e 64 20 7a 46 6f 72 6d 61 74 20 73 OK and zFormat s
b2270 65 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 0a 2a et.** to NULL..*
b2280 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b2290 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 void sqlite3Err
b22a0 6f 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 or(sqlite3 *db,
b22b0 69 6e 74 20 65 72 72 5f 63 6f 64 65 2c 20 63 6f int err_code, co
b22c0 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 nst char *zForma
b22d0 74 2c 20 2e 2e 2e 29 7b 0a 20 20 69 66 28 20 64 t, ...){. if( d
b22e0 62 20 26 26 20 28 64 62 2d 3e 70 45 72 72 20 7c b && (db->pErr |
b22f0 7c 20 28 64 62 2d 3e 70 45 72 72 20 3d 20 73 71 | (db->pErr = sq
b2300 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62 lite3ValueNew(db
b2310 29 29 21 3d 30 29 20 29 7b 0a 20 20 20 20 64 62 ))!=0) ){. db
b2320 2d 3e 65 72 72 43 6f 64 65 20 3d 20 65 72 72 5f ->errCode = err_
b2330 63 6f 64 65 3b 0a 20 20 20 20 69 66 28 20 7a 46 code;. if( zF
b2340 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 20 20 63 ormat ){. c
b2350 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20 76 61 har *z;. va
b2360 5f 6c 69 73 74 20 61 70 3b 0a 20 20 20 20 20 20 _list ap;.
b2370 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f va_start(ap, zFo
b2380 72 6d 61 74 29 3b 0a 20 20 20 20 20 20 7a 20 3d rmat);. z =
b2390 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 sqlite3VMPrintf
b23a0 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 (db, zFormat, ap
b23b0 29 3b 0a 20 20 20 20 20 20 76 61 5f 65 6e 64 28 );. va_end(
b23c0 61 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 ap);. sqlit
b23d0 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62 e3ValueSetStr(db
b23e0 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 7a 2c 20 53 ->pErr, -1, z, S
b23f0 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 QLITE_UTF8, SQLI
b2400 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 TE_DYNAMIC);.
b2410 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 }else{. sq
b2420 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 lite3ValueSetStr
b2430 28 64 62 2d 3e 70 45 72 72 2c 20 30 2c 20 30 2c (db->pErr, 0, 0,
b2440 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 SQLITE_UTF8, SQ
b2450 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 LITE_STATIC);.
b2460 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
b2470 20 41 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 Add an error me
b2480 73 73 61 67 65 20 74 6f 20 70 50 61 72 73 65 2d ssage to pParse-
b2490 3e 7a 45 72 72 4d 73 67 20 61 6e 64 20 69 6e 63 >zErrMsg and inc
b24a0 72 65 6d 65 6e 74 20 70 50 61 72 73 65 2d 3e 6e rement pParse->n
b24b0 45 72 72 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c Err..** The foll
b24c0 6f 77 69 6e 67 20 66 6f 72 6d 61 74 74 69 6e 67 owing formatting
b24d0 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20 characters are
b24e0 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 allowed:.**.**
b24f0 20 20 20 20 25 73 20 20 20 20 20 20 49 6e 73 65 %s Inse
b2500 72 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20 rt a string.**
b2510 20 20 20 20 25 7a 20 20 20 20 20 20 41 20 73 74 %z A st
b2520 72 69 6e 67 20 74 68 61 74 20 73 68 6f 75 6c 64 ring that should
b2530 20 62 65 20 66 72 65 65 64 20 61 66 74 65 72 20 be freed after
b2540 75 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 20 20 use.** %d
b2550 20 20 20 20 49 6e 73 65 72 74 20 61 6e 20 69 6e Insert an in
b2560 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 25 54 teger.** %T
b2570 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 74 Insert a t
b2580 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20 oken.** %S
b2590 20 20 20 20 20 49 6e 73 65 72 74 20 74 68 65 20 Insert the
b25a0 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 first element of
b25b0 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a a SrcList.**.**
b25c0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 This function s
b25d0 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 74 6f hould be used to
b25e0 20 72 65 70 6f 72 74 20 61 6e 79 20 65 72 72 6f report any erro
b25f0 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 77 68 r that occurs wh
b2600 69 6c 73 74 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e ilst.** compilin
b2610 67 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 g an SQL stateme
b2620 6e 74 20 28 69 2e 65 2e 20 77 69 74 68 69 6e 20 nt (i.e. within
b2630 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 sqlite3_prepare(
b2640 29 29 2e 20 54 68 65 0a 2a 2a 20 6c 61 73 74 20 )). The.** last
b2650 74 68 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 thing the sqlite
b2660 33 5f 70 72 65 70 61 72 65 28 29 20 66 75 6e 63 3_prepare() func
b2670 74 69 6f 6e 20 64 6f 65 73 20 69 73 20 63 6f 70 tion does is cop
b2680 79 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 73 y the error.** s
b2690 74 6f 72 65 64 20 62 79 20 74 68 69 73 20 66 75 tored by this fu
b26a0 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 nction into the
b26b0 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 database handle
b26c0 75 73 69 6e 67 20 73 71 6c 69 74 65 33 45 72 72 using sqlite3Err
b26d0 6f 72 28 29 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f or()..** Functio
b26e0 6e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 n sqlite3Error()
b26f0 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 should be used
b2700 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 during statement
b2710 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 28 73 execution.** (s
b2720 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 65 74 qlite3_step() et
b2730 63 2e 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 c.)..*/.SQLITE_P
b2740 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
b2750 74 65 33 45 72 72 6f 72 4d 73 67 28 50 61 72 73 te3ErrorMsg(Pars
b2760 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 e *pParse, const
b2770 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 char *zFormat,
b2780 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 ...){. va_list
b2790 61 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 ap;. sqlite3 *d
b27a0 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
b27b0 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b pParse->nErr++
b27c0 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
b27d0 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 7a 45 e(db, pParse->zE
b27e0 72 72 4d 73 67 29 3b 0a 20 20 76 61 5f 73 74 61 rrMsg);. va_sta
b27f0 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b rt(ap, zFormat);
b2800 0a 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d . pParse->zErrM
b2810 73 67 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 sg = sqlite3VMPr
b2820 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 intf(db, zFormat
b2830 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 , ap);. va_end(
b2840 61 70 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 72 ap);. pParse->r
b2850 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
b2860 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 ;.}../*.** Clear
b2870 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
b2880 67 65 20 69 6e 20 70 50 61 72 73 65 2c 20 69 66 ge in pParse, if
b2890 20 61 6e 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 any.*/.SQLITE_P
b28a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
b28b0 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 50 61 te3ErrorClear(Pa
b28c0 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 rse *pParse){.
b28d0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50 sqlite3DbFree(pP
b28e0 61 72 73 65 2d 3e 64 62 2c 20 70 50 61 72 73 65 arse->db, pParse
b28f0 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 50 ->zErrMsg);. pP
b2900 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 arse->zErrMsg =
b2910 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 0;. pParse->nEr
b2920 72 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 r = 0;.}../*.**
b2930 43 6f 6e 76 65 72 74 20 61 6e 20 53 51 4c 2d 73 Convert an SQL-s
b2940 74 79 6c 65 20 71 75 6f 74 65 64 20 73 74 72 69 tyle quoted stri
b2950 6e 67 20 69 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c ng into a normal
b2960 20 73 74 72 69 6e 67 20 62 79 20 72 65 6d 6f 76 string by remov
b2970 69 6e 67 0a 2a 2a 20 74 68 65 20 71 75 6f 74 65 ing.** the quote
b2980 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 54 68 characters. Th
b2990 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 e conversion is
b29a0 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 done in-place.
b29b0 49 66 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 If the.** input
b29c0 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77 does not begin w
b29d0 69 74 68 20 61 20 71 75 6f 74 65 20 63 68 61 72 ith a quote char
b29e0 61 63 74 65 72 2c 20 74 68 65 6e 20 74 68 69 73 acter, then this
b29f0 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 routine.** is a
b2a00 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 no-op..**.** Th
b2a10 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 6d e input string m
b2a20 75 73 74 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d ust be zero-term
b2a30 69 6e 61 74 65 64 2e 20 20 41 20 6e 65 77 20 7a inated. A new z
b2a40 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 0a 2a ero-terminator.*
b2a50 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 * is added to th
b2a60 65 20 64 65 71 75 6f 74 65 64 20 73 74 72 69 6e e dequoted strin
b2a70 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 g..**.** The ret
b2a80 75 72 6e 20 76 61 6c 75 65 20 69 73 20 2d 31 20 urn value is -1
b2a90 69 66 20 6e 6f 20 64 65 71 75 6f 74 69 6e 67 20 if no dequoting
b2aa0 6f 63 63 75 72 73 20 6f 72 20 74 68 65 20 6c 65 occurs or the le
b2ab0 6e 67 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 64 ngth of the.** d
b2ac0 65 71 75 6f 74 65 64 20 73 74 72 69 6e 67 2c 20 equoted string,
b2ad0 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65 exclusive of the
b2ae0 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 zero terminator
b2af0 2c 20 69 66 20 64 65 71 75 6f 74 69 6e 67 20 64 , if dequoting d
b2b00 6f 65 73 0a 2a 2a 20 6f 63 63 75 72 2e 0a 2a 2a oes.** occur..**
b2b10 0a 2a 2a 20 32 30 30 32 2d 46 65 62 2d 31 34 3a .** 2002-Feb-14:
b2b20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
b2b30 20 65 78 74 65 6e 64 65 64 20 74 6f 20 72 65 6d extended to rem
b2b40 6f 76 65 20 4d 53 2d 41 63 63 65 73 73 20 73 74 ove MS-Access st
b2b50 79 6c 65 0a 2a 2a 20 62 72 61 63 6b 65 74 73 20 yle.** brackets
b2b60 66 72 6f 6d 20 61 72 6f 75 6e 64 20 69 64 65 6e from around iden
b2b70 74 69 66 65 72 73 2e 20 20 46 6f 72 20 65 78 61 tifers. For exa
b2b80 6d 70 6c 65 3a 20 20 22 5b 61 2d 62 2d 63 5d 22 mple: "[a-b-c]"
b2b90 20 62 65 63 6f 6d 65 73 0a 2a 2a 20 22 61 2d 62 becomes.** "a-b
b2ba0 2d 63 22 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 -c"..*/.SQLITE_P
b2bb0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b2bc0 65 33 44 65 71 75 6f 74 65 28 63 68 61 72 20 2a e3Dequote(char *
b2bd0 7a 29 7b 0a 20 20 63 68 61 72 20 71 75 6f 74 65 z){. char quote
b2be0 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 ;. int i, j;.
b2bf0 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 if( z==0 ) retur
b2c00 6e 20 2d 31 3b 0a 20 20 71 75 6f 74 65 20 3d 20 n -1;. quote =
b2c10 7a 5b 30 5d 3b 0a 20 20 73 77 69 74 63 68 28 20 z[0];. switch(
b2c20 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 63 61 73 quote ){. cas
b2c30 65 20 27 5c 27 27 3a 20 20 62 72 65 61 6b 3b 0a e '\'': break;.
b2c40 20 20 20 20 63 61 73 65 20 27 22 27 3a 20 20 20 case '"':
b2c50 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
b2c60 27 60 27 3a 20 20 20 62 72 65 61 6b 3b 20 20 20 '`': break;
b2c70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
b2c80 46 6f 72 20 4d 79 53 51 4c 20 63 6f 6d 70 61 74 For MySQL compat
b2c90 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 63 ibility */. c
b2ca0 61 73 65 20 27 5b 27 3a 20 20 20 71 75 6f 74 65 ase '[': quote
b2cb0 20 3d 20 27 5d 27 3b 20 20 62 72 65 61 6b 3b 20 = ']'; break;
b2cc0 20 2f 2a 20 46 6f 72 20 4d 53 20 53 71 6c 53 65 /* For MS SqlSe
b2cd0 72 76 65 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 rver compatibili
b2ce0 74 79 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c ty */. defaul
b2cf0 74 3a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b t: return -1;
b2d00 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 2c 20 . }. for(i=1,
b2d10 6a 3d 30 3b 20 41 4c 57 41 59 53 28 7a 5b 69 5d j=0; ALWAYS(z[i]
b2d20 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 ); i++){. if(
b2d30 20 7a 5b 69 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a z[i]==quote ){.
b2d40 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d if( z[i+1]
b2d50 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 ==quote ){.
b2d60 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74 z[j++] = quot
b2d70 65 3b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a e;. i++;.
b2d80 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
b2d90 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
b2da0 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
b2db0 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b z[j++] = z[
b2dc0 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 i];. }. }.
b2dd0 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 z[j] = 0;. retu
b2de0 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76 rn j;.}../* Conv
b2df0 65 6e 69 65 6e 74 20 73 68 6f 72 74 2d 68 61 6e enient short-han
b2e00 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 70 70 d */.#define Upp
b2e10 65 72 54 6f 4c 6f 77 65 72 20 73 71 6c 69 74 65 erToLower sqlite
b2e20 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 0a 0a 2f 3UpperToLower../
b2e30 2a 0a 2a 2a 20 53 6f 6d 65 20 73 79 73 74 65 6d *.** Some system
b2e40 73 20 68 61 76 65 20 73 74 72 69 63 6d 70 28 29 s have stricmp()
b2e50 2e 20 20 4f 74 68 65 72 73 20 68 61 76 65 20 73 . Others have s
b2e60 74 72 63 61 73 65 63 6d 70 28 29 2e 20 20 42 65 trcasecmp(). Be
b2e70 63 61 75 73 65 0a 2a 2a 20 74 68 65 72 65 20 69 cause.** there i
b2e80 73 20 6e 6f 20 63 6f 6e 73 69 73 74 65 6e 63 79 s no consistency
b2e90 2c 20 77 65 20 77 69 6c 6c 20 64 65 66 69 6e 65 , we will define
b2ea0 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c our own..*/.SQL
b2eb0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b2ec0 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 63 sqlite3StrICmp(c
b2ed0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 onst char *zLeft
b2ee0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 , const char *zR
b2ef0 69 67 68 74 29 7b 0a 20 20 72 65 67 69 73 74 65 ight){. registe
b2f00 72 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 r unsigned char
b2f10 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75 *a, *b;. a = (u
b2f20 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a nsigned char *)z
b2f30 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73 Left;. b = (uns
b2f40 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69 igned char *)zRi
b2f50 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 2a 61 ght;. while( *a
b2f60 21 3d 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f !=0 && UpperToLo
b2f70 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f wer[*a]==UpperTo
b2f80 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b Lower[*b]){ a++;
b2f90 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e b++; }. return
b2fa0 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 UpperToLower[*a
b2fb0 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 ] - UpperToLower
b2fc0 5b 2a 62 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 [*b];.}.SQLITE_A
b2fd0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 PI int sqlite3_s
b2fe0 74 72 6e 69 63 6d 70 28 63 6f 6e 73 74 20 63 68 trnicmp(const ch
b2ff0 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 ar *zLeft, const
b3000 20 63 68 61 72 20 2a 7a 52 69 67 68 74 2c 20 69 char *zRight, i
b3010 6e 74 20 4e 29 7b 0a 20 20 72 65 67 69 73 74 65 nt N){. registe
b3020 72 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 r unsigned char
b3030 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75 *a, *b;. a = (u
b3040 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a nsigned char *)z
b3050 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73 Left;. b = (uns
b3060 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69 igned char *)zRi
b3070 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d ght;. while( N-
b3080 2d 20 3e 20 30 20 26 26 20 2a 61 21 3d 30 20 26 - > 0 && *a!=0 &
b3090 26 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a & UpperToLower[*
b30a0 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72 a]==UpperToLower
b30b0 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b [*b]){ a++; b++;
b30c0 20 7d 0a 20 20 72 65 74 75 72 6e 20 4e 3c 30 20 }. return N<0
b30d0 3f 20 30 20 3a 20 55 70 70 65 72 54 6f 4c 6f 77 ? 0 : UpperToLow
b30e0 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 6f er[*a] - UpperTo
b30f0 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a Lower[*b];.}../*
b3100 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 .** Return TRUE
b3110 69 66 20 7a 20 69 73 20 61 20 70 75 72 65 20 6e if z is a pure n
b3120 75 6d 65 72 69 63 20 73 74 72 69 6e 67 2e 20 20 umeric string.
b3130 52 65 74 75 72 6e 20 46 41 4c 53 45 20 61 6e 64 Return FALSE and
b3140 20 6c 65 61 76 65 0a 2a 2a 20 2a 72 65 61 6c 6e leave.** *realn
b3150 75 6d 20 75 6e 63 68 61 6e 67 65 64 20 69 66 20 um unchanged if
b3160 74 68 65 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 the string conta
b3170 69 6e 73 20 61 6e 79 20 63 68 61 72 61 63 74 65 ins any characte
b3180 72 20 77 68 69 63 68 20 69 73 20 6e 6f 74 0a 2a r which is not.*
b3190 2a 20 70 61 72 74 20 6f 66 20 61 20 6e 75 6d 62 * part of a numb
b31a0 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 er..**.** If the
b31b0 20 73 74 72 69 6e 67 20 69 73 20 70 75 72 65 20 string is pure
b31c0 6e 75 6d 65 72 69 63 2c 20 73 65 74 20 2a 72 65 numeric, set *re
b31d0 61 6c 6e 75 6d 20 74 6f 20 54 52 55 45 20 69 66 alnum to TRUE if
b31e0 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 63 the string.** c
b31f0 6f 6e 74 61 69 6e 73 20 74 68 65 20 27 2e 27 20 ontains the '.'
b3200 63 68 61 72 61 63 74 65 72 20 6f 72 20 61 6e 20 character or an
b3210 22 45 2b 30 30 30 22 20 73 74 79 6c 65 20 65 78 "E+000" style ex
b3220 70 6f 6e 65 6e 74 69 61 74 69 6f 6e 20 73 75 66 ponentiation suf
b3230 66 69 78 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 fix..** Otherwis
b3240 65 20 73 65 74 20 2a 72 65 61 6c 6e 75 6d 20 74 e set *realnum t
b3250 6f 20 46 41 4c 53 45 2e 20 20 4e 6f 74 65 20 74 o FALSE. Note t
b3260 68 61 74 20 6a 75 73 74 20 62 65 63 61 75 65 20 hat just becaue
b3270 2a 72 65 61 6c 6e 75 6d 20 69 73 0a 2a 2a 20 66 *realnum is.** f
b3280 61 6c 73 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65 alse does not me
b3290 61 6e 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 an that the numb
b32a0 65 72 20 63 61 6e 20 62 65 20 73 75 63 63 65 73 er can be succes
b32b0 73 66 75 6c 6c 79 20 63 6f 6e 76 65 72 74 65 64 sfully converted
b32c0 20 69 6e 74 6f 0a 2a 2a 20 61 6e 20 69 6e 74 65 into.** an inte
b32d0 67 65 72 20 2d 20 69 74 20 6d 69 67 68 74 20 62 ger - it might b
b32e0 65 20 74 6f 6f 20 62 69 67 2e 0a 2a 2a 0a 2a 2a e too big..**.**
b32f0 20 41 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 An empty string
b3300 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 6e is considered n
b3310 6f 6e 2d 6e 75 6d 65 72 69 63 2e 0a 2a 2f 0a 53 on-numeric..*/.S
b3320 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
b3330 74 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 t sqlite3IsNumbe
b3340 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c r(const char *z,
b3350 20 69 6e 74 20 2a 72 65 61 6c 6e 75 6d 2c 20 75 int *realnum, u
b3360 38 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 69 6e 8 enc){. int in
b3370 63 72 20 3d 20 28 65 6e 63 3d 3d 53 51 4c 49 54 cr = (enc==SQLIT
b3380 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0a 20 20 69 E_UTF8?1:2);. i
b3390 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 f( enc==SQLITE_U
b33a0 54 46 31 36 42 45 20 29 20 7a 2b 2b 3b 0a 20 20 TF16BE ) z++;.
b33b0 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 7c 7c 20 2a if( *z=='-' || *
b33c0 7a 3d 3d 27 2b 27 20 29 20 7a 20 2b 3d 20 69 6e z=='+' ) z += in
b33d0 63 72 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 cr;. if( !sqlit
b33e0 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b e3Isdigit(*z) ){
b33f0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
b3400 20 7d 0a 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a }. z += incr;.
b3410 20 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 30 3b 0a *realnum = 0;.
b3420 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 while( sqlite3
b3430 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20 7a Isdigit(*z) ){ z
b3440 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 69 66 += incr; }. if
b3450 28 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 ( *z=='.' ){.
b3460 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 z += incr;.
b3470 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 if( !sqlite3Isdi
b3480 67 69 74 28 2a 7a 29 20 29 20 72 65 74 75 72 6e git(*z) ) return
b3490 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 0;. while( s
b34a0 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a qlite3Isdigit(*z
b34b0 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 ) ){ z += incr;
b34c0 7d 0a 20 20 20 20 2a 72 65 61 6c 6e 75 6d 20 3d }. *realnum =
b34d0 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a 7a 1;. }. if( *z
b34e0 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 =='e' || *z=='E'
b34f0 20 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63 ){. z += inc
b3500 72 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27 r;. if( *z=='
b3510 2b 27 20 7c 7c 20 2a 7a 3d 3d 27 2d 27 20 29 20 +' || *z=='-' )
b3520 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 z += incr;. i
b3530 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 f( !sqlite3Isdig
b3540 69 74 28 2a 7a 29 20 29 20 72 65 74 75 72 6e 20 it(*z) ) return
b3550 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 0;. while( sq
b3560 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 lite3Isdigit(*z)
b3570 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d ){ z += incr; }
b3580 0a 20 20 20 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 . *realnum =
b3590 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 1;. }. return
b35a0 2a 7a 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 *z==0;.}../*.**
b35b0 54 68 65 20 73 74 72 69 6e 67 20 7a 5b 5d 20 69 The string z[] i
b35c0 73 20 61 6e 20 41 53 43 49 49 20 72 65 70 72 65 s an ASCII repre
b35d0 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 72 sentation of a r
b35e0 65 61 6c 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 43 eal number..** C
b35f0 6f 6e 76 65 72 74 20 74 68 69 73 20 73 74 72 69 onvert this stri
b3600 6e 67 20 74 6f 20 61 20 64 6f 75 62 6c 65 2e 0a ng to a double..
b3610 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
b3620 6e 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 ne assumes that
b3630 7a 5b 5d 20 72 65 61 6c 6c 79 20 69 73 20 61 20 z[] really is a
b3640 76 61 6c 69 64 20 6e 75 6d 62 65 72 2e 20 20 49 valid number. I
b3650 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 2c 20 f it.** is not,
b3660 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 75 6e the result is un
b3670 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 defined..**.** T
b3680 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
b3690 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 sed instead of t
b36a0 68 65 20 6c 69 62 72 61 72 79 20 61 74 6f 66 28 he library atof(
b36b0 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 63 61 75 ) function becau
b36c0 73 65 0a 2a 2a 20 74 68 65 20 6c 69 62 72 61 72 se.** the librar
b36d0 79 20 61 74 6f 66 28 29 20 6d 69 67 68 74 20 77 y atof() might w
b36e0 61 6e 74 20 74 6f 20 75 73 65 20 22 2c 22 20 61 ant to use "," a
b36f0 73 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f s the decimal po
b3700 69 6e 74 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f int instead.** o
b3710 66 20 22 2e 22 20 64 65 70 65 6e 64 69 6e 67 20 f "." depending
b3720 6f 6e 20 68 6f 77 20 6c 6f 63 61 6c 65 20 69 73 on how locale is
b3730 20 73 65 74 2e 20 20 42 75 74 20 74 68 61 74 20 set. But that
b3740 77 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 would cause prob
b3750 6c 65 6d 73 0a 2a 2a 20 66 6f 72 20 53 51 4c 2e lems.** for SQL.
b3760 20 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 6e So this routin
b3770 65 20 61 6c 77 61 79 73 20 75 73 65 73 20 22 2e e always uses ".
b3780 22 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 " regardless of
b3790 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 locale..*/.SQLIT
b37a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
b37b0 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 20 lite3AtoF(const
b37c0 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 20 char *z, double
b37d0 2a 70 52 65 73 75 6c 74 29 7b 0a 23 69 66 6e 64 *pResult){.#ifnd
b37e0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ef SQLITE_OMIT_F
b37f0 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 LOATING_POINT.
b3800 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 42 65 67 const char *zBeg
b3810 69 6e 20 3d 20 7a 3b 0a 20 20 2f 2a 20 73 69 67 in = z;. /* sig
b3820 6e 20 2a 20 73 69 67 6e 69 66 69 63 61 6e 64 20 n * significand
b3830 2a 20 28 31 30 20 5e 20 28 65 73 69 67 6e 20 2a * (10 ^ (esign *
b3840 20 65 78 70 6f 6e 65 6e 74 29 29 20 2a 2f 0a 20 exponent)) */.
b3850 20 69 6e 74 20 73 69 67 6e 20 3d 20 31 3b 20 20 int sign = 1;
b3860 20 2f 2a 20 73 69 67 6e 20 6f 66 20 73 69 67 6e /* sign of sign
b3870 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 36 34 ificand */. i64
b3880 20 73 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 s = 0; /*
b3890 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 significand */.
b38a0 20 69 6e 74 20 64 20 3d 20 30 3b 20 20 20 20 20 int d = 0;
b38b0 20 2f 2a 20 61 64 6a 75 73 74 20 65 78 70 6f 6e /* adjust expon
b38c0 65 6e 74 20 66 6f 72 20 73 68 69 66 74 69 6e 67 ent for shifting
b38d0 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a decimal point *
b38e0 2f 0a 20 20 69 6e 74 20 65 73 69 67 6e 20 3d 20 /. int esign =
b38f0 31 3b 20 20 2f 2a 20 73 69 67 6e 20 6f 66 20 65 1; /* sign of e
b3900 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 xponent */. int
b3910 20 65 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 e = 0; /*
b3920 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 64 6f exponent */. do
b3930 75 62 6c 65 20 72 65 73 75 6c 74 3b 0a 20 20 69 uble result;. i
b3940 6e 74 20 6e 44 69 67 69 74 73 20 3d 20 30 3b 0a nt nDigits = 0;.
b3950 0a 20 20 2f 2a 20 73 6b 69 70 20 6c 65 61 64 69 . /* skip leadi
b3960 6e 67 20 73 70 61 63 65 73 20 2a 2f 0a 20 20 77 ng spaces */. w
b3970 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 hile( sqlite3Iss
b3980 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b 3b 0a pace(*z) ) z++;.
b3990 20 20 2f 2a 20 67 65 74 20 73 69 67 6e 20 6f 66 /* get sign of
b39a0 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a significand */.
b39b0 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b if( *z=='-' ){
b39c0 0a 20 20 20 20 73 69 67 6e 20 3d 20 2d 31 3b 0a . sign = -1;.
b39d0 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 65 6c 73 65 z++;. }else
b39e0 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a if( *z=='+' ){.
b39f0 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 2f z++;. }. /
b3a00 2a 20 73 6b 69 70 20 6c 65 61 64 69 6e 67 20 7a * skip leading z
b3a10 65 72 6f 65 73 20 2a 2f 0a 20 20 77 68 69 6c 65 eroes */. while
b3a20 28 20 7a 5b 30 5d 3d 3d 27 30 27 20 29 20 7a 2b ( z[0]=='0' ) z+
b3a30 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 3b 0a 0a 20 +, nDigits++;..
b3a40 20 2f 2a 20 63 6f 70 79 20 6d 61 78 20 73 69 67 /* copy max sig
b3a50 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 73 20 nificant digits
b3a60 74 6f 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a to significand *
b3a70 2f 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 /. while( sqlit
b3a80 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 26 26 e3Isdigit(*z) &&
b3a90 20 73 3c 28 28 4c 41 52 47 45 53 54 5f 49 4e 54 s<((LARGEST_INT
b3aa0 36 34 2d 39 29 2f 31 30 29 20 29 7b 0a 20 20 20 64-9)/10) ){.
b3ab0 20 73 20 3d 20 73 2a 31 30 20 2b 20 28 2a 7a 20 s = s*10 + (*z
b3ac0 2d 20 27 30 27 29 3b 0a 20 20 20 20 7a 2b 2b 2c - '0');. z++,
b3ad0 20 6e 44 69 67 69 74 73 2b 2b 3b 0a 20 20 7d 0a nDigits++;. }.
b3ae0 20 20 2f 2a 20 73 6b 69 70 20 6e 6f 6e 2d 73 69 /* skip non-si
b3af0 67 6e 69 66 69 63 61 6e 74 20 73 69 67 6e 69 66 gnificant signif
b3b00 69 63 61 6e 64 20 64 69 67 69 74 73 0a 20 20 2a icand digits. *
b3b10 2a 20 28 69 6e 63 72 65 61 73 65 20 65 78 70 6f * (increase expo
b3b20 6e 65 6e 74 20 62 79 20 64 20 74 6f 20 73 68 69 nent by d to shi
b3b30 66 74 20 64 65 63 69 6d 61 6c 20 6c 65 66 74 29 ft decimal left)
b3b40 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 73 71 6c */. while( sql
b3b50 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 ite3Isdigit(*z)
b3b60 29 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b ) z++, nDigits++
b3b70 2c 20 64 2b 2b 3b 0a 0a 20 20 2f 2a 20 69 66 20 , d++;.. /* if
b3b80 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 73 decimal point is
b3b90 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 66 present */. if
b3ba0 28 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 ( *z=='.' ){.
b3bb0 20 7a 2b 2b 3b 0a 20 20 20 20 2f 2a 20 63 6f 70 z++;. /* cop
b3bc0 79 20 64 69 67 69 74 73 20 66 72 6f 6d 20 61 66 y digits from af
b3bd0 74 65 72 20 64 65 63 69 6d 61 6c 20 74 6f 20 73 ter decimal to s
b3be0 69 67 6e 69 66 69 63 61 6e 64 0a 20 20 20 20 2a ignificand. *
b3bf0 2a 20 28 64 65 63 72 65 61 73 65 20 65 78 70 6f * (decrease expo
b3c00 6e 65 6e 74 20 62 79 20 64 20 74 6f 20 73 68 69 nent by d to shi
b3c10 66 74 20 64 65 63 69 6d 61 6c 20 72 69 67 68 74 ft decimal right
b3c20 29 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 ) */. while(
b3c30 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a sqlite3Isdigit(*
b3c40 7a 29 20 26 26 20 73 3c 28 28 4c 41 52 47 45 53 z) && s<((LARGES
b3c50 54 5f 49 4e 54 36 34 2d 39 29 2f 31 30 29 20 29 T_INT64-9)/10) )
b3c60 7b 0a 20 20 20 20 20 20 73 20 3d 20 73 2a 31 30 {. s = s*10
b3c70 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 + (*z - '0');.
b3c80 20 20 20 20 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 z++, nDigit
b3c90 73 2b 2b 2c 20 64 2d 2d 3b 0a 20 20 20 20 7d 0a s++, d--;. }.
b3ca0 20 20 20 20 2f 2a 20 73 6b 69 70 20 6e 6f 6e 2d /* skip non-
b3cb0 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 significant digi
b3cc0 74 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 ts */. while(
b3cd0 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 sqlite3Isdigit(
b3ce0 2a 7a 29 20 29 20 7a 2b 2b 2c 20 6e 44 69 67 69 *z) ) z++, nDigi
b3cf0 74 73 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 ts++;. }.. /*
b3d00 69 66 20 65 78 70 6f 6e 65 6e 74 20 69 73 20 70 if exponent is p
b3d10 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 66 28 20 resent */. if(
b3d20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 *z=='e' || *z=='
b3d30 45 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 E' ){. z++;.
b3d40 20 20 20 2f 2a 20 67 65 74 20 73 69 67 6e 20 6f /* get sign o
b3d50 66 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 f exponent */.
b3d60 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b if( *z=='-' ){
b3d70 0a 20 20 20 20 20 20 65 73 69 67 6e 20 3d 20 2d . esign = -
b3d80 31 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 1;. z++;.
b3d90 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d }else if( *z==
b3da0 27 2b 27 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b '+' ){. z++
b3db0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 63 ;. }. /* c
b3dc0 6f 70 79 20 64 69 67 69 74 73 20 74 6f 20 65 78 opy digits to ex
b3dd0 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 77 68 ponent */. wh
b3de0 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 ile( sqlite3Isdi
b3df0 67 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20 git(*z) ){.
b3e00 20 65 20 3d 20 65 2a 31 30 20 2b 20 28 2a 7a 20 e = e*10 + (*z
b3e10 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 20 7a 2b - '0');. z+
b3e20 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 +;. }. }..
b3e30 2f 2a 20 61 64 6a 75 73 74 20 65 78 70 6f 6e 65 /* adjust expone
b3e40 6e 74 20 62 79 20 64 2c 20 61 6e 64 20 75 70 64 nt by d, and upd
b3e50 61 74 65 20 73 69 67 6e 20 2a 2f 0a 20 20 65 20 ate sign */. e
b3e60 3d 20 28 65 2a 65 73 69 67 6e 29 20 2b 20 64 3b = (e*esign) + d;
b3e70 0a 20 20 69 66 28 20 65 3c 30 20 29 20 7b 0a 20 . if( e<0 ) {.
b3e80 20 20 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 esign = -1;.
b3e90 20 20 20 65 20 2a 3d 20 2d 31 3b 0a 20 20 7d 20 e *= -1;. }
b3ea0 65 6c 73 65 20 7b 0a 20 20 20 20 65 73 69 67 6e else {. esign
b3eb0 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 = 1;. }.. /*
b3ec0 69 66 20 30 20 73 69 67 6e 69 66 69 63 61 6e 64 if 0 significand
b3ed0 20 2a 2f 0a 20 20 69 66 28 20 21 73 20 29 20 7b */. if( !s ) {
b3ee0 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 65 20 49 . /* In the I
b3ef0 45 45 45 20 37 35 34 20 73 74 61 6e 64 61 72 64 EEE 754 standard
b3f00 2c 20 7a 65 72 6f 20 69 73 20 73 69 67 6e 65 64 , zero is signed
b3f10 2e 0a 20 20 20 20 2a 2a 20 41 64 64 20 74 68 65 .. ** Add the
b3f20 20 73 69 67 6e 20 69 66 20 77 65 27 76 65 20 73 sign if we've s
b3f30 65 65 6e 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 een at least one
b3f40 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 72 65 digit */. re
b3f50 73 75 6c 74 20 3d 20 28 73 69 67 6e 3c 30 20 26 sult = (sign<0 &
b3f60 26 20 6e 44 69 67 69 74 73 29 20 3f 20 2d 28 64 & nDigits) ? -(d
b3f70 6f 75 62 6c 65 29 30 20 3a 20 28 64 6f 75 62 6c ouble)0 : (doubl
b3f80 65 29 30 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a e)0;. } else {.
b3f90 20 20 20 20 2f 2a 20 61 74 74 65 6d 70 74 20 74 /* attempt t
b3fa0 6f 20 72 65 64 75 63 65 20 65 78 70 6f 6e 65 6e o reduce exponen
b3fb0 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 73 69 t */. if( esi
b3fc0 67 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 77 68 gn>0 ){. wh
b3fd0 69 6c 65 28 20 73 3c 28 4c 41 52 47 45 53 54 5f ile( s<(LARGEST_
b3fe0 49 4e 54 36 34 2f 31 30 29 20 26 26 20 65 3e 30 INT64/10) && e>0
b3ff0 20 29 20 65 2d 2d 2c 73 2a 3d 31 30 3b 0a 20 20 ) e--,s*=10;.
b4000 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 77 }else{. w
b4010 68 69 6c 65 28 20 21 28 73 25 31 30 29 20 26 26 hile( !(s%10) &&
b4020 20 65 3e 30 20 29 20 65 2d 2d 2c 73 2f 3d 31 30 e>0 ) e--,s/=10
b4030 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
b4040 61 64 6a 75 73 74 20 74 68 65 20 73 69 67 6e 20 adjust the sign
b4050 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a of significand *
b4060 2f 0a 20 20 20 20 73 20 3d 20 73 69 67 6e 3c 30 /. s = sign<0
b4070 20 3f 20 2d 73 20 3a 20 73 3b 0a 0a 20 20 20 20 ? -s : s;..
b4080 2f 2a 20 69 66 20 65 78 70 6f 6e 65 6e 74 2c 20 /* if exponent,
b4090 73 63 61 6c 65 20 73 69 67 6e 69 66 69 63 61 6e scale significan
b40a0 64 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 d as appropriate
b40b0 0a 20 20 20 20 2a 2a 20 61 6e 64 20 73 74 6f 72 . ** and stor
b40c0 65 20 69 6e 20 72 65 73 75 6c 74 2e 20 2a 2f 0a e in result. */.
b40d0 20 20 20 20 69 66 28 20 65 20 29 7b 0a 20 20 20 if( e ){.
b40e0 20 20 20 64 6f 75 62 6c 65 20 73 63 61 6c 65 20 double scale
b40f0 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 2f 2a 20 = 1.0;. /*
b4100 61 74 74 65 6d 70 74 20 74 6f 20 68 61 6e 64 6c attempt to handl
b4110 65 20 65 78 74 72 65 6d 65 6c 79 20 73 6d 61 6c e extremely smal
b4120 6c 2f 6c 61 72 67 65 20 6e 75 6d 62 65 72 73 20 l/large numbers
b4130 62 65 74 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 better */.
b4140 69 66 28 20 65 3e 33 30 37 20 26 26 20 65 3c 33 if( e>307 && e<3
b4150 34 32 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68 42 ){. wh
b4160 69 6c 65 28 20 65 25 33 30 38 20 29 20 7b 20 73 ile( e%308 ) { s
b4170 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 3b 20 cale *= 1.0e+1;
b4180 65 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 20 20 20 e -= 1; }.
b4190 20 20 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b if( esign<0 ){
b41a0 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c . resul
b41b0 74 20 3d 20 73 20 2f 20 73 63 61 6c 65 3b 0a 20 t = s / scale;.
b41c0 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 20 result
b41d0 2f 3d 20 31 2e 30 65 2b 33 30 38 3b 0a 20 20 20 /= 1.0e+308;.
b41e0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
b41f0 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73 result = s
b4200 20 2a 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20 * scale;.
b4210 20 20 20 20 72 65 73 75 6c 74 20 2a 3d 20 31 2e result *= 1.
b4220 30 65 2b 33 30 38 3b 0a 20 20 20 20 20 20 20 20 0e+308;.
b4230 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
b4240 20 20 20 20 20 20 20 2f 2a 20 31 2e 30 65 2b 32 /* 1.0e+2
b4250 32 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74 2 is the largest
b4260 20 70 6f 77 65 72 20 6f 66 20 31 30 20 74 68 61 power of 10 tha
b4270 6e 20 63 61 6e 20 62 65 20 0a 20 20 20 20 20 20 n can be .
b4280 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 65 64 ** represented
b4290 20 65 78 61 63 74 6c 79 2e 20 2a 2f 0a 20 20 20 exactly. */.
b42a0 20 20 20 20 20 77 68 69 6c 65 28 20 65 25 32 32 while( e%22
b42b0 20 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e ) { scale *= 1.
b42c0 30 65 2b 31 3b 20 65 20 2d 3d 20 31 3b 20 7d 0a 0e+1; e -= 1; }.
b42d0 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 65 while( e
b42e0 3e 30 20 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20 >0 ) { scale *=
b42f0 31 2e 30 65 2b 32 32 3b 20 65 20 2d 3d 20 32 32 1.0e+22; e -= 22
b4300 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 ; }. if(
b4310 65 73 69 67 6e 3c 30 20 29 7b 0a 20 20 20 20 20 esign<0 ){.
b4320 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73 20 result = s
b4330 2f 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20 20 / scale;.
b4340 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
b4350 20 20 72 65 73 75 6c 74 20 3d 20 73 20 2a 20 73 result = s * s
b4360 63 61 6c 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a cale;. }.
b4370 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c }. } el
b4380 73 65 20 7b 0a 20 20 20 20 20 20 72 65 73 75 6c se {. resul
b4390 74 20 3d 20 28 64 6f 75 62 6c 65 29 73 3b 0a 20 t = (double)s;.
b43a0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 73 }. }.. /* s
b43b0 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 tore the result
b43c0 2a 2f 0a 20 20 2a 70 52 65 73 75 6c 74 20 3d 20 */. *pResult =
b43d0 72 65 73 75 6c 74 3b 0a 0a 20 20 2f 2a 20 72 65 result;.. /* re
b43e0 74 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66 20 63 turn number of c
b43f0 68 61 72 61 63 74 65 72 73 20 75 73 65 64 20 2a haracters used *
b4400 2f 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 /. return (int)
b4410 28 7a 20 2d 20 7a 42 65 67 69 6e 29 3b 0a 23 65 (z - zBegin);.#e
b4420 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 73 71 6c lse. return sql
b4430 69 74 65 33 41 74 6f 69 36 34 28 7a 2c 20 70 52 ite3Atoi64(z, pR
b4440 65 73 75 6c 74 29 3b 0a 23 65 6e 64 69 66 20 2f esult);.#endif /
b4450 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c * SQLITE_OMIT_FL
b4460 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a OATING_POINT */.
b4470 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 }../*.** Compare
b4480 20 74 68 65 20 31 39 2d 63 68 61 72 61 63 74 65 the 19-characte
b4490 72 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 61 67 r string zNum ag
b44a0 61 69 6e 73 74 20 74 68 65 20 74 65 78 74 20 72 ainst the text r
b44b0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a epresentation.**
b44c0 20 76 61 6c 75 65 20 32 5e 36 33 3a 20 20 39 32 value 2^63: 92
b44d0 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 2337203685477580
b44e0 38 2e 20 20 52 65 74 75 72 6e 20 6e 65 67 61 74 8. Return negat
b44f0 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f ive, zero, or po
b4500 73 69 74 69 76 65 0a 2a 2a 20 69 66 20 7a 4e 75 sitive.** if zNu
b4510 6d 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 m is less than,
b4520 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 equal to, or gre
b4530 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 73 74 ater than the st
b4540 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 69 ring..**.** Unli
b4550 6b 65 20 6d 65 6d 63 6d 70 28 29 20 74 68 69 73 ke memcmp() this
b4560 20 72 6f 75 74 69 6e 65 20 69 73 20 67 75 61 72 routine is guar
b4570 61 6e 74 65 65 64 20 74 6f 20 72 65 74 75 72 6e anteed to return
b4580 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 0a the difference.
b4590 2a 2a 20 69 6e 20 74 68 65 20 76 61 6c 75 65 73 ** in the values
b45a0 20 6f 66 20 74 68 65 20 6c 61 73 74 20 64 69 67 of the last dig
b45b0 69 74 20 69 66 20 74 68 65 20 6f 6e 6c 79 20 64 it if the only d
b45c0 69 66 66 65 72 65 6e 63 65 20 69 73 20 69 6e 20 ifference is in
b45d0 74 68 65 0a 2a 2a 20 6c 61 73 74 20 64 69 67 69 the.** last digi
b45e0 74 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d t. So, for exam
b45f0 70 6c 65 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 ple,.**.**
b4600 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 22 39 compare2pow63("9
b4610 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 2233720368547758
b4620 30 30 22 29 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 00").**.** will
b4630 72 65 74 75 72 6e 20 2d 38 2e 0a 2a 2f 0a 73 74 return -8..*/.st
b4640 61 74 69 63 20 69 6e 74 20 63 6f 6d 70 61 72 65 atic int compare
b4650 32 70 6f 77 36 33 28 63 6f 6e 73 74 20 63 68 61 2pow63(const cha
b4660 72 20 2a 7a 4e 75 6d 29 7b 0a 20 20 69 6e 74 20 r *zNum){. int
b4670 63 3b 0a 20 20 63 20 3d 20 6d 65 6d 63 6d 70 28 c;. c = memcmp(
b4680 7a 4e 75 6d 2c 22 39 32 32 33 33 37 32 30 33 36 zNum,"9223372036
b4690 38 35 34 37 37 35 38 30 22 2c 31 38 29 2a 31 30 85477580",18)*10
b46a0 3b 0a 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a ;. if( c==0 ){.
b46b0 20 20 20 20 63 20 3d 20 7a 4e 75 6d 5b 31 38 5d c = zNum[18]
b46c0 20 2d 20 27 38 27 3b 0a 20 20 7d 0a 20 20 72 65 - '8';. }. re
b46d0 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a turn c;.}.../*.*
b46e0 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 * Return TRUE if
b46f0 20 7a 4e 75 6d 20 69 73 20 61 20 36 34 2d 62 69 zNum is a 64-bi
b4700 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 t signed integer
b4710 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 and write.** th
b4720 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 e value of the i
b4730 6e 74 65 67 65 72 20 69 6e 74 6f 20 2a 70 4e 75 nteger into *pNu
b4740 6d 2e 20 20 49 66 20 7a 4e 75 6d 20 69 73 20 6e m. If zNum is n
b4750 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a ot an integer.**
b4760 20 6f 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65 or is an intege
b4770 72 20 74 68 61 74 20 69 73 20 74 6f 6f 20 6c 61 r that is too la
b4780 72 67 65 20 74 6f 20 62 65 20 65 78 70 72 65 73 rge to be expres
b4790 73 65 64 20 77 69 74 68 20 36 34 20 62 69 74 73 sed with 64 bits
b47a0 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e ,.** then return
b47b0 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 false..**.** Wh
b47c0 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 en this routine
b47d0 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 77 was originally w
b47e0 72 69 74 74 65 6e 20 69 74 20 64 65 61 6c 74 20 ritten it dealt
b47f0 77 69 74 68 20 6f 6e 6c 79 0a 2a 2a 20 33 32 2d with only.** 32-
b4800 62 69 74 20 6e 75 6d 62 65 72 73 2e 20 20 41 74 bit numbers. At
b4810 20 74 68 61 74 20 74 69 6d 65 2c 20 69 74 20 77 that time, it w
b4820 61 73 20 6d 75 63 68 20 66 61 73 74 65 72 20 74 as much faster t
b4830 68 61 6e 20 74 68 65 0a 2a 2a 20 61 74 6f 69 28 han the.** atoi(
b4840 29 20 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e ) library routin
b4850 65 20 69 6e 20 52 65 64 48 61 74 20 37 2e 32 2e e in RedHat 7.2.
b4860 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b4870 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 TE int sqlite3At
b4880 6f 69 36 34 28 63 6f 6e 73 74 20 63 68 61 72 20 oi64(const char
b4890 2a 7a 4e 75 6d 2c 20 69 36 34 20 2a 70 4e 75 6d *zNum, i64 *pNum
b48a0 29 7b 0a 20 20 69 36 34 20 76 20 3d 20 30 3b 0a ){. i64 v = 0;.
b48b0 20 20 69 6e 74 20 6e 65 67 3b 0a 20 20 69 6e 74 int neg;. int
b48c0 20 69 2c 20 63 3b 0a 20 20 63 6f 6e 73 74 20 63 i, c;. const c
b48d0 68 61 72 20 2a 7a 53 74 61 72 74 3b 0a 20 20 77 har *zStart;. w
b48e0 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 hile( sqlite3Iss
b48f0 70 61 63 65 28 2a 7a 4e 75 6d 29 20 29 20 7a 4e pace(*zNum) ) zN
b4900 75 6d 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 4e 75 um++;. if( *zNu
b4910 6d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 m=='-' ){. ne
b4920 67 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b g = 1;. zNum+
b4930 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a +;. }else if( *
b4940 7a 4e 75 6d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 zNum=='+' ){.
b4950 20 6e 65 67 20 3d 20 30 3b 0a 20 20 20 20 7a 4e neg = 0;. zN
b4960 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 um++;. }else{.
b4970 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20 7d 0a neg = 0;. }.
b4980 20 20 7a 53 74 61 72 74 20 3d 20 7a 4e 75 6d 3b zStart = zNum;
b4990 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 . while( zNum[0
b49a0 5d 3d 3d 27 30 27 20 29 7b 20 7a 4e 75 6d 2b 2b ]=='0' ){ zNum++
b49b0 3b 20 7d 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72 ; } /* Skip over
b49c0 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 leading zeros.
b49d0 54 69 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a Ticket #2454 */.
b49e0 20 20 66 6f 72 28 69 3d 30 3b 20 28 63 3d 7a 4e for(i=0; (c=zN
b49f0 75 6d 5b 69 5d 29 3e 3d 27 30 27 20 26 26 20 63 um[i])>='0' && c
b4a00 3c 3d 27 39 27 3b 20 69 2b 2b 29 7b 0a 20 20 20 <='9'; i++){.
b4a10 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20 v = v*10 + c -
b4a20 27 30 27 3b 0a 20 20 7d 0a 20 20 2a 70 4e 75 6d '0';. }. *pNum
b4a30 20 3d 20 6e 65 67 20 3f 20 2d 76 20 3a 20 76 3b = neg ? -v : v;
b4a40 0a 20 20 69 66 28 20 63 21 3d 30 20 7c 7c 20 28 . if( c!=0 || (
b4a50 69 3d 3d 30 20 26 26 20 7a 53 74 61 72 74 3d 3d i==0 && zStart==
b4a60 7a 4e 75 6d 29 20 7c 7c 20 69 3e 31 39 20 29 7b zNum) || i>19 ){
b4a70 0a 20 20 20 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 . /* zNum is
b4a80 65 6d 70 74 79 20 6f 72 20 63 6f 6e 74 61 69 6e empty or contain
b4a90 73 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 74 65 s non-numeric te
b4aa0 78 74 20 6f 72 20 69 73 20 6c 6f 6e 67 65 72 0a xt or is longer.
b4ab0 20 20 20 20 2a 2a 20 74 68 61 6e 20 31 39 20 64 ** than 19 d
b4ac0 69 67 69 74 73 20 28 74 68 75 73 20 67 75 61 72 igits (thus guar
b4ad0 61 6e 74 69 6e 67 20 74 68 61 74 20 69 74 20 69 anting that it i
b4ae0 73 20 74 6f 6f 20 6c 61 72 67 65 29 20 2a 2f 0a s too large) */.
b4af0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
b4b00 7d 65 6c 73 65 20 69 66 28 20 69 3c 31 39 20 29 }else if( i<19 )
b4b10 7b 0a 20 20 20 20 2f 2a 20 4c 65 73 73 20 74 68 {. /* Less th
b4b20 61 6e 20 31 39 20 64 69 67 69 74 73 2c 20 73 6f an 19 digits, so
b4b30 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 74 we know that it
b4b40 20 66 69 74 73 20 69 6e 20 36 34 20 62 69 74 73 fits in 64 bits
b4b50 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 */. return 1
b4b60 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
b4b70 2a 20 31 39 2d 64 69 67 69 74 20 6e 75 6d 62 65 * 19-digit numbe
b4b80 72 73 20 6d 75 73 74 20 62 65 20 6e 6f 20 6c 61 rs must be no la
b4b90 72 67 65 72 20 74 68 61 6e 20 39 32 32 33 33 37 rger than 922337
b4ba0 32 30 33 36 38 35 34 37 37 35 38 30 37 20 69 66 2036854775807 if
b4bb0 20 70 6f 73 69 74 69 76 65 0a 20 20 20 20 2a 2a positive. **
b4bc0 20 6f 72 20 39 32 32 33 33 37 32 30 33 36 38 35 or 922337203685
b4bd0 34 37 37 35 38 30 38 20 69 66 20 6e 65 67 61 74 4775808 if negat
b4be0 69 76 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 ive. Note that
b4bf0 39 32 32 33 33 37 32 30 33 36 38 35 34 36 36 35 9223372036854665
b4c00 38 30 38 0a 20 20 20 20 2a 2a 20 69 73 20 32 5e 808. ** is 2^
b4c10 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 63. */. retur
b4c20 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 n compare2pow63(
b4c30 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d zNum)<neg;. }.}
b4c40 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 ../*.** The stri
b4c50 6e 67 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e ng zNum represen
b4c60 74 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 69 ts an unsigned i
b4c70 6e 74 65 67 65 72 2e 20 20 54 68 65 20 7a 4e 75 nteger. The zNu
b4c80 6d 20 73 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e 73 m string.** cons
b4c90 69 73 74 73 20 6f 66 20 6f 6e 65 20 6f 72 20 6d ists of one or m
b4ca0 6f 72 65 20 64 69 67 69 74 20 63 68 61 72 61 63 ore digit charac
b4cb0 74 65 72 73 20 61 6e 64 20 69 73 20 74 65 72 6d ters and is term
b4cc0 69 6e 61 74 65 64 20 62 79 0a 2a 2a 20 61 20 7a inated by.** a z
b4cd0 65 72 6f 20 63 68 61 72 61 63 74 65 72 2e 20 20 ero character.
b4ce0 41 6e 79 20 73 74 72 61 79 20 63 68 61 72 61 63 Any stray charac
b4cf0 74 65 72 73 20 69 6e 20 7a 4e 75 6d 20 72 65 73 ters in zNum res
b4d00 75 6c 74 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 ult in undefined
b4d10 0a 2a 2a 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a .** behavior..**
b4d20 0a 2a 2a 20 49 66 20 74 68 65 20 75 6e 73 69 67 .** If the unsig
b4d30 6e 65 64 20 69 6e 74 65 67 65 72 20 74 68 61 74 ned integer that
b4d40 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 73 zNum represents
b4d50 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 61 0a 2a will fit in a.*
b4d60 2a 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 * 64-bit signed
b4d70 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 integer, return
b4d80 54 52 55 45 2e 20 20 4f 74 68 65 72 77 69 73 65 TRUE. Otherwise
b4d90 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a 2a return FALSE..*
b4da0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 65 67 46 *.** If the negF
b4db0 6c 61 67 20 70 61 72 61 6d 65 74 65 72 20 69 73 lag parameter is
b4dc0 20 74 72 75 65 2c 20 74 68 61 74 20 6d 65 61 6e true, that mean
b4dd0 73 20 74 68 61 74 20 7a 4e 75 6d 20 72 65 61 6c s that zNum real
b4de0 6c 79 20 72 65 70 72 65 73 65 6e 74 73 0a 2a 2a ly represents.**
b4df0 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 a negative numb
b4e00 65 72 2e 20 20 28 54 68 65 20 6c 65 61 64 69 6e er. (The leadin
b4e10 67 20 22 2d 22 20 69 73 20 6f 6d 69 74 74 65 64 g "-" is omitted
b4e20 20 66 72 6f 6d 20 7a 4e 75 6d 2e 29 20 20 54 68 from zNum.) Th
b4e30 69 73 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 is.** parameter
b4e40 69 73 20 6e 65 65 64 65 64 20 74 6f 20 64 65 74 is needed to det
b4e50 65 72 6d 69 6e 65 20 61 20 62 6f 75 6e 64 61 72 ermine a boundar
b4e60 79 20 63 61 73 65 2e 20 20 41 20 73 74 72 69 6e y case. A strin
b4e70 67 0a 2a 2a 20 6f 66 20 22 39 32 32 33 33 37 33 g.** of "9223373
b4e80 30 33 36 38 35 34 37 37 35 38 30 38 22 20 72 65 036854775808" re
b4e90 74 75 72 6e 73 20 66 61 6c 73 65 20 69 66 20 6e turns false if n
b4ea0 65 67 46 6c 61 67 20 69 73 20 66 61 6c 73 65 20 egFlag is false
b4eb0 6f 72 20 74 72 75 65 0a 2a 2a 20 69 66 20 6e 65 or true.** if ne
b4ec0 67 46 6c 61 67 20 69 73 20 74 72 75 65 2e 0a 2a gFlag is true..*
b4ed0 2a 0a 2a 2a 20 4c 65 61 64 69 6e 67 20 7a 65 72 *.** Leading zer
b4ee0 6f 73 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a os are ignored..
b4ef0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b4f00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 74 E int sqlite3Fit
b4f10 73 49 6e 36 34 42 69 74 73 28 63 6f 6e 73 74 20 sIn64Bits(const
b4f20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20 char *zNum, int
b4f30 6e 65 67 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 negFlag){. int
b4f40 69 3b 0a 20 20 69 6e 74 20 6e 65 67 20 3d 20 30 i;. int neg = 0
b4f50 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 7a 4e 75 ;.. assert( zNu
b4f60 6d 5b 30 5d 3e 3d 27 30 27 20 26 26 20 7a 4e 75 m[0]>='0' && zNu
b4f70 6d 5b 30 5d 3c 3d 27 39 27 20 29 3b 20 2f 2a 20 m[0]<='9' ); /*
b4f80 7a 4e 75 6d 20 69 73 20 61 6e 20 75 6e 73 69 67 zNum is an unsig
b4f90 6e 65 64 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 ned number */..
b4fa0 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 6e if( negFlag ) n
b4fb0 65 67 20 3d 20 31 2d 6e 65 67 3b 0a 20 20 77 68 eg = 1-neg;. wh
b4fc0 69 6c 65 28 20 2a 7a 4e 75 6d 3d 3d 27 30 27 20 ile( *zNum=='0'
b4fd0 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 20 20 ){. zNum++;
b4fe0 20 2f 2a 20 53 6b 69 70 20 6c 65 61 64 69 6e 67 /* Skip leading
b4ff0 20 7a 65 72 6f 73 2e 20 20 54 69 63 6b 65 74 20 zeros. Ticket
b5000 23 32 34 35 34 20 2a 2f 0a 20 20 7d 0a 20 20 66 #2454 */. }. f
b5010 6f 72 28 69 3d 30 3b 20 7a 4e 75 6d 5b 69 5d 3b or(i=0; zNum[i];
b5020 20 69 2b 2b 29 7b 20 61 73 73 65 72 74 28 20 7a i++){ assert( z
b5030 4e 75 6d 5b 69 5d 3e 3d 27 30 27 20 26 26 20 7a Num[i]>='0' && z
b5040 4e 75 6d 5b 69 5d 3c 3d 27 39 27 20 29 3b 20 7d Num[i]<='9' ); }
b5050 0a 20 20 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 . if( i<19 ){.
b5060 20 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 65 64 /* Guaranteed
b5070 20 74 6f 20 66 69 74 20 69 66 20 6c 65 73 73 20 to fit if less
b5080 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a than 19 digits *
b5090 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a /. return 1;.
b50a0 20 20 7d 65 6c 73 65 20 69 66 28 20 69 3e 31 39 }else if( i>19
b50b0 20 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 72 61 ){. /* Guara
b50c0 6e 74 65 65 64 20 74 6f 20 62 65 20 74 6f 6f 20 nteed to be too
b50d0 62 69 67 20 69 66 20 67 72 65 61 74 65 72 20 74 big if greater t
b50e0 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a 2f han 19 digits */
b50f0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
b5100 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 }else{. /* C
b5110 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 32 ompare against 2
b5120 5e 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 ^63. */. retu
b5130 72 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 rn compare2pow63
b5140 28 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a (zNum)<neg;. }.
b5150 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 7a 4e 75 6d }../*.** If zNum
b5160 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 69 represents an i
b5170 6e 74 65 67 65 72 20 74 68 61 74 20 77 69 6c 6c nteger that will
b5180 20 66 69 74 20 69 6e 20 33 32 2d 62 69 74 73 2c fit in 32-bits,
b5190 20 74 68 65 6e 20 73 65 74 0a 2a 2a 20 2a 70 56 then set.** *pV
b51a0 61 6c 75 65 20 74 6f 20 74 68 61 74 20 69 6e 74 alue to that int
b51b0 65 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20 eger and return
b51c0 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69 73 65 true. Otherwise
b51d0 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a return false..*
b51e0 2a 0a 2a 2a 20 41 6e 79 20 6e 6f 6e 2d 6e 75 6d *.** Any non-num
b51f0 65 72 69 63 20 63 68 61 72 61 63 74 65 72 73 20 eric characters
b5200 74 68 61 74 20 66 6f 6c 6c 6f 77 69 6e 67 20 7a that following z
b5210 4e 75 6d 20 61 72 65 20 69 67 6e 6f 72 65 64 2e Num are ignored.
b5220 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 69 66 66 .** This is diff
b5230 65 72 65 6e 74 20 66 72 6f 6d 20 73 71 6c 69 74 erent from sqlit
b5240 65 33 41 74 6f 69 36 34 28 29 20 77 68 69 63 68 e3Atoi64() which
b5250 20 72 65 71 75 69 72 65 73 20 74 68 65 0a 2a 2a requires the.**
b5260 20 69 6e 70 75 74 20 6e 75 6d 62 65 72 20 74 6f input number to
b5270 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 be zero-termina
b5280 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ted..*/.SQLITE_P
b5290 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b52a0 65 33 47 65 74 49 6e 74 33 32 28 63 6f 6e 73 74 e3GetInt32(const
b52b0 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 char *zNum, int
b52c0 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 73 71 6c *pValue){. sql
b52d0 69 74 65 5f 69 6e 74 36 34 20 76 20 3d 20 30 3b ite_int64 v = 0;
b52e0 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 69 . int i, c;. i
b52f0 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 69 66 nt neg = 0;. if
b5300 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2d 27 20 29 ( zNum[0]=='-' )
b5310 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 {. neg = 1;.
b5320 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c zNum++;. }el
b5330 73 65 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d se if( zNum[0]==
b5340 27 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b '+' ){. zNum+
b5350 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 +;. }. while(
b5360 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 20 7a zNum[0]=='0' ) z
b5370 4e 75 6d 2b 2b 3b 0a 20 20 66 6f 72 28 69 3d 30 Num++;. for(i=0
b5380 3b 20 69 3c 31 31 20 26 26 20 28 63 20 3d 20 7a ; i<11 && (c = z
b5390 4e 75 6d 5b 69 5d 20 2d 20 27 30 27 29 3e 3d 30 Num[i] - '0')>=0
b53a0 20 26 26 20 63 3c 3d 39 3b 20 69 2b 2b 29 7b 0a && c<=9; i++){.
b53b0 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 v = v*10 + c
b53c0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 ;. }.. /* The
b53d0 6c 6f 6e 67 65 73 74 20 64 65 63 69 6d 61 6c 20 longest decimal
b53e0 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f representation o
b53f0 66 20 61 20 33 32 20 62 69 74 20 69 6e 74 65 67 f a 32 bit integ
b5400 65 72 20 69 73 20 31 30 20 64 69 67 69 74 73 3a er is 10 digits:
b5410 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 . **. **
b5420 20 20 20 20 20 20 20 31 32 33 34 35 36 37 38 39 123456789
b5430 30 0a 20 20 2a 2a 20 20 20 20 20 32 5e 33 31 20 0. ** 2^31
b5440 2d 3e 20 32 31 34 37 34 38 33 36 34 38 0a 20 20 -> 2147483648.
b5450 2a 2f 0a 20 20 69 66 28 20 69 3e 31 30 20 29 7b */. if( i>10 ){
b5460 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
b5470 20 7d 0a 20 20 69 66 28 20 76 2d 6e 65 67 3e 32 }. if( v-neg>2
b5480 31 34 37 34 38 33 36 34 37 20 29 7b 0a 20 20 20 147483647 ){.
b5490 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
b54a0 20 69 66 28 20 6e 65 67 20 29 7b 0a 20 20 20 20 if( neg ){.
b54b0 76 20 3d 20 2d 76 3b 0a 20 20 7d 0a 20 20 2a 70 v = -v;. }. *p
b54c0 56 61 6c 75 65 20 3d 20 28 69 6e 74 29 76 3b 0a Value = (int)v;.
b54d0 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f return 1;.}../
b54e0 2a 0a 2a 2a 20 54 68 65 20 76 61 72 69 61 62 6c *.** The variabl
b54f0 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 e-length integer
b5500 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 73 20 encoding is as
b5510 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b follows:.**.** K
b5520 45 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 41 EY:.** A
b5530 20 3d 20 30 78 78 78 78 78 78 78 20 20 20 20 37 = 0xxxxxxx 7
b5540 20 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e bits of data an
b5550 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a d one flag bit.*
b5560 2a 20 20 20 20 20 20 20 20 20 42 20 3d 20 31 78 * B = 1x
b5570 78 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73 xxxxxx 7 bits
b5580 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 of data and one
b5590 20 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 flag bit.**
b55a0 20 20 20 20 20 43 20 3d 20 78 78 78 78 78 78 78 C = xxxxxxx
b55b0 78 20 20 20 20 38 20 62 69 74 73 20 6f 66 20 64 x 8 bits of d
b55c0 61 74 61 0a 2a 2a 0a 2a 2a 20 20 37 20 62 69 74 ata.**.** 7 bit
b55d0 73 20 2d 20 41 0a 2a 2a 20 31 34 20 62 69 74 73 s - A.** 14 bits
b55e0 20 2d 20 42 41 0a 2a 2a 20 32 31 20 62 69 74 73 - BA.** 21 bits
b55f0 20 2d 20 42 42 41 0a 2a 2a 20 32 38 20 62 69 74 - BBA.** 28 bit
b5600 73 20 2d 20 42 42 42 41 0a 2a 2a 20 33 35 20 62 s - BBBA.** 35 b
b5610 69 74 73 20 2d 20 42 42 42 42 41 0a 2a 2a 20 34 its - BBBBA.** 4
b5620 32 20 62 69 74 73 20 2d 20 42 42 42 42 42 41 0a 2 bits - BBBBBA.
b5630 2a 2a 20 34 39 20 62 69 74 73 20 2d 20 42 42 42 ** 49 bits - BBB
b5640 42 42 42 41 0a 2a 2a 20 35 36 20 62 69 74 73 20 BBBA.** 56 bits
b5650 2d 20 42 42 42 42 42 42 42 41 0a 2a 2a 20 36 34 - BBBBBBBA.** 64
b5660 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 42 42 bits - BBBBBBBB
b5670 43 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 C.*/../*.** Writ
b5680 65 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 e a 64-bit varia
b5690 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 ble-length integ
b56a0 65 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73 74 61 er to memory sta
b56b0 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a rting at p[0]..*
b56c0 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 * The length of
b56d0 64 61 74 61 20 77 72 69 74 65 20 77 69 6c 6c 20 data write will
b56e0 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 be between 1 and
b56f0 20 39 20 62 79 74 65 73 2e 20 20 54 68 65 20 6e 9 bytes. The n
b5700 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 umber.** of byte
b5710 73 20 77 72 69 74 74 65 6e 20 69 73 20 72 65 74 s written is ret
b5720 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 urned..**.** A v
b5730 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 ariable-length i
b5740 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73 20 nteger consists
b5750 6f 66 20 74 68 65 20 6c 6f 77 65 72 20 37 20 62 of the lower 7 b
b5760 69 74 73 20 6f 66 20 65 61 63 68 20 62 79 74 65 its of each byte
b5770 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 62 79 74 65 .** for all byte
b5780 73 20 74 68 61 74 20 68 61 76 65 20 74 68 65 20 s that have the
b5790 38 74 68 20 62 69 74 20 73 65 74 20 61 6e 64 20 8th bit set and
b57a0 6f 6e 65 20 62 79 74 65 20 77 69 74 68 20 74 68 one byte with th
b57b0 65 20 38 74 68 0a 2a 2a 20 62 69 74 20 63 6c 65 e 8th.** bit cle
b57c0 61 72 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20 ar. Except, if
b57d0 77 65 20 67 65 74 20 74 6f 20 74 68 65 20 39 74 we get to the 9t
b57e0 68 20 62 79 74 65 2c 20 69 74 20 73 74 6f 72 65 h byte, it store
b57f0 73 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 38 20 s the full.** 8
b5800 62 69 74 73 20 61 6e 64 20 69 73 20 74 68 65 20 bits and is the
b5810 6c 61 73 74 20 62 79 74 65 2e 0a 2a 2f 0a 53 51 last byte..*/.SQ
b5820 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
b5830 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e sqlite3PutVarin
b5840 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t(unsigned char
b5850 2a 70 2c 20 75 36 34 20 76 29 7b 0a 20 20 69 6e *p, u64 v){. in
b5860 74 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 75 38 20 t i, j, n;. u8
b5870 62 75 66 5b 31 30 5d 3b 0a 20 20 69 66 28 20 76 buf[10];. if( v
b5880 20 26 20 28 28 28 75 36 34 29 30 78 66 66 30 30 & (((u64)0xff00
b5890 30 30 30 30 29 3c 3c 33 32 29 20 29 7b 0a 20 20 0000)<<32) ){.
b58a0 20 20 70 5b 38 5d 20 3d 20 28 75 38 29 76 3b 0a p[8] = (u8)v;.
b58b0 20 20 20 20 76 20 3e 3e 3d 20 38 3b 0a 20 20 20 v >>= 8;.
b58c0 20 66 6f 72 28 69 3d 37 3b 20 69 3e 3d 30 3b 20 for(i=7; i>=0;
b58d0 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 5b 69 5d i--){. p[i]
b58e0 20 3d 20 28 75 38 29 28 28 76 20 26 20 30 78 37 = (u8)((v & 0x7
b58f0 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 f) | 0x80);.
b5900 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 20 20 7d v >>= 7;. }
b5910 0a 20 20 20 20 72 65 74 75 72 6e 20 39 3b 0a 20 . return 9;.
b5920 20 7d 20 20 20 20 0a 20 20 6e 20 3d 20 30 3b 0a } . n = 0;.
b5930 20 20 64 6f 7b 0a 20 20 20 20 62 75 66 5b 6e 2b do{. buf[n+
b5940 2b 5d 20 3d 20 28 75 38 29 28 28 76 20 26 20 30 +] = (u8)((v & 0
b5950 78 37 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 x7f) | 0x80);.
b5960 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 v >>= 7;. }wh
b5970 69 6c 65 28 20 76 21 3d 30 20 29 3b 0a 20 20 62 ile( v!=0 );. b
b5980 75 66 5b 30 5d 20 26 3d 20 30 78 37 66 3b 0a 20 uf[0] &= 0x7f;.
b5990 20 61 73 73 65 72 74 28 20 6e 3c 3d 39 20 29 3b assert( n<=9 );
b59a0 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 6e 2d . for(i=0, j=n-
b59b0 31 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 2c 20 69 2b 1; j>=0; j--, i+
b59c0 2b 29 7b 0a 20 20 20 20 70 5b 69 5d 20 3d 20 62 +){. p[i] = b
b59d0 75 66 5b 6a 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 uf[j];. }. ret
b59e0 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn n;.}../*.**
b59f0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
b5a00 61 20 66 61 73 74 65 72 20 76 65 72 73 69 6f 6e a faster version
b5a10 20 6f 66 20 73 71 6c 69 74 65 33 50 75 74 56 61 of sqlite3PutVa
b5a20 72 69 6e 74 28 29 20 74 68 61 74 20 6f 6e 6c 79 rint() that only
b5a30 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 33 32 .** works for 32
b5a40 2d 62 69 74 20 70 6f 73 69 74 69 76 65 20 69 6e -bit positive in
b5a50 74 65 67 65 72 73 20 61 6e 64 20 77 68 69 63 68 tegers and which
b5a60 20 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 66 6f is optimized fo
b5a70 72 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 r.** the common
b5a80 63 61 73 65 20 6f 66 20 73 6d 61 6c 6c 20 69 6e case of small in
b5a90 74 65 67 65 72 73 2e 20 20 41 20 4d 41 43 52 4f tegers. A MACRO
b5aa0 20 76 65 72 73 69 6f 6e 2c 20 70 75 74 56 61 72 version, putVar
b5ab0 69 6e 74 33 32 2c 0a 2a 2a 20 69 73 20 70 72 6f int32,.** is pro
b5ac0 76 69 64 65 64 20 77 68 69 63 68 20 69 6e 6c 69 vided which inli
b5ad0 6e 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 nes the single-b
b5ae0 79 74 65 20 63 61 73 65 2e 20 20 41 6c 6c 20 63 yte case. All c
b5af0 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65 0a 2a ode should use.*
b5b00 2a 20 74 68 65 20 4d 41 43 52 4f 20 76 65 72 73 * the MACRO vers
b5b10 69 6f 6e 20 61 73 20 74 68 69 73 20 66 75 6e 63 ion as this func
b5b20 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65 tion assumes the
b5b30 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 single-byte cas
b5b40 65 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 e has.** already
b5b50 20 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a been handled..*
b5b60 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b5b70 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 int sqlite3PutV
b5b80 61 72 69 6e 74 33 32 28 75 6e 73 69 67 6e 65 64 arint32(unsigned
b5b90 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20 76 29 char *p, u32 v)
b5ba0 7b 0a 23 69 66 6e 64 65 66 20 70 75 74 56 61 72 {.#ifndef putVar
b5bb0 69 6e 74 33 32 0a 20 20 69 66 28 20 28 76 20 26 int32. if( (v &
b5bc0 20 7e 30 78 37 66 29 3d 3d 30 20 29 7b 0a 20 20 ~0x7f)==0 ){.
b5bd0 20 20 70 5b 30 5d 20 3d 20 76 3b 0a 20 20 20 20 p[0] = v;.
b5be0 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 return 1;. }.#e
b5bf0 6e 64 69 66 0a 20 20 69 66 28 20 28 76 20 26 20 ndif. if( (v &
b5c00 7e 30 78 33 66 66 66 29 3d 3d 30 20 29 7b 0a 20 ~0x3fff)==0 ){.
b5c10 20 20 20 70 5b 30 5d 20 3d 20 28 75 38 29 28 28 p[0] = (u8)((
b5c20 76 3e 3e 37 29 20 7c 20 30 78 38 30 29 3b 0a 20 v>>7) | 0x80);.
b5c30 20 20 20 70 5b 31 5d 20 3d 20 28 75 38 29 28 76 p[1] = (u8)(v
b5c40 20 26 20 30 78 37 66 29 3b 0a 20 20 20 20 72 65 & 0x7f);. re
b5c50 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 20 20 72 65 turn 2;. }. re
b5c60 74 75 72 6e 20 73 71 6c 69 74 65 33 50 75 74 56 turn sqlite3PutV
b5c70 61 72 69 6e 74 28 70 2c 20 76 29 3b 0a 7d 0a 0a arint(p, v);.}..
b5c80 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 36 34 2d /*.** Read a 64-
b5c90 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e bit variable-len
b5ca0 67 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d gth integer from
b5cb0 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 memory starting
b5cc0 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 at p[0]..** Ret
b5cd0 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
b5ce0 66 20 62 79 74 65 73 20 72 65 61 64 2e 20 20 54 f bytes read. T
b5cf0 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 he value is stor
b5d00 65 64 20 69 6e 20 2a 76 2e 0a 2a 2f 0a 53 51 4c ed in *v..*/.SQL
b5d10 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 ITE_PRIVATE u8 s
b5d20 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 qlite3GetVarint(
b5d30 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
b5d40 68 61 72 20 2a 70 2c 20 75 36 34 20 2a 76 29 7b har *p, u64 *v){
b5d50 0a 20 20 75 33 32 20 61 2c 62 2c 73 3b 0a 0a 20 . u32 a,b,s;..
b5d60 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a a = *p;. /* a:
b5d70 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a p0 (unmasked) *
b5d80 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 30 /. if (!(a&0x80
b5d90 29 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20 3d 20 )). {. *v =
b5da0 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b a;. return 1;
b5db0 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 . }.. p++;. b
b5dc0 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 = *p;. /* b: p
b5dd0 31 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 1 (unmasked) */.
b5de0 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 if (!(b&0x80))
b5df0 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 30 78 . {. a &= 0x
b5e00 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 7f;. a = a<<7
b5e10 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 ;. a |= b;.
b5e20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 *v = a;. re
b5e30 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 70 turn 2;. }.. p
b5e40 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b ++;. a = a<<14;
b5e50 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a . a |= *p;. /*
b5e60 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 a: p0<<14 | p2
b5e70 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 (unmasked) */.
b5e80 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 if (!(a&0x80)).
b5e90 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 {. a &= (0x7
b5ea0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 f<<14)|(0x7f);.
b5eb0 20 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 b &= 0x7f;.
b5ec0 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 b = b<<7;.
b5ed0 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d a |= b;. *v =
b5ee0 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 33 a;. return 3
b5ef0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 45 31 ;. }.. /* CSE1
b5f00 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 from below */.
b5f10 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 a &= (0x7f<<14)
b5f20 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a |(0x7f);. p++;.
b5f30 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 b = b<<14;. b
b5f40 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 |= *p;. /* b:
b5f50 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d p1<<14 | p3 (unm
b5f60 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 asked) */. if (
b5f70 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 !(b&0x80)). {.
b5f80 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 b &= (0x7f<<1
b5f90 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 2f 4)|(0x7f);. /
b5fa0 2a 20 6d 6f 76 65 64 20 43 53 45 31 20 75 70 20 * moved CSE1 up
b5fb0 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 */. /* a &= (
b5fc0 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
b5fd0 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 3c 3c ; */. a = a<<
b5fe0 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 7;. a |= b;.
b5ff0 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 *v = a;. r
b6000 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 eturn 4;. }..
b6010 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 /* a: p0<<14 | p
b6020 32 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2 (masked) */.
b6030 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 /* b: p1<<14 | p
b6040 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 3 (unmasked) */.
b6050 20 20 2f 2a 20 31 3a 73 61 76 65 20 6f 66 66 20 /* 1:save off
b6060 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20 p0<<21 | p1<<14
b6070 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61 | p2<<7 | p3 (ma
b6080 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 6d 6f sked) */. /* mo
b6090 76 65 64 20 43 53 45 31 20 75 70 20 2a 2f 0a 20 ved CSE1 up */.
b60a0 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c /* a &= (0x7f<<
b60b0 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 14)|(0x7f); */.
b60c0 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 b &= (0x7f<<14)
b60d0 7c 28 30 78 37 66 29 3b 0a 20 20 73 20 3d 20 61 |(0x7f);. s = a
b60e0 3b 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c 31 34 ;. /* s: p0<<14
b60f0 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29 20 2a | p2 (masked) *
b6100 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 /.. p++;. a =
b6110 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 a<<14;. a |= *p
b6120 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 32 38 ;. /* a: p0<<28
b6130 20 7c 20 70 32 3c 3c 31 34 20 7c 20 70 34 20 28 | p2<<14 | p4 (
b6140 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 unmasked) */. i
b6150 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 f (!(a&0x80)).
b6160 7b 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20 {. /* we can
b6170 73 6b 69 70 20 74 68 65 73 65 20 63 61 75 73 65 skip these cause
b6180 20 74 68 65 79 20 77 65 72 65 20 28 65 66 66 65 they were (effe
b6190 63 74 69 76 65 6c 79 29 20 64 6f 6e 65 20 61 62 ctively) done ab
b61a0 6f 76 65 20 69 6e 20 63 61 6c 63 27 69 6e 67 20 ove in calc'ing
b61b0 73 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d s */. /* a &=
b61c0 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78 37 (0x7f<<28)|(0x7
b61d0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a f<<14)|(0x7f); *
b61e0 2f 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20 28 30 /. /* b &= (0
b61f0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
b6200 20 2a 2f 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 */. b = b<<7
b6210 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 ;. a |= b;.
b6220 20 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 20 s = s>>18;.
b6230 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c *v = ((u64)s)<<
b6240 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 32 | a;. retu
b6250 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 rn 5;. }.. /*
b6260 32 3a 73 61 76 65 20 6f 66 66 20 70 30 3c 3c 32 2:save off p0<<2
b6270 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 1 | p1<<14 | p2<
b6280 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 <7 | p3 (masked)
b6290 20 2a 2f 0a 20 20 73 20 3d 20 73 3c 3c 37 3b 0a */. s = s<<7;.
b62a0 20 20 73 20 7c 3d 20 62 3b 0a 20 20 2f 2a 20 73 s |= b;. /* s
b62b0 3a 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 : p0<<21 | p1<<1
b62c0 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 4 | p2<<7 | p3 (
b62d0 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 70 2b masked) */.. p+
b62e0 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a +;. b = b<<14;.
b62f0 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 b |= *p;. /*
b6300 62 3a 20 70 31 3c 3c 32 38 20 7c 20 70 33 3c 3c b: p1<<28 | p3<<
b6310 31 34 20 7c 20 70 35 20 28 75 6e 6d 61 73 6b 65 14 | p5 (unmaske
b6320 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 d) */. if (!(b&
b6330 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 0x80)). {. /
b6340 2a 20 77 65 20 63 61 6e 20 73 6b 69 70 20 74 68 * we can skip th
b6350 69 73 20 63 61 75 73 65 20 69 74 20 77 61 73 20 is cause it was
b6360 28 65 66 66 65 63 74 69 76 65 6c 79 29 20 64 6f (effectively) do
b6370 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61 6c 63 ne above in calc
b6380 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 2f 2a 'ing s */. /*
b6390 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 b &= (0x7f<<28)
b63a0 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 |(0x7f<<14)|(0x7
b63b0 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 f); */. a &=
b63c0 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
b63d0 29 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b );. a = a<<7;
b63e0 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 . a |= b;.
b63f0 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 20 20 s = s>>18;.
b6400 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 *v = ((u64)s)<<3
b6410 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 2 | a;. retur
b6420 6e 20 36 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b n 6;. }.. p++;
b6430 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 . a = a<<14;.
b6440 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a a |= *p;. /* a:
b6450 20 70 32 3c 3c 32 38 20 7c 20 70 34 3c 3c 31 34 p2<<28 | p4<<14
b6460 20 7c 20 70 36 20 28 75 6e 6d 61 73 6b 65 64 29 | p6 (unmasked)
b6470 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 */. if (!(a&0x
b6480 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 80)). {. a &
b6490 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 78 = (0x1f<<28)|(0x
b64a0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
b64b0 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c b &= (0x7f<<
b64c0 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 14)|(0x7f);.
b64d0 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 b = b<<7;. a
b64e0 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e |= b;. s = s>
b64f0 3e 31 31 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 >11;. *v = ((
b6500 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a u64)s)<<32 | a;.
b6510 20 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 20 return 7;.
b6520 7d 0a 0a 20 20 2f 2a 20 43 53 45 32 20 66 72 6f }.. /* CSE2 fro
b6530 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20 26 m below */. a &
b6540 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 = (0x7f<<14)|(0x
b6550 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 7f);. p++;. b
b6560 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 = b<<14;. b |=
b6570 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 33 3c 3c *p;. /* b: p3<<
b6580 32 38 20 7c 20 70 35 3c 3c 31 34 20 7c 20 70 37 28 | p5<<14 | p7
b6590 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 (unmasked) */.
b65a0 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a if (!(b&0x80)).
b65b0 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 30 78 {. b &= (0x
b65c0 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 1f<<28)|(0x7f<<1
b65d0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 2f 4)|(0x7f);. /
b65e0 2a 20 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20 * moved CSE2 up
b65f0 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 */. /* a &= (
b6600 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
b6610 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 3c 3c ; */. a = a<<
b6620 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 7;. a |= b;.
b6630 20 20 20 73 20 3d 20 73 3e 3e 34 3b 0a 20 20 20 s = s>>4;.
b6640 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c *v = ((u64)s)<<
b6650 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 32 | a;. retu
b6660 72 6e 20 38 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b rn 8;. }.. p++
b6670 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 35 3b 0a 20 ;. a = a<<15;.
b6680 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 a |= *p;. /* a
b6690 3a 20 70 34 3c 3c 32 39 20 7c 20 70 36 3c 3c 31 : p4<<29 | p6<<1
b66a0 35 20 7c 20 70 38 20 28 75 6e 6d 61 73 6b 65 64 5 | p8 (unmasked
b66b0 29 20 2a 2f 0a 0a 20 20 2f 2a 20 6d 6f 76 65 64 ) */.. /* moved
b66c0 20 43 53 45 32 20 75 70 20 2a 2f 0a 20 20 2f 2a CSE2 up */. /*
b66d0 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 39 29 a &= (0x7f<<29)
b66e0 7c 28 30 78 37 66 3c 3c 31 35 29 7c 28 30 78 66 |(0x7f<<15)|(0xf
b66f0 66 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30 f); */. b &= (0
b6700 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
b6710 0a 20 20 62 20 3d 20 62 3c 3c 38 3b 0a 20 20 61 . b = b<<8;. a
b6720 20 7c 3d 20 62 3b 0a 0a 20 20 73 20 3d 20 73 3c |= b;.. s = s<
b6730 3c 34 3b 0a 20 20 62 20 3d 20 70 5b 2d 34 5d 3b <4;. b = p[-4];
b6740 0a 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 . b &= 0x7f;.
b6750 62 20 3d 20 62 3e 3e 33 3b 0a 20 20 73 20 7c 3d b = b>>3;. s |=
b6760 20 62 3b 0a 0a 20 20 2a 76 20 3d 20 28 28 75 36 b;.. *v = ((u6
b6770 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 0a 20 4)s)<<32 | a;..
b6780 20 72 65 74 75 72 6e 20 39 3b 0a 7d 0a 0a 2f 2a return 9;.}../*
b6790 0a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69 .** Read a 32-bi
b67a0 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 t variable-lengt
b67b0 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d h integer from m
b67c0 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61 emory starting a
b67d0 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75 72 t p[0]..** Retur
b67e0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
b67f0 62 79 74 65 73 20 72 65 61 64 2e 20 20 54 68 65 bytes read. The
b6800 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 value is stored
b6810 20 69 6e 20 2a 76 2e 0a 2a 2a 0a 2a 2a 20 49 66 in *v..**.** If
b6820 20 74 68 65 20 76 61 72 69 6e 74 20 73 74 6f 72 the varint stor
b6830 65 64 20 69 6e 20 70 5b 30 5d 20 69 73 20 6c 61 ed in p[0] is la
b6840 72 67 65 72 20 74 68 61 6e 20 63 61 6e 20 66 69 rger than can fi
b6850 74 20 69 6e 20 61 20 33 32 2d 62 69 74 20 75 6e t in a 32-bit un
b6860 73 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65 signed.** intege
b6870 72 2c 20 74 68 65 6e 20 73 65 74 20 2a 76 20 74 r, then set *v t
b6880 6f 20 30 78 66 66 66 66 66 66 66 66 2e 0a 2a 2a o 0xffffffff..**
b6890 0a 2a 2a 20 41 20 4d 41 43 52 4f 20 76 65 72 73 .** A MACRO vers
b68a0 69 6f 6e 2c 20 67 65 74 56 61 72 69 6e 74 33 32 ion, getVarint32
b68b0 2c 20 69 73 20 70 72 6f 76 69 64 65 64 20 77 68 , is provided wh
b68c0 69 63 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 ich inlines the
b68d0 0a 2a 2a 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 .** single-byte
b68e0 63 61 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20 case. All code
b68f0 73 68 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d should use the M
b6900 41 43 52 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 ACRO version as
b6910 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f .** this functio
b6920 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20 73 69 n assumes the si
b6930 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 20 68 ngle-byte case h
b6940 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
b6950 68 61 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 handled..*/.SQLI
b6960 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 TE_PRIVATE u8 sq
b6970 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 lite3GetVarint32
b6980 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 (const unsigned
b6990 63 68 61 72 20 2a 70 2c 20 75 33 32 20 2a 76 29 char *p, u32 *v)
b69a0 7b 0a 20 20 75 33 32 20 61 2c 62 3b 0a 0a 20 20 {. u32 a,b;..
b69b0 2f 2a 20 54 68 65 20 31 2d 62 79 74 65 20 63 61 /* The 1-byte ca
b69c0 73 65 2e 20 20 4f 76 65 72 77 68 65 6c 6d 69 6e se. Overwhelmin
b69d0 67 6c 79 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d gly the most com
b69e0 6d 6f 6e 2e 20 20 48 61 6e 64 6c 65 64 20 69 6e mon. Handled in
b69f0 6c 69 6e 65 0a 20 20 2a 2a 20 62 79 20 74 68 65 line. ** by the
b6a00 20 67 65 74 56 61 72 69 6e 33 32 28 29 20 6d 61 getVarin32() ma
b6a10 63 72 6f 20 2a 2f 0a 20 20 61 20 3d 20 2a 70 3b cro */. a = *p;
b6a20 0a 20 20 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d . /* a: p0 (unm
b6a30 61 73 6b 65 64 29 20 2a 2f 0a 23 69 66 6e 64 65 asked) */.#ifnde
b6a40 66 20 67 65 74 56 61 72 69 6e 74 33 32 0a 20 20 f getVarint32.
b6a50 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 if (!(a&0x80)).
b6a60 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 {. /* Values
b6a70 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 31 between 0 and 1
b6a80 32 37 20 2a 2f 0a 20 20 20 20 2a 76 20 3d 20 61 27 */. *v = a
b6a90 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a ;. return 1;.
b6aa0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a }.#endif.. /*
b6ab0 20 54 68 65 20 32 2d 62 79 74 65 20 63 61 73 65 The 2-byte case
b6ac0 20 2a 2f 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d */. p++;. b =
b6ad0 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 20 *p;. /* b: p1
b6ae0 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 (unmasked) */.
b6af0 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 if (!(b&0x80)).
b6b00 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 {. /* Values
b6b10 20 62 65 74 77 65 65 6e 20 31 32 38 20 61 6e 64 between 128 and
b6b20 20 31 36 33 38 33 20 2a 2f 0a 20 20 20 20 61 20 16383 */. a
b6b30 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 61 20 3d &= 0x7f;. a =
b6b40 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 a<<7;. *v =
b6b50 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 a | b;. retur
b6b60 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 n 2;. }.. /* T
b6b70 68 65 20 33 2d 62 79 74 65 20 63 61 73 65 20 2a he 3-byte case *
b6b80 2f 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 /. p++;. a = a
b6b90 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b <<14;. a |= *p;
b6ba0 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 . /* a: p0<<14
b6bb0 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 64 29 20 | p2 (unmasked)
b6bc0 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 */. if (!(a&0x8
b6bd0 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56 0)). {. /* V
b6be0 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 36 alues between 16
b6bf0 33 38 34 20 61 6e 64 20 32 30 39 37 31 35 31 20 384 and 2097151
b6c00 2a 2f 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 */. a &= (0x7
b6c10 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 f<<14)|(0x7f);.
b6c20 20 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 b &= 0x7f;.
b6c30 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 b = b<<7;.
b6c40 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 *v = a | b;.
b6c50 72 65 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 return 3;. }..
b6c60 20 2f 2a 20 41 20 33 32 2d 62 69 74 20 76 61 72 /* A 32-bit var
b6c70 69 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 int is used to s
b6c80 74 6f 72 65 20 73 69 7a 65 20 69 6e 66 6f 72 6d tore size inform
b6c90 61 74 69 6f 6e 20 69 6e 20 62 74 72 65 65 73 2e ation in btrees.
b6ca0 0a 20 20 2a 2a 20 4f 62 6a 65 63 74 73 20 61 72 . ** Objects ar
b6cb0 65 20 72 61 72 65 6c 79 20 6c 61 72 67 65 72 20 e rarely larger
b6cc0 74 68 61 6e 20 32 4d 69 42 20 6c 69 6d 69 74 20 than 2MiB limit
b6cd0 6f 66 20 61 20 33 2d 62 79 74 65 20 76 61 72 69 of a 3-byte vari
b6ce0 6e 74 2e 0a 20 20 2a 2a 20 41 20 33 2d 62 79 74 nt.. ** A 3-byt
b6cf0 65 20 76 61 72 69 6e 74 20 69 73 20 73 75 66 66 e varint is suff
b6d00 69 63 69 65 6e 74 2c 20 66 6f 72 20 65 78 61 6d icient, for exam
b6d10 70 6c 65 2c 20 74 6f 20 72 65 63 6f 72 64 20 74 ple, to record t
b6d20 68 65 20 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 he size. ** of
b6d30 61 20 31 30 34 38 35 36 39 2d 62 79 74 65 20 42 a 1048569-byte B
b6d40 4c 4f 42 20 6f 72 20 73 74 72 69 6e 67 2e 0a 20 LOB or string..
b6d50 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 6f 6e 6c 79 **. ** We only
b6d60 20 75 6e 72 6f 6c 6c 20 74 68 65 20 66 69 72 73 unroll the firs
b6d70 74 20 31 2d 2c 20 32 2d 2c 20 61 6e 64 20 33 2d t 1-, 2-, and 3-
b6d80 20 62 79 74 65 20 63 61 73 65 73 2e 20 20 54 68 byte cases. Th
b6d90 65 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65 e very. ** rare
b6da0 20 6c 61 72 67 65 72 20 63 61 73 65 73 20 63 61 larger cases ca
b6db0 6e 20 62 65 20 68 61 6e 64 6c 65 64 20 62 79 20 n be handled by
b6dc0 74 68 65 20 73 6c 6f 77 65 72 20 36 34 2d 62 69 the slower 64-bi
b6dd0 74 20 76 61 72 69 6e 74 0a 20 20 2a 2a 20 72 6f t varint. ** ro
b6de0 75 74 69 6e 65 2e 0a 20 20 2a 2f 0a 23 69 66 20 utine.. */.#if
b6df0 31 0a 20 20 7b 0a 20 20 20 20 75 36 34 20 76 36 1. {. u64 v6
b6e00 34 3b 0a 20 20 20 20 75 38 20 6e 3b 0a 0a 20 20 4;. u8 n;..
b6e10 20 20 70 20 2d 3d 20 32 3b 0a 20 20 20 20 6e 20 p -= 2;. n
b6e20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 = sqlite3GetVari
b6e30 6e 74 28 70 2c 20 26 76 36 34 29 3b 0a 20 20 20 nt(p, &v64);.
b6e40 20 61 73 73 65 72 74 28 20 6e 3e 33 20 26 26 20 assert( n>3 &&
b6e50 6e 3c 3d 39 20 29 3b 0a 20 20 20 20 69 66 28 20 n<=9 );. if(
b6e60 28 76 36 34 20 26 20 53 51 4c 49 54 45 5f 4d 41 (v64 & SQLITE_MA
b6e70 58 5f 55 33 32 29 21 3d 76 36 34 20 29 7b 0a 20 X_U32)!=v64 ){.
b6e80 20 20 20 20 20 2a 76 20 3d 20 30 78 66 66 66 66 *v = 0xffff
b6e90 66 66 66 66 3b 0a 20 20 20 20 7d 65 6c 73 65 7b ffff;. }else{
b6ea0 0a 20 20 20 20 20 20 2a 76 20 3d 20 28 75 33 32 . *v = (u32
b6eb0 29 76 36 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 )v64;. }.
b6ec0 72 65 74 75 72 6e 20 6e 3b 0a 20 20 7d 0a 0a 23 return n;. }..#
b6ed0 65 6c 73 65 0a 20 20 2f 2a 20 46 6f 72 20 66 6f else. /* For fo
b6ee0 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 28 6b 65 llowing code (ke
b6ef0 70 74 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 pt for historica
b6f00 6c 20 72 65 63 6f 72 64 20 6f 6e 6c 79 29 20 73 l record only) s
b6f10 68 6f 77 73 20 61 6e 0a 20 20 2a 2a 20 75 6e 72 hows an. ** unr
b6f20 6f 6c 6c 69 6e 67 20 66 6f 72 20 74 68 65 20 33 olling for the 3
b6f30 2d 20 61 6e 64 20 34 2d 62 79 74 65 20 76 61 72 - and 4-byte var
b6f40 69 6e 74 20 63 61 73 65 73 2e 20 20 54 68 69 73 int cases. This
b6f50 20 63 6f 64 65 20 69 73 0a 20 20 2a 2a 20 73 6c code is. ** sl
b6f60 69 67 68 74 6c 79 20 66 61 73 74 65 72 2c 20 62 ightly faster, b
b6f70 75 74 20 69 74 20 69 73 20 61 6c 73 6f 20 6c 61 ut it is also la
b6f80 72 67 65 72 20 61 6e 64 20 6d 75 63 68 20 68 61 rger and much ha
b6f90 72 64 65 72 20 74 6f 20 74 65 73 74 2e 0a 20 20 rder to test..
b6fa0 2a 2f 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 */. p++;. b =
b6fb0 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 b<<14;. b |= *p
b6fc0 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 ;. /* b: p1<<14
b6fd0 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65 64 29 | p3 (unmasked)
b6fe0 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78 */. if (!(b&0x
b6ff0 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 80)). {. /*
b7000 56 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 32 Values between 2
b7010 30 39 37 31 35 32 20 61 6e 64 20 32 36 38 34 33 097152 and 26843
b7020 35 34 35 35 20 2a 2f 0a 20 20 20 20 62 20 26 3d 5455 */. b &=
b7030 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 (0x7f<<14)|(0x7
b7040 66 29 3b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 f);. a &= (0x
b7050 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
b7060 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 a = a<<7;.
b7070 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 *v = a | b;.
b7080 20 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a return 4;. }.
b7090 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c . p++;. a = a<
b70a0 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a <14;. a |= *p;.
b70b0 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 32 38 20 7c /* a: p0<<28 |
b70c0 20 70 32 3c 3c 31 34 20 7c 20 70 34 20 28 75 6e p2<<14 | p4 (un
b70d0 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 masked) */. if
b70e0 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a (!(a&0x80)). {.
b70f0 20 20 20 20 2f 2a 20 57 61 6c 75 65 73 20 20 62 /* Walues b
b7100 65 74 77 65 65 6e 20 32 36 38 34 33 35 34 35 36 etween 268435456
b7110 20 61 6e 64 20 33 34 33 35 39 37 33 38 33 36 37 and 34359738367
b7120 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 28 30 78 */. a &= (0x
b7130 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 1f<<28)|(0x7f<<1
b7140 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 4)|(0x7f);. b
b7150 20 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 &= (0x1f<<28)|(
b7160 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
b7170 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a ;. b = b<<7;.
b7180 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a *v = a | b;.
b7190 20 20 20 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 return 5;.
b71a0 7d 0a 0a 20 20 2f 2a 20 57 65 20 63 61 6e 20 6f }.. /* We can o
b71b0 6e 6c 79 20 72 65 61 63 68 20 74 68 69 73 20 70 nly reach this p
b71c0 6f 69 6e 74 20 77 68 65 6e 20 72 65 61 64 69 6e oint when readin
b71d0 67 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 g a corrupt data
b71e0 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 base. ** file.
b71f0 20 49 6e 20 74 68 61 74 20 63 61 73 65 20 77 65 In that case we
b7200 20 61 72 65 20 6e 6f 74 20 69 6e 20 61 6e 79 20 are not in any
b7210 68 75 72 72 79 2e 20 20 55 73 65 20 74 68 65 20 hurry. Use the
b7220 28 72 65 6c 61 74 69 76 65 6c 79 0a 20 20 2a 2a (relatively. **
b7230 20 73 6c 6f 77 29 20 67 65 6e 65 72 61 6c 2d 70 slow) general-p
b7240 75 72 70 6f 73 65 20 73 71 6c 69 74 65 33 47 65 urpose sqlite3Ge
b7250 74 56 61 72 69 6e 74 28 29 20 72 6f 75 74 69 6e tVarint() routin
b7260 65 20 74 6f 20 65 78 74 72 61 63 74 20 74 68 65 e to extract the
b7270 0a 20 20 2a 2a 20 76 61 6c 75 65 2e 20 2a 2f 0a . ** value. */.
b7280 20 20 7b 0a 20 20 20 20 75 36 34 20 76 36 34 3b {. u64 v64;
b7290 0a 20 20 20 20 75 38 20 6e 3b 0a 0a 20 20 20 20 . u8 n;..
b72a0 70 20 2d 3d 20 34 3b 0a 20 20 20 20 6e 20 3d 20 p -= 4;. n =
b72b0 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 sqlite3GetVarint
b72c0 28 70 2c 20 26 76 36 34 29 3b 0a 20 20 20 20 61 (p, &v64);. a
b72d0 73 73 65 72 74 28 20 6e 3e 35 20 26 26 20 6e 3c ssert( n>5 && n<
b72e0 3d 39 20 29 3b 0a 20 20 20 20 2a 76 20 3d 20 28 =9 );. *v = (
b72f0 75 33 32 29 76 36 34 3b 0a 20 20 20 20 72 65 74 u32)v64;. ret
b7300 75 72 6e 20 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 urn n;. }.#endi
b7310 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 f.}../*.** Retur
b7320 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
b7330 62 79 74 65 73 20 74 68 61 74 20 77 69 6c 6c 20 bytes that will
b7340 62 65 20 6e 65 65 64 65 64 20 74 6f 20 73 74 6f be needed to sto
b7350 72 65 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 re the given.**
b7360 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0a 64-bit integer..
b7370 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b7380 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 72 E int sqlite3Var
b7390 69 6e 74 4c 65 6e 28 75 36 34 20 76 29 7b 0a 20 intLen(u64 v){.
b73a0 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 64 6f int i = 0;. do
b73b0 7b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 76 {. i++;. v
b73c0 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 >>= 7;. }while
b73d0 28 20 76 21 3d 30 20 26 26 20 41 4c 57 41 59 53 ( v!=0 && ALWAYS
b73e0 28 69 3c 39 29 20 29 3b 0a 20 20 72 65 74 75 72 (i<9) );. retur
b73f0 6e 20 69 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 n i;.}.../*.** R
b7400 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 20 66 ead or write a f
b7410 6f 75 72 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 our-byte big-end
b7420 69 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 ian integer valu
b7430 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
b7440 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 VATE u32 sqlite3
b7450 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74 20 75 Get4byte(const u
b7460 38 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 8 *p){. return
b7470 28 70 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 70 5b (p[0]<<24) | (p[
b7480 31 5d 3c 3c 31 36 29 20 7c 20 28 70 5b 32 5d 3c 1]<<16) | (p[2]<
b7490 3c 38 29 20 7c 20 70 5b 33 5d 3b 0a 7d 0a 53 51 <8) | p[3];.}.SQ
b74a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
b74b0 64 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 d sqlite3Put4byt
b74c0 65 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 e(unsigned char
b74d0 2a 70 2c 20 75 33 32 20 76 29 7b 0a 20 20 70 5b *p, u32 v){. p[
b74e0 30 5d 20 3d 20 28 75 38 29 28 76 3e 3e 32 34 29 0] = (u8)(v>>24)
b74f0 3b 0a 20 20 70 5b 31 5d 20 3d 20 28 75 38 29 28 ;. p[1] = (u8)(
b7500 76 3e 3e 31 36 29 3b 0a 20 20 70 5b 32 5d 20 3d v>>16);. p[2] =
b7510 20 28 75 38 29 28 76 3e 3e 38 29 3b 0a 20 20 70 (u8)(v>>8);. p
b7520 5b 33 5d 20 3d 20 28 75 38 29 76 3b 0a 7d 0a 0a [3] = (u8)v;.}..
b7530 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
b7540 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f QLITE_OMIT_BLOB_
b7550 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64 65 66 69 LITERAL) || defi
b7560 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 53 5f 43 ned(SQLITE_HAS_C
b7570 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e ODEC)./*.** Tran
b7580 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 20 62 slate a single b
b7590 79 74 65 20 6f 66 20 48 65 78 20 69 6e 74 6f 20 yte of Hex into
b75a0 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 54 an integer..** T
b75b0 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 his routine only
b75c0 20 77 6f 72 6b 73 20 69 66 20 68 20 72 65 61 6c works if h real
b75d0 6c 79 20 69 73 20 61 20 76 61 6c 69 64 20 68 65 ly is a valid he
b75e0 78 61 64 65 63 69 6d 61 6c 0a 2a 2a 20 63 68 61 xadecimal.** cha
b75f0 72 61 63 74 65 72 3a 20 20 30 2e 2e 39 61 2e 2e racter: 0..9a..
b7600 66 41 2e 2e 46 0a 2a 2f 0a 73 74 61 74 69 63 20 fA..F.*/.static
b7610 75 38 20 68 65 78 54 6f 49 6e 74 28 69 6e 74 20 u8 hexToInt(int
b7620 68 29 7b 0a 20 20 61 73 73 65 72 74 28 20 28 68 h){. assert( (h
b7630 3e 3d 27 30 27 20 26 26 20 68 3c 3d 27 39 27 29 >='0' && h<='9')
b7640 20 7c 7c 20 20 28 68 3e 3d 27 61 27 20 26 26 20 || (h>='a' &&
b7650 68 3c 3d 27 66 27 29 20 7c 7c 20 20 28 68 3e 3d h<='f') || (h>=
b7660 27 41 27 20 26 26 20 68 3c 3d 27 46 27 29 20 29 'A' && h<='F') )
b7670 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
b7680 41 53 43 49 49 0a 20 20 68 20 2b 3d 20 39 2a 28 ASCII. h += 9*(
b7690 31 26 28 68 3e 3e 36 29 29 3b 0a 23 65 6e 64 69 1&(h>>6));.#endi
b76a0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
b76b0 45 42 43 44 49 43 0a 20 20 68 20 2b 3d 20 39 2a EBCDIC. h += 9*
b76c0 28 31 26 7e 28 68 3e 3e 34 29 29 3b 0a 23 65 6e (1&~(h>>4));.#en
b76d0 64 69 66 0a 20 20 72 65 74 75 72 6e 20 28 75 38 dif. return (u8
b76e0 29 28 68 20 26 20 30 78 66 29 3b 0a 7d 0a 23 65 )(h & 0xf);.}.#e
b76f0 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f ndif /* !SQLITE_
b7700 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 OMIT_BLOB_LITERA
b7710 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48 41 53 5f L || SQLITE_HAS_
b7720 43 4f 44 45 43 20 2a 2f 0a 0a 23 69 66 20 21 64 CODEC */..#if !d
b7730 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
b7740 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29 IT_BLOB_LITERAL)
b7750 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 || defined(SQLI
b7760 54 45 5f 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a TE_HAS_CODEC)./*
b7770 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 42 4c .** Convert a BL
b7780 4f 42 20 6c 69 74 65 72 61 6c 20 6f 66 20 74 68 OB literal of th
b7790 65 20 66 6f 72 6d 20 22 78 27 68 68 68 68 68 68 e form "x'hhhhhh
b77a0 27 22 20 69 6e 74 6f 20 69 74 73 20 62 69 6e 61 '" into its bina
b77b0 72 79 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 52 65 ry.** value. Re
b77c0 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
b77d0 6f 20 69 74 73 20 62 69 6e 61 72 79 20 76 61 6c o its binary val
b77e0 75 65 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f ue. Space to ho
b77f0 6c 64 20 74 68 65 0a 2a 2a 20 62 69 6e 61 72 79 ld the.** binary
b7800 20 76 61 6c 75 65 20 68 61 73 20 62 65 65 6e 20 value has been
b7810 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 obtained from ma
b7820 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 62 65 lloc and must be
b7830 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 74 68 65 freed by.** the
b7840 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 calling routine
b7850 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
b7860 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
b7870 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 3HexToBlob(sqlit
b7880 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 e3 *db, const ch
b7890 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 ar *z, int n){.
b78a0 20 63 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 char *zBlob;.
b78b0 69 6e 74 20 69 3b 0a 0a 20 20 7a 42 6c 6f 62 20 int i;.. zBlob
b78c0 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 = (char *)sqlite
b78d0 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
b78e0 20 6e 2f 32 20 2b 20 31 29 3b 0a 20 20 6e 2d 2d n/2 + 1);. n--
b78f0 3b 0a 20 20 69 66 28 20 7a 42 6c 6f 62 20 29 7b ;. if( zBlob ){
b7900 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
b7910 6e 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 n; i+=2){.
b7920 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 28 68 65 zBlob[i/2] = (he
b7930 78 54 6f 49 6e 74 28 7a 5b 69 5d 29 3c 3c 34 29 xToInt(z[i])<<4)
b7940 20 7c 20 68 65 78 54 6f 49 6e 74 28 7a 5b 69 2b | hexToInt(z[i+
b7950 31 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 1]);. }. z
b7960 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 30 3b 0a 20 Blob[i/2] = 0;.
b7970 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 42 6c 6f }. return zBlo
b7980 62 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 b;.}.#endif /* !
b7990 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 SQLITE_OMIT_BLOB
b79a0 5f 4c 49 54 45 52 41 4c 20 7c 7c 20 53 51 4c 49 _LITERAL || SQLI
b79b0 54 45 5f 48 41 53 5f 43 4f 44 45 43 20 2a 2f 0a TE_HAS_CODEC */.
b79c0 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
b79d0 68 65 20 73 71 6c 69 74 65 2e 6d 61 67 69 63 20 he sqlite.magic
b79e0 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49 from SQLITE_MAGI
b79f0 43 5f 4f 50 45 4e 20 74 6f 20 53 51 4c 49 54 45 C_OPEN to SQLITE
b7a00 5f 4d 41 47 49 43 5f 42 55 53 59 2e 0a 2a 2a 20 _MAGIC_BUSY..**
b7a10 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 Return an error
b7a20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68 (non-zero) if th
b7a30 65 20 6d 61 67 69 63 20 77 61 73 20 6e 6f 74 20 e magic was not
b7a40 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 SQLITE_MAGIC_OPE
b7a50 4e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72 N.** when this r
b7a60 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
b7a70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
b7a80 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 tine is called w
b7a90 68 65 6e 20 65 6e 74 65 72 69 6e 67 20 61 6e 20 hen entering an
b7aa0 53 51 4c 69 74 65 20 41 50 49 2e 20 20 54 68 65 SQLite API. The
b7ab0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 SQLITE_MAGIC_OP
b7ac0 45 4e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 64 69 EN.** value indi
b7ad0 63 61 74 65 73 20 74 68 61 74 20 74 68 65 20 64 cates that the d
b7ae0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
b7af0 6f 6e 20 70 61 73 73 65 64 20 69 6e 74 6f 20 74 on passed into t
b7b00 68 65 20 41 50 49 20 69 73 0a 2a 2a 20 6f 70 65 he API is.** ope
b7b10 6e 20 61 6e 64 20 69 73 20 6e 6f 74 20 62 65 69 n and is not bei
b7b20 6e 67 20 75 73 65 64 20 62 79 20 61 6e 6f 74 68 ng used by anoth
b7b30 65 72 20 74 68 72 65 61 64 2e 20 20 42 79 20 63 er thread. By c
b7b40 68 61 6e 67 69 6e 67 20 74 68 65 20 76 61 6c 75 hanging the valu
b7b50 65 0a 2a 2a 20 74 6f 20 53 51 4c 49 54 45 5f 4d e.** to SQLITE_M
b7b60 41 47 49 43 5f 42 55 53 59 20 77 65 20 69 6e 64 AGIC_BUSY we ind
b7b70 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20 63 icate that the c
b7b80 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20 onnection is in
b7b90 75 73 65 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 53 use..** sqlite3S
b7ba0 61 66 65 74 79 4f 66 66 28 29 20 62 65 6c 6f 77 afetyOff() below
b7bb0 20 77 69 6c 6c 20 63 68 61 6e 67 65 20 74 68 65 will change the
b7bc0 20 76 61 6c 75 65 20 62 61 63 6b 20 74 6f 20 53 value back to S
b7bd0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e QLITE_MAGIC_OPEN
b7be0 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 41 50 49 .** when the API
b7bf0 20 65 78 69 74 73 2e 20 0a 2a 2a 0a 2a 2a 20 54 exits. .**.** T
b7c00 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
b7c10 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65 74 65 attempt to dete
b7c20 63 74 20 69 66 20 74 77 6f 20 74 68 72 65 61 64 ct if two thread
b7c30 73 20 75 73 65 20 74 68 65 0a 2a 2a 20 73 61 6d s use the.** sam
b7c40 65 20 73 71 6c 69 74 65 2a 20 70 6f 69 6e 74 65 e sqlite* pointe
b7c50 72 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 r at the same ti
b7c60 6d 65 2e 20 20 54 68 65 72 65 20 69 73 20 61 20 me. There is a
b7c70 72 61 63 65 20 0a 2a 2a 20 63 6f 6e 64 69 74 69 race .** conditi
b7c80 6f 6e 20 73 6f 20 69 74 20 69 73 20 70 6f 73 73 on so it is poss
b7c90 69 62 6c 65 20 74 68 61 74 20 74 68 65 20 65 72 ible that the er
b7ca0 72 6f 72 20 69 73 20 6e 6f 74 20 64 65 74 65 63 ror is not detec
b7cb0 74 65 64 2e 0a 2a 2a 20 42 75 74 20 75 73 75 61 ted..** But usua
b7cc0 6c 6c 79 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 lly the problem
b7cd0 77 69 6c 6c 20 62 65 20 73 65 65 6e 2e 20 20 54 will be seen. T
b7ce0 68 65 20 72 65 73 75 6c 74 20 77 69 6c 6c 20 62 he result will b
b7cf0 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 77 68 e an.** error wh
b7d00 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 20 ich can be used
b7d10 74 6f 20 64 65 62 75 67 20 74 68 65 20 61 70 70 to debug the app
b7d20 6c 69 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73 lication that is
b7d30 0a 2a 2a 20 75 73 69 6e 67 20 53 51 4c 69 74 65 .** using SQLite
b7d40 20 69 6e 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a incorrectly..**
b7d50 0a 2a 2a 20 54 69 63 6b 65 74 20 23 32 30 32 3a .** Ticket #202:
b7d60 20 20 49 66 20 64 62 2d 3e 6d 61 67 69 63 20 69 If db->magic i
b7d70 73 20 6e 6f 74 20 61 20 76 61 6c 69 64 20 6f 70 s not a valid op
b7d80 65 6e 20 76 61 6c 75 65 2c 20 74 61 6b 65 20 63 en value, take c
b7d90 61 72 65 20 6e 6f 74 0a 2a 2a 20 74 6f 20 6d 6f are not.** to mo
b7da0 64 69 66 79 20 74 68 65 20 64 62 20 73 74 72 75 dify the db stru
b7db0 63 74 75 72 65 20 61 74 20 61 6c 6c 2e 20 20 49 cture at all. I
b7dc0 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20 t could be that
b7dd0 64 62 20 69 73 20 61 20 73 74 61 6c 65 0a 2a 2a db is a stale.**
b7de0 20 70 6f 69 6e 74 65 72 2e 20 20 49 6e 20 6f 74 pointer. In ot
b7df0 68 65 72 20 77 6f 72 64 73 2c 20 69 74 20 63 6f her words, it co
b7e00 75 6c 64 20 62 65 20 74 68 61 74 20 74 68 65 72 uld be that ther
b7e10 65 20 68 61 73 20 62 65 65 6e 20 61 20 70 72 69 e has been a pri
b7e20 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 or.** call to sq
b7e30 6c 69 74 65 33 5f 63 6c 6f 73 65 28 64 62 29 20 lite3_close(db)
b7e40 61 6e 64 20 64 62 20 68 61 73 20 62 65 65 6e 20 and db has been
b7e50 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 41 6e deallocated. An
b7e60 64 20 77 65 20 64 6f 0a 2a 2a 20 6e 6f 74 20 77 d we do.** not w
b7e70 61 6e 74 20 74 6f 20 77 72 69 74 65 20 69 6e 74 ant to write int
b7e80 6f 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 6d 65 o deallocated me
b7e90 6d 6f 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 mory..*/.#ifdef
b7ea0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c SQLITE_DEBUG.SQL
b7eb0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b7ec0 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 sqlite3SafetyOn(
b7ed0 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
b7ee0 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 if( db->magic==S
b7ef0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e QLITE_MAGIC_OPEN
b7f00 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 ){. db->magi
b7f10 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 c = SQLITE_MAGIC
b7f20 5f 42 55 53 59 3b 0a 20 20 20 20 61 73 73 65 72 _BUSY;. asser
b7f30 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
b7f40 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 _held(db->mutex)
b7f50 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 );. return 0
b7f60 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64 62 ;. }else if( db
b7f70 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f ->magic==SQLITE_
b7f80 4d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 MAGIC_BUSY ){.
b7f90 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 db->magic = SQ
b7fa0 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 LITE_MAGIC_ERROR
b7fb0 3b 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 ;. db->u1.isI
b7fc0 6e 74 65 72 72 75 70 74 65 64 20 3d 20 31 3b 0a nterrupted = 1;.
b7fd0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a }. return 1;.
b7fe0 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
b7ff0 43 68 61 6e 67 65 20 74 68 65 20 6d 61 67 69 63 Change the magic
b8000 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 47 from SQLITE_MAG
b8010 49 43 5f 42 55 53 59 20 74 6f 20 53 51 4c 49 54 IC_BUSY to SQLIT
b8020 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 2e 0a 2a 2a E_MAGIC_OPEN..**
b8030 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 Return an error
b8040 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74 (non-zero) if t
b8050 68 65 20 6d 61 67 69 63 20 77 61 73 20 6e 6f 74 he magic was not
b8060 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 SQLITE_MAGIC_BU
b8070 53 59 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 SY.** when this
b8080 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
b8090 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c d..*/.#ifdef SQL
b80a0 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 ITE_DEBUG.SQLITE
b80b0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
b80c0 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 73 71 ite3SafetyOff(sq
b80d0 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 lite3 *db){. if
b80e0 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c ( db->magic==SQL
b80f0 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 ITE_MAGIC_BUSY )
b8100 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 {. db->magic
b8110 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f = SQLITE_MAGIC_O
b8120 50 45 4e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 PEN;. assert(
b8130 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
b8140 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 eld(db->mutex) )
b8150 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
b8160 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d }else{. db-
b8170 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f >magic = SQLITE_
b8180 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20 MAGIC_ERROR;.
b8190 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 db->u1.isInterr
b81a0 75 70 74 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 upted = 1;. r
b81b0 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23 eturn 1;. }.}.#
b81c0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 endif../*.** Che
b81d0 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 ck to make sure
b81e0 77 65 20 68 61 76 65 20 61 20 76 61 6c 69 64 20 we have a valid
b81f0 64 62 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 69 db pointer. Thi
b8200 73 20 74 65 73 74 20 69 73 20 6e 6f 74 0a 2a 2a s test is not.**
b8210 20 66 6f 6f 6c 70 72 6f 6f 66 20 62 75 74 20 69 foolproof but i
b8220 74 20 64 6f 65 73 20 70 72 6f 76 69 64 65 20 73 t does provide s
b8230 6f 6d 65 20 6d 65 61 73 75 72 65 20 6f 66 20 70 ome measure of p
b8240 72 6f 74 65 63 74 69 6f 6e 20 61 67 61 69 6e 73 rotection agains
b8250 74 0a 2a 2a 20 6d 69 73 75 73 65 20 6f 66 20 74 t.** misuse of t
b8260 68 65 20 69 6e 74 65 72 66 61 63 65 20 73 75 63 he interface suc
b8270 68 20 61 73 20 70 61 73 73 69 6e 67 20 69 6e 20 h as passing in
b8280 64 62 20 70 6f 69 6e 74 65 72 73 20 74 68 61 74 db pointers that
b8290 20 61 72 65 0a 2a 2a 20 4e 55 4c 4c 20 6f 72 20 are.** NULL or
b82a0 77 68 69 63 68 20 68 61 76 65 20 62 65 65 6e 20 which have been
b82b0 70 72 65 76 69 6f 75 73 6c 79 20 63 6c 6f 73 65 previously close
b82c0 64 2e 20 20 49 66 20 74 68 69 73 20 72 6f 75 74 d. If this rout
b82d0 69 6e 65 20 72 65 74 75 72 6e 73 0a 2a 2a 20 31 ine returns.** 1
b82e0 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 it means that t
b82f0 68 65 20 64 62 20 70 6f 69 6e 74 65 72 20 69 73 he db pointer is
b8300 20 76 61 6c 69 64 20 61 6e 64 20 30 20 69 66 20 valid and 0 if
b8310 69 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 it should not be
b8320 0a 2a 2a 20 64 65 72 65 66 65 72 65 6e 63 65 64 .** dereferenced
b8330 20 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 2e for any reason.
b8340 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 The calling fu
b8350 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 69 6e nction should in
b8360 76 6f 6b 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d voke.** SQLITE_M
b8370 49 53 55 53 45 20 69 6d 6d 65 64 69 61 74 65 6c ISUSE immediatel
b8380 79 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 y..**.** sqlite3
b8390 53 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 29 20 SafetyCheckOk()
b83a0 72 65 71 75 69 72 65 73 20 74 68 61 74 20 74 68 requires that th
b83b0 65 20 64 62 20 70 6f 69 6e 74 65 72 20 62 65 20 e db pointer be
b83c0 76 61 6c 69 64 20 66 6f 72 0a 2a 2a 20 75 73 65 valid for.** use
b83d0 2e 20 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 . sqlite3Safety
b83e0 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 29 20 CheckSickOrOk()
b83f0 61 6c 6c 6f 77 73 20 61 20 64 62 20 70 6f 69 6e allows a db poin
b8400 74 65 72 20 74 68 61 74 20 66 61 69 6c 65 64 20 ter that failed
b8410 74 6f 0a 2a 2a 20 6f 70 65 6e 20 70 72 6f 70 65 to.** open prope
b8420 72 6c 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 66 rly and is not f
b8430 69 74 20 66 6f 72 20 67 65 6e 65 72 61 6c 20 75 it for general u
b8440 73 65 20 62 75 74 20 77 68 69 63 68 20 63 61 6e se but which can
b8450 20 62 65 0a 2a 2a 20 75 73 65 64 20 61 73 20 61 be.** used as a
b8460 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 n argument to sq
b8470 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 6f lite3_errmsg() o
b8480 72 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 r sqlite3_close(
b8490 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
b84a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
b84b0 53 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 SafetyCheckOk(sq
b84c0 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 75 33 lite3 *db){. u3
b84d0 32 20 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 64 2 magic;. if( d
b84e0 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b b==0 ) return 0;
b84f0 0a 20 20 6d 61 67 69 63 20 3d 20 64 62 2d 3e 6d . magic = db->m
b8500 61 67 69 63 3b 0a 20 20 69 66 28 20 6d 61 67 69 agic;. if( magi
b8510 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f c!=SQLITE_MAGIC_
b8520 4f 50 45 4e 20 0a 23 69 66 64 65 66 20 53 51 4c OPEN .#ifdef SQL
b8530 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 26 ITE_DEBUG. &
b8540 26 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f & magic!=SQLITE_
b8550 4d 41 47 49 43 5f 42 55 53 59 0a 23 65 6e 64 69 MAGIC_BUSY.#endi
b8560 66 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 f. ){. retur
b8570 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 n 0;. }else{.
b8580 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
b8590 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
b85a0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 int sqlite3Safe
b85b0 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 tyCheckSickOrOk(
b85c0 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
b85d0 75 33 32 20 6d 61 67 69 63 3b 0a 20 20 6d 61 67 u32 magic;. mag
b85e0 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b 0a ic = db->magic;.
b85f0 20 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51 4c if( magic!=SQL
b8600 49 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 26 ITE_MAGIC_SICK &
b8610 26 0a 20 20 20 20 20 20 6d 61 67 69 63 21 3d 53 &. magic!=S
b8620 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e QLITE_MAGIC_OPEN
b8630 20 26 26 0a 20 20 20 20 20 20 6d 61 67 69 63 21 &&. magic!
b8640 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 =SQLITE_MAGIC_BU
b8650 53 59 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 SY ) return 0;.
b8660 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a return 1;.}../*
b8670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
b8680 64 20 6f 66 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a d of util.c ****
b8690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b86a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b86b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
b86c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
b86d0 67 69 6e 20 66 69 6c 65 20 68 61 73 68 2e 63 20 gin file hash.c
b86e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b86f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
b8710 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 .** 2001 Septemb
b8720 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 er 22.**.** The
b8730 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
b8740 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
b8750 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
b8760 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
b8770 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
b8780 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
b8790 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
b87a0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
b87b0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
b87c0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
b87d0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
b87e0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
b87f0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
b8800 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
b8810 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
b8820 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
b8830 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
b8840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8880 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 ****.** This is
b8890 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
b88a0 6f 6e 20 6f 66 20 67 65 6e 65 72 69 63 20 68 61 on of generic ha
b88b0 73 68 2d 74 61 62 6c 65 73 0a 2a 2a 20 75 73 65 sh-tables.** use
b88c0 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a d in SQLite..**.
b88d0 2a 2a 20 24 49 64 3a 20 68 61 73 68 2e 63 2c 76 ** $Id: hash.c,v
b88e0 20 31 2e 33 38 20 32 30 30 39 2f 30 35 2f 30 39 1.38 2009/05/09
b88f0 20 32 33 3a 32 39 3a 31 32 20 64 72 68 20 45 78 23:29:12 drh Ex
b8900 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 54 75 72 6e 20 p $.*/../* Turn
b8910 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f bulk memory into
b8920 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f 62 a hash table ob
b8930 6a 65 63 74 20 62 79 20 69 6e 69 74 69 61 6c 69 ject by initiali
b8940 7a 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 65 6c zing the.** fiel
b8950 64 73 20 6f 66 20 74 68 65 20 48 61 73 68 20 73 ds of the Hash s
b8960 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 tructure..**.**
b8970 22 70 4e 65 77 22 20 69 73 20 61 20 70 6f 69 6e "pNew" is a poin
b8980 74 65 72 20 74 6f 20 74 68 65 20 68 61 73 68 20 ter to the hash
b8990 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 74 6f table that is to
b89a0 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e be initialized.
b89b0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b89c0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 TE void sqlite3H
b89d0 61 73 68 49 6e 69 74 28 48 61 73 68 20 2a 70 4e ashInit(Hash *pN
b89e0 65 77 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 ew){. assert( p
b89f0 4e 65 77 21 3d 30 20 29 3b 0a 20 20 70 4e 65 77 New!=0 );. pNew
b8a00 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 70 ->first = 0;. p
b8a10 4e 65 77 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a New->count = 0;.
b8a20 20 20 70 4e 65 77 2d 3e 68 74 73 69 7a 65 20 3d pNew->htsize =
b8a30 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 20 3d 0;. pNew->ht =
b8a40 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 0;.}../* Remove
b8a50 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 66 72 6f all entries fro
b8a60 6d 20 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20 m a hash table.
b8a70 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d Reclaim all mem
b8a80 6f 72 79 2e 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 ory..** Call thi
b8a90 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 64 65 6c s routine to del
b8aa0 65 74 65 20 61 20 68 61 73 68 20 74 61 62 6c 65 ete a hash table
b8ab0 20 6f 72 20 74 6f 20 72 65 73 65 74 20 61 20 68 or to reset a h
b8ac0 61 73 68 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 ash table.** to
b8ad0 74 68 65 20 65 6d 70 74 79 20 73 74 61 74 65 2e the empty state.
b8ae0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b8af0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 TE void sqlite3H
b8b00 61 73 68 43 6c 65 61 72 28 48 61 73 68 20 2a 70 ashClear(Hash *p
b8b10 48 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a H){. HashElem *
b8b20 65 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a elem; /*
b8b30 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 For looping ove
b8b40 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f r all elements o
b8b50 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 0a f the table */..
b8b60 20 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 assert( pH!=0
b8b70 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e );. elem = pH->
b8b80 66 69 72 73 74 3b 0a 20 20 70 48 2d 3e 66 69 72 first;. pH->fir
b8b90 73 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 st = 0;. sqlite
b8ba0 33 5f 66 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 3_free(pH->ht);.
b8bb0 20 20 70 48 2d 3e 68 74 20 3d 20 30 3b 0a 20 20 pH->ht = 0;.
b8bc0 70 48 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a pH->htsize = 0;.
b8bd0 20 20 77 68 69 6c 65 28 20 65 6c 65 6d 20 29 7b while( elem ){
b8be0 0a 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e . HashElem *n
b8bf0 65 78 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d ext_elem = elem-
b8c00 3e 6e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 >next;. sqlit
b8c10 65 33 5f 66 72 65 65 28 65 6c 65 6d 29 3b 0a 20 e3_free(elem);.
b8c20 20 20 20 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 elem = next_e
b8c30 6c 65 6d 3b 0a 20 20 7d 0a 20 20 70 48 2d 3e 63 lem;. }. pH->c
b8c40 6f 75 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a ount = 0;.}../*.
b8c50 2a 2a 20 54 68 65 20 68 61 73 68 69 6e 67 20 66 ** The hashing f
b8c60 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 unction..*/.stat
b8c70 69 63 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 ic unsigned int
b8c80 73 74 72 48 61 73 68 28 63 6f 6e 73 74 20 63 68 strHash(const ch
b8c90 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 4b 65 79 29 ar *z, int nKey)
b8ca0 7b 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20 {. int h = 0;.
b8cb0 20 61 73 73 65 72 74 28 20 6e 4b 65 79 3e 3d 30 assert( nKey>=0
b8cc0 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4b 65 );. while( nKe
b8cd0 79 20 3e 20 30 20 20 29 7b 0a 20 20 20 20 68 20 y > 0 ){. h
b8ce0 3d 20 28 68 3c 3c 33 29 20 5e 20 68 20 5e 20 73 = (h<<3) ^ h ^ s
b8cf0 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 qlite3UpperToLow
b8d00 65 72 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 er[(unsigned cha
b8d10 72 29 2a 7a 2b 2b 5d 3b 0a 20 20 20 20 6e 4b 65 r)*z++];. nKe
b8d20 79 2d 2d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 y--;. }. retur
b8d30 6e 20 68 3b 0a 7d 0a 0a 0a 2f 2a 20 4c 69 6e 6b n h;.}.../* Link
b8d40 20 70 4e 65 77 20 65 6c 65 6d 65 6e 74 20 69 6e pNew element in
b8d50 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c to the hash tabl
b8d60 65 20 70 48 2e 20 20 49 66 20 70 45 6e 74 72 79 e pH. If pEntry
b8d70 21 3d 30 20 74 68 65 6e 20 61 6c 73 6f 0a 2a 2a !=0 then also.**
b8d80 20 69 6e 73 65 72 74 20 70 4e 65 77 20 69 6e 74 insert pNew int
b8d90 6f 20 74 68 65 20 70 45 6e 74 72 79 20 68 61 73 o the pEntry has
b8da0 68 20 62 75 63 6b 65 74 2e 0a 2a 2f 0a 73 74 61 h bucket..*/.sta
b8db0 74 69 63 20 76 6f 69 64 20 69 6e 73 65 72 74 45 tic void insertE
b8dc0 6c 65 6d 65 6e 74 28 0a 20 20 48 61 73 68 20 2a lement(. Hash *
b8dd0 70 48 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 pH,
b8de0 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 /* The complete
b8df0 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 hash table */.
b8e00 20 73 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e struct _ht *pEn
b8e10 74 72 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 65 try, /* The e
b8e20 6e 74 72 79 20 69 6e 74 6f 20 77 68 69 63 68 20 ntry into which
b8e30 70 4e 65 77 20 69 73 20 69 6e 73 65 72 74 65 64 pNew is inserted
b8e40 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a */. HashElem *
b8e50 70 4e 65 77 20 20 20 20 20 20 20 20 20 2f 2a 20 pNew /*
b8e60 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 The element to b
b8e70 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b e inserted */.){
b8e80 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 48 65 . HashElem *pHe
b8e90 61 64 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 72 ad; /* Fir
b8ea0 73 74 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 st element alrea
b8eb0 64 79 20 69 6e 20 70 45 6e 74 72 79 20 2a 2f 0a dy in pEntry */.
b8ec0 20 20 69 66 28 20 70 45 6e 74 72 79 20 29 7b 0a if( pEntry ){.
b8ed0 20 20 20 20 70 48 65 61 64 20 3d 20 70 45 6e 74 pHead = pEnt
b8ee0 72 79 2d 3e 63 6f 75 6e 74 20 3f 20 70 45 6e 74 ry->count ? pEnt
b8ef0 72 79 2d 3e 63 68 61 69 6e 20 3a 20 30 3b 0a 20 ry->chain : 0;.
b8f00 20 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 pEntry->count
b8f10 2b 2b 3b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e ++;. pEntry->
b8f20 63 68 61 69 6e 20 3d 20 70 4e 65 77 3b 0a 20 20 chain = pNew;.
b8f30 7d 65 6c 73 65 7b 0a 20 20 20 20 70 48 65 61 64 }else{. pHead
b8f40 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 = 0;. }. if(
b8f50 70 48 65 61 64 20 29 7b 0a 20 20 20 20 70 4e 65 pHead ){. pNe
b8f60 77 2d 3e 6e 65 78 74 20 3d 20 70 48 65 61 64 3b w->next = pHead;
b8f70 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 . pNew->prev
b8f80 3d 20 70 48 65 61 64 2d 3e 70 72 65 76 3b 0a 20 = pHead->prev;.
b8f90 20 20 20 69 66 28 20 70 48 65 61 64 2d 3e 70 72 if( pHead->pr
b8fa0 65 76 20 29 7b 20 70 48 65 61 64 2d 3e 70 72 65 ev ){ pHead->pre
b8fb0 76 2d 3e 6e 65 78 74 20 3d 20 70 4e 65 77 3b 20 v->next = pNew;
b8fc0 7d 0a 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 }. else
b8fd0 20 20 20 20 20 20 20 7b 20 70 48 2d 3e 66 69 72 { pH->fir
b8fe0 73 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 st = pNew; }.
b8ff0 20 70 48 65 61 64 2d 3e 70 72 65 76 20 3d 20 70 pHead->prev = p
b9000 4e 65 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 New;. }else{.
b9010 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 pNew->next = p
b9020 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20 69 66 H->first;. if
b9030 28 20 70 48 2d 3e 66 69 72 73 74 20 29 7b 20 70 ( pH->first ){ p
b9040 48 2d 3e 66 69 72 73 74 2d 3e 70 72 65 76 20 3d H->first->prev =
b9050 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 4e 65 pNew; }. pNe
b9060 77 2d 3e 70 72 65 76 20 3d 20 30 3b 0a 20 20 20 w->prev = 0;.
b9070 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 pH->first = pNe
b9080 77 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 52 65 w;. }.}.../* Re
b9090 73 69 7a 65 20 74 68 65 20 68 61 73 68 20 74 61 size the hash ta
b90a0 62 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 ble so that it c
b90b0 61 6e 74 61 69 6e 73 20 22 6e 65 77 5f 73 69 7a antains "new_siz
b90c0 65 22 20 62 75 63 6b 65 74 73 2e 0a 2a 2a 0a 2a e" buckets..**.*
b90d0 2a 20 54 68 65 20 68 61 73 68 20 74 61 62 6c 65 * The hash table
b90e0 20 6d 69 67 68 74 20 66 61 69 6c 20 74 6f 20 72 might fail to r
b90f0 65 73 69 7a 65 20 69 66 20 73 71 6c 69 74 65 33 esize if sqlite3
b9100 5f 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 _malloc() fails
b9110 6f 72 0a 2a 2a 20 69 66 20 74 68 65 20 6e 65 77 or.** if the new
b9120 20 73 69 7a 65 20 69 73 20 74 68 65 20 73 61 6d size is the sam
b9130 65 20 61 73 20 74 68 65 20 70 72 69 6f 72 20 73 e as the prior s
b9140 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 54 ize..** Return T
b9150 52 55 45 20 69 66 20 74 68 65 20 72 65 73 69 7a RUE if the resiz
b9160 65 20 6f 63 63 75 72 73 20 61 6e 64 20 66 61 6c e occurs and fal
b9170 73 65 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 se if not..*/.st
b9180 61 74 69 63 20 69 6e 74 20 72 65 68 61 73 68 28 atic int rehash(
b9190 48 61 73 68 20 2a 70 48 2c 20 75 6e 73 69 67 6e Hash *pH, unsign
b91a0 65 64 20 69 6e 74 20 6e 65 77 5f 73 69 7a 65 29 ed int new_size)
b91b0 7b 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20 2a {. struct _ht *
b91c0 6e 65 77 5f 68 74 3b 20 20 20 20 20 20 20 20 20 new_ht;
b91d0 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 68 61 /* The new ha
b91e0 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 sh table */. Ha
b91f0 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 2c 20 2a 6e shElem *elem, *n
b9200 65 78 74 5f 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 ext_elem; /*
b9210 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 For looping over
b9220 20 65 78 69 73 74 69 6e 67 20 65 6c 65 6d 65 6e existing elemen
b9230 74 73 20 2a 2f 0a 0a 23 69 66 20 53 51 4c 49 54 ts */..#if SQLIT
b9240 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 E_MALLOC_SOFT_LI
b9250 4d 49 54 3e 30 0a 20 20 69 66 28 20 6e 65 77 5f MIT>0. if( new_
b9260 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75 size*sizeof(stru
b9270 63 74 20 5f 68 74 29 3e 53 51 4c 49 54 45 5f 4d ct _ht)>SQLITE_M
b9280 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 ALLOC_SOFT_LIMIT
b9290 20 29 7b 0a 20 20 20 20 6e 65 77 5f 73 69 7a 65 ){. new_size
b92a0 20 3d 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 = SQLITE_MALLOC
b92b0 5f 53 4f 46 54 5f 4c 49 4d 49 54 2f 73 69 7a 65 _SOFT_LIMIT/size
b92c0 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3b 0a of(struct _ht);.
b92d0 20 20 7d 0a 20 20 69 66 28 20 6e 65 77 5f 73 69 }. if( new_si
b92e0 7a 65 3d 3d 70 48 2d 3e 68 74 73 69 7a 65 20 29 ze==pH->htsize )
b92f0 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 return 0;.#endi
b9300 66 0a 0a 20 20 2f 2a 20 54 68 65 20 69 6e 61 62 f.. /* The inab
b9310 69 6c 69 74 79 20 74 6f 20 61 6c 6c 6f 63 61 74 ility to allocat
b9320 65 73 20 73 70 61 63 65 20 66 6f 72 20 61 20 6c es space for a l
b9330 61 72 67 65 72 20 68 61 73 68 20 74 61 62 6c 65 arger hash table
b9340 20 69 73 0a 20 20 2a 2a 20 61 20 70 65 72 66 6f is. ** a perfo
b9350 72 6d 61 6e 63 65 20 68 69 74 20 62 75 74 20 69 rmance hit but i
b9360 74 20 69 73 20 6e 6f 74 20 61 20 66 61 74 61 6c t is not a fatal
b9370 20 65 72 72 6f 72 2e 20 20 53 6f 20 6d 61 72 6b error. So mark
b9380 20 74 68 65 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 the. ** alloca
b9390 74 69 6f 6e 20 61 73 20 61 20 62 65 6e 69 67 6e tion as a benign
b93a0 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .. */. sqlite3
b93b0 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f BeginBenignMallo
b93c0 63 28 29 3b 0a 20 20 6e 65 77 5f 68 74 20 3d 20 c();. new_ht =
b93d0 28 73 74 72 75 63 74 20 5f 68 74 20 2a 29 73 71 (struct _ht *)sq
b93e0 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 65 77 lite3Malloc( new
b93f0 5f 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 _size*sizeof(str
b9400 75 63 74 20 5f 68 74 29 20 29 3b 0a 20 20 73 71 uct _ht) );. sq
b9410 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 lite3EndBenignMa
b9420 6c 6c 6f 63 28 29 3b 0a 0a 20 20 69 66 28 20 6e lloc();.. if( n
b9430 65 77 5f 68 74 3d 3d 30 20 29 20 72 65 74 75 72 ew_ht==0 ) retur
b9440 6e 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 n 0;. sqlite3_f
b9450 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 ree(pH->ht);. p
b9460 48 2d 3e 68 74 20 3d 20 6e 65 77 5f 68 74 3b 0a H->ht = new_ht;.
b9470 20 20 70 48 2d 3e 68 74 73 69 7a 65 20 3d 20 6e pH->htsize = n
b9480 65 77 5f 73 69 7a 65 20 3d 20 73 71 6c 69 74 65 ew_size = sqlite
b9490 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 6e 65 77 5f 3MallocSize(new_
b94a0 68 74 29 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 ht)/sizeof(struc
b94b0 74 20 5f 68 74 29 3b 0a 20 20 6d 65 6d 73 65 74 t _ht);. memset
b94c0 28 6e 65 77 5f 68 74 2c 20 30 2c 20 6e 65 77 5f (new_ht, 0, new_
b94d0 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75 size*sizeof(stru
b94e0 63 74 20 5f 68 74 29 29 3b 0a 20 20 66 6f 72 28 ct _ht));. for(
b94f0 65 6c 65 6d 3d 70 48 2d 3e 66 69 72 73 74 2c 20 elem=pH->first,
b9500 70 48 2d 3e 66 69 72 73 74 3d 30 3b 20 65 6c 65 pH->first=0; ele
b9510 6d 3b 20 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 m; elem = next_e
b9520 6c 65 6d 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e lem){. unsign
b9530 65 64 20 69 6e 74 20 68 20 3d 20 73 74 72 48 61 ed int h = strHa
b9540 73 68 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 65 sh(elem->pKey, e
b9550 6c 65 6d 2d 3e 6e 4b 65 79 29 20 25 20 6e 65 77 lem->nKey) % new
b9560 5f 73 69 7a 65 3b 0a 20 20 20 20 6e 65 78 74 5f _size;. next_
b9570 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 elem = elem->nex
b9580 74 3b 0a 20 20 20 20 69 6e 73 65 72 74 45 6c 65 t;. insertEle
b9590 6d 65 6e 74 28 70 48 2c 20 26 6e 65 77 5f 68 74 ment(pH, &new_ht
b95a0 5b 68 5d 2c 20 65 6c 65 6d 29 3b 0a 20 20 7d 0a [h], elem);. }.
b95b0 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f return 1;.}../
b95c0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
b95d0 28 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 (for internal us
b95e0 65 20 6f 6e 6c 79 29 20 6c 6f 63 61 74 65 73 20 e only) locates
b95f0 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 20 61 6e an element in an
b9600 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 74 .** hash table t
b9610 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20 hat matches the
b9620 67 69 76 65 6e 20 6b 65 79 2e 20 20 54 68 65 20 given key. The
b9630 68 61 73 68 20 66 6f 72 20 74 68 69 73 20 6b 65 hash for this ke
b9640 79 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 y has.** already
b9650 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 20 61 been computed a
b9660 6e 64 20 69 73 20 70 61 73 73 65 64 20 61 73 20 nd is passed as
b9670 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 the 4th paramete
b9680 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 48 61 73 r..*/.static Has
b9690 68 45 6c 65 6d 20 2a 66 69 6e 64 45 6c 65 6d 65 hElem *findEleme
b96a0 6e 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20 63 ntGivenHash(. c
b96b0 6f 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20 20 onst Hash *pH,
b96c0 20 20 20 2f 2a 20 54 68 65 20 70 48 20 74 6f 20 /* The pH to
b96d0 62 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 be searched */.
b96e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 const char *pKe
b96f0 79 2c 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 y, /* The key
b9700 77 65 20 61 72 65 20 73 65 61 72 63 68 69 6e 67 we are searching
b9710 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b for */. int nK
b9720 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ey, /*
b9730 20 42 79 74 65 73 20 69 6e 20 6b 65 79 20 28 6e Bytes in key (n
b9740 6f 74 20 63 6f 75 6e 74 69 6e 67 20 7a 65 72 6f ot counting zero
b9750 20 74 65 72 6d 69 6e 61 74 6f 72 29 20 2a 2f 0a terminator) */.
b9760 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 unsigned int h
b9770 20 20 20 20 20 20 2f 2a 20 54 68 65 20 68 61 73 /* The has
b9780 68 20 66 6f 72 20 74 68 69 73 20 6b 65 79 2e 20 h for this key.
b9790 2a 2f 0a 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d */.){. HashElem
b97a0 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 *elem;
b97b0 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 /* Used t
b97c0 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20 o loop thru the
b97d0 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a element list */.
b97e0 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 int count;
b97f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b9800 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c /* Number of el
b9810 65 6d 65 6e 74 73 20 6c 65 66 74 20 74 6f 20 74 ements left to t
b9820 65 73 74 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 est */.. if( pH
b9830 2d 3e 68 74 20 29 7b 0a 20 20 20 20 73 74 72 75 ->ht ){. stru
b9840 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 20 3d ct _ht *pEntry =
b9850 20 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 &pH->ht[h];.
b9860 20 65 6c 65 6d 20 3d 20 70 45 6e 74 72 79 2d 3e elem = pEntry->
b9870 63 68 61 69 6e 3b 0a 20 20 20 20 63 6f 75 6e 74 chain;. count
b9880 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 = pEntry->count
b9890 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 65 ;. }else{. e
b98a0 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b lem = pH->first;
b98b0 0a 20 20 20 20 63 6f 75 6e 74 20 3d 20 70 48 2d . count = pH-
b98c0 3e 63 6f 75 6e 74 3b 0a 20 20 7d 0a 20 20 77 68 >count;. }. wh
b98d0 69 6c 65 28 20 63 6f 75 6e 74 2d 2d 20 26 26 20 ile( count-- &&
b98e0 41 4c 57 41 59 53 28 65 6c 65 6d 29 20 29 7b 0a ALWAYS(elem) ){.
b98f0 20 20 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 4b if( elem->nK
b9900 65 79 3d 3d 6e 4b 65 79 20 26 26 20 73 71 6c 69 ey==nKey && sqli
b9910 74 65 33 53 74 72 4e 49 43 6d 70 28 65 6c 65 6d te3StrNICmp(elem
b9920 2d 3e 70 4b 65 79 2c 70 4b 65 79 2c 6e 4b 65 79 ->pKey,pKey,nKey
b9930 29 3d 3d 30 20 29 7b 20 0a 20 20 20 20 20 20 72 )==0 ){ . r
b9940 65 74 75 72 6e 20 65 6c 65 6d 3b 0a 20 20 20 20 eturn elem;.
b9950 7d 0a 20 20 20 20 65 6c 65 6d 20 3d 20 65 6c 65 }. elem = ele
b9960 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 m->next;. }. r
b9970 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 eturn 0;.}../* R
b9980 65 6d 6f 76 65 20 61 20 73 69 6e 67 6c 65 20 65 emove a single e
b9990 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 68 61 ntry from the ha
b99a0 73 68 20 74 61 62 6c 65 20 67 69 76 65 6e 20 61 sh table given a
b99b0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 pointer to that
b99c0 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 61 6e 64 20 .** element and
b99d0 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20 65 6c a hash on the el
b99e0 65 6d 65 6e 74 27 73 20 6b 65 79 2e 0a 2a 2f 0a ement's key..*/.
b99f0 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6d 6f static void remo
b9a00 76 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 veElementGivenHa
b9a10 73 68 28 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 sh(. Hash *pH,
b9a20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 /* The p
b9a30 48 20 63 6f 6e 74 61 69 6e 69 6e 67 20 22 65 6c H containing "el
b9a40 65 6d 22 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 em" */. HashEle
b9a50 6d 2a 20 65 6c 65 6d 2c 20 20 20 2f 2a 20 54 68 m* elem, /* Th
b9a60 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 e element to be
b9a70 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 removed from the
b9a80 20 70 48 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 pH */. unsigne
b9a90 64 20 69 6e 74 20 68 20 20 20 20 2f 2a 20 48 61 d int h /* Ha
b9aa0 73 68 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 sh value for the
b9ab0 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 element */.){.
b9ac0 20 73 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e struct _ht *pEn
b9ad0 74 72 79 3b 0a 20 20 69 66 28 20 65 6c 65 6d 2d try;. if( elem-
b9ae0 3e 70 72 65 76 20 29 7b 0a 20 20 20 20 65 6c 65 >prev ){. ele
b9af0 6d 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 m->prev->next =
b9b00 65 6c 65 6d 2d 3e 6e 65 78 74 3b 20 0a 20 20 7d elem->next; . }
b9b10 65 6c 73 65 7b 0a 20 20 20 20 70 48 2d 3e 66 69 else{. pH->fi
b9b20 72 73 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 rst = elem->next
b9b30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 65 6c 65 6d ;. }. if( elem
b9b40 2d 3e 6e 65 78 74 20 29 7b 0a 20 20 20 20 65 6c ->next ){. el
b9b50 65 6d 2d 3e 6e 65 78 74 2d 3e 70 72 65 76 20 3d em->next->prev =
b9b60 20 65 6c 65 6d 2d 3e 70 72 65 76 3b 0a 20 20 7d elem->prev;. }
b9b70 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b . if( pH->ht ){
b9b80 0a 20 20 20 20 70 45 6e 74 72 79 20 3d 20 26 70 . pEntry = &p
b9b90 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20 69 66 H->ht[h];. if
b9ba0 28 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3d ( pEntry->chain=
b9bb0 3d 65 6c 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 =elem ){. p
b9bc0 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 65 Entry->chain = e
b9bd0 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 7d lem->next;. }
b9be0 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 . pEntry->cou
b9bf0 6e 74 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 nt--;. assert
b9c00 28 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3e ( pEntry->count>
b9c10 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 =0 );. }. sqli
b9c20 74 65 33 5f 66 72 65 65 28 20 65 6c 65 6d 20 29 te3_free( elem )
b9c30 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2d 2d 3b ;. pH->count--;
b9c40 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 . if( pH->count
b9c50 3c 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 <=0 ){. asser
b9c60 74 28 20 70 48 2d 3e 66 69 72 73 74 3d 3d 30 20 t( pH->first==0
b9c70 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
b9c80 48 2d 3e 63 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 H->count==0 );.
b9c90 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c sqlite3HashCl
b9ca0 65 61 72 28 70 48 29 3b 0a 20 20 7d 0a 7d 0a 0a ear(pH);. }.}..
b9cb0 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f /* Attempt to lo
b9cc0 63 61 74 65 20 61 6e 20 65 6c 65 6d 65 6e 74 20 cate an element
b9cd0 6f 66 20 74 68 65 20 68 61 73 68 20 74 61 62 6c of the hash tabl
b9ce0 65 20 70 48 20 77 69 74 68 20 61 20 6b 65 79 0a e pH with a key.
b9cf0 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 ** that matches
b9d00 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 52 65 74 75 pKey,nKey. Retu
b9d10 72 6e 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 rn the data for
b9d20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 69 66 20 this element if
b9d30 69 74 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 it is.** found,
b9d40 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 or NULL if there
b9d50 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f is no match..*/
b9d60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b9d70 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 void *sqlite3Has
b9d80 68 46 69 6e 64 28 63 6f 6e 73 74 20 48 61 73 68 hFind(const Hash
b9d90 20 2a 70 48 2c 20 63 6f 6e 73 74 20 63 68 61 72 *pH, const char
b9da0 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 *pKey, int nKey
b9db0 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 ){. HashElem *e
b9dc0 6c 65 6d 3b 20 20 20 20 2f 2a 20 54 68 65 20 65 lem; /* The e
b9dd0 6c 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 74 63 lement that matc
b9de0 68 65 73 20 6b 65 79 20 2a 2f 0a 20 20 75 6e 73 hes key */. uns
b9df0 69 67 6e 65 64 20 69 6e 74 20 68 3b 20 20 20 20 igned int h;
b9e00 2f 2a 20 41 20 68 61 73 68 20 6f 6e 20 6b 65 79 /* A hash on key
b9e10 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
b9e20 48 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 H!=0 );. assert
b9e30 28 20 70 4b 65 79 21 3d 30 20 29 3b 0a 20 20 61 ( pKey!=0 );. a
b9e40 73 73 65 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 ssert( nKey>=0 )
b9e50 3b 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 ;. if( pH->ht )
b9e60 7b 0a 20 20 20 20 68 20 3d 20 73 74 72 48 61 73 {. h = strHas
b9e70 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 h(pKey, nKey) %
b9e80 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 7d 65 pH->htsize;. }e
b9e90 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 30 3b 0a lse{. h = 0;.
b9ea0 20 20 7d 0a 20 20 65 6c 65 6d 20 3d 20 66 69 6e }. elem = fin
b9eb0 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 dElementGivenHas
b9ec0 68 28 70 48 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 h(pH, pKey, nKey
b9ed0 2c 20 68 29 3b 0a 20 20 72 65 74 75 72 6e 20 65 , h);. return e
b9ee0 6c 65 6d 20 3f 20 65 6c 65 6d 2d 3e 64 61 74 61 lem ? elem->data
b9ef0 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 20 49 6e 73 65 : 0;.}../* Inse
b9f00 72 74 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e rt an element in
b9f10 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c to the hash tabl
b9f20 65 20 70 48 2e 20 20 54 68 65 20 6b 65 79 20 69 e pH. The key i
b9f30 73 20 70 4b 65 79 2c 6e 4b 65 79 0a 2a 2a 20 61 s pKey,nKey.** a
b9f40 6e 64 20 74 68 65 20 64 61 74 61 20 69 73 20 22 nd the data is "
b9f50 64 61 74 61 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 data"..**.** If
b9f60 6e 6f 20 65 6c 65 6d 65 6e 74 20 65 78 69 73 74 no element exist
b9f70 73 20 77 69 74 68 20 61 20 6d 61 74 63 68 69 6e s with a matchin
b9f80 67 20 6b 65 79 2c 20 74 68 65 6e 20 61 20 6e 65 g key, then a ne
b9f90 77 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 69 73 20 w.** element is
b9fa0 63 72 65 61 74 65 64 20 61 6e 64 20 4e 55 4c 4c created and NULL
b9fb0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
b9fc0 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 65 .** If another e
b9fd0 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 65 lement already e
b9fe0 78 69 73 74 73 20 77 69 74 68 20 74 68 65 20 73 xists with the s
b9ff0 61 6d 65 20 6b 65 79 2c 20 74 68 65 6e 20 74 68 ame key, then th
ba000 65 0a 2a 2a 20 6e 65 77 20 64 61 74 61 20 72 65 e.** new data re
ba010 70 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 20 64 places the old d
ba020 61 74 61 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 ata and the old
ba030 64 61 74 61 20 69 73 20 72 65 74 75 72 6e 65 64 data is returned
ba040 2e 0a 2a 2a 20 54 68 65 20 6b 65 79 20 69 73 20 ..** The key is
ba050 6e 6f 74 20 63 6f 70 69 65 64 20 69 6e 20 74 68 not copied in th
ba060 69 73 20 69 6e 73 74 61 6e 63 65 2e 20 20 49 66 is instance. If
ba070 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c a malloc fails,
ba080 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 then.** the new
ba090 20 64 61 74 61 20 69 73 20 72 65 74 75 72 6e 65 data is returne
ba0a0 64 20 61 6e 64 20 74 68 65 20 68 61 73 68 20 74 d and the hash t
ba0b0 61 62 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 able is unchange
ba0c0 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
ba0d0 22 64 61 74 61 22 20 70 61 72 61 6d 65 74 65 72 "data" parameter
ba0e0 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f to this functio
ba0f0 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 n is NULL, then
ba100 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 63 the.** element c
ba110 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 orresponding to
ba120 22 6b 65 79 22 20 69 73 20 72 65 6d 6f 76 65 64 "key" is removed
ba130 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 from the hash t
ba140 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f able..*/.SQLITE_
ba150 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
ba160 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 lite3HashInsert(
ba170 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20 Hash *pH, const
ba180 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20 char *pKey, int
ba190 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 64 61 74 61 nKey, void *data
ba1a0 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e ){. unsigned in
ba1b0 74 20 68 3b 20 20 20 20 20 20 20 2f 2a 20 74 68 t h; /* th
ba1c0 65 20 68 61 73 68 20 6f 66 20 74 68 65 20 6b 65 e hash of the ke
ba1d0 79 20 6d 6f 64 75 6c 6f 20 68 61 73 68 20 74 61 y modulo hash ta
ba1e0 62 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 20 48 61 ble size */. Ha
ba1f0 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 shElem *elem;
ba200 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c /* Used to l
ba210 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c 65 oop thru the ele
ba220 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 48 ment list */. H
ba230 61 73 68 45 6c 65 6d 20 2a 6e 65 77 5f 65 6c 65 ashElem *new_ele
ba240 6d 3b 20 20 20 2f 2a 20 4e 65 77 20 65 6c 65 6d m; /* New elem
ba250 65 6e 74 20 61 64 64 65 64 20 74 6f 20 74 68 65 ent added to the
ba260 20 70 48 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 pH */.. assert
ba270 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 61 73 73 ( pH!=0 );. ass
ba280 65 72 74 28 20 70 4b 65 79 21 3d 30 20 29 3b 0a ert( pKey!=0 );.
ba290 20 20 61 73 73 65 72 74 28 20 6e 4b 65 79 3e 3d assert( nKey>=
ba2a0 30 20 29 3b 0a 20 20 69 66 28 20 70 48 2d 3e 68 0 );. if( pH->h
ba2b0 74 73 69 7a 65 20 29 7b 0a 20 20 20 20 68 20 3d tsize ){. h =
ba2c0 20 73 74 72 48 61 73 68 28 70 4b 65 79 2c 20 6e strHash(pKey, n
ba2d0 4b 65 79 29 20 25 20 70 48 2d 3e 68 74 73 69 7a Key) % pH->htsiz
ba2e0 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 e;. }else{.
ba2f0 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 65 6c 65 h = 0;. }. ele
ba300 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e 74 47 m = findElementG
ba310 69 76 65 6e 48 61 73 68 28 70 48 2c 70 4b 65 79 ivenHash(pH,pKey
ba320 2c 6e 4b 65 79 2c 68 29 3b 0a 20 20 69 66 28 20 ,nKey,h);. if(
ba330 65 6c 65 6d 20 29 7b 0a 20 20 20 20 76 6f 69 64 elem ){. void
ba340 20 2a 6f 6c 64 5f 64 61 74 61 20 3d 20 65 6c 65 *old_data = ele
ba350 6d 2d 3e 64 61 74 61 3b 0a 20 20 20 20 69 66 28 m->data;. if(
ba360 20 64 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 data==0 ){.
ba370 20 20 72 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 47 removeElementG
ba380 69 76 65 6e 48 61 73 68 28 70 48 2c 65 6c 65 6d ivenHash(pH,elem
ba390 2c 68 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ,h);. }else{.
ba3a0 20 20 20 20 20 20 65 6c 65 6d 2d 3e 64 61 74 61 elem->data
ba3b0 20 3d 20 64 61 74 61 3b 0a 20 20 20 20 20 20 65 = data;. e
ba3c0 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79 lem->pKey = pKey
ba3d0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e ;. assert(n
ba3e0 4b 65 79 3d 3d 65 6c 65 6d 2d 3e 6e 4b 65 79 29 Key==elem->nKey)
ba3f0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
ba400 72 6e 20 6f 6c 64 5f 64 61 74 61 3b 0a 20 20 7d rn old_data;. }
ba410 0a 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20 29 . if( data==0 )
ba420 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 65 77 return 0;. new
ba430 5f 65 6c 65 6d 20 3d 20 28 48 61 73 68 45 6c 65 _elem = (HashEle
ba440 6d 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 m*)sqlite3Malloc
ba450 28 20 73 69 7a 65 6f 66 28 48 61 73 68 45 6c 65 ( sizeof(HashEle
ba460 6d 29 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f m) );. if( new_
ba470 65 6c 65 6d 3d 3d 30 20 29 20 72 65 74 75 72 6e elem==0 ) return
ba480 20 64 61 74 61 3b 0a 20 20 6e 65 77 5f 65 6c 65 data;. new_ele
ba490 6d 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a m->pKey = pKey;.
ba4a0 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 new_elem->nKey
ba4b0 20 3d 20 6e 4b 65 79 3b 0a 20 20 6e 65 77 5f 65 = nKey;. new_e
ba4c0 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 lem->data = data
ba4d0 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2b 2b 3b ;. pH->count++;
ba4e0 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 . if( pH->count
ba4f0 3e 3d 31 30 20 26 26 20 70 48 2d 3e 63 6f 75 6e >=10 && pH->coun
ba500 74 20 3e 20 32 2a 70 48 2d 3e 68 74 73 69 7a 65 t > 2*pH->htsize
ba510 20 29 7b 0a 20 20 20 20 69 66 28 20 72 65 68 61 ){. if( reha
ba520 73 68 28 70 48 2c 20 70 48 2d 3e 63 6f 75 6e 74 sh(pH, pH->count
ba530 2a 32 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 *2) ){. ass
ba540 65 72 74 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e ert( pH->htsize>
ba550 30 20 29 3b 0a 20 20 20 20 20 20 68 20 3d 20 73 0 );. h = s
ba560 74 72 48 61 73 68 28 70 4b 65 79 2c 20 6e 4b 65 trHash(pKey, nKe
ba570 79 29 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b y) % pH->htsize;
ba580 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
ba590 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 69 pH->ht ){. i
ba5a0 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c nsertElement(pH,
ba5b0 20 26 70 48 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 &pH->ht[h], new
ba5c0 5f 65 6c 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b _elem);. }else{
ba5d0 0a 20 20 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 . insertEleme
ba5e0 6e 74 28 70 48 2c 20 30 2c 20 6e 65 77 5f 65 6c nt(pH, 0, new_el
ba5f0 65 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 em);. }. retur
ba600 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a n 0;.}../*******
ba610 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 ******* End of h
ba620 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ash.c **********
ba630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba650 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
ba660 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
ba670 6c 65 20 6f 70 63 6f 64 65 73 2e 63 20 2a 2a 2a le opcodes.c ***
ba680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba6a0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 41 75 74 6f 6d ******/./* Autom
ba6b0 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 atically generat
ba6c0 65 64 2e 20 20 44 6f 20 6e 6f 74 20 65 64 69 74 ed. Do not edit
ba6d0 20 2a 2f 0a 2f 2a 20 53 65 65 20 74 68 65 20 6d */./* See the m
ba6e0 6b 6f 70 63 6f 64 65 63 2e 61 77 6b 20 73 63 72 kopcodec.awk scr
ba6f0 69 70 74 20 66 6f 72 20 64 65 74 61 69 6c 73 2e ipt for details.
ba700 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 */.#if !defined
ba710 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 (SQLITE_OMIT_EXP
ba720 4c 41 49 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65 LAIN) || !define
ba730 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 d(NDEBUG) || def
ba740 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c ined(VDBE_PROFIL
ba750 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 E) || defined(SQ
ba760 4c 49 54 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 LITE_DEBUG).SQLI
ba770 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
ba780 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4f 70 char *sqlite3Op
ba790 63 6f 64 65 4e 61 6d 65 28 69 6e 74 20 69 29 7b codeName(int i){
ba7a0 0a 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 . static const c
ba7b0 68 61 72 20 2a 63 6f 6e 73 74 20 61 7a 4e 61 6d har *const azNam
ba7c0 65 5b 5d 20 3d 20 7b 20 22 3f 22 2c 0a 20 20 20 e[] = { "?",.
ba7d0 20 20 2f 2a 20 20 20 31 20 2a 2f 20 22 52 65 61 /* 1 */ "Rea
ba7e0 64 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f dCookie",. /
ba7f0 2a 20 20 20 32 20 2a 2f 20 22 41 75 74 6f 43 6f * 2 */ "AutoCo
ba800 6d 6d 69 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 mmit",. /*
ba810 20 33 20 2a 2f 20 22 46 6f 75 6e 64 22 2c 0a 20 3 */ "Found",.
ba820 20 20 20 20 2f 2a 20 20 20 34 20 2a 2f 20 22 4e /* 4 */ "N
ba830 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a ullRow",. /*
ba840 20 20 20 35 20 2a 2f 20 22 52 6f 77 53 65 74 54 5 */ "RowSetT
ba850 65 73 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 est",. /*
ba860 36 20 2a 2f 20 22 56 61 72 69 61 62 6c 65 22 2c 6 */ "Variable",
ba870 0a 20 20 20 20 20 2f 2a 20 20 20 37 20 2a 2f 20 . /* 7 */
ba880 22 52 65 61 6c 41 66 66 69 6e 69 74 79 22 2c 0a "RealAffinity",.
ba890 20 20 20 20 20 2f 2a 20 20 20 38 20 2a 2f 20 22 /* 8 */ "
ba8a0 53 6f 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Sort",. /*
ba8b0 20 39 20 2a 2f 20 22 41 66 66 69 6e 69 74 79 22 9 */ "Affinity"
ba8c0 2c 0a 20 20 20 20 20 2f 2a 20 20 31 30 20 2a 2f ,. /* 10 */
ba8d0 20 22 49 66 4e 6f 74 22 2c 0a 20 20 20 20 20 2f "IfNot",. /
ba8e0 2a 20 20 31 31 20 2a 2f 20 22 47 6f 73 75 62 22 * 11 */ "Gosub"
ba8f0 2c 0a 20 20 20 20 20 2f 2a 20 20 31 32 20 2a 2f ,. /* 12 */
ba900 20 22 4e 6f 74 46 6f 75 6e 64 22 2c 0a 20 20 20 "NotFound",.
ba910 20 20 2f 2a 20 20 31 33 20 2a 2f 20 22 52 65 73 /* 13 */ "Res
ba920 75 6c 74 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a ultRow",. /*
ba930 20 20 31 34 20 2a 2f 20 22 53 65 65 6b 4c 65 22 14 */ "SeekLe"
ba940 2c 0a 20 20 20 20 20 2f 2a 20 20 31 35 20 2a 2f ,. /* 15 */
ba950 20 22 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f "Rowid",. /
ba960 2a 20 20 31 36 20 2a 2f 20 22 43 72 65 61 74 65 * 16 */ "Create
ba970 49 6e 64 65 78 22 2c 0a 20 20 20 20 20 2f 2a 20 Index",. /*
ba980 20 31 37 20 2a 2f 20 22 45 78 70 6c 61 69 6e 22 17 */ "Explain"
ba990 2c 0a 20 20 20 20 20 2f 2a 20 20 31 38 20 2a 2f ,. /* 18 */
ba9a0 20 22 44 72 6f 70 49 6e 64 65 78 22 2c 0a 20 20 "DropIndex",.
ba9b0 20 20 20 2f 2a 20 20 31 39 20 2a 2f 20 22 4e 6f /* 19 */ "No
ba9c0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 30 20 t",. /* 20
ba9d0 2a 2f 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 */ "Null",.
ba9e0 2f 2a 20 20 32 31 20 2a 2f 20 22 50 72 6f 67 72 /* 21 */ "Progr
ba9f0 61 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 32 am",. /* 22
baa00 20 2a 2f 20 22 49 6e 74 36 34 22 2c 0a 20 20 20 */ "Int64",.
baa10 20 20 2f 2a 20 20 32 33 20 2a 2f 20 22 4c 6f 61 /* 23 */ "Loa
baa20 64 41 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20 20 dAnalysis",.
baa30 20 2f 2a 20 20 32 34 20 2a 2f 20 22 49 64 78 49 /* 24 */ "IdxI
baa40 6e 73 65 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 nsert",. /*
baa50 20 32 35 20 2a 2f 20 22 56 55 70 64 61 74 65 22 25 */ "VUpdate"
baa60 2c 0a 20 20 20 20 20 2f 2a 20 20 32 36 20 2a 2f ,. /* 26 */
baa70 20 22 4e 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a "Next",. /*
baa80 20 20 32 37 20 2a 2f 20 22 53 65 65 6b 4c 74 22 27 */ "SeekLt"
baa90 2c 0a 20 20 20 20 20 2f 2a 20 20 32 38 20 2a 2f ,. /* 28 */
baaa0 20 22 52 65 77 69 6e 64 22 2c 0a 20 20 20 20 20 "Rewind",.
baab0 2f 2a 20 20 32 39 20 2a 2f 20 22 52 6f 77 53 65 /* 29 */ "RowSe
baac0 74 52 65 61 64 22 2c 0a 20 20 20 20 20 2f 2a 20 tRead",. /*
baad0 20 33 30 20 2a 2f 20 22 4c 61 73 74 22 2c 0a 20 30 */ "Last",.
baae0 20 20 20 20 2f 2a 20 20 33 31 20 2a 2f 20 22 4d /* 31 */ "M
baaf0 75 73 74 42 65 49 6e 74 22 2c 0a 20 20 20 20 20 ustBeInt",.
bab00 2f 2a 20 20 33 32 20 2a 2f 20 22 49 6e 63 72 56 /* 32 */ "IncrV
bab10 61 63 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 acuum",. /*
bab20 20 33 33 20 2a 2f 20 22 53 74 72 69 6e 67 22 2c 33 */ "String",
bab30 0a 20 20 20 20 20 2f 2a 20 20 33 34 20 2a 2f 20 . /* 34 */
bab40 22 56 46 69 6c 74 65 72 22 2c 0a 20 20 20 20 20 "VFilter",.
bab50 2f 2a 20 20 33 35 20 2a 2f 20 22 43 6f 75 6e 74 /* 35 */ "Count
bab60 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 36 20 2a ",. /* 36 *
bab70 2f 20 22 43 6c 6f 73 65 22 2c 0a 20 20 20 20 20 / "Close",.
bab80 2f 2a 20 20 33 37 20 2a 2f 20 22 41 67 67 46 69 /* 37 */ "AggFi
bab90 6e 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 nal",. /* 3
baba0 38 20 2a 2f 20 22 52 6f 77 44 61 74 61 22 2c 0a 8 */ "RowData",.
babb0 20 20 20 20 20 2f 2a 20 20 33 39 20 2a 2f 20 22 /* 39 */ "
babc0 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 IdxRowid",.
babd0 2f 2a 20 20 34 30 20 2a 2f 20 22 50 61 72 61 6d /* 40 */ "Param
babe0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 31 20 2a ",. /* 41 *
babf0 2f 20 22 50 61 67 65 63 6f 75 6e 74 22 2c 0a 20 / "Pagecount",.
bac00 20 20 20 20 2f 2a 20 20 34 32 20 2a 2f 20 22 53 /* 42 */ "S
bac10 65 65 6b 47 65 22 2c 0a 20 20 20 20 20 2f 2a 20 eekGe",. /*
bac20 20 34 33 20 2a 2f 20 22 4f 70 65 6e 50 73 65 75 43 */ "OpenPseu
bac30 64 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 34 do",. /* 44
bac40 20 2a 2f 20 22 48 61 6c 74 22 2c 0a 20 20 20 20 */ "Halt",.
bac50 20 2f 2a 20 20 34 35 20 2a 2f 20 22 43 6f 6d 70 /* 45 */ "Comp
bac60 61 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 are",. /* 4
bac70 36 20 2a 2f 20 22 4e 65 77 52 6f 77 69 64 22 2c 6 */ "NewRowid",
bac80 0a 20 20 20 20 20 2f 2a 20 20 34 37 20 2a 2f 20 . /* 47 */
bac90 22 49 64 78 4c 54 22 2c 0a 20 20 20 20 20 2f 2a "IdxLT",. /*
baca0 20 20 34 38 20 2a 2f 20 22 53 65 65 6b 47 74 22 48 */ "SeekGt"
bacb0 2c 0a 20 20 20 20 20 2f 2a 20 20 34 39 20 2a 2f ,. /* 49 */
bacc0 20 22 4d 65 6d 4d 61 78 22 2c 0a 20 20 20 20 20 "MemMax",.
bacd0 2f 2a 20 20 35 30 20 2a 2f 20 22 46 75 6e 63 74 /* 50 */ "Funct
bace0 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 ion",. /* 5
bacf0 31 20 2a 2f 20 22 49 6e 74 65 67 72 69 74 79 43 1 */ "IntegrityC
bad00 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 32 20 k",. /* 52
bad10 2a 2f 20 22 46 6b 43 6f 75 6e 74 65 72 22 2c 0a */ "FkCounter",.
bad20 20 20 20 20 20 2f 2a 20 20 35 33 20 2a 2f 20 22 /* 53 */ "
bad30 53 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f 2a 20 SCopy",. /*
bad40 20 35 34 20 2a 2f 20 22 49 66 4e 65 67 22 2c 0a 54 */ "IfNeg",.
bad50 20 20 20 20 20 2f 2a 20 20 35 35 20 2a 2f 20 22 /* 55 */ "
bad60 4e 6f 74 45 78 69 73 74 73 22 2c 0a 20 20 20 20 NotExists",.
bad70 20 2f 2a 20 20 35 36 20 2a 2f 20 22 56 44 65 73 /* 56 */ "VDes
bad80 74 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 troy",. /*
bad90 35 37 20 2a 2f 20 22 49 64 78 44 65 6c 65 74 65 57 */ "IdxDelete
bada0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 38 20 2a ",. /* 58 *
badb0 2f 20 22 56 61 63 75 75 6d 22 2c 0a 20 20 20 20 / "Vacuum",.
badc0 20 2f 2a 20 20 35 39 20 2a 2f 20 22 43 6f 70 79 /* 59 */ "Copy
badd0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 30 20 2a ",. /* 60 *
bade0 2f 20 22 49 66 22 2c 0a 20 20 20 20 20 2f 2a 20 / "If",. /*
badf0 20 36 31 20 2a 2f 20 22 4a 75 6d 70 22 2c 0a 20 61 */ "Jump",.
bae00 20 20 20 20 2f 2a 20 20 36 32 20 2a 2f 20 22 44 /* 62 */ "D
bae10 65 73 74 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a estroy",. /*
bae20 20 20 36 33 20 2a 2f 20 22 41 67 67 53 74 65 70 63 */ "AggStep
bae30 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 34 20 2a ",. /* 64 *
bae40 2f 20 22 43 6c 65 61 72 22 2c 0a 20 20 20 20 20 / "Clear",.
bae50 2f 2a 20 20 36 35 20 2a 2f 20 22 49 6e 73 65 72 /* 65 */ "Inser
bae60 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 36 20 t",. /* 66
bae70 2a 2f 20 22 50 65 72 6d 75 74 61 74 69 6f 6e 22 */ "Permutation"
bae80 2c 0a 20 20 20 20 20 2f 2a 20 20 36 37 20 2a 2f ,. /* 67 */
bae90 20 22 56 42 65 67 69 6e 22 2c 0a 20 20 20 20 20 "VBegin",.
baea0 2f 2a 20 20 36 38 20 2a 2f 20 22 4f 72 22 2c 0a /* 68 */ "Or",.
baeb0 20 20 20 20 20 2f 2a 20 20 36 39 20 2a 2f 20 22 /* 69 */ "
baec0 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 And",. /* 7
baed0 30 20 2a 2f 20 22 4f 70 65 6e 45 70 68 65 6d 65 0 */ "OpenEpheme
baee0 72 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 ral",. /* 7
baef0 31 20 2a 2f 20 22 49 64 78 47 45 22 2c 0a 20 20 1 */ "IdxGE",.
baf00 20 20 20 2f 2a 20 20 37 32 20 2a 2f 20 22 54 72 /* 72 */ "Tr
baf10 61 63 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 ace",. /* 7
baf20 33 20 2a 2f 20 22 49 73 4e 75 6c 6c 22 2c 0a 20 3 */ "IsNull",.
baf30 20 20 20 20 2f 2a 20 20 37 34 20 2a 2f 20 22 4e /* 74 */ "N
baf40 6f 74 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a otNull",. /*
baf50 20 20 37 35 20 2a 2f 20 22 4e 65 22 2c 0a 20 20 75 */ "Ne",.
baf60 20 20 20 2f 2a 20 20 37 36 20 2a 2f 20 22 45 71 /* 76 */ "Eq
baf70 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 37 20 2a ",. /* 77 *
baf80 2f 20 22 47 74 22 2c 0a 20 20 20 20 20 2f 2a 20 / "Gt",. /*
baf90 20 37 38 20 2a 2f 20 22 4c 65 22 2c 0a 20 20 20 78 */ "Le",.
bafa0 20 20 2f 2a 20 20 37 39 20 2a 2f 20 22 4c 74 22 /* 79 */ "Lt"
bafb0 2c 0a 20 20 20 20 20 2f 2a 20 20 38 30 20 2a 2f ,. /* 80 */
bafc0 20 22 47 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 "Ge",. /*
bafd0 38 31 20 2a 2f 20 22 4d 61 6b 65 52 65 63 6f 72 81 */ "MakeRecor
bafe0 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 32 20 d",. /* 82
baff0 2a 2f 20 22 42 69 74 41 6e 64 22 2c 0a 20 20 20 */ "BitAnd",.
bb000 20 20 2f 2a 20 20 38 33 20 2a 2f 20 22 42 69 74 /* 83 */ "Bit
bb010 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 34 Or",. /* 84
bb020 20 2a 2f 20 22 53 68 69 66 74 4c 65 66 74 22 2c */ "ShiftLeft",
bb030 0a 20 20 20 20 20 2f 2a 20 20 38 35 20 2a 2f 20 . /* 85 */
bb040 22 53 68 69 66 74 52 69 67 68 74 22 2c 0a 20 20 "ShiftRight",.
bb050 20 20 20 2f 2a 20 20 38 36 20 2a 2f 20 22 41 64 /* 86 */ "Ad
bb060 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 37 20 d",. /* 87
bb070 2a 2f 20 22 53 75 62 74 72 61 63 74 22 2c 0a 20 */ "Subtract",.
bb080 20 20 20 20 2f 2a 20 20 38 38 20 2a 2f 20 22 4d /* 88 */ "M
bb090 75 6c 74 69 70 6c 79 22 2c 0a 20 20 20 20 20 2f ultiply",. /
bb0a0 2a 20 20 38 39 20 2a 2f 20 22 44 69 76 69 64 65 * 89 */ "Divide
bb0b0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 30 20 2a ",. /* 90 *
bb0c0 2f 20 22 52 65 6d 61 69 6e 64 65 72 22 2c 0a 20 / "Remainder",.
bb0d0 20 20 20 20 2f 2a 20 20 39 31 20 2a 2f 20 22 43 /* 91 */ "C
bb0e0 6f 6e 63 61 74 22 2c 0a 20 20 20 20 20 2f 2a 20 oncat",. /*
bb0f0 20 39 32 20 2a 2f 20 22 59 69 65 6c 64 22 2c 0a 92 */ "Yield",.
bb100 20 20 20 20 20 2f 2a 20 20 39 33 20 2a 2f 20 22 /* 93 */ "
bb110 42 69 74 4e 6f 74 22 2c 0a 20 20 20 20 20 2f 2a BitNot",. /*
bb120 20 20 39 34 20 2a 2f 20 22 53 74 72 69 6e 67 38 94 */ "String8
bb130 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 35 20 2a ",. /* 95 *
bb140 2f 20 22 53 65 74 43 6f 6f 6b 69 65 22 2c 0a 20 / "SetCookie",.
bb150 20 20 20 20 2f 2a 20 20 39 36 20 2a 2f 20 22 50 /* 96 */ "P
bb160 72 65 76 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 rev",. /* 9
bb170 37 20 2a 2f 20 22 44 72 6f 70 54 72 69 67 67 65 7 */ "DropTrigge
bb180 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 38 20 r",. /* 98
bb190 2a 2f 20 22 46 6b 49 66 5a 65 72 6f 22 2c 0a 20 */ "FkIfZero",.
bb1a0 20 20 20 20 2f 2a 20 20 39 39 20 2a 2f 20 22 56 /* 99 */ "V
bb1b0 43 6f 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a Column",. /*
bb1c0 20 31 30 30 20 2a 2f 20 22 52 65 74 75 72 6e 22 100 */ "Return"
bb1d0 2c 0a 20 20 20 20 20 2f 2a 20 31 30 31 20 2a 2f ,. /* 101 */
bb1e0 20 22 4f 70 65 6e 57 72 69 74 65 22 2c 0a 20 20 "OpenWrite",.
bb1f0 20 20 20 2f 2a 20 31 30 32 20 2a 2f 20 22 49 6e /* 102 */ "In
bb200 74 65 67 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 teger",. /*
bb210 31 30 33 20 2a 2f 20 22 54 72 61 6e 73 61 63 74 103 */ "Transact
bb220 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 ion",. /* 10
bb230 34 20 2a 2f 20 22 49 66 50 6f 73 22 2c 0a 20 20 4 */ "IfPos",.
bb240 20 20 20 2f 2a 20 31 30 35 20 2a 2f 20 22 52 6f /* 105 */ "Ro
bb250 77 53 65 74 41 64 64 22 2c 0a 20 20 20 20 20 2f wSetAdd",. /
bb260 2a 20 31 30 36 20 2a 2f 20 22 43 6f 6c 6c 53 65 * 106 */ "CollSe
bb270 71 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 37 20 q",. /* 107
bb280 2a 2f 20 22 53 61 76 65 70 6f 69 6e 74 22 2c 0a */ "Savepoint",.
bb290 20 20 20 20 20 2f 2a 20 31 30 38 20 2a 2f 20 22 /* 108 */ "
bb2a0 56 52 65 6e 61 6d 65 22 2c 0a 20 20 20 20 20 2f VRename",. /
bb2b0 2a 20 31 30 39 20 2a 2f 20 22 53 65 71 75 65 6e * 109 */ "Sequen
bb2c0 63 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 30 ce",. /* 110
bb2d0 20 2a 2f 20 22 48 61 6c 74 49 66 4e 75 6c 6c 22 */ "HaltIfNull"
bb2e0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31 20 2a 2f ,. /* 111 */
bb2f0 20 22 56 43 72 65 61 74 65 22 2c 0a 20 20 20 20 "VCreate",.
bb300 20 2f 2a 20 31 31 32 20 2a 2f 20 22 43 72 65 61 /* 112 */ "Crea
bb310 74 65 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f teTable",. /
bb320 2a 20 31 31 33 20 2a 2f 20 22 41 64 64 49 6d 6d * 113 */ "AddImm
bb330 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 34 20 2a ",. /* 114 *
bb340 2f 20 22 44 72 6f 70 54 61 62 6c 65 22 2c 0a 20 / "DropTable",.
bb350 20 20 20 20 2f 2a 20 31 31 35 20 2a 2f 20 22 49 /* 115 */ "I
bb360 73 55 6e 69 71 75 65 22 2c 0a 20 20 20 20 20 2f sUnique",. /
bb370 2a 20 31 31 36 20 2a 2f 20 22 56 4f 70 65 6e 22 * 116 */ "VOpen"
bb380 2c 0a 20 20 20 20 20 2f 2a 20 31 31 37 20 2a 2f ,. /* 117 */
bb390 20 22 49 66 5a 65 72 6f 22 2c 0a 20 20 20 20 20 "IfZero",.
bb3a0 2f 2a 20 31 31 38 20 2a 2f 20 22 4e 6f 6f 70 22 /* 118 */ "Noop"
bb3b0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 39 20 2a 2f ,. /* 119 */
bb3c0 20 22 49 6e 73 65 72 74 49 6e 74 22 2c 0a 20 20 "InsertInt",.
bb3d0 20 20 20 2f 2a 20 31 32 30 20 2a 2f 20 22 52 6f /* 120 */ "Ro
bb3e0 77 4b 65 79 22 2c 0a 20 20 20 20 20 2f 2a 20 31 wKey",. /* 1
bb3f0 32 31 20 2a 2f 20 22 45 78 70 69 72 65 22 2c 0a 21 */ "Expire",.
bb400 20 20 20 20 20 2f 2a 20 31 32 32 20 2a 2f 20 22 /* 122 */ "
bb410 44 65 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a Delete",. /*
bb420 20 31 32 33 20 2a 2f 20 22 42 6c 6f 62 22 2c 0a 123 */ "Blob",.
bb430 20 20 20 20 20 2f 2a 20 31 32 34 20 2a 2f 20 22 /* 124 */ "
bb440 4d 6f 76 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 Move",. /* 1
bb450 32 35 20 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20 20 25 */ "Goto",.
bb460 20 20 20 2f 2a 20 31 32 36 20 2a 2f 20 22 50 61 /* 126 */ "Pa
bb470 72 73 65 53 63 68 65 6d 61 22 2c 0a 20 20 20 20 rseSchema",.
bb480 20 2f 2a 20 31 32 37 20 2a 2f 20 22 56 4e 65 78 /* 127 */ "VNex
bb490 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 38 20 t",. /* 128
bb4a0 2a 2f 20 22 53 65 65 6b 22 2c 0a 20 20 20 20 20 */ "Seek",.
bb4b0 2f 2a 20 31 32 39 20 2a 2f 20 22 54 61 62 6c 65 /* 129 */ "Table
bb4c0 4c 6f 63 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 31 Lock",. /* 1
bb4d0 33 30 20 2a 2f 20 22 52 65 61 6c 22 2c 0a 20 20 30 */ "Real",.
bb4e0 20 20 20 2f 2a 20 31 33 31 20 2a 2f 20 22 56 65 /* 131 */ "Ve
bb4f0 72 69 66 79 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 rifyCookie",.
bb500 20 20 2f 2a 20 31 33 32 20 2a 2f 20 22 43 6f 6c /* 132 */ "Col
bb510 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 umn",. /* 13
bb520 33 20 2a 2f 20 22 4f 70 65 6e 52 65 61 64 22 2c 3 */ "OpenRead",
bb530 0a 20 20 20 20 20 2f 2a 20 31 33 34 20 2a 2f 20 . /* 134 */
bb540 22 52 65 73 65 74 43 6f 75 6e 74 22 2c 0a 20 20 "ResetCount",.
bb550 20 20 20 2f 2a 20 31 33 35 20 2a 2f 20 22 4e 6f /* 135 */ "No
bb560 74 55 73 65 64 5f 31 33 35 22 2c 0a 20 20 20 20 tUsed_135",.
bb570 20 2f 2a 20 31 33 36 20 2a 2f 20 22 4e 6f 74 55 /* 136 */ "NotU
bb580 73 65 64 5f 31 33 36 22 2c 0a 20 20 20 20 20 2f sed_136",. /
bb590 2a 20 31 33 37 20 2a 2f 20 22 4e 6f 74 55 73 65 * 137 */ "NotUse
bb5a0 64 5f 31 33 37 22 2c 0a 20 20 20 20 20 2f 2a 20 d_137",. /*
bb5b0 31 33 38 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 138 */ "NotUsed_
bb5c0 31 33 38 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 138",. /* 13
bb5d0 39 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 9 */ "NotUsed_13
bb5e0 39 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 30 20 9",. /* 140
bb5f0 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 34 30 22 */ "NotUsed_140"
bb600 2c 0a 20 20 20 20 20 2f 2a 20 31 34 31 20 2a 2f ,. /* 141 */
bb610 20 22 54 6f 54 65 78 74 22 2c 0a 20 20 20 20 20 "ToText",.
bb620 2f 2a 20 31 34 32 20 2a 2f 20 22 54 6f 42 6c 6f /* 142 */ "ToBlo
bb630 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 33 20 b",. /* 143
bb640 2a 2f 20 22 54 6f 4e 75 6d 65 72 69 63 22 2c 0a */ "ToNumeric",.
bb650 20 20 20 20 20 2f 2a 20 31 34 34 20 2a 2f 20 22 /* 144 */ "
bb660 54 6f 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 ToInt",. /*
bb670 31 34 35 20 2a 2f 20 22 54 6f 52 65 61 6c 22 2c 145 */ "ToReal",
bb680 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20 61 . };. return a
bb690 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d 0a 23 65 6e 64 zName[i];.}.#end
bb6a0 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a if../***********
bb6b0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 *** End of opcod
bb6c0 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a es.c ***********
bb6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb6f0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
bb700 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f *** Begin file o
bb710 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a s_os2.c ********
bb720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb740 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 46 **/./*.** 2006 F
bb750 65 62 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 eb 14.**.** The
bb760 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
bb770 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
bb780 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
bb790 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
bb7a0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
bb7b0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
bb7c0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
bb7d0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
bb7e0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
bb7f0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
bb800 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
bb810 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
bb820 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
bb830 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
bb840 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
bb850 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
bb860 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
bb870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
bb8c0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
bb8d0 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 ns code that is
bb8e0 73 70 65 63 69 66 69 63 20 74 6f 20 4f 53 2f 32 specific to OS/2
bb8f0 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f ..**.** $Id: os_
bb900 6f 73 32 2e 63 2c 76 20 31 2e 36 33 20 32 30 30 os2.c,v 1.63 200
bb910 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32 34 8/12/10 19:26:24
bb920 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a drh Exp $.*/...
bb930 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 #if SQLITE_OS_OS
bb940 32 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 2../*.** A Note
bb950 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c 6c About Memory All
bb960 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 ocation:.**.** T
bb970 68 69 73 20 64 72 69 76 65 72 20 75 73 65 73 20 his driver uses
bb980 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 20 malloc()/free()
bb990 64 69 72 65 63 74 6c 79 20 72 61 74 68 65 72 20 directly rather
bb9a0 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75 than going throu
bb9b0 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 gh.** the SQLite
bb9c0 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69 74 65 -wrappers sqlite
bb9d0 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 3_malloc()/sqlit
bb9e0 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68 6f 73 e3_free(). Thos
bb9f0 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20 61 72 e wrappers.** ar
bba00 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75 e designed for u
bba10 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73 se on embedded s
bba20 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d 65 6d ystems where mem
bba30 6f 72 79 20 69 73 20 73 63 61 72 63 65 20 61 6e ory is scarce an
bba40 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c d.** malloc fail
bba50 75 72 65 73 20 68 61 70 70 65 6e 20 66 72 65 71 ures happen freq
bba60 75 65 6e 74 6c 79 2e 20 20 4f 53 2f 32 20 64 6f uently. OS/2 do
bba70 65 73 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 es not typically
bba80 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 run on.** embed
bba90 64 65 64 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 ded systems, and
bbaa0 20 77 68 65 6e 20 69 74 20 64 6f 65 73 20 74 68 when it does th
bbab0 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 e developers nor
bbac0 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 67 67 65 mally have bigge
bbad0 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f r.** problems to
bbae0 20 77 6f 72 72 79 20 61 62 6f 75 74 20 74 68 61 worry about tha
bbaf0 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 n running out of
bbb00 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 memory. So the
bbb10 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 re is not.** a c
bbb20 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 ompelling need t
bbb30 6f 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65 o use the wrappe
bbb40 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 rs..**.** But th
bbb50 65 72 65 20 69 73 20 61 20 67 6f 6f 64 20 72 65 ere is a good re
bbb60 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 ason to not use
bbb70 74 68 65 20 77 72 61 70 70 65 72 73 2e 20 20 49 the wrappers. I
bbb80 66 20 77 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 f we use the.**
bbb90 77 72 61 70 70 65 72 73 20 74 68 65 6e 20 77 65 wrappers then we
bbba0 20 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 will get simula
bbbb0 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 ted malloc() fai
bbbc0 6c 75 72 65 73 20 77 69 74 68 69 6e 20 74 68 69 lures within thi
bbbd0 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 41 6e s.** driver. An
bbbe0 64 20 74 68 61 74 20 63 61 75 73 65 73 20 61 6c d that causes al
bbbf0 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c l kinds of probl
bbc00 65 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 73 74 ems for our test
bbc10 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 s. We.** could
bbc20 65 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 20 74 enhance SQLite t
bbc30 6f 20 64 65 61 6c 20 77 69 74 68 20 73 69 6d 75 o deal with simu
bbc40 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 lated malloc fai
bbc50 6c 75 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 lures within.**
bbc60 74 68 65 20 4f 53 20 64 72 69 76 65 72 2c 20 62 the OS driver, b
bbc70 75 74 20 74 68 65 20 63 6f 64 65 20 74 6f 20 64 ut the code to d
bbc80 65 61 6c 20 77 69 74 68 20 74 68 6f 73 65 20 66 eal with those f
bbc90 61 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 ailure would not
bbca0 0a 2a 2a 20 62 65 20 65 78 65 72 63 69 73 65 64 .** be exercised
bbcb0 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 63 68 on Linux (which
bbcc0 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
bbcd0 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 o malloc() in th
bbce0 65 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 e driver).** and
bbcf0 20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 61 76 so we would hav
bbd00 65 20 64 69 66 66 69 63 75 6c 74 79 20 77 72 69 e difficulty wri
bbd10 74 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65 ting coverage te
bbd20 73 74 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 sts for that.**
bbd30 63 6f 64 65 2e 20 20 42 65 74 74 65 72 20 74 6f code. Better to
bbd40 20 6c 65 61 76 65 20 74 68 65 20 63 6f 64 65 20 leave the code
bbd50 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a out, we think..*
bbd60 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f *.** The point o
bbd70 66 20 74 68 69 73 20 64 69 73 63 75 73 73 69 6f f this discussio
bbd80 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a n is as follows:
bbd90 20 20 57 68 65 6e 20 63 72 65 61 74 69 6e 67 20 When creating
bbda0 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 a new.** OS laye
bbdb0 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 r for an embedde
bbdc0 64 20 73 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 d system, if you
bbdd0 20 75 73 65 20 74 68 69 73 20 66 69 6c 65 20 61 use this file a
bbde0 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a s an example,.**
bbdf0 20 61 76 6f 69 64 20 74 68 65 20 75 73 65 20 6f avoid the use o
bbe00 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 f malloc()/free(
bbe10 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 69 6e ). Those routin
bbe20 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 4f 53 es work ok on OS
bbe30 2f 32 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 /2.** desktops b
bbe40 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 ut not so well i
bbe50 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 n embedded syste
bbe60 6d 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 ms..*/../*.** Ma
bbe70 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64 65 74 cros used to det
bbe80 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f ermine whether o
bbe90 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 68 72 r not to use thr
bbea0 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 eads..*/.#if def
bbeb0 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 52 45 ined(SQLITE_THRE
bbec0 41 44 53 41 46 45 29 20 26 26 20 53 51 4c 49 54 ADSAFE) && SQLIT
bbed0 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20 64 E_THREADSAFE.# d
bbee0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 32 efine SQLITE_OS2
bbef0 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64 69 _THREADS 1.#endi
bbf00 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 f../*.** Include
bbf10 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f code that is co
bbf20 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a mmon to all os_*
bbf30 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a .c files.*/./***
bbf40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
bbf50 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 ude os_common.h
bbf60 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
bbf70 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a os_os2.c ******
bbf80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
bbf90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
bbfa0 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e n file os_common
bbfb0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
bbfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
bbfe0 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a * 2004 May 22.**
bbff0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
bc000 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
bc010 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
bc020 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
bc030 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
bc040 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
bc050 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
bc060 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
bc070 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
bc080 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
bc090 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
bc0a0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
bc0b0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
bc0c0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
bc0d0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
bc0e0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
bc0f0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
bc100 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
bc110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc150 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
bc160 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f e contains macro
bc170 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 62 s and a little b
bc180 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20 it of code that
bc190 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 is common to.**
bc1a0 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 66 all of the platf
bc1b0 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69 6c orm-specific fil
bc1c0 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 es (os_*.c) and
bc1d0 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e 74 is #included int
bc1e0 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 o those.** files
bc1f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ..**.** This fil
bc200 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e 63 e should be #inc
bc210 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f 73 5f luded by the os_
bc220 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 *.c files only.
bc230 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 It is not a.**
bc240 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 general purpose
bc250 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a header file..**.
bc260 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d 6f ** $Id: os_commo
bc270 6e 2e 68 2c 76 20 31 2e 33 38 20 32 30 30 39 2f n.h,v 1.38 2009/
bc280 30 32 2f 32 34 20 31 38 3a 34 30 3a 35 30 20 64 02/24 18:40:50 d
bc290 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 anielk1977 Exp $
bc2a0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f .*/.#ifndef _OS_
bc2b0 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e COMMON_H_.#defin
bc2c0 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a e _OS_COMMON_H_.
bc2d0 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 ./*.** At least
bc2e0 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 73 6c two bugs have sl
bc2f0 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 73 65 ipped in because
bc300 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 65 20 we changed the
bc310 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a 20 MEMORY_DEBUG.**
bc320 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 45 5f macro to SQLITE_
bc330 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 20 6f DEBUG and some o
bc340 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 20 68 lder makefiles h
bc350 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 ave not yet made
bc360 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e 20 the.** switch.
bc370 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 The following c
bc380 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 63 68 ode should catch
bc390 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 61 74 this problem at
bc3a0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a compile-time..*
bc3b0 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 59 5f /.#ifdef MEMORY_
bc3c0 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 22 54 DEBUG.# error "T
bc3d0 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 20 he MEMORY_DEBUG
bc3e0 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65 74 macro is obsolet
bc3f0 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 5f 44 e. Use SQLITE_D
bc400 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 0a 23 EBUG instead.".#
bc410 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 endif..#ifdef SQ
bc420 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 LITE_DEBUG.SQLIT
bc430 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
bc440 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d 20 30 lite3OSTrace = 0
bc450 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ;.#define OSTRAC
bc460 45 31 28 58 29 20 20 20 20 20 20 20 20 20 69 66 E1(X) if
bc470 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 ( sqlite3OSTrace
bc480 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ) sqlite3DebugP
bc490 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 6e 65 rintf(X).#define
bc4a0 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 20 20 OSTRACE2(X,Y)
bc4b0 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
bc4c0 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 OSTrace ) sqlite
bc4d0 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 3DebugPrintf(X,Y
bc4e0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
bc4f0 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 69 66 E3(X,Y,Z) if
bc500 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 ( sqlite3OSTrace
bc510 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ) sqlite3DebugP
bc520 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 rintf(X,Y,Z).#de
bc530 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c fine OSTRACE4(X,
bc540 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 71 6c Y,Z,A) if( sql
bc550 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 ite3OSTrace ) sq
bc560 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
bc570 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e (X,Y,Z,A).#defin
bc580 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a e OSTRACE5(X,Y,Z
bc590 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 74 65 ,A,B) if( sqlite
bc5a0 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 3OSTrace ) sqlit
bc5b0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
bc5c0 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 Y,Z,A,B).#define
bc5d0 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c OSTRACE6(X,Y,Z,
bc5e0 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 66 28 A,B,C) \. if(
bc5f0 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 sqlite3OSTrace)
bc600 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
bc610 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a tf(X,Y,Z,A,B,C).
bc620 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 #define OSTRACE7
bc630 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20 (X,Y,Z,A,B,C,D)
bc640 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 \. if(sqlite3
bc650 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 OSTrace) sqlite3
bc660 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
bc670 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65 Z,A,B,C,D).#else
bc680 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
bc690 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 1(X).#define OST
bc6a0 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 66 69 RACE2(X,Y).#defi
bc6b0 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c ne OSTRACE3(X,Y,
bc6c0 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 Z).#define OSTRA
bc6d0 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 CE4(X,Y,Z,A).#de
bc6e0 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c fine OSTRACE5(X,
bc6f0 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 Y,Z,A,B).#define
bc700 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c OSTRACE6(X,Y,Z,
bc710 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f A,B,C).#define O
bc720 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c STRACE7(X,Y,Z,A,
bc730 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f B,C,D).#endif../
bc740 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 *.** Macros for
bc750 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 61 63 performance trac
bc760 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 ing. Normally t
bc770 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 urned off. Only
bc780 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 works.** on i48
bc790 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 6 hardware..*/.#
bc7a0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 45 52 ifdef SQLITE_PER
bc7b0 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a FORMANCE_TRACE..
bc7c0 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20 /* .** hwtime.h
bc7d0 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 contains inline
bc7e0 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66 assembler code f
bc7f0 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 or implementing
bc800 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d .** high-perform
bc810 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74 ance timing rout
bc820 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a ines..*/./******
bc830 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 ******** Include
bc840 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65 hwtime.h in the
bc850 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f middle of os_co
bc860 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mmon.h *********
bc870 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
bc880 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
bc890 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a ile hwtime.h ***
bc8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc8c0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
bc8d0 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 008 May 27.**.**
bc8e0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
bc8f0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
bc900 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
bc910 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
bc920 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
bc930 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
bc940 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
bc950 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
bc960 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
bc970 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
bc980 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
bc990 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
bc9a0 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
bc9b0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
bc9c0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
bc9d0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
bc9e0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
bc9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bca00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bca10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bca20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bca30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
bca40 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
bca50 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 ontains inline a
bca60 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72 sm code for retr
bca70 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72 ieving "high-per
bca80 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 formance".** cou
bca90 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c nters for x86 cl
bcaa0 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 ass CPUs..**.**
bcab0 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20 $Id: hwtime.h,v
bcac0 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31 20 31 1.3 2008/08/01 1
bcad0 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20 45 78 4:33:15 shane Ex
bcae0 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f p $.*/.#ifndef _
bcaf0 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e HWTIME_H_.#defin
bcb00 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a e _HWTIME_H_../*
bcb10 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
bcb20 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 g routine only w
bcb30 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d orks on pentium-
bcb40 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29 class (or newer)
bcb50 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 processors..**
bcb60 49 74 20 75 73 65 73 20 74 68 65 20 52 44 54 53 It uses the RDTS
bcb70 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64 C opcode to read
bcb80 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 the cycle count
bcb90 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 value out of th
bcba0 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 e.** processor a
bcbb0 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 nd returns that
bcbc0 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e value. This can
bcbd0 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 69 67 be used for hig
bcbe0 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 h-res.** profili
bcbf0 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 ng..*/.#if (defi
bcc00 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c ned(__GNUC__) ||
bcc10 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 defined(_MSC_VE
bcc20 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28 R)) && \. (
bcc30 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c defined(i386) ||
bcc40 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f defined(__i386_
bcc50 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d _) || defined(_M
bcc60 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64 _IX86)).. #if d
bcc70 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 efined(__GNUC__)
bcc80 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 .. __inline__ s
bcc90 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
bcca0 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
bccb0 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 {. unsigned
bccc0 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 int lo, hi;.
bccd0 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 __asm__ __volat
bcce0 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a ile__ ("rdtsc" :
bccf0 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 "=a" (lo), "=d"
bcd00 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74 (hi));. ret
bcd10 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 urn (sqlite_uint
bcd20 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 64)hi << 32 | lo
bcd30 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 ;. }.. #elif d
bcd40 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 efined(_MSC_VER)
bcd50 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e .. __declspec(n
bcd60 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 aked) __inline s
bcd70 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 qlite_uint64 __c
bcd80 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69 decl sqlite3Hwti
bcd90 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f me(void){. _
bcda0 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 _asm {. r
bcdb0 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 65 74 dtsc. ret
bcdc0 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20 ; return
bcdd0 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58 value at EDX:EAX
bcde0 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 . }. }.. #
bcdf0 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 endif..#elif (de
bce00 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 fined(__GNUC__)
bce10 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 && defined(__x86
bce20 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c _64__)).. __inl
bce30 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e ine__ sqlite_uin
bce40 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d t64 sqlite3Hwtim
bce50 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 e(void){. u
bce60 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c nsigned long val
bce70 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 ;. __asm__
bce80 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 __volatile__ ("r
bce90 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61 dtsc" : "=A" (va
bcea0 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 l));. retur
bceb0 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c n val;. }. .#el
bcec0 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e if (defined(__GN
bced0 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 UC__) && defined
bcee0 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f (__ppc__)).. __
bcef0 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f inline__ sqlite_
bcf00 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 uint64 sqlite3Hw
bcf10 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 time(void){.
bcf20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 unsigned long
bcf30 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 long retval;.
bcf40 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 unsigned long
bcf50 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 junk;. __a
bcf60 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f sm__ __volatile_
bcf70 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 _ ("\n\.
bcf80 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20 1: mftbu
bcf90 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 %1\n\.
bcfa0 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 20 20 mftb
bcfb0 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 %L0\n\.
bcfc0 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 75 mftbu
bcfd0 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 %0\n\.
bcfe0 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 77 20 cmpw
bcff0 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 %0,%1\n\.
bd000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6e bn
bd010 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 20 20 e 1b".
bd020 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d : "=
bd030 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 r" (retval), "=r
bd040 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 " (junk));.
bd050 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a return retval;.
bd060 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 }..#else.. #e
bd070 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d rror Need implem
bd080 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 entation of sqli
bd090 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 te3Hwtime() for
bd0a0 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a your platform...
bd0b0 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d /*. ** To com
bd0c0 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70 pile without imp
bd0d0 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 lementing sqlite
bd0e0 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 3Hwtime() for yo
bd0f0 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a ur platform,. *
bd100 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 * you can remove
bd110 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f the above #erro
bd120 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f r and use the fo
bd130 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 llowing. ** stu
bd140 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 b function. You
bd150 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e will lose timin
bd160 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 g support for ma
bd170 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 ny. ** of the d
bd180 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73 ebugging and tes
bd190 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20 ting utilities,
bd1a0 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74 but it should at
bd1b0 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 . ** least comp
bd1c0 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a ile and run.. *
bd1d0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
bd1e0 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 sqlite_uint64
bd1f0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 sqlite3Hwtime(v
bd200 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73 oid){ return ((s
bd210 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b qlite_uint64)0);
bd220 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 }..#endif..#end
bd230 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f if /* !defined(_
bd240 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f HWTIME_H_) */../
bd250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
bd260 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a nd of hwtime.h *
bd270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
bd2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 ************** C
bd2b0 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 ontinuing where
bd2c0 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f we left off in o
bd2d0 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a s_common.h *****
bd2e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
bd2f0 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 static sqlite_ui
bd300 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a 73 74 nt64 g_start;.st
bd310 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 atic sqlite_uint
bd320 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64 64 g_elapsed;.#d
bd330 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 efine TIMER_STAR
bd340 54 20 20 20 20 20 20 20 67 5f 73 74 61 72 74 3d T g_start=
bd350 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 0a sqlite3Hwtime().
bd360 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e #define TIMER_EN
bd370 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c 61 70 D g_elap
bd380 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74 69 6d sed=sqlite3Hwtim
bd390 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 e()-g_start.#def
bd3a0 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 ine TIMER_ELAPSE
bd3b0 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 0a D g_elapsed.
bd3c0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 49 #else.#define TI
bd3d0 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66 69 6e MER_START.#defin
bd3e0 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 e TIMER_END.#def
bd3f0 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 ine TIMER_ELAPSE
bd400 44 20 20 20 20 20 28 28 73 71 6c 69 74 65 5f 75 D ((sqlite_u
bd410 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69 66 0a int64)0).#endif.
bd420 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d ./*.** If we com
bd430 70 69 6c 65 20 77 69 74 68 20 74 68 65 20 53 51 pile with the SQ
bd440 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 LITE_TEST macro
bd450 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 66 6f set, then the fo
bd460 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a llowing block.**
bd470 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 of code will gi
bd480 76 65 20 75 73 20 74 68 65 20 61 62 69 6c 69 74 ve us the abilit
bd490 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 y to simulate a
bd4a0 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 disk I/O error.
bd4b0 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 This.** is used
bd4c0 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 68 65 for testing the
bd4d0 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f I/O recovery lo
bd4e0 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 gic..*/.#ifdef S
bd4f0 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
bd500 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
bd510 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 3_io_error_hit =
bd520 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 0; /
bd530 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f * Total number o
bd540 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a f I/O Errors */.
bd550 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
bd560 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
bd570 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20 hardhit = 0;
bd580 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
bd590 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f non-benign erro
bd5a0 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 rs */.SQLITE_API
bd5b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f int sqlite3_io_
bd5c0 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 error_pending =
bd5d0 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 0; /* Cou
bd5e0 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 nt down to first
bd5f0 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 I/O error */.SQ
bd600 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
bd610 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 ite3_io_error_pe
bd620 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20 rsist = 0;
bd630 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f /* True if I/O
bd640 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 74 20 errors persist
bd650 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
bd660 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 t sqlite3_io_err
bd670 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 or_benign = 0;
bd680 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
bd690 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 65 6e f errors are ben
bd6a0 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ign */.SQLITE_AP
bd6b0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 I int sqlite3_di
bd6c0 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d skfull_pending =
bd6d0 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 0;.SQLITE_API i
bd6e0 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 nt sqlite3_diskf
bd6f0 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 ull = 0;.#define
bd700 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
bd710 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 65 Benign(X) sqlite
bd720 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 3_io_error_benig
bd730 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 n=(X).#define Si
bd740 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 4f mulateIOError(CO
bd750 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28 73 71 DE) \. if( (sq
bd760 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
bd770 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 74 65 ersist && sqlite
bd780 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 3_io_error_hit)
bd790 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 \. || sqli
bd7a0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e te3_io_error_pen
bd7b0 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c ding-- == 1 ) \
bd7c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7b . {
bd7d0 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 local_ioerr();
bd7e0 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 CODE; }.static v
bd7f0 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 oid local_ioerr(
bd800 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 49 ){. IOTRACE(("I
bd810 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c OERR\n"));. sql
bd820 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 ite3_io_error_hi
bd830 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 t++;. if( !sqli
bd840 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e te3_io_error_ben
bd850 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 6f ign ) sqlite3_io
bd860 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b _error_hardhit++
bd870 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 ;.}.#define Simu
bd880 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f lateDiskfullErro
bd890 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 r(CODE) \. if(
bd8a0 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
bd8b0 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 l_pending ){ \.
bd8c0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f if( sqlite3_
bd8d0 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 diskfull_pending
bd8e0 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 == 1 ){ \.
bd8f0 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b local_ioerr();
bd900 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 \. sqlite
bd910 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 3_diskfull = 1;
bd920 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 \. sqlite3
bd930 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 _io_error_hit =
bd940 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 1; \. CODE
bd950 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 ; \. }else{
bd960 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 \. sqlite3
bd970 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e _diskfull_pendin
bd980 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a g--; \. } \.
bd990 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 }.#else.#defi
bd9a0 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 ne SimulateIOErr
bd9b0 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 orBenign(X).#def
bd9c0 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ine SimulateIOEr
bd9d0 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 20 53 ror(A).#define S
bd9e0 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 imulateDiskfullE
bd9f0 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a rror(A).#endif..
bda00 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 /*.** When testi
bda10 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 ng, keep a count
bda20 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f of the number o
bda30 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f f open files..*/
bda40 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
bda50 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 EST.SQLITE_API i
bda60 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f nt sqlite3_open_
bda70 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a file_count = 0;.
bda80 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e #define OpenCoun
bda90 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f ter(X) sqlite3_
bdaa0 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b open_file_count+
bdab0 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 =(X).#else.#defi
bdac0 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 ne OpenCounter(X
bdad0 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 ).#endif..#endif
bdae0 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f 53 /* !defined(_OS
bdaf0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a _COMMON_H_) */..
bdb00 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
bdb10 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e End of os_common
bdb20 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
bdb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
bdb50 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
bdb60 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
bdb70 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
bdb80 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a os_os2.c *******
bdb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
bdba0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 73 32 46 69 ./*.** The os2Fi
bdbb0 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 le structure is
bdbc0 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 subclass of sqli
bdbd0 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69 66 69 te3_file specifi
bdbe0 63 20 66 6f 72 20 74 68 65 20 4f 53 2f 32 0a 2a c for the OS/2.*
bdbf0 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20 6c 61 * protability la
bdc00 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 yer..*/.typedef
bdc10 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65 20 6f struct os2File o
bdc20 73 32 46 69 6c 65 3b 0a 73 74 72 75 63 74 20 6f s2File;.struct o
bdc30 73 32 46 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 s2File {. const
bdc40 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
bdc50 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 2f ods *pMethod; /
bdc60 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66 69 72 * Always the fir
bdc70 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 48 46 st entry */. HF
bdc80 49 4c 45 20 68 3b 20 20 20 20 20 20 20 20 20 20 ILE h;
bdc90 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c /* Handl
bdca0 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 e for accessing
bdcb0 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 the file */. ch
bdcc0 61 72 2a 20 70 61 74 68 54 6f 44 65 6c 3b 20 20 ar* pathToDel;
bdcd0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
bdce0 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 of file to delet
bdcf0 65 20 6f 6e 20 63 6c 6f 73 65 2c 20 4e 55 4c 4c e on close, NULL
bdd00 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 75 6e 73 if not */. uns
bdd10 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 igned char lockt
bdd20 79 70 65 3b 20 20 20 2f 2a 20 54 79 70 65 20 6f ype; /* Type o
bdd30 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 f lock currently
bdd40 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 held on this fi
bdd50 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e le */.};..#defin
bdd60 65 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 31 e LOCK_TIMEOUT 1
bdd70 30 4c 20 2f 2a 20 74 68 65 20 64 65 66 61 75 6c 0L /* the defaul
bdd80 74 20 6c 6f 63 6b 69 6e 67 20 74 69 6d 65 6f 75 t locking timeou
bdd90 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a t */../*********
bdda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bddb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bddc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bddd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdde0 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 ****.** The next
bddf0 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e group of routin
bde00 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 es implement the
bde10 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73 70 65 I/O methods spe
bde20 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 cified.** by the
bde30 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
bde40 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a 2a ods object..****
bde50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bde60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bde70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bde80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bde90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a **********/../*.
bdea0 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e ** Close a file.
bdeb0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
bdec0 73 32 43 6c 6f 73 65 28 20 73 71 6c 69 74 65 33 s2Close( sqlite3
bded0 5f 66 69 6c 65 20 2a 69 64 20 29 7b 0a 20 20 41 _file *id ){. A
bdee0 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 PIRET rc = NO_ER
bdef0 52 4f 52 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a ROR;. os2File *
bdf00 70 46 69 6c 65 3b 0a 20 20 69 66 28 20 69 64 20 pFile;. if( id
bdf10 26 26 20 28 70 46 69 6c 65 20 3d 20 28 6f 73 32 && (pFile = (os2
bdf20 46 69 6c 65 2a 29 69 64 29 20 21 3d 20 30 20 29 File*)id) != 0 )
bdf30 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 20 {. OSTRACE2(
bdf40 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46 "CLOSE %d\n", pF
bdf50 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 72 63 ile->h );. rc
bdf60 20 3d 20 44 6f 73 43 6c 6f 73 65 28 20 70 46 69 = DosClose( pFi
bdf70 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 70 46 69 le->h );. pFi
bdf80 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e le->locktype = N
bdf90 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 69 66 28 20 O_LOCK;. if(
bdfa0 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c pFile->pathToDel
bdfb0 20 21 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 != NULL ){.
bdfc0 20 20 72 63 20 3d 20 44 6f 73 46 6f 72 63 65 44 rc = DosForceD
bdfd0 65 6c 65 74 65 28 20 28 50 53 5a 29 70 46 69 6c elete( (PSZ)pFil
bdfe0 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a e->pathToDel );.
bdff0 20 20 20 20 20 20 66 72 65 65 28 20 70 46 69 6c free( pFil
be000 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a e->pathToDel );.
be010 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 pFile->pat
be020 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 hToDel = NULL;.
be030 20 20 20 7d 0a 20 20 20 20 69 64 20 3d 20 30 3b }. id = 0;
be040 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 . OpenCounter
be050 28 20 2d 31 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 ( -1 );. }.. r
be060 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 eturn rc == NO_E
be070 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b RROR ? SQLITE_OK
be080 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b : SQLITE_IOERR;
be090 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 .}../*.** Read d
be0a0 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 ata from a file
be0b0 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20 into a buffer.
be0c0 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
be0d0 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 if all.** bytes
be0e0 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63 65 were read succe
be0f0 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 ssfully and SQLI
be100 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74 TE_IOERR if anyt
be110 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f hing goes.** wro
be120 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
be130 74 20 6f 73 32 52 65 61 64 28 0a 20 20 73 71 6c t os2Read(. sql
be140 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 ite3_file *id,
be150 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
be160 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72 6f File to read fro
be170 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 m */. void *pBu
be180 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f,
be190 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
be1a0 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 69 content into thi
be1b0 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e s buffer */. in
be1c0 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 t amt,
be1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
be1e0 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
be1f0 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 to read */. sq
be200 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 lite3_int64 offs
be210 65 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a et /*
be220 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 Begin reading a
be230 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f t this offset */
be240 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 .){. ULONG file
be250 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 Location = 0L;.
be260 20 55 4c 4f 4e 47 20 67 6f 74 3b 0a 20 20 6f 73 ULONG got;. os
be270 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 2File *pFile = (
be280 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 os2File*)id;. a
be290 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a ssert( id!=0 );.
be2a0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
be2b0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
be2c0 5f 49 4f 45 52 52 5f 52 45 41 44 20 29 3b 0a 20 _IOERR_READ );.
be2d0 20 4f 53 54 52 41 43 45 33 28 20 22 52 45 41 44 OSTRACE3( "READ
be2e0 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 %d lock=%d\n",
be2f0 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d pFile->h, pFile-
be300 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 69 >locktype );. i
be310 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50 74 72 f( DosSetFilePtr
be320 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 65 (pFile->h, offse
be330 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26 t, FILE_BEGIN, &
be340 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21 3d fileLocation) !=
be350 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
be360 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
be370 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20 OERR;. }. if(
be380 44 6f 73 52 65 61 64 28 20 70 46 69 6c 65 2d 3e DosRead( pFile->
be390 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 h, pBuf, amt, &g
be3a0 6f 74 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 ot ) != NO_ERROR
be3b0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
be3c0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 QLITE_IOERR_READ
be3d0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f 74 20 ;. }. if( got
be3e0 3d 3d 20 28 55 4c 4f 4e 47 29 61 6d 74 20 29 0a == (ULONG)amt ).
be3f0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
be400 45 5f 4f 4b 3b 0a 20 20 65 6c 73 65 20 7b 0a 20 E_OK;. else {.
be410 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70 6f 72 /* Unread por
be420 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 69 6e 70 tions of the inp
be430 75 74 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 ut buffer must b
be440 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f e zero-filled */
be450 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 . memset(&((c
be460 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c har*)pBuf)[got],
be470 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 0, amt-got);.
be480 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
be490 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 IOERR_SHORT_READ
be4a0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 ;. }.}../*.** W
be4b0 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d 20 61 rite data from a
be4c0 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 20 66 buffer into a f
be4d0 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c ile. Return SQL
be4e0 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 ITE_OK on succes
be4f0 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 s.** or some oth
be500 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e er error code on
be510 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 failure..*/.sta
be520 74 69 63 20 69 6e 74 20 6f 73 32 57 72 69 74 65 tic int os2Write
be530 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 (. sqlite3_file
be540 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 *id,
be550 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 77 /* File to w
be560 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 rite into */. c
be570 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c onst void *pBuf,
be580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
be590 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 62 * The bytes to b
be5a0 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 e written */. i
be5b0 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 nt amt,
be5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
be5d0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
be5e0 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 s to write */.
be5f0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 sqlite3_int64 of
be600 66 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20 fset
be610 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 /* Offset into t
be620 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67 69 6e he file to begin
be630 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f 0a 29 writing at */.)
be640 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 4c 6f {. ULONG fileLo
be650 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 20 41 cation = 0L;. A
be660 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 PIRET rc = NO_ER
be670 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 77 72 6f ROR;. ULONG wro
be680 74 65 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 te;. os2File *p
be690 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a File = (os2File*
be6a0 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 69 )id;. assert( i
be6b0 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 d!=0 );. Simula
be6c0 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 teIOError( retur
be6d0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 n SQLITE_IOERR_W
be6e0 52 49 54 45 20 29 3b 0a 20 20 53 69 6d 75 6c 61 RITE );. Simula
be6f0 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 teDiskfullError(
be700 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 return SQLITE_F
be710 55 4c 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 ULL );. OSTRACE
be720 33 28 20 22 57 52 49 54 45 20 25 64 20 6c 6f 63 3( "WRITE %d loc
be730 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e k=%d\n", pFile->
be740 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 h, pFile->lockty
be750 70 65 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 53 pe );. if( DosS
be760 65 74 46 69 6c 65 50 74 72 28 70 46 69 6c 65 2d etFilePtr(pFile-
be770 3e 68 2c 20 6f 66 66 73 65 74 2c 20 46 49 4c 45 >h, offset, FILE
be780 5f 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c 6f 63 _BEGIN, &fileLoc
be790 61 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52 52 ation) != NO_ERR
be7a0 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e OR ){. return
be7b0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 SQLITE_IOERR;.
be7c0 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 6d 74 }. assert( amt
be7d0 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 61 >0 );. while( a
be7e0 6d 74 20 3e 20 30 20 26 26 0a 20 20 20 20 20 20 mt > 0 &&.
be7f0 20 20 20 28 20 72 63 20 3d 20 44 6f 73 57 72 69 ( rc = DosWri
be800 74 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20 28 50 te( pFile->h, (P
be810 56 4f 49 44 29 70 42 75 66 2c 20 61 6d 74 2c 20 VOID)pBuf, amt,
be820 26 77 72 6f 74 65 20 29 20 29 20 3d 3d 20 4e 4f &wrote ) ) == NO
be830 5f 45 52 52 4f 52 20 26 26 0a 20 20 20 20 20 20 _ERROR &&.
be840 20 20 20 77 72 6f 74 65 20 3e 20 30 0a 20 20 29 wrote > 0. )
be850 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f {. amt -= wro
be860 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26 te;. pBuf = &
be870 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72 ((char*)pBuf)[wr
be880 6f 74 65 5d 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 ote];. }.. ret
be890 75 72 6e 20 28 20 72 63 20 21 3d 20 4e 4f 5f 45 urn ( rc != NO_E
be8a0 52 52 4f 52 20 7c 7c 20 61 6d 74 20 3e 20 28 69 RROR || amt > (i
be8b0 6e 74 29 77 72 6f 74 65 20 29 20 3f 20 53 51 4c nt)wrote ) ? SQL
be8c0 49 54 45 5f 46 55 4c 4c 20 3a 20 53 51 4c 49 54 ITE_FULL : SQLIT
be8d0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK;.}../*.** T
be8e0 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 runcate an open
be8f0 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 file to a specif
be900 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 ied size.*/.stat
be910 69 63 20 69 6e 74 20 6f 73 32 54 72 75 6e 63 61 ic int os2Trunca
be920 74 65 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 te( sqlite3_file
be930 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65 20 *id, i64 nByte
be940 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d ){. APIRET rc =
be950 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73 32 NO_ERROR;. os2
be960 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f File *pFile = (o
be970 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f 53 s2File*)id;. OS
be980 54 52 41 43 45 33 28 20 22 54 52 55 4e 43 41 54 TRACE3( "TRUNCAT
be990 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46 E %d %lld\n", pF
be9a0 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b ile->h, nByte );
be9b0 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
be9c0 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 or( return SQLIT
be9d0 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 E_IOERR_TRUNCATE
be9e0 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 53 65 );. rc = DosSe
be9f0 74 46 69 6c 65 53 69 7a 65 28 20 70 46 69 6c 65 tFileSize( pFile
bea00 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20 ->h, nByte );.
bea10 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f return rc == NO_
bea20 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f ERROR ? SQLITE_O
bea30 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 K : SQLITE_IOERR
bea40 5f 54 52 55 4e 43 41 54 45 3b 0a 7d 0a 0a 23 69 _TRUNCATE;.}..#i
bea50 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
bea60 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 ./*.** Count the
bea70 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73 number of fulls
bea80 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 yncs and normal
bea90 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20 syncs. This is
beaa0 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 used to test.**
beab0 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66 that syncs and f
beac0 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63 ullsyncs are occ
bead0 75 72 69 6e 67 20 61 74 20 74 68 65 20 72 69 67 uring at the rig
beae0 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c ht times..*/.SQL
beaf0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
beb00 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d te3_sync_count =
beb10 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 0;.SQLITE_API i
beb20 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 nt sqlite3_fulls
beb30 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 ync_count = 0;.#
beb40 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b endif../*.** Mak
beb50 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 e sure all write
beb60 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 s to a particula
beb70 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 r file are commi
beb80 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f tted to disk..*/
beb90 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 53 .static int os2S
beba0 79 6e 63 28 20 73 71 6c 69 74 65 33 5f 66 69 6c ync( sqlite3_fil
bebb0 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 e *id, int flags
bebc0 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 ){. os2File *p
bebd0 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a File = (os2File*
bebe0 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 )id;. OSTRACE3(
bebf0 20 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d 25 "SYNC %d lock=%
bec00 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
bec10 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
bec20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 );.#ifdef SQLITE
bec30 5f 54 45 53 54 0a 20 20 69 66 28 20 66 6c 61 67 _TEST. if( flag
bec40 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f s & SQLITE_SYNC_
bec50 46 55 4c 4c 29 7b 0a 20 20 20 20 73 71 6c 69 74 FULL){. sqlit
bec60 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e e3_fullsync_coun
bec70 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 t++;. }. sqlit
bec80 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b e3_sync_count++;
bec90 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20 .#endif. /* If
beca0 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 we compiled with
becb0 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 the SQLITE_NO_S
becc0 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 YNC flag, then s
becd0 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a yncing is a. **
bece0 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 no-op. */.#ifd
becf0 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e ef SQLITE_NO_SYN
bed00 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d C. UNUSED_PARAM
bed10 45 54 45 52 28 70 46 69 6c 65 29 3b 0a 20 20 72 ETER(pFile);. r
bed20 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
bed30 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 .#else. return
bed40 44 6f 73 52 65 73 65 74 42 75 66 66 65 72 28 20 DosResetBuffer(
bed50 70 46 69 6c 65 2d 3e 68 20 29 20 3d 3d 20 4e 4f pFile->h ) == NO
bed60 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f _ERROR ? SQLITE_
bed70 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 OK : SQLITE_IOER
bed80 52 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a R;.#endif.}../*.
bed90 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 ** Determine the
beda0 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 current size of
bedb0 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 a file in bytes
bedc0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
bedd0 73 32 46 69 6c 65 53 69 7a 65 28 20 73 71 6c 69 s2FileSize( sqli
bede0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 te3_file *id, sq
bedf0 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 lite3_int64 *pSi
bee00 7a 65 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 ze ){. APIRET r
bee10 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 c = NO_ERROR;.
bee20 46 49 4c 45 53 54 41 54 55 53 33 20 66 73 74 73 FILESTATUS3 fsts
bee30 33 46 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d 65 6d 3FileInfo;. mem
bee40 73 65 74 28 26 66 73 74 73 33 46 69 6c 65 49 6e set(&fsts3FileIn
bee50 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 fo, 0, sizeof(fs
bee60 74 73 33 46 69 6c 65 49 6e 66 6f 29 29 3b 0a 20 ts3FileInfo));.
bee70 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 assert( id!=0 )
bee80 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
bee90 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 ror( return SQLI
beea0 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 29 TE_IOERR_FSTAT )
beeb0 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 ;. rc = DosQuer
beec0 79 46 69 6c 65 49 6e 66 6f 28 20 28 28 6f 73 32 yFileInfo( ((os2
beed0 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 46 49 File*)id)->h, FI
beee0 4c 5f 53 54 41 4e 44 41 52 44 2c 20 26 66 73 74 L_STANDARD, &fst
beef0 73 33 46 69 6c 65 49 6e 66 6f 2c 20 73 69 7a 65 s3FileInfo, size
bef00 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 29 20 of(FILESTATUS3)
bef10 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d 20 4e );. if( rc == N
bef20 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 2a O_ERROR ){. *
bef30 70 53 69 7a 65 20 3d 20 66 73 74 73 33 46 69 6c pSize = fsts3Fil
bef40 65 49 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a 20 20 eInfo.cbFile;.
bef50 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
bef60 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 OK;. }else{.
bef70 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
bef80 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a OERR_FSTAT;. }.
bef90 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 }../*.** Acquire
befa0 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a a reader lock..
befb0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 */.static int ge
befc0 74 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69 tReadLock( os2Fi
befd0 6c 65 20 2a 70 46 69 6c 65 20 29 7b 0a 20 20 46 le *pFile ){. F
befe0 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 ILELOCK LockAre
beff0 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 a,. U
bf000 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41 50 49 nlockArea;. API
bf010 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d 73 65 RET res;. memse
bf020 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 t(&LockArea, 0,
bf030 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 sizeof(LockArea)
bf040 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c );. memset(&Unl
bf050 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 ockArea, 0, size
bf060 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b of(UnlockArea));
bf070 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 . LockArea.lOff
bf080 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 set = SHARED_FIR
bf090 53 54 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c ST;. LockArea.l
bf0a0 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 Range = SHARED_S
bf0b0 49 5a 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 IZE;. UnlockAre
bf0c0 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a a.lOffset = 0L;.
bf0d0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 UnlockArea.lRa
bf0e0 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 72 65 73 20 nge = 0L;. res
bf0f0 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b = DosSetFileLock
bf100 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e s( pFile->h, &Un
bf110 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 lockArea, &LockA
bf120 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 rea, LOCK_TIMEOU
bf130 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54 52 41 T, 1L );. OSTRA
bf140 43 45 33 28 20 22 47 45 54 52 45 41 44 4c 4f 43 CE3( "GETREADLOC
bf150 4b 20 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 K %d res=%d\n",
bf160 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b pFile->h, res );
bf170 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d . return res;.}
bf180 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 ../*.** Undo a r
bf190 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 eadlock.*/.stati
bf1a0 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64 c int unlockRead
bf1b0 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a 69 Lock( os2File *i
bf1c0 64 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 d ){. FILELOCK
bf1d0 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 LockArea,.
bf1e0 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 UnlockAre
bf1f0 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65 73 3b a;. APIRET res;
bf200 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 . memset(&LockA
bf210 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c rea, 0, sizeof(L
bf220 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d ockArea));. mem
bf230 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c set(&UnlockArea,
bf240 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 0, sizeof(Unloc
bf250 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63 6b 41 kArea));. LockA
bf260 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c rea.lOffset = 0L
bf270 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 ;. LockArea.lRa
bf280 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c 6f nge = 0L;. Unlo
bf290 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
bf2a0 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 SHARED_FIRST;.
bf2b0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e UnlockArea.lRan
bf2c0 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 ge = SHARED_SIZE
bf2d0 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 ;. res = DosSet
bf2e0 46 69 6c 65 4c 6f 63 6b 73 28 20 69 64 2d 3e 68 FileLocks( id->h
bf2f0 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 , &UnlockArea, &
bf300 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 LockArea, LOCK_T
bf310 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20 IMEOUT, 1L );.
bf320 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 OSTRACE3( "UNLOC
bf330 4b 2d 52 45 41 44 4c 4f 43 4b 20 66 69 6c 65 20 K-READLOCK file
bf340 68 61 6e 64 6c 65 3d 25 64 20 72 65 73 3d 25 64 handle=%d res=%d
bf350 3f 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 72 65 73 ?\n", id->h, res
bf360 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 );. return res
bf370 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 ;.}../*.** Lock
bf380 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 the file with th
bf390 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 e lock specified
bf3a0 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f by parameter lo
bf3b0 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 cktype - one.**
bf3c0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
bf3d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 :.**.** (1)
bf3e0 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 SHARED_LOCK.**
bf3f0 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f (2) RESERVED_
bf400 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 LOCK.** (3)
bf410 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 PENDING_LOCK.**
bf420 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 (4) EXCLUSIV
bf430 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d E_LOCK.**.** Som
bf440 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 etimes when requ
bf450 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 esting one lock
bf460 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 state, additiona
bf470 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a l lock states.**
bf480 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e are inserted in
bf490 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c between. The l
bf4a0 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 ocking might fai
bf4b0 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 l on one of the
bf4c0 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 later.** transit
bf4d0 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 ions leaving the
bf4e0 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 lock state diff
bf4f0 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 erent from what
bf500 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a it started but.*
bf510 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 * still short of
bf520 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 its goal. The
bf530 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 following chart
bf540 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 shows the allowe
bf550 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 d.** transitions
bf560 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 and the inserte
bf570 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 d intermediate s
bf580 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 tates:.**.**
bf590 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 UNLOCKED -> SHAR
bf5a0 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 ED.** SHARED
bf5b0 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 -> RESERVED.**
bf5c0 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e SHARED -> (PEN
bf5d0 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 DING) -> EXCLUSI
bf5e0 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 VE.** RESERVE
bf5f0 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d D -> (PENDING) -
bf600 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 > EXCLUSIVE.**
bf610 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 PENDING -> EXC
bf620 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 LUSIVE.**.** Thi
bf630 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f s routine will o
bf640 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c nly increase a l
bf650 6f 63 6b 2e 20 20 54 68 65 20 6f 73 32 55 6e 6c ock. The os2Unl
bf660 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a ock() routine.**
bf670 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b erases all lock
bf680 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 s at once and re
bf690 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 turns us immedia
bf6a0 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 tely to locking
bf6b0 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 level 0..** It i
bf6c0 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 s not possible t
bf6d0 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b o lower the lock
bf6e0 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 ing level one st
bf6f0 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59 ep at a time. Y
bf700 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 ou.** must go st
bf710 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e raight to lockin
bf720 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 g level 0..*/.st
bf730 61 74 69 63 20 69 6e 74 20 6f 73 32 4c 6f 63 6b atic int os2Lock
bf740 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a ( sqlite3_file *
bf750 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
bf760 20 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 ){. int rc = S
bf770 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 QLITE_OK;
bf780 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 /* Return code f
bf790 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65 73 20 rom subroutines
bf7a0 2a 2f 0a 20 20 41 50 49 52 45 54 20 72 65 73 20 */. APIRET res
bf7b0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 20 20 20 2f = NO_ERROR; /
bf7c0 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 4f * Result of an O
bf7d0 53 2f 32 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a 2f S/2 lock call */
bf7e0 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b 74 79 . int newLockty
bf7f0 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 74 pe; /* Set
bf800 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
bf810 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 20 62 to this value b
bf820 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 2a 2f efore exiting */
bf830 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64 69 6e . int gotPendin
bf840 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 gLock = 0;/* Tru
bf850 65 20 69 66 20 77 65 20 61 63 71 75 69 72 65 64 e if we acquired
bf860 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 a PENDING lock
bf870 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20 20 46 this time */. F
bf880 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 ILELOCK LockAre
bf890 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 a,. U
bf8a0 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6f 73 32 nlockArea;. os2
bf8b0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f File *pFile = (o
bf8c0 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 6d 65 s2File*)id;. me
bf8d0 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 mset(&LockArea,
bf8e0 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 0, sizeof(LockAr
bf8f0 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 ea));. memset(&
bf900 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 UnlockArea, 0, s
bf910 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 izeof(UnlockArea
bf920 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 ));. assert( pF
bf930 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 ile!=0 );. OSTR
bf940 41 43 45 34 28 20 22 4c 4f 43 4b 20 25 64 20 25 ACE4( "LOCK %d %
bf950 64 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 d was %d\n", pFi
bf960 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c le->h, locktype,
bf970 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
bf980 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 );.. /* If the
bf990 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 re is already a
bf9a0 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 lock of this typ
bf9b0 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 e or more restri
bf9c0 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a ctive on the. *
bf9d0 2a 20 6f 73 32 46 69 6c 65 2c 20 64 6f 20 6e 6f * os2File, do no
bf9e0 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 thing. Don't use
bf9f0 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 the end_lock: e
bfa00 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a xit path, as. *
bfa10 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f * sqlite3_mutex_
bfa20 65 6e 74 65 72 28 29 20 68 61 73 6e 27 74 20 62 enter() hasn't b
bfa30 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a een called yet..
bfa40 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 */. if( pFile
bfa50 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b ->locktype>=lock
bfa60 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 type ){. OSTR
bfa70 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 25 ACE3( "LOCK %d %
bfa80 64 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 d ok (already he
bfa90 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 ld)\n", pFile->h
bfaa0 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 , locktype );.
bfab0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
bfac0 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 OK;. }.. /* Ma
bfad0 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b ke sure the lock
bfae0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 ing sequence is
bfaf0 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 correct. */. a
bfb00 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f ssert( pFile->lo
bfb10 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 cktype!=NO_LOCK
bfb20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 || locktype==SHA
bfb30 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 RED_LOCK );. as
bfb40 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d sert( locktype!=
bfb50 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a PENDING_LOCK );.
bfb60 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 assert( lockty
bfb70 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 pe!=RESERVED_LOC
bfb80 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b K || pFile->lock
bfb90 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
bfba0 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 K );.. /* Lock
bfbb0 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b the PENDING_LOCK
bfbc0 20 62 79 74 65 20 69 66 20 77 65 20 6e 65 65 64 byte if we need
bfbd0 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 50 45 to acquire a PE
bfbe0 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 NDING lock or.
bfbf0 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b ** a SHARED lock
bfc00 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 63 71 . If we are acq
bfc10 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 uiring a SHARED
bfc20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69 73 lock, the acquis
bfc30 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 ition of. ** th
bfc40 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 e PENDING_LOCK b
bfc50 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72 79 yte is temporary
bfc60 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b .. */. newLock
bfc70 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f type = pFile->lo
bfc80 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 70 46 cktype;. if( pF
bfc90 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e ile->locktype==N
bfca0 4f 5f 4c 4f 43 4b 0a 20 20 20 20 20 20 7c 7c 20 O_LOCK. ||
bfcb0 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 (locktype==EXCLU
bfcc0 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 SIVE_LOCK && pFi
bfcd0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 le->locktype==RE
bfce0 53 45 52 56 45 44 5f 4c 4f 43 4b 29 0a 20 20 29 SERVED_LOCK). )
bfcf0 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c {. LockArea.l
bfd00 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 Offset = PENDING
bfd10 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 _BYTE;. LockA
bfd20 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b rea.lRange = 1L;
bfd30 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e . UnlockArea.
bfd40 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 lOffset = 0L;.
bfd50 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 UnlockArea.lRa
bfd60 6e 67 65 20 3d 20 30 4c 3b 0a 0a 20 20 20 20 2f nge = 0L;.. /
bfd70 2a 20 77 61 69 74 20 6c 6f 6e 67 65 72 20 74 68 * wait longer th
bfd80 61 6e 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 an LOCK_TIMEOUT
bfd90 68 65 72 65 20 6e 6f 74 20 74 6f 20 68 61 76 65 here not to have
bfda0 20 74 6f 20 74 72 79 20 6d 75 6c 74 69 70 6c 65 to try multiple
bfdb0 20 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20 72 65 times */. re
bfdc0 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f s = DosSetFileLo
bfdd0 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 cks( pFile->h, &
bfde0 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 UnlockArea, &Loc
bfdf0 6b 41 72 65 61 2c 20 31 30 30 4c 2c 20 30 4c 20 kArea, 100L, 0L
bfe00 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d );. if( res =
bfe10 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
bfe20 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f gotPendingLo
bfe30 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 4f 53 ck = 1;. OS
bfe40 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 TRACE3( "LOCK %d
bfe50 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 6f pending lock bo
bfe60 6f 6c 65 61 6e 20 73 65 74 2e 20 20 72 65 73 3d olean set. res=
bfe70 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
bfe80 20 72 65 73 20 29 3b 0a 20 20 20 20 7d 0a 20 20 res );. }.
bfe90 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 }.. /* Acquire
bfea0 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20 20 a shared lock.
bfeb0 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 */. if( locktyp
bfec0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 e==SHARED_LOCK &
bfed0 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f & res == NO_ERRO
bfee0 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 R ){. assert(
bfef0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
bff00 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 ==NO_LOCK );.
bff10 20 72 65 73 20 3d 20 67 65 74 52 65 61 64 4c 6f res = getReadLo
bff20 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 ck(pFile);. i
bff30 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 f( res == NO_ERR
bff40 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c OR ){. newL
bff50 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 ocktype = SHARED
bff60 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 _LOCK;. }.
bff70 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b OSTRACE3( "LOCK
bff80 20 25 64 20 61 63 71 75 69 72 65 20 73 68 61 72 %d acquire shar
bff90 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c ed lock. res=%d\
bffa0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 n", pFile->h, re
bffb0 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 s );. }.. /* A
bffc0 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56 45 cquire a RESERVE
bffd0 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 D lock. */. if
bffe0 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 ( locktype==RESE
bfff0 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 RVED_LOCK && res
c0000 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a == NO_ERROR ){.
c0010 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c assert( pFil
c0020 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 e->locktype==SHA
c0030 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 RED_LOCK );.
c0040 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 LockArea.lOffset
c0050 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 = RESERVED_BYTE
c0060 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c ;. LockArea.l
c0070 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 Range = 1L;.
c0080 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 UnlockArea.lOffs
c0090 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c et = 0L;. Unl
c00a0 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d ockArea.lRange =
c00b0 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 0L;. res = D
c00c0 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 osSetFileLocks(
c00d0 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 pFile->h, &Unloc
c00e0 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 kArea, &LockArea
c00f0 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 , LOCK_TIMEOUT,
c0100 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 0L );. if( re
c0110 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b s == NO_ERROR ){
c0120 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 . newLockty
c0130 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f pe = RESERVED_LO
c0140 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 CK;. }. OS
c0150 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 TRACE3( "LOCK %d
c0160 20 61 63 71 75 69 72 65 20 72 65 73 65 72 76 65 acquire reserve
c0170 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e d lock. res=%d\n
c0180 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 ", pFile->h, res
c0190 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 );. }.. /* Ac
c01a0 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 quire a PENDING
c01b0 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 lock. */. if(
c01c0 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 locktype==EXCLUS
c01d0 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 IVE_LOCK && res
c01e0 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 == NO_ERROR ){.
c01f0 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d newLocktype =
c0200 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 PENDING_LOCK;.
c0210 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 gotPendingLoc
c0220 6b 20 3d 20 30 3b 0a 20 20 20 20 4f 53 54 52 41 k = 0;. OSTRA
c0230 43 45 32 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 CE2( "LOCK %d ac
c0240 71 75 69 72 65 20 70 65 6e 64 69 6e 67 20 6c 6f quire pending lo
c0250 63 6b 2e 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b ck. pending lock
c0260 20 62 6f 6f 6c 65 61 6e 20 75 6e 73 65 74 2e 5c boolean unset.\
c0270 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a n", pFile->h );.
c0280 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 }.. /* Acquir
c0290 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c e an EXCLUSIVE l
c02a0 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c ock. */. if( l
c02b0 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 ocktype==EXCLUSI
c02c0 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d VE_LOCK && res =
c02d0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
c02e0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d assert( pFile-
c02f0 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 45 >locktype>=SHARE
c0300 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 D_LOCK );. re
c0310 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f s = unlockReadLo
c0320 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f ck(pFile);. O
c0330 53 54 52 41 43 45 32 28 20 22 75 6e 72 65 61 64 STRACE2( "unread
c0340 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 lock = %d\n", re
c0350 73 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 s );. LockAre
c0360 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 52 a.lOffset = SHAR
c0370 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 4c 6f ED_FIRST;. Lo
c0380 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
c0390 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 SHARED_SIZE;.
c03a0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 UnlockArea.lOff
c03b0 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e set = 0L;. Un
c03c0 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 lockArea.lRange
c03d0 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 = 0L;. res =
c03e0 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 DosSetFileLocks(
c03f0 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f pFile->h, &Unlo
c0400 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
c0410 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c a, LOCK_TIMEOUT,
c0420 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 0L );. if( r
c0430 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 es == NO_ERROR )
c0440 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 {. newLockt
c0450 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f ype = EXCLUSIVE_
c0460 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b LOCK;. }else{
c0470 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 . OSTRACE2(
c0480 20 22 4f 53 2f 32 20 65 72 72 6f 72 2d 63 6f 64 "OS/2 error-cod
c0490 65 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 29 e = %d\n", res )
c04a0 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61 64 4c ;. getReadL
c04b0 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 ock(pFile);.
c04c0 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 }. OSTRACE3(
c04d0 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72 65 "LOCK %d acquire
c04e0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e exclusive lock.
c04f0 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 res=%d\n", pFi
c0500 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 le->h, res );.
c0510 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 }.. /* If we ar
c0520 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44 e holding a PEND
c0530 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75 ING lock that ou
c0540 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65 61 73 ght to be releas
c0550 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 ed, then. ** re
c0560 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 lease it now..
c0570 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 6e 64 */. if( gotPend
c0580 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 ingLock && lockt
c0590 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
c05a0 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 3b 0a 20 ){. int r;.
c05b0 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 LockArea.lOff
c05c0 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f set = 0L;. Lo
c05d0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
c05e0 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 0L;. UnlockAr
c05f0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e ea.lOffset = PEN
c0600 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 DING_BYTE;. U
c0610 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
c0620 20 3d 20 31 4c 3b 0a 20 20 20 20 72 20 3d 20 44 = 1L;. r = D
c0630 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 osSetFileLocks(
c0640 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 pFile->h, &Unloc
c0650 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 kArea, &LockArea
c0660 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 , LOCK_TIMEOUT,
c0670 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 0L );. OSTRAC
c0680 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 75 6e 6c E3( "LOCK %d unl
c0690 6f 63 6b 69 6e 67 20 70 65 6e 64 69 6e 67 2f 69 ocking pending/i
c06a0 73 20 73 68 61 72 65 64 2e 20 72 3d 25 64 5c 6e s shared. r=%d\n
c06b0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20 29 ", pFile->h, r )
c06c0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 ;. }.. /* Upda
c06d0 74 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 te the state of
c06e0 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c the lock has hel
c06f0 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 d in the file de
c0700 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 scriptor then.
c0710 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61 70 ** return the ap
c0720 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74 propriate result
c0730 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 code.. */. if
c0740 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f ( res == NO_ERRO
c0750 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 R ){. rc = SQ
c0760 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 LITE_OK;. }else
c0770 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 20 {. OSTRACE4(
c0780 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 20 "LOCK FAILED %d
c0790 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20 62 75 trying for %d bu
c07a0 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69 t got %d\n", pFi
c07b0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 le->h,.
c07c0 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e locktype, n
c07d0 65 77 4c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 ewLocktype );.
c07e0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 rc = SQLITE_BU
c07f0 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d SY;. }. pFile-
c0800 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6e 65 77 4c >locktype = newL
c0810 6f 63 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 ocktype;. OSTRA
c0820 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 6e 6f CE3( "LOCK %d no
c0830 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e w %d\n", pFile->
c0840 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 h, pFile->lockty
c0850 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 pe );. return r
c0860 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 c;.}../*.** This
c0870 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 routine checks
c0880 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 if there is a RE
c0890 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 SERVED lock held
c08a0 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 on the specifie
c08b0 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 d.** file by thi
c08c0 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 s or any other p
c08d0 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 rocess. If such
c08e0 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 a lock is held,
c08f0 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 return.** non-ze
c0900 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 7a 65 ro, otherwise ze
c0910 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ro..*/.static in
c0920 74 20 6f 73 32 43 68 65 63 6b 52 65 73 65 72 76 t os2CheckReserv
c0930 65 64 4c 6f 63 6b 28 20 73 71 6c 69 74 65 33 5f edLock( sqlite3_
c0940 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 file *id, int *p
c0950 4f 75 74 20 29 7b 0a 20 20 69 6e 74 20 72 20 3d Out ){. int r =
c0960 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 0;. os2File *p
c0970 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a File = (os2File*
c0980 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 70 )id;. assert( p
c0990 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66 28 File!=0 );. if(
c09a0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
c09b0 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 >=RESERVED_LOCK
c09c0 29 7b 0a 20 20 20 20 72 20 3d 20 31 3b 0a 20 20 ){. r = 1;.
c09d0 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 OSTRACE3( "TES
c09e0 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 T WR-LOCK %d %d
c09f0 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c (local)\n", pFil
c0a00 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 65 6c e->h, r );. }el
c0a10 73 65 7b 0a 20 20 20 20 46 49 4c 45 4c 4f 43 4b se{. FILELOCK
c0a20 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 LockArea,.
c0a30 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b Unlock
c0a40 41 72 65 61 3b 0a 20 20 20 20 41 50 49 52 45 54 Area;. APIRET
c0a50 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a rc = NO_ERROR;.
c0a60 20 20 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b memset(&Lock
c0a70 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Area, 0, sizeof(
c0a80 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 LockArea));.
c0a90 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 memset(&UnlockAr
c0aa0 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e ea, 0, sizeof(Un
c0ab0 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 lockArea));.
c0ac0 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 LockArea.lOffset
c0ad0 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 = RESERVED_BYTE
c0ae0 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c ;. LockArea.l
c0af0 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 Range = 1L;.
c0b00 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 UnlockArea.lOffs
c0b10 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c et = 0L;. Unl
c0b20 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d ockArea.lRange =
c0b30 20 30 4c 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 0L;. rc = Do
c0b40 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 sSetFileLocks( p
c0b50 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b File->h, &Unlock
c0b60 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c Area, &LockArea,
c0b70 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 LOCK_TIMEOUT, 0
c0b80 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 L );. OSTRACE
c0b90 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 3( "TEST WR-LOCK
c0ba0 20 25 64 20 6c 6f 63 6b 20 72 65 73 65 72 76 65 %d lock reserve
c0bb0 64 20 62 79 74 65 20 72 63 3d 25 64 5c 6e 22 2c d byte rc=%d\n",
c0bc0 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20 29 3b pFile->h, rc );
c0bd0 0a 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20 4e . if( rc == N
c0be0 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 O_ERROR ){.
c0bf0 20 41 50 49 52 45 54 20 72 63 75 20 3d 20 4e 4f APIRET rcu = NO
c0c00 5f 45 52 52 4f 52 3b 20 2f 2a 20 72 65 74 75 72 _ERROR; /* retur
c0c10 6e 20 63 6f 64 65 20 66 6f 72 20 75 6e 6c 6f 63 n code for unloc
c0c20 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 4c 6f king */. Lo
c0c30 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
c0c40 20 30 4c 3b 0a 20 20 20 20 20 20 4c 6f 63 6b 41 0L;. LockA
c0c50 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b rea.lRange = 0L;
c0c60 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 . UnlockAre
c0c70 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 a.lOffset = RESE
c0c80 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 20 RVED_BYTE;.
c0c90 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e UnlockArea.lRan
c0ca0 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 20 20 72 ge = 1L;. r
c0cb0 63 75 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c cu = DosSetFileL
c0cc0 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 ocks( pFile->h,
c0cd0 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f &UnlockArea, &Lo
c0ce0 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d ckArea, LOCK_TIM
c0cf0 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 EOUT, 0L );.
c0d00 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 OSTRACE3( "TES
c0d10 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 75 6e 6c T WR-LOCK %d unl
c0d20 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62 79 74 ock reserved byt
c0d30 65 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 e r=%d\n", pFile
c0d40 2d 3e 68 2c 20 72 63 75 20 29 3b 0a 20 20 20 20 ->h, rcu );.
c0d50 7d 0a 20 20 20 20 72 20 3d 20 21 28 72 63 20 3d }. r = !(rc =
c0d60 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 20 = NO_ERROR);.
c0d70 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 54 OSTRACE3( "TEST
c0d80 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 WR-LOCK %d %d (
c0d90 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c remote)\n", pFil
c0da0 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 0a 20 e->h, r );. }.
c0db0 20 2a 70 4f 75 74 20 3d 20 72 3b 0a 20 20 72 65 *pOut = r;. re
c0dc0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
c0dd0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 }../*.** Lower t
c0de0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
c0df0 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 on file descrip
c0e00 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 tor id to lockty
c0e10 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a pe. locktype.**
c0e20 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 must be either
c0e30 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 NO_LOCK or SHARE
c0e40 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 D_LOCK..**.** If
c0e50 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 the locking lev
c0e60 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 el of the file d
c0e70 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 escriptor is alr
c0e80 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 eady at or below
c0e90 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 .** the requeste
c0ea0 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c d locking level,
c0eb0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
c0ec0 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 a no-op..**.**
c0ed0 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 It is not possib
c0ee0 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f 75 74 le for this rout
c0ef0 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66 20 74 ine to fail if t
c0f00 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
c0f10 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b nt.** is NO_LOCK
c0f20 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 . If the second
c0f30 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 48 41 argument is SHA
c0f40 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 RED_LOCK then th
c0f50 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 is routine.** mi
c0f60 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ght return SQLIT
c0f70 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 E_IOERR;.*/.stat
c0f80 69 63 20 69 6e 74 20 6f 73 32 55 6e 6c 6f 63 6b ic int os2Unlock
c0f90 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a ( sqlite3_file *
c0fa0 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
c0fb0 20 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a ){. int type;.
c0fc0 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 os2File *pFile
c0fd0 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b = (os2File*)id;
c0fe0 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 53 . APIRET rc = S
c0ff0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 41 50 49 52 QLITE_OK;. APIR
c1000 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52 52 4f ET res = NO_ERRO
c1010 52 3b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c R;. FILELOCK L
c1020 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 ockArea,.
c1030 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b UnlockArea;
c1040 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 . memset(&LockA
c1050 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c rea, 0, sizeof(L
c1060 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d ockArea));. mem
c1070 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c set(&UnlockArea,
c1080 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 0, sizeof(Unloc
c1090 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73 65 72 kArea));. asser
c10a0 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 t( pFile!=0 );.
c10b0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
c10c0 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e<=SHARED_LOCK )
c10d0 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 55 ;. OSTRACE4( "U
c10e0 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 77 NLOCK %d to %d w
c10f0 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d as %d\n", pFile-
c1100 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 >h, locktype, pF
c1110 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b ile->locktype );
c1120 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c 65 2d . type = pFile-
c1130 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 >locktype;. if(
c1140 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 type>=EXCLUSIVE
c1150 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 _LOCK ){. Loc
c1160 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 kArea.lOffset =
c1170 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 0L;. LockArea
c1180 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 .lRange = 0L;.
c1190 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 UnlockArea.lOf
c11a0 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 fset = SHARED_FI
c11b0 52 53 54 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 RST;. UnlockA
c11c0 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 rea.lRange = SHA
c11d0 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 72 65 RED_SIZE;. re
c11e0 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f s = DosSetFileLo
c11f0 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 cks( pFile->h, &
c1200 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 UnlockArea, &Loc
c1210 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 kArea, LOCK_TIME
c1220 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f OUT, 0L );. O
c1230 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b STRACE3( "UNLOCK
c1240 20 25 64 20 65 78 63 6c 75 73 69 76 65 20 6c 6f %d exclusive lo
c1250 63 6b 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 ck res=%d\n", pF
c1260 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 ile->h, res );.
c1270 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d if( locktype=
c1280 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 =SHARED_LOCK &&
c1290 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c getReadLock(pFil
c12a0 65 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 e) != NO_ERROR )
c12b0 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 {. /* This
c12c0 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70 should never hap
c12d0 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 pen. We should
c12e0 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65 20 74 always be able t
c12f0 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 63 71 o. ** reacq
c1300 75 69 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f uire the read lo
c1310 63 6b 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54 52 ck */. OSTR
c1320 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 ACE3( "UNLOCK %d
c1330 20 74 6f 20 25 64 20 67 65 74 52 65 61 64 4c 6f to %d getReadLo
c1340 63 6b 28 29 20 66 61 69 6c 65 64 5c 6e 22 2c 20 ck() failed\n",
c1350 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 pFile->h, lockty
c1360 70 65 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d pe );. rc =
c1370 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e SQLITE_IOERR_UN
c1380 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a LOCK;. }. }.
c1390 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45 53 45 if( type>=RESE
c13a0 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 RVED_LOCK ){.
c13b0 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 LockArea.lOffse
c13c0 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b t = 0L;. Lock
c13d0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c Area.lRange = 0L
c13e0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 ;. UnlockArea
c13f0 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52 .lOffset = RESER
c1400 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e VED_BYTE;. Un
c1410 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 lockArea.lRange
c1420 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 = 1L;. res =
c1430 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 DosSetFileLocks(
c1440 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f pFile->h, &Unlo
c1450 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
c1460 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c a, LOCK_TIMEOUT,
c1470 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 0L );. OSTRA
c1480 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 CE3( "UNLOCK %d
c1490 72 65 73 65 72 76 65 64 20 72 65 73 3d 25 64 5c reserved res=%d\
c14a0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 n", pFile->h, re
c14b0 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6c s );. }. if( l
c14c0 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b ocktype==NO_LOCK
c14d0 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 45 44 && type>=SHARED
c14e0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 _LOCK ){. res
c14f0 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 = unlockReadLoc
c1500 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 k(pFile);. OS
c1510 54 52 41 43 45 35 28 20 22 55 4e 4c 4f 43 4b 20 TRACE5( "UNLOCK
c1520 25 64 20 69 73 20 25 64 20 77 61 6e 74 20 25 64 %d is %d want %d
c1530 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c res=%d\n", pFil
c1540 65 2d 3e 68 2c 20 74 79 70 65 2c 20 6c 6f 63 6b e->h, type, lock
c1550 74 79 70 65 2c 20 72 65 73 20 29 3b 0a 20 20 7d type, res );. }
c1560 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e . if( type>=PEN
c1570 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 DING_LOCK ){.
c1580 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 LockArea.lOffse
c1590 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b t = 0L;. Lock
c15a0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c Area.lRange = 0L
c15b0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 ;. UnlockArea
c15c0 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 .lOffset = PENDI
c15d0 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c NG_BYTE;. Unl
c15e0 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d ockArea.lRange =
c15f0 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 1L;. res = D
c1600 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 osSetFileLocks(
c1610 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 pFile->h, &Unloc
c1620 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 kArea, &LockArea
c1630 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 , LOCK_TIMEOUT,
c1640 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 0L );. OSTRAC
c1650 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 70 E3( "UNLOCK %d p
c1660 65 6e 64 69 6e 67 20 72 65 73 3d 25 64 5c 6e 22 ending res=%d\n"
c1670 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 , pFile->h, res
c1680 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e );. }. pFile->
c1690 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 locktype = lockt
c16a0 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 ype;. OSTRACE3(
c16b0 20 22 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f 77 20 "UNLOCK %d now
c16c0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
c16d0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
c16e0 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b );. return rc;
c16f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f .}../*.** Contro
c1700 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74 l and query of t
c1710 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e he open file han
c1720 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 dle..*/.static i
c1730 6e 74 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f nt os2FileContro
c1740 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a l(sqlite3_file *
c1750 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 id, int op, void
c1760 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 *pArg){. switc
c1770 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 h( op ){. cas
c1780 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c e SQLITE_FCNTL_L
c1790 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 OCKSTATE: {.
c17a0 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 *(int*)pArg =
c17b0 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e ((os2File*)id)->
c17c0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 locktype;.
c17d0 4f 53 54 52 41 43 45 33 28 20 22 46 43 4e 54 4c OSTRACE3( "FCNTL
c17e0 5f 4c 4f 43 4b 53 54 41 54 45 20 25 64 20 6c 6f _LOCKSTATE %d lo
c17f0 63 6b 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 32 46 ck=%d\n", ((os2F
c1800 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 28 6f ile*)id)->h, ((o
c1810 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 s2File*)id)->loc
c1820 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72 ktype );. r
c1830 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
c1840 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
c1850 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
c1860 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
c1870 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a n the sector siz
c1880 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 e in bytes of th
c1890 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f e underlying blo
c18a0 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a ck device for.**
c18b0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66 the specified f
c18c0 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d ile. This is alm
c18d0 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 ost always 512 b
c18e0 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 ytes, but may be
c18f0 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 .** larger for s
c1900 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a ome devices..**.
c1910 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 ** SQLite code a
c1920 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 ssumes this func
c1930 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c tion cannot fail
c1940 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 . It also assume
c1950 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f s that.** if two
c1960 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 files are creat
c1970 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 ed in the same f
c1980 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 ile-system direc
c1990 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 tory (i.e..** a
c19a0 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 database and its
c19b0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 journal file) t
c19c0 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 hat the sector s
c19d0 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a ize will be the.
c19e0 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 ** same for both
c19f0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
c1a00 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 28 73 71 os2SectorSize(sq
c1a10 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b lite3_file *id){
c1a20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
c1a30 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f _DEFAULT_SECTOR_
c1a40 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 SIZE;.}../*.** R
c1a50 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72 20 6f eturn a vector o
c1a60 66 20 64 65 76 69 63 65 20 63 68 61 72 61 63 74 f device charact
c1a70 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 61 eristics..*/.sta
c1a80 74 69 63 20 69 6e 74 20 6f 73 32 44 65 76 69 63 tic int os2Devic
c1a90 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 eCharacteristics
c1aa0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
c1ab0 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a d){. return 0;.
c1ac0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 63 }.../*.** Charac
c1ad0 74 65 72 20 73 65 74 20 63 6f 6e 76 65 72 73 69 ter set conversi
c1ae0 6f 6e 20 6f 62 6a 65 63 74 73 20 75 73 65 64 20 on objects used
c1af0 62 79 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72 6f by conversion ro
c1b00 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 utines..*/.stati
c1b10 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20 75 63 c UconvObject uc
c1b20 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f 2a 20 Utf8 = NULL; /*
c1b30 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65 6e 20 convert between
c1b40 55 54 46 2d 38 20 61 6e 64 20 55 43 53 2d 32 20 UTF-8 and UCS-2
c1b50 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e 76 4f */.static UconvO
c1b60 62 6a 65 63 74 20 75 63 6c 43 70 20 3d 20 4e 55 bject uclCp = NU
c1b70 4c 4c 3b 20 20 2f 2a 20 63 6f 6e 76 65 72 74 20 LL; /* convert
c1b80 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 20 63 6f between local co
c1b90 64 65 70 61 67 65 20 61 6e 64 20 55 43 53 2d 32 depage and UCS-2
c1ba0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 */../*.** Helpe
c1bb0 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e r function to in
c1bc0 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e itialize the con
c1bd0 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 version objects
c1be0 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54 46 2d from and to UTF-
c1bf0 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 8..*/.static voi
c1c00 64 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 d initUconvObjec
c1c10 74 73 28 20 76 6f 69 64 20 29 7b 0a 20 20 69 66 ts( void ){. if
c1c20 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76 ( UniCreateUconv
c1c30 4f 62 6a 65 63 74 28 20 55 54 46 5f 38 2c 20 26 Object( UTF_8, &
c1c40 75 63 55 74 66 38 20 29 20 21 3d 20 55 4c 53 5f ucUtf8 ) != ULS_
c1c50 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 75 63 SUCCESS ). uc
c1c60 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20 69 Utf8 = NULL;. i
c1c70 66 20 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f f ( UniCreateUco
c1c80 6e 76 4f 62 6a 65 63 74 28 20 28 55 6e 69 43 68 nvObject( (UniCh
c1c90 61 72 20 2a 29 4c 22 40 70 61 74 68 3d 79 65 73 ar *)L"@path=yes
c1ca0 22 2c 20 26 75 63 6c 43 70 20 29 20 21 3d 20 55 ", &uclCp ) != U
c1cb0 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 LS_SUCCESS ).
c1cc0 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d uclCp = NULL;.}
c1cd0 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 ../*.** Helper f
c1ce0 75 6e 63 74 69 6f 6e 20 74 6f 20 66 72 65 65 20 unction to free
c1cf0 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f the conversion o
c1d00 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e 64 20 bjects from and
c1d10 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74 61 to UTF-8..*/.sta
c1d20 74 69 63 20 76 6f 69 64 20 66 72 65 65 55 63 6f tic void freeUco
c1d30 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69 64 20 nvObjects( void
c1d40 29 7b 0a 20 20 69 66 20 28 20 75 63 55 74 66 38 ){. if ( ucUtf8
c1d50 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65 55 63 ). UniFreeUc
c1d60 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 55 74 66 onvObject( ucUtf
c1d70 38 20 29 3b 0a 20 20 69 66 20 28 20 75 63 6c 43 8 );. if ( uclC
c1d80 70 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65 55 p ). UniFreeU
c1d90 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 6c 43 convObject( uclC
c1da0 70 20 29 3b 0a 20 20 75 63 55 74 66 38 20 3d 20 p );. ucUtf8 =
c1db0 4e 55 4c 4c 3b 0a 20 20 75 63 6c 43 70 20 3d 20 NULL;. uclCp =
c1dc0 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 NULL;.}../*.** H
c1dd0 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 elper function t
c1de0 6f 20 63 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 o convert UTF-8
c1df0 66 69 6c 65 6e 61 6d 65 73 20 74 6f 20 6c 6f 63 filenames to loc
c1e00 61 6c 20 4f 53 2f 32 20 63 6f 64 65 70 61 67 65 al OS/2 codepage
c1e10 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 ..** The two-ste
c1e20 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74 p process: first
c1e30 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63 convert the inc
c1e40 6f 6d 69 6e 67 20 55 54 46 2d 38 20 73 74 72 69 oming UTF-8 stri
c1e50 6e 67 0a 2a 2a 20 69 6e 74 6f 20 55 43 53 2d 32 ng.** into UCS-2
c1e60 20 61 6e 64 20 74 68 65 6e 20 66 72 6f 6d 20 55 and then from U
c1e70 43 53 2d 32 20 74 6f 20 74 68 65 20 63 75 72 72 CS-2 to the curr
c1e80 65 6e 74 20 63 6f 64 65 70 61 67 65 2e 0a 2a 2a ent codepage..**
c1e90 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 63 68 The returned ch
c1ea0 61 72 20 70 6f 69 6e 74 65 72 20 68 61 73 20 74 ar pointer has t
c1eb0 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f 0a 73 o be freed..*/.s
c1ec0 74 61 74 69 63 20 63 68 61 72 20 2a 63 6f 6e 76 tatic char *conv
c1ed0 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 ertUtf8PathToCp(
c1ee0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 20 const char *in
c1ef0 29 7b 0a 20 20 55 6e 69 43 68 61 72 20 74 65 6d ){. UniChar tem
c1f00 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41 54 48 pPath[CCHMAXPATH
c1f10 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d ];. char *out =
c1f20 20 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f 63 28 (char *)calloc(
c1f30 20 43 43 48 4d 41 58 50 41 54 48 2c 20 31 20 29 CCHMAXPATH, 1 )
c1f40 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74 20 29 0a ;.. if( !out ).
c1f50 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b return NULL;
c1f60 0a 0a 20 20 69 66 28 20 21 75 63 55 74 66 38 20 .. if( !ucUtf8
c1f70 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20 20 20 20 || !uclCp ).
c1f80 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 initUconvObjects
c1f90 28 29 3b 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d ();.. /* determ
c1fa0 69 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74 ine string for t
c1fb0 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 he conversion of
c1fc0 20 55 54 46 2d 38 20 77 68 69 63 68 20 69 73 20 UTF-8 which is
c1fd0 43 50 31 32 30 38 20 2a 2f 0a 20 20 69 66 28 20 CP1208 */. if(
c1fe0 55 6e 69 53 74 72 54 6f 55 63 73 28 20 75 63 55 UniStrToUcs( ucU
c1ff0 74 66 38 2c 20 74 65 6d 70 50 61 74 68 2c 20 28 tf8, tempPath, (
c2000 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d 41 char *)in, CCHMA
c2010 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53 5f 53 XPATH ) != ULS_S
c2020 55 43 43 45 53 53 20 29 0a 20 20 20 20 72 65 74 UCCESS ). ret
c2030 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 20 63 urn out; /* if c
c2040 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c 73 2c onversion fails,
c2050 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d 70 74 return the empt
c2060 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 2f y string */.. /
c2070 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 * conversion for
c2080 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67 current codepag
c2090 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 e which can be u
c20a0 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a 2f sed for paths */
c20b0 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d 55 63 73 . UniStrFromUcs
c20c0 28 20 75 63 6c 43 70 2c 20 6f 75 74 2c 20 74 65 ( uclCp, out, te
c20d0 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 58 50 41 mpPath, CCHMAXPA
c20e0 54 48 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 TH );.. return
c20f0 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 out;.}../*.** He
c2100 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f lper function to
c2110 20 63 6f 6e 76 65 72 74 20 66 69 6c 65 6e 61 6d convert filenam
c2120 65 73 20 66 72 6f 6d 20 6c 6f 63 61 6c 20 63 6f es from local co
c2130 64 65 70 61 67 65 20 74 6f 20 55 54 46 2d 38 2e depage to UTF-8.
c2140 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 70 .** The two-step
c2150 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74 20 process: first
c2160 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63 6f convert the inco
c2170 6d 69 6e 67 20 63 6f 64 65 70 61 67 65 2d 73 70 ming codepage-sp
c2180 65 63 69 66 69 63 0a 2a 2a 20 73 74 72 69 6e 67 ecific.** string
c2190 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64 20 into UCS-2 and
c21a0 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32 20 then from UCS-2
c21b0 74 6f 20 74 68 65 20 63 6f 64 65 70 61 67 65 20 to the codepage
c21c0 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 of UTF-8..** The
c21d0 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 20 70 returned char p
c21e0 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 62 65 ointer has to be
c21f0 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 freed..**.** Th
c2200 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e is function is n
c2210 6f 6e 2d 73 74 61 74 69 63 20 74 6f 20 62 65 20 on-static to be
c2220 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68 69 73 able to use this
c2230 20 69 6e 20 73 68 65 6c 6c 2e 63 20 61 6e 64 0a in shell.c and.
c2240 2a 2a 20 73 69 6d 69 6c 61 72 20 61 70 70 6c 69 ** similar appli
c2250 63 61 74 69 6f 6e 73 20 74 68 61 74 20 74 61 6b cations that tak
c2260 65 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 20 61 e command line a
c2270 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63 68 61 rguments..*/.cha
c2280 72 20 2a 63 6f 6e 76 65 72 74 43 70 50 61 74 68 r *convertCpPath
c2290 54 6f 55 74 66 38 28 20 63 6f 6e 73 74 20 63 68 ToUtf8( const ch
c22a0 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e 69 43 ar *in ){. UniC
c22b0 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43 43 48 har tempPath[CCH
c22c0 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68 61 72 MAXPATH];. char
c22d0 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20 2a 29 *out = (char *)
c22e0 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58 50 41 calloc( CCHMAXPA
c22f0 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66 28 20 TH, 1 );.. if(
c2300 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74 75 72 !out ). retur
c2310 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 20 21 n NULL;.. if( !
c2320 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c 43 70 ucUtf8 || !uclCp
c2330 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f 6e 76 ). initUconv
c2340 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20 2f 2a Objects();.. /*
c2350 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 conversion for
c2360 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 current codepage
c2370 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 which can be us
c2380 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a ed for paths */.
c2390 20 20 69 66 28 20 55 6e 69 53 74 72 54 6f 55 63 if( UniStrToUc
c23a0 73 28 20 75 63 6c 43 70 2c 20 74 65 6d 70 50 61 s( uclCp, tempPa
c23b0 74 68 2c 20 28 63 68 61 72 20 2a 29 69 6e 2c 20 th, (char *)in,
c23c0 43 43 48 4d 41 58 50 41 54 48 20 29 20 21 3d 20 CCHMAXPATH ) !=
c23d0 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 ULS_SUCCESS ).
c23e0 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a return out; /*
c23f0 20 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 if conversion f
c2400 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 74 68 65 ails, return the
c2410 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 2a 2f empty string */
c2420 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e 65 .. /* determine
c2430 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 string for the
c2440 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55 54 conversion of UT
c2450 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 50 31 F-8 which is CP1
c2460 32 30 38 20 2a 2f 0a 20 20 55 6e 69 53 74 72 46 208 */. UniStrF
c2470 72 6f 6d 55 63 73 28 20 75 63 55 74 66 38 2c 20 romUcs( ucUtf8,
c2480 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c 20 43 out, tempPath, C
c2490 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a 20 20 CHMAXPATH );..
c24a0 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f return out;.}../
c24b0 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 74 6f 72 *.** This vector
c24c0 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 65 defines all the
c24d0 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 63 61 methods that ca
c24e0 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e 0a n operate on an.
c24f0 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ** sqlite3_file
c2500 66 6f 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74 61 74 for os2..*/.stat
c2510 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
c2520 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 73 32 49 _io_methods os2I
c2530 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c oMethod = {. 1,
c2540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2550 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 /* iVers
c2560 69 6f 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c 6f 73 ion */. os2Clos
c2570 65 2c 0a 20 20 6f 73 32 52 65 61 64 2c 0a 20 20 e,. os2Read,.
c2580 6f 73 32 57 72 69 74 65 2c 0a 20 20 6f 73 32 54 os2Write,. os2T
c2590 72 75 6e 63 61 74 65 2c 0a 20 20 6f 73 32 53 79 runcate,. os2Sy
c25a0 6e 63 2c 0a 20 20 6f 73 32 46 69 6c 65 53 69 7a nc,. os2FileSiz
c25b0 65 2c 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a 20 20 e,. os2Lock,.
c25c0 6f 73 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f 73 32 os2Unlock,. os2
c25d0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
c25e0 6b 2c 0a 20 20 6f 73 32 46 69 6c 65 43 6f 6e 74 k,. os2FileCont
c25f0 72 6f 6c 2c 0a 20 20 6f 73 32 53 65 63 74 6f 72 rol,. os2Sector
c2600 53 69 7a 65 2c 0a 20 20 6f 73 32 44 65 76 69 63 Size,. os2Devic
c2610 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 eCharacteristics
c2620 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .};../**********
c2630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2670 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 74 *.** Here ends t
c2680 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74 he I/O methods t
c2690 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73 71 6c hat form the sql
c26a0 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
c26b0 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 object..**.** Th
c26c0 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20 e next block of
c26d0 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 code implements
c26e0 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64 73 2e the VFS methods.
c26f0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
c2700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
c2740 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74 /*.** Create a t
c2750 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 emporary file na
c2760 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 me in zBuf. zBu
c2770 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e f must be big en
c2780 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20 ough to.** hold
c2790 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e at pVfs->mxPathn
c27a0 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a ame characters..
c27b0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 */.static int ge
c27c0 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 tTempname(int nB
c27d0 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 20 29 uf, char *zBuf )
c27e0 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 {. static const
c27f0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a unsigned char z
c2800 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 Chars[] =. "a
c2810 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 bcdefghijklmnopq
c2820 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 rstuvwxyz". "
c2830 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 ABCDEFGHIJKLMNOP
c2840 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 QRSTUVWXYZ".
c2850 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 "0123456789";.
c2860 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61 72 int i, j;. char
c2870 20 7a 54 65 6d 70 50 61 74 68 42 75 66 5b 33 5d zTempPathBuf[3]
c2880 3b 0a 20 20 50 53 5a 20 7a 54 65 6d 70 50 61 74 ;. PSZ zTempPat
c2890 68 20 3d 20 28 50 53 5a 29 26 7a 54 65 6d 70 50 h = (PSZ)&zTempP
c28a0 61 74 68 42 75 66 3b 0a 20 20 69 66 28 20 73 71 athBuf;. if( sq
c28b0 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 lite3_temp_direc
c28c0 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 54 65 6d tory ){. zTem
c28d0 70 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 5f pPath = sqlite3_
c28e0 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a temp_directory;.
c28f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 }else{. if(
c2900 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53 DosScanEnv( (PS
c2910 5a 29 22 54 45 4d 50 22 2c 20 26 7a 54 65 6d 70 Z)"TEMP", &zTemp
c2920 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20 Path ) ){.
c2930 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 if( DosScanEnv(
c2940 28 50 53 5a 29 22 54 4d 50 22 2c 20 26 7a 54 65 (PSZ)"TMP", &zTe
c2950 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 mpPath ) ){.
c2960 20 20 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45 if( DosScanE
c2970 6e 76 28 20 28 50 53 5a 29 22 54 4d 50 44 49 52 nv( (PSZ)"TMPDIR
c2980 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 29 20 ", &zTempPath )
c2990 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 55 4c ){. UL
c29a0 4f 4e 47 20 75 6c 44 72 69 76 65 4e 75 6d 20 3d ONG ulDriveNum =
c29b0 20 30 2c 20 75 6c 44 72 69 76 65 4d 61 70 20 3d 0, ulDriveMap =
c29c0 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 44 0;. D
c29d0 6f 73 51 75 65 72 79 43 75 72 72 65 6e 74 44 69 osQueryCurrentDi
c29e0 73 6b 28 20 26 75 6c 44 72 69 76 65 4e 75 6d 2c sk( &ulDriveNum,
c29f0 20 26 75 6c 44 72 69 76 65 4d 61 70 20 29 3b 0a &ulDriveMap );.
c2a00 20 20 20 20 20 20 20 20 20 20 20 73 70 72 69 6e sprin
c2a10 74 66 28 20 28 63 68 61 72 2a 29 7a 54 65 6d 70 tf( (char*)zTemp
c2a20 50 61 74 68 2c 20 22 25 63 3a 22 2c 20 28 63 68 Path, "%c:", (ch
c2a30 61 72 29 28 20 27 41 27 20 2b 20 75 6c 44 72 69 ar)( 'A' + ulDri
c2a40 76 65 4e 75 6d 20 2d 20 31 20 29 20 29 3b 0a 20 veNum - 1 ) );.
c2a50 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
c2a60 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 . }. }. /*
c2a70 53 74 72 69 70 20 6f 66 66 20 61 20 74 72 61 69 Strip off a trai
c2a80 6c 69 6e 67 20 73 6c 61 73 68 65 73 20 6f 72 20 ling slashes or
c2a90 62 61 63 6b 73 6c 61 73 68 65 73 2c 20 6f 74 68 backslashes, oth
c2aa0 65 72 77 69 73 65 20 77 65 20 77 6f 75 6c 64 20 erwise we would
c2ab0 67 65 74 20 2a 0a 20 20 20 2a 20 6d 75 6c 74 69 get *. * multi
c2ac0 70 6c 65 20 28 62 61 63 6b 29 73 6c 61 73 68 65 ple (back)slashe
c2ad0 73 20 77 68 69 63 68 20 63 61 75 73 65 73 20 44 s which causes D
c2ae0 6f 73 4f 70 65 6e 28 29 20 74 6f 20 66 61 69 6c osOpen() to fail
c2af0 2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a . *
c2b00 0a 20 20 20 2a 20 54 72 61 69 6c 69 6e 67 20 73 . * Trailing s
c2b10 70 61 63 65 73 20 61 72 65 20 6e 6f 74 20 61 6c paces are not al
c2b20 6c 6f 77 65 64 2c 20 65 69 74 68 65 72 2e 20 20 lowed, either.
c2b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2b40 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 6a */. j
c2b50 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
c2b60 33 30 28 7a 54 65 6d 70 50 61 74 68 29 3b 0a 20 30(zTempPath);.
c2b70 20 77 68 69 6c 65 28 20 6a 20 3e 20 30 20 26 26 while( j > 0 &&
c2b80 20 28 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 ( zTempPath[j-1
c2b90 5d 20 3d 3d 20 27 5c 5c 27 20 7c 7c 20 7a 54 65 ] == '\\' || zTe
c2ba0 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 mpPath[j-1] == '
c2bb0 2f 27 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 /'.
c2bc0 20 20 20 20 20 20 20 7c 7c 20 7a 54 65 6d 70 50 || zTempP
c2bd0 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 20 27 20 ath[j-1] == ' '
c2be0 29 20 29 7b 0a 20 20 20 20 6a 2d 2d 3b 0a 20 20 ) ){. j--;.
c2bf0 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b 6a 5d }. zTempPath[j]
c2c00 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 21 = '\0';. if( !
c2c10 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 sqlite3_temp_dir
c2c20 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 63 68 ectory ){. ch
c2c30 61 72 20 2a 7a 54 65 6d 70 50 61 74 68 55 54 46 ar *zTempPathUTF
c2c40 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61 74 68 = convertCpPath
c2c50 54 6f 55 74 66 38 28 20 7a 54 65 6d 70 50 61 74 ToUtf8( zTempPat
c2c60 68 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 h );. sqlite3
c2c70 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d _snprintf( nBuf-
c2c80 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 30, zBuf,.
c2c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2ca0 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d "%s\\"SQLITE_TEM
c2cb0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a P_FILE_PREFIX, z
c2cc0 54 65 6d 70 50 61 74 68 55 54 46 20 29 3b 0a 20 TempPathUTF );.
c2cd0 20 20 20 66 72 65 65 28 20 7a 54 65 6d 70 50 61 free( zTempPa
c2ce0 74 68 55 54 46 20 29 3b 0a 20 20 7d 65 6c 73 65 thUTF );. }else
c2cf0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e {. sqlite3_sn
c2d00 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33 30 2c printf( nBuf-30,
c2d10 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20 zBuf,.
c2d20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25 73 "%s
c2d30 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 \\"SQLITE_TEMP_F
c2d40 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 65 6d ILE_PREFIX, zTem
c2d50 70 50 61 74 68 20 29 3b 0a 20 20 7d 0a 20 20 6a pPath );. }. j
c2d60 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
c2d70 33 30 28 20 7a 42 75 66 20 29 3b 0a 20 20 73 71 30( zBuf );. sq
c2d80 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 lite3_randomness
c2d90 28 20 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 20 29 ( 20, &zBuf[j] )
c2da0 3b 0a 20 20 66 6f 72 28 20 69 20 3d 20 30 3b 20 ;. for( i = 0;
c2db0 69 20 3c 20 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b i < 20; i++, j++
c2dc0 20 29 7b 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 ){. zBuf[j]
c2dd0 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 5b 20 = (char)zChars[
c2de0 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 ((unsigned char)
c2df0 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 zBuf[j])%(sizeof
c2e00 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 (zChars)-1) ];.
c2e10 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 }. zBuf[j] = 0
c2e20 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 20 22 54 ;. OSTRACE2( "T
c2e30 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25 73 EMP FILENAME: %s
c2e40 5c 6e 22 2c 20 7a 42 75 66 20 29 3b 0a 20 20 72 \n", zBuf );. r
c2e50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
c2e60 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 .}.../*.** Turn
c2e70 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e a relative pathn
c2e80 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 ame into a full
c2e90 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69 74 65 pathname. Write
c2ea0 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 74 the full.** pat
c2eb0 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 46 75 6c 6c hname into zFull
c2ec0 5b 5d 2e 20 20 7a 46 75 6c 6c 5b 5d 20 77 69 6c []. zFull[] wil
c2ed0 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 70 56 l be at least pV
c2ee0 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 2a fs->mxPathname.*
c2ef0 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e * bytes in size.
c2f00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
c2f10 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a s2FullPathname(.
c2f20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
c2f30 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a Vfs, /*
c2f40 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 Pointer to vfs
c2f50 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 object */. cons
c2f60 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 t char *zRelativ
c2f70 65 2c 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 e, /* Possi
c2f80 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70 bly relative inp
c2f90 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 ut path */. int
c2fa0 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 nFull,
c2fb0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
c2fc0 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 66 65 of output buffe
c2fd0 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 r in bytes */.
c2fe0 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20 20 20 char *zFull
c2ff0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
c3000 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a utput buffer */.
c3010 29 7b 0a 20 20 63 68 61 72 20 2a 7a 52 65 6c 61 ){. char *zRela
c3020 74 69 76 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 tiveCp = convert
c3030 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 52 Utf8PathToCp( zR
c3040 65 6c 61 74 69 76 65 20 29 3b 0a 20 20 63 68 61 elative );. cha
c3050 72 20 7a 46 75 6c 6c 43 70 5b 43 43 48 4d 41 58 r zFullCp[CCHMAX
c3060 50 41 54 48 5d 20 3d 20 22 5c 30 22 3b 0a 20 20 PATH] = "\0";.
c3070 63 68 61 72 20 2a 7a 46 75 6c 6c 55 54 46 3b 0a char *zFullUTF;.
c3080 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 44 6f APIRET rc = Do
c3090 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f 28 20 sQueryPathInfo(
c30a0 7a 52 65 6c 61 74 69 76 65 43 70 2c 20 46 49 4c zRelativeCp, FIL
c30b0 5f 51 55 45 52 59 46 55 4c 4c 4e 41 4d 45 2c 20 _QUERYFULLNAME,
c30c0 7a 46 75 6c 6c 43 70 2c 0a 20 20 20 20 20 20 20 zFullCp,.
c30d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c30e0 20 20 20 20 20 20 20 20 20 43 43 48 4d 41 58 50 CCHMAXP
c30f0 41 54 48 20 29 3b 0a 20 20 66 72 65 65 28 20 7a ATH );. free( z
c3100 52 65 6c 61 74 69 76 65 43 70 20 29 3b 0a 20 20 RelativeCp );.
c3110 7a 46 75 6c 6c 55 54 46 20 3d 20 63 6f 6e 76 65 zFullUTF = conve
c3120 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 28 20 rtCpPathToUtf8(
c3130 7a 46 75 6c 6c 43 70 20 29 3b 0a 20 20 73 71 6c zFullCp );. sql
c3140 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e ite3_snprintf( n
c3150 46 75 6c 6c 2c 20 7a 46 75 6c 6c 2c 20 7a 46 75 Full, zFull, zFu
c3160 6c 6c 55 54 46 20 29 3b 0a 20 20 66 72 65 65 28 llUTF );. free(
c3170 20 7a 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20 72 zFullUTF );. r
c3180 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 eturn rc == NO_E
c3190 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b RROR ? SQLITE_OK
c31a0 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b : SQLITE_IOERR;
c31b0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 .}.../*.** Open
c31c0 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 a file..*/.stati
c31d0 63 20 69 6e 74 20 6f 73 32 4f 70 65 6e 28 0a 20 c int os2Open(.
c31e0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
c31f0 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f fs, /
c3200 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 * Not used */.
c3210 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
c3220 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e, /*
c3230 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c Name of the fil
c3240 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 e */. sqlite3_f
c3250 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 ile *id,
c3260 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 /* Write th
c3270 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68 61 e SQLite file ha
c3280 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 ndle here */. i
c3290 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 nt flags,
c32a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c32b0 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20 Open mode flags
c32c0 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c */. int *pOutFl
c32d0 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 20 ags
c32e0 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72 65 74 /* Status ret
c32f0 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a urn flags */.){.
c3300 20 20 48 46 49 4c 45 20 68 3b 0a 20 20 55 4c 4f HFILE h;. ULO
c3310 4e 47 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 NG ulFileAttribu
c3320 74 65 20 3d 20 46 49 4c 45 5f 4e 4f 52 4d 41 4c te = FILE_NORMAL
c3330 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 65 6e ;. ULONG ulOpen
c3340 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 55 4c 4f Flags = 0;. ULO
c3350 4e 47 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 3d 20 NG ulOpenMode =
c3360 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 0;. os2File *pF
c3370 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 ile = (os2File*)
c3380 69 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 id;. APIRET rc
c3390 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 4c = NO_ERROR;. UL
c33a0 4f 4e 47 20 75 6c 41 63 74 69 6f 6e 3b 0a 20 20 ONG ulAction;.
c33b0 63 68 61 72 20 2a 7a 4e 61 6d 65 43 70 3b 0a 20 char *zNameCp;.
c33c0 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b 43 char zTmpname[C
c33d0 43 48 4d 41 58 50 41 54 48 2b 31 5d 3b 20 20 20 CHMAXPATH+1];
c33e0 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 68 6f /* Buffer to ho
c33f0 6c 64 20 6e 61 6d 65 20 6f 66 20 74 65 6d 70 20 ld name of temp
c3400 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 file */.. /* If
c3410 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
c3420 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e ment to this fun
c3430 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 ction is NULL, g
c3440 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a 2a 20 enerate a . **
c3450 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e temporary file n
c3460 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f ame to use . */
c3470 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 29 7b . if( !zName ){
c3480 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 67 65 . int rc = ge
c3490 74 54 65 6d 70 6e 61 6d 65 28 43 43 48 4d 41 58 tTempname(CCHMAX
c34a0 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 PATH+1, zTmpname
c34b0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
c34c0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
c34d0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
c34e0 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 7a }. zName = z
c34f0 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 0a 20 Tmpname;. }...
c3500 20 6d 65 6d 73 65 74 28 20 70 46 69 6c 65 2c 20 memset( pFile,
c3510 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c 65 0, sizeof(*pFile
c3520 29 20 29 3b 0a 0a 20 20 4f 53 54 52 41 43 45 32 ) );.. OSTRACE2
c3530 28 20 22 4f 50 45 4e 20 77 61 6e 74 20 25 64 5c ( "OPEN want %d\
c3540 6e 22 2c 20 66 6c 61 67 73 20 29 3b 0a 0a 20 20 n", flags );..
c3550 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 if( flags & SQLI
c3560 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
c3570 45 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d E ){. ulOpenM
c3580 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 43 45 ode |= OPEN_ACCE
c3590 53 53 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 20 SS_READWRITE;.
c35a0 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 OSTRACE1( "OPE
c35b0 4e 20 72 65 61 64 2f 77 72 69 74 65 5c 6e 22 20 N read/write\n"
c35c0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
c35d0 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 ulOpenMode |= OP
c35e0 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44 4f 4e EN_ACCESS_READON
c35f0 4c 59 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 LY;. OSTRACE1
c3600 28 20 22 4f 50 45 4e 20 72 65 61 64 20 6f 6e 6c ( "OPEN read onl
c3610 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 y\n" );. }.. i
c3620 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 f( flags & SQLIT
c3630 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 29 7b E_OPEN_CREATE ){
c3640 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 . ulOpenFlags
c3650 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f |= OPEN_ACTION_
c3660 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c OPEN_IF_EXISTS |
c3670 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 43 52 45 OPEN_ACTION_CRE
c3680 41 54 45 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20 ATE_IF_NEW;.
c3690 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 OSTRACE1( "OPEN
c36a0 6f 70 65 6e 20 6e 65 77 2f 63 72 65 61 74 65 5c open new/create\
c36b0 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 n" );. }else{.
c36c0 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 7c ulOpenFlags |
c36d0 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 = OPEN_ACTION_OP
c36e0 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c 20 4f EN_IF_EXISTS | O
c36f0 50 45 4e 5f 41 43 54 49 4f 4e 5f 46 41 49 4c 5f PEN_ACTION_FAIL_
c3700 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 54 52 IF_NEW;. OSTR
c3710 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70 65 6e ACE1( "OPEN open
c3720 20 65 78 69 73 74 69 6e 67 5c 6e 22 20 29 3b 0a existing\n" );.
c3730 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 }.. if( flags
c3740 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d & SQLITE_OPEN_M
c3750 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 75 6c AIN_DB ){. ul
c3760 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e OpenMode |= OPEN
c3770 5f 53 48 41 52 45 5f 44 45 4e 59 4e 4f 4e 45 3b _SHARE_DENYNONE;
c3780 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 . OSTRACE1( "
c3790 4f 50 45 4e 20 73 68 61 72 65 20 72 65 61 64 2f OPEN share read/
c37a0 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 write\n" );. }e
c37b0 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d lse{. ulOpenM
c37c0 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 52 ode |= OPEN_SHAR
c37d0 45 5f 44 45 4e 59 57 52 49 54 45 3b 0a 20 20 20 E_DENYWRITE;.
c37e0 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e OSTRACE1( "OPEN
c37f0 20 73 68 61 72 65 20 72 65 61 64 20 6f 6e 6c 79 share read only
c3800 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 \n" );. }.. if
c3810 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ( flags & SQLITE
c3820 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c _OPEN_DELETEONCL
c3830 4f 53 45 20 29 7b 0a 20 20 20 20 63 68 61 72 20 OSE ){. char
c3840 70 61 74 68 55 74 66 38 5b 43 43 48 4d 41 58 50 pathUtf8[CCHMAXP
c3850 41 54 48 5d 3b 0a 23 69 66 64 65 66 20 4e 44 45 ATH];.#ifdef NDE
c3860 42 55 47 20 2f 2a 20 77 68 65 6e 20 64 65 62 75 BUG /* when debu
c3870 67 67 69 6e 67 20 77 65 20 77 61 6e 74 20 74 6f gging we want to
c3880 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 make sure it is
c3890 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 20 20 deleted */.
c38a0 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 20 ulFileAttribute
c38b0 3d 20 46 49 4c 45 5f 48 49 44 44 45 4e 3b 0a 23 = FILE_HIDDEN;.#
c38c0 65 6e 64 69 66 0a 20 20 20 20 6f 73 32 46 75 6c endif. os2Ful
c38d0 6c 50 61 74 68 6e 61 6d 65 28 20 70 56 66 73 2c lPathname( pVfs,
c38e0 20 7a 4e 61 6d 65 2c 20 43 43 48 4d 41 58 50 41 zName, CCHMAXPA
c38f0 54 48 2c 20 70 61 74 68 55 74 66 38 20 29 3b 0a TH, pathUtf8 );.
c3900 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 pFile->pathT
c3910 6f 44 65 6c 20 3d 20 63 6f 6e 76 65 72 74 55 74 oDel = convertUt
c3920 66 38 50 61 74 68 54 6f 43 70 28 20 70 61 74 68 f8PathToCp( path
c3930 55 74 66 38 20 29 3b 0a 20 20 20 20 4f 53 54 52 Utf8 );. OSTR
c3940 41 43 45 31 28 20 22 4f 50 45 4e 20 68 69 64 64 ACE1( "OPEN hidd
c3950 65 6e 2f 64 65 6c 65 74 65 20 6f 6e 20 63 6c 6f en/delete on clo
c3960 73 65 20 66 69 6c 65 20 61 74 74 72 69 62 75 74 se file attribut
c3970 65 73 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 es\n" );. }else
c3980 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 {. pFile->pat
c3990 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 hToDel = NULL;.
c39a0 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 OSTRACE1( "OP
c39b0 45 4e 20 6e 6f 72 6d 61 6c 20 66 69 6c 65 20 61 EN normal file a
c39c0 74 74 72 69 62 75 74 65 5c 6e 22 20 29 3b 0a 20 ttribute\n" );.
c39d0 20 7d 0a 0a 20 20 2f 2a 20 61 6c 77 61 79 73 20 }.. /* always
c39e0 6f 70 65 6e 20 69 6e 20 72 61 6e 64 6f 6d 20 61 open in random a
c39f0 63 63 65 73 73 20 6d 6f 64 65 20 66 6f 72 20 70 ccess mode for p
c3a00 6f 73 73 69 62 6c 79 20 62 65 74 74 65 72 20 73 ossibly better s
c3a10 70 65 65 64 20 2a 2f 0a 20 20 75 6c 4f 70 65 6e peed */. ulOpen
c3a20 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 Mode |= OPEN_FLA
c3a30 47 53 5f 52 41 4e 44 4f 4d 3b 0a 20 20 75 6c 4f GS_RANDOM;. ulO
c3a40 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f penMode |= OPEN_
c3a50 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f 45 52 FLAGS_FAIL_ON_ER
c3a60 52 4f 52 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 ROR;. ulOpenMod
c3a70 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f e |= OPEN_FLAGS_
c3a80 4e 4f 49 4e 48 45 52 49 54 3b 0a 0a 20 20 7a 4e NOINHERIT;.. zN
c3a90 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 ameCp = convertU
c3aa0 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 4e 61 tf8PathToCp( zNa
c3ab0 6d 65 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 me );. rc = Dos
c3ac0 4f 70 65 6e 28 20 28 50 53 5a 29 7a 4e 61 6d 65 Open( (PSZ)zName
c3ad0 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 Cp,.
c3ae0 20 20 20 20 26 68 2c 0a 20 20 20 20 20 20 20 20 &h,.
c3af0 20 20 20 20 20 20 20 20 26 75 6c 41 63 74 69 6f &ulActio
c3b00 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n,.
c3b10 20 20 20 30 4c 2c 0a 20 20 20 20 20 20 20 20 20 0L,.
c3b20 20 20 20 20 20 20 20 75 6c 46 69 6c 65 41 74 74 ulFileAtt
c3b30 72 69 62 75 74 65 2c 0a 20 20 20 20 20 20 20 20 ribute,.
c3b40 20 20 20 20 20 20 20 20 75 6c 4f 70 65 6e 46 6c ulOpenFl
c3b50 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ags,.
c3b60 20 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 2c ulOpenMode,
c3b70 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
c3b80 20 28 50 45 41 4f 50 32 29 4e 55 4c 4c 20 29 3b (PEAOP2)NULL );
c3b90 0a 20 20 66 72 65 65 28 20 7a 4e 61 6d 65 43 70 . free( zNameCp
c3ba0 20 29 3b 0a 20 20 69 66 28 20 72 63 20 21 3d 20 );. if( rc !=
c3bb0 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
c3bc0 4f 53 54 52 41 43 45 37 28 20 22 4f 50 45 4e 20 OSTRACE7( "OPEN
c3bd0 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65 20 72 Invalid handle r
c3be0 63 3d 25 64 3a 20 7a 4e 61 6d 65 3d 25 73 2c 20 c=%d: zName=%s,
c3bf0 75 6c 41 63 74 69 6f 6e 3d 25 23 6c 78 2c 20 75 ulAction=%#lx, u
c3c00 6c 41 74 74 72 3d 25 23 6c 78 2c 20 75 6c 46 6c lAttr=%#lx, ulFl
c3c10 61 67 73 3d 25 23 6c 78 2c 20 75 6c 4d 6f 64 65 ags=%#lx, ulMode
c3c20 3d 25 23 6c 78 5c 6e 22 2c 0a 20 20 20 20 20 20 =%#lx\n",.
c3c30 20 20 20 20 20 20 20 20 72 63 2c 20 7a 4e 61 6d rc, zNam
c3c40 65 2c 20 75 6c 41 63 74 69 6f 6e 2c 20 75 6c 46 e, ulAction, ulF
c3c50 69 6c 65 41 74 74 72 69 62 75 74 65 2c 20 75 6c ileAttribute, ul
c3c60 4f 70 65 6e 46 6c 61 67 73 2c 20 75 6c 4f 70 65 OpenFlags, ulOpe
c3c70 6e 4d 6f 64 65 20 29 3b 0a 20 20 20 20 69 66 28 nMode );. if(
c3c80 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 pFile->pathToDe
c3c90 6c 20 29 0a 20 20 20 20 20 20 66 72 65 65 28 20 l ). free(
c3ca0 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c pFile->pathToDel
c3cb0 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 );. pFile->p
c3cc0 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b athToDel = NULL;
c3cd0 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 . if( flags &
c3ce0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
c3cf0 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 DWRITE ){.
c3d00 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45 4e 20 OSTRACE2( "OPEN
c3d10 25 64 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c %d Invalid handl
c3d20 65 5c 6e 22 2c 20 28 28 66 6c 61 67 73 20 7c 20 e\n", ((flags |
c3d30 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
c3d40 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54 45 5f ONLY) & ~SQLITE_
c3d50 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 20 OPEN_READWRITE)
c3d60 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
c3d70 6f 73 32 4f 70 65 6e 28 20 70 56 66 73 2c 20 7a os2Open( pVfs, z
c3d80 4e 61 6d 65 2c 20 69 64 2c 0a 20 20 20 20 20 20 Name, id,.
c3d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3da0 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 ((flags | SQLITE
c3db0 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 _OPEN_READONLY)
c3dc0 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 & ~SQLITE_OPEN_R
c3dd0 45 41 44 57 52 49 54 45 29 2c 0a 20 20 20 20 20 EADWRITE),.
c3de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3df0 20 70 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20 20 pOutFlags );.
c3e00 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
c3e10 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e eturn SQLITE_CAN
c3e20 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d TOPEN;. }. }
c3e30 0a 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67 .. if( pOutFlag
c3e40 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c s ){. *pOutFl
c3e50 61 67 73 20 3d 20 66 6c 61 67 73 20 26 20 53 51 ags = flags & SQ
c3e60 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
c3e70 49 54 45 20 3f 20 53 51 4c 49 54 45 5f 4f 50 45 ITE ? SQLITE_OPE
c3e80 4e 5f 52 45 41 44 57 52 49 54 45 20 3a 20 53 51 N_READWRITE : SQ
c3e90 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e LITE_OPEN_READON
c3ea0 4c 59 3b 0a 20 20 7d 0a 0a 20 20 70 46 69 6c 65 LY;. }.. pFile
c3eb0 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 6f 73 32 ->pMethod = &os2
c3ec0 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69 6c IoMethod;. pFil
c3ed0 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 4f 70 65 6e e->h = h;. Open
c3ee0 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 4f Counter(+1);. O
c3ef0 53 54 52 41 43 45 33 28 20 22 4f 50 45 4e 20 25 STRACE3( "OPEN %
c3f00 64 20 70 4f 75 74 46 6c 61 67 73 3d 25 64 5c 6e d pOutFlags=%d\n
c3f10 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 4f 75 ", pFile->h, pOu
c3f20 74 46 6c 61 67 73 20 29 3b 0a 20 20 72 65 74 75 tFlags );. retu
c3f30 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
c3f40 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 ./*.** Delete th
c3f50 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2f e named file..*/
c3f60 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 44 .static int os2D
c3f70 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 elete(. sqlite3
c3f80 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
c3f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3fa0 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f /* Not used on o
c3fb0 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 s2 */. const ch
c3fc0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 ar *zFilename,
c3fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
c3fe0 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 * Name of file t
c3ff0 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e o delete */. in
c4000 74 20 73 79 6e 63 44 69 72 20 20 20 20 20 20 20 t syncDir
c4010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c4020 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 /* Not used
c4030 20 6f 6e 20 6f 73 32 20 2a 2f 0a 29 7b 0a 20 20 on os2 */.){.
c4040 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 APIRET rc = NO_E
c4050 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a 7a 46 RROR;. char *zF
c4060 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 ilenameCp = conv
c4070 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 ertUtf8PathToCp(
c4080 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 zFilename );.
c4090 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
c40a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
c40b0 4f 45 52 52 5f 44 45 4c 45 54 45 20 29 3b 0a 20 OERR_DELETE );.
c40c0 20 72 63 20 3d 20 44 6f 73 44 65 6c 65 74 65 28 rc = DosDelete(
c40d0 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 43 (PSZ)zFilenameC
c40e0 70 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 69 p );. free( zFi
c40f0 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 4f 53 lenameCp );. OS
c4100 54 52 41 43 45 32 28 20 22 44 45 4c 45 54 45 20 TRACE2( "DELETE
c4110 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65 \"%s\"\n", zFile
c4120 6e 61 6d 65 20 29 3b 0a 20 20 72 65 74 75 72 6e name );. return
c4130 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 rc == NO_ERROR
c4140 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 ? SQLITE_OK : SQ
c4150 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 LITE_IOERR_DELET
c4160 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 E;.}../*.** Chec
c4170 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 k the existance
c4180 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20 61 20 and status of a
c4190 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
c41a0 69 6e 74 20 6f 73 32 41 63 63 65 73 73 28 0a 20 int os2Access(.
c41b0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
c41c0 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f fs, /* No
c41d0 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a 2f t used on os2 */
c41e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
c41f0 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 2f 2a 20 Filename, /*
c4200 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 Name of file to
c4210 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 check */. int f
c4220 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 lags,
c4230 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 /* Type of
c4240 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 test to make on
c4250 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 this file */. i
c4260 6e 74 20 2a 70 4f 75 74 20 20 20 20 20 20 20 20 nt *pOut
c4270 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
c4280 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a e results here *
c4290 2f 0a 29 7b 0a 20 20 46 49 4c 45 53 54 41 54 55 /.){. FILESTATU
c42a0 53 33 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e S3 fsts3ConfigIn
c42b0 66 6f 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 fo;. APIRET rc
c42c0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 68 = NO_ERROR;. ch
c42d0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 20 ar *zFilenameCp
c42e0 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 = convertUtf8Pat
c42f0 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61 6d 65 hToCp( zFilename
c4300 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 20 26 );.. memset( &
c4310 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2c fsts3ConfigInfo,
c4320 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73 33 0, sizeof(fsts3
c4330 43 6f 6e 66 69 67 49 6e 66 6f 29 20 29 3b 0a 20 ConfigInfo) );.
c4340 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50 61 rc = DosQueryPa
c4350 74 68 49 6e 66 6f 28 20 28 50 53 5a 29 7a 46 69 thInfo( (PSZ)zFi
c4360 6c 65 6e 61 6d 65 43 70 2c 20 46 49 4c 5f 53 54 lenameCp, FIL_ST
c4370 41 4e 44 41 52 44 2c 0a 20 20 20 20 20 20 20 20 ANDARD,.
c4380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c4390 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 &fsts3ConfigInf
c43a0 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45 53 54 o, sizeof(FILEST
c43b0 41 54 55 53 33 29 20 29 3b 0a 20 20 66 72 65 65 ATUS3) );. free
c43c0 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b ( zFilenameCp );
c43d0 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 41 43 . OSTRACE4( "AC
c43e0 43 45 53 53 20 66 73 74 73 33 43 6f 6e 66 69 67 CESS fsts3Config
c43f0 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 3d 25 64 Info.attrFile=%d
c4400 20 66 6c 61 67 73 3d 25 64 20 72 63 3d 25 64 5c flags=%d rc=%d\
c4410 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
c4420 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e fsts3ConfigInfo.
c4430 61 74 74 72 46 69 6c 65 2c 20 66 6c 61 67 73 2c attrFile, flags,
c4440 20 72 63 20 29 3b 0a 20 20 73 77 69 74 63 68 28 rc );. switch(
c4450 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 flags ){. ca
c4460 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 se SQLITE_ACCESS
c4470 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 73 65 20 _READ:. case
c4480 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 SQLITE_ACCESS_EX
c4490 49 53 54 53 3a 0a 20 20 20 20 20 20 72 63 20 3d ISTS:. rc =
c44a0 20 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 (rc == NO_ERROR
c44b0 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 );. OSTRACE
c44c0 33 28 20 22 41 43 43 45 53 53 20 25 73 20 61 63 3( "ACCESS %s ac
c44d0 63 65 73 73 20 6f 66 20 72 65 61 64 20 61 6e 64 cess of read and
c44e0 20 65 78 69 73 74 73 20 20 72 63 3d 25 64 5c 6e exists rc=%d\n
c44f0 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63 ", zFilename, rc
c4500 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b );. break;
c4510 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
c4520 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 _ACCESS_READWRIT
c4530 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 72 E:. rc = (r
c4540 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 26 c == NO_ERROR) &
c4550 26 20 28 20 28 66 73 74 73 33 43 6f 6e 66 69 67 & ( (fsts3Config
c4560 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 20 26 20 Info.attrFile &
c4570 46 49 4c 45 5f 52 45 41 44 4f 4e 4c 59 29 20 3d FILE_READONLY) =
c4580 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 4f 53 54 = 0 );. OST
c4590 52 41 43 45 33 28 20 22 41 43 43 45 53 53 20 25 RACE3( "ACCESS %
c45a0 73 20 61 63 63 65 73 73 20 6f 66 20 72 65 61 64 s access of read
c45b0 2f 77 72 69 74 65 20 20 72 63 3d 25 64 5c 6e 22 /write rc=%d\n"
c45c0 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63 20 , zFilename, rc
c45d0 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
c45e0 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 default:.
c45f0 20 20 20 61 73 73 65 72 74 28 20 21 22 49 6e 76 assert( !"Inv
c4600 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d alid flags argum
c4610 65 6e 74 22 20 29 3b 0a 20 20 7d 0a 20 20 2a 70 ent" );. }. *p
c4620 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 75 Out = rc;. retu
c4630 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
c4640 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
c4650 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e _OMIT_LOAD_EXTEN
c4660 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 SION./*.** Inter
c4670 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e faces for openin
c4680 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 g a shared libra
c4690 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 ry, finding entr
c46a0 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 y points.** with
c46b0 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 in the shared li
c46c0 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 brary, and closi
c46d0 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 ng the shared li
c46e0 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 brary..*/./*.**
c46f0 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f Interfaces for o
c4700 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20 pening a shared
c4710 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 library, finding
c4720 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a entry points.**
c4730 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 within the shar
c4740 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 ed library, and
c4750 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 closing the shar
c4760 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 ed library..*/.s
c4770 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 32 44 tatic void *os2D
c4780 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 lOpen(sqlite3_vf
c4790 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 s *pVfs, const c
c47a0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b har *zFilename){
c47b0 0a 20 20 55 43 48 41 52 20 6c 6f 61 64 45 72 72 . UCHAR loadErr
c47c0 5b 32 35 36 5d 3b 0a 20 20 48 4d 4f 44 55 4c 45 [256];. HMODULE
c47d0 20 68 6d 6f 64 3b 0a 20 20 41 50 49 52 45 54 20 hmod;. APIRET
c47e0 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c rc;. char *zFil
c47f0 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 enameCp = conver
c4800 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 7a 46 tUtf8PathToCp(zF
c4810 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 63 20 3d ilename);. rc =
c4820 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 28 28 DosLoadModule((
c4830 50 53 5a 29 6c 6f 61 64 45 72 72 2c 20 73 69 7a PSZ)loadErr, siz
c4840 65 6f 66 28 6c 6f 61 64 45 72 72 29 2c 20 7a 46 eof(loadErr), zF
c4850 69 6c 65 6e 61 6d 65 43 70 2c 20 26 68 6d 6f 64 ilenameCp, &hmod
c4860 29 3b 0a 20 20 66 72 65 65 28 7a 46 69 6c 65 6e );. free(zFilen
c4870 61 6d 65 43 70 29 3b 0a 20 20 72 65 74 75 72 6e ameCp);. return
c4880 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 rc != NO_ERROR
c4890 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 68 6d 6f ? 0 : (void*)hmo
c48a0 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e 6f 2d d;.}./*.** A no-
c48b0 6f 70 20 73 69 6e 63 65 20 74 68 65 20 65 72 72 op since the err
c48c0 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
c48d0 6e 65 64 20 6f 6e 20 74 68 65 20 44 6f 73 4c 6f ned on the DosLo
c48e0 61 64 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e 0a 2a adModule call..*
c48f0 2a 20 6f 73 32 44 6c 6f 70 65 6e 20 72 65 74 75 * os2Dlopen retu
c4900 72 6e 73 20 7a 65 72 6f 20 69 66 20 44 6f 73 4c rns zero if DosL
c4910 6f 61 64 4d 6f 64 75 6c 65 20 69 73 20 6e 6f 74 oadModule is not
c4920 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2f 0a successful..*/.
c4930 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 44 static void os2D
c4940 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 lError(sqlite3_v
c4950 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 fs *pVfs, int nB
c4960 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 uf, char *zBufOu
c4970 74 29 7b 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a t){./* no-op */.
c4980 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6f }.static void *o
c4990 73 32 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f s2DlSym(sqlite3_
c49a0 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 vfs *pVfs, void
c49b0 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 *pHandle, const
c49c0 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a char *zSymbol){.
c49d0 20 20 50 46 4e 20 70 66 6e 3b 0a 20 20 41 50 49 PFN pfn;. API
c49e0 52 45 54 20 72 63 3b 0a 20 20 72 63 20 3d 20 44 RET rc;. rc = D
c49f0 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64 72 28 osQueryProcAddr(
c4a00 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 (HMODULE)pHandle
c4a10 2c 20 30 4c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 26 , 0L, zSymbol, &
c4a20 70 66 6e 29 3b 0a 20 20 69 66 28 20 72 63 20 21 pfn);. if( rc !
c4a30 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
c4a40 20 20 2f 2a 20 69 66 20 74 68 65 20 73 79 6d 62 /* if the symb
c4a50 6f 6c 20 69 74 73 65 6c 66 20 77 61 73 20 6e 6f ol itself was no
c4a60 74 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 68 20 t found, search
c4a70 61 67 61 69 6e 20 66 6f 72 20 74 68 65 20 73 61 again for the sa
c4a80 6d 65 0a 20 20 20 20 20 2a 20 73 79 6d 62 6f 6c me. * symbol
c4a90 20 77 69 74 68 20 61 6e 20 65 78 74 72 61 20 75 with an extra u
c4aa0 6e 64 65 72 73 63 6f 72 65 2c 20 74 68 61 74 20 nderscore, that
c4ab0 6d 69 67 68 74 20 62 65 20 6e 65 65 64 65 64 20 might be needed
c4ac0 64 65 70 65 6e 64 69 6e 67 0a 20 20 20 20 20 2a depending. *
c4ad0 20 6f 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 on the calling
c4ae0 63 6f 6e 76 65 6e 74 69 6f 6e 20 2a 2f 0a 20 20 convention */.
c4af0 20 20 63 68 61 72 20 5f 7a 53 79 6d 62 6f 6c 5b char _zSymbol[
c4b00 32 35 36 5d 20 3d 20 22 5f 22 3b 0a 20 20 20 20 256] = "_";.
c4b10 73 74 72 6e 63 61 74 28 5f 7a 53 79 6d 62 6f 6c strncat(_zSymbol
c4b20 2c 20 7a 53 79 6d 62 6f 6c 2c 20 32 35 35 29 3b , zSymbol, 255);
c4b30 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 51 75 65 . rc = DosQue
c4b40 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d 4f 44 ryProcAddr((HMOD
c4b50 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30 4c 2c ULE)pHandle, 0L,
c4b60 20 5f 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e 29 _zSymbol, &pfn)
c4b70 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
c4b80 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 c != NO_ERROR ?
c4b90 30 20 3a 20 28 76 6f 69 64 2a 29 70 66 6e 3b 0a 0 : (void*)pfn;.
c4ba0 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 }.static void os
c4bb0 32 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 2DlClose(sqlite3
c4bc0 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 _vfs *pVfs, void
c4bd0 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 44 6f *pHandle){. Do
c4be0 73 46 72 65 65 4d 6f 64 75 6c 65 28 28 48 4d 4f sFreeModule((HMO
c4bf0 44 55 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d DULE)pHandle);.}
c4c00 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c .#else /* if SQL
c4c10 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 ITE_OMIT_LOAD_EX
c4c20 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e TENSION is defin
c4c30 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 ed: */. #define
c4c40 20 6f 73 32 44 6c 4f 70 65 6e 20 30 0a 20 20 23 os2DlOpen 0. #
c4c50 64 65 66 69 6e 65 20 6f 73 32 44 6c 45 72 72 6f define os2DlErro
c4c60 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 r 0. #define os
c4c70 32 44 6c 53 79 6d 20 30 0a 20 20 23 64 65 66 69 2DlSym 0. #defi
c4c80 6e 65 20 6f 73 32 44 6c 43 6c 6f 73 65 20 30 0a ne os2DlClose 0.
c4c90 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 57 #endif.../*.** W
c4ca0 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 66 20 rite up to nBuf
c4cb0 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e bytes of randomn
c4cc0 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e 0a 2a ess into zBuf..*
c4cd0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 /.static int os2
c4ce0 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 Randomness(sqlit
c4cf0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e e3_vfs *pVfs, in
c4d00 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 t nBuf, char *zB
c4d10 75 66 20 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 uf ){. int n =
c4d20 30 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 0;.#if defined(S
c4d30 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 6e 20 QLITE_TEST). n
c4d40 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74 = nBuf;. memset
c4d50 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b (zBuf, 0, nBuf);
c4d60 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73 69 7a .#else. int siz
c4d70 65 6f 66 55 4c 6f 6e 67 20 3d 20 73 69 7a 65 6f eofULong = sizeo
c4d80 66 28 55 4c 4f 4e 47 29 3b 0a 20 20 69 66 28 20 f(ULONG);. if(
c4d90 28 69 6e 74 29 73 69 7a 65 6f 66 28 44 41 54 45 (int)sizeof(DATE
c4da0 54 49 4d 45 29 20 3c 3d 20 6e 42 75 66 20 2d 20 TIME) <= nBuf -
c4db0 6e 20 29 7b 0a 20 20 20 20 44 41 54 45 54 49 4d n ){. DATETIM
c4dc0 45 20 78 3b 0a 20 20 20 20 44 6f 73 47 65 74 44 E x;. DosGetD
c4dd0 61 74 65 54 69 6d 65 28 26 78 29 3b 0a 20 20 20 ateTime(&x);.
c4de0 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d memcpy(&zBuf[n]
c4df0 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29 29 , &x, sizeof(x))
c4e00 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f ;. n += sizeo
c4e10 66 28 78 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 f(x);. }.. if(
c4e20 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 sizeofULong <=
c4e30 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 nBuf - n ){.
c4e40 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 44 PPIB ppib;. D
c4e50 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 osGetInfoBlocks(
c4e60 4e 55 4c 4c 2c 20 26 70 70 69 62 29 3b 0a 20 20 NULL, &ppib);.
c4e70 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e memcpy(&zBuf[n
c4e80 5d 2c 20 26 70 70 69 62 2d 3e 70 69 62 5f 75 6c ], &ppib->pib_ul
c4e90 70 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 pid, sizeofULong
c4ea0 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 );. n += size
c4eb0 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20 ofULong;. }..
c4ec0 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 if( sizeofULong
c4ed0 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 <= nBuf - n ){.
c4ee0 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 PTIB ptib;.
c4ef0 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 DosGetInfoBloc
c4f00 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b ks(&ptib, NULL);
c4f10 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 . memcpy(&zBu
c4f20 66 5b 6e 5d 2c 20 26 70 74 69 62 2d 3e 74 69 62 f[n], &ptib->tib
c4f30 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 _ptib2->tib2_ult
c4f40 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 id, sizeofULong)
c4f50 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f ;. n += sizeo
c4f60 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20 2f fULong;. }.. /
c4f70 2a 20 69 66 20 77 65 20 73 74 69 6c 6c 20 68 61 * if we still ha
c4f80 76 65 6e 27 74 20 66 69 6c 6c 65 64 20 74 68 65 ven't filled the
c4f90 20 62 75 66 66 65 72 20 79 65 74 20 74 68 65 20 buffer yet the
c4fa0 66 6f 6c 6c 6f 77 69 6e 67 20 77 69 6c 6c 20 2a following will *
c4fb0 2f 0a 20 20 2f 2a 20 67 72 61 62 20 65 76 65 72 /. /* grab ever
c4fc0 79 74 68 69 6e 67 20 6f 6e 63 65 20 69 6e 73 74 ything once inst
c4fd0 65 61 64 20 6f 66 20 6d 61 6b 69 6e 67 20 73 65 ead of making se
c4fe0 76 65 72 61 6c 20 63 61 6c 6c 73 20 66 6f 72 20 veral calls for
c4ff0 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 2a 2f a single item */
c5000 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f . if( sizeofULo
c5010 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 ng <= nBuf - n )
c5020 7b 0a 20 20 20 20 55 4c 4f 4e 47 20 75 6c 53 79 {. ULONG ulSy
c5030 73 49 6e 66 6f 5b 51 53 56 5f 4d 41 58 5d 3b 0a sInfo[QSV_MAX];.
c5040 20 20 20 20 44 6f 73 51 75 65 72 79 53 79 73 49 DosQuerySysI
c5050 6e 66 6f 28 31 4c 2c 20 51 53 56 5f 4d 41 58 2c nfo(1L, QSV_MAX,
c5060 20 75 6c 53 79 73 49 6e 66 6f 2c 20 73 69 7a 65 ulSysInfo, size
c5070 6f 66 55 4c 6f 6e 67 20 2a 20 51 53 56 5f 4d 41 ofULong * QSV_MA
c5080 58 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 X);.. memcpy(
c5090 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 &zBuf[n], &ulSys
c50a0 49 6e 66 6f 5b 51 53 56 5f 4d 53 5f 43 4f 55 4e Info[QSV_MS_COUN
c50b0 54 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c T - 1], sizeofUL
c50c0 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 ong);. n += s
c50d0 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 0a 20 20 20 izeofULong;..
c50e0 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 if( sizeofULong
c50f0 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a <= nBuf - n ){.
c5100 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 memcpy(&zB
c5110 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 uf[n], &ulSysInf
c5120 6f 5b 51 53 56 5f 54 49 4d 45 52 5f 49 4e 54 45 o[QSV_TIMER_INTE
c5130 52 56 41 4c 20 2d 20 31 5d 2c 20 73 69 7a 65 6f RVAL - 1], sizeo
c5140 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e fULong);. n
c5150 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b += sizeofULong;
c5160 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 . }. if( s
c5170 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 izeofULong <= nB
c5180 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20 uf - n ){.
c5190 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c memcpy(&zBuf[n],
c51a0 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f &ulSysInfo[QSV_
c51b0 54 49 4d 45 5f 4c 4f 57 20 2d 20 31 5d 2c 20 73 TIME_LOW - 1], s
c51c0 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 izeofULong);.
c51d0 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c n += sizeofUL
c51e0 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ong;. }. i
c51f0 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c f( sizeofULong <
c5200 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 = nBuf - n ){.
c5210 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 memcpy(&zBuf
c5220 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b [n], &ulSysInfo[
c5230 51 53 56 5f 54 49 4d 45 5f 48 49 47 48 20 2d 20 QSV_TIME_HIGH -
c5240 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 1], sizeofULong)
c5250 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a ;. n += siz
c5260 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a eofULong;. }.
c5270 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c if( sizeofUL
c5280 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 ong <= nBuf - n
c5290 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 ){. memcpy(
c52a0 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 &zBuf[n], &ulSys
c52b0 49 6e 66 6f 5b 51 53 56 5f 54 4f 54 41 56 41 49 Info[QSV_TOTAVAI
c52c0 4c 4d 45 4d 20 2d 20 31 5d 2c 20 73 69 7a 65 6f LMEM - 1], sizeo
c52d0 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e fULong);. n
c52e0 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b += sizeofULong;
c52f0 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 . }. }.#endi
c5300 66 0a 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d f.. return n;.}
c5310 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f ../*.** Sleep fo
c5320 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 r a little while
c5330 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d . Return the am
c5340 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 ount of time sle
c5350 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d pt..** The argum
c5360 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 ent is the numbe
c5370 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 r of microsecond
c5380 73 20 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 s we want to sle
c5390 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 ep..** The retur
c53a0 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e n value is the n
c53b0 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 umber of microse
c53c0 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 conds of sleep a
c53d0 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 ctually.** reque
c53e0 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e sted from the un
c53f0 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 derlying operati
c5400 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d ng system, a num
c5410 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 ber which.** mig
c5420 68 74 20 62 65 20 67 72 65 61 74 65 72 20 74 68 ht be greater th
c5430 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 an or equal to t
c5440 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 he argument, but
c5450 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 not less.** tha
c5460 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a n the argument..
c5470 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
c5480 32 53 6c 65 65 70 28 20 73 71 6c 69 74 65 33 5f 2Sleep( sqlite3_
c5490 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d vfs *pVfs, int m
c54a0 69 63 72 6f 73 65 63 20 29 7b 0a 20 20 44 6f 73 icrosec ){. Dos
c54b0 53 6c 65 65 70 28 20 28 6d 69 63 72 6f 73 65 63 Sleep( (microsec
c54c0 2f 31 30 30 30 29 20 29 3b 0a 20 20 72 65 74 75 /1000) );. retu
c54d0 72 6e 20 6d 69 63 72 6f 73 65 63 3b 0a 7d 0a 0a rn microsec;.}..
c54e0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
c54f0 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 ing variable, if
c5500 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 set to a non-ze
c5510 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65 ro value, become
c5520 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 s the result.**
c5530 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 71 returned from sq
c5540 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 lite3OsCurrentTi
c5550 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73 20 75 me(). This is u
c5560 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e sed for testing.
c5570 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
c5580 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 E_TEST.SQLITE_AP
c5590 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 I int sqlite3_cu
c55a0 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a rrent_time = 0;.
c55b0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 #endif../*.** Fi
c55c0 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 nd the current t
c55d0 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 ime (in Universa
c55e0 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 l Coordinated Ti
c55f0 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a me). Write the.
c5600 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 ** current time
c5610 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 and date as a Ju
c5620 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 lian Day number
c5630 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a into *prNow and.
c5640 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 ** return 0. Re
c5650 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 turn 1 if the ti
c5660 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e me and date cann
c5670 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a ot be found..*/.
c5680 69 6e 74 20 6f 73 32 43 75 72 72 65 6e 74 54 69 int os2CurrentTi
c5690 6d 65 28 20 73 71 6c 69 74 65 33 5f 76 66 73 20 me( sqlite3_vfs
c56a0 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 *pVfs, double *p
c56b0 72 4e 6f 77 20 29 7b 0a 20 20 64 6f 75 62 6c 65 rNow ){. double
c56c0 20 6e 6f 77 3b 0a 20 20 53 48 4f 52 54 20 6d 69 now;. SHORT mi
c56d0 6e 75 74 65 3b 20 2f 2a 20 6e 65 65 64 73 20 74 nute; /* needs t
c56e0 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 63 6f 70 o be able to cop
c56f0 65 20 77 69 74 68 20 6e 65 67 61 74 69 76 65 20 e with negative
c5700 74 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 timezone offset
c5710 2a 2f 0a 20 20 55 53 48 4f 52 54 20 73 65 63 6f */. USHORT seco
c5720 6e 64 2c 20 68 6f 75 72 2c 0a 20 20 20 20 20 20 nd, hour,.
c5730 20 20 20 64 61 79 2c 20 6d 6f 6e 74 68 2c 20 79 day, month, y
c5740 65 61 72 3b 0a 20 20 44 41 54 45 54 49 4d 45 20 ear;. DATETIME
c5750 64 74 3b 0a 20 20 44 6f 73 47 65 74 44 61 74 65 dt;. DosGetDate
c5760 54 69 6d 65 28 20 26 64 74 20 29 3b 0a 20 20 73 Time( &dt );. s
c5770 65 63 6f 6e 64 20 3d 20 28 55 53 48 4f 52 54 29 econd = (USHORT)
c5780 64 74 2e 73 65 63 6f 6e 64 73 3b 0a 20 20 6d 69 dt.seconds;. mi
c5790 6e 75 74 65 20 3d 20 28 53 48 4f 52 54 29 64 74 nute = (SHORT)dt
c57a0 2e 6d 69 6e 75 74 65 73 20 2b 20 64 74 2e 74 69 .minutes + dt.ti
c57b0 6d 65 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72 20 3d mezone;. hour =
c57c0 20 28 55 53 48 4f 52 54 29 64 74 2e 68 6f 75 72 (USHORT)dt.hour
c57d0 73 3b 0a 20 20 64 61 79 20 3d 20 28 55 53 48 4f s;. day = (USHO
c57e0 52 54 29 64 74 2e 64 61 79 3b 0a 20 20 6d 6f 6e RT)dt.day;. mon
c57f0 74 68 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e th = (USHORT)dt.
c5800 6d 6f 6e 74 68 3b 0a 20 20 79 65 61 72 20 3d 20 month;. year =
c5810 28 55 53 48 4f 52 54 29 64 74 2e 79 65 61 72 3b (USHORT)dt.year;
c5820 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 69 .. /* Calculati
c5830 6f 6e 73 20 66 72 6f 6d 20 68 74 74 70 3a 2f 2f ons from http://
c5840 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c 65 2e www.astro.keele.
c5850 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f ac.uk/~rno/Astro
c5860 6e 6f 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a 20 20 nomy/hjd.html.
c5870 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61 73 http://www.as
c5880 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b 2f tro.keele.ac.uk/
c5890 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f 68 ~rno/Astronomy/h
c58a0 6a 64 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20 2f 2a jd-0.1.c */. /*
c58b0 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65 20 4a Calculate the J
c58c0 75 6c 69 61 6e 20 64 61 79 73 20 2a 2f 0a 20 20 ulian days */.
c58d0 6e 6f 77 20 3d 20 64 61 79 20 2d 20 33 32 30 37 now = day - 3207
c58e0 36 20 2b 0a 20 20 20 20 31 34 36 31 2a 28 79 65 6 +. 1461*(ye
c58f0 61 72 20 2b 20 34 38 30 30 20 2b 20 28 6d 6f 6e ar + 4800 + (mon
c5900 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 34 20 2b th - 14)/12)/4 +
c5910 0a 20 20 20 20 33 36 37 2a 28 6d 6f 6e 74 68 20 . 367*(month
c5920 2d 20 32 20 2d 20 28 6d 6f 6e 74 68 20 2d 20 31 - 2 - (month - 1
c5930 34 29 2f 31 32 2a 31 32 29 2f 31 32 20 2d 0a 20 4)/12*12)/12 -.
c5940 20 20 20 33 2a 28 28 79 65 61 72 20 2b 20 34 39 3*((year + 49
c5950 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31 34 00 + (month - 14
c5960 29 2f 31 32 29 2f 31 30 30 29 2f 34 3b 0a 0a 20 )/12)/100)/4;..
c5970 20 2f 2a 20 41 64 64 20 74 68 65 20 66 72 61 63 /* Add the frac
c5980 74 69 6f 6e 61 6c 20 68 6f 75 72 73 2c 20 6d 69 tional hours, mi
c5990 6e 73 20 61 6e 64 20 73 65 63 6f 6e 64 73 20 2a ns and seconds *
c59a0 2f 0a 20 20 6e 6f 77 20 2b 3d 20 28 68 6f 75 72 /. now += (hour
c59b0 20 2b 20 31 32 2e 30 29 2f 32 34 2e 30 3b 0a 20 + 12.0)/24.0;.
c59c0 20 6e 6f 77 20 2b 3d 20 6d 69 6e 75 74 65 2f 31 now += minute/1
c59d0 34 34 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d 20 440.0;. now +=
c59e0 73 65 63 6f 6e 64 2f 38 36 34 30 30 2e 30 3b 0a second/86400.0;.
c59f0 20 20 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77 3b 0a *prNow = now;.
c5a00 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
c5a10 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 ST. if( sqlite3
c5a20 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b _current_time ){
c5a30 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 . *prNow = sq
c5a40 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 lite3_current_ti
c5a50 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 me/86400.0 + 244
c5a60 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 0587.5;. }.#end
c5a70 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d if. return 0;.}
c5a80 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 ..static int os2
c5a90 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c GetLastError(sql
c5aa0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
c5ab0 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a int nBuf, char *
c5ac0 7a 42 75 66 29 7b 0a 20 20 72 65 74 75 72 6e 20 zBuf){. return
c5ad0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 0;.}../*.** Init
c5ae0 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 ialize and deini
c5af0 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 tialize the oper
c5b00 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 ating system int
c5b10 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 erface..*/.SQLIT
c5b20 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
c5b30 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 3_os_init(void){
c5b40 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 . static sqlite
c5b50 33 5f 76 66 73 20 6f 73 32 56 66 73 20 3d 20 7b 3_vfs os2Vfs = {
c5b60 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 . 1,
c5b70 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 /* iVers
c5b80 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f ion */. sizeo
c5b90 66 28 6f 73 32 46 69 6c 65 29 2c 20 20 20 2f 2a f(os2File), /*
c5ba0 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 szOsFile */.
c5bb0 20 43 43 48 4d 41 58 50 41 54 48 2c 20 20 20 20 CCHMAXPATH,
c5bc0 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d /* mxPathnam
c5bd0 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 e */. 0,
c5be0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 /* p
c5bf0 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 6f 73 32 Next */. "os2
c5c00 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ", /
c5c10 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 * zName */. 0
c5c20 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
c5c30 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f /* pAppData */
c5c40 0a 0a 20 20 20 20 6f 73 32 4f 70 65 6e 2c 20 20 .. os2Open,
c5c50 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 /* xOpe
c5c60 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 65 6c 65 n */. os2Dele
c5c70 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 te, /* x
c5c80 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 6f 73 Delete */. os
c5c90 32 41 63 63 65 73 73 2c 20 20 20 20 20 20 20 20 2Access,
c5ca0 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 /* xAccess */.
c5cb0 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 os2FullPathna
c5cc0 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 me, /* xFullPa
c5cd0 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 thname */. os
c5ce0 32 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 2DlOpen,
c5cf0 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 /* xDlOpen */.
c5d00 20 20 20 6f 73 32 44 6c 45 72 72 6f 72 2c 20 20 os2DlError,
c5d10 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f /* xDlErro
c5d20 72 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 53 79 r */. os2DlSy
c5d30 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 m, /* x
c5d40 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 6f 73 32 DlSym */. os2
c5d50 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 DlClose,
c5d60 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 /* xDlClose */.
c5d70 20 20 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 73 73 os2Randomness
c5d80 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d , /* xRandom
c5d90 6e 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73 32 53 ness */. os2S
c5da0 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 2f leep, /
c5db0 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 * xSleep */.
c5dc0 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 os2CurrentTime,
c5dd0 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 /* xCurrentTi
c5de0 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 47 65 74 me */. os2Get
c5df0 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 LastError /*
c5e00 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f xGetLastError */
c5e10 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f . };. sqlite3_
c5e20 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 6f 73 vfs_register(&os
c5e30 32 56 66 73 2c 20 31 29 3b 0a 20 20 69 6e 69 74 2Vfs, 1);. init
c5e40 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a UconvObjects();.
c5e50 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
c5e60 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 OK;.}.SQLITE_API
c5e70 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f int sqlite3_os_
c5e80 65 6e 64 28 76 6f 69 64 29 7b 0a 20 20 66 72 65 end(void){. fre
c5e90 65 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b eUconvObjects();
c5ea0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
c5eb0 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f _OK;.}..#endif /
c5ec0 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 * SQLITE_OS_OS2
c5ed0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
c5ee0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 6f 73 *** End of os_os
c5ef0 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2.c ************
c5f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5f20 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
c5f30 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f *** Begin file o
c5f40 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a s_unix.c *******
c5f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5f70 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d **/./*.** 2004 M
c5f80 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ay 22.**.** The
c5f90 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
c5fa0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
c5fb0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
c5fc0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
c5fd0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
c5fe0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
c5ff0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
c6000 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
c6010 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
c6020 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
c6030 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
c6040 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
c6050 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
c6060 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
c6070 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
c6080 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
c6090 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
c60a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c60b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c60c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c60d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c60e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
c60f0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
c6100 6e 73 20 74 68 65 20 56 46 53 20 69 6d 70 6c 65 ns the VFS imple
c6110 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 75 6e mentation for un
c6120 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 74 69 6e ix-like operatin
c6130 67 20 73 79 73 74 65 6d 73 0a 2a 2a 20 69 6e 63 g systems.** inc
c6140 6c 75 64 65 20 4c 69 6e 75 78 2c 20 4d 61 63 4f lude Linux, MacO
c6150 53 58 2c 20 2a 42 53 44 2c 20 51 4e 58 2c 20 56 SX, *BSD, QNX, V
c6160 78 57 6f 72 6b 73 2c 20 41 49 58 2c 20 48 50 55 xWorks, AIX, HPU
c6170 58 2c 20 61 6e 64 20 6f 74 68 65 72 73 2e 0a 2a X, and others..*
c6180 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 61 *.** There are a
c6190 63 74 75 61 6c 6c 79 20 73 65 76 65 72 61 6c 20 ctually several
c61a0 64 69 66 66 65 72 65 6e 74 20 56 46 53 20 69 6d different VFS im
c61b0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 69 6e plementations in
c61c0 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 54 this file..** T
c61d0 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 20 61 he differences a
c61e0 72 65 20 69 6e 20 74 68 65 20 77 61 79 20 74 68 re in the way th
c61f0 61 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 at file locking
c6200 69 73 20 64 6f 6e 65 2e 20 20 54 68 65 20 64 65 is done. The de
c6210 66 61 75 6c 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 fault.** impleme
c6220 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 50 6f 73 ntation uses Pos
c6230 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b ix Advisory Lock
c6240 73 2e 20 20 41 6c 74 65 72 6e 61 74 69 76 65 20 s. Alternative
c6250 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a implementations.
c6260 2a 2a 20 75 73 65 20 66 6c 6f 63 6b 28 29 2c 20 ** use flock(),
c6270 64 6f 74 2d 66 69 6c 65 73 2c 20 76 61 72 69 6f dot-files, vario
c6280 75 73 20 70 72 6f 70 72 69 65 74 61 72 79 20 6c us proprietary l
c6290 6f 63 6b 69 6e 67 20 73 63 68 65 6d 61 73 2c 20 ocking schemas,
c62a0 6f 72 20 73 69 6d 70 6c 79 0a 2a 2a 20 73 6b 69 or simply.** ski
c62b0 70 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 74 6f p locking all to
c62c0 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 gether..**.** Th
c62d0 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20 69 is source file i
c62e0 73 20 6f 72 67 61 6e 69 7a 65 64 20 69 6e 74 6f s organized into
c62f0 20 64 69 76 69 73 69 6f 6e 73 20 77 68 65 72 65 divisions where
c6300 20 74 68 65 20 6c 6f 67 69 63 20 66 6f 72 20 76 the logic for v
c6310 61 72 69 6f 75 73 0a 2a 2a 20 73 75 62 66 75 6e arious.** subfun
c6320 63 74 69 6f 6e 73 20 69 73 20 63 6f 6e 74 61 69 ctions is contai
c6330 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 61 ned within the a
c6340 70 70 72 6f 70 72 69 61 74 65 20 64 69 76 69 73 ppropriate divis
c6350 69 6f 6e 2e 20 20 50 4c 45 41 53 45 0a 2a 2a 20 ion. PLEASE.**
c6360 4b 45 45 50 20 54 48 45 20 53 54 52 55 43 54 55 KEEP THE STRUCTU
c6370 52 45 20 4f 46 20 54 48 49 53 20 46 49 4c 45 20 RE OF THIS FILE
c6380 49 4e 54 41 43 54 2e 20 20 4e 65 77 20 63 6f 64 INTACT. New cod
c6390 65 20 73 68 6f 75 6c 64 20 62 65 20 70 6c 61 63 e should be plac
c63a0 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 ed.** in the cor
c63b0 72 65 63 74 20 64 69 76 69 73 69 6f 6e 20 61 6e rect division an
c63c0 64 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 65 61 d should be clea
c63d0 72 6c 79 20 6c 61 62 65 6c 65 64 2e 0a 2a 2a 0a rly labeled..**.
c63e0 2a 2a 20 54 68 65 20 6c 61 79 6f 75 74 20 6f 66 ** The layout of
c63f0 20 64 69 76 69 73 69 6f 6e 73 20 69 73 20 61 73 divisions is as
c6400 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 follows:.**.**
c6410 20 20 2a 20 20 47 65 6e 65 72 61 6c 2d 70 75 72 * General-pur
c6420 70 6f 73 65 20 64 65 63 6c 61 72 61 74 69 6f 6e pose declaration
c6430 73 20 61 6e 64 20 75 74 69 6c 69 74 79 20 66 75 s and utility fu
c6440 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a 20 nctions..** *
c6450 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 Unique file ID
c6460 6c 6f 67 69 63 20 75 73 65 64 20 62 79 20 56 78 logic used by Vx
c6470 57 6f 72 6b 73 2e 0a 2a 2a 20 20 20 2a 20 20 56 Works..** * V
c6480 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 70 arious locking p
c6490 72 69 6d 69 74 69 76 65 20 69 6d 70 6c 65 6d 65 rimitive impleme
c64a0 6e 74 61 74 69 6f 6e 73 20 28 61 6c 6c 20 65 78 ntations (all ex
c64b0 63 65 70 74 20 70 72 6f 78 79 20 6c 6f 63 6b 69 cept proxy locki
c64c0 6e 67 29 3a 0a 2a 2a 20 20 20 20 20 20 2b 20 66 ng):.** + f
c64d0 6f 72 20 50 6f 73 69 78 20 41 64 76 69 73 6f 72 or Posix Advisor
c64e0 79 20 4c 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 20 y Locks.**
c64f0 2b 20 66 6f 72 20 6e 6f 2d 6f 70 20 6c 6f 63 6b + for no-op lock
c6500 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 s.** + for
c6510 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 0a 2a dot-file locks.*
c6520 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 66 6c 6f * + for flo
c6530 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 ck() locking.**
c6540 20 20 20 20 20 2b 20 66 6f 72 20 6e 61 6d 65 64 + for named
c6550 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73 semaphore locks
c6560 20 28 56 78 57 6f 72 6b 73 20 6f 6e 6c 79 29 0a (VxWorks only).
c6570 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 41 46 ** + for AF
c6580 50 20 66 69 6c 65 73 79 73 74 65 6d 20 6c 6f 63 P filesystem loc
c6590 6b 73 20 28 4d 61 63 4f 53 58 20 6f 6e 6c 79 29 ks (MacOSX only)
c65a0 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 .** * sqlite3
c65b0 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 6e 6f _file methods no
c65c0 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 t associated wit
c65d0 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 20 h locking..**
c65e0 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f * Definitions o
c65f0 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 f sqlite3_io_met
c6600 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 6f 72 hods objects for
c6610 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 all locking.**
c6620 20 20 20 20 20 6d 65 74 68 6f 64 73 20 70 6c 75 methods plu
c6630 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 s "finder" funct
c6640 69 6f 6e 73 20 66 6f 72 20 65 61 63 68 20 6c 6f ions for each lo
c6650 63 6b 69 6e 67 20 6d 65 74 68 6f 64 2e 0a 2a 2a cking method..**
c6660 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f 76 66 * sqlite3_vf
c6670 73 20 6d 65 74 68 6f 64 20 69 6d 70 6c 65 6d 65 s method impleme
c6680 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a ntations..** *
c6690 20 20 4c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 Locking primit
c66a0 69 76 65 73 20 66 6f 72 20 74 68 65 20 70 72 6f ives for the pro
c66b0 78 79 20 75 62 65 72 2d 6c 6f 63 6b 69 6e 67 2d xy uber-locking-
c66c0 6d 65 74 68 6f 64 2e 20 28 4d 61 63 4f 53 58 20 method. (MacOSX
c66d0 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 20 44 65 only).** * De
c66e0 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71 6c finitions of sql
c66f0 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 ite3_vfs objects
c6700 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 for all locking
c6710 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 20 20 20 20 methods.**
c6720 20 70 6c 75 73 20 69 6d 70 6c 65 6d 65 6e 74 61 plus implementa
c6730 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 tions of sqlite3
c6740 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 _os_init() and s
c6750 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e qlite3_os_end().
c6760 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f .*/.#if SQLITE_O
c6770 53 5f 55 4e 49 58 20 20 20 20 20 20 20 20 20 20 S_UNIX
c6780 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65 /* This file
c6790 20 69 73 20 75 73 65 64 20 6f 6e 20 75 6e 69 78 is used on unix
c67a0 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 only */../*.**
c67b0 54 68 65 72 65 20 61 72 65 20 76 61 72 69 6f 75 There are variou
c67c0 73 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 66 69 s methods for fi
c67d0 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 64 20 le locking used
c67e0 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 63 79 0a for concurrency.
c67f0 2a 2a 20 63 6f 6e 74 72 6f 6c 3a 0a 2a 2a 0a 2a ** control:.**.*
c6800 2a 20 20 20 31 2e 20 50 4f 53 49 58 20 6c 6f 63 * 1. POSIX loc
c6810 6b 69 6e 67 20 28 74 68 65 20 64 65 66 61 75 6c king (the defaul
c6820 74 29 2c 0a 2a 2a 20 20 20 32 2e 20 4e 6f 20 6c t),.** 2. No l
c6830 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 33 2e 20 ocking,.** 3.
c6840 44 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 Dot-file locking
c6850 2c 0a 2a 2a 20 20 20 34 2e 20 66 6c 6f 63 6b 28 ,.** 4. flock(
c6860 29 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 ) locking,.**
c6870 35 2e 20 41 46 50 20 6c 6f 63 6b 69 6e 67 20 28 5. AFP locking (
c6880 4f 53 58 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 20 OSX only),.**
c6890 36 2e 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 73 6. Named POSIX s
c68a0 65 6d 61 70 68 6f 72 65 73 20 28 56 58 57 6f 72 emaphores (VXWor
c68b0 6b 73 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 20 37 ks only),.** 7
c68c0 2e 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e . proxy locking.
c68d0 20 28 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a 0a 2a (OSX only).**.*
c68e0 2a 20 53 74 79 6c 65 73 20 34 2c 20 35 2c 20 61 * Styles 4, 5, a
c68f0 6e 64 20 37 20 61 72 65 20 6f 6e 6c 79 20 61 76 nd 7 are only av
c6900 61 69 6c 61 62 6c 65 20 6f 66 20 53 51 4c 49 54 ailable of SQLIT
c6910 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
c6920 5f 53 54 59 4c 45 0a 2a 2a 20 69 73 20 64 65 66 _STYLE.** is def
c6930 69 6e 65 64 20 74 6f 20 31 2e 20 20 54 68 65 20 ined to 1. The
c6940 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
c6950 43 4b 49 4e 47 5f 53 54 59 4c 45 20 61 6c 73 6f CKING_STYLE also
c6960 20 65 6e 61 62 6c 65 73 20 61 75 74 6f 6d 61 74 enables automat
c6970 69 63 0a 2a 2a 20 73 65 6c 65 63 74 69 6f 6e 20 ic.** selection
c6980 6f 66 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 of the appropria
c6990 74 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 te locking style
c69a0 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 66 69 based on the fi
c69b0 6c 65 73 79 73 74 65 6d 0a 2a 2a 20 77 68 65 72 lesystem.** wher
c69c0 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 e the database i
c69d0 73 20 6c 6f 63 61 74 65 64 2e 20 20 0a 2a 2f 0a s located. .*/.
c69e0 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
c69f0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
c6a00 4e 47 5f 53 54 59 4c 45 29 0a 23 20 20 69 66 20 NG_STYLE).# if
c6a10 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f defined(__APPLE_
c6a20 5f 29 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 _).# define S
c6a30 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
c6a40 4b 49 4e 47 5f 53 54 59 4c 45 20 31 0a 23 20 20 KING_STYLE 1.#
c6a50 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65 else.# define
c6a60 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
c6a70 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 30 0a 23 OCKING_STYLE 0.#
c6a80 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a endif.#endif..
c6a90 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68 65 /*.** Define the
c6aa0 20 4f 53 5f 56 58 57 4f 52 4b 53 20 70 72 65 2d OS_VXWORKS pre-
c6ab0 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 processor macro
c6ac0 74 6f 20 31 20 69 66 20 62 75 69 6c 64 69 6e 67 to 1 if building
c6ad0 20 6f 6e 20 0a 2a 2a 20 76 78 77 6f 72 6b 73 2c on .** vxworks,
c6ae0 20 6f 72 20 30 20 6f 74 68 65 72 77 69 73 65 2e or 0 otherwise.
c6af0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4f 53 5f 56 .*/.#ifndef OS_V
c6b00 58 57 4f 52 4b 53 0a 23 20 20 69 66 20 64 65 66 XWORKS.# if def
c6b10 69 6e 65 64 28 5f 5f 52 54 50 5f 5f 29 20 7c 7c ined(__RTP__) ||
c6b20 20 64 65 66 69 6e 65 64 28 5f 57 52 53 5f 4b 45 defined(_WRS_KE
c6b30 52 4e 45 4c 29 0a 23 20 20 20 20 64 65 66 69 6e RNEL).# defin
c6b40 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 31 0a 23 e OS_VXWORKS 1.#
c6b50 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 else.# defi
c6b60 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 30 0a ne OS_VXWORKS 0.
c6b70 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a # endif.#endif.
c6b80 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 65 ./*.** These #de
c6b90 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e 61 fines should ena
c6ba0 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73 75 ble >2GB file su
c6bb0 70 70 6f 72 74 20 6f 6e 20 50 6f 73 69 78 20 69 pport on Posix i
c6bc0 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 f the.** underly
c6bd0 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 ing operating sy
c6be0 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69 74 stem supports it
c6bf0 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61 63 . If the OS lac
c6c00 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c 65 ks.** large file
c6c10 20 73 75 70 70 6f 72 74 2c 20 74 68 65 73 65 20 support, these
c6c20 73 68 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f 70 73 should be no-ops
c6c30 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20 66 69 ..**.** Large fi
c6c40 6c 65 20 73 75 70 70 6f 72 74 20 63 61 6e 20 62 le support can b
c6c50 65 20 64 69 73 61 62 6c 65 64 20 75 73 69 6e 67 e disabled using
c6c60 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 44 49 the -DSQLITE_DI
c6c70 53 41 42 4c 45 5f 4c 46 53 20 73 77 69 74 63 68 SABLE_LFS switch
c6c80 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 .** on the compi
c6c90 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 ler command line
c6ca0 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63 65 73 . This is neces
c6cb0 73 61 72 79 20 69 66 20 79 6f 75 20 61 72 65 20 sary if you are
c6cc0 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f 6e 20 compiling.** on
c6cd0 61 20 72 65 63 65 6e 74 20 6d 61 63 68 69 6e 65 a recent machine
c6ce0 20 28 65 78 3a 20 52 65 64 48 61 74 20 37 2e 32 (ex: RedHat 7.2
c6cf0 29 20 62 75 74 20 79 6f 75 20 77 61 6e 74 20 79 ) but you want y
c6d00 6f 75 72 20 63 6f 64 65 20 74 6f 20 77 6f 72 6b our code to work
c6d10 0a 2a 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65 72 20 .** on an older
c6d20 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 machine (ex: Red
c6d30 48 61 74 20 36 2e 30 29 2e 20 20 49 66 20 79 6f Hat 6.0). If yo
c6d40 75 20 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 65 64 u compile on Red
c6d50 48 61 74 20 37 2e 32 0a 2a 2a 20 77 69 74 68 6f Hat 7.2.** witho
c6d60 75 74 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 ut this option,
c6d70 4c 46 53 20 69 73 20 65 6e 61 62 6c 65 2e 20 20 LFS is enable.
c6d80 42 75 74 20 4c 46 53 20 64 6f 65 73 20 6e 6f 74 But LFS does not
c6d90 20 65 78 69 73 74 20 69 6e 20 74 68 65 20 6b 65 exist in the ke
c6da0 72 6e 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 48 61 rnel.** in RedHa
c6db0 74 20 36 2e 30 2c 20 73 6f 20 74 68 65 20 63 6f t 6.0, so the co
c6dc0 64 65 20 77 6f 6e 27 74 20 77 6f 72 6b 2e 20 20 de won't work.
c6dd0 48 65 6e 63 65 2c 20 66 6f 72 20 6d 61 78 69 6d Hence, for maxim
c6de0 75 6d 20 62 69 6e 61 72 79 0a 2a 2a 20 70 6f 72 um binary.** por
c6df0 74 61 62 69 6c 69 74 79 20 79 6f 75 20 73 68 6f tability you sho
c6e00 75 6c 64 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a 2a uld omit LFS..**
c6e10 0a 2a 2a 20 54 68 65 20 70 72 65 76 69 6f 75 73 .** The previous
c6e20 20 70 61 72 61 67 72 61 70 68 20 77 61 73 20 77 paragraph was w
c6e30 72 69 74 74 65 6e 20 69 6e 20 32 30 30 35 2e 20 ritten in 2005.
c6e40 20 28 54 68 69 73 20 70 61 72 61 67 72 61 70 68 (This paragraph
c6e50 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 6f is written.** o
c6e60 6e 20 32 30 30 38 2d 31 31 2d 32 38 2e 29 20 54 n 2008-11-28.) T
c6e70 68 65 73 65 20 64 61 79 73 2c 20 61 6c 6c 20 4c hese days, all L
c6e80 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 73 75 70 inux kernels sup
c6e90 70 6f 72 74 20 6c 61 72 67 65 20 66 69 6c 65 73 port large files
c6ea0 2c 20 73 6f 0a 2a 2a 20 79 6f 75 20 73 68 6f 75 , so.** you shou
c6eb0 6c 64 20 70 72 6f 62 61 62 6c 79 20 6c 65 61 76 ld probably leav
c6ec0 65 20 4c 46 53 20 65 6e 61 62 6c 65 64 2e 20 20 e LFS enabled.
c6ed0 42 75 74 20 73 6f 6d 65 20 65 6d 62 65 64 64 65 But some embedde
c6ee0 64 20 70 6c 61 74 66 6f 72 6d 73 20 6d 69 67 68 d platforms migh
c6ef0 74 0a 2a 2a 20 6c 61 63 6b 20 4c 46 53 20 69 6e t.** lack LFS in
c6f00 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20 which case the
c6f10 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c SQLITE_DISABLE_L
c6f20 46 53 20 6d 61 63 72 6f 20 6d 69 67 68 74 20 73 FS macro might s
c6f30 74 69 6c 6c 20 62 65 20 75 73 65 66 75 6c 2e 0a till be useful..
c6f40 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
c6f50 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a 23 20 E_DISABLE_LFS.#
c6f60 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 5f 46 49 define _LARGE_FI
c6f70 4c 45 20 20 20 20 20 20 20 31 0a 23 20 69 66 6e LE 1.# ifn
c6f80 64 65 66 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 def _FILE_OFFSET
c6f90 5f 42 49 54 53 0a 23 20 20 20 64 65 66 69 6e 65 _BITS.# define
c6fa0 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 _FILE_OFFSET_BI
c6fb0 54 53 20 36 34 0a 23 20 65 6e 64 69 66 0a 23 20 TS 64.# endif.#
c6fc0 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 46 49 4c define _LARGEFIL
c6fd0 45 5f 53 4f 55 52 43 45 20 31 0a 23 65 6e 64 69 E_SOURCE 1.#endi
c6fe0 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61 6e 64 61 72 f../*.** standar
c6ff0 64 20 69 6e 63 6c 75 64 65 20 66 69 6c 65 73 2e d include files.
c7000 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 .*/.#include <sy
c7010 73 2f 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c s/types.h>.#incl
c7020 75 64 65 20 3c 73 79 73 2f 73 74 61 74 2e 68 3e ude <sys/stat.h>
c7030 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e 74 6c .#include <fcntl
c7040 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e .h>.#include <un
c7050 69 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 istd.h>.#include
c7060 20 3c 73 79 73 2f 74 69 6d 65 2e 68 3e 0a 23 69 <sys/time.h>.#i
c7070 6e 63 6c 75 64 65 20 3c 65 72 72 6e 6f 2e 68 3e nclude <errno.h>
c7080 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 ..#if SQLITE_ENA
c7090 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
c70a0 45 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 E.# include <sys
c70b0 2f 69 6f 63 74 6c 2e 68 3e 0a 23 20 69 66 20 4f /ioctl.h>.# if O
c70c0 53 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69 6e 63 S_VXWORKS.# inc
c70d0 6c 75 64 65 20 3c 73 65 6d 61 70 68 6f 72 65 2e lude <semaphore.
c70e0 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6c h>.# include <l
c70f0 69 6d 69 74 73 2e 68 3e 0a 23 20 65 6c 73 65 0a imits.h>.# else.
c7100 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f # include <sys/
c7110 66 69 6c 65 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 file.h>.# inclu
c7120 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e 68 3e de <sys/param.h>
c7130 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 .# include <sys
c7140 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 20 65 6e 64 69 /mount.h>.# endi
c7150 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 f.#endif /* SQLI
c7160 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
c7170 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a G_STYLE */../*.*
c7180 2a 20 49 66 20 77 65 20 61 72 65 20 74 6f 20 62 * If we are to b
c7190 65 20 74 68 72 65 61 64 2d 73 61 66 65 2c 20 69 e thread-safe, i
c71a0 6e 63 6c 75 64 65 20 74 68 65 20 70 74 68 72 65 nclude the pthre
c71b0 61 64 73 20 68 65 61 64 65 72 20 61 6e 64 20 64 ads header and d
c71c0 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53 51 4c efine.** the SQL
c71d0 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 53 ITE_UNIX_THREADS
c71e0 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 20 53 macro..*/.#if S
c71f0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
c7200 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
c7210 5f 55 4e 49 58 5f 54 48 52 45 41 44 53 20 31 0a _UNIX_THREADS 1.
c7220 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 #endif../*.** De
c7230 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69 6f 6e fault permission
c7240 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20 s when creating
c7250 61 20 6e 65 77 20 66 69 6c 65 0a 2a 2f 0a 23 69 a new file.*/.#i
c7260 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 fndef SQLITE_DEF
c7270 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 AULT_FILE_PERMIS
c7280 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20 53 SIONS.# define S
c7290 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 QLITE_DEFAULT_FI
c72a0 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 20 30 LE_PERMISSIONS 0
c72b0 36 34 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 20 644.#endif../*.
c72c0 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 72 6d 69 ** Default permi
c72d0 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 72 65 61 ssions when crea
c72e0 74 69 6e 67 20 61 75 74 6f 20 70 72 6f 78 79 20 ting auto proxy
c72f0 64 69 72 0a 20 2a 2f 0a 23 69 66 6e 64 65 66 20 dir. */.#ifndef
c7300 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 SQLITE_DEFAULT_P
c7310 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 ROXYDIR_PERMISSI
c7320 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20 53 51 4c ONS.# define SQL
c7330 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 ITE_DEFAULT_PROX
c7340 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 YDIR_PERMISSIONS
c7350 20 30 37 35 35 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0755.#endif../*
c7360 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 75 70 70 .** Maximum supp
c7370 6f 72 74 65 64 20 70 61 74 68 2d 6c 65 6e 67 74 orted path-lengt
c7380 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 h..*/.#define MA
c7390 58 5f 50 41 54 48 4e 41 4d 45 20 35 31 32 0a 0a X_PATHNAME 512..
c73a0 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20 73 65 74 20 74 /*.** Only set t
c73b0 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 69 66 20 he lastErrno if
c73c0 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 the error code i
c73d0 73 20 61 20 72 65 61 6c 20 65 72 72 6f 72 20 61 s a real error a
c73e0 6e 64 20 6e 6f 74 20 0a 2a 2a 20 61 20 6e 6f 72 nd not .** a nor
c73f0 6d 61 6c 20 65 78 70 65 63 74 65 64 20 72 65 74 mal expected ret
c7400 75 72 6e 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 urn code of SQLI
c7410 54 45 5f 42 55 53 59 20 6f 72 20 53 51 4c 49 54 TE_BUSY or SQLIT
c7420 45 5f 4f 4b 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 E_OK.*/.#define
c7430 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 78 29 IS_LOCK_ERROR(x)
c7440 20 20 28 28 78 20 21 3d 20 53 51 4c 49 54 45 5f ((x != SQLITE_
c7450 4f 4b 29 20 26 26 20 28 78 20 21 3d 20 53 51 4c OK) && (x != SQL
c7460 49 54 45 5f 42 55 53 59 29 29 0a 0a 0a 2f 2a 0a ITE_BUSY)).../*.
c7470 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 2c 20 61 66 ** Sometimes, af
c7480 74 65 72 20 61 20 66 69 6c 65 20 68 61 6e 64 6c ter a file handl
c7490 65 20 69 73 20 63 6c 6f 73 65 64 20 62 79 20 53 e is closed by S
c74a0 51 4c 69 74 65 2c 20 74 68 65 20 66 69 6c 65 20 QLite, the file
c74b0 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a 20 63 61 descriptor.** ca
c74c0 6e 6e 6f 74 20 62 65 20 63 6c 6f 73 65 64 20 69 nnot be closed i
c74d0 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 6e 20 74 mmediately. In t
c74e0 68 65 73 65 20 63 61 73 65 73 2c 20 69 6e 73 74 hese cases, inst
c74f0 61 6e 63 65 73 20 6f 66 20 74 68 65 20 66 6f 6c ances of the fol
c7500 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 lowing.** struct
c7510 75 72 65 20 61 72 65 20 75 73 65 64 20 74 6f 20 ure are used to
c7520 73 74 6f 72 65 20 74 68 65 20 66 69 6c 65 20 64 store the file d
c7530 65 73 63 72 69 70 74 6f 72 20 77 68 69 6c 65 20 escriptor while
c7540 77 61 69 74 69 6e 67 20 66 6f 72 20 61 6e 0a 2a waiting for an.*
c7550 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f * opportunity to
c7560 20 65 69 74 68 65 72 20 63 6c 6f 73 65 20 6f 72 either close or
c7570 20 72 65 75 73 65 20 69 74 2e 0a 2a 2f 0a 74 79 reuse it..*/.ty
c7580 70 65 64 65 66 20 73 74 72 75 63 74 20 55 6e 69 pedef struct Uni
c7590 78 55 6e 75 73 65 64 46 64 20 55 6e 69 78 55 6e xUnusedFd UnixUn
c75a0 75 73 65 64 46 64 3b 0a 73 74 72 75 63 74 20 55 usedFd;.struct U
c75b0 6e 69 78 55 6e 75 73 65 64 46 64 20 7b 0a 20 20 nixUnusedFd {.
c75c0 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 int fd;
c75d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c /* Fil
c75e0 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 e descriptor to
c75f0 63 6c 6f 73 65 20 2a 2f 0a 20 20 69 6e 74 20 66 close */. int f
c7600 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 lags;
c7610 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 74 68 /* Flags th
c7620 69 73 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 is file descript
c7630 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 77 69 or was opened wi
c7640 74 68 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 73 th */. UnixUnus
c7650 65 64 46 64 20 2a 70 4e 65 78 74 3b 20 20 20 20 edFd *pNext;
c7660 20 20 2f 2a 20 4e 65 78 74 20 75 6e 75 73 65 64 /* Next unused
c7670 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
c7680 20 6f 6e 20 73 61 6d 65 20 66 69 6c 65 20 2a 2f on same file */
c7690 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 75 .};../*.** The u
c76a0 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 nixFile structur
c76b0 65 20 69 73 20 73 75 62 63 6c 61 73 73 20 6f 66 e is subclass of
c76c0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 70 sqlite3_file sp
c76d0 65 63 69 66 69 63 20 74 6f 20 74 68 65 20 75 6e ecific to the un
c76e0 69 78 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d ix.** VFS implem
c76f0 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 entations..*/.ty
c7700 70 65 64 65 66 20 73 74 72 75 63 74 20 75 6e 69 pedef struct uni
c7710 78 46 69 6c 65 20 75 6e 69 78 46 69 6c 65 3b 0a xFile unixFile;.
c7720 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 20 struct unixFile
c7730 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d {. sqlite3_io_m
c7740 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 4d ethods const *pM
c7750 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79 ethod; /* Alway
c7760 73 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 s the first entr
c7770 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e y */. struct un
c7780 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e ixOpenCnt *pOpen
c7790 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 ; /* Info
c77a0 61 62 6f 75 74 20 61 6c 6c 20 6f 70 65 6e 20 66 about all open f
c77b0 64 27 73 20 6f 6e 20 74 68 69 73 20 69 6e 6f 64 d's on this inod
c77c0 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e e */. struct un
c77d0 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 ixLockInfo *pLoc
c77e0 6b 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 k; /* Info
c77f0 61 62 6f 75 74 20 6c 6f 63 6b 73 20 6f 6e 20 74 about locks on t
c7800 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 20 69 his inode */. i
c7810 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 nt h;
c7820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7830 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 /* The file desc
c7840 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 riptor */. int
c7850 64 69 72 66 64 3b 20 20 20 20 20 20 20 20 20 20 dirfd;
c7860 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c7870 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 File descriptor
c7880 66 6f 72 20 74 68 65 20 64 69 72 65 63 74 6f 72 for the director
c7890 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 y */. unsigned
c78a0 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20 20 char locktype;
c78b0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
c78c0 79 70 65 20 6f 66 20 6c 6f 63 6b 20 68 65 6c 64 ype of lock held
c78d0 20 6f 6e 20 74 68 69 73 20 66 64 20 2a 2f 0a 20 on this fd */.
c78e0 20 69 6e 74 20 6c 61 73 74 45 72 72 6e 6f 3b 20 int lastErrno;
c78f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7900 20 20 2f 2a 20 54 68 65 20 75 6e 69 78 20 65 72 /* The unix er
c7910 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c 61 73 rno from the las
c7920 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 20 t I/O error */.
c7930 20 76 6f 69 64 20 2a 6c 6f 63 6b 69 6e 67 43 6f void *lockingCo
c7940 6e 74 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 ntext;
c7950 20 20 2f 2a 20 4c 6f 63 6b 69 6e 67 20 73 74 79 /* Locking sty
c7960 6c 65 20 73 70 65 63 69 66 69 63 20 73 74 61 74 le specific stat
c7970 65 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 73 65 e */. UnixUnuse
c7980 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20 20 20 dFd *pUnused;
c7990 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 2d 61 /* Pre-a
c79a0 6c 6c 6f 63 61 74 65 64 20 55 6e 69 78 55 6e 75 llocated UnixUnu
c79b0 73 65 64 46 64 20 2a 2f 0a 20 20 69 6e 74 20 66 sedFd */. int f
c79c0 69 6c 65 46 6c 61 67 73 3b 20 20 20 20 20 20 20 ileFlags;
c79d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
c79e0 69 73 63 65 6c 6c 61 6e 6f 75 73 20 66 6c 61 67 iscellanous flag
c79f0 73 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f s */.#if SQLITE_
c7a00 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
c7a10 54 59 4c 45 0a 20 20 69 6e 74 20 6f 70 65 6e 46 TYLE. int openF
c7a20 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 lags;
c7a30 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 /* The f
c7a40 6c 61 67 73 20 73 70 65 63 69 66 69 65 64 20 61 lags specified a
c7a50 74 20 6f 70 65 6e 28 29 20 2a 2f 0a 23 65 6e 64 t open() */.#end
c7a60 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 if.#if SQLITE_TH
c7a70 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 READSAFE && defi
c7a80 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 ned(__linux__).
c7a90 20 70 74 68 72 65 61 64 5f 74 20 74 69 64 3b 20 pthread_t tid;
c7aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7ab0 20 20 2f 2a 20 54 68 65 20 74 68 72 65 61 64 20 /* The thread
c7ac0 74 68 61 74 20 22 6f 77 6e 73 22 20 74 68 69 73 that "owns" this
c7ad0 20 75 6e 69 78 46 69 6c 65 20 2a 2f 0a 23 65 6e unixFile */.#en
c7ae0 64 69 66 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 dif.#if OS_VXWOR
c7af0 4b 53 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74 KS. int isDelet
c7b00 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
c7b10 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 /* Delete
c7b20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74 72 75 65 on close if true
c7b30 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76 78 77 */. struct vxw
c7b40 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 64 3b orksFileId *pId;
c7b50 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 71 75 65 /* Unique
c7b60 20 66 69 6c 65 20 49 44 20 2a 2f 0a 23 65 6e 64 file ID */.#end
c7b70 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 if.#ifndef NDEBU
c7b80 47 0a 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 G. /* The next
c7b90 67 72 6f 75 70 20 6f 66 20 76 61 72 69 61 62 6c group of variabl
c7ba0 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 74 es are used to t
c7bb0 72 61 63 6b 20 77 68 65 74 68 65 72 20 6f 72 20 rack whether or
c7bc0 6e 6f 74 20 74 68 65 0a 20 20 2a 2a 20 74 72 61 not the. ** tra
c7bd0 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 nsaction counter
c7be0 20 69 6e 20 62 79 74 65 73 20 32 34 2d 32 37 20 in bytes 24-27
c7bf0 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 of database file
c7c00 73 20 61 72 65 20 75 70 64 61 74 65 64 0a 20 20 s are updated.
c7c10 2a 2a 20 77 68 65 6e 65 76 65 72 20 61 6e 79 20 ** whenever any
c7c20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 part of the data
c7c30 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20 20 41 base changes. A
c7c40 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c n assertion faul
c7c50 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 6f 63 63 75 t will. ** occu
c7c60 72 20 69 66 20 61 20 66 69 6c 65 20 69 73 20 75 r if a file is u
c7c70 70 64 61 74 65 64 20 77 69 74 68 6f 75 74 20 61 pdated without a
c7c80 6c 73 6f 20 75 70 64 61 74 69 6e 67 20 74 68 65 lso updating the
c7c90 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a transaction. *
c7ca0 2a 20 63 6f 75 6e 74 65 72 2e 20 20 54 68 69 73 * counter. This
c7cb0 20 74 65 73 74 20 69 73 20 6d 61 64 65 20 74 6f test is made to
c7cc0 20 61 76 6f 69 64 20 6e 65 77 20 70 72 6f 62 6c avoid new probl
c7cd0 65 6d 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 ems similar to t
c7ce0 68 65 0a 20 20 2a 2a 20 6f 6e 65 20 64 65 73 63 he. ** one desc
c7cf0 72 69 62 65 64 20 62 79 20 74 69 63 6b 65 74 20 ribed by ticket
c7d00 23 33 35 38 34 2e 20 0a 20 20 2a 2f 0a 20 20 75 #3584. . */. u
c7d10 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 72 61 nsigned char tra
c7d20 6e 73 43 6e 74 72 43 68 6e 67 3b 20 20 20 2f 2a nsCntrChng; /*
c7d30 20 54 72 75 65 20 69 66 20 74 68 65 20 74 72 61 True if the tra
c7d40 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 nsaction counter
c7d50 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 6e changed */. un
c7d60 73 69 67 6e 65 64 20 63 68 61 72 20 64 62 55 70 signed char dbUp
c7d70 64 61 74 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 date; /*
c7d80 54 72 75 65 20 69 66 20 61 6e 79 20 70 61 72 74 True if any part
c7d90 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c of database fil
c7da0 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 e changed */. u
c7db0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 6e 4e nsigned char inN
c7dc0 6f 72 6d 61 6c 57 72 69 74 65 3b 20 20 20 2f 2a ormalWrite; /*
c7dd0 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 6e 6f True if in a no
c7de0 72 6d 61 6c 20 77 72 69 74 65 20 6f 70 65 72 61 rmal write opera
c7df0 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 tion */.#endif.#
c7e00 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
c7e10 54 0a 20 20 2f 2a 20 49 6e 20 74 65 73 74 20 6d T. /* In test m
c7e20 6f 64 65 2c 20 69 6e 63 72 65 61 73 65 20 74 68 ode, increase th
c7e30 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73 20 73 e size of this s
c7e40 74 72 75 63 74 75 72 65 20 61 20 62 69 74 20 73 tructure a bit s
c7e50 6f 20 74 68 61 74 20 0a 20 20 2a 2a 20 69 74 20 o that . ** it
c7e60 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 is larger than t
c7e70 68 65 20 73 74 72 75 63 74 20 43 72 61 73 68 46 he struct CrashF
c7e80 69 6c 65 20 64 65 66 69 6e 65 64 20 69 6e 20 74 ile defined in t
c7e90 65 73 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20 20 63 est6.c.. */. c
c7ea0 68 61 72 20 61 50 61 64 64 69 6e 67 5b 33 32 5d har aPadding[32]
c7eb0 3b 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a ;.#endif.};../*.
c7ec0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
c7ed0 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 62 macros define b
c7ee0 69 74 73 20 69 6e 20 75 6e 69 78 46 69 6c 65 2e its in unixFile.
c7ef0 66 69 6c 65 46 6c 61 67 73 0a 2a 2f 0a 23 64 65 fileFlags.*/.#de
c7f00 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 48 4f 4c fine SQLITE_WHOL
c7f10 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 20 E_FILE_LOCKING
c7f20 30 78 30 30 30 31 20 20 20 2f 2a 20 55 73 65 20 0x0001 /* Use
c7f30 77 68 6f 6c 65 2d 66 69 6c 65 20 6c 6f 63 6b 69 whole-file locki
c7f40 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 ng */../*.** Inc
c7f50 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20 69 lude code that i
c7f60 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 s common to all
c7f70 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a os_*.c files.*/.
c7f80 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
c7f90 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f Include os_commo
c7fa0 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c n.h in the middl
c7fb0 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20 2a e of os_unix.c *
c7fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
c7fd0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
c7fe0 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f Begin file os_co
c7ff0 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mmon.h *********
c8000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
c8020 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 /*.** 2004 May 2
c8030 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 2.**.** The auth
c8040 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
c8050 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
c8060 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
c8070 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
c8080 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
c8090 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
c80a0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
c80b0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
c80c0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
c80d0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
c80e0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
c80f0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
c8100 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
c8110 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
c8120 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
c8130 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
c8140 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
c8150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8190 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
c81a0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d file contains m
c81b0 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 acros and a litt
c81c0 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20 74 le bit of code t
c81d0 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f hat is common to
c81e0 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 .** all of the p
c81f0 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 latform-specific
c8200 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 files (os_*.c)
c8210 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65 64 and is #included
c8220 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 into those.** f
c8230 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 iles..**.** This
c8240 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 file should be
c8250 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68 65 #included by the
c8260 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e os_*.c files on
c8270 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 61 ly. It is not a
c8280 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70 .** general purp
c8290 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e ose header file.
c82a0 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 .**.** $Id: os_c
c82b0 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 20 32 ommon.h,v 1.38 2
c82c0 30 30 39 2f 30 32 2f 32 34 20 31 38 3a 34 30 3a 009/02/24 18:40:
c82d0 35 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 50 danielk1977 E
c82e0 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 xp $.*/.#ifndef
c82f0 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 _OS_COMMON_H_.#d
c8300 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e efine _OS_COMMON
c8310 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 _H_../*.** At le
c8320 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 ast two bugs hav
c8330 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 e slipped in bec
c8340 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 ause we changed
c8350 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 the MEMORY_DEBUG
c8360 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c .** macro to SQL
c8370 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f ITE_DEBUG and so
c8380 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c me older makefil
c8390 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 es have not yet
c83a0 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 made the.** swit
c83b0 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ch. The followi
c83c0 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 ng code should c
c83d0 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 atch this proble
c83e0 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d m at compile-tim
c83f0 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d e..*/.#ifdef MEM
c8400 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f ORY_DEBUG.# erro
c8410 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 r "The MEMORY_DE
c8420 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 BUG macro is obs
c8430 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 olete. Use SQLI
c8440 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 TE_DEBUG instead
c8450 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 .".#endif..#ifde
c8460 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 f SQLITE_DEBUG.S
c8470 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
c8480 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 t sqlite3OSTrace
c8490 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 = 0;.#define OS
c84a0 54 52 41 43 45 31 28 58 29 20 20 20 20 20 20 20 TRACE1(X)
c84b0 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 if( sqlite3OST
c84c0 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 race ) sqlite3De
c84d0 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65 bugPrintf(X).#de
c84e0 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c fine OSTRACE2(X,
c84f0 59 29 20 20 20 20 20 20 20 69 66 28 20 73 71 6c Y) if( sql
c8500 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 ite3OSTrace ) sq
c8510 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
c8520 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 (X,Y).#define OS
c8530 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 TRACE3(X,Y,Z)
c8540 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 if( sqlite3OST
c8550 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 race ) sqlite3De
c8560 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 bugPrintf(X,Y,Z)
c8570 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
c8580 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 4(X,Y,Z,A) if(
c8590 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 sqlite3OSTrace
c85a0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
c85b0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 intf(X,Y,Z,A).#d
c85c0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 efine OSTRACE5(X
c85d0 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 ,Y,Z,A,B) if( sq
c85e0 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 lite3OSTrace ) s
c85f0 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
c8600 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 f(X,Y,Z,A,B).#de
c8610 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c fine OSTRACE6(X,
c8620 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 Y,Z,A,B,C) \.
c8630 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 if(sqlite3OSTra
c8640 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 ce) sqlite3Debug
c8650 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 Printf(X,Y,Z,A,B
c8660 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,C).#define OSTR
c8670 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 ACE7(X,Y,Z,A,B,C
c8680 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c ,D) \. if(sql
c8690 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c ite3OSTrace) sql
c86a0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
c86b0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 X,Y,Z,A,B,C,D).#
c86c0 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 else.#define OST
c86d0 52 41 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65 RACE1(X).#define
c86e0 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23 OSTRACE2(X,Y).#
c86f0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 define OSTRACE3(
c8700 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f X,Y,Z).#define O
c8710 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 STRACE4(X,Y,Z,A)
c8720 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
c8730 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 5(X,Y,Z,A,B).#de
c8740 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c fine OSTRACE6(X,
c8750 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 Y,Z,A,B,C).#defi
c8760 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c ne OSTRACE7(X,Y,
c8770 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 Z,A,B,C,D).#endi
c8780 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 f../*.** Macros
c8790 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 for performance
c87a0 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c tracing. Normal
c87b0 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 ly turned off.
c87c0 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e Only works.** on
c87d0 20 69 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a i486 hardware..
c87e0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
c87f0 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 _PERFORMANCE_TRA
c8800 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d CE../* .** hwtim
c8810 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c e.h contains inl
c8820 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f ine assembler co
c8830 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 de for implement
c8840 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 ing .** high-per
c8850 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 formance timing
c8860 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a routines..*/./**
c8870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 ************ Inc
c8880 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e lude hwtime.h in
c8890 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f the middle of o
c88a0 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a s_common.h *****
c88b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
c88c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
c88d0 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 in file hwtime.h
c88e0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
c88f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
c8910 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a ** 2008 May 27.*
c8920 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
c8930 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
c8940 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
c8950 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
c8960 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
c8970 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
c8980 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
c8990 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
c89a0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
c89b0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
c89c0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
c89d0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
c89e0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
c89f0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
c8a00 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
c8a10 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
c8a20 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
c8a30 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
c8a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8a80 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
c8a90 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 le contains inli
c8aa0 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 ne asm code for
c8ab0 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 retrieving "high
c8ac0 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a -performance".**
c8ad0 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 counters for x8
c8ae0 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a 6 class CPUs..**
c8af0 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e .** $Id: hwtime.
c8b00 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f h,v 1.3 2008/08/
c8b10 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e 01 14:33:15 shan
c8b20 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 e Exp $.*/.#ifnd
c8b30 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 ef _HWTIME_H_.#d
c8b40 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f efine _HWTIME_H_
c8b50 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
c8b60 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e owing routine on
c8b70 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 ly works on pent
c8b80 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 ium-class (or ne
c8b90 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e wer) processors.
c8ba0 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 65 20 .** It uses the
c8bb0 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 RDTSC opcode to
c8bc0 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 20 63 read the cycle c
c8bd0 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f ount value out o
c8be0 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 f the.** process
c8bf0 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 or and returns t
c8c00 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73 hat value. This
c8c10 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 can be used for
c8c20 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f high-res.** pro
c8c30 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 filing..*/.#if (
c8c40 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f defined(__GNUC__
c8c50 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 ) || defined(_MS
c8c60 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20 C_VER)) && \.
c8c70 20 20 20 28 64 65 66 69 6e 65 64 28 69 33 38 36 (defined(i386
c8c80 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 ) || defined(__i
c8c90 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 386__) || define
c8ca0 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 d(_M_IX86)).. #
c8cb0 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 if defined(__GNU
c8cc0 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 C__).. __inline
c8cd0 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 __ sqlite_uint64
c8ce0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 sqlite3Hwtime(v
c8cf0 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 oid){. unsig
c8d00 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a ned int lo, hi;.
c8d10 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 __asm__ __v
c8d20 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 olatile__ ("rdts
c8d30 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 c" : "=a" (lo),
c8d40 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20 "=d" (hi));.
c8d50 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f return (sqlite_
c8d60 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 uint64)hi << 32
c8d70 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c | lo;. }.. #el
c8d80 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f if defined(_MSC_
c8d90 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 VER).. __declsp
c8da0 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 ec(naked) __inli
c8db0 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 ne sqlite_uint64
c8dc0 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33 __cdecl sqlite3
c8dd0 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 Hwtime(void){.
c8de0 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 __asm {.
c8df0 20 20 20 72 64 74 73 63 0a 20 20 20 20 20 20 20 rdtsc.
c8e00 20 72 65 74 20 20 20 20 20 20 20 3b 20 72 65 74 ret ; ret
c8e10 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58 urn value at EDX
c8e20 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a :EAX. }. }.
c8e30 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 . #endif..#elif
c8e40 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 (defined(__GNUC
c8e50 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f __) && defined(_
c8e60 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f _x86_64__)).. _
c8e70 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 _inline__ sqlite
c8e80 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 _uint64 sqlite3H
c8e90 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 wtime(void){.
c8ea0 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 unsigned long
c8eb0 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 val;. __as
c8ec0 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f m__ __volatile__
c8ed0 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 ("rdtsc" : "=A"
c8ee0 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 (val));. r
c8ef0 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 eturn val;. }.
c8f00 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 .#elif (defined(
c8f10 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 __GNUC__) && def
c8f20 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a ined(__ppc__))..
c8f30 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c __inline__ sql
c8f40 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 ite_uint64 sqlit
c8f50 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a e3Hwtime(void){.
c8f60 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c unsigned l
c8f70 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b ong long retval;
c8f80 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 . unsigned
c8f90 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 long junk;.
c8fa0 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 __asm__ __volat
c8fb0 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 ile__ ("\n\.
c8fc0 20 20 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66 1: mf
c8fd0 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 tbu %1\n\.
c8fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 mf
c8ff0 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 tb %L0\n\.
c9000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d m
c9010 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 ftbu %0\n\.
c9020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 c
c9030 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a mpw %0,%1\n\.
c9040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9050 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20 bne 1b".
c9060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9070 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c : "=r" (retval),
c9080 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 "=r" (junk));.
c9090 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 return retv
c90a0 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a al;. }..#else..
c90b0 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d #error Need im
c90c0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
c90d0 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 sqlite3Hwtime()
c90e0 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 for your platfor
c90f0 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f m... /*. ** To
c9100 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 compile without
c9110 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 implementing sq
c9120 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f lite3Hwtime() fo
c9130 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c r your platform,
c9140 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 . ** you can re
c9150 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65 20 23 move the above #
c9160 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68 error and use th
c9170 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a e following. **
c9180 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 stub function.
c9190 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 You will lose t
c91a0 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f iming support fo
c91b0 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 r many. ** of t
c91c0 68 65 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 he debugging and
c91d0 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69 testing utiliti
c91e0 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c es, but it shoul
c91f0 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 d at. ** least
c9200 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e compile and run.
c9210 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 . */.SQLITE_PRI
c9220 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 VATE sqlite_ui
c9230 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 nt64 sqlite3Hwti
c9240 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e me(void){ return
c9250 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 ((sqlite_uint64
c9260 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a )0); }..#endif..
c9270 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e #endif /* !defin
c9280 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a ed(_HWTIME_H_) *
c9290 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
c92a0 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 ** End of hwtime
c92b0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
c92c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c92d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c92e0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
c92f0 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 ** Continuing wh
c9300 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 ere we left off
c9310 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a in os_common.h *
c9320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9330 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 */..static sqlit
c9340 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61 72 74 e_uint64 g_start
c9350 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f ;.static sqlite_
c9360 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 64 uint64 g_elapsed
c9370 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f ;.#define TIMER_
c9380 53 54 41 52 54 20 20 20 20 20 20 20 67 5f 73 74 START g_st
c9390 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74 69 6d art=sqlite3Hwtim
c93a0 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 e().#define TIME
c93b0 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20 67 5f R_END g_
c93c0 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65 33 48 elapsed=sqlite3H
c93d0 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a wtime()-g_start.
c93e0 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c #define TIMER_EL
c93f0 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c 61 70 APSED g_elap
c9400 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e sed.#else.#defin
c9410 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 e TIMER_START.#d
c9420 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a efine TIMER_END.
c9430 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c #define TIMER_EL
c9440 41 50 53 45 44 20 20 20 20 20 28 28 73 71 6c 69 APSED ((sqli
c9450 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 6e te_uint64)0).#en
c9460 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 dif../*.** If we
c9470 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68 compile with th
c9480 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 e SQLITE_TEST ma
c9490 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68 cro set, then th
c94a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 e following bloc
c94b0 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c k.** of code wil
c94c0 6c 20 67 69 76 65 20 75 73 20 74 68 65 20 61 62 l give us the ab
c94d0 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 ility to simulat
c94e0 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 e a disk I/O err
c94f0 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 or. This.** is
c9500 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 used for testing
c9510 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 the I/O recover
c9520 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 y logic..*/.#ifd
c9530 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
c9540 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
c9550 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 lite3_io_error_h
c9560 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 it = 0;
c9570 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 /* Total numb
c9580 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 er of I/O Errors
c9590 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
c95a0 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
c95b0 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20 30 3b ror_hardhit = 0;
c95c0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
c95d0 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 r of non-benign
c95e0 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 errors */.SQLITE
c95f0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
c9600 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e _io_error_pendin
c9610 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a g = 0; /*
c9620 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 Count down to f
c9630 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a irst I/O error *
c9640 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
c9650 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
c9660 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b 20 20 r_persist = 0;
c9670 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
c9680 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 73 I/O errors pers
c9690 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ist */.SQLITE_AP
c96a0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f I int sqlite3_io
c96b0 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 _error_benign =
c96c0 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 0; /* Tr
c96d0 75 65 20 69 66 20 65 72 72 6f 72 73 20 61 72 65 ue if errors are
c96e0 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 benign */.SQLIT
c96f0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
c9700 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 3_diskfull_pendi
c9710 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 ng = 0;.SQLITE_A
c9720 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 PI int sqlite3_d
c9730 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 iskfull = 0;.#de
c9740 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 fine SimulateIOE
c9750 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71 rrorBenign(X) sq
c9760 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 lite3_io_error_b
c9770 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e enign=(X).#defin
c9780 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f e SimulateIOErro
c9790 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 r(CODE) \. if(
c97a0 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 (sqlite3_io_err
c97b0 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 or_persist && sq
c97c0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 lite3_io_error_h
c97d0 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 it) \. ||
c97e0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
c97f0 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 _pending-- == 1
c9800 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 ) \.
c9810 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 { local_ioerr
c9820 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 (); CODE; }.stat
c9830 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f ic void local_io
c9840 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 err(){. IOTRACE
c9850 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 (("IOERR\n"));.
c9860 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
c9870 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 r_hit++;. if( !
c9880 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
c9890 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 _benign ) sqlite
c98a0 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 3_io_error_hardh
c98b0 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 it++;.}.#define
c98c0 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c SimulateDiskfull
c98d0 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 Error(CODE) \.
c98e0 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 if( sqlite3_dis
c98f0 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b kfull_pending ){
c9900 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71 6c 69 \. if( sqli
c9910 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e te3_diskfull_pen
c9920 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 ding == 1 ){ \.
c9930 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 local_ioer
c9940 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 r(); \. sq
c9950 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d lite3_diskfull =
c9960 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 1; \. sql
c9970 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 ite3_io_error_hi
c9980 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 t = 1; \.
c9990 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c CODE; \. }el
c99a0 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c se{ \. sql
c99b0 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 ite3_diskfull_pe
c99c0 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 nding--; \.
c99d0 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 } \. }.#else.#
c99e0 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 define SimulateI
c99f0 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a OErrorBenign(X).
c9a00 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 #define Simulate
c9a10 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65 66 69 IOError(A).#defi
c9a20 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 ne SimulateDiskf
c9a30 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 ullError(A).#end
c9a40 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 if../*.** When t
c9a50 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 esting, keep a c
c9a60 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 ount of the numb
c9a70 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 er of open files
c9a80 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
c9a90 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 TE_TEST.SQLITE_A
c9aa0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
c9ab0 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d pen_file_count =
c9ac0 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 0;.#define Open
c9ad0 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69 Counter(X) sqli
c9ae0 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f te3_open_file_co
c9af0 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 unt+=(X).#else.#
c9b00 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 define OpenCount
c9b10 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 er(X).#endif..#e
c9b20 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 ndif /* !defined
c9b30 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 (_OS_COMMON_H_)
c9b40 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
c9b50 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f *** End of os_co
c9b60 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mmon.h *********
c9b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9b90 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
c9ba0 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
c9bb0 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
c9bc0 20 69 6e 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a in os_unix.c **
c9bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9be0 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e **/../*.** Defin
c9bf0 65 20 76 61 72 69 6f 75 73 20 6d 61 63 72 6f 73 e various macros
c9c00 20 74 68 61 74 20 61 72 65 20 6d 69 73 73 69 6e that are missin
c9c10 67 20 66 72 6f 6d 20 73 6f 6d 65 20 73 79 73 74 g from some syst
c9c20 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ems..*/.#ifndef
c9c30 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 65 O_LARGEFILE.# de
c9c40 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 fine O_LARGEFILE
c9c50 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 0.#endif.#ifdef
c9c60 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f SQLITE_DISABLE_
c9c70 4c 46 53 0a 23 20 75 6e 64 65 66 20 4f 5f 4c 41 LFS.# undef O_LA
c9c80 52 47 45 46 49 4c 45 0a 23 20 64 65 66 69 6e 65 RGEFILE.# define
c9c90 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30 0a 23 O_LARGEFILE 0.#
c9ca0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f 5f endif.#ifndef O_
c9cb0 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 64 65 66 69 6e NOFOLLOW.# defin
c9cc0 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 30 0a 23 e O_NOFOLLOW 0.#
c9cd0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f 5f endif.#ifndef O_
c9ce0 42 49 4e 41 52 59 0a 23 20 64 65 66 69 6e 65 20 BINARY.# define
c9cf0 4f 5f 42 49 4e 41 52 59 20 30 0a 23 65 6e 64 69 O_BINARY 0.#endi
c9d00 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 44 4a 47 f../*.** The DJG
c9d10 50 50 20 63 6f 6d 70 69 6c 65 72 20 65 6e 76 69 PP compiler envi
c9d20 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20 6d 6f ronment looks mo
c9d30 73 74 6c 79 20 6c 69 6b 65 20 55 6e 69 78 2c 20 stly like Unix,
c9d40 62 75 74 20 69 74 0a 2a 2a 20 6c 61 63 6b 73 20 but it.** lacks
c9d50 74 68 65 20 66 63 6e 74 6c 28 29 20 73 79 73 74 the fcntl() syst
c9d60 65 6d 20 63 61 6c 6c 2e 20 20 53 6f 20 72 65 64 em call. So red
c9d70 65 66 69 6e 65 20 66 63 6e 74 6c 28 29 20 74 6f efine fcntl() to
c9d80 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a be something.**
c9d90 20 74 68 61 74 20 61 6c 77 61 79 73 20 73 75 63 that always suc
c9da0 63 65 65 64 73 2e 20 20 54 68 69 73 20 6d 65 61 ceeds. This mea
c9db0 6e 73 20 74 68 61 74 20 6c 6f 63 6b 69 6e 67 20 ns that locking
c9dc0 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 20 75 does not occur u
c9dd0 6e 64 65 72 0a 2a 2a 20 44 4a 47 50 50 2e 20 20 nder.** DJGPP.
c9de0 42 75 74 20 69 74 20 69 73 20 44 4f 53 20 2d 20 But it is DOS -
c9df0 77 68 61 74 20 64 69 64 20 79 6f 75 20 65 78 70 what did you exp
c9e00 65 63 74 3f 0a 2a 2f 0a 23 69 66 64 65 66 20 5f ect?.*/.#ifdef _
c9e10 5f 44 4a 47 50 50 5f 5f 0a 23 20 64 65 66 69 6e _DJGPP__.# defin
c9e20 65 20 66 63 6e 74 6c 28 41 2c 42 2c 43 29 20 30 e fcntl(A,B,C) 0
c9e30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
c9e40 68 65 20 74 68 72 65 61 64 69 64 20 6d 61 63 72 he threadid macr
c9e50 6f 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 74 68 o resolves to th
c9e60 65 20 74 68 72 65 61 64 2d 69 64 20 6f 72 20 74 e thread-id or t
c9e70 6f 20 30 2e 20 20 55 73 65 64 20 66 6f 72 0a 2a o 0. Used for.*
c9e80 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 * testing and de
c9e90 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f bugging only..*/
c9ea0 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
c9eb0 41 44 53 41 46 45 0a 23 64 65 66 69 6e 65 20 74 ADSAFE.#define t
c9ec0 68 72 65 61 64 69 64 20 70 74 68 72 65 61 64 5f hreadid pthread_
c9ed0 73 65 6c 66 28 29 0a 23 65 6c 73 65 0a 23 64 65 self().#else.#de
c9ee0 66 69 6e 65 20 74 68 72 65 61 64 69 64 20 30 0a fine threadid 0.
c9ef0 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 48 #endif.../*.** H
c9f00 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 elper functions
c9f10 74 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 to obtain and re
c9f20 6c 69 6e 71 75 69 73 68 20 74 68 65 20 67 6c 6f linquish the glo
c9f30 62 61 6c 20 6d 75 74 65 78 2e 20 54 68 65 0a 2a bal mutex. The.*
c9f40 2a 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 69 * global mutex i
c9f50 73 20 75 73 65 64 20 74 6f 20 70 72 6f 74 65 63 s used to protec
c9f60 74 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e t the unixOpenCn
c9f70 74 2c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 t, unixLockInfo
c9f80 61 6e 64 0a 2a 2a 20 76 78 77 6f 72 6b 73 46 69 and.** vxworksFi
c9f90 6c 65 49 64 20 6f 62 6a 65 63 74 73 20 75 73 65 leId objects use
c9fa0 64 20 62 79 20 74 68 69 73 20 66 69 6c 65 2c 20 d by this file,
c9fb0 61 6c 6c 20 6f 66 20 77 68 69 63 68 20 6d 61 79 all of which may
c9fc0 20 62 65 20 0a 2a 2a 20 73 68 61 72 65 64 20 62 be .** shared b
c9fd0 79 20 6d 75 6c 74 69 70 6c 65 20 74 68 72 65 61 y multiple threa
c9fe0 64 73 2e 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 ds..**.** Functi
c9ff0 6f 6e 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 on unixMutexHeld
ca000 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61 73 () is used to as
ca010 73 65 72 74 28 29 20 74 68 61 74 20 74 68 65 20 sert() that the
ca020 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 0a 2a 2a global mutex .**
ca030 20 69 73 20 68 65 6c 64 20 77 68 65 6e 20 72 65 is held when re
ca040 71 75 69 72 65 64 2e 20 54 68 69 73 20 66 75 6e quired. This fun
ca050 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 ction is only us
ca060 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 73 ed as part of as
ca070 73 65 72 74 28 29 20 0a 2a 2a 20 73 74 61 74 65 sert() .** state
ca080 6d 65 6e 74 73 2e 20 65 2e 67 2e 0a 2a 2a 0a 2a ments. e.g..**.*
ca090 2a 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 * unixEnterMut
ca0a0 65 78 28 29 0a 2a 2a 20 20 20 20 20 61 73 73 65 ex().** asse
ca0b0 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c rt( unixMutexHel
ca0c0 64 28 29 20 29 3b 0a 2a 2a 20 20 20 75 6e 69 78 d() );.** unix
ca0d0 45 6e 74 65 72 4c 65 61 76 65 28 29 0a 2a 2f 0a EnterLeave().*/.
ca0e0 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 static void unix
ca0f0 45 6e 74 65 72 4d 75 74 65 78 28 76 6f 69 64 29 EnterMutex(void)
ca100 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 {. sqlite3_mute
ca110 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d x_enter(sqlite3M
ca120 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
ca130 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 _MUTEX_STATIC_MA
ca140 53 54 45 52 29 29 3b 0a 7d 0a 73 74 61 74 69 63 STER));.}.static
ca150 20 76 6f 69 64 20 75 6e 69 78 4c 65 61 76 65 4d void unixLeaveM
ca160 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 71 utex(void){. sq
ca170 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
ca180 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c e(sqlite3MutexAl
ca190 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
ca1a0 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 _STATIC_MASTER))
ca1b0 3b 0a 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 ;.}.#ifdef SQLIT
ca1c0 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 E_DEBUG.static i
ca1d0 6e 74 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 nt unixMutexHeld
ca1e0 28 76 6f 69 64 29 20 7b 0a 20 20 72 65 74 75 72 (void) {. retur
ca1f0 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f n sqlite3_mutex_
ca200 68 65 6c 64 28 73 71 6c 69 74 65 33 4d 75 74 65 held(sqlite3Mute
ca210 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
ca220 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
ca230 52 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a R));.}.#endif...
ca240 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
ca250 42 55 47 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 BUG./*.** Helper
ca260 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 70 72 function for pr
ca270 69 6e 74 69 6e 67 20 6f 75 74 20 74 72 61 63 65 inting out trace
ca280 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f information fro
ca290 6d 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20 62 m debugging.** b
ca2a0 69 6e 61 72 69 65 73 2e 20 54 68 69 73 20 72 65 inaries. This re
ca2b0 74 75 72 6e 73 20 74 68 65 20 73 74 72 69 6e 67 turns the string
ca2c0 20 72 65 70 72 65 73 65 74 61 74 69 6f 6e 20 6f represetation o
ca2d0 66 20 74 68 65 20 73 75 70 70 6c 69 65 64 0a 2a f the supplied.*
ca2e0 2a 20 69 6e 74 65 67 65 72 20 6c 6f 63 6b 2d 74 * integer lock-t
ca2f0 79 70 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 ype..*/.static c
ca300 6f 6e 73 74 20 63 68 61 72 20 2a 6c 6f 63 6b 74 onst char *lockt
ca310 79 70 65 4e 61 6d 65 28 69 6e 74 20 6c 6f 63 6b ypeName(int lock
ca320 74 79 70 65 29 7b 0a 20 20 73 77 69 74 63 68 28 type){. switch(
ca330 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 locktype ){.
ca340 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b 3a 20 72 case NO_LOCK: r
ca350 65 74 75 72 6e 20 22 4e 4f 4e 45 22 3b 0a 20 20 eturn "NONE";.
ca360 20 20 63 61 73 65 20 53 48 41 52 45 44 5f 4c 4f case SHARED_LO
ca370 43 4b 3a 20 72 65 74 75 72 6e 20 22 53 48 41 52 CK: return "SHAR
ca380 45 44 22 3b 0a 20 20 20 20 63 61 73 65 20 52 45 ED";. case RE
ca390 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 SERVED_LOCK: ret
ca3a0 75 72 6e 20 22 52 45 53 45 52 56 45 44 22 3b 0a urn "RESERVED";.
ca3b0 20 20 20 20 63 61 73 65 20 50 45 4e 44 49 4e 47 case PENDING
ca3c0 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 50 _LOCK: return "P
ca3d0 45 4e 44 49 4e 47 22 3b 0a 20 20 20 20 63 61 73 ENDING";. cas
ca3e0 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b e EXCLUSIVE_LOCK
ca3f0 3a 20 72 65 74 75 72 6e 20 22 45 58 43 4c 55 53 : return "EXCLUS
ca400 49 56 45 22 3b 0a 20 20 7d 0a 20 20 72 65 74 75 IVE";. }. retu
ca410 72 6e 20 22 45 52 52 4f 52 22 3b 0a 7d 0a 23 65 rn "ERROR";.}.#e
ca420 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c ndif..#ifdef SQL
ca430 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a 2f ITE_LOCK_TRACE./
ca440 2a 0a 2a 2a 20 50 72 69 6e 74 20 6f 75 74 20 69 *.** Print out i
ca450 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
ca460 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 all locking ope
ca470 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 rations..**.** T
ca480 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
ca490 73 65 64 20 66 6f 72 20 74 72 6f 75 62 6c 65 73 sed for troubles
ca4a0 68 6f 6f 74 69 6e 67 20 6c 6f 63 6b 73 20 6f 6e hooting locks on
ca4b0 20 6d 75 6c 74 69 74 68 72 65 61 64 65 64 0a 2a multithreaded.*
ca4c0 2a 20 70 6c 61 74 66 6f 72 6d 73 2e 20 20 45 6e * platforms. En
ca4d0 61 62 6c 65 20 62 79 20 63 6f 6d 70 69 6c 69 6e able by compilin
ca4e0 67 20 77 69 74 68 20 74 68 65 20 2d 44 53 51 4c g with the -DSQL
ca4f0 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a 2a ITE_LOCK_TRACE.*
ca500 2a 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f * command-line o
ca510 70 74 69 6f 6e 20 6f 6e 20 74 68 65 20 63 6f 6d ption on the com
ca520 70 69 6c 65 72 2e 20 20 54 68 69 73 20 63 6f 64 piler. This cod
ca530 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a e is normally.**
ca540 20 74 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2f 0a turned off..*/.
ca550 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 54 static int lockT
ca560 72 61 63 65 28 69 6e 74 20 66 64 2c 20 69 6e 74 race(int fd, int
ca570 20 6f 70 2c 20 73 74 72 75 63 74 20 66 6c 6f 63 op, struct floc
ca580 6b 20 2a 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a k *p){. char *z
ca590 4f 70 4e 61 6d 65 2c 20 2a 7a 54 79 70 65 3b 0a OpName, *zType;.
ca5a0 20 20 69 6e 74 20 73 3b 0a 20 20 69 6e 74 20 73 int s;. int s
ca5b0 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20 69 66 28 avedErrno;. if(
ca5c0 20 6f 70 3d 3d 46 5f 47 45 54 4c 4b 20 29 7b 0a op==F_GETLK ){.
ca5d0 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 47 zOpName = "G
ca5e0 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 ETLK";. }else i
ca5f0 66 28 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 29 f( op==F_SETLK )
ca600 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 {. zOpName =
ca610 22 53 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 "SETLK";. }else
ca620 7b 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 6c 28 {. s = fcntl(
ca630 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20 20 20 20 fd, op, p);.
ca640 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
ca650 74 66 28 22 66 63 6e 74 6c 20 75 6e 6b 6e 6f 77 tf("fcntl unknow
ca660 6e 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 66 n %d %d %d\n", f
ca670 64 2c 20 6f 70 2c 20 73 29 3b 0a 20 20 20 20 72 d, op, s);. r
ca680 65 74 75 72 6e 20 73 3b 0a 20 20 7d 0a 20 20 69 eturn s;. }. i
ca690 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f f( p->l_type==F_
ca6a0 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79 RDLCK ){. zTy
ca6b0 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20 20 pe = "RDLCK";.
ca6c0 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f 74 }else if( p->l_t
ca6d0 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a ype==F_WRLCK ){.
ca6e0 20 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52 4c zType = "WRL
ca6f0 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 CK";. }else if(
ca700 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e p->l_type==F_UN
ca710 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 LCK ){. zType
ca720 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 7d 65 = "UNLCK";. }e
ca730 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 lse{. assert(
ca740 20 30 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 0 );. }. asse
ca750 72 74 28 20 70 2d 3e 6c 5f 77 68 65 6e 63 65 3d rt( p->l_whence=
ca760 3d 53 45 45 4b 5f 53 45 54 20 29 3b 0a 20 20 73 =SEEK_SET );. s
ca770 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 6f 70 2c = fcntl(fd, op,
ca780 20 70 29 3b 0a 20 20 73 61 76 65 64 45 72 72 6e p);. savedErrn
ca790 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 73 71 6c o = errno;. sql
ca7a0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
ca7b0 22 66 63 6e 74 6c 20 25 64 20 25 64 20 25 73 20 "fcntl %d %d %s
ca7c0 25 73 20 25 64 20 25 64 20 25 64 20 25 64 5c 6e %s %d %d %d %d\n
ca7d0 22 2c 0a 20 20 20 20 20 74 68 72 65 61 64 69 64 ",. threadid
ca7e0 2c 20 66 64 2c 20 7a 4f 70 4e 61 6d 65 2c 20 7a , fd, zOpName, z
ca7f0 54 79 70 65 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f Type, (int)p->l_
ca800 73 74 61 72 74 2c 20 28 69 6e 74 29 70 2d 3e 6c start, (int)p->l
ca810 5f 6c 65 6e 2c 0a 20 20 20 20 20 28 69 6e 74 29 _len,. (int)
ca820 70 2d 3e 6c 5f 70 69 64 2c 20 73 29 3b 0a 20 20 p->l_pid, s);.
ca830 69 66 28 20 73 3d 3d 28 2d 31 29 20 26 26 20 6f if( s==(-1) && o
ca840 70 3d 3d 46 5f 53 45 54 4c 4b 20 26 26 20 28 70 p==F_SETLK && (p
ca850 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 ->l_type==F_RDLC
ca860 4b 20 7c 7c 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d K || p->l_type==
ca870 46 5f 57 52 4c 43 4b 29 20 29 7b 0a 20 20 20 20 F_WRLCK) ){.
ca880 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 32 3b struct flock l2;
ca890 0a 20 20 20 20 6c 32 20 3d 20 2a 70 3b 0a 20 20 . l2 = *p;.
ca8a0 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45 fcntl(fd, F_GE
ca8b0 54 4c 4b 2c 20 26 6c 32 29 3b 0a 20 20 20 20 69 TLK, &l2);. i
ca8c0 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f f( l2.l_type==F_
ca8d0 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a RDLCK ){. z
ca8e0 54 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b 0a Type = "RDLCK";.
ca8f0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 32 }else if( l2
ca900 2e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b .l_type==F_WRLCK
ca910 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 ){. zType
ca920 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 20 20 20 7d = "WRLCK";. }
ca930 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74 79 else if( l2.l_ty
ca940 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 pe==F_UNLCK ){.
ca950 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 55 4e zType = "UN
ca960 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 7b LCK";. }else{
ca970 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30 . assert( 0
ca980 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 );. }. sq
ca990 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
ca9a0 28 22 66 63 6e 74 6c 2d 66 61 69 6c 75 72 65 2d ("fcntl-failure-
ca9b0 72 65 61 73 6f 6e 3a 20 25 73 20 25 64 20 25 64 reason: %s %d %d
ca9c0 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 7a %d\n",. z
ca9d0 54 79 70 65 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f Type, (int)l2.l_
ca9e0 73 74 61 72 74 2c 20 28 69 6e 74 29 6c 32 2e 6c start, (int)l2.l
ca9f0 5f 6c 65 6e 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f _len, (int)l2.l_
caa00 70 69 64 29 3b 0a 20 20 7d 0a 20 20 65 72 72 6e pid);. }. errn
caa10 6f 20 3d 20 73 61 76 65 64 45 72 72 6e 6f 3b 0a o = savedErrno;.
caa20 20 20 72 65 74 75 72 6e 20 73 3b 0a 7d 0a 23 64 return s;.}.#d
caa30 65 66 69 6e 65 20 66 63 6e 74 6c 20 6c 6f 63 6b efine fcntl lock
caa40 54 72 61 63 65 0a 23 65 6e 64 69 66 20 2f 2a 20 Trace.#endif /*
caa50 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 SQLITE_LOCK_TRAC
caa60 45 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 E */..../*.** Th
caa70 69 73 20 72 6f 75 74 69 6e 65 20 74 72 61 6e 73 is routine trans
caa80 6c 61 74 65 73 20 61 20 73 74 61 6e 64 61 72 64 lates a standard
caa90 20 50 4f 53 49 58 20 65 72 72 6e 6f 20 63 6f 64 POSIX errno cod
caaa0 65 20 69 6e 74 6f 20 73 6f 6d 65 74 68 69 6e 67 e into something
caab0 0a 2a 2a 20 75 73 65 66 75 6c 20 74 6f 20 74 68 .** useful to th
caac0 65 20 63 6c 69 65 6e 74 73 20 6f 66 20 74 68 65 e clients of the
caad0 20 73 71 6c 69 74 65 33 20 66 75 6e 63 74 69 6f sqlite3 functio
caae0 6e 73 2e 20 20 53 70 65 63 69 66 69 63 61 6c 6c ns. Specificall
caaf0 79 2c 20 69 74 20 69 73 0a 2a 2a 20 69 6e 74 65 y, it is.** inte
cab00 6e 64 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 nded to translat
cab10 65 20 61 20 76 61 72 69 65 74 79 20 6f 66 20 22 e a variety of "
cab20 74 72 79 20 61 67 61 69 6e 22 20 65 72 72 6f 72 try again" error
cab30 73 20 69 6e 74 6f 20 53 51 4c 49 54 45 5f 42 55 s into SQLITE_BU
cab40 53 59 0a 2a 2a 20 61 6e 64 20 61 20 76 61 72 69 SY.** and a vari
cab50 65 74 79 20 6f 66 20 22 70 6c 65 61 73 65 20 63 ety of "please c
cab60 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 64 65 lose the file de
cab70 73 63 72 69 70 74 6f 72 20 4e 4f 57 22 20 65 72 scriptor NOW" er
cab80 72 6f 72 73 20 69 6e 74 6f 20 0a 2a 2a 20 53 51 rors into .** SQ
cab90 4c 49 54 45 5f 49 4f 45 52 52 0a 2a 2a 20 0a 2a LITE_IOERR.** .*
caba0 2a 20 45 72 72 6f 72 73 20 64 75 72 69 6e 67 20 * Errors during
cabb0 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f initialization o
cabc0 66 20 6c 6f 63 6b 73 2c 20 6f 72 20 66 69 6c 65 f locks, or file
cabd0 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 system support
cabe0 66 6f 72 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 73 68 for locks,.** sh
cabf0 6f 75 6c 64 20 68 61 6e 64 6c 65 20 45 4e 4f 4c ould handle ENOL
cac00 43 4b 2c 20 45 4e 4f 54 53 55 50 2c 20 45 4f 50 CK, ENOTSUP, EOP
cac10 4e 4f 54 53 55 50 50 20 73 65 70 61 72 61 74 65 NOTSUPP separate
cac20 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ly..*/.static in
cac30 74 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f t sqliteErrorFro
cac40 6d 50 6f 73 69 78 45 72 72 6f 72 28 69 6e 74 20 mPosixError(int
cac50 70 6f 73 69 78 45 72 72 6f 72 2c 20 69 6e 74 20 posixError, int
cac60 73 71 6c 69 74 65 49 4f 45 72 72 29 20 7b 0a 20 sqliteIOErr) {.
cac70 20 73 77 69 74 63 68 20 28 70 6f 73 69 78 45 72 switch (posixEr
cac80 72 6f 72 29 20 7b 0a 20 20 63 61 73 65 20 30 3a ror) {. case 0:
cac90 20 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c . return SQL
caca0 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20 63 ITE_OK;. . c
cacb0 61 73 65 20 45 41 47 41 49 4e 3a 0a 20 20 63 61 ase EAGAIN:. ca
cacc0 73 65 20 45 54 49 4d 45 44 4f 55 54 3a 0a 20 20 se ETIMEDOUT:.
cacd0 63 61 73 65 20 45 42 55 53 59 3a 0a 20 20 63 61 case EBUSY:. ca
cace0 73 65 20 45 49 4e 54 52 3a 0a 20 20 63 61 73 65 se EINTR:. case
cacf0 20 45 4e 4f 4c 43 4b 3a 20 20 0a 20 20 20 20 2f ENOLCK: . /
cad00 2a 20 72 61 6e 64 6f 6d 20 4e 46 53 20 72 65 74 * random NFS ret
cad10 72 79 20 65 72 72 6f 72 2c 20 75 6e 6c 65 73 73 ry error, unless
cad20 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73 during file sys
cad30 74 65 6d 20 73 75 70 70 6f 72 74 20 0a 20 20 20 tem support .
cad40 20 20 2a 20 69 6e 74 72 6f 73 70 65 63 74 69 6f * introspectio
cad50 6e 2c 20 69 6e 20 77 68 69 63 68 20 69 74 20 61 n, in which it a
cad60 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 68 ctually means wh
cad70 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 20 20 at it says */.
cad80 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
cad90 42 55 53 59 3b 0a 20 20 20 20 0a 20 20 63 61 73 BUSY;. . cas
cada0 65 20 45 41 43 43 45 53 3a 20 0a 20 20 20 20 2f e EACCES: . /
cadb0 2a 20 45 41 43 43 45 53 20 69 73 20 6c 69 6b 65 * EACCES is like
cadc0 20 45 41 47 41 49 4e 20 64 75 72 69 6e 67 20 6c EAGAIN during l
cadd0 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e ocking operation
cade0 73 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f s, but not any o
cadf0 74 68 65 72 20 74 69 6d 65 2a 2f 0a 20 20 20 20 ther time*/.
cae00 69 66 28 20 28 73 71 6c 69 74 65 49 4f 45 72 72 if( (sqliteIOErr
cae10 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 == SQLITE_IOERR
cae20 5f 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28 73 71 6c _LOCK) || ..(sql
cae30 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 iteIOErr == SQLI
cae40 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 TE_IOERR_UNLOCK)
cae50 20 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f 45 || ..(sqliteIOE
cae60 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 rr == SQLITE_IOE
cae70 52 52 5f 52 44 4c 4f 43 4b 29 20 7c 7c 0a 09 28 RR_RDLOCK) ||..(
cae80 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 sqliteIOErr == S
cae90 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 QLITE_IOERR_CHEC
caea0 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 20 29 KRESERVEDLOCK) )
caeb0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
caec0 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 QLITE_BUSY;.
caed0 7d 0a 20 20 20 20 2f 2a 20 65 6c 73 65 20 66 61 }. /* else fa
caee0 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20 ll through */.
caef0 63 61 73 65 20 45 50 45 52 4d 3a 20 0a 20 20 20 case EPERM: .
caf00 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 return SQLITE_P
caf10 45 52 4d 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 ERM;. . case
caf20 20 45 44 45 41 44 4c 4b 3a 0a 20 20 20 20 72 65 EDEADLK:. re
caf30 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
caf40 52 5f 42 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 0a R_BLOCKED;. .
caf50 23 69 66 20 45 4f 50 4e 4f 54 53 55 50 50 21 3d #if EOPNOTSUPP!=
caf60 45 4e 4f 54 53 55 50 0a 20 20 63 61 73 65 20 45 ENOTSUP. case E
caf70 4f 50 4e 4f 54 53 55 50 50 3a 20 0a 20 20 20 20 OPNOTSUPP: .
caf80 2f 2a 20 73 6f 6d 65 74 68 69 6e 67 20 77 65 6e /* something wen
caf90 74 20 74 65 72 72 69 62 6c 79 20 61 77 72 79 2c t terribly awry,
cafa0 20 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66 unless during f
cafb0 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f ile system suppo
cafc0 72 74 20 0a 20 20 20 20 20 2a 20 69 6e 74 72 6f rt . * intro
cafd0 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 spection, in whi
cafe0 63 68 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d ch it actually m
caff0 65 61 6e 73 20 77 68 61 74 20 69 74 20 73 61 79 eans what it say
cb000 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 s */.#endif.#ifd
cb010 65 66 20 45 4e 4f 54 53 55 50 0a 20 20 63 61 73 ef ENOTSUP. cas
cb020 65 20 45 4e 4f 54 53 55 50 3a 20 0a 20 20 20 20 e ENOTSUP: .
cb030 2f 2a 20 69 6e 76 61 6c 69 64 20 66 64 2c 20 75 /* invalid fd, u
cb040 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c nless during fil
cb050 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 e system support
cb060 20 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 introspection,
cb070 69 6e 20 77 68 69 63 68 20 0a 20 20 20 20 20 2a in which . *
cb080 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 it actually mea
cb090 6e 73 20 77 68 61 74 20 69 74 20 73 61 79 73 20 ns what it says
cb0a0 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 61 73 65 */.#endif. case
cb0b0 20 45 49 4f 3a 0a 20 20 63 61 73 65 20 45 42 41 EIO:. case EBA
cb0c0 44 46 3a 0a 20 20 63 61 73 65 20 45 49 4e 56 41 DF:. case EINVA
cb0d0 4c 3a 0a 20 20 63 61 73 65 20 45 4e 4f 54 43 4f L:. case ENOTCO
cb0e0 4e 4e 3a 0a 20 20 63 61 73 65 20 45 4e 4f 44 45 NN:. case ENODE
cb0f0 56 3a 0a 20 20 63 61 73 65 20 45 4e 58 49 4f 3a V:. case ENXIO:
cb100 0a 20 20 63 61 73 65 20 45 4e 4f 45 4e 54 3a 0a . case ENOENT:.
cb110 20 20 63 61 73 65 20 45 53 54 41 4c 45 3a 0a 20 case ESTALE:.
cb120 20 63 61 73 65 20 45 4e 4f 53 59 53 3a 0a 20 20 case ENOSYS:.
cb130 20 20 2f 2a 20 74 68 65 73 65 20 73 68 6f 75 6c /* these shoul
cb140 64 20 66 6f 72 63 65 20 74 68 65 20 63 6c 69 65 d force the clie
cb150 6e 74 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 nt to close the
cb160 66 69 6c 65 20 61 6e 64 20 72 65 63 6f 6e 6e 65 file and reconne
cb170 63 74 20 2a 2f 0a 20 20 20 20 0a 20 20 64 65 66 ct */. . def
cb180 61 75 6c 74 3a 20 0a 20 20 20 20 72 65 74 75 72 ault: . retur
cb190 6e 20 73 71 6c 69 74 65 49 4f 45 72 72 3b 0a 20 n sqliteIOErr;.
cb1a0 20 7d 0a 7d 0a 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a }.}..../*******
cb1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb1f0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
cb200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
cb210 20 55 6e 69 71 75 65 20 46 69 6c 65 20 49 44 20 Unique File ID
cb220 55 74 69 6c 69 74 79 20 55 73 65 64 20 42 79 20 Utility Used By
cb230 56 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a VxWorks ********
cb240 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 6e *******.**.** On
cb250 20 6d 6f 73 74 20 76 65 72 73 69 6f 6e 73 20 6f most versions o
cb260 66 20 75 6e 69 78 2c 20 77 65 20 63 61 6e 20 67 f unix, we can g
cb270 65 74 20 61 20 75 6e 69 71 75 65 20 49 44 20 66 et a unique ID f
cb280 6f 72 20 61 20 66 69 6c 65 20 62 79 20 63 6f 6e or a file by con
cb290 63 61 74 65 6e 61 74 69 6e 67 0a 2a 2a 20 74 68 catenating.** th
cb2a0 65 20 64 65 76 69 63 65 20 6e 75 6d 62 65 72 20 e device number
cb2b0 61 6e 64 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 and the inode nu
cb2c0 6d 62 65 72 2e 20 20 42 75 74 20 74 68 69 73 20 mber. But this
cb2d0 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e does not work on
cb2e0 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 4f 6e 20 VxWorks..** On
cb2f0 56 78 57 6f 72 6b 73 2c 20 61 20 75 6e 69 71 75 VxWorks, a uniqu
cb300 65 20 66 69 6c 65 20 69 64 20 6d 75 73 74 20 62 e file id must b
cb310 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 63 e based on the c
cb320 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d anonical filenam
cb330 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 e..**.** A point
cb340 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 er to an instanc
cb350 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
cb360 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 61 6e ng structure can
cb370 20 62 65 20 75 73 65 64 20 61 73 20 61 0a 2a 2a be used as a.**
cb380 20 75 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 unique file ID
cb390 69 6e 20 56 78 57 6f 72 6b 73 2e 20 20 45 61 63 in VxWorks. Eac
cb3a0 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 h instance of th
cb3b0 69 73 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e is structure con
cb3c0 74 61 69 6e 73 0a 2a 2a 20 61 20 63 6f 70 79 20 tains.** a copy
cb3d0 6f 66 20 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c of the canonical
cb3e0 20 66 69 6c 65 6e 61 6d 65 2e 20 20 54 68 65 72 filename. Ther
cb3f0 65 20 69 73 20 61 6c 73 6f 20 61 20 72 65 66 65 e is also a refe
cb400 72 65 6e 63 65 20 63 6f 75 6e 74 2e 20 20 0a 2a rence count. .*
cb410 2a 20 54 68 65 20 73 74 72 75 63 74 75 72 65 20 * The structure
cb420 69 73 20 72 65 63 6c 61 69 6d 65 64 20 77 68 65 is reclaimed whe
cb430 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
cb440 70 6f 69 6e 74 65 72 73 20 74 6f 20 69 74 20 64 pointers to it d
cb450 72 6f 70 73 20 74 6f 0a 2a 2a 20 7a 65 72 6f 2e rops to.** zero.
cb460 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 .**.** There are
cb470 20 6e 65 76 65 72 20 76 65 72 79 20 6d 61 6e 79 never very many
cb480 20 66 69 6c 65 73 20 6f 70 65 6e 20 61 74 20 6f files open at o
cb490 6e 65 20 74 69 6d 65 20 61 6e 64 20 6c 6f 6f 6b ne time and look
cb4a0 75 70 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 61 ups are not.** a
cb4b0 20 70 65 72 66 6f 72 6d 61 6e 63 65 2d 63 72 69 performance-cri
cb4c0 74 69 63 61 6c 20 70 61 74 68 2c 20 73 6f 20 69 tical path, so i
cb4d0 74 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20 t is sufficient
cb4e0 74 6f 20 70 75 74 20 74 68 65 73 65 0a 2a 2a 20 to put these.**
cb4f0 73 74 72 75 63 74 75 72 65 73 20 6f 6e 20 61 20 structures on a
cb500 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a linked list..*/.
cb510 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 struct vxworksFi
cb520 6c 65 49 64 20 7b 0a 20 20 73 74 72 75 63 74 20 leId {. struct
cb530 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 vxworksFileId *p
cb540 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 69 Next; /* Next i
cb550 6e 20 61 20 6c 69 73 74 20 6f 66 20 74 68 65 6d n a list of them
cb560 20 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 all */. int nR
cb570 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ef;
cb580 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
cb590 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 r of references
cb5a0 74 6f 20 74 68 69 73 20 6f 6e 65 20 2a 2f 0a 20 to this one */.
cb5b0 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 int nName;
cb5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
cb5d0 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 * Length of the
cb5e0 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 5b 5d zCanonicalName[]
cb5f0 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61 string */. cha
cb600 72 20 2a 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d r *zCanonicalNam
cb610 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 e; /* Ca
cb620 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 nonical filename
cb630 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 20 4f 53 5f 56 */.};..#if OS_V
cb640 58 57 4f 52 4b 53 0a 2f 2a 20 0a 2a 2a 20 41 6c XWORKS./* .** Al
cb650 6c 20 75 6e 69 71 75 65 20 66 69 6c 65 6e 61 6d l unique filenam
cb660 65 73 20 61 72 65 20 68 65 6c 64 20 6f 6e 20 61 es are held on a
cb670 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 linked list hea
cb680 64 65 64 20 62 79 20 74 68 69 73 0a 2a 2a 20 76 ded by this.** v
cb690 61 72 69 61 62 6c 65 3a 0a 2a 2f 0a 73 74 61 74 ariable:.*/.stat
cb6a0 69 63 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b ic struct vxwork
cb6b0 73 46 69 6c 65 49 64 20 2a 76 78 77 6f 72 6b 73 sFileId *vxworks
cb6c0 46 69 6c 65 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f FileList = 0;../
cb6d0 2a 0a 2a 2a 20 53 69 6d 70 6c 69 66 79 20 61 20 *.** Simplify a
cb6e0 66 69 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 69 74 filename into it
cb6f0 73 20 63 61 6e 6f 6e 69 63 61 6c 20 66 6f 72 6d s canonical form
cb700 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e 67 20 74 68 .** by making th
cb710 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e e following chan
cb720 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 72 65 ges:.**.** * re
cb730 6d 6f 76 69 6e 67 20 61 6e 79 20 74 72 61 69 6c moving any trail
cb740 69 6e 67 20 61 6e 64 20 64 75 70 6c 69 63 61 74 ing and duplicat
cb750 65 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 e /.** * conver
cb760 74 20 2f 2e 2f 20 69 6e 74 6f 20 6a 75 73 74 20 t /./ into just
cb770 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 74 20 /.** * convert
cb780 2f 41 2f 2e 2e 2f 20 77 68 65 72 65 20 41 20 69 /A/../ where A i
cb790 73 20 61 6e 79 20 73 69 6d 70 6c 65 20 6e 61 6d s any simple nam
cb7a0 65 20 69 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a e into just /.**
cb7b0 0a 2a 2a 20 43 68 61 6e 67 65 73 20 61 72 65 20 .** Changes are
cb7c0 6d 61 64 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 made in-place.
cb7d0 52 65 74 75 72 6e 20 74 68 65 20 6e 65 77 20 6e Return the new n
cb7e0 61 6d 65 20 6c 65 6e 67 74 68 2e 0a 2a 2a 0a 2a ame length..**.*
cb7f0 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 66 * The original f
cb800 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 20 7a 5b ilename is in z[
cb810 30 2e 2e 6e 2d 31 5d 2e 20 20 52 65 74 75 72 6e 0..n-1]. Return
cb820 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
cb830 2a 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 * characters in
cb840 74 68 65 20 73 69 6d 70 6c 69 66 69 65 64 20 6e the simplified n
cb850 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ame..*/.static i
cb860 6e 74 20 76 78 77 6f 72 6b 73 53 69 6d 70 6c 69 nt vxworksSimpli
cb870 66 79 4e 61 6d 65 28 63 68 61 72 20 2a 7a 2c 20 fyName(char *z,
cb880 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 2c int n){. int i,
cb890 20 6a 3b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 31 j;. while( n>1
cb8a0 20 26 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 2f 27 20 && z[n-1]=='/'
cb8b0 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 66 6f 72 28 ){ n--; }. for(
cb8c0 69 3d 6a 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 i=j=0; i<n; i++)
cb8d0 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d {. if( z[i]==
cb8e0 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 69 66 28 '/' ){. if(
cb8f0 20 7a 5b 69 2b 31 5d 3d 3d 27 2f 27 20 29 20 63 z[i+1]=='/' ) c
cb900 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 ontinue;. i
cb910 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 f( z[i+1]=='.' &
cb920 26 20 69 2b 32 3c 6e 20 26 26 20 7a 5b 69 2b 32 & i+2<n && z[i+2
cb930 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 ]=='/' ){.
cb940 20 20 69 20 2b 3d 20 31 3b 0a 20 20 20 20 20 20 i += 1;.
cb950 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 continue;.
cb960 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 7a 5b }. if( z[
cb970 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 69 2b 33 i+1]=='.' && i+3
cb980 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2e <n && z[i+2]=='.
cb990 27 20 26 26 20 7a 5b 69 2b 33 5d 3d 3d 27 2f 27 ' && z[i+3]=='/'
cb9a0 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68 69 6c ){. whil
cb9b0 65 28 20 6a 3e 30 20 26 26 20 7a 5b 6a 2d 31 5d e( j>0 && z[j-1]
cb9c0 21 3d 27 2f 27 20 29 7b 20 6a 2d 2d 3b 20 7d 0a !='/' ){ j--; }.
cb9d0 20 20 20 20 20 20 20 20 69 66 28 20 6a 3e 30 20 if( j>0
cb9e0 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 ){ j--; }.
cb9f0 20 20 69 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 i += 2;.
cba00 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 continue;.
cba10 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 5b }. }. z[
cba20 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 7d j++] = z[i];. }
cba30 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72 . z[j] = 0;. r
cba40 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn j;.}../*.*
cba50 2a 20 46 69 6e 64 20 61 20 75 6e 69 71 75 65 20 * Find a unique
cba60 66 69 6c 65 20 49 44 20 66 6f 72 20 74 68 65 20 file ID for the
cba70 67 69 76 65 6e 20 61 62 73 6f 6c 75 74 65 20 70 given absolute p
cba80 61 74 68 6e 61 6d 65 2e 20 20 52 65 74 75 72 6e athname. Return
cba90 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f .** a pointer to
cbaa0 20 74 68 65 20 76 78 77 6f 72 6b 73 46 69 6c 65 the vxworksFile
cbab0 49 64 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 Id object. This
cbac0 20 70 6f 69 6e 74 65 72 20 69 73 20 74 68 65 20 pointer is the
cbad0 75 6e 69 71 75 65 0a 2a 2a 20 66 69 6c 65 20 49 unique.** file I
cbae0 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 52 65 D..**.** The nRe
cbaf0 66 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 76 f field of the v
cbb00 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a xworksFileId obj
cbb10 65 63 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 ect is increment
cbb20 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 ed before.** the
cbb30 20 6f 62 6a 65 63 74 20 69 73 20 72 65 74 75 72 object is retur
cbb40 6e 65 64 2e 20 20 41 20 6e 65 77 20 76 78 77 6f ned. A new vxwo
cbb50 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 rksFileId object
cbb60 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 is created.** a
cbb70 6e 64 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 nd added to the
cbb80 67 6c 6f 62 61 6c 20 6c 69 73 74 20 69 66 20 6e global list if n
cbb90 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 ecessary..**.**
cbba0 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f If a memory allo
cbbb0 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 cation error occ
cbbc0 75 72 73 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c urs, return NULL
cbbd0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 ..*/.static stru
cbbe0 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 ct vxworksFileId
cbbf0 20 2a 76 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c *vxworksFindFil
cbc00 65 49 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a eId(const char *
cbc10 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 29 7b 0a zAbsoluteName){.
cbc20 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 struct vxworks
cbc30 46 69 6c 65 49 64 20 2a 70 4e 65 77 3b 20 20 20 FileId *pNew;
cbc40 20 20 20 20 20 20 2f 2a 20 73 65 61 72 63 68 20 /* search
cbc50 6b 65 79 20 61 6e 64 20 6e 65 77 20 66 69 6c 65 key and new file
cbc60 20 49 44 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ID */. struct
cbc70 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 vxworksFileId *p
cbc80 43 61 6e 64 69 64 61 74 65 3b 20 20 20 2f 2a 20 Candidate; /*
cbc90 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 For looping over
cbca0 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 20 49 existing file I
cbcb0 44 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 Ds */. int n;
cbcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cbcd0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
cbce0 65 6e 67 74 68 20 6f 66 20 7a 41 62 73 6f 6c 75 ength of zAbsolu
cbcf0 74 65 4e 61 6d 65 20 73 74 72 69 6e 67 20 2a 2f teName string */
cbd00 0a 0a 20 20 61 73 73 65 72 74 28 20 7a 41 62 73 .. assert( zAbs
cbd10 6f 6c 75 74 65 4e 61 6d 65 5b 30 5d 3d 3d 27 2f oluteName[0]=='/
cbd20 27 20 29 3b 0a 20 20 6e 20 3d 20 28 69 6e 74 29 ' );. n = (int)
cbd30 73 74 72 6c 65 6e 28 7a 41 62 73 6f 6c 75 74 65 strlen(zAbsolute
cbd40 4e 61 6d 65 29 3b 0a 20 20 70 4e 65 77 20 3d 20 Name);. pNew =
cbd50 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 sqlite3_malloc(
cbd60 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 2b 20 sizeof(*pNew) +
cbd70 28 6e 2b 31 29 20 29 3b 0a 20 20 69 66 28 20 70 (n+1) );. if( p
cbd80 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 New==0 ) return
cbd90 30 3b 0a 20 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f 0;. pNew->zCano
cbda0 6e 69 63 61 6c 4e 61 6d 65 20 3d 20 28 63 68 61 nicalName = (cha
cbdb0 72 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 20 6d r*)&pNew[1];. m
cbdc0 65 6d 63 70 79 28 70 4e 65 77 2d 3e 7a 43 61 6e emcpy(pNew->zCan
cbdd0 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 7a 41 62 73 onicalName, zAbs
cbde0 6f 6c 75 74 65 4e 61 6d 65 2c 20 6e 2b 31 29 3b oluteName, n+1);
cbdf0 0a 20 20 6e 20 3d 20 76 78 77 6f 72 6b 73 53 69 . n = vxworksSi
cbe00 6d 70 6c 69 66 79 4e 61 6d 65 28 70 4e 65 77 2d mplifyName(pNew-
cbe10 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c >zCanonicalName,
cbe20 20 6e 29 3b 0a 0a 20 20 2f 2a 20 53 65 61 72 63 n);.. /* Searc
cbe30 68 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e h for an existin
cbe40 67 20 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 g entry that mat
cbe50 63 68 69 6e 67 20 74 68 65 20 63 61 6e 6f 6e 69 ching the canoni
cbe60 63 61 6c 20 6e 61 6d 65 2e 0a 20 20 2a 2a 20 49 cal name.. ** I
cbe70 66 20 66 6f 75 6e 64 2c 20 69 6e 63 72 65 6d 65 f found, increme
cbe80 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 nt the reference
cbe90 20 63 6f 75 6e 74 20 61 6e 64 20 72 65 74 75 72 count and retur
cbea0 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 20 n a pointer to.
cbeb0 20 2a 2a 20 74 68 65 20 65 78 69 73 74 69 6e 67 ** the existing
cbec0 20 66 69 6c 65 20 49 44 2e 0a 20 20 2a 2f 0a 20 file ID.. */.
cbed0 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
cbee0 29 3b 0a 20 20 66 6f 72 28 70 43 61 6e 64 69 64 );. for(pCandid
cbef0 61 74 65 3d 76 78 77 6f 72 6b 73 46 69 6c 65 4c ate=vxworksFileL
cbf00 69 73 74 3b 20 70 43 61 6e 64 69 64 61 74 65 3b ist; pCandidate;
cbf10 20 70 43 61 6e 64 69 64 61 74 65 3d 70 43 61 6e pCandidate=pCan
cbf20 64 69 64 61 74 65 2d 3e 70 4e 65 78 74 29 7b 0a didate->pNext){.
cbf30 20 20 20 20 69 66 28 20 70 43 61 6e 64 69 64 61 if( pCandida
cbf40 74 65 2d 3e 6e 4e 61 6d 65 3d 3d 6e 20 0a 20 20 te->nName==n .
cbf50 20 20 20 26 26 20 6d 65 6d 63 6d 70 28 70 43 61 && memcmp(pCa
cbf60 6e 64 69 64 61 74 65 2d 3e 7a 43 61 6e 6f 6e 69 ndidate->zCanoni
cbf70 63 61 6c 4e 61 6d 65 2c 20 70 4e 65 77 2d 3e 7a calName, pNew->z
cbf80 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e CanonicalName, n
cbf90 29 3d 3d 30 0a 20 20 20 20 29 7b 0a 20 20 20 20 )==0. ){.
cbfa0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
cbfb0 70 4e 65 77 29 3b 0a 20 20 20 20 20 20 20 70 43 pNew);. pC
cbfc0 61 6e 64 69 64 61 74 65 2d 3e 6e 52 65 66 2b 2b andidate->nRef++
cbfd0 3b 0a 20 20 20 20 20 20 20 75 6e 69 78 4c 65 61 ;. unixLea
cbfe0 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 veMutex();.
cbff0 20 20 72 65 74 75 72 6e 20 70 43 61 6e 64 69 64 return pCandid
cc000 61 74 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a ate;. }. }..
cc010 20 20 2f 2a 20 4e 6f 20 6d 61 74 63 68 20 77 61 /* No match wa
cc020 73 20 66 6f 75 6e 64 2e 20 20 57 65 20 77 69 6c s found. We wil
cc030 6c 20 6d 61 6b 65 20 61 20 6e 65 77 20 66 69 6c l make a new fil
cc040 65 20 49 44 20 2a 2f 0a 20 20 70 4e 65 77 2d 3e e ID */. pNew->
cc050 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e 65 77 nRef = 1;. pNew
cc060 2d 3e 6e 4e 61 6d 65 20 3d 20 6e 3b 0a 20 20 70 ->nName = n;. p
cc070 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 76 78 77 New->pNext = vxw
cc080 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b 0a 20 20 orksFileList;.
cc090 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 20 vxworksFileList
cc0a0 3d 20 70 4e 65 77 3b 0a 20 20 75 6e 69 78 4c 65 = pNew;. unixLe
cc0b0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 aveMutex();. re
cc0c0 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a turn pNew;.}../*
cc0d0 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 .** Decrement th
cc0e0 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e e reference coun
cc0f0 74 20 6f 6e 20 61 20 76 78 77 6f 72 6b 73 46 69 t on a vxworksFi
cc100 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20 20 46 72 leId object. Fr
cc110 65 65 0a 2a 2a 20 74 68 65 20 6f 62 6a 65 63 74 ee.** the object
cc120 20 77 68 65 6e 20 74 68 65 20 72 65 66 65 72 65 when the refere
cc130 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68 65 nce count reache
cc140 73 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 s zero..*/.stati
cc150 63 20 76 6f 69 64 20 76 78 77 6f 72 6b 73 52 65 c void vxworksRe
cc160 6c 65 61 73 65 46 69 6c 65 49 64 28 73 74 72 75 leaseFileId(stru
cc170 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 ct vxworksFileId
cc180 20 2a 70 49 64 29 7b 0a 20 20 75 6e 69 78 45 6e *pId){. unixEn
cc190 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 61 73 terMutex();. as
cc1a0 73 65 72 74 28 20 70 49 64 2d 3e 6e 52 65 66 3e sert( pId->nRef>
cc1b0 30 20 29 3b 0a 20 20 70 49 64 2d 3e 6e 52 65 66 0 );. pId->nRef
cc1c0 2d 2d 3b 0a 20 20 69 66 28 20 70 49 64 2d 3e 6e --;. if( pId->n
cc1d0 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 Ref==0 ){. st
cc1e0 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 ruct vxworksFile
cc1f0 49 64 20 2a 2a 70 70 3b 0a 20 20 20 20 66 6f 72 Id **pp;. for
cc200 28 70 70 3d 26 76 78 77 6f 72 6b 73 46 69 6c 65 (pp=&vxworksFile
cc210 4c 69 73 74 3b 20 2a 70 70 20 26 26 20 2a 70 70 List; *pp && *pp
cc220 21 3d 70 49 64 3b 20 70 70 20 3d 20 26 28 28 2a !=pId; pp = &((*
cc230 70 70 29 2d 3e 70 4e 65 78 74 29 29 7b 7d 0a 20 pp)->pNext)){}.
cc240 20 20 20 61 73 73 65 72 74 28 20 2a 70 70 3d 3d assert( *pp==
cc250 70 49 64 20 29 3b 0a 20 20 20 20 2a 70 70 20 3d pId );. *pp =
cc260 20 70 49 64 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 pId->pNext;.
cc270 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49 sqlite3_free(pI
cc280 64 29 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 d);. }. unixLe
cc290 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 23 65 aveMutex();.}.#e
cc2a0 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 ndif /* OS_VXWOR
cc2b0 4b 53 20 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a KS */./*********
cc2c0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 55 6e ****** End of Un
cc2d0 69 71 75 65 20 46 69 6c 65 20 49 44 20 55 74 69 ique File ID Uti
cc2e0 6c 69 74 79 20 55 73 65 64 20 42 79 20 56 78 57 lity Used By VxW
cc2f0 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a orks ***********
cc300 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
cc310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc350 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a ****/.../*******
cc360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc3a0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
cc3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc3c0 2a 2a 2a 20 50 6f 73 69 78 20 41 64 76 69 73 6f *** Posix Adviso
cc3d0 72 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a ry Locking *****
cc3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc3f0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 4f *******.**.** PO
cc400 53 49 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 SIX advisory loc
cc410 6b 73 20 61 72 65 20 62 72 6f 6b 65 6e 20 62 79 ks are broken by
cc420 20 64 65 73 69 67 6e 2e 20 20 41 4e 53 49 20 53 design. ANSI S
cc430 54 44 20 31 30 30 33 2e 31 20 28 31 39 39 36 29 TD 1003.1 (1996)
cc440 0a 2a 2a 20 73 65 63 74 69 6f 6e 20 36 2e 35 2e .** section 6.5.
cc450 32 2e 32 20 6c 69 6e 65 73 20 34 38 33 20 74 68 2.2 lines 483 th
cc460 72 6f 75 67 68 20 34 39 30 20 73 70 65 63 69 66 rough 490 specif
cc470 79 20 74 68 61 74 20 77 68 65 6e 20 61 20 70 72 y that when a pr
cc480 6f 63 65 73 73 0a 2a 2a 20 73 65 74 73 20 6f 72 ocess.** sets or
cc490 20 63 6c 65 61 72 73 20 61 20 6c 6f 63 6b 2c 20 clears a lock,
cc4a0 74 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f that operation o
cc4b0 76 65 72 72 69 64 65 73 20 61 6e 79 20 70 72 69 verrides any pri
cc4c0 6f 72 20 6c 6f 63 6b 73 20 73 65 74 0a 2a 2a 20 or locks set.**
cc4d0 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 by the same proc
cc4e0 65 73 73 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f ess. It does no
cc4f0 74 20 65 78 70 6c 69 63 69 74 6c 79 20 73 61 79 t explicitly say
cc500 20 73 6f 2c 20 62 75 74 20 74 68 69 73 20 69 6d so, but this im
cc510 70 6c 69 65 73 0a 2a 2a 20 74 68 61 74 20 69 74 plies.** that it
cc520 20 6f 76 65 72 72 69 64 65 73 20 6c 6f 63 6b 73 overrides locks
cc530 20 73 65 74 20 62 79 20 74 68 65 20 73 61 6d 65 set by the same
cc540 20 70 72 6f 63 65 73 73 20 75 73 69 6e 67 20 61 process using a
cc550 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 69 different.** fi
cc560 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 le descriptor.
cc570 43 6f 6e 73 69 64 65 72 20 74 68 69 73 20 74 65 Consider this te
cc580 73 74 20 63 61 73 65 3a 0a 2a 2a 0a 2a 2a 20 20 st case:.**.**
cc590 20 20 20 20 20 69 6e 74 20 66 64 31 20 3d 20 6f int fd1 = o
cc5a0 70 65 6e 28 22 2e 2f 66 69 6c 65 31 22 2c 20 4f pen("./file1", O
cc5b0 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 _RDWR|O_CREAT, 0
cc5c0 36 34 34 29 3b 0a 2a 2a 20 20 20 20 20 20 20 69 644);.** i
cc5d0 6e 74 20 66 64 32 20 3d 20 6f 70 65 6e 28 22 2e nt fd2 = open(".
cc5e0 2f 66 69 6c 65 32 22 2c 20 4f 5f 52 44 57 52 7c /file2", O_RDWR|
cc5f0 4f 5f 43 52 45 41 54 2c 20 30 36 34 34 29 3b 0a O_CREAT, 0644);.
cc600 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 2e 2f **.** Suppose ./
cc610 66 69 6c 65 31 20 61 6e 64 20 2e 2f 66 69 6c 65 file1 and ./file
cc620 32 20 61 72 65 20 72 65 61 6c 6c 79 20 74 68 65 2 are really the
cc630 20 73 61 6d 65 20 66 69 6c 65 20 28 62 65 63 61 same file (beca
cc640 75 73 65 0a 2a 2a 20 6f 6e 65 20 69 73 20 61 20 use.** one is a
cc650 68 61 72 64 20 6f 72 20 73 79 6d 62 6f 6c 69 63 hard or symbolic
cc660 20 6c 69 6e 6b 20 74 6f 20 74 68 65 20 6f 74 68 link to the oth
cc670 65 72 29 20 74 68 65 6e 20 69 66 20 79 6f 75 20 er) then if you
cc680 73 65 74 0a 2a 2a 20 61 6e 20 65 78 63 6c 75 73 set.** an exclus
cc690 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 66 64 31 2c ive lock on fd1,
cc6a0 20 74 68 65 6e 20 74 72 79 20 74 6f 20 67 65 74 then try to get
cc6b0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
cc6c0 63 6b 0a 2a 2a 20 6f 6e 20 66 64 32 2c 20 69 74 ck.** on fd2, it
cc6d0 20 77 6f 72 6b 73 2e 20 20 49 20 77 6f 75 6c 64 works. I would
cc6e0 20 68 61 76 65 20 65 78 70 65 63 74 65 64 20 74 have expected t
cc6f0 68 65 20 73 65 63 6f 6e 64 20 6c 6f 63 6b 20 74 he second lock t
cc700 6f 0a 2a 2a 20 66 61 69 6c 20 73 69 6e 63 65 20 o.** fail since
cc710 74 68 65 72 65 20 77 61 73 20 61 6c 72 65 61 64 there was alread
cc720 79 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 y a lock on the
cc730 66 69 6c 65 20 64 75 65 20 74 6f 20 66 64 31 2e file due to fd1.
cc740 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 73 6f 2e 20 .** But not so.
cc750 20 53 69 6e 63 65 20 62 6f 74 68 20 6c 6f 63 6b Since both lock
cc760 73 20 63 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 s came from the
cc770 73 61 6d 65 20 70 72 6f 63 65 73 73 2c 20 74 68 same process, th
cc780 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 6f 76 65 72 e.** second over
cc790 72 69 64 65 73 20 74 68 65 20 66 69 72 73 74 2c rides the first,
cc7a0 20 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 even though the
cc7b0 79 20 77 65 72 65 20 6f 6e 20 64 69 66 66 65 72 y were on differ
cc7c0 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 ent.** file desc
cc7d0 72 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20 6f riptors opened o
cc7e0 6e 20 64 69 66 66 65 72 65 6e 74 20 66 69 6c 65 n different file
cc7f0 20 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 names..**.** Th
cc800 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 is means that we
cc810 20 63 61 6e 6e 6f 74 20 75 73 65 20 50 4f 53 49 cannot use POSI
cc820 58 20 6c 6f 63 6b 73 20 74 6f 20 73 79 6e 63 68 X locks to synch
cc830 72 6f 6e 69 7a 65 20 66 69 6c 65 20 61 63 63 65 ronize file acce
cc840 73 73 0a 2a 2a 20 61 6d 6f 6e 67 20 63 6f 6d 70 ss.** among comp
cc850 65 74 69 6e 67 20 74 68 72 65 61 64 73 20 6f 66 eting threads of
cc860 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 the same proces
cc870 73 2e 20 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 s. POSIX locks
cc880 77 69 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 0a 2a will work fine.*
cc890 2a 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a 65 * to synchronize
cc8a0 20 61 63 63 65 73 73 20 66 6f 72 20 74 68 72 65 access for thre
cc8b0 61 64 73 20 69 6e 20 73 65 70 61 72 61 74 65 20 ads in separate
cc8c0 70 72 6f 63 65 73 73 65 73 2c 20 62 75 74 20 6e processes, but n
cc8d0 6f 74 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 69 ot.** threads wi
cc8e0 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20 70 72 thin the same pr
cc8f0 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 ocess..**.** To
cc900 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 work around the
cc910 70 72 6f 62 6c 65 6d 2c 20 53 51 4c 69 74 65 20 problem, SQLite
cc920 68 61 73 20 74 6f 20 6d 61 6e 61 67 65 20 66 69 has to manage fi
cc930 6c 65 20 6c 6f 63 6b 73 20 69 6e 74 65 72 6e 61 le locks interna
cc940 6c 6c 79 0a 2a 2a 20 6f 6e 20 69 74 73 20 6f 77 lly.** on its ow
cc950 6e 2e 20 20 57 68 65 6e 65 76 65 72 20 61 20 6e n. Whenever a n
cc960 65 77 20 64 61 74 61 62 61 73 65 20 69 73 20 6f ew database is o
cc970 70 65 6e 65 64 2c 20 77 65 20 68 61 76 65 20 74 pened, we have t
cc980 6f 20 66 69 6e 64 20 74 68 65 0a 2a 2a 20 73 70 o find the.** sp
cc990 65 63 69 66 69 63 20 69 6e 6f 64 65 20 6f 66 20 ecific inode of
cc9a0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
cc9b0 65 20 28 74 68 65 20 69 6e 6f 64 65 20 69 73 20 e (the inode is
cc9c0 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 determined by th
cc9d0 65 0a 2a 2a 20 73 74 5f 64 65 76 20 61 6e 64 20 e.** st_dev and
cc9e0 73 74 5f 69 6e 6f 20 66 69 65 6c 64 73 20 6f 66 st_ino fields of
cc9f0 20 74 68 65 20 73 74 61 74 20 73 74 72 75 63 74 the stat struct
cca00 75 72 65 20 74 68 61 74 20 66 73 74 61 74 28 29 ure that fstat()
cca10 20 66 69 6c 6c 73 20 69 6e 29 0a 2a 2a 20 61 6e fills in).** an
cca20 64 20 63 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b d check for lock
cca30 73 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 69 s already existi
cca40 6e 67 20 6f 6e 20 74 68 61 74 20 69 6e 6f 64 65 ng on that inode
cca50 2e 20 20 57 68 65 6e 20 6c 6f 63 6b 73 20 61 72 . When locks ar
cca60 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 6f 72 20 e.** created or
cca70 72 65 6d 6f 76 65 64 2c 20 77 65 20 68 61 76 65 removed, we have
cca80 20 74 6f 20 6c 6f 6f 6b 20 61 74 20 6f 75 72 20 to look at our
cca90 6f 77 6e 20 69 6e 74 65 72 6e 61 6c 20 72 65 63 own internal rec
ccaa0 6f 72 64 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 6f ord of the.** lo
ccab0 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 61 6e cks to see if an
ccac0 6f 74 68 65 72 20 74 68 72 65 61 64 20 68 61 73 other thread has
ccad0 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20 previously set
ccae0 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20 73 a lock on that s
ccaf0 61 6d 65 0a 2a 2a 20 69 6e 6f 64 65 2e 0a 2a 2a ame.** inode..**
ccb00 0a 2a 2a 20 28 41 73 69 64 65 3a 20 54 68 65 20 .** (Aside: The
ccb10 75 73 65 20 6f 66 20 69 6e 6f 64 65 20 6e 75 6d use of inode num
ccb20 62 65 72 73 20 61 73 20 75 6e 69 71 75 65 20 49 bers as unique I
ccb30 44 73 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b Ds does not work
ccb40 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 on VxWorks..**
ccb50 46 6f 72 20 56 78 57 6f 72 6b 73 2c 20 77 65 20 For VxWorks, we
ccb60 68 61 76 65 20 74 6f 20 75 73 65 20 74 68 65 20 have to use the
ccb70 61 6c 74 65 72 6e 61 74 69 76 65 20 75 6e 69 71 alternative uniq
ccb80 75 65 20 49 44 20 73 79 73 74 65 6d 20 62 61 73 ue ID system bas
ccb90 65 64 20 6f 6e 0a 2a 2a 20 63 61 6e 6f 6e 69 63 ed on.** canonic
ccba0 61 6c 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 al filename and
ccbb0 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 implemented in t
ccbc0 68 65 20 70 72 65 76 69 6f 75 73 20 64 69 76 69 he previous divi
ccbd0 73 69 6f 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 sion.).**.** The
ccbe0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 sqlite3_file st
ccbf0 72 75 63 74 75 72 65 20 66 6f 72 20 50 4f 53 49 ructure for POSI
ccc00 58 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6a X is no longer j
ccc10 75 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20 66 ust an integer f
ccc20 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f ile.** descripto
ccc30 72 2e 20 20 49 74 20 69 73 20 6e 6f 77 20 61 20 r. It is now a
ccc40 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 68 structure that h
ccc50 6f 6c 64 73 20 74 68 65 20 69 6e 74 65 67 65 72 olds the integer
ccc60 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 file.** descrip
ccc70 74 6f 72 20 61 6e 64 20 61 20 70 6f 69 6e 74 65 tor and a pointe
ccc80 72 20 74 6f 20 61 20 73 74 72 75 63 74 75 72 65 r to a structure
ccc90 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 that describes
ccca0 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 the internal.**
cccb0 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 63 6f 72 locks on the cor
cccc0 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e 6f 64 65 responding inode
cccd0 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 . There is one
ccce0 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 locking structur
cccf0 65 0a 2a 2a 20 70 65 72 20 69 6e 6f 64 65 2c 20 e.** per inode,
ccd00 73 6f 20 69 66 20 74 68 65 20 73 61 6d 65 20 69 so if the same i
ccd10 6e 6f 64 65 20 69 73 20 6f 70 65 6e 65 64 20 74 node is opened t
ccd20 77 69 63 65 2c 20 62 6f 74 68 20 75 6e 69 78 46 wice, both unixF
ccd30 69 6c 65 20 73 74 72 75 63 74 75 72 65 73 0a 2a ile structures.*
ccd40 2a 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 * point to the s
ccd50 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 ame locking stru
ccd60 63 74 75 72 65 2e 20 20 54 68 65 20 6c 6f 63 6b cture. The lock
ccd70 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b 65 ing structure ke
ccd80 65 70 73 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e eps.** a referen
ccd90 63 65 20 63 6f 75 6e 74 20 28 73 6f 20 77 65 20 ce count (so we
ccda0 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 6e 20 74 will know when t
ccdb0 6f 20 64 65 6c 65 74 65 20 69 74 29 20 61 6e 64 o delete it) and
ccdc0 20 61 20 22 63 6e 74 22 0a 2a 2a 20 66 69 65 6c a "cnt".** fiel
ccdd0 64 20 74 68 61 74 20 74 65 6c 6c 73 20 75 73 20 d that tells us
ccde0 69 74 73 20 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 its internal loc
ccdf0 6b 20 73 74 61 74 75 73 2e 20 20 63 6e 74 3d 3d k status. cnt==
cce00 30 20 6d 65 61 6e 73 20 74 68 65 0a 2a 2a 20 66 0 means the.** f
cce10 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2e ile is unlocked.
cce20 20 20 63 6e 74 3d 3d 2d 31 20 6d 65 61 6e 73 20 cnt==-1 means
cce30 74 68 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20 the file has an
cce40 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 0a exclusive lock..
cce50 2a 2a 20 63 6e 74 3e 30 20 6d 65 61 6e 73 20 74 ** cnt>0 means t
cce60 68 65 72 65 20 61 72 65 20 63 6e 74 20 73 68 61 here are cnt sha
cce70 72 65 64 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 red locks on the
cce80 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 file..**.** Any
cce90 20 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 6b attempt to lock
ccea0 20 6f 72 20 75 6e 6c 6f 63 6b 20 61 20 66 69 6c or unlock a fil
cceb0 65 20 66 69 72 73 74 20 63 68 65 63 6b 73 20 74 e first checks t
ccec0 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 he locking.** st
cced0 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 66 63 ructure. The fc
ccee0 6e 74 6c 28 29 20 73 79 73 74 65 6d 20 63 61 6c ntl() system cal
ccef0 6c 20 69 73 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65 l is only invoke
ccf00 64 20 74 6f 20 73 65 74 20 61 20 0a 2a 2a 20 50 d to set a .** P
ccf10 4f 53 49 58 20 6c 6f 63 6b 20 69 66 20 74 68 65 OSIX lock if the
ccf20 20 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 internal lock s
ccf30 74 72 75 63 74 75 72 65 20 74 72 61 6e 73 69 74 tructure transit
ccf40 69 6f 6e 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 ions between.**
ccf50 61 20 6c 6f 63 6b 65 64 20 61 6e 64 20 61 6e 20 a locked and an
ccf60 75 6e 6c 6f 63 6b 65 64 20 73 74 61 74 65 2e 0a unlocked state..
ccf70 2a 2a 0a 2a 2a 20 42 75 74 20 77 61 69 74 3a 20 **.** But wait:
ccf80 20 74 68 65 72 65 20 61 72 65 20 79 65 74 20 6d there are yet m
ccf90 6f 72 65 20 70 72 6f 62 6c 65 6d 73 20 77 69 74 ore problems wit
ccfa0 68 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 h POSIX advisory
ccfb0 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 locks..**.** If
ccfc0 20 79 6f 75 20 63 6c 6f 73 65 20 61 20 66 69 6c you close a fil
ccfd0 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 61 e descriptor tha
ccfe0 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 66 69 t points to a fi
ccff0 6c 65 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b le that has lock
cd000 73 2c 0a 2a 2a 20 61 6c 6c 20 6c 6f 63 6b 73 20 s,.** all locks
cd010 6f 6e 20 74 68 61 74 20 66 69 6c 65 20 74 68 61 on that file tha
cd020 74 20 61 72 65 20 6f 77 6e 65 64 20 62 79 20 74 t are owned by t
cd030 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65 he current proce
cd040 73 73 20 61 72 65 0a 2a 2a 20 72 65 6c 65 61 73 ss are.** releas
cd050 65 64 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f ed. To work aro
cd060 75 6e 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d und this problem
cd070 2c 20 65 61 63 68 20 75 6e 69 78 46 69 6c 65 20 , each unixFile
cd080 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 structure contai
cd090 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 ns.** a pointer
cd0a0 74 6f 20 61 6e 20 75 6e 69 78 4f 70 65 6e 43 6e to an unixOpenCn
cd0b0 74 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 t structure. Th
cd0c0 65 72 65 20 69 73 20 6f 6e 65 20 75 6e 69 78 4f ere is one unixO
cd0d0 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 penCnt structure
cd0e0 0a 2a 2a 20 70 65 72 20 6f 70 65 6e 20 69 6e 6f .** per open ino
cd0f0 64 65 2c 20 77 68 69 63 68 20 6d 65 61 6e 73 20 de, which means
cd100 74 68 61 74 20 6d 75 6c 74 69 70 6c 65 20 75 6e that multiple un
cd110 69 78 46 69 6c 65 20 63 61 6e 20 70 6f 69 6e 74 ixFile can point
cd120 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 to a single.**
cd130 75 6e 69 78 4f 70 65 6e 43 6e 74 2e 20 20 57 68 unixOpenCnt. Wh
cd140 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 en an attempt is
cd150 20 6d 61 64 65 20 74 6f 20 63 6c 6f 73 65 20 61 made to close a
cd160 6e 20 75 6e 69 78 46 69 6c 65 2c 20 69 66 20 74 n unixFile, if t
cd170 68 65 72 65 20 61 72 65 0a 2a 2a 20 6f 74 68 65 here are.** othe
cd180 72 20 75 6e 69 78 46 69 6c 65 20 6f 70 65 6e 20 r unixFile open
cd190 6f 6e 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64 on the same inod
cd1a0 65 20 74 68 61 74 20 61 72 65 20 68 6f 6c 64 69 e that are holdi
cd1b0 6e 67 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 61 ng locks, the ca
cd1c0 6c 6c 0a 2a 2a 20 74 6f 20 63 6c 6f 73 65 28 29 ll.** to close()
cd1d0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
cd1e0 70 74 6f 72 20 69 73 20 64 65 66 65 72 72 65 64 ptor is deferred
cd1f0 20 75 6e 74 69 6c 20 61 6c 6c 20 6f 66 20 74 68 until all of th
cd200 65 20 6c 6f 63 6b 73 20 63 6c 65 61 72 2e 0a 2a e locks clear..*
cd210 2a 20 54 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e * The unixOpenCn
cd220 74 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 t structure keep
cd230 73 20 61 20 6c 69 73 74 20 6f 66 20 66 69 6c 65 s a list of file
cd240 20 64 65 73 63 72 69 70 74 6f 72 73 20 74 68 61 descriptors tha
cd250 74 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 62 65 20 t need to.** be
cd260 63 6c 6f 73 65 64 20 61 6e 64 20 74 68 61 74 20 closed and that
cd270 6c 69 73 74 20 69 73 20 77 61 6c 6b 65 64 20 28 list is walked (
cd280 61 6e 64 20 63 6c 65 61 72 65 64 29 20 77 68 65 and cleared) whe
cd290 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 0a n the last lock.
cd2a0 2a 2a 20 63 6c 65 61 72 73 2e 0a 2a 2a 0a 2a 2a ** clears..**.**
cd2b0 20 59 65 74 20 61 6e 6f 74 68 65 72 20 70 72 6f Yet another pro
cd2c0 62 6c 65 6d 3a 20 20 4c 69 6e 75 78 54 68 72 65 blem: LinuxThre
cd2d0 61 64 73 20 64 6f 20 6e 6f 74 20 70 6c 61 79 20 ads do not play
cd2e0 77 65 6c 6c 20 77 69 74 68 20 70 6f 73 69 78 20 well with posix
cd2f0 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 4d 61 6e locks..**.** Man
cd300 79 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 y older versions
cd310 20 6f 66 20 6c 69 6e 75 78 20 75 73 65 20 74 68 of linux use th
cd320 65 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 6c e LinuxThreads l
cd330 69 62 72 61 72 79 20 77 68 69 63 68 20 69 73 0a ibrary which is.
cd340 2a 2a 20 6e 6f 74 20 70 6f 73 69 78 20 63 6f 6d ** not posix com
cd350 70 6c 69 61 6e 74 2e 20 20 55 6e 64 65 72 20 4c pliant. Under L
cd360 69 6e 75 78 54 68 72 65 61 64 73 2c 20 61 20 6c inuxThreads, a l
cd370 6f 63 6b 20 63 72 65 61 74 65 64 20 62 79 20 74 ock created by t
cd380 68 72 65 61 64 0a 2a 2a 20 41 20 63 61 6e 6e 6f hread.** A canno
cd390 74 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6f 72 t be modified or
cd3a0 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 61 overridden by a
cd3b0 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 different threa
cd3c0 64 20 42 2e 0a 2a 2a 20 4f 6e 6c 79 20 74 68 72 d B..** Only thr
cd3d0 65 61 64 20 41 20 63 61 6e 20 6d 6f 64 69 66 79 ead A can modify
cd3e0 20 74 68 65 20 6c 6f 63 6b 2e 20 20 4c 6f 63 6b the lock. Lock
cd3f0 69 6e 67 20 62 65 68 61 76 69 6f 72 20 69 73 20 ing behavior is
cd400 63 6f 72 72 65 63 74 0a 2a 2a 20 69 66 20 74 68 correct.** if th
cd410 65 20 61 70 70 6c 69 61 74 69 6f 6e 20 75 73 65 e appliation use
cd420 73 20 74 68 65 20 6e 65 77 65 72 20 4e 61 74 69 s the newer Nati
cd430 76 65 20 50 6f 73 69 78 20 54 68 72 65 61 64 20 ve Posix Thread
cd440 4c 69 62 72 61 72 79 20 28 4e 50 54 4c 29 0a 2a Library (NPTL).*
cd450 2a 20 6f 6e 20 6c 69 6e 75 78 20 2d 20 77 69 74 * on linux - wit
cd460 68 20 4e 50 54 4c 20 61 20 6c 6f 63 6b 20 63 72 h NPTL a lock cr
cd470 65 61 74 65 64 20 62 79 20 74 68 72 65 61 64 20 eated by thread
cd480 41 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 6c A can override l
cd490 6f 63 6b 73 0a 2a 2a 20 69 6e 20 74 68 72 65 61 ocks.** in threa
cd4a0 64 20 42 2e 20 20 42 75 74 20 74 68 65 72 65 20 d B. But there
cd4b0 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b 6e 6f is no way to kno
cd4c0 77 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d w at compile-tim
cd4d0 65 20 77 68 69 63 68 0a 2a 2a 20 74 68 72 65 61 e which.** threa
cd4e0 64 69 6e 67 20 6c 69 62 72 61 72 79 20 69 73 20 ding library is
cd4f0 62 65 69 6e 67 20 75 73 65 64 2e 20 20 53 6f 20 being used. So
cd500 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 there is no way
cd510 74 6f 20 6b 6e 6f 77 20 61 74 0a 2a 2a 20 63 6f to know at.** co
cd520 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 65 74 68 mpile-time wheth
cd530 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 er or not thread
cd540 20 41 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 A can override
cd550 6c 6f 63 6b 73 20 6f 6e 20 74 68 72 65 61 64 20 locks on thread
cd560 42 2e 0a 2a 2a 20 57 65 20 68 61 76 65 20 74 6f B..** We have to
cd570 20 64 6f 20 61 20 72 75 6e 2d 74 69 6d 65 20 63 do a run-time c
cd580 68 65 63 6b 20 74 6f 20 64 69 73 63 6f 76 65 72 heck to discover
cd590 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 the behavior of
cd5a0 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 the.** current
cd5b0 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4f process..**.** O
cd5c0 6e 20 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 n systems where
cd5d0 74 68 72 65 61 64 20 41 20 69 73 20 75 6e 61 62 thread A is unab
cd5e0 6c 65 20 74 6f 20 6d 6f 64 69 66 79 20 6c 6f 63 le to modify loc
cd5f0 6b 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a ks created by.**
cd600 20 74 68 72 65 61 64 20 42 2c 20 77 65 20 68 61 thread B, we ha
cd610 76 65 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b ve to keep track
cd620 20 6f 66 20 77 68 69 63 68 20 74 68 72 65 61 64 of which thread
cd630 20 63 72 65 61 74 65 64 20 65 61 63 68 0a 2a 2a created each.**
cd640 20 6c 6f 63 6b 2e 20 20 48 65 6e 63 65 20 74 68 lock. Hence th
cd650 65 72 65 20 69 73 20 61 6e 20 65 78 74 72 61 20 ere is an extra
cd660 66 69 65 6c 64 20 69 6e 20 74 68 65 20 6b 65 79 field in the key
cd670 20 74 6f 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b to the unixLock
cd680 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 Info.** structur
cd690 65 20 74 6f 20 72 65 63 6f 72 64 20 74 68 69 73 e to record this
cd6a0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 41 information. A
cd6b0 6e 64 20 6f 6e 20 74 68 6f 73 65 20 73 79 73 74 nd on those syst
cd6c0 65 6d 73 20 69 74 0a 2a 2a 20 69 73 20 69 6c 6c ems it.** is ill
cd6d0 65 67 61 6c 20 74 6f 20 62 65 67 69 6e 20 61 20 egal to begin a
cd6e0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 6f transaction in o
cd6f0 6e 65 20 74 68 72 65 61 64 20 61 6e 64 20 66 69 ne thread and fi
cd700 6e 69 73 68 20 69 74 0a 2a 2a 20 69 6e 20 61 6e nish it.** in an
cd710 6f 74 68 65 72 2e 20 20 46 6f 72 20 74 68 69 73 other. For this
cd720 20 6c 61 74 74 65 72 20 72 65 73 74 72 69 63 74 latter restrict
cd730 69 6f 6e 2c 20 74 68 65 72 65 20 69 73 20 6e 6f ion, there is no
cd740 20 77 6f 72 6b 2d 61 72 6f 75 6e 64 2e 0a 2a 2a work-around..**
cd750 20 49 74 20 69 73 20 61 20 6c 69 6d 69 74 61 74 It is a limitat
cd760 69 6f 6e 20 6f 66 20 4c 69 6e 75 78 54 68 72 65 ion of LinuxThre
cd770 61 64 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 ads..*/../*.** S
cd780 65 74 20 6f 72 20 63 68 65 63 6b 20 74 68 65 20 et or check the
cd790 75 6e 69 78 46 69 6c 65 2e 74 69 64 20 66 69 65 unixFile.tid fie
cd7a0 6c 64 2e 20 20 54 68 69 73 20 66 69 65 6c 64 20 ld. This field
cd7b0 69 73 20 73 65 74 20 77 68 65 6e 20 61 6e 20 75 is set when an u
cd7c0 6e 69 78 46 69 6c 65 0a 2a 2a 20 69 73 20 66 69 nixFile.** is fi
cd7d0 72 73 74 20 6f 70 65 6e 65 64 2e 20 20 41 6c 6c rst opened. All
cd7e0 20 73 75 62 73 65 71 75 65 6e 74 20 75 73 65 73 subsequent uses
cd7f0 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 of the unixFile
cd800 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68 65 verify that the
cd810 0a 2a 2a 20 73 61 6d 65 20 74 68 72 65 61 64 20 .** same thread
cd820 69 73 20 6f 70 65 72 61 74 69 6e 67 20 6f 6e 20 is operating on
cd830 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 20 20 53 the unixFile. S
cd840 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 ome operating sy
cd850 73 74 65 6d 73 20 64 6f 0a 2a 2a 20 6e 6f 74 20 stems do.** not
cd860 61 6c 6c 6f 77 20 6c 6f 63 6b 73 20 74 6f 20 62 allow locks to b
cd870 65 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 e overridden by
cd880 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 61 6e other threads an
cd890 64 20 74 68 61 74 20 72 65 73 74 72 69 63 74 69 d that restricti
cd8a0 6f 6e 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 on.** means that
cd8b0 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 61 sqlite3* databa
cd8c0 73 65 20 68 61 6e 64 6c 65 73 20 63 61 6e 6e 6f se handles canno
cd8d0 74 20 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20 t be moved from
cd8e0 6f 6e 65 20 74 68 72 65 61 64 0a 2a 2a 20 74 6f one thread.** to
cd8f0 20 61 6e 6f 74 68 65 72 20 77 68 69 6c 65 20 6c another while l
cd900 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 2e 0a 2a ocks are held..*
cd910 2a 0a 2a 2a 20 56 65 72 73 69 6f 6e 20 33 2e 33 *.** Version 3.3
cd920 2e 31 20 28 32 30 30 36 2d 30 31 2d 31 35 29 3a .1 (2006-01-15):
cd930 20 20 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 62 unixFile can b
cd940 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f 6e 65 e moved from one
cd950 20 74 68 72 65 61 64 20 74 6f 0a 2a 2a 20 61 6e thread to.** an
cd960 6f 74 68 65 72 20 61 73 20 6c 6f 6e 67 20 61 73 other as long as
cd970 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 we are running
cd980 6f 6e 20 61 20 73 79 73 74 65 6d 20 74 68 61 74 on a system that
cd990 20 73 75 70 70 6f 72 74 73 20 74 68 72 65 61 64 supports thread
cd9a0 73 0a 2a 2a 20 6f 76 65 72 72 69 64 69 6e 67 20 s.** overriding
cd9b0 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b each others lock
cd9c0 73 20 28 77 68 69 63 68 20 69 73 20 6e 6f 77 20 s (which is now
cd9d0 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 20 the most common
cd9e0 62 65 68 61 76 69 6f 72 29 0a 2a 2a 20 6f 72 20 behavior).** or
cd9f0 69 66 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 if no locks are
cda00 68 65 6c 64 2e 20 20 42 75 74 20 74 68 65 20 75 held. But the u
cda10 6e 69 78 46 69 6c 65 2e 70 4c 6f 63 6b 20 66 69 nixFile.pLock fi
cda20 65 6c 64 20 6e 65 65 64 73 20 74 6f 20 62 65 0a eld needs to be.
cda30 2a 2a 20 72 65 63 6f 6d 70 75 74 65 64 20 62 65 ** recomputed be
cda40 63 61 75 73 65 20 69 74 73 20 6b 65 79 20 69 6e cause its key in
cda50 63 6c 75 64 65 73 20 74 68 65 20 74 68 72 65 61 cludes the threa
cda60 64 2d 69 64 2e 20 20 53 65 65 20 74 68 65 20 0a d-id. See the .
cda70 2a 2a 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 ** transferOwner
cda80 73 68 69 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 ship() function
cda90 62 65 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 69 below for additi
cdaa0 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
cdab0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 .*/.#if SQLITE_T
cdac0 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 HREADSAFE && def
cdad0 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a ined(__linux__).
cdae0 23 20 64 65 66 69 6e 65 20 53 45 54 5f 54 48 52 # define SET_THR
cdaf0 45 41 44 49 44 28 58 29 20 20 20 28 58 29 2d 3e EADID(X) (X)->
cdb00 74 69 64 20 3d 20 70 74 68 72 65 61 64 5f 73 65 tid = pthread_se
cdb10 6c 66 28 29 0a 23 20 64 65 66 69 6e 65 20 43 48 lf().# define CH
cdb20 45 43 4b 5f 54 48 52 45 41 44 49 44 28 58 29 20 ECK_THREADID(X)
cdb30 28 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 (threadsOverride
cdb40 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3d EachOthersLocks=
cdb50 3d 30 20 26 26 20 5c 0a 20 20 20 20 20 20 20 20 =0 && \.
cdb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cdb70 20 20 20 20 21 70 74 68 72 65 61 64 5f 65 71 75 !pthread_equ
cdb80 61 6c 28 28 58 29 2d 3e 74 69 64 2c 20 70 74 68 al((X)->tid, pth
cdb90 72 65 61 64 5f 73 65 6c 66 28 29 29 29 0a 23 65 read_self())).#e
cdba0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 45 54 lse.# define SET
cdbb0 5f 54 48 52 45 41 44 49 44 28 58 29 0a 23 20 64 _THREADID(X).# d
cdbc0 65 66 69 6e 65 20 43 48 45 43 4b 5f 54 48 52 45 efine CHECK_THRE
cdbd0 41 44 49 44 28 58 29 20 30 0a 23 65 6e 64 69 66 ADID(X) 0.#endif
cdbe0 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
cdbf0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
cdc00 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 73 wing structure s
cdc10 65 72 76 65 73 20 61 73 20 74 68 65 20 6b 65 79 erves as the key
cdc20 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 used.** to loca
cdc30 74 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 te a particular
cdc40 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 unixOpenCnt stru
cdc50 63 74 75 72 65 20 67 69 76 65 6e 20 69 74 73 20 cture given its
cdc60 69 6e 6f 64 65 2e 20 20 54 68 69 73 0a 2a 2a 20 inode. This.**
cdc70 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 is the same as t
cdc80 68 65 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 65 he unixLockKey e
cdc90 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 74 xcept that the t
cdca0 68 72 65 61 64 20 49 44 20 69 73 20 6f 6d 69 74 hread ID is omit
cdcb0 74 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 ted..*/.struct u
cdcc0 6e 69 78 46 69 6c 65 49 64 20 7b 0a 20 20 64 65 nixFileId {. de
cdcd0 76 5f 74 20 64 65 76 3b 20 20 20 20 20 20 20 20 v_t dev;
cdce0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 76 /* Dev
cdcf0 69 63 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 69 ice number */.#i
cdd00 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 f OS_VXWORKS. s
cdd10 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c truct vxworksFil
cdd20 65 49 64 20 2a 70 49 64 3b 20 20 2f 2a 20 55 6e eId *pId; /* Un
cdd30 69 71 75 65 20 66 69 6c 65 20 49 44 20 66 6f 72 ique file ID for
cdd40 20 76 78 77 6f 72 6b 73 2e 20 2a 2f 0a 23 65 6c vxworks. */.#el
cdd50 73 65 0a 20 20 69 6e 6f 5f 74 20 69 6e 6f 3b 20 se. ino_t ino;
cdd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cdd70 20 2f 2a 20 49 6e 6f 64 65 20 6e 75 6d 62 65 72 /* Inode number
cdd80 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f */.#endif.};../
cdd90 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
cdda0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
cddb0 67 20 73 74 72 75 63 74 75 72 65 20 73 65 72 76 g structure serv
cddc0 65 73 20 61 73 20 74 68 65 20 6b 65 79 20 75 73 es as the key us
cddd0 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 ed.** to locate
cdde0 61 20 70 61 72 74 69 63 75 6c 61 72 20 75 6e 69 a particular uni
cddf0 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 xLockInfo struct
cde00 75 72 65 20 67 69 76 65 6e 20 69 74 73 20 69 6e ure given its in
cde10 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ode..**.** If th
cde20 72 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65 reads cannot ove
cde30 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 rride each other
cde40 73 20 6c 6f 63 6b 73 20 28 4c 69 6e 75 78 54 68 s locks (LinuxTh
cde50 72 65 61 64 73 29 2c 20 74 68 65 6e 20 77 65 0a reads), then we.
cde60 2a 2a 20 73 65 74 20 74 68 65 20 75 6e 69 78 4c ** set the unixL
cde70 6f 63 6b 4b 65 79 2e 74 69 64 20 66 69 65 6c 64 ockKey.tid field
cde80 20 74 6f 20 74 68 65 20 74 68 72 65 61 64 20 49 to the thread I
cde90 44 2e 20 20 49 66 20 74 68 72 65 61 64 73 20 63 D. If threads c
cdea0 61 6e 20 6f 76 65 72 72 69 64 65 0a 2a 2a 20 65 an override.** e
cdeb0 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 ach others locks
cdec0 20 28 50 6f 73 69 78 20 61 6e 64 20 4e 50 54 4c (Posix and NPTL
cded0 29 20 74 68 65 6e 20 74 69 64 20 69 73 20 61 6c ) then tid is al
cdee0 77 61 79 73 20 73 65 74 20 74 6f 20 7a 65 72 6f ways set to zero
cdef0 2e 0a 2a 2a 20 74 69 64 20 69 73 20 6f 6d 69 74 ..** tid is omit
cdf00 74 65 64 20 69 66 20 77 65 20 63 6f 6d 70 69 6c ted if we compil
cdf10 65 20 77 69 74 68 6f 75 74 20 74 68 72 65 61 64 e without thread
cdf20 69 6e 67 20 73 75 70 70 6f 72 74 20 6f 72 20 6f ing support or o
cdf30 6e 20 61 6e 20 4f 53 0a 2a 2a 20 6f 74 68 65 72 n an OS.** other
cdf40 20 74 68 61 6e 20 6c 69 6e 75 78 2e 0a 2a 2f 0a than linux..*/.
cdf50 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b struct unixLockK
cdf60 65 79 20 7b 0a 20 20 73 74 72 75 63 74 20 75 6e ey {. struct un
cdf70 69 78 46 69 6c 65 49 64 20 66 69 64 3b 20 20 2f ixFileId fid; /
cdf80 2a 20 55 6e 69 71 75 65 20 69 64 65 6e 74 69 66 * Unique identif
cdf90 69 65 72 20 66 6f 72 20 74 68 65 20 66 69 6c 65 ier for the file
cdfa0 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 */.#if SQLITE_T
cdfb0 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 HREADSAFE && def
cdfc0 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a ined(__linux__).
cdfd0 20 20 70 74 68 72 65 61 64 5f 74 20 74 69 64 3b pthread_t tid;
cdfe0 20 20 2f 2a 20 54 68 72 65 61 64 20 49 44 20 6f /* Thread ID o
cdff0 66 20 6c 6f 63 6b 20 6f 77 6e 65 72 2e 20 5a 65 f lock owner. Ze
ce000 72 6f 20 69 66 20 6e 6f 74 20 75 73 69 6e 67 20 ro if not using
ce010 4c 69 6e 75 78 54 68 72 65 61 64 73 20 2a 2f 0a LinuxThreads */.
ce020 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a #endif.};../*.**
ce030 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
ce040 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
ce050 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 ructure is alloc
ce060 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 6f 70 ated for each op
ce070 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 4f 72 en.** inode. Or
ce080 2c 20 6f 6e 20 4c 69 6e 75 78 54 68 72 65 61 64 , on LinuxThread
ce090 73 2c 20 74 68 65 72 65 20 69 73 20 6f 6e 65 20 s, there is one
ce0a0 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 of these structu
ce0b0 72 65 73 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 res for.** each
ce0c0 69 6e 6f 64 65 20 6f 70 65 6e 65 64 20 62 79 20 inode opened by
ce0d0 65 61 63 68 20 74 68 72 65 61 64 2e 0a 2a 2a 0a each thread..**.
ce0e0 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 6f 64 ** A single inod
ce0f0 65 20 63 61 6e 20 68 61 76 65 20 6d 75 6c 74 69 e can have multi
ce100 70 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 ple file descrip
ce110 74 6f 72 73 2c 20 73 6f 20 65 61 63 68 20 75 6e tors, so each un
ce120 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 ixFile.** struct
ce130 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 ure contains a p
ce140 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 ointer to an ins
ce150 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 tance of this ob
ce160 6a 65 63 74 20 61 6e 64 20 74 68 69 73 0a 2a 2a ject and this.**
ce170 20 6f 62 6a 65 63 74 20 6b 65 65 70 73 20 61 20 object keeps a
ce180 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d count of the num
ce190 62 65 72 20 6f 66 20 75 6e 69 78 46 69 6c 65 20 ber of unixFile
ce1a0 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 74 2e 0a pointing to it..
ce1b0 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 4c 6f */.struct unixLo
ce1c0 63 6b 49 6e 66 6f 20 7b 0a 20 20 73 74 72 75 63 ckInfo {. struc
ce1d0 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c 6f t unixLockKey lo
ce1e0 63 6b 4b 65 79 3b 20 20 20 20 20 2f 2a 20 54 68 ckKey; /* Th
ce1f0 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a e lookup key */.
ce200 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 int cnt;
ce210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ce220 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 53 /* Number of S
ce230 48 41 52 45 44 20 6c 6f 63 6b 73 20 68 65 6c 64 HARED locks held
ce240 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f 63 6b 74 79 */. int lockty
ce250 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 pe;
ce260 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 /* One of
ce270 53 48 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 53 SHARED_LOCK, RES
ce280 45 52 56 45 44 5f 4c 4f 43 4b 20 65 74 63 2e 20 ERVED_LOCK etc.
ce290 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 */. int nRef;
ce2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ce2b0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
ce2c0 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 f pointers to th
ce2d0 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a is structure */.
ce2e0 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 struct unixLoc
ce2f0 6b 49 6e 66 6f 20 2a 70 4e 65 78 74 3b 20 20 20 kInfo *pNext;
ce300 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c /* List of all
ce310 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 6f 62 unixLockInfo ob
ce320 6a 65 63 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 jects */. struc
ce330 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a t unixLockInfo *
ce340 70 50 72 65 76 3b 20 20 20 20 20 2f 2a 20 20 20 pPrev; /*
ce350 20 2e 2e 2e 2e 20 64 6f 75 62 6c 79 20 6c 69 6e .... doubly lin
ce360 6b 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ked */.};../*.**
ce370 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
ce380 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
ce390 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 ructure is alloc
ce3a0 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 6f 70 ated for each op
ce3b0 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 54 68 en.** inode. Th
ce3c0 69 73 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 is structure kee
ce3d0 70 73 20 74 72 61 63 6b 20 6f 66 20 74 68 65 20 ps track of the
ce3e0 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 number of locks
ce3f0 6f 6e 20 74 68 61 74 0a 2a 2a 20 69 6e 6f 64 65 on that.** inode
ce400 2e 20 20 49 66 20 61 20 63 6c 6f 73 65 20 69 73 . If a close is
ce410 20 61 74 74 65 6d 70 74 65 64 20 61 67 61 69 6e attempted again
ce420 73 74 20 61 6e 20 69 6e 6f 64 65 20 74 68 61 74 st an inode that
ce430 20 69 73 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6c is holding.** l
ce440 6f 63 6b 73 2c 20 74 68 65 20 63 6c 6f 73 65 20 ocks, the close
ce450 69 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 is deferred unti
ce460 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20 63 6c 65 61 l all locks clea
ce470 72 20 62 79 20 61 64 64 69 6e 67 20 74 68 65 0a r by adding the.
ce480 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ** file descript
ce490 6f 72 20 74 6f 20 62 65 20 63 6c 6f 73 65 64 20 or to be closed
ce4a0 74 6f 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c to the pending l
ce4b0 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a ist..**.** TODO:
ce4c0 20 20 43 6f 6e 73 69 64 65 72 20 63 68 61 6e 67 Consider chang
ce4d0 69 6e 67 20 74 68 69 73 20 73 6f 20 74 68 61 74 ing this so that
ce4e0 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 there is only a
ce4f0 20 73 69 6e 67 6c 65 20 66 69 6c 65 0a 2a 2a 20 single file.**
ce500 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 65 descriptor for e
ce510 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 2c 20 65 ach open file, e
ce520 76 65 6e 20 77 68 65 6e 20 69 74 20 69 73 20 6f ven when it is o
ce530 70 65 6e 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 pened multiple t
ce540 69 6d 65 73 2e 0a 2a 2a 20 54 68 65 20 63 6c 6f imes..** The clo
ce550 73 65 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c se() system call
ce560 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 6f 63 63 75 would only occu
ce570 72 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 r when the last
ce580 64 61 74 61 62 61 73 65 0a 2a 2a 20 75 73 69 6e database.** usin
ce590 67 20 74 68 65 20 66 69 6c 65 20 63 6c 6f 73 65 g the file close
ce5a0 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 s..*/.struct uni
ce5b0 78 4f 70 65 6e 43 6e 74 20 7b 0a 20 20 73 74 72 xOpenCnt {. str
ce5c0 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66 uct unixFileId f
ce5d0 69 6c 65 49 64 3b 20 20 20 2f 2a 20 54 68 65 20 ileId; /* The
ce5e0 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a 20 20 lookup key */.
ce5f0 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 int nRef;
ce600 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
ce610 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 umber of pointer
ce620 73 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 s to this struct
ce630 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f ure */. int nLo
ce640 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ck;
ce650 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
ce660 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f f outstanding lo
ce670 63 6b 73 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 cks */. UnixUnu
ce680 73 65 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20 sedFd *pUnused;
ce690 20 20 20 20 20 2f 2a 20 55 6e 75 73 65 64 20 66 /* Unused f
ce6a0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 ile descriptors
ce6b0 74 6f 20 63 6c 6f 73 65 20 2a 2f 0a 23 69 66 20 to close */.#if
ce6c0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 65 6d OS_VXWORKS. sem
ce6d0 5f 74 20 2a 70 53 65 6d 3b 20 20 20 20 20 20 20 _t *pSem;
ce6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
ce6f0 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 73 65 6d Named POSIX sem
ce700 61 70 68 6f 72 65 20 2a 2f 0a 20 20 63 68 61 72 aphore */. char
ce710 20 61 53 65 6d 4e 61 6d 65 5b 4d 41 58 5f 50 41 aSemName[MAX_PA
ce720 54 48 4e 41 4d 45 2b 32 5d 3b 20 20 20 2f 2a 20 THNAME+2]; /*
ce730 4e 61 6d 65 20 6f 66 20 74 68 61 74 20 73 65 6d Name of that sem
ce740 61 70 68 6f 72 65 20 2a 2f 0a 23 65 6e 64 69 66 aphore */.#endif
ce750 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 . struct unixOp
ce760 65 6e 43 6e 74 20 2a 70 4e 65 78 74 2c 20 2a 70 enCnt *pNext, *p
ce770 50 72 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74 20 Prev; /* List
ce780 6f 66 20 61 6c 6c 20 75 6e 69 78 4f 70 65 6e 43 of all unixOpenC
ce790 6e 74 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 7d 3b nt objects */.};
ce7a0 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 74 73 20 6f 66 ../*.** Lists of
ce7b0 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 all unixLockInf
ce7c0 6f 20 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43 6e o and unixOpenCn
ce7d0 74 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 73 t objects. Thes
ce7e0 65 20 75 73 65 64 20 74 6f 20 62 65 20 68 61 73 e used to be has
ce7f0 68 0a 2a 2a 20 74 61 62 6c 65 73 2e 20 20 42 75 h.** tables. Bu
ce800 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 t the number of
ce810 6f 62 6a 65 63 74 73 20 69 73 20 72 61 72 65 6c objects is rarel
ce820 79 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 64 6f y more than a do
ce830 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e 65 76 65 72 zen and.** never
ce840 20 65 78 63 65 65 64 73 20 61 20 66 65 77 20 74 exceeds a few t
ce850 68 6f 75 73 61 6e 64 2e 20 20 41 6e 64 20 6c 6f housand. And lo
ce860 6f 6b 75 70 20 69 73 20 6e 6f 74 20 6f 6e 20 61 okup is not on a
ce870 20 63 72 69 74 69 63 61 6c 0a 2a 2a 20 70 61 74 critical.** pat
ce880 68 20 73 6f 20 61 20 73 69 6d 70 6c 65 20 6c 69 h so a simple li
ce890 6e 6b 65 64 20 6c 69 73 74 20 77 69 6c 6c 20 73 nked list will s
ce8a0 75 66 66 69 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 uffice..*/.stati
ce8b0 63 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 c struct unixLoc
ce8c0 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c 69 73 74 20 kInfo *lockList
ce8d0 3d 20 30 3b 0a 73 74 61 74 69 63 20 73 74 72 75 = 0;.static stru
ce8e0 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a ct unixOpenCnt *
ce8f0 6f 70 65 6e 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f openList = 0;../
ce900 2a 0a 2a 2a 20 54 68 69 73 20 76 61 72 69 61 62 *.** This variab
ce910 6c 65 20 72 65 6d 65 6d 62 65 72 73 20 77 68 65 le remembers whe
ce920 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 ther or not thre
ce930 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 ads can override
ce940 20 65 61 63 68 20 6f 74 68 65 72 73 0a 2a 2a 20 each others.**
ce950 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 locks..**.**
ce960 30 3a 20 20 4e 6f 2e 20 20 54 68 72 65 61 64 73 0: No. Threads
ce970 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65 cannot override
ce980 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 each others loc
ce990 6b 73 2e 20 20 28 4c 69 6e 75 78 54 68 72 65 61 ks. (LinuxThrea
ce9a0 64 73 29 0a 2a 2a 20 20 20 20 31 3a 20 20 59 65 ds).** 1: Ye
ce9b0 73 2e 20 20 54 68 72 65 61 64 73 20 63 61 6e 20 s. Threads can
ce9c0 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 override each ot
ce9d0 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 28 50 6f hers locks. (Po
ce9e0 73 69 78 20 26 20 4e 4c 50 54 29 0a 2a 2a 20 20 six & NLPT).**
ce9f0 20 2d 31 3a 20 20 57 65 20 64 6f 6e 27 74 20 6b -1: We don't k
cea00 6e 6f 77 20 79 65 74 2e 0a 2a 2a 0a 2a 2a 20 4f now yet..**.** O
cea10 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20 n some systems,
cea20 77 65 20 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 69 we know at compi
cea30 6c 65 2d 74 69 6d 65 20 69 66 20 74 68 72 65 61 le-time if threa
cea40 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 ds can override
cea50 65 61 63 68 0a 2a 2a 20 6f 74 68 65 72 73 20 6c each.** others l
cea60 6f 63 6b 73 2e 20 20 4f 6e 20 74 68 6f 73 65 20 ocks. On those
cea70 73 79 73 74 65 6d 73 2c 20 74 68 65 20 53 51 4c systems, the SQL
cea80 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 ITE_THREAD_OVERR
cea90 49 44 45 5f 4c 4f 43 4b 20 6d 61 63 72 6f 0a 2a IDE_LOCK macro.*
ceaa0 2a 20 77 69 6c 6c 20 62 65 20 73 65 74 20 61 70 * will be set ap
ceab0 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 20 4f 6e propriately. On
ceac0 20 6f 74 68 65 72 20 73 79 73 74 65 6d 73 2c 20 other systems,
cead0 77 65 20 68 61 76 65 20 74 6f 20 63 68 65 63 6b we have to check
ceae0 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2e 20 at.** runtime.
ceaf0 20 4f 6e 20 74 68 65 73 65 20 6c 61 74 74 65 72 On these latter
ceb00 20 73 79 73 74 65 6d 73 2c 20 53 51 4c 54 49 45 systems, SQLTIE
ceb10 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 _THREAD_OVERRIDE
ceb20 5f 4c 4f 43 4b 20 69 73 0a 2a 2a 20 75 6e 64 65 _LOCK is.** unde
ceb30 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 fined..**.** Thi
ceb40 73 20 76 61 72 69 61 62 6c 65 20 6e 6f 72 6d 61 s variable norma
ceb50 6c 6c 79 20 68 61 73 20 66 69 6c 65 20 73 63 6f lly has file sco
ceb60 70 65 20 6f 6e 6c 79 2e 20 20 42 75 74 20 64 75 pe only. But du
ceb70 72 69 6e 67 20 74 65 73 74 69 6e 67 2c 20 77 65 ring testing, we
ceb80 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 61 20 67 6c make.** it a gl
ceb90 6f 62 61 6c 20 73 6f 20 74 68 61 74 20 74 68 65 obal so that the
ceba0 20 74 65 73 74 20 63 6f 64 65 20 63 61 6e 20 63 test code can c
cebb0 68 61 6e 67 65 20 69 74 73 20 76 61 6c 75 65 20 hange its value
cebc0 69 6e 20 6f 72 64 65 72 20 74 6f 20 76 65 72 69 in order to veri
cebd0 66 79 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 72 fy.** that the r
cebe0 69 67 68 74 20 73 74 75 66 66 20 68 61 70 70 65 ight stuff happe
cebf0 6e 73 20 69 6e 20 65 69 74 68 65 72 20 63 61 73 ns in either cas
cec00 65 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 e..*/.#if SQLITE
cec10 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 _THREADSAFE && d
cec20 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f efined(__linux__
cec30 29 0a 23 20 20 69 66 6e 64 65 66 20 53 51 4c 49 ).# ifndef SQLI
cec40 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 TE_THREAD_OVERRI
cec50 44 45 5f 4c 4f 43 4b 0a 23 20 20 20 20 64 65 66 DE_LOCK.# def
cec60 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 ine SQLITE_THREA
cec70 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20 D_OVERRIDE_LOCK
cec80 2d 31 0a 23 20 20 65 6e 64 69 66 0a 23 20 20 69 -1.# endif.# i
cec90 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
ceca0 0a 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65 72 .int threadsOver
cecb0 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f rideEachOthersLo
cecc0 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 52 cks = SQLITE_THR
cecd0 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 EAD_OVERRIDE_LOC
cece0 4b 3b 0a 23 20 20 65 6c 73 65 0a 73 74 61 74 69 K;.# else.stati
cecf0 63 20 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65 c int threadsOve
ced00 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c rrideEachOthersL
ced10 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 ocks = SQLITE_TH
ced20 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f READ_OVERRIDE_LO
ced30 43 4b 3b 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e CK;.# endif.#en
ced40 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 dif../*.** This
ced50 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 structure holds
ced60 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 information pass
ced70 65 64 20 69 6e 74 6f 20 69 6e 64 69 76 69 64 75 ed into individu
ced80 61 6c 20 74 65 73 74 0a 2a 2a 20 74 68 72 65 61 al test.** threa
ced90 64 73 20 62 79 20 74 68 65 20 74 65 73 74 54 68 ds by the testTh
ceda0 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 readLockingBehav
cedb0 69 6f 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a ior() routine..*
cedc0 2f 0a 73 74 72 75 63 74 20 74 68 72 65 61 64 54 /.struct threadT
cedd0 65 73 74 44 61 74 61 20 7b 0a 20 20 69 6e 74 20 estData {. int
cede0 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 fd;
cedf0 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 62 65 /* File to be
cee00 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 73 74 72 locked */. str
cee10 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 20 uct flock lock;
cee20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 /* The locki
cee30 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a ng operation */.
cee40 20 20 69 6e 74 20 72 65 73 75 6c 74 3b 20 20 20 int result;
cee50 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 /* Resu
cee60 6c 74 20 6f 66 20 74 68 65 20 6c 6f 63 6b 69 6e lt of the lockin
cee70 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 7d g operation */.}
cee80 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 ;..#if SQLITE_TH
cee90 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 READSAFE && defi
ceea0 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 2f ned(__linux__)./
ceeb0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
ceec0 6f 6e 20 69 73 20 75 73 65 64 20 61 73 20 74 68 on is used as th
ceed0 65 20 6d 61 69 6e 20 72 6f 75 74 69 6e 65 20 66 e main routine f
ceee0 6f 72 20 61 20 74 68 72 65 61 64 20 6c 61 75 6e or a thread laun
ceef0 63 68 65 64 20 62 79 0a 2a 2a 20 74 65 73 74 54 ched by.** testT
cef00 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 hreadLockingBeha
cef10 76 69 6f 72 28 29 2e 20 49 74 20 74 65 73 74 73 vior(). It tests
cef20 20 77 68 65 74 68 65 72 20 74 68 65 20 73 68 61 whether the sha
cef30 72 65 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 red-lock obtaine
cef40 64 0a 2a 2a 20 62 79 20 74 68 65 20 6d 61 69 6e d.** by the main
cef50 20 74 68 72 65 61 64 20 69 6e 20 74 65 73 74 54 thread in testT
cef60 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 hreadLockingBeha
cef70 76 69 6f 72 28 29 20 63 6f 6e 66 6c 69 63 74 73 vior() conflicts
cef80 20 77 69 74 68 20 61 0a 2a 2a 20 68 79 70 6f 74 with a.** hypot
cef90 68 65 74 69 63 61 6c 20 77 72 69 74 65 2d 6c 6f hetical write-lo
cefa0 63 6b 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74 ck obtained by t
cefb0 68 69 73 20 74 68 72 65 61 64 20 6f 6e 20 74 68 his thread on th
cefc0 65 20 73 61 6d 65 20 66 69 6c 65 2e 0a 2a 2a 0a e same file..**.
cefd0 2a 2a 20 54 68 65 20 77 72 69 74 65 2d 6c 6f 63 ** The write-loc
cefe0 6b 20 69 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c k is not actuall
ceff0 79 20 61 63 71 75 69 72 65 64 2c 20 61 73 20 74 y acquired, as t
cf000 68 69 73 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 his is not possi
cf010 62 6c 65 20 69 66 20 0a 2a 2a 20 74 68 65 20 66 ble if .** the f
cf020 69 6c 65 20 69 73 20 6f 70 65 6e 20 69 6e 20 72 ile is open in r
cf030 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 28 73 ead-only mode (s
cf040 65 65 20 74 69 63 6b 65 74 20 23 33 34 37 32 29 ee ticket #3472)
cf050 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 ..*/ .static voi
cf060 64 20 2a 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67 d *threadLocking
cf070 54 65 73 74 28 76 6f 69 64 20 2a 70 41 72 67 29 Test(void *pArg)
cf080 7b 0a 20 20 73 74 72 75 63 74 20 74 68 72 65 61 {. struct threa
cf090 64 54 65 73 74 44 61 74 61 20 2a 70 44 61 74 61 dTestData *pData
cf0a0 20 3d 20 28 73 74 72 75 63 74 20 74 68 72 65 61 = (struct threa
cf0b0 64 54 65 73 74 44 61 74 61 2a 29 70 41 72 67 3b dTestData*)pArg;
cf0c0 0a 20 20 70 44 61 74 61 2d 3e 72 65 73 75 6c 74 . pData->result
cf0d0 20 3d 20 66 63 6e 74 6c 28 70 44 61 74 61 2d 3e = fcntl(pData->
cf0e0 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 70 44 fd, F_GETLK, &pD
cf0f0 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 20 72 65 ata->lock);. re
cf100 74 75 72 6e 20 70 41 72 67 3b 0a 7d 0a 23 65 6e turn pArg;.}.#en
cf110 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 48 dif /* SQLITE_TH
cf120 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 READSAFE && defi
cf130 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 20 2a ned(__linux__) *
cf140 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 /...#if SQLITE_T
cf150 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 HREADSAFE && def
cf160 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a ined(__linux__).
cf170 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 65 /*.** This proce
cf180 64 75 72 65 20 61 74 74 65 6d 70 74 73 20 74 6f dure attempts to
cf190 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 determine wheth
cf1a0 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 er or not thread
cf1b0 73 0a 2a 2a 20 63 61 6e 20 6f 76 65 72 72 69 64 s.** can overrid
cf1c0 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f e each others lo
cf1d0 63 6b 73 20 74 68 65 6e 20 73 65 74 73 20 74 68 cks then sets th
cf1e0 65 20 0a 2a 2a 20 74 68 72 65 61 64 73 4f 76 65 e .** threadsOve
cf1f0 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c rrideEachOthersL
cf200 6f 63 6b 73 20 76 61 72 69 61 62 6c 65 20 61 70 ocks variable ap
cf210 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a propriately..*/.
cf220 73 74 61 74 69 63 20 76 6f 69 64 20 74 65 73 74 static void test
cf230 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 ThreadLockingBeh
cf240 61 76 69 6f 72 28 69 6e 74 20 66 64 5f 6f 72 69 avior(int fd_ori
cf250 67 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 g){. int fd;.
cf260 69 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74 int rc;. struct
cf270 20 74 68 72 65 61 64 54 65 73 74 44 61 74 61 20 threadTestData
cf280 64 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 d;. struct floc
cf290 6b 20 6c 3b 0a 20 20 70 74 68 72 65 61 64 5f 74 k l;. pthread_t
cf2a0 20 74 3b 0a 0a 20 20 66 64 20 3d 20 64 75 70 28 t;.. fd = dup(
cf2b0 66 64 5f 6f 72 69 67 29 3b 0a 20 20 69 66 28 20 fd_orig);. if(
cf2c0 66 64 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 fd<0 ) return;.
cf2d0 20 6d 65 6d 73 65 74 28 26 6c 2c 20 30 2c 20 73 memset(&l, 0, s
cf2e0 69 7a 65 6f 66 28 6c 29 29 3b 0a 20 20 6c 2e 6c izeof(l));. l.l
cf2f0 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b _type = F_RDLCK;
cf300 0a 20 20 6c 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a . l.l_len = 1;.
cf310 20 20 6c 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b l.l_start = 0;
cf320 0a 20 20 6c 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 . l.l_whence =
cf330 53 45 45 4b 5f 53 45 54 3b 0a 20 20 72 63 20 3d SEEK_SET;. rc =
cf340 20 66 63 6e 74 6c 28 66 64 5f 6f 72 69 67 2c 20 fcntl(fd_orig,
cf350 46 5f 53 45 54 4c 4b 2c 20 26 6c 29 3b 0a 20 20 F_SETLK, &l);.
cf360 69 66 28 20 72 63 21 3d 30 20 29 20 72 65 74 75 if( rc!=0 ) retu
cf370 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 2c rn;. memset(&d,
cf380 20 30 2c 20 73 69 7a 65 6f 66 28 64 29 29 3b 0a 0, sizeof(d));.
cf390 20 20 64 2e 66 64 20 3d 20 66 64 3b 0a 20 20 64 d.fd = fd;. d
cf3a0 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a 20 20 64 2e 6c .lock = l;. d.l
cf3b0 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 ock.l_type = F_W
cf3c0 52 4c 43 4b 3b 0a 20 20 69 66 28 20 70 74 68 72 RLCK;. if( pthr
cf3d0 65 61 64 5f 63 72 65 61 74 65 28 26 74 2c 20 30 ead_create(&t, 0
cf3e0 2c 20 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67 54 , threadLockingT
cf3f0 65 73 74 2c 20 26 64 29 3d 3d 30 20 29 7b 0a 20 est, &d)==0 ){.
cf400 20 20 20 70 74 68 72 65 61 64 5f 6a 6f 69 6e 28 pthread_join(
cf410 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 63 6c 6f t, 0);. }. clo
cf420 73 65 28 66 64 29 3b 0a 20 20 69 66 28 20 64 2e se(fd);. if( d.
cf430 72 65 73 75 6c 74 21 3d 30 20 29 20 72 65 74 75 result!=0 ) retu
cf440 72 6e 3b 0a 20 20 74 68 72 65 61 64 73 4f 76 65 rn;. threadsOve
cf450 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c rrideEachOthersL
cf460 6f 63 6b 73 20 3d 20 28 64 2e 6c 6f 63 6b 2e 6c ocks = (d.lock.l
cf470 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 29 3b _type==F_UNLCK);
cf480 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
cf490 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 ITE_THREADSAFE &
cf4a0 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 & defined(__linu
cf4b0 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 x__) */../*.** R
cf4c0 65 6c 65 61 73 65 20 61 20 75 6e 69 78 4c 6f 63 elease a unixLoc
cf4d0 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 kInfo structure
cf4e0 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 previously alloc
cf4f0 61 74 65 64 20 62 79 20 66 69 6e 64 4c 6f 63 6b ated by findLock
cf500 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 Info()..**.** Th
cf510 65 20 6d 75 74 65 78 20 65 6e 74 65 72 65 64 20 e mutex entered
cf520 75 73 69 6e 67 20 74 68 65 20 75 6e 69 78 45 6e using the unixEn
cf530 74 65 72 4d 75 74 65 78 28 29 20 66 75 6e 63 74 terMutex() funct
cf540 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 65 6c 64 ion must be held
cf550 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 66 75 .** when this fu
cf560 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
cf570 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
cf580 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f releaseLockInfo
cf590 28 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b (struct unixLock
cf5a0 49 6e 66 6f 20 2a 70 4c 6f 63 6b 29 7b 0a 20 20 Info *pLock){.
cf5b0 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65 assert( unixMute
cf5c0 78 48 65 6c 64 28 29 20 29 3b 0a 20 20 69 66 28 xHeld() );. if(
cf5d0 20 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c pLock ){. pL
cf5e0 6f 63 6b 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 ock->nRef--;.
cf5f0 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 if( pLock->nRef
cf600 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ==0 ){. if(
cf610 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 20 29 7b pLock->pPrev ){
cf620 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
cf630 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e 70 pLock->pPrev->p
cf640 4e 65 78 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 Next==pLock );.
cf650 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 50 pLock->pP
cf660 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4c 6f rev->pNext = pLo
cf670 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 ck->pNext;.
cf680 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
cf690 61 73 73 65 72 74 28 20 6c 6f 63 6b 4c 69 73 74 assert( lockList
cf6a0 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 ==pLock );.
cf6b0 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c lockList = pL
cf6c0 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 ock->pNext;.
cf6d0 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4c }. if( pL
cf6e0 6f 63 6b 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 ock->pNext ){.
cf6f0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c assert( pL
cf700 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 ock->pNext->pPre
cf710 76 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 v==pLock );.
cf720 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 pLock->pNext
cf730 2d 3e 70 50 72 65 76 20 3d 20 70 4c 6f 63 6b 2d ->pPrev = pLock-
cf740 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a >pPrev;. }.
cf750 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
cf760 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 7d ee(pLock);. }
cf770 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
cf780 6c 65 61 73 65 20 61 20 75 6e 69 78 4f 70 65 6e lease a unixOpen
cf790 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 70 72 Cnt structure pr
cf7a0 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 eviously allocat
cf7b0 65 64 20 62 79 20 66 69 6e 64 4c 6f 63 6b 49 6e ed by findLockIn
cf7c0 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 fo()..**.** The
cf7d0 6d 75 74 65 78 20 65 6e 74 65 72 65 64 20 75 73 mutex entered us
cf7e0 69 6e 67 20 74 68 65 20 75 6e 69 78 45 6e 74 65 ing the unixEnte
cf7f0 72 4d 75 74 65 78 28 29 20 66 75 6e 63 74 69 6f rMutex() functio
cf800 6e 20 6d 75 73 74 20 62 65 20 68 65 6c 64 0a 2a n must be held.*
cf810 2a 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 * when this func
cf820 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a tion is called..
cf830 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 */.static void r
cf840 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 73 74 eleaseOpenCnt(st
cf850 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 ruct unixOpenCnt
cf860 20 2a 70 4f 70 65 6e 29 7b 0a 20 20 61 73 73 65 *pOpen){. asse
cf870 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c rt( unixMutexHel
cf880 64 28 29 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 d() );. if( pOp
cf890 65 6e 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e 2d en ){. pOpen-
cf8a0 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 >nRef--;. if(
cf8b0 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 3d 3d 30 20 pOpen->nRef==0
cf8c0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 ){. if( pOp
cf8d0 65 6e 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 en->pPrev ){.
cf8e0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
cf8f0 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 en->pPrev->pNext
cf900 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 ==pOpen );.
cf910 20 20 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 2d pOpen->pPrev-
cf920 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65 6e 2d 3e >pNext = pOpen->
cf930 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 65 6c pNext;. }el
cf940 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 se{. asse
cf950 72 74 28 20 6f 70 65 6e 4c 69 73 74 3d 3d 70 4f rt( openList==pO
cf960 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 6f pen );. o
cf970 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e 2d penList = pOpen-
cf980 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a >pNext;. }.
cf990 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e 2d if( pOpen-
cf9a0 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 >pNext ){.
cf9b0 20 20 61 73 73 65 72 74 28 20 70 4f 70 65 6e 2d assert( pOpen-
cf9c0 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 >pNext->pPrev==p
cf9d0 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 Open );.
cf9e0 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50 pOpen->pNext->pP
cf9f0 72 65 76 20 3d 20 70 4f 70 65 6e 2d 3e 70 50 72 rev = pOpen->pPr
cfa00 65 76 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 20 ev;. }.#if
cfa10 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
cfa20 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c E && defined(__l
cfa30 69 6e 75 78 5f 5f 29 0a 20 20 20 20 20 20 61 73 inux__). as
cfa40 73 65 72 74 28 20 21 70 4f 70 65 6e 2d 3e 70 55 sert( !pOpen->pU
cfa50 6e 75 73 65 64 20 7c 7c 20 74 68 72 65 61 64 73 nused || threads
cfa60 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 OverrideEachOthe
cfa70 72 73 4c 6f 63 6b 73 3d 3d 30 20 29 3b 0a 23 65 rsLocks==0 );.#e
cfa80 6e 64 69 66 0a 0a 20 20 20 20 20 20 2f 2a 20 49 ndif.. /* I
cfa90 66 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 f pOpen->pUnused
cfaa0 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 2c 20 74 68 is not null, th
cfab0 65 6e 20 6d 65 6d 6f 72 79 20 61 6e 64 20 66 69 en memory and fi
cfac0 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 73 0a 20 le-descriptors.
cfad0 20 20 20 20 20 2a 2a 20 61 72 65 20 6c 65 61 6b ** are leak
cfae0 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 ed.. **.
cfaf0 20 20 20 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 ** This will
cfb00 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 2c 20 only happen if,
cfb10 75 6e 64 65 72 20 4c 69 6e 75 78 74 68 72 65 61 under Linuxthrea
cfb20 64 73 2c 20 74 68 65 20 75 73 65 72 20 68 61 73 ds, the user has
cfb30 20 6f 70 65 6e 65 64 0a 20 20 20 20 20 20 2a 2a opened. **
cfb40 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
cfb50 6e 20 6f 6e 65 20 74 68 72 65 61 64 2c 20 74 68 n one thread, th
cfb60 65 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 63 en attempts to c
cfb70 6c 6f 73 65 20 74 68 65 20 64 61 74 61 62 61 73 lose the databas
cfb80 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c e. ** handl
cfb90 65 20 66 72 6f 6d 20 61 6e 6f 74 68 65 72 20 74 e from another t
cfba0 68 72 65 61 64 20 28 77 69 74 68 6f 75 74 20 66 hread (without f
cfbb0 69 72 73 74 20 75 6e 6c 6f 63 6b 69 6e 67 20 74 irst unlocking t
cfbc0 68 65 20 64 62 20 66 69 6c 65 29 2e 0a 20 20 20 he db file)..
cfbd0 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61 20 ** This is a
cfbe0 6d 69 73 75 73 65 2e 20 20 2a 2f 0a 20 20 20 20 misuse. */.
cfbf0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
cfc00 4f 70 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d Open);. }. }
cfc10 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 .}../*.** Given
cfc20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f a file descripto
cfc30 72 2c 20 6c 6f 63 61 74 65 20 75 6e 69 78 4c 6f r, locate unixLo
cfc40 63 6b 49 6e 66 6f 20 61 6e 64 20 75 6e 69 78 4f ckInfo and unixO
cfc50 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 penCnt structure
cfc60 73 20 74 68 61 74 0a 2a 2a 20 64 65 73 63 72 69 s that.** descri
cfc70 62 65 73 20 74 68 61 74 20 66 69 6c 65 20 64 65 bes that file de
cfc80 73 63 72 69 70 74 6f 72 2e 20 20 43 72 65 61 74 scriptor. Creat
cfc90 65 20 6e 65 77 20 6f 6e 65 73 20 69 66 20 6e 65 e new ones if ne
cfca0 63 65 73 73 61 72 79 2e 20 20 54 68 65 0a 2a 2a cessary. The.**
cfcb0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 20 6d return values m
cfcc0 69 67 68 74 20 62 65 20 75 6e 69 6e 69 74 69 61 ight be uninitia
cfcd0 6c 69 7a 65 64 20 69 66 20 61 6e 20 65 72 72 6f lized if an erro
cfce0 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 r occurs..**.**
cfcf0 54 68 65 20 6d 75 74 65 78 20 65 6e 74 65 72 65 The mutex entere
cfd00 64 20 75 73 69 6e 67 20 74 68 65 20 75 6e 69 78 d using the unix
cfd10 45 6e 74 65 72 4d 75 74 65 78 28 29 20 66 75 6e EnterMutex() fun
cfd20 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 65 ction must be he
cfd30 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 ld.** when this
cfd40 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
cfd50 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e ed..**.** Return
cfd60 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 an appropriate
cfd70 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 error code..*/.s
cfd80 74 61 74 69 63 20 69 6e 74 20 66 69 6e 64 4c 6f tatic int findLo
cfd90 63 6b 49 6e 66 6f 28 0a 20 20 75 6e 69 78 46 69 ckInfo(. unixFi
cfda0 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 20 le *pFile,
cfdb0 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 78 /* Unix
cfdc0 20 66 69 6c 65 20 77 69 74 68 20 66 69 6c 65 20 file with file
cfdd0 64 65 73 63 20 75 73 65 64 20 69 6e 20 74 68 65 desc used in the
cfde0 20 6b 65 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 key */. struct
cfdf0 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 2a unixLockInfo **
cfe00 70 70 4c 6f 63 6b 2c 20 20 2f 2a 20 52 65 74 75 ppLock, /* Retu
cfe10 72 6e 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 rn the unixLockI
cfe20 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 68 65 nfo structure he
cfe30 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 re */. struct u
cfe40 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 2a 70 70 4f nixOpenCnt **ppO
cfe50 70 65 6e 20 20 20 20 2f 2a 20 52 65 74 75 72 6e pen /* Return
cfe60 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 the unixOpenCnt
cfe70 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65 20 structure here
cfe80 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 */.){. int rc;
cfe90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cfea0 20 20 20 20 20 20 20 2f 2a 20 53 79 73 74 65 6d /* System
cfeb0 20 63 61 6c 6c 20 72 65 74 75 72 6e 20 63 6f 64 call return cod
cfec0 65 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b 20 20 e */. int fd;
cfed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cfee0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c /* The fil
cfef0 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 e descriptor for
cff00 20 70 46 69 6c 65 20 2a 2f 0a 20 20 73 74 72 75 pFile */. stru
cff10 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c ct unixLockKey l
cff20 6f 63 6b 4b 65 79 3b 20 20 20 20 2f 2a 20 4c 6f ockKey; /* Lo
cff30 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20 74 68 65 okup key for the
cff40 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 unixLockInfo st
cff50 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 73 74 72 ructure */. str
cff60 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66 uct unixFileId f
cff70 69 6c 65 49 64 3b 20 20 20 20 20 20 2f 2a 20 4c ileId; /* L
cff80 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20 74 68 ookup key for th
cff90 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 e unixOpenCnt st
cffa0 72 75 63 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 ruct */. struct
cffb0 20 73 74 61 74 20 73 74 61 74 62 75 66 3b 20 20 stat statbuf;
cffc0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 2d /* Low-
cffd0 6c 65 76 65 6c 20 66 69 6c 65 20 69 6e 66 6f 72 level file infor
cffe0 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 mation */. stru
cfff0 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 ct unixLockInfo
d0000 2a 70 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 43 61 *pLock = 0;/* Ca
d0010 6e 64 69 64 61 74 65 20 75 6e 69 78 4c 6f 63 6b ndidate unixLock
d0020 49 6e 66 6f 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 Info object */.
d0030 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e struct unixOpen
d0040 43 6e 74 20 2a 70 4f 70 65 6e 3b 20 20 20 20 20 Cnt *pOpen;
d0050 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 75 6e 69 /* Candidate uni
d0060 78 4f 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 20 xOpenCnt object
d0070 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 75 6e */.. assert( un
d0080 69 78 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b ixMutexHeld() );
d0090 0a 0a 20 20 2f 2a 20 47 65 74 20 6c 6f 77 2d 6c .. /* Get low-l
d00a0 65 76 65 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e evel information
d00b0 20 61 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20 about the file
d00c0 74 68 61 74 20 77 65 20 63 61 6e 20 75 73 65 64 that we can used
d00d0 20 74 6f 0a 20 20 2a 2a 20 63 72 65 61 74 65 20 to. ** create
d00e0 61 20 75 6e 69 71 75 65 20 6e 61 6d 65 20 66 6f a unique name fo
d00f0 72 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2f r the file.. */
d0100 0a 20 20 66 64 20 3d 20 70 46 69 6c 65 2d 3e 68 . fd = pFile->h
d0110 3b 0a 20 20 72 63 20 3d 20 66 73 74 61 74 28 66 ;. rc = fstat(f
d0120 64 2c 20 26 73 74 61 74 62 75 66 29 3b 0a 20 20 d, &statbuf);.
d0130 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 if( rc!=0 ){.
d0140 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
d0150 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23 69 66 64 65 o = errno;.#ifde
d0160 66 20 45 4f 56 45 52 46 4c 4f 57 0a 20 20 20 20 f EOVERFLOW.
d0170 69 66 28 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 if( pFile->lastE
d0180 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f 57 20 rrno==EOVERFLOW
d0190 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
d01a0 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 66 0a 20 20 NOLFS;.#endif.
d01b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d01c0 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a 23 69 66 64 IOERR;. }..#ifd
d01d0 65 66 20 5f 5f 41 50 50 4c 45 5f 5f 0a 20 20 2f ef __APPLE__. /
d01e0 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e 20 61 6e 20 * On OS X on an
d01f0 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 74 65 6d msdos filesystem
d0200 2c 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 , the inode numb
d0210 65 72 20 69 73 20 72 65 70 6f 72 74 65 64 0a 20 er is reported.
d0220 20 2a 2a 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 ** incorrectly
d0230 66 6f 72 20 7a 65 72 6f 2d 73 69 7a 65 20 66 69 for zero-size fi
d0240 6c 65 73 2e 20 20 53 65 65 20 74 69 63 6b 65 74 les. See ticket
d0250 20 23 33 32 36 30 2e 20 20 54 6f 20 77 6f 72 6b #3260. To work
d0260 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20 74 68 69 . ** around thi
d0270 73 20 70 72 6f 62 6c 65 6d 20 28 77 65 20 63 6f s problem (we co
d0280 6e 73 69 64 65 72 20 69 74 20 61 20 62 75 67 20 nsider it a bug
d0290 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 20 53 51 4c in OS X, not SQL
d02a0 69 74 65 29 0a 20 20 2a 2a 20 77 65 20 61 6c 77 ite). ** we alw
d02b0 61 79 73 20 69 6e 63 72 65 61 73 65 20 74 68 65 ays increase the
d02c0 20 66 69 6c 65 20 73 69 7a 65 20 74 6f 20 31 20 file size to 1
d02d0 62 79 20 77 72 69 74 69 6e 67 20 61 20 73 69 6e by writing a sin
d02e0 67 6c 65 20 62 79 74 65 0a 20 20 2a 2a 20 70 72 gle byte. ** pr
d02f0 69 6f 72 20 74 6f 20 61 63 63 65 73 73 69 6e 67 ior to accessing
d0300 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65 the inode numbe
d0310 72 2e 20 20 54 68 65 20 6f 6e 65 20 62 79 74 65 r. The one byte
d0320 20 77 72 69 74 74 65 6e 20 69 73 0a 20 20 2a 2a written is. **
d0330 20 61 6e 20 41 53 43 49 49 20 27 53 27 20 63 68 an ASCII 'S' ch
d0340 61 72 61 63 74 65 72 20 77 68 69 63 68 20 61 6c aracter which al
d0350 73 6f 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 so happens to be
d0360 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 0a the first byte.
d0370 20 20 2a 2a 20 69 6e 20 74 68 65 20 68 65 61 64 ** in the head
d0380 65 72 20 6f 66 20 65 76 65 72 79 20 53 51 4c 69 er of every SQLi
d0390 74 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 6e te database. In
d03a0 20 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 this way, if th
d03b0 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20 72 61 ere. ** is a ra
d03c0 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 73 75 63 ce condition suc
d03d0 68 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 h that another t
d03e0 68 72 65 61 64 20 68 61 73 20 61 6c 72 65 61 64 hread has alread
d03f0 79 20 70 6f 70 75 6c 61 74 65 64 0a 20 20 2a 2a y populated. **
d0400 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 the first page
d0410 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2c of the database,
d0420 20 6e 6f 20 64 61 6d 61 67 65 20 69 73 20 64 6f no damage is do
d0430 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 ne.. */. if( s
d0440 74 61 74 62 75 66 2e 73 74 5f 73 69 7a 65 3d 3d tatbuf.st_size==
d0450 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 77 72 0 ){. rc = wr
d0460 69 74 65 28 66 64 2c 20 22 53 22 2c 20 31 29 3b ite(fd, "S", 1);
d0470 0a 20 20 20 20 69 66 28 20 72 63 21 3d 31 20 29 . if( rc!=1 )
d0480 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
d0490 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 20 QLITE_IOERR;.
d04a0 20 7d 0a 20 20 20 20 72 63 20 3d 20 66 73 74 61 }. rc = fsta
d04b0 74 28 66 64 2c 20 26 73 74 61 74 62 75 66 29 3b t(fd, &statbuf);
d04c0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 30 20 29 . if( rc!=0 )
d04d0 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c {. pFile->l
d04e0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f astErrno = errno
d04f0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
d0500 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 20 QLITE_IOERR;.
d0510 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 }. }.#endif..
d0520 20 6d 65 6d 73 65 74 28 26 6c 6f 63 6b 4b 65 79 memset(&lockKey
d0530 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6c 6f 63 6b , 0, sizeof(lock
d0540 4b 65 79 29 29 3b 0a 20 20 6c 6f 63 6b 4b 65 79 Key));. lockKey
d0550 2e 66 69 64 2e 64 65 76 20 3d 20 73 74 61 74 62 .fid.dev = statb
d0560 75 66 2e 73 74 5f 64 65 76 3b 0a 23 69 66 20 4f uf.st_dev;.#if O
d0570 53 5f 56 58 57 4f 52 4b 53 0a 20 20 6c 6f 63 6b S_VXWORKS. lock
d0580 4b 65 79 2e 66 69 64 2e 70 49 64 20 3d 20 70 46 Key.fid.pId = pF
d0590 69 6c 65 2d 3e 70 49 64 3b 0a 23 65 6c 73 65 0a ile->pId;.#else.
d05a0 20 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 69 6e lockKey.fid.in
d05b0 6f 20 3d 20 73 74 61 74 62 75 66 2e 73 74 5f 69 o = statbuf.st_i
d05c0 6e 6f 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 no;.#endif.#if S
d05d0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
d05e0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 && defined(__li
d05f0 6e 75 78 5f 5f 29 0a 20 20 69 66 28 20 74 68 72 nux__). if( thr
d0600 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 eadsOverrideEach
d0610 4f 74 68 65 72 73 4c 6f 63 6b 73 3c 30 20 29 7b OthersLocks<0 ){
d0620 0a 20 20 20 20 74 65 73 74 54 68 72 65 61 64 4c . testThreadL
d0630 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 66 ockingBehavior(f
d0640 64 29 3b 0a 20 20 7d 0a 20 20 6c 6f 63 6b 4b 65 d);. }. lockKe
d0650 79 2e 74 69 64 20 3d 20 74 68 72 65 61 64 73 4f y.tid = threadsO
d0660 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 verrideEachOther
d0670 73 4c 6f 63 6b 73 20 3f 20 30 20 3a 20 70 74 68 sLocks ? 0 : pth
d0680 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 23 65 6e read_self();.#en
d0690 64 69 66 0a 20 20 66 69 6c 65 49 64 20 3d 20 6c dif. fileId = l
d06a0 6f 63 6b 4b 65 79 2e 66 69 64 3b 0a 20 20 69 66 ockKey.fid;. if
d06b0 28 20 70 70 4c 6f 63 6b 21 3d 30 20 29 7b 0a 20 ( ppLock!=0 ){.
d06c0 20 20 20 70 4c 6f 63 6b 20 3d 20 6c 6f 63 6b 4c pLock = lockL
d06d0 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 ist;. while(
d06e0 70 4c 6f 63 6b 20 26 26 20 6d 65 6d 63 6d 70 28 pLock && memcmp(
d06f0 26 6c 6f 63 6b 4b 65 79 2c 20 26 70 4c 6f 63 6b &lockKey, &pLock
d0700 2d 3e 6c 6f 63 6b 4b 65 79 2c 20 73 69 7a 65 6f ->lockKey, sizeo
d0710 66 28 6c 6f 63 6b 4b 65 79 29 29 20 29 7b 0a 20 f(lockKey)) ){.
d0720 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70 4c 6f pLock = pLo
d0730 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d ck->pNext;. }
d0740 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 3d 3d . if( pLock==
d0750 30 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 0 ){. pLock
d0760 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f = sqlite3_mallo
d0770 63 28 20 73 69 7a 65 6f 66 28 2a 70 4c 6f 63 6b c( sizeof(*pLock
d0780 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 ) );. if( p
d0790 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Lock==0 ){.
d07a0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
d07b0 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f OMEM;. go
d07c0 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b to exit_findlock
d07d0 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 info;. }.
d07e0 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4c 6f 63 memcpy(&pLoc
d07f0 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 26 6c 6f 63 6b k->lockKey,&lock
d0800 4b 65 79 2c 73 69 7a 65 6f 66 28 6c 6f 63 6b 4b Key,sizeof(lockK
d0810 65 79 29 29 3b 0a 20 20 20 20 20 20 70 4c 6f 63 ey));. pLoc
d0820 6b 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 k->nRef = 1;.
d0830 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20 pLock->cnt =
d0840 30 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 0;. pLock->
d0850 6c 6f 63 6b 74 79 70 65 20 3d 20 30 3b 0a 20 20 locktype = 0;.
d0860 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 pLock->pNext
d0870 20 3d 20 6c 6f 63 6b 4c 69 73 74 3b 0a 20 20 20 = lockList;.
d0880 20 20 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 20 pLock->pPrev
d0890 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 6c = 0;. if( l
d08a0 6f 63 6b 4c 69 73 74 20 29 20 6c 6f 63 6b 4c 69 ockList ) lockLi
d08b0 73 74 2d 3e 70 50 72 65 76 20 3d 20 70 4c 6f 63 st->pPrev = pLoc
d08c0 6b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 4c 69 73 k;. lockLis
d08d0 74 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 20 20 7d t = pLock;. }
d08e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4c 6f 63 else{. pLoc
d08f0 6b 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d k->nRef++;. }
d0900 0a 20 20 20 20 2a 70 70 4c 6f 63 6b 20 3d 20 70 . *ppLock = p
d0910 4c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 Lock;. }. if(
d0920 70 70 4f 70 65 6e 21 3d 30 20 29 7b 0a 20 20 20 ppOpen!=0 ){.
d0930 20 70 4f 70 65 6e 20 3d 20 6f 70 65 6e 4c 69 73 pOpen = openLis
d0940 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 4f t;. while( pO
d0950 70 65 6e 20 26 26 20 6d 65 6d 63 6d 70 28 26 66 pen && memcmp(&f
d0960 69 6c 65 49 64 2c 20 26 70 4f 70 65 6e 2d 3e 66 ileId, &pOpen->f
d0970 69 6c 65 49 64 2c 20 73 69 7a 65 6f 66 28 66 69 ileId, sizeof(fi
d0980 6c 65 49 64 29 29 20 29 7b 0a 20 20 20 20 20 20 leId)) ){.
d0990 70 4f 70 65 6e 20 3d 20 70 4f 70 65 6e 2d 3e 70 pOpen = pOpen->p
d09a0 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Next;. }.
d09b0 69 66 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a if( pOpen==0 ){.
d09c0 20 20 20 20 20 20 70 4f 70 65 6e 20 3d 20 73 71 pOpen = sq
d09d0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 lite3_malloc( si
d09e0 7a 65 6f 66 28 2a 70 4f 70 65 6e 29 20 29 3b 0a zeof(*pOpen) );.
d09f0 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e 3d if( pOpen=
d0a00 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 =0 ){. re
d0a10 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 4c leaseLockInfo(pL
d0a20 6f 63 6b 29 3b 0a 20 20 20 20 20 20 20 20 72 63 ock);. rc
d0a30 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
d0a40 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 . goto ex
d0a50 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b it_findlockinfo;
d0a60 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d . }. m
d0a70 65 6d 73 65 74 28 70 4f 70 65 6e 2c 20 30 2c 20 emset(pOpen, 0,
d0a80 73 69 7a 65 6f 66 28 2a 70 4f 70 65 6e 29 29 3b sizeof(*pOpen));
d0a90 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 66 69 . pOpen->fi
d0aa0 6c 65 49 64 20 3d 20 66 69 6c 65 49 64 3b 0a 20 leId = fileId;.
d0ab0 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 pOpen->nRef
d0ac0 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 65 = 1;. pOpe
d0ad0 6e 2d 3e 70 4e 65 78 74 20 3d 20 6f 70 65 6e 4c n->pNext = openL
d0ae0 69 73 74 3b 0a 20 20 20 20 20 20 69 66 28 20 6f ist;. if( o
d0af0 70 65 6e 4c 69 73 74 20 29 20 6f 70 65 6e 4c 69 penList ) openLi
d0b00 73 74 2d 3e 70 50 72 65 76 20 3d 20 70 4f 70 65 st->pPrev = pOpe
d0b10 6e 3b 0a 20 20 20 20 20 20 6f 70 65 6e 4c 69 73 n;. openLis
d0b20 74 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 20 20 7d t = pOpen;. }
d0b30 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4f 70 65 else{. pOpe
d0b40 6e 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d n->nRef++;. }
d0b50 0a 20 20 20 20 2a 70 70 4f 70 65 6e 20 3d 20 70 . *ppOpen = p
d0b60 4f 70 65 6e 3b 0a 20 20 7d 0a 0a 65 78 69 74 5f Open;. }..exit_
d0b70 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3a 0a 20 20 findlockinfo:.
d0b80 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
d0b90 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20 63 75 .** If we are cu
d0ba0 72 72 65 6e 74 6c 79 20 69 6e 20 61 20 64 69 66 rrently in a dif
d0bb0 66 65 72 65 6e 74 20 74 68 72 65 61 64 20 74 68 ferent thread th
d0bc0 61 6e 20 74 68 65 20 74 68 72 65 61 64 20 74 68 an the thread th
d0bd0 61 74 20 74 68 65 0a 2a 2a 20 75 6e 69 78 46 69 at the.** unixFi
d0be0 6c 65 20 61 72 67 75 6d 65 6e 74 20 62 65 6c 6f le argument belo
d0bf0 6e 67 73 20 74 6f 2c 20 74 68 65 6e 20 74 72 61 ngs to, then tra
d0c00 6e 73 66 65 72 20 6f 77 6e 65 72 73 68 69 70 20 nsfer ownership
d0c10 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 0a of the unixFile.
d0c20 2a 2a 20 6f 76 65 72 20 74 6f 20 74 68 65 20 63 ** over to the c
d0c30 75 72 72 65 6e 74 20 74 68 72 65 61 64 2e 0a 2a urrent thread..*
d0c40 2a 0a 2a 2a 20 41 20 75 6e 69 78 46 69 6c 65 20 *.** A unixFile
d0c50 69 73 20 6f 6e 6c 79 20 6f 77 6e 65 64 20 62 79 is only owned by
d0c60 20 61 20 74 68 72 65 61 64 20 6f 6e 20 73 79 73 a thread on sys
d0c70 74 65 6d 73 20 74 68 61 74 20 75 73 65 20 4c 69 tems that use Li
d0c80 6e 75 78 54 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a nuxThreads..**.*
d0c90 2a 20 4f 77 6e 65 72 73 68 69 70 20 74 72 61 6e * Ownership tran
d0ca0 73 66 65 72 20 69 73 20 6f 6e 6c 79 20 61 6c 6c sfer is only all
d0cb0 6f 77 65 64 20 69 66 20 74 68 65 20 75 6e 69 78 owed if the unix
d0cc0 46 69 6c 65 20 69 73 20 63 75 72 72 65 6e 74 6c File is currentl
d0cd0 79 20 75 6e 6c 6f 63 6b 65 64 2e 0a 2a 2a 20 49 y unlocked..** I
d0ce0 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 69 f the unixFile i
d0cf0 73 20 6c 6f 63 6b 65 64 20 61 6e 64 20 61 6e 20 s locked and an
d0d00 6f 77 6e 65 72 73 68 69 70 20 69 73 20 77 72 6f ownership is wro
d0d10 6e 67 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 0a ng, then return.
d0d20 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 ** SQLITE_MISUSE
d0d30 2e 20 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 . SQLITE_OK is
d0d40 72 65 74 75 72 6e 65 64 20 69 66 20 65 76 65 72 returned if ever
d0d50 79 74 68 69 6e 67 20 77 6f 72 6b 73 2e 0a 2a 2f ything works..*/
d0d60 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
d0d70 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 ADSAFE && define
d0d80 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 73 74 61 d(__linux__).sta
d0d90 74 69 63 20 69 6e 74 20 74 72 61 6e 73 66 65 72 tic int transfer
d0da0 4f 77 6e 65 72 73 68 69 70 28 75 6e 69 78 46 69 Ownership(unixFi
d0db0 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e le *pFile){. in
d0dc0 74 20 72 63 3b 0a 20 20 70 74 68 72 65 61 64 5f t rc;. pthread_
d0dd0 74 20 68 53 65 6c 66 3b 0a 20 20 69 66 28 20 74 t hSelf;. if( t
d0de0 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 hreadsOverrideEa
d0df0 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 29 7b chOthersLocks ){
d0e00 0a 20 20 20 20 2f 2a 20 4f 77 6e 65 72 73 68 69 . /* Ownershi
d0e10 70 20 74 72 61 6e 73 66 65 72 73 20 6e 6f 74 20 p transfers not
d0e20 6e 65 65 64 65 64 20 6f 6e 20 74 68 69 73 20 73 needed on this s
d0e30 79 73 74 65 6d 20 2a 2f 0a 20 20 20 20 72 65 74 ystem */. ret
d0e40 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
d0e50 20 7d 0a 20 20 68 53 65 6c 66 20 3d 20 70 74 68 }. hSelf = pth
d0e60 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 69 read_self();. i
d0e70 66 28 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c f( pthread_equal
d0e80 28 70 46 69 6c 65 2d 3e 74 69 64 2c 20 68 53 65 (pFile->tid, hSe
d0e90 6c 66 29 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 lf) ){. /* We
d0ea0 20 61 72 65 20 73 74 69 6c 6c 20 69 6e 20 74 68 are still in th
d0eb0 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 2a 2f e same thread */
d0ec0 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 22 4e . OSTRACE1("N
d0ed0 6f 2d 74 72 61 6e 73 66 65 72 2c 20 73 61 6d 65 o-transfer, same
d0ee0 20 74 68 72 65 61 64 5c 6e 22 29 3b 0a 20 20 20 thread\n");.
d0ef0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
d0f00 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 69 K;. }. if( pFi
d0f10 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f le->locktype!=NO
d0f20 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 _LOCK ){. /*
d0f30 57 65 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 We cannot change
d0f40 20 6f 77 6e 65 72 73 68 69 70 20 77 68 69 6c 65 ownership while
d0f50 20 77 65 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 we are holding
d0f60 61 20 6c 6f 63 6b 21 20 2a 2f 0a 20 20 20 20 72 a lock! */. r
d0f70 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 eturn SQLITE_MIS
d0f80 55 53 45 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 USE;. }. OSTRA
d0f90 43 45 34 28 22 54 72 61 6e 73 66 65 72 20 6f 77 CE4("Transfer ow
d0fa0 6e 65 72 73 68 69 70 20 6f 66 20 25 64 20 66 72 nership of %d fr
d0fb0 6f 6d 20 25 64 20 74 6f 20 25 64 5c 6e 22 2c 0a om %d to %d\n",.
d0fc0 20 20 20 20 20 20 20 20 20 20 20 20 70 46 69 6c pFil
d0fd0 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 74 69 64 e->h, pFile->tid
d0fe0 2c 20 68 53 65 6c 66 29 3b 0a 20 20 70 46 69 6c , hSelf);. pFil
d0ff0 65 2d 3e 74 69 64 20 3d 20 68 53 65 6c 66 3b 0a e->tid = hSelf;.
d1000 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 70 4c 6f if (pFile->pLo
d1010 63 6b 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 20 20 ck != NULL) {.
d1020 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 releaseLockInf
d1030 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b o(pFile->pLock);
d1040 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f . rc = findLo
d1050 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2c 20 26 70 ckInfo(pFile, &p
d1060 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2c 20 30 29 3b File->pLock, 0);
d1070 0a 20 20 20 20 4f 53 54 52 41 43 45 35 28 22 4c . OSTRACE5("L
d1080 4f 43 4b 20 20 20 20 25 64 20 69 73 20 6e 6f 77 OCK %d is now
d1090 20 25 73 28 25 73 2c 25 64 29 5c 6e 22 2c 20 70 %s(%s,%d)\n", p
d10a0 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 File->h,.
d10b0 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 locktypeName
d10c0 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 (pFile->locktype
d10d0 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f ),. lo
d10e0 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 cktypeName(pFile
d10f0 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 ->pLock->locktyp
d1100 65 29 2c 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b e), pFile->pLock
d1110 2d 3e 63 6e 74 29 3b 0a 20 20 20 20 72 65 74 75 ->cnt);. retu
d1120 72 6e 20 72 63 3b 0a 20 20 7d 20 65 6c 73 65 20 rn rc;. } else
d1130 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
d1140 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 23 65 ITE_OK;. }.}.#e
d1150 6c 73 65 20 20 2f 2a 20 69 66 20 6e 6f 74 20 53 lse /* if not S
d1160 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
d1170 20 2a 2f 0a 20 20 2f 2a 20 4f 6e 20 73 69 6e 67 */. /* On sing
d1180 6c 65 2d 74 68 72 65 61 64 65 64 20 62 75 69 6c le-threaded buil
d1190 64 73 2c 20 6f 77 6e 65 72 73 68 69 70 20 74 72 ds, ownership tr
d11a0 61 6e 73 66 65 72 20 69 73 20 61 20 6e 6f 2d 6f ansfer is a no-o
d11b0 70 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 74 72 p */.# define tr
d11c0 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 ansferOwnership(
d11d0 58 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e X) SQLITE_OK.#en
d11e0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 48 dif /* SQLITE_TH
d11f0 52 45 41 44 53 41 46 45 20 2a 2f 0a 0a 0a 2f 2a READSAFE */.../*
d1200 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
d1210 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 checks if there
d1220 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c is a RESERVED l
d1230 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 ock held on the
d1240 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c specified.** fil
d1250 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 e by this or any
d1260 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 other process.
d1270 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 If such a lock i
d1280 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 s held, set *pRe
d1290 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e sOut.** to a non
d12a0 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 -zero value othe
d12b0 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 rwise *pResOut i
d12c0 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 s set to zero.
d12d0 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
d12e0 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 .** is set to SQ
d12f0 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 LITE_OK unless a
d1300 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 n I/O error occu
d1310 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 rs during lock c
d1320 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 hecking..*/.stat
d1330 69 63 20 69 6e 74 20 75 6e 69 78 43 68 65 63 6b ic int unixCheck
d1340 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c ReservedLock(sql
d1350 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
d1360 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 nt *pResOut){.
d1370 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
d1380 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 OK;. int reserv
d1390 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 ed = 0;. unixFi
d13a0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
d13b0 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 53 69 xFile*)id;.. Si
d13c0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 mulateIOError( r
d13d0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
d13e0 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 RR_CHECKRESERVED
d13f0 4c 4f 43 4b 3b 20 29 3b 0a 0a 20 20 61 73 73 65 LOCK; );.. asse
d1400 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 75 rt( pFile );. u
d1410 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b nixEnterMutex();
d1420 20 2f 2a 20 42 65 63 61 75 73 65 20 70 46 69 6c /* Because pFil
d1430 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 e->pLock is shar
d1440 65 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 ed across thread
d1450 73 20 2a 2f 0a 0a 20 20 2f 2a 20 43 68 65 63 6b s */.. /* Check
d1460 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 if a thread in
d1470 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c this process hol
d1480 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a ds such a lock *
d1490 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 /. if( pFile->p
d14a0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 Lock->locktype>S
d14b0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 HARED_LOCK ){.
d14c0 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a reserved = 1;.
d14d0 20 20 7d 0a 0a 20 20 2f 2a 20 4f 74 68 65 72 77 }.. /* Otherw
d14e0 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 ise see if some
d14f0 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f other process ho
d1500 6c 64 73 20 69 74 2e 0a 20 20 2a 2f 0a 23 69 66 lds it.. */.#if
d1510 6e 64 65 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 20 ndef __DJGPP__.
d1520 20 69 66 28 20 21 72 65 73 65 72 76 65 64 20 29 if( !reserved )
d1530 7b 0a 20 20 20 20 73 74 72 75 63 74 20 66 6c 6f {. struct flo
d1540 63 6b 20 6c 6f 63 6b 3b 0a 20 20 20 20 6c 6f 63 ck lock;. loc
d1550 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 k.l_whence = SEE
d1560 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e K_SET;. lock.
d1570 6c 5f 73 74 61 72 74 20 3d 20 52 45 53 45 52 56 l_start = RESERV
d1580 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 ED_BYTE;. loc
d1590 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 20 k.l_len = 1;.
d15a0 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 lock.l_type = F
d15b0 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 69 66 20 28 _WRLCK;. if (
d15c0 2d 31 20 3d 3d 20 66 63 6e 74 6c 28 70 46 69 6c -1 == fcntl(pFil
d15d0 65 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 e->h, F_GETLK, &
d15e0 6c 6f 63 6b 29 29 20 7b 0a 20 20 20 20 20 20 69 lock)) {. i
d15f0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e nt tErrno = errn
d1600 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 o;. rc = sq
d1610 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
d1620 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
d1630 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 SQLITE_IOERR_CHE
d1640 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 3b CKRESERVEDLOCK);
d1650 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 . pFile->la
d1660 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
d1670 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28 ;. } else if(
d1680 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 21 3d 46 5f lock.l_type!=F_
d1690 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 UNLCK ){. r
d16a0 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 eserved = 1;.
d16b0 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }. }.#endif.
d16c0 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 . unixLeaveMute
d16d0 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 x();. OSTRACE4(
d16e0 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 "TEST WR-LOCK %d
d16f0 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 %d %d\n", pFile
d1700 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 ->h, rc, reserve
d1710 64 29 3b 0a 0a 20 20 2a 70 52 65 73 4f 75 74 20 d);.. *pResOut
d1720 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 = reserved;. re
d1730 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
d1740 2a 20 50 65 72 66 6f 72 6d 20 61 20 66 69 6c 65 * Perform a file
d1750 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 locking operati
d1760 6f 6e 20 6f 6e 20 61 20 72 61 6e 67 65 20 6f 66 on on a range of
d1770 20 62 79 74 65 73 20 69 6e 20 61 20 66 69 6c 65 bytes in a file
d1780 2e 0a 2a 2a 20 54 68 65 20 22 6f 70 22 20 70 61 ..** The "op" pa
d1790 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 rameter should b
d17a0 65 20 6f 6e 65 20 6f 66 20 46 5f 52 44 4c 43 4b e one of F_RDLCK
d17b0 2c 20 46 5f 57 52 4c 43 4b 2c 20 6f 72 20 46 5f , F_WRLCK, or F_
d17c0 55 4e 4c 43 4b 2e 0a 2a 2a 20 52 65 74 75 72 6e UNLCK..** Return
d17d0 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 0 on success or
d17e0 20 2d 31 20 66 6f 72 20 66 61 69 6c 75 72 65 2e -1 for failure.
d17f0 20 20 4f 6e 20 66 61 69 6c 75 72 65 2c 20 77 72 On failure, wr
d1800 69 74 65 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a ite the error.**
d1810 20 63 6f 64 65 20 69 6e 74 6f 20 2a 70 45 72 72 code into *pErr
d1820 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 code..**.** If t
d1830 68 65 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f he SQLITE_WHOLE_
d1840 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 62 69 74 FILE_LOCKING bit
d1850 20 69 73 20 63 6c 65 61 72 2c 20 74 68 65 6e 20 is clear, then
d1860 6f 6e 6c 79 20 6c 6f 63 6b 0a 2a 2a 20 74 68 65 only lock.** the
d1870 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20 range of bytes
d1880 6f 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 on the locking p
d1890 61 67 65 20 62 65 74 77 65 65 6e 20 53 48 41 52 age between SHAR
d18a0 45 44 5f 46 49 52 53 54 20 61 6e 64 0a 2a 2a 20 ED_FIRST and.**
d18b0 53 48 41 52 45 44 5f 53 49 5a 45 2e 20 20 49 66 SHARED_SIZE. If
d18c0 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 SQLITE_WHOLE_FI
d18d0 4c 45 5f 4c 4f 43 4b 49 4e 47 20 69 73 20 73 65 LE_LOCKING is se
d18e0 74 2c 20 74 68 65 6e 20 6c 6f 63 6b 20 61 6c 6c t, then lock all
d18f0 0a 2a 2a 20 62 79 74 65 73 20 66 72 6f 6d 20 30 .** bytes from 0
d1900 20 75 70 20 74 6f 20 62 75 74 20 6e 6f 74 20 69 up to but not i
d1910 6e 63 6c 75 64 69 6e 67 20 50 45 4e 44 49 4e 47 ncluding PENDING
d1920 5f 42 59 54 45 2c 20 61 6e 64 20 61 6c 6c 20 62 _BYTE, and all b
d1930 79 74 65 73 0a 2a 2a 20 74 68 61 74 20 66 6f 6c ytes.** that fol
d1940 6c 6f 77 20 53 48 41 52 45 44 5f 46 49 52 53 54 low SHARED_FIRST
d1950 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 ..**.** In other
d1960 20 77 6f 72 64 73 2c 20 6f 66 20 53 51 4c 49 54 words, of SQLIT
d1970 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 E_WHOLE_FILE_LOC
d1980 4b 49 4e 47 20 69 66 20 66 61 6c 73 65 20 28 74 KING if false (t
d1990 68 65 20 68 69 73 74 6f 72 69 63 61 6c 0a 2a 2a he historical.**
d19a0 20 64 65 66 61 75 6c 74 20 63 61 73 65 29 20 74 default case) t
d19b0 68 65 6e 20 6f 6e 6c 79 20 6c 6f 63 6b 20 61 20 hen only lock a
d19c0 73 6d 61 6c 6c 20 72 61 6e 67 65 20 6f 66 20 62 small range of b
d19d0 79 74 65 73 20 66 72 6f 6d 20 53 48 41 52 45 44 ytes from SHARED
d19e0 5f 46 49 52 53 54 0a 2a 2a 20 74 68 72 6f 75 67 _FIRST.** throug
d19f0 68 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b 53 h SHARED_FIRST+S
d1a00 48 41 52 45 44 5f 53 49 5a 45 2d 31 2e 20 20 42 HARED_SIZE-1. B
d1a10 75 74 20 69 66 20 53 51 4c 49 54 45 5f 57 48 4f ut if SQLITE_WHO
d1a20 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 LE_FILE_LOCKING
d1a30 69 73 0a 2a 2a 20 74 72 75 65 20 74 68 65 6e 20 is.** true then
d1a40 6c 6f 63 6b 20 65 76 65 72 79 20 62 79 74 65 20 lock every byte
d1a50 69 6e 20 74 68 65 20 66 69 6c 65 20 65 78 63 65 in the file exce
d1a60 70 74 20 66 6f 72 20 50 45 4e 44 49 4e 47 5f 42 pt for PENDING_B
d1a70 59 54 45 20 61 6e 64 0a 2a 2a 20 52 45 53 45 52 YTE and.** RESER
d1a80 56 45 44 5f 42 59 54 45 2e 0a 2a 2a 0a 2a 2a 20 VED_BYTE..**.**
d1a90 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c SQLITE_WHOLE_FIL
d1aa0 45 5f 4c 4f 43 4b 49 4e 47 3d 74 72 75 65 20 6f E_LOCKING=true o
d1ab0 76 65 72 6c 61 70 73 20 53 51 4c 49 54 45 5f 57 verlaps SQLITE_W
d1ac0 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e HOLE_FILE_LOCKIN
d1ad0 47 3d 66 61 6c 73 65 0a 2a 2a 20 61 6e 64 20 73 G=false.** and s
d1ae0 6f 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 63 o the locking sc
d1af0 68 65 6d 65 73 20 61 72 65 20 63 6f 6d 70 61 74 hemes are compat
d1b00 69 62 6c 65 2e 20 20 4f 6e 65 20 74 79 70 65 20 ible. One type
d1b10 6f 66 20 6c 6f 63 6b 20 77 69 6c 6c 0a 2a 2a 20 of lock will.**
d1b20 65 66 66 65 63 74 69 76 65 6c 79 20 65 78 63 6c effectively excl
d1b30 75 64 65 20 74 68 65 20 6f 74 68 65 72 20 74 79 ude the other ty
d1b40 70 65 2e 20 20 54 68 65 20 72 65 61 73 6f 6e 20 pe. The reason
d1b50 66 6f 72 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a for using the.**
d1b60 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 SQLITE_WHOLE_FI
d1b70 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 74 72 75 65 20 LE_LOCKING=true
d1b80 69 73 20 74 68 61 74 20 62 79 20 69 6e 64 69 63 is that by indic
d1b90 61 74 69 6e 67 20 74 68 65 20 66 75 6c 6c 20 72 ating the full r
d1ba0 61 6e 67 65 0a 2a 2a 20 6f 66 20 62 79 74 65 73 ange.** of bytes
d1bb0 20 74 6f 20 62 65 20 72 65 61 64 20 6f 72 20 77 to be read or w
d1bc0 72 69 74 74 65 6e 2c 20 77 65 20 67 69 76 65 20 ritten, we give
d1bd0 68 69 6e 74 73 20 74 6f 20 4e 46 53 20 74 6f 20 hints to NFS to
d1be0 68 65 6c 70 20 69 74 0a 2a 2a 20 6d 61 69 6e 74 help it.** maint
d1bf0 61 69 6e 20 63 61 63 68 65 20 63 6f 68 65 72 65 ain cache cohere
d1c00 6e 63 79 2e 20 20 4f 6e 20 74 68 65 20 6f 74 68 ncy. On the oth
d1c10 65 72 20 68 61 6e 64 2c 20 77 68 6f 6c 65 20 66 er hand, whole f
d1c20 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 69 ile locking.** i
d1c30 73 20 73 6c 6f 77 65 72 2c 20 73 6f 20 77 65 20 s slower, so we
d1c40 64 6f 6e 27 74 20 77 61 6e 74 20 74 6f 20 75 73 don't want to us
d1c50 65 20 69 74 20 65 78 63 65 70 74 20 66 6f 72 20 e it except for
d1c60 4e 46 53 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 NFS..*/.static i
d1c70 6e 74 20 72 61 6e 67 65 4c 6f 63 6b 28 75 6e 69 nt rangeLock(uni
d1c80 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 69 6e xFile *pFile, in
d1c90 74 20 6f 70 2c 20 69 6e 74 20 2a 70 45 72 72 63 t op, int *pErrc
d1ca0 6f 64 65 29 7b 0a 20 20 73 74 72 75 63 74 20 66 ode){. struct f
d1cb0 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 lock lock;. int
d1cc0 20 72 63 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 74 79 rc;. lock.l_ty
d1cd0 70 65 20 3d 20 6f 70 3b 0a 20 20 6c 6f 63 6b 2e pe = op;. lock.
d1ce0 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 l_start = SHARED
d1cf0 5f 46 49 52 53 54 3b 0a 20 20 6c 6f 63 6b 2e 6c _FIRST;. lock.l
d1d00 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 _whence = SEEK_S
d1d10 45 54 3b 0a 20 20 69 66 28 20 28 70 46 69 6c 65 ET;. if( (pFile
d1d20 2d 3e 66 69 6c 65 46 6c 61 67 73 20 26 20 53 51 ->fileFlags & SQ
d1d30 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f LITE_WHOLE_FILE_
d1d40 4c 4f 43 4b 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 LOCKING)==0 ){.
d1d50 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 lock.l_len =
d1d60 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 SHARED_SIZE;.
d1d70 20 72 63 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c rc = fcntl(pFil
d1d80 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 e->h, F_SETLK, &
d1d90 6c 6f 63 6b 29 3b 0a 20 20 20 20 2a 70 45 72 72 lock);. *pErr
d1da0 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 code = errno;.
d1db0 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 63 6b 2e }else{. lock.
d1dc0 6c 5f 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 72 l_len = 0;. r
d1dd0 63 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d c = fcntl(pFile-
d1de0 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f >h, F_SETLK, &lo
d1df0 63 6b 29 3b 0a 20 20 20 20 2a 70 45 72 72 63 6f ck);. *pErrco
d1e00 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 de = errno;.
d1e10 69 66 28 20 4e 45 56 45 52 28 6f 70 3d 3d 46 5f if( NEVER(op==F_
d1e20 55 4e 4c 43 4b 29 20 7c 7c 20 72 63 21 3d 28 2d UNLCK) || rc!=(-
d1e30 31 29 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 1) ){. lock
d1e40 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 .l_start = 0;.
d1e50 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d lock.l_len =
d1e60 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 PENDING_BYTE;.
d1e70 20 20 20 20 20 72 63 20 3d 20 66 63 6e 74 6c 28 rc = fcntl(
d1e80 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c pFile->h, F_SETL
d1e90 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 K, &lock);.
d1ea0 20 69 66 28 20 41 4c 57 41 59 53 28 6f 70 21 3d if( ALWAYS(op!=
d1eb0 46 5f 55 4e 4c 43 4b 29 20 26 26 20 72 63 3d 3d F_UNLCK) && rc==
d1ec0 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 (-1) ){.
d1ed0 2a 70 45 72 72 63 6f 64 65 20 3d 20 65 72 72 6e *pErrcode = errn
d1ee0 6f 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e o;. lock.
d1ef0 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b l_type = F_UNLCK
d1f00 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c ;. lock.l
d1f10 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f _start = SHARED_
d1f20 46 49 52 53 54 3b 0a 20 20 20 20 20 20 20 20 6c FIRST;. l
d1f30 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 3b 0a 20 ock.l_len = 0;.
d1f40 20 20 20 20 20 20 20 66 63 6e 74 6c 28 70 46 69 fcntl(pFi
d1f50 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 le->h, F_SETLK,
d1f60 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a &lock);. }.
d1f70 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
d1f80 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
d1f90 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 Lock the file wi
d1fa0 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 th the lock spec
d1fb0 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 ified by paramet
d1fc0 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e er locktype - on
d1fd0 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c e.** of the foll
d1fe0 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 owing:.**.**
d1ff0 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b (1) SHARED_LOCK
d2000 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 .** (2) RESE
d2010 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 RVED_LOCK.**
d2020 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 (3) PENDING_LOC
d2030 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 K.** (4) EXC
d2040 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a LUSIVE_LOCK.**.*
d2050 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e * Sometimes when
d2060 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 requesting one
d2070 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 lock state, addi
d2080 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 tional lock stat
d2090 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 es.** are insert
d20a0 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 ed in between.
d20b0 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 The locking migh
d20c0 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 t fail on one of
d20d0 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 the later.** tr
d20e0 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e ansitions leavin
d20f0 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 g the lock state
d2100 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 different from
d2110 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20 what it started
d2120 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f but.** still sho
d2130 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 rt of its goal.
d2140 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 The following c
d2150 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 hart shows the a
d2160 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 llowed.** transi
d2170 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e tions and the in
d2180 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 serted intermedi
d2190 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a ate states:.**.*
d21a0 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e * UNLOCKED ->
d21b0 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 SHARED.** SH
d21c0 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 ARED -> RESERVED
d21d0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
d21e0 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 (PENDING) -> EX
d21f0 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 CLUSIVE.** RE
d2200 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 SERVED -> (PENDI
d2210 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
d2220 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d .** PENDING -
d2230 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a > EXCLUSIVE.**.*
d2240 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 * This routine w
d2250 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 ill only increas
d2260 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 e a lock. Use t
d2270 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f he sqlite3OsUnlo
d2280 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 ck().** routine
d2290 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 to lower a locki
d22a0 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 ng level..*/.sta
d22b0 74 69 63 20 69 6e 74 20 75 6e 69 78 4c 6f 63 6b tic int unixLock
d22c0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
d22d0 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 d, int locktype)
d22e0 7b 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f {. /* The follo
d22f0 77 69 6e 67 20 64 65 73 63 72 69 62 65 73 20 74 wing describes t
d2300 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
d2310 6e 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 n of the various
d2320 20 6c 6f 63 6b 73 20 61 6e 64 0a 20 20 2a 2a 20 locks and. **
d2330 6c 6f 63 6b 20 74 72 61 6e 73 69 74 69 6f 6e 73 lock transitions
d2340 20 69 6e 20 74 65 72 6d 73 20 6f 66 20 74 68 65 in terms of the
d2350 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 POSIX advisory
d2360 73 68 61 72 65 64 20 61 6e 64 20 65 78 63 6c 75 shared and exclu
d2370 73 69 76 65 0a 20 20 2a 2a 20 6c 6f 63 6b 20 70 sive. ** lock p
d2380 72 69 6d 69 74 69 76 65 73 20 28 63 61 6c 6c 65 rimitives (calle
d2390 64 20 72 65 61 64 2d 6c 6f 63 6b 73 20 61 6e 64 d read-locks and
d23a0 20 77 72 69 74 65 2d 6c 6f 63 6b 73 20 62 65 6c write-locks bel
d23b0 6f 77 2c 20 74 6f 20 61 76 6f 69 64 0a 20 20 2a ow, to avoid. *
d23c0 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 77 69 74 68 * confusion with
d23d0 20 53 51 4c 69 74 65 20 6c 6f 63 6b 20 6e 61 6d SQLite lock nam
d23e0 65 73 29 2e 20 54 68 65 20 61 6c 67 6f 72 69 74 es). The algorit
d23f0 68 6d 73 20 61 72 65 20 63 6f 6d 70 6c 69 63 61 hms are complica
d2400 74 65 64 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c ted. ** slightl
d2410 79 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 y in order to be
d2420 20 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68 compatible with
d2430 20 77 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 windows systems
d2440 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 0a simultaneously.
d2450 20 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74 ** accessing t
d2460 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
d2470 20 66 69 6c 65 2c 20 69 6e 20 63 61 73 65 20 74 file, in case t
d2480 68 61 74 20 69 73 20 65 76 65 72 20 72 65 71 75 hat is ever requ
d2490 69 72 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 ired.. **. **
d24a0 53 79 6d 62 6f 6c 73 20 64 65 66 69 6e 65 64 20 Symbols defined
d24b0 69 6e 20 6f 73 2e 68 20 69 6e 64 65 6e 74 69 66 in os.h indentif
d24c0 79 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20 62 y the 'pending b
d24d0 79 74 65 27 20 61 6e 64 20 74 68 65 20 27 72 65 yte' and the 're
d24e0 73 65 72 76 65 64 0a 20 20 2a 2a 20 62 79 74 65 served. ** byte
d24f0 27 2c 20 65 61 63 68 20 73 69 6e 67 6c 65 20 62 ', each single b
d2500 79 74 65 73 20 61 74 20 77 65 6c 6c 20 6b 6e 6f ytes at well kno
d2510 77 6e 20 6f 66 66 73 65 74 73 2c 20 61 6e 64 20 wn offsets, and
d2520 74 68 65 20 27 73 68 61 72 65 64 20 62 79 74 65 the 'shared byte
d2530 0a 20 20 2a 2a 20 72 61 6e 67 65 27 2c 20 61 20 . ** range', a
d2540 72 61 6e 67 65 20 6f 66 20 35 31 30 20 62 79 74 range of 510 byt
d2550 65 73 20 61 74 20 61 20 77 65 6c 6c 20 6b 6e 6f es at a well kno
d2560 77 6e 20 6f 66 66 73 65 74 2e 0a 20 20 2a 2a 0a wn offset.. **.
d2570 20 20 2a 2a 20 54 6f 20 6f 62 74 61 69 6e 20 61 ** To obtain a
d2580 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 61 20 SHARED lock, a
d2590 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 read-lock is obt
d25a0 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 27 70 65 ained on the 'pe
d25b0 6e 64 69 6e 67 0a 20 20 2a 2a 20 62 79 74 65 27 nding. ** byte'
d25c0 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 73 75 . If this is su
d25d0 63 63 65 73 73 66 75 6c 2c 20 61 20 72 61 6e 64 ccessful, a rand
d25e0 6f 6d 20 62 79 74 65 20 66 72 6f 6d 20 74 68 65 om byte from the
d25f0 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 'shared byte.
d2600 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 72 65 61 ** range' is rea
d2610 64 2d 6c 6f 63 6b 65 64 20 61 6e 64 20 74 68 65 d-locked and the
d2620 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 27 70 65 lock on the 'pe
d2630 6e 64 69 6e 67 20 62 79 74 65 27 20 72 65 6c 65 nding byte' rele
d2640 61 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 ased.. **. **
d2650 41 20 70 72 6f 63 65 73 73 20 6d 61 79 20 6f 6e A process may on
d2660 6c 79 20 6f 62 74 61 69 6e 20 61 20 52 45 53 45 ly obtain a RESE
d2670 52 56 45 44 20 6c 6f 63 6b 20 61 66 74 65 72 20 RVED lock after
d2680 69 74 20 68 61 73 20 61 20 53 48 41 52 45 44 20 it has a SHARED
d2690 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 41 20 52 45 53 lock.. ** A RES
d26a0 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20 69 6d ERVED lock is im
d26b0 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 67 72 61 plemented by gra
d26c0 62 62 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f bbing a write-lo
d26d0 63 6b 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 27 ck on the. ** '
d26e0 72 65 73 65 72 76 65 64 20 62 79 74 65 27 2e 20 reserved byte'.
d26f0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f . **. ** A pro
d2700 63 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 cess may only ob
d2710 74 61 69 6e 20 61 20 50 45 4e 44 49 4e 47 20 6c tain a PENDING l
d2720 6f 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 73 ock after it has
d2730 20 6f 62 74 61 69 6e 65 64 20 61 0a 20 20 2a 2a obtained a. **
d2740 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 41 20 SHARED lock. A
d2750 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 PENDING lock is
d2760 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 6f implemented by o
d2770 62 74 61 69 6e 69 6e 67 20 61 20 77 72 69 74 65 btaining a write
d2780 2d 6c 6f 63 6b 0a 20 20 2a 2a 20 6f 6e 20 74 68 -lock. ** on th
d2790 65 20 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 e 'pending byte'
d27a0 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 . This ensures t
d27b0 68 61 74 20 6e 6f 20 6e 65 77 20 53 48 41 52 45 hat no new SHARE
d27c0 44 20 6c 6f 63 6b 73 20 63 61 6e 20 62 65 0a 20 D locks can be.
d27d0 20 2a 2a 20 6f 62 74 61 69 6e 65 64 2c 20 62 75 ** obtained, bu
d27e0 74 20 65 78 69 73 74 69 6e 67 20 53 48 41 52 45 t existing SHARE
d27f0 44 20 6c 6f 63 6b 73 20 61 72 65 20 61 6c 6c 6f D locks are allo
d2800 77 65 64 20 74 6f 20 70 65 72 73 69 73 74 2e 20 wed to persist.
d2810 41 20 70 72 6f 63 65 73 73 0a 20 20 2a 2a 20 64 A process. ** d
d2820 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 oes not have to
d2830 6f 62 74 61 69 6e 20 61 20 52 45 53 45 52 56 45 obtain a RESERVE
d2840 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 77 61 D lock on the wa
d2850 79 20 74 6f 20 61 20 50 45 4e 44 49 4e 47 20 6c y to a PENDING l
d2860 6f 63 6b 2e 0a 20 20 2a 2a 20 54 68 69 73 20 70 ock.. ** This p
d2870 72 6f 70 65 72 74 79 20 69 73 20 75 73 65 64 20 roperty is used
d2880 62 79 20 74 68 65 20 61 6c 67 6f 72 69 74 68 6d by the algorithm
d2890 20 66 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 for rolling bac
d28a0 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 k a journal file
d28b0 0a 20 20 2a 2a 20 61 66 74 65 72 20 61 20 63 72 . ** after a cr
d28c0 61 73 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 ash.. **. ** A
d28d0 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b n EXCLUSIVE lock
d28e0 2c 20 6f 62 74 61 69 6e 65 64 20 61 66 74 65 72 , obtained after
d28f0 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 a PENDING lock
d2900 69 73 20 68 65 6c 64 2c 20 69 73 0a 20 20 2a 2a is held, is. **
d2910 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 implemented by
d2920 6f 62 74 61 69 6e 69 6e 67 20 61 20 77 72 69 74 obtaining a writ
d2930 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 65 6e e-lock on the en
d2940 74 69 72 65 20 27 73 68 61 72 65 64 20 62 79 74 tire 'shared byt
d2950 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 2e 20 53 e. ** range'. S
d2960 69 6e 63 65 20 61 6c 6c 20 6f 74 68 65 72 20 6c ince all other l
d2970 6f 63 6b 73 20 72 65 71 75 69 72 65 20 61 20 72 ocks require a r
d2980 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 6f 6e 65 20 ead-lock on one
d2990 6f 66 20 74 68 65 20 62 79 74 65 73 0a 20 20 2a of the bytes. *
d29a0 2a 20 77 69 74 68 69 6e 20 74 68 69 73 20 72 61 * within this ra
d29b0 6e 67 65 2c 20 74 68 69 73 20 65 6e 73 75 72 65 nge, this ensure
d29c0 73 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 s that no other
d29d0 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 20 6f locks are held o
d29e0 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 n the. ** datab
d29f0 61 73 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 ase. . **. **
d2a00 54 68 65 20 72 65 61 73 6f 6e 20 61 20 73 69 6e The reason a sin
d2a10 67 6c 65 20 62 79 74 65 20 63 61 6e 6e 6f 74 20 gle byte cannot
d2a20 62 65 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 be used instead
d2a30 6f 66 20 74 68 65 20 27 73 68 61 72 65 64 20 62 of the 'shared b
d2a40 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 20 yte. ** range'
d2a50 69 73 20 74 68 61 74 20 73 6f 6d 65 20 76 65 72 is that some ver
d2a60 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73 sions of windows
d2a70 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 do not support
d2a80 72 65 61 64 2d 6c 6f 63 6b 73 2e 20 42 79 0a 20 read-locks. By.
d2a90 20 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61 20 72 61 ** locking a ra
d2aa0 6e 64 6f 6d 20 62 79 74 65 20 66 72 6f 6d 20 61 ndom byte from a
d2ab0 20 72 61 6e 67 65 2c 20 63 6f 6e 63 75 72 72 65 range, concurre
d2ac0 6e 74 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 nt SHARED locks
d2ad0 6d 61 79 20 65 78 69 73 74 0a 20 20 2a 2a 20 65 may exist. ** e
d2ae0 76 65 6e 20 69 66 20 74 68 65 20 6c 6f 63 6b 69 ven if the locki
d2af0 6e 67 20 70 72 69 6d 69 74 69 76 65 20 75 73 65 ng primitive use
d2b00 64 20 69 73 20 61 6c 77 61 79 73 20 61 20 77 72 d is always a wr
d2b10 69 74 65 2d 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 ite-lock.. */.
d2b20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
d2b30 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 _OK;. unixFile
d2b40 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
d2b50 6c 65 2a 29 69 64 3b 0a 20 20 73 74 72 75 63 74 le*)id;. struct
d2b60 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 unixLockInfo *p
d2b70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c Lock = pFile->pL
d2b80 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74 20 66 6c ock;. struct fl
d2b90 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 ock lock;. int
d2ba0 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 74 45 72 s = 0;. int tEr
d2bb0 72 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rno;.. assert(
d2bc0 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 pFile );. OSTRA
d2bd0 43 45 37 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 CE7("LOCK %d
d2be0 25 73 20 77 61 73 20 25 73 28 25 73 2c 25 64 29 %s was %s(%s,%d)
d2bf0 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c pid=%d\n", pFil
d2c00 65 2d 3e 68 2c 0a 20 20 20 20 20 20 6c 6f 63 6b e->h,. lock
d2c10 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 typeName(locktyp
d2c20 65 29 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 e), locktypeName
d2c30 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 (pFile->locktype
d2c40 29 2c 0a 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 ),. locktyp
d2c50 65 4e 61 6d 65 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 eName(pLock->loc
d2c60 6b 74 79 70 65 29 2c 20 70 4c 6f 63 6b 2d 3e 63 ktype), pLock->c
d2c70 6e 74 20 2c 20 67 65 74 70 69 64 28 29 29 3b 0a nt , getpid());.
d2c80 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 . /* If there i
d2c90 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b s already a lock
d2ca0 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 of this type or
d2cb0 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 more restrictiv
d2cc0 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e e on the. ** un
d2cd0 69 78 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 ixFile, do nothi
d2ce0 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 ng. Don't use th
d2cf0 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 e end_lock: exit
d2d00 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 75 path, as. ** u
d2d10 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 nixEnterMutex()
d2d20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c hasn't been call
d2d30 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 ed yet.. */. i
d2d40 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
d2d50 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a pe>=locktype ){.
d2d60 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 4c 4f OSTRACE3("LO
d2d70 43 4b 20 20 20 20 25 64 20 25 73 20 6f 6b 20 28 CK %d %s ok (
d2d80 61 6c 72 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 already held)\n"
d2d90 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 , pFile->h,.
d2da0 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 locktype
d2db0 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29 3b Name(locktype));
d2dc0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d2dd0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a TE_OK;. }.. /*
d2de0 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c Make sure the l
d2df0 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 ocking sequence
d2e00 69 73 20 63 6f 72 72 65 63 74 2e 0a 20 20 2a 2a is correct.. **
d2e10 20 20 28 31 29 20 57 65 20 6e 65 76 65 72 20 6d (1) We never m
d2e20 6f 76 65 20 66 72 6f 6d 20 75 6e 6c 6f 63 6b 65 ove from unlocke
d2e30 64 20 74 6f 20 61 6e 79 74 68 69 6e 67 20 68 69 d to anything hi
d2e40 67 68 65 72 20 74 68 61 6e 20 73 68 61 72 65 64 gher than shared
d2e50 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 20 28 32 29 lock.. ** (2)
d2e60 20 53 51 4c 69 74 65 20 6e 65 76 65 72 20 65 78 SQLite never ex
d2e70 70 6c 69 63 69 74 6c 79 20 72 65 71 75 65 73 74 plicitly request
d2e80 73 20 61 20 70 65 6e 64 69 67 20 6c 6f 63 6b 2e s a pendig lock.
d2e90 0a 20 20 2a 2a 20 20 28 33 29 20 41 20 73 68 61 . ** (3) A sha
d2ea0 72 65 64 20 6c 6f 63 6b 20 69 73 20 61 6c 77 61 red lock is alwa
d2eb0 79 73 20 68 65 6c 64 20 77 68 65 6e 20 61 20 72 ys held when a r
d2ec0 65 73 65 72 76 65 20 6c 6f 63 6b 20 69 73 20 72 eserve lock is r
d2ed0 65 71 75 65 73 74 65 64 2e 0a 20 20 2a 2f 0a 20 equested.. */.
d2ee0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e assert( pFile->
d2ef0 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 locktype!=NO_LOC
d2f00 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 K || locktype==S
d2f10 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
d2f20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
d2f30 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 !=PENDING_LOCK )
d2f40 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b ;. assert( lock
d2f50 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c type!=RESERVED_L
d2f60 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f OCK || pFile->lo
d2f70 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
d2f80 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 OCK );.. /* Thi
d2f90 73 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65 s mutex is neede
d2fa0 64 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d d because pFile-
d2fb0 3e 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64 >pLock is shared
d2fc0 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 0a across threads.
d2fd0 20 20 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 */. unixEnter
d2fe0 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d Mutex();.. /* M
d2ff0 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 75 72 ake sure the cur
d3000 72 65 6e 74 20 74 68 72 65 61 64 20 6f 77 6e 73 rent thread owns
d3010 20 74 68 65 20 70 46 69 6c 65 2e 0a 20 20 2a 2f the pFile.. */
d3020 0a 20 20 72 63 20 3d 20 74 72 61 6e 73 66 65 72 . rc = transfer
d3030 4f 77 6e 65 72 73 68 69 70 28 70 46 69 6c 65 29 Ownership(pFile)
d3040 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
d3050 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 69 TE_OK ){. uni
d3060 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 xLeaveMutex();.
d3070 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
d3080 7d 0a 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c }. pLock = pFil
d3090 65 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 e->pLock;.. /*
d30a0 49 66 20 73 6f 6d 65 20 74 68 72 65 61 64 20 75 If some thread u
d30b0 73 69 6e 67 20 74 68 69 73 20 50 49 44 20 68 61 sing this PID ha
d30c0 73 20 61 20 6c 6f 63 6b 20 76 69 61 20 61 20 64 s a lock via a d
d30d0 69 66 66 65 72 65 6e 74 20 75 6e 69 78 46 69 6c ifferent unixFil
d30e0 65 2a 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 74 e*. ** handle t
d30f0 68 61 74 20 70 72 65 63 6c 75 64 65 73 20 74 68 hat precludes th
d3100 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b e requested lock
d3110 2c 20 72 65 74 75 72 6e 20 42 55 53 59 2e 0a 20 , return BUSY..
d3120 20 2a 2f 0a 20 20 69 66 28 20 28 70 46 69 6c 65 */. if( (pFile
d3130 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 70 4c 6f 63 ->locktype!=pLoc
d3140 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 26 26 20 0a k->locktype && .
d3150 20 20 20 20 20 20 20 20 20 20 28 70 4c 6f 63 6b (pLock
d3160 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 50 45 4e 44 ->locktype>=PEND
d3170 49 4e 47 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b ING_LOCK || lock
d3180 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b type>SHARED_LOCK
d3190 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d )). ){. rc =
d31a0 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
d31b0 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b goto end_lock;
d31c0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 . }.. /* If a
d31d0 53 48 41 52 45 44 20 6c 6f 63 6b 20 69 73 20 72 SHARED lock is r
d31e0 65 71 75 65 73 74 65 64 2c 20 61 6e 64 20 73 6f equested, and so
d31f0 6d 65 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 me thread using
d3200 74 68 69 73 20 50 49 44 20 61 6c 72 65 61 64 79 this PID already
d3210 0a 20 20 2a 2a 20 68 61 73 20 61 20 53 48 41 52 . ** has a SHAR
d3220 45 44 20 6f 72 20 52 45 53 45 52 56 45 44 20 6c ED or RESERVED l
d3230 6f 63 6b 2c 20 74 68 65 6e 20 69 6e 63 72 65 6d ock, then increm
d3240 65 6e 74 20 72 65 66 65 72 65 6e 63 65 20 63 6f ent reference co
d3250 75 6e 74 73 20 61 6e 64 0a 20 20 2a 2a 20 72 65 unts and. ** re
d3260 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a turn SQLITE_OK..
d3270 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 */. if( lockt
d3280 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
d3290 20 26 26 20 0a 20 20 20 20 20 20 28 70 4c 6f 63 && . (pLoc
d32a0 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 k->locktype==SHA
d32b0 52 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 4c 6f 63 RED_LOCK || pLoc
d32c0 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 k->locktype==RES
d32d0 45 52 56 45 44 5f 4c 4f 43 4b 29 20 29 7b 0a 20 ERVED_LOCK) ){.
d32e0 20 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 assert( lockt
d32f0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
d3300 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
d3310 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
d3320 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =0 );. assert
d3330 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3e 30 20 29 ( pLock->cnt>0 )
d3340 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 ;. pFile->loc
d3350 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c ktype = SHARED_L
d3360 4f 43 4b 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e OCK;. pLock->
d3370 63 6e 74 2b 2b 3b 0a 20 20 20 20 70 46 69 6c 65 cnt++;. pFile
d3380 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b ->pOpen->nLock++
d3390 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c ;. goto end_l
d33a0 6f 63 6b 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 ock;. }... /*
d33b0 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 A PENDING lock i
d33c0 73 20 6e 65 65 64 65 64 20 62 65 66 6f 72 65 20 s needed before
d33d0 61 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 acquiring a SHAR
d33e0 45 44 20 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f ED lock and befo
d33f0 72 65 0a 20 20 2a 2a 20 61 63 71 75 69 72 69 6e re. ** acquirin
d3400 67 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c g an EXCLUSIVE l
d3410 6f 63 6b 2e 20 20 46 6f 72 20 74 68 65 20 53 48 ock. For the SH
d3420 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 50 ARED lock, the P
d3430 45 4e 44 49 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a ENDING will. **
d3440 20 62 65 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 be released..
d3450 2a 2f 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 */. lock.l_len
d3460 3d 20 31 4c 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 = 1L;. lock.l_w
d3470 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 hence = SEEK_SET
d3480 3b 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 ;. if( locktype
d3490 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 ==SHARED_LOCK .
d34a0 20 20 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 || (locktyp
d34b0 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 e==EXCLUSIVE_LOC
d34c0 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b K && pFile->lock
d34d0 74 79 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 type<PENDING_LOC
d34e0 4b 29 0a 20 20 29 7b 0a 20 20 20 20 6c 6f 63 6b K). ){. lock
d34f0 2e 6c 5f 74 79 70 65 20 3d 20 28 6c 6f 63 6b 74 .l_type = (lockt
d3500 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
d3510 3f 46 5f 52 44 4c 43 4b 3a 46 5f 57 52 4c 43 4b ?F_RDLCK:F_WRLCK
d3520 29 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 );. lock.l_st
d3530 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 art = PENDING_BY
d3540 54 45 3b 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 TE;. s = fcnt
d3550 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 l(pFile->h, F_SE
d3560 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 TLK, &lock);.
d3570 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a if( s==(-1) ){.
d3580 20 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 tErrno = e
d3590 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d rrno;. rc =
d35a0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d sqliteErrorFrom
d35b0 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e PosixError(tErrn
d35c0 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f o, SQLITE_IOERR_
d35d0 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 LOCK);. if(
d35e0 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 IS_LOCK_ERROR(r
d35f0 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 c) ){. pF
d3600 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
d3610 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d tErrno;. }
d3620 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f . goto end_
d3630 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a lock;. }. }.
d3640 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f .. /* If contro
d3650 6c 20 67 65 74 73 20 74 6f 20 74 68 69 73 20 70 l gets to this p
d3660 6f 69 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61 oint, then actua
d3670 6c 6c 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64 lly go ahead and
d3680 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 make. ** opera
d3690 74 69 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c ting system call
d36a0 73 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 s for the specif
d36b0 69 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 ied lock.. */.
d36c0 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 if( locktype==S
d36d0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 HARED_LOCK ){.
d36e0 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d assert( pLock-
d36f0 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 >cnt==0 );. a
d3700 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f ssert( pLock->lo
d3710 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 0a 20 20 cktype==0 );..
d3720 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68 65 /* Now get the
d3730 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 read-lock */.
d3740 20 20 73 20 3d 20 72 61 6e 67 65 4c 6f 63 6b 28 s = rangeLock(
d3750 70 46 69 6c 65 2c 20 46 5f 52 44 4c 43 4b 2c 20 pFile, F_RDLCK,
d3760 26 74 45 72 72 6e 6f 29 3b 0a 0a 20 20 20 20 2f &tErrno);.. /
d3770 2a 20 44 72 6f 70 20 74 68 65 20 74 65 6d 70 6f * Drop the tempo
d3780 72 61 72 79 20 50 45 4e 44 49 4e 47 20 6c 6f 63 rary PENDING loc
d3790 6b 20 2a 2f 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f k */. lock.l_
d37a0 73 74 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f start = PENDING_
d37b0 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c BYTE;. lock.l
d37c0 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 20 20 20 20 6c _len = 1L;. l
d37d0 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 ock.l_type = F_U
d37e0 4e 4c 43 4b 3b 0a 20 20 20 20 69 66 28 20 66 63 NLCK;. if( fc
d37f0 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f ntl(pFile->h, F_
d3800 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 30 SETLK, &lock)!=0
d3810 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 20 ){. if( s
d3820 21 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 != -1 ){.
d3830 20 2f 2a 20 54 68 69 73 20 63 6f 75 6c 64 20 68 /* This could h
d3840 61 70 70 65 6e 20 77 69 74 68 20 61 20 6e 65 74 appen with a net
d3850 77 6f 72 6b 20 6d 6f 75 6e 74 20 2a 2f 0a 20 20 work mount */.
d3860 20 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 tErrno = e
d3870 72 72 6e 6f 3b 20 0a 20 20 20 20 20 20 20 20 72 rrno; . r
d3880 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 c = sqliteErrorF
d3890 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 romPosixError(tE
d38a0 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 rrno, SQLITE_IOE
d38b0 52 52 5f 55 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20 RR_UNLOCK); .
d38c0 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b if( IS_LOCK
d38d0 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 _ERROR(rc) ){.
d38e0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c pFile->l
d38f0 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e astErrno = tErrn
d3900 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 o;. }.
d3910 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f goto end_lo
d3920 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ck;. }.
d3930 7d 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 }. if( s==(-1
d3940 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 ) ){. rc =
d3950 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 sqliteErrorFromP
d3960 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f osixError(tErrno
d3970 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c , SQLITE_IOERR_L
d3980 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 OCK);. if(
d3990 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 IS_LOCK_ERROR(rc
d39a0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 ) ){. pFi
d39b0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d39c0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a tErrno;. }.
d39d0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
d39e0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d39f0 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a = SHARED_LOCK;.
d3a00 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 pFile->pOp
d3a10 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 en->nLock++;.
d3a20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20 pLock->cnt =
d3a30 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 1;. }. }else
d3a40 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 if( locktype==E
d3a50 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 XCLUSIVE_LOCK &&
d3a60 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3e 31 20 29 7b pLock->cnt>1 ){
d3a70 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 74 . /* We are t
d3a80 72 79 69 6e 67 20 66 6f 72 20 61 6e 20 65 78 63 rying for an exc
d3a90 6c 75 73 69 76 65 20 6c 6f 63 6b 20 62 75 74 20 lusive lock but
d3aa0 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 another thread i
d3ab0 6e 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 73 61 n this. ** sa
d3ac0 6d 65 20 70 72 6f 63 65 73 73 20 69 73 20 73 74 me process is st
d3ad0 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 20 73 68 ill holding a sh
d3ae0 61 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 ared lock. */.
d3af0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 rc = SQLITE_BU
d3b00 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 SY;. }else{.
d3b10 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74 20 /* The request
d3b20 77 61 73 20 66 6f 72 20 61 20 52 45 53 45 52 56 was for a RESERV
d3b30 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 ED or EXCLUSIVE
d3b40 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0a 20 20 20 lock. It is.
d3b50 20 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 61 74 ** assumed that
d3b60 20 74 68 65 72 65 20 69 73 20 61 20 53 48 41 52 there is a SHAR
d3b70 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f ED or greater lo
d3b80 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20 ck on the file.
d3b90 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20 ** already..
d3ba0 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
d3bb0 28 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b ( 0!=pFile->lock
d3bc0 74 79 70 65 20 29 3b 0a 20 20 20 20 6c 6f 63 6b type );. lock
d3bd0 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 .l_type = F_WRLC
d3be0 4b 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 6c K;. switch( l
d3bf0 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 20 ocktype ){.
d3c00 20 63 61 73 65 20 52 45 53 45 52 56 45 44 5f 4c case RESERVED_L
d3c10 4f 43 4b 3a 0a 20 20 20 20 20 20 20 20 6c 6f 63 OCK:. loc
d3c20 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 52 45 53 45 k.l_start = RESE
d3c30 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 20 RVED_BYTE;.
d3c40 20 20 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 s = fcntl(pFi
d3c50 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 le->h, F_SETLK,
d3c60 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 20 20 &lock);.
d3c70 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
d3c80 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
d3c90 20 20 20 20 20 63 61 73 65 20 45 58 43 4c 55 53 case EXCLUS
d3ca0 49 56 45 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 20 IVE_LOCK:.
d3cb0 20 20 73 20 3d 20 72 61 6e 67 65 4c 6f 63 6b 28 s = rangeLock(
d3cc0 70 46 69 6c 65 2c 20 46 5f 57 52 4c 43 4b 2c 20 pFile, F_WRLCK,
d3cd0 26 74 45 72 72 6e 6f 29 3b 0a 20 20 20 20 20 20 &tErrno);.
d3ce0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 64 break;. d
d3cf0 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 efault:.
d3d00 61 73 73 65 72 74 28 30 29 3b 0a 20 20 20 20 7d assert(0);. }
d3d10 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 . if( s==(-1)
d3d20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
d3d30 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
d3d40 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
d3d50 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f SQLITE_IOERR_LO
d3d60 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 CK);. if( I
d3d70 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 S_LOCK_ERROR(rc)
d3d80 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c ){. pFil
d3d90 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 e->lastErrno = t
d3da0 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 Errno;. }.
d3db0 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 0a 23 69 66 }. }. ..#if
d3dc0 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a ndef NDEBUG. /*
d3dd0 20 53 65 74 20 75 70 20 74 68 65 20 74 72 61 6e Set up the tran
d3de0 73 61 63 74 69 6f 6e 2d 63 6f 75 6e 74 65 72 20 saction-counter
d3df0 63 68 61 6e 67 65 20 63 68 65 63 6b 69 6e 67 20 change checking
d3e00 66 6c 61 67 73 20 77 68 65 6e 0a 20 20 2a 2a 20 flags when. **
d3e10 74 72 61 6e 73 69 74 69 6f 6e 69 6e 67 20 66 72 transitioning fr
d3e20 6f 6d 20 61 20 53 48 41 52 45 44 20 74 6f 20 61 om a SHARED to a
d3e30 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e 20 RESERVED lock.
d3e40 20 54 68 65 20 63 68 61 6e 67 65 0a 20 20 2a 2a The change. **
d3e50 20 66 72 6f 6d 20 53 48 41 52 45 44 20 74 6f 20 from SHARED to
d3e60 52 45 53 45 52 56 45 44 20 6d 61 72 6b 73 20 74 RESERVED marks t
d3e70 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
d3e80 61 20 6e 6f 72 6d 61 6c 0a 20 20 2a 2a 20 77 72 a normal. ** wr
d3e90 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 28 6e ite operation (n
d3ea0 6f 74 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c ot a hot journal
d3eb0 20 72 6f 6c 6c 62 61 63 6b 29 2e 0a 20 20 2a 2f rollback).. */
d3ec0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
d3ed0 45 5f 4f 4b 0a 20 20 20 26 26 20 70 46 69 6c 65 E_OK. && pFile
d3ee0 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 ->locktype<=SHAR
d3ef0 45 44 5f 4c 4f 43 4b 0a 20 20 20 26 26 20 6c 6f ED_LOCK. && lo
d3f00 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 cktype==RESERVED
d3f10 5f 4c 4f 43 4b 0a 20 20 29 7b 0a 20 20 20 20 70 _LOCK. ){. p
d3f20 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 File->transCntrC
d3f30 68 6e 67 20 3d 20 30 3b 0a 20 20 20 20 70 46 69 hng = 0;. pFi
d3f40 6c 65 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30 le->dbUpdate = 0
d3f50 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e ;. pFile->inN
d3f60 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20 31 3b 0a ormalWrite = 1;.
d3f70 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 69 }.#endif... i
d3f80 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
d3f90 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ){. pFile->l
d3fa0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 ocktype = lockty
d3fb0 70 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c pe;. pLock->l
d3fc0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 ocktype = lockty
d3fd0 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 pe;. }else if(
d3fe0 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 locktype==EXCLUS
d3ff0 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 IVE_LOCK ){.
d4000 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
d4010 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a = PENDING_LOCK;.
d4020 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 pLock->lockt
d4030 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f ype = PENDING_LO
d4040 43 4b 3b 0a 20 20 7d 0a 0a 65 6e 64 5f 6c 6f 63 CK;. }..end_loc
d4050 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 k:. unixLeaveMu
d4060 74 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 tex();. OSTRACE
d4070 34 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 4("LOCK %d %s
d4080 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 %s\n", pFile->h
d4090 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c , locktypeName(l
d40a0 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20 ocktype), .
d40b0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f rc==SQLITE_OK ?
d40c0 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 "ok" : "failed"
d40d0 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
d40e0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 }../*.** Close a
d40f0 6c 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ll file descript
d4100 6f 72 73 20 61 63 63 75 6d 75 61 74 65 64 20 69 ors accumuated i
d4110 6e 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e n the unixOpenCn
d4120 74 2d 3e 70 55 6e 75 73 65 64 20 6c 69 73 74 2e t->pUnused list.
d4130 0a 2a 2a 20 49 66 20 61 6c 6c 20 73 75 63 68 20 .** If all such
d4140 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 file descriptors
d4150 20 61 72 65 20 63 6c 6f 73 65 64 20 77 69 74 68 are closed with
d4160 6f 75 74 20 65 72 72 6f 72 2c 20 74 68 65 20 6c out error, the l
d4170 69 73 74 20 69 73 0a 2a 2a 20 63 6c 65 61 72 65 ist is.** cleare
d4180 64 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 d and SQLITE_OK
d4190 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
d41a0 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e Otherwise, if an
d41b0 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 error occurs, t
d41c0 68 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 hen successfully
d41d0 20 63 6c 6f 73 65 64 20 66 69 6c 65 20 64 65 73 closed file des
d41e0 63 72 69 70 74 6f 72 0a 2a 2a 20 65 6e 74 72 69 criptor.** entri
d41f0 65 73 20 61 72 65 20 72 65 6d 6f 76 65 64 20 66 es are removed f
d4200 72 6f 6d 20 74 68 65 20 6c 69 73 74 2c 20 61 6e rom the list, an
d4210 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 d SQLITE_IOERR_C
d4220 4c 4f 53 45 20 72 65 74 75 72 6e 65 64 2e 20 0a LOSE returned. .
d4230 2a 2a 20 6e 6f 74 20 64 65 6c 65 74 65 64 20 61 ** not deleted a
d4240 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f nd SQLITE_IOERR_
d4250 43 4c 4f 53 45 20 72 65 74 75 72 6e 65 64 2e 0a CLOSE returned..
d4260 2a 2f 20 0a 73 74 61 74 69 63 20 69 6e 74 20 63 */ .static int c
d4270 6c 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 28 75 losePendingFds(u
d4280 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b nixFile *pFile){
d4290 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
d42a0 54 45 5f 4f 4b 3b 0a 20 20 73 74 72 75 63 74 20 TE_OK;. struct
d42b0 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 unixOpenCnt *pOp
d42c0 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 en = pFile->pOpe
d42d0 6e 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 n;. UnixUnusedF
d42e0 64 20 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 d *pError = 0;.
d42f0 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 UnixUnusedFd *p
d4300 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 ;. UnixUnusedFd
d4310 20 2a 70 4e 65 78 74 3b 0a 20 20 66 6f 72 28 70 *pNext;. for(p
d4320 3d 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 3b =pOpen->pUnused;
d4330 20 70 3b 20 70 3d 70 4e 65 78 74 29 7b 0a 20 20 p; p=pNext){.
d4340 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 pNext = p->pNe
d4350 78 74 3b 0a 20 20 20 20 69 66 28 20 63 6c 6f 73 xt;. if( clos
d4360 65 28 70 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 e(p->fd) ){.
d4370 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d4380 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
d4390 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
d43a0 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 ERR_CLOSE;.
d43b0 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 45 72 72 p->pNext = pErr
d43c0 6f 72 3b 0a 20 20 20 20 20 20 70 45 72 72 6f 72 or;. pError
d43d0 20 3d 20 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b = p;. }else{
d43e0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 . sqlite3_f
d43f0 72 65 65 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 ree(p);. }.
d4400 7d 0a 20 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 }. pOpen->pUnus
d4410 65 64 20 3d 20 70 45 72 72 6f 72 3b 0a 20 20 72 ed = pError;. r
d4420 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
d4430 2a 2a 20 41 64 64 20 74 68 65 20 66 69 6c 65 20 ** Add the file
d4440 64 65 73 63 72 69 70 74 6f 72 20 75 73 65 64 20 descriptor used
d4450 62 79 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 70 by file handle p
d4460 46 69 6c 65 20 74 6f 20 74 68 65 20 63 6f 72 72 File to the corr
d4470 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 70 55 6e esponding.** pUn
d4480 75 73 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 used list..*/.st
d4490 61 74 69 63 20 76 6f 69 64 20 73 65 74 50 65 6e atic void setPen
d44a0 64 69 6e 67 46 64 28 75 6e 69 78 46 69 6c 65 20 dingFd(unixFile
d44b0 2a 70 46 69 6c 65 29 7b 0a 20 20 73 74 72 75 63 *pFile){. struc
d44c0 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 t unixOpenCnt *p
d44d0 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f Open = pFile->pO
d44e0 70 65 6e 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 pen;. UnixUnuse
d44f0 64 46 64 20 2a 70 20 3d 20 70 46 69 6c 65 2d 3e dFd *p = pFile->
d4500 70 55 6e 75 73 65 64 3b 0a 20 20 70 2d 3e 70 4e pUnused;. p->pN
d4510 65 78 74 20 3d 20 70 4f 70 65 6e 2d 3e 70 55 6e ext = pOpen->pUn
d4520 75 73 65 64 3b 0a 20 20 70 4f 70 65 6e 2d 3e 70 used;. pOpen->p
d4530 55 6e 75 73 65 64 20 3d 20 70 3b 0a 20 20 70 46 Unused = p;. pF
d4540 69 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20 20 70 ile->h = -1;. p
d4550 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64 20 3d 20 File->pUnused =
d4560 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 0;.}../*.** Lowe
d4570 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 r the locking le
d4580 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 vel on file desc
d4590 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 riptor pFile to
d45a0 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 locktype. lockt
d45b0 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 ype.** must be e
d45c0 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 ither NO_LOCK or
d45d0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a SHARED_LOCK..**
d45e0 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 .** If the locki
d45f0 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 ng level of the
d4600 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
d4610 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 is already at or
d4620 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 below.** the re
d4630 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 quested locking
d4640 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 level, this rout
d4650 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ine is a no-op..
d4660 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
d4670 69 78 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 ixUnlock(sqlite3
d4680 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c _file *id, int l
d4690 6f 63 6b 74 79 70 65 29 7b 0a 20 20 75 6e 69 78 ocktype){. unix
d46a0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
d46b0 6e 69 78 46 69 6c 65 2a 29 69 64 3b 20 2f 2a 20 nixFile*)id; /*
d46c0 54 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f The open file */
d46d0 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f . struct unixLo
d46e0 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b 20 20 ckInfo *pLock;
d46f0 20 20 20 20 2f 2a 20 53 74 72 75 63 74 75 72 65 /* Structure
d4700 20 64 65 73 63 72 69 62 69 6e 67 20 63 75 72 72 describing curr
d4710 65 6e 74 20 6c 6f 63 6b 20 73 74 61 74 65 20 2a ent lock state *
d4720 2f 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b /. struct flock
d4730 20 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 lock;
d4740 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 /* Informat
d4750 69 6f 6e 20 70 61 73 73 65 64 20 69 6e 74 6f 20 ion passed into
d4760 66 63 6e 74 6c 28 29 20 2a 2f 0a 20 20 69 6e 74 fcntl() */. int
d4770 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
d4780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
d4790 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f Return code fro
d47a0 6d 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 m this interface
d47b0 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 */. int h;
d47c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d47d0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e /* The un
d47e0 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 64 65 derlying file de
d47f0 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e scriptor */. in
d4800 74 20 74 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 t tErrno;
d4810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
d4820 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 66 72 6f * Error code fro
d4830 6d 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 65 72 m system call er
d4840 72 6f 72 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 rors */.. asser
d4850 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 t( pFile );. OS
d4860 54 52 41 43 45 37 28 22 55 4e 4c 4f 43 4b 20 20 TRACE7("UNLOCK
d4870 25 64 20 25 64 20 77 61 73 20 25 64 28 25 64 2c %d %d was %d(%d,
d4880 25 64 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 %d) pid=%d\n", p
d4890 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
d48a0 65 2c 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e e,. pFile->
d48b0 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d locktype, pFile-
d48c0 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 >pLock->locktype
d48d0 2c 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e , pFile->pLock->
d48e0 63 6e 74 2c 20 67 65 74 70 69 64 28 29 29 3b 0a cnt, getpid());.
d48f0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
d4900 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype<=SHARED_LOCK
d4910 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d );. if( pFile-
d4920 3e 6c 6f 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 >locktype<=lockt
d4930 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ype ){. retur
d4940 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
d4950 0a 20 20 69 66 28 20 43 48 45 43 4b 5f 54 48 52 . if( CHECK_THR
d4960 45 41 44 49 44 28 70 46 69 6c 65 29 20 29 7b 0a EADID(pFile) ){.
d4970 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d4980 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 E_MISUSE;. }.
d4990 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
d49a0 3b 0a 20 20 68 20 3d 20 70 46 69 6c 65 2d 3e 68 ;. h = pFile->h
d49b0 3b 0a 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c ;. pLock = pFil
d49c0 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 61 73 73 65 e->pLock;. asse
d49d0 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 21 3d rt( pLock->cnt!=
d49e0 30 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 0 );. if( pFile
d49f0 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 ->locktype>SHARE
d4a00 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 D_LOCK ){. as
d4a10 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 sert( pLock->loc
d4a20 6b 74 79 70 65 3d 3d 70 46 69 6c 65 2d 3e 6c 6f ktype==pFile->lo
d4a30 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 53 69 cktype );. Si
d4a40 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e mulateIOErrorBen
d4a50 69 67 6e 28 31 29 3b 0a 20 20 20 20 53 69 6d 75 ign(1);. Simu
d4a60 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 68 3d 28 lateIOError( h=(
d4a70 2d 31 29 20 29 0a 20 20 20 20 53 69 6d 75 6c 61 -1) ). Simula
d4a80 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 teIOErrorBenign(
d4a90 30 29 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 0);..#ifndef NDE
d4aa0 42 55 47 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 BUG. /* When
d4ab0 72 65 64 75 63 69 6e 67 20 61 20 6c 6f 63 6b 20 reducing a lock
d4ac0 73 75 63 68 20 74 68 61 74 20 6f 74 68 65 72 20 such that other
d4ad0 70 72 6f 63 65 73 73 65 73 20 63 61 6e 20 73 74 processes can st
d4ae0 61 72 74 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 art. ** readi
d4af0 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ng the database
d4b00 66 69 6c 65 20 61 67 61 69 6e 2c 20 6d 61 6b 65 file again, make
d4b10 20 73 75 72 65 20 74 68 61 74 20 74 68 65 0a 20 sure that the.
d4b20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f ** transactio
d4b30 6e 20 63 6f 75 6e 74 65 72 20 77 61 73 20 75 70 n counter was up
d4b40 64 61 74 65 64 20 69 66 20 61 6e 79 20 70 61 72 dated if any par
d4b50 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 t of the databas
d4b60 65 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 68 e. ** file ch
d4b70 61 6e 67 65 64 2e 20 20 49 66 20 74 68 65 20 74 anged. If the t
d4b80 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 ransaction count
d4b90 65 72 20 69 73 20 6e 6f 74 20 75 70 64 61 74 65 er is not update
d4ba0 64 2c 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 d,. ** other
d4bb0 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74 connections to t
d4bc0 68 65 20 73 61 6d 65 20 66 69 6c 65 20 6d 69 67 he same file mig
d4bd0 68 74 20 6e 6f 74 20 72 65 61 6c 69 7a 65 20 74 ht not realize t
d4be0 68 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 hat. ** the f
d4bf0 69 6c 65 20 68 61 73 20 63 68 61 6e 67 65 64 20 ile has changed
d4c00 61 6e 64 20 68 65 6e 63 65 20 6d 69 67 68 74 20 and hence might
d4c10 6e 6f 74 20 6b 6e 6f 77 20 74 6f 20 66 6c 75 73 not know to flus
d4c20 68 20 74 68 65 69 72 0a 20 20 20 20 2a 2a 20 63 h their. ** c
d4c30 61 63 68 65 2e 20 20 54 68 65 20 75 73 65 20 6f ache. The use o
d4c40 66 20 61 20 73 74 61 6c 65 20 63 61 63 68 65 20 f a stale cache
d4c50 63 61 6e 20 6c 65 61 64 20 74 6f 20 64 61 74 61 can lead to data
d4c60 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e base corruption.
d4c70 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
d4c80 72 74 28 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 rt( pFile->inNor
d4c90 6d 61 6c 57 72 69 74 65 3d 3d 30 0a 20 20 20 20 malWrite==0.
d4ca0 20 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 64 || pFile->d
d4cb0 62 55 70 64 61 74 65 3d 3d 30 0a 20 20 20 20 20 bUpdate==0.
d4cc0 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 74 72 || pFile->tr
d4cd0 61 6e 73 43 6e 74 72 43 68 6e 67 3d 3d 31 20 29 ansCntrChng==1 )
d4ce0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e ;. pFile->inN
d4cf0 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20 30 3b 0a ormalWrite = 0;.
d4d00 23 65 6e 64 69 66 0a 0a 0a 20 20 20 20 69 66 28 #endif... if(
d4d10 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 locktype==SHARE
d4d20 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 D_LOCK ){.
d4d30 69 66 28 20 72 61 6e 67 65 4c 6f 63 6b 28 70 46 if( rangeLock(pF
d4d40 69 6c 65 2c 20 46 5f 52 44 4c 43 4b 2c 20 26 74 ile, F_RDLCK, &t
d4d50 45 72 72 6e 6f 29 3d 3d 28 2d 31 29 20 29 7b 0a Errno)==(-1) ){.
d4d60 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
d4d70 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
d4d80 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
d4d90 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f QLITE_IOERR_RDLO
d4da0 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 CK);. if(
d4db0 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 IS_LOCK_ERROR(r
d4dc0 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 c) ){.
d4dd0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
d4de0 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 = tErrno;.
d4df0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 }. got
d4e00 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 o end_unlock;.
d4e10 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
d4e20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f lock.l_type = F_
d4e30 55 4e 4c 43 4b 3b 0a 20 20 20 20 6c 6f 63 6b 2e UNLCK;. lock.
d4e40 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f l_whence = SEEK_
d4e50 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f SET;. lock.l_
d4e60 73 74 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f start = PENDING_
d4e70 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c BYTE;. lock.l
d4e80 5f 6c 65 6e 20 3d 20 32 4c 3b 20 20 61 73 73 65 _len = 2L; asse
d4e90 72 74 28 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 rt( PENDING_BYTE
d4ea0 2b 31 3d 3d 52 45 53 45 52 56 45 44 5f 42 59 54 +1==RESERVED_BYT
d4eb0 45 20 29 3b 0a 20 20 20 20 69 66 28 20 66 63 6e E );. if( fcn
d4ec0 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 tl(h, F_SETLK, &
d4ed0 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 7b 0a 20 lock)!=(-1) ){.
d4ee0 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b pLock->lock
d4ef0 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f type = SHARED_LO
d4f00 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 CK;. }else{.
d4f10 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 tErrno = er
d4f20 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 rno;. rc =
d4f30 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 sqliteErrorFromP
d4f40 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f osixError(tErrno
d4f50 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 , SQLITE_IOERR_U
d4f60 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 NLOCK);. if
d4f70 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 ( IS_LOCK_ERROR(
d4f80 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 rc) ){. p
d4f90 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
d4fa0 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = tErrno;.
d4fb0 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 }. goto end
d4fc0 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 _unlock;. }.
d4fd0 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 }. if( locktyp
d4fe0 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 e==NO_LOCK ){.
d4ff0 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 struct unixOpe
d5000 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 0a 0a 20 20 nCnt *pOpen;..
d5010 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 /* Decrement t
d5020 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 63 he shared lock c
d5030 6f 75 6e 74 65 72 2e 20 20 52 65 6c 65 61 73 65 ounter. Release
d5040 20 74 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 the lock using
d5050 61 6e 0a 20 20 20 20 2a 2a 20 4f 53 20 63 61 6c an. ** OS cal
d5060 6c 20 6f 6e 6c 79 20 77 68 65 6e 20 61 6c 6c 20 l only when all
d5070 74 68 72 65 61 64 73 20 69 6e 20 74 68 69 73 20 threads in this
d5080 73 61 6d 65 20 70 72 6f 63 65 73 73 20 68 61 76 same process hav
d5090 65 20 72 65 6c 65 61 73 65 64 0a 20 20 20 20 2a e released. *
d50a0 2a 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 * the lock..
d50b0 2a 2f 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e */. pLock->cn
d50c0 74 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f t--;. if( pLo
d50d0 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 ck->cnt==0 ){.
d50e0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 lock.l_type
d50f0 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 20 = F_UNLCK;.
d5100 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d lock.l_whence =
d5110 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20 SEEK_SET;.
d5120 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 lock.l_start =
d5130 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 4c 3b lock.l_len = 0L;
d5140 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 . SimulateI
d5150 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b OErrorBenign(1);
d5160 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 . SimulateI
d5170 4f 45 72 72 6f 72 28 20 68 3d 28 2d 31 29 20 29 OError( h=(-1) )
d5180 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 . SimulateI
d5190 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b OErrorBenign(0);
d51a0 0a 20 20 20 20 20 20 69 66 28 20 66 63 6e 74 6c . if( fcntl
d51b0 28 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f (h, F_SETLK, &lo
d51c0 63 6b 29 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20 ck)!=(-1) ){.
d51d0 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b pLock->lock
d51e0 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a type = NO_LOCK;.
d51f0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
d5200 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 tErrno = er
d5210 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 63 20 rno;. rc
d5220 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f = sqliteErrorFro
d5230 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 mPosixError(tErr
d5240 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 no, SQLITE_IOERR
d5250 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 _UNLOCK);.
d5260 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
d5270 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 ROR(rc) ){.
d5280 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
d5290 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a Errno = tErrno;.
d52a0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
d52b0 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 pLock->locktyp
d52c0 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 e = NO_LOCK;.
d52d0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b pFile->lock
d52e0 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a type = NO_LOCK;.
d52f0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 }. }..
d5300 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 /* Decrement
d5310 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 6c 6f 63 the count of loc
d5320 6b 73 20 61 67 61 69 6e 73 74 20 74 68 69 73 20 ks against this
d5330 73 61 6d 65 20 66 69 6c 65 2e 20 20 57 68 65 6e same file. When
d5340 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 75 6e the. ** coun
d5350 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 t reaches zero,
d5360 63 6c 6f 73 65 20 61 6e 79 20 6f 74 68 65 72 20 close any other
d5370 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 file descriptors
d5380 20 77 68 6f 73 65 20 63 6c 6f 73 65 0a 20 20 20 whose close.
d5390 20 2a 2a 20 77 61 73 20 64 65 66 65 72 72 65 64 ** was deferred
d53a0 20 62 65 63 61 75 73 65 20 6f 66 20 6f 75 74 73 because of outs
d53b0 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2e 0a 20 tanding locks..
d53c0 20 20 20 2a 2f 0a 20 20 20 20 70 4f 70 65 6e 20 */. pOpen
d53d0 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a = pFile->pOpen;.
d53e0 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b pOpen->nLock
d53f0 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 --;. assert(
d5400 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30 20 pOpen->nLock>=0
d5410 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 65 6e );. if( pOpen
d5420 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 ->nLock==0 ){.
d5430 20 20 20 20 69 6e 74 20 72 63 32 20 3d 20 63 6c int rc2 = cl
d5440 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 28 70 46 osePendingFds(pF
d5450 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ile);. if(
d5460 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
d5470 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 . rc = rc
d5480 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 2;. }. }
d5490 0a 20 20 7d 0a 09 0a 65 6e 64 5f 75 6e 6c 6f 63 . }...end_unloc
d54a0 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 k:. unixLeaveMu
d54b0 74 65 78 28 29 3b 0a 20 20 69 66 28 20 72 63 3d tex();. if( rc=
d54c0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 70 46 69 =SQLITE_OK ) pFi
d54d0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c le->locktype = l
d54e0 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 ocktype;. retur
d54f0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
d5500 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 70 65 72 his function per
d5510 66 6f 72 6d 73 20 74 68 65 20 70 61 72 74 73 20 forms the parts
d5520 6f 66 20 74 68 65 20 22 63 6c 6f 73 65 20 66 69 of the "close fi
d5530 6c 65 22 20 6f 70 65 72 61 74 69 6f 6e 20 0a 2a le" operation .*
d5540 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 * common to all
d5550 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 73 2e locking schemes.
d5560 20 49 74 20 63 6c 6f 73 65 73 20 74 68 65 20 64 It closes the d
d5570 69 72 65 63 74 6f 72 79 20 61 6e 64 20 66 69 6c irectory and fil
d5580 65 0a 2a 2a 20 68 61 6e 64 6c 65 73 2c 20 69 66 e.** handles, if
d5590 20 74 68 65 79 20 61 72 65 20 76 61 6c 69 64 2c they are valid,
d55a0 20 61 6e 64 20 73 65 74 73 20 61 6c 6c 20 66 69 and sets all fi
d55b0 65 6c 64 73 20 6f 66 20 74 68 65 20 75 6e 69 78 elds of the unix
d55c0 46 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 File.** structur
d55d0 65 20 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 74 e to 0..**.** It
d55e0 20 69 73 20 2a 6e 6f 74 2a 20 6e 65 63 65 73 73 is *not* necess
d55f0 61 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ary to hold the
d5600 6d 75 74 65 78 20 77 68 65 6e 20 74 68 69 73 20 mutex when this
d5610 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
d5620 64 2c 0a 2a 2a 20 65 76 65 6e 20 6f 6e 20 56 78 d,.** even on Vx
d5630 57 6f 72 6b 73 2e 20 20 41 20 6d 75 74 65 78 20 Works. A mutex
d5640 77 69 6c 6c 20 62 65 20 61 63 71 75 69 72 65 64 will be acquired
d5650 20 6f 6e 20 56 78 57 6f 72 6b 73 20 62 79 20 74 on VxWorks by t
d5660 68 65 0a 2a 2a 20 76 78 77 6f 72 6b 73 52 65 6c he.** vxworksRel
d5670 65 61 73 65 46 69 6c 65 49 64 28 29 20 72 6f 75 easeFileId() rou
d5680 74 69 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tine..*/.static
d5690 69 6e 74 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c int closeUnixFil
d56a0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
d56b0 69 64 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 id){. unixFile
d56c0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
d56d0 6c 65 2a 29 69 64 3b 0a 20 20 69 66 28 20 70 46 le*)id;. if( pF
d56e0 69 6c 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70 ile ){. if( p
d56f0 46 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29 File->dirfd>=0 )
d5700 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 {. int err
d5710 3d 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64 = close(pFile->d
d5720 69 72 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28 irfd);. if(
d5730 20 65 72 72 20 29 7b 0a 20 20 20 20 20 20 20 20 err ){.
d5740 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
d5750 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
d5760 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d5770 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b IOERR_DIR_CLOSE;
d5780 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
d5790 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 64 69 72 pFile->dir
d57a0 66 64 3d 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 fd=-1;. }.
d57b0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 46 69 }. if( pFi
d57c0 6c 65 2d 3e 68 3e 3d 30 20 29 7b 0a 20 20 20 20 le->h>=0 ){.
d57d0 20 20 69 6e 74 20 65 72 72 20 3d 20 63 6c 6f 73 int err = clos
d57e0 65 28 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 e(pFile->h);.
d57f0 20 20 20 69 66 28 20 65 72 72 20 29 7b 0a 20 20 if( err ){.
d5800 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
d5810 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
d5820 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
d5830 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 QLITE_IOERR_CLOS
d5840 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d E;. }. }
d5850 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a .#if OS_VXWORKS.
d5860 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 if( pFile->p
d5870 49 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 Id ){. if(
d5880 70 46 69 6c 65 2d 3e 69 73 44 65 6c 65 74 65 20 pFile->isDelete
d5890 29 7b 0a 20 20 20 20 20 20 20 20 75 6e 6c 69 6e ){. unlin
d58a0 6b 28 70 46 69 6c 65 2d 3e 70 49 64 2d 3e 7a 43 k(pFile->pId->zC
d58b0 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 anonicalName);.
d58c0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 76 78 77 }. vxw
d58d0 6f 72 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49 orksReleaseFileI
d58e0 64 28 70 46 69 6c 65 2d 3e 70 49 64 29 3b 0a 20 d(pFile->pId);.
d58f0 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 49 64 20 pFile->pId
d5900 3d 20 30 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 = 0;. }.#endi
d5910 66 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 f. OSTRACE2("
d5920 43 4c 4f 53 45 20 20 20 25 2d 33 64 5c 6e 22 2c CLOSE %-3d\n",
d5930 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 pFile->h);.
d5940 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b OpenCounter(-1);
d5950 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
d5960 65 28 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64 e(pFile->pUnused
d5970 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 46 );. memset(pF
d5980 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 ile, 0, sizeof(u
d5990 6e 69 78 46 69 6c 65 29 29 3b 0a 20 20 7d 0a 20 nixFile));. }.
d59a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
d59b0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 K;.}../*.** Clos
d59c0 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 e a file..*/.sta
d59d0 74 69 63 20 69 6e 74 20 75 6e 69 78 43 6c 6f 73 tic int unixClos
d59e0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
d59f0 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 id){. int rc =
d5a00 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 SQLITE_OK;. if(
d5a10 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 id ){. unixF
d5a20 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
d5a30 69 78 46 69 6c 65 20 2a 29 69 64 3b 0a 20 20 20 ixFile *)id;.
d5a40 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 unixUnlock(id,
d5a50 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 75 6e NO_LOCK);. un
d5a60 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a ixEnterMutex();.
d5a70 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 if( pFile->p
d5a80 4f 70 65 6e 20 26 26 20 70 46 69 6c 65 2d 3e 70 Open && pFile->p
d5a90 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20 Open->nLock ){.
d5aa0 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 /* If there
d5ab0 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 are outstanding
d5ac0 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61 locks, do not a
d5ad0 63 74 75 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68 ctually close th
d5ae0 65 20 66 69 6c 65 20 6a 75 73 74 0a 20 20 20 20 e file just.
d5af0 20 20 2a 2a 20 79 65 74 20 62 65 63 61 75 73 65 ** yet because
d5b00 20 74 68 61 74 20 77 6f 75 6c 64 20 63 6c 65 61 that would clea
d5b10 72 20 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 r those locks.
d5b20 49 6e 73 74 65 61 64 2c 20 61 64 64 20 74 68 65 Instead, add the
d5b30 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64 file. ** d
d5b40 65 73 63 72 69 70 74 6f 72 20 74 6f 20 70 4f 70 escriptor to pOp
d5b50 65 6e 2d 3e 70 55 6e 75 73 65 64 20 6c 69 73 74 en->pUnused list
d5b60 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 20 61 75 . It will be au
d5b70 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 tomatically clos
d5b80 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 77 68 65 ed . ** whe
d5b90 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20 n the last lock
d5ba0 69 73 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 is cleared..
d5bb0 20 20 2a 2f 0a 20 20 20 20 20 20 73 65 74 50 65 */. setPe
d5bc0 6e 64 69 6e 67 46 64 28 70 46 69 6c 65 29 3b 0a ndingFd(pFile);.
d5bd0 20 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 61 73 }. releas
d5be0 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d eLockInfo(pFile-
d5bf0 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 65 6c >pLock);. rel
d5c00 65 61 73 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c easeOpenCnt(pFil
d5c10 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 72 e->pOpen);. r
d5c20 63 20 3d 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c c = closeUnixFil
d5c30 65 28 69 64 29 3b 0a 20 20 20 20 75 6e 69 78 4c e(id);. unixL
d5c40 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d eaveMutex();. }
d5c50 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
d5c60 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
d5c70 20 45 6e 64 20 6f 66 20 74 68 65 20 70 6f 73 69 End of the posi
d5c80 78 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 20 x advisory lock
d5c90 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a implementation *
d5ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5cb0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
d5cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
d5d00 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
d5d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5d50 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
d5d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5d70 20 4e 6f 2d 6f 70 20 4c 6f 63 6b 69 6e 67 20 2a No-op Locking *
d5d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5da0 2a 0a 2a 2a 0a 2a 2a 20 4f 66 20 74 68 65 20 76 *.**.** Of the v
d5db0 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 69 arious locking i
d5dc0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 61 mplementations a
d5dd0 76 61 69 6c 61 62 6c 65 2c 20 74 68 69 73 20 69 vailable, this i
d5de0 73 20 62 79 20 66 61 72 20 74 68 65 0a 2a 2a 20 s by far the.**
d5df0 73 69 6d 70 6c 65 73 74 3a 20 20 6c 6f 63 6b 69 simplest: locki
d5e00 6e 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 20 20 ng is ignored.
d5e10 4e 6f 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 No attempt is ma
d5e20 64 65 20 74 6f 20 6c 6f 63 6b 20 74 68 65 20 64 de to lock the d
d5e30 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 atabase.** file
d5e40 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 77 for reading or w
d5e50 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 riting..**.** Th
d5e60 69 73 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 is locking mode
d5e70 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 is appropriate f
d5e80 6f 72 20 75 73 65 20 6f 6e 20 72 65 61 64 2d 6f or use on read-o
d5e90 6e 6c 79 20 64 61 74 61 62 61 73 65 73 0a 2a 2a nly databases.**
d5ea0 20 28 65 78 3a 20 64 61 74 61 62 61 73 65 73 20 (ex: databases
d5eb0 74 68 61 74 20 61 72 65 20 62 75 72 6e 65 64 20 that are burned
d5ec0 69 6e 74 6f 20 43 44 2d 52 4f 4d 2c 20 66 6f 72 into CD-ROM, for
d5ed0 20 65 78 61 6d 70 6c 65 2e 29 20 20 49 74 20 63 example.) It c
d5ee0 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 75 73 an.** also be us
d5ef0 65 64 20 69 66 20 74 68 65 20 61 70 70 6c 69 63 ed if the applic
d5f00 61 74 69 6f 6e 20 65 6d 70 6c 6f 79 73 20 73 6f ation employs so
d5f10 6d 65 20 65 78 74 65 72 6e 61 6c 20 6d 65 63 68 me external mech
d5f20 61 6e 69 73 6d 20 74 6f 0a 2a 2a 20 70 72 65 76 anism to.** prev
d5f30 65 6e 74 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 ent simultaneous
d5f40 20 61 63 63 65 73 73 20 6f 66 20 74 68 65 20 73 access of the s
d5f50 61 6d 65 20 64 61 74 61 62 61 73 65 20 62 79 20 ame database by
d5f60 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 64 two or more.** d
d5f70 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
d5f80 6f 6e 73 2e 20 20 42 75 74 20 74 68 65 72 65 20 ons. But there
d5f90 69 73 20 61 20 73 65 72 69 6f 75 73 20 72 69 73 is a serious ris
d5fa0 6b 20 6f 66 20 64 61 74 61 62 61 73 65 0a 2a 2a k of database.**
d5fb0 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 66 20 74 corruption if t
d5fc0 68 69 73 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 his locking mode
d5fd0 20 69 73 20 75 73 65 64 20 69 6e 20 73 69 74 75 is used in situ
d5fe0 61 74 69 6f 6e 73 20 77 68 65 72 65 20 6d 75 6c ations where mul
d5ff0 74 69 70 6c 65 0a 2a 2a 20 64 61 74 61 62 61 73 tiple.** databas
d6000 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 e connections ar
d6010 65 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 e accessing the
d6020 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 same database fi
d6030 6c 65 20 61 74 20 74 68 65 20 73 61 6d 65 0a 2a le at the same.*
d6040 2a 20 74 69 6d 65 20 61 6e 64 20 6f 6e 65 20 6f * time and one o
d6050 72 20 6d 6f 72 65 20 6f 66 20 74 68 6f 73 65 20 r more of those
d6060 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 20 connections are
d6070 77 72 69 74 69 6e 67 2e 0a 2a 2f 0a 0a 73 74 61 writing..*/..sta
d6080 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 43 68 tic int nolockCh
d6090 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
d60a0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f sqlite3_file *No
d60b0 74 55 73 65 64 2c 20 69 6e 74 20 2a 70 52 65 73 tUsed, int *pRes
d60c0 4f 75 74 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 Out){. UNUSED_P
d60d0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
d60e0 29 3b 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 );. *pResOut =
d60f0 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 0;. return SQLI
d6100 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 TE_OK;.}.static
d6110 69 6e 74 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 28 73 int nolockLock(s
d6120 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 qlite3_file *Not
d6130 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 Used, int NotUse
d6140 64 32 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 d2){. UNUSED_PA
d6150 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 RAMETER2(NotUsed
d6160 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 72 , NotUsed2);. r
d6170 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
d6180 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f .}.static int no
d6190 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 lockUnlock(sqlit
d61a0 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 e3_file *NotUsed
d61b0 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b , int NotUsed2){
d61c0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
d61d0 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f TER2(NotUsed, No
d61e0 74 55 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72 tUsed2);. retur
d61f0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
d6200 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 /*.** Close the
d6210 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
d6220 69 6e 74 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 28 int nolockClose(
d6230 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
d6240 29 20 7b 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f ) {. return clo
d6250 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a seUnixFile(id);.
d6260 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
d6270 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 ******* End of t
d6280 68 65 20 6e 6f 2d 6f 70 20 6c 6f 63 6b 20 69 6d he no-op lock im
d6290 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a plementation ***
d62a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d62b0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
d62c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d62d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d62e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d62f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6300 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
d6310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6350 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***.************
d6360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
d6370 67 69 6e 20 64 6f 74 2d 66 69 6c 65 20 4c 6f 63 gin dot-file Loc
d6380 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a king ***********
d6390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d63a0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 6f ***.**.** The do
d63b0 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 6d tfile locking im
d63c0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 plementation use
d63d0 73 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 s the existance
d63e0 6f 66 20 73 65 70 61 72 61 74 65 20 6c 6f 63 6b of separate lock
d63f0 0a 2a 2a 20 66 69 6c 65 73 20 69 6e 20 6f 72 64 .** files in ord
d6400 65 72 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 er to control ac
d6410 63 65 73 73 20 74 6f 20 74 68 65 20 64 61 74 61 cess to the data
d6420 62 61 73 65 2e 20 20 54 68 69 73 20 77 6f 72 6b base. This work
d6430 73 20 6f 6e 20 6a 75 73 74 0a 2a 2a 20 61 62 6f s on just.** abo
d6440 75 74 20 65 76 65 72 79 20 66 69 6c 65 73 79 73 ut every filesys
d6450 74 65 6d 20 69 6d 61 67 69 6e 61 62 6c 65 2e 20 tem imaginable.
d6460 20 42 75 74 20 74 68 65 72 65 20 61 72 65 20 73 But there are s
d6470 65 72 69 6f 75 73 20 64 6f 77 6e 73 69 64 65 73 erious downsides
d6480 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20 :.**.** (1)
d6490 54 68 65 72 65 20 69 73 20 7a 65 72 6f 20 63 6f There is zero co
d64a0 6e 63 75 72 72 65 6e 63 79 2e 20 20 41 20 73 69 ncurrency. A si
d64b0 6e 67 6c 65 20 72 65 61 64 65 72 20 62 6c 6f 63 ngle reader bloc
d64c0 6b 73 20 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 ks all other.**
d64d0 20 20 20 20 20 20 20 20 63 6f 6e 6e 65 63 74 69 connecti
d64e0 6f 6e 73 20 66 72 6f 6d 20 72 65 61 64 69 6e 67 ons from reading
d64f0 20 6f 72 20 77 72 69 74 69 6e 67 20 74 68 65 20 or writing the
d6500 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 database..**.**
d6510 20 20 20 28 32 29 20 20 41 6e 20 61 70 70 6c 69 (2) An appli
d6520 63 61 74 69 6f 6e 20 63 72 61 73 68 20 6f 72 20 cation crash or
d6530 70 6f 77 65 72 20 6c 6f 73 73 20 63 61 6e 20 6c power loss can l
d6540 65 61 76 65 20 73 74 61 6c 65 20 6c 6f 63 6b 20 eave stale lock
d6550 66 69 6c 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 files.**
d6560 20 73 69 74 74 69 6e 67 20 61 72 6f 75 6e 64 20 sitting around
d6570 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 that need to be
d6580 63 6c 65 61 72 65 64 20 6d 61 6e 75 61 6c 6c 79 cleared manually
d6590 2e 0a 2a 2a 0a 2a 2a 20 4e 65 76 65 72 74 68 65 ..**.** Neverthe
d65a0 6c 65 73 73 2c 20 61 20 64 6f 74 6c 6f 63 6b 20 less, a dotlock
d65b0 69 73 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 is an appropriat
d65c0 65 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 66 e locking mode f
d65d0 6f 72 20 75 73 65 20 69 66 20 6e 6f 0a 2a 2a 20 or use if no.**
d65e0 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67 20 73 74 other locking st
d65f0 72 61 74 65 67 79 20 69 73 20 61 76 61 69 6c 61 rategy is availa
d6600 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 44 6f 74 66 69 ble..**.** Dotfi
d6610 6c 65 20 6c 6f 63 6b 69 6e 67 20 77 6f 72 6b 73 le locking works
d6620 20 62 79 20 63 72 65 61 74 69 6e 67 20 61 20 66 by creating a f
d6630 69 6c 65 20 69 6e 20 74 68 65 20 73 61 6d 65 20 ile in the same
d6640 64 69 72 65 63 74 6f 72 79 20 61 73 20 74 68 65 directory as the
d6650 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 6e 64 .** database and
d6660 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e with the same n
d6670 61 6d 65 20 62 75 74 20 77 69 74 68 20 61 20 22 ame but with a "
d6680 2e 6c 6f 63 6b 22 20 65 78 74 65 6e 73 69 6f 6e .lock" extension
d6690 20 61 64 64 65 64 2e 0a 2a 2a 20 54 68 65 20 65 added..** The e
d66a0 78 69 73 74 61 6e 63 65 20 6f 66 20 61 20 6c 6f xistance of a lo
d66b0 63 6b 20 66 69 6c 65 20 69 6d 70 6c 69 65 73 20 ck file implies
d66c0 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
d66d0 6b 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20 6c 6f k. All other lo
d66e0 63 6b 0a 2a 2a 20 74 79 70 65 73 20 28 53 48 41 ck.** types (SHA
d66f0 52 45 44 2c 20 52 45 53 45 52 56 45 44 2c 20 50 RED, RESERVED, P
d6700 45 4e 44 49 4e 47 29 20 61 72 65 20 6d 61 70 70 ENDING) are mapp
d6710 65 64 20 69 6e 74 6f 20 45 58 43 4c 55 53 49 56 ed into EXCLUSIV
d6720 45 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 E..*/../*.** The
d6730 20 66 69 6c 65 20 73 75 66 66 69 78 20 61 64 64 file suffix add
d6740 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 20 62 ed to the data b
d6750 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 ase filename in
d6760 6f 72 64 65 72 20 74 6f 20 63 72 65 61 74 65 20 order to create
d6770 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20 66 69 6c 65 the.** lock file
d6780 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 4f 54 ..*/.#define DOT
d6790 4c 4f 43 4b 5f 53 55 46 46 49 58 20 22 2e 6c 6f LOCK_SUFFIX ".lo
d67a0 63 6b 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ck"../*.** This
d67b0 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 routine checks i
d67c0 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 f there is a RES
d67d0 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 ERVED lock held
d67e0 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 on the specified
d67f0 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 .** file by this
d6800 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 or any other pr
d6810 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 ocess. If such a
d6820 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 lock is held, s
d6830 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 et *pResOut.** t
d6840 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c o a non-zero val
d6850 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 ue otherwise *pR
d6860 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 esOut is set to
d6870 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 zero. The retur
d6880 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 n value.** is se
d6890 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 t to SQLITE_OK u
d68a0 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 nless an I/O err
d68b0 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
d68c0 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a lock checking..
d68d0 2a 2a 0a 2a 2a 20 49 6e 20 64 6f 74 66 69 6c 65 **.** In dotfile
d68e0 20 6c 6f 63 6b 69 6e 67 2c 20 65 69 74 68 65 72 locking, either
d68f0 20 61 20 6c 6f 63 6b 20 65 78 69 73 74 73 20 6f a lock exists o
d6900 72 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 20 20 r it does not.
d6910 53 6f 20 69 6e 20 74 68 69 73 0a 2a 2a 20 76 61 So in this.** va
d6920 72 69 61 74 69 6f 6e 20 6f 66 20 43 68 65 63 6b riation of Check
d6930 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 29 2c 20 ReservedLock(),
d6940 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 *pResOut is set
d6950 74 6f 20 74 72 75 65 20 69 66 20 61 6e 79 20 6c to true if any l
d6960 6f 63 6b 0a 2a 2a 20 69 73 20 68 65 6c 64 20 6f ock.** is held o
d6970 6e 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20 66 n the file and f
d6980 61 6c 73 65 20 69 66 20 74 68 65 20 66 69 6c 65 alse if the file
d6990 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2e 0a 2a 2f is unlocked..*/
d69a0 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c .static int dotl
d69b0 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 ockCheckReserved
d69c0 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
d69d0 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 e *id, int *pRes
d69e0 4f 75 74 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 Out) {. int rc
d69f0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
d6a00 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b nt reserved = 0;
d6a10 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
d6a20 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
d6a30 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 id;.. SimulateI
d6a40 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 OError( return S
d6a50 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 QLITE_IOERR_CHEC
d6a60 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 KRESERVEDLOCK; )
d6a70 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 ;. . assert( p
d6a80 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 File );.. /* Ch
d6a90 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 eck if a thread
d6aa0 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 in this process
d6ab0 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 holds such a loc
d6ac0 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 k */. if( pFile
d6ad0 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 ->locktype>SHARE
d6ae0 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a D_LOCK ){. /*
d6af0 20 45 69 74 68 65 72 20 74 68 69 73 20 63 6f 6e Either this con
d6b00 6e 65 63 74 69 6f 6e 20 6f 72 20 73 6f 6d 65 20 nection or some
d6b10 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e other connection
d6b20 20 69 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f in the same pro
d6b30 63 65 73 73 0a 20 20 20 20 2a 2a 20 68 6f 6c 64 cess. ** hold
d6b40 73 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 s a lock on the
d6b50 66 69 6c 65 2e 20 20 4e 6f 20 6e 65 65 64 20 74 file. No need t
d6b60 6f 20 63 68 65 63 6b 20 66 75 72 74 68 65 72 2e o check further.
d6b70 20 2a 2f 0a 20 20 20 20 72 65 73 65 72 76 65 64 */. reserved
d6b80 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 1;. }else{.
d6b90 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 20 69 /* The lock i
d6ba0 73 20 68 65 6c 64 20 69 66 20 61 6e 64 20 6f 6e s held if and on
d6bb0 6c 79 20 69 66 20 74 68 65 20 6c 6f 63 6b 66 69 ly if the lockfi
d6bc0 6c 65 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 le exists */.
d6bd0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 6f const char *zLo
d6be0 63 6b 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 ckFile = (const
d6bf0 63 68 61 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 char*)pFile->loc
d6c00 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 kingContext;.
d6c10 20 72 65 73 65 72 76 65 64 20 3d 20 61 63 63 65 reserved = acce
d6c20 73 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 30 29 ss(zLockFile, 0)
d6c30 3d 3d 30 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 ==0;. }. OSTRA
d6c40 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 CE4("TEST WR-LOC
d6c50 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 K %d %d %d\n", p
d6c60 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 File->h, rc, res
d6c70 65 72 76 65 64 29 3b 0a 20 20 2a 70 52 65 73 4f erved);. *pResO
d6c80 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 ut = reserved;.
d6c90 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
d6ca0 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 *.** Lock the fi
d6cb0 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b le with the lock
d6cc0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 specified by pa
d6cd0 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 rameter locktype
d6ce0 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 - one.** of the
d6cf0 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a following:.**.*
d6d00 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 * (1) SHARED
d6d10 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 _LOCK.** (2)
d6d20 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a RESERVED_LOCK.*
d6d30 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e * (3) PENDIN
d6d40 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 G_LOCK.** (4
d6d50 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ) EXCLUSIVE_LOCK
d6d60 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 .**.** Sometimes
d6d70 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 when requesting
d6d80 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c one lock state,
d6d90 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b additional lock
d6da0 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 states.** are i
d6db0 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 nserted in betwe
d6dc0 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 en. The locking
d6dd0 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f might fail on o
d6de0 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a ne of the later.
d6df0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c ** transitions l
d6e00 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 eaving the lock
d6e10 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 state different
d6e20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 from what it sta
d6e30 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c rted but.** stil
d6e40 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 l short of its g
d6e50 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 oal. The follow
d6e60 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 ing chart shows
d6e70 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 the allowed.** t
d6e80 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 ransitions and t
d6e90 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 he inserted inte
d6ea0 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a rmediate states:
d6eb0 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b .**.** UNLOCK
d6ec0 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 ED -> SHARED.**
d6ed0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 SHARED -> RES
d6ee0 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 ERVED.** SHAR
d6ef0 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 ED -> (PENDING)
d6f00 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 -> EXCLUSIVE.**
d6f10 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 RESERVED -> (
d6f20 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c PENDING) -> EXCL
d6f30 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 USIVE.** PEND
d6f40 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 ING -> EXCLUSIVE
d6f50 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
d6f60 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e ine will only in
d6f70 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 crease a lock.
d6f80 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f Use the sqlite3O
d6f90 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 sUnlock().** rou
d6fa0 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 tine to lower a
d6fb0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a locking level..*
d6fc0 2a 0a 2a 2a 20 57 69 74 68 20 64 6f 74 66 69 6c *.** With dotfil
d6fd0 65 20 6c 6f 63 6b 69 6e 67 2c 20 77 65 20 72 65 e locking, we re
d6fe0 61 6c 6c 79 20 6f 6e 6c 79 20 73 75 70 70 6f 72 ally only suppor
d6ff0 74 20 73 74 61 74 65 20 28 34 29 3a 20 45 58 43 t state (4): EXC
d7000 4c 55 53 49 56 45 2e 0a 2a 2a 20 42 75 74 20 77 LUSIVE..** But w
d7010 65 20 74 72 61 63 6b 20 74 68 65 20 6f 74 68 65 e track the othe
d7020 72 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 73 r locking levels
d7030 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a 2f 0a internally..*/.
d7040 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f static int dotlo
d7050 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 ckLock(sqlite3_f
d7060 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 ile *id, int loc
d7070 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 ktype) {. unixF
d7080 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
d7090 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e ixFile*)id;. in
d70a0 74 20 66 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c t fd;. char *zL
d70b0 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 61 72 20 ockFile = (char
d70c0 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 *)pFile->locking
d70d0 43 6f 6e 74 65 78 74 3b 0a 20 20 69 6e 74 20 72 Context;. int r
d70e0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a c = SQLITE_OK;..
d70f0 0a 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 . /* If we have
d7100 20 61 6e 79 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 any lock, then
d7110 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 6c the lock file al
d7120 72 65 61 64 79 20 65 78 69 73 74 73 2e 20 20 41 ready exists. A
d7130 6c 6c 20 77 65 20 68 61 76 65 0a 20 20 2a 2a 20 ll we have. **
d7140 74 6f 20 64 6f 20 69 73 20 61 64 6a 75 73 74 20 to do is adjust
d7150 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20 72 65 63 our internal rec
d7160 6f 72 64 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 ord of the lock
d7170 6c 65 76 65 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 level.. */. if
d7180 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
d7190 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 e > NO_LOCK ){.
d71a0 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
d71b0 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 23 pe = locktype;.#
d71c0 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 if !OS_VXWORKS.
d71d0 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 75 70 64 /* Always upd
d71e0 61 74 65 20 74 68 65 20 74 69 6d 65 73 74 61 6d ate the timestam
d71f0 70 20 6f 6e 20 74 68 65 20 6f 6c 64 20 66 69 6c p on the old fil
d7200 65 20 2a 2f 0a 20 20 20 20 75 74 69 6d 65 73 28 e */. utimes(
d7210 7a 4c 6f 63 6b 46 69 6c 65 2c 20 4e 55 4c 4c 29 zLockFile, NULL)
d7220 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 74 ;.#endif. ret
d7230 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
d7240 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62 20 }. . /* grab
d7250 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
d7260 6b 20 2a 2f 0a 20 20 66 64 20 3d 20 6f 70 65 6e k */. fd = open
d7270 28 7a 4c 6f 63 6b 46 69 6c 65 2c 4f 5f 52 44 4f (zLockFile,O_RDO
d7280 4e 4c 59 7c 4f 5f 43 52 45 41 54 7c 4f 5f 45 58 NLY|O_CREAT|O_EX
d7290 43 4c 2c 30 36 30 30 29 3b 0a 20 20 69 66 28 20 CL,0600);. if(
d72a0 66 64 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 66 fd<0 ){. /* f
d72b0 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e 2f 63 72 ailed to open/cr
d72c0 65 61 74 65 20 74 68 65 20 66 69 6c 65 2c 20 73 eate the file, s
d72d0 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 61 79 20 omeone else may
d72e0 68 61 76 65 20 73 74 6f 6c 65 6e 20 74 68 65 20 have stolen the
d72f0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 lock */. int
d7300 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
d7310 20 20 20 20 69 66 28 20 45 45 58 49 53 54 20 3d if( EEXIST =
d7320 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20 20 = tErrno ){.
d7330 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 rc = SQLITE_BU
d7340 53 59 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b SY;. } else {
d7350 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
d7360 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 teErrorFromPosix
d7370 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 Error(tErrno, SQ
d7380 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 LITE_IOERR_LOCK)
d7390 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c ;. if( IS_L
d73a0 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b OCK_ERROR(rc) ){
d73b0 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e . pFile->
d73c0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 lastErrno = tErr
d73d0 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 no;. }.
d73e0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
d73f0 0a 20 20 7d 20 0a 20 20 69 66 28 20 63 6c 6f 73 . } . if( clos
d7400 65 28 66 64 29 20 29 7b 0a 20 20 20 20 70 46 69 e(fd) ){. pFi
d7410 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d7420 65 72 72 6e 6f 3b 0a 20 20 20 20 72 63 20 3d 20 errno;. rc =
d7430 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f SQLITE_IOERR_CLO
d7440 53 45 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 SE;. }. . /*
d7450 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65 20 got it, set the
d7460 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20 type and return
d7470 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 6c ok */. pFile->l
d7480 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 ocktype = lockty
d7490 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b pe;. return rc;
d74a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 .}../*.** Lower
d74b0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
d74c0 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 l on file descri
d74d0 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f ptor pFile to lo
d74e0 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 cktype. locktyp
d74f0 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 e.** must be eit
d7500 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 her NO_LOCK or S
d7510 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a HARED_LOCK..**.*
d7520 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 * If the locking
d7530 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 level of the fi
d7540 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 le descriptor is
d7550 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 already at or b
d7560 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 elow.** the requ
d7570 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 ested locking le
d7580 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e vel, this routin
d7590 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a e is a no-op..**
d75a0 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 6c 6f 63 .** When the loc
d75b0 6b 69 6e 67 20 6c 65 76 65 6c 20 72 65 61 63 68 king level reach
d75c0 65 73 20 4e 4f 5f 4c 4f 43 4b 2c 20 64 65 6c 65 es NO_LOCK, dele
d75d0 74 65 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 te the lock file
d75e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
d75f0 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 dotlockUnlock(sq
d7600 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
d7610 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a int locktype) {.
d7620 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
d7630 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
d7640 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b d;. char *zLock
d7650 46 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 70 File = (char *)p
d7660 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
d7670 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 text;.. assert(
d7680 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 pFile );. OSTR
d7690 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 ACE5("UNLOCK %d
d76a0 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 %d was %d pid=%
d76b0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
d76c0 6c 6f 63 6b 74 79 70 65 2c 0a 09 20 20 20 70 46 locktype,.. pF
d76d0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 ile->locktype, g
d76e0 65 74 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65 etpid());. asse
d76f0 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 rt( locktype<=SH
d7700 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a ARED_LOCK );. .
d7710 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f /* no-op if po
d7720 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 ssible */. if(
d7730 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
d7740 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 =locktype ){.
d7750 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
d7760 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 20 K;. }.. /* To
d7770 64 6f 77 6e 67 72 61 64 65 20 74 6f 20 73 68 61 downgrade to sha
d7780 72 65 64 2c 20 73 69 6d 70 6c 79 20 75 70 64 61 red, simply upda
d7790 74 65 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20 te our internal
d77a0 6e 6f 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 notion of the.
d77b0 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65 2e 20 20 ** lock state.
d77c0 4e 6f 20 6e 65 65 64 20 74 6f 20 6d 65 73 73 20 No need to mess
d77d0 77 69 74 68 20 74 68 65 20 66 69 6c 65 20 6f 6e with the file on
d77e0 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 disk.. */. if
d77f0 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 ( locktype==SHAR
d7800 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 ED_LOCK ){. p
d7810 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
d7820 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 SHARED_LOCK;.
d7830 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d7840 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 OK;. }. . /*
d7850 54 6f 20 66 75 6c 6c 79 20 75 6e 6c 6f 63 6b 20 To fully unlock
d7860 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 64 65 the database, de
d7870 6c 65 74 65 20 74 68 65 20 6c 6f 63 6b 20 66 69 lete the lock fi
d7880 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 le */. assert(
d7890 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 locktype==NO_LOC
d78a0 4b 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6e K );. if( unlin
d78b0 6b 28 7a 4c 6f 63 6b 46 69 6c 65 29 20 29 7b 0a k(zLockFile) ){.
d78c0 20 20 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a int rc = 0;.
d78d0 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d int tErrno =
d78e0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 errno;. if(
d78f0 45 4e 4f 45 4e 54 20 21 3d 20 74 45 72 72 6e 6f ENOENT != tErrno
d7900 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
d7910 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
d7920 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
d7930 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e SQLITE_IOERR_UN
d7940 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 LOCK);. }.
d7950 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 if( IS_LOCK_ERR
d7960 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 OR(rc) ){.
d7970 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
d7980 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d = tErrno;. }
d7990 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 20 . return rc;
d79a0 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f . }. pFile->lo
d79b0 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b cktype = NO_LOCK
d79c0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
d79d0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
d79e0 6c 6f 73 65 20 61 20 66 69 6c 65 2e 20 20 4d 61 lose a file. Ma
d79f0 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b ke sure the lock
d7a00 20 68 61 73 20 62 65 65 6e 20 72 65 6c 65 61 73 has been releas
d7a10 65 64 20 62 65 66 6f 72 65 20 63 6c 6f 73 69 6e ed before closin
d7a20 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 g..*/.static int
d7a30 20 64 6f 74 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 dotlockClose(sq
d7a40 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 lite3_file *id)
d7a50 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 {. int rc;. if
d7a60 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 ( id ){. unix
d7a70 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
d7a80 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 nixFile*)id;.
d7a90 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 dotlockUnlock(i
d7aa0 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 d, NO_LOCK);.
d7ab0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 sqlite3_free(pF
d7ac0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
d7ad0 65 78 74 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d ext);. }. rc =
d7ae0 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 closeUnixFile(i
d7af0 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b d);. return rc;
d7b00 0a 7d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}./************
d7b10 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 ****** End of th
d7b20 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 20 e dot-file lock
d7b30 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a implementation *
d7b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7b50 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
d7b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7ba0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
d7bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7bf0 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***.************
d7c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
d7c10 65 67 69 6e 20 66 6c 6f 63 6b 20 4c 6f 63 6b 69 egin flock Locki
d7c20 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ng *************
d7c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7c40 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 ***.**.** Use th
d7c50 65 20 66 6c 6f 63 6b 28 29 20 73 79 73 74 65 6d e flock() system
d7c60 20 63 61 6c 6c 20 74 6f 20 64 6f 20 66 69 6c 65 call to do file
d7c70 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 locking..**.**
d7c80 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 flock() locking
d7c90 69 73 20 6c 69 6b 65 20 64 6f 74 2d 66 69 6c 65 is like dot-file
d7ca0 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 68 61 74 locking in that
d7cb0 20 74 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 the various.**
d7cc0 66 69 6e 65 2d 67 72 61 69 6e 20 6c 6f 63 6b 69 fine-grain locki
d7cd0 6e 67 20 6c 65 76 65 6c 73 20 73 75 70 70 6f 72 ng levels suppor
d7ce0 74 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 72 ted by SQLite ar
d7cf0 65 20 63 6f 6c 6c 61 70 73 65 64 20 69 6e 74 6f e collapsed into
d7d00 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 65 78 63 .** a single exc
d7d10 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 49 6e lusive lock. In
d7d20 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 53 48 other words, SH
d7d30 41 52 45 44 2c 20 52 45 53 45 52 56 45 44 2c 20 ARED, RESERVED,
d7d40 61 6e 64 0a 2a 2a 20 50 45 4e 44 49 4e 47 20 6c and.** PENDING l
d7d50 6f 63 6b 73 20 61 72 65 20 74 68 65 20 73 61 6d ocks are the sam
d7d60 65 20 74 68 69 6e 67 20 61 73 20 61 6e 20 45 58 e thing as an EX
d7d70 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 53 CLUSIVE lock. S
d7d80 51 4c 69 74 65 0a 2a 2a 20 73 74 69 6c 6c 20 77 QLite.** still w
d7d90 6f 72 6b 73 20 77 68 65 6e 20 79 6f 75 20 64 6f orks when you do
d7da0 20 74 68 69 73 2c 20 62 75 74 20 63 6f 6e 63 75 this, but concu
d7db0 72 72 65 6e 63 79 20 69 73 20 72 65 64 75 63 65 rrency is reduce
d7dc0 64 20 73 69 6e 63 65 0a 2a 2a 20 6f 6e 6c 79 20 d since.** only
d7dd0 61 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 a single process
d7de0 20 63 61 6e 20 62 65 20 72 65 61 64 69 6e 67 20 can be reading
d7df0 74 68 65 20 64 61 74 61 62 61 73 65 20 61 74 20 the database at
d7e00 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6d a time..**.** Om
d7e10 69 74 20 74 68 69 73 20 73 65 63 74 69 6f 6e 20 it this section
d7e20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 if SQLITE_ENABLE
d7e30 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 69 _LOCKING_STYLE i
d7e40 73 20 74 75 72 6e 65 64 20 6f 66 66 20 6f 72 20 s turned off or
d7e50 69 66 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 if.** compiling
d7e60 66 6f 72 20 56 58 57 4f 52 4b 53 2e 0a 2a 2f 0a for VXWORKS..*/.
d7e70 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c #if SQLITE_ENABL
d7e80 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
d7e90 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 0a && !OS_VXWORKS..
d7ea0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
d7eb0 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 ne checks if the
d7ec0 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 re is a RESERVED
d7ed0 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 lock held on th
d7ee0 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 e specified.** f
d7ef0 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 ile by this or a
d7f00 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 ny other process
d7f10 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b . If such a lock
d7f20 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 is held, set *p
d7f30 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e ResOut.** to a n
d7f40 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 on-zero value ot
d7f50 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 herwise *pResOut
d7f60 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e is set to zero.
d7f70 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c The return val
d7f80 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 ue.** is set to
d7f90 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 SQLITE_OK unless
d7fa0 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 an I/O error oc
d7fb0 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b curs during lock
d7fc0 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 checking..*/.st
d7fd0 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 68 atic int flockCh
d7fe0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
d7ff0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
d8000 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b , int *pResOut){
d8010 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
d8020 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 TE_OK;. int res
d8030 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 erved = 0;. uni
d8040 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
d8050 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 unixFile*)id;.
d8060 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
d8070 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 or( return SQLIT
d8080 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 E_IOERR_CHECKRES
d8090 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 ERVEDLOCK; );.
d80a0 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
d80b0 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 );. . /* Chec
d80c0 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e k if a thread in
d80d0 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f this process ho
d80e0 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 lds such a lock
d80f0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e */. if( pFile->
d8100 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f locktype>SHARED_
d8110 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 LOCK ){. rese
d8120 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 rved = 1;. }.
d8130 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 . /* Otherwise
d8140 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 see if some othe
d8150 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 r process holds
d8160 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 it. */. if( !re
d8170 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a served ){. /*
d8180 20 61 74 74 65 6d 70 74 20 74 6f 20 67 65 74 20 attempt to get
d8190 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 the lock */.
d81a0 69 6e 74 20 6c 72 63 20 3d 20 66 6c 6f 63 6b 28 int lrc = flock(
d81b0 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 pFile->h, LOCK_E
d81c0 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 3b 0a 20 20 X | LOCK_NB);.
d81d0 20 20 69 66 28 20 21 6c 72 63 20 29 7b 0a 20 20 if( !lrc ){.
d81e0 20 20 20 20 2f 2a 20 67 6f 74 20 74 68 65 20 6c /* got the l
d81f0 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 2a ock, unlock it *
d8200 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 66 6c /. lrc = fl
d8210 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f ock(pFile->h, LO
d8220 43 4b 5f 55 4e 29 3b 0a 20 20 20 20 20 20 69 66 CK_UN);. if
d8230 20 28 20 6c 72 63 20 29 20 7b 0a 20 20 20 20 20 ( lrc ) {.
d8240 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 int tErrno =
d8250 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 2f errno;. /
d8260 2a 20 75 6e 6c 6f 63 6b 20 66 61 69 6c 65 64 20 * unlock failed
d8270 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f with an error */
d8280 0a 20 20 20 20 20 20 20 20 6c 72 63 20 3d 20 73 . lrc = s
d8290 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
d82a0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
d82b0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e SQLITE_IOERR_UN
d82c0 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 20 LOCK); .
d82d0 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f if( IS_LOCK_ERRO
d82e0 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20 20 20 R(lrc) ){.
d82f0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
d8300 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 rrno = tErrno;.
d8310 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72 rc = lr
d8320 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 c;. }.
d8330 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 }. } else
d8340 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 {. int tErr
d8350 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
d8360 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a reserved = 1;.
d8370 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e 65 /* someone
d8380 20 65 6c 73 65 20 6d 69 67 68 74 20 68 61 76 65 else might have
d8390 20 69 74 20 72 65 73 65 72 76 65 64 20 2a 2f 0a it reserved */.
d83a0 20 20 20 20 20 20 6c 72 63 20 3d 20 73 71 6c 69 lrc = sqli
d83b0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 teErrorFromPosix
d83c0 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 Error(tErrno, SQ
d83d0 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 LITE_IOERR_LOCK)
d83e0 3b 20 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f ; . if( IS_
d83f0 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 LOCK_ERROR(lrc)
d8400 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 ){. pFile
d8410 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 ->lastErrno = tE
d8420 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 63 rrno;. rc
d8430 20 3d 20 6c 72 63 3b 0a 20 20 20 20 20 20 7d 0a = lrc;. }.
d8440 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 }. }. OSTR
d8450 41 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f ACE4("TEST WR-LO
d8460 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 CK %d %d %d\n",
d8470 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 pFile->h, rc, re
d8480 73 65 72 76 65 64 29 3b 0a 0a 23 69 66 64 65 66 served);..#ifdef
d8490 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 SQLITE_IGNORE_F
d84a0 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 LOCK_LOCK_ERRORS
d84b0 0a 20 20 69 66 28 20 28 72 63 20 26 20 53 51 4c . if( (rc & SQL
d84c0 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 ITE_IOERR) == SQ
d84d0 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 LITE_IOERR ){.
d84e0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
d84f0 3b 0a 20 20 20 20 72 65 73 65 72 76 65 64 3d 31 ;. reserved=1
d8500 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ;. }.#endif /*
d8510 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c SQLITE_IGNORE_FL
d8520 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 OCK_LOCK_ERRORS
d8530 2a 2f 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 */. *pResOut =
d8540 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 reserved;. retu
d8550 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
d8560 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 Lock the file wi
d8570 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 th the lock spec
d8580 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 ified by paramet
d8590 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e er locktype - on
d85a0 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c e.** of the foll
d85b0 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 owing:.**.**
d85c0 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b (1) SHARED_LOCK
d85d0 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 .** (2) RESE
d85e0 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 RVED_LOCK.**
d85f0 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 (3) PENDING_LOC
d8600 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 K.** (4) EXC
d8610 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a LUSIVE_LOCK.**.*
d8620 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e * Sometimes when
d8630 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 requesting one
d8640 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 lock state, addi
d8650 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 tional lock stat
d8660 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 es.** are insert
d8670 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 ed in between.
d8680 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 The locking migh
d8690 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 t fail on one of
d86a0 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 the later.** tr
d86b0 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e ansitions leavin
d86c0 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 g the lock state
d86d0 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 different from
d86e0 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20 what it started
d86f0 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f but.** still sho
d8700 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 rt of its goal.
d8710 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 The following c
d8720 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 hart shows the a
d8730 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 llowed.** transi
d8740 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e tions and the in
d8750 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 serted intermedi
d8760 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a ate states:.**.*
d8770 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e * UNLOCKED ->
d8780 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 SHARED.** SH
d8790 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 ARED -> RESERVED
d87a0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
d87b0 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 (PENDING) -> EX
d87c0 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 CLUSIVE.** RE
d87d0 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 SERVED -> (PENDI
d87e0 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
d87f0 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d .** PENDING -
d8800 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a > EXCLUSIVE.**.*
d8810 2a 20 66 6c 6f 63 6b 28 29 20 6f 6e 6c 79 20 72 * flock() only r
d8820 65 61 6c 6c 79 20 73 75 70 70 6f 72 74 20 45 58 eally support EX
d8830 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 2e 20 20 CLUSIVE locks.
d8840 57 65 20 74 72 61 63 6b 20 69 6e 74 65 72 6d 65 We track interme
d8850 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 73 74 diate.** lock st
d8860 61 74 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 ates in the sqli
d8870 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75 te3_file structu
d8880 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c 6f 63 6b re, but all lock
d8890 73 20 53 48 41 52 45 44 20 6f 72 0a 2a 2a 20 61 s SHARED or.** a
d88a0 62 6f 76 65 20 61 72 65 20 72 65 61 6c 6c 79 20 bove are really
d88b0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 20 EXCLUSIVE locks
d88c0 61 6e 64 20 65 78 63 6c 75 64 65 20 61 6c 6c 20 and exclude all
d88d0 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 other processes
d88e0 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 20 74 from.** access t
d88f0 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 he file..**.** T
d8900 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
d8910 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 only increase a
d8920 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 lock. Use the
d8930 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 sqlite3OsUnlock(
d8940 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 ).** routine to
d8950 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 lower a locking
d8960 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 level..*/.static
d8970 20 69 6e 74 20 66 6c 6f 63 6b 4c 6f 63 6b 28 73 int flockLock(s
d8980 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
d8990 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b int locktype) {
d89a0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
d89b0 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c TE_OK;. unixFil
d89c0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
d89d0 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 File*)id;.. ass
d89e0 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 ert( pFile );..
d89f0 20 2f 2a 20 69 66 20 77 65 20 61 6c 72 65 61 64 /* if we alread
d8a00 79 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 y have a lock, i
d8a10 74 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 t is exclusive.
d8a20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 . ** Just adju
d8a30 73 74 20 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e st level and pun
d8a40 74 20 6f 6e 20 6f 75 74 74 61 20 68 65 72 65 2e t on outta here.
d8a50 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d */. if (pFile-
d8a60 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c >locktype > NO_L
d8a70 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 OCK) {. pFile
d8a80 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 ->locktype = loc
d8a90 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 74 75 72 ktype;. retur
d8aa0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
d8ab0 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62 20 61 6e . . /* grab an
d8ac0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 exclusive lock
d8ad0 2a 2f 0a 20 20 0a 20 20 69 66 20 28 66 6c 6f 63 */. . if (floc
d8ae0 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b k(pFile->h, LOCK
d8af0 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 29 20 _EX | LOCK_NB))
d8b00 7b 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f {. int tErrno
d8b10 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 2f 2a = errno;. /*
d8b20 20 64 69 64 6e 27 74 20 67 65 74 2c 20 6d 75 73 didn't get, mus
d8b30 74 20 62 65 20 62 75 73 79 20 2a 2f 0a 20 20 20 t be busy */.
d8b40 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f rc = sqliteErro
d8b50 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 rFromPosixError(
d8b60 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 tErrno, SQLITE_I
d8b70 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 OERR_LOCK);.
d8b80 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f if( IS_LOCK_ERRO
d8b90 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 R(rc) ){. p
d8ba0 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
d8bb0 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a = tErrno;. }.
d8bc0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f } else {. /
d8bd0 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 * got it, set th
d8be0 65 20 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 e type and retur
d8bf0 6e 20 6f 6b 20 2a 2f 0a 20 20 20 20 70 46 69 6c n ok */. pFil
d8c00 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f e->locktype = lo
d8c10 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 4f 53 cktype;. }. OS
d8c20 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 TRACE4("LOCK
d8c30 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 %d %s %s\n", pFi
d8c40 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e le->h, locktypeN
d8c50 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a ame(locktype), .
d8c60 20 20 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 rc==S
d8c70 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 QLITE_OK ? "ok"
d8c80 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 23 69 66 : "failed");.#if
d8c90 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 def SQLITE_IGNOR
d8ca0 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 E_FLOCK_LOCK_ERR
d8cb0 4f 52 53 0a 20 20 69 66 28 20 28 72 63 20 26 20 ORS. if( (rc &
d8cc0 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d SQLITE_IOERR) ==
d8cd0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b SQLITE_IOERR ){
d8ce0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
d8cf0 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64 69 _BUSY;. }.#endi
d8d00 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f f /* SQLITE_IGNO
d8d10 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 RE_FLOCK_LOCK_ER
d8d20 52 4f 52 53 20 2a 2f 0a 20 20 72 65 74 75 72 6e RORS */. return
d8d30 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c rc;.}.../*.** L
d8d40 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 ower the locking
d8d50 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 level on file d
d8d60 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 escriptor pFile
d8d70 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f to locktype. lo
d8d80 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 cktype.** must b
d8d90 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b e either NO_LOCK
d8da0 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e or SHARED_LOCK.
d8db0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f .**.** If the lo
d8dc0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 cking level of t
d8dd0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
d8de0 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 or is already at
d8df0 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 or below.** the
d8e00 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 requested locki
d8e10 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 ng level, this r
d8e20 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
d8e30 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 p..*/.static int
d8e40 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c flockUnlock(sql
d8e50 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
d8e60 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 nt locktype) {.
d8e70 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
d8e80 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
d8e90 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 ;. . assert( p
d8ea0 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 File );. OSTRAC
d8eb0 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 E5("UNLOCK %d %
d8ec0 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c d was %d pid=%d\
d8ed0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f n", pFile->h, lo
d8ee0 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 cktype,.
d8ef0 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
d8f00 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 pe, getpid());.
d8f10 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
d8f20 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e<=SHARED_LOCK )
d8f30 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 ;. . /* no-op
d8f40 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 if possible */.
d8f50 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
d8f60 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 type==locktype )
d8f70 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
d8f80 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 ITE_OK;. }. .
d8f90 20 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a /* shared can j
d8fa0 75 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75 ust be set becau
d8fb0 73 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 se we always hav
d8fc0 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a e an exclusive *
d8fd0 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 /. if (locktype
d8fe0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b ==SHARED_LOCK) {
d8ff0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b . pFile->lock
d9000 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b type = locktype;
d9010 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d9020 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 TE_OK;. }. .
d9030 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 2c 20 75 /* no, really, u
d9040 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 6e 74 20 nlock. */. int
d9050 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 rc = flock(pFile
d9060 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 ->h, LOCK_UN);.
d9070 20 69 66 20 28 72 63 29 20 7b 0a 20 20 20 20 69 if (rc) {. i
d9080 6e 74 20 72 2c 20 74 45 72 72 6e 6f 20 3d 20 65 nt r, tErrno = e
d9090 72 72 6e 6f 3b 0a 20 20 20 20 72 20 3d 20 73 71 rrno;. r = sq
d90a0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
d90b0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
d90c0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c SQLITE_IOERR_UNL
d90d0 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 OCK);. if( IS
d90e0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 29 20 29 _LOCK_ERROR(r) )
d90f0 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c {. pFile->l
d9100 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e astErrno = tErrn
d9110 6f 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 o;. }.#ifdef
d9120 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c SQLITE_IGNORE_FL
d9130 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a OCK_LOCK_ERRORS.
d9140 20 20 20 20 69 66 28 20 28 72 20 26 20 53 51 4c if( (r & SQL
d9150 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 ITE_IOERR) == SQ
d9160 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 LITE_IOERR ){.
d9170 20 20 20 20 72 20 3d 20 53 51 4c 49 54 45 5f 42 r = SQLITE_B
d9180 55 53 59 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 USY;. }.#endi
d9190 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f f /* SQLITE_IGNO
d91a0 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 RE_FLOCK_LOCK_ER
d91b0 52 4f 52 53 20 2a 2f 0a 20 20 20 20 0a 20 20 20 RORS */. .
d91c0 20 72 65 74 75 72 6e 20 72 3b 0a 20 20 7d 20 65 return r;. } e
d91d0 6c 73 65 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d lse {. pFile-
d91e0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c >locktype = NO_L
d91f0 4f 43 4b 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 OCK;. return
d9200 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d SQLITE_OK;. }.}
d9210 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 ../*.** Close a
d9220 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
d9230 69 6e 74 20 66 6c 6f 63 6b 43 6c 6f 73 65 28 73 int flockClose(s
d9240 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
d9250 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 {. if( id ){.
d9260 20 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 flockUnlock(i
d9270 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 7d d, NO_LOCK);. }
d9280 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 . return closeU
d9290 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a nixFile(id);.}..
d92a0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
d92b0 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
d92c0 53 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 STYLE && !OS_VXW
d92d0 4f 52 4b 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a ORK */../*******
d92e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
d92f0 20 6f 66 20 74 68 65 20 66 6c 6f 63 6b 20 6c 6f of the flock lo
d9300 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ck implementatio
d9310 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n **************
d9320 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
d9330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9370 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a ******/../******
d9380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d93a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d93b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d93c0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a ********.*******
d93d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d93e0 2a 20 42 65 67 69 6e 20 4e 61 6d 65 64 20 53 65 * Begin Named Se
d93f0 6d 61 70 68 6f 72 65 20 4c 6f 63 6b 69 6e 67 20 maphore Locking
d9400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9410 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e ********.**.** N
d9420 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c amed semaphore l
d9430 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 73 ocking is only s
d9440 75 70 70 6f 72 74 65 64 20 6f 6e 20 56 78 57 6f upported on VxWo
d9450 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70 rks..**.** Semap
d9460 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 hore locking is
d9470 6c 69 6b 65 20 64 6f 74 2d 6c 6f 63 6b 20 61 6e like dot-lock an
d9480 64 20 66 6c 6f 63 6b 20 69 6e 20 74 68 61 74 20 d flock in that
d9490 69 74 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 0a 2a it really only.*
d94a0 2a 20 73 75 70 70 6f 72 74 73 20 45 58 43 4c 55 * supports EXCLU
d94b0 53 49 56 45 20 6c 6f 63 6b 69 6e 67 2e 20 20 4f SIVE locking. O
d94c0 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f nly a single pro
d94d0 63 65 73 73 20 63 61 6e 20 72 65 61 64 20 6f 72 cess can read or
d94e0 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 64 61 write.** the da
d94f0 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 61 tabase file at a
d9500 20 74 69 6d 65 2e 20 20 54 68 69 73 20 72 65 64 time. This red
d9510 75 63 65 73 20 70 6f 74 65 6e 74 69 61 6c 20 63 uces potential c
d9520 6f 6e 63 75 72 72 65 6e 63 79 2c 20 62 75 74 0a oncurrency, but.
d9530 2a 2a 20 6d 61 6b 65 73 20 74 68 65 20 6c 6f 63 ** makes the loc
d9540 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e k implementation
d9550 20 6d 75 63 68 20 65 61 73 69 65 72 2e 0a 2a 2f much easier..*/
d9560 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a .#if OS_VXWORKS.
d9570 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
d9580 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 ine checks if th
d9590 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45 ere is a RESERVE
d95a0 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 D lock held on t
d95b0 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 he specified.**
d95c0 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 file by this or
d95d0 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 any other proces
d95e0 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 s. If such a loc
d95f0 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a k is held, set *
d9600 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 pResOut.** to a
d9610 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f non-zero value o
d9620 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 therwise *pResOu
d9630 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f t is set to zero
d9640 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 . The return va
d9650 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f lue.** is set to
d9660 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 SQLITE_OK unles
d9670 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f s an I/O error o
d9680 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 ccurs during loc
d9690 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 k checking..*/.s
d96a0 74 61 74 69 63 20 69 6e 74 20 73 65 6d 43 68 65 tatic int semChe
d96b0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 ckReservedLock(s
d96c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
d96d0 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b int *pResOut) {
d96e0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
d96f0 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 TE_OK;. int res
d9700 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 erved = 0;. uni
d9710 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
d9720 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 unixFile*)id;..
d9730 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d9740 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
d9750 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 IOERR_CHECKRESER
d9760 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 VEDLOCK; );. .
d9770 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
d9780 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 ;.. /* Check if
d9790 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 a thread in thi
d97a0 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 s process holds
d97b0 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 such a lock */.
d97c0 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
d97d0 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b type>SHARED_LOCK
d97e0 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 ){. reserved
d97f0 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f = 1;. }. . /
d9800 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 * Otherwise see
d9810 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 if some other pr
d9820 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20 ocess holds it.
d9830 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 */. if( !reserv
d9840 65 64 20 29 7b 0a 20 20 20 20 73 65 6d 5f 74 20 ed ){. sem_t
d9850 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 *pSem = pFile->p
d9860 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20 20 20 Open->pSem;.
d9870 73 74 72 75 63 74 20 73 74 61 74 20 73 74 61 74 struct stat stat
d9880 42 75 66 3b 0a 0a 20 20 20 20 69 66 28 20 73 65 Buf;.. if( se
d9890 6d 5f 74 72 79 77 61 69 74 28 70 53 65 6d 29 3d m_trywait(pSem)=
d98a0 3d 2d 31 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 =-1 ){. int
d98b0 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b tErrno = errno;
d98c0 0a 20 20 20 20 20 20 69 66 28 20 45 41 47 41 49 . if( EAGAI
d98d0 4e 20 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 N != tErrno ){.
d98e0 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
d98f0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 teErrorFromPosix
d9900 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 Error(tErrno, SQ
d9910 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b LITE_IOERR_CHECK
d9920 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 RESERVEDLOCK);.
d9930 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
d9940 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
d9950 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b ;. } else {
d9960 0a 20 20 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65 . /* some
d9970 6f 6e 65 20 65 6c 73 65 20 68 61 73 20 74 68 65 one else has the
d9980 20 6c 6f 63 6b 20 77 68 65 6e 20 77 65 20 61 72 lock when we ar
d9990 65 20 69 6e 20 4e 4f 5f 4c 4f 43 4b 20 2a 2f 0a e in NO_LOCK */.
d99a0 20 20 20 20 20 20 20 20 72 65 73 65 72 76 65 64 reserved
d99b0 20 3d 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 = (pFile->lockt
d99c0 79 70 65 20 3c 20 53 48 41 52 45 44 5f 4c 4f 43 ype < SHARED_LOC
d99d0 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 K);. }.
d99e0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 }else{. /*
d99f0 77 65 20 63 6f 75 6c 64 20 68 61 76 65 20 69 74 we could have it
d9a00 20 69 66 20 77 65 20 77 61 6e 74 20 69 74 20 2a if we want it *
d9a10 2f 0a 20 20 20 20 20 20 73 65 6d 5f 70 6f 73 74 /. sem_post
d9a20 28 70 53 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 (pSem);. }.
d9a30 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 }. OSTRACE4("TE
d9a40 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 ST WR-LOCK %d %d
d9a50 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 %d\n", pFile->h
d9a60 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b , rc, reserved);
d9a70 0a 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 .. *pResOut = r
d9a80 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 eserved;. retur
d9a90 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c n rc;.}../*.** L
d9aa0 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 ock the file wit
d9ab0 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 h the lock speci
d9ac0 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 fied by paramete
d9ad0 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 r locktype - one
d9ae0 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f .** of the follo
d9af0 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 wing:.**.**
d9b00 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a (1) SHARED_LOCK.
d9b10 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 ** (2) RESER
d9b20 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 VED_LOCK.**
d9b30 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b (3) PENDING_LOCK
d9b40 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c .** (4) EXCL
d9b50 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a USIVE_LOCK.**.**
d9b60 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 Sometimes when
d9b70 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c requesting one l
d9b80 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 ock state, addit
d9b90 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 ional lock state
d9ba0 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 s.** are inserte
d9bb0 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 d in between. T
d9bc0 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 he locking might
d9bd0 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 fail on one of
d9be0 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 the later.** tra
d9bf0 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 nsitions leaving
d9c00 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 the lock state
d9c10 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 different from w
d9c20 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 hat it started b
d9c30 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 ut.** still shor
d9c40 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 t of its goal.
d9c50 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 The following ch
d9c60 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c art shows the al
d9c70 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 lowed.** transit
d9c80 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 ions and the ins
d9c90 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 erted intermedia
d9ca0 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a te states:.**.**
d9cb0 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 UNLOCKED ->
d9cc0 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 SHARED.** SHA
d9cd0 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a RED -> RESERVED.
d9ce0 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 ** SHARED ->
d9cf0 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 (PENDING) -> EXC
d9d00 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 LUSIVE.** RES
d9d10 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e ERVED -> (PENDIN
d9d20 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a G) -> EXCLUSIVE.
d9d30 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e ** PENDING ->
d9d40 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a EXCLUSIVE.**.**
d9d50 20 53 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73 Semaphore locks
d9d60 20 6f 6e 6c 79 20 72 65 61 6c 6c 79 20 73 75 70 only really sup
d9d70 70 6f 72 74 20 45 58 43 4c 55 53 49 56 45 20 6c port EXCLUSIVE l
d9d80 6f 63 6b 73 2e 20 20 57 65 20 74 72 61 63 6b 20 ocks. We track
d9d90 69 6e 74 65 72 6d 65 64 69 61 74 65 0a 2a 2a 20 intermediate.**
d9da0 6c 6f 63 6b 20 73 74 61 74 65 73 20 69 6e 20 74 lock states in t
d9db0 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 he sqlite3_file
d9dc0 73 74 72 75 63 74 75 72 65 2c 20 62 75 74 20 61 structure, but a
d9dd0 6c 6c 20 6c 6f 63 6b 73 20 53 48 41 52 45 44 20 ll locks SHARED
d9de0 6f 72 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 or.** above are
d9df0 72 65 61 6c 6c 79 20 45 58 43 4c 55 53 49 56 45 really EXCLUSIVE
d9e00 20 6c 6f 63 6b 73 20 61 6e 64 20 65 78 63 6c 75 locks and exclu
d9e10 64 65 20 61 6c 6c 20 6f 74 68 65 72 20 70 72 6f de all other pro
d9e20 63 65 73 73 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 cesses from.** a
d9e30 63 63 65 73 73 20 74 68 65 20 66 69 6c 65 2e 0a ccess the file..
d9e40 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
d9e50 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 ne will only inc
d9e60 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 rease a lock. U
d9e70 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 se the sqlite3Os
d9e80 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 Unlock().** rout
d9e90 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c ine to lower a l
d9ea0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f ocking level..*/
d9eb0 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d 4c .static int semL
d9ec0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
d9ed0 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 *id, int lockty
d9ee0 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 pe) {. unixFile
d9ef0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
d9f00 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66 ile*)id;. int f
d9f10 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d d;. sem_t *pSem
d9f20 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d = pFile->pOpen-
d9f30 3e 70 53 65 6d 3b 0a 20 20 69 6e 74 20 72 63 20 >pSem;. int rc
d9f40 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 = SQLITE_OK;..
d9f50 2f 2a 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 /* if we already
d9f60 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 have a lock, it
d9f70 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 is exclusive.
d9f80 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 . ** Just adjus
d9f90 74 20 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 t level and punt
d9fa0 20 6f 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 on outta here.
d9fb0 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e */. if (pFile->
d9fc0 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f locktype > NO_LO
d9fd0 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d CK) {. pFile-
d9fe0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b >locktype = lock
d9ff0 74 79 70 65 3b 0a 20 20 20 20 72 63 20 3d 20 53 type;. rc = S
da000 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 67 6f QLITE_OK;. go
da010 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b to sem_end_lock;
da020 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6c 6f 63 . }. . /* loc
da030 6b 20 73 65 6d 61 70 68 6f 72 65 20 6e 6f 77 20 k semaphore now
da040 62 75 74 20 62 61 69 6c 20 6f 75 74 20 77 68 65 but bail out whe
da050 6e 20 61 6c 72 65 61 64 79 20 6c 6f 63 6b 65 64 n already locked
da060 2e 20 2a 2f 0a 20 20 69 66 28 20 73 65 6d 5f 74 . */. if( sem_t
da070 72 79 77 61 69 74 28 70 53 65 6d 29 3d 3d 2d 31 rywait(pSem)==-1
da080 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
da090 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f ITE_BUSY;. go
da0a0 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b to sem_end_lock;
da0b0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 67 6f 74 20 69 . }.. /* got i
da0c0 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20 t, set the type
da0d0 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f and return ok */
da0e0 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 . pFile->lockty
da0f0 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 0a pe = locktype;..
da100 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 sem_end_lock:.
da110 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
da120 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c *.** Lower the l
da130 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 ocking level on
da140 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
da150 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 pFile to locktyp
da160 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 e. locktype.**
da170 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e must be either N
da180 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 O_LOCK or SHARED
da190 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 _LOCK..**.** If
da1a0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
da1b0 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 l of the file de
da1c0 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 scriptor is alre
da1d0 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a ady at or below.
da1e0 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 ** the requested
da1f0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 locking level,
da200 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
da210 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 a no-op..*/.stat
da220 69 63 20 69 6e 74 20 73 65 6d 55 6e 6c 6f 63 6b ic int semUnlock
da230 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
da240 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 d, int locktype)
da250 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 {. unixFile *p
da260 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
da270 2a 29 69 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 *)id;. sem_t *p
da280 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 Sem = pFile->pOp
da290 65 6e 2d 3e 70 53 65 6d 3b 0a 0a 20 20 61 73 73 en->pSem;.. ass
da2a0 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 ert( pFile );.
da2b0 61 73 73 65 72 74 28 20 70 53 65 6d 20 29 3b 0a assert( pSem );.
da2c0 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f OSTRACE5("UNLO
da2d0 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64 CK %d %d was %d
da2e0 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c pid=%d\n", pFil
da2f0 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a e->h, locktype,.
da300 09 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 . pFile->lockt
da310 79 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a ype, getpid());.
da320 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 assert( lockty
da330 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe<=SHARED_LOCK
da340 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 );. . /* no-op
da350 20 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a if possible */.
da360 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 if( pFile->loc
da370 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 ktype==locktype
da380 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
da390 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a LITE_OK;. }. .
da3a0 20 20 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 /* shared can
da3b0 6a 75 73 74 20 62 65 20 73 65 74 20 62 65 63 61 just be set beca
da3c0 75 73 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 use we always ha
da3d0 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 ve an exclusive
da3e0 2a 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 */. if (locktyp
da3f0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 e==SHARED_LOCK)
da400 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 {. pFile->loc
da410 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 ktype = locktype
da420 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
da430 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 ITE_OK;. }. .
da440 20 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 20 75 /* no, really u
da450 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 66 20 28 nlock. */. if (
da460 20 73 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29 3d sem_post(pSem)=
da470 3d 2d 31 20 29 20 7b 0a 20 20 20 20 69 6e 74 20 =-1 ) {. int
da480 72 63 2c 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 rc, tErrno = err
da490 6e 6f 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c no;. rc = sql
da4a0 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
da4b0 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
da4c0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f QLITE_IOERR_UNLO
da4d0 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f CK);. if( IS_
da4e0 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 LOCK_ERROR(rc) )
da4f0 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c {. pFile->l
da500 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e astErrno = tErrn
da510 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 o;. }. ret
da520 75 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 20 70 urn rc; . }. p
da530 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
da540 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74 75 NO_LOCK;. retu
da550 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
da560 0a 2f 2a 0a 20 2a 2a 20 43 6c 6f 73 65 20 61 20 ./*. ** Close a
da570 66 69 6c 65 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 file.. */.static
da580 20 69 6e 74 20 73 65 6d 43 6c 6f 73 65 28 73 71 int semClose(sq
da590 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 lite3_file *id)
da5a0 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 {. if( id ){.
da5b0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
da5c0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
da5d0 64 3b 0a 20 20 20 20 73 65 6d 55 6e 6c 6f 63 6b d;. semUnlock
da5e0 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 (id, NO_LOCK);.
da5f0 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 assert( pFile
da600 20 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 );. unixEnte
da610 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 rMutex();. re
da620 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 leaseLockInfo(pF
da630 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 ile->pLock);.
da640 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 releaseOpenCnt(
da650 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 pFile->pOpen);.
da660 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 unixLeaveMute
da670 78 28 29 3b 0a 20 20 20 20 63 6c 6f 73 65 55 6e x();. closeUn
da680 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 7d 0a ixFile(id);. }.
da690 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
da6a0 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a OK;.}..#endif /*
da6b0 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 2f OS_VXWORKS */./
da6c0 2a 0a 2a 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 *.** Named semap
da6d0 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 hore locking is
da6e0 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f only available o
da6f0 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a n VxWorks..**.**
da700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
da710 64 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 73 d of the named s
da720 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 20 69 6d emaphore lock im
da730 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a plementation ***
da740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
da750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a ************/...
da7a0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
da7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
da7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
da810 6e 20 41 46 50 20 4c 6f 63 6b 69 6e 67 20 2a 2a n AFP Locking **
da820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
da840 2a 2a 0a 2a 2a 20 41 46 50 20 69 73 20 74 68 65 **.** AFP is the
da850 20 41 70 70 6c 65 20 46 69 6c 69 6e 67 20 50 72 Apple Filing Pr
da860 6f 74 6f 63 6f 6c 2e 20 20 41 46 50 20 69 73 20 otocol. AFP is
da870 61 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65 73 79 a network filesy
da880 73 74 65 6d 20 66 6f 75 6e 64 0a 2a 2a 20 6f 6e stem found.** on
da890 20 41 70 70 6c 65 20 4d 61 63 69 6e 74 6f 73 68 Apple Macintosh
da8a0 20 63 6f 6d 70 75 74 65 72 73 20 2d 20 62 6f 74 computers - bot
da8b0 68 20 4f 53 39 20 61 6e 64 20 4f 53 58 2e 0a 2a h OS9 and OSX..*
da8c0 2a 0a 2a 2a 20 54 68 69 72 64 2d 70 61 72 74 79 *.** Third-party
da8d0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
da8e0 20 6f 66 20 41 46 50 20 61 72 65 20 61 76 61 69 of AFP are avai
da8f0 6c 61 62 6c 65 2e 20 20 42 75 74 20 74 68 69 73 lable. But this
da900 20 63 6f 64 65 20 68 65 72 65 0a 2a 2a 20 6f 6e code here.** on
da910 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 4f 53 58 2e ly works on OSX.
da920 0a 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 .*/..#if defined
da930 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 (__APPLE__) && S
da940 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
da950 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a 0a 2a 2a KING_STYLE./*.**
da960 20 54 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 The afpLockingC
da970 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65 ontext structure
da980 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 61 66 contains all af
da990 70 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 63 20 p lock specific
da9a0 73 74 61 74 65 0a 2a 2f 0a 74 79 70 65 64 65 66 state.*/.typedef
da9b0 20 73 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 struct afpLocki
da9c0 6e 67 43 6f 6e 74 65 78 74 20 61 66 70 4c 6f 63 ngContext afpLoc
da9d0 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 74 72 kingContext;.str
da9e0 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f uct afpLockingCo
da9f0 6e 74 65 78 74 20 7b 0a 20 20 75 6e 73 69 67 6e ntext {. unsign
daa00 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73 68 61 ed long long sha
daa10 72 65 64 42 79 74 65 3b 0a 20 20 63 6f 6e 73 74 redByte;. const
daa20 20 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 char *dbPath;
daa30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 /* Na
daa40 6d 65 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 me of the open f
daa50 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 72 75 63 ile */.};..struc
daa60 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 t ByteRangeLockP
daa70 42 32 0a 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 B2.{. unsigned
daa80 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 long long offset
daa90 3b 20 20 20 20 20 20 20 20 2f 2a 20 6f 66 66 73 ; /* offs
daaa0 65 74 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 et to first byte
daab0 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e to lock */. un
daac0 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
daad0 20 6c 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 length;
daae0 2f 2a 20 6e 62 72 20 6f 66 20 62 79 74 65 73 20 /* nbr of bytes
daaf0 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 to lock */. uns
dab00 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
dab10 72 65 74 52 61 6e 67 65 53 74 61 72 74 3b 20 2f retRangeStart; /
dab20 2a 20 6e 62 72 20 6f 66 20 31 73 74 20 62 79 74 * nbr of 1st byt
dab30 65 20 6c 6f 63 6b 65 64 20 69 66 20 73 75 63 63 e locked if succ
dab40 65 73 73 66 75 6c 20 2a 2f 0a 20 20 75 6e 73 69 essful */. unsi
dab50 67 6e 65 64 20 63 68 61 72 20 75 6e 4c 6f 63 6b gned char unLock
dab60 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20 2f 2a Flag; /*
dab70 20 31 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30 20 3d 1 = unlock, 0 =
dab80 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 lock */. unsig
dab90 6e 65 64 20 63 68 61 72 20 73 74 61 72 74 45 6e ned char startEn
daba0 64 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20 dFlag; /*
dabb0 31 3d 72 65 6c 20 74 6f 20 65 6e 64 20 6f 66 20 1=rel to end of
dabc0 66 6f 72 6b 2c 20 30 3d 72 65 6c 20 74 6f 20 73 fork, 0=rel to s
dabd0 74 61 72 74 20 2a 2f 0a 20 20 69 6e 74 20 66 64 tart */. int fd
dabe0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
dabf0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 /* f
dac00 69 6c 65 20 64 65 73 63 20 74 6f 20 61 73 73 6f ile desc to asso
dac10 63 20 74 68 69 73 20 6c 6f 63 6b 20 77 69 74 68 c this lock with
dac20 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 */.};..#define
dac30 61 66 70 66 73 42 79 74 65 52 61 6e 67 65 4c 6f afpfsByteRangeLo
dac40 63 6b 32 46 53 43 54 4c 20 20 20 20 20 20 20 20 ck2FSCTL
dac50 5f 49 4f 57 52 28 27 7a 27 2c 20 32 33 2c 20 73 _IOWR('z', 23, s
dac60 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c truct ByteRangeL
dac70 6f 63 6b 50 42 32 29 0a 0a 2f 2a 0a 2a 2a 20 54 ockPB2)../*.** T
dac80 68 69 73 20 69 73 20 61 20 75 74 69 6c 69 74 79 his is a utility
dac90 20 66 6f 72 20 73 65 74 74 69 6e 67 20 6f 72 20 for setting or
daca0 63 6c 65 61 72 69 6e 67 20 61 20 62 69 74 2d 72 clearing a bit-r
dacb0 61 6e 67 65 20 6c 6f 63 6b 20 6f 6e 20 61 6e 0a ange lock on an.
dacc0 2a 2a 20 41 46 50 20 66 69 6c 65 73 79 73 74 65 ** AFP filesyste
dacd0 6d 2e 0a 2a 2a 20 0a 2a 2a 20 52 65 74 75 72 6e m..** .** Return
dace0 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 SQLITE_OK on su
dacf0 63 63 65 73 73 2c 20 53 51 4c 49 54 45 5f 42 55 ccess, SQLITE_BU
dad00 53 59 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a SY on failure..*
dad10 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 /.static int afp
dad20 53 65 74 4c 6f 63 6b 28 0a 20 20 63 6f 6e 73 74 SetLock(. const
dad30 20 63 68 61 72 20 2a 70 61 74 68 2c 20 20 20 20 char *path,
dad40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
dad50 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f e of the file to
dad60 20 62 65 20 6c 6f 63 6b 65 64 20 6f 72 20 75 6e be locked or un
dad70 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e 69 78 locked */. unix
dad80 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 File *pFile,
dad90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 /* Op
dada0 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 en file descript
dadb0 6f 72 20 6f 6e 20 70 61 74 68 20 2a 2f 0a 20 20 or on path */.
dadc0 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
dadd0 6e 67 20 6f 66 66 73 65 74 2c 20 20 20 20 20 2f ng offset, /
dade0 2a 20 46 69 72 73 74 20 62 79 74 65 20 74 6f 20 * First byte to
dadf0 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 be locked */. u
dae00 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
dae10 67 20 6c 65 6e 67 74 68 2c 20 20 20 20 20 2f 2a g length, /*
dae20 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
dae30 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e to lock */. in
dae40 74 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20 20 20 t setLockFlag
dae50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
dae60 54 72 75 65 20 74 6f 20 73 65 74 20 6c 6f 63 6b True to set lock
dae70 2e 20 20 46 61 6c 73 65 20 74 6f 20 63 6c 65 61 . False to clea
dae80 72 20 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a 20 20 73 r lock */.){. s
dae90 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c truct ByteRangeL
daea0 6f 63 6b 50 42 32 20 70 62 3b 0a 20 20 69 6e 74 ockPB2 pb;. int
daeb0 20 65 72 72 3b 0a 20 20 0a 20 20 70 62 2e 75 6e err;. . pb.un
daec0 4c 6f 63 6b 46 6c 61 67 20 3d 20 73 65 74 4c 6f LockFlag = setLo
daed0 63 6b 46 6c 61 67 20 3f 20 30 20 3a 20 31 3b 0a ckFlag ? 0 : 1;.
daee0 20 20 70 62 2e 73 74 61 72 74 45 6e 64 46 6c 61 pb.startEndFla
daef0 67 20 3d 20 30 3b 0a 20 20 70 62 2e 6f 66 66 73 g = 0;. pb.offs
daf00 65 74 20 3d 20 6f 66 66 73 65 74 3b 0a 20 20 70 et = offset;. p
daf10 62 2e 6c 65 6e 67 74 68 20 3d 20 6c 65 6e 67 74 b.length = lengt
daf20 68 3b 20 0a 20 20 70 62 2e 66 64 20 3d 20 70 46 h; . pb.fd = pF
daf30 69 6c 65 2d 3e 68 3b 0a 20 20 0a 20 20 4f 53 54 ile->h;. . OST
daf40 52 41 43 45 36 28 22 41 46 50 53 45 54 4c 4f 43 RACE6("AFPSETLOC
daf50 4b 20 5b 25 73 5d 20 66 6f 72 20 25 64 25 73 20 K [%s] for %d%s
daf60 69 6e 20 72 61 6e 67 65 20 25 6c 6c 78 3a 25 6c in range %llx:%l
daf70 6c 78 5c 6e 22 2c 20 0a 20 20 20 20 28 73 65 74 lx\n", . (set
daf80 4c 6f 63 6b 46 6c 61 67 3f 22 4f 4e 22 3a 22 4f LockFlag?"ON":"O
daf90 46 46 22 29 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 FF"), pFile->h,
dafa0 28 70 62 2e 66 64 3d 3d 2d 31 3f 22 5b 74 65 73 (pb.fd==-1?"[tes
dafb0 74 76 61 6c 2d 31 5d 22 3a 22 22 29 2c 0a 20 20 tval-1]":""),.
dafc0 20 20 6f 66 66 73 65 74 2c 20 6c 65 6e 67 74 68 offset, length
dafd0 29 3b 0a 20 20 65 72 72 20 3d 20 66 73 63 74 6c );. err = fsctl
dafe0 28 70 61 74 68 2c 20 61 66 70 66 73 42 79 74 65 (path, afpfsByte
daff0 52 61 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 2c RangeLock2FSCTL,
db000 20 26 70 62 2c 20 30 29 3b 0a 20 20 69 66 20 28 &pb, 0);. if (
db010 20 65 72 72 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 err==-1 ) {.
db020 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 69 6e 74 int rc;. int
db030 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b tErrno = errno;
db040 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 41 . OSTRACE4("A
db050 46 50 53 45 54 4c 4f 43 4b 20 66 61 69 6c 65 64 FPSETLOCK failed
db060 20 74 6f 20 66 73 63 74 6c 28 29 20 27 25 73 27 to fsctl() '%s'
db070 20 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 %d %s\n",.
db080 20 20 20 20 20 20 20 20 70 61 74 68 2c 20 74 45 path, tE
db090 72 72 6e 6f 2c 20 73 74 72 65 72 72 6f 72 28 74 rrno, strerror(t
db0a0 45 72 72 6e 6f 29 29 3b 0a 23 69 66 64 65 66 20 Errno));.#ifdef
db0b0 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46 SQLITE_IGNORE_AF
db0c0 50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 P_LOCK_ERRORS.
db0d0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 rc = SQLITE_BU
db0e0 53 59 3b 0a 23 65 6c 73 65 0a 20 20 20 20 72 63 SY;.#else. rc
db0f0 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 = sqliteErrorFr
db100 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 omPosixError(tEr
db110 72 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 rno,.
db120 20 20 20 20 20 20 20 20 20 73 65 74 4c 6f 63 6b setLock
db130 46 6c 61 67 20 3f 20 53 51 4c 49 54 45 5f 49 4f Flag ? SQLITE_IO
db140 45 52 52 5f 4c 4f 43 4b 20 3a 20 53 51 4c 49 54 ERR_LOCK : SQLIT
db150 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b E_IOERR_UNLOCK);
db160 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
db170 45 5f 49 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 E_IGNORE_AFP_LOC
db180 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 K_ERRORS */.
db190 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f if( IS_LOCK_ERRO
db1a0 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 R(rc) ){. p
db1b0 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
db1c0 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a = tErrno;. }.
db1d0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
db1e0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 } else {. re
db1f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
db200 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 }.}../*.** Thi
db210 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 s routine checks
db220 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 if there is a R
db230 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c ESERVED lock hel
db240 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 d on the specifi
db250 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 ed.** file by th
db260 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 is or any other
db270 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 process. If such
db280 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c a lock is held,
db290 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a set *pResOut.**
db2a0 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 to a non-zero v
db2b0 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a alue otherwise *
db2c0 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 pResOut is set t
db2d0 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 o zero. The ret
db2e0 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 urn value.** is
db2f0 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b set to SQLITE_OK
db300 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 unless an I/O e
db310 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 rror occurs duri
db320 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 ng lock checking
db330 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
db340 61 66 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 afpCheckReserved
db350 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
db360 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 e *id, int *pRes
db370 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d Out){. int rc =
db380 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e SQLITE_OK;. in
db390 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a t reserved = 0;.
db3a0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
db3b0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
db3c0 64 3b 0a 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 d;. . Simulate
db3d0 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 IOError( return
db3e0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 SQLITE_IOERR_CHE
db3f0 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 CKRESERVEDLOCK;
db400 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 );. . assert(
db410 70 46 69 6c 65 20 29 3b 0a 20 20 61 66 70 4c 6f pFile );. afpLo
db420 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f ckingContext *co
db430 6e 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b ntext = (afpLock
db440 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 ingContext *) pF
db450 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
db460 65 78 74 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 ext;. . /* Che
db470 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 ck if a thread i
db480 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 n this process h
db490 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b olds such a lock
db4a0 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d */. if( pFile-
db4b0 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 >locktype>SHARED
db4c0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 _LOCK ){. res
db4d0 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 erved = 1;. }.
db4e0 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 . /* Otherwise
db4f0 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 see if some oth
db500 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 er process holds
db510 20 69 74 2e 0a 20 20 20 2a 2f 0a 20 20 69 66 28 it.. */. if(
db520 20 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 !reserved ){.
db530 20 20 2f 2a 20 6c 6f 63 6b 20 74 68 65 20 52 45 /* lock the RE
db540 53 45 52 56 45 44 20 62 79 74 65 20 2a 2f 0a 20 SERVED byte */.
db550 20 20 20 69 6e 74 20 6c 72 63 20 3d 20 61 66 70 int lrc = afp
db560 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d SetLock(context-
db570 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 >dbPath, pFile,
db580 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 RESERVED_BYTE, 1
db590 2c 31 29 3b 20 20 0a 20 20 20 20 69 66 28 20 53 ,1); . if( S
db5a0 51 4c 49 54 45 5f 4f 4b 3d 3d 6c 72 63 20 29 7b QLITE_OK==lrc ){
db5b0 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 77 65 20 . /* if we
db5c0 73 75 63 63 65 65 64 65 64 20 69 6e 20 74 61 6b succeeded in tak
db5d0 69 6e 67 20 74 68 65 20 72 65 73 65 72 76 65 64 ing the reserved
db5e0 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 lock, unlock it
db5f0 20 74 6f 20 72 65 73 74 6f 72 65 0a 20 20 20 20 to restore.
db600 20 20 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 ** the origina
db610 6c 20 73 74 61 74 65 20 2a 2f 0a 20 20 20 20 20 l state */.
db620 20 6c 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 lrc = afpSetLoc
db630 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 k(context->dbPat
db640 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 h, pFile, RESERV
db650 45 44 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a ED_BYTE, 1, 0);.
db660 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 } else {.
db670 20 20 20 2f 2a 20 69 66 20 77 65 20 66 61 69 6c /* if we fail
db680 65 64 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f ed to get the lo
db690 63 6b 20 74 68 65 6e 20 73 6f 6d 65 6f 6e 65 20 ck then someone
db6a0 65 6c 73 65 20 6d 75 73 74 20 68 61 76 65 20 69 else must have i
db6b0 74 20 2a 2f 0a 20 20 20 20 20 20 72 65 73 65 72 t */. reser
db6c0 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 ved = 1;. }.
db6d0 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
db6e0 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 RROR(lrc) ){.
db6f0 20 20 20 72 63 3d 6c 72 63 3b 0a 20 20 20 20 7d rc=lrc;. }
db700 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41 43 . }. . OSTRAC
db710 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b E4("TEST WR-LOCK
db720 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 %d %d %d\n", pF
db730 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 ile->h, rc, rese
db740 72 76 65 64 29 3b 0a 20 20 0a 20 20 2a 70 52 65 rved);. . *pRe
db750 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b sOut = reserved;
db760 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
db770 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 ./*.** Lock the
db780 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f file with the lo
db790 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20 ck specified by
db7a0 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 parameter lockty
db7b0 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 pe - one.** of t
db7c0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a he following:.**
db7d0 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 .** (1) SHAR
db7e0 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 ED_LOCK.** (
db7f0 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 2) RESERVED_LOCK
db800 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 .** (3) PEND
db810 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 ING_LOCK.**
db820 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f (4) EXCLUSIVE_LO
db830 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d CK.**.** Sometim
db840 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 es when requesti
db850 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 ng one lock stat
db860 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f e, additional lo
db870 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 ck states.** are
db880 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 inserted in bet
db890 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 ween. The locki
db8a0 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e ng might fail on
db8b0 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 one of the late
db8c0 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 r.** transitions
db8d0 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 leaving the loc
db8e0 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e k state differen
db8f0 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 t from what it s
db900 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 tarted but.** st
db910 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 ill short of its
db920 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c goal. The foll
db930 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 owing chart show
db940 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a s the allowed.**
db950 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 transitions and
db960 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e the inserted in
db970 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 termediate state
db980 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f s:.**.** UNLO
db990 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a CKED -> SHARED.*
db9a0 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 * SHARED -> R
db9b0 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 ESERVED.** SH
db9c0 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 ARED -> (PENDING
db9d0 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a ) -> EXCLUSIVE.*
db9e0 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e * RESERVED ->
db9f0 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 (PENDING) -> EX
dba00 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 CLUSIVE.** PE
dba10 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 NDING -> EXCLUSI
dba20 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f VE.**.** This ro
dba30 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 utine will only
dba40 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e increase a lock.
dba50 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 Use the sqlite
dba60 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 3OsUnlock().** r
dba70 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 outine to lower
dba80 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e a locking level.
dba90 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 .*/.static int a
dbaa0 66 70 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 fpLock(sqlite3_f
dbab0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 ile *id, int loc
dbac0 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 ktype){. int rc
dbad0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
dbae0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
dbaf0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
dbb00 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e . afpLockingCon
dbb10 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 text *context =
dbb20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 (afpLockingConte
dbb30 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 xt *) pFile->loc
dbb40 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a kingContext;. .
dbb50 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
dbb60 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 4c );. OSTRACE5("L
dbb70 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 73 OCK %d %s was
dbb80 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 %s pid=%d\n", p
dbb90 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 File->h,.
dbba0 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c locktypeName(l
dbbb0 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79 ocktype), lockty
dbbc0 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f peName(pFile->lo
dbbd0 63 6b 74 79 70 65 29 2c 20 67 65 74 70 69 64 28 cktype), getpid(
dbbe0 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 ));.. /* If the
dbbf0 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 re is already a
dbc00 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 lock of this typ
dbc10 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 e or more restri
dbc20 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a ctive on the. *
dbc30 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e * unixFile, do n
dbc40 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 othing. Don't us
dbc50 65 20 74 68 65 20 61 66 70 5f 65 6e 64 5f 6c 6f e the afp_end_lo
dbc60 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 ck: exit path, a
dbc70 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74 65 72 s. ** unixEnter
dbc80 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62 Mutex() hasn't b
dbc90 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a een called yet..
dbca0 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 */. if( pFile
dbcb0 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b ->locktype>=lock
dbcc0 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 type ){. OSTR
dbcd0 41 43 45 33 28 22 4c 4f 43 4b 20 20 20 20 25 64 ACE3("LOCK %d
dbce0 20 25 73 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 %s ok (already
dbcf0 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d held)\n", pFile-
dbd00 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c >h,. l
dbd10 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b ocktypeName(lock
dbd20 74 79 70 65 29 29 3b 0a 20 20 20 20 72 65 74 75 type));. retu
dbd30 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
dbd40 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 }.. /* Make sur
dbd50 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 e the locking se
dbd60 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 quence is correc
dbd70 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 t. */. assert(
dbd80 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
dbd90 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 !=NO_LOCK || loc
dbda0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
dbdb0 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 CK );. assert(
dbdc0 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e locktype!=PENDIN
dbdd0 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 G_LOCK );. asse
dbde0 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 rt( locktype!=RE
dbdf0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 SERVED_LOCK || p
dbe00 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d File->locktype==
dbe10 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 SHARED_LOCK );.
dbe20 20 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 . /* This mute
dbe30 78 20 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 x is needed beca
dbe40 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b use pFile->pLock
dbe50 20 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 is shared acros
dbe60 73 20 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20 s threads. */.
dbe70 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
dbe80 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 );.. /* Make su
dbe90 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 re the current t
dbea0 68 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70 hread owns the p
dbeb0 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 File.. */. rc
dbec0 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 = transferOwners
dbed0 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 hip(pFile);. if
dbee0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
dbef0 29 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 ){. unixLeave
dbf00 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74 Mutex();. ret
dbf10 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 20 20 urn rc;. }.
dbf20 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20 . /* A PENDING
dbf30 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65 64 20 62 lock is needed b
dbf40 65 66 6f 72 65 20 61 63 71 75 69 72 69 6e 67 20 efore acquiring
dbf50 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e a SHARED lock an
dbf60 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 63 d before. ** ac
dbf70 71 75 69 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 quiring an EXCLU
dbf80 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 SIVE lock. For
dbf90 74 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c the SHARED lock,
dbfa0 20 74 68 65 20 50 45 4e 44 49 4e 47 20 77 69 6c the PENDING wil
dbfb0 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 l. ** be releas
dbfc0 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c ed.. */. if( l
dbfd0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
dbfe0 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c 20 28 LOCK . || (
dbff0 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 locktype==EXCLUS
dc000 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c IVE_LOCK && pFil
dc010 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 4e 44 e->locktype<PEND
dc020 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 ING_LOCK). ){.
dc030 20 20 20 69 6e 74 20 66 61 69 6c 65 64 3b 0a 20 int failed;.
dc040 20 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53 failed = afpS
dc050 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e etLock(context->
dc060 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 dbPath, pFile, P
dc070 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 ENDING_BYTE, 1,
dc080 31 29 3b 0a 20 20 20 20 69 66 20 28 66 61 69 6c 1);. if (fail
dc090 65 64 29 20 7b 0a 20 20 20 20 20 20 72 63 20 3d ed) {. rc =
dc0a0 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 67 failed;. g
dc0b0 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b oto afp_end_lock
dc0c0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 ;. }. }. .
dc0d0 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67 /* If control g
dc0e0 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e ets to this poin
dc0f0 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c 6c 79 t, then actually
dc100 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 6d 61 go ahead and ma
dc110 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e ke. ** operatin
dc120 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 66 g system calls f
dc130 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64 or the specified
dc140 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 lock.. */. if
dc150 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 ( locktype==SHAR
dc160 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 69 ED_LOCK ){. i
dc170 6e 74 20 6c 6b 2c 20 6c 72 63 31 2c 20 6c 72 63 nt lk, lrc1, lrc
dc180 32 3b 0a 20 20 20 20 69 6e 74 20 6c 72 63 31 45 2;. int lrc1E
dc190 72 72 6e 6f 20 3d 20 30 3b 0a 20 20 20 20 0a 20 rrno = 0;. .
dc1a0 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68 /* Now get th
dc1b0 65 20 72 65 61 64 2d 6c 6f 63 6b 20 53 48 41 52 e read-lock SHAR
dc1c0 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20 20 2f ED_LOCK */. /
dc1d0 2a 20 6e 6f 74 65 20 74 68 61 74 20 74 68 65 20 * note that the
dc1e0 71 75 61 6c 69 74 79 20 6f 66 20 74 68 65 20 72 quality of the r
dc1f0 61 6e 64 6f 6d 6e 65 73 73 20 64 6f 65 73 6e 27 andomness doesn'
dc200 74 20 6d 61 74 74 65 72 20 74 68 61 74 20 6d 75 t matter that mu
dc210 63 68 20 2a 2f 0a 20 20 20 20 6c 6b 20 3d 20 72 ch */. lk = r
dc220 61 6e 64 6f 6d 28 29 3b 20 0a 20 20 20 20 63 6f andom(); . co
dc230 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 ntext->sharedByt
dc240 65 20 3d 20 28 6c 6b 20 26 20 30 78 37 66 66 66 e = (lk & 0x7fff
dc250 66 66 66 66 29 25 28 53 48 41 52 45 44 5f 53 49 ffff)%(SHARED_SI
dc260 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20 6c 72 63 ZE - 1);. lrc
dc270 31 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 1 = afpSetLock(c
dc280 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 ontext->dbPath,
dc290 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 20 20 20 pFile, .
dc2a0 20 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b 63 SHARED_FIRST+c
dc2b0 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79 ontext->sharedBy
dc2c0 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 69 te, 1, 1);. i
dc2d0 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
dc2e0 28 6c 72 63 31 29 20 29 7b 0a 20 20 20 20 20 20 (lrc1) ){.
dc2f0 6c 72 63 31 45 72 72 6e 6f 20 3d 20 70 46 69 6c lrc1Errno = pFil
dc300 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 e->lastErrno;.
dc310 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 }. /* Drop
dc320 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45 the temporary PE
dc330 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 NDING lock */.
dc340 20 20 6c 72 63 32 20 3d 20 61 66 70 53 65 74 4c lrc2 = afpSetL
dc350 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 ock(context->dbP
dc360 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 ath, pFile, PEND
dc370 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b ING_BYTE, 1, 0);
dc380 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 49 53 . . if( IS
dc390 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 31 _LOCK_ERROR(lrc1
dc3a0 29 20 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c ) ) {. pFil
dc3b0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c e->lastErrno = l
dc3c0 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 rc1Errno;.
dc3d0 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 20 rc = lrc1;.
dc3e0 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f goto afp_end_lo
dc3f0 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 ck;. } else i
dc400 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
dc410 28 6c 72 63 32 29 20 29 7b 0a 20 20 20 20 20 20 (lrc2) ){.
dc420 72 63 20 3d 20 6c 72 63 32 3b 0a 20 20 20 20 20 rc = lrc2;.
dc430 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f goto afp_end_lo
dc440 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 ck;. } else i
dc450 66 28 20 6c 72 63 31 20 21 3d 20 53 51 4c 49 54 f( lrc1 != SQLIT
dc460 45 5f 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20 72 E_OK ) {. r
dc470 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 7d 20 c = lrc1;. }
dc480 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 70 46 69 else {. pFi
dc490 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 le->locktype = S
dc4a0 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 HARED_LOCK;.
dc4b0 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e pFile->pOpen->
dc4c0 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 7d 0a 20 nLock++;. }.
dc4d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 }else{. /* T
dc4e0 68 65 20 72 65 71 75 65 73 74 20 77 61 73 20 66 he request was f
dc4f0 6f 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 or a RESERVED or
dc500 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e EXCLUSIVE lock.
dc510 20 20 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 It is. ** a
dc520 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 72 ssumed that ther
dc530 65 20 69 73 20 61 20 53 48 41 52 45 44 20 6f 72 e is a SHARED or
dc540 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e greater lock on
dc550 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a the file. **
dc560 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f already.. */
dc570 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64 20 . int failed
dc580 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 = 0;. assert(
dc590 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 0!=pFile->lockt
dc5a0 79 70 65 20 29 3b 0a 20 20 20 20 69 66 20 28 6c ype );. if (l
dc5b0 6f 63 6b 74 79 70 65 20 3e 3d 20 52 45 53 45 52 ocktype >= RESER
dc5c0 56 45 44 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c VED_LOCK && pFil
dc5d0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 52 45 e->locktype < RE
dc5e0 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 SERVED_LOCK) {.
dc5f0 20 20 20 20 20 20 20 2f 2a 20 41 63 71 75 69 72 /* Acquir
dc600 65 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 e a RESERVED loc
dc610 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 69 k */. fai
dc620 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b led = afpSetLock
dc630 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 (context->dbPath
dc640 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 , pFile, RESERVE
dc650 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 0a 20 20 D_BYTE, 1,1);.
dc660 20 20 7d 0a 20 20 20 20 69 66 20 28 21 66 61 69 }. if (!fai
dc670 6c 65 64 20 26 26 20 6c 6f 63 6b 74 79 70 65 20 led && locktype
dc680 3d 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 == EXCLUSIVE_LOC
dc690 4b 29 20 7b 0a 20 20 20 20 20 20 2f 2a 20 41 63 K) {. /* Ac
dc6a0 71 75 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49 quire an EXCLUSI
dc6b0 56 45 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 VE lock */.
dc6c0 20 20 20 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d . /* Rem
dc6d0 6f 76 65 20 74 68 65 20 73 68 61 72 65 64 20 6c ove the shared l
dc6e0 6f 63 6b 20 62 65 66 6f 72 65 20 74 72 79 69 6e ock before tryin
dc6f0 67 20 74 68 65 20 72 61 6e 67 65 2e 20 20 77 65 g the range. we
dc700 27 6c 6c 20 6e 65 65 64 20 74 6f 20 0a 20 20 20 'll need to .
dc710 20 20 20 2a 2a 20 72 65 65 73 74 61 62 6c 69 73 ** reestablis
dc720 68 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 h the shared loc
dc730 6b 20 69 66 20 77 65 20 63 61 6e 27 74 20 67 65 k if we can't ge
dc740 74 20 74 68 65 20 20 61 66 70 55 6e 6c 6f 63 6b t the afpUnlock
dc750 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
dc760 69 66 28 20 21 28 66 61 69 6c 65 64 20 3d 20 61 if( !(failed = a
dc770 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 fpSetLock(contex
dc780 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 t->dbPath, pFile
dc790 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b , SHARED_FIRST +
dc7a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
dc7b0 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 78 contex
dc7c0 74 2d 3e 73 68 61 72 65 64 42 79 74 65 2c 20 31 t->sharedByte, 1
dc7d0 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 20 20 20 , 0)) ){.
dc7e0 20 69 6e 74 20 66 61 69 6c 65 64 32 20 3d 20 53 int failed2 = S
dc7f0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
dc800 20 20 2f 2a 20 6e 6f 77 20 61 74 74 65 6d 6d 70 /* now attemmp
dc810 74 20 74 6f 20 67 65 74 20 74 68 65 20 65 78 63 t to get the exc
dc820 6c 75 73 69 76 65 20 6c 6f 63 6b 20 72 61 6e 67 lusive lock rang
dc830 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 69 e */. fai
dc840 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b led = afpSetLock
dc850 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 (context->dbPath
dc860 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f , pFile, SHARED_
dc870 46 49 52 53 54 2c 20 0a 20 20 20 20 20 20 20 20 FIRST, .
dc880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dc890 20 20 20 20 20 20 20 53 48 41 52 45 44 5f 53 49 SHARED_SI
dc8a0 5a 45 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 ZE, 1);.
dc8b0 69 66 28 20 66 61 69 6c 65 64 20 26 26 20 28 66 if( failed && (f
dc8c0 61 69 6c 65 64 32 20 3d 20 61 66 70 53 65 74 4c ailed2 = afpSetL
dc8d0 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 ock(context->dbP
dc8e0 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 ath, pFile, .
dc8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dc900 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 53 54 SHARED_FIRST
dc910 20 2b 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 + context->shar
dc920 65 64 42 79 74 65 2c 20 31 2c 20 31 29 29 20 29 edByte, 1, 1)) )
dc930 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 {. /* C
dc940 61 6e 27 74 20 72 65 65 73 74 61 62 6c 69 73 68 an't reestablish
dc950 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b the shared lock
dc960 2e 20 20 53 71 6c 69 74 65 20 63 61 6e 27 74 20 . Sqlite can't
dc970 64 65 61 6c 2c 20 74 68 69 73 20 69 73 0a 20 20 deal, this is.
dc980 20 20 20 20 20 20 20 20 2a 2a 20 61 20 63 72 69 ** a cri
dc990 74 69 63 61 6c 20 49 2f 4f 20 65 72 72 6f 72 0a tical I/O error.
dc9a0 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 */.
dc9b0 20 20 20 20 20 20 20 72 63 20 3d 20 28 28 66 61 rc = ((fa
dc9c0 69 6c 65 64 20 26 20 53 51 4c 49 54 45 5f 49 4f iled & SQLITE_IO
dc9d0 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 ERR) == SQLITE_I
dc9e0 4f 45 52 52 29 20 3f 20 66 61 69 6c 65 64 32 20 OERR) ? failed2
dc9f0 3a 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 : .
dca00 20 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c SQLITE_IOERR_L
dca10 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 67 OCK;. g
dca20 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b oto afp_end_lock
dca30 3b 0a 20 20 20 20 20 20 20 20 7d 20 0a 20 20 20 ;. } .
dca40 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
dca50 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 20 0a rc = failed; .
dca60 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
dca70 20 20 69 66 28 20 66 61 69 6c 65 64 20 29 7b 0a if( failed ){.
dca80 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 rc = faile
dca90 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a d;. }. }. .
dcaa0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
dcab0 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 _OK ){. pFile
dcac0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 ->locktype = loc
dcad0 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69 ktype;. }else i
dcae0 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 f( locktype==EXC
dcaf0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 LUSIVE_LOCK ){.
dcb00 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
dcb10 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 pe = PENDING_LOC
dcb20 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66 70 5f 65 6e K;. }. .afp_en
dcb30 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65 d_lock:. unixLe
dcb40 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 aveMutex();. OS
dcb50 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 TRACE4("LOCK
dcb60 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 %d %s %s\n", pFi
dcb70 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e le->h, locktypeN
dcb80 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a ame(locktype), .
dcb90 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c rc==SQL
dcba0 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 ITE_OK ? "ok" :
dcbb0 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 72 65 74 "failed");. ret
dcbc0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
dcbd0 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 Lower the locki
dcbe0 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 ng level on file
dcbf0 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c descriptor pFil
dcc00 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 e to locktype.
dcc10 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 locktype.** must
dcc20 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f be either NO_LO
dcc30 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 CK or SHARED_LOC
dcc40 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 K..**.** If the
dcc50 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 locking level of
dcc60 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
dcc70 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 ptor is already
dcc80 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 at or below.** t
dcc90 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 he requested loc
dcca0 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 king level, this
dccb0 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
dccc0 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 -op..*/.static i
dccd0 6e 74 20 61 66 70 55 6e 6c 6f 63 6b 28 73 71 6c nt afpUnlock(sql
dcce0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
dccf0 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 nt locktype) {.
dcd00 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
dcd10 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 _OK;. unixFile
dcd20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
dcd30 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63 le*)id;. afpLoc
dcd40 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 kingContext *pCt
dcd50 78 20 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 x = (afpLockingC
dcd60 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 6c 65 2d ontext *) pFile-
dcd70 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b >lockingContext;
dcd80 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c .. assert( pFil
dcd90 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 e );. OSTRACE5(
dcda0 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 "UNLOCK %d %d w
dcdb0 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c as %d pid=%d\n",
dcdc0 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 pFile->h, lockt
dcdd0 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 70 46 ype,. pF
dcde0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 ile->locktype, g
dcdf0 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 61 73 73 etpid());.. ass
dce00 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 ert( locktype<=S
dce10 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
dce20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
dce30 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b ype<=locktype ){
dce40 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
dce50 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 TE_OK;. }. if(
dce60 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 CHECK_THREADID(
dce70 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 72 65 pFile) ){. re
dce80 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 turn SQLITE_MISU
dce90 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e SE;. }. unixEn
dcea0 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 terMutex();. if
dceb0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
dcec0 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b e>SHARED_LOCK ){
dced0 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 70 46 . . if( pF
dcee0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 45 ile->locktype==E
dcef0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b XCLUSIVE_LOCK ){
dcf00 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66 70 53 . rc = afpS
dcf10 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 etLock(pCtx->dbP
dcf20 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 ath, pFile, SHAR
dcf30 45 44 5f 46 49 52 53 54 2c 20 53 48 41 52 45 44 ED_FIRST, SHARED
dcf40 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20 20 _SIZE, 0);.
dcf50 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
dcf60 4f 4b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d OK && locktype==
dcf70 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SHARED_LOCK ){.
dcf80 20 20 20 20 20 20 20 2f 2a 20 6f 6e 6c 79 20 72 /* only r
dcf90 65 2d 65 73 74 61 62 6c 69 73 68 20 74 68 65 20 e-establish the
dcfa0 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 66 20 6e shared lock if n
dcfb0 65 63 65 73 73 61 72 79 20 2a 2f 0a 20 20 20 20 ecessary */.
dcfc0 20 20 20 20 69 6e 74 20 73 68 61 72 65 64 4c 6f int sharedLo
dcfd0 63 6b 42 79 74 65 20 3d 20 53 48 41 52 45 44 5f ckByte = SHARED_
dcfe0 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 68 61 72 FIRST+pCtx->shar
dcff0 65 64 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20 edByte;.
dd000 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 rc = afpSetLock(
dd010 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 pCtx->dbPath, pF
dd020 69 6c 65 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42 ile, sharedLockB
dd030 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 yte, 1, 1);.
dd040 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
dd050 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
dd060 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 && pFile->lockty
dd070 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b pe>=PENDING_LOCK
dd080 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 ){. rc = a
dd090 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e fpSetLock(pCtx->
dd0a0 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 dbPath, pFile, P
dd0b0 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 ENDING_BYTE, 1,
dd0c0 30 29 3b 0a 20 20 20 20 7d 20 0a 20 20 20 20 69 0);. } . i
dd0d0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
dd0e0 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 && pFile->lockt
dd0f0 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f ype>=RESERVED_LO
dd100 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d CK ){. rc =
dd110 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 afpSetLock(pCtx
dd120 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c ->dbPath, pFile,
dd130 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 RESERVED_BYTE,
dd140 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 1, 0);. }. }
dd150 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 else if( locktyp
dd160 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 e==NO_LOCK ){.
dd170 20 20 2f 2a 20 63 6c 65 61 72 20 74 68 65 20 73 /* clear the s
dd180 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 hared lock */.
dd190 20 20 69 6e 74 20 73 68 61 72 65 64 4c 6f 63 6b int sharedLock
dd1a0 42 79 74 65 20 3d 20 53 48 41 52 45 44 5f 46 49 Byte = SHARED_FI
dd1b0 52 53 54 2b 70 43 74 78 2d 3e 73 68 61 72 65 64 RST+pCtx->shared
dd1c0 42 79 74 65 3b 0a 20 20 20 20 72 63 20 3d 20 61 Byte;. rc = a
dd1d0 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e fpSetLock(pCtx->
dd1e0 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 73 dbPath, pFile, s
dd1f0 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 haredLockByte, 1
dd200 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 , 0);. }.. if(
dd210 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
dd220 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 {. if( lockty
dd230 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 pe==NO_LOCK ){.
dd240 20 20 20 20 20 73 74 72 75 63 74 20 75 6e 69 78 struct unix
dd250 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d OpenCnt *pOpen =
dd260 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 pFile->pOpen;.
dd270 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 pOpen->nLoc
dd280 6b 2d 2d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 k--;. asser
dd290 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e t( pOpen->nLock>
dd2a0 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 =0 );. if(
dd2b0 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 pOpen->nLock==0
dd2c0 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
dd2d0 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 28 closePendingFds(
dd2e0 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 7d 0a pFile);. }.
dd2f0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 75 6e 69 78 }. }. unix
dd300 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
dd310 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
dd320 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e K ){. pFile->
dd330 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 locktype = lockt
dd340 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ype;. }. retur
dd350 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 n rc;.}../*.** C
dd360 6c 6f 73 65 20 61 20 66 69 6c 65 20 26 20 63 6c lose a file & cl
dd370 65 61 6e 75 70 20 41 46 50 20 73 70 65 63 69 66 eanup AFP specif
dd380 69 63 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 ic locking conte
dd390 78 74 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e xt .*/.static in
dd3a0 74 20 61 66 70 43 6c 6f 73 65 28 73 71 6c 69 74 t afpClose(sqlit
dd3b0 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 e3_file *id) {.
dd3c0 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75 if( id ){. u
dd3d0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
dd3e0 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
dd3f0 20 20 20 20 61 66 70 55 6e 6c 6f 63 6b 28 69 64 afpUnlock(id
dd400 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 , NO_LOCK);.
dd410 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
dd420 3b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d ;. if( pFile-
dd430 3e 70 4f 70 65 6e 20 26 26 20 70 46 69 6c 65 2d >pOpen && pFile-
dd440 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b >pOpen->nLock ){
dd450 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
dd460 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69 re are outstandi
dd470 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 ng locks, do not
dd480 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f 73 65 20 actually close
dd490 74 68 65 20 66 69 6c 65 20 6a 75 73 74 0a 20 20 the file just.
dd4a0 20 20 20 20 2a 2a 20 79 65 74 20 62 65 63 61 75 ** yet becau
dd4b0 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20 63 6c se that would cl
dd4c0 65 61 72 20 74 68 6f 73 65 20 6c 6f 63 6b 73 2e ear those locks.
dd4d0 20 20 49 6e 73 74 65 61 64 2c 20 61 64 64 20 74 Instead, add t
dd4e0 68 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a he file. **
dd4f0 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 70 descriptor to p
dd500 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 2e 20 Open->aPending.
dd510 20 49 74 20 77 69 6c 6c 20 62 65 20 61 75 74 6f It will be auto
dd520 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 64 matically closed
dd530 20 77 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 74 when. ** t
dd540 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20 he last lock is
dd550 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 20 20 2a cleared.. *
dd560 2f 0a 20 20 20 20 20 20 73 65 74 50 65 6e 64 69 /. setPendi
dd570 6e 67 46 64 28 70 46 69 6c 65 29 3b 0a 20 20 20 ngFd(pFile);.
dd580 20 7d 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70 }. releaseOp
dd590 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 enCnt(pFile->pOp
dd5a0 65 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 en);. sqlite3
dd5b0 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 _free(pFile->loc
dd5c0 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 kingContext);.
dd5d0 20 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 closeUnixFile(
dd5e0 69 64 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 id);. unixLea
dd5f0 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 veMutex();. }.
dd600 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
dd610 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 K;.}..#endif /*
dd620 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f defined(__APPLE_
dd630 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 _) && SQLITE_ENA
dd640 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
dd650 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 E */./*.** The c
dd660 6f 64 65 20 61 62 6f 76 65 20 69 73 20 74 68 65 ode above is the
dd670 20 41 46 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d AFP lock implem
dd680 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 20 63 entation. The c
dd690 6f 64 65 20 69 73 20 73 70 65 63 69 66 69 63 0a ode is specific.
dd6a0 2a 2a 20 74 6f 20 4d 61 63 4f 53 58 20 61 6e 64 ** to MacOSX and
dd6b0 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f does not work o
dd6c0 6e 20 6f 74 68 65 72 20 75 6e 69 78 20 70 6c 61 n other unix pla
dd6d0 74 66 6f 72 6d 73 2e 20 20 4e 6f 20 61 6c 74 65 tforms. No alte
dd6e0 72 6e 61 74 69 76 65 0a 2a 2a 20 69 73 20 61 76 rnative.** is av
dd6f0 61 69 6c 61 62 6c 65 2e 20 20 49 66 20 79 6f 75 ailable. If you
dd700 20 64 6f 6e 27 74 20 63 6f 6d 70 69 6c 65 20 66 don't compile f
dd710 6f 72 20 61 20 6d 61 63 2c 20 74 68 65 6e 20 74 or a mac, then t
dd720 68 65 20 22 75 6e 69 78 2d 61 66 70 22 0a 2a 2a he "unix-afp".**
dd730 20 56 46 53 20 69 73 20 6e 6f 74 20 61 76 61 69 VFS is not avai
dd740 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a lable..**.******
dd750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
dd760 45 6e 64 20 6f 66 20 74 68 65 20 41 46 50 20 6c End of the AFP l
dd770 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ock implementati
dd780 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a on *************
dd790 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
dd7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd7e0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a ********/.../***
dd7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
dd840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 6e ************ Non
dd850 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 -locking sqlite3
dd860 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 2a 2a _file methods **
dd870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
dd890 2a 20 54 68 65 20 6e 65 78 74 20 64 69 76 69 73 * The next divis
dd8a0 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 ion contains imp
dd8b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 lementations for
dd8c0 20 61 6c 6c 20 6d 65 74 68 6f 64 73 20 6f 66 20 all methods of
dd8d0 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f the .** sqlite3_
dd8e0 66 69 6c 65 20 6f 62 6a 65 63 74 20 6f 74 68 65 file object othe
dd8f0 72 20 74 68 61 6e 20 74 68 65 20 6c 6f 63 6b 69 r than the locki
dd900 6e 67 20 6d 65 74 68 6f 64 73 2e 20 20 54 68 65 ng methods. The
dd910 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 6d 65 74 68 locking.** meth
dd920 6f 64 73 20 77 65 72 65 20 64 65 66 69 6e 65 64 ods were defined
dd930 20 69 6e 20 64 69 76 69 73 69 6f 6e 73 20 61 62 in divisions ab
dd940 6f 76 65 20 28 6f 6e 65 20 6c 6f 63 6b 69 6e 67 ove (one locking
dd950 20 6d 65 74 68 6f 64 20 70 65 72 0a 2a 2a 20 64 method per.** d
dd960 69 76 69 73 69 6f 6e 29 2e 20 20 54 68 6f 73 65 ivision). Those
dd970 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 61 72 methods that ar
dd980 65 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 e common to all
dd990 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 73 0a 2a 2a locking modes.**
dd9a0 20 61 72 65 20 67 61 74 68 65 72 20 74 6f 67 65 are gather toge
dd9b0 74 68 65 72 20 69 6e 74 6f 20 74 68 69 73 20 64 ther into this d
dd9c0 69 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a ivision..*/../*.
dd9d0 2a 2a 20 53 65 65 6b 20 74 6f 20 74 68 65 20 6f ** Seek to the o
dd9e0 66 66 73 65 74 20 70 61 73 73 65 64 20 61 73 20 ffset passed as
dd9f0 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
dda00 65 6e 74 2c 20 74 68 65 6e 20 72 65 61 64 20 63 ent, then read c
dda10 6e 74 20 0a 2a 2a 20 62 79 74 65 73 20 69 6e 74 nt .** bytes int
dda20 6f 20 70 42 75 66 2e 20 52 65 74 75 72 6e 20 74 o pBuf. Return t
dda30 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
dda40 65 73 20 61 63 74 75 61 6c 6c 79 20 72 65 61 64 es actually read
dda50 2e 0a 2a 2a 0a 2a 2a 20 4e 42 3a 20 20 49 66 20 ..**.** NB: If
dda60 79 6f 75 20 64 65 66 69 6e 65 20 55 53 45 5f 50 you define USE_P
dda70 52 45 41 44 20 6f 72 20 55 53 45 5f 50 52 45 41 READ or USE_PREA
dda80 44 36 34 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 D64, then it mig
dda90 68 74 20 61 6c 73 6f 0a 2a 2a 20 62 65 20 6e 65 ht also.** be ne
ddaa0 63 65 73 73 61 72 79 20 74 6f 20 64 65 66 69 6e cessary to defin
ddab0 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 e _XOPEN_SOURCE
ddac0 74 6f 20 62 65 20 35 30 30 2e 20 20 54 68 69 73 to be 500. This
ddad0 20 76 61 72 69 65 73 20 66 72 6f 6d 0a 2a 2a 20 varies from.**
ddae0 6f 6e 65 20 73 79 73 74 65 6d 20 74 6f 20 61 6e one system to an
ddaf0 6f 74 68 65 72 2e 20 20 53 69 6e 63 65 20 53 51 other. Since SQ
ddb00 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 64 65 Lite does not de
ddb10 66 69 6e 65 20 55 53 45 5f 50 52 45 41 44 0a 2a fine USE_PREAD.*
ddb20 2a 20 61 6e 79 20 61 6e 79 20 66 6f 72 6d 20 62 * any any form b
ddb30 79 20 64 65 66 61 75 6c 74 2c 20 77 65 20 77 69 y default, we wi
ddb40 6c 6c 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 ll not attempt t
ddb50 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f o define _XOPEN_
ddb60 53 4f 55 52 43 45 2e 0a 2a 2a 20 53 65 65 20 74 SOURCE..** See t
ddb70 69 63 6b 65 74 73 20 23 32 37 34 31 20 61 6e 64 ickets #2741 and
ddb80 20 23 32 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 54 6f #2681..**.** To
ddb90 20 61 76 6f 69 64 20 73 74 6f 6d 70 69 6e 67 20 avoid stomping
ddba0 74 68 65 20 65 72 72 6e 6f 20 76 61 6c 75 65 20 the errno value
ddbb0 6f 6e 20 61 20 66 61 69 6c 65 64 20 72 65 61 64 on a failed read
ddbc0 20 74 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 76 the lastErrno v
ddbd0 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 62 alue.** is set b
ddbe0 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e efore returning.
ddbf0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
ddc00 65 65 6b 41 6e 64 52 65 61 64 28 75 6e 69 78 46 eekAndRead(unixF
ddc10 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 ile *id, sqlite3
ddc20 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 2c 20 76 _int64 offset, v
ddc30 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63 oid *pBuf, int c
ddc40 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a nt){. int got;.
ddc50 20 20 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b i64 newOffset;
ddc60 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a . TIMER_START;.
ddc70 23 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f #if defined(USE_
ddc80 50 52 45 41 44 29 0a 20 20 67 6f 74 20 3d 20 70 PREAD). got = p
ddc90 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 read(id->h, pBuf
ddca0 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a , cnt, offset);.
ddcb0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
ddcc0 72 28 20 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 r( got = -1 );.#
ddcd0 65 6c 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 elif defined(USE
ddce0 5f 50 52 45 41 44 36 34 29 0a 20 20 67 6f 74 20 _PREAD64). got
ddcf0 3d 20 70 72 65 61 64 36 34 28 69 64 2d 3e 68 2c = pread64(id->h,
ddd00 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 pBuf, cnt, offs
ddd10 65 74 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 et);. SimulateI
ddd20 4f 45 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 OError( got = -1
ddd30 20 29 3b 0a 23 65 6c 73 65 0a 20 20 6e 65 77 4f );.#else. newO
ddd40 66 66 73 65 74 20 3d 20 6c 73 65 65 6b 28 69 64 ffset = lseek(id
ddd50 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 53 45 45 ->h, offset, SEE
ddd60 4b 5f 53 45 54 29 3b 0a 20 20 53 69 6d 75 6c 61 K_SET);. Simula
ddd70 74 65 49 4f 45 72 72 6f 72 28 20 6e 65 77 4f 66 teIOError( newOf
ddd80 66 73 65 74 2d 2d 20 29 3b 0a 20 20 69 66 28 20 fset-- );. if(
ddd90 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66 66 73 65 newOffset!=offse
ddda0 74 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 65 77 t ){. if( new
dddb0 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20 29 7b 0a Offset == -1 ){.
dddc0 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 ((unixFile
dddd0 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f *)id)->lastErrno
ddde0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 7d 65 = errno;. }e
dddf0 6c 73 65 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 lse{. ((uni
dde00 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 xFile*)id)->last
dde10 45 72 72 6e 6f 20 3d 20 30 3b 09 09 09 0a 20 20 Errno = 0;....
dde20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 2d }. return -
dde30 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20 3d 20 72 1;. }. got = r
dde40 65 61 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c ead(id->h, pBuf,
dde50 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 cnt);.#endif.
dde60 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 69 66 28 TIMER_END;. if(
dde70 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20 20 28 28 got<0 ){. ((
dde80 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c unixFile*)id)->l
dde90 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f astErrno = errno
ddea0 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 35 ;. }. OSTRACE5
ddeb0 28 22 52 45 41 44 20 20 20 20 25 2d 33 64 20 25 ("READ %-3d %
ddec0 35 64 20 25 37 6c 6c 64 20 25 6c 6c 75 5c 6e 22 5d %7lld %llu\n"
dded0 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 2c 20 6f 66 , id->h, got, of
ddee0 66 73 65 74 2c 20 54 49 4d 45 52 5f 45 4c 41 50 fset, TIMER_ELAP
ddef0 53 45 44 29 3b 0a 20 20 72 65 74 75 72 6e 20 67 SED);. return g
ddf00 6f 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 ot;.}../*.** Rea
ddf10 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 d data from a fi
ddf20 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 le into a buffer
ddf30 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 . Return SQLITE
ddf40 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 _OK if all.** by
ddf50 74 65 73 20 77 65 72 65 20 72 65 61 64 20 73 75 tes were read su
ddf60 63 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 ccessfully and S
ddf70 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 QLITE_IOERR if a
ddf80 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 nything goes.**
ddf90 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 wrong..*/.static
ddfa0 20 69 6e 74 20 75 6e 69 78 52 65 61 64 28 0a 20 int unixRead(.
ddfb0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 sqlite3_file *i
ddfc0 64 2c 20 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 d, . void *pBuf
ddfd0 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 , . int amt,.
ddfe0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 sqlite3_int64 of
ddff0 66 73 65 74 0a 29 7b 0a 20 20 75 6e 69 78 46 69 fset.){. unixFi
de000 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
de010 78 46 69 6c 65 20 2a 29 69 64 3b 0a 20 20 69 6e xFile *)id;. in
de020 74 20 67 6f 74 3b 0a 20 20 61 73 73 65 72 74 28 t got;. assert(
de030 20 69 64 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 id );.. /* If
de040 74 68 69 73 20 69 73 20 61 20 64 61 74 61 62 61 this is a databa
de050 73 65 20 66 69 6c 65 20 28 6e 6f 74 20 61 20 6a se file (not a j
de060 6f 75 72 6e 61 6c 2c 20 6d 61 73 74 65 72 2d 6a ournal, master-j
de070 6f 75 72 6e 61 6c 20 6f 72 20 74 65 6d 70 0a 20 ournal or temp.
de080 20 2a 2a 20 66 69 6c 65 29 2c 20 74 68 65 20 62 ** file), the b
de090 79 74 65 73 20 69 6e 20 74 68 65 20 6c 6f 63 6b ytes in the lock
de0a0 69 6e 67 20 72 61 6e 67 65 20 73 68 6f 75 6c 64 ing range should
de0b0 20 6e 65 76 65 72 20 62 65 20 72 65 61 64 20 6f never be read o
de0c0 72 20 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 r written. */.
de0d0 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 70 assert( pFile->p
de0e0 55 6e 75 73 65 64 3d 3d 30 0a 20 20 20 20 20 20 Unused==0.
de0f0 20 7c 7c 20 6f 66 66 73 65 74 3e 3d 50 45 4e 44 || offset>=PEND
de100 49 4e 47 5f 42 59 54 45 2b 35 31 32 0a 20 20 20 ING_BYTE+512.
de110 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 2b 61 6d || offset+am
de120 74 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 t<=PENDING_BYTE
de130 0a 20 20 29 3b 0a 0a 20 20 67 6f 74 20 3d 20 73 . );.. got = s
de140 65 65 6b 41 6e 64 52 65 61 64 28 70 46 69 6c 65 eekAndRead(pFile
de150 2c 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 , offset, pBuf,
de160 61 6d 74 29 3b 0a 20 20 69 66 28 20 67 6f 74 3d amt);. if( got=
de170 3d 61 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75 =amt ){. retu
de180 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
de190 7d 65 6c 73 65 20 69 66 28 20 67 6f 74 3c 30 20 }else if( got<0
de1a0 29 7b 0a 20 20 20 20 2f 2a 20 6c 61 73 74 45 72 ){. /* lastEr
de1b0 72 6e 6f 20 73 65 74 20 62 79 20 73 65 65 6b 41 rno set by seekA
de1c0 6e 64 52 65 61 64 20 2a 2f 0a 20 20 20 20 72 65 ndRead */. re
de1d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
de1e0 52 5f 52 45 41 44 3b 0a 20 20 7d 65 6c 73 65 7b R_READ;. }else{
de1f0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 . pFile->last
de200 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20 6e 6f Errno = 0; /* no
de210 74 20 61 20 73 79 73 74 65 6d 20 65 72 72 6f 72 t a system error
de220 20 2a 2f 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 */. /* Unrea
de230 64 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 62 d parts of the b
de240 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a 65 uffer must be ze
de250 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 ro-filled */.
de260 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a memset(&((char*
de270 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 )pBuf)[got], 0,
de280 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 amt-got);. re
de290 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
de2a0 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 R_SHORT_READ;.
de2b0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 6b 20 }.}../*.** Seek
de2c0 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 69 6e to the offset in
de2d0 20 69 64 2d 3e 6f 66 66 73 65 74 20 74 68 65 6e id->offset then
de2e0 20 72 65 61 64 20 63 6e 74 20 62 79 74 65 73 20 read cnt bytes
de2f0 69 6e 74 6f 20 70 42 75 66 2e 0a 2a 2a 20 52 65 into pBuf..** Re
de300 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
de310 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c of bytes actuall
de320 79 20 72 65 61 64 2e 20 20 55 70 64 61 74 65 20 y read. Update
de330 74 68 65 20 6f 66 66 73 65 74 2e 0a 2a 2a 0a 2a the offset..**.*
de340 2a 20 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d 70 * To avoid stomp
de350 69 6e 67 20 74 68 65 20 65 72 72 6e 6f 20 76 61 ing the errno va
de360 6c 75 65 20 6f 6e 20 61 20 66 61 69 6c 65 64 20 lue on a failed
de370 77 72 69 74 65 20 74 68 65 20 6c 61 73 74 45 72 write the lastEr
de380 72 6e 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 rno value.** is
de390 73 65 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 set before retur
de3a0 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ning..*/.static
de3b0 69 6e 74 20 73 65 65 6b 41 6e 64 57 72 69 74 65 int seekAndWrite
de3c0 28 75 6e 69 78 46 69 6c 65 20 2a 69 64 2c 20 69 (unixFile *id, i
de3d0 36 34 20 6f 66 66 73 65 74 2c 20 63 6f 6e 73 74 64 offset, const
de3e0 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 void *pBuf, int
de3f0 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f 74 cnt){. int got
de400 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66 73 65 ;. i64 newOffse
de410 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 54 t;. TIMER_START
de420 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55 53 ;.#if defined(US
de430 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74 20 3d E_PREAD). got =
de440 20 70 77 72 69 74 65 28 69 64 2d 3e 68 2c 20 70 pwrite(id->h, p
de450 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 Buf, cnt, offset
de460 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 );.#elif defined
de470 28 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 (USE_PREAD64).
de480 67 6f 74 20 3d 20 70 77 72 69 74 65 36 34 28 69 got = pwrite64(i
de490 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c d->h, pBuf, cnt,
de4a0 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c 73 65 0a offset);.#else.
de4b0 20 20 6e 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 newOffset = ls
de4c0 65 65 6b 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 eek(id->h, offse
de4d0 74 2c 20 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 t, SEEK_SET);.
de4e0 69 66 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f if( newOffset!=o
de4f0 66 66 73 65 74 20 29 7b 0a 20 20 20 20 69 66 28 ffset ){. if(
de500 20 6e 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 newOffset == -1
de510 20 29 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 ){. ((unix
de520 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 File*)id)->lastE
de530 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
de540 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 }else{. (
de550 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e (unixFile*)id)->
de560 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09 09 lastErrno = 0;..
de570 09 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 .. }. retu
de580 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 rn -1;. }. got
de590 20 3d 20 77 72 69 74 65 28 69 64 2d 3e 68 2c 20 = write(id->h,
de5a0 70 42 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 pBuf, cnt);.#end
de5b0 69 66 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a if. TIMER_END;.
de5c0 20 20 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 if( got<0 ){.
de5d0 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 ((unixFile*)i
de5e0 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 d)->lastErrno =
de5f0 65 72 72 6e 6f 3b 0a 20 20 7d 0a 0a 20 20 4f 53 errno;. }.. OS
de600 54 52 41 43 45 35 28 22 57 52 49 54 45 20 20 20 TRACE5("WRITE
de610 25 2d 33 64 20 25 35 64 20 25 37 6c 6c 64 20 25 %-3d %5d %7lld %
de620 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 llu\n", id->h, g
de630 6f 74 2c 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 ot, offset, TIME
de640 52 5f 45 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 R_ELAPSED);. re
de650 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a 2f 2a turn got;.}.../*
de660 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 .** Write data f
de670 72 6f 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 rom a buffer int
de680 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 o a file. Retur
de690 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 n SQLITE_OK on s
de6a0 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d uccess.** or som
de6b0 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f e other error co
de6c0 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a de on failure..*
de6d0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
de6e0 78 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 xWrite(. sqlite
de6f0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 63 3_file *id, . c
de700 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c onst void *pBuf,
de710 20 0a 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 . int amt,. s
de720 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 qlite3_int64 off
de730 73 65 74 20 0a 29 7b 0a 20 20 75 6e 69 78 46 69 set .){. unixFi
de740 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
de750 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 xFile*)id;. int
de760 20 77 72 6f 74 65 20 3d 20 30 3b 0a 20 20 61 73 wrote = 0;. as
de770 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 61 73 sert( id );. as
de780 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 0a sert( amt>0 );..
de790 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
de7a0 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 a database file
de7b0 28 6e 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 2c 20 (not a journal,
de7c0 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6f master-journal o
de7d0 72 20 74 65 6d 70 0a 20 20 2a 2a 20 66 69 6c 65 r temp. ** file
de7e0 29 2c 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 ), the bytes in
de7f0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 the locking rang
de800 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 e should never b
de810 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 e read or writte
de820 6e 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 n. */. assert(
de830 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64 3d 3d pFile->pUnused==
de840 30 0a 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 0. || offs
de850 65 74 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 et>=PENDING_BYTE
de860 2b 35 31 32 0a 20 20 20 20 20 20 20 7c 7c 20 6f +512. || o
de870 66 66 73 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 49 ffset+amt<=PENDI
de880 4e 47 5f 42 59 54 45 20 0a 20 20 29 3b 0a 0a 23 NG_BYTE . );..#
de890 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 ifndef NDEBUG.
de8a0 2f 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 /* If we are doi
de8b0 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 ng a normal writ
de8c0 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 e to a database
de8d0 66 69 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64 file (as opposed
de8e0 20 74 6f 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 to. ** doing a
de8f0 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c hot-journal rol
de900 6c 62 61 63 6b 20 6f 72 20 61 20 77 72 69 74 65 lback or a write
de910 20 74 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74 to some file ot
de920 68 65 72 20 74 68 61 6e 20 61 0a 20 20 2a 2a 20 her than a. **
de930 6e 6f 72 6d 61 6c 20 64 61 74 61 62 61 73 65 20 normal database
de940 66 69 6c 65 29 20 74 68 65 6e 20 72 65 63 6f 72 file) then recor
de950 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20 d the fact that
de960 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a the database. *
de970 2a 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20 20 * has changed.
de980 49 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 If the transacti
de990 6f 6e 20 63 6f 75 6e 74 65 72 20 69 73 20 6d 6f on counter is mo
de9a0 64 69 66 69 65 64 2c 20 72 65 63 6f 72 64 20 74 dified, record t
de9b0 68 61 74 0a 20 20 2a 2a 20 66 61 63 74 20 74 6f hat. ** fact to
de9c0 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 o.. */. if( pF
de9d0 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 ile->inNormalWri
de9e0 74 65 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d te ){. pFile-
de9f0 3e 64 62 55 70 64 61 74 65 20 3d 20 31 3b 20 20 >dbUpdate = 1;
dea00 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
dea10 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 has been modifie
dea20 64 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f 66 66 d */. if( off
dea30 73 65 74 3c 3d 32 34 20 26 26 20 6f 66 66 73 65 set<=24 && offse
dea40 74 2b 61 6d 74 3e 3d 32 37 20 29 7b 0a 20 20 20 t+amt>=27 ){.
dea50 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 int rc;.
dea60 20 63 68 61 72 20 6f 6c 64 43 6e 74 72 5b 34 5d char oldCntr[4]
dea70 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 ;. Simulate
dea80 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 IOErrorBenign(1)
dea90 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 65 65 ;. rc = see
deaa0 6b 41 6e 64 52 65 61 64 28 70 46 69 6c 65 2c 20 kAndRead(pFile,
deab0 32 34 2c 20 6f 6c 64 43 6e 74 72 2c 20 34 29 3b 24, oldCntr, 4);
deac0 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 . SimulateI
dead0 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b OErrorBenign(0);
deae0 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 34 . if( rc!=4
deaf0 20 7c 7c 20 6d 65 6d 63 6d 70 28 6f 6c 64 43 6e || memcmp(oldCn
deb00 74 72 2c 20 26 28 28 63 68 61 72 2a 29 70 42 75 tr, &((char*)pBu
deb10 66 29 5b 32 34 2d 6f 66 66 73 65 74 5d 2c 20 34 f)[24-offset], 4
deb20 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )!=0 ){.
deb30 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 pFile->transCntr
deb40 43 68 6e 67 20 3d 20 31 3b 20 20 2f 2a 20 54 68 Chng = 1; /* Th
deb50 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f e transaction co
deb60 75 6e 74 65 72 20 68 61 73 20 63 68 61 6e 67 65 unter has change
deb70 64 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 d */. }.
deb80 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 }. }.#endif..
deb90 20 77 68 69 6c 65 28 20 61 6d 74 3e 30 20 26 26 while( amt>0 &&
deba0 20 28 77 72 6f 74 65 20 3d 20 73 65 65 6b 41 6e (wrote = seekAn
debb0 64 57 72 69 74 65 28 70 46 69 6c 65 2c 20 6f 66 dWrite(pFile, of
debc0 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d 74 29 fset, pBuf, amt)
debd0 29 3e 30 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d )>0 ){. amt -
debe0 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 6f 66 66 = wrote;. off
debf0 73 65 74 20 2b 3d 20 77 72 6f 74 65 3b 0a 20 20 set += wrote;.
dec00 20 20 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 pBuf = &((char
dec10 2a 29 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a *)pBuf)[wrote];.
dec20 20 20 7d 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f }. SimulateIO
dec30 45 72 72 6f 72 28 28 20 77 72 6f 74 65 3d 28 2d Error(( wrote=(-
dec40 31 29 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20 1), amt=1 ));.
dec50 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c SimulateDiskfull
dec60 45 72 72 6f 72 28 28 20 77 72 6f 74 65 3d 30 2c Error(( wrote=0,
dec70 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20 69 66 28 amt=1 ));. if(
dec80 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 69 66 amt>0 ){. if
dec90 28 20 77 72 6f 74 65 3c 30 20 29 7b 0a 20 20 20 ( wrote<0 ){.
deca0 20 20 20 2f 2a 20 6c 61 73 74 45 72 72 6e 6f 20 /* lastErrno
decb0 73 65 74 20 62 79 20 73 65 65 6b 41 6e 64 57 72 set by seekAndWr
decc0 69 74 65 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 ite */. ret
decd0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
dece0 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 _WRITE;. }els
decf0 65 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e e{. pFile->
ded00 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f lastErrno = 0; /
ded10 2a 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65 * not a system e
ded20 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 rror */. re
ded30 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c turn SQLITE_FULL
ded40 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
ded50 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
ded60 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }..#ifdef SQLITE
ded70 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e _TEST./*.** Coun
ded80 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 t the number of
ded90 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f fullsyncs and no
deda0 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 rmal syncs. Thi
dedb0 73 20 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 s is used to tes
dedc0 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 t.** that syncs
dedd0 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 and fullsyncs ar
dede0 65 20 6f 63 63 75 72 72 69 6e 67 20 61 74 20 74 e occurring at t
dedf0 68 65 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a he right times..
dee00 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
dee10 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 t sqlite3_sync_c
dee20 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 ount = 0;.SQLITE
dee30 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
dee40 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 _fullsync_count
dee50 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a = 0;.#endif../*.
dee60 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 74 72 75 ** We do not tru
dee70 73 74 20 73 79 73 74 65 6d 73 20 74 6f 20 70 72 st systems to pr
dee80 6f 76 69 64 65 20 61 20 77 6f 72 6b 69 6e 67 20 ovide a working
dee90 66 64 61 74 61 73 79 6e 63 28 29 2e 20 20 53 6f fdatasync(). So
deea0 6d 65 20 64 6f 2e 0a 2a 2a 20 4f 74 68 65 72 73 me do..** Others
deeb0 20 64 6f 20 6e 6f 2e 20 20 54 6f 20 62 65 20 73 do no. To be s
deec0 61 66 65 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 afe, we will sti
deed0 63 6b 20 77 69 74 68 20 74 68 65 20 28 73 6c 6f ck with the (slo
deee0 77 65 72 29 20 66 73 79 6e 63 28 29 2e 0a 2a 2a wer) fsync()..**
deef0 20 49 66 20 79 6f 75 20 6b 6e 6f 77 20 74 68 61 If you know tha
def00 74 20 79 6f 75 72 20 73 79 73 74 65 6d 20 64 6f t your system do
def10 65 73 20 73 75 70 70 6f 72 74 20 66 64 61 74 61 es support fdata
def20 73 79 6e 63 28 29 20 63 6f 72 72 65 63 74 6c 79 sync() correctly
def30 2c 0a 2a 2a 20 74 68 65 6e 20 73 69 6d 70 6c 79 ,.** then simply
def40 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 2d 44 compile with -D
def50 66 64 61 74 61 73 79 6e 63 3d 66 64 61 74 61 73 fdatasync=fdatas
def60 79 6e 63 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 ync.*/.#if !defi
def70 6e 65 64 28 66 64 61 74 61 73 79 6e 63 29 20 26 ned(fdatasync) &
def80 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e & !defined(__lin
def90 75 78 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 66 ux__).# define f
defa0 64 61 74 61 73 79 6e 63 20 66 73 79 6e 63 0a 23 datasync fsync.#
defb0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 endif../*.** Def
defc0 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 ine HAVE_FULLFSY
defd0 4e 43 20 74 6f 20 30 20 6f 72 20 31 20 64 65 70 NC to 0 or 1 dep
defe0 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 ending on whethe
deff0 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 r or not.** the
df000 46 5f 46 55 4c 4c 46 53 59 4e 43 20 6d 61 63 72 F_FULLFSYNC macr
df010 6f 20 69 73 20 64 65 66 69 6e 65 64 2e 20 20 46 o is defined. F
df020 5f 46 55 4c 4c 46 53 59 4e 43 20 69 73 20 63 75 _FULLFSYNC is cu
df030 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c 79 20 rrently.** only
df040 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 available on Mac
df050 20 4f 53 20 58 2e 20 20 42 75 74 20 74 68 61 74 OS X. But that
df060 20 63 6f 75 6c 64 20 63 68 61 6e 67 65 2e 0a 2a could change..*
df070 2f 0a 23 69 66 64 65 66 20 46 5f 46 55 4c 4c 46 /.#ifdef F_FULLF
df080 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 48 41 SYNC.# define HA
df090 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 31 0a 23 VE_FULLFSYNC 1.#
df0a0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 48 41 else.# define HA
df0b0 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 30 0a 23 VE_FULLFSYNC 0.#
df0c0 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 endif.../*.** Th
df0d0 65 20 66 73 79 6e 63 28 29 20 73 79 73 74 65 6d e fsync() system
df0e0 20 63 61 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 77 call does not w
df0f0 6f 72 6b 20 61 73 20 61 64 76 65 72 74 69 73 65 ork as advertise
df100 64 20 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75 6e 69 d on many.** uni
df110 78 20 73 79 73 74 65 6d 73 2e 20 20 54 68 65 20 x systems. The
df120 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 63 65 64 following proced
df130 75 72 65 20 69 73 20 61 6e 20 61 74 74 65 6d 70 ure is an attemp
df140 74 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 t to make.** it
df150 77 6f 72 6b 20 62 65 74 74 65 72 2e 0a 2a 2a 0a work better..**.
df160 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4e 4f ** The SQLITE_NO
df170 5f 53 59 4e 43 20 6d 61 63 72 6f 20 64 69 73 61 _SYNC macro disa
df180 62 6c 65 73 20 61 6c 6c 20 66 73 79 6e 63 28 29 bles all fsync()
df190 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 66 s. This is usef
df1a0 75 6c 0a 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e ul.** for testin
df1b0 67 20 77 68 65 6e 20 77 65 20 77 61 6e 74 20 74 g when we want t
df1c0 6f 20 72 75 6e 20 74 68 72 6f 75 67 68 20 74 68 o run through th
df1d0 65 20 74 65 73 74 20 73 75 69 74 65 20 71 75 69 e test suite qui
df1e0 63 6b 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61 72 65 ckly..** You are
df1f0 20 73 74 72 6f 6e 67 6c 79 20 61 64 76 69 73 65 strongly advise
df200 64 20 2a 6e 6f 74 2a 20 74 6f 20 64 65 70 6c 6f d *not* to deplo
df210 79 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f y with SQLITE_NO
df220 5f 53 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c 65 64 _SYNC.** enabled
df230 2c 20 68 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 , however, since
df240 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f with SQLITE_NO_
df250 53 59 4e 43 20 65 6e 61 62 6c 65 64 2c 20 61 6e SYNC enabled, an
df260 20 4f 53 20 63 72 61 73 68 0a 2a 2a 20 6f 72 20 OS crash.** or
df270 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 69 power failure wi
df280 6c 6c 20 6c 69 6b 65 6c 79 20 63 6f 72 72 75 70 ll likely corrup
df290 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 t the database f
df2a0 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 ile..**.** SQLit
df2b0 65 20 73 65 74 73 20 74 68 65 20 64 61 74 61 4f e sets the dataO
df2c0 6e 6c 79 20 66 6c 61 67 20 69 66 20 74 68 65 20 nly flag if the
df2d0 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 size of the file
df2e0 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a is unchanged..*
df2f0 2a 20 54 68 65 20 69 64 65 61 20 62 65 68 69 6e * The idea behin
df300 64 20 64 61 74 61 4f 6e 6c 79 20 69 73 20 74 68 d dataOnly is th
df310 61 74 20 69 74 20 73 68 6f 75 6c 64 20 6f 6e 6c at it should onl
df320 79 20 77 72 69 74 65 20 74 68 65 20 66 69 6c 65 y write the file
df330 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 74 6f 20 64 content.** to d
df340 69 73 6b 2c 20 6e 6f 74 20 74 68 65 20 69 6e 6f isk, not the ino
df350 64 65 2e 20 20 57 65 20 6f 6e 6c 79 20 73 65 74 de. We only set
df360 20 64 61 74 61 4f 6e 6c 79 20 69 66 20 74 68 65 dataOnly if the
df370 20 66 69 6c 65 20 73 69 7a 65 20 69 73 20 0a 2a file size is .*
df380 2a 20 75 6e 63 68 61 6e 67 65 64 20 73 69 6e 63 * unchanged sinc
df390 65 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 e the file size
df3a0 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 is part of the i
df3b0 6e 6f 64 65 2e 20 20 48 6f 77 65 76 65 72 2c 20 node. However,
df3c0 0a 2a 2a 20 54 65 64 20 54 73 27 6f 20 74 65 6c .** Ted Ts'o tel
df3d0 6c 73 20 75 73 20 74 68 61 74 20 66 64 61 74 61 ls us that fdata
df3e0 73 79 6e 63 28 29 20 77 69 6c 6c 20 61 6c 73 6f sync() will also
df3f0 20 77 72 69 74 65 20 74 68 65 20 69 6e 6f 64 65 write the inode
df400 20 69 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 if the.** file
df410 73 69 7a 65 20 68 61 73 20 63 68 61 6e 67 65 64 size has changed
df420 2e 20 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 6c . The only real
df430 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 difference betw
df440 65 65 6e 20 66 64 61 74 61 73 79 6e 63 28 29 0a een fdatasync().
df450 2a 2a 20 61 6e 64 20 66 73 79 6e 63 28 29 2c 20 ** and fsync(),
df460 54 65 64 20 74 65 6c 6c 73 20 75 73 2c 20 69 73 Ted tells us, is
df470 20 74 68 61 74 20 66 64 61 74 61 73 79 6e 63 28 that fdatasync(
df480 29 20 77 69 6c 6c 20 6e 6f 74 20 66 6c 75 73 68 ) will not flush
df490 20 74 68 65 0a 2a 2a 20 69 6e 6f 64 65 20 69 66 the.** inode if
df4a0 20 74 68 65 20 6d 74 69 6d 65 20 6f 72 20 6f 77 the mtime or ow
df4b0 6e 65 72 20 6f 72 20 6f 74 68 65 72 20 69 6e 6f ner or other ino
df4c0 64 65 20 61 74 74 72 69 62 75 74 65 73 20 68 61 de attributes ha
df4d0 76 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 57 ve changed..** W
df4e0 65 20 6f 6e 6c 79 20 63 61 72 65 20 61 62 6f 75 e only care abou
df4f0 74 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2c t the file size,
df500 20 6e 6f 74 20 74 68 65 20 6f 74 68 65 72 20 66 not the other f
df510 69 6c 65 20 61 74 74 72 69 62 75 74 65 73 2c 20 ile attributes,
df520 73 6f 0a 2a 2a 20 61 73 20 66 61 72 20 61 73 20 so.** as far as
df530 53 51 4c 69 74 65 20 69 73 20 63 6f 6e 63 65 72 SQLite is concer
df540 6e 65 64 2c 20 61 6e 20 66 64 61 74 61 73 79 6e ned, an fdatasyn
df550 63 28 29 20 69 73 20 61 6c 77 61 79 73 20 61 64 c() is always ad
df560 65 71 75 61 74 65 2e 0a 2a 2a 20 53 6f 2c 20 77 equate..** So, w
df570 65 20 61 6c 77 61 79 73 20 75 73 65 20 66 64 61 e always use fda
df580 74 61 73 79 6e 63 28 29 20 69 66 20 69 74 20 69 tasync() if it i
df590 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 72 65 67 s available, reg
df5a0 61 72 64 6c 65 73 73 20 6f 66 0a 2a 2a 20 74 68 ardless of.** th
df5b0 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 e value of the d
df5c0 61 74 61 4f 6e 6c 79 20 66 6c 61 67 2e 0a 2a 2f ataOnly flag..*/
df5d0 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c .static int full
df5e0 5f 66 73 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 _fsync(int fd, i
df5f0 6e 74 20 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 nt fullSync, int
df600 20 64 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e dataOnly){. in
df610 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 t rc;.. /* The
df620 66 6f 6c 6c 6f 77 69 6e 67 20 22 69 66 64 65 66 following "ifdef
df630 2f 65 6c 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f /elif/else/" blo
df640 63 6b 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 ck has the same
df650 73 74 72 75 63 74 75 72 65 20 61 73 0a 20 20 2a structure as. *
df660 2a 20 74 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e * the one below.
df670 20 49 74 20 69 73 20 72 65 70 6c 69 63 61 74 65 It is replicate
df680 64 20 68 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f d here solely to
df690 20 61 76 6f 69 64 20 63 6c 75 74 74 65 72 69 6e avoid clutterin
df6a0 67 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72 g . ** up the r
df6b0 65 61 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68 eal code with th
df6c0 65 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 e UNUSED_PARAMET
df6d0 45 52 28 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a ER() macros.. *
df6e0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
df6f0 4e 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 NO_SYNC. UNUSED
df700 5f 50 41 52 41 4d 45 54 45 52 28 66 64 29 3b 0a _PARAMETER(fd);.
df710 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
df720 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 ER(fullSync);.
df730 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
df740 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69 (dataOnly);.#eli
df750 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 f HAVE_FULLFSYNC
df760 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
df770 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 TER(dataOnly);.#
df780 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 else. UNUSED_PA
df790 52 41 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 RAMETER(fullSync
df7a0 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
df7b0 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b METER(dataOnly);
df7c0 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 .#endif.. /* Re
df7d0 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20 cord the number
df7e0 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 77 65 of times that we
df7f0 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79 do a normal fsy
df800 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46 nc() and . ** F
df810 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69 73 20 69 ULLSYNC. This i
df820 73 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 s used during te
df830 73 74 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20 sting to verify
df840 74 68 61 74 20 74 68 69 73 20 70 72 6f 63 65 64 that this proced
df850 75 72 65 0a 20 20 2a 2a 20 67 65 74 73 20 63 61 ure. ** gets ca
df860 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 6f lled with the co
df870 72 72 65 63 74 20 61 72 67 75 6d 65 6e 74 73 2e rrect arguments.
df880 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c . */.#ifdef SQL
df890 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 ITE_TEST. if( f
df8a0 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65 ullSync ) sqlite
df8b0 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 3_fullsync_count
df8c0 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 ++;. sqlite3_sy
df8d0 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 nc_count++;.#end
df8e0 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 if.. /* If we c
df8f0 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 ompiled with the
df900 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 SQLITE_NO_SYNC
df910 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 flag, then synci
df920 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d ng is a. ** no-
df930 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 op. */.#ifdef S
df940 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 QLITE_NO_SYNC.
df950 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
df960 23 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 #elif HAVE_FULLF
df970 53 59 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53 SYNC. if( fullS
df980 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ync ){. rc =
df990 66 63 6e 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c fcntl(fd, F_FULL
df9a0 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c FSYNC, 0);. }el
df9b0 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a se{. rc = 1;.
df9c0 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }. /* If the
df9d0 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65 64 FULLFSYNC failed
df9e0 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61 , fall back to a
df9f0 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 66 73 79 ttempting an fsy
dfa00 6e 63 28 29 2e 0a 20 20 2a 2a 20 49 74 20 73 68 nc().. ** It sh
dfa10 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69 ouldn't be possi
dfa20 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e ble for fullfsyn
dfa30 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65 c to fail on the
dfa40 20 6c 6f 63 61 6c 20 0a 20 20 2a 2a 20 66 69 6c local . ** fil
dfa50 65 20 73 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58 e system (on OSX
dfa60 29 2c 20 73 6f 20 66 61 69 6c 75 72 65 20 69 6e ), so failure in
dfa70 64 69 63 61 74 65 73 20 74 68 61 74 20 46 55 4c dicates that FUL
dfa80 4c 46 53 59 4e 43 0a 20 20 2a 2a 20 69 73 6e 27 LFSYNC. ** isn'
dfa90 74 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 t supported for
dfaa0 74 68 69 73 20 66 69 6c 65 20 73 79 73 74 65 6d this file system
dfab0 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e . So, attempt an
dfac0 20 66 73 79 6e 63 20 0a 20 20 2a 2a 20 61 6e 64 fsync . ** and
dfad0 20 28 66 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72 (for now) ignor
dfae0 65 20 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f e the overhead o
dfaf0 66 20 61 20 73 75 70 65 72 66 6c 75 6f 75 73 20 f a superfluous
dfb00 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20 fcntl call. .
dfb10 2a 2a 20 49 74 27 64 20 62 65 20 62 65 74 74 65 ** It'd be bette
dfb20 72 20 74 6f 20 64 65 74 65 63 74 20 66 75 6c 6c r to detect full
dfb30 66 73 79 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e fsync support on
dfb40 63 65 20 61 6e 64 20 61 76 6f 69 64 20 0a 20 20 ce and avoid .
dfb50 2a 2a 20 74 68 65 20 66 63 6e 74 6c 20 63 61 6c ** the fcntl cal
dfb60 6c 20 65 76 65 72 79 20 74 69 6d 65 20 73 79 6e l every time syn
dfb70 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a c is called.. *
dfb80 2f 0a 20 20 69 66 28 20 72 63 20 29 20 72 63 20 /. if( rc ) rc
dfb90 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 0a 23 65 = fsync(fd);..#e
dfba0 6c 73 65 20 0a 20 20 72 63 20 3d 20 66 64 61 74 lse . rc = fdat
dfbb0 61 73 79 6e 63 28 66 64 29 3b 0a 23 69 66 20 4f async(fd);.#if O
dfbc0 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 S_VXWORKS. if(
dfbd0 72 63 3d 3d 2d 31 20 26 26 20 65 72 72 6e 6f 3d rc==-1 && errno=
dfbe0 3d 45 4e 4f 54 53 55 50 20 29 7b 0a 20 20 20 20 =ENOTSUP ){.
dfbf0 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a rc = fsync(fd);.
dfc00 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 }.#endif /* OS
dfc10 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 23 65 6e 64 _VXWORKS */.#end
dfc20 69 66 20 2f 2a 20 69 66 64 65 66 20 53 51 4c 49 if /* ifdef SQLI
dfc30 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6c 69 66 20 TE_NO_SYNC elif
dfc40 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 2a HAVE_FULLFSYNC *
dfc50 2f 0a 0a 20 20 69 66 28 20 4f 53 5f 56 58 57 4f /.. if( OS_VXWO
dfc60 52 4b 53 20 26 26 20 72 63 21 3d 20 2d 31 20 29 RKS && rc!= -1 )
dfc70 7b 0a 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 {. rc = 0;.
dfc80 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
dfc90 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 ../*.** Make sur
dfca0 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 e all writes to
dfcb0 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c a particular fil
dfcc0 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 e are committed
dfcd0 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 49 to disk..**.** I
dfce0 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30 20 74 68 f dataOnly==0 th
dfcf0 65 6e 20 62 6f 74 68 20 74 68 65 20 66 69 6c 65 en both the file
dfd00 20 69 74 73 65 6c 66 20 61 6e 64 20 69 74 73 20 itself and its
dfd10 6d 65 74 61 64 61 74 61 20 28 66 69 6c 65 0a 2a metadata (file.*
dfd20 2a 20 73 69 7a 65 2c 20 61 63 63 65 73 73 20 74 * size, access t
dfd30 69 6d 65 2c 20 65 74 63 29 20 61 72 65 20 73 79 ime, etc) are sy
dfd40 6e 63 65 64 2e 20 20 49 66 20 64 61 74 61 4f 6e nced. If dataOn
dfd50 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e 6c 79 20 ly!=0 then only
dfd60 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 61 74 61 the.** file data
dfd70 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a is synced..**.*
dfd80 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c 20 61 6c * Under Unix, al
dfd90 73 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 so make sure tha
dfda0 74 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 t the directory
dfdb0 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 entry for the fi
dfdc0 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 le.** has been c
dfdd0 72 65 61 74 65 64 20 62 79 20 66 73 79 6e 63 2d reated by fsync-
dfde0 69 6e 67 20 74 68 65 20 64 69 72 65 63 74 6f 72 ing the director
dfdf0 79 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 y that contains
dfe00 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 66 20 the file..** If
dfe10 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 69 we do not do thi
dfe20 73 20 61 6e 64 20 77 65 20 65 6e 63 6f 75 6e 74 s and we encount
dfe30 65 72 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 er a power failu
dfe40 72 65 2c 20 74 68 65 20 64 69 72 65 63 74 6f 72 re, the director
dfe50 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 y.** entry for t
dfe60 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 he journal might
dfe70 20 6e 6f 74 20 65 78 69 73 74 20 61 66 74 65 72 not exist after
dfe80 20 77 65 20 72 65 62 6f 6f 74 2e 20 20 54 68 65 we reboot. The
dfe90 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69 74 65 20 next.** SQLite
dfea0 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 66 69 to access the fi
dfeb0 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e 6f 77 le will not know
dfec0 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 that the journa
dfed0 6c 20 65 78 69 73 74 73 20 28 62 65 63 61 75 73 l exists (becaus
dfee0 65 0a 2a 2a 20 74 68 65 20 64 69 72 65 63 74 6f e.** the directo
dfef0 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 ry entry for the
dff00 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6e 65 76 journal was nev
dff10 65 72 20 63 72 65 61 74 65 64 29 20 61 6e 64 20 er created) and
dff20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a the transaction.
dff30 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c 6c ** will not roll
dff40 20 62 61 63 6b 20 2d 20 70 6f 73 73 69 62 6c 79 back - possibly
dff50 20 6c 65 61 64 69 6e 67 20 74 6f 20 64 61 74 61 leading to data
dff60 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e base corruption.
dff70 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
dff80 6e 69 78 53 79 6e 63 28 73 71 6c 69 74 65 33 5f nixSync(sqlite3_
dff90 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c file *id, int fl
dffa0 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a ags){. int rc;.
dffb0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
dffc0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
dffd0 64 3b 0a 0a 20 20 69 6e 74 20 69 73 44 61 74 61 d;.. int isData
dffe0 4f 6e 6c 79 20 3d 20 28 66 6c 61 67 73 26 53 51 Only = (flags&SQ
dfff0 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e LITE_SYNC_DATAON
e0000 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 46 75 6c LY);. int isFul
e0010 6c 73 79 6e 63 20 3d 20 28 66 6c 61 67 73 26 30 lsync = (flags&0
e0020 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e x0F)==SQLITE_SYN
e0030 43 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 43 68 C_FULL;.. /* Ch
e0040 65 63 6b 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 eck that one of
e0050 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d SQLITE_SYNC_NORM
e0060 41 4c 20 6f 72 20 46 55 4c 4c 20 77 61 73 20 70 AL or FULL was p
e0070 61 73 73 65 64 20 2a 2f 0a 20 20 61 73 73 65 72 assed */. asser
e0080 74 28 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d t((flags&0x0F)==
e0090 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d SQLITE_SYNC_NORM
e00a0 41 4c 0a 20 20 20 20 20 20 7c 7c 20 28 66 6c 61 AL. || (fla
e00b0 67 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 gs&0x0F)==SQLITE
e00c0 5f 53 59 4e 43 5f 46 55 4c 4c 0a 20 20 29 3b 0a _SYNC_FULL. );.
e00d0 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61 6e 6e 6f . /* Unix canno
e00e0 74 2c 20 62 75 74 20 73 6f 6d 65 20 73 79 73 74 t, but some syst
e00f0 65 6d 73 20 6d 61 79 20 72 65 74 75 72 6e 20 53 ems may return S
e0100 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72 6f 6d 20 QLITE_FULL from
e0110 68 65 72 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20 here. This. **
e0120 6c 69 6e 65 20 69 73 20 74 6f 20 74 65 73 74 20 line is to test
e0130 74 68 61 74 20 64 6f 69 6e 67 20 73 6f 20 64 6f that doing so do
e0140 65 73 20 6e 6f 74 20 63 61 75 73 65 20 61 6e 79 es not cause any
e0150 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a 2f 0a problems.. */.
e0160 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 SimulateDiskfu
e0170 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 llError( return
e0180 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 0a SQLITE_FULL );..
e0190 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
e01a0 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 53 );. OSTRACE2("S
e01b0 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e 22 2c 20 YNC %-3d\n",
e01c0 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 72 63 20 pFile->h);. rc
e01d0 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46 69 = full_fsync(pFi
e01e0 6c 65 2d 3e 68 2c 20 69 73 46 75 6c 6c 73 79 6e le->h, isFullsyn
e01f0 63 2c 20 69 73 44 61 74 61 4f 6e 6c 79 29 3b 0a c, isDataOnly);.
e0200 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
e0210 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 r( rc=1 );. if(
e0220 20 72 63 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 rc ){. pFile
e0230 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
e0240 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 rno;. return
e0250 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 SQLITE_IOERR_FSY
e0260 4e 43 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 NC;. }. if( pF
e0270 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b ile->dirfd>=0 ){
e0280 0a 20 20 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 . int err;.
e0290 20 20 4f 53 54 52 41 43 45 34 28 22 44 49 52 53 OSTRACE4("DIRS
e02a0 59 4e 43 20 25 2d 33 64 20 28 68 61 76 65 5f 66 YNC %-3d (have_f
e02b0 75 6c 6c 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c ullfsync=%d full
e02c0 73 79 6e 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69 sync=%d)\n", pFi
e02d0 6c 65 2d 3e 64 69 72 66 64 2c 0a 20 20 20 20 20 le->dirfd,.
e02e0 20 20 20 20 20 20 20 48 41 56 45 5f 46 55 4c 4c HAVE_FULL
e02f0 46 53 59 4e 43 2c 20 69 73 46 75 6c 6c 73 79 6e FSYNC, isFullsyn
e0300 63 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 c);.#ifndef SQLI
e0310 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 TE_DISABLE_DIRSY
e0320 4e 43 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 69 NC. /* The di
e0330 72 65 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 rectory sync is
e0340 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69 only attempted i
e0350 66 20 66 75 6c 6c 5f 66 73 79 6e 63 20 69 73 0a f full_fsync is.
e0360 20 20 20 20 2a 2a 20 74 75 72 6e 65 64 20 6f 66 ** turned of
e0370 66 20 6f 72 20 75 6e 61 76 61 69 6c 61 62 6c 65 f or unavailable
e0380 2e 20 20 49 66 20 61 20 66 75 6c 6c 5f 66 73 79 . If a full_fsy
e0390 6e 63 20 6f 63 63 75 72 72 65 64 20 61 62 6f 76 nc occurred abov
e03a0 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 e,. ** then t
e03b0 68 65 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e he directory syn
e03c0 63 20 69 73 20 73 75 70 65 72 66 6c 75 6f 75 73 c is superfluous
e03d0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
e03e0 20 28 21 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e (!HAVE_FULLFSYN
e03f0 43 20 7c 7c 20 21 69 73 46 75 6c 6c 73 79 6e 63 C || !isFullsync
e0400 29 20 26 26 20 66 75 6c 6c 5f 66 73 79 6e 63 28 ) && full_fsync(
e0410 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c 30 2c 30 pFile->dirfd,0,0
e0420 29 20 29 7b 0a 20 20 20 20 20 20 20 2f 2a 0a 20 ) ){. /*.
e0430 20 20 20 20 20 20 2a 2a 20 57 65 20 68 61 76 65 ** We have
e0440 20 72 65 63 65 69 76 65 64 20 6d 75 6c 74 69 70 received multip
e0450 6c 65 20 72 65 70 6f 72 74 73 20 6f 66 20 66 73 le reports of fs
e0460 79 6e 63 28 29 20 72 65 74 75 72 6e 69 6e 67 0a ync() returning.
e0470 20 20 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 73 ** errors
e0480 20 77 68 65 6e 20 61 70 70 6c 69 65 64 20 74 6f when applied to
e0490 20 64 69 72 65 63 74 6f 72 69 65 73 20 6f 6e 20 directories on
e04a0 63 65 72 74 61 69 6e 20 66 69 6c 65 20 73 79 73 certain file sys
e04b0 74 65 6d 73 2e 0a 20 20 20 20 20 20 20 2a 2a 20 tems.. **
e04c0 41 20 66 61 69 6c 65 64 20 64 69 72 65 63 74 6f A failed directo
e04d0 72 79 20 73 79 6e 63 20 69 73 20 6e 6f 74 20 61 ry sync is not a
e04e0 20 62 69 67 20 64 65 61 6c 2e 20 20 53 6f 20 69 big deal. So i
e04f0 74 20 73 65 65 6d 73 0a 20 20 20 20 20 20 20 2a t seems. *
e0500 2a 20 62 65 74 74 65 72 20 74 6f 20 69 67 6e 6f * better to igno
e0510 72 65 20 74 68 65 20 65 72 72 6f 72 2e 20 20 54 re the error. T
e0520 69 63 6b 65 74 20 23 31 36 35 37 0a 20 20 20 20 icket #1657.
e0530 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 2f 2a 20 */. /*
e0540 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
e0550 20 3d 20 65 72 72 6e 6f 3b 20 2a 2f 0a 20 20 20 = errno; */.
e0560 20 20 20 20 2f 2a 20 72 65 74 75 72 6e 20 53 51 /* return SQ
e0570 4c 49 54 45 5f 49 4f 45 52 52 3b 20 2a 2f 0a 20 LITE_IOERR; */.
e0580 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
e0590 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c err = close(pFil
e05a0 65 2d 3e 64 69 72 66 64 29 3b 20 2f 2a 20 4f 6e e->dirfd); /* On
e05b0 6c 79 20 6e 65 65 64 20 74 6f 20 73 79 6e 63 20 ly need to sync
e05c0 6f 6e 63 65 2c 20 73 6f 20 63 6c 6f 73 65 20 74 once, so close t
e05d0 68 65 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 72 he */. if( er
e05e0 72 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 r==0 ){
e05f0 20 20 20 20 20 2f 2a 20 64 69 72 65 63 74 6f 72 /* director
e0600 79 20 77 68 65 6e 20 77 65 20 61 72 65 20 64 6f y when we are do
e0610 6e 65 20 2a 2f 0a 20 20 20 20 20 20 70 46 69 6c ne */. pFil
e0620 65 2d 3e 64 69 72 66 64 20 3d 20 2d 31 3b 0a 20 e->dirfd = -1;.
e0630 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
e0640 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
e0650 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
e0660 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 rc = SQLITE_IOER
e0670 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 R_DIR_CLOSE;.
e0680 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
e0690 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 rc;.}../*.** Tru
e06a0 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 ncate an open fi
e06b0 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 le to a specifie
e06c0 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 d size.*/.static
e06d0 20 69 6e 74 20 75 6e 69 78 54 72 75 6e 63 61 74 int unixTruncat
e06e0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
e06f0 69 64 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0a id, i64 nByte){.
e0700 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 int rc;. asse
e0710 72 74 28 20 69 64 20 29 3b 0a 20 20 53 69 6d 75 rt( id );. Simu
e0720 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 lateIOError( ret
e0730 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
e0740 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72 _TRUNCATE );. r
e0750 63 20 3d 20 66 74 72 75 6e 63 61 74 65 28 28 28 c = ftruncate(((
e0760 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 unixFile*)id)->h
e0770 2c 20 28 6f 66 66 5f 74 29 6e 42 79 74 65 29 3b , (off_t)nByte);
e0780 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 . if( rc ){.
e0790 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 ((unixFile*)id)
e07a0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
e07b0 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 rno;. return
e07c0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 SQLITE_IOERR_TRU
e07d0 4e 43 41 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a NCATE;. }else{.
e07e0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
e07f0 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a E_OK;. }.}../*.
e0800 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 ** Determine the
e0810 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 current size of
e0820 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 a file in bytes
e0830 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
e0840 6e 69 78 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 nixFileSize(sqli
e0850 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 te3_file *id, i6
e0860 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 4 *pSize){. int
e0870 20 72 63 3b 0a 20 20 73 74 72 75 63 74 20 73 74 rc;. struct st
e0880 61 74 20 62 75 66 3b 0a 20 20 61 73 73 65 72 74 at buf;. assert
e0890 28 20 69 64 20 29 3b 0a 20 20 72 63 20 3d 20 66 ( id );. rc = f
e08a0 73 74 61 74 28 28 28 75 6e 69 78 46 69 6c 65 2a stat(((unixFile*
e08b0 29 69 64 29 2d 3e 68 2c 20 26 62 75 66 29 3b 0a )id)->h, &buf);.
e08c0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
e08d0 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 r( rc=1 );. if(
e08e0 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 28 28 rc!=0 ){. ((
e08f0 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c unixFile*)id)->l
e0900 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f astErrno = errno
e0910 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
e0920 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b ITE_IOERR_FSTAT;
e0930 0a 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 . }. *pSize =
e0940 62 75 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a 20 20 buf.st_size;..
e0950 2f 2a 20 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20 /* When opening
e0960 61 20 7a 65 72 6f 2d 73 69 7a 65 20 64 61 74 61 a zero-size data
e0970 62 61 73 65 2c 20 74 68 65 20 66 69 6e 64 4c 6f base, the findLo
e0980 63 6b 49 6e 66 6f 28 29 20 70 72 6f 63 65 64 75 ckInfo() procedu
e0990 72 65 0a 20 20 2a 2a 20 77 72 69 74 65 73 20 61 re. ** writes a
e09a0 20 73 69 6e 67 6c 65 20 62 79 74 65 20 69 6e 74 single byte int
e09b0 6f 20 74 68 61 74 20 66 69 6c 65 20 69 6e 20 6f o that file in o
e09c0 72 64 65 72 20 74 6f 20 77 6f 72 6b 20 61 72 6f rder to work aro
e09d0 75 6e 64 20 61 20 62 75 67 0a 20 20 2a 2a 20 69 und a bug. ** i
e09e0 6e 20 74 68 65 20 4f 53 2d 58 20 6d 73 64 6f 73 n the OS-X msdos
e09f0 20 66 69 6c 65 73 79 73 74 65 6d 2e 20 20 49 6e filesystem. In
e0a00 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 order to avoid
e0a10 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 75 70 problems with up
e0a20 70 65 72 0a 20 20 2a 2a 20 6c 61 79 65 72 73 2c per. ** layers,
e0a30 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65 70 6f we need to repo
e0a40 72 74 20 74 68 69 73 20 66 69 6c 65 20 73 69 7a rt this file siz
e0a50 65 20 61 73 20 7a 65 72 6f 20 65 76 65 6e 20 74 e as zero even t
e0a60 68 6f 75 67 68 20 69 74 20 69 73 0a 20 20 2a 2a hough it is. **
e0a70 20 72 65 61 6c 6c 79 20 31 2e 20 20 20 54 69 63 really 1. Tic
e0a80 6b 65 74 20 23 33 32 36 30 2e 0a 20 20 2a 2f 0a ket #3260.. */.
e0a90 20 20 69 66 28 20 2a 70 53 69 7a 65 3d 3d 31 20 if( *pSize==1
e0aa0 29 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 0a 0a ) *pSize = 0;...
e0ab0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e0ac0 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 OK;.}..#if SQLIT
e0ad0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
e0ae0 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 _STYLE && define
e0af0 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a d(__APPLE__)./*.
e0b00 2a 2a 20 48 61 6e 64 6c 65 72 20 66 6f 72 20 70 ** Handler for p
e0b10 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 66 69 6c roxy-locking fil
e0b20 65 2d 63 6f 6e 74 72 6f 6c 20 76 65 72 62 73 2e e-control verbs.
e0b30 20 20 44 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 Defined below
e0b40 69 6e 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 69 in the.** proxyi
e0b50 6e 67 20 6c 6f 63 6b 69 6e 67 20 64 69 76 69 73 ng locking divis
e0b60 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ion..*/.static i
e0b70 6e 74 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 nt proxyFileCont
e0b80 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 rol(sqlite3_file
e0b90 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 65 *,int,void*);.#e
e0ba0 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 ndif.../*.** Inf
e0bb0 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20 63 6f 6e ormation and con
e0bc0 74 72 6f 6c 20 6f 66 20 61 6e 20 6f 70 65 6e 20 trol of an open
e0bd0 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a file handle..*/.
e0be0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 static int unixF
e0bf0 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 ileControl(sqlit
e0c00 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
e0c10 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 op, void *pArg)
e0c20 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 {. switch( op )
e0c30 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 {. case SQLIT
e0c40 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 E_FCNTL_LOCKSTAT
e0c50 45 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 E: {. *(int
e0c60 2a 29 70 41 72 67 20 3d 20 28 28 75 6e 69 78 46 *)pArg = ((unixF
e0c70 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 ile*)id)->lockty
e0c80 70 65 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e pe;. return
e0c90 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
e0ca0 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 }. case SQLIT
e0cb0 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a E_LAST_ERRNO: {.
e0cc0 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 *(int*)pAr
e0cd0 67 20 3d 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 g = ((unixFile*)
e0ce0 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a id)->lastErrno;.
e0cf0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
e0d00 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 69 ITE_OK;. }.#i
e0d10 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 fndef NDEBUG.
e0d20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 63 61 /* The pager ca
e0d30 6c 6c 73 20 74 68 69 73 20 6d 65 74 68 6f 64 20 lls this method
e0d40 74 6f 20 73 69 67 6e 61 6c 20 74 68 61 74 20 69 to signal that i
e0d50 74 20 68 61 73 20 64 6f 6e 65 0a 20 20 20 20 2a t has done. *
e0d60 2a 20 61 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 * a rollback and
e0d70 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 that the databa
e0d80 73 65 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 se is therefore
e0d90 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 0a 20 20 unchanged and.
e0da0 20 20 2a 2a 20 69 74 20 68 65 6e 63 65 20 69 74 ** it hence it
e0db0 20 69 73 20 4f 4b 20 66 6f 72 20 74 68 65 20 74 is OK for the t
e0dc0 72 61 6e 73 61 63 74 69 6f 6e 20 63 68 61 6e 67 ransaction chang
e0dd0 65 20 63 6f 75 6e 74 65 72 20 74 6f 20 62 65 0a e counter to be.
e0de0 20 20 20 20 2a 2a 20 75 6e 63 68 61 6e 67 65 64 ** unchanged
e0df0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 .. */. cas
e0e00 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 e SQLITE_FCNTL_D
e0e10 42 5f 55 4e 43 48 41 4e 47 45 44 3a 20 7b 0a 20 B_UNCHANGED: {.
e0e20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a ((unixFile*
e0e30 29 69 64 29 2d 3e 64 62 55 70 64 61 74 65 20 3d )id)->dbUpdate =
e0e40 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 0;. return
e0e50 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
e0e60 7d 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c }.#endif.#if SQL
e0e70 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
e0e80 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 NG_STYLE && defi
e0e90 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 ned(__APPLE__).
e0ea0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 case SQLITE_S
e0eb0 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 ET_LOCKPROXYFILE
e0ec0 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 :. case SQLIT
e0ed0 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 E_GET_LOCKPROXYF
e0ee0 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 ILE: {. ret
e0ef0 75 72 6e 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e urn proxyFileCon
e0f00 74 72 6f 6c 28 69 64 2c 6f 70 2c 70 41 72 67 29 trol(id,op,pArg)
e0f10 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f ;. }.#endif /
e0f20 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f * SQLITE_ENABLE_
e0f30 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 LOCKING_STYLE &&
e0f40 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 defined(__APPLE
e0f50 5f 5f 29 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 __) */. }. ret
e0f60 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
e0f70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
e0f80 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a n the sector siz
e0f90 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 e in bytes of th
e0fa0 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f e underlying blo
e0fb0 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a ck device for.**
e0fc0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66 the specified f
e0fd0 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d ile. This is alm
e0fe0 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 ost always 512 b
e0ff0 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 ytes, but may be
e1000 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 .** larger for s
e1010 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a ome devices..**.
e1020 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 ** SQLite code a
e1030 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 ssumes this func
e1040 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c tion cannot fail
e1050 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 . It also assume
e1060 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f s that.** if two
e1070 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 files are creat
e1080 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 ed in the same f
e1090 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 ile-system direc
e10a0 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 tory (i.e..** a
e10b0 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 database and its
e10c0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 journal file) t
e10d0 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 hat the sector s
e10e0 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a ize will be the.
e10f0 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 ** same for both
e1100 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e1110 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 28 73 unixSectorSize(s
e1120 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 qlite3_file *Not
e1130 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f Used){. UNUSED_
e1140 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
e1150 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c d);. return SQL
e1160 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 ITE_DEFAULT_SECT
e1170 4f 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a OR_SIZE;.}../*.*
e1180 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 65 76 * Return the dev
e1190 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74 ice characterist
e11a0 69 63 73 20 66 6f 72 20 74 68 65 20 66 69 6c 65 ics for the file
e11b0 2e 20 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 . This is always
e11c0 20 30 20 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a 0 for unix..*/.
e11d0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 44 static int unixD
e11e0 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
e11f0 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c tics(sqlite3_fil
e1200 65 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 e *NotUsed){. U
e1210 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
e1220 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 NotUsed);. retu
e1230 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 rn 0;.}../*.** H
e1240 65 72 65 20 65 6e 64 73 20 74 68 65 20 69 6d 70 ere ends the imp
e1250 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 lementation of a
e1260 6c 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ll sqlite3_file
e1270 6d 65 74 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 2a 2a methods..**.****
e1280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1290 2a 2a 20 45 6e 64 20 73 71 6c 69 74 65 33 5f 66 ** End sqlite3_f
e12a0 69 6c 65 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a ile Methods ****
e12b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e12c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
e12d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e12e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e12f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a **********/../*.
e1320 2a 2a 20 54 68 69 73 20 64 69 76 69 73 69 6f 6e ** This division
e1330 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 contains defini
e1340 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 tions of sqlite3
e1350 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 _io_methods obje
e1360 63 74 73 20 74 68 61 74 0a 2a 2a 20 69 6d 70 6c cts that.** impl
e1370 65 6d 65 6e 74 20 76 61 72 69 6f 75 73 20 66 69 ement various fi
e1380 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 le locking strat
e1390 65 67 69 65 73 2e 20 20 49 74 20 61 6c 73 6f 20 egies. It also
e13a0 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 contains definit
e13b0 69 6f 6e 73 0a 2a 2a 20 6f 66 20 22 66 69 6e 64 ions.** of "find
e13c0 65 72 22 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 er" functions.
e13d0 41 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f A finder-functio
e13e0 6e 20 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 n is used to loc
e13f0 61 74 65 20 74 68 65 20 61 70 70 72 6f 70 72 69 ate the appropri
e1400 61 74 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 ate.** sqlite3_i
e1410 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 o_methods object
e1420 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 for a particula
e1430 72 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e r database file.
e1440 20 20 54 68 65 20 70 41 70 70 44 61 74 61 0a 2a The pAppData.*
e1450 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 73 * field of the s
e1460 71 6c 69 74 65 33 5f 76 66 73 20 56 46 53 20 6f qlite3_vfs VFS o
e1470 62 6a 65 63 74 73 20 61 72 65 20 69 6e 69 74 69 bjects are initi
e1480 61 6c 69 7a 65 64 20 74 6f 20 62 65 20 70 6f 69 alized to be poi
e1490 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68 65 20 nters to.** the
e14a0 63 6f 72 72 65 63 74 20 66 69 6e 64 65 72 2d 66 correct finder-f
e14b0 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 61 74 unction for that
e14c0 20 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 VFS..**.** Most
e14d0 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e finder function
e14e0 73 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 s return a point
e14f0 65 72 20 74 6f 20 61 20 66 69 78 65 64 20 73 71 er to a fixed sq
e1500 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
e1510 0a 2a 2a 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 .** object. The
e1520 20 6f 6e 6c 79 20 69 6e 74 65 72 65 73 74 69 6e only interestin
e1530 67 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f g finder-functio
e1540 6e 20 69 73 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 n is autolockIoF
e1550 69 6e 64 65 72 2c 20 77 68 69 63 68 0a 2a 2a 20 inder, which.**
e1560 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c looks at the fil
e1570 65 73 79 73 74 65 6d 20 74 79 70 65 20 61 6e 64 esystem type and
e1580 20 74 72 69 65 73 20 74 6f 20 67 75 65 73 73 20 tries to guess
e1590 74 68 65 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67 the best locking
e15a0 0a 2a 2a 20 73 74 72 61 74 65 67 79 20 66 72 6f .** strategy fro
e15b0 6d 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f m that..**.** Fo
e15c0 72 20 66 69 6e 64 65 72 2d 66 75 6e 74 69 6f 6e r finder-funtion
e15d0 20 46 2c 20 74 77 6f 20 6f 62 6a 65 63 74 73 20 F, two objects
e15e0 61 72 65 20 63 72 65 61 74 65 64 3a 0a 2a 2a 0a are created:.**.
e15f0 2a 2a 20 20 20 20 28 31 29 20 54 68 65 20 72 65 ** (1) The re
e1600 61 6c 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 al finder-functi
e1610 6f 6e 20 6e 61 6d 65 64 20 22 46 49 6d 70 74 28 on named "FImpt(
e1620 29 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 )"..**.** (2)
e1630 20 41 20 63 6f 6e 73 74 61 6e 74 20 70 6f 69 6e A constant poin
e1640 74 65 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63 ter to this func
e1650 74 69 6f 6e 20 6e 61 6d 65 64 20 6a 75 73 74 20 tion named just
e1660 22 46 22 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 20 "F"..**.**.** A
e1670 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 pointer to the F
e1680 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 pointer is used
e1690 20 61 73 20 74 68 65 20 70 41 70 70 44 61 74 61 as the pAppData
e16a0 20 76 61 6c 75 65 20 66 6f 72 20 56 46 53 0a 2a value for VFS.*
e16b0 2a 20 6f 62 6a 65 63 74 73 2e 20 20 57 65 20 68 * objects. We h
e16c0 61 76 65 20 74 6f 20 64 6f 20 74 68 69 73 20 69 ave to do this i
e16d0 6e 73 74 65 61 64 20 6f 66 20 6c 65 74 74 69 6e nstead of lettin
e16e0 67 20 70 41 70 70 44 61 74 61 20 70 6f 69 6e 74 g pAppData point
e16f0 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 61 74 20 .** directly at
e1700 74 68 65 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 the finder-funct
e1710 69 6f 6e 20 73 69 6e 63 65 20 43 39 30 20 72 75 ion since C90 ru
e1720 6c 65 73 20 70 72 65 76 65 6e 74 20 61 20 76 6f les prevent a vo
e1730 69 64 2a 0a 2a 2a 20 66 72 6f 6d 20 62 65 20 63 id*.** from be c
e1740 61 73 74 20 69 6e 74 6f 20 61 20 66 75 6e 63 74 ast into a funct
e1750 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a ion pointer..**.
e1760 2a 2a 0a 2a 2a 20 45 61 63 68 20 69 6e 73 74 61 **.** Each insta
e1770 6e 63 65 20 6f 66 20 74 68 69 73 20 6d 61 63 72 nce of this macr
e1780 6f 20 67 65 6e 65 72 61 74 65 73 20 74 77 6f 20 o generates two
e1790 6f 62 6a 65 63 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 objects:.**.**
e17a0 20 2a 20 20 41 20 63 6f 6e 73 74 61 6e 74 20 73 * A constant s
e17b0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e17c0 73 20 6f 62 6a 65 63 74 20 63 61 6c 6c 20 4d 45 s object call ME
e17d0 54 48 4f 44 20 74 68 61 74 20 68 61 73 20 6c 6f THOD that has lo
e17e0 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65 cking.** me
e17f0 74 68 6f 64 73 20 43 4c 4f 53 45 2c 20 4c 4f 43 thods CLOSE, LOC
e1800 4b 2c 20 55 4e 4c 4f 43 4b 2c 20 43 4b 52 45 53 K, UNLOCK, CKRES
e1810 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 LOCK..**.** *
e1820 20 41 6e 20 49 2f 4f 20 6d 65 74 68 6f 64 20 66 An I/O method f
e1830 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 63 inder function c
e1840 61 6c 6c 65 64 20 46 49 4e 44 45 52 20 74 68 61 alled FINDER tha
e1850 74 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e t returns a poin
e1860 74 65 72 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 ter.** to t
e1870 68 65 20 4d 45 54 48 4f 44 20 6f 62 6a 65 63 74 he METHOD object
e1880 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 in the previous
e1890 20 62 75 6c 6c 65 74 2e 0a 2a 2f 0a 23 64 65 66 bullet..*/.#def
e18a0 69 6e 65 20 49 4f 4d 45 54 48 4f 44 53 28 46 49 ine IOMETHODS(FI
e18b0 4e 44 45 52 2c 20 4d 45 54 48 4f 44 2c 20 43 4c NDER, METHOD, CL
e18c0 4f 53 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43 OSE, LOCK, UNLOC
e18d0 4b 2c 20 43 4b 4c 4f 43 4b 29 20 20 20 20 20 20 K, CKLOCK)
e18e0 20 20 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 \.stati
e18f0 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f c const sqlite3_
e1900 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 45 54 48 4f io_methods METHO
e1910 44 20 3d 20 7b 20 20 20 20 20 20 20 20 20 20 20 D = {
e1920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1930 20 20 20 20 20 20 20 20 5c 0a 20 20 20 31 2c 20 \. 1,
e1940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1950 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 /* iVer
e1960 73 69 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 sion */
e1970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1980 20 20 20 20 20 20 20 5c 0a 20 20 20 43 4c 4f 53 \. CLOS
e1990 45 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E,
e19a0 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 /* xClos
e19b0 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 e */
e19c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e19d0 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 52 \. unixR
e19e0 65 61 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 ead,
e19f0 20 20 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 /* xRead
e1a00 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e1a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1a20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 57 72 \. unixWr
e1a30 69 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ite,
e1a40 20 20 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20 /* xWrite
e1a50 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e1a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1a70 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 54 72 75 \. unixTru
e1a80 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 ncate,
e1a90 20 20 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 /* xTruncat
e1aa0 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 e */
e1ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1ac0 20 20 20 5c 0a 20 20 20 75 6e 69 78 53 79 6e 63 \. unixSync
e1ad0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e1ae0 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 20 /* xSync */
e1af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1b10 20 20 5c 0a 20 20 20 75 6e 69 78 46 69 6c 65 53 \. unixFileS
e1b20 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ize,
e1b30 20 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 /* xFileSize
e1b40 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e1b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1b60 20 5c 0a 20 20 20 4c 4f 43 4b 2c 20 20 20 20 20 \. LOCK,
e1b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1b80 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 20 20 20 /* xLock */
e1b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1bb0 5c 0a 20 20 20 55 4e 4c 4f 43 4b 2c 20 20 20 20 \. UNLOCK,
e1bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1bd0 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20 20 /* xUnlock */
e1be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
e1c00 0a 20 20 20 43 4b 4c 4f 43 4b 2c 20 20 20 20 20 . CKLOCK,
e1c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1c20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 /* xCheckReserve
e1c30 64 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 dLock */
e1c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
e1c50 20 20 20 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 unixFileContr
e1c60 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f ol, /
e1c70 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a * xFileControl *
e1c80 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
e1c90 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
e1ca0 20 20 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 unixSectorSize
e1cb0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
e1cc0 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 20 xSectorSize */
e1cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1ce0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
e1cf0 20 75 6e 69 78 44 65 76 69 63 65 43 68 61 72 61 unixDeviceChara
e1d00 63 74 65 72 69 73 74 69 63 73 20 20 20 2f 2a 20 cteristics /*
e1d10 78 44 65 76 69 63 65 43 61 70 61 62 69 6c 69 74 xDeviceCapabilit
e1d20 69 65 73 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ies */
e1d30 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 3b 20 \.};
e1d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1d80 20 20 20 20 20 20 20 20 20 20 5c 0a 73 74 61 74 \.stat
e1d90 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
e1da0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 46 49 4e _io_methods *FIN
e1db0 44 45 52 23 23 49 6d 70 6c 28 63 6f 6e 73 74 20 DER##Impl(const
e1dc0 63 68 61 72 20 2a 7a 2c 20 75 6e 69 78 46 69 6c char *z, unixFil
e1dd0 65 20 2a 70 29 7b 20 20 20 5c 0a 20 20 55 4e 55 e *p){ \. UNU
e1de0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a 29 SED_PARAMETER(z)
e1df0 3b 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 ; UNUSED_PARAMET
e1e00 45 52 28 70 29 3b 20 20 20 20 20 20 20 20 20 20 ER(p);
e1e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1e20 20 20 20 20 20 20 20 20 5c 0a 20 20 72 65 74 75 \. retu
e1e30 72 6e 20 26 4d 45 54 48 4f 44 3b 20 20 20 20 20 rn &METHOD;
e1e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1e70 20 20 20 20 20 20 20 5c 0a 7d 20 20 20 20 20 20 \.}
e1e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1ec0 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20 63 \.static c
e1ed0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f onst sqlite3_io_
e1ee0 6d 65 74 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 methods *(*const
e1ef0 20 46 49 4e 44 45 52 29 28 63 6f 6e 73 74 20 63 FINDER)(const c
e1f00 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 20 2a 70 har*,unixFile *p
e1f10 29 20 20 20 20 5c 0a 20 20 20 20 3d 20 46 49 4e ) \. = FIN
e1f20 44 45 52 23 23 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a DER##Impl;../*.*
e1f30 2a 20 48 65 72 65 20 61 72 65 20 61 6c 6c 20 6f * Here are all o
e1f40 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f f the sqlite3_io
e1f50 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 _methods objects
e1f60 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74 68 65 for each of the
e1f70 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 .** locking stra
e1f80 74 65 67 69 65 73 2e 20 20 46 75 6e 63 74 69 6f tegies. Functio
e1f90 6e 73 20 74 68 61 74 20 72 65 74 75 72 6e 20 70 ns that return p
e1fa0 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 73 65 ointers to these
e1fb0 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 61 72 65 20 methods.** are
e1fc0 61 6c 73 6f 20 63 72 65 61 74 65 64 2e 0a 2a 2f also created..*/
e1fd0 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 6f .IOMETHODS(. po
e1fe0 73 69 78 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 sixIoFinder,
e1ff0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 /* Finde
e2000 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 r function name
e2010 2a 2f 0a 20 20 70 6f 73 69 78 49 6f 4d 65 74 68 */. posixIoMeth
e2020 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f ods, /
e2030 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 * sqlite3_io_met
e2040 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 hods object name
e2050 20 2a 2f 0a 20 20 75 6e 69 78 43 6c 6f 73 65 2c */. unixClose,
e2060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2070 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 /* xClose method
e2080 20 2a 2f 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 */. unixLock,
e2090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e20a0 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 /* xLock method
e20b0 2a 2f 0a 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c */. unixUnlock,
e20c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
e20d0 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 * xUnlock method
e20e0 20 2a 2f 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 */. unixCheckR
e20f0 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 eservedLock
e2100 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 /* xCheckReserve
e2110 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a dLock method */.
e2120 29 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 6e ).IOMETHODS(. n
e2130 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 olockIoFinder,
e2140 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 /* Find
e2150 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 er function name
e2160 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d 65 */. nolockIoMe
e2170 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 thods,
e2180 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 /* sqlite3_io_me
e2190 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d thods object nam
e21a0 65 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 6c 6f e */. nolockClo
e21b0 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 se,
e21c0 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f /* xClose metho
e21d0 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f 63 d */. nolockLoc
e21e0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k,
e21f0 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 /* xLock method
e2200 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f */. nolockUnlo
e2210 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ck,
e2220 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f /* xUnlock metho
e2230 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 68 65 d */. nolockChe
e2240 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 ckReservedLock
e2250 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 /* xCheckReserv
e2260 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f edLock method */
e2270 0a 29 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 .).IOMETHODS(.
e2280 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c dotlockIoFinder,
e2290 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e /* Fin
e22a0 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d der function nam
e22b0 65 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f e */. dotlockIo
e22c0 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 Methods,
e22d0 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d /* sqlite3_io_m
e22e0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 ethods object na
e22f0 6d 65 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 me */. dotlockC
e2300 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 lose,
e2310 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 /* xClose meth
e2320 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 4c od */. dotlockL
e2330 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 ock,
e2340 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f /* xLock metho
e2350 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e d */. dotlockUn
e2360 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 lock,
e2370 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 /* xUnlock meth
e2380 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 od */. dotlockC
e2390 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
e23a0 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 /* xCheckReser
e23b0 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a vedLock method *
e23c0 2f 0a 29 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f /.)..#if SQLITE_
e23d0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
e23e0 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f TYLE && !OS_VXWO
e23f0 52 4b 53 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 RKS.IOMETHODS(.
e2400 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 flockIoFinder,
e2410 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
e2420 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 nder function na
e2430 6d 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 49 6f 4d me */. flockIoM
e2440 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 ethods,
e2450 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f /* sqlite3_io_
e2460 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e methods object n
e2470 61 6d 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 6c ame */. flockCl
e2480 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ose,
e2490 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 /* xClose met
e24a0 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 4c 6f hod */. flockLo
e24b0 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ck,
e24c0 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 /* xLock meth
e24d0 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e 6c od */. flockUnl
e24e0 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 ock,
e24f0 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 /* xUnlock met
e2500 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 68 hod */. flockCh
e2510 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 eckReservedLock
e2520 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 /* xCheckRese
e2530 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 rvedLock method
e2540 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 */.).#endif..#if
e2550 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 OS_VXWORKS.IOME
e2560 54 48 4f 44 53 28 0a 20 20 73 65 6d 49 6f 46 69 THODS(. semIoFi
e2570 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 nder,
e2580 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e /* Finder fun
e2590 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 ction name */.
e25a0 73 65 6d 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 semIoMethods,
e25b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c /* sql
e25c0 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
e25d0 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 object name */.
e25e0 20 73 65 6d 43 6c 6f 73 65 2c 20 20 20 20 20 20 semClose,
e25f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 /* xC
e2600 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 lose method */.
e2610 20 73 65 6d 4c 6f 63 6b 2c 20 20 20 20 20 20 20 semLock,
e2620 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c /* xL
e2630 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ock method */.
e2640 73 65 6d 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 semUnlock,
e2650 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e /* xUn
e2660 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 lock method */.
e2670 20 73 65 6d 43 68 65 63 6b 52 65 73 65 72 76 65 semCheckReserve
e2680 64 4c 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 dLock /* xC
e2690 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
e26a0 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e method */.).#en
e26b0 64 69 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 dif..#if defined
e26c0 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 (__APPLE__) && S
e26d0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
e26e0 4b 49 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54 KING_STYLE.IOMET
e26f0 48 4f 44 53 28 0a 20 20 61 66 70 49 6f 46 69 6e HODS(. afpIoFin
e2700 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 der,
e2710 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 /* Finder func
e2720 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 tion name */. a
e2730 66 70 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 fpIoMethods,
e2740 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 /* sqli
e2750 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f te3_io_methods o
e2760 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 bject name */.
e2770 61 66 70 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 afpClose,
e2780 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c /* xCl
e2790 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ose method */.
e27a0 61 66 70 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 afpLock,
e27b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f /* xLo
e27c0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 ck method */. a
e27d0 66 70 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 fpUnlock,
e27e0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c /* xUnl
e27f0 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ock method */.
e2800 61 66 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 afpCheckReserved
e2810 4c 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 Lock /* xCh
e2820 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 eckReservedLock
e2830 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 method */.).#end
e2840 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 57 if../*.** The "W
e2850 68 6f 6c 65 20 46 69 6c 65 20 4c 6f 63 6b 69 6e hole File Lockin
e2860 67 22 20 66 69 6e 64 65 72 20 72 65 74 75 72 6e g" finder return
e2870 73 20 74 68 65 20 73 61 6d 65 20 73 65 74 20 6f s the same set o
e2880 66 20 6d 65 74 68 6f 64 73 20 61 73 0a 2a 2a 20 f methods as.**
e2890 74 68 65 20 70 6f 73 69 78 20 6c 6f 63 6b 69 6e the posix lockin
e28a0 67 20 66 69 6e 64 65 72 2e 20 20 42 75 74 20 69 g finder. But i
e28b0 74 20 61 6c 73 6f 20 73 65 74 73 20 74 68 65 20 t also sets the
e28c0 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c SQLITE_WHOLE_FIL
e28d0 45 5f 4c 4f 43 4b 49 4e 47 0a 2a 2a 20 66 6c 61 E_LOCKING.** fla
e28e0 67 20 74 6f 20 66 6f 72 63 65 20 74 68 65 20 70 g to force the p
e28f0 6f 73 69 78 20 61 64 76 69 73 6f 72 79 20 6c 6f osix advisory lo
e2900 63 6b 73 20 74 6f 20 63 6f 76 65 72 20 74 68 65 cks to cover the
e2910 20 77 68 6f 6c 65 20 66 69 6c 65 20 69 6e 73 74 whole file inst
e2920 65 61 64 0a 2a 2a 20 6f 66 20 6a 75 73 74 20 61 ead.** of just a
e2930 20 73 6d 61 6c 6c 20 73 70 61 6e 20 6f 66 20 62 small span of b
e2940 79 74 65 73 20 6e 65 61 72 20 74 68 65 20 31 47 ytes near the 1G
e2950 69 42 20 62 6f 75 6e 64 61 72 79 2e 20 20 57 68 iB boundary. Wh
e2960 6f 6c 65 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 ole File Locking
e2970 0a 2a 2a 20 69 73 20 75 73 65 66 75 6c 20 6f 6e .** is useful on
e2980 20 4e 46 53 2d 6d 6f 75 6e 74 65 64 20 66 69 6c NFS-mounted fil
e2990 65 73 20 73 69 6e 63 65 20 69 74 20 68 65 6c 70 es since it help
e29a0 73 20 4e 46 53 20 74 6f 20 6d 61 69 6e 74 61 69 s NFS to maintai
e29b0 6e 20 63 61 63 68 65 0a 2a 2a 20 63 6f 68 65 72 n cache.** coher
e29c0 65 6e 63 79 2e 20 20 42 75 74 20 69 74 20 69 73 ency. But it is
e29d0 20 61 20 64 65 74 72 69 6d 65 6e 74 20 74 6f 20 a detriment to
e29e0 6f 74 68 65 72 20 66 69 6c 65 73 79 73 74 65 6d other filesystem
e29f0 73 20 73 69 6e 63 65 20 69 74 20 72 75 6e 73 0a s since it runs.
e2a00 2a 2a 20 73 6c 6f 77 65 72 2e 0a 2a 2f 0a 73 74 ** slower..*/.st
e2a10 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 atic const sqlit
e2a20 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 e3_io_methods *p
e2a30 6f 73 69 78 57 66 6c 49 6f 46 69 6e 64 65 72 49 osixWflIoFinderI
e2a40 6d 70 6c 28 63 6f 6e 73 74 20 63 68 61 72 2a 7a mpl(const char*z
e2a50 2c 20 75 6e 69 78 46 69 6c 65 2a 70 29 7b 0a 20 , unixFile*p){.
e2a60 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
e2a70 52 28 7a 29 3b 0a 20 20 70 2d 3e 66 69 6c 65 46 R(z);. p->fileF
e2a80 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 57 48 lags = SQLITE_WH
e2a90 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 OLE_FILE_LOCKING
e2aa0 3b 0a 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 ;. return &posi
e2ab0 78 49 6f 4d 65 74 68 6f 64 73 3b 0a 7d 0a 73 74 xIoMethods;.}.st
e2ac0 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 atic const sqlit
e2ad0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 0a 20 e3_io_methods .
e2ae0 20 2a 28 2a 63 6f 6e 73 74 20 70 6f 73 69 78 57 *(*const posixW
e2af0 66 6c 49 6f 46 69 6e 64 65 72 29 28 63 6f 6e 73 flIoFinder)(cons
e2b00 74 20 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 t char*,unixFile
e2b10 20 2a 70 29 20 3d 20 70 6f 73 69 78 57 66 6c 49 *p) = posixWflI
e2b20 6f 46 69 6e 64 65 72 49 6d 70 6c 3b 0a 0a 2f 2a oFinderImpl;../*
e2b30 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 6c 6f .** The proxy lo
e2b40 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 69 73 20 cking method is
e2b50 61 20 22 73 75 70 65 72 2d 6d 65 74 68 6f 64 22 a "super-method"
e2b60 20 69 6e 20 74 68 65 20 73 65 6e 73 65 20 74 68 in the sense th
e2b70 61 74 20 69 74 0a 2a 2a 20 6f 70 65 6e 73 20 73 at it.** opens s
e2b80 65 63 6f 6e 64 61 72 79 20 66 69 6c 65 20 64 65 econdary file de
e2b90 73 63 72 69 70 74 6f 72 73 20 66 6f 72 20 74 68 scriptors for th
e2ba0 65 20 63 6f 6e 63 68 20 61 6e 64 20 6c 6f 63 6b e conch and lock
e2bb0 20 66 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 69 74 files and.** it
e2bc0 20 75 73 65 73 20 70 72 6f 78 79 2c 20 64 6f 74 uses proxy, dot
e2bd0 2d 66 69 6c 65 2c 20 41 46 50 2c 20 61 6e 64 20 -file, AFP, and
e2be0 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 flock() locking
e2bf0 6d 65 74 68 6f 64 73 20 6f 6e 20 74 68 6f 73 65 methods on those
e2c00 0a 2a 2a 20 73 65 63 6f 6e 64 61 72 79 20 66 69 .** secondary fi
e2c10 6c 65 73 2e 20 20 46 6f 72 20 74 68 69 73 20 72 les. For this r
e2c20 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 eason, the divis
e2c30 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ion that impleme
e2c40 6e 74 73 0a 2a 2a 20 70 72 6f 78 79 20 6c 6f 63 nts.** proxy loc
e2c50 6b 69 6e 67 20 69 73 20 6c 6f 63 61 74 65 64 20 king is located
e2c60 6d 75 63 68 20 66 75 72 74 68 65 72 20 64 6f 77 much further dow
e2c70 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 n in the file.
e2c80 42 75 74 20 77 65 20 6e 65 65 64 0a 2a 2a 20 74 But we need.** t
e2c90 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 64 o go ahead and d
e2ca0 65 66 69 6e 65 20 74 68 65 20 73 71 6c 69 74 65 efine the sqlite
e2cb0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 61 6e 64 3_io_methods and
e2cc0 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e finder function
e2cd0 0a 2a 2a 20 66 6f 72 20 70 72 6f 78 79 20 6c 6f .** for proxy lo
e2ce0 63 6b 69 6e 67 20 68 65 72 65 2e 20 20 53 6f 20 cking here. So
e2cf0 77 65 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61 we forward decla
e2d00 72 65 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f re the I/O metho
e2d10 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e ds..*/.#if defin
e2d20 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 ed(__APPLE__) &&
e2d30 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
e2d40 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 73 74 61 OCKING_STYLE.sta
e2d50 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f tic int proxyClo
e2d60 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a se(sqlite3_file*
e2d70 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 );.static int pr
e2d80 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f oxyLock(sqlite3_
e2d90 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 file*, int);.sta
e2da0 74 69 63 20 69 6e 74 20 70 72 6f 78 79 55 6e 6c tic int proxyUnl
e2db0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
e2dc0 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 69 63 20 *, int);.static
e2dd0 69 6e 74 20 70 72 6f 78 79 43 68 65 63 6b 52 65 int proxyCheckRe
e2de0 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 servedLock(sqlit
e2df0 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2a 29 3b e3_file*, int*);
e2e00 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 72 .IOMETHODS(. pr
e2e10 6f 78 79 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 oxyIoFinder,
e2e20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 /* Finde
e2e30 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 r function name
e2e40 2a 2f 0a 20 20 70 72 6f 78 79 49 6f 4d 65 74 68 */. proxyIoMeth
e2e50 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f ods, /
e2e60 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 * sqlite3_io_met
e2e70 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 hods object name
e2e80 20 2a 2f 0a 20 20 70 72 6f 78 79 43 6c 6f 73 65 */. proxyClose
e2e90 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e2ea0 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 /* xClose method
e2eb0 20 2a 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 2c */. proxyLock,
e2ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2ed0 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 /* xLock method
e2ee0 2a 2f 0a 20 20 70 72 6f 78 79 55 6e 6c 6f 63 6b */. proxyUnlock
e2ef0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
e2f00 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 * xUnlock method
e2f10 20 2a 2f 0a 20 20 70 72 6f 78 79 43 68 65 63 6b */. proxyCheck
e2f20 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 ReservedLock
e2f30 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 /* xCheckReserve
e2f40 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a dLock method */.
e2f50 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 20 64 ).#endif...#if d
e2f60 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f efined(__APPLE__
e2f70 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 ) && SQLITE_ENAB
e2f80 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
e2f90 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 69 ./* .** This "fi
e2fa0 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 nder" function a
e2fb0 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72 ttempts to deter
e2fc0 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f mine the best lo
e2fd0 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 0a cking strategy .
e2fe0 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 ** for the datab
e2ff0 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61 ase file "filePa
e3000 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65 th". It then re
e3010 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65 turns the sqlite
e3020 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 3_io_methods.**
e3030 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c object that impl
e3040 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74 72 61 ements that stra
e3050 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tegy..**.** This
e3060 20 69 73 20 66 6f 72 20 4d 61 63 4f 53 58 20 6f is for MacOSX o
e3070 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 nly..*/.static c
e3080 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f onst sqlite3_io_
e3090 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f 6c 6f 63 methods *autoloc
e30a0 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 28 0a 20 kIoFinderImpl(.
e30b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c const char *fil
e30c0 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 6e 61 6d ePath, /* nam
e30d0 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
e30e0 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 e file */. unix
e30f0 46 69 6c 65 20 2a 70 4e 65 77 20 20 20 20 20 20 File *pNew
e3100 20 20 20 20 20 2f 2a 20 6f 70 65 6e 20 66 69 6c /* open fil
e3110 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65 e object for the
e3120 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
e3130 2f 0a 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f /.){. static co
e3140 6e 73 74 20 73 74 72 75 63 74 20 4d 61 70 70 69 nst struct Mappi
e3150 6e 67 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 ng {. const c
e3160 68 61 72 20 2a 7a 46 69 6c 65 73 79 73 74 65 6d har *zFilesystem
e3170 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
e3180 2a 20 46 69 6c 65 73 79 73 74 65 6d 20 74 79 70 * Filesystem typ
e3190 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 63 6f e name */. co
e31a0 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d nst sqlite3_io_m
e31b0 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73 ethods *pMethods
e31c0 3b 20 20 20 2f 2a 20 41 70 70 72 6f 70 72 69 61 ; /* Appropria
e31d0 74 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f te locking metho
e31e0 64 20 2a 2f 0a 20 20 7d 20 61 4d 61 70 5b 5d 20 d */. } aMap[]
e31f0 3d 20 7b 0a 20 20 20 20 7b 20 22 68 66 73 22 2c = {. { "hfs",
e3200 20 20 20 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 &posixIoMeth
e3210 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 22 75 66 ods },. { "uf
e3220 73 22 2c 20 20 20 20 26 70 6f 73 69 78 49 6f 4d s", &posixIoM
e3230 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 ethods },. {
e3240 22 61 66 70 66 73 22 2c 20 20 26 61 66 70 49 6f "afpfs", &afpIo
e3250 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 69 66 64 65 Methods },.#ifde
e3260 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
e3270 41 46 50 5f 4c 4f 43 4b 49 4e 47 5f 53 4d 42 0a AFP_LOCKING_SMB.
e3280 20 20 20 20 7b 20 22 73 6d 62 66 73 22 2c 20 20 { "smbfs",
e3290 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 7d 2c &afpIoMethods },
e32a0 0a 23 65 6c 73 65 0a 20 20 20 20 7b 20 22 73 6d .#else. { "sm
e32b0 62 66 73 22 2c 20 20 26 66 6c 6f 63 6b 49 6f 4d bfs", &flockIoM
e32c0 65 74 68 6f 64 73 20 7d 2c 0a 23 65 6e 64 69 66 ethods },.#endif
e32d0 0a 20 20 20 20 7b 20 22 77 65 62 64 61 76 22 2c . { "webdav",
e32e0 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 &nolockIoMethod
e32f0 73 20 7d 2c 0a 20 20 20 20 7b 20 30 2c 20 30 20 s },. { 0, 0
e3300 7d 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a }. };. int i;.
e3310 20 20 73 74 72 75 63 74 20 73 74 61 74 66 73 20 struct statfs
e3320 66 73 49 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74 fsInfo;. struct
e3330 20 66 6c 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b flock lockInfo;
e3340 0a 0a 20 20 69 66 28 20 21 66 69 6c 65 50 61 74 .. if( !filePat
e3350 68 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 h ){. /* If f
e3360 69 6c 65 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 ilePath==NULL th
e3370 61 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 at means we are
e3380 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20 74 dealing with a t
e3390 72 61 6e 73 69 65 6e 74 20 66 69 6c 65 0a 20 20 ransient file.
e33a0 20 20 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e ** that does n
e33b0 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f ot need to be lo
e33c0 63 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 72 65 74 cked. */. ret
e33d0 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 urn &nolockIoMet
e33e0 68 6f 64 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 hods;. }. if(
e33f0 73 74 61 74 66 73 28 66 69 6c 65 50 61 74 68 2c statfs(filePath,
e3400 20 26 66 73 49 6e 66 6f 29 20 21 3d 20 2d 31 20 &fsInfo) != -1
e3410 29 7b 0a 20 20 20 20 69 66 28 20 66 73 49 6e 66 ){. if( fsInf
e3420 6f 2e 66 5f 66 6c 61 67 73 20 26 20 4d 4e 54 5f o.f_flags & MNT_
e3430 52 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 RDONLY ){.
e3440 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f return &nolockIo
e3450 4d 65 74 68 6f 64 73 3b 0a 20 20 20 20 7d 0a 20 Methods;. }.
e3460 20 20 20 66 6f 72 28 69 3d 30 3b 20 61 4d 61 70 for(i=0; aMap
e3470 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d 3b [i].zFilesystem;
e3480 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 i++){. if(
e3490 20 73 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66 strcmp(fsInfo.f
e34a0 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20 61 4d 61 _fstypename, aMa
e34b0 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d p[i].zFilesystem
e34c0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
e34d0 72 65 74 75 72 6e 20 61 4d 61 70 5b 69 5d 2e 70 return aMap[i].p
e34e0 4d 65 74 68 6f 64 73 3b 0a 20 20 20 20 20 20 7d Methods;. }
e34f0 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
e3500 20 44 65 66 61 75 6c 74 20 63 61 73 65 2e 20 48 Default case. H
e3510 61 6e 64 6c 65 73 2c 20 61 6d 6f 6e 67 73 74 20 andles, amongst
e3520 6f 74 68 65 72 73 2c 20 22 6e 66 73 22 2e 0a 20 others, "nfs"..
e3530 20 2a 2a 20 54 65 73 74 20 62 79 74 65 2d 72 61 ** Test byte-ra
e3540 6e 67 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 66 nge lock using f
e3550 63 6e 74 6c 28 29 2e 20 49 66 20 74 68 65 20 63 cntl(). If the c
e3560 61 6c 6c 20 73 75 63 63 65 65 64 73 2c 20 0a 20 all succeeds, .
e3570 20 2a 2a 20 61 73 73 75 6d 65 20 74 68 61 74 20 ** assume that
e3580 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 the file-system
e3590 73 75 70 70 6f 72 74 73 20 50 4f 53 49 58 20 73 supports POSIX s
e35a0 74 79 6c 65 20 6c 6f 63 6b 73 2e 20 0a 20 20 2a tyle locks. . *
e35b0 2f 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c /. lockInfo.l_l
e35c0 65 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e en = 1;. lockIn
e35d0 66 6f 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a fo.l_start = 0;.
e35e0 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65 lockInfo.l_whe
e35f0 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a nce = SEEK_SET;.
e3600 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70 lockInfo.l_typ
e3610 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69 e = F_RDLCK;. i
e3620 66 28 20 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68 f( fcntl(pNew->h
e3630 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b , F_GETLK, &lock
e3640 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 Info)!=-1 ) {.
e3650 20 20 70 4e 65 77 2d 3e 66 69 6c 65 46 6c 61 67 pNew->fileFlag
e3660 73 20 3d 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 s = SQLITE_WHOLE
e3670 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a 20 _FILE_LOCKING;.
e3680 20 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 return &posix
e3690 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c IoMethods;. }el
e36a0 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 se{. return &
e36b0 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 dotlockIoMethods
e36c0 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 ;. }.}.static c
e36d0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f onst sqlite3_io_
e36e0 6d 65 74 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f methods . *(*co
e36f0 6e 73 74 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 nst autolockIoFi
e3700 6e 64 65 72 29 28 63 6f 6e 73 74 20 63 68 61 72 nder)(const char
e3710 2a 2c 75 6e 69 78 46 69 6c 65 2a 29 20 3d 20 61 *,unixFile*) = a
e3720 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 utolockIoFinderI
e3730 6d 70 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 mpl;..#endif /*
e3740 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f defined(__APPLE_
e3750 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 _) && SQLITE_ENA
e3760 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
e3770 45 20 2a 2f 0a 0a 23 69 66 20 4f 53 5f 56 58 57 E */..#if OS_VXW
e3780 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 45 5f 45 ORKS && SQLITE_E
e3790 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
e37a0 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 YLE./* .** This
e37b0 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f "finder" functio
e37c0 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 64 65 n attempts to de
e37d0 74 65 72 6d 69 6e 65 20 74 68 65 20 62 65 73 74 termine the best
e37e0 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 locking strateg
e37f0 79 20 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 y .** for the da
e3800 74 61 62 61 73 65 20 66 69 6c 65 20 22 66 69 6c tabase file "fil
e3810 65 50 61 74 68 22 2e 20 20 49 74 20 74 68 65 6e ePath". It then
e3820 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 71 6c returns the sql
e3830 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a ite3_io_methods.
e3840 2a 2a 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69 ** object that i
e3850 6d 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 73 mplements that s
e3860 74 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 trategy..**.** T
e3870 68 69 73 20 69 73 20 66 6f 72 20 56 58 57 6f 72 his is for VXWor
e3880 6b 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 ks only..*/.stat
e3890 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
e38a0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 75 74 _io_methods *aut
e38b0 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 olockIoFinderImp
e38c0 6c 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 l(. const char
e38d0 2a 66 69 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a *filePath, /*
e38e0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 name of the dat
e38f0 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 abase file */.
e3900 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 20 unixFile *pNew
e3910 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 /* the
e3920 6f 70 65 6e 20 66 69 6c 65 20 6f 62 6a 65 63 74 open file object
e3930 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 */.){. struct
e3940 66 6c 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a flock lockInfo;.
e3950 0a 20 20 69 66 28 20 21 66 69 6c 65 50 61 74 68 . if( !filePath
e3960 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 ){. /* If fi
e3970 6c 65 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 lePath==NULL tha
e3980 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 64 t means we are d
e3990 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20 74 72 ealing with a tr
e39a0 61 6e 73 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 ansient file.
e39b0 20 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f ** that does no
e39c0 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 t need to be loc
e39d0 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 ked. */. retu
e39e0 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 rn &nolockIoMeth
e39f0 6f 64 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 ods;. }.. /* T
e3a00 65 73 74 20 69 66 20 66 63 6e 74 6c 28 29 20 69 est if fcntl() i
e3a10 73 20 73 75 70 70 6f 72 74 65 64 20 61 6e 64 20 s supported and
e3a20 75 73 65 20 50 4f 53 49 58 20 73 74 79 6c 65 20 use POSIX style
e3a30 6c 6f 63 6b 73 2e 0a 20 20 2a 2a 20 4f 74 68 65 locks.. ** Othe
e3a40 72 77 69 73 65 20 66 61 6c 6c 20 62 61 63 6b 20 rwise fall back
e3a50 74 6f 20 74 68 65 20 6e 61 6d 65 64 20 73 65 6d to the named sem
e3a60 61 70 68 6f 72 65 20 6d 65 74 68 6f 64 2e 0a 20 aphore method..
e3a70 20 2a 2f 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c */. lockInfo.l
e3a80 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b _len = 1;. lock
e3a90 49 6e 66 6f 2e 6c 5f 73 74 61 72 74 20 3d 20 30 Info.l_start = 0
e3aa0 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 ;. lockInfo.l_w
e3ab0 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 hence = SEEK_SET
e3ac0 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 ;. lockInfo.l_t
e3ad0 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 ype = F_RDLCK;.
e3ae0 20 69 66 28 20 66 63 6e 74 6c 28 70 4e 65 77 2d if( fcntl(pNew-
e3af0 3e 68 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f >h, F_GETLK, &lo
e3b00 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a ckInfo)!=-1 ) {.
e3b10 20 20 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 return &posi
e3b20 78 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 xIoMethods;. }e
e3b30 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
e3b40 26 73 65 6d 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 &semIoMethods;.
e3b50 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 }.}.static cons
e3b60 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
e3b70 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 hods . *(*const
e3b80 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 autolockIoFinde
e3b90 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 r)(const char*,u
e3ba0 6e 69 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f nixFile*) = auto
e3bb0 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c lockIoFinderImpl
e3bc0 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f ;..#endif /* OS_
e3bd0 56 58 57 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 VXWORKS && SQLIT
e3be0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
e3bf0 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a _STYLE */../*.**
e3c00 20 41 6e 20 61 62 73 74 72 61 63 74 20 74 79 70 An abstract typ
e3c10 65 20 66 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 e for a pointer
e3c20 74 6f 20 61 20 49 4f 20 6d 65 74 68 6f 64 20 66 to a IO method f
e3c30 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 3a 0a inder function:.
e3c40 2a 2f 0a 74 79 70 65 64 65 66 20 63 6f 6e 73 74 */.typedef const
e3c50 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
e3c60 6f 64 73 20 2a 28 2a 66 69 6e 64 65 72 5f 74 79 ods *(*finder_ty
e3c70 70 65 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c pe)(const char*,
e3c80 75 6e 69 78 46 69 6c 65 2a 29 3b 0a 0a 0a 2f 2a unixFile*);.../*
e3c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
e3ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3cf0 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 ******** sqlite3
e3d00 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 2a 2a 2a _vfs methods ***
e3d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
e3d30 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20 63 6f This division co
e3d40 6e 74 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 ntains the imple
e3d50 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 65 74 mentation of met
e3d60 68 6f 64 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 hods on the.** s
e3d70 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 qlite3_vfs objec
e3d80 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 t..*/../*.** Ini
e3d90 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e 74 tialize the cont
e3da0 65 6e 74 73 20 6f 66 20 74 68 65 20 75 6e 69 78 ents of the unix
e3db0 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 70 File structure p
e3dc0 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 49 64 ointed to by pId
e3dd0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e3de0 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 0a fillInUnixFile(.
e3df0 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
e3e00 56 66 73 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69 Vfs, /* Poi
e3e10 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 nter to vfs obje
e3e20 63 74 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 20 ct */. int h,
e3e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3e40 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 /* Open file des
e3e50 63 72 69 70 74 6f 72 20 6f 66 20 66 69 6c 65 20 criptor of file
e3e60 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a being opened */.
e3e70 20 20 69 6e 74 20 64 69 72 66 64 2c 20 20 20 20 int dirfd,
e3e80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 /* Dir
e3e90 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 73 63 ectory file desc
e3ea0 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 riptor */. sqli
e3eb0 74 65 33 5f 66 69 6c 65 20 2a 70 49 64 2c 20 20 te3_file *pId,
e3ec0 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 6f 20 /* Write to
e3ed0 74 68 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72 the unixFile str
e3ee0 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 ucture here */.
e3ef0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
e3f00 6c 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 lename, /* Name
e3f10 20 6f 66 20 74 68 65 20 66 69 6c 65 20 62 65 69 of the file bei
e3f20 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 ng opened */. i
e3f30 6e 74 20 6e 6f 4c 6f 63 6b 2c 20 20 20 20 20 20 nt noLock,
e3f40 20 20 20 20 20 20 20 2f 2a 20 4f 6d 69 74 20 6c /* Omit l
e3f50 6f 63 6b 69 6e 67 20 69 66 20 74 72 75 65 20 2a ocking if true *
e3f60 2f 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65 /. int isDelete
e3f70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
e3f80 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 20 69 elete on close i
e3f90 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 63 f true */.){. c
e3fa0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f onst sqlite3_io_
e3fb0 6d 65 74 68 6f 64 73 20 2a 70 4c 6f 63 6b 69 6e methods *pLockin
e3fc0 67 53 74 79 6c 65 3b 0a 20 20 75 6e 69 78 46 69 gStyle;. unixFi
e3fd0 6c 65 20 2a 70 4e 65 77 20 3d 20 28 75 6e 69 78 le *pNew = (unix
e3fe0 46 69 6c 65 20 2a 29 70 49 64 3b 0a 20 20 69 6e File *)pId;. in
e3ff0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
e4000 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 ;.. assert( pNe
e4010 77 2d 3e 70 4c 6f 63 6b 3d 3d 4e 55 4c 4c 20 29 w->pLock==NULL )
e4020 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 ;. assert( pNew
e4030 2d 3e 70 4f 70 65 6e 3d 3d 4e 55 4c 4c 20 29 3b ->pOpen==NULL );
e4040 0a 0a 20 20 2f 2a 20 50 61 72 61 6d 65 74 65 72 .. /* Parameter
e4050 20 69 73 44 65 6c 65 74 65 20 69 73 20 6f 6e 6c isDelete is onl
e4060 79 20 75 73 65 64 20 6f 6e 20 76 78 77 6f 72 6b y used on vxwork
e4070 73 2e 20 45 78 70 72 65 73 73 20 74 68 69 73 20 s. Express this
e4080 65 78 70 6c 69 63 69 74 6c 79 20 0a 20 20 2a 2a explicitly . **
e4090 20 68 65 72 65 20 74 6f 20 70 72 65 76 65 6e 74 here to prevent
e40a0 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e compiler warnin
e40b0 67 73 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 gs about unused
e40c0 70 61 72 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f parameters.. */
e40d0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
e40e0 54 45 52 28 69 73 44 65 6c 65 74 65 29 3b 0a 0a TER(isDelete);..
e40f0 20 20 4f 53 54 52 41 43 45 33 28 22 4f 50 45 4e OSTRACE3("OPEN
e4100 20 20 20 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20 %-3d %s\n",
e4110 68 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 20 20 h, zFilename);
e4120 20 20 0a 20 20 70 4e 65 77 2d 3e 68 20 3d 20 68 . pNew->h = h
e4130 3b 0a 20 20 70 4e 65 77 2d 3e 64 69 72 66 64 20 ;. pNew->dirfd
e4140 3d 20 64 69 72 66 64 3b 0a 20 20 53 45 54 5f 54 = dirfd;. SET_T
e4150 48 52 45 41 44 49 44 28 70 4e 65 77 29 3b 0a 20 HREADID(pNew);.
e4160 20 70 4e 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73 pNew->fileFlags
e4170 20 3d 20 30 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 = 0;..#if OS_VX
e4180 57 4f 52 4b 53 0a 20 20 70 4e 65 77 2d 3e 70 49 WORKS. pNew->pI
e4190 64 20 3d 20 76 78 77 6f 72 6b 73 46 69 6e 64 46 d = vxworksFindF
e41a0 69 6c 65 49 64 28 7a 46 69 6c 65 6e 61 6d 65 29 ileId(zFilename)
e41b0 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 49 ;. if( pNew->pI
e41c0 64 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 6f 4c 6f d==0 ){. noLo
e41d0 63 6b 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d ck = 1;. rc =
e41e0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
e41f0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 }.#endif.. if(
e4200 20 6e 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 noLock ){. p
e4210 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 26 LockingStyle = &
e4220 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b nolockIoMethods;
e4230 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4c . }else{. pL
e4240 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 28 2a ockingStyle = (*
e4250 2a 28 66 69 6e 64 65 72 5f 74 79 70 65 2a 29 70 *(finder_type*)p
e4260 56 66 73 2d 3e 70 41 70 70 44 61 74 61 29 28 7a Vfs->pAppData)(z
e4270 46 69 6c 65 6e 61 6d 65 2c 20 70 4e 65 77 29 3b Filename, pNew);
e4280 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 .#if SQLITE_ENAB
e4290 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
e42a0 0a 20 20 20 20 2f 2a 20 43 61 63 68 65 20 7a 46 . /* Cache zF
e42b0 69 6c 65 6e 61 6d 65 20 69 6e 20 74 68 65 20 6c ilename in the l
e42c0 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 28 ocking context (
e42d0 41 46 50 20 61 6e 64 20 64 6f 74 6c 6f 63 6b 20 AFP and dotlock
e42e0 6f 76 65 72 72 69 64 65 29 20 66 6f 72 0a 20 20 override) for.
e42f0 20 20 2a 2a 20 70 72 6f 78 79 4c 6f 63 6b 20 61 ** proxyLock a
e4300 63 74 69 76 61 74 69 6f 6e 20 69 73 20 70 6f 73 ctivation is pos
e4310 73 69 62 6c 65 20 28 72 65 6d 6f 74 65 20 70 72 sible (remote pr
e4320 6f 78 79 20 69 73 20 62 61 73 65 64 20 6f 6e 20 oxy is based on
e4330 64 62 20 6e 61 6d 65 29 0a 20 20 20 20 2a 2a 20 db name). **
e4340 7a 46 69 6c 65 6e 61 6d 65 20 72 65 6d 61 69 6e zFilename remain
e4350 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 66 69 s valid until fi
e4360 6c 65 20 69 73 20 63 6c 6f 73 65 64 2c 20 74 6f le is closed, to
e4370 20 73 75 70 70 6f 72 74 20 2a 2f 0a 20 20 20 20 support */.
e4380 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e pNew->lockingCon
e4390 74 65 78 74 20 3d 20 28 76 6f 69 64 2a 29 7a 46 text = (void*)zF
e43a0 69 6c 65 6e 61 6d 65 3b 0a 23 65 6e 64 69 66 0a ilename;.#endif.
e43b0 20 20 7d 0a 0a 20 20 69 66 28 20 70 4c 6f 63 6b }.. if( pLock
e43c0 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 70 6f 73 ingStyle == &pos
e43d0 69 78 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 ixIoMethods ){.
e43e0 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 unixEnterMute
e43f0 78 28 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 x();. rc = fi
e4400 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c ndLockInfo(pNew,
e4410 20 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 &pNew->pLock, &
e4420 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 pNew->pOpen);.
e4430 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
e4440 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 _OK ){. /*
e4450 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
e4460 72 65 64 20 69 6e 20 66 69 6e 64 4c 6f 63 6b 49 red in findLockI
e4470 6e 66 6f 28 29 2c 20 63 6c 6f 73 65 20 74 68 65 nfo(), close the
e4480 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
e4490 0a 20 20 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 . ** immedi
e44a0 61 74 65 6c 79 2c 20 62 65 66 6f 72 65 20 72 65 ately, before re
e44b0 6c 65 61 73 69 6e 67 20 74 68 65 20 6d 75 74 65 leasing the mute
e44c0 78 2e 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 x. findLockInfo(
e44d0 29 20 6d 61 79 20 66 61 69 6c 0a 20 20 20 20 20 ) may fail.
e44e0 20 2a 2a 20 69 6e 20 74 77 6f 20 73 63 65 6e 61 ** in two scena
e44f0 72 69 6f 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 rios:. **.
e4500 20 20 20 20 20 2a 2a 20 20 20 28 61 29 20 41 20 ** (a) A
e4510 63 61 6c 6c 20 74 6f 20 66 73 74 61 74 28 29 20 call to fstat()
e4520 66 61 69 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a failed.. **
e4530 20 20 20 28 62 29 20 41 20 6d 61 6c 6c 6f 63 20 (b) A malloc
e4540 66 61 69 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a failed.. **
e4550 0a 20 20 20 20 20 20 2a 2a 20 53 63 65 6e 61 72 . ** Scenar
e4560 69 6f 20 28 62 29 20 6d 61 79 20 6f 6e 6c 79 20 io (b) may only
e4570 6f 63 63 75 72 20 69 66 20 74 68 65 20 70 72 6f occur if the pro
e4580 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 cess is holding
e4590 6e 6f 20 6f 74 68 65 72 0a 20 20 20 20 20 20 2a no other. *
e45a0 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f * file descripto
e45b0 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 rs open on the s
e45c0 61 6d 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 ame file. If the
e45d0 72 65 20 77 65 72 65 20 6f 74 68 65 72 20 66 69 re were other fi
e45e0 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 63 le. ** desc
e45f0 72 69 70 74 6f 72 73 20 6f 6e 20 74 68 69 73 20 riptors on this
e4600 66 69 6c 65 2c 20 74 68 65 6e 20 6e 6f 20 6d 61 file, then no ma
e4610 6c 6c 6f 63 20 77 6f 75 6c 64 20 62 65 20 72 65 lloc would be re
e4620 71 75 69 72 65 64 20 62 79 0a 20 20 20 20 20 20 quired by.
e4630 2a 2a 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 ** findLockInfo(
e4640 29 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68 ). If this is th
e4650 65 20 63 61 73 65 2c 20 69 74 20 69 73 20 71 75 e case, it is qu
e4660 69 74 65 20 73 61 66 65 20 74 6f 20 63 6c 6f 73 ite safe to clos
e4670 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c e. ** handl
e4680 65 20 68 20 2d 20 61 73 20 69 74 20 69 73 20 67 e h - as it is g
e4690 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 20 6e uaranteed that n
e46a0 6f 20 70 6f 73 69 78 20 6c 6f 63 6b 73 20 77 69 o posix locks wi
e46b0 6c 6c 20 62 65 20 72 65 6c 65 61 73 65 64 0a 20 ll be released.
e46c0 20 20 20 20 20 2a 2a 20 62 79 20 64 6f 69 6e 67 ** by doing
e46d0 20 73 6f 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 so.. **.
e46e0 20 20 20 20 2a 2a 20 49 66 20 73 63 65 6e 61 72 ** If scenar
e46f0 69 6f 20 28 61 29 20 63 61 75 73 65 64 20 74 68 io (a) caused th
e4700 65 20 65 72 72 6f 72 20 74 68 65 6e 20 74 68 69 e error then thi
e4710 6e 67 73 20 61 72 65 20 6e 6f 74 20 73 6f 20 73 ngs are not so s
e4720 61 66 65 2e 20 54 68 65 0a 20 20 20 20 20 20 2a afe. The. *
e4730 2a 20 69 6d 70 6c 69 63 69 74 20 61 73 73 75 6d * implicit assum
e4740 70 74 69 6f 6e 20 68 65 72 65 20 69 73 20 74 68 ption here is th
e4750 61 74 20 69 66 20 66 73 74 61 74 28 29 20 66 61 at if fstat() fa
e4760 69 6c 73 2c 20 74 68 69 6e 67 73 20 61 72 65 20 ils, things are
e4770 69 6e 0a 20 20 20 20 20 20 2a 2a 20 73 75 63 68 in. ** such
e4780 20 62 61 64 20 73 68 61 70 65 20 74 68 61 74 20 bad shape that
e4790 64 72 6f 70 70 69 6e 67 20 61 20 6c 6f 63 6b 20 dropping a lock
e47a0 6f 72 20 74 77 6f 20 64 6f 65 73 6e 27 74 20 6d or two doesn't m
e47b0 61 74 74 65 72 20 6d 75 63 68 2e 0a 20 20 20 20 atter much..
e47c0 20 20 2a 2f 0a 20 20 20 20 20 20 63 6c 6f 73 65 */. close
e47d0 28 68 29 3b 0a 20 20 20 20 20 20 68 20 3d 20 2d (h);. h = -
e47e0 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 1;. }. uni
e47f0 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 xLeaveMutex();.
e4800 20 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 }..#if SQLITE_E
e4810 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
e4820 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f YLE && defined(_
e4830 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 65 6c 73 65 _APPLE__). else
e4840 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 if( pLockingSty
e4850 6c 65 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 le == &afpIoMeth
e4860 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 41 46 ods ){. /* AF
e4870 50 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 P locking uses t
e4880 68 65 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20 he file path so
e4890 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 it needs to be i
e48a0 6e 63 6c 75 64 65 64 20 69 6e 0a 20 20 20 20 2a ncluded in. *
e48b0 2a 20 74 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 * the afpLocking
e48c0 43 6f 6e 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a Context.. */.
e48d0 20 20 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f afpLockingCo
e48e0 6e 74 65 78 74 20 2a 70 43 74 78 3b 0a 20 20 20 ntext *pCtx;.
e48f0 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pNew->lockingCo
e4900 6e 74 65 78 74 20 3d 20 70 43 74 78 20 3d 20 73 ntext = pCtx = s
e4910 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 qlite3_malloc( s
e4920 69 7a 65 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a izeof(*pCtx) );.
e4930 20 20 20 20 69 66 28 20 70 43 74 78 3d 3d 30 20 if( pCtx==0
e4940 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
e4950 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
e4960 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 }else{. /*
e4970 4e 42 3a 20 7a 46 69 6c 65 6e 61 6d 65 20 65 78 NB: zFilename ex
e4980 69 73 74 73 20 61 6e 64 20 72 65 6d 61 69 6e 73 ists and remains
e4990 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 valid until the
e49a0 20 66 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 0a file is closed.
e49b0 20 20 20 20 20 20 2a 2a 20 61 63 63 6f 72 64 69 ** accordi
e49c0 6e 67 20 74 6f 20 72 65 71 75 69 72 65 6d 65 6e ng to requiremen
e49d0 74 20 46 31 31 31 34 31 2e 20 20 53 6f 20 77 65 t F11141. So we
e49e0 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 do not need to
e49f0 6d 61 6b 65 20 61 0a 20 20 20 20 20 20 2a 2a 20 make a. **
e4a00 63 6f 70 79 20 6f 66 20 74 68 65 20 66 69 6c 65 copy of the file
e4a10 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 70 name. */. p
e4a20 43 74 78 2d 3e 64 62 50 61 74 68 20 3d 20 7a 46 Ctx->dbPath = zF
e4a30 69 6c 65 6e 61 6d 65 3b 0a 20 20 20 20 20 20 73 ilename;. s
e4a40 72 61 6e 64 6f 6d 64 65 76 28 29 3b 0a 20 20 20 randomdev();.
e4a50 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 unixEnterMute
e4a60 78 28 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 x();. rc =
e4a70 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 findLockInfo(pNe
e4a80 77 2c 20 4e 55 4c 4c 2c 20 26 70 4e 65 77 2d 3e w, NULL, &pNew->
e4a90 70 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 75 6e pOpen);. un
e4aa0 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 20 ixLeaveMutex();
e4ab0 20 20 20 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 . }.
e4ac0 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 65 6c 73 65 }.#endif.. else
e4ad0 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 if( pLockingSty
e4ae0 6c 65 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f le == &dotlockIo
e4af0 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f Methods ){. /
e4b00 2a 20 44 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e * Dotfile lockin
e4b10 67 20 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 g uses the file
e4b20 70 61 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 path so it needs
e4b30 20 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 to be included
e4b40 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 6f in. ** the do
e4b50 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 tlockLockingCont
e4b60 65 78 74 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ext . */.
e4b70 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b char *zLockFile;
e4b80 0a 20 20 20 20 69 6e 74 20 6e 46 69 6c 65 6e 61 . int nFilena
e4b90 6d 65 3b 0a 20 20 20 20 6e 46 69 6c 65 6e 61 6d me;. nFilenam
e4ba0 65 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 e = (int)strlen(
e4bb0 7a 46 69 6c 65 6e 61 6d 65 29 20 2b 20 36 3b 0a zFilename) + 6;.
e4bc0 20 20 20 20 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 zLockFile =
e4bd0 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f (char *)sqlite3_
e4be0 6d 61 6c 6c 6f 63 28 6e 46 69 6c 65 6e 61 6d 65 malloc(nFilename
e4bf0 29 3b 0a 20 20 20 20 69 66 28 20 7a 4c 6f 63 6b );. if( zLock
e4c00 46 69 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 File==0 ){.
e4c10 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
e4c20 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 EM;. }else{.
e4c30 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
e4c40 72 69 6e 74 66 28 6e 46 69 6c 65 6e 61 6d 65 2c rintf(nFilename,
e4c50 20 7a 4c 6f 63 6b 46 69 6c 65 2c 20 22 25 73 22 zLockFile, "%s"
e4c60 20 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 2c DOTLOCK_SUFFIX,
e4c70 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 zFilename);.
e4c80 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 }. pNew->loc
e4c90 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 7a 4c kingContext = zL
e4ca0 6f 63 6b 46 69 6c 65 3b 0a 20 20 7d 0a 0a 23 69 ockFile;. }..#i
e4cb0 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 65 f OS_VXWORKS. e
e4cc0 6c 73 65 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 lse if( pLocking
e4cd0 53 74 79 6c 65 20 3d 3d 20 26 73 65 6d 49 6f 4d Style == &semIoM
e4ce0 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a ethods ){. /*
e4cf0 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 Named semaphore
e4d00 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 locking uses th
e4d10 65 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 e file path so i
e4d20 74 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20 t needs to be.
e4d30 20 20 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e ** included in
e4d40 20 74 68 65 20 73 65 6d 4c 6f 63 6b 69 6e 67 43 the semLockingC
e4d50 6f 6e 74 65 78 74 0a 20 20 20 20 2a 2f 0a 20 20 ontext. */.
e4d60 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 unixEnterMutex
e4d70 28 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e ();. rc = fin
e4d80 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 dLockInfo(pNew,
e4d90 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 &pNew->pLock, &p
e4da0 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 New->pOpen);.
e4db0 20 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 if( (rc==SQLITE
e4dc0 5f 4f 4b 29 20 26 26 20 28 70 4e 65 77 2d 3e 70 _OK) && (pNew->p
e4dd0 4f 70 65 6e 2d 3e 70 53 65 6d 3d 3d 4e 55 4c 4c Open->pSem==NULL
e4de0 29 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 ) ){. char
e4df0 2a 7a 53 65 6d 4e 61 6d 65 20 3d 20 70 4e 65 77 *zSemName = pNew
e4e00 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d ->pOpen->aSemNam
e4e10 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a e;. int n;.
e4e20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
e4e30 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e printf(MAX_PATHN
e4e40 41 4d 45 2c 20 7a 53 65 6d 4e 61 6d 65 2c 20 22 AME, zSemName, "
e4e50 2f 25 73 2e 73 65 6d 22 2c 0a 20 20 20 20 20 20 /%s.sem",.
e4e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e4e70 20 70 4e 65 77 2d 3e 70 49 64 2d 3e 7a 43 61 6e pNew->pId->zCan
e4e80 6f 6e 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 onicalName);.
e4e90 20 20 20 66 6f 72 28 20 6e 3d 31 3b 20 7a 53 65 for( n=1; zSe
e4ea0 6d 4e 61 6d 65 5b 6e 5d 3b 20 6e 2b 2b 20 29 0a mName[n]; n++ ).
e4eb0 20 20 20 20 20 20 20 20 69 66 28 20 7a 53 65 6d if( zSem
e4ec0 4e 61 6d 65 5b 6e 5d 3d 3d 27 2f 27 20 29 20 7a Name[n]=='/' ) z
e4ed0 53 65 6d 4e 61 6d 65 5b 6e 5d 20 3d 20 27 5f 27 SemName[n] = '_'
e4ee0 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4f ;. pNew->pO
e4ef0 70 65 6e 2d 3e 70 53 65 6d 20 3d 20 73 65 6d 5f pen->pSem = sem_
e4f00 6f 70 65 6e 28 7a 53 65 6d 4e 61 6d 65 2c 20 4f open(zSemName, O
e4f10 5f 43 52 45 41 54 2c 20 30 36 36 36 2c 20 31 29 _CREAT, 0666, 1)
e4f20 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 ;. if( pNew
e4f30 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 3d ->pOpen->pSem ==
e4f40 20 53 45 4d 5f 46 41 49 4c 45 44 20 29 7b 0a 20 SEM_FAILED ){.
e4f50 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
e4f60 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
e4f70 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 pNew->pOpen->a
e4f80 53 65 6d 4e 61 6d 65 5b 30 5d 20 3d 20 27 5c 30 SemName[0] = '\0
e4f90 27 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d ';. }. }
e4fa0 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 . unixLeaveMu
e4fb0 74 65 78 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 tex();. }.#endi
e4fc0 66 0a 20 20 0a 20 20 70 4e 65 77 2d 3e 6c 61 73 f. . pNew->las
e4fd0 74 45 72 72 6e 6f 20 3d 20 30 3b 0a 23 69 66 20 tErrno = 0;.#if
e4fe0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 OS_VXWORKS. if(
e4ff0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
e5000 7b 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 46 69 {. unlink(zFi
e5010 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 69 73 44 lename);. isD
e5020 65 6c 65 74 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 elete = 0;. }.
e5030 20 70 4e 65 77 2d 3e 69 73 44 65 6c 65 74 65 20 pNew->isDelete
e5040 3d 20 69 73 44 65 6c 65 74 65 3b 0a 23 65 6e 64 = isDelete;.#end
e5050 69 66 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c if. if( rc!=SQL
e5060 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 ITE_OK ){. if
e5070 28 20 64 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f ( dirfd>=0 ) clo
e5080 73 65 28 64 69 72 66 64 29 3b 20 2f 2a 20 73 69 se(dirfd); /* si
e5090 6c 65 6e 74 20 6c 65 61 6b 20 69 66 20 66 61 69 lent leak if fai
e50a0 6c 2c 20 61 6c 72 65 61 64 79 20 69 6e 20 65 72 l, already in er
e50b0 72 6f 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 68 ror */. if( h
e50c0 3e 3d 30 20 29 20 63 6c 6f 73 65 28 68 29 3b 0a >=0 ) close(h);.
e50d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 }else{. pNe
e50e0 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70 4c 6f w->pMethod = pLo
e50f0 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 20 20 ckingStyle;.
e5100 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b OpenCounter(+1);
e5110 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
e5120 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 ;.}../*.** Open
e5130 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f a file descripto
e5140 72 20 74 6f 20 74 68 65 20 64 69 72 65 63 74 6f r to the directo
e5150 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 ry containing fi
e5160 6c 65 20 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a le zFilename..**
e5170 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
e5180 2a 70 46 64 20 69 73 20 73 65 74 20 74 6f 20 74 *pFd is set to t
e5190 68 65 20 6f 70 65 6e 65 64 20 66 69 6c 65 20 64 he opened file d
e51a0 65 73 63 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a escriptor and.**
e51b0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
e51c0 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 turned. If an er
e51d0 72 6f 72 20 6f 63 63 75 72 73 2c 20 65 69 74 68 ror occurs, eith
e51e0 65 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a er SQLITE_NOMEM.
e51f0 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e ** or SQLITE_CAN
e5200 54 4f 50 45 4e 20 69 73 20 72 65 74 75 72 6e 65 TOPEN is returne
e5210 64 20 61 6e 64 20 2a 70 46 64 20 69 73 20 73 65 d and *pFd is se
e5220 74 20 74 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65 t to an undefine
e5230 64 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a d.** value..**.*
e5240 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 * If SQLITE_OK i
e5250 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 20 s returned, the
e5260 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e caller is respon
e5270 73 69 62 6c 65 20 66 6f 72 20 63 6c 6f 73 69 6e sible for closin
e5280 67 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 64 65 g.** the file de
e5290 73 63 72 69 70 74 6f 72 20 2a 70 46 64 20 75 73 scriptor *pFd us
e52a0 69 6e 67 20 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a ing close()..*/.
e52b0 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 static int openD
e52c0 69 72 65 63 74 6f 72 79 28 63 6f 6e 73 74 20 63 irectory(const c
e52d0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 har *zFilename,
e52e0 69 6e 74 20 2a 70 46 64 29 7b 0a 20 20 69 6e 74 int *pFd){. int
e52f0 20 69 69 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 ii;. int fd =
e5300 2d 31 3b 0a 20 20 63 68 61 72 20 7a 44 69 72 6e -1;. char zDirn
e5310 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 ame[MAX_PATHNAME
e5320 2b 31 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f +1];.. sqlite3_
e5330 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 snprintf(MAX_PAT
e5340 48 4e 41 4d 45 2c 20 7a 44 69 72 6e 61 6d 65 2c HNAME, zDirname,
e5350 20 22 25 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 "%s", zFilename
e5360 29 3b 0a 20 20 66 6f 72 28 69 69 3d 28 69 6e 74 );. for(ii=(int
e5370 29 73 74 72 6c 65 6e 28 7a 44 69 72 6e 61 6d 65 )strlen(zDirname
e5380 29 3b 20 69 69 3e 31 20 26 26 20 7a 44 69 72 6e ); ii>1 && zDirn
e5390 61 6d 65 5b 69 69 5d 21 3d 27 2f 27 3b 20 69 69 ame[ii]!='/'; ii
e53a0 2d 2d 29 3b 0a 20 20 69 66 28 20 69 69 3e 30 20 --);. if( ii>0
e53b0 29 7b 0a 20 20 20 20 7a 44 69 72 6e 61 6d 65 5b ){. zDirname[
e53c0 69 69 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 ii] = '\0';.
e53d0 66 64 20 3d 20 6f 70 65 6e 28 7a 44 69 72 6e 61 fd = open(zDirna
e53e0 6d 65 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 42 me, O_RDONLY|O_B
e53f0 49 4e 41 52 59 2c 20 30 29 3b 0a 20 20 20 20 69 INARY, 0);. i
e5400 66 28 20 66 64 3e 3d 30 20 29 7b 0a 23 69 66 64 f( fd>=0 ){.#ifd
e5410 65 66 20 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20 ef FD_CLOEXEC.
e5420 20 20 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f fcntl(fd, F_
e5430 53 45 54 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c SETFD, fcntl(fd,
e5440 20 46 5f 47 45 54 46 44 2c 20 30 29 20 7c 20 46 F_GETFD, 0) | F
e5450 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 D_CLOEXEC);.#end
e5460 69 66 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 if. OSTRACE
e5470 33 28 22 4f 50 45 4e 44 49 52 20 25 2d 33 64 20 3("OPENDIR %-3d
e5480 25 73 5c 6e 22 2c 20 66 64 2c 20 7a 44 69 72 6e %s\n", fd, zDirn
e5490 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ame);. }. }.
e54a0 20 20 2a 70 46 64 20 3d 20 66 64 3b 0a 20 20 72 *pFd = fd;. r
e54b0 65 74 75 72 6e 20 28 66 64 3e 3d 30 3f 53 51 4c eturn (fd>=0?SQL
e54c0 49 54 45 5f 4f 4b 3a 53 51 4c 49 54 45 5f 43 41 ITE_OK:SQLITE_CA
e54d0 4e 54 4f 50 45 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a NTOPEN);.}../*.*
e54e0 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d 70 6f * Create a tempo
e54f0 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 rary file name i
e5500 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 n zBuf. zBuf mu
e5510 73 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a st be allocated.
e5520 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e ** by the callin
e5530 67 20 70 72 6f 63 65 73 73 20 61 6e 64 20 6d 75 g process and mu
e5540 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75 67 68 st be big enough
e5550 20 74 6f 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 to hold at leas
e5560 74 0a 2a 2a 20 70 56 66 73 2d 3e 6d 78 50 61 74 t.** pVfs->mxPat
e5570 68 6e 61 6d 65 20 62 79 74 65 73 2e 0a 2a 2f 0a hname bytes..*/.
e5580 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 54 65 static int getTe
e5590 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c mpname(int nBuf,
e55a0 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 char *zBuf){.
e55b0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 static const cha
e55c0 72 20 2a 61 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a r *azDirs[] = {.
e55d0 20 20 20 20 20 30 2c 0a 20 20 20 20 20 30 2c 0a 0,. 0,.
e55e0 20 20 20 20 20 22 2f 76 61 72 2f 74 6d 70 22 2c "/var/tmp",
e55f0 0a 20 20 20 20 20 22 2f 75 73 72 2f 74 6d 70 22 . "/usr/tmp"
e5600 2c 0a 20 20 20 20 20 22 2f 74 6d 70 22 2c 0a 20 ,. "/tmp",.
e5610 20 20 20 20 22 2e 22 2c 0a 20 20 7d 3b 0a 20 20 ".",. };.
e5620 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 static const uns
e5630 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68 61 72 igned char zChar
e5640 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 64 65 s[] =. "abcde
e5650 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 fghijklmnopqrstu
e5660 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42 43 44 vwxyz". "ABCD
e5670 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 EFGHIJKLMNOPQRST
e5680 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30 31 32 UVWXYZ". "012
e5690 33 34 35 36 37 38 39 22 3b 0a 20 20 75 6e 73 69 3456789";. unsi
e56a0 67 6e 65 64 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 gned int i, j;.
e56b0 20 73 74 72 75 63 74 20 73 74 61 74 20 62 75 66 struct stat buf
e56c0 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ;. const char *
e56d0 7a 44 69 72 20 3d 20 22 2e 22 3b 0a 0a 20 20 2f zDir = ".";.. /
e56e0 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69 * It's odd to si
e56f0 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 mulate an io-err
e5700 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72 65 61 or here, but rea
e5710 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75 73 74 lly this is just
e5720 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 . ** using the
e5730 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74 io-error infrast
e5740 72 75 63 74 75 72 65 20 74 6f 20 74 65 73 74 20 ructure to test
e5750 74 68 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64 that SQLite hand
e5760 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 les this. ** fu
e5770 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 nction failing.
e5780 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 . */. Simulate
e5790 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 IOError( return
e57a0 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 3b 0a SQLITE_IOERR );.
e57b0 0a 20 20 61 7a 44 69 72 73 5b 30 5d 20 3d 20 73 . azDirs[0] = s
e57c0 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 qlite3_temp_dire
e57d0 63 74 6f 72 79 3b 0a 20 20 69 66 20 28 4e 55 4c ctory;. if (NUL
e57e0 4c 20 3d 3d 20 61 7a 44 69 72 73 5b 31 5d 29 20 L == azDirs[1])
e57f0 7b 0a 20 20 20 20 61 7a 44 69 72 73 5b 31 5d 20 {. azDirs[1]
e5800 3d 20 67 65 74 65 6e 76 28 22 54 4d 50 44 49 52 = getenv("TMPDIR
e5810 22 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 66 6f 72 ");. }. . for
e5820 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 (i=0; i<sizeof(a
e5830 7a 44 69 72 73 29 2f 73 69 7a 65 6f 66 28 61 7a zDirs)/sizeof(az
e5840 44 69 72 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a Dirs[0]); i++){.
e5850 20 20 20 20 69 66 28 20 61 7a 44 69 72 73 5b 69 if( azDirs[i
e5860 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b ]==0 ) continue;
e5870 0a 20 20 20 20 69 66 28 20 73 74 61 74 28 61 7a . if( stat(az
e5880 44 69 72 73 5b 69 5d 2c 20 26 62 75 66 29 20 29 Dirs[i], &buf) )
e5890 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 continue;. i
e58a0 66 28 20 21 53 5f 49 53 44 49 52 28 62 75 66 2e f( !S_ISDIR(buf.
e58b0 73 74 5f 6d 6f 64 65 29 20 29 20 63 6f 6e 74 69 st_mode) ) conti
e58c0 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 61 63 63 nue;. if( acc
e58d0 65 73 73 28 61 7a 44 69 72 73 5b 69 5d 2c 20 30 ess(azDirs[i], 0
e58e0 37 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 7) ) continue;.
e58f0 20 20 20 7a 44 69 72 20 3d 20 61 7a 44 69 72 73 zDir = azDirs
e5900 5b 69 5d 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a [i];. break;.
e5910 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 }.. /* Check
e5920 74 68 61 74 20 74 68 65 20 6f 75 74 70 75 74 20 that the output
e5930 62 75 66 66 65 72 20 69 73 20 6c 61 72 67 65 20 buffer is large
e5940 65 6e 6f 75 67 68 20 66 6f 72 20 74 68 65 20 74 enough for the t
e5950 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 0a 20 emporary file .
e5960 20 2a 2a 20 6e 61 6d 65 2e 20 49 66 20 69 74 20 ** name. If it
e5970 69 73 20 6e 6f 74 2c 20 72 65 74 75 72 6e 20 53 is not, return S
e5980 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 20 20 2a QLITE_ERROR.. *
e5990 2f 0a 20 20 69 66 28 20 28 73 74 72 6c 65 6e 28 /. if( (strlen(
e59a0 7a 44 69 72 29 20 2b 20 73 74 72 6c 65 6e 28 53 zDir) + strlen(S
e59b0 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f QLITE_TEMP_FILE_
e59c0 50 52 45 46 49 58 29 20 2b 20 31 37 29 20 3e 3d PREFIX) + 17) >=
e59d0 20 28 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 7b (size_t)nBuf ){
e59e0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
e59f0 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 TE_ERROR;. }..
e5a00 20 64 6f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 do{. sqlite3
e5a10 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 2d 31 _snprintf(nBuf-1
e5a20 37 2c 20 7a 42 75 66 2c 20 22 25 73 2f 22 53 51 7, zBuf, "%s/"SQ
e5a30 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 LITE_TEMP_FILE_P
e5a40 52 45 46 49 58 2c 20 7a 44 69 72 29 3b 0a 20 20 REFIX, zDir);.
e5a50 20 20 6a 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 j = (int)strle
e5a60 6e 28 7a 42 75 66 29 3b 0a 20 20 20 20 73 71 6c n(zBuf);. sql
e5a70 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 ite3_randomness(
e5a80 31 35 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 15, &zBuf[j]);.
e5a90 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 35 for(i=0; i<15
e5aa0 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 ; i++, j++){.
e5ab0 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 zBuf[j] = (ch
e5ac0 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 ar)zChars[ ((uns
e5ad0 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b igned char)zBuf[
e5ae0 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 j])%(sizeof(zCha
e5af0 72 73 29 2d 31 29 20 5d 3b 0a 20 20 20 20 7d 0a rs)-1) ];. }.
e5b00 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b zBuf[j] = 0;
e5b10 0a 20 20 7d 77 68 69 6c 65 28 20 61 63 63 65 73 . }while( acces
e5b20 73 28 7a 42 75 66 2c 30 29 3d 3d 30 20 29 3b 0a s(zBuf,0)==0 );.
e5b30 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e5b40 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 OK;.}..#if SQLIT
e5b50 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
e5b60 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 _STYLE && define
e5b70 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a d(__APPLE__)./*.
e5b80 2a 2a 20 52 6f 75 74 69 6e 65 20 74 6f 20 74 72 ** Routine to tr
e5b90 61 6e 73 66 6f 72 6d 20 61 20 75 6e 69 78 46 69 ansform a unixFi
e5ba0 6c 65 20 69 6e 74 6f 20 61 20 70 72 6f 78 79 2d le into a proxy-
e5bb0 6c 6f 63 6b 69 6e 67 20 75 6e 69 78 46 69 6c 65 locking unixFile
e5bc0 2e 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 ..** Implementat
e5bd0 69 6f 6e 20 69 6e 20 74 68 65 20 70 72 6f 78 79 ion in the proxy
e5be0 2d 6c 6f 63 6b 20 64 69 76 69 73 69 6f 6e 2c 20 -lock division,
e5bf0 62 75 74 20 75 73 65 64 20 62 79 20 75 6e 69 78 but used by unix
e5c00 4f 70 65 6e 28 29 0a 2a 2a 20 69 66 20 53 51 4c Open().** if SQL
e5c10 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 ITE_PREFER_PROXY
e5c20 5f 4c 4f 43 4b 49 4e 47 20 69 73 20 64 65 66 69 _LOCKING is defi
e5c30 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ned..*/.static i
e5c40 6e 74 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 nt proxyTransfor
e5c50 6d 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69 mUnixFile(unixFi
e5c60 6c 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a le*, const char*
e5c70 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a );.#endif../*.**
e5c80 20 53 65 61 72 63 68 20 66 6f 72 20 61 6e 20 75 Search for an u
e5c90 6e 75 73 65 64 20 66 69 6c 65 20 64 65 73 63 72 nused file descr
e5ca0 69 70 74 6f 72 20 74 68 61 74 20 77 61 73 20 6f iptor that was o
e5cb0 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 pened on the dat
e5cc0 61 62 61 73 65 20 0a 2a 2a 20 66 69 6c 65 20 28 abase .** file (
e5cd0 6e 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 20 6f 72 not a journal or
e5ce0 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 master-journal
e5cf0 66 69 6c 65 29 20 69 64 65 6e 74 69 66 69 65 64 file) identified
e5d00 20 62 79 20 70 61 74 68 6e 61 6d 65 0a 2a 2a 20 by pathname.**
e5d10 7a 50 61 74 68 20 77 69 74 68 20 53 51 4c 49 54 zPath with SQLIT
e5d20 45 5f 4f 50 45 4e 5f 58 58 58 20 66 6c 61 67 73 E_OPEN_XXX flags
e5d30 20 6d 61 74 63 68 69 6e 67 20 74 68 6f 73 65 20 matching those
e5d40 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 passed as the se
e5d50 63 6f 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 cond.** argument
e5d60 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f to this functio
e5d70 6e 2e 0a 2a 2a 0a 2a 2a 20 53 75 63 68 20 61 20 n..**.** Such a
e5d80 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
e5d90 6d 61 79 20 65 78 69 73 74 20 69 66 20 61 20 64 may exist if a d
e5da0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
e5db0 6f 6e 20 77 61 73 20 63 6c 6f 73 65 64 0a 2a 2a on was closed.**
e5dc0 20 62 75 74 20 74 68 65 20 61 73 73 6f 63 69 61 but the associa
e5dd0 74 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 ted file descrip
e5de0 74 6f 72 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 tor could not be
e5df0 20 63 6c 6f 73 65 64 20 62 65 63 61 75 73 65 20 closed because
e5e00 73 6f 6d 65 0a 2a 2a 20 6f 74 68 65 72 20 66 69 some.** other fi
e5e10 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 70 le descriptor op
e5e20 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 66 en on the same f
e5e30 69 6c 65 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 ile is holding a
e5e40 20 66 69 6c 65 2d 6c 6f 63 6b 2e 0a 2a 2a 20 52 file-lock..** R
e5e50 65 66 65 72 20 74 6f 20 63 6f 6d 6d 65 6e 74 73 efer to comments
e5e60 20 69 6e 20 74 68 65 20 75 6e 69 78 43 6c 6f 73 in the unixClos
e5e70 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 e() function and
e5e80 20 74 68 65 20 6c 65 6e 67 74 68 79 20 63 6f 6d the lengthy com
e5e90 6d 65 6e 74 0a 2a 2a 20 64 65 73 63 72 69 62 69 ment.** describi
e5ea0 6e 67 20 22 50 6f 73 69 78 20 41 64 76 69 73 6f ng "Posix Adviso
e5eb0 72 79 20 4c 6f 63 6b 69 6e 67 22 20 61 74 20 74 ry Locking" at t
e5ec0 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 69 73 he start of this
e5ed0 20 66 69 6c 65 20 66 6f 72 20 0a 2a 2a 20 66 75 file for .** fu
e5ee0 72 74 68 65 72 20 64 65 74 61 69 6c 73 2e 20 41 rther details. A
e5ef0 6c 73 6f 2c 20 74 69 63 6b 65 74 20 23 34 30 31 lso, ticket #401
e5f00 38 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 75 8..**.** If a su
e5f10 69 74 61 62 6c 65 20 66 69 6c 65 20 64 65 73 63 itable file desc
e5f20 72 69 70 74 6f 72 20 69 73 20 66 6f 75 6e 64 2c riptor is found,
e5f30 20 74 68 65 6e 20 69 74 20 69 73 20 72 65 74 75 then it is retu
e5f40 72 6e 65 64 2e 20 49 66 20 6e 6f 0a 2a 2a 20 73 rned. If no.** s
e5f50 75 63 68 20 66 69 6c 65 20 64 65 73 63 72 69 70 uch file descrip
e5f60 74 6f 72 20 69 73 20 6c 6f 63 61 74 65 64 2c 20 tor is located,
e5f70 2d 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a -1 is returned..
e5f80 2a 2f 0a 73 74 61 74 69 63 20 55 6e 69 78 55 6e */.static UnixUn
e5f90 75 73 65 64 46 64 20 2a 66 69 6e 64 52 65 75 73 usedFd *findReus
e5fa0 61 62 6c 65 46 64 28 63 6f 6e 73 74 20 63 68 61 ableFd(const cha
e5fb0 72 20 2a 7a 50 61 74 68 2c 20 69 6e 74 20 66 6c r *zPath, int fl
e5fc0 61 67 73 29 7b 0a 20 20 55 6e 69 78 55 6e 75 73 ags){. UnixUnus
e5fd0 65 64 46 64 20 2a 70 55 6e 75 73 65 64 20 3d 20 edFd *pUnused =
e5fe0 30 3b 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 0;.. /* Do not
e5ff0 73 65 61 72 63 68 20 66 6f 72 20 61 6e 20 75 6e search for an un
e6000 75 73 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 used file descri
e6010 70 74 6f 72 20 6f 6e 20 76 78 77 6f 72 6b 73 2e ptor on vxworks.
e6020 20 4e 6f 74 20 62 65 63 61 75 73 65 0a 20 20 2a Not because. *
e6030 2a 20 76 78 77 6f 72 6b 73 20 77 6f 75 6c 64 20 * vxworks would
e6040 6e 6f 74 20 62 65 6e 65 66 69 74 20 66 72 6f 6d not benefit from
e6050 20 74 68 65 20 63 68 61 6e 67 65 20 28 69 74 20 the change (it
e6060 6d 69 67 68 74 2c 20 77 65 27 72 65 20 6e 6f 74 might, we're not
e6070 20 73 75 72 65 29 2c 0a 20 20 2a 2a 20 62 75 74 sure),. ** but
e6080 20 62 65 63 61 75 73 65 20 6e 6f 20 77 61 79 20 because no way
e6090 74 6f 20 74 65 73 74 20 69 74 20 69 73 20 63 75 to test it is cu
e60a0 72 72 65 6e 74 6c 79 20 61 76 61 69 6c 61 62 6c rrently availabl
e60b0 65 2e 20 49 74 20 69 73 20 62 65 74 74 65 72 20 e. It is better
e60c0 0a 20 20 2a 2a 20 6e 6f 74 20 74 6f 20 72 69 73 . ** not to ris
e60d0 6b 20 62 72 65 61 6b 69 6e 67 20 76 78 77 6f 72 k breaking vxwor
e60e0 6b 73 20 73 75 70 70 6f 72 74 20 66 6f 72 20 74 ks support for t
e60f0 68 65 20 73 61 6b 65 20 6f 66 20 73 75 63 68 20 he sake of such
e6100 61 6e 20 6f 62 73 63 75 72 65 20 0a 20 20 2a 2a an obscure . **
e6110 20 66 65 61 74 75 72 65 2e 20 20 2a 2f 0a 23 69 feature. */.#i
e6120 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 f !OS_VXWORKS.
e6130 73 74 72 75 63 74 20 73 74 61 74 20 73 53 74 61 struct stat sSta
e6140 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
e6150 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 /* Results
e6160 6f 66 20 73 74 61 74 28 29 20 63 61 6c 6c 20 2a of stat() call *
e6170 2f 0a 0a 20 20 2f 2a 20 41 20 73 74 61 74 28 29 /.. /* A stat()
e6180 20 63 61 6c 6c 20 6d 61 79 20 66 61 69 6c 20 66 call may fail f
e6190 6f 72 20 76 61 72 69 6f 75 73 20 72 65 61 73 6f or various reaso
e61a0 6e 73 2e 20 49 66 20 74 68 69 73 20 68 61 70 70 ns. If this happ
e61b0 65 6e 73 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 ens, it is. **
e61c0 61 6c 6d 6f 73 74 20 63 65 72 74 61 69 6e 20 74 almost certain t
e61d0 68 61 74 20 61 6e 20 6f 70 65 6e 28 29 20 63 61 hat an open() ca
e61e0 6c 6c 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 ll on the same p
e61f0 61 74 68 20 77 69 6c 6c 20 61 6c 73 6f 20 66 61 ath will also fa
e6200 69 6c 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69 il.. ** For thi
e6210 73 20 72 65 61 73 6f 6e 2c 20 69 66 20 61 6e 20 s reason, if an
e6220 65 72 72 6f 72 20 6f 63 63 75 72 73 20 69 6e 20 error occurs in
e6230 74 68 65 20 73 74 61 74 28 29 20 63 61 6c 6c 20 the stat() call
e6240 68 65 72 65 2c 20 69 74 20 69 73 0a 20 20 2a 2a here, it is. **
e6250 20 69 67 6e 6f 72 65 64 20 61 6e 64 20 2d 31 20 ignored and -1
e6260 69 73 20 72 65 74 75 72 6e 65 64 2e 20 54 68 65 is returned. The
e6270 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 74 72 79 caller will try
e6280 20 74 6f 20 6f 70 65 6e 20 61 20 6e 65 77 20 66 to open a new f
e6290 69 6c 65 0a 20 20 2a 2a 20 64 65 73 63 72 69 70 ile. ** descrip
e62a0 74 6f 72 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 tor on the same
e62b0 70 61 74 68 2c 20 66 61 69 6c 2c 20 61 6e 64 20 path, fail, and
e62c0 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 return an error
e62d0 74 6f 20 53 51 4c 69 74 65 2e 0a 20 20 2a 2a 0a to SQLite.. **.
e62e0 20 20 2a 2a 20 45 76 65 6e 20 69 66 20 61 20 73 ** Even if a s
e62f0 75 62 73 65 71 75 65 6e 74 20 6f 70 65 6e 28 29 ubsequent open()
e6300 20 63 61 6c 6c 20 64 6f 65 73 20 73 75 63 63 65 call does succe
e6310 65 64 2c 20 74 68 65 20 63 6f 6e 73 65 71 75 65 ed, the conseque
e6320 6e 63 65 73 20 6f 66 0a 20 20 2a 2a 20 6e 6f 74 nces of. ** not
e6330 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 61 searching for a
e6340 20 72 65 73 75 73 61 62 6c 65 20 66 69 6c 65 20 resusable file
e6350 64 65 73 63 72 69 70 74 6f 72 20 61 72 65 20 6e descriptor are n
e6360 6f 74 20 64 69 72 65 2e 20 20 2a 2f 0a 20 20 69 ot dire. */. i
e6370 66 28 20 30 3d 3d 73 74 61 74 28 7a 50 61 74 68 f( 0==stat(zPath
e6380 2c 20 26 73 53 74 61 74 29 20 29 7b 0a 20 20 20 , &sStat) ){.
e6390 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e struct unixOpen
e63a0 43 6e 74 20 2a 70 4f 3b 0a 20 20 20 20 73 74 72 Cnt *pO;. str
e63b0 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 69 uct unixFileId i
e63c0 64 3b 0a 20 20 20 20 69 64 2e 64 65 76 20 3d 20 d;. id.dev =
e63d0 73 53 74 61 74 2e 73 74 5f 64 65 76 3b 0a 20 20 sStat.st_dev;.
e63e0 20 20 69 64 2e 69 6e 6f 20 3d 20 73 53 74 61 74 id.ino = sStat
e63f0 2e 73 74 5f 69 6e 6f 3b 0a 0a 20 20 20 20 75 6e .st_ino;.. un
e6400 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a ixEnterMutex();.
e6410 20 20 20 20 66 6f 72 28 70 4f 3d 6f 70 65 6e 4c for(pO=openL
e6420 69 73 74 3b 20 70 4f 20 26 26 20 6d 65 6d 63 6d ist; pO && memcm
e6430 70 28 26 69 64 2c 20 26 70 4f 2d 3e 66 69 6c 65 p(&id, &pO->file
e6440 49 64 2c 20 73 69 7a 65 6f 66 28 69 64 29 29 3b Id, sizeof(id));
e6450 20 70 4f 3d 70 4f 2d 3e 70 4e 65 78 74 29 3b 0a pO=pO->pNext);.
e6460 20 20 20 20 69 66 28 20 70 4f 20 29 7b 0a 20 20 if( pO ){.
e6470 20 20 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 UnixUnusedFd
e6480 20 2a 2a 70 70 3b 0a 20 20 20 20 20 20 66 6f 72 **pp;. for
e6490 28 70 70 3d 26 70 4f 2d 3e 70 55 6e 75 73 65 64 (pp=&pO->pUnused
e64a0 3b 20 2a 70 70 20 26 26 20 28 2a 70 70 29 2d 3e ; *pp && (*pp)->
e64b0 66 6c 61 67 73 21 3d 66 6c 61 67 73 3b 20 70 70 flags!=flags; pp
e64c0 3d 26 28 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 =&((*pp)->pNext)
e64d0 29 3b 0a 20 20 20 20 20 20 70 55 6e 75 73 65 64 );. pUnused
e64e0 20 3d 20 2a 70 70 3b 0a 20 20 20 20 20 20 69 66 = *pp;. if
e64f0 28 20 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 ( pUnused ){.
e6500 20 20 20 20 20 2a 70 70 20 3d 20 70 55 6e 75 73 *pp = pUnus
e6510 65 64 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 ed->pNext;.
e6520 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 }. }. uni
e6530 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 xLeaveMutex();.
e6540 20 7d 0a 23 65 6e 64 69 66 20 20 20 20 2f 2a 20 }.#endif /*
e6550 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53 20 2a if !OS_VXWORKS *
e6560 2f 0a 20 20 72 65 74 75 72 6e 20 70 55 6e 75 73 /. return pUnus
e6570 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 ed;.}../*.** Ope
e6580 6e 20 74 68 65 20 66 69 6c 65 20 7a 50 61 74 68 n the file zPath
e6590 2e 0a 2a 2a 20 0a 2a 2a 20 50 72 65 76 69 6f 75 ..** .** Previou
e65a0 73 6c 79 2c 20 74 68 65 20 53 51 4c 69 74 65 20 sly, the SQLite
e65b0 4f 53 20 6c 61 79 65 72 20 75 73 65 64 20 74 68 OS layer used th
e65c0 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e ree functions in
e65d0 20 70 6c 61 63 65 20 6f 66 20 74 68 69 73 0a 2a place of this.*
e65e0 2a 20 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 * one:.**.**
e65f0 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 sqlite3OsOpenRe
e6600 61 64 57 72 69 74 65 28 29 3b 0a 2a 2a 20 20 20 adWrite();.**
e6610 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 sqlite3OsOpenR
e6620 65 61 64 4f 6e 6c 79 28 29 3b 0a 2a 2a 20 20 20 eadOnly();.**
e6630 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45 sqlite3OsOpenE
e6640 78 63 6c 75 73 69 76 65 28 29 3b 0a 2a 2a 0a 2a xclusive();.**.*
e6650 2a 20 54 68 65 73 65 20 63 61 6c 6c 73 20 63 6f * These calls co
e6660 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 rrespond to the
e6670 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e following combin
e6680 61 74 69 6f 6e 73 20 6f 66 20 66 6c 61 67 73 3a ations of flags:
e6690 0a 2a 2a 0a 2a 2a 20 20 20 20 20 52 65 61 64 57 .**.** ReadW
e66a0 72 69 74 65 28 29 20 2d 3e 20 20 20 20 20 28 52 rite() -> (R
e66b0 45 41 44 57 52 49 54 45 20 7c 20 43 52 45 41 54 EADWRITE | CREAT
e66c0 45 29 0a 2a 2a 20 20 20 20 20 52 65 61 64 4f 6e E).** ReadOn
e66d0 6c 79 28 29 20 20 2d 3e 20 20 20 20 20 28 52 45 ly() -> (RE
e66e0 41 44 4f 4e 4c 59 29 20 0a 2a 2a 20 20 20 20 20 ADONLY) .**
e66f0 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20 OpenExclusive()
e6700 2d 3e 20 28 52 45 41 44 57 52 49 54 45 20 7c 20 -> (READWRITE |
e6710 43 52 45 41 54 45 20 7c 20 45 58 43 4c 55 53 49 CREATE | EXCLUSI
e6720 56 45 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c VE).**.** The ol
e6730 64 20 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 d OpenExclusive(
e6740 29 20 61 63 63 65 70 74 65 64 20 61 20 62 6f 6f ) accepted a boo
e6750 6c 65 61 6e 20 61 72 67 75 6d 65 6e 74 20 2d 20 lean argument -
e6760 22 64 65 6c 46 6c 61 67 22 2e 20 49 66 0a 2a 2a "delFlag". If.**
e6770 20 74 72 75 65 2c 20 74 68 65 20 66 69 6c 65 20 true, the file
e6780 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 20 74 was configured t
e6790 6f 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c o be automatical
e67a0 6c 79 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 ly deleted when
e67b0 74 68 65 0a 2a 2a 20 66 69 6c 65 20 68 61 6e 64 the.** file hand
e67c0 6c 65 20 63 6c 6f 73 65 64 2e 20 54 6f 20 61 63 le closed. To ac
e67d0 68 69 65 76 65 20 74 68 65 20 73 61 6d 65 20 65 hieve the same e
e67e0 66 66 65 63 74 20 75 73 69 6e 67 20 74 68 69 73 ffect using this
e67f0 20 6e 65 77 20 0a 2a 2a 20 69 6e 74 65 72 66 61 new .** interfa
e6800 63 65 2c 20 61 64 64 20 74 68 65 20 44 45 4c 45 ce, add the DELE
e6810 54 45 4f 4e 43 4c 4f 53 45 20 66 6c 61 67 20 74 TEONCLOSE flag t
e6820 6f 20 74 68 6f 73 65 20 73 70 65 63 69 66 69 65 o those specifie
e6830 64 20 61 62 6f 76 65 20 66 6f 72 20 0a 2a 2a 20 d above for .**
e6840 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 2e OpenExclusive().
e6850 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
e6860 6e 69 78 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 nixOpen(. sqlit
e6870 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 e3_vfs *pVfs,
e6880 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 /* The V
e6890 46 53 20 66 6f 72 20 77 68 69 63 68 20 74 68 69 FS for which thi
e68a0 73 20 69 73 20 74 68 65 20 78 4f 70 65 6e 20 6d s is the xOpen m
e68b0 65 74 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 ethod */. const
e68c0 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 char *zPath,
e68d0 20 20 20 20 20 20 20 20 2f 2a 20 50 61 74 68 6e /* Pathn
e68e0 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 ame of file to b
e68f0 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 73 71 e opened */. sq
e6900 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c lite3_file *pFil
e6910 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 e, /* Th
e6920 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
e6930 72 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 20 69 r to be filled i
e6940 6e 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 n */. int flags
e6950 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e6960 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 66 6c 61 /* Input fla
e6970 67 73 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 gs to control th
e6980 65 20 6f 70 65 6e 69 6e 67 20 2a 2f 0a 20 20 69 e opening */. i
e6990 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 nt *pOutFlags
e69a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
e69b0 75 74 70 75 74 20 66 6c 61 67 73 20 72 65 74 75 utput flags retu
e69c0 72 6e 65 64 20 74 6f 20 53 51 4c 69 74 65 20 63 rned to SQLite c
e69d0 6f 72 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 69 78 ore */.){. unix
e69e0 46 69 6c 65 20 2a 70 20 3d 20 28 75 6e 69 78 46 File *p = (unixF
e69f0 69 6c 65 20 2a 29 70 46 69 6c 65 3b 0a 20 20 69 ile *)pFile;. i
e6a00 6e 74 20 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 nt fd = -1;
e6a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e6a20 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 File descriptor
e6a30 20 72 65 74 75 72 6e 65 64 20 62 79 20 6f 70 65 returned by ope
e6a40 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 n() */. int dir
e6a50 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 fd = -1;
e6a60 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 /* Direc
e6a70 74 6f 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 tory file descri
e6a80 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 ptor */. int op
e6a90 65 6e 46 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 enFlags = 0;
e6aa0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 /* Flag
e6ab0 73 20 74 6f 20 70 61 73 73 20 74 6f 20 6f 70 65 s to pass to ope
e6ac0 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 n() */. int eTy
e6ad0 70 65 20 3d 20 66 6c 61 67 73 26 30 78 46 46 46 pe = flags&0xFFF
e6ae0 46 46 46 30 30 3b 20 20 2f 2a 20 54 79 70 65 20 FFF00; /* Type
e6af0 6f 66 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 of file to open
e6b00 2a 2f 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 3b */. int noLock;
e6b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e6b20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f /* True to o
e6b30 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d mit locking prim
e6b40 69 74 69 76 65 73 20 2a 2f 0a 20 20 69 6e 74 20 itives */. int
e6b50 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
e6b60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 75 /* Fu
e6b70 6e 63 74 69 6f 6e 20 52 65 74 75 72 6e 20 43 6f nction Return Co
e6b80 64 65 20 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 45 de */.. int isE
e6b90 78 63 6c 75 73 69 76 65 20 20 3d 20 28 66 6c 61 xclusive = (fla
e6ba0 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
e6bb0 5f 45 58 43 4c 55 53 49 56 45 29 3b 0a 20 20 69 _EXCLUSIVE);. i
e6bc0 6e 74 20 69 73 44 65 6c 65 74 65 20 20 20 20 20 nt isDelete
e6bd0 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 = (flags & SQLIT
e6be0 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 E_OPEN_DELETEONC
e6bf0 4c 4f 53 45 29 3b 0a 20 20 69 6e 74 20 69 73 43 LOSE);. int isC
e6c00 72 65 61 74 65 20 20 20 20 20 3d 20 28 66 6c 61 reate = (fla
e6c10 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
e6c20 5f 43 52 45 41 54 45 29 3b 0a 20 20 69 6e 74 20 _CREATE);. int
e6c30 69 73 52 65 61 64 6f 6e 6c 79 20 20 20 3d 20 28 isReadonly = (
e6c40 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
e6c50 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 PEN_READONLY);.
e6c60 20 69 6e 74 20 69 73 52 65 61 64 57 72 69 74 65 int isReadWrite
e6c70 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c = (flags & SQL
e6c80 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
e6c90 54 45 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 63 72 TE);.. /* If cr
e6ca0 65 61 74 69 6e 67 20 61 20 6d 61 73 74 65 72 20 eating a master
e6cb0 6f 72 20 6d 61 69 6e 2d 66 69 6c 65 20 6a 6f 75 or main-file jou
e6cc0 72 6e 61 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 rnal, this funct
e6cd0 69 6f 6e 20 77 69 6c 6c 20 6f 70 65 6e 0a 20 20 ion will open.
e6ce0 2a 2a 20 61 20 66 69 6c 65 2d 64 65 73 63 72 69 ** a file-descri
e6cf0 70 74 6f 72 20 6f 6e 20 74 68 65 20 64 69 72 65 ptor on the dire
e6d00 63 74 6f 72 79 20 74 6f 6f 2e 20 54 68 65 20 66 ctory too. The f
e6d10 69 72 73 74 20 74 69 6d 65 20 75 6e 69 78 53 79 irst time unixSy
e6d20 6e 63 28 29 0a 20 20 2a 2a 20 69 73 20 63 61 6c nc(). ** is cal
e6d30 6c 65 64 20 74 68 65 20 64 69 72 65 63 74 6f 72 led the director
e6d40 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f y file descripto
e6d50 72 20 77 69 6c 6c 20 62 65 20 66 73 79 6e 63 28 r will be fsync(
e6d60 29 65 64 20 61 6e 64 20 63 6c 6f 73 65 28 29 64 )ed and close()d
e6d70 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f .. */. int isO
e6d80 70 65 6e 44 69 72 65 63 74 6f 72 79 20 3d 20 28 penDirectory = (
e6d90 69 73 43 72 65 61 74 65 20 26 26 20 0a 20 20 20 isCreate && .
e6da0 20 20 20 28 65 54 79 70 65 3d 3d 53 51 4c 49 54 (eType==SQLIT
e6db0 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f E_OPEN_MASTER_JO
e6dc0 55 52 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d URNAL || eType==
e6dd0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
e6de0 5f 4a 4f 55 52 4e 41 4c 29 0a 20 20 29 3b 0a 0a _JOURNAL). );..
e6df0 20 20 2f 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 /* If argument
e6e00 20 7a 50 61 74 68 20 69 73 20 61 20 4e 55 4c 4c zPath is a NULL
e6e10 20 70 6f 69 6e 74 65 72 2c 20 74 68 69 73 20 66 pointer, this f
e6e20 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 71 75 69 unction is requi
e6e30 72 65 64 20 74 6f 20 6f 70 65 6e 0a 20 20 2a 2a red to open. **
e6e40 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c a temporary fil
e6e50 65 2e 20 55 73 65 20 74 68 69 73 20 62 75 66 66 e. Use this buff
e6e60 65 72 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 er to store the
e6e70 66 69 6c 65 20 6e 61 6d 65 20 69 6e 2e 0a 20 20 file name in..
e6e80 2a 2f 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 */. char zTmpna
e6e90 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b me[MAX_PATHNAME+
e6ea0 31 5d 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 1];. const char
e6eb0 20 2a 7a 4e 61 6d 65 20 3d 20 7a 50 61 74 68 3b *zName = zPath;
e6ec0 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 .. /* Check the
e6ed0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 following state
e6ee0 6d 65 6e 74 73 20 61 72 65 20 74 72 75 65 3a 20 ments are true:
e6ef0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 28 61 29 . **. ** (a)
e6f00 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 Exactly one of
e6f10 74 68 65 20 52 45 41 44 57 52 49 54 45 20 61 6e the READWRITE an
e6f20 64 20 52 45 41 44 4f 4e 4c 59 20 66 6c 61 67 73 d READONLY flags
e6f30 20 6d 75 73 74 20 62 65 20 73 65 74 2c 20 61 6e must be set, an
e6f40 64 20 0a 20 20 2a 2a 20 20 20 28 62 29 20 69 66 d . ** (b) if
e6f50 20 43 52 45 41 54 45 20 69 73 20 73 65 74 2c 20 CREATE is set,
e6f60 74 68 65 6e 20 52 45 41 44 57 52 49 54 45 20 6d then READWRITE m
e6f70 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2c ust also be set,
e6f80 20 61 6e 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 and. ** (c)
e6f90 69 66 20 45 58 43 4c 55 53 49 56 45 20 69 73 20 if EXCLUSIVE is
e6fa0 73 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 set, then CREATE
e6fb0 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 must also be se
e6fc0 74 2e 0a 20 20 2a 2a 20 20 20 28 64 29 20 69 66 t.. ** (d) if
e6fd0 20 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 69 DELETEONCLOSE i
e6fe0 73 20 73 65 74 2c 20 74 68 65 6e 20 43 52 45 41 s set, then CREA
e6ff0 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 TE must also be
e7000 73 65 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 set.. */. asse
e7010 72 74 28 28 69 73 52 65 61 64 6f 6e 6c 79 3d 3d rt((isReadonly==
e7020 30 20 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 0 || isReadWrite
e7030 3d 3d 30 29 20 26 26 20 28 69 73 52 65 61 64 57 ==0) && (isReadW
e7040 72 69 74 65 20 7c 7c 20 69 73 52 65 61 64 6f 6e rite || isReadon
e7050 6c 79 29 29 3b 0a 20 20 61 73 73 65 72 74 28 69 ly));. assert(i
e7060 73 43 72 65 61 74 65 3d 3d 30 20 7c 7c 20 69 73 sCreate==0 || is
e7070 52 65 61 64 57 72 69 74 65 29 3b 0a 20 20 61 73 ReadWrite);. as
e7080 73 65 72 74 28 69 73 45 78 63 6c 75 73 69 76 65 sert(isExclusive
e7090 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29 ==0 || isCreate)
e70a0 3b 0a 20 20 61 73 73 65 72 74 28 69 73 44 65 6c ;. assert(isDel
e70b0 65 74 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 ete==0 || isCrea
e70c0 74 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d te);.. /* The m
e70d0 61 69 6e 20 44 42 2c 20 6d 61 69 6e 20 6a 6f 75 ain DB, main jou
e70e0 72 6e 61 6c 2c 20 61 6e 64 20 6d 61 73 74 65 72 rnal, and master
e70f0 20 6a 6f 75 72 6e 61 6c 20 61 72 65 20 6e 65 76 journal are nev
e7100 65 72 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 er automatically
e7110 0a 20 20 2a 2a 20 64 65 6c 65 74 65 64 2e 20 4e . ** deleted. N
e7120 6f 72 20 61 72 65 20 74 68 65 79 20 65 76 65 72 or are they ever
e7130 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 temporary files
e7140 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
e7150 28 21 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e (!isDelete && zN
e7160 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 ame) || eType!=S
e7170 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
e7180 44 42 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 DB );. assert(
e7190 28 21 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e (!isDelete && zN
e71a0 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 ame) || eType!=S
e71b0 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
e71c0 4a 4f 55 52 4e 41 4c 20 29 3b 0a 20 20 61 73 73 JOURNAL );. ass
e71d0 65 72 74 28 20 28 21 69 73 44 65 6c 65 74 65 20 ert( (!isDelete
e71e0 26 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 && zName) || eTy
e71f0 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f pe!=SQLITE_OPEN_
e7200 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 29 MASTER_JOURNAL )
e7210 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 ;.. /* Assert t
e7220 68 61 74 20 74 68 65 20 75 70 70 65 72 20 6c 61 hat the upper la
e7230 79 65 72 20 68 61 73 20 73 65 74 20 6f 6e 65 20 yer has set one
e7240 6f 66 20 74 68 65 20 22 66 69 6c 65 2d 74 79 70 of the "file-typ
e7250 65 22 20 66 6c 61 67 73 2e 20 2a 2f 0a 20 20 61 e" flags. */. a
e7260 73 73 65 72 74 28 20 65 54 79 70 65 3d 3d 53 51 ssert( eType==SQ
e7270 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 LITE_OPEN_MAIN_D
e7280 42 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d B || eType=
e7290 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d =SQLITE_OPEN_TEM
e72a0 50 5f 44 42 20 0a 20 20 20 20 20 20 20 7c 7c 20 P_DB . ||
e72b0 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 eType==SQLITE_OP
e72c0 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 EN_MAIN_JOURNAL
e72d0 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 || eType==SQLITE
e72e0 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e _OPEN_TEMP_JOURN
e72f0 41 4c 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 AL . || eT
e7300 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype==SQLITE_OPEN
e7310 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 20 20 7c 7c _SUBJOURNAL ||
e7320 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f eType==SQLITE_O
e7330 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e PEN_MASTER_JOURN
e7340 41 4c 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 AL . || eT
e7350 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype==SQLITE_OPEN
e7360 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 0a 20 20 _TRANSIENT_DB.
e7370 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 );.. memset(p,
e7380 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 0, sizeof(unixFi
e7390 6c 65 29 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 le));.. if( eTy
e73a0 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f pe==SQLITE_OPEN_
e73b0 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 55 MAIN_DB ){. U
e73c0 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e nixUnusedFd *pUn
e73d0 75 73 65 64 3b 0a 20 20 20 20 70 55 6e 75 73 65 used;. pUnuse
e73e0 64 20 3d 20 66 69 6e 64 52 65 75 73 61 62 6c 65 d = findReusable
e73f0 46 64 28 7a 4e 61 6d 65 2c 20 66 6c 61 67 73 29 Fd(zName, flags)
e7400 3b 0a 20 20 20 20 69 66 28 20 70 55 6e 75 73 65 ;. if( pUnuse
e7410 64 20 29 7b 0a 20 20 20 20 20 20 66 64 20 3d 20 d ){. fd =
e7420 70 55 6e 75 73 65 64 2d 3e 66 64 3b 0a 20 20 20 pUnused->fd;.
e7430 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 55 }else{. pU
e7440 6e 75 73 65 64 20 3d 20 73 71 6c 69 74 65 33 5f nused = sqlite3_
e7450 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70 malloc(sizeof(*p
e7460 55 6e 75 73 65 64 29 29 3b 0a 20 20 20 20 20 20 Unused));.
e7470 69 66 28 20 21 70 55 6e 75 73 65 64 20 29 7b 0a if( !pUnused ){.
e7480 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
e7490 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
e74a0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 }. }. p
e74b0 2d 3e 70 55 6e 75 73 65 64 20 3d 20 70 55 6e 75 ->pUnused = pUnu
e74c0 73 65 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 sed;. }else if(
e74d0 20 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 2f !zName ){. /
e74e0 2a 20 49 66 20 7a 4e 61 6d 65 20 69 73 20 4e 55 * If zName is NU
e74f0 4c 4c 2c 20 74 68 65 20 75 70 70 65 72 20 6c 61 LL, the upper la
e7500 79 65 72 20 69 73 20 72 65 71 75 65 73 74 69 6e yer is requestin
e7510 67 20 61 20 74 65 6d 70 20 66 69 6c 65 2e 20 2a g a temp file. *
e7520 2f 0a 20 20 20 20 61 73 73 65 72 74 28 69 73 44 /. assert(isD
e7530 65 6c 65 74 65 20 26 26 20 21 69 73 4f 70 65 6e elete && !isOpen
e7540 44 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 20 20 Directory);.
e7550 72 63 20 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65 rc = getTempname
e7560 28 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 2c (MAX_PATHNAME+1,
e7570 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 zTmpname);.
e7580 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
e7590 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 K ){. retur
e75a0 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
e75b0 7a 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 zName = zTmpname
e75c0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 ;. }.. /* Dete
e75d0 72 6d 69 6e 65 20 74 68 65 20 76 61 6c 75 65 20 rmine the value
e75e0 6f 66 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 of the flags par
e75f0 61 6d 65 74 65 72 20 70 61 73 73 65 64 20 74 6f ameter passed to
e7600 20 50 4f 53 49 58 20 66 75 6e 63 74 69 6f 6e 0a POSIX function.
e7610 20 20 2a 2a 20 6f 70 65 6e 28 29 2e 20 54 68 65 ** open(). The
e7620 73 65 20 6d 75 73 74 20 62 65 20 63 61 6c 63 75 se must be calcu
e7630 6c 61 74 65 64 20 65 76 65 6e 20 69 66 20 6f 70 lated even if op
e7640 65 6e 28 29 20 69 73 20 6e 6f 74 20 63 61 6c 6c en() is not call
e7650 65 64 2c 20 61 73 0a 20 20 2a 2a 20 74 68 65 79 ed, as. ** they
e7660 20 6d 61 79 20 62 65 20 73 74 6f 72 65 64 20 61 may be stored a
e7670 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 66 69 s part of the fi
e7680 6c 65 20 68 61 6e 64 6c 65 20 61 6e 64 20 75 73 le handle and us
e7690 65 64 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 ed by the . **
e76a0 27 63 6f 6e 63 68 20 66 69 6c 65 27 20 6c 6f 63 'conch file' loc
e76b0 6b 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 20 6c king functions l
e76c0 61 74 65 72 20 6f 6e 2e 20 20 2a 2f 0a 20 20 69 ater on. */. i
e76d0 66 28 20 69 73 52 65 61 64 6f 6e 6c 79 20 29 20 f( isReadonly )
e76e0 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f openFlags |= O_
e76f0 52 44 4f 4e 4c 59 3b 0a 20 20 69 66 28 20 69 73 RDONLY;. if( is
e7700 52 65 61 64 57 72 69 74 65 20 29 20 6f 70 65 6e ReadWrite ) open
e7710 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 57 52 3b Flags |= O_RDWR;
e7720 0a 20 20 69 66 28 20 69 73 43 72 65 61 74 65 20 . if( isCreate
e7730 29 20 20 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c ) openFlags |
e7740 3d 20 4f 5f 43 52 45 41 54 3b 0a 20 20 69 66 28 = O_CREAT;. if(
e7750 20 69 73 45 78 63 6c 75 73 69 76 65 20 29 20 6f isExclusive ) o
e7760 70 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f 5f 45 penFlags |= (O_E
e7770 58 43 4c 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 29 3b XCL|O_NOFOLLOW);
e7780 0a 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 . openFlags |=
e7790 28 4f 5f 4c 41 52 47 45 46 49 4c 45 7c 4f 5f 42 (O_LARGEFILE|O_B
e77a0 49 4e 41 52 59 29 3b 0a 0a 20 20 69 66 28 20 66 INARY);.. if( f
e77b0 64 3c 30 20 29 7b 0a 20 20 20 20 6d 6f 64 65 5f d<0 ){. mode_
e77c0 74 20 6f 70 65 6e 4d 6f 64 65 20 3d 20 28 69 73 t openMode = (is
e77d0 44 65 6c 65 74 65 3f 30 36 30 30 3a 53 51 4c 49 Delete?0600:SQLI
e77e0 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f TE_DEFAULT_FILE_
e77f0 50 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 PERMISSIONS);.
e7800 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d fd = open(zNam
e7810 65 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20 6f 70 e, openFlags, op
e7820 65 6e 4d 6f 64 65 29 3b 0a 20 20 20 20 4f 53 54 enMode);. OST
e7830 52 41 43 45 34 28 22 4f 50 45 4e 58 20 20 20 25 RACE4("OPENX %
e7840 2d 33 64 20 25 73 20 30 25 6f 5c 6e 22 2c 20 66 -3d %s 0%o\n", f
e7850 64 2c 20 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c d, zName, openFl
e7860 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 66 64 ags);. if( fd
e7870 3c 30 20 26 26 20 65 72 72 6e 6f 21 3d 45 49 53 <0 && errno!=EIS
e7880 44 49 52 20 26 26 20 69 73 52 65 61 64 57 72 69 DIR && isReadWri
e7890 74 65 20 26 26 20 21 69 73 45 78 63 6c 75 73 69 te && !isExclusi
e78a0 76 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 ve ){. /* F
e78b0 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e 20 74 68 ailed to open th
e78c0 65 20 66 69 6c 65 20 66 6f 72 20 72 65 61 64 2f e file for read/
e78d0 77 72 69 74 65 20 61 63 63 65 73 73 2e 20 54 72 write access. Tr
e78e0 79 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 2a 2f 0a y read-only. */.
e78f0 20 20 20 20 20 20 66 6c 61 67 73 20 26 3d 20 7e flags &= ~
e7900 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 (SQLITE_OPEN_REA
e7910 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 DWRITE|SQLITE_OP
e7920 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20 20 20 20 EN_CREATE);.
e7930 20 20 6f 70 65 6e 46 6c 61 67 73 20 26 3d 20 7e openFlags &= ~
e7940 28 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54 29 (O_RDWR|O_CREAT)
e7950 3b 0a 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d ;. flags |=
e7960 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
e7970 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 6f 70 65 DONLY;. ope
e7980 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 4f 4e nFlags |= O_RDON
e7990 4c 59 3b 0a 20 20 20 20 20 20 66 64 20 3d 20 6f LY;. fd = o
e79a0 70 65 6e 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 pen(zName, openF
e79b0 6c 61 67 73 2c 20 6f 70 65 6e 4d 6f 64 65 29 3b lags, openMode);
e79c0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 66 . }. if( f
e79d0 64 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 d<0 ){. rc
e79e0 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 = SQLITE_CANTOPE
e79f0 4e 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 N;. goto op
e7a00 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 en_finished;.
e7a10 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 }. }. assert(
e7a20 20 66 64 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 fd>=0 );. if(
e7a30 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 pOutFlags ){.
e7a40 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c *pOutFlags = fl
e7a50 61 67 73 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 ags;. }.. if(
e7a60 70 2d 3e 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 p->pUnused ){.
e7a70 20 20 70 2d 3e 70 55 6e 75 73 65 64 2d 3e 66 64 p->pUnused->fd
e7a80 20 3d 20 66 64 3b 0a 20 20 20 20 70 2d 3e 70 55 = fd;. p->pU
e7a90 6e 75 73 65 64 2d 3e 66 6c 61 67 73 20 3d 20 66 nused->flags = f
e7aa0 6c 61 67 73 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 lags;. }.. if(
e7ab0 20 69 73 44 65 6c 65 74 65 20 29 7b 0a 23 69 66 isDelete ){.#if
e7ac0 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 OS_VXWORKS.
e7ad0 7a 50 61 74 68 20 3d 20 7a 4e 61 6d 65 3b 0a 23 zPath = zName;.#
e7ae0 65 6c 73 65 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 else. unlink(
e7af0 7a 4e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 zName);.#endif.
e7b00 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e }.#if SQLITE_EN
e7b10 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
e7b20 4c 45 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 70 LE. else{. p
e7b30 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 3d 20 6f 70 ->openFlags = op
e7b40 65 6e 46 6c 61 67 73 3b 0a 20 20 7d 0a 23 65 6e enFlags;. }.#en
e7b50 64 69 66 0a 0a 20 20 69 66 28 20 69 73 4f 70 65 dif.. if( isOpe
e7b60 6e 44 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 nDirectory ){.
e7b70 20 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 rc = openDirec
e7b80 74 6f 72 79 28 7a 50 61 74 68 2c 20 26 64 69 72 tory(zPath, &dir
e7b90 66 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 fd);. if( rc!
e7ba0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
e7bb0 20 20 20 20 2f 2a 20 49 74 20 69 73 20 73 61 66 /* It is saf
e7bc0 65 20 74 6f 20 63 6c 6f 73 65 20 66 64 20 61 74 e to close fd at
e7bd0 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 62 65 63 this point, bec
e7be0 61 75 73 65 20 69 74 20 69 73 20 67 75 61 72 61 ause it is guara
e7bf0 6e 74 65 65 64 20 6e 6f 74 0a 20 20 20 20 20 20 nteed not.
e7c00 2a 2a 20 74 6f 20 62 65 20 6f 70 65 6e 20 6f 6e ** to be open on
e7c10 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 a database file
e7c20 2e 20 49 66 20 69 74 20 77 65 72 65 20 6f 70 65 . If it were ope
e7c30 6e 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 n on a database
e7c40 66 69 6c 65 2c 0a 20 20 20 20 20 20 2a 2a 20 69 file,. ** i
e7c50 74 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 t would not be s
e7c60 61 66 65 20 74 6f 20 63 6c 6f 73 65 20 61 73 20 afe to close as
e7c70 74 68 69 73 20 77 6f 75 6c 64 20 72 65 6c 65 61 this would relea
e7c80 73 65 20 61 6e 79 20 6c 6f 63 6b 73 20 68 65 6c se any locks hel
e7c90 64 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 d. ** on th
e7ca0 65 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 70 e file by this p
e7cb0 72 6f 63 65 73 73 2e 20 20 2a 2f 0a 20 20 20 20 rocess. */.
e7cc0 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 assert( eType!
e7cd0 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 =SQLITE_OPEN_MAI
e7ce0 4e 5f 44 42 20 29 3b 0a 20 20 20 20 20 20 63 6c N_DB );. cl
e7cf0 6f 73 65 28 66 64 29 3b 20 20 20 20 20 20 20 20 ose(fd);
e7d00 20 20 20 20 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 /* silently
e7d10 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 61 leak if fail, a
e7d20 6c 72 65 61 64 79 20 69 6e 20 65 72 72 6f 72 20 lready in error
e7d30 2a 2f 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 */. goto op
e7d40 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 en_finished;.
e7d50 20 7d 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 46 }. }..#ifdef F
e7d60 44 5f 43 4c 4f 45 58 45 43 0a 20 20 66 63 6e 74 D_CLOEXEC. fcnt
e7d70 6c 28 66 64 2c 20 46 5f 53 45 54 46 44 2c 20 66 l(fd, F_SETFD, f
e7d80 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 cntl(fd, F_GETFD
e7d90 2c 20 30 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 , 0) | FD_CLOEXE
e7da0 43 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 6e 6f C);.#endif.. no
e7db0 4c 6f 63 6b 20 3d 20 65 54 79 70 65 21 3d 53 51 Lock = eType!=SQ
e7dc0 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 LITE_OPEN_MAIN_D
e7dd0 42 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 50 B;..#if SQLITE_P
e7de0 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b REFER_PROXY_LOCK
e7df0 49 4e 47 0a 20 20 69 66 28 20 7a 50 61 74 68 21 ING. if( zPath!
e7e00 3d 4e 55 4c 4c 20 26 26 20 21 6e 6f 4c 6f 63 6b =NULL && !noLock
e7e10 20 26 26 20 70 56 66 73 2d 3e 78 4f 70 65 6e 20 && pVfs->xOpen
e7e20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 65 6e 76 ){. char *env
e7e30 66 6f 72 63 65 20 3d 20 67 65 74 65 6e 76 28 22 force = getenv("
e7e40 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f SQLITE_FORCE_PRO
e7e50 58 59 5f 4c 4f 43 4b 49 4e 47 22 29 3b 0a 20 20 XY_LOCKING");.
e7e60 20 20 69 6e 74 20 75 73 65 50 72 6f 78 79 20 3d int useProxy =
e7e70 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 51 4c 49 0;.. /* SQLI
e7e80 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c TE_FORCE_PROXY_L
e7e90 4f 43 4b 49 4e 47 3d 3d 31 20 6d 65 61 6e 73 20 OCKING==1 means
e7ea0 66 6f 72 63 65 20 61 6c 77 61 79 73 20 75 73 65 force always use
e7eb0 20 70 72 6f 78 79 2c 20 30 20 6d 65 61 6e 73 20 proxy, 0 means
e7ec0 0a 20 20 20 20 2a 2a 20 6e 65 76 65 72 20 75 73 . ** never us
e7ed0 65 20 70 72 6f 78 79 2c 20 4e 55 4c 4c 20 6d 65 e proxy, NULL me
e7ee0 61 6e 73 20 75 73 65 20 70 72 6f 78 79 20 66 6f ans use proxy fo
e7ef0 72 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 r non-local file
e7f00 73 20 6f 6e 6c 79 2e 20 20 2a 2f 0a 20 20 20 20 s only. */.
e7f10 69 66 28 20 65 6e 76 66 6f 72 63 65 21 3d 4e 55 if( envforce!=NU
e7f20 4c 4c 20 29 7b 0a 20 20 20 20 20 20 75 73 65 50 LL ){. useP
e7f30 72 6f 78 79 20 3d 20 61 74 6f 69 28 65 6e 76 66 roxy = atoi(envf
e7f40 6f 72 63 65 29 3e 30 3b 0a 20 20 20 20 7d 65 6c orce)>0;. }el
e7f50 73 65 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 se{. struct
e7f60 20 73 74 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a statfs fsInfo;.
e7f70 20 20 20 20 20 20 69 66 28 20 73 74 61 74 66 73 if( statfs
e7f80 28 7a 50 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 (zPath, &fsInfo)
e7f90 20 3d 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 == -1 ){.
e7fa0 20 20 2f 2a 20 49 6e 20 74 68 65 6f 72 79 2c 20 /* In theory,
e7fb0 74 68 65 20 63 6c 6f 73 65 28 66 64 29 20 63 61 the close(fd) ca
e7fc0 6c 6c 20 69 73 20 73 75 62 2d 6f 70 74 69 6d 61 ll is sub-optima
e7fd0 6c 2e 20 49 66 20 74 68 65 20 66 69 6c 65 20 6f l. If the file o
e7fe0 70 65 6e 65 64 0a 20 20 20 20 20 20 20 20 2a 2a pened. **
e7ff0 20 77 69 74 68 20 66 64 20 69 73 20 61 20 64 61 with fd is a da
e8000 74 61 62 61 73 65 20 66 69 6c 65 2c 20 61 6e 64 tabase file, and
e8010 20 74 68 65 72 65 20 61 72 65 20 6f 74 68 65 72 there are other
e8020 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 70 65 connections ope
e8030 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 n. ** on
e8040 74 68 61 74 20 66 69 6c 65 20 74 68 61 74 20 61 that file that a
e8050 72 65 20 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c re currently hol
e8060 64 69 6e 67 20 61 64 76 69 73 6f 72 79 20 6c 6f ding advisory lo
e8070 63 6b 73 20 6f 6e 20 69 74 2c 0a 20 20 20 20 20 cks on it,.
e8080 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 ** then the c
e8090 61 6c 6c 20 74 6f 20 63 6c 6f 73 65 28 29 20 77 all to close() w
e80a0 69 6c 6c 20 63 61 6e 63 65 6c 20 74 68 6f 73 65 ill cancel those
e80b0 20 6c 6f 63 6b 73 2e 20 49 6e 20 70 72 61 63 74 locks. In pract
e80c0 69 63 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 ice,. **
e80d0 77 65 27 72 65 20 61 73 73 75 6d 69 6e 67 20 74 we're assuming t
e80e0 68 61 74 20 73 74 61 74 66 73 28 29 20 64 6f 65 hat statfs() doe
e80f0 73 6e 27 74 20 66 61 69 6c 20 76 65 72 79 20 6f sn't fail very o
e8100 66 74 65 6e 2e 20 41 74 20 6c 65 61 73 74 0a 20 ften. At least.
e8110 20 20 20 20 20 20 20 2a 2a 20 6e 6f 74 20 77 68 ** not wh
e8120 69 6c 65 20 6f 74 68 65 72 20 66 69 6c 65 20 64 ile other file d
e8130 65 73 63 72 69 70 74 6f 72 73 20 6f 70 65 6e 65 escriptors opene
e8140 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 d by the same pr
e8150 6f 63 65 73 73 20 6f 6e 0a 20 20 20 20 20 20 20 ocess on.
e8160 20 2a 2a 20 74 68 65 20 73 61 6d 65 20 66 69 6c ** the same fil
e8170 65 20 61 72 65 20 77 6f 72 6b 69 6e 67 2e 20 20 e are working.
e8180 2a 2f 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c 61 */. p->la
e8190 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b stErrno = errno;
e81a0 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 69 72 . if( dir
e81b0 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 fd>=0 ){.
e81c0 20 20 20 63 6c 6f 73 65 28 64 69 72 66 64 29 3b close(dirfd);
e81d0 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 /* silently lea
e81e0 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e 20 65 72 k if fail, in er
e81f0 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d ror */. }
e8200 0a 20 20 20 20 20 20 20 20 63 6c 6f 73 65 28 66 . close(f
e8210 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 d); /* silently
e8220 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e leak if fail, in
e8230 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 error */.
e8240 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
e8250 45 52 52 5f 41 43 43 45 53 53 3b 0a 20 20 20 20 ERR_ACCESS;.
e8260 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 goto open_fi
e8270 6e 69 73 68 65 64 3b 0a 20 20 20 20 20 20 7d 0a nished;. }.
e8280 20 20 20 20 20 20 75 73 65 50 72 6f 78 79 20 3d useProxy =
e8290 20 21 28 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 !(fsInfo.f_flag
e82a0 73 26 4d 4e 54 5f 4c 4f 43 41 4c 29 3b 0a 20 20 s&MNT_LOCAL);.
e82b0 20 20 7d 0a 20 20 20 20 69 66 28 20 75 73 65 50 }. if( useP
e82c0 72 6f 78 79 20 29 7b 0a 20 20 20 20 20 20 72 63 roxy ){. rc
e82d0 20 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c = fillInUnixFil
e82e0 65 28 70 56 66 73 2c 20 66 64 2c 20 64 69 72 66 e(pVfs, fd, dirf
e82f0 64 2c 20 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c d, pFile, zPath,
e8300 20 6e 6f 4c 6f 63 6b 2c 20 69 73 44 65 6c 65 74 noLock, isDelet
e8310 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 e);. if( rc
e8320 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
e8330 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 rc = prox
e8340 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 yTransformUnixFi
e8350 6c 65 28 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 le((unixFile*)pF
e8360 69 6c 65 2c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a ile, ":auto:");.
e8370 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f }. go
e8380 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 to open_finished
e8390 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 ;. }. }.#end
e83a0 69 66 0a 20 20 0a 20 20 72 63 20 3d 20 66 69 6c if. . rc = fil
e83b0 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70 56 66 73 lInUnixFile(pVfs
e83c0 2c 20 66 64 2c 20 64 69 72 66 64 2c 20 70 46 69 , fd, dirfd, pFi
e83d0 6c 65 2c 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63 le, zPath, noLoc
e83e0 6b 2c 20 69 73 44 65 6c 65 74 65 29 3b 0a 6f 70 k, isDelete);.op
e83f0 65 6e 5f 66 69 6e 69 73 68 65 64 3a 0a 20 20 69 en_finished:. i
e8400 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
e8410 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
e8420 66 72 65 65 28 70 2d 3e 70 55 6e 75 73 65 64 29 free(p->pUnused)
e8430 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
e8440 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c c;.}.../*.** Del
e8450 65 74 65 20 74 68 65 20 66 69 6c 65 20 61 74 20 ete the file at
e8460 7a 50 61 74 68 2e 20 49 66 20 74 68 65 20 64 69 zPath. If the di
e8470 72 53 79 6e 63 20 61 72 67 75 6d 65 6e 74 20 69 rSync argument i
e8480 73 20 74 72 75 65 2c 20 66 73 79 6e 63 28 29 0a s true, fsync().
e8490 2a 2a 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 ** the directory
e84a0 20 61 66 74 65 72 20 64 65 6c 65 74 69 6e 67 20 after deleting
e84b0 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 the file..*/.sta
e84c0 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65 6c 65 tic int unixDele
e84d0 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 te(. sqlite3_vf
e84e0 73 20 2a 4e 6f 74 55 73 65 64 2c 20 20 20 20 20 s *NotUsed,
e84f0 2f 2a 20 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e /* VFS containin
e8500 67 20 74 68 69 73 20 61 73 20 74 68 65 20 78 44 g this as the xD
e8510 65 6c 65 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a elete method */.
e8520 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 const char *zP
e8530 61 74 68 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e ath, /* N
e8540 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 ame of file to b
e8550 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 e deleted */. i
e8560 6e 74 20 64 69 72 53 79 6e 63 20 20 20 20 20 20 nt dirSync
e8570 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 /* If t
e8580 72 75 65 2c 20 66 73 79 6e 63 28 29 20 64 69 72 rue, fsync() dir
e8590 65 63 74 6f 72 79 20 61 66 74 65 72 20 64 65 6c ectory after del
e85a0 65 74 69 6e 67 20 66 69 6c 65 20 2a 2f 0a 29 7b eting file */.){
e85b0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
e85c0 54 45 5f 4f 4b 3b 0a 20 20 55 4e 55 53 45 44 5f TE_OK;. UNUSED_
e85d0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
e85e0 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f d);. SimulateIO
e85f0 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c Error(return SQL
e8600 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 ITE_IOERR_DELETE
e8610 29 3b 0a 20 20 75 6e 6c 69 6e 6b 28 7a 50 61 74 );. unlink(zPat
e8620 68 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 h);.#ifndef SQLI
e8630 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 TE_DISABLE_DIRSY
e8640 4e 43 0a 20 20 69 66 28 20 64 69 72 53 79 6e 63 NC. if( dirSync
e8650 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 64 3b 0a ){. int fd;.
e8660 20 20 20 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 rc = openDir
e8670 65 63 74 6f 72 79 28 7a 50 61 74 68 2c 20 26 66 ectory(zPath, &f
e8680 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d d);. if( rc==
e8690 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 SQLITE_OK ){.#if
e86a0 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 OS_VXWORKS.
e86b0 20 20 69 66 28 20 66 73 79 6e 63 28 66 64 29 3d if( fsync(fd)=
e86c0 3d 2d 31 20 29 0a 23 65 6c 73 65 0a 20 20 20 20 =-1 ).#else.
e86d0 20 20 69 66 28 20 66 73 79 6e 63 28 66 64 29 20 if( fsync(fd)
e86e0 29 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b ).#endif. {
e86f0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
e8700 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 LITE_IOERR_DIR_F
e8710 53 59 4e 43 3b 0a 20 20 20 20 20 20 7d 0a 20 20 SYNC;. }.
e8720 20 20 20 20 69 66 28 20 63 6c 6f 73 65 28 66 64 if( close(fd
e8730 29 26 26 21 72 63 20 29 7b 0a 20 20 20 20 20 20 )&&!rc ){.
e8740 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
e8750 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 ERR_DIR_CLOSE;.
e8760 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
e8770 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e .#endif. return
e8780 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 rc;.}../*.** Te
e8790 73 74 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 st the existance
e87a0 20 6f 66 20 6f 72 20 61 63 63 65 73 73 20 70 65 of or access pe
e87b0 72 6d 69 73 73 69 6f 6e 73 20 6f 66 20 66 69 6c rmissions of fil
e87c0 65 20 7a 50 61 74 68 2e 20 54 68 65 0a 2a 2a 20 e zPath. The.**
e87d0 74 65 73 74 20 70 65 72 66 6f 72 6d 65 64 20 64 test performed d
e87e0 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 epends on the va
e87f0 6c 75 65 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a lue of flags:.**
e8800 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41 .** SQLITE_A
e8810 43 43 45 53 53 5f 45 58 49 53 54 53 3a 20 52 65 CCESS_EXISTS: Re
e8820 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 66 69 turn 1 if the fi
e8830 6c 65 20 65 78 69 73 74 73 0a 2a 2a 20 20 20 20 le exists.**
e8840 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 SQLITE_ACCESS_R
e8850 45 41 44 57 52 49 54 45 3a 20 52 65 74 75 72 6e EADWRITE: Return
e8860 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 1 if the file i
e8870 73 20 72 65 61 64 20 61 6e 64 20 77 72 69 74 61 s read and writa
e8880 62 6c 65 2e 0a 2a 2a 20 20 20 20 20 53 51 4c 49 ble..** SQLI
e8890 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 4f 4e TE_ACCESS_READON
e88a0 4c 59 3a 20 52 65 74 75 72 6e 20 31 20 69 66 20 LY: Return 1 if
e88b0 74 68 65 20 66 69 6c 65 20 69 73 20 72 65 61 64 the file is read
e88c0 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 able..**.** Othe
e88d0 72 77 69 73 65 20 72 65 74 75 72 6e 20 30 2e 0a rwise return 0..
e88e0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
e88f0 69 78 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 ixAccess(. sqli
e8900 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 te3_vfs *NotUsed
e8910 2c 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 63 , /* The VFS c
e8920 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73 20 78 ontaining this x
e8930 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 2a 2f Access method */
e8940 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
e8950 50 61 74 68 2c 20 20 20 20 20 20 2f 2a 20 50 61 Path, /* Pa
e8960 74 68 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 th of the file t
e8970 6f 20 65 78 61 6d 69 6e 65 20 2a 2f 0a 20 20 69 o examine */. i
e8980 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 nt flags,
e8990 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 64 /* What d
e89a0 6f 20 77 65 20 77 61 6e 74 20 74 6f 20 6c 65 61 o we want to lea
e89b0 72 6e 20 61 62 6f 75 74 20 74 68 65 20 7a 50 61 rn about the zPa
e89c0 74 68 20 66 69 6c 65 3f 20 2a 2f 0a 20 20 69 6e th file? */. in
e89d0 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20 20 20 t *pResOut
e89e0 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 72 /* Write r
e89f0 65 73 75 6c 74 20 62 6f 6f 6c 65 61 6e 20 68 65 esult boolean he
e8a00 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 re */.){. int a
e8a10 6d 6f 64 65 20 3d 20 30 3b 0a 20 20 55 4e 55 53 mode = 0;. UNUS
e8a20 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
e8a30 55 73 65 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 Used);. Simulat
e8a40 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e eIOError( return
e8a50 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 SQLITE_IOERR_AC
e8a60 43 45 53 53 3b 20 29 3b 0a 20 20 73 77 69 74 63 CESS; );. switc
e8a70 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 h( flags ){.
e8a80 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 case SQLITE_ACCE
e8a90 53 53 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20 SS_EXISTS:.
e8aa0 20 61 6d 6f 64 65 20 3d 20 46 5f 4f 4b 3b 0a 20 amode = F_OK;.
e8ab0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
e8ac0 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 case SQLITE_ACCE
e8ad0 53 53 5f 52 45 41 44 57 52 49 54 45 3a 0a 20 20 SS_READWRITE:.
e8ae0 20 20 20 20 61 6d 6f 64 65 20 3d 20 57 5f 4f 4b amode = W_OK
e8af0 7c 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 |R_OK;. bre
e8b00 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c ak;. case SQL
e8b10 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a ITE_ACCESS_READ:
e8b20 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 52 . amode = R
e8b30 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b _OK;. break
e8b40 3b 0a 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a ;.. default:.
e8b50 20 20 20 20 20 20 61 73 73 65 72 74 28 21 22 49 assert(!"I
e8b60 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 nvalid flags arg
e8b70 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a ument");. }. *
e8b80 70 52 65 73 4f 75 74 20 3d 20 28 61 63 63 65 73 pResOut = (acces
e8b90 73 28 7a 50 61 74 68 2c 20 61 6d 6f 64 65 29 3d s(zPath, amode)=
e8ba0 3d 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 =0);. return SQ
e8bb0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a LITE_OK;.}.../*.
e8bc0 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74 69 ** Turn a relati
e8bd0 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f ve pathname into
e8be0 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 a full pathname
e8bf0 2e 20 54 68 65 20 72 65 6c 61 74 69 76 65 20 70 . The relative p
e8c00 61 74 68 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 ath.** is stored
e8c10 20 61 73 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e as a nul-termin
e8c20 61 74 65 64 20 73 74 72 69 6e 67 20 69 6e 20 74 ated string in t
e8c30 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 he buffer pointe
e8c40 64 20 74 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68 d to by.** zPath
e8c50 2e 20 0a 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f . .**.** zOut po
e8c60 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66 65 72 ints to a buffer
e8c70 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 71 6c of at least sql
e8c80 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e ite3_vfs.mxPathn
e8c90 61 6d 65 20 62 79 74 65 73 20 0a 2a 2a 20 28 69 ame bytes .** (i
e8ca0 6e 20 74 68 69 73 20 63 61 73 65 2c 20 4d 41 58 n this case, MAX
e8cb0 5f 50 41 54 48 4e 41 4d 45 20 62 79 74 65 73 29 _PATHNAME bytes)
e8cc0 2e 20 54 68 65 20 66 75 6c 6c 2d 70 61 74 68 20 . The full-path
e8cd0 69 73 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a is written to.**
e8ce0 20 74 68 69 73 20 62 75 66 66 65 72 20 62 65 66 this buffer bef
e8cf0 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a ore returning..*
e8d00 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
e8d10 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 xFullPathname(.
e8d20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
e8d30 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f fs, /
e8d40 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 * Pointer to vfs
e8d50 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e object */. con
e8d60 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 st char *zPath,
e8d70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f /* Po
e8d80 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 ssibly relative
e8d90 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 input path */.
e8da0 69 6e 74 20 6e 4f 75 74 2c 20 20 20 20 20 20 20 int nOut,
e8db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e8dc0 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 Size of output
e8dd0 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 buffer in bytes
e8de0 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 */. char *zOut
e8df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e8e00 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 /* Output buf
e8e10 66 65 72 20 2a 2f 0a 29 7b 0a 0a 20 20 2f 2a 20 fer */.){.. /*
e8e20 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 It's odd to simu
e8e30 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 late an io-error
e8e40 20 68 65 72 65 2c 20 62 75 74 20 72 65 61 6c 6c here, but reall
e8e50 79 20 74 68 69 73 20 69 73 20 6a 75 73 74 0a 20 y this is just.
e8e60 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 69 6f ** using the io
e8e70 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74 72 75 -error infrastru
e8e80 63 74 75 72 65 20 74 6f 20 74 65 73 74 20 74 68 cture to test th
e8e90 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 at SQLite handle
e8ea0 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 s this. ** func
e8eb0 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 54 68 tion failing. Th
e8ec0 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6c is function coul
e8ed0 64 20 66 61 69 6c 20 69 66 2c 20 66 6f 72 20 65 d fail if, for e
e8ee0 78 61 6d 70 6c 65 2c 20 74 68 65 0a 20 20 2a 2a xample, the. **
e8ef0 20 63 75 72 72 65 6e 74 20 77 6f 72 6b 69 6e 67 current working
e8f00 20 64 69 72 65 63 74 6f 72 79 20 68 61 73 20 62 directory has b
e8f10 65 65 6e 20 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20 een unlinked..
e8f20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 */. SimulateIOE
e8f30 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
e8f40 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a 0a 20 20 ITE_ERROR );..
e8f50 61 73 73 65 72 74 28 20 70 56 66 73 2d 3e 6d 78 assert( pVfs->mx
e8f60 50 61 74 68 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41 Pathname==MAX_PA
e8f70 54 48 4e 41 4d 45 20 29 3b 0a 20 20 55 4e 55 53 THNAME );. UNUS
e8f80 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 ED_PARAMETER(pVf
e8f90 73 29 3b 0a 0a 20 20 7a 4f 75 74 5b 6e 4f 75 74 s);.. zOut[nOut
e8fa0 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 -1] = '\0';. if
e8fb0 28 20 7a 50 61 74 68 5b 30 5d 3d 3d 27 2f 27 20 ( zPath[0]=='/'
e8fc0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 ){. sqlite3_s
e8fd0 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2c 20 7a 4f nprintf(nOut, zO
e8fe0 75 74 2c 20 22 25 73 22 2c 20 7a 50 61 74 68 29 ut, "%s", zPath)
e8ff0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
e9000 6e 74 20 6e 43 77 64 3b 0a 20 20 20 20 69 66 28 nt nCwd;. if(
e9010 20 67 65 74 63 77 64 28 7a 4f 75 74 2c 20 6e 4f getcwd(zOut, nO
e9020 75 74 2d 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 ut-1)==0 ){.
e9030 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e9040 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a CANTOPEN;. }.
e9050 20 20 20 20 6e 43 77 64 20 3d 20 28 69 6e 74 29 nCwd = (int)
e9060 73 74 72 6c 65 6e 28 7a 4f 75 74 29 3b 0a 20 20 strlen(zOut);.
e9070 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
e9080 74 66 28 6e 4f 75 74 2d 6e 43 77 64 2c 20 26 7a tf(nOut-nCwd, &z
e9090 4f 75 74 5b 6e 43 77 64 5d 2c 20 22 2f 25 73 22 Out[nCwd], "/%s"
e90a0 2c 20 7a 50 61 74 68 29 3b 0a 20 20 7d 0a 20 20 , zPath);. }.
e90b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
e90c0 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 ;.}...#ifndef SQ
e90d0 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 LITE_OMIT_LOAD_E
e90e0 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 XTENSION./*.** I
e90f0 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 nterfaces for op
e9100 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c ening a shared l
e9110 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 ibrary, finding
e9120 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 entry points.**
e9130 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 within the share
e9140 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 d library, and c
e9150 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 losing the share
e9160 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 d library..*/.#i
e9170 6e 63 6c 75 64 65 20 3c 64 6c 66 63 6e 2e 68 3e nclude <dlfcn.h>
e9180 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 75 6e .static void *un
e9190 69 78 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 ixDlOpen(sqlite3
e91a0 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 63 _vfs *NotUsed, c
e91b0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
e91c0 6e 61 6d 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f name){. UNUSED_
e91d0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
e91e0 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 64 6c 6f d);. return dlo
e91f0 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 52 pen(zFilename, R
e9200 54 4c 44 5f 4e 4f 57 20 7c 20 52 54 4c 44 5f 47 TLD_NOW | RTLD_G
e9210 4c 4f 42 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a LOBAL);.}../*.**
e9220 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 SQLite calls th
e9230 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 6d 65 is function imme
e9240 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 61 20 diately after a
e9250 63 61 6c 6c 20 74 6f 20 75 6e 69 78 44 6c 53 79 call to unixDlSy
e9260 6d 28 29 20 6f 72 0a 2a 2a 20 75 6e 69 78 44 6c m() or.** unixDl
e9270 4f 70 65 6e 28 29 20 66 61 69 6c 73 20 28 72 65 Open() fails (re
e9280 74 75 72 6e 73 20 61 20 6e 75 6c 6c 20 70 6f 69 turns a null poi
e9290 6e 74 65 72 29 2e 20 49 66 20 61 20 6d 6f 72 65 nter). If a more
e92a0 20 64 65 74 61 69 6c 65 64 20 65 72 72 6f 72 0a detailed error.
e92b0 2a 2a 20 6d 65 73 73 61 67 65 20 69 73 20 61 76 ** message is av
e92c0 61 69 6c 61 62 6c 65 2c 20 69 74 20 69 73 20 77 ailable, it is w
e92d0 72 69 74 74 65 6e 20 74 6f 20 7a 42 75 66 4f 75 ritten to zBufOu
e92e0 74 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 6d t. If no error m
e92f0 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 61 76 61 essage.** is ava
e9300 69 6c 61 62 6c 65 2c 20 7a 42 75 66 4f 75 74 20 ilable, zBufOut
e9310 69 73 20 6c 65 66 74 20 75 6e 6d 6f 64 69 66 69 is left unmodifi
e9320 65 64 20 61 6e 64 20 53 51 4c 69 74 65 20 75 73 ed and SQLite us
e9330 65 73 20 61 20 64 65 66 61 75 6c 74 0a 2a 2a 20 es a default.**
e9340 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a error message..*
e9350 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e /.static void un
e9360 69 78 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 ixDlError(sqlite
e9370 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 3_vfs *NotUsed,
e9380 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a int nBuf, char *
e9390 7a 42 75 66 4f 75 74 29 7b 0a 20 20 63 68 61 72 zBufOut){. char
e93a0 20 2a 7a 45 72 72 3b 0a 20 20 55 4e 55 53 45 44 *zErr;. UNUSED
e93b0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
e93c0 65 64 29 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 ed);. unixEnter
e93d0 4d 75 74 65 78 28 29 3b 0a 20 20 7a 45 72 72 20 Mutex();. zErr
e93e0 3d 20 64 6c 65 72 72 6f 72 28 29 3b 0a 20 20 69 = dlerror();. i
e93f0 66 28 20 7a 45 72 72 20 29 7b 0a 20 20 20 20 73 f( zErr ){. s
e9400 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
e9410 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74 2c 20 22 nBuf, zBufOut, "
e9420 25 73 22 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a %s", zErr);. }.
e9430 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 unixLeaveMutex
e9440 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 ();.}.static voi
e9450 64 20 28 2a 75 6e 69 78 44 6c 53 79 6d 28 73 71 d (*unixDlSym(sq
e9460 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 lite3_vfs *NotUs
e9470 65 64 2c 20 76 6f 69 64 20 2a 70 2c 20 63 6f 6e ed, void *p, con
e9480 73 74 20 63 68 61 72 2a 7a 53 79 6d 29 29 28 76 st char*zSym))(v
e9490 6f 69 64 29 7b 0a 20 20 2f 2a 20 0a 20 20 2a 2a oid){. /* . **
e94a0 20 47 43 43 20 77 69 74 68 20 2d 70 65 64 61 6e GCC with -pedan
e94b0 74 69 63 2d 65 72 72 6f 72 73 20 73 61 79 73 20 tic-errors says
e94c0 74 68 61 74 20 43 39 30 20 64 6f 65 73 20 6e 6f that C90 does no
e94d0 74 20 61 6c 6c 6f 77 20 61 20 76 6f 69 64 2a 20 t allow a void*
e94e0 74 6f 20 62 65 0a 20 20 2a 2a 20 63 61 73 74 20 to be. ** cast
e94f0 69 6e 74 6f 20 61 20 70 6f 69 6e 74 65 72 20 74 into a pointer t
e9500 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 o a function. A
e9510 6e 64 20 79 65 74 20 74 68 65 20 6c 69 62 72 61 nd yet the libra
e9520 72 79 20 64 6c 73 79 6d 28 29 20 72 6f 75 74 69 ry dlsym() routi
e9530 6e 65 0a 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 ne. ** returns
e9540 61 20 76 6f 69 64 2a 20 77 68 69 63 68 20 69 73 a void* which is
e9550 20 72 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 really a pointe
e9560 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e r to a function.
e9570 20 20 53 6f 20 68 6f 77 20 64 6f 20 77 65 0a 20 So how do we.
e9580 20 2a 2a 20 75 73 65 20 64 6c 73 79 6d 28 29 20 ** use dlsym()
e9590 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 with -pedantic-e
e95a0 72 72 6f 72 73 3f 0a 20 20 2a 2a 0a 20 20 2a 2a rrors?. **. **
e95b0 20 56 61 72 69 61 62 6c 65 20 78 20 62 65 6c 6f Variable x belo
e95c0 77 20 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20 w is defined to
e95d0 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 be a pointer to
e95e0 61 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 69 6e a function takin
e95f0 67 0a 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 g. ** parameter
e9600 73 20 76 6f 69 64 2a 20 61 6e 64 20 63 6f 6e 73 s void* and cons
e9610 74 20 63 68 61 72 2a 20 61 6e 64 20 72 65 74 75 t char* and retu
e9620 72 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 rning a pointer
e9630 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 to a function..
e9640 20 2a 2a 20 57 65 20 69 6e 69 74 69 61 6c 69 7a ** We initializ
e9650 65 20 78 20 62 79 20 61 73 73 69 67 6e 69 6e 67 e x by assigning
e9660 20 69 74 20 61 20 70 6f 69 6e 74 65 72 20 74 6f it a pointer to
e9670 20 74 68 65 20 64 6c 73 79 6d 28 29 20 66 75 6e the dlsym() fun
e9680 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 28 54 68 61 ction.. ** (Tha
e9690 74 20 61 73 73 69 67 6e 6d 65 6e 74 20 72 65 71 t assignment req
e96a0 75 69 72 65 73 20 61 20 63 61 73 74 2e 29 20 20 uires a cast.)
e96b0 54 68 65 6e 20 77 65 20 63 61 6c 6c 20 74 68 65 Then we call the
e96c0 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 0a 20 function that.
e96d0 20 2a 2a 20 78 20 70 6f 69 6e 74 73 20 74 6f 2e ** x points to.
e96e0 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 . **. ** Thi
e96f0 73 20 77 6f 72 6b 2d 61 72 6f 75 6e 64 20 69 73 s work-around is
e9700 20 75 6e 6c 69 6b 65 6c 79 20 74 6f 20 77 6f 72 unlikely to wor
e9710 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 61 k correctly on a
e9720 6e 79 20 73 79 73 74 65 6d 20 77 68 65 72 65 0a ny system where.
e9730 20 20 2a 2a 20 79 6f 75 20 72 65 61 6c 6c 79 20 ** you really
e9740 63 61 6e 6e 6f 74 20 63 61 73 74 20 61 20 66 75 cannot cast a fu
e9750 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 69 nction pointer i
e9760 6e 74 6f 20 76 6f 69 64 2a 2e 20 20 42 75 74 20 nto void*. But
e9770 74 68 65 6e 2c 20 6f 6e 20 74 68 65 0a 20 20 2a then, on the. *
e9780 2a 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 64 6c * other hand, dl
e9790 73 79 6d 28 29 20 77 69 6c 6c 20 6e 6f 74 20 77 sym() will not w
e97a0 6f 72 6b 20 6f 6e 20 73 75 63 68 20 61 20 73 79 ork on such a sy
e97b0 73 74 65 6d 20 65 69 74 68 65 72 2c 20 73 6f 20 stem either, so
e97c0 77 65 20 68 61 76 65 0a 20 20 2a 2a 20 6e 6f 74 we have. ** not
e97d0 20 72 65 61 6c 6c 79 20 6c 6f 73 74 20 61 6e 79 really lost any
e97e0 74 68 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 76 6f thing.. */. vo
e97f0 69 64 20 28 2a 28 2a 78 29 28 76 6f 69 64 2a 2c id (*(*x)(void*,
e9800 63 6f 6e 73 74 20 63 68 61 72 2a 29 29 28 76 6f const char*))(vo
e9810 69 64 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 id);. UNUSED_PA
e9820 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
e9830 3b 0a 20 20 78 20 3d 20 28 76 6f 69 64 28 2a 28 ;. x = (void(*(
e9840 2a 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 *)(void*,const c
e9850 68 61 72 2a 29 29 28 76 6f 69 64 29 29 64 6c 73 har*))(void))dls
e9860 79 6d 3b 0a 20 20 72 65 74 75 72 6e 20 28 2a 78 ym;. return (*x
e9870 29 28 70 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 73 74 )(p, zSym);.}.st
e9880 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 44 6c atic void unixDl
e9890 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 Close(sqlite3_vf
e98a0 73 20 2a 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 s *NotUsed, void
e98b0 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e *pHandle){. UN
e98c0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
e98d0 6f 74 55 73 65 64 29 3b 0a 20 20 64 6c 63 6c 6f otUsed);. dlclo
e98e0 73 65 28 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 se(pHandle);.}.#
e98f0 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 else /* if SQLIT
e9900 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
e9910 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 NSION is defined
e9920 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 75 : */. #define u
e9930 6e 69 78 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 nixDlOpen 0. #
e9940 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 45 72 72 define unixDlErr
e9950 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 75 or 0. #define u
e9960 6e 69 78 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 nixDlSym 0. #
e9970 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 43 6c 6f define unixDlClo
e9980 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a se 0.#endif../*.
e9990 2a 2a 20 57 72 69 74 65 20 6e 42 75 66 20 62 79 ** Write nBuf by
e99a0 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 20 64 61 tes of random da
e99b0 74 61 20 74 6f 20 74 68 65 20 73 75 70 70 6c 69 ta to the suppli
e99c0 65 64 20 62 75 66 66 65 72 20 7a 42 75 66 2e 0a ed buffer zBuf..
e99d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
e99e0 69 78 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c ixRandomness(sql
e99f0 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 ite3_vfs *NotUse
e9a00 64 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 d, int nBuf, cha
e9a10 72 20 2a 7a 42 75 66 29 7b 0a 20 20 55 4e 55 53 r *zBuf){. UNUS
e9a20 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
e9a30 55 73 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28 Used);. assert(
e9a40 28 73 69 7a 65 5f 74 29 6e 42 75 66 3e 3d 28 73 (size_t)nBuf>=(s
e9a50 69 7a 65 6f 66 28 74 69 6d 65 5f 74 29 2b 73 69 izeof(time_t)+si
e9a60 7a 65 6f 66 28 69 6e 74 29 29 29 3b 0a 0a 20 20 zeof(int)));..
e9a70 2f 2a 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e /* We have to in
e9a80 69 74 69 61 6c 69 7a 65 20 7a 42 75 66 20 74 6f itialize zBuf to
e9a90 20 70 72 65 76 65 6e 74 20 76 61 6c 67 72 69 6e prevent valgrin
e9aa0 64 20 66 72 6f 6d 20 72 65 70 6f 72 74 69 6e 67 d from reporting
e9ab0 0a 20 20 2a 2a 20 65 72 72 6f 72 73 2e 20 20 54 . ** errors. T
e9ac0 68 65 20 72 65 70 6f 72 74 73 20 69 73 73 75 65 he reports issue
e9ad0 64 20 62 79 20 76 61 6c 67 72 69 6e 64 20 61 72 d by valgrind ar
e9ae0 65 20 69 6e 63 6f 72 72 65 63 74 20 2d 20 77 65 e incorrect - we
e9af0 20 77 6f 75 6c 64 0a 20 20 2a 2a 20 70 72 65 66 would. ** pref
e9b00 65 72 20 74 68 61 74 20 74 68 65 20 72 61 6e 64 er that the rand
e9b10 6f 6d 6e 65 73 73 20 62 65 20 69 6e 63 72 65 61 omness be increa
e9b20 73 65 64 20 62 79 20 6d 61 6b 69 6e 67 20 75 73 sed by making us
e9b30 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 75 6e e of the. ** un
e9b40 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 70 61 63 initialized spac
e9b50 65 20 69 6e 20 7a 42 75 66 20 2d 20 62 75 74 20 e in zBuf - but
e9b60 76 61 6c 67 72 69 6e 64 20 65 72 72 6f 72 73 20 valgrind errors
e9b70 74 65 6e 64 20 74 6f 20 77 6f 72 72 79 0a 20 20 tend to worry.
e9b80 2a 2a 20 73 6f 6d 65 20 75 73 65 72 73 2e 20 20 ** some users.
e9b90 52 61 74 68 65 72 20 74 68 61 6e 20 61 72 67 75 Rather than argu
e9ba0 65 2c 20 69 74 20 73 65 65 6d 73 20 65 61 73 69 e, it seems easi
e9bb0 65 72 20 6a 75 73 74 20 74 6f 20 69 6e 69 74 69 er just to initi
e9bc0 61 6c 69 7a 65 0a 20 20 2a 2a 20 74 68 65 20 77 alize. ** the w
e9bd0 68 6f 6c 65 20 61 72 72 61 79 20 61 6e 64 20 73 hole array and s
e9be0 69 6c 65 6e 63 65 20 76 61 6c 67 72 69 6e 64 2c ilence valgrind,
e9bf0 20 65 76 65 6e 20 69 66 20 74 68 61 74 20 6d 65 even if that me
e9c00 61 6e 73 20 6c 65 73 73 20 72 61 6e 64 6f 6d 6e ans less randomn
e9c10 65 73 73 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 ess. ** in the
e9c20 72 61 6e 64 6f 6d 20 73 65 65 64 2e 0a 20 20 2a random seed.. *
e9c30 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 74 65 73 74 *. ** When test
e9c40 69 6e 67 2c 20 69 6e 69 74 69 61 6c 69 7a 69 6e ing, initializin
e9c50 67 20 7a 42 75 66 5b 5d 20 74 6f 20 7a 65 72 6f g zBuf[] to zero
e9c60 20 69 73 20 61 6c 6c 20 77 65 20 64 6f 2e 20 20 is all we do.
e9c70 54 68 61 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 That means. **
e9c80 74 68 61 74 20 77 65 20 61 6c 77 61 79 73 20 75 that we always u
e9c90 73 65 20 74 68 65 20 73 61 6d 65 20 72 61 6e 64 se the same rand
e9ca0 6f 6d 20 6e 75 6d 62 65 72 20 73 65 71 75 65 6e om number sequen
e9cb0 63 65 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20 ce. This makes
e9cc0 74 68 65 0a 20 20 2a 2a 20 74 65 73 74 73 20 72 the. ** tests r
e9cd0 65 70 65 61 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a epeatable.. */.
e9ce0 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 memset(zBuf, 0
e9cf0 2c 20 6e 42 75 66 29 3b 0a 23 69 66 20 21 64 65 , nBuf);.#if !de
e9d00 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 fined(SQLITE_TES
e9d10 54 29 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 70 T). {. int p
e9d20 69 64 2c 20 66 64 3b 0a 20 20 20 20 66 64 20 3d id, fd;. fd =
e9d30 20 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e open("/dev/uran
e9d40 64 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b dom", O_RDONLY);
e9d50 0a 20 20 20 20 69 66 28 20 66 64 3c 30 20 29 7b . if( fd<0 ){
e9d60 0a 20 20 20 20 20 20 74 69 6d 65 5f 74 20 74 3b . time_t t;
e9d70 0a 20 20 20 20 20 20 74 69 6d 65 28 26 74 29 3b . time(&t);
e9d80 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 42 . memcpy(zB
e9d90 75 66 2c 20 26 74 2c 20 73 69 7a 65 6f 66 28 74 uf, &t, sizeof(t
e9da0 29 29 3b 0a 20 20 20 20 20 20 70 69 64 20 3d 20 ));. pid =
e9db0 67 65 74 70 69 64 28 29 3b 0a 20 20 20 20 20 20 getpid();.
e9dc0 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 73 69 7a memcpy(&zBuf[siz
e9dd0 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 2c 20 73 eof(t)], &pid, s
e9de0 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 izeof(pid));.
e9df0 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f assert( sizeo
e9e00 66 28 74 29 2b 73 69 7a 65 6f 66 28 70 69 64 29 f(t)+sizeof(pid)
e9e10 3c 3d 28 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 <=(size_t)nBuf )
e9e20 3b 0a 20 20 20 20 20 20 6e 42 75 66 20 3d 20 73 ;. nBuf = s
e9e30 69 7a 65 6f 66 28 74 29 20 2b 20 73 69 7a 65 6f izeof(t) + sizeo
e9e40 66 28 70 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 f(pid);. }els
e9e50 65 7b 0a 20 20 20 20 20 20 6e 42 75 66 20 3d 20 e{. nBuf =
e9e60 72 65 61 64 28 66 64 2c 20 7a 42 75 66 2c 20 6e read(fd, zBuf, n
e9e70 42 75 66 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 Buf);. clos
e9e80 65 28 66 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d e(fd);. }. }
e9e90 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e .#endif. return
e9ea0 20 6e 42 75 66 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a nBuf;.}.../*.**
e9eb0 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74 Sleep for a lit
e9ec0 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75 tle while. Retu
e9ed0 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 rn the amount of
e9ee0 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2a 20 time slept..**
e9ef0 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 The argument is
e9f00 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 the number of mi
e9f10 63 72 6f 73 65 63 6f 6e 64 73 20 77 65 20 77 61 croseconds we wa
e9f20 6e 74 20 74 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20 nt to sleep..**
e9f30 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
e9f40 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
e9f50 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 6f f microseconds o
e9f60 66 20 73 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 f sleep actually
e9f70 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 20 66 72 .** requested fr
e9f80 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e om the underlyin
e9f90 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 g operating syst
e9fa0 65 6d 2c 20 61 20 6e 75 6d 62 65 72 20 77 68 69 em, a number whi
e9fb0 63 68 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 67 ch.** might be g
e9fc0 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 reater than or e
e9fd0 71 75 61 6c 20 74 6f 20 74 68 65 20 61 72 67 75 qual to the argu
e9fe0 6d 65 6e 74 2c 20 62 75 74 20 6e 6f 74 20 6c 65 ment, but not le
e9ff0 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 ss.** than the a
ea000 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 rgument..*/.stat
ea010 69 63 20 69 6e 74 20 75 6e 69 78 53 6c 65 65 70 ic int unixSleep
ea020 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f (sqlite3_vfs *No
ea030 74 55 73 65 64 2c 20 69 6e 74 20 6d 69 63 72 6f tUsed, int micro
ea040 73 65 63 6f 6e 64 73 29 7b 0a 23 69 66 20 4f 53 seconds){.#if OS
ea050 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 _VXWORKS. struc
ea060 74 20 74 69 6d 65 73 70 65 63 20 73 70 3b 0a 0a t timespec sp;..
ea070 20 20 73 70 2e 74 76 5f 73 65 63 20 3d 20 6d 69 sp.tv_sec = mi
ea080 63 72 6f 73 65 63 6f 6e 64 73 20 2f 20 31 30 30 croseconds / 100
ea090 30 30 30 30 3b 0a 20 20 73 70 2e 74 76 5f 6e 73 0000;. sp.tv_ns
ea0a0 65 63 20 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e ec = (microsecon
ea0b0 64 73 20 25 20 31 30 30 30 30 30 30 29 20 2a 20 ds % 1000000) *
ea0c0 31 30 30 30 3b 0a 20 20 6e 61 6e 6f 73 6c 65 65 1000;. nanoslee
ea0d0 70 28 26 73 70 2c 20 4e 55 4c 4c 29 3b 0a 20 20 p(&sp, NULL);.
ea0e0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
ea0f0 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 (NotUsed);. ret
ea100 75 72 6e 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 urn microseconds
ea110 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 ;.#elif defined(
ea120 48 41 56 45 5f 55 53 4c 45 45 50 29 20 26 26 20 HAVE_USLEEP) &&
ea130 48 41 56 45 5f 55 53 4c 45 45 50 0a 20 20 75 73 HAVE_USLEEP. us
ea140 6c 65 65 70 28 6d 69 63 72 6f 73 65 63 6f 6e 64 leep(microsecond
ea150 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 s);. UNUSED_PAR
ea160 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
ea170 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 . return micros
ea180 65 63 6f 6e 64 73 3b 0a 23 65 6c 73 65 0a 20 20 econds;.#else.
ea190 69 6e 74 20 73 65 63 6f 6e 64 73 20 3d 20 28 6d int seconds = (m
ea1a0 69 63 72 6f 73 65 63 6f 6e 64 73 2b 39 39 39 39 icroseconds+9999
ea1b0 39 39 29 2f 31 30 30 30 30 30 30 3b 0a 20 20 73 99)/1000000;. s
ea1c0 6c 65 65 70 28 73 65 63 6f 6e 64 73 29 3b 0a 20 leep(seconds);.
ea1d0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
ea1e0 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 R(NotUsed);. re
ea1f0 74 75 72 6e 20 73 65 63 6f 6e 64 73 2a 31 30 30 turn seconds*100
ea200 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0000;.#endif.}..
ea210 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
ea220 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 ing variable, if
ea230 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 set to a non-ze
ea240 72 6f 20 76 61 6c 75 65 2c 20 69 73 20 69 6e 74 ro value, is int
ea250 65 72 70 72 65 74 65 64 20 61 73 0a 2a 2a 20 74 erpreted as.** t
ea260 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 63 he number of sec
ea270 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 20 onds since 1970
ea280 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 73 and is used to s
ea290 65 74 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 et the result of
ea2a0 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 43 75 72 .** sqlite3OsCur
ea2b0 72 65 6e 74 54 69 6d 65 28 29 20 64 75 72 69 6e rentTime() durin
ea2c0 67 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 g testing..*/.#i
ea2d0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
ea2e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
ea2f0 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f sqlite3_current_
ea300 74 69 6d 65 20 3d 20 30 3b 20 20 2f 2a 20 46 61 time = 0; /* Fa
ea310 6b 65 20 73 79 73 74 65 6d 20 74 69 6d 65 20 69 ke system time i
ea320 6e 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 n seconds since
ea330 31 39 37 30 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 1970. */.#endif.
ea340 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 ./*.** Find the
ea350 63 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e current time (in
ea360 20 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 Universal Coord
ea370 69 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 inated Time). W
ea380 72 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 rite the.** curr
ea390 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 ent time and dat
ea3a0 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 e as a Julian Da
ea3b0 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 y number into *p
ea3c0 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 rNow and.** retu
ea3d0 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 rn 0. Return 1
ea3e0 69 66 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 if the time and
ea3f0 64 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 date cannot be f
ea400 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ound..*/.static
ea410 69 6e 74 20 75 6e 69 78 43 75 72 72 65 6e 74 54 int unixCurrentT
ea420 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ime(sqlite3_vfs
ea430 2a 4e 6f 74 55 73 65 64 2c 20 64 6f 75 62 6c 65 *NotUsed, double
ea440 20 2a 70 72 4e 6f 77 29 7b 0a 23 69 66 20 64 65 *prNow){.#if de
ea450 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
ea460 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
ea470 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 ). time_t t;.
ea480 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a 70 72 4e time(&t);. *prN
ea490 6f 77 20 3d 20 28 28 28 73 71 6c 69 74 65 33 5f ow = (((sqlite3_
ea4a0 69 6e 74 36 34 29 74 29 2f 38 36 34 30 20 2b 20 int64)t)/8640 +
ea4b0 32 34 34 30 35 38 37 35 29 2f 31 30 3b 0a 23 65 24405875)/10;.#e
ea4c0 6c 69 66 20 64 65 66 69 6e 65 64 28 4e 4f 5f 47 lif defined(NO_G
ea4d0 45 54 54 4f 44 29 0a 20 20 74 69 6d 65 5f 74 20 ETTOD). time_t
ea4e0 74 3b 0a 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 t;. time(&t);.
ea4f0 20 2a 70 72 4e 6f 77 20 3d 20 74 2f 38 36 34 30 *prNow = t/8640
ea500 30 2e 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0.0 + 2440587.5;
ea510 0a 23 65 6c 69 66 20 4f 53 5f 56 58 57 4f 52 4b .#elif OS_VXWORK
ea520 53 0a 20 20 73 74 72 75 63 74 20 74 69 6d 65 73 S. struct times
ea530 70 65 63 20 73 4e 6f 77 3b 0a 20 20 63 6c 6f 63 pec sNow;. cloc
ea540 6b 5f 67 65 74 74 69 6d 65 28 43 4c 4f 43 4b 5f k_gettime(CLOCK_
ea550 52 45 41 4c 54 49 4d 45 2c 20 26 73 4e 6f 77 29 REALTIME, &sNow)
ea560 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 ;. *prNow = 244
ea570 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 0587.5 + sNow.tv
ea580 5f 73 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 _sec/86400.0 + s
ea590 4e 6f 77 2e 74 76 5f 6e 73 65 63 2f 38 36 34 30 Now.tv_nsec/8640
ea5a0 30 30 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 0000000000.0;.#e
ea5b0 6c 73 65 0a 20 20 73 74 72 75 63 74 20 74 69 6d lse. struct tim
ea5c0 65 76 61 6c 20 73 4e 6f 77 3b 0a 20 20 67 65 74 eval sNow;. get
ea5d0 74 69 6d 65 6f 66 64 61 79 28 26 73 4e 6f 77 2c timeofday(&sNow,
ea5e0 20 30 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 0);. *prNow =
ea5f0 32 34 34 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2440587.5 + sNow
ea600 2e 74 76 5f 73 65 63 2f 38 36 34 30 30 2e 30 20 .tv_sec/86400.0
ea610 2b 20 73 4e 6f 77 2e 74 76 5f 75 73 65 63 2f 38 + sNow.tv_usec/8
ea620 36 34 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6400000000.0;.#e
ea630 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c ndif..#ifdef SQL
ea640 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 73 ITE_TEST. if( s
ea650 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 qlite3_current_t
ea660 69 6d 65 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f ime ){. *prNo
ea670 77 20 3d 20 73 71 6c 69 74 65 33 5f 63 75 72 72 w = sqlite3_curr
ea680 65 6e 74 5f 74 69 6d 65 2f 38 36 34 30 30 2e 30 ent_time/86400.0
ea690 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 20 20 + 2440587.5;.
ea6a0 7d 0a 23 65 6e 64 69 66 0a 20 20 55 4e 55 53 45 }.#endif. UNUSE
ea6b0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
ea6c0 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 sed);. return 0
ea6d0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 61 64 ;.}../*.** We ad
ea6e0 64 65 64 20 74 68 65 20 78 47 65 74 4c 61 73 74 ded the xGetLast
ea6f0 45 72 72 6f 72 28 29 20 6d 65 74 68 6f 64 20 77 Error() method w
ea700 69 74 68 20 74 68 65 20 69 6e 74 65 6e 74 69 6f ith the intentio
ea710 6e 20 6f 66 20 70 72 6f 76 69 64 69 6e 67 0a 2a n of providing.*
ea720 2a 20 62 65 74 74 65 72 20 6c 6f 77 2d 6c 65 76 * better low-lev
ea730 65 6c 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 el error message
ea740 73 20 77 68 65 6e 20 6f 70 65 72 61 74 69 6e 67 s when operating
ea750 2d 73 79 73 74 65 6d 20 70 72 6f 62 6c 65 6d 73 -system problems
ea760 20 63 6f 6d 65 20 75 70 0a 2a 2a 20 64 75 72 69 come up.** duri
ea770 6e 67 20 53 51 4c 69 74 65 20 6f 70 65 72 61 74 ng SQLite operat
ea780 69 6f 6e 2e 20 20 42 75 74 20 73 6f 20 66 61 72 ion. But so far
ea790 2c 20 6e 6f 6e 65 20 6f 66 20 74 68 61 74 20 68 , none of that h
ea7a0 61 73 20 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e as been implemen
ea7b0 74 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f ted.** in the co
ea7c0 72 65 2e 20 20 53 6f 20 74 68 69 73 20 72 6f 75 re. So this rou
ea7d0 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 tine is never ca
ea7e0 6c 6c 65 64 2e 20 20 46 6f 72 20 6e 6f 77 2c 20 lled. For now,
ea7f0 69 74 20 69 73 20 6d 65 72 65 6c 79 0a 2a 2a 20 it is merely.**
ea800 61 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72 2e 0a a place-holder..
ea810 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
ea820 69 78 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 ixGetLastError(s
ea830 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 qlite3_vfs *NotU
ea840 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 sed, int NotUsed
ea850 32 2c 20 63 68 61 72 20 2a 4e 6f 74 55 73 65 64 2, char *NotUsed
ea860 33 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 3){. UNUSED_PAR
ea870 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
ea880 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
ea890 54 45 52 28 4e 6f 74 55 73 65 64 32 29 3b 0a 20 TER(NotUsed2);.
ea8a0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
ea8b0 52 28 4e 6f 74 55 73 65 64 33 29 3b 0a 20 20 72 R(NotUsed3);. r
ea8c0 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
ea8d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea8e0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 ******* End of s
ea8f0 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f qlite3_vfs metho
ea900 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ds *************
ea910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
ea920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
ea970 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
ea980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea9a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
ea9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
ea9e0 20 50 72 6f 78 79 20 4c 6f 63 6b 69 6e 67 20 2a Proxy Locking *
ea9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
eaa10 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b **.** Proxy lock
eaa20 69 6e 67 20 69 73 20 61 20 22 75 62 65 72 2d 6c ing is a "uber-l
eaa30 6f 63 6b 69 6e 67 2d 6d 65 74 68 6f 64 22 20 69 ocking-method" i
eaa40 6e 20 74 68 69 73 20 73 65 6e 73 65 3a 20 20 49 n this sense: I
eaa50 74 20 75 73 65 73 20 74 68 65 0a 2a 2a 20 6f 74 t uses the.** ot
eaa60 68 65 72 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 her locking meth
eaa70 6f 64 73 20 6f 6e 20 73 65 63 6f 6e 64 61 72 79 ods on secondary
eaa80 20 6c 6f 63 6b 20 66 69 6c 65 73 2e 20 20 50 72 lock files. Pr
eaa90 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 oxy locking is a
eaaa0 0a 2a 2a 20 6d 65 74 61 2d 6c 61 79 65 72 20 6f .** meta-layer o
eaab0 76 65 72 20 74 6f 70 20 6f 66 20 74 68 65 20 70 ver top of the p
eaac0 72 69 6d 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67 rimitive locking
eaad0 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 62 6f implemented abo
eaae0 76 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 69 73 ve. For.** this
eaaf0 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 reason, the div
eab00 69 73 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 ision that imple
eab10 6d 65 6e 74 73 20 6f 66 20 70 72 6f 78 79 20 6c ments of proxy l
eab20 6f 63 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 ocking is deferr
eab30 65 64 0a 2a 2a 20 75 6e 74 69 6c 20 6c 61 74 65 ed.** until late
eab40 20 69 6e 20 74 68 65 20 66 69 6c 65 20 28 68 65 in the file (he
eab50 72 65 29 20 61 66 74 65 72 20 61 6c 6c 20 6f 66 re) after all of
eab60 20 74 68 65 20 6f 74 68 65 72 20 49 2f 4f 20 6d the other I/O m
eab70 65 74 68 6f 64 73 20 68 61 76 65 0a 2a 2a 20 62 ethods have.** b
eab80 65 65 6e 20 64 65 66 69 6e 65 64 20 2d 20 73 6f een defined - so
eab90 20 74 68 61 74 20 74 68 65 20 70 72 69 6d 69 74 that the primit
eaba0 69 76 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 ive locking meth
eabb0 6f 64 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c ods are availabl
eabc0 65 0a 2a 2a 20 61 73 20 73 65 72 76 69 63 65 73 e.** as services
eabd0 20 74 6f 20 68 65 6c 70 20 77 69 74 68 20 74 68 to help with th
eabe0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
eabf0 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e of proxy lockin
eac00 67 2e 0a 2a 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a g..**.****.**.**
eac10 20 54 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 The default loc
eac20 6b 69 6e 67 20 73 63 68 65 6d 65 73 20 69 6e 20 king schemes in
eac30 53 51 4c 69 74 65 20 75 73 65 20 62 79 74 65 2d SQLite use byte-
eac40 72 61 6e 67 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 range locks on t
eac50 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 he.** database f
eac60 69 6c 65 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 ile to coordinat
eac70 65 20 73 61 66 65 2c 20 63 6f 6e 63 75 72 72 65 e safe, concurre
eac80 6e 74 20 61 63 63 65 73 73 20 62 79 20 6d 75 6c nt access by mul
eac90 74 69 70 6c 65 20 72 65 61 64 65 72 73 0a 2a 2a tiple readers.**
eaca0 20 61 6e 64 20 77 72 69 74 65 72 73 20 5b 68 74 and writers [ht
eacb0 74 70 3a 2f 2f 73 71 6c 69 74 65 2e 6f 72 67 2f tp://sqlite.org/
eacc0 6c 6f 63 6b 69 6e 67 76 33 2e 68 74 6d 6c 5d 2e lockingv3.html].
eacd0 20 20 54 68 65 20 66 69 76 65 20 66 69 6c 65 20 The five file
eace0 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 61 74 65 locking.** state
eacf0 73 20 28 55 4e 4c 4f 43 4b 45 44 2c 20 50 45 4e s (UNLOCKED, PEN
ead00 44 49 4e 47 2c 20 53 48 41 52 45 44 2c 20 52 45 DING, SHARED, RE
ead10 53 45 52 56 45 44 2c 20 45 58 43 4c 55 53 49 56 SERVED, EXCLUSIV
ead20 45 29 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 E) are implement
ead30 65 64 0a 2a 2a 20 61 73 20 50 4f 53 49 58 20 72 ed.** as POSIX r
ead40 65 61 64 20 26 20 77 72 69 74 65 20 6c 6f 63 6b ead & write lock
ead50 73 20 6f 76 65 72 20 66 69 78 65 64 20 73 65 74 s over fixed set
ead60 20 6f 66 20 6c 6f 63 61 74 69 6f 6e 73 20 28 76 of locations (v
ead70 69 61 20 66 73 63 74 6c 29 2c 0a 2a 2a 20 6f 6e ia fsctl),.** on
ead80 20 41 46 50 20 61 6e 64 20 53 4d 42 20 6f 6e 6c AFP and SMB onl
ead90 79 20 65 78 63 6c 75 73 69 76 65 20 62 79 74 65 y exclusive byte
eada0 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65 -range locks are
eadb0 20 61 76 61 69 6c 61 62 6c 65 20 76 69 61 20 66 available via f
eadc0 73 63 74 6c 0a 2a 2a 20 77 69 74 68 20 5f 49 4f sctl.** with _IO
eadd0 57 52 28 27 7a 27 2c 20 32 33 2c 20 73 74 72 75 WR('z', 23, stru
eade0 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b ct ByteRangeLock
eadf0 50 42 32 29 20 74 6f 20 74 72 61 63 6b 20 74 68 PB2) to track th
eae00 65 20 73 61 6d 65 20 35 20 73 74 61 74 65 73 2e e same 5 states.
eae10 0a 2a 2a 20 54 6f 20 73 69 6d 75 6c 61 74 65 20 .** To simulate
eae20 61 20 46 5f 52 44 4c 43 4b 20 6f 6e 20 74 68 65 a F_RDLCK on the
eae30 20 73 68 61 72 65 64 20 72 61 6e 67 65 2c 20 6f shared range, o
eae40 6e 20 41 46 50 20 61 20 72 61 6e 64 6f 6d 6c 79 n AFP a randomly
eae50 20 73 65 6c 65 63 74 65 64 0a 2a 2a 20 61 64 64 selected.** add
eae60 72 65 73 73 20 69 6e 20 74 68 65 20 73 68 61 72 ress in the shar
eae70 65 64 20 72 61 6e 67 65 20 69 73 20 74 61 6b 65 ed range is take
eae80 6e 20 66 6f 72 20 61 20 53 48 41 52 45 44 20 6c n for a SHARED l
eae90 6f 63 6b 2c 20 74 68 65 20 65 6e 74 69 72 65 0a ock, the entire.
eaea0 2a 2a 20 73 68 61 72 65 64 20 72 61 6e 67 65 20 ** shared range
eaeb0 69 73 20 74 61 6b 65 6e 20 66 6f 72 20 61 6e 20 is taken for an
eaec0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 29 3a EXCLUSIVE lock):
eaed0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 50 45 4e 44 .**.** PEND
eaee0 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20 20 20 ING_BYTE
eaef0 30 78 34 30 30 30 30 30 30 30 09 09 20 20 20 09 0x40000000.. .
eaf00 0a 2a 2a 20 20 20 20 20 20 52 45 53 45 52 56 45 .** RESERVE
eaf10 44 5f 42 59 54 45 20 20 20 20 20 20 20 30 78 34 D_BYTE 0x4
eaf20 30 30 30 30 30 30 31 0a 2a 2a 20 20 20 20 20 20 0000001.**
eaf30 53 48 41 52 45 44 5f 52 41 4e 47 45 20 20 20 20 SHARED_RANGE
eaf40 20 20 20 20 30 78 34 30 30 30 30 30 30 32 20 2d 0x40000002 -
eaf50 3e 20 30 78 34 30 30 30 30 32 30 30 0a 2a 2a 0a > 0x40000200.**.
eaf60 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 77 65 ** This works we
eaf70 6c 6c 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 ll on the local
eaf80 66 69 6c 65 20 73 79 73 74 65 6d 2c 20 62 75 74 file system, but
eaf90 20 73 68 6f 77 73 20 61 20 6e 65 61 72 6c 79 20 shows a nearly
eafa0 31 30 30 78 0a 2a 2a 20 73 6c 6f 77 64 6f 77 6e 100x.** slowdown
eafb0 20 69 6e 20 72 65 61 64 20 70 65 72 66 6f 72 6d in read perform
eafc0 61 6e 63 65 20 6f 6e 20 41 46 50 20 62 65 63 61 ance on AFP beca
eafd0 75 73 65 20 74 68 65 20 41 46 50 20 63 6c 69 65 use the AFP clie
eafe0 6e 74 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 74 nt disables.** t
eaff0 68 65 20 72 65 61 64 20 63 61 63 68 65 20 77 68 he read cache wh
eb000 65 6e 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f en byte-range lo
eb010 63 6b 73 20 61 72 65 20 70 72 65 73 65 6e 74 2e cks are present.
eb020 20 20 45 6e 61 62 6c 69 6e 67 20 74 68 65 20 72 Enabling the r
eb030 65 61 64 0a 2a 2a 20 63 61 63 68 65 20 65 78 70 ead.** cache exp
eb040 6f 73 65 73 20 61 20 63 61 63 68 65 20 63 6f 68 oses a cache coh
eb050 65 72 65 6e 63 79 20 70 72 6f 62 6c 65 6d 20 74 erency problem t
eb060 68 61 74 20 69 73 20 70 72 65 73 65 6e 74 20 6f hat is present o
eb070 6e 20 61 6c 6c 20 4f 53 20 58 0a 2a 2a 20 73 75 n all OS X.** su
eb080 70 70 6f 72 74 65 64 20 6e 65 74 77 6f 72 6b 20 pported network
eb090 66 69 6c 65 20 73 79 73 74 65 6d 73 2e 20 20 4e file systems. N
eb0a0 46 53 20 61 6e 64 20 41 46 50 20 62 6f 74 68 20 FS and AFP both
eb0b0 6f 62 73 65 72 76 65 20 74 68 65 0a 2a 2a 20 63 observe the.** c
eb0c0 6c 6f 73 65 2d 74 6f 2d 6f 70 65 6e 20 73 65 6d lose-to-open sem
eb0d0 61 6e 74 69 63 73 20 66 6f 72 20 65 6e 73 75 72 antics for ensur
eb0e0 69 6e 67 20 63 61 63 68 65 20 63 6f 68 65 72 65 ing cache cohere
eb0f0 6e 63 79 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 6e ncy.** [http://n
eb100 66 73 2e 73 6f 75 72 63 65 66 6f 72 67 65 2e 6e fs.sourceforge.n
eb110 65 74 2f 23 66 61 71 5f 61 38 5d 2c 20 77 68 69 et/#faq_a8], whi
eb120 63 68 20 64 6f 65 73 20 6e 6f 74 20 65 66 66 65 ch does not effe
eb130 63 74 69 76 65 6c 79 0a 2a 2a 20 61 64 64 72 65 ctively.** addre
eb140 73 73 20 74 68 65 20 72 65 71 75 69 72 65 6d 65 ss the requireme
eb150 6e 74 73 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 nts for concurre
eb160 6e 74 20 64 61 74 61 62 61 73 65 20 61 63 63 65 nt database acce
eb170 73 73 20 62 79 20 6d 75 6c 74 69 70 6c 65 0a 2a ss by multiple.*
eb180 2a 20 72 65 61 64 65 72 73 20 61 6e 64 20 77 72 * readers and wr
eb190 69 74 65 72 73 0a 2a 2a 20 5b 68 74 74 70 3a 2f iters.** [http:/
eb1a0 2f 77 77 77 2e 6e 61 62 62 6c 65 2e 63 6f 6d 2f /www.nabble.com/
eb1b0 53 51 4c 69 74 65 2d 6f 6e 2d 4e 46 53 2d 63 61 SQLite-on-NFS-ca
eb1c0 63 68 65 2d 63 6f 68 65 72 65 6e 63 79 2d 74 64 che-coherency-td
eb1d0 31 35 36 35 35 37 30 31 2e 68 74 6d 6c 5d 2e 0a 15655701.html]..
eb1e0 2a 2a 0a 2a 2a 20 54 6f 20 61 64 64 72 65 73 73 **.** To address
eb1f0 20 74 68 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 the performance
eb200 20 61 6e 64 20 63 61 63 68 65 20 63 6f 68 65 72 and cache coher
eb210 65 6e 63 79 20 69 73 73 75 65 73 2c 20 70 72 6f ency issues, pro
eb220 78 79 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a xy file locking.
eb230 2a 2a 20 63 68 61 6e 67 65 73 20 74 68 65 20 77 ** changes the w
eb240 61 79 20 64 61 74 61 62 61 73 65 20 61 63 63 65 ay database acce
eb250 73 73 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 ss is controlled
eb260 20 62 79 20 6c 69 6d 69 74 69 6e 67 20 61 63 63 by limiting acc
eb270 65 73 73 20 74 6f 20 61 0a 2a 2a 20 73 69 6e 67 ess to a.** sing
eb280 6c 65 20 68 6f 73 74 20 61 74 20 61 20 74 69 6d le host at a tim
eb290 65 20 61 6e 64 20 6d 6f 76 69 6e 67 20 66 69 6c e and moving fil
eb2a0 65 20 6c 6f 63 6b 73 20 6f 66 66 20 6f 66 20 74 e locks off of t
eb2b0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
eb2c0 0a 2a 2a 20 61 6e 64 20 6f 6e 74 6f 20 61 20 70 .** and onto a p
eb2d0 72 6f 78 79 20 66 69 6c 65 20 6f 6e 20 74 68 65 roxy file on the
eb2e0 20 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74 local file syst
eb2f0 65 6d 2e 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 em. .**.**.** U
eb300 73 69 6e 67 20 70 72 6f 78 79 20 6c 6f 63 6b 73 sing proxy locks
eb310 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d .** ------------
eb320 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 43 20 41 50 -----.**.** C AP
eb330 49 73 0a 2a 2a 0a 2a 2a 20 20 73 71 6c 69 74 65 Is.**.** sqlite
eb340 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 3_file_control(d
eb350 62 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 b, dbname, SQLIT
eb360 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 E_SET_LOCKPROXYF
eb370 49 4c 45 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 ILE,.**
eb380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3c 70 <p
eb390 72 6f 78 79 5f 70 61 74 68 3e 20 7c 20 22 3a 61 roxy_path> | ":a
eb3a0 75 74 6f 3a 22 29 3b 0a 2a 2a 20 20 73 71 6c 69 uto:");.** sqli
eb3b0 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c te3_file_control
eb3c0 28 64 62 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c (db, dbname, SQL
eb3d0 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 ITE_GET_LOCKPROX
eb3e0 59 46 49 4c 45 2c 20 26 3c 70 72 6f 78 79 5f 70 YFILE, &<proxy_p
eb3f0 61 74 68 3e 29 3b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 ath>);.**.**.**
eb400 53 51 4c 20 70 72 61 67 6d 61 73 0a 2a 2a 0a 2a SQL pragmas.**.*
eb410 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 * PRAGMA [datab
eb420 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f ase.]lock_proxy_
eb430 66 69 6c 65 3d 3c 70 72 6f 78 79 5f 70 61 74 68 file=<proxy_path
eb440 3e 20 7c 20 3a 61 75 74 6f 3a 0a 2a 2a 20 20 50 > | :auto:.** P
eb450 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e RAGMA [database.
eb460 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 ]lock_proxy_file
eb470 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 79 69 6e .**.** Specifyin
eb480 67 20 22 3a 61 75 74 6f 3a 22 20 6d 65 61 6e 73 g ":auto:" means
eb490 20 74 68 61 74 20 69 66 20 74 68 65 72 65 20 69 that if there i
eb4a0 73 20 61 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 s a conch file w
eb4b0 69 74 68 20 61 20 6d 61 74 63 68 69 6e 67 0a 2a ith a matching.*
eb4c0 2a 20 68 6f 73 74 20 49 44 20 69 6e 20 69 74 2c * host ID in it,
eb4d0 20 74 68 65 20 70 72 6f 78 79 20 70 61 74 68 20 the proxy path
eb4e0 69 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c in the conch fil
eb4f0 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 2c 20 e will be used,
eb500 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 61 20 70 otherwise.** a p
eb510 72 6f 78 79 20 70 61 74 68 20 62 61 73 65 64 20 roxy path based
eb520 6f 6e 20 74 68 65 20 75 73 65 72 27 73 20 74 65 on the user's te
eb530 6d 70 20 64 69 72 0a 2a 2a 20 28 76 69 61 20 63 mp dir.** (via c
eb540 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41 52 57 49 onfstr(_CS_DARWI
eb550 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c N_USER_TEMP_DIR,
eb560 2e 2e 2e 29 29 20 77 69 6c 6c 20 62 65 20 75 73 ...)) will be us
eb570 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 61 63 ed and the.** ac
eb580 74 75 61 6c 20 70 72 6f 78 79 20 66 69 6c 65 20 tual proxy file
eb590 6e 61 6d 65 20 69 73 20 67 65 6e 65 72 61 74 65 name is generate
eb5a0 64 20 66 72 6f 6d 20 74 68 65 20 6e 61 6d 65 20 d from the name
eb5b0 61 6e 64 20 70 61 74 68 20 6f 66 20 74 68 65 0a and path of the.
eb5c0 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
eb5d0 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a . For example:.
eb5e0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 46 6f 72 20 **.** For
eb5f0 64 61 74 61 62 61 73 65 20 70 61 74 68 20 22 2f database path "/
eb600 55 73 65 72 73 2f 6d 65 2f 66 6f 6f 2e 64 62 22 Users/me/foo.db"
eb610 20 0a 2a 2a 20 20 20 20 20 20 20 54 68 65 20 6c .** The l
eb620 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c 20 62 65 ock path will be
eb630 20 22 3c 74 6d 70 64 69 72 3e 2f 73 71 6c 69 74 "<tmpdir>/sqlit
eb640 65 70 6c 6f 63 6b 73 2f 5f 55 73 65 72 73 5f 6d eplocks/_Users_m
eb650 65 5f 66 6f 6f 2e 64 62 3a 61 75 74 6f 3a 22 29 e_foo.db:auto:")
eb660 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 20 6c 6f .**.** Once a lo
eb670 63 6b 20 70 72 6f 78 79 20 69 73 20 63 6f 6e 66 ck proxy is conf
eb680 69 67 75 72 65 64 20 66 6f 72 20 61 20 64 61 74 igured for a dat
eb690 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
eb6a0 2c 20 69 74 20 63 61 6e 20 6e 6f 74 0a 2a 2a 20 , it can not.**
eb6b0 62 65 20 72 65 6d 6f 76 65 64 2c 20 68 6f 77 65 be removed, howe
eb6c0 76 65 72 20 69 74 20 6d 61 79 20 62 65 20 73 77 ver it may be sw
eb6d0 69 74 63 68 65 64 20 74 6f 20 61 20 64 69 66 66 itched to a diff
eb6e0 65 72 65 6e 74 20 70 72 6f 78 79 20 70 61 74 68 erent proxy path
eb6f0 20 76 69 61 0a 2a 2a 20 74 68 65 20 61 62 6f 76 via.** the abov
eb700 65 20 41 50 49 73 20 28 61 73 73 75 6d 69 6e 67 e APIs (assuming
eb710 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 the conch file
eb720 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 68 65 6c is not being hel
eb730 64 20 62 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 d by another.**
eb740 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 70 72 connection or pr
eb750 6f 63 65 73 73 29 2e 20 0a 2a 2a 0a 2a 2a 0a 2a ocess). .**.**.*
eb760 2a 20 48 6f 77 20 70 72 6f 78 79 20 6c 6f 63 6b * How proxy lock
eb770 69 6e 67 20 77 6f 72 6b 73 0a 2a 2a 20 2d 2d 2d ing works.** ---
eb780 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
eb790 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79 ----.**.** Proxy
eb7a0 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 72 65 file locking re
eb7b0 6c 69 65 73 20 70 72 69 6d 61 72 69 6c 79 20 6f lies primarily o
eb7c0 6e 20 74 77 6f 20 6e 65 77 20 73 75 70 70 6f 72 n two new suppor
eb7d0 74 69 6e 67 20 66 69 6c 65 73 3a 20 0a 2a 2a 0a ting files: .**.
eb7e0 2a 2a 20 20 20 2a 20 20 63 6f 6e 63 68 20 66 69 ** * conch fi
eb7f0 6c 65 20 74 6f 20 6c 69 6d 69 74 20 61 63 63 65 le to limit acce
eb800 73 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ss to the databa
eb810 73 65 20 66 69 6c 65 20 74 6f 20 61 20 73 69 6e se file to a sin
eb820 67 6c 65 20 68 6f 73 74 0a 2a 2a 20 20 20 20 20 gle host.**
eb830 20 61 74 20 61 20 74 69 6d 65 0a 2a 2a 0a 2a 2a at a time.**.**
eb840 20 20 20 2a 20 20 70 72 6f 78 79 20 66 69 6c 65 * proxy file
eb850 20 74 6f 20 61 63 74 20 61 73 20 61 20 70 72 6f to act as a pro
eb860 78 79 20 66 6f 72 20 74 68 65 20 61 64 76 69 73 xy for the advis
eb870 6f 72 79 20 6c 6f 63 6b 73 20 6e 6f 72 6d 61 6c ory locks normal
eb880 6c 79 0a 2a 2a 20 20 20 20 20 20 74 61 6b 65 6e ly.** taken
eb890 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
eb8a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 63 68 .**.** The conch
eb8b0 20 66 69 6c 65 20 2d 20 74 6f 20 75 73 65 20 61 file - to use a
eb8c0 20 70 72 6f 78 79 20 66 69 6c 65 2c 20 73 71 6c proxy file, sql
eb8d0 69 74 65 20 6d 75 73 74 20 66 69 72 73 74 20 22 ite must first "
eb8e0 68 6f 6c 64 20 74 68 65 20 63 6f 6e 63 68 22 0a hold the conch".
eb8f0 2a 2a 20 62 79 20 74 61 6b 69 6e 67 20 61 6e 20 ** by taking an
eb900 73 71 6c 69 74 65 2d 73 74 79 6c 65 20 73 68 61 sqlite-style sha
eb910 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 red lock on the
eb920 63 6f 6e 63 68 20 66 69 6c 65 2c 20 72 65 61 64 conch file, read
eb930 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 ing the.** conte
eb940 6e 74 73 20 61 6e 64 20 63 6f 6d 70 61 72 69 6e nts and comparin
eb950 67 20 74 68 65 20 68 6f 73 74 27 73 20 75 6e 69 g the host's uni
eb960 71 75 65 20 68 6f 73 74 20 49 44 20 28 73 65 65 que host ID (see
eb970 20 62 65 6c 6f 77 29 20 61 6e 64 20 6c 6f 63 6b below) and lock
eb980 0a 2a 2a 20 70 72 6f 78 79 20 70 61 74 68 20 61 .** proxy path a
eb990 67 61 69 6e 73 74 20 74 68 65 20 76 61 6c 75 65 gainst the value
eb9a0 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 s stored in the
eb9b0 63 6f 6e 63 68 2e 20 20 54 68 65 20 63 6f 6e 63 conch. The conc
eb9c0 68 20 66 69 6c 65 20 69 73 0a 2a 2a 20 73 74 6f h file is.** sto
eb9d0 72 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 red in the same
eb9e0 64 69 72 65 63 74 6f 72 79 20 61 73 20 74 68 65 directory as the
eb9f0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
eba00 6e 64 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 nd the file name
eba10 0a 2a 2a 20 69 73 20 70 61 74 74 65 72 6e 65 64 .** is patterned
eba20 20 61 66 74 65 72 20 74 68 65 20 64 61 74 61 62 after the datab
eba30 61 73 65 20 66 69 6c 65 20 6e 61 6d 65 20 61 73 ase file name as
eba40 20 22 2e 3c 64 61 74 61 62 61 73 65 6e 61 6d 65 ".<databasename
eba50 3e 2d 63 6f 6e 63 68 22 2e 0a 2a 2a 20 49 66 20 >-conch"..** If
eba60 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 64 the conch file d
eba70 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 6f oes not exist, o
eba80 72 20 69 74 27 73 20 63 6f 6e 74 65 6e 74 73 20 r it's contents
eba90 64 6f 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 do not match the
ebaa0 0a 2a 2a 20 68 6f 73 74 20 49 44 20 61 6e 64 2f .** host ID and/
ebab0 6f 72 20 70 72 6f 78 79 20 70 61 74 68 2c 20 74 or proxy path, t
ebac0 68 65 6e 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 hen the lock is
ebad0 65 73 63 61 6c 61 74 65 64 20 74 6f 20 61 6e 20 escalated to an
ebae0 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20 6c 6f 63 exclusive.** loc
ebaf0 6b 20 61 6e 64 20 74 68 65 20 63 6f 6e 63 68 20 k and the conch
ebb00 66 69 6c 65 20 63 6f 6e 74 65 6e 74 73 20 69 73 file contents is
ebb10 20 75 70 64 61 74 65 64 20 77 69 74 68 20 74 68 updated with th
ebb20 65 20 68 6f 73 74 20 49 44 20 61 6e 64 20 70 72 e host ID and pr
ebb30 6f 78 79 0a 2a 2a 20 70 61 74 68 20 61 6e 64 20 oxy.** path and
ebb40 74 68 65 20 6c 6f 63 6b 20 69 73 20 64 6f 77 6e the lock is down
ebb50 67 72 61 64 65 64 20 74 6f 20 61 20 73 68 61 72 graded to a shar
ebb60 65 64 20 6c 6f 63 6b 20 61 67 61 69 6e 2e 20 20 ed lock again.
ebb70 49 66 20 74 68 65 20 63 6f 6e 63 68 0a 2a 2a 20 If the conch.**
ebb80 69 73 20 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 is held by anoth
ebb90 65 72 20 70 72 6f 63 65 73 73 20 28 77 69 74 68 er process (with
ebba0 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 29 2c a shared lock),
ebbb0 20 74 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c the exclusive l
ebbc0 6f 63 6b 0a 2a 2a 20 77 69 6c 6c 20 66 61 69 6c ock.** will fail
ebbd0 20 61 6e 64 20 53 51 4c 49 54 45 5f 42 55 53 59 and SQLITE_BUSY
ebbe0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
ebbf0 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 66 69 .** The proxy fi
ebc00 6c 65 20 2d 20 61 20 73 69 6e 67 6c 65 2d 62 79 le - a single-by
ebc10 74 65 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 te file used for
ebc20 20 61 6c 6c 20 61 64 76 69 73 6f 72 79 20 66 69 all advisory fi
ebc30 6c 65 20 6c 6f 63 6b 73 0a 2a 2a 20 6e 6f 72 6d le locks.** norm
ebc40 61 6c 6c 79 20 74 61 6b 65 6e 20 6f 6e 20 74 68 ally taken on th
ebc50 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
ebc60 20 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 66 This allows f
ebc70 6f 72 20 73 61 66 65 20 73 68 61 72 69 6e 67 0a or safe sharing.
ebc80 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ** of the databa
ebc90 73 65 20 66 69 6c 65 20 66 6f 72 20 6d 75 6c 74 se file for mult
ebca0 69 70 6c 65 20 72 65 61 64 65 72 73 20 61 6e 64 iple readers and
ebcb0 20 77 72 69 74 65 72 73 20 6f 6e 20 74 68 65 20 writers on the
ebcc0 73 61 6d 65 0a 2a 2a 20 68 6f 73 74 20 28 74 68 same.** host (th
ebcd0 65 20 63 6f 6e 63 68 20 65 6e 73 75 72 65 73 20 e conch ensures
ebce0 74 68 61 74 20 74 68 65 79 20 61 6c 6c 20 75 73 that they all us
ebcf0 65 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 61 6c e the same local
ebd00 20 6c 6f 63 6b 20 66 69 6c 65 29 2e 0a 2a 2a 0a lock file)..**.
ebd10 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 74 68 ** There is a th
ebd20 69 72 64 20 66 69 6c 65 20 2d 20 74 68 65 20 68 ird file - the h
ebd30 6f 73 74 20 49 44 20 66 69 6c 65 20 2d 20 75 73 ost ID file - us
ebd40 65 64 20 61 73 20 61 20 70 65 72 73 69 73 74 65 ed as a persiste
ebd50 6e 74 20 72 65 63 6f 72 64 0a 2a 2a 20 6f 66 20 nt record.** of
ebd60 61 20 75 6e 69 71 75 65 20 69 64 65 6e 74 69 66 a unique identif
ebd70 69 65 72 20 66 6f 72 20 74 68 65 20 68 6f 73 74 ier for the host
ebd80 2c 20 61 20 31 32 38 2d 62 79 74 65 20 75 6e 69 , a 128-byte uni
ebd90 71 75 65 20 68 6f 73 74 20 69 64 20 66 69 6c 65 que host id file
ebda0 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 74 68 20 .** in the path
ebdb0 64 65 66 69 6e 65 64 20 62 79 20 74 68 65 20 48 defined by the H
ebdc0 4f 53 54 49 44 50 41 54 48 20 6d 61 63 72 6f 20 OSTIDPATH macro
ebdd0 28 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69 (default value i
ebde0 73 0a 2a 2a 20 2f 4c 69 62 72 61 72 79 2f 43 61 s.** /Library/Ca
ebdf0 63 68 65 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e ches/.com.apple.
ebe00 73 71 6c 69 74 65 43 6f 6e 63 68 48 6f 73 74 49 sqliteConchHostI
ebe10 64 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 65 73 d)..**.** Reques
ebe20 74 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 70 72 ting the lock pr
ebe30 6f 78 79 20 64 6f 65 73 20 6e 6f 74 20 69 6d 6d oxy does not imm
ebe40 65 64 69 61 74 65 6c 79 20 74 61 6b 65 20 74 68 ediately take th
ebe50 65 20 63 6f 6e 63 68 2c 20 69 74 20 69 73 0a 2a e conch, it is.*
ebe60 2a 20 6f 6e 6c 79 20 74 61 6b 65 6e 20 77 68 65 * only taken whe
ebe70 6e 20 74 68 65 20 66 69 72 73 74 20 72 65 71 75 n the first requ
ebe80 65 73 74 20 74 6f 20 6c 6f 63 6b 20 64 61 74 61 est to lock data
ebe90 62 61 73 65 20 66 69 6c 65 20 69 73 20 6d 61 64 base file is mad
ebea0 65 2e 20 20 0a 2a 2a 20 54 68 69 73 20 6d 61 74 e. .** This mat
ebeb0 63 68 65 73 20 74 68 65 20 73 65 6d 61 6e 74 69 ches the semanti
ebec0 63 73 20 6f 66 20 74 68 65 20 74 72 61 64 69 74 cs of the tradit
ebed0 69 6f 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 62 65 ional locking be
ebee0 68 61 76 69 6f 72 2c 20 77 68 65 72 65 0a 2a 2a havior, where.**
ebef0 20 6f 70 65 6e 69 6e 67 20 61 20 63 6f 6e 6e 65 opening a conne
ebf00 63 74 69 6f 6e 20 74 6f 20 61 20 64 61 74 61 62 ction to a datab
ebf10 61 73 65 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f ase file does no
ebf20 74 20 74 61 6b 65 20 61 20 6c 6f 63 6b 20 6f 6e t take a lock on
ebf30 20 69 74 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72 it..** The shar
ebf40 65 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6e 20 6f ed lock and an o
ebf50 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 pen file descrip
ebf60 74 6f 72 20 61 72 65 20 6d 61 69 6e 74 61 69 6e tor are maintain
ebf70 65 64 20 75 6e 74 69 6c 20 0a 2a 2a 20 74 68 65 ed until .** the
ebf80 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74 connection to t
ebf90 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63 he database is c
ebfa0 6c 6f 73 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 losed. .**.** Th
ebfb0 65 20 70 72 6f 78 79 20 66 69 6c 65 20 61 6e 64 e proxy file and
ebfc0 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 the lock file a
ebfd0 72 65 20 6e 65 76 65 72 20 64 65 6c 65 74 65 64 re never deleted
ebfe0 20 73 6f 20 74 68 65 79 20 6f 6e 6c 79 20 6e 65 so they only ne
ebff0 65 64 0a 2a 2a 20 74 6f 20 62 65 20 63 72 65 61 ed.** to be crea
ec000 74 65 64 20 74 68 65 20 66 69 72 73 74 20 74 69 ted the first ti
ec010 6d 65 20 74 68 65 79 20 61 72 65 20 75 73 65 64 me they are used
ec020 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 ..**.** Configur
ec030 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 0a 2a 2a ation options.**
ec040 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
ec050 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 53 51 ------.**.** SQ
ec060 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 LITE_PREFER_PROX
ec070 59 5f 4c 4f 43 4b 49 4e 47 0a 2a 2a 0a 2a 2a 20 Y_LOCKING.**.**
ec080 20 20 20 20 20 20 44 61 74 61 62 61 73 65 20 66 Database f
ec090 69 6c 65 73 20 61 63 63 65 73 73 65 64 20 6f 6e iles accessed on
ec0a0 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 20 non-local file
ec0b0 73 79 73 74 65 6d 73 20 61 72 65 0a 2a 2a 20 20 systems are.**
ec0c0 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 61 6c automatical
ec0d0 6c 79 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f ly configured fo
ec0e0 72 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2c r proxy locking,
ec0f0 20 6c 6f 63 6b 20 66 69 6c 65 73 20 61 72 65 0a lock files are.
ec100 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 64 20 61 ** named a
ec110 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 75 73 69 utomatically usi
ec120 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f 67 69 ng the same logi
ec130 63 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 50 52 c as.** PR
ec140 41 47 4d 41 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f AGMA lock_proxy_
ec150 66 69 6c 65 3d 22 3a 61 75 74 6f 3a 22 0a 2a 2a file=":auto:".**
ec160 20 20 20 20 0a 2a 2a 20 20 53 51 4c 49 54 45 5f .** SQLITE_
ec170 50 52 4f 58 59 5f 44 45 42 55 47 0a 2a 2a 0a 2a PROXY_DEBUG.**.*
ec180 2a 20 20 20 20 20 20 20 45 6e 61 62 6c 65 73 20 * Enables
ec190 74 68 65 20 6c 6f 67 67 69 6e 67 20 6f 66 20 65 the logging of e
ec1a0 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 64 75 rror messages du
ec1b0 72 69 6e 67 20 68 6f 73 74 20 69 64 20 66 69 6c ring host id fil
ec1c0 65 0a 2a 2a 20 20 20 20 20 20 20 72 65 74 72 69 e.** retri
ec1d0 65 76 61 6c 20 61 6e 64 20 63 72 65 61 74 69 6f eval and creatio
ec1e0 6e 0a 2a 2a 0a 2a 2a 20 20 48 4f 53 54 49 44 50 n.**.** HOSTIDP
ec1f0 41 54 48 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 ATH.**.**
ec200 4f 76 65 72 72 69 64 65 73 20 74 68 65 20 64 65 Overrides the de
ec210 66 61 75 6c 74 20 68 6f 73 74 20 49 44 20 66 69 fault host ID fi
ec220 6c 65 20 70 61 74 68 20 6c 6f 63 61 74 69 6f 6e le path location
ec230 0a 2a 2a 0a 2a 2a 20 20 4c 4f 43 4b 50 52 4f 58 .**.** LOCKPROX
ec240 59 44 49 52 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 YDIR.**.**
ec250 20 4f 76 65 72 72 69 64 65 73 20 74 68 65 20 64 Overrides the d
ec260 65 66 61 75 6c 74 20 64 69 72 65 63 74 6f 72 79 efault directory
ec270 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 20 70 used for lock p
ec280 72 6f 78 79 20 66 69 6c 65 73 20 74 68 61 74 0a roxy files that.
ec290 2a 2a 20 20 20 20 20 20 20 61 72 65 20 6e 61 6d ** are nam
ec2a0 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ed automatically
ec2b0 20 76 69 61 20 74 68 65 20 22 3a 61 75 74 6f 3a via the ":auto:
ec2c0 22 20 73 65 74 74 69 6e 67 0a 2a 2a 0a 2a 2a 20 " setting.**.**
ec2d0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
ec2e0 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 PROXYDIR_PERMISS
ec2f0 49 4f 4e 53 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 IONS.**.**
ec300 20 50 65 72 6d 69 73 73 69 6f 6e 73 20 74 6f 20 Permissions to
ec310 75 73 65 20 77 68 65 6e 20 63 72 65 61 74 69 6e use when creatin
ec320 67 20 61 20 64 69 72 65 63 74 6f 72 79 20 66 6f g a directory fo
ec330 72 20 73 74 6f 72 69 6e 67 20 74 68 65 0a 2a 2a r storing the.**
ec340 20 20 20 20 20 20 20 6c 6f 63 6b 20 70 72 6f 78 lock prox
ec350 79 20 66 69 6c 65 73 2c 20 6f 6e 6c 79 20 75 73 y files, only us
ec360 65 64 20 77 68 65 6e 20 4c 4f 43 4b 50 52 4f 58 ed when LOCKPROX
ec370 59 44 49 52 20 69 73 20 6e 6f 74 20 73 65 74 2e YDIR is not set.
ec380 0a 2a 2a 20 20 20 20 0a 2a 2a 20 20 20 20 0a 2a .** .** .*
ec390 2a 20 41 73 20 6d 65 6e 74 69 6f 6e 65 64 20 61 * As mentioned a
ec3a0 62 6f 76 65 2c 20 77 68 65 6e 20 63 6f 6d 70 69 bove, when compi
ec3b0 6c 65 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f led with SQLITE_
ec3c0 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 PREFER_PROXY_LOC
ec3d0 4b 49 4e 47 2c 0a 2a 2a 20 73 65 74 74 69 6e 67 KING,.** setting
ec3e0 20 74 68 65 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 the environment
ec3f0 20 76 61 72 69 61 62 6c 65 20 53 51 4c 49 54 45 variable SQLITE
ec400 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 _FORCE_PROXY_LOC
ec410 4b 49 4e 47 20 74 6f 20 31 20 77 69 6c 6c 0a 2a KING to 1 will.*
ec420 2a 20 66 6f 72 63 65 20 70 72 6f 78 79 20 6c 6f * force proxy lo
ec430 63 6b 69 6e 67 20 74 6f 20 62 65 20 75 73 65 64 cking to be used
ec440 20 66 6f 72 20 65 76 65 72 79 20 64 61 74 61 62 for every datab
ec450 61 73 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 2c ase file opened,
ec460 20 61 6e 64 20 30 0a 2a 2a 20 77 69 6c 6c 20 66 and 0.** will f
ec470 6f 72 63 65 20 61 75 74 6f 6d 61 74 69 63 20 70 orce automatic p
ec480 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 roxy locking to
ec490 62 65 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 be disabled for
ec4a0 61 6c 6c 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 all database.**
ec4b0 66 69 6c 65 73 20 28 65 78 70 6c 69 63 69 74 79 files (explicity
ec4c0 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 53 51 4c calling the SQL
ec4d0 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 ITE_SET_LOCKPROX
ec4e0 59 46 49 4c 45 20 70 72 61 67 6d 61 20 6f 72 0a YFILE pragma or.
ec4f0 2a 2a 20 73 71 6c 69 74 65 5f 66 69 6c 65 5f 63 ** sqlite_file_c
ec500 6f 6e 74 72 6f 6c 20 41 50 49 20 69 73 20 6e 6f ontrol API is no
ec510 74 20 61 66 66 65 63 74 65 64 20 62 79 20 53 51 t affected by SQ
ec520 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 LITE_FORCE_PROXY
ec530 5f 4c 4f 43 4b 49 4e 47 29 2e 0a 2a 2f 0a 0a 2f _LOCKING)..*/../
ec540 2a 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 *.** Proxy locki
ec550 6e 67 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c ng is only avail
ec560 61 62 6c 65 20 6f 6e 20 4d 61 63 4f 53 58 20 0a able on MacOSX .
ec570 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f */.#if defined(_
ec580 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c _APPLE__) && SQL
ec590 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
ec5a0 4e 47 5f 53 54 59 4c 45 0a 0a 23 69 66 64 65 66 NG_STYLE..#ifdef
ec5b0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20 SQLITE_TEST./*
ec5c0 73 69 6d 75 6c 61 74 65 20 6d 75 6c 74 69 70 6c simulate multipl
ec5d0 65 20 68 6f 73 74 73 20 62 79 20 63 72 65 61 74 e hosts by creat
ec5e0 69 6e 67 20 75 6e 69 71 75 65 20 68 6f 73 74 69 ing unique hosti
ec5f0 64 20 66 69 6c 65 20 70 61 74 68 73 20 2a 2f 0a d file paths */.
ec600 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
ec610 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 qlite3_hostid_nu
ec620 6d 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f m = 0;.#endif../
ec630 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 4c 6f *.** The proxyLo
ec640 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 68 61 73 ckingContext has
ec650 20 74 68 65 20 70 61 74 68 20 61 6e 64 20 66 69 the path and fi
ec660 6c 65 20 73 74 72 75 63 74 75 72 65 73 20 66 6f le structures fo
ec670 72 20 74 68 65 20 72 65 6d 6f 74 65 20 0a 2a 2a r the remote .**
ec680 20 61 6e 64 20 6c 6f 63 61 6c 20 70 72 6f 78 79 and local proxy
ec690 20 66 69 6c 65 73 20 69 6e 20 69 74 0a 2a 2f 0a files in it.*/.
ec6a0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 70 typedef struct p
ec6b0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
ec6c0 78 74 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 xt proxyLockingC
ec6d0 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 20 70 ontext;.struct p
ec6e0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
ec6f0 78 74 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 xt {. unixFile
ec700 2a 63 6f 6e 63 68 46 69 6c 65 3b 20 20 20 20 20 *conchFile;
ec710 20 20 20 20 2f 2a 20 4f 70 65 6e 20 63 6f 6e 63 /* Open conc
ec720 68 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 h file */. char
ec730 20 2a 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 3b *conchFilePath;
ec740 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
ec750 20 6f 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 of the conch fi
ec760 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 le */. unixFile
ec770 20 2a 6c 6f 63 6b 50 72 6f 78 79 3b 20 20 20 20 *lockProxy;
ec780 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 72 6f /* Open pro
ec790 78 79 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a xy lock file */.
ec7a0 20 20 63 68 61 72 20 2a 6c 6f 63 6b 50 72 6f 78 char *lockProx
ec7b0 79 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 2f yPath; /
ec7c0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 70 72 * Name of the pr
ec7d0 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f oxy lock file */
ec7e0 0a 20 20 63 68 61 72 20 2a 64 62 50 61 74 68 3b . char *dbPath;
ec7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ec800 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f /* Name of the o
ec810 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e pen file */. in
ec820 74 20 63 6f 6e 63 68 48 65 6c 64 3b 20 20 20 20 t conchHeld;
ec830 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
ec840 75 65 20 69 66 20 74 68 65 20 63 6f 6e 63 68 20 ue if the conch
ec850 69 73 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c is currently hel
ec860 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 6f 6c 64 d */. void *old
ec870 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 LockingContext;
ec880 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 /* Original
ec890 6c 6f 63 6b 69 6e 67 63 6f 6e 74 65 78 74 20 74 lockingcontext t
ec8a0 6f 20 72 65 73 74 6f 72 65 20 6f 6e 20 63 6c 6f o restore on clo
ec8b0 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f se */. sqlite3_
ec8c0 69 6f 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 io_methods const
ec8d0 20 2a 70 4f 6c 64 4d 65 74 68 6f 64 3b 20 20 20 *pOldMethod;
ec8e0 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 49 2f /* Original I/
ec8f0 4f 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 63 6c O methods for cl
ec900 6f 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 48 4f ose */.};../* HO
ec910 53 54 49 44 4c 45 4e 20 61 6e 64 20 43 4f 4e 43 STIDLEN and CONC
ec920 48 4c 45 4e 20 62 6f 74 68 20 69 6e 63 6c 75 64 HLEN both includ
ec930 65 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 e space for the
ec940 73 74 72 69 6e 67 20 0a 2a 2a 20 74 65 72 6d 69 string .** termi
ec950 6e 61 74 69 6e 67 20 6e 75 6c 20 0a 2a 2f 0a 23 nating nul .*/.#
ec960 64 65 66 69 6e 65 20 48 4f 53 54 49 44 4c 45 4e define HOSTIDLEN
ec970 20 20 20 20 20 20 20 20 20 31 32 38 0a 23 64 65 128.#de
ec980 66 69 6e 65 20 43 4f 4e 43 48 4c 45 4e 20 20 20 fine CONCHLEN
ec990 20 20 20 20 20 20 20 28 4d 41 58 50 41 54 48 4c (MAXPATHL
ec9a0 45 4e 2b 48 4f 53 54 49 44 4c 45 4e 2b 31 29 0a EN+HOSTIDLEN+1).
ec9b0 23 69 66 6e 64 65 66 20 48 4f 53 54 49 44 50 41 #ifndef HOSTIDPA
ec9c0 54 48 0a 23 20 64 65 66 69 6e 65 20 48 4f 53 54 TH.# define HOST
ec9d0 49 44 50 41 54 48 20 20 20 20 20 20 20 22 2f 4c IDPATH "/L
ec9e0 69 62 72 61 72 79 2f 43 61 63 68 65 73 2f 2e 63 ibrary/Caches/.c
ec9f0 6f 6d 2e 61 70 70 6c 65 2e 73 71 6c 69 74 65 43 om.apple.sqliteC
eca00 6f 6e 63 68 48 6f 73 74 49 64 22 0a 23 65 6e 64 onchHostId".#end
eca10 69 66 0a 0a 2f 2a 20 62 61 73 69 63 61 6c 6c 79 if../* basically
eca20 20 61 20 63 6f 70 79 20 6f 66 20 75 6e 69 78 52 a copy of unixR
eca30 61 6e 64 6f 6d 6e 65 73 73 20 77 69 74 68 20 64 andomness with d
eca40 69 66 66 65 72 65 6e 74 0a 2a 2a 20 74 65 73 74 ifferent.** test
eca50 20 62 65 68 61 76 69 6f 72 20 62 75 69 6c 74 20 behavior built
eca60 69 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 in */.static int
eca70 20 70 72 6f 78 79 47 65 6e 65 72 61 74 65 48 6f proxyGenerateHo
eca80 73 74 49 44 28 63 68 61 72 20 2a 70 48 6f 73 74 stID(char *pHost
eca90 49 44 29 7b 0a 20 20 69 6e 74 20 70 69 64 2c 20 ID){. int pid,
ecaa0 66 64 2c 20 6c 65 6e 3b 0a 20 20 75 6e 73 69 67 fd, len;. unsig
ecab0 6e 65 64 20 63 68 61 72 20 2a 6b 65 79 20 3d 20 ned char *key =
ecac0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a (unsigned char *
ecad0 29 70 48 6f 73 74 49 44 3b 0a 20 20 0a 20 20 6d )pHostID;. . m
ecae0 65 6d 73 65 74 28 6b 65 79 2c 20 30 2c 20 48 4f emset(key, 0, HO
ecaf0 53 54 49 44 4c 45 4e 29 3b 0a 20 20 6c 65 6e 20 STIDLEN);. len
ecb00 3d 20 30 3b 0a 20 20 66 64 20 3d 20 6f 70 65 6e = 0;. fd = open
ecb10 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c ("/dev/urandom",
ecb20 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20 69 66 O_RDONLY);. if
ecb30 28 20 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 6c ( fd>=0 ){. l
ecb40 65 6e 20 3d 20 72 65 61 64 28 66 64 2c 20 6b 65 en = read(fd, ke
ecb50 79 2c 20 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 y, HOSTIDLEN);.
ecb60 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a close(fd); /*
ecb70 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 silently leak t
ecb80 68 65 20 66 64 20 69 66 20 69 74 20 66 61 69 6c he fd if it fail
ecb90 73 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 6c s */. }. if( l
ecba0 65 6e 20 3c 20 48 4f 53 54 49 44 4c 45 4e 20 29 en < HOSTIDLEN )
ecbb0 7b 0a 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a {. time_t t;.
ecbc0 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 time(&t);.
ecbd0 20 20 6d 65 6d 63 70 79 28 6b 65 79 2c 20 26 74 memcpy(key, &t
ecbe0 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b 0a 20 20 , sizeof(t));.
ecbf0 20 20 70 69 64 20 3d 20 67 65 74 70 69 64 28 29 pid = getpid()
ecc00 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 6b 65 ;. memcpy(&ke
ecc10 79 5b 73 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70 y[sizeof(t)], &p
ecc20 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 29 id, sizeof(pid))
ecc30 3b 0a 20 20 7d 0a 20 20 0a 23 69 66 64 65 66 20 ;. }. .#ifdef
ecc40 4d 41 4b 45 5f 50 52 45 54 54 59 5f 48 4f 53 54 MAKE_PRETTY_HOST
ecc50 49 44 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 ID. {. int i
ecc60 3b 0a 20 20 20 20 2f 2a 20 66 69 6c 74 65 72 20 ;. /* filter
ecc70 74 68 65 20 62 79 74 65 73 20 69 6e 74 6f 20 70 the bytes into p
ecc80 72 69 6e 74 61 62 6c 65 20 61 73 63 69 69 20 63 rintable ascii c
ecc90 68 61 72 61 63 74 65 72 73 20 61 6e 64 20 4e 55 haracters and NU
ecca0 4c 20 74 65 72 6d 69 6e 61 74 65 20 2a 2f 0a 20 L terminate */.
eccb0 20 20 20 6b 65 79 5b 28 48 4f 53 54 49 44 4c 45 key[(HOSTIDLE
eccc0 4e 2d 31 29 5d 20 3d 20 30 78 30 30 3b 0a 20 20 N-1)] = 0x00;.
eccd0 20 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 28 48 for( i=0; i<(H
ecce0 4f 53 54 49 44 4c 45 4e 2d 31 29 3b 20 69 2b 2b OSTIDLEN-1); i++
eccf0 20 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e ){. unsign
ecd00 65 64 20 63 68 61 72 20 70 61 20 3d 20 6b 65 79 ed char pa = key
ecd10 5b 69 5d 26 30 78 37 46 3b 0a 20 20 20 20 20 20 [i]&0x7F;.
ecd20 69 66 28 20 70 61 3c 30 78 32 30 20 29 7b 0a 20 if( pa<0x20 ){.
ecd30 20 20 20 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20 key[i] =
ecd40 28 6b 65 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20 (key[i]&0x80 ==
ecd50 30 78 38 30 29 20 3f 20 70 61 2b 30 78 34 30 20 0x80) ? pa+0x40
ecd60 3a 20 70 61 2b 30 78 32 30 3b 0a 20 20 20 20 20 : pa+0x20;.
ecd70 20 7d 65 6c 73 65 20 69 66 28 20 70 61 3d 3d 30 }else if( pa==0
ecd80 78 37 46 20 29 7b 0a 20 20 20 20 20 20 20 20 6b x7F ){. k
ecd90 65 79 5b 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 ey[i] = (key[i]&
ecda0 30 78 38 30 20 3d 3d 20 30 78 38 30 29 20 3f 20 0x80 == 0x80) ?
ecdb0 70 61 3d 30 78 32 30 20 3a 20 70 61 2b 30 78 37 pa=0x20 : pa+0x7
ecdc0 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d E;. }. }
ecdd0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 . }.#endif. re
ecde0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
ecdf0 7d 0a 0a 2f 2a 20 77 72 69 74 65 73 20 74 68 65 }../* writes the
ece00 20 68 6f 73 74 20 69 64 20 70 61 74 68 20 74 6f host id path to
ece10 20 70 61 74 68 2c 20 70 61 74 68 20 73 68 6f 75 path, path shou
ece20 6c 64 20 62 65 20 61 6e 20 70 72 65 2d 61 6c 6c ld be an pre-all
ece30 6f 63 61 74 65 64 20 62 75 66 66 65 72 0a 2a 2a ocated buffer.**
ece40 20 77 69 74 68 20 65 6e 6f 75 67 68 20 73 70 61 with enough spa
ece50 63 65 20 66 6f 72 20 61 20 70 61 74 68 20 0a 2a ce for a path .*
ece60 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 72 /.static void pr
ece70 6f 78 79 47 65 74 48 6f 73 74 49 44 50 61 74 68 oxyGetHostIDPath
ece80 28 63 68 61 72 20 2a 70 61 74 68 2c 20 73 69 7a (char *path, siz
ece90 65 5f 74 20 6c 65 6e 29 7b 0a 20 20 73 74 72 6c e_t len){. strl
ecea0 63 70 79 28 70 61 74 68 2c 20 48 4f 53 54 49 44 cpy(path, HOSTID
eceb0 50 41 54 48 2c 20 6c 65 6e 29 3b 0a 23 69 66 64 PATH, len);.#ifd
ecec0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 ef SQLITE_TEST.
eced0 20 69 66 28 20 73 71 6c 69 74 65 33 5f 68 6f 73 if( sqlite3_hos
ecee0 74 69 64 5f 6e 75 6d 3e 30 20 29 7b 0a 20 20 20 tid_num>0 ){.
ecef0 20 63 68 61 72 20 73 75 66 66 69 78 5b 32 5d 20 char suffix[2]
ecf00 3d 20 22 31 22 3b 0a 20 20 20 20 73 75 66 66 69 = "1";. suffi
ecf10 78 5b 30 5d 20 3d 20 73 75 66 66 69 78 5b 30 5d x[0] = suffix[0]
ecf20 20 2b 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 + sqlite3_hosti
ecf30 64 5f 6e 75 6d 3b 0a 20 20 20 20 73 74 72 6c 63 d_num;. strlc
ecf40 61 74 28 70 61 74 68 2c 20 73 75 66 66 69 78 2c at(path, suffix,
ecf50 20 6c 65 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 len);. }.#endi
ecf60 66 0a 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 f. OSTRACE3("GE
ecf70 54 48 4f 53 54 49 44 50 41 54 48 20 20 25 73 20 THOSTIDPATH %s
ecf80 70 69 64 3d 25 64 5c 6e 22 2c 20 70 61 74 68 2c pid=%d\n", path,
ecf90 20 67 65 74 70 69 64 28 29 29 3b 0a 7d 0a 0a 2f getpid());.}../
ecfa0 2a 20 67 65 74 20 74 68 65 20 68 6f 73 74 20 49 * get the host I
ecfb0 44 20 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 20 D from a sqlite
ecfc0 68 6f 73 74 69 64 20 66 69 6c 65 20 73 74 6f 72 hostid file stor
ecfd0 65 64 20 69 6e 20 74 68 65 20 0a 2a 2a 20 75 73 ed in the .** us
ecfe0 65 72 2d 73 70 65 63 69 66 69 63 20 74 6d 70 20 er-specific tmp
ecff0 64 69 72 65 63 74 6f 72 79 2c 20 63 72 65 61 74 directory, creat
ed000 65 20 74 68 65 20 49 44 20 69 66 20 69 74 27 73 e the ID if it's
ed010 20 6e 6f 74 20 74 68 65 72 65 20 61 6c 72 65 61 not there alrea
ed020 64 79 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e dy .*/.static in
ed030 74 20 70 72 6f 78 79 47 65 74 48 6f 73 74 49 44 t proxyGetHostID
ed040 28 63 68 61 72 20 2a 70 48 6f 73 74 49 44 2c 20 (char *pHostID,
ed050 69 6e 74 20 2a 70 45 72 72 6f 72 29 7b 0a 20 20 int *pError){.
ed060 69 6e 74 20 66 64 3b 0a 20 20 63 68 61 72 20 70 int fd;. char p
ed070 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b ath[MAXPATHLEN];
ed080 20 0a 20 20 73 69 7a 65 5f 74 20 6c 65 6e 3b 0a . size_t len;.
ed090 20 20 69 6e 74 20 72 63 3d 53 51 4c 49 54 45 5f int rc=SQLITE_
ed0a0 4f 4b 3b 0a 0a 20 20 70 72 6f 78 79 47 65 74 48 OK;.. proxyGetH
ed0b0 6f 73 74 49 44 50 61 74 68 28 70 61 74 68 2c 20 ostIDPath(path,
ed0c0 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 2f MAXPATHLEN);. /
ed0d0 2a 20 74 72 79 20 74 6f 20 63 72 65 61 74 65 20 * try to create
ed0e0 74 68 65 20 68 6f 73 74 20 49 44 20 66 69 6c 65 the host ID file
ed0f0 2c 20 69 66 20 69 74 20 61 6c 72 65 61 64 79 20 , if it already
ed100 65 78 69 73 74 73 20 72 65 61 64 20 74 68 65 20 exists read the
ed110 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 66 64 contents */. fd
ed120 20 3d 20 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f = open(path, O_
ed130 43 52 45 41 54 7c 4f 5f 57 52 4f 4e 4c 59 7c 4f CREAT|O_WRONLY|O
ed140 5f 45 58 43 4c 2c 20 30 36 34 34 29 3b 0a 20 20 _EXCL, 0644);.
ed150 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 if( fd<0 ){.
ed160 69 6e 74 20 65 72 72 3d 65 72 72 6e 6f 3b 0a 09 int err=errno;..
ed170 09 0a 20 20 20 20 69 66 28 20 65 72 72 21 3d 45 .. if( err!=E
ed180 45 58 49 53 54 20 29 7b 0a 23 69 66 64 65 66 20 EXIST ){.#ifdef
ed190 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 SQLITE_PROXY_DEB
ed1a0 55 47 20 2f 2a 20 73 65 74 20 74 68 65 20 73 71 UG /* set the sq
ed1b0 6c 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 lite error messa
ed1c0 67 65 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 ge instead */.
ed1d0 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 fprintf(stde
ed1e0 72 72 2c 20 22 73 71 6c 69 74 65 20 65 72 72 6f rr, "sqlite erro
ed1f0 72 20 63 72 65 61 74 69 6e 67 20 68 6f 73 74 20 r creating host
ed200 49 44 20 66 69 6c 65 20 25 73 3a 20 25 73 5c 6e ID file %s: %s\n
ed210 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
ed220 20 70 61 74 68 2c 20 73 74 72 65 72 72 6f 72 28 path, strerror(
ed230 65 72 72 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 err));.#endif.
ed240 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
ed250 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d 0a 20 20 E_PERM;. }.
ed260 20 20 2f 2a 20 63 6f 75 6c 64 6e 27 74 20 63 72 /* couldn't cr
ed270 65 61 74 65 20 74 68 65 20 66 69 6c 65 2c 20 72 eate the file, r
ed280 65 61 64 20 69 74 20 69 6e 73 74 65 61 64 20 2a ead it instead *
ed290 2f 0a 20 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 /. fd = open(
ed2a0 70 61 74 68 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f path, O_RDONLY|O
ed2b0 5f 45 58 43 4c 29 3b 0a 20 20 20 20 69 66 28 20 _EXCL);. if(
ed2c0 66 64 3c 30 20 29 7b 0a 23 69 66 64 65 66 20 53 fd<0 ){.#ifdef S
ed2d0 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 QLITE_PROXY_DEBU
ed2e0 47 20 2f 2a 20 73 65 74 20 74 68 65 20 73 71 6c G /* set the sql
ed2f0 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 ite error messag
ed300 65 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 e instead */.
ed310 20 20 20 69 6e 74 20 65 72 72 20 3d 20 65 72 72 int err = err
ed320 6e 6f 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 no;. fprint
ed330 66 28 73 74 64 65 72 72 2c 20 22 73 71 6c 69 74 f(stderr, "sqlit
ed340 65 20 65 72 72 6f 72 20 6f 70 65 6e 69 6e 67 20 e error opening
ed350 68 6f 73 74 20 49 44 20 66 69 6c 65 20 25 73 3a host ID file %s:
ed360 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 %s\n",.
ed370 20 20 20 20 20 20 70 61 74 68 2c 20 73 74 72 65 path, stre
ed380 72 72 6f 72 28 65 72 72 29 29 3b 0a 23 65 6e 64 rror(err));.#end
ed390 69 66 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 if. return
ed3a0 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20 SQLITE_PERM;.
ed3b0 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 70 72 65 }. len = pre
ed3c0 61 64 28 66 64 2c 20 70 48 6f 73 74 49 44 2c 20 ad(fd, pHostID,
ed3d0 48 4f 53 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 HOSTIDLEN, 0);.
ed3e0 20 20 20 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a if( len<0 ){.
ed3f0 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 *pError =
ed400 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 errno;. rc
ed410 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 = SQLITE_IOERR_R
ed420 45 41 44 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 EAD;. }else i
ed430 66 28 20 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e f( len<HOSTIDLEN
ed440 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f ){. *pErro
ed450 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 r = 0;. rc
ed460 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 = SQLITE_IOERR_S
ed470 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 20 20 7d HORT_READ;. }
ed480 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 . close(fd);
ed490 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b /* silently leak
ed4a0 20 74 68 65 20 66 64 20 69 66 20 69 74 20 66 61 the fd if it fa
ed4b0 69 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 ils */. OSTRA
ed4c0 43 45 33 28 22 47 45 54 48 4f 53 54 49 44 20 20 CE3("GETHOSTID
ed4d0 72 65 61 64 20 25 73 20 70 69 64 3d 25 64 5c 6e read %s pid=%d\n
ed4e0 22 2c 20 70 48 6f 73 74 49 44 2c 20 67 65 74 70 ", pHostID, getp
ed4f0 69 64 28 29 29 3b 0a 20 20 20 20 72 65 74 75 72 id());. retur
ed500 6e 20 72 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 n rc;. }else{.
ed510 20 20 20 2f 2a 20 77 65 27 72 65 20 63 72 65 61 /* we're crea
ed520 74 69 6e 67 20 74 68 65 20 68 6f 73 74 20 49 44 ting the host ID
ed530 20 66 69 6c 65 20 28 75 73 65 20 61 20 72 61 6e file (use a ran
ed540 64 6f 6d 20 73 74 72 69 6e 67 20 6f 66 20 62 79 dom string of by
ed550 74 65 73 29 20 2a 2f 0a 20 20 20 20 70 72 6f 78 tes) */. prox
ed560 79 47 65 6e 65 72 61 74 65 48 6f 73 74 49 44 28 yGenerateHostID(
ed570 70 48 6f 73 74 49 44 29 3b 0a 20 20 20 20 6c 65 pHostID);. le
ed580 6e 20 3d 20 70 77 72 69 74 65 28 66 64 2c 20 70 n = pwrite(fd, p
ed590 48 6f 73 74 49 44 2c 20 48 4f 53 54 49 44 4c 45 HostID, HOSTIDLE
ed5a0 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c N, 0);. if( l
ed5b0 65 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 en<0 ){. *p
ed5c0 45 72 72 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 Error = errno;.
ed5d0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
ed5e0 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 _IOERR_WRITE;.
ed5f0 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65 6e 3c }else if( len<
ed600 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20 HOSTIDLEN ){.
ed610 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a *pError = 0;.
ed620 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
ed630 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 E_IOERR_WRITE;.
ed640 20 20 20 7d 0a 20 20 20 20 63 6c 6f 73 65 28 66 }. close(f
ed650 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 d); /* silently
ed660 6c 65 61 6b 20 74 68 65 20 66 64 20 69 66 20 69 leak the fd if i
ed670 74 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 4f t fails */. O
ed680 53 54 52 41 43 45 33 28 22 47 45 54 48 4f 53 54 STRACE3("GETHOST
ed690 49 44 20 20 77 72 6f 74 65 20 25 73 20 70 69 64 ID wrote %s pid
ed6a0 3d 25 64 5c 6e 22 2c 20 70 48 6f 73 74 49 44 2c =%d\n", pHostID,
ed6b0 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 20 20 getpid());.
ed6c0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d return rc;. }.}
ed6d0 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f ..static int pro
ed6e0 78 79 47 65 74 4c 6f 63 6b 50 61 74 68 28 63 6f xyGetLockPath(co
ed6f0 6e 73 74 20 63 68 61 72 20 2a 64 62 50 61 74 68 nst char *dbPath
ed700 2c 20 63 68 61 72 20 2a 6c 50 61 74 68 2c 20 73 , char *lPath, s
ed710 69 7a 65 5f 74 20 6d 61 78 4c 65 6e 29 7b 0a 20 ize_t maxLen){.
ed720 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 int len;. int
ed730 64 62 4c 65 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a dbLen;. int i;.
ed740 0a 23 69 66 64 65 66 20 4c 4f 43 4b 50 52 4f 58 .#ifdef LOCKPROX
ed750 59 44 49 52 0a 20 20 6c 65 6e 20 3d 20 73 74 72 YDIR. len = str
ed760 6c 63 70 79 28 6c 50 61 74 68 2c 20 4c 4f 43 4b lcpy(lPath, LOCK
ed770 50 52 4f 58 59 44 49 52 2c 20 6d 61 78 4c 65 6e PROXYDIR, maxLen
ed780 29 3b 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 );.#else.# ifdef
ed790 20 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 _CS_DARWIN_USER
ed7a0 5f 54 45 4d 50 5f 44 49 52 0a 20 20 7b 0a 20 20 _TEMP_DIR. {.
ed7b0 20 20 63 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41 confstr(_CS_DA
ed7c0 52 57 49 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 RWIN_USER_TEMP_D
ed7d0 49 52 2c 20 6c 50 61 74 68 2c 20 6d 61 78 4c 65 IR, lPath, maxLe
ed7e0 6e 29 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74 n);. len = st
ed7f0 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 22 73 71 rlcat(lPath, "sq
ed800 6c 69 74 65 70 6c 6f 63 6b 73 22 2c 20 6d 61 78 liteplocks", max
ed810 4c 65 6e 29 3b 0a 20 20 20 20 69 66 28 20 6d 6b Len);. if( mk
ed820 64 69 72 28 6c 50 61 74 68 2c 20 53 51 4c 49 54 dir(lPath, SQLIT
ed830 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 E_DEFAULT_PROXYD
ed840 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 20 IR_PERMISSIONS)
ed850 29 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 6d ){. /* if m
ed860 6b 64 69 72 20 66 61 69 6c 73 2c 20 68 61 6e 64 kdir fails, hand
ed870 6c 65 20 61 73 20 6c 6f 63 6b 20 66 69 6c 65 20 le as lock file
ed880 63 72 65 61 74 69 6f 6e 20 66 61 69 6c 75 72 65 creation failure
ed890 20 2a 2f 0a 23 20 20 69 66 64 65 66 20 53 51 4c */.# ifdef SQL
ed8a0 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 ITE_DEBUG.
ed8b0 69 6e 74 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b int err = errno;
ed8c0 0a 20 20 20 20 20 20 69 66 28 20 65 72 72 21 3d . if( err!=
ed8d0 45 45 58 49 53 54 20 29 7b 0a 20 20 20 20 20 20 EEXIST ){.
ed8e0 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 fprintf(stderr
ed8f0 2c 20 22 70 72 6f 78 79 47 65 74 4c 6f 63 6b 50 , "proxyGetLockP
ed900 61 74 68 3a 20 6d 6b 64 69 72 28 25 73 2c 30 25 ath: mkdir(%s,0%
ed910 6f 29 20 65 72 72 6f 72 20 25 64 20 25 73 5c 6e o) error %d %s\n
ed920 22 2c 20 6c 50 61 74 68 2c 0a 20 20 20 20 20 20 ", lPath,.
ed930 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 SQLITE
ed940 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 _DEFAULT_PROXYDI
ed950 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 2c 20 65 R_PERMISSIONS, e
ed960 72 72 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72 rr, strerror(err
ed970 29 29 3b 0a 20 20 20 20 20 20 7d 0a 23 20 20 65 ));. }.# e
ed980 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ndif. }else{.
ed990 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 OSTRACE3("
ed9a0 47 45 54 4c 4f 43 4b 50 41 54 48 20 20 6d 6b 64 GETLOCKPATH mkd
ed9b0 69 72 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c ir %s pid=%d\n",
ed9c0 20 6c 50 61 74 68 2c 20 67 65 74 70 69 64 28 29 lPath, getpid()
ed9d0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 );. }. .
ed9e0 7d 0a 23 20 65 6c 73 65 0a 20 20 6c 65 6e 20 3d }.# else. len =
ed9f0 20 73 74 72 6c 63 70 79 28 6c 50 61 74 68 2c 20 strlcpy(lPath,
eda00 22 2f 74 6d 70 2f 22 2c 20 6d 61 78 4c 65 6e 29 "/tmp/", maxLen)
eda10 3b 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 ;.# endif.#endif
eda20 0a 0a 20 20 69 66 28 20 6c 50 61 74 68 5b 6c 65 .. if( lPath[le
eda30 6e 2d 31 5d 21 3d 27 2f 27 20 29 7b 0a 20 20 20 n-1]!='/' ){.
eda40 20 6c 65 6e 20 3d 20 73 74 72 6c 63 61 74 28 6c len = strlcat(l
eda50 50 61 74 68 2c 20 22 2f 22 2c 20 6d 61 78 4c 65 Path, "/", maxLe
eda60 6e 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 n);. }. . /*
eda70 74 72 61 6e 73 66 6f 72 6d 20 74 68 65 20 64 62 transform the db
eda80 20 70 61 74 68 20 74 6f 20 61 20 75 6e 69 71 75 path to a uniqu
eda90 65 20 63 61 63 68 65 20 6e 61 6d 65 20 2a 2f 0a e cache name */.
edaa0 20 20 64 62 4c 65 6e 20 3d 20 28 69 6e 74 29 73 dbLen = (int)s
edab0 74 72 6c 65 6e 28 64 62 50 61 74 68 29 3b 0a 20 trlen(dbPath);.
edac0 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 64 62 4c for( i=0; i<dbL
edad0 65 6e 20 26 26 20 28 69 2b 6c 65 6e 2b 37 29 3c en && (i+len+7)<
edae0 6d 61 78 4c 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 maxLen; i++){.
edaf0 20 20 63 68 61 72 20 63 20 3d 20 64 62 50 61 74 char c = dbPat
edb00 68 5b 69 5d 3b 0a 20 20 20 20 6c 50 61 74 68 5b h[i];. lPath[
edb10 69 2b 6c 65 6e 5d 20 3d 20 28 63 3d 3d 27 2f 27 i+len] = (c=='/'
edb20 29 3f 27 5f 27 3a 63 3b 0a 20 20 7d 0a 20 20 6c )?'_':c;. }. l
edb30 50 61 74 68 5b 69 2b 6c 65 6e 5d 3d 27 5c 30 27 Path[i+len]='\0'
edb40 3b 0a 20 20 73 74 72 6c 63 61 74 28 6c 50 61 74 ;. strlcat(lPat
edb50 68 2c 20 22 3a 61 75 74 6f 3a 22 2c 20 6d 61 78 h, ":auto:", max
edb60 4c 65 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 Len);. return S
edb70 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
edb80 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 ** Create a new
edb90 56 46 53 20 66 69 6c 65 20 64 65 73 63 72 69 70 VFS file descrip
edba0 74 6f 72 20 28 73 74 6f 72 65 64 20 69 6e 20 6d tor (stored in m
edbb0 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
edbc0 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d rom.** sqlite3_m
edbd0 61 6c 6c 6f 63 29 20 61 6e 64 20 6f 70 65 6e 20 alloc) and open
edbe0 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 64 20 22 the file named "
edbf0 70 61 74 68 22 20 69 6e 20 74 68 65 20 66 69 6c path" in the fil
edc00 65 20 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2a e descriptor..**
edc10 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 69 .** The caller i
edc20 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 6e 6f s responsible no
edc30 74 20 6f 6e 6c 79 20 66 6f 72 20 63 6c 6f 73 69 t only for closi
edc40 6e 67 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 ng the file desc
edc50 72 69 70 74 6f 72 0a 2a 2a 20 62 75 74 20 61 6c riptor.** but al
edc60 73 6f 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74 so for freeing t
edc70 68 65 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 he memory associ
edc80 61 74 65 64 20 77 69 74 68 20 74 68 65 20 66 69 ated with the fi
edc90 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a le descriptor..*
edca0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f /.static int pro
edcb0 78 79 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65 xyCreateUnixFile
edcc0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 (const char *pat
edcd0 68 2c 20 75 6e 69 78 46 69 6c 65 20 2a 2a 70 70 h, unixFile **pp
edce0 46 69 6c 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 File) {. unixFi
edcf0 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 le *pNew;. int
edd00 66 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f flags = SQLITE_O
edd10 50 45 4e 5f 4d 41 49 4e 5f 44 42 7c 53 51 4c 49 PEN_MAIN_DB|SQLI
edd20 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 TE_OPEN_CREATE|S
edd30 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
edd40 52 49 54 45 3b 0a 20 20 69 6e 74 20 72 63 20 3d RITE;. int rc =
edd50 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 SQLITE_OK;. sq
edd60 6c 69 74 65 33 5f 76 66 73 20 64 75 6d 6d 79 56 lite3_vfs dummyV
edd70 66 73 3b 0a 0a 20 20 70 4e 65 77 20 3d 20 28 75 fs;.. pNew = (u
edd80 6e 69 78 46 69 6c 65 20 2a 29 73 71 6c 69 74 65 nixFile *)sqlite
edd90 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 3_malloc(sizeof(
edda0 75 6e 69 78 46 69 6c 65 29 29 3b 0a 20 20 69 66 unixFile));. if
eddb0 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 72 ( !pNew ){. r
eddc0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
eddd0 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 EM;. }. memset
edde0 28 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 (pNew, 0, sizeof
eddf0 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20 (unixFile));..
ede00 2f 2a 20 43 61 6c 6c 20 75 6e 69 78 4f 70 65 6e /* Call unixOpen
ede10 28 29 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 70 () to open the p
ede20 72 6f 78 79 20 66 69 6c 65 2e 20 54 68 65 20 66 roxy file. The f
ede30 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f 20 75 lags passed to u
ede40 6e 69 78 4f 70 65 6e 28 29 0a 20 20 2a 2a 20 73 nixOpen(). ** s
ede50 75 67 67 65 73 74 20 74 68 61 74 20 74 68 65 20 uggest that the
ede60 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 file being opene
ede70 64 20 69 73 20 61 20 22 6d 61 69 6e 20 64 61 74 d is a "main dat
ede80 61 62 61 73 65 22 2e 20 54 68 69 73 20 69 73 0a abase". This is.
ede90 20 20 2a 2a 20 6e 65 63 65 73 73 61 72 79 20 61 ** necessary a
edea0 73 20 6f 74 68 65 72 20 66 69 6c 65 20 74 79 70 s other file typ
edeb0 65 73 20 64 6f 20 6e 6f 74 20 6e 65 63 65 73 73 es do not necess
edec0 61 72 69 6c 79 20 73 75 70 70 6f 72 74 20 6c 6f arily support lo
eded0 63 6b 69 6e 67 2e 20 49 74 0a 20 20 2a 2a 20 69 cking. It. ** i
edee0 73 20 62 65 74 74 65 72 20 74 6f 20 75 73 65 20 s better to use
edef0 75 6e 69 78 4f 70 65 6e 28 29 20 69 6e 73 74 65 unixOpen() inste
edf00 61 64 20 6f 66 20 6f 70 65 6e 69 6e 67 20 74 68 ad of opening th
edf10 65 20 66 69 6c 65 20 64 69 72 65 63 74 6c 79 20 e file directly
edf20 77 69 74 68 0a 20 20 2a 2a 20 6f 70 65 6e 28 29 with. ** open()
edf30 2c 20 61 73 20 75 6e 69 78 4f 70 65 6e 28 29 20 , as unixOpen()
edf40 73 65 74 73 20 75 70 20 74 68 65 20 76 61 72 69 sets up the vari
edf50 6f 75 73 20 6d 65 63 68 61 6e 69 73 6d 73 20 72 ous mechanisms r
edf60 65 71 75 69 72 65 64 20 74 6f 0a 20 20 2a 2a 20 equired to. **
edf70 6d 61 6b 65 20 73 75 72 65 20 61 20 63 61 6c 6c make sure a call
edf80 20 74 6f 20 63 6c 6f 73 65 28 29 20 64 6f 65 73 to close() does
edf90 20 6e 6f 74 20 63 61 75 73 65 20 74 68 65 20 73 not cause the s
edfa0 79 73 74 65 6d 20 74 6f 20 64 69 73 63 61 72 64 ystem to discard
edfb0 0a 20 20 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b . ** POSIX lock
edfc0 73 20 70 72 65 6d 61 74 75 72 65 6c 79 2e 0a 20 s prematurely..
edfd0 20 2a 2a 0a 20 20 2a 2a 20 49 74 20 69 73 20 69 **. ** It is i
edfe0 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 74 68 mportant that th
edff0 65 20 78 4f 70 65 6e 20 6d 65 6d 62 65 72 20 6f e xOpen member o
ee000 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65 63 74 f the VFS object
ee010 20 70 61 73 73 65 64 20 74 6f 20 0a 20 20 2a 2a passed to . **
ee020 20 75 6e 69 78 4f 70 65 6e 28 29 20 69 73 20 4e unixOpen() is N
ee030 55 4c 4c 2e 20 54 68 69 73 20 74 65 6c 6c 73 20 ULL. This tells
ee040 75 6e 69 78 4f 70 65 6e 28 29 20 6d 61 79 20 74 unixOpen() may t
ee050 72 79 20 74 6f 20 6f 70 65 6e 20 61 20 70 72 6f ry to open a pro
ee060 78 79 2d 66 69 6c 65 20 0a 20 20 2a 2a 20 66 6f xy-file . ** fo
ee070 72 20 74 68 65 20 70 72 6f 78 79 2d 66 69 6c 65 r the proxy-file
ee080 20 28 63 72 65 61 74 69 6e 67 20 61 20 70 6f 74 (creating a pot
ee090 65 6e 74 69 61 6c 20 69 6e 66 69 6e 69 74 65 20 ential infinite
ee0a0 6c 6f 6f 70 29 2e 0a 20 20 2a 2f 0a 20 20 64 75 loop).. */. du
ee0b0 6d 6d 79 56 66 73 2e 70 41 70 70 44 61 74 61 20 mmyVfs.pAppData
ee0c0 3d 20 28 76 6f 69 64 2a 29 26 61 75 74 6f 6c 6f = (void*)&autolo
ee0d0 63 6b 49 6f 46 69 6e 64 65 72 3b 0a 20 20 64 75 ckIoFinder;. du
ee0e0 6d 6d 79 56 66 73 2e 78 4f 70 65 6e 20 3d 20 30 mmyVfs.xOpen = 0
ee0f0 3b 0a 20 20 72 63 20 3d 20 75 6e 69 78 4f 70 65 ;. rc = unixOpe
ee100 6e 28 26 64 75 6d 6d 79 56 66 73 2c 20 70 61 74 n(&dummyVfs, pat
ee110 68 2c 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 h, (sqlite3_file
ee120 20 2a 29 70 4e 65 77 2c 20 66 6c 61 67 73 2c 20 *)pNew, flags,
ee130 26 66 6c 61 67 73 29 3b 0a 20 20 69 66 28 20 72 &flags);. if( r
ee140 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
ee150 28 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 4f 50 (flags&SQLITE_OP
ee160 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 29 7b 0a EN_READONLY) ){.
ee170 20 20 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f pNew->pMetho
ee180 64 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 d->xClose((sqlit
ee190 65 33 5f 66 69 6c 65 20 2a 29 70 4e 65 77 29 3b e3_file *)pNew);
ee1a0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
ee1b0 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 0a _CANTOPEN;. }..
ee1c0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
ee1d0 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 _OK ){. sqlit
ee1e0 65 33 5f 66 72 65 65 28 70 4e 65 77 29 3b 0a 20 e3_free(pNew);.
ee1f0 20 20 20 70 4e 65 77 20 3d 20 30 3b 0a 20 20 7d pNew = 0;. }
ee200 0a 0a 20 20 2a 70 70 46 69 6c 65 20 3d 20 70 4e .. *ppFile = pN
ee210 65 77 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ew;. return rc;
ee220 0a 7d 0a 0a 2f 2a 20 74 61 6b 65 73 20 74 68 65 .}../* takes the
ee230 20 63 6f 6e 63 68 20 62 79 20 74 61 6b 69 6e 67 conch by taking
ee240 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 a shared lock a
ee250 6e 64 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74 nd read the cont
ee260 65 6e 74 73 20 63 6f 6e 63 68 2c 20 69 66 20 0a ents conch, if .
ee270 2a 2a 20 6c 6f 63 6b 50 61 74 68 20 69 73 20 6e ** lockPath is n
ee280 6f 6e 2d 4e 55 4c 4c 2c 20 74 68 65 20 68 6f 73 on-NULL, the hos
ee290 74 20 49 44 20 61 6e 64 20 6c 6f 63 6b 20 66 69 t ID and lock fi
ee2a0 6c 65 20 70 61 74 68 20 6d 75 73 74 20 6d 61 74 le path must mat
ee2b0 63 68 2e 20 20 41 20 4e 55 4c 4c 20 0a 2a 2a 20 ch. A NULL .**
ee2c0 6c 6f 63 6b 50 61 74 68 20 6d 65 61 6e 73 20 74 lockPath means t
ee2d0 68 61 74 20 74 68 65 20 6c 6f 63 6b 50 61 74 68 hat the lockPath
ee2e0 20 69 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 in the conch fi
ee2f0 6c 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 le will be used
ee300 69 66 20 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20 if the .** host
ee310 49 44 73 20 6d 61 74 63 68 2c 20 6f 72 20 61 20 IDs match, or a
ee320 6e 65 77 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 new lock path wi
ee330 6c 6c 20 62 65 20 67 65 6e 65 72 61 74 65 64 20 ll be generated
ee340 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a automatically .*
ee350 2a 20 61 6e 64 20 77 72 69 74 74 65 6e 20 74 6f * and written to
ee360 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 2e the conch file.
ee370 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
ee380 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 75 6e roxyTakeConch(un
ee390 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a ixFile *pFile){.
ee3a0 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f proxyLockingCo
ee3b0 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 ntext *pCtx = (p
ee3c0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
ee3d0 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b xt *)pFile->lock
ee3e0 69 6e 67 43 6f 6e 74 65 78 74 3b 20 0a 20 20 0a ingContext; . .
ee3f0 20 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 if( pCtx->conc
ee400 68 48 65 6c 64 3e 30 20 29 7b 0a 20 20 20 20 72 hHeld>0 ){. r
ee410 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
ee420 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e . }else{. un
ee430 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c ixFile *conchFil
ee440 65 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 e = pCtx->conchF
ee450 69 6c 65 3b 0a 20 20 20 20 63 68 61 72 20 74 65 ile;. char te
ee460 73 74 56 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e stValue[CONCHLEN
ee470 5d 3b 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 63 ];. char conc
ee480 68 56 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d hValue[CONCHLEN]
ee490 3b 0a 20 20 20 20 63 68 61 72 20 6c 6f 63 6b 50 ;. char lockP
ee4a0 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b ath[MAXPATHLEN];
ee4b0 0a 20 20 20 20 63 68 61 72 20 2a 74 4c 6f 63 6b . char *tLock
ee4c0 50 61 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 Path = NULL;.
ee4d0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
ee4e0 5f 4f 4b 3b 0a 20 20 20 20 69 6e 74 20 72 65 61 _OK;. int rea
ee4f0 64 52 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b dRc = SQLITE_OK;
ee500 0a 20 20 20 20 69 6e 74 20 73 79 6e 63 50 65 72 . int syncPer
ee510 6d 73 20 3d 20 30 3b 0a 0a 20 20 20 20 4f 53 54 ms = 0;.. OST
ee520 52 41 43 45 34 28 22 54 41 4b 45 43 4f 4e 43 48 RACE4("TAKECONCH
ee530 20 20 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d %d for %s pid=
ee540 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 %d\n", conchFile
ee550 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ->h,.
ee560 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f (pCtx->lockPro
ee570 78 79 50 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c xyPath ? pCtx->l
ee580 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3a 20 22 ockProxyPath : "
ee590 3a 61 75 74 6f 3a 22 29 2c 20 67 65 74 70 69 64 :auto:"), getpid
ee5a0 28 29 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 63 ());.. rc = c
ee5b0 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f onchFile->pMetho
ee5c0 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 d->xLock((sqlite
ee5d0 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 3_file*)conchFil
ee5e0 65 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b e, SHARED_LOCK);
ee5f0 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
ee600 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
ee610 69 6e 74 20 70 45 72 72 6f 72 20 3d 20 30 3b 0a int pError = 0;.
ee620 20 20 20 20 20 20 6d 65 6d 73 65 74 28 74 65 73 memset(tes
ee630 74 56 61 6c 75 65 2c 20 30 2c 20 43 4f 4e 43 48 tValue, 0, CONCH
ee640 4c 45 4e 29 3b 20 2f 2a 20 63 6f 6e 63 68 20 69 LEN); /* conch i
ee650 73 20 66 69 78 65 64 20 73 69 7a 65 20 2a 2f 0a s fixed size */.
ee660 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 rc = proxy
ee670 47 65 74 48 6f 73 74 49 44 28 74 65 73 74 56 61 GetHostID(testVa
ee680 6c 75 65 2c 20 26 70 45 72 72 6f 72 29 3b 0a 20 lue, &pError);.
ee690 20 20 20 20 20 69 66 28 20 28 72 63 26 30 78 66 if( (rc&0xf
ee6a0 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 f)==SQLITE_IOERR
ee6b0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c ){. pFil
ee6c0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 70 e->lastErrno = p
ee6d0 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 Error;. }.
ee6e0 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c if( pCtx->l
ee6f0 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a ockProxyPath ){.
ee700 20 20 20 20 20 20 20 20 73 74 72 6c 63 70 79 28 strlcpy(
ee710 26 74 65 73 74 56 61 6c 75 65 5b 48 4f 53 54 49 &testValue[HOSTI
ee720 44 4c 45 4e 5d 2c 20 70 43 74 78 2d 3e 6c 6f 63 DLEN], pCtx->loc
ee730 6b 50 72 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 kProxyPath, MAXP
ee740 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20 7d ATHLEN);. }
ee750 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 . }. if( r
ee760 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
ee770 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 goto end_t
ee780 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a akeconch;. }.
ee790 20 20 20 20 0a 20 20 20 20 72 65 61 64 52 63 20 . readRc
ee7a0 3d 20 75 6e 69 78 52 65 61 64 28 28 73 71 6c 69 = unixRead((sqli
ee7b0 74 65 33 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 te3_file *)conch
ee7c0 46 69 6c 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 File, conchValue
ee7d0 2c 20 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a , CONCHLEN, 0);.
ee7e0 20 20 20 20 69 66 28 20 72 65 61 64 52 63 21 3d if( readRc!=
ee7f0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f SQLITE_IOERR_SHO
ee800 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 RT_READ ){.
ee810 20 69 66 28 20 72 65 61 64 52 63 21 3d 53 51 4c if( readRc!=SQL
ee820 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
ee830 20 20 69 66 28 20 28 72 63 26 30 78 66 66 29 3d if( (rc&0xff)=
ee840 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b =SQLITE_IOERR ){
ee850 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 . pFile
ee860 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 63 6f ->lastErrno = co
ee870 6e 63 68 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 nchFile->lastErr
ee880 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 no;. }.
ee890 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 52 rc = readR
ee8a0 63 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 c;. goto
ee8b0 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 end_takeconch;.
ee8c0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 }. /*
ee8d0 69 66 20 74 68 65 20 63 6f 6e 63 68 20 68 61 73 if the conch has
ee8e0 20 64 61 74 61 20 63 6f 6d 70 61 72 65 20 74 68 data compare th
ee8f0 65 20 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 e contents */.
ee900 20 20 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c if( !pCtx->l
ee910 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a ockProxyPath ){.
ee920 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 72 20 61 /* for a
ee930 75 74 6f 2d 6e 61 6d 65 64 20 6c 6f 63 61 6c 20 uto-named local
ee940 6c 6f 63 6b 20 66 69 6c 65 2c 20 6a 75 73 74 20 lock file, just
ee950 63 68 65 63 6b 20 74 68 65 20 68 6f 73 74 20 49 check the host I
ee960 44 20 61 6e 64 20 77 65 27 6c 6c 0a 20 20 20 20 D and we'll.
ee970 20 20 20 20 20 2a 2a 20 75 73 65 20 74 68 65 20 ** use the
ee980 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 local lock file
ee990 70 61 74 68 20 74 68 61 74 27 73 20 61 6c 72 65 path that's alre
ee9a0 61 64 79 20 69 6e 20 74 68 65 72 65 20 2a 2f 0a ady in there */.
ee9b0 20 20 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d if( !mem
ee9c0 63 6d 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 cmp(testValue, c
ee9d0 6f 6e 63 68 56 61 6c 75 65 2c 20 48 4f 53 54 49 onchValue, HOSTI
ee9e0 44 4c 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 DLEN) ){.
ee9f0 20 20 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 28 tLockPath = (
eea00 63 68 61 72 20 2a 29 26 63 6f 6e 63 68 56 61 6c char *)&conchVal
eea10 75 65 5b 48 4f 53 54 49 44 4c 45 4e 5d 3b 0a 20 ue[HOSTIDLEN];.
eea20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e goto en
eea30 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 d_takeconch;.
eea40 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
eea50 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 77 se{. /* w
eea60 65 27 76 65 20 67 6f 74 20 74 68 65 20 63 6f 6e e've got the con
eea70 63 68 20 69 66 20 63 6f 6e 63 68 56 61 6c 75 65 ch if conchValue
eea80 20 6d 61 74 63 68 65 73 20 6f 75 72 20 70 61 74 matches our pat
eea90 68 20 61 6e 64 20 68 6f 73 74 20 49 44 20 2a 2f h and host ID */
eeaa0 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 6d 65 . if( !me
eeab0 6d 63 6d 70 28 74 65 73 74 56 61 6c 75 65 2c 20 mcmp(testValue,
eeac0 63 6f 6e 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43 conchValue, CONC
eead0 48 4c 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 HLEN) ){.
eeae0 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 goto end_take
eeaf0 63 6f 6e 63 68 3b 0a 20 20 20 20 20 20 20 20 7d conch;. }
eeb00 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
eeb10 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 61 20 73 se{. /* a s
eeb20 68 6f 72 74 20 72 65 61 64 20 6d 65 61 6e 73 20 hort read means
eeb30 77 65 27 72 65 20 22 63 72 65 61 74 69 6e 67 22 we're "creating"
eeb40 20 74 68 65 20 63 6f 6e 63 68 20 28 65 76 65 6e the conch (even
eeb50 20 74 68 6f 75 67 68 20 69 74 20 63 6f 75 6c 64 though it could
eeb60 20 0a 20 20 20 20 20 20 2a 2a 20 68 61 76 65 20 . ** have
eeb70 62 65 65 6e 20 75 73 65 72 2d 69 6e 74 65 72 76 been user-interv
eeb80 65 6e 74 69 6f 6e 29 2c 20 69 66 20 77 65 20 61 ention), if we a
eeb90 63 71 75 69 72 65 20 74 68 65 20 65 78 63 6c 75 cquire the exclu
eeba0 73 69 76 65 20 6c 6f 63 6b 2c 0a 20 20 20 20 20 sive lock,.
eebb0 20 2a 2a 20 77 65 27 6c 6c 20 74 72 79 20 74 6f ** we'll try to
eebc0 20 6d 61 74 63 68 20 74 68 65 20 63 75 72 72 65 match the curre
eebd0 6e 74 20 6f 6e 2d 64 69 73 6b 20 70 65 72 6d 69 nt on-disk permi
eebe0 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 64 61 ssions of the da
eebf0 74 61 62 61 73 65 0a 20 20 20 20 20 20 2a 2f 0a tabase. */.
eec00 20 20 20 20 20 20 73 79 6e 63 50 65 72 6d 73 20 syncPerms
eec10 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a = 1;. }. .
eec20 20 20 20 20 2f 2a 20 65 69 74 68 65 72 20 63 6f /* either co
eec30 6e 63 68 20 77 61 73 20 65 6d 74 70 79 20 6f 72 nch was emtpy or
eec40 20 64 69 64 6e 27 74 20 6d 61 74 63 68 20 2a 2f didn't match */
eec50 0a 20 20 20 20 69 66 28 20 21 70 43 74 78 2d 3e . if( !pCtx->
eec60 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b lockProxyPath ){
eec70 0a 20 20 20 20 20 20 70 72 6f 78 79 47 65 74 4c . proxyGetL
eec80 6f 63 6b 50 61 74 68 28 70 43 74 78 2d 3e 64 62 ockPath(pCtx->db
eec90 50 61 74 68 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 Path, lockPath,
eeca0 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 MAXPATHLEN);.
eecb0 20 20 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 6c tLockPath = l
eecc0 6f 63 6b 50 61 74 68 3b 0a 20 20 20 20 20 20 73 ockPath;. s
eecd0 74 72 6c 63 70 79 28 26 74 65 73 74 56 61 6c 75 trlcpy(&testValu
eece0 65 5b 48 4f 53 54 49 44 4c 45 4e 5d 2c 20 6c 6f e[HOSTIDLEN], lo
eecf0 63 6b 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c ckPath, MAXPATHL
eed00 45 4e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a EN);. }. .
eed10 20 20 20 20 2f 2a 20 75 70 64 61 74 65 20 63 6f /* update co
eed20 6e 63 68 20 77 69 74 68 20 68 6f 73 74 20 61 6e nch with host an
eed30 64 20 70 61 74 68 20 28 74 68 69 73 20 77 69 6c d path (this wil
eed40 6c 20 66 61 69 6c 20 69 66 20 6f 74 68 65 72 20 l fail if other
eed50 70 72 6f 63 65 73 73 0a 20 20 20 20 20 2a 2a 20 process. **
eed60 68 61 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63 has a shared loc
eed70 6b 20 61 6c 72 65 61 64 79 29 20 2a 2f 0a 20 20 k already) */.
eed80 20 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 rc = conchFile
eed90 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b ->pMethod->xLock
eeda0 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 ((sqlite3_file*)
eedb0 63 6f 6e 63 68 46 69 6c 65 2c 20 45 58 43 4c 55 conchFile, EXCLU
eedc0 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 SIVE_LOCK);.
eedd0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
eede0 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 K ){. rc =
eedf0 75 6e 69 78 57 72 69 74 65 28 28 73 71 6c 69 74 unixWrite((sqlit
eee00 65 33 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 e3_file *)conchF
eee10 69 6c 65 2c 20 74 65 73 74 56 61 6c 75 65 2c 20 ile, testValue,
eee20 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 CONCHLEN, 0);.
eee30 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
eee40 54 45 5f 4f 4b 20 26 26 20 73 79 6e 63 50 65 72 TE_OK && syncPer
eee50 6d 73 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 ms ){. st
eee60 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 ruct stat buf;.
eee70 20 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d int err =
eee80 20 66 73 74 61 74 28 70 46 69 6c 65 2d 3e 68 2c fstat(pFile->h,
eee90 20 26 62 75 66 29 3b 0a 20 20 20 20 20 20 20 20 &buf);.
eeea0 69 66 28 20 65 72 72 3d 3d 30 20 29 7b 0a 20 20 if( err==0 ){.
eeeb0 20 20 20 20 20 20 20 20 2f 2a 20 74 72 79 20 74 /* try t
eeec0 6f 20 6d 61 74 63 68 20 74 68 65 20 64 61 74 61 o match the data
eeed0 62 61 73 65 20 66 69 6c 65 20 70 65 72 6d 69 73 base file permis
eeee0 73 69 6f 6e 73 2c 20 69 67 6e 6f 72 65 20 66 61 sions, ignore fa
eeef0 69 6c 75 72 65 20 2a 2f 0a 23 69 66 6e 64 65 66 ilure */.#ifndef
eef00 20 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 SQLITE_PROXY_DE
eef10 42 55 47 0a 20 20 20 20 20 20 20 20 20 20 66 63 BUG. fc
eef20 68 6d 6f 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e hmod(conchFile->
eef30 68 2c 20 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b h, buf.st_mode);
eef40 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 20 .#else.
eef50 20 69 66 28 20 66 63 68 6d 6f 64 28 63 6f 6e 63 if( fchmod(conc
eef60 68 46 69 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74 hFile->h, buf.st
eef70 5f 6d 6f 64 65 29 21 3d 30 20 29 7b 0a 20 20 20 _mode)!=0 ){.
eef80 20 20 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64 int cod
eef90 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 e = errno;.
eefa0 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 fprintf(s
eefb0 74 64 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 tderr, "fchmod %
eefc0 6f 20 46 41 49 4c 45 44 20 77 69 74 68 20 25 64 o FAILED with %d
eefd0 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 %s\n",.
eefe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
eeff0 20 20 20 20 20 62 75 66 2e 73 74 5f 6d 6f 64 65 buf.st_mode
ef000 2c 20 63 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 , code, strerror
ef010 28 63 6f 64 65 29 29 3b 0a 20 20 20 20 20 20 20 (code));.
ef020 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 } else {.
ef030 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 fprintf(
ef040 73 74 64 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 stderr, "fchmod
ef050 25 6f 20 53 55 43 43 45 44 45 44 5c 6e 22 2c 62 %o SUCCEDED\n",b
ef060 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a 20 20 20 uf.st_mode);.
ef070 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
ef080 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
ef090 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 65 72 72 int code = err
ef0a0 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 66 70 no;. fp
ef0b0 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 53 rintf(stderr, "S
ef0c0 54 41 54 20 46 41 49 4c 45 44 5b 25 64 5d 20 77 TAT FAILED[%d] w
ef0d0 69 74 68 20 25 64 20 25 73 5c 6e 22 2c 20 0a 20 ith %d %s\n", .
ef0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef0f0 20 20 20 20 20 20 20 20 20 65 72 72 2c 20 63 6f err, co
ef100 64 65 2c 20 73 74 72 65 72 72 6f 72 28 63 6f 64 de, strerror(cod
ef110 65 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 e));.#endif.
ef120 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
ef130 20 20 7d 0a 20 20 20 20 63 6f 6e 63 68 46 69 6c }. conchFil
ef140 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c e->pMethod->xUnl
ef150 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c ock((sqlite3_fil
ef160 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 53 48 e*)conchFile, SH
ef170 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 0a 65 ARED_LOCK);. .e
ef180 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3a 0a 20 20 nd_takeconch:.
ef190 20 20 4f 53 54 52 41 43 45 32 28 22 54 52 41 4e OSTRACE2("TRAN
ef1a0 53 50 52 4f 58 59 3a 20 43 4c 4f 53 45 20 20 25 SPROXY: CLOSE %
ef1b0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b d\n", pFile->h);
ef1c0 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
ef1d0 49 54 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d ITE_OK && pFile-
ef1e0 3e 6f 70 65 6e 46 6c 61 67 73 20 29 7b 0a 20 20 >openFlags ){.
ef1f0 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 if( pFile->h
ef200 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 53 54 >=0 ){.#ifdef ST
ef210 52 49 43 54 5f 43 4c 4f 53 45 5f 45 52 52 4f 52 RICT_CLOSE_ERROR
ef220 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f . if( clo
ef230 73 65 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a se(pFile->h) ){.
ef240 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d pFile-
ef250 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
ef260 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 no;. re
ef270 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
ef280 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 20 R_CLOSE;.
ef290 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 }.#else.
ef2a0 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 close(pFile->h)
ef2b0 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 ; /* silently le
ef2c0 61 6b 20 66 64 20 69 66 20 66 61 69 6c 20 2a 2f ak fd if fail */
ef2d0 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a .#endif. }.
ef2e0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d pFile->h =
ef2f0 20 2d 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 -1;. int f
ef300 64 20 3d 20 6f 70 65 6e 28 70 43 74 78 2d 3e 64 d = open(pCtx->d
ef310 62 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e 6f 70 bPath, pFile->op
ef320 65 6e 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 enFlags,.
ef330 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c SQL
ef340 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 ITE_DEFAULT_FILE
ef350 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 _PERMISSIONS);.
ef360 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 54 OSTRACE2("T
ef370 52 41 4e 53 50 52 4f 58 59 3a 20 4f 50 45 4e 20 RANSPROXY: OPEN
ef380 20 25 64 5c 6e 22 2c 20 66 64 29 3b 0a 20 20 20 %d\n", fd);.
ef390 20 20 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a if( fd>=0 ){.
ef3a0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 pFile->h
ef3b0 20 3d 20 66 64 3b 0a 20 20 20 20 20 20 7d 65 6c = fd;. }el
ef3c0 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 3d 53 se{. rc=S
ef3d0 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 20 QLITE_CANTOPEN;
ef3e0 2f 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 3f 20 /* SQLITE_BUSY?
ef3f0 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 20 63 proxyTakeConch c
ef400 61 6c 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20 alled.
ef410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef420 20 20 20 20 20 64 75 72 69 6e 67 20 6c 6f 63 6b during lock
ef430 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 ing */. }.
ef440 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d }. if( rc=
ef450 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 =SQLITE_OK && !p
ef460 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 29 Ctx->lockProxy )
ef470 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 61 {. char *pa
ef480 74 68 20 3d 20 74 4c 6f 63 6b 50 61 74 68 20 3f th = tLockPath ?
ef490 20 74 4c 6f 63 6b 50 61 74 68 20 3a 20 70 43 74 tLockPath : pCt
ef4a0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 x->lockProxyPath
ef4b0 3b 0a 20 20 20 20 20 20 2f 2a 20 41 43 53 3a 20 ;. /* ACS:
ef4c0 4e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 63 Need to make a c
ef4d0 6f 70 79 20 6f 66 20 70 61 74 68 20 73 6f 6d 65 opy of path some
ef4e0 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20 20 20 72 times */. r
ef4f0 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55 c = proxyCreateU
ef500 6e 69 78 46 69 6c 65 28 70 61 74 68 2c 20 26 70 nixFile(path, &p
ef510 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 29 3b Ctx->lockProxy);
ef520 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 . }. if( r
ef530 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
ef540 20 20 20 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 pCtx->conc
ef550 68 48 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 hHeld = 1;..
ef560 20 20 69 66 28 20 74 4c 6f 63 6b 50 61 74 68 20 if( tLockPath
ef570 29 7b 0a 20 20 20 20 20 20 20 20 70 43 74 78 2d ){. pCtx-
ef580 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d >lockProxyPath =
ef590 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 sqlite3DbStrDup
ef5a0 28 30 2c 20 74 4c 6f 63 6b 50 61 74 68 29 3b 0a (0, tLockPath);.
ef5b0 20 20 20 20 20 20 20 20 69 66 28 20 70 43 74 78 if( pCtx
ef5c0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 ->lockProxy->pMe
ef5d0 74 68 6f 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 thod == &afpIoMe
ef5e0 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 20 thods ){.
ef5f0 20 20 20 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 ((afpLockingC
ef600 6f 6e 74 65 78 74 20 2a 29 70 43 74 78 2d 3e 6c ontext *)pCtx->l
ef610 6f 63 6b 50 72 6f 78 79 2d 3e 6c 6f 63 6b 69 6e ockProxy->lockin
ef620 67 43 6f 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 gContext)->dbPat
ef630 68 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 h =.
ef640 20 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c pCtx->l
ef650 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 ockProxyPath;.
ef660 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
ef670 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 } else {.
ef680 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d conchFile->pM
ef690 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 ethod->xUnlock((
ef6a0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f sqlite3_file*)co
ef6b0 6e 63 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b nchFile, NO_LOCK
ef6c0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 );. }. OST
ef6d0 52 41 43 45 33 28 22 54 41 4b 45 43 4f 4e 43 48 RACE3("TAKECONCH
ef6e0 20 20 25 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 %d %s\n", conc
ef6f0 68 46 69 6c 65 2d 3e 68 2c 20 72 63 3d 3d 53 51 hFile->h, rc==SQ
ef700 4c 49 54 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 66 61 LITE_OK?"ok":"fa
ef710 69 6c 65 64 22 29 3b 0a 20 20 20 20 72 65 74 75 iled");. retu
ef720 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a rn rc;. }.}../*
ef730 0a 2a 2a 20 49 66 20 70 46 69 6c 65 20 68 6f 6c .** If pFile hol
ef740 64 73 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 63 ds a lock on a c
ef750 6f 6e 63 68 20 66 69 6c 65 2c 20 74 68 65 6e 20 onch file, then
ef760 72 65 6c 65 61 73 65 20 74 68 61 74 20 6c 6f 63 release that loc
ef770 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 k..*/.static int
ef780 20 70 72 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e proxyReleaseCon
ef790 63 68 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 ch(unixFile *pFi
ef7a0 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 le){. int rc;
ef7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef7c0 20 20 20 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 /* Subroutine
ef7d0 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a return code */.
ef7e0 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f proxyLockingCo
ef7f0 6e 74 65 78 74 20 2a 70 43 74 78 3b 20 20 2f 2a ntext *pCtx; /*
ef800 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e The locking con
ef810 74 65 78 74 20 66 6f 72 20 74 68 65 20 70 72 6f text for the pro
ef820 78 79 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 69 xy lock */. uni
ef830 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 xFile *conchFile
ef840 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 ; /* Name
ef850 20 6f 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 of the conch fi
ef860 6c 65 20 2a 2f 0a 0a 20 20 70 43 74 78 20 3d 20 le */.. pCtx =
ef870 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e (proxyLockingCon
ef880 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f text *)pFile->lo
ef890 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 ckingContext;.
ef8a0 63 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78 conchFile = pCtx
ef8b0 2d 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 4f ->conchFile;. O
ef8c0 53 54 52 41 43 45 34 28 22 52 45 4c 45 41 53 45 STRACE4("RELEASE
ef8d0 43 4f 4e 43 48 20 20 25 64 20 66 6f 72 20 25 73 CONCH %d for %s
ef8e0 20 70 69 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 pid=%d\n", conc
ef8f0 68 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 hFile->h,.
ef900 20 20 20 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b (pCtx->lock
ef910 50 72 6f 78 79 50 61 74 68 20 3f 20 70 43 74 78 ProxyPath ? pCtx
ef920 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 ->lockProxyPath
ef930 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 0a 20 20 : ":auto:"), .
ef940 20 20 20 20 20 20 20 20 20 67 65 74 70 69 64 28 getpid(
ef950 29 29 3b 0a 20 20 70 43 74 78 2d 3e 63 6f 6e 63 ));. pCtx->conc
ef960 68 48 65 6c 64 20 3d 20 30 3b 0a 20 20 72 63 20 hHeld = 0;. rc
ef970 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 = conchFile->pMe
ef980 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 thod->xUnlock((s
ef990 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e qlite3_file*)con
ef9a0 63 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 chFile, NO_LOCK)
ef9b0 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 52 45 ;. OSTRACE3("RE
ef9c0 4c 45 41 53 45 43 4f 4e 43 48 20 20 25 64 20 25 LEASECONCH %d %
ef9d0 73 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d s\n", conchFile-
ef9e0 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 >h,. (
ef9f0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 rc==SQLITE_OK ?
efa00 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 "ok" : "failed")
efa10 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
efa20 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 }../*.** Given t
efa30 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 he name of a dat
efa40 61 62 61 73 65 20 66 69 6c 65 2c 20 63 6f 6d 70 abase file, comp
efa50 75 74 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 ute the name of
efa60 69 74 73 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a its conch file..
efa70 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 63 6f 6e ** Store the con
efa80 63 68 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6d ch filename in m
efa90 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
efaa0 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c rom sqlite3_mall
efab0 6f 63 28 29 2e 0a 2a 2a 20 4d 61 6b 65 20 2a 70 oc()..** Make *p
efac0 43 6f 6e 63 68 50 61 74 68 20 70 6f 69 6e 74 20 ConchPath point
efad0 74 6f 20 74 68 65 20 6e 65 77 20 6e 61 6d 65 2e to the new name.
efae0 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f Return SQLITE_
efaf0 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a OK on success.**
efb00 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d or SQLITE_NOMEM
efb10 20 69 66 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 if unable to ob
efb20 74 61 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a tain memory..**.
efb30 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 ** The caller is
efb40 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 responsible for
efb50 20 65 6e 73 75 72 69 6e 67 20 74 68 61 74 20 74 ensuring that t
efb60 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d he allocated mem
efb70 6f 72 79 0a 2a 2a 20 73 70 61 63 65 20 69 73 20 ory.** space is
efb80 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 eventually freed
efb90 2e 0a 2a 2a 0a 2a 2a 20 2a 70 43 6f 6e 63 68 50 ..**.** *pConchP
efba0 61 74 68 20 69 73 20 73 65 74 20 74 6f 20 4e 55 ath is set to NU
efbb0 4c 4c 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 LL if a memory a
efbc0 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 llocation error
efbd0 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 occurs..*/.stati
efbe0 63 20 69 6e 74 20 70 72 6f 78 79 43 72 65 61 74 c int proxyCreat
efbf0 65 43 6f 6e 63 68 50 61 74 68 6e 61 6d 65 28 63 eConchPathname(c
efc00 68 61 72 20 2a 64 62 50 61 74 68 2c 20 63 68 61 har *dbPath, cha
efc10 72 20 2a 2a 70 43 6f 6e 63 68 50 61 74 68 29 7b r **pConchPath){
efc20 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
efc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
efc40 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
efc50 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 */. int len =
efc60 28 69 6e 74 29 73 74 72 6c 65 6e 28 64 62 50 61 (int)strlen(dbPa
efc70 74 68 29 3b 20 2f 2a 20 4c 65 6e 67 74 68 20 6f th); /* Length o
efc80 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e f database filen
efc90 61 6d 65 20 2d 20 64 62 50 61 74 68 20 2a 2f 0a ame - dbPath */.
efca0 20 20 63 68 61 72 20 2a 63 6f 6e 63 68 50 61 74 char *conchPat
efcb0 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
efcc0 2f 2a 20 62 75 66 66 65 72 20 69 6e 20 77 68 69 /* buffer in whi
efcd0 63 68 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 ch to construct
efce0 63 6f 6e 63 68 20 6e 61 6d 65 20 2a 2f 0a 0a 20 conch name */..
efcf0 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 /* Allocate spa
efd00 63 65 20 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 ce for the conch
efd10 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6e filename and in
efd20 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6e 61 6d itialize the nam
efd30 65 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6e 61 e to. ** the na
efd40 6d 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e me of the origin
efd50 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 al database file
efd60 2e 20 2a 2f 20 20 0a 20 20 2a 70 43 6f 6e 63 68 . */ . *pConch
efd70 50 61 74 68 20 3d 20 63 6f 6e 63 68 50 61 74 68 Path = conchPath
efd80 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 = (char *)sqlit
efd90 65 33 5f 6d 61 6c 6c 6f 63 28 6c 65 6e 20 2b 20 e3_malloc(len +
efda0 38 29 3b 0a 20 20 69 66 28 20 63 6f 6e 63 68 50 8);. if( conchP
efdb0 61 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ath==0 ){. re
efdc0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
efdd0 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 M;. }. memcpy(
efde0 63 6f 6e 63 68 50 61 74 68 2c 20 64 62 50 61 74 conchPath, dbPat
efdf0 68 2c 20 6c 65 6e 2b 31 29 3b 0a 20 20 0a 20 20 h, len+1);. .
efe00 2f 2a 20 6e 6f 77 20 69 6e 73 65 72 74 20 61 20 /* now insert a
efe10 22 2e 22 20 62 65 66 6f 72 65 20 74 68 65 20 6c "." before the l
efe20 61 73 74 20 2f 20 63 68 61 72 61 63 74 65 72 20 ast / character
efe30 2a 2f 0a 20 20 66 6f 72 28 20 69 3d 28 6c 65 6e */. for( i=(len
efe40 2d 31 29 3b 20 69 3e 3d 30 3b 20 69 2d 2d 20 29 -1); i>=0; i-- )
efe50 7b 0a 20 20 20 20 69 66 28 20 63 6f 6e 63 68 50 {. if( conchP
efe60 61 74 68 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 ath[i]=='/' ){.
efe70 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 i++;.
efe80 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d break;. }. }
efe90 0a 20 20 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d . conchPath[i]=
efea0 27 2e 27 3b 0a 20 20 77 68 69 6c 65 20 28 20 69 '.';. while ( i
efeb0 3c 6c 65 6e 20 29 7b 0a 20 20 20 20 63 6f 6e 63 <len ){. conc
efec0 68 50 61 74 68 5b 69 2b 31 5d 3d 64 62 50 61 74 hPath[i+1]=dbPat
efed0 68 5b 69 5d 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 h[i];. i++;.
efee0 20 7d 0a 0a 20 20 2f 2a 20 61 70 70 65 6e 64 20 }.. /* append
efef0 74 68 65 20 22 2d 63 6f 6e 63 68 22 20 73 75 66 the "-conch" suf
eff00 66 69 78 20 74 6f 20 74 68 65 20 66 69 6c 65 20 fix to the file
eff10 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 26 63 6f 6e */. memcpy(&con
eff20 63 68 50 61 74 68 5b 69 2b 31 5d 2c 20 22 2d 63 chPath[i+1], "-c
eff30 6f 6e 63 68 22 2c 20 37 29 3b 0a 20 20 61 73 73 onch", 7);. ass
eff40 65 72 74 28 20 28 69 6e 74 29 73 74 72 6c 65 6e ert( (int)strlen
eff50 28 63 6f 6e 63 68 50 61 74 68 29 20 3d 3d 20 6c (conchPath) == l
eff60 65 6e 2b 37 20 29 3b 0a 0a 20 20 72 65 74 75 72 en+7 );.. retur
eff70 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
eff80 0a 2f 2a 20 54 61 6b 65 73 20 61 20 66 75 6c 6c ./* Takes a full
eff90 79 20 63 6f 6e 66 69 67 75 72 65 64 20 70 72 6f y configured pro
effa0 78 79 20 6c 6f 63 6b 69 6e 67 2d 73 74 79 6c 65 xy locking-style
effb0 20 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 73 unix file and s
effc0 77 69 74 63 68 65 73 0a 2a 2a 20 74 68 65 20 6c witches.** the l
effd0 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 ocal lock file p
effe0 61 74 68 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ath .*/.static i
efff0 6e 74 20 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f nt switchLockPro
f0000 78 79 50 61 74 68 28 75 6e 69 78 46 69 6c 65 20 xyPath(unixFile
f0010 2a 70 46 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 *pFile, const ch
f0020 61 72 20 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 ar *path) {. pr
f0030 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
f0040 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 t *pCtx = (proxy
f0050 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 LockingContext*)
f0060 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
f0070 6e 74 65 78 74 3b 0a 20 20 63 68 61 72 20 2a 6f ntext;. char *o
f0080 6c 64 50 61 74 68 20 3d 20 70 43 74 78 2d 3e 6c ldPath = pCtx->l
f0090 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 ockProxyPath;.
f00a0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
f00b0 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 46 69 6c 65 OK;.. if( pFile
f00c0 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c ->locktype!=NO_L
f00d0 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 OCK ){. retur
f00e0 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 n SQLITE_BUSY;.
f00f0 20 7d 20 20 0a 0a 20 20 2f 2a 20 6e 6f 74 68 69 } .. /* nothi
f0100 6e 67 20 74 6f 20 64 6f 20 69 66 20 74 68 65 20 ng to do if the
f0110 70 61 74 68 20 69 73 20 4e 55 4c 4c 2c 20 3a 61 path is NULL, :a
f0120 75 74 6f 3a 20 6f 72 20 6d 61 74 63 68 65 73 20 uto: or matches
f0130 74 68 65 20 65 78 69 73 74 69 6e 67 20 70 61 74 the existing pat
f0140 68 20 2a 2f 0a 20 20 69 66 28 20 21 70 61 74 68 h */. if( !path
f0150 20 7c 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 || path[0]=='\0
f0160 27 20 7c 7c 20 21 73 74 72 63 6d 70 28 70 61 74 ' || !strcmp(pat
f0170 68 2c 20 22 3a 61 75 74 6f 3a 22 29 20 7c 7c 0a h, ":auto:") ||.
f0180 20 20 20 20 28 6f 6c 64 50 61 74 68 20 26 26 20 (oldPath &&
f0190 21 73 74 72 6e 63 6d 70 28 6f 6c 64 50 61 74 68 !strncmp(oldPath
f01a0 2c 20 70 61 74 68 2c 20 4d 41 58 50 41 54 48 4c , path, MAXPATHL
f01b0 45 4e 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 EN)) ){. retu
f01c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
f01d0 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 46 }else{. unixF
f01e0 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d ile *lockProxy =
f01f0 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 pCtx->lockProxy
f0200 3b 0a 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b ;. pCtx->lock
f0210 50 72 6f 78 79 3d 4e 55 4c 4c 3b 0a 20 20 20 20 Proxy=NULL;.
f0220 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 pCtx->conchHeld
f0230 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 = 0;. if( loc
f0240 6b 50 72 6f 78 79 21 3d 4e 55 4c 4c 20 29 7b 0a kProxy!=NULL ){.
f0250 20 20 20 20 20 20 72 63 3d 6c 6f 63 6b 50 72 6f rc=lockPro
f0260 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c xy->pMethod->xCl
f0270 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c ose((sqlite3_fil
f0280 65 20 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a e *)lockProxy);.
f0290 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 if( rc ) r
f02a0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
f02b0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f 63 sqlite3_free(loc
f02c0 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20 kProxy);. }.
f02d0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
f02e0 6f 6c 64 50 61 74 68 29 3b 0a 20 20 20 20 70 43 oldPath);. pC
f02f0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 tx->lockProxyPat
f0300 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 h = sqlite3DbStr
f0310 44 75 70 28 30 2c 20 70 61 74 68 29 3b 0a 20 20 Dup(0, path);.
f0320 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e 20 72 63 }. . return rc
f0330 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 46 69 6c 65 ;.}../*.** pFile
f0340 20 69 73 20 61 20 66 69 6c 65 20 74 68 61 74 20 is a file that
f0350 68 61 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 has been opened
f0360 62 79 20 61 20 70 72 69 6f 72 20 78 4f 70 65 6e by a prior xOpen
f0370 20 63 61 6c 6c 2e 20 20 64 62 50 61 74 68 0a 2a call. dbPath.*
f0380 2a 20 69 73 20 61 20 73 74 72 69 6e 67 20 62 75 * is a string bu
f0390 66 66 65 72 20 61 74 20 6c 65 61 73 74 20 4d 41 ffer at least MA
f03a0 58 50 41 54 48 4c 45 4e 2b 31 20 63 68 61 72 61 XPATHLEN+1 chara
f03b0 63 74 65 72 73 20 69 6e 20 73 69 7a 65 2e 0a 2a cters in size..*
f03c0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
f03d0 65 20 66 69 6e 64 20 74 68 65 20 66 69 6c 65 6e e find the filen
f03e0 61 6d 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 ame associated w
f03f0 69 74 68 20 70 46 69 6c 65 20 61 6e 64 20 77 72 ith pFile and wr
f0400 69 74 65 73 20 69 74 0a 2a 2a 20 69 6e 74 20 64 ites it.** int d
f0410 62 50 61 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 bPath..*/.static
f0420 20 69 6e 74 20 70 72 6f 78 79 47 65 74 44 62 50 int proxyGetDbP
f0430 61 74 68 46 6f 72 55 6e 69 78 46 69 6c 65 28 75 athForUnixFile(u
f0440 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 nixFile *pFile,
f0450 63 68 61 72 20 2a 64 62 50 61 74 68 29 7b 0a 23 char *dbPath){.#
f0460 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 if defined(__APP
f0470 4c 45 5f 5f 29 0a 20 20 69 66 28 20 70 46 69 6c LE__). if( pFil
f0480 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 e->pMethod == &a
f0490 66 70 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 fpIoMethods ){.
f04a0 20 20 20 2f 2a 20 61 66 70 20 73 74 79 6c 65 20 /* afp style
f04b0 6b 65 65 70 73 20 61 20 72 65 66 65 72 65 6e 63 keeps a referenc
f04c0 65 20 74 6f 20 74 68 65 20 64 62 20 70 61 74 68 e to the db path
f04d0 20 69 6e 20 74 68 65 20 66 69 6c 65 50 61 74 68 in the filePath
f04e0 20 66 69 65 6c 64 20 0a 20 20 20 20 2a 2a 20 6f field . ** o
f04f0 66 20 74 68 65 20 73 74 72 75 63 74 20 2a 2f 0a f the struct */.
f0500 20 20 20 20 61 73 73 65 72 74 28 20 28 69 6e 74 assert( (int
f0510 29 73 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 )strlen((char*)p
f0520 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
f0530 74 65 78 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 text)<=MAXPATHLE
f0540 4e 20 29 3b 0a 20 20 20 20 73 74 72 63 70 79 28 N );. strcpy(
f0550 64 62 50 61 74 68 2c 20 28 28 61 66 70 4c 6f 63 dbPath, ((afpLoc
f0560 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 kingContext *)pF
f0570 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
f0580 65 78 74 29 2d 3e 64 62 50 61 74 68 29 3b 0a 20 ext)->dbPath);.
f0590 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 }else.#endif.
f05a0 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 if( pFile->pMeth
f05b0 6f 64 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f od == &dotlockIo
f05c0 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f Methods ){. /
f05d0 2a 20 64 6f 74 20 6c 6f 63 6b 20 73 74 79 6c 65 * dot lock style
f05e0 20 75 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e uses the lockin
f05f0 67 20 63 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f g context to sto
f0600 72 65 20 74 68 65 20 64 6f 74 20 6c 6f 63 6b 0a re the dot lock.
f0610 20 20 20 20 2a 2a 20 66 69 6c 65 20 70 61 74 68 ** file path
f0620 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e 20 */. int len
f0630 3d 20 73 74 72 6c 65 6e 28 28 63 68 61 72 20 2a = strlen((char *
f0640 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 )pFile->lockingC
f0650 6f 6e 74 65 78 74 29 20 2d 20 73 74 72 6c 65 6e ontext) - strlen
f0660 28 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 29 (DOTLOCK_SUFFIX)
f0670 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 64 62 50 ;. memcpy(dbP
f0680 61 74 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69 ath, (char *)pFi
f0690 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
f06a0 78 74 2c 20 6c 65 6e 20 2b 20 31 29 3b 0a 20 20 xt, len + 1);.
f06b0 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 61 6c }else{. /* al
f06c0 6c 20 6f 74 68 65 72 20 73 74 79 6c 65 73 20 75 l other styles u
f06d0 73 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 se the locking c
f06e0 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 65 20 ontext to store
f06f0 74 68 65 20 64 62 20 66 69 6c 65 20 70 61 74 68 the db file path
f0700 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
f0710 73 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 strlen((char*)pF
f0720 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
f0730 65 78 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e ext)<=MAXPATHLEN
f0740 20 29 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 );. strcpy(d
f0750 62 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29 70 bPath, (char *)p
f0760 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
f0770 74 65 78 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 text);. }. ret
f0780 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
f0790 0a 0a 2f 2a 0a 2a 2a 20 54 61 6b 65 73 20 61 6e ../*.** Takes an
f07a0 20 61 6c 72 65 61 64 79 20 66 69 6c 6c 65 64 20 already filled
f07b0 69 6e 20 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 in unix file and
f07c0 20 61 6c 74 65 72 73 20 69 74 20 73 6f 20 61 6c alters it so al
f07d0 6c 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 0a l file locking .
f07e0 2a 2a 20 77 69 6c 6c 20 62 65 20 70 65 72 66 6f ** will be perfo
f07f0 72 6d 65 64 20 6f 6e 20 74 68 65 20 6c 6f 63 61 rmed on the loca
f0800 6c 20 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c l proxy lock fil
f0810 65 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e e. The followin
f0820 67 20 66 69 65 6c 64 73 0a 2a 2a 20 61 72 65 20 g fields.** are
f0830 70 72 65 73 65 72 76 65 64 20 69 6e 20 74 68 65 preserved in the
f0840 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 locking context
f0850 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 63 61 so that they ca
f0860 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 61 6e n be restored an
f0870 64 20 0a 2a 2a 20 74 68 65 20 75 6e 69 78 20 73 d .** the unix s
f0880 74 72 75 63 74 75 72 65 20 70 72 6f 70 65 72 6c tructure properl
f0890 79 20 63 6c 65 61 6e 65 64 20 75 70 20 61 74 20 y cleaned up at
f08a0 63 6c 6f 73 65 20 74 69 6d 65 3a 0a 2a 2a 20 20 close time:.**
f08b0 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
f08c0 0a 2a 2a 20 20 2d 3e 70 4d 65 74 68 6f 64 0a 2a .** ->pMethod.*
f08d0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f /.static int pro
f08e0 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 xyTransformUnixF
f08f0 69 6c 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 ile(unixFile *pF
f0900 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 ile, const char
f0910 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 6f 78 79 *path) {. proxy
f0920 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a LockingContext *
f0930 70 43 74 78 3b 0a 20 20 63 68 61 72 20 64 62 50 pCtx;. char dbP
f0940 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e 2b 31 ath[MAXPATHLEN+1
f0950 5d 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 ]; /* Name
f0960 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
f0970 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 file */. char
f0980 2a 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a *lockPath=NULL;.
f0990 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
f09a0 45 5f 4f 4b 3b 0a 20 20 0a 20 20 69 66 28 20 70 E_OK;. . if( p
f09b0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d File->locktype!=
f09c0 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 NO_LOCK ){. r
f09d0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 eturn SQLITE_BUS
f09e0 59 3b 0a 20 20 7d 0a 20 20 70 72 6f 78 79 47 65 Y;. }. proxyGe
f09f0 74 44 62 50 61 74 68 46 6f 72 55 6e 69 78 46 69 tDbPathForUnixFi
f0a00 6c 65 28 70 46 69 6c 65 2c 20 64 62 50 61 74 68 le(pFile, dbPath
f0a10 29 3b 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c );. if( !path |
f0a20 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 | path[0]=='\0'
f0a30 7c 7c 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c || !strcmp(path,
f0a40 20 22 3a 61 75 74 6f 3a 22 29 20 29 7b 0a 20 20 ":auto:") ){.
f0a50 20 20 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b lockPath=NULL;
f0a60 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f . }else{. lo
f0a70 63 6b 50 61 74 68 3d 28 63 68 61 72 20 2a 29 70 ckPath=(char *)p
f0a80 61 74 68 3b 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 ath;. }. . OS
f0a90 54 52 41 43 45 34 28 22 54 52 41 4e 53 50 52 4f TRACE4("TRANSPRO
f0aa0 58 59 20 20 25 64 20 66 6f 72 20 25 73 20 70 69 XY %d for %s pi
f0ab0 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e d=%d\n", pFile->
f0ac0 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 6c h,. (l
f0ad0 6f 63 6b 50 61 74 68 20 3f 20 6c 6f 63 6b 50 61 ockPath ? lockPa
f0ae0 74 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 th : ":auto:"),
f0af0 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 70 43 getpid());.. pC
f0b00 74 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c tx = sqlite3_mal
f0b10 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 43 74 loc( sizeof(*pCt
f0b20 78 29 20 29 3b 0a 20 20 69 66 28 20 70 43 74 78 x) );. if( pCtx
f0b30 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
f0b40 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
f0b50 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 43 74 }. memset(pCt
f0b60 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 x, 0, sizeof(*pC
f0b70 74 78 29 29 3b 0a 0a 20 20 72 63 20 3d 20 70 72 tx));.. rc = pr
f0b80 6f 78 79 43 72 65 61 74 65 43 6f 6e 63 68 50 61 oxyCreateConchPa
f0b90 74 68 6e 61 6d 65 28 64 62 50 61 74 68 2c 20 26 thname(dbPath, &
f0ba0 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 pCtx->conchFileP
f0bb0 61 74 68 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d ath);. if( rc==
f0bc0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
f0bd0 20 72 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74 rc = proxyCreat
f0be0 65 55 6e 69 78 46 69 6c 65 28 70 43 74 78 2d 3e eUnixFile(pCtx->
f0bf0 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 2c 20 26 conchFilePath, &
f0c00 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 pCtx->conchFile)
f0c10 3b 0a 20 20 7d 20 20 0a 20 20 69 66 28 20 72 63 ;. } . if( rc
f0c20 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c ==SQLITE_OK && l
f0c30 6f 63 6b 50 61 74 68 20 29 7b 0a 20 20 20 20 70 ockPath ){. p
f0c40 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 Ctx->lockProxyPa
f0c50 74 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 th = sqlite3DbSt
f0c60 72 44 75 70 28 30 2c 20 6c 6f 63 6b 50 61 74 68 rDup(0, lockPath
f0c70 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 );. }.. if( rc
f0c80 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
f0c90 20 20 20 2f 2a 20 61 6c 6c 20 6d 65 6d 6f 72 79 /* all memory
f0ca0 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 70 is allocated, p
f0cb0 72 6f 78 79 73 20 61 72 65 20 63 72 65 61 74 65 roxys are create
f0cc0 64 20 61 6e 64 20 61 73 73 69 67 6e 65 64 2c 20 d and assigned,
f0cd0 0a 20 20 20 20 2a 2a 20 73 77 69 74 63 68 20 74 . ** switch t
f0ce0 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 he locking conte
f0cf0 78 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 xt and pMethod t
f0d00 68 65 6e 20 72 65 74 75 72 6e 2e 0a 20 20 20 20 hen return..
f0d10 2a 2f 0a 20 20 20 20 70 43 74 78 2d 3e 64 62 50 */. pCtx->dbP
f0d20 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 ath = sqlite3DbS
f0d30 74 72 44 75 70 28 30 2c 20 64 62 50 61 74 68 29 trDup(0, dbPath)
f0d40 3b 0a 20 20 20 20 70 43 74 78 2d 3e 6f 6c 64 4c ;. pCtx->oldL
f0d50 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 ockingContext =
f0d60 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
f0d70 6e 74 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 ntext;. pFile
f0d80 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
f0d90 20 3d 20 70 43 74 78 3b 0a 20 20 20 20 70 43 74 = pCtx;. pCt
f0da0 78 2d 3e 70 4f 6c 64 4d 65 74 68 6f 64 20 3d 20 x->pOldMethod =
f0db0 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 3b 0a pFile->pMethod;.
f0dc0 20 20 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 pFile->pMeth
f0dd0 6f 64 20 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 od = &proxyIoMet
f0de0 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 hods;. }else{.
f0df0 20 20 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e if( pCtx->con
f0e00 63 68 46 69 6c 65 20 29 7b 20 0a 20 20 20 20 20 chFile ){ .
f0e10 20 72 63 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 rc = pCtx->conc
f0e20 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e hFile->pMethod->
f0e30 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f xClose((sqlite3_
f0e40 66 69 6c 65 20 2a 29 70 43 74 78 2d 3e 63 6f 6e file *)pCtx->con
f0e50 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 chFile);. i
f0e60 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
f0e70 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 c;. sqlite3
f0e80 5f 66 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 _free(pCtx->conc
f0e90 68 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 hFile);. }.
f0ea0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
f0eb0 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 Ctx->conchFilePa
f0ec0 74 68 29 3b 20 0a 20 20 20 20 73 71 6c 69 74 65 th); . sqlite
f0ed0 33 5f 66 72 65 65 28 70 43 74 78 29 3b 0a 20 20 3_free(pCtx);.
f0ee0 7d 0a 20 20 4f 53 54 52 41 43 45 33 28 22 54 52 }. OSTRACE3("TR
f0ef0 41 4e 53 50 52 4f 58 59 20 20 25 64 20 25 73 5c ANSPROXY %d %s\
f0f00 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 n", pFile->h,.
f0f10 20 20 20 20 20 20 20 20 20 28 72 63 3d 3d 53 51 (rc==SQ
f0f20 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a LITE_OK ? "ok" :
f0f30 20 22 66 61 69 6c 65 64 22 29 29 3b 0a 20 20 72 "failed"));. r
f0f40 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
f0f50 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
f0f60 20 68 61 6e 64 6c 65 73 20 73 71 6c 69 74 65 33 handles sqlite3
f0f70 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 20 _file_control()
f0f80 63 61 6c 6c 73 20 74 68 61 74 20 61 72 65 20 73 calls that are s
f0f90 70 65 63 69 66 69 63 0a 2a 2a 20 74 6f 20 70 72 pecific.** to pr
f0fa0 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a oxy locking..*/.
f0fb0 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
f0fc0 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 FileControl(sqli
f0fd0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
f0fe0 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 t op, void *pArg
f0ff0 29 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 ){. switch( op
f1000 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 ){. case SQLI
f1010 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 TE_GET_LOCKPROXY
f1020 46 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e FILE: {. un
f1030 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
f1040 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
f1050 20 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e if( pFile->
f1060 70 4d 65 74 68 6f 64 20 3d 3d 20 26 70 72 6f 78 pMethod == &prox
f1070 79 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 yIoMethods ){.
f1080 20 20 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 proxyLocki
f1090 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 ngContext *pCtx
f10a0 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 = (proxyLockingC
f10b0 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c ontext*)pFile->l
f10c0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 ockingContext;.
f10d0 20 20 20 20 20 20 20 70 72 6f 78 79 54 61 6b 65 proxyTake
f10e0 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 Conch(pFile);.
f10f0 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e if( pCtx->
f1100 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b lockProxyPath ){
f1110 0a 20 20 20 20 20 20 20 20 20 20 2a 28 63 6f 6e . *(con
f1120 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 st char **)pArg
f1130 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 = pCtx->lockProx
f1140 79 50 61 74 68 3b 0a 20 20 20 20 20 20 20 20 7d yPath;. }
f1150 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
f1160 2a 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 *(const char **)
f1170 70 41 72 67 20 3d 20 22 3a 61 75 74 6f 3a 20 28 pArg = ":auto: (
f1180 6e 6f 74 20 68 65 6c 64 29 22 3b 0a 20 20 20 20 not held)";.
f1190 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 65 6c }. } el
f11a0 73 65 20 7b 0a 20 20 20 20 20 20 20 20 2a 28 63 se {. *(c
f11b0 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 onst char **)pAr
f11c0 67 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 g = NULL;.
f11d0 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 }. return S
f11e0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
f11f0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
f1200 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c SET_LOCKPROXYFIL
f1210 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 E: {. unixF
f1220 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
f1230 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 ixFile*)id;.
f1240 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
f1250 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 69 6e 74 20 E_OK;. int
f1260 69 73 50 72 6f 78 79 53 74 79 6c 65 20 3d 20 28 isProxyStyle = (
f1270 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d pFile->pMethod =
f1280 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 = &proxyIoMethod
f1290 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 41 s);. if( pA
f12a0 72 67 3d 3d 4e 55 4c 4c 20 7c 7c 20 28 63 6f 6e rg==NULL || (con
f12b0 73 74 20 63 68 61 72 20 2a 29 70 41 72 67 3d 3d st char *)pArg==
f12c0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 0 ){. if(
f12d0 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20 29 7b isProxyStyle ){
f12e0 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75 . /* tu
f12f0 72 6e 20 6f 66 66 20 70 72 6f 78 79 20 6c 6f 63 rn off proxy loc
f1300 6b 69 6e 67 20 2d 20 6e 6f 74 20 73 75 70 70 6f king - not suppo
f1310 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 rted */.
f1320 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 rc = SQLITE_ER
f1330 52 4f 52 20 2f 2a 53 51 4c 49 54 45 5f 50 52 4f ROR /*SQLITE_PRO
f1340 54 4f 43 4f 4c 3f 20 53 51 4c 49 54 45 5f 4d 49 TOCOL? SQLITE_MI
f1350 53 55 53 45 3f 2a 2f 3b 0a 20 20 20 20 20 20 20 SUSE?*/;.
f1360 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
f1370 20 20 2f 2a 20 74 75 72 6e 20 6f 66 66 20 70 72 /* turn off pr
f1380 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 2d 20 61 6c oxy locking - al
f1390 72 65 61 64 79 20 6f 66 66 20 2d 20 4e 4f 4f 50 ready off - NOOP
f13a0 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 */. rc
f13b0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
f13c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 }. }e
f13d0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e lse{. con
f13e0 73 74 20 63 68 61 72 20 2a 70 72 6f 78 79 50 61 st char *proxyPa
f13f0 74 68 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 th = (const char
f1400 20 2a 29 70 41 72 67 3b 0a 20 20 20 20 20 20 20 *)pArg;.
f1410 20 69 66 28 20 69 73 50 72 6f 78 79 53 74 79 6c if( isProxyStyl
f1420 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 e ){. p
f1430 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
f1440 78 74 20 2a 70 43 74 78 20 3d 20 0a 20 20 20 20 xt *pCtx = .
f1450 20 20 20 20 20 20 20 20 28 70 72 6f 78 79 4c 6f (proxyLo
f1460 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 ckingContext*)pF
f1470 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
f1480 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 69 ext;. i
f1490 66 28 20 21 73 74 72 63 6d 70 28 70 41 72 67 2c f( !strcmp(pArg,
f14a0 20 22 3a 61 75 74 6f 3a 22 29 20 0a 20 20 20 20 ":auto:") .
f14b0 20 20 20 20 20 20 20 7c 7c 20 28 70 43 74 78 2d || (pCtx-
f14c0 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 26 >lockProxyPath &
f14d0 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 &.
f14e0 20 21 73 74 72 6e 63 6d 70 28 70 43 74 78 2d 3e !strncmp(pCtx->
f14f0 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 2c 20 70 lockProxyPath, p
f1500 72 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 roxyPath, MAXPAT
f1510 48 4c 45 4e 29 29 0a 20 20 20 20 20 20 20 20 20 HLEN)).
f1520 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
f1530 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
f1540 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
f1550 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 . rc
f1560 3d 20 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 = switchLockProx
f1570 79 50 61 74 68 28 70 46 69 6c 65 2c 20 70 72 6f yPath(pFile, pro
f1580 78 79 50 61 74 68 29 3b 0a 20 20 20 20 20 20 20 xyPath);.
f1590 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c }. }el
f15a0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a se{. /*
f15b0 20 74 75 72 6e 20 6f 6e 20 70 72 6f 78 79 20 66 turn on proxy f
f15c0 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 ile locking */.
f15d0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 rc = pr
f15e0 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 oxyTransformUnix
f15f0 46 69 6c 65 28 70 46 69 6c 65 2c 20 70 72 6f 78 File(pFile, prox
f1600 79 50 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 yPath);.
f1610 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
f1620 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
f1630 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a . default: {.
f1640 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 assert( 0
f1650 29 3b 20 20 2f 2a 20 54 68 65 20 63 61 6c 6c 20 ); /* The call
f1660 61 73 73 75 72 65 73 20 74 68 61 74 20 6f 6e 6c assures that onl
f1670 79 20 76 61 6c 69 64 20 6f 70 63 6f 64 65 73 20 y valid opcodes
f1680 61 72 65 20 73 65 6e 74 20 2a 2f 0a 20 20 20 20 are sent */.
f1690 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 }. }. /*NOTREA
f16a0 43 48 45 44 2a 2f 0a 20 20 72 65 74 75 72 6e 20 CHED*/. return
f16b0 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a SQLITE_ERROR;.}.
f16c0 0a 2f 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 ./*.** Within th
f16d0 69 73 20 64 69 76 69 73 69 6f 6e 20 28 74 68 65 is division (the
f16e0 20 70 72 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e proxying lockin
f16f0 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e g implementation
f1700 29 20 74 68 65 20 70 72 6f 63 65 64 75 72 65 73 ) the procedures
f1710 0a 2a 2a 20 61 62 6f 76 65 20 74 68 69 73 20 70 .** above this p
f1720 6f 69 6e 74 20 61 72 65 20 61 6c 6c 20 75 74 69 oint are all uti
f1730 6c 69 74 69 65 73 2e 20 20 54 68 65 20 6c 6f 63 lities. The loc
f1740 6b 2d 72 65 6c 61 74 65 64 20 6d 65 74 68 6f 64 k-related method
f1750 73 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 78 s of the.** prox
f1760 79 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 y-locking sqlite
f1770 33 5f 69 6f 5f 6d 65 74 68 6f 64 20 6f 62 6a 65 3_io_method obje
f1780 63 74 20 66 6f 6c 6c 6f 77 2e 0a 2a 2f 0a 0a 0a ct follow..*/...
f1790 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
f17a0 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 ne checks if the
f17b0 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 re is a RESERVED
f17c0 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 lock held on th
f17d0 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 e specified.** f
f17e0 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 ile by this or a
f17f0 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 ny other process
f1800 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b . If such a lock
f1810 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 is held, set *p
f1820 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e ResOut.** to a n
f1830 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 on-zero value ot
f1840 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 herwise *pResOut
f1850 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e is set to zero.
f1860 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c The return val
f1870 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 ue.** is set to
f1880 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 SQLITE_OK unless
f1890 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 an I/O error oc
f18a0 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b curs during lock
f18b0 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 checking..*/.st
f18c0 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 68 atic int proxyCh
f18d0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
f18e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
f18f0 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 , int *pResOut)
f1900 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 {. unixFile *pF
f1910 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
f1920 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 )id;. int rc =
f1930 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 proxyTakeConch(p
f1940 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d File);. if( rc=
f1950 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
f1960 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f proxyLockingCo
f1970 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 ntext *pCtx = (p
f1980 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
f1990 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b xt *)pFile->lock
f19a0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 ingContext;.
f19b0 75 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 unixFile *proxy
f19c0 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 = pCtx->lockProx
f19d0 79 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 72 y;. return pr
f19e0 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 oxy->pMethod->xC
f19f0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
f1a00 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 ((sqlite3_file*)
f1a10 70 72 6f 78 79 2c 20 70 52 65 73 4f 75 74 29 3b proxy, pResOut);
f1a20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
f1a30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 ;.}../*.** Lock
f1a40 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 the file with th
f1a50 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 e lock specified
f1a60 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f by parameter lo
f1a70 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 cktype - one.**
f1a80 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
f1a90 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 :.**.** (1)
f1aa0 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 SHARED_LOCK.**
f1ab0 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f (2) RESERVED_
f1ac0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 LOCK.** (3)
f1ad0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 PENDING_LOCK.**
f1ae0 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 (4) EXCLUSIV
f1af0 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d E_LOCK.**.** Som
f1b00 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 etimes when requ
f1b10 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 esting one lock
f1b20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 state, additiona
f1b30 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a l lock states.**
f1b40 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e are inserted in
f1b50 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c between. The l
f1b60 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 ocking might fai
f1b70 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 l on one of the
f1b80 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 later.** transit
f1b90 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 ions leaving the
f1ba0 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 lock state diff
f1bb0 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 erent from what
f1bc0 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a it started but.*
f1bd0 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 * still short of
f1be0 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 its goal. The
f1bf0 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 following chart
f1c00 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 shows the allowe
f1c10 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 d.** transitions
f1c20 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 and the inserte
f1c30 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 d intermediate s
f1c40 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 tates:.**.**
f1c50 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 UNLOCKED -> SHAR
f1c60 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 ED.** SHARED
f1c70 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 -> RESERVED.**
f1c80 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e SHARED -> (PEN
f1c90 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 DING) -> EXCLUSI
f1ca0 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 VE.** RESERVE
f1cb0 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d D -> (PENDING) -
f1cc0 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 > EXCLUSIVE.**
f1cd0 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 PENDING -> EXC
f1ce0 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 LUSIVE.**.** Thi
f1cf0 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f s routine will o
f1d00 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c nly increase a l
f1d10 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 ock. Use the sq
f1d20 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a lite3OsUnlock().
f1d30 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f ** routine to lo
f1d40 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 wer a locking le
f1d50 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 vel..*/.static i
f1d60 6e 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c nt proxyLock(sql
f1d70 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
f1d80 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 nt locktype) {.
f1d90 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
f1da0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
f1db0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f ;. int rc = pro
f1dc0 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c xyTakeConch(pFil
f1dd0 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 e);. if( rc==SQ
f1de0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 LITE_OK ){. p
f1df0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
f1e00 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 xt *pCtx = (prox
f1e10 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
f1e20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 *)pFile->locking
f1e30 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 Context;. uni
f1e40 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 xFile *proxy = p
f1e50 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a Ctx->lockProxy;.
f1e60 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e rc = proxy->
f1e70 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 pMethod->xLock((
f1e80 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 sqlite3_file*)pr
f1e90 6f 78 79 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a oxy, locktype);.
f1ea0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
f1eb0 79 70 65 20 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63 ype = proxy->loc
f1ec0 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 ktype;. }. ret
f1ed0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a urn rc;.}.../*.*
f1ee0 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b * Lower the lock
f1ef0 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c ing level on fil
f1f00 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 e descriptor pFi
f1f10 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 le to locktype.
f1f20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 locktype.** mus
f1f30 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c t be either NO_L
f1f40 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f OCK or SHARED_LO
f1f50 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 CK..**.** If the
f1f60 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f locking level o
f1f70 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 f the file descr
f1f80 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 iptor is already
f1f90 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 at or below.**
f1fa0 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f the requested lo
f1fb0 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 cking level, thi
f1fc0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e s routine is a n
f1fd0 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 o-op..*/.static
f1fe0 69 6e 74 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 28 int proxyUnlock(
f1ff0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
f2000 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 , int locktype)
f2010 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 {. unixFile *pF
f2020 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
f2030 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 )id;. int rc =
f2040 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 proxyTakeConch(p
f2050 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d File);. if( rc=
f2060 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
f2070 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f proxyLockingCo
f2080 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 ntext *pCtx = (p
f2090 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
f20a0 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b xt *)pFile->lock
f20b0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 ingContext;.
f20c0 75 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 unixFile *proxy
f20d0 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 = pCtx->lockProx
f20e0 79 3b 0a 20 20 20 20 72 63 20 3d 20 70 72 6f 78 y;. rc = prox
f20f0 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c y->pMethod->xUnl
f2100 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c ock((sqlite3_fil
f2110 65 2a 29 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 e*)proxy, lockty
f2120 70 65 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e pe);. pFile->
f2130 6c 6f 63 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 locktype = proxy
f2140 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a ->locktype;. }.
f2150 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
f2160 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 /*.** Close a fi
f2170 6c 65 20 74 68 61 74 20 75 73 65 73 20 70 72 6f le that uses pro
f2180 78 79 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 xy locks..*/.sta
f2190 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f tic int proxyClo
f21a0 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 se(sqlite3_file
f21b0 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 *id) {. if( id
f21c0 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 ){. unixFile
f21d0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
f21e0 6c 65 2a 29 69 64 3b 0a 20 20 20 20 70 72 6f 78 le*)id;. prox
f21f0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
f2200 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f *pCtx = (proxyLo
f2210 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 ckingContext *)p
f2220 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
f2230 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 text;. unixFi
f2240 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 le *lockProxy =
f2250 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b pCtx->lockProxy;
f2260 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 . unixFile *c
f2270 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d onchFile = pCtx-
f2280 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 >conchFile;.
f2290 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
f22a0 4f 4b 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 OK;. . if(
f22b0 20 6c 6f 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20 lockProxy ){.
f22c0 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f rc = lockPro
f22d0 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e xy->pMethod->xUn
f22e0 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 lock((sqlite3_fi
f22f0 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 2c 20 4e le*)lockProxy, N
f2300 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 O_LOCK);. i
f2310 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
f2320 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f c;. rc = lo
f2330 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 ckProxy->pMethod
f2340 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 ->xClose((sqlite
f2350 33 5f 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 3_file*)lockProx
f2360 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 y);. if( rc
f2370 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
f2380 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
f2390 28 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 (lockProxy);.
f23a0 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f pCtx->lockPro
f23b0 78 79 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 xy = 0;. }.
f23c0 20 20 69 66 28 20 63 6f 6e 63 68 46 69 6c 65 20 if( conchFile
f23d0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 74 ){. if( pCt
f23e0 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 29 7b 0a x->conchHeld ){.
f23f0 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f rc = pro
f2400 78 79 52 65 6c 65 61 73 65 43 6f 6e 63 68 28 70 xyReleaseConch(p
f2410 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 69 File);. i
f2420 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
f2430 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 c;. }.
f2440 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d rc = conchFile-
f2450 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 >pMethod->xClose
f2460 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 ((sqlite3_file*)
f2470 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 conchFile);.
f2480 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
f2490 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 n rc;. sqli
f24a0 74 65 33 5f 66 72 65 65 28 63 6f 6e 63 68 46 69 te3_free(conchFi
f24b0 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 le);. }. s
f24c0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 qlite3_free(pCtx
f24d0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 29 ->lockProxyPath)
f24e0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 ;. sqlite3_fr
f24f0 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 ee(pCtx->conchFi
f2500 6c 65 50 61 74 68 29 3b 0a 20 20 20 20 73 71 6c lePath);. sql
f2510 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e ite3_free(pCtx->
f2520 64 62 50 61 74 68 29 3b 0a 20 20 20 20 2f 2a 20 dbPath);. /*
f2530 72 65 73 74 6f 72 65 20 74 68 65 20 6f 72 69 67 restore the orig
f2540 69 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e inal locking con
f2550 74 65 78 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 text and pMethod
f2560 20 74 68 65 6e 20 63 6c 6f 73 65 20 69 74 20 2a then close it *
f2570 2f 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 /. pFile->loc
f2580 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 kingContext = pC
f2590 74 78 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f tx->oldLockingCo
f25a0 6e 74 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 ntext;. pFile
f25b0 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70 43 74 78 ->pMethod = pCtx
f25c0 2d 3e 70 4f 6c 64 4d 65 74 68 6f 64 3b 0a 20 20 ->pOldMethod;.
f25d0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
f25e0 43 74 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e Ctx);. return
f25f0 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d pFile->pMethod-
f2600 3e 78 43 6c 6f 73 65 28 69 64 29 3b 0a 20 20 7d >xClose(id);. }
f2610 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
f2620 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 23 65 6e 64 69 66 _OK;.}....#endif
f2630 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 /* defined(__AP
f2640 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 PLE__) && SQLITE
f2650 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
f2660 53 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 STYLE */./*.** T
f2670 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 he proxy locking
f2680 20 73 74 79 6c 65 20 69 73 20 69 6e 74 65 6e 64 style is intend
f2690 65 64 20 66 6f 72 20 75 73 65 20 77 69 74 68 20 ed for use with
f26a0 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d 73 2e AFP filesystems.
f26b0 0a 2a 2a 20 41 6e 64 20 73 69 6e 63 65 20 41 46 .** And since AF
f26c0 50 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 P is only suppor
f26d0 74 65 64 20 6f 6e 20 4d 61 63 4f 53 58 2c 20 74 ted on MacOSX, t
f26e0 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 he proxy locking
f26f0 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 74 is also.** rest
f2700 72 69 63 74 65 64 20 74 6f 20 4d 61 63 4f 53 58 ricted to MacOSX
f2710 2e 0a 2a 2a 20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a ..** .**.*******
f2720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
f2730 20 6f 66 20 74 68 65 20 70 72 6f 78 79 20 6c 6f of the proxy lo
f2740 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ck implementatio
f2750 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n **************
f2760 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a ********.*******
f2770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f27a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f27b0 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 *******/../*.**
f27c0 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f Initialize the o
f27d0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
f27e0 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a interface..**.**
f27f0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 This routine re
f2800 67 69 73 74 65 72 73 20 61 6c 6c 20 56 46 53 20 gisters all VFS
f2810 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
f2820 66 6f 72 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 for unix-like op
f2830 65 72 61 74 69 6e 67 0a 2a 2a 20 73 79 73 74 65 erating.** syste
f2840 6d 73 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e ms. This routin
f2850 65 2c 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 e, and the sqlit
f2860 65 33 5f 6f 73 5f 65 6e 64 28 29 20 72 6f 75 74 e3_os_end() rout
f2870 69 6e 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 ine that follows
f2880 2c 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 74 ,.** should be t
f2890 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 he only routines
f28a0 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 74 68 in this file th
f28b0 61 74 20 61 72 65 20 76 69 73 69 62 6c 65 20 66 at are visible f
f28c0 72 6f 6d 20 6f 74 68 65 72 0a 2a 2a 20 66 69 6c rom other.** fil
f28d0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 es..**.** This r
f28e0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
f28f0 20 6f 6e 63 65 20 64 75 72 69 6e 67 20 53 51 4c once during SQL
f2900 69 74 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 ite initializati
f2910 6f 6e 20 61 6e 64 20 62 79 20 61 0a 2a 2a 20 73 on and by a.** s
f2920 69 6e 67 6c 65 20 74 68 72 65 61 64 2e 20 20 54 ingle thread. T
f2930 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
f2940 74 69 6f 6e 20 61 6e 64 20 6d 75 74 65 78 20 73 tion and mutex s
f2950 75 62 73 79 73 74 65 6d 73 20 68 61 76 65 20 6e ubsystems have n
f2960 6f 74 0a 2a 2a 20 6e 65 63 65 73 73 61 72 69 6c ot.** necessaril
f2970 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a y been initializ
f2980 65 64 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 ed when this rou
f2990 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 tine is called,
f29a0 61 6e 64 20 73 6f 20 74 68 65 79 0a 2a 2a 20 73 and so they.** s
f29b0 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 hould not be use
f29c0 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 d..*/.SQLITE_API
f29d0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f int sqlite3_os_
f29e0 69 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f init(void){ . /
f29f0 2a 20 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c * . ** The foll
f2a00 6f 77 69 6e 67 20 6d 61 63 72 6f 20 64 65 66 69 owing macro defi
f2a10 6e 65 73 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a nes an initializ
f2a20 65 72 20 66 6f 72 20 61 6e 20 73 71 6c 69 74 65 er for an sqlite
f2a30 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e 0a 20 20 3_vfs object..
f2a40 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 ** The name of t
f2a50 68 65 20 56 46 53 20 69 73 20 4e 41 4d 45 2e 20 he VFS is NAME.
f2a60 20 54 68 65 20 70 41 70 70 44 61 74 61 20 69 73 The pAppData is
f2a70 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
f2a80 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 74 6f 20 pointer. ** to
f2a90 74 68 65 20 22 66 69 6e 64 65 72 22 20 66 75 6e the "finder" fun
f2aa0 63 74 69 6f 6e 2e 20 20 28 70 41 70 70 44 61 74 ction. (pAppDat
f2ab0 61 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 a is a pointer t
f2ac0 6f 20 61 20 70 6f 69 6e 74 65 72 20 62 65 63 61 o a pointer beca
f2ad0 75 73 65 0a 20 20 2a 2a 20 73 69 6c 6c 79 20 43 use. ** silly C
f2ae0 39 30 20 72 75 6c 65 73 20 70 72 6f 68 69 62 69 90 rules prohibi
f2af0 74 20 61 20 76 6f 69 64 2a 20 66 72 6f 6d 20 62 t a void* from b
f2b00 65 69 6e 67 20 63 61 73 74 20 74 6f 20 61 20 66 eing cast to a f
f2b10 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 0a unction pointer.
f2b20 20 20 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 68 ** and so we h
f2b30 61 76 65 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 ave to go throug
f2b40 68 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61 h the intermedia
f2b50 74 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 76 te pointer to av
f2b60 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a oid problems. *
f2b70 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 * when compiling
f2b80 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d with -pedantic-
f2b90 65 72 72 6f 72 73 20 6f 6e 20 47 43 43 2e 29 0a errors on GCC.).
f2ba0 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 46 49 **. ** The FI
f2bb0 4e 44 45 52 20 70 61 72 61 6d 65 74 65 72 20 74 NDER parameter t
f2bc0 6f 20 74 68 69 73 20 6d 61 63 72 6f 20 69 73 20 o this macro is
f2bd0 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
f2be0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 20 pointer to the.
f2bf0 20 2a 2a 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 ** finder-funct
f2c00 69 6f 6e 2e 20 20 54 68 65 20 66 69 6e 64 65 72 ion. The finder
f2c10 2d 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e -function return
f2c20 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 s a pointer to t
f2c30 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 69 he. ** sqlite_i
f2c40 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 o_methods object
f2c50 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 that implements
f2c60 20 74 68 65 20 64 65 73 69 72 65 64 20 6c 6f 63 the desired loc
f2c70 6b 69 6e 67 0a 20 20 2a 2a 20 62 65 68 61 76 69 king. ** behavi
f2c80 6f 72 73 2e 20 20 53 65 65 20 74 68 65 20 64 69 ors. See the di
f2c90 76 69 73 69 6f 6e 20 61 62 6f 76 65 20 74 68 61 vision above tha
f2ca0 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 t contains the I
f2cb0 4f 4d 45 54 48 4f 44 53 0a 20 20 2a 2a 20 6d 61 OMETHODS. ** ma
f2cc0 63 72 6f 20 66 6f 72 20 61 64 64 69 74 69 6f 6e cro for addition
f2cd0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 information on
f2ce0 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 73 finder-functions
f2cf0 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 73 74 .. **. ** Most
f2d00 20 66 69 6e 64 65 72 73 20 73 69 6d 70 6c 79 20 finders simply
f2d10 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 return a pointer
f2d20 20 74 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69 to a fixed sqli
f2d30 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 20 te3_io_methods.
f2d40 20 2a 2a 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 ** object. But
f2d50 20 74 68 65 20 22 61 75 74 6f 6c 6f 63 6b 49 6f the "autolockIo
f2d60 46 69 6e 64 65 72 22 20 61 76 61 69 6c 61 62 6c Finder" availabl
f2d70 65 20 6f 6e 20 4d 61 63 4f 53 58 20 64 6f 65 73 e on MacOSX does
f2d80 20 61 20 6c 69 74 74 6c 65 0a 20 20 2a 2a 20 6d a little. ** m
f2d90 6f 72 65 20 74 68 61 6e 20 74 68 61 74 3b 20 69 ore than that; i
f2da0 74 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 t looks at the f
f2db0 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20 74 ilesystem type t
f2dc0 68 61 74 20 68 6f 73 74 73 20 74 68 65 20 0a 20 hat hosts the .
f2dd0 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c ** database fil
f2de0 65 20 61 6e 64 20 74 72 69 65 73 20 74 6f 20 63 e and tries to c
f2df0 68 6f 6f 73 65 20 61 6e 20 6c 6f 63 6b 69 6e 67 hoose an locking
f2e00 20 6d 65 74 68 6f 64 20 61 70 70 72 6f 70 72 69 method appropri
f2e10 61 74 65 20 66 6f 72 0a 20 20 2a 2a 20 74 68 61 ate for. ** tha
f2e20 74 20 66 69 6c 65 73 79 73 74 65 6d 20 74 69 6d t filesystem tim
f2e30 65 2e 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e e.. */. #defin
f2e40 65 20 55 4e 49 58 56 46 53 28 56 46 53 4e 41 4d e UNIXVFS(VFSNAM
f2e50 45 2c 20 46 49 4e 44 45 52 29 20 7b 20 20 20 20 E, FINDER) {
f2e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2e70 20 20 20 20 5c 0a 20 20 20 20 31 2c 20 20 20 20 \. 1,
f2e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2e90 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20 /* iVersion */
f2ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2eb0 20 20 5c 0a 20 20 20 20 73 69 7a 65 6f 66 28 75 \. sizeof(u
f2ec0 6e 69 78 46 69 6c 65 29 2c 20 20 20 20 20 2f 2a nixFile), /*
f2ed0 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 20 20 20 20 szOsFile */
f2ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2ef0 5c 0a 20 20 20 20 4d 41 58 5f 50 41 54 48 4e 41 \. MAX_PATHNA
f2f00 4d 45 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 6d ME, /* m
f2f10 78 50 61 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20 xPathname */
f2f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
f2f30 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 0,
f2f40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 /* pNe
f2f50 78 74 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 xt */
f2f60 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
f2f70 20 20 56 46 53 4e 41 4d 45 2c 20 20 20 20 20 20 VFSNAME,
f2f80 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 /* zName
f2f90 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f2fa0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
f2fb0 28 76 6f 69 64 2a 29 26 46 49 4e 44 45 52 2c 20 (void*)&FINDER,
f2fc0 20 20 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 /* pAppDat
f2fd0 61 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 a */
f2fe0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e \. un
f2ff0 69 78 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 ixOpen,
f3000 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 20 /* xOpen */
f3010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3020 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 \. unix
f3030 44 65 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 Delete,
f3040 20 20 2f 2a 20 78 44 65 6c 65 74 65 20 2a 2f 20 /* xDelete */
f3050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3060 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 41 63 \. unixAc
f3070 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 20 20 cess,
f3080 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 20 20 20 /* xAccess */
f3090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f30a0 20 20 5c 0a 20 20 20 20 75 6e 69 78 46 75 6c 6c \. unixFull
f30b0 50 61 74 68 6e 61 6d 65 2c 20 20 20 20 20 2f 2a Pathname, /*
f30c0 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a xFullPathname *
f30d0 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
f30e0 5c 0a 20 20 20 20 75 6e 69 78 44 6c 4f 70 65 6e \. unixDlOpen
f30f0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 , /* x
f3100 44 6c 4f 70 65 6e 20 2a 2f 20 20 20 20 20 20 20 DlOpen */
f3110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
f3120 20 20 20 20 75 6e 69 78 44 6c 45 72 72 6f 72 2c unixDlError,
f3130 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c /* xDl
f3140 45 72 72 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 Error */
f3150 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
f3160 20 20 75 6e 69 78 44 6c 53 79 6d 2c 20 20 20 20 unixDlSym,
f3170 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 /* xDlSy
f3180 6d 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 m */
f3190 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
f31a0 75 6e 69 78 44 6c 43 6c 6f 73 65 2c 20 20 20 20 unixDlClose,
f31b0 20 20 20 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 /* xDlClos
f31c0 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 e */
f31d0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e \. un
f31e0 69 78 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 ixRandomness,
f31f0 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 /* xRandomne
f3200 73 73 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 ss */
f3210 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 \. unix
f3220 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 Sleep,
f3230 20 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 20 20 /* xSleep */
f3240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3250 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 43 75 \. unixCu
f3260 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 20 20 20 rrentTime,
f3270 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20 /* xCurrentTime
f3280 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f3290 20 20 5c 0a 20 20 20 20 75 6e 69 78 47 65 74 4c \. unixGetL
f32a0 61 73 74 45 72 72 6f 72 20 20 20 20 20 20 2f 2a astError /*
f32b0 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a xGetLastError *
f32c0 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
f32d0 5c 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a \. }.. /*. **
f32e0 20 41 6c 6c 20 64 65 66 61 75 6c 74 20 56 46 53 All default VFS
f32f0 65 73 20 66 6f 72 20 75 6e 69 78 20 61 72 65 20 es for unix are
f3300 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 contained in the
f3310 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 following array
f3320 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 .. **. ** Note
f3330 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 that the sqlite
f3340 33 5f 76 66 73 2e 70 4e 65 78 74 20 66 69 65 6c 3_vfs.pNext fiel
f3350 64 20 6f 66 20 74 68 65 20 56 46 53 20 6f 62 6a d of the VFS obj
f3360 65 63 74 20 69 73 20 6d 6f 64 69 66 69 65 64 0a ect is modified.
f3370 20 20 2a 2a 20 62 79 20 74 68 65 20 53 51 4c 69 ** by the SQLi
f3380 74 65 20 63 6f 72 65 20 77 68 65 6e 20 74 68 65 te core when the
f3390 20 56 46 53 20 69 73 20 72 65 67 69 73 74 65 72 VFS is register
f33a0 65 64 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c ed. So the foll
f33b0 6f 77 69 6e 67 0a 20 20 2a 2a 20 61 72 72 61 79 owing. ** array
f33c0 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 cannot be const
f33d0 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 .. */. static
f33e0 73 71 6c 69 74 65 33 5f 76 66 73 20 61 56 66 73 sqlite3_vfs aVfs
f33f0 5b 5d 20 3d 20 7b 0a 23 69 66 20 53 51 4c 49 54 [] = {.#if SQLIT
f3400 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
f3410 5f 53 54 59 4c 45 20 26 26 20 28 4f 53 5f 56 58 _STYLE && (OS_VX
f3420 57 4f 52 4b 53 20 7c 7c 20 64 65 66 69 6e 65 64 WORKS || defined
f3430 28 5f 5f 41 50 50 4c 45 5f 5f 29 29 0a 20 20 20 (__APPLE__)).
f3440 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 22 2c UNIXVFS("unix",
f3450 20 20 20 20 20 20 20 20 20 20 61 75 74 6f 6c 6f autolo
f3460 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 ckIoFinder ),.#e
f3470 6c 73 65 0a 20 20 20 20 55 4e 49 58 56 46 53 28 lse. UNIXVFS(
f3480 22 75 6e 69 78 22 2c 20 20 20 20 20 20 20 20 20 "unix",
f3490 20 70 6f 73 69 78 49 6f 46 69 6e 64 65 72 20 29 posixIoFinder )
f34a0 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 55 4e 49 ,.#endif. UNI
f34b0 58 56 46 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22 XVFS("unix-none"
f34c0 2c 20 20 20 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 , nolockIoFi
f34d0 6e 64 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 nder ),. UNIX
f34e0 56 46 53 28 22 75 6e 69 78 2d 64 6f 74 66 69 6c VFS("unix-dotfil
f34f0 65 22 2c 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 e", dotlockIoFi
f3500 6e 64 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 nder ),. UNIX
f3510 56 46 53 28 22 75 6e 69 78 2d 77 66 6c 22 2c 20 VFS("unix-wfl",
f3520 20 20 20 20 20 70 6f 73 69 78 57 66 6c 49 6f 46 posixWflIoF
f3530 69 6e 64 65 72 20 29 2c 0a 23 69 66 20 4f 53 5f inder ),.#if OS_
f3540 56 58 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 VXWORKS. UNIX
f3550 56 46 53 28 22 75 6e 69 78 2d 6e 61 6d 65 64 73 VFS("unix-nameds
f3560 65 6d 22 2c 20 73 65 6d 49 6f 46 69 6e 64 65 72 em", semIoFinder
f3570 20 29 2c 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 ),.#endif.#if S
f3580 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
f3590 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 20 20 55 KING_STYLE. U
f35a0 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 70 6f 73 NIXVFS("unix-pos
f35b0 69 78 22 2c 20 20 20 20 70 6f 73 69 78 49 6f 46 ix", posixIoF
f35c0 69 6e 64 65 72 20 29 2c 0a 23 69 66 20 21 4f 53 inder ),.#if !OS
f35d0 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 _VXWORKS. UNI
f35e0 58 56 46 53 28 22 75 6e 69 78 2d 66 6c 6f 63 6b XVFS("unix-flock
f35f0 22 2c 20 20 20 20 66 6c 6f 63 6b 49 6f 46 69 6e ", flockIoFin
f3600 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a 23 65 der ),.#endif.#e
f3610 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f ndif.#if SQLITE_
f3620 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
f3630 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 TYLE && defined(
f3640 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20 20 55 __APPLE__). U
f3650 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 61 66 70 NIXVFS("unix-afp
f3660 22 2c 20 20 20 20 20 20 61 66 70 49 6f 46 69 6e ", afpIoFin
f3670 64 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 der ),. UNIXV
f3680 46 53 28 22 75 6e 69 78 2d 70 72 6f 78 79 22 2c FS("unix-proxy",
f3690 20 20 20 20 70 72 6f 78 79 49 6f 46 69 6e 64 65 proxyIoFinde
f36a0 72 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b r ),.#endif. };
f36b0 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
f36c0 69 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c i; /* L
f36d0 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a oop counter */..
f36e0 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c /* Register al
f36f0 6c 20 56 46 53 65 73 20 64 65 66 69 6e 65 64 20 l VFSes defined
f3700 69 6e 20 74 68 65 20 61 56 66 73 5b 5d 20 61 72 in the aVfs[] ar
f3710 72 61 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 ray */. for(i=0
f3720 3b 20 69 3c 28 73 69 7a 65 6f 66 28 61 56 66 73 ; i<(sizeof(aVfs
f3730 29 2f 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 )/sizeof(sqlite3
f3740 5f 76 66 73 29 29 3b 20 69 2b 2b 29 7b 0a 20 20 _vfs)); i++){.
f3750 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 sqlite3_vfs_re
f3760 67 69 73 74 65 72 28 26 61 56 66 73 5b 69 5d 2c gister(&aVfs[i],
f3770 20 69 3d 3d 30 29 3b 0a 20 20 7d 0a 20 20 72 65 i==0);. }. re
f3780 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
f3790 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f .}../*.** Shutdo
f37a0 77 6e 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 wn the operating
f37b0 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 system interfac
f37c0 65 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 70 e..**.** Some op
f37d0 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20 erating systems
f37e0 6d 69 67 68 74 20 6e 65 65 64 20 74 6f 20 64 6f might need to do
f37f0 20 73 6f 6d 65 20 63 6c 65 61 6e 75 70 20 69 6e some cleanup in
f3800 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 0a 2a this routine,.*
f3810 2a 20 74 6f 20 72 65 6c 65 61 73 65 20 64 79 6e * to release dyn
f3820 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 amically allocat
f3830 65 64 20 6f 62 6a 65 63 74 73 2e 20 20 42 75 74 ed objects. But
f3840 20 6e 6f 74 20 6f 6e 20 75 6e 69 78 2e 0a 2a 2a not on unix..**
f3850 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
f3860 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 75 6e 69 a no-op for uni
f3870 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 x..*/.SQLITE_API
f3880 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f int sqlite3_os_
f3890 65 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 end(void){ . re
f38a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
f38b0 0a 7d 0a 20 0a 23 65 6e 64 69 66 20 2f 2a 20 53 .}. .#endif /* S
f38c0 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 2a 2f QLITE_OS_UNIX */
f38d0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
f38e0 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 75 6e 69 78 * End of os_unix
f38f0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
f3900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3920 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
f3930 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f * Begin file os_
f3940 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a win.c **********
f3950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3970 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 /./*.** 2004 May
f3980 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 22.**.** The au
f3990 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
f39a0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
f39b0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
f39c0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
f39d0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
f39e0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
f39f0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
f3a00 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
f3a10 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
f3a20 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
f3a30 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
f3a40 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
f3a50 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
f3a60 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
f3a70 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
f3a80 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
f3a90 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
f3aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3ae0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
f3af0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
f3b00 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 73 70 code that is sp
f3b10 65 63 69 66 69 63 20 74 6f 20 77 69 6e 64 6f 77 ecific to window
f3b20 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 s..*/.#if SQLITE
f3b30 5f 4f 53 5f 57 49 4e 20 20 20 20 20 20 20 20 20 _OS_WIN
f3b40 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 /* This fi
f3b50 6c 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 77 le is used for w
f3b60 69 6e 64 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a indows only */..
f3b70 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 ./*.** A Note Ab
f3b80 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 out Memory Alloc
f3b90 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 ation:.**.** Thi
f3ba0 73 20 64 72 69 76 65 72 20 75 73 65 73 20 6d 61 s driver uses ma
f3bb0 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 20 64 69 lloc()/free() di
f3bc0 72 65 63 74 6c 79 20 72 61 74 68 65 72 20 74 68 rectly rather th
f3bd0 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 an going through
f3be0 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 2d 77 .** the SQLite-w
f3bf0 72 61 70 70 65 72 73 20 73 71 6c 69 74 65 33 5f rappers sqlite3_
f3c00 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 malloc()/sqlite3
f3c10 5f 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 _free(). Those
f3c20 77 72 61 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 wrappers.** are
f3c30 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75 73 65 designed for use
f3c40 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 on embedded sys
f3c50 74 65 6d 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 tems where memor
f3c60 79 20 69 73 20 73 63 61 72 63 65 20 61 6e 64 0a y is scarce and.
f3c70 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 ** malloc failur
f3c80 65 73 20 68 61 70 70 65 6e 20 66 72 65 71 75 65 es happen freque
f3c90 6e 74 6c 79 2e 20 20 57 69 6e 33 32 20 64 6f 65 ntly. Win32 doe
f3ca0 73 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 s not typically
f3cb0 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 run on.** embedd
f3cc0 65 64 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 ed systems, and
f3cd0 77 68 65 6e 20 69 74 20 64 6f 65 73 20 74 68 65 when it does the
f3ce0 20 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d developers norm
f3cf0 61 6c 6c 79 20 68 61 76 65 20 62 69 67 67 65 72 ally have bigger
f3d00 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 .** problems to
f3d10 77 6f 72 72 79 20 61 62 6f 75 74 20 74 68 61 6e worry about than
f3d20 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 running out of
f3d30 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 memory. So ther
f3d40 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f e is not.** a co
f3d50 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f mpelling need to
f3d60 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65 72 use the wrapper
f3d70 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 s..**.** But the
f3d80 72 65 20 69 73 20 61 20 67 6f 6f 64 20 72 65 61 re is a good rea
f3d90 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 son to not use t
f3da0 68 65 20 77 72 61 70 70 65 72 73 2e 20 20 49 66 he wrappers. If
f3db0 20 77 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 we use the.** w
f3dc0 72 61 70 70 65 72 73 20 74 68 65 6e 20 77 65 20 rappers then we
f3dd0 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 will get simulat
f3de0 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c ed malloc() fail
f3df0 75 72 65 73 20 77 69 74 68 69 6e 20 74 68 69 73 ures within this
f3e00 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 41 6e 64 .** driver. And
f3e10 20 74 68 61 74 20 63 61 75 73 65 73 20 61 6c 6c that causes all
f3e20 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 kinds of proble
f3e30 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 73 74 73 ms for our tests
f3e40 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 . We.** could e
f3e50 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 20 74 6f nhance SQLite to
f3e60 20 64 65 61 6c 20 77 69 74 68 20 73 69 6d 75 6c deal with simul
f3e70 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c ated malloc fail
f3e80 75 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 ures within.** t
f3e90 68 65 20 4f 53 20 64 72 69 76 65 72 2c 20 62 75 he OS driver, bu
f3ea0 74 20 74 68 65 20 63 6f 64 65 20 74 6f 20 64 65 t the code to de
f3eb0 61 6c 20 77 69 74 68 20 74 68 6f 73 65 20 66 61 al with those fa
f3ec0 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a ilure would not.
f3ed0 2a 2a 20 62 65 20 65 78 65 72 63 69 73 65 64 20 ** be exercised
f3ee0 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 63 68 20 on Linux (which
f3ef0 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f does not need to
f3f00 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 malloc() in the
f3f10 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 driver).** and
f3f20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65 so we would have
f3f30 20 64 69 66 66 69 63 75 6c 74 79 20 77 72 69 74 difficulty writ
f3f40 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 ing coverage tes
f3f50 74 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 ts for that.** c
f3f60 6f 64 65 2e 20 20 42 65 74 74 65 72 20 74 6f 20 ode. Better to
f3f70 6c 65 61 76 65 20 74 68 65 20 63 6f 64 65 20 6f leave the code o
f3f80 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a ut, we think..**
f3f90 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 .** The point of
f3fa0 20 74 68 69 73 20 64 69 73 63 75 73 73 69 6f 6e this discussion
f3fb0 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 is as follows:
f3fc0 20 57 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 When creating a
f3fd0 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 new.** OS layer
f3fe0 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 64 for an embedded
f3ff0 20 73 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 system, if you
f4000 75 73 65 20 74 68 69 73 20 66 69 6c 65 20 61 73 use this file as
f4010 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 an example,.**
f4020 61 76 6f 69 64 20 74 68 65 20 75 73 65 20 6f 66 avoid the use of
f4030 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 malloc()/free()
f4040 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 . Those routine
f4050 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 77 69 6e s work ok on win
f4060 64 6f 77 73 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 dows.** desktops
f4070 20 62 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c but not so well
f4080 20 69 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 in embedded sys
f4090 74 65 6d 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 6c 75 tems..*/..#inclu
f40a0 64 65 20 3c 77 69 6e 62 61 73 65 2e 68 3e 0a 0a de <winbase.h>..
f40b0 23 69 66 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f #ifdef __CYGWIN_
f40c0 5f 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 _.# include <sys
f40d0 2f 63 79 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69 /cygwin.h>.#endi
f40e0 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 f../*.** Macros
f40f0 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e used to determin
f4100 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 e whether or not
f4110 20 74 6f 20 75 73 65 20 74 68 72 65 61 64 73 2e to use threads.
f4120 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .*/.#if defined(
f4130 54 48 52 45 41 44 53 41 46 45 29 20 26 26 20 54 THREADSAFE) && T
f4140 48 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 HREADSAFE.# defi
f4150 6e 65 20 53 51 4c 49 54 45 5f 57 33 32 5f 54 48 ne SQLITE_W32_TH
f4160 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a READS 1.#endif..
f4170 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f /*.** Include co
f4180 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f de that is commo
f4190 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 n to all os_*.c
f41a0 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a files.*/./******
f41b0 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 ******** Include
f41c0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 os_common.h in
f41d0 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 the middle of os
f41e0 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a _win.c *********
f41f0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
f4200 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
f4210 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 ile os_common.h
f4220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4240 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
f4250 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 004 May 22.**.**
f4260 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
f4270 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
f4280 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
f4290 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
f42a0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
f42b0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
f42c0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
f42d0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
f42e0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
f42f0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
f4300 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
f4310 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
f4320 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
f4330 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
f4340 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
f4350 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
f4360 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
f4370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f43a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f43b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
f43c0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
f43d0 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 ontains macros a
f43e0 6e 64 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20 nd a little bit
f43f0 6f 66 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 of code that is
f4400 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c common to.** all
f4410 20 6f 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d of the platform
f4420 2d 73 70 65 63 69 66 69 63 20 66 69 6c 65 73 20 -specific files
f4430 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 (os_*.c) and is
f4440 23 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 #included into t
f4450 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a hose.** files..*
f4460 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73 *.** This file s
f4470 68 6f 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 hould be #includ
f4480 65 64 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 ed by the os_*.c
f4490 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 files only. It
f44a0 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e is not a.** gen
f44b0 65 72 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61 eral purpose hea
f44c0 64 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 der file..**.**
f44d0 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 $Id: os_common.h
f44e0 2c 76 20 31 2e 33 38 20 32 30 30 39 2f 30 32 2f ,v 1.38 2009/02/
f44f0 32 34 20 31 38 3a 34 30 3a 35 30 20 64 61 6e 69 24 18:40:50 dani
f4500 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f elk1977 Exp $.*/
f4510 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d .#ifndef _OS_COM
f4520 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f MON_H_.#define _
f4530 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a OS_COMMON_H_../*
f4540 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f .** At least two
f4550 20 62 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 bugs have slipp
f4560 65 64 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 ed in because we
f4570 20 63 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d changed the MEM
f4580 4f 52 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 ORY_DEBUG.** mac
f4590 72 6f 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 ro to SQLITE_DEB
f45a0 55 47 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 UG and some olde
f45b0 72 20 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 r makefiles have
f45c0 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 not yet made th
f45d0 65 0a 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 e.** switch. Th
f45e0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 e following code
f45f0 20 73 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 should catch th
f4600 69 73 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f is problem at co
f4610 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 mpile-time..*/.#
f4620 69 66 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 ifdef MEMORY_DEB
f4630 55 47 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 UG.# error "The
f4640 4d 45 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 MEMORY_DEBUG mac
f4650 72 6f 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 ro is obsolete.
f4660 20 55 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 Use SQLITE_DEBU
f4670 47 20 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 G instead.".#end
f4680 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
f4690 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 E_DEBUG.SQLITE_P
f46a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
f46b0 65 33 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a 23 e3OSTrace = 0;.#
f46c0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 define OSTRACE1(
f46d0 58 29 20 20 20 20 20 20 20 20 20 69 66 28 20 73 X) if( s
f46e0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
f46f0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
f4700 74 66 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 tf(X).#define OS
f4710 54 52 41 43 45 32 28 58 2c 59 29 20 20 20 20 20 TRACE2(X,Y)
f4720 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 if( sqlite3OST
f4730 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 race ) sqlite3De
f4740 62 75 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 bugPrintf(X,Y).#
f4750 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 define OSTRACE3(
f4760 58 2c 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73 X,Y,Z) if( s
f4770 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
f4780 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
f4790 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e tf(X,Y,Z).#defin
f47a0 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a e OSTRACE4(X,Y,Z
f47b0 2c 41 29 20 20 20 69 66 28 20 73 71 6c 69 74 65 ,A) if( sqlite
f47c0 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 3OSTrace ) sqlit
f47d0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
f47e0 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f Y,Z,A).#define O
f47f0 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c STRACE5(X,Y,Z,A,
f4800 42 29 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 B) if( sqlite3OS
f4810 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 Trace ) sqlite3D
f4820 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a ebugPrintf(X,Y,Z
f4830 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,A,B).#define OS
f4840 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 TRACE6(X,Y,Z,A,B
f4850 2c 43 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c ,C) \. if(sql
f4860 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c ite3OSTrace) sql
f4870 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
f4880 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 X,Y,Z,A,B,C).#de
f4890 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c fine OSTRACE7(X,
f48a0 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 Y,Z,A,B,C,D) \.
f48b0 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 if(sqlite3OST
f48c0 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 race) sqlite3Deb
f48d0 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 ugPrintf(X,Y,Z,A
f48e0 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 ,B,C,D).#else.#d
f48f0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 efine OSTRACE1(X
f4900 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
f4910 45 32 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 E2(X,Y).#define
f4920 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a OSTRACE3(X,Y,Z).
f4930 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 #define OSTRACE4
f4940 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e (X,Y,Z,A).#defin
f4950 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a e OSTRACE5(X,Y,Z
f4960 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,A,B).#define OS
f4970 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 TRACE6(X,Y,Z,A,B
f4980 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,C).#define OSTR
f4990 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 ACE7(X,Y,Z,A,B,C
f49a0 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ,D).#endif../*.*
f49b0 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72 * Macros for per
f49c0 66 6f 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 formance tracing
f49d0 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e . Normally turn
f49e0 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f ed off. Only wo
f49f0 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 rks.** on i486 h
f4a00 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 ardware..*/.#ifd
f4a10 65 66 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 ef SQLITE_PERFOR
f4a20 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 MANCE_TRACE../*
f4a30 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e .** hwtime.h con
f4a40 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 tains inline ass
f4a50 65 6d 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 embler code for
f4a60 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a implementing .**
f4a70 20 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 high-performanc
f4a80 65 20 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 e timing routine
f4a90 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a s..*/./*********
f4aa0 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 ***** Include hw
f4ab0 74 69 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 time.h in the mi
f4ac0 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f ddle of os_commo
f4ad0 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.h ************
f4ae0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
f4af0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
f4b00 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a hwtime.h ******
f4b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4b30 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 ****/./*.** 2008
f4b40 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 May 27.**.** Th
f4b50 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
f4b60 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
f4b70 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
f4b80 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
f4b90 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
f4ba0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
f4bb0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
f4bc0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
f4bd0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
f4be0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
f4bf0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
f4c00 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
f4c10 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
f4c20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
f4c30 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
f4c40 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
f4c50 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
f4c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
f4cb0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
f4cc0 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 ains inline asm
f4cd0 63 6f 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 code for retriev
f4ce0 69 6e 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 ing "high-perfor
f4cf0 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 mance".** counte
f4d00 72 73 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 rs for x86 class
f4d10 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 CPUs..**.** $Id
f4d20 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 : hwtime.h,v 1.3
f4d30 20 32 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 2008/08/01 14:3
f4d40 33 3a 31 35 20 73 68 61 6e 65 20 45 78 70 20 24 3:15 shane Exp $
f4d50 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 .*/.#ifndef _HWT
f4d60 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f IME_H_.#define _
f4d70 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a HWTIME_H_../*.**
f4d80 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 The following r
f4d90 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b outine only work
f4da0 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 s on pentium-cla
f4db0 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 ss (or newer) pr
f4dc0 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 ocessors..** It
f4dd0 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20 6f uses the RDTSC o
f4de0 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 pcode to read th
f4df0 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 e cycle count va
f4e00 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a lue out of the.*
f4e10 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 * processor and
f4e20 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c returns that val
f4e30 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 ue. This can be
f4e40 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 used for high-r
f4e50 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e es.** profiling.
f4e60 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 .*/.#if (defined
f4e70 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 (__GNUC__) || de
f4e80 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 fined(_MSC_VER))
f4e90 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 && \. (def
f4ea0 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 ined(i386) || de
f4eb0 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 fined(__i386__)
f4ec0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 || defined(_M_IX
f4ed0 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 86)).. #if defi
f4ee0 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 ned(__GNUC__)..
f4ef0 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 __inline__ sqli
f4f00 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 te_uint64 sqlite
f4f10 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 3Hwtime(void){.
f4f20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 unsigned int
f4f30 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f lo, hi;. __
f4f40 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 asm__ __volatile
f4f50 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d __ ("rdtsc" : "=
f4f60 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 a" (lo), "=d" (h
f4f70 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e i));. return
f4f80 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 (sqlite_uint64)
f4f90 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 hi << 32 | lo;.
f4fa0 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 }.. #elif defi
f4fb0 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 ned(_MSC_VER)..
f4fc0 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 __declspec(nake
f4fd0 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 d) __inline sqli
f4fe0 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 te_uint64 __cdec
f4ff0 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 l sqlite3Hwtime(
f5000 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 void){. __as
f5010 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 m {. rdts
f5020 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20 c. ret
f5030 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c ; return val
f5040 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 ue at EDX:EAX.
f5050 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 }. }.. #end
f5060 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e if..#elif (defin
f5070 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 ed(__GNUC__) &&
f5080 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 defined(__x86_64
f5090 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 __)).. __inline
f50a0 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 __ sqlite_uint64
f50b0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 sqlite3Hwtime(v
f50c0 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 oid){. unsi
f50d0 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 gned long val;.
f50e0 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 __asm__ __v
f50f0 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 olatile__ ("rdts
f5100 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 c" : "=A" (val))
f5110 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 ;. return v
f5120 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 al;. }. .#elif
f5130 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f (defined(__GNUC_
f5140 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f _) && defined(__
f5150 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c ppc__)).. __inl
f5160 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e ine__ sqlite_uin
f5170 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d t64 sqlite3Hwtim
f5180 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 e(void){. u
f5190 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
f51a0 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 g retval;.
f51b0 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 unsigned long ju
f51c0 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f nk;. __asm_
f51d0 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 _ __volatile__ (
f51e0 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 "\n\. 1
f51f0 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 : mftbu %
f5200 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 1\n\.
f5210 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 mftb %
f5220 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 L0\n\.
f5230 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 mftbu
f5240 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 %0\n\.
f5250 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 cmpw
f5260 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 %0,%1\n\.
f5270 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 bne
f5280 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 1b".
f5290 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 : "=r"
f52a0 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 (retval), "=r" (
f52b0 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 junk));. re
f52c0 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d turn retval;. }
f52d0 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f ..#else.. #erro
f52e0 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 r Need implement
f52f0 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 ation of sqlite3
f5300 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 Hwtime() for you
f5310 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f r platform... /
f5320 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c *. ** To compil
f5330 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d e without implem
f5340 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 enting sqlite3Hw
f5350 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 time() for your
f5360 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 platform,. ** y
f5370 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 ou can remove th
f5380 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 e above #error a
f5390 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f nd use the follo
f53a0 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 wing. ** stub f
f53b0 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 unction. You wi
f53c0 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 ll lose timing s
f53d0 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a upport for many.
f53e0 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 ** of the debu
f53f0 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e gging and testin
f5400 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 g utilities, but
f5410 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 it should at.
f5420 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 ** least compile
f5430 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 and run.. */.S
f5440 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
f5450 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 sqlite_uint64 sq
f5460 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
f5470 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 ){ return ((sqli
f5480 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a te_uint64)0); }.
f5490 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 .#endif..#endif
f54a0 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 /* !defined(_HWT
f54b0 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a IME_H_) */../***
f54c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
f54d0 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a of hwtime.h ****
f54e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f54f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
f5510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
f5520 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
f5530 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 left off in os_c
f5540 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a ommon.h ********
f5550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 **********/..sta
f5560 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 tic sqlite_uint6
f5570 34 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 4 g_start;.stati
f5580 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 c sqlite_uint64
f5590 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69 g_elapsed;.#defi
f55a0 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20 ne TIMER_START
f55b0 20 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c g_start=sql
f55c0 69 74 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65 ite3Hwtime().#de
f55d0 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 fine TIMER_END
f55e0 20 20 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 g_elapsed
f55f0 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 =sqlite3Hwtime()
f5600 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65 -g_start.#define
f5610 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 TIMER_ELAPSED
f5620 20 20 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c g_elapsed.#el
f5630 73 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 se.#define TIMER
f5640 5f 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54 _START.#define T
f5650 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 IMER_END.#define
f5660 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 TIMER_ELAPSED
f5670 20 20 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 ((sqlite_uint
f5680 36 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 64)0).#endif../*
f5690 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c .** If we compil
f56a0 65 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 e with the SQLIT
f56b0 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74 E_TEST macro set
f56c0 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f , then the follo
f56d0 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 wing block.** of
f56e0 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20 code will give
f56f0 75 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 us the ability t
f5700 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73 o simulate a dis
f5710 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 k I/O error. Th
f5720 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f is.** is used fo
f5730 72 20 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f r testing the I/
f5740 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 O recovery logic
f5750 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
f5760 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 TE_TEST.SQLITE_A
f5770 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 PI int sqlite3_i
f5780 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b o_error_hit = 0;
f5790 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
f57a0 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 otal number of I
f57b0 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c /O Errors */.SQL
f57c0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
f57d0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 te3_io_error_har
f57e0 64 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 dhit = 0;
f57f0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f /* Number of no
f5800 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 n-benign errors
f5810 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
f5820 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 t sqlite3_io_err
f5830 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 or_pending = 0;
f5840 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 /* Count
f5850 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f down to first I/
f5860 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 O error */.SQLIT
f5870 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
f5880 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 3_io_error_persi
f5890 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f st = 0; /
f58a0 2a 20 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72 * True if I/O er
f58b0 72 6f 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a rors persist */.
f58c0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
f58d0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
f58e0 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20 benign = 0;
f58f0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65 /* True if e
f5900 72 72 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e rrors are benign
f5910 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
f5920 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 nt sqlite3_diskf
f5930 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b ull_pending = 0;
f5940 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
f5950 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c sqlite3_diskfull
f5960 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69 = 0;.#define Si
f5970 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e mulateIOErrorBen
f5980 69 67 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69 ign(X) sqlite3_i
f5990 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 o_error_benign=(
f59a0 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c X).#define Simul
f59b0 61 74 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29 ateIOError(CODE)
f59c0 20 20 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74 \. if( (sqlit
f59d0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 e3_io_error_pers
f59e0 69 73 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69 ist && sqlite3_i
f59f0 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20 o_error_hit) \.
f5a00 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 || sqlite3
f5a10 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e _io_error_pendin
f5a20 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20 g-- == 1 ) \.
f5a30 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f { lo
f5a40 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44 cal_ioerr(); COD
f5a50 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 E; }.static void
f5a60 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a local_ioerr(){.
f5a70 20 20 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52 IOTRACE(("IOER
f5a80 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 R\n"));. sqlite
f5a90 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3_io_error_hit++
f5aa0 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 ;. if( !sqlite3
f5ab0 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e _io_error_benign
f5ac0 20 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 ) sqlite3_io_er
f5ad0 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d ror_hardhit++;.}
f5ae0 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 .#define Simulat
f5af0 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 eDiskfullError(C
f5b00 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 ODE) \. if( sq
f5b10 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 lite3_diskfull_p
f5b20 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 ending ){ \.
f5b30 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 if( sqlite3_dis
f5b40 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d kfull_pending ==
f5b50 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 1 ){ \. l
f5b60 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a ocal_ioerr(); \.
f5b70 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 sqlite3_d
f5b80 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 iskfull = 1; \.
f5b90 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f sqlite3_io
f5ba0 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 _error_hit = 1;
f5bb0 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c \. CODE; \
f5bc0 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 . }else{ \.
f5bd0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 sqlite3_di
f5be0 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d skfull_pending--
f5bf0 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 ; \. } \.
f5c00 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 }.#else.#define
f5c10 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 SimulateIOErrorB
f5c20 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65 enign(X).#define
f5c30 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
f5c40 28 41 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 (A).#define Simu
f5c50 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f lateDiskfullErro
f5c60 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a r(A).#endif../*.
f5c70 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c ** When testing,
f5c80 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 keep a count of
f5c90 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f the number of o
f5ca0 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 pen files..*/.#i
f5cb0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
f5cc0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
f5cd0 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c sqlite3_open_fil
f5ce0 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 e_count = 0;.#de
f5cf0 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 fine OpenCounter
f5d00 28 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 (X) sqlite3_ope
f5d10 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 n_file_count+=(X
f5d20 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 ).#else.#define
f5d30 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 OpenCounter(X).#
f5d40 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a endif..#endif /*
f5d50 20 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f !defined(_OS_CO
f5d60 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a MMON_H_) */../**
f5d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
f5d80 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 of os_common.h
f5d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
f5dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e ************ Con
f5dd0 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 tinuing where we
f5de0 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f left off in os_
f5df0 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a win.c **********
f5e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
f5e10 0a 2a 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f .** Some microso
f5e20 66 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 ft compilers lac
f5e30 6b 20 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f k this definitio
f5e40 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e n..*/.#ifndef IN
f5e50 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 VALID_FILE_ATTRI
f5e60 42 55 54 45 53 0a 23 20 64 65 66 69 6e 65 20 49 BUTES.# define I
f5e70 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 NVALID_FILE_ATTR
f5e80 49 42 55 54 45 53 20 28 28 44 57 4f 52 44 29 2d IBUTES ((DWORD)-
f5e90 31 29 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 1) .#endif../*.*
f5ea0 2a 20 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 * Determine if w
f5eb0 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 e are dealing wi
f5ec0 74 68 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 th WindowsCE - w
f5ed0 68 69 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a hich has a much.
f5ee0 2a 2a 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a ** reduced API..
f5ef0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
f5f00 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 _WINCE.# define
f5f10 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 AreFileApisANSI(
f5f20 29 20 31 0a 23 20 64 65 66 69 6e 65 20 46 6f 72 ) 1.# define For
f5f30 6d 61 74 4d 65 73 73 61 67 65 57 28 61 2c 62 2c matMessageW(a,b,
f5f40 63 2c 64 2c 65 2c 66 2c 67 29 20 30 0a 23 65 6e c,d,e,f,g) 0.#en
f5f50 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 69 6e 43 45 dif../*.** WinCE
f5f60 20 6c 61 63 6b 73 20 6e 61 74 69 76 65 20 73 75 lacks native su
f5f70 70 70 6f 72 74 20 66 6f 72 20 66 69 6c 65 20 6c pport for file l
f5f80 6f 63 6b 69 6e 67 20 73 6f 20 77 65 20 68 61 76 ocking so we hav
f5f90 65 20 74 6f 20 66 61 6b 65 20 69 74 0a 2a 2a 20 e to fake it.**
f5fa0 77 69 74 68 20 73 6f 6d 65 20 63 6f 64 65 20 6f with some code o
f5fb0 66 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 f our own..*/.#i
f5fc0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
f5fd0 45 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 E.typedef struct
f5fe0 20 77 69 6e 63 65 4c 6f 63 6b 20 7b 0a 20 20 69 winceLock {. i
f5ff0 6e 74 20 6e 52 65 61 64 65 72 73 3b 20 20 20 20 nt nReaders;
f6000 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
f6010 72 65 61 64 65 72 20 6c 6f 63 6b 73 20 6f 62 74 reader locks obt
f6020 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 ained */. BOOL
f6030 62 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 2f bPending; /
f6040 2a 20 49 6e 64 69 63 61 74 65 73 20 61 20 70 65 * Indicates a pe
f6050 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 nding lock has b
f6060 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a een obtained */.
f6070 20 20 42 4f 4f 4c 20 62 52 65 73 65 72 76 65 64 BOOL bReserved
f6080 3b 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 ; /* Indicat
f6090 65 73 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f es a reserved lo
f60a0 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 ck has been obta
f60b0 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 ined */. BOOL b
f60c0 45 78 63 6c 75 73 69 76 65 3b 20 20 20 20 2f 2a Exclusive; /*
f60d0 20 49 6e 64 69 63 61 74 65 73 20 61 6e 20 65 78 Indicates an ex
f60e0 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 68 61 73 clusive lock has
f60f0 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a been obtained *
f6100 2f 0a 7d 20 77 69 6e 63 65 4c 6f 63 6b 3b 0a 23 /.} winceLock;.#
f6110 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
f6120 20 77 69 6e 46 69 6c 65 20 73 74 72 75 63 74 75 winFile structu
f6130 72 65 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 re is a subclass
f6140 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 of sqlite3_file
f6150 2a 20 73 70 65 63 69 66 69 63 20 74 6f 20 74 68 * specific to th
f6160 65 20 77 69 6e 33 32 0a 2a 2a 20 70 6f 72 74 61 e win32.** porta
f6170 62 69 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f bility layer..*/
f6180 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
f6190 77 69 6e 46 69 6c 65 20 77 69 6e 46 69 6c 65 3b winFile winFile;
f61a0 0a 73 74 72 75 63 74 20 77 69 6e 46 69 6c 65 20 .struct winFile
f61b0 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 {. const sqlite
f61c0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 3_io_methods *pM
f61d0 65 74 68 6f 64 3b 2f 2a 20 4d 75 73 74 20 62 65 ethod;/* Must be
f61e0 20 66 69 72 73 74 20 2a 2f 0a 20 20 48 41 4e 44 first */. HAND
f61f0 4c 45 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 LE h;
f6200 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f /* Handle fo
f6210 72 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 r accessing the
f6220 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e file */. unsign
f6230 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 ed char locktype
f6240 3b 20 2f 2a 20 54 79 70 65 20 6f 66 20 6c 6f 63 ; /* Type of loc
f6250 6b 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 k currently held
f6260 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f on this file */
f6270 0a 20 20 73 68 6f 72 74 20 73 68 61 72 65 64 4c . short sharedL
f6280 6f 63 6b 42 79 74 65 3b 20 20 20 2f 2a 20 52 61 ockByte; /* Ra
f6290 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 79 ndomly chosen by
f62a0 74 65 20 75 73 65 64 20 61 73 20 61 20 73 68 61 te used as a sha
f62b0 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 44 57 red lock */. DW
f62c0 4f 52 44 20 6c 61 73 74 45 72 72 6e 6f 3b 20 20 ORD lastErrno;
f62d0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 69 6e /* The Win
f62e0 64 6f 77 73 20 65 72 72 6e 6f 20 66 72 6f 6d 20 dows errno from
f62f0 74 68 65 20 6c 61 73 74 20 49 2f 4f 20 65 72 72 the last I/O err
f6300 6f 72 20 2a 2f 0a 20 20 44 57 4f 52 44 20 73 65 or */. DWORD se
f6310 63 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20 ctorSize;
f6320 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 6f /* Sector size o
f6330 66 20 74 68 65 20 64 65 76 69 63 65 20 66 69 6c f the device fil
f6340 65 20 69 73 20 6f 6e 20 2a 2f 0a 23 69 66 20 53 e is on */.#if S
f6350 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 QLITE_OS_WINCE.
f6360 20 57 43 48 41 52 20 2a 7a 44 65 6c 65 74 65 4f WCHAR *zDeleteO
f6370 6e 43 6c 6f 73 65 3b 20 20 2f 2a 20 4e 61 6d 65 nClose; /* Name
f6380 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 of file to dele
f6390 74 65 20 77 68 65 6e 20 63 6c 6f 73 69 6e 67 20 te when closing
f63a0 2a 2f 0a 20 20 48 41 4e 44 4c 45 20 68 4d 75 74 */. HANDLE hMut
f63b0 65 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ex; /*
f63c0 4d 75 74 65 78 20 75 73 65 64 20 74 6f 20 63 6f Mutex used to co
f63d0 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 ntrol access to
f63e0 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 20 20 shared lock */
f63f0 0a 20 20 48 41 4e 44 4c 45 20 68 53 68 61 72 65 . HANDLE hShare
f6400 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 d; /* Sh
f6410 61 72 65 64 20 6d 65 6d 6f 72 79 20 73 65 67 6d ared memory segm
f6420 65 6e 74 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 ent used for loc
f6430 6b 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65 4c king */. winceL
f6440 6f 63 6b 20 6c 6f 63 61 6c 3b 20 20 20 20 20 20 ock local;
f6450 20 20 2f 2a 20 4c 6f 63 6b 73 20 6f 62 74 61 69 /* Locks obtai
f6460 6e 65 64 20 62 79 20 74 68 69 73 20 69 6e 73 74 ned by this inst
f6470 61 6e 63 65 20 6f 66 20 77 69 6e 46 69 6c 65 20 ance of winFile
f6480 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f 63 6b 20 2a */. winceLock *
f6490 73 68 61 72 65 64 3b 20 20 20 20 20 20 2f 2a 20 shared; /*
f64a0 47 6c 6f 62 61 6c 20 73 68 61 72 65 64 20 6c 6f Global shared lo
f64b0 63 6b 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 ck memory for th
f64c0 65 20 66 69 6c 65 20 20 2a 2f 0a 23 65 6e 64 69 e file */.#endi
f64d0 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 f.};../*.** Forw
f64e0 61 72 64 20 70 72 6f 74 6f 74 79 70 65 73 2e 0a ard prototypes..
f64f0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 */.static int ge
f6500 74 53 65 63 74 6f 72 53 69 7a 65 28 0a 20 20 20 tSectorSize(.
f6510 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
f6520 66 73 2c 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 fs,. const ch
f6530 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 20 20 20 ar *zRelative
f6540 20 20 2f 2a 20 55 54 46 2d 38 20 66 69 6c 65 20 /* UTF-8 file
f6550 6e 61 6d 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a name */.);../*.*
f6560 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
f6570 76 61 72 69 61 62 6c 65 20 69 73 20 28 6e 6f 72 variable is (nor
f6580 6d 61 6c 6c 79 29 20 73 65 74 20 6f 6e 63 65 20 mally) set once
f6590 61 6e 64 20 6e 65 76 65 72 20 63 68 61 6e 67 65 and never change
f65a0 73 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 2e s.** thereafter.
f65b0 20 20 49 74 20 72 65 63 6f 72 64 73 20 77 68 65 It records whe
f65c0 74 68 65 72 20 74 68 65 20 6f 70 65 72 61 74 69 ther the operati
f65d0 6e 67 20 73 79 73 74 65 6d 20 69 73 20 57 69 6e ng system is Win
f65e0 39 35 0a 2a 2a 20 6f 72 20 57 69 6e 4e 54 2e 0a 95.** or WinNT..
f65f0 2a 2a 0a 2a 2a 20 30 3a 20 20 20 4f 70 65 72 61 **.** 0: Opera
f6600 74 69 6e 67 20 73 79 73 74 65 6d 20 75 6e 6b 6e ting system unkn
f6610 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 20 20 4f 70 65 own..** 1: Ope
f6620 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 rating system is
f6630 20 57 69 6e 39 35 2e 0a 2a 2a 20 32 3a 20 20 20 Win95..** 2:
f6640 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d Operating system
f6650 20 69 73 20 57 69 6e 4e 54 2e 0a 2a 2a 0a 2a 2a is WinNT..**.**
f6660 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 66 61 63 In order to fac
f6670 69 6c 69 74 61 74 65 20 74 65 73 74 69 6e 67 20 ilitate testing
f6680 6f 6e 20 61 20 57 69 6e 4e 54 20 73 79 73 74 65 on a WinNT syste
f6690 6d 2c 20 74 68 65 20 74 65 73 74 20 66 69 78 74 m, the test fixt
f66a0 75 72 65 0a 2a 2a 20 63 61 6e 20 6d 61 6e 75 61 ure.** can manua
f66b0 6c 6c 79 20 73 65 74 20 74 68 69 73 20 76 61 6c lly set this val
f66c0 75 65 20 74 6f 20 31 20 74 6f 20 65 6d 75 6c 61 ue to 1 to emula
f66d0 74 65 20 57 69 6e 39 38 20 62 65 68 61 76 69 6f te Win98 behavio
f66e0 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c r..*/.#ifdef SQL
f66f0 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
f6700 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
f6710 6f 73 5f 74 79 70 65 20 3d 20 30 3b 0a 23 65 6c os_type = 0;.#el
f6720 73 65 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 se.static int sq
f6730 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 lite3_os_type =
f6740 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0;.#endif../*.**
f6750 20 52 65 74 75 72 6e 20 74 72 75 65 20 28 6e 6f Return true (no
f6760 6e 2d 7a 65 72 6f 29 20 69 66 20 77 65 20 61 72 n-zero) if we ar
f6770 65 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 e running under
f6780 57 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 WinNT, Win2K, Wi
f6790 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 nXP,.** or WinCE
f67a0 2e 20 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20 . Return false
f67b0 28 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 (zero) for Win95
f67c0 2c 20 57 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d , Win98, or WinM
f67d0 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 E..**.** Here is
f67e0 20 61 6e 20 69 6e 74 65 72 65 73 74 69 6e 67 20 an interesting
f67f0 6f 62 73 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 observation: Wi
f6800 6e 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 n95, Win98, and
f6810 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 WinME lack.** th
f6820 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 e LockFileEx() A
f6830 50 49 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20 PI. But we can
f6840 73 74 69 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 still statically
f6850 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 link against th
f6860 61 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e at.** API as lon
f6870 67 20 61 73 20 77 65 20 64 6f 6e 27 74 20 63 61 g as we don't ca
f6880 6c 6c 20 69 74 20 77 68 65 6e 20 72 75 6e 6e 69 ll it when runni
f6890 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 ng Win95/98/ME.
f68a0 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 A call to.** th
f68b0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
f68c0 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 ed to determine
f68d0 69 66 20 74 68 65 20 68 6f 73 74 20 69 73 20 57 if the host is W
f68e0 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a in95/98/ME or.**
f68f0 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 WinNT/2K/XP so
f6900 74 68 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e 6f that we will kno
f6910 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 w whether or not
f6920 20 77 65 20 63 61 6e 20 73 61 66 65 6c 79 20 63 we can safely c
f6930 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 all.** the LockF
f6940 69 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a 2f 0a ileEx() API..*/.
f6950 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
f6960 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 69 73 4e NCE.# define isN
f6970 54 28 29 20 20 28 31 29 0a 23 65 6c 73 65 0a 20 T() (1).#else.
f6980 20 73 74 61 74 69 63 20 69 6e 74 20 69 73 4e 54 static int isNT
f6990 28 76 6f 69 64 29 7b 0a 20 20 20 20 69 66 28 20 (void){. if(
f69a0 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d sqlite3_os_type=
f69b0 3d 30 20 29 7b 0a 20 20 20 20 20 20 4f 53 56 45 =0 ){. OSVE
f69c0 52 53 49 4f 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b RSIONINFO sInfo;
f69d0 0a 20 20 20 20 20 20 73 49 6e 66 6f 2e 64 77 4f . sInfo.dwO
f69e0 53 56 65 72 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 SVersionInfoSize
f69f0 20 3d 20 73 69 7a 65 6f 66 28 73 49 6e 66 6f 29 = sizeof(sInfo)
f6a00 3b 0a 20 20 20 20 20 20 47 65 74 56 65 72 73 69 ;. GetVersi
f6a10 6f 6e 45 78 28 26 73 49 6e 66 6f 29 3b 0a 20 20 onEx(&sInfo);.
f6a20 20 20 20 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 sqlite3_os_t
f6a30 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c ype = sInfo.dwPl
f6a40 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c atformId==VER_PL
f6a50 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 ATFORM_WIN32_NT
f6a60 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 ? 2 : 1;. }.
f6a70 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 return sqlite
f6a80 33 5f 6f 73 5f 74 79 70 65 3d 3d 32 3b 0a 20 20 3_os_type==2;.
f6a90 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
f6aa0 54 45 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a TE_OS_WINCE */..
f6ab0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 /*.** Convert a
f6ac0 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 UTF-8 string to
f6ad0 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 microsoft unicod
f6ae0 65 20 28 55 54 46 2d 31 36 3f 29 2e 20 0a 2a 2a e (UTF-16?). .**
f6af0 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c .** Space to hol
f6b00 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 d the returned s
f6b10 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 tring is obtaine
f6b20 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a d from malloc..*
f6b30 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52 20 2a /.static WCHAR *
f6b40 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 63 6f utf8ToUnicode(co
f6b50 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
f6b60 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 43 68 61 ame){. int nCha
f6b70 72 3b 0a 20 20 57 43 48 41 52 20 2a 7a 57 69 64 r;. WCHAR *zWid
f6b80 65 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 6e 43 eFilename;.. nC
f6b90 68 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 har = MultiByteT
f6ba0 6f 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 oWideChar(CP_UTF
f6bb0 38 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 8, 0, zFilename,
f6bc0 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 20 -1, NULL, 0);.
f6bd0 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d zWideFilename =
f6be0 20 6d 61 6c 6c 6f 63 28 20 6e 43 68 61 72 2a 73 malloc( nChar*s
f6bf0 69 7a 65 6f 66 28 7a 57 69 64 65 46 69 6c 65 6e izeof(zWideFilen
f6c00 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 ame[0]) );. if(
f6c10 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3d 3d zWideFilename==
f6c20 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
f6c30 30 3b 0a 20 20 7d 0a 20 20 6e 43 68 61 72 20 3d 0;. }. nChar =
f6c40 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 MultiByteToWide
f6c50 43 68 61 72 28 43 50 5f 55 54 46 38 2c 20 30 2c Char(CP_UTF8, 0,
f6c60 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 zFilename, -1,
f6c70 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 6e zWideFilename, n
f6c80 43 68 61 72 29 3b 0a 20 20 69 66 28 20 6e 43 68 Char);. if( nCh
f6c90 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20 66 72 65 ar==0 ){. fre
f6ca0 65 28 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 e(zWideFilename)
f6cb0 3b 0a 20 20 20 20 7a 57 69 64 65 46 69 6c 65 6e ;. zWideFilen
f6cc0 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 ame = 0;. }. r
f6cd0 65 74 75 72 6e 20 7a 57 69 64 65 46 69 6c 65 6e eturn zWideFilen
f6ce0 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f ame;.}../*.** Co
f6cf0 6e 76 65 72 74 20 6d 69 63 72 6f 73 6f 66 74 20 nvert microsoft
f6d00 75 6e 69 63 6f 64 65 20 74 6f 20 55 54 46 2d 38 unicode to UTF-8
f6d10 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 . Space to hold
f6d20 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 the returned st
f6d30 72 69 6e 67 20 69 73 0a 2a 2a 20 6f 62 74 61 69 ring is.** obtai
f6d40 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 ned from malloc(
f6d50 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 )..*/.static cha
f6d60 72 20 2a 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 r *unicodeToUtf8
f6d70 28 63 6f 6e 73 74 20 57 43 48 41 52 20 2a 7a 57 (const WCHAR *zW
f6d80 69 64 65 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 ideFilename){.
f6d90 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 63 68 61 int nByte;. cha
f6da0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 r *zFilename;..
f6db0 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 nByte = WideCha
f6dc0 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f rToMultiByte(CP_
f6dd0 55 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69 UTF8, 0, zWideFi
f6de0 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 lename, -1, 0, 0
f6df0 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65 , 0, 0);. zFile
f6e00 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e name = malloc( n
f6e10 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a 46 Byte );. if( zF
f6e20 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 ilename==0 ){.
f6e30 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
f6e40 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 nByte = WideCh
f6e50 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 arToMultiByte(CP
f6e60 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 _UTF8, 0, zWideF
f6e70 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 ilename, -1, zFi
f6e80 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 lename, nByte,.
f6e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 0,
f6eb0 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 20 0);. if( nByte
f6ec0 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 72 65 65 == 0 ){. free
f6ed0 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 (zFilename);.
f6ee0 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a zFilename = 0;.
f6ef0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 }. return zFi
f6f00 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a lename;.}../*.**
f6f10 20 43 6f 6e 76 65 72 74 20 61 6e 20 61 6e 73 69 Convert an ansi
f6f20 20 73 74 72 69 6e 67 20 74 6f 20 6d 69 63 72 6f string to micro
f6f30 73 6f 66 74 20 75 6e 69 63 6f 64 65 2c 20 62 61 soft unicode, ba
f6f40 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 75 sed on the.** cu
f6f50 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 73 rrent codepage s
f6f60 65 74 74 69 6e 67 73 20 66 6f 72 20 66 69 6c 65 ettings for file
f6f70 20 61 70 69 73 2e 0a 2a 2a 20 0a 2a 2a 20 53 70 apis..** .** Sp
f6f80 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ace to hold the
f6f90 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 returned string
f6fa0 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 is obtained.** f
f6fb0 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f 0a 73 rom malloc..*/.s
f6fc0 74 61 74 69 63 20 57 43 48 41 52 20 2a 6d 62 63 tatic WCHAR *mbc
f6fd0 73 54 6f 55 6e 69 63 6f 64 65 28 63 6f 6e 73 74 sToUnicode(const
f6fe0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
f6ff0 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a ){. int nByte;.
f7000 20 20 57 43 48 41 52 20 2a 7a 4d 62 63 73 46 69 WCHAR *zMbcsFi
f7010 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f lename;. int co
f7020 64 65 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 depage = AreFile
f7030 41 70 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f ApisANSI() ? CP_
f7040 41 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a ACP : CP_OEMCP;.
f7050 0a 20 20 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 . nByte = Multi
f7060 42 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 63 ByteToWideChar(c
f7070 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c odepage, 0, zFil
f7080 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c ename, -1, NULL,
f7090 30 29 2a 73 69 7a 65 6f 66 28 57 43 48 41 52 29 0)*sizeof(WCHAR)
f70a0 3b 0a 20 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d ;. zMbcsFilenam
f70b0 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 e = malloc( nByt
f70c0 65 2a 73 69 7a 65 6f 66 28 7a 4d 62 63 73 46 69 e*sizeof(zMbcsFi
f70d0 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 lename[0]) );.
f70e0 69 66 28 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d if( zMbcsFilenam
f70f0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 e==0 ){. retu
f7100 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 rn 0;. }. nByt
f7110 65 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 e = MultiByteToW
f7120 69 64 65 43 68 61 72 28 63 6f 64 65 70 61 67 65 ideChar(codepage
f7130 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 , 0, zFilename,
f7140 2d 31 2c 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d -1, zMbcsFilenam
f7150 65 2c 20 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 e, nByte);. if(
f7160 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a 20 20 20 nByte==0 ){.
f7170 20 66 72 65 65 28 7a 4d 62 63 73 46 69 6c 65 6e free(zMbcsFilen
f7180 61 6d 65 29 3b 0a 20 20 20 20 7a 4d 62 63 73 46 ame);. zMbcsF
f7190 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d ilename = 0;. }
f71a0 0a 20 20 72 65 74 75 72 6e 20 7a 4d 62 63 73 46 . return zMbcsF
f71b0 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a ilename;.}../*.*
f71c0 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73 * Convert micros
f71d0 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 6d oft unicode to m
f71e0 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 74 ultibyte charact
f71f0 65 72 20 73 74 72 69 6e 67 2c 20 62 61 73 65 64 er string, based
f7200 20 6f 6e 20 74 68 65 0a 2a 2a 20 75 73 65 72 27 on the.** user'
f7210 73 20 41 6e 73 69 20 63 6f 64 65 70 61 67 65 2e s Ansi codepage.
f7220 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 .**.** Space to
f7230 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 hold the returne
f7240 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 d string is obta
f7250 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c ined from.** mal
f7260 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 loc()..*/.static
f7270 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f char *unicodeTo
f7280 4d 62 63 73 28 63 6f 6e 73 74 20 57 43 48 41 52 Mbcs(const WCHAR
f7290 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 *zWideFilename)
f72a0 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 {. int nByte;.
f72b0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
f72c0 3b 0a 20 20 69 6e 74 20 63 6f 64 65 70 61 67 65 ;. int codepage
f72d0 20 3d 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e = AreFileApisAN
f72e0 53 49 28 29 20 3f 20 43 50 5f 41 43 50 20 3a 20 SI() ? CP_ACP :
f72f0 43 50 5f 4f 45 4d 43 50 3b 0a 0a 20 20 6e 42 79 CP_OEMCP;.. nBy
f7300 74 65 20 3d 20 57 69 64 65 43 68 61 72 54 6f 4d te = WideCharToM
f7310 75 6c 74 69 42 79 74 65 28 63 6f 64 65 70 61 67 ultiByte(codepag
f7320 65 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e e, 0, zWideFilen
f7330 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 30 ame, -1, 0, 0, 0
f7340 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65 6e 61 6d , 0);. zFilenam
f7350 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 e = malloc( nByt
f7360 65 20 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 e );. if( zFile
f7370 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 name==0 ){. r
f7380 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e eturn 0;. }. n
f7390 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 54 Byte = WideCharT
f73a0 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f 64 65 70 oMultiByte(codep
f73b0 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c age, 0, zWideFil
f73c0 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c 65 ename, -1, zFile
f73d0 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20 20 name, nByte,.
f73e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f73f0 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 29 0, 0)
f7400 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d ;. if( nByte ==
f7410 20 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 0 ){. free(z
f7420 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a Filename);. z
f7430 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 Filename = 0;.
f7440 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 }. return zFile
f7450 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 name;.}../*.** C
f7460 6f 6e 76 65 72 74 20 6d 75 6c 74 69 62 79 74 65 onvert multibyte
f7470 20 63 68 61 72 61 63 74 65 72 20 73 74 72 69 6e character strin
f7480 67 20 74 6f 20 55 54 46 2d 38 2e 20 20 53 70 61 g to UTF-8. Spa
f7490 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a ce to hold the.*
f74a0 2a 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e * returned strin
f74b0 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 g is obtained fr
f74c0 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a om malloc()..*/.
f74d0 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 SQLITE_API char
f74e0 2a 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d *sqlite3_win32_m
f74f0 62 63 73 5f 74 6f 5f 75 74 66 38 28 63 6f 6e 73 bcs_to_utf8(cons
f7500 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
f7510 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c e){. char *zFil
f7520 65 6e 61 6d 65 55 74 66 38 3b 0a 20 20 57 43 48 enameUtf8;. WCH
f7530 41 52 20 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 AR *zTmpWide;..
f7540 20 7a 54 6d 70 57 69 64 65 20 3d 20 6d 62 63 73 zTmpWide = mbcs
f7550 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e ToUnicode(zFilen
f7560 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 ame);. if( zTmp
f7570 57 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 Wide==0 ){. r
f7580 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a eturn 0;. }. z
f7590 46 69 6c 65 6e 61 6d 65 55 74 66 38 20 3d 20 75 FilenameUtf8 = u
f75a0 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 6d nicodeToUtf8(zTm
f75b0 70 57 69 64 65 29 3b 0a 20 20 66 72 65 65 28 7a pWide);. free(z
f75c0 54 6d 70 57 69 64 65 29 3b 0a 20 20 72 65 74 75 TmpWide);. retu
f75d0 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 rn zFilenameUtf8
f75e0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 ;.}../*.** Conve
f75f0 72 74 20 55 54 46 2d 38 20 74 6f 20 6d 75 6c 74 rt UTF-8 to mult
f7600 69 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 ibyte character
f7610 73 74 72 69 6e 67 2e 20 20 53 70 61 63 65 20 74 string. Space t
f7620 6f 20 68 6f 6c 64 20 74 68 65 20 0a 2a 2a 20 72 o hold the .** r
f7630 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 eturned string i
f7640 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 s obtained from
f7650 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 malloc()..*/.sta
f7660 74 69 63 20 63 68 61 72 20 2a 75 74 66 38 54 6f tic char *utf8To
f7670 4d 62 63 73 28 63 6f 6e 73 74 20 63 68 61 72 20 Mbcs(const char
f7680 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 63 *zFilename){. c
f7690 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 4d 62 har *zFilenameMb
f76a0 63 73 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6d cs;. WCHAR *zTm
f76b0 70 57 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57 69 pWide;.. zTmpWi
f76c0 64 65 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f de = utf8ToUnico
f76d0 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 de(zFilename);.
f76e0 20 69 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30 if( zTmpWide==0
f76f0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
f7700 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d ;. }. zFilenam
f7710 65 4d 62 63 73 20 3d 20 75 6e 69 63 6f 64 65 54 eMbcs = unicodeT
f7720 6f 4d 62 63 73 28 7a 54 6d 70 57 69 64 65 29 3b oMbcs(zTmpWide);
f7730 0a 20 20 66 72 65 65 28 7a 54 6d 70 57 69 64 65 . free(zTmpWide
f7740 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c );. return zFil
f7750 65 6e 61 6d 65 4d 62 63 73 3b 0a 7d 0a 0a 23 69 enameMbcs;.}..#i
f7760 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
f7770 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a E./*************
f7780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f77a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f77b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
f77c0 54 68 69 73 20 73 65 63 74 69 6f 6e 20 63 6f 6e This section con
f77d0 74 61 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 57 tains code for W
f77e0 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a 2f 0a 2f 2a inCE only..*/./*
f77f0 0a 2a 2a 20 57 69 6e 64 6f 77 73 43 45 20 64 6f .** WindowsCE do
f7800 65 73 20 6e 6f 74 20 68 61 76 65 20 61 20 6c 6f es not have a lo
f7810 63 61 6c 74 69 6d 65 28 29 20 66 75 6e 63 74 69 caltime() functi
f7820 6f 6e 2e 20 20 53 6f 20 63 72 65 61 74 65 20 61 on. So create a
f7830 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 65 2e 0a .** substitute..
f7840 2a 2f 0a 73 74 72 75 63 74 20 74 6d 20 2a 5f 5f */.struct tm *__
f7850 63 64 65 63 6c 20 6c 6f 63 61 6c 74 69 6d 65 28 cdecl localtime(
f7860 63 6f 6e 73 74 20 74 69 6d 65 5f 74 20 2a 74 29 const time_t *t)
f7870 0a 7b 0a 20 20 73 74 61 74 69 63 20 73 74 72 75 .{. static stru
f7880 63 74 20 74 6d 20 79 3b 0a 20 20 46 49 4c 45 54 ct tm y;. FILET
f7890 49 4d 45 20 75 54 6d 2c 20 6c 54 6d 3b 0a 20 20 IME uTm, lTm;.
f78a0 53 59 53 54 45 4d 54 49 4d 45 20 70 54 6d 3b 0a SYSTEMTIME pTm;.
f78b0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
f78c0 74 36 34 3b 0a 20 20 74 36 34 20 3d 20 2a 74 3b t64;. t64 = *t;
f78d0 0a 20 20 74 36 34 20 3d 20 28 74 36 34 20 2b 20 . t64 = (t64 +
f78e0 31 31 36 34 34 34 37 33 36 30 30 29 2a 31 30 30 11644473600)*100
f78f0 30 30 30 30 30 3b 0a 20 20 75 54 6d 2e 64 77 4c 00000;. uTm.dwL
f7900 6f 77 44 61 74 65 54 69 6d 65 20 3d 20 28 44 57 owDateTime = (DW
f7910 4f 52 44 29 28 74 36 34 20 26 20 30 78 46 46 46 ORD)(t64 & 0xFFF
f7920 46 46 46 46 46 29 3b 0a 20 20 75 54 6d 2e 64 77 FFFFF);. uTm.dw
f7930 48 69 67 68 44 61 74 65 54 69 6d 65 3d 20 28 44 HighDateTime= (D
f7940 57 4f 52 44 29 28 74 36 34 20 3e 3e 20 33 32 29 WORD)(t64 >> 32)
f7950 3b 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f 4c 6f ;. FileTimeToLo
f7960 63 61 6c 46 69 6c 65 54 69 6d 65 28 26 75 54 6d calFileTime(&uTm
f7970 2c 26 6c 54 6d 29 3b 0a 20 20 46 69 6c 65 54 69 ,&lTm);. FileTi
f7980 6d 65 54 6f 53 79 73 74 65 6d 54 69 6d 65 28 26 meToSystemTime(&
f7990 6c 54 6d 2c 26 70 54 6d 29 3b 0a 20 20 79 2e 74 lTm,&pTm);. y.t
f79a0 6d 5f 79 65 61 72 20 3d 20 70 54 6d 2e 77 59 65 m_year = pTm.wYe
f79b0 61 72 20 2d 20 31 39 30 30 3b 0a 20 20 79 2e 74 ar - 1900;. y.t
f79c0 6d 5f 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d 6f 6e m_mon = pTm.wMon
f79d0 74 68 20 2d 20 31 3b 0a 20 20 79 2e 74 6d 5f 77 th - 1;. y.tm_w
f79e0 64 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 4f 66 day = pTm.wDayOf
f79f0 57 65 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d 64 61 Week;. y.tm_mda
f7a00 79 20 3d 20 70 54 6d 2e 77 44 61 79 3b 0a 20 20 y = pTm.wDay;.
f7a10 79 2e 74 6d 5f 68 6f 75 72 20 3d 20 70 54 6d 2e y.tm_hour = pTm.
f7a20 77 48 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f 6d 69 wHour;. y.tm_mi
f7a30 6e 20 3d 20 70 54 6d 2e 77 4d 69 6e 75 74 65 3b n = pTm.wMinute;
f7a40 0a 20 20 79 2e 74 6d 5f 73 65 63 20 3d 20 70 54 . y.tm_sec = pT
f7a50 6d 2e 77 53 65 63 6f 6e 64 3b 0a 20 20 72 65 74 m.wSecond;. ret
f7a60 75 72 6e 20 26 79 3b 0a 7d 0a 0a 2f 2a 20 54 68 urn &y;.}../* Th
f7a70 69 73 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 is will never be
f7a80 20 63 61 6c 6c 65 64 2c 20 62 75 74 20 64 65 66 called, but def
f7a90 69 6e 65 64 20 74 6f 20 6d 61 6b 65 20 74 68 65 ined to make the
f7aa0 20 63 6f 64 65 20 63 6f 6d 70 69 6c 65 20 2a 2f code compile */
f7ab0 0a 23 64 65 66 69 6e 65 20 47 65 74 54 65 6d 70 .#define GetTemp
f7ac0 50 61 74 68 41 28 61 2c 62 29 0a 0a 23 64 65 66 PathA(a,b)..#def
f7ad0 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 28 61 2c 62 ine LockFile(a,b
f7ae0 2c 63 2c 64 2c 65 29 20 20 20 20 20 20 20 77 69 ,c,d,e) wi
f7af0 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 26 61 2c 20 nceLockFile(&a,
f7b00 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 66 b, c, d, e).#def
f7b10 69 6e 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 61 ine UnlockFile(a
f7b20 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 77 69 ,b,c,d,e) wi
f7b30 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 26 61 nceUnlockFile(&a
f7b40 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 , b, c, d, e).#d
f7b50 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 45 78 efine LockFileEx
f7b60 28 61 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 20 20 (a,b,c,d,e,f)
f7b70 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 winceLockFileEx(
f7b80 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 2c 20 &a, b, c, d, e,
f7b90 66 29 0a 0a 23 64 65 66 69 6e 65 20 48 41 4e 44 f)..#define HAND
f7ba0 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 61 29 LE_TO_WINFILE(a)
f7bb0 20 28 77 69 6e 46 69 6c 65 2a 29 26 28 28 63 68 (winFile*)&((ch
f7bc0 61 72 2a 29 61 29 5b 2d 28 69 6e 74 29 6f 66 66 ar*)a)[-(int)off
f7bd0 73 65 74 6f 66 28 77 69 6e 46 69 6c 65 2c 68 29 setof(winFile,h)
f7be0 5d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 ]../*.** Acquire
f7bf0 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 68 a lock on the h
f7c00 61 6e 64 6c 65 20 68 0a 2a 2f 0a 73 74 61 74 69 andle h.*/.stati
f7c10 63 20 76 6f 69 64 20 77 69 6e 63 65 4d 75 74 65 c void winceMute
f7c20 78 41 63 71 75 69 72 65 28 48 41 4e 44 4c 45 20 xAcquire(HANDLE
f7c30 68 29 7b 0a 20 20 20 44 57 4f 52 44 20 64 77 45 h){. DWORD dwE
f7c40 72 72 3b 0a 20 20 20 64 6f 20 7b 0a 20 20 20 20 rr;. do {.
f7c50 20 64 77 45 72 72 20 3d 20 57 61 69 74 46 6f 72 dwErr = WaitFor
f7c60 53 69 6e 67 6c 65 4f 62 6a 65 63 74 28 68 2c 20 SingleObject(h,
f7c70 49 4e 46 49 4e 49 54 45 29 3b 0a 20 20 20 7d 20 INFINITE);. }
f7c80 77 68 69 6c 65 20 28 64 77 45 72 72 20 21 3d 20 while (dwErr !=
f7c90 57 41 49 54 5f 4f 42 4a 45 43 54 5f 30 20 26 26 WAIT_OBJECT_0 &&
f7ca0 20 64 77 45 72 72 20 21 3d 20 57 41 49 54 5f 41 dwErr != WAIT_A
f7cb0 42 41 4e 44 4f 4e 45 44 29 3b 0a 7d 0a 2f 2a 0a BANDONED);.}./*.
f7cc0 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 6c 6f 63 ** Release a loc
f7cd0 6b 20 61 63 71 75 69 72 65 64 20 62 79 20 77 69 k acquired by wi
f7ce0 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 nceMutexAcquire(
f7cf0 29 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 77 69 6e ).*/.#define win
f7d00 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 68 ceMutexRelease(h
f7d10 29 20 52 65 6c 65 61 73 65 4d 75 74 65 78 28 68 ) ReleaseMutex(h
f7d20 29 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 )../*.** Create
f7d30 74 68 65 20 6d 75 74 65 78 20 61 6e 64 20 73 68 the mutex and sh
f7d40 61 72 65 64 20 6d 65 6d 6f 72 79 20 75 73 65 64 ared memory used
f7d50 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 for locking in
f7d60 74 68 65 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 the file.** desc
f7d70 72 69 70 74 6f 72 20 70 46 69 6c 65 0a 2a 2f 0a riptor pFile.*/.
f7d80 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 static BOOL winc
f7d90 65 43 72 65 61 74 65 4c 6f 63 6b 28 63 6f 6e 73 eCreateLock(cons
f7da0 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
f7db0 65 2c 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c e, winFile *pFil
f7dc0 65 29 7b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6f e){. WCHAR *zTo
f7dd0 6b 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4e 61 6d k;. WCHAR *zNam
f7de0 65 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 e = utf8ToUnicod
f7df0 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 e(zFilename);.
f7e00 42 4f 4f 4c 20 62 49 6e 69 74 20 3d 20 54 52 55 BOOL bInit = TRU
f7e10 45 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c E;.. /* Initial
f7e20 69 7a 65 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f ize the local lo
f7e30 63 6b 64 61 74 61 20 2a 2f 0a 20 20 5a 65 72 6f ckdata */. Zero
f7e40 4d 65 6d 6f 72 79 28 26 70 46 69 6c 65 2d 3e 6c Memory(&pFile->l
f7e50 6f 63 61 6c 2c 20 73 69 7a 65 6f 66 28 70 46 69 ocal, sizeof(pFi
f7e60 6c 65 2d 3e 6c 6f 63 61 6c 29 29 3b 0a 0a 20 20 le->local));..
f7e70 2f 2a 20 52 65 70 6c 61 63 65 20 74 68 65 20 62 /* Replace the b
f7e80 61 63 6b 73 6c 61 73 68 65 73 20 66 72 6f 6d 20 ackslashes from
f7e90 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 the filename and
f7ea0 20 6c 6f 77 65 72 63 61 73 65 20 69 74 0a 20 20 lowercase it.
f7eb0 2a 2a 20 74 6f 20 64 65 72 69 76 65 20 61 20 6d ** to derive a m
f7ec0 75 74 65 78 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 utex name. */.
f7ed0 7a 54 6f 6b 20 3d 20 43 68 61 72 4c 6f 77 65 72 zTok = CharLower
f7ee0 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 20 W(zName);. for
f7ef0 28 3b 2a 7a 54 6f 6b 3b 7a 54 6f 6b 2b 2b 29 7b (;*zTok;zTok++){
f7f00 0a 20 20 20 20 69 66 20 28 2a 7a 54 6f 6b 20 3d . if (*zTok =
f7f10 3d 20 27 5c 5c 27 29 20 2a 7a 54 6f 6b 20 3d 20 = '\\') *zTok =
f7f20 27 5f 27 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 '_';. }.. /* C
f7f30 72 65 61 74 65 2f 6f 70 65 6e 20 74 68 65 20 6e reate/open the n
f7f40 61 6d 65 64 20 6d 75 74 65 78 20 2a 2f 0a 20 20 amed mutex */.
f7f50 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 pFile->hMutex =
f7f60 43 72 65 61 74 65 4d 75 74 65 78 57 28 4e 55 4c CreateMutexW(NUL
f7f70 4c 2c 20 46 41 4c 53 45 2c 20 7a 4e 61 6d 65 29 L, FALSE, zName)
f7f80 3b 0a 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e ;. if (!pFile->
f7f90 68 4d 75 74 65 78 29 7b 0a 20 20 20 20 70 46 69 hMutex){. pFi
f7fa0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
f7fb0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a GetLastError();.
f7fc0 20 20 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b free(zName);
f7fd0 0a 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 . return FALS
f7fe0 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 E;. }.. /* Acq
f7ff0 75 69 72 65 20 74 68 65 20 6d 75 74 65 78 20 62 uire the mutex b
f8000 65 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 efore continuing
f8010 20 2a 2f 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 */. winceMutex
f8020 41 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 Acquire(pFile->h
f8030 4d 75 74 65 78 29 3b 0a 20 20 0a 20 20 2f 2a 20 Mutex);. . /*
f8040 53 69 6e 63 65 20 74 68 65 20 6e 61 6d 65 73 20 Since the names
f8050 6f 66 20 6e 61 6d 65 64 20 6d 75 74 65 78 65 73 of named mutexes
f8060 2c 20 73 65 6d 61 70 68 6f 72 65 73 2c 20 66 69 , semaphores, fi
f8070 6c 65 20 6d 61 70 70 69 6e 67 73 20 65 74 63 20 le mappings etc
f8080 61 72 65 20 0a 20 20 2a 2a 20 63 61 73 65 2d 73 are . ** case-s
f8090 65 6e 73 69 74 69 76 65 2c 20 74 61 6b 65 20 61 ensitive, take a
f80a0 64 76 61 6e 74 61 67 65 20 6f 66 20 74 68 61 74 dvantage of that
f80b0 20 62 79 20 75 70 70 65 72 63 61 73 69 6e 67 20 by uppercasing
f80c0 74 68 65 20 6d 75 74 65 78 20 6e 61 6d 65 0a 20 the mutex name.
f80d0 20 2a 2a 20 61 6e 64 20 75 73 69 6e 67 20 74 68 ** and using th
f80e0 61 74 20 61 73 20 74 68 65 20 73 68 61 72 65 64 at as the shared
f80f0 20 66 69 6c 65 6d 61 70 70 69 6e 67 20 6e 61 6d filemapping nam
f8100 65 2e 0a 20 20 2a 2f 0a 20 20 43 68 61 72 55 70 e.. */. CharUp
f8110 70 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 perW(zName);. p
f8120 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 File->hShared =
f8130 43 72 65 61 74 65 46 69 6c 65 4d 61 70 70 69 6e CreateFileMappin
f8140 67 57 28 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c gW(INVALID_HANDL
f8150 45 5f 56 41 4c 55 45 2c 20 4e 55 4c 4c 2c 0a 20 E_VALUE, NULL,.
f8160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8180 20 20 20 20 20 20 50 41 47 45 5f 52 45 41 44 57 PAGE_READW
f8190 52 49 54 45 2c 20 30 2c 20 73 69 7a 65 6f 66 28 RITE, 0, sizeof(
f81a0 77 69 6e 63 65 4c 6f 63 6b 29 2c 0a 20 20 20 20 winceLock),.
f81b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f81c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f81d0 20 20 20 7a 4e 61 6d 65 29 3b 20 20 0a 0a 20 20 zName); ..
f81e0 2f 2a 20 53 65 74 20 61 20 66 6c 61 67 20 74 68 /* Set a flag th
f81f0 61 74 20 69 6e 64 69 63 61 74 65 73 20 77 65 27 at indicates we'
f8200 72 65 20 74 68 65 20 66 69 72 73 74 20 74 6f 20 re the first to
f8210 63 72 65 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 create the memor
f8220 79 20 73 6f 20 69 74 20 0a 20 20 2a 2a 20 6d 75 y so it . ** mu
f8230 73 74 20 62 65 20 7a 65 72 6f 2d 69 6e 69 74 69 st be zero-initi
f8240 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 66 20 28 alized */. if (
f8250 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 3d GetLastError() =
f8260 3d 20 45 52 52 4f 52 5f 41 4c 52 45 41 44 59 5f = ERROR_ALREADY_
f8270 45 58 49 53 54 53 29 7b 0a 20 20 20 20 62 49 6e EXISTS){. bIn
f8280 69 74 20 3d 20 46 41 4c 53 45 3b 0a 20 20 7d 0a it = FALSE;. }.
f8290 0a 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a . free(zName);.
f82a0 0a 20 20 2f 2a 20 49 66 20 77 65 20 73 75 63 63 . /* If we succ
f82b0 65 65 64 65 64 20 69 6e 20 6d 61 6b 69 6e 67 20 eeded in making
f82c0 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 the shared memor
f82d0 79 20 68 61 6e 64 6c 65 2c 20 6d 61 70 20 69 74 y handle, map it
f82e0 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 . */. if (pFile
f82f0 2d 3e 68 53 68 61 72 65 64 29 7b 0a 20 20 20 20 ->hShared){.
f8300 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 20 3d 20 pFile->shared =
f8310 28 77 69 6e 63 65 4c 6f 63 6b 2a 29 4d 61 70 56 (winceLock*)MapV
f8320 69 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d iewOfFile(pFile-
f8330 3e 68 53 68 61 72 65 64 2c 20 0a 20 20 20 20 20 >hShared, .
f8340 20 20 20 20 20 20 20 20 46 49 4c 45 5f 4d 41 50 FILE_MAP
f8350 5f 52 45 41 44 7c 46 49 4c 45 5f 4d 41 50 5f 57 _READ|FILE_MAP_W
f8360 52 49 54 45 2c 20 30 2c 20 30 2c 20 73 69 7a 65 RITE, 0, 0, size
f8370 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a of(winceLock));.
f8380 20 20 20 20 2f 2a 20 49 66 20 6d 61 70 70 69 6e /* If mappin
f8390 67 20 66 61 69 6c 65 64 2c 20 63 6c 6f 73 65 20 g failed, close
f83a0 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 the shared memor
f83b0 79 20 68 61 6e 64 6c 65 20 61 6e 64 20 65 72 61 y handle and era
f83c0 73 65 20 69 74 20 2a 2f 0a 20 20 20 20 69 66 20 se it */. if
f83d0 28 21 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 (!pFile->shared)
f83e0 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c {. pFile->l
f83f0 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 astErrno = GetLa
f8400 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 20 stError();.
f8410 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 CloseHandle(pFi
f8420 6c 65 2d 3e 68 53 68 61 72 65 64 29 3b 0a 20 20 le->hShared);.
f8430 20 20 20 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 pFile->hShar
f8440 65 64 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d ed = NULL;. }
f8450 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 73 68 . }.. /* If sh
f8460 61 72 65 64 20 6d 65 6d 6f 72 79 20 63 6f 75 6c ared memory coul
f8470 64 20 6e 6f 74 20 62 65 20 63 72 65 61 74 65 64 d not be created
f8480 2c 20 74 68 65 6e 20 63 6c 6f 73 65 20 74 68 65 , then close the
f8490 20 6d 75 74 65 78 20 61 6e 64 20 66 61 69 6c 20 mutex and fail
f84a0 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e */. if (pFile->
f84b0 68 53 68 61 72 65 64 20 3d 3d 20 4e 55 4c 4c 29 hShared == NULL)
f84c0 7b 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 {. winceMutex
f84d0 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 Release(pFile->h
f84e0 4d 75 74 65 78 29 3b 0a 20 20 20 20 43 6c 6f 73 Mutex);. Clos
f84f0 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 eHandle(pFile->h
f8500 4d 75 74 65 78 29 3b 0a 20 20 20 20 70 46 69 6c Mutex);. pFil
f8510 65 2d 3e 68 4d 75 74 65 78 20 3d 20 4e 55 4c 4c e->hMutex = NULL
f8520 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c ;. return FAL
f8530 53 45 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 SE;. }. . /*
f8540 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 73 Initialize the s
f8550 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 69 66 20 hared memory if
f8560 77 65 27 72 65 20 73 75 70 70 6f 73 65 64 20 74 we're supposed t
f8570 6f 20 2a 2f 0a 20 20 69 66 20 28 62 49 6e 69 74 o */. if (bInit
f8580 29 20 7b 0a 20 20 20 20 5a 65 72 6f 4d 65 6d 6f ) {. ZeroMemo
f8590 72 79 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 ry(pFile->shared
f85a0 2c 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f , sizeof(winceLo
f85b0 63 6b 29 29 3b 0a 20 20 7d 0a 0a 20 20 77 69 6e ck));. }.. win
f85c0 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 ceMutexRelease(p
f85d0 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 File->hMutex);.
f85e0 20 72 65 74 75 72 6e 20 54 52 55 45 3b 0a 7d 0a return TRUE;.}.
f85f0 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 74 ./*.** Destroy t
f8600 68 65 20 70 61 72 74 20 6f 66 20 77 69 6e 46 69 he part of winFi
f8610 6c 65 20 74 68 61 74 20 64 65 61 6c 73 20 77 69 le that deals wi
f8620 74 68 20 77 69 6e 63 65 20 6c 6f 63 6b 73 0a 2a th wince locks.*
f8630 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 /.static void wi
f8640 6e 63 65 44 65 73 74 72 6f 79 4c 6f 63 6b 28 77 nceDestroyLock(w
f8650 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a inFile *pFile){.
f8660 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 4d 75 if (pFile->hMu
f8670 74 65 78 29 7b 0a 20 20 20 20 2f 2a 20 41 63 71 tex){. /* Acq
f8680 75 69 72 65 20 74 68 65 20 6d 75 74 65 78 20 2a uire the mutex *
f8690 2f 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 /. winceMutex
f86a0 41 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 Acquire(pFile->h
f86b0 4d 75 74 65 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 Mutex);.. /*
f86c0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c The following bl
f86d0 6f 63 6b 73 20 73 68 6f 75 6c 64 20 70 72 6f 62 ocks should prob
f86e0 61 62 6c 79 20 61 73 73 65 72 74 20 69 6e 20 64 ably assert in d
f86f0 65 62 75 67 20 6d 6f 64 65 2c 20 62 75 74 20 74 ebug mode, but t
f8700 68 65 79 0a 20 20 20 20 20 20 20 61 72 65 20 74 hey. are t
f8710 6f 20 63 6c 65 61 6e 75 70 20 69 6e 20 63 61 73 o cleanup in cas
f8720 65 20 61 6e 79 20 6c 6f 63 6b 73 20 72 65 6d 61 e any locks rema
f8730 69 6e 65 64 20 6f 70 65 6e 20 2a 2f 0a 20 20 20 ined open */.
f8740 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 if (pFile->loca
f8750 6c 2e 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 20 l.nReaders){.
f8760 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 pFile->shared
f8770 2d 3e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 ->nReaders --;.
f8780 20 20 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69 }. if (pFi
f8790 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 le->local.bReser
f87a0 76 65 64 29 7b 0a 20 20 20 20 20 20 70 46 69 6c ved){. pFil
f87b0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 e->shared->bRese
f87c0 72 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 rved = FALSE;.
f87d0 20 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69 6c }. if (pFil
f87e0 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e e->local.bPendin
f87f0 67 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d g){. pFile-
f8800 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e >shared->bPendin
f8810 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d g = FALSE;. }
f8820 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
f8830 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 local.bExclusive
f8840 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
f8850 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 shared->bExclusi
f8860 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 ve = FALSE;.
f8870 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 2d 72 65 66 }.. /* De-ref
f8880 65 72 65 6e 63 65 20 61 6e 64 20 63 6c 6f 73 65 erence and close
f8890 20 6f 75 72 20 63 6f 70 79 20 6f 66 20 74 68 65 our copy of the
f88a0 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 68 shared memory h
f88b0 61 6e 64 6c 65 20 2a 2f 0a 20 20 20 20 55 6e 6d andle */. Unm
f88c0 61 70 56 69 65 77 4f 66 46 69 6c 65 28 70 46 69 apViewOfFile(pFi
f88d0 6c 65 2d 3e 73 68 61 72 65 64 29 3b 0a 20 20 20 le->shared);.
f88e0 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 CloseHandle(pFi
f88f0 6c 65 2d 3e 68 53 68 61 72 65 64 29 3b 0a 0a 20 le->hShared);..
f8900 20 20 20 2f 2a 20 44 6f 6e 65 20 77 69 74 68 20 /* Done with
f8910 74 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 the mutex */.
f8920 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 winceMutexRelea
f8930 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 se(pFile->hMutex
f8940 29 3b 20 20 20 20 0a 20 20 20 20 43 6c 6f 73 65 ); . Close
f8950 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d Handle(pFile->hM
f8960 75 74 65 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 utex);. pFile
f8970 2d 3e 68 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b ->hMutex = NULL;
f8980 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41 . }.}../* .** A
f8990 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e n implementation
f89a0 20 6f 66 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 of the LockFile
f89b0 28 29 20 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 () API of window
f89c0 73 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 s for wince.*/.s
f89d0 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 tatic BOOL wince
f89e0 4c 6f 63 6b 46 69 6c 65 28 0a 20 20 48 41 4e 44 LockFile(. HAND
f89f0 4c 45 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57 LE *phFile,. DW
f8a00 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 ORD dwFileOffset
f8a10 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 Low,. DWORD dwF
f8a20 69 6c 65 4f 66 66 73 65 74 48 69 67 68 2c 0a 20 ileOffsetHigh,.
f8a30 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 DWORD nNumberOf
f8a40 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a BytesToLockLow,.
f8a50 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f DWORD nNumberO
f8a60 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 fBytesToLockHigh
f8a70 0a 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 .){. winFile *p
f8a80 46 69 6c 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f File = HANDLE_TO
f8a90 5f 57 49 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 _WINFILE(phFile)
f8aa0 3b 0a 20 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e ;. BOOL bReturn
f8ab0 20 3d 20 46 41 4c 53 45 3b 0a 0a 20 20 55 4e 55 = FALSE;.. UNU
f8ac0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 77 SED_PARAMETER(dw
f8ad0 46 69 6c 65 4f 66 66 73 65 74 48 69 67 68 29 3b FileOffsetHigh);
f8ae0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
f8af0 54 45 52 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 TER(nNumberOfByt
f8b00 65 73 54 6f 4c 6f 63 6b 48 69 67 68 29 3b 0a 0a esToLockHigh);..
f8b10 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d if (!pFile->hM
f8b20 75 74 65 78 29 20 72 65 74 75 72 6e 20 54 52 55 utex) return TRU
f8b30 45 3b 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41 E;. winceMutexA
f8b40 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d cquire(pFile->hM
f8b50 75 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 57 61 6e utex);.. /* Wan
f8b60 74 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69 76 ting an exclusiv
f8b70 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 69 66 20 e lock? */. if
f8b80 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 (dwFileOffsetLow
f8b90 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 == (DWORD)SHARE
f8ba0 44 5f 46 49 52 53 54 0a 20 20 20 20 20 20 20 26 D_FIRST. &
f8bb0 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 & nNumberOfBytes
f8bc0 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 57 ToLockLow == (DW
f8bd0 4f 52 44 29 53 48 41 52 45 44 5f 53 49 5a 45 29 ORD)SHARED_SIZE)
f8be0 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d {. if (pFile-
f8bf0 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 >shared->nReader
f8c00 73 20 3d 3d 20 30 20 26 26 20 70 46 69 6c 65 2d s == 0 && pFile-
f8c10 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 >shared->bExclus
f8c20 69 76 65 20 3d 3d 20 30 29 7b 0a 20 20 20 20 20 ive == 0){.
f8c30 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
f8c40 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54 52 >bExclusive = TR
f8c50 55 45 3b 0a 20 20 20 20 20 20 20 70 46 69 6c 65 UE;. pFile
f8c60 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 ->local.bExclusi
f8c70 76 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 ve = TRUE;.
f8c80 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 bReturn = TRUE
f8c90 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
f8ca0 2a 20 57 61 6e 74 20 61 20 72 65 61 64 2d 6f 6e * Want a read-on
f8cb0 6c 79 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 6c ly lock? */. el
f8cc0 73 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 se if (dwFileOff
f8cd0 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 setLow == (DWORD
f8ce0 29 53 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 )SHARED_FIRST &&
f8cf0 0a 20 20 20 20 20 20 20 20 20 20 20 6e 4e 75 6d . nNum
f8d00 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b berOfBytesToLock
f8d10 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 Low == 1){. i
f8d20 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 f (pFile->shared
f8d30 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 ->bExclusive ==
f8d40 30 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 0){. pFile-
f8d50 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 >local.nReaders
f8d60 2b 2b 3b 0a 20 20 20 20 20 20 69 66 20 28 70 46 ++;. if (pF
f8d70 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 ile->local.nRead
f8d80 65 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20 20 20 ers == 1){.
f8d90 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 pFile->shared
f8da0 2d 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 ->nReaders ++;.
f8db0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 }. bRe
f8dc0 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 turn = TRUE;.
f8dd0 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e }. }.. /* Wan
f8de0 74 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b t a pending lock
f8df0 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 ? */. else if (
f8e00 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 dwFileOffsetLow
f8e10 3d 3d 20 28 44 57 4f 52 44 29 50 45 4e 44 49 4e == (DWORD)PENDIN
f8e20 47 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 G_BYTE && nNumbe
f8e30 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f rOfBytesToLockLo
f8e40 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 2f 2a 20 w == 1){. /*
f8e50 49 66 20 6e 6f 20 70 65 6e 64 69 6e 67 20 6c 6f If no pending lo
f8e60 63 6b 20 68 61 73 20 62 65 65 6e 20 61 63 71 75 ck has been acqu
f8e70 69 72 65 64 2c 20 74 68 65 6e 20 61 63 71 75 69 ired, then acqui
f8e80 72 65 20 69 74 20 2a 2f 0a 20 20 20 20 69 66 20 re it */. if
f8e90 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e (pFile->shared->
f8ea0 62 50 65 6e 64 69 6e 67 20 3d 3d 20 30 29 20 7b bPending == 0) {
f8eb0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 . pFile->sh
f8ec0 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d ared->bPending =
f8ed0 20 54 52 55 45 3b 0a 20 20 20 20 20 20 70 46 69 TRUE;. pFi
f8ee0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 le->local.bPendi
f8ef0 6e 67 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 ng = TRUE;.
f8f00 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b bReturn = TRUE;
f8f10 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
f8f20 20 57 61 6e 74 20 61 20 72 65 73 65 72 76 65 64 Want a reserved
f8f30 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 lock? */. else
f8f40 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 if (dwFileOffse
f8f50 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 52 tLow == (DWORD)R
f8f60 45 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 20 ESERVED_BYTE &&
f8f70 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
f8f80 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 LockLow == 1){.
f8f90 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 if (pFile->sh
f8fa0 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 ared->bReserved
f8fb0 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 46 == 0) {. pF
f8fc0 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 ile->shared->bRe
f8fd0 73 65 72 76 65 64 20 3d 20 54 52 55 45 3b 0a 20 served = TRUE;.
f8fe0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 pFile->loca
f8ff0 6c 2e 62 52 65 73 65 72 76 65 64 20 3d 20 54 52 l.bReserved = TR
f9000 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 UE;. bRetur
f9010 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a n = TRUE;. }.
f9020 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 }.. winceMute
f9030 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e xRelease(pFile->
f9040 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 hMutex);. retur
f9050 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a n bReturn;.}../*
f9060 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 .** An implement
f9070 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 55 6e 6c ation of the Unl
f9080 6f 63 6b 46 69 6c 65 20 41 50 49 20 6f 66 20 77 ockFile API of w
f9090 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 indows for wince
f90a0 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 .*/.static BOOL
f90b0 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 winceUnlockFile(
f90c0 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c . HANDLE *phFil
f90d0 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c e,. DWORD dwFil
f90e0 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 eOffsetLow,. DW
f90f0 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 ORD dwFileOffset
f9100 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e High,. DWORD nN
f9110 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e umberOfBytesToUn
f9120 6c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 lockLow,. DWORD
f9130 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 nNumberOfBytesT
f9140 6f 55 6e 6c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 oUnlockHigh.){.
f9150 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 winFile *pFile
f9160 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 = HANDLE_TO_WINF
f9170 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 ILE(phFile);. B
f9180 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41 OOL bReturn = FA
f9190 4c 53 45 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 LSE;.. UNUSED_P
f91a0 41 52 41 4d 45 54 45 52 28 64 77 46 69 6c 65 4f ARAMETER(dwFileO
f91b0 66 66 73 65 74 48 69 67 68 29 3b 0a 20 20 55 4e ffsetHigh);. UN
f91c0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e USED_PARAMETER(n
f91d0 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 NumberOfBytesToU
f91e0 6e 6c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 20 69 nlockHigh);.. i
f91f0 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74 65 f (!pFile->hMute
f9200 78 29 20 72 65 74 75 72 6e 20 54 52 55 45 3b 0a x) return TRUE;.
f9210 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 winceMutexAcqu
f9220 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 ire(pFile->hMute
f9230 78 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 x);.. /* Releas
f9240 69 6e 67 20 61 20 72 65 61 64 65 72 20 6c 6f 63 ing a reader loc
f9250 6b 20 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 k or an exclusiv
f9260 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 20 28 e lock */. if (
f9270 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 dwFileOffsetLow
f9280 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 == (DWORD)SHARED
f9290 5f 46 49 52 53 54 29 7b 0a 20 20 20 20 2f 2a 20 _FIRST){. /*
f92a0 44 69 64 20 77 65 20 68 61 76 65 20 61 6e 20 65 Did we have an e
f92b0 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a xclusive lock? *
f92c0 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d /. if (pFile-
f92d0 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 >local.bExclusiv
f92e0 65 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 e){. assert
f92f0 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 (nNumberOfBytesT
f9300 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 oUnlockLow == (D
f9310 57 4f 52 44 29 53 48 41 52 45 44 5f 53 49 5a 45 WORD)SHARED_SIZE
f9320 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e );. pFile->
f9330 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 local.bExclusive
f9340 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 = FALSE;.
f9350 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 pFile->shared->b
f9360 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 Exclusive = FALS
f9370 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e E;. bReturn
f9380 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 0a = TRUE;. }..
f9390 20 20 20 20 2f 2a 20 44 69 64 20 77 65 20 6a 75 /* Did we ju
f93a0 73 74 20 68 61 76 65 20 61 20 72 65 61 64 65 72 st have a reader
f93b0 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 65 6c lock? */. el
f93c0 73 65 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f se if (pFile->lo
f93d0 63 61 6c 2e 6e 52 65 61 64 65 72 73 29 7b 0a 20 cal.nReaders){.
f93e0 20 20 20 20 20 61 73 73 65 72 74 28 6e 4e 75 6d assert(nNum
f93f0 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f berOfBytesToUnlo
f9400 63 6b 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 ckLow == (DWORD)
f9410 53 48 41 52 45 44 5f 53 49 5a 45 20 7c 7c 20 6e SHARED_SIZE || n
f9420 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 NumberOfBytesToU
f9430 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 3b 0a nlockLow == 1);.
f9440 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 pFile->loc
f9450 61 6c 2e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a al.nReaders --;.
f9460 20 20 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d if (pFile-
f9470 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 >local.nReaders
f9480 3d 3d 20 30 29 0a 20 20 20 20 20 20 7b 0a 20 20 == 0). {.
f9490 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 pFile->sha
f94a0 72 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2d 2d red->nReaders --
f94b0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
f94c0 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a bReturn = TRUE;.
f94d0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
f94e0 52 65 6c 65 61 73 69 6e 67 20 61 20 70 65 6e 64 Releasing a pend
f94f0 69 6e 67 20 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c ing lock */. el
f9500 73 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 se if (dwFileOff
f9510 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 setLow == (DWORD
f9520 29 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 )PENDING_BYTE &&
f9530 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 nNumberOfBytesT
f9540 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 oUnlockLow == 1)
f9550 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d {. if (pFile-
f9560 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 29 >local.bPending)
f9570 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c {. pFile->l
f9580 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 ocal.bPending =
f9590 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69 FALSE;. pFi
f95a0 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e le->shared->bPen
f95b0 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 ding = FALSE;.
f95c0 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 bReturn = TR
f95d0 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 UE;. }. }.
f95e0 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72 /* Releasing a r
f95f0 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a 2f 0a eserved lock */.
f9600 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c else if (dwFil
f9610 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 eOffsetLow == (D
f9620 57 4f 52 44 29 52 45 53 45 52 56 45 44 5f 42 59 WORD)RESERVED_BY
f9630 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 TE && nNumberOfB
f9640 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 ytesToUnlockLow
f9650 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 == 1){. if (p
f9660 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 File->local.bRes
f9670 65 72 76 65 64 29 20 7b 0a 20 20 20 20 20 20 70 erved) {. p
f9680 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 File->local.bRes
f9690 65 72 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 erved = FALSE;.
f96a0 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 pFile->shar
f96b0 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 ed->bReserved =
f96c0 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 FALSE;. bRe
f96d0 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 turn = TRUE;.
f96e0 20 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d }. }.. winceM
f96f0 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c utexRelease(pFil
f9700 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 e->hMutex);. re
f9710 74 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a turn bReturn;.}.
f9720 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d ./*.** An implem
f9730 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
f9740 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 LockFileEx() API
f9750 20 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 of windows for
f9760 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 wince.*/.static
f9770 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 BOOL winceLockFi
f9780 6c 65 45 78 28 0a 20 20 48 41 4e 44 4c 45 20 2a leEx(. HANDLE *
f9790 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 phFile,. DWORD
f97a0 64 77 46 6c 61 67 73 2c 0a 20 20 44 57 4f 52 44 dwFlags,. DWORD
f97b0 20 64 77 52 65 73 65 72 76 65 64 2c 0a 20 20 44 dwReserved,. D
f97c0 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 WORD nNumberOfBy
f97d0 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 tesToLockLow,.
f97e0 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 DWORD nNumberOfB
f97f0 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 2c 0a ytesToLockHigh,.
f9800 20 20 4c 50 4f 56 45 52 4c 41 50 50 45 44 20 6c LPOVERLAPPED l
f9810 70 4f 76 65 72 6c 61 70 70 65 64 0a 29 7b 0a 20 pOverlapped.){.
f9820 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
f9830 52 28 64 77 52 65 73 65 72 76 65 64 29 3b 0a 20 R(dwReserved);.
f9840 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
f9850 52 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 R(nNumberOfBytes
f9860 54 6f 4c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 20 ToLockHigh);..
f9870 2f 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 /* If the caller
f9880 20 77 61 6e 74 73 20 61 20 73 68 61 72 65 64 20 wants a shared
f9890 72 65 61 64 20 6c 6f 63 6b 2c 20 66 6f 72 77 61 read lock, forwa
f98a0 72 64 20 74 68 69 73 20 63 61 6c 6c 0a 20 20 2a rd this call. *
f98b0 2a 20 74 6f 20 77 69 6e 63 65 4c 6f 63 6b 46 69 * to winceLockFi
f98c0 6c 65 20 2a 2f 0a 20 20 69 66 20 28 6c 70 4f 76 le */. if (lpOv
f98d0 65 72 6c 61 70 70 65 64 2d 3e 4f 66 66 73 65 74 erlapped->Offset
f98e0 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 == (DWORD)SHARE
f98f0 44 5f 46 49 52 53 54 20 26 26 0a 20 20 20 20 20 D_FIRST &&.
f9900 20 64 77 46 6c 61 67 73 20 3d 3d 20 31 20 26 26 dwFlags == 1 &&
f9910 0a 20 20 20 20 20 20 6e 4e 75 6d 62 65 72 4f 66 . nNumberOf
f9920 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d BytesToLockLow =
f9930 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f = (DWORD)SHARED_
f9940 53 49 5a 45 29 7b 0a 20 20 20 20 72 65 74 75 72 SIZE){. retur
f9950 6e 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 n winceLockFile(
f9960 70 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 phFile, SHARED_F
f9970 49 52 53 54 2c 20 30 2c 20 31 2c 20 30 29 3b 0a IRST, 0, 1, 0);.
f9980 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 46 41 4c }. return FAL
f9990 53 45 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64 20 SE;.}./*.** End
f99a0 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 of the special c
f99b0 6f 64 65 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2a ode for wince.**
f99c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f99d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f99e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f99f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e ***********/.#en
f9a10 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 dif /* SQLITE_OS
f9a20 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a _WINCE */../****
f9a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 *********.** The
f9a80 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 next group of r
f9a90 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e outines implemen
f9aa0 74 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 t the I/O method
f9ab0 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 s specified.** b
f9ac0 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f y the sqlite3_io
f9ad0 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e _methods object.
f9ae0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
f9af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
f9b30 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 ../*.** Close a
f9b40 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 file..**.** It i
f9b50 73 20 72 65 70 6f 72 74 65 64 20 74 68 61 74 20 s reported that
f9b60 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6c an attempt to cl
f9b70 6f 73 65 20 61 20 68 61 6e 64 6c 65 20 6d 69 67 ose a handle mig
f9b80 68 74 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 ht sometimes.**
f9b90 66 61 69 6c 2e 20 20 54 68 69 73 20 69 73 20 61 fail. This is a
f9ba0 20 76 65 72 79 20 75 6e 72 65 61 73 6f 6e 61 62 very unreasonab
f9bb0 6c 65 20 72 65 73 75 6c 74 2c 20 62 75 74 20 77 le result, but w
f9bc0 69 6e 64 6f 77 73 20 69 73 20 6e 6f 74 6f 72 69 indows is notori
f9bd0 6f 75 73 0a 2a 2a 20 66 6f 72 20 62 65 69 6e 67 ous.** for being
f9be0 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 73 6f unreasonable so
f9bf0 20 49 20 64 6f 20 6e 6f 74 20 64 6f 75 62 74 20 I do not doubt
f9c00 74 68 61 74 20 69 74 20 6d 69 67 68 74 20 68 61 that it might ha
f9c10 70 70 65 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65 ppen. If.** the
f9c20 20 63 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77 65 close fails, we
f9c30 20 70 61 75 73 65 20 66 6f 72 20 31 30 30 20 6d pause for 100 m
f9c40 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 illiseconds and
f9c50 74 72 79 20 61 67 61 69 6e 2e 20 20 41 73 0a 2a try again. As.*
f9c60 2a 20 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c 4f * many as MX_CLO
f9c70 53 45 5f 41 54 54 45 4d 50 54 20 61 74 74 65 6d SE_ATTEMPT attem
f9c80 70 74 73 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 pts to close the
f9c90 20 68 61 6e 64 6c 65 20 61 72 65 20 6d 61 64 65 handle are made
f9ca0 20 62 65 66 6f 72 65 0a 2a 2a 20 67 69 76 69 6e before.** givin
f9cb0 67 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69 g up and returni
f9cc0 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a ng an error..*/.
f9cd0 23 64 65 66 69 6e 65 20 4d 58 5f 43 4c 4f 53 45 #define MX_CLOSE
f9ce0 5f 41 54 54 45 4d 50 54 20 33 0a 73 74 61 74 69 _ATTEMPT 3.stati
f9cf0 63 20 69 6e 74 20 77 69 6e 43 6c 6f 73 65 28 73 c int winClose(s
f9d00 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
f9d10 7b 0a 20 20 69 6e 74 20 72 63 2c 20 63 6e 74 20 {. int rc, cnt
f9d20 3d 20 30 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a = 0;. winFile *
f9d30 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 pFile = (winFile
f9d40 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 *)id;.. assert(
f9d50 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 id!=0 );. OSTR
f9d60 41 43 45 32 28 22 43 4c 4f 53 45 20 25 64 5c 6e ACE2("CLOSE %d\n
f9d70 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 ", pFile->h);.
f9d80 64 6f 7b 0a 20 20 20 20 72 63 20 3d 20 43 6c 6f do{. rc = Clo
f9d90 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e seHandle(pFile->
f9da0 68 29 3b 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 h);. }while( rc
f9db0 3d 3d 30 20 26 26 20 2b 2b 63 6e 74 20 3c 20 4d ==0 && ++cnt < M
f9dc0 58 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 X_CLOSE_ATTEMPT
f9dd0 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c 20 && (Sleep(100),
f9de0 31 29 20 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 1) );.#if SQLITE
f9df0 5f 4f 53 5f 57 49 4e 43 45 0a 23 64 65 66 69 6e _OS_WINCE.#defin
f9e00 65 20 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e e WINCE_DELETION
f9e10 5f 41 54 54 45 4d 50 54 53 20 33 0a 20 20 77 69 _ATTEMPTS 3. wi
f9e20 6e 63 65 44 65 73 74 72 6f 79 4c 6f 63 6b 28 70 nceDestroyLock(p
f9e30 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 46 69 File);. if( pFi
f9e40 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f le->zDeleteOnClo
f9e50 73 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e se ){. int cn
f9e60 74 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 t = 0;. while
f9e70 28 0a 20 20 20 20 20 20 20 20 20 20 20 44 65 6c (. Del
f9e80 65 74 65 46 69 6c 65 57 28 70 46 69 6c 65 2d 3e eteFileW(pFile->
f9e90 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3d zDeleteOnClose)=
f9ea0 3d 30 0a 20 20 20 20 20 20 20 20 26 26 20 47 65 =0. && Ge
f9eb0 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 tFileAttributesW
f9ec0 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f (pFile->zDeleteO
f9ed0 6e 43 6c 6f 73 65 29 21 3d 30 78 66 66 66 66 66 nClose)!=0xfffff
f9ee0 66 66 66 20 0a 20 20 20 20 20 20 20 20 26 26 20 fff . &&
f9ef0 63 6e 74 2b 2b 20 3c 20 57 49 4e 43 45 5f 44 45 cnt++ < WINCE_DE
f9f00 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 0a LETION_ATTEMPTS.
f9f10 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 53 6c ){. Sl
f9f20 65 65 70 28 31 30 30 29 3b 20 20 2f 2a 20 57 61 eep(100); /* Wa
f9f30 69 74 20 61 20 6c 69 74 74 6c 65 20 62 65 66 6f it a little befo
f9f40 72 65 20 74 72 79 69 6e 67 20 61 67 61 69 6e 20 re trying again
f9f50 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 66 72 65 */. }. fre
f9f60 65 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 e(pFile->zDelete
f9f70 4f 6e 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 23 65 OnClose);. }.#e
f9f80 6e 64 69 66 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 ndif. OpenCount
f9f90 65 72 28 2d 31 29 3b 0a 20 20 72 65 74 75 72 6e er(-1);. return
f9fa0 20 72 63 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 rc ? SQLITE_OK
f9fb0 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a : SQLITE_IOERR;.
f9fc0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69 }../*.** Some mi
f9fd0 63 72 6f 73 6f 66 74 20 63 6f 6d 70 69 6c 65 72 crosoft compiler
f9fe0 73 20 6c 61 63 6b 20 74 68 69 73 20 64 65 66 69 s lack this defi
f9ff0 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 nition..*/.#ifnd
fa000 65 66 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 ef INVALID_SET_F
fa010 49 4c 45 5f 50 4f 49 4e 54 45 52 0a 23 20 64 65 ILE_POINTER.# de
fa020 66 69 6e 65 20 49 4e 56 41 4c 49 44 5f 53 45 54 fine INVALID_SET
fa030 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 28 28 _FILE_POINTER ((
fa040 44 57 4f 52 44 29 2d 31 29 0a 23 65 6e 64 69 66 DWORD)-1).#endif
fa050 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 ../*.** Read dat
fa060 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e a from a file in
fa070 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65 to a buffer. Re
fa080 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 turn SQLITE_OK i
fa090 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 f all.** bytes w
fa0a0 65 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73 ere read success
fa0b0 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 fully and SQLITE
fa0c0 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69 _IOERR if anythi
fa0d0 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 ng goes.** wrong
fa0e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
fa0f0 77 69 6e 52 65 61 64 28 0a 20 20 73 71 6c 69 74 winRead(. sqlit
fa100 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 e3_file *id,
fa110 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f /* File to
fa120 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 read from */.
fa130 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 void *pBuf,
fa140 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 /* Wr
fa150 69 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f ite content into
fa160 20 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f 0a this buffer */.
fa170 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 int amt,
fa180 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
fa190 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
fa1a0 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c to read */. sql
fa1b0 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 ite3_int64 offse
fa1c0 74 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e t /* Begin
fa1d0 20 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 73 reading at this
fa1e0 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 offset */.){.
fa1f0 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 3d LONG upperBits =
fa200 20 28 4c 4f 4e 47 29 28 28 6f 66 66 73 65 74 3e (LONG)((offset>
fa210 3e 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66 >32) & 0x7ffffff
fa220 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 f);. LONG lower
fa230 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 Bits = (LONG)(of
fa240 66 73 65 74 20 26 20 30 78 66 66 66 66 66 66 66 fset & 0xfffffff
fa250 66 29 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a f);. DWORD rc;.
fa260 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 winFile *pFile
fa270 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b = (winFile*)id;
fa280 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a . DWORD error;.
fa290 20 20 44 57 4f 52 44 20 67 6f 74 3b 0a 0a 20 20 DWORD got;..
fa2a0 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b assert( id!=0 );
fa2b0 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
fa2c0 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 or(return SQLITE
fa2d0 5f 49 4f 45 52 52 5f 52 45 41 44 29 3b 0a 20 20 _IOERR_READ);.
fa2e0 4f 53 54 52 41 43 45 33 28 22 52 45 41 44 20 25 OSTRACE3("READ %
fa2f0 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 d lock=%d\n", pF
fa300 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c ile->h, pFile->l
fa310 6f 63 6b 74 79 70 65 29 3b 0a 20 20 72 63 20 3d ocktype);. rc =
fa320 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 SetFilePointer(
fa330 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 pFile->h, lowerB
fa340 69 74 73 2c 20 26 75 70 70 65 72 42 69 74 73 2c its, &upperBits,
fa350 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 FILE_BEGIN);.
fa360 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f if( rc==INVALID_
fa370 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 SET_FILE_POINTER
fa380 20 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c 61 && (error=GetLa
fa390 73 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 stError())!=NO_E
fa3a0 52 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69 6c RROR ){. pFil
fa3b0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 e->lastErrno = e
fa3c0 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 6e rror;. return
fa3d0 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 SQLITE_FULL;.
fa3e0 7d 0a 20 20 69 66 28 20 21 52 65 61 64 46 69 6c }. if( !ReadFil
fa3f0 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 e(pFile->h, pBuf
fa400 2c 20 61 6d 74 2c 20 26 67 6f 74 2c 20 30 29 20 , amt, &got, 0)
fa410 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 ){. 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 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69 66 R_READ;. }. if
fa460 28 20 67 6f 74 3d 3d 28 44 57 4f 52 44 29 61 6d ( got==(DWORD)am
fa470 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
fa480 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c SQLITE_OK;. }el
fa490 73 65 7b 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 se{. /* Unrea
fa4a0 64 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 62 d parts of the b
fa4b0 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a 65 uffer must be ze
fa4c0 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 ro-filled */.
fa4d0 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a memset(&((char*
fa4e0 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 )pBuf)[got], 0,
fa4f0 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 amt-got);. re
fa500 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
fa510 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 R_SHORT_READ;.
fa520 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 }.}../*.** Write
fa530 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 data from a buf
fa540 66 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e fer into a file.
fa550 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f Return SQLITE_
fa560 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a OK on success.**
fa570 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 or some other e
fa580 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 rror code on fai
fa590 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 lure..*/.static
fa5a0 69 6e 74 20 77 69 6e 57 72 69 74 65 28 0a 20 20 int winWrite(.
fa5b0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
fa5c0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c , /* Fil
fa5d0 65 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 e to write into
fa5e0 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
fa5f0 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 2f *pBuf, /
fa600 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 62 * The bytes to b
fa610 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 e written */. i
fa620 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 nt amt,
fa630 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
fa640 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 er of bytes to w
fa650 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 rite */. sqlite
fa660 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20 3_int64 offset
fa670 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e /* Offset in
fa680 74 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 to the file to b
fa690 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20 egin writing at
fa6a0 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 */.){. LONG upp
fa6b0 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 erBits = (LONG)(
fa6c0 28 6f 66 66 73 65 74 3e 3e 33 32 29 20 26 20 30 (offset>>32) & 0
fa6d0 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c 4f x7fffffff);. LO
fa6e0 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 28 NG lowerBits = (
fa6f0 4c 4f 4e 47 29 28 6f 66 66 73 65 74 20 26 20 30 LONG)(offset & 0
fa700 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44 57 xffffffff);. DW
fa710 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c ORD rc;. winFil
fa720 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 e *pFile = (winF
fa730 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 ile*)id;. DWORD
fa740 20 65 72 72 6f 72 3b 0a 20 20 44 57 4f 52 44 20 error;. DWORD
fa750 77 72 6f 74 65 20 3d 20 30 3b 0a 0a 20 20 61 73 wrote = 0;.. as
fa760 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 sert( id!=0 );.
fa770 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
fa780 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 (return SQLITE_I
fa790 4f 45 52 52 5f 57 52 49 54 45 29 3b 0a 20 20 53 OERR_WRITE);. S
fa7a0 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 imulateDiskfullE
fa7b0 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 rror(return SQLI
fa7c0 54 45 5f 46 55 4c 4c 29 3b 0a 20 20 4f 53 54 52 TE_FULL);. OSTR
fa7d0 41 43 45 33 28 22 57 52 49 54 45 20 25 64 20 6c ACE3("WRITE %d l
fa7e0 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 ock=%d\n", pFile
fa7f0 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ->h, pFile->lock
fa800 74 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 53 65 type);. rc = Se
fa810 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 tFilePointer(pFi
fa820 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 le->h, lowerBits
fa830 2c 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49 , &upperBits, FI
fa840 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 LE_BEGIN);. if(
fa850 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 rc==INVALID_SET
fa860 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 _FILE_POINTER &&
fa870 20 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74 45 (error=GetLastE
fa880 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f rror())!=NO_ERRO
fa890 52 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e R ){. pFile->
fa8a0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f lastErrno = erro
fa8b0 72 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 r;. return SQ
fa8c0 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20 LITE_FULL;. }.
fa8d0 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29 assert( amt>0 )
fa8e0 3b 0a 20 20 77 68 69 6c 65 28 0a 20 20 20 20 20 ;. while(.
fa8f0 61 6d 74 3e 30 0a 20 20 20 20 20 26 26 20 28 72 amt>0. && (r
fa900 63 20 3d 20 57 72 69 74 65 46 69 6c 65 28 70 46 c = WriteFile(pF
fa910 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d ile->h, pBuf, am
fa920 74 2c 20 26 77 72 6f 74 65 2c 20 30 29 29 21 3d t, &wrote, 0))!=
fa930 30 0a 20 20 20 20 20 26 26 20 77 72 6f 74 65 3e 0. && wrote>
fa940 30 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 0. ){. amt -
fa950 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 = wrote;. pBu
fa960 66 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 75 f = &((char*)pBu
fa970 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 20 f)[wrote];. }.
fa980 20 69 66 28 20 21 72 63 20 7c 7c 20 61 6d 74 3e if( !rc || amt>
fa990 28 69 6e 74 29 77 72 6f 74 65 20 29 7b 0a 20 20 (int)wrote ){.
fa9a0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
fa9b0 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f no = GetLastErro
fa9c0 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 r();. return
fa9d0 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d SQLITE_FULL;. }
fa9e0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
fa9f0 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 _OK;.}../*.** Tr
faa00 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 uncate an open f
faa10 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 69 ile to a specifi
faa20 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 ed size.*/.stati
faa30 63 20 69 6e 74 20 77 69 6e 54 72 75 6e 63 61 74 c int winTruncat
faa40 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
faa50 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 id, sqlite3_int6
faa60 34 20 6e 42 79 74 65 29 7b 0a 20 20 4c 4f 4e 47 4 nByte){. LONG
faa70 20 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c 4f upperBits = (LO
faa80 4e 47 29 28 28 6e 42 79 74 65 3e 3e 33 32 29 20 NG)((nByte>>32)
faa90 26 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 & 0x7fffffff);.
faaa0 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 LONG lowerBits
faab0 3d 20 28 4c 4f 4e 47 29 28 6e 42 79 74 65 20 26 = (LONG)(nByte &
faac0 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 0xffffffff);.
faad0 44 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 DWORD rc;. winF
faae0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 ile *pFile = (wi
faaf0 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f nFile*)id;. DWO
fab00 52 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73 73 RD error;.. ass
fab10 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 ert( id!=0 );.
fab20 4f 53 54 52 41 43 45 33 28 22 54 52 55 4e 43 41 OSTRACE3("TRUNCA
fab30 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 TE %d %lld\n", p
fab40 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 29 3b File->h, nByte);
fab50 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
fab60 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 or(return SQLITE
fab70 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 29 _IOERR_TRUNCATE)
fab80 3b 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65 ;. rc = SetFile
fab90 50 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 Pointer(pFile->h
faba0 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 , lowerBits, &up
fabb0 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 perBits, FILE_BE
fabc0 47 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d GIN);. if( rc==
fabd0 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 INVALID_SET_FILE
fabe0 5f 50 4f 49 4e 54 45 52 20 26 26 20 28 65 72 72 _POINTER && (err
fabf0 6f 72 3d 47 65 74 4c 61 73 74 45 72 72 6f 72 28 or=GetLastError(
fac00 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a ))!=NO_ERROR ){.
fac10 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
fac20 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 rrno = error;.
fac30 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
fac40 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a IOERR_TRUNCATE;.
fac50 20 20 7d 0a 20 20 2f 2a 20 53 65 74 45 6e 64 4f }. /* SetEndO
fac60 66 46 69 6c 65 20 77 69 6c 6c 20 66 61 69 6c 20 fFile will fail
fac70 69 66 20 6e 42 79 74 65 20 69 73 20 6e 65 67 61 if nByte is nega
fac80 74 69 76 65 20 2a 2f 0a 20 20 69 66 28 20 21 53 tive */. if( !S
fac90 65 74 45 6e 64 4f 66 46 69 6c 65 28 70 46 69 6c etEndOfFile(pFil
faca0 65 2d 3e 68 29 20 29 7b 0a 20 20 20 20 70 46 69 e->h) ){. pFi
facb0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
facc0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a GetLastError();.
facd0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
face0 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 E_IOERR_TRUNCATE
facf0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
fad00 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 QLITE_OK;.}..#if
fad10 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
fad20 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20 /*.** Count the
fad30 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73 79 number of fullsy
fad40 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 73 ncs and normal s
fad50 79 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20 75 yncs. This is u
fad60 73 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 sed to test.** t
fad70 68 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66 75 hat syncs and fu
fad80 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63 75 llsyncs are occu
fad90 72 69 6e 67 20 61 74 20 74 68 65 20 72 69 67 68 ring at the righ
fada0 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49 t times..*/.SQLI
fadb0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
fadc0 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 e3_sync_count =
fadd0 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 0;.SQLITE_API in
fade0 74 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 t sqlite3_fullsy
fadf0 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 nc_count = 0;.#e
fae00 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 ndif../*.** Make
fae10 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 sure all writes
fae20 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 to a particular
fae30 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 file are commit
fae40 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f 0a ted to disk..*/.
fae50 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 79 static int winSy
fae60 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 nc(sqlite3_file
fae70 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b *id, int flags){
fae80 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
fae90 4e 4f 5f 53 59 4e 43 0a 20 20 77 69 6e 46 69 6c NO_SYNC. winFil
faea0 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 e *pFile = (winF
faeb0 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 ile*)id;.. asse
faec0 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f rt( id!=0 );. O
faed0 53 54 52 41 43 45 33 28 22 53 59 4e 43 20 25 64 STRACE3("SYNC %d
faee0 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 lock=%d\n", pFi
faef0 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f le->h, pFile->lo
faf00 63 6b 74 79 70 65 29 3b 0a 23 65 6c 73 65 0a 20 cktype);.#else.
faf10 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
faf20 52 28 69 64 29 3b 0a 23 65 6e 64 69 66 0a 23 69 R(id);.#endif.#i
faf30 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 fndef SQLITE_TES
faf40 54 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d T. UNUSED_PARAM
faf50 45 54 45 52 28 66 6c 61 67 73 29 3b 0a 23 65 6c ETER(flags);.#el
faf60 73 65 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 se. if( flags &
faf70 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c SQLITE_SYNC_FUL
faf80 4c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 L ){. sqlite3
faf90 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b _fullsync_count+
fafa0 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 +;. }. sqlite3
fafb0 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 _sync_count++;.#
fafc0 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20 77 65 endif. /* If we
fafd0 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 compiled with t
fafe0 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e he SQLITE_NO_SYN
faff0 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e C flag, then syn
fb000 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e cing is a. ** n
fb010 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 o-op. */.#ifdef
fb020 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a SQLITE_NO_SYNC.
fb030 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
fb040 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a 20 20 69 66 E_OK;.#else. if
fb050 28 20 46 6c 75 73 68 46 69 6c 65 42 75 66 66 65 ( FlushFileBuffe
fb060 72 73 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a rs(pFile->h) ){.
fb070 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
fb080 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 E_OK;. }else{.
fb090 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
fb0a0 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 rno = GetLastErr
fb0b0 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e or();. return
fb0c0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 SQLITE_IOERR;.
fb0d0 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a }.#endif.}../*.
fb0e0 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 ** Determine the
fb0f0 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 current size of
fb100 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 a file in bytes
fb110 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
fb120 69 6e 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 inFileSize(sqlit
fb130 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c e3_file *id, sql
fb140 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a ite3_int64 *pSiz
fb150 65 29 7b 0a 20 20 44 57 4f 52 44 20 75 70 70 65 e){. DWORD uppe
fb160 72 42 69 74 73 3b 0a 20 20 44 57 4f 52 44 20 6c rBits;. DWORD l
fb170 6f 77 65 72 42 69 74 73 3b 0a 20 20 77 69 6e 46 owerBits;. winF
fb180 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 ile *pFile = (wi
fb190 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f nFile*)id;. DWO
fb1a0 52 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73 73 RD error;.. ass
fb1b0 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 ert( id!=0 );.
fb1c0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
fb1d0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
fb1e0 45 52 52 5f 46 53 54 41 54 29 3b 0a 20 20 6c 6f ERR_FSTAT);. lo
fb1f0 77 65 72 42 69 74 73 20 3d 20 47 65 74 46 69 6c werBits = GetFil
fb200 65 53 69 7a 65 28 70 46 69 6c 65 2d 3e 68 2c 20 eSize(pFile->h,
fb210 26 75 70 70 65 72 42 69 74 73 29 3b 0a 20 20 69 &upperBits);. i
fb220 66 28 20 20 20 28 6c 6f 77 65 72 42 69 74 73 20 f( (lowerBits
fb230 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f == INVALID_FILE_
fb240 53 49 5a 45 29 0a 20 20 20 20 20 26 26 20 28 28 SIZE). && ((
fb250 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 error = GetLastE
fb260 72 72 6f 72 28 29 29 20 21 3d 20 4e 4f 5f 45 52 rror()) != NO_ER
fb270 52 4f 52 29 20 29 0a 20 20 7b 0a 20 20 20 20 70 ROR) ). {. p
fb280 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
fb290 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 = error;. ret
fb2a0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
fb2b0 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 20 20 2a 70 _FSTAT;. }. *p
fb2c0 53 69 7a 65 20 3d 20 28 28 28 73 71 6c 69 74 65 Size = (((sqlite
fb2d0 33 5f 69 6e 74 36 34 29 75 70 70 65 72 42 69 74 3_int64)upperBit
fb2e0 73 29 3c 3c 33 32 29 20 2b 20 6c 6f 77 65 72 42 s)<<32) + lowerB
fb2f0 69 74 73 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 its;. return SQ
fb300 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
fb310 2a 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f * LOCKFILE_FAIL_
fb320 49 4d 4d 45 44 49 41 54 45 4c 59 20 69 73 20 75 IMMEDIATELY is u
fb330 6e 64 65 66 69 6e 65 64 20 6f 6e 20 73 6f 6d 65 ndefined on some
fb340 20 57 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 Windows systems
fb350 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4c 4f 43 ..*/.#ifndef LOC
fb360 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 KFILE_FAIL_IMMED
fb370 49 41 54 45 4c 59 0a 23 20 64 65 66 69 6e 65 20 IATELY.# define
fb380 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d LOCKFILE_FAIL_IM
fb390 4d 45 44 49 41 54 45 4c 59 20 31 0a 23 65 6e 64 MEDIATELY 1.#end
fb3a0 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 if../*.** Acquir
fb3b0 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e e a reader lock.
fb3c0 0a 2a 2a 20 44 69 66 66 65 72 65 6e 74 20 41 50 .** Different AP
fb3d0 49 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 I routines are c
fb3e0 61 6c 6c 65 64 20 64 65 70 65 6e 64 69 6e 67 20 alled depending
fb3f0 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f on whether or no
fb400 74 20 74 68 69 73 0a 2a 2a 20 69 73 20 57 69 6e t this.** is Win
fb410 39 35 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a 2f 0a 95 or WinNT..*/.
fb420 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 52 65 static int getRe
fb430 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a adLock(winFile *
fb440 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65 pFile){. int re
fb450 73 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 s;. if( isNT()
fb460 29 7b 0a 20 20 20 20 4f 56 45 52 4c 41 50 50 45 ){. OVERLAPPE
fb470 44 20 6f 76 6c 70 3b 0a 20 20 20 20 6f 76 6c 70 D ovlp;. ovlp
fb480 2e 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 .Offset = SHARED
fb490 5f 46 49 52 53 54 3b 0a 20 20 20 20 6f 76 6c 70 _FIRST;. ovlp
fb4a0 2e 4f 66 66 73 65 74 48 69 67 68 20 3d 20 30 3b .OffsetHigh = 0;
fb4b0 0a 20 20 20 20 6f 76 6c 70 2e 68 45 76 65 6e 74 . ovlp.hEvent
fb4c0 20 3d 20 30 3b 0a 20 20 20 20 72 65 73 20 3d 20 = 0;. res =
fb4d0 4c 6f 63 6b 46 69 6c 65 45 78 28 70 46 69 6c 65 LockFileEx(pFile
fb4e0 2d 3e 68 2c 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 ->h, LOCKFILE_FA
fb4f0 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 2c 0a IL_IMMEDIATELY,.
fb500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fb510 20 20 20 20 20 30 2c 20 53 48 41 52 45 44 5f 53 0, SHARED_S
fb520 49 5a 45 2c 20 30 2c 20 26 6f 76 6c 70 29 3b 0a IZE, 0, &ovlp);.
fb530 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 /* isNT() is 1 i
fb540 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
fb550 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c E==1, so this el
fb560 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 se is never exec
fb570 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 uted. .*/.#if SQ
fb580 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 LITE_OS_WINCE==0
fb590 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e . }else{. in
fb5a0 74 20 6c 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 t lk;. sqlite
fb5b0 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 3_randomness(siz
fb5c0 65 6f 66 28 6c 6b 29 2c 20 26 6c 6b 29 3b 0a 20 eof(lk), &lk);.
fb5d0 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 pFile->shared
fb5e0 4c 6f 63 6b 42 79 74 65 20 3d 20 28 73 68 6f 72 LockByte = (shor
fb5f0 74 29 28 28 6c 6b 20 26 20 30 78 37 66 66 66 66 t)((lk & 0x7ffff
fb600 66 66 66 29 25 28 53 48 41 52 45 44 5f 53 49 5a fff)%(SHARED_SIZ
fb610 45 20 2d 20 31 29 29 3b 0a 20 20 20 20 72 65 73 E - 1));. res
fb620 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c = LockFile(pFil
fb630 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 e->h, SHARED_FIR
fb640 53 54 2b 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 ST+pFile->shared
fb650 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20 LockByte, 0, 1,
fb660 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 0);.#endif. }.
fb670 20 69 66 28 20 72 65 73 20 3d 3d 20 30 20 29 7b if( res == 0 ){
fb680 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 . pFile->last
fb690 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 Errno = GetLastE
fb6a0 72 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20 72 65 rror();. }. re
fb6b0 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a turn res;.}../*.
fb6c0 2a 2a 20 55 6e 64 6f 20 61 20 72 65 61 64 6c 6f ** Undo a readlo
fb6d0 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 ck.*/.static int
fb6e0 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 unlockReadLock(
fb6f0 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b winFile *pFile){
fb700 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 66 . int res;. if
fb710 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 ( isNT() ){.
fb720 72 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 res = UnlockFile
fb730 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 (pFile->h, SHARE
fb740 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52 D_FIRST, 0, SHAR
fb750 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 2f 2a 20 ED_SIZE, 0);./*
fb760 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 isNT() is 1 if S
fb770 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
fb780 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 1, so this else
fb790 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 is never execute
fb7a0 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 d. .*/.#if SQLIT
fb7b0 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 E_OS_WINCE==0.
fb7c0 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 73 20 3d }else{. res =
fb7d0 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c UnlockFile(pFil
fb7e0 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 e->h, SHARED_FIR
fb7f0 53 54 20 2b 20 70 46 69 6c 65 2d 3e 73 68 61 72 ST + pFile->shar
fb800 65 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 edLockByte, 0, 1
fb810 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d , 0);.#endif. }
fb820 0a 20 20 69 66 28 20 72 65 73 20 3d 3d 20 30 20 . if( res == 0
fb830 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 ){. pFile->la
fb840 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 stErrno = GetLas
fb850 74 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20 tError();. }.
fb860 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f return res;.}../
fb870 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 *.** Lock the fi
fb880 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b le with the lock
fb890 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 specified by pa
fb8a0 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 rameter locktype
fb8b0 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 - one.** of the
fb8c0 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a following:.**.*
fb8d0 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 * (1) SHARED
fb8e0 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 _LOCK.** (2)
fb8f0 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a RESERVED_LOCK.*
fb900 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e * (3) PENDIN
fb910 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 G_LOCK.** (4
fb920 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ) EXCLUSIVE_LOCK
fb930 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 .**.** Sometimes
fb940 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 when requesting
fb950 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c one lock state,
fb960 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b additional lock
fb970 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 states.** are i
fb980 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 nserted in betwe
fb990 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 en. The locking
fb9a0 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f might fail on o
fb9b0 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a ne of the later.
fb9c0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c ** transitions l
fb9d0 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 eaving the lock
fb9e0 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 state different
fb9f0 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 from what it sta
fba00 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c rted but.** stil
fba10 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 l short of its g
fba20 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 oal. The follow
fba30 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 ing chart shows
fba40 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 the allowed.** t
fba50 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 ransitions and t
fba60 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 he inserted inte
fba70 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a rmediate states:
fba80 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b .**.** UNLOCK
fba90 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 ED -> SHARED.**
fbaa0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 SHARED -> RES
fbab0 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 ERVED.** SHAR
fbac0 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 ED -> (PENDING)
fbad0 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 -> EXCLUSIVE.**
fbae0 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 RESERVED -> (
fbaf0 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c PENDING) -> EXCL
fbb00 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 USIVE.** PEND
fbb10 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 ING -> EXCLUSIVE
fbb20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
fbb30 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e ine will only in
fbb40 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 crease a lock.
fbb50 54 68 65 20 77 69 6e 55 6e 6c 6f 63 6b 28 29 20 The winUnlock()
fbb60 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73 65 routine.** erase
fbb70 73 20 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 6f s all locks at o
fbb80 6e 63 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20 nce and returns
fbb90 75 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 us immediately t
fbba0 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 o locking level
fbbb0 30 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 0..** It is not
fbbc0 70 6f 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77 65 possible to lowe
fbbd0 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 r the locking le
fbbe0 76 65 6c 20 6f 6e 65 20 73 74 65 70 20 61 74 20 vel one step at
fbbf0 61 20 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 a time. You.**
fbc00 6d 75 73 74 20 67 6f 20 73 74 72 61 69 67 68 74 must go straight
fbc10 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 to locking leve
fbc20 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 l 0..*/.static i
fbc30 6e 74 20 77 69 6e 4c 6f 63 6b 28 73 71 6c 69 74 nt winLock(sqlit
fbc40 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
fbc50 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e locktype){. in
fbc60 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
fbc70 3b 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 ; /* Return c
fbc80 6f 64 65 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 ode from subrout
fbc90 69 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 ines */. int re
fbca0 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 s = 1;
fbcb0 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 20 /* Result of a
fbcc0 77 69 6e 64 6f 77 73 20 6c 6f 63 6b 20 63 61 6c windows lock cal
fbcd0 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f l */. int newLo
fbce0 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a cktype; /*
fbcf0 20 53 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b Set pFile->lock
fbd00 74 79 70 65 20 74 6f 20 74 68 69 73 20 76 61 6c type to this val
fbd10 75 65 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e ue before exitin
fbd20 67 20 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 g */. int gotPe
fbd30 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a ndingLock = 0;/*
fbd40 20 54 72 75 65 20 69 66 20 77 65 20 61 63 71 75 True if we acqu
fbd50 69 72 65 64 20 61 20 50 45 4e 44 49 4e 47 20 6c ired a PENDING l
fbd60 6f 63 6b 20 74 68 69 73 20 74 69 6d 65 20 2a 2f ock this time */
fbd70 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c . winFile *pFil
fbd80 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 e = (winFile*)id
fbd90 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 20 ;. DWORD error
fbda0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 0a 20 20 61 = NO_ERROR;.. a
fbdb0 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a ssert( id!=0 );.
fbdc0 20 20 4f 53 54 52 41 43 45 35 28 22 4c 4f 43 4b OSTRACE5("LOCK
fbdd0 20 25 64 20 25 64 20 77 61 73 20 25 64 28 25 64 %d %d was %d(%d
fbde0 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 )\n",.
fbdf0 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 pFile->h, lockty
fbe00 70 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pe, pFile->lockt
fbe10 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72 ype, pFile->shar
fbe20 65 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 0a 20 20 edLockByte);..
fbe30 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 /* If there is a
fbe40 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 lready a lock of
fbe50 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f this type or mo
fbe60 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20 6f re restrictive o
fbe70 6e 20 74 68 65 0a 20 20 2a 2a 20 4f 73 46 69 6c n the. ** OsFil
fbe80 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 e, do nothing. D
fbe90 6f 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e 64 on't use the end
fbea0 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 _lock: exit path
fbeb0 2c 20 61 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 , as. ** sqlite
fbec0 33 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29 20 3OsEnterMutex()
fbed0 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c hasn't been call
fbee0 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 ed yet.. */. i
fbef0 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
fbf00 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a pe>=locktype ){.
fbf10 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
fbf20 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 E_OK;. }.. /*
fbf30 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f Make sure the lo
fbf40 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 cking sequence i
fbf50 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 s correct. */.
fbf60 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e assert( pFile->
fbf70 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 locktype!=NO_LOC
fbf80 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 K || locktype==S
fbf90 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
fbfa0 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
fbfb0 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 !=PENDING_LOCK )
fbfc0 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b ;. assert( lock
fbfd0 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c type!=RESERVED_L
fbfe0 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f OCK || pFile->lo
fbff0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
fc000 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 OCK );.. /* Loc
fc010 6b 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f k the PENDING_LO
fc020 43 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65 CK byte if we ne
fc030 65 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 ed to acquire a
fc040 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a PENDING lock or.
fc050 20 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f ** a SHARED lo
fc060 63 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 ck. If we are a
fc070 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 cquiring a SHARE
fc080 44 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 D lock, the acqu
fc090 69 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 isition of. **
fc0a0 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b the PENDING_LOCK
fc0b0 20 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61 byte is tempora
fc0c0 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f ry.. */. newLo
fc0d0 63 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e cktype = pFile->
fc0e0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 locktype;. if(
fc0f0 20 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 (pFile->lockty
fc100 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 29 0a 20 20 20 pe==NO_LOCK).
fc110 20 20 7c 7c 20 28 20 20 20 28 6c 6f 63 6b 74 79 || ( (lockty
fc120 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f pe==EXCLUSIVE_LO
fc130 43 4b 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 CK). &&
fc140 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 (pFile->locktype
fc150 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 ==RESERVED_LOCK)
fc160 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 ). ){. int c
fc170 6e 74 20 3d 20 33 3b 0a 20 20 20 20 77 68 69 6c nt = 3;. whil
fc180 65 28 20 63 6e 74 2d 2d 3e 30 20 26 26 20 28 72 e( cnt-->0 && (r
fc190 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 es = LockFile(pF
fc1a0 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f ile->h, PENDING_
fc1b0 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 29 3d BYTE, 0, 1, 0))=
fc1c0 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 =0 ){. /* T
fc1d0 72 79 20 33 20 74 69 6d 65 73 20 74 6f 20 67 65 ry 3 times to ge
fc1e0 74 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f t the pending lo
fc1f0 63 6b 2e 20 20 54 68 65 20 70 65 6e 64 69 6e 67 ck. The pending
fc200 20 6c 6f 63 6b 20 6d 69 67 68 74 20 62 65 0a 20 lock might be.
fc210 20 20 20 20 20 2a 2a 20 68 65 6c 64 20 62 79 20 ** held by
fc220 61 6e 6f 74 68 65 72 20 72 65 61 64 65 72 20 70 another reader p
fc230 72 6f 63 65 73 73 20 77 68 6f 20 77 69 6c 6c 20 rocess who will
fc240 72 65 6c 65 61 73 65 20 69 74 20 6d 6f 6d 65 6e release it momen
fc250 74 61 72 69 6c 79 2e 0a 20 20 20 20 20 20 2a 2f tarily.. */
fc260 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 . OSTRACE2(
fc270 22 63 6f 75 6c 64 20 6e 6f 74 20 67 65 74 20 61 "could not get a
fc280 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20 63 PENDING lock. c
fc290 6e 74 3d 25 64 5c 6e 22 2c 20 63 6e 74 29 3b 0a nt=%d\n", cnt);.
fc2a0 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b 0a Sleep(1);.
fc2b0 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 50 65 6e }. gotPen
fc2c0 64 69 6e 67 4c 6f 63 6b 20 3d 20 72 65 73 3b 0a dingLock = res;.
fc2d0 20 20 20 20 69 66 28 20 21 72 65 73 20 29 7b 0a if( !res ){.
fc2e0 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65 error = Ge
fc2f0 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 tLastError();.
fc300 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 }. }.. /* Ac
fc310 71 75 69 72 65 20 61 20 73 68 61 72 65 64 20 6c quire a shared l
fc320 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c ock. */. if( l
fc330 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
fc340 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 LOCK && res ){.
fc350 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 assert( pFile
fc360 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c ->locktype==NO_L
fc370 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d OCK );. res =
fc380 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 getReadLock(pFi
fc390 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 le);. if( res
fc3a0 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 ){. newLoc
fc3b0 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c ktype = SHARED_L
fc3c0 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a OCK;. }else{.
fc3d0 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65 error = Ge
fc3e0 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 tLastError();.
fc3f0 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 }. }.. /* Ac
fc400 71 75 69 72 65 20 61 20 52 45 53 45 52 56 45 44 quire a RESERVED
fc410 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 lock. */. if(
fc420 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 locktype==RESER
fc430 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 VED_LOCK && res
fc440 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
fc450 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d File->locktype==
fc460 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 SHARED_LOCK );.
fc470 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c res = LockFil
fc480 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 e(pFile->h, RESE
fc490 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c RVED_BYTE, 0, 1,
fc4a0 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 0);. if( res
fc4b0 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 ){. newLoc
fc4c0 6b 74 79 70 65 20 3d 20 52 45 53 45 52 56 45 44 ktype = RESERVED
fc4d0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 _LOCK;. }else
fc4e0 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 {. error =
fc4f0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a GetLastError();.
fc500 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
fc510 41 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e Acquire a PENDIN
fc520 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 G lock. */. if
fc530 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c ( locktype==EXCL
fc540 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 USIVE_LOCK && re
fc550 73 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f 63 6b s ){. newLock
fc560 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c type = PENDING_L
fc570 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 6e 64 OCK;. gotPend
fc580 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 7d ingLock = 0;. }
fc590 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 .. /* Acquire a
fc5a0 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b n EXCLUSIVE lock
fc5b0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b . */. if( lock
fc5c0 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f type==EXCLUSIVE_
fc5d0 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 LOCK && res ){.
fc5e0 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 assert( pFile
fc5f0 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 ->locktype>=SHAR
fc600 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 ED_LOCK );. r
fc610 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c es = unlockReadL
fc620 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 ock(pFile);.
fc630 4f 53 54 52 41 43 45 32 28 22 75 6e 72 65 61 64 OSTRACE2("unread
fc640 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 lock = %d\n", re
fc650 73 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f s);. res = Lo
fc660 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c ckFile(pFile->h,
fc670 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 SHARED_FIRST, 0
fc680 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 , SHARED_SIZE, 0
fc690 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 );. if( res )
fc6a0 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 {. newLockt
fc6b0 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f ype = EXCLUSIVE_
fc6c0 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b LOCK;. }else{
fc6d0 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 . error = G
fc6e0 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 etLastError();.
fc6f0 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 65 OSTRACE2("e
fc700 72 72 6f 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e rror-code = %d\n
fc710 22 2c 20 65 72 72 6f 72 29 3b 0a 20 20 20 20 20 ", error);.
fc720 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 getReadLock(pFi
fc730 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a le);. }. }..
fc740 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 68 /* If we are h
fc750 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e 47 olding a PENDING
fc760 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 74 lock that ought
fc770 20 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64 2c to be released,
fc780 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 then. ** relea
fc790 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a se it now.. */.
fc7a0 20 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e 67 if( gotPending
fc7b0 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65 Lock && locktype
fc7c0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b ==SHARED_LOCK ){
fc7d0 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 . UnlockFile(
fc7e0 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e pFile->h, PENDIN
fc7f0 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 G_BYTE, 0, 1, 0)
fc800 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 ;. }.. /* Upda
fc810 74 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 te the state of
fc820 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c the lock has hel
fc830 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 d in the file de
fc840 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 scriptor then.
fc850 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61 70 ** return the ap
fc860 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74 propriate result
fc870 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 code.. */. if
fc880 28 20 72 65 73 20 29 7b 0a 20 20 20 20 72 63 20 ( res ){. rc
fc890 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d = SQLITE_OK;. }
fc8a0 65 6c 73 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 else{. OSTRAC
fc8b0 45 34 28 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 E4("LOCK FAILED
fc8c0 25 64 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64 %d trying for %d
fc8d0 20 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 but got %d\n",
fc8e0 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 pFile->h,.
fc8f0 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e locktype, n
fc900 65 77 4c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 ewLocktype);.
fc910 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
fc920 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 o = error;. r
fc930 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b c = SQLITE_BUSY;
fc940 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f . }. pFile->lo
fc950 63 6b 74 79 70 65 20 3d 20 28 75 38 29 6e 65 77 cktype = (u8)new
fc960 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 Locktype;. retu
fc970 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
fc980 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 This routine che
fc990 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 cks if there is
fc9a0 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
fc9b0 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 held on the spec
fc9c0 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 ified.** file by
fc9d0 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 this or any oth
fc9e0 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 er process. If s
fc9f0 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 uch a lock is he
fca00 6c 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f ld, return.** no
fca10 6e 2d 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 n-zero, otherwis
fca20 65 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 e zero..*/.stati
fca30 63 20 69 6e 74 20 77 69 6e 43 68 65 63 6b 52 65 c int winCheckRe
fca40 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 servedLock(sqlit
fca50 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
fca60 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e *pResOut){. in
fca70 74 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 t rc;. winFile
fca80 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c *pFile = (winFil
fca90 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 e*)id;.. assert
fcaa0 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 69 66 28 ( id!=0 );. if(
fcab0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
fcac0 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 >=RESERVED_LOCK
fcad0 29 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 ){. rc = 1;.
fcae0 20 20 20 4f 53 54 52 41 43 45 33 28 22 54 45 53 OSTRACE3("TES
fcaf0 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 T WR-LOCK %d %d
fcb00 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c (local)\n", pFil
fcb10 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 65 6c e->h, rc);. }el
fcb20 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 4c 6f 63 se{. rc = Loc
fcb30 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 kFile(pFile->h,
fcb40 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 RESERVED_BYTE, 0
fcb50 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 , 1, 0);. if(
fcb60 20 72 63 20 29 7b 0a 20 20 20 20 20 20 55 6e 6c rc ){. Unl
fcb70 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 ockFile(pFile->h
fcb80 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c , RESERVED_BYTE,
fcb90 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0, 1, 0);. }
fcba0 0a 20 20 20 20 72 63 20 3d 20 21 72 63 3b 0a 20 . rc = !rc;.
fcbb0 20 20 20 4f 53 54 52 41 43 45 33 28 22 54 45 53 OSTRACE3("TES
fcbc0 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 T WR-LOCK %d %d
fcbd0 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69 (remote)\n", pFi
fcbe0 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 0a le->h, rc);. }.
fcbf0 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 3b *pResOut = rc;
fcc00 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
fcc10 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f _OK;.}../*.** Lo
fcc20 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 wer the locking
fcc30 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 level on file de
fcc40 73 63 72 69 70 74 6f 72 20 69 64 20 74 6f 20 6c scriptor id to l
fcc50 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 ocktype. lockty
fcc60 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 pe.** must be ei
fcc70 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 ther NO_LOCK or
fcc80 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a SHARED_LOCK..**.
fcc90 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e ** If the lockin
fcca0 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 g level of the f
fccb0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 ile descriptor i
fccc0 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 s already at or
fccd0 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 below.** the req
fcce0 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c uested locking l
fccf0 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 evel, this routi
fcd00 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a ne is a no-op..*
fcd10 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 *.** It is not p
fcd20 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 69 73 ossible for this
fcd30 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 61 69 6c routine to fail
fcd40 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 if the second a
fcd50 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f rgument.** is NO
fcd60 5f 4c 4f 43 4b 2e 20 20 49 66 20 74 68 65 20 73 _LOCK. If the s
fcd70 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 econd argument i
fcd80 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68 s SHARED_LOCK th
fcd90 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a en this routine.
fcda0 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 ** might return
fcdb0 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f SQLITE_IOERR;.*/
fcdc0 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 55 .static int winU
fcdd0 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 nlock(sqlite3_fi
fcde0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b le *id, int lock
fcdf0 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 74 79 70 type){. int typ
fce00 65 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 e;. winFile *pF
fce10 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 ile = (winFile*)
fce20 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 id;. int rc = S
fce30 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 QLITE_OK;. asse
fce40 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a rt( pFile!=0 );.
fce50 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 assert( lockty
fce60 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe<=SHARED_LOCK
fce70 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 );. OSTRACE5("U
fce80 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 77 NLOCK %d to %d w
fce90 61 73 20 25 64 28 25 64 29 5c 6e 22 2c 20 70 46 as %d(%d)\n", pF
fcea0 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 ile->h, locktype
fceb0 2c 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c ,. pFil
fcec0 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 e->locktype, pFi
fced0 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 le->sharedLockBy
fcee0 74 65 29 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 te);. type = pF
fcef0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 ile->locktype;.
fcf00 20 69 66 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 if( type>=EXCLU
fcf10 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 SIVE_LOCK ){.
fcf20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c UnlockFile(pFil
fcf30 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 e->h, SHARED_FIR
fcf40 53 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49 ST, 0, SHARED_SI
fcf50 5a 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 ZE, 0);. if(
fcf60 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
fcf70 5f 4c 4f 43 4b 20 26 26 20 21 67 65 74 52 65 61 _LOCK && !getRea
fcf80 64 4c 6f 63 6b 28 70 46 69 6c 65 29 20 29 7b 0a dLock(pFile) ){.
fcf90 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 68 /* This sh
fcfa0 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 ould never happe
fcfb0 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 61 6c n. We should al
fcfc0 77 61 79 73 20 62 65 20 61 62 6c 65 20 74 6f 0a ways be able to.
fcfd0 20 20 20 20 20 20 2a 2a 20 72 65 61 63 71 75 69 ** reacqui
fcfe0 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b re the read lock
fcff0 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 */. rc = S
fd000 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f QLITE_IOERR_UNLO
fd010 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 CK;. }. }.
fd020 69 66 28 20 74 79 70 65 3e 3d 52 45 53 45 52 56 if( type>=RESERV
fd030 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 ED_LOCK ){. U
fd040 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d nlockFile(pFile-
fd050 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 >h, RESERVED_BYT
fd060 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d E, 0, 1, 0);. }
fd070 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d . if( locktype=
fd080 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20 74 79 70 65 =NO_LOCK && type
fd090 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b >=SHARED_LOCK ){
fd0a0 0a 20 20 20 20 75 6e 6c 6f 63 6b 52 65 61 64 4c . unlockReadL
fd0b0 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 7d 0a ock(pFile);. }.
fd0c0 20 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e 44 if( type>=PEND
fd0d0 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 ING_LOCK ){.
fd0e0 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 UnlockFile(pFile
fd0f0 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 ->h, PENDING_BYT
fd100 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d E, 0, 1, 0);. }
fd110 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 . pFile->lockty
fd120 70 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70 pe = (u8)locktyp
fd130 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a e;. return rc;.
fd140 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c }../*.** Control
fd150 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74 68 and query of th
fd160 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 e open file hand
fd170 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
fd180 74 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c t winFileControl
fd190 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
fd1a0 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 d, int op, void
fd1b0 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 *pArg){. switch
fd1c0 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 ( op ){. case
fd1d0 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f SQLITE_FCNTL_LO
fd1e0 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 CKSTATE: {.
fd1f0 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 *(int*)pArg = (
fd200 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c (winFile*)id)->l
fd210 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 72 ocktype;. r
fd220 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
fd230 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
fd240 53 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e SQLITE_LAST_ERRN
fd250 4f 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 O: {. *(int
fd260 2a 29 70 41 72 67 20 3d 20 28 69 6e 74 29 28 28 *)pArg = (int)((
fd270 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 winFile*)id)->la
fd280 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 stErrno;. r
fd290 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
fd2a0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
fd2b0 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
fd2c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
fd2d0 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a n the sector siz
fd2e0 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 e in bytes of th
fd2f0 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f e underlying blo
fd300 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a ck device for.**
fd310 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66 the specified f
fd320 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d ile. This is alm
fd330 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 ost always 512 b
fd340 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 ytes, but may be
fd350 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 .** larger for s
fd360 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a ome devices..**.
fd370 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 ** SQLite code a
fd380 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 ssumes this func
fd390 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c tion cannot fail
fd3a0 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 . It also assume
fd3b0 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f s that.** if two
fd3c0 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 files are creat
fd3d0 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 ed in the same f
fd3e0 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 ile-system direc
fd3f0 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 tory (i.e..** a
fd400 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 database and its
fd410 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 journal file) t
fd420 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 hat the sector s
fd430 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a ize will be the.
fd440 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 ** same for both
fd450 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
fd460 77 69 6e 53 65 63 74 6f 72 53 69 7a 65 28 73 71 winSectorSize(sq
fd470 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b lite3_file *id){
fd480 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 . assert( id!=0
fd490 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e );. return (in
fd4a0 74 29 28 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 t)(((winFile*)id
fd4b0 29 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a )->sectorSize);.
fd4c0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
fd4d0 61 20 76 65 63 74 6f 72 20 6f 66 20 64 65 76 69 a vector of devi
fd4e0 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 ce characteristi
fd4f0 63 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e cs..*/.static in
fd500 74 20 77 69 6e 44 65 76 69 63 65 43 68 61 72 61 t winDeviceChara
fd510 63 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 cteristics(sqlit
fd520 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 e3_file *id){.
fd530 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
fd540 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 (id);. return 0
fd550 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
fd560 76 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20 61 vector defines a
fd570 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20 74 ll the methods t
fd580 68 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65 20 hat can operate
fd590 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 on an.** sqlite3
fd5a0 5f 66 69 6c 65 20 66 6f 72 20 77 69 6e 33 32 2e _file for win32.
fd5b0 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 .*/.static const
fd5c0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
fd5d0 6f 64 73 20 77 69 6e 49 6f 4d 65 74 68 6f 64 20 ods winIoMethod
fd5e0 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 = {. 1,
fd5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd600 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 /* iVersion */.
fd610 20 77 69 6e 43 6c 6f 73 65 2c 0a 20 20 77 69 6e winClose,. win
fd620 52 65 61 64 2c 0a 20 20 77 69 6e 57 72 69 74 65 Read,. winWrite
fd630 2c 0a 20 20 77 69 6e 54 72 75 6e 63 61 74 65 2c ,. winTruncate,
fd640 0a 20 20 77 69 6e 53 79 6e 63 2c 0a 20 20 77 69 . winSync,. wi
fd650 6e 46 69 6c 65 53 69 7a 65 2c 0a 20 20 77 69 6e nFileSize,. win
fd660 4c 6f 63 6b 2c 0a 20 20 77 69 6e 55 6e 6c 6f 63 Lock,. winUnloc
fd670 6b 2c 0a 20 20 77 69 6e 43 68 65 63 6b 52 65 73 k,. winCheckRes
fd680 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 77 69 6e ervedLock,. win
fd690 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 77 FileControl,. w
fd6a0 69 6e 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20 inSectorSize,.
fd6b0 77 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 74 winDeviceCharact
fd6c0 65 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a 2a eristics.};../**
fd6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd710 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 72 *********.** Her
fd720 65 20 65 6e 64 73 20 74 68 65 20 49 2f 4f 20 6d e ends the I/O m
fd730 65 74 68 6f 64 73 20 74 68 61 74 20 66 6f 72 6d ethods that form
fd740 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f the sqlite3_io_
fd750 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a methods object..
fd760 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 62 **.** The next b
fd770 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69 6d 70 lock of code imp
fd780 6c 65 6d 65 6e 74 73 20 74 68 65 20 56 46 53 20 lements the VFS
fd790 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a 2a methods..*******
fd7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd7e0 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f *****/../*.** Co
fd7f0 6e 76 65 72 74 20 61 20 55 54 46 2d 38 20 66 69 nvert a UTF-8 fi
fd800 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 77 68 61 74 lename into what
fd810 65 76 65 72 20 66 6f 72 6d 20 74 68 65 20 75 6e ever form the un
fd820 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 6f 70 65 72 derlying.** oper
fd830 61 74 69 6e 67 20 73 79 73 74 65 6d 20 77 61 6e ating system wan
fd840 74 73 20 66 69 6c 65 6e 61 6d 65 73 20 69 6e 2e ts filenames in.
fd850 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 Space to hold
fd860 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 the result.** is
fd870 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d obtained from m
fd880 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 62 alloc and must b
fd890 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63 e freed by the c
fd8a0 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 alling.** functi
fd8b0 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f on..*/.static vo
fd8c0 69 64 20 2a 63 6f 6e 76 65 72 74 55 74 66 38 46 id *convertUtf8F
fd8d0 69 6c 65 6e 61 6d 65 28 63 6f 6e 73 74 20 63 68 ilename(const ch
fd8e0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a ar *zFilename){.
fd8f0 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 void *zConvert
fd900 65 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 69 73 ed = 0;. if( is
fd910 4e 54 28 29 20 29 7b 0a 20 20 20 20 7a 43 6f 6e NT() ){. zCon
fd920 76 65 72 74 65 64 20 3d 20 75 74 66 38 54 6f 55 verted = utf8ToU
fd930 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 nicode(zFilename
fd940 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 );./* isNT() is
fd950 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 1 if SQLITE_OS_W
fd960 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 INCE==1, so this
fd970 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 else is never e
fd980 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 xecuted. .*/.#if
fd990 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
fd9a0 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ==0. }else{.
fd9b0 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 74 zConverted = ut
fd9c0 66 38 54 6f 4d 62 63 73 28 7a 46 69 6c 65 6e 61 f8ToMbcs(zFilena
fd9d0 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a me);.#endif. }.
fd9e0 20 20 2f 2a 20 63 61 6c 6c 65 72 20 77 69 6c 6c /* caller will
fd9f0 20 68 61 6e 64 6c 65 20 6f 75 74 20 6f 66 20 6d handle out of m
fda00 65 6d 6f 72 79 20 2a 2f 0a 20 20 72 65 74 75 72 emory */. retur
fda10 6e 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 7d 0a n zConverted;.}.
fda20 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 ./*.** Create a
fda30 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e temporary file n
fda40 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 ame in zBuf. zB
fda50 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 65 uf must be big e
fda60 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 nough to.** hold
fda70 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 at pVfs->mxPath
fda80 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 2e name characters.
fda90 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 .*/.static int g
fdaa0 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e etTempname(int n
fdab0 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 Buf, char *zBuf)
fdac0 7b 0a 20 20 73 74 61 74 69 63 20 63 68 61 72 20 {. static char
fdad0 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 zChars[] =. "
fdae0 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 abcdefghijklmnop
fdaf0 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 qrstuvwxyz".
fdb00 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f "ABCDEFGHIJKLMNO
fdb10 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 PQRSTUVWXYZ".
fdb20 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 "0123456789";.
fdb30 20 73 69 7a 65 5f 74 20 69 2c 20 6a 3b 0a 20 20 size_t i, j;.
fdb40 63 68 61 72 20 7a 54 65 6d 70 50 61 74 68 5b 4d char zTempPath[M
fdb50 41 58 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 66 AX_PATH+1];. if
fdb60 28 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 ( sqlite3_temp_d
fdb70 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 irectory ){.
fdb80 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
fdb90 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 (MAX_PATH-30, zT
fdba0 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 73 empPath, "%s", s
fdbb0 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 qlite3_temp_dire
fdbc0 63 74 6f 72 79 29 3b 0a 20 20 7d 65 6c 73 65 20 ctory);. }else
fdbd0 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 if( isNT() ){.
fdbe0 20 20 63 68 61 72 20 2a 7a 4d 75 6c 74 69 3b 0a char *zMulti;.
fdbf0 20 20 20 20 57 43 48 41 52 20 7a 57 69 64 65 50 WCHAR zWideP
fdc00 61 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 ath[MAX_PATH];.
fdc10 20 20 20 47 65 74 54 65 6d 70 50 61 74 68 57 28 GetTempPathW(
fdc20 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 57 69 MAX_PATH-30, zWi
fdc30 64 65 50 61 74 68 29 3b 0a 20 20 20 20 7a 4d 75 dePath);. zMu
fdc40 6c 74 69 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 lti = unicodeToU
fdc50 74 66 38 28 7a 57 69 64 65 50 61 74 68 29 3b 0a tf8(zWidePath);.
fdc60 20 20 20 20 69 66 28 20 7a 4d 75 6c 74 69 20 29 if( zMulti )
fdc70 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
fdc80 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 snprintf(MAX_PAT
fdc90 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c H-30, zTempPath,
fdca0 20 22 25 73 22 2c 20 7a 4d 75 6c 74 69 29 3b 0a "%s", zMulti);.
fdcb0 20 20 20 20 20 20 66 72 65 65 28 7a 4d 75 6c 74 free(zMult
fdcc0 69 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 i);. }else{.
fdcd0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
fdce0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
fdcf0 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 /* isNT() is 1 i
fdd00 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
fdd10 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c E==1, so this el
fdd20 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 se is never exec
fdd30 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 uted. .** Since
fdd40 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f the ASCII versio
fdd50 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f n of these Windo
fdd60 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 ws API do not ex
fdd70 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a ist for WINCE,.*
fdd80 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 * it's important
fdd90 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 to not referenc
fdda0 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 e them for WINCE
fddb0 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 builds..*/.#if
fddc0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
fddd0 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 =0. }else{.
fdde0 63 68 61 72 20 2a 7a 55 74 66 38 3b 0a 20 20 20 char *zUtf8;.
fddf0 20 63 68 61 72 20 7a 4d 62 63 73 50 61 74 68 5b char zMbcsPath[
fde00 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20 47 MAX_PATH];. G
fde10 65 74 54 65 6d 70 50 61 74 68 41 28 4d 41 58 5f etTempPathA(MAX_
fde20 50 41 54 48 2d 33 30 2c 20 7a 4d 62 63 73 50 61 PATH-30, zMbcsPa
fde30 74 68 29 3b 0a 20 20 20 20 7a 55 74 66 38 20 3d th);. zUtf8 =
fde40 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d sqlite3_win32_m
fde50 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 4d 62 63 bcs_to_utf8(zMbc
fde60 73 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20 sPath);. if(
fde70 7a 55 74 66 38 20 29 7b 0a 20 20 20 20 20 20 73 zUtf8 ){. s
fde80 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
fde90 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 MAX_PATH-30, zTe
fdea0 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a 55 mpPath, "%s", zU
fdeb0 74 66 38 29 3b 0a 20 20 20 20 20 20 66 72 65 65 tf8);. free
fdec0 28 7a 55 74 66 38 29 3b 0a 20 20 20 20 7d 65 6c (zUtf8);. }el
fded0 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e se{. return
fdee0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
fdef0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a }.#endif. }.
fdf00 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 33 53 for(i=sqlite3S
fdf10 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 50 61 74 trlen30(zTempPat
fdf20 68 29 3b 20 69 3e 30 20 26 26 20 7a 54 65 6d 70 h); i>0 && zTemp
fdf30 50 61 74 68 5b 69 2d 31 5d 3d 3d 27 5c 5c 27 3b Path[i-1]=='\\';
fdf40 20 69 2d 2d 29 7b 7d 0a 20 20 7a 54 65 6d 70 50 i--){}. zTempP
fdf50 61 74 68 5b 69 5d 20 3d 20 30 3b 0a 20 20 73 71 ath[i] = 0;. sq
fdf60 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
fdf70 42 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 Buf-30, zBuf,.
fdf80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fdf90 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 "%s\\"SQLITE_TE
fdfa0 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 MP_FILE_PREFIX,
fdfb0 7a 54 65 6d 70 50 61 74 68 29 3b 0a 20 20 6a 20 zTempPath);. j
fdfc0 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
fdfd0 30 28 7a 42 75 66 29 3b 0a 20 20 73 71 6c 69 74 0(zBuf);. sqlit
fdfe0 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 32 30 e3_randomness(20
fdff0 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 66 , &zBuf[j]);. f
fe000 6f 72 28 69 3d 30 3b 20 69 3c 32 30 3b 20 69 2b or(i=0; i<20; i+
fe010 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 42 75 +, j++){. zBu
fe020 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 f[j] = (char)zCh
fe030 61 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 ars[ ((unsigned
fe040 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 char)zBuf[j])%(s
fe050 69 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 izeof(zChars)-1)
fe060 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a ];. }. zBuf[j
fe070 5d 20 3d 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 ] = 0;. OSTRACE
fe080 32 28 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 2("TEMP FILENAME
fe090 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a : %s\n", zBuf);.
fe0a0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
fe0b0 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 OK; .}../*.** Th
fe0c0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f e return value o
fe0d0 66 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 f getLastErrorMs
fe0e0 67 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 20 g.** is zero if
fe0f0 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 the error messag
fe100 65 20 66 69 74 73 20 69 6e 20 74 68 65 20 62 75 e fits in the bu
fe110 66 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 ffer, or non-zer
fe120 6f 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 28 o.** otherwise (
fe130 69 66 20 74 68 65 20 6d 65 73 73 61 67 65 20 77 if the message w
fe140 61 73 20 74 72 75 6e 63 61 74 65 64 29 2e 0a 2a as truncated)..*
fe150 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 /.static int get
fe160 4c 61 73 74 45 72 72 6f 72 4d 73 67 28 69 6e 74 LastErrorMsg(int
fe170 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 nBuf, char *zBu
fe180 66 29 7b 0a 20 20 2f 2a 20 46 6f 72 6d 61 74 4d f){. /* FormatM
fe190 65 73 73 61 67 65 20 72 65 74 75 72 6e 73 20 30 essage returns 0
fe1a0 20 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20 4f 74 on failure. Ot
fe1b0 68 65 72 77 69 73 65 20 69 74 0a 20 20 2a 2a 20 herwise it. **
fe1c0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 returns the numb
fe1d0 65 72 20 6f 66 20 54 43 48 41 52 73 20 77 72 69 er of TCHARs wri
fe1e0 74 74 65 6e 20 74 6f 20 74 68 65 20 6f 75 74 70 tten to the outp
fe1f0 75 74 0a 20 20 2a 2a 20 62 75 66 66 65 72 2c 20 ut. ** buffer,
fe200 65 78 63 6c 75 64 69 6e 67 20 74 68 65 20 74 65 excluding the te
fe210 72 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 6c 20 63 rminating null c
fe220 68 61 72 2e 0a 20 20 2a 2f 0a 20 20 44 57 4f 52 har.. */. DWOR
fe230 44 20 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 D error = GetLas
fe240 74 45 72 72 6f 72 28 29 3b 0a 20 20 44 57 4f 52 tError();. DWOR
fe250 44 20 64 77 4c 65 6e 20 3d 20 30 3b 0a 20 20 63 D dwLen = 0;. c
fe260 68 61 72 20 2a 7a 4f 75 74 3b 0a 0a 20 20 69 66 har *zOut;.. if
fe270 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 ( isNT() ){.
fe280 57 43 48 41 52 20 2a 7a 54 65 6d 70 57 69 64 65 WCHAR *zTempWide
fe290 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 64 77 4c = NULL;. dwL
fe2a0 65 6e 20 3d 20 46 6f 72 6d 61 74 4d 65 73 73 61 en = FormatMessa
fe2b0 67 65 57 28 46 4f 52 4d 41 54 5f 4d 45 53 53 41 geW(FORMAT_MESSA
fe2c0 47 45 5f 41 4c 4c 4f 43 41 54 45 5f 42 55 46 46 GE_ALLOCATE_BUFF
fe2d0 45 52 20 7c 20 46 4f 52 4d 41 54 5f 4d 45 53 53 ER | FORMAT_MESS
fe2e0 41 47 45 5f 46 52 4f 4d 5f 53 59 53 54 45 4d 20 AGE_FROM_SYSTEM
fe2f0 7c 20 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 | FORMAT_MESSAGE
fe300 5f 49 47 4e 4f 52 45 5f 49 4e 53 45 52 54 53 2c _IGNORE_INSERTS,
fe310 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
fe320 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c NULL
fe330 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
fe340 20 20 20 20 20 20 20 20 20 20 20 20 20 65 72 72 err
fe350 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 or,.
fe360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
fe370 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
fe380 20 20 20 20 20 20 20 20 20 20 20 20 20 28 4c 50 (LP
fe390 57 53 54 52 29 20 26 7a 54 65 6d 70 57 69 64 65 WSTR) &zTempWide
fe3a0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
fe3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0a 0,.
fe3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe3d0 20 20 20 20 20 20 20 20 20 20 20 30 29 3b 0a 20 0);.
fe3e0 20 20 20 69 66 28 20 64 77 4c 65 6e 20 3e 20 30 if( dwLen > 0
fe3f0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 61 6c 6c ){. /* all
fe400 6f 63 61 74 65 20 61 20 62 75 66 66 65 72 20 61 ocate a buffer a
fe410 6e 64 20 63 6f 6e 76 65 72 74 20 74 6f 20 55 54 nd convert to UT
fe420 46 38 20 2a 2f 0a 20 20 20 20 20 20 7a 4f 75 74 F8 */. zOut
fe430 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 = unicodeToUtf8
fe440 28 7a 54 65 6d 70 57 69 64 65 29 3b 0a 20 20 20 (zTempWide);.
fe450 20 20 20 2f 2a 20 66 72 65 65 20 74 68 65 20 73 /* free the s
fe460 79 73 74 65 6d 20 62 75 66 66 65 72 20 61 6c 6c ystem buffer all
fe470 6f 63 61 74 65 64 20 62 79 20 46 6f 72 6d 61 74 ocated by Format
fe480 4d 65 73 73 61 67 65 20 2a 2f 0a 20 20 20 20 20 Message */.
fe490 20 4c 6f 63 61 6c 46 72 65 65 28 7a 54 65 6d 70 LocalFree(zTemp
fe4a0 57 69 64 65 29 3b 0a 20 20 20 20 7d 0a 2f 2a 20 Wide);. }./*
fe4b0 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 isNT() is 1 if S
fe4c0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
fe4d0 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 1, so this else
fe4e0 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 is never execute
fe4f0 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 d. .** Since the
fe500 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f ASCII version o
fe510 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 f these Windows
fe520 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 API do not exist
fe530 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 for WINCE,.** i
fe540 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f t's important to
fe550 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 not reference t
fe560 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 hem for WINCE bu
fe570 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c ilds..*/.#if SQL
fe580 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a ITE_OS_WINCE==0.
fe590 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 }else{. cha
fe5a0 72 20 2a 7a 54 65 6d 70 20 3d 20 4e 55 4c 4c 3b r *zTemp = NULL;
fe5b0 0a 20 20 20 20 64 77 4c 65 6e 20 3d 20 46 6f 72 . dwLen = For
fe5c0 6d 61 74 4d 65 73 73 61 67 65 41 28 46 4f 52 4d matMessageA(FORM
fe5d0 41 54 5f 4d 45 53 53 41 47 45 5f 41 4c 4c 4f 43 AT_MESSAGE_ALLOC
fe5e0 41 54 45 5f 42 55 46 46 45 52 20 7c 20 46 4f 52 ATE_BUFFER | FOR
fe5f0 4d 41 54 5f 4d 45 53 53 41 47 45 5f 46 52 4f 4d MAT_MESSAGE_FROM
fe600 5f 53 59 53 54 45 4d 20 7c 20 46 4f 52 4d 41 54 _SYSTEM | FORMAT
fe610 5f 4d 45 53 53 41 47 45 5f 49 47 4e 4f 52 45 5f _MESSAGE_IGNORE_
fe620 49 4e 53 45 52 54 53 2c 0a 20 20 20 20 20 20 20 INSERTS,.
fe630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe640 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 NULL,.
fe650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe660 20 20 20 20 20 65 72 72 6f 72 2c 0a 20 20 20 20 error,.
fe670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe680 20 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 0,.
fe690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe6a0 20 20 20 20 20 28 4c 50 53 54 52 29 20 26 7a 54 (LPSTR) &zT
fe6b0 65 6d 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 emp,.
fe6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe6d0 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 0,.
fe6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 29 0)
fe6f0 3b 0a 20 20 20 20 69 66 28 20 64 77 4c 65 6e 20 ;. if( dwLen
fe700 3e 20 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 > 0 ){. /*
fe710 61 6c 6c 6f 63 61 74 65 20 61 20 62 75 66 66 65 allocate a buffe
fe720 72 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 74 6f r and convert to
fe730 20 55 54 46 38 20 2a 2f 0a 20 20 20 20 20 20 7a UTF8 */. z
fe740 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 77 69 Out = sqlite3_wi
fe750 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 n32_mbcs_to_utf8
fe760 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 2f (zTemp);. /
fe770 2a 20 66 72 65 65 20 74 68 65 20 73 79 73 74 65 * free the syste
fe780 6d 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 m buffer allocat
fe790 65 64 20 62 79 20 46 6f 72 6d 61 74 4d 65 73 73 ed by FormatMess
fe7a0 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 4c 6f 63 age */. Loc
fe7b0 61 6c 46 72 65 65 28 7a 54 65 6d 70 29 3b 0a 20 alFree(zTemp);.
fe7c0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a }.#endif. }.
fe7d0 20 20 69 66 28 20 30 20 3d 3d 20 64 77 4c 65 6e if( 0 == dwLen
fe7e0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
fe7f0 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a snprintf(nBuf, z
fe800 42 75 66 2c 20 22 4f 73 45 72 72 6f 72 20 30 78 Buf, "OsError 0x
fe810 25 78 20 28 25 75 29 22 2c 20 65 72 72 6f 72 2c %x (%u)", error,
fe820 20 65 72 72 6f 72 29 3b 0a 20 20 7d 65 6c 73 65 error);. }else
fe830 7b 0a 20 20 20 20 2f 2a 20 63 6f 70 79 20 61 20 {. /* copy a
fe840 6d 61 78 69 6d 75 6d 20 6f 66 20 6e 42 75 66 20 maximum of nBuf
fe850 63 68 61 72 73 20 74 6f 20 6f 75 74 70 75 74 20 chars to output
fe860 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20 73 71 buffer */. sq
fe870 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
fe880 42 75 66 2c 20 7a 42 75 66 2c 20 22 25 73 22 2c Buf, zBuf, "%s",
fe890 20 7a 4f 75 74 29 3b 0a 20 20 20 20 2f 2a 20 66 zOut);. /* f
fe8a0 72 65 65 20 74 68 65 20 55 54 46 38 20 62 75 66 ree the UTF8 buf
fe8b0 66 65 72 20 2a 2f 0a 20 20 20 20 66 72 65 65 28 fer */. free(
fe8c0 7a 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 zOut);. }. ret
fe8d0 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
fe8e0 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a Open a file..*/.
fe8f0 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4f 70 static int winOp
fe900 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 en(. sqlite3_vf
fe910 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 s *pVfs,
fe920 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 /* Not used */.
fe930 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
fe940 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 me, /* Na
fe950 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 28 me of the file (
fe960 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 UTF-8) */. sqli
fe970 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 te3_file *id,
fe980 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 /* Write t
fe990 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68 he SQLite file h
fe9a0 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 andle here */.
fe9b0 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 int flags,
fe9c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 /* Ope
fe9d0 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a n mode flags */.
fe9e0 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 int *pOutFlags
fe9f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
fea00 74 61 74 75 73 20 72 65 74 75 72 6e 20 66 6c 61 tatus return fla
fea10 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 41 4e 44 4c gs */.){. HANDL
fea20 45 20 68 3b 0a 20 20 44 57 4f 52 44 20 64 77 44 E h;. DWORD dwD
fea30 65 73 69 72 65 64 41 63 63 65 73 73 3b 0a 20 20 esiredAccess;.
fea40 44 57 4f 52 44 20 64 77 53 68 61 72 65 4d 6f 64 DWORD dwShareMod
fea50 65 3b 0a 20 20 44 57 4f 52 44 20 64 77 43 72 65 e;. DWORD dwCre
fea60 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e ationDisposition
fea70 3b 0a 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67 ;. DWORD dwFlag
fea80 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d sAndAttributes =
fea90 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 0;.#if SQLITE_O
feaa0 53 5f 57 49 4e 43 45 0a 20 20 69 6e 74 20 69 73 S_WINCE. int is
feab0 54 65 6d 70 20 3d 20 30 3b 0a 23 65 6e 64 69 66 Temp = 0;.#endif
feac0 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c . winFile *pFil
fead0 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 e = (winFile*)id
feae0 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 ;. void *zConve
feaf0 72 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 rted;
feb00 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d /* Filenam
feb10 65 20 69 6e 20 4f 53 20 65 6e 63 6f 64 69 6e 67 e in OS encoding
feb20 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
feb30 20 2a 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 4e *zUtf8Name = zN
feb40 61 6d 65 3b 20 20 20 20 2f 2a 20 46 69 6c 65 6e ame; /* Filen
feb50 61 6d 65 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 ame in UTF-8 enc
feb60 6f 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 oding */. char
feb70 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 zTmpname[MAX_PAT
feb80 48 2b 31 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 H+1]; /*
feb90 42 75 66 66 65 72 20 75 73 65 64 20 74 6f 20 63 Buffer used to c
feba0 72 65 61 74 65 20 74 65 6d 70 20 66 69 6c 65 6e reate temp filen
febb0 61 6d 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ame */.. assert
febc0 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 55 4e 55 ( id!=0 );. UNU
febd0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 SED_PARAMETER(pV
febe0 66 73 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 fs);.. /* If th
febf0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
fec00 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 t to this functi
fec10 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 65 6e 65 on is NULL, gene
fec20 72 61 74 65 20 61 20 0a 20 20 2a 2a 20 74 65 6d rate a . ** tem
fec30 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 porary file name
fec40 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f 0a 20 20 to use . */.
fec50 69 66 28 20 21 7a 55 74 66 38 4e 61 6d 65 20 29 if( !zUtf8Name )
fec60 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 67 {. int rc = g
fec70 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 etTempname(MAX_P
fec80 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 ATH+1, zTmpname)
fec90 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
feca0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
fecb0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
fecc0 7d 0a 20 20 20 20 7a 55 74 66 38 4e 61 6d 65 20 }. zUtf8Name
fecd0 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a = zTmpname;. }.
fece0 0a 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 68 . /* Convert th
fecf0 65 20 66 69 6c 65 6e 61 6d 65 20 74 6f 20 74 68 e filename to th
fed00 65 20 73 79 73 74 65 6d 20 65 6e 63 6f 64 69 6e e system encodin
fed10 67 2e 20 2a 2f 0a 20 20 7a 43 6f 6e 76 65 72 74 g. */. zConvert
fed20 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 ed = convertUtf8
fed30 46 69 6c 65 6e 61 6d 65 28 7a 55 74 66 38 4e 61 Filename(zUtf8Na
fed40 6d 65 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 me);. if( zConv
fed50 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 erted==0 ){.
fed60 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
fed70 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 MEM;. }.. if(
fed80 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
fed90 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b PEN_READWRITE ){
feda0 0a 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 . dwDesiredAc
fedb0 63 65 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 cess = GENERIC_R
fedc0 45 41 44 20 7c 20 47 45 4e 45 52 49 43 5f 57 52 EAD | GENERIC_WR
fedd0 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ITE;. }else{.
fede0 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65 73 dwDesiredAcces
fedf0 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44 s = GENERIC_READ
fee00 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 51 4c 49 54 ;. }. /* SQLIT
fee10 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 E_OPEN_EXCLUSIVE
fee20 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 is used to make
fee30 20 73 75 72 65 20 74 68 61 74 20 61 20 6e 65 77 sure that a new
fee40 20 66 69 6c 65 20 69 73 20 0a 20 20 2a 2a 20 63 file is . ** c
fee50 72 65 61 74 65 64 2e 20 53 51 4c 69 74 65 20 64 reated. SQLite d
fee60 6f 65 73 6e 27 74 20 75 73 65 20 69 74 20 74 6f oesn't use it to
fee70 20 69 6e 64 69 63 61 74 65 20 22 65 78 63 6c 75 indicate "exclu
fee80 73 69 76 65 20 61 63 63 65 73 73 22 20 0a 20 20 sive access" .
fee90 2a 2a 20 61 73 20 69 74 20 69 73 20 75 73 75 61 ** as it is usua
feea0 6c 6c 79 20 75 6e 64 65 72 73 74 6f 6f 64 2e 0a lly understood..
feeb0 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 21 28 */. assert(!(
feec0 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
feed0 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 29 20 7c PEN_EXCLUSIVE) |
feee0 7c 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 | (flags & SQLIT
feef0 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 29 3b E_OPEN_CREATE));
fef00 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 . if( flags & S
fef10 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 QLITE_OPEN_EXCLU
fef20 53 49 56 45 20 29 7b 0a 20 20 20 20 2f 2a 20 43 SIVE ){. /* C
fef30 72 65 61 74 65 73 20 61 20 6e 65 77 20 66 69 6c reates a new fil
fef40 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74 20 64 6f e, only if it do
fef50 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 es not already e
fef60 78 69 73 74 2e 20 2a 2f 0a 20 20 20 20 2f 2a 20 xist. */. /*
fef70 49 66 20 74 68 65 20 66 69 6c 65 20 65 78 69 73 If the file exis
fef80 74 73 2c 20 69 74 20 66 61 69 6c 73 2e 20 2a 2f ts, it fails. */
fef90 0a 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 . dwCreationD
fefa0 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 43 52 45 isposition = CRE
fefb0 41 54 45 5f 4e 45 57 3b 0a 20 20 7d 65 6c 73 65 ATE_NEW;. }else
fefc0 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c if( flags & SQL
fefd0 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 ITE_OPEN_CREATE
fefe0 29 7b 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 65 ){. /* Open e
feff0 78 69 73 74 69 6e 67 20 66 69 6c 65 2c 20 6f 72 xisting file, or
ff000 20 63 72 65 61 74 65 20 69 66 20 69 74 20 64 6f create if it do
ff010 65 73 6e 27 74 20 65 78 69 73 74 20 2a 2f 0a 20 esn't exist */.
ff020 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 dwCreationDis
ff030 70 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f position = OPEN_
ff040 41 4c 57 41 59 53 3b 0a 20 20 7d 65 6c 73 65 7b ALWAYS;. }else{
ff050 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 73 20 61 20 . /* Opens a
ff060 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74 file, only if it
ff070 20 65 78 69 73 74 73 2e 20 2a 2f 0a 20 20 20 20 exists. */.
ff080 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 dwCreationDispos
ff090 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 45 58 49 ition = OPEN_EXI
ff0a0 53 54 49 4e 47 3b 0a 20 20 7d 0a 20 20 64 77 53 STING;. }. dwS
ff0b0 68 61 72 65 4d 6f 64 65 20 3d 20 46 49 4c 45 5f hareMode = FILE_
ff0c0 53 48 41 52 45 5f 52 45 41 44 20 7c 20 46 49 4c SHARE_READ | FIL
ff0d0 45 5f 53 48 41 52 45 5f 57 52 49 54 45 3b 0a 20 E_SHARE_WRITE;.
ff0e0 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c if( flags & SQL
ff0f0 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f ITE_OPEN_DELETEO
ff100 4e 43 4c 4f 53 45 20 29 7b 0a 23 69 66 20 53 51 NCLOSE ){.#if SQ
ff110 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 LITE_OS_WINCE.
ff120 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 dwFlagsAndAttr
ff130 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 ibutes = FILE_AT
ff140 54 52 49 42 55 54 45 5f 48 49 44 44 45 4e 3b 0a TRIBUTE_HIDDEN;.
ff150 20 20 20 20 69 73 54 65 6d 70 20 3d 20 31 3b 0a isTemp = 1;.
ff160 23 65 6c 73 65 0a 20 20 20 20 64 77 46 6c 61 67 #else. dwFlag
ff170 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d sAndAttributes =
ff180 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f FILE_ATTRIBUTE_
ff190 54 45 4d 50 4f 52 41 52 59 0a 20 20 20 20 20 20 TEMPORARY.
ff1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff1b0 20 20 20 20 20 20 20 20 20 7c 20 46 49 4c 45 5f | FILE_
ff1c0 41 54 54 52 49 42 55 54 45 5f 48 49 44 44 45 4e ATTRIBUTE_HIDDEN
ff1d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
ff1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff1f0 7c 20 46 49 4c 45 5f 46 4c 41 47 5f 44 45 4c 45 | FILE_FLAG_DELE
ff200 54 45 5f 4f 4e 5f 43 4c 4f 53 45 3b 0a 23 65 6e TE_ON_CLOSE;.#en
ff210 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 dif. }else{.
ff220 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 dwFlagsAndAttri
ff230 62 75 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 butes = FILE_ATT
ff240 52 49 42 55 54 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 RIBUTE_NORMAL;.
ff250 20 7d 0a 20 20 2f 2a 20 52 65 70 6f 72 74 73 20 }. /* Reports
ff260 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 65 from the interne
ff270 74 20 61 72 65 20 74 68 61 74 20 70 65 72 66 6f t are that perfo
ff280 72 6d 61 6e 63 65 20 69 73 20 61 6c 77 61 79 73 rmance is always
ff290 0a 20 20 2a 2a 20 62 65 74 74 65 72 20 69 66 20 . ** better if
ff2a0 46 49 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d FILE_FLAG_RANDOM
ff2b0 5f 41 43 43 45 53 53 20 69 73 20 75 73 65 64 2e _ACCESS is used.
ff2c0 20 20 54 69 63 6b 65 74 20 23 32 36 39 39 2e 20 Ticket #2699.
ff2d0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
ff2e0 5f 57 49 4e 43 45 0a 20 20 64 77 46 6c 61 67 73 _WINCE. dwFlags
ff2f0 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 7c 3d AndAttributes |=
ff300 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f FILE_FLAG_RANDO
ff310 4d 5f 41 43 43 45 53 53 3b 0a 23 65 6e 64 69 66 M_ACCESS;.#endif
ff320 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b . if( isNT() ){
ff330 0a 20 20 20 20 68 20 3d 20 43 72 65 61 74 65 46 . h = CreateF
ff340 69 6c 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f ileW((WCHAR*)zCo
ff350 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 nverted,.
ff360 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 2c dwDesiredAccess,
ff370 0a 20 20 20 20 20 20 20 64 77 53 68 61 72 65 4d . dwShareM
ff380 6f 64 65 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c ode,. NULL
ff390 2c 0a 20 20 20 20 20 20 20 64 77 43 72 65 61 74 ,. dwCreat
ff3a0 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 2c 0a ionDisposition,.
ff3b0 20 20 20 20 20 20 20 64 77 46 6c 61 67 73 41 6e dwFlagsAn
ff3c0 64 41 74 74 72 69 62 75 74 65 73 2c 0a 20 20 20 dAttributes,.
ff3d0 20 20 20 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a NULL. );.
ff3e0 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 /* isNT() is 1 i
ff3f0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
ff400 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c E==1, so this el
ff410 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 se is never exec
ff420 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 uted. .** Since
ff430 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f the ASCII versio
ff440 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f n of these Windo
ff450 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 ws API do not ex
ff460 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a ist for WINCE,.*
ff470 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 * it's important
ff480 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 to not referenc
ff490 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 e them for WINCE
ff4a0 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 builds..*/.#if
ff4b0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
ff4c0 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 =0. }else{.
ff4d0 68 20 3d 20 43 72 65 61 74 65 46 69 6c 65 41 28 h = CreateFileA(
ff4e0 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 (char*)zConverte
ff4f0 64 2c 0a 20 20 20 20 20 20 20 64 77 44 65 73 69 d,. dwDesi
ff500 72 65 64 41 63 63 65 73 73 2c 0a 20 20 20 20 20 redAccess,.
ff510 20 20 64 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20 dwShareMode,.
ff520 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 NULL,.
ff530 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 dwCreationDis
ff540 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 20 20 20 position,.
ff550 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 dwFlagsAndAttri
ff560 62 75 74 65 73 2c 0a 20 20 20 20 20 20 20 4e 55 butes,. NU
ff570 4c 4c 0a 20 20 20 20 29 3b 0a 23 65 6e 64 69 66 LL. );.#endif
ff580 0a 20 20 7d 0a 20 20 69 66 28 20 68 3d 3d 49 4e . }. if( h==IN
ff590 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c VALID_HANDLE_VAL
ff5a0 55 45 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a UE ){. free(z
ff5b0 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 Converted);.
ff5c0 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 if( flags & SQLI
ff5d0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
ff5e0 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 E ){. retur
ff5f0 6e 20 77 69 6e 4f 70 65 6e 28 70 56 66 73 2c 20 n winOpen(pVfs,
ff600 7a 4e 61 6d 65 2c 20 69 64 2c 20 0a 20 20 20 20 zName, id, .
ff610 20 20 20 20 20 20 20 20 20 28 28 66 6c 61 67 73 ((flags
ff620 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 |SQLITE_OPEN_REA
ff630 44 4f 4e 4c 59 29 26 7e 53 51 4c 49 54 45 5f 4f DONLY)&~SQLITE_O
ff640 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 2c 20 PEN_READWRITE),
ff650 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20 pOutFlags);.
ff660 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 }else{. ret
ff670 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f urn SQLITE_CANTO
ff680 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 PEN;. }. }.
ff690 20 69 66 28 20 70 4f 75 74 46 6c 61 67 73 20 29 if( pOutFlags )
ff6a0 7b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 {. if( flags
ff6b0 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 & SQLITE_OPEN_RE
ff6c0 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 ADWRITE ){.
ff6d0 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 53 51 *pOutFlags = SQ
ff6e0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
ff6f0 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ITE;. }else{.
ff700 20 20 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 *pOutFlags
ff710 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 = SQLITE_OPEN_R
ff720 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 EADONLY;. }.
ff730 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 46 69 6c }. memset(pFil
ff740 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 e, 0, sizeof(*pF
ff750 69 6c 65 29 29 3b 0a 20 20 70 46 69 6c 65 2d 3e ile));. pFile->
ff760 70 4d 65 74 68 6f 64 20 3d 20 26 77 69 6e 49 6f pMethod = &winIo
ff770 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69 6c 65 2d Method;. pFile-
ff780 3e 68 20 3d 20 68 3b 0a 20 20 70 46 69 6c 65 2d >h = h;. pFile-
ff790 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 4e 4f 5f >lastErrno = NO_
ff7a0 45 52 52 4f 52 3b 0a 20 20 70 46 69 6c 65 2d 3e ERROR;. pFile->
ff7b0 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 67 65 74 sectorSize = get
ff7c0 53 65 63 74 6f 72 53 69 7a 65 28 70 56 66 73 2c SectorSize(pVfs,
ff7d0 20 7a 55 74 66 38 4e 61 6d 65 29 3b 0a 23 69 66 zUtf8Name);.#if
ff7e0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
ff7f0 0a 20 20 69 66 28 20 28 66 6c 61 67 73 20 26 20 . if( (flags &
ff800 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 (SQLITE_OPEN_REA
ff810 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 DWRITE|SQLITE_OP
ff820 45 4e 5f 4d 41 49 4e 5f 44 42 29 29 20 3d 3d 0a EN_MAIN_DB)) ==.
ff830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
ff840 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
ff850 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 WRITE|SQLITE_OPE
ff860 4e 5f 4d 41 49 4e 5f 44 42 29 0a 20 20 20 20 20 N_MAIN_DB).
ff870 20 20 26 26 20 21 77 69 6e 63 65 43 72 65 61 74 && !winceCreat
ff880 65 4c 6f 63 6b 28 7a 4e 61 6d 65 2c 20 70 46 69 eLock(zName, pFi
ff890 6c 65 29 0a 20 20 29 7b 0a 20 20 20 20 43 6c 6f le). ){. Clo
ff8a0 73 65 48 61 6e 64 6c 65 28 68 29 3b 0a 20 20 20 seHandle(h);.
ff8b0 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 free(zConverted
ff8c0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
ff8d0 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 LITE_CANTOPEN;.
ff8e0 20 7d 0a 20 20 69 66 28 20 69 73 54 65 6d 70 20 }. if( isTemp
ff8f0 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 7a 44 ){. pFile->zD
ff900 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 3d 20 7a eleteOnClose = z
ff910 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 7d 65 6c Converted;. }el
ff920 73 65 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 se.#endif. {.
ff930 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 free(zConverte
ff940 64 29 3b 0a 20 20 7d 0a 20 20 4f 70 65 6e 43 6f d);. }. OpenCo
ff950 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 72 65 74 unter(+1);. ret
ff960 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
ff970 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 ../*.** Delete t
ff980 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a he named file..*
ff990 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 *.** Note that w
ff9a0 69 6e 64 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 indows does not
ff9b0 61 6c 6c 6f 77 20 61 20 66 69 6c 65 20 74 6f 20 allow a file to
ff9c0 62 65 20 64 65 6c 65 74 65 64 20 69 66 20 73 6f be deleted if so
ff9d0 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 72 6f 63 me other.** proc
ff9e0 65 73 73 20 68 61 73 20 69 74 20 6f 70 65 6e 2e ess has it open.
ff9f0 20 20 53 6f 6d 65 74 69 6d 65 73 20 61 20 76 69 Sometimes a vi
ffa00 72 75 73 20 73 63 61 6e 6e 65 72 20 6f 72 20 69 rus scanner or i
ffa10 6e 64 65 78 69 6e 67 20 70 72 6f 67 72 61 6d 0a ndexing program.
ffa20 2a 2a 20 77 69 6c 6c 20 6f 70 65 6e 20 61 20 6a ** will open a j
ffa30 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 68 6f 72 ournal file shor
ffa40 74 6c 79 20 61 66 74 65 72 20 69 74 20 69 73 20 tly after it is
ffa50 63 72 65 61 74 65 64 20 69 6e 20 6f 72 64 65 72 created in order
ffa60 20 74 6f 20 64 6f 0a 2a 2a 20 77 68 61 74 65 76 to do.** whatev
ffa70 65 72 20 69 74 20 64 6f 65 73 2e 20 20 57 68 69 er it does. Whi
ffa80 6c 65 20 74 68 69 73 20 6f 74 68 65 72 20 70 72 le this other pr
ffa90 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 ocess is holding
ffaa0 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 the.** file ope
ffab0 6e 2c 20 77 65 20 77 69 6c 6c 20 62 65 20 75 6e n, we will be un
ffac0 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 69 able to delete i
ffad0 74 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 t. To work arou
ffae0 6e 64 20 74 68 69 73 0a 2a 2a 20 70 72 6f 62 6c nd this.** probl
ffaf0 65 6d 2c 20 77 65 20 64 65 6c 61 79 20 31 30 30 em, we delay 100
ffb00 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e milliseconds an
ffb10 64 20 74 72 79 20 74 6f 20 64 65 6c 65 74 65 20 d try to delete
ffb20 61 67 61 69 6e 2e 20 20 55 70 0a 2a 2a 20 74 6f again. Up.** to
ffb30 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 MX_DELETION_ATT
ffb40 45 4d 50 54 73 20 64 65 6c 65 74 69 6f 6e 20 61 EMPTs deletion a
ffb50 74 74 65 6d 70 74 73 20 61 72 65 20 72 75 6e 20 ttempts are run
ffb60 62 65 66 6f 72 65 20 67 69 76 69 6e 67 0a 2a 2a before giving.**
ffb70 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e up and returnin
ffb80 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 g an error..*/.#
ffb90 64 65 66 69 6e 65 20 4d 58 5f 44 45 4c 45 54 49 define MX_DELETI
ffba0 4f 4e 5f 41 54 54 45 4d 50 54 53 20 35 0a 73 74 ON_ATTEMPTS 5.st
ffbb0 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 6c 65 atic int winDele
ffbc0 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 te(. sqlite3_vf
ffbd0 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 s *pVfs,
ffbe0 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e /* Not used on
ffbf0 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e 73 win32 */. cons
ffc00 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
ffc10 65 2c 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 e, /* Name
ffc20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 of file to delet
ffc30 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79 6e 63 44 e */. int syncD
ffc40 69 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ir
ffc50 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f /* Not used o
ffc60 6e 20 77 69 6e 33 32 20 2a 2f 0a 29 7b 0a 20 20 n win32 */.){.
ffc70 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 44 int cnt = 0;. D
ffc80 57 4f 52 44 20 72 63 3b 0a 20 20 44 57 4f 52 44 WORD rc;. DWORD
ffc90 20 65 72 72 6f 72 20 3d 20 30 3b 0a 20 20 76 6f error = 0;. vo
ffca0 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d id *zConverted =
ffcb0 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 convertUtf8File
ffcc0 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b name(zFilename);
ffcd0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
ffce0 54 45 52 28 70 56 66 73 29 3b 0a 20 20 55 4e 55 TER(pVfs);. UNU
ffcf0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 73 79 SED_PARAMETER(sy
ffd00 6e 63 44 69 72 29 3b 0a 20 20 69 66 28 20 7a 43 ncDir);. if( zC
ffd10 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 onverted==0 ){.
ffd20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
ffd30 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 53 69 _NOMEM;. }. Si
ffd40 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 mulateIOError(re
ffd50 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
ffd60 52 5f 44 45 4c 45 54 45 29 3b 0a 20 20 69 66 28 R_DELETE);. if(
ffd70 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 64 isNT() ){. d
ffd80 6f 7b 0a 20 20 20 20 20 20 44 65 6c 65 74 65 46 o{. DeleteF
ffd90 69 6c 65 57 28 7a 43 6f 6e 76 65 72 74 65 64 29 ileW(zConverted)
ffda0 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 20 20 ;. }while(
ffdb0 28 20 20 20 28 28 72 63 20 3d 20 47 65 74 46 69 ( ((rc = GetFi
ffdc0 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 7a 43 leAttributesW(zC
ffdd0 6f 6e 76 65 72 74 65 64 29 29 20 21 3d 20 49 4e onverted)) != IN
ffde0 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 VALID_FILE_ATTRI
ffdf0 42 55 54 45 53 29 0a 20 20 20 20 20 20 20 20 20 BUTES).
ffe00 20 20 20 20 20 20 7c 7c 20 28 28 65 72 72 6f 72 || ((error
ffe10 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 = GetLastError(
ffe20 29 29 20 3d 3d 20 45 52 52 4f 52 5f 41 43 43 45 )) == ERROR_ACCE
ffe30 53 53 5f 44 45 4e 49 45 44 29 29 0a 20 20 20 20 SS_DENIED)).
ffe40 20 20 20 20 20 20 20 26 26 20 28 2b 2b 63 6e 74 && (++cnt
ffe50 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 < MX_DELETION_A
ffe60 54 54 45 4d 50 54 53 29 0a 20 20 20 20 20 20 20 TTEMPTS).
ffe70 20 20 20 20 26 26 20 28 53 6c 65 65 70 28 31 30 && (Sleep(10
ffe80 30 29 2c 20 31 29 20 29 3b 0a 2f 2a 20 69 73 4e 0), 1) );./* isN
ffe90 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 T() is 1 if SQLI
ffea0 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 TE_OS_WINCE==1,
ffeb0 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 so this else is
ffec0 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 never executed.
ffed0 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 .** Since the AS
ffee0 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 CII version of t
ffef0 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 hese Windows API
fff00 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f do not exist fo
fff10 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 r WINCE,.** it's
fff20 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f important to no
fff30 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d t reference them
fff40 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 for WINCE build
fff50 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 s..*/.#if SQLITE
fff60 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d _OS_WINCE==0. }
fff70 65 6c 73 65 7b 0a 20 20 20 20 64 6f 7b 0a 20 20 else{. do{.
fff80 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65 41 28 DeleteFileA(
fff90 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 zConverted);.
fffa0 20 7d 77 68 69 6c 65 28 20 20 20 28 20 20 20 28 }while( ( (
fffb0 28 72 63 20 3d 20 47 65 74 46 69 6c 65 41 74 74 (rc = GetFileAtt
fffc0 72 69 62 75 74 65 73 41 28 7a 43 6f 6e 76 65 72 ributesA(zConver
fffd0 74 65 64 29 29 20 21 3d 20 49 4e 56 41 4c 49 44 ted)) != INVALID
fffe0 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 _FILE_ATTRIBUTES
ffff0 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ).
10000 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d 20 47 65 || ((error = Ge
10001 74 4c 61 73 74 45 72 72 6f 72 28 29 29 20 3d 3d tLastError()) ==
10002 20 45 52 52 4f 52 5f 41 43 43 45 53 53 5f 44 45 ERROR_ACCESS_DE
10003 4e 49 45 44 29 29 0a 20 20 20 20 20 20 20 20 20 NIED)).
10004 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c 20 4d 58 && (++cnt < MX
10005 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 _DELETION_ATTEMP
10006 54 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 TS). &
10007 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31 & (Sleep(100), 1
10008 29 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a ) );.#endif. }.
10009 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 free(zConverte
1000a 64 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 d);. OSTRACE2("
1000b 44 45 4c 45 54 45 20 5c 22 25 73 5c 22 5c 6e 22 DELETE \"%s\"\n"
1000c 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 , zFilename);.
1000d 72 65 74 75 72 6e 20 28 20 20 20 28 72 63 20 3d return ( (rc =
1000e 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 = INVALID_FILE_A
1000f 54 54 52 49 42 55 54 45 53 29 20 0a 20 20 20 20 TTRIBUTES) .
10010 20 20 20 20 20 20 26 26 20 28 65 72 72 6f 72 20 && (error
10011 3d 3d 20 45 52 52 4f 52 5f 46 49 4c 45 5f 4e 4f == ERROR_FILE_NO
10012 54 5f 46 4f 55 4e 44 29 29 20 3f 20 53 51 4c 49 T_FOUND)) ? SQLI
10013 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 TE_OK : SQLITE_I
10014 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a 0a OERR_DELETE;.}..
10015 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 /*.** Check the
10016 65 78 69 73 74 61 6e 63 65 20 61 6e 64 20 73 74 existance and st
10017 61 74 75 73 20 6f 66 20 61 20 66 69 6c 65 2e 0a atus of a file..
10018 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 */.static int wi
10019 6e 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 nAccess(. sqlit
1001a 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 e3_vfs *pVfs,
1001b 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 /* Not use
1001c 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 d on win32 */.
1001d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
1001e 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 ename, /* Na
1001f 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 63 68 me of file to ch
10020 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 eck */. int fla
10021 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 gs,
10022 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 /* Type of t
10023 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 74 est to make on t
10024 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e his file */. in
10025 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20 20 20 t *pResOut
10026 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a /* OUT:
10027 20 52 65 73 75 6c 74 20 2a 2f 0a 29 7b 0a 20 20 Result */.){.
10028 44 57 4f 52 44 20 61 74 74 72 3b 0a 20 20 69 6e DWORD attr;. in
10029 74 20 72 63 20 3d 20 30 3b 0a 20 20 76 6f 69 64 t rc = 0;. void
1002a 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 *zConverted = c
1002b 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 onvertUtf8Filena
1002c 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 me(zFilename);.
1002d 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
1002e 52 28 70 56 66 73 29 3b 0a 20 20 69 66 28 20 7a R(pVfs);. if( z
1002f 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a Converted==0 ){.
10030 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
10031 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 E_NOMEM;. }. i
10032 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 f( isNT() ){.
10033 20 61 74 74 72 20 3d 20 47 65 74 46 69 6c 65 41 attr = GetFileA
10034 74 74 72 69 62 75 74 65 73 57 28 28 57 43 48 41 ttributesW((WCHA
10035 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a R*)zConverted);.
10036 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 /* isNT() is 1 i
10037 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
10038 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c E==1, so this el
10039 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 se is never exec
1003a 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 uted. .** Since
1003b 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f the ASCII versio
1003c 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f n of these Windo
1003d 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 ws API do not ex
1003e 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a ist for WINCE,.*
1003f 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 * it's important
10040 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 to not referenc
10041 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 e them for WINCE
10042 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 builds..*/.#if
10043 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
10044 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 =0. }else{.
10045 61 74 74 72 20 3d 20 47 65 74 46 69 6c 65 41 74 attr = GetFileAt
10046 74 72 69 62 75 74 65 73 41 28 28 63 68 61 72 2a tributesA((char*
10047 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65 )zConverted);.#e
10048 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 ndif. }. free(
10049 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 73 zConverted);. s
1004a 77 69 74 63 68 28 20 66 6c 61 67 73 20 29 7b 0a witch( flags ){.
1004b 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
1004c 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 20 ACCESS_READ:.
1004d 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 case SQLITE_ACC
1004e 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 ESS_EXISTS:.
1004f 20 20 72 63 20 3d 20 61 74 74 72 21 3d 49 4e 56 rc = attr!=INV
10050 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 ALID_FILE_ATTRIB
10051 55 54 45 53 3b 0a 20 20 20 20 20 20 62 72 65 61 UTES;. brea
10052 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 k;. case SQLI
10053 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 TE_ACCESS_READWR
10054 49 54 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 ITE:. rc =
10055 28 61 74 74 72 20 26 20 46 49 4c 45 5f 41 54 54 (attr & FILE_ATT
10056 52 49 42 55 54 45 5f 52 45 41 44 4f 4e 4c 59 29 RIBUTE_READONLY)
10057 3d 3d 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b ==0;. break
10058 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 ;. default:.
10059 20 20 20 20 20 61 73 73 65 72 74 28 21 22 49 6e assert(!"In
1005a 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 valid flags argu
1005b 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 ment");. }. *p
1005c 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 ResOut = rc;. r
1005d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1005e 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 .}.../*.** Turn
1005f 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e a relative pathn
10060 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 ame into a full
10061 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69 74 65 pathname. Write
10062 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 74 the full.** pat
10063 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 4f 75 74 5b hname into zOut[
10064 5d 2e 20 20 7a 4f 75 74 5b 5d 20 77 69 6c 6c 20 ]. zOut[] will
10065 62 65 20 61 74 20 6c 65 61 73 74 20 70 56 66 73 be at least pVfs
10066 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 2a 2a 20 ->mxPathname.**
10067 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a bytes in size..*
10068 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e /.static int win
10069 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 FullPathname(.
1006a 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
1006b 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a s, /*
1006c 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 Pointer to vfs
1006d 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 object */. cons
1006e 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 t char *zRelativ
1006f 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 e, /* Pos
10070 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 sibly relative i
10071 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 nput path */. i
10072 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 nt nFull,
10073 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10074 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 Size of output b
10075 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a uffer in bytes *
10076 2f 0a 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 /. char *zFull
10077 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10078 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 /* Output buff
10079 65 72 20 2a 2f 0a 29 7b 0a 20 20 0a 23 69 66 20 er */.){. .#if
1007a 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e defined(__CYGWIN
1007b 5f 5f 29 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 __). UNUSED_PAR
1007c 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 AMETER(nFull);.
1007d 20 63 79 67 77 69 6e 5f 63 6f 6e 76 5f 74 6f 5f cygwin_conv_to_
1007e 66 75 6c 6c 5f 77 69 6e 33 32 5f 70 61 74 68 28 full_win32_path(
1007f 7a 52 65 6c 61 74 69 76 65 2c 20 7a 46 75 6c 6c zRelative, zFull
10080 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
10081 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 TE_OK;.#endif..#
10082 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
10083 43 45 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 CE. UNUSED_PARA
10084 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 METER(nFull);.
10085 2f 2a 20 57 69 6e 43 45 20 68 61 73 20 6e 6f 20 /* WinCE has no
10086 63 6f 6e 63 65 70 74 20 6f 66 20 61 20 72 65 6c concept of a rel
10087 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 2c 20 ative pathname,
10088 6f 72 20 73 6f 20 49 20 61 6d 20 74 6f 6c 64 2e or so I am told.
10089 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e */. sqlite3_sn
1008a 70 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 printf(pVfs->mxP
1008b 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 athname, zFull,
1008c 22 25 73 22 2c 20 7a 52 65 6c 61 74 69 76 65 29 "%s", zRelative)
1008d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
1008e 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 E_OK;.#endif..#i
1008f 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e f !SQLITE_OS_WIN
10090 43 45 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f CE && !defined(_
10091 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 69 6e 74 _CYGWIN__). int
10092 20 6e 42 79 74 65 3b 0a 20 20 76 6f 69 64 20 2a nByte;. void *
10093 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 63 68 zConverted;. ch
10094 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 55 4e 55 53 ar *zOut;. UNUS
10095 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 46 75 ED_PARAMETER(nFu
10096 6c 6c 29 3b 0a 20 20 7a 43 6f 6e 76 65 72 74 65 ll);. zConverte
10097 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 d = convertUtf8F
10098 69 6c 65 6e 61 6d 65 28 7a 52 65 6c 61 74 69 76 ilename(zRelativ
10099 65 29 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 e);. if( isNT()
1009a 20 29 7b 0a 20 20 20 20 57 43 48 41 52 20 2a 7a ){. WCHAR *z
1009b 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 Temp;. nByte
1009c 3d 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d = GetFullPathNam
1009d 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 eW((WCHAR*)zConv
1009e 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 30 29 20 erted, 0, 0, 0)
1009f 2b 20 33 3b 0a 20 20 20 20 7a 54 65 6d 70 20 3d + 3;. zTemp =
100a0 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73 malloc( nByte*s
100a1 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 izeof(zTemp[0])
100a2 29 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 );. if( zTemp
100a3 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 72 65 ==0 ){. fre
100a4 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 e(zConverted);.
100a5 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
100a6 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
100a7 20 20 20 20 47 65 74 46 75 6c 6c 50 61 74 68 4e GetFullPathN
100a8 61 6d 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f ameW((WCHAR*)zCo
100a9 6e 76 65 72 74 65 64 2c 20 6e 42 79 74 65 2c 20 nverted, nByte,
100aa 7a 54 65 6d 70 2c 20 30 29 3b 0a 20 20 20 20 66 zTemp, 0);. f
100ab 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b ree(zConverted);
100ac 0a 20 20 20 20 7a 4f 75 74 20 3d 20 75 6e 69 63 . zOut = unic
100ad 6f 64 65 54 6f 55 74 66 38 28 7a 54 65 6d 70 29 odeToUtf8(zTemp)
100ae 3b 0a 20 20 20 20 66 72 65 65 28 7a 54 65 6d 70 ;. free(zTemp
100af 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 );./* isNT() is
100b0 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 1 if SQLITE_OS_W
100b1 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 INCE==1, so this
100b2 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 else is never e
100b3 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e xecuted. .** Sin
100b4 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72 ce the ASCII ver
100b5 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 sion of these Wi
100b6 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 ndows API do not
100b7 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 exist for WINCE
100b8 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 ,.** it's import
100b9 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 ant to not refer
100ba 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 ence them for WI
100bb 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 NCE builds..*/.#
100bc 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
100bd 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 CE==0. }else{.
100be 20 20 20 63 68 61 72 20 2a 7a 54 65 6d 70 3b 0a char *zTemp;.
100bf 20 20 20 20 6e 42 79 74 65 20 3d 20 47 65 74 46 nByte = GetF
100c0 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 68 ullPathNameA((ch
100c1 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 ar*)zConverted,
100c2 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20 0, 0, 0) + 3;.
100c3 20 20 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63 zTemp = malloc
100c4 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a ( nByte*sizeof(z
100c5 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20 Temp[0]) );.
100c6 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a if( zTemp==0 ){.
100c7 20 20 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 free(zConv
100c8 65 72 74 65 64 29 3b 0a 20 20 20 20 20 20 72 65 erted);. re
100c9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
100ca 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 47 65 74 M;. }. Get
100cb 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 FullPathNameA((c
100cc 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c har*)zConverted,
100cd 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30 nByte, zTemp, 0
100ce 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e );. free(zCon
100cf 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f 75 verted);. zOu
100d0 74 20 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 t = sqlite3_win3
100d1 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 2_mbcs_to_utf8(z
100d2 54 65 6d 70 29 3b 0a 20 20 20 20 66 72 65 65 28 Temp);. free(
100d3 7a 54 65 6d 70 29 3b 0a 23 65 6e 64 69 66 0a 20 zTemp);.#endif.
100d4 20 7d 0a 20 20 69 66 28 20 7a 4f 75 74 20 29 7b }. if( zOut ){
100d5 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
100d6 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 61 rintf(pVfs->mxPa
100d7 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 thname, zFull, "
100d8 25 73 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 %s", zOut);.
100d9 66 72 65 65 28 7a 4f 75 74 29 3b 0a 20 20 20 20 free(zOut);.
100da 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
100db 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
100dc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
100dd 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d EM;. }.#endif.}
100de 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 ../*.** Get the
100df 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 74 sector size of t
100e0 68 65 20 64 65 76 69 63 65 20 75 73 65 64 20 74 he device used t
100e1 6f 20 73 74 6f 72 65 0a 2a 2a 20 66 69 6c 65 2e o store.** file.
100e2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 .*/.static int g
100e3 65 74 53 65 63 74 6f 72 53 69 7a 65 28 0a 20 20 etSectorSize(.
100e4 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
100e5 56 66 73 2c 0a 20 20 20 20 63 6f 6e 73 74 20 63 Vfs,. const c
100e6 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 20 20 har *zRelative
100e7 20 20 20 2f 2a 20 55 54 46 2d 38 20 66 69 6c 65 /* UTF-8 file
100e8 20 6e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 44 57 name */.){. DW
100e9 4f 52 44 20 62 79 74 65 73 50 65 72 53 65 63 74 ORD bytesPerSect
100ea 6f 72 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 or = SQLITE_DEFA
100eb 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b ULT_SECTOR_SIZE;
100ec 0a 20 20 2f 2a 20 47 65 74 44 69 73 6b 46 72 65 . /* GetDiskFre
100ed 65 53 70 61 63 65 20 69 73 20 6e 6f 74 20 73 75 eSpace is not su
100ee 70 70 6f 72 74 65 64 20 75 6e 64 65 72 20 57 49 pported under WI
100ef 4e 43 45 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 NCE */.#if SQLIT
100f0 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 55 4e 55 E_OS_WINCE. UNU
100f1 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 SED_PARAMETER(pV
100f2 66 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 fs);. UNUSED_PA
100f3 52 41 4d 45 54 45 52 28 7a 52 65 6c 61 74 69 76 RAMETER(zRelativ
100f4 65 29 3b 0a 23 65 6c 73 65 0a 20 20 63 68 61 72 e);.#else. char
100f5 20 7a 46 75 6c 6c 70 61 74 68 5b 4d 41 58 5f 50 zFullpath[MAX_P
100f6 41 54 48 2b 31 5d 3b 0a 20 20 69 6e 74 20 72 63 ATH+1];. int rc
100f7 3b 0a 20 20 44 57 4f 52 44 20 64 77 52 65 74 20 ;. DWORD dwRet
100f8 3d 20 30 3b 0a 20 20 44 57 4f 52 44 20 64 77 44 = 0;. DWORD dwD
100f9 75 6d 6d 79 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a ummy;.. /*. **
100fa 20 57 65 20 6e 65 65 64 20 74 6f 20 67 65 74 20 We need to get
100fb 74 68 65 20 66 75 6c 6c 20 70 61 74 68 20 6e 61 the full path na
100fc 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 0a 20 me of the file.
100fd 20 2a 2a 20 74 6f 20 67 65 74 20 74 68 65 20 64 ** to get the d
100fe 72 69 76 65 20 6c 65 74 74 65 72 20 74 6f 20 6c rive letter to l
100ff 6f 6f 6b 20 75 70 20 74 68 65 20 73 65 63 74 6f ook up the secto
10100 72 0a 20 20 2a 2a 20 73 69 7a 65 2e 0a 20 20 2a r. ** size.. *
10101 2f 0a 20 20 72 63 20 3d 20 77 69 6e 46 75 6c 6c /. rc = winFull
10102 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a Pathname(pVfs, z
10103 52 65 6c 61 74 69 76 65 2c 20 4d 41 58 5f 50 41 Relative, MAX_PA
10104 54 48 2c 20 7a 46 75 6c 6c 70 61 74 68 29 3b 0a TH, zFullpath);.
10105 20 20 69 66 28 20 72 63 20 3d 3d 20 53 51 4c 49 if( rc == SQLI
10106 54 45 5f 4f 4b 20 29 0a 20 20 7b 0a 20 20 20 20 TE_OK ). {.
10107 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 void *zConverted
10108 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 = convertUtf8Fi
10109 6c 65 6e 61 6d 65 28 7a 46 75 6c 6c 70 61 74 68 lename(zFullpath
1010a 29 3b 0a 20 20 20 20 69 66 28 20 7a 43 6f 6e 76 );. if( zConv
1010b 65 72 74 65 64 20 29 7b 0a 20 20 20 20 20 20 69 erted ){. i
1010c 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 f( isNT() ){.
1010d 20 20 20 20 20 2f 2a 20 74 72 69 6d 20 70 61 74 /* trim pat
1010e 68 20 74 6f 20 6a 75 73 74 20 64 72 69 76 65 20 h to just drive
1010f 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20 reference */.
10110 20 20 20 20 20 57 43 48 41 52 20 2a 70 20 3d 20 WCHAR *p =
10111 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 20 20 zConverted;.
10112 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70 2b 2b 29 for(;*p;p++)
10113 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
10114 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b 0a 20 20 *p == '\\' ){.
10115 20 20 20 20 20 20 20 20 20 20 2a 70 20 3d 20 27 *p = '
10116 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 \0';.
10117 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
10118 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
10119 20 20 20 20 20 20 64 77 52 65 74 20 3d 20 47 65 dwRet = Ge
1011a 74 44 69 73 6b 46 72 65 65 53 70 61 63 65 57 28 tDiskFreeSpaceW(
1011b 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 (WCHAR*)zConvert
1011c 65 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ed,.
1011d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1011e 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a &dwDummy,.
1011f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10121 20 20 26 62 79 74 65 73 50 65 72 53 65 63 74 6f &bytesPerSecto
10122 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 r,.
10123 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10124 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 &dwDummy,.
10125 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10126 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10127 20 26 64 77 44 75 6d 6d 79 29 3b 0a 20 20 20 20 &dwDummy);.
10128 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
10129 20 2f 2a 20 74 72 69 6d 20 70 61 74 68 20 74 6f /* trim path to
1012a 20 6a 75 73 74 20 64 72 69 76 65 20 72 65 66 65 just drive refe
1012b 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20 20 20 20 rence */.
1012c 20 63 68 61 72 20 2a 70 20 3d 20 28 63 68 61 72 char *p = (char
1012d 20 2a 29 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 *)zConverted;.
1012e 20 20 20 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70 for(;*p;p
1012f 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 ++){. i
10130 66 28 20 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b f( *p == '\\' ){
10131 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 20 . *p
10132 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 = '\0';.
10133 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
10134 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
10135 0a 20 20 20 20 20 20 20 20 64 77 52 65 74 20 3d . dwRet =
10136 20 47 65 74 44 69 73 6b 46 72 65 65 53 70 61 63 GetDiskFreeSpac
10137 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 eA((char*)zConve
10138 72 74 65 64 2c 0a 20 20 20 20 20 20 20 20 20 20 rted,.
10139 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1013a 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 &dwDummy
1013b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1013c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1013d 20 20 20 20 26 62 79 74 65 73 50 65 72 53 65 63 &bytesPerSec
1013e 74 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 tor,.
1013f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10140 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c &dwDummy,
10141 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
10142 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10143 20 20 20 26 64 77 44 75 6d 6d 79 29 3b 0a 20 20 &dwDummy);.
10144 20 20 20 20 7d 0a 20 20 20 20 20 20 66 72 65 65 }. free
10145 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 (zConverted);.
10146 20 20 7d 0a 20 20 20 20 69 66 28 20 21 64 77 52 }. if( !dwR
10147 65 74 20 29 7b 0a 20 20 20 20 20 20 62 79 74 65 et ){. byte
10148 73 50 65 72 53 65 63 74 6f 72 20 3d 20 53 51 4c sPerSector = SQL
10149 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 ITE_DEFAULT_SECT
1014a 4f 52 5f 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 OR_SIZE;. }.
1014b 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 }.#endif. retu
1014c 72 6e 20 28 69 6e 74 29 20 62 79 74 65 73 50 65 rn (int) bytesPe
1014d 72 53 65 63 74 6f 72 3b 20 0a 7d 0a 0a 23 69 66 rSector; .}..#if
1014e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1014f 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a _LOAD_EXTENSION.
10150 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 /*.** Interfaces
10151 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 for opening a s
10152 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 hared library, f
10153 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 inding entry poi
10154 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 nts.** within th
10155 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 e shared library
10156 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 , and closing th
10157 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 e shared library
10158 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 ..*/./*.** Inter
10159 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e faces for openin
1015a 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 g a shared libra
1015b 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 ry, finding entr
1015c 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 y points.** with
1015d 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 in the shared li
1015e 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 brary, and closi
1015f 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 ng the shared li
10160 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 brary..*/.static
10161 20 76 6f 69 64 20 2a 77 69 6e 44 6c 4f 70 65 6e void *winDlOpen
10162 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
10163 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a fs, const char *
10164 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 48 41 zFilename){. HA
10165 4e 44 4c 45 20 68 3b 0a 20 20 76 6f 69 64 20 2a NDLE h;. void *
10166 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e zConverted = con
10167 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 vertUtf8Filename
10168 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 (zFilename);. U
10169 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
1016a 70 56 66 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f pVfs);. if( zCo
1016b 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 nverted==0 ){.
1016c 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
1016d 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a if( isNT() ){.
1016e 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c 69 62 72 h = LoadLibr
1016f 61 72 79 57 28 28 57 43 48 41 52 2a 29 7a 43 6f aryW((WCHAR*)zCo
10170 6e 76 65 72 74 65 64 29 3b 0a 2f 2a 20 69 73 4e nverted);./* isN
10171 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 T() is 1 if SQLI
10172 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 TE_OS_WINCE==1,
10173 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 so this else is
10174 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 never executed.
10175 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 .** Since the AS
10176 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 CII version of t
10177 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 hese Windows API
10178 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f do not exist fo
10179 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 r WINCE,.** it's
1017a 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f important to no
1017b 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d t reference them
1017c 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 for WINCE build
1017d 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 s..*/.#if SQLITE
1017e 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d _OS_WINCE==0. }
1017f 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 4c 6f else{. h = Lo
10180 61 64 4c 69 62 72 61 72 79 41 28 28 63 68 61 72 adLibraryA((char
10181 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 *)zConverted);.#
10182 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 endif. }. free
10183 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 (zConverted);.
10184 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 68 3b return (void*)h;
10185 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 .}.static void w
10186 69 6e 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 inDlError(sqlite
10187 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 3_vfs *pVfs, int
10188 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 nBuf, char *zBu
10189 66 4f 75 74 29 7b 0a 20 20 55 4e 55 53 45 44 5f fOut){. UNUSED_
1018a 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b PARAMETER(pVfs);
1018b 0a 20 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d . getLastErrorM
1018c 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74 sg(nBuf, zBufOut
1018d 29 3b 0a 7d 0a 76 6f 69 64 20 28 2a 77 69 6e 44 );.}.void (*winD
1018e 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 lSym(sqlite3_vfs
1018f 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 *pVfs, void *pH
10190 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 andle, const cha
10191 72 20 2a 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 r *zSymbol))(voi
10192 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 d){. UNUSED_PAR
10193 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 23 69 AMETER(pVfs);.#i
10194 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
10195 45 0a 20 20 2f 2a 20 54 68 65 20 47 65 74 50 72 E. /* The GetPr
10196 6f 63 41 64 64 72 65 73 73 41 28 29 20 72 6f 75 ocAddressA() rou
10197 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 tine is only ava
10198 69 6c 61 62 6c 65 20 6f 6e 20 77 69 6e 63 65 2e ilable on wince.
10199 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 76 6f */. return (vo
1019a 69 64 28 2a 29 28 76 6f 69 64 29 29 47 65 74 50 id(*)(void))GetP
1019b 72 6f 63 41 64 64 72 65 73 73 41 28 28 48 41 4e rocAddressA((HAN
1019c 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79 DLE)pHandle, zSy
1019d 6d 62 6f 6c 29 3b 0a 23 65 6c 73 65 0a 20 20 2f mbol);.#else. /
1019e 2a 20 41 6c 6c 20 6f 74 68 65 72 20 77 69 6e 64 * All other wind
1019f 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 20 65 78 ows platforms ex
101a0 70 65 63 74 20 47 65 74 50 72 6f 63 41 64 64 72 pect GetProcAddr
101a1 65 73 73 28 29 20 74 6f 20 74 61 6b 65 0a 20 20 ess() to take.
101a2 2a 2a 20 61 6e 20 41 6e 73 69 20 73 74 72 69 6e ** an Ansi strin
101a3 67 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 g regardless of
101a4 74 68 65 20 5f 55 4e 49 43 4f 44 45 20 73 65 74 the _UNICODE set
101a5 74 69 6e 67 20 2a 2f 0a 20 20 72 65 74 75 72 6e ting */. return
101a6 20 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 29 29 (void(*)(void))
101a7 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 28 28 GetProcAddress((
101a8 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 HANDLE)pHandle,
101a9 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6e 64 69 66 zSymbol);.#endif
101aa 0a 7d 0a 76 6f 69 64 20 77 69 6e 44 6c 43 6c 6f .}.void winDlClo
101ab 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a se(sqlite3_vfs *
101ac 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e pVfs, void *pHan
101ad 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 dle){. UNUSED_P
101ae 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a ARAMETER(pVfs);.
101af 20 20 46 72 65 65 4c 69 62 72 61 72 79 28 28 48 FreeLibrary((H
101b0 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a ANDLE)pHandle);.
101b1 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 }.#else /* if SQ
101b2 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 LITE_OMIT_LOAD_E
101b3 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 XTENSION is defi
101b4 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e ned: */. #defin
101b5 65 20 77 69 6e 44 6c 4f 70 65 6e 20 20 30 0a 20 e winDlOpen 0.
101b6 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 45 72 #define winDlEr
101b7 72 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 ror 0. #define
101b8 77 69 6e 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 winDlSym 0. #
101b9 64 65 66 69 6e 65 20 77 69 6e 44 6c 43 6c 6f 73 define winDlClos
101ba 65 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a e 0.#endif.../*.
101bb 2a 2a 20 57 72 69 74 65 20 75 70 20 74 6f 20 6e ** Write up to n
101bc 42 75 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e Buf bytes of ran
101bd 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 domness into zBu
101be 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 f..*/.static int
101bf 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 28 73 winRandomness(s
101c0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
101c1 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 , int nBuf, char
101c2 20 2a 7a 42 75 66 29 7b 0a 20 20 69 6e 74 20 6e *zBuf){. int n
101c3 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50 = 0;. UNUSED_P
101c4 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a ARAMETER(pVfs);.
101c5 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
101c6 54 45 5f 54 45 53 54 29 0a 20 20 6e 20 3d 20 6e TE_TEST). n = n
101c7 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 42 Buf;. memset(zB
101c8 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b 0a 23 65 uf, 0, nBuf);.#e
101c9 6c 73 65 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 lse. if( sizeof
101ca 28 53 59 53 54 45 4d 54 49 4d 45 29 3c 3d 6e 42 (SYSTEMTIME)<=nB
101cb 75 66 2d 6e 20 29 7b 0a 20 20 20 20 53 59 53 54 uf-n ){. SYST
101cc 45 4d 54 49 4d 45 20 78 3b 0a 20 20 20 20 47 65 EMTIME x;. Ge
101cd 74 53 79 73 74 65 6d 54 69 6d 65 28 26 78 29 3b tSystemTime(&x);
101ce 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 . memcpy(&zBu
101cf 66 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 f[n], &x, sizeof
101d0 28 78 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 (x));. n += s
101d1 69 7a 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 20 20 izeof(x);. }.
101d2 69 66 28 20 73 69 7a 65 6f 66 28 44 57 4f 52 44 if( sizeof(DWORD
101d3 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 )<=nBuf-n ){.
101d4 20 44 57 4f 52 44 20 70 69 64 20 3d 20 47 65 74 DWORD pid = Get
101d5 43 75 72 72 65 6e 74 50 72 6f 63 65 73 73 49 64 CurrentProcessId
101d6 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 ();. memcpy(&
101d7 7a 42 75 66 5b 6e 5d 2c 20 26 70 69 64 2c 20 73 zBuf[n], &pid, s
101d8 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 izeof(pid));.
101d9 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 70 69 64 n += sizeof(pid
101da 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a );. }. if( siz
101db 65 6f 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 eof(DWORD)<=nBuf
101dc 2d 6e 20 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 -n ){. DWORD
101dd 63 6e 74 20 3d 20 47 65 74 54 69 63 6b 43 6f 75 cnt = GetTickCou
101de 6e 74 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 nt();. memcpy
101df 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 63 6e 74 2c (&zBuf[n], &cnt,
101e0 20 73 69 7a 65 6f 66 28 63 6e 74 29 29 3b 0a 20 sizeof(cnt));.
101e1 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 63 n += sizeof(c
101e2 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 nt);. }. if( s
101e3 69 7a 65 6f 66 28 4c 41 52 47 45 5f 49 4e 54 45 izeof(LARGE_INTE
101e4 47 45 52 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a GER)<=nBuf-n ){.
101e5 20 20 20 20 4c 41 52 47 45 5f 49 4e 54 45 47 45 LARGE_INTEGE
101e6 52 20 69 3b 0a 20 20 20 20 51 75 65 72 79 50 65 R i;. QueryPe
101e7 72 66 6f 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72 rformanceCounter
101e8 28 26 69 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 (&i);. memcpy
101e9 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 69 2c 20 73 (&zBuf[n], &i, s
101ea 69 7a 65 6f 66 28 69 29 29 3b 0a 20 20 20 20 6e izeof(i));. n
101eb 20 2b 3d 20 73 69 7a 65 6f 66 28 69 29 3b 0a 20 += sizeof(i);.
101ec 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 }.#endif. retu
101ed 72 6e 20 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 rn n;.}.../*.**
101ee 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 Sleep for a litt
101ef 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 le while. Retur
101f0 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 n the amount of
101f1 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 73 time slept..*/.s
101f2 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 6c 65 tatic int winSle
101f3 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ep(sqlite3_vfs *
101f4 70 56 66 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73 pVfs, int micros
101f5 65 63 29 7b 0a 20 20 53 6c 65 65 70 28 28 6d 69 ec){. Sleep((mi
101f6 63 72 6f 73 65 63 2b 39 39 39 29 2f 31 30 30 30 crosec+999)/1000
101f7 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
101f8 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 72 METER(pVfs);. r
101f9 65 74 75 72 6e 20 28 28 6d 69 63 72 6f 73 65 63 eturn ((microsec
101fa 2b 39 39 39 29 2f 31 30 30 30 29 2a 31 30 30 30 +999)/1000)*1000
101fb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 ;.}../*.** The f
101fc 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c ollowing variabl
101fd 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61 20 6e e, if set to a n
101fe 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 on-zero value, b
101ff 65 63 6f 6d 65 73 20 74 68 65 20 72 65 73 75 6c ecomes the resul
10200 74 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 66 72 t.** returned fr
10201 6f 6d 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 om sqlite3OsCurr
10202 65 6e 74 54 69 6d 65 28 29 2e 20 20 54 68 69 73 entTime(). This
10203 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 is used for tes
10204 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 ting..*/.#ifdef
10205 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 SQLITE_TEST.SQLI
10206 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
10207 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 e3_current_time
10208 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a = 0;.#endif../*.
10209 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75 72 72 ** Find the curr
1020a 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 ent time (in Uni
1020b 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 versal Coordinat
1020c 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69 74 65 ed Time). Write
1020d 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 the.** current
1020e 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 61 73 time and date as
1020f 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 a Julian Day nu
10210 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 mber into *prNow
10211 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 and.** return 0
10212 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 . Return 1 if t
10213 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 he time and date
10214 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 cannot be found
10215 2e 0a 2a 2f 0a 69 6e 74 20 77 69 6e 43 75 72 72 ..*/.int winCurr
10216 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f entTime(sqlite3_
10217 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c vfs *pVfs, doubl
10218 65 20 2a 70 72 4e 6f 77 29 7b 0a 20 20 46 49 4c e *prNow){. FIL
10219 45 54 49 4d 45 20 66 74 3b 0a 20 20 2f 2a 20 46 ETIME ft;. /* F
1021a 49 4c 45 54 49 4d 45 20 73 74 72 75 63 74 75 72 ILETIME structur
1021b 65 20 69 73 20 61 20 36 34 2d 62 69 74 20 76 61 e is a 64-bit va
1021c 6c 75 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67 lue representing
1021d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a the number of .
1021e 20 20 20 20 20 31 30 30 2d 6e 61 6e 6f 73 65 63 100-nanosec
1021f 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 20 73 69 ond intervals si
10220 6e 63 65 20 4a 61 6e 75 61 72 79 20 31 2c 20 31 nce January 1, 1
10221 36 30 31 20 28 3d 20 4a 44 20 32 33 30 35 38 31 601 (= JD 230581
10222 33 2e 35 29 2e 20 0a 20 20 2a 2f 0a 20 20 73 71 3.5). . */. sq
10223 6c 69 74 65 33 5f 69 6e 74 36 34 20 74 69 6d 65 lite3_int64 time
10224 57 3b 20 20 20 2f 2a 20 57 68 6f 6c 65 20 64 61 W; /* Whole da
10225 79 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f ys */. sqlite3_
10226 69 6e 74 36 34 20 74 69 6d 65 46 3b 20 20 20 2f int64 timeF; /
10227 2a 20 46 72 61 63 74 69 6f 6e 61 6c 20 44 61 79 * Fractional Day
10228 73 20 2a 2f 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65 s */.. /* Numbe
10229 72 20 6f 66 20 31 30 30 2d 6e 61 6e 6f 73 65 63 r of 100-nanosec
1022a 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 20 69 6e ond intervals in
1022b 20 61 20 73 69 6e 67 6c 65 20 64 61 79 20 2a 2f a single day */
1022c 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
1022d 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 74 sqlite3_int64 nt
1022e 75 50 65 72 44 61 79 20 3d 20 0a 20 20 20 20 20 uPerDay = .
1022f 20 31 30 30 30 30 30 30 30 2a 28 73 71 6c 69 74 10000000*(sqlit
10230 65 33 5f 69 6e 74 36 34 29 38 36 34 30 30 3b 0a e3_int64)86400;.
10231 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 . /* Number of
10232 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 100-nanosecond i
10233 6e 74 65 72 76 61 6c 73 20 69 6e 20 68 61 6c 66 ntervals in half
10234 20 6f 66 20 61 20 64 61 79 20 2a 2f 0a 20 20 73 of a day */. s
10235 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 tatic const sqli
10236 74 65 33 5f 69 6e 74 36 34 20 6e 74 75 50 65 72 te3_int64 ntuPer
10237 48 61 6c 66 44 61 79 20 3d 20 0a 20 20 20 20 20 HalfDay = .
10238 20 31 30 30 30 30 30 30 30 2a 28 73 71 6c 69 74 10000000*(sqlit
10239 65 33 5f 69 6e 74 36 34 29 34 33 32 30 30 3b 0a e3_int64)43200;.
1023a 0a 20 20 2f 2a 20 32 5e 33 32 20 2d 20 74 6f 20 . /* 2^32 - to
1023b 61 76 6f 69 64 20 75 73 65 20 6f 66 20 4c 4c 20 avoid use of LL
1023c 61 6e 64 20 77 61 72 6e 69 6e 67 73 20 69 6e 20 and warnings in
1023d 67 63 63 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 gcc */. static
1023e 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6e const sqlite3_in
1023f 74 36 34 20 6d 61 78 33 32 42 69 74 56 61 6c 75 t64 max32BitValu
10240 65 20 3d 20 0a 20 20 20 20 20 20 28 73 71 6c 69 e = . (sqli
10241 74 65 33 5f 69 6e 74 36 34 29 32 30 30 30 30 30 te3_int64)200000
10242 30 30 30 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 0000 + (sqlite3_
10243 69 6e 74 36 34 29 32 30 30 30 30 30 30 30 30 30 int64)2000000000
10244 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 + (sqlite3_int6
10245 34 29 32 39 34 39 36 37 32 39 36 3b 0a 0a 23 69 4)294967296;..#i
10246 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
10247 45 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20 74 E. SYSTEMTIME t
10248 69 6d 65 3b 0a 20 20 47 65 74 53 79 73 74 65 6d ime;. GetSystem
10249 54 69 6d 65 28 26 74 69 6d 65 29 3b 0a 20 20 2f Time(&time);. /
1024a 2a 20 69 66 20 53 79 73 74 65 6d 54 69 6d 65 54 * if SystemTimeT
1024b 6f 46 69 6c 65 54 69 6d 65 28 29 20 66 61 69 6c oFileTime() fail
1024c 73 2c 20 69 74 20 72 65 74 75 72 6e 73 20 7a 65 s, it returns ze
1024d 72 6f 2e 20 2a 2f 0a 20 20 69 66 20 28 21 53 79 ro. */. if (!Sy
1024e 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 stemTimeToFileTi
1024f 6d 65 28 26 74 69 6d 65 2c 26 66 74 29 29 7b 0a me(&time,&ft)){.
10250 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
10251 7d 0a 23 65 6c 73 65 0a 20 20 47 65 74 53 79 73 }.#else. GetSys
10252 74 65 6d 54 69 6d 65 41 73 46 69 6c 65 54 69 6d temTimeAsFileTim
10253 65 28 20 26 66 74 20 29 3b 0a 23 65 6e 64 69 66 e( &ft );.#endif
10254 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
10255 54 45 52 28 70 56 66 73 29 3b 0a 20 20 74 69 6d TER(pVfs);. tim
10256 65 57 20 3d 20 28 28 28 73 71 6c 69 74 65 33 5f eW = (((sqlite3_
10257 69 6e 74 36 34 29 66 74 2e 64 77 48 69 67 68 44 int64)ft.dwHighD
10258 61 74 65 54 69 6d 65 29 2a 6d 61 78 33 32 42 69 ateTime)*max32Bi
10259 74 56 61 6c 75 65 29 20 2b 20 28 73 71 6c 69 74 tValue) + (sqlit
1025a 65 33 5f 69 6e 74 36 34 29 66 74 2e 64 77 4c 6f e3_int64)ft.dwLo
1025b 77 44 61 74 65 54 69 6d 65 3b 0a 20 20 74 69 6d wDateTime;. tim
1025c 65 46 20 3d 20 74 69 6d 65 57 20 25 20 6e 74 75 eF = timeW % ntu
1025d 50 65 72 44 61 79 3b 20 20 20 20 20 20 20 20 20 PerDay;
1025e 20 2f 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20 64 /* fractional d
1025f 61 79 73 20 28 31 30 30 2d 6e 61 6e 6f 73 65 63 ays (100-nanosec
10260 6f 6e 64 73 29 20 2a 2f 0a 20 20 74 69 6d 65 57 onds) */. timeW
10261 20 3d 20 74 69 6d 65 57 20 2f 20 6e 74 75 50 65 = timeW / ntuPe
10262 72 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f rDay; /
10263 2a 20 77 68 6f 6c 65 20 64 61 79 73 20 2a 2f 0a * whole days */.
10264 20 20 74 69 6d 65 57 20 3d 20 74 69 6d 65 57 20 timeW = timeW
10265 2b 20 32 33 30 35 38 31 33 3b 20 20 20 20 20 20 + 2305813;
10266 20 20 20 20 20 20 2f 2a 20 61 64 64 20 77 68 6f /* add who
10267 6c 65 20 64 61 79 73 20 28 66 72 6f 6d 20 32 33 le days (from 23
10268 30 35 38 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69 05813.5) */. ti
10269 6d 65 46 20 3d 20 74 69 6d 65 46 20 2b 20 6e 74 meF = timeF + nt
1026a 75 50 65 72 48 61 6c 66 44 61 79 3b 20 20 20 20 uPerHalfDay;
1026b 20 20 2f 2a 20 61 64 64 20 68 61 6c 66 20 61 20 /* add half a
1026c 64 61 79 20 28 66 72 6f 6d 20 32 33 30 35 38 31 day (from 230581
1026d 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3.5) */. timeW
1026e 3d 20 74 69 6d 65 57 20 2b 20 28 74 69 6d 65 46 = timeW + (timeF
1026f 2f 6e 74 75 50 65 72 44 61 79 29 3b 20 20 2f 2a /ntuPerDay); /*
10270 20 61 64 64 20 77 68 6f 6c 65 20 64 61 79 20 69 add whole day i
10271 66 20 68 61 6c 66 20 64 61 79 20 6d 61 64 65 20 f half day made
10272 6f 6e 65 20 2a 2f 0a 20 20 74 69 6d 65 46 20 3d one */. timeF =
10273 20 74 69 6d 65 46 20 25 20 6e 74 75 50 65 72 44 timeF % ntuPerD
10274 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ay; /*
10275 63 6f 6d 70 75 74 65 20 6e 65 77 20 66 72 61 63 compute new frac
10276 74 69 6f 6e 61 6c 20 64 61 79 73 20 2a 2f 0a 20 tional days */.
10277 20 2a 70 72 4e 6f 77 20 3d 20 28 64 6f 75 62 6c *prNow = (doubl
10278 65 29 74 69 6d 65 57 20 2b 20 28 28 64 6f 75 62 e)timeW + ((doub
10279 6c 65 29 74 69 6d 65 46 20 2f 20 28 64 6f 75 62 le)timeF / (doub
1027a 6c 65 29 6e 74 75 50 65 72 44 61 79 29 3b 0a 23 le)ntuPerDay);.#
1027b 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
1027c 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f T. if( sqlite3_
1027d 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a current_time ){.
1027e 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 28 28 64 *prNow = ((d
1027f 6f 75 62 6c 65 29 73 71 6c 69 74 65 33 5f 63 75 ouble)sqlite3_cu
10280 72 72 65 6e 74 5f 74 69 6d 65 20 2b 20 28 64 6f rrent_time + (do
10281 75 62 6c 65 29 34 33 32 30 30 29 20 2f 20 28 64 uble)43200) / (d
10282 6f 75 62 6c 65 29 38 36 34 30 30 20 2b 20 28 64 ouble)86400 + (d
10283 6f 75 62 6c 65 29 32 34 34 30 35 38 37 3b 0a 20 ouble)2440587;.
10284 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 }.#endif. retu
10285 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 rn 0;.}../*.** T
10286 68 65 20 69 64 65 61 20 69 73 20 74 68 61 74 20 he idea is that
10287 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f this function wo
10288 72 6b 73 20 6c 69 6b 65 20 61 20 63 6f 6d 62 69 rks like a combi
10289 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 47 65 74 nation of.** Get
1028a 4c 61 73 74 45 72 72 6f 72 28 29 20 61 6e 64 20 LastError() and
1028b 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 28 29 20 FormatMessage()
1028c 6f 6e 20 77 69 6e 64 6f 77 73 20 28 6f 72 20 65 on windows (or e
1028d 72 72 6e 6f 20 61 6e 64 0a 2a 2a 20 73 74 72 65 rrno and.** stre
1028e 72 72 6f 72 5f 72 28 29 20 6f 6e 20 75 6e 69 78 rror_r() on unix
1028f 29 2e 20 41 66 74 65 72 20 61 6e 20 65 72 72 6f ). After an erro
10290 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 r is returned by
10291 20 61 6e 20 4f 53 0a 2a 2a 20 66 75 6e 63 74 69 an OS.** functi
10292 6f 6e 2c 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 on, SQLite calls
10293 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 this function w
10294 69 74 68 20 7a 42 75 66 20 70 6f 69 6e 74 69 6e ith zBuf pointin
10295 67 20 74 6f 0a 2a 2a 20 61 20 62 75 66 66 65 72 g to.** a buffer
10296 20 6f 66 20 6e 42 75 66 20 62 79 74 65 73 2e 20 of nBuf bytes.
10297 54 68 65 20 4f 53 20 6c 61 79 65 72 20 73 68 6f The OS layer sho
10298 75 6c 64 20 70 6f 70 75 6c 61 74 65 20 74 68 65 uld populate the
10299 0a 2a 2a 20 62 75 66 66 65 72 20 77 69 74 68 20 .** buffer with
1029a 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 a nul-terminated
1029b 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 65 UTF-8 encoded e
1029c 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 rror message.**
1029d 64 65 73 63 72 69 62 69 6e 67 20 74 68 65 20 6c describing the l
1029e 61 73 74 20 49 4f 20 65 72 72 6f 72 20 74 6f 20 ast IO error to
1029f 68 61 76 65 20 6f 63 63 75 72 72 65 64 20 77 69 have occurred wi
102a0 74 68 69 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67 thin the calling
102a1 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a .** thread..**.*
102a2 2a 20 49 66 20 74 68 65 20 65 72 72 6f 72 20 6d * If the error m
102a3 65 73 73 61 67 65 20 69 73 20 74 6f 6f 20 6c 61 essage is too la
102a4 72 67 65 20 66 6f 72 20 74 68 65 20 73 75 70 70 rge for the supp
102a5 6c 69 65 64 20 62 75 66 66 65 72 2c 0a 2a 2a 20 lied buffer,.**
102a6 69 74 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75 it should be tru
102a7 6e 63 61 74 65 64 2e 20 54 68 65 20 72 65 74 75 ncated. The retu
102a8 72 6e 20 76 61 6c 75 65 20 6f 66 20 78 47 65 74 rn value of xGet
102a9 4c 61 73 74 45 72 72 6f 72 0a 2a 2a 20 69 73 20 LastError.** is
102aa 7a 65 72 6f 20 69 66 20 74 68 65 20 65 72 72 6f zero if the erro
102ab 72 20 6d 65 73 73 61 67 65 20 66 69 74 73 20 69 r message fits i
102ac 6e 20 74 68 65 20 62 75 66 66 65 72 2c 20 6f 72 n the buffer, or
102ad 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 non-zero.** oth
102ae 65 72 77 69 73 65 20 28 69 66 20 74 68 65 20 6d erwise (if the m
102af 65 73 73 61 67 65 20 77 61 73 20 74 72 75 6e 63 essage was trunc
102b0 61 74 65 64 29 2e 20 49 66 20 6e 6f 6e 2d 7a 65 ated). If non-ze
102b1 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a ro is returned,.
102b2 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 6e 6f ** then it is no
102b3 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 69 t necessary to i
102b4 6e 63 6c 75 64 65 20 74 68 65 20 6e 75 6c 2d 74 nclude the nul-t
102b5 65 72 6d 69 6e 61 74 6f 72 20 63 68 61 72 61 63 erminator charac
102b6 74 65 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 75 ter.** in the ou
102b7 74 70 75 74 20 62 75 66 66 65 72 2e 0a 2a 2a 0a tput buffer..**.
102b8 2a 2a 20 4e 6f 74 20 73 75 70 70 6c 79 69 6e 67 ** Not supplying
102b9 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
102ba 65 20 77 69 6c 6c 20 68 61 76 65 20 6e 6f 20 61 e will have no a
102bb 64 76 65 72 73 65 20 65 66 66 65 63 74 0a 2a 2a dverse effect.**
102bc 20 6f 6e 20 53 51 4c 69 74 65 2e 20 49 74 20 69 on SQLite. It i
102bd 73 20 66 69 6e 65 20 74 6f 20 68 61 76 65 20 61 s fine to have a
102be 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e n implementation
102bf 20 74 68 61 74 20 6e 65 76 65 72 0a 2a 2a 20 72 that never.** r
102c0 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 eturns an error
102c1 6d 65 73 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 message:.**.**
102c2 20 69 6e 74 20 78 47 65 74 4c 61 73 74 45 72 72 int xGetLastErr
102c3 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a or(sqlite3_vfs *
102c4 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 pVfs, int nBuf,
102c5 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 2a 2a 20 char *zBuf){.**
102c6 20 20 20 20 61 73 73 65 72 74 28 7a 42 75 66 5b assert(zBuf[
102c7 30 5d 3d 3d 27 5c 30 27 29 3b 0a 2a 2a 20 20 20 0]=='\0');.**
102c8 20 20 72 65 74 75 72 6e 20 30 3b 0a 2a 2a 20 20 return 0;.**
102c9 20 7d 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 }.**.** However
102ca 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 if an error mes
102cb 73 61 67 65 20 69 73 20 73 75 70 70 6c 69 65 64 sage is supplied
102cc 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 69 6e 63 , it will be inc
102cd 6f 72 70 6f 72 61 74 65 64 0a 2a 2a 20 62 79 20 orporated.** by
102ce 73 71 6c 69 74 65 20 69 6e 74 6f 20 74 68 65 20 sqlite into the
102cf 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 61 76 error message av
102d0 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 65 20 75 ailable to the u
102d1 73 65 72 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c ser using.** sql
102d2 69 74 65 33 5f 65 72 72 6d 73 67 28 29 2c 20 70 ite3_errmsg(), p
102d3 6f 73 73 69 62 6c 79 20 6d 61 6b 69 6e 67 20 49 ossibly making I
102d4 4f 20 65 72 72 6f 72 73 20 65 61 73 69 65 72 20 O errors easier
102d5 74 6f 20 64 65 62 75 67 2e 0a 2a 2f 0a 73 74 61 to debug..*/.sta
102d6 74 69 63 20 69 6e 74 20 77 69 6e 47 65 74 4c 61 tic int winGetLa
102d7 73 74 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f stError(sqlite3_
102d8 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e vfs *pVfs, int n
102d9 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 Buf, char *zBuf)
102da 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
102db 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 72 65 ETER(pVfs);. re
102dc 74 75 72 6e 20 67 65 74 4c 61 73 74 45 72 72 6f turn getLastErro
102dd 72 4d 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 29 rMsg(nBuf, zBuf)
102de 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 ;.}../*.** Initi
102df 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 alize and deinit
102e0 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 ialize the opera
102e1 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 ting system inte
102e2 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 rface..*/.SQLITE
102e3 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
102e4 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a _os_init(void){.
102e5 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 static sqlite3
102e6 5f 76 66 73 20 77 69 6e 56 66 73 20 3d 20 7b 0a _vfs winVfs = {.
102e7 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 1,
102e8 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 /* iVersi
102e9 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 on */. sizeof
102ea 28 77 69 6e 46 69 6c 65 29 2c 20 20 20 2f 2a 20 (winFile), /*
102eb 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 szOsFile */.
102ec 4d 41 58 5f 50 41 54 48 2c 20 20 20 20 20 20 20 MAX_PATH,
102ed 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 /* mxPathname
102ee 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 */. 0,
102ef 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e /* pN
102f0 65 78 74 20 2a 2f 0a 20 20 20 20 22 77 69 6e 33 ext */. "win3
102f1 32 22 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 2", /*
102f2 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c zName */. 0,
102f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
102f4 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a /* pAppData */.
102f5 20 0a 20 20 20 20 77 69 6e 4f 70 65 6e 2c 20 20 . winOpen,
102f6 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 /* xOpe
102f7 6e 20 2a 2f 0a 20 20 20 20 77 69 6e 44 65 6c 65 n */. winDele
102f8 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 te, /* x
102f9 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 77 69 Delete */. wi
102fa 6e 41 63 63 65 73 73 2c 20 20 20 20 20 20 20 20 nAccess,
102fb 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 /* xAccess */.
102fc 20 20 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 winFullPathna
102fd 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 me, /* xFullPa
102fe 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 77 69 thname */. wi
102ff 6e 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 nDlOpen,
10300 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 /* xDlOpen */.
10301 20 20 20 77 69 6e 44 6c 45 72 72 6f 72 2c 20 20 winDlError,
10302 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f /* xDlErro
10303 72 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 53 79 r */. winDlSy
10304 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 m, /* x
10305 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 77 69 6e DlSym */. win
10306 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 DlClose,
10307 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 /* xDlClose */.
10308 20 20 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 winRandomness
10309 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d , /* xRandom
1030a 6e 65 73 73 20 2a 2f 0a 20 20 20 20 77 69 6e 53 ness */. winS
1030b 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 2f leep, /
1030c 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 * xSleep */.
1030d 77 69 6e 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 winCurrentTime,
1030e 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 /* xCurrentTi
1030f 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 47 65 74 me */. winGet
10310 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 LastError /*
10311 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f xGetLastError */
10312 0a 20 20 7d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 . };.. sqlite3
10313 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 77 _vfs_register(&w
10314 69 6e 56 66 73 2c 20 31 29 3b 0a 20 20 72 65 74 inVfs, 1);. ret
10315 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a urn SQLITE_OK; .
10316 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 }.SQLITE_API int
10317 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 sqlite3_os_end(
10318 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 75 72 6e void){ . return
10319 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 SQLITE_OK;.}..#
1031a 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1031b 4f 53 5f 57 49 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a OS_WIN */../****
1031c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
1031d 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a f os_win.c *****
1031e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1031f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
10321 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
10322 20 66 69 6c 65 20 62 69 74 76 65 63 2e 63 20 2a file bitvec.c *
10323 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10324 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10325 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
10326 20 32 30 30 38 20 46 65 62 72 75 61 72 79 20 31 2008 February 1
10327 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6.**.** The auth
10328 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
10329 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
1032a 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
1032b 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
1032c 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
1032d 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
1032e 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
1032f 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
10330 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
10331 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
10332 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
10333 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
10334 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
10335 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
10336 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
10337 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
10338 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
10339 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1033a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1033b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1033c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1033d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d .** This file im
1033e 70 6c 65 6d 65 6e 74 73 20 61 6e 20 6f 62 6a 65 plements an obje
1033f 63 74 20 74 68 61 74 20 72 65 70 72 65 73 65 6e ct that represen
10340 74 73 20 61 20 66 69 78 65 64 2d 6c 65 6e 67 74 ts a fixed-lengt
10341 68 0a 2a 2a 20 62 69 74 6d 61 70 2e 20 20 42 69 h.** bitmap. Bi
10342 74 73 20 61 72 65 20 6e 75 6d 62 65 72 65 64 20 ts are numbered
10343 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 31 2e starting with 1.
10344 0a 2a 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 .**.** A bitmap
10345 69 73 20 75 73 65 64 20 74 6f 20 72 65 63 6f 72 is used to recor
10346 64 20 77 68 69 63 68 20 70 61 67 65 73 20 6f 66 d which pages of
10347 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 a database file
10348 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20 6a 6f have been.** jo
10349 75 72 6e 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 urnalled during
1034a 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f a transaction, o
1034b 72 20 77 68 69 63 68 20 70 61 67 65 73 20 68 61 r which pages ha
1034c 76 65 20 74 68 65 20 22 64 6f 6e 74 2d 77 72 69 ve the "dont-wri
1034d 74 65 22 0a 2a 2a 20 70 72 6f 70 65 72 74 79 2e te".** property.
1034e 20 20 55 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 61 Usually only a
1034f 20 66 65 77 20 70 61 67 65 73 20 61 72 65 20 6d few pages are m
10350 65 65 74 20 65 69 74 68 65 72 20 63 6f 6e 64 69 eet either condi
10351 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 tion..** So the
10352 62 69 74 6d 61 70 20 69 73 20 75 73 75 61 6c 6c bitmap is usuall
10353 79 20 73 70 61 72 73 65 20 61 6e 64 20 68 61 73 y sparse and has
10354 20 6c 6f 77 20 63 61 72 64 69 6e 61 6c 69 74 79 low cardinality
10355 2e 0a 2a 2a 20 42 75 74 20 73 6f 6d 65 74 69 6d ..** But sometim
10356 65 73 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 es (for example
10357 77 68 65 6e 20 64 75 72 69 6e 67 20 61 20 44 52 when during a DR
10358 4f 50 20 6f 66 20 61 20 6c 61 72 67 65 20 74 61 OP of a large ta
10359 62 6c 65 29 20 6d 6f 73 74 0a 2a 2a 20 6f 72 20 ble) most.** or
1035a 61 6c 6c 20 6f 66 20 74 68 65 20 70 61 67 65 73 all of the pages
1035b 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 63 in a database c
1035c 61 6e 20 67 65 74 20 6a 6f 75 72 6e 61 6c 6c 65 an get journalle
1035d 64 2e 20 20 49 6e 20 74 68 6f 73 65 20 63 61 73 d. In those cas
1035e 65 73 2c 20 0a 2a 2a 20 74 68 65 20 62 69 74 6d es, .** the bitm
1035f 61 70 20 62 65 63 6f 6d 65 73 20 64 65 6e 73 65 ap becomes dense
10360 20 77 69 74 68 20 68 69 67 68 20 63 61 72 64 69 with high cardi
10361 6e 61 6c 69 74 79 2e 20 20 54 68 65 20 61 6c 67 nality. The alg
10362 6f 72 69 74 68 6d 20 6e 65 65 64 73 20 0a 2a 2a orithm needs .**
10363 20 74 6f 20 68 61 6e 64 6c 65 20 62 6f 74 68 20 to handle both
10364 63 61 73 65 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a cases well..**.*
10365 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 * The size of th
10366 65 20 62 69 74 6d 61 70 20 69 73 20 66 69 78 65 e bitmap is fixe
10367 64 20 77 68 65 6e 20 74 68 65 20 6f 62 6a 65 63 d when the objec
10368 74 20 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a t is created..**
10369 0a 2a 2a 20 41 6c 6c 20 62 69 74 73 20 61 72 65 .** All bits are
1036a 20 63 6c 65 61 72 20 77 68 65 6e 20 74 68 65 20 clear when the
1036b 62 69 74 6d 61 70 20 69 73 20 63 72 65 61 74 65 bitmap is create
1036c 64 2e 20 20 49 6e 64 69 76 69 64 75 61 6c 20 62 d. Individual b
1036d 69 74 73 0a 2a 2a 20 6d 61 79 20 62 65 20 73 65 its.** may be se
1036e 74 20 6f 72 20 63 6c 65 61 72 65 64 20 6f 6e 65 t or cleared one
1036f 20 61 74 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a at a time..**.*
10370 2a 20 54 65 73 74 20 6f 70 65 72 61 74 69 6f 6e * Test operation
10371 73 20 61 72 65 20 61 62 6f 75 74 20 31 30 30 20 s are about 100
10372 74 69 6d 65 73 20 6d 6f 72 65 20 63 6f 6d 6d 6f times more commo
10373 6e 20 74 68 61 74 20 73 65 74 20 6f 70 65 72 61 n that set opera
10374 74 69 6f 6e 73 2e 0a 2a 2a 20 43 6c 65 61 72 20 tions..** Clear
10375 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 65 operations are e
10376 78 63 65 65 64 69 6e 67 6c 79 20 72 61 72 65 2e xceedingly rare.
10377 20 20 54 68 65 72 65 20 61 72 65 20 75 73 75 61 There are usua
10378 6c 6c 79 20 62 65 74 77 65 65 6e 0a 2a 2a 20 35 lly between.** 5
10379 20 61 6e 64 20 35 30 30 20 73 65 74 20 6f 70 65 and 500 set ope
1037a 72 61 74 69 6f 6e 73 20 70 65 72 20 42 69 74 76 rations per Bitv
1037b 65 63 20 6f 62 6a 65 63 74 2c 20 74 68 6f 75 67 ec object, thoug
1037c 68 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 h the number of
1037d 73 65 74 73 20 63 61 6e 0a 2a 2a 20 73 6f 6d 65 sets can.** some
1037e 74 69 6d 65 73 20 67 72 6f 77 20 69 6e 74 6f 20 times grow into
1037f 74 65 6e 73 20 6f 66 20 74 68 6f 75 73 61 6e 64 tens of thousand
10380 73 20 6f 72 20 6c 61 72 67 65 72 2e 20 20 54 68 s or larger. Th
10381 65 20 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a e size of the.**
10382 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 20 69 Bitvec object i
10383 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
10384 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 pages in the dat
10385 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 74 68 abase file at th
10386 65 0a 2a 2a 20 73 74 61 72 74 20 6f 66 20 61 20 e.** start of a
10387 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 transaction, and
10388 20 69 73 20 74 68 75 73 20 75 73 75 61 6c 6c 79 is thus usually
10389 20 6c 65 73 73 20 74 68 61 6e 20 61 20 66 65 77 less than a few
1038a 20 74 68 6f 75 73 61 6e 64 2c 0a 2a 2a 20 62 75 thousand,.** bu
1038b 74 20 63 61 6e 20 62 65 20 61 73 20 6c 61 72 67 t can be as larg
1038c 65 20 61 73 20 32 20 62 69 6c 6c 69 6f 6e 20 66 e as 2 billion f
1038d 6f 72 20 61 20 72 65 61 6c 6c 79 20 62 69 67 20 or a really big
1038e 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 database..**.**
1038f 40 28 23 29 20 24 49 64 3a 20 62 69 74 76 65 63 @(#) $Id: bitvec
10390 2e 63 2c 76 20 31 2e 31 37 20 32 30 30 39 2f 30 .c,v 1.17 2009/0
10391 37 2f 32 35 20 31 37 3a 33 33 3a 32 36 20 64 72 7/25 17:33:26 dr
10392 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 53 h Exp $.*/../* S
10393 69 7a 65 20 6f 66 20 74 68 65 20 42 69 74 76 65 ize of the Bitve
10394 63 20 73 74 72 75 63 74 75 72 65 20 69 6e 20 62 c structure in b
10395 79 74 65 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 ytes. */.#define
10396 20 42 49 54 56 45 43 5f 53 5a 20 20 20 20 20 20 BITVEC_SZ
10397 20 20 28 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 (sizeof(void*)
10398 2a 31 32 38 29 20 20 2f 2a 20 35 31 32 20 6f 6e *128) /* 512 on
10399 20 33 32 62 69 74 2e 20 20 31 30 32 34 20 6f 6e 32bit. 1024 on
1039a 20 36 34 62 69 74 20 2a 2f 0a 0a 2f 2a 20 52 6f 64bit */../* Ro
1039b 75 6e 64 20 74 68 65 20 75 6e 69 6f 6e 20 73 69 und the union si
1039c 7a 65 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 6e ze down to the n
1039d 65 61 72 65 73 74 20 70 6f 69 6e 74 65 72 20 62 earest pointer b
1039e 6f 75 6e 64 61 72 79 2c 20 73 69 6e 63 65 20 74 oundary, since t
1039f 68 61 74 27 73 20 68 6f 77 20 0a 2a 2a 20 69 74 hat's how .** it
103a0 20 77 69 6c 6c 20 62 65 20 61 6c 69 67 6e 65 64 will be aligned
103a1 20 77 69 74 68 69 6e 20 74 68 65 20 42 69 74 76 within the Bitv
103a2 65 63 20 73 74 72 75 63 74 2e 20 2a 2f 0a 23 64 ec struct. */.#d
103a3 65 66 69 6e 65 20 42 49 54 56 45 43 5f 55 53 49 efine BITVEC_USI
103a4 5a 45 20 20 20 20 20 28 28 28 42 49 54 56 45 43 ZE (((BITVEC
103a5 5f 53 5a 2d 28 33 2a 73 69 7a 65 6f 66 28 75 33 _SZ-(3*sizeof(u3
103a6 32 29 29 29 2f 73 69 7a 65 6f 66 28 42 69 74 76 2)))/sizeof(Bitv
103a7 65 63 2a 29 29 2a 73 69 7a 65 6f 66 28 42 69 74 ec*))*sizeof(Bit
103a8 76 65 63 2a 29 29 0a 0a 2f 2a 20 54 79 70 65 20 vec*))../* Type
103a9 6f 66 20 74 68 65 20 61 72 72 61 79 20 22 65 6c of the array "el
103aa 65 6d 65 6e 74 22 20 66 6f 72 20 74 68 65 20 62 ement" for the b
103ab 69 74 6d 61 70 20 72 65 70 72 65 73 65 6e 74 61 itmap representa
103ac 74 69 6f 6e 2e 20 0a 2a 2a 20 53 68 6f 75 6c 64 tion. .** Should
103ad 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 be a power of 2
103ae 2c 20 61 6e 64 20 69 64 65 61 6c 6c 79 2c 20 65 , and ideally, e
103af 76 65 6e 6c 79 20 64 69 76 69 64 65 20 69 6e 74 venly divide int
103b0 6f 20 42 49 54 56 45 43 5f 55 53 49 5a 45 2e 20 o BITVEC_USIZE.
103b1 0a 2a 2a 20 53 65 74 74 69 6e 67 20 74 68 69 73 .** Setting this
103b2 20 74 6f 20 74 68 65 20 22 6e 61 74 75 72 61 6c to the "natural
103b3 20 77 6f 72 64 22 20 73 69 7a 65 20 6f 66 20 79 word" size of y
103b4 6f 75 72 20 43 50 55 20 6d 61 79 20 69 6d 70 72 our CPU may impr
103b5 6f 76 65 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e ove.** performan
103b6 63 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 ce. */.#define B
103b7 49 54 56 45 43 5f 54 45 4c 45 4d 20 20 20 20 20 ITVEC_TELEM
103b8 75 38 0a 2f 2a 20 53 69 7a 65 2c 20 69 6e 20 62 u8./* Size, in b
103b9 69 74 73 2c 20 6f 66 20 74 68 65 20 62 69 74 6d its, of the bitm
103ba 61 70 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 23 ap element. */.#
103bb 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 53 5a define BITVEC_SZ
103bc 45 4c 45 4d 20 20 20 20 38 0a 2f 2a 20 4e 75 6d ELEM 8./* Num
103bd 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 ber of elements
103be 69 6e 20 61 20 62 69 74 6d 61 70 20 61 72 72 61 in a bitmap arra
103bf 79 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 y. */.#define BI
103c0 54 56 45 43 5f 4e 45 4c 45 4d 20 20 20 20 20 28 TVEC_NELEM (
103c1 42 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a BITVEC_USIZE/siz
103c2 65 6f 66 28 42 49 54 56 45 43 5f 54 45 4c 45 4d eof(BITVEC_TELEM
103c3 29 29 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 ))./* Number of
103c4 62 69 74 73 20 69 6e 20 74 68 65 20 62 69 74 6d bits in the bitm
103c5 61 70 20 61 72 72 61 79 2e 20 2a 2f 0a 23 64 65 ap array. */.#de
103c6 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 42 49 54 fine BITVEC_NBIT
103c7 20 20 20 20 20 20 28 42 49 54 56 45 43 5f 4e 45 (BITVEC_NE
103c8 4c 45 4d 2a 42 49 54 56 45 43 5f 53 5a 45 4c 45 LEM*BITVEC_SZELE
103c9 4d 29 0a 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 M)../* Number of
103ca 20 75 33 32 20 76 61 6c 75 65 73 20 69 6e 20 68 u32 values in h
103cb 61 73 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 23 64 ash table. */.#d
103cc 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 49 4e efine BITVEC_NIN
103cd 54 20 20 20 20 20 20 28 42 49 54 56 45 43 5f 55 T (BITVEC_U
103ce 53 49 5a 45 2f 73 69 7a 65 6f 66 28 75 33 32 29 SIZE/sizeof(u32)
103cf 29 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d )./* Maximum num
103d0 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
103d1 6e 20 68 61 73 68 20 74 61 62 6c 65 20 62 65 66 n hash table bef
103d2 6f 72 65 20 0a 2a 2a 20 73 75 62 2d 64 69 76 69 ore .** sub-divi
103d3 64 69 6e 67 20 61 6e 64 20 72 65 2d 68 61 73 68 ding and re-hash
103d4 69 6e 67 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ing. */.#define
103d5 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20 20 20 BITVEC_MXHASH
103d6 20 28 42 49 54 56 45 43 5f 4e 49 4e 54 2f 32 29 (BITVEC_NINT/2)
103d7 0a 2f 2a 20 48 61 73 68 69 6e 67 20 66 75 6e 63 ./* Hashing func
103d8 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 61 48 61 tion for the aHa
103d9 73 68 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f sh representatio
103da 6e 2e 0a 2a 2a 20 45 6d 70 69 72 69 63 61 6c 20 n..** Empirical
103db 74 65 73 74 69 6e 67 20 73 68 6f 77 65 64 20 74 testing showed t
103dc 68 61 74 20 74 68 65 20 2a 33 37 20 6d 75 6c 74 hat the *37 mult
103dd 69 70 6c 69 65 72 20 0a 2a 2a 20 28 61 6e 20 61 iplier .** (an a
103de 72 62 69 74 72 61 72 79 20 70 72 69 6d 65 29 69 rbitrary prime)i
103df 6e 20 74 68 65 20 68 61 73 68 20 66 75 6e 63 74 n the hash funct
103e0 69 6f 6e 20 70 72 6f 76 69 64 65 64 20 0a 2a 2a ion provided .**
103e1 20 6e 6f 20 66 65 77 65 72 20 63 6f 6c 6c 69 73 no fewer collis
103e2 69 6f 6e 73 20 74 68 61 6e 20 74 68 65 20 6e 6f ions than the no
103e3 2d 6f 70 20 2a 31 2e 20 2a 2f 0a 23 64 65 66 69 -op *1. */.#defi
103e4 6e 65 20 42 49 54 56 45 43 5f 48 41 53 48 28 58 ne BITVEC_HASH(X
103e5 29 20 20 20 28 28 28 58 29 2a 31 29 25 42 49 54 ) (((X)*1)%BIT
103e6 56 45 43 5f 4e 49 4e 54 29 0a 0a 23 64 65 66 69 VEC_NINT)..#defi
103e7 6e 65 20 42 49 54 56 45 43 5f 4e 50 54 52 20 20 ne BITVEC_NPTR
103e8 20 20 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a (BITVEC_USIZ
103e9 45 2f 73 69 7a 65 6f 66 28 42 69 74 76 65 63 20 E/sizeof(Bitvec
103ea 2a 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 62 69 *)).../*.** A bi
103eb 74 6d 61 70 20 69 73 20 61 6e 20 69 6e 73 74 61 tmap is an insta
103ec 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
103ed 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a wing structure..
103ee 2a 2a 0a 2a 2a 20 54 68 69 73 20 62 69 74 6d 61 **.** This bitma
103ef 70 20 72 65 63 6f 72 64 73 20 74 68 65 20 65 78 p records the ex
103f0 69 73 74 61 6e 63 65 20 6f 66 20 7a 65 72 6f 20 istance of zero
103f1 6f 72 20 6d 6f 72 65 20 62 69 74 73 0a 2a 2a 20 or more bits.**
103f2 77 69 74 68 20 76 61 6c 75 65 73 20 62 65 74 77 with values betw
103f3 65 65 6e 20 31 20 61 6e 64 20 69 53 69 7a 65 2c een 1 and iSize,
103f4 20 69 6e 63 6c 75 73 69 76 65 2e 0a 2a 2a 0a 2a inclusive..**.*
103f5 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 * There are thre
103f6 65 20 70 6f 73 73 69 62 6c 65 20 72 65 70 72 65 e possible repre
103f7 73 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 sentations of th
103f8 65 20 62 69 74 6d 61 70 2e 0a 2a 2a 20 49 66 20 e bitmap..** If
103f9 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 iSize<=BITVEC_NB
103fa 49 54 2c 20 74 68 65 6e 20 42 69 74 76 65 63 2e IT, then Bitvec.
103fb 75 2e 61 42 69 74 6d 61 70 5b 5d 20 69 73 20 61 u.aBitmap[] is a
103fc 20 73 74 72 61 69 67 68 74 0a 2a 2a 20 62 69 74 straight.** bit
103fd 6d 61 70 2e 20 20 54 68 65 20 6c 65 61 73 74 20 map. The least
103fe 73 69 67 6e 69 66 69 63 61 6e 74 20 62 69 74 20 significant bit
103ff 69 73 20 62 69 74 20 31 2e 0a 2a 2a 0a 2a 2a 20 is bit 1..**.**
10400 49 66 20 69 53 69 7a 65 3e 42 49 54 56 45 43 5f If iSize>BITVEC_
10401 4e 42 49 54 20 61 6e 64 20 69 44 69 76 69 73 6f NBIT and iDiviso
10402 72 3d 3d 30 20 74 68 65 6e 20 42 69 74 76 65 63 r==0 then Bitvec
10403 2e 75 2e 61 48 61 73 68 5b 5d 20 69 73 0a 2a 2a .u.aHash[] is.**
10404 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 74 68 a hash table th
10405 61 74 20 77 69 6c 6c 20 68 6f 6c 64 20 75 70 20 at will hold up
10406 74 6f 20 42 49 54 56 45 43 5f 4d 58 48 41 53 48 to BITVEC_MXHASH
10407 20 64 69 73 74 69 6e 63 74 20 76 61 6c 75 65 73 distinct values
10408 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ..**.** Otherwis
10409 65 2c 20 74 68 65 20 76 61 6c 75 65 20 69 20 69 e, the value i i
1040a 73 20 72 65 64 69 72 65 63 74 65 64 20 69 6e 74 s redirected int
1040b 6f 20 6f 6e 65 20 6f 66 20 42 49 54 56 45 43 5f o one of BITVEC_
1040c 4e 50 54 52 0a 2a 2a 20 73 75 62 2d 62 69 74 6d NPTR.** sub-bitm
1040d 61 70 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 aps pointed to b
1040e 79 20 42 69 74 76 65 63 2e 75 2e 61 70 53 75 62 y Bitvec.u.apSub
1040f 5b 5d 2e 20 20 45 61 63 68 20 73 75 62 62 69 74 []. Each subbit
10410 6d 61 70 0a 2a 2a 20 68 61 6e 64 6c 65 73 20 75 map.** handles u
10411 70 20 74 6f 20 69 44 69 76 69 73 6f 72 20 73 65 p to iDivisor se
10412 70 61 72 61 74 65 20 76 61 6c 75 65 73 20 6f 66 parate values of
10413 20 69 2e 20 20 61 70 53 75 62 5b 30 5d 20 68 6f i. apSub[0] ho
10414 6c 64 73 0a 2a 2a 20 76 61 6c 75 65 73 20 62 65 lds.** values be
10415 74 77 65 65 6e 20 31 20 61 6e 64 20 69 44 69 76 tween 1 and iDiv
10416 69 73 6f 72 2e 20 20 61 70 53 75 62 5b 31 5d 20 isor. apSub[1]
10417 68 6f 6c 64 73 20 76 61 6c 75 65 73 20 62 65 74 holds values bet
10418 77 65 65 6e 0a 2a 2a 20 69 44 69 76 69 73 6f 72 ween.** iDivisor
10419 2b 31 20 61 6e 64 20 32 2a 69 44 69 76 69 73 6f +1 and 2*iDiviso
1041a 72 2e 20 20 61 70 53 75 62 5b 4e 5d 20 68 6f 6c r. apSub[N] hol
1041b 64 73 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 ds values betwee
1041c 6e 0a 2a 2a 20 4e 2a 69 44 69 76 69 73 6f 72 2b n.** N*iDivisor+
1041d 31 20 61 6e 64 20 28 4e 2b 31 29 2a 69 44 69 76 1 and (N+1)*iDiv
1041e 69 73 6f 72 2e 20 20 45 61 63 68 20 73 75 62 62 isor. Each subb
1041f 69 74 6d 61 70 20 69 73 20 6e 6f 72 6d 61 6c 69 itmap is normali
10420 7a 65 64 0a 2a 2a 20 74 6f 20 68 6f 6c 64 20 64 zed.** to hold d
10421 65 61 6c 20 77 69 74 68 20 76 61 6c 75 65 73 20 eal with values
10422 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 69 44 between 1 and iD
10423 69 76 69 73 6f 72 2e 0a 2a 2f 0a 73 74 72 75 63 ivisor..*/.struc
10424 74 20 42 69 74 76 65 63 20 7b 0a 20 20 75 33 32 t Bitvec {. u32
10425 20 69 53 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 iSize; /*
10426 4d 61 78 69 6d 75 6d 20 62 69 74 20 69 6e 64 65 Maximum bit inde
10427 78 2e 20 20 4d 61 78 20 69 53 69 7a 65 20 69 73 x. Max iSize is
10428 20 34 2c 32 39 34 2c 39 36 37 2c 32 39 36 2e 20 4,294,967,296.
10429 2a 2f 0a 20 20 75 33 32 20 6e 53 65 74 3b 20 20 */. u32 nSet;
1042a 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1042b 66 20 62 69 74 73 20 74 68 61 74 20 61 72 65 20 f bits that are
1042c 73 65 74 20 2d 20 6f 6e 6c 79 20 76 61 6c 69 64 set - only valid
1042d 20 66 6f 72 20 61 48 61 73 68 0a 20 20 20 20 20 for aHash.
1042e 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 **
1042f 65 6c 65 6d 65 6e 74 2e 20 20 4d 61 78 20 69 73 element. Max is
10430 20 42 49 54 56 45 43 5f 4e 49 4e 54 2e 20 20 46 BITVEC_NINT. F
10431 6f 72 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 or BITVEC_SZ of
10432 35 31 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 512,.
10433 20 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 77 ** this w
10434 6f 75 6c 64 20 62 65 20 31 32 35 2e 20 2a 2f 0a ould be 125. */.
10435 20 20 75 33 32 20 69 44 69 76 69 73 6f 72 3b 20 u32 iDivisor;
10436 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
10437 69 74 73 20 68 61 6e 64 6c 65 64 20 62 79 20 65 its handled by e
10438 61 63 68 20 61 70 53 75 62 5b 5d 20 65 6e 74 72 ach apSub[] entr
10439 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 y. */.
1043a 20 20 20 20 20 20 20 20 2f 2a 20 53 68 6f 75 6c /* Shoul
1043b 64 20 3e 3d 30 20 66 6f 72 20 61 70 53 75 62 20 d >=0 for apSub
1043c 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 element. */.
1043d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1043e 20 4d 61 78 20 69 44 69 76 69 73 6f 72 20 69 73 Max iDivisor is
1043f 20 6d 61 78 28 75 33 32 29 20 2f 20 42 49 54 56 max(u32) / BITV
10440 45 43 5f 4e 50 54 52 20 2b 20 31 2e 20 20 2a 2f EC_NPTR + 1. */
10441 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
10442 20 20 20 2f 2a 20 46 6f 72 20 61 20 42 49 54 56 /* For a BITV
10443 45 43 5f 53 5a 20 6f 66 20 35 31 32 2c 20 74 68 EC_SZ of 512, th
10444 69 73 20 77 6f 75 6c 64 20 62 65 20 33 34 2c 33 is would be 34,3
10445 35 39 2c 37 33 39 2e 20 2a 2f 0a 20 20 75 6e 69 59,739. */. uni
10446 6f 6e 20 7b 0a 20 20 20 20 42 49 54 56 45 43 5f on {. BITVEC_
10447 54 45 4c 45 4d 20 61 42 69 74 6d 61 70 5b 42 49 TELEM aBitmap[BI
10448 54 56 45 43 5f 4e 45 4c 45 4d 5d 3b 20 20 20 20 TVEC_NELEM];
10449 2f 2a 20 42 69 74 6d 61 70 20 72 65 70 72 65 73 /* Bitmap repres
1044a 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 entation */.
1044b 75 33 32 20 61 48 61 73 68 5b 42 49 54 56 45 43 u32 aHash[BITVEC
1044c 5f 4e 49 4e 54 5d 3b 20 20 20 20 20 20 2f 2a 20 _NINT]; /*
1044d 48 61 73 68 20 74 61 62 6c 65 20 72 65 70 72 65 Hash table repre
1044e 73 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 sentation */.
1044f 20 42 69 74 76 65 63 20 2a 61 70 53 75 62 5b 42 Bitvec *apSub[B
10450 49 54 56 45 43 5f 4e 50 54 52 5d 3b 20 20 2f 2a ITVEC_NPTR]; /*
10451 20 52 65 63 75 72 73 69 76 65 20 72 65 70 72 65 Recursive repre
10452 73 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 7d sentation */. }
10453 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 u;.};../*.** Cr
10454 65 61 74 65 20 61 20 6e 65 77 20 62 69 74 6d 61 eate a new bitma
10455 70 20 6f 62 6a 65 63 74 20 61 62 6c 65 20 74 6f p object able to
10456 20 68 61 6e 64 6c 65 20 62 69 74 73 20 62 65 74 handle bits bet
10457 77 65 65 6e 20 30 20 61 6e 64 20 69 53 69 7a 65 ween 0 and iSize
10458 2c 0a 2a 2a 20 69 6e 63 6c 75 73 69 76 65 2e 20 ,.** inclusive.
10459 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
1045a 72 20 74 6f 20 74 68 65 20 6e 65 77 20 6f 62 6a r to the new obj
1045b 65 63 74 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c ect. Return NUL
1045c 4c 20 69 66 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 L if .** malloc
1045d 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 fails..*/.SQLITE
1045e 5f 50 52 49 56 41 54 45 20 42 69 74 76 65 63 20 _PRIVATE Bitvec
1045f 2a 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 *sqlite3BitvecCr
10460 65 61 74 65 28 75 33 32 20 69 53 69 7a 65 29 7b eate(u32 iSize){
10461 0a 20 20 42 69 74 76 65 63 20 2a 70 3b 0a 20 20 . Bitvec *p;.
10462 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 2a assert( sizeof(*
10463 70 29 3d 3d 42 49 54 56 45 43 5f 53 5a 20 29 3b p)==BITVEC_SZ );
10464 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 . p = sqlite3Ma
10465 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 llocZero( sizeof
10466 28 2a 70 29 20 29 3b 0a 20 20 69 66 28 20 70 20 (*p) );. if( p
10467 29 7b 0a 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 ){. p->iSize
10468 3d 20 69 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 = iSize;. }. r
10469 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
1046a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 * Check to see i
1046b 66 20 74 68 65 20 69 2d 74 68 20 62 69 74 20 69 f the i-th bit i
1046c 73 20 73 65 74 2e 20 20 52 65 74 75 72 6e 20 74 s set. Return t
1046d 72 75 65 20 6f 72 20 66 61 6c 73 65 2e 0a 2a 2a rue or false..**
1046e 20 49 66 20 70 20 69 73 20 4e 55 4c 4c 20 28 69 If p is NULL (i
1046f 66 20 74 68 65 20 62 69 74 6d 61 70 20 68 61 73 f the bitmap has
10470 20 6e 6f 74 20 62 65 65 6e 20 63 72 65 61 74 65 not been create
10471 64 29 20 6f 72 20 69 66 0a 2a 2a 20 69 20 69 73 d) or if.** i is
10472 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 out of range, t
10473 68 65 6e 20 72 65 74 75 72 6e 20 66 61 6c 73 65 hen return false
10474 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
10475 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
10476 69 74 76 65 63 54 65 73 74 28 42 69 74 76 65 63 itvecTest(Bitvec
10477 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 69 *p, u32 i){. i
10478 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
10479 20 30 3b 0a 20 20 69 66 28 20 69 3e 70 2d 3e 69 0;. if( i>p->i
1047a 53 69 7a 65 20 7c 7c 20 69 3d 3d 30 20 29 20 72 Size || i==0 ) r
1047b 65 74 75 72 6e 20 30 3b 0a 20 20 69 2d 2d 3b 0a eturn 0;. i--;.
1047c 20 20 77 68 69 6c 65 28 20 70 2d 3e 69 44 69 76 while( p->iDiv
1047d 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 33 32 20 isor ){. u32
1047e 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69 bin = i/p->iDivi
1047f 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69 25 70 sor;. i = i%p
10480 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 ->iDivisor;.
10481 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 p = p->u.apSub[b
10482 69 6e 5d 3b 0a 20 20 20 20 69 66 20 28 21 70 29 in];. if (!p)
10483 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 {. return
10484 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 0;. }. }. i
10485 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 f( p->iSize<=BIT
10486 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20 VEC_NBIT ){.
10487 72 65 74 75 72 6e 20 28 70 2d 3e 75 2e 61 42 69 return (p->u.aBi
10488 74 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a tmap[i/BITVEC_SZ
10489 45 4c 45 4d 5d 20 26 20 28 31 3c 3c 28 69 26 28 ELEM] & (1<<(i&(
1048a 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 BITVEC_SZELEM-1)
1048b 29 29 29 21 3d 30 3b 0a 20 20 7d 20 65 6c 73 65 )))!=0;. } else
1048c 7b 0a 20 20 20 20 75 33 32 20 68 20 3d 20 42 49 {. u32 h = BI
1048d 54 56 45 43 5f 48 41 53 48 28 69 2b 2b 29 3b 0a TVEC_HASH(i++);.
1048e 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e while( p->u.
1048f 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 aHash[h] ){.
10490 20 20 69 66 28 20 70 2d 3e 75 2e 61 48 61 73 68 if( p->u.aHash
10491 5b 68 5d 3d 3d 69 20 29 20 72 65 74 75 72 6e 20 [h]==i ) return
10492 31 3b 0a 20 20 20 20 20 20 68 20 3d 20 28 68 2b 1;. h = (h+
10493 31 29 20 25 20 42 49 54 56 45 43 5f 4e 49 4e 54 1) % BITVEC_NINT
10494 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
10495 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a rn 0;. }.}../*.
10496 2a 2a 20 53 65 74 20 74 68 65 20 69 2d 74 68 20 ** Set the i-th
10497 62 69 74 2e 20 20 52 65 74 75 72 6e 20 30 20 6f bit. Return 0 o
10498 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 61 6e n success and an
10499 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 0a 2a error code if.*
1049a 2a 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 * anything goes
1049b 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 wrong..**.** Thi
1049c 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 s routine might
1049d 63 61 75 73 65 20 73 75 62 2d 62 69 74 6d 61 70 cause sub-bitmap
1049e 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 s to be allocate
1049f 64 2e 20 20 46 61 69 6c 69 6e 67 0a 2a 2a 20 74 d. Failing.** t
104a0 6f 20 67 65 74 20 74 68 65 20 6d 65 6d 6f 72 79 o get the memory
104a1 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 needed to hold
104a2 74 68 65 20 73 75 62 2d 62 69 74 6d 61 70 20 69 the sub-bitmap i
104a3 73 20 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20 74 68 s the only.** th
104a4 61 74 20 63 61 6e 20 67 6f 20 77 72 6f 6e 67 20 at can go wrong
104a5 77 69 74 68 20 61 6e 20 69 6e 73 65 72 74 2c 20 with an insert,
104a6 61 73 73 75 6d 69 6e 67 20 70 20 61 6e 64 20 69 assuming p and i
104a7 20 61 72 65 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a are valid..**.*
104a8 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 * The calling fu
104a9 6e 63 74 69 6f 6e 20 6d 75 73 74 20 65 6e 73 75 nction must ensu
104aa 72 65 20 74 68 61 74 20 70 20 69 73 20 61 20 76 re that p is a v
104ab 61 6c 69 64 20 42 69 74 76 65 63 20 6f 62 6a 65 alid Bitvec obje
104ac 63 74 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 ct.** and that t
104ad 68 65 20 76 61 6c 75 65 20 66 6f 72 20 22 69 22 he value for "i"
104ae 20 69 73 20 77 69 74 68 69 6e 20 72 61 6e 67 65 is within range
104af 20 6f 66 20 74 68 65 20 42 69 74 76 65 63 20 6f of the Bitvec o
104b0 62 6a 65 63 74 2e 0a 2a 2a 20 4f 74 68 65 72 77 bject..** Otherw
104b1 69 73 65 20 74 68 65 20 62 65 68 61 76 69 6f 72 ise the behavior
104b2 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a is undefined..*
104b3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
104b4 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 int sqlite3Bitv
104b5 65 63 53 65 74 28 42 69 74 76 65 63 20 2a 70 2c ecSet(Bitvec *p,
104b6 20 75 33 32 20 69 29 7b 0a 20 20 75 33 32 20 68 u32 i){. u32 h
104b7 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
104b8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
104b9 0a 20 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 . assert( i>0 )
104ba 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3c 3d 70 ;. assert( i<=p
104bb 2d 3e 69 53 69 7a 65 20 29 3b 0a 20 20 69 2d 2d ->iSize );. i--
104bc 3b 0a 20 20 77 68 69 6c 65 28 28 70 2d 3e 69 53 ;. while((p->iS
104bd 69 7a 65 20 3e 20 42 49 54 56 45 43 5f 4e 42 49 ize > BITVEC_NBI
104be 54 29 20 26 26 20 70 2d 3e 69 44 69 76 69 73 6f T) && p->iDiviso
104bf 72 29 20 7b 0a 20 20 20 20 75 33 32 20 62 69 6e r) {. u32 bin
104c0 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 = i/p->iDivisor
104c1 3b 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 ;. i = i%p->i
104c2 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 66 28 Divisor;. if(
104c3 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d p->u.apSub[bin]
104c4 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e ==0 ){. p->
104c5 75 2e 61 70 53 75 62 5b 62 69 6e 5d 20 3d 20 73 u.apSub[bin] = s
104c6 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 qlite3BitvecCrea
104c7 74 65 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 te( p->iDivisor
104c8 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e );. if( p->
104c9 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 u.apSub[bin]==0
104ca 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
104cb 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 NOMEM;. }.
104cc 20 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b p = p->u.apSub[
104cd 62 69 6e 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 bin];. }. if(
104ce 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 p->iSize<=BITVEC
104cf 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e _NBIT ){. p->
104d0 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 u.aBitmap[i/BITV
104d1 45 43 5f 53 5a 45 4c 45 4d 5d 20 7c 3d 20 31 20 EC_SZELEM] |= 1
104d2 3c 3c 20 28 69 26 28 42 49 54 56 45 43 5f 53 5a << (i&(BITVEC_SZ
104d3 45 4c 45 4d 2d 31 29 29 3b 0a 20 20 20 20 72 65 ELEM-1));. re
104d4 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
104d5 20 20 7d 0a 20 20 68 20 3d 20 42 49 54 56 45 43 }. h = BITVEC
104d6 5f 48 41 53 48 28 69 2b 2b 29 3b 0a 20 20 2f 2a _HASH(i++);. /*
104d7 20 69 66 20 74 68 65 72 65 20 77 61 73 6e 27 74 if there wasn't
104d8 20 61 20 68 61 73 68 20 63 6f 6c 6c 69 73 69 6f a hash collisio
104d9 6e 2c 20 61 6e 64 20 74 68 69 73 20 64 6f 65 73 n, and this does
104da 6e 27 74 20 2a 2f 0a 20 20 2f 2a 20 63 6f 6d 70 n't */. /* comp
104db 6c 65 74 65 6c 79 20 66 69 6c 6c 20 74 68 65 20 letely fill the
104dc 68 61 73 68 2c 20 74 68 65 6e 20 6a 75 73 74 20 hash, then just
104dd 61 64 64 20 69 74 20 77 69 74 68 6f 75 74 20 2a add it without *
104de 2f 0a 20 20 2f 2a 20 77 6f 72 72 69 6e 67 20 61 /. /* worring a
104df 62 6f 75 74 20 73 75 62 2d 64 69 76 69 64 69 6e bout sub-dividin
104e0 67 20 61 6e 64 20 72 65 2d 68 61 73 68 69 6e 67 g and re-hashing
104e1 2e 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 75 . */. if( !p->u
104e2 2e 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 .aHash[h] ){.
104e3 20 69 66 20 28 70 2d 3e 6e 53 65 74 3c 28 42 49 if (p->nSet<(BI
104e4 54 56 45 43 5f 4e 49 4e 54 2d 31 29 29 20 7b 0a TVEC_NINT-1)) {.
104e5 20 20 20 20 20 20 67 6f 74 6f 20 62 69 74 76 65 goto bitve
104e6 63 5f 73 65 74 5f 65 6e 64 3b 0a 20 20 20 20 7d c_set_end;. }
104e7 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 67 6f else {. go
104e8 74 6f 20 62 69 74 76 65 63 5f 73 65 74 5f 72 65 to bitvec_set_re
104e9 68 61 73 68 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a hash;. }. }.
104ea 20 20 2f 2a 20 74 68 65 72 65 20 77 61 73 20 61 /* there was a
104eb 20 63 6f 6c 6c 69 73 69 6f 6e 2c 20 63 68 65 63 collision, chec
104ec 6b 20 74 6f 20 73 65 65 20 69 66 20 69 74 27 73 k to see if it's
104ed 20 61 6c 72 65 61 64 79 20 2a 2f 0a 20 20 2f 2a already */. /*
104ee 20 69 6e 20 68 61 73 68 2c 20 69 66 20 6e 6f 74 in hash, if not
104ef 2c 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 20 , try to find a
104f0 73 70 6f 74 20 66 6f 72 20 69 74 20 2a 2f 0a 20 spot for it */.
104f1 20 64 6f 20 7b 0a 20 20 20 20 69 66 28 20 70 2d do {. if( p-
104f2 3e 75 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 >u.aHash[h]==i )
104f3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
104f4 4b 3b 0a 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 K;. h++;.
104f5 69 66 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 if( h>=BITVEC_NI
104f6 4e 54 20 29 20 68 20 3d 20 30 3b 0a 20 20 7d 20 NT ) h = 0;. }
104f7 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 while( p->u.aHas
104f8 68 5b 68 5d 20 29 3b 0a 20 20 2f 2a 20 77 65 20 h[h] );. /* we
104f9 64 69 64 6e 27 74 20 66 69 6e 64 20 69 74 20 69 didn't find it i
104fa 6e 20 74 68 65 20 68 61 73 68 2e 20 20 68 20 70 n the hash. h p
104fb 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 oints to the fir
104fc 73 74 20 2a 2f 0a 20 20 2f 2a 20 61 76 61 69 6c st */. /* avail
104fd 61 62 6c 65 20 66 72 65 65 20 73 70 6f 74 2e 20 able free spot.
104fe 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 check to see if
104ff 74 68 69 73 20 69 73 20 67 6f 69 6e 67 20 74 6f this is going to
10500 20 2a 2f 0a 20 20 2f 2a 20 6d 61 6b 65 20 6f 75 */. /* make ou
10501 72 20 68 61 73 68 20 74 6f 6f 20 22 66 75 6c 6c r hash too "full
10502 22 2e 20 20 2a 2f 0a 62 69 74 76 65 63 5f 73 65 ". */.bitvec_se
10503 74 5f 72 65 68 61 73 68 3a 0a 20 20 69 66 28 20 t_rehash:. if(
10504 70 2d 3e 6e 53 65 74 3e 3d 42 49 54 56 45 43 5f p->nSet>=BITVEC_
10505 4d 58 48 41 53 48 20 29 7b 0a 20 20 20 20 75 6e MXHASH ){. un
10506 73 69 67 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 signed int j;.
10507 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 75 33 int rc;. u3
10508 32 20 2a 61 69 56 61 6c 75 65 73 20 3d 20 73 71 2 *aiValues = sq
10509 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 lite3StackAllocR
1050a 61 77 28 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e aw(0, sizeof(p->
1050b 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 20 20 69 u.aHash));. i
1050c 66 28 20 61 69 56 61 6c 75 65 73 3d 3d 30 20 29 f( aiValues==0 )
1050d 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
1050e 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
1050f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 }else{. me
10510 6d 63 70 79 28 61 69 56 61 6c 75 65 73 2c 20 70 mcpy(aiValues, p
10511 2d 3e 75 2e 61 48 61 73 68 2c 20 73 69 7a 65 6f ->u.aHash, sizeo
10512 66 28 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a f(p->u.aHash));.
10513 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 2d 3e memset(p->
10514 75 2e 61 70 53 75 62 2c 20 30 2c 20 73 69 7a 65 u.apSub, 0, size
10515 6f 66 28 70 2d 3e 75 2e 61 70 53 75 62 29 29 3b of(p->u.apSub));
10516 0a 20 20 20 20 20 20 70 2d 3e 69 44 69 76 69 73 . p->iDivis
10517 6f 72 20 3d 20 28 70 2d 3e 69 53 69 7a 65 20 2b or = (p->iSize +
10518 20 42 49 54 56 45 43 5f 4e 50 54 52 20 2d 20 31 BITVEC_NPTR - 1
10519 29 2f 42 49 54 56 45 43 5f 4e 50 54 52 3b 0a 20 )/BITVEC_NPTR;.
1051a 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1051b 33 42 69 74 76 65 63 53 65 74 28 70 2c 20 69 29 3BitvecSet(p, i)
1051c 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b ;. for(j=0;
1051d 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 j<BITVEC_NINT;
1051e 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 j++){. if
1051f 28 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20 29 20 ( aiValues[j] )
10520 72 63 20 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 rc |= sqlite3Bit
10521 76 65 63 53 65 74 28 70 2c 20 61 69 56 61 6c 75 vecSet(p, aiValu
10522 65 73 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 7d 0a es[j]);. }.
10523 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 sqlite3Sta
10524 63 6b 46 72 65 65 28 30 2c 20 61 69 56 61 6c 75 ckFree(0, aiValu
10525 65 73 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 es);. retur
10526 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a n rc;. }. }.
10527 62 69 74 76 65 63 5f 73 65 74 5f 65 6e 64 3a 0a bitvec_set_end:.
10528 20 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 70 p->nSet++;. p
10529 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 69 ->u.aHash[h] = i
1052a 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
1052b 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
1052c 6c 65 61 72 20 74 68 65 20 69 2d 74 68 20 62 69 lear the i-th bi
1052d 74 2e 0a 2a 2a 0a 2a 2a 20 70 42 75 66 20 6d 75 t..**.** pBuf mu
1052e 73 74 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 st be a pointer
1052f 74 6f 20 61 74 20 6c 65 61 73 74 20 42 49 54 56 to at least BITV
10530 45 43 5f 53 5a 20 62 79 74 65 73 20 6f 66 20 74 EC_SZ bytes of t
10531 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 emporary storage
10532 0a 2a 2a 20 74 68 61 74 20 42 69 74 76 65 63 43 .** that BitvecC
10533 6c 65 61 72 20 63 61 6e 20 75 73 65 20 74 6f 20 lear can use to
10534 72 65 62 75 69 6c 74 20 69 74 73 20 68 61 73 68 rebuilt its hash
10535 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 table..*/.SQLIT
10536 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
10537 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 qlite3BitvecClea
10538 72 28 42 69 74 76 65 63 20 2a 70 2c 20 75 33 32 r(Bitvec *p, u32
10539 20 69 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b i, void *pBuf){
1053a 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
1053b 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 turn;. assert(
1053c 69 3e 30 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20 20 i>0 );. i--;.
1053d 77 68 69 6c 65 28 20 70 2d 3e 69 44 69 76 69 73 while( p->iDivis
1053e 6f 72 20 29 7b 0a 20 20 20 20 75 33 32 20 62 69 or ){. u32 bi
1053f 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f n = i/p->iDiviso
10540 72 3b 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e r;. i = i%p->
10541 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 70 20 iDivisor;. p
10542 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e = p->u.apSub[bin
10543 5d 3b 0a 20 20 20 20 69 66 20 28 21 70 29 20 7b ];. if (!p) {
10544 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 . return;.
10545 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 }. }. if( p
10546 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f ->iSize<=BITVEC_
10547 4e 42 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 NBIT ){. p->u
10548 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 .aBitmap[i/BITVE
10549 43 5f 53 5a 45 4c 45 4d 5d 20 26 3d 20 7e 28 31 C_SZELEM] &= ~(1
1054a 20 3c 3c 20 28 69 26 28 42 49 54 56 45 43 5f 53 << (i&(BITVEC_S
1054b 5a 45 4c 45 4d 2d 31 29 29 29 3b 0a 20 20 7d 65 ZELEM-1)));. }e
1054c 6c 73 65 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 lse{. unsigne
1054d 64 20 69 6e 74 20 6a 3b 0a 20 20 20 20 75 33 32 d int j;. u32
1054e 20 2a 61 69 56 61 6c 75 65 73 20 3d 20 70 42 75 *aiValues = pBu
1054f 66 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 69 f;. memcpy(ai
10550 56 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 Values, p->u.aHa
10551 73 68 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e sh, sizeof(p->u.
10552 61 48 61 73 68 29 29 3b 0a 20 20 20 20 6d 65 6d aHash));. mem
10553 73 65 74 28 70 2d 3e 75 2e 61 48 61 73 68 2c 20 set(p->u.aHash,
10554 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 0, sizeof(p->u.a
10555 48 61 73 68 29 29 3b 0a 20 20 20 20 70 2d 3e 6e Hash));. p->n
10556 53 65 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 Set = 0;. for
10557 28 6a 3d 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e (j=0; j<BITVEC_N
10558 49 4e 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 INT; j++){.
10559 20 69 66 28 20 61 69 56 61 6c 75 65 73 5b 6a 5d if( aiValues[j]
1055a 20 26 26 20 61 69 56 61 6c 75 65 73 5b 6a 5d 21 && aiValues[j]!
1055b 3d 28 69 2b 31 29 20 29 7b 0a 20 20 20 20 20 20 =(i+1) ){.
1055c 20 20 75 33 32 20 68 20 3d 20 42 49 54 56 45 43 u32 h = BITVEC
1055d 5f 48 41 53 48 28 61 69 56 61 6c 75 65 73 5b 6a _HASH(aiValues[j
1055e 5d 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 70 2d ]-1);. p-
1055f 3e 6e 53 65 74 2b 2b 3b 0a 20 20 20 20 20 20 20 >nSet++;.
10560 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 while( p->u.aHa
10561 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 20 20 20 sh[h] ){.
10562 20 20 20 68 2b 2b 3b 0a 20 20 20 20 20 20 20 20 h++;.
10563 20 20 69 66 28 20 68 3e 3d 42 49 54 56 45 43 5f if( h>=BITVEC_
10564 4e 49 4e 54 20 29 20 68 20 3d 20 30 3b 0a 20 20 NINT ) h = 0;.
10565 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
10566 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 p->u.aHash[h] =
10567 61 69 56 61 6c 75 65 73 5b 6a 5d 3b 0a 20 20 20 aiValues[j];.
10568 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d }. }. }.}
10569 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 ../*.** Destroy
1056a 61 20 62 69 74 6d 61 70 20 6f 62 6a 65 63 74 2e a bitmap object.
1056b 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 Reclaim all me
1056c 6d 6f 72 79 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 mory used..*/.SQ
1056d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1056e 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 d sqlite3BitvecD
1056f 65 73 74 72 6f 79 28 42 69 74 76 65 63 20 2a 70 estroy(Bitvec *p
10570 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 ){. if( p==0 )
10571 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d return;. if( p-
10572 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 >iDivisor ){.
10573 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b unsigned int i;
10574 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
10575 42 49 54 56 45 43 5f 4e 50 54 52 3b 20 69 2b 2b BITVEC_NPTR; i++
10576 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
10577 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 2d BitvecDestroy(p-
10578 3e 75 2e 61 70 53 75 62 5b 69 5d 29 3b 0a 20 20 >u.apSub[i]);.
10579 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 }. }. sqlite
1057a 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 3_free(p);.}../*
1057b 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 .** Return the v
1057c 61 6c 75 65 20 6f 66 20 74 68 65 20 69 53 69 7a alue of the iSiz
1057d 65 20 70 61 72 61 6d 65 74 65 72 20 73 70 65 63 e parameter spec
1057e 69 66 69 65 64 20 77 68 65 6e 20 42 69 74 76 65 ified when Bitve
1057f 63 20 2a 70 0a 2a 2a 20 77 61 73 20 63 72 65 61 c *p.** was crea
10580 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ted..*/.SQLITE_P
10581 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 RIVATE u32 sqlit
10582 65 33 42 69 74 76 65 63 53 69 7a 65 28 42 69 74 e3BitvecSize(Bit
10583 76 65 63 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 vec *p){. retur
10584 6e 20 70 2d 3e 69 53 69 7a 65 3b 0a 7d 0a 0a 23 n p->iSize;.}..#
10585 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
10586 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a IT_BUILTIN_TEST.
10587 2f 2a 0a 2a 2a 20 4c 65 74 20 56 5b 5d 20 62 65 /*.** Let V[] be
10588 20 61 6e 20 61 72 72 61 79 20 6f 66 20 75 6e 73 an array of uns
10589 69 67 6e 65 64 20 63 68 61 72 61 63 74 65 72 73 igned characters
1058a 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20 68 sufficient to h
1058b 6f 6c 64 0a 2a 2a 20 75 70 20 74 6f 20 4e 20 62 old.** up to N b
1058c 69 74 73 2e 20 20 4c 65 74 20 49 20 62 65 20 61 its. Let I be a
1058d 6e 20 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 n integer betwee
1058e 6e 20 30 20 61 6e 64 20 4e 2e 20 20 30 3c 3d 49 n 0 and N. 0<=I
1058f 3c 4e 2e 0a 2a 2a 20 54 68 65 6e 20 74 68 65 20 <N..** Then the
10590 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 following macros
10591 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 can be used to
10592 73 65 74 2c 20 63 6c 65 61 72 2c 20 6f 72 20 74 set, clear, or t
10593 65 73 74 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 est.** individua
10594 6c 20 62 69 74 73 20 77 69 74 68 69 6e 20 56 2e l bits within V.
10595 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 45 54 42 .*/.#define SETB
10596 49 54 28 56 2c 49 29 20 20 20 20 20 20 56 5b 49 IT(V,I) V[I
10597 3e 3e 33 5d 20 7c 3d 20 28 31 3c 3c 28 49 26 37 >>3] |= (1<<(I&7
10598 29 29 0a 23 64 65 66 69 6e 65 20 43 4c 45 41 52 )).#define CLEAR
10599 42 49 54 28 56 2c 49 29 20 20 20 20 56 5b 49 3e BIT(V,I) V[I>
1059a 3e 33 5d 20 26 3d 20 7e 28 31 3c 3c 28 49 26 37 >3] &= ~(1<<(I&7
1059b 29 29 0a 23 64 65 66 69 6e 65 20 54 45 53 54 42 )).#define TESTB
1059c 49 54 28 56 2c 49 29 20 20 20 20 20 28 56 5b 49 IT(V,I) (V[I
1059d 3e 3e 33 5d 26 28 31 3c 3c 28 49 26 37 29 29 29 >>3]&(1<<(I&7)))
1059e 21 3d 30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 !=0../*.** This
1059f 72 6f 75 74 69 6e 65 20 72 75 6e 73 20 61 6e 20 routine runs an
105a0 65 78 74 65 6e 73 69 76 65 20 74 65 73 74 20 6f extensive test o
105a1 66 20 74 68 65 20 42 69 74 76 65 63 20 63 6f 64 f the Bitvec cod
105a2 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 e..**.** The inp
105a3 75 74 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f ut is an array o
105a4 66 20 69 6e 74 65 67 65 72 73 20 74 68 61 74 20 f integers that
105a5 61 63 74 73 20 61 73 20 61 20 70 72 6f 67 72 61 acts as a progra
105a6 6d 0a 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 m.** to test the
105a7 20 42 69 74 76 65 63 2e 20 20 54 68 65 20 69 6e Bitvec. The in
105a8 74 65 67 65 72 73 20 61 72 65 20 6f 70 63 6f 64 tegers are opcod
105a9 65 73 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62 es followed.** b
105aa 79 20 30 2c 20 31 2c 20 6f 72 20 33 20 6f 70 65 y 0, 1, or 3 ope
105ab 72 61 6e 64 73 2c 20 64 65 70 65 6e 64 69 6e 67 rands, depending
105ac 20 6f 6e 20 74 68 65 20 6f 70 63 6f 64 65 2e 20 on the opcode.
105ad 20 41 6e 6f 74 68 65 72 0a 2a 2a 20 6f 70 63 6f Another.** opco
105ae 64 65 20 66 6f 6c 6c 6f 77 73 20 69 6d 6d 65 64 de follows immed
105af 69 61 74 65 6c 79 20 61 66 74 65 72 20 74 68 65 iately after the
105b0 20 6c 61 73 74 20 6f 70 65 72 61 6e 64 2e 0a 2a last operand..*
105b1 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 36 *.** There are 6
105b2 20 6f 70 63 6f 64 65 73 20 6e 75 6d 62 65 72 65 opcodes numbere
105b3 64 20 66 72 6f 6d 20 30 20 74 68 72 6f 75 67 68 d from 0 through
105b4 20 35 2e 20 20 30 20 69 73 20 74 68 65 0a 2a 2a 5. 0 is the.**
105b5 20 22 68 61 6c 74 22 20 6f 70 63 6f 64 65 20 61 "halt" opcode a
105b6 6e 64 20 63 61 75 73 65 73 20 74 68 65 20 74 65 nd causes the te
105b7 73 74 20 74 6f 20 65 6e 64 2e 0a 2a 2a 0a 2a 2a st to end..**.**
105b8 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 48 0 H
105b9 61 6c 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74 alt and return t
105ba 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 he number of err
105bb 6f 72 73 0a 2a 2a 20 20 20 20 31 20 4e 20 53 20 ors.** 1 N S
105bc 58 20 20 20 20 53 65 74 20 4e 20 62 69 74 73 20 X Set N bits
105bd 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 beginning with S
105be 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 69 6e and incrementin
105bf 67 20 62 79 20 58 0a 2a 2a 20 20 20 20 32 20 4e g by X.** 2 N
105c0 20 53 20 58 20 20 20 20 43 6c 65 61 72 20 4e 20 S X Clear N
105c1 62 69 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 bits beginning w
105c2 69 74 68 20 53 20 61 6e 64 20 69 6e 63 72 65 6d ith S and increm
105c3 65 6e 74 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20 enting by X.**
105c4 20 20 33 20 4e 20 20 20 20 20 20 20 20 53 65 74 3 N Set
105c5 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 N randomly chos
105c6 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20 20 34 20 en bits.** 4
105c7 4e 20 20 20 20 20 20 20 20 43 6c 65 61 72 20 4e N Clear N
105c8 20 72 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e randomly chosen
105c9 20 62 69 74 73 0a 2a 2a 20 20 20 20 35 20 4e 20 bits.** 5 N
105ca 53 20 58 20 20 20 20 53 65 74 20 4e 20 62 69 74 S X Set N bit
105cb 73 20 66 72 6f 6d 20 53 20 69 6e 63 72 65 6d 65 s from S increme
105cc 6e 74 20 58 20 69 6e 20 61 72 72 61 79 20 6f 6e nt X in array on
105cd 6c 79 2c 20 6e 6f 74 20 69 6e 20 62 69 74 76 65 ly, not in bitve
105ce 63 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 63 6f c.**.** The opco
105cf 64 65 73 20 31 20 74 68 72 6f 75 67 68 20 34 20 des 1 through 4
105d0 70 65 72 66 6f 72 6d 20 73 65 74 20 61 6e 64 20 perform set and
105d1 63 6c 65 61 72 20 6f 70 65 72 61 74 69 6f 6e 73 clear operations
105d2 20 61 72 65 20 70 65 72 66 6f 72 6d 65 64 0a 2a are performed.*
105d3 2a 20 6f 6e 20 62 6f 74 68 20 61 20 42 69 74 76 * on both a Bitv
105d4 65 63 20 6f 62 6a 65 63 74 20 61 6e 64 20 6f 6e ec object and on
105d5 20 61 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 a linear array
105d6 6f 66 20 62 69 74 73 20 6f 62 74 61 69 6e 65 64 of bits obtained
105d7 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a from malloc..**
105d8 20 4f 70 63 6f 64 65 20 35 20 77 6f 72 6b 73 20 Opcode 5 works
105d9 6f 6e 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 on the linear ar
105da 72 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 6f 6e ray only, not on
105db 20 74 68 65 20 42 69 74 76 65 63 2e 0a 2a 2a 20 the Bitvec..**
105dc 4f 70 63 6f 64 65 20 35 20 69 73 20 75 73 65 64 Opcode 5 is used
105dd 20 74 6f 20 64 65 6c 69 62 65 72 61 74 65 6c 79 to deliberately
105de 20 69 6e 64 75 63 65 20 61 20 66 61 75 6c 74 20 induce a fault
105df 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 in order to.** c
105e0 6f 6e 66 69 72 6d 20 74 68 61 74 20 65 72 72 6f onfirm that erro
105e1 72 20 64 65 74 65 63 74 69 6f 6e 20 77 6f 72 6b r detection work
105e2 73 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20 s..**.** At the
105e3 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 74 68 conclusion of th
105e4 65 20 74 65 73 74 20 74 68 65 20 6c 69 6e 65 61 e test the linea
105e5 72 20 61 72 72 61 79 20 69 73 20 63 6f 6d 70 61 r array is compa
105e6 72 65 64 0a 2a 2a 20 61 67 61 69 6e 73 74 20 74 red.** against t
105e7 68 65 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 he Bitvec object
105e8 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20 . If there are
105e9 61 6e 79 20 64 69 66 66 65 72 65 6e 63 65 73 2c any differences,
105ea 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 .** an error is
105eb 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68 returned. If th
105ec 65 79 20 61 72 65 20 74 68 65 20 73 61 6d 65 2c ey are the same,
105ed 20 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 zero is returne
105ee 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 d..**.** If a me
105ef 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
105f0 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 error occurs, re
105f1 74 75 72 6e 20 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 turn -1..*/.SQLI
105f2 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
105f3 71 6c 69 74 65 33 42 69 74 76 65 63 42 75 69 6c qlite3BitvecBuil
105f4 74 69 6e 54 65 73 74 28 69 6e 74 20 73 7a 2c 20 tinTest(int sz,
105f5 69 6e 74 20 2a 61 4f 70 29 7b 0a 20 20 42 69 74 int *aOp){. Bit
105f6 76 65 63 20 2a 70 42 69 74 76 65 63 20 3d 20 30 vec *pBitvec = 0
105f7 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 ;. unsigned cha
105f8 72 20 2a 70 56 20 3d 20 30 3b 0a 20 20 69 6e 74 r *pV = 0;. int
105f9 20 72 63 20 3d 20 2d 31 3b 0a 20 20 69 6e 74 20 rc = -1;. int
105fa 69 2c 20 6e 78 2c 20 70 63 2c 20 6f 70 3b 0a 20 i, nx, pc, op;.
105fb 20 76 6f 69 64 20 2a 70 54 6d 70 53 70 61 63 65 void *pTmpSpace
105fc 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 ;.. /* Allocate
105fd 20 74 68 65 20 42 69 74 76 65 63 20 74 6f 20 62 the Bitvec to b
105fe 65 20 74 65 73 74 65 64 20 61 6e 64 20 61 20 6c e tested and a l
105ff 69 6e 65 61 72 20 61 72 72 61 79 20 6f 66 0a 20 inear array of.
10600 20 2a 2a 20 62 69 74 73 20 74 6f 20 61 63 74 20 ** bits to act
10601 61 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 as the reference
10602 20 2a 2f 0a 20 20 70 42 69 74 76 65 63 20 3d 20 */. pBitvec =
10603 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 sqlite3BitvecCre
10604 61 74 65 28 20 73 7a 20 29 3b 0a 20 20 70 56 20 ate( sz );. pV
10605 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 = sqlite3_malloc
10606 28 20 28 73 7a 2b 37 29 2f 38 20 2b 20 31 20 29 ( (sz+7)/8 + 1 )
10607 3b 0a 20 20 70 54 6d 70 53 70 61 63 65 20 3d 20 ;. pTmpSpace =
10608 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 42 sqlite3_malloc(B
10609 49 54 56 45 43 5f 53 5a 29 3b 0a 20 20 69 66 28 ITVEC_SZ);. if(
1060a 20 70 42 69 74 76 65 63 3d 3d 30 20 7c 7c 20 70 pBitvec==0 || p
1060b 56 3d 3d 30 20 7c 7c 20 70 54 6d 70 53 70 61 63 V==0 || pTmpSpac
1060c 65 3d 3d 30 20 20 29 20 67 6f 74 6f 20 62 69 74 e==0 ) goto bit
1060d 76 65 63 5f 65 6e 64 3b 0a 20 20 6d 65 6d 73 65 vec_end;. memse
1060e 74 28 70 56 2c 20 30 2c 20 28 73 7a 2b 37 29 2f t(pV, 0, (sz+7)/
1060f 38 20 2b 20 31 29 3b 0a 0a 20 20 2f 2a 20 4e 55 8 + 1);.. /* NU
10610 4c 4c 20 70 42 69 74 76 65 63 20 74 65 73 74 73 LL pBitvec tests
10611 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 69 74 */. sqlite3Bit
10612 76 65 63 53 65 74 28 30 2c 20 31 29 3b 0a 20 20 vecSet(0, 1);.
10613 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 sqlite3BitvecCle
10614 61 72 28 30 2c 20 31 2c 20 70 54 6d 70 53 70 61 ar(0, 1, pTmpSpa
10615 63 65 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20 74 ce);.. /* Run t
10616 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 he program */.
10617 70 63 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 pc = 0;. while(
10618 20 28 6f 70 20 3d 20 61 4f 70 5b 70 63 5d 29 21 (op = aOp[pc])!
10619 3d 30 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 =0 ){. switch
1061a 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61 ( op ){. ca
1061b 73 65 20 31 3a 0a 20 20 20 20 20 20 63 61 73 65 se 1:. case
1061c 20 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20 35 2:. case 5
1061d 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d : {. nx =
1061e 20 34 3b 0a 20 20 20 20 20 20 20 20 69 20 3d 20 4;. i =
1061f 61 4f 70 5b 70 63 2b 32 5d 20 2d 20 31 3b 0a 20 aOp[pc+2] - 1;.
10620 20 20 20 20 20 20 20 61 4f 70 5b 70 63 2b 32 5d aOp[pc+2]
10621 20 2b 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b 0a 20 += aOp[pc+3];.
10622 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
10623 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 }. case
10624 20 33 3a 0a 20 20 20 20 20 20 63 61 73 65 20 34 3:. case 4
10625 3a 20 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 : . default
10626 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d : {. nx =
10627 20 32 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 2;. sqli
10628 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 te3_randomness(s
10629 69 7a 65 6f 66 28 69 29 2c 20 26 69 29 3b 0a 20 izeof(i), &i);.
1062a 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1062b 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
1062c 69 66 28 20 28 2d 2d 61 4f 70 5b 70 63 2b 31 5d if( (--aOp[pc+1]
1062d 29 20 3e 20 30 20 29 20 6e 78 20 3d 20 30 3b 0a ) > 0 ) nx = 0;.
1062e 20 20 20 20 70 63 20 2b 3d 20 6e 78 3b 0a 20 20 pc += nx;.
1062f 20 20 69 20 3d 20 28 69 20 26 20 30 78 37 66 66 i = (i & 0x7ff
10630 66 66 66 66 66 29 25 73 7a 3b 0a 20 20 20 20 69 fffff)%sz;. i
10631 66 28 20 28 6f 70 20 26 20 31 29 21 3d 30 20 29 f( (op & 1)!=0 )
10632 7b 0a 20 20 20 20 20 20 53 45 54 42 49 54 28 70 {. SETBIT(p
10633 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 V, (i+1));.
10634 20 69 66 28 20 6f 70 21 3d 35 20 29 7b 0a 20 20 if( op!=5 ){.
10635 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
10636 33 42 69 74 76 65 63 53 65 74 28 70 42 69 74 76 3BitvecSet(pBitv
10637 65 63 2c 20 69 2b 31 29 20 29 20 67 6f 74 6f 20 ec, i+1) ) goto
10638 62 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20 20 20 bitvec_end;.
10639 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1063a 20 20 20 20 20 43 4c 45 41 52 42 49 54 28 70 56 CLEARBIT(pV
1063b 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20 , (i+1));.
1063c 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 sqlite3BitvecCle
1063d 61 72 28 70 42 69 74 76 65 63 2c 20 69 2b 31 2c ar(pBitvec, i+1,
1063e 20 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 pTmpSpace);.
1063f 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 }. }.. /* Tes
10640 74 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 t to make sure t
10641 68 65 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 he linear array
10642 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65 73 20 exactly matches
10643 74 68 65 0a 20 20 2a 2a 20 42 69 74 76 65 63 20 the. ** Bitvec
10644 6f 62 6a 65 63 74 2e 20 20 53 74 61 72 74 20 77 object. Start w
10645 69 74 68 20 74 68 65 20 61 73 73 75 6d 70 74 69 ith the assumpti
10646 6f 6e 20 74 68 61 74 20 74 68 65 79 20 64 6f 0a on that they do.
10647 20 20 2a 2a 20 6d 61 74 63 68 20 28 72 63 3d 3d ** match (rc==
10648 30 29 2e 20 20 43 68 61 6e 67 65 20 72 63 20 74 0). Change rc t
10649 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 o non-zero if a
1064a 64 69 73 63 72 65 70 61 6e 63 79 0a 20 20 2a 2a discrepancy. **
1064b 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a is found.. */.
1064c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 rc = sqlite3Bi
1064d 74 76 65 63 54 65 73 74 28 30 2c 30 29 20 2b 20 tvecTest(0,0) +
1064e 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 sqlite3BitvecTes
1064f 74 28 70 42 69 74 76 65 63 2c 20 73 7a 2b 31 29 t(pBitvec, sz+1)
10650 0a 20 20 20 20 20 20 20 20 20 20 2b 20 73 71 6c . + sql
10651 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 ite3BitvecTest(p
10652 42 69 74 76 65 63 2c 20 30 29 0a 20 20 20 20 20 Bitvec, 0).
10653 20 20 20 20 20 2b 20 28 73 71 6c 69 74 65 33 42 + (sqlite3B
10654 69 74 76 65 63 53 69 7a 65 28 70 42 69 74 76 65 itvecSize(pBitve
10655 63 29 20 2d 20 73 7a 29 3b 0a 20 20 66 6f 72 28 c) - sz);. for(
10656 69 3d 31 3b 20 69 3c 3d 73 7a 3b 20 69 2b 2b 29 i=1; i<=sz; i++)
10657 7b 0a 20 20 20 20 69 66 28 20 20 28 54 45 53 54 {. if( (TEST
10658 42 49 54 28 70 56 2c 69 29 29 21 3d 73 71 6c 69 BIT(pV,i))!=sqli
10659 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 42 te3BitvecTest(pB
1065a 69 74 76 65 63 2c 69 29 20 29 7b 0a 20 20 20 20 itvec,i) ){.
1065b 20 20 72 63 20 3d 20 69 3b 0a 20 20 20 20 20 20 rc = i;.
1065c 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d break;. }. }
1065d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6c 6c 6f .. /* Free allo
1065e 63 61 74 65 64 20 73 74 72 75 63 74 75 72 65 20 cated structure
1065f 2a 2f 0a 62 69 74 76 65 63 5f 65 6e 64 3a 0a 20 */.bitvec_end:.
10660 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 sqlite3_free(pT
10661 6d 70 53 70 61 63 65 29 3b 0a 20 20 73 71 6c 69 mpSpace);. sqli
10662 74 65 33 5f 66 72 65 65 28 70 56 29 3b 0a 20 20 te3_free(pV);.
10663 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 sqlite3BitvecDes
10664 74 72 6f 79 28 70 42 69 74 76 65 63 29 3b 0a 20 troy(pBitvec);.
10665 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 return rc;.}.#e
10666 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
10667 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 MIT_BUILTIN_TEST
10668 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
10669 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 69 74 76 **** End of bitv
1066a 65 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ec.c ***********
1066b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1066c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1066d 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
1066e 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
1066f 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a pcache.c *******
10670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10671 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10672 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 ***/./*.** 2008
10673 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 August 05.**.**
10674 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
10675 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
10676 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
10677 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
10678 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
10679 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
1067a 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
1067b 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
1067c 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
1067d 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
1067e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
1067f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
10680 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
10681 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
10682 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
10683 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
10684 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
10685 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10686 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10687 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10688 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10689 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
1068a 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 file implements
1068b 20 74 68 61 74 20 70 61 67 65 20 63 61 63 68 65 that page cache
1068c 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 ..**.** @(#) $Id
1068d 3a 20 70 63 61 63 68 65 2e 63 2c 76 20 31 2e 34 : pcache.c,v 1.4
1068e 37 20 32 30 30 39 2f 30 37 2f 32 35 20 31 31 3a 7 2009/07/25 11:
1068f 34 36 3a 34 39 20 64 61 6e 69 65 6c 6b 31 39 37 46:49 danielk197
10690 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 7 Exp $.*/../*.*
10691 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20 70 61 67 * A complete pag
10692 65 20 63 61 63 68 65 20 69 73 20 61 6e 20 69 6e e cache is an in
10693 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 stance of this s
10694 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 tructure..*/.str
10695 75 63 74 20 50 43 61 63 68 65 20 7b 0a 20 20 50 uct PCache {. P
10696 67 48 64 72 20 2a 70 44 69 72 74 79 2c 20 2a 70 gHdr *pDirty, *p
10697 44 69 72 74 79 54 61 69 6c 3b 20 20 20 20 20 20 DirtyTail;
10698 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 /* List of di
10699 72 74 79 20 70 61 67 65 73 20 69 6e 20 4c 52 55 rty pages in LRU
1069a 20 6f 72 64 65 72 20 2a 2f 0a 20 20 50 67 48 64 order */. PgHd
1069b 72 20 2a 70 53 79 6e 63 65 64 3b 20 20 20 20 20 r *pSynced;
1069c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1069d 2f 2a 20 4c 61 73 74 20 73 79 6e 63 65 64 20 70 /* Last synced p
1069e 61 67 65 20 69 6e 20 64 69 72 74 79 20 70 61 67 age in dirty pag
1069f 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 e list */. int
106a0 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 nRef;
106a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106a2 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 /* Number of ref
106a3 65 72 65 6e 63 65 64 20 70 61 67 65 73 20 2a 2f erenced pages */
106a4 0a 20 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 . int nMax;
106a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106a6 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 /* Config
106a7 75 72 65 64 20 63 61 63 68 65 20 73 69 7a 65 20 ured cache size
106a8 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b */. int szPage;
106a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106aa 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
106ab 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 69 of every page i
106ac 6e 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a n this cache */.
106ad 20 20 69 6e 74 20 73 7a 45 78 74 72 61 3b 20 20 int szExtra;
106ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106af 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
106b0 20 65 78 74 72 61 20 73 70 61 63 65 20 66 6f 72 extra space for
106b1 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 each page */.
106b2 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20 int bPurgeable;
106b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106b4 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 /* True if p
106b5 61 67 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b ages are on back
106b6 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 ing store */. i
106b7 6e 74 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f nt (*xStress)(vo
106b8 69 64 2a 2c 50 67 48 64 72 2a 29 3b 20 20 20 20 id*,PgHdr*);
106b9 20 20 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 /* Call to tr
106ba 79 20 6d 61 6b 65 20 61 20 70 61 67 65 20 63 6c y make a page cl
106bb 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ean */. void *p
106bc 53 74 72 65 73 73 3b 20 20 20 20 20 20 20 20 20 Stress;
106bd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
106be 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 Argument to xStr
106bf 65 73 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ess */. sqlite3
106c0 5f 70 63 61 63 68 65 20 2a 70 43 61 63 68 65 3b _pcache *pCache;
106c1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
106c2 50 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 Pluggable cache
106c3 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 50 67 48 64 module */. PgHd
106c4 72 20 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 r *pPage1;
106c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106c6 2f 2a 20 52 65 66 65 72 65 6e 63 65 20 74 6f 20 /* Reference to
106c7 70 61 67 65 20 31 20 2a 2f 0a 7d 3b 0a 0a 2f 2a page 1 */.};../*
106c8 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 .** Some of the
106c9 61 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 20 assert() macros
106ca 69 6e 20 74 68 69 73 20 63 6f 64 65 20 61 72 65 in this code are
106cb 20 74 6f 6f 20 65 78 70 65 6e 73 69 76 65 20 74 too expensive t
106cc 6f 20 72 75 6e 0a 2a 2a 20 65 76 65 6e 20 64 75 o run.** even du
106cd 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 64 65 62 75 ring normal debu
106ce 67 67 69 6e 67 2e 20 20 55 73 65 20 74 68 65 6d gging. Use them
106cf 20 6f 6e 6c 79 20 72 61 72 65 6c 79 20 6f 6e 20 only rarely on
106d0 6c 6f 6e 67 2d 72 75 6e 6e 69 6e 67 0a 2a 2a 20 long-running.**
106d1 74 65 73 74 73 2e 20 20 45 6e 61 62 6c 65 20 74 tests. Enable t
106d2 68 65 20 65 78 70 65 6e 73 69 76 65 20 61 73 73 he expensive ass
106d3 65 72 74 73 20 75 73 69 6e 67 20 74 68 65 0a 2a erts using the.*
106d4 2a 20 2d 44 53 51 4c 49 54 45 5f 45 4e 41 42 4c * -DSQLITE_ENABL
106d5 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 E_EXPENSIVE_ASSE
106d6 52 54 3d 31 20 63 6f 6d 70 69 6c 65 2d 74 69 6d RT=1 compile-tim
106d7 65 20 6f 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 e option..*/.#if
106d8 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
106d9 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 E_EXPENSIVE_ASSE
106da 52 54 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65 RT.# define expe
106db 6e 73 69 76 65 5f 61 73 73 65 72 74 28 58 29 20 nsive_assert(X)
106dc 20 61 73 73 65 72 74 28 58 29 0a 23 65 6c 73 65 assert(X).#else
106dd 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65 6e 73 .# define expens
106de 69 76 65 5f 61 73 73 65 72 74 28 58 29 0a 23 65 ive_assert(X).#e
106df 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ndif../*********
106e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4c 69 6e 6b 65 64 ********* Linked
106e2 20 4c 69 73 74 20 4d 61 6e 61 67 65 6d 65 6e 74 List Management
106e3 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
106e4 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 *****/..#if !def
106e5 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 ined(NDEBUG) &&
106e6 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 defined(SQLITE_E
106e7 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f NABLE_EXPENSIVE_
106e8 41 53 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20 43 68 ASSERT)./*.** Ch
106e9 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 43 61 eck that the pCa
106ea 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 76 61 72 che->pSynced var
106eb 69 61 62 6c 65 20 69 73 20 73 65 74 20 63 6f 72 iable is set cor
106ec 72 65 63 74 6c 79 2e 20 49 66 20 69 74 0a 2a 2a rectly. If it.**
106ed 20 69 73 20 6e 6f 74 2c 20 65 69 74 68 65 72 20 is not, either
106ee 66 61 69 6c 20 61 6e 20 61 73 73 65 72 74 20 6f fail an assert o
106ef 72 20 72 65 74 75 72 6e 20 7a 65 72 6f 2e 20 4f r return zero. O
106f0 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e therwise, return
106f1 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 54 68 .** non-zero. Th
106f2 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 is is only used
106f3 69 6e 20 64 65 62 75 67 67 69 6e 67 20 62 75 69 in debugging bui
106f4 6c 64 73 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a lds, as follows:
106f5 0a 2a 2a 0a 2a 2a 20 20 20 65 78 70 65 6e 73 69 .**.** expensi
106f6 76 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 ve_assert( pcach
106f7 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70 43 61 eCheckSynced(pCa
106f8 63 68 65 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 che) );.*/.stati
106f9 63 20 69 6e 74 20 70 63 61 63 68 65 43 68 65 63 c int pcacheChec
106fa 6b 53 79 6e 63 65 64 28 50 43 61 63 68 65 20 2a kSynced(PCache *
106fb 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72 pCache){. PgHdr
106fc 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 *p;. for(p=pCa
106fd 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b che->pDirtyTail;
106fe 20 70 21 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e p!=pCache->pSyn
106ff 63 65 64 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 ced; p=p->pDirty
10700 50 72 65 76 29 7b 0a 20 20 20 20 61 73 73 65 72 Prev){. asser
10701 74 28 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 t( p->nRef || (p
10702 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 ->flags&PGHDR_NE
10703 45 44 5f 53 59 4e 43 29 20 29 3b 0a 20 20 7d 0a ED_SYNC) );. }.
10704 20 20 72 65 74 75 72 6e 20 28 70 3d 3d 30 20 7c return (p==0 |
10705 7c 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d | p->nRef || (p-
10706 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
10707 44 5f 53 59 4e 43 29 3d 3d 30 29 3b 0a 7d 0a 23 D_SYNC)==0);.}.#
10708 65 6e 64 69 66 20 2f 2a 20 21 4e 44 45 42 55 47 endif /* !NDEBUG
10709 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
1070a 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 E_EXPENSIVE_ASSE
1070b 52 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d RT */../*.** Rem
1070c 6f 76 65 20 70 61 67 65 20 70 50 61 67 65 20 66 ove page pPage f
1070d 72 6f 6d 20 74 68 65 20 6c 69 73 74 20 6f 66 20 rom the list of
1070e 64 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a dirty pages..*/.
1070f 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 static void pcac
10710 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 heRemoveFromDirt
10711 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70 50 61 yList(PgHdr *pPa
10712 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 ge){. PCache *p
10713 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 = pPage->pCache
10714 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 ;.. assert( pPa
10715 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 7c ge->pDirtyNext |
10716 7c 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 | pPage==p->pDir
10717 74 79 54 61 69 6c 20 29 3b 0a 20 20 61 73 73 65 tyTail );. asse
10718 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 rt( pPage->pDirt
10719 79 50 72 65 76 20 7c 7c 20 70 50 61 67 65 3d 3d yPrev || pPage==
1071a 70 2d 3e 70 44 69 72 74 79 20 29 3b 0a 0a 20 20 p->pDirty );..
1071b 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 50 43 /* Update the PC
1071c 61 63 68 65 31 2e 70 53 79 6e 63 65 64 20 76 61 ache1.pSynced va
1071d 72 69 61 62 6c 65 20 69 66 20 6e 65 63 65 73 73 riable if necess
1071e 61 72 79 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d ary. */. if( p-
1071f 3e 70 53 79 6e 63 65 64 3d 3d 70 50 61 67 65 20 >pSynced==pPage
10720 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 53 ){. PgHdr *pS
10721 79 6e 63 65 64 20 3d 20 70 50 61 67 65 2d 3e 70 ynced = pPage->p
10722 44 69 72 74 79 50 72 65 76 3b 0a 20 20 20 20 77 DirtyPrev;. w
10723 68 69 6c 65 28 20 70 53 79 6e 63 65 64 20 26 26 hile( pSynced &&
10724 20 28 70 53 79 6e 63 65 64 2d 3e 66 6c 61 67 73 (pSynced->flags
10725 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
10726 29 20 29 7b 0a 20 20 20 20 20 20 70 53 79 6e 63 ) ){. pSync
10727 65 64 20 3d 20 70 53 79 6e 63 65 64 2d 3e 70 44 ed = pSynced->pD
10728 69 72 74 79 50 72 65 76 3b 0a 20 20 20 20 7d 0a irtyPrev;. }.
10729 20 20 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d p->pSynced =
1072a 20 70 53 79 6e 63 65 64 3b 0a 20 20 7d 0a 0a 20 pSynced;. }..
1072b 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 if( pPage->pDir
1072c 74 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 50 tyNext ){. pP
1072d 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d age->pDirtyNext-
1072e 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20 70 50 >pDirtyPrev = pP
1072f 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b age->pDirtyPrev;
10730 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 . }else{. as
10731 73 65 72 74 28 20 70 50 61 67 65 3d 3d 70 2d 3e sert( pPage==p->
10732 70 44 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20 pDirtyTail );.
10733 20 20 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 p->pDirtyTail
10734 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 = pPage->pDirtyP
10735 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 rev;. }. if( p
10736 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 Page->pDirtyPrev
10737 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 ){. pPage->p
10738 44 69 72 74 79 50 72 65 76 2d 3e 70 44 69 72 74 DirtyPrev->pDirt
10739 79 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 yNext = pPage->p
1073a 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 65 6c DirtyNext;. }el
1073b 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
1073c 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 pPage==p->pDirty
1073d 20 29 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 );. p->pDirt
1073e 79 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 y = pPage->pDirt
1073f 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 70 50 61 yNext;. }. pPa
10740 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d ge->pDirtyNext =
10741 20 30 3b 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 0;. pPage->pDi
10742 72 74 79 50 72 65 76 20 3d 20 30 3b 0a 0a 20 20 rtyPrev = 0;..
10743 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 expensive_assert
10744 28 20 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e ( pcacheCheckSyn
10745 63 65 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a ced(p) );.}../*.
10746 2a 2a 20 41 64 64 20 70 61 67 65 20 70 50 61 67 ** Add page pPag
10747 65 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 e to the head of
10748 20 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 20 the dirty list
10749 28 50 43 61 63 68 65 31 2e 70 44 69 72 74 79 20 (PCache1.pDirty
1074a 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 70 50 61 is set to.** pPa
1074b 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ge)..*/.static v
1074c 6f 69 64 20 70 63 61 63 68 65 41 64 64 54 6f 44 oid pcacheAddToD
1074d 69 72 74 79 4c 69 73 74 28 50 67 48 64 72 20 2a irtyList(PgHdr *
1074e 70 50 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65 pPage){. PCache
1074f 20 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 *p = pPage->pCa
10750 63 68 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 che;.. assert(
10751 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 pPage->pDirtyNex
10752 74 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 70 t==0 && pPage->p
10753 44 69 72 74 79 50 72 65 76 3d 3d 30 20 26 26 20 DirtyPrev==0 &&
10754 70 2d 3e 70 44 69 72 74 79 21 3d 70 50 61 67 65 p->pDirty!=pPage
10755 20 29 3b 0a 0a 20 20 70 50 61 67 65 2d 3e 70 44 );.. pPage->pD
10756 69 72 74 79 4e 65 78 74 20 3d 20 70 2d 3e 70 44 irtyNext = p->pD
10757 69 72 74 79 3b 0a 20 20 69 66 28 20 70 50 61 67 irty;. if( pPag
10758 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 29 7b e->pDirtyNext ){
10759 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
1075a 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e ge->pDirtyNext->
1075b 70 44 69 72 74 79 50 72 65 76 3d 3d 30 20 29 3b pDirtyPrev==0 );
1075c 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 69 72 . pPage->pDir
1075d 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 72 tyNext->pDirtyPr
1075e 65 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a ev = pPage;. }.
1075f 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 p->pDirty = pP
10760 61 67 65 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 age;. if( !p->p
10761 44 69 72 74 79 54 61 69 6c 20 29 7b 0a 20 20 20 DirtyTail ){.
10762 20 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d p->pDirtyTail =
10763 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 69 66 pPage;. }. if
10764 28 20 21 70 2d 3e 70 53 79 6e 63 65 64 20 26 26 ( !p->pSynced &&
10765 20 30 3d 3d 28 70 50 61 67 65 2d 3e 66 6c 61 67 0==(pPage->flag
10766 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e s&PGHDR_NEED_SYN
10767 43 29 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 79 C) ){. p->pSy
10768 6e 63 65 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 nced = pPage;.
10769 7d 0a 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 }. expensive_as
1076a 73 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63 sert( pcacheChec
1076b 6b 53 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a kSynced(p) );.}.
1076c 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72 20 61 ./*.** Wrapper a
1076d 72 6f 75 6e 64 20 74 68 65 20 70 6c 75 67 67 61 round the plugga
1076e 62 6c 65 20 63 61 63 68 65 73 20 78 55 6e 70 69 ble caches xUnpi
1076f 6e 20 6d 65 74 68 6f 64 2e 20 49 66 20 74 68 65 n method. If the
10770 20 63 61 63 68 65 20 69 73 0a 2a 2a 20 62 65 69 cache is.** bei
10771 6e 67 20 75 73 65 64 20 66 6f 72 20 61 6e 20 69 ng used for an i
10772 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
10773 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e e, this function
10774 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
10775 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 static void pcac
10776 68 65 55 6e 70 69 6e 28 50 67 48 64 72 20 2a 70 heUnpin(PgHdr *p
10777 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 ){. PCache *pCa
10778 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b che = p->pCache;
10779 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 . if( pCache->b
1077a 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 Purgeable ){.
1077b 20 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 if( p->pgno==1
1077c 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d ){. pCache-
1077d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 20 >pPage1 = 0;.
1077e 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c }. sqlite3Gl
1077f 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 obalConfig.pcach
10780 65 2e 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d e.xUnpin(pCache-
10781 3e 70 43 61 63 68 65 2c 20 70 2c 20 30 29 3b 0a >pCache, p, 0);.
10782 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a }.}../********
10783 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10784 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10785 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 *********** Gene
10786 72 61 6c 20 49 6e 74 65 72 66 61 63 65 73 20 2a ral Interfaces *
10787 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6e 69 74 *****.**.** Init
10788 69 61 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64 ialize and shutd
10789 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 own the page cac
1078a 68 65 20 73 75 62 73 79 73 74 65 6d 2e 20 4e 65 he subsystem. Ne
1078b 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 0a ither of these .
1078c 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 ** functions are
1078d 20 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a threadsafe..*/.
1078e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1078f 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nt sqlite3Pcache
10790 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 Initialize(void)
10791 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 {. if( sqlite3G
10792 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
10793 68 65 2e 78 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 he.xInit==0 ){.
10794 20 20 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 sqlite3PCache
10795 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 SetDefault();.
10796 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 }. return sqlit
10797 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
10798 63 61 63 68 65 2e 78 49 6e 69 74 28 73 71 6c 69 cache.xInit(sqli
10799 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
1079a 70 63 61 63 68 65 2e 70 41 72 67 29 3b 0a 7d 0a pcache.pArg);.}.
1079b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1079c 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
1079d 65 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 7b eShutdown(void){
1079e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c . if( sqlite3Gl
1079f 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 obalConfig.pcach
107a0 65 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20 e.xShutdown ){.
107a1 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sqlite3Global
107a2 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 53 Config.pcache.xS
107a3 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 hutdown(sqlite3G
107a4 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
107a5 68 65 2e 70 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a he.pArg);. }.}.
107a6 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
107a7 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 e size in bytes
107a8 6f 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65 of a PCache obje
107a9 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ct..*/.SQLITE_PR
107aa 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
107ab 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64 3PcacheSize(void
107ac 29 7b 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 ){ return sizeof
107ad 28 50 43 61 63 68 65 29 3b 20 7d 0a 0a 2f 2a 0a (PCache); }../*.
107ae 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 ** Create a new
107af 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 53 PCache object. S
107b0 74 6f 72 61 67 65 20 73 70 61 63 65 20 74 6f 20 torage space to
107b1 68 6f 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 0a hold the object.
107b2 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 ** has already b
107b3 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e een allocated an
107b4 64 20 69 73 20 70 61 73 73 65 64 20 69 6e 20 61 d is passed in a
107b5 73 20 74 68 65 20 70 20 70 6f 69 6e 74 65 72 2e s the p pointer.
107b6 20 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 .** The caller
107b7 64 69 73 63 6f 76 65 72 73 20 68 6f 77 20 6d 75 discovers how mu
107b8 63 68 20 73 70 61 63 65 20 6e 65 65 64 73 20 74 ch space needs t
107b9 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 o be allocated b
107ba 79 20 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 73 71 y .** calling sq
107bb 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 lite3PcacheSize(
107bc 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
107bd 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
107be 33 50 63 61 63 68 65 4f 70 65 6e 28 0a 20 20 69 3PcacheOpen(. i
107bf 6e 74 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20 nt szPage,
107c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
107c1 69 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 ize of every pag
107c2 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 e */. int szExt
107c3 72 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ra,
107c4 20 20 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61 /* Extra spa
107c5 63 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ce associated wi
107c6 74 68 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a th each page */.
107c7 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 int bPurgeable
107c8 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
107c9 2a 20 54 72 75 65 20 69 66 20 70 61 67 65 73 20 * True if pages
107ca 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 are on backing s
107cb 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a tore */. int (*
107cc 78 53 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 xStress)(void*,P
107cd 67 48 64 72 2a 29 2c 2f 2a 20 43 61 6c 6c 20 74 gHdr*),/* Call t
107ce 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 o try to make pa
107cf 67 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 ges clean */. v
107d0 6f 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 oid *pStress,
107d1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
107d2 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 rgument to xStre
107d3 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a ss */. PCache *
107d4 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p
107d5 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 /* Prealloc
107d6 61 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 74 ated space for t
107d7 68 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 7b 0a he PCache */.){.
107d8 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 memset(p, 0, s
107d9 69 7a 65 6f 66 28 50 43 61 63 68 65 29 29 3b 0a izeof(PCache));.
107da 20 20 70 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a p->szPage = sz
107db 50 61 67 65 3b 0a 20 20 70 2d 3e 73 7a 45 78 74 Page;. p->szExt
107dc 72 61 20 3d 20 73 7a 45 78 74 72 61 3b 0a 20 20 ra = szExtra;.
107dd 70 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 3d 20 p->bPurgeable =
107de 62 50 75 72 67 65 61 62 6c 65 3b 0a 20 20 70 2d bPurgeable;. p-
107df 3e 78 53 74 72 65 73 73 20 3d 20 78 53 74 72 65 >xStress = xStre
107e0 73 73 3b 0a 20 20 70 2d 3e 70 53 74 72 65 73 73 ss;. p->pStress
107e1 20 3d 20 70 53 74 72 65 73 73 3b 0a 20 20 70 2d = pStress;. p-
107e2 3e 6e 4d 61 78 20 3d 20 31 30 30 3b 0a 7d 0a 0a >nMax = 100;.}..
107e3 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
107e4 20 70 61 67 65 20 73 69 7a 65 20 66 6f 72 20 50 page size for P
107e5 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 54 68 Cache object. Th
107e6 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e e caller must en
107e7 73 75 72 65 20 74 68 61 74 20 74 68 65 72 65 0a sure that there.
107e8 2a 2a 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 ** are no outsta
107e9 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 nding page refer
107ea 65 6e 63 65 73 20 77 68 65 6e 20 74 68 69 73 20 ences when this
107eb 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
107ec 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
107ed 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
107ee 65 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53 e3PcacheSetPageS
107ef 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 ize(PCache *pCac
107f0 68 65 2c 20 69 6e 74 20 73 7a 50 61 67 65 29 7b he, int szPage){
107f1 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 . assert( pCach
107f2 65 2d 3e 6e 52 65 66 3d 3d 30 20 26 26 20 70 43 e->nRef==0 && pC
107f3 61 63 68 65 2d 3e 70 44 69 72 74 79 3d 3d 30 20 ache->pDirty==0
107f4 29 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d );. if( pCache-
107f5 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 >pCache ){. s
107f6 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
107f7 69 67 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72 ig.pcache.xDestr
107f8 6f 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 oy(pCache->pCach
107f9 65 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e e);. pCache->
107fa 70 43 61 63 68 65 20 3d 20 30 3b 0a 20 20 7d 0a pCache = 0;. }.
107fb 20 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 pCache->szPage
107fc 20 3d 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a 2f 2a = szPage;.}../*
107fd 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69 .** Try to obtai
107fe 6e 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 n a page from th
107ff 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 e cache..*/.SQLI
10800 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
10801 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 qlite3PcacheFetc
10802 68 28 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 h(. PCache *pCa
10803 63 68 65 2c 20 20 20 20 20 20 20 2f 2a 20 4f 62 che, /* Ob
10804 74 61 69 6e 20 74 68 65 20 70 61 67 65 20 66 72 tain the page fr
10805 6f 6d 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f om this cache */
10806 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 . Pgno pgno,
10807 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
10808 20 6e 75 6d 62 65 72 20 74 6f 20 6f 62 74 61 69 number to obtai
10809 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61 74 n */. int creat
1080a 65 46 6c 61 67 2c 20 20 20 20 20 20 20 2f 2a 20 eFlag, /*
1080b 49 66 20 74 72 75 65 2c 20 63 72 65 61 74 65 20 If true, create
1080c 70 61 67 65 20 69 66 20 69 74 20 64 6f 65 73 20 page if it does
1080d 6e 6f 74 20 65 78 69 73 74 20 61 6c 72 65 61 64 not exist alread
1080e 79 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 2a 70 y */. PgHdr **p
1080f 70 50 61 67 65 20 20 20 20 20 20 20 20 2f 2a 20 pPage /*
10810 57 72 69 74 65 20 74 68 65 20 70 61 67 65 20 68 Write the page h
10811 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 ere */.){. PgHd
10812 72 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 r *pPage = 0;.
10813 69 6e 74 20 65 43 72 65 61 74 65 3b 0a 0a 20 20 int eCreate;..
10814 61 73 73 65 72 74 28 20 70 43 61 63 68 65 21 3d assert( pCache!=
10815 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 0 );. assert( c
10816 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 7c 7c 20 reateFlag==1 ||
10817 63 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 29 3b createFlag==0 );
10818 0a 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3e . assert( pgno>
10819 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 0 );.. /* If th
1081a 65 20 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68 e pluggable cach
1081b 65 20 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 e (sqlite3_pcach
1081c 65 2a 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e e*) has not been
1081d 20 61 6c 6c 6f 63 61 74 65 64 2c 0a 20 20 2a 2a allocated,. **
1081e 20 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 allocate it now
1081f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 .. */. if( !pC
10820 61 63 68 65 2d 3e 70 43 61 63 68 65 20 26 26 20 ache->pCache &&
10821 63 72 65 61 74 65 46 6c 61 67 20 29 7b 0a 20 20 createFlag ){.
10822 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 sqlite3_pcache
10823 20 2a 70 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 *p;. int nBy
10824 74 65 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 te;. nByte =
10825 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 2b pCache->szPage +
10826 20 70 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 pCache->szExtra
10827 20 2b 20 73 69 7a 65 6f 66 28 50 67 48 64 72 29 + sizeof(PgHdr)
10828 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 ;. p = sqlite
10829 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
1082a 61 63 68 65 2e 78 43 72 65 61 74 65 28 6e 42 79 ache.xCreate(nBy
1082b 74 65 2c 20 70 43 61 63 68 65 2d 3e 62 50 75 72 te, pCache->bPur
1082c 67 65 61 62 6c 65 29 3b 0a 20 20 20 20 69 66 28 geable);. if(
1082d 20 21 70 20 29 7b 0a 20 20 20 20 20 20 72 65 74 !p ){. ret
1082e 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
1082f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
10830 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
10831 70 63 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a pcache.xCachesiz
10832 65 28 70 2c 20 70 43 61 63 68 65 2d 3e 6e 4d 61 e(p, pCache->nMa
10833 78 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e x);. pCache->
10834 70 43 61 63 68 65 20 3d 20 70 3b 0a 20 20 7d 0a pCache = p;. }.
10835 0a 20 20 65 43 72 65 61 74 65 20 3d 20 63 72 65 . eCreate = cre
10836 61 74 65 46 6c 61 67 20 2a 20 28 31 20 2b 20 28 ateFlag * (1 + (
10837 21 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 !pCache->bPurgea
10838 62 6c 65 20 7c 7c 20 21 70 43 61 63 68 65 2d 3e ble || !pCache->
10839 70 44 69 72 74 79 29 29 3b 0a 20 20 69 66 28 20 pDirty));. if(
1083a 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 pCache->pCache )
1083b 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73 71 {. pPage = sq
1083c 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
1083d 67 2e 70 63 61 63 68 65 2e 78 46 65 74 63 68 28 g.pcache.xFetch(
1083e 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 pCache->pCache,
1083f 70 67 6e 6f 2c 20 65 43 72 65 61 74 65 29 3b 0a pgno, eCreate);.
10840 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 }.. if( !pPag
10841 65 20 26 26 20 65 43 72 65 61 74 65 3d 3d 31 20 e && eCreate==1
10842 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 ){. PgHdr *pP
10843 67 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 g;.. /* Find
10844 61 20 64 69 72 74 79 20 70 61 67 65 20 74 6f 20 a dirty page to
10845 77 72 69 74 65 2d 6f 75 74 20 61 6e 64 20 72 65 write-out and re
10846 63 79 63 6c 65 2e 20 46 69 72 73 74 20 74 72 79 cycle. First try
10847 20 74 6f 20 66 69 6e 64 20 61 20 0a 20 20 20 20 to find a .
10848 2a 2a 20 70 61 67 65 20 74 68 61 74 20 64 6f 65 ** page that doe
10849 73 20 6e 6f 74 20 72 65 71 75 69 72 65 20 61 20 s not require a
1084a 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 28 6f 6e journal-sync (on
1084b 65 20 77 69 74 68 20 50 47 48 44 52 5f 4e 45 45 e with PGHDR_NEE
1084c 44 5f 53 59 4e 43 0a 20 20 20 20 2a 2a 20 63 6c D_SYNC. ** cl
1084d 65 61 72 65 64 29 2c 20 62 75 74 20 69 66 20 74 eared), but if t
1084e 68 61 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 hat is not possi
1084f 62 6c 65 20 73 65 74 74 6c 65 20 66 6f 72 20 61 ble settle for a
10850 6e 79 20 6f 74 68 65 72 20 0a 20 20 20 20 2a 2a ny other . **
10851 20 75 6e 72 65 66 65 72 65 6e 63 65 64 20 64 69 unreferenced di
10852 72 74 79 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f rty page.. */
10853 0a 20 20 20 20 65 78 70 65 6e 73 69 76 65 5f 61 . expensive_a
10854 73 73 65 72 74 28 20 70 63 61 63 68 65 43 68 65 ssert( pcacheChe
10855 63 6b 53 79 6e 63 65 64 28 70 43 61 63 68 65 29 ckSynced(pCache)
10856 20 29 3b 0a 20 20 20 20 66 6f 72 28 70 50 67 3d );. for(pPg=
10857 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b pCache->pSynced;
10858 20 0a 20 20 20 20 20 20 20 20 70 50 67 20 26 26 . pPg &&
10859 20 28 70 50 67 2d 3e 6e 52 65 66 20 7c 7c 20 28 (pPg->nRef || (
1085a 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 pPg->flags&PGHDR
1085b 5f 4e 45 45 44 5f 53 59 4e 43 29 29 3b 20 0a 20 _NEED_SYNC)); .
1085c 20 20 20 20 20 20 20 70 50 67 3d 70 50 67 2d 3e pPg=pPg->
1085d 70 44 69 72 74 79 50 72 65 76 0a 20 20 20 20 29 pDirtyPrev. )
1085e 3b 0a 20 20 20 20 69 66 28 20 21 70 50 67 20 29 ;. if( !pPg )
1085f 7b 0a 20 20 20 20 20 20 66 6f 72 28 70 50 67 3d {. for(pPg=
10860 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 pCache->pDirtyTa
10861 69 6c 3b 20 70 50 67 20 26 26 20 70 50 67 2d 3e il; pPg && pPg->
10862 6e 52 65 66 3b 20 70 50 67 3d 70 50 67 2d 3e 70 nRef; pPg=pPg->p
10863 44 69 72 74 79 50 72 65 76 29 3b 0a 20 20 20 20 DirtyPrev);.
10864 7d 0a 20 20 20 20 69 66 28 20 70 50 67 20 29 7b }. if( pPg ){
10865 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 . int rc;.
10866 20 20 20 20 20 72 63 20 3d 20 70 43 61 63 68 65 rc = pCache
10867 2d 3e 78 53 74 72 65 73 73 28 70 43 61 63 68 65 ->xStress(pCache
10868 2d 3e 70 53 74 72 65 73 73 2c 20 70 50 67 29 3b ->pStress, pPg);
10869 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
1086a 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d QLITE_OK && rc!=
1086b 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 SQLITE_BUSY ){.
1086c 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
1086d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1086e 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c . pPage = sql
1086f 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
10870 2e 70 63 61 63 68 65 2e 78 46 65 74 63 68 28 70 .pcache.xFetch(p
10871 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 Cache->pCache, p
10872 67 6e 6f 2c 20 32 29 3b 0a 20 20 7d 0a 0a 20 20 gno, 2);. }..
10873 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 if( pPage ){.
10874 20 69 66 28 20 21 70 50 61 67 65 2d 3e 70 44 61 if( !pPage->pDa
10875 74 61 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 ta ){. mems
10876 65 74 28 70 50 61 67 65 2c 20 30 2c 20 73 69 7a et(pPage, 0, siz
10877 65 6f 66 28 50 67 48 64 72 29 20 2b 20 70 43 61 eof(PgHdr) + pCa
10878 63 68 65 2d 3e 73 7a 45 78 74 72 61 29 3b 0a 20 che->szExtra);.
10879 20 20 20 20 20 70 50 61 67 65 2d 3e 70 45 78 74 pPage->pExt
1087a 72 61 20 3d 20 28 76 6f 69 64 2a 29 26 70 50 61 ra = (void*)&pPa
1087b 67 65 5b 31 5d 3b 0a 20 20 20 20 20 20 70 50 61 ge[1];. pPa
1087c 67 65 2d 3e 70 44 61 74 61 20 3d 20 28 76 6f 69 ge->pData = (voi
1087d 64 20 2a 29 26 28 28 63 68 61 72 20 2a 29 70 50 d *)&((char *)pP
1087e 61 67 65 29 5b 73 69 7a 65 6f 66 28 50 67 48 64 age)[sizeof(PgHd
1087f 72 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 45 r) + pCache->szE
10880 78 74 72 61 5d 3b 0a 20 20 20 20 20 20 70 50 61 xtra];. pPa
10881 67 65 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 ge->pCache = pCa
10882 63 68 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 che;. pPage
10883 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 ->pgno = pgno;.
10884 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
10885 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d 3d pPage->pCache==
10886 70 43 61 63 68 65 20 29 3b 0a 20 20 20 20 61 73 pCache );. as
10887 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e sert( pPage->pgn
10888 6f 3d 3d 70 67 6e 6f 20 29 3b 0a 20 20 20 20 61 o==pgno );. a
10889 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 45 ssert( pPage->pE
1088a 78 74 72 61 3d 3d 28 76 6f 69 64 20 2a 29 26 70 xtra==(void *)&p
1088b 50 61 67 65 5b 31 5d 20 29 3b 0a 0a 20 20 20 20 Page[1] );..
1088c 69 66 28 20 30 3d 3d 70 50 61 67 65 2d 3e 6e 52 if( 0==pPage->nR
1088d 65 66 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 ef ){. pCac
1088e 68 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 he->nRef++;.
1088f 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 52 65 }. pPage->nRe
10890 66 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 67 6e f++;. if( pgn
10891 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 43 o==1 ){. pC
10892 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d 20 70 ache->pPage1 = p
10893 50 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Page;. }. }.
10894 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50 61 67 *ppPage = pPag
10895 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61 e;. return (pPa
10896 67 65 3d 3d 30 20 26 26 20 65 43 72 65 61 74 65 ge==0 && eCreate
10897 29 20 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d ) ? SQLITE_NOMEM
10898 20 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a : SQLITE_OK;.}.
10899 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 ./*.** Decrement
1089a 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 the reference c
1089b 6f 75 6e 74 20 6f 6e 20 61 20 70 61 67 65 2e 20 ount on a page.
1089c 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 63 If the page is c
1089d 6c 65 61 6e 20 61 6e 64 20 74 68 65 0a 2a 2a 20 lean and the.**
1089e 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 reference count
1089f 64 72 6f 70 73 20 74 6f 20 30 2c 20 74 68 65 6e drops to 0, then
108a0 20 69 74 20 69 73 20 6d 61 64 65 20 65 6c 69 62 it is made elib
108a1 6c 65 20 66 6f 72 20 72 65 63 79 63 6c 69 6e 67 le for recycling
108a2 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
108a3 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
108a4 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 PcacheRelease(Pg
108a5 48 64 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 Hdr *p){. asser
108a6 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a t( p->nRef>0 );.
108a7 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 p->nRef--;. i
108a8 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b f( p->nRef==0 ){
108a9 0a 20 20 20 20 50 43 61 63 68 65 20 2a 70 43 61 . PCache *pCa
108aa 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b che = p->pCache;
108ab 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 . pCache->nRe
108ac 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 28 70 2d f--;. if( (p-
108ad 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 >flags&PGHDR_DIR
108ae 54 59 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 TY)==0 ){.
108af 70 63 61 63 68 65 55 6e 70 69 6e 28 70 29 3b 0a pcacheUnpin(p);.
108b0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
108b1 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 /* Move the pag
108b2 65 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 e to the head of
108b3 20 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 2e the dirty list.
108b4 20 2a 2f 0a 20 20 20 20 20 20 70 63 61 63 68 65 */. pcache
108b5 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c RemoveFromDirtyL
108b6 69 73 74 28 70 29 3b 0a 20 20 20 20 20 20 70 63 ist(p);. pc
108b7 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 acheAddToDirtyLi
108b8 73 74 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d st(p);. }. }
108b9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 61 .}../*.** Increa
108ba 73 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 se the reference
108bb 20 63 6f 75 6e 74 20 6f 66 20 61 20 73 75 70 70 count of a supp
108bc 6c 69 65 64 20 70 61 67 65 20 62 79 20 31 2e 0a lied page by 1..
108bd 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
108be 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
108bf 61 63 68 65 52 65 66 28 50 67 48 64 72 20 2a 70 acheRef(PgHdr *p
108c0 29 7b 0a 20 20 61 73 73 65 72 74 28 70 2d 3e 6e ){. assert(p->n
108c1 52 65 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e 52 65 Ref>0);. p->nRe
108c2 66 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 f++;.}../*.** Dr
108c3 6f 70 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 op a page from t
108c4 68 65 20 63 61 63 68 65 2e 20 54 68 65 72 65 20 he cache. There
108c5 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 must be exactly
108c6 6f 6e 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f one reference to
108c7 20 74 68 65 0a 2a 2a 20 70 61 67 65 2e 20 54 68 the.** page. Th
108c8 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65 is function dele
108c9 74 65 73 20 74 68 61 74 20 72 65 66 65 72 65 6e tes that referen
108ca 63 65 2c 20 73 6f 20 61 66 74 65 72 20 69 74 20 ce, so after it
108cb 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 70 returns the.** p
108cc 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 age pointed to b
108cd 79 20 70 20 69 73 20 69 6e 76 61 6c 69 64 2e 0a y p is invalid..
108ce 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
108cf 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
108d0 61 63 68 65 44 72 6f 70 28 50 67 48 64 72 20 2a acheDrop(PgHdr *
108d1 70 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 p){. PCache *pC
108d2 61 63 68 65 3b 0a 20 20 61 73 73 65 72 74 28 20 ache;. assert(
108d3 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20 p->nRef==1 );.
108d4 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 50 47 48 if( p->flags&PGH
108d5 44 52 5f 44 49 52 54 59 20 29 7b 0a 20 20 20 20 DR_DIRTY ){.
108d6 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d pcacheRemoveFrom
108d7 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 DirtyList(p);.
108d8 7d 0a 20 20 70 43 61 63 68 65 20 3d 20 70 2d 3e }. pCache = p->
108d9 70 43 61 63 68 65 3b 0a 20 20 70 43 61 63 68 65 pCache;. pCache
108da 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 ->nRef--;. if(
108db 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 p->pgno==1 ){.
108dc 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 pCache->pPage1
108dd 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 = 0;. }. sqli
108de 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
108df 70 63 61 63 68 65 2e 78 55 6e 70 69 6e 28 70 43 pcache.xUnpin(pC
108e0 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c ache->pCache, p,
108e1 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 1);.}../*.** Ma
108e2 6b 65 20 73 75 72 65 20 74 68 65 20 70 61 67 65 ke sure the page
108e3 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 is marked as di
108e4 72 74 79 2e 20 49 66 20 69 74 20 69 73 6e 27 74 rty. If it isn't
108e5 20 64 69 72 74 79 20 61 6c 72 65 61 64 79 2c 0a dirty already,.
108e6 2a 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a ** make it so..*
108e7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
108e8 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
108e9 63 68 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48 cheMakeDirty(PgH
108ea 64 72 20 2a 70 29 7b 0a 20 20 70 2d 3e 66 6c 61 dr *p){. p->fla
108eb 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 44 4f 4e gs &= ~PGHDR_DON
108ec 54 5f 57 52 49 54 45 3b 0a 20 20 61 73 73 65 72 T_WRITE;. asser
108ed 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a t( p->nRef>0 );.
108ee 20 20 69 66 28 20 30 3d 3d 28 70 2d 3e 66 6c 61 if( 0==(p->fla
108ef 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 gs & PGHDR_DIRTY
108f0 29 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 ) ){. p->flag
108f1 73 20 7c 3d 20 50 47 48 44 52 5f 44 49 52 54 59 s |= PGHDR_DIRTY
108f2 3b 0a 20 20 20 20 70 63 61 63 68 65 41 64 64 54 ;. pcacheAddT
108f3 6f 44 69 72 74 79 4c 69 73 74 28 20 70 29 3b 0a oDirtyList( p);.
108f4 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b }.}../*.** Mak
108f5 65 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20 e sure the page
108f6 69 73 20 6d 61 72 6b 65 64 20 61 73 20 63 6c 65 is marked as cle
108f7 61 6e 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 an. If it isn't
108f8 63 6c 65 61 6e 20 61 6c 72 65 61 64 79 2c 0a 2a clean already,.*
108f9 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f * make it so..*/
108fa 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
108fb 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
108fc 68 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 heMakeClean(PgHd
108fd 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 28 70 2d r *p){. if( (p-
108fe 3e 66 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 >flags & PGHDR_D
108ff 49 52 54 59 29 20 29 7b 0a 20 20 20 20 70 63 61 IRTY) ){. pca
10900 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 cheRemoveFromDir
10901 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 tyList(p);. p
10902 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 50 47 48 ->flags &= ~(PGH
10903 44 52 5f 44 49 52 54 59 7c 50 47 48 44 52 5f 4e DR_DIRTY|PGHDR_N
10904 45 45 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 EED_SYNC);. i
10905 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b f( p->nRef==0 ){
10906 0a 20 20 20 20 20 20 70 63 61 63 68 65 55 6e 70 . pcacheUnp
10907 69 6e 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d in(p);. }. }
10908 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 .}../*.** Make e
10909 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 very page in the
1090a 20 63 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f cache clean..*/
1090b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1090c 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
1090d 68 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 heCleanAll(PCach
1090e 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 e *pCache){. Pg
1090f 48 64 72 20 2a 70 3b 0a 20 20 77 68 69 6c 65 28 Hdr *p;. while(
10910 20 28 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 (p = pCache->pD
10911 69 72 74 79 29 21 3d 30 20 29 7b 0a 20 20 20 20 irty)!=0 ){.
10912 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b sqlite3PcacheMak
10913 65 43 6c 65 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d eClean(p);. }.}
10914 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 ../*.** Clear th
10915 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e e PGHDR_NEED_SYN
10916 43 20 66 6c 61 67 20 66 72 6f 6d 20 61 6c 6c 20 C flag from all
10917 64 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a dirty pages..*/.
10918 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
10919 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
1091a 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 eClearSyncFlags(
1091b 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b PCache *pCache){
1091c 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 . PgHdr *p;. f
1091d 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 or(p=pCache->pDi
1091e 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 rty; p; p=p->pDi
1091f 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d rtyNext){. p-
10920 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 >flags &= ~PGHDR
10921 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a _NEED_SYNC;. }.
10922 20 20 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 pCache->pSynce
10923 64 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 d = pCache->pDir
10924 74 79 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a tyTail;.}../*.**
10925 20 43 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 Change the page
10926 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 number of page
10927 70 20 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a p to newPgno. .*
10928 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
10929 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
1092a 63 68 65 4d 6f 76 65 28 50 67 48 64 72 20 2a 70 cheMove(PgHdr *p
1092b 2c 20 50 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b , Pgno newPgno){
1092c 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 . PCache *pCach
1092d 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 e = p->pCache;.
1092e 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 assert( p->nRef
1092f 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 >0 );. assert(
10930 6e 65 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 20 73 newPgno>0 );. s
10931 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
10932 69 67 2e 70 63 61 63 68 65 2e 78 52 65 6b 65 79 ig.pcache.xRekey
10933 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c (pCache->pCache,
10934 20 70 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e 65 77 p, p->pgno, new
10935 50 67 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f Pgno);. p->pgno
10936 20 3d 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 69 66 = newPgno;. if
10937 28 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 ( (p->flags&PGHD
10938 52 5f 44 49 52 54 59 29 20 26 26 20 28 70 2d 3e R_DIRTY) && (p->
10939 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
1093a 5f 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 63 _SYNC) ){. pc
1093b 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 acheRemoveFromDi
1093c 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 rtyList(p);.
1093d 70 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 pcacheAddToDirty
1093e 4c 69 73 74 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a List(p);. }.}..
1093f 2f 2a 0a 2a 2a 20 44 72 6f 70 20 65 76 65 72 79 /*.** Drop every
10940 20 63 61 63 68 65 20 65 6e 74 72 79 20 77 68 6f cache entry who
10941 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 se page number i
10942 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 22 s greater than "
10943 70 67 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20 63 61 pgno". The.** ca
10944 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 ller must ensure
10945 20 74 68 61 74 20 74 68 65 72 65 20 61 72 65 20 that there are
10946 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 no outstanding r
10947 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 eferences to any
10948 20 70 61 67 65 73 0a 2a 2a 20 6f 74 68 65 72 20 pages.** other
10949 74 68 61 6e 20 70 61 67 65 20 31 20 77 69 74 68 than page 1 with
1094a 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 67 a page number g
1094b 72 65 61 74 65 72 20 74 68 61 6e 20 70 67 6e 6f reater than pgno
1094c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 ..**.** If there
1094d 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 is a reference
1094e 74 6f 20 70 61 67 65 20 31 20 61 6e 64 20 74 68 to page 1 and th
1094f 65 20 70 67 6e 6f 20 70 61 72 61 6d 65 74 65 72 e pgno parameter
10950 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 0a passed to this.
10951 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 30 ** function is 0
10952 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 20 , then the data
10953 61 72 65 61 20 61 73 73 6f 63 69 61 74 65 64 20 area associated
10954 77 69 74 68 20 70 61 67 65 20 31 20 69 73 20 7a with page 1 is z
10955 65 72 6f 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68 eroed, but.** th
10956 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 e page object is
10957 20 6e 6f 74 20 64 72 6f 70 70 65 64 2e 0a 2a 2f not dropped..*/
10958 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
10959 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
1095a 68 65 54 72 75 6e 63 61 74 65 28 50 43 61 63 68 heTruncate(PCach
1095b 65 20 2a 70 43 61 63 68 65 2c 20 50 67 6e 6f 20 e *pCache, Pgno
1095c 70 67 6e 6f 29 7b 0a 20 20 69 66 28 20 70 43 61 pgno){. if( pCa
1095d 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 che->pCache ){.
1095e 20 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 PgHdr *p;.
1095f 20 50 67 48 64 72 20 2a 70 4e 65 78 74 3b 0a 20 PgHdr *pNext;.
10960 20 20 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d for(p=pCache-
10961 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 4e >pDirty; p; p=pN
10962 65 78 74 29 7b 0a 20 20 20 20 20 20 70 4e 65 78 ext){. pNex
10963 74 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 78 t = p->pDirtyNex
10964 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e t;. if( p->
10965 70 67 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 pgno>pgno ){.
10966 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
10967 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 flags&PGHDR_DIRT
10968 59 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c Y );. sql
10969 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c ite3PcacheMakeCl
1096a 65 61 6e 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a ean(p);. }.
1096b 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 67 }. if( pg
1096c 6e 6f 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d no==0 && pCache-
1096d 3e 70 50 61 67 65 31 20 29 7b 0a 20 20 20 20 20 >pPage1 ){.
1096e 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65 2d 3e memset(pCache->
1096f 70 50 61 67 65 31 2d 3e 70 44 61 74 61 2c 20 30 pPage1->pData, 0
10970 2c 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 , pCache->szPage
10971 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 );. pgno =
10972 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 1;. }. sql
10973 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
10974 2e 70 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74 .pcache.xTruncat
10975 65 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 e(pCache->pCache
10976 2c 20 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d 0a 7d , pgno+1);. }.}
10977 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 ../*.** Close a
10978 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 cache..*/.SQLITE
10979 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1097a 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 lite3PcacheClose
1097b 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 (PCache *pCache)
1097c 7b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e {. if( pCache->
1097d 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 pCache ){. sq
1097e 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
1097f 67 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f g.pcache.xDestro
10980 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 y(pCache->pCache
10981 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a );. }.}../* .**
10982 20 44 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e Discard the con
10983 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 tents of the cac
10984 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 he..*/.SQLITE_PR
10985 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
10986 65 33 50 63 61 63 68 65 43 6c 65 61 72 28 50 43 e3PcacheClear(PC
10987 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 ache *pCache){.
10988 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 sqlite3PcacheTr
10989 75 6e 63 61 74 65 28 70 43 61 63 68 65 2c 20 30 uncate(pCache, 0
1098a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 );.}../*.** Merg
1098b 65 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 70 e two lists of p
1098c 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 ages connected b
1098d 79 20 70 44 69 72 74 79 20 61 6e 64 20 69 6e 20 y pDirty and in
1098e 70 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 44 pgno order..** D
1098f 6f 20 6e 6f 74 20 62 6f 74 68 20 66 69 78 69 6e o not both fixin
10990 67 20 74 68 65 20 70 44 69 72 74 79 50 72 65 76 g the pDirtyPrev
10991 20 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 pointers..*/.st
10992 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 atic PgHdr *pcac
10993 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 74 heMergeDirtyList
10994 28 50 67 48 64 72 20 2a 70 41 2c 20 50 67 48 64 (PgHdr *pA, PgHd
10995 72 20 2a 70 42 29 7b 0a 20 20 50 67 48 64 72 20 r *pB){. PgHdr
10996 72 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a result, *pTail;.
10997 20 20 70 54 61 69 6c 20 3d 20 26 72 65 73 75 6c pTail = &resul
10998 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 t;. while( pA &
10999 26 20 70 42 20 29 7b 0a 20 20 20 20 69 66 28 20 & pB ){. if(
1099a 70 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e pA->pgno<pB->pgn
1099b 6f 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c o ){. pTail
1099c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 ->pDirty = pA;.
1099d 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 41 3b pTail = pA;
1099e 0a 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e . pA = pA->
1099f 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73 pDirty;. }els
109a0 65 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e e{. pTail->
109a1 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 pDirty = pB;.
109a2 20 20 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20 pTail = pB;.
109a3 20 20 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 44 pB = pB->pD
109a4 69 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a irty;. }. }.
109a5 20 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 if( pA ){.
109a6 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 pTail->pDirty =
109a7 70 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 pA;. }else if(
109a8 70 42 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d pB ){. pTail-
109a9 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 >pDirty = pB;.
109aa 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 61 69 6c }else{. pTail
109ab 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 ->pDirty = 0;.
109ac 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 75 6c }. return resul
109ad 74 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a t.pDirty;.}../*.
109ae 2a 2a 20 53 6f 72 74 20 74 68 65 20 6c 69 73 74 ** Sort the list
109af 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 63 63 of pages in acc
109b0 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 ending order by
109b1 70 67 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 65 pgno. Pages are
109b2 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 .** connected by
109b3 20 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 73 pDirty pointers
109b4 2e 20 20 54 68 65 20 70 44 69 72 74 79 50 72 65 . The pDirtyPre
109b5 76 20 70 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a v pointers are.*
109b6 2a 20 63 6f 72 72 75 70 74 65 64 20 62 79 20 74 * corrupted by t
109b7 68 69 73 20 73 6f 72 74 2e 0a 2a 2a 0a 2a 2a 20 his sort..**.**
109b8 53 69 6e 63 65 20 74 68 65 72 65 20 63 61 6e 6e Since there cann
109b9 6f 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 ot be more than
109ba 32 5e 33 31 20 64 69 73 74 69 6e 63 74 20 70 61 2^31 distinct pa
109bb 67 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 ges in a databas
109bc 65 2c 0a 2a 2a 20 74 68 65 72 65 20 63 61 6e 6e e,.** there cann
109bd 6f 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 ot be more than
109be 33 31 20 62 75 63 6b 65 74 73 20 72 65 71 75 69 31 buckets requi
109bf 72 65 64 20 62 79 20 74 68 65 20 6d 65 72 67 65 red by the merge
109c0 20 73 6f 72 74 65 72 2e 0a 2a 2a 20 4f 6e 65 20 sorter..** One
109c1 65 78 74 72 61 20 62 75 63 6b 65 74 20 69 73 20 extra bucket is
109c2 61 64 64 65 64 20 74 6f 20 63 61 74 63 68 20 6f added to catch o
109c3 76 65 72 66 6c 6f 77 20 69 6e 20 63 61 73 65 20 verflow in case
109c4 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 65 76 65 something.** eve
109c5 72 20 63 68 61 6e 67 65 73 20 74 6f 20 6d 61 6b r changes to mak
109c6 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 e the previous s
109c7 65 6e 74 65 6e 63 65 20 69 6e 63 6f 72 72 65 63 entence incorrec
109c8 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f t..*/.#define N_
109c9 53 4f 52 54 5f 42 55 43 4b 45 54 20 20 33 32 0a SORT_BUCKET 32.
109ca 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 static PgHdr *pc
109cb 61 63 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73 acheSortDirtyLis
109cc 74 28 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 t(PgHdr *pIn){.
109cd 20 50 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 PgHdr *a[N_SORT
109ce 5f 42 55 43 4b 45 54 5d 2c 20 2a 70 3b 0a 20 20 _BUCKET], *p;.
109cf 69 6e 74 20 69 3b 0a 20 20 6d 65 6d 73 65 74 28 int i;. memset(
109d0 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 29 29 a, 0, sizeof(a))
109d1 3b 0a 20 20 77 68 69 6c 65 28 20 70 49 6e 20 29 ;. while( pIn )
109d2 7b 0a 20 20 20 20 70 20 3d 20 70 49 6e 3b 0a 20 {. p = pIn;.
109d3 20 20 20 70 49 6e 20 3d 20 70 2d 3e 70 44 69 72 pIn = p->pDir
109d4 74 79 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 ty;. p->pDirt
109d5 79 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 y = 0;. for(i
109d6 3d 30 3b 20 41 4c 57 41 59 53 28 69 3c 4e 5f 53 =0; ALWAYS(i<N_S
109d7 4f 52 54 5f 42 55 43 4b 45 54 2d 31 29 3b 20 69 ORT_BUCKET-1); i
109d8 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 ++){. if( a
109d9 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 [i]==0 ){.
109da 20 20 61 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20 a[i] = p;.
109db 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
109dc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
109dd 70 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 p = pcacheMergeD
109de 69 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 irtyList(a[i], p
109df 29 3b 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20 );. a[i]
109e0 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 0;. }.
109e1 20 7d 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 }. if( NEVER
109e2 28 69 3d 3d 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 (i==N_SORT_BUCKE
109e3 54 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a T-1) ){. /*
109e4 20 54 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68 To get here, th
109e5 65 72 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32 ere need to be 2
109e6 5e 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29 ^(N_SORT_BUCKET)
109e7 20 65 6c 65 6d 65 6e 74 73 20 69 6e 0a 20 20 20 elements in.
109e8 20 20 20 2a 2a 20 74 68 65 20 69 6e 70 75 74 20 ** the input
109e9 6c 69 73 74 2e 20 20 42 75 74 20 74 68 61 74 20 list. But that
109ea 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 2e 0a 20 is impossible..
109eb 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 5b */. a[
109ec 69 5d 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 i] = pcacheMerge
109ed 44 69 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 DirtyList(a[i],
109ee 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 p);. }. }.
109ef 70 20 3d 20 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 p = a[0];. for(
109f0 69 3d 31 3b 20 69 3c 4e 5f 53 4f 52 54 5f 42 55 i=1; i<N_SORT_BU
109f1 43 4b 45 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 CKET; i++){.
109f2 70 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 p = pcacheMergeD
109f3 69 72 74 79 4c 69 73 74 28 70 2c 20 61 5b 69 5d irtyList(p, a[i]
109f4 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
109f5 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 p;.}../*.** Retu
109f6 72 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c rn a list of all
109f7 20 64 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 dirty pages in
109f8 74 68 65 20 63 61 63 68 65 2c 20 73 6f 72 74 65 the cache, sorte
109f9 64 20 62 79 20 70 61 67 65 20 6e 75 6d 62 65 72 d by page number
109fa 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
109fb 41 54 45 20 50 67 48 64 72 20 2a 73 71 6c 69 74 ATE PgHdr *sqlit
109fc 65 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 e3PcacheDirtyLis
109fd 74 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 t(PCache *pCache
109fe 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 ){. PgHdr *p;.
109ff 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 for(p=pCache->p
10a00 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 Dirty; p; p=p->p
10a01 44 69 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 DirtyNext){.
10a02 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 2d 3e 70 p->pDirty = p->p
10a03 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 DirtyNext;. }.
10a04 20 72 65 74 75 72 6e 20 70 63 61 63 68 65 53 6f return pcacheSo
10a05 72 74 44 69 72 74 79 4c 69 73 74 28 70 43 61 63 rtDirtyList(pCac
10a06 68 65 2d 3e 70 44 69 72 74 79 29 3b 0a 7d 0a 0a he->pDirty);.}..
10a07 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 /* .** Return th
10a08 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
10a09 66 20 72 65 66 65 72 65 6e 63 65 64 20 70 61 67 f referenced pag
10a0a 65 73 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 es held by the c
10a0b 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ache..*/.SQLITE_
10a0c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
10a0d 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e te3PcacheRefCoun
10a0e 74 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 t(PCache *pCache
10a0f 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 61 63 ){. return pCac
10a10 68 65 2d 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 0a he->nRef;.}../*.
10a11 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
10a12 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 mber of referenc
10a13 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 73 es to the page s
10a14 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 upplied as an ar
10a15 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 gument..*/.SQLIT
10a16 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
10a17 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 lite3PcachePageR
10a18 65 66 63 6f 75 6e 74 28 50 67 48 64 72 20 2a 70 efcount(PgHdr *p
10a19 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e ){. return p->n
10a1a 52 65 66 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 Ref;.}../* .** R
10a1b 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 eturn the total
10a1c 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
10a1d 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f in the cache..*/
10a1e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
10a1f 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 int sqlite3Pcach
10a20 65 50 61 67 65 63 6f 75 6e 74 28 50 43 61 63 68 ePagecount(PCach
10a21 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e e *pCache){. in
10a22 74 20 6e 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 t nPage = 0;. i
10a23 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 f( pCache->pCach
10a24 65 20 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d e ){. nPage =
10a25 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
10a26 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 50 61 67 nfig.pcache.xPag
10a27 65 63 6f 75 6e 74 28 70 43 61 63 68 65 2d 3e 70 ecount(pCache->p
10a28 43 61 63 68 65 29 3b 0a 20 20 7d 0a 20 20 72 65 Cache);. }. re
10a29 74 75 72 6e 20 6e 50 61 67 65 3b 0a 7d 0a 0a 23 turn nPage;.}..#
10a2a 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
10a2b 54 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 T./*.** Get the
10a2c 73 75 67 67 65 73 74 65 64 20 63 61 63 68 65 2d suggested cache-
10a2d 73 69 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 size value..*/.S
10a2e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
10a2f 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 t sqlite3PcacheG
10a30 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 etCachesize(PCac
10a31 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 he *pCache){. r
10a32 65 74 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 4d eturn pCache->nM
10a33 61 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a ax;.}.#endif../*
10a34 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 75 67 67 .** Set the sugg
10a35 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 ested cache-size
10a36 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 value..*/.SQLIT
10a37 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
10a38 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 43 qlite3PcacheSetC
10a39 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 achesize(PCache
10a3a 2a 70 43 61 63 68 65 2c 20 69 6e 74 20 6d 78 50 *pCache, int mxP
10a3b 61 67 65 29 7b 0a 20 20 70 43 61 63 68 65 2d 3e age){. pCache->
10a3c 6e 4d 61 78 20 3d 20 6d 78 50 61 67 65 3b 0a 20 nMax = mxPage;.
10a3d 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 if( pCache->pCa
10a3e 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 che ){. sqlit
10a3f 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
10a40 63 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 cache.xCachesize
10a41 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c (pCache->pCache,
10a42 20 6d 78 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a mxPage);. }.}.
10a43 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
10a44 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 ITE_CHECK_PAGES)
10a45 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 || defined(SQLI
10a46 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 TE_DEBUG)./*.**
10a47 46 6f 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61 For all dirty pa
10a48 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e ges currently in
10a49 20 74 68 65 20 63 61 63 68 65 2c 20 69 6e 76 6f the cache, invo
10a4a 6b 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64 ke the specified
10a4b 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 .** callback. Th
10a4c 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 is is only used
10a4d 69 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43 48 if the SQLITE_CH
10a4e 45 43 4b 5f 50 41 47 45 53 20 6d 61 63 72 6f 20 ECK_PAGES macro
10a4f 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a is.** defined..*
10a50 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
10a51 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
10a52 63 68 65 49 74 65 72 61 74 65 44 69 72 74 79 28 cheIterateDirty(
10a53 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 PCache *pCache,
10a54 76 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 void (*xIter)(Pg
10a55 48 64 72 20 2a 29 29 7b 0a 20 20 50 67 48 64 72 Hdr *)){. PgHdr
10a56 20 2a 70 44 69 72 74 79 3b 0a 20 20 66 6f 72 28 *pDirty;. for(
10a57 70 44 69 72 74 79 3d 70 43 61 63 68 65 2d 3e 70 pDirty=pCache->p
10a58 44 69 72 74 79 3b 20 70 44 69 72 74 79 3b 20 70 Dirty; pDirty; p
10a59 44 69 72 74 79 3d 70 44 69 72 74 79 2d 3e 70 44 Dirty=pDirty->pD
10a5a 69 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 78 irtyNext){. x
10a5b 49 74 65 72 28 70 44 69 72 74 79 29 3b 0a 20 20 Iter(pDirty);.
10a5c 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a }.}.#endif../***
10a5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
10a5e 6f 66 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a of pcache.c ****
10a5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
10a62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
10a63 6e 20 66 69 6c 65 20 70 63 61 63 68 65 31 2e 63 n file pcache1.c
10a64 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
10a65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
10a67 2a 20 32 30 30 38 20 4e 6f 76 65 6d 62 65 72 20 * 2008 November
10a68 30 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 05.**.** The aut
10a69 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
10a6a 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
10a6b 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
10a6c 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
10a6d 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
10a6e 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
10a6f 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
10a70 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
10a71 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
10a72 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
10a73 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
10a74 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
10a75 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
10a76 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
10a77 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
10a78 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
10a79 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
10a7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a7e 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
10a7f 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 e implements the
10a80 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 default page ca
10a81 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 che implementati
10a82 6f 6e 20 28 74 68 65 0a 2a 2a 20 73 71 6c 69 74 on (the.** sqlit
10a83 65 33 5f 70 63 61 63 68 65 20 69 6e 74 65 72 66 e3_pcache interf
10a84 61 63 65 29 2e 20 49 74 20 61 6c 73 6f 20 63 6f ace). It also co
10a85 6e 74 61 69 6e 73 20 70 61 72 74 20 6f 66 20 74 ntains part of t
10a86 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
10a87 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 49 n.** of the SQLI
10a88 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 TE_CONFIG_PAGECA
10a89 43 48 45 20 61 6e 64 20 73 71 6c 69 74 65 33 5f CHE and sqlite3_
10a8a 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 release_memory()
10a8b 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 20 49 66 features..** If
10a8c 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 the default pag
10a8d 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e e cache implemen
10a8e 74 61 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 tation is overri
10a8f 64 65 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 den, then neithe
10a90 72 20 6f 66 0a 2a 2a 20 74 68 65 73 65 20 74 77 r of.** these tw
10a91 6f 20 66 65 61 74 75 72 65 73 20 61 72 65 20 61 o features are a
10a92 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 vailable..**.**
10a93 40 28 23 29 20 24 49 64 3a 20 70 63 61 63 68 65 @(#) $Id: pcache
10a94 31 2e 63 2c 76 20 31 2e 31 39 20 32 30 30 39 2f 1.c,v 1.19 2009/
10a95 30 37 2f 31 37 20 31 31 3a 34 34 3a 30 37 20 64 07/17 11:44:07 d
10a96 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 74 79 rh Exp $.*/...ty
10a97 70 65 64 65 66 20 73 74 72 75 63 74 20 50 43 61 pedef struct PCa
10a98 63 68 65 31 20 50 43 61 63 68 65 31 3b 0a 74 79 che1 PCache1;.ty
10a99 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48 pedef struct PgH
10a9a 64 72 31 20 50 67 48 64 72 31 3b 0a 74 79 70 65 dr1 PgHdr1;.type
10a9b 64 65 66 20 73 74 72 75 63 74 20 50 67 46 72 65 def struct PgFre
10a9c 65 73 6c 6f 74 20 50 67 46 72 65 65 73 6c 6f 74 eslot PgFreeslot
10a9d 3b 0a 0a 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 ;../* Pointers t
10a9e 6f 20 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 o structures of
10a9f 74 68 69 73 20 74 79 70 65 20 61 72 65 20 63 61 this type are ca
10aa0 73 74 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 st and returned
10aa1 61 73 20 0a 2a 2a 20 6f 70 61 71 75 65 20 73 71 as .** opaque sq
10aa2 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 20 68 61 lite3_pcache* ha
10aa3 6e 64 6c 65 73 0a 2a 2f 0a 73 74 72 75 63 74 20 ndles.*/.struct
10aa4 50 43 61 63 68 65 31 20 7b 0a 20 20 2f 2a 20 43 PCache1 {. /* C
10aa5 61 63 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 ache configurati
10aa6 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 2e 20 50 on parameters. P
10aa7 61 67 65 20 73 69 7a 65 20 28 73 7a 50 61 67 65 age size (szPage
10aa8 29 20 61 6e 64 20 74 68 65 20 70 75 72 67 65 61 ) and the purgea
10aa9 62 6c 65 0a 20 20 2a 2a 20 66 6c 61 67 20 28 62 ble. ** flag (b
10aaa 50 75 72 67 65 61 62 6c 65 29 20 61 72 65 20 73 Purgeable) are s
10aab 65 74 20 77 68 65 6e 20 74 68 65 20 63 61 63 68 et when the cach
10aac 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 6e 4d e is created. nM
10aad 61 78 20 6d 61 79 20 62 65 20 0a 20 20 2a 2a 20 ax may be . **
10aae 6d 6f 64 69 66 69 65 64 20 61 74 20 61 6e 79 20 modified at any
10aaf 74 69 6d 65 20 62 79 20 61 20 63 61 6c 6c 20 74 time by a call t
10ab0 6f 20 74 68 65 20 70 63 61 63 68 65 31 43 61 63 o the pcache1Cac
10ab1 68 65 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 2e heSize() method.
10ab2 0a 20 20 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c . ** The global
10ab3 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 mutex must be h
10ab4 65 6c 64 20 77 68 65 6e 20 61 63 63 65 73 73 69 eld when accessi
10ab5 6e 67 20 6e 4d 61 78 2e 0a 20 20 2a 2f 0a 20 20 ng nMax.. */.
10ab6 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 int szPage;
10ab7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10ab8 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 /* Size of a
10ab9 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 73 20 69 llocated pages i
10aba 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 n bytes */. int
10abb 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20 20 20 bPurgeable;
10abc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10abd 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61 63 68 /* True if cach
10abe 65 20 69 73 20 70 75 72 67 65 61 62 6c 65 20 2a e is purgeable *
10abf 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 /. unsigned int
10ac0 20 6e 4d 69 6e 3b 20 20 20 20 20 20 20 20 20 20 nMin;
10ac1 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d /* Minim
10ac2 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 um number of pag
10ac3 65 73 20 72 65 73 65 72 76 65 64 20 2a 2f 0a 20 es reserved */.
10ac4 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4d unsigned int nM
10ac5 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ax;
10ac6 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 /* Configur
10ac7 65 64 20 22 63 61 63 68 65 5f 73 69 7a 65 22 20 ed "cache_size"
10ac8 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 48 value */.. /* H
10ac9 61 73 68 20 74 61 62 6c 65 20 6f 66 20 61 6c 6c ash table of all
10aca 20 70 61 67 65 73 2e 20 54 68 65 20 66 6f 6c 6c pages. The foll
10acb 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20 owing variables
10acc 6d 61 79 20 6f 6e 6c 79 20 62 65 20 61 63 63 65 may only be acce
10acd 73 73 65 64 0a 20 20 2a 2a 20 77 68 65 6e 20 74 ssed. ** when t
10ace 68 65 20 61 63 63 65 73 73 6f 72 20 69 73 20 68 he accessor is h
10acf 6f 6c 64 69 6e 67 20 74 68 65 20 67 6c 6f 62 61 olding the globa
10ad0 6c 20 6d 75 74 65 78 20 28 73 65 65 20 70 63 61 l mutex (see pca
10ad1 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 che1EnterMutex()
10ad2 20 0a 20 20 2a 2a 20 61 6e 64 20 70 63 61 63 68 . ** and pcach
10ad3 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 29 2e e1LeaveMutex()).
10ad4 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 . */. unsigned
10ad5 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 int nRecyclable
10ad6 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
10ad7 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
10ad8 6e 20 74 68 65 20 4c 52 55 20 6c 69 73 74 20 2a n the LRU list *
10ad9 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 /. unsigned int
10ada 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 nPage;
10adb 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
10adc 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
10add 20 69 6e 20 61 70 48 61 73 68 20 2a 2f 0a 20 20 in apHash */.
10ade 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 48 61 unsigned int nHa
10adf 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 sh;
10ae0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
10ae1 20 73 6c 6f 74 73 20 69 6e 20 61 70 48 61 73 68 slots in apHash
10ae2 5b 5d 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a [] */. PgHdr1 *
10ae3 2a 61 70 48 61 73 68 3b 20 20 20 20 20 20 20 20 *apHash;
10ae4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 /* H
10ae5 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 61 ash table for fa
10ae6 73 74 20 6c 6f 6f 6b 75 70 20 62 79 20 6b 65 79 st lookup by key
10ae7 20 2a 2f 0a 0a 20 20 75 6e 73 69 67 6e 65 64 20 */.. unsigned
10ae8 69 6e 74 20 69 4d 61 78 4b 65 79 3b 20 20 20 20 int iMaxKey;
10ae9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 /* La
10aea 72 67 65 73 74 20 6b 65 79 20 73 65 65 6e 20 73 rgest key seen s
10aeb 69 6e 63 65 20 78 54 72 75 6e 63 61 74 65 28 29 ince xTruncate()
10aec 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 */.};../*.** Ea
10aed 63 68 20 63 61 63 68 65 20 65 6e 74 72 79 20 69 ch cache entry i
10aee 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 s represented by
10aef 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
10af0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a the following .*
10af1 2a 20 73 74 72 75 63 74 75 72 65 2e 20 41 20 62 * structure. A b
10af2 75 66 66 65 72 20 6f 66 20 50 67 48 64 72 31 2e uffer of PgHdr1.
10af3 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 62 pCache->szPage b
10af4 79 74 65 73 20 69 73 20 61 6c 6c 6f 63 61 74 65 ytes is allocate
10af5 64 20 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 62 d .** directly b
10af6 65 66 6f 72 65 20 74 68 69 73 20 73 74 72 75 63 efore this struc
10af7 74 75 72 65 20 69 6e 20 6d 65 6d 6f 72 79 20 28 ture in memory (
10af8 73 65 65 20 74 68 65 20 50 47 48 44 52 31 5f 54 see the PGHDR1_T
10af9 4f 5f 50 41 47 45 28 29 20 0a 2a 2a 20 6d 61 63 O_PAGE() .** mac
10afa 72 6f 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a 73 74 ro below)..*/.st
10afb 72 75 63 74 20 50 67 48 64 72 31 20 7b 0a 20 20 ruct PgHdr1 {.
10afc 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 unsigned int iKe
10afd 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f y; /
10afe 2a 20 4b 65 79 20 76 61 6c 75 65 20 28 70 61 67 * Key value (pag
10aff 65 20 6e 75 6d 62 65 72 29 20 2a 2f 0a 20 20 50 e number) */. P
10b00 67 48 64 72 31 20 2a 70 4e 65 78 74 3b 20 20 20 gHdr1 *pNext;
10b01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10b02 20 4e 65 78 74 20 69 6e 20 68 61 73 68 20 74 61 Next in hash ta
10b03 62 6c 65 20 63 68 61 69 6e 20 2a 2f 0a 20 20 50 ble chain */. P
10b04 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 3b 20 Cache1 *pCache;
10b05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10b06 20 43 61 63 68 65 20 74 68 61 74 20 63 75 72 72 Cache that curr
10b07 65 6e 74 6c 79 20 6f 77 6e 73 20 74 68 69 73 20 ently owns this
10b08 70 61 67 65 20 2a 2f 0a 20 20 50 67 48 64 72 31 page */. PgHdr1
10b09 20 2a 70 4c 72 75 4e 65 78 74 3b 20 20 20 20 20 *pLruNext;
10b0a 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 /* Next
10b0b 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 in LRU list of
10b0c 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a unpinned pages *
10b0d 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 /. PgHdr1 *pLru
10b0e 50 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 Prev;
10b0f 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 69 /* Previous i
10b10 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e n LRU list of un
10b11 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a pinned pages */.
10b12 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 73 };../*.** Free s
10b13 6c 6f 74 73 20 69 6e 20 74 68 65 20 61 6c 6c 6f lots in the allo
10b14 63 61 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69 cator used to di
10b15 76 69 64 65 20 75 70 20 74 68 65 20 62 75 66 66 vide up the buff
10b16 65 72 20 70 72 6f 76 69 64 65 64 20 75 73 69 6e er provided usin
10b17 67 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f g.** the SQLITE_
10b18 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
10b19 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 73 mechanism..*/.s
10b1a 74 72 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74 truct PgFreeslot
10b1b 20 7b 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20 {. PgFreeslot
10b1c 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 *pNext; /* Next
10b1d 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 7d 3b free slot */.};
10b1e 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 64 ../*.** Global d
10b1f 61 74 61 20 75 73 65 64 20 62 79 20 74 68 69 73 ata used by this
10b20 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 cache..*/.stati
10b21 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 c SQLITE_WSD str
10b22 75 63 74 20 50 43 61 63 68 65 47 6c 6f 62 61 6c uct PCacheGlobal
10b23 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 {. sqlite3_mut
10b24 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 ex *mutex;
10b25 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74 61 74 /* stat
10b26 69 63 20 6d 75 74 65 78 20 4d 55 54 45 58 5f 53 ic mutex MUTEX_S
10b27 54 41 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a 20 20 TATIC_LRU */..
10b28 69 6e 74 20 6e 4d 61 78 50 61 67 65 3b 20 20 20 int nMaxPage;
10b29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b2a 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d /* Sum of nM
10b2b 61 78 50 61 67 65 20 66 6f 72 20 70 75 72 67 65 axPage for purge
10b2c 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 able caches */.
10b2d 20 69 6e 74 20 6e 4d 69 6e 50 61 67 65 3b 20 20 int nMinPage;
10b2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b2f 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e /* Sum of n
10b30 4d 69 6e 50 61 67 65 20 66 6f 72 20 70 75 72 67 MinPage for purg
10b31 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a eable caches */.
10b32 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 50 61 int nCurrentPa
10b33 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
10b34 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
10b35 6f 66 20 70 75 72 67 65 61 62 6c 65 20 70 61 67 of purgeable pag
10b36 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a es allocated */.
10b37 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 48 65 PgHdr1 *pLruHe
10b38 61 64 2c 20 2a 70 4c 72 75 54 61 69 6c 3b 20 20 ad, *pLruTail;
10b39 20 20 20 20 20 20 2f 2a 20 4c 52 55 20 6c 69 73 /* LRU lis
10b3a 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 t of unpinned pa
10b3b 67 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56 61 72 ges */.. /* Var
10b3c 69 61 62 6c 65 73 20 72 65 6c 61 74 65 64 20 74 iables related t
10b3d 6f 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f o SQLITE_CONFIG_
10b3e 50 41 47 45 43 41 43 48 45 20 73 65 74 74 69 6e PAGECACHE settin
10b3f 67 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 gs. */. int szS
10b40 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 lot;
10b41 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10b42 53 69 7a 65 20 6f 66 20 65 61 63 68 20 66 72 65 Size of each fre
10b43 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f 69 64 e slot */. void
10b44 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b *pStart, *pEnd;
10b45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b46 2f 2a 20 42 6f 75 6e 64 73 20 6f 66 20 70 61 67 /* Bounds of pag
10b47 65 63 61 63 68 65 20 6d 61 6c 6c 6f 63 20 72 61 ecache malloc ra
10b48 6e 67 65 20 2a 2f 0a 20 20 50 67 46 72 65 65 73 nge */. PgFrees
10b49 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 20 20 lot *pFree;
10b4a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10b4b 46 72 65 65 20 70 61 67 65 20 62 6c 6f 63 6b 73 Free page blocks
10b4c 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74 */. int isInit
10b4d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
10b4e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
10b4f 65 20 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 64 e if initialized
10b50 20 2a 2f 0a 7d 20 70 63 61 63 68 65 31 5f 67 3b */.} pcache1_g;
10b51 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f 64 65 ../*.** All code
10b52 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 73 68 in this file sh
10b53 6f 75 6c 64 20 61 63 63 65 73 73 20 74 68 65 20 ould access the
10b54 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 global structure
10b55 20 61 62 6f 76 65 20 76 69 61 20 74 68 65 0a 2a above via the.*
10b56 2a 20 61 6c 69 61 73 20 22 70 63 61 63 68 65 31 * alias "pcache1
10b57 22 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 ". This ensures
10b58 74 68 61 74 20 74 68 65 20 57 53 44 20 65 6d 75 that the WSD emu
10b59 6c 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 77 lation is used w
10b5a 68 65 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 hen.** compiling
10b5b 20 66 6f 72 20 73 79 73 74 65 6d 73 20 74 68 61 for systems tha
10b5c 74 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 t do not support
10b5d 20 72 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a 23 64 real WSD..*/.#d
10b5e 65 66 69 6e 65 20 70 63 61 63 68 65 31 20 28 47 efine pcache1 (G
10b5f 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 50 43 61 LOBAL(struct PCa
10b60 63 68 65 47 6c 6f 62 61 6c 2c 20 70 63 61 63 68 cheGlobal, pcach
10b61 65 31 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 e1_g))../*.** Wh
10b62 65 6e 20 61 20 50 67 48 64 72 31 20 73 74 72 75 en a PgHdr1 stru
10b63 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 cture is allocat
10b64 65 64 2c 20 74 68 65 20 61 73 73 6f 63 69 61 74 ed, the associat
10b65 65 64 20 50 43 61 63 68 65 31 2e 73 7a 50 61 67 ed PCache1.szPag
10b66 65 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 64 61 e.** bytes of da
10b67 74 61 20 61 72 65 20 6c 6f 63 61 74 65 64 20 64 ta are located d
10b68 69 72 65 63 74 6c 79 20 62 65 66 6f 72 65 20 69 irectly before i
10b69 74 20 69 6e 20 6d 65 6d 6f 72 79 20 28 69 2e 65 t in memory (i.e
10b6a 2e 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 73 . the total.** s
10b6b 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 ize of the alloc
10b6c 61 74 69 6f 6e 20 69 73 20 73 69 7a 65 6f 66 28 ation is sizeof(
10b6d 50 67 48 64 72 31 29 2b 50 43 61 63 68 65 31 2e PgHdr1)+PCache1.
10b6e 73 7a 50 61 67 65 20 62 79 74 65 29 2e 20 54 68 szPage byte). Th
10b6f 65 0a 2a 2a 20 50 47 48 44 52 31 5f 54 4f 5f 50 e.** PGHDR1_TO_P
10b70 41 47 45 28 29 20 6d 61 63 72 6f 20 74 61 6b 65 AGE() macro take
10b71 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
10b72 20 50 67 48 64 72 31 20 73 74 72 75 63 74 75 72 PgHdr1 structur
10b73 65 20 61 73 0a 2a 2a 20 61 6e 20 61 72 67 75 6d e as.** an argum
10b74 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 73 20 ent and returns
10b75 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
10b76 20 61 73 73 6f 63 69 61 74 65 64 20 62 6c 6f 63 associated bloc
10b77 6b 20 6f 66 20 73 7a 50 61 67 65 0a 2a 2a 20 62 k of szPage.** b
10b78 79 74 65 73 2e 20 54 68 65 20 50 41 47 45 5f 54 ytes. The PAGE_T
10b79 4f 5f 50 47 48 44 52 31 28 29 20 6d 61 63 72 6f O_PGHDR1() macro
10b7a 20 64 6f 65 73 20 74 68 65 20 6f 70 70 6f 73 69 does the opposi
10b7b 74 65 3a 20 69 74 73 20 61 72 67 75 6d 65 6e 74 te: its argument
10b7c 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 is.** a pointer
10b7d 20 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 73 to a block of s
10b7e 7a 50 61 67 65 20 62 79 74 65 73 20 6f 66 20 64 zPage bytes of d
10b7f 61 74 61 20 61 6e 64 20 74 68 65 20 72 65 74 75 ata and the retu
10b80 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 rn value is.** a
10b81 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
10b82 61 73 73 6f 63 69 61 74 65 64 20 50 67 48 64 72 associated PgHdr
10b83 31 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 1 structure..**.
10b84 2a 2a 20 20 20 61 73 73 65 72 74 28 20 50 47 48 ** assert( PGH
10b85 44 52 31 5f 54 4f 5f 50 41 47 45 28 50 41 47 45 DR1_TO_PAGE(PAGE
10b86 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 _TO_PGHDR1(pCach
10b87 65 2c 20 58 29 29 3d 3d 58 20 29 3b 0a 2a 2f 0a e, X))==X );.*/.
10b88 23 64 65 66 69 6e 65 20 50 47 48 44 52 31 5f 54 #define PGHDR1_T
10b89 4f 5f 50 41 47 45 28 70 29 20 20 20 20 28 76 6f O_PAGE(p) (vo
10b8a 69 64 2a 29 28 28 28 63 68 61 72 2a 29 70 29 20 id*)(((char*)p)
10b8b 2d 20 70 2d 3e 70 43 61 63 68 65 2d 3e 73 7a 50 - p->pCache->szP
10b8c 61 67 65 29 0a 23 64 65 66 69 6e 65 20 50 41 47 age).#define PAG
10b8d 45 5f 54 4f 5f 50 47 48 44 52 31 28 63 2c 20 70 E_TO_PGHDR1(c, p
10b8e 29 20 28 50 67 48 64 72 31 2a 29 28 28 28 63 68 ) (PgHdr1*)(((ch
10b8f 61 72 2a 29 70 29 20 2b 20 63 2d 3e 73 7a 50 61 ar*)p) + c->szPa
10b90 67 65 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f ge)../*.** Macro
10b91 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 64 20 6c s to enter and l
10b92 65 61 76 65 20 74 68 65 20 67 6c 6f 62 61 6c 20 eave the global
10b93 4c 52 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a 23 64 LRU mutex..*/.#d
10b94 65 66 69 6e 65 20 70 63 61 63 68 65 31 45 6e 74 efine pcache1Ent
10b95 65 72 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65 erMutex() sqlite
10b96 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 63 3_mutex_enter(pc
10b97 61 63 68 65 31 2e 6d 75 74 65 78 29 0a 23 64 65 ache1.mutex).#de
10b98 66 69 6e 65 20 70 63 61 63 68 65 31 4c 65 61 76 fine pcache1Leav
10b99 65 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65 33 eMutex() sqlite3
10b9a 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 63 61 _mutex_leave(pca
10b9b 63 68 65 31 2e 6d 75 74 65 78 29 0a 0a 2f 2a 2a che1.mutex)../**
10b9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b9f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
10ba1 2a 2a 2a 2a 2a 2a 2a 20 50 61 67 65 20 41 6c 6c ******* Page All
10ba2 6f 63 61 74 69 6f 6e 2f 53 51 4c 49 54 45 5f 43 ocation/SQLITE_C
10ba3 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 52 65 6c ONFIG_PCACHE Rel
10ba4 61 74 65 64 20 46 75 6e 63 74 69 6f 6e 73 20 2a ated Functions *
10ba5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
10ba6 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
10ba7 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75 ion is called du
10ba8 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 ring initializat
10ba9 69 6f 6e 20 69 66 20 61 20 73 74 61 74 69 63 20 ion if a static
10baa 62 75 66 66 65 72 20 69 73 20 0a 2a 2a 20 73 75 buffer is .** su
10bab 70 70 6c 69 65 64 20 74 6f 20 75 73 65 20 66 6f pplied to use fo
10bac 72 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 r the page-cache
10bad 20 62 79 20 70 61 73 73 69 6e 67 20 74 68 65 20 by passing the
10bae 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 SQLITE_CONFIG_PA
10baf 47 45 43 41 43 48 45 0a 2a 2a 20 76 65 72 62 20 GECACHE.** verb
10bb0 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 to sqlite3_confi
10bb1 67 28 29 2e 20 50 61 72 61 6d 65 74 65 72 20 70 g(). Parameter p
10bb2 42 75 66 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e Buf points to an
10bb3 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67 allocation larg
10bb4 65 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 63 e.** enough to c
10bb5 6f 6e 74 61 69 6e 20 27 6e 27 20 62 75 66 66 65 ontain 'n' buffe
10bb6 72 73 20 6f 66 20 27 73 7a 27 20 62 79 74 65 73 rs of 'sz' bytes
10bb7 20 65 61 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 each..*/.SQLITE
10bb8 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
10bb9 6c 69 74 65 33 50 43 61 63 68 65 42 75 66 66 65 lite3PCacheBuffe
10bba 72 53 65 74 75 70 28 76 6f 69 64 20 2a 70 42 75 rSetup(void *pBu
10bbb 66 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e f, int sz, int n
10bbc 29 7b 0a 20 20 69 66 28 20 70 63 61 63 68 65 31 ){. if( pcache1
10bbd 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 50 .isInit ){. P
10bbe 67 46 72 65 65 73 6c 6f 74 20 2a 70 3b 0a 20 20 gFreeslot *p;.
10bbf 20 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e sz = ROUNDDOWN
10bc0 38 28 73 7a 29 3b 0a 20 20 20 20 70 63 61 63 68 8(sz);. pcach
10bc1 65 31 2e 73 7a 53 6c 6f 74 20 3d 20 73 7a 3b 0a e1.szSlot = sz;.
10bc2 20 20 20 20 70 63 61 63 68 65 31 2e 70 53 74 61 pcache1.pSta
10bc3 72 74 20 3d 20 70 42 75 66 3b 0a 20 20 20 20 70 rt = pBuf;. p
10bc4 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 30 cache1.pFree = 0
10bc5 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 2d 2d ;. while( n--
10bc6 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 28 50 ){. p = (P
10bc7 67 46 72 65 65 73 6c 6f 74 2a 29 70 42 75 66 3b gFreeslot*)pBuf;
10bc8 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 . p->pNext
10bc9 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b = pcache1.pFree;
10bca 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 . pcache1.p
10bcb 46 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 20 20 Free = p;.
10bcc 70 42 75 66 20 3d 20 28 76 6f 69 64 2a 29 26 28 pBuf = (void*)&(
10bcd 28 63 68 61 72 2a 29 70 42 75 66 29 5b 73 7a 5d (char*)pBuf)[sz]
10bce 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 ;. }. pcac
10bcf 68 65 31 2e 70 45 6e 64 20 3d 20 70 42 75 66 3b he1.pEnd = pBuf;
10bd0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 . }.}../*.** Ma
10bd1 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 73 lloc function us
10bd2 65 64 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 ed within this f
10bd3 69 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 ile to allocate
10bd4 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 space from the b
10bd5 75 66 66 65 72 0a 2a 2a 20 63 6f 6e 66 69 67 75 uffer.** configu
10bd6 72 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 red using sqlite
10bd7 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 3_config(SQLITE_
10bd8 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
10bd9 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 ) option. If no
10bda 0a 2a 2a 20 73 75 63 68 20 62 75 66 66 65 72 20 .** such buffer
10bdb 65 78 69 73 74 73 20 6f 72 20 74 68 65 72 65 20 exists or there
10bdc 69 73 20 6e 6f 20 73 70 61 63 65 20 6c 65 66 74 is no space left
10bdd 20 69 6e 20 69 74 2c 20 74 68 69 73 20 66 75 6e in it, this fun
10bde 63 74 69 6f 6e 20 66 61 6c 6c 73 20 0a 2a 2a 20 ction falls .**
10bdf 62 61 63 6b 20 74 6f 20 73 71 6c 69 74 65 33 4d back to sqlite3M
10be0 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 alloc()..*/.stat
10be1 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68 65 31 ic void *pcache1
10be2 41 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 Alloc(int nByte)
10be3 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 {. void *p;. a
10be4 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
10be5 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 utex_held(pcache
10be6 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 1.mutex) );. if
10be7 28 20 6e 42 79 74 65 3c 3d 70 63 61 63 68 65 31 ( nByte<=pcache1
10be8 2e 73 7a 53 6c 6f 74 20 26 26 20 70 63 61 63 68 .szSlot && pcach
10be9 65 31 2e 70 46 72 65 65 20 29 7b 0a 20 20 20 20 e1.pFree ){.
10bea 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31 2e assert( pcache1.
10beb 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 70 20 isInit );. p
10bec 3d 20 28 50 67 48 64 72 31 20 2a 29 70 63 61 63 = (PgHdr1 *)pcac
10bed 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 he1.pFree;. p
10bee 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70 cache1.pFree = p
10bef 63 61 63 68 65 31 2e 70 46 72 65 65 2d 3e 70 4e cache1.pFree->pN
10bf0 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ext;. sqlite3
10bf1 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 StatusSet(SQLITE
10bf2 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 _STATUS_PAGECACH
10bf3 45 5f 53 49 5a 45 2c 20 6e 42 79 74 65 29 3b 0a E_SIZE, nByte);.
10bf4 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 sqlite3Statu
10bf5 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 sAdd(SQLITE_STAT
10bf6 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 US_PAGECACHE_USE
10bf7 44 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a D, 1);. }else{.
10bf8 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 . /* Allocate
10bf9 20 61 20 6e 65 77 20 62 75 66 66 65 72 20 75 73 a new buffer us
10bfa 69 6e 67 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f ing sqlite3Mallo
10bfb 63 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 c. Before doing
10bfc 73 6f 2c 20 65 78 69 74 20 74 68 65 0a 20 20 20 so, exit the.
10bfd 20 2a 2a 20 67 6c 6f 62 61 6c 20 70 63 61 63 68 ** global pcach
10bfe 65 20 6d 75 74 65 78 20 61 6e 64 20 75 6e 6c 6f e mutex and unlo
10bff 63 6b 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 ck the pager-cac
10c00 68 65 20 6f 62 6a 65 63 74 20 70 43 61 63 68 65 he object pCache
10c01 2e 20 54 68 69 73 20 69 73 20 0a 20 20 20 20 2a . This is . *
10c02 2a 20 73 6f 20 74 68 61 74 20 69 66 20 74 68 65 * so that if the
10c03 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f attempt to allo
10c04 63 61 74 65 20 61 20 6e 65 77 20 62 75 66 66 65 cate a new buffe
10c05 72 20 63 61 75 73 65 73 20 74 68 65 20 74 68 65 r causes the the
10c06 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 66 69 67 75 . ** configu
10c07 72 65 64 20 73 6f 66 74 2d 68 65 61 70 2d 6c 69 red soft-heap-li
10c08 6d 69 74 20 74 6f 20 62 65 20 62 72 65 61 63 68 mit to be breach
10c09 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 70 ed, it will be p
10c0a 6f 73 73 69 62 6c 65 20 74 6f 0a 20 20 20 20 2a ossible to. *
10c0b 2a 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 * reclaim memory
10c0c 20 66 72 6f 6d 20 74 68 69 73 20 70 61 67 65 72 from this pager
10c0d 2d 63 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 -cache.. */.
10c0e 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d pcache1LeaveM
10c0f 75 74 65 78 28 29 3b 0a 20 20 20 20 70 20 3d 20 utex();. p =
10c10 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 sqlite3Malloc(nB
10c11 79 74 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 yte);. pcache
10c12 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 1EnterMutex();.
10c13 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 if( p ){.
10c14 20 20 69 6e 74 20 73 7a 20 3d 20 73 71 6c 69 74 int sz = sqlit
10c15 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b e3MallocSize(p);
10c16 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 . sqlite3St
10c17 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 atusAdd(SQLITE_S
10c18 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f TATUS_PAGECACHE_
10c19 4f 56 45 52 46 4c 4f 57 2c 20 73 7a 29 3b 0a 20 OVERFLOW, sz);.
10c1a 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
10c1b 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 n p;.}../*.** Fr
10c1c 65 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 ee an allocated
10c1d 62 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 buffer obtained
10c1e 66 72 6f 6d 20 70 63 61 63 68 65 31 41 6c 6c 6f from pcache1Allo
10c1f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 c()..*/.static v
10c20 6f 69 64 20 70 63 61 63 68 65 31 46 72 65 65 28 oid pcache1Free(
10c21 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 void *p){. asse
10c22 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
10c23 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d x_held(pcache1.m
10c24 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 utex) );. if( p
10c25 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
10c26 69 66 28 20 70 3e 3d 70 63 61 63 68 65 31 2e 70 if( p>=pcache1.p
10c27 53 74 61 72 74 20 26 26 20 70 3c 70 63 61 63 68 Start && p<pcach
10c28 65 31 2e 70 45 6e 64 20 29 7b 0a 20 20 20 20 50 e1.pEnd ){. P
10c29 67 46 72 65 65 73 6c 6f 74 20 2a 70 53 6c 6f 74 gFreeslot *pSlot
10c2a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 ;. sqlite3Sta
10c2b 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
10c2c 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 ATUS_PAGECACHE_U
10c2d 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20 70 53 SED, -1);. pS
10c2e 6c 6f 74 20 3d 20 28 50 67 46 72 65 65 73 6c 6f lot = (PgFreeslo
10c2f 74 2a 29 70 3b 0a 20 20 20 20 70 53 6c 6f 74 2d t*)p;. pSlot-
10c30 3e 70 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31 >pNext = pcache1
10c31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 61 63 .pFree;. pcac
10c32 68 65 31 2e 70 46 72 65 65 20 3d 20 70 53 6c 6f he1.pFree = pSlo
10c33 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 t;. }else{.
10c34 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69 int iSize = sqli
10c35 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 te3MallocSize(p)
10c36 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 ;. sqlite3Sta
10c37 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
10c38 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f ATUS_PAGECACHE_O
10c39 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 VERFLOW, -iSize)
10c3a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 ;. sqlite3_fr
10c3b 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ee(p);. }.}../*
10c3c 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e .** Allocate a n
10c3d 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 ew page object i
10c3e 6e 69 74 69 61 6c 6c 79 20 61 73 73 6f 63 69 61 nitially associa
10c3f 74 65 64 20 77 69 74 68 20 63 61 63 68 65 20 70 ted with cache p
10c40 43 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 Cache..*/.static
10c41 20 50 67 48 64 72 31 20 2a 70 63 61 63 68 65 31 PgHdr1 *pcache1
10c42 41 6c 6c 6f 63 50 61 67 65 28 50 43 61 63 68 65 AllocPage(PCache
10c43 31 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 1 *pCache){. in
10c44 74 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 t nByte = sizeof
10c45 28 50 67 48 64 72 31 29 20 2b 20 70 43 61 63 68 (PgHdr1) + pCach
10c46 65 2d 3e 73 7a 50 61 67 65 3b 0a 20 20 76 6f 69 e->szPage;. voi
10c47 64 20 2a 70 50 67 20 3d 20 70 63 61 63 68 65 31 d *pPg = pcache1
10c48 41 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20 Alloc(nByte);.
10c49 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 69 66 28 PgHdr1 *p;. if(
10c4a 20 70 50 67 20 29 7b 0a 20 20 20 20 70 20 3d 20 pPg ){. p =
10c4b 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 PAGE_TO_PGHDR1(p
10c4c 43 61 63 68 65 2c 20 70 50 67 29 3b 0a 20 20 20 Cache, pPg);.
10c4d 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 if( pCache->bPu
10c4e 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 rgeable ){.
10c4f 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e pcache1.nCurren
10c50 74 50 61 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 tPage++;. }.
10c51 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 }else{. p =
10c52 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
10c53 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 p;.}../*.** Free
10c54 20 61 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61 a page object a
10c55 6c 6c 6f 63 61 74 65 64 20 62 79 20 70 63 61 63 llocated by pcac
10c56 68 65 31 41 6c 6c 6f 63 50 61 67 65 28 29 2e 0a he1AllocPage()..
10c57 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 **.** The pointe
10c58 72 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 r is allowed to
10c59 62 65 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 be NULL, which i
10c5a 73 20 70 72 75 64 65 6e 74 2e 20 20 42 75 74 20 s prudent. But
10c5b 69 74 20 74 75 72 6e 73 20 6f 75 74 0a 2a 2a 20 it turns out.**
10c5c 74 68 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 that the current
10c5d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
10c5e 68 61 70 70 65 6e 73 20 74 6f 20 6e 65 76 65 72 happens to never
10c5f 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 call this routi
10c60 6e 65 0a 2a 2a 20 77 69 74 68 20 61 20 4e 55 4c ne.** with a NUL
10c61 4c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 77 65 L pointer, so we
10c62 20 6d 61 72 6b 20 74 68 65 20 4e 55 4c 4c 20 74 mark the NULL t
10c63 65 73 74 20 77 69 74 68 20 41 4c 57 41 59 53 28 est with ALWAYS(
10c64 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
10c65 64 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 d pcache1FreePag
10c66 65 28 50 67 48 64 72 31 20 2a 70 29 7b 0a 20 20 e(PgHdr1 *p){.
10c67 69 66 28 20 41 4c 57 41 59 53 28 70 29 20 29 7b if( ALWAYS(p) ){
10c68 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 43 61 63 . if( p->pCac
10c69 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 he->bPurgeable )
10c6a 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e {. pcache1.
10c6b 6e 43 75 72 72 65 6e 74 50 61 67 65 2d 2d 3b 0a nCurrentPage--;.
10c6c 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65 }. pcache
10c6d 31 46 72 65 65 28 50 47 48 44 52 31 5f 54 4f 5f 1Free(PGHDR1_TO_
10c6e 50 41 47 45 28 70 29 29 3b 0a 20 20 7d 0a 7d 0a PAGE(p));. }.}.
10c6f 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75 ./*.** Malloc fu
10c70 6e 63 74 69 6f 6e 20 75 73 65 64 20 62 79 20 53 nction used by S
10c71 51 4c 69 74 65 20 74 6f 20 6f 62 74 61 69 6e 20 QLite to obtain
10c72 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 space from the b
10c73 75 66 66 65 72 20 63 6f 6e 66 69 67 75 72 65 64 uffer configured
10c74 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65 .** using sqlite
10c75 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 3_config(SQLITE_
10c76 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
10c77 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 ) option. If no
10c78 73 75 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 65 such buffer.** e
10c79 78 69 73 74 73 2c 20 74 68 69 73 20 66 75 6e 63 xists, this func
10c7a 74 69 6f 6e 20 66 61 6c 6c 73 20 62 61 63 6b 20 tion falls back
10c7b 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 to sqlite3Malloc
10c7c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ()..*/.SQLITE_PR
10c7d 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
10c7e 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e te3PageMalloc(in
10c7f 74 20 73 7a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 t sz){. void *p
10c80 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
10c81 4d 75 74 65 78 28 29 3b 0a 20 20 70 20 3d 20 70 Mutex();. p = p
10c82 63 61 63 68 65 31 41 6c 6c 6f 63 28 73 7a 29 3b cache1Alloc(sz);
10c83 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d . pcache1LeaveM
10c84 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e utex();. return
10c85 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 p;.}../*.** Fre
10c86 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 e an allocated b
10c87 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 uffer obtained f
10c88 72 6f 6d 20 73 71 6c 69 74 65 33 50 61 67 65 4d rom sqlite3PageM
10c89 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 alloc()..*/.SQLI
10c8a 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
10c8b 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 sqlite3PageFree(
10c8c 76 6f 69 64 20 2a 70 29 7b 0a 20 20 70 63 61 63 void *p){. pcac
10c8d 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b he1EnterMutex();
10c8e 0a 20 20 70 63 61 63 68 65 31 46 72 65 65 28 70 . pcache1Free(p
10c8f 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 );. pcache1Leav
10c90 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 2a eMutex();.}../**
10c91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
10c96 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 ******* General
10c97 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 46 Implementation F
10c98 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a unctions *******
10c99 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c9a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
10c9b 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
10c9c 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 ion is used to r
10c9d 65 73 69 7a 65 20 74 68 65 20 68 61 73 68 20 74 esize the hash t
10c9e 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 68 65 able used by the
10c9f 20 63 61 63 68 65 20 70 61 73 73 65 64 0a 2a 2a cache passed.**
10ca0 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 as the first ar
10ca1 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 gument..**.** Th
10ca2 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d e global mutex m
10ca3 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e ust be held when
10ca4 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
10ca5 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 s called..*/.sta
10ca6 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 52 tic int pcache1R
10ca7 65 73 69 7a 65 48 61 73 68 28 50 43 61 63 68 65 esizeHash(PCache
10ca8 31 20 2a 70 29 7b 0a 20 20 50 67 48 64 72 31 20 1 *p){. PgHdr1
10ca9 2a 2a 61 70 4e 65 77 3b 0a 20 20 75 6e 73 69 67 **apNew;. unsig
10caa 6e 65 64 20 69 6e 74 20 6e 4e 65 77 3b 0a 20 20 ned int nNew;.
10cab 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a unsigned int i;.
10cac 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
10cad 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 e3_mutex_held(pc
10cae 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a ache1.mutex) );.
10caf 0a 20 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 48 61 . nNew = p->nHa
10cb0 73 68 2a 32 3b 0a 20 20 69 66 28 20 6e 4e 65 77 sh*2;. if( nNew
10cb1 3c 32 35 36 20 29 7b 0a 20 20 20 20 6e 4e 65 77 <256 ){. nNew
10cb2 20 3d 20 32 35 36 3b 0a 20 20 7d 0a 0a 20 20 70 = 256;. }.. p
10cb3 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 cache1LeaveMutex
10cb4 28 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48 61 ();. if( p->nHa
10cb5 73 68 20 29 7b 20 73 71 6c 69 74 65 33 42 65 67 sh ){ sqlite3Beg
10cb6 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 inBenignMalloc()
10cb7 3b 20 7d 0a 20 20 61 70 4e 65 77 20 3d 20 28 50 ; }. apNew = (P
10cb8 67 48 64 72 31 20 2a 2a 29 73 71 6c 69 74 65 33 gHdr1 **)sqlite3
10cb9 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50 _malloc(sizeof(P
10cba 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a gHdr1 *)*nNew);.
10cbb 20 20 69 66 28 20 70 2d 3e 6e 48 61 73 68 20 29 if( p->nHash )
10cbc 7b 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 { sqlite3EndBeni
10cbd 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20 gnMalloc(); }.
10cbe 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 pcache1EnterMute
10cbf 78 28 29 3b 0a 20 20 69 66 28 20 61 70 4e 65 77 x();. if( apNew
10cc0 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 61 ){. memset(a
10cc1 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 pNew, 0, sizeof(
10cc2 50 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b PgHdr1 *)*nNew);
10cc3 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
10cc4 70 2d 3e 6e 48 61 73 68 3b 20 69 2b 2b 29 7b 0a p->nHash; i++){.
10cc5 20 20 20 20 20 20 50 67 48 64 72 31 20 2a 70 50 PgHdr1 *pP
10cc6 61 67 65 3b 0a 20 20 20 20 20 20 50 67 48 64 72 age;. PgHdr
10cc7 31 20 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 61 70 1 *pNext = p->ap
10cc8 48 61 73 68 5b 69 5d 3b 0a 20 20 20 20 20 20 77 Hash[i];. w
10cc9 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d 20 70 hile( (pPage = p
10cca 4e 65 78 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 Next)!=0 ){.
10ccb 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 unsigned int
10ccc 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79 h = pPage->iKey
10ccd 20 25 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20 20 % nNew;.
10cce 20 70 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e pNext = pPage->
10ccf 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70 pNext;. p
10cd0 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 61 70 Page->pNext = ap
10cd1 4e 65 77 5b 68 5d 3b 0a 20 20 20 20 20 20 20 20 New[h];.
10cd2 61 70 4e 65 77 5b 68 5d 20 3d 20 70 50 61 67 65 apNew[h] = pPage
10cd3 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
10cd4 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
10cd5 28 70 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 20 (p->apHash);.
10cd6 20 70 2d 3e 61 70 48 61 73 68 20 3d 20 61 70 4e p->apHash = apN
10cd7 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 48 61 73 68 ew;. p->nHash
10cd8 20 3d 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 = nNew;. }..
10cd9 72 65 74 75 72 6e 20 28 70 2d 3e 61 70 48 61 73 return (p->apHas
10cda 68 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 h ? SQLITE_OK :
10cdb 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d SQLITE_NOMEM);.}
10cdc 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
10cdd 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e ction is used in
10cde 74 65 72 6e 61 6c 6c 79 20 74 6f 20 72 65 6d 6f ternally to remo
10cdf 76 65 20 74 68 65 20 70 61 67 65 20 70 50 61 67 ve the page pPag
10ce0 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 67 e from the .** g
10ce1 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2c 20 lobal LRU list,
10ce2 69 66 20 69 73 20 70 61 72 74 20 6f 66 20 69 74 if is part of it
10ce3 2e 20 49 66 20 70 50 61 67 65 20 69 73 20 6e 6f . If pPage is no
10ce4 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c t part of the gl
10ce5 6f 62 61 6c 0a 2a 2a 20 4c 52 55 20 6c 69 73 74 obal.** LRU list
10ce6 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 , then this func
10ce7 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tion is a no-op.
10ce8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 .**.** The globa
10ce9 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 l mutex must be
10cea 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 held when this f
10ceb 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
10cec 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
10ced 64 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 d pcache1PinPage
10cee 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b (PgHdr1 *pPage){
10cef 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
10cf0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 e3_mutex_held(pc
10cf1 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a ache1.mutex) );.
10cf2 20 20 69 66 28 20 70 50 61 67 65 20 26 26 20 28 if( pPage && (
10cf3 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 pPage->pLruNext
10cf4 7c 7c 20 70 50 61 67 65 3d 3d 70 63 61 63 68 65 || pPage==pcache
10cf5 31 2e 70 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20 1.pLruTail) ){.
10cf6 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c if( pPage->pL
10cf7 72 75 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 ruPrev ){.
10cf8 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 2d pPage->pLruPrev-
10cf9 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 50 61 67 >pLruNext = pPag
10cfa 65 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 e->pLruNext;.
10cfb 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 }. if( pPage
10cfc 2d 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20 ->pLruNext ){.
10cfd 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e pPage->pLruN
10cfe 65 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 ext->pLruPrev =
10cff 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b pPage->pLruPrev;
10d00 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
10d01 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3d cache1.pLruHead=
10d02 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 =pPage ){.
10d03 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 pcache1.pLruHead
10d04 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 = pPage->pLruNe
10d05 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 xt;. }. if
10d06 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 ( pcache1.pLruTa
10d07 69 6c 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 il==pPage ){.
10d08 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 pcache1.pLruT
10d09 61 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 ail = pPage->pLr
10d0a 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 uPrev;. }.
10d0b 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 pPage->pLruNext
10d0c 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d = 0;. pPage-
10d0d 3e 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 >pLruPrev = 0;.
10d0e 20 20 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 pPage->pCache
10d0f 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2d 2d 3b ->nRecyclable--;
10d10 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 . }.}.../*.** R
10d11 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 73 emove the page s
10d12 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 upplied as an ar
10d13 67 75 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 gument from the
10d14 68 61 73 68 20 74 61 62 6c 65 20 0a 2a 2a 20 28 hash table .** (
10d15 50 43 61 63 68 65 31 2e 61 70 48 61 73 68 20 73 PCache1.apHash s
10d16 74 72 75 63 74 75 72 65 29 20 74 68 61 74 20 69 tructure) that i
10d17 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 73 t is currently s
10d18 74 6f 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 tored in..**.**
10d19 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 The global mutex
10d1a 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 must be held wh
10d1b 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
10d1c 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 is called..*/.s
10d1d 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 tatic void pcach
10d1e 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 e1RemoveFromHash
10d1f 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b (PgHdr1 *pPage){
10d20 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
10d21 68 3b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 h;. PCache1 *pC
10d22 61 63 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43 ache = pPage->pC
10d23 61 63 68 65 3b 0a 20 20 50 67 48 64 72 31 20 2a ache;. PgHdr1 *
10d24 2a 70 70 3b 0a 0a 20 20 68 20 3d 20 70 50 61 67 *pp;.. h = pPag
10d25 65 2d 3e 69 4b 65 79 20 25 20 70 43 61 63 68 65 e->iKey % pCache
10d26 2d 3e 6e 48 61 73 68 3b 0a 20 20 66 6f 72 28 70 ->nHash;. for(p
10d27 70 3d 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73 p=&pCache->apHas
10d28 68 5b 68 5d 3b 20 28 2a 70 70 29 21 3d 70 50 61 h[h]; (*pp)!=pPa
10d29 67 65 3b 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70 ge; pp=&(*pp)->p
10d2a 4e 65 78 74 29 3b 0a 20 20 2a 70 70 20 3d 20 28 Next);. *pp = (
10d2b 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 *pp)->pNext;..
10d2c 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b pCache->nPage--;
10d2d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
10d2e 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 re are currently
10d2f 20 6d 6f 72 65 20 74 68 61 6e 20 70 63 61 63 68 more than pcach
10d30 65 2e 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73 e.nMaxPage pages
10d31 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a allocated, try.
10d32 2a 2a 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61 ** to recycle pa
10d33 67 65 73 20 74 6f 20 72 65 64 75 63 65 20 74 68 ges to reduce th
10d34 65 20 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74 e number allocat
10d35 65 64 20 74 6f 20 70 63 61 63 68 65 2e 6e 4d 61 ed to pcache.nMa
10d36 78 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 xPage..*/.static
10d37 20 76 6f 69 64 20 70 63 61 63 68 65 31 45 6e 66 void pcache1Enf
10d38 6f 72 63 65 4d 61 78 50 61 67 65 28 76 6f 69 64 orceMaxPage(void
10d39 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
10d3a 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
10d3b 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 pcache1.mutex) )
10d3c 3b 0a 20 20 77 68 69 6c 65 28 20 70 63 61 63 68 ;. while( pcach
10d3d 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e e1.nCurrentPage>
10d3e 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 pcache1.nMaxPage
10d3f 20 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 && pcache1.pLru
10d40 54 61 69 6c 20 29 7b 0a 20 20 20 20 50 67 48 64 Tail ){. PgHd
10d41 72 31 20 2a 70 20 3d 20 70 63 61 63 68 65 31 2e r1 *p = pcache1.
10d42 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 63 pLruTail;. pc
10d43 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b ache1PinPage(p);
10d44 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f . pcache1Remo
10d45 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20 veFromHash(p);.
10d46 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61 pcache1FreePa
10d47 67 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ge(p);. }.}../*
10d48 0a 2a 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20 .** Discard all
10d49 70 61 67 65 73 20 66 72 6f 6d 20 63 61 63 68 65 pages from cache
10d4a 20 70 43 61 63 68 65 20 77 69 74 68 20 61 20 70 pCache with a p
10d4b 61 67 65 20 6e 75 6d 62 65 72 20 28 6b 65 79 20 age number (key
10d4c 76 61 6c 75 65 29 20 0a 2a 2a 20 67 72 65 61 74 value) .** great
10d4d 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c er than or equal
10d4e 20 74 6f 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20 to iLimit. Any
10d4f 70 69 6e 6e 65 64 20 70 61 67 65 73 20 74 68 61 pinned pages tha
10d50 74 20 6d 65 65 74 20 74 68 69 73 20 0a 2a 2a 20 t meet this .**
10d51 63 72 69 74 65 72 69 61 20 61 72 65 20 75 6e 70 criteria are unp
10d52 69 6e 6e 65 64 20 62 65 66 6f 72 65 20 74 68 65 inned before the
10d53 79 20 61 72 65 20 64 69 73 63 61 72 64 65 64 2e y are discarded.
10d54 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 .**.** The globa
10d55 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 l mutex must be
10d56 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 held when this f
10d57 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
10d58 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
10d59 64 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 d pcache1Truncat
10d5a 65 55 6e 73 61 66 65 28 0a 20 20 50 43 61 63 68 eUnsafe(. PCach
10d5b 65 31 20 2a 70 43 61 63 68 65 2c 20 0a 20 20 75 e1 *pCache, . u
10d5c 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d nsigned int iLim
10d5d 69 74 20 0a 29 7b 0a 20 20 54 45 53 54 4f 4e 4c it .){. TESTONL
10d5e 59 28 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 Y( unsigned int
10d5f 6e 50 61 67 65 20 3d 20 30 3b 20 29 20 20 20 20 nPage = 0; )
10d60 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 61 73 73 /* Used to ass
10d61 65 72 74 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 ert pCache->nPag
10d62 65 20 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a e is correct */.
10d63 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 unsigned int h
10d64 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
10d65 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
10d66 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b cache1.mutex) );
10d67 0a 20 20 66 6f 72 28 68 3d 30 3b 20 68 3c 70 43 . for(h=0; h<pC
10d68 61 63 68 65 2d 3e 6e 48 61 73 68 3b 20 68 2b 2b ache->nHash; h++
10d69 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a 2a ){. PgHdr1 **
10d6a 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 pp = &pCache->ap
10d6b 48 61 73 68 5b 68 5d 3b 20 0a 20 20 20 20 50 67 Hash[h]; . Pg
10d6c 48 64 72 31 20 2a 70 50 61 67 65 3b 0a 20 20 20 Hdr1 *pPage;.
10d6d 20 77 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d while( (pPage =
10d6e 20 2a 70 70 29 21 3d 30 20 29 7b 0a 20 20 20 20 *pp)!=0 ){.
10d6f 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 4b 65 if( pPage->iKe
10d70 79 3e 3d 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 y>=iLimit ){.
10d71 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 61 pCache->nPa
10d72 67 65 2d 2d 3b 0a 20 20 20 20 20 20 20 20 2a 70 ge--;. *p
10d73 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 p = pPage->pNext
10d74 3b 0a 20 20 20 20 20 20 20 20 70 63 61 63 68 65 ;. pcache
10d75 31 50 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 1PinPage(pPage);
10d76 0a 20 20 20 20 20 20 20 20 70 63 61 63 68 65 31 . pcache1
10d77 46 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b FreePage(pPage);
10d78 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
10d79 20 20 20 20 20 20 70 70 20 3d 20 26 70 50 61 67 pp = &pPag
10d7a 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 e->pNext;.
10d7b 20 20 54 45 53 54 4f 4e 4c 59 28 20 6e 50 61 67 TESTONLY( nPag
10d7c 65 2b 2b 3b 20 29 0a 20 20 20 20 20 20 7d 0a 20 e++; ). }.
10d7d 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 }. }. asser
10d7e 74 28 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 t( pCache->nPage
10d7f 3d 3d 6e 50 61 67 65 20 29 3b 0a 7d 0a 0a 2f 2a ==nPage );.}../*
10d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10d81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10d82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10d83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10d84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
10d85 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 ******** sqlite3
10d86 5f 70 63 61 63 68 65 20 4d 65 74 68 6f 64 73 20 _pcache Methods
10d87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10d88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10d89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
10d8a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
10d8b 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c ation of the sql
10d8c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 49 6e 69 ite3_pcache.xIni
10d8d 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 t method..*/.sta
10d8e 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 49 tic int pcache1I
10d8f 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 nit(void *NotUse
10d90 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 d){. UNUSED_PAR
10d91 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
10d92 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 63 68 . assert( pcach
10d93 65 31 2e 69 73 49 6e 69 74 3d 3d 30 20 29 3b 0a e1.isInit==0 );.
10d94 20 20 6d 65 6d 73 65 74 28 26 70 63 61 63 68 65 memset(&pcache
10d95 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 63 61 1, 0, sizeof(pca
10d96 63 68 65 31 29 29 3b 0a 20 20 69 66 28 20 73 71 che1));. if( sq
10d97 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
10d98 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a g.bCoreMutex ){.
10d99 20 20 20 20 70 63 61 63 68 65 31 2e 6d 75 74 65 pcache1.mute
10d9a 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 x = sqlite3_mute
10d9b 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d x_alloc(SQLITE_M
10d9c 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 29 UTEX_STATIC_LRU)
10d9d 3b 0a 20 20 7d 0a 20 20 70 63 61 63 68 65 31 2e ;. }. pcache1.
10d9e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 72 65 isInit = 1;. re
10d9f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
10da0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 }../*.** Impleme
10da1 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 ntation of the s
10da2 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 53 qlite3_pcache.xS
10da3 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 2e 0a hutdown method..
10da4 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 ** Note that the
10da5 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 61 6c static mutex al
10da6 6c 6f 63 61 74 65 64 20 69 6e 20 78 49 6e 69 74 located in xInit
10da7 20 64 6f 65 73 20 0a 2a 2a 20 6e 6f 74 20 6e 65 does .** not ne
10da8 65 64 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a ed to be freed..
10da9 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
10daa 63 61 63 68 65 31 53 68 75 74 64 6f 77 6e 28 76 cache1Shutdown(v
10dab 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 oid *NotUsed){.
10dac 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
10dad 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 R(NotUsed);. as
10dae 73 65 72 74 28 20 70 63 61 63 68 65 31 2e 69 73 sert( pcache1.is
10daf 49 6e 69 74 21 3d 30 20 29 3b 0a 20 20 6d 65 6d Init!=0 );. mem
10db0 73 65 74 28 26 70 63 61 63 68 65 31 2c 20 30 2c set(&pcache1, 0,
10db1 20 73 69 7a 65 6f 66 28 70 63 61 63 68 65 31 29 sizeof(pcache1)
10db2 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c );.}../*.** Impl
10db3 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
10db4 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 e sqlite3_pcache
10db5 2e 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 2e .xCreate method.
10db6 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 .**.** Allocate
10db7 61 20 6e 65 77 20 63 61 63 68 65 2e 0a 2a 2f 0a a new cache..*/.
10db8 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 70 static sqlite3_p
10db9 63 61 63 68 65 20 2a 70 63 61 63 68 65 31 43 72 cache *pcache1Cr
10dba 65 61 74 65 28 69 6e 74 20 73 7a 50 61 67 65 2c eate(int szPage,
10dbb 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29 int bPurgeable)
10dbc 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 {. PCache1 *pCa
10dbd 63 68 65 3b 0a 0a 20 20 70 43 61 63 68 65 20 3d che;.. pCache =
10dbe 20 28 50 43 61 63 68 65 31 20 2a 29 73 71 6c 69 (PCache1 *)sqli
10dbf 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f te3_malloc(sizeo
10dc0 66 28 50 43 61 63 68 65 31 29 29 3b 0a 20 20 69 f(PCache1));. i
10dc1 66 28 20 70 43 61 63 68 65 20 29 7b 0a 20 20 20 f( pCache ){.
10dc2 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65 2c 20 memset(pCache,
10dc3 30 2c 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 0, sizeof(PCache
10dc4 31 29 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 1));. pCache-
10dc5 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65 >szPage = szPage
10dc6 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 62 50 ;. pCache->bP
10dc7 75 72 67 65 61 62 6c 65 20 3d 20 28 62 50 75 72 urgeable = (bPur
10dc8 67 65 61 62 6c 65 20 3f 20 31 20 3a 20 30 29 3b geable ? 1 : 0);
10dc9 0a 20 20 20 20 69 66 28 20 62 50 75 72 67 65 61 . if( bPurgea
10dca 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 43 61 ble ){. pCa
10dcb 63 68 65 2d 3e 6e 4d 69 6e 20 3d 20 31 30 3b 0a che->nMin = 10;.
10dcc 20 20 20 20 20 20 70 63 61 63 68 65 31 45 6e 74 pcache1Ent
10dcd 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 erMutex();.
10dce 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 pcache1.nMinPag
10dcf 65 20 2b 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 e += pCache->nMi
10dd0 6e 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 n;. pcache1
10dd1 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
10dd2 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
10dd3 20 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 (sqlite3_pcache
10dd4 20 2a 29 70 43 61 63 68 65 3b 0a 7d 0a 0a 2f 2a *)pCache;.}../*
10dd5 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 .** Implementati
10dd6 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 on of the sqlite
10dd7 33 5f 70 63 61 63 68 65 2e 78 43 61 63 68 65 73 3_pcache.xCaches
10dd8 69 7a 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a ize method. .**.
10dd9 2a 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 ** Configure the
10dda 20 63 61 63 68 65 5f 73 69 7a 65 20 6c 69 6d 69 cache_size limi
10ddb 74 20 66 6f 72 20 61 20 63 61 63 68 65 2e 0a 2a t for a cache..*
10ddc 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 /.static void pc
10ddd 61 63 68 65 31 43 61 63 68 65 73 69 7a 65 28 73 ache1Cachesize(s
10dde 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 qlite3_pcache *p
10ddf 2c 20 69 6e 74 20 6e 4d 61 78 29 7b 0a 20 20 50 , int nMax){. P
10de0 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d Cache1 *pCache =
10de1 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 (PCache1 *)p;.
10de2 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 if( pCache->bPu
10de3 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 70 rgeable ){. p
10de4 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 cache1EnterMutex
10de5 28 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e ();. pcache1.
10de6 6e 4d 61 78 50 61 67 65 20 2b 3d 20 28 6e 4d 61 nMaxPage += (nMa
10de7 78 20 2d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 x - pCache->nMax
10de8 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e );. pCache->n
10de9 4d 61 78 20 3d 20 6e 4d 61 78 3b 0a 20 20 20 20 Max = nMax;.
10dea 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 pcache1EnforceMa
10deb 78 50 61 67 65 28 29 3b 0a 20 20 20 20 70 63 61 xPage();. pca
10dec 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 che1LeaveMutex()
10ded 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ;. }.}../*.** I
10dee 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
10def 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 the sqlite3_pca
10df0 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 20 6d che.xPagecount m
10df1 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 ethod. .*/.stati
10df2 63 20 69 6e 74 20 70 63 61 63 68 65 31 50 61 67 c int pcache1Pag
10df3 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 70 ecount(sqlite3_p
10df4 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 6e 74 cache *p){. int
10df5 20 6e 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 n;. pcache1Ent
10df6 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 6e 20 3d erMutex();. n =
10df7 20 28 28 50 43 61 63 68 65 31 20 2a 29 70 29 2d ((PCache1 *)p)-
10df8 3e 6e 50 61 67 65 3b 0a 20 20 70 63 61 63 68 65 >nPage;. pcache
10df9 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 1LeaveMutex();.
10dfa 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a return n;.}../*
10dfb 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 .** Implementati
10dfc 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 on of the sqlite
10dfd 33 5f 70 63 61 63 68 65 2e 78 46 65 74 63 68 20 3_pcache.xFetch
10dfe 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 46 method. .**.** F
10dff 65 74 63 68 20 61 20 70 61 67 65 20 62 79 20 6b etch a page by k
10e00 65 79 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 ey value..**.**
10e01 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 Whether or not a
10e02 20 6e 65 77 20 70 61 67 65 20 6d 61 79 20 62 65 new page may be
10e03 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 allocated by th
10e04 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 70 65 is function depe
10e05 6e 64 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 76 61 nds on.** the va
10e06 6c 75 65 20 6f 66 20 74 68 65 20 63 72 65 61 74 lue of the creat
10e07 65 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 2e 20 eFlag argument.
10e08 20 30 20 6d 65 61 6e 73 20 64 6f 20 6e 6f 74 20 0 means do not
10e09 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 0a 2a allocate a new.*
10e0a 2a 20 70 61 67 65 2e 20 20 31 20 6d 65 61 6e 73 * page. 1 means
10e0b 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 allocate a new
10e0c 70 61 67 65 20 69 66 20 73 70 61 63 65 20 69 73 page if space is
10e0d 20 65 61 73 69 6c 79 20 61 76 61 69 6c 61 62 6c easily availabl
10e0e 65 2e 20 20 32 20 0a 2a 2a 20 6d 65 61 6e 73 20 e. 2 .** means
10e0f 74 6f 20 74 72 79 20 72 65 61 6c 6c 79 20 68 61 to try really ha
10e10 72 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 rd to allocate a
10e11 20 6e 65 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a new page..**.**
10e12 20 46 6f 72 20 61 20 6e 6f 6e 2d 70 75 72 67 65 For a non-purge
10e13 61 62 6c 65 20 63 61 63 68 65 20 28 61 20 63 61 able cache (a ca
10e14 63 68 65 20 75 73 65 64 20 61 73 20 74 68 65 20 che used as the
10e15 73 74 6f 72 61 67 65 20 66 6f 72 20 61 6e 20 69 storage for an i
10e16 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 n-memory.** data
10e17 62 61 73 65 29 20 74 68 65 72 65 20 69 73 20 72 base) there is r
10e18 65 61 6c 6c 79 20 6e 6f 20 64 69 66 66 65 72 65 eally no differe
10e19 6e 63 65 20 62 65 74 77 65 65 6e 20 63 72 65 61 nce between crea
10e1a 74 65 46 6c 61 67 20 31 20 61 6e 64 20 32 2e 20 teFlag 1 and 2.
10e1b 20 53 6f 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 So.** the calli
10e1c 6e 67 20 66 75 6e 63 74 69 6f 6e 20 28 70 63 61 ng function (pca
10e1d 63 68 65 2e 63 29 20 77 69 6c 6c 20 6e 65 76 65 che.c) will neve
10e1e 72 20 68 61 76 65 20 61 20 63 72 65 61 74 65 46 r have a createF
10e1f 6c 61 67 20 6f 66 20 31 20 6f 6e 0a 2a 2a 20 61 lag of 1 on.** a
10e20 20 6e 6f 6e 2d 70 75 72 67 61 62 6c 65 20 63 61 non-purgable ca
10e21 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 che..**.** There
10e22 20 61 72 65 20 74 68 72 65 65 20 64 69 66 66 65 are three diffe
10e23 72 65 6e 74 20 61 70 70 72 6f 61 63 68 65 73 20 rent approaches
10e24 74 6f 20 6f 62 74 61 69 6e 69 6e 67 20 73 70 61 to obtaining spa
10e25 63 65 20 66 6f 72 20 61 20 70 61 67 65 2c 0a 2a ce for a page,.*
10e26 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 * depending on t
10e27 68 65 20 76 61 6c 75 65 20 6f 66 20 70 61 72 61 he value of para
10e28 6d 65 74 65 72 20 63 72 65 61 74 65 46 6c 61 67 meter createFlag
10e29 20 28 77 68 69 63 68 20 6d 61 79 20 62 65 20 30 (which may be 0
10e2a 2c 20 31 20 6f 72 20 32 29 2e 0a 2a 2a 0a 2a 2a , 1 or 2)..**.**
10e2b 20 20 20 31 2e 20 52 65 67 61 72 64 6c 65 73 73 1. Regardless
10e2c 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 of the value of
10e2d 20 63 72 65 61 74 65 46 6c 61 67 2c 20 74 68 65 createFlag, the
10e2e 20 63 61 63 68 65 20 69 73 20 73 65 61 72 63 68 cache is search
10e2f 65 64 20 66 6f 72 20 61 20 0a 2a 2a 20 20 20 20 ed for a .**
10e30 20 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 copy of the re
10e31 71 75 65 73 74 65 64 20 70 61 67 65 2e 20 49 66 quested page. If
10e32 20 6f 6e 65 20 69 73 20 66 6f 75 6e 64 2c 20 69 one is found, i
10e33 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a t is returned..*
10e34 2a 0a 2a 2a 20 20 20 32 2e 20 49 66 20 63 72 65 *.** 2. If cre
10e35 61 74 65 46 6c 61 67 3d 3d 30 20 61 6e 64 20 74 ateFlag==0 and t
10e36 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 he page is not a
10e37 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 lready in the ca
10e38 63 68 65 2c 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 che, NULL is.**
10e39 20 20 20 20 20 72 65 74 75 72 6e 65 64 2e 0a 2a returned..*
10e3a 2a 0a 2a 2a 20 20 20 33 2e 20 49 66 20 63 72 65 *.** 3. If cre
10e3b 61 74 65 46 6c 61 67 20 69 73 20 31 2c 20 61 6e ateFlag is 1, an
10e3c 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f d the page is no
10e3d 74 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 t already in the
10e3e 20 63 61 63 68 65 2c 0a 2a 2a 20 20 20 20 20 20 cache,.**
10e3f 61 6e 64 20 69 66 20 65 69 74 68 65 72 20 6f 66 and if either of
10e40 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 the following a
10e41 72 65 20 74 72 75 65 2c 20 72 65 74 75 72 6e 20 re true, return
10e42 4e 55 4c 4c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 NULL:.**.**
10e43 20 20 28 61 29 20 74 68 65 20 6e 75 6d 62 65 72 (a) the number
10e44 20 6f 66 20 70 61 67 65 73 20 70 69 6e 6e 65 64 of pages pinned
10e45 20 62 79 20 74 68 65 20 63 61 63 68 65 20 69 73 by the cache is
10e46 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a greater than.**
10e47 20 20 20 20 20 20 20 20 20 20 20 50 43 61 63 68 PCach
10e48 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 20 20 e1.nMax, or.**
10e49 20 20 20 20 20 28 62 29 20 74 68 65 20 6e 75 6d (b) the num
10e4a 62 65 72 20 6f 66 20 70 61 67 65 73 20 70 69 6e ber of pages pin
10e4b 6e 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65 ned by the cache
10e4c 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
10e4d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 68 .** th
10e4e 65 20 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f e sum of nMax fo
10e4f 72 20 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 20 r all purgeable
10e50 63 61 63 68 65 73 2c 20 6c 65 73 73 20 74 68 65 caches, less the
10e51 20 73 75 6d 20 6f 66 20 0a 2a 2a 20 20 20 20 20 sum of .**
10e52 20 20 20 20 20 20 6e 4d 69 6e 20 66 6f 72 20 61 nMin for a
10e53 6c 6c 20 6f 74 68 65 72 20 70 75 72 67 65 61 62 ll other purgeab
10e54 6c 65 20 63 61 63 68 65 73 2e 20 0a 2a 2a 0a 2a le caches. .**.*
10e55 2a 20 20 20 34 2e 20 49 66 20 6e 6f 6e 65 20 6f * 4. If none o
10e56 66 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65 f the first thre
10e57 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 70 70 e conditions app
10e58 6c 79 20 61 6e 64 20 74 68 65 20 63 61 63 68 65 ly and the cache
10e59 20 69 73 20 6d 61 72 6b 65 64 0a 2a 2a 20 20 20 is marked.**
10e5a 20 20 20 61 73 20 70 75 72 67 65 61 62 6c 65 2c as purgeable,
10e5b 20 61 6e 64 20 69 66 20 6f 6e 65 20 6f 66 20 74 and if one of t
10e5c 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 he following is
10e5d 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 true:.**.**
10e5e 20 20 28 61 29 20 54 68 65 20 6e 75 6d 62 65 72 (a) The number
10e5f 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 of pages alloca
10e60 74 65 64 20 66 6f 72 20 74 68 65 20 63 61 63 68 ted for the cach
10e61 65 20 69 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a e is already .**
10e62 20 20 20 20 20 20 20 20 20 20 20 50 43 61 63 68 PCach
10e63 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 0a 2a e1.nMax, or.**.*
10e64 2a 20 20 20 20 20 20 20 28 62 29 20 54 68 65 20 * (b) The
10e65 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
10e66 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 6c allocated for al
10e67 6c 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 l purgeable cach
10e68 65 73 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 es is.**
10e69 20 20 20 61 6c 72 65 61 64 79 20 65 71 75 61 6c already equal
10e6a 20 74 6f 20 6f 72 20 67 72 65 61 74 65 72 20 74 to or greater t
10e6b 68 61 6e 20 74 68 65 20 73 75 6d 20 6f 66 20 6e han the sum of n
10e6c 4d 61 78 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 20 Max for all.**
10e6d 20 20 20 20 20 20 20 20 20 70 75 72 67 65 61 62 purgeab
10e6e 6c 65 20 63 61 63 68 65 73 2c 0a 2a 2a 0a 2a 2a le caches,.**.**
10e6f 20 20 20 20 20 20 74 68 65 6e 20 61 74 74 65 6d then attem
10e70 70 74 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20 pt to recycle a
10e71 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 4c 52 page from the LR
10e72 55 20 6c 69 73 74 2e 20 49 66 20 69 74 20 69 73 U list. If it is
10e73 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 20 20 20 the right.**
10e74 20 20 20 73 69 7a 65 2c 20 72 65 74 75 72 6e 20 size, return
10e75 74 68 65 20 72 65 63 79 63 6c 65 64 20 62 75 66 the recycled buf
10e76 66 65 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 fer. Otherwise,
10e77 66 72 65 65 20 74 68 65 20 62 75 66 66 65 72 20 free the buffer
10e78 61 6e 64 0a 2a 2a 20 20 20 20 20 20 70 72 6f 63 and.** proc
10e79 65 65 64 20 74 6f 20 73 74 65 70 20 35 2e 20 0a eed to step 5. .
10e7a 2a 2a 0a 2a 2a 20 20 20 35 2e 20 4f 74 68 65 72 **.** 5. Other
10e7b 77 69 73 65 2c 20 61 6c 6c 6f 63 61 74 65 20 61 wise, allocate a
10e7c 6e 64 20 72 65 74 75 72 6e 20 61 20 6e 65 77 20 nd return a new
10e7d 70 61 67 65 20 62 75 66 66 65 72 2e 0a 2a 2f 0a page buffer..*/.
10e7e 73 74 61 74 69 63 20 76 6f 69 64 20 2a 70 63 61 static void *pca
10e7f 63 68 65 31 46 65 74 63 68 28 73 71 6c 69 74 65 che1Fetch(sqlite
10e80 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 75 6e 73 3_pcache *p, uns
10e81 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 79 2c 20 igned int iKey,
10e82 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 29 7b int createFlag){
10e83 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
10e84 6e 50 69 6e 6e 65 64 3b 0a 20 20 50 43 61 63 68 nPinned;. PCach
10e85 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 e1 *pCache = (PC
10e86 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 ache1 *)p;. PgH
10e87 64 72 31 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a dr1 *pPage = 0;.
10e88 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 . assert( pCach
10e89 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 7c 7c e->bPurgeable ||
10e8a 20 63 72 65 61 74 65 46 6c 61 67 21 3d 31 20 29 createFlag!=1 )
10e8b 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
10e8c 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 63 Mutex();. if( c
10e8d 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 reateFlag==1 ) s
10e8e 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
10e8f 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 2f 2a nMalloc();.. /*
10e90 20 53 65 61 72 63 68 20 74 68 65 20 68 61 73 68 Search the hash
10e91 20 74 61 62 6c 65 20 66 6f 72 20 61 6e 20 65 78 table for an ex
10e92 69 73 74 69 6e 67 20 65 6e 74 72 79 2e 20 2a 2f isting entry. */
10e93 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 6e . if( pCache->n
10e94 48 61 73 68 3e 30 20 29 7b 0a 20 20 20 20 75 6e Hash>0 ){. un
10e95 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 signed int h = i
10e96 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 Key % pCache->nH
10e97 61 73 68 3b 0a 20 20 20 20 66 6f 72 28 70 50 61 ash;. for(pPa
10e98 67 65 3d 70 43 61 63 68 65 2d 3e 61 70 48 61 73 ge=pCache->apHas
10e99 68 5b 68 5d 3b 20 70 50 61 67 65 26 26 70 50 61 h[h]; pPage&&pPa
10e9a 67 65 2d 3e 69 4b 65 79 21 3d 69 4b 65 79 3b 20 ge->iKey!=iKey;
10e9b 70 50 61 67 65 3d 70 50 61 67 65 2d 3e 70 4e 65 pPage=pPage->pNe
10e9c 78 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 xt);. }.. if(
10e9d 70 50 61 67 65 20 7c 7c 20 63 72 65 61 74 65 46 pPage || createF
10e9e 6c 61 67 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 lag==0 ){. pc
10e9f 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 ache1PinPage(pPa
10ea0 67 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 66 65 ge);. goto fe
10ea1 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 tch_out;. }..
10ea2 2f 2a 20 53 74 65 70 20 33 20 6f 66 20 68 65 61 /* Step 3 of hea
10ea3 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 20 2a 2f 0a der comment. */.
10ea4 20 20 6e 50 69 6e 6e 65 64 20 3d 20 70 43 61 63 nPinned = pCac
10ea5 68 65 2d 3e 6e 50 61 67 65 20 2d 20 70 43 61 63 he->nPage - pCac
10ea6 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 3b he->nRecyclable;
10ea7 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 . if( createFla
10ea8 67 3d 3d 31 20 26 26 20 28 0a 20 20 20 20 20 20 g==1 && (.
10ea9 20 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 63 61 63 nPinned>=(pcac
10eaa 68 65 31 2e 6e 4d 61 78 50 61 67 65 2b 70 43 61 he1.nMaxPage+pCa
10eab 63 68 65 2d 3e 6e 4d 69 6e 2d 70 63 61 63 68 65 che->nMin-pcache
10eac 31 2e 6e 4d 69 6e 50 61 67 65 29 0a 20 20 20 20 1.nMinPage).
10ead 20 7c 7c 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 43 || nPinned>=(pC
10eae 61 63 68 65 2d 3e 6e 4d 61 78 20 2a 20 39 20 2f ache->nMax * 9 /
10eaf 20 31 30 29 0a 20 20 29 29 7b 0a 20 20 20 20 67 10). )){. g
10eb0 6f 74 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 oto fetch_out;.
10eb1 20 7d 0a 0a 20 20 69 66 28 20 70 43 61 63 68 65 }.. if( pCache
10eb2 2d 3e 6e 50 61 67 65 3e 3d 70 43 61 63 68 65 2d ->nPage>=pCache-
10eb3 3e 6e 48 61 73 68 20 26 26 20 70 63 61 63 68 65 >nHash && pcache
10eb4 31 52 65 73 69 7a 65 48 61 73 68 28 70 43 61 63 1ResizeHash(pCac
10eb5 68 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 he) ){. goto
10eb6 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a fetch_out;. }..
10eb7 20 20 2f 2a 20 53 74 65 70 20 34 2e 20 54 72 79 /* Step 4. Try
10eb8 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61 to recycle a pa
10eb9 67 65 20 62 75 66 66 65 72 20 69 66 20 61 70 70 ge buffer if app
10eba 72 6f 70 72 69 61 74 65 2e 20 2a 2f 0a 20 20 69 ropriate. */. i
10ebb 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 f( pCache->bPurg
10ebc 65 61 62 6c 65 20 26 26 20 70 63 61 63 68 65 31 eable && pcache1
10ebd 2e 70 4c 72 75 54 61 69 6c 20 26 26 20 28 0a 20 .pLruTail && (.
10ebe 20 20 20 20 28 70 43 61 63 68 65 2d 3e 6e 50 61 (pCache->nPa
10ebf 67 65 2b 31 3e 3d 70 43 61 63 68 65 2d 3e 6e 4d ge+1>=pCache->nM
10ec0 61 78 29 20 7c 7c 20 70 63 61 63 68 65 31 2e 6e ax) || pcache1.n
10ec1 43 75 72 72 65 6e 74 50 61 67 65 3e 3d 70 63 61 CurrentPage>=pca
10ec2 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 0a 20 20 che1.nMaxPage.
10ec3 29 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 )){. pPage =
10ec4 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c pcache1.pLruTail
10ec5 3b 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6d ;. pcache1Rem
10ec6 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 50 61 67 oveFromHash(pPag
10ec7 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50 e);. pcache1P
10ec8 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 inPage(pPage);.
10ec9 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 43 if( pPage->pC
10eca 61 63 68 65 2d 3e 73 7a 50 61 67 65 21 3d 70 43 ache->szPage!=pC
10ecb 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 29 7b 0a ache->szPage ){.
10ecc 20 20 20 20 20 20 70 63 61 63 68 65 31 46 72 65 pcache1Fre
10ecd 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
10ece 20 20 20 20 70 50 61 67 65 20 3d 20 30 3b 0a 20 pPage = 0;.
10ecf 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
10ed0 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 pcache1.nCurrent
10ed1 50 61 67 65 20 2d 3d 20 28 70 50 61 67 65 2d 3e Page -= (pPage->
10ed2 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 pCache->bPurgeab
10ed3 6c 65 20 2d 20 70 43 61 63 68 65 2d 3e 62 50 75 le - pCache->bPu
10ed4 72 67 65 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a rgeable);. }.
10ed5 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35 }.. /* Step 5
10ed6 2e 20 49 66 20 61 20 75 73 61 62 6c 65 20 70 61 . If a usable pa
10ed7 67 65 20 62 75 66 66 65 72 20 68 61 73 20 73 74 ge buffer has st
10ed8 69 6c 6c 20 6e 6f 74 20 62 65 65 6e 20 66 6f 75 ill not been fou
10ed9 6e 64 2c 20 0a 20 20 2a 2a 20 61 74 74 65 6d 70 nd, . ** attemp
10eda 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 t to allocate a
10edb 6e 65 77 20 6f 6e 65 2e 20 0a 20 20 2a 2f 0a 20 new one. . */.
10edc 20 69 66 28 20 21 70 50 61 67 65 20 29 7b 0a 20 if( !pPage ){.
10edd 20 20 20 70 50 61 67 65 20 3d 20 70 63 61 63 68 pPage = pcach
10ede 65 31 41 6c 6c 6f 63 50 61 67 65 28 70 43 61 63 e1AllocPage(pCac
10edf 68 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 he);. }.. if(
10ee0 70 50 61 67 65 20 29 7b 0a 20 20 20 20 75 6e 73 pPage ){. uns
10ee1 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 4b igned int h = iK
10ee2 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 ey % pCache->nHa
10ee3 73 68 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e sh;. pCache->
10ee4 6e 50 61 67 65 2b 2b 3b 0a 20 20 20 20 70 50 61 nPage++;. pPa
10ee5 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b ge->iKey = iKey;
10ee6 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 . pPage->pNex
10ee7 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61 t = pCache->apHa
10ee8 73 68 5b 68 5d 3b 0a 20 20 20 20 70 50 61 67 65 sh[h];. pPage
10ee9 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 68 ->pCache = pCach
10eea 65 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c e;. pPage->pL
10eeb 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 ruPrev = 0;.
10eec 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 pPage->pLruNext
10eed 3d 20 30 3b 0a 20 20 20 20 2a 28 76 6f 69 64 20 = 0;. *(void
10eee 2a 2a 29 28 50 47 48 44 52 31 5f 54 4f 5f 50 41 **)(PGHDR1_TO_PA
10eef 47 45 28 70 50 61 67 65 29 29 20 3d 20 30 3b 0a GE(pPage)) = 0;.
10ef0 20 20 20 20 70 43 61 63 68 65 2d 3e 61 70 48 61 pCache->apHa
10ef1 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20 sh[h] = pPage;.
10ef2 20 7d 0a 0a 66 65 74 63 68 5f 6f 75 74 3a 0a 20 }..fetch_out:.
10ef3 20 69 66 28 20 70 50 61 67 65 20 26 26 20 69 4b if( pPage && iK
10ef4 65 79 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b ey>pCache->iMaxK
10ef5 65 79 20 29 7b 0a 20 20 20 20 70 43 61 63 68 65 ey ){. pCache
10ef6 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4b 65 79 ->iMaxKey = iKey
10ef7 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 72 65 61 ;. }. if( crea
10ef8 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c 69 teFlag==1 ) sqli
10ef9 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
10efa 6f 63 28 29 3b 0a 20 20 70 63 61 63 68 65 31 4c oc();. pcache1L
10efb 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 eaveMutex();. r
10efc 65 74 75 72 6e 20 28 70 50 61 67 65 20 3f 20 50 eturn (pPage ? P
10efd 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 50 GHDR1_TO_PAGE(pP
10efe 61 67 65 29 20 3a 20 30 29 3b 0a 7d 0a 0a 0a 2f age) : 0);.}.../
10eff 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
10f00 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ion of the sqlit
10f01 65 33 5f 70 63 61 63 68 65 2e 78 55 6e 70 69 6e e3_pcache.xUnpin
10f02 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 4d method..**.** M
10f03 61 72 6b 20 61 20 70 61 67 65 20 61 73 20 75 6e ark a page as un
10f04 70 69 6e 6e 65 64 20 28 65 6c 69 67 69 62 6c 65 pinned (eligible
10f05 20 66 6f 72 20 61 73 79 6e 63 68 72 6f 6e 6f 75 for asynchronou
10f06 73 20 72 65 63 79 63 6c 69 6e 67 29 2e 0a 2a 2f s recycling)..*/
10f07 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 .static void pca
10f08 63 68 65 31 55 6e 70 69 6e 28 73 71 6c 69 74 65 che1Unpin(sqlite
10f09 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 76 6f 69 3_pcache *p, voi
10f0a 64 20 2a 70 50 67 2c 20 69 6e 74 20 72 65 75 73 d *pPg, int reus
10f0b 65 55 6e 6c 69 6b 65 6c 79 29 7b 0a 20 20 50 43 eUnlikely){. PC
10f0c 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 ache1 *pCache =
10f0d 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 (PCache1 *)p;.
10f0e 50 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 PgHdr1 *pPage =
10f0f 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 PAGE_TO_PGHDR1(p
10f10 43 61 63 68 65 2c 20 70 50 67 29 3b 0a 20 0a 20 Cache, pPg);. .
10f11 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
10f12 70 43 61 63 68 65 3d 3d 70 43 61 63 68 65 20 29 pCache==pCache )
10f13 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
10f14 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 49 Mutex();.. /* I
10f15 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 74 6f t is an error to
10f16 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 call this funct
10f17 69 6f 6e 20 69 66 20 74 68 65 20 70 61 67 65 20 ion if the page
10f18 69 73 20 61 6c 72 65 61 64 79 20 0a 20 20 2a 2a is already . **
10f19 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f part of the glo
10f1a 62 61 6c 20 4c 52 55 20 6c 69 73 74 2e 0a 20 20 bal LRU list..
10f1b 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 */. assert( pPa
10f1c 67 65 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 30 20 ge->pLruPrev==0
10f1d 26 26 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 && pPage->pLruNe
10f1e 78 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 xt==0 );. asser
10f1f 74 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 t( pcache1.pLruH
10f20 65 61 64 21 3d 70 50 61 67 65 20 26 26 20 70 63 ead!=pPage && pc
10f21 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 21 3d ache1.pLruTail!=
10f22 70 50 61 67 65 20 29 3b 0a 0a 20 20 69 66 28 20 pPage );.. if(
10f23 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 20 7c 7c reuseUnlikely ||
10f24 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e pcache1.nCurren
10f25 74 50 61 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d tPage>pcache1.nM
10f26 61 78 50 61 67 65 20 29 7b 0a 20 20 20 20 70 63 axPage ){. pc
10f27 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 ache1RemoveFromH
10f28 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20 ash(pPage);.
10f29 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 pcache1FreePage(
10f2a 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b pPage);. }else{
10f2b 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 . /* Add the
10f2c 70 61 67 65 20 74 6f 20 74 68 65 20 67 6c 6f 62 page to the glob
10f2d 61 6c 20 4c 52 55 20 6c 69 73 74 2e 20 4e 6f 72 al LRU list. Nor
10f2e 6d 61 6c 6c 79 2c 20 74 68 65 20 70 61 67 65 20 mally, the page
10f2f 69 73 20 61 64 64 65 64 20 74 6f 0a 20 20 20 20 is added to.
10f30 2a 2a 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 ** the head of t
10f31 68 65 20 6c 69 73 74 20 28 6c 61 73 74 20 70 61 he list (last pa
10f32 67 65 20 74 6f 20 62 65 20 72 65 63 79 63 6c 65 ge to be recycle
10f33 64 29 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 d). However, if
10f34 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 65 75 73 the . ** reus
10f35 65 55 6e 6c 69 6b 65 6c 79 20 66 6c 61 67 20 70 eUnlikely flag p
10f36 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 assed to this fu
10f37 6e 63 74 69 6f 6e 20 69 73 20 74 72 75 65 2c 20 nction is true,
10f38 74 68 65 20 70 61 67 65 20 69 73 20 61 64 64 65 the page is adde
10f39 64 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 d. ** to the
10f3a 74 61 69 6c 20 6f 66 20 74 68 65 20 6c 69 73 74 tail of the list
10f3b 20 28 66 69 72 73 74 20 70 61 67 65 20 74 6f 20 (first page to
10f3c 62 65 20 72 65 63 79 63 6c 65 64 29 2e 0a 20 20 be recycled)..
10f3d 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 61 */. if( pca
10f3e 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 29 7b che1.pLruHead ){
10f3f 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 . pcache1.p
10f40 4c 72 75 48 65 61 64 2d 3e 70 4c 72 75 50 72 65 LruHead->pLruPre
10f41 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 v = pPage;.
10f42 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 pPage->pLruNext
10f43 20 3d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 = pcache1.pLruH
10f44 65 61 64 3b 0a 20 20 20 20 20 20 70 63 61 63 68 ead;. pcach
10f45 65 31 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50 e1.pLruHead = pP
10f46 61 67 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a age;. }else{.
10f47 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c pcache1.pL
10f48 72 75 54 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a ruTail = pPage;.
10f49 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c pcache1.pL
10f4a 72 75 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a ruHead = pPage;.
10f4b 20 20 20 20 7d 0a 20 20 20 20 70 43 61 63 68 65 }. pCache
10f4c 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b ->nRecyclable++;
10f4d 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c . }.. pcache1L
10f4e 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a eaveMutex();.}..
10f4f 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 /*.** Implementa
10f50 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 tion of the sqli
10f51 74 65 33 5f 70 63 61 63 68 65 2e 78 52 65 6b 65 te3_pcache.xReke
10f52 79 20 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 y method. .*/.st
10f53 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
10f54 31 52 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 1Rekey(. sqlite
10f55 33 5f 70 63 61 63 68 65 20 2a 70 2c 0a 20 20 76 3_pcache *p,. v
10f56 6f 69 64 20 2a 70 50 67 2c 0a 20 20 75 6e 73 69 oid *pPg,. unsi
10f57 67 6e 65 64 20 69 6e 74 20 69 4f 6c 64 2c 0a 20 gned int iOld,.
10f58 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4e unsigned int iN
10f59 65 77 0a 29 7b 0a 20 20 50 43 61 63 68 65 31 20 ew.){. PCache1
10f5a 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 *pCache = (PCach
10f5b 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 e1 *)p;. PgHdr1
10f5c 20 2a 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54 *pPage = PAGE_T
10f5d 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c O_PGHDR1(pCache,
10f5e 20 70 50 67 29 3b 0a 20 20 50 67 48 64 72 31 20 pPg);. PgHdr1
10f5f 2a 2a 70 70 3b 0a 20 20 75 6e 73 69 67 6e 65 64 **pp;. unsigned
10f60 20 69 6e 74 20 68 3b 20 0a 20 20 61 73 73 65 72 int h; . asser
10f61 74 28 20 70 50 61 67 65 2d 3e 69 4b 65 79 3d 3d t( pPage->iKey==
10f62 69 4f 6c 64 20 29 3b 0a 20 20 61 73 73 65 72 74 iOld );. assert
10f63 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d ( pPage->pCache=
10f64 3d 70 43 61 63 68 65 20 29 3b 0a 0a 20 20 70 63 =pCache );.. pc
10f65 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 ache1EnterMutex(
10f66 29 3b 0a 0a 20 20 68 20 3d 20 69 4f 6c 64 25 70 );.. h = iOld%p
10f67 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 Cache->nHash;.
10f68 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 pp = &pCache->ap
10f69 48 61 73 68 5b 68 5d 3b 0a 20 20 77 68 69 6c 65 Hash[h];. while
10f6a 28 20 28 2a 70 70 29 21 3d 70 50 61 67 65 20 29 ( (*pp)!=pPage )
10f6b 7b 0a 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70 {. pp = &(*pp
10f6c 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 )->pNext;. }.
10f6d 2a 70 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 *pp = pPage->pNe
10f6e 78 74 3b 0a 0a 20 20 68 20 3d 20 69 4e 65 77 25 xt;.. h = iNew%
10f6f 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 pCache->nHash;.
10f70 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69 pPage->iKey = i
10f71 4e 65 77 3b 0a 20 20 70 50 61 67 65 2d 3e 70 4e New;. pPage->pN
10f72 65 78 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 ext = pCache->ap
10f73 48 61 73 68 5b 68 5d 3b 0a 20 20 70 43 61 63 68 Hash[h];. pCach
10f74 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20 3d 20 70 e->apHash[h] = p
10f75 50 61 67 65 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 Page;.. /* The
10f76 78 52 65 6b 65 79 28 29 20 69 6e 74 65 72 66 61 xRekey() interfa
10f77 63 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 ce is only used
10f78 74 6f 20 6d 6f 76 65 20 70 61 67 65 73 20 65 61 to move pages ea
10f79 72 6c 69 65 72 20 69 6e 20 74 68 65 0a 20 20 2a rlier in the. *
10f7a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 * database file
10f7b 28 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 6f 76 (in order to mov
10f7c 65 20 61 6c 6c 20 66 72 65 65 20 70 61 67 65 73 e all free pages
10f7d 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 to the end of t
10f7e 68 65 0a 20 20 2a 2a 20 66 69 6c 65 20 77 68 65 he. ** file whe
10f7f 72 65 20 74 68 65 79 20 63 61 6e 20 62 65 20 74 re they can be t
10f80 72 75 6e 63 61 74 65 64 20 6f 66 66 2e 29 20 20 runcated off.)
10f81 48 65 6e 63 65 2c 20 69 74 20 69 73 20 6e 6f 74 Hence, it is not
10f82 20 70 6f 73 73 69 62 6c 65 0a 20 20 2a 2a 20 66 possible. ** f
10f83 6f 72 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 or the new page
10f84 6e 75 6d 62 65 72 20 74 6f 20 62 65 20 67 72 65 number to be gre
10f85 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 6c 61 ater than the la
10f86 72 67 65 73 74 20 70 72 65 76 69 6f 75 73 6c 79 rgest previously
10f87 0a 20 20 2a 2a 20 66 65 74 63 68 65 64 20 70 61 . ** fetched pa
10f88 67 65 2e 20 20 42 75 74 20 77 65 20 72 65 74 61 ge. But we reta
10f89 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 in the following
10f8a 20 74 65 73 74 20 69 6e 20 63 61 73 65 20 78 52 test in case xR
10f8b 65 6b 65 79 28 29 0a 20 20 2a 2a 20 62 65 67 69 ekey(). ** begi
10f8c 6e 73 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e ns to be used in
10f8d 20 64 69 66 66 65 72 65 6e 74 20 77 61 79 73 20 different ways
10f8e 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e 0a 20 in the future..
10f8f 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 */. if( NEVER(
10f90 69 4e 65 77 3e 70 43 61 63 68 65 2d 3e 69 4d 61 iNew>pCache->iMa
10f91 78 4b 65 79 29 20 29 7b 0a 20 20 20 20 70 43 61 xKey) ){. pCa
10f92 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 che->iMaxKey = i
10f93 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 63 New;. }.. pcac
10f94 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b he1LeaveMutex();
10f95 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d .}../*.** Implem
10f96 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
10f97 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 sqlite3_pcache.x
10f98 54 72 75 6e 63 61 74 65 20 6d 65 74 68 6f 64 2e Truncate method.
10f99 20 0a 2a 2a 0a 2a 2a 20 44 69 73 63 61 72 64 20 .**.** Discard
10f9a 61 6c 6c 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 all unpinned pag
10f9b 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 es in the cache
10f9c 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62 with a page numb
10f9d 65 72 20 65 71 75 61 6c 20 74 6f 0a 2a 2a 20 6f er equal to.** o
10f9e 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 r greater than p
10f9f 61 72 61 6d 65 74 65 72 20 69 4c 69 6d 69 74 2e arameter iLimit.
10fa0 20 41 6e 79 20 70 69 6e 6e 65 64 20 70 61 67 65 Any pinned page
10fa1 73 20 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 s with a page nu
10fa2 6d 62 65 72 0a 2a 2a 20 65 71 75 61 6c 20 74 6f mber.** equal to
10fa3 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e or greater than
10fa4 20 69 4c 69 6d 69 74 20 61 72 65 20 69 6d 70 6c iLimit are impl
10fa5 69 63 69 74 6c 79 20 75 6e 70 69 6e 6e 65 64 2e icitly unpinned.
10fa6 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
10fa7 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 28 pcache1Truncate(
10fa8 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a sqlite3_pcache *
10fa9 70 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 p, unsigned int
10faa 69 4c 69 6d 69 74 29 7b 0a 20 20 50 43 61 63 68 iLimit){. PCach
10fab 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 e1 *pCache = (PC
10fac 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 70 63 61 ache1 *)p;. pca
10fad 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 che1EnterMutex()
10fae 3b 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3c 3d ;. if( iLimit<=
10faf 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 pCache->iMaxKey
10fb0 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 54 72 ){. pcache1Tr
10fb1 75 6e 63 61 74 65 55 6e 73 61 66 65 28 70 43 61 uncateUnsafe(pCa
10fb2 63 68 65 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 che, iLimit);.
10fb3 20 20 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 pCache->iMaxKe
10fb4 79 20 3d 20 69 4c 69 6d 69 74 2d 31 3b 0a 20 20 y = iLimit-1;.
10fb5 7d 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 }. pcache1Leave
10fb6 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a Mutex();.}../*.*
10fb7 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
10fb8 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
10fb9 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79 20 pcache.xDestroy
10fba 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 method. .**.** D
10fbb 65 73 74 72 6f 79 20 61 20 63 61 63 68 65 20 61 estroy a cache a
10fbc 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 70 llocated using p
10fbd 63 61 63 68 65 31 43 72 65 61 74 65 28 29 2e 0a cache1Create()..
10fbe 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
10fbf 63 61 63 68 65 31 44 65 73 74 72 6f 79 28 73 71 cache1Destroy(sq
10fc0 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 29 lite3_pcache *p)
10fc1 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 {. PCache1 *pCa
10fc2 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a che = (PCache1 *
10fc3 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 )p;. pcache1Ent
10fc4 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 erMutex();. pca
10fc5 63 68 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 che1TruncateUnsa
10fc6 66 65 28 70 43 61 63 68 65 2c 20 30 29 3b 0a 20 fe(pCache, 0);.
10fc7 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 pcache1.nMaxPag
10fc8 65 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 e -= pCache->nMa
10fc9 78 3b 0a 20 20 70 63 61 63 68 65 31 2e 6e 4d 69 x;. pcache1.nMi
10fca 6e 50 61 67 65 20 2d 3d 20 70 43 61 63 68 65 2d nPage -= pCache-
10fcb 3e 6e 4d 69 6e 3b 0a 20 20 70 63 61 63 68 65 31 >nMin;. pcache1
10fcc 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 28 29 EnforceMaxPage()
10fcd 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 ;. pcache1Leave
10fce 4d 75 74 65 78 28 29 3b 0a 20 20 73 71 6c 69 74 Mutex();. sqlit
10fcf 65 33 5f 66 72 65 65 28 70 43 61 63 68 65 2d 3e e3_free(pCache->
10fd0 61 70 48 61 73 68 29 3b 0a 20 20 73 71 6c 69 74 apHash);. sqlit
10fd1 65 33 5f 66 72 65 65 28 70 43 61 63 68 65 29 3b e3_free(pCache);
10fd2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
10fd3 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
10fd4 64 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c d during initial
10fd5 69 7a 61 74 69 6f 6e 20 28 73 71 6c 69 74 65 33 ization (sqlite3
10fd6 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 29 20 74 _initialize()) t
10fd7 6f 0a 2a 2a 20 69 6e 73 74 61 6c 6c 20 74 68 65 o.** install the
10fd8 20 64 65 66 61 75 6c 74 20 70 6c 75 67 67 61 62 default pluggab
10fd9 6c 65 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 2c le cache module,
10fda 20 61 73 73 75 6d 69 6e 67 20 74 68 65 20 75 73 assuming the us
10fdb 65 72 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 61 6c er has not.** al
10fdc 72 65 61 64 79 20 70 72 6f 76 69 64 65 64 20 61 ready provided a
10fdd 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 2e 0a 2a n alternative..*
10fde 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
10fdf 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 void sqlite3PCa
10fe0 63 68 65 53 65 74 44 65 66 61 75 6c 74 28 76 6f cheSetDefault(vo
10fe1 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 id){. static sq
10fe2 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 lite3_pcache_met
10fe3 68 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 hods defaultMeth
10fe4 6f 64 73 20 3d 20 7b 0a 20 20 20 20 30 2c 20 20 ods = {. 0,
10fe5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10fe6 20 20 20 20 20 2f 2a 20 70 41 72 67 20 2a 2f 0a /* pArg */.
10fe7 20 20 20 20 70 63 61 63 68 65 31 49 6e 69 74 2c pcache1Init,
10fe8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10fe9 78 49 6e 69 74 20 2a 2f 0a 20 20 20 20 70 63 61 xInit */. pca
10fea 63 68 65 31 53 68 75 74 64 6f 77 6e 2c 20 20 20 che1Shutdown,
10feb 20 20 20 20 20 20 2f 2a 20 78 53 68 75 74 64 6f /* xShutdo
10fec 77 6e 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 wn */. pcache
10fed 31 43 72 65 61 74 65 2c 20 20 20 20 20 20 20 20 1Create,
10fee 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20 2a 2f /* xCreate */
10fef 0a 20 20 20 20 70 63 61 63 68 65 31 43 61 63 68 . pcache1Cach
10ff0 65 73 69 7a 65 2c 20 20 20 20 20 20 20 20 2f 2a esize, /*
10ff1 20 78 43 61 63 68 65 73 69 7a 65 20 2a 2f 0a 20 xCachesize */.
10ff2 20 20 20 70 63 61 63 68 65 31 50 61 67 65 63 6f pcache1Pageco
10ff3 75 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 unt, /* x
10ff4 50 61 67 65 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 Pagecount */.
10ff5 20 70 63 61 63 68 65 31 46 65 74 63 68 2c 20 20 pcache1Fetch,
10ff6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 65 /* xFe
10ff7 74 63 68 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 tch */. pcach
10ff8 65 31 55 6e 70 69 6e 2c 20 20 20 20 20 20 20 20 e1Unpin,
10ff9 20 20 20 20 2f 2a 20 78 55 6e 70 69 6e 20 2a 2f /* xUnpin */
10ffa 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6b 65 . pcache1Reke
10ffb 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a y, /*
10ffc 20 78 52 65 6b 65 79 20 2a 2f 0a 20 20 20 20 70 xRekey */. p
10ffd 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 2c 20 cache1Truncate,
10ffe 20 20 20 20 20 20 20 20 2f 2a 20 78 54 72 75 6e /* xTrun
10fff 63 61 74 65 20 2a 2f 0a 20 20 20 20 70 63 61 63 cate */. pcac
11000 68 65 31 44 65 73 74 72 6f 79 20 20 20 20 20 20 he1Destroy
11001 20 20 20 20 20 2f 2a 20 78 44 65 73 74 72 6f 79 /* xDestroy
11002 20 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 */. };. sqlit
11003 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 e3_config(SQLITE
11004 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 2c 20 _CONFIG_PCACHE,
11005 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 &defaultMethods)
11006 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ;.}..#ifdef SQLI
11007 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 TE_ENABLE_MEMORY
11008 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a 2a _MANAGEMENT./*.*
11009 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
1100a 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 66 72 65 is called to fre
1100b 65 20 73 75 70 65 72 66 6c 75 6f 75 73 20 64 79 e superfluous dy
1100c 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 namically alloca
1100d 74 65 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 65 ted memory.** he
1100e 6c 64 20 62 79 20 74 68 65 20 70 61 67 65 72 20 ld by the pager
1100f 73 79 73 74 65 6d 2e 20 4d 65 6d 6f 72 79 20 69 system. Memory i
11010 6e 20 75 73 65 20 62 79 20 61 6e 79 20 53 51 4c n use by any SQL
11011 69 74 65 20 70 61 67 65 72 20 61 6c 6c 6f 63 61 ite pager alloca
11012 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 75 ted.** by the cu
11013 72 72 65 6e 74 20 74 68 72 65 61 64 20 6d 61 79 rrent thread may
11014 20 62 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 be sqlite3_free
11015 28 29 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52 65 71 ()ed..**.** nReq
11016 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
11017 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 f bytes of memor
11018 79 20 72 65 71 75 69 72 65 64 2e 20 4f 6e 63 65 y required. Once
11019 20 74 68 69 73 20 6d 75 63 68 20 68 61 73 0a 2a this much has.*
1101a 2a 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2c * been released,
1101b 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 the function re
1101c 74 75 72 6e 73 2e 20 54 68 65 20 72 65 74 75 72 turns. The retur
1101d 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 74 n value is the t
1101e 6f 74 61 6c 20 6e 75 6d 62 65 72 20 0a 2a 2a 20 otal number .**
1101f 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f of bytes of memo
11020 72 79 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2f 0a ry released..*/.
11021 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
11022 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nt sqlite3Pcache
11023 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e ReleaseMemory(in
11024 74 20 6e 52 65 71 29 7b 0a 20 20 69 6e 74 20 6e t nReq){. int n
11025 46 72 65 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 Free = 0;. if(
11026 70 63 61 63 68 65 31 2e 70 53 74 61 72 74 3d 3d pcache1.pStart==
11027 30 20 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20 0 ){. PgHdr1
11028 2a 70 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45 *p;. pcache1E
11029 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 nterMutex();.
1102a 20 77 68 69 6c 65 28 20 28 6e 52 65 71 3c 30 20 while( (nReq<0
1102b 7c 7c 20 6e 46 72 65 65 3c 6e 52 65 71 29 20 26 || nFree<nReq) &
1102c 26 20 28 70 3d 70 63 61 63 68 65 31 2e 70 4c 72 & (p=pcache1.pLr
1102d 75 54 61 69 6c 29 20 29 7b 0a 20 20 20 20 20 20 uTail) ){.
1102e 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 33 nFree += sqlite3
1102f 4d 61 6c 6c 6f 63 53 69 7a 65 28 50 47 48 44 52 MallocSize(PGHDR
11030 31 5f 54 4f 5f 50 41 47 45 28 70 29 29 3b 0a 20 1_TO_PAGE(p));.
11031 20 20 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 pcache1PinP
11032 61 67 65 28 70 29 3b 0a 20 20 20 20 20 20 70 63 age(p);. pc
11033 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 ache1RemoveFromH
11034 61 73 68 28 70 29 3b 0a 20 20 20 20 20 20 70 63 ash(p);. pc
11035 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70 29 ache1FreePage(p)
11036 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 ;. }. pcac
11037 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b he1LeaveMutex();
11038 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 46 . }. return nF
11039 72 65 65 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a ree;.}.#endif /*
1103a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d SQLITE_ENABLE_M
1103b 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 EMORY_MANAGEMENT
1103c 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 */..#ifdef SQLI
1103d 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 TE_TEST./*.** Th
1103e 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 is function is u
1103f 73 65 64 20 62 79 20 74 65 73 74 20 70 72 6f 63 sed by test proc
11040 65 64 75 72 65 73 20 74 6f 20 69 6e 73 70 65 63 edures to inspec
11041 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 t the internal s
11042 74 61 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 67 tate.** of the g
11043 6c 6f 62 61 6c 20 63 61 63 68 65 2e 0a 2a 2f 0a lobal cache..*/.
11044 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
11045 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
11046 65 53 74 61 74 73 28 0a 20 20 69 6e 74 20 2a 70 eStats(. int *p
11047 6e 43 75 72 72 65 6e 74 2c 20 20 20 20 20 20 2f nCurrent, /
11048 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d * OUT: Total num
11049 62 65 72 20 6f 66 20 70 61 67 65 73 20 63 61 63 ber of pages cac
1104a 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e hed */. int *pn
1104b 4d 61 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a Max, /*
1104c 20 4f 55 54 3a 20 47 6c 6f 62 61 6c 20 6d 61 78 OUT: Global max
1104d 69 6d 75 6d 20 63 61 63 68 65 20 73 69 7a 65 20 imum cache size
1104e 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 69 6e 2c */. int *pnMin,
1104f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
11050 3a 20 53 75 6d 20 6f 66 20 50 43 61 63 68 65 31 : Sum of PCache1
11051 2e 6e 4d 69 6e 20 66 6f 72 20 70 75 72 67 65 61 .nMin for purgea
11052 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 ble caches */.
11053 69 6e 74 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c int *pnRecyclabl
11054 65 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 74 e /* OUT: Tot
11055 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 al number of pag
11056 65 73 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 es available for
11057 20 72 65 63 79 63 6c 69 6e 67 20 2a 2f 0a 29 7b recycling */.){
11058 0a 20 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 . PgHdr1 *p;.
11059 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 20 int nRecyclable
1105a 3d 20 30 3b 0a 20 20 66 6f 72 28 70 3d 70 63 61 = 0;. for(p=pca
1105b 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3b 20 70 che1.pLruHead; p
1105c 3b 20 70 3d 70 2d 3e 70 4c 72 75 4e 65 78 74 29 ; p=p->pLruNext)
1105d 7b 0a 20 20 20 20 6e 52 65 63 79 63 6c 61 62 6c {. nRecyclabl
1105e 65 2b 2b 3b 0a 20 20 7d 0a 20 20 2a 70 6e 43 75 e++;. }. *pnCu
1105f 72 72 65 6e 74 20 3d 20 70 63 61 63 68 65 31 2e rrent = pcache1.
11060 6e 43 75 72 72 65 6e 74 50 61 67 65 3b 0a 20 20 nCurrentPage;.
11061 2a 70 6e 4d 61 78 20 3d 20 70 63 61 63 68 65 31 *pnMax = pcache1
11062 2e 6e 4d 61 78 50 61 67 65 3b 0a 20 20 2a 70 6e .nMaxPage;. *pn
11063 4d 69 6e 20 3d 20 70 63 61 63 68 65 31 2e 6e 4d Min = pcache1.nM
11064 69 6e 50 61 67 65 3b 0a 20 20 2a 70 6e 52 65 63 inPage;. *pnRec
11065 79 63 6c 61 62 6c 65 20 3d 20 6e 52 65 63 79 63 yclable = nRecyc
11066 6c 61 62 6c 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a lable;.}.#endif.
11067 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
11068 20 45 6e 64 20 6f 66 20 70 63 61 63 68 65 31 2e End of pcache1.
11069 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1106a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1106b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1106c 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1106d 20 42 65 67 69 6e 20 66 69 6c 65 20 72 6f 77 73 Begin file rows
1106e 65 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a et.c ***********
1106f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
11071 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 44 65 63 65 ./*.** 2008 Dece
11072 6d 62 65 72 20 33 0a 2a 2a 0a 2a 2a 20 54 68 65 mber 3.**.** The
11073 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
11074 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
11075 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
11076 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
11077 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
11078 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
11079 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
1107a 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
1107b 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
1107c 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
1107d 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
1107e 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
1107f 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
11080 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
11081 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
11082 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
11083 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
11084 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11085 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11086 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11087 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11088 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
11089 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e module implemen
1108a 74 73 20 61 6e 20 6f 62 6a 65 63 74 20 77 65 20 ts an object we
1108b 63 61 6c 6c 20 61 20 22 52 6f 77 53 65 74 22 2e call a "RowSet".
1108c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 52 6f 77 53 65 .**.** The RowSe
1108d 74 20 6f 62 6a 65 63 74 20 69 73 20 61 20 63 6f t object is a co
1108e 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 72 6f 77 69 llection of rowi
1108f 64 73 2e 20 20 52 6f 77 69 64 73 0a 2a 2a 20 61 ds. Rowids.** a
11090 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f re inserted into
11091 20 74 68 65 20 52 6f 77 53 65 74 20 69 6e 20 61 the RowSet in a
11092 6e 20 61 72 62 69 74 72 61 72 79 20 6f 72 64 65 n arbitrary orde
11093 72 2e 20 20 49 6e 73 65 72 74 73 0a 2a 2a 20 63 r. Inserts.** c
11094 61 6e 20 62 65 20 69 6e 74 65 72 6d 69 78 65 64 an be intermixed
11095 20 77 69 74 68 20 74 65 73 74 73 20 74 6f 20 73 with tests to s
11096 65 65 20 69 66 20 61 20 67 69 76 65 6e 20 72 6f ee if a given ro
11097 77 69 64 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 wid has been.**
11098 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 65 72 previously inser
11099 74 65 64 20 69 6e 74 6f 20 74 68 65 20 52 6f 77 ted into the Row
1109a 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 Set..**.** After
1109b 20 61 6c 6c 20 69 6e 73 65 72 74 73 20 61 72 65 all inserts are
1109c 20 66 69 6e 69 73 68 65 64 2c 20 69 74 20 69 73 finished, it is
1109d 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 65 78 74 possible to ext
1109e 72 61 63 74 20 74 68 65 0a 2a 2a 20 65 6c 65 6d ract the.** elem
1109f 65 6e 74 73 20 6f 66 20 74 68 65 20 52 6f 77 53 ents of the RowS
110a0 65 74 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 et in sorted ord
110a1 65 72 2e 20 20 4f 6e 63 65 20 74 68 69 73 20 65 er. Once this e
110a2 78 74 72 61 63 74 69 6f 6e 0a 2a 2a 20 70 72 6f xtraction.** pro
110a3 63 65 73 73 20 68 61 73 20 73 74 61 72 74 65 64 cess has started
110a4 2c 20 6e 6f 20 6e 65 77 20 65 6c 65 6d 65 6e 74 , no new element
110a5 73 20 6d 61 79 20 62 65 20 69 6e 73 65 72 74 65 s may be inserte
110a6 64 2e 0a 2a 2a 0a 2a 2a 20 48 65 6e 63 65 2c 20 d..**.** Hence,
110a7 74 68 65 20 70 72 69 6d 69 74 69 76 65 20 6f 70 the primitive op
110a8 65 72 61 74 69 6f 6e 73 20 66 6f 72 20 61 20 52 erations for a R
110a9 6f 77 53 65 74 20 61 72 65 3a 0a 2a 2a 0a 2a 2a owSet are:.**.**
110aa 20 20 20 20 43 52 45 41 54 45 0a 2a 2a 20 20 20 CREATE.**
110ab 20 49 4e 53 45 52 54 0a 2a 2a 20 20 20 20 54 45 INSERT.** TE
110ac 53 54 0a 2a 2a 20 20 20 20 53 4d 41 4c 4c 45 53 ST.** SMALLES
110ad 54 0a 2a 2a 20 20 20 20 44 45 53 54 52 4f 59 0a T.** DESTROY.
110ae 2a 2a 0a 2a 2a 20 54 68 65 20 43 52 45 41 54 45 **.** The CREATE
110af 20 61 6e 64 20 44 45 53 54 52 4f 59 20 70 72 69 and DESTROY pri
110b0 6d 69 74 69 76 65 73 20 61 72 65 20 74 68 65 20 mitives are the
110b1 63 6f 6e 73 74 72 75 63 74 6f 72 20 61 6e 64 20 constructor and
110b2 64 65 73 74 72 75 63 74 6f 72 2c 0a 2a 2a 20 6f destructor,.** o
110b3 62 76 69 6f 75 73 6c 79 2e 20 20 54 68 65 20 49 bviously. The I
110b4 4e 53 45 52 54 20 70 72 69 6d 69 74 69 76 65 20 NSERT primitive
110b5 61 64 64 73 20 61 20 6e 65 77 20 65 6c 65 6d 65 adds a new eleme
110b6 6e 74 20 74 6f 20 74 68 65 20 52 6f 77 53 65 74 nt to the RowSet
110b7 2e 0a 2a 2a 20 54 45 53 54 20 63 68 65 63 6b 73 ..** TEST checks
110b8 20 74 6f 20 73 65 65 20 69 66 20 61 6e 20 65 6c to see if an el
110b9 65 6d 65 6e 74 20 69 73 20 61 6c 72 65 61 64 79 ement is already
110ba 20 69 6e 20 74 68 65 20 52 6f 77 53 65 74 2e 20 in the RowSet.
110bb 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 65 78 74 SMALLEST.** ext
110bc 72 61 63 74 73 20 74 68 65 20 6c 65 61 73 74 20 racts the least
110bd 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 52 value from the R
110be 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 owSet..**.** The
110bf 20 49 4e 53 45 52 54 20 70 72 69 6d 69 74 69 76 INSERT primitiv
110c0 65 20 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 e might allocate
110c1 20 61 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f additional memo
110c2 72 79 2e 20 20 4d 65 6d 6f 72 79 20 69 73 0a 2a ry. Memory is.*
110c3 2a 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 63 * allocated in c
110c4 68 75 6e 6b 73 20 73 6f 20 6d 6f 73 74 20 49 4e hunks so most IN
110c5 53 45 52 54 73 20 64 6f 20 6e 6f 20 61 6c 6c 6f SERTs do no allo
110c6 63 61 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 69 cation. There i
110c7 73 20 61 6e 20 0a 2a 2a 20 75 70 70 65 72 20 62 s an .** upper b
110c8 6f 75 6e 64 20 6f 6e 20 74 68 65 20 73 69 7a 65 ound on the size
110c9 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 of allocated me
110ca 6d 6f 72 79 2e 20 20 4e 6f 20 6d 65 6d 6f 72 79 mory. No memory
110cb 20 69 73 20 66 72 65 65 64 0a 2a 2a 20 75 6e 74 is freed.** unt
110cc 69 6c 20 44 45 53 54 52 4f 59 2e 0a 2a 2a 0a 2a il DESTROY..**.*
110cd 2a 20 54 68 65 20 54 45 53 54 20 70 72 69 6d 69 * The TEST primi
110ce 74 69 76 65 20 69 6e 63 6c 75 64 65 73 20 61 20 tive includes a
110cf 22 62 61 74 63 68 22 20 6e 75 6d 62 65 72 2e 20 "batch" number.
110d0 20 54 68 65 20 54 45 53 54 20 70 72 69 6d 69 74 The TEST primit
110d1 69 76 65 0a 2a 2a 20 77 69 6c 6c 20 6f 6e 6c 79 ive.** will only
110d2 20 73 65 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 see elements th
110d3 61 74 20 77 65 72 65 20 69 6e 73 65 72 74 65 64 at were inserted
110d4 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61 73 74 before the last
110d5 20 63 68 61 6e 67 65 0a 2a 2a 20 69 6e 20 74 68 change.** in th
110d6 65 20 62 61 74 63 68 20 6e 75 6d 62 65 72 2e 20 e batch number.
110d7 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
110d8 20 69 66 20 61 6e 20 49 4e 53 45 52 54 20 6f 63 if an INSERT oc
110d9 63 75 72 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 curs between.**
110da 74 77 6f 20 54 45 53 54 73 20 77 68 65 72 65 20 two TESTs where
110db 74 68 65 20 54 45 53 54 73 20 68 61 76 65 20 74 the TESTs have t
110dc 68 65 20 73 61 6d 65 20 62 61 74 63 68 20 6e 75 he same batch nu
110dd 62 6d 65 72 2c 20 74 68 65 6e 20 74 68 65 0a 2a bmer, then the.*
110de 2a 20 76 61 6c 75 65 20 61 64 64 65 64 20 62 79 * value added by
110df 20 74 68 65 20 49 4e 53 45 52 54 20 77 69 6c 6c the INSERT will
110e0 20 6e 6f 74 20 62 65 20 76 69 73 69 62 6c 65 20 not be visible
110e1 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 54 45 to the second TE
110e2 53 54 2e 0a 2a 2a 20 54 68 65 20 69 6e 69 74 69 ST..** The initi
110e3 61 6c 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 al batch number
110e4 69 73 20 7a 65 72 6f 2c 20 73 6f 20 69 66 20 74 is zero, so if t
110e5 68 65 20 76 65 72 79 20 66 69 72 73 74 20 54 45 he very first TE
110e6 53 54 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 ST contains.** a
110e7 20 6e 6f 6e 2d 7a 65 72 6f 20 62 61 74 63 68 20 non-zero batch
110e8 6e 75 6d 62 65 72 2c 20 69 74 20 77 69 6c 6c 20 number, it will
110e9 73 65 65 20 61 6c 6c 20 70 72 69 6f 72 20 49 4e see all prior IN
110ea 53 45 52 54 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 SERTs..**.** No
110eb 49 4e 53 45 52 54 73 20 6d 61 79 20 6f 63 63 75 INSERTs may occu
110ec 72 73 20 61 66 74 65 72 20 61 20 53 4d 41 4c 4c rs after a SMALL
110ed 45 53 54 2e 20 20 41 6e 20 61 73 73 65 72 74 69 EST. An asserti
110ee 6f 6e 20 77 69 6c 6c 20 66 61 69 6c 20 69 66 0a on will fail if.
110ef 2a 2a 20 74 68 61 74 20 69 73 20 61 74 74 65 6d ** that is attem
110f0 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 pted..**.** The
110f1 63 6f 73 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 cost of an INSER
110f2 54 20 69 73 20 72 6f 75 67 68 6c 79 20 63 6f 6e T is roughly con
110f3 73 74 61 6e 74 2e 20 20 28 53 6f 6d 65 74 69 6d stant. (Sometim
110f4 65 20 6e 65 77 20 6d 65 6d 6f 72 79 0a 2a 2a 20 e new memory.**
110f5 68 61 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 has to be alloca
110f6 74 65 64 20 6f 6e 20 61 6e 20 49 4e 53 45 52 54 ted on an INSERT
110f7 2e 29 20 20 54 68 65 20 63 6f 73 74 20 6f 66 20 .) The cost of
110f8 61 20 54 45 53 54 20 77 69 74 68 20 61 20 6e 65 a TEST with a ne
110f9 77 0a 2a 2a 20 62 61 74 63 68 20 6e 75 6d 62 65 w.** batch numbe
110fa 72 20 69 73 20 4f 28 4e 6c 6f 67 4e 29 20 77 68 r is O(NlogN) wh
110fb 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d ere N is the num
110fc 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 ber of elements
110fd 69 6e 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a in the RowSet..*
110fe 2a 20 54 68 65 20 63 6f 73 74 20 6f 66 20 61 20 * The cost of a
110ff 54 45 53 54 20 75 73 69 6e 67 20 74 68 65 20 73 TEST using the s
11100 61 6d 65 20 62 61 74 63 68 20 6e 75 6d 62 65 72 ame batch number
11101 20 69 73 20 4f 28 6c 6f 67 4e 29 2e 20 20 54 68 is O(logN). Th
11102 65 20 63 6f 73 74 0a 2a 2a 20 6f 66 20 74 68 65 e cost.** of the
11103 20 66 69 72 73 74 20 53 4d 41 4c 4c 45 53 54 20 first SMALLEST
11104 69 73 20 4f 28 4e 6c 6f 67 4e 29 2e 20 20 53 65 is O(NlogN). Se
11105 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 cond and subsequ
11106 65 6e 74 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 ent SMALLEST.**
11107 70 72 69 6d 69 74 69 76 65 73 20 61 72 65 20 63 primitives are c
11108 6f 6e 73 74 61 6e 74 20 74 69 6d 65 2e 20 20 54 onstant time. T
11109 68 65 20 63 6f 73 74 20 6f 66 20 44 45 53 54 52 he cost of DESTR
1110a 4f 59 20 69 73 20 4f 28 4e 29 2e 0a 2a 2a 0a 2a OY is O(N)..**.*
1110b 2a 20 54 68 65 72 65 20 69 73 20 61 6e 20 61 64 * There is an ad
1110c 64 65 64 20 63 6f 73 74 20 6f 66 20 4f 28 4e 29 ded cost of O(N)
1110d 20 77 68 65 6e 20 73 77 69 74 63 68 69 6e 67 20 when switching
1110e 62 65 74 77 65 65 6e 20 54 45 53 54 20 61 6e 64 between TEST and
1110f 0a 2a 2a 20 53 4d 41 4c 4c 45 53 54 20 70 72 69 .** SMALLEST pri
11110 6d 69 74 69 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 mitives..**.** $
11111 49 64 3a 20 72 6f 77 73 65 74 2e 63 2c 76 20 31 Id: rowset.c,v 1
11112 2e 37 20 32 30 30 39 2f 30 35 2f 32 32 20 30 31 .7 2009/05/22 01
11113 3a 30 30 3a 31 33 20 64 72 68 20 45 78 70 20 24 :00:13 drh Exp $
11114 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 61 72 67 .*/.../*.** Targ
11115 65 74 20 73 69 7a 65 20 66 6f 72 20 61 6c 6c 6f et size for allo
11116 63 61 74 69 6f 6e 20 63 68 75 6e 6b 73 2e 0a 2a cation chunks..*
11117 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54 /.#define ROWSET
11118 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49 5a 45 _ALLOCATION_SIZE
11119 20 31 30 32 34 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 1024../*.** The
1111a 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 65 number of rowse
1111b 74 20 65 6e 74 72 69 65 73 20 70 65 72 20 61 6c t entries per al
1111c 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e 6b 2e 0a location chunk..
1111d 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57 53 45 */.#define ROWSE
1111e 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e T_ENTRY_PER_CHUN
1111f 4b 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 K \.
11120 20 20 20 20 20 20 20 20 20 20 20 20 28 28 52 4f ((RO
11121 57 53 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f WSET_ALLOCATION_
11122 53 49 5a 45 2d 38 29 2f 73 69 7a 65 6f 66 28 73 SIZE-8)/sizeof(s
11123 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
11124 79 29 29 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 y))../*.** Each
11125 65 6e 74 72 79 20 69 6e 20 61 20 52 6f 77 53 65 entry in a RowSe
11126 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 t is an instance
11127 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
11128 67 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72 g object..*/.str
11129 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
1112a 7b 20 20 20 20 20 20 20 20 20 20 20 20 0a 20 20 { .
1112b 69 36 34 20 76 3b 20 20 20 20 20 20 20 20 20 20 i64 v;
1112c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1112d 20 52 4f 57 49 44 20 76 61 6c 75 65 20 66 6f 72 ROWID value for
1112e 20 74 68 69 73 20 65 6e 74 72 79 20 2a 2f 0a 20 this entry */.
1112f 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
11130 74 72 79 20 2a 70 52 69 67 68 74 3b 20 20 20 2f try *pRight; /
11131 2a 20 52 69 67 68 74 20 73 75 62 74 72 65 65 20 * Right subtree
11132 28 6c 61 72 67 65 72 20 65 6e 74 72 69 65 73 29 (larger entries)
11133 20 6f 72 20 6c 69 73 74 20 2a 2f 0a 20 20 73 74 or list */. st
11134 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
11135 20 2a 70 4c 65 66 74 3b 20 20 20 20 2f 2a 20 4c *pLeft; /* L
11136 65 66 74 20 73 75 62 74 72 65 65 20 28 73 6d 61 eft subtree (sma
11137 6c 6c 65 72 20 65 6e 74 72 69 65 73 29 20 2a 2f ller entries) */
11138 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 77 53 65 .};../*.** RowSe
11139 74 45 6e 74 72 79 20 6f 62 6a 65 63 74 73 20 61 tEntry objects a
1113a 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 re allocated in
1113b 6c 61 72 67 65 20 63 68 75 6e 6b 73 20 28 69 6e large chunks (in
1113c 73 74 61 6e 63 65 73 20 6f 66 20 74 68 65 0a 2a stances of the.*
1113d 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 * following stru
1113e 63 74 75 72 65 29 20 74 6f 20 72 65 64 75 63 65 cture) to reduce
1113f 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
11140 6f 6e 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68 on overhead. Th
11141 65 0a 2a 2a 20 63 68 75 6e 6b 73 20 61 72 65 20 e.** chunks are
11142 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 kept on a linked
11143 20 6c 69 73 74 20 73 6f 20 74 68 61 74 20 74 68 list so that th
11144 65 79 20 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f ey can be deallo
11145 63 61 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 cated.** when th
11146 65 20 52 6f 77 53 65 74 20 69 73 20 64 65 73 74 e RowSet is dest
11147 72 6f 79 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 royed..*/.struct
11148 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 7b 0a 20 RowSetChunk {.
11149 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 struct RowSetCh
1114a 75 6e 6b 20 2a 70 4e 65 78 74 43 68 75 6e 6b 3b unk *pNextChunk;
1114b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 /* Next
1114c 63 68 75 6e 6b 20 6f 6e 20 6c 69 73 74 20 6f 66 chunk on list of
1114d 20 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 73 them all */. s
1114e 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
1114f 79 20 61 45 6e 74 72 79 5b 52 4f 57 53 45 54 5f y aEntry[ROWSET_
11150 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 5d ENTRY_PER_CHUNK]
11151 3b 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 65 ; /* Allocated e
11152 6e 74 72 69 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a ntries */.};../*
11153 0a 2a 2a 20 41 20 52 6f 77 53 65 74 20 69 6e 20 .** A RowSet in
11154 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
11155 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
11156 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 ucture..**.** A
11157 74 79 70 65 64 65 66 20 6f 66 20 74 68 69 73 20 typedef of this
11158 73 74 72 75 63 74 75 72 65 20 69 66 20 66 6f 75 structure if fou
11159 6e 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e nd in sqliteInt.
1115a 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 h..*/.struct Row
1115b 53 65 74 20 7b 0a 20 20 73 74 72 75 63 74 20 52 Set {. struct R
1115c 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 43 68 75 owSetChunk *pChu
1115d 6e 6b 3b 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f nk; /* List o
1115e 66 20 61 6c 6c 20 63 68 75 6e 6b 20 61 6c 6c 6f f all chunk allo
1115f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 71 6c cations */. sql
11160 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
11161 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
11162 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
11163 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 ection */. stru
11164 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
11165 70 45 6e 74 72 79 3b 20 20 20 20 2f 2a 20 4c 69 pEntry; /* Li
11166 73 74 20 6f 66 20 65 6e 74 72 69 65 73 20 75 73 st of entries us
11167 69 6e 67 20 70 52 69 67 68 74 20 2a 2f 0a 20 20 ing pRight */.
11168 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
11169 72 79 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 2f ry *pLast; /
1116a 2a 20 4c 61 73 74 20 65 6e 74 72 79 20 6f 6e 20 * Last entry on
1116b 74 68 65 20 70 45 6e 74 72 79 20 6c 69 73 74 20 the pEntry list
1116c 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 */. struct RowS
1116d 65 74 45 6e 74 72 79 20 2a 70 46 72 65 73 68 3b etEntry *pFresh;
1116e 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 6f 66 /* Source of
1116f 20 6e 65 77 20 65 6e 74 72 79 20 6f 62 6a 65 63 new entry objec
11170 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 ts */. struct R
11171 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 54 72 65 owSetEntry *pTre
11172 65 3b 20 20 20 20 20 2f 2a 20 42 69 6e 61 72 79 e; /* Binary
11173 20 74 72 65 65 20 6f 66 20 65 6e 74 72 69 65 73 tree of entries
11174 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72 65 73 68 */. u16 nFresh
11175 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11176 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
11177 66 20 6f 62 6a 65 63 74 73 20 6f 6e 20 70 46 72 f objects on pFr
11178 65 73 68 20 2a 2f 0a 20 20 75 38 20 69 73 53 6f esh */. u8 isSo
11179 72 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 rted;
1117a 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
1117b 69 66 20 70 45 6e 74 72 79 20 69 73 20 73 6f 72 if pEntry is sor
1117c 74 65 64 20 2a 2f 0a 20 20 75 38 20 69 42 61 74 ted */. u8 iBat
1117d 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ch;
1117e 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 /* Curre
1117f 6e 74 20 69 6e 73 65 72 74 20 62 61 74 63 68 20 nt insert batch
11180 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 */.};../*.** Tur
11181 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e n bulk memory in
11182 74 6f 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 to a RowSet obje
11183 63 74 2e 20 20 4e 20 62 79 74 65 73 20 6f 66 20 ct. N bytes of
11184 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 76 memory.** are av
11185 61 69 6c 61 62 6c 65 20 61 74 20 70 53 70 61 63 ailable at pSpac
11186 65 2e 20 20 54 68 65 20 64 62 20 70 6f 69 6e 74 e. The db point
11187 65 72 20 69 73 20 75 73 65 64 20 61 73 20 61 20 er is used as a
11188 6d 65 6d 6f 72 79 20 63 6f 6e 74 65 78 74 0a 2a memory context.*
11189 2a 20 66 6f 72 20 61 6e 79 20 73 75 62 73 65 71 * for any subseq
1118a 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 uent allocations
1118b 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 6f 63 that need to oc
1118c 63 75 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 cur..** Return a
1118d 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
1118e 6e 65 77 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 new RowSet objec
1118f 74 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 75 73 74 t..**.** It must
11190 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 be the case tha
11191 74 20 4e 20 69 73 20 73 75 66 66 69 63 69 65 6e t N is sufficien
11192 74 20 74 6f 20 6d 61 6b 65 20 61 20 52 6f 77 73 t to make a Rows
11193 65 74 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 61 et. If not.** a
11194 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c n assertion faul
11195 74 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 0a 2a 2a t occurs..** .**
11196 20 49 66 20 4e 20 69 73 20 6c 61 72 67 65 72 20 If N is larger
11197 74 68 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d than the minimum
11198 2c 20 75 73 65 20 74 68 65 20 73 75 72 70 6c 75 , use the surplu
11199 73 20 61 73 20 61 6e 20 69 6e 69 74 69 61 6c 0a s as an initial.
1119a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 ** allocation of
1119b 20 65 6e 74 72 69 65 73 20 61 76 61 69 6c 61 62 entries availab
1119c 6c 65 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 2e le to be filled.
1119d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1119e 54 45 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 74 TE RowSet *sqlit
1119f 65 33 52 6f 77 53 65 74 49 6e 69 74 28 73 71 6c e3RowSetInit(sql
111a0 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a ite3 *db, void *
111a1 70 53 70 61 63 65 2c 20 75 6e 73 69 67 6e 65 64 pSpace, unsigned
111a2 20 69 6e 74 20 4e 29 7b 0a 20 20 52 6f 77 53 65 int N){. RowSe
111a3 74 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 t *p;. assert(
111a4 4e 20 3e 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 N >= ROUND8(size
111a5 6f 66 28 2a 70 29 29 20 29 3b 0a 20 20 70 20 3d of(*p)) );. p =
111a6 20 70 53 70 61 63 65 3b 0a 20 20 70 2d 3e 70 43 pSpace;. p->pC
111a7 68 75 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 64 hunk = 0;. p->d
111a8 62 20 3d 20 64 62 3b 0a 20 20 70 2d 3e 70 45 6e b = db;. p->pEn
111a9 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c try = 0;. p->pL
111aa 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 54 ast = 0;. p->pT
111ab 72 65 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 46 ree = 0;. p->pF
111ac 72 65 73 68 20 3d 20 28 73 74 72 75 63 74 20 52 resh = (struct R
111ad 6f 77 53 65 74 45 6e 74 72 79 2a 29 28 52 4f 55 owSetEntry*)(ROU
111ae 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 29 29 20 ND8(sizeof(*p))
111af 2b 20 28 63 68 61 72 2a 29 70 29 3b 0a 20 20 70 + (char*)p);. p
111b0 2d 3e 6e 46 72 65 73 68 20 3d 20 28 75 31 36 29 ->nFresh = (u16)
111b1 28 28 4e 20 2d 20 52 4f 55 4e 44 38 28 73 69 7a ((N - ROUND8(siz
111b2 65 6f 66 28 2a 70 29 29 29 2f 73 69 7a 65 6f 66 eof(*p)))/sizeof
111b3 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e (struct RowSetEn
111b4 74 72 79 29 29 3b 0a 20 20 70 2d 3e 69 73 53 6f try));. p->isSo
111b5 72 74 65 64 20 3d 20 31 3b 0a 20 20 70 2d 3e 69 rted = 1;. p->i
111b6 42 61 74 63 68 20 3d 20 30 3b 0a 20 20 72 65 74 Batch = 0;. ret
111b7 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
111b8 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c 6c 20 63 Deallocate all c
111b9 68 75 6e 6b 73 20 66 72 6f 6d 20 61 20 52 6f 77 hunks from a Row
111ba 53 65 74 2e 20 20 54 68 69 73 20 66 72 65 65 73 Set. This frees
111bb 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 74 68 61 74 all memory that
111bc 0a 2a 2a 20 74 68 65 20 52 6f 77 53 65 74 20 68 .** the RowSet h
111bd 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 6f 76 65 as allocated ove
111be 72 20 69 74 73 20 6c 69 66 65 74 69 6d 65 2e 20 r its lifetime.
111bf 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
111c0 0a 2a 2a 20 74 68 65 20 64 65 73 74 72 75 63 74 .** the destruct
111c1 6f 72 20 66 6f 72 20 74 68 65 20 52 6f 77 53 65 or for the RowSe
111c2 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
111c3 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
111c4 33 52 6f 77 53 65 74 43 6c 65 61 72 28 52 6f 77 3RowSetClear(Row
111c5 53 65 74 20 2a 70 29 7b 0a 20 20 73 74 72 75 63 Set *p){. struc
111c6 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 t RowSetChunk *p
111c7 43 68 75 6e 6b 2c 20 2a 70 4e 65 78 74 43 68 75 Chunk, *pNextChu
111c8 6e 6b 3b 0a 20 20 66 6f 72 28 70 43 68 75 6e 6b nk;. for(pChunk
111c9 3d 70 2d 3e 70 43 68 75 6e 6b 3b 20 70 43 68 75 =p->pChunk; pChu
111ca 6e 6b 3b 20 70 43 68 75 6e 6b 20 3d 20 70 4e 65 nk; pChunk = pNe
111cb 78 74 43 68 75 6e 6b 29 7b 0a 20 20 20 20 70 4e xtChunk){. pN
111cc 65 78 74 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e extChunk = pChun
111cd 6b 2d 3e 70 4e 65 78 74 43 68 75 6e 6b 3b 0a 20 k->pNextChunk;.
111ce 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
111cf 28 70 2d 3e 64 62 2c 20 70 43 68 75 6e 6b 29 3b (p->db, pChunk);
111d0 0a 20 20 7d 0a 20 20 70 2d 3e 70 43 68 75 6e 6b . }. p->pChunk
111d1 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 = 0;. p->nFres
111d2 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 45 6e 74 h = 0;. p->pEnt
111d3 72 79 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c 61 ry = 0;. p->pLa
111d4 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 54 72 st = 0;. p->pTr
111d5 65 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53 ee = 0;. p->isS
111d6 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a orted = 1;.}../*
111d7 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 77 .** Insert a new
111d8 20 76 61 6c 75 65 20 69 6e 74 6f 20 61 20 52 6f value into a Ro
111d9 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 wSet..**.** The
111da 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 mallocFailed fla
111db 67 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 g of the databas
111dc 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 e connection is
111dd 73 65 74 20 69 66 20 61 0a 2a 2a 20 6d 65 6d 6f set if a.** memo
111de 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 ry allocation fa
111df 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ils..*/.SQLITE_P
111e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
111e1 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 te3RowSetInsert(
111e2 52 6f 77 53 65 74 20 2a 70 2c 20 69 36 34 20 72 RowSet *p, i64 r
111e3 6f 77 69 64 29 7b 0a 20 20 73 74 72 75 63 74 20 owid){. struct
111e4 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45 6e RowSetEntry *pEn
111e5 74 72 79 3b 20 20 2f 2a 20 54 68 65 20 6e 65 77 try; /* The new
111e6 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 entry */. stru
111e7 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
111e8 70 4c 61 73 74 3b 20 20 20 2f 2a 20 54 68 65 20 pLast; /* The
111e9 6c 61 73 74 20 70 72 69 6f 72 20 65 6e 74 72 79 last prior entry
111ea 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 21 */. assert( p!
111eb 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e =0 );. if( p->n
111ec 46 72 65 73 68 3d 3d 30 20 29 7b 0a 20 20 20 20 Fresh==0 ){.
111ed 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 struct RowSetChu
111ee 6e 6b 20 2a 70 4e 65 77 3b 0a 20 20 20 20 70 4e nk *pNew;. pN
111ef 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 ew = sqlite3DbMa
111f0 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 73 llocRaw(p->db, s
111f1 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 izeof(*pNew));.
111f2 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 if( pNew==0 )
111f3 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a {. return;.
111f4 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e }. pNew->
111f5 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20 70 2d 3e pNextChunk = p->
111f6 70 43 68 75 6e 6b 3b 0a 20 20 20 20 70 2d 3e 70 pChunk;. p->p
111f7 43 68 75 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 Chunk = pNew;.
111f8 20 20 70 2d 3e 70 46 72 65 73 68 20 3d 20 70 4e p->pFresh = pN
111f9 65 77 2d 3e 61 45 6e 74 72 79 3b 0a 20 20 20 20 ew->aEntry;.
111fa 70 2d 3e 6e 46 72 65 73 68 20 3d 20 52 4f 57 53 p->nFresh = ROWS
111fb 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 ET_ENTRY_PER_CHU
111fc 4e 4b 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 NK;. }. pEntry
111fd 20 3d 20 70 2d 3e 70 46 72 65 73 68 2b 2b 3b 0a = p->pFresh++;.
111fe 20 20 70 2d 3e 6e 46 72 65 73 68 2d 2d 3b 0a 20 p->nFresh--;.
111ff 20 70 45 6e 74 72 79 2d 3e 76 20 3d 20 72 6f 77 pEntry->v = row
11200 69 64 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 70 52 id;. pEntry->pR
11201 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 4c 61 73 ight = 0;. pLas
11202 74 20 3d 20 70 2d 3e 70 4c 61 73 74 3b 0a 20 20 t = p->pLast;.
11203 69 66 28 20 70 4c 61 73 74 20 29 7b 0a 20 20 20 if( pLast ){.
11204 20 69 66 28 20 70 2d 3e 69 73 53 6f 72 74 65 64 if( p->isSorted
11205 20 26 26 20 72 6f 77 69 64 3c 3d 70 4c 61 73 74 && rowid<=pLast
11206 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e ->v ){. p->
11207 69 73 53 6f 72 74 65 64 20 3d 20 30 3b 0a 20 20 isSorted = 0;.
11208 20 20 7d 0a 20 20 20 20 70 4c 61 73 74 2d 3e 70 }. pLast->p
11209 52 69 67 68 74 20 3d 20 70 45 6e 74 72 79 3b 0a Right = pEntry;.
1120a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
1120b 65 72 74 28 20 70 2d 3e 70 45 6e 74 72 79 3d 3d ert( p->pEntry==
1120c 30 20 29 3b 20 2f 2a 20 46 69 72 65 73 20 69 66 0 ); /* Fires if
1120d 20 49 4e 53 45 52 54 20 61 66 74 65 72 20 53 4d INSERT after SM
1120e 41 4c 4c 45 53 54 20 2a 2f 0a 20 20 20 20 70 2d ALLEST */. p-
1120f 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 79 >pEntry = pEntry
11210 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c 61 73 74 ;. }. p->pLast
11211 20 3d 20 70 45 6e 74 72 79 3b 0a 7d 0a 0a 2f 2a = pEntry;.}../*
11212 0a 2a 2a 20 4d 65 72 67 65 20 74 77 6f 20 6c 69 .** Merge two li
11213 73 74 73 20 6f 66 20 52 6f 77 53 65 74 45 6e 74 sts of RowSetEnt
11214 72 79 20 6f 62 6a 65 63 74 73 2e 20 20 52 65 6d ry objects. Rem
11215 6f 76 65 20 64 75 70 6c 69 63 61 74 65 73 2e 0a ove duplicates..
11216 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 **.** The input
11217 6c 69 73 74 73 20 61 72 65 20 63 6f 6e 6e 65 63 lists are connec
11218 74 65 64 20 76 69 61 20 70 52 69 67 68 74 20 70 ted via pRight p
11219 6f 69 6e 74 65 72 73 20 61 6e 64 20 61 72 65 20 ointers and are
1121a 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 6f 20 65 .** assumed to e
1121b 61 63 68 20 61 6c 72 65 61 64 79 20 62 65 20 69 ach already be i
1121c 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a n sorted order..
1121d 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 */.static struct
1121e 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f RowSetEntry *ro
1121f 77 53 65 74 4d 65 72 67 65 28 0a 20 20 73 74 72 wSetMerge(. str
11220 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
11221 2a 70 41 2c 20 20 20 20 2f 2a 20 46 69 72 73 74 *pA, /* First
11222 20 73 6f 72 74 65 64 20 6c 69 73 74 20 74 6f 20 sorted list to
11223 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a 20 20 73 be merged */. s
11224 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
11225 79 20 2a 70 42 20 20 20 20 20 2f 2a 20 53 65 63 y *pB /* Sec
11226 6f 6e 64 20 73 6f 72 74 65 64 20 6c 69 73 74 20 ond sorted list
11227 74 6f 20 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a to be merged */.
11228 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 ){. struct RowS
11229 65 74 45 6e 74 72 79 20 68 65 61 64 3b 0a 20 20 etEntry head;.
1122a 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
1122b 72 79 20 2a 70 54 61 69 6c 3b 0a 0a 20 20 70 54 ry *pTail;.. pT
1122c 61 69 6c 20 3d 20 26 68 65 61 64 3b 0a 20 20 77 ail = &head;. w
1122d 68 69 6c 65 28 20 70 41 20 26 26 20 70 42 20 29 hile( pA && pB )
1122e 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 41 {. assert( pA
1122f 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 ->pRight==0 || p
11230 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52 69 67 68 74 A->v<=pA->pRight
11231 2d 3e 76 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ->v );. asser
11232 74 28 20 70 42 2d 3e 70 52 69 67 68 74 3d 3d 30 t( pB->pRight==0
11233 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 || pB->v<=pB->p
11234 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 Right->v );.
11235 69 66 28 20 70 41 2d 3e 76 3c 70 42 2d 3e 76 20 if( pA->v<pB->v
11236 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e ){. pTail->
11237 70 52 69 67 68 74 20 3d 20 70 41 3b 0a 20 20 20 pRight = pA;.
11238 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 52 69 67 pA = pA->pRig
11239 68 74 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20 ht;. pTail
1123a 3d 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b = pTail->pRight;
1123b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 . }else if( p
1123c 42 2d 3e 76 3c 70 41 2d 3e 76 20 29 7b 0a 20 20 B->v<pA->v ){.
1123d 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 pTail->pRigh
1123e 74 20 3d 20 70 42 3b 0a 20 20 20 20 20 20 70 42 t = pB;. pB
1123f 20 3d 20 70 42 2d 3e 70 52 69 67 68 74 3b 0a 20 = pB->pRight;.
11240 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 54 61 pTail = pTa
11241 69 6c 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 il->pRight;.
11242 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 41 20 }else{. pA
11243 3d 20 70 41 2d 3e 70 52 69 67 68 74 3b 0a 20 20 = pA->pRight;.
11244 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 41 }. }. if( pA
11245 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
11246 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c pA->pRight==0 ||
11247 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52 69 67 pA->v<=pA->pRig
11248 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 70 54 61 ht->v );. pTa
11249 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 41 3b il->pRight = pA;
1124a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 . }else{. as
1124b 73 65 72 74 28 20 70 42 3d 3d 30 20 7c 7c 20 70 sert( pB==0 || p
1124c 42 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 B->pRight==0 ||
1124d 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 52 69 67 68 pB->v<=pB->pRigh
1124e 74 2d 3e 76 20 29 3b 0a 20 20 20 20 70 54 61 69 t->v );. pTai
1124f 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 42 3b 0a l->pRight = pB;.
11250 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 65 61 }. return hea
11251 64 2e 70 52 69 67 68 74 3b 0a 7d 0a 0a 2f 2a 0a d.pRight;.}../*.
11252 2a 2a 20 53 6f 72 74 20 61 6c 6c 20 65 6c 65 6d ** Sort all elem
11253 65 6e 74 73 20 6f 6e 20 74 68 65 20 70 45 6e 74 ents on the pEnt
11254 72 79 20 6c 69 73 74 20 6f 66 20 74 68 65 20 52 ry list of the R
11255 6f 77 53 65 74 20 69 6e 74 6f 20 61 73 63 65 6e owSet into ascen
11256 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 20 0a ding order..*/ .
11257 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53 static void rowS
11258 65 74 53 6f 72 74 28 52 6f 77 53 65 74 20 2a 70 etSort(RowSet *p
11259 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e ){. unsigned in
1125a 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 52 6f t i;. struct Ro
1125b 77 53 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 wSetEntry *pEntr
1125c 79 3b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 y;. struct RowS
1125d 65 74 45 6e 74 72 79 20 2a 61 42 75 63 6b 65 74 etEntry *aBucket
1125e 5b 34 30 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 [40];.. assert(
1125f 20 70 2d 3e 69 73 53 6f 72 74 65 64 3d 3d 30 20 p->isSorted==0
11260 29 3b 0a 20 20 6d 65 6d 73 65 74 28 61 42 75 63 );. memset(aBuc
11261 6b 65 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 ket, 0, sizeof(a
11262 42 75 63 6b 65 74 29 29 3b 0a 20 20 77 68 69 6c Bucket));. whil
11263 65 28 20 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a e( p->pEntry ){.
11264 20 20 20 20 70 45 6e 74 72 79 20 3d 20 70 2d 3e pEntry = p->
11265 70 45 6e 74 72 79 3b 0a 20 20 20 20 70 2d 3e 70 pEntry;. p->p
11266 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 79 2d 3e Entry = pEntry->
11267 70 52 69 67 68 74 3b 0a 20 20 20 20 70 45 6e 74 pRight;. pEnt
11268 72 79 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a ry->pRight = 0;.
11269 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 61 42 75 for(i=0; aBu
1126a 63 6b 65 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 cket[i]; i++){.
1126b 20 20 20 20 20 70 45 6e 74 72 79 20 3d 20 72 6f pEntry = ro
1126c 77 53 65 74 4d 65 72 67 65 28 61 42 75 63 6b 65 wSetMerge(aBucke
1126d 74 5b 69 5d 2c 20 70 45 6e 74 72 79 29 3b 0a 20 t[i], pEntry);.
1126e 20 20 20 20 20 61 42 75 63 6b 65 74 5b 69 5d 20 aBucket[i]
1126f 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 = 0;. }. a
11270 42 75 63 6b 65 74 5b 69 5d 20 3d 20 70 45 6e 74 Bucket[i] = pEnt
11271 72 79 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 ry;. }. pEntry
11272 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b = 0;. for(i=0;
11273 20 69 3c 73 69 7a 65 6f 66 28 61 42 75 63 6b 65 i<sizeof(aBucke
11274 74 29 2f 73 69 7a 65 6f 66 28 61 42 75 63 6b 65 t)/sizeof(aBucke
11275 74 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 t[0]); i++){.
11276 20 70 45 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 pEntry = rowSet
11277 4d 65 72 67 65 28 70 45 6e 74 72 79 2c 20 61 42 Merge(pEntry, aB
11278 75 63 6b 65 74 5b 69 5d 29 3b 0a 20 20 7d 0a 20 ucket[i]);. }.
11279 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e p->pEntry = pEn
1127a 74 72 79 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20 try;. p->pLast
1127b 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 74 = 0;. p->isSort
1127c 65 64 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a ed = 1;.}.../*.*
1127d 2a 20 54 68 65 20 69 6e 70 75 74 2c 20 70 49 6e * The input, pIn
1127e 2c 20 69 73 20 61 20 62 69 6e 61 72 79 20 74 72 , is a binary tr
1127f 65 65 20 28 6f 72 20 73 75 62 74 72 65 65 29 20 ee (or subtree)
11280 6f 66 20 52 6f 77 53 65 74 45 6e 74 72 79 20 6f of RowSetEntry o
11281 62 6a 65 63 74 73 2e 0a 2a 2a 20 43 6f 6e 76 65 bjects..** Conve
11282 72 74 20 74 68 69 73 20 74 72 65 65 20 69 6e 74 rt this tree int
11283 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 o a linked list
11284 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 74 68 65 connected by the
11285 20 70 52 69 67 68 74 20 70 6f 69 6e 74 65 72 73 pRight pointers
11286 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e 20 70 .** and return p
11287 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 66 ointers to the f
11288 69 72 73 74 20 61 6e 64 20 6c 61 73 74 20 65 6c irst and last el
11289 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6e 65 ements of the ne
1128a 77 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 w list..*/.stati
1128b 63 20 76 6f 69 64 20 72 6f 77 53 65 74 54 72 65 c void rowSetTre
1128c 65 54 6f 4c 69 73 74 28 0a 20 20 73 74 72 75 63 eToList(. struc
1128d 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 t RowSetEntry *p
1128e 49 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 In, /* R
1128f 6f 6f 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74 oot of the input
11290 20 74 72 65 65 20 2a 2f 0a 20 20 73 74 72 75 63 tree */. struc
11291 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a t RowSetEntry **
11292 70 70 46 69 72 73 74 2c 20 20 20 20 2f 2a 20 57 ppFirst, /* W
11293 72 69 74 65 20 68 65 61 64 20 6f 66 20 74 68 65 rite head of the
11294 20 6f 75 74 70 75 74 20 6c 69 73 74 20 68 65 72 output list her
11295 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f e */. struct Ro
11296 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 70 4c 61 wSetEntry **ppLa
11297 73 74 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 st /* Write
11298 20 74 61 69 6c 20 6f 66 20 74 68 65 20 6f 75 74 tail of the out
11299 70 75 74 20 6c 69 73 74 20 68 65 72 65 20 2a 2f put list here */
1129a 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 49 .){. assert( pI
1129b 6e 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 49 n!=0 );. if( pI
1129c 6e 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 n->pLeft ){.
1129d 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
1129e 72 79 20 2a 70 3b 0a 20 20 20 20 72 6f 77 53 65 ry *p;. rowSe
1129f 74 54 72 65 65 54 6f 4c 69 73 74 28 70 49 6e 2d tTreeToList(pIn-
112a0 3e 70 4c 65 66 74 2c 20 70 70 46 69 72 73 74 2c >pLeft, ppFirst,
112a1 20 26 70 29 3b 0a 20 20 20 20 70 2d 3e 70 52 69 &p);. p->pRi
112a2 67 68 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 65 6c ght = pIn;. }el
112a3 73 65 7b 0a 20 20 20 20 2a 70 70 46 69 72 73 74 se{. *ppFirst
112a4 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a 20 20 69 66 = pIn;. }. if
112a5 28 20 70 49 6e 2d 3e 70 52 69 67 68 74 20 29 7b ( pIn->pRight ){
112a6 0a 20 20 20 20 72 6f 77 53 65 74 54 72 65 65 54 . rowSetTreeT
112a7 6f 4c 69 73 74 28 70 49 6e 2d 3e 70 52 69 67 68 oList(pIn->pRigh
112a8 74 2c 20 26 70 49 6e 2d 3e 70 52 69 67 68 74 2c t, &pIn->pRight,
112a9 20 70 70 4c 61 73 74 29 3b 0a 20 20 7d 65 6c 73 ppLast);. }els
112aa 65 7b 0a 20 20 20 20 2a 70 70 4c 61 73 74 20 3d e{. *ppLast =
112ab 20 70 49 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 pIn;. }. asse
112ac 72 74 28 20 28 2a 70 70 4c 61 73 74 29 2d 3e 70 rt( (*ppLast)->p
112ad 52 69 67 68 74 3d 3d 30 20 29 3b 0a 7d 0a 0a 0a Right==0 );.}...
112ae 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 /*.** Convert a
112af 73 6f 72 74 65 64 20 6c 69 73 74 20 6f 66 20 65 sorted list of e
112b0 6c 65 6d 65 6e 74 73 20 28 63 6f 6e 6e 65 63 74 lements (connect
112b1 65 64 20 62 79 20 70 52 69 67 68 74 29 20 69 6e ed by pRight) in
112b2 74 6f 20 61 20 62 69 6e 61 72 79 0a 2a 2a 20 74 to a binary.** t
112b3 72 65 65 20 77 69 74 68 20 64 65 70 74 68 20 6f ree with depth o
112b4 66 20 69 44 65 70 74 68 2e 20 20 41 20 64 65 70 f iDepth. A dep
112b5 74 68 20 6f 66 20 31 20 6d 65 61 6e 73 20 74 68 th of 1 means th
112b6 65 20 74 72 65 65 20 63 6f 6e 74 61 69 6e 73 20 e tree contains
112b7 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6e 6f 64 65 a single.** node
112b8 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 taken from the
112b9 68 65 61 64 20 6f 66 20 2a 70 70 4c 69 73 74 2e head of *ppList.
112ba 20 20 41 20 64 65 70 74 68 20 6f 66 20 32 20 6d A depth of 2 m
112bb 65 61 6e 73 20 61 20 74 72 65 65 20 77 69 74 68 eans a tree with
112bc 0a 2a 2a 20 74 68 72 65 65 20 6e 6f 64 65 73 2e .** three nodes.
112bd 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a And so forth..
112be 2a 2a 0a 2a 2a 20 55 73 65 20 61 73 20 6d 61 6e **.** Use as man
112bf 79 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 y entries from t
112c0 68 65 20 69 6e 70 75 74 20 6c 69 73 74 20 61 73 he input list as
112c1 20 72 65 71 75 69 72 65 64 20 61 6e 64 20 75 70 required and up
112c2 64 61 74 65 20 74 68 65 0a 2a 2a 20 2a 70 70 4c date the.** *ppL
112c3 69 73 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 ist to point to
112c4 74 68 65 20 75 6e 75 73 65 64 20 65 6c 65 6d 65 the unused eleme
112c5 6e 74 73 20 6f 66 20 74 68 65 20 6c 69 73 74 2e nts of the list.
112c6 20 20 49 66 20 74 68 65 20 69 6e 70 75 74 0a 2a If the input.*
112c7 2a 20 6c 69 73 74 20 63 6f 6e 74 61 69 6e 73 20 * list contains
112c8 74 6f 6f 20 66 65 77 20 65 6c 65 6d 65 6e 74 73 too few elements
112c9 2c 20 74 68 65 6e 20 63 6f 6e 73 74 72 75 63 74 , then construct
112ca 20 61 6e 20 69 6e 63 6f 6d 70 6c 65 74 65 20 74 an incomplete t
112cb 72 65 65 0a 2a 2a 20 61 6e 64 20 6c 65 61 76 65 ree.** and leave
112cc 20 2a 70 70 4c 69 73 74 20 73 65 74 20 74 6f 20 *ppList set to
112cd 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 NULL..**.** Retu
112ce 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
112cf 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 the root of the
112d0 63 6f 6e 73 74 72 75 63 74 65 64 20 62 69 6e 61 constructed bina
112d1 72 79 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 ry tree..*/.stat
112d2 69 63 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 ic struct RowSet
112d3 45 6e 74 72 79 20 2a 72 6f 77 53 65 74 4e 44 65 Entry *rowSetNDe
112d4 65 70 54 72 65 65 28 0a 20 20 73 74 72 75 63 74 epTree(. struct
112d5 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 RowSetEntry **p
112d6 70 4c 69 73 74 2c 0a 20 20 69 6e 74 20 69 44 65 pList,. int iDe
112d7 70 74 68 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 pth.){. struct
112d8 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 20 RowSetEntry *p;
112d9 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 /* Root
112da 6f 66 20 74 68 65 20 6e 65 77 20 74 72 65 65 20 of the new tree
112db 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 */. struct RowS
112dc 65 74 45 6e 74 72 79 20 2a 70 4c 65 66 74 3b 20 etEntry *pLeft;
112dd 20 20 20 20 2f 2a 20 4c 65 66 74 20 73 75 62 74 /* Left subt
112de 72 65 65 20 2a 2f 0a 20 20 69 66 28 20 2a 70 70 ree */. if( *pp
112df 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 List==0 ){. r
112e0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 eturn 0;. }. i
112e1 66 28 20 69 44 65 70 74 68 3d 3d 31 20 29 7b 0a f( iDepth==1 ){.
112e2 20 20 20 20 70 20 3d 20 2a 70 70 4c 69 73 74 3b p = *ppList;
112e3 0a 20 20 20 20 2a 70 70 4c 69 73 74 20 3d 20 70 . *ppList = p
112e4 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 70 2d ->pRight;. p-
112e5 3e 70 4c 65 66 74 20 3d 20 70 2d 3e 70 52 69 67 >pLeft = p->pRig
112e6 68 74 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 ht = 0;. retu
112e7 72 6e 20 70 3b 0a 20 20 7d 0a 20 20 70 4c 65 66 rn p;. }. pLef
112e8 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 65 70 54 t = rowSetNDeepT
112e9 72 65 65 28 70 70 4c 69 73 74 2c 20 69 44 65 70 ree(ppList, iDep
112ea 74 68 2d 31 29 3b 0a 20 20 70 20 3d 20 2a 70 70 th-1);. p = *pp
112eb 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 List;. if( p==0
112ec 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 ){. return p
112ed 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 Left;. }. p->p
112ee 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20 Left = pLeft;.
112ef 2a 70 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69 *ppList = p->pRi
112f0 67 68 74 3b 0a 20 20 70 2d 3e 70 52 69 67 68 74 ght;. p->pRight
112f1 20 3d 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72 = rowSetNDeepTr
112f2 65 65 28 70 70 4c 69 73 74 2c 20 69 44 65 70 74 ee(ppList, iDept
112f3 68 2d 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 h-1);. return p
112f4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 ;.}../*.** Conve
112f5 72 74 20 61 20 73 6f 72 74 65 64 20 6c 69 73 74 rt a sorted list
112f6 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 74 of elements int
112f7 6f 20 61 20 62 69 6e 61 72 79 20 74 72 65 65 2e o a binary tree.
112f8 20 4d 61 6b 65 20 74 68 65 20 74 72 65 65 0a 2a Make the tree.*
112f9 2a 20 61 73 20 64 65 65 70 20 61 73 20 69 74 20 * as deep as it
112fa 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 20 6f needs to be in o
112fb 72 64 65 72 20 74 6f 20 63 6f 6e 74 61 69 6e 20 rder to contain
112fc 74 68 65 20 65 6e 74 69 72 65 20 6c 69 73 74 2e the entire list.
112fd 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 .*/.static struc
112fe 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 t RowSetEntry *r
112ff 6f 77 53 65 74 4c 69 73 74 54 6f 54 72 65 65 28 owSetListToTree(
11300 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
11301 72 79 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e ry *pList){. in
11302 74 20 69 44 65 70 74 68 3b 20 20 20 20 20 20 20 t iDepth;
11303 20 20 20 20 2f 2a 20 44 65 70 74 68 20 6f 66 20 /* Depth of
11304 74 68 65 20 74 72 65 65 20 73 6f 20 66 61 72 20 the tree so far
11305 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 */. struct RowS
11306 65 74 45 6e 74 72 79 20 2a 70 3b 20 20 20 20 20 etEntry *p;
11307 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 74 72 65 /* Current tre
11308 65 20 72 6f 6f 74 20 2a 2f 0a 20 20 73 74 72 75 e root */. stru
11309 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
1130a 70 4c 65 66 74 3b 20 20 20 2f 2a 20 4c 65 66 74 pLeft; /* Left
1130b 20 73 75 62 74 72 65 65 20 2a 2f 0a 0a 20 20 61 subtree */.. a
1130c 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 ssert( pList!=0
1130d 29 3b 0a 20 20 70 20 3d 20 70 4c 69 73 74 3b 0a );. p = pList;.
1130e 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69 pList = p->pRi
1130f 67 68 74 3b 0a 20 20 70 2d 3e 70 4c 65 66 74 20 ght;. p->pLeft
11310 3d 20 70 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b = p->pRight = 0;
11311 0a 20 20 66 6f 72 28 69 44 65 70 74 68 3d 31 3b . for(iDepth=1;
11312 20 70 4c 69 73 74 3b 20 69 44 65 70 74 68 2b 2b pList; iDepth++
11313 29 7b 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 70 ){. pLeft = p
11314 3b 0a 20 20 20 20 70 20 3d 20 70 4c 69 73 74 3b ;. p = pList;
11315 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e . pList = p->
11316 70 52 69 67 68 74 3b 0a 20 20 20 20 70 2d 3e 70 pRight;. p->p
11317 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20 Left = pLeft;.
11318 20 20 70 2d 3e 70 52 69 67 68 74 20 3d 20 72 6f p->pRight = ro
11319 77 53 65 74 4e 44 65 65 70 54 72 65 65 28 26 70 wSetNDeepTree(&p
1131a 4c 69 73 74 2c 20 69 44 65 70 74 68 29 3b 0a 20 List, iDepth);.
1131b 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d }. return p;.}
1131c 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 ../*.** Convert
1131d 74 68 65 20 6c 69 73 74 20 69 6e 20 70 2d 3e 70 the list in p->p
1131e 45 6e 74 72 79 20 69 6e 74 6f 20 61 20 73 6f 72 Entry into a sor
1131f 74 65 64 20 6c 69 73 74 20 69 66 20 69 74 20 69 ted list if it i
11320 73 20 6e 6f 74 0a 2a 2a 20 73 6f 72 74 65 64 20 s not.** sorted
11321 61 6c 72 65 61 64 79 2e 20 20 49 66 20 74 68 65 already. If the
11322 72 65 20 69 73 20 61 20 62 69 6e 61 72 79 20 74 re is a binary t
11323 72 65 65 20 6f 6e 20 70 2d 3e 70 54 72 65 65 2c ree on p->pTree,
11324 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76 65 72 74 then.** convert
11325 20 69 74 20 69 6e 74 6f 20 61 20 6c 69 73 74 20 it into a list
11326 74 6f 6f 20 61 6e 64 20 6d 65 72 67 65 20 69 74 too and merge it
11327 20 69 6e 74 6f 20 74 68 65 20 70 2d 3e 70 45 6e into the p->pEn
11328 74 72 79 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 try list..*/.sta
11329 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 74 54 tic void rowSetT
1132a 6f 4c 69 73 74 28 52 6f 77 53 65 74 20 2a 70 29 oList(RowSet *p)
1132b 7b 0a 20 20 69 66 28 20 21 70 2d 3e 69 73 53 6f {. if( !p->isSo
1132c 72 74 65 64 20 29 7b 0a 20 20 20 20 72 6f 77 53 rted ){. rowS
1132d 65 74 53 6f 72 74 28 70 29 3b 0a 20 20 7d 0a 20 etSort(p);. }.
1132e 20 69 66 28 20 70 2d 3e 70 54 72 65 65 20 29 7b if( p->pTree ){
1132f 0a 20 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 . struct RowS
11330 65 74 45 6e 74 72 79 20 2a 70 48 65 61 64 2c 20 etEntry *pHead,
11331 2a 70 54 61 69 6c 3b 0a 20 20 20 20 72 6f 77 53 *pTail;. rowS
11332 65 74 54 72 65 65 54 6f 4c 69 73 74 28 70 2d 3e etTreeToList(p->
11333 70 54 72 65 65 2c 20 26 70 48 65 61 64 2c 20 26 pTree, &pHead, &
11334 70 54 61 69 6c 29 3b 0a 20 20 20 20 70 2d 3e 70 pTail);. p->p
11335 54 72 65 65 20 3d 20 30 3b 0a 20 20 20 20 70 2d Tree = 0;. p-
11336 3e 70 45 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 >pEntry = rowSet
11337 4d 65 72 67 65 28 70 2d 3e 70 45 6e 74 72 79 2c Merge(p->pEntry,
11338 20 70 48 65 61 64 29 3b 0a 20 20 7d 0a 7d 0a 0a pHead);. }.}..
11339 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 /*.** Extract th
1133a 65 20 73 6d 61 6c 6c 65 73 74 20 65 6c 65 6d 65 e smallest eleme
1133b 6e 74 20 66 72 6f 6d 20 74 68 65 20 52 6f 77 53 nt from the RowS
1133c 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 et..** Write the
1133d 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 2a 70 element into *p
1133e 52 6f 77 69 64 2e 20 20 52 65 74 75 72 6e 20 31 Rowid. Return 1
1133f 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 52 65 on success. Re
11340 74 75 72 6e 0a 2a 2a 20 30 20 69 66 20 74 68 65 turn.** 0 if the
11341 20 52 6f 77 53 65 74 20 69 73 20 61 6c 72 65 61 RowSet is alrea
11342 64 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 dy empty..**.**
11343 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 After this routi
11344 6e 65 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c ne has been call
11345 65 64 2c 20 74 68 65 20 73 71 6c 69 74 65 33 52 ed, the sqlite3R
11346 6f 77 53 65 74 49 6e 73 65 72 74 28 29 0a 2a 2a owSetInsert().**
11347 20 72 6f 75 74 69 6e 65 20 6d 61 79 20 6e 6f 74 routine may not
11348 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e be called again
11349 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 . .*/.SQLITE_PR
1134a 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1134b 33 52 6f 77 53 65 74 4e 65 78 74 28 52 6f 77 53 3RowSetNext(RowS
1134c 65 74 20 2a 70 2c 20 69 36 34 20 2a 70 52 6f 77 et *p, i64 *pRow
1134d 69 64 29 7b 0a 20 20 72 6f 77 53 65 74 54 6f 4c id){. rowSetToL
1134e 69 73 74 28 70 29 3b 0a 20 20 69 66 28 20 70 2d ist(p);. if( p-
1134f 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 2a >pEntry ){. *
11350 70 52 6f 77 69 64 20 3d 20 70 2d 3e 70 45 6e 74 pRowid = p->pEnt
11351 72 79 2d 3e 76 3b 0a 20 20 20 20 70 2d 3e 70 45 ry->v;. p->pE
11352 6e 74 72 79 20 3d 20 70 2d 3e 70 45 6e 74 72 79 ntry = p->pEntry
11353 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 69 66 ->pRight;. if
11354 28 20 70 2d 3e 70 45 6e 74 72 79 3d 3d 30 20 29 ( p->pEntry==0 )
11355 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 {. sqlite3R
11356 6f 77 53 65 74 43 6c 65 61 72 28 70 29 3b 0a 20 owSetClear(p);.
11357 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 }. return
11358 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 1;. }else{.
11359 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a return 0;. }.}.
1135a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 ./*.** Check to
1135b 73 65 65 20 69 66 20 65 6c 65 6d 65 6e 74 20 69 see if element i
1135c 52 6f 77 69 64 20 77 61 73 20 69 6e 73 65 72 74 Rowid was insert
1135d 65 64 20 69 6e 74 6f 20 74 68 65 20 74 68 65 20 ed into the the
1135e 72 6f 77 73 65 74 20 61 73 0a 2a 2a 20 70 61 72 rowset as.** par
1135f 74 20 6f 66 20 61 6e 79 20 69 6e 73 65 72 74 20 t of any insert
11360 62 61 74 63 68 20 70 72 69 6f 72 20 74 6f 20 69 batch prior to i
11361 42 61 74 63 68 2e 20 20 52 65 74 75 72 6e 20 31 Batch. Return 1
11362 20 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 or 0..*/.SQLITE
11363 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
11364 69 74 65 33 52 6f 77 53 65 74 54 65 73 74 28 52 ite3RowSetTest(R
11365 6f 77 53 65 74 20 2a 70 52 6f 77 53 65 74 2c 20 owSet *pRowSet,
11366 75 38 20 69 42 61 74 63 68 2c 20 73 71 6c 69 74 u8 iBatch, sqlit
11367 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 29 e3_int64 iRowid)
11368 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 {. struct RowSe
11369 74 45 6e 74 72 79 20 2a 70 3b 0a 20 20 69 66 28 tEntry *p;. if(
1136a 20 69 42 61 74 63 68 21 3d 70 52 6f 77 53 65 74 iBatch!=pRowSet
1136b 2d 3e 69 42 61 74 63 68 20 29 7b 0a 20 20 20 20 ->iBatch ){.
1136c 69 66 28 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e if( pRowSet->pEn
1136d 74 72 79 20 29 7b 0a 20 20 20 20 20 20 72 6f 77 try ){. row
1136e 53 65 74 54 6f 4c 69 73 74 28 70 52 6f 77 53 65 SetToList(pRowSe
1136f 74 29 3b 0a 20 20 20 20 20 20 70 52 6f 77 53 65 t);. pRowSe
11370 74 2d 3e 70 54 72 65 65 20 3d 20 72 6f 77 53 65 t->pTree = rowSe
11371 74 4c 69 73 74 54 6f 54 72 65 65 28 70 52 6f 77 tListToTree(pRow
11372 53 65 74 2d 3e 70 45 6e 74 72 79 29 3b 0a 20 20 Set->pEntry);.
11373 20 20 20 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e pRowSet->pEn
11374 74 72 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 try = 0;. p
11375 52 6f 77 53 65 74 2d 3e 70 4c 61 73 74 20 3d 20 RowSet->pLast =
11376 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 6f 0;. }. pRo
11377 77 53 65 74 2d 3e 69 42 61 74 63 68 20 3d 20 69 wSet->iBatch = i
11378 42 61 74 63 68 3b 0a 20 20 7d 0a 20 20 70 20 3d Batch;. }. p =
11379 20 70 52 6f 77 53 65 74 2d 3e 70 54 72 65 65 3b pRowSet->pTree;
1137a 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 . while( p ){.
1137b 20 20 20 69 66 28 20 70 2d 3e 76 3c 69 52 6f 77 if( p->v<iRow
1137c 69 64 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 id ){. p =
1137d 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 7d p->pRight;. }
1137e 65 6c 73 65 20 69 66 28 20 70 2d 3e 76 3e 69 52 else if( p->v>iR
1137f 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 70 20 owid ){. p
11380 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 = p->pLeft;.
11381 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 }else{. ret
11382 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d urn 1;. }. }
11383 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
11384 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
11385 45 6e 64 20 6f 66 20 72 6f 77 73 65 74 2e 63 20 End of rowset.c
11386 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11387 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11388 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
11389 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1138a 42 65 67 69 6e 20 66 69 6c 65 20 70 61 67 65 72 Begin file pager
1138b 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1138c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1138d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1138e 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
1138f 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
11390 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
11391 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
11392 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
11393 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
11394 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
11395 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
11396 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
11397 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
11398 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
11399 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
1139a 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
1139b 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
1139c 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
1139d 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
1139e 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
1139f 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
113a0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
113a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113a5 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 ******.** This i
113a6 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 s the implementa
113a7 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 65 tion of the page
113a8 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d cache subsystem
113a9 20 6f 72 20 22 70 61 67 65 72 22 2e 0a 2a 2a 20 or "pager"..**
113aa 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 69 73 .** The pager is
113ab 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 used to access
113ac 61 20 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 a database disk
113ad 66 69 6c 65 2e 20 20 49 74 20 69 6d 70 6c 65 6d file. It implem
113ae 65 6e 74 73 0a 2a 2a 20 61 74 6f 6d 69 63 20 63 ents.** atomic c
113af 6f 6d 6d 69 74 20 61 6e 64 20 72 6f 6c 6c 62 61 ommit and rollba
113b0 63 6b 20 74 68 72 6f 75 67 68 20 74 68 65 20 75 ck through the u
113b1 73 65 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 se of a journal
113b2 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 file that.** is
113b3 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68 separate from th
113b4 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
113b5 20 20 54 68 65 20 70 61 67 65 72 20 61 6c 73 6f The pager also
113b6 20 69 6d 70 6c 65 6d 65 6e 74 73 20 66 69 6c 65 implements file
113b7 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 70 .** locking to p
113b8 72 65 76 65 6e 74 20 74 77 6f 20 70 72 6f 63 65 revent two proce
113b9 73 73 65 73 20 66 72 6f 6d 20 77 72 69 74 69 6e sses from writin
113ba 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 g the same datab
113bb 61 73 65 0a 2a 2a 20 66 69 6c 65 20 73 69 6d 75 ase.** file simu
113bc 6c 74 61 6e 65 6f 75 73 6c 79 2c 20 6f 72 20 6f ltaneously, or o
113bd 6e 65 20 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 ne process from
113be 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 reading the data
113bf 62 61 73 65 20 77 68 69 6c 65 0a 2a 2a 20 61 6e base while.** an
113c0 6f 74 68 65 72 20 69 73 20 77 72 69 74 69 6e 67 other is writing
113c1 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 ..**.** @(#) $Id
113c2 3a 20 70 61 67 65 72 2e 63 2c 76 20 31 2e 36 32 : pager.c,v 1.62
113c3 39 20 32 30 30 39 2f 30 38 2f 31 30 20 31 37 3a 9 2009/08/10 17:
113c4 34 38 3a 35 37 20 64 72 68 20 45 78 70 20 24 0a 48:57 drh Exp $.
113c5 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
113c6 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 0a 2f E_OMIT_DISKIO../
113c7 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 *.** Macros for
113c8 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e 67 2e troubleshooting.
113c9 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 Normally turne
113ca 64 20 6f 66 66 0a 2a 2f 0a 23 69 66 20 30 0a 69 d off.*/.#if 0.i
113cb 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 nt sqlite3PagerT
113cc 72 61 63 65 3d 31 3b 20 20 2f 2a 20 54 72 75 65 race=1; /* True
113cd 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 61 63 69 to enable traci
113ce 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 ng */.#define sq
113cf 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
113d0 20 70 72 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 printf.#define
113d1 50 41 47 45 52 54 52 41 43 45 28 58 29 20 20 20 PAGERTRACE(X)
113d2 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 if( sqlite3Pag
113d3 65 72 54 72 61 63 65 20 29 7b 20 73 71 6c 69 74 erTrace ){ sqlit
113d4 65 33 44 65 62 75 67 50 72 69 6e 74 66 20 58 3b e3DebugPrintf X;
113d5 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 }.#else.#define
113d6 20 50 41 47 45 52 54 52 41 43 45 28 58 29 0a 23 PAGERTRACE(X).#
113d7 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
113d8 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 6d following two m
113d9 61 63 72 6f 73 20 61 72 65 20 75 73 65 64 20 77 acros are used w
113da 69 74 68 69 6e 20 74 68 65 20 50 41 47 45 52 54 ithin the PAGERT
113db 52 41 43 45 28 29 20 6d 61 63 72 6f 73 20 61 62 RACE() macros ab
113dc 6f 76 65 0a 2a 2a 20 74 6f 20 70 72 69 6e 74 20 ove.** to print
113dd 6f 75 74 20 66 69 6c 65 2d 64 65 73 63 72 69 70 out file-descrip
113de 74 6f 72 73 2e 20 0a 2a 2a 0a 2a 2a 20 50 41 47 tors. .**.** PAG
113df 45 52 49 44 28 29 20 74 61 6b 65 73 20 61 20 70 ERID() takes a p
113e0 6f 69 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65 ointer to a Page
113e1 72 20 73 74 72 75 63 74 20 61 73 20 69 74 73 20 r struct as its
113e2 61 72 67 75 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a argument. The.**
113e3 20 61 73 73 6f 63 69 61 74 65 64 20 66 69 6c 65 associated file
113e4 2d 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 72 -descriptor is r
113e5 65 74 75 72 6e 65 64 2e 20 46 49 4c 45 48 41 4e eturned. FILEHAN
113e6 44 4c 45 49 44 28 29 20 74 61 6b 65 73 20 61 6e DLEID() takes an
113e7 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 0a 2a 2a sqlite3_file.**
113e8 20 73 74 72 75 63 74 20 61 73 20 69 74 73 20 61 struct as its a
113e9 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 rgument..*/.#def
113ea 69 6e 65 20 50 41 47 45 52 49 44 28 70 29 20 28 ine PAGERID(p) (
113eb 28 69 6e 74 29 28 70 2d 3e 66 64 29 29 0a 23 64 (int)(p->fd)).#d
113ec 65 66 69 6e 65 20 46 49 4c 45 48 41 4e 44 4c 45 efine FILEHANDLE
113ed 49 44 28 66 64 29 20 28 28 69 6e 74 29 66 64 29 ID(fd) ((int)fd)
113ee 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 ../*.** The page
113ef 20 63 61 63 68 65 20 61 73 20 61 20 77 68 6f 6c cache as a whol
113f0 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 20 6f e is always in o
113f1 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ne of the follow
113f2 69 6e 67 0a 2a 2a 20 73 74 61 74 65 73 3a 0a 2a ing.** states:.*
113f3 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 55 4e 4c *.** PAGER_UNL
113f4 4f 43 4b 20 20 20 20 20 20 20 20 54 68 65 20 70 OCK The p
113f5 61 67 65 20 63 61 63 68 65 20 69 73 20 6e 6f 74 age cache is not
113f6 20 63 75 72 72 65 6e 74 6c 79 20 72 65 61 64 69 currently readi
113f7 6e 67 20 6f 72 20 0a 2a 2a 20 20 20 20 20 20 20 ng or .**
113f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
113f9 77 72 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 writing the data
113fa 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 72 base file. Ther
113fb 65 20 69 73 20 6e 6f 0a 2a 2a 20 20 20 20 20 20 e is no.**
113fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
113fd 20 64 61 74 61 20 68 65 6c 64 20 69 6e 20 6d 65 data held in me
113fe 6d 6f 72 79 2e 20 20 54 68 69 73 20 69 73 20 74 mory. This is t
113ff 68 65 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 20 20 he initial.**
11400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11401 20 20 20 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a state..**.**
11402 20 20 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 PAGER_SHARED
11403 20 20 20 20 20 20 20 54 68 65 20 70 61 67 65 20 The page
11404 63 61 63 68 65 20 69 73 20 72 65 61 64 69 6e 67 cache is reading
11405 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a the database..*
11406 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
11407 20 20 20 20 20 20 20 20 57 72 69 74 69 6e 67 20 Writing
11408 69 73 20 6e 6f 74 20 70 65 72 6d 69 74 74 65 64 is not permitted
11409 2e 20 20 54 68 65 72 65 20 63 61 6e 20 62 65 0a . There can be.
1140a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
1140b 20 20 20 20 20 20 20 20 20 6d 75 6c 74 69 70 6c multipl
1140c 65 20 72 65 61 64 65 72 73 20 61 63 63 65 73 73 e readers access
1140d 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 ing the same dat
1140e 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 abase.**
1140f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 f
11410 69 6c 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 ile at the same
11411 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 time..**.** PA
11412 47 45 52 5f 52 45 53 45 52 56 45 44 20 20 20 20 GER_RESERVED
11413 20 20 54 68 69 73 20 70 72 6f 63 65 73 73 20 68 This process h
11414 61 73 20 72 65 73 65 72 76 65 64 20 74 68 65 20 as reserved the
11415 64 61 74 61 62 61 73 65 20 66 6f 72 20 77 72 69 database for wri
11416 74 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 ting.**
11417 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75 bu
11418 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 6d 61 t has not yet ma
11419 64 65 20 61 6e 79 20 63 68 61 6e 67 65 73 2e 20 de any changes.
1141a 20 4f 6e 6c 79 20 6f 6e 65 20 70 72 6f 63 65 73 Only one proces
1141b 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 s.**
1141c 20 20 20 20 20 20 20 20 20 20 20 61 74 20 61 20 at a
1141d 74 69 6d 65 20 63 61 6e 20 72 65 73 65 72 76 65 time can reserve
1141e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 the database.
1141f 54 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 The original.**
11420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11421 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20 66 database f
11422 69 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e ile has not been
11423 20 6d 6f 64 69 66 69 65 64 20 73 6f 20 6f 74 68 modified so oth
11424 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 er.**
11425 20 20 20 20 20 20 20 20 20 20 20 20 70 72 6f 63 proc
11426 65 73 73 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 esses may still
11427 62 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 6f be reading the o
11428 6e 2d 64 69 73 6b 0a 2a 2a 20 20 20 20 20 20 20 n-disk.**
11429 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1142a 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
1142b 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 45 58 43 *.** PAGER_EXC
1142c 4c 55 53 49 56 45 20 20 20 20 20 54 68 65 20 70 LUSIVE The p
1142d 61 67 65 20 63 61 63 68 65 20 69 73 20 77 72 69 age cache is wri
1142e 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 ting the databas
1142f 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 e..**
11430 20 20 20 20 20 20 20 20 20 20 20 20 41 63 63 65 Acce
11431 73 73 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e ss is exclusive.
11432 20 20 4e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 No other proce
11433 73 73 65 73 20 6f 72 0a 2a 2a 20 20 20 20 20 20 sses or.**
11434 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11435 20 74 68 72 65 61 64 73 20 63 61 6e 20 62 65 20 threads can be
11436 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 reading or writi
11437 6e 67 20 77 68 69 6c 65 20 6f 6e 65 0a 2a 2a 20 ng while one.**
11438 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11439 20 20 20 20 20 20 70 72 6f 63 65 73 73 20 69 73 process is
1143a 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 writing..**.**
1143b 20 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20 PAGER_SYNCED
1143c 20 20 20 20 20 20 54 68 65 20 70 61 67 65 72 20 The pager
1143d 6d 6f 76 65 73 20 74 6f 20 74 68 69 73 20 73 74 moves to this st
1143e 61 74 65 20 66 72 6f 6d 20 50 41 47 45 52 5f 45 ate from PAGER_E
1143f 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 XCLUSIVE.**
11440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11441 20 20 61 66 74 65 72 20 61 6c 6c 20 64 69 72 74 after all dirt
11442 79 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 y pages have bee
11443 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 n written to the
11444 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
11445 20 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61 databa
11446 73 65 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 se file and the
11447 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 73 79 file has been sy
11448 6e 63 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 nced to.**
11449 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1144a 20 64 69 73 6b 2e 20 41 6c 6c 20 74 68 61 74 20 disk. All that
1144b 72 65 6d 61 69 6e 73 20 74 6f 20 64 6f 20 69 73 remains to do is
1144c 20 74 6f 20 72 65 6d 6f 76 65 20 6f 72 0a 2a 2a to remove or.**
1144d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1144e 20 20 20 20 20 20 20 74 72 75 6e 63 61 74 65 20 truncate
1144f 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11450 20 61 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 and the transac
11451 74 69 6f 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 tion .**
11452 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 w
11453 69 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 ill be committed
11454 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 ..**.** The page
11455 20 63 61 63 68 65 20 63 6f 6d 65 73 20 75 70 20 cache comes up
11456 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e in PAGER_UNLOCK.
11457 20 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 The first time
11458 20 61 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 a.** sqlite3Pag
11459 65 72 47 65 74 28 29 20 6f 63 63 75 72 73 2c 20 erGet() occurs,
1145a 74 68 65 20 73 74 61 74 65 20 74 72 61 6e 73 69 the state transi
1145b 74 69 6f 6e 73 20 74 6f 20 50 41 47 45 52 5f 53 tions to PAGER_S
1145c 48 41 52 45 44 2e 0a 2a 2a 20 41 66 74 65 72 20 HARED..** After
1145d 61 6c 6c 20 70 61 67 65 73 20 68 61 76 65 20 62 all pages have b
1145e 65 65 6e 20 72 65 6c 65 61 73 65 64 20 75 73 69 een released usi
1145f 6e 67 20 73 71 6c 69 74 65 5f 70 61 67 65 5f 75 ng sqlite_page_u
11460 6e 72 65 66 28 29 2c 0a 2a 2a 20 74 68 65 20 73 nref(),.** the s
11461 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 tate transitions
11462 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 55 back to PAGER_U
11463 4e 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 73 NLOCK. The firs
11464 74 20 74 69 6d 65 0a 2a 2a 20 74 68 61 74 20 73 t time.** that s
11465 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
11466 28 29 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 () is called, th
11467 65 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 e state transiti
11468 6f 6e 73 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f ons to.** PAGER_
11469 52 45 53 45 52 56 45 44 2e 20 20 28 4e 6f 74 65 RESERVED. (Note
1146a 20 74 68 61 74 20 73 71 6c 69 74 65 33 50 61 67 that sqlite3Pag
1146b 65 72 57 72 69 74 65 28 29 20 63 61 6e 20 6f 6e erWrite() can on
1146c 6c 79 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 ly be.** called
1146d 6f 6e 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e on an outstandin
1146e 67 20 70 61 67 65 20 77 68 69 63 68 20 6d 65 61 g page which mea
1146f 6e 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65 ns that the page
11470 72 20 6d 75 73 74 0a 2a 2a 20 62 65 20 69 6e 20 r must.** be in
11471 50 41 47 45 52 5f 53 48 41 52 45 44 20 62 65 66 PAGER_SHARED bef
11472 6f 72 65 20 69 74 20 74 72 61 6e 73 69 74 69 6f ore it transitio
11473 6e 73 20 74 6f 20 50 41 47 45 52 5f 52 45 53 45 ns to PAGER_RESE
11474 52 56 45 44 2e 29 0a 2a 2a 20 50 41 47 45 52 5f RVED.).** PAGER_
11475 52 45 53 45 52 56 45 44 20 6d 65 61 6e 73 20 74 RESERVED means t
11476 68 61 74 20 74 68 65 72 65 20 69 73 20 61 6e 20 hat there is an
11477 6f 70 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f open rollback jo
11478 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 74 72 urnal..** The tr
11479 61 6e 73 69 74 69 6f 6e 20 74 6f 20 50 41 47 45 ansition to PAGE
1147a 52 5f 45 58 43 4c 55 53 49 56 45 20 6f 63 63 75 R_EXCLUSIVE occu
1147b 72 73 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68 rs before any ch
1147c 61 6e 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 anges.** are mad
1147d 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 e to the databas
1147e 65 20 66 69 6c 65 2c 20 74 68 6f 75 67 68 20 77 e file, though w
1147f 72 69 74 65 73 20 74 6f 20 74 68 65 20 72 6f 6c rites to the rol
11480 6c 62 61 63 6b 0a 2a 2a 20 6a 6f 75 72 6e 61 6c lback.** journal
11481 20 6f 63 63 75 72 73 20 77 69 74 68 20 6a 75 73 occurs with jus
11482 74 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 t PAGER_RESERVED
11483 2e 20 20 41 66 74 65 72 20 61 6e 20 73 71 6c 69 . After an sqli
11484 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
11485 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 ().** or sqlite3
11486 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 PagerCommitPhase
11487 54 77 6f 28 29 2c 20 74 68 65 20 73 74 61 74 65 Two(), the state
11488 20 63 61 6e 20 67 6f 20 62 61 63 6b 20 74 6f 20 can go back to
11489 50 41 47 45 52 5f 53 48 41 52 45 44 2c 0a 2a 2a PAGER_SHARED,.**
1148a 20 6f 72 20 69 74 20 63 61 6e 20 73 74 61 79 20 or it can stay
1148b 61 74 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 at PAGER_EXCLUSI
1148c 56 45 20 69 66 20 77 65 20 61 72 65 20 69 6e 20 VE if we are in
1148d 65 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 exclusive access
1148e 20 6d 6f 64 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e mode..*/.#defin
1148f 65 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 e PAGER_UNLOCK
11490 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41 0.#define PA
11491 47 45 52 5f 53 48 41 52 45 44 20 20 20 20 20 20 GER_SHARED
11492 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 53 1 /* same as S
11493 48 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 HARED_LOCK */.#d
11494 65 66 69 6e 65 20 50 41 47 45 52 5f 52 45 53 45 efine PAGER_RESE
11495 52 56 45 44 20 20 20 20 32 20 20 20 2f 2a 20 73 RVED 2 /* s
11496 61 6d 65 20 61 73 20 52 45 53 45 52 56 45 44 5f ame as RESERVED_
11497 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 LOCK */.#define
11498 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 PAGER_EXCLUSIVE
11499 20 20 34 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 4 /* same as
1149a 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 EXCLUSIVE_LOCK
1149b 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 */.#define PAGER
1149c 5f 53 59 4e 43 45 44 20 20 20 20 20 20 35 0a 0a _SYNCED 5..
1149d 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 75 73 /*.** A macro us
1149e 65 64 20 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 ed for invoking
1149f 74 68 65 20 63 6f 64 65 63 20 69 66 20 74 68 65 the codec if the
114a0 72 65 20 69 73 20 6f 6e 65 0a 2a 2f 0a 23 69 66 re is one.*/.#if
114a1 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 def SQLITE_HAS_C
114a2 4f 44 45 43 0a 23 20 64 65 66 69 6e 65 20 43 4f ODEC.# define CO
114a3 44 45 43 31 28 50 2c 44 2c 4e 2c 58 2c 45 29 20 DEC1(P,D,N,X,E)
114a4 5c 0a 20 20 20 20 69 66 28 20 50 2d 3e 78 43 6f \. if( P->xCo
114a5 64 65 63 20 26 26 20 50 2d 3e 78 43 6f 64 65 63 dec && P->xCodec
114a6 28 50 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e 2c 58 (P->pCodec,D,N,X
114a7 29 3d 3d 30 20 29 7b 20 45 3b 20 7d 0a 23 20 64 )==0 ){ E; }.# d
114a8 65 66 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 efine CODEC2(P,D
114a9 2c 4e 2c 58 2c 45 2c 4f 29 20 5c 0a 20 20 20 20 ,N,X,E,O) \.
114aa 69 66 28 20 50 2d 3e 78 43 6f 64 65 63 3d 3d 30 if( P->xCodec==0
114ab 20 29 7b 20 4f 3d 28 63 68 61 72 2a 29 44 3b 20 ){ O=(char*)D;
114ac 7d 65 6c 73 65 20 5c 0a 20 20 20 20 69 66 28 20 }else \. if(
114ad 28 4f 3d 28 63 68 61 72 2a 29 28 50 2d 3e 78 43 (O=(char*)(P->xC
114ae 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 2c 44 odec(P->pCodec,D
114af 2c 4e 2c 58 29 29 29 3d 3d 30 20 29 7b 20 45 3b ,N,X)))==0 ){ E;
114b0 20 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e }.#else.# defin
114b1 65 20 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58 e CODEC1(P,D,N,X
114b2 2c 45 29 20 20 20 2f 2a 20 4e 4f 2d 4f 50 20 2a ,E) /* NO-OP *
114b3 2f 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 /.# define CODEC
114b4 32 28 50 2c 44 2c 4e 2c 58 2c 45 2c 4f 29 20 4f 2(P,D,N,X,E,O) O
114b5 3d 28 63 68 61 72 2a 29 44 0a 23 65 6e 64 69 66 =(char*)D.#endif
114b6 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 ../*.** The maxi
114b7 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 65 63 74 mum allowed sect
114b8 6f 72 20 73 69 7a 65 2e 20 36 34 4b 69 42 2e 20 or size. 64KiB.
114b9 49 66 20 74 68 65 20 78 53 65 63 74 6f 72 73 69 If the xSectorsi
114ba 7a 65 28 29 20 6d 65 74 68 6f 64 20 0a 2a 2a 20 ze() method .**
114bb 72 65 74 75 72 6e 73 20 61 20 76 61 6c 75 65 20 returns a value
114bc 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 73 larger than this
114bd 2c 20 74 68 65 6e 20 4d 41 58 5f 53 45 43 54 4f , then MAX_SECTO
114be 52 5f 53 49 5a 45 20 69 73 20 75 73 65 64 20 69 R_SIZE is used i
114bf 6e 73 74 65 61 64 2e 0a 2a 2a 20 54 68 69 73 20 nstead..** This
114c0 63 6f 75 6c 64 20 63 6f 6e 63 65 69 76 61 62 6c could conceivabl
114c1 79 20 63 61 75 73 65 20 63 6f 72 72 75 70 74 69 y cause corrupti
114c2 6f 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 70 on following a p
114c3 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f 6e 0a ower failure on.
114c4 2a 2a 20 73 75 63 68 20 61 20 73 79 73 74 65 6d ** such a system
114c5 2e 20 54 68 69 73 20 69 73 20 63 75 72 72 65 6e . This is curren
114c6 74 6c 79 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e tly an undocumen
114c7 74 65 64 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64 ted limit..*/.#d
114c8 65 66 69 6e 65 20 4d 41 58 5f 53 45 43 54 4f 52 efine MAX_SECTOR
114c9 5f 53 49 5a 45 20 30 78 31 30 30 30 30 0a 0a 2f _SIZE 0x10000../
114ca 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
114cb 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
114cc 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 g structure is a
114cd 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 llocated for eac
114ce 68 20 61 63 74 69 76 65 0a 2a 2a 20 73 61 76 65 h active.** save
114cf 70 6f 69 6e 74 20 61 6e 64 20 73 74 61 74 65 6d point and statem
114d0 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
114d1 69 6e 20 74 68 65 20 73 79 73 74 65 6d 2e 20 41 in the system. A
114d2 6c 6c 20 73 75 63 68 20 73 74 72 75 63 74 75 72 ll such structur
114d3 65 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 es.** are stored
114d4 20 69 6e 20 74 68 65 20 50 61 67 65 72 2e 61 53 in the Pager.aS
114d5 61 76 65 70 6f 69 6e 74 5b 5d 20 61 72 72 61 79 avepoint[] array
114d6 2c 20 77 68 69 63 68 20 69 73 20 61 6c 6c 6f 63 , which is alloc
114d7 61 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65 73 69 ated and.** resi
114d8 7a 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 zed using sqlite
114d9 33 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 3Realloc()..**.*
114da 2a 20 57 68 65 6e 20 61 20 73 61 76 65 70 6f 69 * When a savepoi
114db 6e 74 20 69 73 20 63 72 65 61 74 65 64 2c 20 74 nt is created, t
114dc 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e he PagerSavepoin
114dd 74 2e 69 48 64 72 4f 66 66 73 65 74 20 66 69 65 t.iHdrOffset fie
114de 6c 64 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 ld is.** set to
114df 30 2e 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 2d 0. If a journal-
114e0 68 65 61 64 65 72 20 69 73 20 77 72 69 74 74 65 header is writte
114e1 6e 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 n into the main
114e2 6a 6f 75 72 6e 61 6c 20 77 68 69 6c 65 0a 2a 2a journal while.**
114e3 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 69 the savepoint i
114e4 73 20 61 63 74 69 76 65 2c 20 74 68 65 6e 20 69 s active, then i
114e5 48 64 72 4f 66 66 73 65 74 20 69 73 20 73 65 74 HdrOffset is set
114e6 20 74 6f 20 74 68 65 20 62 79 74 65 20 6f 66 66 to the byte off
114e7 73 65 74 20 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 set .** immediat
114e8 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 ely following th
114e9 65 20 6c 61 73 74 20 6a 6f 75 72 6e 61 6c 20 72 e last journal r
114ea 65 63 6f 72 64 20 77 72 69 74 74 65 6e 20 69 6e ecord written in
114eb 74 6f 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a to the main.** j
114ec 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 20 74 68 ournal before th
114ed 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 e journal-header
114ee 2e 20 54 68 69 73 20 69 73 20 72 65 71 75 69 72 . This is requir
114ef 65 64 20 64 75 72 69 6e 67 20 73 61 76 65 70 6f ed during savepo
114f0 69 6e 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 int.** rollback
114f1 28 73 65 65 20 70 61 67 65 72 50 6c 61 79 62 61 (see pagerPlayba
114f2 63 6b 53 61 76 65 70 6f 69 6e 74 28 29 29 2e 0a ckSavepoint())..
114f3 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
114f4 74 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 t PagerSavepoint
114f5 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 3b PagerSavepoint;
114f6 0a 73 74 72 75 63 74 20 50 61 67 65 72 53 61 76 .struct PagerSav
114f7 65 70 6f 69 6e 74 20 7b 0a 20 20 69 36 34 20 69 epoint {. i64 i
114f8 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 Offset;
114f9 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 /* Start
114fa 69 6e 67 20 6f 66 66 73 65 74 20 69 6e 20 6d 61 ing offset in ma
114fb 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 in journal */.
114fc 69 36 34 20 69 48 64 72 4f 66 66 73 65 74 3b 20 i64 iHdrOffset;
114fd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
114fe 53 65 65 20 61 62 6f 76 65 20 2a 2f 0a 20 20 42 See above */. B
114ff 69 74 76 65 63 20 2a 70 49 6e 53 61 76 65 70 6f itvec *pInSavepo
11500 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 int; /* S
11501 65 74 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 et of pages in t
11502 68 69 73 20 73 61 76 65 70 6f 69 6e 74 20 2a 2f his savepoint */
11503 0a 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 20 . Pgno nOrig;
11504 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11505 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e 75 6d 62 /* Original numb
11506 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 66 er of pages in f
11507 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 53 ile */. Pgno iS
11508 75 62 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 ubRec;
11509 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
1150a 66 20 66 69 72 73 74 20 72 65 63 6f 72 64 20 69 f first record i
1150b 6e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f n sub-journal */
1150c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6f 70 65 .};../*.** A ope
1150d 6e 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 n page cache is
1150e 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
1150f 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
11510 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 65 72 ucture..**.** er
11511 72 43 6f 64 65 0a 2a 2a 0a 2a 2a 20 20 20 50 61 rCode.**.** Pa
11512 67 65 72 2e 65 72 72 43 6f 64 65 20 6d 61 79 20 ger.errCode may
11513 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 be set to SQLITE
11514 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 _IOERR, SQLITE_C
11515 4f 52 52 55 50 54 2c 20 6f 72 0a 2a 2a 20 20 20 ORRUPT, or.**
11516 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 20 or SQLITE_FULL.
11517 4f 6e 63 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 Once one of the
11518 66 69 72 73 74 20 74 68 72 65 65 20 65 72 72 6f first three erro
11519 72 73 20 6f 63 63 75 72 73 2c 20 69 74 20 70 65 rs occurs, it pe
1151a 72 73 69 73 74 73 0a 2a 2a 20 20 20 61 6e 64 20 rsists.** and
1151b 69 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 74 is returned as t
1151c 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65 76 65 he result of eve
1151d 72 79 20 6d 61 6a 6f 72 20 70 61 67 65 72 20 41 ry major pager A
1151e 50 49 20 63 61 6c 6c 2e 20 20 54 68 65 0a 2a 2a PI call. The.**
1151f 20 20 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 72 SQLITE_FULL r
11520 65 74 75 72 6e 20 63 6f 64 65 20 69 73 20 73 6c eturn code is sl
11521 69 67 68 74 6c 79 20 64 69 66 66 65 72 65 6e 74 ightly different
11522 2e 20 49 74 20 70 65 72 73 69 73 74 73 20 6f 6e . It persists on
11523 6c 79 20 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20 ly until the.**
11524 20 20 6e 65 78 74 20 73 75 63 63 65 73 73 66 75 next successfu
11525 6c 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 70 65 l rollback is pe
11526 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65 20 70 rformed on the p
11527 61 67 65 72 20 63 61 63 68 65 2e 20 41 6c 73 6f ager cache. Also
11528 2c 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46 55 ,.** SQLITE_FU
11529 4c 4c 20 64 6f 65 73 20 6e 6f 74 20 61 66 66 65 LL does not affe
1152a 63 74 20 74 68 65 20 73 71 6c 69 74 65 33 50 61 ct the sqlite3Pa
1152b 67 65 72 47 65 74 28 29 20 61 6e 64 20 73 71 6c gerGet() and sql
1152c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 ite3PagerLookup(
1152d 29 0a 2a 2a 20 20 20 41 50 49 73 2c 20 74 68 65 ).** APIs, the
1152e 79 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 75 y may still be u
1152f 73 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 sed successfully
11530 2e 0a 2a 2a 0a 2a 2a 20 64 62 53 69 7a 65 56 61 ..**.** dbSizeVa
11531 6c 69 64 2c 20 64 62 53 69 7a 65 2c 20 64 62 4f lid, dbSize, dbO
11532 72 69 67 53 69 7a 65 2c 20 64 62 46 69 6c 65 53 rigSize, dbFileS
11533 69 7a 65 0a 2a 2a 0a 2a 2a 20 20 20 4d 61 6e 61 ize.**.** Mana
11534 67 69 6e 67 20 74 68 65 20 73 69 7a 65 20 6f 66 ging the size of
11535 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
11536 6c 65 20 69 6e 20 70 61 67 65 73 20 69 73 20 61 le in pages is a
11537 20 6c 69 74 74 6c 65 20 63 6f 6d 70 6c 69 63 61 little complica
11538 74 65 64 2e 0a 2a 2a 20 20 20 54 68 65 20 76 61 ted..** The va
11539 72 69 61 62 6c 65 20 50 61 67 65 72 2e 64 62 53 riable Pager.dbS
1153a 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ize contains the
1153b 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
1153c 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 that the databa
1153d 73 65 0a 2a 2a 20 20 20 69 6d 61 67 65 20 63 75 se.** image cu
1153e 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 rrently contains
1153f 2e 20 41 73 20 74 68 65 20 64 61 74 61 62 61 73 . As the databas
11540 65 20 69 6d 61 67 65 20 67 72 6f 77 73 20 6f 72 e image grows or
11541 20 73 68 72 69 6e 6b 73 20 74 68 69 73 0a 2a 2a shrinks this.**
11542 20 20 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 variable is u
11543 70 64 61 74 65 64 2e 20 54 68 65 20 76 61 72 69 pdated. The vari
11544 61 62 6c 65 20 50 61 67 65 72 2e 64 62 46 69 6c able Pager.dbFil
11545 65 53 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74 eSize contains t
11546 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 6f he number.** o
11547 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 f pages in the d
11548 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 atabase file. Th
11549 69 73 20 6d 61 79 20 62 65 20 64 69 66 66 65 72 is may be differ
1154a 65 6e 74 20 66 72 6f 6d 20 50 61 67 65 72 2e 64 ent from Pager.d
1154b 62 53 69 7a 65 0a 2a 2a 20 20 20 69 66 20 73 6f bSize.** if so
1154c 6d 65 20 70 61 67 65 73 20 68 61 76 65 20 62 65 me pages have be
1154d 65 6e 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 en appended to t
1154e 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 he database imag
1154f 65 20 62 75 74 20 6e 6f 74 20 79 65 74 20 77 72 e but not yet wr
11550 69 74 74 65 6e 0a 2a 2a 20 20 20 6f 75 74 20 66 itten.** out f
11551 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 74 6f rom the cache to
11552 20 74 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 the actual file
11553 20 6f 6e 20 64 69 73 6b 2e 20 4f 72 20 69 66 20 on disk. Or if
11554 74 68 65 20 69 6d 61 67 65 20 68 61 73 20 62 65 the image has be
11555 65 6e 0a 2a 2a 20 20 20 74 72 75 6e 63 61 74 65 en.** truncate
11556 64 20 62 79 20 61 6e 20 69 6e 63 72 65 6d 65 6e d by an incremen
11557 74 61 6c 2d 76 61 63 75 75 6d 20 6f 70 65 72 61 tal-vacuum opera
11558 74 69 6f 6e 2e 20 54 68 65 20 50 61 67 65 72 2e tion. The Pager.
11559 64 62 4f 72 69 67 53 69 7a 65 20 76 61 72 69 61 dbOrigSize varia
1155a 62 6c 65 0a 2a 2a 20 20 20 63 6f 6e 74 61 69 6e ble.** contain
1155b 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
1155c 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 pages in the dat
1155d 61 62 61 73 65 20 69 6d 61 67 65 20 77 68 65 6e abase image when
1155e 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 the current.**
1155f 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 transaction wa
11560 73 20 6f 70 65 6e 65 64 2e 20 54 68 65 20 63 6f s opened. The co
11561 6e 74 65 6e 74 73 20 6f 66 20 61 6c 6c 20 74 68 ntents of all th
11562 72 65 65 20 6f 66 20 74 68 65 73 65 20 76 61 72 ree of these var
11563 69 61 62 6c 65 73 20 69 73 0a 2a 2a 20 20 20 6f iables is.** o
11564 6e 6c 79 20 67 75 61 72 61 6e 74 65 65 64 20 74 nly guaranteed t
11565 6f 20 62 65 20 63 6f 72 72 65 63 74 20 69 66 20 o be correct if
11566 74 68 65 20 62 6f 6f 6c 65 61 6e 20 50 61 67 65 the boolean Page
11567 72 2e 64 62 53 69 7a 65 56 61 6c 69 64 20 69 73 r.dbSizeValid is
11568 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 true..**.** T
11569 4f 44 4f 3a 20 55 6e 64 65 72 20 77 68 61 74 20 ODO: Under what
1156a 63 6f 6e 64 69 74 69 6f 6e 73 20 69 73 20 64 62 conditions is db
1156b 53 69 7a 65 56 61 6c 69 64 20 73 65 74 3f 20 43 SizeValid set? C
1156c 6c 65 61 72 65 64 3f 0a 2a 2a 0a 2a 2a 20 63 68 leared?.**.** ch
1156d 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 0a 2a 2a angeCountDone.**
1156e 0a 2a 2a 20 20 20 54 68 69 73 20 62 6f 6f 6c 65 .** This boole
1156f 61 6e 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 an variable is u
11570 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 sed to make sure
11571 20 74 68 61 74 20 74 68 65 20 63 68 61 6e 67 65 that the change
11572 2d 63 6f 75 6e 74 65 72 20 0a 2a 2a 20 20 20 28 -counter .** (
11573 74 68 65 20 34 2d 62 79 74 65 20 68 65 61 64 65 the 4-byte heade
11574 72 20 66 69 65 6c 64 20 61 74 20 62 79 74 65 20 r field at byte
11575 6f 66 66 73 65 74 20 32 34 20 6f 66 20 74 68 65 offset 24 of the
11576 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 29 20 database file)
11577 69 73 20 0a 2a 2a 20 20 20 6e 6f 74 20 75 70 64 is .** not upd
11578 61 74 65 64 20 6d 6f 72 65 20 6f 66 74 65 6e 20 ated more often
11579 74 68 61 6e 20 6e 65 63 65 73 73 61 72 79 2e 20 than necessary.
1157a 0a 2a 2a 0a 2a 2a 20 20 20 49 74 20 69 73 20 73 .**.** It is s
1157b 65 74 20 74 6f 20 74 72 75 65 20 77 68 65 6e 20 et to true when
1157c 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
1157d 65 72 20 66 69 65 6c 64 20 69 73 20 75 70 64 61 er field is upda
1157e 74 65 64 2c 20 77 68 69 63 68 20 0a 2a 2a 20 20 ted, which .**
1157f 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e can only happen
11580 20 69 66 20 61 6e 20 65 78 63 6c 75 73 69 76 65 if an exclusive
11581 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e lock is held on
11582 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
11583 6c 65 2e 0a 2a 2a 20 20 20 49 74 20 69 73 20 63 le..** It is c
11584 6c 65 61 72 65 64 20 28 73 65 74 20 74 6f 20 66 leared (set to f
11585 61 6c 73 65 29 20 77 68 65 6e 65 76 65 72 20 61 alse) whenever a
11586 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
11587 20 69 73 20 0a 2a 2a 20 20 20 72 65 6c 69 6e 71 is .** relinq
11588 75 69 73 68 65 64 20 6f 6e 20 74 68 65 20 64 61 uished on the da
11589 74 61 62 61 73 65 20 66 69 6c 65 2e 20 45 61 63 tabase file. Eac
1158a 68 20 74 69 6d 65 20 61 20 74 72 61 6e 73 61 63 h time a transac
1158b 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 tion is committe
1158c 64 2c 0a 2a 2a 20 20 20 54 68 65 20 63 68 61 6e d,.** The chan
1158d 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 geCountDone flag
1158e 20 69 73 20 69 6e 73 70 65 63 74 65 64 2e 20 49 is inspected. I
1158f 66 20 69 74 20 69 73 20 74 72 75 65 2c 20 74 68 f it is true, th
11590 65 20 77 6f 72 6b 20 6f 66 0a 2a 2a 20 20 20 75 e work of.** u
11591 70 64 61 74 69 6e 67 20 74 68 65 20 63 68 61 6e pdating the chan
11592 67 65 2d 63 6f 75 6e 74 65 72 20 69 73 20 6f 6d ge-counter is om
11593 69 74 74 65 64 20 66 6f 72 20 74 68 65 20 63 75 itted for the cu
11594 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f rrent transactio
11595 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 n..**.** This
11596 6d 65 63 68 61 6e 69 73 6d 20 6d 65 61 6e 73 20 mechanism means
11597 74 68 61 74 20 77 68 65 6e 20 72 75 6e 6e 69 6e that when runnin
11598 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d g in exclusive m
11599 6f 64 65 2c 20 61 20 63 6f 6e 6e 65 63 74 69 6f ode, a connectio
1159a 6e 20 0a 2a 2a 20 20 20 6e 65 65 64 20 6f 6e 6c n .** need onl
1159b 79 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61 y update the cha
1159c 6e 67 65 2d 63 6f 75 6e 74 65 72 20 6f 6e 63 65 nge-counter once
1159d 2c 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 , for the first
1159e 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 transaction.**
1159f 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a committed..**.*
115a0 2a 20 64 62 4d 6f 64 69 66 69 65 64 0a 2a 2a 0a * dbModified.**.
115a1 2a 2a 20 20 20 54 68 65 20 64 62 4d 6f 64 69 66 ** The dbModif
115a2 69 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 ied flag is set
115a3 77 68 65 6e 65 76 65 72 20 61 20 64 61 74 61 62 whenever a datab
115a4 61 73 65 20 70 61 67 65 20 69 73 20 64 69 72 74 ase page is dirt
115a5 69 65 64 2e 0a 2a 2a 20 20 20 49 74 20 69 73 20 ied..** It is
115a6 63 6c 65 61 72 65 64 20 61 74 20 74 68 65 20 65 cleared at the e
115a7 6e 64 20 6f 66 20 65 61 63 68 20 74 72 61 6e 73 nd of each trans
115a8 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 action..**.**
115a9 49 74 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 It is used when
115aa 63 6f 6d 6d 69 74 74 69 6e 67 20 6f 72 20 6f 74 committing or ot
115ab 68 65 72 77 69 73 65 20 65 6e 64 69 6e 67 20 61 herwise ending a
115ac 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 transaction. If
115ad 0a 2a 2a 20 20 20 74 68 65 20 64 62 4d 6f 64 69 .** the dbModi
115ae 66 69 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65 fied flag is cle
115af 61 72 20 74 68 65 6e 20 6c 65 73 73 20 77 6f 72 ar then less wor
115b0 6b 20 68 61 73 20 74 6f 20 62 65 20 64 6f 6e 65 k has to be done
115b1 2e 0a 2a 2a 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 53 ..**.** journalS
115b2 74 61 72 74 65 64 0a 2a 2a 0a 2a 2a 20 20 20 54 tarted.**.** T
115b3 68 69 73 20 66 6c 61 67 20 69 73 20 73 65 74 20 his flag is set
115b4 77 68 65 6e 65 76 65 72 20 74 68 65 20 74 68 65 whenever the the
115b5 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 69 73 main journal is
115b6 20 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 synced. .**.**
115b7 20 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 The point of t
115b8 68 69 73 20 66 6c 61 67 20 69 73 20 74 68 61 74 his flag is that
115b9 20 69 74 20 6d 75 73 74 20 62 65 20 73 65 74 20 it must be set
115ba 61 66 74 65 72 20 74 68 65 20 0a 2a 2a 20 20 20 after the .**
115bb 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 first journal he
115bc 61 64 65 72 20 69 6e 20 61 20 6a 6f 75 72 6e 61 ader in a journa
115bd 6c 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 l file has been
115be 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a synced to disk..
115bf 2a 2a 20 20 20 41 66 74 65 72 20 74 68 69 73 20 ** After this
115c0 68 61 73 20 68 61 70 70 65 6e 65 64 2c 20 6e 65 has happened, ne
115c1 77 20 70 61 67 65 73 20 61 70 70 65 6e 64 65 64 w pages appended
115c2 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
115c3 20 0a 2a 2a 20 20 20 64 6f 20 6e 6f 74 20 6e 65 .** do not ne
115c4 65 64 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 ed the PGHDR_NEE
115c5 44 5f 53 59 4e 43 20 66 6c 61 67 20 73 65 74 2c D_SYNC flag set,
115c6 20 61 73 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 as they do not
115c7 6e 65 65 64 0a 2a 2a 20 20 20 74 6f 20 77 61 69 need.** to wai
115c8 74 20 66 6f 72 20 61 20 6a 6f 75 72 6e 61 6c 20 t for a journal
115c9 73 79 6e 63 20 62 65 66 6f 72 65 20 74 68 65 79 sync before they
115ca 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 can be written
115cb 6f 75 74 20 74 6f 0a 2a 2a 20 20 20 74 68 65 20 out to.** the
115cc 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 73 database file (s
115cd 65 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 67 65 ee function page
115ce 72 5f 77 72 69 74 65 28 29 29 2e 0a 2a 2a 20 20 r_write())..**
115cf 20 0a 2a 2a 20 73 65 74 4d 61 73 74 65 72 0a 2a .** setMaster.*
115d0 2a 0a 2a 2a 20 20 20 54 68 69 73 20 76 61 72 69 *.** This vari
115d1 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 able is used to
115d2 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 ensure that the
115d3 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
115d4 69 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 28 69 ile name.** (i
115d5 66 20 61 6e 79 29 20 69 73 20 6f 6e 6c 79 20 77 f any) is only w
115d6 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
115d7 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 6e 63 journal file onc
115d8 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 57 68 65 6e 20 e..**.** When
115d9 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 72 61 committing a tra
115da 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 20 6d 61 nsaction, the ma
115db 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
115dc 65 20 6e 61 6d 65 20 28 69 66 20 61 6e 79 29 0a e name (if any).
115dd 2a 2a 20 20 20 6d 61 79 20 62 65 20 77 72 69 74 ** may be writ
115de 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 ten into the jou
115df 72 6e 61 6c 20 66 69 6c 65 20 77 68 69 6c 65 20 rnal file while
115e0 74 68 65 20 70 61 67 65 72 20 69 73 20 73 74 69 the pager is sti
115e1 6c 6c 20 69 6e 0a 2a 2a 20 20 20 50 41 47 45 52 ll in.** PAGER
115e2 5f 52 45 53 45 52 56 45 44 20 73 74 61 74 65 20 _RESERVED state
115e3 28 73 65 65 20 43 6f 6d 6d 69 74 50 68 61 73 65 (see CommitPhase
115e4 4f 6e 65 28 29 20 66 6f 72 20 74 68 65 20 61 63 One() for the ac
115e5 74 69 6f 6e 29 2e 20 49 74 0a 2a 2a 20 20 20 74 tion). It.** t
115e6 68 65 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 hen attempts to
115e7 75 70 67 72 61 64 65 20 74 6f 20 61 6e 20 65 78 upgrade to an ex
115e8 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 49 66 clusive lock. If
115e9 20 74 68 69 73 20 61 74 74 65 6d 70 74 0a 2a 2a this attempt.**
115ea 20 20 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 53 fails, then S
115eb 51 4c 49 54 45 5f 42 55 53 59 20 6d 61 79 20 62 QLITE_BUSY may b
115ec 65 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 e returned to th
115ed 65 20 75 73 65 72 20 61 6e 64 20 74 68 65 20 75 e user and the u
115ee 73 65 72 0a 2a 2a 20 20 20 6d 61 79 20 61 74 74 ser.** may att
115ef 65 6d 70 74 20 74 6f 20 63 6f 6d 6d 69 74 20 74 empt to commit t
115f0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 he transaction a
115f1 67 61 69 6e 20 6c 61 74 65 72 20 28 63 61 6c 6c gain later (call
115f2 69 6e 67 0a 2a 2a 20 20 20 43 6f 6d 6d 69 74 50 ing.** CommitP
115f3 68 61 73 65 4f 6e 65 28 29 20 61 67 61 69 6e 29 haseOne() again)
115f4 2e 20 54 68 69 73 20 66 6c 61 67 20 69 73 20 75 . This flag is u
115f5 73 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 sed to ensure th
115f6 61 74 20 74 68 65 20 0a 2a 2a 20 20 20 6d 61 73 at the .** mas
115f7 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 ter journal name
115f8 20 69 73 20 6f 6e 6c 79 20 77 72 69 74 74 65 6e is only written
115f9 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 to the journal
115fa 66 69 6c 65 20 74 68 65 20 66 69 72 73 74 0a 2a file the first.*
115fb 2a 20 20 20 74 69 6d 65 20 43 6f 6d 6d 69 74 50 * time CommitP
115fc 68 61 73 65 4f 6e 65 28 29 20 69 73 20 63 61 6c haseOne() is cal
115fd 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 64 6f 4e 6f 74 led..**.** doNot
115fe 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 Sync.**.** Thi
115ff 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 s variable is se
11600 74 20 61 6e 64 20 63 6c 65 61 72 65 64 20 62 79 t and cleared by
11601 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
11602 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 6e 65 65 64 te()..**.** need
11603 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 4f 44 Sync.**.** TOD
11604 4f 3a 20 49 74 20 6d 69 67 68 74 20 62 65 20 65 O: It might be e
11605 61 73 69 65 72 20 74 6f 20 73 65 74 20 74 68 69 asier to set thi
11606 73 20 76 61 72 69 61 62 6c 65 20 69 6e 20 77 72 s variable in wr
11607 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 29 0a iteJournalHdr().
11608 2a 2a 20 20 20 61 6e 64 20 77 72 69 74 65 4d 61 ** and writeMa
11609 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 6f 6e sterJournal() on
1160a 6c 79 2e 20 43 68 61 6e 67 65 20 69 74 73 20 6d ly. Change its m
1160b 65 61 6e 69 6e 67 20 74 6f 20 22 75 6e 73 79 6e eaning to "unsyn
1160c 63 65 64 20 64 61 74 61 0a 2a 2a 20 20 20 68 61 ced data.** ha
1160d 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 s been written t
1160e 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 22 2e 0a o the journal"..
1160f 2a 2a 0a 2a 2a 20 73 75 62 6a 49 6e 4d 65 6d 6f **.** subjInMemo
11610 72 79 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 ry.**.** This
11611 69 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 72 is a boolean var
11612 69 61 62 6c 65 2e 20 49 66 20 74 72 75 65 2c 20 iable. If true,
11613 74 68 65 6e 20 61 6e 79 20 72 65 71 75 69 72 65 then any require
11614 64 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a d sub-journal.**
11615 20 20 20 69 73 20 6f 70 65 6e 65 64 20 61 73 20 is opened as
11616 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 an in-memory jou
11617 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 66 61 rnal file. If fa
11618 6c 73 65 2c 20 74 68 65 6e 20 69 6e 2d 6d 65 6d lse, then in-mem
11619 6f 72 79 0a 2a 2a 20 20 20 73 75 62 2d 6a 6f 75 ory.** sub-jou
1161a 72 6e 61 6c 73 20 61 72 65 20 6f 6e 6c 79 20 75 rnals are only u
1161b 73 65 64 20 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72 sed for in-memor
1161c 79 20 70 61 67 65 72 20 66 69 6c 65 73 2e 0a 2a y pager files..*
1161d 2f 0a 73 74 72 75 63 74 20 50 61 67 65 72 20 7b /.struct Pager {
1161e 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
1161f 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 2f pVfs; /
11620 2a 20 4f 53 20 66 75 6e 63 74 69 6f 6e 73 20 74 * OS functions t
11621 6f 20 75 73 65 20 66 6f 72 20 49 4f 20 2a 2f 0a o use for IO */.
11622 20 20 75 38 20 65 78 63 6c 75 73 69 76 65 4d 6f u8 exclusiveMo
11623 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a de; /*
11624 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 20 69 Boolean. True i
11625 66 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 3d f locking_mode==
11626 45 58 43 4c 55 53 49 56 45 20 2a 2f 0a 20 20 75 EXCLUSIVE */. u
11627 38 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 20 20 8 journalMode;
11628 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e /* On
11629 20 6f 66 20 74 68 65 20 50 41 47 45 52 5f 4a 4f of the PAGER_JO
1162a 55 52 4e 41 4c 4d 4f 44 45 5f 2a 20 76 61 6c 75 URNALMODE_* valu
1162b 65 73 20 2a 2f 0a 20 20 75 38 20 75 73 65 4a 6f es */. u8 useJo
1162c 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 urnal;
1162d 20 20 20 20 2f 2a 20 55 73 65 20 61 20 72 6f 6c /* Use a rol
1162e 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 6f 6e lback journal on
1162f 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 this file */.
11630 75 38 20 6e 6f 52 65 61 64 6c 6f 63 6b 3b 20 20 u8 noReadlock;
11631 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
11632 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 74 6f 20 o not bother to
11633 6f 62 74 61 69 6e 20 72 65 61 64 6c 6f 63 6b 73 obtain readlocks
11634 20 2a 2f 0a 20 20 75 38 20 6e 6f 53 79 6e 63 3b */. u8 noSync;
11635 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11636 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 79 6e 63 /* Do not sync
11637 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 66 20 the journal if
11638 74 72 75 65 20 2a 2f 0a 20 20 75 38 20 66 75 6c true */. u8 ful
11639 6c 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 lSync;
1163a 20 20 20 20 20 20 2f 2a 20 44 6f 20 65 78 74 72 /* Do extr
1163b 61 20 73 79 6e 63 73 20 6f 66 20 74 68 65 20 6a a syncs of the j
1163c 6f 75 72 6e 61 6c 20 66 6f 72 20 72 6f 62 75 73 ournal for robus
1163d 74 6e 65 73 73 20 2a 2f 0a 20 20 75 38 20 73 79 tness */. u8 sy
1163e 6e 63 5f 66 6c 61 67 73 3b 20 20 20 20 20 20 20 nc_flags;
1163f 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 /* One of
11640 20 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20 SYNC_NORMAL or
11641 53 59 4e 43 5f 46 55 4c 4c 20 2a 2f 0a 20 20 75 SYNC_FULL */. u
11642 38 20 74 65 6d 70 46 69 6c 65 3b 20 20 20 20 20 8 tempFile;
11643 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 /* zF
11644 69 6c 65 6e 61 6d 65 20 69 73 20 61 20 74 65 6d ilename is a tem
11645 70 6f 72 61 72 79 20 66 69 6c 65 20 2a 2f 0a 20 porary file */.
11646 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 u8 readOnly;
11647 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11648 54 72 75 65 20 66 6f 72 20 61 20 72 65 61 64 2d True for a read-
11649 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f only database */
1164a 0a 20 20 75 38 20 6d 65 6d 44 62 3b 20 20 20 20 . u8 memDb;
1164b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1164c 2a 20 54 72 75 65 20 74 6f 20 69 6e 68 69 62 69 * True to inhibi
1164d 74 20 61 6c 6c 20 66 69 6c 65 20 49 2f 4f 20 2a t all file I/O *
1164e 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c /.. /* The foll
1164f 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 63 6f 6e 74 owing block cont
11650 61 69 6e 73 20 74 68 6f 73 65 20 63 6c 61 73 73 ains those class
11651 20 6d 65 6d 62 65 72 73 20 74 68 61 74 20 61 72 members that ar
11652 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 20 20 e dynamically.
11653 2a 2a 20 6d 6f 64 69 66 69 65 64 20 64 75 72 69 ** modified duri
11654 6e 67 20 6e 6f 72 6d 61 6c 20 6f 70 65 72 61 74 ng normal operat
11655 69 6f 6e 73 2e 20 54 68 65 20 6f 74 68 65 72 20 ions. The other
11656 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 69 variables in thi
11657 73 20 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2a s structure. **
11658 20 61 72 65 20 65 69 74 68 65 72 20 63 6f 6e 73 are either cons
11659 74 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 20 tant throughout
1165a 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 the lifetime of
1165b 74 68 65 20 70 61 67 65 72 2c 20 6f 72 20 65 6c the pager, or el
1165c 73 65 0a 20 20 2a 2a 20 75 73 65 64 20 74 6f 20 se. ** used to
1165d 73 74 6f 72 65 20 63 6f 6e 66 69 67 75 72 61 74 store configurat
1165e 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 20 74 ion parameters t
1165f 68 61 74 20 61 66 66 65 63 74 20 74 68 65 20 77 hat affect the w
11660 61 79 20 74 68 65 20 70 61 67 65 72 20 0a 20 20 ay the pager .
11661 2a 2a 20 6f 70 65 72 61 74 65 73 2e 0a 20 20 2a ** operates.. *
11662 2a 0a 20 20 2a 2a 20 54 68 65 20 27 73 74 61 74 *. ** The 'stat
11663 65 27 20 76 61 72 69 61 62 6c 65 20 69 73 20 64 e' variable is d
11664 65 73 63 72 69 62 65 64 20 69 6e 20 6d 6f 72 65 escribed in more
11665 20 64 65 74 61 69 6c 20 61 6c 6f 6e 67 20 77 69 detail along wi
11666 74 68 20 74 68 65 0a 20 20 2a 2a 20 64 65 73 63 th the. ** desc
11667 72 69 70 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 riptions of the
11668 76 61 6c 75 65 73 20 69 74 20 6d 61 79 20 74 61 values it may ta
11669 6b 65 20 2d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 ke - PAGER_UNLOC
1166a 4b 20 65 74 63 2e 20 4d 61 6e 79 20 6f 66 20 74 K etc. Many of t
1166b 68 65 0a 20 20 2a 2a 20 6f 74 68 65 72 20 76 61 he. ** other va
1166c 72 69 61 62 6c 65 73 20 69 6e 20 74 68 69 73 20 riables in this
1166d 62 6c 6f 63 6b 20 61 72 65 20 64 65 73 63 72 69 block are descri
1166e 62 65 64 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65 bed in the comme
1166f 6e 74 20 64 69 72 65 63 74 6c 79 20 0a 20 20 2a nt directly . *
11670 2a 20 61 62 6f 76 65 20 74 68 69 73 20 63 6c 61 * above this cla
11671 73 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 20 ss definition..
11672 20 2a 2f 0a 20 20 75 38 20 73 74 61 74 65 3b 20 */. u8 state;
11673 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11674 20 20 2f 2a 20 50 41 47 45 52 5f 55 4e 4c 4f 43 /* PAGER_UNLOC
11675 4b 2c 20 5f 53 48 41 52 45 44 2c 20 5f 52 45 53 K, _SHARED, _RES
11676 45 52 56 45 44 2c 20 65 74 63 2e 20 2a 2f 0a 20 ERVED, etc. */.
11677 20 75 38 20 64 62 4d 6f 64 69 66 69 65 64 3b 20 u8 dbModified;
11678 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11679 54 72 75 65 20 69 66 20 74 68 65 72 65 20 61 72 True if there ar
1167a 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 74 6f e any changes to
1167b 20 74 68 65 20 44 62 20 2a 2f 0a 20 20 75 38 20 the Db */. u8
1167c 6e 65 65 64 53 79 6e 63 3b 20 20 20 20 20 20 20 needSync;
1167d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
1167e 20 69 66 20 61 6e 20 66 73 79 6e 63 28 29 20 69 if an fsync() i
1167f 73 20 6e 65 65 64 65 64 20 6f 6e 20 74 68 65 20 s needed on the
11680 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 journal */. u8
11681 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 3b 20 journalStarted;
11682 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
11683 20 69 66 20 68 65 61 64 65 72 20 6f 66 20 6a 6f if header of jo
11684 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 urnal is synced
11685 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 43 6f */. u8 changeCo
11686 75 6e 74 44 6f 6e 65 3b 20 20 20 20 20 20 20 20 untDone;
11687 20 2f 2a 20 53 65 74 20 61 66 74 65 72 20 69 6e /* Set after in
11688 63 72 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 63 crementing the c
11689 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f hange-counter */
1168a 0a 20 20 75 38 20 73 65 74 4d 61 73 74 65 72 3b . u8 setMaster;
1168b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1168c 2a 20 54 72 75 65 20 69 66 20 61 20 6d 2d 6a 20 * True if a m-j
1168d 6e 61 6d 65 20 68 61 73 20 62 65 65 6e 20 77 72 name has been wr
1168e 69 74 74 65 6e 20 74 6f 20 6a 72 6e 6c 20 2a 2f itten to jrnl */
1168f 0a 20 20 75 38 20 64 6f 4e 6f 74 53 79 6e 63 3b . u8 doNotSync;
11690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11691 2a 20 42 6f 6f 6c 65 61 6e 2e 20 57 68 69 6c 65 * Boolean. While
11692 20 74 72 75 65 2c 20 64 6f 20 6e 6f 74 20 73 70 true, do not sp
11693 69 6c 6c 20 74 68 65 20 63 61 63 68 65 20 2a 2f ill the cache */
11694 0a 20 20 75 38 20 64 62 53 69 7a 65 56 61 6c 69 . u8 dbSizeVali
11695 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f d; /
11696 2a 20 53 65 74 20 77 68 65 6e 20 64 62 53 69 7a * Set when dbSiz
11697 65 20 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a e is correct */.
11698 20 20 75 38 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 u8 subjInMemor
11699 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a y; /*
1169a 20 54 72 75 65 20 74 6f 20 75 73 65 20 69 6e 2d True to use in-
1169b 6d 65 6d 6f 72 79 20 73 75 62 2d 6a 6f 75 72 6e memory sub-journ
1169c 61 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 als */. Pgno db
1169d 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 Size;
1169e 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1169f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 f pages in the d
116a0 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 50 67 6e atabase */. Pgn
116a1 6f 20 64 62 4f 72 69 67 53 69 7a 65 3b 20 20 20 o dbOrigSize;
116a2 20 20 20 20 20 20 20 20 20 2f 2a 20 64 62 53 69 /* dbSi
116a3 7a 65 20 62 65 66 6f 72 65 20 74 68 65 20 63 75 ze before the cu
116a4 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f rrent transactio
116a5 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 46 69 n */. Pgno dbFi
116a6 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 leSize;
116a7 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
116a8 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 pages in the dat
116a9 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 abase file */.
116aa 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 20 20 int errCode;
116ab 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
116ac 6e 65 20 6f 66 20 73 65 76 65 72 61 6c 20 6b 69 ne of several ki
116ad 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f nds of errors */
116ae 0a 20 20 69 6e 74 20 6e 52 65 63 3b 20 20 20 20 . int nRec;
116af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
116b0 2a 20 50 61 67 65 73 20 6a 6f 75 72 6e 61 6c 6c * Pages journall
116b1 65 64 20 73 69 6e 63 65 20 6c 61 73 74 20 6a 2d ed since last j-
116b2 68 65 61 64 65 72 20 77 72 69 74 74 65 6e 20 2a header written *
116b3 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 49 6e 69 /. u32 cksumIni
116b4 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
116b5 2f 2a 20 51 75 61 73 69 2d 72 61 6e 64 6f 6d 20 /* Quasi-random
116b6 76 61 6c 75 65 20 61 64 64 65 64 20 74 6f 20 65 value added to e
116b7 76 65 72 79 20 63 68 65 63 6b 73 75 6d 20 2a 2f very checksum */
116b8 0a 20 20 75 33 32 20 6e 53 75 62 52 65 63 3b 20 . u32 nSubRec;
116b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
116ba 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f * Number of reco
116bb 72 64 73 20 77 72 69 74 74 65 6e 20 74 6f 20 73 rds written to s
116bc 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 ub-journal */.
116bd 42 69 74 76 65 63 20 2a 70 49 6e 4a 6f 75 72 6e Bitvec *pInJourn
116be 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f al; /* O
116bf 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63 68 20 ne bit for each
116c0 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 page in the data
116c1 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 73 base file */. s
116c2 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 3b qlite3_file *fd;
116c3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
116c4 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f le descriptor fo
116c5 72 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 r database */.
116c6 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66 sqlite3_file *jf
116c7 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 d; /* F
116c8 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 ile descriptor f
116c9 6f 72 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 or main journal
116ca 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c */. sqlite3_fil
116cb 65 20 2a 73 6a 66 64 3b 20 20 20 20 20 20 20 20 e *sjfd;
116cc 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 /* File descrip
116cd 74 6f 72 20 66 6f 72 20 73 75 62 2d 6a 6f 75 72 tor for sub-jour
116ce 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 nal */. i64 jou
116cf 72 6e 61 6c 4f 66 66 3b 20 20 20 20 20 20 20 20 rnalOff;
116d0 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 /* Current
116d1 77 72 69 74 65 20 6f 66 66 73 65 74 20 69 6e 20 write offset in
116d2 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
116d3 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 */. i64 journa
116d4 6c 48 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 lHdr;
116d5 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 /* Byte offset
116d6 20 74 6f 20 70 72 65 76 69 6f 75 73 20 6a 6f 75 to previous jou
116d7 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 20 rnal header */.
116d8 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 PagerSavepoint
116d9 2a 61 53 61 76 65 70 6f 69 6e 74 3b 20 2f 2a 20 *aSavepoint; /*
116da 41 72 72 61 79 20 6f 66 20 61 63 74 69 76 65 20 Array of active
116db 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 savepoints */.
116dc 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b 20 int nSavepoint;
116dd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
116de 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 umber of element
116df 73 20 69 6e 20 61 53 61 76 65 70 6f 69 6e 74 5b s in aSavepoint[
116e0 5d 20 2a 2f 0a 20 20 63 68 61 72 20 64 62 46 69 ] */. char dbFi
116e1 6c 65 56 65 72 73 5b 31 36 5d 3b 20 20 20 20 20 leVers[16];
116e2 20 20 20 2f 2a 20 43 68 61 6e 67 65 73 20 77 68 /* Changes wh
116e3 65 6e 65 76 65 72 20 64 61 74 61 62 61 73 65 20 enever database
116e4 66 69 6c 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a file changes */.
116e5 20 20 75 33 32 20 73 65 63 74 6f 72 53 69 7a 65 u32 sectorSize
116e6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
116e7 20 41 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20 Assumed sector
116e8 73 69 7a 65 20 64 75 72 69 6e 67 20 72 6f 6c 6c size during roll
116e9 62 61 63 6b 20 2a 2f 0a 0a 20 20 75 31 36 20 6e back */.. u16 n
116ea 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 Extra;
116eb 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 /* Add th
116ec 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20 74 6f is many bytes to
116ed 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 each in-memory
116ee 70 61 67 65 20 2a 2f 0a 20 20 69 31 36 20 6e 52 page */. i16 nR
116ef 65 73 65 72 76 65 3b 20 20 20 20 20 20 20 20 20 eserve;
116f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
116f1 6f 66 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 of unused bytes
116f2 61 74 20 65 6e 64 20 6f 66 20 65 61 63 68 20 70 at end of each p
116f3 61 67 65 20 2a 2f 0a 20 20 75 33 32 20 76 66 73 age */. u32 vfs
116f4 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 Flags;
116f5 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 66 6f /* Flags fo
116f6 72 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f r sqlite3_vfs.xO
116f7 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 70 pen() */. int p
116f8 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 ageSize;
116f9 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
116fa 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61 20 70 of bytes in a p
116fb 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 age */. Pgno mx
116fc 50 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 Pgno;
116fd 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 /* Maximum
116fe 61 6c 6c 6f 77 65 64 20 73 69 7a 65 20 6f 66 20 allowed size of
116ff 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a the database */.
11700 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d char *zFilenam
11701 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
11702 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 Name of the dat
11703 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 abase file */.
11704 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 char *zJournal;
11705 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
11706 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e ame of the journ
11707 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 al file */. int
11708 20 28 2a 78 42 75 73 79 48 61 6e 64 6c 65 72 29 (*xBusyHandler)
11709 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 46 75 6e 63 (void*); /* Func
1170a 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 68 65 tion to call whe
1170b 6e 20 62 75 73 79 20 2a 2f 0a 20 20 76 6f 69 64 n busy */. void
1170c 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 *pBusyHandlerAr
1170d 67 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 g; /* Conte
1170e 78 74 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 xt argument for
1170f 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a xBusyHandler */.
11710 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
11711 53 54 0a 20 20 69 6e 74 20 6e 48 69 74 2c 20 6e ST. int nHit, n
11712 4d 69 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 Miss;
11713 20 2f 2a 20 43 61 63 68 65 20 68 69 74 73 20 61 /* Cache hits a
11714 6e 64 20 6d 69 73 73 69 6e 67 20 2a 2f 0a 20 20 nd missing */.
11715 69 6e 74 20 6e 52 65 61 64 2c 20 6e 57 72 69 74 int nRead, nWrit
11716 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 e; /* D
11717 61 74 61 62 61 73 65 20 70 61 67 65 73 20 72 65 atabase pages re
11718 61 64 2f 77 72 69 74 74 65 6e 20 2a 2f 0a 23 65 ad/written */.#e
11719 6e 64 69 66 0a 20 20 76 6f 69 64 20 28 2a 78 52 ndif. void (*xR
1171a 65 69 6e 69 74 65 72 29 28 44 62 50 61 67 65 2a einiter)(DbPage*
1171b 29 3b 20 2f 2a 20 43 61 6c 6c 20 74 68 69 73 20 ); /* Call this
1171c 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 72 65 6c routine when rel
1171d 6f 61 64 69 6e 67 20 70 61 67 65 73 20 2a 2f 0a oading pages */.
1171e 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 #ifdef SQLITE_HA
1171f 53 5f 43 4f 44 45 43 0a 20 20 76 6f 69 64 20 2a S_CODEC. void *
11720 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c (*xCodec)(void*,
11721 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b void*,Pgno,int);
11722 20 2f 2a 20 52 6f 75 74 69 6e 65 20 66 6f 72 20 /* Routine for
11723 65 6e 2f 64 65 63 6f 64 69 6e 67 20 64 61 74 61 en/decoding data
11724 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f */. void (*xCo
11725 64 65 63 53 69 7a 65 43 68 6e 67 29 28 76 6f 69 decSizeChng)(voi
11726 64 2a 2c 69 6e 74 2c 69 6e 74 29 3b 20 2f 2a 20 d*,int,int); /*
11727 4e 6f 74 69 66 79 20 6f 66 20 70 61 67 65 20 73 Notify of page s
11728 69 7a 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20 ize changes */.
11729 20 76 6f 69 64 20 28 2a 78 43 6f 64 65 63 46 72 void (*xCodecFr
1172a 65 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 ee)(void*);
1172b 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 72 /* Destr
1172c 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 63 6f uctor for the co
1172d 64 65 63 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 dec */. void *p
1172e 43 6f 64 65 63 3b 20 20 20 20 20 20 20 20 20 20 Codec;
1172f 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 /* First ar
11730 67 75 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65 63 gument to xCodec
11731 2e 2e 2e 20 6d 65 74 68 6f 64 73 20 2a 2f 0a 23 ... methods */.#
11732 65 6e 64 69 66 0a 20 20 63 68 61 72 20 2a 70 54 endif. char *pT
11733 6d 70 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 mpSpace;
11734 20 20 20 20 2f 2a 20 50 61 67 65 72 2e 70 61 67 /* Pager.pag
11735 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 eSize bytes of s
11736 70 61 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65 pace for tmp use
11737 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 */. i64 journa
11738 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 20 lSizeLimit;
11739 20 20 2f 2a 20 53 69 7a 65 20 6c 69 6d 69 74 20 /* Size limit
1173a 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a for persistent j
1173b 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a ournal files */.
1173c 20 20 50 43 61 63 68 65 20 2a 70 50 43 61 63 68 PCache *pPCach
1173d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
1173e 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65 Pointer to page
1173f 20 63 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f cache object */
11740 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 . sqlite3_backu
11741 70 20 2a 70 42 61 63 6b 75 70 3b 20 20 20 20 2f p *pBackup; /
11742 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6c 69 73 * Pointer to lis
11743 74 20 6f 66 20 6f 6e 67 6f 69 6e 67 20 62 61 63 t of ongoing bac
11744 6b 75 70 20 70 72 6f 63 65 73 73 65 73 20 2a 2f kup processes */
11745 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 .};../*.** The f
11746 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 ollowing global
11747 76 61 72 69 61 62 6c 65 73 20 68 6f 6c 64 20 63 variables hold c
11748 6f 75 6e 74 65 72 73 20 75 73 65 64 20 66 6f 72 ounters used for
11749 0a 2a 2a 20 74 65 73 74 69 6e 67 20 70 75 72 70 .** testing purp
1174a 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 54 68 65 73 oses only. Thes
1174b 65 20 76 61 72 69 61 62 6c 65 73 20 64 6f 20 6e e variables do n
1174c 6f 74 20 65 78 69 73 74 20 69 6e 0a 2a 2a 20 61 ot exist in.** a
1174d 20 6e 6f 6e 2d 74 65 73 74 69 6e 67 20 62 75 69 non-testing bui
1174e 6c 64 2e 20 20 54 68 65 73 65 20 76 61 72 69 61 ld. These varia
1174f 62 6c 65 73 20 61 72 65 20 6e 6f 74 20 74 68 72 bles are not thr
11750 65 61 64 2d 73 61 66 65 2e 0a 2a 2f 0a 23 69 66 ead-safe..*/.#if
11751 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
11752 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
11753 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 72 65 61 qlite3_pager_rea
11754 64 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 ddb_count = 0;
11755 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 /* Number of f
11756 75 6c 6c 20 70 61 67 65 73 20 72 65 61 64 20 66 ull pages read f
11757 72 6f 6d 20 44 42 20 2a 2f 0a 53 51 4c 49 54 45 rom DB */.SQLITE
11758 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
11759 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63 _pager_writedb_c
1175a 6f 75 6e 74 20 3d 20 30 3b 20 20 20 2f 2a 20 4e ount = 0; /* N
1175b 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61 umber of full pa
1175c 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 44 ges written to D
1175d 42 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 B */.SQLITE_API
1175e 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 int sqlite3_page
1175f 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 20 3d r_writej_count =
11760 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 0; /* Number
11761 20 6f 66 20 70 61 67 65 73 20 77 72 69 74 74 65 of pages writte
11762 6e 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a n to journal */.
11763 23 20 64 65 66 69 6e 65 20 50 41 47 45 52 5f 49 # define PAGER_I
11764 4e 43 52 28 76 29 20 20 76 2b 2b 0a 23 65 6c 73 NCR(v) v++.#els
11765 65 0a 23 20 64 65 66 69 6e 65 20 50 41 47 45 52 e.# define PAGER
11766 5f 49 4e 43 52 28 76 29 0a 23 65 6e 64 69 66 0a _INCR(v).#endif.
11767 0a 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 75 72 6e 61 6c .../*.** Journal
11768 20 66 69 6c 65 73 20 62 65 67 69 6e 20 77 69 74 files begin wit
11769 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 h the following
1176a 6d 61 67 69 63 20 73 74 72 69 6e 67 2e 20 20 54 magic string. T
1176b 68 65 20 64 61 74 61 0a 2a 2a 20 77 61 73 20 6f he data.** was o
1176c 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 2f 64 65 btained from /de
1176d 76 2f 72 61 6e 64 6f 6d 2e 20 20 49 74 20 69 73 v/random. It is
1176e 20 75 73 65 64 20 6f 6e 6c 79 20 61 73 20 61 20 used only as a
1176f 73 61 6e 69 74 79 20 63 68 65 63 6b 2e 0a 2a 2a sanity check..**
11770 0a 2a 2a 20 53 69 6e 63 65 20 76 65 72 73 69 6f .** Since versio
11771 6e 20 32 2e 38 2e 30 2c 20 74 68 65 20 6a 6f 75 n 2.8.0, the jou
11772 72 6e 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 74 rnal format cont
11773 61 69 6e 73 20 61 64 64 69 74 69 6f 6e 61 6c 20 ains additional
11774 73 61 6e 69 74 79 0a 2a 2a 20 63 68 65 63 6b 69 sanity.** checki
11775 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 ng information.
11776 20 49 66 20 74 68 65 20 70 6f 77 65 72 20 66 61 If the power fa
11777 69 6c 73 20 77 68 69 6c 65 20 74 68 65 20 6a 6f ils while the jo
11778 75 72 6e 61 6c 20 69 73 20 62 65 69 6e 67 0a 2a urnal is being.*
11779 2a 20 77 72 69 74 74 65 6e 2c 20 73 65 6d 69 2d * written, semi-
1177a 72 61 6e 64 6f 6d 20 67 61 72 62 61 67 65 20 64 random garbage d
1177b 61 74 61 20 6d 69 67 68 74 20 61 70 70 65 61 72 ata might appear
1177c 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a in the journal.
1177d 2a 2a 20 66 69 6c 65 20 61 66 74 65 72 20 70 6f ** file after po
1177e 77 65 72 20 69 73 20 72 65 73 74 6f 72 65 64 2e wer is restored.
1177f 20 20 49 66 20 61 6e 20 61 74 74 65 6d 70 74 20 If an attempt
11780 69 73 20 74 68 65 6e 20 6d 61 64 65 0a 2a 2a 20 is then made.**
11781 74 6f 20 72 6f 6c 6c 20 74 68 65 20 6a 6f 75 72 to roll the jour
11782 6e 61 6c 20 62 61 63 6b 2c 20 74 68 65 20 64 61 nal back, the da
11783 74 61 62 61 73 65 20 63 6f 75 6c 64 20 62 65 20 tabase could be
11784 63 6f 72 72 75 70 74 65 64 2e 20 20 54 68 65 20 corrupted. The
11785 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 73 61 additional.** sa
11786 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 64 61 nity checking da
11787 74 61 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 ta is an attempt
11788 20 74 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65 to discover the
11789 20 67 61 72 62 61 67 65 20 69 6e 20 74 68 65 0a garbage in the.
1178a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 69 ** journal and i
1178b 67 6e 6f 72 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 gnore it..**.**
1178c 54 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b The sanity check
1178d 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ing information
1178e 66 6f 72 20 74 68 65 20 6e 65 77 20 6a 6f 75 72 for the new jour
1178f 6e 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 73 69 nal format consi
11790 73 74 73 0a 2a 2a 20 6f 66 20 61 20 33 32 2d 62 sts.** of a 32-b
11791 69 74 20 63 68 65 63 6b 73 75 6d 20 6f 6e 20 65 it checksum on e
11792 61 63 68 20 70 61 67 65 20 6f 66 20 64 61 74 61 ach page of data
11793 2e 20 20 54 68 65 20 63 68 65 63 6b 73 75 6d 20 . The checksum
11794 63 6f 76 65 72 73 20 62 6f 74 68 0a 2a 2a 20 74 covers both.** t
11795 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61 he page number a
11796 6e 64 20 74 68 65 20 70 50 61 67 65 72 2d 3e 70 nd the pPager->p
11797 61 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 ageSize bytes of
11798 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 70 61 data for the pa
11799 67 65 2e 0a 2a 2a 20 54 68 69 73 20 63 6b 73 75 ge..** This cksu
1179a 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 m is initialized
1179b 20 74 6f 20 61 20 33 32 2d 62 69 74 20 72 61 6e to a 32-bit ran
1179c 64 6f 6d 20 76 61 6c 75 65 20 74 68 61 74 20 61 dom value that a
1179d 70 70 65 61 72 73 20 69 6e 20 74 68 65 0a 2a 2a ppears in the.**
1179e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 72 69 journal file ri
1179f 67 68 74 20 61 66 74 65 72 20 74 68 65 20 68 65 ght after the he
117a0 61 64 65 72 2e 20 20 54 68 65 20 72 61 6e 64 6f ader. The rando
117a1 6d 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 69 73 m initializer is
117a2 20 69 6d 70 6f 72 74 61 6e 74 2c 0a 2a 2a 20 62 important,.** b
117a3 65 63 61 75 73 65 20 67 61 72 62 61 67 65 20 64 ecause garbage d
117a4 61 74 61 20 74 68 61 74 20 61 70 70 65 61 72 73 ata that appears
117a5 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 at the end of a
117a6 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 69 6b 65 journal is like
117a7 6c 79 0a 2a 2a 20 64 61 74 61 20 74 68 61 74 20 ly.** data that
117a8 77 61 73 20 6f 6e 63 65 20 69 6e 20 6f 74 68 65 was once in othe
117a9 72 20 66 69 6c 65 73 20 74 68 61 74 20 68 61 76 r files that hav
117aa 65 20 6e 6f 77 20 62 65 65 6e 20 64 65 6c 65 74 e now been delet
117ab 65 64 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 67 ed. If the.** g
117ac 61 72 62 61 67 65 20 64 61 74 61 20 63 61 6d 65 arbage data came
117ad 20 66 72 6f 6d 20 61 6e 20 6f 62 73 6f 6c 65 74 from an obsolet
117ae 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 e journal file,
117af 74 68 65 20 63 68 65 63 6b 73 75 6d 73 20 6d 69 the checksums mi
117b0 67 68 74 0a 2a 2a 20 62 65 20 63 6f 72 72 65 63 ght.** be correc
117b1 74 2e 20 20 42 75 74 20 62 79 20 69 6e 69 74 69 t. But by initi
117b2 61 6c 69 7a 69 6e 67 20 74 68 65 20 63 68 65 63 alizing the chec
117b3 6b 73 75 6d 20 74 6f 20 72 61 6e 64 6f 6d 20 76 ksum to random v
117b4 61 6c 75 65 20 77 68 69 63 68 0a 2a 2a 20 69 73 alue which.** is
117b5 20 64 69 66 66 65 72 65 6e 74 20 66 6f 72 20 65 different for e
117b6 76 65 72 79 20 6a 6f 75 72 6e 61 6c 2c 20 77 65 very journal, we
117b7 20 6d 69 6e 69 6d 69 7a 65 20 74 68 61 74 20 72 minimize that r
117b8 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 isk..*/.static c
117b9 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
117ba 61 72 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 ar aJournalMagic
117bb 5b 5d 20 3d 20 7b 0a 20 20 30 78 64 39 2c 20 30 [] = {. 0xd9, 0
117bc 78 64 35 2c 20 30 78 30 35 2c 20 30 78 66 39 2c xd5, 0x05, 0xf9,
117bd 20 30 78 32 30 2c 20 30 78 61 31 2c 20 30 78 36 0x20, 0xa1, 0x6
117be 33 2c 20 30 78 64 37 2c 0a 7d 3b 0a 0a 2f 2a 0a 3, 0xd7,.};../*.
117bf 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 ** The size of t
117c0 68 65 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 he of each page
117c1 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20 6a 6f record in the jo
117c2 75 72 6e 61 6c 20 69 73 20 67 69 76 65 6e 20 62 urnal is given b
117c3 79 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 y.** the followi
117c4 6e 67 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 64 65 ng macro..*/.#de
117c5 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f fine JOURNAL_PG_
117c6 53 5a 28 70 50 61 67 65 72 29 20 20 28 28 70 50 SZ(pPager) ((pP
117c7 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 20 ager->pageSize)
117c8 2b 20 38 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 + 8)../*.** The
117c9 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 journal header s
117ca 69 7a 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 ize for this pag
117cb 65 72 2e 20 54 68 69 73 20 69 73 20 75 73 75 61 er. This is usua
117cc 6c 6c 79 20 74 68 65 20 73 61 6d 65 20 0a 2a 2a lly the same .**
117cd 20 73 69 7a 65 20 61 73 20 61 20 73 69 6e 67 6c size as a singl
117ce 65 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 20 53 e disk sector. S
117cf 65 65 20 61 6c 73 6f 20 73 65 74 53 65 63 74 6f ee also setSecto
117d0 72 53 69 7a 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 rSize()..*/.#def
117d1 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f ine JOURNAL_HDR_
117d2 53 5a 28 70 50 61 67 65 72 29 20 28 70 50 61 67 SZ(pPager) (pPag
117d3 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 0a er->sectorSize).
117d4 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f ./*.** The macro
117d5 20 4d 45 4d 44 42 20 69 73 20 74 72 75 65 20 69 MEMDB is true i
117d6 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 f we are dealing
117d7 20 77 69 74 68 20 61 6e 20 69 6e 2d 6d 65 6d 6f with an in-memo
117d8 72 79 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 ry database..**
117d9 57 65 20 64 6f 20 74 68 69 73 20 61 73 20 61 20 We do this as a
117da 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 20 69 66 macro so that if
117db 20 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 the SQLITE_OMIT
117dc 5f 4d 45 4d 4f 52 59 44 42 20 6d 61 63 72 6f 20 _MEMORYDB macro
117dd 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 20 76 is set,.** the v
117de 61 6c 75 65 20 6f 66 20 4d 45 4d 44 42 20 77 69 alue of MEMDB wi
117df 6c 6c 20 62 65 20 61 20 63 6f 6e 73 74 61 6e 74 ll be a constant
117e0 20 61 6e 64 20 74 68 65 20 63 6f 6d 70 69 6c 65 and the compile
117e1 72 20 77 69 6c 6c 20 6f 70 74 69 6d 69 7a 65 0a r will optimize.
117e2 2a 2a 20 6f 75 74 20 63 6f 64 65 20 74 68 61 74 ** out code that
117e3 20 77 6f 75 6c 64 20 6e 65 76 65 72 20 65 78 65 would never exe
117e4 63 75 74 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 cute..*/.#ifdef
117e5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f SQLITE_OMIT_MEMO
117e6 52 59 44 42 0a 23 20 64 65 66 69 6e 65 20 4d 45 RYDB.# define ME
117e7 4d 44 42 20 30 0a 23 65 6c 73 65 0a 23 20 64 65 MDB 0.#else.# de
117e8 66 69 6e 65 20 4d 45 4d 44 42 20 70 50 61 67 65 fine MEMDB pPage
117e9 72 2d 3e 6d 65 6d 44 62 0a 23 65 6e 64 69 66 0a r->memDb.#endif.
117ea 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d ./*.** The maxim
117eb 75 6d 20 6c 65 67 61 6c 20 70 61 67 65 20 6e 75 um legal page nu
117ec 6d 62 65 72 20 69 73 20 28 32 5e 33 31 20 2d 20 mber is (2^31 -
117ed 31 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 1)..*/.#define P
117ee 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 32 31 AGER_MAX_PGNO 21
117ef 34 37 34 38 33 36 34 37 0a 0a 23 69 66 6e 64 65 47483647..#ifnde
117f0 66 20 4e 44 45 42 55 47 20 0a 2f 2a 0a 2a 2a 20 f NDEBUG ./*.**
117f1 55 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 Usage:.**.** a
117f2 73 73 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 ssert( assert_pa
117f3 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 ger_state(pPager
117f4 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ) );.*/.static i
117f5 6e 74 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f nt assert_pager_
117f6 73 74 61 74 65 28 50 61 67 65 72 20 2a 70 50 61 state(Pager *pPa
117f7 67 65 72 29 7b 0a 0a 20 20 2f 2a 20 41 20 74 65 ger){.. /* A te
117f8 6d 70 2d 66 69 6c 65 20 69 73 20 61 6c 77 61 79 mp-file is alway
117f9 73 20 69 6e 20 50 41 47 45 52 5f 45 58 43 4c 55 s in PAGER_EXCLU
117fa 53 49 56 45 20 6f 72 20 50 41 47 45 52 5f 53 59 SIVE or PAGER_SY
117fb 4e 43 45 44 20 73 74 61 74 65 2e 20 2a 2f 0a 20 NCED state. */.
117fc 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
117fd 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c 20 >tempFile==0 ||
117fe 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 pPager->state>=P
117ff 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 AGER_EXCLUSIVE )
11800 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 63 68 61 6e ;.. /* The chan
11801 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 geCountDone flag
11802 20 69 73 20 61 6c 77 61 79 73 20 73 65 74 20 66 is always set f
11803 6f 72 20 74 65 6d 70 2d 66 69 6c 65 73 20 2a 2f or temp-files */
11804 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
11805 72 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c r->tempFile==0 |
11806 7c 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 | pPager->change
11807 43 6f 75 6e 74 44 6f 6e 65 20 29 3b 0a 0a 20 20 CountDone );..
11808 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 return 1;.}.#end
11809 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e if../*.** Return
1180a 20 74 72 75 65 20 69 66 20 69 74 20 69 73 20 6e true if it is n
1180b 65 63 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 ecessary to writ
1180c 65 20 70 61 67 65 20 2a 70 50 67 20 69 6e 74 6f e page *pPg into
1180d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
1180e 2e 0a 2a 2a 20 41 20 70 61 67 65 20 6e 65 65 64 ..** A page need
1180f 73 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 s to be written
11810 69 6e 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 into the sub-jou
11811 72 6e 61 6c 20 69 66 20 74 68 65 72 65 20 65 78 rnal if there ex
11812 69 73 74 73 20 6f 6e 65 0a 2a 2a 20 6f 72 20 6d ists one.** or m
11813 6f 72 65 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 ore open savepoi
11814 6e 74 73 20 66 6f 72 20 77 68 69 63 68 3a 0a 2a nts for which:.*
11815 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 70 61 67 *.** * The pag
11816 65 2d 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 e-number is less
11817 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
11818 6f 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 o PagerSavepoint
11819 2e 6e 4f 72 69 67 2c 20 61 6e 64 0a 2a 2a 20 20 .nOrig, and.**
1181a 20 2a 20 54 68 65 20 62 69 74 20 63 6f 72 72 65 * The bit corre
1181b 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 sponding to the
1181c 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 6e page-number is n
1181d 6f 74 20 73 65 74 20 69 6e 0a 2a 2a 20 20 20 20 ot set in.**
1181e 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e PagerSavepoint.
1181f 70 49 6e 53 61 76 65 70 6f 69 6e 74 2e 0a 2a 2f pInSavepoint..*/
11820 0a 73 74 61 74 69 63 20 69 6e 74 20 73 75 62 6a .static int subj
11821 52 65 71 75 69 72 65 73 50 61 67 65 28 50 67 48 RequiresPage(PgH
11822 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 67 6e 6f dr *pPg){. Pgno
11823 20 70 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e pgno = pPg->pgn
11824 6f 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 o;. Pager *pPag
11825 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 er = pPg->pPager
11826 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 ;. int i;. for
11827 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 72 2d 3e (i=0; i<pPager->
11828 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 2b 2b 29 nSavepoint; i++)
11829 7b 0a 20 20 20 20 50 61 67 65 72 53 61 76 65 70 {. PagerSavep
1182a 6f 69 6e 74 20 2a 70 20 3d 20 26 70 50 61 67 65 oint *p = &pPage
1182b 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 5d r->aSavepoint[i]
1182c 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 4f 72 ;. if( p->nOr
1182d 69 67 3e 3d 70 67 6e 6f 20 26 26 20 30 3d 3d 73 ig>=pgno && 0==s
1182e 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 qlite3BitvecTest
1182f 28 70 2d 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 (p->pInSavepoint
11830 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 , pgno) ){.
11831 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
11832 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
11833 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
11834 20 74 72 75 65 20 69 66 20 74 68 65 20 70 61 67 true if the pag
11835 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 e is already in
11836 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11837 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
11838 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 50 67 pageInJournal(Pg
11839 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 Hdr *pPg){. ret
1183a 75 72 6e 20 73 71 6c 69 74 65 33 42 69 74 76 65 urn sqlite3Bitve
1183b 63 54 65 73 74 28 70 50 67 2d 3e 70 50 61 67 65 cTest(pPg->pPage
1183c 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 r->pInJournal, p
1183d 50 67 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a Pg->pgno);.}../*
1183e 0a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69 .** Read a 32-bi
1183f 74 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 74 t integer from t
11840 68 65 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 he given file de
11841 73 63 72 69 70 74 6f 72 2e 20 20 53 74 6f 72 65 scriptor. Store
11842 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 the integer.**
11843 74 68 61 74 20 69 73 20 72 65 61 64 20 69 6e 20 that is read in
11844 2a 70 52 65 73 2e 20 20 52 65 74 75 72 6e 20 53 *pRes. Return S
11845 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 QLITE_OK if ever
11846 79 74 68 69 6e 67 20 77 6f 72 6b 65 64 2c 20 6f ything worked, o
11847 72 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f r an.** error co
11848 64 65 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 de is something
11849 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a goes wrong..**.*
1184a 2a 20 41 6c 6c 20 76 61 6c 75 65 73 20 61 72 65 * All values are
1184b 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b 20 stored on disk
1184c 61 73 20 62 69 67 2d 65 6e 64 69 61 6e 2e 0a 2a as big-endian..*
1184d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 /.static int rea
1184e 64 33 32 62 69 74 73 28 73 71 6c 69 74 65 33 5f d32bits(sqlite3_
1184f 66 69 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 file *fd, i64 of
11850 66 73 65 74 2c 20 75 33 32 20 2a 70 52 65 73 29 fset, u32 *pRes)
11851 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 {. unsigned cha
11852 72 20 61 63 5b 34 5d 3b 0a 20 20 69 6e 74 20 72 r ac[4];. int r
11853 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 c = sqlite3OsRea
11854 64 28 66 64 2c 20 61 63 2c 20 73 69 7a 65 6f 66 d(fd, ac, sizeof
11855 28 61 63 29 2c 20 6f 66 66 73 65 74 29 3b 0a 20 (ac), offset);.
11856 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
11857 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 OK ){. *pRes
11858 3d 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 = sqlite3Get4byt
11859 65 28 61 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 e(ac);. }. ret
1185a 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1185b 20 57 72 69 74 65 20 61 20 33 32 2d 62 69 74 20 Write a 32-bit
1185c 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 61 20 73 integer into a s
1185d 74 72 69 6e 67 20 62 75 66 66 65 72 20 69 6e 20 tring buffer in
1185e 62 69 67 2d 65 6e 64 69 61 6e 20 62 79 74 65 20 big-endian byte
1185f 6f 72 64 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e order..*/.#defin
11860 65 20 70 75 74 33 32 62 69 74 73 28 41 2c 42 29 e put32bits(A,B)
11861 20 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 sqlite3Put4byt
11862 65 28 28 75 38 2a 29 41 2c 42 29 0a 0a 2f 2a 0a e((u8*)A,B)../*.
11863 2a 2a 20 57 72 69 74 65 20 61 20 33 32 2d 62 69 ** Write a 32-bi
11864 74 20 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 74 t integer into t
11865 68 65 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 he given file de
11866 73 63 72 69 70 74 6f 72 2e 20 20 52 65 74 75 72 scriptor. Retur
11867 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f n SQLITE_OK.** o
11868 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 n success or an
11869 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f error code is so
1186a 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f mething goes wro
1186b 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
1186c 74 20 77 72 69 74 65 33 32 62 69 74 73 28 73 71 t write32bits(sq
1186d 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 lite3_file *fd,
1186e 69 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20 i64 offset, u32
1186f 76 61 6c 29 7b 0a 20 20 63 68 61 72 20 61 63 5b val){. char ac[
11870 34 5d 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28 4];. put32bits(
11871 61 63 2c 20 76 61 6c 29 3b 0a 20 20 72 65 74 75 ac, val);. retu
11872 72 6e 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 rn sqlite3OsWrit
11873 65 28 66 64 2c 20 61 63 2c 20 34 2c 20 6f 66 66 e(fd, ac, 4, off
11874 73 65 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 set);.}../*.** T
11875 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 he argument to t
11876 68 69 73 20 6d 61 63 72 6f 20 69 73 20 61 20 66 his macro is a f
11877 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 28 ile descriptor (
11878 74 79 70 65 20 73 71 6c 69 74 65 33 5f 66 69 6c type sqlite3_fil
11879 65 2a 29 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30 e*)..** Return 0
1187a 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 6f 70 if it is not op
1187b 65 6e 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 20 en, or non-zero
1187c 28 62 75 74 20 6e 6f 74 20 31 29 20 69 66 20 69 (but not 1) if i
1187d 74 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 t is..**.** This
1187e 20 69 73 20 73 6f 20 74 68 61 74 20 65 78 70 72 is so that expr
1187f 65 73 73 69 6f 6e 73 20 63 61 6e 20 62 65 20 77 essions can be w
11880 72 69 74 74 65 6e 20 61 73 3a 0a 2a 2a 0a 2a 2a ritten as:.**.**
11881 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 if( isOpen(pP
11882 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 20 2e 2e ager->jfd) ){ ..
11883 2e 0a 2a 2a 0a 2a 2a 20 69 6e 73 74 65 61 64 20 ..**.** instead
11884 6f 66 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 70 of.**.** if( p
11885 50 61 67 65 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74 Pager->jfd->pMet
11886 68 6f 64 73 20 29 7b 20 2e 2e 2e 0a 2a 2f 0a 23 hods ){ ....*/.#
11887 64 65 66 69 6e 65 20 69 73 4f 70 65 6e 28 70 46 define isOpen(pF
11888 64 29 20 28 28 70 46 64 29 2d 3e 70 4d 65 74 68 d) ((pFd)->pMeth
11889 6f 64 73 29 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 66 ods)../*.** If f
1188a 69 6c 65 20 70 46 64 20 69 73 20 6f 70 65 6e 2c ile pFd is open,
1188b 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4f 73 55 call sqlite3OsU
1188c 6e 6c 6f 63 6b 28 29 20 6f 6e 20 69 74 2e 0a 2a nlock() on it..*
1188d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 55 /.static int osU
1188e 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 nlock(sqlite3_fi
1188f 6c 65 20 2a 70 46 64 2c 20 69 6e 74 20 65 4c 6f le *pFd, int eLo
11890 63 6b 29 7b 0a 20 20 69 66 28 20 21 69 73 4f 70 ck){. if( !isOp
11891 65 6e 28 70 46 64 29 20 29 7b 0a 20 20 20 20 72 en(pFd) ){. r
11892 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
11893 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 . }. return sq
11894 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 46 lite3OsUnlock(pF
11895 64 2c 20 65 4c 6f 63 6b 29 3b 0a 7d 0a 0a 2f 2a d, eLock);.}../*
11896 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
11897 6e 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 n determines whe
11898 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 ther or not the
11899 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 atomic-write opt
1189a 69 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6e imization.** can
1189b 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74 68 be used with th
1189c 69 73 20 70 61 67 65 72 2e 20 54 68 65 20 6f 70 is pager. The op
1189d 74 69 6d 69 7a 61 74 69 6f 6e 20 63 61 6e 20 62 timization can b
1189e 65 20 75 73 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a e used if:.**.**
1189f 20 20 28 61 29 20 74 68 65 20 76 61 6c 75 65 20 (a) the value
118a0 72 65 74 75 72 6e 65 64 20 62 79 20 4f 73 44 65 returned by OsDe
118a1 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
118a2 69 63 73 28 29 20 69 6e 64 69 63 61 74 65 73 20 ics() indicates
118a3 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 61 20 64 that.** a d
118a4 61 74 61 62 61 73 65 20 70 61 67 65 20 6d 61 79 atabase page may
118a5 20 62 65 20 77 72 69 74 74 65 6e 20 61 74 6f 6d be written atom
118a6 69 63 61 6c 6c 79 2c 20 61 6e 64 0a 2a 2a 20 20 ically, and.**
118a7 28 62 29 20 74 68 65 20 76 61 6c 75 65 20 72 65 (b) the value re
118a8 74 75 72 6e 65 64 20 62 79 20 4f 73 53 65 63 74 turned by OsSect
118a9 6f 72 53 69 7a 65 28 29 20 69 73 20 6c 65 73 73 orSize() is less
118aa 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a than or equal.*
118ab 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20 70 61 * to the pa
118ac 67 65 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54 ge size..**.** T
118ad 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 he optimization
118ae 69 73 20 61 6c 73 6f 20 61 6c 77 61 79 73 20 65 is also always e
118af 6e 61 62 6c 65 64 20 66 6f 72 20 74 65 6d 70 6f nabled for tempo
118b0 72 61 72 79 20 66 69 6c 65 73 2e 20 49 74 20 69 rary files. It i
118b1 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 74 6f s.** an error to
118b2 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 call this funct
118b3 69 6f 6e 20 69 66 20 70 50 61 67 65 72 20 69 73 ion if pPager is
118b4 20 6f 70 65 6e 65 64 20 6f 6e 20 61 6e 20 69 6e opened on an in
118b5 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 -memory.** datab
118b6 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ase..**.** If th
118b7 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63 e optimization c
118b8 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 2c 20 30 annot be used, 0
118b9 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 is returned. If
118ba 20 69 74 20 63 61 6e 20 62 65 20 75 73 65 64 2c it can be used,
118bb 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 61 6c .** then the val
118bc 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 ue returned is t
118bd 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a he size of the j
118be 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e ournal file when
118bf 20 69 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 it.** contains
118c0 72 6f 6c 6c 62 61 63 6b 20 64 61 74 61 20 66 6f rollback data fo
118c1 72 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 70 61 r exactly one pa
118c2 67 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ge..*/.#ifdef SQ
118c3 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d LITE_ENABLE_ATOM
118c4 49 43 5f 57 52 49 54 45 0a 73 74 61 74 69 63 20 IC_WRITE.static
118c5 69 6e 74 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 int jrnlBufferSi
118c6 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ze(Pager *pPager
118c7 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 4d 45 ){. assert( !ME
118c8 4d 44 42 20 29 3b 0a 20 20 69 66 28 20 21 70 50 MDB );. if( !pP
118c9 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 ager->tempFile )
118ca 7b 0a 20 20 20 20 69 6e 74 20 64 63 3b 20 20 20 {. int dc;
118cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
118cc 20 20 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63 /* Devic
118cd 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 e characteristic
118ce 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 65 s */. int nSe
118cf 63 74 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 ctor;
118d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 /* Se
118d1 63 74 6f 72 20 73 69 7a 65 20 2a 2f 0a 20 20 20 ctor size */.
118d2 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 int szPage;
118d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
118d4 20 20 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 /* Page size
118d5 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 */.. assert(
118d6 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 isOpen(pPager->f
118d7 64 29 20 29 3b 0a 20 20 20 20 64 63 20 3d 20 73 d) );. dc = s
118d8 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 qlite3OsDeviceCh
118d9 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 aracteristics(pP
118da 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 6e ager->fd);. n
118db 53 65 63 74 6f 72 20 3d 20 70 50 61 67 65 72 2d Sector = pPager-
118dc 3e 73 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 20 >sectorSize;.
118dd 20 73 7a 50 61 67 65 20 3d 20 70 50 61 67 65 72 szPage = pPager
118de 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 0a 20 20 20 ->pageSize;..
118df 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 assert(SQLITE_I
118e0 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d 3d OCAP_ATOMIC512==
118e1 28 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 61 (512>>8));. a
118e2 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 ssert(SQLITE_IOC
118e3 41 50 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 AP_ATOMIC64K==(6
118e4 35 35 33 36 3e 3e 38 29 29 3b 0a 20 20 20 20 69 5536>>8));. i
118e5 66 28 20 30 3d 3d 28 64 63 26 28 53 51 4c 49 54 f( 0==(dc&(SQLIT
118e6 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 E_IOCAP_ATOMIC|(
118e7 73 7a 50 61 67 65 3e 3e 38 29 29 20 7c 7c 20 6e szPage>>8)) || n
118e8 53 65 63 74 6f 72 3e 73 7a 50 61 67 65 29 20 29 Sector>szPage) )
118e9 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 {. return 0
118ea 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 ;. }. }.. r
118eb 65 74 75 72 6e 20 4a 4f 55 52 4e 41 4c 5f 48 44 eturn JOURNAL_HD
118ec 52 5f 53 5a 28 70 50 61 67 65 72 29 20 2b 20 4a R_SZ(pPager) + J
118ed 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 OURNAL_PG_SZ(pPa
118ee 67 65 72 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a ger);.}.#endif..
118ef 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f /*.** If SQLITE_
118f0 43 48 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64 CHECK_PAGES is d
118f1 65 66 69 6e 65 64 20 74 68 65 6e 20 77 65 20 64 efined then we d
118f2 6f 20 73 6f 6d 65 20 73 61 6e 69 74 79 20 63 68 o some sanity ch
118f3 65 63 6b 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65 ecking.** on the
118f4 20 63 61 63 68 65 20 75 73 69 6e 67 20 61 20 68 cache using a h
118f5 61 73 68 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 ash function. T
118f6 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 his is used for
118f7 74 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 testing.** and d
118f8 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a ebugging only..*
118f9 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
118fa 43 48 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 0a 2a CHECK_PAGES./*.*
118fb 2a 20 52 65 74 75 72 6e 20 61 20 33 32 2d 62 69 * Return a 32-bi
118fc 74 20 68 61 73 68 20 6f 66 20 74 68 65 20 70 61 t hash of the pa
118fd 67 65 20 64 61 74 61 20 66 6f 72 20 70 50 61 67 ge data for pPag
118fe 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 e..*/.static u32
118ff 20 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 pager_datahash(
11900 69 6e 74 20 6e 42 79 74 65 2c 20 75 6e 73 69 67 int nByte, unsig
11901 6e 65 64 20 63 68 61 72 20 2a 70 44 61 74 61 29 ned char *pData)
11902 7b 0a 20 20 75 33 32 20 68 61 73 68 20 3d 20 30 {. u32 hash = 0
11903 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 ;. int i;. for
11904 28 69 3d 30 3b 20 69 3c 6e 42 79 74 65 3b 20 69 (i=0; i<nByte; i
11905 2b 2b 29 7b 0a 20 20 20 20 68 61 73 68 20 3d 20 ++){. hash =
11906 28 68 61 73 68 2a 31 30 33 39 29 20 2b 20 70 44 (hash*1039) + pD
11907 61 74 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 ata[i];. }. re
11908 74 75 72 6e 20 68 61 73 68 3b 0a 7d 0a 73 74 61 turn hash;.}.sta
11909 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 70 61 tic u32 pager_pa
1190a 67 65 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 gehash(PgHdr *pP
1190b 61 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 age){. return p
1190c 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 ager_datahash(pP
1190d 61 67 65 2d 3e 70 50 61 67 65 72 2d 3e 70 61 67 age->pPager->pag
1190e 65 53 69 7a 65 2c 20 28 75 6e 73 69 67 6e 65 64 eSize, (unsigned
1190f 20 63 68 61 72 20 2a 29 70 50 61 67 65 2d 3e 70 char *)pPage->p
11910 44 61 74 61 29 3b 0a 7d 0a 73 74 61 74 69 63 20 Data);.}.static
11911 76 6f 69 64 20 70 61 67 65 72 5f 73 65 74 5f 70 void pager_set_p
11912 61 67 65 68 61 73 68 28 50 67 48 64 72 20 2a 70 agehash(PgHdr *p
11913 50 61 67 65 29 7b 0a 20 20 70 50 61 67 65 2d 3e Page){. pPage->
11914 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 pageHash = pager
11915 5f 70 61 67 65 68 61 73 68 28 70 50 61 67 65 29 _pagehash(pPage)
11916 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 ;.}../*.** The C
11917 48 45 43 4b 5f 50 41 47 45 20 6d 61 63 72 6f 20 HECK_PAGE macro
11918 74 61 6b 65 73 20 61 20 50 67 48 64 72 2a 20 61 takes a PgHdr* a
11919 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49 s an argument. I
1191a 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 f SQLITE_CHECK_P
1191b 41 47 45 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e AGES.** is defin
1191c 65 64 2c 20 61 6e 64 20 4e 44 45 42 55 47 20 69 ed, and NDEBUG i
1191d 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2c 20 61 s not defined, a
1191e 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 n assert() state
1191f 6d 65 6e 74 20 63 68 65 63 6b 73 0a 2a 2a 20 74 ment checks.** t
11920 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20 hat the page is
11921 65 69 74 68 65 72 20 64 69 72 74 79 20 6f 72 20 either dirty or
11922 73 74 69 6c 6c 20 6d 61 74 63 68 65 73 20 74 68 still matches th
11923 65 20 63 61 6c 63 75 6c 61 74 65 64 20 70 61 67 e calculated pag
11924 65 2d 68 61 73 68 2e 0a 2a 2f 0a 23 64 65 66 69 e-hash..*/.#defi
11925 6e 65 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29 ne CHECK_PAGE(x)
11926 20 63 68 65 63 6b 50 61 67 65 28 78 29 0a 73 74 checkPage(x).st
11927 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 atic void checkP
11928 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b age(PgHdr *pPg){
11929 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
1192a 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a = pPg->pPager;.
1192b 20 20 61 73 73 65 72 74 28 20 21 70 50 67 2d 3e assert( !pPg->
1192c 70 61 67 65 48 61 73 68 20 7c 7c 20 70 50 61 67 pageHash || pPag
1192d 65 72 2d 3e 65 72 72 43 6f 64 65 0a 20 20 20 20 er->errCode.
1192e 20 20 7c 7c 20 28 70 50 67 2d 3e 66 6c 61 67 73 || (pPg->flags
1192f 26 50 47 48 44 52 5f 44 49 52 54 59 29 20 7c 7c &PGHDR_DIRTY) ||
11930 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 3d 3d pPg->pageHash==
11931 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 pager_pagehash(p
11932 50 67 29 20 29 3b 0a 7d 0a 0a 23 65 6c 73 65 0a Pg) );.}..#else.
11933 23 64 65 66 69 6e 65 20 70 61 67 65 72 5f 64 61 #define pager_da
11934 74 61 68 61 73 68 28 58 2c 59 29 20 20 30 0a 23 tahash(X,Y) 0.#
11935 64 65 66 69 6e 65 20 70 61 67 65 72 5f 70 61 67 define pager_pag
11936 65 68 61 73 68 28 58 29 20 20 30 0a 23 64 65 66 ehash(X) 0.#def
11937 69 6e 65 20 43 48 45 43 4b 5f 50 41 47 45 28 78 ine CHECK_PAGE(x
11938 29 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c ).#endif /* SQL
11939 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 ITE_CHECK_PAGES
1193a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 */../*.** When t
1193b 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 74 68 his is called th
1193c 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 e journal file f
1193d 6f 72 20 70 61 67 65 72 20 70 50 61 67 65 72 20 or pager pPager
1193e 6d 75 73 74 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a must be open..**
1193f 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 This function a
11940 74 74 65 6d 70 74 73 20 74 6f 20 72 65 61 64 20 ttempts to read
11941 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
11942 20 66 69 6c 65 20 6e 61 6d 65 20 66 72 6f 6d 20 file name from
11943 74 68 65 20 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 the .** end of t
11944 68 65 20 66 69 6c 65 20 61 6e 64 2c 20 69 66 20 he file and, if
11945 73 75 63 63 65 73 73 66 75 6c 2c 20 63 6f 70 69 successful, copi
11946 65 73 20 69 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 es it into memor
11947 79 20 73 75 70 70 6c 69 65 64 20 0a 2a 2a 20 62 y supplied .** b
11948 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 53 65 y the caller. Se
11949 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 e comments above
1194a 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 writeMasterJour
1194b 6e 61 6c 28 29 20 66 6f 72 20 74 68 65 20 66 6f nal() for the fo
1194c 72 6d 61 74 0a 2a 2a 20 75 73 65 64 20 74 6f 20 rmat.** used to
1194d 73 74 6f 72 65 20 61 20 6d 61 73 74 65 72 20 6a store a master j
1194e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 ournal file name
1194f 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 at the end of a
11950 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a journal file..*
11951 2a 0a 2a 2a 20 7a 4d 61 73 74 65 72 20 6d 75 73 *.** zMaster mus
11952 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 t point to a buf
11953 66 65 72 20 6f 66 20 61 74 20 6c 65 61 73 74 20 fer of at least
11954 6e 4d 61 73 74 65 72 20 62 79 74 65 73 20 61 6c nMaster bytes al
11955 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 located by.** th
11956 65 20 63 61 6c 6c 65 72 2e 20 54 68 69 73 20 73 e caller. This s
11957 68 6f 75 6c 64 20 62 65 20 73 71 6c 69 74 65 33 hould be sqlite3
11958 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 2b _vfs.mxPathname+
11959 31 20 28 74 6f 20 65 6e 73 75 72 65 20 74 68 65 1 (to ensure the
1195a 72 65 20 69 73 0a 2a 2a 20 65 6e 6f 75 67 68 20 re is.** enough
1195b 73 70 61 63 65 20 74 6f 20 77 72 69 74 65 20 74 space to write t
1195c 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
1195d 6c 20 6e 61 6d 65 29 2e 20 49 66 20 74 68 65 20 l name). If the
1195e 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a master journal.*
1195f 2a 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 6a 6f * name in the jo
11960 75 72 6e 61 6c 20 69 73 20 6c 6f 6e 67 65 72 20 urnal is longer
11961 74 68 61 6e 20 6e 4d 61 73 74 65 72 20 62 79 74 than nMaster byt
11962 65 73 20 28 69 6e 63 6c 75 64 69 6e 67 20 61 0a es (including a.
11963 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f ** nul-terminato
11964 72 29 2c 20 74 68 65 6e 20 74 68 69 73 20 69 73 r), then this is
11965 20 68 61 6e 64 6c 65 64 20 61 73 20 69 66 20 6e handled as if n
11966 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c o master journal
11967 20 6e 61 6d 65 0a 2a 2a 20 77 65 72 65 20 70 72 name.** were pr
11968 65 73 65 6e 74 20 69 6e 20 74 68 65 20 6a 6f 75 esent in the jou
11969 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 rnal..**.** If a
1196a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1196b 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65 file name is pre
1196c 73 65 6e 74 20 61 74 20 74 68 65 20 65 6e 64 20 sent at the end
1196d 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a of the journal.*
1196e 2a 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 * file, then it
1196f 69 73 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 is copied into t
11970 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 he buffer pointe
11971 64 20 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 2e d to by zMaster.
11972 20 41 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e A.** nul-termin
11973 61 74 6f 72 20 62 79 74 65 20 69 73 20 61 70 70 ator byte is app
11974 65 6e 64 65 64 20 74 6f 20 74 68 65 20 62 75 66 ended to the buf
11975 66 65 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 fer following th
11976 65 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 e master.** jour
11977 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 2e 0a 2a nal file name..*
11978 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 64 65 *.** If it is de
11979 74 65 72 6d 69 6e 65 64 20 74 68 61 74 20 6e 6f termined that no
1197a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1197b 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65 file name is pre
1197c 73 65 6e 74 20 0a 2a 2a 20 7a 4d 61 73 74 65 72 sent .** zMaster
1197d 5b 30 5d 20 69 73 20 73 65 74 20 74 6f 20 30 20 [0] is set to 0
1197e 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 and SQLITE_OK re
1197f 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 turned..**.** If
11980 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
11981 20 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20 66 while reading f
11982 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 rom the journal
11983 66 69 6c 65 2c 20 61 6e 20 53 51 4c 69 74 65 0a file, an SQLite.
11984 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 ** error code is
11985 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 returned..*/.st
11986 61 74 69 63 20 69 6e 74 20 72 65 61 64 4d 61 73 atic int readMas
11987 74 65 72 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 terJournal(sqlit
11988 65 33 5f 66 69 6c 65 20 2a 70 4a 72 6e 6c 2c 20 e3_file *pJrnl,
11989 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 75 char *zMaster, u
1198a 33 32 20 6e 4d 61 73 74 65 72 29 7b 0a 20 20 69 32 nMaster){. i
1198b 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
1198c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
1198d 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 33 urn code */. u3
1198e 32 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 2 len;
1198f 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 /* Leng
11990 74 68 20 69 6e 20 62 79 74 65 73 20 6f 66 20 6d th in bytes of m
11991 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 aster journal na
11992 6d 65 20 2a 2f 0a 20 20 69 36 34 20 73 7a 4a 3b me */. i64 szJ;
11993 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11994 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65 /* Total size
11995 20 69 6e 20 62 79 74 65 73 20 6f 66 20 6a 6f 75 in bytes of jou
11996 72 6e 61 6c 20 66 69 6c 65 20 70 4a 72 6e 6c 20 rnal file pJrnl
11997 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20 */. u32 cksum;
11998 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11999 2f 2a 20 4d 4a 20 63 68 65 63 6b 73 75 6d 20 76 /* MJ checksum v
1199a 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 6a alue read from j
1199b 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 ournal */. u32
1199c 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 u;
1199d 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e /* Unsign
1199e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 ed loop counter
1199f 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
119a0 61 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20 ar aMagic[8];
119a1 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 /* A buffer to h
119a2 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 20 68 65 old the magic he
119a3 61 64 65 72 20 2a 2f 0a 20 20 7a 4d 61 73 74 65 ader */. zMaste
119a4 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 r[0] = '\0';..
119a5 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 if( SQLITE_OK!=(
119a6 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 rc = sqlite3OsFi
119a7 6c 65 53 69 7a 65 28 70 4a 72 6e 6c 2c 20 26 73 leSize(pJrnl, &s
119a8 7a 4a 29 29 0a 20 20 20 7c 7c 20 73 7a 4a 3c 31 zJ)). || szJ<1
119a9 36 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 6. || SQLITE_O
119aa 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 K!=(rc = read32b
119ab 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 its(pJrnl, szJ-1
119ac 36 2c 20 26 6c 65 6e 29 29 0a 20 20 20 7c 7c 20 6, &len)). ||
119ad 6c 65 6e 3e 3d 6e 4d 61 73 74 65 72 20 0a 20 20 len>=nMaster .
119ae 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 || SQLITE_OK!=(
119af 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
119b0 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 32 2c 20 26 pJrnl, szJ-12, &
119b1 63 6b 73 75 6d 29 29 0a 20 20 20 7c 7c 20 53 51 cksum)). || SQ
119b2 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 LITE_OK!=(rc = s
119b3 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 4a 72 qlite3OsRead(pJr
119b4 6e 6c 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 73 nl, aMagic, 8, s
119b5 7a 4a 2d 38 29 29 0a 20 20 20 7c 7c 20 6d 65 6d zJ-8)). || mem
119b6 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 cmp(aMagic, aJou
119b7 72 6e 61 6c 4d 61 67 69 63 2c 20 38 29 0a 20 20 rnalMagic, 8).
119b8 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 || SQLITE_OK!=(
119b9 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 rc = sqlite3OsRe
119ba 61 64 28 70 4a 72 6e 6c 2c 20 7a 4d 61 73 74 65 ad(pJrnl, zMaste
119bb 72 2c 20 6c 65 6e 2c 20 73 7a 4a 2d 31 36 2d 6c r, len, szJ-16-l
119bc 65 6e 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 en)). ){. re
119bd 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 turn rc;. }..
119be 2f 2a 20 53 65 65 20 69 66 20 74 68 65 20 63 68 /* See if the ch
119bf 65 63 6b 73 75 6d 20 6d 61 74 63 68 65 73 20 74 ecksum matches t
119c0 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
119c1 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 6f 72 28 l name */. for(
119c2 75 3d 30 3b 20 75 3c 6c 65 6e 3b 20 75 2b 2b 29 u=0; u<len; u++)
119c3 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2d 3d 20 7a {. cksum -= z
119c4 4d 61 73 74 65 72 5b 75 5d 3b 0a 20 20 7d 0a 20 Master[u];. }.
119c5 20 69 66 28 20 63 6b 73 75 6d 20 29 7b 0a 20 20 if( cksum ){.
119c6 20 20 2f 2a 20 49 66 20 74 68 65 20 63 68 65 63 /* If the chec
119c7 6b 73 75 6d 20 64 6f 65 73 6e 27 74 20 61 64 64 ksum doesn't add
119c8 20 75 70 2c 20 74 68 65 6e 20 6f 6e 65 20 6f 72 up, then one or
119c9 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 64 69 73 more of the dis
119ca 6b 20 73 65 63 74 6f 72 73 0a 20 20 20 20 2a 2a k sectors. **
119cb 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
119cc 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
119cd 69 6c 65 6e 61 6d 65 20 69 73 20 63 6f 72 72 75 ilename is corru
119ce 70 74 65 64 2e 20 54 68 69 73 20 6d 65 61 6e 73 pted. This means
119cf 0a 20 20 20 20 2a 2a 20 64 65 66 69 6e 69 74 65 . ** definite
119d0 6c 79 20 72 6f 6c 6c 20 62 61 63 6b 2c 20 73 6f ly roll back, so
119d1 20 6a 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c just return SQL
119d2 49 54 45 5f 4f 4b 20 61 6e 64 20 72 65 70 6f 72 ITE_OK and repor
119d3 74 20 61 20 28 6e 75 6c 29 0a 20 20 20 20 2a 2a t a (nul). **
119d4 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 master-journal
119d5 66 69 6c 65 6e 61 6d 65 2e 0a 20 20 20 20 2a 2f filename.. */
119d6 0a 20 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 . len = 0;.
119d7 7d 0a 20 20 7a 4d 61 73 74 65 72 5b 6c 65 6e 5d }. zMaster[len]
119d8 20 3d 20 27 5c 30 27 3b 0a 20 20 20 0a 20 20 72 = '\0';. . r
119d9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
119da 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
119db 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74 the offset of t
119dc 68 65 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 he sector bounda
119dd 72 79 20 61 74 20 6f 72 20 69 6d 6d 65 64 69 61 ry at or immedia
119de 74 65 6c 79 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 tely .** followi
119df 6e 67 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 ng the value in
119e0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
119e1 66 66 2c 20 61 73 73 75 6d 69 6e 67 20 61 20 73 ff, assuming a s
119e2 65 63 74 6f 72 20 0a 2a 2a 20 73 69 7a 65 20 6f ector .** size o
119e3 66 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 f pPager->sector
119e4 53 69 7a 65 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a Size bytes..**.*
119e5 2a 20 69 2e 65 20 66 6f 72 20 61 20 73 65 63 74 * i.e for a sect
119e6 6f 72 20 73 69 7a 65 20 6f 66 20 35 31 32 3a 0a or size of 512:.
119e7 2a 2a 0a 2a 2a 20 20 20 50 61 67 65 72 2e 6a 6f **.** Pager.jo
119e8 75 72 6e 61 6c 4f 66 66 20 20 20 20 20 20 20 20 urnalOff
119e9 20 20 52 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a Return value.*
119ea 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d * ------------
119eb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
119ec 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 -----------.**
119ed 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
119ee 20 20 20 20 20 20 20 20 20 20 20 30 0a 2a 2a 20 0.**
119ef 20 20 35 31 32 20 20 20 20 20 20 20 20 20 20 20 512
119f0 20 20 20 20 20 20 20 20 20 20 20 20 35 31 32 0a 512.
119f1 2a 2a 20 20 20 31 30 30 20 20 20 20 20 20 20 20 ** 100
119f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
119f3 31 32 0a 2a 2a 20 20 20 32 30 30 30 20 20 20 20 12.** 2000
119f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
119f5 20 20 32 30 34 38 0a 2a 2a 20 0a 2a 2f 0a 73 74 2048.** .*/.st
119f6 61 74 69 63 20 69 36 34 20 6a 6f 75 72 6e 61 6c atic i64 journal
119f7 48 64 72 4f 66 66 73 65 74 28 50 61 67 65 72 20 HdrOffset(Pager
119f8 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 36 34 20 *pPager){. i64
119f9 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 69 36 offset = 0;. i6
119fa 34 20 63 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 4 c = pPager->jo
119fb 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 69 66 28 20 urnalOff;. if(
119fc 63 20 29 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 c ){. offset
119fd 3d 20 28 28 63 2d 31 29 2f 4a 4f 55 52 4e 41 4c = ((c-1)/JOURNAL
119fe 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 _HDR_SZ(pPager)
119ff 2b 20 31 29 20 2a 20 4a 4f 55 52 4e 41 4c 5f 48 + 1) * JOURNAL_H
11a00 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 DR_SZ(pPager);.
11a01 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66 }. assert( off
11a02 73 65 74 25 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f set%JOURNAL_HDR_
11a03 53 5a 28 70 50 61 67 65 72 29 3d 3d 30 20 29 3b SZ(pPager)==0 );
11a04 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 . assert( offse
11a05 74 3e 3d 63 20 29 3b 0a 20 20 61 73 73 65 72 74 t>=c );. assert
11a06 28 20 28 6f 66 66 73 65 74 2d 63 29 3c 4a 4f 55 ( (offset-c)<JOU
11a07 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
11a08 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 er) );. return
11a09 6f 66 66 73 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a offset;.}../*.**
11a0a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c The journal fil
11a0b 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 77 e must be open w
11a0c 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
11a0d 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a n is called..**.
11a0e 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
11a0f 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 is a no-op if t
11a10 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11a11 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 77 72 69 has not been wri
11a12 74 74 65 6e 20 74 6f 0a 2a 2a 20 77 69 74 68 69 tten to.** withi
11a13 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 n the current tr
11a14 61 6e 73 61 63 74 69 6f 6e 20 28 69 2e 65 2e 20 ansaction (i.e.
11a15 69 66 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c if Pager.journal
11a16 4f 66 66 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 49 Off==0)..**.** I
11a17 66 20 64 6f 54 72 75 6e 63 61 74 65 20 69 73 20 f doTruncate is
11a18 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 20 74 68 65 20 non-zero or the
11a19 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a Pager.journalSiz
11a1a 65 4c 69 6d 69 74 20 76 61 72 69 61 62 6c 65 20 eLimit variable
11a1b 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 2c 20 is.** set to 0,
11a1c 74 68 65 6e 20 74 72 75 6e 63 61 74 65 20 74 68 then truncate th
11a1d 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 e journal file t
11a1e 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69 6e 20 o zero bytes in
11a1f 73 69 7a 65 2e 20 4f 74 68 65 72 77 69 73 65 2c size. Otherwise,
11a20 0a 2a 2a 20 7a 65 72 6f 20 74 68 65 20 32 38 2d .** zero the 28-
11a21 62 79 74 65 20 68 65 61 64 65 72 20 61 74 20 74 byte header at t
11a22 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 he start of the
11a23 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 6e journal file. In
11a24 20 65 69 74 68 65 72 20 63 61 73 65 2c 20 0a 2a either case, .*
11a25 2a 20 69 66 20 74 68 65 20 70 61 67 65 72 20 69 * if the pager i
11a26 73 20 6e 6f 74 20 69 6e 20 6e 6f 2d 73 79 6e 63 s not in no-sync
11a27 20 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 20 mode, sync the
11a28 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6d 6d journal file imm
11a29 65 64 69 61 74 65 6c 79 20 0a 2a 2a 20 61 66 74 ediately .** aft
11a2a 65 72 20 77 72 69 74 69 6e 67 20 6f 72 20 74 72 er writing or tr
11a2b 75 6e 63 61 74 69 6e 67 20 69 74 2e 0a 2a 2a 0a uncating it..**.
11a2c 2a 2a 20 49 66 20 50 61 67 65 72 2e 6a 6f 75 72 ** If Pager.jour
11a2d 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 69 73 20 nalSizeLimit is
11a2e 73 65 74 20 74 6f 20 61 20 70 6f 73 69 74 69 76 set to a positiv
11a2f 65 2c 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 e, non-zero valu
11a30 65 2c 20 61 6e 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 e, and.** follow
11a31 69 6e 67 20 74 68 65 20 74 72 75 6e 63 61 74 69 ing the truncati
11a32 6f 6e 20 6f 72 20 7a 65 72 6f 69 6e 67 20 64 65 on or zeroing de
11a33 73 63 72 69 62 65 64 20 61 62 6f 76 65 20 74 68 scribed above th
11a34 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 0a 2a e size of the .*
11a35 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 * journal file i
11a36 6e 20 62 79 74 65 73 20 69 73 20 6c 61 72 67 65 n bytes is large
11a37 72 20 74 68 61 6e 20 74 68 69 73 20 76 61 6c 75 r than this valu
11a38 65 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 e, then truncate
11a39 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 the.** journal
11a3a 66 69 6c 65 20 74 6f 20 50 61 67 65 72 2e 6a 6f file to Pager.jo
11a3b 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 62 urnalSizeLimit b
11a3c 79 74 65 73 2e 20 54 68 65 20 6a 6f 75 72 6e 61 ytes. The journa
11a3d 6c 20 66 69 6c 65 20 64 6f 65 73 0a 2a 2a 20 6e l file does.** n
11a3e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 73 79 ot need to be sy
11a3f 6e 63 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 nced following t
11a40 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a his operation..*
11a41 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 *.** If an IO er
11a42 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 62 61 6e ror occurs, aban
11a43 64 6f 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 don processing a
11a44 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 49 4f nd return the IO
11a45 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 20 error code..**
11a46 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 Otherwise, retur
11a47 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a n SQLITE_OK..*/.
11a48 73 74 61 74 69 63 20 69 6e 74 20 7a 65 72 6f 4a static int zeroJ
11a49 6f 75 72 6e 61 6c 48 64 72 28 50 61 67 65 72 20 ournalHdr(Pager
11a4a 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 64 6f 54 *pPager, int doT
11a4b 72 75 6e 63 61 74 65 29 7b 0a 20 20 69 6e 74 20 runcate){. int
11a4c 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
11a4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11a4f 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
11a50 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e assert( isOpen
11a51 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b (pPager->jfd) );
11a52 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a . if( pPager->j
11a53 6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 ournalOff ){.
11a54 20 63 6f 6e 73 74 20 69 36 34 20 69 4c 69 6d 69 const i64 iLimi
11a55 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 t = pPager->jour
11a56 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20 nalSizeLimit;
11a57 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 /* Local cache
11a58 6f 66 20 6a 73 6c 20 2a 2f 0a 0a 20 20 20 20 49 of jsl */.. I
11a59 4f 54 52 41 43 45 28 28 22 4a 5a 45 52 4f 48 44 OTRACE(("JZEROHD
11a5a 52 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 R %p\n", pPager)
11a5b 29 0a 20 20 20 20 69 66 28 20 64 6f 54 72 75 6e ). if( doTrun
11a5c 63 61 74 65 20 7c 7c 20 69 4c 69 6d 69 74 3d 3d cate || iLimit==
11a5d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
11a5e 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 sqlite3OsTruncat
11a5f 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 30 e(pPager->jfd, 0
11a60 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
11a61 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 static const
11a62 20 63 68 61 72 20 7a 65 72 6f 48 64 72 5b 32 38 char zeroHdr[28
11a63 5d 20 3d 20 7b 30 7d 3b 0a 20 20 20 20 20 20 72 ] = {0};. r
11a64 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
11a65 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 te(pPager->jfd,
11a66 7a 65 72 6f 48 64 72 2c 20 73 69 7a 65 6f 66 28 zeroHdr, sizeof(
11a67 7a 65 72 6f 48 64 72 29 2c 20 30 29 3b 0a 20 20 zeroHdr), 0);.
11a68 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d }. if( rc==
11a69 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 50 SQLITE_OK && !pP
11a6a 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a ager->noSync ){.
11a6b 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
11a6c 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d e3OsSync(pPager-
11a6d 3e 6a 66 64 2c 20 53 51 4c 49 54 45 5f 53 59 4e >jfd, SQLITE_SYN
11a6e 43 5f 44 41 54 41 4f 4e 4c 59 7c 70 50 61 67 65 C_DATAONLY|pPage
11a6f 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a r->sync_flags);.
11a70 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 }.. /* At
11a71 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 this point the
11a72 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 transaction is c
11a73 6f 6d 6d 69 74 74 65 64 20 62 75 74 20 74 68 65 ommitted but the
11a74 20 77 72 69 74 65 20 6c 6f 63 6b 20 0a 20 20 20 write lock .
11a75 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 68 65 6c ** is still hel
11a76 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 49 d on the file. I
11a77 66 20 74 68 65 72 65 20 69 73 20 61 20 73 69 7a f there is a siz
11a78 65 20 6c 69 6d 69 74 20 63 6f 6e 66 69 67 75 72 e limit configur
11a79 65 64 20 66 6f 72 20 0a 20 20 20 20 2a 2a 20 74 ed for . ** t
11a7a 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f he persistent jo
11a7b 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 20 6a 6f urnal and the jo
11a7c 75 72 6e 61 6c 20 66 69 6c 65 20 63 75 72 72 65 urnal file curre
11a7d 6e 74 6c 79 20 63 6f 6e 73 75 6d 65 73 20 6d 6f ntly consumes mo
11a7e 72 65 0a 20 20 20 20 2a 2a 20 73 70 61 63 65 20 re. ** space
11a7f 74 68 61 6e 20 74 68 61 74 20 6c 69 6d 69 74 20 than that limit
11a80 61 6c 6c 6f 77 73 20 66 6f 72 2c 20 74 72 75 6e allows for, trun
11a81 63 61 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 cate it now. The
11a82 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 re is no need.
11a83 20 20 2a 2a 20 74 6f 20 73 79 6e 63 20 74 68 65 ** to sync the
11a84 20 66 69 6c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 file following
11a85 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a this operation..
11a86 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 */. if( r
11a87 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
11a88 69 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 20 20 20 iLimit>0 ){.
11a89 20 20 69 36 34 20 73 7a 3b 0a 20 20 20 20 20 20 i64 sz;.
11a8a 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 rc = sqlite3OsFi
11a8b 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a leSize(pPager->j
11a8c 66 64 2c 20 26 73 7a 29 3b 0a 20 20 20 20 20 20 fd, &sz);.
11a8d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
11a8e 4b 20 26 26 20 73 7a 3e 69 4c 69 6d 69 74 20 29 K && sz>iLimit )
11a8f 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
11a90 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 qlite3OsTruncate
11a91 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 4c (pPager->jfd, iL
11a92 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 imit);. }.
11a93 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
11a94 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
11a95 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11a96 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 must be open whe
11a97 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 n this routine i
11a98 73 20 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 s called. A jour
11a99 6e 61 6c 0a 2a 2a 20 68 65 61 64 65 72 20 28 4a nal.** header (J
11a9a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 OURNAL_HDR_SZ by
11a9b 74 65 73 29 20 69 73 20 77 72 69 74 74 65 6e 20 tes) is written
11a9c 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c into the journal
11a9d 20 66 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a 20 file at the.**
11a9e 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e current location
11a9f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d ..**.** The form
11aa0 61 74 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e at for the journ
11aa1 61 6c 20 68 65 61 64 65 72 20 69 73 20 61 73 20 al header is as
11aa2 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 2d 20 38 20 follows:.** - 8
11aa3 62 79 74 65 73 3a 20 4d 61 67 69 63 20 69 64 65 bytes: Magic ide
11aa4 6e 74 69 66 79 69 6e 67 20 6a 6f 75 72 6e 61 6c ntifying journal
11aa5 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20 2d 20 34 20 format..** - 4
11aa6 62 79 74 65 73 3a 20 4e 75 6d 62 65 72 20 6f 66 bytes: Number of
11aa7 20 72 65 63 6f 72 64 73 20 69 6e 20 6a 6f 75 72 records in jour
11aa8 6e 61 6c 2c 20 6f 72 20 2d 31 20 6e 6f 2d 73 79 nal, or -1 no-sy
11aa9 6e 63 20 6d 6f 64 65 20 69 73 20 6f 6e 2e 0a 2a nc mode is on..*
11aaa 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 52 61 6e * - 4 bytes: Ran
11aab 64 6f 6d 20 6e 75 6d 62 65 72 20 75 73 65 64 20 dom number used
11aac 66 6f 72 20 70 61 67 65 20 68 61 73 68 2e 0a 2a for page hash..*
11aad 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 49 6e 69 * - 4 bytes: Ini
11aae 74 69 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 tial database pa
11aaf 67 65 20 63 6f 75 6e 74 2e 0a 2a 2a 20 2d 20 34 ge count..** - 4
11ab0 20 62 79 74 65 73 3a 20 53 65 63 74 6f 72 20 73 bytes: Sector s
11ab1 69 7a 65 20 75 73 65 64 20 62 79 20 74 68 65 20 ize used by the
11ab2 70 72 6f 63 65 73 73 20 74 68 61 74 20 77 72 6f process that wro
11ab3 74 65 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e te this journal.
11ab4 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 44 .** - 4 bytes: D
11ab5 61 74 61 62 61 73 65 20 70 61 67 65 20 73 69 7a atabase page siz
11ab6 65 2e 0a 2a 2a 20 0a 2a 2a 20 46 6f 6c 6c 6f 77 e..** .** Follow
11ab7 65 64 20 62 79 20 28 4a 4f 55 52 4e 41 4c 5f 48 ed by (JOURNAL_H
11ab8 44 52 5f 53 5a 20 2d 20 32 38 29 20 62 79 74 65 DR_SZ - 28) byte
11ab9 73 20 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63 s of unused spac
11aba 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
11abb 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 writeJournalHdr
11abc 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
11abd 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
11abe 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 TE_OK;
11abf 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
11ac0 20 63 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 code */. char
11ac1 2a 7a 48 65 61 64 65 72 20 3d 20 70 50 61 67 65 *zHeader = pPage
11ac2 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 20 20 2f r->pTmpSpace; /
11ac3 2a 20 54 65 6d 70 6f 72 61 72 79 20 73 70 61 63 * Temporary spac
11ac4 65 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20 e used to build
11ac5 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 header */. u32
11ac6 6e 48 65 61 64 65 72 20 3d 20 70 50 61 67 65 72 nHeader = pPager
11ac7 2d 3e 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 ->pageSize;
11ac8 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 /* Size of buffe
11ac9 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 r pointed to by
11aca 7a 48 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 zHeader */. u32
11acb 20 6e 57 72 69 74 65 3b 20 20 20 20 20 20 20 20 nWrite;
11acc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11acd 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 68 65 61 /* Bytes of hea
11ace 64 65 72 20 73 65 63 74 6f 72 20 77 72 69 74 74 der sector writt
11acf 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 69 3b 20 en */. int ii;
11ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11ad1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
11ad2 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a oop counter */..
11ad3 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e assert( isOpen
11ad4 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b (pPager->jfd) );
11ad5 20 20 20 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c /* Journal
11ad6 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 file must be op
11ad7 65 6e 2e 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 48 en. */.. if( nH
11ad8 65 61 64 65 72 3e 4a 4f 55 52 4e 41 4c 5f 48 44 eader>JOURNAL_HD
11ad9 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a R_SZ(pPager) ){.
11ada 20 20 20 20 6e 48 65 61 64 65 72 20 3d 20 4a 4f nHeader = JO
11adb 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
11adc 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 ger);. }.. /*
11add 49 66 20 74 68 65 72 65 20 61 72 65 20 61 63 74 If there are act
11ade 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 ive savepoints a
11adf 6e 64 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 77 nd any of them w
11ae0 65 72 65 20 63 72 65 61 74 65 64 20 0a 20 20 2a ere created . *
11ae1 2a 20 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74 * since the most
11ae2 20 72 65 63 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 recent journal
11ae3 68 65 61 64 65 72 20 77 61 73 20 77 72 69 74 74 header was writt
11ae4 65 6e 2c 20 75 70 64 61 74 65 20 74 68 65 20 0a en, update the .
11ae5 20 20 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f ** PagerSavepo
11ae6 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 66 int.iHdrOffset f
11ae7 69 65 6c 64 73 20 6e 6f 77 2e 0a 20 20 2a 2f 0a ields now.. */.
11ae8 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 for(ii=0; ii<p
11ae9 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e Pager->nSavepoin
11aea 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66 t; ii++){. if
11aeb 28 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 ( pPager->aSavep
11aec 6f 69 6e 74 5b 69 69 5d 2e 69 48 64 72 4f 66 66 oint[ii].iHdrOff
11aed 73 65 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 set==0 ){.
11aee 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 pPager->aSavepoi
11aef 6e 74 5b 69 69 5d 2e 69 48 64 72 4f 66 66 73 65 nt[ii].iHdrOffse
11af0 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 t = pPager->jour
11af1 6e 61 6c 4f 66 66 3b 0a 20 20 20 20 7d 0a 20 20 nalOff;. }.
11af2 7d 0a 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 }.. pPager->jou
11af3 72 6e 61 6c 48 64 72 20 3d 20 70 50 61 67 65 72 rnalHdr = pPager
11af4 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a ->journalOff = j
11af5 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 ournalHdrOffset(
11af6 70 50 61 67 65 72 29 3b 0a 0a 20 20 2f 2a 20 0a pPager);.. /* .
11af7 20 20 2a 2a 20 57 72 69 74 65 20 74 68 65 20 6e ** Write the n
11af8 52 65 63 20 46 69 65 6c 64 20 2d 20 74 68 65 20 Rec Field - the
11af9 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 72 number of page r
11afa 65 63 6f 72 64 73 20 74 68 61 74 20 66 6f 6c 6c ecords that foll
11afb 6f 77 20 74 68 69 73 0a 20 20 2a 2a 20 6a 6f 75 ow this. ** jou
11afc 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 4e 6f 72 rnal header. Nor
11afd 6d 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 20 77 mally, zero is w
11afe 72 69 74 74 65 6e 20 74 6f 20 74 68 69 73 20 76 ritten to this v
11aff 61 6c 75 65 20 61 74 20 74 68 69 73 20 74 69 6d alue at this tim
11b00 65 2e 0a 20 20 2a 2a 20 41 66 74 65 72 20 74 68 e.. ** After th
11b01 65 20 72 65 63 6f 72 64 73 20 61 72 65 20 61 64 e records are ad
11b02 64 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ded to the journ
11b03 61 6c 20 28 61 6e 64 20 74 68 65 20 6a 6f 75 72 al (and the jour
11b04 6e 61 6c 20 73 79 6e 63 65 64 2c 20 0a 20 20 2a nal synced, . *
11b05 2a 20 69 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e * if in full-syn
11b06 63 20 6d 6f 64 65 29 2c 20 74 68 65 20 7a 65 72 c mode), the zer
11b07 6f 20 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e o is overwritten
11b08 20 77 69 74 68 20 74 68 65 20 74 72 75 65 20 6e with the true n
11b09 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 72 65 umber. ** of re
11b0a 63 6f 72 64 73 20 28 73 65 65 20 73 79 6e 63 4a cords (see syncJ
11b0b 6f 75 72 6e 61 6c 28 29 29 2e 0a 20 20 2a 2a 0a ournal()).. **.
11b0c 20 20 2a 2a 20 41 20 66 61 73 74 65 72 20 61 6c ** A faster al
11b0d 74 65 72 6e 61 74 69 76 65 20 69 73 20 74 6f 20 ternative is to
11b0e 77 72 69 74 65 20 30 78 46 46 46 46 46 46 46 46 write 0xFFFFFFFF
11b0f 20 74 6f 20 74 68 65 20 6e 52 65 63 20 66 69 65 to the nRec fie
11b10 6c 64 2e 20 57 68 65 6e 0a 20 20 2a 2a 20 72 65 ld. When. ** re
11b11 61 64 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 ading the journa
11b12 6c 20 74 68 69 73 20 76 61 6c 75 65 20 74 65 6c l this value tel
11b13 6c 73 20 53 51 4c 69 74 65 20 74 6f 20 61 73 73 ls SQLite to ass
11b14 75 6d 65 20 74 68 61 74 20 74 68 65 0a 20 20 2a ume that the. *
11b15 2a 20 72 65 73 74 20 6f 66 20 74 68 65 20 6a 6f * rest of the jo
11b16 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74 61 urnal file conta
11b17 69 6e 73 20 76 61 6c 69 64 20 70 61 67 65 20 72 ins valid page r
11b18 65 63 6f 72 64 73 2e 20 54 68 69 73 20 61 73 73 ecords. This ass
11b19 75 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 umption. ** is
11b1a 64 61 6e 67 65 72 6f 75 73 2c 20 61 73 20 69 66 dangerous, as if
11b1b 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 a failure occur
11b1c 72 65 64 20 77 68 69 6c 73 74 20 77 72 69 74 69 red whilst writi
11b1d 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 ng to the journa
11b1e 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 20 6d l. ** file it m
11b1f 61 79 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65 20 ay contain some
11b20 67 61 72 62 61 67 65 20 64 61 74 61 2e 20 54 68 garbage data. Th
11b21 65 72 65 20 61 72 65 20 74 77 6f 20 73 63 65 6e ere are two scen
11b22 61 72 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72 65 arios. ** where
11b23 20 74 68 69 73 20 72 69 73 6b 20 63 61 6e 20 62 this risk can b
11b24 65 20 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a 0a e ignored:. **.
11b25 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 68 ** * When th
11b26 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 6e 6f e pager is in no
11b27 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72 72 -sync mode. Corr
11b28 75 70 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c 6f uption can follo
11b29 77 20 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f 77 w a. ** pow
11b2a 65 72 20 66 61 69 6c 75 72 65 20 69 6e 20 74 68 er failure in th
11b2b 69 73 20 63 61 73 65 20 61 6e 79 77 61 79 2e 0a is case anyway..
11b2c 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 **. ** * Wh
11b2d 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 49 4f en the SQLITE_IO
11b2e 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 CAP_SAFE_APPEND
11b2f 66 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68 69 flag is set. Thi
11b30 73 20 67 75 61 72 61 6e 74 65 65 73 0a 20 20 2a s guarantees. *
11b31 2a 20 20 20 20 20 74 68 61 74 20 67 61 72 62 61 * that garba
11b32 67 65 20 64 61 74 61 20 69 73 20 6e 65 76 65 72 ge data is never
11b33 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 appended to the
11b34 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 journal file..
11b35 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 */. assert( is
11b36 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
11b37 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 || pPager->noSy
11b38 6e 63 20 29 3b 0a 20 20 69 66 28 20 28 70 50 61 nc );. if( (pPa
11b39 67 65 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c 20 ger->noSync) ||
11b3a 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c (pPager->journal
11b3b 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
11b3c 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 0a NALMODE_MEMORY).
11b3d 20 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f 73 || (sqlite3Os
11b3e 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
11b3f 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 stics(pPager->fd
11b40 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 )&SQLITE_IOCAP_S
11b41 41 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 29 AFE_APPEND) . )
11b42 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 48 65 {. memcpy(zHe
11b43 61 64 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 ader, aJournalMa
11b44 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 gic, sizeof(aJou
11b45 72 6e 61 6c 4d 61 67 69 63 29 29 3b 0a 20 20 20 rnalMagic));.
11b46 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 put32bits(&zHea
11b47 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 der[sizeof(aJour
11b48 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 30 78 66 66 nalMagic)], 0xff
11b49 66 66 66 66 66 66 29 3b 0a 20 20 7d 65 6c 73 65 ffffff);. }else
11b4a 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 7a 48 65 {. memset(zHe
11b4b 61 64 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 ader, 0, sizeof(
11b4c 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 34 aJournalMagic)+4
11b4d 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 );. }.. /* The
11b4e 20 72 61 6e 64 6f 6d 20 63 68 65 63 6b 2d 68 61 random check-ha
11b4f 73 68 20 69 6e 69 74 69 61 6c 69 73 65 72 20 2a sh initialiser *
11b50 2f 20 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e / . sqlite3_ran
11b51 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 70 domness(sizeof(p
11b52 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 Pager->cksumInit
11b53 29 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b 73 75 ), &pPager->cksu
11b54 6d 49 6e 69 74 29 3b 0a 20 20 70 75 74 33 32 62 mInit);. put32b
11b55 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a its(&zHeader[siz
11b56 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 eof(aJournalMagi
11b57 63 29 2b 34 5d 2c 20 70 50 61 67 65 72 2d 3e 63 c)+4], pPager->c
11b58 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 2f 2a 20 ksumInit);. /*
11b59 54 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74 61 The initial data
11b5a 62 61 73 65 20 73 69 7a 65 20 2a 2f 0a 20 20 70 base size */. p
11b5b 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 ut32bits(&zHeade
11b5c 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 r[sizeof(aJourna
11b5d 6c 4d 61 67 69 63 29 2b 38 5d 2c 20 70 50 61 67 lMagic)+8], pPag
11b5e 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 29 3b er->dbOrigSize);
11b5f 0a 20 20 2f 2a 20 54 68 65 20 61 73 73 75 6d 65 . /* The assume
11b60 64 20 73 65 63 74 6f 72 20 73 69 7a 65 20 66 6f d sector size fo
11b61 72 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 2a r this process *
11b62 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 7a /. put32bits(&z
11b63 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a Header[sizeof(aJ
11b64 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 32 5d ournalMagic)+12]
11b65 2c 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 , pPager->sector
11b66 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 Size);.. /* The
11b67 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 page size */.
11b68 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 put32bits(&zHead
11b69 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e er[sizeof(aJourn
11b6a 61 6c 4d 61 67 69 63 29 2b 31 36 5d 2c 20 70 50 alMagic)+16], pP
11b6b 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b ager->pageSize);
11b6c 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a .. /* Initializ
11b6d 69 6e 67 20 74 68 65 20 74 61 69 6c 20 6f 66 20 ing the tail of
11b6e 74 68 65 20 62 75 66 66 65 72 20 69 73 20 6e 6f the buffer is no
11b6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20 20 45 76 t necessary. Ev
11b70 65 72 79 74 68 69 6e 67 0a 20 20 2a 2a 20 77 6f erything. ** wo
11b71 72 6b 73 20 66 69 6e 64 20 69 66 20 74 68 65 20 rks find if the
11b72 66 6f 6c 6c 6f 77 69 6e 67 20 6d 65 6d 73 65 74 following memset
11b73 28 29 20 69 73 20 6f 6d 69 74 74 65 64 2e 20 20 () is omitted.
11b74 42 75 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 But initializing
11b75 0a 20 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 . ** the memory
11b76 20 70 72 65 76 65 6e 74 73 20 76 61 6c 67 72 69 prevents valgri
11b77 6e 64 20 66 72 6f 6d 20 63 6f 6d 70 6c 61 69 6e nd from complain
11b78 69 6e 67 2c 20 73 6f 20 77 65 20 61 72 65 20 77 ing, so we are w
11b79 69 6c 6c 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 74 illing to. ** t
11b7a 61 6b 65 20 74 68 65 20 70 65 72 66 6f 72 6d 61 ake the performa
11b7b 6e 63 65 20 68 69 74 2e 0a 20 20 2a 2f 0a 20 20 nce hit.. */.
11b7c 6d 65 6d 73 65 74 28 26 7a 48 65 61 64 65 72 5b memset(&zHeader[
11b7d 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d sizeof(aJournalM
11b7e 61 67 69 63 29 2b 32 30 5d 2c 20 30 2c 0a 20 20 agic)+20], 0,.
11b7f 20 20 20 20 20 20 20 6e 48 65 61 64 65 72 2d 28 nHeader-(
11b80 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d sizeof(aJournalM
11b81 61 67 69 63 29 2b 32 30 29 29 3b 0a 0a 20 20 2f agic)+20));.. /
11b82 2a 20 49 6e 20 74 68 65 6f 72 79 2c 20 69 74 20 * In theory, it
11b83 69 73 20 6f 6e 6c 79 20 6e 65 63 65 73 73 61 72 is only necessar
11b84 79 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 32 y to write the 2
11b85 38 20 62 79 74 65 73 20 74 68 61 74 20 74 68 65 8 bytes that the
11b86 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 68 . ** journal h
11b87 65 61 64 65 72 20 63 6f 6e 73 75 6d 65 73 20 74 eader consumes t
11b88 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 o the journal fi
11b89 6c 65 20 68 65 72 65 2e 20 54 68 65 6e 20 69 6e le here. Then in
11b8a 63 72 65 6d 65 6e 74 20 74 68 65 20 0a 20 20 2a crement the . *
11b8b 2a 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f * Pager.journalO
11b8c 66 66 20 76 61 72 69 61 62 6c 65 20 62 79 20 4a ff variable by J
11b8d 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 73 6f OURNAL_HDR_SZ so
11b8e 20 74 68 61 74 20 74 68 65 20 6e 65 78 74 20 0a that the next .
11b8f 20 20 2a 2a 20 72 65 63 6f 72 64 20 69 73 20 77 ** record is w
11b90 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 66 6f ritten to the fo
11b91 6c 6c 6f 77 69 6e 67 20 73 65 63 74 6f 72 20 28 llowing sector (
11b92 6c 65 61 76 69 6e 67 20 61 20 67 61 70 20 69 6e leaving a gap in
11b93 20 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 74 the file. ** t
11b94 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6d 70 6c hat will be impl
11b95 69 63 69 74 6c 79 20 66 69 6c 6c 65 64 20 69 6e icitly filled in
11b96 20 62 79 20 74 68 65 20 4f 53 29 2e 0a 20 20 2a by the OS).. *
11b97 2a 0a 20 20 2a 2a 20 48 6f 77 65 76 65 72 20 69 *. ** However i
11b98 74 20 68 61 73 20 62 65 65 6e 20 64 69 73 63 6f t has been disco
11b99 76 65 72 65 64 20 74 68 61 74 20 6f 6e 20 73 6f vered that on so
11b9a 6d 65 20 73 79 73 74 65 6d 73 20 74 68 69 73 20 me systems this
11b9b 70 61 74 74 65 72 6e 20 63 61 6e 20 0a 20 20 2a pattern can . *
11b9c 2a 20 62 65 20 73 69 67 6e 69 66 69 63 61 6e 74 * be significant
11b9d 6c 79 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 63 ly slower than c
11b9e 6f 6e 74 69 67 75 6f 75 73 6c 79 20 77 72 69 74 ontiguously writ
11b9f 69 6e 67 20 64 61 74 61 20 74 6f 20 74 68 65 20 ing data to the
11ba0 66 69 6c 65 2c 0a 20 20 2a 2a 20 65 76 65 6e 20 file,. ** even
11ba1 69 66 20 74 68 61 74 20 6d 65 61 6e 73 20 65 78 if that means ex
11ba2 70 6c 69 63 69 74 6c 79 20 77 72 69 74 69 6e 67 plicitly writing
11ba3 20 64 61 74 61 20 74 6f 20 74 68 65 20 62 6c 6f data to the blo
11ba4 63 6b 20 6f 66 20 0a 20 20 2a 2a 20 28 4a 4f 55 ck of . ** (JOU
11ba5 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 RNAL_HDR_SZ - 28
11ba6 29 20 62 79 74 65 73 20 74 68 61 74 20 77 69 6c ) bytes that wil
11ba7 6c 20 6e 6f 74 20 62 65 20 75 73 65 64 2e 20 53 l not be used. S
11ba8 6f 20 74 68 61 74 20 69 73 20 77 68 61 74 0a 20 o that is what.
11ba9 20 2a 2a 20 69 73 20 64 6f 6e 65 2e 20 0a 20 20 ** is done. .
11baa 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 6c 6f 6f 70 **. ** The loop
11bab 20 69 73 20 72 65 71 75 69 72 65 64 20 68 65 72 is required her
11bac 65 20 69 6e 20 63 61 73 65 20 74 68 65 20 73 65 e in case the se
11bad 63 74 6f 72 2d 73 69 7a 65 20 69 73 20 6c 61 72 ctor-size is lar
11bae 67 65 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20 ger than the .
11baf 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 67 65 ** database page
11bb0 20 73 69 7a 65 2e 20 53 69 6e 63 65 20 74 68 65 size. Since the
11bb1 20 7a 48 65 61 64 65 72 20 62 75 66 66 65 72 20 zHeader buffer
11bb2 69 73 20 6f 6e 6c 79 20 50 61 67 65 72 2e 70 61 is only Pager.pa
11bb3 67 65 53 69 7a 65 0a 20 20 2a 2a 20 62 79 74 65 geSize. ** byte
11bb4 73 20 69 6e 20 73 69 7a 65 2c 20 6d 6f 72 65 20 s in size, more
11bb5 74 68 61 6e 20 6f 6e 65 20 63 61 6c 6c 20 74 6f than one call to
11bb6 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
11bb7 29 20 6d 61 79 20 62 65 20 72 65 71 75 69 72 65 ) may be require
11bb8 64 0a 20 20 2a 2a 20 74 6f 20 70 6f 70 75 6c 61 d. ** to popula
11bb9 74 65 20 74 68 65 20 65 6e 74 69 72 65 20 6a 6f te the entire jo
11bba 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 65 63 urnal header sec
11bbb 74 6f 72 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 tor.. */ . for
11bbc 28 6e 57 72 69 74 65 3d 30 3b 20 72 63 3d 3d 53 (nWrite=0; rc==S
11bbd 51 4c 49 54 45 5f 4f 4b 26 26 6e 57 72 69 74 65 QLITE_OK&&nWrite
11bbe 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 <JOURNAL_HDR_SZ(
11bbf 70 50 61 67 65 72 29 3b 20 6e 57 72 69 74 65 2b pPager); nWrite+
11bc0 3d 6e 48 65 61 64 65 72 29 7b 0a 20 20 20 20 49 =nHeader){. I
11bc1 4f 54 52 41 43 45 28 28 22 4a 48 44 52 20 25 70 OTRACE(("JHDR %p
11bc2 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 %lld %d\n", pPa
11bc3 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 ger, pPager->jou
11bc4 72 6e 61 6c 48 64 72 2c 20 6e 48 65 61 64 65 72 rnalHdr, nHeader
11bc5 29 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 )). rc = sqli
11bc6 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 te3OsWrite(pPage
11bc7 72 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 2c r->jfd, zHeader,
11bc8 20 6e 48 65 61 64 65 72 2c 20 70 50 61 67 65 72 nHeader, pPager
11bc9 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 ->journalOff);.
11bca 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
11bcb 61 6c 4f 66 66 20 2b 3d 20 6e 48 65 61 64 65 72 alOff += nHeader
11bcc 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
11bcd 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 rc;.}../*.** The
11bce 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 journal file mu
11bcf 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 st be open when
11bd0 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2e 20 this is called.
11bd1 41 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 A journal header
11bd2 20 66 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52 4e 41 file.** (JOURNA
11bd3 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 L_HDR_SZ bytes)
11bd4 69 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 is read from the
11bd5 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f current locatio
11bd6 6e 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c n in the journal
11bd7 0a 2a 2a 20 66 69 6c 65 2e 20 54 68 65 20 63 75 .** file. The cu
11bd8 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 69 rrent location i
11bd9 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
11bda 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a le is given by.*
11bdb 2a 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 * pPager->journa
11bdc 6c 4f 66 66 2e 20 53 65 65 20 63 6f 6d 6d 65 6e lOff. See commen
11bdd 74 73 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f ts above functio
11bde 6e 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 n writeJournalHd
11bdf 72 28 29 20 66 6f 72 0a 2a 2a 20 61 20 64 65 73 r() for.** a des
11be0 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 cription of the
11be1 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 journal header f
11be2 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ormat..**.** If
11be3 74 68 65 20 68 65 61 64 65 72 20 69 73 20 72 65 the header is re
11be4 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c ad successfully,
11be5 20 2a 70 4e 52 65 63 20 69 73 20 73 65 74 20 74 *pNRec is set t
11be6 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a o the number of.
11be7 2a 2a 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 ** page records
11be8 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 68 following this h
11be9 65 61 64 65 72 20 61 6e 64 20 2a 70 44 62 53 69 eader and *pDbSi
11bea 7a 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 ze is set to the
11beb 20 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 size of the.**
11bec 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 20 database before
11bed 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
11bee 62 65 67 61 6e 2c 20 69 6e 20 70 61 67 65 73 2e began, in pages.
11bef 20 41 6c 73 6f 2c 20 70 50 61 67 65 72 2d 3e 63 Also, pPager->c
11bf0 6b 73 75 6d 49 6e 69 74 0a 2a 2a 20 69 73 20 73 ksumInit.** is s
11bf1 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 et to the value
11bf2 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f read from the jo
11bf3 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 53 51 urnal header. SQ
11bf4 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
11bf5 6e 65 64 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 ned.** in this c
11bf6 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ase..**.** If th
11bf7 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
11bf8 20 66 69 6c 65 20 61 70 70 65 61 72 73 20 74 6f file appears to
11bf9 20 62 65 20 63 6f 72 72 75 70 74 65 64 2c 20 53 be corrupted, S
11bfa 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 0a 2a 2a QLITE_DONE is.**
11bfb 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 returned and *p
11bfc 4e 52 65 63 20 61 6e 64 20 2a 50 44 62 53 69 7a NRec and *PDbSiz
11bfd 65 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e e are undefined.
11bfe 20 20 49 66 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 If JOURNAL_HDR
11bff 5f 53 5a 20 62 79 74 65 73 0a 2a 2a 20 63 61 6e _SZ bytes.** can
11c00 6e 6f 74 20 62 65 20 72 65 61 64 20 66 72 6f 6d not be read from
11c01 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
11c02 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 e an error code
11c03 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a is returned..*/.
11c04 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 4a static int readJ
11c05 6f 75 72 6e 61 6c 48 64 72 28 0a 20 20 50 61 67 ournalHdr(. Pag
11c06 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 er *pPager,
11c07 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
11c08 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 er object */. i
11c09 6e 74 20 69 73 48 6f 74 2c 0a 20 20 69 36 34 20 nt isHot,. i64
11c0a 6a 6f 75 72 6e 61 6c 53 69 7a 65 2c 20 20 20 20 journalSize,
11c0b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
11c0c 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 6a 6f 75 of the open jou
11c0d 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 rnal file in byt
11c0e 65 73 20 2a 2f 0a 20 20 75 33 32 20 2a 70 4e 52 es */. u32 *pNR
11c0f 65 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ec,
11c10 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 56 61 6c /* OUT: Val
11c11 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 ue read from the
11c12 20 6e 52 65 63 20 66 69 65 6c 64 20 2a 2f 0a 20 nRec field */.
11c13 20 75 33 32 20 2a 70 44 62 53 69 7a 65 20 20 20 u32 *pDbSize
11c14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11c15 20 4f 55 54 3a 20 56 61 6c 75 65 20 6f 66 20 6f OUT: Value of o
11c16 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 riginal database
11c17 20 73 69 7a 65 20 66 69 65 6c 64 20 2a 2f 0a 29 size field */.)
11c18 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 {. int rc;
11c19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11c1a 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
11c1b 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
11c1c 61 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20 ar aMagic[8];
11c1d 20 20 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f /* A buffer to
11c1e 20 68 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 20 hold the magic
11c1f 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20 header */. i64
11c20 69 48 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 iHdrOff;
11c21 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
11c22 65 74 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 et of journal he
11c23 61 64 65 72 20 62 65 69 6e 67 20 72 65 61 64 20 ader being read
11c24 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 73 */.. assert( is
11c25 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
11c26 29 20 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f 75 ) ); /* Jou
11c27 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 rnal file must b
11c28 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 2f 2a e open. */.. /*
11c29 20 41 64 76 61 6e 63 65 20 50 61 67 65 72 2e 6a Advance Pager.j
11c2a 6f 75 72 6e 61 6c 4f 66 66 20 74 6f 20 74 68 65 ournalOff to the
11c2b 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6e 65 start of the ne
11c2c 78 74 20 73 65 63 74 6f 72 2e 20 49 66 20 74 68 xt sector. If th
11c2d 65 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 e. ** journal f
11c2e 69 6c 65 20 69 73 20 74 6f 6f 20 73 6d 61 6c 6c ile is too small
11c2f 20 66 6f 72 20 74 68 65 72 65 20 74 6f 20 62 65 for there to be
11c30 20 61 20 68 65 61 64 65 72 20 73 74 6f 72 65 64 a header stored
11c31 20 61 74 20 74 68 69 73 0a 20 20 2a 2a 20 70 6f at this. ** po
11c32 69 6e 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 int, return SQLI
11c33 54 45 5f 44 4f 4e 45 2e 0a 20 20 2a 2f 0a 20 20 TE_DONE.. */.
11c34 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
11c35 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f ff = journalHdrO
11c36 66 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 ffset(pPager);.
11c37 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 if( pPager->jou
11c38 72 6e 61 6c 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f rnalOff+JOURNAL_
11c39 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 3e HDR_SZ(pPager) >
11c3a 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 20 29 7b 0a journalSize ){.
11c3b 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
11c3c 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 48 E_DONE;. }. iH
11c3d 64 72 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e drOff = pPager->
11c3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f journalOff;.. /
11c3f 2a 20 52 65 61 64 20 69 6e 20 74 68 65 20 66 69 * Read in the fi
11c40 72 73 74 20 38 20 62 79 74 65 73 20 6f 66 20 74 rst 8 bytes of t
11c41 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 he journal heade
11c42 72 2e 20 49 66 20 74 68 65 79 20 64 6f 20 6e 6f r. If they do no
11c43 74 20 6d 61 74 63 68 0a 20 20 2a 2a 20 74 68 65 t match. ** the
11c44 20 20 6d 61 67 69 63 20 73 74 72 69 6e 67 20 66 magic string f
11c45 6f 75 6e 64 20 61 74 20 74 68 65 20 73 74 61 72 ound at the star
11c46 74 20 6f 66 20 65 61 63 68 20 6a 6f 75 72 6e 61 t of each journa
11c47 6c 20 68 65 61 64 65 72 2c 20 72 65 74 75 72 6e l header, return
11c48 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 4f 4e . ** SQLITE_DON
11c49 45 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f E. If an IO erro
11c4a 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e r occurs, return
11c4b 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 an error code.
11c4c 4f 74 68 65 72 77 69 73 65 2c 0a 20 20 2a 2a 20 Otherwise,. **
11c4d 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 proceed.. */.
11c4e 69 66 28 20 69 73 48 6f 74 20 7c 7c 20 69 48 64 if( isHot || iHd
11c4f 72 4f 66 66 21 3d 70 50 61 67 65 72 2d 3e 6a 6f rOff!=pPager->jo
11c50 75 72 6e 61 6c 48 64 72 20 29 7b 0a 20 20 20 20 urnalHdr ){.
11c51 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 rc = sqlite3OsRe
11c52 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 ad(pPager->jfd,
11c53 61 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 aMagic, sizeof(a
11c54 4d 61 67 69 63 29 2c 20 69 48 64 72 4f 66 66 29 Magic), iHdrOff)
11c55 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a ;. if( rc ){.
11c56 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
11c57 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d . }. if( m
11c58 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a emcmp(aMagic, aJ
11c59 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a ournalMagic, siz
11c5a 65 6f 66 28 61 4d 61 67 69 63 29 29 21 3d 30 20 eof(aMagic))!=0
11c5b 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
11c5c 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 SQLITE_DONE;.
11c5d 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 }. }.. /* Rea
11c5e 64 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65 d the first thre
11c5f 65 20 33 32 2d 62 69 74 20 66 69 65 6c 64 73 20 e 32-bit fields
11c60 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 of the journal h
11c61 65 61 64 65 72 3a 20 54 68 65 20 6e 52 65 63 0a eader: The nRec.
11c62 20 20 2a 2a 20 66 69 65 6c 64 2c 20 74 68 65 20 ** field, the
11c63 63 68 65 63 6b 73 75 6d 2d 69 6e 69 74 69 61 6c checksum-initial
11c64 69 7a 65 72 20 61 6e 64 20 74 68 65 20 64 61 74 izer and the dat
11c65 61 62 61 73 65 20 73 69 7a 65 20 61 74 20 74 68 abase size at th
11c66 65 20 73 74 61 72 74 0a 20 20 2a 2a 20 6f 66 20 e start. ** of
11c67 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e the transaction.
11c68 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 Return an error
11c69 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e code if anythin
11c6a 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 20 20 g goes wrong..
11c6b 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f */. if( SQLITE_
11c6c 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 OK!=(rc = read32
11c6d 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
11c6e 2c 20 69 48 64 72 4f 66 66 2b 38 2c 20 70 4e 52 , iHdrOff+8, pNR
11c6f 65 63 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 ec)). || SQLIT
11c70 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 E_OK!=(rc = read
11c71 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 32bits(pPager->j
11c72 66 64 2c 20 69 48 64 72 4f 66 66 2b 31 32 2c 20 fd, iHdrOff+12,
11c73 26 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e &pPager->cksumIn
11c74 69 74 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 it)). || SQLIT
11c75 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 E_OK!=(rc = read
11c76 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 32bits(pPager->j
11c77 66 64 2c 20 69 48 64 72 4f 66 66 2b 31 36 2c 20 fd, iHdrOff+16,
11c78 70 44 62 53 69 7a 65 29 29 0a 20 20 29 7b 0a 20 pDbSize)). ){.
11c79 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
11c7a 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d }.. if( pPager-
11c7b 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 >journalOff==0 )
11c7c 7b 0a 20 20 20 20 75 33 32 20 69 50 61 67 65 53 {. u32 iPageS
11c7d 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
11c7e 20 20 20 2f 2a 20 50 61 67 65 2d 73 69 7a 65 20 /* Page-size
11c7f 66 69 65 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 6c field of journal
11c80 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 75 header */. u
11c81 33 32 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 20 32 iSectorSize;
11c82 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
11c83 65 63 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c 64 ector-size field
11c84 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 of journal head
11c85 65 72 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 50 er */. u16 iP
11c86 61 67 65 53 69 7a 65 31 36 3b 20 20 20 20 20 20 ageSize16;
11c87 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f /* Copy o
11c88 66 20 69 50 61 67 65 53 69 7a 65 20 69 6e 20 31 f iPageSize in 1
11c89 36 2d 62 69 74 20 76 61 72 69 61 62 6c 65 20 2a 6-bit variable *
11c8a 2f 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 /.. /* Read t
11c8b 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 he page-size and
11c8c 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 6a 6f 75 sector-size jou
11c8d 72 6e 61 6c 20 68 65 61 64 65 72 20 66 69 65 6c rnal header fiel
11c8e 64 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 ds. */. if( S
11c8f 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 QLITE_OK!=(rc =
11c90 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67 65 read32bits(pPage
11c91 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b r->jfd, iHdrOff+
11c92 32 30 2c 20 26 69 53 65 63 74 6f 72 53 69 7a 65 20, &iSectorSize
11c93 29 29 0a 20 20 20 20 20 7c 7c 20 53 51 4c 49 54 )). || SQLIT
11c94 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 E_OK!=(rc = read
11c95 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 32bits(pPager->j
11c96 66 64 2c 20 69 48 64 72 4f 66 66 2b 32 34 2c 20 fd, iHdrOff+24,
11c97 26 69 50 61 67 65 53 69 7a 65 29 29 0a 20 20 20 &iPageSize)).
11c98 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
11c99 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 rc;. }..
11c9a 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 /* Check that th
11c9b 65 20 76 61 6c 75 65 73 20 72 65 61 64 20 66 72 e values read fr
11c9c 6f 6d 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 om the page-size
11c9d 20 61 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 and sector-size
11c9e 20 66 69 65 6c 64 73 0a 20 20 20 20 2a 2a 20 61 fields. ** a
11c9f 72 65 20 77 69 74 68 69 6e 20 72 61 6e 67 65 2e re within range.
11ca0 20 54 6f 20 62 65 20 27 69 6e 20 72 61 6e 67 65 To be 'in range
11ca1 27 2c 20 62 6f 74 68 20 76 61 6c 75 65 73 20 6e ', both values n
11ca2 65 65 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 eed to be a powe
11ca3 72 0a 20 20 20 20 2a 2a 20 6f 66 20 74 77 6f 20 r. ** of two
11ca4 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 greater than or
11ca5 65 71 75 61 6c 20 74 6f 20 35 31 32 2c 20 61 6e equal to 512, an
11ca6 64 20 6e 6f 74 20 67 72 65 61 74 65 72 20 74 68 d not greater th
11ca7 61 6e 20 74 68 65 69 72 20 0a 20 20 20 20 2a 2a an their . **
11ca8 20 72 65 73 70 65 63 74 69 76 65 20 63 6f 6d 70 respective comp
11ca9 69 6c 65 20 74 69 6d 65 20 6d 61 78 69 6d 75 6d ile time maximum
11caa 20 6c 69 6d 69 74 73 2e 0a 20 20 20 20 2a 2f 0a limits.. */.
11cab 20 20 20 20 69 66 28 20 69 50 61 67 65 53 69 7a if( iPageSiz
11cac 65 3c 35 31 32 20 20 20 20 20 20 20 20 20 20 20 e<512
11cad 20 20 20 20 20 20 20 7c 7c 20 69 53 65 63 74 6f || iSecto
11cae 72 53 69 7a 65 3c 35 31 32 0a 20 20 20 20 20 7c rSize<512. |
11caf 7c 20 69 50 61 67 65 53 69 7a 65 3e 53 51 4c 49 | iPageSize>SQLI
11cb0 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 TE_MAX_PAGE_SIZE
11cb1 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 3e || iSectorSize>
11cb2 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 0a MAX_SECTOR_SIZE.
11cb3 20 20 20 20 20 7c 7c 20 28 28 69 50 61 67 65 53 || ((iPageS
11cb4 69 7a 65 2d 31 29 26 69 50 61 67 65 53 69 7a 65 ize-1)&iPageSize
11cb5 29 21 3d 30 20 20 20 7c 7c 20 28 28 69 53 65 63 )!=0 || ((iSec
11cb6 74 6f 72 53 69 7a 65 2d 31 29 26 69 53 65 63 74 torSize-1)&iSect
11cb7 6f 72 53 69 7a 65 29 21 3d 30 20 0a 20 20 20 20 orSize)!=0 .
11cb8 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 ){. /* If t
11cb9 68 65 20 65 69 74 68 65 72 20 74 68 65 20 70 61 he either the pa
11cba 67 65 2d 73 69 7a 65 20 6f 72 20 73 65 63 74 6f ge-size or secto
11cbb 72 2d 73 69 7a 65 20 69 6e 20 74 68 65 20 6a 6f r-size in the jo
11cbc 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 urnal-header is
11cbd 0a 20 20 20 20 20 20 2a 2a 20 69 6e 76 61 6c 69 . ** invali
11cbe 64 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f 63 d, then the proc
11cbf 65 73 73 20 74 68 61 74 20 77 72 6f 74 65 20 74 ess that wrote t
11cc0 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 he journal-heade
11cc1 72 20 6d 75 73 74 20 68 61 76 65 20 0a 20 20 20 r must have .
11cc2 20 20 20 2a 2a 20 63 72 61 73 68 65 64 20 62 65 ** crashed be
11cc3 66 6f 72 65 20 74 68 65 20 68 65 61 64 65 72 20 fore the header
11cc4 77 61 73 20 73 79 6e 63 65 64 2e 20 49 6e 20 74 was synced. In t
11cc5 68 69 73 20 63 61 73 65 20 73 74 6f 70 20 72 65 his case stop re
11cc6 61 64 69 6e 67 20 0a 20 20 20 20 20 20 2a 2a 20 ading . **
11cc7 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11cc8 20 68 65 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a here.. */.
11cc9 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
11cca 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a ITE_DONE;. }.
11ccb 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 . /* Update t
11ccc 68 65 20 70 61 67 65 2d 73 69 7a 65 20 74 6f 20 he page-size to
11ccd 6d 61 74 63 68 20 74 68 65 20 76 61 6c 75 65 20 match the value
11cce 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f read from the jo
11ccf 75 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2a 20 55 urnal. . ** U
11cd0 73 65 20 61 20 74 65 73 74 63 61 73 65 28 29 20 se a testcase()
11cd1 6d 61 63 72 6f 20 74 6f 20 6d 61 6b 65 20 73 75 macro to make su
11cd2 72 65 20 74 68 61 74 20 6d 61 6c 6c 6f 63 20 66 re that malloc f
11cd3 61 69 6c 75 72 65 20 77 69 74 68 69 6e 20 0a 20 ailure within .
11cd4 20 20 20 2a 2a 20 50 61 67 65 72 53 65 74 50 61 ** PagerSetPa
11cd5 67 65 73 69 7a 65 28 29 20 69 73 20 74 65 73 74 gesize() is test
11cd6 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ed.. */. i
11cd7 50 61 67 65 53 69 7a 65 31 36 20 3d 20 28 75 31 PageSize16 = (u1
11cd8 36 29 69 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 6)iPageSize;.
11cd9 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
11cda 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 50 erSetPagesize(pP
11cdb 61 67 65 72 2c 20 26 69 50 61 67 65 53 69 7a 65 ager, &iPageSize
11cdc 31 36 2c 20 2d 31 29 3b 0a 20 20 20 20 74 65 73 16, -1);. tes
11cdd 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 tcase( rc!=SQLIT
11cde 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 E_OK );. asse
11cdf 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f rt( rc!=SQLITE_O
11ce0 4b 20 7c 7c 20 69 50 61 67 65 53 69 7a 65 31 36 K || iPageSize16
11ce1 3d 3d 28 75 31 36 29 69 50 61 67 65 53 69 7a 65 ==(u16)iPageSize
11ce2 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 );.. /* Upda
11ce3 74 65 20 74 68 65 20 61 73 73 75 6d 65 64 20 73 te the assumed s
11ce4 65 63 74 6f 72 2d 73 69 7a 65 20 74 6f 20 6d 61 ector-size to ma
11ce5 74 63 68 20 74 68 65 20 76 61 6c 75 65 20 75 73 tch the value us
11ce6 65 64 20 62 79 20 0a 20 20 20 20 2a 2a 20 74 68 ed by . ** th
11ce7 65 20 70 72 6f 63 65 73 73 20 74 68 61 74 20 63 e process that c
11ce8 72 65 61 74 65 64 20 74 68 69 73 20 6a 6f 75 72 reated this jour
11ce9 6e 61 6c 2e 20 49 66 20 74 68 69 73 20 6a 6f 75 nal. If this jou
11cea 72 6e 61 6c 20 77 61 73 0a 20 20 20 20 2a 2a 20 rnal was. **
11ceb 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72 6f created by a pro
11cec 63 65 73 73 20 6f 74 68 65 72 20 74 68 61 6e 20 cess other than
11ced 74 68 69 73 20 6f 6e 65 2c 20 74 68 65 6e 20 74 this one, then t
11cee 68 69 73 20 72 6f 75 74 69 6e 65 0a 20 20 20 20 his routine.
11cef 2a 2a 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c ** is being call
11cf0 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 70 ed from within p
11cf1 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 2e ager_playback().
11cf2 20 54 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 The local value
11cf3 0a 20 20 20 20 2a 2a 20 6f 66 20 50 61 67 65 72 . ** of Pager
11cf4 2e 73 65 63 74 6f 72 53 69 7a 65 20 69 73 20 72 .sectorSize is r
11cf5 65 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 65 estored at the e
11cf6 6e 64 20 6f 66 20 74 68 61 74 20 72 6f 75 74 69 nd of that routi
11cf7 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 ne.. */. p
11cf8 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a Pager->sectorSiz
11cf9 65 20 3d 20 69 53 65 63 74 6f 72 53 69 7a 65 3b e = iSectorSize;
11cfa 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 72 2d 3e . }.. pPager->
11cfb 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 4a 4f journalOff += JO
11cfc 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
11cfd 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 ger);. return r
11cfe 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 c;.}.../*.** Wri
11cff 74 65 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 te the supplied
11d00 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
11d01 61 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 ame into the jou
11d02 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 rnal file for pa
11d03 67 65 72 0a 2a 2a 20 70 50 61 67 65 72 20 61 74 ger.** pPager at
11d04 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 the current loc
11d05 61 74 69 6f 6e 2e 20 54 68 65 20 6d 61 73 74 65 ation. The maste
11d06 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 6d r journal name m
11d07 75 73 74 20 62 65 20 74 68 65 20 6c 61 73 74 0a ust be the last.
11d08 2a 2a 20 74 68 69 6e 67 20 77 72 69 74 74 65 6e ** thing written
11d09 20 74 6f 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 to a journal fi
11d0a 6c 65 2e 20 49 66 20 74 68 65 20 70 61 67 65 72 le. If the pager
11d0b 20 69 73 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 is in full-sync
11d0c 20 6d 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 6a 6f mode, the.** jo
11d0d 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 urnal file descr
11d0e 69 70 74 6f 72 20 69 73 20 61 64 76 61 6e 63 65 iptor is advance
11d0f 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65 d to the next se
11d10 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 20 62 65 ctor boundary be
11d11 66 6f 72 65 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 fore.** anything
11d12 20 69 73 20 77 72 69 74 74 65 6e 2e 20 54 68 65 is written. The
11d13 20 66 6f 72 6d 61 74 20 69 73 3a 0a 2a 2a 0a 2a format is:.**.*
11d14 2a 20 20 20 2b 20 34 20 62 79 74 65 73 3a 20 50 * + 4 bytes: P
11d15 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 2e 0a 2a 2a AGER_MJ_PGNO..**
11d16 20 20 20 2b 20 4e 20 62 79 74 65 73 3a 20 4d 61 + N bytes: Ma
11d17 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
11d18 65 6e 61 6d 65 20 69 6e 20 75 74 66 2d 38 2e 0a ename in utf-8..
11d19 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 73 3a 20 ** + 4 bytes:
11d1a 4e 20 28 6c 65 6e 67 74 68 20 6f 66 20 6d 61 73 N (length of mas
11d1b 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 ter journal name
11d1c 20 69 6e 20 62 79 74 65 73 2c 20 6e 6f 20 6e 75 in bytes, no nu
11d1d 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 29 2e 0a 2a l-terminator)..*
11d1e 2a 20 20 20 2b 20 34 20 62 79 74 65 73 3a 20 4d * + 4 bytes: M
11d1f 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 aster journal na
11d20 6d 65 20 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a 20 me checksum..**
11d21 20 20 2b 20 38 20 62 79 74 65 73 3a 20 61 4a 6f + 8 bytes: aJo
11d22 75 72 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a urnalMagic[]..**
11d23 0a 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 6a .** The master j
11d24 6f 75 72 6e 61 6c 20 70 61 67 65 20 63 68 65 63 ournal page chec
11d25 6b 73 75 6d 20 69 73 20 74 68 65 20 73 75 6d 20 ksum is the sum
11d26 6f 66 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 of the bytes in
11d27 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f the master.** jo
11d28 75 72 6e 61 6c 20 6e 61 6d 65 2c 20 77 68 65 72 urnal name, wher
11d29 65 20 65 61 63 68 20 62 79 74 65 20 69 73 20 69 e each byte is i
11d2a 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61 20 nterpreted as a
11d2b 73 69 67 6e 65 64 20 38 2d 62 69 74 20 69 6e 74 signed 8-bit int
11d2c 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a eger..**.** If z
11d2d 4d 61 73 74 65 72 20 69 73 20 61 20 4e 55 4c 4c Master is a NULL
11d2e 20 70 6f 69 6e 74 65 72 20 28 6f 63 63 75 72 73 pointer (occurs
11d2f 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61 for a single da
11d30 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 tabase transacti
11d31 6f 6e 29 2c 20 0a 2a 2a 20 74 68 69 73 20 63 61 on), .** this ca
11d32 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a ll is a no-op..*
11d33 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 69 /.static int wri
11d34 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 teMasterJournal(
11d35 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 Pager *pPager, c
11d36 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 onst char *zMast
11d37 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 er){. int rc;
11d38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11d39 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
11d3a 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 n code */. int
11d3b 6e 4d 61 73 74 65 72 3b 20 20 20 20 20 20 20 20 nMaster;
11d3c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11d3d 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67 Length of string
11d3e 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 20 20 69 36 zMaster */. i6
11d3f 34 20 69 48 64 72 4f 66 66 3b 20 20 20 20 20 20 4 iHdrOff;
11d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11d41 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 65 61 64 * Offset of head
11d42 65 72 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 er in journal fi
11d43 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a 72 6e 6c le */. i64 jrnl
11d44 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 Size;
11d45 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
11d46 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 of journal file
11d47 20 6f 6e 20 64 69 73 6b 20 2a 2f 0a 20 20 75 33 on disk */. u3
11d48 32 20 63 6b 73 75 6d 20 3d 20 30 3b 20 20 20 20 2 cksum = 0;
11d49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11d4a 2a 20 43 68 65 63 6b 73 75 6d 20 6f 66 20 73 74 * Checksum of st
11d4b 72 69 6e 67 20 7a 4d 61 73 74 65 72 20 2a 2f 0a ring zMaster */.
11d4c 0a 20 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 . if( !zMaster
11d4d 7c 7c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 || pPager->setMa
11d4e 73 74 65 72 0a 20 20 20 7c 7c 20 70 50 61 67 65 ster. || pPage
11d4f 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d r->journalMode==
11d50 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
11d51 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 7c 7c 20 E_MEMORY . ||
11d52 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
11d53 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
11d54 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 29 7b ALMODE_OFF . ){
11d55 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
11d56 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 50 61 TE_OK;. }. pPa
11d57 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d ger->setMaster =
11d58 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 1;. assert( is
11d59 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
11d5a 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 ) );.. /* Calcu
11d5b 6c 61 74 65 20 74 68 65 20 6c 65 6e 67 74 68 20 late the length
11d5c 69 6e 20 62 79 74 65 73 20 61 6e 64 20 74 68 65 in bytes and the
11d5d 20 63 68 65 63 6b 73 75 6d 20 6f 66 20 7a 4d 61 checksum of zMa
11d5e 73 74 65 72 20 2a 2f 0a 20 20 66 6f 72 28 6e 4d ster */. for(nM
11d5f 61 73 74 65 72 3d 30 3b 20 7a 4d 61 73 74 65 72 aster=0; zMaster
11d60 5b 6e 4d 61 73 74 65 72 5d 3b 20 6e 4d 61 73 74 [nMaster]; nMast
11d61 65 72 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d er++){. cksum
11d62 20 2b 3d 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 += zMaster[nMas
11d63 74 65 72 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 ter];. }.. /*
11d64 49 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 If in full-sync
11d65 6d 6f 64 65 2c 20 61 64 76 61 6e 63 65 20 74 6f mode, advance to
11d66 20 74 68 65 20 6e 65 78 74 20 64 69 73 6b 20 73 the next disk s
11d67 65 63 74 6f 72 20 62 65 66 6f 72 65 20 77 72 69 ector before wri
11d68 74 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6d 61 ting. ** the ma
11d69 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d ster journal nam
11d6a 65 2e 20 54 68 69 73 20 69 73 20 69 6e 20 63 61 e. This is in ca
11d6b 73 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 se the previous
11d6c 70 61 67 65 20 77 72 69 74 74 65 6e 20 74 6f 0a page written to.
11d6d 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c ** the journal
11d6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
11d6f 6e 20 73 79 6e 63 65 64 2e 0a 20 20 2a 2f 0a 20 n synced.. */.
11d70 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c if( pPager->ful
11d71 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20 70 50 61 lSync ){. pPa
11d72 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
11d73 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 = journalHdrOffs
11d74 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a et(pPager);. }.
11d75 20 20 69 48 64 72 4f 66 66 20 3d 20 70 50 61 67 iHdrOff = pPag
11d76 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a er->journalOff;.
11d77 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 . /* Write the
11d78 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 64 master journal d
11d79 61 74 61 20 74 6f 20 74 68 65 20 65 6e 64 20 6f ata to the end o
11d7a 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
11d7b 6c 65 2e 20 49 66 0a 20 20 2a 2a 20 61 6e 20 65 le. If. ** an e
11d7c 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 rror occurs, ret
11d7d 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f urn the error co
11d7e 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 de to the caller
11d7f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 30 20 .. */. if( (0
11d80 21 3d 20 28 72 63 20 3d 20 77 72 69 74 65 33 32 != (rc = write32
11d81 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
11d82 2c 20 69 48 64 72 4f 66 66 2c 20 50 41 47 45 52 , iHdrOff, PAGER
11d83 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 _MJ_PGNO(pPager)
11d84 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 ))). || (0 !=
11d85 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 (rc = sqlite3OsW
11d86 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 rite(pPager->jfd
11d87 2c 20 7a 4d 61 73 74 65 72 2c 20 6e 4d 61 73 74 , zMaster, nMast
11d88 65 72 2c 20 69 48 64 72 4f 66 66 2b 34 29 29 29 er, iHdrOff+4)))
11d89 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 . || (0 != (rc
11d8a 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 = write32bits(p
11d8b 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 Pager->jfd, iHdr
11d8c 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2c 20 6e Off+4+nMaster, n
11d8d 4d 61 73 74 65 72 29 29 29 0a 20 20 20 7c 7c 20 Master))). ||
11d8e 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74 (0 != (rc = writ
11d8f 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e e32bits(pPager->
11d90 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e jfd, iHdrOff+4+n
11d91 4d 61 73 74 65 72 2b 34 2c 20 63 6b 73 75 6d 29 Master+4, cksum)
11d92 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 )). || (0 != (
11d93 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
11d94 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ite(pPager->jfd,
11d95 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 aJournalMagic,
11d96 38 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 8, iHdrOff+4+nMa
11d97 73 74 65 72 2b 38 29 29 29 0a 20 20 29 7b 0a 20 ster+8))). ){.
11d98 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
11d99 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 }. pPager->jour
11d9a 6e 61 6c 4f 66 66 20 2b 3d 20 28 6e 4d 61 73 74 nalOff += (nMast
11d9b 65 72 2b 32 30 29 3b 0a 20 20 70 50 61 67 65 72 er+20);. pPager
11d9c 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 21 70 50 ->needSync = !pP
11d9d 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 0a 20 ager->noSync;..
11d9e 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 /* If the pager
11d9f 20 69 73 20 69 6e 20 70 65 72 69 73 74 65 6e 74 is in peristent
11da0 2d 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 2c 20 74 -journal mode, t
11da1 68 65 6e 20 74 68 65 20 70 68 79 73 69 63 61 6c hen the physical
11da2 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 66 . ** journal-f
11da3 69 6c 65 20 6d 61 79 20 65 78 74 65 6e 64 20 70 ile may extend p
11da4 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 ast the end of t
11da5 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 he master-journa
11da6 6c 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e 64 20 l name. ** and
11da7 38 20 62 79 74 65 73 20 6f 66 20 6d 61 67 69 63 8 bytes of magic
11da8 20 64 61 74 61 20 6a 75 73 74 20 77 72 69 74 74 data just writt
11da9 65 6e 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 en to the file.
11daa 54 68 69 73 20 69 73 20 0a 20 20 2a 2a 20 64 61 This is . ** da
11dab 6e 67 65 72 6f 75 73 20 62 65 63 61 75 73 65 20 ngerous because
11dac 74 68 65 20 63 6f 64 65 20 74 6f 20 72 6f 6c 6c the code to roll
11dad 62 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e back a hot-journ
11dae 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 77 69 6c al file. ** wil
11daf 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f l not be able to
11db0 20 66 69 6e 64 20 74 68 65 20 6d 61 73 74 65 72 find the master
11db1 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 74 6f -journal name to
11db2 20 64 65 74 65 72 6d 69 6e 65 20 0a 20 20 2a 2a determine . **
11db3 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
11db4 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 68 the journal is h
11db5 6f 74 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 ot. . **. ** E
11db6 61 73 69 65 73 74 20 74 68 69 6e 67 20 74 6f 20 asiest thing to
11db7 64 6f 20 69 6e 20 74 68 69 73 20 73 63 65 6e 61 do in this scena
11db8 72 69 6f 20 69 73 20 74 6f 20 74 72 75 6e 63 61 rio is to trunca
11db9 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a te the journal .
11dba 20 20 2a 2a 20 66 69 6c 65 20 74 6f 20 74 68 65 ** file to the
11dbb 20 72 65 71 75 69 72 65 64 20 73 69 7a 65 2e 0a required size..
11dbc 20 20 2a 2f 20 0a 20 20 69 66 28 20 53 51 4c 49 */ . if( SQLI
11dbd 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c TE_OK==(rc = sql
11dbe 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 ite3OsFileSize(p
11dbf 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 6a 72 6e Pager->jfd, &jrn
11dc0 6c 53 69 7a 65 29 29 0a 20 20 20 26 26 20 6a 72 lSize)). && jr
11dc1 6e 6c 53 69 7a 65 3e 70 50 61 67 65 72 2d 3e 6a nlSize>pPager->j
11dc2 6f 75 72 6e 61 6c 4f 66 66 0a 20 20 29 7b 0a 20 ournalOff. ){.
11dc3 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
11dc4 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 sTruncate(pPager
11dc5 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a ->jfd, pPager->j
11dc6 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 20 7d 0a ournalOff);. }.
11dc7 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
11dc8 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 20 70 61 67 /*.** Find a pag
11dc9 65 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 e in the hash ta
11dca 62 6c 65 20 67 69 76 65 6e 20 69 74 73 20 70 61 ble given its pa
11dcb 67 65 20 6e 75 6d 62 65 72 2e 20 52 65 74 75 72 ge number. Retur
11dcc 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 n.** a pointer t
11dcd 6f 20 74 68 65 20 70 61 67 65 20 6f 72 20 4e 55 o the page or NU
11dce 4c 4c 20 69 66 20 74 68 65 20 72 65 71 75 65 73 LL if the reques
11dcf 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f 74 20 ted page is not
11dd0 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 6e 20 6d .** already in m
11dd1 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 emory..*/.static
11dd2 20 50 67 48 64 72 20 2a 70 61 67 65 72 5f 6c 6f PgHdr *pager_lo
11dd3 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 okup(Pager *pPag
11dd4 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a er, Pgno pgno){.
11dd5 20 20 50 67 48 64 72 20 2a 70 3b 20 20 20 20 20 PgHdr *p;
11dd6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11dd7 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61 /* Return va
11dd8 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 74 20 lue */.. /* It
11dd9 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
11dda 66 6f 72 20 61 20 63 61 6c 6c 20 74 6f 20 50 63 for a call to Pc
11ddb 61 63 68 65 46 65 74 63 68 28 29 20 77 69 74 68 acheFetch() with
11ddc 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 74 createFlag==0 t
11ddd 6f 0a 20 20 2a 2a 20 66 61 69 6c 2c 20 73 69 6e o. ** fail, sin
11dde 63 65 20 6e 6f 20 61 74 74 65 6d 70 74 20 74 6f ce no attempt to
11ddf 20 61 6c 6c 6f 63 61 74 65 20 64 79 6e 61 6d 69 allocate dynami
11de0 63 20 6d 65 6d 6f 72 79 20 77 69 6c 6c 20 62 65 c memory will be
11de1 20 6d 61 64 65 2e 0a 20 20 2a 2f 0a 20 20 28 76 made.. */. (v
11de2 6f 69 64 29 73 71 6c 69 74 65 33 50 63 61 63 68 oid)sqlite3Pcach
11de3 65 46 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70 eFetch(pPager->p
11de4 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 30 2c PCache, pgno, 0,
11de5 20 26 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 &p);. return p
11de6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 65 73 ;.}../*.** Unles
11de7 73 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 s the pager is i
11de8 6e 20 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 64 n error-state, d
11de9 69 73 63 61 72 64 20 61 6c 6c 20 69 6e 2d 6d 65 iscard all in-me
11dea 6d 6f 72 79 20 70 61 67 65 73 2e 20 49 66 0a 2a mory pages. If.*
11deb 2a 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 * the pager is i
11dec 6e 20 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 74 n error-state, t
11ded 68 65 6e 20 74 68 69 73 20 63 61 6c 6c 20 69 73 hen this call is
11dee 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 a no-op..**.**
11def 54 4f 44 4f 3a 20 57 68 79 20 63 61 6e 20 77 65 TODO: Why can we
11df0 20 6e 6f 74 20 72 65 73 65 74 20 74 68 65 20 70 not reset the p
11df1 61 67 65 72 20 77 68 69 6c 65 20 69 6e 20 65 72 ager while in er
11df2 72 6f 72 20 73 74 61 74 65 3f 0a 2a 2f 0a 73 74 ror state?.*/.st
11df3 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f atic void pager_
11df4 72 65 73 65 74 28 50 61 67 65 72 20 2a 70 50 61 reset(Pager *pPa
11df5 67 65 72 29 7b 0a 20 20 69 66 28 20 53 51 4c 49 ger){. if( SQLI
11df6 54 45 5f 4f 4b 3d 3d 70 50 61 67 65 72 2d 3e 65 TE_OK==pPager->e
11df7 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 73 71 rrCode ){. sq
11df8 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 lite3BackupResta
11df9 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b rt(pPager->pBack
11dfa 75 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 up);. sqlite3
11dfb 50 63 61 63 68 65 43 6c 65 61 72 28 70 50 61 67 PcacheClear(pPag
11dfc 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 er->pPCache);.
11dfd 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
11dfe 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 7d Valid = 0;. }.}
11dff 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c ../*.** Free all
11e00 20 73 74 72 75 63 74 75 72 65 73 20 69 6e 20 74 structures in t
11e01 68 65 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f he Pager.aSavepo
11e02 69 6e 74 5b 5d 20 61 72 72 61 79 20 61 6e 64 20 int[] array and
11e03 73 65 74 20 62 6f 74 68 0a 2a 2a 20 50 61 67 65 set both.** Page
11e04 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 61 6e 64 r.aSavepoint and
11e05 20 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e Pager.nSavepoin
11e06 74 20 74 6f 20 7a 65 72 6f 2e 20 43 6c 6f 73 65 t to zero. Close
11e07 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
11e08 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 6f 70 65 .** if it is ope
11e09 6e 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 20 n and the pager
11e0a 69 73 20 6e 6f 74 20 69 6e 20 65 78 63 6c 75 73 is not in exclus
11e0b 69 76 65 20 6d 6f 64 65 2e 0a 2a 2f 0a 73 74 61 ive mode..*/.sta
11e0c 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 tic void release
11e0d 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 50 61 AllSavepoints(Pa
11e0e 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
11e0f 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 int ii;
11e10 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f /* Iterato
11e11 72 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 r for looping th
11e12 72 6f 75 67 68 20 50 61 67 65 72 2e 61 53 61 76 rough Pager.aSav
11e13 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 66 6f 72 28 epoint */. for(
11e14 69 69 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d ii=0; ii<pPager-
11e15 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b >nSavepoint; ii+
11e16 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 +){. sqlite3B
11e17 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 itvecDestroy(pPa
11e18 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b ger->aSavepoint[
11e19 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 ii].pInSavepoint
11e1a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 50 );. }. if( !pP
11e1b 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d ager->exclusiveM
11e1c 6f 64 65 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 ode || sqlite3Is
11e1d 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 MemJournal(pPage
11e1e 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 r->sjfd) ){.
11e1f 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 sqlite3OsClose(p
11e20 50 61 67 65 72 2d 3e 73 6a 66 64 29 3b 0a 20 20 Pager->sjfd);.
11e21 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 }. sqlite3_free
11e22 28 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f (pPager->aSavepo
11e23 69 6e 74 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e int);. pPager->
11e24 61 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a aSavepoint = 0;.
11e25 20 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 pPager->nSavep
11e26 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 oint = 0;. pPag
11e27 65 72 2d 3e 6e 53 75 62 52 65 63 20 3d 20 30 3b er->nSubRec = 0;
11e28 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 .}../*.** Set th
11e29 65 20 62 69 74 20 6e 75 6d 62 65 72 20 70 67 6e e bit number pgn
11e2a 6f 20 69 6e 20 74 68 65 20 50 61 67 65 72 53 61 o in the PagerSa
11e2b 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 vepoint.pInSavep
11e2c 6f 69 6e 74 20 0a 2a 2a 20 62 69 74 76 65 63 73 oint .** bitvecs
11e2d 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 73 61 76 of all open sav
11e2e 65 70 6f 69 6e 74 73 2e 20 52 65 74 75 72 6e 20 epoints. Return
11e2f 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 SQLITE_OK if suc
11e30 63 65 73 73 66 75 6c 0a 2a 2a 20 6f 72 20 53 51 cessful.** or SQ
11e31 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 LITE_NOMEM if a
11e32 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f malloc failure o
11e33 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 ccurs..*/.static
11e34 20 69 6e 74 20 61 64 64 54 6f 53 61 76 65 70 6f int addToSavepo
11e35 69 6e 74 42 69 74 76 65 63 73 28 50 61 67 65 72 intBitvecs(Pager
11e36 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 *pPager, Pgno p
11e37 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20 gno){. int ii;
11e38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11e39 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
11e3a 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 r */. int rc =
11e3b 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
11e3c 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 /* Result code
11e3d 2a 2f 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 */.. for(ii=0;
11e3e 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 ii<pPager->nSave
11e3f 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 point; ii++){.
11e40 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 PagerSavepoint
11e41 20 2a 70 20 3d 20 26 70 50 61 67 65 72 2d 3e 61 *p = &pPager->a
11e42 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 3b 0a 20 Savepoint[ii];.
11e43 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 2d 3e if( pgno<=p->
11e44 6e 4f 72 69 67 20 29 7b 0a 20 20 20 20 20 20 72 nOrig ){. r
11e45 63 20 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76 c |= sqlite3Bitv
11e46 65 63 53 65 74 28 70 2d 3e 70 49 6e 53 61 76 65 ecSet(p->pInSave
11e47 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 3b 0a 20 20 point, pgno);.
11e48 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 testcase( rc
11e49 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 ==SQLITE_NOMEM )
11e4a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
11e4b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
11e4c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc==SQLITE_NOME
11e4d 4d 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 M );. }. }.
11e4e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
11e4f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 74 68 65 20 *.** Unlock the
11e50 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 database file. T
11e51 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
11e52 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 70 a no-op if the p
11e53 61 67 65 72 0a 2a 2a 20 69 73 20 69 6e 20 65 78 ager.** is in ex
11e54 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e 0a 2a 2a clusive mode..**
11e55 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 .** If the pager
11e56 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e is currently in
11e57 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 64 69 error state, di
11e58 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e scard the conten
11e59 74 73 20 6f 66 20 0a 2a 2a 20 74 68 65 20 63 61 ts of .** the ca
11e5a 63 68 65 20 61 6e 64 20 72 65 73 65 74 20 74 68 che and reset th
11e5b 65 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72 e Pager structur
11e5c 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 e internal state
11e5d 2e 20 49 66 20 74 68 65 72 65 20 69 73 0a 2a 2a . If there is.**
11e5e 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c an open journal
11e5f 2d 66 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 -file, then the
11e60 6e 65 78 74 20 74 69 6d 65 20 61 20 73 68 61 72 next time a shar
11e61 65 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 ed-lock is obtai
11e62 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 70 61 ned.** on the pa
11e63 67 65 72 20 66 69 6c 65 20 28 62 79 20 74 68 69 ger file (by thi
11e64 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 s or any other p
11e65 72 6f 63 65 73 73 29 2c 20 69 74 20 77 69 6c 6c rocess), it will
11e66 20 62 65 0a 2a 2a 20 74 72 65 61 74 65 64 20 61 be.** treated a
11e67 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 s a hot-journal
11e68 61 6e 64 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e and rolled back.
11e69 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
11e6a 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 50 61 67 pager_unlock(Pag
11e6b 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 er *pPager){. i
11e6c 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c f( !pPager->excl
11e6d 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 usiveMode ){.
11e6e 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
11e6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11e70 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
11e71 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 63 . /* Always c
11e72 6c 6f 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c lose the journal
11e73 20 66 69 6c 65 20 77 68 65 6e 20 64 72 6f 70 70 file when dropp
11e74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 ing the database
11e75 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2a 20 4f 74 lock.. ** Ot
11e76 68 65 72 77 69 73 65 2c 20 61 6e 6f 74 68 65 72 herwise, another
11e77 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 connection with
11e78 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 64 65 journal_mode=de
11e79 6c 65 74 65 20 6d 69 67 68 74 0a 20 20 20 20 2a lete might. *
11e7a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 66 69 6c * delete the fil
11e7b 65 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 e out from under
11e7c 20 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 us.. */.
11e7d 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 sqlite3OsClose(p
11e7e 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 Pager->jfd);.
11e7f 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 sqlite3BitvecDe
11e80 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49 stroy(pPager->pI
11e81 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 70 nJournal);. p
11e82 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
11e83 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 6c 65 61 l = 0;. relea
11e84 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 seAllSavepoints(
11e85 70 50 61 67 65 72 29 3b 0a 0a 20 20 20 20 2f 2a pPager);.. /*
11e86 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 If the file is
11e87 75 6e 6c 6f 63 6b 65 64 2c 20 73 6f 6d 65 62 6f unlocked, somebo
11e88 64 79 20 65 6c 73 65 20 6d 69 67 68 74 20 63 68 dy else might ch
11e89 61 6e 67 65 20 69 74 2e 20 54 68 65 0a 20 20 20 ange it. The.
11e8a 20 2a 2a 20 76 61 6c 75 65 73 20 73 74 6f 72 65 ** values store
11e8b 64 20 69 6e 20 50 61 67 65 72 2e 64 62 53 69 7a d in Pager.dbSiz
11e8c 65 20 65 74 63 2e 20 6d 69 67 68 74 20 62 65 63 e etc. might bec
11e8d 6f 6d 65 20 69 6e 76 61 6c 69 64 20 69 66 0a 20 ome invalid if.
11e8e 20 20 20 2a 2a 20 74 68 69 73 20 68 61 70 70 65 ** this happe
11e8f 6e 73 2e 20 54 4f 44 4f 3a 20 52 65 61 6c 6c 79 ns. TODO: Really
11e90 2c 20 74 68 69 73 20 64 6f 65 73 6e 27 74 20 6e , this doesn't n
11e91 65 65 64 20 74 6f 20 62 65 20 63 6c 65 61 72 65 eed to be cleare
11e92 64 0a 20 20 20 20 2a 2a 20 75 6e 74 69 6c 20 74 d. ** until t
11e93 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 he change-counte
11e94 72 20 63 68 65 63 6b 20 66 61 69 6c 73 20 69 6e r check fails in
11e95 20 50 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b PagerSharedLock
11e96 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 ().. */. p
11e97 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c Pager->dbSizeVal
11e98 69 64 20 3d 20 30 3b 0a 0a 20 20 20 20 72 63 20 id = 0;.. rc
11e99 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 = osUnlock(pPage
11e9a 72 2d 3e 66 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b r->fd, NO_LOCK);
11e9b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 . if( rc ){.
11e9c 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 pPager->err
11e9d 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 20 20 7d Code = rc;. }
11e9e 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 55 . IOTRACE(("U
11e9f 4e 4c 4f 43 4b 20 25 70 5c 6e 22 2c 20 70 50 61 NLOCK %p\n", pPa
11ea0 67 65 72 29 29 0a 0a 20 20 20 20 2f 2a 20 49 66 ger)).. /* If
11ea1 20 50 61 67 65 72 2e 65 72 72 43 6f 64 65 20 69 Pager.errCode i
11ea2 73 20 73 65 74 2c 20 74 68 65 20 63 6f 6e 74 65 s set, the conte
11ea3 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 nts of the pager
11ea4 20 63 61 63 68 65 20 63 61 6e 6e 6f 74 20 62 65 cache cannot be
11ea5 0a 20 20 20 20 2a 2a 20 74 72 75 73 74 65 64 2e . ** trusted.
11ea6 20 4e 6f 77 20 74 68 61 74 20 74 68 65 20 70 61 Now that the pa
11ea7 67 65 72 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f ger file is unlo
11ea8 63 6b 65 64 2c 20 74 68 65 20 63 6f 6e 74 65 6e cked, the conten
11ea9 74 73 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a ts of the. **
11eaa 20 63 61 63 68 65 20 63 61 6e 20 62 65 20 64 69 cache can be di
11eab 73 63 61 72 64 65 64 20 61 6e 64 20 74 68 65 20 scarded and the
11eac 65 72 72 6f 72 20 63 6f 64 65 20 73 61 66 65 6c error code safel
11ead 79 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 2a y cleared.. *
11eae 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 /. if( pPager
11eaf 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 ->errCode ){.
11eb0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
11eb1 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
11eb2 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
11eb3 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
11eb4 20 20 20 7d 0a 20 20 20 20 20 20 70 61 67 65 72 }. pager
11eb5 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a _reset(pPager);.
11eb6 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 }.. pPage
11eb7 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f r->changeCountDo
11eb8 6e 65 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 ne = 0;. pPag
11eb9 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 er->state = PAGE
11eba 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 7d 0a 7d 0a R_UNLOCK;. }.}.
11ebb 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
11ebc 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 63 tion should be c
11ebd 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 49 4f alled when an IO
11ebe 45 52 52 2c 20 43 4f 52 52 55 50 54 20 6f 72 20 ERR, CORRUPT or
11ebf 46 55 4c 4c 20 65 72 72 6f 72 0a 2a 2a 20 6d 61 FULL error.** ma
11ec0 79 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 2e y have occurred.
11ec1 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d The first argum
11ec2 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ent is a pointer
11ec3 20 74 6f 20 74 68 65 20 70 61 67 65 72 20 0a 2a to the pager .*
11ec4 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 * structure, the
11ec5 20 73 65 63 6f 6e 64 20 74 68 65 20 65 72 72 6f second the erro
11ec6 72 2d 63 6f 64 65 20 61 62 6f 75 74 20 74 6f 20 r-code about to
11ec7 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 be returned by a
11ec8 20 70 61 67 65 72 20 0a 2a 2a 20 41 50 49 20 66 pager .** API f
11ec9 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c unction. The val
11eca 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 ue returned is a
11ecb 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 65 63 copy of the sec
11ecc 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a ond argument .**
11ecd 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f to this functio
11ece 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 n. .**.** If the
11ecf 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
11ed0 20 69 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 is SQLITE_IOERR
11ed1 2c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 , SQLITE_CORRUPT
11ed2 2c 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c , or SQLITE_FULL
11ed3 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 62 65 .** the error be
11ed4 63 6f 6d 65 73 20 70 65 72 73 69 73 74 65 6e 74 comes persistent
11ed5 2e 20 55 6e 74 69 6c 20 74 68 65 20 70 65 72 73 . Until the pers
11ed6 69 73 74 65 6e 20 65 72 72 6f 72 20 69 73 20 63 isten error is c
11ed7 6c 65 61 72 65 64 2c 0a 2a 2a 20 73 75 62 73 65 leared,.** subse
11ed8 71 75 65 6e 74 20 41 50 49 20 63 61 6c 6c 73 20 quent API calls
11ed9 6f 6e 20 74 68 69 73 20 50 61 67 65 72 20 77 69 on this Pager wi
11eda 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 72 ll immediately r
11edb 65 74 75 72 6e 20 74 68 65 20 73 61 6d 65 20 0a eturn the same .
11edc 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a ** error code..*
11edd 2a 0a 2a 2a 20 41 20 70 65 72 73 69 73 74 65 6e *.** A persisten
11ede 74 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 65 t error indicate
11edf 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 s that the conte
11ee0 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 nts of the pager
11ee1 2d 63 61 63 68 65 20 0a 2a 2a 20 63 61 6e 6e 6f -cache .** canno
11ee2 74 20 62 65 20 74 72 75 73 74 65 64 2e 20 54 68 t be trusted. Th
11ee3 69 73 20 73 74 61 74 65 20 63 61 6e 20 62 65 20 is state can be
11ee4 63 6c 65 61 72 65 64 20 62 79 20 63 6f 6d 70 6c cleared by compl
11ee5 65 74 65 6c 79 20 64 69 73 63 61 72 64 69 6e 67 etely discarding
11ee6 20 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 .** the content
11ee7 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63 s of the pager-c
11ee8 61 63 68 65 2e 20 49 66 20 61 20 74 72 61 6e 73 ache. If a trans
11ee9 61 63 74 69 6f 6e 20 77 61 73 20 61 63 74 69 76 action was activ
11eea 65 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 65 e when.** the pe
11eeb 72 73 69 73 74 65 6e 74 20 65 72 72 6f 72 20 6f rsistent error o
11eec 63 63 75 72 72 65 64 2c 20 74 68 65 6e 20 74 68 ccurred, then th
11eed 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
11eee 61 6c 20 6d 61 79 20 6e 65 65 64 0a 2a 2a 20 74 al may need.** t
11eef 6f 20 62 65 20 72 65 70 6c 61 79 65 64 20 74 6f o be replayed to
11ef0 20 72 65 73 74 6f 72 65 20 74 68 65 20 63 6f 6e restore the con
11ef1 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74 tents of the dat
11ef2 61 62 61 73 65 20 66 69 6c 65 20 28 61 73 20 69 abase file (as i
11ef3 66 0a 2a 2a 20 69 74 20 77 65 72 65 20 61 20 68 f.** it were a h
11ef4 6f 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2f 0a ot-journal)..*/.
11ef5 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
11ef6 5f 65 72 72 6f 72 28 50 61 67 65 72 20 2a 70 50 _error(Pager *pP
11ef7 61 67 65 72 2c 20 69 6e 74 20 72 63 29 7b 0a 20 ager, int rc){.
11ef8 20 69 6e 74 20 72 63 32 20 3d 20 72 63 20 26 20 int rc2 = rc &
11ef9 30 78 66 66 3b 0a 20 20 61 73 73 65 72 74 28 20 0xff;. assert(
11efa 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
11efb 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 61 73 73 !MEMDB );. ass
11efc 65 72 74 28 0a 20 20 20 20 20 20 20 70 50 61 67 ert(. pPag
11efd 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c er->errCode==SQL
11efe 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 20 20 ITE_FULL ||.
11eff 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f pPager->errCo
11f00 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c de==SQLITE_OK ||
11f01 0a 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d . (pPager-
11f02 3e 65 72 72 43 6f 64 65 20 26 20 30 78 66 66 29 >errCode & 0xff)
11f03 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 ==SQLITE_IOERR.
11f04 20 29 3b 0a 20 20 69 66 28 20 72 63 32 3d 3d 53 );. if( rc2==S
11f05 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 20 72 63 QLITE_FULL || rc
11f06 32 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 2==SQLITE_IOERR
11f07 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 ){. pPager->e
11f08 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 7d rrCode = rc;. }
11f09 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
11f0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 ./*.** Execute a
11f0b 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 61 20 74 rollback if a t
11f0c 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 ransaction is ac
11f0d 74 69 76 65 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 tive and unlock
11f0e 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 the .** database
11f0f 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 file. .**.** If
11f10 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 61 the pager has a
11f11 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64 20 74 lready entered t
11f12 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 he error state,
11f13 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 0a do not attempt .
11f14 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 ** the rollback
11f15 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20 49 6e at this time. In
11f16 73 74 65 61 64 2c 20 70 61 67 65 72 5f 75 6e 6c stead, pager_unl
11f17 6f 63 6b 28 29 20 69 73 20 63 61 6c 6c 65 64 2e ock() is called.
11f18 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 The.** call to
11f19 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 77 pager_unlock() w
11f1a 69 6c 6c 20 64 69 73 63 61 72 64 20 61 6c 6c 20 ill discard all
11f1b 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 2c in-memory pages,
11f1c 20 75 6e 6c 6f 63 6b 0a 2a 2a 20 74 68 65 20 64 unlock.** the d
11f1d 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
11f1e 20 63 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 clear the error
11f1f 20 73 74 61 74 65 2e 20 49 66 20 74 68 69 73 20 state. If this
11f20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 means that.** th
11f21 65 72 65 20 69 73 20 61 20 68 6f 74 2d 6a 6f 75 ere is a hot-jou
11f22 72 6e 61 6c 20 6c 65 66 74 20 69 6e 20 74 68 65 rnal left in the
11f23 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68 file-system, th
11f24 65 20 6e 65 78 74 20 63 6f 6e 6e 65 63 74 69 6f e next connectio
11f25 6e 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 61 n.** to obtain a
11f26 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 shared lock on
11f27 74 68 65 20 70 61 67 65 72 20 28 77 68 69 63 68 the pager (which
11f28 20 6d 61 79 20 62 65 20 74 68 69 73 20 6f 6e 65 may be this one
11f29 29 20 77 69 6c 6c 0a 2a 2a 20 72 6f 6c 6c 20 69 ) will.** roll i
11f2a 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 t back..**.** If
11f2b 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 6e the pager has n
11f2c 6f 74 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 ot already enter
11f2d 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 ed the error sta
11f2e 74 65 2c 20 62 75 74 20 61 6e 20 49 4f 20 6f 72 te, but an IO or
11f2f 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 .** malloc error
11f30 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 61 occurs during a
11f31 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 6e 20 rollback, then
11f32 74 68 69 73 20 77 69 6c 6c 20 69 74 73 65 6c 66 this will itself
11f33 20 63 61 75 73 65 20 0a 2a 2a 20 74 68 65 20 70 cause .** the p
11f34 61 67 65 72 20 74 6f 20 65 6e 74 65 72 20 74 68 ager to enter th
11f35 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 57 e error state. W
11f36 68 69 63 68 20 77 69 6c 6c 20 62 65 20 63 6c 65 hich will be cle
11f37 61 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 ared by the.** c
11f38 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e 6c all to pager_unl
11f39 6f 63 6b 28 29 2c 20 61 73 20 64 65 73 63 72 69 ock(), as descri
11f3a 62 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 bed above..*/.st
11f3b 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 55 atic void pagerU
11f3c 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b nlockAndRollback
11f3d 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
11f3e 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 . if( pPager->e
11f3f 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f rrCode==SQLITE_O
11f40 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 73 74 61 K && pPager->sta
11f41 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 te>=PAGER_RESERV
11f42 45 44 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ED ){. sqlite
11f43 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 3BeginBenignMall
11f44 6f 63 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 oc();. sqlite
11f45 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 3PagerRollback(p
11f46 50 61 67 65 72 29 3b 0a 20 20 20 20 73 71 6c 69 Pager);. sqli
11f47 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
11f48 6f 63 28 29 3b 0a 20 20 7d 0a 20 20 70 61 67 65 oc();. }. page
11f49 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 r_unlock(pPager)
11f4a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
11f4b 72 6f 75 74 69 6e 65 20 65 6e 64 73 20 61 20 74 routine ends a t
11f4c 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20 74 72 ransaction. A tr
11f4d 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 75 73 75 ansaction is usu
11f4e 61 6c 6c 79 20 65 6e 64 65 64 20 62 79 20 0a 2a ally ended by .*
11f4f 2a 20 65 69 74 68 65 72 20 61 20 43 4f 4d 4d 49 * either a COMMI
11f50 54 20 6f 72 20 61 20 52 4f 4c 4c 42 41 43 4b 20 T or a ROLLBACK
11f51 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 69 73 20 operation. This
11f52 72 6f 75 74 69 6e 65 20 6d 61 79 20 62 65 20 63 routine may be c
11f53 61 6c 6c 65 64 20 0a 2a 2a 20 61 66 74 65 72 20 alled .** after
11f54 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20 68 6f rollback of a ho
11f55 74 2d 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 69 66 t-journal, or if
11f56 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
11f57 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e 67 0a 2a while opening.*
11f58 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 * the journal fi
11f59 6c 65 20 6f 72 20 77 72 69 74 69 6e 67 20 74 68 le or writing th
11f5a 65 20 76 65 72 79 20 66 69 72 73 74 20 6a 6f 75 e very first jou
11f5b 72 6e 61 6c 2d 68 65 61 64 65 72 20 6f 66 20 61 rnal-header of a
11f5c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 72 61 .** database tra
11f5d 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 0a 2a 2a nsaction..** .**
11f5e 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 If the pager is
11f5f 20 69 6e 20 50 41 47 45 52 5f 53 48 41 52 45 44 in PAGER_SHARED
11f60 20 6f 72 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b or PAGER_UNLOCK
11f61 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 state when this
11f62 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63 .** routine is c
11f63 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e alled, it is a n
11f64 6f 2d 6f 70 20 28 72 65 74 75 72 6e 73 20 53 51 o-op (returns SQ
11f65 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 LITE_OK)..**.**
11f66 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 79 20 61 Otherwise, any a
11f67 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 ctive savepoints
11f68 20 61 72 65 20 72 65 6c 65 61 73 65 64 2e 0a 2a are released..*
11f69 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 *.** If the jour
11f6a 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e nal file is open
11f6b 2c 20 74 68 65 6e 20 69 74 20 69 73 20 22 66 69 , then it is "fi
11f6c 6e 61 6c 69 7a 65 64 22 2e 20 4f 6e 63 65 20 61 nalized". Once a
11f6d 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 66 69 6c journal .** fil
11f6e 65 20 68 61 73 20 62 65 65 6e 20 66 69 6e 61 6c e has been final
11f6f 69 7a 65 64 20 69 74 20 69 73 20 6e 6f 74 20 70 ized it is not p
11f70 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 69 ossible to use i
11f71 74 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 61 t to roll back a
11f72 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e .** transaction
11f73 2e 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 62 65 . Nor will it be
11f74 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 considered to b
11f75 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 e a hot-journal
11f76 62 79 20 74 68 69 73 0a 2a 2a 20 6f 72 20 61 6e by this.** or an
11f77 79 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 y other database
11f78 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 45 78 61 connection. Exa
11f79 63 74 6c 79 20 68 6f 77 20 61 20 6a 6f 75 72 6e ctly how a journ
11f7a 61 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 0a al is finalized.
11f7b 2a 2a 20 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 ** depends on wh
11f7c 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 ether or not the
11f7d 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e pager is runnin
11f7e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d g in exclusive m
11f7f 6f 64 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 63 ode and.** the c
11f80 75 72 72 65 6e 74 20 6a 6f 75 72 6e 61 6c 2d 6d urrent journal-m
11f81 6f 64 65 20 28 50 61 67 65 72 2e 6a 6f 75 72 6e ode (Pager.journ
11f82 61 6c 4d 6f 64 65 20 76 61 6c 75 65 29 2c 20 61 alMode value), a
11f83 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
11f84 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d journalMode==
11f85 4d 45 4d 4f 52 59 0a 2a 2a 20 20 20 20 20 4a 6f MEMORY.** Jo
11f86 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 urnal file descr
11f87 69 70 74 6f 72 20 69 73 20 73 69 6d 70 6c 79 20 iptor is simply
11f88 63 6c 6f 73 65 64 2e 20 54 68 69 73 20 64 65 73 closed. This des
11f89 74 72 6f 79 73 20 61 6e 20 0a 2a 2a 20 20 20 20 troys an .**
11f8a 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e in-memory journ
11f8b 61 6c 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 al..**.** jour
11f8c 6e 61 6c 4d 6f 64 65 3d 3d 54 52 55 4e 43 41 54 nalMode==TRUNCAT
11f8d 45 0a 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c E.** Journal
11f8e 20 66 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74 file is truncat
11f8f 65 64 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 ed to zero bytes
11f90 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 in size..**.**
11f91 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 journalMode==P
11f92 45 52 53 49 53 54 0a 2a 2a 20 20 20 20 20 54 68 ERSIST.** Th
11f93 65 20 66 69 72 73 74 20 32 38 20 62 79 74 65 73 e first 28 bytes
11f94 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
11f95 66 69 6c 65 20 61 72 65 20 7a 65 72 6f 65 64 2e file are zeroed.
11f96 20 54 68 69 73 20 69 6e 76 61 6c 69 64 61 74 65 This invalidate
11f97 73 0a 2a 2a 20 20 20 20 20 74 68 65 20 66 69 72 s.** the fir
11f98 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 st journal heade
11f99 72 20 69 6e 20 74 68 65 20 66 69 6c 65 2c 20 61 r in the file, a
11f9a 6e 64 20 68 65 6e 63 65 20 74 68 65 20 65 6e 74 nd hence the ent
11f9b 69 72 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 ire journal.**
11f9c 20 20 20 66 69 6c 65 2e 20 41 6e 20 69 6e 76 61 file. An inva
11f9d 6c 69 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 lid journal file
11f9e 20 63 61 6e 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 cannot be rolle
11f9f 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 d back..**.**
11fa0 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 44 45 4c journalMode==DEL
11fa1 45 54 45 0a 2a 2a 20 20 20 20 20 54 68 65 20 6a ETE.** The j
11fa2 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 ournal file is c
11fa3 6c 6f 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65 losed and delete
11fa4 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f d using sqlite3O
11fa5 73 44 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a sDelete()..**.**
11fa6 20 20 20 20 20 49 66 20 74 68 65 20 70 61 67 65 If the page
11fa7 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 r is running in
11fa8 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 exclusive mode,
11fa9 74 68 69 73 20 6d 65 74 68 6f 64 20 6f 66 20 66 this method of f
11faa 69 6e 61 6c 69 7a 69 6e 67 0a 2a 2a 20 20 20 20 inalizing.**
11fab 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
11fac 65 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e e is never used.
11fad 20 49 6e 73 74 65 61 64 2c 20 69 66 20 74 68 65 Instead, if the
11fae 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 69 73 0a journalMode is.
11faf 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 61 6e ** DELETE an
11fb0 64 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 d the pager is i
11fb1 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 n exclusive mode
11fb2 2c 20 74 68 65 20 6d 65 74 68 6f 64 20 64 65 73 , the method des
11fb3 63 72 69 62 65 64 20 75 6e 64 65 72 0a 2a 2a 20 cribed under.**
11fb4 20 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d journalMode=
11fb5 3d 50 45 52 53 49 53 54 20 69 73 20 75 73 65 64 =PERSIST is used
11fb6 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 instead..**.**
11fb7 41 66 74 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 After the journa
11fb8 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 2c 20 l is finalized,
11fb9 69 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 6e 6f if running in no
11fba 6e 2d 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 n-exclusive mode
11fbb 2c 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 6d , the.** pager m
11fbc 6f 76 65 73 20 74 6f 20 50 41 47 45 52 5f 53 48 oves to PAGER_SH
11fbd 41 52 45 44 20 73 74 61 74 65 20 28 61 6e 64 20 ARED state (and
11fbe 64 6f 77 6e 67 72 61 64 65 73 20 74 68 65 20 6c downgrades the l
11fbf 6f 63 6b 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61 ock on the.** da
11fc0 74 61 62 61 73 65 20 66 69 6c 65 20 61 63 63 6f tabase file acco
11fc1 72 64 69 6e 67 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 rdingly)..**.**
11fc2 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 If the pager is
11fc3 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 running in exclu
11fc4 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 73 sive mode and is
11fc5 20 69 6e 20 50 41 47 45 52 5f 53 59 4e 43 45 44 in PAGER_SYNCED
11fc6 20 73 74 61 74 65 2c 0a 2a 2a 20 69 74 20 6d 6f state,.** it mo
11fc7 76 65 73 20 74 6f 20 50 41 47 45 52 5f 45 58 43 ves to PAGER_EXC
11fc8 4c 55 53 49 56 45 2e 20 4e 6f 20 6c 6f 63 6b 73 LUSIVE. No locks
11fc9 20 61 72 65 20 64 6f 77 6e 67 72 61 64 65 64 20 are downgraded
11fca 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 0a when running in.
11fcb 2a 2a 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 ** exclusive mod
11fcc 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f e..**.** SQLITE_
11fcd 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 OK is returned i
11fce 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 f no error occur
11fcf 73 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f s. If an error o
11fd0 63 63 75 72 73 20 64 75 72 69 6e 67 0a 2a 2a 20 ccurs during.**
11fd1 61 6e 79 20 6f 66 20 74 68 65 20 49 4f 20 6f 70 any of the IO op
11fd2 65 72 61 74 69 6f 6e 73 20 74 6f 20 66 69 6e 61 erations to fina
11fd3 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c lize the journal
11fd4 20 66 69 6c 65 20 6f 72 20 75 6e 6c 6f 63 6b 20 file or unlock
11fd5 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
11fd6 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f then the IO erro
11fd7 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
11fd8 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 ed to the user.
11fd9 49 66 20 74 68 65 20 0a 2a 2a 20 6f 70 65 72 61 If the .** opera
11fda 74 69 6f 6e 20 74 6f 20 66 69 6e 61 6c 69 7a 65 tion to finalize
11fdb 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
11fdc 65 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 68 e fails, then th
11fdd 65 20 63 6f 64 65 20 73 74 69 6c 6c 0a 2a 2a 20 e code still.**
11fde 74 72 69 65 73 20 74 6f 20 75 6e 6c 6f 63 6b 20 tries to unlock
11fdf 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
11fe0 65 20 69 66 20 6e 6f 74 20 69 6e 20 65 78 63 6c e if not in excl
11fe1 75 73 69 76 65 20 6d 6f 64 65 2e 20 49 66 20 74 usive mode. If t
11fe2 68 65 0a 2a 2a 20 75 6e 6c 6f 63 6b 20 6f 70 65 he.** unlock ope
11fe3 72 61 74 69 6f 6e 20 66 61 69 6c 73 20 61 73 20 ration fails as
11fe4 77 65 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20 66 well, then the f
11fe5 69 72 73 74 20 65 72 72 6f 72 20 63 6f 64 65 20 irst error code
11fe6 72 65 6c 61 74 65 64 0a 2a 2a 20 74 6f 20 74 68 related.** to th
11fe7 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 65 6e e first error en
11fe8 63 6f 75 6e 74 65 72 65 64 20 28 74 68 65 20 6a countered (the j
11fe9 6f 75 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 74 ournal finalizat
11fea 69 6f 6e 20 6f 6e 65 29 20 69 73 0a 2a 2a 20 72 ion one) is.** r
11feb 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 eturned..*/.stat
11fec 69 63 20 69 6e 74 20 70 61 67 65 72 5f 65 6e 64 ic int pager_end
11fed 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 67 _transaction(Pag
11fee 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
11fef 68 61 73 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e hasMaster){. in
11ff0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
11ff1 3b 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 ; /* Error
11ff2 63 6f 64 65 20 66 72 6f 6d 20 6a 6f 75 72 6e 61 code from journa
11ff3 6c 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f l finalization o
11ff4 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e peration */. in
11ff5 74 20 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f t rc2 = SQLITE_O
11ff6 4b 3b 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 K; /* Error
11ff7 63 6f 64 65 20 66 72 6f 6d 20 64 62 20 66 69 6c code from db fil
11ff8 65 20 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 74 69 e unlock operati
11ff9 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 50 61 on */.. if( pPa
11ffa 67 65 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 52 ger->state<PAGER
11ffb 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 _RESERVED ){.
11ffc 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
11ffd 4b 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 K;. }. release
11ffe 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50 AllSavepoints(pP
11fff 61 67 65 72 29 3b 0a 0a 20 20 61 73 73 65 72 74 ager);.. assert
12000 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
12001 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d >jfd) || pPager-
12002 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 >pInJournal==0 )
12003 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 ;. if( isOpen(p
12004 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 0a Pager->jfd) ){..
12005 20 20 20 20 2f 2a 20 46 69 6e 61 6c 69 7a 65 20 /* Finalize
12006 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
12007 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c . */. if( sql
12008 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c ite3IsMemJournal
12009 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b (pPager->jfd) ){
1200a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1200b 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
1200c 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
1200d 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a LMODE_MEMORY );.
1200e 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 sqlite3OsC
1200f 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 lose(pPager->jfd
12010 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 );. }else if(
12011 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12012 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
12013 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 NALMODE_TRUNCATE
12014 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 ){. if( pP
12015 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
12016 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ==0 ){. r
12017 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
12018 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
12019 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1201a 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 OsTruncate(pPage
1201b 72 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 r->jfd, 0);.
1201c 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 }. pPager
1201d 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 ->journalOff = 0
1201e 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e ;. pPager->
1201f 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d journalStarted =
12020 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 0;. }else if
12021 28 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 ( pPager->exclus
12022 69 76 65 4d 6f 64 65 20 0a 20 20 20 20 20 7c 7c iveMode . ||
12023 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12024 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
12025 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a NALMODE_PERSIST.
12026 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 ){. rc
12027 3d 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 = zeroJournalHdr
12028 28 70 50 61 67 65 72 2c 20 68 61 73 4d 61 73 74 (pPager, hasMast
12029 65 72 29 3b 0a 20 20 20 20 20 20 70 61 67 65 72 er);. pager
1202a 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 _error(pPager, r
1202b 63 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 c);. pPager
1202c 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 ->journalOff = 0
1202d 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e ;. pPager->
1202e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d journalStarted =
1202f 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 0;. }else{.
12030 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 /* This bra
12031 6e 63 68 20 6d 61 79 20 62 65 20 65 78 65 63 75 nch may be execu
12032 74 65 64 20 77 69 74 68 20 50 61 67 65 72 2e 6a ted with Pager.j
12033 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 4d 4f ournalMode==MEMO
12034 52 59 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 61 RY if. ** a
12035 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 77 61 73 hot-journal was
12036 20 6a 75 73 74 20 72 6f 6c 6c 65 64 20 62 61 63 just rolled bac
12037 6b 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 k. In this case
12038 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 the journal.
12039 20 20 2a 2a 20 66 69 6c 65 20 73 68 6f 75 6c 64 ** file should
1203a 20 62 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 64 be closed and d
1203b 65 6c 65 74 65 64 2e 20 49 66 20 74 68 69 73 20 eleted. If this
1203c 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 72 69 74 65 connection write
1203d 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 s to. ** th
1203e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
1203f 20 69 74 20 77 69 6c 6c 20 64 6f 20 73 6f 20 75 it will do so u
12040 73 69 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 sing an in-memor
12041 79 20 6a 6f 75 72 6e 61 6c 2e 20 20 2a 2f 0a 20 y journal. */.
12042 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
12043 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
12044 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
12045 4f 44 45 5f 44 45 4c 45 54 45 20 0a 20 20 20 20 ODE_DELETE .
12046 20 20 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 || pPager
12047 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
12048 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
12049 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 20 29 _MEMORY . )
1204a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f ;. sqlite3O
1204b 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a sClose(pPager->j
1204c 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 fd);. if( !
1204d 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
1204e 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
1204f 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 sqlite3OsDelete
12050 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 (pPager->pVfs, p
12051 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c Pager->zJournal,
12052 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0);. }.
12053 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 }..#ifdef SQLIT
12054 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 E_CHECK_PAGES.
12055 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 sqlite3PcacheI
12056 74 65 72 61 74 65 44 69 72 74 79 28 70 50 61 67 terateDirty(pPag
12057 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67 er->pPCache, pag
12058 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 29 er_set_pagehash)
12059 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 73 71 ;.#endif.. sq
1205a 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e lite3PcacheClean
1205b 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 All(pPager->pPCa
1205c 63 68 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 che);. sqlite
1205d 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 3BitvecDestroy(p
1205e 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
1205f 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e l);. pPager->
12060 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a pInJournal = 0;.
12061 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 pPager->nRec
12062 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 = 0;. }.. if(
12063 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 !pPager->exclus
12064 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72 iveMode ){. r
12065 63 32 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 c2 = osUnlock(pP
12066 61 67 65 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 ager->fd, SHARED
12067 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 _LOCK);. pPag
12068 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 er->state = PAGE
12069 52 5f 53 48 41 52 45 44 3b 0a 20 20 20 20 70 50 R_SHARED;. pP
1206a 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e ager->changeCoun
1206b 74 44 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 65 6c tDone = 0;. }el
1206c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 se if( pPager->s
1206d 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 tate==PAGER_SYNC
1206e 45 44 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 ED ){. pPager
1206f 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f ->state = PAGER_
12070 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20 EXCLUSIVE;. }.
12071 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 pPager->setMast
12072 65 72 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 er = 0;. pPager
12073 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a ->needSync = 0;.
12074 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 pPager->dbModi
12075 66 69 65 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 fied = 0;.. /*
12076 54 4f 44 4f 3a 20 49 73 20 74 68 69 73 20 6f 70 TODO: Is this op
12077 74 69 6d 61 6c 3f 20 57 68 79 20 69 73 20 74 68 timal? Why is th
12078 65 20 64 62 20 73 69 7a 65 20 69 6e 76 61 6c 69 e db size invali
12079 64 61 74 65 64 20 68 65 72 65 20 0a 20 20 2a 2a dated here . **
1207a 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 when the databa
1207b 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 75 se file is not u
1207c 6e 6c 6f 63 6b 65 64 3f 20 2a 2f 0a 20 20 70 50 nlocked? */. pP
1207d 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 ager->dbOrigSize
1207e 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 50 = 0;. sqlite3P
1207f 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 70 50 cacheTruncate(pP
12080 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 ager->pPCache, p
12081 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a Pager->dbSize);.
12082 20 20 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a if( !MEMDB ){.
12083 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 pPager->dbSi
12084 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d zeValid = 0;. }
12085 0a 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d .. return (rc==
12086 53 51 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63 SQLITE_OK?rc2:rc
12087 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 );.}../*.** Para
12088 6d 65 74 65 72 20 61 44 61 74 61 20 6d 75 73 74 meter aData must
12089 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 point to a buff
1208a 65 72 20 6f 66 20 70 50 61 67 65 72 2d 3e 70 61 er of pPager->pa
1208b 67 65 53 69 7a 65 20 62 79 74 65 73 0a 2a 2a 20 geSize bytes.**
1208c 6f 66 20 64 61 74 61 2e 20 43 6f 6d 70 75 74 65 of data. Compute
1208d 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 63 68 and return a ch
1208e 65 63 6b 73 75 6d 20 62 61 73 65 64 20 6f 6e 74 ecksum based ont
1208f 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
12090 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 6f 66 the .** page of
12091 20 64 61 74 61 20 61 6e 64 20 74 68 65 20 63 75 data and the cu
12092 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 rrent value of p
12093 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 Pager->cksumInit
12094 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 ..**.** This is
12095 6e 6f 74 20 61 20 72 65 61 6c 20 63 68 65 63 6b not a real check
12096 73 75 6d 2e 20 49 74 20 69 73 20 72 65 61 6c 6c sum. It is reall
12097 79 20 6a 75 73 74 20 74 68 65 20 73 75 6d 20 6f y just the sum o
12098 66 20 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d f the .** random
12099 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 28 initial value (
1209a 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 pPager->cksumIni
1209b 74 29 20 61 6e 64 20 65 76 65 72 79 20 32 30 30 t) and every 200
1209c 74 68 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 th byte.** of th
1209d 65 20 70 61 67 65 20 64 61 74 61 2c 20 73 74 61 e page data, sta
1209e 72 74 69 6e 67 20 77 69 74 68 20 62 79 74 65 20 rting with byte
1209f 6f 66 66 73 65 74 20 28 70 50 61 67 65 72 2d 3e offset (pPager->
120a0 70 61 67 65 53 69 7a 65 25 32 30 30 29 2e 0a 2a pageSize%200)..*
120a1 2a 20 45 61 63 68 20 62 79 74 65 20 69 73 20 69 * Each byte is i
120a2 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61 6e nterpreted as an
120a3 20 38 2d 62 69 74 20 75 6e 73 69 67 6e 65 64 20 8-bit unsigned
120a4 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 integer..**.** C
120a5 68 61 6e 67 69 6e 67 20 74 68 65 20 66 6f 72 6d hanging the form
120a6 75 6c 61 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 ula used to comp
120a7 75 74 65 20 74 68 69 73 20 63 68 65 63 6b 73 75 ute this checksu
120a8 6d 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a m results in an.
120a9 2a 2a 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 ** incompatible
120aa 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 journal file for
120ab 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6a 6f mat..**.** If jo
120ac 75 72 6e 61 6c 20 63 6f 72 72 75 70 74 69 6f 6e urnal corruption
120ad 20 6f 63 63 75 72 73 20 64 75 65 20 74 6f 20 61 occurs due to a
120ae 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20 power failure,
120af 74 68 65 20 6d 6f 73 74 20 6c 69 6b 65 6c 79 20 the most likely
120b0 0a 2a 2a 20 73 63 65 6e 61 72 69 6f 20 69 73 20 .** scenario is
120b1 74 68 61 74 20 6f 6e 65 20 65 6e 64 20 6f 72 20 that one end or
120b2 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65 the other of the
120b3 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 20 record will be
120b4 63 68 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 74 20 changed. .** It
120b5 69 73 20 6d 75 63 68 20 6c 65 73 73 20 6c 69 6b is much less lik
120b6 65 6c 79 20 74 68 61 74 20 74 68 65 20 74 77 6f ely that the two
120b7 20 65 6e 64 73 20 6f 66 20 74 68 65 20 6a 6f 75 ends of the jou
120b8 72 6e 61 6c 20 72 65 63 6f 72 64 20 77 69 6c 6c rnal record will
120b9 20 62 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 be.** correct a
120ba 6e 64 20 74 68 65 20 6d 69 64 64 6c 65 20 62 65 nd the middle be
120bb 20 63 6f 72 72 75 70 74 2e 20 20 54 68 75 73 2c corrupt. Thus,
120bc 20 74 68 69 73 20 22 63 68 65 63 6b 73 75 6d 22 this "checksum"
120bd 20 73 63 68 65 6d 65 2c 0a 2a 2a 20 74 68 6f 75 scheme,.** thou
120be 67 68 20 66 61 73 74 20 61 6e 64 20 73 69 6d 70 gh fast and simp
120bf 6c 65 2c 20 63 61 74 63 68 65 73 20 74 68 65 20 le, catches the
120c0 6d 6f 73 74 6c 79 20 6c 69 6b 65 6c 79 20 6b 69 mostly likely ki
120c1 6e 64 20 6f 66 20 63 6f 72 72 75 70 74 69 6f 6e nd of corruption
120c2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20 ..*/.static u32
120c3 70 61 67 65 72 5f 63 6b 73 75 6d 28 50 61 67 65 pager_cksum(Page
120c4 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 r *pPager, const
120c5 20 75 38 20 2a 61 44 61 74 61 29 7b 0a 20 20 75 u8 *aData){. u
120c6 33 32 20 63 6b 73 75 6d 20 3d 20 70 50 61 67 65 32 cksum = pPage
120c7 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 r->cksumInit;
120c8 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 /* Checksu
120c9 6d 20 76 61 6c 75 65 20 74 6f 20 72 65 74 75 72 m value to retur
120ca 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 n */. int i = p
120cb 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2d Pager->pageSize-
120cc 32 30 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 200; /*
120cd 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
120ce 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b . while( i>0 ){
120cf 0a 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44 . cksum += aD
120d0 61 74 61 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d ata[i];. i -=
120d1 20 32 30 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 200;. }. retu
120d2 72 6e 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 0a rn cksum;.}../*.
120d3 2a 2a 20 52 65 61 64 20 61 20 73 69 6e 67 6c 65 ** Read a single
120d4 20 70 61 67 65 20 66 72 6f 6d 20 65 69 74 68 65 page from eithe
120d5 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 r the journal fi
120d6 6c 65 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e le (if isMainJrn
120d7 6c 3d 3d 31 29 20 6f 72 0a 2a 2a 20 66 72 6f 6d l==1) or.** from
120d8 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
120d9 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d (if isMainJrnl=
120da 3d 30 29 20 61 6e 64 20 70 6c 61 79 62 61 63 6b =0) and playback
120db 20 74 68 61 74 20 70 61 67 65 2e 0a 2a 2a 20 54 that page..** T
120dc 68 65 20 70 61 67 65 20 62 65 67 69 6e 73 20 61 he page begins a
120dd 74 20 6f 66 66 73 65 74 20 2a 70 4f 66 66 73 65 t offset *pOffse
120de 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e t into the file.
120df 20 54 68 65 20 2a 70 4f 66 66 73 65 74 0a 2a 2a The *pOffset.**
120e0 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 value is increa
120e1 73 65 64 20 74 6f 20 74 68 65 20 73 74 61 72 74 sed to the start
120e2 20 6f 66 20 74 68 65 20 6e 65 78 74 20 70 61 67 of the next pag
120e3 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c e in the journal
120e4 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 4d 61 ..**.** The isMa
120e5 69 6e 4a 72 6e 6c 20 66 6c 61 67 20 69 73 20 74 inJrnl flag is t
120e6 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 74 rue if this is t
120e7 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b he main rollback
120e8 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20 journal and.**
120e9 66 61 6c 73 65 20 66 6f 72 20 74 68 65 20 73 74 false for the st
120ea 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e atement journal.
120eb 20 20 54 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 The main rollb
120ec 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 75 73 65 73 ack journal uses
120ed 0a 2a 2a 20 63 68 65 63 6b 73 75 6d 73 20 2d 20 .** checksums -
120ee 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f the statement jo
120ef 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 2e 0a urnal does not..
120f0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 **.** If the pag
120f1 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
120f2 70 61 67 65 20 72 65 63 6f 72 64 20 72 65 61 64 page record read
120f3 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 from the (sub-)
120f4 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 journal file.**
120f5 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
120f6 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 the current valu
120f7 65 20 6f 66 20 50 61 67 65 72 2e 64 62 53 69 7a e of Pager.dbSiz
120f8 65 2c 20 74 68 65 6e 20 70 6c 61 79 62 61 63 6b e, then playback
120f9 20 69 73 0a 2a 2a 20 73 6b 69 70 70 65 64 20 61 is.** skipped a
120fa 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 nd SQLITE_OK is
120fb 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
120fc 49 66 20 70 44 6f 6e 65 20 69 73 20 6e 6f 74 20 If pDone is not
120fd 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 NULL, then it is
120fe 20 61 20 72 65 63 6f 72 64 20 6f 66 20 70 61 67 a record of pag
120ff 65 73 20 74 68 61 74 20 68 61 76 65 20 61 6c 72 es that have alr
12100 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20 70 6c 61 eady.** been pla
12101 79 65 64 20 62 61 63 6b 2e 20 20 49 66 20 74 68 yed back. If th
12102 65 20 70 61 67 65 20 61 74 20 2a 70 4f 66 66 73 e page at *pOffs
12103 65 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 et has already b
12104 65 65 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 0a een played back.
12105 2a 2a 20 28 69 66 20 74 68 65 20 63 6f 72 72 65 ** (if the corre
12106 73 70 6f 6e 64 69 6e 67 20 70 44 6f 6e 65 20 62 sponding pDone b
12107 69 74 20 69 73 20 73 65 74 29 20 74 68 65 6e 20 it is set) then
12108 73 6b 69 70 20 74 68 65 20 70 6c 61 79 62 61 63 skip the playbac
12109 6b 2e 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 k..** Make sure
1210a 74 68 65 20 70 44 6f 6e 65 20 62 69 74 20 63 6f the pDone bit co
1210b 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 rresponding to t
1210c 68 65 20 2a 70 4f 66 66 73 65 74 20 70 61 67 65 he *pOffset page
1210d 20 69 73 20 73 65 74 0a 2a 2a 20 70 72 69 6f 72 is set.** prior
1210e 20 74 6f 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a to returning..*
1210f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 *.** If the page
12110 20 72 65 63 6f 72 64 20 69 73 20 73 75 63 63 65 record is succe
12111 73 73 66 75 6c 6c 79 20 72 65 61 64 20 66 72 6f ssfully read fro
12112 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 m the (sub-)jour
12113 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 nal file.** and
12114 70 6c 61 79 65 64 20 62 61 63 6b 2c 20 74 68 65 played back, the
12115 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 n SQLITE_OK is r
12116 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 eturned. If an I
12117 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a O error occurs.*
12118 2a 20 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20 * while reading
12119 74 68 65 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 the record from
1211a 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 the (sub-)journa
1211b 6c 20 66 69 6c 65 20 6f 72 20 77 68 69 6c 65 20 l file or while
1211c 77 72 69 74 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 writing.** to th
1211d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
1211e 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 then the IO err
1211f 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
12120 6e 65 64 2e 20 49 66 20 64 61 74 61 0a 2a 2a 20 ned. If data.**
12121 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 is successfully
12122 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73 read from the (s
12123 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ub-)journal file
12124 20 62 75 74 20 61 70 70 65 61 72 73 20 74 6f 20 but appears to
12125 62 65 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64 2c be.** corrupted,
12126 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 SQLITE_DONE is
12127 72 65 74 75 72 6e 65 64 2e 20 44 61 74 61 20 69 returned. Data i
12128 73 20 63 6f 6e 73 69 64 65 72 65 64 20 63 6f 72 s considered cor
12129 72 75 70 74 65 64 20 69 6e 0a 2a 2a 20 74 77 6f rupted in.** two
1212a 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a circumstances:.
1212b 2a 2a 20 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 ** .** * If th
1212c 65 20 72 65 63 6f 72 64 20 70 61 67 65 2d 6e 75 e record page-nu
1212d 6d 62 65 72 20 69 73 20 69 6c 6c 65 67 61 6c 20 mber is illegal
1212e 28 30 20 6f 72 20 50 41 47 45 52 5f 4d 4a 5f 50 (0 or PAGER_MJ_P
1212f 47 4e 4f 29 2c 20 6f 72 0a 2a 2a 20 20 20 2a 20 GNO), or.** *
12130 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 If the record is
12131 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 being rolled ba
12132 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e ck from the main
12133 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a journal file.**
12134 20 20 20 20 20 61 6e 64 20 74 68 65 20 63 68 65 and the che
12135 63 6b 73 75 6d 20 66 69 65 6c 64 20 64 6f 65 73 cksum field does
12136 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 72 not match the r
12137 65 63 6f 72 64 20 63 6f 6e 74 65 6e 74 2e 0a 2a ecord content..*
12138 2a 0a 2a 2a 20 4e 65 69 74 68 65 72 20 6f 66 20 *.** Neither of
12139 74 68 65 73 65 20 74 77 6f 20 73 63 65 6e 61 72 these two scenar
1213a 69 6f 73 20 61 72 65 20 70 6f 73 73 69 62 6c 65 ios are possible
1213b 20 64 75 72 69 6e 67 20 61 20 73 61 76 65 70 6f during a savepo
1213c 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a int rollback..**
1213d 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 .** If this is a
1213e 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 savepoint rollb
1213f 61 63 6b 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 ack, then memory
12140 20 6d 61 79 20 68 61 76 65 20 74 6f 20 62 65 20 may have to be
12141 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 dynamically.** a
12142 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 llocated by this
12143 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 74 68 function. If th
12144 69 73 20 69 73 20 74 68 65 20 63 61 73 65 20 61 is is the case a
12145 6e 64 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e nd an allocation
12146 20 66 61 69 6c 73 2c 0a 2a 2a 20 53 51 4c 49 54 fails,.** SQLIT
12147 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 E_NOMEM is retur
12148 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ned..*/.static i
12149 6e 74 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 nt pager_playbac
1214a 6b 5f 6f 6e 65 5f 70 61 67 65 28 0a 20 20 50 61 k_one_page(. Pa
1214b 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 ger *pPager,
1214c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1214d 68 65 20 70 61 67 65 72 20 62 65 69 6e 67 20 70 he pager being p
1214e 6c 61 79 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 layed back */.
1214f 69 6e 74 20 69 73 4d 61 69 6e 4a 72 6e 6c 2c 20 int isMainJrnl,
12150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
12151 20 31 20 2d 3e 20 6d 61 69 6e 20 6a 6f 75 72 6e 1 -> main journ
12152 61 6c 2e 20 30 20 2d 3e 20 73 75 62 2d 6a 6f 75 al. 0 -> sub-jou
12153 72 6e 61 6c 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 rnal. */. int i
12154 73 55 6e 73 79 6e 63 2c 20 20 20 20 20 20 20 20 sUnsync,
12155 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
12156 20 69 66 20 72 65 61 64 69 6e 67 20 66 72 6f 6d if reading from
12157 20 75 6e 73 79 6e 63 65 64 20 6d 61 69 6e 20 6a unsynced main j
12158 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 ournal */. i64
12159 2a 70 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 *pOffset,
1215a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 /* Off
1215b 73 65 74 20 6f 66 20 72 65 63 6f 72 64 20 74 6f set of record to
1215c 20 70 6c 61 79 62 61 63 6b 20 2a 2f 0a 20 20 69 playback */. i
1215d 6e 74 20 69 73 53 61 76 65 70 6e 74 2c 20 20 20 nt isSavepnt,
1215e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1215f 54 72 75 65 20 66 6f 72 20 61 20 73 61 76 65 70 True for a savep
12160 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f oint rollback */
12161 0a 20 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 . Bitvec *pDone
12162 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12163 20 2f 2a 20 42 69 74 76 65 63 20 6f 66 20 70 61 /* Bitvec of pa
12164 67 65 73 20 61 6c 72 65 61 64 79 20 70 6c 61 79 ges already play
12165 65 64 20 62 61 63 6b 20 2a 2f 0a 29 7b 0a 20 20 ed back */.){.
12166 69 6e 74 20 72 63 3b 0a 20 20 50 67 48 64 72 20 int rc;. PgHdr
12167 2a 70 50 67 3b 20 20 20 20 20 20 20 20 20 20 20 *pPg;
12168 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 78 /* An ex
12169 69 73 74 69 6e 67 20 70 61 67 65 20 69 6e 20 74 isting page in t
1216a 68 65 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 he cache */. Pg
1216b 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 no pgno;
1216c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1216d 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f he page number o
1216e 66 20 61 20 70 61 67 65 20 69 6e 20 6a 6f 75 72 f a page in jour
1216f 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 nal */. u32 cks
12170 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 um;
12171 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 /* Checks
12172 75 6d 20 75 73 65 64 20 66 6f 72 20 73 61 6e 69 um used for sani
12173 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 ty checking */.
12174 20 75 38 20 2a 61 44 61 74 61 3b 20 20 20 20 20 u8 *aData;
12175 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
12176 2a 20 54 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 * Temporary stor
12177 61 67 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 age for the page
12178 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 */. sqlite3_fi
12179 6c 65 20 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 le *jfd;
1217a 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 /* The file
1217b 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74 descriptor for t
1217c 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1217d 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 28 69 */.. assert( (i
1217e 73 4d 61 69 6e 4a 72 6e 6c 26 7e 31 29 3d 3d 30 sMainJrnl&~1)==0
1217f 20 29 3b 20 20 20 20 20 20 2f 2a 20 69 73 4d 61 ); /* isMa
12180 69 6e 4a 72 6e 6c 20 69 73 20 30 20 6f 72 20 31 inJrnl is 0 or 1
12181 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 69 */. assert( (i
12182 73 53 61 76 65 70 6e 74 26 7e 31 29 3d 3d 30 20 sSavepnt&~1)==0
12183 29 3b 20 20 20 20 20 20 20 2f 2a 20 69 73 53 61 ); /* isSa
12184 76 65 70 6e 74 20 69 73 20 30 20 6f 72 20 31 20 vepnt is 0 or 1
12185 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 4d */. assert( isM
12186 61 69 6e 4a 72 6e 6c 20 7c 7c 20 70 44 6f 6e 65 ainJrnl || pDone
12187 20 29 3b 20 20 20 20 20 2f 2a 20 70 44 6f 6e 65 ); /* pDone
12188 20 61 6c 77 61 79 73 20 75 73 65 64 20 6f 6e 20 always used on
12189 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a sub-journals */.
1218a 20 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65 assert( isSave
1218b 70 6e 74 20 7c 7c 20 70 44 6f 6e 65 3d 3d 30 20 pnt || pDone==0
1218c 29 3b 20 20 20 2f 2a 20 70 44 6f 6e 65 20 6e 65 ); /* pDone ne
1218d 76 65 72 20 75 73 65 64 20 6f 6e 20 6e 6f 6e 2d ver used on non-
1218e 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 savepoint */..
1218f 61 44 61 74 61 20 3d 20 28 75 38 2a 29 70 50 61 aData = (u8*)pPa
12190 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a ger->pTmpSpace;.
12191 20 20 61 73 73 65 72 74 28 20 61 44 61 74 61 20 assert( aData
12192 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 ); /* Te
12193 6d 70 20 73 74 6f 72 61 67 65 20 6d 75 73 74 20 mp storage must
12194 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 have already bee
12195 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a n allocated */..
12196 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 /* Read the pa
12197 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 ge number and pa
12198 67 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 ge data from the
12199 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 73 75 62 2d journal or sub-
1219a 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c journal. ** fil
1219b 65 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 e. Return an err
1219c 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 or code to the c
1219d 61 6c 6c 65 72 20 69 66 20 61 6e 20 49 4f 20 65 aller if an IO e
1219e 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 20 20 2a rror occurs.. *
1219f 2f 0a 20 20 6a 66 64 20 3d 20 69 73 4d 61 69 6e /. jfd = isMain
121a0 4a 72 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e 6a Jrnl ? pPager->j
121a1 66 64 20 3a 20 70 50 61 67 65 72 2d 3e 73 6a 66 fd : pPager->sjf
121a2 64 3b 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 d;. rc = read32
121a3 62 69 74 73 28 6a 66 64 2c 20 2a 70 4f 66 66 73 bits(jfd, *pOffs
121a4 65 74 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 69 66 et, &pgno);. if
121a5 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
121a6 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 ) return rc;. r
121a7 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 c = sqlite3OsRea
121a8 64 28 6a 66 64 2c 20 61 44 61 74 61 2c 20 70 50 d(jfd, aData, pP
121a9 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 ager->pageSize,
121aa 28 2a 70 4f 66 66 73 65 74 29 2b 34 29 3b 0a 20 (*pOffset)+4);.
121ab 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
121ac 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a OK ) return rc;.
121ad 20 20 2a 70 4f 66 66 73 65 74 20 2b 3d 20 70 50 *pOffset += pP
121ae 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 2b ager->pageSize +
121af 20 34 20 2b 20 69 73 4d 61 69 6e 4a 72 6e 6c 2a 4 + isMainJrnl*
121b0 34 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 4;.. /* Sanity
121b1 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 checking on the
121b2 70 61 67 65 2e 20 20 54 68 69 73 20 69 73 20 6d page. This is m
121b3 6f 72 65 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 ore important th
121b4 61 74 20 49 20 6f 72 69 67 69 6e 61 6c 6c 79 0a at I originally.
121b5 20 20 2a 2a 20 74 68 6f 75 67 68 74 2e 20 20 49 ** thought. I
121b6 66 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 f a power failur
121b7 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 e occurs while t
121b8 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 he journal is be
121b9 69 6e 67 20 77 72 69 74 74 65 6e 2c 0a 20 20 2a ing written,. *
121ba 2a 20 69 74 20 63 6f 75 6c 64 20 63 61 75 73 65 * it could cause
121bb 20 69 6e 76 61 6c 69 64 20 64 61 74 61 20 74 6f invalid data to
121bc 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f be written into
121bd 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 57 the journal. W
121be 65 20 6e 65 65 64 20 74 6f 0a 20 20 2a 2a 20 64 e need to. ** d
121bf 65 74 65 63 74 20 74 68 69 73 20 69 6e 76 61 6c etect this inval
121c0 69 64 20 64 61 74 61 20 28 77 69 74 68 20 68 69 id data (with hi
121c1 67 68 20 70 72 6f 62 61 62 69 6c 69 74 79 29 20 gh probability)
121c2 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 20 and ignore it..
121c3 20 2a 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d */. if( pgno==
121c4 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 0 || pgno==PAGER
121c5 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 _MJ_PGNO(pPager)
121c6 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
121c7 21 69 73 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 !isSavepnt );.
121c8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
121c9 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 DONE;. }. if(
121ca 70 67 6e 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 pgno>(Pgno)pPage
121cb 72 2d 3e 64 62 53 69 7a 65 20 7c 7c 20 73 71 6c r->dbSize || sql
121cc 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 ite3BitvecTest(p
121cd 44 6f 6e 65 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 Done, pgno) ){.
121ce 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
121cf 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 _OK;. }. if( i
121d0 73 4d 61 69 6e 4a 72 6e 6c 20 29 7b 0a 20 20 20 sMainJrnl ){.
121d1 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 rc = read32bits
121d2 28 6a 66 64 2c 20 28 2a 70 4f 66 66 73 65 74 29 (jfd, (*pOffset)
121d3 2d 34 2c 20 26 63 6b 73 75 6d 29 3b 0a 20 20 20 -4, &cksum);.
121d4 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
121d5 20 72 63 3b 0a 20 20 20 20 69 66 28 20 21 69 73 rc;. if( !is
121d6 53 61 76 65 70 6e 74 20 26 26 20 70 61 67 65 72 Savepnt && pager
121d7 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 61 _cksum(pPager, a
121d8 44 61 74 61 29 21 3d 63 6b 73 75 6d 20 29 7b 0a Data)!=cksum ){.
121d9 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
121da 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a ITE_DONE;. }.
121db 20 20 7d 0a 0a 20 20 69 66 28 20 70 44 6f 6e 65 }.. if( pDone
121dc 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 && (rc = sqlite
121dd 33 42 69 74 76 65 63 53 65 74 28 70 44 6f 6e 65 3BitvecSet(pDone
121de 2c 20 70 67 6e 6f 29 29 21 3d 53 51 4c 49 54 45 , pgno))!=SQLITE
121df 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 _OK ){. retur
121e0 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 n rc;. }.. ass
121e1 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
121e2 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56 te==PAGER_RESERV
121e3 45 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 ED || pPager->st
121e4 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 ate>=PAGER_EXCLU
121e5 53 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 SIVE );.. /* If
121e6 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e the pager is in
121e7 20 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2c RESERVED state,
121e8 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75 73 74 then there must
121e9 20 62 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 be a copy of th
121ea 69 73 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 is. ** page in
121eb 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e the pager cache.
121ec 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75 In this case ju
121ed 73 74 20 75 70 64 61 74 65 20 74 68 65 20 70 61 st update the pa
121ee 67 65 72 20 63 61 63 68 65 2c 0a 20 20 2a 2a 20 ger cache,. **
121ef 6e 6f 74 20 74 68 65 20 64 61 74 61 62 61 73 65 not the database
121f0 20 66 69 6c 65 2e 20 54 68 65 20 70 61 67 65 20 file. The page
121f1 69 73 20 6c 65 66 74 20 6d 61 72 6b 65 64 20 64 is left marked d
121f2 69 72 74 79 20 69 6e 20 74 68 69 73 20 63 61 73 irty in this cas
121f3 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 e.. **. ** An
121f4 65 78 63 65 70 74 69 6f 6e 20 74 6f 20 74 68 65 exception to the
121f5 20 61 62 6f 76 65 20 72 75 6c 65 3a 20 49 66 20 above rule: If
121f6 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 the database is
121f7 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 0a in no-sync mode.
121f8 20 20 2a 2a 20 61 6e 64 20 61 20 70 61 67 65 20 ** and a page
121f9 69 73 20 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 is moved during
121fa 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 an incremental v
121fb 61 63 75 75 6d 20 74 68 65 6e 20 74 68 65 20 70 acuum then the p
121fc 61 67 65 20 6d 61 79 0a 20 20 2a 2a 20 6e 6f 74 age may. ** not
121fd 20 62 65 20 69 6e 20 74 68 65 20 70 61 67 65 72 be in the pager
121fe 20 63 61 63 68 65 2e 20 4c 61 74 65 72 3a 20 69 cache. Later: i
121ff 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 f a malloc() or
12200 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a IO error occurs.
12201 20 20 2a 2a 20 64 75 72 69 6e 67 20 61 20 4d 6f ** during a Mo
12202 76 65 70 61 67 65 28 29 20 63 61 6c 6c 2c 20 74 vepage() call, t
12203 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d 61 79 hen the page may
12204 20 6e 6f 74 20 62 65 20 69 6e 20 74 68 65 20 63 not be in the c
12205 61 63 68 65 0a 20 20 2a 2a 20 65 69 74 68 65 72 ache. ** either
12206 2e 20 53 6f 20 74 68 65 20 63 6f 6e 64 69 74 69 . So the conditi
12207 6f 6e 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 on described in
12208 74 68 65 20 61 62 6f 76 65 20 70 61 72 61 67 72 the above paragr
12209 61 70 68 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 aph is not. **
1220a 61 73 73 65 72 74 28 29 61 62 6c 65 2e 0a 20 20 assert()able..
1220b 2a 2a 0a 20 20 2a 2a 20 49 66 20 69 6e 20 45 58 **. ** If in EX
1220c 43 4c 55 53 49 56 45 20 73 74 61 74 65 2c 20 74 CLUSIVE state, t
1220d 68 65 6e 20 77 65 20 75 70 64 61 74 65 20 74 68 hen we update th
1220e 65 20 70 61 67 65 72 20 63 61 63 68 65 20 69 66 e pager cache if
1220f 20 69 74 20 65 78 69 73 74 73 0a 20 20 2a 2a 20 it exists. **
12210 61 6e 64 20 74 68 65 20 6d 61 69 6e 20 66 69 6c and the main fil
12211 65 2e 20 54 68 65 20 70 61 67 65 20 69 73 20 74 e. The page is t
12212 68 65 6e 20 6d 61 72 6b 65 64 20 6e 6f 74 20 64 hen marked not d
12213 69 72 74 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 irty.. **. **
12214 54 69 63 6b 65 74 20 23 31 31 37 31 3a 20 20 54 Ticket #1171: T
12215 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 he statement jou
12216 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 rnal might conta
12217 69 6e 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 in page content
12218 74 68 61 74 20 69 73 0a 20 20 2a 2a 20 64 69 66 that is. ** dif
12219 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 ferent from the
1221a 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 page content at
1221b 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 the start of the
1221c 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 transaction..
1221d 2a 2a 20 54 68 69 73 20 6f 63 63 75 72 73 20 77 ** This occurs w
1221e 68 65 6e 20 61 20 70 61 67 65 20 69 73 20 63 68 hen a page is ch
1221f 61 6e 67 65 64 20 70 72 69 6f 72 20 74 6f 20 74 anged prior to t
12220 68 65 20 73 74 61 72 74 20 6f 66 20 61 20 73 74 he start of a st
12221 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 68 65 atement. ** the
12222 6e 20 63 68 61 6e 67 65 64 20 61 67 61 69 6e 20 n changed again
12223 77 69 74 68 69 6e 20 74 68 65 20 73 74 61 74 65 within the state
12224 6d 65 6e 74 2e 20 20 57 68 65 6e 20 72 6f 6c 6c ment. When roll
12225 69 6e 67 20 62 61 63 6b 20 73 75 63 68 20 61 0a ing back such a.
12226 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 ** statement w
12227 65 20 6d 75 73 74 20 6e 6f 74 20 77 72 69 74 65 e must not write
12228 20 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c to the original
12229 20 64 61 74 61 62 61 73 65 20 75 6e 6c 65 73 73 database unless
1222a 20 77 65 20 6b 6e 6f 77 0a 20 20 2a 2a 20 66 6f we know. ** fo
1222b 72 20 63 65 72 74 61 69 6e 20 74 68 61 74 20 6f r certain that o
1222c 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e riginal page con
1222d 74 65 6e 74 73 20 61 72 65 20 73 79 6e 63 65 64 tents are synced
1222e 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 72 into the main r
1222f 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 ollback. ** jou
12230 72 6e 61 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 rnal. Otherwise
12231 2c 20 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6d , a power loss m
12232 69 67 68 74 20 6c 65 61 76 65 20 6d 6f 64 69 66 ight leave modif
12233 69 65 64 20 64 61 74 61 20 69 6e 20 74 68 65 0a ied data in the.
12234 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 ** database fi
12235 6c 65 20 77 69 74 68 6f 75 74 20 61 6e 20 65 6e le without an en
12236 74 72 79 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 try in the rollb
12237 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74 ack journal that
12238 20 63 61 6e 0a 20 20 2a 2a 20 72 65 73 74 6f 72 can. ** restor
12239 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 e the database t
1223a 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 66 o its original f
1223b 6f 72 6d 2e 20 20 54 77 6f 20 63 6f 6e 64 69 74 orm. Two condit
1223c 69 6f 6e 73 20 6d 75 73 74 20 62 65 0a 20 20 2a ions must be. *
1223d 2a 20 6d 65 74 20 62 65 66 6f 72 65 20 77 72 69 * met before wri
1223e 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 ting to the data
1223f 62 61 73 65 20 66 69 6c 65 73 2e 20 28 31 29 20 base files. (1)
12240 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 the database mus
12241 74 20 62 65 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 t be. ** locked
12242 2e 20 20 28 32 29 20 77 65 20 6b 6e 6f 77 20 74 . (2) we know t
12243 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c hat the original
12244 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 73 page content is
12245 20 66 75 6c 6c 79 20 73 79 6e 63 65 64 0a 20 20 fully synced.
12246 2a 2a 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a ** in the main j
12247 6f 75 72 6e 61 6c 20 65 69 74 68 65 72 20 62 65 ournal either be
12248 63 61 75 73 65 20 74 68 65 20 70 61 67 65 20 69 cause the page i
12249 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 6f s not in cache o
1224a 72 20 65 6c 73 65 0a 20 20 2a 2a 20 74 68 65 20 r else. ** the
1224b 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 page is marked a
1224c 73 20 6e 65 65 64 53 79 6e 63 3d 3d 30 2e 0a 20 s needSync==0..
1224d 20 2a 2a 0a 20 20 2a 2a 20 32 30 30 38 2d 30 34 **. ** 2008-04
1224e 2d 31 34 3a 20 20 57 68 65 6e 20 61 74 74 65 6d -14: When attem
1224f 70 74 69 6e 67 20 74 6f 20 76 61 63 75 75 6d 20 pting to vacuum
12250 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 a corrupt databa
12251 73 65 20 66 69 6c 65 2c 20 69 74 0a 20 20 2a 2a se file, it. **
12252 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 is possible to
12253 66 61 69 6c 20 61 20 73 74 61 74 65 6d 65 6e 74 fail a statement
12254 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 on a database t
12255 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 79 65 74 hat does not yet
12256 20 65 78 69 73 74 2e 0a 20 20 2a 2a 20 44 6f 20 exist.. ** Do
12257 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 77 not attempt to w
12258 72 69 74 65 20 69 66 20 64 61 74 61 62 61 73 65 rite if database
12259 20 66 69 6c 65 20 68 61 73 20 6e 65 76 65 72 20 file has never
1225a 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 20 20 2a been opened.. *
1225b 2f 0a 20 20 70 50 67 20 3d 20 70 61 67 65 72 5f /. pPg = pager_
1225c 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 lookup(pPager, p
1225d 67 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 gno);. assert(
1225e 70 50 67 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b pPg || !MEMDB );
1225f 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 . PAGERTRACE(("
12260 50 4c 41 59 42 41 43 4b 20 25 64 20 70 61 67 65 PLAYBACK %d page
12261 20 25 64 20 68 61 73 68 28 25 30 38 78 29 20 25 %d hash(%08x) %
12262 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 s\n",.
12263 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 PAGERID(pPa
12264 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 ger), pgno, page
12265 72 5f 64 61 74 61 68 61 73 68 28 70 50 61 67 65 r_datahash(pPage
12266 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 61 44 61 r->pageSize, aDa
12267 74 61 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ta),.
12268 20 20 20 20 28 69 73 4d 61 69 6e 4a 72 6e 6c 3f (isMainJrnl?
12269 22 6d 61 69 6e 2d 6a 6f 75 72 6e 61 6c 22 3a 22 "main-journal":"
1226a 73 75 62 2d 6a 6f 75 72 6e 61 6c 22 29 0a 20 20 sub-journal").
1226b 29 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65 ));. if( (pPage
1226c 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f r->state>=PAGER_
1226d 45 58 43 4c 55 53 49 56 45 29 0a 20 20 20 26 26 EXCLUSIVE). &&
1226e 20 28 70 50 67 3d 3d 30 20 7c 7c 20 30 3d 3d 28 (pPg==0 || 0==(
1226f 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 pPg->flags&PGHDR
12270 5f 4e 45 45 44 5f 53 59 4e 43 29 29 0a 20 20 20 _NEED_SYNC)).
12271 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 && isOpen(pPager
12272 2d 3e 66 64 29 0a 20 20 20 26 26 20 21 69 73 55 ->fd). && !isU
12273 6e 73 79 6e 63 0a 20 20 29 7b 0a 20 20 20 20 69 nsync. ){. i
12274 36 34 20 6f 66 73 74 20 3d 20 28 70 67 6e 6f 2d 64 ofst = (pgno-
12275 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 1)*(i64)pPager->
12276 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 pageSize;. rc
12277 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
12278 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 61 44 e(pPager->fd, aD
12279 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 ata, pPager->pag
1227a 65 53 69 7a 65 2c 20 6f 66 73 74 29 3b 0a 20 20 eSize, ofst);.
1227b 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 if( pgno>pPage
1227c 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 7b r->dbFileSize ){
1227d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 . pPager->d
1227e 62 46 69 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f bFileSize = pgno
1227f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
12280 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 pPager->pBackup
12281 29 7b 0a 20 20 20 20 20 20 43 4f 44 45 43 31 28 ){. CODEC1(
12282 70 50 61 67 65 72 2c 20 61 44 61 74 61 2c 20 70 pPager, aData, p
12283 67 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 4c 49 54 gno, 3, rc=SQLIT
12284 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 20 20 E_NOMEM);.
12285 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 sqlite3BackupUpd
12286 61 74 65 28 70 50 61 67 65 72 2d 3e 70 42 61 63 ate(pPager->pBac
12287 6b 75 70 2c 20 70 67 6e 6f 2c 20 61 44 61 74 61 kup, pgno, aData
12288 29 3b 0a 20 20 20 20 20 20 43 4f 44 45 43 31 28 );. CODEC1(
12289 70 50 61 67 65 72 2c 20 61 44 61 74 61 2c 20 70 pPager, aData, p
1228a 67 6e 6f 2c 20 30 2c 20 72 63 3d 53 51 4c 49 54 gno, 0, rc=SQLIT
1228b 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 7d 0a E_NOMEM);. }.
1228c 20 20 7d 65 6c 73 65 20 69 66 28 20 21 69 73 4d }else if( !isM
1228d 61 69 6e 4a 72 6e 6c 20 26 26 20 70 50 67 3d 3d ainJrnl && pPg==
1228e 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 0 ){. /* If t
1228f 68 69 73 20 69 73 20 61 20 72 6f 6c 6c 62 61 63 his is a rollbac
12290 6b 20 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 k of a savepoint
12291 20 61 6e 64 20 64 61 74 61 20 77 61 73 20 6e 6f and data was no
12292 74 20 77 72 69 74 74 65 6e 20 74 6f 0a 20 20 20 t written to.
12293 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 ** the database
12294 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 and the page is
12295 20 6e 6f 74 20 69 6e 2d 6d 65 6d 6f 72 79 2c 20 not in-memory,
12296 74 68 65 72 65 20 69 73 20 61 20 70 6f 74 65 6e there is a poten
12297 74 69 61 6c 0a 20 20 20 20 2a 2a 20 70 72 6f 62 tial. ** prob
12298 6c 65 6d 2e 20 57 68 65 6e 20 74 68 65 20 70 61 lem. When the pa
12299 67 65 20 69 73 20 6e 65 78 74 20 66 65 74 63 68 ge is next fetch
1229a 65 64 20 62 79 20 74 68 65 20 62 2d 74 72 65 65 ed by the b-tree
1229b 20 6c 61 79 65 72 2c 20 69 74 20 0a 20 20 20 20 layer, it .
1229c 2a 2a 20 77 69 6c 6c 20 62 65 20 72 65 61 64 20 ** will be read
1229d 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
1229e 65 20 66 69 6c 65 2c 20 77 68 69 63 68 20 6d 61 e file, which ma
1229f 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 y or may not be
122a0 0a 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 2e . ** current.
122a1 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 . **. **
122a2 54 68 65 72 65 20 61 72 65 20 61 20 63 6f 75 70 There are a coup
122a3 6c 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 le of different
122a4 77 61 79 73 20 74 68 69 73 20 63 61 6e 20 68 61 ways this can ha
122a5 70 70 65 6e 2e 20 41 6c 6c 20 61 72 65 20 71 75 ppen. All are qu
122a6 69 74 65 0a 20 20 20 20 2a 2a 20 6f 62 73 63 75 ite. ** obscu
122a7 72 65 2e 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67 re. When running
122a8 20 69 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 in synchronous
122a9 6d 6f 64 65 2c 20 74 68 69 73 20 63 61 6e 20 6f mode, this can o
122aa 6e 6c 79 20 68 61 70 70 65 6e 20 0a 20 20 20 20 nly happen .
122ab 2a 2a 20 69 66 20 74 68 65 20 70 61 67 65 20 69 ** if the page i
122ac 73 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 s on the free-li
122ad 73 74 20 61 74 20 74 68 65 20 73 74 61 72 74 20 st at the start
122ae 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 of the transacti
122af 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 on, then. **
122b0 70 6f 70 75 6c 61 74 65 64 2c 20 74 68 65 6e 20 populated, then
122b1 6d 6f 76 65 64 20 75 73 69 6e 67 20 73 71 6c 69 moved using sqli
122b2 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 te3PagerMovepage
122b3 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a ().. **. *
122b4 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 * The solution i
122b5 73 20 74 6f 20 61 64 64 20 61 6e 20 69 6e 2d 6d s to add an in-m
122b6 65 6d 6f 72 79 20 70 61 67 65 20 74 6f 20 74 68 emory page to th
122b7 65 20 63 61 63 68 65 20 63 6f 6e 74 61 69 6e 69 e cache containi
122b8 6e 67 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 ng. ** the da
122b9 74 61 20 6a 75 73 74 20 72 65 61 64 20 66 72 6f ta just read fro
122ba 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 m the sub-journa
122bb 6c 2e 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 l. Mark the page
122bc 20 61 73 20 64 69 72 74 79 20 0a 20 20 20 20 2a as dirty . *
122bd 2a 20 61 6e 64 20 69 66 20 74 68 65 20 70 61 67 * and if the pag
122be 65 72 20 72 65 71 75 69 72 65 73 20 61 20 6a 6f er requires a jo
122bf 75 72 6e 61 6c 2d 73 79 6e 63 2c 20 74 68 65 6e urnal-sync, then
122c0 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 mark the page a
122c1 73 20 0a 20 20 20 20 2a 2a 20 72 65 71 75 69 72 s . ** requir
122c2 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 ing a journal-sy
122c3 6e 63 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 nc before it is
122c4 77 72 69 74 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a written.. */.
122c5 20 20 20 20 61 73 73 65 72 74 28 20 69 73 53 61 assert( isSa
122c6 76 65 70 6e 74 20 29 3b 0a 20 20 20 20 69 66 28 vepnt );. if(
122c7 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 (rc = sqlite3Pa
122c8 67 65 72 41 63 71 75 69 72 65 28 70 50 61 67 65 gerAcquire(pPage
122c9 72 2c 20 70 67 6e 6f 2c 20 26 70 50 67 2c 20 31 r, pgno, &pPg, 1
122ca 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b ))!=SQLITE_OK ){
122cb 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
122cc 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 67 2d ;. }. pPg-
122cd 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 >flags &= ~PGHDR
122ce 5f 4e 45 45 44 5f 52 45 41 44 3b 0a 20 20 20 20 _NEED_READ;.
122cf 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b sqlite3PcacheMak
122d0 65 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20 7d eDirty(pPg);. }
122d1 0a 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 . if( pPg ){.
122d2 20 20 2f 2a 20 4e 6f 20 70 61 67 65 20 73 68 6f /* No page sho
122d3 75 6c 64 20 65 76 65 72 20 62 65 20 65 78 70 6c uld ever be expl
122d4 69 63 69 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 icitly rolled ba
122d5 63 6b 20 74 68 61 74 20 69 73 20 69 6e 20 75 73 ck that is in us
122d6 65 2c 20 65 78 63 65 70 74 0a 20 20 20 20 2a 2a e, except. **
122d7 20 66 6f 72 20 70 61 67 65 20 31 20 77 68 69 63 for page 1 whic
122d8 68 20 69 73 20 68 65 6c 64 20 69 6e 20 75 73 65 h is held in use
122d9 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 in order to kee
122da 70 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 p the lock on th
122db 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 e. ** databas
122dc 65 20 61 63 74 69 76 65 2e 20 48 6f 77 65 76 65 e active. Howeve
122dd 72 20 73 75 63 68 20 61 20 70 61 67 65 20 6d 61 r such a page ma
122de 79 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b y be rolled back
122df 20 61 73 20 61 20 72 65 73 75 6c 74 0a 20 20 20 as a result.
122e0 20 2a 2a 20 6f 66 20 61 6e 20 69 6e 74 65 72 6e ** of an intern
122e1 61 6c 20 65 72 72 6f 72 20 72 65 73 75 6c 74 69 al error resulti
122e2 6e 67 20 69 6e 20 61 6e 20 61 75 74 6f 6d 61 74 ng in an automat
122e3 69 63 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a ic call to. *
122e4 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f * sqlite3PagerRo
122e5 6c 6c 62 61 63 6b 28 29 2e 0a 20 20 20 20 2a 2f llback().. */
122e6 0a 20 20 20 20 76 6f 69 64 20 2a 70 44 61 74 61 . void *pData
122e7 3b 0a 20 20 20 20 70 44 61 74 61 20 3d 20 70 50 ;. pData = pP
122e8 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 6d 65 g->pData;. me
122e9 6d 63 70 79 28 70 44 61 74 61 2c 20 61 44 61 74 mcpy(pData, aDat
122ea 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 a, pPager->pageS
122eb 69 7a 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72 ize);. pPager
122ec 2d 3e 78 52 65 69 6e 69 74 65 72 28 70 50 67 29 ->xReiniter(pPg)
122ed 3b 0a 20 20 20 20 69 66 28 20 69 73 4d 61 69 6e ;. if( isMain
122ee 4a 72 6e 6c 20 26 26 20 28 21 69 73 53 61 76 65 Jrnl && (!isSave
122ef 70 6e 74 20 7c 7c 20 2a 70 4f 66 66 73 65 74 3c pnt || *pOffset<
122f0 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c =pPager->journal
122f1 48 64 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a Hdr) ){. /*
122f2 20 49 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 If the contents
122f3 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 77 65 of this page we
122f4 72 65 20 6a 75 73 74 20 72 65 73 74 6f 72 65 64 re just restored
122f5 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 0a from the main .
122f6 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c ** journal
122f7 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 73 20 file, then its
122f8 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 content must be
122f9 61 73 20 74 68 65 79 20 77 65 72 65 20 77 68 65 as they were whe
122fa 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 n the . **
122fb 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 transaction was
122fc 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 49 6e first opened. In
122fd 20 74 68 69 73 20 63 61 73 65 20 77 65 20 63 61 this case we ca
122fe 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 0a n mark the page.
122ff 20 20 20 20 20 20 2a 2a 20 61 73 20 63 6c 65 61 ** as clea
12300 6e 2c 20 73 69 6e 63 65 20 74 68 65 72 65 20 77 n, since there w
12301 69 6c 6c 20 62 65 20 6e 6f 20 6e 65 65 64 20 74 ill be no need t
12302 6f 20 77 72 69 74 65 20 69 74 20 6f 75 74 20 74 o write it out t
12303 6f 20 74 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a o the.. **.
12304 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 20 69 ** There i
12305 73 20 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e 20 s one exception
12306 74 6f 20 74 68 69 73 20 72 75 6c 65 2e 20 49 66 to this rule. If
12307 20 74 68 65 20 70 61 67 65 20 69 73 20 62 65 69 the page is bei
12308 6e 67 20 72 6f 6c 6c 65 64 0a 20 20 20 20 20 20 ng rolled.
12309 2a 2a 20 62 61 63 6b 20 61 73 20 70 61 72 74 20 ** back as part
1230a 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 28 of a savepoint (
1230b 6f 72 20 73 74 61 74 65 6d 65 6e 74 29 20 72 6f or statement) ro
1230c 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 61 6e 20 0a llback from an .
1230d 20 20 20 20 20 20 2a 2a 20 75 6e 73 79 6e 63 65 ** unsynce
1230e 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 d portion of the
1230f 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 main journal fi
12310 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 20 6e le, then it is n
12311 6f 74 20 73 61 66 65 0a 20 20 20 20 20 20 2a 2a ot safe. **
12312 20 74 6f 20 6d 61 72 6b 20 74 68 65 20 70 61 67 to mark the pag
12313 65 20 61 73 20 63 6c 65 61 6e 2e 20 54 68 69 73 e as clean. This
12314 20 69 73 20 62 65 63 61 75 73 65 20 6d 61 72 6b is because mark
12315 69 6e 67 20 74 68 65 20 70 61 67 65 20 61 73 0a ing the page as.
12316 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 6e 20 77 ** clean w
12317 69 6c 6c 20 63 6c 65 61 72 20 74 68 65 20 50 47 ill clear the PG
12318 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c HDR_NEED_SYNC fl
12319 61 67 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61 ag. Since the pa
1231a 67 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 ge is. ** a
1231b 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 6a 6f lready in the jo
1231c 75 72 6e 61 6c 20 66 69 6c 65 20 28 72 65 63 6f urnal file (reco
1231d 72 64 65 64 20 69 6e 20 50 61 67 65 72 2e 70 49 rded in Pager.pI
1231e 6e 4a 6f 75 72 6e 61 6c 29 20 61 6e 64 0a 20 20 nJournal) and.
1231f 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52 ** the PGHDR
12320 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 _NEED_SYNC flag
12321 69 73 20 63 6c 65 61 72 65 64 2c 20 69 66 20 74 is cleared, if t
12322 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74 he page is writt
12323 65 6e 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 61 en to. ** a
12324 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 69 73 gain within this
12325 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 transaction, it
12326 20 77 69 6c 6c 20 62 65 20 6d 61 72 6b 65 64 20 will be marked
12327 61 73 20 64 69 72 74 79 20 62 75 74 0a 20 20 20 as dirty but.
12328 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52 5f ** the PGHDR_
12329 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 77 NEED_SYNC flag w
1232a 69 6c 6c 20 6e 6f 74 20 62 65 20 73 65 74 2e 20 ill not be set.
1232b 49 74 20 63 6f 75 6c 64 20 74 68 65 6e 20 70 6f It could then po
1232c 74 65 6e 74 69 61 6c 6c 79 0a 20 20 20 20 20 20 tentially.
1232d 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75 ** be written ou
1232e 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 t into the datab
1232f 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20 ase file before
12330 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 its journal file
12331 0a 20 20 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e . ** segmen
12332 74 20 69 73 20 73 79 6e 63 65 64 2e 20 49 66 20 t is synced. If
12333 61 20 63 72 61 73 68 20 6f 63 63 75 72 73 20 64 a crash occurs d
12334 75 72 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f 77 69 uring or followi
12335 6e 67 20 74 68 69 73 2c 0a 20 20 20 20 20 20 2a ng this,. *
12336 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 * database corru
12337 70 74 69 6f 6e 20 6d 61 79 20 65 6e 73 75 65 2e ption may ensue.
12338 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
12339 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b sqlite3PcacheMak
1233a 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 20 eClean(pPg);.
1233b 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }.#ifdef SQLITE
1233c 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 _CHECK_PAGES.
1233d 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d pPg->pageHash =
1233e 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 pager_pagehash(
1233f 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 pPg);.#endif.
12340 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73 20 /* If this was
12341 70 61 67 65 20 31 2c 20 74 68 65 6e 20 72 65 73 page 1, then res
12342 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20 6f tore the value o
12343 66 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 f Pager.dbFileVe
12344 72 73 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 74 68 rs.. ** Do th
12345 69 73 20 62 65 66 6f 72 65 20 61 6e 79 20 64 65 is before any de
12346 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 69 coding. */. i
12347 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 f( pgno==1 ){.
12348 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 memcpy(&pPag
12349 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 er->dbFileVers,
1234a 26 28 28 75 38 2a 29 70 44 61 74 61 29 5b 32 34 &((u8*)pData)[24
1234b 5d 2c 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d ],sizeof(pPager-
1234c 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 >dbFileVers));.
1234d 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 }.. /* Dec
1234e 6f 64 65 20 74 68 65 20 70 61 67 65 20 6a 75 73 ode the page jus
1234f 74 20 72 65 61 64 20 66 72 6f 6d 20 64 69 73 6b t read from disk
12350 20 2a 2f 0a 20 20 20 20 43 4f 44 45 43 31 28 70 */. CODEC1(p
12351 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 Pager, pData, pP
12352 67 2d 3e 70 67 6e 6f 2c 20 33 2c 20 72 63 3d 53 g->pgno, 3, rc=S
12353 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 QLITE_NOMEM);.
12354 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 sqlite3PcacheR
12355 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 20 7d elease(pPg);. }
12356 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
12357 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 ./*.** Parameter
12358 20 7a 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 zMaster is the
12359 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 name of a master
1235a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 journal file. A
1235b 20 73 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 6c 0a single journal.
1235c 2a 2a 20 66 69 6c 65 20 74 68 61 74 20 72 65 66 ** file that ref
1235d 65 72 72 65 64 20 74 6f 20 74 68 65 20 6d 61 73 erred to the mas
1235e 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
1235f 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 72 has just been r
12360 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 20 54 olled back..** T
12361 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 his routine chec
12362 6b 73 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 ks if it is poss
12363 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74 ible to delete t
12364 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
12365 6c 20 66 69 6c 65 2c 0a 2a 2a 20 61 6e 64 20 64 l file,.** and d
12366 6f 65 73 20 73 6f 20 69 66 20 69 74 20 69 73 2e oes so if it is.
12367 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 .**.** Argument
12368 7a 4d 61 73 74 65 72 20 6d 61 79 20 70 6f 69 6e zMaster may poin
12369 74 20 74 6f 20 50 61 67 65 72 2e 70 54 6d 70 53 t to Pager.pTmpS
1236a 70 61 63 65 2e 20 53 6f 20 74 68 61 74 20 62 75 pace. So that bu
1236b 66 66 65 72 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 ffer is not .**
1236c 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 available for us
1236d 65 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 75 e within this fu
1236e 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 nction..**.** Wh
1236f 65 6e 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 en a master jour
12370 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 72 65 61 nal file is crea
12371 74 65 64 2c 20 69 74 20 69 73 20 70 6f 70 75 6c ted, it is popul
12372 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 ated with the na
12373 6d 65 73 20 0a 2a 2a 20 6f 66 20 61 6c 6c 20 6f mes .** of all o
12374 66 20 69 74 73 20 63 68 69 6c 64 20 6a 6f 75 72 f its child jour
12375 6e 61 6c 73 2c 20 6f 6e 65 20 61 66 74 65 72 20 nals, one after
12376 61 6e 6f 74 68 65 72 2c 20 66 6f 72 6d 61 74 74 another, formatt
12377 65 64 20 61 73 20 75 74 66 2d 38 20 0a 2a 2a 20 ed as utf-8 .**
12378 65 6e 63 6f 64 65 64 20 74 65 78 74 2e 20 54 68 encoded text. Th
12379 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20 63 68 e end of each ch
1237a 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ild journal file
1237b 20 69 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 is marked with
1237c 61 20 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e a .** nul-termin
1237d 61 74 6f 72 20 62 79 74 65 20 28 30 78 30 30 29 ator byte (0x00)
1237e 2e 20 69 2e 65 2e 20 74 68 65 20 65 6e 74 69 72 . i.e. the entir
1237f 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20 e contents of a
12380 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a master journal.*
12381 2a 20 66 69 6c 65 20 66 6f 72 20 61 20 74 72 61 * file for a tra
12382 6e 73 61 63 74 69 6f 6e 20 69 6e 76 6f 6c 76 69 nsaction involvi
12383 6e 67 20 74 77 6f 20 64 61 74 61 62 61 73 65 73 ng two databases
12384 20 6d 69 67 68 74 20 62 65 3a 0a 2a 2a 0a 2a 2a might be:.**.**
12385 20 20 20 22 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 61 "/home/bill/a
12386 2e 64 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 2f .db-journal\x00/
12387 68 6f 6d 65 2f 62 69 6c 6c 2f 62 2e 64 62 2d 6a home/bill/b.db-j
12388 6f 75 72 6e 61 6c 5c 78 30 30 22 0a 2a 2a 0a 2a ournal\x00".**.*
12389 2a 20 41 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e * A master journ
1238a 61 6c 20 66 69 6c 65 20 6d 61 79 20 6f 6e 6c 79 al file may only
1238b 20 62 65 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 be deleted once
1238c 20 61 6c 6c 20 6f 66 20 69 74 73 20 63 68 69 6c all of its chil
1238d 64 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 73 20 68 d .** journals h
1238e 61 76 65 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 ave been rolled
1238f 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 back..**.** This
12390 20 66 75 6e 63 74 69 6f 6e 20 72 65 61 64 73 20 function reads
12391 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
12392 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e the master-journ
12393 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 0a 2a 2a al file into .**
12394 20 6d 65 6d 6f 72 79 20 61 6e 64 20 6c 6f 6f 70 memory and loop
12395 73 20 74 68 72 6f 75 67 68 20 65 61 63 68 20 6f s through each o
12396 66 20 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 f the child jour
12397 6e 61 6c 20 6e 61 6d 65 73 2e 20 46 6f 72 0a 2a nal names. For.*
12398 2a 20 65 61 63 68 20 63 68 69 6c 64 20 6a 6f 75 * each child jou
12399 72 6e 61 6c 2c 20 69 74 20 63 68 65 63 6b 73 20 rnal, it checks
1239a 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 69 66 if:.**.** * if
1239b 20 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e the child journ
1239c 61 6c 20 65 78 69 73 74 73 2c 20 61 6e 64 20 69 al exists, and i
1239d 66 20 73 6f 0a 2a 2a 20 20 20 2a 20 69 66 20 74 f so.** * if t
1239e 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c he child journal
1239f 20 63 6f 6e 74 61 69 6e 73 20 61 20 72 65 66 65 contains a refe
123a0 72 65 6e 63 65 20 74 6f 20 6d 61 73 74 65 72 20 rence to master
123a1 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 journal .**
123a2 66 69 6c 65 20 7a 4d 61 73 74 65 72 0a 2a 2a 0a file zMaster.**.
123a3 2a 2a 20 49 66 20 61 20 63 68 69 6c 64 20 6a 6f ** If a child jo
123a4 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f 75 urnal can be fou
123a5 6e 64 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 nd that matches
123a6 62 6f 74 68 20 6f 66 20 74 68 65 20 63 72 69 74 both of the crit
123a7 65 72 69 61 0a 2a 2a 20 61 62 6f 76 65 2c 20 74 eria.** above, t
123a8 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
123a9 75 72 6e 73 20 77 69 74 68 6f 75 74 20 64 6f 69 urns without doi
123aa 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20 4f 74 68 ng anything. Oth
123ab 65 72 77 69 73 65 2c 20 69 66 0a 2a 2a 20 6e 6f erwise, if.** no
123ac 20 73 75 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 such child jour
123ad 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 nal can be found
123ae 2c 20 66 69 6c 65 20 7a 4d 61 73 74 65 72 20 69 , file zMaster i
123af 73 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 0a 2a s deleted from.*
123b0 2a 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 * the file-syste
123b1 6d 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f m using sqlite3O
123b2 73 44 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a sDelete()..**.**
123b3 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 If an IO error
123b4 77 69 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63 within this func
123b5 74 69 6f 6e 2c 20 61 6e 20 65 72 72 6f 72 20 63 tion, an error c
123b6 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
123b7 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f This.** functio
123b8 6e 20 61 6c 6c 6f 63 61 74 65 73 20 6d 65 6d 6f n allocates memo
123b9 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 ry by calling sq
123ba 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 20 49 lite3Malloc(). I
123bb 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a f an allocation.
123bc 2a 2a 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 ** fails, SQLITE
123bd 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e _NOMEM is return
123be 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 ed. Otherwise, i
123bf 66 20 6e 6f 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f f no IO or mallo
123c0 63 20 65 72 72 6f 72 73 20 0a 2a 2a 20 6f 63 63 c errors .** occ
123c1 75 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 ur, SQLITE_OK is
123c2 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
123c3 20 54 4f 44 4f 3a 20 54 68 69 73 20 66 75 6e 63 TODO: This func
123c4 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 tion allocates a
123c5 20 73 69 6e 67 6c 65 20 62 6c 6f 63 6b 20 6f 66 single block of
123c6 20 6d 65 6d 6f 72 79 20 74 6f 20 6c 6f 61 64 0a memory to load.
123c7 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f ** the entire co
123c8 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 6d 61 ntents of the ma
123c9 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
123ca 65 2e 20 54 68 69 73 20 63 6f 75 6c 64 20 62 65 e. This could be
123cb 0a 2a 2a 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 .** a couple of
123cc 6b 69 6c 6f 62 79 74 65 73 20 6f 72 20 73 6f 20 kilobytes or so
123cd 2d 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 6c 61 - potentially la
123ce 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 61 rger than the pa
123cf 67 65 20 0a 2a 2a 20 73 69 7a 65 2e 0a 2a 2f 0a ge .** size..*/.
123d0 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
123d1 5f 64 65 6c 6d 61 73 74 65 72 28 50 61 67 65 72 _delmaster(Pager
123d2 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 *pPager, const
123d3 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a char *zMaster){.
123d4 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
123d5 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 Vfs = pPager->pV
123d6 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 fs;. int rc;
123d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
123d8 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
123d9 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 /. sqlite3_file
123da 20 2a 70 4d 61 73 74 65 72 3b 20 20 20 20 2f 2a *pMaster; /*
123db 20 4d 61 6c 6c 6f 63 27 64 20 6d 61 73 74 65 72 Malloc'd master
123dc 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 -journal file de
123dd 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71 scriptor */. sq
123de 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 6f 75 lite3_file *pJou
123df 72 6e 61 6c 3b 20 20 20 2f 2a 20 4d 61 6c 6c 6f rnal; /* Mallo
123e0 63 27 64 20 63 68 69 6c 64 2d 6a 6f 75 72 6e 61 c'd child-journa
123e1 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f l file descripto
123e2 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 r */. char *zMa
123e3 73 74 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b sterJournal = 0;
123e4 20 2f 2a 20 43 6f 6e 74 65 6e 74 73 20 6f 66 20 /* Contents of
123e5 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
123e6 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6e 4d 61 ile */. i64 nMa
123e7 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 sterJournal;
123e8 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6d 61 /* Size of ma
123e9 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
123ea 65 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 e */.. /* Alloc
123eb 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 62 6f ate space for bo
123ec 74 68 20 74 68 65 20 70 4a 6f 75 72 6e 61 6c 20 th the pJournal
123ed 61 6e 64 20 70 4d 61 73 74 65 72 20 66 69 6c 65 and pMaster file
123ee 20 64 65 73 63 72 69 70 74 6f 72 73 2e 0a 20 20 descriptors..
123ef 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c ** If successful
123f0 2c 20 6f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 , open the maste
123f1 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 r journal file f
123f2 6f 72 20 72 65 61 64 69 6e 67 2e 0a 20 20 2a 2f or reading.. */
123f3 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 28 73 71 . pMaster = (sq
123f4 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c lite3_file *)sql
123f5 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 ite3MallocZero(p
123f6 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 2a 20 Vfs->szOsFile *
123f7 32 29 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c 20 3d 2);. pJournal =
123f8 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a (sqlite3_file *
123f9 29 28 28 28 75 38 20 2a 29 70 4d 61 73 74 65 72 )(((u8 *)pMaster
123fa 29 20 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 ) + pVfs->szOsFi
123fb 6c 65 29 3b 0a 20 20 69 66 28 20 21 70 4d 61 73 le);. if( !pMas
123fc 74 65 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ter ){. rc =
123fd 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
123fe 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 }else{. const
123ff 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 int flags = (SQ
12400 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e LITE_OPEN_READON
12401 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d LY|SQLITE_OPEN_M
12402 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0a ASTER_JOURNAL);.
12403 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
12404 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d 61 OsOpen(pVfs, zMa
12405 73 74 65 72 2c 20 70 4d 61 73 74 65 72 2c 20 66 ster, pMaster, f
12406 6c 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 lags, 0);. }.
12407 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
12408 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 K ) goto delmast
12409 65 72 5f 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20 er_out;.. rc =
1240a 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a sqlite3OsFileSiz
1240b 65 28 70 4d 61 73 74 65 72 2c 20 26 6e 4d 61 73 e(pMaster, &nMas
1240c 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 69 terJournal);. i
1240d 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1240e 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 ) goto delmaste
1240f 72 5f 6f 75 74 3b 0a 0a 20 20 69 66 28 20 6e 4d r_out;.. if( nM
12410 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3e 30 20 29 asterJournal>0 )
12411 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4a 6f 75 {. char *zJou
12412 72 6e 61 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a rnal;. char *
12413 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 30 3b 0a zMasterPtr = 0;.
12414 20 20 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 50 int nMasterP
12415 74 72 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 tr = pVfs->mxPat
12416 68 6e 61 6d 65 2b 31 3b 0a 0a 20 20 20 20 2f 2a hname+1;.. /*
12417 20 4c 6f 61 64 20 74 68 65 20 65 6e 74 69 72 65 Load the entire
12418 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
12419 66 69 6c 65 20 69 6e 74 6f 20 73 70 61 63 65 20 file into space
1241a 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 20 20 obtained from.
1241b 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c ** sqlite3_mal
1241c 6c 6f 63 28 29 20 61 6e 64 20 70 6f 69 6e 74 65 loc() and pointe
1241d 64 20 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 4a d to by zMasterJ
1241e 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2f 0a ournal. . */.
1241f 20 20 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e zMasterJourn
12420 61 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c al = sqlite3Mall
12421 6f 63 28 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a oc((int)nMasterJ
12422 6f 75 72 6e 61 6c 20 2b 20 6e 4d 61 73 74 65 72 ournal + nMaster
12423 50 74 72 20 2b 20 31 29 3b 0a 20 20 20 20 69 66 Ptr + 1);. if
12424 28 20 21 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 ( !zMasterJourna
12425 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 l ){. rc =
12426 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
12427 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 goto delmast
12428 65 72 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 er_out;. }.
12429 20 20 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 26 zMasterPtr = &
1242a 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e zMasterJournal[n
1242b 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2b 31 5d MasterJournal+1]
1242c 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1242d 65 33 4f 73 52 65 61 64 28 70 4d 61 73 74 65 72 e3OsRead(pMaster
1242e 2c 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c , zMasterJournal
1242f 2c 20 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f , (int)nMasterJo
12430 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 69 urnal, 0);. i
12431 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
12432 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 ) goto delmaste
12433 72 5f 6f 75 74 3b 0a 20 20 20 20 7a 4d 61 73 74 r_out;. zMast
12434 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 erJournal[nMaste
12435 72 4a 6f 75 72 6e 61 6c 5d 20 3d 20 30 3b 0a 0a rJournal] = 0;..
12436 20 20 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a zJournal = z
12437 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 0a 20 MasterJournal;.
12438 20 20 20 77 68 69 6c 65 28 20 28 7a 4a 6f 75 72 while( (zJour
12439 6e 61 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e nal-zMasterJourn
1243a 61 6c 29 3c 6e 4d 61 73 74 65 72 4a 6f 75 72 6e al)<nMasterJourn
1243b 61 6c 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 al ){. int
1243c 65 78 69 73 74 73 3b 0a 20 20 20 20 20 20 72 63 exists;. rc
1243d 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 = sqlite3OsAcce
1243e 73 73 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 ss(pVfs, zJourna
1243f 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 l, SQLITE_ACCESS
12440 5f 45 58 49 53 54 53 2c 20 26 65 78 69 73 74 73 _EXISTS, &exists
12441 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
12442 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
12443 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 goto delma
12444 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 ster_out;.
12445 7d 0a 20 20 20 20 20 20 69 66 28 20 65 78 69 73 }. if( exis
12446 74 73 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a ts ){. /*
12447 20 4f 6e 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 One of the jour
12448 6e 61 6c 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 nals pointed to
12449 62 79 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f by the master jo
1244a 75 72 6e 61 6c 20 65 78 69 73 74 73 2e 0a 20 20 urnal exists..
1244b 20 20 20 20 20 20 2a 2a 20 4f 70 65 6e 20 69 74 ** Open it
1244c 20 61 6e 64 20 63 68 65 63 6b 20 69 66 20 69 74 and check if it
1244d 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6d points at the m
1244e 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 49 aster journal. I
1244f 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 2c f. ** so,
12450 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 return without
12451 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6d 61 73 deleting the mas
12452 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
12453 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
12454 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 int c;.
12455 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 int flags =
12456 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 (SQLITE_OPEN_REA
12457 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 DONLY|SQLITE_OPE
12458 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 3b N_MAIN_JOURNAL);
12459 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
1245a 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 lite3OsOpen(pVfs
1245b 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20 70 4a 6f 75 , zJournal, pJou
1245c 72 6e 61 6c 2c 20 66 6c 61 67 73 2c 20 30 29 3b rnal, flags, 0);
1245d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
1245e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1245f 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c goto del
12460 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 master_out;.
12461 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 72 }.. r
12462 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f c = readMasterJo
12463 75 72 6e 61 6c 28 70 4a 6f 75 72 6e 61 6c 2c 20 urnal(pJournal,
12464 7a 4d 61 73 74 65 72 50 74 72 2c 20 6e 4d 61 73 zMasterPtr, nMas
12465 74 65 72 50 74 72 29 3b 0a 20 20 20 20 20 20 20 terPtr);.
12466 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
12467 70 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 pJournal);.
12468 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
12469 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1246a 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 goto delmaster
1246b 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a _out;. }.
1246c 0a 20 20 20 20 20 20 20 20 63 20 3d 20 7a 4d 61 . c = zMa
1246d 73 74 65 72 50 74 72 5b 30 5d 21 3d 30 20 26 26 sterPtr[0]!=0 &&
1246e 20 73 74 72 63 6d 70 28 7a 4d 61 73 74 65 72 50 strcmp(zMasterP
1246f 74 72 2c 20 7a 4d 61 73 74 65 72 29 3d 3d 30 3b tr, zMaster)==0;
12470 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 20 29 . if( c )
12471 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 {. /* W
12472 65 20 68 61 76 65 20 61 20 6d 61 74 63 68 2e 20 e have a match.
12473 44 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 Do not delete th
12474 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
12475 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 file. */.
12476 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 goto delmast
12477 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 er_out;.
12478 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
12479 7a 4a 6f 75 72 6e 61 6c 20 2b 3d 20 28 73 71 6c zJournal += (sql
1247a 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4a 6f ite3Strlen30(zJo
1247b 75 72 6e 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d urnal)+1);. }
1247c 0a 20 20 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73 . }. . rc = s
1247d 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 qlite3OsDelete(p
1247e 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 Vfs, zMaster, 0)
1247f 3b 0a 0a 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 ;..delmaster_out
12480 3a 0a 20 20 69 66 28 20 7a 4d 61 73 74 65 72 4a :. if( zMasterJ
12481 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 ournal ){. sq
12482 6c 69 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 lite3_free(zMast
12483 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 7d 20 erJournal);. }
12484 20 0a 20 20 69 66 28 20 70 4d 61 73 74 65 72 20 . if( pMaster
12485 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 ){. sqlite3Os
12486 43 6c 6f 73 65 28 70 4d 61 73 74 65 72 29 3b 0a Close(pMaster);.
12487 20 20 20 20 61 73 73 65 72 74 28 20 21 69 73 4f assert( !isO
12488 70 65 6e 28 70 4a 6f 75 72 6e 61 6c 29 20 29 3b pen(pJournal) );
12489 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 . }. sqlite3_f
1248a 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 ree(pMaster);.
1248b 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f return rc;.}.../
1248c 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
1248d 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 on is used to ch
1248e 61 6e 67 65 20 74 68 65 20 61 63 74 75 61 6c 20 ange the actual
1248f 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 size of the data
12490 62 61 73 65 20 0a 2a 2a 20 66 69 6c 65 20 69 6e base .** file in
12491 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d the file-system
12492 2e 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 . This only happ
12493 65 6e 73 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 ens when committ
12494 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f ing a transactio
12495 6e 2c 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 69 6e 67 n,.** or rolling
12496 20 62 61 63 6b 20 61 20 74 72 61 6e 73 61 63 74 back a transact
12497 69 6f 6e 20 28 69 6e 63 6c 75 64 69 6e 67 20 72 ion (including r
12498 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f olling back a ho
12499 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2a 0a 2a t-journal)..**.*
1249a 2a 20 49 66 20 74 68 65 20 6d 61 69 6e 20 64 61 * If the main da
1249b 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6e tabase file is n
1249c 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 61 6e 20 65 ot open, or an e
1249d 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 xclusive lock is
1249e 20 6e 6f 74 0a 2a 2a 20 68 65 6c 64 2c 20 74 68 not.** held, th
1249f 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
124a0 20 6e 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 no-op. Otherwis
124a1 65 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 e, the size of t
124a2 68 65 20 66 69 6c 65 20 69 73 0a 2a 2a 20 63 68 he file is.** ch
124a3 61 6e 67 65 64 20 74 6f 20 6e 50 61 67 65 20 70 anged to nPage p
124a4 61 67 65 73 20 28 6e 50 61 67 65 2a 70 50 61 67 ages (nPage*pPag
124a5 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 er->pageSize byt
124a6 65 73 29 2e 20 49 66 20 74 68 65 20 66 69 6c 65 es). If the file
124a7 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 63 .** on disk is c
124a8 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 urrently larger
124a9 74 68 61 6e 20 6e 50 61 67 65 20 70 61 67 65 73 than nPage pages
124aa 2c 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 56 , then use the V
124ab 46 53 0a 2a 2a 20 78 54 72 75 6e 63 61 74 65 28 FS.** xTruncate(
124ac 29 20 6d 65 74 68 6f 64 20 74 6f 20 74 72 75 6e ) method to trun
124ad 63 61 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f cate it..**.** O
124ae 72 2c 20 69 74 20 6d 69 67 68 74 20 6d 69 67 68 r, it might migh
124af 74 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68 t be the case th
124b0 61 74 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 at the file on d
124b1 69 73 6b 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 isk is smaller t
124b2 68 61 6e 20 0a 2a 2a 20 6e 50 61 67 65 20 70 61 han .** nPage pa
124b3 67 65 73 2e 20 53 6f 6d 65 20 6f 70 65 72 61 74 ges. Some operat
124b4 69 6e 67 20 73 79 73 74 65 6d 20 69 6d 70 6c 65 ing system imple
124b5 6d 65 6e 74 61 74 69 6f 6e 73 20 63 61 6e 20 67 mentations can g
124b6 65 74 20 63 6f 6e 66 75 73 65 64 20 69 66 20 0a et confused if .
124b7 2a 2a 20 79 6f 75 20 74 72 79 20 74 6f 20 74 72 ** you try to tr
124b8 75 6e 63 61 74 65 20 61 20 66 69 6c 65 20 74 6f uncate a file to
124b9 20 73 6f 6d 65 20 73 69 7a 65 20 74 68 61 74 20 some size that
124ba 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 is larger than i
124bb 74 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 t .** currently
124bc 69 73 2c 20 73 6f 20 64 65 74 65 63 74 20 74 68 is, so detect th
124bd 69 73 20 63 61 73 65 20 61 6e 64 20 77 72 69 74 is case and writ
124be 65 20 61 20 73 69 6e 67 6c 65 20 7a 65 72 6f 20 e a single zero
124bf 62 79 74 65 20 74 6f 20 0a 2a 2a 20 74 68 65 20 byte to .** the
124c0 65 6e 64 20 6f 66 20 74 68 65 20 6e 65 77 20 66 end of the new f
124c1 69 6c 65 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a ile instead..**.
124c2 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c ** If successful
124c3 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
124c4 4f 4b 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 OK. If an IO err
124c5 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 or occurs while
124c6 6d 6f 64 69 66 79 69 6e 67 0a 2a 2a 20 74 68 65 modifying.** the
124c7 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
124c8 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 return the error
124c9 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c code to the cal
124ca 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ler..*/.static i
124cb 6e 74 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 nt pager_truncat
124cc 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c e(Pager *pPager,
124cd 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 Pgno nPage){.
124ce 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
124cf 4f 4b 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 OK;. if( pPager
124d0 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 ->state>=PAGER_E
124d1 58 43 4c 55 53 49 56 45 20 26 26 20 69 73 4f 70 XCLUSIVE && isOp
124d2 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 en(pPager->fd) )
124d3 7b 0a 20 20 20 20 69 36 34 20 63 75 72 72 65 6e {. i64 curren
124d4 74 53 69 7a 65 2c 20 6e 65 77 53 69 7a 65 3b 0a tSize, newSize;.
124d5 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 20 /* TODO: Is
124d6 69 74 20 73 61 66 65 20 74 6f 20 75 73 65 20 50 it safe to use P
124d7 61 67 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 ager.dbFileSize
124d8 68 65 72 65 3f 20 2a 2f 0a 20 20 20 20 72 63 20 here? */. rc
124d9 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 = sqlite3OsFileS
124da 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ize(pPager->fd,
124db 26 63 75 72 72 65 6e 74 53 69 7a 65 29 3b 0a 20 ¤tSize);.
124dc 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 70 50 61 newSize = pPa
124dd 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2a 28 69 ger->pageSize*(i
124de 36 34 29 6e 50 61 67 65 3b 0a 20 20 20 20 69 66 64)nPage;. if
124df 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
124e0 26 26 20 63 75 72 72 65 6e 74 53 69 7a 65 21 3d && currentSize!=
124e1 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 newSize ){.
124e2 20 69 66 28 20 63 75 72 72 65 6e 74 53 69 7a 65 if( currentSize
124e3 3e 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 >newSize ){.
124e4 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
124e5 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 OsTruncate(pPage
124e6 72 2d 3e 66 64 2c 20 6e 65 77 53 69 7a 65 29 3b r->fd, newSize);
124e7 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
124e8 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
124e9 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 e3OsWrite(pPager
124ea 2d 3e 66 64 2c 20 22 22 2c 20 31 2c 20 6e 65 77 ->fd, "", 1, new
124eb 53 69 7a 65 2d 31 29 3b 0a 20 20 20 20 20 20 7d Size-1);. }
124ec 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
124ed 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
124ee 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 pPager->dbFi
124ef 6c 65 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a leSize = nPage;.
124f0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
124f1 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
124f2 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
124f3 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 61 67 value of the Pag
124f4 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61 er.sectorSize va
124f5 72 69 61 62 6c 65 20 66 6f 72 20 74 68 65 20 67 riable for the g
124f6 69 76 65 6e 0a 2a 2a 20 70 61 67 65 72 20 62 61 iven.** pager ba
124f7 73 65 64 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 sed on the value
124f8 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 returned by the
124f9 20 78 53 65 63 74 6f 72 53 69 7a 65 20 6d 65 74 xSectorSize met
124fa 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6f 70 hod.** of the op
124fb 65 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 en database file
124fc 2e 20 54 68 65 20 73 65 63 74 6f 72 20 73 69 7a . The sector siz
124fd 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 75 e will be used u
124fe 73 65 64 20 0a 2a 2a 20 74 6f 20 64 65 74 65 72 sed .** to deter
124ff 6d 69 6e 65 20 74 68 65 20 73 69 7a 65 20 61 6e mine the size an
12500 64 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 6a d alignment of j
12501 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 61 6e ournal header an
12502 64 20 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 d .** master jou
12503 72 6e 61 6c 20 70 6f 69 6e 74 65 72 73 20 77 69 rnal pointers wi
12504 74 68 69 6e 20 63 72 65 61 74 65 64 20 6a 6f 75 thin created jou
12505 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a rnal files..**.*
12506 2a 20 46 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 * For temporary
12507 66 69 6c 65 73 20 74 68 65 20 65 66 66 65 63 74 files the effect
12508 69 76 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 ive sector size
12509 69 73 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 is always 512 by
1250a 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 tes..**.** Other
1250b 77 69 73 65 2c 20 66 6f 72 20 6e 6f 6e 2d 74 65 wise, for non-te
1250c 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2c 20 74 mporary files, t
1250d 68 65 20 65 66 66 65 63 74 69 76 65 20 73 65 63 he effective sec
1250e 74 6f 72 20 73 69 7a 65 20 69 73 0a 2a 2a 20 74 tor size is.** t
1250f 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
12510 64 20 62 79 20 74 68 65 20 78 53 65 63 74 6f 72 d by the xSector
12511 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 72 6f Size() method ro
12512 75 6e 64 65 64 20 75 70 20 74 6f 20 35 31 32 20 unded up to 512
12513 69 66 0a 2a 2a 20 69 74 20 69 73 20 6c 65 73 73 if.** it is less
12514 20 74 68 61 6e 20 35 31 32 2c 20 6f 72 20 72 6f than 512, or ro
12515 75 6e 64 65 64 20 64 6f 77 6e 20 74 6f 20 4d 41 unded down to MA
12516 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 66 X_SECTOR_SIZE if
12517 20 69 74 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 it.** is greate
12518 72 20 74 68 61 6e 20 4d 41 58 5f 53 45 43 54 4f r than MAX_SECTO
12519 52 5f 53 49 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 R_SIZE..*/.stati
1251a 63 20 76 6f 69 64 20 73 65 74 53 65 63 74 6f 72 c void setSector
1251b 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 Size(Pager *pPag
1251c 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 er){. assert( i
1251d 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 sOpen(pPager->fd
1251e 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d ) || pPager->tem
1251f 70 46 69 6c 65 20 29 3b 0a 0a 20 20 69 66 28 20 pFile );.. if(
12520 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c !pPager->tempFil
12521 65 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74 e ){. /* Sect
12522 6f 72 20 73 69 7a 65 20 64 6f 65 73 6e 27 74 20 or size doesn't
12523 6d 61 74 74 65 72 20 66 6f 72 20 74 65 6d 70 6f matter for tempo
12524 72 61 72 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f rary files. Also
12525 2c 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a , the file. *
12526 2a 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 62 * may not have b
12527 65 65 6e 20 6f 70 65 6e 65 64 20 79 65 74 2c 20 een opened yet,
12528 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 in which case th
12529 65 20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 e OsSectorSize()
1252a 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c . ** call wil
1252b 6c 20 73 65 67 66 61 75 6c 74 2e 0a 20 20 20 20 l segfault..
1252c 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 */. pPager->s
1252d 65 63 74 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69 ectorSize = sqli
1252e 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 te3OsSectorSize(
1252f 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d pPager->fd);. }
12530 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 . if( pPager->s
12531 65 63 74 6f 72 53 69 7a 65 3c 35 31 32 20 29 7b ectorSize<512 ){
12532 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 . pPager->sec
12533 74 6f 72 53 69 7a 65 20 3d 20 35 31 32 3b 0a 20 torSize = 512;.
12534 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d }. if( pPager-
12535 3e 73 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f >sectorSize>MAX_
12536 53 45 43 54 4f 52 5f 53 49 5a 45 20 29 7b 0a 20 SECTOR_SIZE ){.
12537 20 20 20 61 73 73 65 72 74 28 20 4d 41 58 5f 53 assert( MAX_S
12538 45 43 54 4f 52 5f 53 49 5a 45 3e 3d 35 31 32 20 ECTOR_SIZE>=512
12539 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 );. pPager->s
1253a 65 63 74 6f 72 53 69 7a 65 20 3d 20 4d 41 58 5f ectorSize = MAX_
1253b 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 7d SECTOR_SIZE;. }
1253c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 .}../*.** Playba
1253d 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 ck the journal a
1253e 6e 64 20 74 68 75 73 20 72 65 73 74 6f 72 65 20 nd thus restore
1253f 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
12540 65 20 74 6f 0a 2a 2a 20 74 68 65 20 73 74 61 74 e to.** the stat
12541 65 20 69 74 20 77 61 73 20 69 6e 20 62 65 66 6f e it was in befo
12542 72 65 20 77 65 20 73 74 61 72 74 65 64 20 6d 61 re we started ma
12543 6b 69 6e 67 20 63 68 61 6e 67 65 73 2e 20 20 0a king changes. .
12544 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 **.** The journa
12545 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 69 73 l file format is
12546 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a as follows: .**
12547 0a 2a 2a 20 20 28 31 29 20 20 38 20 62 79 74 65 .** (1) 8 byte
12548 20 70 72 65 66 69 78 2e 20 20 41 20 63 6f 70 79 prefix. A copy
12549 20 6f 66 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 of aJournalMagi
1254a 63 5b 5d 2e 0a 2a 2a 20 20 28 32 29 20 20 34 20 c[]..** (2) 4
1254b 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 byte big-endian
1254c 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 integer which is
1254d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 the number of v
1254e 61 6c 69 64 20 70 61 67 65 20 72 65 63 6f 72 64 alid page record
1254f 73 0a 2a 2a 20 20 20 20 20 20 20 69 6e 20 74 68 s.** in th
12550 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 66 20 74 e journal. If t
12551 68 69 73 20 76 61 6c 75 65 20 69 73 20 30 78 66 his value is 0xf
12552 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20 63 6f fffffff, then co
12553 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 mpute the.**
12554 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 number of pag
12555 65 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 e records from t
12556 68 65 20 6a 6f 75 72 6e 61 6c 20 73 69 7a 65 2e he journal size.
12557 0a 2a 2a 20 20 28 33 29 20 20 34 20 62 79 74 65 .** (3) 4 byte
12558 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 big-endian inte
12559 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 ger which is the
1255a 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 66 initial value f
1255b 6f 72 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 or the .**
1255c 20 73 61 6e 69 74 79 20 63 68 65 63 6b 73 75 6d sanity checksum
1255d 2e 0a 2a 2a 20 20 28 34 29 20 20 34 20 62 79 74 ..** (4) 4 byt
1255e 65 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 e integer which
1255f 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
12560 20 70 61 67 65 73 20 74 6f 20 74 72 75 6e 63 61 pages to trunca
12561 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 te the.**
12562 64 61 74 61 62 61 73 65 20 74 6f 20 64 75 72 69 database to duri
12563 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a ng a rollback..*
12564 2a 20 20 28 35 29 20 20 34 20 62 79 74 65 20 62 * (5) 4 byte b
12565 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 ig-endian intege
12566 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 73 r which is the s
12567 65 63 74 6f 72 20 73 69 7a 65 2e 20 20 54 68 65 ector size. The
12568 20 68 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 20 header.**
12569 20 69 73 20 74 68 69 73 20 6d 61 6e 79 20 62 79 is this many by
1256a 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 tes in size..**
1256b 20 28 36 29 20 20 34 20 62 79 74 65 20 62 69 67 (6) 4 byte big
1256c 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 -endian integer
1256d 77 68 69 63 68 20 69 73 20 74 68 65 20 70 61 67 which is the pag
1256e 65 20 63 61 73 65 2e 0a 2a 2a 20 20 28 37 29 20 e case..** (7)
1256f 20 34 20 62 79 74 65 20 69 6e 74 65 67 65 72 20 4 byte integer
12570 77 68 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d which is the num
12571 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
12572 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
12573 61 6c 0a 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 al.** name
12574 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6d 61 79 . The value may
12575 20 62 65 20 7a 65 72 6f 20 28 69 6e 64 69 63 61 be zero (indica
12576 74 65 20 74 68 61 74 20 74 68 65 72 65 20 69 73 te that there is
12577 20 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20 no master.**
12578 20 20 20 20 6a 6f 75 72 6e 61 6c 2e 29 0a 2a 2a journal.).**
12579 20 20 28 38 29 20 20 4e 20 62 79 74 65 73 20 6f (8) N bytes o
1257a 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 f the master jou
1257b 72 6e 61 6c 20 6e 61 6d 65 2e 20 20 54 68 65 20 rnal name. The
1257c 6e 61 6d 65 20 77 69 6c 6c 20 62 65 20 6e 75 6c name will be nul
1257d 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 -terminated.**
1257e 20 20 20 20 20 61 6e 64 20 6d 69 67 68 74 20 62 and might b
1257f 65 20 73 68 6f 72 74 65 72 20 74 68 61 6e 20 74 e shorter than t
12580 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 he value read fr
12581 6f 6d 20 28 35 29 2e 20 20 49 66 20 74 68 65 20 om (5). If the
12582 66 69 72 73 74 20 62 79 74 65 0a 2a 2a 20 20 20 first byte.**
12583 20 20 20 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 of the name
12584 69 73 20 5c 30 30 30 20 74 68 65 6e 20 74 68 65 is \000 then the
12585 72 65 20 69 73 20 6e 6f 20 6d 61 73 74 65 72 20 re is no master
12586 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61 journal. The ma
12587 73 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 6a 6f ster.** jo
12588 75 72 6e 61 6c 20 6e 61 6d 65 20 69 73 20 73 74 urnal name is st
12589 6f 72 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a ored in UTF-8..*
1258a 2a 20 20 28 39 29 20 20 5a 65 72 6f 20 6f 72 20 * (9) Zero or
1258b 6d 6f 72 65 20 70 61 67 65 73 20 69 6e 73 74 61 more pages insta
1258c 6e 63 65 73 2c 20 65 61 63 68 20 61 73 20 66 6f nces, each as fo
1258d 6c 6c 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 20 20 llows:.**
1258e 20 2b 20 20 34 20 62 79 74 65 20 70 61 67 65 20 + 4 byte page
1258f 6e 75 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 20 number..**
12590 20 20 2b 20 20 70 50 61 67 65 72 2d 3e 70 61 67 + pPager->pag
12591 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64 eSize bytes of d
12592 61 74 61 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b ata..** +
12593 20 20 34 20 62 79 74 65 20 63 68 65 63 6b 73 75 4 byte checksu
12594 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 20 m.**.** When we
12595 73 70 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f 75 speak of the jou
12596 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 77 65 20 rnal header, we
12597 6d 65 61 6e 20 74 68 65 20 66 69 72 73 74 20 38 mean the first 8
12598 20 69 74 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 2a items above..**
12599 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74 Each entry in t
1259a 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e he journal is an
1259b 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
1259c 20 39 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a 9th item..**.**
1259d 20 43 61 6c 6c 20 74 68 65 20 76 61 6c 75 65 20 Call the value
1259e 66 72 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64 20 from the second
1259f 62 75 6c 6c 65 74 20 22 6e 52 65 63 22 2e 20 20 bullet "nRec".
125a0 6e 52 65 63 20 69 73 20 74 68 65 20 6e 75 6d 62 nRec is the numb
125a1 65 72 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 70 er of.** valid p
125a2 61 67 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 age entries in t
125a3 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20 he journal. In
125a4 6d 6f 73 74 20 63 61 73 65 73 2c 20 79 6f 75 20 most cases, you
125a5 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a can compute the.
125a6 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 ** value of nRec
125a7 20 66 72 6f 6d 20 74 68 65 20 73 69 7a 65 20 6f from the size o
125a8 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
125a9 6c 65 2e 20 20 42 75 74 20 69 66 20 61 20 70 6f le. But if a po
125aa 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f wer.** failure o
125ab 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 74 68 ccurred while th
125ac 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 e journal was be
125ad 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 69 74 20 ing written, it
125ae 63 6f 75 6c 64 20 62 65 20 74 68 65 0a 2a 2a 20 could be the.**
125af 63 61 73 65 20 74 68 61 74 20 74 68 65 20 73 69 case that the si
125b0 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 ze of the journa
125b1 6c 20 66 69 6c 65 20 68 61 64 20 61 6c 72 65 61 l file had alrea
125b2 64 79 20 62 65 65 6e 20 69 6e 63 72 65 61 73 65 dy been increase
125b3 64 20 62 75 74 0a 2a 2a 20 74 68 65 20 65 78 74 d but.** the ext
125b4 72 61 20 65 6e 74 72 69 65 73 20 68 61 64 20 6e ra entries had n
125b5 6f 74 20 79 65 74 20 6d 61 64 65 20 69 74 20 73 ot yet made it s
125b6 61 66 65 6c 79 20 74 6f 20 64 69 73 6b 2e 20 20 afely to disk.
125b7 49 6e 20 73 75 63 68 20 61 20 63 61 73 65 2c 0a In such a case,.
125b8 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ** the value of
125b9 6e 52 65 63 20 63 6f 6d 70 75 74 65 64 20 66 72 nRec computed fr
125ba 6f 6d 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 om the file size
125bb 20 77 6f 75 6c 64 20 62 65 20 74 6f 6f 20 6c 61 would be too la
125bc 72 67 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 61 rge. For.** tha
125bd 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 61 6c 77 t reason, we alw
125be 61 79 73 20 75 73 65 20 74 68 65 20 6e 52 65 63 ays use the nRec
125bf 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 68 65 value in the he
125c0 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ader..**.** If t
125c1 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 73 he nRec value is
125c2 20 30 78 66 66 66 66 66 66 66 66 20 69 74 20 6d 0xffffffff it m
125c3 65 61 6e 73 20 74 68 61 74 20 6e 52 65 63 20 73 eans that nRec s
125c4 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 hould be compute
125c5 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66 69 d.** from the fi
125c6 6c 65 20 73 69 7a 65 2e 20 20 54 68 69 73 20 76 le size. This v
125c7 61 6c 75 65 20 69 73 20 75 73 65 64 20 77 68 65 alue is used whe
125c8 6e 20 74 68 65 20 75 73 65 72 20 73 65 6c 65 63 n the user selec
125c9 74 73 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 6e ts the.** no-syn
125ca 63 20 6f 70 74 69 6f 6e 20 66 6f 72 20 74 68 65 c option for the
125cb 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 20 70 6f 77 journal. A pow
125cc 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 er failure could
125cd 20 6c 65 61 64 20 74 6f 20 63 6f 72 72 75 70 74 lead to corrupt
125ce 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 ion.** in this c
125cf 61 73 65 2e 20 20 42 75 74 20 66 6f 72 20 74 68 ase. But for th
125d0 69 6e 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f 72 ings like tempor
125d1 61 72 79 20 74 61 62 6c 65 20 28 77 68 69 63 68 ary table (which
125d2 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c 65 will be.** dele
125d3 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 6f 77 ted when the pow
125d4 65 72 20 69 73 20 72 65 73 74 6f 72 65 64 29 20 er is restored)
125d5 77 65 20 64 6f 6e 27 74 20 63 61 72 65 2e 20 20 we don't care.
125d6 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 .**.** If the fi
125d7 6c 65 20 6f 70 65 6e 65 64 20 61 73 20 74 68 65 le opened as the
125d8 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
125d9 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d not a well-form
125da 65 64 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 ed.** journal fi
125db 6c 65 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 le then all page
125dc 73 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 s up to the firs
125dd 74 20 63 6f 72 72 75 70 74 65 64 20 70 61 67 65 t corrupted page
125de 20 61 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 are rolled.** b
125df 61 63 6b 20 28 6f 72 20 6e 6f 20 70 61 67 65 73 ack (or no pages
125e0 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 if the journal
125e1 68 65 61 64 65 72 20 69 73 20 63 6f 72 72 75 70 header is corrup
125e2 74 65 64 29 2e 20 54 68 65 20 6a 6f 75 72 6e 61 ted). The journa
125e3 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 74 68 65 l file.** is the
125e4 6e 20 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51 n deleted and SQ
125e5 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 LITE_OK returned
125e6 2c 20 6a 75 73 74 20 61 73 20 69 66 20 6e 6f 20 , just as if no
125e7 63 6f 72 72 75 70 74 69 6f 6e 20 68 61 64 0a 2a corruption had.*
125e8 2a 20 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65 72 * been encounter
125e9 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 ed..**.** If an
125ea 49 2f 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 I/O or malloc()
125eb 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 error occurs, th
125ec 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69 e journal-file i
125ed 73 20 6e 6f 74 20 64 65 6c 65 74 65 64 0a 2a 2a s not deleted.**
125ee 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f and an error co
125ef 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a de is returned..
125f0 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 48 6f 74 20 **.** The isHot
125f1 70 61 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61 parameter indica
125f2 74 65 73 20 74 68 61 74 20 77 65 20 61 72 65 20 tes that we are
125f3 74 72 79 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 trying to rollba
125f4 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 ck a journal.**
125f5 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 61 20 that might be a
125f6 68 6f 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 72 hot journal. Or
125f7 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 , it could be th
125f8 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 at the journal i
125f9 73 20 0a 2a 2a 20 70 72 65 73 65 72 76 65 64 20 s .** preserved
125fa 62 65 63 61 75 73 65 20 6f 66 20 4a 4f 55 52 4e because of JOURN
125fb 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 6f ALMODE_PERSIST o
125fc 72 20 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 r JOURNALMODE_TR
125fd 55 4e 43 41 54 45 2e 0a 2a 2a 20 49 66 20 74 68 UNCATE..** If th
125fe 65 20 6a 6f 75 72 6e 61 6c 20 72 65 61 6c 6c 79 e journal really
125ff 20 69 73 20 68 6f 74 2c 20 72 65 73 65 74 20 74 is hot, reset t
12600 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20 70 he pager cache p
12601 72 69 6f 72 20 72 6f 6c 6c 69 6e 67 0a 2a 2a 20 rior rolling.**
12602 62 61 63 6b 20 61 6e 79 20 63 6f 6e 74 65 6e 74 back any content
12603 2e 20 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 . If the journa
12604 6c 20 69 73 20 6d 65 72 65 6c 79 20 70 65 72 73 l is merely pers
12605 69 73 74 65 6e 74 2c 20 6e 6f 20 72 65 73 65 74 istent, no reset
12606 20 69 73 0a 2a 2a 20 6e 65 65 64 65 64 2e 0a 2a is.** needed..*
12607 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
12608 65 72 5f 70 6c 61 79 62 61 63 6b 28 50 61 67 65 er_playback(Page
12609 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69 r *pPager, int i
1260a 73 48 6f 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 sHot){. sqlite3
1260b 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50 61 _vfs *pVfs = pPa
1260c 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 36 34 ger->pVfs;. i64
1260d 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 szJ;
1260e 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
1260f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
12610 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 e in bytes */.
12611 75 33 32 20 6e 52 65 63 3b 20 20 20 20 20 20 20 u32 nRec;
12612 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
12613 65 72 20 6f 66 20 52 65 63 6f 72 64 73 20 69 6e er of Records in
12614 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a the journal */.
12615 20 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 u32 u;
12616 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e /* Un
12617 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e signed loop coun
12618 74 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 ter */. Pgno mx
12619 50 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 Pg = 0;
1261a 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
1261b 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 20 69 original file i
1261c 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 n pages */. int
1261d 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
1261e 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 /* Result
1261f 63 6f 64 65 20 6f 66 20 61 20 73 75 62 72 6f 75 code of a subrou
12620 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 tine */. int re
12621 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 s = 1;
12622 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 /* Value retu
12623 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 4f rned by sqlite3O
12624 73 41 63 63 65 73 73 28 29 20 2a 2f 0a 20 20 63 sAccess() */. c
12625 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 30 har *zMaster = 0
12626 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 ; /* Name
12627 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 of master journa
12628 6c 20 66 69 6c 65 20 69 66 20 61 6e 79 20 2a 2f l file if any */
12629 0a 20 20 69 6e 74 20 6e 65 65 64 50 61 67 65 72 . int needPager
1262a 52 65 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 54 Reset; /* T
1262b 72 75 65 20 74 6f 20 72 65 73 65 74 20 70 61 67 rue to reset pag
1262c 65 20 70 72 69 6f 72 20 74 6f 20 66 69 72 73 74 e prior to first
1262d 20 70 61 67 65 20 72 6f 6c 6c 62 61 63 6b 20 2a page rollback *
1262e 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f /.. /* Figure o
1262f 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 63 6f ut how many reco
12630 72 64 73 20 61 72 65 20 69 6e 20 74 68 65 20 6a rds are in the j
12631 6f 75 72 6e 61 6c 2e 20 20 41 62 6f 72 74 20 65 ournal. Abort e
12632 61 72 6c 79 20 69 66 0a 20 20 2a 2a 20 74 68 65 arly if. ** the
12633 20 6a 6f 75 72 6e 61 6c 20 69 73 20 65 6d 70 74 journal is empt
12634 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 y.. */. assert
12635 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
12636 3e 6a 66 64 29 20 29 3b 0a 20 20 72 63 20 3d 20 >jfd) );. rc =
12637 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a sqlite3OsFileSiz
12638 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 e(pPager->jfd, &
12639 73 7a 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d szJ);. if( rc!=
1263a 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a 4a SQLITE_OK || szJ
1263b 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 ==0 ){. goto
1263c 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 end_playback;.
1263d 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 }.. /* Read the
1263e 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1263f 6e 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 6a 6f name from the jo
12640 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 20 urnal, if it is
12641 70 72 65 73 65 6e 74 2e 0a 20 20 2a 2a 20 49 66 present.. ** If
12642 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 a master journa
12643 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 73 l file name is s
12644 70 65 63 69 66 69 65 64 2c 20 62 75 74 20 74 68 pecified, but th
12645 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 0a 20 20 e file is not.
12646 2a 2a 20 70 72 65 73 65 6e 74 20 6f 6e 20 64 69 ** present on di
12647 73 6b 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 sk, then the jou
12648 72 6e 61 6c 20 69 73 20 6e 6f 74 20 68 6f 74 20 rnal is not hot
12649 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 and does not nee
1264a 64 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70 6c 61 d to be. ** pla
1264b 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a 2a 0a 20 yed back.. **.
1264c 20 2a 2a 20 54 4f 44 4f 3a 20 54 65 63 68 6e 69 ** TODO: Techni
1264d 63 61 6c 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 cally the follow
1264e 69 6e 67 20 69 73 20 61 6e 20 65 72 72 6f 72 20 ing is an error
1264f 62 65 63 61 75 73 65 20 69 74 20 61 73 73 75 6d because it assum
12650 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 62 75 66 es that. ** buf
12651 66 65 72 20 50 61 67 65 72 2e 70 54 6d 70 53 70 fer Pager.pTmpSp
12652 61 63 65 20 69 73 20 28 6d 78 50 61 74 68 6e 61 ace is (mxPathna
12653 6d 65 2b 31 29 20 62 79 74 65 73 20 6f 72 20 6c me+1) bytes or l
12654 61 72 67 65 72 2e 20 69 2e 65 2e 20 74 68 61 74 arger. i.e. that
12655 0a 20 20 2a 2a 20 28 70 50 61 67 65 72 2d 3e 70 . ** (pPager->p
12656 61 67 65 53 69 7a 65 20 3e 3d 20 70 50 61 67 65 ageSize >= pPage
12657 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e r->pVfs->mxPathn
12658 61 6d 65 2b 31 29 2e 20 55 73 69 6e 67 20 6f 73 ame+1). Using os
12659 5f 75 6e 69 78 2e 63 2c 0a 20 20 2a 2a 20 20 6d _unix.c,. ** m
1265a 78 50 61 74 68 6e 61 6d 65 20 69 73 20 35 31 32 xPathname is 512
1265b 2c 20 77 68 69 63 68 20 69 73 20 74 68 65 20 73 , which is the s
1265c 61 6d 65 20 61 73 20 74 68 65 20 6d 69 6e 69 6d ame as the minim
1265d 75 6d 20 61 6c 6c 6f 77 61 62 6c 65 20 76 61 6c um allowable val
1265e 75 65 0a 20 20 2a 2a 20 66 6f 72 20 70 61 67 65 ue. ** for page
1265f 53 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 7a 4d 61 Size.. */. zMa
12660 73 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 ster = pPager->p
12661 54 6d 70 53 70 61 63 65 3b 0a 20 20 72 63 20 3d TmpSpace;. rc =
12662 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e readMasterJourn
12663 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 al(pPager->jfd,
12664 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d zMaster, pPager-
12665 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d >pVfs->mxPathnam
12666 65 2b 31 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d e+1);. if( rc==
12667 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 SQLITE_OK && zMa
12668 73 74 65 72 5b 30 5d 20 29 7b 0a 20 20 20 20 72 ster[0] ){. r
12669 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 c = sqlite3OsAcc
1266a 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 ess(pVfs, zMaste
1266b 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 r, SQLITE_ACCESS
1266c 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29 3b 0a _EXISTS, &res);.
1266d 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 }. zMaster =
1266e 30 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 0;. if( rc!=SQL
1266f 49 54 45 5f 4f 4b 20 7c 7c 20 21 72 65 73 20 29 ITE_OK || !res )
12670 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 {. goto end_p
12671 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a 20 20 70 layback;. }. p
12672 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
12673 66 20 3d 20 30 3b 0a 20 20 6e 65 65 64 50 61 67 f = 0;. needPag
12674 65 72 52 65 73 65 74 20 3d 20 69 73 48 6f 74 3b erReset = isHot;
12675 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 .. /* This loop
12676 20 74 65 72 6d 69 6e 61 74 65 73 20 65 69 74 68 terminates eith
12677 65 72 20 77 68 65 6e 20 61 20 72 65 61 64 4a 6f er when a readJo
12678 75 72 6e 61 6c 48 64 72 28 29 20 6f 72 20 0a 20 urnalHdr() or .
12679 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 61 ** pager_playba
1267a 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20 63 61 ck_one_page() ca
1267b 6c 6c 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 ll returns SQLIT
1267c 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 49 4f 20 E_DONE or an IO
1267d 65 72 72 6f 72 20 0a 20 20 2a 2a 20 6f 63 63 75 error . ** occu
1267e 72 73 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c rs. . */. whil
1267f 65 28 20 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 e( 1 ){. int
12680 69 73 55 6e 73 79 6e 63 20 3d 20 30 3b 0a 0a 20 isUnsync = 0;..
12681 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6e /* Read the n
12682 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 ext journal head
12683 65 72 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 er from the jour
12684 6e 61 6c 20 66 69 6c 65 2e 20 20 49 66 20 74 68 nal file. If th
12685 65 72 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e ere are. ** n
12686 6f 74 20 65 6e 6f 75 67 68 20 62 79 74 65 73 20 ot enough bytes
12687 6c 65 66 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 left in the jour
12688 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 61 20 63 nal file for a c
12689 6f 6d 70 6c 65 74 65 20 68 65 61 64 65 72 2c 20 omplete header,
1268a 6f 72 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 or. ** it is
1268b 63 6f 72 72 75 70 74 65 64 2c 20 74 68 65 6e 20 corrupted, then
1268c 61 20 70 72 6f 63 65 73 73 20 6d 75 73 74 20 6f a process must o
1268d 66 20 66 61 69 6c 65 64 20 77 68 69 6c 65 20 77 f failed while w
1268e 72 69 74 69 6e 67 20 69 74 2e 0a 20 20 20 20 2a riting it.. *
1268f 2a 20 54 68 69 73 20 69 6e 64 69 63 61 74 65 73 * This indicates
12690 20 6e 6f 74 68 69 6e 67 20 6d 6f 72 65 20 6e 65 nothing more ne
12691 65 64 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 eds to be rolled
12692 20 62 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 back.. */.
12693 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e rc = readJourn
12694 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 69 73 alHdr(pPager, is
12695 48 6f 74 2c 20 73 7a 4a 2c 20 26 6e 52 65 63 2c Hot, szJ, &nRec,
12696 20 26 6d 78 50 67 29 3b 0a 20 20 20 20 69 66 28 &mxPg);. if(
12697 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
12698 7b 20 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d { . if( rc=
12699 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a =SQLITE_DONE ){.
1269a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
1269b 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a ITE_OK;. }.
1269c 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 goto end_p
1269d 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 7d 0a 0a layback;. }..
1269e 20 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 /* If nRec i
1269f 73 20 30 78 66 66 66 66 66 66 66 66 2c 20 74 68 s 0xffffffff, th
126a0 65 6e 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 en this journal
126a1 77 61 73 20 63 72 65 61 74 65 64 20 62 79 20 61 was created by a
126a2 20 70 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a 20 process. **
126a3 77 6f 72 6b 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 working in no-sy
126a4 6e 63 20 6d 6f 64 65 2e 20 54 68 69 73 20 6d 65 nc mode. This me
126a5 61 6e 73 20 74 68 61 74 20 74 68 65 20 72 65 73 ans that the res
126a6 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c t of the journal
126a7 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 6f 6e . ** file con
126a8 73 69 73 74 73 20 6f 66 20 70 61 67 65 73 2c 20 sists of pages,
126a9 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 there are no mor
126aa 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
126ab 73 2e 20 43 6f 6d 70 75 74 65 0a 20 20 20 20 2a s. Compute. *
126ac 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e * the value of n
126ad 52 65 63 20 62 61 73 65 64 20 6f 6e 20 74 68 69 Rec based on thi
126ae 73 20 61 73 73 75 6d 70 74 69 6f 6e 2e 0a 20 20 s assumption..
126af 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 65 */. if( nRe
126b0 63 3d 3d 30 78 66 66 66 66 66 66 66 66 20 29 7b c==0xffffffff ){
126b1 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
126b2 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
126b3 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 f==JOURNAL_HDR_S
126b4 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 Z(pPager) );.
126b5 20 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 nRec = (int)(
126b6 28 73 7a 4a 20 2d 20 4a 4f 55 52 4e 41 4c 5f 48 (szJ - JOURNAL_H
126b7 44 52 5f 53 5a 28 70 50 61 67 65 72 29 29 2f 4a DR_SZ(pPager))/J
126b8 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 OURNAL_PG_SZ(pPa
126b9 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 ger));. }..
126ba 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 20 /* If nRec is
126bb 30 20 61 6e 64 20 74 68 69 73 20 72 6f 6c 6c 62 0 and this rollb
126bc 61 63 6b 20 69 73 20 6f 66 20 61 20 74 72 61 6e ack is of a tran
126bd 73 61 63 74 69 6f 6e 20 63 72 65 61 74 65 64 20 saction created
126be 62 79 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 70 by this. ** p
126bf 72 6f 63 65 73 73 20 61 6e 64 20 69 66 20 74 68 rocess and if th
126c0 69 73 20 69 73 20 74 68 65 20 66 69 6e 61 6c 20 is is the final
126c1 68 65 61 64 65 72 20 69 6e 20 74 68 65 20 6a 6f header in the jo
126c2 75 72 6e 61 6c 2c 20 74 68 65 6e 20 69 74 20 6d urnal, then it m
126c3 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 eans. ** that
126c4 20 74 68 69 73 20 70 61 72 74 20 6f 66 20 74 68 this part of th
126c5 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 e journal was be
126c6 69 6e 67 20 66 69 6c 6c 65 64 20 62 75 74 20 68 ing filled but h
126c7 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 0a as not yet been.
126c8 20 20 20 20 2a 2a 20 73 79 6e 63 65 64 20 74 6f ** synced to
126c9 20 64 69 73 6b 2e 20 20 43 6f 6d 70 75 74 65 20 disk. Compute
126ca 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
126cb 67 65 73 20 62 61 73 65 64 20 6f 6e 20 74 68 65 ges based on the
126cc 20 72 65 6d 61 69 6e 69 6e 67 0a 20 20 20 20 2a remaining. *
126cd 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 * size of the fi
126ce 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a le.. **. *
126cf 2a 20 54 68 65 20 74 68 69 72 64 20 74 65 72 6d * The third term
126d0 20 6f 66 20 74 68 65 20 74 65 73 74 20 77 61 73 of the test was
126d1 20 61 64 64 65 64 20 74 6f 20 66 69 78 20 74 69 added to fix ti
126d2 63 6b 65 74 20 23 32 35 36 35 2e 0a 20 20 20 20 cket #2565..
126d3 2a 2a 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 ** When rolling
126d4 62 61 63 6b 20 61 20 68 6f 74 20 6a 6f 75 72 6e back a hot journ
126d5 61 6c 2c 20 6e 52 65 63 3d 3d 30 20 61 6c 77 61 al, nRec==0 alwa
126d6 79 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 ys means that th
126d7 65 20 6e 65 78 74 0a 20 20 20 20 2a 2a 20 63 68 e next. ** ch
126d8 75 6e 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e unk of the journ
126d9 61 6c 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72 6f al contains zero
126da 20 70 61 67 65 73 20 74 6f 20 62 65 20 72 6f 6c pages to be rol
126db 6c 65 64 20 62 61 63 6b 2e 20 20 42 75 74 0a 20 led back. But.
126dc 20 20 20 2a 2a 20 77 68 65 6e 20 64 6f 69 6e 67 ** when doing
126dd 20 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64 20 a ROLLBACK and
126de 74 68 65 20 6e 52 65 63 3d 3d 30 20 63 68 75 6e the nRec==0 chun
126df 6b 20 69 73 20 74 68 65 20 6c 61 73 74 20 63 68 k is the last ch
126e0 75 6e 6b 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 unk in. ** th
126e1 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 6d 65 e journal, it me
126e2 61 6e 73 20 74 68 61 74 20 74 68 65 20 6a 6f 75 ans that the jou
126e3 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 rnal might conta
126e4 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 20 20 in additional.
126e5 20 20 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20 ** pages that
126e6 6e 65 65 64 20 74 6f 20 62 65 20 72 6f 6c 6c 65 need to be rolle
126e7 64 20 62 61 63 6b 20 61 6e 64 20 74 68 61 74 20 d back and that
126e8 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
126e9 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 68 6f 75 ges . ** shou
126ea 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 20 62 ld be computed b
126eb 61 73 65 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 ased on the jour
126ec 6e 61 6c 20 66 69 6c 65 20 73 69 7a 65 2e 0a 20 nal file size..
126ed 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 */. if( nR
126ee 65 63 3d 3d 30 20 26 26 20 21 69 73 48 6f 74 20 ec==0 && !isHot
126ef 26 26 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 &&. pPage
126f0 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f r->journalHdr+JO
126f1 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
126f2 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f ger)==pPager->jo
126f3 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 urnalOff ){.
126f4 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 28 nRec = (int)((
126f5 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f szJ - pPager->jo
126f6 75 72 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 urnalOff) / JOUR
126f7 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 NAL_PG_SZ(pPager
126f8 29 29 3b 0a 20 20 20 20 20 20 69 73 55 6e 73 79 ));. isUnsy
126f9 6e 63 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 nc = 1;. }..
126fa 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 /* If this is
126fb 20 74 68 65 20 66 69 72 73 74 20 68 65 61 64 65 the first heade
126fc 72 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 r read from the
126fd 6a 6f 75 72 6e 61 6c 2c 20 74 72 75 6e 63 61 74 journal, truncat
126fe 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 e the. ** dat
126ff 61 62 61 73 65 20 66 69 6c 65 20 62 61 63 6b 20 abase file back
12700 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 to its original
12701 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 size.. */.
12702 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 if( pPager->jou
12703 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c rnalOff==JOURNAL
12704 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 _HDR_SZ(pPager)
12705 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 ){. rc = pa
12706 67 65 72 5f 74 72 75 6e 63 61 74 65 28 70 50 61 ger_truncate(pPa
12707 67 65 72 2c 20 6d 78 50 67 29 3b 0a 20 20 20 20 ger, mxPg);.
12708 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
12709 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 _OK ){. g
1270a 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b oto end_playback
1270b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1270c 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d pPager->dbSize =
1270d 20 6d 78 50 67 3b 0a 20 20 20 20 7d 0a 0a 20 20 mxPg;. }..
1270e 20 20 2f 2a 20 43 6f 70 79 20 6f 72 69 67 69 6e /* Copy origin
1270f 61 6c 20 70 61 67 65 73 20 6f 75 74 20 6f 66 20 al pages out of
12710 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 the journal and
12711 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 0a 20 back into the .
12712 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 ** database f
12713 69 6c 65 20 61 6e 64 2f 6f 72 20 70 61 67 65 20 ile and/or page
12714 63 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 cache.. */.
12715 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6e 52 65 for(u=0; u<nRe
12716 63 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 20 20 69 c; u++){. i
12717 66 28 20 6e 65 65 64 50 61 67 65 72 52 65 73 65 f( needPagerRese
12718 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 61 67 t ){. pag
12719 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 er_reset(pPager)
1271a 3b 0a 20 20 20 20 20 20 20 20 6e 65 65 64 50 61 ;. needPa
1271b 67 65 72 52 65 73 65 74 20 3d 20 30 3b 0a 20 20 gerReset = 0;.
1271c 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d }. rc =
1271d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f pager_playback_
1271e 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c one_page(pPager,
1271f 31 2c 69 73 55 6e 73 79 6e 63 2c 26 70 50 61 67 1,isUnsync,&pPag
12720 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 30 er->journalOff,0
12721 2c 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ,0);. if( r
12722 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
12723 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
12724 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 SQLITE_DONE ){.
12725 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 rc = SQ
12726 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 LITE_OK;.
12727 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
12728 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 20 alOff = szJ;.
12729 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1272a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1272b 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 /* If we
1272c 61 72 65 20 75 6e 61 62 6c 65 20 74 6f 20 72 6f are unable to ro
1272d 6c 6c 62 61 63 6b 2c 20 71 75 69 74 20 61 6e 64 llback, quit and
1272e 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f return the erro
1272f 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 r. ** c
12730 6f 64 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 ode. This will
12731 63 61 75 73 65 20 74 68 65 20 70 61 67 65 72 20 cause the pager
12732 74 6f 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 to enter the err
12733 6f 72 20 73 74 61 74 65 0a 20 20 20 20 20 20 20 or state.
12734 20 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 6e 6f ** so that no
12735 20 66 75 72 74 68 65 72 20 68 61 72 6d 20 77 69 further harm wi
12736 6c 6c 20 62 65 20 64 6f 6e 65 2e 20 20 50 65 72 ll be done. Per
12737 68 61 70 73 20 74 68 65 20 6e 65 78 74 0a 20 20 haps the next.
12738 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 ** proce
12739 73 73 20 74 6f 20 63 6f 6d 65 20 61 6c 6f 6e 67 ss to come along
1273a 20 77 69 6c 6c 20 62 65 20 61 62 6c 65 20 74 6f will be able to
1273b 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 64 61 rollback the da
1273c 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20 tabase..
1273d 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 67 */. g
1273e 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b oto end_playback
1273f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
12740 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
12741 2f 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20 /*NOTREACHED*/.
12742 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 0a 65 assert( 0 );..e
12743 6e 64 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20 2f nd_playback:. /
12744 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20 61 20 72 6f * Following a ro
12745 6c 6c 62 61 63 6b 2c 20 74 68 65 20 64 61 74 61 llback, the data
12746 62 61 73 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 base file should
12747 20 62 65 20 62 61 63 6b 20 69 6e 20 69 74 73 20 be back in its
12748 6f 72 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 73 74 original. ** st
12749 61 74 65 20 70 72 69 6f 72 20 74 6f 20 74 68 65 ate prior to the
1274a 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 start of the tr
1274b 61 6e 73 61 63 74 69 6f 6e 2c 20 73 6f 20 69 6e ansaction, so in
1274c 76 6f 6b 65 20 74 68 65 0a 20 20 2a 2a 20 53 51 voke the. ** SQ
1274d 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e LITE_FCNTL_DB_UN
1274e 43 48 41 4e 47 45 44 20 66 69 6c 65 2d 63 6f 6e CHANGED file-con
1274f 74 72 6f 6c 20 6d 65 74 68 6f 64 20 74 6f 20 64 trol method to d
12750 69 73 61 62 6c 65 20 74 68 65 0a 20 20 2a 2a 20 isable the. **
12751 61 73 73 65 72 74 69 6f 6e 20 74 68 61 74 20 74 assertion that t
12752 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 he transaction c
12753 6f 75 6e 74 65 72 20 77 61 73 20 6d 6f 64 69 66 ounter was modif
12754 69 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 ied.. */. asse
12755 72 74 28 0a 20 20 20 20 70 50 61 67 65 72 2d 3e rt(. pPager->
12756 66 64 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 30 20 fd->pMethods==0
12757 7c 7c 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 ||. sqlite3Os
12758 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 50 61 67 FileControl(pPag
12759 65 72 2d 3e 66 64 2c 53 51 4c 49 54 45 5f 46 43 er->fd,SQLITE_FC
1275a 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 NTL_DB_UNCHANGED
1275b 2c 30 29 3e 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 ,0)>=SQLITE_OK.
1275c 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 );.. /* If thi
1275d 73 20 70 6c 61 79 62 61 63 6b 20 69 73 20 68 61 s playback is ha
1275e 70 70 65 6e 69 6e 67 20 61 75 74 6f 6d 61 74 69 ppening automati
1275f 63 61 6c 6c 79 20 61 73 20 61 20 72 65 73 75 6c cally as a resul
12760 74 20 6f 66 20 61 6e 20 49 4f 20 6f 72 20 0a 20 t of an IO or .
12761 20 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 ** malloc error
12762 20 74 68 61 74 20 6f 63 63 75 72 72 65 64 20 61 that occurred a
12763 66 74 65 72 20 74 68 65 20 63 68 61 6e 67 65 2d fter the change-
12764 63 6f 75 6e 74 65 72 20 77 61 73 20 75 70 64 61 counter was upda
12765 74 65 64 20 62 75 74 20 0a 20 20 2a 2a 20 62 65 ted but . ** be
12766 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 fore the transac
12767 74 69 6f 6e 20 77 61 73 20 63 6f 6d 6d 69 74 74 tion was committ
12768 65 64 2c 20 74 68 65 6e 20 74 68 65 20 63 68 61 ed, then the cha
12769 6e 67 65 2d 63 6f 75 6e 74 65 72 20 0a 20 20 2a nge-counter . *
1276a 2a 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 6d * modification m
1276b 61 79 20 6a 75 73 74 20 68 61 76 65 20 62 65 65 ay just have bee
1276c 6e 20 72 65 76 65 72 74 65 64 2e 20 49 66 20 74 n reverted. If t
1276d 68 69 73 20 68 61 70 70 65 6e 73 20 69 6e 20 65 his happens in e
1276e 78 63 6c 75 73 69 76 65 20 0a 20 20 2a 2a 20 6d xclusive . ** m
1276f 6f 64 65 2c 20 74 68 65 6e 20 73 75 62 73 65 71 ode, then subseq
12770 75 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e uent transaction
12771 73 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 s performed by t
12772 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 he connection wi
12773 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 75 70 64 61 ll not. ** upda
12774 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f te the change-co
12775 75 6e 74 65 72 20 61 74 20 61 6c 6c 2e 20 54 68 unter at all. Th
12776 69 73 20 6d 61 79 20 6c 65 61 64 20 74 6f 20 63 is may lead to c
12777 61 63 68 65 20 69 6e 63 6f 6e 73 69 73 74 65 6e ache inconsisten
12778 63 79 0a 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 73 cy. ** problems
12779 20 66 6f 72 20 6f 74 68 65 72 20 70 72 6f 63 65 for other proce
1277a 73 73 65 73 20 61 74 20 73 6f 6d 65 20 70 6f 69 sses at some poi
1277b 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 nt in the future
1277c 2e 20 53 6f 2c 20 6a 75 73 74 0a 20 20 2a 2a 20 . So, just. **
1277d 69 6e 20 63 61 73 65 20 74 68 69 73 20 68 61 73 in case this has
1277e 20 68 61 70 70 65 6e 65 64 2c 20 63 6c 65 61 72 happened, clear
1277f 20 74 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 the changeCount
12780 44 6f 6e 65 20 66 6c 61 67 20 6e 6f 77 2e 0a 20 Done flag now..
12781 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 63 68 */. pPager->ch
12782 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 angeCountDone =
12783 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
12784 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c ;.. if( rc==SQL
12785 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 7a 4d ITE_OK ){. zM
12786 61 73 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e aster = pPager->
12787 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 20 20 72 pTmpSpace;. r
12788 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f c = readMasterJo
12789 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 urnal(pPager->jf
1278a 64 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 d, zMaster, pPag
1278b 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 er->pVfs->mxPath
1278c 6e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 74 65 73 name+1);. tes
1278d 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 tcase( rc!=SQLIT
1278e 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 66 E_OK );. }. if
1278f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
12790 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 ){. rc = page
12791 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f r_end_transactio
12792 6e 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 n(pPager, zMaste
12793 72 5b 30 5d 21 3d 27 5c 30 27 29 3b 0a 20 20 20 r[0]!='\0');.
12794 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 testcase( rc!=S
12795 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a QLITE_OK );. }.
12796 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
12797 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 _OK && zMaster[0
12798 5d 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 ] && res ){.
12799 2f 2a 20 49 66 20 74 68 65 72 65 20 77 61 73 20 /* If there was
1279a 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
1279b 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e and this routin
1279c 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 73 75 e will return su
1279d 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a 20 73 65 ccess,. ** se
1279e 65 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 e if it is possi
1279f 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 ble to delete th
127a0 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
127a1 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
127a2 3d 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 65 = pager_delmaste
127a3 72 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 r(pPager, zMaste
127a4 72 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 r);. testcase
127a5 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
127a6 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 );. }.. /* The
127a7 20 50 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a Pager.sectorSiz
127a8 65 20 76 61 72 69 61 62 6c 65 20 6d 61 79 20 68 e variable may h
127a9 61 76 65 20 62 65 65 6e 20 75 70 64 61 74 65 64 ave been updated
127aa 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 0a 20 while rolling.
127ab 20 2a 2a 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e ** back a journ
127ac 61 6c 20 63 72 65 61 74 65 64 20 62 79 20 61 20 al created by a
127ad 70 72 6f 63 65 73 73 20 77 69 74 68 20 61 20 64 process with a d
127ae 69 66 66 65 72 65 6e 74 20 73 65 63 74 6f 72 20 ifferent sector
127af 73 69 7a 65 0a 20 20 2a 2a 20 76 61 6c 75 65 2e size. ** value.
127b0 20 52 65 73 65 74 20 69 74 20 74 6f 20 74 68 65 Reset it to the
127b1 20 63 6f 72 72 65 63 74 20 76 61 6c 75 65 20 66 correct value f
127b2 6f 72 20 74 68 69 73 20 70 72 6f 63 65 73 73 2e or this process.
127b3 0a 20 20 2a 2f 0a 20 20 73 65 74 53 65 63 74 6f . */. setSecto
127b4 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 rSize(pPager);.
127b5 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
127b6 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b 20 73 61 *.** Playback sa
127b7 76 65 70 6f 69 6e 74 20 70 53 61 76 65 70 6f 69 vepoint pSavepoi
127b8 6e 74 2e 20 4f 72 2c 20 69 66 20 70 53 61 76 65 nt. Or, if pSave
127b9 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 2c 20 74 68 65 point==NULL, the
127ba 6e 20 70 6c 61 79 62 61 63 6b 0a 2a 2a 20 74 68 n playback.** th
127bb 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72 20 e entire master
127bc 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 journal file. Th
127bd 65 20 63 61 73 65 20 70 53 61 76 65 70 6f 69 6e e case pSavepoin
127be 74 3d 3d 4e 55 4c 4c 20 6f 63 63 75 72 73 20 77 t==NULL occurs w
127bf 68 65 6e 20 0a 2a 2a 20 61 20 52 4f 4c 4c 42 41 hen .** a ROLLBA
127c0 43 4b 20 54 4f 20 63 6f 6d 6d 61 6e 64 20 69 73 CK TO command is
127c1 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 61 20 53 41 invoked on a SA
127c2 56 45 50 4f 49 4e 54 20 74 68 61 74 20 69 73 20 VEPOINT that is
127c3 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a a transaction .*
127c4 2a 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a * savepoint..**.
127c5 2a 2a 20 57 68 65 6e 20 70 53 61 76 65 70 6f 69 ** When pSavepoi
127c6 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 28 nt is not NULL (
127c7 6d 65 61 6e 69 6e 67 20 61 20 6e 6f 6e 2d 74 72 meaning a non-tr
127c8 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f ansaction savepo
127c9 69 6e 74 20 69 73 20 0a 2a 2a 20 62 65 69 6e 67 int is .** being
127ca 20 72 6f 6c 6c 65 64 20 62 61 63 6b 29 2c 20 74 rolled back), t
127cb 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b hen the rollback
127cc 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 75 70 20 consists of up
127cd 74 6f 20 74 68 72 65 65 20 73 74 61 67 65 73 2c to three stages,
127ce 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 69 6e .** performed in
127cf 20 74 68 65 20 6f 72 64 65 72 20 73 70 65 63 69 the order speci
127d0 66 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 fied:.**.** *
127d1 50 61 67 65 73 20 61 72 65 20 70 6c 61 79 65 64 Pages are played
127d2 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d back from the m
127d3 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 61 72 ain journal star
127d4 74 69 6e 67 20 61 74 20 62 79 74 65 0a 2a 2a 20 ting at byte.**
127d5 20 20 20 20 6f 66 66 73 65 74 20 50 61 67 65 72 offset Pager
127d6 53 61 76 65 70 6f 69 6e 74 2e 69 4f 66 66 73 65 Savepoint.iOffse
127d7 74 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 t and continuing
127d8 20 74 6f 20 0a 2a 2a 20 20 20 20 20 50 61 67 65 to .** Page
127d9 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f rSavepoint.iHdrO
127da 66 66 73 65 74 2c 20 6f 72 20 74 6f 20 74 68 65 ffset, or to the
127db 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 69 6e end of the main
127dc 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 journal.**
127dd 66 69 6c 65 20 69 66 20 50 61 67 65 72 53 61 76 file if PagerSav
127de 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 epoint.iHdrOffse
127df 74 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a t is zero..**.**
127e0 20 20 20 2a 20 49 66 20 50 61 67 65 72 53 61 76 * If PagerSav
127e1 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 epoint.iHdrOffse
127e2 74 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 t is not zero, t
127e3 68 65 6e 20 70 61 67 65 73 20 61 72 65 20 70 6c hen pages are pl
127e4 61 79 65 64 0a 2a 2a 20 20 20 20 20 62 61 63 6b ayed.** back
127e5 20 73 74 61 72 74 69 6e 67 20 66 72 6f 6d 20 74 starting from t
127e6 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 he journal heade
127e7 72 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f r immediately fo
127e8 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 20 20 20 20 llowing .**
127e9 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 PagerSavepoint.i
127ea 48 64 72 4f 66 66 73 65 74 20 74 6f 20 74 68 65 HdrOffset to the
127eb 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 69 6e end of the main
127ec 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a journal file..*
127ed 2a 0a 2a 2a 20 20 20 2a 20 50 61 67 65 73 20 61 *.** * Pages a
127ee 72 65 20 74 68 65 6e 20 70 6c 61 79 65 64 20 62 re then played b
127ef 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 73 75 62 ack from the sub
127f0 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 73 -journal file, s
127f1 74 61 72 74 69 6e 67 0a 2a 2a 20 20 20 20 20 77 tarting.** w
127f2 69 74 68 20 74 68 65 20 50 61 67 65 72 53 61 76 ith the PagerSav
127f3 65 70 6f 69 6e 74 2e 69 53 75 62 52 65 63 20 61 epoint.iSubRec a
127f4 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f nd continuing to
127f5 20 74 68 65 20 65 6e 64 20 6f 66 0a 2a 2a 20 20 the end of.**
127f6 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 the journal f
127f7 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 6f 75 ile..**.** Throu
127f8 67 68 6f 75 74 20 74 68 65 20 72 6f 6c 6c 62 61 ghout the rollba
127f9 63 6b 20 70 72 6f 63 65 73 73 2c 20 65 61 63 68 ck process, each
127fa 20 74 69 6d 65 20 61 20 70 61 67 65 20 69 73 20 time a page is
127fb 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 65 rolled back, the
127fc 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e .** correspondin
127fd 67 20 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 g bit is set in
127fe 61 20 62 69 74 76 65 63 20 73 74 72 75 63 74 75 a bitvec structu
127ff 72 65 20 28 76 61 72 69 61 62 6c 65 20 70 44 6f re (variable pDo
12800 6e 65 20 69 6e 20 74 68 65 0a 2a 2a 20 69 6d 70 ne in the.** imp
12801 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 62 65 6c 6f lementation belo
12802 77 29 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 w). This is used
12803 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 to ensure that
12804 61 20 70 61 67 65 20 69 73 20 6f 6e 6c 79 0a 2a a page is only.*
12805 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 * rolled back th
12806 65 20 66 69 72 73 74 20 74 69 6d 65 20 69 74 20 e first time it
12807 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 69 is encountered i
12808 6e 20 65 69 74 68 65 72 20 6a 6f 75 72 6e 61 6c n either journal
12809 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 53 61 76 65 ..**.** If pSave
1280a 70 6f 69 6e 74 20 69 73 20 4e 55 4c 4c 2c 20 74 point is NULL, t
1280b 68 65 6e 20 70 61 67 65 73 20 61 72 65 20 6f 6e hen pages are on
1280c 6c 79 20 70 6c 61 79 65 64 20 62 61 63 6b 20 66 ly played back f
1280d 72 6f 6d 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 rom the main.**
1280e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 journal file. Th
1280f 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 ere is no need f
12810 6f 72 20 61 20 62 69 74 76 65 63 20 69 6e 20 74 or a bitvec in t
12811 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 his case..**.**
12812 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c 20 In either case,
12813 62 65 66 6f 72 65 20 70 6c 61 79 62 61 63 6b 20 before playback
12814 63 6f 6d 6d 65 6e 63 65 73 20 74 68 65 20 50 61 commences the Pa
12815 67 65 72 2e 64 62 53 69 7a 65 20 76 61 72 69 61 ger.dbSize varia
12816 62 6c 65 0a 2a 2a 20 69 73 20 72 65 73 65 74 20 ble.** is reset
12817 74 6f 20 74 68 65 20 76 61 6c 75 65 20 74 68 61 to the value tha
12818 74 20 69 74 20 68 65 6c 64 20 61 74 20 74 68 65 t it held at the
12819 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 73 61 start of the sa
1281a 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 28 6f 72 20 vepoint .** (or
1281b 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 4e 6f transaction). No
1281c 20 70 61 67 65 20 77 69 74 68 20 61 20 70 61 67 page with a pag
1281d 65 2d 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 e-number greater
1281e 20 74 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65 than this value
1281f 0a 2a 2a 20 69 73 20 70 6c 61 79 65 64 20 62 61 .** is played ba
12820 63 6b 2e 20 49 66 20 6f 6e 65 20 69 73 20 65 6e ck. If one is en
12821 63 6f 75 6e 74 65 72 65 64 20 69 74 20 69 73 20 countered it is
12822 73 69 6d 70 6c 79 20 73 6b 69 70 70 65 64 2e 0a simply skipped..
12823 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
12824 67 65 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 gerPlaybackSavep
12825 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 oint(Pager *pPag
12826 65 72 2c 20 50 61 67 65 72 53 61 76 65 70 6f 69 er, PagerSavepoi
12827 6e 74 20 2a 70 53 61 76 65 70 6f 69 6e 74 29 7b nt *pSavepoint){
12828 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 . i64 szJ;
12829 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
1282a 66 66 65 63 74 69 76 65 20 73 69 7a 65 20 6f 66 ffective size of
1282b 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 the main journa
1282c 6c 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f l */. i64 iHdrO
1282d 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ff;
1282e 2f 2a 20 45 6e 64 20 6f 66 20 66 69 72 73 74 20 /* End of first
1282f 73 65 67 6d 65 6e 74 20 6f 66 20 6d 61 69 6e 2d segment of main-
12830 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 73 20 journal records
12831 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 */. int rc = SQ
12832 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a LITE_OK; /*
12833 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
12834 20 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 Bitvec *pDone
12835 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 42 69 = 0; /* Bi
12836 74 76 65 63 20 74 6f 20 65 6e 73 75 72 65 20 70 tvec to ensure p
12837 61 67 65 73 20 70 6c 61 79 65 64 20 62 61 63 6b ages played back
12838 20 6f 6e 6c 79 20 6f 6e 63 65 20 2a 2f 0a 0a 20 only once */..
12839 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
1283a 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 >state>=PAGER_SH
1283b 41 52 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c ARED );.. /* Al
1283c 6c 6f 63 61 74 65 20 61 20 62 69 74 76 65 63 20 locate a bitvec
1283d 74 6f 20 75 73 65 20 74 6f 20 73 74 6f 72 65 20 to use to store
1283e 74 68 65 20 73 65 74 20 6f 66 20 70 61 67 65 73 the set of pages
1283f 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 2a 2f 0a rolled back */.
12840 20 20 69 66 28 20 70 53 61 76 65 70 6f 69 6e 74 if( pSavepoint
12841 20 29 7b 0a 20 20 20 20 70 44 6f 6e 65 20 3d 20 ){. pDone =
12842 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 sqlite3BitvecCre
12843 61 74 65 28 70 53 61 76 65 70 6f 69 6e 74 2d 3e ate(pSavepoint->
12844 6e 4f 72 69 67 29 3b 0a 20 20 20 20 69 66 28 20 nOrig);. if(
12845 21 70 44 6f 6e 65 20 29 7b 0a 20 20 20 20 20 20 !pDone ){.
12846 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
12847 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a MEM;. }. }..
12848 20 20 2f 2a 20 53 65 74 20 74 68 65 20 64 61 74 /* Set the dat
12849 61 62 61 73 65 20 73 69 7a 65 20 62 61 63 6b 20 abase size back
1284a 74 6f 20 74 68 65 20 76 61 6c 75 65 20 69 74 20 to the value it
1284b 77 61 73 20 62 65 66 6f 72 65 20 74 68 65 20 73 was before the s
1284c 61 76 65 70 6f 69 6e 74 20 0a 20 20 2a 2a 20 62 avepoint . ** b
1284d 65 69 6e 67 20 72 65 76 65 72 74 65 64 20 77 61 eing reverted wa
1284e 73 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 s opened.. */.
1284f 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 pPager->dbSize
12850 3d 20 70 53 61 76 65 70 6f 69 6e 74 20 3f 20 70 = pSavepoint ? p
12851 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67 Savepoint->nOrig
12852 20 3a 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 : pPager->dbOri
12853 67 53 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 73 65 gSize;.. /* Use
12854 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12855 4f 66 66 20 61 73 20 74 68 65 20 65 66 66 65 63 Off as the effec
12856 74 69 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65 tive size of the
12857 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a 20 main rollback.
12858 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 ** journal. Th
12859 65 20 61 63 74 75 61 6c 20 66 69 6c 65 20 6d 69 e actual file mi
1285a 67 68 74 20 62 65 20 6c 61 72 67 65 72 20 74 68 ght be larger th
1285b 61 6e 20 74 68 69 73 20 69 6e 0a 20 20 2a 2a 20 an this in. **
1285c 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
1285d 45 5f 54 52 55 4e 43 41 54 45 20 6f 72 20 50 41 E_TRUNCATE or PA
1285e 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
1285f 50 45 52 53 49 53 54 2e 20 20 42 75 74 20 61 6e PERSIST. But an
12860 79 74 68 69 6e 67 0a 20 20 2a 2a 20 70 61 73 74 ything. ** past
12861 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12862 4f 66 66 20 69 73 20 6f 66 66 2d 6c 69 6d 69 74 Off is off-limit
12863 73 20 74 6f 20 75 73 2e 0a 20 20 2a 2f 0a 20 20 s to us.. */.
12864 73 7a 4a 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f szJ = pPager->jo
12865 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 urnalOff;.. /*
12866 42 65 67 69 6e 20 62 79 20 72 6f 6c 6c 69 6e 67 Begin by rolling
12867 20 62 61 63 6b 20 72 65 63 6f 72 64 73 20 66 72 back records fr
12868 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 om the main jour
12869 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a nal starting at.
1286a 20 20 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f ** PagerSavepo
1286b 69 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 20 int.iOffset and
1286c 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 continuing to th
1286d 65 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 e next journal h
1286e 65 61 64 65 72 2e 0a 20 20 2a 2a 20 54 68 65 72 eader.. ** Ther
1286f 65 20 6d 69 67 68 74 20 62 65 20 72 65 63 6f 72 e might be recor
12870 64 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a ds in the main j
12871 6f 75 72 6e 61 6c 20 74 68 61 74 20 68 61 76 65 ournal that have
12872 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 a page number.
12873 20 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e ** greater than
12874 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 the current dat
12875 61 62 61 73 65 20 73 69 7a 65 20 28 70 50 61 67 abase size (pPag
12876 65 72 2d 3e 64 62 53 69 7a 65 29 20 62 75 74 20 er->dbSize) but
12877 74 68 6f 73 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 those. ** will
12878 62 65 20 73 6b 69 70 70 65 64 20 61 75 74 6f 6d be skipped autom
12879 61 74 69 63 61 6c 6c 79 2e 20 20 50 61 67 65 73 atically. Pages
1287a 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 70 44 are added to pD
1287b 6f 6e 65 20 61 73 20 74 68 65 79 0a 20 20 2a 2a one as they. **
1287c 20 61 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b are played back
1287d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 .. */. if( pSa
1287e 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69 vepoint ){. i
1287f 48 64 72 4f 66 66 20 3d 20 70 53 61 76 65 70 6f HdrOff = pSavepo
12880 69 6e 74 2d 3e 69 48 64 72 4f 66 66 73 65 74 20 int->iHdrOffset
12881 3f 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 48 ? pSavepoint->iH
12882 64 72 4f 66 66 73 65 74 20 3a 20 73 7a 4a 3b 0a drOffset : szJ;.
12883 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
12884 6e 61 6c 4f 66 66 20 3d 20 70 53 61 76 65 70 6f nalOff = pSavepo
12885 69 6e 74 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 int->iOffset;.
12886 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c while( rc==SQL
12887 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 ITE_OK && pPager
12888 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 69 48 64 ->journalOff<iHd
12889 72 4f 66 66 20 29 7b 0a 20 20 20 20 20 20 72 63 rOff ){. rc
1288a 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 = pager_playbac
1288b 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 k_one_page(pPage
1288c 72 2c 20 31 2c 20 30 2c 20 26 70 50 61 67 65 72 r, 1, 0, &pPager
1288d 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 2c ->journalOff, 1,
1288e 20 70 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 pDone);. }.
1288f 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 assert( rc!=S
12890 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 QLITE_DONE );.
12891 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 }else{. pPage
12892 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 r->journalOff =
12893 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 0;. }.. /* Con
12894 74 69 6e 75 65 20 72 6f 6c 6c 69 6e 67 20 62 61 tinue rolling ba
12895 63 6b 20 72 65 63 6f 72 64 73 20 6f 75 74 20 6f ck records out o
12896 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e f the main journ
12897 61 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a 20 al starting at.
12898 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 6a 6f ** the first jo
12899 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 65 65 urnal header see
1289a 6e 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 n and continuing
1289b 20 75 6e 74 69 6c 20 74 68 65 20 65 66 66 65 63 until the effec
1289c 74 69 76 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 tive end. ** of
1289d 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 the main journa
1289e 6c 20 66 69 6c 65 2e 20 20 43 6f 6e 74 69 6e 75 l file. Continu
1289f 65 20 74 6f 20 73 6b 69 70 20 6f 75 74 2d 6f 66 e to skip out-of
128a0 2d 72 61 6e 67 65 20 70 61 67 65 73 20 61 6e 64 -range pages and
128a1 0a 20 20 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 61 . ** continue a
128a2 64 64 69 6e 67 20 70 61 67 65 73 20 72 6f 6c 6c dding pages roll
128a3 65 64 20 62 61 63 6b 20 74 6f 20 70 44 6f 6e 65 ed back to pDone
128a4 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 .. */. while(
128a5 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
128a6 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
128a7 4f 66 66 3c 73 7a 4a 20 29 7b 0a 20 20 20 20 75 Off<szJ ){. u
128a8 33 32 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 32 ii;
128a9 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
128aa 72 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 4a 52 r */. u32 nJR
128ab 65 63 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e ec = 0; /* N
128ac 75 6d 62 65 72 20 6f 66 20 4a 6f 75 72 6e 61 6c umber of Journal
128ad 20 52 65 63 6f 72 64 73 20 2a 2f 0a 20 20 20 20 Records */.
128ae 75 33 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 72 u32 dummy;. r
128af 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 c = readJournalH
128b0 64 72 28 70 50 61 67 65 72 2c 20 30 2c 20 73 7a dr(pPager, 0, sz
128b1 4a 2c 20 26 6e 4a 52 65 63 2c 20 26 64 75 6d 6d J, &nJRec, &dumm
128b2 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 y);. assert(
128b3 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 rc!=SQLITE_DONE
128b4 29 3b 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a );.. /*. *
128b5 2a 20 54 68 65 20 22 70 50 61 67 65 72 2d 3e 6a * The "pPager->j
128b6 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 ournalHdr+JOURNA
128b7 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 L_HDR_SZ(pPager)
128b8 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ==pPager->journa
128b9 6c 4f 66 66 22 0a 20 20 20 20 2a 2a 20 74 65 73 lOff". ** tes
128ba 74 20 69 73 20 72 65 6c 61 74 65 64 20 74 6f 20 t is related to
128bb 74 69 63 6b 65 74 20 23 32 35 36 35 2e 20 20 53 ticket #2565. S
128bc 65 65 20 74 68 65 20 64 69 73 63 75 73 73 69 6f ee the discussio
128bd 6e 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 n in the. **
128be 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 pager_playback()
128bf 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 64 function for ad
128c0 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
128c1 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tion.. */.
128c2 20 69 66 28 20 6e 4a 52 65 63 3d 3d 30 20 0a 20 if( nJRec==0 .
128c3 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a && pPager->j
128c4 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 ournalHdr+JOURNA
128c5 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 L_HDR_SZ(pPager)
128c6 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ==pPager->journa
128c7 6c 4f 66 66 0a 20 20 20 20 29 7b 0a 20 20 20 20 lOff. ){.
128c8 20 20 6e 4a 52 65 63 20 3d 20 28 75 33 32 29 28 nJRec = (u32)(
128c9 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a (szJ - pPager->j
128ca 6f 75 72 6e 61 6c 4f 66 66 29 2f 4a 4f 55 52 4e ournalOff)/JOURN
128cb 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 AL_PG_SZ(pPager)
128cc 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 );. }. for
128cd 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 (ii=0; rc==SQLIT
128ce 45 5f 4f 4b 20 26 26 20 69 69 3c 6e 4a 52 65 63 E_OK && ii<nJRec
128cf 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 && pPager->jour
128d0 6e 61 6c 4f 66 66 3c 73 7a 4a 3b 20 69 69 2b 2b nalOff<szJ; ii++
128d1 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 ){. rc = pa
128d2 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 ger_playback_one
128d3 5f 70 61 67 65 28 70 50 61 67 65 72 2c 20 31 2c _page(pPager, 1,
128d4 20 30 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 0, &pPager->jou
128d5 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 44 6f 6e rnalOff, 1, pDon
128d6 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 e);. }. as
128d7 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
128d8 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 20 20 61 _DONE );. }. a
128d9 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
128da 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e E_OK || pPager->
128db 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 73 7a 4a 20 journalOff==szJ
128dc 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 );.. /* Finally
128dd 2c 20 20 72 6f 6c 6c 62 61 63 6b 20 70 61 67 65 , rollback page
128de 73 20 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a s from the sub-j
128df 6f 75 72 6e 61 6c 2e 20 20 50 61 67 65 20 74 68 ournal. Page th
128e0 61 74 20 77 65 72 65 0a 20 20 2a 2a 20 70 72 65 at were. ** pre
128e1 76 69 6f 75 73 6c 79 20 72 6f 6c 6c 65 64 20 62 viously rolled b
128e2 61 63 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6d ack out of the m
128e3 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 ain journal (and
128e4 20 61 72 65 20 68 65 6e 63 65 20 69 6e 20 70 44 are hence in pD
128e5 6f 6e 65 29 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 one). ** will b
128e6 65 20 73 6b 69 70 70 65 64 2e 20 20 4f 75 74 2d e skipped. Out-
128e7 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 73 20 61 of-range pages a
128e8 72 65 20 61 6c 73 6f 20 73 6b 69 70 70 65 64 2e re also skipped.
128e9 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 . */. if( pSav
128ea 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 75 33 epoint ){. u3
128eb 32 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 2 ii;
128ec 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
128ed 20 2a 2f 0a 20 20 20 20 69 36 34 20 6f 66 66 73 */. i64 offs
128ee 65 74 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d et = pSavepoint-
128ef 3e 69 53 75 62 52 65 63 2a 28 34 2b 70 50 61 67 >iSubRec*(4+pPag
128f0 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 er->pageSize);.
128f1 20 20 20 66 6f 72 28 69 69 3d 70 53 61 76 65 70 for(ii=pSavep
128f2 6f 69 6e 74 2d 3e 69 53 75 62 52 65 63 3b 20 72 oint->iSubRec; r
128f3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
128f4 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 ii<pPager->nSubR
128f5 65 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 ec; ii++){.
128f6 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 3d assert( offset=
128f7 3d 69 69 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 =ii*(4+pPager->p
128f8 61 67 65 53 69 7a 65 29 20 29 3b 0a 20 20 20 20 ageSize) );.
128f9 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 rc = pager_pla
128fa 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 yback_one_page(p
128fb 50 61 67 65 72 2c 20 30 2c 20 30 2c 20 26 6f 66 Pager, 0, 0, &of
128fc 66 73 65 74 2c 20 31 2c 20 70 44 6f 6e 65 29 3b fset, 1, pDone);
128fd 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
128fe 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f t( rc!=SQLITE_DO
128ff 4e 45 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c NE );. }.. sql
12900 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f ite3BitvecDestro
12901 79 28 70 44 6f 6e 65 29 3b 0a 20 20 69 66 28 20 y(pDone);. if(
12902 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
12903 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 . pPager->jou
12904 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 rnalOff = szJ;.
12905 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
12906 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 }../*.** Change
12907 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 the maximum numb
12908 65 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 er of in-memory
12909 70 61 67 65 73 20 74 68 61 74 20 61 72 65 20 61 pages that are a
1290a 6c 6c 6f 77 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 llowed..*/.SQLIT
1290b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1290c 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 61 qlite3PagerSetCa
1290d 63 68 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70 chesize(Pager *p
1290e 50 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 Pager, int mxPag
1290f 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 e){. sqlite3Pca
12910 63 68 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 cheSetCachesize(
12911 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c pPager->pPCache,
12912 20 6d 78 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a mxPage);.}../*.
12913 2a 2a 20 41 64 6a 75 73 74 20 74 68 65 20 72 6f ** Adjust the ro
12914 62 75 73 74 6e 65 73 73 20 6f 66 20 74 68 65 20 bustness of the
12915 64 61 74 61 62 61 73 65 20 74 6f 20 64 61 6d 61 database to dama
12916 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63 72 61 ge due to OS cra
12917 73 68 65 73 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 shes.** or power
12918 20 66 61 69 6c 75 72 65 73 20 62 79 20 63 68 61 failures by cha
12919 6e 67 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 nging the number
1291a 20 6f 66 20 73 79 6e 63 73 28 29 73 20 77 68 65 of syncs()s whe
1291b 6e 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 n writing.** the
1291c 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
1291d 6c 2e 20 20 54 68 65 72 65 20 61 72 65 20 74 68 l. There are th
1291e 72 65 65 20 6c 65 76 65 6c 73 3a 0a 2a 2a 0a 2a ree levels:.**.*
1291f 2a 20 20 20 20 4f 46 46 20 20 20 20 20 20 20 73 * OFF s
12920 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 29 20 69 qlite3OsSync() i
12921 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 2e 20 s never called.
12922 20 54 68 69 73 20 69 73 20 74 68 65 20 64 65 66 This is the def
12923 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 ault.**
12924 20 20 20 20 20 66 6f 72 20 74 65 6d 70 6f 72 61 for tempora
12925 72 79 20 61 6e 64 20 74 72 61 6e 73 69 65 6e 74 ry and transient
12926 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 files..**.**
12927 20 4e 4f 52 4d 41 4c 20 20 20 20 54 68 65 20 6a NORMAL The j
12928 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 ournal is synced
12929 20 6f 6e 63 65 20 62 65 66 6f 72 65 20 77 72 69 once before wri
1292a 74 65 73 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 tes begin on the
1292b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1292c 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 database. This
1292d 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 64 65 is normally ade
1292e 71 75 61 74 65 20 70 72 6f 74 65 63 74 69 6f 6e quate protection
1292f 2c 20 62 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 , but.**
12930 20 20 20 20 20 20 69 74 20 69 73 20 74 68 65 6f it is theo
12931 72 65 74 69 63 61 6c 6c 79 20 70 6f 73 73 69 62 retically possib
12932 6c 65 2c 20 74 68 6f 75 67 68 20 76 65 72 79 20 le, though very
12933 75 6e 6c 69 6b 65 6c 79 2c 0a 2a 2a 20 20 20 20 unlikely,.**
12934 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20 61 that a
12935 6e 20 69 6e 6f 70 65 72 74 75 6e 65 20 70 6f 77 n inopertune pow
12936 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 er failure could
12937 20 6c 65 61 76 65 20 74 68 65 20 6a 6f 75 72 6e leave the journ
12938 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 al.**
12939 20 20 20 69 6e 20 61 20 73 74 61 74 65 20 77 68 in a state wh
1293a 69 63 68 20 77 6f 75 6c 64 20 63 61 75 73 65 20 ich would cause
1293b 64 61 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61 damage to the da
1293c 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 tabase.**
1293d 20 20 20 20 20 20 20 77 68 65 6e 20 69 74 20 69 when it i
1293e 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a s rolled back..*
1293f 2a 0a 2a 2a 20 20 20 20 46 55 4c 4c 20 20 20 20 *.** FULL
12940 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 The journal is
12941 20 73 79 6e 63 65 64 20 74 77 69 63 65 20 62 65 synced twice be
12942 66 6f 72 65 20 77 72 69 74 65 73 20 62 65 67 69 fore writes begi
12943 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 n on the.**
12944 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 databas
12945 65 20 28 77 69 74 68 20 73 6f 6d 65 20 61 64 64 e (with some add
12946 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
12947 69 6f 6e 20 2d 20 74 68 65 20 6e 52 65 63 20 66 ion - the nRec f
12948 69 65 6c 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 ield.**
12949 20 20 20 20 20 6f 66 20 74 68 65 20 6a 6f 75 72 of the jour
1294a 6e 61 6c 20 68 65 61 64 65 72 20 2d 20 62 65 69 nal header - bei
1294b 6e 67 20 77 72 69 74 74 65 6e 20 69 6e 20 62 65 ng written in be
1294c 74 77 65 65 6e 20 74 68 65 20 74 77 6f 0a 2a 2a tween the two.**
1294d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 79 sy
1294e 6e 63 73 29 2e 20 20 49 66 20 77 65 20 61 73 73 ncs). If we ass
1294f 75 6d 65 20 74 68 61 74 20 77 72 69 74 69 6e 67 ume that writing
12950 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 a.**
12951 20 20 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 single disk s
12952 65 63 74 6f 72 20 69 73 20 61 74 6f 6d 69 63 2c ector is atomic,
12953 20 74 68 65 6e 20 74 68 69 73 20 6d 6f 64 65 20 then this mode
12954 70 72 6f 76 69 64 65 73 0a 2a 2a 20 20 20 20 20 provides.**
12955 20 20 20 20 20 20 20 20 20 61 73 73 75 72 61 6e assuran
12956 63 65 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 ce that the jour
12957 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 nal will not be
12958 63 6f 72 72 75 70 74 65 64 20 74 6f 20 74 68 65 corrupted to the
12959 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1295a 20 70 6f 69 6e 74 20 6f 66 20 63 61 75 73 69 6e point of causin
1295b 67 20 64 61 6d 61 67 65 20 74 6f 20 74 68 65 20 g damage to the
1295c 64 61 74 61 62 61 73 65 20 64 75 72 69 6e 67 20 database during
1295d 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 rollback..**.**
1295e 4e 75 6d 65 72 69 63 20 76 61 6c 75 65 73 20 61 Numeric values a
1295f 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
12960 68 65 73 65 20 73 74 61 74 65 73 20 61 72 65 20 hese states are
12961 4f 46 46 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32 OFF==1, NORMAL=2
12962 2c 0a 2a 2a 20 61 6e 64 20 46 55 4c 4c 3d 33 2e ,.** and FULL=3.
12963 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
12964 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 TE_OMIT_PAGER_PR
12965 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f 50 52 49 AGMAS.SQLITE_PRI
12966 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
12967 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79 4c 3PagerSetSafetyL
12968 65 76 65 6c 28 50 61 67 65 72 20 2a 70 50 61 67 evel(Pager *pPag
12969 65 72 2c 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 er, int level, i
1296a 6e 74 20 62 46 75 6c 6c 46 73 79 6e 63 29 7b 0a nt bFullFsync){.
1296b 20 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 pPager->noSync
1296c 20 3d 20 20 28 6c 65 76 65 6c 3d 3d 31 20 7c 7c = (level==1 ||
1296d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c pPager->tempFil
1296e 65 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 e) ?1:0;. pPage
1296f 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 28 6c r->fullSync = (l
12970 65 76 65 6c 3d 3d 33 20 26 26 20 21 70 50 61 67 evel==3 && !pPag
12971 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 20 3f 31 er->tempFile) ?1
12972 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 :0;. pPager->sy
12973 6e 63 5f 66 6c 61 67 73 20 3d 20 28 62 46 75 6c nc_flags = (bFul
12974 6c 46 73 79 6e 63 3f 53 51 4c 49 54 45 5f 53 59 lFsync?SQLITE_SY
12975 4e 43 5f 46 55 4c 4c 3a 53 51 4c 49 54 45 5f 53 NC_FULL:SQLITE_S
12976 59 4e 43 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20 69 YNC_NORMAL);. i
12977 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e f( pPager->noSyn
12978 63 20 29 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 c ) pPager->need
12979 53 79 6e 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 Sync = 0;.}.#end
1297a 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f if../*.** The fo
1297b 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 llowing global v
1297c 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 ariable is incre
1297d 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 20 mented whenever
1297e 74 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 61 the library.** a
1297f 74 74 65 6d 70 74 73 20 74 6f 20 6f 70 65 6e 20 ttempts to open
12980 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 a temporary file
12981 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 . This informat
12982 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72 0a ion is used for.
12983 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 ** testing and a
12984 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 20 20 0a nalysis only. .
12985 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
12986 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
12987 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 int sqlite3_ope
12988 6e 74 65 6d 70 5f 63 6f 75 6e 74 20 3d 20 30 3b ntemp_count = 0;
12989 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f .#endif../*.** O
1298a 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 pen a temporary
1298b 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 file..**.** Writ
1298c 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 e the file descr
1298d 69 70 74 6f 72 20 69 6e 74 6f 20 2a 70 46 69 6c iptor into *pFil
1298e 65 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 e. Return SQLITE
1298f 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 0a _OK on success .
12990 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 ** or some other
12991 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 77 error code if w
12992 65 20 66 61 69 6c 2e 20 54 68 65 20 4f 53 20 77 e fail. The OS w
12993 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c ill automaticall
12994 79 20 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 y .** delete the
12995 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 temporary file
12996 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 when it is close
12997 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 d..**.** The fla
12998 67 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 gs passed to the
12999 20 56 46 53 20 6c 61 79 65 72 20 78 4f 70 65 6e VFS layer xOpen
1299a 28 29 20 63 61 6c 6c 20 61 72 65 20 74 68 6f 73 () call are thos
1299b 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 e specified.** b
1299c 79 20 70 61 72 61 6d 65 74 65 72 20 76 66 73 46 y parameter vfsF
1299d 6c 61 67 73 20 4f 52 65 64 20 77 69 74 68 20 74 lags ORed with t
1299e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a he following:.**
1299f 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f .** SQLITE_O
129a0 50 45 4e 5f 52 45 41 44 57 52 49 54 45 0a 2a 2a PEN_READWRITE.**
129a1 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e SQLITE_OPEN
129a2 5f 43 52 45 41 54 45 0a 2a 2a 20 20 20 20 20 53 _CREATE.** S
129a3 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 QLITE_OPEN_EXCLU
129a4 53 49 56 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 SIVE.** SQLI
129a5 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e TE_OPEN_DELETEON
129a6 43 4c 4f 53 45 0a 2a 2f 0a 73 74 61 74 69 63 20 CLOSE.*/.static
129a7 69 6e 74 20 70 61 67 65 72 4f 70 65 6e 74 65 6d int pagerOpentem
129a8 70 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 p(. Pager *pPag
129a9 65 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 er, /* Th
129aa 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 2a e pager object *
129ab 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 /. sqlite3_file
129ac 20 2a 70 46 69 6c 65 2c 20 20 2f 2a 20 57 72 69 *pFile, /* Wri
129ad 74 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 te the file desc
129ae 72 69 70 74 6f 72 20 68 65 72 65 20 2a 2f 0a 20 riptor here */.
129af 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 int vfsFlags
129b0 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
129b1 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 passed through t
129b2 6f 20 74 68 65 20 56 46 53 20 2a 2f 0a 29 7b 0a o the VFS */.){.
129b3 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
129b4 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
129b5 6e 20 63 6f 64 65 20 2a 2f 0a 0a 23 69 66 64 65 n code */..#ifde
129b6 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 f SQLITE_TEST.
129b7 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d 70 sqlite3_opentemp
129b8 5f 63 6f 75 6e 74 2b 2b 3b 20 20 2f 2a 20 55 73 _count++; /* Us
129b9 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 ed for testing a
129ba 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 nd analysis only
129bb 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 76 66 */.#endif.. vf
129bc 73 46 6c 61 67 73 20 7c 3d 20 20 53 51 4c 49 54 sFlags |= SQLIT
129bd 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
129be 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 | SQLITE_OPEN_C
129bf 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20 20 20 REATE |.
129c0 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f SQLITE_OPEN_
129c1 45 58 43 4c 55 53 49 56 45 20 7c 20 53 51 4c 49 EXCLUSIVE | SQLI
129c2 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e TE_OPEN_DELETEON
129c3 43 4c 4f 53 45 3b 0a 20 20 72 63 20 3d 20 73 71 CLOSE;. rc = sq
129c4 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 50 61 67 lite3OsOpen(pPag
129c5 65 72 2d 3e 70 56 66 73 2c 20 30 2c 20 70 46 69 er->pVfs, 0, pFi
129c6 6c 65 2c 20 76 66 73 46 6c 61 67 73 2c 20 30 29 le, vfsFlags, 0)
129c7 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d ;. assert( rc!=
129c8 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f SQLITE_OK || isO
129c9 70 65 6e 28 70 46 69 6c 65 29 20 29 3b 0a 20 20 pen(pFile) );.
129ca 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
129cb 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 75 73 79 .** Set the busy
129cc 20 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f handler functio
129cd 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 n..**.** The pag
129ce 65 72 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 62 er invokes the b
129cf 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 66 20 73 usy-handler if s
129d0 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 20 72 qlite3OsLock() r
129d1 65 74 75 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 eturns .** SQLIT
129d2 45 5f 42 55 53 59 20 77 68 65 6e 20 74 72 79 69 E_BUSY when tryi
129d3 6e 67 20 74 6f 20 75 70 67 72 61 64 65 20 66 72 ng to upgrade fr
129d4 6f 6d 20 6e 6f 2d 6c 6f 63 6b 20 74 6f 20 61 20 om no-lock to a
129d5 53 48 41 52 45 44 20 6c 6f 63 6b 2c 0a 2a 2a 20 SHARED lock,.**
129d6 6f 72 20 77 68 65 6e 20 74 72 79 69 6e 67 20 74 or when trying t
129d7 6f 20 75 70 67 72 61 64 65 20 66 72 6f 6d 20 61 o upgrade from a
129d8 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 74 RESERVED lock t
129d9 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 0a o an EXCLUSIVE .
129da 2a 2a 20 6c 6f 63 6b 2e 20 49 74 20 64 6f 65 73 ** lock. It does
129db 20 2a 6e 6f 74 2a 20 69 6e 76 6f 6b 65 20 74 68 *not* invoke th
129dc 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 77 e busy handler w
129dd 68 65 6e 20 75 70 67 72 61 64 69 6e 67 20 66 72 hen upgrading fr
129de 6f 6d 0a 2a 2a 20 53 48 41 52 45 44 20 74 6f 20 om.** SHARED to
129df 52 45 53 45 52 56 45 44 2c 20 6f 72 20 77 68 65 RESERVED, or whe
129e0 6e 20 75 70 67 72 61 64 69 6e 67 20 66 72 6f 6d n upgrading from
129e1 20 53 48 41 52 45 44 20 74 6f 20 45 58 43 4c 55 SHARED to EXCLU
129e2 53 49 56 45 0a 2a 2a 20 28 77 68 69 63 68 20 6f SIVE.** (which o
129e3 63 63 75 72 73 20 64 75 72 69 6e 67 20 68 6f 74 ccurs during hot
129e4 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 -journal rollbac
129e5 6b 29 2e 20 53 75 6d 6d 61 72 79 3a 0a 2a 2a 0a k). Summary:.**.
129e6 2a 2a 20 20 20 54 72 61 6e 73 69 74 69 6f 6e 20 ** Transition
129e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
129e8 20 20 20 20 20 20 20 7c 20 49 6e 76 6f 6b 65 73 | Invokes
129e9 20 78 42 75 73 79 48 61 6e 64 6c 65 72 0a 2a 2a xBusyHandler.**
129ea 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d -------------
129eb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
129ec 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
129ed 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 -----------.**
129ee 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 20 2d NO_LOCK -
129ef 3e 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 > SHARED_LOCK
129f0 20 20 20 7c 20 59 65 73 0a 2a 2a 20 20 20 53 48 | Yes.** SH
129f1 41 52 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 52 ARED_LOCK -> R
129f2 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 20 ESERVED_LOCK
129f3 7c 20 4e 6f 0a 2a 2a 20 20 20 53 48 41 52 45 44 | No.** SHARED
129f4 5f 4c 4f 43 4b 20 20 20 2d 3e 20 45 58 43 4c 55 _LOCK -> EXCLU
129f5 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 4e 6f SIVE_LOCK | No
129f6 0a 2a 2a 20 20 20 52 45 53 45 52 56 45 44 5f 4c .** RESERVED_L
129f7 4f 43 4b 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 OCK -> EXCLUSIVE
129f8 5f 4c 4f 43 4b 20 20 20 7c 20 59 65 73 0a 2a 2a _LOCK | Yes.**
129f9 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 73 79 2d .** If the busy-
129fa 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b handler callback
129fb 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 returns non-zer
129fc 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 0a o, the lock is .
129fd 2a 2a 20 72 65 74 72 69 65 64 2e 20 49 66 20 69 ** retried. If i
129fe 74 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 2c 20 t returns zero,
129ff 74 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f then the SQLITE_
12a00 42 55 53 59 20 65 72 72 6f 72 20 69 73 0a 2a 2a BUSY error is.**
12a01 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 returned to the
12a02 20 63 61 6c 6c 65 72 20 6f 66 20 74 68 65 20 70 caller of the p
12a03 61 67 65 72 20 41 50 49 20 66 75 6e 63 74 69 6f ager API functio
12a04 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
12a05 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
12a06 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 3PagerSetBusyhan
12a07 64 6c 65 72 28 0a 20 20 50 61 67 65 72 20 2a 70 dler(. Pager *p
12a08 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 Pager,
12a09 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12a0a 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a Pager object */.
12a0b 20 20 69 6e 74 20 28 2a 78 42 75 73 79 48 61 6e int (*xBusyHan
12a0c 64 6c 65 72 29 28 76 6f 69 64 20 2a 29 2c 20 20 dler)(void *),
12a0d 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
12a0e 72 20 74 6f 20 62 75 73 79 2d 68 61 6e 64 6c 65 r to busy-handle
12a0f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 r function */.
12a10 76 6f 69 64 20 2a 70 42 75 73 79 48 61 6e 64 6c void *pBusyHandl
12a11 65 72 41 72 67 20 20 20 20 20 20 20 20 20 20 20 erArg
12a12 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 /* Argument
12a13 20 74 6f 20 70 61 73 73 20 74 6f 20 78 42 75 73 to pass to xBus
12a14 79 48 61 6e 64 6c 65 72 20 2a 2f 0a 29 7b 20 20 yHandler */.){
12a15 0a 20 20 70 50 61 67 65 72 2d 3e 78 42 75 73 79 . pPager->xBusy
12a16 48 61 6e 64 6c 65 72 20 3d 20 78 42 75 73 79 48 Handler = xBusyH
12a17 61 6e 64 6c 65 72 3b 0a 20 20 70 50 61 67 65 72 andler;. pPager
12a18 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 ->pBusyHandlerAr
12a19 67 20 3d 20 70 42 75 73 79 48 61 6e 64 6c 65 72 g = pBusyHandler
12a1a 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 Arg;.}../*.** Re
12a1b 70 6f 72 74 20 74 68 65 20 63 75 72 72 65 6e 74 port the current
12a1c 20 70 61 67 65 20 73 69 7a 65 20 61 6e 64 20 6e page size and n
12a1d 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 umber of reserve
12a1e 64 20 62 79 74 65 73 20 62 61 63 6b 0a 2a 2a 20 d bytes back.**
12a1f 74 6f 20 74 68 65 20 63 6f 64 65 63 2e 0a 2a 2f to the codec..*/
12a20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 .#ifdef SQLITE_H
12a21 41 53 5f 43 4f 44 45 43 0a 73 74 61 74 69 63 20 AS_CODEC.static
12a22 76 6f 69 64 20 70 61 67 65 72 52 65 70 6f 72 74 void pagerReport
12a23 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 Size(Pager *pPag
12a24 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 er){. if( pPage
12a25 72 2d 3e 78 43 6f 64 65 63 53 69 7a 65 43 68 6e r->xCodecSizeChn
12a26 67 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d g ){. pPager-
12a27 3e 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 28 >xCodecSizeChng(
12a28 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 2c 20 pPager->pCodec,
12a29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
12a2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
12a2b 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 6e (in
12a2c 74 29 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 72 t)pPager->nReser
12a2d 76 65 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 ve);. }.}.#else
12a2e 0a 23 20 64 65 66 69 6e 65 20 70 61 67 65 72 52 .# define pagerR
12a2f 65 70 6f 72 74 53 69 7a 65 28 58 29 20 20 20 20 eportSize(X)
12a30 20 2f 2a 20 4e 6f 2d 6f 70 20 69 66 20 77 65 20 /* No-op if we
12a31 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 61 do not support a
12a32 20 63 6f 64 65 63 20 2a 2f 0a 23 65 6e 64 69 66 codec */.#endif
12a33 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
12a34 68 65 20 70 61 67 65 20 73 69 7a 65 20 75 73 65 he page size use
12a35 64 20 62 79 20 74 68 65 20 50 61 67 65 72 20 6f d by the Pager o
12a36 62 6a 65 63 74 2e 20 54 68 65 20 6e 65 77 20 70 bject. The new p
12a37 61 67 65 20 73 69 7a 65 20 0a 2a 2a 20 69 73 20 age size .** is
12a38 70 61 73 73 65 64 20 69 6e 20 2a 70 50 61 67 65 passed in *pPage
12a39 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 Size..**.** If t
12a3a 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 he pager is in t
12a3b 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 77 he error state w
12a3c 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
12a3d 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 0a n is called, it.
12a3e 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 54 ** is a no-op. T
12a3f 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
12a40 64 20 69 73 20 74 68 65 20 65 72 72 6f 72 20 73 d is the error s
12a41 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 tate error code
12a42 28 69 2e 65 2e 20 0a 2a 2a 20 6f 6e 65 20 6f 66 (i.e. .** one of
12a43 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53 SQLITE_IOERR, S
12a44 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 6f 72 QLITE_CORRUPT or
12a45 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29 2e 0a 2a SQLITE_FULL)..*
12a46 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 *.** Otherwise,
12a47 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f if all of the fo
12a48 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 llowing are true
12a49 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 20 :.**.** * the
12a4a 6e 65 77 20 70 61 67 65 20 73 69 7a 65 20 28 76 new page size (v
12a4b 61 6c 75 65 20 6f 66 20 2a 70 50 61 67 65 53 69 alue of *pPageSi
12a4c 7a 65 29 20 69 73 20 76 61 6c 69 64 20 28 61 20 ze) is valid (a
12a4d 70 6f 77 65 72 20 0a 2a 2a 20 20 20 20 20 6f 66 power .** of
12a4e 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35 31 32 two between 512
12a4f 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 41 58 5f and SQLITE_MAX_
12a50 50 41 47 45 5f 53 49 5a 45 2c 20 69 6e 63 6c 75 PAGE_SIZE, inclu
12a51 73 69 76 65 29 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a sive), and.**.**
12a52 20 20 20 2a 20 74 68 65 72 65 20 61 72 65 20 6e * there are n
12a53 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 o outstanding pa
12a54 67 65 20 72 65 66 65 72 65 6e 63 65 73 2c 20 61 ge references, a
12a55 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 nd.**.** * the
12a56 20 64 61 74 61 62 61 73 65 20 69 73 20 65 69 74 database is eit
12a57 68 65 72 20 6e 6f 74 20 61 6e 20 69 6e 2d 6d 65 her not an in-me
12a58 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 6f 72 mory database or
12a59 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 61 6e it is.** an
12a5a 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 in-memory datab
12a5b 61 73 65 20 74 68 61 74 20 63 75 72 72 65 6e 74 ase that current
12a5c 6c 79 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 7a ly consists of z
12a5d 65 72 6f 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a ero pages..**.**
12a5e 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 then the pager
12a5f 6f 62 6a 65 63 74 20 70 61 67 65 20 73 69 7a 65 object page size
12a60 20 69 73 20 73 65 74 20 74 6f 20 2a 70 50 61 67 is set to *pPag
12a61 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 eSize..**.** If
12a62 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 the page size is
12a63 20 63 68 61 6e 67 65 64 2c 20 74 68 65 6e 20 74 changed, then t
12a64 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 his function use
12a65 73 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 61 s sqlite3PagerMa
12a66 6c 6c 6f 63 28 29 20 0a 2a 2a 20 74 6f 20 6f 62 lloc() .** to ob
12a67 74 61 69 6e 20 61 20 6e 65 77 20 50 61 67 65 72 tain a new Pager
12a68 2e 70 54 6d 70 53 70 61 63 65 20 62 75 66 66 65 .pTmpSpace buffe
12a69 72 2e 20 49 66 20 74 68 69 73 20 61 6c 6c 6f 63 r. If this alloc
12a6a 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 20 0a 2a ation attempt .*
12a6b 2a 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f * fails, SQLITE_
12a6c 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 NOMEM is returne
12a6d 64 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 73 d and the page s
12a6e 69 7a 65 20 72 65 6d 61 69 6e 73 20 75 6e 63 68 ize remains unch
12a6f 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 6e 20 61 6c anged. .** In al
12a70 6c 20 6f 74 68 65 72 20 63 61 73 65 73 2c 20 53 l other cases, S
12a71 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
12a72 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 rned..**.** If t
12a73 68 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 he page size is
12a74 6e 6f 74 20 63 68 61 6e 67 65 64 2c 20 65 69 74 not changed, eit
12a75 68 65 72 20 62 65 63 61 75 73 65 20 6f 6e 65 20 her because one
12a76 6f 66 20 74 68 65 20 65 6e 75 6d 65 72 61 74 65 of the enumerate
12a77 64 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 73 20 d.** conditions
12a78 61 62 6f 76 65 20 69 73 20 6e 6f 74 20 74 72 75 above is not tru
12a79 65 2c 20 74 68 65 20 70 61 67 65 72 20 77 61 73 e, the pager was
12a7a 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 in error state
12a7b 77 68 65 6e 20 74 68 69 73 0a 2a 2a 20 66 75 6e when this.** fun
12a7c 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 ction was called
12a7d 2c 20 6f 72 20 62 65 63 61 75 73 65 20 74 68 65 , or because the
12a7e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
12a7f 6f 6e 20 61 74 74 65 6d 70 74 20 66 61 69 6c 65 on attempt faile
12a80 64 2c 20 0a 2a 2a 20 74 68 65 6e 20 2a 70 50 61 d, .** then *pPa
12a81 67 65 53 69 7a 65 20 69 73 20 73 65 74 20 74 6f geSize is set to
12a82 20 74 68 65 20 6f 6c 64 2c 20 72 65 74 61 69 6e the old, retain
12a83 65 64 20 70 61 67 65 20 73 69 7a 65 20 62 65 66 ed page size bef
12a84 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a ore returning..*
12a85 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
12a86 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
12a87 72 53 65 74 50 61 67 65 73 69 7a 65 28 50 61 67 rSetPagesize(Pag
12a88 65 72 20 2a 70 50 61 67 65 72 2c 20 75 31 36 20 er *pPager, u16
12a89 2a 70 50 61 67 65 53 69 7a 65 2c 20 69 6e 74 20 *pPageSize, int
12a8a 6e 52 65 73 65 72 76 65 29 7b 0a 20 20 69 6e 74 nReserve){. int
12a8b 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 rc = pPager->er
12a8c 72 43 6f 64 65 3b 0a 0a 20 20 69 66 28 20 72 63 rCode;.. if( rc
12a8d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
12a8e 20 20 20 75 31 36 20 70 61 67 65 53 69 7a 65 20 u16 pageSize
12a8f 3d 20 2a 70 50 61 67 65 53 69 7a 65 3b 0a 20 20 = *pPageSize;.
12a90 20 20 61 73 73 65 72 74 28 20 70 61 67 65 53 69 assert( pageSi
12a91 7a 65 3d 3d 30 20 7c 7c 20 28 70 61 67 65 53 69 ze==0 || (pageSi
12a92 7a 65 3e 3d 35 31 32 20 26 26 20 70 61 67 65 53 ze>=512 && pageS
12a93 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f ize<=SQLITE_MAX_
12a94 50 41 47 45 5f 53 49 5a 45 29 20 29 3b 0a 20 20 PAGE_SIZE) );.
12a95 20 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e 6d if( (pPager->m
12a96 65 6d 44 62 3d 3d 30 20 7c 7c 20 70 50 61 67 65 emDb==0 || pPage
12a97 72 2d 3e 64 62 53 69 7a 65 3d 3d 30 29 0a 20 20 r->dbSize==0).
12a98 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 63 61 && sqlite3Pca
12a99 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 cheRefCount(pPag
12a9a 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 er->pPCache)==0
12a9b 0a 20 20 20 20 20 26 26 20 70 61 67 65 53 69 7a . && pageSiz
12a9c 65 20 26 26 20 70 61 67 65 53 69 7a 65 21 3d 70 e && pageSize!=p
12a9d 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 Pager->pageSize
12a9e 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 63 68 . ){. ch
12a9f 61 72 20 2a 70 4e 65 77 20 3d 20 28 63 68 61 72 ar *pNew = (char
12aa0 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 4d 61 *)sqlite3PageMa
12aa1 6c 6c 6f 63 28 70 61 67 65 53 69 7a 65 29 3b 0a lloc(pageSize);.
12aa2 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 77 20 if( !pNew
12aa3 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
12aa4 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
12aa5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
12aa6 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 pager_reset(p
12aa7 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 Pager);.
12aa8 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
12aa9 20 3d 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 = pageSize;.
12aaa 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
12aab 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70 54 6d Free(pPager->pTm
12aac 70 53 70 61 63 65 29 3b 0a 20 20 20 20 20 20 20 pSpace);.
12aad 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 pPager->pTmpSpa
12aae 63 65 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 ce = pNew;.
12aaf 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 sqlite3Pcache
12ab0 53 65 74 50 61 67 65 53 69 7a 65 28 70 50 61 67 SetPageSize(pPag
12ab1 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67 er->pPCache, pag
12ab2 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a eSize);. }.
12ab3 20 20 20 20 7d 0a 20 20 20 20 2a 70 50 61 67 65 }. *pPage
12ab4 53 69 7a 65 20 3d 20 28 75 31 36 29 70 50 61 67 Size = (u16)pPag
12ab5 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 er->pageSize;.
12ab6 20 20 69 66 28 20 6e 52 65 73 65 72 76 65 3c 30 if( nReserve<0
12ab7 20 29 20 6e 52 65 73 65 72 76 65 20 3d 20 70 50 ) nReserve = pP
12ab8 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 3b 0a ager->nReserve;.
12ab9 20 20 20 20 61 73 73 65 72 74 28 20 6e 52 65 73 assert( nRes
12aba 65 72 76 65 3e 3d 30 20 26 26 20 6e 52 65 73 65 erve>=0 && nRese
12abb 72 76 65 3c 31 30 30 30 20 29 3b 0a 20 20 20 20 rve<1000 );.
12abc 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 pPager->nReserve
12abd 20 3d 20 28 69 31 36 29 6e 52 65 73 65 72 76 65 = (i16)nReserve
12abe 3b 0a 20 20 20 20 70 61 67 65 72 52 65 70 6f 72 ;. pagerRepor
12abf 74 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 tSize(pPager);.
12ac0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
12ac1 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
12ac2 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
12ac3 20 22 74 65 6d 70 6f 72 61 72 79 20 70 61 67 65 "temporary page
12ac4 22 20 62 75 66 66 65 72 20 68 65 6c 64 20 69 6e " buffer held in
12ac5 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62 79 20 74 ternally.** by t
12ac6 68 65 20 70 61 67 65 72 2e 20 20 54 68 69 73 20 he pager. This
12ac7 69 73 20 61 20 62 75 66 66 65 72 20 74 68 61 74 is a buffer that
12ac8 20 69 73 20 62 69 67 20 65 6e 6f 75 67 68 20 74 is big enough t
12ac9 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 65 6e o hold the.** en
12aca 74 69 72 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 tire content of
12acb 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2e a database page.
12acc 20 20 54 68 69 73 20 62 75 66 66 65 72 20 69 73 This buffer is
12acd 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 used internally
12ace 0a 2a 2a 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62 .** during rollb
12acf 61 63 6b 20 61 6e 64 20 77 69 6c 6c 20 62 65 20 ack and will be
12ad0 6f 76 65 72 77 72 69 74 74 65 6e 20 77 68 65 6e overwritten when
12ad1 65 76 65 72 20 61 20 72 6f 6c 6c 62 61 63 6b 0a ever a rollback.
12ad2 2a 2a 20 6f 63 63 75 72 73 2e 20 20 42 75 74 20 ** occurs. But
12ad3 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 20 61 72 other modules ar
12ad4 65 20 66 72 65 65 20 74 6f 20 75 73 65 20 69 74 e free to use it
12ad5 20 74 6f 6f 2c 20 61 73 20 6c 6f 6e 67 20 61 73 too, as long as
12ad6 0a 2a 2a 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 73 .** no rollbacks
12ad7 20 61 72 65 20 68 61 70 70 65 6e 69 6e 67 2e 0a are happening..
12ad8 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
12ad9 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 E void *sqlite3P
12ada 61 67 65 72 54 65 6d 70 53 70 61 63 65 28 50 61 agerTempSpace(Pa
12adb 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
12adc 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 70 return pPager->p
12add 54 6d 70 53 70 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a TmpSpace;.}../*.
12ade 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73 65 ** Attempt to se
12adf 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 64 61 t the maximum da
12ae0 74 61 62 61 73 65 20 70 61 67 65 20 63 6f 75 6e tabase page coun
12ae1 74 20 69 66 20 6d 78 50 61 67 65 20 69 73 20 70 t if mxPage is p
12ae2 6f 73 69 74 69 76 65 2e 20 0a 2a 2a 20 4d 61 6b ositive. .** Mak
12ae3 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 69 66 20 e no changes if
12ae4 6d 78 50 61 67 65 20 69 73 20 7a 65 72 6f 20 6f mxPage is zero o
12ae5 72 20 6e 65 67 61 74 69 76 65 2e 20 20 41 6e 64 r negative. And
12ae6 20 6e 65 76 65 72 20 72 65 64 75 63 65 20 74 68 never reduce th
12ae7 65 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 70 61 67 e.** maximum pag
12ae8 65 20 63 6f 75 6e 74 20 62 65 6c 6f 77 20 74 68 e count below th
12ae9 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f e current size o
12aea 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a f the database..
12aeb 2a 2a 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 **.** Regardless
12aec 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 of mxPage, retu
12aed 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d rn the current m
12aee 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e aximum page coun
12aef 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
12af0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
12af1 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e PagerMaxPageCoun
12af2 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c t(Pager *pPager,
12af3 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 int mxPage){.
12af4 69 66 28 20 6d 78 50 61 67 65 3e 30 20 29 7b 0a if( mxPage>0 ){.
12af5 20 20 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 pPager->mxPg
12af6 6e 6f 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 7d no = mxPage;. }
12af7 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 . sqlite3PagerP
12af8 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c agecount(pPager,
12af9 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 0);. return pP
12afa 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 3b 0a 7d 0a ager->mxPgno;.}.
12afb 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
12afc 77 69 6e 67 20 73 65 74 20 6f 66 20 72 6f 75 74 wing set of rout
12afd 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f ines are used to
12afe 20 64 69 73 61 62 6c 65 20 74 68 65 20 73 69 6d disable the sim
12aff 75 6c 61 74 65 64 0a 2a 2a 20 49 2f 4f 20 65 72 ulated.** I/O er
12b00 72 6f 72 20 6d 65 63 68 61 6e 69 73 6d 2e 20 20 ror mechanism.
12b01 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 These routines a
12b02 72 65 20 75 73 65 64 20 74 6f 20 61 76 6f 69 64 re used to avoid
12b03 20 73 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 65 72 simulated.** er
12b04 72 6f 72 73 20 69 6e 20 70 6c 61 63 65 73 20 77 rors in places w
12b05 68 65 72 65 20 77 65 20 64 6f 20 6e 6f 74 20 63 here we do not c
12b06 61 72 65 20 61 62 6f 75 74 20 65 72 72 6f 72 73 are about errors
12b07 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 2d ..**.** Unless -
12b08 44 53 51 4c 49 54 45 5f 54 45 53 54 3d 31 20 69 DSQLITE_TEST=1 i
12b09 73 20 75 73 65 64 2c 20 74 68 65 73 65 20 72 6f s used, these ro
12b0a 75 74 69 6e 65 73 20 61 72 65 20 61 6c 6c 20 6e utines are all n
12b0b 6f 2d 6f 70 73 0a 2a 2a 20 61 6e 64 20 67 65 6e o-ops.** and gen
12b0c 65 72 61 74 65 20 6e 6f 20 63 6f 64 65 2e 0a 2a erate no code..*
12b0d 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
12b0e 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 TEST.SQLITE_API
12b0f 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 extern int sqlit
12b10 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 e3_io_error_pend
12b11 69 6e 67 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 ing;.SQLITE_API
12b12 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 extern int sqlit
12b13 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 3b e3_io_error_hit;
12b14 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 .static int save
12b15 64 5f 63 6e 74 3b 0a 76 6f 69 64 20 64 69 73 61 d_cnt;.void disa
12b16 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f ble_simulated_io
12b17 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20 _errors(void){.
12b18 20 73 61 76 65 64 5f 63 6e 74 20 3d 20 73 71 6c saved_cnt = sql
12b19 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 ite3_io_error_pe
12b1a 6e 64 69 6e 67 3b 0a 20 20 73 71 6c 69 74 65 33 nding;. sqlite3
12b1b 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e _io_error_pendin
12b1c 67 20 3d 20 2d 31 3b 0a 7d 0a 76 6f 69 64 20 65 g = -1;.}.void e
12b1d 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f nable_simulated_
12b1e 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 7b io_errors(void){
12b1f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 . sqlite3_io_er
12b20 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 73 61 ror_pending = sa
12b21 76 65 64 5f 63 6e 74 3b 0a 7d 0a 23 65 6c 73 65 ved_cnt;.}.#else
12b22 0a 23 20 64 65 66 69 6e 65 20 64 69 73 61 62 6c .# define disabl
12b23 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 e_simulated_io_e
12b24 72 72 6f 72 73 28 29 0a 23 20 64 65 66 69 6e 65 rrors().# define
12b25 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 enable_simulate
12b26 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 65 d_io_errors().#e
12b27 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 ndif../*.** Read
12b28 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 79 74 the first N byt
12b29 65 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 es from the begi
12b2a 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 66 69 6c nning of the fil
12b2b 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a e into memory.**
12b2c 20 74 68 61 74 20 70 44 65 73 74 20 70 6f 69 6e that pDest poin
12b2d 74 73 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 ts to. .**.** If
12b2e 20 74 68 65 20 70 61 67 65 72 20 77 61 73 20 6f the pager was o
12b2f 70 65 6e 65 64 20 6f 6e 20 61 20 74 72 61 6e 73 pened on a trans
12b30 69 65 6e 74 20 66 69 6c 65 20 28 7a 46 69 6c 65 ient file (zFile
12b31 6e 61 6d 65 3d 3d 22 22 29 2c 20 6f 72 0a 2a 2a name==""), or.**
12b32 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 66 69 6c opened on a fil
12b33 65 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79 e less than N by
12b34 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 74 68 65 tes in size, the
12b35 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69 output buffer i
12b36 73 0a 2a 2a 20 7a 65 72 6f 65 64 20 61 6e 64 20 s.** zeroed and
12b37 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e SQLITE_OK return
12b38 65 64 2e 20 54 68 65 20 72 61 74 69 6f 6e 61 6c ed. The rational
12b39 65 20 66 6f 72 20 74 68 69 73 20 69 73 20 74 68 e for this is th
12b3a 61 74 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 at this .** func
12b3b 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 tion is used to
12b3c 72 65 61 64 20 64 61 74 61 62 61 73 65 20 68 65 read database he
12b3d 61 64 65 72 73 2c 20 61 6e 64 20 61 20 6e 65 77 aders, and a new
12b3e 20 74 72 61 6e 73 69 65 6e 74 20 6f 72 0a 2a 2a transient or.**
12b3f 20 7a 65 72 6f 20 73 69 7a 65 64 20 64 61 74 61 zero sized data
12b40 62 61 73 65 20 68 61 73 20 61 20 68 65 61 64 65 base has a heade
12b41 72 20 74 68 61 6e 20 63 6f 6e 73 69 73 74 73 20 r than consists
12b42 65 6e 74 69 72 65 6c 79 20 6f 66 20 7a 65 72 6f entirely of zero
12b43 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 es..**.** If any
12b44 20 49 4f 20 65 72 72 6f 72 20 61 70 61 72 74 20 IO error apart
12b45 66 72 6f 6d 20 53 51 4c 49 54 45 5f 49 4f 45 52 from SQLITE_IOER
12b46 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 69 73 20 R_SHORT_READ is
12b47 65 6e 63 6f 75 6e 74 65 72 65 64 2c 0a 2a 2a 20 encountered,.**
12b48 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 the error code i
12b49 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 s returned to th
12b4a 65 20 63 61 6c 6c 65 72 20 61 6e 64 20 74 68 65 e caller and the
12b4b 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
12b4c 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75 66 66 65 .** output buffe
12b4d 72 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a r undefined..*/.
12b4e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
12b4f 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 nt sqlite3PagerR
12b50 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 50 61 eadFileheader(Pa
12b51 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
12b52 20 4e 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 N, unsigned cha
12b53 72 20 2a 70 44 65 73 74 29 7b 0a 20 20 69 6e 74 r *pDest){. int
12b54 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
12b55 0a 20 20 6d 65 6d 73 65 74 28 70 44 65 73 74 2c . memset(pDest,
12b56 20 30 2c 20 4e 29 3b 0a 20 20 61 73 73 65 72 74 0, N);. assert
12b57 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
12b58 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e >fd) || pPager->
12b59 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 69 66 tempFile );. if
12b5a 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
12b5b 3e 66 64 29 20 29 7b 0a 20 20 20 20 49 4f 54 52 >fd) ){. IOTR
12b5c 41 43 45 28 28 22 44 42 48 44 52 20 25 70 20 30 ACE(("DBHDR %p 0
12b5d 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 %d\n", pPager,
12b5e 4e 29 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c N)). rc = sql
12b5f 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 ite3OsRead(pPage
12b60 72 2d 3e 66 64 2c 20 70 44 65 73 74 2c 20 4e 2c r->fd, pDest, N,
12b61 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 0);. if( rc=
12b62 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 =SQLITE_IOERR_SH
12b63 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 ORT_READ ){.
12b64 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
12b65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
12b66 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
12b67 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 * Return the tot
12b68 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 al number of pag
12b69 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 es in the databa
12b6a 73 65 20 66 69 6c 65 20 61 73 73 6f 63 69 61 74 se file associat
12b6b 65 64 20 0a 2a 2a 20 77 69 74 68 20 70 50 61 67 ed .** with pPag
12b6c 65 72 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 74 68 er. Normally, th
12b6d 69 73 20 69 73 20 63 61 6c 63 75 6c 61 74 65 64 is is calculated
12b6e 20 61 73 20 28 3c 64 62 20 66 69 6c 65 20 73 69 as (<db file si
12b6f 7a 65 3e 2f 3c 70 61 67 65 2d 73 69 7a 65 3e 29 ze>/<page-size>)
12b70 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 69 66 ..** However, if
12b71 20 74 68 65 20 66 69 6c 65 20 69 73 20 62 65 74 the file is bet
12b72 77 65 65 6e 20 31 20 61 6e 64 20 3c 70 61 67 65 ween 1 and <page
12b73 2d 73 69 7a 65 3e 20 62 79 74 65 73 20 69 6e 20 -size> bytes in
12b74 73 69 7a 65 2c 20 74 68 65 6e 20 0a 2a 2a 20 74 size, then .** t
12b75 68 69 73 20 69 73 20 63 6f 6e 73 69 64 65 72 65 his is considere
12b76 64 20 61 20 31 20 70 61 67 65 20 66 69 6c 65 2e d a 1 page file.
12b77 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 .**.** If the pa
12b78 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f 72 20 ger is in error
12b79 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 state when this
12b7a 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
12b7b 65 64 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 ed, then the.**
12b7c 65 72 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f error state erro
12b7d 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
12b7e 65 64 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 6c ed and *pnPage l
12b7f 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 20 4f eft unchanged. O
12b80 72 2c 0a 2a 2a 20 69 66 20 74 68 65 20 66 69 6c r,.** if the fil
12b81 65 20 73 79 73 74 65 6d 20 68 61 73 20 74 6f 20 e system has to
12b82 62 65 20 71 75 65 72 69 65 64 20 66 6f 72 20 74 be queried for t
12b83 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 he size of the f
12b84 69 6c 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 71 ile and.** the q
12b85 75 65 72 79 20 61 74 74 65 6d 70 74 20 72 65 74 uery attempt ret
12b86 75 72 6e 73 20 61 6e 20 49 4f 20 65 72 72 6f 72 urns an IO error
12b87 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 , the IO error c
12b88 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 0a ode is returned.
12b89 2a 2a 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 69 ** and *pnPage i
12b8a 73 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 s left unchanged
12b8b 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ..**.** Otherwis
12b8c 65 2c 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 e, if everything
12b8d 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 is successful,
12b8e 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 then SQLITE_OK i
12b8f 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e s returned.** an
12b90 64 20 2a 70 6e 50 61 67 65 20 69 73 20 73 65 74 d *pnPage is set
12b91 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f to the number o
12b92 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 f pages in the d
12b93 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 atabase..*/.SQLI
12b94 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
12b95 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 qlite3PagerPagec
12b96 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 ount(Pager *pPag
12b97 65 72 2c 20 69 6e 74 20 2a 70 6e 50 61 67 65 29 er, int *pnPage)
12b98 7b 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20 {. Pgno nPage;
12b99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
12b9a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e Value to return
12b9b 20 76 69 61 20 2a 70 6e 50 61 67 65 20 2a 2f 0a via *pnPage */.
12b9c 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 . /* If the pag
12b9d 65 72 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e er is already in
12b9e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
12b9f 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 , return the err
12ba0 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 69 66 or code. */. if
12ba1 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 ( pPager->errCod
12ba2 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
12ba3 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b pPager->errCode;
12ba4 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 72 . }.. /* Deter
12ba5 6d 69 6e 65 20 74 68 65 20 6e 75 6d 62 65 72 20 mine the number
12ba6 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 of pages in the
12ba7 66 69 6c 65 2e 20 53 74 6f 72 65 20 74 68 69 73 file. Store this
12ba8 20 69 6e 20 6e 50 61 67 65 2e 20 2a 2f 0a 20 20 in nPage. */.
12ba9 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 if( pPager->dbSi
12baa 7a 65 56 61 6c 69 64 20 29 7b 0a 20 20 20 20 6e zeValid ){. n
12bab 50 61 67 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 Page = pPager->d
12bac 62 53 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a bSize;. }else{.
12bad 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 int rc;
12bae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
12baf 72 72 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79 rror returned by
12bb0 20 4f 73 46 69 6c 65 53 69 7a 65 28 29 20 2a 2f OsFileSize() */
12bb1 0a 20 20 20 20 69 36 34 20 6e 20 3d 20 30 3b 20 . i64 n = 0;
12bb2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12bb3 46 69 6c 65 20 73 69 7a 65 20 69 6e 20 62 79 74 File size in byt
12bb4 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f es returned by O
12bb5 73 46 69 6c 65 53 69 7a 65 28 29 20 2a 2f 0a 0a sFileSize() */..
12bb6 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 assert( isOp
12bb7 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c en(pPager->fd) |
12bb8 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 | pPager->tempFi
12bb9 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 20 69 73 le );. if( is
12bba 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
12bbb 20 26 26 20 28 30 20 21 3d 20 28 72 63 20 3d 20 && (0 != (rc =
12bbc 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a sqlite3OsFileSiz
12bbd 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 6e e(pPager->fd, &n
12bbe 29 29 29 20 29 7b 0a 20 20 20 20 20 20 70 61 67 ))) ){. pag
12bbf 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c er_error(pPager,
12bc0 20 72 63 29 3b 0a 20 20 20 20 20 20 72 65 74 75 rc);. retu
12bc1 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
12bc2 20 69 66 28 20 6e 3e 30 20 26 26 20 6e 3c 70 50 if( n>0 && n<pP
12bc3 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 29 ager->pageSize )
12bc4 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 {. nPage =
12bc5 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 1;. }else{.
12bc6 20 20 20 20 6e 50 61 67 65 20 3d 20 28 50 67 6e nPage = (Pgn
12bc7 6f 29 28 6e 20 2f 20 70 50 61 67 65 72 2d 3e 70 o)(n / pPager->p
12bc8 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a ageSize);. }.
12bc9 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
12bca 73 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c state!=PAGER_UNL
12bcb 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 OCK ){. pPa
12bcc 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50 ger->dbSize = nP
12bcd 61 67 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 age;. pPage
12bce 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d 20 r->dbFileSize =
12bcf 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 70 50 61 nPage;. pPa
12bd0 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 ger->dbSizeValid
12bd1 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 1;. }. }.
12bd2 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72 . /* If the cur
12bd3 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 70 rent number of p
12bd4 61 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 ages in the file
12bd5 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
12bd6 20 74 68 65 20 0a 20 20 2a 2a 20 63 6f 6e 66 69 the . ** confi
12bd7 67 75 72 65 64 20 6d 61 78 69 6d 75 6d 20 70 61 gured maximum pa
12bd8 67 65 72 20 6e 75 6d 62 65 72 2c 20 69 6e 63 72 ger number, incr
12bd9 65 61 73 65 20 74 68 65 20 61 6c 6c 6f 77 65 64 ease the allowed
12bda 20 6c 69 6d 69 74 20 73 6f 0a 20 20 2a 2a 20 74 limit so. ** t
12bdb 68 61 74 20 74 68 65 20 66 69 6c 65 20 63 61 6e hat the file can
12bdc 20 62 65 20 72 65 61 64 2e 0a 20 20 2a 2f 0a 20 be read.. */.
12bdd 20 69 66 28 20 6e 50 61 67 65 3e 70 50 61 67 65 if( nPage>pPage
12bde 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20 20 r->mxPgno ){.
12bdf 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 pPager->mxPgno
12be0 3d 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a 20 = (Pgno)nPage;.
12be1 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 }.. /* Set the
12be2 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 output variable
12be3 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 and return SQLI
12be4 54 45 5f 4f 4b 20 2a 2f 0a 20 20 69 66 28 20 70 TE_OK */. if( p
12be5 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 6e nPage ){. *pn
12be6 50 61 67 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 Page = nPage;.
12be7 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
12be8 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 E_OK;.}.../*.**
12be9 54 72 79 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 Try to obtain a
12bea 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 6c 6f 63 lock of type loc
12beb 6b 74 79 70 65 20 6f 6e 20 74 68 65 20 64 61 74 ktype on the dat
12bec 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 0a 2a abase file. If.*
12bed 2a 20 61 20 73 69 6d 69 6c 61 72 20 6f 72 20 67 * a similar or g
12bee 72 65 61 74 65 72 20 6c 6f 63 6b 20 69 73 20 61 reater lock is a
12bef 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 74 68 69 lready held, thi
12bf0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 s function is a
12bf1 6e 6f 2d 6f 70 0a 2a 2a 20 28 72 65 74 75 72 6e no-op.** (return
12bf2 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b 20 69 6d ing SQLITE_OK im
12bf3 6d 65 64 69 61 74 65 6c 79 29 2e 0a 2a 2a 0a 2a mediately)..**.*
12bf4 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 74 74 * Otherwise, att
12bf5 65 6d 70 74 20 74 6f 20 6f 62 74 61 69 6e 20 74 empt to obtain t
12bf6 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 73 71 he lock using sq
12bf7 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 49 lite3OsLock(). I
12bf8 6e 76 6f 6b 65 20 0a 2a 2a 20 74 68 65 20 62 75 nvoke .** the bu
12bf9 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 sy callback if t
12bfa 68 65 20 6c 6f 63 6b 20 69 73 20 63 75 72 72 65 he lock is curre
12bfb 6e 74 6c 79 20 6e 6f 74 20 61 76 61 69 6c 61 62 ntly not availab
12bfc 6c 65 2e 20 52 65 70 65 61 74 20 0a 2a 2a 20 75 le. Repeat .** u
12bfd 6e 74 69 6c 20 74 68 65 20 62 75 73 79 20 63 61 ntil the busy ca
12bfe 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 66 llback returns f
12bff 61 6c 73 65 20 6f 72 20 75 6e 74 69 6c 20 74 68 alse or until th
12c00 65 20 61 74 74 65 6d 70 74 20 74 6f 20 0a 2a 2a e attempt to .**
12c01 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 6f 63 6b obtain the lock
12c02 20 73 75 63 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a succeeds..**.**
12c03 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
12c04 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 K on success and
12c05 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 an error code i
12c06 66 20 77 65 20 63 61 6e 6e 6f 74 20 6f 62 74 61 f we cannot obta
12c07 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 20 in.** the lock.
12c08 49 66 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 6f If the lock is o
12c09 62 74 61 69 6e 65 64 20 73 75 63 63 65 73 73 66 btained successf
12c0a 75 6c 6c 79 2c 20 73 65 74 20 74 68 65 20 50 61 ully, set the Pa
12c0b 67 65 72 2e 73 74 61 74 65 20 0a 2a 2a 20 76 61 ger.state .** va
12c0c 72 69 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 riable to lockty
12c0d 70 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e pe before return
12c0e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ing..*/.static i
12c0f 6e 74 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e nt pager_wait_on
12c10 5f 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 _lock(Pager *pPa
12c11 67 65 72 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 ger, int locktyp
12c12 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 e){. int rc;
12c13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12c14 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
12c15 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 turn code */..
12c16 2f 2a 20 54 68 65 20 4f 53 20 6c 6f 63 6b 20 76 /* The OS lock v
12c17 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20 74 68 alues must be th
12c18 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 50 61 e same as the Pa
12c19 67 65 72 20 6c 6f 63 6b 20 76 61 6c 75 65 73 20 ger lock values
12c1a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 */. assert( PAG
12c1b 45 52 5f 53 48 41 52 45 44 3d 3d 53 48 41 52 45 ER_SHARED==SHARE
12c1c 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 D_LOCK );. asse
12c1d 72 74 28 20 50 41 47 45 52 5f 52 45 53 45 52 56 rt( PAGER_RESERV
12c1e 45 44 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 ED==RESERVED_LOC
12c1f 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 K );. assert( P
12c20 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3d 3d AGER_EXCLUSIVE==
12c21 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 EXCLUSIVE_LOCK )
12c22 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 ;.. /* If the f
12c23 69 6c 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ile is currently
12c24 20 75 6e 6c 6f 63 6b 65 64 20 74 68 65 6e 20 74 unlocked then t
12c25 68 65 20 73 69 7a 65 20 6d 75 73 74 20 62 65 20 he size must be
12c26 75 6e 6b 6e 6f 77 6e 20 2a 2f 0a 20 20 61 73 73 unknown */. ass
12c27 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
12c28 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 te>=PAGER_SHARED
12c29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 || pPager->dbSi
12c2a 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 0a 20 zeValid==0 );..
12c2b 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 /* Check that t
12c2c 68 69 73 20 69 73 20 65 69 74 68 65 72 20 61 20 his is either a
12c2d 6e 6f 2d 6f 70 20 28 62 65 63 61 75 73 65 20 74 no-op (because t
12c2e 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 he requested loc
12c2f 6b 20 69 73 20 0a 20 20 2a 2a 20 61 6c 72 65 61 k is . ** alrea
12c30 64 79 20 68 65 6c 64 2c 20 6f 72 20 6f 6e 65 20 dy held, or one
12c31 6f 66 20 74 68 65 20 74 72 61 6e 73 69 73 74 69 of the transisti
12c32 6f 6e 73 20 74 68 61 74 20 74 68 65 20 62 75 73 ons that the bus
12c33 79 2d 68 61 6e 64 6c 65 72 0a 20 20 2a 2a 20 6d y-handler. ** m
12c34 61 79 20 62 65 20 69 6e 76 6f 6b 65 64 20 64 75 ay be invoked du
12c35 72 69 6e 67 2c 20 61 63 63 6f 72 64 69 6e 67 20 ring, according
12c36 74 6f 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 61 to the comment a
12c37 62 6f 76 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 bove. ** sqlite
12c38 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 3PagerSetBusyhan
12c39 64 6c 65 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 dler().. */. a
12c3a 73 73 65 72 74 28 20 28 70 50 61 67 65 72 2d 3e ssert( (pPager->
12c3b 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 29 state>=locktype)
12c3c 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67 . || (pPag
12c3d 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 er->state==PAGER
12c3e 5f 55 4e 4c 4f 43 4b 20 26 26 20 6c 6f 63 6b 74 _UNLOCK && lockt
12c3f 79 70 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 ype==PAGER_SHARE
12c40 44 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 50 D). || (pP
12c41 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 ager->state==PAG
12c42 45 52 5f 52 45 53 45 52 56 45 44 20 26 26 20 6c ER_RESERVED && l
12c43 6f 63 6b 74 79 70 65 3d 3d 50 41 47 45 52 5f 45 ocktype==PAGER_E
12c44 58 43 4c 55 53 49 56 45 29 0a 20 20 29 3b 0a 0a XCLUSIVE). );..
12c45 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 if( pPager->st
12c46 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b ate>=locktype ){
12c47 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
12c48 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 _OK;. }else{.
12c49 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 72 63 20 do {. rc
12c4a 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 = sqlite3OsLock(
12c4b 70 50 61 67 65 72 2d 3e 66 64 2c 20 6c 6f 63 6b pPager->fd, lock
12c4c 74 79 70 65 29 3b 0a 20 20 20 20 7d 77 68 69 6c type);. }whil
12c4d 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 e( rc==SQLITE_BU
12c4e 53 59 20 26 26 20 70 50 61 67 65 72 2d 3e 78 42 SY && pPager->xB
12c4f 75 73 79 48 61 6e 64 6c 65 72 28 70 50 61 67 65 usyHandler(pPage
12c50 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 r->pBusyHandlerA
12c51 72 67 29 20 29 3b 0a 20 20 20 20 69 66 28 20 72 rg) );. if( r
12c52 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
12c53 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 pPager->st
12c54 61 74 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 ate = (u8)lockty
12c55 70 65 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 pe;. IOTRAC
12c56 45 28 28 22 4c 4f 43 4b 20 25 70 20 25 64 5c 6e E(("LOCK %p %d\n
12c57 22 2c 20 70 50 61 67 65 72 2c 20 6c 6f 63 6b 74 ", pPager, lockt
12c58 79 70 65 29 29 0a 20 20 20 20 7d 0a 20 20 7d 0a ype)). }. }.
12c59 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
12c5a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 61 /*.** Function a
12c5b 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e ssertTruncateCon
12c5c 73 74 72 61 69 6e 74 28 70 50 61 67 65 72 29 20 straint(pPager)
12c5d 63 68 65 63 6b 73 20 74 68 61 74 20 6f 6e 65 20 checks that one
12c5e 6f 66 20 74 68 65 20 0a 2a 2a 20 66 6f 6c 6c 6f of the .** follo
12c5f 77 69 6e 67 20 69 73 20 74 72 75 65 20 66 6f 72 wing is true for
12c60 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 all dirty pages
12c61 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 currently in th
12c62 65 20 70 61 67 65 2d 63 61 63 68 65 3a 0a 2a 2a e page-cache:.**
12c63 0a 2a 2a 20 20 20 61 29 20 54 68 65 20 70 61 67 .** a) The pag
12c64 65 20 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 e number is less
12c65 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
12c66 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 o the size of th
12c67 65 20 0a 2a 2a 20 20 20 20 20 20 63 75 72 72 65 e .** curre
12c68 6e 74 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 nt database imag
12c69 65 2c 20 69 6e 20 70 61 67 65 73 2c 20 4f 52 0a e, in pages, OR.
12c6a 2a 2a 0a 2a 2a 20 20 20 62 29 20 69 66 20 74 68 **.** b) if th
12c6b 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 77 e page content w
12c6c 65 72 65 20 77 72 69 74 74 65 6e 20 61 74 20 74 ere written at t
12c6d 68 69 73 20 74 69 6d 65 2c 20 69 74 20 77 6f 75 his time, it wou
12c6e 6c 64 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 62 ld not.** b
12c6f 65 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77 e necessary to w
12c70 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 rite the current
12c71 20 63 6f 6e 74 65 6e 74 20 6f 75 74 20 74 6f 20 content out to
12c72 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a the sub-journal.
12c73 2a 2a 20 20 20 20 20 20 28 61 73 20 64 65 74 65 ** (as dete
12c74 72 6d 69 6e 65 64 20 62 79 20 66 75 6e 63 74 69 rmined by functi
12c75 6f 6e 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 on subjRequiresP
12c76 61 67 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 age())..**.** If
12c77 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 61 the condition a
12c78 73 73 65 72 74 65 64 20 62 79 20 74 68 69 73 20 sserted by this
12c79 66 75 6e 63 74 69 6f 6e 20 77 65 72 65 20 6e 6f function were no
12c7a 74 20 74 72 75 65 2c 20 61 6e 64 20 74 68 65 0a t true, and the.
12c7b 2a 2a 20 64 69 72 74 79 20 70 61 67 65 20 77 65 ** dirty page we
12c7c 72 65 20 74 6f 20 62 65 20 64 69 73 63 61 72 64 re to be discard
12c7d 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 ed from the cach
12c7e 65 20 76 69 61 20 74 68 65 20 70 61 67 65 72 53 e via the pagerS
12c7f 74 72 65 73 73 28 29 0a 2a 2a 20 72 6f 75 74 69 tress().** routi
12c80 6e 65 2c 20 70 61 67 65 72 53 74 72 65 73 73 28 ne, pagerStress(
12c81 29 20 77 6f 75 6c 64 20 6e 6f 74 20 77 72 69 74 ) would not writ
12c82 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 e the current pa
12c83 67 65 20 63 6f 6e 74 65 6e 74 20 74 6f 0a 2a 2a ge content to.**
12c84 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
12c85 6c 65 2e 20 49 66 20 61 20 73 61 76 65 70 6f 69 le. If a savepoi
12c86 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 nt transaction w
12c87 65 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 ere rolled back
12c88 61 66 74 65 72 0a 2a 2a 20 74 68 69 73 20 68 61 after.** this ha
12c89 70 70 65 6e 65 64 2c 20 74 68 65 20 63 6f 72 72 ppened, the corr
12c8a 65 63 74 20 62 65 68 61 76 69 6f 75 72 20 77 6f ect behaviour wo
12c8b 75 6c 64 20 62 65 20 74 6f 20 72 65 73 74 6f 72 uld be to restor
12c8c 65 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a e the current.**
12c8d 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 content of the
12c8e 70 61 67 65 2e 20 48 6f 77 65 76 65 72 2c 20 73 page. However, s
12c8f 69 6e 63 65 20 74 68 69 73 20 63 6f 6e 74 65 6e ince this conten
12c90 74 20 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 t is not present
12c91 20 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 74 68 in either.** th
12c92 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
12c93 6f 72 20 74 68 65 20 70 6f 72 74 69 6f 6e 20 6f or the portion o
12c94 66 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a f the rollback j
12c95 6f 75 72 6e 61 6c 20 61 6e 64 20 0a 2a 2a 20 73 ournal and .** s
12c96 75 62 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 65 ub-journal rolle
12c97 64 20 62 61 63 6b 20 74 68 65 20 63 6f 6e 74 65 d back the conte
12c98 6e 74 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 nt could not be
12c99 72 65 73 74 6f 72 65 64 20 61 6e 64 20 74 68 65 restored and the
12c9a 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69 6d 61 .** database ima
12c9b 67 65 20 77 6f 75 6c 64 20 62 65 63 6f 6d 65 20 ge would become
12c9c 63 6f 72 72 75 70 74 2e 20 49 74 20 69 73 20 74 corrupt. It is t
12c9d 68 65 72 65 66 6f 72 65 20 66 6f 72 74 75 6e 61 herefore fortuna
12c9e 74 65 20 74 68 61 74 20 0a 2a 2a 20 74 68 69 73 te that .** this
12c9f 20 63 69 72 63 75 6d 73 74 61 6e 63 65 20 63 61 circumstance ca
12ca0 6e 6e 6f 74 20 61 72 69 73 65 2e 0a 2a 2f 0a 23 nnot arise..*/.#
12ca1 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
12ca2 45 5f 44 45 42 55 47 29 0a 73 74 61 74 69 63 20 E_DEBUG).static
12ca3 76 6f 69 64 20 61 73 73 65 72 74 54 72 75 6e 63 void assertTrunc
12ca4 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 62 28 ateConstraintCb(
12ca5 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 61 PgHdr *pPg){. a
12ca6 73 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 ssert( pPg->flag
12ca7 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b s&PGHDR_DIRTY );
12ca8 0a 20 20 61 73 73 65 72 74 28 20 21 73 75 62 6a . assert( !subj
12ca9 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67 RequiresPage(pPg
12caa 29 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d ) || pPg->pgno<=
12cab 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 64 62 53 pPg->pPager->dbS
12cac 69 7a 65 20 29 3b 0a 7d 0a 73 74 61 74 69 63 20 ize );.}.static
12cad 76 6f 69 64 20 61 73 73 65 72 74 54 72 75 6e 63 void assertTrunc
12cae 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 50 61 ateConstraint(Pa
12caf 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
12cb0 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 sqlite3PcacheIte
12cb1 72 61 74 65 44 69 72 74 79 28 70 50 61 67 65 72 rateDirty(pPager
12cb2 2d 3e 70 50 43 61 63 68 65 2c 20 61 73 73 65 72 ->pPCache, asser
12cb3 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 tTruncateConstra
12cb4 69 6e 74 43 62 29 3b 0a 7d 0a 23 65 6c 73 65 0a intCb);.}.#else.
12cb5 23 20 64 65 66 69 6e 65 20 61 73 73 65 72 74 54 # define assertT
12cb6 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e runcateConstrain
12cb7 74 28 70 50 61 67 65 72 29 0a 23 65 6e 64 69 66 t(pPager).#endif
12cb8 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 ../*.** Truncate
12cb9 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 the in-memory d
12cba 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6d 61 atabase file ima
12cbb 67 65 20 74 6f 20 6e 50 61 67 65 20 70 61 67 65 ge to nPage page
12cbc 73 2e 20 54 68 69 73 20 0a 2a 2a 20 66 75 6e 63 s. This .** func
12cbd 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61 63 tion does not ac
12cbe 74 75 61 6c 6c 79 20 6d 6f 64 69 66 79 20 74 68 tually modify th
12cbf 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
12cc0 6f 6e 20 64 69 73 6b 2e 20 49 74 20 0a 2a 2a 20 on disk. It .**
12cc1 6a 75 73 74 20 73 65 74 73 20 74 68 65 20 69 6e just sets the in
12cc2 74 65 72 6e 61 6c 20 73 74 61 74 65 20 6f 66 20 ternal state of
12cc3 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 the pager object
12cc4 20 73 6f 20 74 68 61 74 20 74 68 65 20 0a 2a 2a so that the .**
12cc5 20 74 72 75 6e 63 61 74 69 6f 6e 20 77 69 6c 6c truncation will
12cc6 20 62 65 20 64 6f 6e 65 20 77 68 65 6e 20 74 68 be done when th
12cc7 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 e current transa
12cc8 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 ction is committ
12cc9 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
12cca 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
12ccb 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 e3PagerTruncateI
12ccc 6d 61 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 mage(Pager *pPag
12ccd 65 72 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b er, Pgno nPage){
12cce 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
12ccf 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 r->dbSizeValid )
12cd0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
12cd1 65 72 2d 3e 64 62 53 69 7a 65 3e 3d 6e 50 61 67 er->dbSize>=nPag
12cd2 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 e );. assert( p
12cd3 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
12cd4 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a GER_RESERVED );.
12cd5 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
12cd6 20 3d 20 6e 50 61 67 65 3b 0a 20 20 61 73 73 65 = nPage;. asse
12cd7 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 rtTruncateConstr
12cd8 61 69 6e 74 28 70 50 61 67 65 72 29 3b 0a 7d 0a aint(pPager);.}.
12cd9 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 ./*.** Shutdown
12cda 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2e 20 the page cache.
12cdb 20 46 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 Free all memory
12cdc 20 61 6e 64 20 63 6c 6f 73 65 20 61 6c 6c 20 66 and close all f
12cdd 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 iles..**.** If a
12cde 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 transaction was
12cdf 20 69 6e 20 70 72 6f 67 72 65 73 73 20 77 68 65 in progress whe
12ce0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 n this routine i
12ce1 73 20 63 61 6c 6c 65 64 2c 20 74 68 61 74 0a 2a s called, that.*
12ce2 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 * transaction is
12ce3 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41 rolled back. A
12ce4 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 ll outstanding p
12ce5 61 67 65 73 20 61 72 65 20 69 6e 76 61 6c 69 64 ages are invalid
12ce6 61 74 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65 69 ated.** and thei
12ce7 72 20 6d 65 6d 6f 72 79 20 69 73 20 66 72 65 65 r memory is free
12ce8 64 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 d. Any attempt
12ce9 74 6f 20 75 73 65 20 61 20 70 61 67 65 20 61 73 to use a page as
12cea 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 sociated.** with
12ceb 20 74 68 69 73 20 70 61 67 65 20 63 61 63 68 65 this page cache
12cec 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 after this func
12ced 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 77 69 6c tion returns wil
12cee 6c 20 6c 69 6b 65 6c 79 0a 2a 2a 20 72 65 73 75 l likely.** resu
12cef 6c 74 20 69 6e 20 61 20 63 6f 72 65 64 75 6d 70 lt in a coredump
12cf0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
12cf1 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 73 75 63 ction always suc
12cf2 63 65 65 64 73 2e 20 49 66 20 61 20 74 72 61 6e ceeds. If a tran
12cf3 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 saction is activ
12cf4 65 20 61 6e 20 61 74 74 65 6d 70 74 0a 2a 2a 20 e an attempt.**
12cf5 69 73 20 6d 61 64 65 20 74 6f 20 72 6f 6c 6c 20 is made to roll
12cf6 69 74 20 62 61 63 6b 2e 20 49 66 20 61 6e 20 65 it back. If an e
12cf7 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 rror occurs duri
12cf8 6e 67 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 ng the rollback
12cf9 0a 2a 2a 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 .** a hot journa
12cfa 6c 20 6d 61 79 20 62 65 20 6c 65 66 74 20 69 6e l may be left in
12cfb 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20 the filesystem
12cfc 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 but no error is
12cfd 72 65 74 75 72 6e 65 64 0a 2a 2a 20 74 6f 20 74 returned.** to t
12cfe 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 he caller..*/.SQ
12cff 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
12d00 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f sqlite3PagerClo
12d01 73 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 se(Pager *pPager
12d02 29 7b 0a 20 20 64 69 73 61 62 6c 65 5f 73 69 6d ){. disable_sim
12d03 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 ulated_io_errors
12d04 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 ();. sqlite3Beg
12d05 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 inBenignMalloc()
12d06 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 ;. pPager->errC
12d07 6f 64 65 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 ode = 0;. pPage
12d08 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
12d09 20 3d 20 30 3b 0a 20 20 70 61 67 65 72 5f 72 65 = 0;. pager_re
12d0a 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 69 set(pPager);. i
12d0b 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 f( MEMDB ){.
12d0c 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 pager_unlock(pPa
12d0d 67 65 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ger);. }else{.
12d0e 20 20 20 2f 2a 20 53 65 74 20 50 61 67 65 72 2e /* Set Pager.
12d0f 6a 6f 75 72 6e 61 6c 48 64 72 20 74 6f 20 2d 31 journalHdr to -1
12d10 20 66 6f 72 20 74 68 65 20 62 65 6e 65 66 69 74 for the benefit
12d11 20 6f 66 20 74 68 65 20 70 61 67 65 72 5f 70 6c of the pager_pl
12d12 61 79 62 61 63 6b 28 29 20 0a 20 20 20 20 2a 2a ayback() . **
12d13 20 63 61 6c 6c 20 77 68 69 63 68 20 6d 61 79 20 call which may
12d14 62 65 20 6d 61 64 65 20 66 72 6f 6d 20 77 69 74 be made from wit
12d15 68 69 6e 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 hin pagerUnlockA
12d16 6e 64 52 6f 6c 6c 62 61 63 6b 28 29 2e 20 49 66 ndRollback(). If
12d17 20 69 74 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f it. ** is no
12d18 74 20 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 75 t -1, then the u
12d19 6e 73 79 6e 63 65 64 20 70 6f 72 74 69 6f 6e 20 nsynced portion
12d1a 6f 66 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e of an open journ
12d1b 61 6c 20 66 69 6c 65 20 6d 61 79 0a 20 20 20 20 al file may.
12d1c 2a 2a 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 ** be played bac
12d1d 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 k into the datab
12d1e 61 73 65 2e 20 49 66 20 61 20 70 6f 77 65 72 20 ase. If a power
12d1f 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77 failure occurs w
12d20 68 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 69 73 hile. ** this
12d21 20 69 73 20 68 61 70 70 65 6e 69 6e 67 2c 20 74 is happening, t
12d22 68 65 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 he database may
12d23 62 65 63 6f 6d 65 20 63 6f 72 72 75 70 74 2e 0a become corrupt..
12d24 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 */. pPage
12d25 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 r->journalHdr =
12d26 2d 31 3b 0a 20 20 20 20 70 61 67 65 72 55 6e 6c -1;. pagerUnl
12d27 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 ockAndRollback(p
12d28 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 73 71 Pager);. }. sq
12d29 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 lite3EndBenignMa
12d2a 6c 6c 6f 63 28 29 3b 0a 20 20 65 6e 61 62 6c 65 lloc();. enable
12d2b 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 _simulated_io_er
12d2c 72 6f 72 73 28 29 3b 0a 20 20 50 41 47 45 52 54 rors();. PAGERT
12d2d 52 41 43 45 28 28 22 43 4c 4f 53 45 20 25 64 5c RACE(("CLOSE %d\
12d2e 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 n", PAGERID(pPag
12d2f 65 72 29 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 er)));. IOTRACE
12d30 28 28 22 43 4c 4f 53 45 20 25 70 5c 6e 22 2c 20 (("CLOSE %p\n",
12d31 70 50 61 67 65 72 29 29 0a 20 20 73 71 6c 69 74 pPager)). sqlit
12d32 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 e3OsClose(pPager
12d33 2d 3e 66 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 ->fd);. sqlite3
12d34 50 61 67 65 46 72 65 65 28 70 50 61 67 65 72 2d PageFree(pPager-
12d35 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 73 >pTmpSpace);. s
12d36 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 qlite3PcacheClos
12d37 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 e(pPager->pPCach
12d38 65 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 e);..#ifdef SQLI
12d39 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 69 TE_HAS_CODEC. i
12d3a 66 28 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 f( pPager->xCode
12d3b 63 46 72 65 65 20 29 20 70 50 61 67 65 72 2d 3e cFree ) pPager->
12d3c 78 43 6f 64 65 63 46 72 65 65 28 70 50 61 67 65 xCodecFree(pPage
12d3d 72 2d 3e 70 43 6f 64 65 63 29 3b 0a 23 65 6e 64 r->pCodec);.#end
12d3e 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 if.. assert( !p
12d3f 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e Pager->aSavepoin
12d40 74 20 26 26 20 21 70 50 61 67 65 72 2d 3e 70 49 t && !pPager->pI
12d41 6e 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 nJournal );. as
12d42 73 65 72 74 28 20 21 69 73 4f 70 65 6e 28 70 50 sert( !isOpen(pP
12d43 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 21 69 ager->jfd) && !i
12d44 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a sOpen(pPager->sj
12d45 66 64 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 fd) );.. sqlite
12d46 33 5f 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a 3_free(pPager);.
12d47 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
12d48 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 OK;.}..#if !defi
12d49 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 ned(NDEBUG) || d
12d4a 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 efined(SQLITE_TE
12d4b 53 54 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e ST)./*.** Return
12d4c 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
12d4d 20 66 6f 72 20 70 61 67 65 20 70 50 67 2e 0a 2a for page pPg..*
12d4e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
12d4f 20 50 67 6e 6f 20 73 71 6c 69 74 65 33 50 61 67 Pgno sqlite3Pag
12d50 65 72 50 61 67 65 6e 75 6d 62 65 72 28 44 62 50 erPagenumber(DbP
12d51 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 age *pPg){. ret
12d52 75 72 6e 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 7d urn pPg->pgno;.}
12d53 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 .#endif../*.** I
12d54 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 ncrement the ref
12d55 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 erence count for
12d56 20 70 61 67 65 20 70 50 67 2e 0a 2a 2f 0a 53 51 page pPg..*/.SQ
12d57 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
12d58 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 d sqlite3PagerRe
12d59 66 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a f(DbPage *pPg){.
12d5a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 sqlite3PcacheR
12d5b 65 66 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ef(pPg);.}../*.*
12d5c 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e * Sync the journ
12d5d 61 6c 2e 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 al. In other wor
12d5e 64 73 2c 20 6d 61 6b 65 20 73 75 72 65 20 61 6c ds, make sure al
12d5f 6c 20 74 68 65 20 70 61 67 65 73 20 74 68 61 74 l the pages that
12d60 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 77 72 have.** been wr
12d61 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 itten to the jou
12d62 72 6e 61 6c 20 68 61 76 65 20 61 63 74 75 61 6c rnal have actual
12d63 6c 79 20 72 65 61 63 68 65 64 20 74 68 65 20 73 ly reached the s
12d64 75 72 66 61 63 65 20 6f 66 20 74 68 65 0a 2a 2a urface of the.**
12d65 20 64 69 73 6b 20 61 6e 64 20 63 61 6e 20 62 65 disk and can be
12d66 20 72 65 73 74 6f 72 65 64 20 69 6e 20 74 68 65 restored in the
12d67 20 65 76 65 6e 74 20 6f 66 20 61 20 68 6f 74 2d event of a hot-
12d68 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b journal rollback
12d69 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 ..**.** If the P
12d6a 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c ager.needSync fl
12d6b 61 67 20 69 73 20 6e 6f 74 20 73 65 74 2c 20 74 ag is not set, t
12d6c 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
12d6d 6e 20 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e n is a.** no-op.
12d6e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 Otherwise, the
12d6f 61 63 74 69 6f 6e 73 20 72 65 71 75 69 72 65 64 actions required
12d70 20 64 65 70 65 6e 64 20 6f 6e 20 74 68 65 20 6a depend on the j
12d71 6f 75 72 6e 61 6c 2d 6d 6f 64 65 0a 2a 2a 20 61 ournal-mode.** a
12d72 6e 64 20 74 68 65 20 64 65 76 69 63 65 20 63 68 nd the device ch
12d73 61 72 61 63 74 65 72 69 73 74 69 63 73 20 6f 66 aracteristics of
12d74 20 74 68 65 20 74 68 65 20 66 69 6c 65 2d 73 79 the the file-sy
12d75 73 74 65 6d 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 stem, as follows
12d76 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 :.**.** * If t
12d77 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
12d78 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 is an in-memory
12d79 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6e 6f journal file, no
12d7a 20 61 63 74 69 6f 6e 20 6e 65 65 64 0a 2a 2a 20 action need.**
12d7b 20 20 20 20 62 65 20 74 61 6b 65 6e 2e 0a 2a 2a be taken..**
12d7c 0a 2a 2a 20 20 20 2a 20 4f 74 68 65 72 77 69 73 .** * Otherwis
12d7d 65 2c 20 69 66 20 74 68 65 20 64 65 76 69 63 65 e, if the device
12d7e 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 does not suppor
12d7f 74 20 74 68 65 20 53 41 46 45 5f 41 50 50 45 4e t the SAFE_APPEN
12d80 44 20 70 72 6f 70 65 72 74 79 2c 0a 2a 2a 20 20 D property,.**
12d81 20 20 20 74 68 65 6e 20 74 68 65 20 6e 52 65 63 then the nRec
12d82 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 6d 6f field of the mo
12d83 73 74 20 72 65 63 65 6e 74 6c 79 20 77 72 69 74 st recently writ
12d84 74 65 6e 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 ten journal head
12d85 65 72 0a 2a 2a 20 20 20 20 20 69 73 20 75 70 64 er.** is upd
12d86 61 74 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 ated to contain
12d87 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6a 6f the number of jo
12d88 75 72 6e 61 6c 20 72 65 63 6f 72 64 73 20 74 68 urnal records th
12d89 61 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 62 at have.** b
12d8a 65 65 6e 20 77 72 69 74 74 65 6e 20 66 6f 6c 6c een written foll
12d8b 6f 77 69 6e 67 20 69 74 2e 20 49 66 20 74 68 65 owing it. If the
12d8c 20 70 61 67 65 72 20 69 73 20 6f 70 65 72 61 74 pager is operat
12d8d 69 6e 67 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 ing in full-sync
12d8e 0a 2a 2a 20 20 20 20 20 6d 6f 64 65 2c 20 74 68 .** mode, th
12d8f 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 en the journal f
12d90 69 6c 65 20 69 73 20 73 79 6e 63 65 64 20 62 65 ile is synced be
12d91 66 6f 72 65 20 74 68 69 73 20 66 69 65 6c 64 20 fore this field
12d92 69 73 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a is updated..**.*
12d93 2a 20 20 20 2a 20 49 66 20 74 68 65 20 64 65 76 * * If the dev
12d94 69 63 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 ice does not sup
12d95 70 6f 72 74 20 74 68 65 20 53 45 51 55 45 4e 54 port the SEQUENT
12d96 49 41 4c 20 70 72 6f 70 65 72 74 79 2c 20 74 68 IAL property, th
12d97 65 6e 20 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e en .** journ
12d98 61 6c 20 66 69 6c 65 20 69 73 20 73 79 6e 63 65 al file is synce
12d99 64 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 6e 20 d..**.** Or, in
12d9a 70 73 65 75 64 6f 2d 63 6f 64 65 3a 0a 2a 2a 0a pseudo-code:.**.
12d9b 2a 2a 20 20 20 69 66 28 20 4e 4f 54 20 3c 69 6e ** if( NOT <in
12d9c 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 3e -memory journal>
12d9d 20 29 7b 0a 2a 2a 20 20 20 20 20 69 66 28 20 4e ){.** if( N
12d9e 4f 54 20 53 41 46 45 5f 41 50 50 45 4e 44 20 29 OT SAFE_APPEND )
12d9f 7b 0a 2a 2a 20 20 20 20 20 20 20 69 66 28 20 3c {.** if( <
12da0 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 3e 20 full-sync mode>
12da1 29 20 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c ) xSync(<journal
12da2 20 66 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 20 20 file>);.**
12da3 20 20 3c 75 70 64 61 74 65 20 6e 52 65 63 20 66 <update nRec f
12da4 69 65 6c 64 3e 0a 2a 2a 20 20 20 20 20 7d 20 0a ield>.** } .
12da5 2a 2a 20 20 20 20 20 69 66 28 20 4e 4f 54 20 53 ** if( NOT S
12da6 45 51 55 45 4e 54 49 41 4c 20 29 20 78 53 79 6e EQUENTIAL ) xSyn
12da7 63 28 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 3e c(<journal file>
12da8 29 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 );.** }.**.**
12da9 54 68 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 The Pager.needSy
12daa 6e 63 20 66 6c 61 67 20 69 73 20 6e 65 76 65 72 nc flag is never
12dab 20 62 65 20 73 65 74 20 66 6f 72 20 74 65 6d 70 be set for temp
12dac 6f 72 61 72 79 20 66 69 6c 65 73 2c 20 6f 72 20 orary files, or
12dad 61 6e 79 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 72 any.** file oper
12dae 61 74 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 ating in no-sync
12daf 20 6d 6f 64 65 20 28 50 61 67 65 72 2e 6e 6f 53 mode (Pager.noS
12db0 79 6e 63 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a ync set to non-z
12db1 65 72 6f 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 ero)..**.** If s
12db2 75 63 63 65 73 73 66 75 6c 2c 20 74 68 69 73 20 uccessful, this
12db3 72 6f 75 74 69 6e 65 20 63 6c 65 61 72 73 20 74 routine clears t
12db4 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 he PGHDR_NEED_SY
12db5 4e 43 20 66 6c 61 67 20 6f 66 20 65 76 65 72 79 NC flag of every
12db6 20 0a 2a 2a 20 70 61 67 65 20 63 75 72 72 65 6e .** page curren
12db7 74 6c 79 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f tly held in memo
12db8 72 79 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e ry before return
12db9 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 ing SQLITE_OK. I
12dba 66 20 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72 f an IO.** error
12dbb 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c is encountered,
12dbc 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 then the IO err
12dbd 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
12dbe 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 ned to the calle
12dbf 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
12dc0 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67 syncJournal(Pag
12dc1 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 er *pPager){. i
12dc2 66 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 f( pPager->needS
12dc3 79 6e 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ync ){. asser
12dc4 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 t( !pPager->temp
12dc5 46 69 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 20 File );. if(
12dc6 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
12dc7 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode!=PAGER_JOURN
12dc8 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b ALMODE_MEMORY ){
12dc9 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b 20 20 . int rc;
12dca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12dcb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
12dcc 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
12dcd 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 44 const int iD
12dce 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76 c = sqlite3OsDev
12dcf 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
12dd0 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a cs(pPager->fd);.
12dd1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 73 assert( is
12dd2 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
12dd3 29 20 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 ) );.. if(
12dd4 30 3d 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 0==(iDc&SQLITE_I
12dd5 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 OCAP_SAFE_APPEND
12dd6 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 ) ){. /*
12dd7 54 68 69 73 20 62 6c 6f 63 6b 20 64 65 61 6c 73 This block deals
12dd8 20 77 69 74 68 20 61 6e 20 6f 62 73 63 75 72 65 with an obscure
12dd9 20 70 72 6f 62 6c 65 6d 2e 20 49 66 20 74 68 65 problem. If the
12dda 20 6c 61 73 74 20 63 6f 6e 6e 65 63 74 69 6f 6e last connection
12ddb 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 . ** that
12ddc 20 77 72 6f 74 65 20 74 6f 20 74 68 69 73 20 64 wrote to this d
12ddd 61 74 61 62 61 73 65 20 77 61 73 20 6f 70 65 72 atabase was oper
12dde 61 74 69 6e 67 20 69 6e 20 70 65 72 73 69 73 74 ating in persist
12ddf 65 6e 74 2d 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 ent-journal.
12de0 20 20 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 ** mode, the
12de1 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
12de2 6c 65 20 6d 61 79 20 61 74 20 74 68 69 73 20 70 le may at this p
12de3 6f 69 6e 74 20 61 63 74 75 61 6c 6c 79 20 62 65 oint actually be
12de4 20 6c 61 72 67 65 72 0a 20 20 20 20 20 20 20 20 larger.
12de5 2a 2a 20 74 68 61 6e 20 50 61 67 65 72 2e 6a 6f ** than Pager.jo
12de6 75 72 6e 61 6c 4f 66 66 20 62 79 74 65 73 2e 20 urnalOff bytes.
12de7 49 66 20 74 68 65 20 6e 65 78 74 20 74 68 69 6e If the next thin
12de8 67 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c g in the journal
12de9 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 . ** file
12dea 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 61 happens to be a
12deb 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 journal-header
12dec 28 77 72 69 74 74 65 6e 20 61 73 20 70 61 72 74 (written as part
12ded 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 of the.
12dee 2a 2a 20 70 72 65 76 69 6f 75 73 20 63 6f 6e 6e ** previous conn
12def 65 63 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 74 ections transact
12df0 69 6f 6e 29 2c 20 61 6e 64 20 61 20 63 72 61 73 ion), and a cras
12df1 68 20 6f 72 20 70 6f 77 65 72 2d 66 61 69 6c 75 h or power-failu
12df2 72 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f re . ** o
12df3 63 63 75 72 73 20 61 66 74 65 72 20 6e 52 65 63 ccurs after nRec
12df4 20 69 73 20 75 70 64 61 74 65 64 20 62 75 74 20 is updated but
12df5 62 65 66 6f 72 65 20 74 68 69 73 20 63 6f 6e 6e before this conn
12df6 65 63 74 69 6f 6e 20 77 72 69 74 65 73 20 0a 20 ection writes .
12df7 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 74 68 69 ** anythi
12df8 6e 67 20 65 6c 73 65 20 74 6f 20 74 68 65 20 6a ng else to the j
12df9 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20 ournal file (or
12dfa 63 6f 6d 6d 69 74 73 2f 72 6f 6c 6c 73 20 62 61 commits/rolls ba
12dfb 63 6b 20 69 74 73 20 0a 20 20 20 20 20 20 20 20 ck its .
12dfc 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c ** transaction),
12dfd 20 74 68 65 6e 20 53 51 4c 69 74 65 20 6d 61 79 then SQLite may
12dfe 20 62 65 63 6f 6d 65 20 63 6f 6e 66 75 73 65 64 become confused
12dff 20 77 68 65 6e 20 64 6f 69 6e 67 20 74 68 65 20 when doing the
12e00 0a 20 20 20 20 20 20 20 20 2a 2a 20 68 6f 74 2d . ** hot-
12e01 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b journal rollback
12e02 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 63 6f 76 following recov
12e03 65 72 79 2e 20 49 74 20 6d 61 79 20 72 6f 6c 6c ery. It may roll
12e04 20 62 61 63 6b 20 61 6c 6c 0a 20 20 20 20 20 20 back all.
12e05 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 63 6f 6e ** of this con
12e06 6e 65 63 74 69 6f 6e 73 20 64 61 74 61 2c 20 74 nections data, t
12e07 68 65 6e 20 70 72 6f 63 65 65 64 20 74 6f 20 72 hen proceed to r
12e08 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 74 68 65 20 olling back the
12e09 6f 6c 64 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 old,. **
12e0a 6f 75 74 2d 6f 66 2d 64 61 74 65 20 64 61 74 61 out-of-date data
12e0b 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 74 that follows it
12e0c 2e 20 44 61 74 61 62 61 73 65 20 63 6f 72 72 75 . Database corru
12e0d 70 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a ption.. *
12e0e 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 6f 20 *. ** To
12e0f 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 73 work around this
12e10 2c 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c , if the journal
12e11 20 66 69 6c 65 20 64 6f 65 73 20 61 70 70 65 61 file does appea
12e12 72 20 74 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 20 r to contain.
12e13 20 20 20 20 20 2a 2a 20 61 20 76 61 6c 69 64 20 ** a valid
12e14 68 65 61 64 65 72 20 66 6f 6c 6c 6f 77 69 6e 67 header following
12e15 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 Pager.journalOf
12e16 66 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61 20 f, then write a
12e17 30 78 30 30 0a 20 20 20 20 20 20 20 20 2a 2a 20 0x00. **
12e18 62 79 74 65 20 74 6f 20 74 68 65 20 73 74 61 72 byte to the star
12e19 74 20 6f 66 20 69 74 20 74 6f 20 70 72 65 76 65 t of it to preve
12e1a 6e 74 20 69 74 20 66 72 6f 6d 20 62 65 69 6e 67 nt it from being
12e1b 20 72 65 63 6f 67 6e 69 7a 65 64 2e 0a 20 20 20 recognized..
12e1c 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 **.
12e1d 2a 2a 20 56 61 72 69 61 62 6c 65 20 69 4e 65 78 ** Variable iNex
12e1e 74 48 64 72 4f 66 66 73 65 74 20 69 73 20 73 65 tHdrOffset is se
12e1f 74 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 t to the offset
12e20 61 74 20 77 68 69 63 68 20 74 68 69 73 0a 20 20 at which this.
12e21 20 20 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d ** problem
12e22 61 74 69 63 20 68 65 61 64 65 72 20 77 69 6c 6c atic header will
12e23 20 6f 63 63 75 72 2c 20 69 66 20 69 74 20 65 78 occur, if it ex
12e24 69 73 74 73 2e 20 61 4d 61 67 69 63 20 69 73 20 ists. aMagic is
12e25 75 73 65 64 20 0a 20 20 20 20 20 20 20 20 2a 2a used . **
12e26 20 61 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 as a temporary
12e27 62 75 66 66 65 72 20 74 6f 20 69 6e 73 70 65 63 buffer to inspec
12e28 74 20 74 68 65 20 66 69 72 73 74 20 63 6f 75 70 t the first coup
12e29 6c 65 20 6f 66 20 62 79 74 65 73 20 6f 66 0a 20 le of bytes of.
12e2a 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70 6f ** the po
12e2b 74 65 6e 74 69 61 6c 20 6a 6f 75 72 6e 61 6c 20 tential journal
12e2c 68 65 61 64 65 72 2e 0a 20 20 20 20 20 20 20 20 header..
12e2d 2a 2f 0a 20 20 20 20 20 20 20 20 69 36 34 20 69 */. i64 i
12e2e 4e 65 78 74 48 64 72 4f 66 66 73 65 74 3b 0a 20 NextHdrOffset;.
12e2f 20 20 20 20 20 20 20 75 38 20 61 4d 61 67 69 63 u8 aMagic
12e30 5b 38 5d 3b 0a 09 75 38 20 7a 48 65 61 64 65 72 [8];..u8 zHeader
12e31 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c [sizeof(aJournal
12e32 4d 61 67 69 63 29 2b 34 5d 3b 0a 0a 09 6d 65 6d Magic)+4];...mem
12e33 63 70 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f cpy(zHeader, aJo
12e34 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 urnalMagic, size
12e35 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 of(aJournalMagic
12e36 29 29 3b 0a 09 70 75 74 33 32 62 69 74 73 28 26 ));..put32bits(&
12e37 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 zHeader[sizeof(a
12e38 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 JournalMagic)],
12e39 70 50 61 67 65 72 2d 3e 6e 52 65 63 29 3b 0a 0a pPager->nRec);..
12e3a 20 20 20 20 20 20 20 20 69 4e 65 78 74 48 64 72 iNextHdr
12e3b 4f 66 66 73 65 74 20 3d 20 6a 6f 75 72 6e 61 6c Offset = journal
12e3c 48 64 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 HdrOffset(pPager
12e3d 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
12e3e 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 sqlite3OsRead(pP
12e3f 61 67 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 ager->jfd, aMagi
12e40 63 2c 20 38 2c 20 69 4e 65 78 74 48 64 72 4f 66 c, 8, iNextHdrOf
12e41 66 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 fset);. i
12e42 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
12e43 20 26 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 61 4d && 0==memcmp(aM
12e44 61 67 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 agic, aJournalMa
12e45 67 69 63 2c 20 38 29 20 29 7b 0a 20 20 20 20 20 gic, 8) ){.
12e46 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 static cons
12e47 74 20 75 38 20 7a 65 72 6f 62 79 74 65 20 3d 20 t u8 zerobyte =
12e48 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 0;. rc
12e49 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 = sqlite3OsWrite
12e4a 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 7a (pPager->jfd, &z
12e4b 65 72 6f 62 79 74 65 2c 20 31 2c 20 69 4e 65 78 erobyte, 1, iNex
12e4c 74 48 64 72 4f 66 66 73 65 74 29 3b 0a 20 20 20 tHdrOffset);.
12e4d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
12e4e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
12e4f 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 && rc!=SQLITE_I
12e50 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 OERR_SHORT_READ
12e51 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 ){. ret
12e52 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 urn rc;.
12e53 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 72 }.. /* Wr
12e54 69 74 65 20 74 68 65 20 6e 52 65 63 20 76 61 6c ite the nRec val
12e55 75 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 ue into the jour
12e56 6e 61 6c 20 66 69 6c 65 20 68 65 61 64 65 72 2e nal file header.
12e57 20 49 66 20 69 6e 0a 20 20 20 20 20 20 20 20 2a If in. *
12e58 2a 20 66 75 6c 6c 2d 73 79 6e 63 68 72 6f 6e 6f * full-synchrono
12e59 75 73 20 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68 us mode, sync th
12e5a 65 20 6a 6f 75 72 6e 61 6c 20 66 69 72 73 74 2e e journal first.
12e5b 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 This ensures th
12e5c 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c at. ** al
12e5d 6c 20 64 61 74 61 20 68 61 73 20 72 65 61 6c 6c l data has reall
12e5e 79 20 68 69 74 20 74 68 65 20 64 69 73 6b 20 62 y hit the disk b
12e5f 65 66 6f 72 65 20 6e 52 65 63 20 69 73 20 75 70 efore nRec is up
12e60 64 61 74 65 64 20 74 6f 20 6d 61 72 6b 0a 20 20 dated to mark.
12e61 20 20 20 20 20 20 2a 2a 20 69 74 20 61 73 20 61 ** it as a
12e62 20 63 61 6e 64 69 64 61 74 65 20 66 6f 72 20 72 candidate for r
12e63 6f 6c 6c 62 61 63 6b 2e 0a 20 20 20 20 20 20 20 ollback..
12e64 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 **. ** T
12e65 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69 his is not requi
12e66 72 65 64 20 69 66 20 74 68 65 20 70 65 72 73 69 red if the persi
12e67 73 74 65 6e 74 20 6d 65 64 69 61 20 73 75 70 70 stent media supp
12e68 6f 72 74 73 20 74 68 65 0a 20 20 20 20 20 20 20 orts the.
12e69 20 2a 2a 20 53 41 46 45 5f 41 50 50 45 4e 44 20 ** SAFE_APPEND
12e6a 70 72 6f 70 65 72 74 79 2e 20 42 65 63 61 75 73 property. Becaus
12e6b 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 69 e in this case i
12e6c 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
12e6d 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 6f e . ** fo
12e6e 72 20 67 61 72 62 61 67 65 20 64 61 74 61 20 74 r garbage data t
12e6f 6f 20 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f o be appended to
12e70 20 74 68 65 20 66 69 6c 65 2c 20 74 68 65 20 6e the file, the n
12e71 52 65 63 20 66 69 65 6c 64 0a 20 20 20 20 20 20 Rec field.
12e72 20 20 2a 2a 20 69 73 20 70 6f 70 75 6c 61 74 65 ** is populate
12e73 64 20 77 69 74 68 20 30 78 46 46 46 46 46 46 46 d with 0xFFFFFFF
12e74 46 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e F when the journ
12e75 61 6c 20 68 65 61 64 65 72 20 69 73 20 77 72 69 al header is wri
12e76 74 74 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 tten. **
12e77 61 6e 64 20 6e 65 76 65 72 20 6e 65 65 64 73 20 and never needs
12e78 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20 to be updated..
12e79 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
12e7a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75 if( pPager->fu
12e7b 6c 6c 53 79 6e 63 20 26 26 20 30 3d 3d 28 69 44 llSync && 0==(iD
12e7c 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 c&SQLITE_IOCAP_S
12e7d 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20 EQUENTIAL) ){.
12e7e 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 PAGERTRA
12e7f 43 45 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 CE(("SYNC journa
12e80 6c 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 l of %d\n", PAGE
12e81 52 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 RID(pPager)));.
12e82 20 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 IOTRACE
12e83 28 28 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 (("JSYNC %p\n",
12e84 70 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 pPager)).
12e85 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
12e86 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 sSync(pPager->jf
12e87 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f d, pPager->sync_
12e88 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20 flags);.
12e89 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
12e8a 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b _OK ) return rc;
12e8b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
12e8c 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 48 44 IOTRACE(("JHD
12e8d 52 20 25 70 20 25 6c 6c 64 5c 6e 22 2c 20 70 50 R %p %lld\n", pP
12e8e 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f ager, pPager->jo
12e8f 75 72 6e 61 6c 48 64 72 29 29 3b 0a 20 20 20 20 urnalHdr));.
12e90 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
12e91 4f 73 57 72 69 74 65 28 0a 20 20 20 20 20 20 20 OsWrite(.
12e92 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 66 64 pPager->jfd
12e93 2c 20 7a 48 65 61 64 65 72 2c 20 73 69 7a 65 6f , zHeader, sizeo
12e94 66 28 7a 48 65 61 64 65 72 29 2c 20 70 50 61 67 f(zHeader), pPag
12e95 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 0a 09 er->journalHdr..
12e96 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
12e97 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 c!=SQLITE_OK ) r
12e98 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
12e99 7d 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 }. if( 0==(
12e9a 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 iDc&SQLITE_IOCAP
12e9b 5f 53 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a _SEQUENTIAL) ){.
12e9c 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 PAGERTRA
12e9d 43 45 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 CE(("SYNC journa
12e9e 6c 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 l of %d\n", PAGE
12e9f 52 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 RID(pPager)));.
12ea0 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 IOTRACE((
12ea1 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 "JSYNC %p\n", pP
12ea2 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 72 ager)). r
12ea3 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e c = sqlite3OsSyn
12ea4 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 c(pPager->jfd, p
12ea5 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 Pager->sync_flag
12ea6 73 7c 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 s| . (p
12ea7 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 Pager->sync_flag
12ea8 73 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 s==SQLITE_SYNC_F
12ea9 55 4c 4c 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f ULL?SQLITE_SYNC_
12eaa 44 41 54 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 DATAONLY:0).
12eab 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 );. i
12eac 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
12ead 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
12eae 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
12eaf 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 /* The journal
12eb0 66 69 6c 65 20 77 61 73 20 6a 75 73 74 20 73 75 file was just su
12eb1 63 63 65 73 73 66 75 6c 6c 79 20 73 79 6e 63 65 ccessfully synce
12eb2 64 2e 20 53 65 74 20 50 61 67 65 72 2e 6e 65 65 d. Set Pager.nee
12eb3 64 53 79 6e 63 20 0a 20 20 20 20 2a 2a 20 74 6f dSync . ** to
12eb4 20 7a 65 72 6f 20 61 6e 64 20 63 6c 65 61 72 20 zero and clear
12eb5 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 the PGHDR_NEED_S
12eb6 59 4e 43 20 66 6c 61 67 20 6f 6e 20 61 6c 6c 20 YNC flag on all
12eb7 70 61 67 65 73 73 2e 0a 20 20 20 20 2a 2f 0a 20 pagess.. */.
12eb8 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 pPager->needS
12eb9 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 ync = 0;. pPa
12eba 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 ger->journalStar
12ebb 74 65 64 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c ted = 1;. sql
12ebc 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 53 ite3PcacheClearS
12ebd 79 6e 63 46 6c 61 67 73 28 70 50 61 67 65 72 2d yncFlags(pPager-
12ebe 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 7d 0a 0a >pPCache);. }..
12ebf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
12ec0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 OK;.}../*.** The
12ec1 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
12ec2 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b first in a link
12ec3 65 64 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 ed list of dirty
12ec4 20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 pages connected
12ec5 0a 2a 2a 20 62 79 20 74 68 65 20 50 67 48 64 72 .** by the PgHdr
12ec6 2e 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 2e .pDirty pointer.
12ec7 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 This function w
12ec8 72 69 74 65 73 20 65 61 63 68 20 6f 6e 65 20 6f rites each one o
12ec9 66 20 74 68 65 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f f the.** in-memo
12eca 72 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 ry pages in the
12ecb 6c 69 73 74 20 74 6f 20 74 68 65 20 64 61 74 61 list to the data
12ecc 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 61 base file. The a
12ecd 72 67 75 6d 65 6e 74 20 6d 61 79 0a 2a 2a 20 62 rgument may.** b
12ece 65 20 4e 55 4c 4c 2c 20 72 65 70 72 65 73 65 6e e NULL, represen
12ecf 74 69 6e 67 20 61 6e 20 65 6d 70 74 79 20 6c 69 ting an empty li
12ed0 73 74 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 st. In this case
12ed1 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
12ed2 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a s.** a no-op..**
12ed3 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 6d 75 .** The pager mu
12ed4 73 74 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 st hold at least
12ed5 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
12ed6 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 when this funct
12ed7 69 6f 6e 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 ion.** is called
12ed8 2e 20 42 65 66 6f 72 65 20 77 72 69 74 69 6e 67 . Before writing
12ed9 20 61 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65 anything to the
12eda 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
12edb 74 68 69 73 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 this lock.** is
12edc 75 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 upgraded to an E
12edd 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 XCLUSIVE lock. I
12ede 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f f the lock canno
12edf 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 0a 2a t be obtained,.*
12ee0 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 * SQLITE_BUSY is
12ee1 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f returned and no
12ee2 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e data is written
12ee3 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
12ee4 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 file..** .** If
12ee5 20 74 68 65 20 70 61 67 65 72 20 69 73 20 61 20 the pager is a
12ee6 74 65 6d 70 2d 66 69 6c 65 20 70 61 67 65 72 20 temp-file pager
12ee7 61 6e 64 20 74 68 65 20 61 63 74 75 61 6c 20 66 and the actual f
12ee8 69 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 0a ile-system file.
12ee9 2a 2a 20 69 73 20 6e 6f 74 20 79 65 74 20 6f 70 ** is not yet op
12eea 65 6e 2c 20 69 74 20 69 73 20 63 72 65 61 74 65 en, it is create
12eeb 64 20 61 6e 64 20 6f 70 65 6e 65 64 20 62 65 66 d and opened bef
12eec 6f 72 65 20 61 6e 79 20 64 61 74 61 20 69 73 20 ore any data is
12eed 0a 2a 2a 20 77 72 69 74 74 65 6e 20 6f 75 74 2e .** written out.
12eee 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 65 20 .**.** Once the
12eef 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 75 70 lock has been up
12ef0 67 72 61 64 65 64 20 61 6e 64 2c 20 69 66 20 6e graded and, if n
12ef1 65 63 65 73 73 61 72 79 2c 20 74 68 65 20 66 69 ecessary, the fi
12ef2 6c 65 20 6f 70 65 6e 65 64 2c 0a 2a 2a 20 74 68 le opened,.** th
12ef3 65 20 70 61 67 65 73 20 61 72 65 20 77 72 69 74 e pages are writ
12ef4 74 65 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 64 ten out to the d
12ef5 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 atabase file in
12ef6 6c 69 73 74 20 6f 72 64 65 72 2e 20 57 72 69 74 list order. Writ
12ef7 69 6e 67 0a 2a 2a 20 61 20 70 61 67 65 20 69 73 ing.** a page is
12ef8 20 73 6b 69 70 70 65 64 20 69 66 20 69 74 20 6d skipped if it m
12ef9 65 65 74 73 20 65 69 74 68 65 72 20 6f 66 20 74 eets either of t
12efa 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 he following cri
12efb 74 65 72 69 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a teria:.**.** *
12efc 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 The page number
12efd 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
12efe 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 6f Pager.dbSize, o
12eff 72 0a 2a 2a 20 20 20 2a 20 54 68 65 20 50 47 48 r.** * The PGH
12f00 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c DR_DONT_WRITE fl
12f01 61 67 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 ag is set on the
12f02 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 page..**.** If
12f03 77 72 69 74 69 6e 67 20 6f 75 74 20 61 20 70 61 writing out a pa
12f04 67 65 20 63 61 75 73 65 73 20 74 68 65 20 64 61 ge causes the da
12f05 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 67 tabase file to g
12f06 72 6f 77 2c 20 50 61 67 65 72 2e 64 62 46 69 6c row, Pager.dbFil
12f07 65 53 69 7a 65 0a 2a 2a 20 69 73 20 75 70 64 61 eSize.** is upda
12f08 74 65 64 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e ted accordingly.
12f09 20 49 66 20 70 61 67 65 20 31 20 69 73 20 77 72 If page 1 is wr
12f0a 69 74 74 65 6e 20 6f 75 74 2c 20 74 68 65 6e 20 itten out, then
12f0b 74 68 65 20 76 61 6c 75 65 20 63 61 63 68 65 64 the value cached
12f0c 0a 2a 2a 20 69 6e 20 50 61 67 65 72 2e 64 62 46 .** in Pager.dbF
12f0d 69 6c 65 56 65 72 73 5b 5d 20 69 73 20 75 70 64 ileVers[] is upd
12f0e 61 74 65 64 20 74 6f 20 6d 61 74 63 68 20 74 68 ated to match th
12f0f 65 20 6e 65 77 20 76 61 6c 75 65 20 73 74 6f 72 e new value stor
12f10 65 64 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61 74 ed in.** the dat
12f11 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a abase file..**.*
12f12 2a 20 49 66 20 65 76 65 72 79 74 68 69 6e 67 20 * If everything
12f13 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 is successful, S
12f14 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
12f15 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 rned. If an IO e
12f16 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c rror .** occurs,
12f17 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 an IO error cod
12f18 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f e is returned. O
12f19 72 2c 20 69 66 20 74 68 65 20 45 58 43 4c 55 53 r, if the EXCLUS
12f1a 49 56 45 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 0a IVE lock cannot.
12f1b 2a 2a 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 ** be obtained,
12f1c 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 SQLITE_BUSY is r
12f1d 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 eturned..*/.stat
12f1e 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69 ic int pager_wri
12f1f 74 65 5f 70 61 67 65 6c 69 73 74 28 50 67 48 64 te_pagelist(PgHd
12f20 72 20 2a 70 4c 69 73 74 29 7b 0a 20 20 50 61 67 r *pList){. Pag
12f21 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 er *pPager;
12f22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12f23 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 /* Pager objec
12f24 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 t */. int rc;
12f25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12f26 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
12f27 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 eturn code */..
12f28 20 69 66 28 20 4e 45 56 45 52 28 70 4c 69 73 74 if( NEVER(pList
12f29 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20 53 51 ==0) ) return SQ
12f2a 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 67 65 LITE_OK;. pPage
12f2b 72 20 3d 20 70 4c 69 73 74 2d 3e 70 50 61 67 65 r = pList->pPage
12f2c 72 3b 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 r;.. /* At this
12f2d 20 70 6f 69 6e 74 20 74 68 65 72 65 20 6d 61 79 point there may
12f2e 20 62 65 20 65 69 74 68 65 72 20 61 20 52 45 53 be either a RES
12f2f 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 ERVED or EXCLUSI
12f30 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 VE lock on the.
12f31 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c ** database fil
12f32 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 e. If there is a
12f33 6c 72 65 61 64 79 20 61 6e 20 45 58 43 4c 55 53 lready an EXCLUS
12f34 49 56 45 20 6c 6f 63 6b 2c 20 74 68 65 20 66 6f IVE lock, the fo
12f35 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 63 61 6c llowing. ** cal
12f36 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 l is a no-op..
12f37 2a 2a 0a 20 20 2a 2a 20 4d 6f 76 69 6e 67 20 74 **. ** Moving t
12f38 68 65 20 6c 6f 63 6b 20 66 72 6f 6d 20 52 45 53 he lock from RES
12f39 45 52 56 45 44 20 74 6f 20 45 58 43 4c 55 53 49 ERVED to EXCLUSI
12f3a 56 45 20 61 63 74 75 61 6c 6c 79 20 69 6e 76 6f VE actually invo
12f3b 6c 76 65 73 20 67 6f 69 6e 67 0a 20 20 2a 2a 20 lves going. **
12f3c 74 68 72 6f 75 67 68 20 61 6e 20 69 6e 74 65 72 through an inter
12f3d 6d 65 64 69 61 74 65 20 73 74 61 74 65 20 50 45 mediate state PE
12f3e 4e 44 49 4e 47 2e 20 20 20 41 20 50 45 4e 44 49 NDING. A PENDI
12f3f 4e 47 20 6c 6f 63 6b 20 70 72 65 76 65 6e 74 73 NG lock prevents
12f40 20 6e 65 77 0a 20 20 2a 2a 20 72 65 61 64 65 72 new. ** reader
12f41 73 20 66 72 6f 6d 20 61 74 74 61 63 68 69 6e 67 s from attaching
12f42 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
12f43 20 62 75 74 20 69 73 20 75 6e 73 75 66 66 69 63 but is unsuffic
12f44 69 65 6e 74 20 66 6f 72 20 75 73 20 74 6f 0a 20 ient for us to.
12f45 20 2a 2a 20 77 72 69 74 65 2e 20 20 54 68 65 20 ** write. The
12f46 69 64 65 61 20 6f 66 20 61 20 50 45 4e 44 49 4e idea of a PENDIN
12f47 47 20 6c 6f 63 6b 20 69 73 20 74 6f 20 70 72 65 G lock is to pre
12f48 76 65 6e 74 20 6e 65 77 20 72 65 61 64 65 72 73 vent new readers
12f49 20 66 72 6f 6d 0a 20 20 2a 2a 20 63 6f 6d 69 6e from. ** comin
12f4a 67 20 69 6e 20 77 68 69 6c 65 20 77 65 20 77 61 g in while we wa
12f4b 69 74 20 66 6f 72 20 65 78 69 73 74 69 6e 67 20 it for existing
12f4c 72 65 61 64 65 72 73 20 74 6f 20 63 6c 65 61 72 readers to clear
12f4d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 69 6c .. **. ** Whil
12f4e 65 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 e the pager is i
12f4f 6e 20 74 68 65 20 52 45 53 45 52 56 45 44 20 73 n the RESERVED s
12f50 74 61 74 65 2c 20 74 68 65 20 6f 72 69 67 69 6e tate, the origin
12f51 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 al database file
12f52 0a 20 20 2a 2a 20 69 73 20 75 6e 63 68 61 6e 67 . ** is unchang
12f53 65 64 20 61 6e 64 20 77 65 20 63 61 6e 20 72 6f ed and we can ro
12f54 6c 6c 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68 llback without h
12f55 61 76 69 6e 67 20 74 6f 20 70 6c 61 79 62 61 63 aving to playbac
12f56 6b 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72 6e k the. ** journ
12f57 61 6c 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 67 al into the orig
12f58 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 inal database fi
12f59 6c 65 2e 20 20 4f 6e 63 65 20 77 65 20 74 72 61 le. Once we tra
12f5a 6e 73 69 74 69 6f 6e 20 74 6f 0a 20 20 2a 2a 20 nsition to. **
12f5b 45 58 43 4c 55 53 49 56 45 2c 20 69 74 20 6d 65 EXCLUSIVE, it me
12f5c 61 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 ans the database
12f5d 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 63 file has been c
12f5e 68 61 6e 67 65 64 20 61 6e 64 20 61 6e 79 20 72 hanged and any r
12f5f 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 77 69 6c ollback. ** wil
12f60 6c 20 72 65 71 75 69 72 65 20 61 20 6a 6f 75 72 l require a jour
12f61 6e 61 6c 20 70 6c 61 79 62 61 63 6b 2e 0a 20 20 nal playback..
12f62 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 */. assert( pPa
12f63 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 ger->state>=PAGE
12f64 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 R_RESERVED );.
12f65 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f rc = pager_wait_
12f66 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 on_lock(pPager,
12f67 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b EXCLUSIVE_LOCK);
12f68 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 .. /* If the fi
12f69 6c 65 20 69 73 20 61 20 74 65 6d 70 2d 66 69 6c le is a temp-fil
12f6a 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 e has not yet be
12f6b 65 6e 20 6f 70 65 6e 65 64 2c 20 6f 70 65 6e 20 en opened, open
12f6c 69 74 20 6e 6f 77 2e 20 49 74 0a 20 20 2a 2a 20 it now. It. **
12f6d 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
12f6e 66 6f 72 20 72 63 20 74 6f 20 62 65 20 6f 74 68 for rc to be oth
12f6f 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f er than SQLITE_O
12f70 4b 20 69 66 20 74 68 69 73 20 62 72 61 6e 63 68 K if this branch
12f71 0a 20 20 2a 2a 20 69 73 20 74 61 6b 65 6e 2c 20 . ** is taken,
12f72 61 73 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e as pager_wait_on
12f73 5f 6c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f 2d _lock() is a no-
12f74 6f 70 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c 65 op for temp-file
12f75 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 69 s.. */. if( !i
12f76 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 sOpen(pPager->fd
12f77 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 ) ){. assert(
12f78 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c pPager->tempFil
12f79 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f e && rc==SQLITE_
12f7a 4f 4b 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 OK );. rc = p
12f7b 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 agerOpentemp(pPa
12f7c 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c ger, pPager->fd,
12f7d 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 pPager->vfsFlag
12f7e 73 29 3b 0a 20 20 7d 0a 0a 20 20 77 68 69 6c 65 s);. }.. while
12f7f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
12f80 26 26 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 && pList ){.
12f81 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 4c 69 73 Pgno pgno = pLis
12f82 74 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 20 20 2f 2a t->pgno;.. /*
12f83 20 49 66 20 74 68 65 72 65 20 61 72 65 20 64 69 If there are di
12f84 72 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 rty pages in the
12f85 20 70 61 67 65 20 63 61 63 68 65 20 77 69 74 68 page cache with
12f86 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 67 72 page numbers gr
12f87 65 61 74 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 eater. ** tha
12f88 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 n Pager.dbSize,
12f89 74 68 69 73 20 6d 65 61 6e 73 20 73 71 6c 69 74 this means sqlit
12f8a 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 e3PagerTruncateI
12f8b 6d 61 67 65 28 29 20 77 61 73 20 63 61 6c 6c 65 mage() was calle
12f8c 64 20 74 6f 0a 20 20 20 20 2a 2a 20 6d 61 6b 65 d to. ** make
12f8d 20 74 68 65 20 66 69 6c 65 20 73 6d 61 6c 6c 65 the file smalle
12f8e 72 20 28 70 72 65 73 75 6d 61 62 6c 79 20 62 79 r (presumably by
12f8f 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 6f 64 auto-vacuum cod
12f90 65 29 2e 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 e). Do not write
12f91 0a 20 20 20 20 2a 2a 20 61 6e 79 20 73 75 63 68 . ** any such
12f92 20 70 61 67 65 73 20 74 6f 20 74 68 65 20 66 69 pages to the fi
12f93 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a le.. **. *
12f94 2a 20 41 6c 73 6f 2c 20 64 6f 20 6e 6f 74 20 77 * Also, do not w
12f95 72 69 74 65 20 6f 75 74 20 61 6e 79 20 70 61 67 rite out any pag
12f96 65 20 74 68 61 74 20 68 61 73 20 74 68 65 20 50 e that has the P
12f97 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 GHDR_DONT_WRITE
12f98 66 6c 61 67 0a 20 20 20 20 2a 2a 20 73 65 74 20 flag. ** set
12f99 28 73 65 74 20 62 79 20 73 71 6c 69 74 65 33 50 (set by sqlite3P
12f9a 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 29 29 agerDontWrite())
12f9b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
12f9c 20 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 pgno<=pPager->d
12f9d 62 53 69 7a 65 20 26 26 20 30 3d 3d 28 70 4c 69 bSize && 0==(pLi
12f9e 73 74 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f st->flags&PGHDR_
12f9f 44 4f 4e 54 5f 57 52 49 54 45 29 20 29 7b 0a 20 DONT_WRITE) ){.
12fa0 20 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 i64 offset
12fa1 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 = (pgno-1)*(i64)
12fa2 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
12fa3 3b 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f ; /* Offset to
12fa4 20 77 72 69 74 65 20 2a 2f 0a 20 20 20 20 20 20 write */.
12fa5 63 68 61 72 20 2a 70 44 61 74 61 3b 20 20 20 20 char *pData;
12fa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12fa7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
12fa8 2a 20 44 61 74 61 20 74 6f 20 77 72 69 74 65 20 * Data to write
12fa9 2a 2f 20 20 20 20 0a 0a 20 20 20 20 20 20 2f 2a */ .. /*
12faa 20 45 6e 63 6f 64 65 20 74 68 65 20 64 61 74 61 Encode the data
12fab 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 43 4f base */. CO
12fac 44 45 43 32 28 70 50 61 67 65 72 2c 20 70 4c 69 DEC2(pPager, pLi
12fad 73 74 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c st->pData, pgno,
12fae 20 36 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 6, return SQLIT
12faf 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 29 3b E_NOMEM, pData);
12fb0 0a 0a 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 .. /* Write
12fb1 20 6f 75 74 20 74 68 65 20 70 61 67 65 20 64 61 out the page da
12fb2 74 61 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 ta. */. rc
12fb3 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 = sqlite3OsWrite
12fb4 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 44 61 (pPager->fd, pDa
12fb5 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 ta, pPager->page
12fb6 53 69 7a 65 2c 20 6f 66 66 73 65 74 29 3b 0a 0a Size, offset);..
12fb7 20 20 20 20 20 20 2f 2a 20 49 66 20 70 61 67 65 /* If page
12fb8 20 31 20 77 61 73 20 6a 75 73 74 20 77 72 69 74 1 was just writ
12fb9 74 65 6e 2c 20 75 70 64 61 74 65 20 50 61 67 65 ten, update Page
12fba 72 2e 64 62 46 69 6c 65 56 65 72 73 20 74 6f 20 r.dbFileVers to
12fbb 6d 61 74 63 68 0a 20 20 20 20 20 20 2a 2a 20 74 match. ** t
12fbc 68 65 20 76 61 6c 75 65 20 6e 6f 77 20 73 74 6f he value now sto
12fbd 72 65 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 red in the datab
12fbe 61 73 65 20 66 69 6c 65 2e 20 49 66 20 77 72 69 ase file. If wri
12fbf 74 69 6e 67 20 74 68 69 73 20 0a 20 20 20 20 20 ting this .
12fc0 20 2a 2a 20 70 61 67 65 20 63 61 75 73 65 64 20 ** page caused
12fc1 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
12fc2 65 20 74 6f 20 67 72 6f 77 2c 20 75 70 64 61 74 e to grow, updat
12fc3 65 20 64 62 46 69 6c 65 53 69 7a 65 2e 20 0a 20 e dbFileSize. .
12fc4 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
12fc5 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 ( pgno==1 ){.
12fc6 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 memcpy(&pPa
12fc7 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c ger->dbFileVers,
12fc8 20 26 70 44 61 74 61 5b 32 34 5d 2c 20 73 69 7a &pData[24], siz
12fc9 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 eof(pPager->dbFi
12fca 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20 20 20 leVers));.
12fcb 7d 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f }. if( pgno
12fcc 3e 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 >pPager->dbFileS
12fcd 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ize ){. p
12fce 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a Pager->dbFileSiz
12fcf 65 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 e = pgno;.
12fd0 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61 }.. /* Upda
12fd1 74 65 20 61 6e 79 20 62 61 63 6b 75 70 20 6f 62 te any backup ob
12fd2 6a 65 63 74 73 20 63 6f 70 79 69 6e 67 20 74 68 jects copying th
12fd3 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
12fd4 69 73 20 70 61 67 65 72 2e 20 2a 2f 0a 20 20 20 is pager. */.
12fd5 20 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 sqlite3Backup
12fd6 55 70 64 61 74 65 28 70 50 61 67 65 72 2d 3e 70 Update(pPager->p
12fd7 42 61 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 28 75 Backup, pgno, (u
12fd8 38 2a 29 70 4c 69 73 74 2d 3e 70 44 61 74 61 29 8*)pList->pData)
12fd9 3b 0a 0a 20 20 20 20 20 20 50 41 47 45 52 54 52 ;.. PAGERTR
12fda 41 43 45 28 28 22 53 54 4f 52 45 20 25 64 20 70 ACE(("STORE %d p
12fdb 61 67 65 20 25 64 20 68 61 73 68 28 25 30 38 78 age %d hash(%08x
12fdc 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 )\n",.
12fdd 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 PAGERID
12fde 28 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 (pPager), pgno,
12fdf 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 pager_pagehash(p
12fe0 4c 69 73 74 29 29 29 3b 0a 20 20 20 20 20 20 49 List)));. I
12fe1 4f 54 52 41 43 45 28 28 22 50 47 4f 55 54 20 25 OTRACE(("PGOUT %
12fe2 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c p %d\n", pPager,
12fe3 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 50 pgno));. P
12fe4 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 AGER_INCR(sqlite
12fe5 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f 3_pager_writedb_
12fe6 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 50 41 count);. PA
12fe7 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d GER_INCR(pPager-
12fe8 3e 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 7d 65 >nWrite);. }e
12fe9 6c 73 65 7b 0a 20 20 20 20 20 20 50 41 47 45 52 lse{. PAGER
12fea 54 52 41 43 45 28 28 22 4e 4f 53 54 4f 52 45 20 TRACE(("NOSTORE
12feb 25 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 %d page %d\n", P
12fec 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 AGERID(pPager),
12fed 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 23 69 pgno));. }.#i
12fee 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 fdef SQLITE_CHEC
12fef 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 4c 69 73 K_PAGES. pLis
12ff0 74 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 t->pageHash = pa
12ff1 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69 ger_pagehash(pLi
12ff2 73 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 st);.#endif.
12ff3 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 2d 3e 70 pList = pList->p
12ff4 44 69 72 74 79 3b 0a 20 20 7d 0a 0a 20 20 72 65 Dirty;. }.. re
12ff5 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
12ff6 2a 20 41 70 70 65 6e 64 20 61 20 72 65 63 6f 72 * Append a recor
12ff7 64 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 d of the current
12ff8 20 73 74 61 74 65 20 6f 66 20 70 61 67 65 20 70 state of page p
12ff9 50 67 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f Pg to the sub-jo
12ffa 75 72 6e 61 6c 2e 20 0a 2a 2a 20 49 74 20 69 73 urnal. .** It is
12ffb 20 74 68 65 20 63 61 6c 6c 65 72 73 20 72 65 73 the callers res
12ffc 70 6f 6e 73 69 62 69 6c 69 74 79 20 74 6f 20 75 ponsibility to u
12ffd 73 65 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 se subjRequiresP
12ffe 61 67 65 28 29 20 74 6f 20 63 68 65 63 6b 20 0a age() to check .
12fff 2a 2a 20 74 68 61 74 20 69 74 20 69 73 20 72 65 ** that it is re
13000 61 6c 6c 79 20 72 65 71 75 69 72 65 64 20 62 65 ally required be
13001 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 fore calling thi
13002 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a s function..**.*
13003 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c * If successful,
13004 20 73 65 74 20 74 68 65 20 62 69 74 20 63 6f 72 set the bit cor
13005 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 50 responding to pP
13006 67 2d 3e 70 67 6e 6f 20 69 6e 20 74 68 65 20 62 g->pgno in the b
13007 69 74 76 65 63 73 0a 2a 2a 20 66 6f 72 20 61 6c itvecs.** for al
13008 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 l open savepoint
13009 73 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 s before returni
1300a 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ng..**.** This f
1300b 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
1300c 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 SQLITE_OK if eve
1300d 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 rything is succe
1300e 73 73 66 75 6c 2c 20 61 6e 20 49 4f 0a 2a 2a 20 ssful, an IO.**
1300f 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 74 68 error code if th
13010 65 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 e attempt to wri
13011 74 65 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f te to the sub-jo
13012 75 72 6e 61 6c 20 66 61 69 6c 73 2c 20 6f 72 20 urnal fails, or
13013 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d .** SQLITE_NOMEM
13014 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 if a malloc fai
13015 6c 73 20 77 68 69 6c 65 20 73 65 74 74 69 6e 67 ls while setting
13016 20 61 20 62 69 74 20 69 6e 20 61 20 73 61 76 65 a bit in a save
13017 70 6f 69 6e 74 0a 2a 2a 20 62 69 74 76 65 63 2e point.** bitvec.
13018 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
13019 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 50 67 ubjournalPage(Pg
1301a 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 69 6e 74 Hdr *pPg){. int
1301b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1301c 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
1301d 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a = pPg->pPager;.
1301e 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 if( isOpen(pPa
1301f 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 ger->sjfd) ){.
13020 20 20 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20 void *pData =
13021 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 pPg->pData;.
13022 69 36 34 20 6f 66 66 73 65 74 20 3d 20 70 50 61 i64 offset = pPa
13023 67 65 72 2d 3e 6e 53 75 62 52 65 63 2a 28 34 2b ger->nSubRec*(4+
13024 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
13025 29 3b 0a 20 20 20 20 63 68 61 72 20 2a 70 44 61 );. char *pDa
13026 74 61 32 3b 0a 0a 20 20 20 20 43 4f 44 45 43 32 ta2;.. CODEC2
13027 28 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 (pPager, pData,
13028 70 50 67 2d 3e 70 67 6e 6f 2c 20 37 2c 20 72 65 pPg->pgno, 7, re
13029 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
1302a 4d 2c 20 70 44 61 74 61 32 29 3b 0a 20 20 20 20 M, pData2);.
1302b 50 41 47 45 52 54 52 41 43 45 28 28 22 53 54 4d PAGERTRACE(("STM
1302c 54 2d 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 67 T-JOURNAL %d pag
1302d 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 e %d\n", PAGERID
1302e 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 (pPager), pPg->p
1302f 67 6e 6f 29 29 3b 0a 20 20 0a 20 20 20 20 61 73 gno));. . as
13030 73 65 72 74 28 20 70 61 67 65 49 6e 4a 6f 75 72 sert( pageInJour
13031 6e 61 6c 28 70 50 67 29 20 7c 7c 20 70 50 67 2d nal(pPg) || pPg-
13032 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 >pgno>pPager->db
13033 4f 72 69 67 53 69 7a 65 20 29 3b 0a 20 20 20 20 OrigSize );.
13034 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 rc = write32bits
13035 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 6f (pPager->sjfd, o
13036 66 66 73 65 74 2c 20 70 50 67 2d 3e 70 67 6e 6f ffset, pPg->pgno
13037 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
13038 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
13039 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
1303a 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 73 6a Write(pPager->sj
1303b 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50 61 67 fd, pData2, pPag
1303c 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 er->pageSize, of
1303d 66 73 65 74 2b 34 29 3b 0a 20 20 20 20 7d 0a 20 fset+4);. }.
1303e 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c }. if( rc==SQL
1303f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 ITE_OK ){. pP
13040 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 2b 2b 3b ager->nSubRec++;
13041 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
13042 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3e ger->nSavepoint>
13043 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 64 0 );. rc = ad
13044 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 dToSavepointBitv
13045 65 63 73 28 70 50 61 67 65 72 2c 20 70 50 67 2d ecs(pPager, pPg-
13046 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 >pgno);. }. re
13047 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
13048 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
13049 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 is called by th
1304a 65 20 70 63 61 63 68 65 20 6c 61 79 65 72 20 77 e pcache layer w
1304b 68 65 6e 20 69 74 20 68 61 73 20 72 65 61 63 68 hen it has reach
1304c 65 64 20 73 6f 6d 65 0a 2a 2a 20 73 6f 66 74 20 ed some.** soft
1304d 6d 65 6d 6f 72 79 20 6c 69 6d 69 74 2e 20 54 68 memory limit. Th
1304e 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
1304f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
13050 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63 74 0a a Pager object.
13051 2a 2a 20 28 63 61 73 74 20 61 73 20 61 20 76 6f ** (cast as a vo
13052 69 64 2a 29 2e 20 54 68 65 20 70 61 67 65 72 20 id*). The pager
13053 69 73 20 61 6c 77 61 79 73 20 27 70 75 72 67 65 is always 'purge
13054 61 62 6c 65 27 20 28 6e 6f 74 20 61 6e 20 69 6e able' (not an in
13055 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 -memory.** datab
13056 61 73 65 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 ase). The second
13057 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 72 argument is a r
13058 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 70 61 eference to a pa
13059 67 65 20 74 68 61 74 20 69 73 20 0a 2a 2a 20 63 ge that is .** c
1305a 75 72 72 65 6e 74 6c 79 20 64 69 72 74 79 20 62 urrently dirty b
1305b 75 74 20 68 61 73 20 6e 6f 20 6f 75 74 73 74 61 ut has no outsta
1305c 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 nding references
1305d 2e 20 54 68 65 20 70 61 67 65 0a 2a 2a 20 69 73 . The page.** is
1305e 20 61 6c 77 61 79 73 20 61 73 73 6f 63 69 61 74 always associat
1305f 65 64 20 77 69 74 68 20 74 68 65 20 50 61 67 65 ed with the Page
13060 72 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20 r object passed
13061 61 73 20 74 68 65 20 66 69 72 73 74 20 0a 2a 2a as the first .**
13062 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
13063 20 54 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 73 The job of this
13064 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 6f 20 function is to
13065 6d 61 6b 65 20 70 50 67 20 63 6c 65 61 6e 20 62 make pPg clean b
13066 79 20 77 72 69 74 69 6e 67 20 69 74 73 20 63 6f y writing its co
13067 6e 74 65 6e 74 73 0a 2a 2a 20 6f 75 74 20 74 6f ntents.** out to
13068 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
13069 6c 65 2c 20 69 66 20 70 6f 73 73 69 62 6c 65 2e le, if possible.
1306a 20 54 68 69 73 20 6d 61 79 20 69 6e 76 6f 6c 76 This may involv
1306b 65 20 73 79 6e 63 69 6e 67 20 74 68 65 0a 2a 2a e syncing the.**
1306c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 0a journal file. .
1306d 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 **.** If success
1306e 66 75 6c 2c 20 73 71 6c 69 74 65 33 50 63 61 63 ful, sqlite3Pcac
1306f 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 69 73 heMakeClean() is
13070 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 70 called on the p
13071 61 67 65 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 age and.** SQLIT
13072 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 E_OK returned. I
13073 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 f an IO error oc
13074 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e curs while tryin
13075 67 20 74 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a g to make the.**
13076 20 70 61 67 65 20 63 6c 65 61 6e 2c 20 74 68 65 page clean, the
13077 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 IO error code i
13078 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 74 s returned. If t
13079 68 65 20 70 61 67 65 20 63 61 6e 6e 6f 74 20 62 he page cannot b
1307a 65 0a 2a 2a 20 6d 61 64 65 20 63 6c 65 61 6e 20 e.** made clean
1307b 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 72 for some other r
1307c 65 61 73 6f 6e 2c 20 62 75 74 20 6e 6f 20 65 72 eason, but no er
1307d 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 6e ror occurs, then
1307e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 SQLITE_OK.** is
1307f 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c returned by sql
13080 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c ite3PcacheMakeCl
13081 65 61 6e 28 29 20 69 73 20 6e 6f 74 20 63 61 6c ean() is not cal
13082 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 led..*/.static i
13083 6e 74 20 70 61 67 65 72 53 74 72 65 73 73 28 76 nt pagerStress(v
13084 6f 69 64 20 2a 70 2c 20 50 67 48 64 72 20 2a 70 oid *p, PgHdr *p
13085 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 Pg){. Pager *pP
13086 61 67 65 72 20 3d 20 28 50 61 67 65 72 20 2a 29 ager = (Pager *)
13087 70 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 p;. int rc = SQ
13088 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 LITE_OK;.. asse
13089 72 74 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d rt( pPg->pPager=
1308a 3d 70 50 61 67 65 72 20 29 3b 0a 20 20 61 73 73 =pPager );. ass
1308b 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 ert( pPg->flags&
1308c 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 0a PGHDR_DIRTY );..
1308d 20 20 2f 2a 20 54 68 65 20 64 6f 4e 6f 74 53 79 /* The doNotSy
1308e 6e 63 20 66 6c 61 67 20 69 73 20 73 65 74 20 62 nc flag is set b
1308f 79 20 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 y the sqlite3Pag
13090 65 72 57 72 69 74 65 28 29 20 66 75 6e 63 74 69 erWrite() functi
13091 6f 6e 20 77 68 69 6c 65 20 69 74 0a 20 20 2a 2a on while it. **
13092 20 69 73 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 is journalling
13093 61 20 73 65 74 20 6f 66 20 74 77 6f 20 6f 72 20 a set of two or
13094 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 70 61 more database pa
13095 67 65 73 20 74 68 61 74 20 61 72 65 20 73 74 6f ges that are sto
13096 72 65 64 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 red. ** on the
13097 73 61 6d 65 20 64 69 73 6b 20 73 65 63 74 6f 72 same disk sector
13098 2e 20 53 79 6e 63 69 6e 67 20 74 68 65 20 6a 6f . Syncing the jo
13099 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 61 6c 6c urnal is not all
1309a 6f 77 65 64 20 77 68 69 6c 65 0a 20 20 2a 2a 20 owed while. **
1309b 74 68 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e this is happenin
1309c 67 20 61 73 20 69 74 20 69 73 20 69 6d 70 6f 72 g as it is impor
1309d 74 61 6e 74 20 74 68 61 74 20 61 6c 6c 20 6d 65 tant that all me
1309e 6d 62 65 72 73 20 6f 66 20 73 75 63 68 20 61 0a mbers of such a.
1309f 20 20 2a 2a 20 73 65 74 20 6f 66 20 70 61 67 65 ** set of page
130a0 73 20 61 72 65 20 73 79 6e 63 65 64 20 74 6f 20 s are synced to
130a1 64 69 73 6b 20 74 6f 67 65 74 68 65 72 2e 20 53 disk together. S
130a2 6f 2c 20 69 66 20 74 68 65 20 70 61 67 65 20 74 o, if the page t
130a3 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a his function. *
130a4 2a 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20 6d * is trying to m
130a5 61 6b 65 20 63 6c 65 61 6e 20 77 69 6c 6c 20 72 ake clean will r
130a6 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c equire a journal
130a7 20 73 79 6e 63 20 61 6e 64 20 74 68 65 20 64 6f sync and the do
130a8 4e 6f 74 53 79 6e 63 0a 20 20 2a 2a 20 66 6c 61 NotSync. ** fla
130a9 67 20 69 73 20 73 65 74 2c 20 72 65 74 75 72 6e g is set, return
130aa 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 without doing a
130ab 6e 79 74 68 69 6e 67 2e 20 54 68 65 20 70 63 61 nything. The pca
130ac 63 68 65 20 6c 61 79 65 72 20 77 69 6c 6c 0a 20 che layer will.
130ad 20 2a 2a 20 6a 75 73 74 20 68 61 76 65 20 74 6f ** just have to
130ae 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 61 6c go ahead and al
130af 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 locate a new pag
130b0 65 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 e buffer instead
130b1 20 6f 66 0a 20 20 2a 2a 20 72 65 75 73 69 6e 67 of. ** reusing
130b2 20 70 50 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 pPg.. **. **
130b3 53 69 6d 69 6c 61 72 6c 79 2c 20 69 66 20 74 68 Similarly, if th
130b4 65 20 70 61 67 65 72 20 68 61 73 20 61 6c 72 65 e pager has alre
130b5 61 64 79 20 65 6e 74 65 72 65 64 20 74 68 65 20 ady entered the
130b6 65 72 72 6f 72 20 73 74 61 74 65 2c 20 64 6f 20 error state, do
130b7 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 20 74 6f 20 not. ** try to
130b8 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e write the conten
130b9 74 73 20 6f 66 20 70 50 67 20 74 6f 20 64 69 73 ts of pPg to dis
130ba 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 k.. */. if( NE
130bb 56 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43 VER(pPager->errC
130bc 6f 64 65 29 0a 20 20 20 7c 7c 20 28 70 50 61 67 ode). || (pPag
130bd 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 26 26 er->doNotSync &&
130be 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 pPg->flags&PGHD
130bf 52 5f 4e 45 45 44 5f 53 59 4e 43 29 0a 20 20 29 R_NEED_SYNC). )
130c0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
130c1 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f ITE_OK;. }.. /
130c2 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e * Sync the journ
130c3 61 6c 20 66 69 6c 65 20 69 66 20 72 65 71 75 69 al file if requi
130c4 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 red. */. if( pP
130c5 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
130c6 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 EED_SYNC ){.
130c7 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c rc = syncJournal
130c8 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 (pPager);. if
130c9 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
130ca 26 26 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 && pPager->fullS
130cb 79 6e 63 20 26 26 20 0a 20 20 20 20 20 20 21 28 ync && . !(
130cc 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
130cd 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
130ce 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 20 26 ALMODE_MEMORY) &
130cf 26 0a 20 20 20 20 20 20 21 28 73 71 6c 69 74 65 &. !(sqlite
130d0 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 3OsDeviceCharact
130d1 65 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d eristics(pPager-
130d2 3e 66 64 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 >fd)&SQLITE_IOCA
130d3 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29 0a 20 P_SAFE_APPEND).
130d4 20 20 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 ){. pPag
130d5 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 er->nRec = 0;.
130d6 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 4a 6f rc = writeJo
130d7 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 urnalHdr(pPager)
130d8 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
130d9 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 6e 75 * If the page nu
130da 6d 62 65 72 20 6f 66 20 74 68 69 73 20 70 61 67 mber of this pag
130db 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e e is larger than
130dc 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a the current siz
130dd 65 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 64 61 e of. ** the da
130de 74 61 62 61 73 65 20 69 6d 61 67 65 2c 20 69 74 tabase image, it
130df 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 may need to be
130e0 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 written to the s
130e1 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a ub-journal.. **
130e2 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 This is because
130e3 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 70 61 67 the call to pag
130e4 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 er_write_pagelis
130e5 74 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c 20 6e t() below will n
130e6 6f 74 0a 20 20 2a 2a 20 61 63 74 75 61 6c 6c 79 ot. ** actually
130e7 20 77 72 69 74 65 20 64 61 74 61 20 74 6f 20 74 write data to t
130e8 68 65 20 66 69 6c 65 20 69 6e 20 74 68 69 73 20 he file in this
130e9 63 61 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 case.. **. **
130ea 43 6f 6e 73 69 64 65 72 20 74 68 65 20 66 6f 6c Consider the fol
130eb 6c 6f 77 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lowing sequence
130ec 6f 66 20 65 76 65 6e 74 73 3a 0a 20 20 2a 2a 0a of events:. **.
130ed 20 20 2a 2a 20 20 20 42 45 47 49 4e 3b 0a 20 20 ** BEGIN;.
130ee 2a 2a 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 ** <journal
130ef 70 61 67 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20 page X>. **
130f0 20 3c 6d 6f 64 69 66 79 20 70 61 67 65 20 58 3e <modify page X>
130f1 0a 20 20 2a 2a 20 20 20 20 20 53 41 56 45 50 4f . ** SAVEPO
130f2 49 4e 54 20 73 70 3b 0a 20 20 2a 2a 20 20 20 20 INT sp;. **
130f3 20 20 20 3c 73 68 72 69 6e 6b 20 64 61 74 61 62 <shrink datab
130f4 61 73 65 20 66 69 6c 65 20 74 6f 20 59 20 70 61 ase file to Y pa
130f5 67 65 73 3e 0a 20 20 2a 2a 20 20 20 20 20 20 20 ges>. **
130f6 70 61 67 65 72 53 74 72 65 73 73 28 70 61 67 65 pagerStress(page
130f7 20 58 29 0a 20 20 2a 2a 20 20 20 20 20 52 4f 4c X). ** ROL
130f8 4c 42 41 43 4b 20 54 4f 20 73 70 3b 0a 20 20 2a LBACK TO sp;. *
130f9 2a 0a 20 20 2a 2a 20 49 66 20 28 58 3e 59 29 2c *. ** If (X>Y),
130fa 20 74 68 65 6e 20 77 68 65 6e 20 70 61 67 65 72 then when pager
130fb 53 74 72 65 73 73 20 69 73 20 63 61 6c 6c 65 64 Stress is called
130fc 20 70 61 67 65 20 58 20 77 69 6c 6c 20 6e 6f 74 page X will not
130fd 20 62 65 20 77 72 69 74 74 65 6e 0a 20 20 2a 2a be written. **
130fe 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 out to the data
130ff 62 61 73 65 20 66 69 6c 65 2c 20 62 75 74 20 77 base file, but w
13100 69 6c 6c 20 62 65 20 64 72 6f 70 70 65 64 20 66 ill be dropped f
13101 72 6f 6d 20 74 68 65 20 63 61 63 68 65 2e 20 54 rom the cache. T
13102 68 65 6e 2c 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 hen,. ** follow
13103 69 6e 67 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 ing the "ROLLBAC
13104 4b 20 54 4f 20 73 70 22 20 73 74 61 74 65 6d 65 K TO sp" stateme
13105 6e 74 2c 20 72 65 61 64 69 6e 67 20 70 61 67 65 nt, reading page
13106 20 58 20 77 69 6c 6c 20 72 65 61 64 0a 20 20 2a X will read. *
13107 2a 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 * data from the
13108 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 database file. T
13109 68 69 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20 his will be the
1310a 63 6f 70 79 20 6f 66 20 70 61 67 65 20 58 20 61 copy of page X a
1310b 73 20 69 74 0a 20 20 2a 2a 20 77 61 73 20 77 68 s it. ** was wh
1310c 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 en the transacti
1310d 6f 6e 20 73 74 61 72 74 65 64 2c 20 6e 6f 74 20 on started, not
1310e 61 73 20 69 74 20 77 61 73 20 77 68 65 6e 20 22 as it was when "
1310f 53 41 56 45 50 4f 49 4e 54 20 73 70 22 0a 20 20 SAVEPOINT sp".
13110 2a 2a 20 77 61 73 20 65 78 65 63 75 74 65 64 2e ** was executed.
13111 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 . **. ** The s
13112 6f 6c 75 74 69 6f 6e 20 69 73 20 74 6f 20 77 72 olution is to wr
13113 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ite the current
13114 64 61 74 61 20 66 6f 72 20 70 61 67 65 20 58 20 data for page X
13115 69 6e 74 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73 into the . ** s
13116 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ub-journal file
13117 6e 6f 77 20 28 69 66 20 69 74 20 69 73 20 6e 6f now (if it is no
13118 74 20 61 6c 72 65 61 64 79 20 74 68 65 72 65 29 t already there)
13119 2c 20 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c , so that it wil
1311a 6c 0a 20 20 2a 2a 20 62 65 20 72 65 73 74 6f 72 l. ** be restor
1311b 65 64 20 74 6f 20 69 74 73 20 63 75 72 72 65 6e ed to its curren
1311c 74 20 76 61 6c 75 65 20 77 68 65 6e 20 74 68 65 t value when the
1311d 20 22 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 "ROLLBACK TO sp
1311e 22 20 69 73 20 0a 20 20 2a 2a 20 65 78 65 63 75 " is . ** execu
1311f 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ted.. */. if(
13120 4e 45 56 45 52 28 0a 20 20 20 20 20 20 72 63 3d NEVER(. rc=
13121 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 =SQLITE_OK && pP
13122 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e g->pgno>pPager->
13123 64 62 53 69 7a 65 20 26 26 20 73 75 62 6a 52 65 dbSize && subjRe
13124 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 0a quiresPage(pPg).
13125 20 20 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ) ){. rc =
13126 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 subjournalPage(p
13127 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 Pg);. }.. /* W
13128 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 rite the content
13129 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 6f 75 s of the page ou
1312a 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 t to the databas
1312b 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 69 66 28 e file. */. if(
1312c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1312d 7b 0a 20 20 20 20 70 50 67 2d 3e 70 44 69 72 74 {. pPg->pDirt
1312e 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 y = 0;. rc =
1312f 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 pager_write_page
13130 6c 69 73 74 28 70 50 67 29 3b 0a 20 20 7d 0a 0a list(pPg);. }..
13131 20 20 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 /* Mark the pa
13132 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 2a 2f 0a ge as clean. */.
13133 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
13134 5f 4f 4b 20 29 7b 0a 20 20 20 20 50 41 47 45 52 _OK ){. PAGER
13135 54 52 41 43 45 28 28 22 53 54 52 45 53 53 20 25 TRACE(("STRESS %
13136 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 d page %d\n", PA
13137 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 GERID(pPager), p
13138 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 Pg->pgno));.
13139 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b sqlite3PcacheMak
1313a 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 7d eClean(pPg);. }
1313b 0a 0a 20 20 72 65 74 75 72 6e 20 70 61 67 65 72 .. return pager
1313c 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 _error(pPager, r
1313d 63 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c c);.}.../*.** Al
1313e 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e 69 74 69 locate and initi
1313f 61 6c 69 7a 65 20 61 20 6e 65 77 20 50 61 67 65 alize a new Page
13140 72 20 6f 62 6a 65 63 74 20 61 6e 64 20 70 75 74 r object and put
13141 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 a pointer to it
13142 0a 2a 2a 20 69 6e 20 2a 70 70 50 61 67 65 72 2e .** in *ppPager.
13143 20 54 68 65 20 70 61 67 65 72 20 73 68 6f 75 6c The pager shoul
13144 64 20 65 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 d eventually be
13145 66 72 65 65 64 20 62 79 20 70 61 73 73 69 6e 67 freed by passing
13146 20 69 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 it.** to sqlite
13147 33 50 61 67 65 72 43 6c 6f 73 65 28 29 2e 0a 2a 3PagerClose()..*
13148 2a 0a 2a 2a 20 54 68 65 20 7a 46 69 6c 65 6e 61 *.** The zFilena
13149 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 me argument is t
1314a 68 65 20 70 61 74 68 20 74 6f 20 74 68 65 20 64 he path to the d
1314b 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 atabase file to
1314c 6f 70 65 6e 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c open..** If zFil
1314d 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68 ename is NULL th
1314e 65 6e 20 61 20 72 61 6e 64 6f 6d 6c 79 2d 6e 61 en a randomly-na
1314f 6d 65 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 med temporary fi
13150 6c 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a le is created.**
13151 20 61 6e 64 20 75 73 65 64 20 61 73 20 74 68 65 and used as the
13152 20 66 69 6c 65 20 74 6f 20 62 65 20 63 61 63 68 file to be cach
13153 65 64 2e 20 54 65 6d 70 6f 72 61 72 79 20 66 69 ed. Temporary fi
13154 6c 65 73 20 61 72 65 20 62 65 20 64 65 6c 65 74 les are be delet
13155 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 ed.** automatica
13156 6c 6c 79 20 77 68 65 6e 20 74 68 65 79 20 61 72 lly when they ar
13157 65 20 63 6c 6f 73 65 64 2e 20 49 66 20 7a 46 69 e closed. If zFi
13158 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f lename is ":memo
13159 72 79 3a 22 20 74 68 65 6e 20 0a 2a 2a 20 61 6c ry:" then .** al
1315a 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 l information is
1315b 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 2e 20 held in cache.
1315c 49 74 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 It is never writ
1315d 74 65 6e 20 74 6f 20 64 69 73 6b 2e 20 0a 2a 2a ten to disk. .**
1315e 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 This can be use
1315f 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 d to implement a
13160 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
13161 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 base..**.** The
13162 6e 45 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 nExtra parameter
13163 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 6e specifies the n
13164 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
13165 66 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 f space allocate
13166 64 0a 2a 2a 20 61 6c 6f 6e 67 20 77 69 74 68 20 d.** along with
13167 65 61 63 68 20 70 61 67 65 20 72 65 66 65 72 65 each page refere
13168 6e 63 65 2e 20 54 68 69 73 20 73 70 61 63 65 20 nce. This space
13169 69 73 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 is available to
1316a 74 68 65 20 75 73 65 72 0a 2a 2a 20 76 69 61 20 the user.** via
1316b 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 the sqlite3Pager
1316c 47 65 74 45 78 74 72 61 28 29 20 41 50 49 2e 0a GetExtra() API..
1316d 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 **.** The flags
1316e 61 72 67 75 6d 65 6e 74 20 69 73 20 75 73 65 64 argument is used
1316f 20 74 6f 20 73 70 65 63 69 66 79 20 70 72 6f 70 to specify prop
13170 65 72 74 69 65 73 20 74 68 61 74 20 61 66 66 65 erties that affe
13171 63 74 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 ct the.** operat
13172 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 65 72 ion of the pager
13173 2e 20 49 74 20 73 68 6f 75 6c 64 20 62 65 20 70 . It should be p
13174 61 73 73 65 64 20 73 6f 6d 65 20 62 69 74 77 69 assed some bitwi
13175 73 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 0a 2a se combination.*
13176 2a 20 6f 66 20 74 68 65 20 50 41 47 45 52 5f 4f * of the PAGER_O
13177 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 61 6e 64 20 MIT_JOURNAL and
13178 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 PAGER_NO_READLOC
13179 4b 20 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 K flags..**.** T
1317a 68 65 20 76 66 73 46 6c 61 67 73 20 70 61 72 61 he vfsFlags para
1317b 6d 65 74 65 72 20 69 73 20 61 20 62 69 74 6d 61 meter is a bitma
1317c 73 6b 20 74 6f 20 70 61 73 73 20 74 6f 20 74 68 sk to pass to th
1317d 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 e flags paramete
1317e 72 0a 2a 2a 20 6f 66 20 74 68 65 20 78 4f 70 65 r.** of the xOpe
1317f 6e 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 n() method of th
13180 65 20 73 75 70 70 6c 69 65 64 20 56 46 53 20 77 e supplied VFS w
13181 68 65 6e 20 6f 70 65 6e 69 6e 67 20 66 69 6c 65 hen opening file
13182 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 s. .**.** If the
13183 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 69 73 pager object is
13184 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 allocated and t
13185 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c he specified fil
13186 65 20 6f 70 65 6e 65 64 20 0a 2a 2a 20 73 75 63 e opened .** suc
13187 63 65 73 73 66 75 6c 6c 79 2c 20 53 51 4c 49 54 cessfully, SQLIT
13188 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
13189 20 61 6e 64 20 2a 70 70 50 61 67 65 72 20 73 65 and *ppPager se
1318a 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 0a 2a 2a t to point to.**
1318b 20 74 68 65 20 6e 65 77 20 70 61 67 65 72 20 6f the new pager o
1318c 62 6a 65 63 74 2e 20 49 66 20 61 6e 20 65 72 72 bject. If an err
1318d 6f 72 20 6f 63 63 75 72 73 2c 20 2a 70 70 50 61 or occurs, *ppPa
1318e 67 65 72 20 69 73 20 73 65 74 20 74 6f 20 4e 55 ger is set to NU
1318f 4c 4c 0a 2a 2a 20 61 6e 64 20 65 72 72 6f 72 20 LL.** and error
13190 63 6f 64 65 20 72 65 74 75 72 6e 65 64 2e 20 54 code returned. T
13191 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 his function may
13192 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
13193 4f 4d 45 4d 0a 2a 2a 20 28 73 71 6c 69 74 65 33 OMEM.** (sqlite3
13194 4d 61 6c 6c 6f 63 28 29 20 69 73 20 75 73 65 64 Malloc() is used
13195 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d to allocate mem
13196 6f 72 79 29 2c 20 53 51 4c 49 54 45 5f 43 41 4e ory), SQLITE_CAN
13197 54 4f 50 45 4e 20 6f 72 20 0a 2a 2a 20 76 61 72 TOPEN or .** var
13198 69 6f 75 73 20 53 51 4c 49 54 45 5f 49 4f 5f 58 ious SQLITE_IO_X
13199 58 58 20 65 72 72 6f 72 73 2e 0a 2a 2f 0a 53 51 XX errors..*/.SQ
1319a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1319b 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 sqlite3PagerOpe
1319c 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 n(. sqlite3_vfs
1319d 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 2f 2a *pVfs, /*
1319e 20 54 68 65 20 76 69 72 74 75 61 6c 20 66 69 6c The virtual fil
1319f 65 20 73 79 73 74 65 6d 20 74 6f 20 75 73 65 20 e system to use
131a0 2a 2f 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 */. Pager **ppP
131a1 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a ager, /*
131a2 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 74 68 65 OUT: Return the
131a3 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 Pager structure
131a4 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 here */. const
131a5 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
131a6 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 , /* Name of t
131a7 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
131a8 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e to open */. in
131a9 74 20 6e 45 78 74 72 61 2c 20 20 20 20 20 20 20 t nExtra,
131aa 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 /* Extra
131ab 62 79 74 65 73 20 61 70 70 65 6e 64 20 74 6f 20 bytes append to
131ac 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 each in-memory p
131ad 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 age */. int fla
131ae 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 gs,
131af 20 20 2f 2a 20 66 6c 61 67 73 20 63 6f 6e 74 72 /* flags contr
131b0 6f 6c 6c 69 6e 67 20 74 68 69 73 20 66 69 6c 65 olling this file
131b1 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 */. int vfsFla
131b2 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f gs, /
131b3 2a 20 66 6c 61 67 73 20 70 61 73 73 65 64 20 74 * flags passed t
131b4 68 72 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65 hrough to sqlite
131b5 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 3_vfs.xOpen() */
131b6 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 69 6e 69 . void (*xReini
131b7 74 29 28 44 62 50 61 67 65 2a 29 20 2f 2a 20 46 t)(DbPage*) /* F
131b8 75 6e 63 74 69 6f 6e 20 74 6f 20 72 65 69 6e 69 unction to reini
131b9 74 69 61 6c 69 7a 65 20 70 61 67 65 73 20 2a 2f tialize pages */
131ba 0a 29 7b 0a 20 20 75 38 20 2a 70 50 74 72 3b 0a .){. u8 *pPtr;.
131bb 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 Pager *pPager
131bc 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 = 0; /* Pa
131bd 67 65 72 20 6f 62 6a 65 63 74 20 74 6f 20 61 6c ger object to al
131be 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 locate and retur
131bf 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 n */. int rc =
131c0 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
131c1 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
131c2 2f 0a 20 20 69 6e 74 20 74 65 6d 70 46 69 6c 65 /. int tempFile
131c3 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
131c4 54 72 75 65 20 66 6f 72 20 74 65 6d 70 20 66 69 True for temp fi
131c5 6c 65 73 20 28 69 6e 63 6c 2e 20 69 6e 2d 6d 65 les (incl. in-me
131c6 6d 6f 72 79 20 66 69 6c 65 73 29 20 2a 2f 0a 20 mory files) */.
131c7 20 69 6e 74 20 6d 65 6d 44 62 20 3d 20 30 3b 20 int memDb = 0;
131c8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
131c9 65 20 69 66 20 74 68 69 73 20 69 73 20 61 6e 20 e if this is an
131ca 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 2a in-memory file *
131cb 2f 0a 20 20 69 6e 74 20 72 65 61 64 4f 6e 6c 79 /. int readOnly
131cc 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
131cd 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 True if this is
131ce 61 20 72 65 61 64 2d 6f 6e 6c 79 20 66 69 6c 65 a read-only file
131cf 20 2a 2f 0a 20 20 69 6e 74 20 6a 6f 75 72 6e 61 */. int journa
131d0 6c 46 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 2f lFileSize; /
131d1 2a 20 42 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63 * Bytes to alloc
131d2 61 74 65 20 66 6f 72 20 65 61 63 68 20 6a 6f 75 ate for each jou
131d3 72 6e 61 6c 20 66 64 20 2a 2f 0a 20 20 63 68 61 rnal fd */. cha
131d4 72 20 2a 7a 50 61 74 68 6e 61 6d 65 20 3d 20 30 r *zPathname = 0
131d5 3b 20 20 20 20 20 2f 2a 20 46 75 6c 6c 20 70 61 ; /* Full pa
131d6 74 68 20 74 6f 20 64 61 74 61 62 61 73 65 20 66 th to database f
131d7 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 ile */. int nPa
131d8 74 68 6e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 thname = 0;
131d9 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
131da 79 74 65 73 20 69 6e 20 7a 50 61 74 68 6e 61 6d ytes in zPathnam
131db 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73 65 4a 6f e */. int useJo
131dc 75 72 6e 61 6c 20 3d 20 28 66 6c 61 67 73 20 26 urnal = (flags &
131dd 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 PAGER_OMIT_JOUR
131de 4e 41 4c 29 3d 3d 30 3b 20 2f 2a 20 46 61 6c 73 NAL)==0; /* Fals
131df 65 20 74 6f 20 6f 6d 69 74 20 6a 6f 75 72 6e 61 e to omit journa
131e0 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 52 65 61 l */. int noRea
131e1 64 6c 6f 63 6b 20 3d 20 28 66 6c 61 67 73 20 26 dlock = (flags &
131e2 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f PAGER_NO_READLO
131e3 43 4b 29 21 3d 30 3b 20 20 2f 2a 20 54 72 75 65 CK)!=0; /* True
131e4 20 74 6f 20 6f 6d 69 74 20 72 65 61 64 2d 6c 6f to omit read-lo
131e5 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 70 63 61 63 ck */. int pcac
131e6 68 65 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 heSize = sqlite3
131e7 50 63 61 63 68 65 53 69 7a 65 28 29 3b 20 20 20 PcacheSize();
131e8 20 20 20 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 /* Bytes to
131e9 61 6c 6c 6f 63 61 74 65 20 66 6f 72 20 50 43 61 allocate for PCa
131ea 63 68 65 20 2a 2f 0a 20 20 75 31 36 20 73 7a 50 che */. u16 szP
131eb 61 67 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 45 ageDflt = SQLITE
131ec 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 _DEFAULT_PAGE_SI
131ed 5a 45 3b 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 ZE; /* Default
131ee 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 page size */..
131ef 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f /* Figure out ho
131f0 77 20 6d 75 63 68 20 73 70 61 63 65 20 69 73 20 w much space is
131f1 72 65 71 75 69 72 65 64 20 66 6f 72 20 65 61 63 required for eac
131f2 68 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68 h journal file-h
131f3 61 6e 64 6c 65 0a 20 20 2a 2a 20 28 74 68 65 72 andle. ** (ther
131f4 65 20 61 72 65 20 74 77 6f 20 6f 66 20 74 68 65 e are two of the
131f5 6d 2c 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 m, the main jour
131f6 6e 61 6c 20 61 6e 64 20 74 68 65 20 73 75 62 2d nal and the sub-
131f7 6a 6f 75 72 6e 61 6c 29 2e 20 54 68 69 73 0a 20 journal). This.
131f8 20 2a 2a 20 69 73 20 74 68 65 20 6d 61 78 69 6d ** is the maxim
131f9 75 6d 20 73 70 61 63 65 20 72 65 71 75 69 72 65 um space require
131fa 64 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f d for an in-memo
131fb 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ry journal file
131fc 68 61 6e 64 6c 65 20 0a 20 20 2a 2a 20 61 6e 64 handle . ** and
131fd 20 61 20 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e a regular journ
131fe 61 6c 20 66 69 6c 65 2d 68 61 6e 64 6c 65 2e 20 al file-handle.
131ff 4e 6f 74 65 20 74 68 61 74 20 61 20 22 72 65 67 Note that a "reg
13200 75 6c 61 72 20 6a 6f 75 72 6e 61 6c 2d 68 61 6e ular journal-han
13201 64 6c 65 22 0a 20 20 2a 2a 20 6d 61 79 20 62 65 dle". ** may be
13202 20 61 20 77 72 61 70 70 65 72 20 63 61 70 61 62 a wrapper capab
13203 6c 65 20 6f 66 20 63 61 63 68 69 6e 67 20 74 68 le of caching th
13204 65 20 66 69 72 73 74 20 70 6f 72 74 69 6f 6e 20 e first portion
13205 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 of the journal.
13206 20 2a 2a 20 66 69 6c 65 20 69 6e 20 6d 65 6d 6f ** file in memo
13207 72 79 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 ry to implement
13208 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 the atomic-write
13209 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 28 73 optimization (s
1320a 65 65 20 0a 20 20 2a 2a 20 73 6f 75 72 63 65 20 ee . ** source
1320b 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63 29 2e file journal.c).
1320c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 . */. if( sqli
1320d 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 te3JournalSize(p
1320e 56 66 73 29 3e 73 71 6c 69 74 65 33 4d 65 6d 4a Vfs)>sqlite3MemJ
1320f 6f 75 72 6e 61 6c 53 69 7a 65 28 29 20 29 7b 0a ournalSize() ){.
13210 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 journalFileS
13211 69 7a 65 20 3d 20 52 4f 55 4e 44 38 28 73 71 6c ize = ROUND8(sql
13212 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 ite3JournalSize(
13213 70 56 66 73 29 29 3b 0a 20 20 7d 65 6c 73 65 7b pVfs));. }else{
13214 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 . journalFile
13215 53 69 7a 65 20 3d 20 52 4f 55 4e 44 38 28 73 71 Size = ROUND8(sq
13216 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 lite3MemJournalS
13217 69 7a 65 28 29 29 3b 0a 20 20 7d 0a 0a 20 20 2f ize());. }.. /
13218 2a 20 53 65 74 20 74 68 65 20 6f 75 74 70 75 74 * Set the output
13219 20 76 61 72 69 61 62 6c 65 20 74 6f 20 4e 55 4c variable to NUL
1321a 4c 20 69 6e 20 63 61 73 65 20 61 6e 20 65 72 72 L in case an err
1321b 6f 72 20 6f 63 63 75 72 73 2e 20 2a 2f 0a 20 20 or occurs. */.
1321c 2a 70 70 50 61 67 65 72 20 3d 20 30 3b 0a 0a 20 *ppPager = 0;..
1321d 20 2f 2a 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 /* Compute and
1321e 73 74 6f 72 65 20 74 68 65 20 66 75 6c 6c 20 70 store the full p
1321f 61 74 68 6e 61 6d 65 20 69 6e 20 61 6e 20 61 6c athname in an al
13220 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 70 located buffer p
13221 6f 69 6e 74 65 64 0a 20 20 2a 2a 20 74 6f 20 62 ointed. ** to b
13222 79 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6c 65 6e y zPathname, len
13223 67 74 68 20 6e 50 61 74 68 6e 61 6d 65 2e 20 4f gth nPathname. O
13224 72 2c 20 69 66 20 74 68 69 73 20 69 73 20 61 20 r, if this is a
13225 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2c 0a temporary file,.
13226 20 20 2a 2a 20 6c 65 61 76 65 20 62 6f 74 68 20 ** leave both
13227 6e 50 61 74 68 6e 61 6d 65 20 61 6e 64 20 7a 50 nPathname and zP
13228 61 74 68 6e 61 6d 65 20 73 65 74 20 74 6f 20 30 athname set to 0
13229 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69 .. */. if( zFi
1322a 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e lename && zFilen
1322b 61 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 20 6e 50 ame[0] ){. nP
1322c 61 74 68 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e athname = pVfs->
1322d 6d 78 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 mxPathname+1;.
1322e 20 20 7a 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 zPathname = sq
1322f 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 50 61 74 lite3Malloc(nPat
13230 68 6e 61 6d 65 2a 32 29 3b 0a 20 20 20 20 69 66 hname*2);. if
13231 28 20 7a 50 61 74 68 6e 61 6d 65 3d 3d 30 20 29 ( zPathname==0 )
13232 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
13233 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
13234 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
13235 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a E_OMIT_MEMORYDB.
13236 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a if( strcmp(z
13237 46 69 6c 65 6e 61 6d 65 2c 22 3a 6d 65 6d 6f 72 Filename,":memor
13238 79 3a 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 y:")==0 ){.
13239 20 6d 65 6d 44 62 20 3d 20 31 3b 0a 20 20 20 20 memDb = 1;.
1323a 20 20 7a 50 61 74 68 6e 61 6d 65 5b 30 5d 20 3d zPathname[0] =
1323b 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 0;. }else.#e
1323c 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 ndif. {.
1323d 20 7a 50 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 zPathname[0] =
1323e 30 3b 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 0; /* Make sure
1323f 69 6e 69 74 69 61 6c 69 7a 65 64 20 65 76 65 6e initialized even
13240 20 69 66 20 46 75 6c 6c 50 61 74 68 6e 61 6d 65 if FullPathname
13241 28 29 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 () fails */.
13242 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
13243 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 FullPathname(pVf
13244 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 50 s, zFilename, nP
13245 61 74 68 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 athname, zPathna
13246 6d 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 me);. }..
13247 6e 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 nPathname = sqli
13248 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 50 61 74 te3Strlen30(zPat
13249 68 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 hname);. if(
1324a 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1324b 20 6e 50 61 74 68 6e 61 6d 65 2b 38 3e 70 56 66 nPathname+8>pVf
1324c 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 29 7b s->mxPathname ){
1324d 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 . /* This b
1324e 72 61 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 77 ranch is taken w
1324f 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hen the journal
13250 70 61 74 68 20 72 65 71 75 69 72 65 64 20 62 79 path required by
13251 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 61 . ** the da
13252 74 61 62 61 73 65 20 62 65 69 6e 67 20 6f 70 65 tabase being ope
13253 6e 65 64 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 ned will be more
13254 20 74 68 61 6e 20 70 56 66 73 2d 3e 6d 78 50 61 than pVfs->mxPa
13255 74 68 6e 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 thname. **
13256 62 79 74 65 73 20 69 6e 20 6c 65 6e 67 74 68 2e bytes in length.
13257 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 65 20 This means the
13258 64 61 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 database cannot
13259 62 65 20 6f 70 65 6e 65 64 2c 0a 20 20 20 20 20 be opened,.
1325a 20 2a 2a 20 61 73 20 69 74 20 77 69 6c 6c 20 6e ** as it will n
1325b 6f 74 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74 ot be possible t
1325c 6f 20 6f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e o open the journ
1325d 61 6c 20 66 69 6c 65 20 6f 72 20 65 76 65 6e 0a al file or even.
1325e 20 20 20 20 20 20 2a 2a 20 63 68 65 63 6b 20 66 ** check f
1325f 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c or a hot-journal
13260 20 62 65 66 6f 72 65 20 72 65 61 64 69 6e 67 2e before reading.
13261 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
13262 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 rc = SQLITE_CANT
13263 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 OPEN;. }.
13264 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
13265 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 K ){. sqlit
13266 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d e3_free(zPathnam
13267 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e e);. return
13268 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a rc;. }. }..
13269 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 /* Allocate me
1326a 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 50 61 67 mory for the Pag
1326b 65 72 20 73 74 72 75 63 74 75 72 65 2c 20 50 43 er structure, PC
1326c 61 63 68 65 20 6f 62 6a 65 63 74 2c 20 74 68 65 ache object, the
1326d 0a 20 20 2a 2a 20 74 68 72 65 65 20 66 69 6c 65 . ** three file
1326e 20 64 65 73 63 72 69 70 74 6f 72 73 2c 20 74 68 descriptors, th
1326f 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
13270 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 6a 6f 75 name and the jou
13271 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 rnal . ** file
13272 6e 61 6d 65 2e 20 54 68 65 20 6c 61 79 6f 75 74 name. The layout
13273 20 69 6e 20 6d 65 6d 6f 72 79 20 69 73 20 61 73 in memory is as
13274 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20 follows:. **.
13275 20 2a 2a 20 20 20 20 20 50 61 67 65 72 20 6f 62 ** Pager ob
13276 6a 65 63 74 20 20 20 20 20 20 20 20 20 20 20 20 ject
13277 20 20 20 20 20 20 20 20 28 73 69 7a 65 6f 66 28 (sizeof(
13278 50 61 67 65 72 29 20 62 79 74 65 73 29 0a 20 20 Pager) bytes).
13279 2a 2a 20 20 20 20 20 50 43 61 63 68 65 20 6f 62 ** PCache ob
1327a 6a 65 63 74 20 20 20 20 20 20 20 20 20 20 20 20 ject
1327b 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 50 (sqlite3P
1327c 63 61 63 68 65 53 69 7a 65 28 29 20 62 79 74 65 cacheSize() byte
1327d 73 29 0a 20 20 2a 2a 20 20 20 20 20 44 61 74 61 s). ** Data
1327e 62 61 73 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 base file handle
1327f 20 20 20 20 20 20 20 20 20 20 20 20 28 70 56 66 (pVf
13280 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 62 79 74 65 s->szOsFile byte
13281 73 29 0a 20 20 2a 2a 20 20 20 20 20 53 75 62 2d s). ** Sub-
13282 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e journal file han
13283 64 6c 65 20 20 20 20 20 20 20 20 20 28 6a 6f 75 dle (jou
13284 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 62 79 74 rnalFileSize byt
13285 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 4d 61 69 es). ** Mai
13286 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 n journal file h
13287 61 6e 64 6c 65 20 20 20 20 20 20 20 20 28 6a 6f andle (jo
13288 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 62 79 urnalFileSize by
13289 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 44 61 tes). ** Da
1328a 74 61 62 61 73 65 20 66 69 6c 65 20 6e 61 6d 65 tabase file name
1328b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6e (n
1328c 50 61 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 73 Pathname+1 bytes
1328d 29 0a 20 20 2a 2a 20 20 20 20 20 4a 6f 75 72 6e ). ** Journ
1328e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 20 20 20 al file name
1328f 20 20 20 20 20 20 20 20 20 20 20 28 6e 50 61 74 (nPat
13290 68 6e 61 6d 65 2b 38 2b 31 20 62 79 74 65 73 29 hname+8+1 bytes)
13291 0a 20 20 2a 2f 0a 20 20 70 50 74 72 20 3d 20 28 . */. pPtr = (
13292 75 38 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c u8 *)sqlite3Mall
13293 6f 63 5a 65 72 6f 28 0a 20 20 20 20 52 4f 55 4e ocZero(. ROUN
13294 44 38 28 73 69 7a 65 6f 66 28 2a 70 50 61 67 65 D8(sizeof(*pPage
13295 72 29 29 20 2b 20 20 20 20 20 20 2f 2a 20 50 61 r)) + /* Pa
13296 67 65 72 20 73 74 72 75 63 74 75 72 65 20 2a 2f ger structure */
13297 0a 20 20 20 20 52 4f 55 4e 44 38 28 70 63 61 63 . ROUND8(pcac
13298 68 65 53 69 7a 65 29 20 2b 20 20 20 20 20 20 20 heSize) +
13299 20 20 20 20 2f 2a 20 50 43 61 63 68 65 20 6f 62 /* PCache ob
1329a 6a 65 63 74 20 2a 2f 0a 20 20 20 20 52 4f 55 4e ject */. ROUN
1329b 44 38 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c D8(pVfs->szOsFil
1329c 65 29 20 2b 20 20 20 20 20 20 20 2f 2a 20 54 68 e) + /* Th
1329d 65 20 6d 61 69 6e 20 64 62 20 66 69 6c 65 20 2a e main db file *
1329e 2f 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c /. journalFil
1329f 65 53 69 7a 65 20 2a 20 32 20 2b 20 20 20 20 20 eSize * 2 +
132a0 20 20 20 20 20 2f 2a 20 54 68 65 20 74 77 6f 20 /* The two
132a1 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f journal files */
132a2 20 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 . nPathname
132a3 2b 20 31 20 2b 20 20 20 20 20 20 20 20 20 20 20 + 1 +
132a4 20 20 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d /* zFilenam
132a5 65 20 2a 2f 0a 20 20 20 20 6e 50 61 74 68 6e 61 e */. nPathna
132a6 6d 65 20 2b 20 38 20 2b 20 31 20 20 20 20 20 20 me + 8 + 1
132a7 20 20 20 20 20 20 20 20 2f 2a 20 7a 4a 6f 75 72 /* zJour
132a8 6e 61 6c 20 2a 2f 0a 20 20 29 3b 0a 20 20 61 73 nal */. );. as
132a9 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 sert( EIGHT_BYTE
132aa 5f 41 4c 49 47 4e 4d 45 4e 54 28 53 51 4c 49 54 _ALIGNMENT(SQLIT
132ab 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 6a 6f 75 E_INT_TO_PTR(jou
132ac 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 29 20 29 rnalFileSize)) )
132ad 3b 0a 20 20 69 66 28 20 21 70 50 74 72 20 29 7b ;. if( !pPtr ){
132ae 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
132af 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 e(zPathname);.
132b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
132b1 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 50 61 NOMEM;. }. pPa
132b2 67 65 72 20 3d 20 20 20 20 20 20 20 20 20 20 20 ger =
132b3 20 20 20 28 50 61 67 65 72 2a 29 28 70 50 74 72 (Pager*)(pPtr
132b4 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 50 43 );. pPager->pPC
132b5 61 63 68 65 20 3d 20 20 20 20 28 50 43 61 63 68 ache = (PCach
132b6 65 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 4e e*)(pPtr += ROUN
132b7 44 38 28 73 69 7a 65 6f 66 28 2a 70 50 61 67 65 D8(sizeof(*pPage
132b8 72 29 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e r)));. pPager->
132b9 66 64 20 3d 20 20 20 28 73 71 6c 69 74 65 33 5f fd = (sqlite3_
132ba 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 52 file*)(pPtr += R
132bb 4f 55 4e 44 38 28 70 63 61 63 68 65 53 69 7a 65 OUND8(pcacheSize
132bc 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 6a ));. pPager->sj
132bd 66 64 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 fd = (sqlite3_fi
132be 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 le*)(pPtr += ROU
132bf 4e 44 38 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 ND8(pVfs->szOsFi
132c0 6c 65 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e le));. pPager->
132c1 6a 66 64 20 3d 20 20 28 73 71 6c 69 74 65 33 5f jfd = (sqlite3_
132c2 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 6a file*)(pPtr += j
132c3 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b ournalFileSize);
132c4 0a 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 . pPager->zFile
132c5 6e 61 6d 65 20 3d 20 20 20 20 28 63 68 61 72 2a name = (char*
132c6 29 28 70 50 74 72 20 2b 3d 20 6a 6f 75 72 6e 61 )(pPtr += journa
132c7 6c 46 69 6c 65 53 69 7a 65 29 3b 0a 20 20 61 73 lFileSize);. as
132c8 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 sert( EIGHT_BYTE
132c9 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 50 61 67 65 _ALIGNMENT(pPage
132ca 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 2f 2a r->jfd) );.. /*
132cb 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 50 61 67 Fill in the Pag
132cc 65 72 2e 7a 46 69 6c 65 6e 61 6d 65 20 61 6e 64 er.zFilename and
132cd 20 50 61 67 65 72 2e 7a 4a 6f 75 72 6e 61 6c 20 Pager.zJournal
132ce 62 75 66 66 65 72 73 2c 20 69 66 20 72 65 71 75 buffers, if requ
132cf 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 7a ired. */. if( z
132d0 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 Pathname ){.
132d1 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
132d2 20 3d 20 20 20 28 63 68 61 72 2a 29 28 70 50 74 = (char*)(pPt
132d3 72 20 2b 3d 20 6e 50 61 74 68 6e 61 6d 65 20 2b r += nPathname +
132d4 20 31 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 1);. memcpy(
132d5 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d pPager->zFilenam
132d6 65 2c 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6e 50 e, zPathname, nP
132d7 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65 athname);. me
132d8 6d 63 70 79 28 70 50 61 67 65 72 2d 3e 7a 4a 6f mcpy(pPager->zJo
132d9 75 72 6e 61 6c 2c 20 7a 50 61 74 68 6e 61 6d 65 urnal, zPathname
132da 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 , nPathname);.
132db 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 memcpy(&pPager
132dc 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b 6e 50 61 74 68 ->zJournal[nPath
132dd 6e 61 6d 65 5d 2c 20 22 2d 6a 6f 75 72 6e 61 6c name], "-journal
132de 22 2c 20 38 29 3b 0a 20 20 20 20 69 66 28 20 70 ", 8);. if( p
132df 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 Pager->zFilename
132e0 5b 30 5d 3d 3d 30 20 29 20 70 50 61 67 65 72 2d [0]==0 ) pPager-
132e1 3e 7a 4a 6f 75 72 6e 61 6c 5b 30 5d 20 3d 20 30 >zJournal[0] = 0
132e2 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 ;. sqlite3_fr
132e3 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 ee(zPathname);.
132e4 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 70 56 66 }. pPager->pVf
132e5 73 20 3d 20 70 56 66 73 3b 0a 20 20 70 50 61 67 s = pVfs;. pPag
132e6 65 72 2d 3e 76 66 73 46 6c 61 67 73 20 3d 20 76 er->vfsFlags = v
132e7 66 73 46 6c 61 67 73 3b 0a 0a 20 20 2f 2a 20 4f fsFlags;.. /* O
132e8 70 65 6e 20 74 68 65 20 70 61 67 65 72 20 66 69 pen the pager fi
132e9 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a le.. */. if( z
132ea 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c Filename && zFil
132eb 65 6e 61 6d 65 5b 30 5d 20 26 26 20 21 6d 65 6d ename[0] && !mem
132ec 44 62 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 6f Db ){. int fo
132ed 75 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ut = 0;
132ee 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 46 /* VF
132ef 53 20 66 6c 61 67 73 20 72 65 74 75 72 6e 65 64 S flags returned
132f0 20 62 79 20 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 by xOpen() */.
132f1 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
132f2 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 sOpen(pVfs, pPag
132f3 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 70 er->zFilename, p
132f4 50 61 67 65 72 2d 3e 66 64 2c 20 76 66 73 46 6c Pager->fd, vfsFl
132f5 61 67 73 2c 20 26 66 6f 75 74 29 3b 0a 20 20 20 ags, &fout);.
132f6 20 72 65 61 64 4f 6e 6c 79 20 3d 20 28 66 6f 75 readOnly = (fou
132f7 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 t&SQLITE_OPEN_RE
132f8 41 44 4f 4e 4c 59 29 3b 0a 0a 20 20 20 20 2f 2a ADONLY);.. /*
132f9 20 49 66 20 74 68 65 20 66 69 6c 65 20 77 61 73 If the file was
132fa 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 successfully op
132fb 65 6e 65 64 20 66 6f 72 20 72 65 61 64 2f 77 72 ened for read/wr
132fc 69 74 65 20 61 63 63 65 73 73 2c 0a 20 20 20 20 ite access,.
132fd 2a 2a 20 63 68 6f 6f 73 65 20 61 20 64 65 66 61 ** choose a defa
132fe 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 69 6e ult page size in
132ff 20 63 61 73 65 20 77 65 20 68 61 76 65 20 74 6f case we have to
13300 20 63 72 65 61 74 65 20 74 68 65 0a 20 20 20 20 create the.
13301 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
13302 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 70 61 . The default pa
13303 67 65 20 73 69 7a 65 20 69 73 20 74 68 65 20 6d ge size is the m
13304 61 78 69 6d 75 6d 20 6f 66 3a 0a 20 20 20 20 2a aximum of:. *
13305 2a 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 53 51 *. ** + SQ
13306 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 LITE_DEFAULT_PAG
13307 45 5f 53 49 5a 45 2c 0a 20 20 20 20 2a 2a 20 20 E_SIZE,. **
13308 20 20 2b 20 54 68 65 20 76 61 6c 75 65 20 72 65 + The value re
13309 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 turned by sqlite
1330a 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 3OsSectorSize().
1330b 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 ** + The
1330c 6c 61 72 67 65 73 74 20 70 61 67 65 20 73 69 7a largest page siz
1330d 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 77 72 e that can be wr
1330e 69 74 74 65 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 itten atomically
1330f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
13310 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
13311 26 20 21 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 & !readOnly ){.
13312 20 20 20 20 20 73 65 74 53 65 63 74 6f 72 53 69 setSectorSi
13313 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 ze(pPager);.
13314 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f assert(SQLITE_
13315 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a DEFAULT_PAGE_SIZ
13316 45 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 E<=SQLITE_MAX_DE
13317 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 29 FAULT_PAGE_SIZE)
13318 3b 0a 20 20 20 20 20 20 69 66 28 20 73 7a 50 61 ;. if( szPa
13319 67 65 44 66 6c 74 3c 70 50 61 67 65 72 2d 3e 73 geDflt<pPager->s
1331a 65 63 74 6f 72 53 69 7a 65 20 29 7b 0a 20 20 20 ectorSize ){.
1331b 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d if( pPager-
1331c 3e 73 65 63 74 6f 72 53 69 7a 65 3e 53 51 4c 49 >sectorSize>SQLI
1331d 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 TE_MAX_DEFAULT_P
1331e 41 47 45 5f 53 49 5a 45 20 29 7b 0a 20 20 20 20 AGE_SIZE ){.
1331f 20 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 szPageDflt
13320 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 = SQLITE_MAX_DE
13321 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b FAULT_PAGE_SIZE;
13322 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
13323 20 20 20 20 20 20 20 20 20 20 73 7a 50 61 67 65 szPage
13324 44 66 6c 74 20 3d 20 28 75 31 36 29 70 50 61 67 Dflt = (u16)pPag
13325 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3b 0a er->sectorSize;.
13326 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
13327 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f }.#ifdef SQLITE_
13328 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 ENABLE_ATOMIC_WR
13329 49 54 45 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 ITE. {.
1332a 20 20 20 20 69 6e 74 20 69 44 63 20 3d 20 73 71 int iDc = sq
1332b 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 lite3OsDeviceCha
1332c 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 racteristics(pPa
1332d 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 20 20 ger->fd);.
1332e 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 int ii;.
1332f 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f assert(SQLITE_
13330 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d IOCAP_ATOMIC512=
13331 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 =(512>>8));.
13332 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 assert(SQLIT
13333 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 E_IOCAP_ATOMIC64
13334 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b 0a K==(65536>>8));.
13335 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 53 assert(S
13336 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c QLITE_MAX_DEFAUL
13337 54 5f 50 41 47 45 5f 53 49 5a 45 3c 3d 36 35 35 T_PAGE_SIZE<=655
13338 33 36 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 36);. for
13339 28 69 69 3d 73 7a 50 61 67 65 44 66 6c 74 3b 20 (ii=szPageDflt;
1333a 69 69 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 44 ii<=SQLITE_MAX_D
1333b 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
1333c 3b 20 69 69 3d 69 69 2a 32 29 7b 0a 20 20 20 20 ; ii=ii*2){.
1333d 20 20 20 20 20 20 69 66 28 20 69 44 63 26 28 53 if( iDc&(S
1333e 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
1333f 49 43 7c 28 69 69 3e 3e 38 29 29 20 29 7b 0a 20 IC|(ii>>8)) ){.
13340 20 20 20 20 20 20 20 20 20 20 20 73 7a 50 61 67 szPag
13341 65 44 66 6c 74 20 3d 20 69 69 3b 0a 20 20 20 20 eDflt = ii;.
13342 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
13343 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
13344 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
13345 20 20 20 20 2f 2a 20 49 66 20 61 20 74 65 6d 70 /* If a temp
13346 6f 72 61 72 79 20 66 69 6c 65 20 69 73 20 72 65 orary file is re
13347 71 75 65 73 74 65 64 2c 20 69 74 20 69 73 20 6e quested, it is n
13348 6f 74 20 6f 70 65 6e 65 64 20 69 6d 6d 65 64 69 ot opened immedi
13349 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 49 6e ately.. ** In
1334a 20 74 68 69 73 20 63 61 73 65 20 77 65 20 61 63 this case we ac
1334b 63 65 70 74 20 74 68 65 20 64 65 66 61 75 6c 74 cept the default
1334c 20 70 61 67 65 20 73 69 7a 65 20 61 6e 64 20 64 page size and d
1334d 65 6c 61 79 20 61 63 74 75 61 6c 6c 79 0a 20 20 elay actually.
1334e 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 74 68 65 ** opening the
1334f 20 66 69 6c 65 20 75 6e 74 69 6c 20 74 68 65 20 file until the
13350 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 4f 73 first call to Os
13351 57 72 69 74 65 28 29 2e 0a 20 20 20 20 2a 2a 0a Write().. **.
13352 20 20 20 20 2a 2a 20 54 68 69 73 20 62 72 61 6e ** This bran
13353 63 68 20 69 73 20 61 6c 73 6f 20 72 75 6e 20 66 ch is also run f
13354 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 or an in-memory
13355 64 61 74 61 62 61 73 65 2e 20 41 6e 20 69 6e 2d database. An in-
13356 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 64 61 memory. ** da
13357 74 61 62 61 73 65 20 69 73 20 74 68 65 20 73 61 tabase is the sa
13358 6d 65 20 61 73 20 61 20 74 65 6d 70 2d 66 69 6c me as a temp-fil
13359 65 20 74 68 61 74 20 69 73 20 6e 65 76 65 72 20 e that is never
1335a 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 0a 20 written out to.
1335b 20 20 20 2a 2a 20 64 69 73 6b 20 61 6e 64 20 75 ** disk and u
1335c 73 65 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 ses an in-memory
1335d 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
1335e 6c 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20 74 l.. */ . t
1335f 65 6d 70 46 69 6c 65 20 3d 20 31 3b 0a 20 20 20 empFile = 1;.
13360 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d pPager->state =
13361 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 PAGER_EXCLUSIVE
13362 3b 0a 20 20 20 20 72 65 61 64 4f 6e 6c 79 20 3d ;. readOnly =
13363 20 28 76 66 73 46 6c 61 67 73 26 53 51 4c 49 54 (vfsFlags&SQLIT
13364 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 E_OPEN_READONLY)
13365 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 ;. }.. /* The
13366 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 74 following call t
13367 6f 20 50 61 67 65 72 53 65 74 50 61 67 65 73 69 o PagerSetPagesi
13368 7a 65 28 29 20 73 65 72 76 65 73 20 74 6f 20 73 ze() serves to s
13369 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 et the value of
1336a 0a 20 20 2a 2a 20 50 61 67 65 72 2e 70 61 67 65 . ** Pager.page
1336b 53 69 7a 65 20 61 6e 64 20 74 6f 20 61 6c 6c 6f Size and to allo
1336c 63 61 74 65 20 74 68 65 20 50 61 67 65 72 2e 70 cate the Pager.p
1336d 54 6d 70 53 70 61 63 65 20 62 75 66 66 65 72 2e TmpSpace buffer.
1336e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d . */. if( rc==
1336f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
13370 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13371 3e 6d 65 6d 44 62 3d 3d 30 20 29 3b 0a 20 20 20 >memDb==0 );.
13372 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
13373 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 50 erSetPagesize(pP
13374 61 67 65 72 2c 20 26 73 7a 50 61 67 65 44 66 6c ager, &szPageDfl
13375 74 2c 20 2d 31 29 3b 0a 20 20 20 20 74 65 73 74 t, -1);. test
13376 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 case( rc!=SQLITE
13377 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a _OK );. }.. /*
13378 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 If an error occ
13379 75 72 72 65 64 20 69 6e 20 65 69 74 68 65 72 20 urred in either
1337a 6f 66 20 74 68 65 20 62 6c 6f 63 6b 73 20 61 62 of the blocks ab
1337b 6f 76 65 2c 20 66 72 65 65 20 74 68 65 20 0a 20 ove, free the .
1337c 20 2a 2a 20 50 61 67 65 72 20 73 74 72 75 63 74 ** Pager struct
1337d 75 72 65 20 61 6e 64 20 63 6c 6f 73 65 20 74 68 ure and close th
1337e 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 e file.. */. i
1337f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
13380 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
13381 21 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 !pPager->pTmpSpa
13382 63 65 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 ce );. sqlite
13383 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3OsClose(pPager-
13384 3e 66 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 >fd);. sqlite
13385 33 5f 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a 3_free(pPager);.
13386 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
13387 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c }.. /* Initial
13388 69 7a 65 20 74 68 65 20 50 43 61 63 68 65 20 6f ize the PCache o
13389 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 61 73 73 65 bject. */. asse
1338a 72 74 28 20 6e 45 78 74 72 61 3c 31 30 30 30 20 rt( nExtra<1000
1338b 29 3b 0a 20 20 6e 45 78 74 72 61 20 3d 20 52 4f );. nExtra = RO
1338c 55 4e 44 38 28 6e 45 78 74 72 61 29 3b 0a 20 20 UND8(nExtra);.
1338d 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 sqlite3PcacheOpe
1338e 6e 28 73 7a 50 61 67 65 44 66 6c 74 2c 20 6e 45 n(szPageDflt, nE
1338f 78 74 72 61 2c 20 21 6d 65 6d 44 62 2c 0a 20 20 xtra, !memDb,.
13390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13391 20 20 21 6d 65 6d 44 62 3f 70 61 67 65 72 53 74 !memDb?pagerSt
13392 72 65 73 73 3a 30 2c 20 28 76 6f 69 64 20 2a 29 ress:0, (void *)
13393 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e pPager, pPager->
13394 70 50 43 61 63 68 65 29 3b 0a 0a 20 20 50 41 47 pPCache);.. PAG
13395 45 52 54 52 41 43 45 28 28 22 4f 50 45 4e 20 25 ERTRACE(("OPEN %
13396 64 20 25 73 5c 6e 22 2c 20 46 49 4c 45 48 41 4e d %s\n", FILEHAN
13397 44 4c 45 49 44 28 70 50 61 67 65 72 2d 3e 66 64 DLEID(pPager->fd
13398 29 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 ), pPager->zFile
13399 6e 61 6d 65 29 29 3b 0a 20 20 49 4f 54 52 41 43 name));. IOTRAC
1339a 45 28 28 22 4f 50 45 4e 20 25 70 20 25 73 5c 6e E(("OPEN %p %s\n
1339b 22 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 ", pPager, pPage
1339c 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 0a 0a r->zFilename))..
1339d 20 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 pPager->useJou
1339e 72 6e 61 6c 20 3d 20 28 75 38 29 75 73 65 4a 6f rnal = (u8)useJo
1339f 75 72 6e 61 6c 3b 0a 20 20 70 50 61 67 65 72 2d urnal;. pPager-
133a0 3e 6e 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 6e >noReadlock = (n
133a1 6f 52 65 61 64 6c 6f 63 6b 20 26 26 20 72 65 61 oReadlock && rea
133a2 64 4f 6e 6c 79 29 20 3f 31 3a 30 3b 0a 20 20 2f dOnly) ?1:0;. /
133a3 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70 * pPager->stmtOp
133a4 65 6e 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 en = 0; */. /*
133a5 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 pPager->stmtInUs
133a6 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 e = 0; */. /* p
133a7 50 61 67 65 72 2d 3e 6e 52 65 66 20 3d 20 30 3b Pager->nRef = 0;
133a8 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 */. pPager->db
133a9 53 69 7a 65 56 61 6c 69 64 20 3d 20 28 75 38 29 SizeValid = (u8)
133aa 6d 65 6d 44 62 3b 0a 20 20 2f 2a 20 70 50 61 67 memDb;. /* pPag
133ab 65 72 2d 3e 73 74 6d 74 53 69 7a 65 20 3d 20 30 er->stmtSize = 0
133ac 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 ; */. /* pPager
133ad 2d 3e 73 74 6d 74 4a 53 69 7a 65 20 3d 20 30 3b ->stmtJSize = 0;
133ae 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d */. /* pPager-
133af 3e 6e 50 61 67 65 20 3d 20 30 3b 20 2a 2f 0a 20 >nPage = 0; */.
133b0 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 pPager->mxPgno
133b1 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 = SQLITE_MAX_PAG
133b2 45 5f 43 4f 55 4e 54 3b 0a 20 20 2f 2a 20 70 50 E_COUNT;. /* pP
133b3 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 ager->state = PA
133b4 47 45 52 5f 55 4e 4c 4f 43 4b 3b 20 2a 2f 0a 20 GER_UNLOCK; */.
133b5 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
133b6 3e 73 74 61 74 65 20 3d 3d 20 28 74 65 6d 70 46 >state == (tempF
133b7 69 6c 65 20 3f 20 50 41 47 45 52 5f 45 58 43 4c ile ? PAGER_EXCL
133b8 55 53 49 56 45 20 3a 20 50 41 47 45 52 5f 55 4e USIVE : PAGER_UN
133b9 4c 4f 43 4b 29 20 29 3b 0a 20 20 2f 2a 20 70 50 LOCK) );. /* pP
133ba 61 67 65 72 2d 3e 65 72 72 4d 61 73 6b 20 3d 20 ager->errMask =
133bb 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 0; */. pPager->
133bc 74 65 6d 70 46 69 6c 65 20 3d 20 28 75 38 29 74 tempFile = (u8)t
133bd 65 6d 70 46 69 6c 65 3b 0a 20 20 61 73 73 65 72 empFile;. asser
133be 74 28 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 t( tempFile==PAG
133bf 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e ER_LOCKINGMODE_N
133c0 4f 52 4d 41 4c 20 0a 20 20 20 20 20 20 20 20 20 ORMAL .
133c1 20 7c 7c 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 || tempFile==PA
133c2 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
133c3 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 61 EXCLUSIVE );. a
133c4 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 ssert( PAGER_LOC
133c5 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 KINGMODE_EXCLUSI
133c6 56 45 3d 3d 31 20 29 3b 0a 20 20 70 50 61 67 65 VE==1 );. pPage
133c7 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
133c8 20 3d 20 28 75 38 29 74 65 6d 70 46 69 6c 65 3b = (u8)tempFile;
133c9 20 0a 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e . pPager->chan
133ca 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 50 geCountDone = pP
133cb 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a ager->tempFile;.
133cc 20 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 pPager->memDb
133cd 3d 20 28 75 38 29 6d 65 6d 44 62 3b 0a 20 20 70 = (u8)memDb;. p
133ce 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 Pager->readOnly
133cf 3d 20 28 75 38 29 72 65 61 64 4f 6e 6c 79 3b 0a = (u8)readOnly;.
133d0 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 65 65 /* pPager->nee
133d1 64 53 79 6e 63 20 3d 20 30 3b 20 2a 2f 0a 20 20 dSync = 0; */.
133d2 61 73 73 65 72 74 28 20 75 73 65 4a 6f 75 72 6e assert( useJourn
133d3 61 6c 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 al || pPager->te
133d4 6d 70 46 69 6c 65 20 29 3b 0a 20 20 70 50 61 67 mpFile );. pPag
133d5 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 70 50 61 er->noSync = pPa
133d6 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 ger->tempFile;.
133d7 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e pPager->fullSyn
133d8 63 20 3d 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 c = pPager->noSy
133d9 6e 63 20 3f 30 3a 31 3b 0a 20 20 70 50 61 67 65 nc ?0:1;. pPage
133da 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d 20 r->sync_flags =
133db 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d SQLITE_SYNC_NORM
133dc 41 4c 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d AL;. /* pPager-
133dd 3e 70 46 69 72 73 74 20 3d 20 30 3b 20 2a 2f 0a >pFirst = 0; */.
133de 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46 69 /* pPager->pFi
133df 72 73 74 53 79 6e 63 65 64 20 3d 20 30 3b 20 2a rstSynced = 0; *
133e0 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 /. /* pPager->p
133e1 4c 61 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 Last = 0; */. p
133e2 50 61 67 65 72 2d 3e 6e 45 78 74 72 61 20 3d 20 Pager->nExtra =
133e3 28 75 31 36 29 6e 45 78 74 72 61 3b 0a 20 20 70 (u16)nExtra;. p
133e4 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 Pager->journalSi
133e5 7a 65 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45 zeLimit = SQLITE
133e6 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c _DEFAULT_JOURNAL
133e7 5f 53 49 5a 45 5f 4c 49 4d 49 54 3b 0a 20 20 61 _SIZE_LIMIT;. a
133e8 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
133e9 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 74 65 6d ager->fd) || tem
133ea 70 46 69 6c 65 20 29 3b 0a 20 20 73 65 74 53 65 pFile );. setSe
133eb 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 ctorSize(pPager)
133ec 3b 0a 20 20 69 66 28 20 21 75 73 65 4a 6f 75 72 ;. if( !useJour
133ed 6e 61 6c 20 29 7b 0a 20 20 20 20 70 50 61 67 65 nal ){. pPage
133ee 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d r->journalMode =
133ef 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f PAGER_JOURNALMO
133f0 44 45 5f 4f 46 46 3b 0a 20 20 7d 65 6c 73 65 20 DE_OFF;. }else
133f1 69 66 28 20 6d 65 6d 44 62 20 29 7b 0a 20 20 20 if( memDb ){.
133f2 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
133f3 4d 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 Mode = PAGER_JOU
133f4 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 3b RNALMODE_MEMORY;
133f5 0a 20 20 7d 0a 20 20 2f 2a 20 70 50 61 67 65 72 . }. /* pPager
133f6 2d 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 20 3d ->xBusyHandler =
133f7 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 0; */. /* pPag
133f8 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 er->pBusyHandler
133f9 41 72 67 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 Arg = 0; */. pP
133fa 61 67 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 20 ager->xReiniter
133fb 3d 20 78 52 65 69 6e 69 74 3b 0a 20 20 2f 2a 20 = xReinit;. /*
133fc 6d 65 6d 73 65 74 28 70 50 61 67 65 72 2d 3e 61 memset(pPager->a
133fd 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Hash, 0, sizeof(
133fe 70 50 61 67 65 72 2d 3e 61 48 61 73 68 29 29 3b pPager->aHash));
133ff 20 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d */. *ppPager =
13400 20 70 50 61 67 65 72 3b 0a 20 20 72 65 74 75 72 pPager;. retur
13401 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
13402 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
13403 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
13404 61 66 74 65 72 20 74 72 61 6e 73 69 74 69 6f 6e after transition
13405 69 6e 67 20 66 72 6f 6d 20 50 41 47 45 52 5f 55 ing from PAGER_U
13406 4e 4c 4f 43 4b 20 74 6f 0a 2a 2a 20 50 41 47 45 NLOCK to.** PAGE
13407 52 5f 53 48 41 52 45 44 20 73 74 61 74 65 2e 20 R_SHARED state.
13408 49 74 20 74 65 73 74 73 20 69 66 20 74 68 65 72 It tests if ther
13409 65 20 69 73 20 61 20 68 6f 74 20 6a 6f 75 72 6e e is a hot journ
1340a 61 6c 20 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a al present in.**
1340b 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d the file-system
1340c 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 70 for the given p
1340d 61 67 65 72 2e 20 41 20 68 6f 74 20 6a 6f 75 72 ager. A hot jour
1340e 6e 61 6c 20 69 73 20 6f 6e 65 20 74 68 61 74 20 nal is one that
1340f 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 .** needs to be
13410 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 41 63 63 played back. Acc
13411 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20 66 ording to this f
13412 75 6e 63 74 69 6f 6e 2c 20 61 20 68 6f 74 2d 6a unction, a hot-j
13413 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 65 ournal.** file e
13414 78 69 73 74 73 20 69 66 20 74 68 65 20 66 6f 6c xists if the fol
13415 6c 6f 77 69 6e 67 20 63 72 69 74 65 72 69 61 20 lowing criteria
13416 61 72 65 20 6d 65 74 3a 0a 2a 2a 0a 2a 2a 20 20 are met:.**.**
13417 20 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 * The journal f
13418 69 6c 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 ile exists in th
13419 65 20 66 69 6c 65 20 73 79 73 74 65 6d 2c 20 61 e file system, a
1341a 6e 64 0a 2a 2a 20 20 20 2a 20 4e 6f 20 70 72 6f nd.** * No pro
1341b 63 65 73 73 20 68 6f 6c 64 73 20 61 20 52 45 53 cess holds a RES
1341c 45 52 56 45 44 20 6f 72 20 67 72 65 61 74 65 72 ERVED or greater
1341d 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
1341e 61 62 61 73 65 20 66 69 6c 65 2c 20 61 6e 64 0a abase file, and.
1341f 2a 2a 20 20 20 2a 20 54 68 65 20 64 61 74 61 62 ** * The datab
13420 61 73 65 20 66 69 6c 65 20 69 74 73 65 6c 66 20 ase file itself
13421 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
13422 30 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 0 bytes in size,
13423 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 and.** * The
13424 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 first byte of th
13425 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 e journal file e
13426 78 69 73 74 73 20 61 6e 64 20 69 73 20 6e 6f 74 xists and is not
13427 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 0x00..**.** If
13428 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 the current size
13429 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1342a 20 66 69 6c 65 20 69 73 20 30 20 62 75 74 20 61 file is 0 but a
1342b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a journal file.**
1342c 20 65 78 69 73 74 73 2c 20 74 68 61 74 20 69 73 exists, that is
1342d 20 70 72 6f 62 61 62 6c 79 20 61 6e 20 6f 6c 64 probably an old
1342e 20 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20 6f 76 journal left ov
1342f 65 72 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a er from a prior.
13430 2a 2a 20 64 61 74 61 62 61 73 65 20 77 69 74 68 ** database with
13431 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20 the same name.
13432 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
13433 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
13434 0a 2a 2a 20 6a 75 73 74 20 64 65 6c 65 74 65 64 .** just deleted
13435 20 75 73 69 6e 67 20 4f 73 44 65 6c 65 74 65 2c using OsDelete,
13436 20 2a 70 45 78 69 73 74 73 20 69 73 20 73 65 74 *pExists is set
13437 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 to 0 and SQLITE
13438 5f 4f 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e _OK.** is return
13439 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ed..**.** This r
1343a 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 outine does not
1343b 63 68 65 63 6b 20 69 66 20 74 68 65 72 65 20 69 check if there i
1343c 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e s a master journ
1343d 61 6c 20 66 69 6c 65 6e 61 6d 65 0a 2a 2a 20 61 al filename.** a
1343e 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
1343f 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 file. If there
13440 69 73 2c 20 61 6e 64 20 74 68 61 74 20 6d 61 73 is, and that mas
13441 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
13442 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 .** does not exi
13443 73 74 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 st, then the jou
13444 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 rnal file is not
13445 20 72 65 61 6c 6c 79 20 68 6f 74 2e 20 49 6e 20 really hot. In
13446 74 68 69 73 0a 2a 2a 20 63 61 73 65 20 74 68 69 this.** case thi
13447 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 s routine will r
13448 65 74 75 72 6e 20 61 20 66 61 6c 73 65 2d 70 6f eturn a false-po
13449 73 69 74 69 76 65 2e 20 54 68 65 20 70 61 67 65 sitive. The page
1344a 72 5f 70 6c 61 79 62 61 63 6b 28 29 0a 2a 2a 20 r_playback().**
1344b 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 69 73 routine will dis
1344c 63 6f 76 65 72 20 74 68 61 74 20 74 68 65 20 6a cover that the j
1344d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e ournal file is n
1344e 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 20 61 6e ot really hot an
1344f 64 20 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 d .** will not r
13450 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 0a 2a 2a oll it back. .**
13451 0a 2a 2a 20 49 66 20 61 20 68 6f 74 2d 6a 6f 75 .** If a hot-jou
13452 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 66 6f 75 rnal file is fou
13453 6e 64 20 74 6f 20 65 78 69 73 74 2c 20 2a 70 45 nd to exist, *pE
13454 78 69 73 74 73 20 69 73 20 73 65 74 20 74 6f 20 xists is set to
13455 31 20 61 6e 64 20 0a 2a 2a 20 53 51 4c 49 54 45 1 and .** SQLITE
13456 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 66 _OK returned. If
13457 20 6e 6f 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 no hot-journal
13458 66 69 6c 65 20 69 73 20 70 72 65 73 65 6e 74 2c file is present,
13459 20 2a 70 45 78 69 73 74 73 20 69 73 0a 2a 2a 20 *pExists is.**
1345a 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c set to 0 and SQL
1345b 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e ITE_OK returned.
1345c 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 If an IO error
1345d 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 occurs while try
1345e 69 6e 67 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d ing.** to determ
1345f 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e ine whether or n
13460 6f 74 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c ot a hot-journal
13461 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20 74 68 file exists, th
13462 65 20 49 4f 20 65 72 72 6f 72 0a 2a 2a 20 63 6f e IO error.** co
13463 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 de is returned a
13464 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 nd the value of
13465 2a 70 45 78 69 73 74 73 20 69 73 20 75 6e 64 65 *pExists is unde
13466 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 fined..*/.static
13467 20 69 6e 74 20 68 61 73 48 6f 74 4a 6f 75 72 6e int hasHotJourn
13468 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 al(Pager *pPager
13469 2c 20 69 6e 74 20 2a 70 45 78 69 73 74 73 29 7b , int *pExists){
1346a 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
1346b 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 const pVfs = pP
1346c 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 6e ager->pVfs;. in
1346d 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
1346e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
1346f 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
13470 69 6e 74 20 65 78 69 73 74 73 3b 20 20 20 20 20 int exists;
13471 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
13472 20 54 72 75 65 20 69 66 20 61 20 6a 6f 75 72 6e True if a journ
13473 61 6c 20 66 69 6c 65 20 69 73 20 70 72 65 73 65 al file is prese
13474 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 nt */.. assert(
13475 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 pPager!=0 );.
13476 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
13477 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 useJournal );.
13478 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
13479 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 Pager->fd) );.
1347a 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 6e 28 assert( !isOpen(
1347b 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a pPager->jfd) );.
1347c 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
1347d 2d 3e 73 74 61 74 65 20 3c 3d 20 50 41 47 45 52 ->state <= PAGER
1347e 5f 53 48 41 52 45 44 20 29 3b 0a 0a 20 20 2a 70 _SHARED );.. *p
1347f 45 78 69 73 74 73 20 3d 20 30 3b 0a 20 20 72 63 Exists = 0;. rc
13480 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 = sqlite3OsAcce
13481 73 73 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d ss(pVfs, pPager-
13482 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 54 >zJournal, SQLIT
13483 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c E_ACCESS_EXISTS,
13484 20 26 65 78 69 73 74 73 29 3b 0a 20 20 69 66 28 &exists);. if(
13485 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
13486 26 20 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20 & exists ){.
13487 69 6e 74 20 6c 6f 63 6b 65 64 3b 20 20 20 20 20 int locked;
13488 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
13489 72 75 65 20 69 66 20 73 6f 6d 65 20 70 72 6f 63 rue if some proc
1348a 65 73 73 20 68 6f 6c 64 73 20 61 20 52 45 53 45 ess holds a RESE
1348b 52 56 45 44 20 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 RVED lock */..
1348c 20 20 2f 2a 20 52 61 63 65 20 63 6f 6e 64 69 74 /* Race condit
1348d 69 6f 6e 20 68 65 72 65 3a 20 20 41 6e 6f 74 68 ion here: Anoth
1348e 65 72 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74 er process might
1348f 20 68 61 76 65 20 62 65 65 6e 20 68 6f 6c 64 69 have been holdi
13490 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 68 ng the. ** th
13491 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 e RESERVED lock
13492 61 6e 64 20 68 61 76 65 20 61 20 6a 6f 75 72 6e and have a journ
13493 61 6c 20 6f 70 65 6e 20 61 74 20 74 68 65 20 73 al open at the s
13494 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 29 qlite3OsAccess()
13495 20 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 61 62 . ** call ab
13496 6f 76 65 2c 20 62 75 74 20 74 68 65 6e 20 64 65 ove, but then de
13497 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c lete the journal
13498 20 61 6e 64 20 64 72 6f 70 20 74 68 65 20 6c 6f and drop the lo
13499 63 6b 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a ck before. **
1349a 20 77 65 20 67 65 74 20 74 6f 20 74 68 65 20 66 we get to the f
1349b 6f 6c 6c 6f 77 69 6e 67 20 73 71 6c 69 74 65 33 ollowing sqlite3
1349c 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c OsCheckReservedL
1349d 6f 63 6b 28 29 20 63 61 6c 6c 2e 20 20 49 66 20 ock() call. If
1349e 74 68 61 74 0a 20 20 20 20 2a 2a 20 69 73 20 74 that. ** is t
1349f 68 65 20 63 61 73 65 2c 20 74 68 69 73 20 72 6f he case, this ro
134a0 75 74 69 6e 65 20 6d 69 67 68 74 20 74 68 69 6e utine might thin
134a1 6b 20 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 k there is a hot
134a2 20 6a 6f 75 72 6e 61 6c 20 77 68 65 6e 0a 20 20 journal when.
134a3 20 20 2a 2a 20 69 6e 20 66 61 63 74 20 74 68 65 ** in fact the
134a4 72 65 20 69 73 20 6e 6f 6e 65 2e 20 20 54 68 69 re is none. Thi
134a5 73 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 66 s results in a f
134a6 61 6c 73 65 2d 70 6f 73 69 74 69 76 65 20 77 68 alse-positive wh
134a7 69 63 68 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 ich will. **
134a8 62 65 20 64 65 61 6c 74 20 77 69 74 68 20 62 79 be dealt with by
134a9 20 74 68 65 20 70 6c 61 79 62 61 63 6b 20 72 6f the playback ro
134aa 75 74 69 6e 65 2e 20 20 54 69 63 6b 65 74 20 23 utine. Ticket #
134ab 33 38 38 33 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 3883.. */.
134ac 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43 rc = sqlite3OsC
134ad 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
134ae 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 6c 6f (pPager->fd, &lo
134af 63 6b 65 64 29 3b 0a 20 20 20 20 69 66 28 20 72 cked);. if( r
134b0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
134b1 21 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 !locked ){.
134b2 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 20 int nPage;..
134b3 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 /* Check the
134b4 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 size of the data
134b5 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 69 74 base file. If it
134b6 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 30 20 70 consists of 0 p
134b7 61 67 65 73 2c 0a 20 20 20 20 20 20 2a 2a 20 74 ages,. ** t
134b8 68 65 6e 20 64 65 6c 65 74 65 20 74 68 65 20 6a hen delete the j
134b9 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 53 65 65 ournal file. See
134ba 20 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d the header comm
134bb 65 6e 74 20 61 62 6f 76 65 20 66 6f 72 20 0a 20 ent above for .
134bc 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65 61 73 ** the reas
134bd 6f 6e 69 6e 67 20 68 65 72 65 2e 20 20 44 65 6c oning here. Del
134be 65 74 65 20 74 68 65 20 6f 62 73 6f 6c 65 74 65 ete the obsolete
134bf 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 75 6e journal file un
134c0 64 65 72 0a 20 20 20 20 20 20 2a 2a 20 61 20 52 der. ** a R
134c1 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 74 6f 20 ESERVED lock to
134c2 61 76 6f 69 64 20 72 61 63 65 20 63 6f 6e 64 69 avoid race condi
134c3 74 69 6f 6e 73 20 61 6e 64 20 74 6f 20 61 76 6f tions and to avo
134c4 69 64 20 76 69 6f 6c 61 74 69 6e 67 0a 20 20 20 id violating.
134c5 20 20 20 2a 2a 20 5b 48 33 33 30 32 30 5d 2e 0a ** [H33020]..
134c6 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 */. r
134c7 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
134c8 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 Pagecount(pPager
134c9 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 20 , &nPage);.
134ca 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
134cb 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 OK ){. if
134cc 28 20 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 ( nPage==0 ){.
134cd 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 sqlite3B
134ce 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 eginBenignMalloc
134cf 28 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ();. if
134d0 28 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 ( sqlite3OsLock(
134d1 70 50 61 67 65 72 2d 3e 66 64 2c 20 52 45 53 45 pPager->fd, RESE
134d2 52 56 45 44 5f 4c 4f 43 4b 29 3d 3d 53 51 4c 49 RVED_LOCK)==SQLI
134d3 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
134d4 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 sqlite3OsDe
134d5 6c 65 74 65 28 70 56 66 73 2c 20 70 50 61 67 65 lete(pVfs, pPage
134d6 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b r->zJournal, 0);
134d7 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
134d8 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 50 61 ite3OsUnlock(pPa
134d9 67 65 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f ger->fd, SHARED_
134da 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 20 LOCK);.
134db 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c }. sql
134dc 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c ite3EndBenignMal
134dd 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 20 20 7d loc();. }
134de 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
134df 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 /* The journal f
134e0 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 20 6e ile exists and n
134e1 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 o other connecti
134e2 6f 6e 20 68 61 73 20 61 20 72 65 73 65 72 76 65 on has a reserve
134e3 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f d. ** o
134e4 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f r greater lock o
134e5 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
134e6 69 6c 65 2e 20 4e 6f 77 20 63 68 65 63 6b 20 74 ile. Now check t
134e7 68 61 74 20 74 68 65 72 65 20 69 73 0a 20 20 20 hat there is.
134e8 20 20 20 20 20 20 20 2a 2a 20 61 74 20 6c 65 61 ** at lea
134e9 73 74 20 6f 6e 65 20 6e 6f 6e 2d 7a 65 72 6f 20 st one non-zero
134ea 62 79 74 65 73 20 61 74 20 74 68 65 20 73 74 61 bytes at the sta
134eb 72 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 rt of the journa
134ec 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20 l file..
134ed 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 ** If there is
134ee 2c 20 74 68 65 6e 20 77 65 20 63 6f 6e 73 69 64 , then we consid
134ef 65 72 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 er this journal
134f0 74 6f 20 62 65 20 68 6f 74 2e 20 49 66 20 6e 6f to be hot. If no
134f1 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a t, . **
134f2 20 69 74 20 63 61 6e 20 62 65 20 69 67 6e 6f 72 it can be ignor
134f3 65 64 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f ed.. */
134f4 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 . int f
134f5 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 = SQLITE_OPEN_R
134f6 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f EADONLY|SQLITE_O
134f7 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c PEN_MAIN_JOURNAL
134f8 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d ;. rc =
134f9 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 sqlite3OsOpen(p
134fa 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f Vfs, pPager->zJo
134fb 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a urnal, pPager->j
134fc 66 64 2c 20 66 2c 20 26 66 29 3b 0a 20 20 20 20 fd, f, &f);.
134fd 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
134fe 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
134ff 20 20 20 20 20 20 20 75 38 20 66 69 72 73 74 20 u8 first
13500 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 = 0;.
13501 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 rc = sqlite3OsR
13502 65 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ead(pPager->jfd,
13503 20 28 76 6f 69 64 20 2a 29 26 66 69 72 73 74 2c (void *)&first,
13504 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 1, 0);.
13505 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
13506 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 TE_IOERR_SHORT_R
13507 45 41 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 EAD ){.
13508 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
13509 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 _OK;.
1350a 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 }. s
1350b 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 qlite3OsClose(pP
1350c 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 ager->jfd);.
1350d 20 20 20 20 20 20 20 20 2a 70 45 78 69 73 74 73 *pExists
1350e 20 3d 20 28 66 69 72 73 74 21 3d 30 29 3b 0a 20 = (first!=0);.
1350f 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 }else i
13510 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 41 f( rc==SQLITE_CA
13511 4e 54 4f 50 45 4e 20 29 7b 0a 20 20 20 20 20 20 NTOPEN ){.
13512 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 63 /* If we c
13513 61 6e 6e 6f 74 20 6f 70 65 6e 20 74 68 65 20 72 annot open the r
13514 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 ollback journal
13515 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 74 6f file in order to
13516 20 73 65 65 20 69 66 0a 20 20 20 20 20 20 20 20 see if.
13517 20 20 20 20 2a 2a 20 69 74 73 20 68 61 73 20 61 ** its has a
13518 20 7a 65 72 6f 20 68 65 61 64 65 72 2c 20 74 68 zero header, th
13519 61 74 20 6d 69 67 68 74 20 62 65 20 64 75 65 20 at might be due
1351a 74 6f 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 2c to an I/O error,
1351b 20 6f 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 or.
1351c 2a 2a 20 69 74 20 6d 69 67 68 74 20 62 65 20 64 ** it might be d
1351d 75 65 20 74 6f 20 74 68 65 20 72 61 63 65 20 63 ue to the race c
1351e 6f 6e 64 69 74 69 6f 6e 20 64 65 73 63 72 69 62 ondition describ
1351f 65 64 20 61 62 6f 76 65 20 61 6e 64 20 69 6e 0a ed above and in.
13520 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 ** t
13521 69 63 6b 65 74 20 23 33 38 38 33 2e 20 20 45 69 icket #3883. Ei
13522 74 68 65 72 20 77 61 79 2c 20 61 73 73 75 6d 65 ther way, assume
13523 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 that the journa
13524 6c 20 69 73 20 68 6f 74 2e 0a 20 20 20 20 20 20 l is hot..
13525 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 6d 69 ** This mi
13526 67 68 74 20 62 65 20 61 20 66 61 6c 73 65 20 70 ght be a false p
13527 6f 73 69 74 69 76 65 2e 20 20 42 75 74 20 69 66 ositive. But if
13528 20 69 74 20 69 73 2c 20 74 68 65 6e 20 74 68 65 it is, then the
13529 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 . **
1352a 61 75 74 6f 6d 61 74 69 63 20 6a 6f 75 72 6e 61 automatic journa
1352b 6c 20 70 6c 61 79 62 61 63 6b 20 61 6e 64 20 72 l playback and r
1352c 65 63 6f 76 65 72 79 20 6d 65 63 68 61 6e 69 73 ecovery mechanis
1352d 6d 20 77 69 6c 6c 20 64 65 61 6c 0a 20 20 20 20 m will deal.
1352e 20 20 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 ** with
1352f 69 74 20 75 6e 64 65 72 20 61 6e 20 45 58 43 4c it under an EXCL
13530 55 53 49 56 45 20 6c 6f 63 6b 20 77 68 65 72 65 USIVE lock where
13531 20 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 we do not need
13532 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a to. *
13533 2a 20 77 6f 72 72 79 20 73 6f 20 6d 75 63 68 20 * worry so much
13534 77 69 74 68 20 72 61 63 65 20 63 6f 6e 64 69 74 with race condit
13535 69 6f 6e 73 2e 0a 20 20 20 20 20 20 20 20 20 20 ions..
13536 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 */.
13537 20 2a 70 45 78 69 73 74 73 20 3d 20 31 3b 0a 20 *pExists = 1;.
13538 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
13539 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
1353a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
1353b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1353c 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }.. return rc;
1353d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 .}../*.** Read t
1353e 68 65 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 70 he content for p
1353f 61 67 65 20 70 50 67 20 6f 75 74 20 6f 66 20 74 age pPg out of t
13540 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
13541 20 61 6e 64 20 69 6e 74 6f 20 0a 2a 2a 20 70 50 and into .** pP
13542 67 2d 3e 70 44 61 74 61 2e 20 41 20 73 68 61 72 g->pData. A shar
13543 65 64 20 6c 6f 63 6b 20 6f 72 20 67 72 65 61 74 ed lock or great
13544 65 72 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 er must be held
13545 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a on the database.
13546 2a 2a 20 66 69 6c 65 20 62 65 66 6f 72 65 20 74 ** file before t
13547 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
13548 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 called..**.** If
13549 20 70 61 67 65 20 31 20 69 73 20 72 65 61 64 2c page 1 is read,
1354a 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 then the value
1354b 6f 66 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 of Pager.dbFileV
1354c 65 72 73 5b 5d 20 69 73 20 73 65 74 20 74 6f 0a ers[] is set to.
1354d 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 ** the value rea
1354e 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 d from the datab
1354f 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ase file..**.**
13550 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f If an IO error o
13551 63 63 75 72 73 2c 20 74 68 65 6e 20 74 68 65 20 ccurs, then the
13552 49 4f 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 IO error is retu
13553 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c rned to the call
13554 65 72 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 er..** Otherwise
13555 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 , SQLITE_OK is r
13556 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 eturned..*/.stat
13557 69 63 20 69 6e 74 20 72 65 61 64 44 62 50 61 67 ic int readDbPag
13558 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 e(PgHdr *pPg){.
13559 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d Pager *pPager =
1355a 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 20 2f 2a pPg->pPager; /*
1355b 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 61 73 Pager object as
1355c 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 61 sociated with pa
1355d 67 65 20 70 50 67 20 2a 2f 0a 20 20 50 67 6e 6f ge pPg */. Pgno
1355e 20 70 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e pgno = pPg->pgn
1355f 6f 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 o; /* Page
13560 20 6e 75 6d 62 65 72 20 74 6f 20 72 65 61 64 20 number to read
13561 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
13562 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13563 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
13564 20 2a 2f 0a 20 20 69 36 34 20 69 4f 66 66 73 65 */. i64 iOffse
13565 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
13566 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 /* Byte offse
13567 74 20 6f 66 20 66 69 6c 65 20 74 6f 20 72 65 61 t of file to rea
13568 64 20 66 72 6f 6d 20 2a 2f 0a 0a 20 20 61 73 73 d from */.. ass
13569 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
1356a 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 te>=PAGER_SHARED
1356b 20 26 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 && !MEMDB );.
1356c 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
1356d 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 0a 20 Pager->fd) );..
1356e 20 69 66 28 20 4e 45 56 45 52 28 21 69 73 4f 70 if( NEVER(!isOp
1356f 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 29 20 en(pPager->fd))
13570 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
13571 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
13572 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 );. memset(pP
13573 67 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 50 61 g->pData, 0, pPa
13574 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a ger->pageSize);.
13575 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
13576 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 4f 66 66 E_OK;. }. iOff
13577 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 set = (pgno-1)*(
13578 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65 i64)pPager->page
13579 53 69 7a 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c Size;. rc = sql
1357a 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 ite3OsRead(pPage
1357b 72 2d 3e 66 64 2c 20 70 50 67 2d 3e 70 44 61 74 r->fd, pPg->pDat
1357c 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 a, pPager->pageS
1357d 69 7a 65 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 ize, iOffset);.
1357e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1357f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 IOERR_SHORT_READ
13580 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
13581 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 ITE_OK;. }. if
13582 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 ( pgno==1 ){.
13583 20 75 38 20 2a 64 62 46 69 6c 65 56 65 72 73 20 u8 *dbFileVers
13584 3d 20 26 28 28 75 38 2a 29 70 50 67 2d 3e 70 44 = &((u8*)pPg->pD
13585 61 74 61 29 5b 32 34 5d 3b 0a 20 20 20 20 6d 65 ata)[24];. me
13586 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62 mcpy(&pPager->db
13587 46 69 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65 FileVers, dbFile
13588 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 70 50 61 Vers, sizeof(pPa
13589 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 ger->dbFileVers)
1358a 29 3b 0a 20 20 7d 0a 20 20 43 4f 44 45 43 31 28 );. }. CODEC1(
1358b 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 44 61 pPager, pPg->pDa
1358c 74 61 2c 20 70 67 6e 6f 2c 20 33 2c 20 72 63 20 ta, pgno, 3, rc
1358d 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b = SQLITE_NOMEM);
1358e 0a 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73 .. PAGER_INCR(s
1358f 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 72 65 61 qlite3_pager_rea
13590 64 64 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 50 41 ddb_count);. PA
13591 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d GER_INCR(pPager-
13592 3e 6e 52 65 61 64 29 3b 0a 20 20 49 4f 54 52 41 >nRead);. IOTRA
13593 43 45 28 28 22 50 47 49 4e 20 25 70 20 25 64 5c CE(("PGIN %p %d\
13594 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f n", pPager, pgno
13595 29 29 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 ));. PAGERTRACE
13596 28 28 22 46 45 54 43 48 20 25 64 20 70 61 67 65 (("FETCH %d page
13597 20 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e %d hash(%08x)\n
13598 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
13599 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 PAGERID(pPager
1359a 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70 ), pgno, pager_p
1359b 61 67 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a agehash(pPg)));.
1359c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1359d 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
1359e 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 tion is called t
1359f 6f 20 6f 62 74 61 69 6e 20 61 20 73 68 61 72 65 o obtain a share
135a0 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 d lock on the da
135a1 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 tabase file..**
135a2 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f It is illegal to
135a3 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 call sqlite3Pag
135a4 65 72 41 63 71 75 69 72 65 28 29 20 75 6e 74 69 erAcquire() unti
135a5 6c 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e l after this fun
135a6 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65 ction.** has bee
135a7 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 n successfully c
135a8 61 6c 6c 65 64 2e 20 49 66 20 61 20 73 68 61 72 alled. If a shar
135a9 65 64 2d 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 ed-lock is alrea
135aa 64 79 20 68 65 6c 64 20 77 68 65 6e 0a 2a 2a 20 dy held when.**
135ab 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
135ac 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 called, it is a
135ad 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 no-op..**.** Th
135ae 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 65 72 e following oper
135af 61 74 69 6f 6e 73 20 61 72 65 20 61 6c 73 6f 20 ations are also
135b0 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 performed by thi
135b1 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a s function..**.*
135b2 2a 20 20 20 31 29 20 49 66 20 74 68 65 20 70 61 * 1) If the pa
135b3 67 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ger is currently
135b4 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b in PAGER_UNLOCK
135b5 20 73 74 61 74 65 20 28 6e 6f 20 6c 6f 63 6b 20 state (no lock
135b6 68 65 6c 64 0a 2a 2a 20 20 20 20 20 20 6f 6e 20 held.** on
135b7 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
135b8 65 29 2c 20 74 68 65 6e 20 61 6e 20 61 74 74 65 e), then an atte
135b9 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f mpt is made to o
135ba 62 74 61 69 6e 20 61 0a 2a 2a 20 20 20 20 20 20 btain a.**
135bb 53 48 41 52 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 SHARED lock on t
135bc 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
135bd 2e 20 49 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 . Immediately af
135be 74 65 72 20 6f 62 74 61 69 6e 69 6e 67 0a 2a 2a ter obtaining.**
135bf 20 20 20 20 20 20 74 68 65 20 53 48 41 52 45 44 the SHARED
135c0 20 6c 6f 63 6b 2c 20 74 68 65 20 66 69 6c 65 2d lock, the file-
135c1 73 79 73 74 65 6d 20 69 73 20 63 68 65 63 6b 65 system is checke
135c2 64 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 d for a hot-jour
135c3 6e 61 6c 2c 0a 2a 2a 20 20 20 20 20 20 77 68 69 nal,.** whi
135c4 63 68 20 69 73 20 70 6c 61 79 65 64 20 62 61 63 ch is played bac
135c5 6b 20 69 66 20 70 72 65 73 65 6e 74 2e 20 46 6f k if present. Fo
135c6 6c 6c 6f 77 69 6e 67 20 61 6e 79 20 68 6f 74 2d llowing any hot-
135c7 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 journal .**
135c8 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20 63 rollback, the c
135c9 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 ontents of the c
135ca 61 63 68 65 20 61 72 65 20 76 61 6c 69 64 61 74 ache are validat
135cb 65 64 20 62 79 20 63 68 65 63 6b 69 6e 67 0a 2a ed by checking.*
135cc 2a 20 20 20 20 20 20 74 68 65 20 27 63 68 61 6e * the 'chan
135cd 67 65 2d 63 6f 75 6e 74 65 72 27 20 66 69 65 6c ge-counter' fiel
135ce 64 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 d of the databas
135cf 65 20 66 69 6c 65 20 68 65 61 64 65 72 20 61 6e e file header an
135d0 64 0a 2a 2a 20 20 20 20 20 20 64 69 73 63 61 72 d.** discar
135d1 64 65 64 20 69 66 20 74 68 65 79 20 61 72 65 20 ded if they are
135d2 66 6f 75 6e 64 20 74 6f 20 62 65 20 69 6e 76 61 found to be inva
135d3 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 lid..**.** 2)
135d4 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 If the pager is
135d5 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 running in exclu
135d6 73 69 76 65 2d 6d 6f 64 65 2c 20 61 6e 64 20 74 sive-mode, and t
135d7 68 65 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 here are current
135d8 6c 79 0a 2a 2a 20 20 20 20 20 20 6e 6f 20 6f 75 ly.** no ou
135d9 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 tstanding refere
135da 6e 63 65 73 20 74 6f 20 61 6e 79 20 70 61 67 65 nces to any page
135db 73 2c 20 61 6e 64 20 69 73 20 69 6e 20 74 68 65 s, and is in the
135dc 20 65 72 72 6f 72 20 73 74 61 74 65 2c 0a 2a 2a error state,.**
135dd 20 20 20 20 20 20 74 68 65 6e 20 61 6e 20 61 74 then an at
135de 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f tempt is made to
135df 20 63 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 clear the error
135e0 20 73 74 61 74 65 20 62 79 20 64 69 73 63 61 72 state by discar
135e1 64 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 ding.** the
135e2 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
135e3 20 70 61 67 65 20 63 61 63 68 65 20 61 6e 64 20 page cache and
135e4 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 6e 79 rolling back any
135e5 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a open journal.**
135e6 20 20 20 20 20 20 66 69 6c 65 2e 0a 2a 2a 0a 2a file..**.*
135e7 2a 20 49 66 20 74 68 65 20 6f 70 65 72 61 74 69 * If the operati
135e8 6f 6e 20 64 65 73 63 72 69 62 65 64 20 62 79 20 on described by
135e9 28 32 29 20 61 62 6f 76 65 20 69 73 20 6e 6f 74 (2) above is not
135ea 20 61 74 74 65 6d 70 74 65 64 2c 20 61 6e 64 20 attempted, and
135eb 69 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 if the.** pager
135ec 69 73 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 is in an error s
135ed 74 61 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20 tate other than
135ee 53 51 4c 49 54 45 5f 46 55 4c 4c 20 77 68 65 6e SQLITE_FULL when
135ef 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c this is called,
135f0 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 73 74 .** the error st
135f1 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 ate error code i
135f2 73 20 72 65 74 75 72 6e 65 64 2e 20 49 74 20 69 s returned. It i
135f3 73 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72 s permitted to r
135f4 65 61 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 ead the.** datab
135f5 61 73 65 20 77 68 65 6e 20 69 6e 20 53 51 4c 49 ase when in SQLI
135f6 54 45 5f 46 55 4c 4c 20 65 72 72 6f 72 20 73 74 TE_FULL error st
135f7 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 ate..**.** Other
135f8 77 69 73 65 2c 20 69 66 20 65 76 65 72 79 74 68 wise, if everyth
135f9 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 ing is successfu
135fa 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 l, SQLITE_OK is
135fb 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 0a returned. If an.
135fc 2a 2a 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 ** IO error occu
135fd 72 73 20 77 68 69 6c 65 20 6c 6f 63 6b 69 6e 67 rs while locking
135fe 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 63 the database, c
135ff 68 65 63 6b 69 6e 67 20 66 6f 72 20 61 20 68 6f hecking for a ho
13600 74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c t-journal.** fil
13601 65 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 e or rolling bac
13602 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 k a journal file
13603 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 , the IO error c
13604 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
13605 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
13606 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
13607 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 50 61 gerSharedLock(Pa
13608 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
13609 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1360a 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 OK;
1360b 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
1360c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72 72 e */. int isErr
1360d 6f 72 52 65 73 65 74 20 3d 20 30 3b 20 20 20 20 orReset = 0;
1360e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
1360f 65 20 69 66 20 72 65 63 6f 76 65 72 69 6e 67 20 e if recovering
13610 66 72 6f 6d 20 65 72 72 6f 72 20 73 74 61 74 65 from error state
13611 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 */.. /* This r
13612 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63 outine is only c
13613 61 6c 6c 65 64 20 66 72 6f 6d 20 62 2d 74 72 65 alled from b-tre
13614 65 20 61 6e 64 20 6f 6e 6c 79 20 77 68 65 6e 20 e and only when
13615 74 68 65 72 65 20 61 72 65 20 6e 6f 0a 20 20 2a there are no. *
13616 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 * outstanding pa
13617 67 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ges */. assert(
13618 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
13619 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 fCount(pPager->p
1361a 50 43 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20 PCache)==0 );.
1361b 69 66 28 20 4e 45 56 45 52 28 4d 45 4d 44 42 20 if( NEVER(MEMDB
1361c 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f && pPager->errCo
1361d 64 65 29 20 29 7b 20 72 65 74 75 72 6e 20 70 50 de) ){ return pP
1361e 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 20 7d ager->errCode; }
1361f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 64 .. /* If this d
13620 61 74 61 62 61 73 65 20 69 73 20 69 6e 20 61 6e atabase is in an
13621 20 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 6e 6f error-state, no
13622 77 20 69 73 20 61 20 63 68 61 6e 63 65 20 74 6f w is a chance to
13623 20 63 6c 65 61 72 0a 20 20 2a 2a 20 74 68 65 20 clear. ** the
13624 65 72 72 6f 72 2e 20 44 69 73 63 61 72 64 20 74 error. Discard t
13625 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
13626 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 61 he pager-cache a
13627 6e 64 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a nd rollback. **
13628 20 61 6e 79 20 68 6f 74 20 6a 6f 75 72 6e 61 6c any hot journal
13629 20 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 in the file-sys
1362a 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 tem.. */. if(
1362b 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
1362c 29 7b 0a 20 20 20 20 69 66 28 20 69 73 4f 70 65 ){. if( isOpe
1362d 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c n(pPager->jfd) |
1362e 7c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e | pPager->zJourn
1362f 61 6c 20 29 7b 0a 20 20 20 20 20 20 69 73 45 72 al ){. isEr
13630 72 6f 72 52 65 73 65 74 20 3d 20 31 3b 0a 20 20 rorReset = 1;.
13631 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e }. pPager->
13632 65 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 errCode = SQLITE
13633 5f 4f 4b 3b 0a 20 20 20 20 70 61 67 65 72 5f 72 _OK;. pager_r
13634 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 eset(pPager);.
13635 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d }.. if( pPager-
13636 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e >state==PAGER_UN
13637 4c 4f 43 4b 20 7c 7c 20 69 73 45 72 72 6f 72 52 LOCK || isErrorR
13638 65 73 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 eset ){. sqli
13639 74 65 33 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 te3_vfs * const
1363a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 pVfs = pPager->p
1363b 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 69 73 48 Vfs;. int isH
1363c 6f 74 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 otJournal = 0;.
1363d 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 assert( !MEMD
1363e 42 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 B );. assert(
1363f 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
13640 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 fCount(pPager->p
13641 50 43 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20 PCache)==0 );.
13642 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f if( pPager->no
13643 52 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 Readlock ){.
13644 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
13645 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 ->readOnly );.
13646 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 pPager->stat
13647 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44 e = PAGER_SHARED
13648 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
13649 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 rc = pager_wa
1364a 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 it_on_lock(pPage
1364b 72 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b r, SHARED_LOCK);
1364c 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
1364d 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1364e 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
1364f 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 er->state==PAGER
13650 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 _UNLOCK );.
13651 20 20 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f return pager_
13652 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 error(pPager, rc
13653 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
13654 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
13655 67 65 72 2d 3e 73 74 61 74 65 3e 3d 53 48 41 52 ger->state>=SHAR
13656 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 20 20 ED_LOCK );..
13657 2f 2a 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 20 /* If a journal
13658 66 69 6c 65 20 65 78 69 73 74 73 2c 20 61 6e 64 file exists, and
13659 20 74 68 65 72 65 20 69 73 20 6e 6f 20 52 45 53 there is no RES
1365a 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 ERVED lock on th
1365b 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 e. ** databas
1365c 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 e file, then it
1365d 65 69 74 68 65 72 20 6e 65 65 64 73 20 74 6f 20 either needs to
1365e 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f be played back o
1365f 72 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a r deleted.. *
13660 2f 0a 20 20 20 20 69 66 28 20 21 69 73 45 72 72 /. if( !isErr
13661 6f 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 20 orReset ){.
13662 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13663 3e 73 74 61 74 65 20 3c 3d 20 50 41 47 45 52 5f >state <= PAGER_
13664 53 48 41 52 45 44 20 29 3b 0a 20 20 20 20 20 20 SHARED );.
13665 72 63 20 3d 20 68 61 73 48 6f 74 4a 6f 75 72 6e rc = hasHotJourn
13666 61 6c 28 70 50 61 67 65 72 2c 20 26 69 73 48 6f al(pPager, &isHo
13667 74 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 tJournal);.
13668 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
13669 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
1366a 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 to failed;.
1366b 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
1366c 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 7c 7c isErrorReset ||
1366d 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 29 7b isHotJournal ){
1366e 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 20 61 6e . /* Get an
1366f 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 EXCLUSIVE lock
13670 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
13671 66 69 6c 65 2e 20 41 74 20 74 68 69 73 20 70 6f file. At this po
13672 69 6e 74 20 69 74 20 69 73 0a 20 20 20 20 20 20 int it is.
13673 2a 2a 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 ** important tha
13674 74 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 t a RESERVED loc
13675 6b 20 69 73 20 6e 6f 74 20 6f 62 74 61 69 6e 65 k is not obtaine
13676 64 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 d on the way to
13677 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 45 58 43 the. ** EXC
13678 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 LUSIVE lock. If
13679 69 74 20 77 65 72 65 2c 20 61 6e 6f 74 68 65 72 it were, another
1367a 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74 20 6f process might o
1367b 70 65 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a pen the. **
1367c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
1367d 64 65 74 65 63 74 20 74 68 65 20 52 45 53 45 52 detect the RESER
1367e 56 45 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 63 6f VED lock, and co
1367f 6e 63 6c 75 64 65 20 74 68 61 74 20 74 68 65 0a nclude that the.
13680 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 ** databas
13681 65 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 61 e is safe to rea
13682 64 20 77 68 69 6c 65 20 74 68 69 73 20 70 72 6f d while this pro
13683 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 72 6f cess is still ro
13684 6c 6c 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 lling the .
13685 20 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 ** hot-journal
13686 62 61 63 6b 2e 0a 20 20 20 20 20 20 2a 2a 20 0a back.. ** .
13687 20 20 20 20 20 20 2a 2a 20 42 65 63 61 75 73 65 ** Because
13688 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 the intermediat
13689 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 e RESERVED lock
1368a 69 73 20 6e 6f 74 20 72 65 71 75 65 73 74 65 64 is not requested
1368b 2c 20 61 6e 79 0a 20 20 20 20 20 20 2a 2a 20 6f , any. ** o
1368c 74 68 65 72 20 70 72 6f 63 65 73 73 20 61 74 74 ther process att
1368d 65 6d 70 74 69 6e 67 20 74 6f 20 61 63 63 65 73 empting to acces
1368e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 s the database f
1368f 69 6c 65 20 77 69 6c 6c 20 67 65 74 20 74 6f 20 ile will get to
13690 0a 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 70 . ** this p
13691 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65 oint in the code
13692 20 61 6e 64 20 66 61 69 6c 20 74 6f 20 6f 62 74 and fail to obt
13693 61 69 6e 20 69 74 73 20 6f 77 6e 20 45 58 43 4c ain its own EXCL
13694 55 53 49 56 45 20 6c 6f 63 6b 20 0a 20 20 20 20 USIVE lock .
13695 20 20 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 ** on the data
13696 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 20 base file..
13697 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 50 */. if( pP
13698 61 67 65 72 2d 3e 73 74 61 74 65 3c 45 58 43 4c ager->state<EXCL
13699 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 USIVE_LOCK ){.
1369a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1369b 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d e3OsLock(pPager-
1369c 3e 66 64 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c >fd, EXCLUSIVE_L
1369d 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 OCK);. if
1369e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1369f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
136a0 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 = pager_error(pP
136a1 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 ager, rc);.
136a2 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 goto failed
136a3 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
136a4 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 pPager->stat
136a5 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 e = PAGER_EXCLUS
136a6 49 56 45 3b 0a 20 20 20 20 20 20 7d 0a 20 0a 20 IVE;. }. .
136a7 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 /* Open the
136a8 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 65 61 journal for rea
136a9 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2e 20 d/write access.
136aa 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 This is because
136ab 69 6e 20 0a 20 20 20 20 20 20 2a 2a 20 65 78 63 in . ** exc
136ac 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 6d 6f lusive-access mo
136ad 64 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 de the file desc
136ae 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65 20 6b riptor will be k
136af 65 70 74 20 6f 70 65 6e 20 61 6e 64 0a 20 20 20 ept open and.
136b0 20 20 20 2a 2a 20 70 6f 73 73 69 62 6c 79 20 75 ** possibly u
136b1 73 65 64 20 66 6f 72 20 61 20 74 72 61 6e 73 61 sed for a transa
136b2 63 74 69 6f 6e 20 6c 61 74 65 72 20 6f 6e 2e 20 ction later on.
136b3 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c On some systems,
136b4 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 4f 73 the. ** Os
136b5 54 72 75 6e 63 61 74 65 28 29 20 63 61 6c 6c 20 Truncate() call
136b6 75 73 65 64 20 69 6e 20 65 78 63 6c 75 73 69 76 used in exclusiv
136b7 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 61 6c e-access mode al
136b8 73 6f 20 72 65 71 75 69 72 65 73 0a 20 20 20 20 so requires.
136b9 20 20 2a 2a 20 61 20 72 65 61 64 2f 77 72 69 74 ** a read/writ
136ba 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 20 e file handle..
136bb 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
136bc 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 ( !isOpen(pPager
136bd 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 ->jfd) ){.
136be 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 int res;.
136bf 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
136c0 73 41 63 63 65 73 73 28 70 56 66 73 2c 70 50 61 sAccess(pVfs,pPa
136c1 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 53 51 ger->zJournal,SQ
136c2 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 LITE_ACCESS_EXIS
136c3 54 53 2c 26 72 65 73 29 3b 0a 20 20 20 20 20 20 TS,&res);.
136c4 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
136c5 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
136c6 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 if( res ){.
136c7 20 20 20 20 20 20 20 20 69 6e 74 20 66 6f 75 74 int fout
136c8 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
136c9 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45 int f = SQLITE
136ca 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c _OPEN_READWRITE|
136cb 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
136cc 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 _JOURNAL;.
136cd 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 assert( !p
136ce 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
136cf 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 );. r
136d0 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 c = sqlite3OsOpe
136d1 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e n(pVfs, pPager->
136d2 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 zJournal, pPager
136d3 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 6f 75 74 29 ->jfd, f, &fout)
136d4 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 ;. as
136d5 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
136d6 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70 50 _OK || isOpen(pP
136d7 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 ager->jfd) );.
136d8 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
136d9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 66 ==SQLITE_OK && f
136da 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f out&SQLITE_OPEN_
136db 52 45 41 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 READONLY ){.
136dc 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
136dd 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a QLITE_CANTOPEN;.
136de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 sq
136df 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 lite3OsClose(pPa
136e0 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 ger->jfd);.
136e1 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
136e2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
136e3 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
136e4 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 journal does not
136e5 20 65 78 69 73 74 2c 20 69 74 20 75 73 75 61 6c exist, it usual
136e6 6c 79 20 6d 65 61 6e 73 20 74 68 61 74 20 73 6f ly means that so
136e7 6d 65 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 me .
136e8 2a 2a 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 ** other connect
136e9 69 6f 6e 20 6d 61 6e 61 67 65 64 20 74 6f 20 67 ion managed to g
136ea 65 74 20 69 6e 20 61 6e 64 20 72 6f 6c 6c 20 69 et in and roll i
136eb 74 20 62 61 63 6b 20 62 65 66 6f 72 65 20 0a 20 t back before .
136ec 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 ** th
136ed 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 is connection ob
136ee 74 61 69 6e 65 64 20 74 68 65 20 65 78 63 6c 75 tained the exclu
136ef 73 69 76 65 20 6c 6f 63 6b 20 61 62 6f 76 65 2e sive lock above.
136f0 20 4f 72 2c 20 69 74 20 0a 20 20 20 20 20 20 20 Or, it .
136f1 20 20 20 20 20 2a 2a 20 6d 61 79 20 6d 65 61 6e ** may mean
136f2 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72 20 that the pager
136f3 77 61 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72 was in the error
136f4 2d 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 -state when this
136f5 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 . **
136f6 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c function was cal
136f7 6c 65 64 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 led and the jour
136f8 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f nal file does no
136f9 74 20 65 78 69 73 74 2e 20 20 2a 2f 0a 20 20 20 t exist. */.
136fa 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 rc = pa
136fb 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 ger_end_transact
136fc 69 6f 6e 28 70 50 61 67 65 72 2c 20 30 29 3b 0a ion(pPager, 0);.
136fd 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
136fe 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
136ff 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
13700 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
13701 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 goto failed;.
13702 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 }.. /*
13703 54 4f 44 4f 3a 20 57 68 79 20 61 72 65 20 74 68 TODO: Why are th
13704 65 73 65 20 63 6c 65 61 72 65 64 20 68 65 72 65 ese cleared here
13705 3f 20 49 73 20 69 74 20 6e 65 63 65 73 73 61 72 ? Is it necessar
13706 79 3f 20 2a 2f 0a 20 20 20 20 20 20 70 50 61 67 y? */. pPag
13707 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 er->journalStart
13708 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 ed = 0;. pP
13709 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
1370a 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 = 0;. pPag
1370b 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 er->setMaster =
1370c 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 0;. pPager-
1370d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 3b >journalHdr = 0;
1370e 0a 20 0a 20 20 20 20 20 20 2f 2a 20 50 6c 61 79 . . /* Play
1370f 62 61 63 6b 20 61 6e 64 20 64 65 6c 65 74 65 20 back and delete
13710 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 72 the journal. Dr
13711 6f 70 20 74 68 65 20 64 61 74 61 62 61 73 65 20 op the database
13712 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 6c write. ** l
13713 6f 63 6b 20 61 6e 64 20 72 65 61 63 71 75 69 72 ock and reacquir
13714 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e e the read lock.
13715 20 50 75 72 67 65 20 74 68 65 20 63 61 63 68 65 Purge the cache
13716 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20 2a 2a before. **
13717 20 70 6c 61 79 69 6e 67 20 62 61 63 6b 20 74 68 playing back th
13718 65 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 73 6f e hot-journal so
13719 20 74 68 61 74 20 77 65 20 64 6f 6e 27 74 20 65 that we don't e
1371a 6e 64 20 75 70 20 77 69 74 68 0a 20 20 20 20 20 nd up with.
1371b 20 2a 2a 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 ** an inconsist
1371c 65 6e 74 20 63 61 63 68 65 2e 0a 20 20 20 20 20 ent cache..
1371d 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 73 */. if( is
1371e 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
1371f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 ) ){. rc
13720 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b = pager_playback
13721 28 70 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 (pPager, 1);.
13722 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
13723 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
13724 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 rc = pager_e
13725 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 rror(pPager, rc)
13726 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f ;. goto
13727 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 failed;.
13728 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
13729 20 61 73 73 65 72 74 28 20 28 70 50 61 67 65 72 assert( (pPager
1372a 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 ->state==PAGER_S
1372b 48 41 52 45 44 29 0a 20 20 20 20 20 20 20 20 20 HARED).
1372c 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 65 78 || (pPager->ex
1372d 63 6c 75 73 69 76 65 4d 6f 64 65 20 26 26 20 70 clusiveMode && p
1372e 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 50 41 47 Pager->state>PAG
1372f 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20 20 20 ER_SHARED).
13730 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 );. }.. i
13731 66 28 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b f( pPager->pBack
13732 75 70 20 7c 7c 20 73 71 6c 69 74 65 33 50 63 61 up || sqlite3Pca
13733 63 68 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 chePagecount(pPa
13734 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20 ger->pPCache)>0
13735 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ){. /* The
13736 73 68 61 72 65 64 2d 6c 6f 63 6b 20 68 61 73 20 shared-lock has
13737 6a 75 73 74 20 62 65 65 6e 20 61 63 71 75 69 72 just been acquir
13738 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ed on the databa
13739 73 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a se file. **
1373a 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 61 and there are a
1373b 6c 72 65 61 64 79 20 70 61 67 65 73 20 69 6e 20 lready pages in
1373c 74 68 65 20 63 61 63 68 65 20 28 66 72 6f 6d 20 the cache (from
1373d 61 20 70 72 65 76 69 6f 75 73 0a 20 20 20 20 20 a previous.
1373e 20 2a 2a 20 72 65 61 64 20 6f 72 20 77 72 69 74 ** read or writ
1373f 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 e transaction).
13740 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 Check to see if
13741 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 the database.
13742 20 20 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 ** has been
13743 6d 6f 64 69 66 69 65 64 2e 20 20 49 66 20 74 68 modified. If th
13744 65 20 64 61 74 61 62 61 73 65 20 68 61 73 20 63 e database has c
13745 68 61 6e 67 65 64 2c 20 66 6c 75 73 68 20 74 68 hanged, flush th
13746 65 0a 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 e. ** cache
13747 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 .. **.
13748 20 2a 2a 20 44 61 74 61 62 61 73 65 20 63 68 61 ** Database cha
13749 6e 67 65 73 20 69 73 20 64 65 74 65 63 74 65 64 nges is detected
1374a 20 62 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 31 by looking at 1
1374b 35 20 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e 5 bytes beginnin
1374c 67 0a 20 20 20 20 20 20 2a 2a 20 61 74 20 6f 66 g. ** at of
1374d 66 73 65 74 20 32 34 20 69 6e 74 6f 20 74 68 65 fset 24 into the
1374e 20 66 69 6c 65 2e 20 20 54 68 65 20 66 69 72 73 file. The firs
1374f 74 20 34 20 6f 66 20 74 68 65 73 65 20 31 36 20 t 4 of these 16
13750 62 79 74 65 73 20 61 72 65 0a 20 20 20 20 20 20 bytes are.
13751 2a 2a 20 61 20 33 32 2d 62 69 74 20 63 6f 75 6e ** a 32-bit coun
13752 74 65 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 ter that is incr
13753 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 emented with eac
13754 68 20 63 68 61 6e 67 65 2e 20 20 54 68 65 0a 20 h change. The.
13755 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 62 79 ** other by
13756 74 65 73 20 63 68 61 6e 67 65 20 72 61 6e 64 6f tes change rando
13757 6d 6c 79 20 77 69 74 68 20 65 61 63 68 20 66 69 mly with each fi
13758 6c 65 20 63 68 61 6e 67 65 20 77 68 65 6e 0a 20 le change when.
13759 20 20 20 20 20 2a 2a 20 61 20 63 6f 64 65 63 20 ** a codec
1375a 69 73 20 69 6e 20 75 73 65 2e 0a 20 20 20 20 20 is in use..
1375b 20 2a 2a 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 ** . ** Th
1375c 65 72 65 20 69 73 20 61 20 76 61 6e 69 73 68 69 ere is a vanishi
1375d 6e 67 6c 79 20 73 6d 61 6c 6c 20 63 68 61 6e 63 ngly small chanc
1375e 65 20 74 68 61 74 20 61 20 63 68 61 6e 67 65 20 e that a change
1375f 77 69 6c 6c 20 6e 6f 74 20 62 65 20 0a 20 20 20 will not be .
13760 20 20 20 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 ** detected.
13761 20 54 68 65 20 63 68 61 6e 63 65 20 6f 66 20 61 The chance of a
13762 6e 20 75 6e 64 65 74 65 63 74 65 64 20 63 68 61 n undetected cha
13763 6e 67 65 20 69 73 20 73 6f 20 73 6d 61 6c 6c 20 nge is so small
13764 74 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 69 74 that. ** it
13765 20 63 61 6e 20 62 65 20 6e 65 67 6c 65 63 74 65 can be neglecte
13766 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 d.. */.
13767 20 20 63 68 61 72 20 64 62 46 69 6c 65 56 65 72 char dbFileVer
13768 73 5b 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d s[sizeof(pPager-
13769 3e 64 62 46 69 6c 65 56 65 72 73 29 5d 3b 0a 20 >dbFileVers)];.
1376a 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
1376b 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 rPagecount(pPage
1376c 72 2c 20 30 29 3b 0a 0a 20 20 20 20 20 20 69 66 r, 0);.. if
1376d 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 ( pPager->errCod
1376e 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 e ){. rc
1376f 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 = pPager->errCod
13770 65 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 e;. goto
13771 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a failed;. }.
13772 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
13773 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c Pager->dbSizeVal
13774 69 64 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 id );. if(
13775 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e 30 pPager->dbSize>0
13776 20 29 7b 0a 20 20 20 20 20 20 20 20 49 4f 54 52 ){. IOTR
13777 41 43 45 28 28 22 43 4b 56 45 52 53 20 25 70 20 ACE(("CKVERS %p
13778 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 73 %d\n", pPager, s
13779 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 izeof(dbFileVers
1377a 29 29 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 )));. rc
1377b 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 = sqlite3OsRead(
1377c 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 64 62 46 pPager->fd, &dbF
1377d 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 ileVers, sizeof(
1377e 64 62 46 69 6c 65 56 65 72 73 29 2c 20 32 34 29 dbFileVers), 24)
1377f 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
13780 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
13781 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 goto fa
13782 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a iled;. }.
13783 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
13784 20 20 20 20 20 6d 65 6d 73 65 74 28 64 62 46 69 memset(dbFi
13785 6c 65 56 65 72 73 2c 20 30 2c 20 73 69 7a 65 6f leVers, 0, sizeo
13786 66 28 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a f(dbFileVers));.
13787 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 }.. i
13788 66 28 20 6d 65 6d 63 6d 70 28 70 50 61 67 65 72 f( memcmp(pPager
13789 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 64 62 ->dbFileVers, db
1378a 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 FileVers, sizeof
1378b 28 64 62 46 69 6c 65 56 65 72 73 29 29 21 3d 30 (dbFileVers))!=0
1378c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 ){. page
1378d 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b r_reset(pPager);
1378e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1378f 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
13790 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
13791 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 || pPager->stat
13792 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 e==PAGER_SHARED
13793 29 3b 0a 20 20 7d 0a 0a 20 66 61 69 6c 65 64 3a );. }.. failed:
13794 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
13795 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 70 E_OK ){. /* p
13796 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 ager_unlock() is
13797 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 65 78 63 a no-op for exc
13798 6c 75 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 lusive mode and
13799 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
1379a 73 65 73 2e 20 2a 2f 0a 20 20 20 20 70 61 67 65 ses. */. page
1379b 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 r_unlock(pPager)
1379c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1379d 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 c;.}../*.** If t
1379e 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 he reference cou
1379f 6e 74 20 68 61 73 20 72 65 61 63 68 65 64 20 7a nt has reached z
137a0 65 72 6f 2c 20 72 6f 6c 6c 62 61 63 6b 20 61 6e ero, rollback an
137a1 79 20 61 63 74 69 76 65 0a 2a 2a 20 74 72 61 6e y active.** tran
137a2 73 61 63 74 69 6f 6e 20 61 6e 64 20 75 6e 6c 6f saction and unlo
137a3 63 6b 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2a ck the pager..**
137a4 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 6e 20 6c .** Except, in l
137a5 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c ocking_mode=EXCL
137a6 55 53 49 56 45 20 77 68 65 6e 20 74 68 65 72 65 USIVE when there
137a7 20 69 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 69 is nothing to i
137a8 6e 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 n.** the rollbac
137a9 6b 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 20 75 k journal, the u
137aa 6e 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 70 65 72 nlock is not per
137ab 66 6f 72 6d 65 64 20 61 6e 64 20 74 68 65 72 65 formed and there
137ac 20 69 73 0a 2a 2a 20 6e 6f 74 68 69 6e 67 20 74 is.** nothing t
137ad 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 73 6f 20 74 o rollback, so t
137ae 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
137af 20 6e 6f 2d 6f 70 2e 0a 2a 2f 20 0a 73 74 61 74 no-op..*/ .stat
137b0 69 63 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c ic void pagerUnl
137b1 6f 63 6b 49 66 55 6e 75 73 65 64 28 50 61 67 65 ockIfUnused(Page
137b2 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 r *pPager){. if
137b3 28 20 28 73 71 6c 69 74 65 33 50 63 61 63 68 65 ( (sqlite3Pcache
137b4 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d RefCount(pPager-
137b5 3e 70 50 43 61 63 68 65 29 3d 3d 30 29 0a 20 20 >pPCache)==0).
137b6 20 26 26 20 28 21 70 50 61 67 65 72 2d 3e 65 78 && (!pPager->ex
137b7 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 70 clusiveMode || p
137b8 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
137b9 66 3e 30 29 20 0a 20 20 29 7b 0a 20 20 20 20 70 f>0) . ){. p
137ba 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c agerUnlockAndRol
137bb 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 lback(pPager);.
137bc 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 }.}../*.** Acqu
137bd 69 72 65 20 61 20 72 65 66 65 72 65 6e 63 65 20 ire a reference
137be 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 20 70 to page number p
137bf 67 6e 6f 20 69 6e 20 70 61 67 65 72 20 70 50 61 gno in pager pPa
137c0 67 65 72 20 28 61 20 70 61 67 65 0a 2a 2a 20 72 ger (a page.** r
137c1 65 66 65 72 65 6e 63 65 20 68 61 73 20 74 79 70 eference has typ
137c2 65 20 44 62 50 61 67 65 2a 29 2e 20 49 66 20 74 e DbPage*). If t
137c3 68 65 20 72 65 71 75 65 73 74 65 64 20 72 65 66 he requested ref
137c4 65 72 65 6e 63 65 20 69 73 20 0a 2a 2a 20 73 75 erence is .** su
137c5 63 63 65 73 73 66 75 6c 6c 79 20 6f 62 74 61 69 ccessfully obtai
137c6 6e 65 64 2c 20 69 74 20 69 73 20 63 6f 70 69 65 ned, it is copie
137c7 64 20 74 6f 20 2a 70 70 50 61 67 65 20 61 6e 64 d to *ppPage and
137c8 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 SQLITE_OK retur
137c9 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ned..**.** If th
137ca 65 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 e requested page
137cb 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 is already in t
137cc 68 65 20 63 61 63 68 65 2c 20 69 74 20 69 73 20 he cache, it is
137cd 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 20 4f 74 returned. .** Ot
137ce 68 65 72 77 69 73 65 2c 20 61 20 6e 65 77 20 70 herwise, a new p
137cf 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 61 6c age object is al
137d0 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75 located and popu
137d1 6c 61 74 65 64 20 77 69 74 68 20 64 61 74 61 0a lated with data.
137d2 2a 2a 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 ** read from the
137d3 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
137d4 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 2c 20 74 In some cases, t
137d5 68 65 20 70 63 61 63 68 65 20 6d 6f 64 75 6c 65 he pcache module
137d6 20 6d 61 79 0a 2a 2a 20 63 68 6f 6f 73 65 20 6e may.** choose n
137d7 6f 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 ot to allocate a
137d8 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 new page object
137d9 20 61 6e 64 20 6d 61 79 20 72 65 75 73 65 20 61 and may reuse a
137da 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 6f 62 n existing.** ob
137db 6a 65 63 74 20 77 69 74 68 20 6e 6f 20 6f 75 74 ject with no out
137dc 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e standing referen
137dd 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 ces..**.** The e
137de 78 74 72 61 20 64 61 74 61 20 61 70 70 65 6e 64 xtra data append
137df 65 64 20 74 6f 20 61 20 70 61 67 65 20 69 73 20 ed to a page is
137e0 61 6c 77 61 79 73 20 69 6e 69 74 69 61 6c 69 7a always initializ
137e1 65 64 20 74 6f 20 7a 65 72 6f 73 20 74 68 65 20 ed to zeros the
137e2 0a 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20 61 .** first time a
137e3 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 page is loaded
137e4 69 6e 74 6f 20 6d 65 6d 6f 72 79 2e 20 49 66 20 into memory. If
137e5 74 68 65 20 70 61 67 65 20 72 65 71 75 65 73 74 the page request
137e6 65 64 20 69 73 20 0a 2a 2a 20 61 6c 72 65 61 64 ed is .** alread
137e7 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 77 y in the cache w
137e8 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
137e9 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 n is called, the
137ea 6e 20 74 68 65 20 65 78 74 72 61 0a 2a 2a 20 64 n the extra.** d
137eb 61 74 61 20 69 73 20 6c 65 66 74 20 61 73 20 69 ata is left as i
137ec 74 20 77 61 73 20 77 68 65 6e 20 74 68 65 20 70 t was when the p
137ed 61 67 65 20 6f 62 6a 65 63 74 20 77 61 73 20 6c age object was l
137ee 61 73 74 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 ast used..**.**
137ef 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 If the database
137f0 69 6d 61 67 65 20 69 73 20 73 6d 61 6c 6c 65 72 image is smaller
137f1 20 74 68 61 6e 20 74 68 65 20 72 65 71 75 65 73 than the reques
137f2 74 65 64 20 70 61 67 65 20 6f 72 20 69 66 20 61 ted page or if a
137f3 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 .** non-zero va
137f4 6c 75 65 20 69 73 20 70 61 73 73 65 64 20 61 73 lue is passed as
137f5 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 70 the noContent p
137f6 61 72 61 6d 65 74 65 72 20 61 6e 64 20 74 68 65 arameter and the
137f7 20 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 20 70 .** requested p
137f8 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 age is not alrea
137f9 64 79 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 dy stored in the
137fa 20 63 61 63 68 65 2c 20 74 68 65 6e 20 6e 6f 20 cache, then no
137fb 0a 2a 2a 20 61 63 74 75 61 6c 20 64 69 73 6b 20 .** actual disk
137fc 72 65 61 64 20 6f 63 63 75 72 73 2e 20 49 6e 20 read occurs. In
137fd 74 68 69 73 20 63 61 73 65 20 74 68 65 20 6d 65 this case the me
137fe 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20 74 68 mory image of th
137ff 65 20 0a 2a 2a 20 70 61 67 65 20 69 73 20 69 6e e .** page is in
13800 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 6c 6c itialized to all
13801 20 7a 65 72 6f 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 zeros. .**.** I
13802 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74 f noContent is t
13803 72 75 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 rue, it means th
13804 61 74 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 at we do not car
13805 65 20 61 62 6f 75 74 20 74 68 65 20 63 6f 6e 74 e about the cont
13806 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 ents.** of the p
13807 61 67 65 2e 20 54 68 69 73 20 6f 63 63 75 72 73 age. This occurs
13808 20 69 6e 20 74 77 6f 20 73 65 70 65 72 61 74 65 in two seperate
13809 20 73 63 65 6e 61 72 69 6f 73 3a 0a 2a 2a 0a 2a scenarios:.**.*
1380a 2a 20 20 20 61 29 20 57 68 65 6e 20 72 65 61 64 * a) When read
1380b 69 6e 67 20 61 20 66 72 65 65 2d 6c 69 73 74 20 ing a free-list
1380c 6c 65 61 66 20 70 61 67 65 20 66 72 6f 6d 20 74 leaf page from t
1380d 68 65 20 64 61 74 61 62 61 73 65 2c 20 61 6e 64 he database, and
1380e 0a 2a 2a 0a 2a 2a 20 20 20 62 29 20 57 68 65 6e .**.** b) When
1380f 20 61 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 a savepoint is
13810 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 being rolled bac
13811 6b 20 61 6e 64 20 77 65 20 6e 65 65 64 20 74 6f k and we need to
13812 20 6c 6f 61 64 0a 2a 2a 20 20 20 20 20 20 61 20 load.** a
13813 6e 65 77 20 70 61 67 65 20 69 6e 74 6f 20 74 68 new page into th
13814 65 20 63 61 63 68 65 20 74 6f 20 70 6f 70 75 6c e cache to popul
13815 61 74 65 20 77 69 74 68 20 74 68 65 20 64 61 74 ate with the dat
13816 61 20 72 65 61 64 0a 2a 2a 20 20 20 20 20 20 66 a read.** f
13817 72 6f 6d 20 74 68 65 20 73 61 76 65 70 6f 69 6e rom the savepoin
13818 74 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a t journal..**.**
13819 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 If noContent is
1381a 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 true, then the
1381b 64 61 74 61 20 72 65 74 75 72 6e 65 64 20 69 73 data returned is
1381c 20 7a 65 72 6f 65 64 20 69 6e 73 74 65 61 64 20 zeroed instead
1381d 6f 66 0a 2a 2a 20 62 65 69 6e 67 20 72 65 61 64 of.** being read
1381e 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 from the databa
1381f 73 65 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 se. Additionally
13820 2c 20 74 68 65 20 62 69 74 73 20 63 6f 72 72 65 , the bits corre
13821 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 70 sponding.** to p
13822 67 6e 6f 20 69 6e 20 50 61 67 65 72 2e 70 49 6e gno in Pager.pIn
13823 4a 6f 75 72 6e 61 6c 20 28 62 69 74 76 65 63 20 Journal (bitvec
13824 6f 66 20 70 61 67 65 73 20 61 6c 72 65 61 64 79 of pages already
13825 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a written to the.
13826 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 ** journal file)
13827 20 61 6e 64 20 74 68 65 20 50 61 67 65 72 53 61 and the PagerSa
13828 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 vepoint.pInSavep
13829 6f 69 6e 74 20 62 69 74 76 65 63 73 20 6f 66 20 oint bitvecs of
1382a 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 73 61 76 65 any open.** save
1382b 70 6f 69 6e 74 73 20 61 72 65 20 73 65 74 2e 20 points are set.
1382c 54 68 69 73 20 6d 65 61 6e 73 20 69 66 20 74 68 This means if th
1382d 65 20 70 61 67 65 20 69 73 20 6d 61 64 65 20 77 e page is made w
1382e 72 69 74 61 62 6c 65 20 61 74 20 61 6e 79 0a 2a ritable at any.*
1382f 2a 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 * point in the f
13830 75 74 75 72 65 2c 20 75 73 69 6e 67 20 61 20 63 uture, using a c
13831 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 50 61 all to sqlite3Pa
13832 67 65 72 57 72 69 74 65 28 29 2c 20 69 74 73 20 gerWrite(), its
13833 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 77 69 6c 6c contents.** will
13834 20 6e 6f 74 20 62 65 20 6a 6f 75 72 6e 61 6c 65 not be journale
13835 64 2e 20 54 68 69 73 20 73 61 76 65 73 20 49 4f d. This saves IO
13836 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 71 75 ..**.** The acqu
13837 69 73 69 74 69 6f 6e 20 6d 69 67 68 74 20 66 61 isition might fa
13838 69 6c 20 66 6f 72 20 73 65 76 65 72 61 6c 20 72 il for several r
13839 65 61 73 6f 6e 73 2e 20 20 49 6e 20 61 6c 6c 20 easons. In all
1383a 63 61 73 65 73 2c 0a 2a 2a 20 61 6e 20 61 70 70 cases,.** an app
1383b 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 ropriate error c
1383c 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 ode is returned
1383d 61 6e 64 20 2a 70 70 50 61 67 65 20 69 73 20 73 and *ppPage is s
1383e 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a et to NULL..**.*
1383f 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 * See also sqlit
13840 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 2e e3PagerLookup().
13841 20 20 42 6f 74 68 20 74 68 69 73 20 72 6f 75 74 Both this rout
13842 69 6e 65 20 61 6e 64 20 4c 6f 6f 6b 75 70 28 29 ine and Lookup()
13843 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 66 attempt.** to f
13844 69 6e 64 20 61 20 70 61 67 65 20 69 6e 20 74 68 ind a page in th
13845 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 e in-memory cach
13846 65 20 66 69 72 73 74 2e 20 20 49 66 20 74 68 65 e first. If the
13847 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 page is not alr
13848 65 61 64 79 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 eady.** in memor
13849 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 y, this routine
1384a 67 6f 65 73 20 74 6f 20 64 69 73 6b 20 74 6f 20 goes to disk to
1384b 72 65 61 64 20 69 74 20 69 6e 20 77 68 65 72 65 read it in where
1384c 61 73 20 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 6a as Lookup().** j
1384d 75 73 74 20 72 65 74 75 72 6e 73 20 30 2e 20 20 ust returns 0.
1384e 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 71 This routine acq
1384f 75 69 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 uires a read-loc
13850 6b 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 k the first time
13851 20 69 74 0a 2a 2a 20 68 61 73 20 74 6f 20 67 6f it.** has to go
13852 20 74 6f 20 64 69 73 6b 2c 20 61 6e 64 20 63 6f to disk, and co
13853 75 6c 64 20 61 6c 73 6f 20 70 6c 61 79 62 61 63 uld also playbac
13854 6b 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c k an old journal
13855 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a if necessary..*
13856 2a 20 53 69 6e 63 65 20 4c 6f 6f 6b 75 70 28 29 * Since Lookup()
13857 20 6e 65 76 65 72 20 67 6f 65 73 20 74 6f 20 64 never goes to d
13858 69 73 6b 2c 20 69 74 20 6e 65 76 65 72 20 68 61 isk, it never ha
13859 73 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 6c s to deal with l
1385a 6f 63 6b 73 0a 2a 2a 20 6f 72 20 6a 6f 75 72 6e ocks.** or journ
1385b 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c al files..*/.SQL
1385c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1385d 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 sqlite3PagerAcqu
1385e 69 72 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 ire(. Pager *pP
1385f 61 67 65 72 2c 20 20 20 20 20 20 2f 2a 20 54 68 ager, /* Th
13860 65 20 70 61 67 65 72 20 6f 70 65 6e 20 6f 6e 20 e pager open on
13861 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
13862 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f e */. Pgno pgno
13863 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 , /* Pa
13864 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 66 65 74 ge number to fet
13865 63 68 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a ch */. DbPage *
13866 2a 70 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 57 *ppPage, /* W
13867 72 69 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74 rite a pointer t
13868 6f 20 74 68 65 20 70 61 67 65 20 68 65 72 65 20 o the page here
13869 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 */. int noConte
1386a 6e 74 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e nt /* Do n
1386b 6f 74 20 62 6f 74 68 65 72 20 72 65 61 64 69 6e ot bother readin
1386c 67 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 64 g content from d
1386d 69 73 6b 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 isk if true */.)
1386e 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 {. int rc;. Pg
1386f 48 64 72 20 2a 70 50 67 3b 0a 0a 20 20 61 73 73 Hdr *pPg;.. ass
13870 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 67 65 ert( assert_page
13871 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 29 20 r_state(pPager)
13872 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
13873 67 65 72 2d 3e 73 74 61 74 65 3e 50 41 47 45 52 ger->state>PAGER
13874 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 0a 20 20 69 66 _UNLOCK );.. if
13875 28 20 70 67 6e 6f 3d 3d 30 20 29 7b 0a 20 20 20 ( pgno==0 ){.
13876 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
13877 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d ORRUPT_BKPT;. }
13878 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 .. /* If the pa
13879 67 65 72 20 69 73 20 69 6e 20 74 68 65 20 65 72 ger is in the er
1387a 72 6f 72 20 73 74 61 74 65 2c 20 72 65 74 75 72 ror state, retur
1387b 6e 20 61 6e 20 65 72 72 6f 72 20 69 6d 6d 65 64 n an error immed
1387c 69 61 74 65 6c 79 2e 20 0a 20 20 2a 2a 20 4f 74 iately. . ** Ot
1387d 68 65 72 77 69 73 65 2c 20 72 65 71 75 65 73 74 herwise, request
1387e 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 74 the page from t
1387f 68 65 20 50 43 61 63 68 65 20 6c 61 79 65 72 2e he PCache layer.
13880 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 */. if( pPager
13881 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 54 ->errCode!=SQLIT
13882 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e E_OK && pPager->
13883 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f errCode!=SQLITE_
13884 46 55 4c 4c 20 29 7b 0a 20 20 20 20 72 63 20 3d FULL ){. rc =
13885 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
13886 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
13887 63 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 c = sqlite3Pcach
13888 65 46 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70 eFetch(pPager->p
13889 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 31 2c PCache, pgno, 1,
1388a 20 70 70 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 ppPage);. }..
1388b 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1388c 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 45 69 74 OK ){. /* Eit
1388d 68 65 72 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 her the call to
1388e 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 sqlite3PcacheFet
1388f 63 68 28 29 20 72 65 74 75 72 6e 65 64 20 61 6e ch() returned an
13890 20 65 72 72 6f 72 20 6f 72 20 74 68 65 0a 20 20 error or the.
13891 20 20 2a 2a 20 70 61 67 65 72 20 77 61 73 20 61 ** pager was a
13892 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 65 72 lready in the er
13893 72 6f 72 2d 73 74 61 74 65 20 77 68 65 6e 20 74 ror-state when t
13894 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 his function was
13895 20 63 61 6c 6c 65 64 2e 0a 20 20 20 20 2a 2a 20 called.. **
13896 53 65 74 20 70 50 67 20 74 6f 20 30 20 61 6e 64 Set pPg to 0 and
13897 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 65 78 63 jump to the exc
13898 65 70 74 69 6f 6e 20 68 61 6e 64 6c 65 72 2e 20 eption handler.
13899 20 2a 2f 0a 20 20 20 20 70 50 67 20 3d 20 30 3b */. pPg = 0;
1389a 0a 20 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f . goto pager_
1389b 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 7d acquire_err;. }
1389c 0a 20 20 61 73 73 65 72 74 28 20 28 2a 70 70 50 . assert( (*ppP
1389d 61 67 65 29 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f age)->pgno==pgno
1389e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 2a );. assert( (*
1389f 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65 72 3d ppPage)->pPager=
138a0 3d 70 50 61 67 65 72 20 7c 7c 20 28 2a 70 70 50 =pPager || (*ppP
138a1 61 67 65 29 2d 3e 70 50 61 67 65 72 3d 3d 30 20 age)->pPager==0
138a2 29 3b 0a 0a 20 20 69 66 28 20 28 2a 70 70 50 61 );.. if( (*ppPa
138a3 67 65 29 2d 3e 70 50 61 67 65 72 20 29 7b 0a 20 ge)->pPager ){.
138a4 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 /* In this ca
138a5 73 65 20 74 68 65 20 70 63 61 63 68 65 20 61 6c se the pcache al
138a6 72 65 61 64 79 20 63 6f 6e 74 61 69 6e 73 20 61 ready contains a
138a7 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 63 6f n initialized co
138a8 70 79 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 py of. ** the
138a9 20 70 61 67 65 2e 20 52 65 74 75 72 6e 20 77 69 page. Return wi
138aa 74 68 6f 75 74 20 66 75 72 74 68 65 72 20 61 64 thout further ad
138ab 6f 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 o. */. asser
138ac 74 28 20 70 67 6e 6f 3c 3d 50 41 47 45 52 5f 4d t( pgno<=PAGER_M
138ad 41 58 5f 50 47 4e 4f 20 26 26 20 70 67 6e 6f 21 AX_PGNO && pgno!
138ae 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 =PAGER_MJ_PGNO(p
138af 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 50 41 Pager) );. PA
138b0 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d GER_INCR(pPager-
138b1 3e 6e 48 69 74 29 3b 0a 20 20 20 20 72 65 74 75 >nHit);. retu
138b2 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 rn SQLITE_OK;..
138b3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 }else{. /* T
138b4 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20 68 he pager cache h
138b5 61 73 20 63 72 65 61 74 65 64 20 61 20 6e 65 77 as created a new
138b6 20 70 61 67 65 2e 20 49 74 73 20 63 6f 6e 74 65 page. Its conte
138b7 6e 74 20 6e 65 65 64 73 20 74 6f 20 0a 20 20 20 nt needs to .
138b8 20 2a 2a 20 62 65 20 69 6e 69 74 69 61 6c 69 7a ** be initializ
138b9 65 64 2e 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ed. */. int
138ba 6e 4d 61 78 3b 0a 0a 20 20 20 20 50 41 47 45 52 nMax;.. PAGER
138bb 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 4d _INCR(pPager->nM
138bc 69 73 73 29 3b 0a 20 20 20 20 70 50 67 20 3d 20 iss);. pPg =
138bd 2a 70 70 50 61 67 65 3b 0a 20 20 20 20 70 50 67 *ppPage;. pPg
138be 2d 3e 70 50 61 67 65 72 20 3d 20 70 50 61 67 65 ->pPager = pPage
138bf 72 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6d r;.. /* The m
138c0 61 78 69 6d 75 6d 20 70 61 67 65 20 6e 75 6d 62 aximum page numb
138c1 65 72 20 69 73 20 32 5e 33 31 2e 20 52 65 74 75 er is 2^31. Retu
138c2 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
138c3 54 20 69 66 20 61 20 70 61 67 65 0a 20 20 20 20 T if a page.
138c4 2a 2a 20 6e 75 6d 62 65 72 20 67 72 65 61 74 65 ** number greate
138c5 72 20 74 68 61 6e 20 74 68 69 73 2c 20 6f 72 20 r than this, or
138c6 74 68 65 20 75 6e 75 73 65 64 20 6c 6f 63 6b 69 the unused locki
138c7 6e 67 2d 70 61 67 65 2c 20 69 73 20 72 65 71 75 ng-page, is requ
138c8 65 73 74 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 ested. */. if
138c9 28 20 70 67 6e 6f 3e 50 41 47 45 52 5f 4d 41 58 ( pgno>PAGER_MAX
138ca 5f 50 47 4e 4f 20 7c 7c 20 70 67 6e 6f 3d 3d 50 _PGNO || pgno==P
138cb 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 AGER_MJ_PGNO(pPa
138cc 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 72 63 ger) ){. rc
138cd 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
138ce 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f T_BKPT;. go
138cf 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 to pager_acquire
138d0 5f 65 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 _err;. }..
138d1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
138d2 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 erPagecount(pPag
138d3 65 72 2c 20 26 6e 4d 61 78 29 3b 0a 20 20 20 20 er, &nMax);.
138d4 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
138d5 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 K ){. goto
138d6 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 pager_acquire_er
138d7 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 r;. }.. if
138d8 28 20 6e 4d 61 78 3c 28 69 6e 74 29 70 67 6e 6f ( nMax<(int)pgno
138d9 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 6e 6f 43 || MEMDB || noC
138da 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 ontent ){.
138db 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d if( pgno>pPager-
138dc 3e 6d 78 50 67 6e 6f 20 29 7b 0a 09 72 63 20 3d >mxPgno ){..rc =
138dd 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 09 67 SQLITE_FULL;..g
138de 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69 72 oto pager_acquir
138df 65 5f 65 72 72 3b 0a 20 20 20 20 20 20 7d 0a 20 e_err;. }.
138e0 20 20 20 20 20 69 66 28 20 6e 6f 43 6f 6e 74 65 if( noConte
138e1 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a nt ){. /*
138e2 20 46 61 69 6c 75 72 65 20 74 6f 20 73 65 74 20 Failure to set
138e3 74 68 65 20 62 69 74 73 20 69 6e 20 74 68 65 20 the bits in the
138e4 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 2d 76 65 InJournal bit-ve
138e5 63 74 6f 72 73 20 69 73 20 62 65 6e 69 67 6e 2e ctors is benign.
138e6 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 74 20 6d . ** It m
138e7 65 72 65 6c 79 20 6d 65 61 6e 73 20 74 68 61 74 erely means that
138e8 20 77 65 20 6d 69 67 68 74 20 64 6f 20 73 6f 6d we might do som
138e9 65 20 65 78 74 72 61 20 77 6f 72 6b 20 74 6f 20 e extra work to
138ea 6a 6f 75 72 6e 61 6c 20 61 20 0a 20 20 20 20 20 journal a .
138eb 20 20 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20 ** page that
138ec 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f does not need to
138ed 20 62 65 20 6a 6f 75 72 6e 61 6c 65 64 2e 20 20 be journaled.
138ee 4e 65 76 65 72 74 68 65 6c 65 73 73 2c 20 62 65 Nevertheless, be
138ef 20 73 75 72 65 20 0a 20 20 20 20 20 20 20 20 2a sure . *
138f0 2a 20 74 6f 20 74 65 73 74 20 74 68 65 20 63 61 * to test the ca
138f1 73 65 20 77 68 65 72 65 20 61 20 6d 61 6c 6c 6f se where a mallo
138f2 63 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 c error occurs w
138f3 68 69 6c 65 20 74 72 79 69 6e 67 20 74 6f 20 73 hile trying to s
138f4 65 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 et . ** a
138f5 20 62 69 74 20 69 6e 20 61 20 62 69 74 20 76 65 bit in a bit ve
138f6 63 74 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f ctor.. */
138f7 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
138f8 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f BeginBenignMallo
138f9 63 28 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 c();. if(
138fa 20 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 pgno<=pPager->d
138fb 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 bOrigSize ){.
138fc 20 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 TESTONLY(
138fd 20 72 63 20 3d 20 29 20 73 71 6c 69 74 65 33 42 rc = ) sqlite3B
138fe 69 74 76 65 63 53 65 74 28 70 50 61 67 65 72 2d itvecSet(pPager-
138ff 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 6e >pInJournal, pgn
13900 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65 o);. te
13901 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 stcase( rc==SQLI
13902 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 TE_NOMEM );.
13903 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 54 45 }. TE
13904 53 54 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 61 STONLY( rc = ) a
13905 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 ddToSavepointBit
13906 76 65 63 73 28 70 50 61 67 65 72 2c 20 70 67 6e vecs(pPager, pgn
13907 6f 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 o);. test
13908 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 case( rc==SQLITE
13909 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 _NOMEM );.
1390a 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 sqlite3EndBeni
1390b 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 gnMalloc();.
1390c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1390d 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e 70 44 61 memset(pPg->pDa
1390e 74 61 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 70 ta, 0, pPager->p
1390f 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 ageSize);.
13910 7d 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 }. IOTRACE(
13911 28 22 5a 45 52 4f 20 25 70 20 25 64 5c 6e 22 2c ("ZERO %p %d\n",
13912 20 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b pPager, pgno));
13913 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
13914 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 assert( pPg->p
13915 50 61 67 65 72 3d 3d 70 50 61 67 65 72 20 29 3b Pager==pPager );
13916 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 . rc = read
13917 44 62 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 DbPage(pPg);.
13918 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
13919 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1391a 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69 goto pager_acqui
1391b 72 65 5f 65 72 72 3b 0a 20 20 20 20 20 20 7d 0a re_err;. }.
1391c 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c }.#ifdef SQL
1391d 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a ITE_CHECK_PAGES.
1391e 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 pPg->pageHas
1391f 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 h = pager_pageha
13920 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a sh(pPg);.#endif.
13921 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 }.. return SQ
13922 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 72 5f LITE_OK;..pager_
13923 61 63 71 75 69 72 65 5f 65 72 72 3a 0a 20 20 61 acquire_err:. a
13924 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
13925 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 70 50 E_OK );. if( pP
13926 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 g ){. sqlite3
13927 50 63 61 63 68 65 44 72 6f 70 28 70 50 67 29 3b PcacheDrop(pPg);
13928 0a 20 20 7d 0a 20 20 70 61 67 65 72 55 6e 6c 6f . }. pagerUnlo
13929 63 6b 49 66 55 6e 75 73 65 64 28 70 50 61 67 65 ckIfUnused(pPage
1392a 72 29 3b 0a 0a 20 20 2a 70 70 50 61 67 65 20 3d r);.. *ppPage =
1392b 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0;. return rc;
1392c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 .}../*.** Acquir
1392d 65 20 61 20 70 61 67 65 20 69 66 20 69 74 20 69 e a page if it i
1392e 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 s already in the
1392f 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 in-memory cache
13930 2e 20 20 44 6f 0a 2a 2a 20 6e 6f 74 20 72 65 61 . Do.** not rea
13931 64 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 d the page from
13932 64 69 73 6b 2e 20 20 52 65 74 75 72 6e 20 61 20 disk. Return a
13933 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 pointer to the p
13934 61 67 65 2c 0a 2a 2a 20 6f 72 20 30 20 69 66 20 age,.** or 0 if
13935 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 the page is not
13936 69 6e 20 63 61 63 68 65 2e 20 41 6c 73 6f 2c 20 in cache. Also,
13937 72 65 74 75 72 6e 20 30 20 69 66 20 74 68 65 20 return 0 if the
13938 0a 2a 2a 20 70 61 67 65 72 20 69 73 20 69 6e 20 .** pager is in
13939 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 74 61 PAGER_UNLOCK sta
1393a 74 65 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e te when this fun
1393b 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c ction is called,
1393c 0a 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 70 61 .** or if the pa
1393d 67 65 72 20 69 73 20 69 6e 20 61 6e 20 65 72 72 ger is in an err
1393e 6f 72 20 73 74 61 74 65 20 6f 74 68 65 72 20 74 or state other t
1393f 68 61 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e han SQLITE_FULL.
13940 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 .**.** See also
13941 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
13942 29 2e 20 20 54 68 65 20 64 69 66 66 65 72 65 6e ). The differen
13943 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 ce between this
13944 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 6e 64 20 73 routine.** and s
13945 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 qlite3PagerGet()
13946 20 69 73 20 74 68 61 74 20 5f 67 65 74 28 29 20 is that _get()
13947 77 69 6c 6c 20 67 6f 20 74 6f 20 74 68 65 20 64 will go to the d
13948 69 73 6b 20 61 6e 64 20 72 65 61 64 0a 2a 2a 20 isk and read.**
13949 69 6e 20 74 68 65 20 70 61 67 65 20 69 66 20 74 in the page if t
1394a 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 he page is not a
1394b 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68 65 2e lready in cache.
1394c 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a This routine.*
1394d 2a 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 * returns NULL i
1394e 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f f the page is no
1394f 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 69 66 t in cache or if
13950 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f a disk I/O erro
13951 72 20 0a 2a 2a 20 68 61 73 20 65 76 65 72 20 68 r .** has ever h
13952 61 70 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 appened..*/.SQLI
13953 54 45 5f 50 52 49 56 41 54 45 20 44 62 50 61 67 TE_PRIVATE DbPag
13954 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c e *sqlite3PagerL
13955 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 ookup(Pager *pPa
13956 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b ger, Pgno pgno){
13957 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 . PgHdr *pPg =
13958 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 0;. assert( pPa
13959 67 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 ger!=0 );. asse
1395a 72 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a 20 rt( pgno!=0 );.
1395b 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
1395c 3e 70 50 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 >pPCache!=0 );.
1395d 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
1395e 3e 73 74 61 74 65 20 3e 20 50 41 47 45 52 5f 55 >state > PAGER_U
1395f 4e 4c 4f 43 4b 20 29 3b 0a 20 20 73 71 6c 69 74 NLOCK );. sqlit
13960 65 33 50 63 61 63 68 65 46 65 74 63 68 28 70 50 e3PcacheFetch(pP
13961 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 ager->pPCache, p
13962 67 6e 6f 2c 20 30 2c 20 26 70 50 67 29 3b 0a 20 gno, 0, &pPg);.
13963 20 72 65 74 75 72 6e 20 70 50 67 3b 0a 7d 0a 0a return pPg;.}..
13964 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 /*.** Release a
13965 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e 0a page reference..
13966 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d **.** If the num
13967 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 ber of reference
13968 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 72 s to the page dr
13969 6f 70 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e op to zero, then
1396a 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 20 the.** page is
1396b 61 64 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55 added to the LRU
1396c 20 6c 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c list. When all
1396d 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 references to a
1396e 6c 6c 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 20 ll pages.** are
1396f 72 65 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c released, a roll
13970 62 61 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20 back occurs and
13971 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 the lock on the
13972 64 61 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 72 database is.** r
13973 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 emoved..*/.SQLIT
13974 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
13975 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
13976 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 (DbPage *pPg){.
13977 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 if( pPg ){.
13978 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 Pager *pPager =
13979 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 pPg->pPager;.
1397a 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
1397b 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 20 20 20 lease(pPg);.
1397c 70 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 pagerUnlockIfUnu
1397d 73 65 64 28 70 50 61 67 65 72 29 3b 0a 20 20 7d sed(pPager);. }
1397e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
1397f 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 main journal fi
13980 6c 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 le has already b
13981 65 65 6e 20 6f 70 65 6e 65 64 2c 20 65 6e 73 75 een opened, ensu
13982 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 73 re that the.** s
13983 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ub-journal file
13984 69 73 20 6f 70 65 6e 20 74 6f 6f 2e 20 49 66 20 is open too. If
13985 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c the main journal
13986 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c 0a 2a 2a is not open,.**
13987 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
13988 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a s a no-op..**.**
13989 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
1398a 74 75 72 6e 65 64 20 69 66 20 65 76 65 72 79 74 turned if everyt
1398b 68 69 6e 67 20 67 6f 65 73 20 61 63 63 6f 72 64 hing goes accord
1398c 69 6e 67 20 74 6f 20 70 6c 61 6e 2e 20 0a 2a 2a ing to plan. .**
1398d 20 41 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 An SQLITE_IOERR
1398e 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64 65 20 _XXX error code
1398f 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 is returned if a
13990 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 73 71 6c call to .** sql
13991 69 74 65 33 4f 73 4f 70 65 6e 28 29 20 66 61 69 ite3OsOpen() fai
13992 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ls..*/.static in
13993 74 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c t openSubJournal
13994 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
13995 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
13996 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 73 4f TE_OK;. if( isO
13997 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
13998 20 26 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67 && !isOpen(pPag
13999 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 er->sjfd) ){.
1399a 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 if( pPager->jou
1399b 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f rnalMode==PAGER_
1399c 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f JOURNALMODE_MEMO
1399d 52 59 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 75 RY || pPager->su
1399e 62 6a 49 6e 4d 65 6d 6f 72 79 20 29 7b 0a 20 20 bjInMemory ){.
1399f 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f sqlite3MemJo
139a0 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 urnalOpen(pPager
139a1 2d 3e 73 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c ->sjfd);. }el
139a2 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 se{. rc = p
139a3 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 agerOpentemp(pPa
139a4 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 6a 66 ger, pPager->sjf
139a5 64 2c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 d, SQLITE_OPEN_S
139a6 55 42 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 UBJOURNAL);.
139a7 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
139a8 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 c;.}../*.** This
139a9 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
139aa 6c 65 64 20 61 74 20 74 68 65 20 73 74 61 72 74 led at the start
139ab 20 6f 66 20 65 76 65 72 79 20 77 72 69 74 65 20 of every write
139ac 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 transaction..**
139ad 54 68 65 72 65 20 6d 75 73 74 20 61 6c 72 65 61 There must alrea
139ae 64 79 20 62 65 20 61 20 52 45 53 45 52 56 45 44 dy be a RESERVED
139af 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f or EXCLUSIVE lo
139b0 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ck on the databa
139b1 73 65 20 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e se .** file when
139b2 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
139b3 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f called..**.** O
139b4 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 pen the journal
139b5 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70 file for pager p
139b6 50 61 67 65 72 20 61 6e 64 20 77 72 69 74 65 20 Pager and write
139b7 61 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 a journal header
139b8 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 72 74 .** to the start
139b9 20 6f 66 20 69 74 2e 20 49 66 20 74 68 65 72 65 of it. If there
139ba 20 61 72 65 20 61 63 74 69 76 65 20 73 61 76 65 are active save
139bb 70 6f 69 6e 74 73 2c 20 6f 70 65 6e 20 74 68 65 points, open the
139bc 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 sub-journal.**
139bd 61 73 20 77 65 6c 6c 2e 20 54 68 69 73 20 66 75 as well. This fu
139be 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 nction is only u
139bf 73 65 64 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 sed when the jou
139c0 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 62 65 69 rnal file is bei
139c1 6e 67 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 74 6f ng .** opened to
139c2 20 77 72 69 74 65 20 61 20 72 6f 6c 6c 62 61 63 write a rollbac
139c3 6b 20 6c 6f 67 20 66 6f 72 20 61 20 74 72 61 6e k log for a tran
139c4 73 61 63 74 69 6f 6e 2e 20 49 74 20 69 73 20 6e saction. It is n
139c5 6f 74 20 75 73 65 64 20 0a 2a 2a 20 77 68 65 6e ot used .** when
139c6 20 6f 70 65 6e 69 6e 67 20 61 20 68 6f 74 20 6a opening a hot j
139c7 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 72 ournal file to r
139c8 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a oll it back..**.
139c9 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 ** If the journa
139ca 6c 20 66 69 6c 65 20 69 73 20 61 6c 72 65 61 64 l file is alread
139cb 79 20 6f 70 65 6e 20 28 61 73 20 69 74 20 6d 61 y open (as it ma
139cc 79 20 62 65 20 69 6e 20 65 78 63 6c 75 73 69 76 y be in exclusiv
139cd 65 20 6d 6f 64 65 29 2c 0a 2a 2a 20 74 68 65 6e e mode),.** then
139ce 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6a this function j
139cf 75 73 74 20 77 72 69 74 65 73 20 61 20 6a 6f 75 ust writes a jou
139d0 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f 20 74 rnal header to t
139d1 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 0a he start of the.
139d2 2a 2a 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 ** already open
139d3 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 file. .**.** Whe
139d4 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 ther or not the
139d5 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
139d6 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73 20 66 opened by this f
139d7 75 6e 63 74 69 6f 6e 2c 20 74 68 65 0a 2a 2a 20 unction, the.**
139d8 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c Pager.pInJournal
139d9 20 62 69 74 76 65 63 20 73 74 72 75 63 74 75 72 bitvec structur
139da 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2e 0a e is allocated..
139db 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c **.** Return SQL
139dc 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 ITE_OK if everyt
139dd 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 hing is successf
139de 75 6c 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 ul. Otherwise, r
139df 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 eturn .** SQLITE
139e0 5f 4e 4f 4d 45 4d 20 69 66 20 74 68 65 20 61 74 _NOMEM if the at
139e1 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 tempt to allocat
139e2 65 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e e Pager.pInJourn
139e3 61 6c 20 66 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a al fails, or .**
139e4 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 an IO error cod
139e5 65 20 69 66 20 6f 70 65 6e 69 6e 67 20 6f 72 20 e if opening or
139e6 77 72 69 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 writing the jour
139e7 6e 61 6c 20 66 69 6c 65 20 66 61 69 6c 73 2e 0a nal file fails..
139e8 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
139e9 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c ger_open_journal
139ea 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
139eb 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
139ec 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 TE_OK;
139ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
139ee 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
139ef 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 sqlite3_vfs *
139f0 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61 const pVfs = pPa
139f1 67 65 72 2d 3e 70 56 66 73 3b 20 20 20 2f 2a 20 ger->pVfs; /*
139f2 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 76 Local cache of v
139f3 66 73 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 0a 20 fs pointer */..
139f4 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
139f5 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 >state>=PAGER_RE
139f6 53 45 52 56 45 44 20 29 3b 0a 20 20 61 73 73 65 SERVED );. asse
139f7 72 74 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a rt( pPager->useJ
139f8 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 ournal );. asse
139f9 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 rt( pPager->jour
139fa 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a nalMode!=PAGER_J
139fb 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 OURNALMODE_OFF )
139fc 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
139fd 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d er->pInJournal==
139fe 30 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 20 0 );. . /* If
139ff 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 65 already in the e
13a00 72 72 6f 72 20 73 74 61 74 65 2c 20 74 68 69 73 rror state, this
13a01 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
13a02 6f 2d 6f 70 2e 20 20 42 75 74 20 6f 6e 0a 20 20 o-op. But on.
13a03 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e ** the other han
13a04 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 d, this routine
13a05 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 is never called
13a06 69 66 20 77 65 20 61 72 65 20 61 6c 72 65 61 64 if we are alread
13a07 79 20 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 72 72 y in. ** an err
13a08 6f 72 20 73 74 61 74 65 2e 20 2a 2f 0a 20 20 69 or state. */. i
13a09 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d f( NEVER(pPager-
13a0a 3e 65 72 72 43 6f 64 65 29 20 29 20 72 65 74 75 >errCode) ) retu
13a0b 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f rn pPager->errCo
13a0c 64 65 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 de;.. /* TODO:
13a0d 49 73 20 69 74 20 72 65 61 6c 6c 79 20 70 6f 73 Is it really pos
13a0e 73 69 62 6c 65 20 74 6f 20 67 65 74 20 68 65 72 sible to get her
13a0f 65 20 77 69 74 68 20 64 62 53 69 7a 65 56 61 6c e with dbSizeVal
13a10 69 64 3d 3d 30 3f 20 49 66 20 6e 6f 74 2c 0a 20 id==0? If not,.
13a11 20 2a 2a 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 ** the call to
13a12 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 29 PagerPagecount()
13a13 20 63 61 6e 20 62 65 20 72 65 6d 6f 76 65 64 2e can be removed.
13a14 0a 20 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 . */. testcase
13a15 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 ( pPager->dbSize
13a16 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 73 71 Valid==0 );. sq
13a17 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f lite3PagerPageco
13a18 75 6e 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a unt(pPager, 0);.
13a19 0a 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f . pPager->pInJo
13a1a 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 42 urnal = sqlite3B
13a1b 69 74 76 65 63 43 72 65 61 74 65 28 70 50 61 67 itvecCreate(pPag
13a1c 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 69 er->dbSize);. i
13a1d 66 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f f( pPager->pInJo
13a1e 75 72 6e 61 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 urnal==0 ){.
13a1f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
13a20 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f MEM;. }.. /* O
13a21 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 pen the journal
13a22 66 69 6c 65 20 69 66 20 69 74 20 69 73 20 6e 6f file if it is no
13a23 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 2e 20 t already open.
13a24 2a 2f 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e */. if( !isOpen
13a25 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b (pPager->jfd) ){
13a26 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d . if( pPager-
13a27 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 >journalMode==PA
13a28 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
13a29 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 20 MEMORY ){.
13a2a 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 sqlite3MemJourna
13a2b 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 lOpen(pPager->jf
13a2c 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 d);. }else{.
13a2d 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 66 const int f
13a2e 6c 61 67 73 20 3d 20 20 20 20 20 20 20 20 20 20 lags =
13a2f 20 20 20 20 20 20 20 20 20 2f 2a 20 56 46 53 20 /* VFS
13a30 66 6c 61 67 73 20 74 6f 20 6f 70 65 6e 20 6a 6f flags to open jo
13a31 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 urnal file */.
13a32 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 SQLITE_OPE
13a33 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 N_READWRITE|SQLI
13a34 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 0a TE_OPEN_CREATE|.
13a35 20 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d (pPager-
13a36 3e 74 65 6d 70 46 69 6c 65 20 3f 20 0a 20 20 20 >tempFile ? .
13a37 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f (SQLITE_O
13a38 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 PEN_DELETEONCLOS
13a39 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 E|SQLITE_OPEN_TE
13a3a 4d 50 5f 4a 4f 55 52 4e 41 4c 29 3a 0a 20 20 20 MP_JOURNAL):.
13a3b 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f (SQLITE_O
13a3c 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c PEN_MAIN_JOURNAL
13a3d 29 0a 20 20 20 20 20 20 20 20 29 3b 0a 23 69 66 ). );.#if
13a3e 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
13a3f 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 E_ATOMIC_WRITE.
13a40 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
13a41 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20 20 3JournalOpen(.
13a42 20 20 20 20 20 20 20 20 70 56 66 73 2c 20 70 50 pVfs, pP
13a43 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 ager->zJournal,
13a44 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 pPager->jfd, fla
13a45 67 73 2c 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 gs, jrnlBufferSi
13a46 7a 65 28 70 50 61 67 65 72 29 0a 20 20 20 20 20 ze(pPager).
13a47 20 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 );.#else.
13a48 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 rc = sqlite3OsOp
13a49 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d en(pVfs, pPager-
13a4a 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 >zJournal, pPage
13a4b 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 30 r->jfd, flags, 0
13a4c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a );.#endif. }.
13a4d 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d assert( rc!=
13a4e 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f SQLITE_OK || isO
13a4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
13a50 20 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 57 );. }... /* W
13a51 72 69 74 65 20 74 68 65 20 66 69 72 73 74 20 6a rite the first j
13a52 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f ournal header to
13a53 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
13a54 65 20 61 6e 64 20 6f 70 65 6e 20 0a 20 20 2a 2a e and open . **
13a55 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
13a56 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 if necessary..
13a57 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 */. if( rc==SQ
13a58 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f LITE_OK ){. /
13a59 2a 20 54 4f 44 4f 3a 20 43 68 65 63 6b 20 69 66 * TODO: Check if
13a5a 20 61 6c 6c 20 6f 66 20 74 68 65 73 65 20 61 72 all of these ar
13a5b 65 20 72 65 61 6c 6c 79 20 72 65 71 75 69 72 65 e really require
13a5c 64 2e 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 d. */. pPager
13a5d 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d 20 70 ->dbOrigSize = p
13a5e 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 Pager->dbSize;.
13a5f 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
13a60 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 alStarted = 0;.
13a61 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 pPager->needS
13a62 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 ync = 0;. pPa
13a63 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 ger->nRec = 0;.
13a64 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
13a65 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 70 alOff = 0;. p
13a66 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 Pager->setMaster
13a67 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 = 0;. pPager
13a68 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 ->journalHdr = 0
13a69 3b 0a 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 ;. rc = write
13a6a 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 JournalHdr(pPage
13a6b 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 r);. }. if( rc
13a6c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 ==SQLITE_OK && p
13a6d 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e Pager->nSavepoin
13a6e 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70 t ){. rc = op
13a6f 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 70 50 61 enSubJournal(pPa
13a70 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 ger);. }.. if(
13a71 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
13a72 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 {. sqlite3Bit
13a73 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 vecDestroy(pPage
13a74 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a r->pInJournal);.
13a75 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a pPager->pInJ
13a76 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 7d 0a ournal = 0;. }.
13a77 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
13a78 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 77 72 /*.** Begin a wr
13a79 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite-transaction
13a7a 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 on the specified
13a7b 20 70 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 49 pager object. I
13a7c 66 20 61 20 0a 2a 2a 20 77 72 69 74 65 2d 74 72 f a .** write-tr
13a7d 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 61 6c ansaction has al
13a7e 72 65 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65 ready been opene
13a7f 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e d, this function
13a80 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a is a no-op..**.
13a81 2a 2a 20 49 66 20 74 68 65 20 65 78 46 6c 61 67 ** If the exFlag
13a82 20 61 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c argument is fal
13a83 73 65 2c 20 74 68 65 6e 20 61 63 71 75 69 72 65 se, then acquire
13a84 20 61 74 20 6c 65 61 73 74 20 61 20 52 45 53 45 at least a RESE
13a85 52 56 45 44 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e 20 RVED.** lock on
13a86 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
13a87 65 2e 20 49 66 20 65 78 46 6c 61 67 20 69 73 20 e. If exFlag is
13a88 74 72 75 65 2c 20 74 68 65 6e 20 61 63 71 75 69 true, then acqui
13a89 72 65 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 61 re at least.** a
13a8a 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b n EXCLUSIVE lock
13a8b 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b . If such a lock
13a8c 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 is already held
13a8d 2c 20 6e 6f 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a , no locking .**
13a8e 20 66 75 6e 63 74 69 6f 6e 73 20 6e 65 65 64 20 functions need
13a8f 62 65 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a be called..**.**
13a90 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 If this is not
13a91 61 20 74 65 6d 70 6f 72 61 72 79 20 6f 72 20 69 a temporary or i
13a92 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 61 6e n-memory file an
13a93 64 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 d, the journal f
13a94 69 6c 65 20 69 73 20 0a 2a 2a 20 6f 70 65 6e 65 ile is .** opene
13a95 64 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 d if it has not
13a96 62 65 65 6e 20 61 6c 72 65 61 64 79 2e 20 46 6f been already. Fo
13a97 72 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 r a temporary fi
13a98 6c 65 2c 20 74 68 65 20 6f 70 65 6e 69 6e 67 20 le, the opening
13a99 0a 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e .** of the journ
13a9a 61 6c 20 66 69 6c 65 20 69 73 20 64 65 66 65 72 al file is defer
13a9b 72 65 64 20 75 6e 74 69 6c 20 74 68 65 72 65 20 red until there
13a9c 69 73 20 61 6e 20 61 63 74 75 61 6c 20 6e 65 65 is an actual nee
13a9d 64 20 74 6f 20 0a 2a 2a 20 77 72 69 74 65 20 74 d to .** write t
13a9e 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 54 o the journal. T
13a9f 4f 44 4f 3a 20 57 68 79 20 68 61 6e 64 6c 65 20 ODO: Why handle
13aa0 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 temporary files
13aa1 64 69 66 66 65 72 65 6e 74 6c 79 3f 0a 2a 2a 0a differently?.**.
13aa2 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 ** If the journa
13aa3 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 l file is opened
13aa4 20 28 6f 72 20 69 66 20 69 74 20 69 73 20 61 6c (or if it is al
13aa5 72 65 61 64 79 20 6f 70 65 6e 29 2c 20 74 68 65 ready open), the
13aa6 6e 20 61 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 68 n a.** journal-h
13aa7 65 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e eader is written
13aa8 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 to the start of
13aa9 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 it..**.** If th
13aaa 65 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61 e subjInMemory a
13aab 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 2d 7a rgument is non-z
13aac 65 72 6f 2c 20 74 68 65 6e 20 61 6e 79 20 73 75 ero, then any su
13aad 62 2d 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e 65 64 b-journal opened
13aae 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 69 73 20 .** within this
13aaf 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c transaction will
13ab0 20 62 65 20 6f 70 65 6e 65 64 20 61 73 20 61 6e be opened as an
13ab1 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 2e in-memory file.
13ab2 20 54 68 69 73 0a 2a 2a 20 68 61 73 20 6e 6f 20 This.** has no
13ab3 65 66 66 65 63 74 20 69 66 20 74 68 65 20 73 75 effect if the su
13ab4 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c 72 b-journal is alr
13ab5 65 61 64 79 20 6f 70 65 6e 65 64 20 28 61 73 20 eady opened (as
13ab6 69 74 20 6d 61 79 20 62 65 20 77 68 65 6e 0a 2a it may be when.*
13ab7 2a 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 * running in exc
13ab8 6c 75 73 69 76 65 20 6d 6f 64 65 29 20 6f 72 20 lusive mode) or
13ab9 69 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 if the transacti
13aba 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 72 65 71 75 on does not requ
13abb 69 72 65 20 61 0a 2a 2a 20 73 75 62 2d 6a 6f 75 ire a.** sub-jou
13abc 72 6e 61 6c 2e 20 49 66 20 74 68 65 20 73 75 62 rnal. If the sub
13abd 6a 49 6e 4d 65 6d 6f 72 79 20 61 72 67 75 6d 65 jInMemory argume
13abe 6e 74 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e nt is zero, then
13abf 20 61 6e 79 20 72 65 71 75 69 72 65 64 0a 2a 2a any required.**
13ac0 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 sub-journal is
13ac1 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 2d 6d implemented in-m
13ac2 65 6d 6f 72 79 20 69 66 20 70 50 61 67 65 72 20 emory if pPager
13ac3 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 is an in-memory
13ac4 64 61 74 61 62 61 73 65 2c 20 0a 2a 2a 20 6f 72 database, .** or
13ac5 20 75 73 69 6e 67 20 61 20 74 65 6d 70 6f 72 61 using a tempora
13ac6 72 79 20 66 69 6c 65 20 6f 74 68 65 72 77 69 73 ry file otherwis
13ac7 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
13ac8 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
13ac9 50 61 67 65 72 42 65 67 69 6e 28 50 61 67 65 72 PagerBegin(Pager
13aca 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 78 *pPager, int ex
13acb 46 6c 61 67 2c 20 69 6e 74 20 73 75 62 6a 49 6e Flag, int subjIn
13acc 4d 65 6d 6f 72 79 29 7b 0a 20 20 69 6e 74 20 72 Memory){. int r
13acd 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
13ace 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13acf 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e >state!=PAGER_UN
13ad0 4c 4f 43 4b 20 29 3b 0a 20 20 70 50 61 67 65 72 LOCK );. pPager
13ad1 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 3d ->subjInMemory =
13ad2 20 28 75 38 29 73 75 62 6a 49 6e 4d 65 6d 6f 72 (u8)subjInMemor
13ad3 79 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d y;. if( pPager-
13ad4 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 >state==PAGER_SH
13ad5 41 52 45 44 20 29 7b 0a 20 20 20 20 61 73 73 65 ARED ){. asse
13ad6 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a rt( pPager->pInJ
13ad7 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 ournal==0 );.
13ad8 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 assert( !MEMDB
13ad9 26 26 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 && !pPager->temp
13ada 46 69 6c 65 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 File );.. /*
13adb 4f 62 74 61 69 6e 20 61 20 52 45 53 45 52 56 45 Obtain a RESERVE
13adc 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 D lock on the da
13add 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 tabase file. If
13ade 74 68 65 20 65 78 46 6c 61 67 20 70 61 72 61 6d the exFlag param
13adf 65 74 65 72 0a 20 20 20 20 2a 2a 20 69 73 20 74 eter. ** is t
13ae0 72 75 65 2c 20 74 68 65 6e 20 69 6d 6d 65 64 69 rue, then immedi
13ae1 61 74 65 6c 79 20 75 70 67 72 61 64 65 20 74 68 ately upgrade th
13ae2 69 73 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 is to an EXCLUSI
13ae3 56 45 20 6c 6f 63 6b 2e 20 54 68 65 0a 20 20 20 VE lock. The.
13ae4 20 2a 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 ** busy-handler
13ae5 20 63 61 6c 6c 62 61 63 6b 20 63 61 6e 20 62 65 callback can be
13ae6 20 75 73 65 64 20 77 68 65 6e 20 75 70 67 72 61 used when upgra
13ae7 64 69 6e 67 20 74 6f 20 74 68 65 20 45 58 43 4c ding to the EXCL
13ae8 55 53 49 56 45 0a 20 20 20 20 2a 2a 20 6c 6f 63 USIVE. ** loc
13ae9 6b 2c 20 62 75 74 20 6e 6f 74 20 77 68 65 6e 20 k, but not when
13aea 6f 62 74 61 69 6e 69 6e 67 20 74 68 65 20 52 45 obtaining the RE
13aeb 53 45 52 56 45 44 20 6c 6f 63 6b 2e 0a 20 20 20 SERVED lock..
13aec 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c */. rc = sql
13aed 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 ite3OsLock(pPage
13aee 72 2d 3e 66 64 2c 20 52 45 53 45 52 56 45 44 5f r->fd, RESERVED_
13aef 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 LOCK);. if( r
13af0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
13af1 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 pPager->st
13af2 61 74 65 20 3d 20 50 41 47 45 52 5f 52 45 53 45 ate = PAGER_RESE
13af3 52 56 45 44 3b 0a 20 20 20 20 20 20 69 66 28 20 RVED;. if(
13af4 65 78 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 exFlag ){.
13af5 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 rc = pager_wai
13af6 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 t_on_lock(pPager
13af7 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b , EXCLUSIVE_LOCK
13af8 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
13af9 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 .. /* If the
13afa 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 73 20 77 required locks w
13afb 65 72 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79 ere successfully
13afc 20 6f 62 74 61 69 6e 65 64 2c 20 6f 70 65 6e 20 obtained, open
13afd 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 the journal.
13afe 2a 2a 20 66 69 6c 65 20 61 6e 64 20 77 72 69 74 ** file and writ
13aff 65 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72 e the first jour
13b00 6e 61 6c 2d 68 65 61 64 65 72 20 74 6f 20 69 74 nal-header to it
13b01 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
13b02 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
13b03 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 & pPager->journa
13b04 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 lMode!=PAGER_JOU
13b05 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a RNALMODE_OFF ){.
13b06 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
13b07 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 _open_journal(pP
13b08 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ager);. }. }
13b09 65 6c 73 65 20 69 66 28 20 69 73 4f 70 65 6e 28 else if( isOpen(
13b0a 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 pPager->jfd) &&
13b0b 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
13b0c 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 ff==0 ){. /*
13b0d 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 This happens whe
13b0e 6e 20 74 68 65 20 70 61 67 65 72 20 77 61 73 20 n the pager was
13b0f 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 61 63 63 in exclusive-acc
13b10 65 73 73 20 6d 6f 64 65 20 74 68 65 20 6c 61 73 ess mode the las
13b11 74 0a 20 20 20 20 2a 2a 20 74 69 6d 65 20 61 20 t. ** time a
13b12 28 72 65 61 64 20 6f 72 20 77 72 69 74 65 29 20 (read or write)
13b13 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 transaction was
13b14 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6e successfully con
13b15 63 6c 75 64 65 64 0a 20 20 20 20 2a 2a 20 62 79 cluded. ** by
13b16 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e this connection
13b17 2e 20 49 6e 73 74 65 61 64 20 6f 66 20 64 65 6c . Instead of del
13b18 65 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 eting the journa
13b19 6c 20 66 69 6c 65 20 69 74 20 77 61 73 20 0a 20 l file it was .
13b1a 20 20 20 2a 2a 20 6b 65 70 74 20 6f 70 65 6e 20 ** kept open
13b1b 61 6e 64 20 65 69 74 68 65 72 20 77 61 73 20 74 and either was t
13b1c 72 75 6e 63 61 74 65 64 20 74 6f 20 30 20 62 79 runcated to 0 by
13b1d 74 65 73 20 6f 72 20 69 74 73 20 68 65 61 64 65 tes or its heade
13b1e 72 20 77 61 73 0a 20 20 20 20 2a 2a 20 6f 76 65 r was. ** ove
13b1f 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 7a 65 rwritten with ze
13b20 72 6f 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ros.. */.
13b21 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
13b22 6e 52 65 63 3d 3d 30 20 29 3b 0a 20 20 20 20 61 nRec==0 );. a
13b23 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 ssert( pPager->d
13b24 62 4f 72 69 67 53 69 7a 65 3d 3d 30 20 29 3b 0a bOrigSize==0 );.
13b25 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
13b26 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d er->pInJournal==
13b27 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 0 );. rc = pa
13b28 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c ger_open_journal
13b29 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 (pPager);. }..
13b2a 20 50 41 47 45 52 54 52 41 43 45 28 28 22 54 52 PAGERTRACE(("TR
13b2b 41 4e 53 41 43 54 49 4f 4e 20 25 64 5c 6e 22 2c ANSACTION %d\n",
13b2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 PAGERID(pPager)
13b2d 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69 ));. assert( !i
13b2e 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
13b2f 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f d) || pPager->jo
13b30 75 72 6e 61 6c 4f 66 66 3e 30 20 7c 7c 20 72 63 urnalOff>0 || rc
13b31 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 !=SQLITE_OK );.
13b32 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
13b33 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 OK ){. assert
13b34 28 20 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 ( !pPager->dbMod
13b35 69 66 69 65 64 20 29 3b 0a 20 20 20 20 2f 2a 20 ified );. /*
13b36 49 67 6e 6f 72 65 20 61 6e 79 20 49 4f 20 65 72 Ignore any IO er
13b37 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 ror that occurs
13b38 77 69 74 68 69 6e 20 70 61 67 65 72 5f 65 6e 64 within pager_end
13b39 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 2e 20 _transaction().
13b3a 54 68 65 0a 20 20 20 20 2a 2a 20 70 75 72 70 6f The. ** purpo
13b3b 73 65 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 20 se of this call
13b3c 69 73 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 is to reset the
13b3d 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 20 6f internal state o
13b3e 66 20 74 68 65 20 70 61 67 65 72 0a 20 20 20 20 f the pager.
13b3f 2a 2a 20 73 75 62 2d 73 79 73 74 65 6d 2e 20 49 ** sub-system. I
13b40 74 20 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 t doesn't matter
13b41 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d if the journal-
13b42 66 69 6c 65 20 69 73 20 6e 6f 74 20 70 72 6f 70 file is not prop
13b43 65 72 6c 79 0a 20 20 20 20 2a 2a 20 66 69 6e 61 erly. ** fina
13b44 6c 69 7a 65 64 20 61 74 20 74 68 69 73 20 70 6f lized at this po
13b45 69 6e 74 20 28 73 69 6e 63 65 20 69 74 20 69 73 int (since it is
13b46 20 6e 6f 74 20 61 20 76 61 6c 69 64 20 6a 6f 75 not a valid jou
13b47 72 6e 61 6c 20 66 69 6c 65 20 61 6e 79 77 61 79 rnal file anyway
13b48 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 61 ).. */. pa
13b49 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 ger_end_transact
13b4a 69 6f 6e 28 70 50 61 67 65 72 2c 20 30 29 3b 0a ion(pPager, 0);.
13b4b 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
13b4c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 .}../*.** Mark a
13b4d 20 73 69 6e 67 6c 65 20 64 61 74 61 20 70 61 67 single data pag
13b4e 65 20 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 e as writeable.
13b4f 54 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74 The page is writ
13b50 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a ten into the .**
13b51 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 main journal or
13b52 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 61 73 20 sub-journal as
13b53 72 65 71 75 69 72 65 64 2e 20 49 66 20 74 68 65 required. If the
13b54 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65 6e page is written
13b55 20 69 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f 66 20 into.** one of
13b56 74 68 65 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 the journals, th
13b57 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
13b58 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 bit is set in th
13b59 65 20 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a e .** Pager.pInJ
13b5a 6f 75 72 6e 61 6c 20 62 69 74 76 65 63 20 61 6e ournal bitvec an
13b5b 64 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 d the PagerSavep
13b5c 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e oint.pInSavepoin
13b5d 74 20 62 69 74 76 65 63 73 0a 2a 2a 20 6f 66 20 t bitvecs.** of
13b5e 61 6e 79 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 any open savepoi
13b5f 6e 74 73 20 61 73 20 61 70 70 72 6f 70 72 69 61 nts as appropria
13b60 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e te..*/.static in
13b61 74 20 70 61 67 65 72 5f 77 72 69 74 65 28 50 67 t pager_write(Pg
13b62 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f 69 Hdr *pPg){. voi
13b63 64 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e d *pData = pPg->
13b64 70 44 61 74 61 3b 0a 20 20 50 61 67 65 72 20 2a pData;. Pager *
13b65 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 pPager = pPg->pP
13b66 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d ager;. int rc =
13b67 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f SQLITE_OK;.. /
13b68 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
13b69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20 75 6e 6c s not called unl
13b6a 65 73 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f ess a transactio
13b6b 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 n has already be
13b6c 65 6e 0a 20 20 2a 2a 20 73 74 61 72 74 65 64 2e en. ** started.
13b6d 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
13b6e 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 pPager->state>=P
13b6f 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b AGER_RESERVED );
13b70 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 .. /* If an err
13b71 6f 72 20 68 61 73 20 62 65 65 6e 20 70 72 65 76 or has been prev
13b72 69 6f 75 73 6c 79 20 64 65 74 65 63 74 65 64 2c iously detected,
13b73 20 77 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 we should not b
13b74 65 0a 20 20 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 e. ** calling t
13b75 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 52 65 his routine. Re
13b76 70 65 61 74 20 74 68 65 20 65 72 72 6f 72 20 66 peat the error f
13b77 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 2e 0a 20 or robustness..
13b78 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 */. if( NEVER(
13b79 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 pPager->errCode)
13b7a 20 29 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 ) return pPage
13b7b 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f r->errCode;.. /
13b7c 2a 20 48 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 * Higher-level r
13b7d 6f 75 74 69 6e 65 73 20 6e 65 76 65 72 20 63 61 outines never ca
13b7e 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ll this function
13b7f 20 69 66 20 64 61 74 61 62 61 73 65 20 69 73 20 if database is
13b80 6e 6f 74 0a 20 20 2a 2a 20 77 72 69 74 61 62 6c not. ** writabl
13b81 65 2e 20 20 42 75 74 20 63 68 65 63 6b 20 61 6e e. But check an
13b82 79 77 61 79 2c 20 6a 75 73 74 20 66 6f 72 20 72 yway, just for r
13b83 6f 62 75 73 74 6e 65 73 73 2e 20 2a 2f 0a 20 20 obustness. */.
13b84 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 if( NEVER(pPager
13b85 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29 20 72 65 ->readOnly) ) re
13b86 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d turn SQLITE_PERM
13b87 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 ;.. assert( !pP
13b88 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 ager->setMaster
13b89 29 3b 0a 0a 20 20 43 48 45 43 4b 5f 50 41 47 45 );.. CHECK_PAGE
13b8a 28 70 50 67 29 3b 0a 0a 20 20 2f 2a 20 4d 61 72 (pPg);.. /* Mar
13b8b 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 64 69 k the page as di
13b8c 72 74 79 2e 20 20 49 66 20 74 68 65 20 70 61 67 rty. If the pag
13b8d 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 e has already be
13b8e 65 6e 20 77 72 69 74 74 65 6e 0a 20 20 2a 2a 20 en written. **
13b8f 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 to the journal t
13b90 68 65 6e 20 77 65 20 63 61 6e 20 72 65 74 75 72 hen we can retur
13b91 6e 20 72 69 67 68 74 20 61 77 61 79 2e 0a 20 20 n right away..
13b92 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 */. sqlite3Pcac
13b93 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50 67 29 heMakeDirty(pPg)
13b94 3b 0a 20 20 69 66 28 20 70 61 67 65 49 6e 4a 6f ;. if( pageInJo
13b95 75 72 6e 61 6c 28 70 50 67 29 20 26 26 20 21 73 urnal(pPg) && !s
13b96 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 ubjRequiresPage(
13b97 70 50 67 29 20 29 7b 0a 20 20 20 20 70 50 61 67 pPg) ){. pPag
13b98 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d er->dbModified =
13b99 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 1;. }else{..
13b9a 20 20 2f 2a 20 49 66 20 77 65 20 67 65 74 20 74 /* If we get t
13b9b 68 69 73 20 66 61 72 2c 20 69 74 20 6d 65 61 6e his far, it mean
13b9c 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 s that the page
13b9d 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 needs to be.
13b9e 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 ** written to th
13b9f 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f e transaction jo
13ba0 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 63 6b 65 urnal or the cke
13ba1 63 6b 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 0a ckpoint journal.
13ba2 20 20 20 20 2a 2a 20 6f 72 20 62 6f 74 68 2e 0a ** or both..
13ba3 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 48 69 **. ** Hi
13ba4 67 68 65 72 20 6c 65 76 65 6c 20 72 6f 75 74 69 gher level routi
13ba5 6e 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20 nes should have
13ba6 61 6c 72 65 61 64 79 20 73 74 61 72 74 65 64 20 already started
13ba7 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 a transaction,.
13ba8 20 20 20 2a 2a 20 77 68 69 63 68 20 6d 65 61 6e ** which mean
13ba9 73 20 74 68 65 79 20 68 61 76 65 20 61 63 71 75 s they have acqu
13baa 69 72 65 64 20 74 68 65 20 6e 65 63 65 73 73 61 ired the necessa
13bab 72 79 20 6c 6f 63 6b 73 20 61 6e 64 20 6f 70 65 ry locks and ope
13bac 6e 65 64 0a 20 20 20 20 2a 2a 20 61 20 72 6f 6c ned. ** a rol
13bad 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 lback journal.
13bae 44 6f 75 62 6c 65 2d 63 68 65 63 6b 20 74 6f 20 Double-check to
13baf 6d 61 6b 65 73 20 73 75 72 65 20 74 68 69 73 20 makes sure this
13bb0 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20 20 20 is the case..
13bb1 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c */. rc = sql
13bb2 69 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 70 ite3PagerBegin(p
13bb3 50 61 67 65 72 2c 20 30 2c 20 70 50 61 67 65 72 Pager, 0, pPager
13bb4 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 3b ->subjInMemory);
13bb5 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 . if( NEVER(r
13bb6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b c!=SQLITE_OK) ){
13bb7 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
13bb8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
13bb9 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e !isOpen(pPager->
13bba 6a 66 64 29 20 26 26 20 70 50 61 67 65 72 2d 3e jfd) && pPager->
13bbb 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 journalMode!=PAG
13bbc 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f ER_JOURNALMODE_O
13bbd 46 46 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 FF ){. asse
13bbe 72 74 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a rt( pPager->useJ
13bbf 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 20 20 20 20 ournal );.
13bc0 72 63 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f rc = pager_open_
13bc1 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b journal(pPager);
13bc2 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
13bc3 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 QLITE_OK ) retur
13bc4 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
13bc5 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 pPager->dbModifi
13bc6 65 64 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20 2f ed = 1;. . /
13bc7 2a 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f * The transactio
13bc8 6e 20 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 78 n journal now ex
13bc9 69 73 74 73 20 61 6e 64 20 77 65 20 68 61 76 65 ists and we have
13bca 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 61 a RESERVED or a
13bcb 6e 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 n. ** EXCLUSI
13bcc 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6d VE lock on the m
13bcd 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c ain database fil
13bce 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 63 75 e. Write the cu
13bcf 72 72 65 6e 74 20 70 61 67 65 20 74 6f 0a 20 20 rrent page to.
13bd0 20 20 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 ** the transac
13bd1 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66 20 tion journal if
13bd2 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20 it is not there
13bd3 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a already.. */.
13bd4 20 20 20 20 69 66 28 20 21 70 61 67 65 49 6e 4a if( !pageInJ
13bd5 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 26 20 69 ournal(pPg) && i
13bd6 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
13bd7 64 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 d) ){. if(
13bd8 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 pPg->pgno<=pPage
13bd9 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b r->dbOrigSize ){
13bda 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 6b 73 . u32 cks
13bdb 75 6d 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 um;. char
13bdc 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 20 *pData2;..
13bdd 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 /* We should
13bde 6e 65 76 65 72 20 77 72 69 74 65 20 74 6f 20 74 never write to t
13bdf 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
13be0 74 68 65 20 70 61 67 65 20 74 68 61 74 0a 20 20 the page that.
13be1 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e ** contain
13be2 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c s the database l
13be3 6f 63 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f ocks. The follo
13be4 77 69 6e 67 20 61 73 73 65 72 74 20 76 65 72 69 wing assert veri
13be5 66 69 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 fies. **
13be6 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e 20 that we do not.
13be7 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 */. asser
13be8 74 28 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50 41 t( pPg->pgno!=PA
13be9 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 GER_MJ_PGNO(pPag
13bea 65 72 29 20 29 3b 0a 20 20 20 20 20 20 20 20 43 er) );. C
13beb 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 70 44 ODEC2(pPager, pD
13bec 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 ata, pPg->pgno,
13bed 37 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 7, return SQLITE
13bee 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 32 29 3b _NOMEM, pData2);
13bef 0a 20 20 20 20 20 20 20 20 63 6b 73 75 6d 20 3d . cksum =
13bf0 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 70 50 61 pager_cksum(pPa
13bf1 67 65 72 2c 20 28 75 38 2a 29 70 44 61 74 61 32 ger, (u8*)pData2
13bf2 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
13bf3 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67 write32bits(pPag
13bf4 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d er->jfd, pPager-
13bf5 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 67 >journalOff, pPg
13bf6 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 ->pgno);.
13bf7 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
13bf8 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
13bf9 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
13bfa 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ite(pPager->jfd,
13bfb 20 70 44 61 74 61 32 2c 20 70 50 61 67 65 72 2d pData2, pPager-
13bfc 3e 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 >pageSize,.
13bfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13bfe 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d pPager-
13bff 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 20 34 29 >journalOff + 4)
13c00 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 ;. pPag
13c01 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b er->journalOff +
13c02 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 = pPager->pageSi
13c03 7a 65 2b 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a ze+4;. }.
13c04 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
13c05 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
13c06 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 rc = writ
13c07 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e e32bits(pPager->
13c08 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 jfd, pPager->jou
13c09 72 6e 61 6c 4f 66 66 2c 20 63 6b 73 75 6d 29 3b rnalOff, cksum);
13c0a 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 . pPage
13c0b 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d r->journalOff +=
13c0c 20 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 4;. }.
13c0d 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 IOTRACE(("
13c0e 4a 4f 55 54 20 25 70 20 25 64 20 25 6c 6c 64 20 JOUT %p %d %lld
13c0f 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 %d\n", pPager, p
13c10 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 20 Pg->pgno, .
13c11 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 67 pPag
13c12 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 er->journalOff,
13c13 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
13c14 29 29 3b 0a 20 20 20 20 20 20 20 20 50 41 47 45 ));. PAGE
13c15 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 R_INCR(sqlite3_p
13c16 61 67 65 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e ager_writej_coun
13c17 74 29 3b 0a 20 20 20 20 20 20 20 20 50 41 47 45 t);. PAGE
13c18 52 54 52 41 43 45 28 28 22 4a 4f 55 52 4e 41 4c RTRACE(("JOURNAL
13c19 20 25 64 20 70 61 67 65 20 25 64 20 6e 65 65 64 %d page %d need
13c1a 53 79 6e 63 3d 25 64 20 68 61 73 68 28 25 30 38 Sync=%d hash(%08
13c1b 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 x)\n",.
13c1c 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 PAGERID(pPag
13c1d 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 er), pPg->pgno,
13c1e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 28 28 . ((
13c1f 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 pPg->flags&PGHDR
13c20 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 29 _NEED_SYNC)?1:0)
13c21 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 , pager_pagehash
13c22 28 70 50 67 29 29 29 3b 0a 0a 20 20 20 20 20 20 (pPg)));..
13c23 20 20 2f 2a 20 45 76 65 6e 20 69 66 20 61 6e 20 /* Even if an
13c24 49 4f 20 6f 72 20 64 69 73 6b 66 75 6c 6c 20 65 IO or diskfull e
13c25 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 77 68 rror occurred wh
13c26 69 6c 65 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 ile journalling
13c27 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 the. ** p
13c28 61 67 65 20 69 6e 20 74 68 65 20 62 6c 6f 63 6b age in the block
13c29 20 61 62 6f 76 65 2c 20 73 65 74 20 74 68 65 20 above, set the
13c2a 6e 65 65 64 2d 73 79 6e 63 20 66 6c 61 67 20 66 need-sync flag f
13c2b 6f 72 20 74 68 65 20 70 61 67 65 2e 0a 20 20 20 or the page..
13c2c 20 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 ** Otherwis
13c2d 65 2c 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e e, when the tran
13c2e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 saction is rolle
13c2f 64 20 62 61 63 6b 2c 20 74 68 65 20 6c 6f 67 69 d back, the logi
13c30 63 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 c in. **
13c31 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 playback_one_pag
13c32 65 28 29 20 77 69 6c 6c 20 74 68 69 6e 6b 20 74 e() will think t
13c33 68 61 74 20 74 68 65 20 70 61 67 65 20 6e 65 65 hat the page nee
13c34 64 73 20 74 6f 20 62 65 20 72 65 73 74 6f 72 65 ds to be restore
13c35 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 d. ** in
13c36 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
13c37 65 2e 20 41 6e 64 20 69 66 20 61 6e 20 49 4f 20 e. And if an IO
13c38 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 error occurs whi
13c39 6c 65 20 64 6f 69 6e 67 20 73 6f 2c 0a 20 20 20 le doing so,.
13c3a 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 6f 72 ** then cor
13c3b 72 75 70 74 69 6f 6e 20 6d 61 79 20 66 6f 6c 6c ruption may foll
13c3c 6f 77 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 ow.. */.
13c3d 20 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67 if( !pPag
13c3e 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 er->noSync ){.
13c3f 20 20 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 pPg->fla
13c40 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 gs |= PGHDR_NEED
13c41 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 _SYNC;.
13c42 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
13c43 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d c = 1;. }
13c44 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 .. /* An
13c45 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 error has occurr
13c46 65 64 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 ed writing to th
13c47 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 e journal file.
13c48 54 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 The . **
13c49 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c transaction will
13c4a 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 be rolled back
13c4b 62 79 20 74 68 65 20 6c 61 79 65 72 20 61 62 6f by the layer abo
13c4c 76 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 ve.. */.
13c4d 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
13c4e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
13c4f 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
13c50 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 . }..
13c51 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 pPager->nRec
13c52 2b 2b 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ++;. asse
13c53 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a rt( pPager->pInJ
13c54 6f 75 72 6e 61 6c 21 3d 30 20 29 3b 0a 20 20 20 ournal!=0 );.
13c55 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
13c56 33 42 69 74 76 65 63 53 65 74 28 70 50 61 67 65 3BitvecSet(pPage
13c57 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 r->pInJournal, p
13c58 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 Pg->pgno);.
13c59 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 3d testcase( rc=
13c5a 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b =SQLITE_NOMEM );
13c5b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
13c5c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc==SQLITE_OK |
13c5d 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d | rc==SQLITE_NOM
13c5e 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 EM );. rc
13c5f 20 7c 3d 20 61 64 64 54 6f 53 61 76 65 70 6f 69 |= addToSavepoi
13c60 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 65 72 ntBitvecs(pPager
13c61 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 , pPg->pgno);.
13c62 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
13c63 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
13c64 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d assert( rc=
13c65 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b =SQLITE_NOMEM );
13c66 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
13c67 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a n rc;. }.
13c68 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
13c69 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 if( !pPager
13c6a 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 ->journalStarted
13c6b 20 26 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 && !pPager->noS
13c6c 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ync ){.
13c6d 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 pPg->flags |= P
13c6e 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a GHDR_NEED_SYNC;.
13c6f 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 pPager
13c70 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a ->needSync = 1;.
13c71 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
13c72 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 41 PAGERTRACE(("A
13c73 50 50 45 4e 44 20 25 64 20 70 61 67 65 20 25 64 PPEND %d page %d
13c74 20 6e 65 65 64 53 79 6e 63 3d 25 64 5c 6e 22 2c needSync=%d\n",
13c75 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
13c76 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 PAGERID(pPager)
13c77 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 0a 20 20 20 , pPg->pgno,.
13c78 20 20 20 20 20 20 20 20 20 20 20 20 28 28 70 50 ((pP
13c79 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
13c7a 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 29 29 29 EED_SYNC)?1:0)))
13c7b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
13c7c 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
13c7d 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e statement journ
13c7e 61 6c 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 al is open and t
13c7f 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 he page is not i
13c80 6e 20 69 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 n it,. ** the
13c81 6e 20 77 72 69 74 65 20 74 68 65 20 63 75 72 72 n write the curr
13c82 65 6e 74 20 70 61 67 65 20 74 6f 20 74 68 65 20 ent page to the
13c83 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 statement journa
13c84 6c 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 l. Note that.
13c85 20 20 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 ** the stateme
13c86 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 nt journal forma
13c87 74 20 64 69 66 66 65 72 73 20 66 72 6f 6d 20 74 t differs from t
13c88 68 65 20 73 74 61 6e 64 61 72 64 20 6a 6f 75 72 he standard jour
13c89 6e 61 6c 20 66 6f 72 6d 61 74 0a 20 20 20 20 2a nal format. *
13c8a 2a 20 69 6e 20 74 68 61 74 20 69 74 20 6f 6d 69 * in that it omi
13c8b 74 73 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73 ts the checksums
13c8c 20 61 6e 64 20 74 68 65 20 68 65 61 64 65 72 2e and the header.
13c8d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
13c8e 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 subjRequiresPage
13c8f 28 70 50 67 29 20 29 7b 0a 20 20 20 20 20 20 72 (pPg) ){. r
13c90 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 c = subjournalPa
13c91 67 65 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20 ge(pPg);. }.
13c92 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 }.. /* Update
13c93 74 68 65 20 64 61 74 61 62 61 73 65 20 73 69 7a the database siz
13c94 65 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20 e and return..
13c95 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 */. assert( pPa
13c96 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 ger->state>=PAGE
13c97 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20 69 66 R_SHARED );. if
13c98 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 ( pPager->dbSize
13c99 3c 70 50 67 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 <pPg->pgno ){.
13c9a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
13c9b 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 = pPg->pgno;.
13c9c 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
13c9d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 64 ../*.** Mark a d
13c9e 61 74 61 20 70 61 67 65 20 61 73 20 77 72 69 74 ata page as writ
13c9f 65 61 62 6c 65 2e 20 54 68 69 73 20 72 6f 75 74 eable. This rout
13ca0 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c ine must be call
13ca1 65 64 20 62 65 66 6f 72 65 20 0a 2a 2a 20 6d 61 ed before .** ma
13ca2 6b 69 6e 67 20 63 68 61 6e 67 65 73 20 74 6f 20 king changes to
13ca3 61 20 70 61 67 65 2e 20 54 68 65 20 63 61 6c 6c a page. The call
13ca4 65 72 20 6d 75 73 74 20 63 68 65 63 6b 20 74 68 er must check th
13ca5 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 0a e return value .
13ca6 2a 2a 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 ** of this funct
13ca7 69 6f 6e 20 61 6e 64 20 62 65 20 63 61 72 65 66 ion and be caref
13ca8 75 6c 20 6e 6f 74 20 74 6f 20 63 68 61 6e 67 65 ul not to change
13ca9 20 61 6e 79 20 70 61 67 65 20 64 61 74 61 20 75 any page data u
13caa 6e 6c 65 73 73 20 0a 2a 2a 20 74 68 69 73 20 72 nless .** this r
13cab 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 outine returns S
13cac 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 QLITE_OK..**.**
13cad 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 The difference b
13cae 65 74 77 65 65 6e 20 74 68 69 73 20 66 75 6e 63 etween this func
13caf 74 69 6f 6e 20 61 6e 64 20 70 61 67 65 72 5f 77 tion and pager_w
13cb0 72 69 74 65 28 29 20 69 73 20 74 68 61 74 20 74 rite() is that t
13cb1 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 his.** function
13cb2 61 6c 73 6f 20 64 65 61 6c 73 20 77 69 74 68 20 also deals with
13cb3 74 68 65 20 73 70 65 63 69 61 6c 20 63 61 73 65 the special case
13cb4 20 77 68 65 72 65 20 32 20 6f 72 20 6d 6f 72 65 where 2 or more
13cb5 20 70 61 67 65 73 0a 2a 2a 20 66 69 74 20 6f 6e pages.** fit on
13cb6 20 61 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 a single disk s
13cb7 65 63 74 6f 72 2e 20 49 6e 20 74 68 69 73 20 63 ector. In this c
13cb8 61 73 65 20 61 6c 6c 20 63 6f 2d 72 65 73 69 64 ase all co-resid
13cb9 65 6e 74 20 70 61 67 65 73 0a 2a 2a 20 6d 75 73 ent pages.** mus
13cba 74 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 t have been writ
13cbb 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ten to the journ
13cbc 61 6c 20 66 69 6c 65 20 62 65 66 6f 72 65 20 72 al file before r
13cbd 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 eturning..**.**
13cbe 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
13cbf 72 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rs, SQLITE_NOMEM
13cc0 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 or an IO error
13cc1 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
13cc2 0a 2a 2a 20 61 73 20 61 70 70 72 6f 70 72 69 61 .** as appropria
13cc3 74 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 te. Otherwise, S
13cc4 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c QLITE_OK..*/.SQL
13cc5 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
13cc6 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
13cc7 65 28 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 e(DbPage *pDbPag
13cc8 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 e){. int rc = S
13cc9 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 50 67 48 QLITE_OK;.. PgH
13cca 64 72 20 2a 70 50 67 20 3d 20 70 44 62 50 61 67 dr *pPg = pDbPag
13ccb 65 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 e;. Pager *pPag
13ccc 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 er = pPg->pPager
13ccd 3b 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 50 65 ;. Pgno nPagePe
13cce 72 53 65 63 74 6f 72 20 3d 20 28 70 50 61 67 65 rSector = (pPage
13ccf 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 2f 70 50 r->sectorSize/pP
13cd0 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b ager->pageSize);
13cd1 0a 0a 20 20 69 66 28 20 6e 50 61 67 65 50 65 72 .. if( nPagePer
13cd2 53 65 63 74 6f 72 3e 31 20 29 7b 0a 20 20 20 20 Sector>1 ){.
13cd3 50 67 6e 6f 20 6e 50 61 67 65 43 6f 75 6e 74 3b Pgno nPageCount;
13cd4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 /* Tot
13cd5 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 al number of pag
13cd6 65 73 20 69 6e 20 64 61 74 61 62 61 73 65 20 66 es in database f
13cd7 69 6c 65 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 ile */. Pgno
13cd8 70 67 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 pg1;
13cd9 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 /* First pa
13cda 67 65 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 ge of the sector
13cdb 20 70 50 67 20 69 73 20 6c 6f 63 61 74 65 64 20 pPg is located
13cdc 6f 6e 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e on. */. int n
13cdd 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
13cde 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
13cdf 66 20 70 61 67 65 73 20 73 74 61 72 74 69 6e 67 f pages starting
13ce0 20 61 74 20 70 67 31 20 74 6f 20 6a 6f 75 72 6e at pg1 to journ
13ce1 61 6c 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 al */. int ii
13ce2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
13ce3 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
13ce4 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e ter */. int n
13ce5 65 65 64 53 79 6e 63 20 3d 20 30 3b 20 20 20 20 eedSync = 0;
13ce6 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
13ce7 61 6e 79 20 70 61 67 65 20 68 61 73 20 50 47 48 any page has PGH
13ce8 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 2a 2f 0a DR_NEED_SYNC */.
13ce9 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 . /* Set the
13cea 64 6f 4e 6f 74 53 79 6e 63 20 66 6c 61 67 20 74 doNotSync flag t
13ceb 6f 20 31 2e 20 54 68 69 73 20 69 73 20 62 65 63 o 1. This is bec
13cec 61 75 73 65 20 77 65 20 63 61 6e 6e 6f 74 20 61 ause we cannot a
13ced 6c 6c 6f 77 20 61 20 6a 6f 75 72 6e 61 6c 0a 20 llow a journal.
13cee 20 20 20 2a 2a 20 68 65 61 64 65 72 20 74 6f 20 ** header to
13cef 62 65 20 77 72 69 74 74 65 6e 20 62 65 74 77 65 be written betwe
13cf0 65 6e 20 74 68 65 20 70 61 67 65 73 20 6a 6f 75 en the pages jou
13cf1 72 6e 61 6c 65 64 20 62 79 20 74 68 69 73 20 66 rnaled by this f
13cf2 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a unction.. */.
13cf3 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d assert( !MEM
13cf4 44 42 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 DB );. assert
13cf5 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 ( pPager->doNotS
13cf6 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50 ync==0 );. pP
13cf7 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 ager->doNotSync
13cf8 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 = 1;.. /* Thi
13cf9 73 20 74 72 69 63 6b 20 61 73 73 75 6d 65 73 20 s trick assumes
13cfa 74 68 61 74 20 62 6f 74 68 20 74 68 65 20 70 61 that both the pa
13cfb 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 ge-size and sect
13cfc 6f 72 2d 73 69 7a 65 20 61 72 65 0a 20 20 20 20 or-size are.
13cfd 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 70 6f ** an integer po
13cfe 77 65 72 20 6f 66 20 32 2e 20 49 74 20 73 65 74 wer of 2. It set
13cff 73 20 76 61 72 69 61 62 6c 65 20 70 67 31 20 74 s variable pg1 t
13d00 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 o the identifier
13d01 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 . ** of the f
13d02 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 irst page of the
13d03 20 73 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c sector pPg is l
13d04 6f 63 61 74 65 64 20 6f 6e 2e 0a 20 20 20 20 2a ocated on.. *
13d05 2f 0a 20 20 20 20 70 67 31 20 3d 20 28 28 70 50 /. pg1 = ((pP
13d06 67 2d 3e 70 67 6e 6f 2d 31 29 20 26 20 7e 28 6e g->pgno-1) & ~(n
13d07 50 61 67 65 50 65 72 53 65 63 74 6f 72 2d 31 29 PagePerSector-1)
13d08 29 20 2b 20 31 3b 0a 0a 20 20 20 20 73 71 6c 69 ) + 1;.. sqli
13d09 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e te3PagerPagecoun
13d0a 74 28 70 50 61 67 65 72 2c 20 28 69 6e 74 20 2a t(pPager, (int *
13d0b 29 26 6e 50 61 67 65 43 6f 75 6e 74 29 3b 0a 20 )&nPageCount);.
13d0c 20 20 20 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f if( pPg->pgno
13d0d 3e 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b 0a 20 >nPageCount ){.
13d0e 20 20 20 20 20 6e 50 61 67 65 20 3d 20 28 70 50 nPage = (pP
13d0f 67 2d 3e 70 67 6e 6f 20 2d 20 70 67 31 29 2b 31 g->pgno - pg1)+1
13d10 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
13d11 28 70 67 31 2b 6e 50 61 67 65 50 65 72 53 65 63 (pg1+nPagePerSec
13d12 74 6f 72 2d 31 29 3e 6e 50 61 67 65 43 6f 75 6e tor-1)>nPageCoun
13d13 74 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 t ){. nPage
13d14 20 3d 20 6e 50 61 67 65 43 6f 75 6e 74 2b 31 2d = nPageCount+1-
13d15 70 67 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a pg1;. }else{.
13d16 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 6e 50 nPage = nP
13d17 61 67 65 50 65 72 53 65 63 74 6f 72 3b 0a 20 20 agePerSector;.
13d18 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 6e }. assert(n
13d19 50 61 67 65 3e 30 29 3b 0a 20 20 20 20 61 73 73 Page>0);. ass
13d1a 65 72 74 28 70 67 31 3c 3d 70 50 67 2d 3e 70 67 ert(pg1<=pPg->pg
13d1b 6e 6f 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 no);. assert(
13d1c 28 70 67 31 2b 6e 50 61 67 65 29 3e 70 50 67 2d (pg1+nPage)>pPg-
13d1d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 20 66 6f 72 >pgno);.. for
13d1e 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67 65 20 (ii=0; ii<nPage
13d1f 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b && rc==SQLITE_OK
13d20 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 50 ; ii++){. P
13d21 67 6e 6f 20 70 67 20 3d 20 70 67 31 2b 69 69 3b gno pg = pg1+ii;
13d22 0a 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 50 . PgHdr *pP
13d23 61 67 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 age;. if( p
13d24 67 3d 3d 70 50 67 2d 3e 70 67 6e 6f 20 7c 7c 20 g==pPg->pgno ||
13d25 21 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 !sqlite3BitvecTe
13d26 73 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f st(pPager->pInJo
13d27 75 72 6e 61 6c 2c 20 70 67 29 20 29 7b 0a 20 20 urnal, pg) ){.
13d28 20 20 20 20 20 20 69 66 28 20 70 67 21 3d 50 41 if( pg!=PA
13d29 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 GER_MJ_PGNO(pPag
13d2a 65 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 er) ){.
13d2b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
13d2c 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 70 67 erGet(pPager, pg
13d2d 2c 20 26 70 50 61 67 65 29 3b 0a 20 20 20 20 20 , &pPage);.
13d2e 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
13d2f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
13d30 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
13d31 5f 77 72 69 74 65 28 70 50 61 67 65 29 3b 0a 20 _write(pPage);.
13d32 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 if( p
13d33 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 Page->flags&PGHD
13d34 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 R_NEED_SYNC ){.
13d35 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 65 65 nee
13d36 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 dSync = 1;.
13d37 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
13d38 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 pPager->needSync
13d39 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d );. }
13d3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
13d3b 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
13d3c 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Page);.
13d3d 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
13d3e 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 50 }else if( (pP
13d3f 61 67 65 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b age = pager_look
13d40 75 70 28 70 50 61 67 65 72 2c 20 70 67 29 29 21 up(pPager, pg))!
13d41 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 =0 ){. if
13d42 28 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 ( pPage->flags&P
13d43 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 GHDR_NEED_SYNC )
13d44 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 {. need
13d45 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 Sync = 1;.
13d46 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 }. sqli
13d47 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 te3PagerUnref(pP
13d48 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 age);. }.
13d49 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 }.. /* If t
13d4a 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 he PGHDR_NEED_SY
13d4b 4e 43 20 66 6c 61 67 20 69 73 20 73 65 74 20 66 NC flag is set f
13d4c 6f 72 20 61 6e 79 20 6f 66 20 74 68 65 20 6e 50 or any of the nP
13d4d 61 67 65 20 70 61 67 65 73 20 0a 20 20 20 20 2a age pages . *
13d4e 2a 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 67 * starting at pg
13d4f 31 2c 20 74 68 65 6e 20 69 74 20 6e 65 65 64 73 1, then it needs
13d50 20 74 6f 20 62 65 20 73 65 74 20 66 6f 72 20 61 to be set for a
13d51 6c 6c 20 6f 66 20 74 68 65 6d 2e 20 42 65 63 61 ll of them. Beca
13d52 75 73 65 0a 20 20 20 20 2a 2a 20 77 72 69 74 69 use. ** writi
13d53 6e 67 20 74 6f 20 61 6e 79 20 6f 66 20 74 68 65 ng to any of the
13d54 73 65 20 6e 50 61 67 65 20 70 61 67 65 73 20 6d se nPage pages m
13d55 61 79 20 64 61 6d 61 67 65 20 74 68 65 20 6f 74 ay damage the ot
13d56 68 65 72 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a hers, the. **
13d57 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 journal file mu
13d58 73 74 20 63 6f 6e 74 61 69 6e 20 73 79 6e 63 28 st contain sync(
13d59 29 65 64 20 63 6f 70 69 65 73 20 6f 66 20 61 6c )ed copies of al
13d5a 6c 20 6f 66 20 74 68 65 6d 0a 20 20 20 20 2a 2a l of them. **
13d5b 20 62 65 66 6f 72 65 20 61 6e 79 20 6f 66 20 74 before any of t
13d5c 68 65 6d 20 63 61 6e 20 62 65 20 77 72 69 74 74 hem can be writt
13d5d 65 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 en out to the da
13d5e 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 tabase file..
13d5f 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d */. if( rc==
13d60 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 65 65 SQLITE_OK && nee
13d61 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 61 dSync ){. a
13d62 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 26 26 ssert( !MEMDB &&
13d63 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d pPager->noSync=
13d64 3d 30 20 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 =0 );. for(
13d65 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67 65 3b 20 ii=0; ii<nPage;
13d66 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 50 ii++){. P
13d67 67 48 64 72 20 2a 70 50 61 67 65 20 3d 20 70 61 gHdr *pPage = pa
13d68 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 ger_lookup(pPage
13d69 72 2c 20 70 67 31 2b 69 69 29 3b 0a 20 20 20 20 r, pg1+ii);.
13d6a 20 20 20 20 69 66 28 20 70 50 61 67 65 20 29 7b if( pPage ){
13d6b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 . pPage
13d6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 ->flags |= PGHDR
13d6d 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 _NEED_SYNC;.
13d6e 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
13d6f 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a erUnref(pPage);.
13d70 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
13d71 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70 }. assert(p
13d72 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29 Pager->needSync)
13d73 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 ;. }.. ass
13d74 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e ert( pPager->doN
13d75 6f 74 53 79 6e 63 3d 3d 31 20 29 3b 0a 20 20 20 otSync==1 );.
13d76 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 pPager->doNotSy
13d77 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b nc = 0;. }else{
13d78 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f . rc = pager_
13d79 77 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a write(pDbPage);.
13d7a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
13d7b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
13d7c 20 54 52 55 45 20 69 66 20 74 68 65 20 70 61 67 TRUE if the pag
13d7d 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61 e given in the a
13d7e 72 67 75 6d 65 6e 74 20 77 61 73 20 70 72 65 76 rgument was prev
13d7f 69 6f 75 73 6c 79 20 70 61 73 73 65 64 0a 2a 2a iously passed.**
13d80 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 to sqlite3Pager
13d81 57 72 69 74 65 28 29 2e 20 20 49 6e 20 6f 74 68 Write(). In oth
13d82 65 72 20 77 6f 72 64 73 2c 20 72 65 74 75 72 6e er words, return
13d83 20 54 52 55 45 20 69 66 20 69 74 20 69 73 20 6f TRUE if it is o
13d84 6b 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20 74 k.** to change t
13d85 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 he content of th
13d86 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e 64 e page..*/.#ifnd
13d87 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45 ef NDEBUG.SQLITE
13d88 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
13d89 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
13d8a 61 62 6c 65 28 44 62 50 61 67 65 20 2a 70 50 67 able(DbPage *pPg
13d8b 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d ){. return pPg-
13d8c 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 >flags&PGHDR_DIR
13d8d 54 59 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a TY;.}.#endif../*
13d8e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 68 .** A call to th
13d8f 69 73 20 72 6f 75 74 69 6e 65 20 74 65 6c 6c 73 is routine tells
13d90 20 74 68 65 20 70 61 67 65 72 20 74 68 61 74 20 the pager that
13d91 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 it is not necess
13d92 61 72 79 20 74 6f 0a 2a 2a 20 77 72 69 74 65 20 ary to.** write
13d93 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 the information
13d94 6f 6e 20 70 61 67 65 20 70 50 67 20 62 61 63 6b on page pPg back
13d95 20 74 6f 20 74 68 65 20 64 69 73 6b 2c 20 65 76 to the disk, ev
13d96 65 6e 20 74 68 6f 75 67 68 0a 2a 2a 20 74 68 61 en though.** tha
13d97 74 20 70 61 67 65 20 6d 69 67 68 74 20 62 65 20 t page might be
13d98 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e marked as dirty.
13d99 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 This happens,
13d9a 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 for example, whe
13d9b 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 68 61 n.** the page ha
13d9c 73 20 62 65 65 6e 20 61 64 64 65 64 20 61 73 20 s been added as
13d9d 61 20 6c 65 61 66 20 6f 66 20 74 68 65 20 66 72 a leaf of the fr
13d9e 65 65 6c 69 73 74 20 61 6e 64 20 73 6f 20 69 74 eelist and so it
13d9f 73 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6e 6f 20 s.** content no
13da0 6c 6f 6e 67 65 72 20 6d 61 74 74 65 72 73 2e 0a longer matters..
13da1 2a 2a 0a 2a 2a 20 54 68 65 20 6f 76 65 72 6c 79 **.** The overly
13da2 69 6e 67 20 73 6f 66 74 77 61 72 65 20 6c 61 79 ing software lay
13da3 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f er calls this ro
13da4 75 74 69 6e 65 20 77 68 65 6e 20 61 6c 6c 20 6f utine when all o
13da5 66 20 74 68 65 20 64 61 74 61 0a 2a 2a 20 6f 6e f the data.** on
13da6 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 20 the given page
13da7 69 73 20 75 6e 75 73 65 64 2e 20 54 68 65 20 70 is unused. The p
13da8 61 67 65 72 20 6d 61 72 6b 73 20 74 68 65 20 70 ager marks the p
13da9 61 67 65 20 61 73 20 63 6c 65 61 6e 20 73 6f 0a age as clean so.
13daa 2a 2a 20 74 68 61 74 20 69 74 20 64 6f 65 73 20 ** that it does
13dab 6e 6f 74 20 67 65 74 20 77 72 69 74 74 65 6e 20 not get written
13dac 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 54 to disk..**.** T
13dad 65 73 74 73 20 73 68 6f 77 20 74 68 61 74 20 74 ests show that t
13dae 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e his optimization
13daf 20 63 61 6e 20 71 75 61 64 72 75 70 6c 65 20 74 can quadruple t
13db0 68 65 20 73 70 65 65 64 20 6f 66 20 6c 61 72 67 he speed of larg
13db1 65 20 0a 2a 2a 20 44 45 4c 45 54 45 20 6f 70 65 e .** DELETE ope
13db2 72 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 rations..*/.SQLI
13db3 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
13db4 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 sqlite3PagerDont
13db5 57 72 69 74 65 28 50 67 48 64 72 20 2a 70 50 67 Write(PgHdr *pPg
13db6 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 ){. Pager *pPag
13db7 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 er = pPg->pPager
13db8 3b 0a 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c ;. if( (pPg->fl
13db9 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 29 ags&PGHDR_DIRTY)
13dba 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 && pPager->nSav
13dbb 65 70 6f 69 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 epoint==0 ){.
13dbc 20 50 41 47 45 52 54 52 41 43 45 28 28 22 44 4f PAGERTRACE(("DO
13dbd 4e 54 5f 57 52 49 54 45 20 70 61 67 65 20 25 64 NT_WRITE page %d
13dbe 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d 3e of %d\n", pPg->
13dbf 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 28 70 50 pgno, PAGERID(pP
13dc0 61 67 65 72 29 29 29 3b 0a 20 20 20 20 49 4f 54 ager)));. IOT
13dc1 52 41 43 45 28 28 22 43 4c 45 41 4e 20 25 70 20 RACE(("CLEAN %p
13dc2 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 %d\n", pPager, p
13dc3 50 67 2d 3e 70 67 6e 6f 29 29 0a 20 20 20 20 70 Pg->pgno)). p
13dc4 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 Pg->flags |= PGH
13dc5 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 3b 0a 23 DR_DONT_WRITE;.#
13dc6 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 ifdef SQLITE_CHE
13dc7 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67 CK_PAGES. pPg
13dc8 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 ->pageHash = pag
13dc9 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 er_pagehash(pPg)
13dca 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a ;.#endif. }.}..
13dcb 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
13dcc 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 ne is called to
13dcd 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61 increment the va
13dce 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 lue of the datab
13dcf 61 73 65 20 66 69 6c 65 20 0a 2a 2a 20 63 68 61 ase file .** cha
13dd0 6e 67 65 2d 63 6f 75 6e 74 65 72 2c 20 73 74 6f nge-counter, sto
13dd1 72 65 64 20 61 73 20 61 20 34 2d 62 79 74 65 20 red as a 4-byte
13dd2 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 big-endian integ
13dd3 65 72 20 73 74 61 72 74 69 6e 67 20 61 74 20 0a er starting at .
13dd4 2a 2a 20 62 79 74 65 20 6f 66 66 73 65 74 20 32 ** byte offset 2
13dd5 34 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 66 4 of the pager f
13dd6 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ile..**.** If th
13dd7 65 20 69 73 44 69 72 65 63 74 4d 6f 64 65 20 66 e isDirectMode f
13dd8 6c 61 67 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 lag is zero, the
13dd9 6e 20 74 68 69 73 20 69 73 20 64 6f 6e 65 20 62 n this is done b
13dda 79 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 y calling .** sq
13ddb 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
13ddc 29 20 6f 6e 20 70 61 67 65 20 31 2c 20 74 68 65 ) on page 1, the
13ddd 6e 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 n modifying the
13dde 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a contents of the.
13ddf 2a 2a 20 70 61 67 65 20 64 61 74 61 2e 20 49 6e ** page data. In
13de0 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 66 this case the f
13de1 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 70 64 61 ile will be upda
13de2 74 65 64 20 77 68 65 6e 20 74 68 65 20 63 75 72 ted when the cur
13de3 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 rent.** transact
13de4 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 ion is committed
13de5 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 44 69 ..**.** The isDi
13de6 72 65 63 74 4d 6f 64 65 20 66 6c 61 67 20 6d 61 rectMode flag ma
13de7 79 20 6f 6e 6c 79 20 62 65 20 6e 6f 6e 2d 7a 65 y only be non-ze
13de8 72 6f 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 ro if the librar
13de9 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 0a 2a y was compiled.*
13dea 2a 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 * with the SQLIT
13deb 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f E_ENABLE_ATOMIC_
13dec 57 52 49 54 45 20 6d 61 63 72 6f 20 64 65 66 69 WRITE macro defi
13ded 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ned. In this cas
13dee 65 2c 0a 2a 2a 20 69 66 20 69 73 44 69 72 65 63 e,.** if isDirec
13def 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 t is non-zero, t
13df0 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 hen the database
13df1 20 66 69 6c 65 20 69 73 20 75 70 64 61 74 65 64 file is updated
13df2 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 directly.** by
13df3 77 72 69 74 69 6e 67 20 61 6e 20 75 70 64 61 74 writing an updat
13df4 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 ed version of pa
13df5 67 65 20 31 20 75 73 69 6e 67 20 61 20 63 61 6c ge 1 using a cal
13df6 6c 20 74 6f 20 74 68 65 20 0a 2a 2a 20 73 71 6c l to the .** sql
13df7 69 74 65 33 4f 73 57 72 69 74 65 28 29 20 66 75 ite3OsWrite() fu
13df8 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 nction..*/.stati
13df9 63 20 69 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 c int pager_incr
13dfa 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 _changecounter(P
13dfb 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e ager *pPager, in
13dfc 74 20 69 73 44 69 72 65 63 74 4d 6f 64 65 29 7b t isDirectMode){
13dfd 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
13dfe 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 44 65 63 TE_OK;.. /* Dec
13dff 6c 61 72 65 20 61 6e 64 20 69 6e 69 74 69 61 6c lare and initial
13e00 69 7a 65 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 ize constant int
13e01 65 67 65 72 20 27 69 73 44 69 72 65 63 74 27 2e eger 'isDirect'.
13e02 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 61 74 6f If the. ** ato
13e03 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 mic-write optimi
13e04 7a 61 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 zation is enable
13e05 64 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2c d in this build,
13e06 20 74 68 65 6e 20 69 73 44 69 72 65 63 74 0a 20 then isDirect.
13e07 20 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a ** is initializ
13e08 65 64 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 ed to the value
13e09 70 61 73 73 65 64 20 61 73 20 74 68 65 20 69 73 passed as the is
13e0a 44 69 72 65 63 74 4d 6f 64 65 20 70 61 72 61 6d DirectMode param
13e0b 65 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 69 eter. ** to thi
13e0c 73 20 66 75 6e 63 74 69 6f 6e 2e 20 4f 74 68 65 s function. Othe
13e0d 72 77 69 73 65 2c 20 69 74 20 69 73 20 61 6c 77 rwise, it is alw
13e0e 61 79 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e ays set to zero.
13e0f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 69 . **. ** The i
13e10 64 65 61 20 69 73 20 74 68 61 74 20 69 66 20 74 dea is that if t
13e11 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 he atomic-write
13e12 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 optimization is
13e13 6e 6f 74 0a 20 20 2a 2a 20 65 6e 61 62 6c 65 64 not. ** enabled
13e14 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 at compile time
13e15 2c 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 , the compiler c
13e16 61 6e 20 6f 6d 69 74 20 74 68 65 20 74 65 73 74 an omit the test
13e17 73 20 6f 66 0a 20 20 2a 2a 20 27 69 73 44 69 72 s of. ** 'isDir
13e18 65 63 74 27 20 62 65 6c 6f 77 2c 20 61 73 20 77 ect' below, as w
13e19 65 6c 6c 20 61 73 20 74 68 65 20 62 6c 6f 63 6b ell as the block
13e1a 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 74 68 65 enclosed in the
13e1b 0a 20 20 2a 2a 20 22 69 66 28 20 69 73 44 69 72 . ** "if( isDir
13e1c 65 63 74 20 29 22 20 63 6f 6e 64 69 74 69 6f 6e ect )" condition
13e1d 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 .. */.#ifndef S
13e1e 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f QLITE_ENABLE_ATO
13e1f 4d 49 43 5f 57 52 49 54 45 0a 23 20 64 65 66 69 MIC_WRITE.# defi
13e20 6e 65 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 30 ne DIRECT_MODE 0
13e21 0a 20 20 61 73 73 65 72 74 28 20 69 73 44 69 72 . assert( isDir
13e22 65 63 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a 20 20 ectMode==0 );.
13e23 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
13e24 28 69 73 44 69 72 65 63 74 4d 6f 64 65 29 3b 0a (isDirectMode);.
13e25 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 44 #else.# define D
13e26 49 52 45 43 54 5f 4d 4f 44 45 20 69 73 44 69 72 IRECT_MODE isDir
13e27 65 63 74 4d 6f 64 65 0a 23 65 6e 64 69 66 0a 0a ectMode.#endif..
13e28 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
13e29 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 ->state>=PAGER_R
13e2a 45 53 45 52 56 45 44 20 29 3b 0a 20 20 69 66 28 ESERVED );. if(
13e2b 20 21 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 !pPager->change
13e2c 43 6f 75 6e 74 44 6f 6e 65 20 26 26 20 41 4c 57 CountDone && ALW
13e2d 41 59 53 28 70 50 61 67 65 72 2d 3e 64 62 53 69 AYS(pPager->dbSi
13e2e 7a 65 3e 30 29 20 29 7b 0a 20 20 20 20 50 67 48 ze>0) ){. PgH
13e2f 64 72 20 2a 70 50 67 48 64 72 3b 20 20 20 20 20 dr *pPgHdr;
13e30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
13e31 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 ference to page
13e32 31 20 2a 2f 0a 20 20 20 20 75 33 32 20 63 68 61 1 */. u32 cha
13e33 6e 67 65 5f 63 6f 75 6e 74 65 72 3b 20 20 20 20 nge_counter;
13e34 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 /* Initia
13e35 6c 20 76 61 6c 75 65 20 6f 66 20 63 68 61 6e 67 l value of chang
13e36 65 2d 63 6f 75 6e 74 65 72 20 66 69 65 6c 64 20 e-counter field
13e37 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 */.. assert(
13e38 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c !pPager->tempFil
13e39 65 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 e && isOpen(pPag
13e3a 65 72 2d 3e 66 64 29 20 29 3b 0a 0a 20 20 20 20 er->fd) );..
13e3b 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 31 20 6f /* Open page 1 o
13e3c 66 20 74 68 65 20 66 69 6c 65 20 66 6f 72 20 77 f the file for w
13e3d 72 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 riting. */. r
13e3e 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
13e3f 47 65 74 28 70 50 61 67 65 72 2c 20 31 2c 20 26 Get(pPager, 1, &
13e40 70 50 67 48 64 72 29 3b 0a 20 20 20 20 61 73 73 pPgHdr);. ass
13e41 65 72 74 28 20 70 50 67 48 64 72 3d 3d 30 20 7c ert( pPgHdr==0 |
13e42 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 | rc==SQLITE_OK
13e43 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 61 );.. /* If pa
13e44 67 65 20 6f 6e 65 20 77 61 73 20 66 65 74 63 68 ge one was fetch
13e45 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c ed successfully,
13e46 20 61 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69 and this functi
13e47 6f 6e 20 69 73 20 6e 6f 74 0a 20 20 20 20 2a 2a on is not. **
13e48 20 6f 70 65 72 61 74 69 6e 67 20 69 6e 20 64 69 operating in di
13e49 72 65 63 74 2d 6d 6f 64 65 2c 20 6d 61 6b 65 20 rect-mode, make
13e4a 70 61 67 65 20 31 20 77 72 69 74 61 62 6c 65 2e page 1 writable.
13e4b 20 20 57 68 65 6e 20 6e 6f 74 20 69 6e 20 0a 20 When not in .
13e4c 20 20 20 2a 2a 20 64 69 72 65 63 74 20 6d 6f 64 ** direct mod
13e4d 65 2c 20 70 61 67 65 20 31 20 69 73 20 61 6c 77 e, page 1 is alw
13e4e 61 79 73 20 68 65 6c 64 20 69 6e 20 63 61 63 68 ays held in cach
13e4f 65 20 61 6e 64 20 68 65 6e 63 65 20 74 68 65 20 e and hence the
13e50 50 61 67 65 72 47 65 74 28 29 0a 20 20 20 20 2a PagerGet(). *
13e51 2a 20 61 62 6f 76 65 20 69 73 20 61 6c 77 61 79 * above is alway
13e52 73 20 73 75 63 63 65 73 73 66 75 6c 20 2d 20 68 s successful - h
13e53 65 6e 63 65 20 74 68 65 20 41 4c 57 41 59 53 20 ence the ALWAYS
13e54 6f 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b on rc==SQLITE_OK
13e55 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
13e56 20 21 44 49 52 45 43 54 5f 4d 4f 44 45 20 26 26 !DIRECT_MODE &&
13e57 20 41 4c 57 41 59 53 28 72 63 3d 3d 53 51 4c 49 ALWAYS(rc==SQLI
13e58 54 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 20 20 TE_OK) ){.
13e59 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
13e5a 72 57 72 69 74 65 28 70 50 67 48 64 72 29 3b 0a rWrite(pPgHdr);.
13e5b 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 }.. if( r
13e5c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
13e5d 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 /* Increme
13e5e 6e 74 20 74 68 65 20 76 61 6c 75 65 20 6a 75 73 nt the value jus
13e5f 74 20 72 65 61 64 20 61 6e 64 20 77 72 69 74 65 t read and write
13e60 20 69 74 20 62 61 63 6b 20 74 6f 20 62 79 74 65 it back to byte
13e61 20 32 34 2e 20 2a 2f 0a 20 20 20 20 20 20 63 68 24. */. ch
13e62 61 6e 67 65 5f 63 6f 75 6e 74 65 72 20 3d 20 73 ange_counter = s
13e63 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 28 qlite3Get4byte((
13e64 75 38 2a 29 70 50 61 67 65 72 2d 3e 64 62 46 69 u8*)pPager->dbFi
13e65 6c 65 56 65 72 73 29 3b 0a 20 20 20 20 20 20 63 leVers);. c
13e66 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 2b 2b 3b hange_counter++;
13e67 0a 20 20 20 20 20 20 70 75 74 33 32 62 69 74 73 . put32bits
13e68 28 28 28 63 68 61 72 2a 29 70 50 67 48 64 72 2d (((char*)pPgHdr-
13e69 3e 70 44 61 74 61 29 2b 32 34 2c 20 63 68 61 6e >pData)+24, chan
13e6a 67 65 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a 20 20 ge_counter);..
13e6b 20 20 20 20 2f 2a 20 49 66 20 72 75 6e 6e 69 6e /* If runnin
13e6c 67 20 69 6e 20 64 69 72 65 63 74 20 6d 6f 64 65 g in direct mode
13e6d 2c 20 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 , write the cont
13e6e 65 6e 74 73 20 6f 66 20 70 61 67 65 20 31 20 74 ents of page 1 t
13e6f 6f 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 o the file. */.
13e70 20 20 20 20 20 69 66 28 20 44 49 52 45 43 54 5f if( DIRECT_
13e71 4d 4f 44 45 20 29 7b 0a 20 20 20 20 20 20 20 20 MODE ){.
13e72 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 const void *zBuf
13e73 20 3d 20 70 50 67 48 64 72 2d 3e 70 44 61 74 61 = pPgHdr->pData
13e74 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
13e75 28 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 ( pPager->dbFile
13e76 53 69 7a 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 Size>0 );.
13e77 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
13e78 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 Write(pPager->fd
13e79 2c 20 7a 42 75 66 2c 20 70 50 61 67 65 72 2d 3e , zBuf, pPager->
13e7a 70 61 67 65 53 69 7a 65 2c 20 30 29 3b 0a 20 20 pageSize, 0);.
13e7b 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
13e7c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
13e7d 20 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 pPager->cha
13e7e 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 ngeCountDone = 1
13e7f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
13e80 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
13e81 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 pPager->changeC
13e82 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a 20 20 ountDone = 1;.
13e83 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
13e84 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 /* Release the
13e85 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e 20 page reference.
13e86 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 */. sqlite3Pa
13e87 67 65 72 55 6e 72 65 66 28 70 50 67 48 64 72 29 gerUnref(pPgHdr)
13e88 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
13e89 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 c;.}../*.** Sync
13e8a 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 20 the pager file
13e8b 74 6f 20 64 69 73 6b 2e 20 54 68 69 73 20 69 73 to disk. This is
13e8c 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 69 6e 2d a no-op for in-
13e8d 6d 65 6d 6f 72 79 20 66 69 6c 65 73 0a 2a 2a 20 memory files.**
13e8e 6f 72 20 70 61 67 65 73 20 77 69 74 68 20 74 68 or pages with th
13e8f 65 20 50 61 67 65 72 2e 6e 6f 53 79 6e 63 20 66 e Pager.noSync f
13e90 6c 61 67 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 lag set..**.** I
13e91 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 f successful, or
13e92 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 70 61 67 called on a pag
13e93 65 72 20 66 6f 72 20 77 68 69 63 68 20 69 74 20 er for which it
13e94 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 74 68 69 73 is a no-op, this
13e95 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 .** function ret
13e96 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 urns SQLITE_OK.
13e97 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 49 4f Otherwise, an IO
13e98 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
13e99 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 eturned..*/.SQLI
13e9a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
13e9b 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 qlite3PagerSync(
13e9c 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
13e9d 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
13e9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13e9f 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
13ea0 20 63 6f 64 65 20 2a 2f 0a 20 20 61 73 73 65 72 code */. asser
13ea1 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 69 t( !MEMDB );. i
13ea2 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e f( pPager->noSyn
13ea3 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 c ){. rc = SQ
13ea4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 LITE_OK;. }else
13ea5 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
13ea6 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d e3OsSync(pPager-
13ea7 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e >fd, pPager->syn
13ea8 63 5f 66 6c 61 67 73 29 3b 0a 20 20 7d 0a 20 20 c_flags);. }.
13ea9 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
13eaa 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 64 61 74 .** Sync the dat
13eab 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72 20 74 abase file for t
13eac 68 65 20 70 61 67 65 72 20 70 50 61 67 65 72 2e he pager pPager.
13ead 20 7a 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20 zMaster points
13eae 74 6f 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f to the name.** o
13eaf 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e f a master journ
13eb0 61 6c 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f al file that sho
13eb1 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e 20 69 uld be written i
13eb2 6e 74 6f 20 74 68 65 20 69 6e 64 69 76 69 64 75 nto the individu
13eb3 61 6c 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 al.** journal fi
13eb4 6c 65 2e 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 le. zMaster may
13eb5 62 65 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 be NULL, which i
13eb6 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 s interpreted as
13eb7 20 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f no master.** jo
13eb8 75 72 6e 61 6c 20 28 61 20 73 69 6e 67 6c 65 20 urnal (a single
13eb9 64 61 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 database transac
13eba 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 tion)..**.** Thi
13ebb 73 20 72 6f 75 74 69 6e 65 20 65 6e 73 75 72 65 s routine ensure
13ebc 73 20 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 s that:.**.**
13ebd 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 * The database f
13ebe 69 6c 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 ile change-count
13ebf 65 72 20 69 73 20 75 70 64 61 74 65 64 2c 0a 2a er is updated,.*
13ec0 2a 20 20 20 2a 20 74 68 65 20 6a 6f 75 72 6e 61 * * the journa
13ec1 6c 20 69 73 20 73 79 6e 63 65 64 20 28 75 6e 6c l is synced (unl
13ec2 65 73 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 ess the atomic-w
13ec3 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f rite optimizatio
13ec4 6e 20 69 73 20 75 73 65 64 29 2c 0a 2a 2a 20 20 n is used),.**
13ec5 20 2a 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 * all dirty pag
13ec6 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 74 es are written t
13ec7 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
13ec8 69 6c 65 2c 20 0a 2a 2a 20 20 20 2a 20 74 68 65 ile, .** * the
13ec9 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
13eca 73 20 74 72 75 6e 63 61 74 65 64 20 28 69 66 20 s truncated (if
13ecb 72 65 71 75 69 72 65 64 29 2c 20 61 6e 64 0a 2a required), and.*
13ecc 2a 20 20 20 2a 20 74 68 65 20 64 61 74 61 62 61 * * the databa
13ecd 73 65 20 66 69 6c 65 20 73 79 6e 63 65 64 2e 20 se file synced.
13ece 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 .**.** The only
13ecf 74 68 69 6e 67 20 74 68 61 74 20 72 65 6d 61 69 thing that remai
13ed0 6e 73 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65 ns to commit the
13ed1 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
13ed2 74 6f 20 66 69 6e 61 6c 69 7a 65 20 0a 2a 2a 20 to finalize .**
13ed3 28 64 65 6c 65 74 65 2c 20 74 72 75 6e 63 61 74 (delete, truncat
13ed4 65 20 6f 72 20 7a 65 72 6f 20 74 68 65 20 66 69 e or zero the fi
13ed5 72 73 74 20 70 61 72 74 20 6f 66 29 20 74 68 65 rst part of) the
13ed6 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f journal file (o
13ed7 72 20 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 r .** delete the
13ed8 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
13ed9 66 69 6c 65 20 69 66 20 73 70 65 63 69 66 69 65 file if specifie
13eda 64 29 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 d)..**.** Note t
13edb 68 61 74 20 69 66 20 7a 4d 61 73 74 65 72 3d 3d hat if zMaster==
13edc 4e 55 4c 4c 2c 20 74 68 69 73 20 64 6f 65 73 20 NULL, this does
13edd 6e 6f 74 20 6f 76 65 72 77 72 69 74 65 20 61 20 not overwrite a
13ede 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 0a 2a previous value.*
13edf 2a 20 70 61 73 73 65 64 20 74 6f 20 61 6e 20 73 * passed to an s
13ee0 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 qlite3PagerCommi
13ee1 74 50 68 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c tPhaseOne() call
13ee2 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 ..**.** If the f
13ee3 69 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 20 2d inal parameter -
13ee4 20 6e 6f 53 79 6e 63 20 2d 20 69 73 20 74 72 75 noSync - is tru
13ee5 65 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 e, then the data
13ee6 62 61 73 65 20 66 69 6c 65 20 69 74 73 65 6c 66 base file itself
13ee7 0a 2a 2a 20 69 73 20 6e 6f 74 20 73 79 6e 63 65 .** is not synce
13ee8 64 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 d. The caller mu
13ee9 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 st call sqlite3P
13eea 61 67 65 72 53 79 6e 63 28 29 20 64 69 72 65 63 agerSync() direc
13eeb 74 6c 79 20 74 6f 0a 2a 2a 20 73 79 6e 63 20 74 tly to.** sync t
13eec 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
13eed 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 before calling
13eee 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 CommitPhaseTwo()
13eef 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 0a 2a to delete the.*
13ef0 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 * journal file i
13ef1 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2f 0a n this case..*/.
13ef2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
13ef3 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 nt sqlite3PagerC
13ef4 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 0a 20 ommitPhaseOne(.
13ef5 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 Pager *pPager,
13ef6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13ef7 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 /* Pager object
13ef8 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
13ef9 20 2a 7a 4d 61 73 74 65 72 2c 20 20 20 20 20 20 *zMaster,
13efa 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74 20 /* If not
13efb 4e 55 4c 4c 2c 20 74 68 65 20 6d 61 73 74 65 72 NULL, the master
13efc 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f journal name */
13efd 0a 20 20 69 6e 74 20 6e 6f 53 79 6e 63 20 20 20 . int noSync
13efe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13eff 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d /* True to om
13f00 69 74 20 74 68 65 20 78 53 79 6e 63 20 6f 6e 20 it the xSync on
13f01 74 68 65 20 64 62 20 66 69 6c 65 20 2a 2f 0a 29 the db file */.)
13f02 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
13f03 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 ITE_OK;
13f04 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
13f05 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 de */.. /* The
13f06 64 62 4f 72 69 67 53 69 7a 65 20 69 73 20 6e 65 dbOrigSize is ne
13f07 76 65 72 20 73 65 74 20 69 66 20 6a 6f 75 72 6e ver set if journ
13f08 61 6c 5f 6d 6f 64 65 3d 4f 46 46 20 2a 2f 0a 20 al_mode=OFF */.
13f09 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13f0a 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 >journalMode!=PA
13f0b 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
13f0c 4f 46 46 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 OFF || pPager->d
13f0d 62 4f 72 69 67 53 69 7a 65 3d 3d 30 20 29 3b 0a bOrigSize==0 );.
13f0e 0a 20 20 2f 2a 20 49 66 20 61 20 70 72 69 6f 72 . /* If a prior
13f0f 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c error occurred,
13f10 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 this routine sh
13f11 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c ould not be call
13f12 65 64 2e 20 20 52 4f 4c 4c 42 41 43 4b 0a 20 20 ed. ROLLBACK.
13f13 2a 2a 20 69 73 20 74 68 65 20 61 70 70 72 6f 70 ** is the approp
13f14 72 69 61 74 65 20 72 65 73 70 6f 6e 73 65 20 74 riate response t
13f15 6f 20 61 6e 20 65 72 72 6f 72 2c 20 6e 6f 74 20 o an error, not
13f16 43 4f 4d 4d 49 54 2e 20 20 47 75 61 72 64 20 61 COMMIT. Guard a
13f17 67 61 69 6e 73 74 0a 20 20 2a 2a 20 63 6f 64 69 gainst. ** codi
13f18 6e 67 20 65 72 72 6f 72 73 20 62 79 20 72 65 70 ng errors by rep
13f19 65 61 74 69 6e 67 20 74 68 65 20 70 72 69 6f 72 eating the prior
13f1a 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 69 66 28 error. */. if(
13f1b 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 NEVER(pPager->e
13f1c 72 72 43 6f 64 65 29 20 29 20 72 65 74 75 72 6e rrCode) ) return
13f1d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
13f1e 3b 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 ;.. PAGERTRACE(
13f1f 28 22 44 41 54 41 42 41 53 45 20 53 59 4e 43 3a ("DATABASE SYNC:
13f20 20 46 69 6c 65 3d 25 73 20 7a 4d 61 73 74 65 72 File=%s zMaster
13f21 3d 25 73 20 6e 53 69 7a 65 3d 25 64 5c 6e 22 2c =%s nSize=%d\n",
13f22 20 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e . pPager->
13f23 7a 46 69 6c 65 6e 61 6d 65 2c 20 7a 4d 61 73 74 zFilename, zMast
13f24 65 72 2c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 er, pPager->dbSi
13f25 7a 65 29 29 3b 0a 0a 20 20 69 66 28 20 4d 45 4d ze));.. if( MEM
13f26 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62 DB && pPager->db
13f27 4d 6f 64 69 66 69 65 64 20 29 7b 0a 20 20 20 20 Modified ){.
13f28 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e /* If this is an
13f29 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 62 2c 20 6f in-memory db, o
13f2a 72 20 6e 6f 20 70 61 67 65 73 20 68 61 76 65 20 r no pages have
13f2b 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 2c been written to,
13f2c 20 6f 72 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 or this. **
13f2d 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 function has alr
13f2e 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 eady been called
13f2f 2c 20 69 74 20 69 73 20 6d 6f 73 74 6c 79 20 61 , it is mostly a
13f30 20 6e 6f 2d 6f 70 2e 20 20 48 6f 77 65 76 65 72 no-op. However
13f31 2c 20 61 6e 79 0a 20 20 20 20 2a 2a 20 62 61 63 , any. ** bac
13f32 6b 75 70 20 69 6e 20 70 72 6f 67 72 65 73 73 20 kup in progress
13f33 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 needs to be rest
13f34 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 arted.. */.
13f35 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 sqlite3BackupR
13f36 65 73 74 61 72 74 28 70 50 61 67 65 72 2d 3e 70 estart(pPager->p
13f37 42 61 63 6b 75 70 29 3b 0a 20 20 7d 65 6c 73 65 Backup);. }else
13f38 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 if( pPager->sta
13f39 74 65 21 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 te!=PAGER_SYNCED
13f3a 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f && pPager->dbMo
13f3b 64 69 66 69 65 64 20 29 7b 0a 0a 20 20 20 20 2f dified ){.. /
13f3c 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
13f3d 62 6c 6f 63 6b 20 75 70 64 61 74 65 73 20 74 68 block updates th
13f3e 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 e change-counter
13f3f 2e 20 45 78 61 63 74 6c 79 20 68 6f 77 20 69 74 . Exactly how it
13f40 0a 20 20 20 20 2a 2a 20 64 6f 65 73 20 74 68 69 . ** does thi
13f41 73 20 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 65 s depends on whe
13f42 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 ther or not the
13f43 61 74 6f 6d 69 63 2d 75 70 64 61 74 65 20 6f 70 atomic-update op
13f44 74 69 6d 69 7a 61 74 69 6f 6e 0a 20 20 20 20 2a timization. *
13f45 2a 20 77 61 73 20 65 6e 61 62 6c 65 64 20 61 74 * was enabled at
13f46 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 61 compile time, a
13f47 6e 64 20 69 66 20 74 68 69 73 20 74 72 61 6e 73 nd if this trans
13f48 61 63 74 69 6f 6e 20 6d 65 65 74 73 20 74 68 65 action meets the
13f49 20 0a 20 20 20 20 2a 2a 20 72 75 6e 74 69 6d 65 . ** runtime
13f4a 20 63 72 69 74 65 72 69 61 20 74 6f 20 75 73 65 criteria to use
13f4b 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 3a 20 the operation:
13f4c 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 . **. **
13f4d 20 20 2a 20 54 68 65 20 66 69 6c 65 2d 73 79 73 * The file-sys
13f4e 74 65 6d 20 73 75 70 70 6f 72 74 73 20 74 68 65 tem supports the
13f4f 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 70 72 atomic-write pr
13f50 6f 70 65 72 74 79 20 66 6f 72 0a 20 20 20 20 2a operty for. *
13f51 2a 20 20 20 20 20 20 62 6c 6f 63 6b 73 20 6f 66 * blocks of
13f52 20 73 69 7a 65 20 70 61 67 65 2d 73 69 7a 65 2c size page-size,
13f53 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 20 20 20 and . **
13f54 2a 20 54 68 69 73 20 63 6f 6d 6d 69 74 20 69 73 * This commit is
13f55 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 20 6d not part of a m
13f56 75 6c 74 69 2d 66 69 6c 65 20 74 72 61 6e 73 61 ulti-file transa
13f57 63 74 69 6f 6e 2c 20 61 6e 64 0a 20 20 20 20 2a ction, and. *
13f58 2a 20 20 20 20 2a 20 45 78 61 63 74 6c 79 20 6f * * Exactly o
13f59 6e 65 20 70 61 67 65 20 68 61 73 20 62 65 65 6e ne page has been
13f5a 20 6d 6f 64 69 66 69 65 64 20 61 6e 64 20 73 74 modified and st
13f5b 6f 72 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e ore in the journ
13f5c 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a al file.. **.
13f5d 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 6f 70 ** If the op
13f5e 74 69 6d 69 7a 61 74 69 6f 6e 20 77 61 73 20 6e timization was n
13f5f 6f 74 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f ot enabled at co
13f60 6d 70 69 6c 65 20 74 69 6d 65 2c 20 74 68 65 6e mpile time, then
13f61 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 the. ** page
13f62 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 r_incr_changecou
13f63 6e 74 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 nter() function
13f64 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 75 70 64 is called to upd
13f65 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 0a 20 ate the change.
13f66 20 20 20 2a 2a 20 63 6f 75 6e 74 65 72 20 69 6e ** counter in
13f67 20 27 69 6e 64 69 72 65 63 74 2d 6d 6f 64 65 27 'indirect-mode'
13f68 2e 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a . If the optimiz
13f69 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 69 6c 65 ation is compile
13f6a 64 20 69 6e 20 62 75 74 0a 20 20 20 20 2a 2a 20 d in but. **
13f6b 69 73 20 6e 6f 74 20 61 70 70 6c 69 63 61 62 6c is not applicabl
13f6c 65 20 74 6f 20 74 68 69 73 20 74 72 61 6e 73 61 e to this transa
13f6d 63 74 69 6f 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 ction, call sqli
13f6e 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 te3JournalCreate
13f6f 28 29 0a 20 20 20 20 2a 2a 20 74 6f 20 6d 61 6b (). ** to mak
13f70 65 20 73 75 72 65 20 74 68 65 20 6a 6f 75 72 6e e sure the journ
13f71 61 6c 20 66 69 6c 65 20 68 61 73 20 61 63 74 75 al file has actu
13f72 61 6c 6c 79 20 62 65 65 6e 20 63 72 65 61 74 65 ally been create
13f73 64 2c 20 74 68 65 6e 20 63 61 6c 6c 0a 20 20 20 d, then call.
13f74 20 2a 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 ** pager_incr_c
13f75 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74 hangecounter() t
13f76 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61 o update the cha
13f77 6e 67 65 2d 63 6f 75 6e 74 65 72 20 69 6e 20 69 nge-counter in i
13f78 6e 64 69 72 65 63 74 0a 20 20 20 20 2a 2a 20 6d ndirect. ** m
13f79 6f 64 65 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 ode. . **.
13f7a 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 ** Otherwise, i
13f7b 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 f the optimizati
13f7c 6f 6e 20 69 73 20 62 6f 74 68 20 65 6e 61 62 6c on is both enabl
13f7d 65 64 20 61 6e 64 20 61 70 70 6c 69 63 61 62 6c ed and applicabl
13f7e 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 e,. ** then c
13f7f 61 6c 6c 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 all pager_incr_c
13f80 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74 hangecounter() t
13f81 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61 o update the cha
13f82 6e 67 65 2d 63 6f 75 6e 74 65 72 0a 20 20 20 20 nge-counter.
13f83 2a 2a 20 69 6e 20 27 64 69 72 65 63 74 27 20 6d ** in 'direct' m
13f84 6f 64 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ode. In this cas
13f85 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 e the journal fi
13f86 6c 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 le will never be
13f87 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 . ** created
13f88 66 6f 72 20 74 68 69 73 20 74 72 61 6e 73 61 63 for this transac
13f89 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 tion.. */.#if
13f8a 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
13f8b 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 E_ATOMIC_WRITE.
13f8c 20 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 20 PgHdr *pPg;.
13f8d 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 assert( isOpe
13f8e 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c n(pPager->jfd) |
13f8f 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 | pPager->journa
13f90 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
13f91 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a RNALMODE_OFF );.
13f92 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74 65 72 if( !zMaster
13f93 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 && isOpen(pPage
13f94 72 2d 3e 6a 66 64 29 20 0a 20 20 20 20 20 26 26 r->jfd) . &&
13f95 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
13f96 4f 66 66 3d 3d 6a 72 6e 6c 42 75 66 66 65 72 53 Off==jrnlBufferS
13f97 69 7a 65 28 70 50 61 67 65 72 29 20 0a 20 20 20 ize(pPager) .
13f98 20 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62 53 && pPager->dbS
13f99 69 7a 65 3e 3d 70 50 61 67 65 72 2d 3e 64 62 46 ize>=pPager->dbF
13f9a 69 6c 65 53 69 7a 65 0a 20 20 20 20 20 26 26 20 ileSize. &&
13f9b 28 30 3d 3d 28 70 50 67 20 3d 20 73 71 6c 69 74 (0==(pPg = sqlit
13f9c 65 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 e3PcacheDirtyLis
13f9d 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 t(pPager->pPCach
13f9e 65 29 29 20 7c 7c 20 30 3d 3d 70 50 67 2d 3e 70 e)) || 0==pPg->p
13f9f 44 69 72 74 79 29 0a 20 20 20 20 29 7b 0a 20 20 Dirty). ){.
13fa0 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 /* Update th
13fa1 65 20 64 62 20 66 69 6c 65 20 63 68 61 6e 67 65 e db file change
13fa2 20 63 6f 75 6e 74 65 72 20 76 69 61 20 74 68 65 counter via the
13fa3 20 64 69 72 65 63 74 2d 77 72 69 74 65 20 6d 65 direct-write me
13fa4 74 68 6f 64 2e 20 54 68 65 20 0a 20 20 20 20 20 thod. The .
13fa5 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 ** following ca
13fa6 6c 6c 20 77 69 6c 6c 20 6d 6f 64 69 66 79 20 74 ll will modify t
13fa7 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 he in-memory rep
13fa8 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70 resentation of p
13fa9 61 67 65 20 31 20 0a 20 20 20 20 20 20 2a 2a 20 age 1 . **
13faa 74 6f 20 69 6e 63 6c 75 64 65 20 74 68 65 20 75 to include the u
13fab 70 64 61 74 65 64 20 63 68 61 6e 67 65 20 63 6f pdated change co
13fac 75 6e 74 65 72 20 61 6e 64 20 74 68 65 6e 20 77 unter and then w
13fad 72 69 74 65 20 70 61 67 65 20 31 20 0a 20 20 20 rite page 1 .
13fae 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 74 ** directly t
13faf 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
13fb0 69 6c 65 2e 20 42 65 63 61 75 73 65 20 6f 66 20 ile. Because of
13fb1 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 the atomic-write
13fb2 20 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f 70 65 . ** prope
13fb3 72 74 79 20 6f 66 20 74 68 65 20 68 6f 73 74 20 rty of the host
13fb4 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68 69 file-system, thi
13fb5 73 20 69 73 20 73 61 66 65 2e 0a 20 20 20 20 20 s is safe..
13fb6 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 70 */. rc = p
13fb7 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 ager_incr_change
13fb8 63 6f 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 counter(pPager,
13fb9 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 1);. }else{.
13fba 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
13fbb 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 70 3JournalCreate(p
13fbc 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 Pager->jfd);.
13fbd 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
13fbe 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
13fbf 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f rc = pager_incr_
13fc0 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 changecounter(pP
13fc1 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 ager, 0);.
13fc2 7d 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 }. }.#else.
13fc3 20 20 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 rc = pager_inc
13fc4 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 r_changecounter(
13fc5 70 50 61 67 65 72 2c 20 30 29 3b 0a 23 65 6e 64 pPager, 0);.#end
13fc6 69 66 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 if. if( rc!=S
13fc7 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 QLITE_OK ) goto
13fc8 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 commit_phase_one
13fc9 5f 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 _exit;.. /* I
13fca 66 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 f this transacti
13fcb 6f 6e 20 68 61 73 20 6d 61 64 65 20 74 68 65 20 on has made the
13fcc 64 61 74 61 62 61 73 65 20 73 6d 61 6c 6c 65 72 database smaller
13fcd 2c 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 , then all pages
13fce 0a 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 64 69 . ** being di
13fcf 73 63 61 72 64 65 64 20 62 79 20 74 68 65 20 74 scarded by the t
13fd0 72 75 6e 63 61 74 69 6f 6e 20 6d 75 73 74 20 62 runcation must b
13fd1 65 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 e written to the
13fd2 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 journal. **
13fd3 66 69 6c 65 2e 20 54 68 69 73 20 63 61 6e 20 6f file. This can o
13fd4 6e 6c 79 20 68 61 70 70 65 6e 20 69 6e 20 61 75 nly happen in au
13fd5 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 2e 0a to-vacuum mode..
13fd6 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 42 65 **. ** Be
13fd7 66 6f 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 fore reading the
13fd8 20 70 61 67 65 73 20 77 69 74 68 20 70 61 67 65 pages with page
13fd9 20 6e 75 6d 62 65 72 73 20 6c 61 72 67 65 72 20 numbers larger
13fda 74 68 61 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a than the . **
13fdb 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f current value o
13fdc 66 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 f Pager.dbSize,
13fdd 73 65 74 20 64 62 53 69 7a 65 20 62 61 63 6b 20 set dbSize back
13fde 74 6f 20 74 68 65 20 76 61 6c 75 65 0a 20 20 20 to the value.
13fdf 20 2a 2a 20 74 68 61 74 20 69 74 20 74 6f 6f 6b ** that it took
13fe0 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 at the start of
13fe1 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
13fe2 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 . Otherwise, the
13fe3 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20 74 6f . ** calls to
13fe4 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
13fe5 28 29 20 72 65 74 75 72 6e 20 7a 65 72 6f 65 64 () return zeroed
13fe6 20 70 61 67 65 73 20 69 6e 73 74 65 61 64 20 6f pages instead o
13fe7 66 20 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e f . ** readin
13fe8 67 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 g data from the
13fe9 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 database file..
13fea 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 68 65 **. ** Whe
13feb 6e 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 3d n journal_mode==
13fec 4f 46 46 20 74 68 65 20 64 62 4f 72 69 67 53 69 OFF the dbOrigSi
13fed 7a 65 20 69 73 20 61 6c 77 61 79 73 20 7a 65 72 ze is always zer
13fee 6f 2c 20 73 6f 20 74 68 69 73 0a 20 20 20 20 2a o, so this. *
13fef 2a 20 62 6c 6f 63 6b 20 6e 65 76 65 72 20 72 75 * block never ru
13ff0 6e 73 20 69 66 20 6a 6f 75 72 6e 61 6c 5f 6d 6f ns if journal_mo
13ff1 64 65 3d 4f 46 46 2e 0a 20 20 20 20 2a 2f 0a 23 de=OFF.. */.#
13ff2 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
13ff3 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
13ff4 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 if( pPager->db
13ff5 53 69 7a 65 3c 70 50 61 67 65 72 2d 3e 64 62 4f Size<pPager->dbO
13ff6 72 69 67 53 69 7a 65 20 0a 20 20 20 20 20 26 26 rigSize . &&
13ff7 20 41 4c 57 41 59 53 28 70 50 61 67 65 72 2d 3e ALWAYS(pPager->
13ff8 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 journalMode!=PAG
13ff9 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f ER_JOURNALMODE_O
13ffa 46 46 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 FF). ){.
13ffb 20 50 67 6e 6f 20 69 3b 20 20 20 20 20 20 20 20 Pgno i;
13ffc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13ffd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 /* It
13ffe 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 erator variable
13fff 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50 */. const P
14000 67 6e 6f 20 69 53 6b 69 70 20 3d 20 50 41 47 45 gno iSkip = PAGE
14001 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 R_MJ_PGNO(pPager
14002 29 3b 20 2f 2a 20 50 65 6e 64 69 6e 67 20 6c 6f ); /* Pending lo
14003 63 6b 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 ck page */.
14004 20 63 6f 6e 73 74 20 50 67 6e 6f 20 64 62 53 69 const Pgno dbSi
14005 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 ze = pPager->dbS
14006 69 7a 65 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 ize; /* Da
14007 74 61 62 61 73 65 20 69 6d 61 67 65 20 73 69 7a tabase image siz
14008 65 20 2a 2f 20 0a 20 20 20 20 20 20 70 50 61 67 e */ . pPag
14009 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 50 61 er->dbSize = pPa
1400a 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3b ger->dbOrigSize;
1400b 0a 20 20 20 20 20 20 66 6f 72 28 20 69 3d 64 62 . for( i=db
1400c 53 69 7a 65 2b 31 3b 20 69 3c 3d 70 50 61 67 65 Size+1; i<=pPage
1400d 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3b 20 69 r->dbOrigSize; i
1400e 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 ++ ){. if
1400f 28 20 21 73 71 6c 69 74 65 33 42 69 74 76 65 63 ( !sqlite3Bitvec
14010 54 65 73 74 28 70 50 61 67 65 72 2d 3e 70 49 6e Test(pPager->pIn
14011 4a 6f 75 72 6e 61 6c 2c 20 69 29 20 26 26 20 69 Journal, i) && i
14012 21 3d 69 53 6b 69 70 20 29 7b 0a 20 20 20 20 20 !=iSkip ){.
14013 20 20 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 PgHdr *pPag
14014 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f e; /
14015 2a 20 50 61 67 65 20 74 6f 20 6a 6f 75 72 6e 61 * Page to journa
14016 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 l */. r
14017 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
14018 47 65 74 28 70 50 61 67 65 72 2c 20 69 2c 20 26 Get(pPager, i, &
14019 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 pPage);.
1401a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1401b 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 _OK ) goto commi
1401c 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 t_phase_one_exit
1401d 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d ;. rc =
1401e 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
1401f 74 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 te(pPage);.
14020 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
14021 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20 rUnref(pPage);.
14022 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 if( rc!
14023 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 =SQLITE_OK ) got
14024 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f o commit_phase_o
14025 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 ne_exit;.
14026 20 7d 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 }. } .
14027 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
14028 20 3d 20 64 62 53 69 7a 65 3b 0a 20 20 20 20 7d = dbSize;. }
14029 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 .#endif.. /*
1402a 57 72 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 Write the master
1402b 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e journal name in
1402c 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
1402d 69 6c 65 2e 20 49 66 20 61 20 6d 61 73 74 65 72 ile. If a master
1402e 20 0a 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c . ** journal
1402f 20 66 69 6c 65 20 6e 61 6d 65 20 68 61 73 20 61 file name has a
14030 6c 72 65 61 64 79 20 62 65 65 6e 20 77 72 69 74 lready been writ
14031 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ten to the journ
14032 61 6c 20 66 69 6c 65 2c 20 0a 20 20 20 20 2a 2a al file, . **
14033 20 6f 72 20 69 66 20 7a 4d 61 73 74 65 72 20 69 or if zMaster i
14034 73 20 4e 55 4c 4c 20 28 6e 6f 20 6d 61 73 74 65 s NULL (no maste
14035 72 20 6a 6f 75 72 6e 61 6c 29 2c 20 74 68 65 6e r journal), then
14036 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 this call is a
14037 6e 6f 2d 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 no-op.. */.
14038 20 20 72 63 20 3d 20 77 72 69 74 65 4d 61 73 74 rc = writeMast
14039 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 erJournal(pPager
1403a 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 , zMaster);.
1403b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1403c 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f K ) goto commit_
1403d 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a phase_one_exit;.
1403e 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 . /* Sync the
1403f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 journal file. I
14040 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 75 70 64 f the atomic-upd
14041 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ate optimization
14042 20 69 73 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a is being. **
14043 20 75 73 65 64 2c 20 74 68 69 73 20 63 61 6c 6c used, this call
14044 20 77 69 6c 6c 20 6e 6f 74 20 63 72 65 61 74 65 will not create
14045 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
14046 65 20 6f 72 20 70 65 72 66 6f 72 6d 20 61 6e 79 e or perform any
14047 0a 20 20 20 20 2a 2a 20 72 65 61 6c 20 49 4f 2e . ** real IO.
14048 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
14049 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61 syncJournal(pPa
1404a 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 ger);. if( rc
1404b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
1404c 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f to commit_phase_
1404d 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 20 20 20 2f one_exit;.. /
1404e 2a 20 57 72 69 74 65 20 61 6c 6c 20 64 69 72 74 * Write all dirt
1404f 79 20 70 61 67 65 73 20 74 6f 20 74 68 65 20 64 y pages to the d
14050 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f atabase file. */
14051 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f . rc = pager_
14052 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28 73 write_pagelist(s
14053 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 72 74 qlite3PcacheDirt
14054 79 4c 69 73 74 28 70 50 61 67 65 72 2d 3e 70 50 yList(pPager->pP
14055 43 61 63 68 65 29 29 3b 0a 20 20 20 20 69 66 28 Cache));. if(
14056 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
14057 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
14058 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 rc!=SQLITE_IOERR
14059 5f 42 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 20 20 _BLOCKED );.
1405a 20 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 goto commit_ph
1405b 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 ase_one_exit;.
1405c 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 }. sqlite3P
1405d 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 cacheCleanAll(pP
1405e 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a ager->pPCache);.
1405f 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 . /* If the f
14060 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e ile on disk is n
14061 6f 74 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65 ot the same size
14062 20 61 73 20 74 68 65 20 64 61 74 61 62 61 73 65 as the database
14063 20 69 6d 61 67 65 2c 0a 20 20 20 20 2a 2a 20 74 image,. ** t
14064 68 65 6e 20 75 73 65 20 70 61 67 65 72 5f 74 72 hen use pager_tr
14065 75 6e 63 61 74 65 20 74 6f 20 67 72 6f 77 20 6f uncate to grow o
14066 72 20 73 68 72 69 6e 6b 20 74 68 65 20 66 69 6c r shrink the fil
14067 65 20 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 e here.. */.
14068 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 if( pPager->d
14069 62 53 69 7a 65 21 3d 70 50 61 67 65 72 2d 3e 64 bSize!=pPager->d
1406a 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 bFileSize ){.
1406b 20 20 20 50 67 6e 6f 20 6e 4e 65 77 20 3d 20 70 Pgno nNew = p
1406c 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 2d 20 Pager->dbSize -
1406d 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d (pPager->dbSize=
1406e 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 =PAGER_MJ_PGNO(p
1406f 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20 20 61 Pager));. a
14070 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
14071 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c tate>=PAGER_EXCL
14072 55 53 49 56 45 20 29 3b 0a 20 20 20 20 20 20 72 USIVE );. r
14073 63 20 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 61 c = pager_trunca
14074 74 65 28 70 50 61 67 65 72 2c 20 6e 4e 65 77 29 te(pPager, nNew)
14075 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
14076 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
14077 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e commit_phase_on
14078 65 5f 65 78 69 74 3b 0a 20 20 20 20 7d 0a 0a 20 e_exit;. }..
14079 20 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 73 /* Finally, s
1407a 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 ync the database
1407b 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 file. */. if
1407c 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e ( !pPager->noSyn
1407d 63 20 26 26 20 21 6e 6f 53 79 6e 63 20 29 7b 0a c && !noSync ){.
1407e 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1407f 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d e3OsSync(pPager-
14080 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e >fd, pPager->syn
14081 63 5f 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a c_flags);. }.
14082 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 44 42 IOTRACE(("DB
14083 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 SYNC %p\n", pPag
14084 65 72 29 29 0a 0a 20 20 20 20 70 50 61 67 65 72 er)).. pPager
14085 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f ->state = PAGER_
14086 53 59 4e 43 45 44 3b 0a 20 20 7d 0a 0a 63 6f 6d SYNCED;. }..com
14087 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 mit_phase_one_ex
14088 69 74 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b it:. return rc;
14089 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 .}.../*.** When
1408a 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
1408b 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 64 61 74 called, the dat
1408c 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 62 abase file has b
1408d 65 65 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a een completely.*
1408e 2a 20 75 70 64 61 74 65 64 20 74 6f 20 72 65 66 * updated to ref
1408f 6c 65 63 74 20 74 68 65 20 63 68 61 6e 67 65 73 lect the changes
14090 20 6d 61 64 65 20 62 79 20 74 68 65 20 63 75 72 made by the cur
14091 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e rent transaction
14092 20 61 6e 64 0a 2a 2a 20 73 79 6e 63 65 64 20 74 and.** synced t
14093 6f 20 64 69 73 6b 2e 20 54 68 65 20 6a 6f 75 72 o disk. The jour
14094 6e 61 6c 20 66 69 6c 65 20 73 74 69 6c 6c 20 65 nal file still e
14095 78 69 73 74 73 20 69 6e 20 74 68 65 20 66 69 6c xists in the fil
14096 65 2d 73 79 73 74 65 6d 20 0a 2a 2a 20 74 68 6f e-system .** tho
14097 75 67 68 2c 20 61 6e 64 20 69 66 20 61 20 66 61 ugh, and if a fa
14098 69 6c 75 72 65 20 6f 63 63 75 72 73 20 61 74 20 ilure occurs at
14099 74 68 69 73 20 70 6f 69 6e 74 20 69 74 20 77 69 this point it wi
1409a 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a ll eventually.**
1409b 20 62 65 20 75 73 65 64 20 61 73 20 61 20 68 6f be used as a ho
1409c 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 t-journal and th
1409d 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 e current transa
1409e 63 74 69 6f 6e 20 72 6f 6c 6c 65 64 20 62 61 63 ction rolled bac
1409f 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 k..**.** This fu
140a0 6e 63 74 69 6f 6e 20 66 69 6e 61 6c 69 7a 65 73 nction finalizes
140a1 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
140a2 65 2c 20 65 69 74 68 65 72 20 62 79 20 64 65 6c e, either by del
140a3 65 74 69 6e 67 2c 20 0a 2a 2a 20 74 72 75 6e 63 eting, .** trunc
140a4 61 74 69 6e 67 20 6f 72 20 70 61 72 74 69 61 6c ating or partial
140a5 6c 79 20 7a 65 72 6f 69 6e 67 20 69 74 2c 20 73 ly zeroing it, s
140a6 6f 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 o that it cannot
140a7 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 66 6f 72 be used .** for
140a8 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c hot-journal rol
140a9 6c 62 61 63 6b 2e 20 4f 6e 63 65 20 74 68 69 73 lback. Once this
140aa 20 69 73 20 64 6f 6e 65 20 74 68 65 20 74 72 61 is done the tra
140ab 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 nsaction is.** i
140ac 72 72 65 76 6f 63 61 62 6c 79 20 63 6f 6d 6d 69 rrevocably commi
140ad 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 tted..**.** If a
140ae 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
140af 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 an IO error code
140b0 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 is returned and
140b1 20 74 68 65 20 70 61 67 65 72 0a 2a 2a 20 6d 6f the pager.** mo
140b2 76 65 73 20 69 6e 74 6f 20 74 68 65 20 65 72 72 ves into the err
140b3 6f 72 20 73 74 61 74 65 2e 20 4f 74 68 65 72 77 or state. Otherw
140b4 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 ise, SQLITE_OK i
140b5 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 s returned..*/.S
140b6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
140b7 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f t sqlite3PagerCo
140b8 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50 61 67 mmitPhaseTwo(Pag
140b9 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 er *pPager){. i
140ba 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
140bb 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 K;
140bc 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
140bd 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 de */.. /* This
140be 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 routine should
140bf 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 69 66 not be called if
140c0 20 61 20 70 72 69 6f 72 20 65 72 72 6f 72 20 68 a prior error h
140c1 61 73 20 6f 63 63 75 72 72 65 64 2e 0a 20 20 2a as occurred.. *
140c2 2a 20 42 75 74 20 69 66 20 28 64 75 65 20 74 6f * But if (due to
140c3 20 61 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20 a coding error
140c4 65 6c 73 65 77 68 65 72 65 20 69 6e 20 74 68 65 elsewhere in the
140c5 20 73 79 73 74 65 6d 29 20 69 74 20 64 6f 65 73 system) it does
140c6 20 67 65 74 0a 20 20 2a 2a 20 63 61 6c 6c 65 64 get. ** called
140c7 2c 20 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 , just return th
140c8 65 20 73 61 6d 65 20 65 72 72 6f 72 20 63 6f 64 e same error cod
140c9 65 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 e without doing
140ca 61 6e 79 74 68 69 6e 67 2e 20 2a 2f 0a 20 20 69 anything. */. i
140cb 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d f( NEVER(pPager-
140cc 3e 65 72 72 43 6f 64 65 29 20 29 20 72 65 74 75 >errCode) ) retu
140cd 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f rn pPager->errCo
140ce 64 65 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 de;.. /* This f
140cf 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e unction should n
140d0 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20 ot be called if
140d1 74 68 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 the pager is not
140d2 20 69 6e 20 61 74 20 6c 65 61 73 74 0a 20 20 2a in at least. *
140d3 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 * PAGER_RESERVED
140d4 20 73 74 61 74 65 2e 20 41 6e 64 20 69 6e 64 65 state. And inde
140d5 65 64 20 53 51 4c 69 74 65 20 6e 65 76 65 72 20 ed SQLite never
140d6 64 6f 65 73 20 74 68 69 73 2e 20 42 75 74 20 69 does this. But i
140d7 74 20 69 73 0a 20 20 2a 2a 20 6e 69 63 65 20 74 t is. ** nice t
140d8 6f 20 68 61 76 65 20 74 68 69 73 20 64 65 66 65 o have this defe
140d9 6e 73 69 76 65 20 74 65 73 74 20 68 65 72 65 20 nsive test here
140da 61 6e 79 77 61 79 2e 0a 20 20 2a 2f 0a 20 20 69 anyway.. */. i
140db 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d f( NEVER(pPager-
140dc 3e 73 74 61 74 65 3c 50 41 47 45 52 5f 52 45 53 >state<PAGER_RES
140dd 45 52 56 45 44 29 20 29 20 72 65 74 75 72 6e 20 ERVED) ) return
140de 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 0a 20 SQLITE_ERROR;..
140df 20 2f 2a 20 41 6e 20 6f 70 74 69 6d 69 7a 61 74 /* An optimizat
140e0 69 6f 6e 2e 20 49 66 20 74 68 65 20 64 61 74 61 ion. If the data
140e1 62 61 73 65 20 77 61 73 20 6e 6f 74 20 61 63 74 base was not act
140e2 75 61 6c 6c 79 20 6d 6f 64 69 66 69 65 64 20 64 ually modified d
140e3 75 72 69 6e 67 0a 20 20 2a 2a 20 74 68 69 73 20 uring. ** this
140e4 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 transaction, the
140e5 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e pager is runnin
140e6 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 6d g in exclusive-m
140e7 6f 64 65 20 61 6e 64 20 69 73 0a 20 20 2a 2a 20 ode and is. **
140e8 75 73 69 6e 67 20 70 65 72 73 69 73 74 65 6e 74 using persistent
140e9 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 6e 20 journals, then
140ea 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
140eb 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 a no-op.. **.
140ec 20 2a 2a 20 54 68 65 20 73 74 61 72 74 20 6f 66 ** The start of
140ed 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
140ee 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 e currently cont
140ef 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 6a 6f ains a single jo
140f0 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 64 urnal . ** head
140f1 65 72 20 77 69 74 68 20 74 68 65 20 6e 52 65 63 er with the nRec
140f2 20 66 69 65 6c 64 20 73 65 74 20 74 6f 20 30 2e field set to 0.
140f3 20 49 66 20 73 75 63 68 20 61 20 6a 6f 75 72 6e If such a journ
140f4 61 6c 20 69 73 20 75 73 65 64 20 61 73 0a 20 20 al is used as.
140f5 2a 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c ** a hot-journal
140f6 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a 6f 75 72 during hot-jour
140f7 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 30 20 nal rollback, 0
140f8 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 62 65 20 changes will be
140f9 6d 61 64 65 0a 20 20 2a 2a 20 74 6f 20 74 68 65 made. ** to the
140fa 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
140fb 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e So there is no n
140fc 65 65 64 20 74 6f 20 7a 65 72 6f 20 74 68 65 20 eed to zero the
140fd 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 journal . ** he
140fe 61 64 65 72 2e 20 53 69 6e 63 65 20 74 68 65 20 ader. Since the
140ff 70 61 67 65 72 20 69 73 20 69 6e 20 65 78 63 6c pager is in excl
14100 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 72 usive mode, ther
14101 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 2a e is no need. *
14102 2a 20 74 6f 20 64 72 6f 70 20 61 6e 79 20 6c 6f * to drop any lo
14103 63 6b 73 20 65 69 74 68 65 72 2e 0a 20 20 2a 2f cks either.. */
14104 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 . if( pPager->d
14105 62 4d 6f 64 69 66 69 65 64 3d 3d 30 20 26 26 20 bModified==0 &&
14106 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
14107 65 4d 6f 64 65 20 0a 20 20 20 26 26 20 70 50 61 eMode . && pPa
14108 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
14109 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
1410a 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20 29 7b ODE_PERSIST. ){
1410b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
1410c 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d ger->journalOff=
1410d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 =JOURNAL_HDR_SZ(
1410e 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 72 pPager) );. r
1410f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
14110 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 . }.. PAGERTRA
14111 43 45 28 28 22 43 4f 4d 4d 49 54 20 25 64 5c 6e CE(("COMMIT %d\n
14112 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 ", PAGERID(pPage
14113 72 29 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r)));. assert(
14114 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 pPager->state==P
14115 41 47 45 52 5f 53 59 4e 43 45 44 20 7c 7c 20 4d AGER_SYNCED || M
14116 45 4d 44 42 20 7c 7c 20 21 70 50 61 67 65 72 2d EMDB || !pPager-
14117 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 3b 0a 20 >dbModified );.
14118 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f rc = pager_end_
14119 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 transaction(pPag
1411a 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d er, pPager->setM
1411b 61 73 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e aster);. return
1411c 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 pager_error(pPa
1411d 67 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a ger, rc);.}../*.
1411e 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 ** Rollback all
1411f 63 68 61 6e 67 65 73 2e 20 54 68 65 20 64 61 74 changes. The dat
14120 61 62 61 73 65 20 66 61 6c 6c 73 20 62 61 63 6b abase falls back
14121 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 to PAGER_SHARED
14122 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 mode..**.** Thi
14123 73 20 66 75 6e 63 74 69 6f 6e 20 70 65 72 66 6f s function perfo
14124 72 6d 73 20 74 77 6f 20 74 61 73 6b 73 3a 0a 2a rms two tasks:.*
14125 2a 0a 2a 2a 20 20 20 31 29 20 49 74 20 72 6f 6c *.** 1) It rol
14126 6c 73 20 62 61 63 6b 20 74 68 65 20 6a 6f 75 72 ls back the jour
14127 6e 61 6c 20 66 69 6c 65 2c 20 72 65 73 74 6f 72 nal file, restor
14128 69 6e 67 20 61 6c 6c 20 64 61 74 61 62 61 73 65 ing all database
14129 20 66 69 6c 65 20 61 6e 64 20 0a 2a 2a 20 20 20 file and .**
1412a 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 in-memory cac
1412b 68 65 20 70 61 67 65 73 20 74 6f 20 74 68 65 20 he pages to the
1412c 73 74 61 74 65 20 74 68 65 79 20 77 65 72 65 20 state they were
1412d 69 6e 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e in when the tran
1412e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 saction.**
1412f 77 61 73 20 6f 70 65 6e 65 64 2c 20 61 6e 64 0a was opened, and.
14130 2a 2a 20 20 20 32 29 20 49 74 20 66 69 6e 61 6c ** 2) It final
14131 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c izes the journal
14132 20 66 69 6c 65 2c 20 73 6f 20 74 68 61 74 20 69 file, so that i
14133 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 66 6f t is not used fo
14134 72 20 68 6f 74 0a 2a 2a 20 20 20 20 20 20 72 6f r hot.** ro
14135 6c 6c 62 61 63 6b 20 61 74 20 61 6e 79 20 70 6f llback at any po
14136 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 int in the futur
14137 65 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 65 63 74 e..**.** subject
14138 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e to the followin
14139 67 20 71 75 61 6c 69 66 69 63 61 74 69 6f 6e 73 g qualifications
1413a 3a 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 74 68 65 :.**.** * If the
1413b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
1413c 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e 20 77 68 not yet open wh
1413d 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
1413e 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 20 is called,.**
1413f 20 74 68 65 6e 20 6f 6e 6c 79 20 28 32 29 20 69 then only (2) i
14140 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 49 6e 20 s performed. In
14141 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 20 this case there
14142 69 73 20 6e 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 is no journal fi
14143 6c 65 0a 2a 2a 20 20 20 74 6f 20 72 6f 6c 6c 20 le.** to roll
14144 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 back..**.** * If
14145 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61 in an error sta
14146 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 te other than SQ
14147 4c 49 54 45 5f 46 55 4c 4c 2c 20 74 68 65 6e 20 LITE_FULL, then
14148 74 61 73 6b 20 28 31 29 20 69 73 20 0a 2a 2a 20 task (1) is .**
14149 20 20 70 65 72 66 6f 72 6d 65 64 2e 20 49 66 20 performed. If
1414a 73 75 63 63 65 73 73 66 75 6c 2c 20 74 61 73 6b successful, task
1414b 20 28 32 29 2e 20 52 65 67 61 72 64 6c 65 73 73 (2). Regardless
1414c 20 6f 66 20 74 68 65 20 6f 75 74 63 6f 6d 65 0a of the outcome.
1414d 2a 2a 20 20 20 6f 66 20 65 69 74 68 65 72 2c 20 ** of either,
1414e 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 the error state
1414f 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
14150 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 turned to the ca
14151 6c 6c 65 72 0a 2a 2a 20 20 20 28 69 2e 65 2e 20 ller.** (i.e.
14152 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 49 4f either SQLITE_IO
14153 45 52 52 20 6f 72 20 53 51 4c 49 54 45 5f 43 4f ERR or SQLITE_CO
14154 52 52 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20 2a 20 RRUPT)..**.** *
14155 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 If the pager is
14156 69 6e 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 in PAGER_RESERVE
14157 44 20 73 74 61 74 65 2c 20 74 68 65 6e 20 61 74 D state, then at
14158 74 65 6d 70 74 20 28 31 29 2e 20 57 68 65 74 68 tempt (1). Wheth
14159 65 72 0a 2a 2a 20 20 20 6f 72 20 6e 6f 74 20 28 er.** or not (
1415a 31 29 20 69 73 20 73 75 63 63 75 73 73 66 75 6c 1) is succussful
1415b 2c 20 61 6c 73 6f 20 61 74 74 65 6d 70 74 20 28 , also attempt (
1415c 32 29 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 2). If successfu
1415d 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 53 l, return.** S
1415e 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 QLITE_OK. Otherw
1415f 69 73 65 2c 20 65 6e 74 65 72 20 74 68 65 20 65 ise, enter the e
14160 72 72 6f 72 20 73 74 61 74 65 20 61 6e 64 20 72 rror state and r
14161 65 74 75 72 6e 20 74 68 65 20 66 69 72 73 74 20 eturn the first
14162 0a 2a 2a 20 20 20 65 72 72 6f 72 20 63 6f 64 65 .** error code
14163 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 20 0a 2a encountered. .*
14164 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69 73 20 63 *.** In this c
14165 61 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 ase there is no
14166 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 chance that the
14167 64 61 74 61 62 61 73 65 20 77 61 73 20 77 72 69 database was wri
14168 74 74 65 6e 20 74 6f 2e 20 0a 2a 2a 20 20 20 53 tten to. .** S
14169 6f 20 69 73 20 73 61 66 65 20 74 6f 20 66 69 6e o is safe to fin
1416a 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 alize the journa
1416b 6c 20 66 69 6c 65 20 65 76 65 6e 20 69 66 20 74 l file even if t
1416c 68 65 20 70 6c 61 79 62 61 63 6b 20 0a 2a 2a 20 he playback .**
1416d 20 20 28 6f 70 65 72 61 74 69 6f 6e 20 31 29 20 (operation 1)
1416e 66 61 69 6c 65 64 2e 20 48 6f 77 65 76 65 72 20 failed. However
1416f 74 68 65 20 70 61 67 65 72 20 6d 75 73 74 20 65 the pager must e
14170 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 nter the error s
14171 74 61 74 65 0a 2a 2a 20 20 20 61 73 20 74 68 65 tate.** as the
14172 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
14173 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 in-memory cache
14174 20 61 72 65 20 6e 6f 77 20 73 75 73 70 65 63 74 are now suspect
14175 2e 0a 2a 2a 0a 2a 2a 20 2a 20 46 69 6e 61 6c 6c ..**.** * Finall
14176 79 2c 20 69 66 20 69 6e 20 50 41 47 45 52 5f 45 y, if in PAGER_E
14177 58 43 4c 55 53 49 56 45 20 73 74 61 74 65 2c 20 XCLUSIVE state,
14178 74 68 65 6e 20 61 74 74 65 6d 70 74 20 28 31 29 then attempt (1)
14179 2e 20 4f 6e 6c 79 0a 2a 2a 20 20 20 61 74 74 65 . Only.** atte
1417a 6d 70 74 20 28 32 29 20 69 66 20 28 31 29 20 69 mpt (2) if (1) i
1417b 73 20 73 75 63 63 65 73 73 66 75 6c 2e 20 52 65 s successful. Re
1417c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 turn SQLITE_OK i
1417d 66 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a f successful,.**
1417e 20 20 20 6f 74 68 65 72 77 69 73 65 20 65 6e 74 otherwise ent
1417f 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 er the error sta
14180 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 te and return th
14181 65 20 65 72 72 6f 72 20 63 6f 64 65 20 66 72 6f e error code fro
14182 6d 20 74 68 65 20 0a 2a 2a 20 20 20 66 61 69 6c m the .** fail
14183 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a ing operation..*
14184 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69 73 20 63 *.** In this c
14185 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ase the database
14186 20 66 69 6c 65 20 6d 61 79 20 68 61 76 65 20 62 file may have b
14187 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 een written to.
14188 53 6f 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 70 So if the.** p
14189 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 74 69 6f layback operatio
1418a 6e 20 64 69 64 20 6e 6f 74 20 73 75 63 63 65 65 n did not succee
1418b 64 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74 20 62 d it would not b
1418c 65 20 73 61 66 65 20 74 6f 20 66 69 6e 61 6c 69 e safe to finali
1418d 7a 65 0a 2a 2a 20 20 20 74 68 65 20 6a 6f 75 72 ze.** the jour
1418e 6e 61 6c 20 66 69 6c 65 2e 20 49 74 20 6e 65 65 nal file. It nee
1418f 64 73 20 74 6f 20 62 65 20 6c 65 66 74 20 69 6e ds to be left in
14190 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d the file-system
14191 20 73 6f 20 74 68 61 74 0a 2a 2a 20 20 20 73 6f so that.** so
14192 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 me other process
14193 20 63 61 6e 20 75 73 65 20 69 74 20 74 6f 20 72 can use it to r
14194 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 estore the datab
14195 61 73 65 20 73 74 61 74 65 20 28 62 79 0a 2a 2a ase state (by.**
14196 20 20 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 hot-journal r
14197 6f 6c 6c 62 61 63 6b 29 2e 0a 2a 2f 0a 53 51 4c ollback)..*/.SQL
14198 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
14199 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c sqlite3PagerRoll
1419a 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 back(Pager *pPag
1419b 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 er){. int rc =
1419c 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
1419d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
1419e 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
1419f 50 41 47 45 52 54 52 41 43 45 28 28 22 52 4f 4c PAGERTRACE(("ROL
141a0 4c 42 41 43 4b 20 25 64 5c 6e 22 2c 20 50 41 47 LBACK %d\n", PAG
141a1 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a ERID(pPager)));.
141a2 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 64 if( !pPager->d
141a3 62 4d 6f 64 69 66 69 65 64 20 7c 7c 20 21 69 73 bModified || !is
141a4 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
141a5 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 ) ){. rc = pa
141a6 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 ger_end_transact
141a7 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67 ion(pPager, pPag
141a8 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a er->setMaster);.
141a9 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 }else if( pPag
141aa 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 26 20 70 er->errCode && p
141ab 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d Pager->errCode!=
141ac 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 SQLITE_FULL ){.
141ad 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 if( pPager->s
141ae 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c tate>=PAGER_EXCL
141af 55 53 49 56 45 20 29 7b 0a 20 20 20 20 20 20 70 USIVE ){. p
141b0 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 ager_playback(pP
141b1 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a ager, 0);. }.
141b2 20 20 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d rc = pPager-
141b3 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 >errCode;. }els
141b4 65 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 e{. if( pPage
141b5 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f r->state==PAGER_
141b6 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 RESERVED ){.
141b7 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20 int rc2;.
141b8 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 rc = pager_play
141b9 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b back(pPager, 0);
141ba 0a 20 20 20 20 20 20 72 63 32 20 3d 20 70 61 67 . rc2 = pag
141bb 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 er_end_transacti
141bc 6f 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 on(pPager, pPage
141bd 72 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 r->setMaster);.
141be 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
141bf 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
141c0 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 rc = rc2;.
141c1 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
141c2 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
141c3 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c playback(pPager,
141c4 20 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 0);. }..
141c5 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20 if( !MEMDB ){.
141c6 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 pPager->dbSi
141c7 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 zeValid = 0;.
141c8 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e }.. /* If an
141c9 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 error occurs du
141ca 72 69 6e 67 20 61 20 52 4f 4c 4c 42 41 43 4b 2c ring a ROLLBACK,
141cb 20 77 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 we can no longe
141cc 72 20 74 72 75 73 74 20 74 68 65 20 70 61 67 65 r trust the page
141cd 72 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 20 r. ** cache.
141ce 53 6f 20 63 61 6c 6c 20 70 61 67 65 72 5f 65 72 So call pager_er
141cf 72 6f 72 28 29 20 6f 6e 20 74 68 65 20 77 61 79 ror() on the way
141d0 20 6f 75 74 20 74 6f 20 6d 61 6b 65 20 61 6e 79 out to make any
141d1 20 65 72 72 6f 72 20 0a 20 20 20 20 2a 2a 20 70 error . ** p
141d2 65 72 73 69 73 74 65 6e 74 2e 0a 20 20 20 20 2a ersistent.. *
141d3 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 /. rc = pager
141d4 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 _error(pPager, r
141d5 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e c);. }. return
141d6 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
141d7 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 turn TRUE if the
141d8 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
141d9 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2d 6f 6e s opened read-on
141da 6c 79 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 ly. Return FALS
141db 45 0a 2a 2a 20 69 66 20 74 68 65 20 64 61 74 61 E.** if the data
141dc 62 61 73 65 20 69 73 20 28 69 6e 20 74 68 65 6f base is (in theo
141dd 72 79 29 20 77 72 69 74 61 62 6c 65 2e 0a 2a 2f ry) writable..*/
141de 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
141df 75 38 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 u8 sqlite3PagerI
141e0 73 72 65 61 64 6f 6e 6c 79 28 50 61 67 65 72 20 sreadonly(Pager
141e1 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 *pPager){. retu
141e2 72 6e 20 70 50 61 67 65 72 2d 3e 72 65 61 64 4f rn pPager->readO
141e3 6e 6c 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 nly;.}../*.** Re
141e4 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
141e5 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f of references to
141e6 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 the pager..*/.S
141e7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
141e8 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 t sqlite3PagerRe
141e9 66 63 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 fcount(Pager *pP
141ea 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
141eb 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 sqlite3PcacheRef
141ec 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 Count(pPager->pP
141ed 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a Cache);.}../*.**
141ee 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
141ef 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 er of references
141f0 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 65 to the specifie
141f1 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 d page..*/.SQLIT
141f2 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
141f3 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 lite3PagerPageRe
141f4 66 63 6f 75 6e 74 28 44 62 50 61 67 65 20 2a 70 fcount(DbPage *p
141f5 50 61 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 Page){. return
141f6 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 sqlite3PcachePag
141f7 65 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 29 eRefcount(pPage)
141f8 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ;.}..#ifdef SQLI
141f9 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 TE_TEST./*.** Th
141fa 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
141fb 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 ed for testing a
141fc 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 nd analysis only
141fd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
141fe 41 54 45 20 69 6e 74 20 2a 73 71 6c 69 74 65 33 ATE int *sqlite3
141ff 50 61 67 65 72 53 74 61 74 73 28 50 61 67 65 72 PagerStats(Pager
14200 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73 74 61 *pPager){. sta
14201 74 69 63 20 69 6e 74 20 61 5b 31 31 5d 3b 0a 20 tic int a[11];.
14202 20 61 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 50 a[0] = sqlite3P
14203 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 cacheRefCount(pP
14204 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a ager->pPCache);.
14205 20 20 61 5b 31 5d 20 3d 20 73 71 6c 69 74 65 33 a[1] = sqlite3
14206 50 63 61 63 68 65 50 61 67 65 63 6f 75 6e 74 28 PcachePagecount(
14207 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
14208 3b 0a 20 20 61 5b 32 5d 20 3d 20 73 71 6c 69 74 ;. a[2] = sqlit
14209 65 33 50 63 61 63 68 65 47 65 74 43 61 63 68 65 e3PcacheGetCache
1420a 73 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43 size(pPager->pPC
1420b 61 63 68 65 29 3b 0a 20 20 61 5b 33 5d 20 3d 20 ache);. a[3] =
1420c 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 pPager->dbSizeVa
1420d 6c 69 64 20 3f 20 28 69 6e 74 29 20 70 50 61 67 lid ? (int) pPag
1420e 65 72 2d 3e 64 62 53 69 7a 65 20 3a 20 2d 31 3b er->dbSize : -1;
1420f 0a 20 20 61 5b 34 5d 20 3d 20 70 50 61 67 65 72 . a[4] = pPager
14210 2d 3e 73 74 61 74 65 3b 0a 20 20 61 5b 35 5d 20 ->state;. a[5]
14211 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 = pPager->errCod
14212 65 3b 0a 20 20 61 5b 36 5d 20 3d 20 70 50 61 67 e;. a[6] = pPag
14213 65 72 2d 3e 6e 48 69 74 3b 0a 20 20 61 5b 37 5d er->nHit;. a[7]
14214 20 3d 20 70 50 61 67 65 72 2d 3e 6e 4d 69 73 73 = pPager->nMiss
14215 3b 0a 20 20 61 5b 38 5d 20 3d 20 30 3b 20 20 2f ;. a[8] = 0; /
14216 2a 20 55 73 65 64 20 74 6f 20 62 65 20 70 50 61 * Used to be pPa
14217 67 65 72 2d 3e 6e 4f 76 66 6c 20 2a 2f 0a 20 20 ger->nOvfl */.
14218 61 5b 39 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e a[9] = pPager->n
14219 52 65 61 64 3b 0a 20 20 61 5b 31 30 5d 20 3d 20 Read;. a[10] =
1421a 70 50 61 67 65 72 2d 3e 6e 57 72 69 74 65 3b 0a pPager->nWrite;.
1421b 20 20 72 65 74 75 72 6e 20 61 3b 0a 7d 0a 23 65 return a;.}.#e
1421c 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ndif../*.** Retu
1421d 72 6e 20 74 72 75 65 20 69 66 20 74 68 69 73 20 rn true if this
1421e 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 is an in-memory
1421f 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 pager..*/.SQLITE
14220 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
14221 69 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 ite3PagerIsMemdb
14222 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
14223 0a 20 20 72 65 74 75 72 6e 20 4d 45 4d 44 42 3b . return MEMDB;
14224 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 .}../*.** Check
14225 74 68 61 74 20 74 68 65 72 65 20 61 72 65 20 61 that there are a
14226 74 20 6c 65 61 73 74 20 6e 53 61 76 65 70 6f 69 t least nSavepoi
14227 6e 74 20 73 61 76 65 70 6f 69 6e 74 73 20 6f 70 nt savepoints op
14228 65 6e 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 en. If there are
14229 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 6c 65 .** currently le
1422a 73 73 20 74 68 61 6e 20 6e 53 61 76 65 70 6f 69 ss than nSavepoi
1422b 6e 74 73 20 6f 70 65 6e 2c 20 74 68 65 6e 20 6f nts open, then o
1422c 70 65 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 pen one or more
1422d 73 61 76 65 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f savepoints.** to
1422e 20 6d 61 6b 65 20 75 70 20 74 68 65 20 64 69 66 make up the dif
1422f 66 65 72 65 6e 63 65 2e 20 49 66 20 74 68 65 20 ference. If the
14230 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f number of savepo
14231 69 6e 74 73 20 69 73 20 61 6c 72 65 61 64 79 0a ints is already.
14232 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6e 53 61 76 ** equal to nSav
14233 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68 69 epoint, then thi
14234 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 s function is a
14235 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 no-op..**.** If
14236 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
14237 69 6f 6e 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 ion fails, SQLIT
14238 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 E_NOMEM is retur
14239 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 ned. If an error
1423a 20 0a 2a 2a 20 6f 63 63 75 72 73 20 77 68 69 6c .** occurs whil
1423b 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 73 75 e opening the su
1423c 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 b-journal file,
1423d 74 68 65 6e 20 61 6e 20 49 4f 20 65 72 72 6f 72 then an IO error
1423e 20 63 6f 64 65 20 69 73 0a 2a 2a 20 72 65 74 75 code is.** retu
1423f 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c rned. Otherwise,
14240 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 SQLITE_OK..*/.S
14241 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
14242 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 t sqlite3PagerOp
14243 65 6e 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 enSavepoint(Page
14244 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6e r *pPager, int n
14245 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 6e Savepoint){. in
14246 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
14247 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
14248 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
14249 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 n code */. int
1424a 6e 43 75 72 72 65 6e 74 20 3d 20 70 50 61 67 65 nCurrent = pPage
1424b 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 r->nSavepoint;
1424c 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 /* Current
1424d 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 number of savep
1424e 6f 69 6e 74 73 20 2a 2f 0a 0a 20 20 69 66 28 20 oints */.. if(
1424f 6e 53 61 76 65 70 6f 69 6e 74 3e 6e 43 75 72 72 nSavepoint>nCurr
14250 65 6e 74 20 26 26 20 70 50 61 67 65 72 2d 3e 75 ent && pPager->u
14251 73 65 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 seJournal ){.
14252 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 int ii;
14253 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14254 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 /* Iter
14255 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f ator variable */
14256 0a 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f . PagerSavepo
14257 69 6e 74 20 2a 61 4e 65 77 3b 20 20 20 20 20 20 int *aNew;
14258 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
14259 4e 65 77 20 50 61 67 65 72 2e 61 53 61 76 65 70 New Pager.aSavep
1425a 6f 69 6e 74 20 61 72 72 61 79 20 2a 2f 0a 0a 20 oint array */..
1425b 20 20 20 2f 2a 20 45 69 74 68 65 72 20 74 68 65 /* Either the
1425c 72 65 20 69 73 20 6e 6f 20 61 63 74 69 76 65 20 re is no active
1425d 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 73 journal or the s
1425e 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 ub-journal is op
1425f 65 6e 20 6f 72 20 0a 20 20 20 20 2a 2a 20 74 68 en or . ** th
14260 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c 77 e journal is alw
14261 61 79 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 ays stored in me
14262 6d 6f 72 79 20 2a 2f 0a 20 20 20 20 61 73 73 65 mory */. asse
14263 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 rt( pPager->nSav
14264 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 69 73 4f epoint==0 || isO
14265 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 pen(pPager->sjfd
14266 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 ) ||.
14267 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
14268 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
14269 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 NALMODE_MEMORY )
1426a 3b 0a 0a 20 20 20 20 2f 2a 20 47 72 6f 77 20 74 ;.. /* Grow t
1426b 68 65 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f he Pager.aSavepo
1426c 69 6e 74 20 61 72 72 61 79 20 75 73 69 6e 67 20 int array using
1426d 72 65 61 6c 6c 6f 63 28 29 2e 20 52 65 74 75 72 realloc(). Retur
1426e 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 n SQLITE_NOMEM.
1426f 20 20 20 2a 2a 20 69 66 20 74 68 65 20 61 6c 6c ** if the all
14270 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2e 20 4f ocation fails. O
14271 74 68 65 72 77 69 73 65 2c 20 7a 65 72 6f 20 74 therwise, zero t
14272 68 65 20 6e 65 77 20 70 6f 72 74 69 6f 6e 20 69 he new portion i
14273 6e 20 63 61 73 65 20 61 20 0a 20 20 20 20 2a 2a n case a . **
14274 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 malloc failure
14275 6f 63 63 75 72 73 20 77 68 69 6c 65 20 70 6f 70 occurs while pop
14276 75 6c 61 74 69 6e 67 20 69 74 20 69 6e 20 74 68 ulating it in th
14277 65 20 66 6f 72 28 2e 2e 2e 29 20 6c 6f 6f 70 20 e for(...) loop
14278 62 65 6c 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 below.. */.
14279 20 20 61 4e 65 77 20 3d 20 28 50 61 67 65 72 53 aNew = (PagerS
1427a 61 76 65 70 6f 69 6e 74 20 2a 29 73 71 6c 69 74 avepoint *)sqlit
1427b 65 33 52 65 61 6c 6c 6f 63 28 0a 20 20 20 20 20 e3Realloc(.
1427c 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 pPager->aSave
1427d 70 6f 69 6e 74 2c 20 73 69 7a 65 6f 66 28 50 61 point, sizeof(Pa
1427e 67 65 72 53 61 76 65 70 6f 69 6e 74 29 2a 6e 53 gerSavepoint)*nS
1427f 61 76 65 70 6f 69 6e 74 0a 20 20 20 20 29 3b 0a avepoint. );.
14280 20 20 20 20 69 66 28 20 21 61 4e 65 77 20 29 7b if( !aNew ){
14281 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
14282 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
14283 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 61 4e }. memset(&aN
14284 65 77 5b 6e 43 75 72 72 65 6e 74 5d 2c 20 30 2c ew[nCurrent], 0,
14285 20 28 6e 53 61 76 65 70 6f 69 6e 74 2d 6e 43 75 (nSavepoint-nCu
14286 72 72 65 6e 74 29 20 2a 20 73 69 7a 65 6f 66 28 rrent) * sizeof(
14287 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 29 29 PagerSavepoint))
14288 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 ;. pPager->aS
14289 61 76 65 70 6f 69 6e 74 20 3d 20 61 4e 65 77 3b avepoint = aNew;
1428a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 61 . pPager->nSa
1428b 76 65 70 6f 69 6e 74 20 3d 20 6e 53 61 76 65 70 vepoint = nSavep
1428c 6f 69 6e 74 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f oint;.. /* Po
1428d 70 75 6c 61 74 65 20 74 68 65 20 50 61 67 65 72 pulate the Pager
1428e 53 61 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74 Savepoint struct
1428f 75 72 65 73 20 6a 75 73 74 20 61 6c 6c 6f 63 61 ures just alloca
14290 74 65 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 ted. */. for(
14291 69 69 3d 6e 43 75 72 72 65 6e 74 3b 20 69 69 3c ii=nCurrent; ii<
14292 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b nSavepoint; ii++
14293 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
14294 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 pPager->dbSizeV
14295 61 6c 69 64 20 29 3b 0a 20 20 20 20 20 20 61 4e alid );. aN
14296 65 77 5b 69 69 5d 2e 6e 4f 72 69 67 20 3d 20 70 ew[ii].nOrig = p
14297 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 Pager->dbSize;.
14298 20 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 if( isOpen(
14299 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 pPager->jfd) &&
1429a 41 4c 57 41 59 53 28 70 50 61 67 65 72 2d 3e 6a ALWAYS(pPager->j
1429b 6f 75 72 6e 61 6c 4f 66 66 3e 30 29 20 29 7b 0a ournalOff>0) ){.
1429c 20 20 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d aNew[ii]
1429d 2e 69 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 .iOffset = pPage
1429e 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 r->journalOff;.
1429f 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
142a0 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 aNew[ii].iOf
142a1 66 73 65 74 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 fset = JOURNAL_H
142a2 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 DR_SZ(pPager);.
142a3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 4e 65 }. aNe
142a4 77 5b 69 69 5d 2e 69 53 75 62 52 65 63 20 3d 20 w[ii].iSubRec =
142a5 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 3b pPager->nSubRec;
142a6 0a 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e . aNew[ii].
142a7 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 73 pInSavepoint = s
142a8 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 qlite3BitvecCrea
142a9 74 65 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a te(pPager->dbSiz
142aa 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 61 e);. if( !a
142ab 4e 65 77 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 New[ii].pInSavep
142ac 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 oint ){.
142ad 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
142ae 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 MEM;. }.
142af 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 }.. /* Open
142b0 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2c the sub-journal,
142b1 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c if it is not al
142b2 72 65 61 64 79 20 6f 70 65 6e 65 64 2e 20 2a 2f ready opened. */
142b3 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e 53 75 . rc = openSu
142b4 62 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 bJournal(pPager)
142b5 3b 0a 20 20 20 20 61 73 73 65 72 74 54 72 75 6e ;. assertTrun
142b6 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 cateConstraint(p
142b7 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 72 Pager);. }.. r
142b8 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
142b9 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
142ba 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 6f is called to ro
142bb 6c 6c 62 61 63 6b 20 6f 72 20 72 65 6c 65 61 73 llback or releas
142bc 65 20 28 63 6f 6d 6d 69 74 29 20 61 20 73 61 76 e (commit) a sav
142bd 65 70 6f 69 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 epoint..** The s
142be 61 76 65 70 6f 69 6e 74 20 74 6f 20 72 65 6c 65 avepoint to rele
142bf 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 ase or rollback
142c0 6e 65 65 64 20 6e 6f 74 20 62 65 20 74 68 65 20 need not be the
142c1 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 0a 2a most recently .*
142c2 2a 20 63 72 65 61 74 65 64 20 73 61 76 65 70 6f * created savepo
142c3 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d int..**.** Param
142c4 65 74 65 72 20 6f 70 20 69 73 20 61 6c 77 61 79 eter op is alway
142c5 73 20 65 69 74 68 65 72 20 53 41 56 45 50 4f 49 s either SAVEPOI
142c6 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 6f 72 20 53 NT_ROLLBACK or S
142c7 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 AVEPOINT_RELEASE
142c8 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 53 41 ..** If it is SA
142c9 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c VEPOINT_RELEASE,
142ca 20 74 68 65 6e 20 72 65 6c 65 61 73 65 20 61 6e then release an
142cb 64 20 64 65 73 74 72 6f 79 20 74 68 65 20 73 61 d destroy the sa
142cc 76 65 70 6f 69 6e 74 20 77 69 74 68 0a 2a 2a 20 vepoint with.**
142cd 69 6e 64 65 78 20 69 53 61 76 65 70 6f 69 6e 74 index iSavepoint
142ce 2e 20 49 66 20 69 74 20 69 73 20 53 41 56 45 50 . If it is SAVEP
142cf 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 OINT_ROLLBACK, t
142d0 68 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 61 6c 6c hen rollback all
142d1 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74 68 61 74 changes.** that
142d2 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 20 73 have occurred s
142d3 69 6e 63 65 20 74 68 65 20 73 70 65 63 69 66 69 ince the specifi
142d4 65 64 20 73 61 76 65 70 6f 69 6e 74 20 77 61 73 ed savepoint was
142d5 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 created..**.**
142d6 54 68 65 20 73 61 76 65 70 6f 69 6e 74 20 74 6f The savepoint to
142d7 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 72 65 6c rollback or rel
142d8 65 61 73 65 20 69 73 20 69 64 65 6e 74 69 66 69 ease is identifi
142d9 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 ed by parameter
142da 0a 2a 2a 20 69 53 61 76 65 70 6f 69 6e 74 2e 20 .** iSavepoint.
142db 41 20 76 61 6c 75 65 20 6f 66 20 30 20 6d 65 61 A value of 0 mea
142dc 6e 73 20 74 6f 20 6f 70 65 72 61 74 65 20 6f 6e ns to operate on
142dd 20 74 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 the outermost s
142de 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 28 74 68 65 avepoint.** (the
142df 20 66 69 72 73 74 20 63 72 65 61 74 65 64 29 2e first created).
142e0 20 41 20 76 61 6c 75 65 20 6f 66 20 28 50 61 67 A value of (Pag
142e1 65 72 2e 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 er.nSavepoint-1)
142e2 20 6d 65 61 6e 73 20 6f 70 65 72 61 74 65 0a 2a means operate.*
142e3 2a 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 * on the most re
142e4 63 65 6e 74 6c 79 20 63 72 65 61 74 65 64 20 73 cently created s
142e5 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 69 53 61 avepoint. If iSa
142e6 76 65 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74 vepoint is great
142e7 65 72 20 74 68 61 6e 0a 2a 2a 20 28 50 61 67 65 er than.** (Page
142e8 72 2e 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 2c r.nSavepoint-1),
142e9 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 then this funct
142ea 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ion is a no-op..
142eb 2a 2a 0a 2a 2a 20 49 66 20 61 20 6e 65 67 61 74 **.** If a negat
142ec 69 76 65 20 76 61 6c 75 65 20 69 73 20 70 61 73 ive value is pas
142ed 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 sed to this func
142ee 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 63 tion, then the c
142ef 75 72 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 urrent.** transa
142f0 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 ction is rolled
142f1 62 61 63 6b 2e 20 54 68 69 73 20 69 73 20 64 69 back. This is di
142f2 66 66 65 72 65 6e 74 20 74 6f 20 63 61 6c 6c 69 fferent to calli
142f3 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 ng .** sqlite3Pa
142f4 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 20 62 65 gerRollback() be
142f5 63 61 75 73 65 20 74 68 69 73 20 66 75 6e 63 74 cause this funct
142f6 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 74 65 72 ion does not ter
142f7 6d 69 6e 61 74 65 0a 2a 2a 20 74 68 65 20 74 72 minate.** the tr
142f8 61 6e 73 61 63 74 69 6f 6e 20 6f 72 20 75 6e 6c ansaction or unl
142f9 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 ock the database
142fa 2c 20 69 74 20 6a 75 73 74 20 72 65 73 74 6f 72 , it just restor
142fb 65 73 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 es the .** conte
142fc 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 nts of the datab
142fd 61 73 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69 ase to its origi
142fe 6e 61 6c 20 73 74 61 74 65 2e 20 0a 2a 2a 0a 2a nal state. .**.*
142ff 2a 20 49 6e 20 61 6e 79 20 63 61 73 65 2c 20 61 * In any case, a
14300 6c 6c 20 73 61 76 65 70 6f 69 6e 74 73 20 77 69 ll savepoints wi
14301 74 68 20 61 6e 20 69 6e 64 65 78 20 67 72 65 61 th an index grea
14302 74 65 72 20 74 68 61 6e 20 69 53 61 76 65 70 6f ter than iSavepo
14303 69 6e 74 20 0a 2a 2a 20 61 72 65 20 64 65 73 74 int .** are dest
14304 72 6f 79 65 64 2e 20 49 66 20 74 68 69 73 20 69 royed. If this i
14305 73 20 61 20 72 65 6c 65 61 73 65 20 6f 70 65 72 s a release oper
14306 61 74 69 6f 6e 20 28 6f 70 3d 3d 53 41 56 45 50 ation (op==SAVEP
14307 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 2c 0a 2a OINT_RELEASE),.*
14308 2a 20 74 68 65 6e 20 73 61 76 65 70 6f 69 6e 74 * then savepoint
14309 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 20 61 iSavepoint is a
1430a 6c 73 6f 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a lso destroyed..*
1430b 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
1430c 6f 6e 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 on may return SQ
1430d 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 LITE_NOMEM if a
1430e 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
1430f 6e 20 66 61 69 6c 73 2c 0a 2a 2a 20 6f 72 20 61 n fails,.** or a
14310 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 n IO error code
14311 69 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f if an IO error o
14312 63 63 75 72 73 20 77 68 69 6c 65 20 72 6f 6c 6c ccurs while roll
14313 69 6e 67 20 62 61 63 6b 20 61 20 0a 2a 2a 20 73 ing back a .** s
14314 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 6e 6f 20 avepoint. If no
14315 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 53 51 errors occur, SQ
14316 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
14317 6e 65 64 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f ned..*/ .SQLITE_
14318 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
14319 74 65 33 50 61 67 65 72 53 61 76 65 70 6f 69 6e te3PagerSavepoin
1431a 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c t(Pager *pPager,
1431b 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 61 int op, int iSa
1431c 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 vepoint){. int
1431d 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1431e 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 . assert( op==S
1431f 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 AVEPOINT_RELEASE
14320 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e || op==SAVEPOIN
14321 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 T_ROLLBACK );.
14322 61 73 73 65 72 74 28 20 69 53 61 76 65 70 6f 69 assert( iSavepoi
14323 6e 74 3e 3d 30 20 7c 7c 20 6f 70 3d 3d 53 41 56 nt>=0 || op==SAV
14324 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 EPOINT_ROLLBACK
14325 29 3b 0a 0a 20 20 69 66 28 20 69 53 61 76 65 70 );.. if( iSavep
14326 6f 69 6e 74 3c 70 50 61 67 65 72 2d 3e 6e 53 61 oint<pPager->nSa
14327 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69 vepoint ){. i
14328 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 nt ii;
14329 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 /* Iterator va
1432a 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e riable */. in
1432b 74 20 6e 4e 65 77 3b 20 20 20 20 20 20 20 20 20 t nNew;
1432c 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 /* Number of re
1432d 6d 61 69 6e 69 6e 67 20 73 61 76 65 70 6f 69 6e maining savepoin
1432e 74 73 20 61 66 74 65 72 20 74 68 69 73 20 6f 70 ts after this op
1432f 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 69 67 . */.. /* Fig
14330 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 ure out how many
14331 20 73 61 76 65 70 6f 69 6e 74 73 20 77 69 6c 6c savepoints will
14332 20 73 74 69 6c 6c 20 62 65 20 61 63 74 69 76 65 still be active
14333 20 61 66 74 65 72 20 74 68 69 73 0a 20 20 20 20 after this.
14334 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 20 53 74 ** operation. St
14335 6f 72 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 ore this value i
14336 6e 20 6e 4e 65 77 2e 20 54 68 65 6e 20 66 72 65 n nNew. Then fre
14337 65 20 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f e resources asso
14338 63 69 61 74 65 64 20 0a 20 20 20 20 2a 2a 20 77 ciated . ** w
14339 69 74 68 20 61 6e 79 20 73 61 76 65 70 6f 69 6e ith any savepoin
1433a 74 73 20 74 68 61 74 20 61 72 65 20 64 65 73 74 ts that are dest
1433b 72 6f 79 65 64 20 62 79 20 74 68 69 73 20 6f 70 royed by this op
1433c 65 72 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a eration.. */.
1433d 20 20 20 20 6e 4e 65 77 20 3d 20 69 53 61 76 65 nNew = iSave
1433e 70 6f 69 6e 74 20 2b 20 28 6f 70 3d 3d 53 41 56 point + (op==SAV
1433f 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 EPOINT_ROLLBACK)
14340 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 6e 4e 65 ;. for(ii=nNe
14341 77 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 w; ii<pPager->nS
14342 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b avepoint; ii++){
14343 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 . sqlite3Bi
14344 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 tvecDestroy(pPag
14345 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 er->aSavepoint[i
14346 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 29 i].pInSavepoint)
14347 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 ;. }. pPag
14348 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d er->nSavepoint =
14349 20 6e 4e 65 77 3b 0a 0a 20 20 20 20 2f 2a 20 49 nNew;.. /* I
1434a 66 20 74 68 69 73 20 69 73 20 61 20 72 6f 6c 6c f this is a roll
1434b 62 61 63 6b 20 6f 70 65 72 61 74 69 6f 6e 2c 20 back operation,
1434c 70 6c 61 79 62 61 63 6b 20 74 68 65 20 73 70 65 playback the spe
1434d 63 69 66 69 65 64 20 73 61 76 65 70 6f 69 6e 74 cified savepoint
1434e 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 .. ** If this
1434f 20 69 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 2c is a temp-file,
14350 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 it is possible
14351 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c that the journal
14352 20 66 69 6c 65 20 68 61 73 0a 20 20 20 20 2a 2a file has. **
14353 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 not yet been op
14354 65 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 ened. In this ca
14355 73 65 20 74 68 65 72 65 20 68 61 76 65 20 62 65 se there have be
14356 65 6e 20 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f en no changes to
14357 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 . ** the data
14358 62 61 73 65 20 66 69 6c 65 2c 20 73 6f 20 74 68 base file, so th
14359 65 20 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 e playback opera
1435a 74 69 6f 6e 20 63 61 6e 20 62 65 20 73 6b 69 70 tion can be skip
1435b 70 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ped.. */.
1435c 69 66 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e if( op==SAVEPOIN
1435d 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20 69 73 T_ROLLBACK && is
1435e 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
1435f 29 20 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72 ) ){. Pager
14360 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 Savepoint *pSave
14361 70 6f 69 6e 74 20 3d 20 28 6e 4e 65 77 3d 3d 30 point = (nNew==0
14362 29 3f 30 3a 26 70 50 61 67 65 72 2d 3e 61 53 61 )?0:&pPager->aSa
14363 76 65 70 6f 69 6e 74 5b 6e 4e 65 77 2d 31 5d 3b vepoint[nNew-1];
14364 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
14365 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 rPlaybackSavepoi
14366 6e 74 28 70 50 61 67 65 72 2c 20 70 53 61 76 65 nt(pPager, pSave
14367 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 61 73 point);. as
14368 73 65 72 74 28 72 63 21 3d 53 51 4c 49 54 45 5f sert(rc!=SQLITE_
14369 44 4f 4e 45 29 3b 0a 20 20 20 20 7d 0a 20 20 0a DONE);. }. .
1436a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 /* If this i
1436b 73 20 61 20 72 65 6c 65 61 73 65 20 6f 66 20 74 s a release of t
1436c 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 he outermost sav
1436d 65 70 6f 69 6e 74 2c 20 74 72 75 6e 63 61 74 65 epoint, truncate
1436e 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 75 62 . ** the sub
1436f 2d 6a 6f 75 72 6e 61 6c 20 74 6f 20 7a 65 72 6f -journal to zero
14370 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 bytes in size.
14371 2a 2f 0a 20 20 20 20 69 66 28 20 6e 4e 65 77 3d */. if( nNew=
14372 3d 30 20 26 26 20 6f 70 3d 3d 53 41 56 45 50 4f =0 && op==SAVEPO
14373 49 4e 54 5f 52 45 4c 45 41 53 45 20 26 26 20 69 INT_RELEASE && i
14374 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a sOpen(pPager->sj
14375 66 64 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 fd) ){. ass
14376 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
14377 4f 4b 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d OK );. rc =
14378 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 sqlite3OsTrunca
14379 74 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c te(pPager->sjfd,
1437a 20 30 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 0);. pPage
1437b 72 2d 3e 6e 53 75 62 52 65 63 20 3d 20 30 3b 0a r->nSubRec = 0;.
1437c 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1437d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1437e 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 Return the full
1437f 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 pathname of the
14380 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
14381 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
14382 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
14383 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d ite3PagerFilenam
14384 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 e(Pager *pPager)
14385 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 {. return pPage
14386 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a r->zFilename;.}.
14387 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
14388 65 20 56 46 53 20 73 74 72 75 63 74 75 72 65 20 e VFS structure
14389 66 6f 72 20 74 68 65 20 70 61 67 65 72 2e 0a 2a for the pager..*
1438a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1438b 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 const sqlite3_v
1438c 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 fs *sqlite3Pager
1438d 56 66 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 Vfs(Pager *pPage
1438e 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 r){. return pPa
1438f 67 65 72 2d 3e 70 56 66 73 3b 0a 7d 0a 0a 2f 2a ger->pVfs;.}../*
14390 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 .** Return the f
14391 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 ile handle for t
14392 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
14393 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 associated.** w
14394 69 74 68 20 74 68 65 20 70 61 67 65 72 2e 20 20 ith the pager.
14395 54 68 69 73 20 6d 69 67 68 74 20 72 65 74 75 72 This might retur
14396 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 66 69 n NULL if the fi
14397 6c 65 20 68 61 73 0a 2a 2a 20 6e 6f 74 20 79 65 le has.** not ye
14398 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 2a t been opened..*
14399 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1439a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 sqlite3_file *s
1439b 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 qlite3PagerFile(
1439c 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
1439d 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
1439e 3e 66 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 >fd;.}../*.** Re
1439f 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 turn the full pa
143a0 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f thname of the jo
143a1 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 urnal file..*/.S
143a2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
143a3 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
143a4 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 3PagerJournalnam
143a5 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 e(Pager *pPager)
143a6 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 {. return pPage
143a7 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a 7d 0a 0a r->zJournal;.}..
143a8 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 /*.** Return tru
143a9 65 20 69 66 20 66 73 79 6e 63 28 29 20 63 61 6c e if fsync() cal
143aa 6c 73 20 61 72 65 20 64 69 73 61 62 6c 65 64 20 ls are disabled
143ab 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 for this pager.
143ac 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a Return FALSE.**
143ad 20 69 66 20 66 73 79 6e 63 28 29 73 20 61 72 65 if fsync()s are
143ae 20 65 78 65 63 75 74 65 64 20 6e 6f 72 6d 61 6c executed normal
143af 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ly..*/.SQLITE_PR
143b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
143b1 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 50 61 67 3PagerNosync(Pag
143b2 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 er *pPager){. r
143b3 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 6e 6f eturn pPager->no
143b4 53 79 6e 63 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 Sync;.}..#ifdef
143b5 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 SQLITE_HAS_CODEC
143b6 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 20 72 65 ./*.** Set or re
143b7 74 72 69 65 76 65 20 74 68 65 20 63 6f 64 65 63 trieve the codec
143b8 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 0a for this pager.
143b9 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 */.static void s
143ba 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 6f qlite3PagerSetCo
143bb 64 65 63 28 0a 20 20 50 61 67 65 72 20 2a 70 50 dec(. Pager *pP
143bc 61 67 65 72 2c 0a 20 20 76 6f 69 64 20 2a 28 2a ager,. void *(*
143bd 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f xCodec)(void*,vo
143be 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c 0a 20 id*,Pgno,int),.
143bf 20 76 6f 69 64 20 28 2a 78 43 6f 64 65 63 53 69 void (*xCodecSi
143c0 7a 65 43 68 6e 67 29 28 76 6f 69 64 2a 2c 69 6e zeChng)(void*,in
143c1 74 2c 69 6e 74 29 2c 0a 20 20 76 6f 69 64 20 28 t,int),. void (
143c2 2a 78 43 6f 64 65 63 46 72 65 65 29 28 76 6f 69 *xCodecFree)(voi
143c3 64 2a 29 2c 0a 20 20 76 6f 69 64 20 2a 70 43 6f d*),. void *pCo
143c4 64 65 63 0a 29 7b 0a 20 20 69 66 28 20 70 50 61 dec.){. if( pPa
143c5 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 20 ger->xCodecFree
143c6 29 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 ) pPager->xCodec
143c7 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70 43 6f Free(pPager->pCo
143c8 64 65 63 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e dec);. pPager->
143c9 78 43 6f 64 65 63 20 3d 20 78 43 6f 64 65 63 3b xCodec = xCodec;
143ca 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 . pPager->xCode
143cb 63 53 69 7a 65 43 68 6e 67 20 3d 20 78 43 6f 64 cSizeChng = xCod
143cc 65 63 53 69 7a 65 43 68 6e 67 3b 0a 20 20 70 50 ecSizeChng;. pP
143cd 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 ager->xCodecFree
143ce 20 3d 20 78 43 6f 64 65 63 46 72 65 65 3b 0a 20 = xCodecFree;.
143cf 20 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 20 pPager->pCodec
143d0 3d 20 70 43 6f 64 65 63 3b 0a 20 20 70 61 67 65 = pCodec;. page
143d1 72 52 65 70 6f 72 74 53 69 7a 65 28 70 50 61 67 rReportSize(pPag
143d2 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f er);.}.static vo
143d3 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 id *sqlite3Pager
143d4 47 65 74 43 6f 64 65 63 28 50 61 67 65 72 20 2a GetCodec(Pager *
143d5 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 pPager){. retur
143d6 6e 20 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 n pPager->pCodec
143d7 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e ;.}.#endif..#ifn
143d8 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
143d9 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a AUTOVACUUM./*.**
143da 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20 70 Move the page p
143db 50 67 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 70 Pg to location p
143dc 67 6e 6f 20 69 6e 20 74 68 65 20 66 69 6c 65 2e gno in the file.
143dd 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 .**.** There mus
143de 74 20 62 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 t be no referenc
143df 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 70 es to the page p
143e0 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 reviously locate
143e1 64 20 61 74 0a 2a 2a 20 70 67 6e 6f 20 28 77 68 d at.** pgno (wh
143e2 69 63 68 20 77 65 20 63 61 6c 6c 20 70 50 67 4f ich we call pPgO
143e3 6c 64 29 20 74 68 6f 75 67 68 20 74 68 61 74 20 ld) though that
143e4 70 61 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 20 page is allowed
143e5 74 6f 20 62 65 0a 2a 2a 20 69 6e 20 63 61 63 68 to be.** in cach
143e6 65 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20 e. If the page
143e7 70 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 previously locat
143e8 65 64 20 61 74 20 70 67 6e 6f 20 69 73 20 6e 6f ed at pgno is no
143e9 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 t already.** in
143ea 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 the rollback jou
143eb 72 6e 61 6c 2c 20 69 74 20 69 73 20 6e 6f 74 20 rnal, it is not
143ec 70 75 74 20 74 68 65 72 65 20 62 79 20 62 79 20 put there by by
143ed 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a this routine..**
143ee 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 73 20 74 .** References t
143ef 6f 20 74 68 65 20 70 61 67 65 20 70 50 67 20 72 o the page pPg r
143f0 65 6d 61 69 6e 20 76 61 6c 69 64 2e 20 55 70 64 emain valid. Upd
143f1 61 74 69 6e 67 20 61 6e 79 0a 2a 2a 20 6d 65 74 ating any.** met
143f2 61 2d 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 a-data associate
143f3 64 20 77 69 74 68 20 70 50 67 20 28 69 2e 65 2e d with pPg (i.e.
143f4 20 64 61 74 61 20 73 74 6f 72 65 64 20 69 6e 20 data stored in
143f5 74 68 65 20 6e 45 78 74 72 61 20 62 79 74 65 73 the nExtra bytes
143f6 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c .** allocated al
143f7 6f 6e 67 20 77 69 74 68 20 74 68 65 20 70 61 67 ong with the pag
143f8 65 29 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e e) is the respon
143f9 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 sibility of the
143fa 63 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 caller..**.** A
143fb 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 transaction must
143fc 20 62 65 20 61 63 74 69 76 65 20 77 68 65 6e 20 be active when
143fd 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
143fe 63 61 6c 6c 65 64 2e 20 49 74 20 75 73 65 64 20 called. It used
143ff 74 6f 20 62 65 0a 2a 2a 20 72 65 71 75 69 72 65 to be.** require
14400 64 20 74 68 61 74 20 61 20 73 74 61 74 65 6d 65 d that a stateme
14401 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 nt transaction w
14402 61 73 20 6e 6f 74 20 61 63 74 69 76 65 2c 20 62 as not active, b
14403 75 74 20 74 68 69 73 20 72 65 73 74 72 69 63 74 ut this restrict
14404 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 ion.** has been
14405 72 65 6d 6f 76 65 64 20 28 43 52 45 41 54 45 20 removed (CREATE
14406 49 4e 44 45 58 20 6e 65 65 64 73 20 74 6f 20 6d INDEX needs to m
14407 6f 76 65 20 61 20 70 61 67 65 20 77 68 65 6e 20 ove a page when
14408 61 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 a statement.** t
14409 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 ransaction is ac
1440a 74 69 76 65 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 tive)..**.** If
1440b 74 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d the fourth argum
1440c 65 6e 74 2c 20 69 73 43 6f 6d 6d 69 74 2c 20 69 ent, isCommit, i
1440d 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
1440e 20 74 68 69 73 20 70 61 67 65 20 69 73 20 62 65 this page is be
1440f 69 6e 67 0a 2a 2a 20 6d 6f 76 65 64 20 61 73 20 ing.** moved as
14410 70 61 72 74 20 6f 66 20 61 20 64 61 74 61 62 61 part of a databa
14411 73 65 20 72 65 6f 72 67 61 6e 69 7a 61 74 69 6f se reorganizatio
14412 6e 20 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68 n just before th
14413 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a e transaction .*
14414 2a 20 69 73 20 62 65 69 6e 67 20 63 6f 6d 6d 69 * is being commi
14415 74 74 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 tted. In this ca
14416 73 65 2c 20 69 74 20 69 73 20 67 75 61 72 61 6e se, it is guaran
14417 74 65 65 64 20 74 68 61 74 20 74 68 65 20 64 61 teed that the da
14418 74 61 62 61 73 65 20 70 61 67 65 20 0a 2a 2a 20 tabase page .**
14419 70 50 67 20 72 65 66 65 72 73 20 74 6f 20 77 69 pPg refers to wi
1441a 6c 6c 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 ll not be writte
1441b 6e 20 74 6f 20 61 67 61 69 6e 20 77 69 74 68 69 n to again withi
1441c 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 n this transacti
1441d 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 on..**.** This f
1441e 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 unction may retu
1441f 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 rn SQLITE_NOMEM
14420 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 or an IO error c
14421 6f 64 65 20 69 66 20 61 6e 20 65 72 72 6f 72 0a ode if an error.
14422 2a 2a 20 6f 63 63 75 72 73 2e 20 4f 74 68 65 72 ** occurs. Other
14423 77 69 73 65 2c 20 69 74 20 72 65 74 75 72 6e 73 wise, it returns
14424 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 SQLITE_OK..*/.S
14425 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
14426 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f t sqlite3PagerMo
14427 76 65 70 61 67 65 28 50 61 67 65 72 20 2a 70 50 vepage(Pager *pP
14428 61 67 65 72 2c 20 44 62 50 61 67 65 20 2a 70 50 ager, DbPage *pP
14429 67 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e g, Pgno pgno, in
1442a 74 20 69 73 43 6f 6d 6d 69 74 29 7b 0a 20 20 50 t isCommit){. P
1442b 67 48 64 72 20 2a 70 50 67 4f 6c 64 3b 20 20 20 gHdr *pPgOld;
1442c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1442d 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 6f 76 he page being ov
1442e 65 72 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 erwritten. */.
1442f 50 67 6e 6f 20 6e 65 65 64 53 79 6e 63 50 67 6e Pgno needSyncPgn
14430 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 o = 0; /*
14431 4f 6c 64 20 76 61 6c 75 65 20 6f 66 20 70 50 67 Old value of pPg
14432 2d 3e 70 67 6e 6f 2c 20 69 66 20 73 79 6e 63 20 ->pgno, if sync
14433 69 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 is required */.
14434 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
14435 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
14436 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
14437 20 20 50 67 6e 6f 20 6f 72 69 67 50 67 6e 6f 3b Pgno origPgno;
14438 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
14439 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 * The original p
1443a 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 age number */..
1443b 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 assert( pPg->nR
1443c 65 66 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 ef>0 );.. /* If
1443d 20 74 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 the page being
1443e 6d 6f 76 65 64 20 69 73 20 64 69 72 74 79 20 61 moved is dirty a
1443f 6e 64 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 nd has not been
14440 73 61 76 65 64 20 62 79 20 74 68 65 20 6c 61 74 saved by the lat
14441 65 73 74 0a 20 20 2a 2a 20 73 61 76 65 70 6f 69 est. ** savepoi
14442 6e 74 2c 20 74 68 65 6e 20 73 61 76 65 20 74 68 nt, then save th
14443 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e e current conten
14444 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 69 ts of the page i
14445 6e 74 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 nto the . ** su
14446 62 2d 6a 6f 75 72 6e 61 6c 20 6e 6f 77 2e 20 54 b-journal now. T
14447 68 69 73 20 69 73 20 72 65 71 75 69 72 65 64 20 his is required
14448 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 66 6f to handle the fo
14449 6c 6c 6f 77 69 6e 67 20 73 63 65 6e 61 72 69 6f llowing scenario
1444a 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 :. **. ** BE
1444b 47 49 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a GIN;. ** <j
1444c 6f 75 72 6e 61 6c 20 70 61 67 65 20 58 2c 20 74 ournal page X, t
1444d 68 65 6e 20 6d 6f 64 69 66 79 20 69 74 20 69 6e hen modify it in
1444e 20 6d 65 6d 6f 72 79 3e 0a 20 20 2a 2a 20 20 20 memory>. **
1444f 20 20 53 41 56 45 50 4f 49 4e 54 20 6f 6e 65 3b SAVEPOINT one;
14450 0a 20 20 2a 2a 20 20 20 20 20 20 20 3c 4d 6f 76 . ** <Mov
14451 65 20 70 61 67 65 20 58 20 74 6f 20 6c 6f 63 61 e page X to loca
14452 74 69 6f 6e 20 59 3e 0a 20 20 2a 2a 20 20 20 20 tion Y>. **
14453 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 ROLLBACK TO one
14454 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 ;. **. ** If p
14455 61 67 65 20 58 20 77 65 72 65 20 6e 6f 74 20 77 age X were not w
14456 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 75 ritten to the su
14457 62 2d 6a 6f 75 72 6e 61 6c 20 68 65 72 65 2c 20 b-journal here,
14458 69 74 20 77 6f 75 6c 64 20 6e 6f 74 0a 20 20 2a it would not. *
14459 2a 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f * be possible to
1445a 20 72 65 73 74 6f 72 65 20 69 74 73 20 63 6f 6e restore its con
1445b 74 65 6e 74 73 20 77 68 65 6e 20 74 68 65 20 22 tents when the "
1445c 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 22 ROLLBACK TO one"
1445d 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 . ** statement
1445e 77 65 72 65 20 69 73 20 70 72 6f 63 65 73 73 65 were is processe
1445f 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 73 75 62 d.. **. ** sub
14460 6a 6f 75 72 6e 61 6c 50 61 67 65 28 29 20 6d 61 journalPage() ma
14461 79 20 6e 65 65 64 20 74 6f 20 61 6c 6c 6f 63 61 y need to alloca
14462 74 65 20 73 70 61 63 65 20 74 6f 20 73 74 6f 72 te space to stor
14463 65 20 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 74 6f e pPg->pgno into
14464 0a 20 20 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 . ** one or mor
14465 65 20 73 61 76 65 70 6f 69 6e 74 20 62 69 74 76 e savepoint bitv
14466 65 63 73 2e 20 54 68 69 73 20 69 73 20 74 68 65 ecs. This is the
14467 20 72 65 61 73 6f 6e 20 74 68 69 73 20 66 75 6e reason this fun
14468 63 74 69 6f 6e 0a 20 20 2a 2a 20 6d 61 79 20 72 ction. ** may r
14469 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
1446a 45 4d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 EM.. */. if( p
1446b 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
1446c 44 49 52 54 59 20 0a 20 20 20 26 26 20 73 75 62 DIRTY . && sub
1446d 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50 jRequiresPage(pP
1446e 67 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f g). && SQLITE_
1446f 4f 4b 21 3d 28 72 63 20 3d 20 73 75 62 6a 6f 75 OK!=(rc = subjou
14470 72 6e 61 6c 50 61 67 65 28 70 50 67 29 29 0a 20 rnalPage(pPg)).
14471 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
14472 63 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 c;. }.. PAGERT
14473 52 41 43 45 28 28 22 4d 4f 56 45 20 25 64 20 70 RACE(("MOVE %d p
14474 61 67 65 20 25 64 20 28 6e 65 65 64 53 79 6e 63 age %d (needSync
14475 3d 25 64 29 20 6d 6f 76 65 73 20 74 6f 20 25 64 =%d) moves to %d
14476 5c 6e 22 2c 20 0a 20 20 20 20 20 20 50 41 47 45 \n", . PAGE
14477 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 RID(pPager), pPg
14478 2d 3e 70 67 6e 6f 2c 20 28 70 50 67 2d 3e 66 6c ->pgno, (pPg->fl
14479 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 ags&PGHDR_NEED_S
1447a 59 4e 43 29 3f 31 3a 30 2c 20 70 67 6e 6f 29 29 YNC)?1:0, pgno))
1447b 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 4d 4f ;. IOTRACE(("MO
1447c 56 45 20 25 70 20 25 64 20 25 64 5c 6e 22 2c 20 VE %p %d %d\n",
1447d 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e pPager, pPg->pgn
1447e 6f 2c 20 70 67 6e 6f 29 29 0a 0a 20 20 2f 2a 20 o, pgno)).. /*
1447f 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e If the journal n
14480 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 28 eeds to be sync(
14481 29 65 64 20 62 65 66 6f 72 65 20 70 61 67 65 20 )ed before page
14482 70 50 67 2d 3e 70 67 6e 6f 20 63 61 6e 0a 20 20 pPg->pgno can.
14483 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f ** be written to
14484 2c 20 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e , store pPg->pgn
14485 6f 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 o in local varia
14486 62 6c 65 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f ble needSyncPgno
14487 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 .. **. ** If t
14488 68 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 he isCommit flag
14489 20 69 73 20 73 65 74 2c 20 74 68 65 72 65 20 69 is set, there i
1448a 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d s no need to rem
1448b 65 6d 62 65 72 20 74 68 61 74 0a 20 20 2a 2a 20 ember that. **
1448c 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 the journal need
1448d 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64 s to be sync()ed
1448e 20 62 65 66 6f 72 65 20 64 61 74 61 62 61 73 65 before database
1448f 20 70 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 page pPg->pgno
14490 0a 20 20 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 . ** can be wri
14491 74 74 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c tten to. The cal
14492 6c 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 ler has already
14493 70 72 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 promised not to
14494 77 72 69 74 65 20 74 6f 20 69 74 2e 0a 20 20 2a write to it.. *
14495 2f 0a 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c /. if( (pPg->fl
14496 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 ags&PGHDR_NEED_S
14497 59 4e 43 29 20 26 26 20 21 69 73 43 6f 6d 6d 69 YNC) && !isCommi
14498 74 20 29 7b 0a 20 20 20 20 6e 65 65 64 53 79 6e t ){. needSyn
14499 63 50 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e cPgno = pPg->pgn
1449a 6f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 o;. assert( p
1449b 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 ageInJournal(pPg
1449c 29 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 ) || pPg->pgno>p
1449d 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a Pager->dbOrigSiz
1449e 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 e );. assert(
1449f 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 pPg->flags&PGHD
144a0 52 5f 44 49 52 54 59 20 29 3b 0a 20 20 20 20 61 R_DIRTY );. a
144a1 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e ssert( pPager->n
144a2 65 65 64 53 79 6e 63 20 29 3b 0a 20 20 7d 0a 0a eedSync );. }..
144a3 20 20 2f 2a 20 49 66 20 74 68 65 20 63 61 63 68 /* If the cach
144a4 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 61 67 e contains a pag
144a5 65 20 77 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 e with page-numb
144a6 65 72 20 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 er pgno, remove
144a7 69 74 0a 20 20 2a 2a 20 66 72 6f 6d 20 69 74 73 it. ** from its
144a8 20 68 61 73 68 20 63 68 61 69 6e 2e 20 41 6c 73 hash chain. Als
144a9 6f 2c 20 69 66 20 74 68 65 20 50 67 48 64 72 2e o, if the PgHdr.
144aa 6e 65 65 64 53 79 6e 63 20 77 61 73 20 73 65 74 needSync was set
144ab 20 66 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 20 for . ** page
144ac 70 67 6e 6f 20 62 65 66 6f 72 65 20 74 68 65 20 pgno before the
144ad 27 6d 6f 76 65 27 20 6f 70 65 72 61 74 69 6f 6e 'move' operation
144ae 2c 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 , it needs to be
144af 20 72 65 74 61 69 6e 65 64 20 0a 20 20 2a 2a 20 retained . **
144b0 66 6f 72 20 74 68 65 20 70 61 67 65 20 6d 6f 76 for the page mov
144b1 65 64 20 74 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 ed there.. */.
144b2 20 70 50 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e pPg->flags &= ~
144b3 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b PGHDR_NEED_SYNC;
144b4 0a 20 20 70 50 67 4f 6c 64 20 3d 20 70 61 67 65 . pPgOld = page
144b5 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c r_lookup(pPager,
144b6 20 70 67 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74 pgno);. assert
144b7 28 20 21 70 50 67 4f 6c 64 20 7c 7c 20 70 50 67 ( !pPgOld || pPg
144b8 4f 6c 64 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a Old->nRef==1 );.
144b9 20 20 69 66 28 20 70 50 67 4f 6c 64 20 29 7b 0a if( pPgOld ){.
144ba 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c pPg->flags |
144bb 3d 20 28 70 50 67 4f 6c 64 2d 3e 66 6c 61 67 73 = (pPgOld->flags
144bc 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
144bd 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 );. sqlite3Pc
144be 61 63 68 65 44 72 6f 70 28 70 50 67 4f 6c 64 29 acheDrop(pPgOld)
144bf 3b 0a 20 20 7d 0a 0a 20 20 6f 72 69 67 50 67 6e ;. }.. origPgn
144c0 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 o = pPg->pgno;.
144c1 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f sqlite3PcacheMo
144c2 76 65 28 70 50 67 2c 20 70 67 6e 6f 29 3b 0a 20 ve(pPg, pgno);.
144c3 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 sqlite3PcacheMa
144c4 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20 keDirty(pPg);.
144c5 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 pPager->dbModifi
144c6 65 64 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20 6e ed = 1;.. if( n
144c7 65 65 64 53 79 6e 63 50 67 6e 6f 20 29 7b 0a 20 eedSyncPgno ){.
144c8 20 20 20 2f 2a 20 49 66 20 6e 65 65 64 53 79 6e /* If needSyn
144c9 63 50 67 6e 6f 20 69 73 20 6e 6f 6e 2d 7a 65 72 cPgno is non-zer
144ca 6f 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 o, then the jour
144cb 6e 61 6c 20 66 69 6c 65 20 6e 65 65 64 73 20 74 nal file needs t
144cc 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20 73 79 6e o be . ** syn
144cd 63 28 29 65 64 20 62 65 66 6f 72 65 20 61 6e 79 c()ed before any
144ce 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e data is written
144cf 20 74 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c to database fil
144d0 65 20 70 61 67 65 20 6e 65 65 64 53 79 6e 63 50 e page needSyncP
144d1 67 6e 6f 2e 0a 20 20 20 20 2a 2a 20 43 75 72 72 gno.. ** Curr
144d2 65 6e 74 6c 79 2c 20 6e 6f 20 73 75 63 68 20 70 ently, no such p
144d3 61 67 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 age exists in th
144d4 65 20 70 61 67 65 2d 63 61 63 68 65 20 61 6e 64 e page-cache and
144d5 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 22 69 73 the . ** "is
144d6 20 6a 6f 75 72 6e 61 6c 65 64 22 20 62 69 74 76 journaled" bitv
144d7 65 63 20 66 6c 61 67 20 68 61 73 20 62 65 65 6e ec flag has been
144d8 20 73 65 74 2e 20 54 68 69 73 20 6e 65 65 64 73 set. This needs
144d9 20 74 6f 20 62 65 20 72 65 6d 65 64 69 65 64 20 to be remedied
144da 62 79 0a 20 20 20 20 2a 2a 20 6c 6f 61 64 69 6e by. ** loadin
144db 67 20 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 g the page into
144dc 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 the pager-cache
144dd 61 6e 64 20 73 65 74 74 69 6e 67 20 74 68 65 20 and setting the
144de 50 67 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 0a PgHdr.needSync .
144df 20 20 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 20 ** flag..
144e0 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 **. ** If th
144e1 65 20 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 e attempt to loa
144e2 64 20 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 d the page into
144e3 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 66 the page-cache f
144e4 61 69 6c 73 2c 20 28 64 75 65 0a 20 20 20 20 2a ails, (due. *
144e5 2a 20 74 6f 20 61 20 6d 61 6c 6c 6f 63 28 29 20 * to a malloc()
144e6 6f 72 20 49 4f 20 66 61 69 6c 75 72 65 29 2c 20 or IO failure),
144e7 63 6c 65 61 72 20 74 68 65 20 62 69 74 20 69 6e clear the bit in
144e8 20 74 68 65 20 70 49 6e 4a 6f 75 72 6e 61 6c 5b the pInJournal[
144e9 5d 0a 20 20 20 20 2a 2a 20 61 72 72 61 79 2e 20 ]. ** array.
144ea 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 Otherwise, if th
144eb 65 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65 64 e page is loaded
144ec 20 61 6e 64 20 77 72 69 74 74 65 6e 20 61 67 61 and written aga
144ed 69 6e 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 69 in in. ** thi
144ee 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 s transaction, i
144ef 74 20 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e t may be written
144f0 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
144f1 20 66 69 6c 65 20 62 65 66 6f 72 65 0a 20 20 20 file before.
144f2 20 2a 2a 20 69 74 20 69 73 20 73 79 6e 63 65 64 ** it is synced
144f3 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 into the journa
144f4 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 77 61 79 l file. This way
144f5 2c 20 69 74 20 6d 61 79 20 65 6e 64 20 75 70 20 , it may end up
144f6 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f in. ** the jo
144f7 75 72 6e 61 6c 20 66 69 6c 65 20 74 77 69 63 65 urnal file twice
144f8 2c 20 62 75 74 20 74 68 61 74 20 69 73 20 6e 6f , but that is no
144f9 74 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 20 t a problem..
144fa 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 **. ** The s
144fb 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 qlite3PagerGet()
144fc 20 63 61 6c 6c 20 6d 61 79 20 63 61 75 73 65 20 call may cause
144fd 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 the journal to s
144fe 79 6e 63 2e 20 53 6f 20 6d 61 6b 65 0a 20 20 20 ync. So make.
144ff 20 2a 2a 20 73 75 72 65 20 74 68 65 20 50 61 67 ** sure the Pag
14500 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 er.needSync flag
14501 20 69 73 20 73 65 74 20 74 6f 6f 2e 0a 20 20 20 is set too..
14502 20 2a 2f 0a 20 20 20 20 50 67 48 64 72 20 2a 70 */. PgHdr *p
14503 50 67 48 64 72 3b 0a 20 20 20 20 61 73 73 65 72 PgHdr;. asser
14504 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 t( pPager->needS
14505 79 6e 63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 ync );. rc =
14506 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
14507 70 50 61 67 65 72 2c 20 6e 65 65 64 53 79 6e 63 pPager, needSync
14508 50 67 6e 6f 2c 20 26 70 50 67 48 64 72 29 3b 0a Pgno, &pPgHdr);.
14509 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1450a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 TE_OK ){. i
1450b 66 28 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 3c f( needSyncPgno<
1450c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 =pPager->dbOrigS
1450d 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 61 ize ){. a
1450e 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 ssert( pPager->p
1450f 54 6d 70 53 70 61 63 65 21 3d 30 20 29 3b 0a 20 TmpSpace!=0 );.
14510 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 sqlite3Bi
14511 74 76 65 63 43 6c 65 61 72 28 70 50 61 67 65 72 tvecClear(pPager
14512 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 6e 65 ->pInJournal, ne
14513 65 64 53 79 6e 63 50 67 6e 6f 2c 20 70 50 61 67 edSyncPgno, pPag
14514 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a er->pTmpSpace);.
14515 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 }. re
14516 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
14517 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 pPager->needS
14518 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 61 73 73 ync = 1;. ass
14519 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 ert( pPager->noS
1451a 79 6e 63 3d 3d 30 20 26 26 20 21 4d 45 4d 44 42 ync==0 && !MEMDB
1451b 20 29 3b 0a 20 20 20 20 70 50 67 48 64 72 2d 3e );. pPgHdr->
1451c 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e flags |= PGHDR_N
1451d 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 73 71 EED_SYNC;. sq
1451e 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 lite3PcacheMakeD
1451f 69 72 74 79 28 70 50 67 48 64 72 29 3b 0a 20 20 irty(pPgHdr);.
14520 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
14521 72 65 66 28 70 50 67 48 64 72 29 3b 0a 20 20 7d ref(pPgHdr);. }
14522 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 6f 72 20 .. /*. ** For
14523 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 an in-memory dat
14524 61 62 61 73 65 2c 20 6d 61 6b 65 20 73 75 72 65 abase, make sure
14525 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 the original pa
14526 67 65 20 63 6f 6e 74 69 6e 75 65 73 0a 20 20 2a ge continues. *
14527 2a 20 74 6f 20 65 78 69 73 74 2c 20 69 6e 20 63 * to exist, in c
14528 61 73 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 ase the transact
14529 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 72 6f 6c ion needs to rol
1452a 6c 20 62 61 63 6b 2e 20 20 57 65 20 61 6c 6c 6f l back. We allo
1452b 63 61 74 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 cate. ** the pa
1452c 67 65 20 6e 6f 77 2c 20 69 6e 73 74 65 61 64 20 ge now, instead
1452d 6f 66 20 61 74 20 72 6f 6c 6c 62 61 63 6b 2c 20 of at rollback,
1452e 62 65 63 61 75 73 65 20 77 65 20 63 61 6e 20 62 because we can b
1452f 65 74 74 65 72 20 64 65 61 6c 0a 20 20 2a 2a 20 etter deal. **
14530 77 69 74 68 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d with an out-of-m
14531 65 6d 6f 72 79 20 65 72 72 6f 72 20 6e 6f 77 2e emory error now.
14532 20 20 54 69 63 6b 65 74 20 23 33 37 36 31 2e 0a Ticket #3761..
14533 20 20 2a 2f 0a 20 20 69 66 28 20 4d 45 4d 44 42 */. if( MEMDB
14534 20 29 7b 0a 20 20 20 20 44 62 50 61 67 65 20 2a ){. DbPage *
14535 70 4e 65 77 3b 0a 20 20 20 20 72 63 20 3d 20 73 pNew;. rc = s
14536 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 qlite3PagerAcqui
14537 72 65 28 70 50 61 67 65 72 2c 20 6f 72 69 67 50 re(pPager, origP
14538 67 6e 6f 2c 20 26 70 4e 65 77 2c 20 31 29 3b 0a gno, &pNew, 1);.
14539 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1453a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 TE_OK ){. s
1453b 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 qlite3PcacheMove
1453c 28 70 50 67 2c 20 6f 72 69 67 50 67 6e 6f 29 3b (pPg, origPgno);
1453d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
1453e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
1453f 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4e te3PagerUnref(pN
14540 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 ew);. }.. retu
14541 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
14542 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 #endif../*.** Re
14543 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
14544 6f 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 o the data for t
14545 68 65 20 73 70 65 63 69 66 69 65 64 20 70 61 67 he specified pag
14546 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
14547 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
14548 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 44 e3PagerGetData(D
14549 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 61 bPage *pPg){. a
1454a 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66 ssert( pPg->nRef
1454b 3e 30 20 7c 7c 20 70 50 67 2d 3e 70 50 61 67 65 >0 || pPg->pPage
1454c 72 2d 3e 6d 65 6d 44 62 20 29 3b 0a 20 20 72 65 r->memDb );. re
1454d 74 75 72 6e 20 70 50 67 2d 3e 70 44 61 74 61 3b turn pPg->pData;
1454e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
1454f 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
14550 65 20 50 61 67 65 72 2e 6e 45 78 74 72 61 20 62 e Pager.nExtra b
14551 79 74 65 73 20 6f 66 20 22 65 78 74 72 61 22 20 ytes of "extra"
14552 73 70 61 63 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61 space .** alloca
14553 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 ted along with t
14554 68 65 20 73 70 65 63 69 66 69 65 64 20 70 61 67 he specified pag
14555 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
14556 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
14557 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 e3PagerGetExtra(
14558 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 DbPage *pPg){.
14559 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 45 78 74 return pPg->pExt
1455a 72 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 ra;.}../*.** Get
1455b 2f 73 65 74 20 74 68 65 20 6c 6f 63 6b 69 6e 67 /set the locking
1455c 2d 6d 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70 -mode for this p
1455d 61 67 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20 ager. Parameter
1455e 65 4d 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e eMode must be on
1455f 65 0a 2a 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f e.** of PAGER_LO
14560 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c CKINGMODE_QUERY,
14561 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f PAGER_LOCKINGMO
14562 44 45 5f 4e 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a DE_NORMAL or .**
14563 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f PAGER_LOCKINGMO
14564 44 45 5f 45 58 43 4c 55 53 49 56 45 2e 20 49 66 DE_EXCLUSIVE. If
14565 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 the parameter i
14566 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20 74 68 s not _QUERY, th
14567 65 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e en.** the lockin
14568 67 2d 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f g-mode is set to
14569 20 74 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 the value speci
1456a 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 fied..**.** The
1456b 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 returned value i
1456c 73 20 65 69 74 68 65 72 20 50 41 47 45 52 5f 4c s either PAGER_L
1456d 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 OCKINGMODE_NORMA
1456e 4c 20 6f 72 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f L or.** PAGER_LO
1456f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 CKINGMODE_EXCLUS
14570 49 56 45 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 IVE, indicating
14571 74 68 65 20 63 75 72 72 65 6e 74 20 28 70 6f 73 the current (pos
14572 73 69 62 6c 79 20 75 70 64 61 74 65 64 29 0a 2a sibly updated).*
14573 2a 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a * locking-mode..
14574 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14575 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
14576 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 erLockingMode(Pa
14577 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
14578 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 eMode){. asser
14579 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f t( eMode==PAGER_
1457a 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 LOCKINGMODE_QUER
1457b 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c Y. ||
1457c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f eMode==PAGER_LO
1457d 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c CKINGMODE_NORMAL
1457e 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 . ||
1457f 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 eMode==PAGER_LOC
14580 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 KINGMODE_EXCLUSI
14581 56 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 VE );. assert(
14582 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 PAGER_LOCKINGMOD
14583 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 61 E_QUERY<0 );. a
14584 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 ssert( PAGER_LOC
14585 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e KINGMODE_NORMAL>
14586 3d 30 20 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b =0 && PAGER_LOCK
14587 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 INGMODE_EXCLUSIV
14588 45 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 65 4d E>=0 );. if( eM
14589 6f 64 65 3e 3d 30 20 26 26 20 21 70 50 61 67 65 ode>=0 && !pPage
1458a 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 r->tempFile ){.
1458b 20 20 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 pPager->exclu
1458c 73 69 76 65 4d 6f 64 65 20 3d 20 28 75 38 29 65 siveMode = (u8)e
1458d 4d 6f 64 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 Mode;. }. retu
1458e 72 6e 20 28 69 6e 74 29 70 50 61 67 65 72 2d 3e rn (int)pPager->
1458f 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3b 0a 7d exclusiveMode;.}
14590 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 20 ../*.** Get/set
14591 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 the journal-mode
14592 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e for this pager.
14593 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f 64 65 Parameter eMode
14594 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 3a must be one of:
14595 0a 2a 2a 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f .**.** PAGER_
14596 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 JOURNALMODE_QUER
14597 59 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f Y.** PAGER_JO
14598 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 URNALMODE_DELETE
14599 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 .** PAGER_JOU
1459a 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 RNALMODE_TRUNCAT
1459b 45 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f E.** PAGER_JO
1459c 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 URNALMODE_PERSIS
1459d 54 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f T.** PAGER_JO
1459e 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a URNALMODE_OFF.**
1459f 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 PAGER_JOURNA
145a0 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 0a 2a 2a 0a LMODE_MEMORY.**.
145a1 2a 2a 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 ** If the parame
145a2 74 65 72 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 ter is not _QUER
145a3 59 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 Y, then the jour
145a4 6e 61 6c 5f 6d 6f 64 65 20 69 73 20 73 65 74 20 nal_mode is set
145a5 74 6f 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 to the.** value
145a6 73 70 65 63 69 66 69 65 64 20 69 66 20 74 68 65 specified if the
145a7 20 63 68 61 6e 67 65 20 69 73 20 61 6c 6c 6f 77 change is allow
145a8 65 64 2e 20 20 54 68 65 20 63 68 61 6e 67 65 20 ed. The change
145a9 69 73 20 64 69 73 61 6c 6c 6f 77 65 64 0a 2a 2a is disallowed.**
145aa 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 for the followi
145ab 6e 67 20 72 65 61 73 6f 6e 73 3a 0a 2a 2a 0a 2a ng reasons:.**.*
145ac 2a 20 20 20 2a 20 20 41 6e 20 69 6e 2d 6d 65 6d * * An in-mem
145ad 6f 72 79 20 64 61 74 61 62 61 73 65 20 63 61 6e ory database can
145ae 20 6f 6e 6c 79 20 68 61 76 65 20 69 74 73 20 6a only have its j
145af 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 73 65 74 20 ournal_mode set
145b0 74 6f 20 5f 4f 46 46 0a 2a 2a 20 20 20 20 20 20 to _OFF.**
145b1 6f 72 20 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a or _MEMORY..**.*
145b2 2a 20 20 20 2a 20 20 54 68 65 20 6a 6f 75 72 6e * * The journ
145b3 61 6c 20 6d 6f 64 65 20 6d 61 79 20 6e 6f 74 20 al mode may not
145b4 62 65 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65 be changed while
145b5 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
145b6 73 20 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 s active..**.**
145b7 54 68 65 20 72 65 74 75 72 6e 65 64 20 69 6e 64 The returned ind
145b8 69 63 61 74 65 20 74 68 65 20 63 75 72 72 65 6e icate the curren
145b9 74 20 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 t (possibly upda
145ba 74 65 64 29 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 ted) journal-mod
145bb 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
145bc 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
145bd 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 PagerJournalMode
145be 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
145bf 69 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 int eMode){. as
145c0 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 sert( eMode==PAG
145c1 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 ER_JOURNALMODE_Q
145c2 55 45 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 UERY.
145c3 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 || eMode==PAGER
145c4 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c _JOURNALMODE_DEL
145c5 45 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 ETE.
145c6 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f || eMode==PAGER_
145c7 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e JOURNALMODE_TRUN
145c8 43 41 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 CATE.
145c9 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 || eMode==PAGER
145ca 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 _JOURNALMODE_PER
145cb 53 49 53 54 0a 20 20 20 20 20 20 20 20 20 20 20 SIST.
145cc 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 || eMode==PAGER
145cd 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 _JOURNALMODE_OFF
145ce 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c . ||
145cf 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f eMode==PAGER_JO
145d0 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 URNALMODE_MEMORY
145d1 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 );. assert( PA
145d2 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
145d3 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 69 66 28 QUERY<0 );. if(
145d4 20 65 4d 6f 64 65 3e 3d 30 0a 20 20 20 26 26 20 eMode>=0. &&
145d5 28 21 4d 45 4d 44 42 20 7c 7c 20 65 4d 6f 64 65 (!MEMDB || eMode
145d6 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
145d7 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 ODE_MEMORY .
145d8 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f || eMo
145d9 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
145da 4c 4d 4f 44 45 5f 4f 46 46 29 0a 20 20 20 26 26 LMODE_OFF). &&
145db 20 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 !pPager->dbModi
145dc 66 69 65 64 0a 20 20 20 26 26 20 28 21 69 73 4f fied. && (!isO
145dd 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
145de 20 7c 7c 20 30 3d 3d 70 50 61 67 65 72 2d 3e 6a || 0==pPager->j
145df 6f 75 72 6e 61 6c 4f 66 66 29 0a 20 20 29 7b 0a ournalOff). ){.
145e0 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 if( isOpen(p
145e1 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 Pager->jfd) ){.
145e2 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c sqlite3OsCl
145e3 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 ose(pPager->jfd)
145e4 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 ;. }. pPag
145e5 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 er->journalMode
145e6 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 7d = (u8)eMode;. }
145e7 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 . return (int)p
145e8 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
145e9 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 de;.}../*.** Get
145ea 2f 73 65 74 20 74 68 65 20 73 69 7a 65 2d 6c 69 /set the size-li
145eb 6d 69 74 20 75 73 65 64 20 66 6f 72 20 70 65 72 mit used for per
145ec 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 sistent journal
145ed 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 files..**.** Set
145ee 74 69 6e 67 20 74 68 65 20 73 69 7a 65 20 6c 69 ting the size li
145ef 6d 69 74 20 74 6f 20 2d 31 20 6d 65 61 6e 73 20 mit to -1 means
145f0 6e 6f 20 6c 69 6d 69 74 20 69 73 20 65 6e 66 6f no limit is enfo
145f1 72 63 65 64 2e 0a 2a 2a 20 41 6e 20 61 74 74 65 rced..** An atte
145f2 6d 70 74 20 74 6f 20 73 65 74 20 61 20 6c 69 6d mpt to set a lim
145f3 69 74 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 it smaller than
145f4 2d 31 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a -1 is a no-op..*
145f5 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
145f6 20 69 36 34 20 73 71 6c 69 74 65 33 50 61 67 65 i64 sqlite3Page
145f7 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 rJournalSizeLimi
145f8 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c t(Pager *pPager,
145f9 20 69 36 34 20 69 4c 69 6d 69 74 29 7b 0a 20 20 i64 iLimit){.
145fa 69 66 28 20 69 4c 69 6d 69 74 3e 3d 2d 31 20 29 if( iLimit>=-1 )
145fb 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f {. pPager->jo
145fc 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d urnalSizeLimit =
145fd 20 69 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 iLimit;. }. r
145fe 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 6a 6f eturn pPager->jo
145ff 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a urnalSizeLimit;.
14600 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
14601 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
14602 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 pPager->pBackup
14603 20 76 61 72 69 61 62 6c 65 2e 20 54 68 65 20 62 variable. The b
14604 61 63 6b 75 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20 ackup module.**
14605 69 6e 20 62 61 63 6b 75 70 2e 63 20 6d 61 69 6e in backup.c main
14606 74 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e tains the conten
14607 74 20 6f 66 20 74 68 69 73 20 76 61 72 69 61 62 t of this variab
14608 6c 65 2e 20 54 68 69 73 20 6d 6f 64 75 6c 65 0a le. This module.
14609 2a 2a 20 75 73 65 73 20 69 74 20 6f 70 61 71 75 ** uses it opaqu
1460a 65 6c 79 20 61 73 20 61 6e 20 61 72 67 75 6d 65 ely as an argume
1460b 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 42 61 63 nt to sqlite3Bac
1460c 6b 75 70 52 65 73 74 61 72 74 28 29 20 61 6e 64 kupRestart() and
1460d 0a 2a 2a 20 73 71 6c 69 74 65 33 42 61 63 6b 75 .** sqlite3Backu
1460e 70 55 70 64 61 74 65 28 29 20 6f 6e 6c 79 2e 0a pUpdate() only..
1460f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14610 45 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 E sqlite3_backup
14611 20 2a 2a 73 71 6c 69 74 65 33 50 61 67 65 72 42 **sqlite3PagerB
14612 61 63 6b 75 70 50 74 72 28 50 61 67 65 72 20 2a ackupPtr(Pager *
14613 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 pPager){. retur
14614 6e 20 26 70 50 61 67 65 72 2d 3e 70 42 61 63 6b n &pPager->pBack
14615 75 70 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a up;.}..#endif /*
14616 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 SQLITE_OMIT_DIS
14617 4b 49 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a KIO */../*******
14618 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 ******* End of p
14619 61 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ager.c *********
1461a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1461b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1461c 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
1461d 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
1461e 6c 65 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a le btmutex.c ***
1461f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14621 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
14622 30 37 20 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 07 August 27.**.
14623 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
14624 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
14625 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
14626 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
14627 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
14628 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
14629 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
1462a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
1462b 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
1462c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1462d 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
1462e 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
1462f 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
14630 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
14631 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
14632 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
14633 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
14634 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
14635 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14636 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14637 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14638 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
14639 2a 20 24 49 64 3a 20 62 74 6d 75 74 65 78 2e 63 * $Id: btmutex.c
1463a 2c 76 20 31 2e 31 37 20 32 30 30 39 2f 30 37 2f ,v 1.17 2009/07/
1463b 32 30 20 31 32 3a 33 33 3a 33 33 20 64 72 68 20 20 12:33:33 drh
1463c 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 Exp $.**.** This
1463d 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
1463e 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c ode used to impl
1463f 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 6f 6e ement mutexes on
14640 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73 2e 0a Btree objects..
14641 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 72 65 61 ** This code rea
14642 6c 6c 79 20 62 65 6c 6f 6e 67 73 20 69 6e 20 62 lly belongs in b
14643 74 72 65 65 2e 63 2e 20 20 42 75 74 20 62 74 72 tree.c. But btr
14644 65 65 2e 63 20 69 73 20 67 65 74 74 69 6e 67 20 ee.c is getting
14645 74 6f 6f 0a 2a 2a 20 62 69 67 20 61 6e 64 20 77 too.** big and w
14646 65 20 77 61 6e 74 20 74 6f 20 62 72 65 61 6b 20 e want to break
14647 69 74 20 64 6f 77 6e 20 73 6f 6d 65 2e 20 20 54 it down some. T
14648 68 69 73 20 70 61 63 6b 61 67 65 64 20 73 65 65 his packaged see
14649 6d 65 64 20 6c 69 6b 65 0a 2a 2a 20 61 20 67 6f med like.** a go
1464a 6f 64 20 62 72 65 61 6b 6f 75 74 2e 0a 2a 2f 0a od breakout..*/.
1464b 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1464c 49 6e 63 6c 75 64 65 20 62 74 72 65 65 49 6e 74 Include btreeInt
1464d 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
1464e 20 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a of btmutex.c **
1464f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
14650 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
14651 42 65 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 Begin file btree
14652 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
14653 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14654 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
14655 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c /*.** 2004 April
14656 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 6.**.** The aut
14657 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
14658 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
14659 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
1465a 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
1465b 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
1465c 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
1465d 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
1465e 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
1465f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
14660 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
14661 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
14662 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
14663 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
14664 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
14665 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
14666 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
14667 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
14668 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14669 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1466a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1466b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1466c 2a 0a 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 49 *.** $Id: btreeI
1466d 6e 74 2e 68 2c 76 20 31 2e 35 32 20 32 30 30 39 nt.h,v 1.52 2009
1466e 2f 30 37 2f 31 35 20 31 37 3a 32 35 3a 34 36 20 /07/15 17:25:46
1466f 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 drh Exp $.**.**
14670 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d This file implem
14671 65 6e 74 73 20 61 20 65 78 74 65 72 6e 61 6c 20 ents a external
14672 28 64 69 73 6b 2d 62 61 73 65 64 29 20 64 61 74 (disk-based) dat
14673 61 62 61 73 65 20 75 73 69 6e 67 20 42 54 72 65 abase using BTre
14674 65 73 2e 0a 2a 2a 20 46 6f 72 20 61 20 64 65 74 es..** For a det
14675 61 69 6c 65 64 20 64 69 73 63 75 73 73 69 6f 6e ailed discussion
14676 20 6f 66 20 42 54 72 65 65 73 2c 20 72 65 66 65 of BTrees, refe
14677 72 20 74 6f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 r to.**.** D
14678 6f 6e 61 6c 64 20 45 2e 20 4b 6e 75 74 68 2c 20 onald E. Knuth,
14679 54 48 45 20 41 52 54 20 4f 46 20 43 4f 4d 50 55 THE ART OF COMPU
1467a 54 45 52 20 50 52 4f 47 52 41 4d 4d 49 4e 47 2c TER PROGRAMMING,
1467b 20 56 6f 6c 75 6d 65 20 33 3a 0a 2a 2a 20 20 20 Volume 3:.**
1467c 20 20 22 53 6f 72 74 69 6e 67 20 41 6e 64 20 53 "Sorting And S
1467d 65 61 72 63 68 69 6e 67 22 2c 20 70 61 67 65 73 earching", pages
1467e 20 34 37 33 2d 34 38 30 2e 20 41 64 64 69 73 6f 473-480. Addiso
1467f 6e 2d 57 65 73 6c 65 79 0a 2a 2a 20 20 20 20 20 n-Wesley.**
14680 50 75 62 6c 69 73 68 69 6e 67 20 43 6f 6d 70 61 Publishing Compa
14681 6e 79 2c 20 52 65 61 64 69 6e 67 2c 20 4d 61 73 ny, Reading, Mas
14682 73 61 63 68 75 73 65 74 74 73 2e 0a 2a 2a 0a 2a sachusetts..**.*
14683 2a 20 54 68 65 20 62 61 73 69 63 20 69 64 65 61 * The basic idea
14684 20 69 73 20 74 68 61 74 20 65 61 63 68 20 70 61 is that each pa
14685 67 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63 ge of the file c
14686 6f 6e 74 61 69 6e 73 20 4e 20 64 61 74 61 62 61 ontains N databa
14687 73 65 0a 2a 2a 20 65 6e 74 72 69 65 73 20 61 6e se.** entries an
14688 64 20 4e 2b 31 20 70 6f 69 6e 74 65 72 73 20 74 d N+1 pointers t
14689 6f 20 73 75 62 70 61 67 65 73 2e 0a 2a 2a 0a 2a o subpages..**.*
1468a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d * ------------
1468b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1468c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1468d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1468e 2d 2d 2d 2d 0a 2a 2a 20 20 20 7c 20 20 50 74 72 ----.** | Ptr
1468f 28 30 29 20 7c 20 4b 65 79 28 30 29 20 7c 20 50 (0) | Key(0) | P
14690 74 72 28 31 29 20 7c 20 4b 65 79 28 31 29 20 7c tr(1) | Key(1) |
14691 20 2e 2e 2e 20 7c 20 4b 65 79 28 4e 2d 31 29 20 ... | Key(N-1)
14692 7c 20 50 74 72 28 4e 29 20 7c 0a 2a 2a 20 20 20 | Ptr(N) |.**
14693 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14694 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14695 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14696 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14697 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 .**.** All of th
14698 65 20 6b 65 79 73 20 6f 6e 20 74 68 65 20 70 61 e keys on the pa
14699 67 65 20 74 68 61 74 20 50 74 72 28 30 29 20 70 ge that Ptr(0) p
1469a 6f 69 6e 74 73 20 74 6f 20 68 61 76 65 20 76 61 oints to have va
1469b 6c 75 65 73 20 6c 65 73 73 0a 2a 2a 20 74 68 61 lues less.** tha
1469c 6e 20 4b 65 79 28 30 29 2e 20 20 41 6c 6c 20 6f n Key(0). All o
1469d 66 20 74 68 65 20 6b 65 79 73 20 6f 6e 20 70 61 f the keys on pa
1469e 67 65 20 50 74 72 28 31 29 20 61 6e 64 20 69 74 ge Ptr(1) and it
1469f 73 20 73 75 62 70 61 67 65 73 20 68 61 76 65 0a s subpages have.
146a0 2a 2a 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 ** values greate
146a1 72 20 74 68 61 6e 20 4b 65 79 28 30 29 20 61 6e r than Key(0) an
146a2 64 20 6c 65 73 73 20 74 68 61 6e 20 4b 65 79 28 d less than Key(
146a3 31 29 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 1). All of the
146a4 6b 65 79 73 0a 2a 2a 20 6f 6e 20 50 74 72 28 4e keys.** on Ptr(N
146a5 29 20 61 6e 64 20 69 74 73 20 73 75 62 70 61 67 ) and its subpag
146a6 65 73 20 68 61 76 65 20 76 61 6c 75 65 73 20 67 es have values g
146a7 72 65 61 74 65 72 20 74 68 61 6e 20 4b 65 79 28 reater than Key(
146a8 4e 2d 31 29 2e 20 20 41 6e 64 0a 2a 2a 20 73 6f N-1). And.** so
146a9 20 66 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 46 69 forth..**.** Fi
146aa 6e 64 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c nding a particul
146ab 61 72 20 6b 65 79 20 72 65 71 75 69 72 65 73 20 ar key requires
146ac 72 65 61 64 69 6e 67 20 4f 28 6c 6f 67 28 4d 29 reading O(log(M)
146ad 29 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 ) pages from the
146ae 20 0a 2a 2a 20 64 69 73 6b 20 77 68 65 72 65 20 .** disk where
146af 4d 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 M is the number
146b0 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 of entries in th
146b1 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e e tree..**.** In
146b2 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 this implementa
146b3 74 69 6f 6e 2c 20 61 20 73 69 6e 67 6c 65 20 66 tion, a single f
146b4 69 6c 65 20 63 61 6e 20 68 6f 6c 64 20 6f 6e 65 ile can hold one
146b5 20 6f 72 20 6d 6f 72 65 20 73 65 70 61 72 61 74 or more separat
146b6 65 20 0a 2a 2a 20 42 54 72 65 65 73 2e 20 20 45 e .** BTrees. E
146b7 61 63 68 20 42 54 72 65 65 20 69 73 20 69 64 65 ach BTree is ide
146b8 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 69 ntified by the i
146b9 6e 64 65 78 20 6f 66 20 69 74 73 20 72 6f 6f 74 ndex of its root
146ba 20 70 61 67 65 2e 20 20 54 68 65 0a 2a 2a 20 6b page. The.** k
146bb 65 79 20 61 6e 64 20 64 61 74 61 20 66 6f 72 20 ey and data for
146bc 61 6e 79 20 65 6e 74 72 79 20 61 72 65 20 63 6f any entry are co
146bd 6d 62 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 74 mbined to form t
146be 68 65 20 22 70 61 79 6c 6f 61 64 22 2e 20 20 41 he "payload". A
146bf 0a 2a 2a 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 .** fixed amount
146c0 20 6f 66 20 70 61 79 6c 6f 61 64 20 63 61 6e 20 of payload can
146c1 62 65 20 63 61 72 72 69 65 64 20 64 69 72 65 63 be carried direc
146c2 74 6c 79 20 6f 6e 20 74 68 65 20 64 61 74 61 62 tly on the datab
146c3 61 73 65 0a 2a 2a 20 70 61 67 65 2e 20 20 49 66 ase.** page. If
146c4 20 74 68 65 20 70 61 79 6c 6f 61 64 20 69 73 20 the payload is
146c5 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 larger than the
146c6 70 72 65 73 65 74 20 61 6d 6f 75 6e 74 20 74 68 preset amount th
146c7 65 6e 20 73 75 72 70 6c 75 73 0a 2a 2a 20 62 79 en surplus.** by
146c8 74 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f tes are stored o
146c9 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 n overflow pages
146ca 2e 20 20 54 68 65 20 70 61 79 6c 6f 61 64 20 66 . The payload f
146cb 6f 72 20 61 6e 20 65 6e 74 72 79 0a 2a 2a 20 61 or an entry.** a
146cc 6e 64 20 74 68 65 20 70 72 65 63 65 64 69 6e 67 nd the preceding
146cd 20 70 6f 69 6e 74 65 72 20 61 72 65 20 63 6f 6d pointer are com
146ce 62 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 61 20 bined to form a
146cf 22 43 65 6c 6c 22 2e 20 20 45 61 63 68 20 0a 2a "Cell". Each .*
146d0 2a 20 70 61 67 65 20 68 61 73 20 61 20 73 6d 61 * page has a sma
146d1 6c 6c 20 68 65 61 64 65 72 20 77 68 69 63 68 20 ll header which
146d2 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 50 74 72 contains the Ptr
146d3 28 4e 29 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 (N) pointer and
146d4 6f 74 68 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d 61 other.** informa
146d5 74 69 6f 6e 20 73 75 63 68 20 61 73 20 74 68 65 tion such as the
146d6 20 73 69 7a 65 20 6f 66 20 6b 65 79 20 61 6e 64 size of key and
146d7 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 4f 52 data..**.** FOR
146d8 4d 41 54 20 44 45 54 41 49 4c 53 0a 2a 2a 0a 2a MAT DETAILS.**.*
146d9 2a 20 54 68 65 20 66 69 6c 65 20 69 73 20 64 69 * The file is di
146da 76 69 64 65 64 20 69 6e 74 6f 20 70 61 67 65 73 vided into pages
146db 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61 67 . The first pag
146dc 65 20 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 e is called page
146dd 20 31 2c 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 1,.** the secon
146de 64 20 69 73 20 70 61 67 65 20 32 2c 20 61 6e 64 d is page 2, and
146df 20 73 6f 20 66 6f 72 74 68 2e 20 20 41 20 70 61 so forth. A pa
146e0 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 7a 65 72 ge number of zer
146e1 6f 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 22 o indicates.** "
146e2 6e 6f 20 73 75 63 68 20 70 61 67 65 22 2e 20 20 no such page".
146e3 54 68 65 20 70 61 67 65 20 73 69 7a 65 20 63 61 The page size ca
146e4 6e 20 62 65 20 61 6e 79 20 70 6f 77 65 72 20 6f n be any power o
146e5 66 20 32 20 62 65 74 77 65 65 6e 20 35 31 32 20 f 2 between 512
146e6 61 6e 64 20 33 32 37 36 38 2e 0a 2a 2a 20 45 61 and 32768..** Ea
146e7 63 68 20 70 61 67 65 20 63 61 6e 20 62 65 20 65 ch page can be e
146e8 69 74 68 65 72 20 61 20 62 74 72 65 65 20 70 61 ither a btree pa
146e9 67 65 2c 20 61 20 66 72 65 65 6c 69 73 74 20 70 ge, a freelist p
146ea 61 67 65 2c 20 61 6e 20 6f 76 65 72 66 6c 6f 77 age, an overflow
146eb 0a 2a 2a 20 70 61 67 65 2c 20 6f 72 20 61 20 70 .** page, or a p
146ec 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2e ointer-map page.
146ed 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 .**.** The first
146ee 20 70 61 67 65 20 69 73 20 61 6c 77 61 79 73 20 page is always
146ef 61 20 62 74 72 65 65 20 70 61 67 65 2e 20 20 54 a btree page. T
146f0 68 65 20 66 69 72 73 74 20 31 30 30 20 62 79 74 he first 100 byt
146f1 65 73 20 6f 66 20 74 68 65 20 66 69 72 73 74 0a es of the first.
146f2 2a 2a 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 20 ** page contain
146f3 61 20 73 70 65 63 69 61 6c 20 68 65 61 64 65 72 a special header
146f4 20 28 74 68 65 20 22 66 69 6c 65 20 68 65 61 64 (the "file head
146f5 65 72 22 29 20 74 68 61 74 20 64 65 73 63 72 69 er") that descri
146f6 62 65 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a bes the file..**
146f7 20 54 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 The format of t
146f8 68 65 20 66 69 6c 65 20 68 65 61 64 65 72 20 69 he file header i
146f9 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a s as follows:.**
146fa 0a 2a 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53 .** OFFSET S
146fb 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49 IZE DESCRIPTI
146fc 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 ON.** 0
146fd 20 20 31 36 20 20 20 20 20 48 65 61 64 65 72 20 16 Header
146fe 73 74 72 69 6e 67 3a 20 22 53 51 4c 69 74 65 20 string: "SQLite
146ff 66 6f 72 6d 61 74 20 33 5c 30 30 30 22 0a 2a 2a format 3\000".**
14700 20 20 20 20 20 31 36 20 20 20 20 20 20 20 32 20 16 2
14701 20 20 20 20 50 61 67 65 20 73 69 7a 65 20 69 6e Page size in
14702 20 62 79 74 65 73 2e 20 20 0a 2a 2a 20 20 20 20 bytes. .**
14703 20 31 38 20 20 20 20 20 20 20 31 20 20 20 20 20 18 1
14704 46 69 6c 65 20 66 6f 72 6d 61 74 20 77 72 69 74 File format writ
14705 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 e version.**
14706 20 31 39 20 20 20 20 20 20 20 31 20 20 20 20 20 19 1
14707 46 69 6c 65 20 66 6f 72 6d 61 74 20 72 65 61 64 File format read
14708 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 version.**
14709 32 30 20 20 20 20 20 20 20 31 20 20 20 20 20 42 20 1 B
1470a 79 74 65 73 20 6f 66 20 75 6e 75 73 65 64 20 73 ytes of unused s
1470b 70 61 63 65 20 61 74 20 74 68 65 20 65 6e 64 20 pace at the end
1470c 6f 66 20 65 61 63 68 20 70 61 67 65 0a 2a 2a 20 of each page.**
1470d 20 20 20 20 32 31 20 20 20 20 20 20 20 31 20 20 21 1
1470e 20 20 20 4d 61 78 20 65 6d 62 65 64 64 65 64 20 Max embedded
1470f 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e payload fraction
14710 0a 2a 2a 20 20 20 20 20 32 32 20 20 20 20 20 20 .** 22
14711 20 31 20 20 20 20 20 4d 69 6e 20 65 6d 62 65 64 1 Min embed
14712 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 ded payload frac
14713 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 33 20 20 tion.** 23
14714 20 20 20 20 20 31 20 20 20 20 20 4d 69 6e 20 6c 1 Min l
14715 65 61 66 20 70 61 79 6c 6f 61 64 20 66 72 61 63 eaf payload frac
14716 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 34 20 20 tion.** 24
14717 20 20 20 20 20 34 20 20 20 20 20 46 69 6c 65 20 4 File
14718 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 0a 2a change counter.*
14719 2a 20 20 20 20 20 32 38 20 20 20 20 20 20 20 34 * 28 4
1471a 20 20 20 20 20 52 65 73 65 72 76 65 64 20 66 6f Reserved fo
1471b 72 20 66 75 74 75 72 65 20 75 73 65 0a 2a 2a 20 r future use.**
1471c 20 20 20 20 33 32 20 20 20 20 20 20 20 34 20 20 32 4
1471d 20 20 20 46 69 72 73 74 20 66 72 65 65 6c 69 73 First freelis
1471e 74 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 33 36 t page.** 36
1471f 20 20 20 20 20 20 20 34 20 20 20 20 20 4e 75 6d 4 Num
14720 62 65 72 20 6f 66 20 66 72 65 65 6c 69 73 74 20 ber of freelist
14721 70 61 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c pages in the fil
14722 65 0a 2a 2a 20 20 20 20 20 34 30 20 20 20 20 20 e.** 40
14723 20 36 30 20 20 20 20 20 31 35 20 34 2d 62 79 74 60 15 4-byt
14724 65 20 6d 65 74 61 20 76 61 6c 75 65 73 20 70 61 e meta values pa
14725 73 73 65 64 20 74 6f 20 68 69 67 68 65 72 20 6c ssed to higher l
14726 61 79 65 72 73 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ayers.**.**
14727 34 30 20 20 20 20 20 20 20 34 20 20 20 20 20 53 40 4 S
14728 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0a 2a 2a 20 chema cookie.**
14729 20 20 20 20 34 34 20 20 20 20 20 20 20 34 20 20 44 4
1472a 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 6f File format o
1472b 66 20 73 63 68 65 6d 61 20 6c 61 79 65 72 0a 2a f schema layer.*
1472c 2a 20 20 20 20 20 34 38 20 20 20 20 20 20 20 34 * 48 4
1472d 20 20 20 20 20 53 69 7a 65 20 6f 66 20 70 61 67 Size of pag
1472e 65 20 63 61 63 68 65 0a 2a 2a 20 20 20 20 20 35 e cache.** 5
1472f 32 20 20 20 20 20 20 20 34 20 20 20 20 20 4c 61 2 4 La
14730 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 20 rgest root-page
14731 28 61 75 74 6f 2f 69 6e 63 72 5f 76 61 63 75 75 (auto/incr_vacuu
14732 6d 29 0a 2a 2a 20 20 20 20 20 35 36 20 20 20 20 m).** 56
14733 20 20 20 34 20 20 20 20 20 31 3d 55 54 46 2d 38 4 1=UTF-8
14734 20 32 3d 55 54 46 31 36 6c 65 20 33 3d 55 54 46 2=UTF16le 3=UTF
14735 31 36 62 65 0a 2a 2a 20 20 20 20 20 36 30 20 20 16be.** 60
14736 20 20 20 20 20 34 20 20 20 20 20 55 73 65 72 20 4 User
14737 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 36 version.** 6
14738 34 20 20 20 20 20 20 20 34 20 20 20 20 20 49 6e 4 4 In
14739 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d cremental vacuum
1473a 20 6d 6f 64 65 0a 2a 2a 20 20 20 20 20 36 38 20 mode.** 68
1473b 20 20 20 20 20 20 34 20 20 20 20 20 75 6e 75 73 4 unus
1473c 65 64 0a 2a 2a 20 20 20 20 20 37 32 20 20 20 20 ed.** 72
1473d 20 20 20 34 20 20 20 20 20 75 6e 75 73 65 64 0a 4 unused.
1473e 2a 2a 20 20 20 20 20 37 36 20 20 20 20 20 20 20 ** 76
1473f 34 20 20 20 20 20 75 6e 75 73 65 64 0a 2a 2a 0a 4 unused.**.
14740 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 69 6e ** All of the in
14741 74 65 67 65 72 20 76 61 6c 75 65 73 20 61 72 65 teger values are
14742 20 62 69 67 2d 65 6e 64 69 61 6e 20 28 6d 6f 73 big-endian (mos
14743 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 62 79 t significant by
14744 74 65 20 66 69 72 73 74 29 2e 0a 2a 2a 0a 2a 2a te first)..**.**
14745 20 54 68 65 20 66 69 6c 65 20 63 68 61 6e 67 65 The file change
14746 20 63 6f 75 6e 74 65 72 20 69 73 20 69 6e 63 72 counter is incr
14747 65 6d 65 6e 74 65 64 20 77 68 65 6e 20 74 68 65 emented when the
14748 20 64 61 74 61 62 61 73 65 20 69 73 20 63 68 61 database is cha
14749 6e 67 65 64 0a 2a 2a 20 54 68 69 73 20 63 6f 75 nged.** This cou
1474a 6e 74 65 72 20 61 6c 6c 6f 77 73 20 6f 74 68 65 nter allows othe
1474b 72 20 70 72 6f 63 65 73 73 65 73 20 74 6f 20 6b r processes to k
1474c 6e 6f 77 20 77 68 65 6e 20 74 68 65 20 66 69 6c now when the fil
1474d 65 20 68 61 73 20 63 68 61 6e 67 65 64 0a 2a 2a e has changed.**
1474e 20 61 6e 64 20 74 68 75 73 20 77 68 65 6e 20 74 and thus when t
1474f 68 65 79 20 6e 65 65 64 20 74 6f 20 66 6c 75 73 hey need to flus
14750 68 20 74 68 65 69 72 20 63 61 63 68 65 2e 0a 2a h their cache..*
14751 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 20 65 6d 62 *.** The max emb
14752 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 edded payload fr
14753 61 63 74 69 6f 6e 20 69 73 20 74 68 65 20 61 6d action is the am
14754 6f 75 6e 74 20 6f 66 20 74 68 65 20 74 6f 74 61 ount of the tota
14755 6c 20 75 73 61 62 6c 65 0a 2a 2a 20 73 70 61 63 l usable.** spac
14756 65 20 69 6e 20 61 20 70 61 67 65 20 74 68 61 74 e in a page that
14757 20 63 61 6e 20 62 65 20 63 6f 6e 73 75 6d 65 64 can be consumed
14758 20 62 79 20 61 20 73 69 6e 67 6c 65 20 63 65 6c by a single cel
14759 6c 20 66 6f 72 20 73 74 61 6e 64 61 72 64 0a 2a l for standard.*
1475a 2a 20 42 2d 74 72 65 65 20 28 6e 6f 6e 2d 4c 45 * B-tree (non-LE
1475b 41 46 44 41 54 41 29 20 74 61 62 6c 65 73 2e 20 AFDATA) tables.
1475c 20 41 20 76 61 6c 75 65 20 6f 66 20 32 35 35 20 A value of 255
1475d 6d 65 61 6e 73 20 31 30 30 25 2e 20 20 54 68 65 means 100%. The
1475e 20 64 65 66 61 75 6c 74 0a 2a 2a 20 69 73 20 74 default.** is t
1475f 6f 20 6c 69 6d 69 74 20 74 68 65 20 6d 61 78 69 o limit the maxi
14760 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 73 6f mum cell size so
14761 20 74 68 61 74 20 61 74 20 6c 65 61 73 74 20 34 that at least 4
14762 20 63 65 6c 6c 73 20 77 69 6c 6c 20 66 69 74 0a cells will fit.
14763 2a 2a 20 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 ** on one page.
14764 20 54 68 75 73 20 74 68 65 20 64 65 66 61 75 6c Thus the defaul
14765 74 20 6d 61 78 20 65 6d 62 65 64 64 65 64 20 70 t max embedded p
14766 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 ayload fraction
14767 69 73 20 36 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 is 64..**.** If
14768 74 68 65 20 70 61 79 6c 6f 61 64 20 66 6f 72 20 the payload for
14769 61 20 63 65 6c 6c 20 69 73 20 6c 61 72 67 65 72 a cell is larger
1476a 20 74 68 61 6e 20 74 68 65 20 6d 61 78 20 70 61 than the max pa
1476b 79 6c 6f 61 64 2c 20 74 68 65 6e 20 65 78 74 72 yload, then extr
1476c 61 0a 2a 2a 20 70 61 79 6c 6f 61 64 20 69 73 20 a.** payload is
1476d 73 70 69 6c 6c 65 64 20 74 6f 20 6f 76 65 72 66 spilled to overf
1476e 6c 6f 77 20 70 61 67 65 73 2e 20 20 4f 6e 63 65 low pages. Once
1476f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
14770 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 0a e is allocated,.
14771 2a 2a 20 61 73 20 6d 61 6e 79 20 62 79 74 65 73 ** as many bytes
14772 20 61 73 20 70 6f 73 73 69 62 6c 65 20 61 72 65 as possible are
14773 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 65 20 moved into the
14774 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 77 overflow pages w
14775 69 74 68 6f 75 74 20 6c 65 74 74 69 6e 67 0a 2a ithout letting.*
14776 2a 20 74 68 65 20 63 65 6c 6c 20 73 69 7a 65 20 * the cell size
14777 64 72 6f 70 20 62 65 6c 6f 77 20 74 68 65 20 6d drop below the m
14778 69 6e 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c in embedded payl
14779 6f 61 64 20 66 72 61 63 74 69 6f 6e 2e 0a 2a 2a oad fraction..**
1477a 0a 2a 2a 20 54 68 65 20 6d 69 6e 20 6c 65 61 66 .** The min leaf
1477b 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f payload fractio
1477c 6e 20 69 73 20 6c 69 6b 65 20 74 68 65 20 6d 69 n is like the mi
1477d 6e 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f n embedded paylo
1477e 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 65 ad fraction.** e
1477f 78 63 65 70 74 20 74 68 61 74 20 69 74 20 61 70 xcept that it ap
14780 70 6c 69 65 73 20 74 6f 20 6c 65 61 66 20 6e 6f plies to leaf no
14781 64 65 73 20 69 6e 20 61 20 4c 45 41 46 44 41 54 des in a LEAFDAT
14782 41 20 74 72 65 65 2e 20 20 54 68 65 20 6d 61 78 A tree. The max
14783 69 6d 75 6d 0a 2a 2a 20 70 61 79 6c 6f 61 64 20 imum.** payload
14784 66 72 61 63 74 69 6f 6e 20 66 6f 72 20 61 20 4c fraction for a L
14785 45 41 46 44 41 54 41 20 74 72 65 65 20 69 73 20 EAFDATA tree is
14786 61 6c 77 61 79 73 20 31 30 30 25 20 28 6f 72 20 always 100% (or
14787 32 35 35 29 20 61 6e 64 20 69 74 0a 2a 2a 20 6e 255) and it.** n
14788 6f 74 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 ot specified in
14789 74 68 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a the header..**.*
1478a 2a 20 45 61 63 68 20 62 74 72 65 65 20 70 61 67 * Each btree pag
1478b 65 73 20 69 73 20 64 69 76 69 64 65 64 20 69 6e es is divided in
1478c 74 6f 20 74 68 72 65 65 20 73 65 63 74 69 6f 6e to three section
1478d 73 3a 20 20 54 68 65 20 68 65 61 64 65 72 2c 20 s: The header,
1478e 74 68 65 0a 2a 2a 20 63 65 6c 6c 20 70 6f 69 6e the.** cell poin
1478f 74 65 72 20 61 72 72 61 79 2c 20 61 6e 64 20 74 ter array, and t
14790 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
14791 61 72 65 61 2e 20 20 50 61 67 65 20 31 20 61 6c area. Page 1 al
14792 73 6f 20 68 61 73 20 61 20 31 30 30 2d 62 79 74 so has a 100-byt
14793 65 0a 2a 2a 20 66 69 6c 65 20 68 65 61 64 65 72 e.** file header
14794 20 74 68 61 74 20 6f 63 63 75 72 73 20 62 65 66 that occurs bef
14795 6f 72 65 20 74 68 65 20 70 61 67 65 20 68 65 61 ore the page hea
14796 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 der..**.**
14797 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |---------------
14798 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 66 69 6c -|.** | fil
14799 65 20 68 65 61 64 65 72 20 20 20 20 7c 20 20 20 e header |
1479a 31 30 30 20 62 79 74 65 73 2e 20 20 50 61 67 65 100 bytes. Page
1479b 20 31 20 6f 6e 6c 79 2e 0a 2a 2a 20 20 20 20 20 1 only..**
1479c 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |--------------
1479d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 70 61 --|.** | pa
1479e 67 65 20 68 65 61 64 65 72 20 20 20 20 7c 20 20 ge header |
1479f 20 38 20 62 79 74 65 73 20 66 6f 72 20 6c 65 61 8 bytes for lea
147a0 76 65 73 2e 20 20 31 32 20 62 79 74 65 73 20 66 ves. 12 bytes f
147a1 6f 72 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 or interior node
147a2 73 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d s.** |-----
147a3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 -----------|.**
147a4 20 20 20 20 20 7c 20 63 65 6c 6c 20 70 6f 69 6e | cell poin
147a5 74 65 72 20 20 20 7c 20 20 20 7c 20 20 32 20 62 ter | | 2 b
147a6 79 74 65 73 20 70 65 72 20 63 65 6c 6c 2e 20 20 ytes per cell.
147a7 53 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a 2a 2a Sorted order..**
147a8 20 20 20 20 20 20 7c 20 61 72 72 61 79 20 20 20 | array
147a9 20 20 20 20 20 20 20 7c 20 20 20 7c 20 20 47 72 | | Gr
147aa 6f 77 73 20 64 6f 77 6e 77 61 72 64 0a 2a 2a 20 ows downward.**
147ab 20 20 20 20 20 7c 20 20 20 20 20 20 20 20 20 20 |
147ac 20 20 20 20 20 20 7c 20 20 20 76 0a 2a 2a 20 20 | v.**
147ad 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |-----------
147ae 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c -----|.** |
147af 20 75 6e 61 6c 6c 6f 63 61 74 65 64 20 20 20 20 unallocated
147b0 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 73 70 61 63 |.** | spac
147b1 65 20 20 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20 e |.**
147b2 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |----------
147b3 2d 2d 2d 2d 2d 2d 7c 20 20 20 5e 20 20 47 72 6f ------| ^ Gro
147b4 77 73 20 75 70 77 61 72 64 73 0a 2a 2a 20 20 20 ws upwards.**
147b5 20 20 20 7c 20 63 65 6c 6c 20 63 6f 6e 74 65 6e | cell conten
147b6 74 20 20 20 7c 20 20 20 7c 20 20 41 72 62 69 74 t | | Arbit
147b7 72 61 72 79 20 6f 72 64 65 72 20 69 6e 74 65 72 rary order inter
147b8 73 70 65 72 73 65 64 20 77 69 74 68 20 66 72 65 spersed with fre
147b9 65 62 6c 6f 63 6b 73 2e 0a 2a 2a 20 20 20 20 20 eblocks..**
147ba 20 7c 20 61 72 65 61 20 20 20 20 20 20 20 20 20 | area
147bb 20 20 7c 20 20 20 7c 20 20 61 6e 64 20 66 72 65 | | and fre
147bc 65 20 73 70 61 63 65 20 66 72 61 67 6d 65 6e 74 e space fragment
147bd 73 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d s..** |----
147be 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a ------------|.**
147bf 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 68 65 61 .** The page hea
147c0 64 65 72 73 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 ders looks like
147c1 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 this:.**.** OF
147c2 46 53 45 54 20 20 20 53 49 5a 45 20 20 20 20 20 FSET SIZE
147c3 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 DESCRIPTION.**
147c4 20 20 20 20 30 20 20 20 20 20 20 20 31 20 20 20 0 1
147c5 20 20 20 46 6c 61 67 73 2e 20 31 3a 20 69 6e 74 Flags. 1: int
147c6 6b 65 79 2c 20 32 3a 20 7a 65 72 6f 64 61 74 61 key, 2: zerodata
147c7 2c 20 34 3a 20 6c 65 61 66 64 61 74 61 2c 20 38 , 4: leafdata, 8
147c8 3a 20 6c 65 61 66 0a 2a 2a 20 20 20 20 20 20 31 : leaf.** 1
147c9 20 20 20 20 20 20 20 32 20 20 20 20 20 20 62 79 2 by
147ca 74 65 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65 te offset to the
147cb 20 66 69 72 73 74 20 66 72 65 65 62 6c 6f 63 6b first freeblock
147cc 0a 2a 2a 20 20 20 20 20 20 33 20 20 20 20 20 20 .** 3
147cd 20 32 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 2 number o
147ce 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 69 73 20 f cells on this
147cf 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 35 20 20 page.** 5
147d0 20 20 20 20 20 32 20 20 20 20 20 20 66 69 72 73 2 firs
147d1 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 63 65 t byte of the ce
147d2 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 0a ll content area.
147d3 2a 2a 20 20 20 20 20 20 37 20 20 20 20 20 20 20 ** 7
147d4 31 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 1 number of
147d5 20 66 72 61 67 6d 65 6e 74 65 64 20 66 72 65 65 fragmented free
147d6 20 62 79 74 65 73 0a 2a 2a 20 20 20 20 20 20 38 bytes.** 8
147d7 20 20 20 20 20 20 20 34 20 20 20 20 20 20 52 69 4 Ri
147d8 67 68 74 20 63 68 69 6c 64 20 28 74 68 65 20 50 ght child (the P
147d9 74 72 28 4e 29 20 76 61 6c 75 65 29 2e 20 20 4f tr(N) value). O
147da 6d 69 74 74 65 64 20 6f 6e 20 6c 65 61 76 65 73 mitted on leaves
147db 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 ..**.** The flag
147dc 73 20 64 65 66 69 6e 65 20 74 68 65 20 66 6f 72 s define the for
147dd 6d 61 74 20 6f 66 20 74 68 69 73 20 62 74 72 65 mat of this btre
147de 65 20 70 61 67 65 2e 20 20 54 68 65 20 6c 65 61 e page. The lea
147df 66 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 f flag means tha
147e0 74 0a 2a 2a 20 74 68 69 73 20 70 61 67 65 20 68 t.** this page h
147e1 61 73 20 6e 6f 20 63 68 69 6c 64 72 65 6e 2e 20 as no children.
147e2 20 54 68 65 20 7a 65 72 6f 64 61 74 61 20 66 6c The zerodata fl
147e3 61 67 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 ag means that th
147e4 69 73 20 70 61 67 65 20 63 61 72 72 69 65 73 0a is page carries.
147e5 2a 2a 20 6f 6e 6c 79 20 6b 65 79 73 20 61 6e 64 ** only keys and
147e6 20 6e 6f 20 64 61 74 61 2e 20 20 54 68 65 20 69 no data. The i
147e7 6e 74 6b 65 79 20 66 6c 61 67 20 6d 65 61 6e 73 ntkey flag means
147e8 20 74 68 61 74 20 74 68 65 20 6b 65 79 20 69 73 that the key is
147e9 20 61 20 69 6e 74 65 67 65 72 0a 2a 2a 20 77 68 a integer.** wh
147ea 69 63 68 20 69 73 20 73 74 6f 72 65 64 20 69 6e ich is stored in
147eb 20 74 68 65 20 6b 65 79 20 73 69 7a 65 20 65 6e the key size en
147ec 74 72 79 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 try of the cell
147ed 68 65 61 64 65 72 20 72 61 74 68 65 72 20 74 68 header rather th
147ee 61 6e 20 69 6e 0a 2a 2a 20 74 68 65 20 70 61 79 an in.** the pay
147ef 6c 6f 61 64 20 61 72 65 61 2e 0a 2a 2a 0a 2a 2a load area..**.**
147f0 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 The cell pointe
147f1 72 20 61 72 72 61 79 20 62 65 67 69 6e 73 20 6f r array begins o
147f2 6e 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 n the first byte
147f3 20 61 66 74 65 72 20 74 68 65 20 70 61 67 65 20 after the page
147f4 68 65 61 64 65 72 2e 0a 2a 2a 20 54 68 65 20 63 header..** The c
147f5 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 ell pointer arra
147f6 79 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72 6f 20 y contains zero
147f7 6f 72 20 6d 6f 72 65 20 32 2d 62 79 74 65 20 6e or more 2-byte n
147f8 75 6d 62 65 72 73 20 77 68 69 63 68 20 61 72 65 umbers which are
147f9 0a 2a 2a 20 6f 66 66 73 65 74 73 20 66 72 6f 6d .** offsets from
147fa 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
147fb 66 20 74 68 65 20 70 61 67 65 20 74 6f 20 74 68 f the page to th
147fc 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 e cell content i
147fd 6e 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 63 6f n the cell.** co
147fe 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 54 68 65 ntent area. The
147ff 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 73 20 6f cell pointers o
14800 63 63 75 72 20 69 6e 20 73 6f 72 74 65 64 20 6f ccur in sorted o
14801 72 64 65 72 2e 20 20 54 68 65 20 73 79 73 74 65 rder. The syste
14802 6d 20 73 74 72 69 76 65 73 0a 2a 2a 20 74 6f 20 m strives.** to
14803 6b 65 65 70 20 66 72 65 65 20 73 70 61 63 65 20 keep free space
14804 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 63 after the last c
14805 65 6c 6c 20 70 6f 69 6e 74 65 72 20 73 6f 20 74 ell pointer so t
14806 68 61 74 20 6e 65 77 20 63 65 6c 6c 73 20 63 61 hat new cells ca
14807 6e 0a 2a 2a 20 62 65 20 65 61 73 69 6c 79 20 61 n.** be easily a
14808 64 64 65 64 20 77 69 74 68 6f 75 74 20 68 61 76 dded without hav
14809 69 6e 67 20 74 6f 20 64 65 66 72 61 67 6d 65 6e ing to defragmen
1480a 74 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a t the page..**.*
1480b 2a 20 43 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 * Cell content i
1480c 73 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 s stored at the
1480d 76 65 72 79 20 65 6e 64 20 6f 66 20 74 68 65 20 very end of the
1480e 70 61 67 65 20 61 6e 64 20 67 72 6f 77 73 20 74 page and grows t
1480f 6f 77 61 72 64 20 74 68 65 0a 2a 2a 20 62 65 67 oward the.** beg
14810 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 inning of the pa
14811 67 65 2e 0a 2a 2a 0a 2a 2a 20 55 6e 75 73 65 64 ge..**.** Unused
14812 20 73 70 61 63 65 20 77 69 74 68 69 6e 20 74 68 space within th
14813 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 e cell content a
14814 72 65 61 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 rea is collected
14815 20 69 6e 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c into a linked l
14816 69 73 74 20 6f 66 0a 2a 2a 20 66 72 65 65 62 6c ist of.** freebl
14817 6f 63 6b 73 2e 20 20 45 61 63 68 20 66 72 65 65 ocks. Each free
14818 62 6c 6f 63 6b 20 69 73 20 61 74 20 6c 65 61 73 block is at leas
14819 74 20 34 20 62 79 74 65 73 20 69 6e 20 73 69 7a t 4 bytes in siz
1481a 65 2e 20 20 54 68 65 20 62 79 74 65 20 6f 66 66 e. The byte off
1481b 73 65 74 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69 set.** to the fi
1481c 72 73 74 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 rst freeblock is
1481d 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 68 65 given in the he
1481e 61 64 65 72 2e 20 20 46 72 65 65 62 6c 6f 63 6b ader. Freeblock
1481f 73 20 6f 63 63 75 72 20 69 6e 0a 2a 2a 20 69 6e s occur in.** in
14820 63 72 65 61 73 69 6e 67 20 6f 72 64 65 72 2e 20 creasing order.
14821 20 42 65 63 61 75 73 65 20 61 20 66 72 65 65 62 Because a freeb
14822 6c 6f 63 6b 20 6d 75 73 74 20 62 65 20 61 74 20 lock must be at
14823 6c 65 61 73 74 20 34 20 62 79 74 65 73 20 69 6e least 4 bytes in
14824 20 73 69 7a 65 2c 0a 2a 2a 20 61 6e 79 20 67 72 size,.** any gr
14825 6f 75 70 20 6f 66 20 33 20 6f 72 20 66 65 77 65 oup of 3 or fewe
14826 72 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 69 r unused bytes i
14827 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 n the cell conte
14828 6e 74 20 61 72 65 61 20 63 61 6e 6e 6f 74 0a 2a nt area cannot.*
14829 2a 20 65 78 69 73 74 20 6f 6e 20 74 68 65 20 66 * exist on the f
1482a 72 65 65 62 6c 6f 63 6b 20 63 68 61 69 6e 2e 20 reeblock chain.
1482b 20 41 20 67 72 6f 75 70 20 6f 66 20 33 20 6f 72 A group of 3 or
1482c 20 66 65 77 65 72 20 66 72 65 65 20 62 79 74 65 fewer free byte
1482d 73 20 69 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 s is called.** a
1482e 20 66 72 61 67 6d 65 6e 74 2e 20 20 54 68 65 20 fragment. The
1482f 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
14830 62 79 74 65 73 20 69 6e 20 61 6c 6c 20 66 72 61 bytes in all fra
14831 67 6d 65 6e 74 73 20 69 73 20 72 65 63 6f 72 64 gments is record
14832 65 64 2e 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 ed..** in the pa
14833 67 65 20 68 65 61 64 65 72 20 61 74 20 6f 66 66 ge header at off
14834 73 65 74 20 37 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 set 7..**.**
14835 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 SIZE DESCRIPT
14836 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 ION.** 2
14837 20 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66 Byte offset of
14838 20 74 68 65 20 6e 65 78 74 20 66 72 65 65 62 6c the next freebl
14839 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 ock.** 2
1483a 20 20 42 79 74 65 73 20 69 6e 20 74 68 69 73 20 Bytes in this
1483b 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 0a 2a 2a 20 freeblock.**.**
1483c 43 65 6c 6c 73 20 61 72 65 20 6f 66 20 76 61 72 Cells are of var
1483d 69 61 62 6c 65 20 6c 65 6e 67 74 68 2e 20 20 43 iable length. C
1483e 65 6c 6c 73 20 61 72 65 20 73 74 6f 72 65 64 20 ells are stored
1483f 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 in the cell cont
14840 65 6e 74 20 61 72 65 61 20 61 74 0a 2a 2a 20 74 ent area at.** t
14841 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61 he end of the pa
14842 67 65 2e 20 20 50 6f 69 6e 74 65 72 73 20 74 6f ge. Pointers to
14843 20 74 68 65 20 63 65 6c 6c 73 20 61 72 65 20 69 the cells are i
14844 6e 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 n the cell point
14845 65 72 20 61 72 72 61 79 0a 2a 2a 20 74 68 61 74 er array.** that
14846 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c immediately fol
14847 6c 6f 77 73 20 74 68 65 20 70 61 67 65 20 68 65 lows the page he
14848 61 64 65 72 2e 20 20 43 65 6c 6c 73 20 69 73 20 ader. Cells is
14849 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 0a not necessarily.
1484a 2a 2a 20 63 6f 6e 74 69 67 75 6f 75 73 20 6f 72 ** contiguous or
1484b 20 69 6e 20 6f 72 64 65 72 2c 20 62 75 74 20 63 in order, but c
1484c 65 6c 6c 20 70 6f 69 6e 74 65 72 73 20 61 72 65 ell pointers are
1484d 20 63 6f 6e 74 69 67 75 6f 75 73 20 61 6e 64 20 contiguous and
1484e 69 6e 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 in order..**.**
1484f 43 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6d 61 6b Cell content mak
14850 65 73 20 75 73 65 20 6f 66 20 76 61 72 69 61 62 es use of variab
14851 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 le length intege
14852 72 73 2e 20 20 41 20 76 61 72 69 61 62 6c 65 0a rs. A variable.
14853 2a 2a 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 ** length intege
14854 72 20 69 73 20 31 20 74 6f 20 39 20 62 79 74 65 r is 1 to 9 byte
14855 73 20 77 68 65 72 65 20 74 68 65 20 6c 6f 77 65 s where the lowe
14856 72 20 37 20 62 69 74 73 20 6f 66 20 65 61 63 68 r 7 bits of each
14857 20 0a 2a 2a 20 62 79 74 65 20 61 72 65 20 75 73 .** byte are us
14858 65 64 2e 20 20 54 68 65 20 69 6e 74 65 67 65 72 ed. The integer
14859 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 6c 6c consists of all
1485a 20 62 79 74 65 73 20 74 68 61 74 20 68 61 76 65 bytes that have
1485b 20 62 69 74 20 38 20 73 65 74 20 61 6e 64 0a 2a bit 8 set and.*
1485c 2a 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 * the first byte
1485d 20 77 69 74 68 20 62 69 74 20 38 20 63 6c 65 61 with bit 8 clea
1485e 72 2e 20 20 54 68 65 20 6d 6f 73 74 20 73 69 67 r. The most sig
1485f 6e 69 66 69 63 61 6e 74 20 62 79 74 65 20 6f 66 nificant byte of
14860 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 the integer.**
14861 61 70 70 65 61 72 73 20 66 69 72 73 74 2e 20 20 appears first.
14862 41 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 A variable-lengt
14863 68 20 69 6e 74 65 67 65 72 20 6d 61 79 20 6e 6f h integer may no
14864 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 39 t be more than 9
14865 20 62 79 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 bytes long..**
14866 41 73 20 61 20 73 70 65 63 69 61 6c 20 63 61 73 As a special cas
14867 65 2c 20 61 6c 6c 20 38 20 62 79 74 65 73 20 6f e, all 8 bytes o
14868 66 20 74 68 65 20 39 74 68 20 62 79 74 65 20 61 f the 9th byte a
14869 72 65 20 75 73 65 64 20 61 73 20 64 61 74 61 2e re used as data.
1486a 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 This.** allows
1486b 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 a 64-bit intege
1486c 72 20 74 6f 20 62 65 20 65 6e 63 6f 64 65 64 20 r to be encoded
1486d 69 6e 20 39 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a in 9 bytes..**.*
1486e 2a 20 20 20 20 30 78 30 30 20 20 20 20 20 20 20 * 0x00
1486f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 b
14870 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30 ecomes 0x000000
14871 30 30 0a 2a 2a 20 20 20 20 30 78 37 66 20 20 20 00.** 0x7f
14872 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14873 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 becomes 0x00
14874 30 30 30 30 37 66 0a 2a 2a 20 20 20 20 30 78 38 00007f.** 0x8
14875 31 20 30 78 30 30 20 20 20 20 20 20 20 20 20 20 1 0x00
14876 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 becomes
14877 30 78 30 30 30 30 30 30 38 30 0a 2a 2a 20 20 20 0x00000080.**
14878 20 30 78 38 32 20 30 78 30 30 20 20 20 20 20 20 0x82 0x00
14879 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d becom
1487a 65 73 20 20 30 78 30 30 30 30 30 31 30 30 0a 2a es 0x00000100.*
1487b 2a 20 20 20 20 30 78 38 30 20 30 78 37 66 20 20 * 0x80 0x7f
1487c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 b
1487d 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30 ecomes 0x000000
1487e 37 66 0a 2a 2a 20 20 20 20 30 78 38 61 20 30 78 7f.** 0x8a 0x
1487f 39 31 20 30 78 64 31 20 30 78 61 63 20 30 78 37 91 0xd1 0xac 0x7
14880 38 20 20 62 65 63 6f 6d 65 73 20 20 30 78 31 32 8 becomes 0x12
14881 33 34 35 36 37 38 0a 2a 2a 20 20 20 20 30 78 38 345678.** 0x8
14882 31 20 30 78 38 31 20 30 78 38 31 20 30 78 38 31 1 0x81 0x81 0x81
14883 20 30 78 30 31 20 20 62 65 63 6f 6d 65 73 20 20 0x01 becomes
14884 30 78 31 30 32 30 34 30 38 31 0a 2a 2a 0a 2a 2a 0x10204081.**.**
14885 20 56 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 Variable length
14886 20 69 6e 74 65 67 65 72 73 20 61 72 65 20 75 73 integers are us
14887 65 64 20 66 6f 72 20 72 6f 77 69 64 73 20 61 6e ed for rowids an
14888 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e 75 d to hold the nu
14889 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 mber of.** bytes
1488a 20 6f 66 20 6b 65 79 20 61 6e 64 20 64 61 74 61 of key and data
1488b 20 69 6e 20 61 20 62 74 72 65 65 20 63 65 6c 6c in a btree cell
1488c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 ..**.** The cont
1488d 65 6e 74 20 6f 66 20 61 20 63 65 6c 6c 20 6c 6f ent of a cell lo
1488e 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a oks like this:.*
1488f 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20 *.** SIZE
14890 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 DESCRIPTION.**
14891 20 20 20 20 34 20 20 20 20 20 50 61 67 65 20 6e 4 Page n
14892 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6c 65 66 umber of the lef
14893 74 20 63 68 69 6c 64 2e 20 4f 6d 69 74 74 65 64 t child. Omitted
14894 20 69 66 20 6c 65 61 66 20 66 6c 61 67 20 69 73 if leaf flag is
14895 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 76 61 72 set..** var
14896 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 Number of by
14897 74 65 73 20 6f 66 20 64 61 74 61 2e 20 4f 6d 69 tes of data. Omi
14898 74 74 65 64 20 69 66 20 74 68 65 20 7a 65 72 6f tted if the zero
14899 64 61 74 61 20 66 6c 61 67 20 69 73 20 73 65 74 data flag is set
1489a 2e 0a 2a 2a 20 20 20 20 20 76 61 72 20 20 20 20 ..** var
1489b 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1489c 6f 66 20 6b 65 79 2e 20 4f 72 20 74 68 65 20 6b of key. Or the k
1489d 65 79 20 69 74 73 65 6c 66 20 69 66 20 69 6e 74 ey itself if int
1489e 6b 65 79 20 66 6c 61 67 20 69 73 20 73 65 74 2e key flag is set.
1489f 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 20 20 50 .** * P
148a0 61 79 6c 6f 61 64 0a 2a 2a 20 20 20 20 20 20 34 ayload.** 4
148a1 20 20 20 20 20 46 69 72 73 74 20 70 61 67 65 20 First page
148a2 6f 66 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 of the overflow
148a3 63 68 61 69 6e 2e 20 20 4f 6d 69 74 74 65 64 20 chain. Omitted
148a4 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a if no overflow.*
148a5 2a 0a 2a 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 *.** Overflow pa
148a6 67 65 73 20 66 6f 72 6d 20 61 20 6c 69 6e 6b 65 ges form a linke
148a7 64 20 6c 69 73 74 2e 20 20 45 61 63 68 20 70 61 d list. Each pa
148a8 67 65 20 65 78 63 65 70 74 20 74 68 65 20 6c 61 ge except the la
148a9 73 74 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 st is completely
148aa 0a 2a 2a 20 66 69 6c 6c 65 64 20 77 69 74 68 20 .** filled with
148ab 64 61 74 61 20 28 70 61 67 65 73 69 7a 65 20 2d data (pagesize -
148ac 20 34 20 62 79 74 65 73 29 2e 20 20 54 68 65 20 4 bytes). The
148ad 6c 61 73 74 20 70 61 67 65 20 63 61 6e 20 68 61 last page can ha
148ae 76 65 20 61 73 20 6c 69 74 74 6c 65 0a 2a 2a 20 ve as little.**
148af 61 73 20 31 20 62 79 74 65 20 6f 66 20 64 61 74 as 1 byte of dat
148b0 61 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 a..**.** SIZE
148b1 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a DESCRIPTION.
148b2 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 50 61 ** 4 Pa
148b3 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 78 ge number of nex
148b4 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 0a t overflow page.
148b5 2a 2a 20 20 20 20 20 20 2a 20 20 20 20 20 44 61 ** * Da
148b6 74 61 0a 2a 2a 0a 2a 2a 20 46 72 65 65 6c 69 73 ta.**.** Freelis
148b7 74 20 70 61 67 65 73 20 63 6f 6d 65 20 69 6e 20 t pages come in
148b8 74 77 6f 20 73 75 62 74 79 70 65 73 3a 20 74 72 two subtypes: tr
148b9 75 6e 6b 20 70 61 67 65 73 20 61 6e 64 20 6c 65 unk pages and le
148ba 61 66 20 70 61 67 65 73 2e 20 20 54 68 65 0a 2a af pages. The.*
148bb 2a 20 66 69 6c 65 20 68 65 61 64 65 72 20 70 6f * file header po
148bc 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 ints to the firs
148bd 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 t in a linked li
148be 73 74 20 6f 66 20 74 72 75 6e 6b 20 70 61 67 65 st of trunk page
148bf 2e 20 20 45 61 63 68 20 74 72 75 6e 6b 0a 2a 2a . Each trunk.**
148c0 20 70 61 67 65 20 70 6f 69 6e 74 73 20 74 6f 20 page points to
148c1 6d 75 6c 74 69 70 6c 65 20 6c 65 61 66 20 70 61 multiple leaf pa
148c2 67 65 73 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e ges. The conten
148c3 74 20 6f 66 20 61 20 6c 65 61 66 20 70 61 67 65 t of a leaf page
148c4 20 69 73 0a 2a 2a 20 75 6e 73 70 65 63 69 66 69 is.** unspecifi
148c5 65 64 2e 20 20 41 20 74 72 75 6e 6b 20 70 61 67 ed. A trunk pag
148c6 65 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 e looks like thi
148c7 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 s:.**.** SIZE
148c8 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a DESCRIPTION.
148c9 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 50 61 ** 4 Pa
148ca 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 78 ge number of nex
148cb 74 20 74 72 75 6e 6b 20 70 61 67 65 0a 2a 2a 20 t trunk page.**
148cc 20 20 20 20 20 34 20 20 20 20 20 4e 75 6d 62 65 4 Numbe
148cd 72 20 6f 66 20 6c 65 61 66 20 70 6f 69 6e 74 65 r of leaf pointe
148ce 72 73 20 6f 6e 20 74 68 69 73 20 70 61 67 65 0a rs on this page.
148cf 2a 2a 20 20 20 20 20 20 2a 20 20 20 20 20 7a 65 ** * ze
148d0 72 6f 20 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 ro or more pages
148d1 20 6e 75 6d 62 65 72 73 20 6f 66 20 6c 65 61 76 numbers of leav
148d2 65 73 0a 2a 2f 0a 0a 0a 2f 2a 20 54 68 65 20 66 es.*/.../* The f
148d3 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 69 ollowing value i
148d4 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 s the maximum ce
148d5 6c 6c 20 73 69 7a 65 20 61 73 73 75 6d 69 6e 67 ll size assuming
148d6 20 61 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 0a a maximum page.
148d7 2a 2a 20 73 69 7a 65 20 67 69 76 65 20 61 62 6f ** size give abo
148d8 76 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d ve..*/.#define M
148d9 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 X_CELL_SIZE(pBt)
148da 20 20 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 (pBt->pageSize
148db 2d 38 29 0a 0a 2f 2a 20 54 68 65 20 6d 61 78 69 -8)../* The maxi
148dc 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 mum number of ce
148dd 6c 6c 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 lls on a single
148de 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 page of the data
148df 62 61 73 65 2e 20 20 54 68 69 73 0a 2a 2a 20 61 base. This.** a
148e0 73 73 75 6d 65 73 20 61 20 6d 69 6e 69 6d 75 6d ssumes a minimum
148e1 20 63 65 6c 6c 20 73 69 7a 65 20 6f 66 20 36 20 cell size of 6
148e2 62 79 74 65 73 20 20 28 34 20 62 79 74 65 73 20 bytes (4 bytes
148e3 66 6f 72 20 74 68 65 20 63 65 6c 6c 20 69 74 73 for the cell its
148e4 65 6c 66 0a 2a 2a 20 70 6c 75 73 20 32 20 62 79 elf.** plus 2 by
148e5 74 65 73 20 66 6f 72 20 74 68 65 20 69 6e 64 65 tes for the inde
148e6 78 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e x to the cell in
148e7 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 the page header
148e8 29 2e 20 20 53 75 63 68 0a 2a 2a 20 73 6d 61 6c ). Such.** smal
148e9 6c 20 63 65 6c 6c 73 20 77 69 6c 6c 20 62 65 20 l cells will be
148ea 72 61 72 65 2c 20 62 75 74 20 74 68 65 79 20 61 rare, but they a
148eb 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a re possible..*/.
148ec 23 64 65 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 28 #define MX_CELL(
148ed 70 42 74 29 20 28 28 70 42 74 2d 3e 70 61 67 65 pBt) ((pBt->page
148ee 53 69 7a 65 2d 38 29 2f 36 29 0a 0a 2f 2a 20 46 Size-8)/6)../* F
148ef 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 orward declarati
148f0 6f 6e 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 ons */.typedef s
148f1 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20 4d 65 truct MemPage Me
148f2 6d 50 61 67 65 3b 0a 74 79 70 65 64 65 66 20 73 mPage;.typedef s
148f3 74 72 75 63 74 20 42 74 4c 6f 63 6b 20 42 74 4c truct BtLock BtL
148f4 6f 63 6b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ock;../*.** This
148f5 20 69 73 20 61 20 6d 61 67 69 63 20 73 74 72 69 is a magic stri
148f6 6e 67 20 74 68 61 74 20 61 70 70 65 61 72 73 20 ng that appears
148f7 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 at the beginning
148f8 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53 51 4c of every.** SQL
148f9 69 74 65 20 64 61 74 61 62 61 73 65 20 69 6e 20 ite database in
148fa 6f 72 64 65 72 20 74 6f 20 69 64 65 6e 74 69 66 order to identif
148fb 79 20 74 68 65 20 66 69 6c 65 20 61 73 20 61 20 y the file as a
148fc 72 65 61 6c 20 64 61 74 61 62 61 73 65 2e 0a 2a real database..*
148fd 2a 0a 2a 2a 20 59 6f 75 20 63 61 6e 20 63 68 61 *.** You can cha
148fe 6e 67 65 20 74 68 69 73 20 76 61 6c 75 65 20 61 nge this value a
148ff 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 62 t compile-time b
14900 79 20 73 70 65 63 69 66 79 69 6e 67 20 61 0a 2a y specifying a.*
14901 2a 20 2d 44 53 51 4c 49 54 45 5f 46 49 4c 45 5f * -DSQLITE_FILE_
14902 48 45 41 44 45 52 3d 22 2e 2e 2e 22 20 6f 6e 20 HEADER="..." on
14903 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d the compiler com
14904 6d 61 6e 64 2d 6c 69 6e 65 2e 20 20 54 68 65 0a mand-line. The.
14905 2a 2a 20 68 65 61 64 65 72 20 6d 75 73 74 20 62 ** header must b
14906 65 20 65 78 61 63 74 6c 79 20 31 36 20 62 79 74 e exactly 16 byt
14907 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 es including the
14908 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 zero-terminator
14909 20 73 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e so.** the strin
1490a 67 20 69 74 73 65 6c 66 20 73 68 6f 75 6c 64 20 g itself should
1490b 62 65 20 31 35 20 63 68 61 72 61 63 74 65 72 73 be 15 characters
1490c 20 6c 6f 6e 67 2e 20 20 49 66 20 79 6f 75 20 63 long. If you c
1490d 68 61 6e 67 65 0a 2a 2a 20 74 68 65 20 68 65 61 hange.** the hea
1490e 64 65 72 2c 20 74 68 65 6e 20 79 6f 75 72 20 63 der, then your c
1490f 75 73 74 6f 6d 20 6c 69 62 72 61 72 79 20 77 69 ustom library wi
14910 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 ll not be able t
14911 6f 20 72 65 61 64 20 0a 2a 2a 20 64 61 74 61 62 o read .** datab
14912 61 73 65 73 20 67 65 6e 65 72 61 74 65 64 20 62 ases generated b
14913 79 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 74 y the standard t
14914 6f 6f 6c 73 20 61 6e 64 20 74 68 65 20 73 74 61 ools and the sta
14915 6e 64 61 72 64 20 74 6f 6f 6c 73 0a 2a 2a 20 77 ndard tools.** w
14916 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 ill not be able
14917 74 6f 20 72 65 61 64 20 64 61 74 61 62 61 73 65 to read database
14918 73 20 63 72 65 61 74 65 64 20 62 79 20 79 6f 75 s created by you
14919 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 61 72 79 r custom library
1491a 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
1491b 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 20 ITE_FILE_HEADER
1491c 2f 2a 20 31 32 33 34 35 36 37 38 39 20 31 32 33 /* 123456789 123
1491d 34 35 36 20 2a 2f 0a 23 20 20 64 65 66 69 6e 65 456 */.# define
1491e 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 SQLITE_FILE_HEA
1491f 44 45 52 20 22 53 51 4c 69 74 65 20 66 6f 72 6d DER "SQLite form
14920 61 74 20 33 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a at 3".#endif../*
14921 0a 2a 2a 20 50 61 67 65 20 74 79 70 65 20 66 6c .** Page type fl
14922 61 67 73 2e 20 20 41 6e 20 4f 52 65 64 20 63 6f ags. An ORed co
14923 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 mbination of the
14924 73 65 20 66 6c 61 67 73 20 61 70 70 65 61 72 20 se flags appear
14925 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 as the.** first
14926 62 79 74 65 20 6f 66 20 6f 6e 2d 64 69 73 6b 20 byte of on-disk
14927 69 6d 61 67 65 20 6f 66 20 65 76 65 72 79 20 42 image of every B
14928 54 72 65 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 64 Tree page..*/.#d
14929 65 66 69 6e 65 20 50 54 46 5f 49 4e 54 4b 45 59 efine PTF_INTKEY
1492a 20 20 20 20 30 78 30 31 0a 23 64 65 66 69 6e 65 0x01.#define
1492b 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 20 30 PTF_ZERODATA 0
1492c 78 30 32 0a 23 64 65 66 69 6e 65 20 50 54 46 5f x02.#define PTF_
1492d 4c 45 41 46 44 41 54 41 20 20 30 78 30 34 0a 23 LEAFDATA 0x04.#
1492e 64 65 66 69 6e 65 20 50 54 46 5f 4c 45 41 46 20 define PTF_LEAF
1492f 20 20 20 20 20 30 78 30 38 0a 0a 2f 2a 0a 2a 2a 0x08../*.**
14930 20 41 73 20 65 61 63 68 20 70 61 67 65 20 6f 66 As each page of
14931 20 74 68 65 20 66 69 6c 65 20 69 73 20 6c 6f 61 the file is loa
14932 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2c ded into memory,
14933 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
14934 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a the following.**
14935 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 70 structure is ap
14936 70 65 6e 64 65 64 20 61 6e 64 20 69 6e 69 74 69 pended and initi
14937 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2e 20 alized to zero.
14938 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 This structure
14939 73 74 6f 72 65 73 0a 2a 2a 20 69 6e 66 6f 72 6d stores.** inform
1493a 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 ation about the
1493b 70 61 67 65 20 74 68 61 74 20 69 73 20 64 65 63 page that is dec
1493c 6f 64 65 64 20 66 72 6f 6d 20 74 68 65 20 72 61 oded from the ra
1493d 77 20 66 69 6c 65 20 70 61 67 65 2e 0a 2a 2a 0a w file page..**.
1493e 2a 2a 20 54 68 65 20 70 50 61 72 65 6e 74 20 66 ** The pParent f
1493f 69 65 6c 64 20 70 6f 69 6e 74 73 20 62 61 63 6b ield points back
14940 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70 to the parent p
14941 61 67 65 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77 age. This allow
14942 73 20 75 73 20 74 6f 0a 2a 2a 20 77 61 6c 6b 20 s us to.** walk
14943 75 70 20 74 68 65 20 42 54 72 65 65 20 66 72 6f up the BTree fro
14944 6d 20 61 6e 79 20 6c 65 61 66 20 74 6f 20 74 68 m any leaf to th
14945 65 20 72 6f 6f 74 2e 20 20 43 61 72 65 20 6d 75 e root. Care mu
14946 73 74 20 62 65 20 74 61 6b 65 6e 20 74 6f 0a 2a st be taken to.*
14947 2a 20 75 6e 72 65 66 28 29 20 74 68 65 20 70 61 * unref() the pa
14948 72 65 6e 74 20 70 61 67 65 20 70 6f 69 6e 74 65 rent page pointe
14949 72 20 77 68 65 6e 20 74 68 69 73 20 70 61 67 65 r when this page
1494a 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 is no longer re
1494b 66 65 72 65 6e 63 65 64 2e 0a 2a 2a 20 54 68 65 ferenced..** The
1494c 20 70 61 67 65 44 65 73 74 72 75 63 74 6f 72 28 pageDestructor(
1494d 29 20 72 6f 75 74 69 6e 65 20 68 61 6e 64 6c 65 ) routine handle
1494e 73 20 74 68 61 74 20 63 68 6f 72 65 2e 0a 2a 2a s that chore..**
1494f 0a 2a 2a 20 41 63 63 65 73 73 20 74 6f 20 61 6c .** Access to al
14950 6c 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73 l fields of this
14951 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63 6f structure is co
14952 6e 74 72 6f 6c 6c 65 64 20 62 79 20 74 68 65 20 ntrolled by the
14953 6d 75 74 65 78 0a 2a 2a 20 73 74 6f 72 65 64 20 mutex.** stored
14954 69 6e 20 4d 65 6d 50 61 67 65 2e 70 42 74 2d 3e in MemPage.pBt->
14955 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 mutex..*/.struct
14956 20 4d 65 6d 50 61 67 65 20 7b 0a 20 20 75 38 20 MemPage {. u8
14957 69 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 isInit;
14958 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 72 65 /* True if pre
14959 76 69 6f 75 73 6c 79 20 69 6e 69 74 69 61 6c 69 viously initiali
1495a 7a 65 64 2e 20 4d 55 53 54 20 42 45 20 46 49 52 zed. MUST BE FIR
1495b 53 54 21 20 2a 2f 0a 20 20 75 38 20 6e 4f 76 65 ST! */. u8 nOve
1495c 72 66 6c 6f 77 3b 20 20 20 20 20 20 20 20 2f 2a rflow; /*
1495d 20 4e 75 6d 62 65 72 20 6f 66 20 6f 76 65 72 66 Number of overf
1495e 6c 6f 77 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 low cell bodies
1495f 69 6e 20 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 in aCell[] */.
14960 75 38 20 69 6e 74 4b 65 79 3b 20 20 20 20 20 20 u8 intKey;
14961 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
14962 69 6e 74 6b 65 79 20 66 6c 61 67 20 69 73 20 73 intkey flag is s
14963 65 74 20 2a 2f 0a 20 20 75 38 20 6c 65 61 66 3b et */. u8 leaf;
14964 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
14965 54 72 75 65 20 69 66 20 6c 65 61 66 20 66 6c 61 True if leaf fla
14966 67 20 69 73 20 73 65 74 20 2a 2f 0a 20 20 75 38 g is set */. u8
14967 20 68 61 73 44 61 74 61 3b 20 20 20 20 20 20 20 hasData;
14968 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
14969 69 73 20 70 61 67 65 20 73 74 6f 72 65 73 20 64 is page stores d
1496a 61 74 61 20 2a 2f 0a 20 20 75 38 20 68 64 72 4f ata */. u8 hdrO
1496b 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a ffset; /*
1496c 20 31 30 30 20 66 6f 72 20 70 61 67 65 20 31 2e 100 for page 1.
1496d 20 20 30 20 6f 74 68 65 72 77 69 73 65 20 2a 2f 0 otherwise */
1496e 0a 20 20 75 38 20 63 68 69 6c 64 50 74 72 53 69 . u8 childPtrSi
1496f 7a 65 3b 20 20 20 20 20 2f 2a 20 30 20 69 66 20 ze; /* 0 if
14970 6c 65 61 66 3d 3d 31 2e 20 20 34 20 69 66 20 6c leaf==1. 4 if l
14971 65 61 66 3d 3d 30 20 2a 2f 0a 20 20 75 31 36 20 eaf==0 */. u16
14972 6d 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 maxLocal;
14973 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74 53 68 /* Copy of BtSh
14974 61 72 65 64 2e 6d 61 78 4c 6f 63 61 6c 20 6f 72 ared.maxLocal or
14975 20 42 74 53 68 61 72 65 64 2e 6d 61 78 4c 65 61 BtShared.maxLea
14976 66 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f f */. u16 minLo
14977 63 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 cal; /* C
14978 6f 70 79 20 6f 66 20 42 74 53 68 61 72 65 64 2e opy of BtShared.
14979 6d 69 6e 4c 6f 63 61 6c 20 6f 72 20 42 74 53 68 minLocal or BtSh
1497a 61 72 65 64 2e 6d 69 6e 4c 65 61 66 20 2a 2f 0a ared.minLeaf */.
1497b 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73 65 74 u16 cellOffset
1497c 3b 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 ; /* Index
1497d 69 6e 20 61 44 61 74 61 20 6f 66 20 66 69 72 73 in aData of firs
1497e 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a t cell pointer *
1497f 2f 0a 20 20 75 31 36 20 6e 46 72 65 65 3b 20 20 /. u16 nFree;
14980 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
14981 65 72 20 6f 66 20 66 72 65 65 20 62 79 74 65 73 er of free bytes
14982 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a on the page */.
14983 20 20 75 31 36 20 6e 43 65 6c 6c 3b 20 20 20 20 u16 nCell;
14984 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
14985 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 69 of cells on thi
14986 73 20 70 61 67 65 2c 20 6c 6f 63 61 6c 20 61 6e s page, local an
14987 64 20 6f 76 66 6c 20 2a 2f 0a 20 20 75 31 36 20 d ovfl */. u16
14988 6d 61 73 6b 50 61 67 65 3b 20 20 20 20 20 20 20 maskPage;
14989 20 2f 2a 20 4d 61 73 6b 20 66 6f 72 20 70 61 67 /* Mask for pag
1498a 65 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 73 74 e offset */. st
1498b 72 75 63 74 20 5f 4f 76 66 6c 43 65 6c 6c 20 7b ruct _OvflCell {
1498c 20 20 20 2f 2a 20 43 65 6c 6c 73 20 74 68 61 74 /* Cells that
1498d 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 6f 6e will not fit on
1498e 20 61 44 61 74 61 5b 5d 20 2a 2f 0a 20 20 20 20 aData[] */.
1498f 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 u8 *pCell;
14990 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 /* Pointers
14991 74 6f 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74 to the body of t
14992 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c he overflow cell
14993 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 64 78 3b */. u16 idx;
14994 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
14995 6e 73 65 72 74 20 74 68 69 73 20 63 65 6c 6c 20 nsert this cell
14996 62 65 66 6f 72 65 20 69 64 78 2d 74 68 20 6e 6f before idx-th no
14997 6e 2d 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 n-overflow cell
14998 2a 2f 0a 20 20 7d 20 61 4f 76 66 6c 5b 35 5d 3b */. } aOvfl[5];
14999 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
1499a 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 ; /* Point
1499b 65 72 20 74 6f 20 42 74 53 68 61 72 65 64 20 74 er to BtShared t
1499c 68 61 74 20 74 68 69 73 20 70 61 67 65 20 69 73 hat this page is
1499d 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 20 75 38 part of */. u8
1499e 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 *aData;
1499f 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
149a0 20 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20 74 disk image of t
149a1 68 65 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a he page data */.
149a2 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 DbPage *pDbPag
149a3 65 3b 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 e; /* Pager
149a4 70 61 67 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 page handle */.
149a5 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 Pgno pgno;
149a6 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 /* Page nu
149a7 6d 62 65 72 20 66 6f 72 20 74 68 69 73 20 70 61 mber for this pa
149a8 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ge */.};../*.**
149a9 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 6d The in-memory im
149aa 61 67 65 20 6f 66 20 61 20 64 69 73 6b 20 70 61 age of a disk pa
149ab 67 65 20 68 61 73 20 74 68 65 20 61 75 78 69 6c ge has the auxil
149ac 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e iary information
149ad 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 74 6f 20 appended.** to
149ae 74 68 65 20 65 6e 64 2e 20 20 45 58 54 52 41 5f the end. EXTRA_
149af 53 49 5a 45 20 69 73 20 74 68 65 20 6e 75 6d 62 SIZE is the numb
149b0 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 er of bytes of s
149b1 70 61 63 65 20 6e 65 65 64 65 64 20 74 6f 20 68 pace needed to h
149b2 6f 6c 64 0a 2a 2a 20 74 68 61 74 20 65 78 74 72 old.** that extr
149b3 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a a information..*
149b4 2f 0a 23 64 65 66 69 6e 65 20 45 58 54 52 41 5f /.#define EXTRA_
149b5 53 49 5a 45 20 73 69 7a 65 6f 66 28 4d 65 6d 50 SIZE sizeof(MemP
149b6 61 67 65 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 age)../*.** A li
149b7 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 nked list of the
149b8 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
149b9 74 75 72 65 73 20 69 73 20 73 74 6f 72 65 64 20 tures is stored
149ba 61 74 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 at BtShared.pLoc
149bb 6b 2e 0a 2a 2a 20 4c 6f 63 6b 73 20 61 72 65 20 k..** Locks are
149bc 61 64 64 65 64 20 28 6f 72 20 75 70 67 72 61 64 added (or upgrad
149bd 65 64 20 66 72 6f 6d 20 52 45 41 44 5f 4c 4f 43 ed from READ_LOC
149be 4b 20 74 6f 20 57 52 49 54 45 5f 4c 4f 43 4b 29 K to WRITE_LOCK)
149bf 20 77 68 65 6e 20 61 20 63 75 72 73 6f 72 20 0a when a cursor .
149c0 2a 2a 20 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20 ** is opened on
149c1 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 the table with r
149c2 6f 6f 74 20 70 61 67 65 20 42 74 53 68 61 72 65 oot page BtShare
149c3 64 2e 69 54 61 62 6c 65 2e 20 4c 6f 63 6b 73 20 d.iTable. Locks
149c4 61 72 65 20 72 65 6d 6f 76 65 64 0a 2a 2a 20 66 are removed.** f
149c5 72 6f 6d 20 74 68 69 73 20 6c 69 73 74 20 77 68 rom this list wh
149c6 65 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e en a transaction
149c7 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 is committed or
149c8 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 6f 72 rolled back, or
149c9 20 77 68 65 6e 0a 2a 2a 20 61 20 62 74 72 65 65 when.** a btree
149ca 20 68 61 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65 handle is close
149cb 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 4c d..*/.struct BtL
149cc 6f 63 6b 20 7b 0a 20 20 42 74 72 65 65 20 2a 70 ock {. Btree *p
149cd 42 74 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a Btree; /*
149ce 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 68 6f Btree handle ho
149cf 6c 64 69 6e 67 20 74 68 69 73 20 6c 6f 63 6b 20 lding this lock
149d0 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 61 62 6c 65 */. Pgno iTable
149d1 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f ; /* Ro
149d2 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65 ot page of table
149d3 20 2a 2f 0a 20 20 75 38 20 65 4c 6f 63 6b 3b 20 */. u8 eLock;
149d4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
149d5 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54 EAD_LOCK or WRIT
149d6 45 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 42 74 4c 6f E_LOCK */. BtLo
149d7 63 6b 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 ck *pNext;
149d8 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 42 74 53 /* Next in BtS
149d9 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 73 74 hared.pLock list
149da 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 43 61 6e 64 69 */.};../* Candi
149db 64 61 74 65 20 76 61 6c 75 65 73 20 66 6f 72 20 date values for
149dc 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 2a 2f 0a BtLock.eLock */.
149dd 23 64 65 66 69 6e 65 20 52 45 41 44 5f 4c 4f 43 #define READ_LOC
149de 4b 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 K 1.#define
149df 57 52 49 54 45 5f 4c 4f 43 4b 20 20 20 20 32 0a WRITE_LOCK 2.
149e0 0a 2f 2a 20 41 20 42 74 72 65 65 20 68 61 6e 64 ./* A Btree hand
149e1 6c 65 0a 2a 2a 0a 2a 2a 20 41 20 64 61 74 61 62 le.**.** A datab
149e2 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 ase connection c
149e3 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 ontains a pointe
149e4 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 r to an instance
149e5 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6f 62 6a 65 of.** this obje
149e6 63 74 20 66 6f 72 20 65 76 65 72 79 20 64 61 74 ct for every dat
149e7 61 62 61 73 65 20 66 69 6c 65 20 74 68 61 74 20 abase file that
149e8 69 74 20 68 61 73 20 6f 70 65 6e 2e 20 20 54 68 it has open. Th
149e9 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 is structure.**
149ea 69 73 20 6f 70 61 71 75 65 20 74 6f 20 74 68 65 is opaque to the
149eb 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
149ec 74 69 6f 6e 2e 20 20 54 68 65 20 64 61 74 61 62 tion. The datab
149ed 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 ase connection c
149ee 61 6e 6e 6f 74 0a 2a 2a 20 73 65 65 20 74 68 65 annot.** see the
149ef 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 internals of th
149f0 69 73 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 is structure and
149f1 20 6f 6e 6c 79 20 64 65 61 6c 73 20 77 69 74 68 only deals with
149f2 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 pointers to.**
149f3 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a this structure..
149f4 2a 2a 0a 2a 2a 20 46 6f 72 20 73 6f 6d 65 20 64 **.** For some d
149f5 61 74 61 62 61 73 65 20 66 69 6c 65 73 2c 20 74 atabase files, t
149f6 68 65 20 73 61 6d 65 20 75 6e 64 65 72 6c 79 69 he same underlyi
149f7 6e 67 20 64 61 74 61 62 61 73 65 20 63 61 63 68 ng database cach
149f8 65 20 6d 69 67 68 74 20 62 65 20 0a 2a 2a 20 73 e might be .** s
149f9 68 61 72 65 64 20 62 65 74 77 65 65 6e 20 6d 75 hared between mu
149fa 6c 74 69 70 6c 65 20 63 6f 6e 6e 65 63 74 69 6f ltiple connectio
149fb 6e 73 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 ns. In that cas
149fc 65 2c 20 65 61 63 68 20 63 6f 6e 6e 65 63 74 69 e, each connecti
149fd 6f 6e 0a 2a 2a 20 68 61 73 20 69 74 20 6f 77 6e on.** has it own
149fe 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
149ff 73 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 65 s object. But e
14a00 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 ach instance of
14a01 74 68 69 73 20 6f 62 6a 65 63 74 0a 2a 2a 20 70 this object.** p
14a02 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 61 6d oints to the sam
14a03 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 e BtShared objec
14a04 74 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65 t. The database
14a05 20 63 61 63 68 65 20 61 6e 64 20 74 68 65 0a 2a cache and the.*
14a06 2a 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 * schema associa
14a07 74 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74 ted with the dat
14a08 61 62 61 73 65 20 66 69 6c 65 20 61 72 65 20 61 abase file are a
14a09 6c 6c 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 ll contained wit
14a0a 68 69 6e 0a 2a 2a 20 74 68 65 20 42 74 53 68 61 hin.** the BtSha
14a0b 72 65 64 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a red object..**.*
14a0c 2a 20 41 6c 6c 20 66 69 65 6c 64 73 20 69 6e 20 * All fields in
14a0d 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 this structure a
14a0e 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 65 re accessed unde
14a0f 72 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e r sqlite3.mutex.
14a10 0a 2a 2a 20 54 68 65 20 70 42 74 20 70 6f 69 6e .** The pBt poin
14a11 74 65 72 20 69 74 73 65 6c 66 20 6d 61 79 20 6e ter itself may n
14a12 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20 77 68 ot be changed wh
14a13 69 6c 65 20 74 68 65 72 65 20 65 78 69 73 74 73 ile there exists
14a14 20 63 75 72 73 6f 72 73 20 0a 2a 2a 20 69 6e 20 cursors .** in
14a15 74 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 42 the referenced B
14a16 74 53 68 61 72 65 64 20 74 68 61 74 20 70 6f 69 tShared that poi
14a17 6e 74 20 62 61 63 6b 20 74 6f 20 74 68 69 73 20 nt back to this
14a18 42 74 72 65 65 20 73 69 6e 63 65 20 74 68 6f 73 Btree since thos
14a19 65 0a 2a 2a 20 63 75 72 73 6f 72 73 20 68 61 76 e.** cursors hav
14a1a 65 20 74 6f 20 64 6f 20 67 6f 20 74 68 72 6f 75 e to do go throu
14a1b 67 68 20 74 68 69 73 20 42 74 72 65 65 20 74 6f gh this Btree to
14a1c 20 66 69 6e 64 20 74 68 65 69 72 20 42 74 53 68 find their BtSh
14a1d 61 72 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 79 ared and.** they
14a1e 20 6f 66 74 65 6e 20 64 6f 20 73 6f 20 77 69 74 often do so wit
14a1f 68 6f 75 74 20 68 6f 6c 64 69 6e 67 20 73 71 6c hout holding sql
14a20 69 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2f 0a 73 ite3.mutex..*/.s
14a21 74 72 75 63 74 20 42 74 72 65 65 20 7b 0a 20 20 truct Btree {.
14a22 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 sqlite3 *db;
14a23 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 /* The databa
14a24 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f se connection ho
14a25 6c 64 69 6e 67 20 74 68 69 73 20 62 74 72 65 65 lding this btree
14a26 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a */. BtShared *
14a27 70 42 74 3b 20 20 20 20 20 2f 2a 20 53 68 61 72 pBt; /* Shar
14a28 61 62 6c 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 able content of
14a29 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 this btree */.
14a2a 75 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 u8 inTrans;
14a2b 20 20 20 2f 2a 20 54 52 41 4e 53 5f 4e 4f 4e 45 /* TRANS_NONE
14a2c 2c 20 54 52 41 4e 53 5f 52 45 41 44 20 6f 72 20 , TRANS_READ or
14a2d 54 52 41 4e 53 5f 57 52 49 54 45 20 2a 2f 0a 20 TRANS_WRITE */.
14a2e 20 75 38 20 73 68 61 72 61 62 6c 65 3b 20 20 20 u8 sharable;
14a2f 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 /* True if w
14a30 65 20 63 61 6e 20 73 68 61 72 65 20 70 42 74 20 e can share pBt
14a31 77 69 74 68 20 61 6e 6f 74 68 65 72 20 64 62 20 with another db
14a32 2a 2f 0a 20 20 75 38 20 6c 6f 63 6b 65 64 3b 20 */. u8 locked;
14a33 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
14a34 69 66 20 64 62 20 63 75 72 72 65 6e 74 6c 79 20 if db currently
14a35 68 61 73 20 70 42 74 20 6c 6f 63 6b 65 64 20 2a has pBt locked *
14a36 2f 0a 20 20 69 6e 74 20 77 61 6e 74 54 6f 4c 6f /. int wantToLo
14a37 63 6b 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 ck; /* Number
14a38 20 6f 66 20 6e 65 73 74 65 64 20 63 61 6c 6c 73 of nested calls
14a39 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 to sqlite3Btree
14a3a 45 6e 74 65 72 28 29 20 2a 2f 0a 20 20 69 6e 74 Enter() */. int
14a3b 20 6e 42 61 63 6b 75 70 3b 20 20 20 20 20 20 20 nBackup;
14a3c 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 63 /* Number of bac
14a3d 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 73 20 72 kup operations r
14a3e 65 61 64 69 6e 67 20 74 68 69 73 20 62 74 72 65 eading this btre
14a3f 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 4e e */. Btree *pN
14a40 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4c 69 73 ext; /* Lis
14a41 74 20 6f 66 20 6f 74 68 65 72 20 73 68 61 72 61 t of other shara
14a42 62 6c 65 20 42 74 72 65 65 73 20 66 72 6f 6d 20 ble Btrees from
14a43 74 68 65 20 73 61 6d 65 20 64 62 20 2a 2f 0a 20 the same db */.
14a44 20 42 74 72 65 65 20 2a 70 50 72 65 76 3b 20 20 Btree *pPrev;
14a45 20 20 20 20 2f 2a 20 42 61 63 6b 20 70 6f 69 6e /* Back poin
14a46 74 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65 20 ter of the same
14a47 6c 69 73 74 20 2a 2f 0a 23 69 66 6e 64 65 66 20 list */.#ifndef
14a48 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
14a49 45 44 5f 43 41 43 48 45 0a 20 20 42 74 4c 6f 63 ED_CACHE. BtLoc
14a4a 6b 20 6c 6f 63 6b 3b 20 20 20 20 20 20 20 2f 2a k lock; /*
14a4b 20 4f 62 6a 65 63 74 20 75 73 65 64 20 74 6f 20 Object used to
14a4c 6c 6f 63 6b 20 70 61 67 65 20 31 20 2a 2f 0a 23 lock page 1 */.#
14a4d 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 endif.};../*.**
14a4e 42 74 72 65 65 2e 69 6e 54 72 61 6e 73 20 6d 61 Btree.inTrans ma
14a4f 79 20 74 61 6b 65 20 6f 6e 65 20 6f 66 20 74 68 y take one of th
14a50 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 e following valu
14a51 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 es..**.** If the
14a52 20 73 68 61 72 65 64 2d 64 61 74 61 20 65 78 74 shared-data ext
14a53 65 6e 73 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 ension is enable
14a54 64 2c 20 74 68 65 72 65 20 6d 61 79 20 62 65 20 d, there may be
14a55 6d 75 6c 74 69 70 6c 65 20 75 73 65 72 73 0a 2a multiple users.*
14a56 2a 20 6f 66 20 74 68 65 20 42 74 72 65 65 20 73 * of the Btree s
14a57 74 72 75 63 74 75 72 65 2e 20 41 74 20 6d 6f 73 tructure. At mos
14a58 74 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 6d t one of these m
14a59 61 79 20 6f 70 65 6e 20 61 20 77 72 69 74 65 20 ay open a write
14a5a 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 transaction,.**
14a5b 62 75 74 20 61 6e 79 20 6e 75 6d 62 65 72 20 6d but any number m
14a5c 61 79 20 68 61 76 65 20 61 63 74 69 76 65 20 72 ay have active r
14a5d 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 ead transactions
14a5e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 52 41 ..*/.#define TRA
14a5f 4e 53 5f 4e 4f 4e 45 20 20 30 0a 23 64 65 66 69 NS_NONE 0.#defi
14a60 6e 65 20 54 52 41 4e 53 5f 52 45 41 44 20 20 31 ne TRANS_READ 1
14a61 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 57 .#define TRANS_W
14a62 52 49 54 45 20 32 0a 0a 2f 2a 0a 2a 2a 20 41 6e RITE 2../*.** An
14a63 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
14a64 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 s object represe
14a65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20 64 61 74 nts a single dat
14a66 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a abase file..** .
14a67 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 61 74 61 ** A single data
14a68 62 61 73 65 20 66 69 6c 65 20 63 61 6e 20 62 65 base file can be
14a69 20 69 6e 20 75 73 65 20 61 73 20 74 68 65 20 73 in use as the s
14a6a 61 6d 65 20 74 69 6d 65 20 62 79 20 74 77 6f 0a ame time by two.
14a6b 2a 2a 20 6f 72 20 6d 6f 72 65 20 64 61 74 61 62 ** or more datab
14a6c 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e ase connections.
14a6d 20 20 57 68 65 6e 20 74 77 6f 20 6f 72 20 6d 6f When two or mo
14a6e 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 re connections a
14a6f 72 65 0a 2a 2a 20 73 68 61 72 69 6e 67 20 74 68 re.** sharing th
14a70 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 e same database
14a71 66 69 6c 65 2c 20 65 61 63 68 20 63 6f 6e 6e 65 file, each conne
14a72 63 74 69 6f 6e 20 68 61 73 20 69 74 20 6f 77 6e ction has it own
14a73 0a 2a 2a 20 70 72 69 76 61 74 65 20 42 74 72 65 .** private Btre
14a74 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65 e object for the
14a75 20 66 69 6c 65 20 61 6e 64 20 65 61 63 68 20 6f file and each o
14a76 66 20 74 68 6f 73 65 20 42 74 72 65 65 73 20 70 f those Btrees p
14a77 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 74 68 69 73 oints.** to this
14a78 20 6f 6e 65 20 42 74 53 68 61 72 65 64 20 6f 62 one BtShared ob
14a79 6a 65 63 74 2e 20 20 42 74 53 68 61 72 65 64 2e ject. BtShared.
14a7a 6e 52 65 66 20 69 73 20 74 68 65 20 6e 75 6d 62 nRef is the numb
14a7b 65 72 20 6f 66 0a 2a 2a 20 63 6f 6e 6e 65 63 74 er of.** connect
14a7c 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c 79 20 73 ions currently s
14a7d 68 61 72 69 6e 67 20 74 68 69 73 20 64 61 74 61 haring this data
14a7e 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a base file..**.**
14a7f 20 46 69 65 6c 64 73 20 69 6e 20 74 68 69 73 20 Fields in this
14a80 73 74 72 75 63 74 75 72 65 20 61 72 65 20 61 63 structure are ac
14a81 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 68 65 cessed under the
14a82 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78 0a BtShared.mutex.
14a83 2a 2a 20 6d 75 74 65 78 2c 20 65 78 63 65 70 74 ** mutex, except
14a84 20 66 6f 72 20 6e 52 65 66 20 61 6e 64 20 70 4e for nRef and pN
14a85 65 78 74 20 77 68 69 63 68 20 61 72 65 20 61 63 ext which are ac
14a86 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 68 65 cessed under the
14a87 0a 2a 2a 20 67 6c 6f 62 61 6c 20 53 51 4c 49 54 .** global SQLIT
14a88 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
14a89 41 53 54 45 52 20 6d 75 74 65 78 2e 20 20 54 68 ASTER mutex. Th
14a8a 65 20 70 50 61 67 65 72 20 66 69 65 6c 64 0a 2a e pPager field.*
14a8b 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 64 * may not be mod
14a8c 69 66 69 65 64 20 6f 6e 63 65 20 69 74 20 69 73 ified once it is
14a8d 20 69 6e 69 74 69 61 6c 6c 79 20 73 65 74 20 61 initially set a
14a8e 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 66 3e 30 s long as nRef>0
14a8f 2e 0a 2a 2a 20 54 68 65 20 70 53 63 68 65 6d 61 ..** The pSchema
14a90 20 66 69 65 6c 64 20 6d 61 79 20 62 65 20 73 65 field may be se
14a91 74 20 6f 6e 63 65 20 75 6e 64 65 72 20 42 74 53 t once under BtS
14a92 68 61 72 65 64 2e 6d 75 74 65 78 20 61 6e 64 0a hared.mutex and.
14a93 2a 2a 20 74 68 65 72 65 61 66 74 65 72 20 69 73 ** thereafter is
14a94 20 75 6e 63 68 61 6e 67 65 64 20 61 73 20 6c 6f unchanged as lo
14a95 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e 0a 2a 2a ng as nRef>0..**
14a96 0a 2a 2a 20 69 73 50 65 6e 64 69 6e 67 3a 0a 2a .** isPending:.*
14a97 2a 0a 2a 2a 20 20 20 49 66 20 61 20 42 74 53 68 *.** If a BtSh
14a98 61 72 65 64 20 63 6c 69 65 6e 74 20 66 61 69 6c ared client fail
14a99 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 77 72 s to obtain a wr
14a9a 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 61 20 64 61 ite-lock on a da
14a9b 74 61 62 61 73 65 0a 2a 2a 20 20 20 74 61 62 6c tabase.** tabl
14a9c 65 20 28 62 65 63 61 75 73 65 20 74 68 65 72 65 e (because there
14a9d 20 65 78 69 73 74 73 20 6f 6e 65 20 6f 72 20 6d exists one or m
14a9e 6f 72 65 20 72 65 61 64 2d 6c 6f 63 6b 73 20 6f ore read-locks o
14a9f 6e 20 74 68 65 20 74 61 62 6c 65 29 2c 0a 2a 2a n the table),.**
14aa0 20 20 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 the shared-ca
14aa1 63 68 65 20 65 6e 74 65 72 73 20 27 70 65 6e 64 che enters 'pend
14aa2 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61 74 65 20 ing-lock' state
14aa3 61 6e 64 20 69 73 50 65 6e 64 69 6e 67 20 69 73 and isPending is
14aa4 0a 2a 2a 20 20 20 73 65 74 20 74 6f 20 74 72 75 .** set to tru
14aa5 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 73 e..**.** The s
14aa6 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65 61 76 hared-cache leav
14aa7 65 73 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20 es the 'pending
14aa8 6c 6f 63 6b 27 20 73 74 61 74 65 20 77 68 65 6e lock' state when
14aa9 20 65 69 74 68 65 72 20 6f 66 0a 2a 2a 20 20 20 either of.**
14aaa 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 63 the following oc
14aab 63 75 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 31 cur:.**.** 1
14aac 29 20 54 68 65 20 63 75 72 72 65 6e 74 20 77 72 ) The current wr
14aad 69 74 65 72 20 28 42 74 53 68 61 72 65 64 2e 70 iter (BtShared.p
14aae 57 72 69 74 65 72 29 20 63 6f 6e 63 6c 75 64 65 Writer) conclude
14aaf 73 20 69 74 73 20 74 72 61 6e 73 61 63 74 69 6f s its transactio
14ab0 6e 2c 20 4f 52 0a 2a 2a 20 20 20 20 20 32 29 20 n, OR.** 2)
14ab1 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f The number of lo
14ab2 63 6b 73 20 68 65 6c 64 20 62 79 20 6f 74 68 65 cks held by othe
14ab3 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 64 72 r connections dr
14ab4 6f 70 73 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 0a ops to zero..**.
14ab5 2a 2a 20 20 20 77 68 69 6c 65 20 69 6e 20 74 68 ** while in th
14ab6 65 20 27 70 65 6e 64 69 6e 67 2d 6c 6f 63 6b 27 e 'pending-lock'
14ab7 20 73 74 61 74 65 2c 20 6e 6f 20 63 6f 6e 6e 65 state, no conne
14ab8 63 74 69 6f 6e 20 6d 61 79 20 73 74 61 72 74 20 ction may start
14ab9 61 20 6e 65 77 0a 2a 2a 20 20 20 74 72 61 6e 73 a new.** trans
14aba 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 action..**.**
14abb 54 68 69 73 20 66 65 61 74 75 72 65 20 69 73 20 This feature is
14abc 69 6e 63 6c 75 64 65 64 20 74 6f 20 68 65 6c 70 included to help
14abd 20 70 72 65 76 65 6e 74 20 77 72 69 74 65 72 2d prevent writer-
14abe 73 74 61 72 76 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 starvation..*/.s
14abf 74 72 75 63 74 20 42 74 53 68 61 72 65 64 20 7b truct BtShared {
14ac0 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
14ac1 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ; /* The
14ac2 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0a 20 20 page cache */.
14ac3 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 sqlite3 *db;
14ac4 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
14ac5 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 e connection cur
14ac6 72 65 6e 74 6c 79 20 75 73 69 6e 67 20 74 68 69 rently using thi
14ac7 73 20 42 74 72 65 65 20 2a 2f 0a 20 20 42 74 43 s Btree */. BtC
14ac8 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 20 ursor *pCursor;
14ac9 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20 /* A list of
14aca 61 6c 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 all open cursors
14acb 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 */. MemPage *p
14acc 50 61 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 46 Page1; /* F
14acd 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 irst page of the
14ace 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 database */. u
14acf 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20 8 readOnly;
14ad0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
14ad1 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 the underlying f
14ad2 69 6c 65 20 69 73 20 72 65 61 64 6f 6e 6c 79 20 ile is readonly
14ad3 2a 2f 0a 20 20 75 38 20 70 61 67 65 53 69 7a 65 */. u8 pageSize
14ad4 46 69 78 65 64 3b 20 20 20 20 20 2f 2a 20 54 72 Fixed; /* Tr
14ad5 75 65 20 69 66 20 74 68 65 20 70 61 67 65 20 73 ue if the page s
14ad6 69 7a 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 ize can no longe
14ad7 72 20 62 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a r be changed */.
14ad8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
14ad9 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
14ada 20 75 38 20 61 75 74 6f 56 61 63 75 75 6d 3b 20 u8 autoVacuum;
14adb 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
14adc 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 f auto-vacuum is
14add 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 75 38 enabled */. u8
14ade 20 69 6e 63 72 56 61 63 75 75 6d 3b 20 20 20 20 incrVacuum;
14adf 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 /* True if i
14ae0 6e 63 72 2d 76 61 63 75 75 6d 20 69 73 20 65 6e ncr-vacuum is en
14ae1 61 62 6c 65 64 20 2a 2f 0a 23 65 6e 64 69 66 0a abled */.#endif.
14ae2 20 20 75 31 36 20 70 61 67 65 53 69 7a 65 3b 20 u16 pageSize;
14ae3 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
14ae4 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
14ae5 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 on a page */.
14ae6 75 31 36 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 u16 usableSize;
14ae7 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
14ae8 6f 66 20 75 73 61 62 6c 65 20 62 79 74 65 73 20 of usable bytes
14ae9 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a on each page */.
14aea 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 u16 maxLocal;
14aeb 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d /* Maxim
14aec 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 um local payload
14aed 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 in non-LEAFDATA
14aee 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 75 31 36 tables */. u16
14aef 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20 minLocal;
14af0 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f /* Minimum lo
14af1 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 6e cal payload in n
14af2 6f 6e 2d 4c 45 41 46 44 41 54 41 20 74 61 62 6c on-LEAFDATA tabl
14af3 65 73 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78 4c es */. u16 maxL
14af4 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a eaf; /*
14af5 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 Maximum local p
14af6 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41 46 ayload in a LEAF
14af7 44 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a 20 20 DATA table */.
14af8 75 31 36 20 6d 69 6e 4c 65 61 66 3b 20 20 20 20 u16 minLeaf;
14af9 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d /* Minimum
14afa 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 local payload i
14afb 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 61 62 n a LEAFDATA tab
14afc 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 le */. u8 inTra
14afd 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 2f 2a nsaction; /*
14afe 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 Transaction sta
14aff 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 72 61 te */. int nTra
14b00 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 2f 2a nsaction; /*
14b01 20 4e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 Number of open
14b02 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 28 72 65 transactions (re
14b03 61 64 20 2b 20 77 72 69 74 65 29 20 2a 2f 0a 20 ad + write) */.
14b04 20 76 6f 69 64 20 2a 70 53 63 68 65 6d 61 3b 20 void *pSchema;
14b05 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
14b06 72 20 74 6f 20 73 70 61 63 65 20 61 6c 6c 6f 63 r to space alloc
14b07 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 42 ated by sqlite3B
14b08 74 72 65 65 53 63 68 65 6d 61 28 29 20 2a 2f 0a treeSchema() */.
14b09 20 20 76 6f 69 64 20 28 2a 78 46 72 65 65 53 63 void (*xFreeSc
14b0a 68 65 6d 61 29 28 76 6f 69 64 2a 29 3b 20 20 2f hema)(void*); /
14b0b 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 * Destructor for
14b0c 20 42 74 53 68 61 72 65 64 2e 70 53 63 68 65 6d BtShared.pSchem
14b0d 61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d a */. sqlite3_m
14b0e 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 2f 2a 20 utex *mutex; /*
14b0f 4e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 Non-recursive mu
14b10 74 65 78 20 72 65 71 75 69 72 65 64 20 74 6f 20 tex required to
14b11 61 63 63 65 73 73 20 74 68 69 73 20 73 74 72 75 access this stru
14b12 63 74 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a ct */. Bitvec *
14b13 70 48 61 73 43 6f 6e 74 65 6e 74 3b 20 20 2f 2a pHasContent; /*
14b14 20 53 65 74 20 6f 66 20 70 61 67 65 73 20 6d 6f Set of pages mo
14b15 76 65 64 20 74 6f 20 66 72 65 65 2d 6c 69 73 74 ved to free-list
14b16 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f this transactio
14b17 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c n */.#ifndef SQL
14b18 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
14b19 43 41 43 48 45 0a 20 20 69 6e 74 20 6e 52 65 66 CACHE. int nRef
14b1a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
14b1b 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 Number of refer
14b1c 65 6e 63 65 73 20 74 6f 20 74 68 69 73 20 73 74 ences to this st
14b1d 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 42 74 53 ructure */. BtS
14b1e 68 61 72 65 64 20 2a 70 4e 65 78 74 3b 20 20 20 hared *pNext;
14b1f 20 20 20 2f 2a 20 4e 65 78 74 20 6f 6e 20 61 20 /* Next on a
14b20 6c 69 73 74 20 6f 66 20 73 68 61 72 61 62 6c 65 list of sharable
14b21 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 BtShared struct
14b22 73 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 s */. BtLock *p
14b23 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 Lock; /*
14b24 4c 69 73 74 20 6f 66 20 6c 6f 63 6b 73 20 68 65 List of locks he
14b25 6c 64 20 6f 6e 20 74 68 69 73 20 73 68 61 72 65 ld on this share
14b26 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 20 2a d-btree struct *
14b27 2f 0a 20 20 42 74 72 65 65 20 2a 70 57 72 69 74 /. Btree *pWrit
14b28 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 42 74 72 er; /* Btr
14b29 65 65 20 77 69 74 68 20 63 75 72 72 65 6e 74 6c ee with currentl
14b2a 79 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 y open write tra
14b2b 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 nsaction */. u8
14b2c 20 69 73 45 78 63 6c 75 73 69 76 65 3b 20 20 20 isExclusive;
14b2d 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 /* True if p
14b2e 57 72 69 74 65 72 20 68 61 73 20 61 6e 20 45 58 Writer has an EX
14b2f 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 CLUSIVE lock on
14b30 74 68 65 20 64 62 20 2a 2f 0a 20 20 75 38 20 69 the db */. u8 i
14b31 73 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 20 sPending;
14b32 20 20 2f 2a 20 49 66 20 77 61 69 74 69 6e 67 20 /* If waiting
14b33 66 6f 72 20 72 65 61 64 2d 6c 6f 63 6b 73 20 74 for read-locks t
14b34 6f 20 63 6c 65 61 72 20 2a 2f 0a 23 65 6e 64 69 o clear */.#endi
14b35 66 0a 20 20 75 38 20 2a 70 54 6d 70 53 70 61 63 f. u8 *pTmpSpac
14b36 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 74 53 e; /* BtS
14b37 68 61 72 65 64 2e 70 61 67 65 53 69 7a 65 20 62 hared.pageSize b
14b38 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 66 6f ytes of space fo
14b39 72 20 74 6d 70 20 75 73 65 20 2a 2f 0a 7d 3b 0a r tmp use */.};.
14b3a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e ./*.** An instan
14b3b 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
14b3c 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 ing structure is
14b3d 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 69 6e used to hold in
14b3e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f formation.** abo
14b3f 75 74 20 61 20 63 65 6c 6c 2e 20 20 54 68 65 20 ut a cell. The
14b40 70 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20 66 parseCellPtr() f
14b41 75 6e 63 74 69 6f 6e 20 66 69 6c 6c 73 20 69 6e unction fills in
14b42 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a this structure.
14b43 2a 2a 20 62 61 73 65 64 20 6f 6e 20 69 6e 66 6f ** based on info
14b44 72 6d 61 74 69 6f 6e 20 65 78 74 72 61 63 74 20 rmation extract
14b45 66 72 6f 6d 20 74 68 65 20 72 61 77 20 64 69 73 from the raw dis
14b46 6b 20 70 61 67 65 2e 0a 2a 2f 0a 74 79 70 65 64 k page..*/.typed
14b47 65 66 20 73 74 72 75 63 74 20 43 65 6c 6c 49 6e ef struct CellIn
14b48 66 6f 20 43 65 6c 6c 49 6e 66 6f 3b 0a 73 74 72 fo CellInfo;.str
14b49 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 7b 0a 20 uct CellInfo {.
14b4a 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 u8 *pCell;
14b4b 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 /* Pointer to th
14b4c 65 20 73 74 61 72 74 20 6f 66 20 63 65 6c 6c 20 e start of cell
14b4d 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 36 34 content */. i64
14b4e 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 54 nKey; /* T
14b4f 68 65 20 6b 65 79 20 66 6f 72 20 49 4e 54 4b 45 he key for INTKE
14b50 59 20 74 61 62 6c 65 73 2c 20 6f 72 20 6e 75 6d Y tables, or num
14b51 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
14b52 6b 65 79 20 2a 2f 0a 20 20 75 33 32 20 6e 44 61 key */. u32 nDa
14b53 74 61 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 ta; /* Numbe
14b54 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 r of bytes of da
14b55 74 61 20 2a 2f 0a 20 20 75 33 32 20 6e 50 61 79 ta */. u32 nPay
14b56 6c 6f 61 64 3b 20 20 2f 2a 20 54 6f 74 61 6c 20 load; /* Total
14b57 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 amount of payloa
14b58 64 20 2a 2f 0a 20 20 75 31 36 20 6e 48 65 61 64 d */. u16 nHead
14b59 65 72 3b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 er; /* Size of
14b5a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
14b5b 74 20 68 65 61 64 65 72 20 69 6e 20 62 79 74 65 t header in byte
14b5c 73 20 2a 2f 0a 20 20 75 31 36 20 6e 4c 6f 63 61 s */. u16 nLoca
14b5d 6c 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 l; /* Amount
14b5e 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 20 of payload held
14b5f 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 75 31 36 locally */. u16
14b60 20 69 4f 76 65 72 66 6c 6f 77 3b 20 2f 2a 20 4f iOverflow; /* O
14b61 66 66 73 65 74 20 74 6f 20 6f 76 65 72 66 6c 6f ffset to overflo
14b62 77 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 w page number.
14b63 5a 65 72 6f 20 69 66 20 6e 6f 20 6f 76 65 72 66 Zero if no overf
14b64 6c 6f 77 20 2a 2f 0a 20 20 75 31 36 20 6e 53 69 low */. u16 nSi
14b65 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 ze; /* Size
14b66 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 of the cell cont
14b67 65 6e 74 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 ent on the main
14b68 62 2d 74 72 65 65 20 70 61 67 65 20 2a 2f 0a 7d b-tree page */.}
14b69 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d ;../*.** Maximum
14b6a 20 64 65 70 74 68 20 6f 66 20 61 6e 20 53 51 4c depth of an SQL
14b6b 69 74 65 20 42 2d 54 72 65 65 20 73 74 72 75 63 ite B-Tree struc
14b6c 74 75 72 65 2e 20 41 6e 79 20 42 2d 54 72 65 65 ture. Any B-Tree
14b6d 20 64 65 65 70 65 72 20 74 68 61 6e 0a 2a 2a 20 deeper than.**
14b6e 74 68 69 73 20 77 69 6c 6c 20 62 65 20 64 65 63 this will be dec
14b6f 6c 61 72 65 64 20 63 6f 72 72 75 70 74 2e 20 54 lared corrupt. T
14b70 68 69 73 20 76 61 6c 75 65 20 69 73 20 63 61 6c his value is cal
14b71 63 75 6c 61 74 65 64 20 62 61 73 65 64 20 6f 6e culated based on
14b72 20 61 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 64 61 a.** maximum da
14b73 74 61 62 61 73 65 20 73 69 7a 65 20 6f 66 20 32 tabase size of 2
14b74 5e 33 31 20 70 61 67 65 73 20 61 20 6d 69 6e 69 ^31 pages a mini
14b75 6d 75 6d 20 66 61 6e 6f 75 74 20 6f 66 20 32 20 mum fanout of 2
14b76 66 6f 72 20 61 0a 2a 2a 20 72 6f 6f 74 2d 6e 6f for a.** root-no
14b77 64 65 20 61 6e 64 20 33 20 66 6f 72 20 61 6c 6c de and 3 for all
14b78 20 6f 74 68 65 72 20 69 6e 74 65 72 6e 61 6c 20 other internal
14b79 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 nodes..**.** If
14b7a 61 20 74 72 65 65 20 74 68 61 74 20 61 70 70 65 a tree that appe
14b7b 61 72 73 20 74 6f 20 62 65 20 74 61 6c 6c 65 72 ars to be taller
14b7c 20 74 68 61 6e 20 74 68 69 73 20 69 73 20 65 6e than this is en
14b7d 63 6f 75 6e 74 65 72 65 64 2c 20 69 74 20 69 73 countered, it is
14b7e 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 61 74 .** assumed that
14b7f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
14b80 20 63 6f 72 72 75 70 74 2e 0a 2a 2f 0a 23 64 65 corrupt..*/.#de
14b81 66 69 6e 65 20 42 54 43 55 52 53 4f 52 5f 4d 41 fine BTCURSOR_MA
14b82 58 5f 44 45 50 54 48 20 32 30 0a 0a 2f 2a 0a 2a X_DEPTH 20../*.*
14b83 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 61 20 * A cursor is a
14b84 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 61 72 pointer to a par
14b85 74 69 63 75 6c 61 72 20 65 6e 74 72 79 20 77 69 ticular entry wi
14b86 74 68 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61 thin a particula
14b87 72 0a 2a 2a 20 62 2d 74 72 65 65 20 77 69 74 68 r.** b-tree with
14b88 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 in a database fi
14b89 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 6e le..**.** The en
14b8a 74 72 79 20 69 73 20 69 64 65 6e 74 69 66 69 65 try is identifie
14b8b 64 20 62 79 20 69 74 73 20 4d 65 6d 50 61 67 65 d by its MemPage
14b8c 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78 20 69 and the index i
14b8d 6e 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 43 65 n.** MemPage.aCe
14b8e 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 65 6e 74 72 ll[] of the entr
14b8f 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c y..**.** A singl
14b90 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
14b91 63 61 6e 20 73 68 61 72 65 64 20 62 79 20 74 77 can shared by tw
14b92 6f 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 o more database
14b93 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20 connections,.**
14b94 62 75 74 20 63 75 72 73 6f 72 73 20 63 61 6e 6e but cursors cann
14b95 6f 74 20 62 65 20 73 68 61 72 65 64 2e 20 20 45 ot be shared. E
14b96 61 63 68 20 63 75 72 73 6f 72 20 69 73 20 61 73 ach cursor is as
14b97 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 0a sociated with a.
14b98 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 ** particular da
14b99 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
14b9a 6e 20 69 64 65 6e 74 69 66 69 65 64 20 42 74 43 n identified BtC
14b9b 75 72 73 6f 72 2e 70 42 74 72 65 65 2e 64 62 2e ursor.pBtree.db.
14b9c 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69 6e .**.** Fields in
14b9d 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
14b9e 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 are accessed und
14b9f 65 72 20 74 68 65 20 42 74 53 68 61 72 65 64 2e er the BtShared.
14ba0 6d 75 74 65 78 0a 2a 2a 20 66 6f 75 6e 64 20 61 mutex.** found a
14ba1 74 20 73 65 6c 66 2d 3e 70 42 74 2d 3e 6d 75 74 t self->pBt->mut
14ba2 65 78 2e 20 0a 2a 2f 0a 73 74 72 75 63 74 20 42 ex. .*/.struct B
14ba3 74 43 75 72 73 6f 72 20 7b 0a 20 20 42 74 72 65 tCursor {. Btre
14ba4 65 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20 20 e *pBtree;
14ba5 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 72 /* The Btr
14ba6 65 65 20 74 6f 20 77 68 69 63 68 20 74 68 69 73 ee to which this
14ba7 20 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 cursor belongs
14ba8 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 */. BtShared *p
14ba9 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f Bt; /
14baa 2a 20 54 68 65 20 42 74 53 68 61 72 65 64 20 74 * The BtShared t
14bab 68 69 73 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 his cursor point
14bac 73 20 74 6f 20 2a 2f 0a 20 20 42 74 43 75 72 73 s to */. BtCurs
14bad 6f 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 or *pNext, *pPre
14bae 76 3b 20 20 2f 2a 20 46 6f 72 6d 73 20 61 20 6c v; /* Forms a l
14baf 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c inked list of al
14bb0 6c 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 73 l cursors */. s
14bb1 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70 truct KeyInfo *p
14bb2 4b 65 79 49 6e 66 6f 3b 20 2f 2a 20 41 72 67 75 KeyInfo; /* Argu
14bb3 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 63 ment passed to c
14bb4 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 omparison functi
14bb5 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e on */. Pgno pgn
14bb6 6f 52 6f 6f 74 3b 20 20 20 20 20 20 20 20 20 20 oRoot;
14bb7 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 /* The root pa
14bb8 67 65 20 6f 66 20 74 68 69 73 20 74 72 65 65 20 ge of this tree
14bb9 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 */. sqlite3_int
14bba 36 34 20 63 61 63 68 65 64 52 6f 77 69 64 3b 20 64 cachedRowid;
14bbb 2f 2a 20 4e 65 78 74 20 72 6f 77 69 64 20 63 61 /* Next rowid ca
14bbc 63 68 65 2e 20 20 30 20 6d 65 61 6e 73 20 6e 6f che. 0 means no
14bbd 74 20 76 61 6c 69 64 20 2a 2f 0a 20 20 43 65 6c t valid */. Cel
14bbe 6c 49 6e 66 6f 20 69 6e 66 6f 3b 20 20 20 20 20 lInfo info;
14bbf 20 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72 73 /* A pars
14bc0 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 77 65 e of the cell we
14bc1 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 20 61 74 are pointing at
14bc2 20 2a 2f 0a 20 20 75 38 20 77 72 46 6c 61 67 3b */. u8 wrFlag;
14bc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14bc4 2f 2a 20 54 72 75 65 20 69 66 20 77 72 69 74 61 /* True if writa
14bc5 62 6c 65 20 2a 2f 0a 20 20 75 38 20 61 74 4c 61 ble */. u8 atLa
14bc6 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 st;
14bc7 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 /* Cursor poi
14bc8 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 nting to the las
14bc9 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 75 38 20 t entry */. u8
14bca 76 61 6c 69 64 4e 4b 65 79 3b 20 20 20 20 20 20 validNKey;
14bcb 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
14bcc 66 20 69 6e 66 6f 2e 6e 4b 65 79 20 69 73 20 76 f info.nKey is v
14bcd 61 6c 69 64 20 2a 2f 0a 20 20 75 38 20 65 53 74 alid */. u8 eSt
14bce 61 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ate;
14bcf 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 /* One of th
14bd0 65 20 43 55 52 53 4f 52 5f 58 58 58 20 63 6f 6e e CURSOR_XXX con
14bd1 73 74 61 6e 74 73 20 28 73 65 65 20 62 65 6c 6f stants (see belo
14bd2 77 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4b w) */. void *pK
14bd3 65 79 3b 20 20 20 20 20 20 2f 2a 20 53 61 76 65 ey; /* Save
14bd4 64 20 6b 65 79 20 74 68 61 74 20 77 61 73 20 63 d key that was c
14bd5 75 72 73 6f 72 27 73 20 6c 61 73 74 20 6b 6e 6f ursor's last kno
14bd6 77 6e 20 70 6f 73 69 74 69 6f 6e 20 2a 2f 0a 20 wn position */.
14bd7 20 69 36 34 20 6e 4b 65 79 3b 20 20 20 20 20 20 i64 nKey;
14bd8 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 4b 65 /* Size of pKe
14bd9 79 2c 20 6f 72 20 6c 61 73 74 20 69 6e 74 65 67 y, or last integ
14bda 65 72 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 er key */. int
14bdb 73 6b 69 70 4e 65 78 74 3b 20 20 20 20 2f 2a 20 skipNext; /*
14bdc 50 72 65 76 28 29 20 69 73 20 6e 6f 6f 70 20 69 Prev() is noop i
14bdd 66 20 6e 65 67 61 74 69 76 65 2e 20 4e 65 78 74 f negative. Next
14bde 28 29 20 69 73 20 6e 6f 6f 70 20 69 66 20 70 6f () is noop if po
14bdf 73 69 74 69 76 65 20 2a 2f 0a 23 69 66 6e 64 65 sitive */.#ifnde
14be0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
14be1 43 52 42 4c 4f 42 0a 20 20 75 38 20 69 73 49 6e CRBLOB. u8 isIn
14be2 63 72 62 6c 6f 62 48 61 6e 64 6c 65 3b 20 20 20 crblobHandle;
14be3 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
14be4 69 73 20 63 75 72 73 6f 72 20 69 73 20 61 6e 20 is cursor is an
14be5 69 6e 63 72 2e 20 69 6f 20 68 61 6e 64 6c 65 20 incr. io handle
14be6 2a 2f 0a 20 20 50 67 6e 6f 20 2a 61 4f 76 65 72 */. Pgno *aOver
14be7 66 6c 6f 77 3b 20 20 20 20 20 20 20 20 20 20 2f flow; /
14be8 2a 20 43 61 63 68 65 20 6f 66 20 6f 76 65 72 66 * Cache of overf
14be9 6c 6f 77 20 70 61 67 65 20 6c 6f 63 61 74 69 6f low page locatio
14bea 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 ns */.#endif. i
14beb 31 36 20 69 50 61 67 65 3b 20 20 20 20 20 20 20 16 iPage;
14bec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14bed 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
14bee 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 69 6e current page in
14bef 20 61 70 50 61 67 65 20 2a 2f 0a 20 20 4d 65 6d apPage */. Mem
14bf0 50 61 67 65 20 2a 61 70 50 61 67 65 5b 42 54 43 Page *apPage[BTC
14bf1 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d URSOR_MAX_DEPTH]
14bf2 3b 20 20 2f 2a 20 50 61 67 65 73 20 66 72 6f 6d ; /* Pages from
14bf3 20 72 6f 6f 74 20 74 6f 20 63 75 72 72 65 6e 74 root to current
14bf4 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 61 page */. u16 a
14bf5 69 49 64 78 5b 42 54 43 55 52 53 4f 52 5f 4d 41 iIdx[BTCURSOR_MA
14bf6 58 5f 44 45 50 54 48 5d 3b 20 20 20 20 20 20 20 X_DEPTH];
14bf7 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 6e 64 65 /* Current inde
14bf8 78 20 69 6e 20 61 70 50 61 67 65 5b 69 5d 20 2a x in apPage[i] *
14bf9 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 74 65 /.};../*.** Pote
14bfa 6e 74 69 61 6c 20 76 61 6c 75 65 73 20 66 6f 72 ntial values for
14bfb 20 42 74 43 75 72 73 6f 72 2e 65 53 74 61 74 65 BtCursor.eState
14bfc 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 56 ..**.** CURSOR_V
14bfd 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 75 72 73 6f ALID:.** Curso
14bfe 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 76 61 r points to a va
14bff 6c 69 64 20 65 6e 74 72 79 2e 20 67 65 74 50 61 lid entry. getPa
14c00 79 6c 6f 61 64 28 29 20 65 74 63 2e 20 6d 61 79 yload() etc. may
14c01 20 62 65 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a be called..**.*
14c02 2a 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 * CURSOR_INVALID
14c03 3a 0a 2a 2a 20 20 20 43 75 72 73 6f 72 20 64 6f :.** Cursor do
14c04 65 73 20 6e 6f 74 20 70 6f 69 6e 74 20 74 6f 20 es not point to
14c05 61 20 76 61 6c 69 64 20 65 6e 74 72 79 2e 20 54 a valid entry. T
14c06 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20 28 his can happen (
14c07 66 6f 72 20 65 78 61 6d 70 6c 65 29 20 0a 2a 2a for example) .**
14c08 20 20 20 62 65 63 61 75 73 65 20 74 68 65 20 74 because the t
14c09 61 62 6c 65 20 69 73 20 65 6d 70 74 79 20 6f 72 able is empty or
14c0a 20 62 65 63 61 75 73 65 20 42 74 72 65 65 43 75 because BtreeCu
14c0b 72 73 6f 72 46 69 72 73 74 28 29 20 68 61 73 20 rsorFirst() has
14c0c 6e 6f 74 20 62 65 65 6e 0a 2a 2a 20 20 20 63 61 not been.** ca
14c0d 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 lled..**.** CURS
14c0e 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 3a 0a OR_REQUIRESEEK:.
14c0f 2a 2a 20 20 20 54 68 65 20 74 61 62 6c 65 20 74 ** The table t
14c10 68 61 74 20 74 68 69 73 20 63 75 72 73 6f 72 20 hat this cursor
14c11 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 73 74 was opened on st
14c12 69 6c 6c 20 65 78 69 73 74 73 2c 20 62 75 74 20 ill exists, but
14c13 68 61 73 20 62 65 65 6e 20 0a 2a 2a 20 20 20 6d has been .** m
14c14 6f 64 69 66 69 65 64 20 73 69 6e 63 65 20 74 68 odified since th
14c15 65 20 63 75 72 73 6f 72 20 77 61 73 20 6c 61 73 e cursor was las
14c16 74 20 75 73 65 64 2e 20 54 68 65 20 63 75 72 73 t used. The curs
14c17 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69 73 20 73 or position is s
14c18 61 76 65 64 0a 2a 2a 20 20 20 69 6e 20 76 61 72 aved.** in var
14c19 69 61 62 6c 65 73 20 42 74 43 75 72 73 6f 72 2e iables BtCursor.
14c1a 70 4b 65 79 20 61 6e 64 20 42 74 43 75 72 73 6f pKey and BtCurso
14c1b 72 2e 6e 4b 65 79 2e 20 57 68 65 6e 20 61 20 63 r.nKey. When a c
14c1c 75 72 73 6f 72 20 69 73 20 69 6e 20 0a 2a 2a 20 ursor is in .**
14c1d 20 20 74 68 69 73 20 73 74 61 74 65 2c 20 72 65 this state, re
14c1e 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 storeCursorPosit
14c1f 69 6f 6e 28 29 20 63 61 6e 20 62 65 20 63 61 6c ion() can be cal
14c20 6c 65 64 20 74 6f 20 61 74 74 65 6d 70 74 20 74 led to attempt t
14c21 6f 0a 2a 2a 20 20 20 73 65 65 6b 20 74 68 65 20 o.** seek the
14c22 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 73 61 cursor to the sa
14c23 76 65 64 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a ved position..**
14c24 0a 2a 2a 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 .** CURSOR_FAULT
14c25 3a 0a 2a 2a 20 20 20 41 20 75 6e 72 65 63 6f 76 :.** A unrecov
14c26 65 72 61 62 6c 65 20 65 72 72 6f 72 20 28 61 6e erable error (an
14c27 20 49 2f 4f 20 65 72 72 6f 72 20 6f 72 20 61 20 I/O error or a
14c28 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 29 20 malloc failure)
14c29 68 61 73 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20 has occurred.**
14c2a 20 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74 on a different
14c2b 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 connection that
14c2c 20 73 68 61 72 65 73 20 74 68 65 20 42 74 53 68 shares the BtSh
14c2d 61 72 65 64 20 63 61 63 68 65 20 77 69 74 68 20 ared cache with
14c2e 74 68 69 73 0a 2a 2a 20 20 20 63 75 72 73 6f 72 this.** cursor
14c2f 2e 20 20 54 68 65 20 65 72 72 6f 72 20 68 61 73 . The error has
14c30 20 6c 65 66 74 20 74 68 65 20 63 61 63 68 65 20 left the cache
14c31 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 in an inconsiste
14c32 6e 74 20 73 74 61 74 65 2e 0a 2a 2a 20 20 20 44 nt state..** D
14c33 6f 20 6e 6f 74 68 69 6e 67 20 65 6c 73 65 20 77 o nothing else w
14c34 69 74 68 20 74 68 69 73 20 63 75 72 73 6f 72 2e ith this cursor.
14c35 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f Any attempt to
14c36 20 75 73 65 20 74 68 65 20 63 75 72 73 6f 72 0a use the cursor.
14c37 2a 2a 20 20 20 73 68 6f 75 6c 64 20 72 65 74 75 ** should retu
14c38 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 rn the error cod
14c39 65 20 73 74 6f 72 65 64 20 69 6e 20 42 74 43 75 e stored in BtCu
14c3a 72 73 6f 72 2e 73 6b 69 70 0a 2a 2f 0a 23 64 65 rsor.skip.*/.#de
14c3b 66 69 6e 65 20 43 55 52 53 4f 52 5f 49 4e 56 41 fine CURSOR_INVA
14c3c 4c 49 44 20 20 20 20 20 20 20 20 20 20 20 30 0a LID 0.
14c3d 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 56 #define CURSOR_V
14c3e 41 4c 49 44 20 20 20 20 20 20 20 20 20 20 20 20 ALID
14c3f 20 31 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 1.#define CURSO
14c40 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 20 20 R_REQUIRESEEK
14c41 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 43 55 2.#define CU
14c42 52 53 4f 52 5f 46 41 55 4c 54 20 20 20 20 20 20 RSOR_FAULT
14c43 20 20 20 20 20 20 20 33 0a 0a 2f 2a 20 0a 2a 2a 3../* .**
14c44 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 The database pa
14c45 67 65 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 42 ge the PENDING_B
14c46 59 54 45 20 6f 63 63 75 70 69 65 73 2e 20 54 68 YTE occupies. Th
14c47 69 73 20 70 61 67 65 20 69 73 20 6e 65 76 65 72 is page is never
14c48 20 75 73 65 64 2e 0a 2a 2f 0a 23 20 64 65 66 69 used..*/.# defi
14c49 6e 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f ne PENDING_BYTE_
14c4a 50 41 47 45 28 70 42 74 29 20 50 41 47 45 52 5f PAGE(pBt) PAGER_
14c4b 4d 4a 5f 50 47 4e 4f 28 70 42 74 29 0a 0a 2f 2a MJ_PGNO(pBt)../*
14c4c 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 .** These macros
14c4d 20 64 65 66 69 6e 65 20 74 68 65 20 6c 6f 63 61 define the loca
14c4e 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 6f 69 6e tion of the poin
14c4f 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f ter-map entry fo
14c50 72 20 61 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 r a .** database
14c51 20 70 61 67 65 2e 20 54 68 65 20 66 69 72 73 74 page. The first
14c52 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 65 61 63 argument to eac
14c53 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 h is the number
14c54 6f 66 20 75 73 61 62 6c 65 0a 2a 2a 20 62 79 74 of usable.** byt
14c55 65 73 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 es on each page
14c56 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
14c57 28 6f 66 74 65 6e 20 31 30 32 34 29 2e 20 54 68 (often 1024). Th
14c58 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 0a e second is the.
14c59 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 ** page number t
14c5a 6f 20 6c 6f 6f 6b 20 75 70 20 69 6e 20 74 68 65 o look up in the
14c5b 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a pointer map..**
14c5c 0a 2a 2a 20 50 54 52 4d 41 50 5f 50 41 47 45 4e .** PTRMAP_PAGEN
14c5d 4f 20 72 65 74 75 72 6e 73 20 74 68 65 20 64 61 O returns the da
14c5e 74 61 62 61 73 65 20 70 61 67 65 20 6e 75 6d 62 tabase page numb
14c5f 65 72 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 er of the pointe
14c60 72 2d 6d 61 70 0a 2a 2a 20 70 61 67 65 20 74 68 r-map.** page th
14c61 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 72 65 at stores the re
14c62 71 75 69 72 65 64 20 70 6f 69 6e 74 65 72 2e 20 quired pointer.
14c63 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 PTRMAP_PTROFFSET
14c64 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 returns.** the
14c65 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 72 65 offset of the re
14c66 71 75 65 73 74 65 64 20 6d 61 70 20 65 6e 74 72 quested map entr
14c67 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 y..**.** If the
14c68 70 67 6e 6f 20 61 72 67 75 6d 65 6e 74 20 70 61 pgno argument pa
14c69 73 73 65 64 20 74 6f 20 50 54 52 4d 41 50 5f 50 ssed to PTRMAP_P
14c6a 41 47 45 4e 4f 20 69 73 20 61 20 70 6f 69 6e 74 AGENO is a point
14c6b 65 72 2d 6d 61 70 20 70 61 67 65 2c 0a 2a 2a 20 er-map page,.**
14c6c 74 68 65 6e 20 70 67 6e 6f 20 69 73 20 72 65 74 then pgno is ret
14c6d 75 72 6e 65 64 2e 20 53 6f 20 28 70 67 6e 6f 3d urned. So (pgno=
14c6e 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 =PTRMAP_PAGENO(p
14c6f 67 73 7a 2c 20 70 67 6e 6f 29 29 20 63 61 6e 20 gsz, pgno)) can
14c70 62 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 74 65 be.** used to te
14c71 73 74 20 69 66 20 70 67 6e 6f 20 69 73 20 61 20 st if pgno is a
14c72 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 pointer-map page
14c73 2e 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 20 . PTRMAP_ISPAGE
14c74 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 implements.** th
14c75 69 73 20 74 65 73 74 2e 0a 2a 2f 0a 23 64 65 66 is test..*/.#def
14c76 69 6e 65 20 50 54 52 4d 41 50 5f 50 41 47 45 4e ine PTRMAP_PAGEN
14c77 4f 28 70 42 74 2c 20 70 67 6e 6f 29 20 70 74 72 O(pBt, pgno) ptr
14c78 6d 61 70 50 61 67 65 6e 6f 28 70 42 74 2c 20 70 mapPageno(pBt, p
14c79 67 6e 6f 29 0a 23 64 65 66 69 6e 65 20 50 54 52 gno).#define PTR
14c7a 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 70 67 MAP_PTROFFSET(pg
14c7b 70 74 72 6d 61 70 2c 20 70 67 6e 6f 29 20 28 35 ptrmap, pgno) (5
14c7c 2a 28 70 67 6e 6f 2d 70 67 70 74 72 6d 61 70 2d *(pgno-pgptrmap-
14c7d 31 29 29 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 1)).#define PTRM
14c7e 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 70 AP_ISPAGE(pBt, p
14c7f 67 6e 6f 29 20 28 50 54 52 4d 41 50 5f 50 41 47 gno) (PTRMAP_PAG
14c80 45 4e 4f 28 28 70 42 74 29 2c 28 70 67 6e 6f 29 ENO((pBt),(pgno)
14c81 29 3d 3d 28 70 67 6e 6f 29 29 0a 0a 2f 2a 0a 2a )==(pgno))../*.*
14c82 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 * The pointer ma
14c83 70 20 69 73 20 61 20 6c 6f 6f 6b 75 70 20 74 61 p is a lookup ta
14c84 62 6c 65 20 74 68 61 74 20 69 64 65 6e 74 69 66 ble that identif
14c85 69 65 73 20 74 68 65 20 70 61 72 65 6e 74 20 70 ies the parent p
14c86 61 67 65 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 age for.** each
14c87 63 68 69 6c 64 20 70 61 67 65 20 69 6e 20 74 68 child page in th
14c88 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
14c89 20 20 54 68 65 20 70 61 72 65 6e 74 20 70 61 67 The parent pag
14c8a 65 20 69 73 20 74 68 65 20 70 61 67 65 20 74 68 e is the page th
14c8b 61 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 61 at.** contains a
14c8c 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
14c8d 63 68 69 6c 64 2e 20 20 45 76 65 72 79 20 70 61 child. Every pa
14c8e 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ge in the databa
14c8f 73 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 30 se contains.** 0
14c90 20 6f 72 20 31 20 70 61 72 65 6e 74 20 70 61 67 or 1 parent pag
14c91 65 73 2e 20 20 28 49 6e 20 74 68 69 73 20 63 6f es. (In this co
14c92 6e 74 65 78 74 20 27 64 61 74 61 62 61 73 65 20 ntext 'database
14c93 70 61 67 65 27 20 72 65 66 65 72 73 0a 2a 2a 20 page' refers.**
14c94 74 6f 20 61 6e 79 20 70 61 67 65 20 74 68 61 74 to any page that
14c95 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 is not part of
14c96 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 the pointer map
14c97 69 74 73 65 6c 66 2e 29 20 20 45 61 63 68 20 70 itself.) Each p
14c98 6f 69 6e 74 65 72 20 6d 61 70 0a 2a 2a 20 65 6e ointer map.** en
14c99 74 72 79 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 try consists of
14c9a 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 27 74 a single byte 't
14c9b 79 70 65 27 20 61 6e 64 20 61 20 34 20 62 79 74 ype' and a 4 byt
14c9c 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 e parent page nu
14c9d 6d 62 65 72 2e 0a 2a 2a 20 54 68 65 20 50 54 52 mber..** The PTR
14c9e 4d 41 50 5f 58 58 58 20 69 64 65 6e 74 69 66 69 MAP_XXX identifi
14c9f 65 72 73 20 62 65 6c 6f 77 20 61 72 65 20 74 68 ers below are th
14ca0 65 20 76 61 6c 69 64 20 74 79 70 65 73 2e 0a 2a e valid types..*
14ca1 2a 0a 2a 2a 20 54 68 65 20 70 75 72 70 6f 73 65 *.** The purpose
14ca2 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 20 of the pointer
14ca3 6d 61 70 20 69 73 20 74 6f 20 66 61 63 69 6c 69 map is to facili
14ca4 74 79 20 6d 6f 76 69 6e 67 20 70 61 67 65 73 20 ty moving pages
14ca5 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 70 6f 73 69 from one.** posi
14ca6 74 69 6f 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 tion in the file
14ca7 20 74 6f 20 61 6e 6f 74 68 65 72 20 61 73 20 70 to another as p
14ca8 61 72 74 20 6f 66 20 61 75 74 6f 76 61 63 75 75 art of autovacuu
14ca9 6d 2e 20 20 57 68 65 6e 20 61 20 70 61 67 65 0a m. When a page.
14caa 2a 2a 20 69 73 20 6d 6f 76 65 64 2c 20 74 68 65 ** is moved, the
14cab 20 70 6f 69 6e 74 65 72 20 69 6e 20 69 74 73 20 pointer in its
14cac 70 61 72 65 6e 74 20 6d 75 73 74 20 62 65 20 75 parent must be u
14cad 70 64 61 74 65 64 20 74 6f 20 70 6f 69 6e 74 20 pdated to point
14cae 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 6f to the.** new lo
14caf 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 70 6f 69 cation. The poi
14cb0 6e 74 65 72 20 6d 61 70 20 69 73 20 75 73 65 64 nter map is used
14cb1 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 70 to locate the p
14cb2 61 72 65 6e 74 20 70 61 67 65 20 71 75 69 63 6b arent page quick
14cb3 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 ly..**.** PTRMAP
14cb4 5f 52 4f 4f 54 50 41 47 45 3a 20 54 68 65 20 64 _ROOTPAGE: The d
14cb5 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20 atabase page is
14cb6 61 20 72 6f 6f 74 2d 70 61 67 65 2e 20 54 68 65 a root-page. The
14cb7 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 page-number is
14cb8 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 not.**
14cb9 20 20 20 20 20 20 20 20 75 73 65 64 20 69 6e 20 used in
14cba 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a this case..**.**
14cbb 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 PTRMAP_FREEPAGE
14cbc 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 : The database p
14cbd 61 67 65 20 69 73 20 61 6e 20 75 6e 75 73 65 64 age is an unused
14cbe 20 28 66 72 65 65 29 20 70 61 67 65 2e 20 54 68 (free) page. Th
14cbf 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 0a 2a e page-number .*
14cc0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
14cc1 20 20 20 69 73 20 6e 6f 74 20 75 73 65 64 20 69 is not used i
14cc2 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a n this case..**.
14cc3 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c ** PTRMAP_OVERFL
14cc4 4f 57 31 3a 20 54 68 65 20 64 61 74 61 62 61 73 OW1: The databas
14cc5 65 20 70 61 67 65 20 69 73 20 74 68 65 20 66 69 e page is the fi
14cc6 72 73 74 20 70 61 67 65 20 69 6e 20 61 20 6c 69 rst page in a li
14cc7 73 74 20 6f 66 20 0a 2a 2a 20 20 20 20 20 20 20 st of .**
14cc8 20 20 20 20 20 20 20 20 20 20 20 20 6f 76 65 72 over
14cc9 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20 flow pages. The
14cca 70 61 67 65 20 6e 75 6d 62 65 72 20 69 64 65 6e page number iden
14ccb 74 69 66 69 65 73 20 74 68 65 20 70 61 67 65 20 tifies the page
14ccc 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 that.**
14ccd 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 61 69 contai
14cce 6e 73 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68 ns the cell with
14ccf 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
14cd0 69 73 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 is overflow page
14cd1 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f ..**.** PTRMAP_O
14cd2 56 45 52 46 4c 4f 57 32 3a 20 54 68 65 20 64 61 VERFLOW2: The da
14cd3 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20 74 tabase page is t
14cd4 68 65 20 73 65 63 6f 6e 64 20 6f 72 20 6c 61 74 he second or lat
14cd5 65 72 20 70 61 67 65 20 69 6e 20 61 20 6c 69 73 er page in a lis
14cd6 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 t of.**
14cd7 20 20 20 20 20 20 20 20 20 20 6f 76 65 72 66 6c overfl
14cd8 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20 70 61 ow pages. The pa
14cd9 67 65 2d 6e 75 6d 62 65 72 20 69 64 65 6e 74 69 ge-number identi
14cda 66 69 65 73 20 74 68 65 20 70 72 65 76 69 6f 75 fies the previou
14cdb 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 s.**
14cdc 20 20 20 20 20 20 20 70 61 67 65 20 69 6e 20 74 page in t
14cdd 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 he overflow page
14cde 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 list..**.** PTR
14cdf 4d 41 50 5f 42 54 52 45 45 3a 20 54 68 65 20 64 MAP_BTREE: The d
14ce0 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20 atabase page is
14ce1 61 20 6e 6f 6e 2d 72 6f 6f 74 20 62 74 72 65 65 a non-root btree
14ce2 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 20 page. The page
14ce3 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20 20 number.**
14ce4 20 20 20 20 20 20 20 20 69 64 65 6e 74 69 66 69 identifi
14ce5 65 73 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 es the parent pa
14ce6 67 65 20 69 6e 20 74 68 65 20 62 74 72 65 65 2e ge in the btree.
14ce7 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 52 4d .*/.#define PTRM
14ce8 41 50 5f 52 4f 4f 54 50 41 47 45 20 31 0a 23 64 AP_ROOTPAGE 1.#d
14ce9 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 46 52 45 efine PTRMAP_FRE
14cea 45 50 41 47 45 20 32 0a 23 64 65 66 69 6e 65 20 EPAGE 2.#define
14ceb 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 PTRMAP_OVERFLOW1
14cec 20 33 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 3.#define PTRMA
14ced 50 5f 4f 56 45 52 46 4c 4f 57 32 20 34 0a 23 64 P_OVERFLOW2 4.#d
14cee 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 42 54 52 efine PTRMAP_BTR
14cef 45 45 20 35 0a 0a 2f 2a 20 41 20 62 75 6e 63 68 EE 5../* A bunch
14cf0 20 6f 66 20 61 73 73 65 72 74 28 29 20 73 74 61 of assert() sta
14cf1 74 65 6d 65 6e 74 73 20 74 6f 20 63 68 65 63 6b tements to check
14cf2 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
14cf3 20 73 74 61 74 65 20 76 61 72 69 61 62 6c 65 73 state variables
14cf4 0a 2a 2a 20 6f 66 20 68 61 6e 64 6c 65 20 70 20 .** of handle p
14cf5 28 74 79 70 65 20 42 74 72 65 65 2a 29 20 61 72 (type Btree*) ar
14cf6 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 63 6f 6e e internally con
14cf7 73 69 73 74 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 sistent..*/.#def
14cf8 69 6e 65 20 62 74 72 65 65 49 6e 74 65 67 72 69 ine btreeIntegri
14cf9 74 79 28 70 29 20 5c 0a 20 20 61 73 73 65 72 74 ty(p) \. assert
14cfa 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e ( p->pBt->inTran
14cfb 73 61 63 74 69 6f 6e 21 3d 54 52 41 4e 53 5f 4e saction!=TRANS_N
14cfc 4f 4e 45 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e 6e ONE || p->pBt->n
14cfd 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 30 20 29 Transaction==0 )
14cfe 3b 20 5c 0a 20 20 61 73 73 65 72 74 28 20 70 2d ; \. assert( p-
14cff 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 >pBt->inTransact
14d00 69 6f 6e 3e 3d 70 2d 3e 69 6e 54 72 61 6e 73 20 ion>=p->inTrans
14d01 29 3b 20 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ); .../*.** The
14d02 49 53 41 55 54 4f 56 41 43 55 55 4d 20 6d 61 63 ISAUTOVACUUM mac
14d03 72 6f 20 69 73 20 75 73 65 64 20 77 69 74 68 69 ro is used withi
14d04 6e 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f n balance_nonroo
14d05 74 28 29 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 t() to determine
14d06 0a 2a 2a 20 69 66 20 74 68 65 20 64 61 74 61 62 .** if the datab
14d07 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 ase supports aut
14d08 6f 2d 76 61 63 75 75 6d 20 6f 72 20 6e 6f 74 2e o-vacuum or not.
14d09 20 42 65 63 61 75 73 65 20 69 74 20 69 73 20 75 Because it is u
14d0a 73 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 61 6e sed.** within an
14d0b 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 expression that
14d0c 20 69 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 is an argument
14d0d 74 6f 20 61 6e 6f 74 68 65 72 20 6d 61 63 72 6f to another macro
14d0e 20 0a 2a 2a 20 28 73 71 6c 69 74 65 4d 61 6c 6c .** (sqliteMall
14d0f 6f 63 52 61 77 29 2c 20 69 74 20 69 73 20 6e 6f ocRaw), it is no
14d10 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 t possible to us
14d11 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 63 6f e conditional co
14d12 6d 70 69 6c 61 74 69 6f 6e 2e 0a 2a 2a 20 53 6f mpilation..** So
14d13 2c 20 74 68 69 73 20 6d 61 63 72 6f 20 69 73 20 , this macro is
14d14 64 65 66 69 6e 65 64 20 69 6e 73 74 65 61 64 2e defined instead.
14d15 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
14d16 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
14d17 55 4d 0a 23 64 65 66 69 6e 65 20 49 53 41 55 54 UM.#define ISAUT
14d18 4f 56 41 43 55 55 4d 20 28 70 42 74 2d 3e 61 75 OVACUUM (pBt->au
14d19 74 6f 56 61 63 75 75 6d 29 0a 23 65 6c 73 65 0a toVacuum).#else.
14d1a 23 64 65 66 69 6e 65 20 49 53 41 55 54 4f 56 41 #define ISAUTOVA
14d1b 43 55 55 4d 20 30 0a 23 65 6e 64 69 66 0a 0a 0a CUUM 0.#endif...
14d1c 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 /*.** This struc
14d1d 74 75 72 65 20 69 73 20 70 61 73 73 65 64 20 61 ture is passed a
14d1e 72 6f 75 6e 64 20 74 68 72 6f 75 67 68 20 61 6c round through al
14d1f 6c 20 74 68 65 20 73 61 6e 69 74 79 20 63 68 65 l the sanity che
14d20 63 6b 69 6e 67 20 72 6f 75 74 69 6e 65 73 0a 2a cking routines.*
14d21 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 * in order to ke
14d22 65 70 20 74 72 61 63 6b 20 6f 66 20 73 6f 6d 65 ep track of some
14d23 20 67 6c 6f 62 61 6c 20 73 74 61 74 65 20 69 6e global state in
14d24 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 formation..*/.ty
14d25 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e 74 pedef struct Int
14d26 65 67 72 69 74 79 43 6b 20 49 6e 74 65 67 72 69 egrityCk Integri
14d27 74 79 43 6b 3b 0a 73 74 72 75 63 74 20 49 6e 74 tyCk;.struct Int
14d28 65 67 72 69 74 79 43 6b 20 7b 0a 20 20 42 74 53 egrityCk {. BtS
14d29 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 2f hared *pBt; /
14d2a 2a 20 54 68 65 20 74 72 65 65 20 62 65 69 6e 67 * The tree being
14d2b 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a checked out */.
14d2c 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b Pager *pPager;
14d2d 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73 6f 63 /* The assoc
14d2e 69 61 74 65 64 20 70 61 67 65 72 2e 20 20 41 6c iated pager. Al
14d2f 73 6f 20 61 63 63 65 73 73 69 62 6c 65 20 62 79 so accessible by
14d30 20 70 42 74 2d 3e 70 50 61 67 65 72 20 2a 2f 0a pBt->pPager */.
14d31 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20 20 20 Pgno nPage;
14d32 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
14d33 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 pages in the da
14d34 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 tabase */. int
14d35 2a 61 6e 52 65 66 3b 20 20 20 20 20 20 20 2f 2a *anRef; /*
14d36 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 Number of times
14d37 20 65 61 63 68 20 70 61 67 65 20 69 73 20 72 65 each page is re
14d38 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 69 6e ferenced */. in
14d39 74 20 6d 78 45 72 72 3b 20 20 20 20 20 20 20 20 t mxErr;
14d3a 2f 2a 20 53 74 6f 70 20 61 63 63 75 6d 75 6c 61 /* Stop accumula
14d3b 74 69 6e 67 20 65 72 72 6f 72 73 20 77 68 65 6e ting errors when
14d3c 20 74 68 69 73 20 72 65 61 63 68 65 73 20 7a 65 this reaches ze
14d3d 72 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 ro */. int nErr
14d3e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d ; /* Num
14d3f 62 65 72 20 6f 66 20 6d 65 73 73 61 67 65 73 20 ber of messages
14d40 77 72 69 74 74 65 6e 20 74 6f 20 7a 45 72 72 4d written to zErrM
14d41 73 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 sg so far */. i
14d42 6e 74 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b nt mallocFailed;
14d43 20 2f 2a 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c /* A memory all
14d44 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 68 61 ocation error ha
14d45 73 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 20 20 s occurred */.
14d46 53 74 72 41 63 63 75 6d 20 65 72 72 4d 73 67 3b StrAccum errMsg;
14d47 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 /* Accumulate
14d48 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 the error messag
14d49 65 20 74 65 78 74 20 68 65 72 65 20 2a 2f 0a 7d e text here */.}
14d4a 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f 72 ;../*.** Read or
14d4b 20 77 72 69 74 65 20 61 20 74 77 6f 2d 20 61 6e write a two- an
14d4c 64 20 66 6f 75 72 2d 62 79 74 65 20 62 69 67 2d d four-byte big-
14d4d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 76 endian integer v
14d4e 61 6c 75 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e alues..*/.#defin
14d4f 65 20 67 65 74 32 62 79 74 65 28 78 29 20 20 20 e get2byte(x)
14d50 28 28 78 29 5b 30 5d 3c 3c 38 20 7c 20 28 78 29 ((x)[0]<<8 | (x)
14d51 5b 31 5d 29 0a 23 64 65 66 69 6e 65 20 70 75 74 [1]).#define put
14d52 32 62 79 74 65 28 70 2c 76 29 20 28 28 70 29 5b 2byte(p,v) ((p)[
14d53 30 5d 20 3d 20 28 75 38 29 28 28 76 29 3e 3e 38 0] = (u8)((v)>>8
14d54 29 2c 20 28 70 29 5b 31 5d 20 3d 20 28 75 38 29 ), (p)[1] = (u8)
14d55 28 76 29 29 0a 23 64 65 66 69 6e 65 20 67 65 74 (v)).#define get
14d56 34 62 79 74 65 20 73 71 6c 69 74 65 33 47 65 74 4byte sqlite3Get
14d57 34 62 79 74 65 0a 23 64 65 66 69 6e 65 20 70 75 4byte.#define pu
14d58 74 34 62 79 74 65 20 73 71 6c 69 74 65 33 50 75 t4byte sqlite3Pu
14d59 74 34 62 79 74 65 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a t4byte../*******
14d5a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 ******* End of b
14d5b 74 72 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a treeInt.h ******
14d5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14d5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14d5e 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
14d5f 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
14d60 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
14d61 20 6f 66 66 20 69 6e 20 62 74 6d 75 74 65 78 2e off in btmutex.
14d62 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
14d63 2a 2a 2a 2a 2a 2a 2f 0a 23 69 66 6e 64 65 66 20 ******/.#ifndef
14d64 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
14d65 45 44 5f 43 41 43 48 45 0a 23 69 66 20 53 51 4c ED_CACHE.#if SQL
14d66 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 0a ITE_THREADSAFE..
14d67 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 /*.** Obtain the
14d68 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 20 BtShared mutex
14d69 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
14d6a 42 2d 54 72 65 65 20 68 61 6e 64 6c 65 20 70 2e B-Tree handle p.
14d6b 20 41 6c 73 6f 2c 0a 2a 2a 20 73 65 74 20 42 74 Also,.** set Bt
14d6c 53 68 61 72 65 64 2e 64 62 20 74 6f 20 74 68 65 Shared.db to the
14d6d 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
14d6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
14d6f 20 70 20 61 6e 64 20 74 68 65 0a 2a 2a 20 70 2d p and the.** p-
14d70 3e 6c 6f 63 6b 65 64 20 62 6f 6f 6c 65 61 6e 20 >locked boolean
14d71 74 6f 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 to true..*/.stat
14d72 69 63 20 76 6f 69 64 20 6c 6f 63 6b 42 74 72 65 ic void lockBtre
14d73 65 4d 75 74 65 78 28 42 74 72 65 65 20 2a 70 29 eMutex(Btree *p)
14d74 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c {. assert( p->l
14d75 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 61 73 ocked==0 );. as
14d76 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
14d77 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70 2d 3e 70 tex_notheld(p->p
14d78 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
14d79 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
14d7a 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 mutex_held(p->db
14d7b 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 73 ->mutex) );.. s
14d7c 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
14d7d 65 72 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 er(p->pBt->mutex
14d7e 29 3b 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 );. p->pBt->db
14d7f 3d 20 70 2d 3e 64 62 3b 0a 20 20 70 2d 3e 6c 6f = p->db;. p->lo
14d80 63 6b 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a cked = 1;.}../*.
14d81 2a 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 42 ** Release the B
14d82 74 53 68 61 72 65 64 20 6d 75 74 65 78 20 61 73 tShared mutex as
14d83 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 42 2d sociated with B-
14d84 54 72 65 65 20 68 61 6e 64 6c 65 20 70 20 61 6e Tree handle p an
14d85 64 0a 2a 2a 20 63 6c 65 61 72 20 74 68 65 20 70 d.** clear the p
14d86 2d 3e 6c 6f 63 6b 65 64 20 62 6f 6f 6c 65 61 6e ->locked boolean
14d87 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
14d88 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 unlockBtreeMute
14d89 78 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 x(Btree *p){. a
14d8a 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 ssert( p->locked
14d8b 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==1 );. assert(
14d8c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
14d8d 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 eld(p->pBt->mute
14d8e 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
14d8f 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
14d90 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ld(p->db->mutex)
14d91 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
14d92 3e 64 62 3d 3d 70 2d 3e 70 42 74 2d 3e 64 62 20 >db==p->pBt->db
14d93 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 );.. sqlite3_mu
14d94 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 42 74 tex_leave(p->pBt
14d95 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6c ->mutex);. p->l
14d96 6f 63 6b 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a ocked = 0;.}../*
14d97 0a 2a 2a 20 45 6e 74 65 72 20 61 20 6d 75 74 65 .** Enter a mute
14d98 78 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 42 x on the given B
14d99 54 72 65 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a Tree object..**.
14d9a 2a 2a 20 49 66 20 74 68 65 20 6f 62 6a 65 63 74 ** If the object
14d9b 20 69 73 20 6e 6f 74 20 73 68 61 72 61 62 6c 65 is not sharable
14d9c 2c 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 20 , then no mutex
14d9d 69 73 20 65 76 65 72 20 72 65 71 75 69 72 65 64 is ever required
14d9e 0a 2a 2a 20 61 6e 64 20 74 68 69 73 20 72 6f 75 .** and this rou
14d9f 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tine is a no-op.
14da0 20 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 The underlying
14da1 20 6d 75 74 65 78 20 69 73 20 6e 6f 6e 2d 72 65 mutex is non-re
14da2 63 75 72 73 69 76 65 2e 0a 2a 2a 20 42 75 74 20 cursive..** But
14da3 77 65 20 6b 65 65 70 20 61 20 72 65 66 65 72 65 we keep a refere
14da4 6e 63 65 20 63 6f 75 6e 74 20 69 6e 20 42 74 72 nce count in Btr
14da5 65 65 2e 77 61 6e 74 54 6f 4c 6f 63 6b 20 73 6f ee.wantToLock so
14da6 20 74 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a the behavior.**
14da7 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61 of this interfa
14da8 63 65 20 69 73 20 72 65 63 75 72 73 69 76 65 2e ce is recursive.
14da9 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 .**.** To avoid
14daa 64 65 61 64 6c 6f 63 6b 73 2c 20 6d 75 6c 74 69 deadlocks, multi
14dab 70 6c 65 20 42 74 72 65 65 73 20 61 72 65 20 6c ple Btrees are l
14dac 6f 63 6b 65 64 20 69 6e 20 74 68 65 20 73 61 6d ocked in the sam
14dad 65 20 6f 72 64 65 72 0a 2a 2a 20 62 79 20 61 6c e order.** by al
14dae 6c 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 l database conne
14daf 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 70 2d 3e ctions. The p->
14db0 70 4e 65 78 74 20 69 73 20 61 20 6c 69 73 74 20 pNext is a list
14db1 6f 66 20 6f 74 68 65 72 0a 2a 2a 20 42 74 72 65 of other.** Btre
14db2 65 73 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 es belonging to
14db3 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
14db4 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 e connection as
14db5 74 68 65 20 70 20 42 74 72 65 65 0a 2a 2a 20 77 the p Btree.** w
14db6 68 69 63 68 20 6e 65 65 64 20 74 6f 20 62 65 20 hich need to be
14db7 6c 6f 63 6b 65 64 20 61 66 74 65 72 20 70 2e 20 locked after p.
14db8 20 49 66 20 77 65 20 63 61 6e 6e 6f 74 20 67 65 If we cannot ge
14db9 74 20 61 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20 70 t a lock on.** p
14dba 2c 20 74 68 65 6e 20 66 69 72 73 74 20 75 6e 6c , then first unl
14dbb 6f 63 6b 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f ock all of the o
14dbc 74 68 65 72 73 20 6f 6e 20 70 2d 3e 70 4e 65 78 thers on p->pNex
14dbd 74 2c 20 74 68 65 6e 20 77 61 69 74 0a 2a 2a 20 t, then wait.**
14dbe 66 6f 72 20 74 68 65 20 6c 6f 63 6b 20 74 6f 20 for the lock to
14dbf 62 65 63 6f 6d 65 20 61 76 61 69 6c 61 62 6c 65 become available
14dc0 20 6f 6e 20 70 2c 20 74 68 65 6e 20 72 65 6c 6f on p, then relo
14dc1 63 6b 20 61 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a ck all of the.**
14dc2 20 73 75 62 73 65 71 75 65 6e 74 20 42 74 72 65 subsequent Btre
14dc3 65 73 20 74 68 61 74 20 64 65 73 69 72 65 20 61 es that desire a
14dc4 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 lock..*/.SQLITE
14dc5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
14dc6 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
14dc7 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 72 Btree *p){. Btr
14dc8 65 65 20 2a 70 4c 61 74 65 72 3b 0a 0a 20 20 2f ee *pLater;.. /
14dc9 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e * Some basic san
14dca 69 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 ity checking on
14dcb 74 68 65 20 42 74 72 65 65 2e 20 20 54 68 65 20 the Btree. The
14dcc 6c 69 73 74 20 6f 66 20 42 74 72 65 65 73 0a 20 list of Btrees.
14dcd 20 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 ** connected by
14dce 20 70 4e 65 78 74 20 61 6e 64 20 70 50 72 65 76 pNext and pPrev
14dcf 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 20 73 6f should be in so
14dd0 72 74 65 64 20 6f 72 64 65 72 20 62 79 0a 20 20 rted order by.
14dd1 2a 2a 20 42 74 72 65 65 2e 70 42 74 20 76 61 6c ** Btree.pBt val
14dd2 75 65 2e 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 ue. All elements
14dd3 20 6f 66 20 74 68 65 20 6c 69 73 74 20 73 68 6f of the list sho
14dd4 75 6c 64 20 62 65 6c 6f 6e 67 20 74 6f 0a 20 20 uld belong to.
14dd5 2a 2a 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e ** the same conn
14dd6 65 63 74 69 6f 6e 2e 20 4f 6e 6c 79 20 73 68 61 ection. Only sha
14dd7 72 65 64 20 42 74 72 65 65 73 20 61 72 65 20 6f red Btrees are o
14dd8 6e 20 74 68 65 20 6c 69 73 74 2e 20 2a 2f 0a 20 n the list. */.
14dd9 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 assert( p->pNex
14dda 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 t==0 || p->pNext
14ddb 2d 3e 70 42 74 3e 70 2d 3e 70 42 74 20 29 3b 0a ->pBt>p->pBt );.
14ddc 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 assert( p->pPr
14ddd 65 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 ev==0 || p->pPre
14dde 76 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b v->pBt<p->pBt );
14ddf 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4e . assert( p->pN
14de0 65 78 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 ext==0 || p->pNe
14de1 78 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b xt->db==p->db );
14de2 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 . assert( p->pP
14de3 72 65 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 rev==0 || p->pPr
14de4 65 76 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b ev->db==p->db );
14de5 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 . assert( p->sh
14de6 61 72 61 62 6c 65 20 7c 7c 20 28 70 2d 3e 70 4e arable || (p->pN
14de7 65 78 74 3d 3d 30 20 26 26 20 70 2d 3e 70 50 72 ext==0 && p->pPr
14de8 65 76 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 ev==0) );.. /*
14de9 43 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 69 6e Check for lockin
14dea 67 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 2a 2f g consistency */
14deb 0a 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e 6c . assert( !p->l
14dec 6f 63 6b 65 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 ocked || p->want
14ded 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 73 ToLock>0 );. as
14dee 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c sert( p->sharabl
14def 65 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f e || p->wantToLo
14df0 63 6b 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 57 ck==0 );.. /* W
14df1 65 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 e should already
14df2 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 hold a lock on
14df3 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
14df4 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 nection */. ass
14df5 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
14df6 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
14df7 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 55 utex) );.. /* U
14df8 6e 6c 65 73 73 20 74 68 65 20 64 61 74 61 62 61 nless the databa
14df9 73 65 20 69 73 20 73 68 61 72 61 62 6c 65 20 61 se is sharable a
14dfa 6e 64 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 nd unlocked, the
14dfb 6e 20 42 74 53 68 61 72 65 64 2e 64 62 0a 20 20 n BtShared.db.
14dfc 2a 2a 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 ** should alread
14dfd 79 20 62 65 20 73 65 74 20 63 6f 72 72 65 63 74 y be set correct
14dfe 6c 79 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ly. */. assert(
14dff 20 28 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 26 (p->locked==0 &
14e00 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 29 20 7c & p->sharable) |
14e01 7c 20 70 2d 3e 70 42 74 2d 3e 64 62 3d 3d 70 2d | p->pBt->db==p-
14e02 3e 64 62 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 >db );.. if( !p
14e03 2d 3e 73 68 61 72 61 62 6c 65 20 29 20 72 65 74 ->sharable ) ret
14e04 75 72 6e 3b 0a 20 20 70 2d 3e 77 61 6e 74 54 6f urn;. p->wantTo
14e05 4c 6f 63 6b 2b 2b 3b 0a 20 20 69 66 28 20 70 2d Lock++;. if( p-
14e06 3e 6c 6f 63 6b 65 64 20 29 20 72 65 74 75 72 6e >locked ) return
14e07 3b 0a 0a 20 20 2f 2a 20 49 6e 20 6d 6f 73 74 20 ;.. /* In most
14e08 63 61 73 65 73 2c 20 77 65 20 73 68 6f 75 6c 64 cases, we should
14e09 20 62 65 20 61 62 6c 65 20 74 6f 20 61 63 71 75 be able to acqu
14e0a 69 72 65 20 74 68 65 20 6c 6f 63 6b 20 77 65 0a ire the lock we.
14e0b 20 20 2a 2a 20 77 61 6e 74 20 77 69 74 68 6f 75 ** want withou
14e0c 74 20 68 61 76 69 6e 67 20 74 6f 20 67 6f 20 74 t having to go t
14e0d 68 72 6f 75 67 68 74 20 74 68 65 20 61 73 63 65 hrought the asce
14e0e 6e 64 69 6e 67 20 6c 6f 63 6b 0a 20 20 2a 2a 20 nding lock. **
14e0f 70 72 6f 63 65 64 75 72 65 20 74 68 61 74 20 66 procedure that f
14e10 6f 6c 6c 6f 77 73 2e 20 20 4a 75 73 74 20 62 65 ollows. Just be
14e11 20 73 75 72 65 20 6e 6f 74 20 74 6f 20 62 6c 6f sure not to blo
14e12 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 ck.. */. if( s
14e13 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
14e14 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3d (p->pBt->mutex)=
14e15 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
14e16 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 p->pBt->db = p
14e17 2d 3e 64 62 3b 0a 20 20 20 20 70 2d 3e 6c 6f 63 ->db;. p->loc
14e18 6b 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 ked = 1;. ret
14e19 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 urn;. }.. /* T
14e1a 6f 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b o avoid deadlock
14e1b 2c 20 66 69 72 73 74 20 72 65 6c 65 61 73 65 20 , first release
14e1c 61 6c 6c 20 6c 6f 63 6b 73 20 77 69 74 68 20 61 all locks with a
14e1d 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 42 74 53 larger. ** BtS
14e1e 68 61 72 65 64 20 61 64 64 72 65 73 73 2e 20 20 hared address.
14e1f 54 68 65 6e 20 61 63 71 75 69 72 65 20 6f 75 72 Then acquire our
14e20 20 6c 6f 63 6b 2e 20 20 54 68 65 6e 20 72 65 61 lock. Then rea
14e21 63 71 75 69 72 65 0a 20 20 2a 2a 20 74 68 65 20 cquire. ** the
14e22 6f 74 68 65 72 20 42 74 53 68 61 72 65 64 20 6c other BtShared l
14e23 6f 63 6b 73 20 74 68 61 74 20 77 65 20 75 73 65 ocks that we use
14e24 64 20 74 6f 20 68 6f 6c 64 20 69 6e 20 61 73 63 d to hold in asc
14e25 65 6e 64 69 6e 67 0a 20 20 2a 2a 20 6f 72 64 65 ending. ** orde
14e26 72 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c r.. */. for(pL
14e27 61 74 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 ater=p->pNext; p
14e28 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c Later; pLater=pL
14e29 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 ater->pNext){.
14e2a 20 20 61 73 73 65 72 74 28 20 70 4c 61 74 65 72 assert( pLater
14e2b 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a 20 20 ->sharable );.
14e2c 20 20 61 73 73 65 72 74 28 20 70 4c 61 74 65 72 assert( pLater
14e2d 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 4c ->pNext==0 || pL
14e2e 61 74 65 72 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 ater->pNext->pBt
14e2f 3e 70 4c 61 74 65 72 2d 3e 70 42 74 20 29 3b 0a >pLater->pBt );.
14e30 20 20 20 20 61 73 73 65 72 74 28 20 21 70 4c 61 assert( !pLa
14e31 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 ter->locked || p
14e32 4c 61 74 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 Later->wantToLoc
14e33 6b 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 k>0 );. if( p
14e34 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b Later->locked ){
14e35 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 . unlockBtr
14e36 65 65 4d 75 74 65 78 28 70 4c 61 74 65 72 29 3b eeMutex(pLater);
14e37 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6c 6f 63 . }. }. loc
14e38 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a kBtreeMutex(p);.
14e39 20 20 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e for(pLater=p->
14e3a 70 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 pNext; pLater; p
14e3b 4c 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e Later=pLater->pN
14e3c 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 4c ext){. if( pL
14e3d 61 74 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b ater->wantToLock
14e3e 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 42 74 ){. lockBt
14e3f 72 65 65 4d 75 74 65 78 28 70 4c 61 74 65 72 29 reeMutex(pLater)
14e40 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f ;. }. }.}../
14e41 2a 0a 2a 2a 20 45 78 69 74 20 74 68 65 20 72 65 *.** Exit the re
14e42 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 6f 6e cursive mutex on
14e43 20 61 20 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c a Btree..*/.SQL
14e44 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
14e45 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
14e46 76 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 ve(Btree *p){.
14e47 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 if( p->sharable
14e48 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
14e49 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 ->wantToLock>0 )
14e4a 3b 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c ;. p->wantToL
14e4b 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 ock--;. if( p
14e4c 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 ->wantToLock==0
14e4d 29 7b 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 ){. unlockB
14e4e 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 treeMutex(p);.
14e4f 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 }. }.}..#ifnd
14e50 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 ef NDEBUG./*.**
14e51 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 Return true if t
14e52 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 he BtShared mute
14e53 78 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 x is held on the
14e54 20 62 74 72 65 65 2c 20 6f 72 20 69 66 20 74 68 btree, or if th
14e55 65 0a 2a 2a 20 42 2d 54 72 65 65 20 69 73 20 6e e.** B-Tree is n
14e56 6f 74 20 6d 61 72 6b 65 64 20 61 73 20 73 68 61 ot marked as sha
14e57 72 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 rable..**.** Thi
14e58 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 s routine is use
14e59 64 20 6f 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 d only from with
14e5a 69 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 in assert() stat
14e5b 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ements..*/.SQLIT
14e5c 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
14e5d 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d lite3BtreeHoldsM
14e5e 75 74 65 78 28 42 74 72 65 65 20 2a 70 29 7b 0a utex(Btree *p){.
14e5f 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 assert( p->sha
14e60 72 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c rable==0 || p->l
14e61 6f 63 6b 65 64 3d 3d 30 20 7c 7c 20 70 2d 3e 77 ocked==0 || p->w
14e62 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 antToLock>0 );.
14e63 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 assert( p->shar
14e64 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f able==0 || p->lo
14e65 63 6b 65 64 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 cked==0 || p->db
14e66 3d 3d 70 2d 3e 70 42 74 2d 3e 64 62 20 29 3b 0a ==p->pBt->db );.
14e67 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 assert( p->sha
14e68 72 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c rable==0 || p->l
14e69 6f 63 6b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 ocked==0 || sqli
14e6a 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
14e6b 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
14e6c 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 . assert( p->sh
14e6d 61 72 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e arable==0 || p->
14e6e 6c 6f 63 6b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c locked==0 || sql
14e6f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
14e70 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
14e71 0a 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 73 .. return (p->s
14e72 68 61 72 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d harable==0 || p-
14e73 3e 6c 6f 63 6b 65 64 29 3b 0a 7d 0a 23 65 6e 64 >locked);.}.#end
14e74 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c if...#ifndef SQL
14e75 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f ITE_OMIT_INCRBLO
14e76 42 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 61 6e B./*.** Enter an
14e77 64 20 6c 65 61 76 65 20 61 20 6d 75 74 65 78 20 d leave a mutex
14e78 6f 6e 20 61 20 42 74 72 65 65 20 67 69 76 65 6e on a Btree given
14e79 20 61 20 63 75 72 73 6f 72 20 6f 77 6e 65 64 20 a cursor owned
14e7a 62 79 20 74 68 61 74 0a 2a 2a 20 42 74 72 65 65 by that.** Btree
14e7b 2e 20 20 54 68 65 73 65 20 65 6e 74 72 79 20 70 . These entry p
14e7c 6f 69 6e 74 73 20 61 72 65 20 75 73 65 64 20 62 oints are used b
14e7d 79 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f y incremental I/
14e7e 4f 20 61 6e 64 20 63 61 6e 20 62 65 0a 2a 2a 20 O and can be.**
14e7f 6f 6d 69 74 74 65 64 20 69 66 20 74 68 61 74 20 omitted if that
14e80 6d 6f 64 75 6c 65 20 69 73 20 6e 6f 74 20 75 73 module is not us
14e81 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
14e82 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
14e83 65 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 e3BtreeEnterCurs
14e84 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 or(BtCursor *pCu
14e85 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 r){. sqlite3Btr
14e86 65 65 45 6e 74 65 72 28 70 43 75 72 2d 3e 70 42 eeEnter(pCur->pB
14e87 74 72 65 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f tree);.}.SQLITE_
14e88 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
14e89 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 ite3BtreeLeaveCu
14e8a 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 rsor(BtCursor *p
14e8b 43 75 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 Cur){. sqlite3B
14e8c 74 72 65 65 4c 65 61 76 65 28 70 43 75 72 2d 3e treeLeave(pCur->
14e8d 70 42 74 72 65 65 29 3b 0a 7d 0a 23 65 6e 64 69 pBtree);.}.#endi
14e8e 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
14e8f 5f 49 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 0a 2f _INCRBLOB */.../
14e90 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d *.** Enter the m
14e91 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20 42 74 utex on every Bt
14e92 72 65 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 ree associated w
14e93 69 74 68 20 61 20 64 61 74 61 62 61 73 65 0a 2a ith a database.*
14e94 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 * connection. T
14e95 68 69 73 20 69 73 20 6e 65 65 64 65 64 20 28 66 his is needed (f
14e96 6f 72 20 65 78 61 6d 70 6c 65 29 20 70 72 69 6f or example) prio
14e97 72 20 74 6f 20 70 61 72 73 69 6e 67 0a 2a 2a 20 r to parsing.**
14e98 61 20 73 74 61 74 65 6d 65 6e 74 20 73 69 6e 63 a statement sinc
14e99 65 20 77 65 20 77 69 6c 6c 20 62 65 20 63 6f 6d e we will be com
14e9a 70 61 72 69 6e 67 20 74 61 62 6c 65 20 61 6e 64 paring table and
14e9b 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 2a 2a column names.**
14e9c 20 61 67 61 69 6e 73 74 20 61 6c 6c 20 73 63 68 against all sch
14e9d 65 6d 61 73 20 61 6e 64 20 77 65 20 64 6f 20 6e emas and we do n
14e9e 6f 74 20 77 61 6e 74 20 74 68 6f 73 65 20 73 63 ot want those sc
14e9f 68 65 6d 61 73 20 62 65 69 6e 67 0a 2a 2a 20 72 hemas being.** r
14ea0 65 73 65 74 20 6f 75 74 20 66 72 6f 6d 20 75 6e eset out from un
14ea1 64 65 72 20 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 der us..**.** Th
14ea2 65 72 65 20 69 73 20 61 20 63 6f 72 72 65 73 70 ere is a corresp
14ea3 6f 6e 64 69 6e 67 20 6c 65 61 76 65 2d 61 6c 6c onding leave-all
14ea4 20 70 72 6f 63 65 64 75 72 65 73 2e 0a 2a 2a 0a procedures..**.
14ea5 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 ** Enter the mut
14ea6 65 78 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e exes in accendin
14ea7 67 20 6f 72 64 65 72 20 62 79 20 42 74 53 68 61 g order by BtSha
14ea8 72 65 64 20 70 6f 69 6e 74 65 72 20 61 64 64 72 red pointer addr
14ea9 65 73 73 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20 ess.** to avoid
14eaa 74 68 65 20 70 6f 73 73 69 62 69 6c 69 74 79 20 the possibility
14eab 6f 66 20 64 65 61 64 6c 6f 63 6b 20 77 68 65 6e of deadlock when
14eac 20 74 77 6f 20 74 68 72 65 61 64 73 20 77 69 74 two threads wit
14ead 68 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 h.** two or more
14eae 20 62 74 72 65 65 73 20 69 6e 20 63 6f 6d 6d 6f btrees in commo
14eaf 6e 20 62 6f 74 68 20 74 72 79 20 74 6f 20 6c 6f n both try to lo
14eb0 63 6b 20 61 6c 6c 20 74 68 65 69 72 20 62 74 72 ck all their btr
14eb1 65 65 73 0a 2a 2a 20 61 74 20 74 68 65 20 73 61 ees.** at the sa
14eb2 6d 65 20 69 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 me instant..*/.S
14eb3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
14eb4 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 id sqlite3BtreeE
14eb5 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 20 nterAll(sqlite3
14eb6 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 *db){. int i;.
14eb7 20 42 74 72 65 65 20 2a 70 2c 20 2a 70 4c 61 74 Btree *p, *pLat
14eb8 65 72 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 er;. assert( sq
14eb9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
14eba 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 (db->mutex) );.
14ebb 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e for(i=0; i<db->
14ebc 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 nDb; i++){. p
14ebd 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
14ebe 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 t;. assert( !
14ebf 70 20 7c 7c 20 28 70 2d 3e 6c 6f 63 6b 65 64 3d p || (p->locked=
14ec0 3d 30 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c =0 && p->sharabl
14ec1 65 29 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e 64 62 e) || p->pBt->db
14ec2 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 20 20 69 ==p->db );. i
14ec3 66 28 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61 f( p && p->shara
14ec4 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e ble ){. p->
14ec5 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 wantToLock++;.
14ec6 20 20 20 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b if( !p->lock
14ec7 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 ed ){. as
14ec8 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c sert( p->wantToL
14ec9 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 ock==1 );.
14eca 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 50 72 65 while( p->pPre
14ecb 76 20 29 20 70 20 3d 20 70 2d 3e 70 50 72 65 76 v ) p = p->pPrev
14ecc 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 ;. /* Rea
14ecd 73 6f 6e 20 66 6f 72 20 41 4c 57 41 59 53 3a 20 son for ALWAYS:
14ece 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 There must be a
14ecf 74 20 6c 65 61 73 74 20 6f 6e 20 75 6e 6c 6f 63 t least on unloc
14ed0 6b 65 64 20 42 74 72 65 65 20 69 6e 0a 20 20 20 ked Btree in.
14ed1 20 20 20 20 20 2a 2a 20 74 68 65 20 63 68 61 69 ** the chai
14ed2 6e 2e 20 20 4f 74 68 65 72 77 69 73 65 20 74 68 n. Otherwise th
14ed3 65 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 74 65 73 e !p->locked tes
14ed4 74 20 61 62 6f 76 65 20 77 6f 75 6c 64 20 68 61 t above would ha
14ed5 76 65 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 20 ve failed */.
14ed6 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 6c while( p->l
14ed7 6f 63 6b 65 64 20 26 26 20 41 4c 57 41 59 53 28 ocked && ALWAYS(
14ed8 70 2d 3e 70 4e 65 78 74 29 20 29 20 70 20 3d 20 p->pNext) ) p =
14ed9 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 p->pNext;.
14eda 20 20 66 6f 72 28 70 4c 61 74 65 72 20 3d 20 70 for(pLater = p
14edb 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b ->pNext; pLater;
14edc 20 70 4c 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e pLater=pLater->
14edd 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 pNext){.
14ede 20 20 69 66 28 20 70 4c 61 74 65 72 2d 3e 6c 6f if( pLater->lo
14edf 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 cked ){.
14ee0 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d unlockBtreeM
14ee1 75 74 65 78 28 70 4c 61 74 65 72 29 3b 0a 20 20 utex(pLater);.
14ee2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
14ee3 20 20 7d 0a 20 20 20 20 20 20 20 20 77 68 69 6c }. whil
14ee4 65 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 e( p ){.
14ee5 20 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 lockBtreeMutex
14ee6 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 (p);. p
14ee7 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 = p->pNext;.
14ee8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
14ee9 20 20 20 7d 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 }. }.}.SQLIT
14eea 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
14eeb 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
14eec 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 29 All(sqlite3 *db)
14eed 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 74 72 {. int i;. Btr
14eee 65 65 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 ee *p;. assert(
14eef 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
14ef0 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 eld(db->mutex) )
14ef1 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 ;. for(i=0; i<d
14ef2 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 b->nDb; i++){.
14ef3 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d p = db->aDb[i]
14ef4 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 20 .pBt;. if( p
14ef5 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 && p->sharable )
14ef6 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
14ef7 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 p->wantToLock>0
14ef8 29 3b 0a 20 20 20 20 20 20 70 2d 3e 77 61 6e 74 );. p->want
14ef9 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 ToLock--;.
14efa 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 if( p->wantToLoc
14efb 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 k==0 ){.
14efc 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 unlockBtreeMutex
14efd 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 (p);. }.
14efe 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 }. }.}..#ifnde
14eff 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 f NDEBUG./*.** R
14f00 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 eturn true if th
14f01 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 e current thread
14f02 20 68 6f 6c 64 73 20 74 68 65 20 64 61 74 61 62 holds the datab
14f03 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a ase connection.*
14f04 2a 20 6d 75 74 65 78 20 61 6e 64 20 61 6c 6c 20 * mutex and all
14f05 72 65 71 75 69 72 65 64 20 42 74 53 68 61 72 65 required BtShare
14f06 64 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a d mutexes..**.**
14f07 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
14f08 20 75 73 65 64 20 69 6e 73 69 64 65 20 61 73 73 used inside ass
14f09 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 ert() statements
14f0a 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 only..*/.SQLITE
14f0b 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
14f0c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c ite3BtreeHoldsAl
14f0d 6c 4d 75 74 65 78 65 73 28 73 71 6c 69 74 65 33 lMutexes(sqlite3
14f0e 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a *db){. int i;.
14f0f 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 6d if( !sqlite3_m
14f10 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 utex_held(db->mu
14f11 74 65 78 29 20 29 7b 0a 20 20 20 20 72 65 74 75 tex) ){. retu
14f12 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 rn 0;. }. for(
14f13 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 i=0; i<db->nDb;
14f14 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 i++){. Btree
14f15 2a 70 3b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e *p;. p = db->
14f16 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 aDb[i].pBt;.
14f17 69 66 28 20 70 20 26 26 20 70 2d 3e 73 68 61 72 if( p && p->shar
14f18 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20 20 20 able &&.
14f19 20 28 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d (p->wantToLock=
14f1a 3d 30 20 7c 7c 20 21 73 71 6c 69 74 65 33 5f 6d =0 || !sqlite3_m
14f1b 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 utex_held(p->pBt
14f1c 2d 3e 6d 75 74 65 78 29 29 20 29 7b 0a 20 20 20 ->mutex)) ){.
14f1d 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 return 0;.
14f1e 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
14f1f 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 1;.}.#endif /* N
14f20 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 DEBUG */../*.**
14f21 41 64 64 20 61 20 6e 65 77 20 42 74 72 65 65 20 Add a new Btree
14f22 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 42 74 72 pointer to a Btr
14f23 65 65 4d 75 74 65 78 41 72 72 61 79 2e 20 0a 2a eeMutexArray. .*
14f24 2a 20 69 66 20 74 68 65 20 70 6f 69 6e 74 65 72 * if the pointer
14f25 20 63 61 6e 20 70 6f 73 73 69 62 6c 79 20 62 65 can possibly be
14f26 20 73 68 61 72 65 64 20 77 69 74 68 0a 2a 2a 20 shared with.**
14f27 61 6e 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 another database
14f28 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a connection..**.
14f29 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 73 20 ** The pointers
14f2a 61 72 65 20 6b 65 70 74 20 69 6e 20 73 6f 72 74 are kept in sort
14f2b 65 64 20 6f 72 64 65 72 20 62 79 20 70 42 74 72 ed order by pBtr
14f2c 65 65 2d 3e 70 42 74 2e 20 20 54 68 61 74 0a 2a ee->pBt. That.*
14f2d 2a 20 77 61 79 20 77 68 65 6e 20 77 65 20 67 6f * way when we go
14f2e 20 74 6f 20 65 6e 74 65 72 20 61 6c 6c 20 74 68 to enter all th
14f2f 65 20 6d 75 74 65 78 65 73 2c 20 77 65 20 63 61 e mutexes, we ca
14f30 6e 20 65 6e 74 65 72 20 74 68 65 6d 0a 2a 2a 20 n enter them.**
14f31 69 6e 20 6f 72 64 65 72 20 77 69 74 68 6f 75 74 in order without
14f32 20 65 76 65 72 79 20 68 61 76 69 6e 67 20 74 6f every having to
14f33 20 62 61 63 6b 75 70 20 61 6e 64 20 72 65 74 72 backup and retr
14f34 79 20 61 6e 64 20 77 69 74 68 6f 75 74 0a 2a 2a y and without.**
14f35 20 77 6f 72 72 79 69 6e 67 20 61 62 6f 75 74 20 worrying about
14f36 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 deadlock..**.**
14f37 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 68 The number of sh
14f38 61 72 65 64 20 62 74 72 65 65 73 20 77 69 6c 6c ared btrees will
14f39 20 61 6c 77 61 79 73 20 62 65 20 73 6d 61 6c 6c always be small
14f3a 20 28 75 73 75 61 6c 6c 79 20 30 20 6f 72 20 31 (usually 0 or 1
14f3b 29 0a 2a 2a 20 73 6f 20 61 6e 20 69 6e 73 65 72 ).** so an inser
14f3c 74 69 6f 6e 20 73 6f 72 74 20 69 73 20 61 6e 20 tion sort is an
14f3d 61 64 65 71 75 61 74 65 20 61 6c 67 6f 72 69 74 adequate algorit
14f3e 68 6d 20 68 65 72 65 2e 0a 2a 2f 0a 53 51 4c 49 hm here..*/.SQLI
14f3f 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
14f40 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 sqlite3BtreeMute
14f41 78 41 72 72 61 79 49 6e 73 65 72 74 28 42 74 72 xArrayInsert(Btr
14f42 65 65 4d 75 74 65 78 41 72 72 61 79 20 2a 70 41 eeMutexArray *pA
14f43 72 72 61 79 2c 20 42 74 72 65 65 20 2a 70 42 74 rray, Btree *pBt
14f44 72 65 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a ree){. int i, j
14f45 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
14f46 74 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 3d t;. if( pBtree=
14f47 3d 30 20 7c 7c 20 70 42 74 72 65 65 2d 3e 73 68 =0 || pBtree->sh
14f48 61 72 61 62 6c 65 3d 3d 30 20 29 20 72 65 74 75 arable==0 ) retu
14f49 72 6e 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 rn;.#ifndef NDEB
14f4a 55 47 0a 20 20 7b 0a 20 20 20 20 66 6f 72 28 69 UG. {. for(i
14f4b 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d =0; i<pArray->nM
14f4c 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 utex; i++){.
14f4d 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 assert( pArray
14f4e 2d 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 ->aBtree[i]!=pBt
14f4f 72 65 65 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ree );. }. }
14f50 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74 .#endif. assert
14f51 28 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 ( pArray->nMutex
14f52 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 >=0 );. assert(
14f53 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3c pArray->nMutex<
14f54 41 72 72 61 79 53 69 7a 65 28 70 41 72 72 61 79 ArraySize(pArray
14f55 2d 3e 61 42 74 72 65 65 29 2d 31 20 29 3b 0a 20 ->aBtree)-1 );.
14f56 20 70 42 74 20 3d 20 70 42 74 72 65 65 2d 3e 70 pBt = pBtree->p
14f57 42 74 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 Bt;. for(i=0; i
14f58 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b <pArray->nMutex;
14f59 20 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 i++){. asser
14f5a 74 28 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 t( pArray->aBtre
14f5b 65 5b 69 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a e[i]!=pBtree );.
14f5c 20 20 20 20 69 66 28 20 70 41 72 72 61 79 2d 3e if( pArray->
14f5d 61 42 74 72 65 65 5b 69 5d 2d 3e 70 42 74 3e 70 aBtree[i]->pBt>p
14f5e 42 74 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 Bt ){. for(
14f5f 6a 3d 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 j=pArray->nMutex
14f60 3b 20 6a 3e 69 3b 20 6a 2d 2d 29 7b 0a 20 20 20 ; j>i; j--){.
14f61 20 20 20 20 20 70 41 72 72 61 79 2d 3e 61 42 74 pArray->aBt
14f62 72 65 65 5b 6a 5d 20 3d 20 70 41 72 72 61 79 2d ree[j] = pArray-
14f63 3e 61 42 74 72 65 65 5b 6a 2d 31 5d 3b 0a 20 20 >aBtree[j-1];.
14f64 20 20 20 20 7d 0a 20 20 20 20 20 20 70 41 72 72 }. pArr
14f65 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 20 3d 20 ay->aBtree[i] =
14f66 70 42 74 72 65 65 3b 0a 20 20 20 20 20 20 70 41 pBtree;. pA
14f67 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b 3b 0a rray->nMutex++;.
14f68 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
14f69 20 20 7d 0a 20 20 7d 0a 20 20 70 41 72 72 61 79 }. }. pArray
14f6a 2d 3e 61 42 74 72 65 65 5b 70 41 72 72 61 79 2d ->aBtree[pArray-
14f6b 3e 6e 4d 75 74 65 78 2b 2b 5d 20 3d 20 70 42 74 >nMutex++] = pBt
14f6c 72 65 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e ree;.}../*.** En
14f6d 74 65 72 20 74 68 65 20 6d 75 74 65 78 20 6f 66 ter the mutex of
14f6e 20 65 76 65 72 79 20 62 74 72 65 65 20 69 6e 20 every btree in
14f6f 74 68 65 20 61 72 72 61 79 2e 20 20 54 68 69 73 the array. This
14f70 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a 2a 20 63 routine is.** c
14f71 61 6c 6c 65 64 20 61 74 20 74 68 65 20 62 65 67 alled at the beg
14f72 69 6e 6e 69 6e 67 20 6f 66 20 73 71 6c 69 74 65 inning of sqlite
14f73 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 54 68 3VdbeExec(). Th
14f74 65 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a e mutexes are.**
14f75 20 65 78 69 74 65 64 20 61 74 20 74 68 65 20 65 exited at the e
14f76 6e 64 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 nd of the same f
14f77 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 unction..*/.SQLI
14f78 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
14f79 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 sqlite3BtreeMute
14f7a 78 41 72 72 61 79 45 6e 74 65 72 28 42 74 72 65 xArrayEnter(Btre
14f7b 65 4d 75 74 65 78 41 72 72 61 79 20 2a 70 41 72 eMutexArray *pAr
14f7c 72 61 79 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 ray){. int i;.
14f7d 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 for(i=0; i<pArr
14f7e 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 ay->nMutex; i++)
14f7f 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 20 3d {. Btree *p =
14f80 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b pArray->aBtree[
14f81 69 5d 3b 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 i];. /* Some
14f82 62 61 73 69 63 20 73 61 6e 69 74 79 20 63 68 65 basic sanity che
14f83 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 61 73 73 cking */. ass
14f84 65 72 74 28 20 69 3d 3d 30 20 7c 7c 20 70 41 72 ert( i==0 || pAr
14f85 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 2d 31 5d ray->aBtree[i-1]
14f86 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a ->pBt<p->pBt );.
14f87 20 20 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e assert( !p->
14f88 6c 6f 63 6b 65 64 20 7c 7c 20 70 2d 3e 77 61 6e locked || p->wan
14f89 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20 tToLock>0 );..
14f8a 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 /* We should a
14f8b 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f lready hold a lo
14f8c 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ck on the databa
14f8d 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
14f8e 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
14f8f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
14f90 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
14f91 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 42 74 72 .. /* The Btr
14f92 65 65 20 69 73 20 73 68 61 72 61 62 6c 65 20 62 ee is sharable b
14f93 65 63 61 75 73 65 20 6f 6e 6c 79 20 73 68 61 72 ecause only shar
14f94 61 62 6c 65 20 42 74 72 65 65 73 20 61 72 65 20 able Btrees are
14f95 65 6e 74 65 72 65 64 0a 20 20 20 20 2a 2a 20 69 entered. ** i
14f96 6e 74 6f 20 74 68 65 20 61 72 72 61 79 20 69 6e nto the array in
14f97 20 74 68 65 20 66 69 72 73 74 20 70 6c 61 63 65 the first place
14f98 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 . */. assert(
14f99 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a p->sharable );.
14f9a 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f . p->wantToLo
14f9b 63 6b 2b 2b 3b 0a 20 20 20 20 69 66 28 20 21 70 ck++;. if( !p
14f9c 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 ->locked ){.
14f9d 20 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 lockBtreeMutex
14f9e 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d (p);. }. }.}
14f9f 0a 0a 2f 2a 0a 2a 2a 20 4c 65 61 76 65 20 74 68 ../*.** Leave th
14fa0 65 20 6d 75 74 65 78 20 6f 66 20 65 76 65 72 79 e mutex of every
14fa1 20 62 74 72 65 65 20 69 6e 20 74 68 65 20 67 72 btree in the gr
14fa2 6f 75 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 oup..*/.SQLITE_P
14fa3 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
14fa4 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 te3BtreeMutexArr
14fa5 61 79 4c 65 61 76 65 28 42 74 72 65 65 4d 75 74 ayLeave(BtreeMut
14fa6 65 78 41 72 72 61 79 20 2a 70 41 72 72 61 79 29 exArray *pArray)
14fa7 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 {. int i;. for
14fa8 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e (i=0; i<pArray->
14fa9 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 nMutex; i++){.
14faa 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 41 72 Btree *p = pAr
14fab 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 3b 0a ray->aBtree[i];.
14fac 20 20 20 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69 /* Some basi
14fad 63 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e c sanity checkin
14fae 67 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 g */. assert(
14faf 20 69 3d 3d 30 20 7c 7c 20 70 41 72 72 61 79 2d i==0 || pArray-
14fb0 3e 61 42 74 72 65 65 5b 69 2d 31 5d 2d 3e 70 42 >aBtree[i-1]->pB
14fb1 74 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 t<p->pBt );.
14fb2 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 assert( p->locke
14fb3 64 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 d );. assert(
14fb4 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 p->wantToLock>0
14fb5 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73 );.. /* We s
14fb6 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f hould already ho
14fb7 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 ld a lock on the
14fb8 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
14fb9 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 tion */. asse
14fba 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
14fbb 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
14fbc 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 70 2d 3e tex) );.. p->
14fbd 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 wantToLock--;.
14fbe 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c if( p->wantToL
14fbf 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ock==0 ){.
14fc0 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 unlockBtreeMutex
14fc1 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d (p);. }. }.}
14fc2 0a 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 ..#else.SQLITE_P
14fc3 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
14fc4 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 42 74 te3BtreeEnter(Bt
14fc5 72 65 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 70 42 ree *p){. p->pB
14fc6 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 7d t->db = p->db;.}
14fc7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14fc8 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
14fc9 65 45 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 eEnterAll(sqlite
14fca 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 3 *db){. int i;
14fcb 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 . for(i=0; i<db
14fcc 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nDb; i++){.
14fcd 20 42 74 72 65 65 20 2a 70 20 3d 20 64 62 2d 3e Btree *p = db->
14fce 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 aDb[i].pBt;.
14fcf 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 70 if( p ){. p
14fd0 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 ->pBt->db = p->d
14fd1 62 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 b;. }. }.}.#
14fd2 65 6e 64 69 66 20 2f 2a 20 69 66 20 53 51 4c 49 endif /* if SQLI
14fd3 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 2a 2f TE_THREADSAFE */
14fd4 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 6e 64 65 .#endif /* ifnde
14fd5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
14fd6 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 2f ARED_CACHE */../
14fd7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
14fd8 6e 64 20 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 nd of btmutex.c
14fd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14fda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14fdb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
14fdc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
14fdd 65 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 2e egin file btree.
14fde 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
14fdf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
14fe1 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 *.** 2004 April
14fe2 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6.**.** The auth
14fe3 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
14fe4 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
14fe5 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
14fe6 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
14fe7 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
14fe8 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
14fe9 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
14fea 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
14feb 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
14fec 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
14fed 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
14fee 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
14fef 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
14ff0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
14ff1 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
14ff2 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
14ff3 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
14ff4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14ff5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14ff6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14ff7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14ff8 0a 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 2e 63 .** $Id: btree.c
14ff9 2c 76 20 31 2e 37 30 35 20 32 30 30 39 2f 30 38 ,v 1.705 2009/08
14ffa 2f 31 30 20 30 33 3a 35 37 3a 35 38 20 73 68 61 /10 03:57:58 sha
14ffb 6e 65 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 ne Exp $.**.** T
14ffc 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 his file impleme
14ffd 6e 74 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 nts a external (
14ffe 64 69 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 disk-based) data
14fff 62 61 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 base using BTree
15000 73 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 68 65 s..** See the he
15001 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 ader comment on
15002 22 62 74 72 65 65 49 6e 74 2e 68 22 20 66 6f 72 "btreeInt.h" for
15003 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f additional info
15004 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c rmation..** Incl
15005 75 64 69 6e 67 20 61 20 64 65 73 63 72 69 70 74 uding a descript
15006 69 6f 6e 20 6f 66 20 66 69 6c 65 20 66 6f 72 6d ion of file form
15007 61 74 20 61 6e 64 20 61 6e 20 6f 76 65 72 76 69 at and an overvi
15008 65 77 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 2e ew of operation.
15009 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 .*/../*.** The h
1500a 65 61 64 65 72 20 73 74 72 69 6e 67 20 74 68 61 eader string tha
1500b 74 20 61 70 70 65 61 72 73 20 61 74 20 74 68 65 t appears at the
1500c 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 beginning of ev
1500d 65 72 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 ery.** SQLite da
1500e 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 tabase..*/.stati
1500f 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 4d 61 c const char zMa
15010 67 69 63 48 65 61 64 65 72 5b 5d 20 3d 20 53 51 gicHeader[] = SQ
15011 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 LITE_FILE_HEADER
15012 3b 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 69 ;../*.** Set thi
15013 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c s global variabl
15014 65 20 74 6f 20 31 20 74 6f 20 65 6e 61 62 6c 65 e to 1 to enable
15015 20 74 72 61 63 69 6e 67 20 75 73 69 6e 67 20 74 tracing using t
15016 68 65 20 54 52 41 43 45 0a 2a 2a 20 6d 61 63 72 he TRACE.** macr
15017 6f 2e 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 o..*/.#if 0.int
15018 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 61 63 sqlite3BtreeTrac
15019 65 3d 31 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f e=1; /* True to
1501a 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 enable tracing
1501b 2a 2f 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43 */.# define TRAC
1501c 45 28 58 29 20 20 69 66 28 73 71 6c 69 74 65 33 E(X) if(sqlite3
1501d 42 74 72 65 65 54 72 61 63 65 29 7b 70 72 69 6e BtreeTrace){prin
1501e 74 66 20 58 3b 66 66 6c 75 73 68 28 73 74 64 6f tf X;fflush(stdo
1501f 75 74 29 3b 7d 0a 23 65 6c 73 65 0a 23 20 64 65 ut);}.#else.# de
15020 66 69 6e 65 20 54 52 41 43 45 28 58 29 0a 23 65 fine TRACE(X).#e
15021 6e 64 69 66 0a 0a 0a 0a 23 69 66 6e 64 65 66 20 ndif....#ifndef
15022 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
15023 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 ED_CACHE./*.** A
15024 20 6c 69 73 74 20 6f 66 20 42 74 53 68 61 72 65 list of BtShare
15025 64 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 d objects that a
15026 72 65 20 65 6c 69 67 69 62 6c 65 20 66 6f 72 20 re eligible for
15027 70 61 72 74 69 63 69 70 61 74 69 6f 6e 0a 2a 2a participation.**
15028 20 69 6e 20 73 68 61 72 65 64 20 63 61 63 68 65 in shared cache
15029 2e 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 . This variable
1502a 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 20 has file scope
1502b 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 62 75 during normal bu
1502c 69 6c 64 73 2c 0a 2a 2a 20 62 75 74 20 74 68 65 ilds,.** but the
1502d 20 74 65 73 74 20 68 61 72 6e 65 73 73 20 6e 65 test harness ne
1502e 65 64 73 20 74 6f 20 61 63 63 65 73 73 20 69 74 eds to access it
1502f 20 73 6f 20 77 65 20 6d 61 6b 65 20 69 74 20 67 so we make it g
15030 6c 6f 62 61 6c 20 66 6f 72 20 0a 2a 2a 20 74 65 lobal for .** te
15031 73 74 20 62 75 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a st builds..**.**
15032 20 41 63 63 65 73 73 20 74 6f 20 74 68 69 73 20 Access to this
15033 76 61 72 69 61 62 6c 65 20 69 73 20 70 72 6f 74 variable is prot
15034 65 63 74 65 64 20 62 79 20 53 51 4c 49 54 45 5f ected by SQLITE_
15035 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
15036 54 45 52 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 TER..*/.#ifdef S
15037 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
15038 45 5f 50 52 49 56 41 54 45 20 42 74 53 68 61 72 E_PRIVATE BtShar
15039 65 64 20 2a 53 51 4c 49 54 45 5f 57 53 44 20 73 ed *SQLITE_WSD s
1503a 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 qlite3SharedCach
1503b 65 4c 69 73 74 20 3d 20 30 3b 0a 23 65 6c 73 65 eList = 0;.#else
1503c 0a 73 74 61 74 69 63 20 42 74 53 68 61 72 65 64 .static BtShared
1503d 20 2a 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c *SQLITE_WSD sql
1503e 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c ite3SharedCacheL
1503f 69 73 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a ist = 0;.#endif.
15040 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
15041 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
15042 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 HE */..#ifndef S
15043 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
15044 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 45 6e D_CACHE./*.** En
15045 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20 able or disable
15046 74 68 65 20 73 68 61 72 65 64 20 70 61 67 65 72 the shared pager
15047 20 61 6e 64 20 73 63 68 65 6d 61 20 66 65 61 74 and schema feat
15048 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ures..**.** This
15049 20 72 6f 75 74 69 6e 65 20 68 61 73 20 6e 6f 20 routine has no
1504a 65 66 66 65 63 74 20 6f 6e 20 65 78 69 73 74 69 effect on existi
1504b 6e 67 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ng database conn
1504c 65 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 ections..** The
1504d 73 68 61 72 65 64 20 63 61 63 68 65 20 73 65 74 shared cache set
1504e 74 69 6e 67 20 65 66 66 65 63 74 73 20 6f 6e 6c ting effects onl
1504f 79 20 66 75 74 75 72 65 20 63 61 6c 6c 73 20 74 y future calls t
15050 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 o.** sqlite3_ope
15051 6e 28 29 2c 20 73 71 6c 69 74 65 33 5f 6f 70 65 n(), sqlite3_ope
15052 6e 31 36 28 29 2c 20 6f 72 20 73 71 6c 69 74 65 n16(), or sqlite
15053 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2f 0a 3_open_v2()..*/.
15054 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
15055 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 qlite3_enable_sh
15056 61 72 65 64 5f 63 61 63 68 65 28 69 6e 74 20 65 ared_cache(int e
15057 6e 61 62 6c 65 29 7b 0a 20 20 73 71 6c 69 74 65 nable){. sqlite
15058 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 68 3GlobalConfig.sh
15059 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c 65 64 aredCacheEnabled
1505a 20 3d 20 65 6e 61 62 6c 65 3b 0a 20 20 72 65 74 = enable;. ret
1505b 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1505c 0a 23 65 6e 64 69 66 0a 0a 0a 0a 23 69 66 64 65 .#endif....#ifde
1505d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
1505e 41 52 45 44 5f 43 41 43 48 45 0a 20 20 2f 2a 0a ARED_CACHE. /*.
1505f 20 20 2a 2a 20 54 68 65 20 66 75 6e 63 74 69 6f ** The functio
15060 6e 73 20 71 75 65 72 79 53 68 61 72 65 64 43 61 ns querySharedCa
15061 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 20 cheTableLock(),
15062 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 setSharedCacheTa
15063 62 6c 65 4c 6f 63 6b 28 29 2c 0a 20 20 2a 2a 20 bleLock(),. **
15064 61 6e 64 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 and clearAllShar
15065 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
15066 73 28 29 0a 20 20 2a 2a 20 6d 61 6e 69 70 75 6c s(). ** manipul
15067 61 74 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 ate entries in t
15068 68 65 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 he BtShared.pLoc
15069 6b 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 75 73 k linked list us
1506a 65 64 20 74 6f 20 73 74 6f 72 65 0a 20 20 2a 2a ed to store. **
1506b 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 74 61 shared-cache ta
1506c 62 6c 65 20 6c 65 76 65 6c 20 6c 6f 63 6b 73 2e ble level locks.
1506d 20 49 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 If the library
1506e 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 is compiled with
1506f 20 74 68 65 0a 20 20 2a 2a 20 73 68 61 72 65 64 the. ** shared
15070 2d 63 61 63 68 65 20 66 65 61 74 75 72 65 20 64 -cache feature d
15071 69 73 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 isabled, then th
15072 65 72 65 20 69 73 20 6f 6e 6c 79 20 65 76 65 72 ere is only ever
15073 20 6f 6e 65 20 75 73 65 72 0a 20 20 2a 2a 20 6f one user. ** o
15074 66 20 65 61 63 68 20 42 74 53 68 61 72 65 64 20 f each BtShared
15075 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 73 6f structure and so
15076 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67 20 69 73 this locking is
15077 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20 not necessary.
15078 0a 20 20 2a 2a 20 53 6f 20 64 65 66 69 6e 65 20 . ** So define
15079 74 68 65 20 6c 6f 63 6b 20 72 65 6c 61 74 65 64 the lock related
1507a 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 20 6e 6f functions as no
1507b 2d 6f 70 73 2e 0a 20 20 2a 2f 0a 20 20 23 64 65 -ops.. */. #de
1507c 66 69 6e 65 20 71 75 65 72 79 53 68 61 72 65 64 fine queryShared
1507d 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 61 CacheTableLock(a
1507e 2c 62 2c 63 29 20 53 51 4c 49 54 45 5f 4f 4b 0a ,b,c) SQLITE_OK.
1507f 20 20 23 64 65 66 69 6e 65 20 73 65 74 53 68 61 #define setSha
15080 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
15081 6b 28 61 2c 62 2c 63 29 20 53 51 4c 49 54 45 5f k(a,b,c) SQLITE_
15082 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 63 6c 65 OK. #define cle
15083 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 arAllSharedCache
15084 54 61 62 6c 65 4c 6f 63 6b 73 28 61 29 0a 20 20 TableLocks(a).
15085 23 64 65 66 69 6e 65 20 64 6f 77 6e 67 72 61 64 #define downgrad
15086 65 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 eAllSharedCacheT
15087 61 62 6c 65 4c 6f 63 6b 73 28 61 29 0a 20 20 23 ableLocks(a). #
15088 64 65 66 69 6e 65 20 68 61 73 53 68 61 72 65 64 define hasShared
15089 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 61 CacheTableLock(a
1508a 2c 62 2c 63 2c 64 29 20 31 0a 20 20 23 64 65 66 ,b,c,d) 1. #def
1508b 69 6e 65 20 68 61 73 52 65 61 64 43 6f 6e 66 6c ine hasReadConfl
1508c 69 63 74 73 28 61 2c 20 62 29 20 30 0a 23 65 6e icts(a, b) 0.#en
1508d 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c dif..#ifndef SQL
1508e 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
1508f 43 41 43 48 45 0a 0a 23 69 66 64 65 66 20 53 51 CACHE..#ifdef SQ
15090 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a LITE_DEBUG./*.**
15091 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
15092 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 61 73 is only used as
15093 20 70 61 72 74 20 6f 66 20 61 6e 20 61 73 73 65 part of an asse
15094 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 2e 20 rt() statement.
15095 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 ***.**.** Check
15096 74 6f 20 73 65 65 20 69 66 20 70 42 74 72 65 65 to see if pBtree
15097 20 68 6f 6c 64 73 20 74 68 65 20 72 65 71 75 69 holds the requi
15098 72 65 64 20 6c 6f 63 6b 73 20 74 6f 20 72 65 61 red locks to rea
15099 64 20 6f 72 20 77 72 69 74 65 20 74 6f 20 74 68 d or write to th
1509a 65 20 0a 2a 2a 20 74 61 62 6c 65 20 77 69 74 68 e .** table with
1509b 20 72 6f 6f 74 20 70 61 67 65 20 69 52 6f 6f 74 root page iRoot
1509c 2e 20 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 . Return 1 if
1509d 69 74 20 64 6f 65 73 20 61 6e 64 20 30 20 69 66 it does and 0 if
1509e 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 not..**.** For
1509f 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 77 72 example, when wr
150a0 69 74 69 6e 67 20 74 6f 20 61 20 74 61 62 6c 65 iting to a table
150a1 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 with root-page
150a2 69 52 6f 6f 74 20 76 69 61 20 0a 2a 2a 20 42 74 iRoot via .** Bt
150a3 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 ree connection p
150a4 42 74 72 65 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 Btree:.**.**
150a5 61 73 73 65 72 74 28 20 68 61 73 53 68 61 72 65 assert( hasShare
150a6 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 dCacheTableLock(
150a7 70 42 74 72 65 65 2c 20 69 52 6f 6f 74 2c 20 30 pBtree, iRoot, 0
150a8 2c 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 29 3b , WRITE_LOCK) );
150a9 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 72 69 74 .**.** When writ
150aa 69 6e 67 20 74 6f 20 61 6e 20 69 6e 64 65 78 20 ing to an index
150ab 74 68 61 74 20 72 65 73 69 64 65 73 20 69 6e 20 that resides in
150ac 61 20 73 68 61 72 61 62 6c 65 20 64 61 74 61 62 a sharable datab
150ad 61 73 65 2c 20 74 68 65 20 0a 2a 2a 20 63 61 6c ase, the .** cal
150ae 6c 65 72 20 73 68 6f 75 6c 64 20 68 61 76 65 20 ler should have
150af 66 69 72 73 74 20 6f 62 74 61 69 6e 65 64 20 61 first obtained a
150b0 20 6c 6f 63 6b 20 73 70 65 63 69 66 79 69 6e 67 lock specifying
150b1 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f the root page o
150b2 66 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 73 70 f.** the corresp
150b3 6f 6e 64 69 6e 67 20 74 61 62 6c 65 2e 20 54 68 onding table. Th
150b4 69 73 20 6d 61 6b 65 73 20 74 68 69 6e 67 73 20 is makes things
150b5 61 20 62 69 74 20 6d 6f 72 65 20 63 6f 6d 70 6c a bit more compl
150b6 69 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20 74 68 icated,.** as th
150b7 69 73 20 6d 6f 64 75 6c 65 20 74 72 65 61 74 73 is module treats
150b8 20 65 61 63 68 20 74 61 62 6c 65 20 61 73 20 61 each table as a
150b9 20 73 65 70 61 72 61 74 65 20 73 74 72 75 63 74 separate struct
150ba 75 72 65 2e 20 54 6f 20 64 65 74 65 72 6d 69 6e ure. To determin
150bb 65 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 63 e.** the table c
150bc 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 orresponding to
150bd 74 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20 the index being
150be 77 72 69 74 74 65 6e 2c 20 74 68 69 73 0a 2a 2a written, this.**
150bf 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 74 6f function has to
150c0 20 73 65 61 72 63 68 20 74 68 72 6f 75 67 68 20 search through
150c1 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 the database sch
150c2 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 ema..**.** Inste
150c3 61 64 20 6f 66 20 61 20 6c 6f 63 6b 20 6f 6e 20 ad of a lock on
150c4 74 68 65 20 74 61 62 6c 65 2f 69 6e 64 65 78 20 the table/index
150c5 72 6f 6f 74 65 64 20 61 74 20 70 61 67 65 20 69 rooted at page i
150c6 52 6f 6f 74 2c 20 74 68 65 20 63 61 6c 6c 65 72 Root, the caller
150c7 20 6d 61 79 0a 2a 2a 20 68 6f 6c 64 20 61 20 77 may.** hold a w
150c8 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 rite-lock on the
150c9 20 73 63 68 65 6d 61 20 74 61 62 6c 65 20 28 72 schema table (r
150ca 6f 6f 74 20 70 61 67 65 20 31 29 2e 20 54 68 69 oot page 1). Thi
150cb 73 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 61 63 63 s is also.** acc
150cc 65 70 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 eptable..*/.stat
150cd 69 63 20 69 6e 74 20 68 61 73 53 68 61 72 65 64 ic int hasShared
150ce 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 0a CacheTableLock(.
150cf 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c Btree *pBtree,
150d0 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 /* Hand
150d1 6c 65 20 74 68 61 74 20 6d 75 73 74 20 68 6f 6c le that must hol
150d2 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f d lock */. Pgno
150d3 20 69 52 6f 6f 74 2c 20 20 20 20 20 20 20 20 20 iRoot,
150d4 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 /* Root page
150d5 6f 66 20 62 2d 74 72 65 65 20 2a 2f 0a 20 20 69 of b-tree */. i
150d6 6e 74 20 69 73 49 6e 64 65 78 2c 20 20 20 20 20 nt isIndex,
150d7 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
150d8 20 69 52 6f 6f 74 20 69 73 20 74 68 65 20 72 6f iRoot is the ro
150d9 6f 74 20 6f 66 20 61 6e 20 69 6e 64 65 78 20 62 ot of an index b
150da 2d 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 65 -tree */. int e
150db 4c 6f 63 6b 54 79 70 65 20 20 20 20 20 20 20 20 LockType
150dc 20 20 2f 2a 20 52 65 71 75 69 72 65 64 20 6c 6f /* Required lo
150dd 63 6b 20 74 79 70 65 20 28 52 45 41 44 5f 4c 4f ck type (READ_LO
150de 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b CK or WRITE_LOCK
150df 29 20 2a 2f 0a 29 7b 0a 20 20 53 63 68 65 6d 61 ) */.){. Schema
150e0 20 2a 70 53 63 68 65 6d 61 20 3d 20 28 53 63 68 *pSchema = (Sch
150e1 65 6d 61 20 2a 29 70 42 74 72 65 65 2d 3e 70 42 ema *)pBtree->pB
150e2 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 50 67 t->pSchema;. Pg
150e3 6e 6f 20 69 54 61 62 20 3d 20 30 3b 0a 20 20 42 no iTab = 0;. B
150e4 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 0a 0a 20 tLock *pLock;..
150e5 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 /* If this data
150e6 62 61 73 65 20 69 73 20 6e 6f 74 20 73 68 61 72 base is not shar
150e7 65 61 62 6c 65 2c 20 6f 72 20 69 66 20 74 68 65 eable, or if the
150e8 20 63 6c 69 65 6e 74 20 69 73 20 72 65 61 64 69 client is readi
150e9 6e 67 0a 20 20 2a 2a 20 61 6e 64 20 68 61 73 20 ng. ** and has
150ea 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 the read-uncommi
150eb 74 74 65 64 20 66 6c 61 67 20 73 65 74 2c 20 74 tted flag set, t
150ec 68 65 6e 20 6e 6f 20 6c 6f 63 6b 20 69 73 20 72 hen no lock is r
150ed 65 71 75 69 72 65 64 2e 20 0a 20 20 2a 2a 20 52 equired. . ** R
150ee 65 74 75 72 6e 20 74 72 75 65 20 69 6d 6d 65 64 eturn true immed
150ef 69 61 74 65 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69 iately.. */. i
150f0 66 28 20 28 70 42 74 72 65 65 2d 3e 73 68 61 72 f( (pBtree->shar
150f1 61 62 6c 65 3d 3d 30 29 0a 20 20 20 7c 7c 20 28 able==0). || (
150f2 65 4c 6f 63 6b 54 79 70 65 3d 3d 52 45 41 44 5f eLockType==READ_
150f3 4c 4f 43 4b 20 26 26 20 28 70 42 74 72 65 65 2d LOCK && (pBtree-
150f4 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c >db->flags & SQL
150f5 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 ITE_ReadUncommit
150f6 74 65 64 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 ted)). ){. r
150f7 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 eturn 1;. }..
150f8 2f 2a 20 49 66 20 74 68 65 20 63 6c 69 65 6e 74 /* If the client
150f9 20 69 73 20 72 65 61 64 69 6e 67 20 20 6f 72 20 is reading or
150fa 77 72 69 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 writing an index
150fb 20 61 6e 64 20 74 68 65 20 73 63 68 65 6d 61 20 and the schema
150fc 69 73 0a 20 20 2a 2a 20 6e 6f 74 20 6c 6f 61 64 is. ** not load
150fd 65 64 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74 ed, then it is t
150fe 6f 6f 20 64 69 66 66 69 63 75 6c 74 20 74 6f 20 oo difficult to
150ff 61 63 74 75 61 6c 6c 79 20 63 68 65 63 6b 20 74 actually check t
15100 6f 20 73 65 65 20 69 66 0a 20 20 2a 2a 20 74 68 o see if. ** th
15101 65 20 63 6f 72 72 65 63 74 20 6c 6f 63 6b 73 20 e correct locks
15102 61 72 65 20 68 65 6c 64 2e 20 20 53 6f 20 64 6f are held. So do
15103 20 6e 6f 74 20 62 6f 74 68 65 72 20 2d 20 6a 75 not bother - ju
15104 73 74 20 72 65 74 75 72 6e 20 74 72 75 65 2e 0a st return true..
15105 20 20 2a 2a 20 54 68 69 73 20 63 61 73 65 20 64 ** This case d
15106 6f 65 73 20 6e 6f 74 20 63 6f 6d 65 20 75 70 20 oes not come up
15107 76 65 72 79 20 6f 66 74 65 6e 20 61 6e 79 68 6f very often anyho
15108 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 w.. */. if( is
15109 49 6e 64 65 78 20 26 26 20 28 21 70 53 63 68 65 Index && (!pSche
1510a 6d 61 20 7c 7c 20 28 70 53 63 68 65 6d 61 2d 3e ma || (pSchema->
1510b 66 6c 61 67 73 26 44 42 5f 53 63 68 65 6d 61 4c flags&DB_SchemaL
1510c 6f 61 64 65 64 29 3d 3d 30 29 20 29 7b 0a 20 20 oaded)==0) ){.
1510d 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
1510e 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 . /* Figure out
1510f 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 74 the root-page t
15110 68 61 74 20 74 68 65 20 6c 6f 63 6b 20 73 68 6f hat the lock sho
15111 75 6c 64 20 62 65 20 68 65 6c 64 20 6f 6e 2e 20 uld be held on.
15112 46 6f 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20 62 For table. ** b
15113 2d 74 72 65 65 73 2c 20 74 68 69 73 20 69 73 20 -trees, this is
15114 6a 75 73 74 20 74 68 65 20 72 6f 6f 74 20 70 61 just the root pa
15115 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 ge of the b-tree
15116 20 62 65 69 6e 67 20 72 65 61 64 20 6f 72 0a 20 being read or.
15117 20 2a 2a 20 77 72 69 74 74 65 6e 2e 20 46 6f 72 ** written. For
15118 20 69 6e 64 65 78 20 62 2d 74 72 65 65 73 2c 20 index b-trees,
15119 69 74 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70 it is the root p
1511a 61 67 65 20 6f 66 20 74 68 65 20 61 73 73 6f 63 age of the assoc
1511b 69 61 74 65 64 0a 20 20 2a 2a 20 74 61 62 6c 65 iated. ** table
1511c 2e 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 49 6e . */. if( isIn
1511d 64 65 78 20 29 7b 0a 20 20 20 20 48 61 73 68 45 dex ){. HashE
1511e 6c 65 6d 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 lem *p;. for(
1511f 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 p=sqliteHashFirs
15120 74 28 26 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 t(&pSchema->idxH
15121 61 73 68 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 ash); p; p=sqlit
15122 65 48 61 73 68 4e 65 78 74 28 70 29 29 7b 0a 20 eHashNext(p)){.
15123 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 Index *pIdx
15124 20 3d 20 28 49 6e 64 65 78 20 2a 29 73 71 6c 69 = (Index *)sqli
15125 74 65 48 61 73 68 44 61 74 61 28 70 29 3b 0a 20 teHashData(p);.
15126 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74 if( pIdx->t
15127 6e 75 6d 3d 3d 28 69 6e 74 29 69 52 6f 6f 74 20 num==(int)iRoot
15128 29 7b 0a 20 20 20 20 20 20 20 20 69 54 61 62 20 ){. iTab
15129 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e = pIdx->pTable->
1512a 74 6e 75 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 tnum;. }.
1512b 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
1512c 20 69 54 61 62 20 3d 20 69 52 6f 6f 74 3b 0a 20 iTab = iRoot;.
1512d 20 7d 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 }.. /* Search
1512e 66 6f 72 20 74 68 65 20 72 65 71 75 69 72 65 64 for the required
1512f 20 6c 6f 63 6b 2e 20 45 69 74 68 65 72 20 61 20 lock. Either a
15130 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 72 6f write-lock on ro
15131 6f 74 2d 70 61 67 65 20 69 54 61 62 2c 20 61 20 ot-page iTab, a
15132 0a 20 20 2a 2a 20 77 72 69 74 65 2d 6c 6f 63 6b . ** write-lock
15133 20 6f 6e 20 74 68 65 20 73 63 68 65 6d 61 20 74 on the schema t
15134 61 62 6c 65 2c 20 6f 72 20 28 69 66 20 74 68 65 able, or (if the
15135 20 63 6c 69 65 6e 74 20 69 73 20 72 65 61 64 69 client is readi
15136 6e 67 29 20 61 0a 20 20 2a 2a 20 72 65 61 64 2d ng) a. ** read-
15137 6c 6f 63 6b 20 6f 6e 20 69 54 61 62 20 77 69 6c lock on iTab wil
15138 6c 20 73 75 66 66 69 63 65 2e 20 52 65 74 75 72 l suffice. Retur
15139 6e 20 31 20 69 66 20 61 6e 79 20 6f 66 20 74 68 n 1 if any of th
1513a 65 73 65 20 61 72 65 20 66 6f 75 6e 64 2e 20 20 ese are found.
1513b 2a 2f 0a 20 20 66 6f 72 28 70 4c 6f 63 6b 3d 70 */. for(pLock=p
1513c 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 4c 6f 63 Btree->pBt->pLoc
1513d 6b 3b 20 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3d k; pLock; pLock=
1513e 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 29 7b 0a 20 pLock->pNext){.
1513f 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 if( pLock->pB
15140 74 72 65 65 3d 3d 70 42 74 72 65 65 20 0a 20 20 tree==pBtree .
15141 20 20 20 26 26 20 28 70 4c 6f 63 6b 2d 3e 69 54 && (pLock->iT
15142 61 62 6c 65 3d 3d 69 54 61 62 20 7c 7c 20 28 70 able==iTab || (p
15143 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 Lock->eLock==WRI
15144 54 45 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b TE_LOCK && pLock
15145 2d 3e 69 54 61 62 6c 65 3d 3d 31 29 29 0a 20 20 ->iTable==1)).
15146 20 20 20 26 26 20 70 4c 6f 63 6b 2d 3e 65 4c 6f && pLock->eLo
15147 63 6b 3e 3d 65 4c 6f 63 6b 54 79 70 65 20 0a 20 ck>=eLockType .
15148 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 ){. retu
15149 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a rn 1;. }. }.
1514a 0a 20 20 2f 2a 20 46 61 69 6c 65 64 20 74 6f 20 . /* Failed to
1514b 66 69 6e 64 20 74 68 65 20 72 65 71 75 69 72 65 find the require
1514c 64 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 72 65 74 d lock. */. ret
1514d 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 urn 0;.}.#endif
1514e 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 /* SQLITE_DEBUG
1514f 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */..#ifdef SQLIT
15150 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 2a 2a 20 E_DEBUG./*.****
15151 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 This function ma
15152 79 20 62 65 20 75 73 65 64 20 61 73 20 70 61 72 y be used as par
15153 74 20 6f 66 20 61 73 73 65 72 74 28 29 20 73 74 t of assert() st
15154 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a atements only. *
15155 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e ***.**.** Return
15156 20 74 72 75 65 20 69 66 20 69 74 20 77 6f 75 6c true if it woul
15157 64 20 62 65 20 69 6c 6c 65 67 61 6c 20 66 6f 72 d be illegal for
15158 20 70 42 74 72 65 65 20 74 6f 20 77 72 69 74 65 pBtree to write
15159 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 74 61 62 into the.** tab
1515a 6c 65 20 6f 72 20 69 6e 64 65 78 20 72 6f 6f 74 le or index root
1515b 65 64 20 61 74 20 69 52 6f 6f 74 20 62 65 63 61 ed at iRoot beca
1515c 75 73 65 20 6f 74 68 65 72 20 73 68 61 72 65 64 use other shared
1515d 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 connections are
1515e 0a 2a 2a 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 .** simultaneous
1515f 6c 79 20 72 65 61 64 69 6e 67 20 74 68 61 74 20 ly reading that
15160 73 61 6d 65 20 74 61 62 6c 65 20 6f 72 20 69 6e same table or in
15161 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 dex..**.** It is
15162 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 70 42 74 illegal for pBt
15163 72 65 65 20 74 6f 20 77 72 69 74 65 20 69 66 20 ree to write if
15164 73 6f 6d 65 20 6f 74 68 65 72 20 42 74 72 65 65 some other Btree
15165 20 6f 62 6a 65 63 74 20 74 68 61 74 0a 2a 2a 20 object that.**
15166 73 68 61 72 65 73 20 74 68 65 20 73 61 6d 65 20 shares the same
15167 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 BtShared object
15168 69 73 20 63 75 72 72 65 6e 74 6c 79 20 72 65 61 is currently rea
15169 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 0a ding or writing.
1516a 2a 2a 20 74 68 65 20 69 52 6f 6f 74 20 74 61 62 ** the iRoot tab
1516b 6c 65 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20 le. Except, if
1516c 74 68 65 20 6f 74 68 65 72 20 42 74 72 65 65 20 the other Btree
1516d 6f 62 6a 65 63 74 20 68 61 73 20 74 68 65 0a 2a object has the.*
1516e 2a 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 * read-uncommitt
1516f 65 64 20 66 6c 61 67 20 73 65 74 2c 20 74 68 65 ed flag set, the
15170 6e 20 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 74 n it is OK for t
15171 68 65 20 6f 74 68 65 72 20 6f 62 6a 65 63 74 20 he other object
15172 74 6f 0a 2a 2a 20 68 61 76 65 20 61 20 72 65 61 to.** have a rea
15173 64 20 63 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20 d cursor..**.**
15174 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 62 65 66 For example, bef
15175 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20 61 ore writing to a
15176 6e 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 ny part of the t
15177 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 0a 2a 2a able or index.**
15178 20 72 6f 6f 74 65 64 20 61 74 20 70 61 67 65 20 rooted at page
15179 69 52 6f 6f 74 2c 20 6f 6e 65 20 73 68 6f 75 6c iRoot, one shoul
1517a 64 20 63 61 6c 6c 3a 0a 2a 2a 0a 2a 2a 20 20 20 d call:.**.**
1517b 20 61 73 73 65 72 74 28 20 21 68 61 73 52 65 61 assert( !hasRea
1517c 64 43 6f 6e 66 6c 69 63 74 73 28 70 42 74 72 65 dConflicts(pBtre
1517d 65 2c 20 69 52 6f 6f 74 29 20 29 3b 0a 2a 2f 0a e, iRoot) );.*/.
1517e 73 74 61 74 69 63 20 69 6e 74 20 68 61 73 52 65 static int hasRe
1517f 61 64 43 6f 6e 66 6c 69 63 74 73 28 42 74 72 65 adConflicts(Btre
15180 65 20 2a 70 42 74 72 65 65 2c 20 50 67 6e 6f 20 e *pBtree, Pgno
15181 69 52 6f 6f 74 29 7b 0a 20 20 42 74 43 75 72 73 iRoot){. BtCurs
15182 6f 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 or *p;. for(p=p
15183 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 43 75 72 Btree->pBt->pCur
15184 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 sor; p; p=p->pNe
15185 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e xt){. if( p->
15186 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 20 pgnoRoot==iRoot
15187 0a 20 20 20 20 20 26 26 20 70 2d 3e 70 42 74 72 . && p->pBtr
15188 65 65 21 3d 70 42 74 72 65 65 0a 20 20 20 20 20 ee!=pBtree.
15189 26 26 20 30 3d 3d 28 70 2d 3e 70 42 74 72 65 65 && 0==(p->pBtree
1518a 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 ->db->flags & SQ
1518b 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 LITE_ReadUncommi
1518c 74 74 65 64 29 0a 20 20 20 20 29 7b 0a 20 20 20 tted). ){.
1518d 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
1518e 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1518f 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 20 20 2f 0;.}.#endif /
15190 2a 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f * #ifdef SQLITE_
15191 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 DEBUG */../*.**
15192 51 75 65 72 79 20 74 6f 20 73 65 65 20 69 66 20 Query to see if
15193 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 20 6d Btree handle p m
15194 61 79 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63 6b ay obtain a lock
15195 20 6f 66 20 74 79 70 65 20 65 4c 6f 63 6b 20 0a of type eLock .
15196 2a 2a 20 28 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 ** (READ_LOCK or
15197 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 6f 6e 20 WRITE_LOCK) on
15198 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 the table with r
15199 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 2e 20 52 oot-page iTab. R
1519a 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f eturn.** SQLITE_
1519b 4f 4b 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 6d OK if the lock m
1519c 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64 20 28 ay be obtained (
1519d 62 79 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 65 by calling.** se
1519e 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c tSharedCacheTabl
1519f 65 4c 6f 63 6b 28 29 29 2c 20 6f 72 20 53 51 4c eLock()), or SQL
151a0 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 6e 6f ITE_LOCKED if no
151a1 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
151a2 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 querySharedCach
151a3 65 54 61 62 6c 65 4c 6f 63 6b 28 42 74 72 65 65 eTableLock(Btree
151a4 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 2c 20 *p, Pgno iTab,
151a5 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 u8 eLock){. BtS
151a6 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
151a7 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 pBt;. BtLock *p
151a8 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 Iter;.. assert(
151a9 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c sqlite3BtreeHol
151aa 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 dsMutex(p) );.
151ab 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 assert( eLock==R
151ac 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63 EAD_LOCK || eLoc
151ad 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b k==WRITE_LOCK );
151ae 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 . assert( p->db
151af 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
151b0 20 21 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 !(p->db->flags&
151b1 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d SQLITE_ReadUncom
151b2 6d 69 74 74 65 64 29 7c 7c 65 4c 6f 63 6b 3d 3d mitted)||eLock==
151b3 57 52 49 54 45 5f 4c 4f 43 4b 7c 7c 69 54 61 62 WRITE_LOCK||iTab
151b4 3d 3d 31 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 ==1 );. . /* I
151b5 66 20 72 65 71 75 65 73 74 69 6e 67 20 61 20 77 f requesting a w
151b6 72 69 74 65 2d 6c 6f 63 6b 2c 20 74 68 65 6e 20 rite-lock, then
151b7 74 68 65 20 42 74 72 65 65 20 6d 75 73 74 20 68 the Btree must h
151b8 61 76 65 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 ave an open writ
151b9 65 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 e. ** transacti
151ba 6f 6e 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 2e on on this file.
151bb 20 41 6e 64 2c 20 6f 62 76 69 6f 75 73 6c 79 2c And, obviously,
151bc 20 66 6f 72 20 74 68 69 73 20 74 6f 20 62 65 20 for this to be
151bd 73 6f 20 74 68 65 72 65 20 0a 20 20 2a 2a 20 6d so there . ** m
151be 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 77 ust be an open w
151bf 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
151c0 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 69 74 73 on the file its
151c1 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 elf.. */. asse
151c2 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f rt( eLock==READ_
151c3 4c 4f 43 4b 20 7c 7c 20 28 70 3d 3d 70 42 74 2d LOCK || (p==pBt-
151c4 3e 70 57 72 69 74 65 72 20 26 26 20 70 2d 3e 69 >pWriter && p->i
151c5 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 nTrans==TRANS_WR
151c6 49 54 45 29 20 29 3b 0a 20 20 61 73 73 65 72 74 ITE) );. assert
151c7 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f ( eLock==READ_LO
151c8 43 4b 20 7c 7c 20 70 42 74 2d 3e 69 6e 54 72 61 CK || pBt->inTra
151c9 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f nsaction==TRANS_
151ca 57 52 49 54 45 20 29 3b 0a 20 20 0a 20 20 2f 2a WRITE );. . /*
151cb 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
151cc 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 a no-op if the
151cd 73 68 61 72 65 64 2d 63 61 63 68 65 20 69 73 20 shared-cache is
151ce 6e 6f 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 not enabled */.
151cf 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c if( !p->sharabl
151d0 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
151d1 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a SQLITE_OK;. }..
151d2 20 20 2f 2a 20 49 66 20 73 6f 6d 65 20 6f 74 68 /* If some oth
151d3 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 er connection is
151d4 20 68 6f 6c 64 69 6e 67 20 61 6e 20 65 78 63 6c holding an excl
151d5 75 73 69 76 65 20 6c 6f 63 6b 2c 20 74 68 65 0a usive lock, the.
151d6 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 20 6c ** requested l
151d7 6f 63 6b 20 6d 61 79 20 6e 6f 74 20 62 65 20 6f ock may not be o
151d8 62 74 61 69 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 btained.. */.
151d9 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74 65 72 if( pBt->pWriter
151da 21 3d 70 20 26 26 20 70 42 74 2d 3e 69 73 45 78 !=p && pBt->isEx
151db 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 20 20 73 clusive ){. s
151dc 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e qlite3Connection
151dd 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 Blocked(p->db, p
151de 42 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62 29 Bt->pWriter->db)
151df 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
151e0 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 ITE_LOCKED_SHARE
151e1 44 43 41 43 48 45 3b 0a 20 20 7d 0a 0a 20 20 66 DCACHE;. }.. f
151e2 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c or(pIter=pBt->pL
151e3 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 ock; pIter; pIte
151e4 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b r=pIter->pNext){
151e5 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 64 . /* The cond
151e6 69 74 69 6f 6e 20 28 70 49 74 65 72 2d 3e 65 4c ition (pIter->eL
151e7 6f 63 6b 21 3d 65 4c 6f 63 6b 29 20 69 6e 20 74 ock!=eLock) in t
151e8 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 66 28 he following if(
151e9 2e 2e 2e 29 20 0a 20 20 20 20 2a 2a 20 73 74 61 ...) . ** sta
151ea 74 65 6d 65 6e 74 20 69 73 20 61 20 73 69 6d 70 tement is a simp
151eb 6c 69 66 69 63 61 74 69 6f 6e 20 6f 66 3a 0a 20 lification of:.
151ec 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 28 **. ** (
151ed 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 eLock==WRITE_LOC
151ee 4b 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 K || pIter->eLoc
151ef 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 29 0a 20 k==WRITE_LOCK).
151f0 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 73 69 6e **. ** sin
151f1 63 65 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 ce we know that
151f2 69 66 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f if eLock==WRITE_
151f3 4c 4f 43 4b 2c 20 74 68 65 6e 20 6e 6f 20 6f 74 LOCK, then no ot
151f4 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 her connection.
151f5 20 20 20 2a 2a 20 6d 61 79 20 68 6f 6c 64 20 61 ** may hold a
151f6 20 57 52 49 54 45 5f 4c 4f 43 4b 20 6f 6e 20 61 WRITE_LOCK on a
151f7 6e 79 20 74 61 62 6c 65 20 69 6e 20 74 68 69 73 ny table in this
151f8 20 66 69 6c 65 20 28 73 69 6e 63 65 20 74 68 65 file (since the
151f9 72 65 20 63 61 6e 0a 20 20 20 20 2a 2a 20 6f 6e re can. ** on
151fa 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 20 77 ly be a single w
151fb 72 69 74 65 72 29 2e 0a 20 20 20 20 2a 2f 0a 20 riter).. */.
151fc 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 72 assert( pIter
151fd 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f ->eLock==READ_LO
151fe 43 4b 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f CK || pIter->eLo
151ff 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 ck==WRITE_LOCK )
15200 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 4c ;. assert( eL
15201 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c ock==READ_LOCK |
15202 7c 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 3d | pIter->pBtree=
15203 3d 70 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f =p || pIter->eLo
15204 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a ck==READ_LOCK);.
15205 20 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e 70 if( pIter->p
15206 42 74 72 65 65 21 3d 70 20 26 26 20 70 49 74 65 Btree!=p && pIte
15207 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 r->iTable==iTab
15208 26 26 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 && pIter->eLock!
15209 3d 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 =eLock ){.
1520a 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f sqlite3Connectio
1520b 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 nBlocked(p->db,
1520c 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64 pIter->pBtree->d
1520d 62 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 4c b);. if( eL
1520e 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 ock==WRITE_LOCK
1520f 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 ){. asser
15210 74 28 20 70 3d 3d 70 42 74 2d 3e 70 57 72 69 74 t( p==pBt->pWrit
15211 65 72 20 29 3b 0a 20 20 20 20 20 20 20 20 70 42 er );. pB
15212 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 31 t->isPending = 1
15213 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
15214 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f return SQLITE_LO
15215 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 CKED_SHAREDCACHE
15216 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
15217 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
15218 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c }.#endif /* !SQL
15219 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
1521a 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 CACHE */..#ifnde
1521b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
1521c 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a ARED_CACHE./*.**
1521d 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 Add a lock on t
1521e 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f he table with ro
1521f 6f 74 2d 70 61 67 65 20 69 54 61 62 6c 65 20 74 ot-page iTable t
15220 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 o the shared-btr
15221 65 65 20 75 73 65 64 0a 2a 2a 20 62 79 20 42 74 ee used.** by Bt
15222 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 50 61 ree handle p. Pa
15223 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b 20 6d 75 rameter eLock mu
15224 73 74 20 62 65 20 65 69 74 68 65 72 20 52 45 41 st be either REA
15225 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 D_LOCK or .** WR
15226 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 ITE_LOCK..**.**
15227 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 This function as
15228 73 75 6d 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 sumes the follow
15229 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 61 29 ing:.**.** (a)
1522a 20 54 68 65 20 73 70 65 63 69 66 69 65 64 20 42 The specified B
1522b 74 72 65 65 20 6f 62 6a 65 63 74 20 70 20 69 73 tree object p is
1522c 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f 20 61 20 connected to a
1522d 73 68 61 72 61 62 6c 65 0a 2a 2a 20 20 20 20 20 sharable.**
1522e 20 20 64 61 74 61 62 61 73 65 20 28 6f 6e 65 20 database (one
1522f 77 69 74 68 20 74 68 65 20 42 74 53 68 61 72 65 with the BtShare
15230 64 2e 73 68 61 72 61 62 6c 65 20 66 6c 61 67 20 d.sharable flag
15231 73 65 74 29 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 set), and.**.**
15232 20 20 28 62 29 20 4e 6f 20 6f 74 68 65 72 20 42 (b) No other B
15233 74 72 65 65 20 6f 62 6a 65 63 74 73 20 68 6f 6c tree objects hol
15234 64 20 61 20 6c 6f 63 6b 20 74 68 61 74 20 63 6f d a lock that co
15235 6e 66 6c 69 63 74 73 0a 2a 2a 20 20 20 20 20 20 nflicts.**
15236 20 77 69 74 68 20 74 68 65 20 72 65 71 75 65 73 with the reques
15237 74 65 64 20 6c 6f 63 6b 20 28 69 2e 65 2e 20 71 ted lock (i.e. q
15238 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 uerySharedCacheT
15239 61 62 6c 65 4c 6f 63 6b 28 29 20 68 61 73 0a 2a ableLock() has.*
1523a 2a 20 20 20 20 20 20 20 61 6c 72 65 61 64 79 20 * already
1523b 62 65 65 6e 20 63 61 6c 6c 65 64 20 61 6e 64 20 been called and
1523c 72 65 74 75 72 6e 65 64 20 53 51 4c 49 54 45 5f returned SQLITE_
1523d 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 OK)..**.** SQLIT
1523e 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
1523f 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 if the lock is
15240 61 64 64 65 64 20 73 75 63 63 65 73 73 66 75 6c added successful
15241 6c 79 2e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d ly. SQLITE_NOMEM
15242 20 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 .** is returned
15243 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 61 74 74 if a malloc att
15244 65 6d 70 74 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 empt fails..*/.s
15245 74 61 74 69 63 20 69 6e 74 20 73 65 74 53 68 61 tatic int setSha
15246 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
15247 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f k(Btree *p, Pgno
15248 20 69 54 61 62 6c 65 2c 20 75 38 20 65 4c 6f 63 iTable, u8 eLoc
15249 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a k){. BtShared *
1524a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
1524b 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 BtLock *pLock =
1524c 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 0;. BtLock *pIt
1524d 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 er;.. assert( s
1524e 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
1524f 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 Mutex(p) );. as
15250 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 sert( eLock==REA
15251 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63 6b 3d D_LOCK || eLock=
15252 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 =WRITE_LOCK );.
15253 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 21 3d assert( p->db!=
15254 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 20 63 6f 6e 0 );.. /* A con
15255 6e 65 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 nection with the
15256 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 read-uncommitte
15257 64 20 66 6c 61 67 20 73 65 74 20 77 69 6c 6c 20 d flag set will
15258 6e 65 76 65 72 20 74 72 79 20 74 6f 0a 20 20 2a never try to. *
15259 2a 20 6f 62 74 61 69 6e 20 61 20 72 65 61 64 2d * obtain a read-
1525a 6c 6f 63 6b 20 75 73 69 6e 67 20 74 68 69 73 20 lock using this
1525b 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 6f 6e function. The on
1525c 6c 79 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 62 74 ly read-lock obt
1525d 61 69 6e 65 64 0a 20 20 2a 2a 20 62 79 20 61 20 ained. ** by a
1525e 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 20 72 65 connection in re
1525f 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 6d ad-uncommitted m
15260 6f 64 65 20 69 73 20 6f 6e 20 74 68 65 20 73 71 ode is on the sq
15261 6c 69 74 65 5f 6d 61 73 74 65 72 20 0a 20 20 2a lite_master . *
15262 2a 20 74 61 62 6c 65 2c 20 61 6e 64 20 74 68 61 * table, and tha
15263 74 20 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e t lock is obtain
15264 65 64 20 69 6e 20 42 74 72 65 65 42 65 67 69 6e ed in BtreeBegin
15265 54 72 61 6e 73 28 29 2e 20 20 2a 2f 0a 20 20 61 Trans(). */. a
15266 73 73 65 72 74 28 20 30 3d 3d 28 70 2d 3e 64 62 ssert( 0==(p->db
15267 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 ->flags&SQLITE_R
15268 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 20 eadUncommitted)
15269 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f || eLock==WRITE_
1526a 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 LOCK );.. /* Th
1526b 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 is function shou
1526c 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 ld only be calle
1526d 64 20 6f 6e 20 61 20 73 68 61 72 61 62 6c 65 20 d on a sharable
1526e 62 2d 74 72 65 65 20 61 66 74 65 72 20 69 74 20 b-tree after it
1526f 0a 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 64 . ** has been d
15270 65 74 65 72 6d 69 6e 65 64 20 74 68 61 74 20 6e etermined that n
15271 6f 20 6f 74 68 65 72 20 62 2d 74 72 65 65 20 68 o other b-tree h
15272 6f 6c 64 73 20 61 20 63 6f 6e 66 6c 69 63 74 69 olds a conflicti
15273 6e 67 20 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 ng lock. */. a
15274 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 ssert( p->sharab
15275 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 le );. assert(
15276 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 SQLITE_OK==query
15277 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
15278 4c 6f 63 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20 Lock(p, iTable,
15279 65 4c 6f 63 6b 29 20 29 3b 0a 0a 20 20 2f 2a 20 eLock) );.. /*
1527a 46 69 72 73 74 20 73 65 61 72 63 68 20 74 68 65 First search the
1527b 20 6c 69 73 74 20 66 6f 72 20 61 6e 20 65 78 69 list for an exi
1527c 73 74 69 6e 67 20 6c 6f 63 6b 20 6f 6e 20 74 68 sting lock on th
1527d 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 66 is table. */. f
1527e 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c or(pIter=pBt->pL
1527f 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 ock; pIter; pIte
15280 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b r=pIter->pNext){
15281 0a 20 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e . if( pIter->
15282 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20 26 iTable==iTable &
15283 26 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 3d & pIter->pBtree=
15284 3d 70 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 =p ){. pLoc
15285 6b 20 3d 20 70 49 74 65 72 3b 0a 20 20 20 20 20 k = pIter;.
15286 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
15287 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61 }.. /* If the a
15288 62 6f 76 65 20 73 65 61 72 63 68 20 64 69 64 20 bove search did
15289 6e 6f 74 20 66 69 6e 64 20 61 20 42 74 4c 6f 63 not find a BtLoc
1528a 6b 20 73 74 72 75 63 74 20 61 73 73 6f 63 69 61 k struct associa
1528b 74 69 6e 67 20 42 74 72 65 65 20 70 0a 20 20 2a ting Btree p. *
1528c 2a 20 77 69 74 68 20 74 61 62 6c 65 20 69 54 61 * with table iTa
1528d 62 6c 65 2c 20 61 6c 6c 6f 63 61 74 65 20 6f 6e ble, allocate on
1528e 65 20 61 6e 64 20 6c 69 6e 6b 20 69 74 20 69 6e e and link it in
1528f 74 6f 20 74 68 65 20 6c 69 73 74 2e 0a 20 20 2a to the list.. *
15290 2f 0a 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 /. if( !pLock )
15291 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20 28 42 {. pLock = (B
15292 74 4c 6f 63 6b 20 2a 29 73 71 6c 69 74 65 33 4d tLock *)sqlite3M
15293 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 allocZero(sizeof
15294 28 42 74 4c 6f 63 6b 29 29 3b 0a 20 20 20 20 69 (BtLock));. i
15295 66 28 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 f( !pLock ){.
15296 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
15297 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 _NOMEM;. }.
15298 20 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 20 pLock->iTable
15299 3d 20 69 54 61 62 6c 65 3b 0a 20 20 20 20 70 4c = iTable;. pL
1529a 6f 63 6b 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b ock->pBtree = p;
1529b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 . pLock->pNex
1529c 74 20 3d 20 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a t = pBt->pLock;.
1529d 20 20 20 20 70 42 74 2d 3e 70 4c 6f 63 6b 20 3d pBt->pLock =
1529e 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f pLock;. }.. /
1529f 2a 20 53 65 74 20 74 68 65 20 42 74 4c 6f 63 6b * Set the BtLock
152a0 2e 65 4c 6f 63 6b 20 76 61 72 69 61 62 6c 65 20 .eLock variable
152a1 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f to the maximum o
152a2 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f f the current lo
152a3 63 6b 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 ck. ** and the
152a4 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 2e 20 requested lock.
152a5 54 68 69 73 20 6d 65 61 6e 73 20 69 66 20 61 20 This means if a
152a6 77 72 69 74 65 2d 6c 6f 63 6b 20 77 61 73 20 61 write-lock was a
152a7 6c 72 65 61 64 79 20 68 65 6c 64 0a 20 20 2a 2a lready held. **
152a8 20 61 6e 64 20 61 20 72 65 61 64 2d 6c 6f 63 6b and a read-lock
152a9 20 72 65 71 75 65 73 74 65 64 2c 20 77 65 20 64 requested, we d
152aa 6f 6e 27 74 20 69 6e 63 6f 72 72 65 63 74 6c 79 on't incorrectly
152ab 20 64 6f 77 6e 67 72 61 64 65 20 74 68 65 20 6c downgrade the l
152ac 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 ock.. */. asse
152ad 72 74 28 20 57 52 49 54 45 5f 4c 4f 43 4b 3e 52 rt( WRITE_LOCK>R
152ae 45 41 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 EAD_LOCK );. if
152af 28 20 65 4c 6f 63 6b 3e 70 4c 6f 63 6b 2d 3e 65 ( eLock>pLock->e
152b0 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 Lock ){. pLoc
152b1 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 65 4c 6f 63 6b k->eLock = eLock
152b2 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
152b3 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e SQLITE_OK;.}.#en
152b4 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f dif /* !SQLITE_O
152b5 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
152b6 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
152b7 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
152b8 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 CACHE./*.** Rele
152b9 61 73 65 20 61 6c 6c 20 74 68 65 20 74 61 62 6c ase all the tabl
152ba 65 20 6c 6f 63 6b 73 20 28 6c 6f 63 6b 73 20 6f e locks (locks o
152bb 62 74 61 69 6e 65 64 20 76 69 61 20 63 61 6c 6c btained via call
152bc 73 20 74 6f 0a 2a 2a 20 74 68 65 20 73 65 74 53 s to.** the setS
152bd 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
152be 6f 63 6b 28 29 20 70 72 6f 63 65 64 75 72 65 29 ock() procedure)
152bf 20 68 65 6c 64 20 62 79 20 42 74 72 65 65 20 6f held by Btree o
152c0 62 6a 65 63 74 20 70 2e 0a 2a 2a 0a 2a 2a 20 54 bject p..**.** T
152c1 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 his function ass
152c2 75 6d 65 73 20 74 68 61 74 20 42 74 72 65 65 20 umes that Btree
152c3 70 20 68 61 73 20 61 6e 20 6f 70 65 6e 20 72 65 p has an open re
152c4 61 64 20 6f 72 20 77 72 69 74 65 20 0a 2a 2a 20 ad or write .**
152c5 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 transaction. If
152c6 69 74 20 64 6f 65 73 20 6e 6f 74 2c 20 74 68 65 it does not, the
152c7 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 69 n the BtShared.i
152c8 73 50 65 6e 64 69 6e 67 20 76 61 72 69 61 62 6c sPending variabl
152c9 65 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e 63 6f e.** may be inco
152ca 72 72 65 63 74 6c 79 20 63 6c 65 61 72 65 64 2e rrectly cleared.
152cb 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
152cc 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 clearAllSharedCa
152cd 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 42 74 cheTableLocks(Bt
152ce 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 ree *p){. BtSha
152cf 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
152d0 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 2a 70 70 t;. BtLock **pp
152d1 49 74 65 72 20 3d 20 26 70 42 74 2d 3e 70 4c 6f Iter = &pBt->pLo
152d2 63 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 ck;.. assert( s
152d3 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
152d4 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 Mutex(p) );. as
152d5 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c sert( p->sharabl
152d6 65 20 7c 7c 20 30 3d 3d 2a 70 70 49 74 65 72 20 e || 0==*ppIter
152d7 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
152d8 69 6e 54 72 61 6e 73 3e 30 20 29 3b 0a 0a 20 20 inTrans>0 );..
152d9 77 68 69 6c 65 28 20 2a 70 70 49 74 65 72 20 29 while( *ppIter )
152da 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 4c {. BtLock *pL
152db 6f 63 6b 20 3d 20 2a 70 70 49 74 65 72 3b 0a 20 ock = *ppIter;.
152dc 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e assert( pBt->
152dd 69 73 45 78 63 6c 75 73 69 76 65 3d 3d 30 20 7c isExclusive==0 |
152de 7c 20 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d | pBt->pWriter==
152df 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 20 29 3b pLock->pBtree );
152e0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f . assert( pLo
152e1 63 6b 2d 3e 70 42 74 72 65 65 2d 3e 69 6e 54 72 ck->pBtree->inTr
152e2 61 6e 73 3e 3d 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 ans>=pLock->eLoc
152e3 6b 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f k );. if( pLo
152e4 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b ck->pBtree==p ){
152e5 0a 20 20 20 20 20 20 2a 70 70 49 74 65 72 20 3d . *ppIter =
152e6 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 pLock->pNext;.
152e7 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f assert( pLo
152e8 63 6b 2d 3e 69 54 61 62 6c 65 21 3d 31 20 7c 7c ck->iTable!=1 ||
152e9 20 70 4c 6f 63 6b 3d 3d 26 70 2d 3e 6c 6f 63 6b pLock==&p->lock
152ea 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c );. if( pL
152eb 6f 63 6b 2d 3e 69 54 61 62 6c 65 21 3d 31 20 29 ock->iTable!=1 )
152ec 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
152ed 33 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 3_free(pLock);.
152ee 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
152ef 7b 0a 20 20 20 20 20 20 70 70 49 74 65 72 20 3d {. ppIter =
152f0 20 26 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a &pLock->pNext;.
152f1 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 }. }.. ass
152f2 65 72 74 28 20 70 42 74 2d 3e 69 73 50 65 6e 64 ert( pBt->isPend
152f3 69 6e 67 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 ing==0 || pBt->p
152f4 57 72 69 74 65 72 20 29 3b 0a 20 20 69 66 28 20 Writer );. if(
152f5 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 20 pBt->pWriter==p
152f6 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 57 72 69 ){. pBt->pWri
152f7 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 ter = 0;. pBt
152f8 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 20 3d 20 ->isExclusive =
152f9 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73 50 65 0;. pBt->isPe
152fa 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 7d 65 6c nding = 0;. }el
152fb 73 65 20 69 66 28 20 70 42 74 2d 3e 6e 54 72 61 se if( pBt->nTra
152fc 6e 73 61 63 74 69 6f 6e 3d 3d 32 20 29 7b 0a 20 nsaction==2 ){.
152fd 20 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 /* This funct
152fe 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 ion is called wh
152ff 65 6e 20 42 74 72 65 65 20 70 20 69 73 20 63 6f en Btree p is co
15300 6e 63 6c 75 64 69 6e 67 20 69 74 73 20 0a 20 20 ncluding its .
15301 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e ** transaction
15302 2e 20 49 66 20 74 68 65 72 65 20 63 75 72 72 65 . If there curre
15303 6e 74 6c 79 20 65 78 69 73 74 73 20 61 20 77 72 ntly exists a wr
15304 69 74 65 72 2c 20 61 6e 64 20 70 20 69 73 20 6e iter, and p is n
15305 6f 74 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 77 ot. ** that w
15306 72 69 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20 riter, then the
15307 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 number of locks
15308 68 65 6c 64 20 62 79 20 63 6f 6e 6e 65 63 74 69 held by connecti
15309 6f 6e 73 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a ons other. **
1530a 20 74 68 61 6e 20 74 68 65 20 77 72 69 74 65 72 than the writer
1530b 20 6d 75 73 74 20 62 65 20 61 62 6f 75 74 20 74 must be about t
1530c 6f 20 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2e 20 o drop to zero.
1530d 49 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20 20 In this case.
1530e 20 2a 2a 20 73 65 74 20 74 68 65 20 69 73 50 65 ** set the isPe
1530f 6e 64 69 6e 67 20 66 6c 61 67 20 74 6f 20 30 2e nding flag to 0.
15310 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 . **. ** I
15311 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 63 f there is not c
15312 75 72 72 65 6e 74 6c 79 20 61 20 77 72 69 74 65 urrently a write
15313 72 2c 20 74 68 65 6e 20 42 74 53 68 61 72 65 64 r, then BtShared
15314 2e 69 73 50 65 6e 64 69 6e 67 20 6d 75 73 74 0a .isPending must.
15315 20 20 20 20 2a 2a 20 62 65 20 7a 65 72 6f 20 61 ** be zero a
15316 6c 72 65 61 64 79 2e 20 53 6f 20 74 68 69 73 20 lready. So this
15317 6e 65 78 74 20 6c 69 6e 65 20 69 73 20 68 61 72 next line is har
15318 6d 6c 65 73 73 20 69 6e 20 74 68 61 74 20 63 61 mless in that ca
15319 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 se.. */. p
1531a 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 Bt->isPending =
1531b 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 0;. }.}../*.**
1531c 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 68 This function ch
1531d 61 6e 67 65 73 20 61 6c 6c 20 77 72 69 74 65 2d anges all write-
1531e 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 42 74 locks held by Bt
1531f 72 65 65 20 70 20 69 6e 74 6f 20 72 65 61 64 2d ree p into read-
15320 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 locks..*/.static
15321 20 76 6f 69 64 20 64 6f 77 6e 67 72 61 64 65 41 void downgradeA
15322 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 llSharedCacheTab
15323 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 leLocks(Btree *p
15324 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
15325 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 Bt = p->pBt;. i
15326 66 28 20 70 42 74 2d 3e 70 57 72 69 74 65 72 3d f( pBt->pWriter=
15327 3d 70 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b =p ){. BtLock
15328 20 2a 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74 *pLock;. pBt
15329 2d 3e 70 57 72 69 74 65 72 20 3d 20 30 3b 0a 20 ->pWriter = 0;.
1532a 20 20 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 pBt->isExclus
1532b 69 76 65 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 ive = 0;. pBt
1532c 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 30 3b ->isPending = 0;
1532d 0a 20 20 20 20 66 6f 72 28 70 4c 6f 63 6b 3d 70 . for(pLock=p
1532e 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b Bt->pLock; pLock
1532f 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 ; pLock=pLock->p
15330 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73 73 Next){. ass
15331 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 ert( pLock->eLoc
15332 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 k==READ_LOCK ||
15333 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 pLock->pBtree==p
15334 20 29 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d );. pLock-
15335 3e 65 4c 6f 63 6b 20 3d 20 52 45 41 44 5f 4c 4f >eLock = READ_LO
15336 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a CK;. }. }.}.
15337 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
15338 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
15339 43 48 45 20 2a 2f 0a 0a 73 74 61 74 69 63 20 76 CHE */..static v
1533a 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67 65 28 oid releasePage(
1533b 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 3b MemPage *pPage);
1533c 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 /* Forward ref
1533d 65 72 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a erence */../*.**
1533e 2a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 *** This routine
1533f 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 65 20 is used inside
15340 6f 66 20 61 73 73 65 72 74 28 29 20 6f 6e 6c 79 of assert() only
15341 20 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 56 65 72 69 ****.**.** Veri
15342 66 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 fy that the curs
15343 6f 72 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 or holds the mut
15344 65 78 20 6f 6e 20 69 74 73 20 42 74 53 68 61 72 ex on its BtShar
15345 65 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c ed.*/.#ifdef SQL
15346 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 ITE_DEBUG.static
15347 20 69 6e 74 20 63 75 72 73 6f 72 48 6f 6c 64 73 int cursorHolds
15348 4d 75 74 65 78 28 42 74 43 75 72 73 6f 72 20 2a Mutex(BtCursor *
15349 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c p){. return sql
1534a 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1534b 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a p->pBt->mutex);.
1534c 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 }.#endif...#ifnd
1534d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
1534e 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e NCRBLOB./*.** In
1534f 76 61 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65 validate the ove
15350 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 rflow page-list
15351 63 61 63 68 65 20 66 6f 72 20 63 75 72 73 6f 72 cache for cursor
15352 20 70 43 75 72 2c 20 69 66 20 61 6e 79 2e 0a 2a pCur, if any..*
15353 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e /.static void in
15354 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 validateOverflow
15355 43 61 63 68 65 28 42 74 43 75 72 73 6f 72 20 2a Cache(BtCursor *
15356 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 pCur){. assert(
15357 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
15358 78 28 70 43 75 72 29 20 29 3b 0a 20 20 73 71 6c x(pCur) );. sql
15359 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e ite3_free(pCur->
1535a 61 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 aOverflow);. pC
1535b 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 ur->aOverflow =
1535c 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 0;.}../*.** Inva
1535d 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65 72 66 lidate the overf
1535e 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 low page-list ca
1535f 63 68 65 20 66 6f 72 20 61 6c 6c 20 63 75 72 73 che for all curs
15360 6f 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e ors opened.** on
15361 20 74 68 65 20 73 68 61 72 65 64 20 62 74 72 65 the shared btre
15362 65 20 73 74 72 75 63 74 75 72 65 20 70 42 74 2e e structure pBt.
15363 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
15364 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 invalidateAllOve
15365 72 66 6c 6f 77 43 61 63 68 65 28 42 74 53 68 61 rflowCache(BtSha
15366 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 red *pBt){. BtC
15367 75 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 ursor *p;. asse
15368 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
15369 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
1536a 78 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 x) );. for(p=pB
1536b 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 t->pCursor; p; p
1536c 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 =p->pNext){.
1536d 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c invalidateOverfl
1536e 6f 77 43 61 63 68 65 28 70 29 3b 0a 20 20 7d 0a owCache(p);. }.
1536f 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
15370 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
15371 20 62 65 66 6f 72 65 20 6d 6f 64 69 66 79 69 6e before modifyin
15372 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f g the contents o
15373 66 20 61 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 f a table.** to
15374 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 69 invalidate any i
15375 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 ncrblob cursors
15376 74 68 61 74 20 61 72 65 20 6f 70 65 6e 20 6f 6e that are open on
15377 20 74 68 65 0a 2a 2a 20 72 6f 77 20 6f 72 20 6f the.** row or o
15378 6e 65 20 6f 66 20 74 68 65 20 72 6f 77 73 20 62 ne of the rows b
15379 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e 0a 2a eing modified..*
1537a 2a 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 *.** If argument
1537b 20 69 73 43 6c 65 61 72 54 61 62 6c 65 20 69 73 isClearTable is
1537c 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 true, then the
1537d 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 entire contents
1537e 6f 66 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 of the.** table
1537f 69 73 20 61 62 6f 75 74 20 74 6f 20 62 65 20 64 is about to be d
15380 65 6c 65 74 65 64 2e 20 49 6e 20 74 68 69 73 20 eleted. In this
15381 63 61 73 65 20 69 6e 76 61 6c 69 64 61 74 65 20 case invalidate
15382 61 6c 6c 20 69 6e 63 72 62 6c 6f 62 0a 2a 2a 20 all incrblob.**
15383 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 cursors open on
15384 61 6e 79 20 72 6f 77 20 77 69 74 68 69 6e 20 74 any row within t
15385 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f he table with ro
15386 6f 74 2d 70 61 67 65 20 70 67 6e 6f 52 6f 6f 74 ot-page pgnoRoot
15387 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ..**.** Otherwis
15388 65 2c 20 69 66 20 61 72 67 75 6d 65 6e 74 20 69 e, if argument i
15389 73 43 6c 65 61 72 54 61 62 6c 65 20 69 73 20 66 sClearTable is f
1538a 61 6c 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 alse, then the r
1538b 6f 77 20 77 69 74 68 0a 2a 2a 20 72 6f 77 69 64 ow with.** rowid
1538c 20 69 52 6f 77 20 69 73 20 62 65 69 6e 67 20 72 iRow is being r
1538d 65 70 6c 61 63 65 64 20 6f 72 20 64 65 6c 65 74 eplaced or delet
1538e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ed. In this case
1538f 20 69 6e 76 61 6c 69 64 61 74 65 0a 2a 2a 20 6f invalidate.** o
15390 6e 6c 79 20 74 68 6f 73 65 20 69 6e 63 72 62 6c nly those incrbl
15391 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 ob cursors open
15392 6f 6e 20 74 68 61 74 20 73 70 65 63 69 66 69 63 on that specific
15393 20 72 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 row..*/.static
15394 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 49 void invalidateI
15395 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 0a ncrblobCursors(.
15396 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c Btree *pBtree,
15397 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
15398 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
15399 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 36 34 o check */. i64
1539a 20 69 52 6f 77 2c 20 20 20 20 20 20 20 20 20 20 iRow,
1539b 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 /* The rowi
1539c 64 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 d that might be
1539d 63 68 61 6e 67 69 6e 67 20 2a 2f 0a 20 20 69 6e changing */. in
1539e 74 20 69 73 43 6c 65 61 72 54 61 62 6c 65 20 20 t isClearTable
1539f 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
153a0 20 61 6c 6c 20 72 6f 77 73 20 61 72 65 20 62 65 all rows are be
153a1 69 6e 67 20 64 65 6c 65 74 65 64 20 2a 2f 0a 29 ing deleted */.)
153a2 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b {. BtCursor *p;
153a3 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
153a4 20 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a = pBtree->pBt;.
153a5 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
153a6 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 3BtreeHoldsMutex
153a7 28 70 42 74 72 65 65 29 20 29 3b 0a 20 20 66 6f (pBtree) );. fo
153a8 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 r(p=pBt->pCursor
153a9 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 ; p; p=p->pNext)
153aa 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 73 49 {. if( p->isI
153ab 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 26 26 ncrblobHandle &&
153ac 20 28 69 73 43 6c 65 61 72 54 61 62 6c 65 20 7c (isClearTable |
153ad 7c 20 70 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d | p->info.nKey==
153ae 69 52 6f 77 29 20 29 7b 0a 20 20 20 20 20 20 70 iRow) ){. p
153af 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f ->eState = CURSO
153b0 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 7d R_INVALID;. }
153b1 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a 20 20 . }.}..#else.
153b2 2f 2a 20 53 74 75 62 20 66 75 6e 63 74 69 6f 6e /* Stub function
153b3 73 20 77 68 65 6e 20 49 4e 43 52 42 4c 4f 42 20 s when INCRBLOB
153b4 69 73 20 6f 6d 69 74 74 65 64 20 2a 2f 0a 20 20 is omitted */.
153b5 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 61 #define invalida
153b6 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 teOverflowCache(
153b7 78 29 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76 x). #define inv
153b8 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c alidateAllOverfl
153b9 6f 77 43 61 63 68 65 28 78 29 0a 20 20 23 64 65 owCache(x). #de
153ba 66 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 49 fine invalidateI
153bb 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 78 ncrblobCursors(x
153bc 2c 79 2c 7a 29 0a 23 65 6e 64 69 66 20 2f 2a 20 ,y,z).#endif /*
153bd 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 SQLITE_OMIT_INCR
153be 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 BLOB */../*.** S
153bf 65 74 20 62 69 74 20 70 67 6e 6f 20 6f 66 20 74 et bit pgno of t
153c0 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 73 he BtShared.pHas
153c1 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e 20 Content bitvec.
153c2 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 0a This is called .
153c3 2a 2a 20 77 68 65 6e 20 61 20 70 61 67 65 20 74 ** when a page t
153c4 68 61 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63 hat previously c
153c5 6f 6e 74 61 69 6e 65 64 20 64 61 74 61 20 62 65 ontained data be
153c6 63 6f 6d 65 73 20 61 20 66 72 65 65 2d 6c 69 73 comes a free-lis
153c7 74 20 6c 65 61 66 20 0a 2a 2a 20 70 61 67 65 2e t leaf .** page.
153c8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 74 53 68 61 .**.** The BtSha
153c9 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 red.pHasContent
153ca 62 69 74 76 65 63 20 65 78 69 73 74 73 20 74 6f bitvec exists to
153cb 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 61 6e 20 work around an
153cc 6f 62 73 63 75 72 65 0a 2a 2a 20 62 75 67 20 63 obscure.** bug c
153cd 61 75 73 65 64 20 62 79 20 74 68 65 20 69 6e 74 aused by the int
153ce 65 72 61 63 74 69 6f 6e 20 6f 66 20 74 77 6f 20 eraction of two
153cf 75 73 65 66 75 6c 20 49 4f 20 6f 70 74 69 6d 69 useful IO optimi
153d0 7a 61 74 69 6f 6e 73 20 73 75 72 72 6f 75 6e 64 zations surround
153d1 69 6e 67 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74 ing.** free-list
153d2 20 6c 65 61 66 20 70 61 67 65 73 3a 0a 2a 2a 0a leaf pages:.**.
153d3 2a 2a 20 20 20 31 29 20 57 68 65 6e 20 61 6c 6c ** 1) When all
153d4 20 64 61 74 61 20 69 73 20 64 65 6c 65 74 65 64 data is deleted
153d5 20 66 72 6f 6d 20 61 20 70 61 67 65 20 61 6e 64 from a page and
153d6 20 74 68 65 20 70 61 67 65 20 62 65 63 6f 6d 65 the page become
153d7 73 0a 2a 2a 20 20 20 20 20 20 61 20 66 72 65 65 s.** a free
153d8 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c -list leaf page,
153d9 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
153da 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
153db 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 database.**
153dc 20 28 61 73 20 66 72 65 65 2d 6c 69 73 74 20 6c (as free-list l
153dd 65 61 66 20 70 61 67 65 73 20 63 6f 6e 74 61 69 eaf pages contai
153de 6e 20 6e 6f 20 6d 65 61 6e 69 6e 67 66 75 6c 20 n no meaningful
153df 64 61 74 61 29 2e 20 53 6f 6d 65 74 69 6d 65 73 data). Sometimes
153e0 0a 2a 2a 20 20 20 20 20 20 73 75 63 68 20 61 20 .** such a
153e1 70 61 67 65 20 69 73 20 6e 6f 74 20 65 76 65 6e page is not even
153e2 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 28 61 73 20 journalled (as
153e3 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6d it will not be m
153e4 6f 64 69 66 69 65 64 2c 0a 2a 2a 20 20 20 20 20 odified,.**
153e5 20 77 68 79 20 62 6f 74 68 65 72 20 6a 6f 75 72 why bother jour
153e6 6e 61 6c 6c 69 6e 67 20 69 74 3f 29 2e 0a 2a 2a nalling it?)..**
153e7 0a 2a 2a 20 20 20 32 29 20 57 68 65 6e 20 61 20 .** 2) When a
153e8 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 free-list leaf p
153e9 61 67 65 20 69 73 20 72 65 75 73 65 64 2c 20 69 age is reused, i
153ea 74 73 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f ts content is no
153eb 74 20 72 65 61 64 0a 2a 2a 20 20 20 20 20 20 66 t read.** f
153ec 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
153ed 20 6f 72 20 77 72 69 74 74 65 6e 20 74 6f 20 74 or written to t
153ee 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
153ef 28 77 68 79 20 73 68 6f 75 6c 64 20 69 74 0a 2a (why should it.*
153f0 2a 20 20 20 20 20 20 62 65 2c 20 69 66 20 69 74 * be, if it
153f1 20 69 73 20 6e 6f 74 20 61 74 20 61 6c 6c 20 6d is not at all m
153f2 65 61 6e 69 6e 67 66 75 6c 3f 29 2e 0a 2a 2a 0a eaningful?)..**.
153f3 2a 2a 20 42 79 20 74 68 65 6d 73 65 6c 76 65 73 ** By themselves
153f4 2c 20 74 68 65 73 65 20 6f 70 74 69 6d 69 7a 61 , these optimiza
153f5 74 69 6f 6e 73 20 77 6f 72 6b 20 66 69 6e 65 20 tions work fine
153f6 61 6e 64 20 70 72 6f 76 69 64 65 20 61 20 68 61 and provide a ha
153f7 6e 64 79 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e ndy.** performan
153f8 63 65 20 62 6f 6f 73 74 20 74 6f 20 62 75 6c 6b ce boost to bulk
153f9 20 64 65 6c 65 74 65 20 6f 72 20 69 6e 73 65 72 delete or inser
153fa 74 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 48 6f t operations. Ho
153fb 77 65 76 65 72 2c 20 69 66 0a 2a 2a 20 61 20 70 wever, if.** a p
153fc 61 67 65 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 age is moved to
153fd 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e the free-list an
153fe 64 20 74 68 65 6e 20 72 65 75 73 65 64 20 77 69 d then reused wi
153ff 74 68 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a thin the same.**
15400 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20 transaction, a
15401 70 72 6f 62 6c 65 6d 20 63 6f 6d 65 73 20 75 70 problem comes up
15402 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 69 73 . If the page is
15403 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 not journalled
15404 77 68 65 6e 0a 2a 2a 20 69 74 20 69 73 20 6d 6f when.** it is mo
15405 76 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 2d ved to the free-
15406 6c 69 73 74 20 61 6e 64 20 69 74 20 69 73 20 61 list and it is a
15407 6c 73 6f 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c lso not journall
15408 65 64 20 77 68 65 6e 20 69 74 0a 2a 2a 20 69 73 ed when it.** is
15409 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 extracted from
1540a 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e the free-list an
1540b 64 20 72 65 75 73 65 64 2c 20 74 68 65 6e 20 74 d reused, then t
1540c 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 he original data
1540d 0a 2a 2a 20 6d 61 79 20 62 65 20 6c 6f 73 74 2e .** may be lost.
1540e 20 49 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 In the event of
1540f 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 69 74 20 a rollback, it
15410 6d 61 79 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 may not be possi
15411 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 73 74 6f 72 ble.** to restor
15412 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 e the database t
15413 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 63 o its original c
15414 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2e 0a 2a 2a onfiguration..**
15415 0a 2a 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f 6e .** The solution
15416 20 69 73 20 74 68 65 20 42 74 53 68 61 72 65 64 is the BtShared
15417 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 .pHasContent bit
15418 76 65 63 2e 20 57 68 65 6e 65 76 65 72 20 61 20 vec. Whenever a
15419 70 61 67 65 20 69 73 20 0a 2a 2a 20 6d 6f 76 65 page is .** move
1541a 64 20 74 6f 20 62 65 63 6f 6d 65 20 61 20 66 72 d to become a fr
1541b 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 ee-list leaf pag
1541c 65 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e e, the correspon
1541d 64 69 6e 67 20 62 69 74 20 69 73 0a 2a 2a 20 73 ding bit is.** s
1541e 65 74 20 69 6e 20 74 68 65 20 62 69 74 76 65 63 et in the bitvec
1541f 2e 20 57 68 65 6e 65 76 65 72 20 61 20 6c 65 61 . Whenever a lea
15420 66 20 70 61 67 65 20 69 73 20 65 78 74 72 61 63 f page is extrac
15421 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66 72 65 ted from the fre
15422 65 2d 6c 69 73 74 2c 0a 2a 2a 20 6f 70 74 69 6d e-list,.** optim
15423 69 7a 61 74 69 6f 6e 20 32 20 61 62 6f 76 65 20 ization 2 above
15424 69 73 20 6f 6d 69 74 74 65 64 20 69 66 20 74 68 is omitted if th
15425 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
15426 62 69 74 20 69 73 20 61 6c 72 65 61 64 79 0a 2a bit is already.*
15427 2a 20 73 65 74 20 69 6e 20 42 74 53 68 61 72 65 * set in BtShare
15428 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 2e 20 54 d.pHasContent. T
15429 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
1542a 68 65 20 62 69 74 76 65 63 20 61 72 65 20 63 6c he bitvec are cl
1542b 65 61 72 65 64 0a 2a 2a 20 61 74 20 74 68 65 20 eared.** at the
1542c 65 6e 64 20 6f 66 20 65 76 65 72 79 20 74 72 61 end of every tra
1542d 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 nsaction..*/.sta
1542e 74 69 63 20 69 6e 74 20 62 74 72 65 65 53 65 74 tic int btreeSet
1542f 48 61 73 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 HasContent(BtSha
15430 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 red *pBt, Pgno p
15431 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d gno){. int rc =
15432 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
15433 28 20 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 ( !pBt->pHasCont
15434 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e ent ){. int n
15435 50 61 67 65 20 3d 20 31 30 30 3b 0a 20 20 20 20 Page = 100;.
15436 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
15437 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 count(pBt->pPage
15438 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 r, &nPage);.
15439 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 50 61 67 /* If sqlite3Pag
1543a 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20 66 61 erPagecount() fa
1543b 69 6c 73 20 74 68 65 72 65 20 69 73 20 6e 6f 20 ils there is no
1543c 68 61 72 6d 20 62 65 63 61 75 73 65 20 74 68 65 harm because the
1543d 0a 20 20 20 20 2a 2a 20 6e 50 61 67 65 20 76 61 . ** nPage va
1543e 72 69 61 62 6c 65 20 69 73 20 75 6e 63 68 61 6e riable is unchan
1543f 67 65 64 20 66 72 6f 6d 20 69 74 73 20 64 65 66 ged from its def
15440 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 31 30 ault value of 10
15441 30 20 2a 2f 0a 20 20 20 20 70 42 74 2d 3e 70 48 0 */. pBt->pH
15442 61 73 43 6f 6e 74 65 6e 74 20 3d 20 73 71 6c 69 asContent = sqli
15443 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 te3BitvecCreate(
15444 28 75 33 32 29 6e 50 61 67 65 29 3b 0a 20 20 20 (u32)nPage);.
15445 20 69 66 28 20 21 70 42 74 2d 3e 70 48 61 73 43 if( !pBt->pHasC
15446 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 ontent ){.
15447 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
15448 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 M;. }. }. i
15449 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1544a 20 26 26 20 70 67 6e 6f 3c 3d 73 71 6c 69 74 65 && pgno<=sqlite
1544b 33 42 69 74 76 65 63 53 69 7a 65 28 70 42 74 2d 3BitvecSize(pBt-
1544c 3e 70 48 61 73 43 6f 6e 74 65 6e 74 29 20 29 7b >pHasContent) ){
1544d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1544e 33 42 69 74 76 65 63 53 65 74 28 70 42 74 2d 3e 3BitvecSet(pBt->
1544f 70 48 61 73 43 6f 6e 74 65 6e 74 2c 20 70 67 6e pHasContent, pgn
15450 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e o);. }. return
15451 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 rc;.}../*.** Qu
15452 65 72 79 20 74 68 65 20 42 74 53 68 61 72 65 64 ery the BtShared
15453 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 76 65 63 .pHasContent vec
15454 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 tor..**.** This
15455 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
15456 65 64 20 77 68 65 6e 20 61 20 66 72 65 65 2d 6c ed when a free-l
15457 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 69 73 ist leaf page is
15458 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 removed from th
15459 65 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 66 e.** free-list f
1545a 6f 72 20 72 65 75 73 65 2e 20 49 74 20 72 65 74 or reuse. It ret
1545b 75 72 6e 73 20 66 61 6c 73 65 20 69 66 20 69 74 urns false if it
1545c 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 74 72 is safe to retr
1545d 69 65 76 65 20 74 68 65 0a 2a 2a 20 70 61 67 65 ieve the.** page
1545e 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 from the pager
1545f 6c 61 79 65 72 20 77 69 74 68 20 74 68 65 20 27 layer with the '
15460 6e 6f 2d 63 6f 6e 74 65 6e 74 27 20 66 6c 61 67 no-content' flag
15461 20 73 65 74 2e 20 54 72 75 65 20 6f 74 68 65 72 set. True other
15462 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 wise..*/.static
15463 69 6e 74 20 62 74 72 65 65 47 65 74 48 61 73 43 int btreeGetHasC
15464 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 20 ontent(BtShared
15465 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 *pBt, Pgno pgno)
15466 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 20 3d 20 {. Bitvec *p =
15467 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 pBt->pHasContent
15468 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20 26 26 ;. return (p &&
15469 20 28 70 67 6e 6f 3e 73 71 6c 69 74 65 33 42 69 (pgno>sqlite3Bi
1546a 74 76 65 63 53 69 7a 65 28 70 29 20 7c 7c 20 73 tvecSize(p) || s
1546b 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 qlite3BitvecTest
1546c 28 70 2c 20 70 67 6e 6f 29 29 29 3b 0a 7d 0a 0a (p, pgno)));.}..
1546d 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 28 64 65 73 /*.** Clear (des
1546e 74 72 6f 79 29 20 74 68 65 20 42 74 53 68 61 72 troy) the BtShar
1546f 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 ed.pHasContent b
15470 69 74 76 65 63 2e 20 54 68 69 73 20 73 68 6f 75 itvec. This shou
15471 6c 64 20 62 65 0a 2a 2a 20 69 6e 76 6f 6b 65 64 ld be.** invoked
15472 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 at the conclusi
15473 6f 6e 20 6f 66 20 65 61 63 68 20 77 72 69 74 65 on of each write
15474 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f -transaction..*/
15475 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 .static void btr
15476 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e eeClearHasConten
15477 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 t(BtShared *pBt)
15478 7b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 {. sqlite3Bitve
15479 63 44 65 73 74 72 6f 79 28 70 42 74 2d 3e 70 48 cDestroy(pBt->pH
1547a 61 73 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 70 42 asContent);. pB
1547b 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d t->pHasContent =
1547c 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 0;.}../*.** Sav
1547d 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 e the current cu
1547e 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69 6e rsor position in
1547f 20 74 68 65 20 76 61 72 69 61 62 6c 65 73 20 42 the variables B
15480 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a 2a tCursor.nKey .**
15481 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 70 4b and BtCursor.pK
15482 65 79 2e 20 54 68 65 20 63 75 72 73 6f 72 27 73 ey. The cursor's
15483 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74 6f state is set to
15484 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 CURSOR_REQUIRES
15485 45 45 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 EEK..**.** The c
15486 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72 aller must ensur
15487 65 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f e that the curso
15488 72 20 69 73 20 76 61 6c 69 64 20 28 68 61 73 20 r is valid (has
15489 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
1548a 41 4c 49 44 29 0a 2a 2a 20 70 72 69 6f 72 20 74 ALID).** prior t
1548b 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 o calling this r
1548c 6f 75 74 69 6e 65 2e 20 20 0a 2a 2f 0a 73 74 61 outine. .*/.sta
1548d 74 69 63 20 69 6e 74 20 73 61 76 65 43 75 72 73 tic int saveCurs
1548e 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 orPosition(BtCur
1548f 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e sor *pCur){. in
15490 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 t rc;.. assert(
15491 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 CURSOR_VALID==p
15492 43 75 72 2d 3e 65 53 74 61 74 65 20 29 3b 0a 20 Cur->eState );.
15493 20 61 73 73 65 72 74 28 20 30 3d 3d 70 43 75 72 assert( 0==pCur
15494 2d 3e 70 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 ->pKey );. asse
15495 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
15496 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 0a 20 utex(pCur) );..
15497 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
15498 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20 eeKeySize(pCur,
15499 26 70 43 75 72 2d 3e 6e 4b 65 79 29 3b 0a 20 20 &pCur->nKey);.
1549a 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
1549b 54 45 5f 4f 4b 20 29 3b 20 20 2f 2a 20 4b 65 79 TE_OK ); /* Key
1549c 53 69 7a 65 28 29 20 63 61 6e 6e 6f 74 20 66 61 Size() cannot fa
1549d 69 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 il */.. /* If t
1549e 68 69 73 20 69 73 20 61 6e 20 69 6e 74 4b 65 79 his is an intKey
1549f 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 74 68 65 table, then the
154a0 20 61 62 6f 76 65 20 63 61 6c 6c 20 74 6f 20 42 above call to B
154a1 74 72 65 65 4b 65 79 53 69 7a 65 28 29 0a 20 20 treeKeySize().
154a2 2a 2a 20 73 74 6f 72 65 73 20 74 68 65 20 69 6e ** stores the in
154a3 74 65 67 65 72 20 6b 65 79 20 69 6e 20 70 43 75 teger key in pCu
154a4 72 2d 3e 6e 4b 65 79 2e 20 49 6e 20 74 68 69 73 r->nKey. In this
154a5 20 63 61 73 65 20 74 68 69 73 20 76 61 6c 75 65 case this value
154a6 20 69 73 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 is. ** all tha
154a7 74 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 4f t is required. O
154a8 74 68 65 72 77 69 73 65 2c 20 69 66 20 70 43 75 therwise, if pCu
154a9 72 20 69 73 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e r is not open on
154aa 20 61 6e 20 69 6e 74 4b 65 79 0a 20 20 2a 2a 20 an intKey. **
154ab 74 61 62 6c 65 2c 20 74 68 65 6e 20 6d 61 6c 6c table, then mall
154ac 6f 63 20 73 70 61 63 65 20 66 6f 72 20 61 6e 64 oc space for and
154ad 20 73 74 6f 72 65 20 74 68 65 20 70 43 75 72 2d store the pCur-
154ae 3e 6e 4b 65 79 20 62 79 74 65 73 20 6f 66 20 6b >nKey bytes of k
154af 65 79 20 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20 ey . ** data..
154b0 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 70 43 75 */. if( 0==pCu
154b1 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e r->apPage[0]->in
154b2 74 4b 65 79 20 29 7b 0a 20 20 20 20 76 6f 69 64 tKey ){. void
154b3 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 *pKey = sqlite3
154b4 4d 61 6c 6c 6f 63 28 20 28 69 6e 74 29 70 43 75 Malloc( (int)pCu
154b5 72 2d 3e 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69 r->nKey );. i
154b6 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 f( pKey ){.
154b7 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
154b8 65 65 4b 65 79 28 70 43 75 72 2c 20 30 2c 20 28 eeKey(pCur, 0, (
154b9 69 6e 74 29 70 43 75 72 2d 3e 6e 4b 65 79 2c 20 int)pCur->nKey,
154ba 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 69 66 28 pKey);. if(
154bb 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
154bc 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e {. pCur->
154bd 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 pKey = pKey;.
154be 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
154bf 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
154c0 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Key);. }.
154c1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
154c2 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
154c3 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 ;. }. }. as
154c4 73 65 72 74 28 20 21 70 43 75 72 2d 3e 61 70 50 sert( !pCur->apP
154c5 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c age[0]->intKey |
154c6 7c 20 21 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b | !pCur->pKey );
154c7 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 .. if( rc==SQLI
154c8 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 TE_OK ){. int
154c9 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b i;. for(i=0;
154ca 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b i<=pCur->iPage;
154cb 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c i++){. rel
154cc 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 easePage(pCur->a
154cd 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20 pPage[i]);.
154ce 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d pCur->apPage[i]
154cf 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
154d0 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31 pCur->iPage = -1
154d1 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 ;. pCur->eSta
154d2 74 65 20 3d 20 43 55 52 53 4f 52 5f 52 45 51 55 te = CURSOR_REQU
154d3 49 52 45 53 45 45 4b 3b 0a 20 20 7d 0a 0a 20 20 IRESEEK;. }..
154d4 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c invalidateOverfl
154d5 6f 77 43 61 63 68 65 28 70 43 75 72 29 3b 0a 20 owCache(pCur);.
154d6 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
154d7 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 70 6f *.** Save the po
154d8 73 69 74 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 63 sitions of all c
154d9 75 72 73 6f 72 73 20 28 65 78 63 65 70 74 20 70 ursors (except p
154da 45 78 63 65 70 74 29 20 74 68 61 74 20 61 72 65 Except) that are
154db 20 6f 70 65 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 open on.** the
154dc 74 61 62 6c 65 20 20 77 69 74 68 20 72 6f 6f 74 table with root
154dd 2d 70 61 67 65 20 69 52 6f 6f 74 2e 20 55 73 75 -page iRoot. Usu
154de 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61 ally, this is ca
154df 6c 6c 65 64 20 6a 75 73 74 20 62 65 66 6f 72 65 lled just before
154e0 20 63 75 72 73 6f 72 0a 2a 2a 20 70 45 78 63 65 cursor.** pExce
154e1 70 74 20 69 73 20 75 73 65 64 20 74 6f 20 6d 6f pt is used to mo
154e2 64 69 66 79 20 74 68 65 20 74 61 62 6c 65 20 28 dify the table (
154e3 42 74 72 65 65 44 65 6c 65 74 65 28 29 20 6f 72 BtreeDelete() or
154e4 20 42 74 72 65 65 49 6e 73 65 72 74 28 29 29 2e BtreeInsert()).
154e5 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
154e6 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 aveAllCursors(Bt
154e7 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e Shared *pBt, Pgn
154e8 6f 20 69 52 6f 6f 74 2c 20 42 74 43 75 72 73 6f o iRoot, BtCurso
154e9 72 20 2a 70 45 78 63 65 70 74 29 7b 0a 20 20 42 r *pExcept){. B
154ea 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73 tCursor *p;. as
154eb 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
154ec 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
154ed 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
154ee 28 20 70 45 78 63 65 70 74 3d 3d 30 20 7c 7c 20 ( pExcept==0 ||
154ef 70 45 78 63 65 70 74 2d 3e 70 42 74 3d 3d 70 42 pExcept->pBt==pB
154f0 74 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 t );. for(p=pBt
154f1 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d ->pCursor; p; p=
154f2 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 p->pNext){. i
154f3 66 28 20 70 21 3d 70 45 78 63 65 70 74 20 26 26 f( p!=pExcept &&
154f4 20 28 30 3d 3d 69 52 6f 6f 74 20 7c 7c 20 70 2d (0==iRoot || p-
154f5 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 >pgnoRoot==iRoot
154f6 29 20 26 26 20 0a 20 20 20 20 20 20 20 20 70 2d ) && . p-
154f7 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
154f8 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 69 VALID ){. i
154f9 6e 74 20 72 63 20 3d 20 73 61 76 65 43 75 72 73 nt rc = saveCurs
154fa 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 3b 0a 20 orPosition(p);.
154fb 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f if( SQLITE_
154fc 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 20 20 OK!=rc ){.
154fd 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
154fe 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
154ff 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
15500 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 K;.}../*.** Clea
15501 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 r the current cu
15502 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a rsor position..*
15503 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
15504 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
15505 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74 eeClearCursor(Bt
15506 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
15507 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
15508 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
15509 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 );. sqlite3_fre
1550a 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 e(pCur->pKey);.
1550b 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b pCur->pKey = 0;
1550c 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 . pCur->eState
1550d 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 = CURSOR_INVALID
1550e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 ;.}../*.** In th
1550f 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 42 74 is version of Bt
15510 72 65 65 4d 6f 76 65 74 6f 2c 20 70 4b 65 79 20 reeMoveto, pKey
15511 69 73 20 61 20 70 61 63 6b 65 64 20 69 6e 64 65 is a packed inde
15512 78 20 72 65 63 6f 72 64 0a 2a 2a 20 73 75 63 68 x record.** such
15513 20 61 73 20 69 73 20 67 65 6e 65 72 61 74 65 64 as is generated
15514 20 62 79 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52 by the OP_MakeR
15515 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 20 20 55 ecord opcode. U
15516 6e 70 61 63 6b 20 74 68 65 0a 2a 2a 20 72 65 63 npack the.** rec
15517 6f 72 64 20 61 6e 64 20 74 68 65 6e 20 63 61 6c ord and then cal
15518 6c 20 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 l BtreeMovetoUnp
15519 61 63 6b 65 64 28 29 20 74 6f 20 64 6f 20 74 68 acked() to do th
1551a 65 20 77 6f 72 6b 2e 0a 2a 2f 0a 73 74 61 74 69 e work..*/.stati
1551b 63 20 69 6e 74 20 62 74 72 65 65 4d 6f 76 65 74 c int btreeMovet
1551c 6f 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 o(. BtCursor *p
1551d 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 75 72 73 Cur, /* Curs
1551e 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 62 or open on the b
1551f 74 72 65 65 20 74 6f 20 62 65 20 73 65 61 72 63 tree to be searc
15520 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 hed */. const v
15521 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 oid *pKey, /*
15522 50 61 63 6b 65 64 20 6b 65 79 20 69 66 20 74 68 Packed key if th
15523 65 20 62 74 72 65 65 20 69 73 20 61 6e 20 69 6e e btree is an in
15524 64 65 78 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 dex */. i64 nKe
15525 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 y, /*
15526 49 6e 74 65 67 65 72 20 6b 65 79 20 66 6f 72 20 Integer key for
15527 74 61 62 6c 65 73 2e 20 20 53 69 7a 65 20 6f 66 tables. Size of
15528 20 70 4b 65 79 20 66 6f 72 20 69 6e 64 69 63 65 pKey for indice
15529 73 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 2c s */. int bias,
1552a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69 /* Bi
1552b 61 73 20 73 65 61 72 63 68 20 74 6f 20 74 68 65 as search to the
1552c 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69 high end */. i
1552d 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 20 20 nt *pRes
1552e 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65 61 72 /* Write sear
1552f 63 68 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 ch results here
15530 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 */.){. int rc;
15531 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15532 20 20 20 2f 2a 20 53 74 61 74 75 73 20 63 6f 64 /* Status cod
15533 65 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 e */. UnpackedR
15534 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 20 ecord *pIdxKey;
15535 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 69 6e /* Unpacked in
15536 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 63 68 61 dex key */. cha
15537 72 20 61 53 70 61 63 65 5b 31 35 30 5d 3b 20 20 r aSpace[150];
15538 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 /* Temp
15539 73 70 61 63 65 20 66 6f 72 20 70 49 64 78 4b 65 space for pIdxKe
1553a 79 20 2d 20 74 6f 20 61 76 6f 69 64 20 61 20 6d y - to avoid a m
1553b 61 6c 6c 6f 63 20 2a 2f 0a 0a 20 20 69 66 28 20 alloc */.. if(
1553c 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 73 73 65 pKey ){. asse
1553d 72 74 28 20 6e 4b 65 79 3d 3d 28 69 36 34 29 28 rt( nKey==(i64)(
1553e 69 6e 74 29 6e 4b 65 79 20 29 3b 0a 20 20 20 20 int)nKey );.
1553f 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69 74 65 pIdxKey = sqlite
15540 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 3VdbeRecordUnpac
15541 6b 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f k(pCur->pKeyInfo
15542 2c 20 28 69 6e 74 29 6e 4b 65 79 2c 20 70 4b 65 , (int)nKey, pKe
15543 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 y,.
15544 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15545 20 20 20 20 20 20 20 20 20 61 53 70 61 63 65 2c aSpace,
15546 20 73 69 7a 65 6f 66 28 61 53 70 61 63 65 29 29 sizeof(aSpace))
15547 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 4b 65 ;. if( pIdxKe
15548 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 y==0 ) return SQ
15549 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 LITE_NOMEM;. }e
1554a 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b 65 79 lse{. pIdxKey
1554b 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63 20 3d = 0;. }. rc =
1554c 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
1554d 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 75 72 etoUnpacked(pCur
1554e 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65 79 2c , pIdxKey, nKey,
1554f 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a 20 20 bias, pRes);.
15550 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 if( pKey ){.
15551 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 sqlite3VdbeDelet
15552 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 eUnpackedRecord(
15553 70 49 64 78 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 pIdxKey);. }.
15554 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
15555 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20 .** Restore the
15556 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 70 6f cursor to the po
15557 73 69 74 69 6f 6e 20 69 74 20 77 61 73 20 69 6e sition it was in
15558 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 20 74 6f (or as close to
15559 20 61 73 20 70 6f 73 73 69 62 6c 65 29 0a 2a 2a as possible).**
1555a 20 77 68 65 6e 20 73 61 76 65 43 75 72 73 6f 72 when saveCursor
1555b 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 73 20 63 Position() was c
1555c 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 68 61 74 alled. Note that
1555d 20 74 68 69 73 20 63 61 6c 6c 20 64 65 6c 65 74 this call delet
1555e 65 73 20 74 68 65 20 0a 2a 2a 20 73 61 76 65 64 es the .** saved
1555f 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f 20 73 position info s
15560 74 6f 72 65 64 20 62 79 20 73 61 76 65 43 75 72 tored by saveCur
15561 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2c 20 73 sorPosition(), s
15562 6f 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a o there can be.*
15563 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 65 66 * at most one ef
15564 66 65 63 74 69 76 65 20 72 65 73 74 6f 72 65 43 fective restoreC
15565 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 ursorPosition()
15566 63 61 6c 6c 20 61 66 74 65 72 20 65 61 63 68 20 call after each
15567 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f 72 50 6f .** saveCursorPo
15568 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 73 74 61 sition()..*/.sta
15569 74 69 63 20 69 6e 74 20 62 74 72 65 65 52 65 73 tic int btreeRes
1556a 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 toreCursorPositi
1556b 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 on(BtCursor *pCu
1556c 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 r){. int rc;.
1556d 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
1556e 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
1556f 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
15570 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 ->eState>=CURSOR
15571 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a _REQUIRESEEK );.
15572 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 if( pCur->eSta
15573 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 te==CURSOR_FAULT
15574 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 ){. return p
15575 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a 20 Cur->skipNext;.
15576 20 7d 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 }. pCur->eStat
15577 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c e = CURSOR_INVAL
15578 49 44 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 ID;. rc = btree
15579 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70 43 75 Moveto(pCur, pCu
1557a 72 2d 3e 70 4b 65 79 2c 20 70 43 75 72 2d 3e 6e r->pKey, pCur->n
1557b 4b 65 79 2c 20 30 2c 20 26 70 43 75 72 2d 3e 73 Key, 0, &pCur->s
1557c 6b 69 70 4e 65 78 74 29 3b 0a 20 20 69 66 28 20 kipNext);. if(
1557d 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1557e 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
1557f 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 e(pCur->pKey);.
15580 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 pCur->pKey =
15581 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 0;. assert( p
15582 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
15583 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 SOR_VALID || pCu
15584 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
15585 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 7d R_INVALID );. }
15586 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
15587 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f 72 65 .#define restore
15588 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 CursorPosition(p
15589 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61 74 65 ) \. (p->eState
1558a 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 >=CURSOR_REQUIRE
1558b 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20 20 20 SEEK ? \.
1558c 20 20 62 74 72 65 65 52 65 73 74 6f 72 65 43 75 btreeRestoreCu
1558d 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 20 rsorPosition(p)
1558e 3a 20 5c 0a 20 20 20 20 20 20 20 20 20 53 51 4c : \. SQL
1558f 49 54 45 5f 4f 4b 29 0a 0a 2f 2a 0a 2a 2a 20 44 ITE_OK)../*.** D
15590 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 etermine whether
15591 20 6f 72 20 6e 6f 74 20 61 20 63 75 72 73 6f 72 or not a cursor
15592 20 68 61 73 20 6d 6f 76 65 64 20 66 72 6f 6d 20 has moved from
15593 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 0a the position it.
15594 2a 2a 20 77 61 73 20 6c 61 73 74 20 70 6c 61 63 ** was last plac
15595 65 64 20 61 74 2e 20 20 43 75 72 73 6f 72 73 20 ed at. Cursors
15596 63 61 6e 20 6d 6f 76 65 20 77 68 65 6e 20 74 68 can move when th
15597 65 20 72 6f 77 20 74 68 65 79 20 61 72 65 20 70 e row they are p
15598 6f 69 6e 74 69 6e 67 0a 2a 2a 20 61 74 20 69 73 ointing.** at is
15599 20 64 65 6c 65 74 65 64 20 6f 75 74 20 66 72 6f deleted out fro
1559a 6d 20 75 6e 64 65 72 20 74 68 65 6d 2e 0a 2a 2a m under them..**
1559b 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1559c 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f returns an erro
1559d 72 20 63 6f 64 65 20 69 66 20 73 6f 6d 65 74 68 r code if someth
1559e 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 20 ing goes wrong.
1559f 20 54 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 The.** integer
155a0 2a 70 48 61 73 4d 6f 76 65 64 20 69 73 20 73 65 *pHasMoved is se
155a1 74 20 74 6f 20 6f 6e 65 20 69 66 20 74 68 65 20 t to one if the
155a2 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 76 65 64 cursor has moved
155a3 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0a 2a and 0 if not..*
155a4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
155a5 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
155a6 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 eCursorHasMoved(
155a7 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
155a8 69 6e 74 20 2a 70 48 61 73 4d 6f 76 65 64 29 7b int *pHasMoved){
155a9 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 . int rc;.. rc
155aa 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 = restoreCursor
155ab 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a Position(pCur);.
155ac 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
155ad 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31 3b 0a *pHasMoved = 1;.
155ae 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
155af 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 }. if( pCur->e
155b0 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 State!=CURSOR_VA
155b1 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 73 6b 69 LID || pCur->ski
155b2 70 4e 65 78 74 21 3d 30 20 29 7b 0a 20 20 20 20 pNext!=0 ){.
155b3 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31 3b 0a *pHasMoved = 1;.
155b4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 48 }else{. *pH
155b5 61 73 4d 6f 76 65 64 20 3d 20 30 3b 0a 20 20 7d asMoved = 0;. }
155b6 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
155b7 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 _OK;.}..#ifndef
155b8 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
155b9 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 47 69 76 VACUUM./*.** Giv
155ba 65 6e 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 en a page number
155bb 20 6f 66 20 61 20 72 65 67 75 6c 61 72 20 64 61 of a regular da
155bc 74 61 62 61 73 65 20 70 61 67 65 2c 20 72 65 74 tabase page, ret
155bd 75 72 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 urn the page.**
155be 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 70 number for the p
155bf 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 20 ointer-map page
155c0 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
155c1 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 0a e entry for the.
155c2 2a 2a 20 69 6e 70 75 74 20 70 61 67 65 20 6e 75 ** input page nu
155c3 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 mber..*/.static
155c4 50 67 6e 6f 20 70 74 72 6d 61 70 50 61 67 65 6e Pgno ptrmapPagen
155c5 6f 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c o(BtShared *pBt,
155c6 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 Pgno pgno){. i
155c7 6e 74 20 6e 50 61 67 65 73 50 65 72 4d 61 70 50 nt nPagesPerMapP
155c8 61 67 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72 age;. Pgno iPtr
155c9 4d 61 70 2c 20 72 65 74 3b 0a 20 20 61 73 73 65 Map, ret;. asse
155ca 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
155cb 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
155cc 78 29 20 29 3b 0a 20 20 6e 50 61 67 65 73 50 65 x) );. nPagesPe
155cd 72 4d 61 70 50 61 67 65 20 3d 20 28 70 42 74 2d rMapPage = (pBt-
155ce 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 29 2b 31 >usableSize/5)+1
155cf 3b 0a 20 20 69 50 74 72 4d 61 70 20 3d 20 28 70 ;. iPtrMap = (p
155d0 67 6e 6f 2d 32 29 2f 6e 50 61 67 65 73 50 65 72 gno-2)/nPagesPer
155d1 4d 61 70 50 61 67 65 3b 0a 20 20 72 65 74 20 3d MapPage;. ret =
155d2 20 28 69 50 74 72 4d 61 70 2a 6e 50 61 67 65 73 (iPtrMap*nPages
155d3 50 65 72 4d 61 70 50 61 67 65 29 20 2b 20 32 3b PerMapPage) + 2;
155d4 20 0a 20 20 69 66 28 20 72 65 74 3d 3d 50 45 4e . if( ret==PEN
155d5 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
155d6 42 74 29 20 29 7b 0a 20 20 20 20 72 65 74 2b 2b Bt) ){. ret++
155d7 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
155d8 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 et;.}../*.** Wri
155d9 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f te an entry into
155da 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
155db 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
155dc 74 69 6e 65 20 75 70 64 61 74 65 73 20 74 68 65 tine updates the
155dd 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 pointer map ent
155de 72 79 20 66 6f 72 20 70 61 67 65 20 6e 75 6d 62 ry for page numb
155df 65 72 20 27 6b 65 79 27 0a 2a 2a 20 73 6f 20 74 er 'key'.** so t
155e0 68 61 74 20 69 74 20 6d 61 70 73 20 74 6f 20 74 hat it maps to t
155e1 79 70 65 20 27 65 54 79 70 65 27 20 61 6e 64 20 ype 'eType' and
155e2 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 parent page numb
155e3 65 72 20 27 70 67 6e 6f 27 2e 0a 2a 2a 0a 2a 2a er 'pgno'..**.**
155e4 20 49 66 20 2a 70 52 43 20 69 73 20 69 6e 69 74 If *pRC is init
155e5 69 61 6c 6c 79 20 6e 6f 6e 2d 7a 65 72 6f 20 28 ially non-zero (
155e6 6e 6f 6e 2d 53 51 4c 49 54 45 5f 4f 4b 29 20 74 non-SQLITE_OK) t
155e7 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
155e8 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 20 is.** a no-op.
155e9 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 If an error occ
155ea 75 72 73 2c 20 74 68 65 20 61 70 70 72 6f 70 72 urs, the appropr
155eb 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 iate error code
155ec 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e is written.** in
155ed 74 6f 20 2a 70 52 43 2e 0a 2a 2f 0a 73 74 61 74 to *pRC..*/.stat
155ee 69 63 20 76 6f 69 64 20 70 74 72 6d 61 70 50 75 ic void ptrmapPu
155ef 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c t(BtShared *pBt,
155f0 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 65 54 Pgno key, u8 eT
155f1 79 70 65 2c 20 50 67 6e 6f 20 70 61 72 65 6e 74 ype, Pgno parent
155f2 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 44 , int *pRC){. D
155f3 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20 bPage *pDbPage;
155f4 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 /* The pointer
155f5 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 map page */. u8
155f6 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 *pPtrmap;
155f7 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d /* The pointer m
155f8 61 70 20 64 61 74 61 20 2a 2f 0a 20 20 50 67 6e ap data */. Pgn
155f9 6f 20 69 50 74 72 6d 61 70 3b 20 20 20 20 20 2f o iPtrmap; /
155fa 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 * The pointer ma
155fb 70 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f p page number */
155fc 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20 . int offset;
155fd 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 /* Offset i
155fe 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 n pointer map pa
155ff 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 ge */. int rc;
15600 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
15601 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 urn code from su
15602 62 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 0a 20 bfunctions */..
15603 20 69 66 28 20 2a 70 52 43 20 29 20 72 65 74 75 if( *pRC ) retu
15604 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 rn;.. assert( s
15605 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
15606 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
15607 0a 20 20 2f 2a 20 54 68 65 20 6d 61 73 74 65 72 . /* The master
15608 2d 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 6e 75 -journal page nu
15609 6d 62 65 72 20 6d 75 73 74 20 6e 65 76 65 72 20 mber must never
1560a 62 65 20 75 73 65 64 20 61 73 20 61 20 70 6f 69 be used as a poi
1560b 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f nter map page */
1560c 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d 50 54 . assert( 0==PT
1560d 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c RMAP_ISPAGE(pBt,
1560e 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 PENDING_BYTE_PA
1560f 47 45 28 70 42 74 29 29 20 29 3b 0a 0a 20 20 61 GE(pBt)) );.. a
15610 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f ssert( pBt->auto
15611 56 61 63 75 75 6d 20 29 3b 0a 20 20 69 66 28 20 Vacuum );. if(
15612 6b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 key==0 ){. *p
15613 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 RC = SQLITE_CORR
15614 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 72 65 UPT_BKPT;. re
15615 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 50 74 72 turn;. }. iPtr
15616 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 47 map = PTRMAP_PAG
15617 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20 ENO(pBt, key);.
15618 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
15619 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 erGet(pBt->pPage
1561a 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44 62 r, iPtrmap, &pDb
1561b 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 Page);. if( rc!
1561c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1561d 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 *pRC = rc;.
1561e 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6f return;. }. o
1561f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50 ffset = PTRMAP_P
15620 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d 61 70 TROFFSET(iPtrmap
15621 2c 20 6b 65 79 29 3b 0a 20 20 69 66 28 20 6f 66 , key);. if( of
15622 66 73 65 74 3c 30 20 29 7b 0a 20 20 20 20 2a 70 fset<0 ){. *p
15623 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 RC = SQLITE_CORR
15624 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 67 6f UPT_BKPT;. go
15625 74 6f 20 70 74 72 6d 61 70 5f 65 78 69 74 3b 0a to ptrmap_exit;.
15626 20 20 7d 0a 20 20 70 50 74 72 6d 61 70 20 3d 20 }. pPtrmap =
15627 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61 67 (u8 *)sqlite3Pag
15628 65 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 erGetData(pDbPag
15629 65 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 65 e);.. if( eType
1562a 21 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 !=pPtrmap[offset
1562b 5d 20 7c 7c 20 67 65 74 34 62 79 74 65 28 26 70 ] || get4byte(&p
1562c 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d Ptrmap[offset+1]
1562d 29 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20 20 20 )!=parent ){.
1562e 20 54 52 41 43 45 28 28 22 50 54 52 4d 41 50 5f TRACE(("PTRMAP_
1562f 55 50 44 41 54 45 3a 20 25 64 2d 3e 28 25 64 2c UPDATE: %d->(%d,
15630 25 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65 54 79 %d)\n", key, eTy
15631 70 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a 20 20 pe, parent));.
15632 20 20 2a 70 52 43 3d 20 72 63 20 3d 20 73 71 6c *pRC= rc = sql
15633 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
15634 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 DbPage);. if(
15635 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
15636 7b 0a 20 20 20 20 20 20 70 50 74 72 6d 61 70 5b {. pPtrmap[
15637 6f 66 66 73 65 74 5d 20 3d 20 65 54 79 70 65 3b offset] = eType;
15638 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 . put4byte(
15639 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b &pPtrmap[offset+
1563a 31 5d 2c 20 70 61 72 65 6e 74 29 3b 0a 20 20 20 1], parent);.
1563b 20 7d 0a 20 20 7d 0a 0a 70 74 72 6d 61 70 5f 65 }. }..ptrmap_e
1563c 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 50 61 xit:. sqlite3Pa
1563d 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 gerUnref(pDbPage
1563e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 );.}../*.** Read
1563f 20 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 an entry from t
15640 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a he pointer map..
15641 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
15642 6e 65 20 72 65 74 72 69 65 76 65 73 20 74 68 65 ne retrieves the
15643 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 pointer map ent
15644 72 79 20 66 6f 72 20 70 61 67 65 20 27 6b 65 79 ry for page 'key
15645 27 2c 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 ', writing.** th
15646 65 20 74 79 70 65 20 61 6e 64 20 70 61 72 65 6e e type and paren
15647 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 6f t page number to
15648 20 2a 70 45 54 79 70 65 20 61 6e 64 20 2a 70 50 *pEType and *pP
15649 67 6e 6f 20 72 65 73 70 65 63 74 69 76 65 6c 79 gno respectively
1564a 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 63 6f ..** An error co
1564b 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 de is returned i
1564c 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 f something goes
1564d 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72 77 69 73 wrong, otherwis
1564e 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a e SQLITE_OK..*/.
1564f 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d 61 static int ptrma
15650 70 47 65 74 28 42 74 53 68 61 72 65 64 20 2a 70 pGet(BtShared *p
15651 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 Bt, Pgno key, u8
15652 20 2a 70 45 54 79 70 65 2c 20 50 67 6e 6f 20 2a *pEType, Pgno *
15653 70 50 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67 65 pPgno){. DbPage
15654 20 2a 70 44 62 50 61 67 65 3b 20 20 20 2f 2a 20 *pDbPage; /*
15655 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 The pointer map
15656 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 50 page */. int iP
15657 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20 trmap; /*
15658 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 Pointer map page
15659 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a index */. u8 *
1565a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f pPtrmap; /
1565b 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 * Pointer map pa
1565c 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 ge data */. int
1565d 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 offset;
1565e 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 65 6e 74 /* Offset of ent
1565f 72 79 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 ry in pointer ma
15660 70 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a p */. int rc;..
15661 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
15662 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
15663 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 69 ->mutex) );.. i
15664 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f Ptrmap = PTRMAP_
15665 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 PAGENO(pBt, key)
15666 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
15667 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 PagerGet(pBt->pP
15668 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 ager, iPtrmap, &
15669 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 pDbPage);. if(
1566a 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 rc!=0 ){. ret
1566b 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 urn rc;. }. pP
1566c 74 72 6d 61 70 20 3d 20 28 75 38 20 2a 29 73 71 trmap = (u8 *)sq
1566d 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 lite3PagerGetDat
1566e 61 28 70 44 62 50 61 67 65 29 3b 0a 0a 20 20 6f a(pDbPage);.. o
1566f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50 ffset = PTRMAP_P
15670 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d 61 70 TROFFSET(iPtrmap
15671 2c 20 6b 65 79 29 3b 0a 20 20 61 73 73 65 72 74 , key);. assert
15672 28 20 70 45 54 79 70 65 21 3d 30 20 29 3b 0a 20 ( pEType!=0 );.
15673 20 2a 70 45 54 79 70 65 20 3d 20 70 50 74 72 6d *pEType = pPtrm
15674 61 70 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 69 66 ap[offset];. if
15675 28 20 70 50 67 6e 6f 20 29 20 2a 70 50 67 6e 6f ( pPgno ) *pPgno
15676 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 74 = get4byte(&pPt
15677 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 3b rmap[offset+1]);
15678 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 .. sqlite3Pager
15679 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a Unref(pDbPage);.
1567a 20 20 69 66 28 20 2a 70 45 54 79 70 65 3c 31 20 if( *pEType<1
1567b 7c 7c 20 2a 70 45 54 79 70 65 3e 35 20 29 20 72 || *pEType>5 ) r
1567c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
1567d 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 72 65 74 RUPT_BKPT;. ret
1567e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1567f 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 64 65 ..#else /* if de
15680 66 69 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4d 49 fined SQLITE_OMI
15681 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a T_AUTOVACUUM */.
15682 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70 #define ptrmap
15683 50 75 74 28 77 2c 78 2c 79 2c 7a 2c 72 63 29 0a Put(w,x,y,z,rc).
15684 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70 #define ptrmap
15685 47 65 74 28 77 2c 78 2c 79 2c 7a 29 20 53 51 4c Get(w,x,y,z) SQL
15686 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 ITE_OK. #define
15687 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 ptrmapPutOvflPt
15688 72 28 78 2c 20 79 2c 20 72 63 29 0a 23 65 6e 64 r(x, y, rc).#end
15689 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 if../*.** Given
1568a 61 20 62 74 72 65 65 20 70 61 67 65 20 61 6e 64 a btree page and
1568b 20 61 20 63 65 6c 6c 20 69 6e 64 65 78 20 28 30 a cell index (0
1568c 20 6d 65 61 6e 73 20 74 68 65 20 66 69 72 73 74 means the first
1568d 20 63 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 68 65 20 cell on.** the
1568e 70 61 67 65 2c 20 31 20 6d 65 61 6e 73 20 74 68 page, 1 means th
1568f 65 20 73 65 63 6f 6e 64 20 63 65 6c 6c 2c 20 61 e second cell, a
15690 6e 64 20 73 6f 20 66 6f 72 74 68 29 20 72 65 74 nd so forth) ret
15691 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a urn a pointer.**
15692 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e to the cell con
15693 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tent..**.** This
15694 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20 6f routine works o
15695 6e 6c 79 20 66 6f 72 20 70 61 67 65 73 20 74 68 nly for pages th
15696 61 74 20 64 6f 20 6e 6f 74 20 63 6f 6e 74 61 69 at do not contai
15697 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 n overflow cells
15698 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 69 6e ..*/.#define fin
15699 64 43 65 6c 6c 28 50 2c 49 29 20 5c 0a 20 20 28 dCell(P,I) \. (
1569a 28 50 29 2d 3e 61 44 61 74 61 20 2b 20 28 28 50 (P)->aData + ((P
1569b 29 2d 3e 6d 61 73 6b 50 61 67 65 20 26 20 67 65 )->maskPage & ge
1569c 74 32 62 79 74 65 28 26 28 50 29 2d 3e 61 44 61 t2byte(&(P)->aDa
1569d 74 61 5b 28 50 29 2d 3e 63 65 6c 6c 4f 66 66 73 ta[(P)->cellOffs
1569e 65 74 2b 32 2a 28 49 29 5d 29 29 29 0a 0a 2f 2a et+2*(I)])))../*
1569f 0a 2a 2a 20 54 68 69 73 20 61 20 6d 6f 72 65 20 .** This a more
156a0 63 6f 6d 70 6c 65 78 20 76 65 72 73 69 6f 6e 20 complex version
156a1 6f 66 20 66 69 6e 64 43 65 6c 6c 28 29 20 74 68 of findCell() th
156a2 61 74 20 77 6f 72 6b 73 20 66 6f 72 0a 2a 2a 20 at works for.**
156a3 70 61 67 65 73 20 74 68 61 74 20 64 6f 20 63 6f pages that do co
156a4 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63 ntain overflow c
156a5 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ells..*/.static
156a6 75 38 20 2a 66 69 6e 64 4f 76 65 72 66 6c 6f 77 u8 *findOverflow
156a7 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 Cell(MemPage *pP
156a8 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b age, int iCell){
156a9 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 . int i;. asse
156aa 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
156ab 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
156ac 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 t->mutex) );. f
156ad 6f 72 28 69 3d 70 50 61 67 65 2d 3e 6e 4f 76 65 or(i=pPage->nOve
156ae 72 66 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b 20 69 rflow-1; i>=0; i
156af 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6b 3b 0a --){. int k;.
156b0 20 20 20 20 73 74 72 75 63 74 20 5f 4f 76 66 6c struct _Ovfl
156b1 43 65 6c 6c 20 2a 70 4f 76 66 6c 3b 0a 20 20 20 Cell *pOvfl;.
156b2 20 70 4f 76 66 6c 20 3d 20 26 70 50 61 67 65 2d pOvfl = &pPage-
156b3 3e 61 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20 20 6b >aOvfl[i];. k
156b4 20 3d 20 70 4f 76 66 6c 2d 3e 69 64 78 3b 0a 20 = pOvfl->idx;.
156b5 20 20 20 69 66 28 20 6b 3c 3d 69 43 65 6c 6c 20 if( k<=iCell
156b6 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d ){. if( k==
156b7 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 iCell ){.
156b8 20 72 65 74 75 72 6e 20 70 4f 76 66 6c 2d 3e 70 return pOvfl->p
156b9 43 65 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Cell;. }.
156ba 20 20 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20 20 20 iCell--;.
156bb 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
156bc 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
156bd 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a iCell);.}../*.**
156be 20 50 61 72 73 65 20 61 20 63 65 6c 6c 20 63 6f Parse a cell co
156bf 6e 74 65 6e 74 20 62 6c 6f 63 6b 20 61 6e 64 20 ntent block and
156c0 66 69 6c 6c 20 69 6e 20 74 68 65 20 43 65 6c 6c fill in the Cell
156c1 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20 Info structure.
156c2 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20 74 77 There.** are tw
156c3 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 o versions of th
156c4 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 62 74 is function. bt
156c5 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74 reeParseCell() t
156c6 61 6b 65 73 20 61 20 0a 2a 2a 20 63 65 6c 6c 20 akes a .** cell
156c7 69 6e 64 65 78 20 61 73 20 74 68 65 20 73 65 63 index as the sec
156c8 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 ond argument and
156c9 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 btreeParseCellP
156ca 74 72 28 29 20 0a 2a 2a 20 74 61 6b 65 73 20 61 tr() .** takes a
156cb 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
156cc 62 6f 64 79 20 6f 66 20 74 68 65 20 63 65 6c 6c body of the cell
156cd 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64 20 61 as its second a
156ce 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 rgument..**.** W
156cf 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c 65 2c ithin this file,
156d0 20 74 68 65 20 70 61 72 73 65 43 65 6c 6c 28 29 the parseCell()
156d1 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 63 61 macro can be ca
156d2 6c 6c 65 64 20 69 6e 73 74 65 61 64 20 6f 66 0a lled instead of.
156d3 2a 2a 20 62 74 72 65 65 50 61 72 73 65 43 65 6c ** btreeParseCel
156d4 6c 50 74 72 28 29 2e 20 55 73 69 6e 67 20 73 6f lPtr(). Using so
156d5 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20 74 68 me compilers, th
156d6 69 73 20 77 69 6c 6c 20 62 65 20 66 61 73 74 65 is will be faste
156d7 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 r..*/.static voi
156d8 64 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c d btreeParseCell
156d9 50 74 72 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a Ptr(. MemPage *
156da 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 2f pPage, /
156db 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e * Page containin
156dc 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 g the cell */.
156dd 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 u8 *pCell,
156de 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
156df 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 74 er to the cell t
156e0 65 78 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e ext. */. CellIn
156e1 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 20 20 fo *pInfo
156e2 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 69 /* Fill in thi
156e3 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 s structure */.)
156e4 7b 0a 20 20 75 31 36 20 6e 3b 20 20 20 20 20 20 {. u16 n;
156e5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
156e6 75 6d 62 65 72 20 62 79 74 65 73 20 69 6e 20 63 umber bytes in c
156e7 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65 61 64 ell content head
156e8 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 50 61 79 er */. u32 nPay
156e9 6c 6f 61 64 3b 20 20 20 20 20 20 20 20 20 20 20 load;
156ea 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
156eb 65 73 20 6f 66 20 63 65 6c 6c 20 70 61 79 6c 6f es of cell paylo
156ec 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 ad */.. assert(
156ed 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
156ee 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
156ef 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70 49 6e mutex) );.. pIn
156f0 66 6f 2d 3e 70 43 65 6c 6c 20 3d 20 70 43 65 6c fo->pCell = pCel
156f1 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 l;. assert( pPa
156f2 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c 20 70 ge->leaf==0 || p
156f3 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20 29 3b Page->leaf==1 );
156f4 0a 20 20 6e 20 3d 20 70 50 61 67 65 2d 3e 63 68 . n = pPage->ch
156f5 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 61 73 ildPtrSize;. as
156f6 73 65 72 74 28 20 6e 3d 3d 34 2d 34 2a 70 50 61 sert( n==4-4*pPa
156f7 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 69 66 ge->leaf );. if
156f8 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 ( pPage->intKey
156f9 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 ){. if( pPage
156fa 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 ->hasData ){.
156fb 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e n += getVarin
156fc 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e t32(&pCell[n], n
156fd 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 7d 65 Payload);. }e
156fe 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 79 6c lse{. nPayl
156ff 6f 61 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 oad = 0;. }.
15700 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e n += getVarin
15701 74 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28 75 36 t(&pCell[n], (u6
15702 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65 79 29 4*)&pInfo->nKey)
15703 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 44 61 ;. pInfo->nDa
15704 74 61 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 ta = nPayload;.
15705 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 66 }else{. pInf
15706 6f 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 o->nData = 0;.
15707 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 n += getVarint
15708 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 32(&pCell[n], nP
15709 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 70 49 6e ayload);. pIn
1570a 66 6f 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61 79 6c fo->nKey = nPayl
1570b 6f 61 64 3b 0a 20 20 7d 0a 20 20 70 49 6e 66 6f oad;. }. pInfo
1570c 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 50 61 ->nPayload = nPa
1570d 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 6f 2d 3e yload;. pInfo->
1570e 6e 48 65 61 64 65 72 20 3d 20 6e 3b 0a 20 20 74 nHeader = n;. t
1570f 65 73 74 63 61 73 65 28 20 6e 50 61 79 6c 6f 61 estcase( nPayloa
15710 64 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 d==pPage->maxLoc
15711 61 6c 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 al );. testcase
15712 28 20 6e 50 61 79 6c 6f 61 64 3d 3d 70 50 61 67 ( nPayload==pPag
15713 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 29 3b e->maxLocal+1 );
15714 0a 20 20 69 66 28 20 6c 69 6b 65 6c 79 28 6e 50 . if( likely(nP
15715 61 79 6c 6f 61 64 3c 3d 70 50 61 67 65 2d 3e 6d ayload<=pPage->m
15716 61 78 4c 6f 63 61 6c 29 20 29 7b 0a 20 20 20 20 axLocal) ){.
15717 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 28 /* This is the (
15718 65 61 73 79 29 20 63 6f 6d 6d 6f 6e 20 63 61 73 easy) common cas
15719 65 20 77 68 65 72 65 20 74 68 65 20 65 6e 74 69 e where the enti
1571a 72 65 20 70 61 79 6c 6f 61 64 20 66 69 74 73 0a re payload fits.
1571b 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 6f ** on the lo
1571c 63 61 6c 20 70 61 67 65 2e 20 20 4e 6f 20 6f 76 cal page. No ov
1571d 65 72 66 6c 6f 77 20 69 73 20 72 65 71 75 69 72 erflow is requir
1571e 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ed.. */. i
1571f 6e 74 20 6e 53 69 7a 65 3b 20 20 20 20 20 20 20 nt nSize;
15720 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65 /* Total size
15721 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 of cell content
15722 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 in bytes */.
15723 20 6e 53 69 7a 65 20 3d 20 6e 50 61 79 6c 6f 61 nSize = nPayloa
15724 64 20 2b 20 6e 3b 0a 20 20 20 20 70 49 6e 66 6f d + n;. pInfo
15725 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 ->nLocal = (u16)
15726 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20 70 49 nPayload;. pI
15727 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 3d nfo->iOverflow =
15728 20 30 3b 0a 20 20 20 20 69 66 28 20 28 6e 53 69 0;. if( (nSi
15729 7a 65 20 26 20 7e 33 29 3d 3d 30 20 29 7b 0a 20 ze & ~3)==0 ){.
1572a 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b 20 nSize = 4;
1572b 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 /* Minimu
1572c 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 34 m cell size is 4
1572d 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 */. }. pI
1572e 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 28 75 31 nfo->nSize = (u1
1572f 36 29 6e 53 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 6)nSize;. }else
15730 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 {. /* If the
15731 70 61 79 6c 6f 61 64 20 77 69 6c 6c 20 6e 6f 74 payload will not
15732 20 66 69 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 fit completely
15733 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 on the local pag
15734 65 2c 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a e, we have. *
15735 2a 20 74 6f 20 64 65 63 69 64 65 20 68 6f 77 20 * to decide how
15736 6d 75 63 68 20 74 6f 20 73 74 6f 72 65 20 6c 6f much to store lo
15737 63 61 6c 6c 79 20 61 6e 64 20 68 6f 77 20 6d 75 cally and how mu
15738 63 68 20 74 6f 20 73 70 69 6c 6c 20 6f 6e 74 6f ch to spill onto
15739 0a 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 . ** overflow
1573a 20 70 61 67 65 73 2e 20 20 54 68 65 20 73 74 72 pages. The str
1573b 61 74 65 67 79 20 69 73 20 74 6f 20 6d 69 6e 69 ategy is to mini
1573c 6d 69 7a 65 20 74 68 65 20 61 6d 6f 75 6e 74 20 mize the amount
1573d 6f 66 20 75 6e 75 73 65 64 0a 20 20 20 20 2a 2a of unused. **
1573e 20 73 70 61 63 65 20 6f 6e 20 6f 76 65 72 66 6c space on overfl
1573f 6f 77 20 70 61 67 65 73 20 77 68 69 6c 65 20 6b ow pages while k
15740 65 65 70 69 6e 67 20 74 68 65 20 61 6d 6f 75 6e eeping the amoun
15741 74 20 6f 66 20 6c 6f 63 61 6c 20 73 74 6f 72 61 t of local stora
15742 67 65 0a 20 20 20 20 2a 2a 20 69 6e 20 62 65 74 ge. ** in bet
15743 77 65 65 6e 20 6d 69 6e 4c 6f 63 61 6c 20 61 6e ween minLocal an
15744 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a 20 20 20 20 d maxLocal..
15745 2a 2a 0a 20 20 20 20 2a 2a 20 57 61 72 6e 69 6e **. ** Warnin
15746 67 3a 20 20 63 68 61 6e 67 69 6e 67 20 74 68 65 g: changing the
15747 20 77 61 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 way overflow pa
15748 79 6c 6f 61 64 20 69 73 20 64 69 73 74 72 69 62 yload is distrib
15749 75 74 65 64 20 69 6e 20 61 6e 79 0a 20 20 20 20 uted in any.
1574a 2a 2a 20 77 61 79 20 77 69 6c 6c 20 72 65 73 75 ** way will resu
1574b 6c 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 lt in an incompa
1574c 74 69 62 6c 65 20 66 69 6c 65 20 66 6f 72 6d 61 tible file forma
1574d 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e t.. */. in
1574e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 2f 2a 20 t minLocal; /*
1574f 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f Minimum amount o
15750 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c f payload held l
15751 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 69 6e ocally */. in
15752 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 2f 2a 20 t maxLocal; /*
15753 4d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f Maximum amount o
15754 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c f payload held l
15755 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 69 6e ocally */. in
15756 74 20 73 75 72 70 6c 75 73 3b 20 20 20 2f 2a 20 t surplus; /*
15757 4f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61 64 Overflow payload
15758 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 6c available for l
15759 6f 63 61 6c 20 73 74 6f 72 61 67 65 20 2a 2f 0a ocal storage */.
1575a 0a 20 20 20 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 . minLocal =
1575b 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b pPage->minLocal;
1575c 0a 20 20 20 20 6d 61 78 4c 6f 63 61 6c 20 3d 20 . maxLocal =
1575d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 3b pPage->maxLocal;
1575e 0a 20 20 20 20 73 75 72 70 6c 75 73 20 3d 20 6d . surplus = m
1575f 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 50 61 79 6c inLocal + (nPayl
15760 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 61 6c 29 25 oad - minLocal)%
15761 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 (pPage->pBt->usa
15762 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20 bleSize - 4);.
15763 20 20 74 65 73 74 63 61 73 65 28 20 73 75 72 70 testcase( surp
15764 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 20 29 3b lus==maxLocal );
15765 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 73 . testcase( s
15766 75 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c urplus==maxLocal
15767 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20 73 75 +1 );. if( su
15768 72 70 6c 75 73 20 3c 3d 20 6d 61 78 4c 6f 63 61 rplus <= maxLoca
15769 6c 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f l ){. pInfo
1576a 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 ->nLocal = (u16)
1576b 73 75 72 70 6c 75 73 3b 0a 20 20 20 20 7d 65 6c surplus;. }el
1576c 73 65 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d se{. pInfo-
1576d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 6d >nLocal = (u16)m
1576e 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d 0a 20 inLocal;. }.
1576f 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 pInfo->iOverf
15770 6c 6f 77 20 3d 20 28 75 31 36 29 28 70 49 6e 66 low = (u16)(pInf
15771 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b 20 6e 29 3b 0a o->nLocal + n);.
15772 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 pInfo->nSize
15773 20 3d 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 = pInfo->iOverf
15774 6c 6f 77 20 2b 20 34 3b 0a 20 20 7d 0a 7d 0a 23 low + 4;. }.}.#
15775 64 65 66 69 6e 65 20 70 61 72 73 65 43 65 6c 6c define parseCell
15776 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c 20 70 (pPage, iCell, p
15777 49 6e 66 6f 29 20 5c 0a 20 20 62 74 72 65 65 50 Info) \. btreeP
15778 61 72 73 65 43 65 6c 6c 50 74 72 28 28 70 50 61 arseCellPtr((pPa
15779 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c 28 28 70 ge), findCell((p
1577a 50 61 67 65 29 2c 20 28 69 43 65 6c 6c 29 29 2c Page), (iCell)),
1577b 20 28 70 49 6e 66 6f 29 29 0a 73 74 61 74 69 63 (pInfo)).static
1577c 20 76 6f 69 64 20 62 74 72 65 65 50 61 72 73 65 void btreeParse
1577d 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 Cell(. MemPage
1577e 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 *pPage,
1577f 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 /* Page containi
15780 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 ng the cell */.
15781 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20 20 20 int iCell,
15782 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
15783 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46 69 72 cell index. Fir
15784 73 74 20 63 65 6c 6c 20 69 73 20 30 20 2a 2f 0a st cell is 0 */.
15785 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 CellInfo *pInf
15786 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c o /* Fil
15787 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 l in this struct
15788 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 61 72 73 ure */.){. pars
15789 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 eCell(pPage, iCe
1578a 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a 0a 2f ll, pInfo);.}../
1578b 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 *.** Compute the
1578c 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
1578d 20 62 79 74 65 73 20 74 68 61 74 20 61 20 43 65 bytes that a Ce
1578e 6c 6c 20 6e 65 65 64 73 20 69 6e 20 74 68 65 20 ll needs in the
1578f 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61 72 65 cell.** data are
15790 61 20 6f 66 20 74 68 65 20 62 74 72 65 65 2d 70 a of the btree-p
15791 61 67 65 2e 20 20 54 68 65 20 72 65 74 75 72 6e age. The return
15792 20 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64 65 73 number includes
15793 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 the cell.** dat
15794 61 20 68 65 61 64 65 72 20 61 6e 64 20 74 68 65 a header and the
15795 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 2c 20 local payload,
15796 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76 65 72 but not any over
15797 66 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a 2a 20 flow page or.**
15798 74 68 65 20 73 70 61 63 65 20 75 73 65 64 20 62 the space used b
15799 79 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 y the cell point
1579a 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 31 er..*/.static u1
1579b 36 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 4d 65 6 cellSizePtr(Me
1579c 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 mPage *pPage, u8
1579d 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 75 38 20 2a *pCell){. u8 *
1579e 70 49 74 65 72 20 3d 20 26 70 43 65 6c 6c 5b 70 pIter = &pCell[p
1579f 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 Page->childPtrSi
157a0 7a 65 5d 3b 0a 20 20 75 33 32 20 6e 53 69 7a 65 ze];. u32 nSize
157a1 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
157a2 5f 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 65 20 _DEBUG. /* The
157a3 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 value returned b
157a4 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 y this function
157a5 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 should always be
157a6 20 74 68 65 20 73 61 6d 65 20 61 73 0a 20 20 2a the same as. *
157a7 2a 20 74 68 65 20 28 43 65 6c 6c 49 6e 66 6f 2e * the (CellInfo.
157a8 6e 53 69 7a 65 29 20 76 61 6c 75 65 20 66 6f 75 nSize) value fou
157a9 6e 64 20 62 79 20 64 6f 69 6e 67 20 61 20 66 75 nd by doing a fu
157aa 6c 6c 20 70 61 72 73 65 20 6f 66 20 74 68 65 0a ll parse of the.
157ab 20 20 2a 2a 20 63 65 6c 6c 2e 20 49 66 20 53 51 ** cell. If SQ
157ac 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 64 65 LITE_DEBUG is de
157ad 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74 fined, an assert
157ae 28 29 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d () at the bottom
157af 20 6f 66 0a 20 20 2a 2a 20 74 68 69 73 20 66 75 of. ** this fu
157b0 6e 63 74 69 6f 6e 20 76 65 72 69 66 69 65 73 20 nction verifies
157b1 74 68 61 74 20 74 68 69 73 20 69 6e 76 61 72 69 that this invari
157b2 61 6e 74 20 69 73 20 6e 6f 74 20 76 69 6f 6c 61 ant is not viola
157b3 74 65 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e ted. */. CellIn
157b4 66 6f 20 64 65 62 75 67 69 6e 66 6f 3b 0a 20 20 fo debuginfo;.
157b5 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 btreeParseCellPt
157b6 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 r(pPage, pCell,
157b7 26 64 65 62 75 67 69 6e 66 6f 29 3b 0a 23 65 6e &debuginfo);.#en
157b8 64 69 66 0a 0a 20 20 69 66 28 20 70 50 61 67 65 dif.. if( pPage
157b9 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 ->intKey ){.
157ba 75 38 20 2a 70 45 6e 64 3b 0a 20 20 20 20 69 66 u8 *pEnd;. if
157bb 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 ( pPage->hasData
157bc 20 29 7b 0a 20 20 20 20 20 20 70 49 74 65 72 20 ){. pIter
157bd 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 70 += getVarint32(p
157be 49 74 65 72 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 Iter, nSize);.
157bf 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e }else{. n
157c0 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a Size = 0;. }.
157c1 0a 20 20 20 20 2f 2a 20 70 49 74 65 72 20 6e 6f . /* pIter no
157c2 77 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 w points at the
157c3 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 6b 64-bit integer k
157c4 65 79 20 76 61 6c 75 65 2c 20 61 20 76 61 72 69 ey value, a vari
157c5 61 62 6c 65 20 6c 65 6e 67 74 68 20 0a 20 20 20 able length .
157c6 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 54 68 65 ** integer. The
157c7 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b following block
157c8 20 6d 6f 76 65 73 20 70 49 74 65 72 20 74 6f 20 moves pIter to
157c9 70 6f 69 6e 74 20 61 74 20 74 68 65 20 66 69 72 point at the fir
157ca 73 74 20 62 79 74 65 0a 20 20 20 20 2a 2a 20 70 st byte. ** p
157cb 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 ast the end of t
157cc 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 2a 2f he key value. */
157cd 0a 20 20 20 20 70 45 6e 64 20 3d 20 26 70 49 74 . pEnd = &pIt
157ce 65 72 5b 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 er[9];. while
157cf 28 20 28 2a 70 49 74 65 72 2b 2b 29 26 30 78 38 ( (*pIter++)&0x8
157d0 30 20 26 26 20 70 49 74 65 72 3c 70 45 6e 64 20 0 && pIter<pEnd
157d1 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
157d2 70 49 74 65 72 20 2b 3d 20 67 65 74 56 61 72 69 pIter += getVari
157d3 6e 74 33 32 28 70 49 74 65 72 2c 20 6e 53 69 7a nt32(pIter, nSiz
157d4 65 29 3b 0a 20 20 7d 0a 0a 20 20 74 65 73 74 63 e);. }.. testc
157d5 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 ase( nSize==pPag
157d6 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 e->maxLocal );.
157d7 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a 65 testcase( nSize
157d8 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 ==pPage->maxLoca
157d9 6c 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e 53 69 l+1 );. if( nSi
157da 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 ze>pPage->maxLoc
157db 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 69 al ){. int mi
157dc 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65 2d 3e nLocal = pPage->
157dd 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 6e 53 minLocal;. nS
157de 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b ize = minLocal +
157df 20 28 6e 53 69 7a 65 20 2d 20 6d 69 6e 4c 6f 63 (nSize - minLoc
157e0 61 6c 29 20 25 20 28 70 50 61 67 65 2d 3e 70 42 al) % (pPage->pB
157e1 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 t->usableSize -
157e2 34 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 4);. testcase
157e3 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e ( nSize==pPage->
157e4 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 20 20 maxLocal );.
157e5 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a 65 3d testcase( nSize=
157e6 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c =pPage->maxLocal
157e7 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 53 +1 );. if( nS
157e8 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 4c 6f ize>pPage->maxLo
157e9 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 6e 53 69 cal ){. nSi
157ea 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 ze = minLocal;.
157eb 20 20 20 7d 0a 20 20 20 20 6e 53 69 7a 65 20 2b }. nSize +
157ec 3d 20 34 3b 0a 20 20 7d 0a 20 20 6e 53 69 7a 65 = 4;. }. nSize
157ed 20 2b 3d 20 28 75 33 32 29 28 70 49 74 65 72 20 += (u32)(pIter
157ee 2d 20 70 43 65 6c 6c 29 3b 0a 0a 20 20 2f 2a 20 - pCell);.. /*
157ef 54 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69 7a 65 The minimum size
157f0 20 6f 66 20 61 6e 79 20 63 65 6c 6c 20 69 73 20 of any cell is
157f1 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 69 66 4 bytes. */. if
157f2 28 20 6e 53 69 7a 65 3c 34 20 29 7b 0a 20 20 20 ( nSize<4 ){.
157f3 20 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 20 7d 0a nSize = 4;. }.
157f4 0a 20 20 61 73 73 65 72 74 28 20 6e 53 69 7a 65 . assert( nSize
157f5 3d 3d 64 65 62 75 67 69 6e 66 6f 2e 6e 53 69 7a ==debuginfo.nSiz
157f6 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 75 e );. return (u
157f7 31 36 29 6e 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66 16)nSize;.}..#if
157f8 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
157f9 0a 2f 2a 20 54 68 69 73 20 76 61 72 69 61 74 69 ./* This variati
157fa 6f 6e 20 6f 6e 20 63 65 6c 6c 53 69 7a 65 50 74 on on cellSizePt
157fb 72 28 29 20 69 73 20 75 73 65 64 20 69 6e 73 69 r() is used insi
157fc 64 65 20 6f 66 20 61 73 73 65 72 74 28 29 20 73 de of assert() s
157fd 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 6f 6e 6c tatements.** onl
157fe 79 2e 20 2a 2f 0a 73 74 61 74 69 63 20 75 31 36 y. */.static u16
157ff 20 63 65 6c 6c 53 69 7a 65 28 4d 65 6d 50 61 67 cellSize(MemPag
15800 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 43 e *pPage, int iC
15801 65 6c 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 ell){. return c
15802 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 ellSizePtr(pPage
15803 2c 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 , findCell(pPage
15804 2c 20 69 43 65 6c 6c 29 29 3b 0a 7d 0a 23 65 6e , iCell));.}.#en
15805 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c dif..#ifndef SQL
15806 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
15807 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 UUM./*.** If the
15808 20 63 65 6c 6c 20 70 43 65 6c 6c 2c 20 70 61 72 cell pCell, par
15809 74 20 6f 66 20 70 61 67 65 20 70 50 61 67 65 20 t of page pPage
1580a 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 contains a point
1580b 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 6f 76 65 72 er.** to an over
1580c 66 6c 6f 77 20 70 61 67 65 2c 20 69 6e 73 65 72 flow page, inser
1580d 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 t an entry into
1580e 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a the pointer-map.
1580f 2a 2a 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 ** for the overf
15810 6c 6f 77 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 low page..*/.sta
15811 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61 70 50 tic void ptrmapP
15812 75 74 4f 76 66 6c 50 74 72 28 4d 65 6d 50 61 67 utOvflPtr(MemPag
15813 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 43 e *pPage, u8 *pC
15814 65 6c 6c 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a ell, int *pRC){.
15815 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b CellInfo info;
15816 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 65 . if( *pRC ) re
15817 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 turn;. assert(
15818 70 43 65 6c 6c 21 3d 30 20 29 3b 0a 20 20 62 74 pCell!=0 );. bt
15819 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
1581a 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 pPage, pCell, &i
1581b 6e 66 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 nfo);. assert(
1581c 28 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61 (info.nData+(pPa
1581d 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 ge->intKey?0:inf
1581e 6f 2e 6e 4b 65 79 29 29 3d 3d 69 6e 66 6f 2e 6e o.nKey))==info.n
1581f 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 69 66 28 Payload );. if(
15820 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 info.iOverflow
15821 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6f 76 66 6c ){. Pgno ovfl
15822 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65 = get4byte(&pCe
15823 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
15824 77 5d 29 3b 0a 20 20 20 20 70 74 72 6d 61 70 50 w]);. ptrmapP
15825 75 74 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 6f ut(pPage->pBt, o
15826 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 vfl, PTRMAP_OVER
15827 46 4c 4f 57 31 2c 20 70 50 61 67 65 2d 3e 70 67 FLOW1, pPage->pg
15828 6e 6f 2c 20 70 52 43 29 3b 0a 20 20 7d 0a 7d 0a no, pRC);. }.}.
15829 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 #endif.../*.** D
1582a 65 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 efragment the pa
1582b 67 65 20 67 69 76 65 6e 2e 20 20 41 6c 6c 20 43 ge given. All C
1582c 65 6c 6c 73 20 61 72 65 20 6d 6f 76 65 64 20 74 ells are moved t
1582d 6f 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 o the.** end of
1582e 74 68 65 20 70 61 67 65 20 61 6e 64 20 61 6c 6c the page and all
1582f 20 66 72 65 65 20 73 70 61 63 65 20 69 73 20 63 free space is c
15830 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e ollected into on
15831 65 0a 2a 2a 20 62 69 67 20 46 72 65 65 42 6c 6b e.** big FreeBlk
15832 20 74 68 61 74 20 6f 63 63 75 72 73 20 69 6e 20 that occurs in
15833 62 65 74 77 65 65 6e 20 74 68 65 20 68 65 61 64 between the head
15834 65 72 20 61 6e 64 20 63 65 6c 6c 0a 2a 2a 20 70 er and cell.** p
15835 6f 69 6e 74 65 72 20 61 72 72 61 79 20 61 6e 64 ointer array and
15836 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
15837 74 20 61 72 65 61 2e 0a 2a 2f 0a 73 74 61 74 69 t area..*/.stati
15838 63 20 69 6e 74 20 64 65 66 72 61 67 6d 65 6e 74 c int defragment
15839 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 Page(MemPage *pP
1583a 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 age){. int i;
1583b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1583c 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
1583d 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 er */. int pc;
1583e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1583f 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 /* Address of
15840 20 61 20 69 2d 74 68 20 63 65 6c 6c 20 2a 2f 0a a i-th cell */.
15841 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20 int hdr;
15842 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
15843 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 70 61 Offset to the pa
15844 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 ge header */. i
15845 6e 74 20 73 69 7a 65 3b 20 20 20 20 20 20 20 20 nt size;
15846 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
15847 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a 20 e of a cell */.
15848 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b int usableSize;
15849 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1584a 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 20 umber of usable
1584b 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 65 20 bytes on a page
1584c 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 */. int cellOff
1584d 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 set;
1584e 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 /* Offset to the
1584f 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 cell pointer ar
15850 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 63 62 72 ray */. int cbr
15851 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k;
15852 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f /* Offset to
15853 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
15854 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e 74 20 t area */. int
15855 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 nCell;
15856 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
15857 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 of cells on the
15858 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 67 page */. unsig
15859 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b 20 ned char *data;
1585a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 /* The pag
1585b 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 6e 73 69 e data */. unsi
1585c 67 6e 65 64 20 63 68 61 72 20 2a 74 65 6d 70 3b gned char *temp;
1585d 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 61 /* Temp a
1585e 72 65 61 20 66 6f 72 20 63 65 6c 6c 20 63 6f 6e rea for cell con
1585f 74 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 tent */. int iC
15860 65 6c 6c 46 69 72 73 74 3b 20 20 20 20 20 20 20 ellFirst;
15861 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 6c /* First al
15862 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 69 6e 64 lowable cell ind
15863 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c ex */. int iCel
15864 6c 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 lLast;
15865 20 20 20 2f 2a 20 4c 61 73 74 20 70 6f 73 73 69 /* Last possi
15866 62 6c 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 2a ble cell index *
15867 2f 0a 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 /... assert( sq
15868 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
15869 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
1586a 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 Page) );. asser
1586b 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 t( pPage->pBt!=0
1586c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
1586d 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
1586e 53 69 7a 65 20 3c 3d 20 53 51 4c 49 54 45 5f 4d Size <= SQLITE_M
1586f 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 29 3b 0a AX_PAGE_SIZE );.
15870 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
15871 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b >nOverflow==0 );
15872 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
15873 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
15874 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
15875 20 29 3b 0a 20 20 74 65 6d 70 20 3d 20 73 71 6c );. temp = sql
15876 69 74 65 33 50 61 67 65 72 54 65 6d 70 53 70 61 ite3PagerTempSpa
15877 63 65 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70 ce(pPage->pBt->p
15878 50 61 67 65 72 29 3b 0a 20 20 64 61 74 61 20 3d Pager);. data =
15879 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 pPage->aData;.
1587a 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 hdr = pPage->hd
1587b 72 4f 66 66 73 65 74 3b 0a 20 20 63 65 6c 6c 4f rOffset;. cellO
1587c 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 63 ffset = pPage->c
1587d 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 6e 43 65 ellOffset;. nCe
1587e 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c ll = pPage->nCel
1587f 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 43 65 l;. assert( nCe
15880 6c 6c 3d 3d 67 65 74 32 62 79 74 65 28 26 64 61 ll==get2byte(&da
15881 74 61 5b 68 64 72 2b 33 5d 29 20 29 3b 0a 20 20 ta[hdr+3]) );.
15882 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 50 61 usableSize = pPa
15883 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 ge->pBt->usableS
15884 69 7a 65 3b 0a 20 20 63 62 72 6b 20 3d 20 67 65 ize;. cbrk = ge
15885 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 t2byte(&data[hdr
15886 2b 35 5d 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 +5]);. memcpy(&
15887 74 65 6d 70 5b 63 62 72 6b 5d 2c 20 26 64 61 74 temp[cbrk], &dat
15888 61 5b 63 62 72 6b 5d 2c 20 75 73 61 62 6c 65 53 a[cbrk], usableS
15889 69 7a 65 20 2d 20 63 62 72 6b 29 3b 0a 20 20 63 ize - cbrk);. c
1588a 62 72 6b 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 brk = usableSize
1588b 3b 0a 20 20 69 43 65 6c 6c 46 69 72 73 74 20 3d ;. iCellFirst =
1588c 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a cellOffset + 2*
1588d 6e 43 65 6c 6c 3b 0a 20 20 69 43 65 6c 6c 4c 61 nCell;. iCellLa
1588e 73 74 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 20 st = usableSize
1588f 2d 20 34 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 - 4;. for(i=0;
15890 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 i<nCell; i++){.
15891 20 20 20 75 38 20 2a 70 41 64 64 72 3b 20 20 20 u8 *pAddr;
15892 20 20 2f 2a 20 54 68 65 20 69 2d 74 68 20 63 65 /* The i-th ce
15893 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 ll pointer */.
15894 20 20 70 41 64 64 72 20 3d 20 26 64 61 74 61 5b pAddr = &data[
15895 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 69 2a 32 cellOffset + i*2
15896 5d 3b 0a 20 20 20 20 70 63 20 3d 20 67 65 74 32 ];. pc = get2
15897 62 79 74 65 28 70 41 64 64 72 29 3b 0a 20 20 20 byte(pAddr);.
15898 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69 testcase( pc==i
15899 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 CellFirst );.
1589a 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69 testcase( pc==i
1589b 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 23 69 66 20 CellLast );.#if
1589c 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
1589d 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f ENABLE_OVERSIZE_
1589e 43 45 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 20 20 CELL_CHECK).
1589f 2f 2a 20 54 68 65 73 65 20 63 6f 6e 64 69 74 69 /* These conditi
158a0 6f 6e 73 20 68 61 76 65 20 61 6c 72 65 61 64 79 ons have already
158a1 20 62 65 65 6e 20 76 65 72 69 66 69 65 64 20 69 been verified i
158a2 6e 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 n btreeInitPage(
158a3 29 0a 20 20 20 20 2a 2a 20 69 66 20 53 51 4c 49 ). ** if SQLI
158a4 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 TE_ENABLE_OVERSI
158a5 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 20 69 73 ZE_CELL_CHECK is
158a6 20 64 65 66 69 6e 65 64 20 0a 20 20 20 20 2a 2f defined . */
158a7 0a 20 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c . if( pc<iCel
158a8 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 65 lFirst || pc>iCe
158a9 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20 llLast ){.
158aa 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
158ab 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
158ac 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 61 73 73 }.#endif. ass
158ad 65 72 74 28 20 70 63 3e 3d 69 43 65 6c 6c 46 69 ert( pc>=iCellFi
158ae 72 73 74 20 26 26 20 70 63 3c 3d 69 43 65 6c 6c rst && pc<=iCell
158af 4c 61 73 74 20 29 3b 0a 20 20 20 20 73 69 7a 65 Last );. size
158b0 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 = cellSizePtr(p
158b1 50 61 67 65 2c 20 26 74 65 6d 70 5b 70 63 5d 29 Page, &temp[pc])
158b2 3b 0a 20 20 20 20 63 62 72 6b 20 2d 3d 20 73 69 ;. cbrk -= si
158b3 7a 65 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 ze;.#if defined(
158b4 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 SQLITE_ENABLE_OV
158b5 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 ERSIZE_CELL_CHEC
158b6 4b 29 0a 20 20 20 20 69 66 28 20 63 62 72 6b 3c K). if( cbrk<
158b7 69 43 65 6c 6c 46 69 72 73 74 20 29 7b 0a 20 20 iCellFirst ){.
158b8 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
158b9 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
158ba 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 }.#else.
158bb 69 66 28 20 63 62 72 6b 3c 69 43 65 6c 6c 46 69 if( cbrk<iCellFi
158bc 72 73 74 20 7c 7c 20 70 63 2b 73 69 7a 65 3e 75 rst || pc+size>u
158bd 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 sableSize ){.
158be 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
158bf 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
158c0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
158c1 61 73 73 65 72 74 28 20 63 62 72 6b 2b 73 69 7a assert( cbrk+siz
158c2 65 3c 3d 75 73 61 62 6c 65 53 69 7a 65 20 26 26 e<=usableSize &&
158c3 20 63 62 72 6b 3e 3d 69 43 65 6c 6c 46 69 72 73 cbrk>=iCellFirs
158c4 74 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 t );. testcas
158c5 65 28 20 63 62 72 6b 2b 73 69 7a 65 3d 3d 75 73 e( cbrk+size==us
158c6 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 ableSize );.
158c7 74 65 73 74 63 61 73 65 28 20 70 63 2b 73 69 7a testcase( pc+siz
158c8 65 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 3b e==usableSize );
158c9 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 . memcpy(&dat
158ca 61 5b 63 62 72 6b 5d 2c 20 26 74 65 6d 70 5b 70 a[cbrk], &temp[p
158cb 63 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 70 c], size);. p
158cc 75 74 32 62 79 74 65 28 70 41 64 64 72 2c 20 63 ut2byte(pAddr, c
158cd 62 72 6b 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 brk);. }. asse
158ce 72 74 28 20 63 62 72 6b 3e 3d 69 43 65 6c 6c 46 rt( cbrk>=iCellF
158cf 69 72 73 74 20 29 3b 0a 20 20 70 75 74 32 62 79 irst );. put2by
158d0 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c te(&data[hdr+5],
158d1 20 63 62 72 6b 29 3b 0a 20 20 64 61 74 61 5b 68 cbrk);. data[h
158d2 64 72 2b 31 5d 20 3d 20 30 3b 0a 20 20 64 61 74 dr+1] = 0;. dat
158d3 61 5b 68 64 72 2b 32 5d 20 3d 20 30 3b 0a 20 20 a[hdr+2] = 0;.
158d4 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20 30 3b data[hdr+7] = 0;
158d5 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b . memset(&data[
158d6 69 43 65 6c 6c 46 69 72 73 74 5d 2c 20 30 2c 20 iCellFirst], 0,
158d7 63 62 72 6b 2d 69 43 65 6c 6c 46 69 72 73 74 29 cbrk-iCellFirst)
158d8 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
158d9 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
158da 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
158db 67 65 29 20 29 3b 0a 20 20 69 66 28 20 63 62 72 ge) );. if( cbr
158dc 6b 2d 69 43 65 6c 6c 46 69 72 73 74 21 3d 70 50 k-iCellFirst!=pP
158dd 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20 age->nFree ){.
158de 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
158df 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
158e0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
158e1 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 E_OK;.}../*.** A
158e2 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79 llocate nByte by
158e3 74 65 73 20 6f 66 20 73 70 61 63 65 20 66 72 6f tes of space fro
158e4 6d 20 77 69 74 68 69 6e 20 74 68 65 20 42 2d 54 m within the B-T
158e5 72 65 65 20 70 61 67 65 20 70 61 73 73 65 64 0a ree page passed.
158e6 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ** as the first
158e7 61 72 67 75 6d 65 6e 74 2e 20 57 72 69 74 65 20 argument. Write
158e8 69 6e 74 6f 20 2a 70 49 64 78 20 74 68 65 20 69 into *pIdx the i
158e9 6e 64 65 78 20 69 6e 74 6f 20 70 50 61 67 65 2d ndex into pPage-
158ea 3e 61 44 61 74 61 5b 5d 0a 2a 2a 20 6f 66 20 74 >aData[].** of t
158eb 68 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 he first byte of
158ec 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 allocated space
158ed 2e 20 52 65 74 75 72 6e 20 65 69 74 68 65 72 20 . Return either
158ee 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 0a 2a 2a 20 SQLITE_OK or.**
158ef 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 28 75 an error code (u
158f0 73 75 61 6c 6c 79 20 53 51 4c 49 54 45 5f 43 4f sually SQLITE_CO
158f1 52 52 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 RRUPT)..**.** Th
158f2 65 20 63 61 6c 6c 65 72 20 67 75 61 72 61 6e 74 e caller guarant
158f3 65 65 73 20 74 68 61 74 20 74 68 65 72 65 20 69 ees that there i
158f4 73 20 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 s sufficient spa
158f5 63 65 20 74 6f 20 6d 61 6b 65 20 74 68 65 0a 2a ce to make the.*
158f6 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 * allocation. T
158f7 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 his routine migh
158f8 74 20 6e 65 65 64 20 74 6f 20 64 65 66 72 61 67 t need to defrag
158f9 6d 65 6e 74 20 69 6e 20 6f 72 64 65 72 20 74 6f ment in order to
158fa 20 62 72 69 6e 67 0a 2a 2a 20 61 6c 6c 20 74 68 bring.** all th
158fb 65 20 73 70 61 63 65 20 74 6f 67 65 74 68 65 72 e space together
158fc 2c 20 68 6f 77 65 76 65 72 2e 20 20 54 68 69 73 , however. This
158fd 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 61 76 routine will av
158fe 6f 69 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 oid using.** the
158ff 20 66 69 72 73 74 20 74 77 6f 20 62 79 74 65 73 first two bytes
15900 20 70 61 73 74 20 74 68 65 20 63 65 6c 6c 20 70 past the cell p
15901 6f 69 6e 74 65 72 20 61 72 65 61 20 73 69 6e 63 ointer area sinc
15902 65 20 70 72 65 73 75 6d 61 62 6c 79 20 74 68 69 e presumably thi
15903 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 s.** allocation
15904 69 73 20 62 65 69 6e 67 20 6d 61 64 65 20 69 6e is being made in
15905 20 6f 72 64 65 72 20 74 6f 20 69 6e 73 65 72 74 order to insert
15906 20 61 20 6e 65 77 20 63 65 6c 6c 2c 20 73 6f 20 a new cell, so
15907 77 65 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 we will.** also
15908 65 6e 64 20 75 70 20 6e 65 65 64 69 6e 67 20 61 end up needing a
15909 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65 new cell pointe
1590a 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
1590b 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 4d allocateSpace(M
1590c 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 emPage *pPage, i
1590d 6e 74 20 6e 42 79 74 65 2c 20 69 6e 74 20 2a 70 nt nByte, int *p
1590e 49 64 78 29 7b 0a 20 20 63 6f 6e 73 74 20 69 6e Idx){. const in
1590f 74 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 t hdr = pPage->h
15910 64 72 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 drOffset; /*
15911 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 70 Local cache of p
15912 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 Page->hdrOffset
15913 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 */. u8 * const
15914 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 data = pPage->aD
15915 61 74 61 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 63 ata; /* Loc
15916 61 6c 20 63 61 63 68 65 20 6f 66 20 70 50 61 67 al cache of pPag
15917 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20 69 6e e->aData */. in
15918 74 20 6e 46 72 61 67 3b 20 20 20 20 20 20 20 20 t nFrag;
15919 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1591a 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1591b 66 72 61 67 6d 65 6e 74 65 64 20 62 79 74 65 73 fragmented bytes
1591c 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 69 on pPage */. i
1591d 6e 74 20 74 6f 70 3b 20 20 20 20 20 20 20 20 20 nt top;
1591e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1591f 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 /* First byt
15920 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e e of cell conten
15921 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e 74 20 t area */. int
15922 67 61 70 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 gap; /* F
15923 69 72 73 74 20 62 79 74 65 20 6f 66 20 67 61 70 irst byte of gap
15924 20 62 65 74 77 65 65 6e 20 63 65 6c 6c 20 70 6f between cell po
15925 69 6e 74 65 72 73 20 61 6e 64 20 63 65 6c 6c 20 inters and cell
15926 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 content */. int
15927 20 72 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 rc; /*
15928 49 6e 74 65 67 65 72 20 72 65 74 75 72 6e 20 63 Integer return c
15929 6f 64 65 20 2a 2f 0a 20 20 0a 20 20 61 73 73 65 ode */. . asse
1592a 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
1592b 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
1592c 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
1592d 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
1592e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 pBt );. assert(
1592f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15930 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
15931 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
15932 72 74 28 20 6e 42 79 74 65 3e 3d 30 20 29 3b 20 rt( nByte>=0 );
15933 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c /* Minimum cell
15934 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 20 20 size is 4 */.
15935 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e assert( pPage->n
15936 46 72 65 65 3e 3d 6e 42 79 74 65 20 29 3b 0a 20 Free>=nByte );.
15937 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
15938 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a nOverflow==0 );.
15939 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 3c assert( nByte<
1593a 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
1593b 6c 65 53 69 7a 65 2d 38 20 29 3b 0a 0a 20 20 6e leSize-8 );.. n
1593c 46 72 61 67 20 3d 20 64 61 74 61 5b 68 64 72 2b Frag = data[hdr+
1593d 37 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 7];. assert( pP
1593e 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 age->cellOffset
1593f 3d 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a == hdr + 12 - 4*
15940 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 pPage->leaf );.
15941 20 67 61 70 20 3d 20 70 50 61 67 65 2d 3e 63 65 gap = pPage->ce
15942 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 llOffset + 2*pPa
15943 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 74 6f 70 ge->nCell;. top
15944 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
15945 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 69 66 28 a[hdr+5]);. if(
15946 20 67 61 70 3e 74 6f 70 20 29 20 72 65 74 75 72 gap>top ) retur
15947 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
15948 5f 42 4b 50 54 3b 0a 20 20 74 65 73 74 63 61 73 _BKPT;. testcas
15949 65 28 20 67 61 70 2b 32 3d 3d 74 6f 70 20 29 3b e( gap+2==top );
1594a 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 61 70 . testcase( gap
1594b 2b 31 3d 3d 74 6f 70 20 29 3b 0a 20 20 74 65 73 +1==top );. tes
1594c 74 63 61 73 65 28 20 67 61 70 3d 3d 74 6f 70 20 tcase( gap==top
1594d 29 3b 0a 0a 20 20 69 66 28 20 6e 46 72 61 67 3e );.. if( nFrag>
1594e 3d 36 30 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6c =60 ){. /* Al
1594f 77 61 79 73 20 64 65 66 72 61 67 6d 65 6e 74 20 ways defragment
15950 68 69 67 68 6c 79 20 66 72 61 67 6d 65 6e 74 65 highly fragmente
15951 64 20 70 61 67 65 73 20 2a 2f 0a 20 20 20 20 72 d pages */. r
15952 63 20 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 61 c = defragmentPa
15953 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69 ge(pPage);. i
15954 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
15955 63 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 c;. top = get
15956 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
15957 35 5d 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 5]);. }else if(
15958 20 67 61 70 2b 32 3c 3d 74 6f 70 20 29 7b 0a 20 gap+2<=top ){.
15959 20 20 20 2f 2a 20 53 65 61 72 63 68 20 74 68 65 /* Search the
1595a 20 66 72 65 65 6c 69 73 74 20 6c 6f 6f 6b 69 6e freelist lookin
1595b 67 20 66 6f 72 20 61 20 66 72 65 65 20 73 6c 6f g for a free slo
1595c 74 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 t big enough to
1595d 73 61 74 69 73 66 79 20 0a 20 20 20 20 2a 2a 20 satisfy . **
1595e 74 68 65 20 72 65 71 75 65 73 74 2e 20 54 68 65 the request. The
1595f 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 6d allocation is m
15960 61 64 65 20 66 72 6f 6d 20 74 68 65 20 66 69 72 ade from the fir
15961 73 74 20 66 72 65 65 20 73 6c 6f 74 20 69 6e 20 st free slot in
15962 0a 20 20 20 20 2a 2a 20 74 68 65 20 6c 69 73 74 . ** the list
15963 20 74 68 61 74 20 69 73 20 6c 61 72 67 65 20 65 that is large e
15964 6e 6f 75 67 68 20 74 6f 20 61 63 63 6f 6d 61 64 nough to accomad
15965 61 74 65 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 ate it.. */.
15966 20 20 20 69 6e 74 20 70 63 2c 20 61 64 64 72 3b int pc, addr;
15967 0a 20 20 20 20 66 6f 72 28 61 64 64 72 3d 68 64 . for(addr=hd
15968 72 2b 31 3b 20 28 70 63 20 3d 20 67 65 74 32 62 r+1; (pc = get2b
15969 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 yte(&data[addr])
1596a 29 3e 30 3b 20 61 64 64 72 3d 70 63 29 7b 0a 20 )>0; addr=pc){.
1596b 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 int size =
1596c 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 get2byte(&data[p
1596d 63 2b 32 5d 29 3b 20 20 20 20 20 2f 2a 20 53 69 c+2]); /* Si
1596e 7a 65 20 6f 66 20 66 72 65 65 20 73 6c 6f 74 20 ze of free slot
1596f 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 69 7a */. if( siz
15970 65 3e 3d 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 e>=nByte ){.
15971 20 20 20 20 69 6e 74 20 78 20 3d 20 73 69 7a 65 int x = size
15972 20 2d 20 6e 42 79 74 65 3b 0a 20 20 20 20 20 20 - nByte;.
15973 20 20 74 65 73 74 63 61 73 65 28 20 78 3d 3d 34 testcase( x==4
15974 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 );. test
15975 63 61 73 65 28 20 78 3d 3d 33 20 29 3b 0a 20 20 case( x==3 );.
15976 20 20 20 20 20 20 69 66 28 20 78 3c 34 20 29 7b if( x<4 ){
15977 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 . /* Re
15978 6d 6f 76 65 20 74 68 65 20 73 6c 6f 74 20 66 72 move the slot fr
15979 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 om the free-list
1597a 2e 20 55 70 64 61 74 65 20 74 68 65 20 6e 75 6d . Update the num
1597b 62 65 72 20 6f 66 0a 20 20 20 20 20 20 20 20 20 ber of.
1597c 20 2a 2a 20 66 72 61 67 6d 65 6e 74 65 64 20 62 ** fragmented b
1597d 79 74 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 ytes within the
1597e 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 page. */.
1597f 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b memcpy(&data[
15980 61 64 64 72 5d 2c 20 26 64 61 74 61 5b 70 63 5d addr], &data[pc]
15981 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 2);.
15982 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20 28 75 data[hdr+7] = (u
15983 38 29 28 6e 46 72 61 67 20 2b 20 78 29 3b 0a 20 8)(nFrag + x);.
15984 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
15985 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 /* The s
15986 6c 6f 74 20 72 65 6d 61 69 6e 73 20 6f 6e 20 74 lot remains on t
15987 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 20 52 65 he free-list. Re
15988 64 75 63 65 20 69 74 73 20 73 69 7a 65 20 74 6f duce its size to
15989 20 61 63 63 6f 75 6e 74 0a 20 20 20 20 20 20 20 account.
1598a 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 70 6f ** for the po
1598b 72 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 rtion used by th
1598c 65 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e e new allocation
1598d 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 . */. p
1598e 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 ut2byte(&data[pc
1598f 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 20 20 20 +2], x);.
15990 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 49 64 78 }. *pIdx
15991 20 3d 20 70 63 20 2b 20 78 3b 0a 20 20 20 20 20 = pc + x;.
15992 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
15993 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 _OK;. }.
15994 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 }. }.. /* Che
15995 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 ck to make sure
15996 74 68 65 72 65 20 69 73 20 65 6e 6f 75 67 68 20 there is enough
15997 73 70 61 63 65 20 69 6e 20 74 68 65 20 67 61 70 space in the gap
15998 20 74 6f 20 73 61 74 69 73 66 79 0a 20 20 2a 2a to satisfy. **
15999 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e the allocation.
1599a 20 20 49 66 20 6e 6f 74 2c 20 64 65 66 72 61 67 If not, defrag
1599b 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 ment.. */. tes
1599c 74 63 61 73 65 28 20 67 61 70 2b 32 2b 6e 42 79 tcase( gap+2+nBy
1599d 74 65 3d 3d 74 6f 70 20 29 3b 0a 20 20 69 66 28 te==top );. if(
1599e 20 67 61 70 2b 32 2b 6e 42 79 74 65 3e 74 6f 70 gap+2+nByte>top
1599f 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 64 65 66 ){. rc = def
159a0 72 61 67 6d 65 6e 74 50 61 67 65 28 70 50 61 67 ragmentPage(pPag
159a1 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 e);. if( rc )
159a2 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
159a3 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 top = get2byte(&
159a4 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 data[hdr+5]);.
159a5 20 20 61 73 73 65 72 74 28 20 67 61 70 2b 6e 42 assert( gap+nB
159a6 79 74 65 3c 3d 74 6f 70 20 29 3b 0a 20 20 7d 0a yte<=top );. }.
159a7 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 .. /* Allocate
159a8 6d 65 6d 6f 72 79 20 66 72 6f 6d 20 74 68 65 20 memory from the
159a9 67 61 70 20 69 6e 20 62 65 74 77 65 65 6e 20 74 gap in between t
159aa 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 he cell pointer
159ab 61 72 72 61 79 0a 20 20 2a 2a 20 61 6e 64 20 74 array. ** and t
159ac 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
159ad 61 72 65 61 2e 20 20 54 68 65 20 62 74 72 65 65 area. The btree
159ae 49 6e 69 74 50 61 67 65 28 29 20 63 61 6c 6c 20 InitPage() call
159af 68 61 73 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a has already. **
159b0 20 76 61 6c 69 64 61 74 65 64 20 74 68 65 20 66 validated the f
159b1 72 65 65 6c 69 73 74 2e 20 20 47 69 76 65 6e 20 reelist. Given
159b2 74 68 61 74 20 74 68 65 20 66 72 65 65 6c 69 73 that the freelis
159b3 74 20 69 73 20 76 61 6c 69 64 2c 20 74 68 65 72 t is valid, ther
159b4 65 0a 20 20 2a 2a 20 69 73 20 6e 6f 20 77 61 79 e. ** is no way
159b5 20 74 68 61 74 20 74 68 65 20 61 6c 6c 6f 63 61 that the alloca
159b6 74 69 6f 6e 20 63 61 6e 20 65 78 74 65 6e 64 20 tion can extend
159b7 6f 66 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 off the end of t
159b8 68 65 20 70 61 67 65 2e 0a 20 20 2a 2a 20 54 68 he page.. ** Th
159b9 65 20 61 73 73 65 72 74 28 29 20 62 65 6c 6f 77 e assert() below
159ba 20 76 65 72 69 66 69 65 73 20 74 68 65 20 70 72 verifies the pr
159bb 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 2e evious sentence.
159bc 0a 20 20 2a 2f 0a 20 20 74 6f 70 20 2d 3d 20 6e . */. top -= n
159bd 42 79 74 65 3b 0a 20 20 70 75 74 32 62 79 74 65 Byte;. put2byte
159be 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 74 (&data[hdr+5], t
159bf 6f 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74 op);. assert( t
159c0 6f 70 2b 6e 42 79 74 65 20 3c 3d 20 70 50 61 67 op+nByte <= pPag
159c1 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 e->pBt->usableSi
159c2 7a 65 20 29 3b 0a 20 20 2a 70 49 64 78 20 3d 20 ze );. *pIdx =
159c3 74 6f 70 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 top;. return SQ
159c4 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
159c5 2a 20 52 65 74 75 72 6e 20 61 20 73 65 63 74 69 * Return a secti
159c6 6f 6e 20 6f 66 20 74 68 65 20 70 50 61 67 65 2d on of the pPage-
159c7 3e 61 44 61 74 61 20 74 6f 20 74 68 65 20 66 72 >aData to the fr
159c8 65 65 6c 69 73 74 2e 0a 2a 2a 20 54 68 65 20 66 eelist..** The f
159c9 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 irst byte of the
159ca 20 6e 65 77 20 66 72 65 65 20 62 6c 6f 63 6b 20 new free block
159cb 69 73 20 70 50 61 67 65 2d 3e 61 44 69 73 6b 5b is pPage->aDisk[
159cc 73 74 61 72 74 5d 0a 2a 2a 20 61 6e 64 20 74 68 start].** and th
159cd 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 6c e size of the bl
159ce 6f 63 6b 20 69 73 20 22 73 69 7a 65 22 20 62 79 ock is "size" by
159cf 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 tes..**.** Most
159d0 6f 66 20 74 68 65 20 65 66 66 6f 72 74 20 68 65 of the effort he
159d1 72 65 20 69 73 20 69 6e 76 6f 6c 76 65 64 20 69 re is involved i
159d2 6e 20 63 6f 61 6c 65 73 69 6e 67 20 61 64 6a 61 n coalesing adja
159d3 63 65 6e 74 0a 2a 2a 20 66 72 65 65 20 62 6c 6f cent.** free blo
159d4 63 6b 73 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c cks into a singl
159d5 65 20 62 69 67 20 66 72 65 65 20 62 6c 6f 63 6b e big free block
159d6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
159d7 66 72 65 65 53 70 61 63 65 28 4d 65 6d 50 61 67 freeSpace(MemPag
159d8 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 73 74 e *pPage, int st
159d9 61 72 74 2c 20 69 6e 74 20 73 69 7a 65 29 7b 0a art, int size){.
159da 20 20 69 6e 74 20 61 64 64 72 2c 20 70 62 65 67 int addr, pbeg
159db 69 6e 2c 20 68 64 72 3b 0a 20 20 69 6e 74 20 69 in, hdr;. int i
159dc 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 Last;
159dd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
159de 4c 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65 Largest possible
159df 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 freeblock offse
159e0 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 t */. unsigned
159e1 63 68 61 72 20 2a 64 61 74 61 20 3d 20 70 50 61 char *data = pPa
159e2 67 65 2d 3e 61 44 61 74 61 3b 0a 0a 20 20 61 73 ge->aData;.. as
159e3 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 sert( pPage->pBt
159e4 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
159e5 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
159e6 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e riteable(pPage->
159e7 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 pDbPage) );. as
159e8 73 65 72 74 28 20 73 74 61 72 74 3e 3d 70 50 61 sert( start>=pPa
159e9 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 36 2b ge->hdrOffset+6+
159ea 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 pPage->childPtrS
159eb 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ize );. assert(
159ec 20 28 73 74 61 72 74 20 2b 20 73 69 7a 65 29 3c (start + size)<
159ed 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 =pPage->pBt->usa
159ee 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 bleSize );. ass
159ef 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
159f0 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
159f1 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
159f2 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 30 20 assert( size>=0
159f3 29 3b 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 ); /* Minimum
159f4 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a cell size is 4 *
159f5 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 /..#ifdef SQLITE
159f6 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 _SECURE_DELETE.
159f7 20 2f 2a 20 4f 76 65 72 77 72 69 74 65 20 64 65 /* Overwrite de
159f8 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f leted informatio
159f9 6e 20 77 69 74 68 20 7a 65 72 6f 73 20 77 68 65 n with zeros whe
159fa 6e 20 74 68 65 20 53 45 43 55 52 45 5f 44 45 4c n the SECURE_DEL
159fb 45 54 45 20 0a 20 20 2a 2a 20 6f 70 74 69 6f 6e ETE . ** option
159fc 20 69 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63 is enabled at c
159fd 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20 ompile-time */.
159fe 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 73 74 memset(&data[st
159ff 61 72 74 5d 2c 20 30 2c 20 73 69 7a 65 29 3b 0a art], 0, size);.
15a00 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 64 64 #endif.. /* Add
15a01 20 74 68 65 20 73 70 61 63 65 20 62 61 63 6b 20 the space back
15a02 69 6e 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 into the linked
15a03 6c 69 73 74 20 6f 66 20 66 72 65 65 62 6c 6f 63 list of freebloc
15a04 6b 73 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 ks. Note that.
15a05 20 2a 2a 20 65 76 65 6e 20 74 68 6f 75 67 68 20 ** even though
15a06 74 68 65 20 66 72 65 65 62 6c 6f 63 6b 20 6c 69 the freeblock li
15a07 73 74 20 77 61 73 20 63 68 65 63 6b 65 64 20 62 st was checked b
15a08 79 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 y btreeInitPage(
15a09 29 2c 0a 20 20 2a 2a 20 62 74 72 65 65 49 6e 69 ),. ** btreeIni
15a0a 74 50 61 67 65 28 29 20 64 69 64 20 6e 6f 74 20 tPage() did not
15a0b 64 65 74 65 63 74 20 6f 76 65 72 6c 61 70 70 69 detect overlappi
15a0c 6e 67 20 63 65 6c 6c 73 20 6f 72 0a 20 20 2a 2a ng cells or. **
15a0d 20 66 72 65 65 62 6c 6f 63 6b 73 20 74 68 61 74 freeblocks that
15a0e 20 6f 76 65 72 6c 61 70 70 65 64 20 63 65 6c 6c overlapped cell
15a0f 73 2e 20 20 20 4e 6f 72 20 64 6f 65 73 20 69 74 s. Nor does it
15a10 20 64 65 74 65 63 74 20 77 68 65 6e 20 74 68 65 detect when the
15a11 0a 20 20 2a 2a 20 63 65 6c 6c 20 63 6f 6e 74 65 . ** cell conte
15a12 6e 74 20 61 72 65 61 20 65 78 63 65 65 64 73 20 nt area exceeds
15a13 74 68 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65 the value in the
15a14 20 70 61 67 65 20 68 65 61 64 65 72 2e 20 20 49 page header. I
15a15 66 20 74 68 65 73 65 0a 20 20 2a 2a 20 73 69 74 f these. ** sit
15a16 75 61 74 69 6f 6e 73 20 61 72 69 73 65 2c 20 74 uations arise, t
15a17 68 65 6e 20 73 75 62 73 65 71 75 65 6e 74 20 69 hen subsequent i
15a18 6e 73 65 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 nsert operations
15a19 20 6d 69 67 68 74 20 63 6f 72 72 75 70 74 0a 20 might corrupt.
15a1a 20 2a 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74 ** the freelist
15a1b 2e 20 20 53 6f 20 77 65 20 64 6f 20 6e 65 65 64 . So we do need
15a1c 20 74 6f 20 63 68 65 63 6b 20 66 6f 72 20 63 6f to check for co
15a1d 72 72 75 70 74 69 6f 6e 20 77 68 69 6c 65 20 73 rruption while s
15a1e 63 61 6e 6e 69 6e 67 0a 20 20 2a 2a 20 74 68 65 canning. ** the
15a1f 20 66 72 65 65 6c 69 73 74 2e 0a 20 20 2a 2f 0a freelist.. */.
15a20 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 hdr = pPage->h
15a21 64 72 4f 66 66 73 65 74 3b 0a 20 20 61 64 64 72 drOffset;. addr
15a22 20 3d 20 68 64 72 20 2b 20 31 3b 0a 20 20 69 4c = hdr + 1;. iL
15a23 61 73 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 ast = pPage->pBt
15a24 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 ->usableSize - 4
15a25 3b 0a 20 20 61 73 73 65 72 74 28 20 73 74 61 72 ;. assert( star
15a26 74 3c 3d 69 4c 61 73 74 20 29 3b 0a 20 20 77 68 t<=iLast );. wh
15a27 69 6c 65 28 20 28 70 62 65 67 69 6e 20 3d 20 67 ile( (pbegin = g
15a28 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 64 et2byte(&data[ad
15a29 64 72 5d 29 29 3c 73 74 61 72 74 20 26 26 20 70 dr]))<start && p
15a2a 62 65 67 69 6e 3e 30 20 29 7b 0a 20 20 20 20 69 begin>0 ){. i
15a2b 66 28 20 70 62 65 67 69 6e 3c 61 64 64 72 2b 34 f( pbegin<addr+4
15a2c 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
15a2d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
15a2e 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 BKPT;. }.
15a2f 61 64 64 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 addr = pbegin;.
15a30 20 7d 0a 20 20 69 66 28 20 70 62 65 67 69 6e 3e }. if( pbegin>
15a31 69 4c 61 73 74 20 29 7b 0a 20 20 20 20 72 65 74 iLast ){. ret
15a32 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
15a33 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 61 PT_BKPT;. }. a
15a34 73 73 65 72 74 28 20 70 62 65 67 69 6e 3e 61 64 ssert( pbegin>ad
15a35 64 72 20 7c 7c 20 70 62 65 67 69 6e 3d 3d 30 20 dr || pbegin==0
15a36 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 );. put2byte(&d
15a37 61 74 61 5b 61 64 64 72 5d 2c 20 73 74 61 72 74 ata[addr], start
15a38 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 );. put2byte(&d
15a39 61 74 61 5b 73 74 61 72 74 5d 2c 20 70 62 65 67 ata[start], pbeg
15a3a 69 6e 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 in);. put2byte(
15a3b 26 64 61 74 61 5b 73 74 61 72 74 2b 32 5d 2c 20 &data[start+2],
15a3c 73 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e size);. pPage->
15a3d 6e 46 72 65 65 20 3d 20 70 50 61 67 65 2d 3e 6e nFree = pPage->n
15a3e 46 72 65 65 20 2b 20 28 75 31 36 29 73 69 7a 65 Free + (u16)size
15a3f 3b 0a 0a 20 20 2f 2a 20 43 6f 61 6c 65 73 63 65 ;.. /* Coalesce
15a40 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65 20 62 adjacent free b
15a41 6c 6f 63 6b 73 20 2a 2f 0a 20 20 61 64 64 72 20 locks */. addr
15a42 3d 20 68 64 72 20 2b 20 31 3b 0a 20 20 77 68 69 = hdr + 1;. whi
15a43 6c 65 28 20 28 70 62 65 67 69 6e 20 3d 20 67 65 le( (pbegin = ge
15a44 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 t2byte(&data[add
15a45 72 5d 29 29 3e 30 20 29 7b 0a 20 20 20 20 69 6e r]))>0 ){. in
15a46 74 20 70 6e 65 78 74 2c 20 70 73 69 7a 65 2c 20 t pnext, psize,
15a47 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 x;. assert( p
15a48 62 65 67 69 6e 3e 61 64 64 72 20 29 3b 0a 20 20 begin>addr );.
15a49 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69 6e assert( pbegin
15a4a 3c 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 <=pPage->pBt->us
15a4b 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b 0a 20 20 ableSize-4 );.
15a4c 20 20 70 6e 65 78 74 20 3d 20 67 65 74 32 62 79 pnext = get2by
15a4d 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 5d te(&data[pbegin]
15a4e 29 3b 0a 20 20 20 20 70 73 69 7a 65 20 3d 20 67 );. psize = g
15a4f 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 et2byte(&data[pb
15a50 65 67 69 6e 2b 32 5d 29 3b 0a 20 20 20 20 69 66 egin+2]);. if
15a51 28 20 70 62 65 67 69 6e 20 2b 20 70 73 69 7a 65 ( pbegin + psize
15a52 20 2b 20 33 20 3e 3d 20 70 6e 65 78 74 20 26 26 + 3 >= pnext &&
15a53 20 70 6e 65 78 74 3e 30 20 29 7b 0a 20 20 20 20 pnext>0 ){.
15a54 20 20 69 6e 74 20 66 72 61 67 20 3d 20 70 6e 65 int frag = pne
15a55 78 74 20 2d 20 28 70 62 65 67 69 6e 2b 70 73 69 xt - (pbegin+psi
15a56 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 ze);. if( (
15a57 66 72 61 67 3c 30 29 20 7c 7c 20 28 66 72 61 67 frag<0) || (frag
15a58 3e 28 69 6e 74 29 64 61 74 61 5b 68 64 72 2b 37 >(int)data[hdr+7
15a59 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 ]) ){. re
15a5a 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
15a5b 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
15a5c 7d 0a 20 20 20 20 20 20 64 61 74 61 5b 68 64 72 }. data[hdr
15a5d 2b 37 5d 20 2d 3d 20 28 75 38 29 66 72 61 67 3b +7] -= (u8)frag;
15a5e 0a 20 20 20 20 20 20 78 20 3d 20 67 65 74 32 62 . x = get2b
15a5f 79 74 65 28 26 64 61 74 61 5b 70 6e 65 78 74 5d yte(&data[pnext]
15a60 29 3b 0a 20 20 20 20 20 20 70 75 74 32 62 79 74 );. put2byt
15a61 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c e(&data[pbegin],
15a62 20 78 29 3b 0a 20 20 20 20 20 20 78 20 3d 20 70 x);. x = p
15a63 6e 65 78 74 20 2b 20 67 65 74 32 62 79 74 65 28 next + get2byte(
15a64 26 64 61 74 61 5b 70 6e 65 78 74 2b 32 5d 29 20 &data[pnext+2])
15a65 2d 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 20 20 - pbegin;.
15a66 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 put2byte(&data[p
15a67 62 65 67 69 6e 2b 32 5d 2c 20 78 29 3b 0a 20 20 begin+2], x);.
15a68 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 }else{. a
15a69 64 64 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 ddr = pbegin;.
15a6a 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 }. }.. /* If
15a6b 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
15a6c 74 20 61 72 65 61 20 62 65 67 69 6e 73 20 77 69 t area begins wi
15a6d 74 68 20 61 20 66 72 65 65 62 6c 6f 63 6b 2c 20 th a freeblock,
15a6e 72 65 6d 6f 76 65 20 69 74 2e 20 2a 2f 0a 20 20 remove it. */.
15a6f 69 66 28 20 64 61 74 61 5b 68 64 72 2b 31 5d 3d if( data[hdr+1]=
15a70 3d 64 61 74 61 5b 68 64 72 2b 35 5d 20 26 26 20 =data[hdr+5] &&
15a71 64 61 74 61 5b 68 64 72 2b 32 5d 3d 3d 64 61 74 data[hdr+2]==dat
15a72 61 5b 68 64 72 2b 36 5d 20 29 7b 0a 20 20 20 20 a[hdr+6] ){.
15a73 69 6e 74 20 74 6f 70 3b 0a 20 20 20 20 70 62 65 int top;. pbe
15a74 67 69 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 gin = get2byte(&
15a75 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 data[hdr+1]);.
15a76 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 68 memcpy(&data[h
15a77 64 72 2b 31 5d 2c 20 26 64 61 74 61 5b 70 62 65 dr+1], &data[pbe
15a78 67 69 6e 5d 2c 20 32 29 3b 0a 20 20 20 20 74 6f gin], 2);. to
15a79 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 p = get2byte(&da
15a7a 74 61 5b 68 64 72 2b 35 5d 29 20 2b 20 67 65 74 ta[hdr+5]) + get
15a7b 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 2byte(&data[pbeg
15a7c 69 6e 2b 32 5d 29 3b 0a 20 20 20 20 70 75 74 32 in+2]);. put2
15a7d 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
15a7e 5d 2c 20 74 6f 70 29 3b 0a 20 20 7d 0a 20 20 61 ], top);. }. a
15a7f 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
15a80 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
15a81 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
15a82 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
15a83 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 E_OK;.}../*.** D
15a84 65 63 6f 64 65 20 74 68 65 20 66 6c 61 67 73 20 ecode the flags
15a85 62 79 74 65 20 28 74 68 65 20 66 69 72 73 74 20 byte (the first
15a86 62 79 74 65 20 6f 66 20 74 68 65 20 68 65 61 64 byte of the head
15a87 65 72 29 20 66 6f 72 20 61 20 70 61 67 65 0a 2a er) for a page.*
15a88 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 * and initialize
15a89 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 4d fields of the M
15a8a 65 6d 50 61 67 65 20 73 74 72 75 63 74 75 72 65 emPage structure
15a8b 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a accordingly..**
15a8c 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 66 6f 6c .** Only the fol
15a8d 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69 lowing combinati
15a8e 6f 6e 73 20 61 72 65 20 73 75 70 70 6f 72 74 65 ons are supporte
15a8f 64 2e 20 20 41 6e 79 74 68 69 6e 67 20 64 69 66 d. Anything dif
15a90 66 65 72 65 6e 74 0a 2a 2a 20 69 6e 64 69 63 61 ferent.** indica
15a91 74 65 73 20 61 20 63 6f 72 72 75 70 74 20 64 61 tes a corrupt da
15a92 74 61 62 61 73 65 20 66 69 6c 65 73 3a 0a 2a 2a tabase files:.**
15a93 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f .** PTF_
15a94 5a 45 52 4f 44 41 54 41 0a 2a 2a 20 20 20 20 20 ZERODATA.**
15a95 20 20 20 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 PTF_ZERODATA
15a96 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2a 20 20 | PTF_LEAF.**
15a97 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41 46 44 PTF_LEAFD
15a98 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 ATA | PTF_INTKEY
15a99 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f .** PTF_
15a9a 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 LEAFDATA | PTF_I
15a9b 4e 54 4b 45 59 20 7c 20 50 54 46 5f 4c 45 41 46 NTKEY | PTF_LEAF
15a9c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 .*/.static int d
15a9d 65 63 6f 64 65 46 6c 61 67 73 28 4d 65 6d 50 61 ecodeFlags(MemPa
15a9e 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 ge *pPage, int f
15a9f 6c 61 67 42 79 74 65 29 7b 0a 20 20 42 74 53 68 lagByte){. BtSh
15aa0 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 2f ared *pBt; /
15aa1 2a 20 41 20 63 6f 70 79 20 6f 66 20 70 50 61 67 * A copy of pPag
15aa2 65 2d 3e 70 42 74 20 2a 2f 0a 0a 20 20 61 73 73 e->pBt */.. ass
15aa3 65 72 74 28 20 70 50 61 67 65 2d 3e 68 64 72 4f ert( pPage->hdrO
15aa4 66 66 73 65 74 3d 3d 28 70 50 61 67 65 2d 3e 70 ffset==(pPage->p
15aa5 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 gno==1 ? 100 : 0
15aa6 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
15aa7 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
15aa8 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
15aa9 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 2d tex) );. pPage-
15aaa 3e 6c 65 61 66 20 3d 20 28 75 38 29 28 66 6c 61 >leaf = (u8)(fla
15aab 67 42 79 74 65 3e 3e 33 29 3b 20 20 61 73 73 65 gByte>>3); asse
15aac 72 74 28 20 50 54 46 5f 4c 45 41 46 20 3d 3d 20 rt( PTF_LEAF ==
15aad 31 3c 3c 33 20 29 3b 0a 20 20 66 6c 61 67 42 79 1<<3 );. flagBy
15aae 74 65 20 26 3d 20 7e 50 54 46 5f 4c 45 41 46 3b te &= ~PTF_LEAF;
15aaf 0a 20 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 . pPage->childP
15ab0 74 72 53 69 7a 65 20 3d 20 34 2d 34 2a 70 50 61 trSize = 4-4*pPa
15ab1 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 70 42 74 20 ge->leaf;. pBt
15ab2 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 = pPage->pBt;.
15ab3 69 66 28 20 66 6c 61 67 42 79 74 65 3d 3d 28 50 if( flagByte==(P
15ab4 54 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 54 TF_LEAFDATA | PT
15ab5 46 5f 49 4e 54 4b 45 59 29 20 29 7b 0a 20 20 20 F_INTKEY) ){.
15ab6 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d pPage->intKey =
15ab7 20 31 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 68 1;. pPage->h
15ab8 61 73 44 61 74 61 20 3d 20 70 50 61 67 65 2d 3e asData = pPage->
15ab9 6c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d leaf;. pPage-
15aba 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d >maxLocal = pBt-
15abb 3e 6d 61 78 4c 65 61 66 3b 0a 20 20 20 20 70 50 >maxLeaf;. pP
15abc 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 age->minLocal =
15abd 70 42 74 2d 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20 pBt->minLeaf;.
15abe 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 42 79 }else if( flagBy
15abf 74 65 3d 3d 50 54 46 5f 5a 45 52 4f 44 41 54 41 te==PTF_ZERODATA
15ac0 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 ){. pPage->i
15ac1 6e 74 4b 65 79 20 3d 20 30 3b 0a 20 20 20 20 70 ntKey = 0;. p
15ac2 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 3d 20 Page->hasData =
15ac3 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 0;. pPage->ma
15ac4 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 xLocal = pBt->ma
15ac5 78 4c 6f 63 61 6c 3b 0a 20 20 20 20 70 50 61 67 xLocal;. pPag
15ac6 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 e->minLocal = pB
15ac7 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d t->minLocal;. }
15ac8 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
15ac9 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
15aca 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 BKPT;. }. retu
15acb 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
15acc 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a ./*.** Initializ
15acd 65 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 e the auxiliary
15ace 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 information for
15acf 61 20 64 69 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a a disk block..**
15ad0 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
15ad1 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e E_OK on success.
15ad2 20 20 49 66 20 77 65 20 73 65 65 20 74 68 61 74 If we see that
15ad3 20 74 68 65 20 70 61 67 65 20 64 6f 65 73 0a 2a the page does.*
15ad4 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 * not contain a
15ad5 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 well-formed data
15ad6 62 61 73 65 20 70 61 67 65 2c 20 74 68 65 6e 20 base page, then
15ad7 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 return .** SQLIT
15ad8 45 5f 43 4f 52 52 55 50 54 2e 20 20 4e 6f 74 65 E_CORRUPT. Note
15ad9 20 74 68 61 74 20 61 20 72 65 74 75 72 6e 20 6f that a return o
15ada 66 20 53 51 4c 49 54 45 5f 4f 4b 20 64 6f 65 73 f SQLITE_OK does
15adb 20 6e 6f 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 not.** guarante
15adc 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 e that the page
15add 69 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20 is well-formed.
15ade 20 49 74 20 6f 6e 6c 79 20 73 68 6f 77 73 20 74 It only shows t
15adf 68 61 74 0a 2a 2a 20 77 65 20 66 61 69 6c 65 64 hat.** we failed
15ae0 20 74 6f 20 64 65 74 65 63 74 20 61 6e 79 20 63 to detect any c
15ae1 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 orruption..*/.st
15ae2 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 49 6e atic int btreeIn
15ae3 69 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a itPage(MemPage *
15ae4 70 50 61 67 65 29 7b 0a 0a 20 20 61 73 73 65 72 pPage){.. asser
15ae5 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 t( pPage->pBt!=0
15ae6 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
15ae7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
15ae8 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
15ae9 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
15aea 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 pPage->pgno==sq
15aeb 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 lite3PagerPagenu
15aec 6d 62 65 72 28 70 50 61 67 65 2d 3e 70 44 62 50 mber(pPage->pDbP
15aed 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 age) );. assert
15aee 28 20 70 50 61 67 65 20 3d 3d 20 73 71 6c 69 74 ( pPage == sqlit
15aef 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 e3PagerGetExtra(
15af0 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
15af1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
15af2 67 65 2d 3e 61 44 61 74 61 20 3d 3d 20 73 71 6c ge->aData == sql
15af3 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 ite3PagerGetData
15af4 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
15af5 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 50 61 67 );.. if( !pPag
15af6 65 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 e->isInit ){.
15af7 20 75 31 36 20 70 63 3b 20 20 20 20 20 20 20 20 u16 pc;
15af8 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f /* Address o
15af9 66 20 61 20 66 72 65 65 62 6c 6f 63 6b 20 77 69 f a freeblock wi
15afa 74 68 69 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 thin pPage->aDat
15afb 61 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 20 68 64 a[] */. u8 hd
15afc 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a r; /*
15afd 20 4f 66 66 73 65 74 20 74 6f 20 62 65 67 69 6e Offset to begin
15afe 6e 69 6e 67 20 6f 66 20 70 61 67 65 20 68 65 61 ning of page hea
15aff 64 65 72 20 2a 2f 0a 20 20 20 20 75 38 20 2a 64 der */. u8 *d
15b00 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ata; /*
15b01 20 45 71 75 61 6c 20 74 6f 20 70 50 61 67 65 2d Equal to pPage-
15b02 3e 61 44 61 74 61 20 2a 2f 0a 20 20 20 20 42 74 >aData */. Bt
15b03 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 Shared *pBt;
15b04 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 /* The main
15b05 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 btree structure
15b06 2a 2f 0a 20 20 20 20 75 31 36 20 75 73 61 62 6c */. u16 usabl
15b07 65 53 69 7a 65 3b 20 20 20 20 2f 2a 20 41 6d 6f eSize; /* Amo
15b08 75 6e 74 20 6f 66 20 75 73 61 62 6c 65 20 73 70 unt of usable sp
15b09 61 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67 65 ace on each page
15b0a 20 2a 2f 0a 20 20 20 20 75 31 36 20 63 65 6c 6c */. u16 cell
15b0b 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 Offset; /* Of
15b0c 66 73 65 74 20 66 72 6f 6d 20 73 74 61 72 74 20 fset from start
15b0d 6f 66 20 70 61 67 65 20 74 6f 20 66 69 72 73 74 of page to first
15b0e 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f cell pointer */
15b0f 0a 20 20 20 20 75 31 36 20 6e 46 72 65 65 3b 20 . u16 nFree;
15b10 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
15b11 72 20 6f 66 20 75 6e 75 73 65 64 20 62 79 74 65 r of unused byte
15b12 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f s on the page */
15b13 0a 20 20 20 20 75 31 36 20 74 6f 70 3b 20 20 20 . u16 top;
15b14 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
15b15 20 62 79 74 65 20 6f 66 20 74 68 65 20 63 65 6c byte of the cel
15b16 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a l content area *
15b17 2f 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 46 /. int iCellF
15b18 69 72 73 74 3b 20 20 20 20 2f 2a 20 46 69 72 73 irst; /* Firs
15b19 74 20 61 6c 6c 6f 77 61 62 6c 65 20 63 65 6c 6c t allowable cell
15b1a 20 6f 72 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66 or freeblock of
15b1b 66 73 65 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 fset */. int
15b1c 69 43 65 6c 6c 4c 61 73 74 3b 20 20 20 20 20 2f iCellLast; /
15b1d 2a 20 4c 61 73 74 20 70 6f 73 73 69 62 6c 65 20 * Last possible
15b1e 63 65 6c 6c 20 6f 72 20 66 72 65 65 62 6c 6f 63 cell or freebloc
15b1f 6b 20 6f 66 66 73 65 74 20 2a 2f 0a 0a 20 20 20 k offset */..
15b20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 pBt = pPage->pB
15b21 74 3b 0a 0a 20 20 20 20 68 64 72 20 3d 20 70 50 t;.. hdr = pP
15b22 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a age->hdrOffset;.
15b23 20 20 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 data = pPage
15b24 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 66 28 ->aData;. if(
15b25 20 64 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61 decodeFlags(pPa
15b26 67 65 2c 20 64 61 74 61 5b 68 64 72 5d 29 20 29 ge, data[hdr]) )
15b27 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
15b28 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
15b29 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 61 assert( pBt->pa
15b2a 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 geSize>=512 && p
15b2b 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 3d 33 32 Bt->pageSize<=32
15b2c 37 36 38 20 29 3b 0a 20 20 20 20 70 50 61 67 65 768 );. pPage
15b2d 2d 3e 6d 61 73 6b 50 61 67 65 20 3d 20 70 42 74 ->maskPage = pBt
15b2e 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 31 3b 0a ->pageSize - 1;.
15b2f 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 pPage->nOver
15b30 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 75 73 flow = 0;. us
15b31 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e ableSize = pBt->
15b32 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 usableSize;.
15b33 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 pPage->cellOffse
15b34 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d t = cellOffset =
15b35 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 hdr + 12 - 4*pP
15b36 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 74 age->leaf;. t
15b37 6f 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 op = get2byte(&d
15b38 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 ata[hdr+5]);.
15b39 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 pPage->nCell =
15b3a 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
15b3b 64 72 2b 33 5d 29 3b 0a 20 20 20 20 69 66 28 20 dr+3]);. if(
15b3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f pPage->nCell>MX_
15b3d 43 45 4c 4c 28 70 42 74 29 20 29 7b 0a 20 20 20 CELL(pBt) ){.
15b3e 20 20 20 2f 2a 20 54 6f 20 6d 61 6e 79 20 63 65 /* To many ce
15b3f 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 lls for a single
15b40 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 67 65 page. The page
15b41 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70 74 must be corrupt
15b42 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e */. return
15b43 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
15b44 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 BKPT;. }.
15b45 74 65 73 74 63 61 73 65 28 20 70 50 61 67 65 2d testcase( pPage-
15b46 3e 6e 43 65 6c 6c 3d 3d 4d 58 5f 43 45 4c 4c 28 >nCell==MX_CELL(
15b47 70 42 74 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 pBt) );.. /*
15b48 41 20 6d 61 6c 66 6f 72 6d 65 64 20 64 61 74 61 A malformed data
15b49 62 61 73 65 20 70 61 67 65 20 6d 69 67 68 74 20 base page might
15b4a 63 61 75 73 65 20 75 73 20 74 6f 20 72 65 61 64 cause us to read
15b4b 20 70 61 73 74 20 74 68 65 20 65 6e 64 0a 20 20 past the end.
15b4c 20 20 2a 2a 20 6f 66 20 70 61 67 65 20 77 68 65 ** of page whe
15b4d 6e 20 70 61 72 73 69 6e 67 20 61 20 63 65 6c 6c n parsing a cell
15b4e 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a . . **. *
15b4f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
15b50 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 63 68 block of code ch
15b51 65 63 6b 73 20 65 61 72 6c 79 20 74 6f 20 73 65 ecks early to se
15b52 65 20 69 66 20 61 20 63 65 6c 6c 20 65 78 74 65 e if a cell exte
15b53 6e 64 73 0a 20 20 20 20 2a 2a 20 70 61 73 74 20 nds. ** past
15b54 74 68 65 20 65 6e 64 20 6f 66 20 61 20 70 61 67 the end of a pag
15b55 65 20 62 6f 75 6e 64 61 72 79 20 61 6e 64 20 63 e boundary and c
15b56 61 75 73 65 73 20 53 51 4c 49 54 45 5f 43 4f 52 auses SQLITE_COR
15b57 52 55 50 54 20 74 6f 20 62 65 20 0a 20 20 20 20 RUPT to be .
15b58 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 66 20 69 ** returned if i
15b59 74 20 64 6f 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 t does.. */.
15b5a 20 20 20 69 43 65 6c 6c 46 69 72 73 74 20 3d 20 iCellFirst =
15b5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 cellOffset + 2*p
15b5c 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 Page->nCell;.
15b5d 20 69 43 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 iCellLast = usa
15b5e 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 23 69 66 bleSize - 4;.#if
15b5f 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
15b60 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f ENABLE_OVERSIZE_
15b61 43 45 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 20 20 CELL_CHECK).
15b62 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 {. int i;
15b63 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
15b64 65 78 20 69 6e 74 6f 20 74 68 65 20 63 65 6c 6c ex into the cell
15b65 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 2a pointer array *
15b66 2f 0a 20 20 20 20 20 20 69 6e 74 20 73 7a 3b 20 /. int sz;
15b67 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
15b68 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a 0a e of a cell */..
15b69 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 if( !pPage
15b6a 2d 3e 6c 65 61 66 20 29 20 69 43 65 6c 6c 4c 61 ->leaf ) iCellLa
15b6b 73 74 2d 2d 3b 0a 20 20 20 20 20 20 66 6f 72 28 st--;. for(
15b6c 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 i=0; i<pPage->nC
15b6d 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 ell; i++){.
15b6e 20 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 pc = get2byte
15b6f 28 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 (&data[cellOffse
15b70 74 2b 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20 20 t+i*2]);.
15b71 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69 testcase( pc==i
15b72 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 CellFirst );.
15b73 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 testcase( p
15b74 63 3d 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a c==iCellLast );.
15b75 20 20 20 20 20 20 20 20 69 66 28 20 70 63 3c 69 if( pc<i
15b76 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e CellFirst || pc>
15b77 69 43 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 iCellLast ){.
15b78 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
15b79 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
15b7a 54 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 T;. }.
15b7b 20 20 20 20 20 73 7a 20 3d 20 63 65 6c 6c 53 69 sz = cellSi
15b7c 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26 64 61 zePtr(pPage, &da
15b7d 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20 20 20 ta[pc]);.
15b7e 20 74 65 73 74 63 61 73 65 28 20 70 63 2b 73 7a testcase( pc+sz
15b7f 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a ==usableSize );.
15b80 20 20 20 20 20 20 20 20 69 66 28 20 70 63 2b 73 if( pc+s
15b81 7a 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a z>usableSize ){.
15b82 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
15b83 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
15b84 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 7d 0a BKPT;. }.
15b85 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
15b86 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 ( !pPage->leaf )
15b87 20 69 43 65 6c 6c 4c 61 73 74 2b 2b 3b 0a 20 20 iCellLast++;.
15b88 20 20 7d 20 20 0a 23 65 6e 64 69 66 0a 0a 20 20 } .#endif..
15b89 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 /* Compute the
15b8a 20 74 6f 74 61 6c 20 66 72 65 65 20 73 70 61 63 total free spac
15b8b 65 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f e on the page */
15b8c 0a 20 20 20 20 70 63 20 3d 20 67 65 74 32 62 79 . pc = get2by
15b8d 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 te(&data[hdr+1])
15b8e 3b 0a 20 20 20 20 6e 46 72 65 65 20 3d 20 64 61 ;. nFree = da
15b8f 74 61 5b 68 64 72 2b 37 5d 20 2b 20 74 6f 70 3b ta[hdr+7] + top;
15b90 0a 20 20 20 20 77 68 69 6c 65 28 20 70 63 3e 30 . while( pc>0
15b91 20 29 7b 0a 20 20 20 20 20 20 75 31 36 20 6e 65 ){. u16 ne
15b92 78 74 2c 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 xt, size;.
15b93 69 66 28 20 70 63 3c 69 43 65 6c 6c 46 69 72 73 if( pc<iCellFirs
15b94 74 20 7c 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 73 t || pc>iCellLas
15b95 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 t ){. /*
15b96 53 74 61 72 74 20 6f 66 20 66 72 65 65 20 62 6c Start of free bl
15b97 6f 63 6b 20 69 73 20 6f 66 66 20 74 68 65 20 70 ock is off the p
15b98 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 age */. r
15b99 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
15b9a 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 RUPT_BKPT; .
15b9b 20 20 7d 0a 20 20 20 20 20 20 6e 65 78 74 20 3d }. next =
15b9c 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
15b9d 70 63 5d 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 pc]);. size
15b9e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
15b9f 61 5b 70 63 2b 32 5d 29 3b 0a 20 20 20 20 20 20 a[pc+2]);.
15ba0 69 66 28 20 28 6e 65 78 74 3e 30 20 26 26 20 6e if( (next>0 && n
15ba1 65 78 74 3c 3d 70 63 2b 73 69 7a 65 2b 33 29 20 ext<=pc+size+3)
15ba2 7c 7c 20 70 63 2b 73 69 7a 65 3e 75 73 61 62 6c || pc+size>usabl
15ba3 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 eSize ){.
15ba4 20 2f 2a 20 46 72 65 65 20 62 6c 6f 63 6b 73 20 /* Free blocks
15ba5 6d 75 73 74 20 62 65 20 69 6e 20 61 73 63 65 6e must be in ascen
15ba6 64 69 6e 67 20 6f 72 64 65 72 2e 20 41 6e 64 20 ding order. And
15ba7 74 68 65 20 6c 61 73 74 20 62 79 74 65 20 6f 66 the last byte of
15ba8 0a 09 2a 2a 20 74 68 65 20 66 72 65 65 2d 62 6c ..** the free-bl
15ba9 6f 63 6b 20 6d 75 73 74 20 6c 69 65 20 6f 6e 20 ock must lie on
15baa 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 the database pag
15bab 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 e. */. r
15bac 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
15bad 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 RUPT_BKPT; .
15bae 20 20 7d 0a 20 20 20 20 20 20 6e 46 72 65 65 20 }. nFree
15baf 3d 20 6e 46 72 65 65 20 2b 20 73 69 7a 65 3b 0a = nFree + size;.
15bb0 20 20 20 20 20 20 70 63 20 3d 20 6e 65 78 74 3b pc = next;
15bb1 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 . }.. /* A
15bb2 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 6e 46 t this point, nF
15bb3 72 65 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ree contains the
15bb4 20 73 75 6d 20 6f 66 20 74 68 65 20 6f 66 66 73 sum of the offs
15bb5 65 74 20 74 6f 20 74 68 65 20 73 74 61 72 74 0a et to the start.
15bb6 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 65 ** of the ce
15bb7 6c 6c 2d 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 ll-content area
15bb8 70 6c 75 73 20 74 68 65 20 6e 75 6d 62 65 72 20 plus the number
15bb9 6f 66 20 66 72 65 65 20 62 79 74 65 73 20 77 69 of free bytes wi
15bba 74 68 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 thin. ** the
15bbb 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 20 61 72 65 cell-content are
15bbc 61 2e 20 49 66 20 74 68 69 73 20 69 73 20 67 72 a. If this is gr
15bbd 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 75 eater than the u
15bbe 73 61 62 6c 65 2d 73 69 7a 65 0a 20 20 20 20 2a sable-size. *
15bbf 2a 20 6f 66 20 74 68 65 20 70 61 67 65 2c 20 74 * of the page, t
15bc0 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d 75 73 hen the page mus
15bc1 74 20 62 65 20 63 6f 72 72 75 70 74 65 64 2e 20 t be corrupted.
15bc2 54 68 69 73 20 63 68 65 63 6b 20 61 6c 73 6f 0a This check also.
15bc3 20 20 20 20 2a 2a 20 73 65 72 76 65 73 20 74 6f ** serves to
15bc4 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68 65 verify that the
15bc5 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 73 offset to the s
15bc6 74 61 72 74 20 6f 66 20 74 68 65 20 63 65 6c 6c tart of the cell
15bc7 2d 63 6f 6e 74 65 6e 74 0a 20 20 20 20 2a 2a 20 -content. **
15bc8 61 72 65 61 2c 20 61 63 63 6f 72 64 69 6e 67 20 area, according
15bc9 74 6f 20 74 68 65 20 70 61 67 65 20 68 65 61 64 to the page head
15bca 65 72 2c 20 6c 69 65 73 20 77 69 74 68 69 6e 20 er, lies within
15bcb 74 68 65 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f the page.. */
15bcc 0a 20 20 20 20 69 66 28 20 6e 46 72 65 65 3e 75 . if( nFree>u
15bcd 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 sableSize ){.
15bce 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
15bcf 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a _CORRUPT_BKPT; .
15bd0 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 2d }. pPage-
15bd1 3e 6e 46 72 65 65 20 3d 20 28 75 31 36 29 28 6e >nFree = (u16)(n
15bd2 46 72 65 65 20 2d 20 69 43 65 6c 6c 46 69 72 73 Free - iCellFirs
15bd3 74 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 t);. pPage->i
15bd4 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 20 sInit = 1;. }.
15bd5 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
15bd6 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 K;.}../*.** Set
15bd7 75 70 20 61 20 72 61 77 20 70 61 67 65 20 73 6f up a raw page so
15bd8 20 74 68 61 74 20 69 74 20 6c 6f 6f 6b 73 20 6c that it looks l
15bd9 69 6b 65 20 61 20 64 61 74 61 62 61 73 65 20 70 ike a database p
15bda 61 67 65 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6e age holding.** n
15bdb 6f 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 o entries..*/.st
15bdc 61 74 69 63 20 76 6f 69 64 20 7a 65 72 6f 50 61 atic void zeroPa
15bdd 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ge(MemPage *pPag
15bde 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 e, int flags){.
15bdf 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
15be0 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 data = pPage->aD
15be1 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65 64 20 ata;. BtShared
15be2 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 *pBt = pPage->pB
15be3 74 3b 0a 20 20 75 38 20 68 64 72 20 3d 20 70 50 t;. u8 hdr = pP
15be4 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a age->hdrOffset;.
15be5 20 20 75 31 36 20 66 69 72 73 74 3b 0a 0a 20 20 u16 first;..
15be6 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
15be7 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 28 70 agerPagenumber(p
15be8 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3d 3d Page->pDbPage)==
15be9 70 50 61 67 65 2d 3e 70 67 6e 6f 20 29 3b 0a 20 pPage->pgno );.
15bea 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15beb 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 50 PagerGetExtra(pP
15bec 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d age->pDbPage) ==
15bed 20 28 76 6f 69 64 2a 29 70 50 61 67 65 20 29 3b (void*)pPage );
15bee 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
15bef 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 e3PagerGetData(p
15bf0 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 3d Page->pDbPage) =
15bf1 3d 20 64 61 74 61 20 29 3b 0a 20 20 61 73 73 65 = data );. asse
15bf2 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
15bf3 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
15bf4 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
15bf5 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15bf6 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
15bf7 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a 6d >mutex) );. /*m
15bf8 65 6d 73 65 74 28 26 64 61 74 61 5b 68 64 72 5d emset(&data[hdr]
15bf9 2c 20 30 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 , 0, pBt->usable
15bfa 53 69 7a 65 20 2d 20 68 64 72 29 3b 2a 2f 0a 20 Size - hdr);*/.
15bfb 20 64 61 74 61 5b 68 64 72 5d 20 3d 20 28 63 68 data[hdr] = (ch
15bfc 61 72 29 66 6c 61 67 73 3b 0a 20 20 66 69 72 73 ar)flags;. firs
15bfd 74 20 3d 20 68 64 72 20 2b 20 38 20 2b 20 34 2a t = hdr + 8 + 4*
15bfe 28 28 66 6c 61 67 73 26 50 54 46 5f 4c 45 41 46 ((flags&PTF_LEAF
15bff 29 3d 3d 30 20 3f 31 3a 30 29 3b 0a 20 20 6d 65 )==0 ?1:0);. me
15c00 6d 73 65 74 28 26 64 61 74 61 5b 68 64 72 2b 31 mset(&data[hdr+1
15c01 5d 2c 20 30 2c 20 34 29 3b 0a 20 20 64 61 74 61 ], 0, 4);. data
15c02 5b 68 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 70 [hdr+7] = 0;. p
15c03 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 ut2byte(&data[hd
15c04 72 2b 35 5d 2c 20 70 42 74 2d 3e 75 73 61 62 6c r+5], pBt->usabl
15c05 65 53 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 2d eSize);. pPage-
15c06 3e 6e 46 72 65 65 20 3d 20 70 42 74 2d 3e 75 73 >nFree = pBt->us
15c07 61 62 6c 65 53 69 7a 65 20 2d 20 66 69 72 73 74 ableSize - first
15c08 3b 0a 20 20 64 65 63 6f 64 65 46 6c 61 67 73 28 ;. decodeFlags(
15c09 70 50 61 67 65 2c 20 66 6c 61 67 73 29 3b 0a 20 pPage, flags);.
15c0a 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 pPage->hdrOffse
15c0b 74 20 3d 20 68 64 72 3b 0a 20 20 70 50 61 67 65 t = hdr;. pPage
15c0c 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 66 ->cellOffset = f
15c0d 69 72 73 74 3b 0a 20 20 70 50 61 67 65 2d 3e 6e irst;. pPage->n
15c0e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 Overflow = 0;.
15c0f 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 61 67 assert( pBt->pag
15c10 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 eSize>=512 && pB
15c11 74 2d 3e 70 61 67 65 53 69 7a 65 3c 3d 33 32 37 t->pageSize<=327
15c12 36 38 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6d 68 );. pPage->m
15c13 61 73 6b 50 61 67 65 20 3d 20 70 42 74 2d 3e 70 askPage = pBt->p
15c14 61 67 65 53 69 7a 65 20 2d 20 31 3b 0a 20 20 70 ageSize - 1;. p
15c15 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 30 3b Page->nCell = 0;
15c16 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 . pPage->isInit
15c17 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 = 1;.}.../*.**
15c18 43 6f 6e 76 65 72 74 20 61 20 44 62 50 61 67 65 Convert a DbPage
15c19 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 74 obtained from t
15c1a 68 65 20 70 61 67 65 72 20 69 6e 74 6f 20 61 20 he pager into a
15c1b 4d 65 6d 50 61 67 65 20 75 73 65 64 20 62 79 0a MemPage used by.
15c1c 2a 2a 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 ** the btree lay
15c1d 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 er..*/.static Me
15c1e 6d 50 61 67 65 20 2a 62 74 72 65 65 50 61 67 65 mPage *btreePage
15c1f 46 72 6f 6d 44 62 50 61 67 65 28 44 62 50 61 67 FromDbPage(DbPag
15c20 65 20 2a 70 44 62 50 61 67 65 2c 20 50 67 6e 6f e *pDbPage, Pgno
15c21 20 70 67 6e 6f 2c 20 42 74 53 68 61 72 65 64 20 pgno, BtShared
15c22 2a 70 42 74 29 7b 0a 20 20 4d 65 6d 50 61 67 65 *pBt){. MemPage
15c23 20 2a 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 *pPage = (MemPa
15c24 67 65 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 ge*)sqlite3Pager
15c25 47 65 74 45 78 74 72 61 28 70 44 62 50 61 67 65 GetExtra(pDbPage
15c26 29 3b 0a 20 20 70 50 61 67 65 2d 3e 61 44 61 74 );. pPage->aDat
15c27 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 a = sqlite3Pager
15c28 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29 GetData(pDbPage)
15c29 3b 0a 20 20 70 50 61 67 65 2d 3e 70 44 62 50 61 ;. pPage->pDbPa
15c2a 67 65 20 3d 20 70 44 62 50 61 67 65 3b 0a 20 20 ge = pDbPage;.
15c2b 70 50 61 67 65 2d 3e 70 42 74 20 3d 20 70 42 74 pPage->pBt = pBt
15c2c 3b 0a 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 ;. pPage->pgno
15c2d 3d 20 70 67 6e 6f 3b 0a 20 20 70 50 61 67 65 2d = pgno;. pPage-
15c2e 3e 68 64 72 4f 66 66 73 65 74 20 3d 20 70 50 61 >hdrOffset = pPa
15c2f 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31 30 ge->pgno==1 ? 10
15c30 30 20 3a 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 0 : 0;. return
15c31 70 50 61 67 65 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a pPage; .}../*.**
15c32 20 47 65 74 20 61 20 70 61 67 65 20 66 72 6f 6d Get a page from
15c33 20 74 68 65 20 70 61 67 65 72 2e 20 20 49 6e 69 the pager. Ini
15c34 74 69 61 6c 69 7a 65 20 74 68 65 20 4d 65 6d 50 tialize the MemP
15c35 61 67 65 2e 70 42 74 20 61 6e 64 0a 2a 2a 20 4d age.pBt and.** M
15c36 65 6d 50 61 67 65 2e 61 44 61 74 61 20 65 6c 65 emPage.aData ele
15c37 6d 65 6e 74 73 20 69 66 20 6e 65 65 64 65 64 2e ments if needed.
15c38 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 6f .**.** If the no
15c39 43 6f 6e 74 65 6e 74 20 66 6c 61 67 20 69 73 20 Content flag is
15c3a 73 65 74 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 set, it means th
15c3b 61 74 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 at we do not car
15c3c 65 20 61 62 6f 75 74 0a 2a 2a 20 74 68 65 20 63 e about.** the c
15c3d 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 ontent of the pa
15c3e 67 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e ge at this time.
15c3f 20 20 53 6f 20 64 6f 20 6e 6f 74 20 67 6f 20 74 So do not go t
15c40 6f 20 74 68 65 20 64 69 73 6b 0a 2a 2a 20 74 6f o the disk.** to
15c41 20 66 65 74 63 68 20 74 68 65 20 63 6f 6e 74 65 fetch the conte
15c42 6e 74 2e 20 20 4a 75 73 74 20 66 69 6c 6c 20 69 nt. Just fill i
15c43 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 n the content wi
15c44 74 68 20 7a 65 72 6f 73 20 66 6f 72 20 6e 6f 77 th zeros for now
15c45 2e 0a 2a 2a 20 49 66 20 69 6e 20 74 68 65 20 66 ..** If in the f
15c46 75 74 75 72 65 20 77 65 20 63 61 6c 6c 20 73 71 uture we call sq
15c47 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
15c48 29 20 6f 6e 20 74 68 69 73 20 70 61 67 65 2c 20 ) on this page,
15c49 74 68 61 74 0a 2a 2a 20 6d 65 61 6e 73 20 77 65 that.** means we
15c4a 20 68 61 76 65 20 73 74 61 72 74 65 64 20 74 6f have started to
15c4b 20 62 65 20 63 6f 6e 63 65 72 6e 65 64 20 61 62 be concerned ab
15c4c 6f 75 74 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 out content and
15c4d 74 68 65 20 64 69 73 6b 0a 2a 2a 20 72 65 61 64 the disk.** read
15c4e 20 73 68 6f 75 6c 64 20 6f 63 63 75 72 20 61 74 should occur at
15c4f 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2f 0a that point..*/.
15c50 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 static int btree
15c51 47 65 74 50 61 67 65 28 0a 20 20 42 74 53 68 61 GetPage(. BtSha
15c52 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 red *pBt,
15c53 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a /* The btree */.
15c54 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 Pgno pgno,
15c55 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
15c56 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f 20 of the page to
15c57 66 65 74 63 68 20 2a 2f 0a 20 20 4d 65 6d 50 61 fetch */. MemPa
15c58 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 ge **ppPage,
15c59 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 /* Return the pa
15c5a 67 65 20 69 6e 20 74 68 69 73 20 70 61 72 61 6d ge in this param
15c5b 65 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f eter */. int no
15c5c 43 6f 6e 74 65 6e 74 20 20 20 20 20 20 20 20 2f Content /
15c5d 2a 20 44 6f 20 6e 6f 74 20 6c 6f 61 64 20 70 61 * Do not load pa
15c5e 67 65 20 63 6f 6e 74 65 6e 74 20 69 66 20 74 72 ge content if tr
15c5f 75 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 ue */.){. int r
15c60 63 3b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 c;. DbPage *pDb
15c61 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 Page;.. assert(
15c62 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15c63 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
15c64 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
15c65 33 50 61 67 65 72 41 63 71 75 69 72 65 28 70 42 3PagerAcquire(pB
15c66 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f 2c t->pPager, pgno,
15c67 20 28 44 62 50 61 67 65 2a 2a 29 26 70 44 62 50 (DbPage**)&pDbP
15c68 61 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b age, noContent);
15c69 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 . if( rc ) retu
15c6a 72 6e 20 72 63 3b 0a 20 20 2a 70 70 50 61 67 65 rn rc;. *ppPage
15c6b 20 3d 20 62 74 72 65 65 50 61 67 65 46 72 6f 6d = btreePageFrom
15c6c 44 62 50 61 67 65 28 70 44 62 50 61 67 65 2c 20 DbPage(pDbPage,
15c6d 70 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 72 65 pgno, pBt);. re
15c6e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
15c6f 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 72 69 65 76 }../*.** Retriev
15c70 65 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 e a page from th
15c71 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 49 e pager cache. I
15c72 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 f the requested
15c73 70 61 67 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 page is not.** a
15c74 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 70 61 lready in the pa
15c75 67 65 72 20 63 61 63 68 65 20 72 65 74 75 72 6e ger cache return
15c76 20 4e 55 4c 4c 2e 20 49 6e 69 74 69 61 6c 69 7a NULL. Initializ
15c77 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 e the MemPage.pB
15c78 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 t and.** MemPage
15c79 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 .aData elements
15c7a 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 if needed..*/.st
15c7b 61 74 69 63 20 4d 65 6d 50 61 67 65 20 2a 62 74 atic MemPage *bt
15c7c 72 65 65 50 61 67 65 4c 6f 6f 6b 75 70 28 42 74 reePageLookup(Bt
15c7d 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e Shared *pBt, Pgn
15c7e 6f 20 70 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67 o pgno){. DbPag
15c7f 65 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 61 73 e *pDbPage;. as
15c80 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
15c81 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
15c82 74 65 78 29 20 29 3b 0a 20 20 70 44 62 50 61 67 tex) );. pDbPag
15c83 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 e = sqlite3Pager
15c84 4c 6f 6f 6b 75 70 28 70 42 74 2d 3e 70 50 61 67 Lookup(pBt->pPag
15c85 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 er, pgno);. if(
15c86 20 70 44 62 50 61 67 65 20 29 7b 0a 20 20 20 20 pDbPage ){.
15c87 72 65 74 75 72 6e 20 62 74 72 65 65 50 61 67 65 return btreePage
15c88 46 72 6f 6d 44 62 50 61 67 65 28 70 44 62 50 61 FromDbPage(pDbPa
15c89 67 65 2c 20 70 67 6e 6f 2c 20 70 42 74 29 3b 0a ge, pgno, pBt);.
15c8a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
15c8b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
15c8c 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
15c8d 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e database file in
15c8e 20 70 61 67 65 73 2e 20 49 66 20 74 68 65 72 65 pages. If there
15c8f 20 69 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 0a is any kind of.
15c90 2a 2a 20 65 72 72 6f 72 2c 20 72 65 74 75 72 6e ** error, return
15c91 20 28 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 ((unsigned int)
15c92 2d 31 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 -1)..*/.static P
15c93 67 6e 6f 20 70 61 67 65 72 50 61 67 65 63 6f 75 gno pagerPagecou
15c94 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 nt(BtShared *pBt
15c95 29 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65 20 3d ){. int nPage =
15c96 20 2d 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 -1;. int rc;.
15c97 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 assert( pBt->pP
15c98 61 67 65 31 20 29 3b 0a 20 20 72 63 20 3d 20 73 age1 );. rc = s
15c99 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 qlite3PagerPagec
15c9a 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 ount(pBt->pPager
15c9b 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 61 73 73 , &nPage);. ass
15c9c 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
15c9d 4f 4b 20 7c 7c 20 6e 50 61 67 65 3d 3d 2d 31 20 OK || nPage==-1
15c9e 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 50 67 6e );. return (Pgn
15c9f 6f 29 6e 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a o)nPage;.}../*.*
15ca0 2a 20 47 65 74 20 61 20 70 61 67 65 20 66 72 6f * Get a page fro
15ca1 6d 20 74 68 65 20 70 61 67 65 72 20 61 6e 64 20 m the pager and
15ca2 69 6e 69 74 69 61 6c 69 7a 65 20 69 74 2e 20 20 initialize it.
15ca3 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
15ca4 6a 75 73 74 20 61 0a 2a 2a 20 63 6f 6e 76 65 6e just a.** conven
15ca5 69 65 6e 63 65 20 77 72 61 70 70 65 72 20 61 72 ience wrapper ar
15ca6 6f 75 6e 64 20 73 65 70 61 72 61 74 65 20 63 61 ound separate ca
15ca7 6c 6c 73 20 74 6f 20 62 74 72 65 65 47 65 74 50 lls to btreeGetP
15ca8 61 67 65 28 29 20 61 6e 64 20 0a 2a 2a 20 62 74 age() and .** bt
15ca9 72 65 65 49 6e 69 74 50 61 67 65 28 29 2e 0a 2a reeInitPage()..*
15caa 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 *.** If an error
15cab 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 74 68 occurs, then th
15cac 65 20 76 61 6c 75 65 20 2a 70 70 50 61 67 65 20 e value *ppPage
15cad 69 73 20 73 65 74 20 74 6f 20 69 73 20 75 6e 64 is set to is und
15cae 65 66 69 6e 65 64 2e 20 49 74 0a 2a 2a 20 6d 61 efined. It.** ma
15caf 79 20 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 y remain unchang
15cb0 65 64 2c 20 6f 72 20 69 74 20 6d 61 79 20 62 65 ed, or it may be
15cb1 20 73 65 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c set to an inval
15cb2 69 64 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 id value..*/.sta
15cb3 74 69 63 20 69 6e 74 20 67 65 74 41 6e 64 49 6e tic int getAndIn
15cb4 69 74 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 itPage(. BtShar
15cb5 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 ed *pBt,
15cb6 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 /* The databas
15cb7 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f e file */. Pgno
15cb8 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 pgno,
15cb9 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 /* Number of th
15cba 65 20 70 61 67 65 20 74 6f 20 67 65 74 20 2a 2f e page to get */
15cbb 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 . MemPage **ppP
15cbc 61 67 65 20 20 20 20 20 2f 2a 20 57 72 69 74 65 age /* Write
15cbd 20 74 68 65 20 70 61 67 65 20 70 6f 69 6e 74 65 the page pointe
15cbe 72 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 r here */.){. i
15cbf 6e 74 20 72 63 3b 0a 20 20 54 45 53 54 4f 4e 4c nt rc;. TESTONL
15cc0 59 28 20 50 67 6e 6f 20 69 4c 61 73 74 50 67 20 Y( Pgno iLastPg
15cc1 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 = pagerPagecount
15cc2 28 70 42 74 29 3b 20 29 0a 20 20 61 73 73 65 72 (pBt); ). asser
15cc3 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
15cc4 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
15cc5 29 20 29 3b 0a 0a 20 20 72 63 20 3d 20 62 74 72 ) );.. rc = btr
15cc6 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 70 eeGetPage(pBt, p
15cc7 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20 30 29 3b gno, ppPage, 0);
15cc8 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
15cc9 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d E_OK ){. rc =
15cca 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 2a btreeInitPage(*
15ccb 70 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 ppPage);. if(
15ccc 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
15ccd 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 {. releaseP
15cce 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 age(*ppPage);.
15ccf 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 }. }.. /* If
15cd0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 the requested p
15cd1 61 67 65 20 6e 75 6d 62 65 72 20 77 61 73 20 65 age number was e
15cd2 69 74 68 65 72 20 30 20 6f 72 20 67 72 65 61 74 ither 0 or great
15cd3 65 72 20 74 68 61 6e 20 74 68 65 20 70 61 67 65 er than the page
15cd4 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 . ** number of
15cd5 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 69 6e the last page in
15cd6 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74 the database, t
15cd7 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f his function sho
15cd8 75 6c 64 20 72 65 74 75 72 6e 0a 20 20 2a 2a 20 uld return. **
15cd9 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 6f SQLITE_CORRUPT o
15cda 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 r some other err
15cdb 6f 72 20 28 69 2e 65 2e 20 53 51 4c 49 54 45 5f or (i.e. SQLITE_
15cdc 46 55 4c 4c 29 2e 20 43 68 65 63 6b 20 74 68 61 FULL). Check tha
15cdd 74 20 74 68 69 73 0a 20 20 2a 2a 20 69 73 20 74 t this. ** is t
15cde 68 65 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 61 he case. */. a
15cdf 73 73 65 72 74 28 20 28 70 67 6e 6f 3e 30 20 26 ssert( (pgno>0 &
15ce0 26 20 70 67 6e 6f 3c 3d 69 4c 61 73 74 50 67 29 & pgno<=iLastPg)
15ce1 20 7c 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f || rc!=SQLITE_O
15ce2 4b 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 K );. testcase(
15ce3 20 70 67 6e 6f 3d 3d 30 20 29 3b 0a 20 20 74 65 pgno==0 );. te
15ce4 73 74 63 61 73 65 28 20 70 67 6e 6f 3d 3d 69 4c stcase( pgno==iL
15ce5 61 73 74 50 67 20 29 3b 0a 0a 20 20 72 65 74 75 astPg );.. retu
15ce6 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
15ce7 52 65 6c 65 61 73 65 20 61 20 4d 65 6d 50 61 67 Release a MemPag
15ce8 65 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 e. This should
15ce9 62 65 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66 be called once f
15cea 6f 72 20 65 61 63 68 20 70 72 69 6f 72 0a 2a 2a or each prior.**
15ceb 20 63 61 6c 6c 20 74 6f 20 62 74 72 65 65 47 65 call to btreeGe
15cec 74 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 tPage..*/.static
15ced 20 76 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67 void releasePag
15cee 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 e(MemPage *pPage
15cef 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 20 29 ){. if( pPage )
15cf0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 {. assert( pP
15cf1 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d age->nOverflow==
15cf2 30 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 0 || sqlite3Page
15cf3 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 50 rPageRefcount(pP
15cf4 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3e 31 20 age->pDbPage)>1
15cf5 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
15cf6 50 61 67 65 2d 3e 61 44 61 74 61 20 29 3b 0a 20 Page->aData );.
15cf7 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
15cf8 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 ->pBt );. ass
15cf9 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
15cfa 72 47 65 74 45 78 74 72 61 28 70 50 61 67 65 2d rGetExtra(pPage-
15cfb 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f >pDbPage) == (vo
15cfc 69 64 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 20 id*)pPage );.
15cfd 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15cfe 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61 PagerGetData(pPa
15cff 67 65 2d 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 ge->pDbPage)==pP
15d00 61 67 65 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 age->aData );.
15d01 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
15d02 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
15d03 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
15d04 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 );. sqlite3Pa
15d05 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 2d 3e gerUnref(pPage->
15d06 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a pDbPage);. }.}.
15d07 0a 2f 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 61 20 ./*.** During a
15d08 72 6f 6c 6c 62 61 63 6b 2c 20 77 68 65 6e 20 74 rollback, when t
15d09 68 65 20 70 61 67 65 72 20 72 65 6c 6f 61 64 73 he pager reloads
15d0a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 information int
15d0b 6f 20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 73 o the cache.** s
15d0c 6f 20 74 68 61 74 20 74 68 65 20 63 61 63 68 65 o that the cache
15d0d 20 69 73 20 72 65 73 74 6f 72 65 64 20 74 6f 20 is restored to
15d0e 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 its original sta
15d0f 74 65 20 61 74 20 74 68 65 20 73 74 61 72 74 20 te at the start
15d10 6f 66 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 of.** the transa
15d11 63 74 69 6f 6e 2c 20 66 6f 72 20 65 61 63 68 20 ction, for each
15d12 70 61 67 65 20 72 65 73 74 6f 72 65 64 20 74 68 page restored th
15d13 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
15d14 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 lled..**.** This
15d15 20 72 6f 75 74 69 6e 65 20 6e 65 65 64 73 20 74 routine needs t
15d16 6f 20 72 65 73 65 74 20 74 68 65 20 65 78 74 72 o reset the extr
15d17 61 20 64 61 74 61 20 73 65 63 74 69 6f 6e 20 61 a data section a
15d18 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
15d19 0a 2a 2a 20 70 61 67 65 20 74 6f 20 61 67 72 65 .** page to agre
15d1a 65 20 77 69 74 68 20 74 68 65 20 72 65 73 74 6f e with the resto
15d1b 72 65 64 20 64 61 74 61 2e 0a 2a 2f 0a 73 74 61 red data..*/.sta
15d1c 74 69 63 20 76 6f 69 64 20 70 61 67 65 52 65 69 tic void pageRei
15d1d 6e 69 74 28 44 62 50 61 67 65 20 2a 70 44 61 74 nit(DbPage *pDat
15d1e 61 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 a){. MemPage *p
15d1f 50 61 67 65 3b 0a 20 20 70 50 61 67 65 20 3d 20 Page;. pPage =
15d20 28 4d 65 6d 50 61 67 65 20 2a 29 73 71 6c 69 74 (MemPage *)sqlit
15d21 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 e3PagerGetExtra(
15d22 70 44 61 74 61 29 3b 0a 20 20 61 73 73 65 72 74 pData);. assert
15d23 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 ( sqlite3PagerPa
15d24 67 65 52 65 66 63 6f 75 6e 74 28 70 44 61 74 61 geRefcount(pData
15d25 29 3e 30 20 29 3b 0a 20 20 69 66 28 20 70 50 61 )>0 );. if( pPa
15d26 67 65 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 ge->isInit ){.
15d27 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
15d28 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
15d29 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
15d2a 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 );. pPage->is
15d2b 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 69 66 Init = 0;. if
15d2c 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 ( sqlite3PagerPa
15d2d 67 65 52 65 66 63 6f 75 6e 74 28 70 44 61 74 61 geRefcount(pData
15d2e 29 3e 31 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 )>1 ){. /*
15d2f 70 50 61 67 65 20 6d 69 67 68 74 20 6e 6f 74 20 pPage might not
15d30 62 65 20 61 20 62 74 72 65 65 20 70 61 67 65 3b be a btree page;
15d31 20 20 69 74 20 6d 69 67 68 74 20 62 65 20 61 6e it might be an
15d32 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 0a 20 overflow page.
15d33 20 20 20 20 20 2a 2a 20 6f 72 20 70 74 72 6d 61 ** or ptrma
15d34 70 20 70 61 67 65 20 6f 72 20 61 20 66 72 65 65 p page or a free
15d35 20 70 61 67 65 2e 20 20 49 6e 20 74 68 6f 73 65 page. In those
15d36 20 63 61 73 65 73 2c 20 74 68 65 20 66 6f 6c 6c cases, the foll
15d37 6f 77 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 63 owing. ** c
15d38 61 6c 6c 20 74 6f 20 62 74 72 65 65 49 6e 69 74 all to btreeInit
15d39 50 61 67 65 28 29 20 77 69 6c 6c 20 6c 69 6b 65 Page() will like
15d3a 6c 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ly return SQLITE
15d3b 5f 43 4f 52 52 55 50 54 2e 0a 20 20 20 20 20 20 _CORRUPT..
15d3c 2a 2a 20 42 75 74 20 6e 6f 20 68 61 72 6d 20 69 ** But no harm i
15d3d 73 20 64 6f 6e 65 20 62 79 20 74 68 69 73 2e 20 s done by this.
15d3e 20 41 6e 64 20 69 74 20 69 73 20 76 65 72 79 20 And it is very
15d3f 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 0a 20 important that.
15d40 20 20 20 20 20 2a 2a 20 62 74 72 65 65 49 6e 69 ** btreeIni
15d41 74 50 61 67 65 28 29 20 62 65 20 63 61 6c 6c 65 tPage() be calle
15d42 64 20 6f 6e 20 65 76 65 72 79 20 62 74 72 65 65 d on every btree
15d43 20 70 61 67 65 20 73 6f 20 77 65 20 6d 61 6b 65 page so we make
15d44 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 61 . ** the ca
15d45 6c 6c 20 66 6f 72 20 65 76 65 72 79 20 70 61 67 ll for every pag
15d46 65 20 74 68 61 74 20 63 6f 6d 65 73 20 69 6e 20 e that comes in
15d47 66 6f 72 20 72 65 2d 69 6e 69 74 69 6e 67 2e 20 for re-initing.
15d48 2a 2f 0a 20 20 20 20 20 20 62 74 72 65 65 49 6e */. btreeIn
15d49 69 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 itPage(pPage);.
15d4a 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
15d4b 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 * Invoke the bus
15d4c 79 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 y handler for a
15d4d 62 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 btree..*/.static
15d4e 20 69 6e 74 20 62 74 72 65 65 49 6e 76 6f 6b 65 int btreeInvoke
15d4f 42 75 73 79 48 61 6e 64 6c 65 72 28 76 6f 69 64 BusyHandler(void
15d50 20 2a 70 41 72 67 29 7b 0a 20 20 42 74 53 68 61 *pArg){. BtSha
15d51 72 65 64 20 2a 70 42 74 20 3d 20 28 42 74 53 68 red *pBt = (BtSh
15d52 61 72 65 64 2a 29 70 41 72 67 3b 0a 20 20 61 73 ared*)pArg;. as
15d53 73 65 72 74 28 20 70 42 74 2d 3e 64 62 20 29 3b sert( pBt->db );
15d54 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
15d55 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
15d56 74 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b t->db->mutex) );
15d57 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
15d58 33 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 3InvokeBusyHandl
15d59 65 72 28 26 70 42 74 2d 3e 64 62 2d 3e 62 75 73 er(&pBt->db->bus
15d5a 79 48 61 6e 64 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a yHandler);.}../*
15d5b 0a 2a 2a 20 4f 70 65 6e 20 61 20 64 61 74 61 62 .** Open a datab
15d5c 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a ase file..** .**
15d5d 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 zFilename is th
15d5e 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 e name of the da
15d5f 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 tabase file. If
15d60 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 zFilename is NU
15d61 4c 4c 0a 2a 2a 20 61 20 6e 65 77 20 64 61 74 61 LL.** a new data
15d62 62 61 73 65 20 77 69 74 68 20 61 20 72 61 6e 64 base with a rand
15d63 6f 6d 20 6e 61 6d 65 20 69 73 20 63 72 65 61 74 om name is creat
15d64 65 64 2e 20 20 54 68 69 73 20 72 61 6e 64 6f 6d ed. This random
15d65 6c 79 20 6e 61 6d 65 64 0a 2a 2a 20 64 61 74 61 ly named.** data
15d66 62 61 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 base file will b
15d67 65 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 73 e deleted when s
15d68 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 qlite3BtreeClose
15d69 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a () is called..**
15d6a 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 If zFilename is
15d6b 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e ":memory:" then
15d6c 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
15d6d 74 61 62 61 73 65 20 69 73 20 63 72 65 61 74 65 tabase is create
15d6e 64 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 75 74 d.** that is aut
15d6f 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 73 74 72 omatically destr
15d70 6f 79 65 64 20 77 68 65 6e 20 69 74 20 69 73 20 oyed when it is
15d71 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 closed..**.** If
15d72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
15d73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 20 already opened
15d74 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 in the same data
15d75 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a base connection.
15d76 2a 2a 20 61 6e 64 20 77 65 20 61 72 65 20 69 6e ** and we are in
15d77 20 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f shared cache mo
15d78 64 65 2c 20 74 68 65 6e 20 74 68 65 20 6f 70 65 de, then the ope
15d79 6e 20 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 n will fail with
15d7a 20 61 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f an.** SQLITE_CO
15d7b 4e 53 54 52 41 49 4e 54 20 65 72 72 6f 72 2e 20 NSTRAINT error.
15d7c 20 57 65 20 63 61 6e 6e 6f 74 20 61 6c 6c 6f 77 We cannot allow
15d7d 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 42 74 53 two or more BtS
15d7e 68 61 72 65 64 0a 2a 2a 20 6f 62 6a 65 63 74 73 hared.** objects
15d7f 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 in the same dat
15d80 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
15d81 20 73 69 6e 63 65 20 64 6f 69 6e 67 20 73 6f 20 since doing so
15d82 77 69 6c 6c 20 6c 65 61 64 0a 2a 2a 20 74 6f 20 will lead.** to
15d83 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 6c 6f problems with lo
15d84 63 6b 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 cking..*/.SQLITE
15d85 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
15d86 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 0a 20 ite3BtreeOpen(.
15d87 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
15d88 6c 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 lename, /* Name
15d89 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e of the file con
15d8a 74 61 69 6e 69 6e 67 20 74 68 65 20 42 54 72 65 taining the BTre
15d8b 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 e database */.
15d8c 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 sqlite3 *db,
15d8d 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 6f 63 /* Assoc
15d8e 69 61 74 65 64 20 64 61 74 61 62 61 73 65 20 68 iated database h
15d8f 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65 andle */. Btree
15d90 20 2a 2a 70 70 42 74 72 65 65 2c 20 20 20 20 20 **ppBtree,
15d91 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
15d92 20 6e 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63 new Btree objec
15d93 74 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a t written here *
15d94 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 /. int flags,
15d95 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
15d96 70 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 ptions */. int
15d97 76 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 vfsFlags
15d98 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 /* Flags pas
15d99 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73 sed through to s
15d9a 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e qlite3_vfs.xOpen
15d9b 28 29 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 () */.){. sqlit
15d9c 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20 e3_vfs *pVfs;
15d9d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
15d9e 20 56 46 53 20 74 6f 20 75 73 65 20 66 6f 72 20 VFS to use for
15d9f 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 this btree */.
15da0 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
15da1 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0; /
15da2 2a 20 53 68 61 72 65 64 20 70 61 72 74 20 6f 66 * Shared part of
15da3 20 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65 btree structure
15da4 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 3b 20 */. Btree *p;
15da5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15da6 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74 /* Handle t
15da7 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 o return */. sq
15da8 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
15da9 65 78 4f 70 65 6e 20 3d 20 30 3b 20 20 2f 2a 20 exOpen = 0; /*
15daa 50 72 65 76 65 6e 74 73 20 61 20 72 61 63 65 20 Prevents a race
15dab 63 6f 6e 64 69 74 69 6f 6e 2e 20 54 69 63 6b 65 condition. Ticke
15dac 74 20 23 33 35 33 37 20 2a 2f 0a 20 20 69 6e 74 t #3537 */. int
15dad 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
15dae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
15daf 65 73 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 esult code from
15db0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f this function */
15db1 0a 20 20 75 38 20 6e 52 65 73 65 72 76 65 3b 20 . u8 nReserve;
15db2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15db3 20 20 2f 2a 20 42 79 74 65 20 6f 66 20 75 6e 75 /* Byte of unu
15db4 73 65 64 20 73 70 61 63 65 20 6f 6e 20 65 61 63 sed space on eac
15db5 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 h page */. unsi
15db6 67 6e 65 64 20 63 68 61 72 20 7a 44 62 48 65 61 gned char zDbHea
15db7 64 65 72 5b 31 30 30 5d 3b 20 20 2f 2a 20 44 61 der[100]; /* Da
15db8 74 61 62 61 73 65 20 68 65 61 64 65 72 20 63 6f tabase header co
15db9 6e 74 65 6e 74 20 2a 2f 0a 0a 20 20 2f 2a 20 53 ntent */.. /* S
15dba 65 74 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 et the variable
15dbb 69 73 4d 65 6d 64 62 20 74 6f 20 74 72 75 65 20 isMemdb to true
15dbc 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 for an in-memory
15dbd 20 64 61 74 61 62 61 73 65 2c 20 6f 72 20 0a 20 database, or .
15dbe 20 2a 2a 20 66 61 6c 73 65 20 66 6f 72 20 61 20 ** false for a
15dbf 66 69 6c 65 2d 62 61 73 65 64 20 64 61 74 61 62 file-based datab
15dc0 61 73 65 2e 20 54 68 69 73 20 73 79 6d 62 6f 6c ase. This symbol
15dc1 20 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 is only require
15dc2 64 20 69 66 0a 20 20 2a 2a 20 65 69 74 68 65 72 d if. ** either
15dc3 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 2d 64 of the shared-d
15dc4 61 74 61 20 6f 72 20 61 75 74 6f 76 61 63 75 75 ata or autovacuu
15dc5 6d 20 66 65 61 74 75 72 65 73 20 61 72 65 20 63 m features are c
15dc6 6f 6d 70 69 6c 65 64 20 0a 20 20 2a 2a 20 69 6e ompiled . ** in
15dc7 74 6f 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a to the library..
15dc8 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 */.#if !define
15dc9 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 d(SQLITE_OMIT_SH
15dca 41 52 45 44 5f 43 41 43 48 45 29 20 7c 7c 20 21 ARED_CACHE) || !
15dcb 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
15dcc 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 29 0a MIT_AUTOVACUUM).
15dcd 20 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f #ifdef SQLITE_
15dce 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 OMIT_MEMORYDB.
15dcf 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 const int isMe
15dd0 6d 64 62 20 3d 20 30 3b 0a 20 20 23 65 6c 73 65 mdb = 0;. #else
15dd1 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 . const int i
15dd2 73 4d 65 6d 64 62 20 3d 20 7a 46 69 6c 65 6e 61 sMemdb = zFilena
15dd3 6d 65 20 26 26 20 21 73 74 72 63 6d 70 28 7a 46 me && !strcmp(zF
15dd4 69 6c 65 6e 61 6d 65 2c 20 22 3a 6d 65 6d 6f 72 ilename, ":memor
15dd5 79 3a 22 29 3b 0a 20 20 23 65 6e 64 69 66 0a 23 y:");. #endif.#
15dd6 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 endif.. assert(
15dd7 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 db!=0 );. asse
15dd8 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
15dd9 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 x_held(db->mutex
15dda 29 20 29 3b 0a 0a 20 20 70 56 66 73 20 3d 20 64 ) );.. pVfs = d
15ddb 62 2d 3e 70 56 66 73 3b 0a 20 20 70 20 3d 20 73 b->pVfs;. p = s
15ddc 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
15ddd 28 73 69 7a 65 6f 66 28 42 74 72 65 65 29 29 3b (sizeof(Btree));
15dde 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 . if( !p ){.
15ddf 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
15de0 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69 OMEM;. }. p->i
15de1 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e nTrans = TRANS_N
15de2 4f 4e 45 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 ONE;. p->db = d
15de3 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 b;.#ifndef SQLIT
15de4 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
15de5 43 48 45 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 70 42 CHE. p->lock.pB
15de6 74 72 65 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6c tree = p;. p->l
15de7 6f 63 6b 2e 69 54 61 62 6c 65 20 3d 20 31 3b 0a ock.iTable = 1;.
15de8 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66 #endif..#if !def
15de9 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
15dea 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 _SHARED_CACHE) &
15deb 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 & !defined(SQLIT
15dec 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 E_OMIT_DISKIO).
15ded 20 2f 2a 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 /*. ** If this
15dee 20 42 74 72 65 65 20 69 73 20 61 20 63 61 6e 64 Btree is a cand
15def 69 64 61 74 65 20 66 6f 72 20 73 68 61 72 65 64 idate for shared
15df0 20 63 61 63 68 65 2c 20 74 72 79 20 74 6f 20 66 cache, try to f
15df1 69 6e 64 20 61 6e 0a 20 20 2a 2a 20 65 78 69 73 ind an. ** exis
15df2 74 69 6e 67 20 42 74 53 68 61 72 65 64 20 6f 62 ting BtShared ob
15df3 6a 65 63 74 20 74 68 61 74 20 77 65 20 63 61 6e ject that we can
15df4 20 73 68 61 72 65 20 77 69 74 68 0a 20 20 2a 2f share with. */
15df5 0a 20 20 69 66 28 20 69 73 4d 65 6d 64 62 3d 3d . if( isMemdb==
15df6 30 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 20 26 0 && zFilename &
15df7 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 29 & zFilename[0] )
15df8 7b 0a 20 20 20 20 69 66 28 20 76 66 73 46 6c 61 {. if( vfsFla
15df9 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
15dfa 5f 53 48 41 52 45 44 43 41 43 48 45 20 29 7b 0a _SHAREDCACHE ){.
15dfb 20 20 20 20 20 20 69 6e 74 20 6e 46 75 6c 6c 50 int nFullP
15dfc 61 74 68 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e athname = pVfs->
15dfd 6d 78 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 mxPathname+1;.
15dfe 20 20 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 50 char *zFullP
15dff 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 athname = sqlite
15e00 33 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 50 61 74 3Malloc(nFullPat
15e01 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 73 71 hname);. sq
15e02 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
15e03 65 78 53 68 61 72 65 64 3b 0a 20 20 20 20 20 20 exShared;.
15e04 70 2d 3e 73 68 61 72 61 62 6c 65 20 3d 20 31 3b p->sharable = 1;
15e05 0a 20 20 20 20 20 20 69 66 28 20 21 7a 46 75 6c . if( !zFul
15e06 6c 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 lPathname ){.
15e07 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
15e08 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 e(p);. re
15e09 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
15e0a 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 M;. }.
15e0b 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 sqlite3OsFullPa
15e0c 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46 69 thname(pVfs, zFi
15e0d 6c 65 6e 61 6d 65 2c 20 6e 46 75 6c 6c 50 61 74 lename, nFullPat
15e0e 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 50 61 74 68 hname, zFullPath
15e0f 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 6d 75 74 name);. mut
15e10 65 78 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 exOpen = sqlite3
15e11 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
15e12 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4f E_MUTEX_STATIC_O
15e13 50 45 4e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 PEN);. sqli
15e14 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
15e15 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 20 20 mutexOpen);.
15e16 20 20 6d 75 74 65 78 53 68 61 72 65 64 20 3d 20 mutexShared =
15e17 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
15e18 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
15e19 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 TATIC_MASTER);.
15e1a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
15e1b 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 53 68 ex_enter(mutexSh
15e1c 61 72 65 64 29 3b 0a 20 20 20 20 20 20 66 6f 72 ared);. for
15e1d 28 70 42 74 3d 47 4c 4f 42 41 4c 28 42 74 53 68 (pBt=GLOBAL(BtSh
15e1e 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 ared*,sqlite3Sha
15e1f 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b 20 70 redCacheList); p
15e20 42 74 3b 20 70 42 74 3d 70 42 74 2d 3e 70 4e 65 Bt; pBt=pBt->pNe
15e21 78 74 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 xt){. ass
15e22 65 72 74 28 20 70 42 74 2d 3e 6e 52 65 66 3e 30 ert( pBt->nRef>0
15e23 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 );. if(
15e24 30 3d 3d 73 74 72 63 6d 70 28 7a 46 75 6c 6c 50 0==strcmp(zFullP
15e25 61 74 68 6e 61 6d 65 2c 20 73 71 6c 69 74 65 33 athname, sqlite3
15e26 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 70 42 PagerFilename(pB
15e27 74 2d 3e 70 50 61 67 65 72 29 29 0a 20 20 20 20 t->pPager)).
15e28 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 &&
15e29 73 71 6c 69 74 65 33 50 61 67 65 72 56 66 73 28 sqlite3PagerVfs(
15e2a 70 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d 70 56 pBt->pPager)==pV
15e2b 66 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 fs ){.
15e2c 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 20 20 int iDb;.
15e2d 20 20 20 66 6f 72 28 69 44 62 3d 64 62 2d 3e 6e for(iDb=db->n
15e2e 44 62 2d 31 3b 20 69 44 62 3e 3d 30 3b 20 69 44 Db-1; iDb>=0; iD
15e2f 62 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 b--){.
15e30 20 20 42 74 72 65 65 20 2a 70 45 78 69 73 74 69 Btree *pExisti
15e31 6e 67 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 ng = db->aDb[iDb
15e32 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 20 20 20 ].pBt;.
15e33 20 20 20 69 66 28 20 70 45 78 69 73 74 69 6e 67 if( pExisting
15e34 20 26 26 20 70 45 78 69 73 74 69 6e 67 2d 3e 70 && pExisting->p
15e35 42 74 3d 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 Bt==pBt ){.
15e36 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
15e37 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 _mutex_leave(mut
15e38 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 exShared);.
15e39 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
15e3a 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 _mutex_leave(mut
15e3b 65 78 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 20 exOpen);.
15e3c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 sqlite3_f
15e3d 72 65 65 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d ree(zFullPathnam
15e3e 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 e);.
15e3f 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
15e40 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 );.
15e41 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
15e42 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 20 ONSTRAINT;.
15e43 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
15e44 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 }. p
15e45 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 20 ->pBt = pBt;.
15e46 20 20 20 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 pBt->nRef
15e47 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 ++;. br
15e48 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 eak;. }.
15e49 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
15e4a 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
15e4b 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 (mutexShared);.
15e4c 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
15e4d 65 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 e(zFullPathname)
15e4e 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 ;. }.#ifdef S
15e4f 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 QLITE_DEBUG.
15e50 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 else{. /* I
15e51 6e 20 64 65 62 75 67 20 6d 6f 64 65 2c 20 77 65 n debug mode, we
15e52 20 6d 61 72 6b 20 61 6c 6c 20 70 65 72 73 69 73 mark all persis
15e53 74 65 6e 74 20 64 61 74 61 62 61 73 65 73 20 61 tent databases a
15e54 73 20 73 68 61 72 61 62 6c 65 0a 20 20 20 20 20 s sharable.
15e55 20 2a 2a 20 65 76 65 6e 20 77 68 65 6e 20 74 68 ** even when th
15e56 65 79 20 61 72 65 20 6e 6f 74 2e 20 20 54 68 69 ey are not. Thi
15e57 73 20 65 78 65 72 63 69 73 65 73 20 74 68 65 20 s exercises the
15e58 6c 6f 63 6b 69 6e 67 20 63 6f 64 65 20 61 6e 64 locking code and
15e59 0a 20 20 20 20 20 20 2a 2a 20 67 69 76 65 73 20 . ** gives
15e5a 6d 6f 72 65 20 6f 70 70 6f 72 74 75 6e 69 74 79 more opportunity
15e5b 20 66 6f 72 20 61 73 73 65 72 74 73 28 73 71 6c for asserts(sql
15e5c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
15e5d 29 29 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74 )). ** stat
15e5e 65 6d 65 6e 74 73 20 74 6f 20 66 69 6e 64 20 6c ements to find l
15e5f 6f 63 6b 69 6e 67 20 70 72 6f 62 6c 65 6d 73 2e ocking problems.
15e60 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
15e61 70 2d 3e 73 68 61 72 61 62 6c 65 20 3d 20 31 3b p->sharable = 1;
15e62 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
15e63 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 }.#endif. if( p
15e64 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 0a Bt==0 ){. /*.
15e65 20 20 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ** The follo
15e66 77 69 6e 67 20 61 73 73 65 72 74 73 20 6d 61 6b wing asserts mak
15e67 65 20 73 75 72 65 20 74 68 61 74 20 73 74 72 75 e sure that stru
15e68 63 74 75 72 65 73 20 75 73 65 64 20 62 79 20 74 ctures used by t
15e69 68 65 20 62 74 72 65 65 20 61 72 65 0a 20 20 20 he btree are.
15e6a 20 2a 2a 20 74 68 65 20 72 69 67 68 74 20 73 69 ** the right si
15e6b 7a 65 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 ze. This is to
15e6c 67 75 61 72 64 20 61 67 61 69 6e 73 74 20 73 69 guard against si
15e6d 7a 65 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 ze changes that
15e6e 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 77 68 result. ** wh
15e6f 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 6f 6e 20 en compiling on
15e70 61 20 64 69 66 66 65 72 65 6e 74 20 61 72 63 68 a different arch
15e71 69 74 65 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f itecture.. */
15e72 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a . assert( siz
15e73 65 6f 66 28 69 36 34 29 3d 3d 38 20 7c 7c 20 73 eof(i64)==8 || s
15e74 69 7a 65 6f 66 28 69 36 34 29 3d 3d 34 20 29 3b izeof(i64)==4 );
15e75 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a . assert( siz
15e76 65 6f 66 28 75 36 34 29 3d 3d 38 20 7c 7c 20 73 eof(u64)==8 || s
15e77 69 7a 65 6f 66 28 75 36 34 29 3d 3d 34 20 29 3b izeof(u64)==4 );
15e78 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a . assert( siz
15e79 65 6f 66 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 eof(u32)==4 );.
15e7a 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f assert( sizeo
15e7b 66 28 75 31 36 29 3d 3d 32 20 29 3b 0a 20 20 20 f(u16)==2 );.
15e7c 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 assert( sizeof(
15e7d 50 67 6e 6f 29 3d 3d 34 20 29 3b 0a 20 20 0a 20 Pgno)==4 );. .
15e7e 20 20 20 70 42 74 20 3d 20 73 71 6c 69 74 65 33 pBt = sqlite3
15e7f 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 MallocZero( size
15e80 6f 66 28 2a 70 42 74 29 20 29 3b 0a 20 20 20 20 of(*pBt) );.
15e81 69 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 if( pBt==0 ){.
15e82 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
15e83 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 NOMEM;. got
15e84 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 o btree_open_out
15e85 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
15e86 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 sqlite3PagerOpe
15e87 6e 28 70 56 66 73 2c 20 26 70 42 74 2d 3e 70 50 n(pVfs, &pBt->pP
15e88 61 67 65 72 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c ager, zFilename,
15e89 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
15e8a 20 20 20 20 20 20 20 20 20 20 20 45 58 54 52 41 EXTRA
15e8b 5f 53 49 5a 45 2c 20 66 6c 61 67 73 2c 20 76 66 _SIZE, flags, vf
15e8c 73 46 6c 61 67 73 2c 20 70 61 67 65 52 65 69 6e sFlags, pageRein
15e8d 69 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d it);. if( rc=
15e8e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
15e8f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
15e90 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 PagerReadFilehea
15e91 64 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72 2c der(pBt->pPager,
15e92 73 69 7a 65 6f 66 28 7a 44 62 48 65 61 64 65 72 sizeof(zDbHeader
15e93 29 2c 7a 44 62 48 65 61 64 65 72 29 3b 0a 20 20 ),zDbHeader);.
15e94 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d }. if( rc!=
15e95 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
15e96 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 goto btree_op
15e97 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 en_out;. }.
15e98 20 20 70 42 74 2d 3e 64 62 20 3d 20 64 62 3b 0a pBt->db = db;.
15e99 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
15e9a 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 70 SetBusyhandler(p
15e9b 42 74 2d 3e 70 50 61 67 65 72 2c 20 62 74 72 65 Bt->pPager, btre
15e9c 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c eInvokeBusyHandl
15e9d 65 72 2c 20 70 42 74 29 3b 0a 20 20 20 20 70 2d er, pBt);. p-
15e9e 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 0a 20 >pBt = pBt;. .
15e9f 20 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 pBt->pCursor
15ea0 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 = 0;. pBt->pP
15ea1 61 67 65 31 20 3d 20 30 3b 0a 20 20 20 20 70 42 age1 = 0;. pB
15ea2 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 73 71 t->readOnly = sq
15ea3 6c 69 74 65 33 50 61 67 65 72 49 73 72 65 61 64 lite3PagerIsread
15ea4 6f 6e 6c 79 28 70 42 74 2d 3e 70 50 61 67 65 72 only(pBt->pPager
15ea5 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65 );. pBt->page
15ea6 53 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 Size = get2byte(
15ea7 26 7a 44 62 48 65 61 64 65 72 5b 31 36 5d 29 3b &zDbHeader[16]);
15ea8 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 70 61 . if( pBt->pa
15ea9 67 65 53 69 7a 65 3c 35 31 32 20 7c 7c 20 70 42 geSize<512 || pB
15eaa 74 2d 3e 70 61 67 65 53 69 7a 65 3e 53 51 4c 49 t->pageSize>SQLI
15eab 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 TE_MAX_PAGE_SIZE
15eac 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 28 28 70 . || ((p
15ead 42 74 2d 3e 70 61 67 65 53 69 7a 65 2d 31 29 26 Bt->pageSize-1)&
15eae 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 21 3d pBt->pageSize)!=
15eaf 30 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 0 ){. pBt->
15eb0 70 61 67 65 53 69 7a 65 20 3d 20 30 3b 0a 23 69 pageSize = 0;.#i
15eb1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
15eb2 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
15eb3 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6d 61 67 /* If the mag
15eb4 69 63 20 6e 61 6d 65 20 22 3a 6d 65 6d 6f 72 79 ic name ":memory
15eb5 3a 22 20 77 69 6c 6c 20 63 72 65 61 74 65 20 61 :" will create a
15eb6 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
15eb7 62 61 73 65 2c 20 74 68 65 6e 0a 20 20 20 20 20 base, then.
15eb8 20 2a 2a 20 6c 65 61 76 65 20 74 68 65 20 61 75 ** leave the au
15eb9 74 6f 56 61 63 75 75 6d 20 6d 6f 64 65 20 61 74 toVacuum mode at
15eba 20 30 20 28 64 6f 20 6e 6f 74 20 61 75 74 6f 2d 0 (do not auto-
15ebb 76 61 63 75 75 6d 29 2c 20 65 76 65 6e 20 69 66 vacuum), even if
15ebc 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 . ** SQLITE
15ebd 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 _DEFAULT_AUTOVAC
15ebe 55 55 4d 20 69 73 20 74 72 75 65 2e 20 4f 6e 20 UUM is true. On
15ebf 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 the other hand,
15ec0 69 66 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 if. ** SQLI
15ec1 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 TE_OMIT_MEMORYDB
15ec2 20 68 61 73 20 62 65 65 6e 20 64 65 66 69 6e 65 has been define
15ec3 64 2c 20 74 68 65 6e 20 22 3a 6d 65 6d 6f 72 79 d, then ":memory
15ec4 3a 22 20 69 73 20 6a 75 73 74 20 61 0a 20 20 20 :" is just a.
15ec5 20 20 20 2a 2a 20 72 65 67 75 6c 61 72 20 66 69 ** regular fi
15ec6 6c 65 2d 6e 61 6d 65 2e 20 49 6e 20 74 68 69 73 le-name. In this
15ec7 20 63 61 73 65 20 74 68 65 20 61 75 74 6f 2d 76 case the auto-v
15ec8 61 63 75 75 6d 20 61 70 70 6c 69 65 73 20 61 73 acuum applies as
15ec9 20 70 65 72 20 6e 6f 72 6d 61 6c 2e 0a 20 20 20 per normal..
15eca 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
15ecb 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 21 69 73 zFilename && !is
15ecc 4d 65 6d 64 62 20 29 7b 0a 20 20 20 20 20 20 20 Memdb ){.
15ecd 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
15ece 20 3d 20 28 53 51 4c 49 54 45 5f 44 45 46 41 55 = (SQLITE_DEFAU
15ecf 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 LT_AUTOVACUUM ?
15ed0 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 20 20 1 : 0);.
15ed1 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 pBt->incrVacuum
15ed2 3d 20 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c = (SQLITE_DEFAUL
15ed3 54 5f 41 55 54 4f 56 41 43 55 55 4d 3d 3d 32 20 T_AUTOVACUUM==2
15ed4 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 ? 1 : 0);.
15ed5 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 6e }.#endif. n
15ed6 52 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 Reserve = 0;.
15ed7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 52 }else{. nR
15ed8 65 73 65 72 76 65 20 3d 20 7a 44 62 48 65 61 64 eserve = zDbHead
15ed9 65 72 5b 32 30 5d 3b 0a 20 20 20 20 20 20 70 42 er[20];. pB
15eda 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 t->pageSizeFixed
15edb 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 = 1;.#ifndef SQ
15edc 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
15edd 43 55 55 4d 0a 20 20 20 20 20 20 70 42 74 2d 3e CUUM. pBt->
15ede 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 autoVacuum = (ge
15edf 74 34 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 t4byte(&zDbHeade
15ee0 72 5b 33 36 20 2b 20 34 2a 34 5d 29 3f 31 3a 30 r[36 + 4*4])?1:0
15ee1 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e );. pBt->in
15ee2 63 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 crVacuum = (get4
15ee3 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b byte(&zDbHeader[
15ee4 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 36 + 7*4])?1:0);
15ee5 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 .#endif. }.
15ee6 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
15ee7 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 gerSetPagesize(p
15ee8 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 Bt->pPager, &pBt
15ee9 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6e 52 65 73 ->pageSize, nRes
15eea 65 72 76 65 29 3b 0a 20 20 20 20 69 66 28 20 72 erve);. if( r
15eeb 63 20 29 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f c ) goto btree_o
15eec 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 70 42 74 pen_out;. pBt
15eed 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 ->usableSize = p
15eee 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 6e Bt->pageSize - n
15eef 52 65 73 65 72 76 65 3b 0a 20 20 20 20 61 73 73 Reserve;. ass
15ef0 65 72 74 28 20 28 70 42 74 2d 3e 70 61 67 65 53 ert( (pBt->pageS
15ef1 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 20 20 ize & 7)==0 );
15ef2 2f 2a 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 6d /* 8-byte alignm
15ef3 65 6e 74 20 6f 66 20 70 61 67 65 53 69 7a 65 20 ent of pageSize
15ef4 2a 2f 0a 20 20 20 0a 23 69 66 20 21 64 65 66 69 */. .#if !defi
15ef5 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
15ef6 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 SHARED_CACHE) &&
15ef7 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
15ef8 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 _OMIT_DISKIO).
15ef9 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6e 65 77 /* Add the new
15efa 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 BtShared object
15efb 20 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c to the linked l
15efc 69 73 74 20 73 68 61 72 61 62 6c 65 20 42 74 53 ist sharable BtS
15efd 68 61 72 65 64 73 2e 0a 20 20 20 20 2a 2f 0a 20 hareds.. */.
15efe 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 if( p->sharab
15eff 6c 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 le ){. sqli
15f00 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
15f01 53 68 61 72 65 64 3b 0a 20 20 20 20 20 20 70 42 Shared;. pB
15f02 74 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 t->nRef = 1;.
15f03 20 20 20 6d 75 74 65 78 53 68 61 72 65 64 20 3d mutexShared =
15f04 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c sqlite3MutexAll
15f05 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
15f06 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a STATIC_MASTER);.
15f07 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 if( SQLITE
15f08 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 73 _THREADSAFE && s
15f09 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
15f0a 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b ig.bCoreMutex ){
15f0b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 6d 75 . pBt->mu
15f0c 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 tex = sqlite3Mut
15f0d 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
15f0e 55 54 45 58 5f 46 41 53 54 29 3b 0a 20 20 20 20 UTEX_FAST);.
15f0f 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6d 75 74 if( pBt->mut
15f10 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ex==0 ){.
15f11 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
15f12 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 OMEM;.
15f13 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
15f14 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
15f15 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f goto btree_open_
15f16 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 out;. }.
15f17 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
15f18 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
15f19 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 (mutexShared);.
15f1a 20 20 20 20 20 70 42 74 2d 3e 70 4e 65 78 74 20 pBt->pNext
15f1b 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 = GLOBAL(BtShare
15f1c 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 d*,sqlite3Shared
15f1d 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20 20 20 CacheList);.
15f1e 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 GLOBAL(BtShare
15f1f 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 d*,sqlite3Shared
15f20 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70 42 74 CacheList) = pBt
15f21 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
15f22 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 mutex_leave(mute
15f23 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 7d 0a xShared);. }.
15f24 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 23 69 66 20 #endif. }..#if
15f25 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
15f26 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
15f27 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 E) && !defined(S
15f28 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 QLITE_OMIT_DISKI
15f29 4f 29 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6e O). /* If the n
15f2a 65 77 20 42 74 72 65 65 20 75 73 65 73 20 61 20 ew Btree uses a
15f2b 73 68 61 72 61 62 6c 65 20 70 42 74 53 68 61 72 sharable pBtShar
15f2c 65 64 2c 20 74 68 65 6e 20 6c 69 6e 6b 20 74 68 ed, then link th
15f2d 65 20 6e 65 77 0a 20 20 2a 2a 20 42 74 72 65 65 e new. ** Btree
15f2e 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 20 6f into the list o
15f2f 66 20 61 6c 6c 20 73 68 61 72 61 62 6c 65 20 42 f all sharable B
15f30 74 72 65 65 73 20 66 6f 72 20 74 68 65 20 73 61 trees for the sa
15f31 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 20 me connection..
15f32 20 2a 2a 20 54 68 65 20 6c 69 73 74 20 69 73 20 ** The list is
15f33 6b 65 70 74 20 69 6e 20 61 73 63 65 6e 64 69 6e kept in ascendin
15f34 67 20 6f 72 64 65 72 20 62 79 20 70 42 74 20 61 g order by pBt a
15f35 64 64 72 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 69 ddress.. */. i
15f36 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 f( p->sharable )
15f37 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 {. int i;.
15f38 20 42 74 72 65 65 20 2a 70 53 69 62 3b 0a 20 20 Btree *pSib;.
15f39 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d for(i=0; i<db-
15f3a 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nDb; i++){.
15f3b 20 20 69 66 28 20 28 70 53 69 62 20 3d 20 64 62 if( (pSib = db
15f3c 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 21 3d 30 ->aDb[i].pBt)!=0
15f3d 20 26 26 20 70 53 69 62 2d 3e 73 68 61 72 61 62 && pSib->sharab
15f3e 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68 le ){. wh
15f3f 69 6c 65 28 20 70 53 69 62 2d 3e 70 50 72 65 76 ile( pSib->pPrev
15f40 20 29 7b 20 70 53 69 62 20 3d 20 70 53 69 62 2d ){ pSib = pSib-
15f41 3e 70 50 72 65 76 3b 20 7d 0a 20 20 20 20 20 20 >pPrev; }.
15f42 20 20 69 66 28 20 70 2d 3e 70 42 74 3c 70 53 69 if( p->pBt<pSi
15f43 62 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 b->pBt ){.
15f44 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 p->pNext = p
15f45 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 20 70 Sib;. p
15f46 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 ->pPrev = 0;.
15f47 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70 50 72 pSib->pPr
15f48 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 ev = p;.
15f49 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
15f4a 20 77 68 69 6c 65 28 20 70 53 69 62 2d 3e 70 4e while( pSib->pN
15f4b 65 78 74 20 26 26 20 70 53 69 62 2d 3e 70 4e 65 ext && pSib->pNe
15f4c 78 74 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 xt->pBt<p->pBt )
15f4d 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 53 {. pS
15f4e 69 62 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74 ib = pSib->pNext
15f4f 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
15f50 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 p->pNext
15f51 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74 3b 0a = pSib->pNext;.
15f52 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 72 p->pPr
15f53 65 76 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 20 ev = pSib;.
15f54 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 if( p->pNex
15f55 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 t ){.
15f56 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 p->pNext->pPrev
15f57 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 = p;.
15f58 7d 0a 20 20 20 20 20 20 20 20 20 20 70 53 69 62 }. pSib
15f59 2d 3e 70 4e 65 78 74 20 3d 20 70 3b 0a 20 20 20 ->pNext = p;.
15f5a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 }. b
15f5b 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
15f5c 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }. }.#endif.
15f5d 20 2a 70 70 42 74 72 65 65 20 3d 20 70 3b 0a 0a *ppBtree = p;..
15f5e 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3a 0a btree_open_out:.
15f5f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
15f60 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 _OK ){. if( p
15f61 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 Bt && pBt->pPage
15f62 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 r ){. sqlit
15f63 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70 42 74 e3PagerClose(pBt
15f64 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d ->pPager);. }
15f65 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
15f66 65 28 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 e(pBt);. sqli
15f67 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 te3_free(p);.
15f68 20 2a 70 70 42 74 72 65 65 20 3d 20 30 3b 0a 20 *ppBtree = 0;.
15f69 20 7d 0a 20 20 69 66 28 20 6d 75 74 65 78 4f 70 }. if( mutexOp
15f6a 65 6e 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 en ){. assert
15f6b 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
15f6c 68 65 6c 64 28 6d 75 74 65 78 4f 70 65 6e 29 20 held(mutexOpen)
15f6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
15f6e 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 utex_leave(mutex
15f6f 4f 70 65 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 Open);. }. ret
15f70 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
15f71 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 42 Decrement the B
15f72 74 53 68 61 72 65 64 2e 6e 52 65 66 20 63 6f 75 tShared.nRef cou
15f73 6e 74 65 72 2e 20 20 57 68 65 6e 20 69 74 20 72 nter. When it r
15f74 65 61 63 68 65 73 20 7a 65 72 6f 2c 0a 2a 2a 20 eaches zero,.**
15f75 72 65 6d 6f 76 65 20 74 68 65 20 42 74 53 68 61 remove the BtSha
15f76 72 65 64 20 73 74 72 75 63 74 75 72 65 20 66 72 red structure fr
15f77 6f 6d 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c om the sharing l
15f78 69 73 74 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 ist. Return.**
15f79 74 72 75 65 20 69 66 20 74 68 65 20 42 74 53 68 true if the BtSh
15f7a 61 72 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65 ared.nRef counte
15f7b 72 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61 r reaches zero a
15f7c 6e 64 20 72 65 74 75 72 6e 0a 2a 2a 20 66 61 6c nd return.** fal
15f7d 73 65 20 69 66 20 69 74 20 69 73 20 73 74 69 6c se if it is stil
15f7e 6c 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2f 0a 73 l positive..*/.s
15f7f 74 61 74 69 63 20 69 6e 74 20 72 65 6d 6f 76 65 tatic int remove
15f80 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 73 74 28 FromSharingList(
15f81 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a BtShared *pBt){.
15f82 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
15f83 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
15f84 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
15f85 20 2a 70 4d 61 73 74 65 72 3b 0a 20 20 42 74 53 *pMaster;. BtS
15f86 68 61 72 65 64 20 2a 70 4c 69 73 74 3b 0a 20 20 hared *pList;.
15f87 69 6e 74 20 72 65 6d 6f 76 65 64 20 3d 20 30 3b int removed = 0;
15f88 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
15f89 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c te3_mutex_nothel
15f8a 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
15f8b 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 73 71 6c . pMaster = sql
15f8c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
15f8d 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
15f8e 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 71 IC_MASTER);. sq
15f8f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
15f90 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 70 42 r(pMaster);. pB
15f91 74 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 t->nRef--;. if(
15f92 20 70 42 74 2d 3e 6e 52 65 66 3c 3d 30 20 29 7b pBt->nRef<=0 ){
15f93 0a 20 20 20 20 69 66 28 20 47 4c 4f 42 41 4c 28 . if( GLOBAL(
15f94 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 BtShared*,sqlite
15f95 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 3SharedCacheList
15f96 29 3d 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 20 )==pBt ){.
15f97 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a GLOBAL(BtShared*
15f98 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 ,sqlite3SharedCa
15f99 63 68 65 4c 69 73 74 29 20 3d 20 70 42 74 2d 3e cheList) = pBt->
15f9a 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 pNext;. }else
15f9b 7b 0a 20 20 20 20 20 20 70 4c 69 73 74 20 3d 20 {. pList =
15f9c 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a GLOBAL(BtShared*
15f9d 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 ,sqlite3SharedCa
15f9e 63 68 65 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 cheList);.
15f9f 77 68 69 6c 65 28 20 41 4c 57 41 59 53 28 70 4c while( ALWAYS(pL
15fa0 69 73 74 29 20 26 26 20 70 4c 69 73 74 2d 3e 70 ist) && pList->p
15fa1 4e 65 78 74 21 3d 70 42 74 20 29 7b 0a 20 20 20 Next!=pBt ){.
15fa2 20 20 20 20 20 70 4c 69 73 74 3d 70 4c 69 73 74 pList=pList
15fa3 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d ->pNext;. }
15fa4 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 . if( ALWAY
15fa5 53 28 70 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 S(pList) ){.
15fa6 20 20 20 20 70 4c 69 73 74 2d 3e 70 4e 65 78 74 pList->pNext
15fa7 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 = pBt->pNext;.
15fa8 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
15fa9 20 69 66 28 20 53 51 4c 49 54 45 5f 54 48 52 45 if( SQLITE_THRE
15faa 41 44 53 41 46 45 20 29 7b 0a 20 20 20 20 20 20 ADSAFE ){.
15fab 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 sqlite3_mutex_fr
15fac 65 65 28 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a ee(pBt->mutex);.
15fad 20 20 20 20 7d 0a 20 20 20 20 72 65 6d 6f 76 65 }. remove
15fae 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c d = 1;. }. sql
15faf 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
15fb0 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74 (pMaster);. ret
15fb1 75 72 6e 20 72 65 6d 6f 76 65 64 3b 0a 23 65 6c urn removed;.#el
15fb2 73 65 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 se. return 1;.#
15fb3 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d endif.}../*.** M
15fb4 61 6b 65 20 73 75 72 65 20 70 42 74 2d 3e 70 54 ake sure pBt->pT
15fb5 6d 70 53 70 61 63 65 20 70 6f 69 6e 74 73 20 74 mpSpace points t
15fb6 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 o an allocation
15fb7 6f 66 20 0a 2a 2a 20 4d 58 5f 43 45 4c 4c 5f 53 of .** MX_CELL_S
15fb8 49 5a 45 28 70 42 74 29 20 62 79 74 65 73 2e 0a IZE(pBt) bytes..
15fb9 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 */.static void a
15fba 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70 61 63 65 llocateTempSpace
15fbb 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b (BtShared *pBt){
15fbc 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 54 6d . if( !pBt->pTm
15fbd 70 53 70 61 63 65 20 29 7b 0a 20 20 20 20 70 42 pSpace ){. pB
15fbe 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 73 t->pTmpSpace = s
15fbf 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 qlite3PageMalloc
15fc0 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 ( pBt->pageSize
15fc1 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
15fc2 46 72 65 65 20 74 68 65 20 70 42 74 2d 3e 70 54 Free the pBt->pT
15fc3 6d 70 53 70 61 63 65 20 61 6c 6c 6f 63 61 74 69 mpSpace allocati
15fc4 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 on.*/.static voi
15fc5 64 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 d freeTempSpace(
15fc6 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a BtShared *pBt){.
15fc7 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 sqlite3PageFre
15fc8 65 28 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 e( pBt->pTmpSpac
15fc9 65 29 3b 0a 20 20 70 42 74 2d 3e 70 54 6d 70 53 e);. pBt->pTmpS
15fca 70 61 63 65 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a pace = 0;.}../*.
15fcb 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 6f 70 65 6e ** Close an open
15fcc 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 6e database and in
15fcd 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 63 75 72 validate all cur
15fce 73 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f sors..*/.SQLITE_
15fcf 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
15fd0 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 42 74 te3BtreeClose(Bt
15fd1 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 ree *p){. BtSha
15fd2 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
15fd3 74 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 t;. BtCursor *p
15fd4 43 75 72 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65 Cur;.. /* Close
15fd5 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f 70 65 all cursors ope
15fd6 6e 65 64 20 76 69 61 20 74 68 69 73 20 68 61 6e ned via this han
15fd7 64 6c 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 dle. */. asser
15fd8 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
15fd9 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 _held(p->db->mut
15fda 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ex) );. sqlite3
15fdb 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
15fdc 20 70 43 75 72 20 3d 20 70 42 74 2d 3e 70 43 75 pCur = pBt->pCu
15fdd 72 73 6f 72 3b 0a 20 20 77 68 69 6c 65 28 20 70 rsor;. while( p
15fde 43 75 72 20 29 7b 0a 20 20 20 20 42 74 43 75 72 Cur ){. BtCur
15fdf 73 6f 72 20 2a 70 54 6d 70 20 3d 20 70 43 75 72 sor *pTmp = pCur
15fe0 3b 0a 20 20 20 20 70 43 75 72 20 3d 20 70 43 75 ;. pCur = pCu
15fe1 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 69 66 r->pNext;. if
15fe2 28 20 70 54 6d 70 2d 3e 70 42 74 72 65 65 3d 3d ( pTmp->pBtree==
15fe3 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 p ){. sqlit
15fe4 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 e3BtreeCloseCurs
15fe5 6f 72 28 70 54 6d 70 29 3b 0a 20 20 20 20 7d 0a or(pTmp);. }.
15fe6 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 6c 6c 62 61 }.. /* Rollba
15fe7 63 6b 20 61 6e 79 20 61 63 74 69 76 65 20 74 72 ck any active tr
15fe8 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 66 72 ansaction and fr
15fe9 65 65 20 74 68 65 20 68 61 6e 64 6c 65 20 73 74 ee the handle st
15fea 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20 54 68 ructure.. ** Th
15feb 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 e call to sqlite
15fec 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 29 3BtreeRollback()
15fed 20 64 72 6f 70 73 20 61 6e 79 20 74 61 62 6c 65 drops any table
15fee 2d 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 0a 20 -locks held by.
15fef 20 2a 2a 20 74 68 69 73 20 68 61 6e 64 6c 65 2e ** this handle.
15ff0 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 . */. sqlite3B
15ff1 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 29 3b treeRollback(p);
15ff2 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
15ff3 65 61 76 65 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 eave(p);.. /* I
15ff4 66 20 74 68 65 72 65 20 61 72 65 20 73 74 69 6c f there are stil
15ff5 6c 20 6f 74 68 65 72 20 6f 75 74 73 74 61 6e 64 l other outstand
15ff6 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 20 74 ing references t
15ff7 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 o the shared-btr
15ff8 65 65 0a 20 20 2a 2a 20 73 74 72 75 63 74 75 72 ee. ** structur
15ff9 65 2c 20 72 65 74 75 72 6e 20 6e 6f 77 2e 20 54 e, return now. T
15ffa 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 he remainder of
15ffb 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 20 63 this procedure c
15ffc 6c 65 61 6e 73 20 0a 20 20 2a 2a 20 75 70 20 74 leans . ** up t
15ffd 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 2e he shared-btree.
15ffe 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
15fff 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 p->wantToLock==0
16000 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 && p->locked==0
16001 20 29 3b 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 );. if( !p->sh
16002 61 72 61 62 6c 65 20 7c 7c 20 72 65 6d 6f 76 65 arable || remove
16003 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 73 74 28 FromSharingList(
16004 70 42 74 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 pBt) ){. /* T
16005 68 65 20 70 42 74 20 69 73 20 6e 6f 20 6c 6f 6e he pBt is no lon
16006 67 65 72 20 6f 6e 20 74 68 65 20 73 68 61 72 69 ger on the shari
16007 6e 67 20 6c 69 73 74 2c 20 73 6f 20 77 65 20 63 ng list, so we c
16008 61 6e 20 61 63 63 65 73 73 0a 20 20 20 20 2a 2a an access. **
16009 20 69 74 20 77 69 74 68 6f 75 74 20 68 61 76 69 it without havi
1600a 6e 67 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d ng to hold the m
1600b 75 74 65 78 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 utex.. **.
1600c 20 2a 2a 20 43 6c 65 61 6e 20 6f 75 74 20 61 6e ** Clean out an
1600d 64 20 64 65 6c 65 74 65 20 74 68 65 20 42 74 53 d delete the BtS
1600e 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 0a 20 20 hared object..
1600f 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
16010 20 21 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 !pBt->pCursor )
16011 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 ;. sqlite3Pag
16012 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70 50 61 erClose(pBt->pPa
16013 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 70 42 ger);. if( pB
16014 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20 26 t->xFreeSchema &
16015 26 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 29 & pBt->pSchema )
16016 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 78 46 72 {. pBt->xFr
16017 65 65 53 63 68 65 6d 61 28 70 42 74 2d 3e 70 53 eeSchema(pBt->pS
16018 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 20 20 chema);. }.
16019 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
1601a 42 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 Bt->pSchema);.
1601b 20 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 freeTempSpace(
1601c 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 pBt);. sqlite
1601d 33 5f 66 72 65 65 28 70 42 74 29 3b 0a 20 20 7d 3_free(pBt);. }
1601e 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1601f 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
16020 48 45 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e HE. assert( p->
16021 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 3b wantToLock==0 );
16022 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f . assert( p->lo
16023 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28 cked==0 );. if(
16024 20 70 2d 3e 70 50 72 65 76 20 29 20 70 2d 3e 70 p->pPrev ) p->p
16025 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 2d Prev->pNext = p-
16026 3e 70 4e 65 78 74 3b 0a 20 20 69 66 28 20 70 2d >pNext;. if( p-
16027 3e 70 4e 65 78 74 20 29 20 70 2d 3e 70 4e 65 78 >pNext ) p->pNex
16028 74 2d 3e 70 50 72 65 76 20 3d 20 70 2d 3e 70 50 t->pPrev = p->pP
16029 72 65 76 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 rev;.#endif.. s
1602a 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
1602b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1602c 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 OK;.}../*.** Cha
1602d 6e 67 65 20 74 68 65 20 6c 69 6d 69 74 20 6f 6e nge the limit on
1602e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
1602f 61 67 65 73 20 61 6c 6c 6f 77 65 64 20 69 6e 20 ages allowed in
16030 74 68 65 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a the cache..**.**
16031 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d The maximum num
16032 62 65 72 20 6f 66 20 63 61 63 68 65 20 70 61 67 ber of cache pag
16033 65 73 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 es is set to the
16034 20 61 62 73 6f 6c 75 74 65 0a 2a 2a 20 76 61 6c absolute.** val
16035 75 65 20 6f 66 20 6d 78 50 61 67 65 2e 20 20 49 ue of mxPage. I
16036 66 20 6d 78 50 61 67 65 20 69 73 20 6e 65 67 61 f mxPage is nega
16037 74 69 76 65 2c 20 74 68 65 20 70 61 67 65 72 20 tive, the pager
16038 77 69 6c 6c 0a 2a 2a 20 6f 70 65 72 61 74 65 20 will.** operate
16039 61 73 79 6e 63 68 72 6f 6e 6f 75 73 6c 79 20 2d asynchronously -
1603a 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 73 74 6f it will not sto
1603b 70 20 74 6f 20 64 6f 20 66 73 79 6e 63 28 29 73 p to do fsync()s
1603c 0a 2a 2a 20 74 6f 20 69 6e 73 75 72 65 20 64 61 .** to insure da
1603d 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f ta is written to
1603e 20 74 68 65 20 64 69 73 6b 20 73 75 72 66 61 63 the disk surfac
1603f 65 20 62 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74 e before.** cont
16040 69 6e 75 69 6e 67 2e 20 20 54 72 61 6e 73 61 63 inuing. Transac
16041 74 69 6f 6e 73 20 73 74 69 6c 6c 20 77 6f 72 6b tions still work
16042 20 69 66 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 if synchronous
16043 69 73 20 6f 66 66 2c 0a 2a 2a 20 61 6e 64 20 74 is off,.** and t
16044 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 6e he database cann
16045 6f 74 20 62 65 20 63 6f 72 72 75 70 74 65 64 20 ot be corrupted
16046 69 66 20 74 68 69 73 20 70 72 6f 67 72 61 6d 0a if this program.
16047 2a 2a 20 63 72 61 73 68 65 73 2e 20 20 42 75 74 ** crashes. But
16048 20 69 66 20 74 68 65 20 6f 70 65 72 61 74 69 6e if the operatin
16049 67 20 73 79 73 74 65 6d 20 63 72 61 73 68 65 73 g system crashes
1604a 20 6f 72 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 or there is.**
1604b 61 6e 20 61 62 72 75 70 74 20 70 6f 77 65 72 20 an abrupt power
1604c 66 61 69 6c 75 72 65 20 77 68 65 6e 20 73 79 6e failure when syn
1604d 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 66 66 2c chronous is off,
1604e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a the database.**
1604f 20 63 6f 75 6c 64 20 62 65 20 6c 65 66 74 20 69 could be left i
16050 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e n an inconsisten
16051 74 20 61 6e 64 20 75 6e 72 65 63 6f 76 65 72 61 t and unrecovera
16052 62 6c 65 20 73 74 61 74 65 2e 0a 2a 2a 20 53 79 ble state..** Sy
16053 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 6e 20 nchronous is on
16054 62 79 20 64 65 66 61 75 6c 74 20 73 6f 20 64 61 by default so da
16055 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f tabase corruptio
16056 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d n is not.** norm
16057 61 6c 6c 79 20 61 20 77 6f 72 72 79 2e 0a 2a 2f ally a worry..*/
16058 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16059 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
1605a 53 65 74 43 61 63 68 65 53 69 7a 65 28 42 74 72 SetCacheSize(Btr
1605b 65 65 20 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67 ee *p, int mxPag
1605c 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a e){. BtShared *
1605d 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
1605e 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1605f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 mutex_held(p->db
16060 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 ->mutex) );. sq
16061 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
16062 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 p);. sqlite3Pag
16063 65 72 53 65 74 43 61 63 68 65 73 69 7a 65 28 70 erSetCachesize(p
16064 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d 78 50 61 Bt->pPager, mxPa
16065 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ge);. sqlite3Bt
16066 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
16067 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
16068 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 .}../*.** Change
16069 20 74 68 65 20 77 61 79 20 64 61 74 61 20 69 73 the way data is
1606a 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 20 synced to disk
1606b 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e 63 72 in order to incr
1606c 65 61 73 65 20 6f 72 20 64 65 63 72 65 61 73 65 ease or decrease
1606d 0a 2a 2a 20 68 6f 77 20 77 65 6c 6c 20 74 68 65 .** how well the
1606e 20 64 61 74 61 62 61 73 65 20 72 65 73 69 73 74 database resist
1606f 73 20 64 61 6d 61 67 65 20 64 75 65 20 74 6f 20 s damage due to
16070 4f 53 20 63 72 61 73 68 65 73 20 61 6e 64 20 70 OS crashes and p
16071 6f 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 73 ower.** failures
16072 2e 20 20 4c 65 76 65 6c 20 31 20 69 73 20 74 68 . Level 1 is th
16073 65 20 73 61 6d 65 20 61 73 20 61 73 79 6e 63 68 e same as asynch
16074 72 6f 6e 6f 75 73 20 28 6e 6f 20 73 79 6e 63 73 ronous (no syncs
16075 28 29 20 6f 63 63 75 72 20 61 6e 64 0a 2a 2a 20 () occur and.**
16076 74 68 65 72 65 20 69 73 20 61 20 68 69 67 68 20 there is a high
16077 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 probability of d
16078 61 6d 61 67 65 29 20 20 4c 65 76 65 6c 20 32 20 amage) Level 2
16079 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 is the default.
1607a 20 54 68 65 72 65 0a 2a 2a 20 69 73 20 61 20 76 There.** is a v
1607b 65 72 79 20 6c 6f 77 20 62 75 74 20 6e 6f 6e 2d ery low but non-
1607c 7a 65 72 6f 20 70 72 6f 62 61 62 69 6c 69 74 79 zero probability
1607d 20 6f 66 20 64 61 6d 61 67 65 2e 20 20 4c 65 76 of damage. Lev
1607e 65 6c 20 33 20 72 65 64 75 63 65 73 20 74 68 65 el 3 reduces the
1607f 0a 2a 2a 20 70 72 6f 62 61 62 69 6c 69 74 79 20 .** probability
16080 6f 66 20 64 61 6d 61 67 65 20 74 6f 20 6e 65 61 of damage to nea
16081 72 20 7a 65 72 6f 20 62 75 74 20 77 69 74 68 20 r zero but with
16082 61 20 77 72 69 74 65 20 70 65 72 66 6f 72 6d 61 a write performa
16083 6e 63 65 20 72 65 64 75 63 74 69 6f 6e 2e 0a 2a nce reduction..*
16084 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
16085 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 _OMIT_PAGER_PRAG
16086 4d 41 53 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 MAS.SQLITE_PRIVA
16087 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
16088 72 65 65 53 65 74 53 61 66 65 74 79 4c 65 76 65 reeSetSafetyLeve
16089 6c 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 l(Btree *p, int
1608a 6c 65 76 65 6c 2c 20 69 6e 74 20 66 75 6c 6c 53 level, int fullS
1608b 79 6e 63 29 7b 0a 20 20 42 74 53 68 61 72 65 64 ync){. BtShared
1608c 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
1608d 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1608e 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
1608f 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
16090 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
16091 72 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 r(p);. sqlite3P
16092 61 67 65 72 53 65 74 53 61 66 65 74 79 4c 65 76 agerSetSafetyLev
16093 65 6c 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 el(pBt->pPager,
16094 6c 65 76 65 6c 2c 20 66 75 6c 6c 53 79 6e 63 29 level, fullSync)
16095 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
16096 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
16097 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
16098 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 #endif../*.** Re
16099 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 turn TRUE if the
1609a 20 67 69 76 65 6e 20 62 74 72 65 65 20 69 73 20 given btree is
1609b 73 65 74 20 74 6f 20 73 61 66 65 74 79 20 6c 65 set to safety le
1609c 76 65 6c 20 31 2e 20 20 49 6e 20 6f 74 68 65 72 vel 1. In other
1609d 0a 2a 2a 20 77 6f 72 64 73 2c 20 72 65 74 75 72 .** words, retur
1609e 6e 20 54 52 55 45 20 69 66 20 6e 6f 20 73 79 6e n TRUE if no syn
1609f 63 28 29 20 6f 63 63 75 72 73 20 6f 6e 20 74 68 c() occurs on th
160a0 65 20 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f e disk files..*/
160a1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
160a2 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
160a3 53 79 6e 63 44 69 73 61 62 6c 65 64 28 42 74 72 SyncDisabled(Btr
160a4 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 ee *p){. BtShar
160a5 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
160a6 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 ;. int rc;. as
160a7 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
160a8 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e tex_held(p->db->
160a9 6d 75 74 65 78 29 20 29 3b 20 20 0a 20 20 73 71 mutex) ); . sq
160aa 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
160ab 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 p);. assert( pB
160ac 74 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 72 t && pBt->pPager
160ad 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 );. rc = sqlit
160ae 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 70 42 e3PagerNosync(pB
160af 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 73 71 t->pPager);. sq
160b0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
160b1 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
160b2 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 .}..#if !defined
160b3 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 (SQLITE_OMIT_PAG
160b4 45 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 ER_PRAGMAS) || !
160b5 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
160b6 4d 49 54 5f 56 41 43 55 55 4d 29 0a 2f 2a 0a 2a MIT_VACUUM)./*.*
160b7 2a 20 43 68 61 6e 67 65 20 74 68 65 20 64 65 66 * Change the def
160b8 61 75 6c 74 20 70 61 67 65 73 20 73 69 7a 65 20 ault pages size
160b9 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f and the number o
160ba 66 20 72 65 73 65 72 76 65 64 20 62 79 74 65 73 f reserved bytes
160bb 20 70 65 72 20 70 61 67 65 2e 0a 2a 2a 20 4f 72 per page..** Or
160bc 2c 20 69 66 20 74 68 65 20 70 61 67 65 20 73 69 , if the page si
160bd 7a 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 ze has already b
160be 65 65 6e 20 66 69 78 65 64 2c 20 72 65 74 75 72 een fixed, retur
160bf 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c n SQLITE_READONL
160c0 59 20 0a 2a 2a 20 77 69 74 68 6f 75 74 20 63 68 Y .** without ch
160c1 61 6e 67 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e anging anything.
160c2 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 .**.** The page
160c3 73 69 7a 65 20 6d 75 73 74 20 62 65 20 61 20 70 size must be a p
160c4 6f 77 65 72 20 6f 66 20 32 20 62 65 74 77 65 65 ower of 2 betwee
160c5 6e 20 35 31 32 20 61 6e 64 20 36 35 35 33 36 2e n 512 and 65536.
160c6 20 20 49 66 20 74 68 65 20 70 61 67 65 0a 2a 2a If the page.**
160c7 20 73 69 7a 65 20 73 75 70 70 6c 69 65 64 20 64 size supplied d
160c8 6f 65 73 20 6e 6f 74 20 6d 65 65 74 20 74 68 69 oes not meet thi
160c9 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 68 65 s constraint the
160ca 6e 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 n the page size
160cb 69 73 20 6e 6f 74 0a 2a 2a 20 63 68 61 6e 67 65 is not.** change
160cc 64 2e 0a 2a 2a 0a 2a 2a 20 50 61 67 65 20 73 69 d..**.** Page si
160cd 7a 65 73 20 61 72 65 20 63 6f 6e 73 74 72 61 69 zes are constrai
160ce 6e 65 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 ned to be a powe
160cf 72 20 6f 66 20 74 77 6f 20 73 6f 20 74 68 61 74 r of two so that
160d0 20 74 68 65 20 72 65 67 69 6f 6e 0a 2a 2a 20 6f the region.** o
160d1 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
160d2 69 6c 65 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 ile used for loc
160d3 6b 69 6e 67 20 28 62 65 67 69 6e 6e 69 6e 67 20 king (beginning
160d4 61 74 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c at PENDING_BYTE,
160d5 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 62 79 .** the first by
160d6 74 65 20 70 61 73 74 20 74 68 65 20 31 47 42 20 te past the 1GB
160d7 62 6f 75 6e 64 61 72 79 2c 20 30 78 34 30 30 30 boundary, 0x4000
160d8 30 30 30 30 29 20 6e 65 65 64 73 20 74 6f 20 6f 0000) needs to o
160d9 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 20 62 ccur.** at the b
160da 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 70 61 eginning of a pa
160db 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 ge..**.** If par
160dc 61 6d 65 74 65 72 20 6e 52 65 73 65 72 76 65 20 ameter nReserve
160dd 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 is less than zer
160de 6f 2c 20 74 68 65 6e 20 74 68 65 20 6e 75 6d 62 o, then the numb
160df 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 0a 2a er of reserved.*
160e0 2a 20 62 79 74 65 73 20 70 65 72 20 70 61 67 65 * bytes per page
160e1 20 69 73 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 is left unchang
160e2 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ed..**.** If the
160e3 20 69 46 69 78 21 3d 30 20 74 68 65 6e 20 74 68 iFix!=0 then th
160e4 65 20 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 e pageSizeFixed
160e5 66 6c 61 67 20 69 73 20 73 65 74 20 73 6f 20 74 flag is set so t
160e6 68 61 74 20 74 68 65 20 70 61 67 65 20 73 69 7a hat the page siz
160e7 65 0a 2a 2a 20 61 6e 64 20 61 75 74 6f 76 61 63 e.** and autovac
160e8 75 75 6d 20 6d 6f 64 65 20 63 61 6e 20 6e 6f 20 uum mode can no
160e9 6c 6f 6e 67 65 72 20 62 65 20 63 68 61 6e 67 65 longer be change
160ea 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
160eb 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
160ec 42 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 BtreeSetPageSize
160ed 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 70 (Btree *p, int p
160ee 61 67 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 65 ageSize, int nRe
160ef 73 65 72 76 65 2c 20 69 6e 74 20 69 46 69 78 29 serve, int iFix)
160f0 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
160f1 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 53 68 61 72 ITE_OK;. BtShar
160f2 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
160f3 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 73 ;. assert( nRes
160f4 65 72 76 65 3e 3d 2d 31 20 26 26 20 6e 52 65 73 erve>=-1 && nRes
160f5 65 72 76 65 3c 3d 32 35 35 20 29 3b 0a 20 20 73 erve<=255 );. s
160f6 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
160f7 28 70 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e (p);. if( pBt->
160f8 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 29 7b pageSizeFixed ){
160f9 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
160fa 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 eLeave(p);. r
160fb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 eturn SQLITE_REA
160fc 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 66 28 DONLY;. }. if(
160fd 20 6e 52 65 73 65 72 76 65 3c 30 20 29 7b 0a 20 nReserve<0 ){.
160fe 20 20 20 6e 52 65 73 65 72 76 65 20 3d 20 70 42 nReserve = pB
160ff 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42 t->pageSize - pB
16100 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 t->usableSize;.
16101 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 }. assert( nRe
16102 73 65 72 76 65 3e 3d 30 20 26 26 20 6e 52 65 73 serve>=0 && nRes
16103 65 72 76 65 3c 3d 32 35 35 20 29 3b 0a 20 20 69 erve<=255 );. i
16104 66 28 20 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 f( pageSize>=512
16105 20 26 26 20 70 61 67 65 53 69 7a 65 3c 3d 53 51 && pageSize<=SQ
16106 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 LITE_MAX_PAGE_SI
16107 5a 45 20 26 26 0a 20 20 20 20 20 20 20 20 28 28 ZE &&. ((
16108 70 61 67 65 53 69 7a 65 2d 31 29 26 70 61 67 65 pageSize-1)&page
16109 53 69 7a 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 Size)==0 ){.
1610a 61 73 73 65 72 74 28 20 28 70 61 67 65 53 69 7a assert( (pageSiz
1610b 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 e & 7)==0 );.
1610c 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 assert( !pBt->p
1610d 50 61 67 65 31 20 26 26 20 21 70 42 74 2d 3e 70 Page1 && !pBt->p
1610e 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 70 42 Cursor );. pB
1610f 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 t->pageSize = (u
16110 31 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 16)pageSize;.
16111 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 freeTempSpace(p
16112 42 74 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 Bt);. }. rc =
16113 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 sqlite3PagerSetP
16114 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 agesize(pBt->pPa
16115 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 ger, &pBt->pageS
16116 69 7a 65 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a ize, nReserve);.
16117 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a pBt->usableSiz
16118 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a e = pBt->pageSiz
16119 65 20 2d 20 28 75 31 36 29 6e 52 65 73 65 72 76 e - (u16)nReserv
1611a 65 3b 0a 20 20 69 66 28 20 69 46 69 78 20 29 20 e;. if( iFix )
1611b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 pBt->pageSizeFix
1611c 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 ed = 1;. sqlite
1611d 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
1611e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1611f 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
16120 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e currently defin
16121 65 64 20 70 61 67 65 20 73 69 7a 65 0a 2a 2f 0a ed page size.*/.
16122 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
16123 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 nt sqlite3BtreeG
16124 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 etPageSize(Btree
16125 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 *p){. return p
16126 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3b ->pBt->pageSize;
16127 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
16128 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
16129 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 61 74 ytes of space at
1612a 20 74 68 65 20 65 6e 64 20 6f 66 20 65 76 65 72 the end of ever
1612b 79 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 61 y page that.** a
1612c 72 65 20 69 6e 74 65 6e 74 75 61 6c 6c 79 20 6c re intentually l
1612d 65 66 74 20 75 6e 75 73 65 64 2e 20 20 54 68 69 eft unused. Thi
1612e 73 20 69 73 20 74 68 65 20 22 72 65 73 65 72 76 s is the "reserv
1612f 65 64 22 20 73 70 61 63 65 20 74 68 61 74 20 69 ed" space that i
16130 73 0a 2a 2a 20 73 6f 6d 65 74 69 6d 65 73 20 75 s.** sometimes u
16131 73 65 64 20 62 79 20 65 78 74 65 6e 73 69 6f 6e sed by extension
16132 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
16133 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
16134 42 74 72 65 65 47 65 74 52 65 73 65 72 76 65 28 BtreeGetReserve(
16135 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 Btree *p){. int
16136 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 n;. sqlite3Btr
16137 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e 20 eeEnter(p);. n
16138 3d 20 70 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 = p->pBt->pageSi
16139 7a 65 20 2d 20 70 2d 3e 70 42 74 2d 3e 75 73 61 ze - p->pBt->usa
1613a 62 6c 65 53 69 7a 65 3b 0a 20 20 73 71 6c 69 74 bleSize;. sqlit
1613b 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
1613c 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a . return n;.}..
1613d 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d 61 /*.** Set the ma
1613e 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 ximum page count
1613f 20 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 20 for a database
16140 69 66 20 6d 78 50 61 67 65 20 69 73 20 70 6f 73 if mxPage is pos
16141 69 74 69 76 65 2e 0a 2a 2a 20 4e 6f 20 63 68 61 itive..** No cha
16142 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 69 66 nges are made if
16143 20 6d 78 50 61 67 65 20 69 73 20 30 20 6f 72 20 mxPage is 0 or
16144 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 20 52 65 67 negative..** Reg
16145 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 76 ardless of the v
16146 61 6c 75 65 20 6f 66 20 6d 78 50 61 67 65 2c 20 alue of mxPage,
16147 72 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d return the maxim
16148 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a um page count..*
16149 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1614a 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
1614b 65 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 42 74 eMaxPageCount(Bt
1614c 72 65 65 20 2a 70 2c 20 69 6e 74 20 6d 78 50 61 ree *p, int mxPa
1614d 67 65 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 ge){. int n;.
1614e 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1614f 72 28 70 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 r(p);. n = sqli
16150 74 65 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 te3PagerMaxPageC
16151 6f 75 6e 74 28 70 2d 3e 70 42 74 2d 3e 70 50 61 ount(p->pBt->pPa
16152 67 65 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 ger, mxPage);.
16153 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
16154 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e e(p);. return n
16155 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 ;.}.#endif /* !d
16156 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
16157 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 IT_PAGER_PRAGMAS
16158 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 ) || !defined(SQ
16159 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d LITE_OMIT_VACUUM
1615a 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e ) */../*.** Chan
1615b 67 65 20 74 68 65 20 27 61 75 74 6f 2d 76 61 63 ge the 'auto-vac
1615c 75 75 6d 27 20 70 72 6f 70 65 72 74 79 20 6f 66 uum' property of
1615d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 the database. I
1615e 66 20 74 68 65 20 27 61 75 74 6f 56 61 63 75 75 f the 'autoVacuu
1615f 6d 27 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 m'.** parameter
16160 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
16161 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f n auto-vacuum mo
16162 64 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 49 de is enabled. I
16163 66 20 7a 65 72 6f 2c 20 69 74 0a 2a 2a 20 69 73 f zero, it.** is
16164 20 64 69 73 61 62 6c 65 64 2e 20 54 68 65 20 64 disabled. The d
16165 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 efault value for
16166 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d the auto-vacuum
16167 20 70 72 6f 70 65 72 74 79 20 69 73 20 0a 2a 2a property is .**
16168 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 determined by t
16169 68 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c he SQLITE_DEFAUL
1616a 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 6d 61 63 T_AUTOVACUUM mac
1616b 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ro..*/.SQLITE_PR
1616c 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1616d 33 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 3BtreeSetAutoVac
1616e 75 75 6d 28 42 74 72 65 65 20 2a 70 2c 20 69 6e uum(Btree *p, in
1616f 74 20 61 75 74 6f 56 61 63 75 75 6d 29 7b 0a 23 t autoVacuum){.#
16170 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
16171 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 72 T_AUTOVACUUM. r
16172 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 eturn SQLITE_REA
16173 44 4f 4e 4c 59 3b 0a 23 65 6c 73 65 0a 20 20 42 DONLY;.#else. B
16174 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
16175 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 20 ->pBt;. int rc
16176 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 = SQLITE_OK;. u
16177 38 20 61 76 20 3d 20 28 75 38 29 61 75 74 6f 56 8 av = (u8)autoV
16178 61 63 75 75 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 acuum;.. sqlite
16179 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
1617a 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 if( pBt->pageS
1617b 69 7a 65 46 69 78 65 64 20 26 26 20 28 61 76 20 izeFixed && (av
1617c 3f 31 3a 30 29 21 3d 70 42 74 2d 3e 61 75 74 6f ?1:0)!=pBt->auto
1617d 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 72 63 Vacuum ){. rc
1617e 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e = SQLITE_READON
1617f 4c 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 LY;. }else{.
16180 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
16181 20 3d 20 61 76 20 3f 31 3a 30 3b 0a 20 20 20 20 = av ?1:0;.
16182 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 pBt->incrVacuum
16183 3d 20 61 76 3d 3d 32 20 3f 31 3a 30 3b 0a 20 20 = av==2 ?1:0;.
16184 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 }. sqlite3Btree
16185 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
16186 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a rn rc;.#endif.}.
16187 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
16188 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 27 e value of the '
16189 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f auto-vacuum' pro
1618a 70 65 72 74 79 2e 20 49 66 20 61 75 74 6f 2d 76 perty. If auto-v
1618b 61 63 75 75 6d 20 69 73 20 0a 2a 2a 20 65 6e 61 acuum is .** ena
1618c 62 6c 65 64 20 31 20 69 73 20 72 65 74 75 72 6e bled 1 is return
1618d 65 64 2e 20 4f 74 68 65 72 77 69 73 65 20 30 2e ed. Otherwise 0.
1618e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1618f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
16190 72 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d reeGetAutoVacuum
16191 28 42 74 72 65 65 20 2a 70 29 7b 0a 23 69 66 64 (Btree *p){.#ifd
16192 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
16193 55 54 4f 56 41 43 55 55 4d 0a 20 20 72 65 74 75 UTOVACUUM. retu
16194 72 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 rn BTREE_AUTOVAC
16195 55 55 4d 5f 4e 4f 4e 45 3b 0a 23 65 6c 73 65 0a UUM_NONE;.#else.
16196 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 int rc;. sqli
16197 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
16198 3b 0a 20 20 72 63 20 3d 20 28 0a 20 20 20 20 28 ;. rc = (. (
16199 21 70 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 !p->pBt->autoVac
1619a 75 75 6d 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 uum)?BTREE_AUTOV
1619b 41 43 55 55 4d 5f 4e 4f 4e 45 3a 0a 20 20 20 20 ACUUM_NONE:.
1619c 28 21 70 2d 3e 70 42 74 2d 3e 69 6e 63 72 56 61 (!p->pBt->incrVa
1619d 63 75 75 6d 29 3f 42 54 52 45 45 5f 41 55 54 4f cuum)?BTREE_AUTO
1619e 56 41 43 55 55 4d 5f 46 55 4c 4c 3a 0a 20 20 20 VACUUM_FULL:.
1619f 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 BTREE_AUTOVACUU
161a0 4d 5f 49 4e 43 52 0a 20 20 29 3b 0a 20 20 73 71 M_INCR. );. sq
161a1 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
161a2 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
161a3 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a .#endif.}.../*.*
161a4 2a 20 47 65 74 20 61 20 72 65 66 65 72 65 6e 63 * Get a referenc
161a5 65 20 74 6f 20 70 50 61 67 65 31 20 6f 66 20 74 e to pPage1 of t
161a6 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
161a7 2e 20 20 54 68 69 73 20 77 69 6c 6c 0a 2a 2a 20 . This will.**
161a8 61 6c 73 6f 20 61 63 71 75 69 72 65 20 61 20 72 also acquire a r
161a9 65 61 64 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20 eadlock on that
161aa 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 file..**.** SQLI
161ab 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
161ac 64 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 d on success. I
161ad 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 6e 6f f the file is no
161ae 74 20 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d t a.** well-form
161af 65 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ed database file
161b0 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 43 4f , then SQLITE_CO
161b1 52 52 55 50 54 20 69 73 20 72 65 74 75 72 6e 65 RRUPT is returne
161b2 64 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 d..** SQLITE_BUS
161b3 59 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 Y is returned if
161b4 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
161b5 20 6c 6f 63 6b 65 64 2e 20 20 53 51 4c 49 54 45 locked. SQLITE
161b6 5f 4e 4f 4d 45 4d 0a 2a 2a 20 69 73 20 72 65 74 _NOMEM.** is ret
161b7 75 72 6e 65 64 20 69 66 20 77 65 20 72 75 6e 20 urned if we run
161b8 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 0a out of memory. .
161b9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f */.static int lo
161ba 63 6b 42 74 72 65 65 28 42 74 53 68 61 72 65 64 ckBtree(BtShared
161bb 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 72 63 *pBt){. int rc
161bc 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
161bd 67 65 31 3b 0a 20 20 69 6e 74 20 6e 50 61 67 65 ge1;. int nPage
161be 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
161bf 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
161c0 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
161c1 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 assert( pBt->pP
161c2 61 67 65 31 3d 3d 30 20 29 3b 0a 20 20 72 63 20 age1==0 );. rc
161c3 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 68 = sqlite3PagerSh
161c4 61 72 65 64 4c 6f 63 6b 28 70 42 74 2d 3e 70 50 aredLock(pBt->pP
161c5 61 67 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 ager);. if( rc!
161c6 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 =SQLITE_OK ) ret
161c7 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 62 urn rc;. rc = b
161c8 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
161c9 20 31 2c 20 26 70 50 61 67 65 31 2c 20 30 29 3b 1, &pPage1, 0);
161ca 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
161cb 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 E_OK ) return rc
161cc 3b 0a 0a 20 20 2f 2a 20 44 6f 20 73 6f 6d 65 20 ;.. /* Do some
161cd 63 68 65 63 6b 69 6e 67 20 74 6f 20 68 65 6c 70 checking to help
161ce 20 69 6e 73 75 72 65 20 74 68 65 20 66 69 6c 65 insure the file
161cf 20 77 65 20 6f 70 65 6e 65 64 20 72 65 61 6c 6c we opened reall
161d0 79 20 69 73 0a 20 20 2a 2a 20 61 20 76 61 6c 69 y is. ** a vali
161d1 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e d database file.
161d2 20 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 . */. rc = sq
161d3 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f lite3PagerPageco
161d4 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c unt(pBt->pPager,
161d5 20 26 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20 &nPage);. if(
161d6 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
161d7 0a 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f . goto page1_
161d8 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 7d init_failed;. }
161d9 65 6c 73 65 20 69 66 28 20 6e 50 61 67 65 3e 30 else if( nPage>0
161da 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 61 67 65 ){. int page
161db 53 69 7a 65 3b 0a 20 20 20 20 69 6e 74 20 75 73 Size;. int us
161dc 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 75 38 ableSize;. u8
161dd 20 2a 70 61 67 65 31 20 3d 20 70 50 61 67 65 31 *page1 = pPage1
161de 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 72 63 20 ->aData;. rc
161df 3d 20 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 3b = SQLITE_NOTADB;
161e0 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 . if( memcmp(
161e1 70 61 67 65 31 2c 20 7a 4d 61 67 69 63 48 65 61 page1, zMagicHea
161e2 64 65 72 2c 20 31 36 29 21 3d 30 20 29 7b 0a 20 der, 16)!=0 ){.
161e3 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f goto page1_
161e4 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 init_failed;.
161e5 20 7d 0a 20 20 20 20 69 66 28 20 70 61 67 65 31 }. if( page1
161e6 5b 31 38 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 [18]>1 ){.
161e7 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 pBt->readOnly =
161e8 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 1;. }. if(
161e9 20 70 61 67 65 31 5b 31 39 5d 3e 31 20 29 7b 0a page1[19]>1 ){.
161ea 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 goto page1
161eb 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 _init_failed;.
161ec 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 }.. /* The
161ed 6d 61 78 69 6d 75 6d 20 65 6d 62 65 64 64 65 64 maximum embedded
161ee 20 66 72 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 fraction must b
161ef 65 20 65 78 61 63 74 6c 79 20 32 35 25 2e 20 20 e exactly 25%.
161f0 41 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a And the minimum.
161f1 20 20 20 20 2a 2a 20 65 6d 62 65 64 64 65 64 20 ** embedded
161f2 66 72 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 fraction must be
161f3 20 31 32 2e 35 25 20 66 6f 72 20 62 6f 74 68 20 12.5% for both
161f4 6c 65 61 66 2d 64 61 74 61 20 61 6e 64 20 6e 6f leaf-data and no
161f5 6e 2d 6c 65 61 66 2d 64 61 74 61 2e 0a 20 20 20 n-leaf-data..
161f6 20 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c ** The original
161f7 20 64 65 73 69 67 6e 20 61 6c 6c 6f 77 65 64 20 design allowed
161f8 74 68 65 73 65 20 61 6d 6f 75 6e 74 73 20 74 6f these amounts to
161f9 20 76 61 72 79 2c 20 62 75 74 20 61 73 20 6f 66 vary, but as of
161fa 0a 20 20 20 20 2a 2a 20 76 65 72 73 69 6f 6e 20 . ** version
161fb 33 2e 36 2e 30 2c 20 77 65 20 72 65 71 75 69 72 3.6.0, we requir
161fc 65 20 74 68 65 6d 20 74 6f 20 62 65 20 66 69 78 e them to be fix
161fd 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ed.. */. i
161fe 66 28 20 6d 65 6d 63 6d 70 28 26 70 61 67 65 31 f( memcmp(&page1
161ff 5b 32 31 5d 2c 20 22 5c 31 30 30 5c 30 34 30 5c [21], "\100\040\
16200 30 34 30 22 2c 33 29 21 3d 30 20 29 7b 0a 20 20 040",3)!=0 ){.
16201 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 goto page1_i
16202 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 nit_failed;.
16203 7d 0a 20 20 20 20 70 61 67 65 53 69 7a 65 20 3d }. pageSize =
16204 20 67 65 74 32 62 79 74 65 28 26 70 61 67 65 31 get2byte(&page1
16205 5b 31 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 28 [16]);. if( (
16206 28 70 61 67 65 53 69 7a 65 2d 31 29 26 70 61 67 (pageSize-1)&pag
16207 65 53 69 7a 65 29 21 3d 30 20 7c 7c 20 70 61 67 eSize)!=0 || pag
16208 65 53 69 7a 65 3c 35 31 32 20 7c 7c 0a 20 20 20 eSize<512 ||.
16209 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4d 41 58 (SQLITE_MAX
1620a 5f 50 41 47 45 5f 53 49 5a 45 3c 33 32 37 36 38 _PAGE_SIZE<32768
1620b 20 26 26 20 70 61 67 65 53 69 7a 65 3e 53 51 4c && pageSize>SQL
1620c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a ITE_MAX_PAGE_SIZ
1620d 45 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 E). ){.
1620e 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f goto page1_init_
1620f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 failed;. }.
16210 20 20 61 73 73 65 72 74 28 20 28 70 61 67 65 53 assert( (pageS
16211 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 ize & 7)==0 );.
16212 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 usableSize =
16213 70 61 67 65 53 69 7a 65 20 2d 20 70 61 67 65 31 pageSize - page1
16214 5b 32 30 5d 3b 0a 20 20 20 20 69 66 28 20 70 61 [20];. if( pa
16215 67 65 53 69 7a 65 21 3d 70 42 74 2d 3e 70 61 67 geSize!=pBt->pag
16216 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 2f eSize ){. /
16217 2a 20 41 66 74 65 72 20 72 65 61 64 69 6e 67 20 * After reading
16218 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f the first page o
16219 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 f the database a
1621a 73 73 75 6d 69 6e 67 20 61 20 70 61 67 65 20 73 ssuming a page s
1621b 69 7a 65 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 ize. ** of
1621c 42 74 53 68 61 72 65 64 2e 70 61 67 65 53 69 7a BtShared.pageSiz
1621d 65 2c 20 77 65 20 68 61 76 65 20 64 69 73 63 6f e, we have disco
1621e 76 65 72 65 64 20 74 68 61 74 20 74 68 65 20 70 vered that the p
1621f 61 67 65 2d 73 69 7a 65 20 69 73 0a 20 20 20 20 age-size is.
16220 20 20 2a 2a 20 61 63 74 75 61 6c 6c 79 20 70 61 ** actually pa
16221 67 65 53 69 7a 65 2e 20 55 6e 6c 6f 63 6b 20 74 geSize. Unlock t
16222 68 65 20 64 61 74 61 62 61 73 65 2c 20 6c 65 61 he database, lea
16223 76 65 20 70 42 74 2d 3e 70 50 61 67 65 31 20 61 ve pBt->pPage1 a
16224 74 0a 20 20 20 20 20 20 2a 2a 20 7a 65 72 6f 20 t. ** zero
16225 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 and return SQLIT
16226 45 5f 4f 4b 2e 20 54 68 65 20 63 61 6c 6c 65 72 E_OK. The caller
16227 20 77 69 6c 6c 20 63 61 6c 6c 20 74 68 69 73 20 will call this
16228 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a function. *
16229 2a 20 61 67 61 69 6e 20 77 69 74 68 20 74 68 65 * again with the
1622a 20 63 6f 72 72 65 63 74 20 70 61 67 65 2d 73 69 correct page-si
1622b 7a 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ze.. */.
1622c 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
1622d 50 61 67 65 31 29 3b 0a 20 20 20 20 20 20 70 42 Page1);. pB
1622e 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 t->usableSize =
1622f 28 75 31 36 29 75 73 61 62 6c 65 53 69 7a 65 3b (u16)usableSize;
16230 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65 . pBt->page
16231 53 69 7a 65 20 3d 20 28 75 31 36 29 70 61 67 65 Size = (u16)page
16232 53 69 7a 65 3b 0a 20 20 20 20 20 20 66 72 65 65 Size;. free
16233 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a TempSpace(pBt);.
16234 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
16235 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 e3PagerSetPagesi
16236 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 ze(pBt->pPager,
16237 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2c 0a &pBt->pageSize,.
16238 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16239 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1623a 20 20 20 70 61 67 65 53 69 7a 65 2d 75 73 61 62 pageSize-usab
1623b 6c 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 72 leSize);. r
1623c 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
1623d 20 20 20 20 69 66 28 20 75 73 61 62 6c 65 53 69 if( usableSi
1623e 7a 65 3c 34 38 30 20 29 7b 0a 20 20 20 20 20 20 ze<480 ){.
1623f 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f goto page1_init_
16240 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 failed;. }.
16241 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 pBt->pageSize
16242 3d 20 28 75 31 36 29 70 61 67 65 53 69 7a 65 3b = (u16)pageSize;
16243 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 . pBt->usable
16244 53 69 7a 65 20 3d 20 28 75 31 36 29 75 73 61 62 Size = (u16)usab
16245 6c 65 53 69 7a 65 3b 0a 23 69 66 6e 64 65 66 20 leSize;.#ifndef
16246 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
16247 56 41 43 55 55 4d 0a 20 20 20 20 70 42 74 2d 3e VACUUM. pBt->
16248 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 autoVacuum = (ge
16249 74 34 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 t4byte(&page1[36
1624a 20 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 + 4*4])?1:0);.
1624b 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 pBt->incrVacu
1624c 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 um = (get4byte(&
1624d 70 61 67 65 31 5b 33 36 20 2b 20 37 2a 34 5d 29 page1[36 + 7*4])
1624e 3f 31 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 ?1:0);.#endif.
1624f 7d 0a 0a 20 20 2f 2a 20 6d 61 78 4c 6f 63 61 6c }.. /* maxLocal
16250 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 is the maximum
16251 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 amount of payloa
16252 64 20 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c d to store local
16253 6c 79 20 66 6f 72 0a 20 20 2a 2a 20 61 20 63 65 ly for. ** a ce
16254 6c 6c 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 69 ll. Make sure i
16255 74 20 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 t is small enoug
16256 68 20 73 6f 20 74 68 61 74 20 61 74 20 6c 65 61 h so that at lea
16257 73 74 20 6d 69 6e 46 61 6e 6f 75 74 0a 20 20 2a st minFanout. *
16258 2a 20 63 65 6c 6c 73 20 63 61 6e 20 77 69 6c 6c * cells can will
16259 20 66 69 74 20 6f 6e 20 6f 6e 65 20 70 61 67 65 fit on one page
1625a 2e 20 20 57 65 20 61 73 73 75 6d 65 20 61 20 31 . We assume a 1
1625b 30 2d 62 79 74 65 20 70 61 67 65 20 68 65 61 64 0-byte page head
1625c 65 72 2e 0a 20 20 2a 2a 20 42 65 73 69 64 65 73 er.. ** Besides
1625d 20 74 68 65 20 70 61 79 6c 6f 61 64 2c 20 74 68 the payload, th
1625e 65 20 63 65 6c 6c 20 6d 75 73 74 20 73 74 6f 72 e cell must stor
1625f 65 3a 0a 20 20 2a 2a 20 20 20 20 20 32 2d 62 79 e:. ** 2-by
16260 74 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 te pointer to th
16261 65 20 63 65 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 e cell. **
16262 34 2d 62 79 74 65 20 63 68 69 6c 64 20 70 6f 69 4-byte child poi
16263 6e 74 65 72 0a 20 20 2a 2a 20 20 20 20 20 39 2d nter. ** 9-
16264 62 79 74 65 20 6e 4b 65 79 20 76 61 6c 75 65 0a byte nKey value.
16265 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 20 ** 4-byte
16266 6e 44 61 74 61 20 76 61 6c 75 65 0a 20 20 2a 2a nData value. **
16267 20 20 20 20 20 34 2d 62 79 74 65 20 6f 76 65 72 4-byte over
16268 66 6c 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 flow page pointe
16269 72 0a 20 20 2a 2a 20 53 6f 20 61 20 63 65 6c 6c r. ** So a cell
1626a 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 32 consists of a 2
1626b 2d 62 79 74 65 20 70 6f 69 6e 65 72 2c 20 61 20 -byte poiner, a
1626c 68 65 61 64 65 72 20 77 68 69 63 68 20 69 73 20 header which is
1626d 61 73 20 6d 75 63 68 20 61 73 0a 20 20 2a 2a 20 as much as. **
1626e 31 37 20 62 79 74 65 73 20 6c 6f 6e 67 2c 20 30 17 bytes long, 0
1626f 20 74 6f 20 4e 20 62 79 74 65 73 20 6f 66 20 70 to N bytes of p
16270 61 79 6c 6f 61 64 2c 20 61 6e 64 20 61 6e 20 6f ayload, and an o
16271 70 74 69 6f 6e 61 6c 20 34 20 62 79 74 65 20 6f ptional 4 byte o
16272 76 65 72 66 6c 6f 77 0a 20 20 2a 2a 20 70 61 67 verflow. ** pag
16273 65 20 70 6f 69 6e 74 65 72 2e 0a 20 20 2a 2f 0a e pointer.. */.
16274 20 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20 pBt->maxLocal
16275 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 = (pBt->usableSi
16276 7a 65 2d 31 32 29 2a 36 34 2f 32 35 35 20 2d 20 ze-12)*64/255 -
16277 32 33 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 6f 23;. pBt->minLo
16278 63 61 6c 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 cal = (pBt->usab
16279 6c 65 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32 35 leSize-12)*32/25
1627a 35 20 2d 20 32 33 3b 0a 20 20 70 42 74 2d 3e 6d 5 - 23;. pBt->m
1627b 61 78 4c 65 61 66 20 3d 20 70 42 74 2d 3e 75 73 axLeaf = pBt->us
1627c 61 62 6c 65 53 69 7a 65 20 2d 20 33 35 3b 0a 20 ableSize - 35;.
1627d 20 70 42 74 2d 3e 6d 69 6e 4c 65 61 66 20 3d 20 pBt->minLeaf =
1627e 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 (pBt->usableSize
1627f 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 -12)*32/255 - 23
16280 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d ;. assert( pBt-
16281 3e 6d 61 78 4c 65 61 66 20 2b 20 32 33 20 3c 3d >maxLeaf + 23 <=
16282 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 MX_CELL_SIZE(pB
16283 74 29 20 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 t) );. pBt->pPa
16284 67 65 31 20 3d 20 70 50 61 67 65 31 3b 0a 20 20 ge1 = pPage1;.
16285 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
16286 3b 0a 0a 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 ;..page1_init_fa
16287 69 6c 65 64 3a 0a 20 20 72 65 6c 65 61 73 65 50 iled:. releaseP
16288 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 70 age(pPage1);. p
16289 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a Bt->pPage1 = 0;.
1628a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1628b 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 /*.** If there a
1628c 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e re no outstandin
1628d 67 20 63 75 72 73 6f 72 73 20 61 6e 64 20 77 65 g cursors and we
1628e 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 20 are not in the
1628f 6d 69 64 64 6c 65 0a 2a 2a 20 6f 66 20 61 20 74 middle.** of a t
16290 72 61 6e 73 61 63 74 69 6f 6e 20 62 75 74 20 74 ransaction but t
16291 68 65 72 65 20 69 73 20 61 20 72 65 61 64 20 6c here is a read l
16292 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
16293 61 73 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 ase, then.** thi
16294 73 20 72 6f 75 74 69 6e 65 20 75 6e 72 65 66 73 s routine unrefs
16295 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 the first page
16296 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
16297 66 69 6c 65 20 77 68 69 63 68 20 0a 2a 2a 20 68 file which .** h
16298 61 73 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 as the effect of
16299 20 72 65 6c 65 61 73 69 6e 67 20 74 68 65 20 72 releasing the r
1629a 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 ead lock..**.**
1629b 49 66 20 74 68 65 72 65 20 69 73 20 61 20 74 72 If there is a tr
1629c 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f ansaction in pro
1629d 67 72 65 73 73 2c 20 74 68 69 73 20 72 6f 75 74 gress, this rout
1629e 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ine is a no-op..
1629f 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 */.static void u
162a0 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 nlockBtreeIfUnus
162a1 65 64 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ed(BtShared *pBt
162a2 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
162a3 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
162a4 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
162a5 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 43 assert( pBt->pC
162a6 75 72 73 6f 72 3d 3d 30 20 7c 7c 20 70 42 74 2d ursor==0 || pBt-
162a7 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 54 >inTransaction>T
162a8 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 69 RANS_NONE );. i
162a9 66 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 f( pBt->inTransa
162aa 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e ction==TRANS_NON
162ab 45 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 31 E && pBt->pPage1
162ac 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 !=0 ){. asser
162ad 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e t( pBt->pPage1->
162ae 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73 aData );. ass
162af 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
162b0 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 rRefcount(pBt->p
162b1 50 61 67 65 72 29 3d 3d 31 20 29 3b 0a 20 20 20 Pager)==1 );.
162b2 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 assert( pBt->pP
162b3 61 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a 20 age1->aData );.
162b4 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
162b5 42 74 2d 3e 70 50 61 67 65 31 29 3b 0a 20 20 20 Bt->pPage1);.
162b6 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 pBt->pPage1 = 0
162b7 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ;. }.}../*.** I
162b8 66 20 70 42 74 20 70 6f 69 6e 74 73 20 74 6f 20 f pBt points to
162b9 61 6e 20 65 6d 70 74 79 20 66 69 6c 65 20 74 68 an empty file th
162ba 65 6e 20 63 6f 6e 76 65 72 74 20 74 68 61 74 20 en convert that
162bb 65 6d 70 74 79 20 66 69 6c 65 0a 2a 2a 20 69 6e empty file.** in
162bc 74 6f 20 61 20 6e 65 77 20 65 6d 70 74 79 20 64 to a new empty d
162bd 61 74 61 62 61 73 65 20 62 79 20 69 6e 69 74 69 atabase by initi
162be 61 6c 69 7a 69 6e 67 20 74 68 65 20 66 69 72 73 alizing the firs
162bf 74 20 70 61 67 65 20 6f 66 0a 2a 2a 20 74 68 65 t page of.** the
162c0 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 database..*/.st
162c1 61 74 69 63 20 69 6e 74 20 6e 65 77 44 61 74 61 atic int newData
162c2 62 61 73 65 28 42 74 53 68 61 72 65 64 20 2a 70 base(BtShared *p
162c3 42 74 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a Bt){. MemPage *
162c4 70 50 31 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 pP1;. unsigned
162c5 63 68 61 72 20 2a 64 61 74 61 3b 0a 20 20 69 6e char *data;. in
162c6 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e 50 61 67 t rc;. int nPag
162c7 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 e;.. assert( sq
162c8 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
162c9 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
162ca 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 /* The databas
162cb 65 20 73 69 7a 65 20 68 61 73 20 61 6c 72 65 61 e size has alrea
162cc 64 79 20 62 65 65 6e 20 6d 65 61 73 75 72 65 64 dy been measured
162cd 20 61 6e 64 20 63 61 63 68 65 64 2c 20 73 6f 20 and cached, so
162ce 66 61 69 6c 75 72 65 0a 20 20 2a 2a 20 69 73 20 failure. ** is
162cf 69 6d 70 6f 73 73 69 62 6c 65 20 68 65 72 65 2e impossible here.
162d0 20 20 49 66 20 74 68 65 20 6f 72 69 67 69 6e 61 If the origina
162d1 6c 20 73 69 7a 65 20 6d 65 61 73 75 72 65 6d 65 l size measureme
162d2 6e 74 20 66 61 69 6c 65 64 2c 20 74 68 65 6e 0a nt failed, then.
162d3 20 20 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 ** processing
162d4 61 62 6f 72 74 73 20 62 65 66 6f 72 65 20 65 6e aborts before en
162d5 74 65 72 69 6e 67 20 74 68 69 73 20 72 6f 75 74 tering this rout
162d6 69 6e 65 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 ine. */. rc = s
162d7 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 qlite3PagerPagec
162d8 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 ount(pBt->pPager
162d9 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 , &nPage);. if(
162da 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54 NEVER(rc!=SQLIT
162db 45 5f 4f 4b 29 20 7c 7c 20 6e 50 61 67 65 3e 30 E_OK) || nPage>0
162dc 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
162dd 63 3b 0a 20 20 7d 0a 20 20 70 50 31 20 3d 20 70 c;. }. pP1 = p
162de 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20 61 73 Bt->pPage1;. as
162df 73 65 72 74 28 20 70 50 31 21 3d 30 20 29 3b 0a sert( pP1!=0 );.
162e0 20 20 64 61 74 61 20 3d 20 70 50 31 2d 3e 61 44 data = pP1->aD
162e1 61 74 61 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 ata;. rc = sqli
162e2 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
162e3 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 1->pDbPage);. i
162e4 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
162e5 63 3b 0a 20 20 6d 65 6d 63 70 79 28 64 61 74 61 c;. memcpy(data
162e6 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 2c 20 , zMagicHeader,
162e7 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48 65 61 sizeof(zMagicHea
162e8 64 65 72 29 29 3b 0a 20 20 61 73 73 65 72 74 28 der));. assert(
162e9 20 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48 65 sizeof(zMagicHe
162ea 61 64 65 72 29 3d 3d 31 36 20 29 3b 0a 20 20 70 ader)==16 );. p
162eb 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 31 36 ut2byte(&data[16
162ec 5d 2c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 ], pBt->pageSize
162ed 29 3b 0a 20 20 64 61 74 61 5b 31 38 5d 20 3d 20 );. data[18] =
162ee 31 3b 0a 20 20 64 61 74 61 5b 31 39 5d 20 3d 20 1;. data[19] =
162ef 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 1;. assert( pBt
162f0 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3c 3d 70 42 ->usableSize<=pB
162f1 74 2d 3e 70 61 67 65 53 69 7a 65 20 26 26 20 70 t->pageSize && p
162f2 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2b 32 Bt->usableSize+2
162f3 35 35 3e 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 55>=pBt->pageSiz
162f4 65 29 3b 0a 20 20 64 61 74 61 5b 32 30 5d 20 3d e);. data[20] =
162f5 20 28 75 38 29 28 70 42 74 2d 3e 70 61 67 65 53 (u8)(pBt->pageS
162f6 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c ize - pBt->usabl
162f7 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b 32 eSize);. data[2
162f8 31 5d 20 3d 20 36 34 3b 0a 20 20 64 61 74 61 5b 1] = 64;. data[
162f9 32 32 5d 20 3d 20 33 32 3b 0a 20 20 64 61 74 61 22] = 32;. data
162fa 5b 32 33 5d 20 3d 20 33 32 3b 0a 20 20 6d 65 6d [23] = 32;. mem
162fb 73 65 74 28 26 64 61 74 61 5b 32 34 5d 2c 20 30 set(&data[24], 0
162fc 2c 20 31 30 30 2d 32 34 29 3b 0a 20 20 7a 65 72 , 100-24);. zer
162fd 6f 50 61 67 65 28 70 50 31 2c 20 50 54 46 5f 49 oPage(pP1, PTF_I
162fe 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 7c 50 NTKEY|PTF_LEAF|P
162ff 54 46 5f 4c 45 41 46 44 41 54 41 20 29 3b 0a 20 TF_LEAFDATA );.
16300 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 pBt->pageSizeFi
16301 78 65 64 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 xed = 1;.#ifndef
16302 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
16303 4f 56 41 43 55 55 4d 0a 20 20 61 73 73 65 72 74 OVACUUM. assert
16304 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
16305 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 61 75 74 m==1 || pBt->aut
16306 6f 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 oVacuum==0 );.
16307 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 63 assert( pBt->inc
16308 72 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 rVacuum==1 || pB
16309 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d 30 t->incrVacuum==0
1630a 20 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 );. put4byte(&
1630b 64 61 74 61 5b 33 36 20 2b 20 34 2a 34 5d 2c 20 data[36 + 4*4],
1630c 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 pBt->autoVacuum)
1630d 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 64 61 ;. put4byte(&da
1630e 74 61 5b 33 36 20 2b 20 37 2a 34 5d 2c 20 70 42 ta[36 + 7*4], pB
1630f 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 3b 0a t->incrVacuum);.
16310 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
16311 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
16312 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73 .** Attempt to s
16313 74 61 72 74 20 61 20 6e 65 77 20 74 72 61 6e 73 tart a new trans
16314 61 63 74 69 6f 6e 2e 20 41 20 77 72 69 74 65 2d action. A write-
16315 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 transaction.** i
16316 73 20 73 74 61 72 74 65 64 20 69 66 20 74 68 65 s started if the
16317 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
16318 20 69 73 20 6e 6f 6e 7a 65 72 6f 2c 20 6f 74 68 is nonzero, oth
16319 65 72 77 69 73 65 20 61 20 72 65 61 64 2d 0a 2a erwise a read-.*
1631a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 * transaction.
1631b 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 If the second ar
1631c 67 75 6d 65 6e 74 20 69 73 20 32 20 6f 72 20 6d gument is 2 or m
1631d 6f 72 65 20 61 6e 64 20 65 78 63 6c 75 73 69 76 ore and exclusiv
1631e 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e e.** transaction
1631f 20 69 73 20 73 74 61 72 74 65 64 2c 20 6d 65 61 is started, mea
16320 6e 69 6e 67 20 74 68 61 74 20 6e 6f 20 6f 74 68 ning that no oth
16321 65 72 20 70 72 6f 63 65 73 73 20 69 73 20 61 6c er process is al
16322 6c 6f 77 65 64 0a 2a 2a 20 74 6f 20 61 63 63 65 lowed.** to acce
16323 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 2e ss the database.
16324 20 20 41 20 70 72 65 65 78 69 73 74 69 6e 67 20 A preexisting
16325 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 transaction may
16326 6e 6f 74 20 62 65 0a 2a 2a 20 75 70 67 72 61 64 not be.** upgrad
16327 65 64 20 74 6f 20 65 78 63 6c 75 73 69 76 65 20 ed to exclusive
16328 62 79 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 by calling this
16329 72 6f 75 74 69 6e 65 20 61 20 73 65 63 6f 6e 64 routine a second
1632a 20 74 69 6d 65 20 2d 20 74 68 65 0a 2a 2a 20 65 time - the.** e
1632b 78 63 6c 75 73 69 76 69 74 79 20 66 6c 61 67 20 xclusivity flag
1632c 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 61 only works for a
1632d 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e new transaction
1632e 2e 0a 2a 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d ..**.** A write-
1632f 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 transaction must
16330 20 62 65 20 73 74 61 72 74 65 64 20 62 65 66 6f be started befo
16331 72 65 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e re attempting an
16332 79 20 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f y .** changes to
16333 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 the database.
16334 4e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c None of the foll
16335 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 0a owing routines .
16336 2a 2a 20 77 69 6c 6c 20 77 6f 72 6b 20 75 6e 6c ** will work unl
16337 65 73 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f ess a transactio
16338 6e 20 69 73 20 73 74 61 72 74 65 64 20 66 69 72 n is started fir
16339 73 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 73 st:.**.** s
1633a 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 qlite3BtreeCreat
1633b 65 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 eTable().**
1633c 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 sqlite3BtreeCre
1633d 61 74 65 49 6e 64 65 78 28 29 0a 2a 2a 20 20 20 ateIndex().**
1633e 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 sqlite3BtreeC
1633f 6c 65 61 72 54 61 62 6c 65 28 29 0a 2a 2a 20 20 learTable().**
16340 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
16341 44 72 6f 70 54 61 62 6c 65 28 29 0a 2a 2a 20 20 DropTable().**
16342 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
16343 49 6e 73 65 72 74 28 29 0a 2a 2a 20 20 20 20 20 Insert().**
16344 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c sqlite3BtreeDel
16345 65 74 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 ete().** sq
16346 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 lite3BtreeUpdate
16347 4d 65 74 61 28 29 0a 2a 2a 0a 2a 2a 20 49 66 20 Meta().**.** If
16348 61 6e 20 69 6e 69 74 69 61 6c 20 61 74 74 65 6d an initial attem
16349 70 74 20 74 6f 20 61 63 71 75 69 72 65 20 74 68 pt to acquire th
1634a 65 20 6c 6f 63 6b 20 66 61 69 6c 73 20 62 65 63 e lock fails bec
1634b 61 75 73 65 20 6f 66 20 6c 6f 63 6b 20 63 6f 6e ause of lock con
1634c 74 65 6e 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 74 tention.** and t
1634d 68 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20 he database was
1634e 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 6c 6f 63 previously unloc
1634f 6b 65 64 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 ked, then invoke
16350 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
16351 72 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73 r.** if there is
16352 20 6f 6e 65 2e 20 20 42 75 74 20 69 66 20 74 68 one. But if th
16353 65 72 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 ere was previous
16354 6c 79 20 61 20 72 65 61 64 2d 6c 6f 63 6b 2c 20 ly a read-lock,
16355 64 6f 20 6e 6f 74 0a 2a 2a 20 69 6e 76 6f 6b 65 do not.** invoke
16356 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
16357 72 20 2d 20 6a 75 73 74 20 72 65 74 75 72 6e 20 r - just return
16358 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 53 51 SQLITE_BUSY. SQ
16359 4c 49 54 45 5f 42 55 53 59 20 69 73 20 0a 2a 2a LITE_BUSY is .**
1635a 20 72 65 74 75 72 6e 65 64 20 77 68 65 6e 20 74 returned when t
1635b 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 here is already
1635c 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 6e 20 6f a read-lock in o
1635d 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 61 20 rder to avoid a
1635e 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 deadlock..**.**
1635f 53 75 70 70 6f 73 65 20 74 68 65 72 65 20 61 72 Suppose there ar
16360 65 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 e two processes
16361 41 20 61 6e 64 20 42 2e 20 20 41 20 68 61 73 20 A and B. A has
16362 61 20 72 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 a read lock and
16363 42 20 68 61 73 0a 2a 2a 20 61 20 72 65 73 65 72 B has.** a reser
16364 76 65 64 20 6c 6f 63 6b 2e 20 20 42 20 74 72 69 ved lock. B tri
16365 65 73 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f es to promote to
16366 20 65 78 63 6c 75 73 69 76 65 20 62 75 74 20 69 exclusive but i
16367 73 20 62 6c 6f 63 6b 65 64 20 62 65 63 61 75 73 s blocked becaus
16368 65 0a 2a 2a 20 6f 66 20 41 27 73 20 72 65 61 64 e.** of A's read
16369 20 6c 6f 63 6b 2e 20 20 41 20 74 72 69 65 73 20 lock. A tries
1636a 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 72 65 to promote to re
1636b 73 65 72 76 65 64 20 62 75 74 20 69 73 20 62 6c served but is bl
1636c 6f 63 6b 65 64 20 62 79 20 42 2e 0a 2a 2a 20 4f ocked by B..** O
1636d 6e 65 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20 ne or the other
1636e 6f 66 20 74 68 65 20 74 77 6f 20 70 72 6f 63 65 of the two proce
1636f 73 73 65 73 20 6d 75 73 74 20 67 69 76 65 20 77 sses must give w
16370 61 79 20 6f 72 20 74 68 65 72 65 20 63 61 6e 20 ay or there can
16371 62 65 0a 2a 2a 20 6e 6f 20 70 72 6f 67 72 65 73 be.** no progres
16372 73 2e 20 20 42 79 20 72 65 74 75 72 6e 69 6e 67 s. By returning
16373 20 53 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 SQLITE_BUSY and
16374 20 6e 6f 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 not invoking th
16375 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a e busy callback.
16376 2a 2a 20 77 68 65 6e 20 41 20 61 6c 72 65 61 64 ** when A alread
16377 79 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 y has a read loc
16378 6b 2c 20 77 65 20 65 6e 63 6f 75 72 61 67 65 20 k, we encourage
16379 41 20 74 6f 20 67 69 76 65 20 75 70 20 61 6e 64 A to give up and
1637a 20 6c 65 74 20 42 0a 2a 2a 20 70 72 6f 63 65 65 let B.** procee
1637b 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
1637c 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1637d 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 BtreeBeginTrans(
1637e 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 77 72 Btree *p, int wr
1637f 66 6c 61 67 29 7b 0a 20 20 73 71 6c 69 74 65 33 flag){. sqlite3
16380 20 2a 70 42 6c 6f 63 6b 20 3d 20 30 3b 0a 20 20 *pBlock = 0;.
16381 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
16382 70 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 p->pBt;. int rc
16383 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
16384 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
16385 65 72 28 70 29 3b 0a 20 20 62 74 72 65 65 49 6e er(p);. btreeIn
16386 74 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 2f tegrity(p);.. /
16387 2a 20 49 66 20 74 68 65 20 62 74 72 65 65 20 69 * If the btree i
16388 73 20 61 6c 72 65 61 64 79 20 69 6e 20 61 20 77 s already in a w
16389 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
1638a 2c 20 6f 72 20 69 74 0a 20 20 2a 2a 20 69 73 20 , or it. ** is
1638b 61 6c 72 65 61 64 79 20 69 6e 20 61 20 72 65 61 already in a rea
1638c 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e d-transaction an
1638d 64 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 d a read-transac
1638e 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 72 65 71 tion. ** is req
1638f 75 65 73 74 65 64 2c 20 74 68 69 73 20 69 73 20 uested, this is
16390 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2f 0a 20 20 a no-op.. */.
16391 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d if( p->inTrans==
16392 54 52 41 4e 53 5f 57 52 49 54 45 20 7c 7c 20 28 TRANS_WRITE || (
16393 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
16394 53 5f 52 45 41 44 20 26 26 20 21 77 72 66 6c 61 S_READ && !wrfla
16395 67 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 g) ){. goto t
16396 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a rans_begun;. }.
16397 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 72 61 6e . /* Write tran
16398 73 61 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 sactions are not
16399 20 70 6f 73 73 69 62 6c 65 20 6f 6e 20 61 20 72 possible on a r
1639a 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 ead-only databas
1639b 65 20 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e e */. if( pBt->
1639c 72 65 61 64 4f 6e 6c 79 20 26 26 20 77 72 66 6c readOnly && wrfl
1639d 61 67 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 ag ){. rc = S
1639e 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a QLITE_READONLY;.
1639f 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 goto trans_b
163a0 65 67 75 6e 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 egun;. }..#ifnd
163a1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
163a2 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 2f 2a HARED_CACHE. /*
163a3 20 49 66 20 61 6e 6f 74 68 65 72 20 64 61 74 61 If another data
163a4 62 61 73 65 20 68 61 6e 64 6c 65 20 68 61 73 20 base handle has
163a5 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 20 61 already opened a
163a6 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
163a7 6f 6e 20 0a 20 20 2a 2a 20 6f 6e 20 74 68 69 73 on . ** on this
163a8 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 73 74 shared-btree st
163a9 72 75 63 74 75 72 65 20 61 6e 64 20 61 20 73 65 ructure and a se
163aa 63 6f 6e 64 20 77 72 69 74 65 20 74 72 61 6e 73 cond write trans
163ab 61 63 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 72 action is. ** r
163ac 65 71 75 65 73 74 65 64 2c 20 72 65 74 75 72 6e equested, return
163ad 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a SQLITE_LOCKED..
163ae 20 20 2a 2f 0a 20 20 69 66 28 20 28 77 72 66 6c */. if( (wrfl
163af 61 67 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 ag && pBt->inTra
163b0 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f nsaction==TRANS_
163b1 57 52 49 54 45 29 20 7c 7c 20 70 42 74 2d 3e 69 WRITE) || pBt->i
163b2 73 50 65 6e 64 69 6e 67 20 29 7b 0a 20 20 20 20 sPending ){.
163b3 70 42 6c 6f 63 6b 20 3d 20 70 42 74 2d 3e 70 57 pBlock = pBt->pW
163b4 72 69 74 65 72 2d 3e 64 62 3b 0a 20 20 7d 65 6c riter->db;. }el
163b5 73 65 20 69 66 28 20 77 72 66 6c 61 67 3e 31 20 se if( wrflag>1
163b6 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 ){. BtLock *p
163b7 49 74 65 72 3b 0a 20 20 20 20 66 6f 72 28 70 49 Iter;. for(pI
163b8 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 ter=pBt->pLock;
163b9 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 pIter; pIter=pIt
163ba 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 er->pNext){.
163bb 20 20 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 if( pIter->pBt
163bc 72 65 65 21 3d 70 20 29 7b 0a 20 20 20 20 20 20 ree!=p ){.
163bd 20 20 70 42 6c 6f 63 6b 20 3d 20 70 49 74 65 72 pBlock = pIter
163be 2d 3e 70 42 74 72 65 65 2d 3e 64 62 3b 0a 20 20 ->pBtree->db;.
163bf 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
163c0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
163c1 20 69 66 28 20 70 42 6c 6f 63 6b 20 29 7b 0a 20 if( pBlock ){.
163c2 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 sqlite3Connec
163c3 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 tionBlocked(p->d
163c4 62 2c 20 70 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 b, pBlock);.
163c5 72 63 20 3d 20 53 51 4c 49 54 45 5f 4c 4f 43 4b rc = SQLITE_LOCK
163c6 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a ED_SHAREDCACHE;.
163c7 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 goto trans_b
163c8 65 67 75 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 egun;. }.#endif
163c9 0a 0a 20 20 2f 2a 20 41 6e 79 20 72 65 61 64 2d .. /* Any read-
163ca 6f 6e 6c 79 20 6f 72 20 72 65 61 64 2d 77 72 69 only or read-wri
163cb 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 te transaction i
163cc 6d 70 6c 69 65 73 20 61 20 72 65 61 64 2d 6c 6f mplies a read-lo
163cd 63 6b 20 6f 6e 20 0a 20 20 2a 2a 20 70 61 67 65 ck on . ** page
163ce 20 31 2e 20 53 6f 20 69 66 20 73 6f 6d 65 20 6f 1. So if some o
163cf 74 68 65 72 20 73 68 61 72 65 64 2d 63 61 63 68 ther shared-cach
163d0 65 20 63 6c 69 65 6e 74 20 61 6c 72 65 61 64 79 e client already
163d1 20 68 61 73 20 61 20 77 72 69 74 65 2d 6c 6f 63 has a write-loc
163d2 6b 20 0a 20 20 2a 2a 20 6f 6e 20 70 61 67 65 20 k . ** on page
163d3 31 2c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 1, the transacti
163d4 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 6f 70 65 on cannot be ope
163d5 6e 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 71 ned. */. rc = q
163d6 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 uerySharedCacheT
163d7 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 ableLock(p, MAST
163d8 45 52 5f 52 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f ER_ROOT, READ_LO
163d9 43 4b 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 CK);. if( SQLIT
163da 45 5f 4f 4b 21 3d 72 63 20 29 20 67 6f 74 6f 20 E_OK!=rc ) goto
163db 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 0a 20 20 trans_begun;..
163dc 64 6f 20 7b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c do {. /* Call
163dd 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 75 6e 74 lockBtree() unt
163de 69 6c 20 65 69 74 68 65 72 20 70 42 74 2d 3e 70 il either pBt->p
163df 50 61 67 65 31 20 69 73 20 70 6f 70 75 6c 61 74 Page1 is populat
163e0 65 64 20 6f 72 0a 20 20 20 20 2a 2a 20 6c 6f 63 ed or. ** loc
163e1 6b 42 74 72 65 65 28 29 20 72 65 74 75 72 6e 73 kBtree() returns
163e2 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 something other
163e3 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e than SQLITE_OK.
163e4 20 6c 6f 63 6b 42 74 72 65 65 28 29 0a 20 20 20 lockBtree().
163e5 20 2a 2a 20 6d 61 79 20 72 65 74 75 72 6e 20 53 ** may return S
163e6 51 4c 49 54 45 5f 4f 4b 20 62 75 74 20 6c 65 61 QLITE_OK but lea
163e7 76 65 20 70 42 74 2d 3e 70 50 61 67 65 31 20 73 ve pBt->pPage1 s
163e8 65 74 20 74 6f 20 30 20 69 66 20 61 66 74 65 72 et to 0 if after
163e9 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 . ** reading
163ea 70 61 67 65 20 31 20 69 74 20 64 69 73 63 6f 76 page 1 it discov
163eb 65 72 73 20 74 68 61 74 20 74 68 65 20 70 61 67 ers that the pag
163ec 65 2d 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 e-size of the da
163ed 74 61 62 61 73 65 20 0a 20 20 20 20 2a 2a 20 66 tabase . ** f
163ee 69 6c 65 20 69 73 20 6e 6f 74 20 70 42 74 2d 3e ile is not pBt->
163ef 70 61 67 65 53 69 7a 65 2e 20 49 6e 20 74 68 69 pageSize. In thi
163f0 73 20 63 61 73 65 20 6c 6f 63 6b 42 74 72 65 65 s case lockBtree
163f1 28 29 20 77 69 6c 6c 20 75 70 64 61 74 65 0a 20 () will update.
163f2 20 20 20 2a 2a 20 70 42 74 2d 3e 70 61 67 65 53 ** pBt->pageS
163f3 69 7a 65 20 74 6f 20 74 68 65 20 70 61 67 65 2d ize to the page-
163f4 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 size of the file
163f5 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 20 20 2a 2f on disk.. */
163f6 0a 20 20 20 20 77 68 69 6c 65 28 20 70 42 74 2d . while( pBt-
163f7 3e 70 50 61 67 65 31 3d 3d 30 20 26 26 20 53 51 >pPage1==0 && SQ
163f8 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 6c LITE_OK==(rc = l
163f9 6f 63 6b 42 74 72 65 65 28 70 42 74 29 29 20 29 ockBtree(pBt)) )
163fa 3b 0a 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 ;.. if( rc==S
163fb 51 4c 49 54 45 5f 4f 4b 20 26 26 20 77 72 66 6c QLITE_OK && wrfl
163fc 61 67 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ag ){. if(
163fd 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b pBt->readOnly ){
163fe 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
163ff 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 LITE_READONLY;.
16400 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
16401 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
16402 50 61 67 65 72 42 65 67 69 6e 28 70 42 74 2d 3e PagerBegin(pBt->
16403 70 50 61 67 65 72 2c 77 72 66 6c 61 67 3e 31 2c pPager,wrflag>1,
16404 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d sqlite3TempInMem
16405 6f 72 79 28 70 2d 3e 64 62 29 29 3b 0a 20 20 20 ory(p->db));.
16406 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
16407 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
16408 20 20 20 20 72 63 20 3d 20 6e 65 77 44 61 74 61 rc = newData
16409 62 61 73 65 28 70 42 74 29 3b 0a 20 20 20 20 20 base(pBt);.
1640a 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1640b 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 72 63 }. . if( rc
1640c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1640d 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 unlockBtree
1640e 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 IfUnused(pBt);.
1640f 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 72 }. }while( r
16410 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 c==SQLITE_BUSY &
16411 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 & pBt->inTransac
16412 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 tion==TRANS_NONE
16413 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 62 74 &&. bt
16414 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e reeInvokeBusyHan
16415 64 6c 65 72 28 70 42 74 29 20 29 3b 0a 0a 20 20 dler(pBt) );..
16416 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
16417 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e K ){. if( p->
16418 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 4e inTrans==TRANS_N
16419 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 70 42 74 ONE ){. pBt
1641a 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2b 2b ->nTransaction++
1641b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
1641c 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
1641d 48 45 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e HE. if( p->
1641e 73 68 61 72 61 62 6c 65 20 29 7b 0a 09 61 73 73 sharable ){..ass
1641f 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 ert( p->lock.pBt
16420 72 65 65 3d 3d 70 20 26 26 20 70 2d 3e 6c 6f 63 ree==p && p->loc
16421 6b 2e 69 54 61 62 6c 65 3d 3d 31 20 29 3b 0a 20 k.iTable==1 );.
16422 20 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e 65 p->lock.e
16423 4c 6f 63 6b 20 3d 20 52 45 41 44 5f 4c 4f 43 4b Lock = READ_LOCK
16424 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c 6f 63 ;. p->loc
16425 6b 2e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 k.pNext = pBt->p
16426 4c 6f 63 6b 3b 0a 20 20 20 20 20 20 20 20 70 42 Lock;. pB
16427 74 2d 3e 70 4c 6f 63 6b 20 3d 20 26 70 2d 3e 6c t->pLock = &p->l
16428 6f 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e ock;. }.#en
16429 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d dif. }. p-
1642a 3e 69 6e 54 72 61 6e 73 20 3d 20 28 77 72 66 6c >inTrans = (wrfl
1642b 61 67 3f 54 52 41 4e 53 5f 57 52 49 54 45 3a 54 ag?TRANS_WRITE:T
1642c 52 41 4e 53 5f 52 45 41 44 29 3b 0a 20 20 20 20 RANS_READ);.
1642d 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 70 if( p->inTrans>p
1642e 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
1642f 6e 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e n ){. pBt->
16430 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 inTransaction =
16431 70 2d 3e 69 6e 54 72 61 6e 73 3b 0a 20 20 20 20 p->inTrans;.
16432 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
16433 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
16434 48 45 0a 20 20 20 20 69 66 28 20 77 72 66 6c 61 HE. if( wrfla
16435 67 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 g ){. asser
16436 74 28 20 21 70 42 74 2d 3e 70 57 72 69 74 65 72 t( !pBt->pWriter
16437 20 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 );. pBt->p
16438 57 72 69 74 65 72 20 3d 20 70 3b 0a 20 20 20 20 Writer = p;.
16439 20 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 pBt->isExclusi
1643a 76 65 20 3d 20 28 75 38 29 28 77 72 66 6c 61 67 ve = (u8)(wrflag
1643b 3e 31 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 >1);. }.#endi
1643c 66 0a 20 20 7d 0a 0a 0a 74 72 61 6e 73 5f 62 65 f. }...trans_be
1643d 67 75 6e 3a 0a 20 20 69 66 28 20 72 63 3d 3d 53 gun:. if( rc==S
1643e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 77 72 66 6c QLITE_OK && wrfl
1643f 61 67 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 ag ){. /* Thi
16440 73 20 63 61 6c 6c 20 6d 61 6b 65 73 20 73 75 72 s call makes sur
16441 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72 e that the pager
16442 20 68 61 73 20 74 68 65 20 63 6f 72 72 65 63 74 has the correct
16443 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 2a number of. *
16444 2a 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 * open savepoint
16445 73 2e 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 s. If the second
16446 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 67 72 parameter is gr
16447 65 61 74 65 72 20 74 68 61 6e 20 30 20 61 6e 64 eater than 0 and
16448 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 75 62 2d . ** the sub-
16449 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 61 journal is not a
1644a 6c 72 65 61 64 79 20 6f 70 65 6e 2c 20 74 68 65 lready open, the
1644b 6e 20 69 74 20 77 69 6c 6c 20 62 65 20 6f 70 65 n it will be ope
1644c 6e 65 64 20 68 65 72 65 2e 0a 20 20 20 20 2a 2f ned here.. */
1644d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1644e 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 3PagerOpenSavepo
1644f 69 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c int(pBt->pPager,
16450 20 70 2d 3e 64 62 2d 3e 6e 53 61 76 65 70 6f 69 p->db->nSavepoi
16451 6e 74 29 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 nt);. }.. btre
16452 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 20 eIntegrity(p);.
16453 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
16454 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
16455 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 rc;.}..#ifndef S
16456 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
16457 41 43 55 55 4d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 ACUUM../*.** Set
16458 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 the pointer-map
16459 20 65 6e 74 72 69 65 73 20 66 6f 72 20 61 6c 6c entries for all
1645a 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 70 61 67 children of pag
1645b 65 20 70 50 61 67 65 2e 20 41 6c 73 6f 2c 20 69 e pPage. Also, i
1645c 66 0a 2a 2a 20 70 50 61 67 65 20 63 6f 6e 74 61 f.** pPage conta
1645d 69 6e 73 20 63 65 6c 6c 73 20 74 68 61 74 20 70 ins cells that p
1645e 6f 69 6e 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 oint to overflow
1645f 20 70 61 67 65 73 2c 20 73 65 74 20 74 68 65 20 pages, set the
16460 70 6f 69 6e 74 65 72 0a 2a 2a 20 6d 61 70 20 65 pointer.** map e
16461 6e 74 72 69 65 73 20 66 6f 72 20 74 68 65 20 6f ntries for the o
16462 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73 verflow pages as
16463 20 77 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 well..*/.static
16464 20 69 6e 74 20 73 65 74 43 68 69 6c 64 50 74 72 int setChildPtr
16465 6d 61 70 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 maps(MemPage *pP
16466 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 age){. int i;
16467 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16468 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
16469 75 6e 74 65 72 20 76 61 72 69 61 62 6c 65 20 2a unter variable *
1646a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 /. int nCell;
1646b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1646c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1646d 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 70 61 67 of cells in pag
1646e 65 20 70 50 61 67 65 20 2a 2f 0a 20 20 69 6e 74 e pPage */. int
1646f 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
16470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16471 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
16472 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 /. BtShared *pB
16473 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a t = pPage->pBt;.
16474 20 20 75 38 20 69 73 49 6e 69 74 4f 72 69 67 20 u8 isInitOrig
16475 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b = pPage->isInit;
16476 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 . Pgno pgno = p
16477 50 61 67 65 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 61 Page->pgno;.. a
16478 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
16479 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d utex_held(pPage-
1647a 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a >pBt->mutex) );.
1647b 20 20 72 63 20 3d 20 62 74 72 65 65 49 6e 69 74 rc = btreeInit
1647c 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 69 Page(pPage);. i
1647d 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1647e 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65 74 ){. goto set
1647f 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f _child_ptrmaps_o
16480 75 74 3b 0a 20 20 7d 0a 20 20 6e 43 65 6c 6c 20 ut;. }. nCell
16481 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a = pPage->nCell;.
16482 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 . for(i=0; i<nC
16483 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 ell; i++){. u
16484 38 20 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 8 *pCell = findC
16485 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a 0a ell(pPage, i);..
16486 20 20 20 20 70 74 72 6d 61 70 50 75 74 4f 76 66 ptrmapPutOvf
16487 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c lPtr(pPage, pCel
16488 6c 2c 20 26 72 63 29 3b 0a 0a 20 20 20 20 69 66 l, &rc);.. if
16489 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 ( !pPage->leaf )
1648a 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69 {. Pgno chi
1648b 6c 64 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 ldPgno = get4byt
1648c 65 28 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 e(pCell);.
1648d 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 63 ptrmapPut(pBt, c
1648e 68 69 6c 64 50 67 6e 6f 2c 20 50 54 52 4d 41 50 hildPgno, PTRMAP
1648f 5f 42 54 52 45 45 2c 20 70 67 6e 6f 2c 20 26 72 _BTREE, pgno, &r
16490 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 c);. }. }..
16491 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
16492 66 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 63 68 f ){. Pgno ch
16493 69 6c 64 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 ildPgno = get4by
16494 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 te(&pPage->aData
16495 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 [pPage->hdrOffse
16496 74 2b 38 5d 29 3b 0a 20 20 20 20 70 74 72 6d 61 t+8]);. ptrma
16497 70 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50 pPut(pBt, childP
16498 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 gno, PTRMAP_BTRE
16499 45 2c 20 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 E, pgno, &rc);.
1649a 20 7d 0a 0a 73 65 74 5f 63 68 69 6c 64 5f 70 74 }..set_child_pt
1649b 72 6d 61 70 73 5f 6f 75 74 3a 0a 20 20 70 50 61 rmaps_out:. pPa
1649c 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 69 73 49 ge->isInit = isI
1649d 6e 69 74 4f 72 69 67 3b 0a 20 20 72 65 74 75 72 nitOrig;. retur
1649e 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 n rc;.}../*.** S
1649f 6f 6d 65 77 68 65 72 65 20 6f 6e 20 70 50 61 67 omewhere on pPag
164a0 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 e is a pointer t
164a1 6f 20 70 61 67 65 20 69 46 72 6f 6d 2e 20 20 4d o page iFrom. M
164a2 6f 64 69 66 79 20 74 68 69 73 20 70 6f 69 6e 74 odify this point
164a3 65 72 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 er so.** that it
164a4 20 70 6f 69 6e 74 73 20 74 6f 20 69 54 6f 2e 20 points to iTo.
164a5 50 61 72 61 6d 65 74 65 72 20 65 54 79 70 65 20 Parameter eType
164a6 64 65 73 63 72 69 62 65 73 20 74 68 65 20 74 79 describes the ty
164a7 70 65 20 6f 66 20 70 6f 69 6e 74 65 72 20 74 6f pe of pointer to
164a8 0a 2a 2a 20 62 65 20 6d 6f 64 69 66 69 65 64 2c .** be modified,
164a9 20 61 73 20 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a as follows:.**
164aa 0a 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 .** PTRMAP_BTREE
164ab 3a 20 20 20 20 20 70 50 61 67 65 20 69 73 20 61 : pPage is a
164ac 20 62 74 72 65 65 2d 70 61 67 65 2e 20 54 68 65 btree-page. The
164ad 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 pointer points
164ae 61 74 20 61 20 63 68 69 6c 64 20 0a 2a 2a 20 20 at a child .**
164af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
164b0 20 70 61 67 65 20 6f 66 20 70 50 61 67 65 2e 0a page of pPage..
164b1 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 **.** PTRMAP_OVE
164b2 52 46 4c 4f 57 31 3a 20 70 50 61 67 65 20 69 73 RFLOW1: pPage is
164b3 20 61 20 62 74 72 65 65 2d 70 61 67 65 2e 20 54 a btree-page. T
164b4 68 65 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 he pointer point
164b5 73 20 61 74 20 61 6e 20 6f 76 65 72 66 6c 6f 77 s at an overflow
164b6 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
164b7 20 20 20 20 20 20 70 61 67 65 20 70 6f 69 6e 74 page point
164b8 65 64 20 74 6f 20 62 79 20 6f 6e 65 20 6f 66 20 ed to by one of
164b9 74 68 65 20 63 65 6c 6c 73 20 6f 6e 20 70 50 61 the cells on pPa
164ba 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 ge..**.** PTRMAP
164bb 5f 4f 56 45 52 46 4c 4f 57 32 3a 20 70 50 61 67 _OVERFLOW2: pPag
164bc 65 20 69 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77 e is an overflow
164bd 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 -page. The point
164be 65 72 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 er points at the
164bf 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20 next.**
164c0 20 20 20 20 20 20 20 20 20 20 20 6f 76 65 72 66 overf
164c1 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 20 low page in the
164c2 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 list..*/.static
164c3 69 6e 74 20 6d 6f 64 69 66 79 50 61 67 65 50 6f int modifyPagePo
164c4 69 6e 74 65 72 28 4d 65 6d 50 61 67 65 20 2a 70 inter(MemPage *p
164c5 50 61 67 65 2c 20 50 67 6e 6f 20 69 46 72 6f 6d Page, Pgno iFrom
164c6 2c 20 50 67 6e 6f 20 69 54 6f 2c 20 75 38 20 65 , Pgno iTo, u8 e
164c7 54 79 70 65 29 7b 0a 20 20 61 73 73 65 72 74 28 Type){. assert(
164c8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
164c9 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
164ca 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
164cb 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
164cc 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
164cd 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
164ce 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d if( eType==PTRM
164cf 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 29 7b 0a AP_OVERFLOW2 ){.
164d0 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 /* The point
164d1 65 72 20 69 73 20 61 6c 77 61 79 73 20 74 68 65 er is always the
164d2 20 66 69 72 73 74 20 34 20 62 79 74 65 73 20 6f first 4 bytes o
164d3 66 20 74 68 65 20 70 61 67 65 20 69 6e 20 74 68 f the page in th
164d4 69 73 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 20 is case. */.
164d5 20 69 66 28 20 67 65 74 34 62 79 74 65 28 70 50 if( get4byte(pP
164d6 61 67 65 2d 3e 61 44 61 74 61 29 21 3d 69 46 72 age->aData)!=iFr
164d7 6f 6d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 om ){. retu
164d8 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
164d9 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 T_BKPT;. }.
164da 20 20 70 75 74 34 62 79 74 65 28 70 50 61 67 65 put4byte(pPage
164db 2d 3e 61 44 61 74 61 2c 20 69 54 6f 29 3b 0a 20 ->aData, iTo);.
164dc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 38 20 69 }else{. u8 i
164dd 73 49 6e 69 74 4f 72 69 67 20 3d 20 70 50 61 67 sInitOrig = pPag
164de 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 20 20 20 69 e->isInit;. i
164df 6e 74 20 69 3b 0a 20 20 20 20 69 6e 74 20 6e 43 nt i;. int nC
164e0 65 6c 6c 3b 0a 0a 20 20 20 20 62 74 72 65 65 49 ell;.. btreeI
164e1 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a nitPage(pPage);.
164e2 20 20 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 nCell = pPag
164e3 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 66 e->nCell;.. f
164e4 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b or(i=0; i<nCell;
164e5 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 i++){. u8
164e6 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c *pCell = findCel
164e7 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 l(pPage, i);.
164e8 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 if( eType==PT
164e9 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 29 RMAP_OVERFLOW1 )
164ea 7b 0a 20 20 20 20 20 20 20 20 43 65 6c 6c 49 6e {. CellIn
164eb 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20 fo info;.
164ec 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 btreeParseCellP
164ed 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c tr(pPage, pCell,
164ee 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 &info);.
164ef 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 if( info.iOverf
164f0 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 low ){.
164f1 20 69 66 28 20 69 46 72 6f 6d 3d 3d 67 65 74 34 if( iFrom==get4
164f2 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f byte(&pCell[info
164f3 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 20 29 7b 0a .iOverflow]) ){.
164f4 20 20 20 20 20 20 20 20 20 20 20 20 70 75 74 34 put4
164f5 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f byte(&pCell[info
164f6 2e 69 4f 76 65 72 66 6c 6f 77 5d 2c 20 69 54 6f .iOverflow], iTo
164f7 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 );. b
164f8 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 reak;.
164f9 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
164fa 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
164fb 20 69 66 28 20 67 65 74 34 62 79 74 65 28 70 43 if( get4byte(pC
164fc 65 6c 6c 29 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 ell)==iFrom ){.
164fd 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 put4byt
164fe 65 28 70 43 65 6c 6c 2c 20 69 54 6f 29 3b 0a 20 e(pCell, iTo);.
164ff 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
16500 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
16501 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 69 }. }. . i
16502 66 28 20 69 3d 3d 6e 43 65 6c 6c 20 29 7b 0a 20 f( i==nCell ){.
16503 20 20 20 20 20 69 66 28 20 65 54 79 70 65 21 3d if( eType!=
16504 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 PTRMAP_BTREE ||
16505 0a 20 20 20 20 20 20 20 20 20 20 67 65 74 34 62 . get4b
16506 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 yte(&pPage->aDat
16507 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 a[pPage->hdrOffs
16508 65 74 2b 38 5d 29 21 3d 69 46 72 6f 6d 20 29 7b et+8])!=iFrom ){
16509 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
1650a 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
1650b 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 KPT;. }.
1650c 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 put4byte(&pPa
1650d 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d ge->aData[pPage-
1650e 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 69 >hdrOffset+8], i
1650f 54 6f 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 To);. }..
16510 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 pPage->isInit =
16511 69 73 49 6e 69 74 4f 72 69 67 3b 0a 20 20 7d 0a isInitOrig;. }.
16512 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
16513 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f OK;.}.../*.** Mo
16514 76 65 20 74 68 65 20 6f 70 65 6e 20 64 61 74 61 ve the open data
16515 62 61 73 65 20 70 61 67 65 20 70 44 62 50 61 67 base page pDbPag
16516 65 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 69 46 e to location iF
16517 72 65 65 50 61 67 65 20 69 6e 20 74 68 65 20 0a reePage in the .
16518 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 ** database. The
16519 20 70 44 62 50 61 67 65 20 72 65 66 65 72 65 6e pDbPage referen
1651a 63 65 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 ce remains valid
1651b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 43 6f ..**.** The isCo
1651c 6d 6d 69 74 20 66 6c 61 67 20 69 6e 64 69 63 61 mmit flag indica
1651d 74 65 73 20 74 68 61 74 20 74 68 65 72 65 20 69 tes that there i
1651e 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d s no need to rem
1651f 65 6d 62 65 72 20 74 68 61 74 0a 2a 2a 20 74 68 ember that.** th
16520 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 e journal needs
16521 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20 62 to be sync()ed b
16522 65 66 6f 72 65 20 64 61 74 61 62 61 73 65 20 70 efore database p
16523 61 67 65 20 70 44 62 50 61 67 65 2d 3e 70 67 6e age pDbPage->pgn
16524 6f 20 0a 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 o .** can be wri
16525 74 74 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c tten to. The cal
16526 6c 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 ler has already
16527 70 72 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 promised not to
16528 77 72 69 74 65 20 74 6f 20 74 68 61 74 0a 2a 2a write to that.**
16529 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 page..*/.static
1652a 20 69 6e 74 20 72 65 6c 6f 63 61 74 65 50 61 67 int relocatePag
1652b 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 e(. BtShared *p
1652c 42 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a Bt, /*
1652d 20 42 74 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 Btree */. MemP
1652e 61 67 65 20 2a 70 44 62 50 61 67 65 2c 20 20 20 age *pDbPage,
1652f 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 /* Open pag
16530 65 20 74 6f 20 6d 6f 76 65 20 2a 2f 0a 20 20 75 e to move */. u
16531 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 20 8 eType,
16532 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
16533 65 72 20 6d 61 70 20 27 74 79 70 65 27 20 65 6e er map 'type' en
16534 74 72 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 try for pDbPage
16535 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72 50 61 */. Pgno iPtrPa
16536 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ge, /*
16537 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 70 61 Pointer map 'pa
16538 67 65 2d 6e 6f 27 20 65 6e 74 72 79 20 66 6f 72 ge-no' entry for
16539 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 pDbPage */. Pg
1653a 6e 6f 20 69 46 72 65 65 50 61 67 65 2c 20 20 20 no iFreePage,
1653b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f /* The lo
1653c 63 61 74 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 70 cation to move p
1653d 44 62 50 61 67 65 20 74 6f 20 2a 2f 0a 20 20 69 DbPage to */. i
1653e 6e 74 20 69 73 43 6f 6d 6d 69 74 20 20 20 20 20 nt isCommit
1653f 20 20 20 20 20 20 20 20 2f 2a 20 69 73 43 6f 6d /* isCom
16540 6d 69 74 20 66 6c 61 67 20 70 61 73 73 65 64 20 mit flag passed
16541 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d to sqlite3PagerM
16542 6f 76 65 70 61 67 65 20 2a 2f 0a 29 7b 0a 20 20 ovepage */.){.
16543 4d 65 6d 50 61 67 65 20 2a 70 50 74 72 50 61 67 MemPage *pPtrPag
16544 65 3b 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 e; /* The page
16545 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 that contains a
16546 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 44 62 50 pointer to pDbP
16547 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 44 age */. Pgno iD
16548 62 50 61 67 65 20 3d 20 70 44 62 50 61 67 65 2d bPage = pDbPage-
16549 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20 2a >pgno;. Pager *
1654a 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 pPager = pBt->pP
1654b 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a ager;. int rc;.
1654c 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 . assert( eType
1654d 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f ==PTRMAP_OVERFLO
1654e 57 32 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 W2 || eType==PTR
1654f 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 7c 7c MAP_OVERFLOW1 ||
16550 20 0a 20 20 20 20 20 20 65 54 79 70 65 3d 3d 50 . eType==P
16551 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 TRMAP_BTREE || e
16552 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f Type==PTRMAP_ROO
16553 54 50 41 47 45 20 29 3b 0a 20 20 61 73 73 65 72 TPAGE );. asser
16554 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
16555 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
16556 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
16557 44 62 50 61 67 65 2d 3e 70 42 74 3d 3d 70 42 74 DbPage->pBt==pBt
16558 20 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 70 );.. /* Move p
16559 61 67 65 20 69 44 62 50 61 67 65 20 66 72 6f 6d age iDbPage from
1655a 20 69 74 73 20 63 75 72 72 65 6e 74 20 6c 6f 63 its current loc
1655b 61 74 69 6f 6e 20 74 6f 20 70 61 67 65 20 6e 75 ation to page nu
1655c 6d 62 65 72 20 69 46 72 65 65 50 61 67 65 20 2a mber iFreePage *
1655d 2f 0a 20 20 54 52 41 43 45 28 28 22 41 55 54 4f /. TRACE(("AUTO
1655e 56 41 43 55 55 4d 3a 20 4d 6f 76 69 6e 67 20 25 VACUUM: Moving %
1655f 64 20 74 6f 20 66 72 65 65 20 70 61 67 65 20 25 d to free page %
16560 64 20 28 70 74 72 20 70 61 67 65 20 25 64 20 74 d (ptr page %d t
16561 79 70 65 20 25 64 29 5c 6e 22 2c 20 0a 20 20 20 ype %d)\n", .
16562 20 20 20 69 44 62 50 61 67 65 2c 20 69 46 72 65 iDbPage, iFre
16563 65 50 61 67 65 2c 20 69 50 74 72 50 61 67 65 2c ePage, iPtrPage,
16564 20 65 54 79 70 65 29 29 3b 0a 20 20 72 63 20 3d eType));. rc =
16565 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 sqlite3PagerMov
16566 65 70 61 67 65 28 70 50 61 67 65 72 2c 20 70 44 epage(pPager, pD
16567 62 50 61 67 65 2d 3e 70 44 62 50 61 67 65 2c 20 bPage->pDbPage,
16568 69 46 72 65 65 50 61 67 65 2c 20 69 73 43 6f 6d iFreePage, isCom
16569 6d 69 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d mit);. if( rc!=
1656a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1656b 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
1656c 20 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 pDbPage->pgno
1656d 3d 20 69 46 72 65 65 50 61 67 65 3b 0a 0a 20 20 = iFreePage;..
1656e 2f 2a 20 49 66 20 70 44 62 50 61 67 65 20 77 61 /* If pDbPage wa
1656f 73 20 61 20 62 74 72 65 65 2d 70 61 67 65 2c 20 s a btree-page,
16570 74 68 65 6e 20 69 74 20 6d 61 79 20 68 61 76 65 then it may have
16571 20 63 68 69 6c 64 20 70 61 67 65 73 20 61 6e 64 child pages and
16572 2f 6f 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 74 /or cells. ** t
16573 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65 hat point to ove
16574 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 rflow pages. The
16575 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 pointer map ent
16576 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 74 68 65 ries for all the
16577 73 65 0a 20 20 2a 2a 20 70 61 67 65 73 20 6e 65 se. ** pages ne
16578 65 64 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 ed to be changed
16579 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 .. **. ** If p
1657a 44 62 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65 DbPage is an ove
1657b 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e rflow page, then
1657c 20 74 68 65 20 66 69 72 73 74 20 34 20 62 79 74 the first 4 byt
1657d 65 73 20 6d 61 79 20 73 74 6f 72 65 20 61 0a 20 es may store a.
1657e 20 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 ** pointer to a
1657f 20 73 75 62 73 65 71 75 65 6e 74 20 6f 76 65 72 subsequent over
16580 66 6c 6f 77 20 70 61 67 65 2e 20 49 66 20 74 68 flow page. If th
16581 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 is is the case,
16582 74 68 65 6e 0a 20 20 2a 2a 20 74 68 65 20 70 6f then. ** the po
16583 69 6e 74 65 72 20 6d 61 70 20 6e 65 65 64 73 20 inter map needs
16584 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 66 6f to be updated fo
16585 72 20 74 68 65 20 73 75 62 73 65 71 75 65 6e 74 r the subsequent
16586 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a overflow page..
16587 20 20 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 65 */. if( eType
16588 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c ==PTRMAP_BTREE |
16589 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f | eType==PTRMAP_
1658a 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 ROOTPAGE ){.
1658b 72 63 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 rc = setChildPtr
1658c 6d 61 70 73 28 70 44 62 50 61 67 65 29 3b 0a 20 maps(pDbPage);.
1658d 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1658e 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 E_OK ){. re
1658f 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
16590 20 7d 65 6c 73 65 7b 0a 20 20 20 20 50 67 6e 6f }else{. Pgno
16591 20 6e 65 78 74 4f 76 66 6c 20 3d 20 67 65 74 34 nextOvfl = get4
16592 62 79 74 65 28 70 44 62 50 61 67 65 2d 3e 61 44 byte(pDbPage->aD
16593 61 74 61 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 ata);. if( ne
16594 78 74 4f 76 66 6c 21 3d 30 20 29 7b 0a 20 20 20 xtOvfl!=0 ){.
16595 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 ptrmapPut(pBt
16596 2c 20 6e 65 78 74 4f 76 66 6c 2c 20 50 54 52 4d , nextOvfl, PTRM
16597 41 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69 46 AP_OVERFLOW2, iF
16598 72 65 65 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 reePage, &rc);.
16599 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1659a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1659b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
1659c 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a }. }. }..
1659d 20 20 2f 2a 20 46 69 78 20 74 68 65 20 64 61 74 /* Fix the dat
1659e 61 62 61 73 65 20 70 6f 69 6e 74 65 72 20 6f 6e abase pointer on
1659f 20 70 61 67 65 20 69 50 74 72 50 61 67 65 20 74 page iPtrPage t
165a0 68 61 74 20 70 6f 69 6e 74 65 64 20 61 74 20 69 hat pointed at i
165a1 44 62 50 61 67 65 20 73 6f 0a 20 20 2a 2a 20 74 DbPage so. ** t
165a2 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 61 74 hat it points at
165a3 20 69 46 72 65 65 50 61 67 65 2e 20 41 6c 73 6f iFreePage. Also
165a4 20 66 69 78 20 74 68 65 20 70 6f 69 6e 74 65 72 fix the pointer
165a5 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 map entry for.
165a6 20 2a 2a 20 69 50 74 72 50 61 67 65 2e 0a 20 20 ** iPtrPage..
165a7 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 65 21 3d */. if( eType!=
165a8 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 PTRMAP_ROOTPAGE
165a9 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 ){. rc = btre
165aa 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 50 eGetPage(pBt, iP
165ab 74 72 50 61 67 65 2c 20 26 70 50 74 72 50 61 67 trPage, &pPtrPag
165ac 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 e, 0);. if( r
165ad 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
165ae 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
165af 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
165b0 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
165b1 65 28 70 50 74 72 50 61 67 65 2d 3e 70 44 62 50 e(pPtrPage->pDbP
165b2 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 age);. if( rc
165b3 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
165b4 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
165b5 28 70 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 (pPtrPage);.
165b6 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
165b7 20 7d 0a 20 20 20 20 72 63 20 3d 20 6d 6f 64 69 }. rc = modi
165b8 66 79 50 61 67 65 50 6f 69 6e 74 65 72 28 70 50 fyPagePointer(pP
165b9 74 72 50 61 67 65 2c 20 69 44 62 50 61 67 65 2c trPage, iDbPage,
165ba 20 69 46 72 65 65 50 61 67 65 2c 20 65 54 79 70 iFreePage, eTyp
165bb 65 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 e);. releaseP
165bc 61 67 65 28 70 50 74 72 50 61 67 65 29 3b 0a 20 age(pPtrPage);.
165bd 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
165be 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 74 E_OK ){. pt
165bf 72 6d 61 70 50 75 74 28 70 42 74 2c 20 69 46 72 rmapPut(pBt, iFr
165c0 65 65 50 61 67 65 2c 20 65 54 79 70 65 2c 20 69 eePage, eType, i
165c1 50 74 72 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 PtrPage, &rc);.
165c2 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
165c3 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 n rc;.}../* Forw
165c4 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 ard declaration
165c5 72 65 71 75 69 72 65 64 20 62 79 20 69 6e 63 72 required by incr
165c6 56 61 63 75 75 6d 53 74 65 70 28 29 2e 20 2a 2f VacuumStep(). */
165c7 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f .static int allo
165c8 63 61 74 65 42 74 72 65 65 50 61 67 65 28 42 74 cateBtreePage(Bt
165c9 53 68 61 72 65 64 20 2a 2c 20 4d 65 6d 50 61 67 Shared *, MemPag
165ca 65 20 2a 2a 2c 20 50 67 6e 6f 20 2a 2c 20 50 67 e **, Pgno *, Pg
165cb 6e 6f 2c 20 75 38 29 3b 0a 0a 2f 2a 0a 2a 2a 20 no, u8);../*.**
165cc 50 65 72 66 6f 72 6d 20 61 20 73 69 6e 67 6c 65 Perform a single
165cd 20 73 74 65 70 20 6f 66 20 61 6e 20 69 6e 63 72 step of an incr
165ce 65 6d 65 6e 74 61 6c 2d 76 61 63 75 75 6d 2e 20 emental-vacuum.
165cf 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a If successful,.*
165d0 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f * return SQLITE_
165d1 4f 4b 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 OK. If there is
165d2 6e 6f 20 77 6f 72 6b 20 74 6f 20 64 6f 20 28 61 no work to do (a
165d3 6e 64 20 74 68 65 72 65 66 6f 72 65 20 6e 6f 0a nd therefore no.
165d4 2a 2a 20 70 6f 69 6e 74 20 69 6e 20 63 61 6c 6c ** point in call
165d5 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f ing this functio
165d6 6e 20 61 67 61 69 6e 29 2c 20 72 65 74 75 72 6e n again), return
165d7 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 0a 2a 2a SQLITE_DONE..**
165d8 0a 2a 2a 20 4d 6f 72 65 20 73 70 65 63 69 66 69 .** More specifi
165d9 63 6c 79 2c 20 74 68 69 73 20 66 75 6e 63 74 69 cly, this functi
165da 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 on attempts to r
165db 65 2d 6f 72 67 61 6e 69 7a 65 20 74 68 65 20 0a e-organize the .
165dc 2a 2a 20 64 61 74 61 62 61 73 65 20 73 6f 20 74 ** database so t
165dd 68 61 74 20 74 68 65 20 6c 61 73 74 20 70 61 67 hat the last pag
165de 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63 75 e of the file cu
165df 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 0a 2a rrently in use.*
165e0 2a 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 69 * is no longer i
165e1 6e 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 n use..**.** If
165e2 74 68 65 20 6e 46 69 6e 20 70 61 72 61 6d 65 74 the nFin paramet
165e3 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 er is non-zero,
165e4 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 this function as
165e5 73 75 6d 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 sumes.** that th
165e6 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6b 65 e caller will ke
165e7 65 70 20 63 61 6c 6c 69 6e 67 20 69 6e 63 72 56 ep calling incrV
165e8 61 63 75 75 6d 53 74 65 70 28 29 20 75 6e 74 69 acuumStep() unti
165e9 6c 0a 2a 2a 20 69 74 20 72 65 74 75 72 6e 73 20 l.** it returns
165ea 53 51 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 SQLITE_DONE or a
165eb 6e 20 65 72 72 6f 72 2c 20 61 6e 64 20 74 68 61 n error, and tha
165ec 74 20 6e 46 69 6e 20 69 73 20 74 68 65 0a 2a 2a t nFin is the.**
165ed 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
165ee 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
165ef 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 le will contain
165f0 61 66 74 65 72 20 74 68 69 73 20 0a 2a 2a 20 70 after this .** p
165f1 72 6f 63 65 73 73 20 69 73 20 63 6f 6d 70 6c 65 rocess is comple
165f2 74 65 2e 20 20 49 66 20 6e 46 69 6e 20 69 73 20 te. If nFin is
165f3 7a 65 72 6f 2c 20 69 74 20 69 73 20 61 73 73 75 zero, it is assu
165f4 6d 65 64 20 74 68 61 74 0a 2a 2a 20 69 6e 63 72 med that.** incr
165f5 56 61 63 75 75 6d 53 74 65 70 28 29 20 77 69 6c VacuumStep() wil
165f6 6c 20 62 65 20 63 61 6c 6c 65 64 20 61 20 66 69 l be called a fi
165f7 6e 69 74 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 nite amount of t
165f8 69 6d 65 73 0a 2a 2a 20 77 68 69 63 68 20 6d 61 imes.** which ma
165f9 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 65 6d 70 y or may not emp
165fa 74 79 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e ty the freelist.
165fb 20 20 41 20 66 75 6c 6c 20 61 75 74 6f 76 61 63 A full autovac
165fc 75 75 6d 0a 2a 2a 20 68 61 73 20 6e 46 69 6e 3e uum.** has nFin>
165fd 30 2e 20 20 41 20 22 50 52 41 47 4d 41 20 69 6e 0. A "PRAGMA in
165fe 63 72 65 6d 65 6e 74 61 6c 5f 76 61 63 75 75 6d cremental_vacuum
165ff 22 20 68 61 73 20 6e 46 69 6e 3d 3d 30 2e 0a 2a " has nFin==0..*
16600 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 63 /.static int inc
16601 72 56 61 63 75 75 6d 53 74 65 70 28 42 74 53 68 rVacuumStep(BtSh
16602 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 ared *pBt, Pgno
16603 6e 46 69 6e 2c 20 50 67 6e 6f 20 69 4c 61 73 74 nFin, Pgno iLast
16604 50 67 29 7b 0a 20 20 50 67 6e 6f 20 6e 46 72 65 Pg){. Pgno nFre
16605 65 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 eList;
16606 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
16607 67 65 73 20 73 74 69 6c 6c 20 6f 6e 20 74 68 65 ges still on the
16608 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 free-list */..
16609 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1660a 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
1660b 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
1660c 65 72 74 28 20 69 4c 61 73 74 50 67 3e 6e 46 69 ert( iLastPg>nFi
1660d 6e 20 29 3b 0a 0a 20 20 69 66 28 20 21 50 54 52 n );.. if( !PTR
1660e 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 MAP_ISPAGE(pBt,
1660f 69 4c 61 73 74 50 67 29 20 26 26 20 69 4c 61 73 iLastPg) && iLas
16610 74 50 67 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 tPg!=PENDING_BYT
16611 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 E_PAGE(pBt) ){.
16612 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 75 int rc;. u
16613 38 20 65 54 79 70 65 3b 0a 20 20 20 20 50 67 6e 8 eType;. Pgn
16614 6f 20 69 50 74 72 50 61 67 65 3b 0a 0a 20 20 20 o iPtrPage;..
16615 20 6e 46 72 65 65 4c 69 73 74 20 3d 20 67 65 74 nFreeList = get
16616 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 4byte(&pBt->pPag
16617 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a e1->aData[36]);.
16618 20 20 20 20 69 66 28 20 6e 46 72 65 65 4c 69 73 if( nFreeLis
16619 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 t==0 ){. re
1661a 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 turn SQLITE_DONE
1661b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 ;. }.. rc
1661c 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c = ptrmapGet(pBt,
1661d 20 69 4c 61 73 74 50 67 2c 20 26 65 54 79 70 65 iLastPg, &eType
1661e 2c 20 26 69 50 74 72 50 61 67 65 29 3b 0a 20 20 , &iPtrPage);.
1661f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
16620 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 _OK ){. ret
16621 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
16622 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 if( eType==PTR
16623 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a MAP_ROOTPAGE ){.
16624 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
16625 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
16626 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 ;. }.. if(
16627 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 eType==PTRMAP_F
16628 52 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 REEPAGE ){.
16629 20 69 66 28 20 6e 46 69 6e 3d 3d 30 20 29 7b 0a if( nFin==0 ){.
1662a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 /* Remov
1662b 65 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 e the page from
1662c 74 68 65 20 66 69 6c 65 73 20 66 72 65 65 2d 6c the files free-l
1662d 69 73 74 2e 20 54 68 69 73 20 69 73 20 6e 6f 74 ist. This is not
1662e 20 72 65 71 75 69 72 65 64 0a 20 20 20 20 20 20 required.
1662f 20 20 2a 2a 20 69 66 20 6e 46 69 6e 20 69 73 20 ** if nFin is
16630 6e 6f 6e 2d 7a 65 72 6f 2e 20 49 6e 20 74 68 61 non-zero. In tha
16631 74 20 63 61 73 65 2c 20 74 68 65 20 66 72 65 65 t case, the free
16632 2d 6c 69 73 74 20 77 69 6c 6c 20 62 65 0a 20 20 -list will be.
16633 20 20 20 20 20 20 2a 2a 20 74 72 75 6e 63 61 74 ** truncat
16634 65 64 20 74 6f 20 7a 65 72 6f 20 61 66 74 65 72 ed to zero after
16635 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 this function r
16636 65 74 75 72 6e 73 2c 20 73 6f 20 69 74 20 64 6f eturns, so it do
16637 65 73 6e 27 74 20 0a 20 20 20 20 20 20 20 20 2a esn't . *
16638 2a 20 6d 61 74 74 65 72 20 69 66 20 69 74 20 73 * matter if it s
16639 74 69 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 73 6f till contains so
1663a 6d 65 20 67 61 72 62 61 67 65 20 65 6e 74 72 69 me garbage entri
1663b 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 es.. */.
1663c 20 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 Pgno iFre
1663d 65 50 67 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d ePg;. Mem
1663e 50 61 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 Page *pFreePg;.
1663f 20 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f rc = allo
16640 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 cateBtreePage(pB
16641 74 2c 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 t, &pFreePg, &iF
16642 72 65 65 50 67 2c 20 69 4c 61 73 74 50 67 2c 20 reePg, iLastPg,
16643 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 1);. if(
16644 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
16645 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
16646 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a n rc;. }.
16647 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
16648 69 46 72 65 65 50 67 3d 3d 69 4c 61 73 74 50 67 iFreePg==iLastPg
16649 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 );. rele
1664a 61 73 65 50 61 67 65 28 70 46 72 65 65 50 67 29 asePage(pFreePg)
1664b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 ;. }. }
1664c 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 50 67 6e else {. Pgn
1664d 6f 20 69 46 72 65 65 50 67 3b 20 20 20 20 20 20 o iFreePg;
1664e 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
1664f 6f 66 20 66 72 65 65 20 70 61 67 65 20 74 6f 20 of free page to
16650 6d 6f 76 65 20 70 4c 61 73 74 50 67 20 74 6f 20 move pLastPg to
16651 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 */. MemPage
16652 20 2a 70 4c 61 73 74 50 67 3b 0a 0a 20 20 20 20 *pLastPg;..
16653 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 rc = btreeGetP
16654 61 67 65 28 70 42 74 2c 20 69 4c 61 73 74 50 67 age(pBt, iLastPg
16655 2c 20 26 70 4c 61 73 74 50 67 2c 20 30 29 3b 0a , &pLastPg, 0);.
16656 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
16657 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
16658 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
16659 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 }.. /*
1665a 49 66 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f 2c If nFin is zero,
1665b 20 74 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 this loop runs
1665c 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 61 6e 64 exactly once and
1665d 20 70 61 67 65 20 70 4c 61 73 74 50 67 0a 20 20 page pLastPg.
1665e 20 20 20 20 2a 2a 20 69 73 20 73 77 61 70 70 65 ** is swappe
1665f 64 20 77 69 74 68 20 74 68 65 20 66 69 72 73 74 d with the first
16660 20 66 72 65 65 20 70 61 67 65 20 70 75 6c 6c 65 free page pulle
16661 64 20 6f 66 66 20 74 68 65 20 66 72 65 65 20 6c d off the free l
16662 69 73 74 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 ist.. **.
16663 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 ** On the ot
16664 68 65 72 20 68 61 6e 64 2c 20 69 66 20 6e 46 69 her hand, if nFi
16665 6e 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 n is greater tha
16666 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6b 65 65 n zero, then kee
16667 70 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 6f 70 69 p. ** loopi
16668 6e 67 20 75 6e 74 69 6c 20 61 20 66 72 65 65 2d ng until a free-
16669 70 61 67 65 20 6c 6f 63 61 74 65 64 20 77 69 74 page located wit
1666a 68 69 6e 20 74 68 65 20 66 69 72 73 74 20 6e 46 hin the first nF
1666b 69 6e 20 70 61 67 65 73 0a 20 20 20 20 20 20 2a in pages. *
1666c 2a 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 * of the file is
1666d 20 66 6f 75 6e 64 2e 0a 20 20 20 20 20 20 2a 2f found.. */
1666e 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 . do {.
1666f 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 MemPage *pFr
16670 65 65 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63 eePg;. rc
16671 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 = allocateBtree
16672 50 61 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 Page(pBt, &pFree
16673 50 67 2c 20 26 69 46 72 65 65 50 67 2c 20 30 2c Pg, &iFreePg, 0,
16674 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 0);. if(
16675 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
16676 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 {. rele
16677 61 73 65 50 61 67 65 28 70 4c 61 73 74 50 67 29 asePage(pLastPg)
16678 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ;. retu
16679 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d rn rc;. }
1667a 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
1667b 50 61 67 65 28 70 46 72 65 65 50 67 29 3b 0a 20 Page(pFreePg);.
1667c 20 20 20 20 20 7d 77 68 69 6c 65 28 20 6e 46 69 }while( nFi
1667d 6e 21 3d 30 20 26 26 20 69 46 72 65 65 50 67 3e n!=0 && iFreePg>
1667e 6e 46 69 6e 20 29 3b 0a 20 20 20 20 20 20 61 73 nFin );. as
1667f 73 65 72 74 28 20 69 46 72 65 65 50 67 3c 69 4c sert( iFreePg<iL
16680 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20 20 0a astPg );. .
16681 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
16682 65 33 50 61 67 65 72 57 72 69 74 65 28 70 4c 61 e3PagerWrite(pLa
16683 73 74 50 67 2d 3e 70 44 62 50 61 67 65 29 3b 0a stPg->pDbPage);.
16684 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
16685 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
16686 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 rc = relocate
16687 50 61 67 65 28 70 42 74 2c 20 70 4c 61 73 74 50 Page(pBt, pLastP
16688 67 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 g, eType, iPtrPa
16689 67 65 2c 20 69 46 72 65 65 50 67 2c 20 6e 46 69 ge, iFreePg, nFi
1668a 6e 21 3d 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 n!=0);. }.
1668b 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
1668c 28 70 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 20 (pLastPg);.
1668d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1668e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 OK ){. re
1668f 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
16690 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 . }. }.. if
16691 28 20 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 ( nFin==0 ){.
16692 20 69 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20 20 iLastPg--;.
16693 77 68 69 6c 65 28 20 69 4c 61 73 74 50 67 3d 3d while( iLastPg==
16694 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
16695 45 28 70 42 74 29 7c 7c 50 54 52 4d 41 50 5f 49 E(pBt)||PTRMAP_I
16696 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74 SPAGE(pBt, iLast
16697 50 67 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 Pg) ){. if(
16698 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 PTRMAP_ISPAGE(p
16699 42 74 2c 20 69 4c 61 73 74 50 67 29 20 29 7b 0a Bt, iLastPg) ){.
1669a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 MemPage
1669b 2a 70 50 67 3b 0a 20 20 20 20 20 20 20 20 69 6e *pPg;. in
1669c 74 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 t rc = btreeGetP
1669d 61 67 65 28 70 42 74 2c 20 69 4c 61 73 74 50 67 age(pBt, iLastPg
1669e 2c 20 26 70 50 67 2c 20 30 29 3b 0a 20 20 20 20 , &pPg, 0);.
1669f 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
166a0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
166a1 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
166a2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
166a3 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
166a4 72 57 72 69 74 65 28 70 50 67 2d 3e 70 44 62 50 rWrite(pPg->pDbP
166a5 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 age);. re
166a6 6c 65 61 73 65 50 61 67 65 28 70 50 67 29 3b 0a leasePage(pPg);.
166a7 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
166a8 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
166a9 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
166aa 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
166ab 20 20 7d 0a 20 20 20 20 20 20 69 4c 61 73 74 50 }. iLastP
166ac 67 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 g--;. }. s
166ad 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 qlite3PagerTrunc
166ae 61 74 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 50 ateImage(pBt->pP
166af 61 67 65 72 2c 20 69 4c 61 73 74 50 67 29 3b 0a ager, iLastPg);.
166b0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
166b1 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
166b2 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 A write-transac
166b3 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 65 tion must be ope
166b4 6e 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 ned before calli
166b5 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ng this function
166b6 2e 0a 2a 2a 20 49 74 20 70 65 72 66 6f 72 6d 73 ..** It performs
166b7 20 61 20 73 69 6e 67 6c 65 20 75 6e 69 74 20 6f a single unit o
166b8 66 20 77 6f 72 6b 20 74 6f 77 61 72 64 73 20 61 f work towards a
166b9 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 n incremental va
166ba 63 75 75 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 cuum..**.** If t
166bb 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 he incremental v
166bc 61 63 75 75 6d 20 69 73 20 66 69 6e 69 73 68 65 acuum is finishe
166bd 64 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e d after this fun
166be 63 74 69 6f 6e 20 68 61 73 20 72 75 6e 2c 0a 2a ction has run,.*
166bf 2a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 * SQLITE_DONE is
166c0 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 69 74 returned. If it
166c1 20 69 73 20 6e 6f 74 20 66 69 6e 69 73 68 65 64 is not finished
166c2 2c 20 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f , but no error o
166c3 63 63 75 72 72 65 64 2c 0a 2a 2a 20 53 51 4c 49 ccurred,.** SQLI
166c4 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
166c5 64 2e 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20 d. Otherwise an
166c6 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 SQLite error cod
166c7 65 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 e. .*/.SQLITE_PR
166c8 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
166c9 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 3BtreeIncrVacuum
166ca 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e (Btree *p){. in
166cb 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 t rc;. BtShared
166cc 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
166cd 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
166ce 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 nter(p);. asser
166cf 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 t( pBt->inTransa
166d0 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 ction==TRANS_WRI
166d1 54 45 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 TE && p->inTrans
166d2 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b ==TRANS_WRITE );
166d3 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 61 75 74 . if( !pBt->aut
166d4 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 72 oVacuum ){. r
166d5 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b c = SQLITE_DONE;
166d6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e . }else{. in
166d7 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 validateAllOverf
166d8 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20 lowCache(pBt);.
166d9 20 20 20 72 63 20 3d 20 69 6e 63 72 56 61 63 75 rc = incrVacu
166da 75 6d 53 74 65 70 28 70 42 74 2c 20 30 2c 20 70 umStep(pBt, 0, p
166db 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 agerPagecount(pB
166dc 74 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 t));. }. sqlit
166dd 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
166de 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
166df 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
166e0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 70 72 ine is called pr
166e1 69 6f 72 20 74 6f 20 73 71 6c 69 74 65 33 50 61 ior to sqlite3Pa
166e2 67 65 72 43 6f 6d 6d 69 74 20 77 68 65 6e 20 61 gerCommit when a
166e3 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 transaction.**
166e4 69 73 20 63 6f 6d 6d 69 74 65 64 20 66 6f 72 20 is commited for
166e5 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 an auto-vacuum d
166e6 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 atabase..**.** I
166e7 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 f SQLITE_OK is r
166e8 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 2a 70 eturned, then *p
166e9 6e 54 72 75 6e 63 20 69 73 20 73 65 74 20 74 6f nTrunc is set to
166ea 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
166eb 61 67 65 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 ages.** the data
166ec 62 61 73 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 base file should
166ed 20 62 65 20 74 72 75 6e 63 61 74 65 64 20 74 6f be truncated to
166ee 20 64 75 72 69 6e 67 20 74 68 65 20 63 6f 6d 6d during the comm
166ef 69 74 20 70 72 6f 63 65 73 73 2e 20 0a 2a 2a 20 it process. .**
166f0 69 2e 65 2e 20 74 68 65 20 64 61 74 61 62 61 73 i.e. the databas
166f1 65 20 68 61 73 20 62 65 65 6e 20 72 65 6f 72 67 e has been reorg
166f2 61 6e 69 7a 65 64 20 73 6f 20 74 68 61 74 20 6f anized so that o
166f3 6e 6c 79 20 74 68 65 20 66 69 72 73 74 20 2a 70 nly the first *p
166f4 6e 54 72 75 6e 63 0a 2a 2a 20 70 61 67 65 73 20 nTrunc.** pages
166f5 61 72 65 20 69 6e 20 75 73 65 2e 0a 2a 2f 0a 73 are in use..*/.s
166f6 74 61 74 69 63 20 69 6e 74 20 61 75 74 6f 56 61 tatic int autoVa
166f7 63 75 75 6d 43 6f 6d 6d 69 74 28 42 74 53 68 61 cuumCommit(BtSha
166f8 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 red *pBt){. int
166f9 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
166fa 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
166fb 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a = pBt->pPager;.
166fc 20 20 56 56 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 VVA_ONLY( int
166fd 6e 52 65 66 20 3d 20 73 71 6c 69 74 65 33 50 61 nRef = sqlite3Pa
166fe 67 65 72 52 65 66 63 6f 75 6e 74 28 70 50 61 67 gerRefcount(pPag
166ff 65 72 29 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 er) );.. assert
16700 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
16701 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
16702 20 29 3b 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 );. invalidate
16703 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 AllOverflowCache
16704 28 70 42 74 29 3b 0a 20 20 61 73 73 65 72 74 28 (pBt);. assert(
16705 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 pBt->autoVacuum)
16706 3b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 69 6e ;. if( !pBt->in
16707 63 72 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 crVacuum ){.
16708 50 67 6e 6f 20 6e 46 69 6e 3b 20 20 20 20 20 20 Pgno nFin;
16709 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1670a 70 61 67 65 73 20 69 6e 20 64 61 74 61 62 61 73 pages in databas
1670b 65 20 61 66 74 65 72 20 61 75 74 6f 76 61 63 75 e after autovacu
1670c 75 6d 69 6e 67 20 2a 2f 0a 20 20 20 20 50 67 6e uming */. Pgn
1670d 6f 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 o nFree;
1670e 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 /* Number of pag
1670f 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 es on the freeli
16710 73 74 20 69 6e 69 74 69 61 6c 6c 79 20 2a 2f 0a st initially */.
16711 20 20 20 20 50 67 6e 6f 20 6e 50 74 72 6d 61 70 Pgno nPtrmap
16712 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 ; /* Number
16713 20 6f 66 20 50 74 72 4d 61 70 20 70 61 67 65 73 of PtrMap pages
16714 20 74 6f 20 62 65 20 66 72 65 65 64 20 2a 2f 0a to be freed */.
16715 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 3b 20 Pgno iFree;
16716 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 /* The ne
16717 78 74 20 70 61 67 65 20 74 6f 20 62 65 20 66 72 xt page to be fr
16718 65 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e eed */. int n
16719 45 6e 74 72 79 3b 20 20 20 20 20 20 20 20 2f 2a Entry; /*
1671a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
1671b 65 73 20 6f 6e 20 6f 6e 65 20 70 74 72 6d 61 70 es on one ptrmap
1671c 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 50 67 6e page */. Pgn
1671d 6f 20 6e 4f 72 69 67 3b 20 20 20 20 20 20 20 20 o nOrig;
1671e 2f 2a 20 44 61 74 61 62 61 73 65 20 73 69 7a 65 /* Database size
1671f 20 62 65 66 6f 72 65 20 66 72 65 65 69 6e 67 20 before freeing
16720 2a 2f 0a 0a 20 20 20 20 6e 4f 72 69 67 20 3d 20 */.. nOrig =
16721 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 pagerPagecount(p
16722 42 74 29 3b 0a 20 20 20 20 69 66 28 20 50 54 52 Bt);. if( PTR
16723 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 MAP_ISPAGE(pBt,
16724 6e 4f 72 69 67 29 20 7c 7c 20 6e 4f 72 69 67 3d nOrig) || nOrig=
16725 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 =PENDING_BYTE_PA
16726 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 GE(pBt) ){.
16727 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f /* It is not po
16728 73 73 69 62 6c 65 20 74 6f 20 63 72 65 61 74 65 ssible to create
16729 20 61 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 a database for
1672a 77 68 69 63 68 20 74 68 65 20 66 69 6e 61 6c 20 which the final
1672b 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 page. ** is
1672c 20 65 69 74 68 65 72 20 61 20 70 6f 69 6e 74 65 either a pointe
1672d 72 2d 6d 61 70 20 70 61 67 65 20 6f 72 20 74 68 r-map page or th
1672e 65 20 70 65 6e 64 69 6e 67 2d 62 79 74 65 20 70 e pending-byte p
1672f 61 67 65 2e 20 49 66 20 6f 6e 65 0a 20 20 20 20 age. If one.
16730 20 20 2a 2a 20 69 73 20 65 6e 63 6f 75 6e 74 65 ** is encounte
16731 72 65 64 2c 20 74 68 69 73 20 69 6e 64 69 63 61 red, this indica
16732 74 65 73 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a tes corruption..
16733 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 */. r
16734 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
16735 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d RUPT_BKPT;. }
16736 0a 0a 20 20 20 20 6e 46 72 65 65 20 3d 20 67 65 .. nFree = ge
16737 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 t4byte(&pBt->pPa
16738 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b ge1->aData[36]);
16739 0a 20 20 20 20 6e 45 6e 74 72 79 20 3d 20 70 42 . nEntry = pB
1673a 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 3b t->usableSize/5;
1673b 0a 20 20 20 20 6e 50 74 72 6d 61 70 20 3d 20 28 . nPtrmap = (
1673c 6e 46 72 65 65 2d 6e 4f 72 69 67 2b 50 54 52 4d nFree-nOrig+PTRM
1673d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6e AP_PAGENO(pBt, n
1673e 4f 72 69 67 29 2b 6e 45 6e 74 72 79 29 2f 6e 45 Orig)+nEntry)/nE
1673f 6e 74 72 79 3b 0a 20 20 20 20 6e 46 69 6e 20 3d ntry;. nFin =
16740 20 6e 4f 72 69 67 20 2d 20 6e 46 72 65 65 20 2d nOrig - nFree -
16741 20 6e 50 74 72 6d 61 70 3b 0a 20 20 20 20 69 66 nPtrmap;. if
16742 28 20 6e 4f 72 69 67 3e 50 45 4e 44 49 4e 47 5f ( nOrig>PENDING_
16743 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 26 BYTE_PAGE(pBt) &
16744 26 20 6e 46 69 6e 3c 50 45 4e 44 49 4e 47 5f 42 & nFin<PENDING_B
16745 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b YTE_PAGE(pBt) ){
16746 0a 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 . nFin--;.
16747 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 }. while(
16748 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 PTRMAP_ISPAGE(pB
16749 74 2c 20 6e 46 69 6e 29 20 7c 7c 20 6e 46 69 6e t, nFin) || nFin
1674a 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 ==PENDING_BYTE_P
1674b 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 AGE(pBt) ){.
1674c 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a nFin--;. }.
1674d 20 20 20 20 69 66 28 20 6e 46 69 6e 3e 6e 4f 72 if( nFin>nOr
1674e 69 67 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 ig ) return SQLI
1674f 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
16750 0a 0a 20 20 20 20 66 6f 72 28 69 46 72 65 65 3d .. for(iFree=
16751 6e 4f 72 69 67 3b 20 69 46 72 65 65 3e 6e 46 69 nOrig; iFree>nFi
16752 6e 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f n && rc==SQLITE_
16753 4f 4b 3b 20 69 46 72 65 65 2d 2d 29 7b 0a 20 20 OK; iFree--){.
16754 20 20 20 20 72 63 20 3d 20 69 6e 63 72 56 61 63 rc = incrVac
16755 75 75 6d 53 74 65 70 28 70 42 74 2c 20 6e 46 69 uumStep(pBt, nFi
16756 6e 2c 20 69 46 72 65 65 29 3b 0a 20 20 20 20 7d n, iFree);. }
16757 0a 20 20 20 20 69 66 28 20 28 72 63 3d 3d 53 51 . if( (rc==SQ
16758 4c 49 54 45 5f 44 4f 4e 45 20 7c 7c 20 72 63 3d LITE_DONE || rc=
16759 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20 6e =SQLITE_OK) && n
1675a 46 72 65 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 Free>0 ){.
1675b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1675c 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1675d 65 33 50 61 67 65 72 57 72 69 74 65 28 70 42 74 e3PagerWrite(pBt
1675e 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 ->pPage1->pDbPag
1675f 65 29 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 e);. put4by
16760 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d te(&pBt->pPage1-
16761 3e 61 44 61 74 61 5b 33 32 5d 2c 20 30 29 3b 0a >aData[32], 0);.
16762 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
16763 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 pBt->pPage1->aDa
16764 74 61 5b 33 36 5d 2c 20 30 29 3b 0a 20 20 20 20 ta[36], 0);.
16765 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 sqlite3PagerTr
16766 75 6e 63 61 74 65 49 6d 61 67 65 28 70 42 74 2d uncateImage(pBt-
16767 3e 70 50 61 67 65 72 2c 20 6e 46 69 6e 29 3b 0a >pPager, nFin);.
16768 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
16769 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1676a 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
1676b 72 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 rRollback(pPager
1676c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
1676d 61 73 73 65 72 74 28 20 6e 52 65 66 3d 3d 73 71 assert( nRef==sq
1676e 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 lite3PagerRefcou
1676f 6e 74 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 nt(pPager) );.
16770 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 return rc;.}..#e
16771 6c 73 65 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 lse /* ifndef SQ
16772 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
16773 43 55 55 4d 20 2a 2f 0a 23 20 64 65 66 69 6e 65 CUUM */.# define
16774 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 setChildPtrmaps
16775 28 78 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 (x) SQLITE_OK.#e
16776 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ndif../*.** This
16777 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68 routine does th
16778 65 20 66 69 72 73 74 20 70 68 61 73 65 20 6f 66 e first phase of
16779 20 61 20 74 77 6f 2d 70 68 61 73 65 20 63 6f 6d a two-phase com
1677a 6d 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 mit. This routi
1677b 6e 65 0a 2a 2a 20 63 61 75 73 65 73 20 61 20 72 ne.** causes a r
1677c 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 ollback journal
1677d 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 28 69 to be created (i
1677e 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c f it does not al
1677f 72 65 61 64 79 20 65 78 69 73 74 29 0a 2a 2a 20 ready exist).**
16780 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 77 69 and populated wi
16781 74 68 20 65 6e 6f 75 67 68 20 69 6e 66 6f 72 6d th enough inform
16782 61 74 69 6f 6e 20 73 6f 20 74 68 61 74 20 69 66 ation so that if
16783 20 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6f 63 a power loss oc
16784 63 75 72 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 curs.** the data
16785 62 61 73 65 20 63 61 6e 20 62 65 20 72 65 73 74 base can be rest
16786 6f 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 ored to its orig
16787 69 6e 61 6c 20 73 74 61 74 65 20 62 79 20 70 6c inal state by pl
16788 61 79 69 6e 67 20 62 61 63 6b 0a 2a 2a 20 74 68 aying back.** th
16789 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 6e e journal. Then
1678a 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
1678b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 72 65 the journal are
1678c 20 66 6c 75 73 68 65 64 20 6f 75 74 20 74 6f 0a flushed out to.
1678d 2a 2a 20 74 68 65 20 64 69 73 6b 2e 20 20 41 66 ** the disk. Af
1678e 74 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ter the journal
1678f 69 73 20 73 61 66 65 6c 79 20 6f 6e 20 6f 78 69 is safely on oxi
16790 64 65 2c 20 74 68 65 20 63 68 61 6e 67 65 73 20 de, the changes
16791 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 to the.** databa
16792 73 65 20 61 72 65 20 77 72 69 74 74 65 6e 20 69 se are written i
16793 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 nto the database
16794 20 66 69 6c 65 20 61 6e 64 20 66 6c 75 73 68 65 file and flushe
16795 64 20 74 6f 20 6f 78 69 64 65 2e 0a 2a 2a 20 41 d to oxide..** A
16796 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69 t the end of thi
16797 73 20 63 61 6c 6c 2c 20 74 68 65 20 72 6f 6c 6c s call, the roll
16798 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 73 74 69 back journal sti
16799 6c 6c 20 65 78 69 73 74 73 20 6f 6e 20 74 68 65 ll exists on the
1679a 0a 2a 2a 20 64 69 73 6b 20 61 6e 64 20 77 65 20 .** disk and we
1679b 61 72 65 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e are still holdin
1679c 67 20 61 6c 6c 20 6c 6f 63 6b 73 2c 20 73 6f 20 g all locks, so
1679d 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
1679e 68 61 73 20 6e 6f 74 0a 2a 2a 20 63 6f 6d 6d 69 has not.** commi
1679f 74 74 65 64 2e 20 20 53 65 65 20 73 71 6c 69 74 tted. See sqlit
167a0 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
167a1 73 65 54 77 6f 28 29 20 66 6f 72 20 74 68 65 20 seTwo() for the
167a2 73 65 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 second phase of
167a3 74 68 65 0a 2a 2a 20 63 6f 6d 6d 69 74 20 70 72 the.** commit pr
167a4 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ocess..**.** Thi
167a5 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f s call is a no-o
167a6 70 20 69 66 20 6e 6f 20 77 72 69 74 65 2d 74 72 p if no write-tr
167a7 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 75 72 ansaction is cur
167a8 72 65 6e 74 6c 79 20 61 63 74 69 76 65 20 6f 6e rently active on
167a9 20 70 42 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 pBt..**.** Othe
167aa 72 77 69 73 65 2c 20 73 79 6e 63 20 74 68 65 20 rwise, sync the
167ab 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f database file fo
167ac 72 20 74 68 65 20 62 74 72 65 65 20 70 42 74 2e r the btree pBt.
167ad 20 7a 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20 zMaster points
167ae 74 6f 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f to.** the name o
167af 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e f a master journ
167b0 61 6c 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f al file that sho
167b1 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e 20 69 uld be written i
167b2 6e 74 6f 20 74 68 65 0a 2a 2a 20 69 6e 64 69 76 nto the.** indiv
167b3 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 idual journal fi
167b4 6c 65 2c 20 6f 72 20 69 73 20 4e 55 4c 4c 2c 20 le, or is NULL,
167b5 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 6d 61 indicating no ma
167b6 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
167b7 65 20 0a 2a 2a 20 28 73 69 6e 67 6c 65 20 64 61 e .** (single da
167b8 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 tabase transacti
167b9 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 on)..**.** When
167ba 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20 this is called,
167bb 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
167bc 61 6c 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 al should alread
167bd 79 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20 63 y have been.** c
167be 72 65 61 74 65 64 2c 20 70 6f 70 75 6c 61 74 65 reated, populate
167bf 64 20 77 69 74 68 20 74 68 69 73 20 6a 6f 75 72 d with this jour
167c0 6e 61 6c 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 nal pointer and
167c1 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a synced to disk..
167c2 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 69 73 20 **.** Once this
167c3 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 72 is routine has r
167c4 65 74 75 72 6e 65 64 2c 20 74 68 65 20 6f 6e 6c eturned, the onl
167c5 79 20 74 68 69 6e 67 20 72 65 71 75 69 72 65 64 y thing required
167c6 20 74 6f 20 63 6f 6d 6d 69 74 0a 2a 2a 20 74 68 to commit.** th
167c7 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 e write-transact
167c8 69 6f 6e 20 66 6f 72 20 74 68 69 73 20 64 61 74 ion for this dat
167c9 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 74 6f abase file is to
167ca 20 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 delete the jour
167cb 6e 61 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 nal..*/.SQLITE_P
167cc 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
167cd 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
167ce 73 65 4f 6e 65 28 42 74 72 65 65 20 2a 70 2c 20 seOne(Btree *p,
167cf 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 const char *zMas
167d0 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ter){. int rc =
167d1 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
167d2 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
167d3 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 ANS_WRITE ){.
167d4 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
167d5 20 70 2d 3e 70 42 74 3b 0a 20 20 20 20 73 71 6c p->pBt;. sql
167d6 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
167d7 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
167d8 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
167d9 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 M. if( pBt->a
167da 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 utoVacuum ){.
167db 20 20 20 72 63 20 3d 20 61 75 74 6f 56 61 63 75 rc = autoVacu
167dc 75 6d 43 6f 6d 6d 69 74 28 70 42 74 29 3b 0a 20 umCommit(pBt);.
167dd 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
167de 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
167df 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
167e0 61 76 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 ave(p);.
167e1 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
167e2 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
167e3 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
167e4 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 PagerCommitPhase
167e5 4f 6e 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c One(pBt->pPager,
167e6 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 zMaster, 0);.
167e7 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
167e8 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 ave(p);. }. re
167e9 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
167ea 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
167eb 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 62 is called from b
167ec 6f 74 68 20 42 74 72 65 65 43 6f 6d 6d 69 74 50 oth BtreeCommitP
167ed 68 61 73 65 54 77 6f 28 29 20 61 6e 64 20 42 74 haseTwo() and Bt
167ee 72 65 65 52 6f 6c 6c 62 61 63 6b 28 29 0a 2a 2a reeRollback().**
167ef 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 at the conclusi
167f0 6f 6e 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 on of a transact
167f1 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ion..*/.static v
167f2 6f 69 64 20 62 74 72 65 65 45 6e 64 54 72 61 6e oid btreeEndTran
167f3 73 61 63 74 69 6f 6e 28 42 74 72 65 65 20 2a 70 saction(Btree *p
167f4 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
167f5 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 Bt = p->pBt;. a
167f6 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 ssert( sqlite3Bt
167f7 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 reeHoldsMutex(p)
167f8 20 29 3b 0a 0a 20 20 62 74 72 65 65 43 6c 65 61 );.. btreeClea
167f9 72 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 29 rHasContent(pBt)
167fa 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 ;. if( p->inTra
167fb 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 ns>TRANS_NONE &&
167fc 20 70 2d 3e 64 62 2d 3e 61 63 74 69 76 65 56 64 p->db->activeVd
167fd 62 65 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f beCnt>1 ){. /
167fe 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6f * If there are o
167ff 74 68 65 72 20 61 63 74 69 76 65 20 73 74 61 74 ther active stat
16800 65 6d 65 6e 74 73 20 74 68 61 74 20 62 65 6c 6f ements that belo
16801 6e 67 20 74 6f 20 74 68 69 73 20 64 61 74 61 62 ng to this datab
16802 61 73 65 0a 20 20 20 20 2a 2a 20 68 61 6e 64 6c ase. ** handl
16803 65 2c 20 64 6f 77 6e 67 72 61 64 65 20 74 6f 20 e, downgrade to
16804 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e a read-only tran
16805 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 6f 74 68 saction. The oth
16806 65 72 20 73 74 61 74 65 6d 65 6e 74 73 0a 20 20 er statements.
16807 20 20 2a 2a 20 6d 61 79 20 73 74 69 6c 6c 20 62 ** may still b
16808 65 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 74 e reading from t
16809 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 2a 2f he database. */
1680a 0a 20 20 20 20 64 6f 77 6e 67 72 61 64 65 41 6c . downgradeAl
1680b 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c lSharedCacheTabl
1680c 65 4c 6f 63 6b 73 28 70 29 3b 0a 20 20 20 20 70 eLocks(p);. p
1680d 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e ->inTrans = TRAN
1680e 53 5f 52 45 41 44 3b 0a 20 20 7d 65 6c 73 65 7b S_READ;. }else{
1680f 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 68 . /* If the h
16810 61 6e 64 6c 65 20 68 61 64 20 61 6e 79 20 6b 69 andle had any ki
16811 6e 64 20 6f 66 20 74 72 61 6e 73 61 63 74 69 6f nd of transactio
16812 6e 20 6f 70 65 6e 2c 20 64 65 63 72 65 6d 65 6e n open, decremen
16813 74 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 74 72 t the . ** tr
16814 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 20 ansaction count
16815 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 62 74 of the shared bt
16816 72 65 65 2e 20 49 66 20 74 68 65 20 74 72 61 6e ree. If the tran
16817 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 20 0a 20 saction count .
16818 20 20 20 2a 2a 20 72 65 61 63 68 65 73 20 30 2c ** reaches 0,
16819 20 73 65 74 20 74 68 65 20 73 68 61 72 65 64 20 set the shared
1681a 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 4e state to TRANS_N
1681b 4f 4e 45 2e 20 54 68 65 20 75 6e 6c 6f 63 6b 42 ONE. The unlockB
1681c 74 72 65 65 49 66 55 6e 75 73 65 64 28 29 0a 20 treeIfUnused().
1681d 20 20 20 2a 2a 20 63 61 6c 6c 20 62 65 6c 6f 77 ** call below
1681e 20 77 69 6c 6c 20 75 6e 6c 6f 63 6b 20 74 68 65 will unlock the
1681f 20 70 61 67 65 72 2e 20 20 2a 2f 0a 20 20 20 20 pager. */.
16820 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d if( p->inTrans!=
16821 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 TRANS_NONE ){.
16822 20 20 20 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 clearAllShar
16823 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
16824 73 28 70 29 3b 0a 20 20 20 20 20 20 70 42 74 2d s(p);. pBt-
16825 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b >nTransaction--;
16826 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 70 42 . if( 0==pB
16827 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 t->nTransaction
16828 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e ){. pBt->
16829 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 inTransaction =
1682a 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 TRANS_NONE;.
1682b 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f }. }.. /
1682c 2a 20 53 65 74 20 74 68 65 20 63 75 72 72 65 6e * Set the curren
1682d 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 t transaction st
1682e 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e ate to TRANS_NON
1682f 45 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 E and unlock the
16830 20 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 20 69 . ** pager i
16831 66 20 74 68 69 73 20 63 61 6c 6c 20 63 6c 6f 73 f this call clos
16832 65 64 20 74 68 65 20 6f 6e 6c 79 20 72 65 61 64 ed the only read
16833 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 or write transa
16834 63 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 70 ction. */. p
16835 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e ->inTrans = TRAN
16836 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 75 6e 6c 6f S_NONE;. unlo
16837 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 ckBtreeIfUnused(
16838 70 42 74 29 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 pBt);. }.. btr
16839 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a eeIntegrity(p);.
1683a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 }../*.** Commit
1683b 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
1683c 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 70 72 6f currently in pro
1683d 67 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 gress..**.** Thi
1683e 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d s routine implem
1683f 65 6e 74 73 20 74 68 65 20 73 65 63 6f 6e 64 20 ents the second
16840 70 68 61 73 65 20 6f 66 20 61 20 32 2d 70 68 61 phase of a 2-pha
16841 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 65 0a se commit. The.
16842 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 ** sqlite3BtreeC
16843 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 ommitPhaseOne()
16844 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68 65 routine does the
16845 20 66 69 72 73 74 20 70 68 61 73 65 20 61 6e 64 first phase and
16846 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 69 6e should.** be in
16847 76 6f 6b 65 64 20 70 72 69 6f 72 20 74 6f 20 63 voked prior to c
16848 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 alling this rout
16849 69 6e 65 2e 20 20 54 68 65 20 73 71 6c 69 74 65 ine. The sqlite
1684a 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
1684b 65 4f 6e 65 28 29 0a 2a 2a 20 72 6f 75 74 69 6e eOne().** routin
1684c 65 20 64 69 64 20 61 6c 6c 20 74 68 65 20 77 6f e did all the wo
1684d 72 6b 20 6f 66 20 77 72 69 74 69 6e 67 20 69 6e rk of writing in
1684e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 74 6f formation out to
1684f 20 64 69 73 6b 20 61 6e 64 20 66 6c 75 73 68 69 disk and flushi
16850 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e ng the.** conten
16851 74 73 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 ts so that they
16852 61 72 65 20 77 72 69 74 74 65 6e 20 6f 6e 74 6f are written onto
16853 20 74 68 65 20 64 69 73 6b 20 70 6c 61 74 74 65 the disk platte
16854 72 2e 20 20 41 6c 6c 20 74 68 69 73 0a 2a 2a 20 r. All this.**
16855 72 6f 75 74 69 6e 65 20 68 61 73 20 74 6f 20 64 routine has to d
16856 6f 20 69 73 20 64 65 6c 65 74 65 20 6f 72 20 74 o is delete or t
16857 72 75 6e 63 61 74 65 20 6f 72 20 7a 65 72 6f 20 runcate or zero
16858 74 68 65 20 68 65 61 64 65 72 20 69 6e 20 74 68 the header in th
16859 65 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 e.** the rollbac
1685a 6b 20 6a 6f 75 72 6e 61 6c 20 28 77 68 69 63 68 k journal (which
1685b 20 63 61 75 73 65 73 20 74 68 65 20 74 72 61 6e causes the tran
1685c 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 saction to commi
1685d 74 29 20 61 6e 64 0a 2a 2a 20 64 72 6f 70 20 6c t) and.** drop l
1685e 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ocks..**.** This
1685f 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 74 68 will release th
16860 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 e write lock on
16861 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
16862 65 2e 20 20 49 66 20 74 68 65 72 65 0a 2a 2a 20 e. If there.**
16863 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 63 75 are no active cu
16864 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 72 rsors, it also r
16865 65 6c 65 61 73 65 73 20 74 68 65 20 72 65 61 64 eleases the read
16866 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 lock..*/.SQLITE
16867 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
16868 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 ite3BtreeCommitP
16869 68 61 73 65 54 77 6f 28 42 74 72 65 65 20 2a 70 haseTwo(Btree *p
1686a 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
1686b 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 Bt = p->pBt;..
1686c 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1686d 72 28 70 29 3b 0a 20 20 62 74 72 65 65 49 6e 74 r(p);. btreeInt
1686e 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 2f 2a egrity(p);.. /*
1686f 20 49 66 20 74 68 65 20 68 61 6e 64 6c 65 20 68 If the handle h
16870 61 73 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 as a write-trans
16871 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20 63 6f 6d action open, com
16872 6d 69 74 20 74 68 65 20 73 68 61 72 65 64 2d 62 mit the shared-b
16873 74 72 65 65 73 20 0a 20 20 2a 2a 20 74 72 61 6e trees . ** tran
16874 73 61 63 74 69 6f 6e 20 61 6e 64 20 73 65 74 20 saction and set
16875 74 68 65 20 73 68 61 72 65 64 20 73 74 61 74 65 the shared state
16876 20 74 6f 20 54 52 41 4e 53 5f 52 45 41 44 2e 0a to TRANS_READ..
16877 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e */. if( p->in
16878 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 Trans==TRANS_WRI
16879 54 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 TE ){. int rc
1687a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 ;. assert( pB
1687b 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
1687c 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b ==TRANS_WRITE );
1687d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 . assert( pBt
1687e 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 30 ->nTransaction>0
1687f 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c );. rc = sql
16880 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 ite3PagerCommitP
16881 68 61 73 65 54 77 6f 28 70 42 74 2d 3e 70 50 61 haseTwo(pBt->pPa
16882 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 ger);. if( rc
16883 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
16884 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
16885 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 20 eLeave(p);.
16886 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
16887 7d 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 }. pBt->inTra
16888 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 nsaction = TRANS
16889 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 62 74 _READ;. }.. bt
1688a 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 69 6f reeEndTransactio
1688b 6e 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 n(p);. sqlite3B
1688c 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
1688d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1688e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 62 6f ;.}../*.** Do bo
1688f 74 68 20 70 68 61 73 65 73 20 6f 66 20 61 20 63 th phases of a c
16890 6f 6d 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ommit..*/.SQLITE
16891 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
16892 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 ite3BtreeCommit(
16893 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 Btree *p){. int
16894 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 rc;. sqlite3Bt
16895 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 reeEnter(p);. r
16896 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
16897 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 CommitPhaseOne(p
16898 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d , 0);. if( rc==
16899 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1689a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1689b 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f eeCommitPhaseTwo
1689c 28 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 (p);. }. sqlit
1689d 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
1689e 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1689f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
168a0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
168a1 20 6e 75 6d 62 65 72 20 6f 66 20 77 72 69 74 65 number of write
168a2 2d 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e -cursors open on
168a3 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 20 54 68 this handle. Th
168a4 69 73 20 69 73 20 66 6f 72 20 75 73 65 0a 2a 2a is is for use.**
168a5 20 69 6e 20 61 73 73 65 72 74 28 29 20 65 78 70 in assert() exp
168a6 72 65 73 73 69 6f 6e 73 2c 20 73 6f 20 69 74 20 ressions, so it
168a7 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 is only compiled
168a8 20 69 66 20 4e 44 45 42 55 47 20 69 73 20 6e 6f if NDEBUG is no
168a9 74 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2a t.** defined..**
168aa 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70 .** For the purp
168ab 6f 73 65 73 20 6f 66 20 74 68 69 73 20 72 6f 75 oses of this rou
168ac 74 69 6e 65 2c 20 61 20 77 72 69 74 65 2d 63 75 tine, a write-cu
168ad 72 73 6f 72 20 69 73 20 61 6e 79 20 63 75 72 73 rsor is any curs
168ae 6f 72 20 74 68 61 74 0a 2a 2a 20 69 73 20 63 61 or that.** is ca
168af 70 61 62 6c 65 20 6f 66 20 77 72 69 74 69 6e 67 pable of writing
168b0 20 74 6f 20 74 68 65 20 64 61 74 61 62 73 65 2e to the databse.
168b1 20 20 54 68 61 74 20 6d 65 61 6e 73 20 74 68 65 That means the
168b2 20 63 75 72 73 6f 72 20 77 61 73 0a 2a 2a 20 6f cursor was.** o
168b3 72 69 67 69 6e 61 6c 6c 79 20 6f 70 65 6e 65 64 riginally opened
168b4 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61 6e 64 for writing and
168b5 20 74 68 65 20 63 75 72 73 6f 72 20 68 61 73 20 the cursor has
168b6 6e 6f 74 20 62 65 20 64 69 73 61 62 6c 65 64 0a not be disabled.
168b7 2a 2a 20 62 79 20 68 61 76 69 6e 67 20 69 74 73 ** by having its
168b8 20 73 74 61 74 65 20 63 68 61 6e 67 65 64 20 74 state changed t
168b9 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 2e 0a o CURSOR_FAULT..
168ba 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f */.static int co
168bb 75 6e 74 57 72 69 74 65 43 75 72 73 6f 72 73 28 untWriteCursors(
168bc 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a BtShared *pBt){.
168bd 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 BtCursor *pCur
168be 3b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20 ;. int r = 0;.
168bf 20 66 6f 72 28 70 43 75 72 3d 70 42 74 2d 3e 70 for(pCur=pBt->p
168c0 43 75 72 73 6f 72 3b 20 70 43 75 72 3b 20 70 43 Cursor; pCur; pC
168c1 75 72 3d 70 43 75 72 2d 3e 70 4e 65 78 74 29 7b ur=pCur->pNext){
168c2 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 77 . if( pCur->w
168c3 72 46 6c 61 67 20 26 26 20 70 43 75 72 2d 3e 65 rFlag && pCur->e
168c4 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 46 41 State!=CURSOR_FA
168c5 55 4c 54 20 29 20 72 2b 2b 3b 20 0a 20 20 7d 0a ULT ) r++; . }.
168c6 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 23 65 return r;.}.#e
168c7 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ndif../*.** This
168c8 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 routine sets th
168c9 65 20 73 74 61 74 65 20 74 6f 20 43 55 52 53 4f e state to CURSO
168ca 52 5f 46 41 55 4c 54 20 61 6e 64 20 74 68 65 20 R_FAULT and the
168cb 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 74 6f error.** code to
168cc 20 65 72 72 43 6f 64 65 20 66 6f 72 20 65 76 65 errCode for eve
168cd 72 79 20 63 75 72 73 6f 72 20 6f 6e 20 42 74 53 ry cursor on BtS
168ce 68 61 72 65 64 20 74 68 61 74 20 70 42 74 72 65 hared that pBtre
168cf 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 73 2e e.** references.
168d0 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 63 75 72 .**.** Every cur
168d1 73 6f 72 20 69 73 20 74 72 69 70 70 65 64 2c 20 sor is tripped,
168d2 69 6e 63 6c 75 64 69 6e 67 20 63 75 72 73 6f 72 including cursor
168d3 73 20 74 68 61 74 20 62 65 6c 6f 6e 67 0a 2a 2a s that belong.**
168d4 20 74 6f 20 6f 74 68 65 72 20 64 61 74 61 62 61 to other databa
168d5 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 se connections t
168d6 68 61 74 20 68 61 70 70 65 6e 20 74 6f 20 62 65 hat happen to be
168d7 20 73 68 61 72 69 6e 67 0a 2a 2a 20 74 68 65 20 sharing.** the
168d8 63 61 63 68 65 20 77 69 74 68 20 70 42 74 72 65 cache with pBtre
168d9 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f e..**.** This ro
168da 75 74 69 6e 65 20 67 65 74 73 20 63 61 6c 6c 65 utine gets calle
168db 64 20 77 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63 d when a rollbac
168dc 6b 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 41 6c 6c k occurs..** All
168dd 20 63 75 72 73 6f 72 73 20 75 73 69 6e 67 20 74 cursors using t
168de 68 65 20 73 61 6d 65 20 63 61 63 68 65 20 6d 75 he same cache mu
168df 73 74 20 62 65 20 74 72 69 70 70 65 64 0a 2a 2a st be tripped.**
168e0 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65 6d to prevent them
168e1 20 66 72 6f 6d 20 74 72 79 69 6e 67 20 74 6f 20 from trying to
168e2 75 73 65 20 74 68 65 20 62 74 72 65 65 20 61 66 use the btree af
168e3 74 65 72 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 ter.** the rollb
168e4 61 63 6b 2e 20 20 54 68 65 20 72 6f 6c 6c 62 61 ack. The rollba
168e5 63 6b 20 6d 61 79 20 68 61 76 65 20 64 65 6c 65 ck may have dele
168e6 74 65 64 20 74 61 62 6c 65 73 0a 2a 2a 20 6f 72 ted tables.** or
168e7 20 6d 6f 76 65 64 20 72 6f 6f 74 20 70 61 67 65 moved root page
168e8 73 2c 20 73 6f 20 69 74 20 69 73 20 6e 6f 74 20 s, so it is not
168e9 73 75 66 66 69 63 69 65 6e 74 20 74 6f 0a 2a 2a sufficient to.**
168ea 20 73 61 76 65 20 74 68 65 20 73 74 61 74 65 20 save the state
168eb 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 of the cursor.
168ec 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20 The cursor must
168ed 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 be.** invalidate
168ee 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
168ef 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
168f0 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 3BtreeTripAllCur
168f1 73 6f 72 73 28 42 74 72 65 65 20 2a 70 42 74 72 sors(Btree *pBtr
168f2 65 65 2c 20 69 6e 74 20 65 72 72 43 6f 64 65 29 ee, int errCode)
168f3 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b {. BtCursor *p;
168f4 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
168f5 6e 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 nter(pBtree);.
168f6 66 6f 72 28 70 3d 70 42 74 72 65 65 2d 3e 70 42 for(p=pBtree->pB
168f7 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 t->pCursor; p; p
168f8 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 =p->pNext){.
168f9 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 int i;. sqlit
168fa 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 e3BtreeClearCurs
168fb 6f 72 28 70 29 3b 0a 20 20 20 20 70 2d 3e 65 53 or(p);. p->eS
168fc 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 46 41 tate = CURSOR_FA
168fd 55 4c 54 3b 0a 20 20 20 20 70 2d 3e 73 6b 69 70 ULT;. p->skip
168fe 4e 65 78 74 20 3d 20 65 72 72 43 6f 64 65 3b 0a Next = errCode;.
168ff 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d for(i=0; i<=
16900 70 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a p->iPage; i++){.
16901 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
16902 65 28 70 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b e(p->apPage[i]);
16903 0a 20 20 20 20 20 20 70 2d 3e 61 70 50 61 67 65 . p->apPage
16904 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 [i] = 0;. }.
16905 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 }. sqlite3Btre
16906 65 4c 65 61 76 65 28 70 42 74 72 65 65 29 3b 0a eLeave(pBtree);.
16907 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 }../*.** Rollbac
16908 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f k the transactio
16909 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 2e 20 20 n in progress.
1690a 41 6c 6c 20 63 75 72 73 6f 72 73 20 77 69 6c 6c All cursors will
1690b 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 65 64 be.** invalided
1690c 20 62 79 20 74 68 69 73 20 6f 70 65 72 61 74 69 by this operati
1690d 6f 6e 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 on. Any attempt
1690e 20 74 6f 20 75 73 65 20 61 20 63 75 72 73 6f 72 to use a cursor
1690f 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 6f 70 65 .** that was ope
16910 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 n at the beginni
16911 6e 67 20 6f 66 20 74 68 69 73 20 6f 70 65 72 61 ng of this opera
16912 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73 75 6c 74 tion will result
16913 0a 2a 2a 20 69 6e 20 61 6e 20 65 72 72 6f 72 2e .** in an error.
16914 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c .**.** This will
16915 20 72 65 6c 65 61 73 65 20 74 68 65 20 77 72 69 release the wri
16916 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 te lock on the d
16917 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 atabase file. I
16918 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e f there.** are n
16919 6f 20 61 63 74 69 76 65 20 63 75 72 73 6f 72 73 o active cursors
1691a 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c 65 61 73 , it also releas
1691b 65 73 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b es the read lock
1691c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1691d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
1691e 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 treeRollback(Btr
1691f 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 ee *p){. int rc
16920 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
16921 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 t = p->pBt;. Me
16922 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b 0a 0a mPage *pPage1;..
16923 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
16924 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 73 ter(p);. rc = s
16925 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 aveAllCursors(pB
16926 74 2c 20 30 2c 20 30 29 3b 0a 23 69 66 6e 64 65 t, 0, 0);.#ifnde
16927 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
16928 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69 66 28 ARED_CACHE. if(
16929 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1692a 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 {. /* This is
1692b 20 61 20 68 6f 72 72 69 62 6c 65 20 73 69 74 75 a horrible situ
1692c 61 74 69 6f 6e 2e 20 41 6e 20 49 4f 20 6f 72 20 ation. An IO or
1692d 6d 61 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 6f malloc() error o
1692e 63 63 75 72 72 65 64 20 77 68 69 6c 73 74 0a 20 ccurred whilst.
1692f 20 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 ** trying to
16930 73 61 76 65 20 63 75 72 73 6f 72 20 70 6f 73 69 save cursor posi
16931 74 69 6f 6e 73 2e 20 49 66 20 74 68 69 73 20 69 tions. If this i
16932 73 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 s an automatic r
16933 6f 6c 6c 62 61 63 6b 20 28 61 73 0a 20 20 20 20 ollback (as.
16934 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 ** the result of
16935 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 6d a constraint, m
16936 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 alloc() failure
16937 6f 72 20 49 4f 20 65 72 72 6f 72 29 20 74 68 65 or IO error) the
16938 6e 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61 n . ** the ca
16939 63 68 65 20 6d 61 79 20 62 65 20 69 6e 74 65 72 che may be inter
1693a 6e 61 6c 6c 79 20 69 6e 63 6f 6e 73 69 73 74 65 nally inconsiste
1693b 6e 74 20 28 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 nt (not contain
1693c 76 61 6c 69 64 20 74 72 65 65 73 29 20 73 6f 0a valid trees) so.
1693d 20 20 20 20 2a 2a 20 77 65 20 63 61 6e 6e 6f 74 ** we cannot
1693e 20 73 69 6d 70 6c 79 20 72 65 74 75 72 6e 20 74 simply return t
1693f 68 65 20 65 72 72 6f 72 20 74 6f 20 74 68 65 20 he error to the
16940 63 61 6c 6c 65 72 2e 20 49 6e 73 74 65 61 64 2c caller. Instead,
16941 20 61 62 6f 72 74 20 0a 20 20 20 20 2a 2a 20 61 abort . ** a
16942 6c 6c 20 71 75 65 72 69 65 73 20 74 68 61 74 20 ll queries that
16943 6d 61 79 20 62 65 20 75 73 69 6e 67 20 61 6e 79 may be using any
16944 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 73 20 of the cursors
16945 74 68 61 74 20 66 61 69 6c 65 64 20 74 6f 20 73 that failed to s
16946 61 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ave.. */.
16947 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 sqlite3BtreeTrip
16948 41 6c 6c 43 75 72 73 6f 72 73 28 70 2c 20 72 63 AllCursors(p, rc
16949 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 );. }.#endif.
1694a 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 btreeIntegrity(p
1694b 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 );.. if( p->inT
1694c 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 rans==TRANS_WRIT
1694d 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 E ){. int rc2
1694e 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 54 ;.. assert( T
1694f 52 41 4e 53 5f 57 52 49 54 45 3d 3d 70 42 74 2d RANS_WRITE==pBt-
16950 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 >inTransaction )
16951 3b 0a 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 ;. rc2 = sqli
16952 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
16953 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 (pBt->pPager);.
16954 20 20 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49 if( rc2!=SQLI
16955 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
16956 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 7d 0a 0a c = rc2;. }..
16957 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6c 6c 62 /* The rollb
16958 61 63 6b 20 6d 61 79 20 68 61 76 65 20 64 65 73 ack may have des
16959 74 72 6f 79 65 64 20 74 68 65 20 70 50 61 67 65 troyed the pPage
1695a 31 2d 3e 61 44 61 74 61 20 76 61 6c 75 65 2e 20 1->aData value.
1695b 20 53 6f 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 So. ** call
1695c 62 74 72 65 65 47 65 74 50 61 67 65 28 29 20 6f btreeGetPage() o
1695d 6e 20 70 61 67 65 20 31 20 61 67 61 69 6e 20 74 n page 1 again t
1695e 6f 20 6d 61 6b 65 0a 20 20 20 20 2a 2a 20 73 75 o make. ** su
1695f 72 65 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 re pPage1->aData
16960 20 69 73 20 73 65 74 20 63 6f 72 72 65 63 74 6c is set correctl
16961 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 74 y. */. if( bt
16962 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
16963 31 2c 20 26 70 50 61 67 65 31 2c 20 30 29 3d 3d 1, &pPage1, 0)==
16964 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
16965 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
16966 50 61 67 65 31 29 3b 0a 20 20 20 20 7d 0a 20 20 Page1);. }.
16967 20 20 61 73 73 65 72 74 28 20 63 6f 75 6e 74 57 assert( countW
16968 72 69 74 65 43 75 72 73 6f 72 73 28 70 42 74 29 riteCursors(pBt)
16969 3d 3d 30 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e ==0 );. pBt->
1696a 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 inTransaction =
1696b 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 7d 0a TRANS_READ;. }.
1696c 0a 20 20 62 74 72 65 65 45 6e 64 54 72 61 6e 73 . btreeEndTrans
1696d 61 63 74 69 6f 6e 28 70 29 3b 0a 20 20 73 71 6c action(p);. sql
1696e 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
1696f 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
16970 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 72 74 20 61 }../*.** Start a
16971 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 statement subtr
16972 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 ansaction. The s
16973 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 ubtransaction ca
16974 6e 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 0a n can be rolled.
16975 2a 2a 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 ** back independ
16976 65 6e 74 6c 79 20 6f 66 20 74 68 65 20 6d 61 69 ently of the mai
16977 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 59 n transaction. Y
16978 6f 75 20 6d 75 73 74 20 73 74 61 72 74 20 61 20 ou must start a
16979 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 transaction .**
1697a 62 65 66 6f 72 65 20 73 74 61 72 74 69 6e 67 20 before starting
1697b 61 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e a subtransaction
1697c 2e 20 54 68 65 20 73 75 62 74 72 61 6e 73 61 63 . The subtransac
1697d 74 69 6f 6e 20 69 73 20 65 6e 64 65 64 20 61 75 tion is ended au
1697e 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 tomatically .**
1697f 69 66 20 74 68 65 20 6d 61 69 6e 20 74 72 61 6e if the main tran
16980 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 20 saction commits
16981 6f 72 20 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a or rolls back..*
16982 2a 0a 2a 2a 20 53 74 61 74 65 6d 65 6e 74 20 73 *.** Statement s
16983 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 ubtransactions a
16984 72 65 20 75 73 65 64 20 61 72 6f 75 6e 64 20 69 re used around i
16985 6e 64 69 76 69 64 75 61 6c 20 53 51 4c 20 73 74 ndividual SQL st
16986 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 atements.** that
16987 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 77 are contained w
16988 69 74 68 69 6e 20 61 20 42 45 47 49 4e 2e 2e 2e ithin a BEGIN...
16989 43 4f 4d 4d 49 54 20 62 6c 6f 63 6b 2e 20 20 49 COMMIT block. I
1698a 66 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a f a constraint.*
1698b 2a 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 * error occurs w
1698c 69 74 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d ithin the statem
1698d 65 6e 74 2c 20 74 68 65 20 65 66 66 65 63 74 20 ent, the effect
1698e 6f 66 20 74 68 61 74 20 6f 6e 65 20 73 74 61 74 of that one stat
1698f 65 6d 65 6e 74 0a 2a 2a 20 63 61 6e 20 62 65 20 ement.** can be
16990 72 6f 6c 6c 65 64 20 62 61 63 6b 20 77 69 74 68 rolled back with
16991 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 72 6f out having to ro
16992 6c 6c 62 61 63 6b 20 74 68 65 20 65 6e 74 69 72 llback the entir
16993 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a e transaction..*
16994 2a 0a 2a 2a 20 41 20 73 74 61 74 65 6d 65 6e 74 *.** A statement
16995 20 73 75 62 2d 74 72 61 6e 73 61 63 74 69 6f 6e sub-transaction
16996 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 is implemented
16997 61 73 20 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 as an anonymous
16998 73 61 76 65 70 6f 69 6e 74 2e 20 54 68 65 0a 2a savepoint. The.*
16999 2a 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 * value passed a
1699a 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 s the second par
1699b 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 74 6f ameter is the to
1699c 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 tal number of sa
1699d 76 65 70 6f 69 6e 74 73 2c 0a 2a 2a 20 69 6e 63 vepoints,.** inc
1699e 6c 75 64 69 6e 67 20 74 68 65 20 6e 65 77 20 61 luding the new a
1699f 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 nonymous savepoi
169a0 6e 74 2c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 nt, open on the
169a1 42 2d 54 72 65 65 2e 20 69 2e 65 2e 20 69 66 20 B-Tree. i.e. if
169a2 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 there.** are no
169a3 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 active savepoint
169a4 73 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 73 s and no other s
169a5 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 tatement-transac
169a6 74 69 6f 6e 73 20 6f 70 65 6e 2c 0a 2a 2a 20 69 tions open,.** i
169a7 53 74 61 74 65 6d 65 6e 74 20 69 73 20 31 2e 20 Statement is 1.
169a8 54 68 69 73 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 This anonymous s
169a9 61 76 65 70 6f 69 6e 74 20 63 61 6e 20 62 65 20 avepoint can be
169aa 72 65 6c 65 61 73 65 64 20 6f 72 20 72 6f 6c 6c released or roll
169ab 65 64 20 62 61 63 6b 0a 2a 2a 20 75 73 69 6e 67 ed back.** using
169ac 20 74 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 the sqlite3Btre
169ad 65 53 61 76 65 70 6f 69 6e 74 28 29 20 66 75 6e eSavepoint() fun
169ae 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ction..*/.SQLITE
169af 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
169b0 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 ite3BtreeBeginSt
169b1 6d 74 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 mt(Btree *p, int
169b2 20 69 53 74 61 74 65 6d 65 6e 74 29 7b 0a 20 20 iStatement){.
169b3 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 int rc;. BtShar
169b4 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
169b5 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
169b6 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 Enter(p);. asse
169b7 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d rt( p->inTrans==
169b8 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 TRANS_WRITE );.
169b9 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 72 65 assert( pBt->re
169ba 61 64 4f 6e 6c 79 3d 3d 30 20 29 3b 0a 20 20 61 adOnly==0 );. a
169bb 73 73 65 72 74 28 20 69 53 74 61 74 65 6d 65 6e ssert( iStatemen
169bc 74 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 t>0 );. assert(
169bd 20 69 53 74 61 74 65 6d 65 6e 74 3e 70 2d 3e 64 iStatement>p->d
169be 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 3b b->nSavepoint );
169bf 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 2d 3e . if( NEVER(p->
169c0 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 inTrans!=TRANS_W
169c1 52 49 54 45 20 7c 7c 20 70 42 74 2d 3e 72 65 61 RITE || pBt->rea
169c2 64 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 72 63 dOnly) ){. rc
169c3 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e = SQLITE_INTERN
169c4 41 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 AL;. }else{.
169c5 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e assert( pBt->in
169c6 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 Transaction==TRA
169c7 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 20 20 NS_WRITE );.
169c8 2f 2a 20 41 74 20 74 68 65 20 70 61 67 65 72 20 /* At the pager
169c9 6c 65 76 65 6c 2c 20 61 20 73 74 61 74 65 6d 65 level, a stateme
169ca 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 nt transaction i
169cb 73 20 61 20 73 61 76 65 70 6f 69 6e 74 20 77 69 s a savepoint wi
169cc 74 68 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 64 th. ** an ind
169cd 65 78 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 ex greater than
169ce 61 6c 6c 20 73 61 76 65 70 6f 69 6e 74 73 20 63 all savepoints c
169cf 72 65 61 74 65 64 20 65 78 70 6c 69 63 69 74 6c reated explicitl
169d0 79 20 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 53 y using. ** S
169d1 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 QL statements. I
169d2 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 t is illegal to
169d3 6f 70 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f 72 open, release or
169d4 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 0a 20 20 rollback any.
169d5 20 20 2a 2a 20 73 75 63 68 20 73 61 76 65 70 6f ** such savepo
169d6 69 6e 74 73 20 77 68 69 6c 65 20 74 68 65 20 73 ints while the s
169d7 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 tatement transac
169d8 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 tion savepoint i
169d9 73 20 61 63 74 69 76 65 2e 0a 20 20 20 20 2a 2f s active.. */
169da 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
169db 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 3PagerOpenSavepo
169dc 69 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c int(pBt->pPager,
169dd 20 69 53 74 61 74 65 6d 65 6e 74 29 3b 0a 20 20 iStatement);.
169de 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 }. sqlite3Btree
169df 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
169e0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
169e1 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d The second argum
169e2 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 ent to this func
169e3 74 69 6f 6e 2c 20 6f 70 2c 20 69 73 20 61 6c 77 tion, op, is alw
169e4 61 79 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f ays SAVEPOINT_RO
169e5 4c 4c 42 41 43 4b 0a 2a 2a 20 6f 72 20 53 41 56 LLBACK.** or SAV
169e6 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20 EPOINT_RELEASE.
169e7 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 69 This function ei
169e8 74 68 65 72 20 72 65 6c 65 61 73 65 73 20 6f 72 ther releases or
169e9 20 72 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65 0a rolls back the.
169ea 2a 2a 20 73 61 76 65 70 6f 69 6e 74 20 69 64 65 ** savepoint ide
169eb 6e 74 69 66 69 65 64 20 62 79 20 70 61 72 61 6d ntified by param
169ec 65 74 65 72 20 69 53 61 76 65 70 6f 69 6e 74 2c eter iSavepoint,
169ed 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 depending on th
169ee 65 20 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 6f e value .** of o
169ef 70 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c p..**.** Normall
169f0 79 2c 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 y, iSavepoint is
169f1 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 greater than or
169f2 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2e 20 equal to zero.
169f3 48 6f 77 65 76 65 72 2c 20 69 66 20 6f 70 20 69 However, if op i
169f4 73 0a 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f 52 s.** SAVEPOINT_R
169f5 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20 69 53 OLLBACK, then iS
169f6 61 76 65 70 6f 69 6e 74 20 6d 61 79 20 61 6c 73 avepoint may als
169f7 6f 20 62 65 20 2d 31 2e 20 49 6e 20 74 68 69 73 o be -1. In this
169f8 20 63 61 73 65 20 74 68 65 20 0a 2a 2a 20 63 6f case the .** co
169f9 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 65 6e ntents of the en
169fa 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e tire transaction
169fb 20 61 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b are rolled back
169fc 2e 20 54 68 69 73 20 69 73 20 64 69 66 66 65 72 . This is differ
169fd 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 61 20 6e 6f ent.** from a no
169fe 72 6d 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e rmal transaction
169ff 20 72 6f 6c 6c 62 61 63 6b 2c 20 61 73 20 6e 6f rollback, as no
16a00 20 6c 6f 63 6b 73 20 61 72 65 20 72 65 6c 65 61 locks are relea
16a01 73 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 74 sed and the.** t
16a02 72 61 6e 73 61 63 74 69 6f 6e 20 72 65 6d 61 69 ransaction remai
16a03 6e 73 20 6f 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 ns open..*/.SQLI
16a04 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
16a05 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70 qlite3BtreeSavep
16a06 6f 69 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69 oint(Btree *p, i
16a07 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65 nt op, int iSave
16a08 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 point){. int rc
16a09 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
16a0a 69 66 28 20 70 20 26 26 20 70 2d 3e 69 6e 54 72 if( p && p->inTr
16a0b 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
16a0c 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 ){. BtShared
16a0d 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
16a0e 20 20 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d assert( op==
16a0f 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 SAVEPOINT_RELEAS
16a10 45 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 E || op==SAVEPOI
16a11 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 NT_ROLLBACK );.
16a12 20 20 20 61 73 73 65 72 74 28 20 69 53 61 76 65 assert( iSave
16a13 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 28 69 53 61 point>=0 || (iSa
16a14 76 65 70 6f 69 6e 74 3d 3d 2d 31 20 26 26 20 6f vepoint==-1 && o
16a15 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c p==SAVEPOINT_ROL
16a16 4c 42 41 43 4b 29 20 29 3b 0a 20 20 20 20 73 71 LBACK) );. sq
16a17 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
16a18 70 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c p);. rc = sql
16a19 69 74 65 33 50 61 67 65 72 53 61 76 65 70 6f 69 ite3PagerSavepoi
16a1a 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 nt(pBt->pPager,
16a1b 6f 70 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b op, iSavepoint);
16a1c 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
16a1d 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
16a1e 72 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 rc = newDatabase
16a1f 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 (pBt);. }.
16a20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
16a21 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ve(p);. }. ret
16a22 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
16a23 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 63 75 Create a new cu
16a24 72 73 6f 72 20 66 6f 72 20 74 68 65 20 42 54 72 rsor for the BTr
16a25 65 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 69 73 ee whose root is
16a26 20 6f 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 on the page.**
16a27 69 54 61 62 6c 65 2e 20 49 66 20 61 20 72 65 61 iTable. If a rea
16a28 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 20 69 73 d-only cursor is
16a29 20 72 65 71 75 65 73 74 65 64 2c 20 69 74 20 69 requested, it i
16a2a 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a s assumed that.*
16a2b 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 61 6c 72 * the caller alr
16a2c 65 61 64 79 20 68 61 73 20 61 74 20 6c 65 61 73 eady has at leas
16a2d 74 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74 72 t a read-only tr
16a2e 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 0a 2a ansaction open.*
16a2f 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 * on the databas
16a30 65 20 61 6c 72 65 61 64 79 2e 20 49 66 20 61 20 e already. If a
16a31 77 72 69 74 65 2d 63 75 72 73 6f 72 20 69 73 20 write-cursor is
16a32 72 65 71 75 65 73 74 65 64 2c 20 74 68 65 6e 0a requested, then.
16a33 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 ** the caller is
16a34 20 61 73 73 75 6d 65 64 20 74 6f 20 68 61 76 65 assumed to have
16a35 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 20 74 an open write t
16a36 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a ransaction..**.*
16a37 2a 20 49 66 20 77 72 46 6c 61 67 3d 3d 30 2c 20 * If wrFlag==0,
16a38 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 then the cursor
16a39 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 can only be used
16a3a 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a 2a 2a for reading..**
16a3b 20 49 66 20 77 72 46 6c 61 67 3d 3d 31 2c 20 74 If wrFlag==1, t
16a3c 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 63 hen the cursor c
16a3d 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 72 an be used for r
16a3e 65 61 64 69 6e 67 20 6f 72 20 66 6f 72 0a 2a 2a eading or for.**
16a3f 20 77 72 69 74 69 6e 67 20 69 66 20 6f 74 68 65 writing if othe
16a40 72 20 63 6f 6e 64 69 74 69 6f 6e 73 20 66 6f 72 r conditions for
16a41 20 77 72 69 74 69 6e 67 20 61 72 65 20 61 6c 73 writing are als
16a42 6f 20 6d 65 74 2e 20 20 54 68 65 73 65 0a 2a 2a o met. These.**
16a43 20 61 72 65 20 74 68 65 20 63 6f 6e 64 69 74 69 are the conditi
16a44 6f 6e 73 20 74 68 61 74 20 6d 75 73 74 20 62 65 ons that must be
16a45 20 6d 65 74 20 69 6e 20 6f 72 64 65 72 20 66 6f met in order fo
16a46 72 20 77 72 69 74 69 6e 67 20 74 6f 0a 2a 2a 20 r writing to.**
16a47 62 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a be allowed:.**.*
16a48 2a 20 31 3a 20 20 54 68 65 20 63 75 72 73 6f 72 * 1: The cursor
16a49 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 must have been
16a4a 6f 70 65 6e 65 64 20 77 69 74 68 20 77 72 46 6c opened with wrFl
16a4b 61 67 3d 3d 31 0a 2a 2a 0a 2a 2a 20 32 3a 20 20 ag==1.**.** 2:
16a4c 4f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 Other database c
16a4d 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 68 61 74 20 onnections that
16a4e 73 68 61 72 65 20 74 68 65 20 73 61 6d 65 20 70 share the same p
16a4f 61 67 65 72 20 63 61 63 68 65 0a 2a 2a 20 20 20 ager cache.**
16a50 20 20 62 75 74 20 77 68 69 63 68 20 61 72 65 20 but which are
16a51 6e 6f 74 20 69 6e 20 74 68 65 20 52 45 41 44 5f not in the READ_
16a52 55 4e 43 4f 4d 4d 49 54 54 45 44 20 73 74 61 74 UNCOMMITTED stat
16a53 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 0a 2a e may not have.*
16a54 2a 20 20 20 20 20 63 75 72 73 6f 72 73 20 6f 70 * cursors op
16a55 65 6e 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d en with wrFlag==
16a56 30 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 74 61 0 on the same ta
16a57 62 6c 65 2e 20 20 4f 74 68 65 72 77 69 73 65 0a ble. Otherwise.
16a58 2a 2a 20 20 20 20 20 74 68 65 20 63 68 61 6e 67 ** the chang
16a59 65 73 20 6d 61 64 65 20 62 79 20 74 68 69 73 20 es made by this
16a5a 77 72 69 74 65 20 63 75 72 73 6f 72 20 77 6f 75 write cursor wou
16a5b 6c 64 20 62 65 20 76 69 73 69 62 6c 65 20 74 6f ld be visible to
16a5c 0a 2a 2a 20 20 20 20 20 74 68 65 20 72 65 61 64 .** the read
16a5d 20 63 75 72 73 6f 72 73 20 69 6e 20 74 68 65 20 cursors in the
16a5e 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 other database c
16a5f 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a onnection..**.**
16a60 20 33 3a 20 20 54 68 65 20 64 61 74 61 62 61 73 3: The databas
16a61 65 20 6d 75 73 74 20 62 65 20 77 72 69 74 61 62 e must be writab
16a62 6c 65 20 28 6e 6f 74 20 6f 6e 20 72 65 61 64 2d le (not on read-
16a63 6f 6e 6c 79 20 6d 65 64 69 61 29 0a 2a 2a 0a 2a only media).**.*
16a64 2a 20 34 3a 20 20 54 68 65 72 65 20 6d 75 73 74 * 4: There must
16a65 20 62 65 20 61 6e 20 61 63 74 69 76 65 20 74 72 be an active tr
16a66 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a ansaction..**.**
16a67 20 4e 6f 20 63 68 65 63 6b 69 6e 67 20 69 73 20 No checking is
16a68 64 6f 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 done to make sur
16a69 65 20 74 68 61 74 20 70 61 67 65 20 69 54 61 62 e that page iTab
16a6a 6c 65 20 72 65 61 6c 6c 79 20 69 73 20 74 68 65 le really is the
16a6b 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 .** root page of
16a6c 20 61 20 62 2d 74 72 65 65 2e 20 20 49 66 20 69 a b-tree. If i
16a6d 74 20 69 73 20 6e 6f 74 2c 20 74 68 65 6e 20 74 t is not, then t
16a6e 68 65 20 63 75 72 73 6f 72 20 61 63 71 75 69 72 he cursor acquir
16a6f 65 64 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 77 ed.** will not w
16a70 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a ork correctly..*
16a71 2a 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d *.** It is assum
16a72 65 64 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 ed that the sqli
16a73 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 te3BtreeCursorSi
16a74 7a 65 28 29 20 62 79 74 65 73 20 6f 66 20 6d 65 ze() bytes of me
16a75 6d 6f 72 79 20 0a 2a 2a 20 70 6f 69 6e 74 65 64 mory .** pointed
16a76 20 74 6f 20 62 79 20 70 43 75 72 20 68 61 76 65 to by pCur have
16a77 20 62 65 65 6e 20 7a 65 72 6f 65 64 20 62 79 20 been zeroed by
16a78 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 73 the caller..*/.s
16a79 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 43 tatic int btreeC
16a7a 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a ursor(. Btree *
16a7b 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p,
16a7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a7d 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a /* The btree */.
16a7e 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 int iTable,
16a7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a80 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 /* Root
16a81 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 74 page of table t
16a82 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 o open */. int
16a83 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 wrFlag,
16a84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a85 20 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 /* 1 to write
16a86 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f . 0 read-only */
16a87 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 . struct KeyInf
16a88 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 o *pKeyInfo,
16a89 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
16a8a 73 74 20 61 72 67 20 74 6f 20 63 6f 6d 70 61 72 st arg to compar
16a8b 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f ison function */
16a8c 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 . BtCursor *pCu
16a8d 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r
16a8e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 /* Spa
16a8f 63 65 20 66 6f 72 20 6e 65 77 20 63 75 72 73 6f ce for new curso
16a90 72 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 r */.){. BtShar
16a91 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
16a92 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
16a93 20 2f 2a 20 53 68 61 72 65 64 20 62 2d 74 72 65 /* Shared b-tre
16a94 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 0a 20 20 61 e handle */.. a
16a95 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 ssert( sqlite3Bt
16a96 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 reeHoldsMutex(p)
16a97 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72 );. assert( wr
16a98 46 6c 61 67 3d 3d 30 20 7c 7c 20 77 72 46 6c 61 Flag==0 || wrFla
16a99 67 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 g==1 );.. /* Th
16a9a 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 e following asse
16a9b 72 74 20 73 74 61 74 65 6d 65 6e 74 73 20 76 65 rt statements ve
16a9c 72 69 66 79 20 74 68 61 74 20 69 66 20 74 68 69 rify that if thi
16a9d 73 20 69 73 20 61 20 73 68 61 72 61 62 6c 65 20 s is a sharable
16a9e 0a 20 20 2a 2a 20 62 2d 74 72 65 65 20 64 61 74 . ** b-tree dat
16a9f 61 62 61 73 65 2c 20 74 68 65 20 63 6f 6e 6e 65 abase, the conne
16aa0 63 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67 ction is holding
16aa1 20 74 68 65 20 72 65 71 75 69 72 65 64 20 74 61 the required ta
16aa2 62 6c 65 20 6c 6f 63 6b 73 2c 20 0a 20 20 2a 2a ble locks, . **
16aa3 20 61 6e 64 20 74 68 61 74 20 6e 6f 20 6f 74 68 and that no oth
16aa4 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 er connection ha
16aa5 73 20 61 6e 79 20 6f 70 65 6e 20 63 75 72 73 6f s any open curso
16aa6 72 20 74 68 61 74 20 63 6f 6e 66 6c 69 63 74 73 r that conflicts
16aa7 20 77 69 74 68 20 0a 20 20 2a 2a 20 74 68 69 73 with . ** this
16aa8 20 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 73 73 lock. */. ass
16aa9 65 72 74 28 20 68 61 73 53 68 61 72 65 64 43 61 ert( hasSharedCa
16aaa 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 cheTableLock(p,
16aab 69 54 61 62 6c 65 2c 20 70 4b 65 79 49 6e 66 6f iTable, pKeyInfo
16aac 21 3d 30 2c 20 77 72 46 6c 61 67 2b 31 29 20 29 !=0, wrFlag+1) )
16aad 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 6c ;. assert( wrFl
16aae 61 67 3d 3d 30 20 7c 7c 20 21 68 61 73 52 65 61 ag==0 || !hasRea
16aaf 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 69 54 dConflicts(p, iT
16ab0 61 62 6c 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 able) );.. /* A
16ab1 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 63 ssert that the c
16ab2 61 6c 6c 65 72 20 68 61 73 20 6f 70 65 6e 65 64 aller has opened
16ab3 20 74 68 65 20 72 65 71 75 69 72 65 64 20 74 72 the required tr
16ab4 61 6e 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 ansaction. */.
16ab5 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 assert( p->inTra
16ab6 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b ns>TRANS_NONE );
16ab7 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 6c 61 . assert( wrFla
16ab8 67 3d 3d 30 20 7c 7c 20 70 2d 3e 69 6e 54 72 61 g==0 || p->inTra
16ab9 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 ns==TRANS_WRITE
16aba 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 );. assert( pBt
16abb 2d 3e 70 50 61 67 65 31 20 26 26 20 70 42 74 2d ->pPage1 && pBt-
16abc 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 29 >pPage1->aData )
16abd 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 77 ;.. if( NEVER(w
16abe 72 46 6c 61 67 20 26 26 20 70 42 74 2d 3e 72 65 rFlag && pBt->re
16abf 61 64 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 72 adOnly) ){. r
16ac0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 eturn SQLITE_REA
16ac1 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 66 28 DONLY;. }. if(
16ac2 20 69 54 61 62 6c 65 3d 3d 31 20 26 26 20 70 61 iTable==1 && pa
16ac3 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 gerPagecount(pBt
16ac4 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 )==0 ){. retu
16ac5 72 6e 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 3b rn SQLITE_EMPTY;
16ac6 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 74 . }.. /* Now t
16ac7 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 65 72 72 hat no other err
16ac8 6f 72 73 20 63 61 6e 20 6f 63 63 75 72 2c 20 66 ors can occur, f
16ac9 69 6e 69 73 68 20 66 69 6c 6c 69 6e 67 20 69 6e inish filling in
16aca 20 74 68 65 20 42 74 43 75 72 73 6f 72 0a 20 20 the BtCursor.
16acb 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 61 6e 64 ** variables and
16acc 20 6c 69 6e 6b 20 74 68 65 20 63 75 72 73 6f 72 link the cursor
16acd 20 69 6e 74 6f 20 74 68 65 20 42 74 53 68 61 72 into the BtShar
16ace 65 64 20 6c 69 73 74 2e 20 20 2a 2f 0a 20 20 70 ed list. */. p
16acf 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 3d 20 Cur->pgnoRoot =
16ad0 28 50 67 6e 6f 29 69 54 61 62 6c 65 3b 0a 20 20 (Pgno)iTable;.
16ad1 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31 pCur->iPage = -1
16ad2 3b 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e ;. pCur->pKeyIn
16ad3 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 fo = pKeyInfo;.
16ad4 20 70 43 75 72 2d 3e 70 42 74 72 65 65 20 3d 20 pCur->pBtree =
16ad5 70 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 20 3d p;. pCur->pBt =
16ad6 20 70 42 74 3b 0a 20 20 70 43 75 72 2d 3e 77 72 pBt;. pCur->wr
16ad7 46 6c 61 67 20 3d 20 28 75 38 29 77 72 46 6c 61 Flag = (u8)wrFla
16ad8 67 3b 0a 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 g;. pCur->pNext
16ad9 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b = pBt->pCursor;
16ada 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 . if( pCur->pNe
16adb 78 74 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e xt ){. pCur->
16adc 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 pNext->pPrev = p
16add 43 75 72 3b 0a 20 20 7d 0a 20 20 70 42 74 2d 3e Cur;. }. pBt->
16ade 70 43 75 72 73 6f 72 20 3d 20 70 43 75 72 3b 0a pCursor = pCur;.
16adf 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d pCur->eState =
16ae0 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b CURSOR_INVALID;
16ae1 0a 20 20 70 43 75 72 2d 3e 63 61 63 68 65 64 52 . pCur->cachedR
16ae2 6f 77 69 64 20 3d 20 30 3b 0a 20 20 72 65 74 75 owid = 0;. retu
16ae3 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
16ae4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
16ae5 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
16ae6 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a ursor(. Btree *
16ae7 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p,
16ae8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ae9 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 /* The btre
16aea 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c e */. int iTabl
16aeb 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
16aec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16aed 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 /* Root page
16aee 6f 66 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e of table to open
16aef 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 */. int wrFlag
16af0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
16af1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16af2 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e /* 1 to write.
16af3 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 0 read-only */.
16af4 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f struct KeyInfo
16af5 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 *pKeyInfo,
16af6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16af7 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 78 43 First arg to xC
16af8 6f 6d 70 61 72 65 28 29 20 2a 2f 0a 20 20 42 74 ompare() */. Bt
16af9 43 75 72 73 6f 72 20 2a 70 43 75 72 20 20 20 20 Cursor *pCur
16afa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16afb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 /* Wri
16afc 74 65 20 6e 65 77 20 63 75 72 73 6f 72 20 68 65 te new cursor he
16afd 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 re */.){. int r
16afe 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 c;. sqlite3Btre
16aff 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 eEnter(p);. rc
16b00 3d 20 62 74 72 65 65 43 75 72 73 6f 72 28 70 2c = btreeCursor(p,
16b01 20 69 54 61 62 6c 65 2c 20 77 72 46 6c 61 67 2c iTable, wrFlag,
16b02 20 70 4b 65 79 49 6e 66 6f 2c 20 70 43 75 72 29 pKeyInfo, pCur)
16b03 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
16b04 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
16b05 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
16b06 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 Return the size
16b07 6f 66 20 61 20 42 74 43 75 72 73 6f 72 20 6f 62 of a BtCursor ob
16b08 6a 65 63 74 20 69 6e 20 62 79 74 65 73 2e 0a 2a ject in bytes..*
16b09 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 *.** This interf
16b0a 61 63 65 73 20 69 73 20 6e 65 65 64 65 64 20 73 aces is needed s
16b0b 6f 20 74 68 61 74 20 75 73 65 72 73 20 6f 66 20 o that users of
16b0c 63 75 72 73 6f 72 73 20 63 61 6e 20 70 72 65 61 cursors can prea
16b0d 6c 6c 6f 63 61 74 65 0a 2a 2a 20 73 75 66 66 69 llocate.** suffi
16b0e 63 69 65 6e 74 20 73 74 6f 72 61 67 65 20 74 6f cient storage to
16b0f 20 68 6f 6c 64 20 61 20 63 75 72 73 6f 72 2e 20 hold a cursor.
16b10 20 54 68 65 20 42 74 43 75 72 73 6f 72 20 6f 62 The BtCursor ob
16b11 6a 65 63 74 20 69 73 20 6f 70 61 71 75 65 0a 2a ject is opaque.*
16b12 2a 20 74 6f 20 75 73 65 72 73 20 73 6f 20 74 68 * to users so th
16b13 65 79 20 63 61 6e 6e 6f 74 20 64 6f 20 74 68 65 ey cannot do the
16b14 20 73 69 7a 65 6f 66 28 29 20 74 68 65 6d 73 65 sizeof() themse
16b15 6c 76 65 73 20 2d 20 74 68 65 79 20 6d 75 73 74 lves - they must
16b16 20 63 61 6c 6c 0a 2a 2a 20 74 68 69 73 20 72 6f call.** this ro
16b17 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 utine..*/.SQLITE
16b18 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
16b19 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 ite3BtreeCursorS
16b1a 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 ize(void){. ret
16b1b 75 72 6e 20 73 69 7a 65 6f 66 28 42 74 43 75 72 urn sizeof(BtCur
16b1c 73 6f 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 sor);.}../*.** S
16b1d 65 74 20 74 68 65 20 63 61 63 68 65 64 20 72 6f et the cached ro
16b1e 77 69 64 20 76 61 6c 75 65 20 6f 66 20 65 76 65 wid value of eve
16b1f 72 79 20 63 75 72 73 6f 72 20 69 6e 20 74 68 65 ry cursor in the
16b20 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 66 same database f
16b21 69 6c 65 0a 2a 2a 20 61 73 20 70 43 75 72 20 61 ile.** as pCur a
16b22 6e 64 20 68 61 76 69 6e 67 20 74 68 65 20 73 61 nd having the sa
16b23 6d 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d me root page num
16b24 62 65 72 20 61 73 20 70 43 75 72 2e 20 20 54 68 ber as pCur. Th
16b25 65 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 73 65 e value is.** se
16b26 74 20 74 6f 20 69 52 6f 77 69 64 2e 0a 2a 2a 0a t to iRowid..**.
16b27 2a 2a 20 4f 6e 6c 79 20 70 6f 73 69 74 69 76 65 ** Only positive
16b28 20 72 6f 77 69 64 20 76 61 6c 75 65 73 20 61 72 rowid values ar
16b29 65 20 63 6f 6e 73 69 64 65 72 65 64 20 76 61 6c e considered val
16b2a 69 64 20 66 6f 72 20 74 68 69 73 20 63 61 63 68 id for this cach
16b2b 65 2e 0a 2a 2a 20 54 68 65 20 63 61 63 68 65 20 e..** The cache
16b2c 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 is initialized t
16b2d 6f 20 7a 65 72 6f 2c 20 69 6e 64 69 63 61 74 69 o zero, indicati
16b2e 6e 67 20 61 6e 20 69 6e 76 61 6c 69 64 20 63 61 ng an invalid ca
16b2f 63 68 65 2e 0a 2a 2a 20 41 20 62 74 72 65 65 20 che..** A btree
16b30 77 69 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 20 77 will work fine w
16b31 69 74 68 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 ith zero or nega
16b32 74 69 76 65 20 72 6f 77 69 64 73 2e 20 20 57 65 tive rowids. We
16b33 20 6a 75 73 74 20 63 61 6e 6e 6f 74 0a 2a 2a 20 just cannot.**
16b34 63 61 63 68 65 20 7a 65 72 6f 20 6f 72 20 6e 65 cache zero or ne
16b35 67 61 74 69 76 65 20 72 6f 77 69 64 73 2c 20 77 gative rowids, w
16b36 68 69 63 68 20 6d 65 61 6e 73 20 74 61 62 6c 65 hich means table
16b37 73 20 74 68 61 74 20 75 73 65 20 7a 65 72 6f 20 s that use zero
16b38 6f 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 72 or.** negative r
16b39 6f 77 69 64 73 20 6d 69 67 68 74 20 72 75 6e 20 owids might run
16b3a 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72 2e a little slower.
16b3b 20 20 42 75 74 20 69 6e 20 70 72 61 63 74 69 63 But in practic
16b3c 65 2c 20 7a 65 72 6f 0a 2a 2a 20 6f 72 20 6e 65 e, zero.** or ne
16b3d 67 61 74 69 76 65 20 72 6f 77 69 64 73 20 61 72 gative rowids ar
16b3e 65 20 76 65 72 79 20 75 6e 63 6f 6d 6d 6f 6e 20 e very uncommon
16b3f 73 6f 20 74 68 69 73 20 73 68 6f 75 6c 64 20 6e so this should n
16b40 6f 74 20 62 65 20 61 20 70 72 6f 62 6c 65 6d 2e ot be a problem.
16b41 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
16b42 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
16b43 74 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 treeSetCachedRow
16b44 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 id(BtCursor *pCu
16b45 72 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 r, sqlite3_int64
16b46 20 69 52 6f 77 69 64 29 7b 0a 20 20 42 74 43 75 iRowid){. BtCu
16b47 72 73 6f 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 rsor *p;. for(p
16b48 3d 70 43 75 72 2d 3e 70 42 74 2d 3e 70 43 75 72 =pCur->pBt->pCur
16b49 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 sor; p; p=p->pNe
16b4a 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e xt){. if( p->
16b4b 70 67 6e 6f 52 6f 6f 74 3d 3d 70 43 75 72 2d 3e pgnoRoot==pCur->
16b4c 70 67 6e 6f 52 6f 6f 74 20 29 20 70 2d 3e 63 61 pgnoRoot ) p->ca
16b4d 63 68 65 64 52 6f 77 69 64 20 3d 20 69 52 6f 77 chedRowid = iRow
16b4e 69 64 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 id;. }. assert
16b4f 28 20 70 43 75 72 2d 3e 63 61 63 68 65 64 52 6f ( pCur->cachedRo
16b50 77 69 64 3d 3d 69 52 6f 77 69 64 20 29 3b 0a 7d wid==iRowid );.}
16b51 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
16b52 68 65 20 63 61 63 68 65 64 20 72 6f 77 69 64 20 he cached rowid
16b53 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 63 75 for the given cu
16b54 72 73 6f 72 2e 20 20 41 20 6e 65 67 61 74 69 76 rsor. A negativ
16b55 65 20 6f 72 20 7a 65 72 6f 0a 2a 2a 20 72 65 74 e or zero.** ret
16b56 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 61 urn value indica
16b57 74 65 73 20 74 68 61 74 20 74 68 65 20 72 6f 77 tes that the row
16b58 69 64 20 63 61 63 68 65 20 69 73 20 69 6e 76 61 id cache is inva
16b59 6c 69 64 20 61 6e 64 20 73 68 6f 75 6c 64 20 62 lid and should b
16b5a 65 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20 49 e.** ignored. I
16b5b 66 20 74 68 65 20 72 6f 77 69 64 20 63 61 63 68 f the rowid cach
16b5c 65 20 68 61 73 20 6e 65 76 65 72 20 62 65 66 6f e has never befo
16b5d 72 65 20 62 65 65 6e 20 73 65 74 2c 20 74 68 65 re been set, the
16b5e 6e 20 61 0a 2a 2a 20 7a 65 72 6f 20 69 73 20 72 n a.** zero is r
16b5f 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 eturned..*/.SQLI
16b60 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 TE_PRIVATE sqlit
16b61 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e3_int64 sqlite3
16b62 42 74 72 65 65 47 65 74 43 61 63 68 65 64 52 6f BtreeGetCachedRo
16b63 77 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 wid(BtCursor *pC
16b64 75 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 ur){. return pC
16b65 75 72 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 3b ur->cachedRowid;
16b66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 .}../*.** Close
16b67 61 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 72 a cursor. The r
16b68 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 ead lock on the
16b69 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 database file is
16b6a 20 72 65 6c 65 61 73 65 64 0a 2a 2a 20 77 68 65 released.** whe
16b6b 6e 20 74 68 65 20 6c 61 73 74 20 63 75 72 73 6f n the last curso
16b6c 72 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a r is closed..*/.
16b6d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
16b6e 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
16b6f 6c 6f 73 65 43 75 72 73 6f 72 28 42 74 43 75 72 loseCursor(BtCur
16b70 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 42 74 sor *pCur){. Bt
16b71 72 65 65 20 2a 70 42 74 72 65 65 20 3d 20 70 43 ree *pBtree = pC
16b72 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 69 66 ur->pBtree;. if
16b73 28 20 70 42 74 72 65 65 20 29 7b 0a 20 20 20 20 ( pBtree ){.
16b74 69 6e 74 20 69 3b 0a 20 20 20 20 42 74 53 68 61 int i;. BtSha
16b75 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72 2d red *pBt = pCur-
16b76 3e 70 42 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 >pBt;. sqlite
16b77 33 42 74 72 65 65 45 6e 74 65 72 28 70 42 74 72 3BtreeEnter(pBtr
16b78 65 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ee);. sqlite3
16b79 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 BtreeClearCursor
16b7a 28 70 43 75 72 29 3b 0a 20 20 20 20 69 66 28 20 (pCur);. if(
16b7b 70 43 75 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 pCur->pPrev ){.
16b7c 20 20 20 20 20 70 43 75 72 2d 3e 70 50 72 65 76 pCur->pPrev
16b7d 2d 3e 70 4e 65 78 74 20 3d 20 70 43 75 72 2d 3e ->pNext = pCur->
16b7e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 pNext;. }else
16b7f 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 43 75 {. pBt->pCu
16b80 72 73 6f 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65 rsor = pCur->pNe
16b81 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 xt;. }. if
16b82 28 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b ( pCur->pNext ){
16b83 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4e 65 . pCur->pNe
16b84 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 43 75 72 xt->pPrev = pCur
16b85 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 ->pPrev;. }.
16b86 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 for(i=0; i<=p
16b87 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 Cur->iPage; i++)
16b88 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 {. releaseP
16b89 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 65 age(pCur->apPage
16b8a 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 [i]);. }.
16b8b 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 unlockBtreeIfUnu
16b8c 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 69 6e sed(pBt);. in
16b8d 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 validateOverflow
16b8e 43 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20 20 Cache(pCur);.
16b8f 20 2f 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 /* sqlite3_free
16b90 28 70 43 75 72 29 3b 20 2a 2f 0a 20 20 20 20 73 (pCur); */. s
16b91 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
16b92 28 70 42 74 72 65 65 29 3b 0a 20 20 7d 0a 20 20 (pBtree);. }.
16b93 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
16b94 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 ;.}../*.** Make
16b95 73 75 72 65 20 74 68 65 20 42 74 43 75 72 73 6f sure the BtCurso
16b96 72 2a 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 r* given in the
16b97 61 72 67 75 6d 65 6e 74 20 68 61 73 20 61 20 76 argument has a v
16b98 61 6c 69 64 0a 2a 2a 20 42 74 43 75 72 73 6f 72 alid.** BtCursor
16b99 2e 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e .info structure.
16b9a 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 61 If it is not a
16b9b 6c 72 65 61 64 79 20 76 61 6c 69 64 2c 20 63 61 lready valid, ca
16b9c 6c 6c 0a 2a 2a 20 62 74 72 65 65 50 61 72 73 65 ll.** btreeParse
16b9d 43 65 6c 6c 28 29 20 74 6f 20 66 69 6c 6c 20 69 Cell() to fill i
16b9e 74 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 42 74 43 75 t in..**.** BtCu
16b9f 72 73 6f 72 2e 69 6e 66 6f 20 69 73 20 61 20 63 rsor.info is a c
16ba0 61 63 68 65 20 6f 66 20 74 68 65 20 69 6e 66 6f ache of the info
16ba1 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 63 rmation in the c
16ba2 75 72 72 65 6e 74 20 63 65 6c 6c 2e 0a 2a 2a 20 urrent cell..**
16ba3 55 73 69 6e 67 20 74 68 69 73 20 63 61 63 68 65 Using this cache
16ba4 20 72 65 64 75 63 65 73 20 74 68 65 20 6e 75 6d reduces the num
16ba5 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 74 6f 20 ber of calls to
16ba6 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 btreeParseCell()
16ba7 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 37 2d 30 36 2d ..**.** 2007-06-
16ba8 32 35 3a 20 20 54 68 65 72 65 20 69 73 20 61 20 25: There is a
16ba9 62 75 67 20 69 6e 20 73 6f 6d 65 20 76 65 72 73 bug in some vers
16baa 69 6f 6e 73 20 6f 66 20 4d 53 56 43 20 74 68 61 ions of MSVC tha
16bab 74 20 63 61 75 73 65 20 74 68 65 0a 2a 2a 20 63 t cause the.** c
16bac 6f 6d 70 69 6c 65 72 20 74 6f 20 63 72 61 73 68 ompiler to crash
16bad 20 77 68 65 6e 20 67 65 74 43 65 6c 6c 49 6e 66 when getCellInf
16bae 6f 28 29 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 o() is implement
16baf 65 64 20 61 73 20 61 20 6d 61 63 72 6f 2e 0a 2a ed as a macro..*
16bb0 2a 20 42 75 74 20 74 68 65 72 65 20 69 73 20 61 * But there is a
16bb1 20 6d 65 61 73 75 72 65 61 62 6c 65 20 73 70 65 measureable spe
16bb2 65 64 20 61 64 76 61 6e 74 61 67 65 20 74 6f 20 ed advantage to
16bb3 75 73 69 6e 67 20 74 68 65 20 6d 61 63 72 6f 20 using the macro
16bb4 6f 6e 20 67 63 63 0a 2a 2a 20 28 77 68 65 6e 20 on gcc.** (when
16bb5 6c 65 73 73 20 63 6f 6d 70 69 6c 65 72 20 6f 70 less compiler op
16bb6 74 69 6d 69 7a 61 74 69 6f 6e 73 20 6c 69 6b 65 timizations like
16bb7 20 2d 4f 73 20 6f 72 20 2d 4f 30 20 61 72 65 20 -Os or -O0 are
16bb8 75 73 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 used and the.**
16bb9 63 6f 6d 70 69 6c 65 72 20 69 73 20 6e 6f 74 20 compiler is not
16bba 64 6f 69 6e 67 20 61 67 72 65 73 73 69 76 65 20 doing agressive
16bbb 69 6e 6c 69 6e 69 6e 67 2e 29 20 20 53 6f 20 77 inlining.) So w
16bbc 65 20 75 73 65 20 61 20 72 65 61 6c 20 66 75 6e e use a real fun
16bbd 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 4d 53 56 ction.** for MSV
16bbe 43 20 61 6e 64 20 61 20 6d 61 63 72 6f 20 66 6f C and a macro fo
16bbf 72 20 65 76 65 72 79 74 68 69 6e 67 20 65 6c 73 r everything els
16bc0 65 2e 20 20 54 69 63 6b 65 74 20 23 32 34 35 37 e. Ticket #2457
16bc1 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 ..*/.#ifndef NDE
16bc2 42 55 47 0a 20 20 73 74 61 74 69 63 20 76 6f 69 BUG. static voi
16bc3 64 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f d assertCellInfo
16bc4 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
16bc5 7b 0a 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 {. CellInfo i
16bc6 6e 66 6f 3b 0a 20 20 20 20 69 6e 74 20 69 50 61 nfo;. int iPa
16bc7 67 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 ge = pCur->iPage
16bc8 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 69 6e ;. memset(&in
16bc9 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 69 6e fo, 0, sizeof(in
16bca 66 6f 29 29 3b 0a 20 20 20 20 62 74 72 65 65 50 fo));. btreeP
16bcb 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61 arseCell(pCur->a
16bcc 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 20 70 43 pPage[iPage], pC
16bcd 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 5d ur->aiIdx[iPage]
16bce 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 61 73 , &info);. as
16bcf 73 65 72 74 28 20 6d 65 6d 63 6d 70 28 26 69 6e sert( memcmp(&in
16bd0 66 6f 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f 2c fo, &pCur->info,
16bd1 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3d 3d sizeof(info))==
16bd2 30 20 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 0 );. }.#else.
16bd3 20 23 64 65 66 69 6e 65 20 61 73 73 65 72 74 43 #define assertC
16bd4 65 6c 6c 49 6e 66 6f 28 78 29 0a 23 65 6e 64 69 ellInfo(x).#endi
16bd5 66 0a 23 69 66 64 65 66 20 5f 4d 53 43 5f 56 45 f.#ifdef _MSC_VE
16bd6 52 0a 20 20 2f 2a 20 55 73 65 20 61 20 72 65 61 R. /* Use a rea
16bd7 6c 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 4d 53 l function in MS
16bd8 56 43 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e VC to work aroun
16bd9 64 20 62 75 67 73 20 69 6e 20 74 68 61 74 20 63 d bugs in that c
16bda 6f 6d 70 69 6c 65 72 2e 20 2a 2f 0a 20 20 73 74 ompiler. */. st
16bdb 61 74 69 63 20 76 6f 69 64 20 67 65 74 43 65 6c atic void getCel
16bdc 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 20 2a lInfo(BtCursor *
16bdd 70 43 75 72 29 7b 0a 20 20 20 20 69 66 28 20 70 pCur){. if( p
16bde 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d Cur->info.nSize=
16bdf 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 =0 ){. int
16be0 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 50 iPage = pCur->iP
16be1 61 67 65 3b 0a 20 20 20 20 20 20 62 74 72 65 65 age;. btree
16be2 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e ParseCell(pCur->
16be3 61 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 70 43 apPage[iPage],pC
16be4 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 5d ur->aiIdx[iPage]
16be5 2c 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20 ,&pCur->info);.
16be6 20 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 pCur->valid
16be7 4e 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 7d 65 NKey = 1;. }e
16be8 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 lse{. asser
16be9 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b tCellInfo(pCur);
16bea 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 . }. }.#else
16beb 20 2f 2a 20 69 66 20 6e 6f 74 20 5f 4d 53 43 5f /* if not _MSC_
16bec 56 45 52 20 2a 2f 0a 20 20 2f 2a 20 55 73 65 20 VER */. /* Use
16bed 61 20 6d 61 63 72 6f 20 69 6e 20 61 6c 6c 20 6f a macro in all o
16bee 74 68 65 72 20 63 6f 6d 70 69 6c 65 72 73 20 73 ther compilers s
16bef 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 o that the funct
16bf0 69 6f 6e 20 69 73 20 69 6e 6c 69 6e 65 64 20 2a ion is inlined *
16bf1 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 43 65 6c /.#define getCel
16bf2 6c 49 6e 66 6f 28 70 43 75 72 29 20 20 20 20 20 lInfo(pCur)
16bf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bf5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bf6 20 5c 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69 \. if( pCur->i
16bf7 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 20 nfo.nSize==0 ){
16bf8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bf9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bfa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bfb 20 20 5c 0a 20 20 20 20 69 6e 74 20 69 50 61 67 \. int iPag
16bfc 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b e = pCur->iPage;
16bfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c00 20 20 20 5c 0a 20 20 20 20 62 74 72 65 65 50 61 \. btreePa
16c01 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 rseCell(pCur->ap
16c02 50 61 67 65 5b 69 50 61 67 65 5d 2c 70 43 75 72 Page[iPage],pCur
16c03 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 5d 2c 26 ->aiIdx[iPage],&
16c04 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 20 5c 0a 20 pCur->info); \.
16c05 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b pCur->validNK
16c06 65 79 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 ey = 1;
16c07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
16c0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20 }else{
16c0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
16c0f 0a 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 . assertCellI
16c10 6e 66 6f 28 70 43 75 72 29 3b 20 20 20 20 20 20 nfo(pCur);
16c11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c14 5c 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 \. }.#endif /*
16c15 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 0a 23 69 66 _MSC_VER */..#if
16c16 6e 64 65 66 20 4e 44 45 42 55 47 20 20 2f 2a 20 ndef NDEBUG /*
16c17 54 68 65 20 6e 65 78 74 20 72 6f 75 74 69 6e 65 The next routine
16c18 20 75 73 65 64 20 6f 6e 6c 79 20 77 69 74 68 69 used only withi
16c19 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 n assert() state
16c1a 6d 65 6e 74 73 20 2a 2f 0a 2f 2a 0a 2a 2a 20 52 ments */./*.** R
16c1b 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 eturn true if th
16c1c 65 20 67 69 76 65 6e 20 42 74 43 75 72 73 6f 72 e given BtCursor
16c1d 20 69 73 20 76 61 6c 69 64 2e 20 20 41 20 76 61 is valid. A va
16c1e 6c 69 64 20 63 75 72 73 6f 72 20 69 73 20 6f 6e lid cursor is on
16c1f 65 0a 2a 2a 20 74 68 61 74 20 69 73 20 63 75 72 e.** that is cur
16c20 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 rently pointing
16c21 74 6f 20 61 20 72 6f 77 20 69 6e 20 61 20 28 6e to a row in a (n
16c22 6f 6e 2d 65 6d 70 74 79 29 20 74 61 62 6c 65 2e on-empty) table.
16c23 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 76 65 .** This is a ve
16c24 72 69 66 69 63 61 74 69 6f 6e 20 72 6f 75 74 69 rification routi
16c25 6e 65 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 ne is used only
16c26 77 69 74 68 69 6e 20 61 73 73 65 72 74 28 29 20 within assert()
16c27 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 statements..*/.S
16c28 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
16c29 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 t sqlite3BtreeCu
16c2a 72 73 6f 72 49 73 56 61 6c 69 64 28 42 74 43 75 rsorIsValid(BtCu
16c2b 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72 rsor *pCur){. r
16c2c 65 74 75 72 6e 20 70 43 75 72 20 26 26 20 70 43 eturn pCur && pC
16c2d 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
16c2e 4f 52 5f 56 41 4c 49 44 3b 0a 7d 0a 23 65 6e 64 OR_VALID;.}.#end
16c2f 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a if /* NDEBUG */.
16c30 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 53 69 7a ./*.** Set *pSiz
16c31 65 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 e to the size of
16c32 20 74 68 65 20 62 75 66 66 65 72 20 6e 65 65 64 the buffer need
16c33 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 76 ed to hold the v
16c34 61 6c 75 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6b alue of.** the k
16c35 65 79 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 ey for the curre
16c36 6e 74 20 65 6e 74 72 79 2e 20 20 49 66 20 74 68 nt entry. If th
16c37 65 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 e cursor is not
16c38 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f 20 61 pointing.** to a
16c39 20 76 61 6c 69 64 20 65 6e 74 72 79 2c 20 2a 70 valid entry, *p
16c3a 53 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 30 Size is set to 0
16c3b 2e 20 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 74 . .**.** For a t
16c3c 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 49 4e able with the IN
16c3d 54 4b 45 59 20 66 6c 61 67 20 73 65 74 2c 20 74 TKEY flag set, t
16c3e 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
16c3f 72 6e 73 20 74 68 65 20 6b 65 79 0a 2a 2a 20 69 rns the key.** i
16c40 74 73 65 6c 66 2c 20 6e 6f 74 20 74 68 65 20 6e tself, not the n
16c41 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
16c42 6e 20 74 68 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a n the key..**.**
16c43 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 The caller must
16c44 20 70 6f 73 69 74 69 6f 6e 20 74 68 65 20 63 75 position the cu
16c45 72 73 6f 72 20 70 72 69 6f 72 20 74 6f 20 69 6e rsor prior to in
16c46 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 74 voking this rout
16c47 69 6e 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 ine..** .** This
16c48 20 72 6f 75 74 69 6e 65 20 63 61 6e 6e 6f 74 20 routine cannot
16c49 66 61 69 6c 2e 20 20 49 74 20 61 6c 77 61 79 73 fail. It always
16c4a 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
16c4b 4f 4b 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f OK. .*/.SQLITE_
16c4c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
16c4d 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 te3BtreeKeySize(
16c4e 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
16c4f 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 61 i64 *pSize){. a
16c50 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
16c51 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
16c52 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
16c53 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
16c54 49 4e 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d INVALID || pCur-
16c55 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
16c56 56 41 4c 49 44 20 29 3b 0a 20 20 69 66 28 20 70 VALID );. if( p
16c57 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 Cur->eState!=CUR
16c58 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 SOR_VALID ){.
16c59 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 20 20 7d *pSize = 0;. }
16c5a 65 6c 73 65 7b 0a 20 20 20 20 67 65 74 43 65 6c else{. getCel
16c5b 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 20 lInfo(pCur);.
16c5c 20 2a 70 53 69 7a 65 20 3d 20 70 43 75 72 2d 3e *pSize = pCur->
16c5d 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20 info.nKey;. }.
16c5e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
16c5f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 K;.}../*.** Set
16c60 2a 70 53 69 7a 65 20 74 6f 20 74 68 65 20 6e 75 *pSize to the nu
16c61 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
16c62 20 64 61 74 61 20 69 6e 20 74 68 65 20 65 6e 74 data in the ent
16c63 72 79 20 74 68 65 0a 2a 2a 20 63 75 72 73 6f 72 ry the.** cursor
16c64 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
16c65 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 s to..**.** The
16c66 63 61 6c 6c 65 72 20 6d 75 73 74 20 67 75 61 72 caller must guar
16c67 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 63 antee that the c
16c68 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e ursor is pointin
16c69 67 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 0a g to a non-NULL.
16c6a 2a 2a 20 76 61 6c 69 64 20 65 6e 74 72 79 2e 20 ** valid entry.
16c6b 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
16c6c 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f the calling pro
16c6d 63 65 64 75 72 65 20 6d 75 73 74 20 67 75 61 72 cedure must guar
16c6e 61 6e 74 65 65 0a 2a 2a 20 74 68 61 74 20 74 68 antee.** that th
16c6f 65 20 63 75 72 73 6f 72 20 68 61 73 20 43 75 72 e cursor has Cur
16c70 73 6f 72 2e 65 53 74 61 74 65 3d 3d 43 55 52 53 sor.eState==CURS
16c71 4f 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a 20 OR_VALID..**.**
16c72 46 61 69 6c 75 72 65 20 69 73 20 6e 6f 74 20 70 Failure is not p
16c73 6f 73 73 69 62 6c 65 2e 20 20 54 68 69 73 20 66 ossible. This f
16c74 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 72 unction always r
16c75 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b eturns SQLITE_OK
16c76 2e 0a 2a 2a 20 49 74 20 6d 69 67 68 74 20 6a 75 ..** It might ju
16c77 73 74 20 61 73 20 77 65 6c 6c 20 62 65 20 61 20 st as well be a
16c78 70 72 6f 63 65 64 75 72 65 20 28 72 65 74 75 72 procedure (retur
16c79 6e 69 6e 67 20 76 6f 69 64 29 20 62 75 74 20 77 ning void) but w
16c7a 65 20 63 6f 6e 74 69 6e 75 65 0a 2a 2a 20 74 6f e continue.** to
16c7b 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 return an integ
16c7c 65 72 20 72 65 73 75 6c 74 20 63 6f 64 65 20 66 er result code f
16c7d 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 or historical re
16c7e 61 73 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 asons..*/.SQLITE
16c7f 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
16c80 69 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a ite3BtreeDataSiz
16c81 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 e(BtCursor *pCur
16c82 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 7b 0a 20 , u32 *pSize){.
16c83 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
16c84 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
16c85 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
16c86 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
16c87 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 67 65 74 R_VALID );. get
16c88 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a CellInfo(pCur);.
16c89 20 20 2a 70 53 69 7a 65 20 3d 20 70 43 75 72 2d *pSize = pCur-
16c8a 3e 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 72 >info.nData;. r
16c8b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
16c8c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 .}../*.** Given
16c8d 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 the page number
16c8e 6f 66 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 of an overflow p
16c8f 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 age in the datab
16c90 61 73 65 20 28 70 61 72 61 6d 65 74 65 72 0a 2a ase (parameter.*
16c91 2a 20 6f 76 66 6c 29 2c 20 74 68 69 73 20 66 75 * ovfl), this fu
16c92 6e 63 74 69 6f 6e 20 66 69 6e 64 73 20 74 68 65 nction finds the
16c93 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
16c94 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e the next page in
16c95 20 74 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 the .** linked
16c96 6c 69 73 74 20 6f 66 20 6f 76 65 72 66 6c 6f 77 list of overflow
16c97 20 70 61 67 65 73 2e 20 49 66 20 70 6f 73 73 69 pages. If possi
16c98 62 6c 65 2c 20 69 74 20 75 73 65 73 20 74 68 65 ble, it uses the
16c99 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 auto-vacuum.**
16c9a 70 6f 69 6e 74 65 72 2d 6d 61 70 20 64 61 74 61 pointer-map data
16c9b 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65 61 64 instead of read
16c9c 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 ing the content
16c9d 6f 66 20 70 61 67 65 20 6f 76 66 6c 20 74 6f 20 of page ovfl to
16c9e 64 6f 20 73 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 do so. .**.** If
16c9f 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
16ca0 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 an SQLite error
16ca1 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
16ca2 64 2e 20 4f 74 68 65 72 77 69 73 65 3a 0a 2a 2a d. Otherwise:.**
16ca3 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d .** The page num
16ca4 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20 ber of the next
16ca5 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e overflow page in
16ca6 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 the linked list
16ca7 20 69 73 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 is .** written
16ca8 74 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 2e 20 49 to *pPgnoNext. I
16ca9 66 20 70 61 67 65 20 6f 76 66 6c 20 69 73 20 74 f page ovfl is t
16caa 68 65 20 6c 61 73 74 20 70 61 67 65 20 69 6e 20 he last page in
16cab 69 74 73 20 6c 69 6e 6b 65 64 20 0a 2a 2a 20 6c its linked .** l
16cac 69 73 74 2c 20 2a 70 50 67 6e 6f 4e 65 78 74 20 ist, *pPgnoNext
16cad 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 is set to zero.
16cae 0a 2a 2a 0a 2a 2a 20 49 66 20 70 70 50 61 67 65 .**.** If ppPage
16caf 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 61 6e is not NULL, an
16cb0 64 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f d a reference to
16cb1 20 74 68 65 20 4d 65 6d 50 61 67 65 20 6f 62 6a the MemPage obj
16cb2 65 63 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e ect correspondin
16cb3 67 0a 2a 2a 20 74 6f 20 70 61 67 65 20 6e 75 6d g.** to page num
16cb4 62 65 72 20 70 4f 76 66 6c 20 77 61 73 20 6f 62 ber pOvfl was ob
16cb5 74 61 69 6e 65 64 2c 20 74 68 65 6e 20 2a 70 70 tained, then *pp
16cb6 50 61 67 65 20 69 73 20 73 65 74 20 74 6f 20 70 Page is set to p
16cb7 6f 69 6e 74 20 74 6f 20 74 68 61 74 0a 2a 2a 20 oint to that.**
16cb8 72 65 66 65 72 65 6e 63 65 2e 20 49 74 20 69 73 reference. It is
16cb9 20 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c the responsibil
16cba 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 ity of the calle
16cbb 72 20 74 6f 20 63 61 6c 6c 20 72 65 6c 65 61 73 r to call releas
16cbc 65 50 61 67 65 28 29 0a 2a 2a 20 6f 6e 20 2a 70 ePage().** on *p
16cbd 70 50 61 67 65 20 74 6f 20 66 72 65 65 20 74 68 pPage to free th
16cbe 65 20 72 65 66 65 72 65 6e 63 65 2e 20 49 6e 20 e reference. In
16cbf 6e 6f 20 72 65 66 65 72 65 6e 63 65 20 77 61 73 no reference was
16cc0 20 6f 62 74 61 69 6e 65 64 20 28 62 65 63 61 75 obtained (becau
16cc1 73 65 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 se.** the pointe
16cc2 72 2d 6d 61 70 20 77 61 73 20 75 73 65 64 20 74 r-map was used t
16cc3 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 76 61 6c o obtain the val
16cc4 75 65 20 66 6f 72 20 2a 70 50 67 6e 6f 4e 65 78 ue for *pPgnoNex
16cc5 74 29 2c 20 74 68 65 6e 0a 2a 2a 20 2a 70 70 50 t), then.** *ppP
16cc6 61 67 65 20 69 73 20 73 65 74 20 74 6f 20 7a 65 age is set to ze
16cc7 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ro..*/.static in
16cc8 74 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 t getOverflowPag
16cc9 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 e(. BtShared *p
16cca 42 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 Bt,
16ccb 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 /* The databas
16ccc 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f e file */. Pgno
16ccd 20 6f 76 66 6c 2c 20 20 20 20 20 20 20 20 20 20 ovfl,
16cce 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 /* Curr
16ccf 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 ent overflow pag
16cd0 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 4d 65 e number */. Me
16cd1 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 mPage **ppPage,
16cd2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 /* OU
16cd3 54 3a 20 4d 65 6d 50 61 67 65 20 68 61 6e 64 6c T: MemPage handl
16cd4 65 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20 e (may be NULL)
16cd5 2a 2f 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f */. Pgno *pPgno
16cd6 4e 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20 Next
16cd7 20 20 2f 2a 20 4f 55 54 3a 20 4e 65 78 74 20 6f /* OUT: Next o
16cd8 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d verflow page num
16cd9 62 65 72 20 2a 2f 0a 29 7b 0a 20 20 50 67 6e 6f ber */.){. Pgno
16cda 20 6e 65 78 74 20 3d 20 30 3b 0a 20 20 4d 65 6d next = 0;. Mem
16cdb 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30 3b Page *pPage = 0;
16cdc 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
16cdd 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 TE_OK;.. assert
16cde 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
16cdf 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
16ce0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 70 50 67 );. assert(pPg
16ce1 6e 6f 4e 65 78 74 29 3b 0a 0a 23 69 66 6e 64 65 noNext);..#ifnde
16ce2 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
16ce3 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20 54 72 TOVACUUM. /* Tr
16ce4 79 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 65 y to find the ne
16ce5 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f xt page in the o
16ce6 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 75 73 69 verflow list usi
16ce7 6e 67 20 74 68 65 0a 20 20 2a 2a 20 61 75 74 6f ng the. ** auto
16ce8 76 61 63 75 75 6d 20 70 6f 69 6e 74 65 72 2d 6d vacuum pointer-m
16ce9 61 70 20 70 61 67 65 73 2e 20 47 75 65 73 73 20 ap pages. Guess
16cea 74 68 61 74 20 74 68 65 20 6e 65 78 74 20 70 61 that the next pa
16ceb 67 65 20 69 6e 20 0a 20 20 2a 2a 20 74 68 65 20 ge in . ** the
16cec 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 69 73 overflow list is
16ced 20 70 61 67 65 20 6e 75 6d 62 65 72 20 28 6f 76 page number (ov
16cee 66 6c 2b 31 29 2e 20 49 66 20 74 68 61 74 20 67 fl+1). If that g
16cef 75 65 73 73 20 74 75 72 6e 73 20 0a 20 20 2a 2a uess turns . **
16cf0 20 6f 75 74 20 74 6f 20 62 65 20 77 72 6f 6e 67 out to be wrong
16cf1 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 6c , fall back to l
16cf2 6f 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 20 oading the data
16cf3 6f 66 20 70 61 67 65 20 0a 20 20 2a 2a 20 6e 75 of page . ** nu
16cf4 6d 62 65 72 20 6f 76 66 6c 20 74 6f 20 64 65 74 mber ovfl to det
16cf5 65 72 6d 69 6e 65 20 74 68 65 20 6e 65 78 74 20 ermine the next
16cf6 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 20 20 2a page number.. *
16cf7 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 /. if( pBt->aut
16cf8 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 oVacuum ){. P
16cf9 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 20 20 50 67 gno pgno;. Pg
16cfa 6e 6f 20 69 47 75 65 73 73 20 3d 20 6f 76 66 6c no iGuess = ovfl
16cfb 2b 31 3b 0a 20 20 20 20 75 38 20 65 54 79 70 65 +1;. u8 eType
16cfc 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 50 54 ;.. while( PT
16cfd 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c RMAP_ISPAGE(pBt,
16cfe 20 69 47 75 65 73 73 29 20 7c 7c 20 69 47 75 65 iGuess) || iGue
16cff 73 73 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 ss==PENDING_BYTE
16d00 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 _PAGE(pBt) ){.
16d01 20 20 20 20 69 47 75 65 73 73 2b 2b 3b 0a 20 20 iGuess++;.
16d02 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 69 47 75 }.. if( iGu
16d03 65 73 73 3c 3d 70 61 67 65 72 50 61 67 65 63 6f ess<=pagerPageco
16d04 75 6e 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 unt(pBt) ){.
16d05 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 rc = ptrmapGet
16d06 28 70 42 74 2c 20 69 47 75 65 73 73 2c 20 26 65 (pBt, iGuess, &e
16d07 54 79 70 65 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 Type, &pgno);.
16d08 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
16d09 54 45 5f 4f 4b 20 26 26 20 65 54 79 70 65 3d 3d TE_OK && eType==
16d0a 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 PTRMAP_OVERFLOW2
16d0b 20 26 26 20 70 67 6e 6f 3d 3d 6f 76 66 6c 20 29 && pgno==ovfl )
16d0c 7b 0a 20 20 20 20 20 20 20 20 6e 65 78 74 20 3d {. next =
16d0d 20 69 47 75 65 73 73 3b 0a 20 20 20 20 20 20 20 iGuess;.
16d0e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e rc = SQLITE_DON
16d0f 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d E;. }. }
16d10 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 61 . }.#endif.. a
16d11 73 73 65 72 74 28 20 6e 65 78 74 3d 3d 30 20 7c ssert( next==0 |
16d12 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e | rc==SQLITE_DON
16d13 45 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 E );. if( rc==S
16d14 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
16d15 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 rc = btreeGetPag
16d16 65 28 70 42 74 2c 20 6f 76 66 6c 2c 20 26 70 50 e(pBt, ovfl, &pP
16d17 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 age, 0);. ass
16d18 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
16d19 4f 4b 20 7c 7c 20 70 50 61 67 65 3d 3d 30 20 29 OK || pPage==0 )
16d1a 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
16d1b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
16d1c 20 6e 65 78 74 20 3d 20 67 65 74 34 62 79 74 65 next = get4byte
16d1d 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29 3b 0a (pPage->aData);.
16d1e 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 50 }. }.. *pP
16d1f 67 6e 6f 4e 65 78 74 20 3d 20 6e 65 78 74 3b 0a gnoNext = next;.
16d20 20 20 69 66 28 20 70 70 50 61 67 65 20 29 7b 0a if( ppPage ){.
16d21 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50 *ppPage = pP
16d22 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 age;. }else{.
16d23 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
16d24 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 age);. }. retu
16d25 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44 rn (rc==SQLITE_D
16d26 4f 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 ONE ? SQLITE_OK
16d27 3a 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 : rc);.}../*.**
16d28 43 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 61 Copy data from a
16d29 20 62 75 66 66 65 72 20 74 6f 20 61 20 70 61 67 buffer to a pag
16d2a 65 2c 20 6f 72 20 66 72 6f 6d 20 61 20 70 61 67 e, or from a pag
16d2b 65 20 74 6f 20 61 20 62 75 66 66 65 72 2e 0a 2a e to a buffer..*
16d2c 2a 0a 2a 2a 20 70 50 61 79 6c 6f 61 64 20 69 73 *.** pPayload is
16d2d 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 64 61 a pointer to da
16d2e 74 61 20 73 74 6f 72 65 64 20 6f 6e 20 64 61 74 ta stored on dat
16d2f 61 62 61 73 65 20 70 61 67 65 20 70 44 62 50 61 abase page pDbPa
16d30 67 65 2e 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 ge..** If argume
16d31 6e 74 20 65 4f 70 20 69 73 20 66 61 6c 73 65 2c nt eOp is false,
16d32 20 74 68 65 6e 20 6e 42 79 74 65 20 62 79 74 65 then nByte byte
16d33 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f s of data are co
16d34 70 69 65 64 0a 2a 2a 20 66 72 6f 6d 20 70 50 61 pied.** from pPa
16d35 79 6c 6f 61 64 20 74 6f 20 74 68 65 20 62 75 66 yload to the buf
16d36 66 65 72 20 70 6f 69 6e 74 65 64 20 61 74 20 62 fer pointed at b
16d37 79 20 70 42 75 66 2e 20 49 66 20 65 4f 70 20 69 y pBuf. If eOp i
16d38 73 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 s true,.** then
16d39 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
16d3a 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e e() is called on
16d3b 20 70 44 62 50 61 67 65 20 61 6e 64 20 6e 42 79 pDbPage and nBy
16d3c 74 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 64 te bytes.** of d
16d3d 61 74 61 20 61 72 65 20 63 6f 70 69 65 64 20 66 ata are copied f
16d3e 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 70 rom the buffer p
16d3f 42 75 66 20 74 6f 20 70 50 61 79 6c 6f 61 64 2e Buf to pPayload.
16d40 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
16d41 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 is returned on
16d42 73 75 63 63 65 73 73 2c 20 6f 74 68 65 72 77 69 success, otherwi
16d43 73 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 se an error code
16d44 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
16d45 63 6f 70 79 50 61 79 6c 6f 61 64 28 0a 20 20 76 copyPayload(. v
16d46 6f 69 64 20 2a 70 50 61 79 6c 6f 61 64 2c 20 20 oid *pPayload,
16d47 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
16d48 74 65 72 20 74 6f 20 70 61 67 65 20 64 61 74 61 ter to page data
16d49 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 */. void *pBuf
16d4a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
16d4b 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 /* Pointer to bu
16d4c 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 ffer */. int nB
16d4d 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 yte,
16d4e 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
16d4f 20 62 79 74 65 73 20 74 6f 20 63 6f 70 79 20 2a bytes to copy *
16d50 2f 0a 20 20 69 6e 74 20 65 4f 70 2c 20 20 20 20 /. int eOp,
16d51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16d52 20 30 20 2d 3e 20 63 6f 70 79 20 66 72 6f 6d 20 0 -> copy from
16d53 70 61 67 65 2c 20 31 20 2d 3e 20 63 6f 70 79 20 page, 1 -> copy
16d54 74 6f 20 70 61 67 65 20 2a 2f 0a 20 20 44 62 50 to page */. DbP
16d55 61 67 65 20 2a 70 44 62 50 61 67 65 20 20 20 20 age *pDbPage
16d56 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 /* Page c
16d57 6f 6e 74 61 69 6e 69 6e 67 20 70 50 61 79 6c 6f ontaining pPaylo
16d58 61 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 65 ad */.){. if( e
16d59 4f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 Op ){. /* Cop
16d5a 79 20 64 61 74 61 20 66 72 6f 6d 20 62 75 66 66 y data from buff
16d5b 65 72 20 74 6f 20 70 61 67 65 20 28 61 20 77 72 er to page (a wr
16d5c 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a ite operation) *
16d5d 2f 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 /. int rc = s
16d5e 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
16d5f 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 (pDbPage);. i
16d60 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
16d61 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
16d62 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d rc;. }. m
16d63 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c 20 emcpy(pPayload,
16d64 70 42 75 66 2c 20 6e 42 79 74 65 29 3b 0a 20 20 pBuf, nByte);.
16d65 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f }else{. /* Co
16d66 70 79 20 64 61 74 61 20 66 72 6f 6d 20 70 61 67 py data from pag
16d67 65 20 74 6f 20 62 75 66 66 65 72 20 28 61 20 72 e to buffer (a r
16d68 65 61 64 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a ead operation) *
16d69 2f 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 42 75 /. memcpy(pBu
16d6a 66 2c 20 70 50 61 79 6c 6f 61 64 2c 20 6e 42 79 f, pPayload, nBy
16d6b 74 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 te);. }. retur
16d6c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
16d6d 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
16d6e 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 ion is used to r
16d6f 65 61 64 20 6f 72 20 6f 76 65 72 77 72 69 74 65 ead or overwrite
16d70 20 70 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 payload informa
16d71 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20 tion.** for the
16d72 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 70 entry that the p
16d73 43 75 72 20 63 75 72 73 6f 72 20 69 73 20 70 6f Cur cursor is po
16d74 69 6e 74 69 6e 67 20 74 6f 2e 20 49 66 20 74 68 inting to. If th
16d75 65 20 65 4f 70 0a 2a 2a 20 70 61 72 61 6d 65 74 e eOp.** paramet
16d76 65 72 20 69 73 20 30 2c 20 74 68 69 73 20 69 73 er is 0, this is
16d77 20 61 20 72 65 61 64 20 6f 70 65 72 61 74 69 6f a read operatio
16d78 6e 20 28 64 61 74 61 20 63 6f 70 69 65 64 20 69 n (data copied i
16d79 6e 74 6f 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 nto.** buffer pB
16d7a 75 66 29 2e 20 49 66 20 69 74 20 69 73 20 6e 6f uf). If it is no
16d7b 6e 2d 7a 65 72 6f 2c 20 61 20 77 72 69 74 65 20 n-zero, a write
16d7c 28 64 61 74 61 20 63 6f 70 69 65 64 20 66 72 6f (data copied fro
16d7d 6d 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 75 66 m.** buffer pBuf
16d7e 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 6f 74 61 6c )..**.** A total
16d7f 20 6f 66 20 22 61 6d 74 22 20 62 79 74 65 73 20 of "amt" bytes
16d80 61 72 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 are read or writ
16d81 74 65 6e 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 ten beginning at
16d82 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 20 44 61 "offset"..** Da
16d83 74 61 20 69 73 20 72 65 61 64 20 74 6f 20 6f 72 ta is read to or
16d84 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 from the buffer
16d85 20 70 42 75 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 pBuf..**.** The
16d86 20 63 6f 6e 74 65 6e 74 20 62 65 69 6e 67 20 72 content being r
16d87 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 20 6d ead or written m
16d88 69 67 68 74 20 61 70 70 65 61 72 20 6f 6e 20 74 ight appear on t
16d89 68 65 20 6d 61 69 6e 20 70 61 67 65 0a 2a 2a 20 he main page.**
16d8a 6f 72 20 62 65 20 73 63 61 74 74 65 72 65 64 20 or be scattered
16d8b 6f 75 74 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 20 out on multiple
16d8c 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 0a overflow pages..
16d8d 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 42 74 43 **.** If the BtC
16d8e 75 72 73 6f 72 2e 69 73 49 6e 63 72 62 6c 6f 62 ursor.isIncrblob
16d8f 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 20 73 Handle flag is s
16d90 65 74 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 et, and the curr
16d91 65 6e 74 0a 2a 2a 20 63 75 72 73 6f 72 20 65 6e ent.** cursor en
16d92 74 72 79 20 75 73 65 73 20 6f 6e 65 20 6f 72 20 try uses one or
16d93 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 more overflow pa
16d94 67 65 73 2c 20 74 68 69 73 20 66 75 6e 63 74 69 ges, this functi
16d95 6f 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 73 20 on.** allocates
16d96 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 6c 61 space for and la
16d97 7a 69 6c 79 20 70 6f 70 6c 75 61 74 65 73 20 74 zily popluates t
16d98 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 he overflow page
16d99 2d 6c 69 73 74 20 0a 2a 2a 20 63 61 63 68 65 20 -list .** cache
16d9a 61 72 72 61 79 20 28 42 74 43 75 72 73 6f 72 2e array (BtCursor.
16d9b 61 4f 76 65 72 66 6c 6f 77 29 2e 20 53 75 62 73 aOverflow). Subs
16d9c 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 75 73 65 equent calls use
16d9d 20 74 68 69 73 0a 2a 2a 20 63 61 63 68 65 20 74 this.** cache t
16d9e 6f 20 6d 61 6b 65 20 73 65 65 6b 69 6e 67 20 74 o make seeking t
16d9f 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 6f o the supplied o
16da0 66 66 73 65 74 20 6d 6f 72 65 20 65 66 66 69 63 ffset more effic
16da1 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 ient..**.** Once
16da2 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
16da3 65 2d 6c 69 73 74 20 63 61 63 68 65 20 68 61 73 e-list cache has
16da4 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c been allocated,
16da5 20 69 74 20 6d 61 79 20 62 65 0a 2a 2a 20 69 6e it may be.** in
16da6 76 61 6c 69 64 61 74 65 64 20 69 66 20 73 6f 6d validated if som
16da7 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72 20 77 e other cursor w
16da8 72 69 74 65 73 20 74 6f 20 74 68 65 20 73 61 6d rites to the sam
16da9 65 20 74 61 62 6c 65 2c 20 6f 72 20 69 66 0a 2a e table, or if.*
16daa 2a 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 * the cursor is
16dab 6d 6f 76 65 64 20 74 6f 20 61 20 64 69 66 66 65 moved to a diffe
16dac 72 65 6e 74 20 72 6f 77 2e 20 41 64 64 69 74 69 rent row. Additi
16dad 6f 6e 61 6c 6c 79 2c 20 69 6e 20 61 75 74 6f 2d onally, in auto-
16dae 76 61 63 75 75 6d 0a 2a 2a 20 6d 6f 64 65 2c 20 vacuum.** mode,
16daf 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 76 the following ev
16db0 65 6e 74 73 20 6d 61 79 20 69 6e 76 61 6c 69 64 ents may invalid
16db1 61 74 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 ate an overflow
16db2 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2e page-list cache.
16db3 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6e .**.** * An in
16db4 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d cremental vacuum
16db5 2c 0a 2a 2a 20 20 20 2a 20 41 20 63 6f 6d 6d 69 ,.** * A commi
16db6 74 20 69 6e 20 61 75 74 6f 5f 76 61 63 75 75 6d t in auto_vacuum
16db7 3d 22 66 75 6c 6c 22 20 6d 6f 64 65 2c 0a 2a 2a ="full" mode,.**
16db8 20 20 20 2a 20 43 72 65 61 74 69 6e 67 20 61 20 * Creating a
16db9 74 61 62 6c 65 20 28 6d 61 79 20 72 65 71 75 69 table (may requi
16dba 72 65 20 6d 6f 76 69 6e 67 20 61 6e 20 6f 76 65 re moving an ove
16dbb 72 66 6c 6f 77 20 70 61 67 65 29 2e 0a 2a 2f 0a rflow page)..*/.
16dbc 73 74 61 74 69 63 20 69 6e 74 20 61 63 63 65 73 static int acces
16dbd 73 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 sPayload(. BtCu
16dbe 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 rsor *pCur,
16dbf 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 /* Cursor point
16dc0 69 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 ing to entry to
16dc1 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 read from */. u
16dc2 33 32 20 6f 66 66 73 65 74 2c 20 20 20 20 20 20 32 offset,
16dc3 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 /* Begin rea
16dc4 64 69 6e 67 20 74 68 69 73 20 66 61 72 20 69 6e ding this far in
16dc5 74 6f 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 to payload */.
16dc6 75 33 32 20 61 6d 74 2c 20 20 20 20 20 20 20 20 u32 amt,
16dc7 20 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 69 /* Read thi
16dc8 73 20 6d 61 6e 79 20 62 79 74 65 73 20 2a 2f 0a s many bytes */.
16dc9 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
16dca 2a 70 42 75 66 2c 20 2f 2a 20 57 72 69 74 65 20 *pBuf, /* Write
16dcb 74 68 65 20 62 79 74 65 73 20 69 6e 74 6f 20 74 the bytes into t
16dcc 68 69 73 20 62 75 66 66 65 72 20 2a 2f 20 0a 20 his buffer */ .
16dcd 20 69 6e 74 20 65 4f 70 20 20 20 20 20 20 20 20 int eOp
16dce 20 20 20 20 20 20 2f 2a 20 7a 65 72 6f 20 74 6f /* zero to
16dcf 20 72 65 61 64 2e 20 6e 6f 6e 2d 7a 65 72 6f 20 read. non-zero
16dd0 74 6f 20 77 72 69 74 65 2e 20 2a 2f 0a 29 7b 0a to write. */.){.
16dd1 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
16dd2 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 20 69 6e 74 *aPayload;. int
16dd3 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
16dd4 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 20 69 . u32 nKey;. i
16dd5 6e 74 20 69 49 64 78 20 3d 20 30 3b 0a 20 20 4d nt iIdx = 0;. M
16dd6 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 emPage *pPage =
16dd7 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
16dd8 72 2d 3e 69 50 61 67 65 5d 3b 20 2f 2a 20 42 74 r->iPage]; /* Bt
16dd9 72 65 65 20 70 61 67 65 20 6f 66 20 63 75 72 72 ree page of curr
16dda 65 6e 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 42 ent entry */. B
16ddb 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
16ddc 43 75 72 2d 3e 70 42 74 3b 20 20 20 20 20 20 20 Cur->pBt;
16ddd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 74 /* Bt
16dde 72 65 65 20 74 68 69 73 20 63 75 72 73 6f 72 20 ree this cursor
16ddf 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a 0a 20 belongs to */..
16de0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 20 29 assert( pPage )
16de1 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
16de2 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
16de3 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 _VALID );. asse
16de4 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b rt( pCur->aiIdx[
16de5 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61 pCur->iPage]<pPa
16de6 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 ge->nCell );. a
16de7 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
16de8 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
16de9 0a 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 .. getCellInfo(
16dea 70 43 75 72 29 3b 0a 20 20 61 50 61 79 6c 6f 61 pCur);. aPayloa
16deb 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 d = pCur->info.p
16dec 43 65 6c 6c 20 2b 20 70 43 75 72 2d 3e 69 6e 66 Cell + pCur->inf
16ded 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 6e 4b 65 o.nHeader;. nKe
16dee 79 20 3d 20 28 70 50 61 67 65 2d 3e 69 6e 74 4b y = (pPage->intK
16def 65 79 20 3f 20 30 20 3a 20 28 69 6e 74 29 70 43 ey ? 0 : (int)pC
16df0 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29 3b 0a ur->info.nKey);.
16df1 0a 20 20 69 66 28 20 4e 45 56 45 52 28 6f 66 66 . if( NEVER(off
16df2 73 65 74 2b 61 6d 74 20 3e 20 6e 4b 65 79 2b 70 set+amt > nKey+p
16df3 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74 61 29 Cur->info.nData)
16df4 20 0a 20 20 20 7c 7c 20 26 61 50 61 79 6c 6f 61 . || &aPayloa
16df5 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f d[pCur->info.nLo
16df6 63 61 6c 5d 20 3e 20 26 70 50 61 67 65 2d 3e 61 cal] > &pPage->a
16df7 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c 65 Data[pBt->usable
16df8 53 69 7a 65 5d 0a 20 20 29 7b 0a 20 20 20 20 2f Size]. ){. /
16df9 2a 20 54 72 79 69 6e 67 20 74 6f 20 72 65 61 64 * Trying to read
16dfa 20 6f 72 20 77 72 69 74 65 20 70 61 73 74 20 74 or write past t
16dfb 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 64 61 he end of the da
16dfc 74 61 20 69 73 20 61 6e 20 65 72 72 6f 72 20 2a ta is an error *
16dfd 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c /. return SQL
16dfe 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
16dff 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 ;. }.. /* Chec
16e00 6b 20 69 66 20 64 61 74 61 20 6d 75 73 74 20 62 k if data must b
16e01 65 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20 74 e read/written t
16e02 6f 2f 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 o/from the btree
16e03 20 70 61 67 65 20 69 74 73 65 6c 66 2e 20 2a 2f page itself. */
16e04 0a 20 20 69 66 28 20 6f 66 66 73 65 74 3c 70 43 . if( offset<pC
16e05 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 ur->info.nLocal
16e06 29 7b 0a 20 20 20 20 69 6e 74 20 61 20 3d 20 61 ){. int a = a
16e07 6d 74 3b 0a 20 20 20 20 69 66 28 20 61 2b 6f 66 mt;. if( a+of
16e08 66 73 65 74 3e 70 43 75 72 2d 3e 69 6e 66 6f 2e fset>pCur->info.
16e09 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 nLocal ){.
16e0a 61 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e a = pCur->info.n
16e0b 4c 6f 63 61 6c 20 2d 20 6f 66 66 73 65 74 3b 0a Local - offset;.
16e0c 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 63 }. rc = c
16e0d 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79 opyPayload(&aPay
16e0e 6c 6f 61 64 5b 6f 66 66 73 65 74 5d 2c 20 70 42 load[offset], pB
16e0f 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 50 61 67 uf, a, eOp, pPag
16e10 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 e->pDbPage);.
16e11 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 offset = 0;.
16e12 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 20 pBuf += a;.
16e13 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 7d 65 6c 73 amt -= a;. }els
16e14 65 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 2d 3d e{. offset -=
16e15 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 pCur->info.nLoc
16e16 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 al;. }.. if( r
16e17 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
16e18 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 63 6f 6e amt>0 ){. con
16e19 73 74 20 75 33 32 20 6f 76 66 6c 53 69 7a 65 20 st u32 ovflSize
16e1a 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
16e1b 65 20 2d 20 34 3b 20 20 2f 2a 20 42 79 74 65 73 e - 4; /* Bytes
16e1c 20 63 6f 6e 74 65 6e 74 20 70 65 72 20 6f 76 66 content per ovf
16e1d 6c 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 50 67 l page */. Pg
16e1e 6e 6f 20 6e 65 78 74 50 61 67 65 3b 0a 0a 20 20 no nextPage;..
16e1f 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67 65 74 nextPage = get
16e20 34 62 79 74 65 28 26 61 50 61 79 6c 6f 61 64 5b 4byte(&aPayload[
16e21 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 pCur->info.nLoca
16e22 6c 5d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 l]);..#ifndef SQ
16e23 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
16e24 4f 42 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 OB. /* If the
16e25 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c isIncrblobHandl
16e26 65 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e e flag is set an
16e27 64 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 61 d the BtCursor.a
16e28 4f 76 65 72 66 6c 6f 77 5b 5d 0a 20 20 20 20 2a Overflow[]. *
16e29 2a 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 61 * has not been a
16e2a 6c 6c 6f 63 61 74 65 64 2c 20 61 6c 6c 6f 63 61 llocated, alloca
16e2b 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 61 te it now. The a
16e2c 72 72 61 79 20 69 73 20 73 69 7a 65 64 20 61 74 rray is sized at
16e2d 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6e 74 72 . ** one entr
16e2e 79 20 66 6f 72 20 65 61 63 68 20 6f 76 65 72 66 y for each overf
16e2f 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 20 low page in the
16e30 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 overflow chain.
16e31 54 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 20 The. ** page
16e32 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 66 69 number of the fi
16e33 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 rst overflow pag
16e34 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 e is stored in a
16e35 4f 76 65 72 66 6c 6f 77 5b 30 5d 2c 0a 20 20 20 Overflow[0],.
16e36 20 2a 2a 20 65 74 63 2e 20 41 20 76 61 6c 75 65 ** etc. A value
16e37 20 6f 66 20 30 20 69 6e 20 74 68 65 20 61 4f 76 of 0 in the aOv
16e38 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 79 20 6d erflow[] array m
16e39 65 61 6e 73 20 22 6e 6f 74 20 79 65 74 20 6b 6e eans "not yet kn
16e3a 6f 77 6e 22 0a 20 20 20 20 2a 2a 20 28 74 68 65 own". ** (the
16e3b 20 63 61 63 68 65 20 69 73 20 6c 61 7a 69 6c 79 cache is lazily
16e3c 20 70 6f 70 75 6c 61 74 65 64 29 2e 0a 20 20 20 populated)..
16e3d 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 75 72 */. if( pCur
16e3e 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 ->isIncrblobHand
16e3f 6c 65 20 26 26 20 21 70 43 75 72 2d 3e 61 4f 76 le && !pCur->aOv
16e40 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 erflow ){.
16e41 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 28 70 43 75 int nOvfl = (pCu
16e42 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 r->info.nPayload
16e43 2d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 -pCur->info.nLoc
16e44 61 6c 2b 6f 76 66 6c 53 69 7a 65 2d 31 29 2f 6f al+ovflSize-1)/o
16e45 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20 20 70 vflSize;. p
16e46 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d Cur->aOverflow =
16e47 20 28 50 67 6e 6f 20 2a 29 73 71 6c 69 74 65 33 (Pgno *)sqlite3
16e48 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f MallocZero(sizeo
16e49 66 28 50 67 6e 6f 29 2a 6e 4f 76 66 6c 29 3b 0a f(Pgno)*nOvfl);.
16e4a 20 20 20 20 20 20 2f 2a 20 6e 4f 76 66 6c 20 69 /* nOvfl i
16e4b 73 20 61 6c 77 61 79 73 20 70 6f 73 69 74 69 76 s always positiv
16e4c 65 2e 20 20 49 66 20 69 74 20 77 65 72 65 20 7a e. If it were z
16e4d 65 72 6f 2c 20 66 65 74 63 68 50 61 79 6c 6f 61 ero, fetchPayloa
16e4e 64 20 77 6f 75 6c 64 20 68 61 76 65 0a 20 20 20 d would have.
16e4f 20 20 20 2a 2a 20 62 65 65 6e 20 75 73 65 64 20 ** been used
16e50 69 6e 73 74 65 61 64 20 6f 66 20 74 68 69 73 20 instead of this
16e51 72 6f 75 74 69 6e 65 2e 20 2a 2f 0a 20 20 20 20 routine. */.
16e52 20 20 69 66 28 20 41 4c 57 41 59 53 28 6e 4f 76 if( ALWAYS(nOv
16e53 66 6c 29 20 26 26 20 21 70 43 75 72 2d 3e 61 4f fl) && !pCur->aO
16e54 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 verflow ){.
16e55 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
16e56 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 OMEM;. }.
16e57 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 }.. /* If t
16e58 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 he overflow page
16e59 2d 6c 69 73 74 20 63 61 63 68 65 20 68 61 73 20 -list cache has
16e5a 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 been allocated a
16e5b 6e 64 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e nd the. ** en
16e5c 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 72 73 try for the firs
16e5d 74 20 72 65 71 75 69 72 65 64 20 6f 76 65 72 66 t required overf
16e5e 6c 6f 77 20 70 61 67 65 20 69 73 20 76 61 6c 69 low page is vali
16e5f 64 2c 20 73 6b 69 70 0a 20 20 20 20 2a 2a 20 64 d, skip. ** d
16e60 69 72 65 63 74 6c 79 20 74 6f 20 69 74 2e 0a 20 irectly to it..
16e61 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 */. if( pC
16e62 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26 26 ur->aOverflow &&
16e63 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 pCur->aOverflow
16e64 5b 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65 [offset/ovflSize
16e65 5d 20 29 7b 0a 20 20 20 20 20 20 69 49 64 78 20 ] ){. iIdx
16e66 3d 20 28 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69 = (offset/ovflSi
16e67 7a 65 29 3b 0a 20 20 20 20 20 20 6e 65 78 74 50 ze);. nextP
16e68 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 age = pCur->aOve
16e69 72 66 6c 6f 77 5b 69 49 64 78 5d 3b 0a 20 20 20 rflow[iIdx];.
16e6a 20 20 20 6f 66 66 73 65 74 20 3d 20 28 6f 66 66 offset = (off
16e6b 73 65 74 25 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 set%ovflSize);.
16e6c 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 }.#endif..
16e6d 20 66 6f 72 28 20 3b 20 72 63 3d 3d 53 51 4c 49 for( ; rc==SQLI
16e6e 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 26 TE_OK && amt>0 &
16e6f 26 20 6e 65 78 74 50 61 67 65 3b 20 69 49 64 78 & nextPage; iIdx
16e70 2b 2b 29 7b 0a 0a 23 69 66 6e 64 65 66 20 53 51 ++){..#ifndef SQ
16e71 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
16e72 4f 42 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 72 OB. /* If r
16e73 65 71 75 69 72 65 64 2c 20 70 6f 70 75 6c 61 74 equired, populat
16e74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 e the overflow p
16e75 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2e 20 age-list cache.
16e76 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 */. if( pCu
16e77 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a r->aOverflow ){.
16e78 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 21 assert(!
16e79 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b pCur->aOverflow[
16e7a 69 49 64 78 5d 20 7c 7c 20 70 43 75 72 2d 3e 61 iIdx] || pCur->a
16e7b 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 3d 3d Overflow[iIdx]==
16e7c 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20 20 nextPage);.
16e7d 20 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c pCur->aOverfl
16e7e 6f 77 5b 69 49 64 78 5d 20 3d 20 6e 65 78 74 50 ow[iIdx] = nextP
16e7f 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e age;. }.#en
16e80 64 69 66 0a 0a 20 20 20 20 20 20 69 66 28 20 6f dif.. if( o
16e81 66 66 73 65 74 3e 3d 6f 76 66 6c 53 69 7a 65 20 ffset>=ovflSize
16e82 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ){. /* Th
16e83 65 20 6f 6e 6c 79 20 72 65 61 73 6f 6e 20 74 6f e only reason to
16e84 20 72 65 61 64 20 74 68 69 73 20 70 61 67 65 20 read this page
16e85 69 73 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 is to obtain the
16e86 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a page. **
16e87 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 number for the
16e88 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 next page in the
16e89 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e overflow chain.
16e8a 20 54 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 The page.
16e8b 20 20 2a 2a 20 64 61 74 61 20 69 73 20 6e 6f 74 ** data is not
16e8c 20 72 65 71 75 69 72 65 64 2e 20 53 6f 20 66 69 required. So fi
16e8d 72 73 74 20 74 72 79 20 74 6f 20 6c 6f 6f 6b 75 rst try to looku
16e8e 70 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 0a 20 p the overflow.
16e8f 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 2d 6c ** page-l
16e90 69 73 74 20 63 61 63 68 65 2c 20 69 66 20 61 6e ist cache, if an
16e91 79 2c 20 74 68 65 6e 20 66 61 6c 6c 20 62 61 63 y, then fall bac
16e92 6b 20 74 6f 20 74 68 65 20 67 65 74 4f 76 65 72 k to the getOver
16e93 66 6c 6f 77 50 61 67 65 28 29 0a 20 20 20 20 20 flowPage().
16e94 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a ** function..
16e95 20 20 20 20 20 20 20 20 2a 2f 0a 23 69 66 6e 64 */.#ifnd
16e96 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
16e97 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 20 20 20 NCRBLOB.
16e98 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 if( pCur->aOverf
16e99 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e 61 4f 76 low && pCur->aOv
16e9a 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 20 29 erflow[iIdx+1] )
16e9b 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 {. next
16e9c 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 Page = pCur->aOv
16e9d 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a erflow[iIdx+1];.
16e9e 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20 0a } else .
16e9f 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 #endif.
16ea0 20 72 63 20 3d 20 67 65 74 4f 76 65 72 66 6c 6f rc = getOverflo
16ea1 77 50 61 67 65 28 70 42 74 2c 20 6e 65 78 74 50 wPage(pBt, nextP
16ea2 61 67 65 2c 20 30 2c 20 26 6e 65 78 74 50 61 67 age, 0, &nextPag
16ea3 65 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 73 e);. offs
16ea4 65 74 20 2d 3d 20 6f 76 66 6c 53 69 7a 65 3b 0a et -= ovflSize;.
16ea5 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
16ea6 20 20 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 /* Need to
16ea7 72 65 61 64 20 74 68 69 73 20 70 61 67 65 20 70 read this page p
16ea8 72 6f 70 65 72 6c 79 2e 20 49 74 20 63 6f 6e 74 roperly. It cont
16ea9 61 69 6e 73 20 73 6f 6d 65 20 6f 66 20 74 68 65 ains some of the
16eaa 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 61 6e 67 . ** rang
16eab 65 20 6f 66 20 64 61 74 61 20 74 68 61 74 20 69 e of data that i
16eac 73 20 62 65 69 6e 67 20 72 65 61 64 20 28 65 4f s being read (eO
16ead 70 3d 3d 30 29 20 6f 72 20 77 72 69 74 74 65 6e p==0) or written
16eae 20 28 65 4f 70 21 3d 30 29 2e 0a 20 20 20 20 20 (eOp!=0)..
16eaf 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 44 62 */. Db
16eb0 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 20 Page *pDbPage;.
16eb1 20 20 20 20 20 20 20 69 6e 74 20 61 20 3d 20 61 int a = a
16eb2 6d 74 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d mt;. rc =
16eb3 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
16eb4 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e 65 (pBt->pPager, ne
16eb5 78 74 50 61 67 65 2c 20 26 70 44 62 50 61 67 65 xtPage, &pDbPage
16eb6 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
16eb7 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
16eb8 20 20 20 20 20 20 20 20 20 20 61 50 61 79 6c 6f aPaylo
16eb9 61 64 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 ad = sqlite3Page
16eba 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 rGetData(pDbPage
16ebb 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78 );. nex
16ebc 74 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 tPage = get4byte
16ebd 28 61 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 (aPayload);.
16ebe 20 20 20 20 20 20 69 66 28 20 61 20 2b 20 6f 66 if( a + of
16ebf 66 73 65 74 20 3e 20 6f 76 66 6c 53 69 7a 65 20 fset > ovflSize
16ec0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 ){. a
16ec1 20 3d 20 6f 76 66 6c 53 69 7a 65 20 2d 20 6f 66 = ovflSize - of
16ec2 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 20 20 fset;.
16ec3 7d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d }. rc =
16ec4 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50 copyPayload(&aP
16ec5 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74 2b 34 5d ayload[offset+4]
16ec6 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20 , pBuf, a, eOp,
16ec7 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
16ec8 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
16ec9 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a Unref(pDbPage);.
16eca 20 20 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 offset
16ecb 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
16ecc 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 20 20 20 20 amt -= a;.
16ecd 20 20 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 pBuf += a;.
16ece 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
16ecf 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 . }. }.. if
16ed0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
16ed1 26 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 && amt>0 ){.
16ed2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
16ed3 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a RRUPT_BKPT;. }.
16ed4 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
16ed5 2f 2a 0a 2a 2a 20 52 65 61 64 20 70 61 72 74 20 /*.** Read part
16ed6 6f 66 20 74 68 65 20 6b 65 79 20 61 73 73 6f 63 of the key assoc
16ed7 69 61 74 65 64 20 77 69 74 68 20 63 75 72 73 6f iated with curso
16ed8 72 20 70 43 75 72 2e 20 20 45 78 61 63 74 6c 79 r pCur. Exactly
16ed9 0a 2a 2a 20 22 61 6d 74 22 20 62 79 74 65 73 20 .** "amt" bytes
16eda 77 69 6c 6c 20 62 65 20 74 72 61 6e 73 66 65 72 will be transfer
16edb 65 64 20 69 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 ed into pBuf[].
16edc 20 54 68 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a The transfer.**
16edd 20 62 65 67 69 6e 73 20 61 74 20 22 6f 66 66 73 begins at "offs
16ede 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 et"..**.** The c
16edf 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72 aller must ensur
16ee0 65 20 74 68 61 74 20 70 43 75 72 20 69 73 20 70 e that pCur is p
16ee1 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 76 61 6c ointing to a val
16ee2 69 64 20 72 6f 77 0a 2a 2a 20 69 6e 20 74 68 65 id row.** in the
16ee3 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 table..**.** Re
16ee4 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f turn SQLITE_OK o
16ee5 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 n success or an
16ee6 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e error code if an
16ee7 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 ything goes.** w
16ee8 72 6f 6e 67 2e 20 20 41 6e 20 65 72 72 6f 72 20 rong. An error
16ee9 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 22 is returned if "
16eea 6f 66 66 73 65 74 2b 61 6d 74 22 20 69 73 20 6c offset+amt" is l
16eeb 61 72 67 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 arger than.** th
16eec 65 20 61 76 61 69 6c 61 62 6c 65 20 70 61 79 6c e available payl
16eed 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 oad..*/.SQLITE_P
16eee 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
16eef 65 33 42 74 72 65 65 4b 65 79 28 42 74 43 75 72 e3BtreeKey(BtCur
16ef0 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 6f sor *pCur, u32 o
16ef1 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 ffset, u32 amt,
16ef2 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 20 61 void *pBuf){. a
16ef3 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
16ef4 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
16ef5 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
16ef6 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
16ef7 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 VALID );. asser
16ef8 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d t( pCur->iPage>=
16ef9 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67 0 && pCur->apPag
16efa 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 e[pCur->iPage] )
16efb 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
16efc 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
16efd 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67 age]<pCur->apPag
16efe 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
16eff 6e 43 65 6c 6c 20 29 3b 0a 20 20 72 65 74 75 72 nCell );. retur
16f00 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 n accessPayload(
16f01 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d pCur, offset, am
16f02 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 t, (unsigned cha
16f03 72 2a 29 70 42 75 66 2c 20 30 29 3b 0a 7d 0a 0a r*)pBuf, 0);.}..
16f04 2f 2a 0a 2a 2a 20 52 65 61 64 20 70 61 72 74 20 /*.** Read part
16f05 6f 66 20 74 68 65 20 64 61 74 61 20 61 73 73 6f of the data asso
16f06 63 69 61 74 65 64 20 77 69 74 68 20 63 75 72 73 ciated with curs
16f07 6f 72 20 70 43 75 72 2e 20 20 45 78 61 63 74 6c or pCur. Exactl
16f08 79 0a 2a 2a 20 22 61 6d 74 22 20 62 79 74 65 73 y.** "amt" bytes
16f09 20 77 69 6c 6c 20 62 65 20 74 72 61 6e 73 66 65 will be transfe
16f0a 72 65 64 20 69 6e 74 6f 20 70 42 75 66 5b 5d 2e red into pBuf[].
16f0b 20 20 54 68 65 20 74 72 61 6e 73 66 65 72 0a 2a The transfer.*
16f0c 2a 20 62 65 67 69 6e 73 20 61 74 20 22 6f 66 66 * begins at "off
16f0d 73 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 set"..**.** Retu
16f0e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 rn SQLITE_OK on
16f0f 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 success or an er
16f10 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 79 74 ror code if anyt
16f11 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f hing goes.** wro
16f12 6e 67 2e 20 20 41 6e 20 65 72 72 6f 72 20 69 73 ng. An error is
16f13 20 72 65 74 75 72 6e 65 64 20 69 66 20 22 6f 66 returned if "of
16f14 66 73 65 74 2b 61 6d 74 22 20 69 73 20 6c 61 72 fset+amt" is lar
16f15 67 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 ger than.** the
16f16 61 76 61 69 6c 61 62 6c 65 20 70 61 79 6c 6f 61 available payloa
16f17 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
16f18 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
16f19 42 74 72 65 65 44 61 74 61 28 42 74 43 75 72 73 BtreeData(BtCurs
16f1a 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 6f 66 or *pCur, u32 of
16f1b 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 fset, u32 amt, v
16f1c 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 20 69 6e oid *pBuf){. in
16f1d 74 20 72 63 3b 0a 0a 23 69 66 6e 64 65 66 20 53 t rc;..#ifndef S
16f1e 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
16f1f 4c 4f 42 0a 20 20 69 66 20 28 20 70 43 75 72 2d LOB. if ( pCur-
16f20 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
16f21 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 INVALID ){. r
16f22 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f eturn SQLITE_ABO
16f23 52 54 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a RT;. }.#endif..
16f24 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
16f25 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
16f26 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f );. rc = resto
16f27 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
16f28 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 (pCur);. if( rc
16f29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
16f2a 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
16f2b 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
16f2c 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 61 73 73 VALID );. ass
16f2d 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 ert( pCur->iPage
16f2e 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 >=0 && pCur->apP
16f2f 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
16f30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
16f31 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
16f32 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61 ->iPage]<pCur->a
16f33 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
16f34 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 e]->nCell );.
16f35 20 72 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c rc = accessPayl
16f36 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 73 65 74 oad(pCur, offset
16f37 2c 20 61 6d 74 2c 20 70 42 75 66 2c 20 30 29 3b , amt, pBuf, 0);
16f38 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
16f39 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
16f3a 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 n a pointer to p
16f3b 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74 69 ayload informati
16f3c 6f 6e 20 66 72 6f 6d 20 74 68 65 20 65 6e 74 72 on from the entr
16f3d 79 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 70 y that the .** p
16f3e 43 75 72 20 63 75 72 73 6f 72 20 69 73 20 70 6f Cur cursor is po
16f3f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 inting to. The
16f40 70 6f 69 6e 74 65 72 20 69 73 20 74 6f 20 74 68 pointer is to th
16f41 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a e beginning of.*
16f42 2a 20 74 68 65 20 6b 65 79 20 69 66 20 73 6b 69 * the key if ski
16f43 70 4b 65 79 3d 3d 30 20 61 6e 64 20 69 74 20 70 pKey==0 and it p
16f44 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 62 65 67 oints to the beg
16f45 69 6e 6e 69 6e 67 20 6f 66 20 64 61 74 61 20 69 inning of data i
16f46 66 0a 2a 2a 20 73 6b 69 70 4b 65 79 3d 3d 31 2e f.** skipKey==1.
16f47 20 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 The number of
16f48 62 79 74 65 73 20 6f 66 20 61 76 61 69 6c 61 62 bytes of availab
16f49 6c 65 20 6b 65 79 2f 64 61 74 61 20 69 73 20 77 le key/data is w
16f4a 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 2a ritten.** into *
16f4b 70 41 6d 74 2e 20 20 49 66 20 2a 70 41 6d 74 3d pAmt. If *pAmt=
16f4c 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c =0, then the val
16f4d 75 65 20 72 65 74 75 72 6e 65 64 20 77 69 6c 6c ue returned will
16f4e 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 20 76 61 6c not be.** a val
16f4f 69 64 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a id pointer..**.*
16f50 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
16f51 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f s an optimizatio
16f52 6e 2e 20 20 49 74 20 69 73 20 63 6f 6d 6d 6f 6e n. It is common
16f53 20 66 6f 72 20 74 68 65 20 65 6e 74 69 72 65 20 for the entire
16f54 6b 65 79 0a 2a 2a 20 61 6e 64 20 64 61 74 61 20 key.** and data
16f55 74 6f 20 66 69 74 20 6f 6e 20 74 68 65 20 6c 6f to fit on the lo
16f56 63 61 6c 20 70 61 67 65 20 61 6e 64 20 66 6f 72 cal page and for
16f57 20 74 68 65 72 65 20 74 6f 20 62 65 20 6e 6f 20 there to be no
16f58 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 overflow.** page
16f59 73 2e 20 20 57 68 65 6e 20 74 68 61 74 20 69 73 s. When that is
16f5a 20 73 6f 2c 20 74 68 69 73 20 72 6f 75 74 69 6e so, this routin
16f5b 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f e can be used to
16f5c 20 61 63 63 65 73 73 20 74 68 65 0a 2a 2a 20 6b access the.** k
16f5d 65 79 20 61 6e 64 20 64 61 74 61 20 77 69 74 68 ey and data with
16f5e 6f 75 74 20 6d 61 6b 69 6e 67 20 61 20 63 6f 70 out making a cop
16f5f 79 2e 20 20 49 66 20 74 68 65 20 6b 65 79 20 61 y. If the key a
16f60 6e 64 2f 6f 72 20 64 61 74 61 20 73 70 69 6c 6c nd/or data spill
16f61 73 0a 2a 2a 20 6f 6e 74 6f 20 6f 76 65 72 66 6c s.** onto overfl
16f62 6f 77 20 70 61 67 65 73 2c 20 74 68 65 6e 20 61 ow pages, then a
16f63 63 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 6d ccessPayload() m
16f64 75 73 74 20 62 65 20 75 73 65 64 20 74 6f 20 72 ust be used to r
16f65 65 61 73 73 65 6d 62 6c 65 0a 2a 2a 20 74 68 65 eassemble.** the
16f66 20 6b 65 79 2f 64 61 74 61 20 61 6e 64 20 63 6f key/data and co
16f67 70 79 20 69 74 20 69 6e 74 6f 20 61 20 70 72 65 py it into a pre
16f68 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 allocated buffer
16f69 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e ..**.** The poin
16f6a 74 65 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 ter returned by
16f6b 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6c 6f 6f this routine loo
16f6c 6b 73 20 64 69 72 65 63 74 6c 79 20 69 6e 74 6f ks directly into
16f6d 20 74 68 65 20 63 61 63 68 65 64 0a 2a 2a 20 70 the cached.** p
16f6e 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 age of the datab
16f6f 61 73 65 2e 20 20 54 68 65 20 64 61 74 61 20 6d ase. The data m
16f70 69 67 68 74 20 63 68 61 6e 67 65 20 6f 72 20 6d ight change or m
16f71 6f 76 65 20 74 68 65 20 6e 65 78 74 20 74 69 6d ove the next tim
16f72 65 0a 2a 2a 20 61 6e 79 20 62 74 72 65 65 20 72 e.** any btree r
16f73 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
16f74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 ..*/.static cons
16f75 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
16f76 2a 66 65 74 63 68 50 61 79 6c 6f 61 64 28 0a 20 *fetchPayload(.
16f77 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c BtCursor *pCur,
16f78 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 /* Cursor
16f79 70 6f 69 6e 74 69 6e 67 20 74 6f 20 65 6e 74 72 pointing to entr
16f7a 79 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a y to read from *
16f7b 2f 0a 20 20 69 6e 74 20 2a 70 41 6d 74 2c 20 20 /. int *pAmt,
16f7c 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
16f7d 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 e the number of
16f7e 61 76 61 69 6c 61 62 6c 65 20 62 79 74 65 73 20 available bytes
16f7f 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6b here */. int sk
16f80 69 70 4b 65 79 20 20 20 20 20 20 20 20 20 20 2f ipKey /
16f81 2a 20 72 65 61 64 20 62 65 67 69 6e 6e 69 6e 67 * read beginning
16f82 20 61 74 20 64 61 74 61 20 69 66 20 74 68 69 73 at data if this
16f83 20 69 73 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 is true */.){.
16f84 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
16f85 61 50 61 79 6c 6f 61 64 3b 0a 20 20 4d 65 6d 50 aPayload;. MemP
16f86 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 75 33 age *pPage;. u3
16f87 32 20 6e 4b 65 79 3b 0a 20 20 75 33 32 20 6e 4c 2 nKey;. u32 nL
16f88 6f 63 61 6c 3b 0a 0a 20 20 61 73 73 65 72 74 28 ocal;.. assert(
16f89 20 70 43 75 72 21 3d 30 20 26 26 20 70 43 75 72 pCur!=0 && pCur
16f8a 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 ->iPage>=0 && pC
16f8b 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
16f8c 3e 69 50 61 67 65 5d 29 3b 0a 20 20 61 73 73 65 >iPage]);. asse
16f8d 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
16f8e 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
16f8f 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 ;. assert( curs
16f90 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
16f91 72 29 20 29 3b 0a 20 20 70 50 61 67 65 20 3d 20 r) );. pPage =
16f92 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
16f93 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 r->iPage];. ass
16f94 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 ert( pCur->aiIdx
16f95 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 [pCur->iPage]<pP
16f96 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 age->nCell );.
16f97 69 66 28 20 4e 45 56 45 52 28 70 43 75 72 2d 3e if( NEVER(pCur->
16f98 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 29 20 29 info.nSize==0) )
16f99 7b 0a 20 20 20 20 62 74 72 65 65 50 61 72 73 65 {. btreeParse
16f9a 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 Cell(pCur->apPag
16f9b 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2c 20 e[pCur->iPage],
16f9c 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
16f9d 2d 3e 69 50 61 67 65 5d 2c 0a 20 20 20 20 20 20 ->iPage],.
16f9e 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70 43 &pC
16f9f 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20 7d 0a 20 ur->info);. }.
16fa0 20 61 50 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 aPayload = pCur
16fa1 2d 3e 69 6e 66 6f 2e 70 43 65 6c 6c 3b 0a 20 20 ->info.pCell;.
16fa2 61 50 61 79 6c 6f 61 64 20 2b 3d 20 70 43 75 72 aPayload += pCur
16fa3 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a ->info.nHeader;.
16fa4 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 if( pPage->int
16fa5 4b 65 79 20 29 7b 0a 20 20 20 20 6e 4b 65 79 20 Key ){. nKey
16fa6 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 0;. }else{.
16fa7 20 20 6e 4b 65 79 20 3d 20 28 69 6e 74 29 70 43 nKey = (int)pC
16fa8 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 ur->info.nKey;.
16fa9 20 7d 0a 20 20 69 66 28 20 73 6b 69 70 4b 65 79 }. if( skipKey
16faa 20 29 7b 0a 20 20 20 20 61 50 61 79 6c 6f 61 64 ){. aPayload
16fab 20 2b 3d 20 6e 4b 65 79 3b 0a 20 20 20 20 6e 4c += nKey;. nL
16fac 6f 63 61 6c 20 3d 20 70 43 75 72 2d 3e 69 6e 66 ocal = pCur->inf
16fad 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6e 4b 65 79 3b o.nLocal - nKey;
16fae 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4c . }else{. nL
16faf 6f 63 61 6c 20 3d 20 70 43 75 72 2d 3e 69 6e 66 ocal = pCur->inf
16fb0 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 61 73 o.nLocal;. as
16fb1 73 65 72 74 28 20 6e 4c 6f 63 61 6c 3c 3d 6e 4b sert( nLocal<=nK
16fb2 65 79 20 29 3b 0a 20 20 7d 0a 20 20 2a 70 41 6d ey );. }. *pAm
16fb3 74 20 3d 20 6e 4c 6f 63 61 6c 3b 0a 20 20 72 65 t = nLocal;. re
16fb4 74 75 72 6e 20 61 50 61 79 6c 6f 61 64 3b 0a 7d turn aPayload;.}
16fb5 0a 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 .../*.** For the
16fb6 20 65 6e 74 72 79 20 74 68 61 74 20 63 75 72 73 entry that curs
16fb7 6f 72 20 70 43 75 72 20 69 73 20 70 6f 69 6e 74 or pCur is point
16fb8 20 74 6f 2c 20 72 65 74 75 72 6e 20 61 73 0a 2a to, return as.*
16fb9 2a 20 6d 61 6e 79 20 62 79 74 65 73 20 6f 66 20 * many bytes of
16fba 74 68 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 the key or data
16fbb 61 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 as are available
16fbc 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 0a 2a 2a on the local.**
16fbd 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 20 57 b-tree page. W
16fbe 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 rite the number
16fbf 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 62 79 74 of available byt
16fc0 65 73 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 0a 2a es into *pAmt..*
16fc1 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 *.** The pointer
16fc2 20 72 65 74 75 72 6e 65 64 20 69 73 20 65 70 68 returned is eph
16fc3 65 6d 65 72 61 6c 2e 20 20 54 68 65 20 6b 65 79 emeral. The key
16fc4 2f 64 61 74 61 20 6d 61 79 20 6d 6f 76 65 0a 2a /data may move.*
16fc5 2a 20 6f 72 20 62 65 20 64 65 73 74 72 6f 79 65 * or be destroye
16fc6 64 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 63 61 d on the next ca
16fc7 6c 6c 20 74 6f 20 61 6e 79 20 42 74 72 65 65 20 ll to any Btree
16fc8 72 6f 75 74 69 6e 65 2c 0a 2a 2a 20 69 6e 63 6c routine,.** incl
16fc9 75 64 69 6e 67 20 63 61 6c 6c 73 20 66 72 6f 6d uding calls from
16fca 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 61 other threads a
16fcb 67 61 69 6e 73 74 20 74 68 65 20 73 61 6d 65 20 gainst the same
16fcc 63 61 63 68 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c cache..** Hence,
16fcd 20 61 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 a mutex on the
16fce 42 74 53 68 61 72 65 64 20 73 68 6f 75 6c 64 20 BtShared should
16fcf 62 65 20 68 65 6c 64 20 70 72 69 6f 72 20 74 6f be held prior to
16fd0 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 calling.** this
16fd1 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 routine..**.**
16fd2 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69 These routines i
16fd3 73 20 75 73 65 64 20 74 6f 20 67 65 74 20 71 75 s used to get qu
16fd4 69 63 6b 20 61 63 63 65 73 73 20 74 6f 20 6b 65 ick access to ke
16fd5 79 20 61 6e 64 20 64 61 74 61 0a 2a 2a 20 69 6e y and data.** in
16fd6 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 the common case
16fd7 20 77 68 65 72 65 20 6e 6f 20 6f 76 65 72 66 6c where no overfl
16fd8 6f 77 20 70 61 67 65 73 20 61 72 65 20 75 73 65 ow pages are use
16fd9 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
16fda 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 VATE const void
16fdb 2a 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 *sqlite3BtreeKey
16fdc 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 20 2a Fetch(BtCursor *
16fdd 70 43 75 72 2c 20 69 6e 74 20 2a 70 41 6d 74 29 pCur, int *pAmt)
16fde 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a {. const void *
16fdf 70 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 p = 0;. assert(
16fe0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
16fe1 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 eld(pCur->pBtree
16fe2 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
16fe3 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
16fe4 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
16fe5 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 );. if( ALWAYS
16fe6 28 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 (pCur->eState==C
16fe7 55 52 53 4f 52 5f 56 41 4c 49 44 29 20 29 7b 0a URSOR_VALID) ){.
16fe8 20 20 20 20 70 20 3d 20 28 63 6f 6e 73 74 20 76 p = (const v
16fe9 6f 69 64 2a 29 66 65 74 63 68 50 61 79 6c 6f 61 oid*)fetchPayloa
16fea 64 28 70 43 75 72 2c 20 70 41 6d 74 2c 20 30 29 d(pCur, pAmt, 0)
16feb 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
16fec 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
16fed 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 TE const void *s
16fee 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 46 qlite3BtreeDataF
16fef 65 74 63 68 28 42 74 43 75 72 73 6f 72 20 2a 70 etch(BtCursor *p
16ff0 43 75 72 2c 20 69 6e 74 20 2a 70 41 6d 74 29 7b Cur, int *pAmt){
16ff1 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 . const void *p
16ff2 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 = 0;. assert(
16ff3 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
16ff4 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d ld(pCur->pBtree-
16ff5 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
16ff6 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
16ff7 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
16ff8 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 );. if( ALWAYS(
16ff9 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
16ffa 52 53 4f 52 5f 56 41 4c 49 44 29 20 29 7b 0a 20 RSOR_VALID) ){.
16ffb 20 20 20 70 20 3d 20 28 63 6f 6e 73 74 20 76 6f p = (const vo
16ffc 69 64 2a 29 66 65 74 63 68 50 61 79 6c 6f 61 64 id*)fetchPayload
16ffd 28 70 43 75 72 2c 20 70 41 6d 74 2c 20 31 29 3b (pCur, pAmt, 1);
16ffe 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
16fff 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 .}.../*.** Move
17000 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 the cursor down
17001 74 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 to a new child p
17002 61 67 65 2e 20 20 54 68 65 20 6e 65 77 50 67 6e age. The newPgn
17003 6f 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 o argument is th
17004 65 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 e.** page number
17005 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 70 61 of the child pa
17006 67 65 20 74 6f 20 6d 6f 76 65 20 74 6f 2e 0a 2a ge to move to..*
17007 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
17008 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 on returns SQLIT
17009 45 5f 43 4f 52 52 55 50 54 20 69 66 20 74 68 65 E_CORRUPT if the
1700a 20 70 61 67 65 2d 68 65 61 64 65 72 20 66 6c 61 page-header fla
1700b 67 73 20 66 69 65 6c 64 20 6f 66 0a 2a 2a 20 74 gs field of.** t
1700c 68 65 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 he new child pag
1700d 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 e does not match
1700e 20 74 68 65 20 66 6c 61 67 73 20 66 69 65 6c 64 the flags field
1700f 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20 28 of the parent (
17010 69 2e 65 2e 0a 2a 2a 20 69 66 20 61 6e 20 69 6e i.e..** if an in
17011 74 6b 65 79 20 70 61 67 65 20 61 70 70 65 61 72 tkey page appear
17012 73 20 74 6f 20 62 65 20 74 68 65 20 70 61 72 65 s to be the pare
17013 6e 74 20 6f 66 20 61 20 6e 6f 6e 2d 69 6e 74 6b nt of a non-intk
17014 65 79 20 70 61 67 65 2c 20 6f 72 0a 2a 2a 20 76 ey page, or.** v
17015 69 63 65 2d 76 65 72 73 61 29 2e 0a 2a 2f 0a 73 ice-versa)..*/.s
17016 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f tatic int moveTo
17017 43 68 69 6c 64 28 42 74 43 75 72 73 6f 72 20 2a Child(BtCursor *
17018 70 43 75 72 2c 20 75 33 32 20 6e 65 77 50 67 6e pCur, u32 newPgn
17019 6f 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 o){. int rc;.
1701a 69 6e 74 20 69 20 3d 20 70 43 75 72 2d 3e 69 50 int i = pCur->iP
1701b 61 67 65 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a age;. MemPage *
1701c 70 4e 65 77 50 61 67 65 3b 0a 20 20 42 74 53 68 pNewPage;. BtSh
1701d 61 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72 ared *pBt = pCur
1701e 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 ->pBt;.. assert
1701f 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
17020 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ex(pCur) );. as
17021 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 sert( pCur->eSta
17022 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
17023 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
17024 75 72 2d 3e 69 50 61 67 65 3c 42 54 43 55 52 53 ur->iPage<BTCURS
17025 4f 52 5f 4d 41 58 5f 44 45 50 54 48 20 29 3b 0a OR_MAX_DEPTH );.
17026 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 if( pCur->iPag
17027 65 3e 3d 28 42 54 43 55 52 53 4f 52 5f 4d 41 58 e>=(BTCURSOR_MAX
17028 5f 44 45 50 54 48 2d 31 29 20 29 7b 0a 20 20 20 _DEPTH-1) ){.
17029 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
1702a 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d ORRUPT_BKPT;. }
1702b 0a 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e . rc = getAndIn
1702c 69 74 50 61 67 65 28 70 42 74 2c 20 6e 65 77 50 itPage(pBt, newP
1702d 67 6e 6f 2c 20 26 70 4e 65 77 50 61 67 65 29 3b gno, &pNewPage);
1702e 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 . if( rc ) retu
1702f 72 6e 20 72 63 3b 0a 20 20 70 43 75 72 2d 3e 61 rn rc;. pCur->a
17030 70 50 61 67 65 5b 69 2b 31 5d 20 3d 20 70 4e 65 pPage[i+1] = pNe
17031 77 50 61 67 65 3b 0a 20 20 70 43 75 72 2d 3e 61 wPage;. pCur->a
17032 69 49 64 78 5b 69 2b 31 5d 20 3d 20 30 3b 0a 20 iIdx[i+1] = 0;.
17033 20 70 43 75 72 2d 3e 69 50 61 67 65 2b 2b 3b 0a pCur->iPage++;.
17034 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 . pCur->info.nS
17035 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d ize = 0;. pCur-
17036 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a >validNKey = 0;.
17037 20 20 69 66 28 20 70 4e 65 77 50 61 67 65 2d 3e if( pNewPage->
17038 6e 43 65 6c 6c 3c 31 20 7c 7c 20 70 4e 65 77 50 nCell<1 || pNewP
17039 61 67 65 2d 3e 69 6e 74 4b 65 79 21 3d 70 43 75 age->intKey!=pCu
1703a 72 2d 3e 61 70 50 61 67 65 5b 69 5d 2d 3e 69 6e r->apPage[i]->in
1703b 74 4b 65 79 20 29 7b 0a 20 20 20 20 72 65 74 75 tKey ){. retu
1703c 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
1703d 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 T_BKPT;. }. re
1703e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1703f 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 }..#ifndef NDEBU
17040 47 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 70 50 61 G./*.** Page pPa
17041 72 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 72 rent is an inter
17042 6e 61 6c 20 28 6e 6f 6e 2d 6c 65 61 66 29 20 74 nal (non-leaf) t
17043 72 65 65 20 70 61 67 65 2e 20 54 68 69 73 20 66 ree page. This f
17044 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20 61 73 73 65 unction .** asse
17045 72 74 73 20 74 68 61 74 20 70 61 67 65 20 6e 75 rts that page nu
17046 6d 62 65 72 20 69 43 68 69 6c 64 20 69 73 20 74 mber iChild is t
17047 68 65 20 6c 65 66 74 2d 63 68 69 6c 64 20 69 66 he left-child if
17048 20 74 68 65 20 69 49 64 78 27 74 68 0a 2a 2a 20 the iIdx'th.**
17049 63 65 6c 6c 20 69 6e 20 70 61 67 65 20 70 50 61 cell in page pPa
1704a 72 65 6e 74 2e 20 4f 72 2c 20 69 66 20 69 49 64 rent. Or, if iId
1704b 78 20 69 73 20 65 71 75 61 6c 20 74 6f 20 74 68 x is equal to th
1704c 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
1704d 66 0a 2a 2a 20 63 65 6c 6c 73 20 69 6e 20 70 50 f.** cells in pP
1704e 61 72 65 6e 74 2c 20 74 68 61 74 20 70 61 67 65 arent, that page
1704f 20 6e 75 6d 62 65 72 20 69 43 68 69 6c 64 20 69 number iChild i
17050 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c s the right-chil
17051 64 20 6f 66 0a 2a 2a 20 74 68 65 20 70 61 67 65 d of.** the page
17052 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
17053 20 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 assertParentInd
17054 65 78 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 ex(MemPage *pPar
17055 65 6e 74 2c 20 69 6e 74 20 69 49 64 78 2c 20 50 ent, int iIdx, P
17056 67 6e 6f 20 69 43 68 69 6c 64 29 7b 0a 20 20 61 gno iChild){. a
17057 73 73 65 72 74 28 20 69 49 64 78 3c 3d 70 50 61 ssert( iIdx<=pPa
17058 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 rent->nCell );.
17059 20 69 66 28 20 69 49 64 78 3d 3d 70 50 61 72 65 if( iIdx==pPare
1705a 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 nt->nCell ){.
1705b 20 61 73 73 65 72 74 28 20 67 65 74 34 62 79 74 assert( get4byt
1705c 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 e(&pParent->aDat
1705d 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 a[pParent->hdrOf
1705e 66 73 65 74 2b 38 5d 29 3d 3d 69 43 68 69 6c 64 fset+8])==iChild
1705f 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 );. }else{.
17060 20 61 73 73 65 72 74 28 20 67 65 74 34 62 79 74 assert( get4byt
17061 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 65 e(findCell(pPare
17062 6e 74 2c 20 69 49 64 78 29 29 3d 3d 69 43 68 69 nt, iIdx))==iChi
17063 6c 64 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 ld );. }.}.#els
17064 65 0a 23 20 20 64 65 66 69 6e 65 20 61 73 73 65 e.# define asse
17065 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 78 2c rtParentIndex(x,
17066 79 2c 7a 29 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a y,z) .#endif../*
17067 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 .** Move the cur
17068 73 6f 72 20 75 70 20 74 6f 20 74 68 65 20 70 61 sor up to the pa
17069 72 65 6e 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a rent page..**.**
1706a 20 70 43 75 72 2d 3e 69 64 78 20 69 73 20 73 65 pCur->idx is se
1706b 74 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e t to the cell in
1706c 64 65 78 20 74 68 61 74 20 63 6f 6e 74 61 69 6e dex that contain
1706d 73 20 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a 2a s the pointer.**
1706e 20 74 6f 20 74 68 65 20 70 61 67 65 20 77 65 20 to the page we
1706f 61 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 2e are coming from.
17070 20 20 49 66 20 77 65 20 61 72 65 20 63 6f 6d 69 If we are comi
17071 6e 67 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 72 ng from the.** r
17072 69 67 68 74 2d 6d 6f 73 74 20 63 68 69 6c 64 20 ight-most child
17073 70 61 67 65 20 74 68 65 6e 20 70 43 75 72 2d 3e page then pCur->
17074 69 64 78 20 69 73 20 73 65 74 20 74 6f 20 6f 6e idx is set to on
17075 65 20 6d 6f 72 65 20 74 68 61 6e 0a 2a 2a 20 74 e more than.** t
17076 68 65 20 6c 61 72 67 65 73 74 20 63 65 6c 6c 20 he largest cell
17077 69 6e 64 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 index..*/.static
17078 20 76 6f 69 64 20 6d 6f 76 65 54 6f 50 61 72 65 void moveToPare
17079 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 nt(BtCursor *pCu
1707a 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 r){. assert( cu
1707b 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
1707c 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
1707d 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
1707e 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a CURSOR_VALID );.
1707f 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
17080 69 50 61 67 65 3e 30 20 29 3b 0a 20 20 61 73 73 iPage>0 );. ass
17081 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 ert( pCur->apPag
17082 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 e[pCur->iPage] )
17083 3b 0a 20 20 61 73 73 65 72 74 50 61 72 65 6e 74 ;. assertParent
17084 49 6e 64 65 78 28 0a 20 20 20 20 70 43 75 72 2d Index(. pCur-
17085 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
17086 61 67 65 2d 31 5d 2c 20 0a 20 20 20 20 70 43 75 age-1], . pCu
17087 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
17088 50 61 67 65 2d 31 5d 2c 20 0a 20 20 20 20 70 43 Page-1], . pC
17089 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
1708a 3e 69 50 61 67 65 5d 2d 3e 70 67 6e 6f 0a 20 20 >iPage]->pgno.
1708b 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 );. releasePage
1708c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 (pCur->apPage[pC
1708d 75 72 2d 3e 69 50 61 67 65 5d 29 3b 0a 20 20 70 ur->iPage]);. p
1708e 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a 20 20 Cur->iPage--;.
1708f 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
17090 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 = 0;. pCur->va
17091 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 7d 0a 0a lidNKey = 0;.}..
17092 2f 2a 0a 2a 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 70 6f 69 6e 74 20 74 ursor to point t
17094 6f 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 o the root page
17095 6f 66 20 69 74 73 20 62 2d 74 72 65 65 20 73 74 of its b-tree st
17096 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 ructure..**.** I
17097 66 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 f the table has
17098 61 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 a virtual root p
17099 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 63 75 age, then the cu
1709a 72 73 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 6f rsor is moved to
1709b 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 point.** to the
1709c 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 virtual root pa
1709d 67 65 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 ge instead of th
1709e 65 20 61 63 74 75 61 6c 20 72 6f 6f 74 20 70 61 e actual root pa
1709f 67 65 2e 20 41 20 74 61 62 6c 65 20 68 61 73 20 ge. A table has
170a0 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 72 6f 6f a.** virtual roo
170a1 74 20 70 61 67 65 20 77 68 65 6e 20 74 68 65 20 t page when the
170a2 61 63 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 actual root page
170a3 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 65 6c contains no cel
170a4 6c 73 20 61 6e 64 20 61 20 0a 2a 2a 20 73 69 6e ls and a .** sin
170a5 67 6c 65 20 63 68 69 6c 64 20 70 61 67 65 2e 20 gle child page.
170a6 54 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 68 61 This can only ha
170a7 70 70 65 6e 20 77 69 74 68 20 74 68 65 20 74 61 ppen with the ta
170a8 62 6c 65 20 72 6f 6f 74 65 64 20 61 74 20 70 61 ble rooted at pa
170a9 67 65 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ge 1..**.** If t
170aa 68 65 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 he b-tree struct
170ab 75 72 65 20 69 73 20 65 6d 70 74 79 2c 20 74 68 ure is empty, th
170ac 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 20 69 e cursor state i
170ad 73 20 73 65 74 20 74 6f 20 0a 2a 2a 20 43 55 52 s set to .** CUR
170ae 53 4f 52 5f 49 4e 56 41 4c 49 44 2e 20 4f 74 68 SOR_INVALID. Oth
170af 65 72 77 69 73 65 2c 20 74 68 65 20 63 75 72 73 erwise, the curs
170b0 6f 72 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 or is set to poi
170b1 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 0a nt to the first.
170b2 2a 2a 20 63 65 6c 6c 20 6c 6f 63 61 74 65 64 20 ** cell located
170b3 6f 6e 20 74 68 65 20 72 6f 6f 74 20 28 6f 72 20 on the root (or
170b4 76 69 72 74 75 61 6c 20 72 6f 6f 74 29 20 70 61 virtual root) pa
170b5 67 65 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f ge and the curso
170b6 72 20 73 74 61 74 65 0a 2a 2a 20 69 73 20 73 65 r state.** is se
170b7 74 20 74 6f 20 43 55 52 53 4f 52 5f 56 41 4c 49 t to CURSOR_VALI
170b8 44 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 D..**.** If this
170b9 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
170ba 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 s successfully,
170bb 69 74 20 6d 61 79 20 62 65 20 61 73 73 75 6d 65 it may be assume
170bc 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 70 61 d that the.** pa
170bd 67 65 2d 68 65 61 64 65 72 20 66 6c 61 67 73 20 ge-header flags
170be 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 indicate that th
170bf 65 20 5b 76 69 72 74 75 61 6c 5d 20 72 6f 6f 74 e [virtual] root
170c0 2d 70 61 67 65 20 69 73 20 74 68 65 20 65 78 70 -page is the exp
170c1 65 63 74 65 64 20 0a 2a 2a 20 6b 69 6e 64 20 6f ected .** kind o
170c2 66 20 62 2d 74 72 65 65 20 70 61 67 65 20 28 69 f b-tree page (i
170c3 2e 65 2e 20 69 66 20 77 68 65 6e 20 6f 70 65 6e .e. if when open
170c4 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 20 74 ing the cursor t
170c5 68 65 20 63 61 6c 6c 65 72 20 64 69 64 20 6e 6f he caller did no
170c6 74 0a 2a 2a 20 73 70 65 63 69 66 79 20 61 20 4b t.** specify a K
170c7 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 eyInfo structure
170c8 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20 the flags byte
170c9 69 73 20 73 65 74 20 74 6f 20 30 78 30 35 20 6f is set to 0x05 o
170ca 72 20 30 78 30 44 2c 0a 2a 2a 20 69 6e 64 69 63 r 0x0D,.** indic
170cb 61 74 69 6e 67 20 61 20 74 61 62 6c 65 20 62 2d ating a table b-
170cc 74 72 65 65 2c 20 6f 72 20 69 66 20 74 68 65 20 tree, or if the
170cd 63 61 6c 6c 65 72 20 64 69 64 20 73 70 65 63 69 caller did speci
170ce 66 79 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a 2a fy a KeyInfo .**
170cf 20 73 74 72 75 63 74 75 72 65 20 74 68 65 20 66 structure the f
170d0 6c 61 67 73 20 62 79 74 65 20 69 73 20 73 65 74 lags byte is set
170d1 20 74 6f 20 30 78 30 32 20 6f 72 20 30 78 30 41 to 0x02 or 0x0A
170d2 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 61 6e 20 , indicating an
170d3 69 6e 64 65 78 0a 2a 2a 20 62 2d 74 72 65 65 29 index.** b-tree)
170d4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
170d5 6d 6f 76 65 54 6f 52 6f 6f 74 28 42 74 43 75 72 moveToRoot(BtCur
170d6 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d 65 sor *pCur){. Me
170d7 6d 50 61 67 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 mPage *pRoot;.
170d8 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
170d9 4f 4b 3b 0a 20 20 42 74 72 65 65 20 2a 70 20 3d OK;. Btree *p =
170da 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 pCur->pBtree;.
170db 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
170dc 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 p->pBt;.. asse
170dd 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
170de 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
170df 61 73 73 65 72 74 28 20 43 55 52 53 4f 52 5f 49 assert( CURSOR_I
170e0 4e 56 41 4c 49 44 20 3c 20 43 55 52 53 4f 52 5f NVALID < CURSOR_
170e1 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 REQUIRESEEK );.
170e2 20 61 73 73 65 72 74 28 20 43 55 52 53 4f 52 5f assert( CURSOR_
170e3 56 41 4c 49 44 20 20 20 3c 20 43 55 52 53 4f 52 VALID < CURSOR
170e4 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a _REQUIRESEEK );.
170e5 20 20 61 73 73 65 72 74 28 20 43 55 52 53 4f 52 assert( CURSOR
170e6 5f 46 41 55 4c 54 20 20 20 3e 20 43 55 52 53 4f _FAULT > CURSO
170e7 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b R_REQUIRESEEK );
170e8 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 . if( pCur->eSt
170e9 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 ate>=CURSOR_REQU
170ea 49 52 45 53 45 45 4b 20 29 7b 0a 20 20 20 20 69 IRESEEK ){. i
170eb 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d f( pCur->eState=
170ec 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b =CURSOR_FAULT ){
170ed 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
170ee 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 21 3d 53 Cur->skipNext!=S
170ef 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 QLITE_OK );.
170f0 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d 3e 73 return pCur->s
170f1 6b 69 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 kipNext;. }.
170f2 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 sqlite3BtreeC
170f3 6c 65 61 72 43 75 72 73 6f 72 28 70 43 75 72 29 learCursor(pCur)
170f4 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 43 75 ;. }.. if( pCu
170f5 72 2d 3e 69 50 61 67 65 3e 3d 30 20 29 7b 0a 20 r->iPage>=0 ){.
170f6 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f int i;. fo
170f7 72 28 69 3d 31 3b 20 69 3c 3d 70 43 75 72 2d 3e r(i=1; i<=pCur->
170f8 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 iPage; i++){.
170f9 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
170fa 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b Cur->apPage[i]);
170fb 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 72 2d . }. pCur-
170fc 3e 69 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d 65 >iPage = 0;. }e
170fd 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 67 65 lse{. rc = ge
170fe 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 tAndInitPage(pBt
170ff 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 , pCur->pgnoRoot
17100 2c 20 26 70 43 75 72 2d 3e 61 70 50 61 67 65 5b , &pCur->apPage[
17101 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 0]);. if( rc!
17102 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
17103 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 pCur->eState
17104 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 = CURSOR_INVALI
17105 44 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 D;. return
17106 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 rc;. }. pC
17107 75 72 2d 3e 69 50 61 67 65 20 3d 20 30 3b 0a 0a ur->iPage = 0;..
17108 20 20 20 20 2f 2a 20 49 66 20 70 43 75 72 2d 3e /* If pCur->
17109 70 4b 65 79 49 6e 66 6f 20 69 73 20 6e 6f 74 20 pKeyInfo is not
1710a 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 63 NULL, then the c
1710b 61 6c 6c 65 72 20 74 68 61 74 20 6f 70 65 6e 65 aller that opene
1710c 64 20 74 68 69 73 20 63 75 72 73 6f 72 0a 20 20 d this cursor.
1710d 20 20 2a 2a 20 65 78 70 65 63 74 65 64 20 74 6f ** expected to
1710e 20 6f 70 65 6e 20 69 74 20 6f 6e 20 61 6e 20 69 open it on an i
1710f 6e 64 65 78 20 62 2d 74 72 65 65 2e 20 4f 74 68 ndex b-tree. Oth
17110 65 72 77 69 73 65 2c 20 69 66 20 70 4b 65 79 49 erwise, if pKeyI
17111 6e 66 6f 20 69 73 0a 20 20 20 20 2a 2a 20 4e 55 nfo is. ** NU
17112 4c 4c 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 65 LL, the caller e
17113 78 70 65 63 74 73 20 61 20 74 61 62 6c 65 20 62 xpects a table b
17114 2d 74 72 65 65 2e 20 49 66 20 74 68 69 73 20 69 -tree. If this i
17115 73 20 6e 6f 74 20 74 68 65 20 63 61 73 65 2c 0a s not the case,.
17116 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 61 6e ** return an
17117 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 SQLITE_CORRUPT
17118 65 72 72 6f 72 2e 20 20 2a 2f 0a 20 20 20 20 61 error. */. a
17119 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 ssert( pCur->apP
1711a 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 3d 3d age[0]->intKey==
1711b 31 20 7c 7c 20 70 43 75 72 2d 3e 61 70 50 61 67 1 || pCur->apPag
1711c 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 3d 3d 30 20 e[0]->intKey==0
1711d 29 3b 0a 20 20 20 20 69 66 28 20 28 70 43 75 72 );. if( (pCur
1711e 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 29 21 3d ->pKeyInfo==0)!=
1711f 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d pCur->apPage[0]-
17120 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 >intKey ){.
17121 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
17122 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
17123 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 }. }.. /* Ass
17124 65 72 74 20 74 68 61 74 20 74 68 65 20 72 6f 6f ert that the roo
17125 74 20 70 61 67 65 20 69 73 20 6f 66 20 74 68 65 t page is of the
17126 20 63 6f 72 72 65 63 74 20 74 79 70 65 2e 20 54 correct type. T
17127 68 69 73 20 6d 75 73 74 20 62 65 20 74 68 65 0a his must be the.
17128 20 20 2a 2a 20 63 61 73 65 20 61 73 20 74 68 65 ** case as the
17129 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 66 75 call to this fu
1712a 6e 63 74 69 6f 6e 20 74 68 61 74 20 6c 6f 61 64 nction that load
1712b 65 64 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 ed the root-page
1712c 20 28 65 69 74 68 65 72 0a 20 20 2a 2a 20 74 68 (either. ** th
1712d 69 73 20 63 61 6c 6c 20 6f 72 20 61 20 70 72 65 is call or a pre
1712e 76 69 6f 75 73 20 69 6e 76 6f 63 61 74 69 6f 6e vious invocation
1712f 29 20 77 6f 75 6c 64 20 68 61 76 65 20 64 65 74 ) would have det
17130 65 63 74 65 64 20 63 6f 72 72 75 70 74 69 6f 6e ected corruption
17131 20 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 61 73 . ** if the as
17132 73 75 6d 70 74 69 6f 6e 20 77 65 72 65 20 6e 6f sumption were no
17133 74 20 74 72 75 65 2c 20 61 6e 64 20 69 74 20 69 t true, and it i
17134 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 s not possible f
17135 6f 72 20 74 68 65 20 66 6c 61 67 73 20 0a 20 20 or the flags .
17136 2a 2a 20 62 79 74 65 20 74 6f 20 68 61 76 65 20 ** byte to have
17137 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 77 68 been modified wh
17138 69 6c 65 20 74 68 69 73 20 63 75 72 73 6f 72 20 ile this cursor
17139 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 66 is holding a ref
1713a 65 72 65 6e 63 65 0a 20 20 2a 2a 20 74 6f 20 74 erence. ** to t
1713b 68 65 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20 70 he page. */. p
1713c 52 6f 6f 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 Root = pCur->apP
1713d 61 67 65 5b 30 5d 3b 0a 20 20 61 73 73 65 72 74 age[0];. assert
1713e 28 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 70 ( pRoot->pgno==p
1713f 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29 3b Cur->pgnoRoot );
17140 0a 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 . assert( pRoot
17141 2d 3e 69 73 49 6e 69 74 20 26 26 20 28 70 43 75 ->isInit && (pCu
17142 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 29 3d r->pKeyInfo==0)=
17143 3d 70 52 6f 6f 74 2d 3e 69 6e 74 4b 65 79 20 29 =pRoot->intKey )
17144 3b 0a 0a 20 20 70 43 75 72 2d 3e 61 69 49 64 78 ;.. pCur->aiIdx
17145 5b 30 5d 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d [0] = 0;. pCur-
17146 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b >info.nSize = 0;
17147 0a 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 . pCur->atLast
17148 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c = 0;. pCur->val
17149 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 0a 20 20 69 idNKey = 0;.. i
1714a 66 28 20 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3d f( pRoot->nCell=
1714b 3d 30 20 26 26 20 21 70 52 6f 6f 74 2d 3e 6c 65 =0 && !pRoot->le
1714c 61 66 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 73 af ){. Pgno s
1714d 75 62 70 61 67 65 3b 0a 20 20 20 20 69 66 28 20 ubpage;. if(
1714e 70 52 6f 6f 74 2d 3e 70 67 6e 6f 21 3d 31 20 29 pRoot->pgno!=1 )
1714f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
17150 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
17151 20 73 75 62 70 61 67 65 20 3d 20 67 65 74 34 62 subpage = get4b
17152 79 74 65 28 26 70 52 6f 6f 74 2d 3e 61 44 61 74 yte(&pRoot->aDat
17153 61 5b 70 52 6f 6f 74 2d 3e 68 64 72 4f 66 66 73 a[pRoot->hdrOffs
17154 65 74 2b 38 5d 29 3b 0a 20 20 20 20 70 43 75 72 et+8]);. pCur
17155 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f ->eState = CURSO
17156 52 5f 56 41 4c 49 44 3b 0a 20 20 20 20 72 63 20 R_VALID;. rc
17157 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 = moveToChild(pC
17158 75 72 2c 20 73 75 62 70 61 67 65 29 3b 0a 20 20 ur, subpage);.
17159 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 75 72 2d }else{. pCur-
1715a 3e 65 53 74 61 74 65 20 3d 20 28 28 70 52 6f 6f >eState = ((pRoo
1715b 74 2d 3e 6e 43 65 6c 6c 3e 30 29 3f 43 55 52 53 t->nCell>0)?CURS
1715c 4f 52 5f 56 41 4c 49 44 3a 43 55 52 53 4f 52 5f OR_VALID:CURSOR_
1715d 49 4e 56 41 4c 49 44 29 3b 0a 20 20 7d 0a 20 20 INVALID);. }.
1715e 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1715f 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 .** Move the cur
17160 73 6f 72 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 sor down to the
17161 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65 left-most leaf e
17162 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65 ntry beneath the
17163 0a 2a 2a 20 65 6e 74 72 79 20 74 6f 20 77 68 69 .** entry to whi
17164 63 68 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 ch it is current
17165 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a ly pointing..**.
17166 2a 2a 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73 74 ** The left-most
17167 20 6c 65 61 66 20 69 73 20 74 68 65 20 6f 6e 65 leaf is the one
17168 20 77 69 74 68 20 74 68 65 20 73 6d 61 6c 6c 65 with the smalle
17169 73 74 20 6b 65 79 20 2d 20 74 68 65 20 66 69 72 st key - the fir
1716a 73 74 0a 2a 2a 20 69 6e 20 61 73 63 65 6e 64 69 st.** in ascendi
1716b 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 ng order..*/.sta
1716c 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 4c 65 tic int moveToLe
1716d 66 74 6d 6f 73 74 28 42 74 43 75 72 73 6f 72 20 ftmost(BtCursor
1716e 2a 70 43 75 72 29 7b 0a 20 20 50 67 6e 6f 20 70 *pCur){. Pgno p
1716f 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 gno;. int rc =
17170 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d SQLITE_OK;. Mem
17171 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 Page *pPage;..
17172 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
17173 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
17174 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
17175 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
17176 5f 56 41 4c 49 44 20 29 3b 0a 20 20 77 68 69 6c _VALID );. whil
17177 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b e( rc==SQLITE_OK
17178 20 26 26 20 21 28 70 50 61 67 65 20 3d 20 70 43 && !(pPage = pC
17179 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
1717a 3e 69 50 61 67 65 5d 29 2d 3e 6c 65 61 66 20 29 >iPage])->leaf )
1717b 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 {. assert( pC
1717c 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
1717d 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 iPage]<pPage->nC
1717e 65 6c 6c 20 29 3b 0a 20 20 20 20 70 67 6e 6f 20 ell );. pgno
1717f 3d 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 = get4byte(findC
17180 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 75 72 2d ell(pPage, pCur-
17181 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
17182 67 65 5d 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 ge]));. rc =
17183 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 moveToChild(pCur
17184 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 , pgno);. }. r
17185 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
17186 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 ** Move the curs
17187 6f 72 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 72 or down to the r
17188 69 67 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65 ight-most leaf e
17189 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65 ntry beneath the
1718a 0a 2a 2a 20 70 61 67 65 20 74 6f 20 77 68 69 63 .** page to whic
1718b 68 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c h it is currentl
1718c 79 20 70 6f 69 6e 74 69 6e 67 2e 20 20 4e 6f 74 y pointing. Not
1718d 69 63 65 20 74 68 65 20 64 69 66 66 65 72 65 6e ice the differen
1718e 63 65 0a 2a 2a 20 62 65 74 77 65 65 6e 20 6d 6f ce.** between mo
1718f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 20 61 veToLeftmost() a
17190 6e 64 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f nd moveToRightmo
17191 73 74 28 29 2e 20 20 6d 6f 76 65 54 6f 4c 65 66 st(). moveToLef
17192 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73 tmost().** finds
17193 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 65 the left-most e
17194 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65 ntry beneath the
17195 20 2a 65 6e 74 72 79 2a 20 77 68 65 72 65 61 73 *entry* whereas
17196 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 moveToRightmost
17197 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68 65 20 ().** finds the
17198 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 right-most entry
17199 20 62 65 6e 65 61 74 68 20 74 68 65 20 2a 70 61 beneath the *pa
1719a 67 65 2a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 ge*..**.** The r
1719b 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 ight-most entry
1719c 69 73 20 74 68 65 20 6f 6e 65 20 77 69 74 68 20 is the one with
1719d 74 68 65 20 6c 61 72 67 65 73 74 20 6b 65 79 20 the largest key
1719e 2d 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 6b 65 - the last.** ke
1719f 79 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f y in ascending o
171a0 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rder..*/.static
171a1 69 6e 74 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d int moveToRightm
171a2 6f 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 ost(BtCursor *pC
171a3 75 72 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f ur){. Pgno pgno
171a4 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
171a5 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 ITE_OK;. MemPag
171a6 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20 e *pPage = 0;..
171a7 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
171a8 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
171a9 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
171aa 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
171ab 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 77 68 69 R_VALID );. whi
171ac 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f le( rc==SQLITE_O
171ad 4b 20 26 26 20 21 28 70 50 61 67 65 20 3d 20 70 K && !(pPage = p
171ae 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
171af 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c 65 61 66 20 ->iPage])->leaf
171b0 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 ){. pgno = ge
171b1 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 t4byte(&pPage->a
171b2 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f Data[pPage->hdrO
171b3 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 70 ffset+8]);. p
171b4 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
171b5 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67 65 2d >iPage] = pPage-
171b6 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 72 63 20 3d >nCell;. rc =
171b7 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 moveToChild(pCu
171b8 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 r, pgno);. }.
171b9 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
171ba 4b 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 61 K ){. pCur->a
171bb 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
171bc 5d 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c ] = pPage->nCell
171bd 2d 31 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e -1;. pCur->in
171be 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
171bf 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 pCur->validNKe
171c0 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 y = 0;. }. ret
171c1 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f urn rc;.}../* Mo
171c2 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f ve the cursor to
171c3 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 the first entry
171c4 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 in the table.
171c5 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
171c6 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 .** on success.
171c7 20 53 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20 Set *pRes to 0
171c8 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 if the cursor ac
171c9 74 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f tually points to
171ca 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 something.** or
171cb 20 73 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20 set *pRes to 1
171cc 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 if the table is
171cd 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 empty..*/.SQLITE
171ce 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
171cf 69 74 65 33 42 74 72 65 65 46 69 72 73 74 28 42 ite3BtreeFirst(B
171d0 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 tCursor *pCur, i
171d1 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 nt *pRes){. int
171d2 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rc;.. assert(
171d3 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
171d4 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
171d5 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
171d6 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 x_held(pCur->pBt
171d7 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 ree->db->mutex)
171d8 29 3b 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f );. rc = moveTo
171d9 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66 Root(pCur);. if
171da 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
171db 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d ){. if( pCur-
171dc 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
171dd 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 INVALID ){.
171de 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
171df 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
171e0 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a e]->nCell==0 );.
171e1 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b *pRes = 1;
171e2 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
171e3 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 TE_OK;. }else
171e4 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
171e5 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
171e6 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c r->iPage]->nCell
171e7 3e 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 >0 );. *pRe
171e8 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 s = 0;. rc
171e9 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 = moveToLeftmost
171ea 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 (pCur);. }.
171eb 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
171ec 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 ../* Move the cu
171ed 72 73 6f 72 20 74 6f 20 74 68 65 20 6c 61 73 74 rsor to the last
171ee 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 entry in the ta
171ef 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c ble. Return SQL
171f0 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 ITE_OK.** on suc
171f1 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52 65 73 cess. Set *pRes
171f2 20 74 6f 20 30 20 69 66 20 74 68 65 20 63 75 72 to 0 if the cur
171f3 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 6f 69 sor actually poi
171f4 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e 67 nts to something
171f5 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 73 .** or set *pRes
171f6 20 74 6f 20 31 20 69 66 20 74 68 65 20 74 61 62 to 1 if the tab
171f7 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a le is empty..*/.
171f8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
171f9 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c nt sqlite3BtreeL
171fa 61 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 ast(BtCursor *pC
171fb 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a ur, int *pRes){.
171fc 20 20 69 6e 74 20 72 63 3b 0a 20 0a 20 20 61 73 int rc;. . as
171fd 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
171fe 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
171ff 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
17200 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 3_mutex_held(pCu
17201 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d r->pBtree->db->m
17202 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 utex) );.. /* I
17203 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 6c 72 f the cursor alr
17204 65 61 64 79 20 70 6f 69 6e 74 73 20 74 6f 20 74 eady points to t
17205 68 65 20 6c 61 73 74 20 65 6e 74 72 79 2c 20 74 he last entry, t
17206 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 his is a no-op.
17207 2a 2f 0a 20 20 69 66 28 20 43 55 52 53 4f 52 5f */. if( CURSOR_
17208 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 VALID==pCur->eSt
17209 61 74 65 20 26 26 20 70 43 75 72 2d 3e 61 74 4c ate && pCur->atL
1720a 61 73 74 20 29 7b 0a 23 69 66 64 65 66 20 53 51 ast ){.#ifdef SQ
1720b 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 2f LITE_DEBUG. /
1720c 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 73 65 72 * This block ser
1720d 76 65 73 20 74 6f 20 61 73 73 65 72 74 28 29 20 ves to assert()
1720e 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 that the cursor
1720f 72 65 61 6c 6c 79 20 64 6f 65 73 20 70 6f 69 6e really does poin
17210 74 20 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 t . ** to the
17211 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 last entry in t
17212 68 65 20 62 2d 74 72 65 65 2e 20 2a 2f 0a 20 20 he b-tree. */.
17213 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 66 6f int ii;. fo
17214 72 28 69 69 3d 30 3b 20 69 69 3c 70 43 75 72 2d r(ii=0; ii<pCur-
17215 3e 69 50 61 67 65 3b 20 69 69 2b 2b 29 7b 0a 20 >iPage; ii++){.
17216 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 assert( pCu
17217 72 2d 3e 61 69 49 64 78 5b 69 69 5d 3d 3d 70 43 r->aiIdx[ii]==pC
17218 75 72 2d 3e 61 70 50 61 67 65 5b 69 69 5d 2d 3e ur->apPage[ii]->
17219 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 7d 0a 20 nCell );. }.
1721a 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
1721b 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
1721c 67 65 5d 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 ge]==pCur->apPag
1721d 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
1721e 6e 43 65 6c 6c 2d 31 20 29 3b 0a 20 20 20 20 61 nCell-1 );. a
1721f 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 ssert( pCur->apP
17220 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
17221 2d 3e 6c 65 61 66 20 29 3b 0a 23 65 6e 64 69 66 ->leaf );.#endif
17222 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
17223 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 72 63 TE_OK;. }.. rc
17224 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 = moveToRoot(pC
17225 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 ur);. if( rc==S
17226 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
17227 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c if( CURSOR_INVAL
17228 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 ID==pCur->eState
17229 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
1722a 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 ( pCur->apPage[p
1722b 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 Cur->iPage]->nCe
1722c 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a ll==0 );. *
1722d 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 7d 65 pRes = 1;. }e
1722e 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 lse{. asser
1722f 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
17230 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
17231 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 . *pRes = 0
17232 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 ;. rc = mov
17233 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 70 43 75 eToRightmost(pCu
17234 72 29 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e r);. pCur->
17235 61 74 4c 61 73 74 20 3d 20 72 63 3d 3d 53 51 4c atLast = rc==SQL
17236 49 54 45 5f 4f 4b 20 3f 31 3a 30 3b 0a 20 20 20 ITE_OK ?1:0;.
17237 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
17238 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 rc;.}../* Move t
17239 68 65 20 63 75 72 73 6f 72 20 73 6f 20 74 68 61 he cursor so tha
1723a 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 t it points to a
1723b 6e 20 65 6e 74 72 79 20 6e 65 61 72 20 74 68 65 n entry near the
1723c 20 6b 65 79 20 0a 2a 2a 20 73 70 65 63 69 66 69 key .** specifi
1723d 65 64 20 62 79 20 70 49 64 78 4b 65 79 20 6f 72 ed by pIdxKey or
1723e 20 69 6e 74 4b 65 79 2e 20 20 20 52 65 74 75 72 intKey. Retur
1723f 6e 20 61 20 73 75 63 63 65 73 73 20 63 6f 64 65 n a success code
17240 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 49 4e 54 4b ..**.** For INTK
17241 45 59 20 74 61 62 6c 65 73 2c 20 74 68 65 20 69 EY tables, the i
17242 6e 74 4b 65 79 20 70 61 72 61 6d 65 74 65 72 20 ntKey parameter
17243 69 73 20 75 73 65 64 2e 20 20 70 49 64 78 4b 65 is used. pIdxKe
17244 79 20 0a 2a 2a 20 6d 75 73 74 20 62 65 20 4e 55 y .** must be NU
17245 4c 4c 2e 20 20 46 6f 72 20 69 6e 64 65 78 20 74 LL. For index t
17246 61 62 6c 65 73 2c 20 70 49 64 78 4b 65 79 20 69 ables, pIdxKey i
17247 73 20 75 73 65 64 20 61 6e 64 20 69 6e 74 4b 65 s used and intKe
17248 79 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 65 64 2e y.** is ignored.
17249 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 78 61 .**.** If an exa
1724a 63 74 20 6d 61 74 63 68 20 69 73 20 6e 6f 74 20 ct match is not
1724b 66 6f 75 6e 64 2c 20 74 68 65 6e 20 74 68 65 20 found, then the
1724c 63 75 72 73 6f 72 20 69 73 20 61 6c 77 61 79 73 cursor is always
1724d 0a 2a 2a 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e .** left pointin
1724e 67 20 61 74 20 61 20 6c 65 61 66 20 70 61 67 65 g at a leaf page
1724f 20 77 68 69 63 68 20 77 6f 75 6c 64 20 68 6f 6c which would hol
17250 64 20 74 68 65 20 65 6e 74 72 79 20 69 66 20 69 d the entry if i
17251 74 0a 2a 2a 20 77 65 72 65 20 70 72 65 73 65 6e t.** were presen
17252 74 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d t. The cursor m
17253 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e ight point to an
17254 20 65 6e 74 72 79 20 74 68 61 74 20 63 6f 6d 65 entry that come
17255 73 0a 2a 2a 20 62 65 66 6f 72 65 20 6f 72 20 61 s.** before or a
17256 66 74 65 72 20 74 68 65 20 6b 65 79 2e 0a 2a 2a fter the key..**
17257 0a 2a 2a 20 41 6e 20 69 6e 74 65 67 65 72 20 69 .** An integer i
17258 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a s written into *
17259 70 52 65 73 20 77 68 69 63 68 20 69 73 20 74 68 pRes which is th
1725a 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 63 e result of.** c
1725b 6f 6d 70 61 72 69 6e 67 20 74 68 65 20 6b 65 79 omparing the key
1725c 20 77 69 74 68 20 74 68 65 20 65 6e 74 72 79 20 with the entry
1725d 74 6f 20 77 68 69 63 68 20 74 68 65 20 63 75 72 to which the cur
1725e 73 6f 72 20 69 73 20 0a 2a 2a 20 70 6f 69 6e 74 sor is .** point
1725f 69 6e 67 2e 20 20 54 68 65 20 6d 65 61 6e 69 6e ing. The meanin
17260 67 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 g of the integer
17261 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a written into.**
17262 20 2a 70 52 65 73 20 69 73 20 61 73 20 66 6f 6c *pRes is as fol
17263 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 lows:.**.**
17264 2a 70 52 65 73 3c 30 20 20 20 20 20 20 54 68 65 *pRes<0 The
17265 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 cursor is left
17266 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 pointing at an e
17267 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 ntry that.**
17268 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 is
17269 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 69 6e smaller than in
1726a 74 4b 65 79 2f 70 49 64 78 4b 65 79 20 6f 72 20 tKey/pIdxKey or
1726b 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 if the table is
1726c 65 6d 70 74 79 0a 2a 2a 20 20 20 20 20 20 20 20 empty.**
1726d 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 74 68 and th
1726e 65 20 63 75 72 73 6f 72 20 69 73 20 74 68 65 72 e cursor is ther
1726f 65 66 6f 72 65 20 6c 65 66 74 20 70 6f 69 6e 74 efore left point
17270 20 74 6f 20 6e 6f 74 68 69 6e 67 2e 0a 2a 2a 0a to nothing..**.
17271 2a 2a 20 20 20 20 20 2a 70 52 65 73 3d 3d 30 20 ** *pRes==0
17272 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 The cursor i
17273 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 s left pointing
17274 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 at an entry that
17275 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
17276 20 20 20 20 20 65 78 61 63 74 6c 79 20 6d 61 74 exactly mat
17277 63 68 65 73 20 69 6e 74 4b 65 79 2f 70 49 64 78 ches intKey/pIdx
17278 4b 65 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a Key..**.** *
17279 70 52 65 73 3e 30 20 20 20 20 20 20 54 68 65 20 pRes>0 The
1727a 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 cursor is left p
1727b 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e ointing at an en
1727c 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 try that.**
1727d 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20 is
1727e 6c 61 72 67 65 72 20 74 68 61 6e 20 69 6e 74 4b larger than intK
1727f 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a ey/pIdxKey..**.*
17280 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
17281 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
17282 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 eMovetoUnpacked(
17283 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 . BtCursor *pCu
17284 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 r, /* T
17285 68 65 20 63 75 72 73 6f 72 20 74 6f 20 62 65 20 he cursor to be
17286 6d 6f 76 65 64 20 2a 2f 0a 20 20 55 6e 70 61 63 moved */. Unpac
17287 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b kedRecord *pIdxK
17288 65 79 2c 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 ey, /* Unpacked
17289 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 69 index key */. i
1728a 36 34 20 69 6e 74 4b 65 79 2c 20 20 20 20 20 20 64 intKey,
1728b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
1728c 61 62 6c 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e able key */. in
1728d 74 20 62 69 61 73 52 69 67 68 74 2c 20 20 20 20 t biasRight,
1728e 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 /* If tru
1728f 65 2c 20 62 69 61 73 20 74 68 65 20 73 65 61 72 e, bias the sear
17290 63 68 20 74 6f 20 74 68 65 20 68 69 67 68 20 65 ch to the high e
17291 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 nd */. int *pRe
17292 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s
17293 20 2f 2a 20 57 72 69 74 65 20 73 65 61 72 63 68 /* Write search
17294 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f results here */
17295 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 .){. int rc;..
17296 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
17297 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
17298 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
17299 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1729a 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 pCur->pBtree->db
1729b 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
1729c 73 65 72 74 28 20 70 52 65 73 20 29 3b 0a 20 20 sert( pRes );.
1729d 61 73 73 65 72 74 28 20 28 70 49 64 78 4b 65 79 assert( (pIdxKey
1729e 3d 3d 30 29 3d 3d 28 70 43 75 72 2d 3e 70 4b 65 ==0)==(pCur->pKe
1729f 79 49 6e 66 6f 3d 3d 30 29 20 29 3b 0a 0a 20 20 yInfo==0) );..
172a0 2f 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 /* If the cursor
172a1 20 69 73 20 61 6c 72 65 61 64 79 20 70 6f 73 69 is already posi
172a2 74 69 6f 6e 65 64 20 61 74 20 74 68 65 20 70 6f tioned at the po
172a3 69 6e 74 20 77 65 20 61 72 65 20 74 72 79 69 6e int we are tryin
172a4 67 0a 20 20 2a 2a 20 74 6f 20 6d 6f 76 65 20 74 g. ** to move t
172a5 6f 2c 20 74 68 65 6e 20 6a 75 73 74 20 72 65 74 o, then just ret
172a6 75 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e urn without doin
172a7 67 20 61 6e 79 20 77 6f 72 6b 20 2a 2f 0a 20 20 g any work */.
172a8 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 if( pCur->eState
172a9 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 26 ==CURSOR_VALID &
172aa 26 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 & pCur->validNKe
172ab 79 20 0a 20 20 20 26 26 20 70 43 75 72 2d 3e 61 y . && pCur->a
172ac 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 pPage[0]->intKey
172ad 20 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 20 70 . ){. if( p
172ae 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d Cur->info.nKey==
172af 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 intKey ){.
172b0 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 *pRes = 0;.
172b1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
172b2 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 K;. }. if(
172b3 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 26 26 pCur->atLast &&
172b4 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 pCur->info.nKey
172b5 3c 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 <intKey ){.
172b6 20 2a 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 *pRes = -1;.
172b7 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
172b8 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a _OK;. }. }..
172b9 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f rc = moveToRoo
172ba 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 t(pCur);. if( r
172bb 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 c ){. return
172bc 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 rc;. }. assert
172bd 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 ( pCur->apPage[p
172be 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 Cur->iPage] );.
172bf 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
172c0 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
172c1 65 5d 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 e]->isInit );.
172c2 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 assert( pCur->ap
172c3 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
172c4 5d 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 70 43 ]->nCell>0 || pC
172c5 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
172c6 4f 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 OR_INVALID );.
172c7 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 if( pCur->eState
172c8 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 ==CURSOR_INVALID
172c9 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 ){. *pRes =
172ca 2d 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 -1;. assert(
172cb 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
172cc 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c r->iPage]->nCell
172cd 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72 ==0 );. retur
172ce 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
172cf 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
172d0 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b >apPage[0]->intK
172d1 65 79 20 7c 7c 20 70 49 64 78 4b 65 79 20 29 3b ey || pIdxKey );
172d2 0a 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 . for(;;){.
172d3 69 6e 74 20 6c 77 72 2c 20 75 70 72 3b 0a 20 20 int lwr, upr;.
172d4 20 20 50 67 6e 6f 20 63 68 6c 64 50 67 3b 0a 20 Pgno chldPg;.
172d5 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 MemPage *pPag
172d6 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 e = pCur->apPage
172d7 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 [pCur->iPage];.
172d8 20 20 20 69 6e 74 20 63 3b 0a 0a 20 20 20 20 2f int c;.. /
172d9 2a 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 6d * pPage->nCell m
172da 75 73 74 20 62 65 20 67 72 65 61 74 65 72 20 74 ust be greater t
172db 68 61 6e 20 7a 65 72 6f 2e 20 49 66 20 74 68 69 han zero. If thi
172dc 73 20 69 73 20 74 68 65 20 72 6f 6f 74 2d 70 61 s is the root-pa
172dd 67 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 75 ge. ** the cu
172de 72 73 6f 72 20 77 6f 75 6c 64 20 68 61 76 65 20 rsor would have
172df 62 65 65 6e 20 49 4e 56 41 4c 49 44 20 61 62 6f been INVALID abo
172e0 76 65 20 61 6e 64 20 74 68 69 73 20 66 6f 72 28 ve and this for(
172e1 3b 3b 29 20 6c 6f 6f 70 0a 20 20 20 20 2a 2a 20 ;;) loop. **
172e2 6e 6f 74 20 72 75 6e 2e 20 49 66 20 74 68 69 73 not run. If this
172e3 20 69 73 20 6e 6f 74 20 74 68 65 20 72 6f 6f 74 is not the root
172e4 2d 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 -page, then the
172e5 6d 6f 76 65 54 6f 43 68 69 6c 64 28 29 20 72 6f moveToChild() ro
172e6 75 74 69 6e 65 0a 20 20 20 20 2a 2a 20 77 6f 75 utine. ** wou
172e7 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 ld have already
172e8 64 65 74 65 63 74 65 64 20 64 62 20 63 6f 72 72 detected db corr
172e9 75 70 74 69 6f 6e 2e 20 53 69 6d 69 6c 61 72 6c uption. Similarl
172ea 79 2c 20 70 50 61 67 65 20 6d 75 73 74 0a 20 20 y, pPage must.
172eb 20 20 2a 2a 20 62 65 20 74 68 65 20 72 69 67 68 ** be the righ
172ec 74 20 6b 69 6e 64 20 28 69 6e 64 65 78 20 6f 72 t kind (index or
172ed 20 74 61 62 6c 65 29 20 6f 66 20 62 2d 74 72 65 table) of b-tre
172ee 65 20 70 61 67 65 2e 20 4f 74 68 65 72 77 69 73 e page. Otherwis
172ef 65 0a 20 20 20 20 2a 2a 20 61 20 6d 6f 76 65 54 e. ** a moveT
172f0 6f 43 68 69 6c 64 28 29 20 6f 72 20 6d 6f 76 65 oChild() or move
172f1 54 6f 52 6f 6f 74 28 29 20 63 61 6c 6c 20 77 6f ToRoot() call wo
172f2 75 6c 64 20 68 61 76 65 20 64 65 74 65 63 74 65 uld have detecte
172f3 64 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 20 2a d corruption. *
172f4 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 /. assert( pP
172f5 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a age->nCell>0 );.
172f6 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
172f7 65 2d 3e 69 6e 74 4b 65 79 3d 3d 28 70 49 64 78 e->intKey==(pIdx
172f8 4b 65 79 3d 3d 30 29 20 29 3b 0a 20 20 20 20 6c Key==0) );. l
172f9 77 72 20 3d 20 30 3b 0a 20 20 20 20 75 70 72 20 wr = 0;. upr
172fa 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 = pPage->nCell-1
172fb 3b 0a 20 20 20 20 69 66 28 20 62 69 61 73 52 69 ;. if( biasRi
172fc 67 68 74 20 29 7b 0a 20 20 20 20 20 20 70 43 75 ght ){. pCu
172fd 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
172fe 50 61 67 65 5d 20 3d 20 28 75 31 36 29 75 70 72 Page] = (u16)upr
172ff 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
17300 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 pCur->aiIdx[p
17301 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 Cur->iPage] = (u
17302 31 36 29 28 28 75 70 72 2b 6c 77 72 29 2f 32 29 16)((upr+lwr)/2)
17303 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 ;. }. for(
17304 3b 3b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 ;;){. int i
17305 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 dx = pCur->aiIdx
17306 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 20 2f [pCur->iPage]; /
17307 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 72 65 * Index of curre
17308 6e 74 20 63 65 6c 6c 20 69 6e 20 70 50 61 67 65 nt cell in pPage
17309 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 2a 70 43 */. u8 *pC
1730a 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 ell;
1730b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1730c 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 75 72 72 Pointer to curr
1730d 65 6e 74 20 63 65 6c 6c 20 69 6e 20 70 50 61 67 ent cell in pPag
1730e 65 20 2a 2f 0a 0a 20 20 20 20 20 20 70 43 75 72 e */.. pCur
1730f 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 ->info.nSize = 0
17310 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 ;. pCell =
17311 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
17312 69 64 78 29 20 2b 20 70 50 61 67 65 2d 3e 63 68 idx) + pPage->ch
17313 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 20 20 ildPtrSize;.
17314 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 if( pPage->int
17315 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 69 Key ){. i
17316 36 34 20 6e 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 64 nCellKey;.
17317 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e if( pPage->
17318 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 20 hasData ){.
17319 20 20 20 20 20 75 33 32 20 64 75 6d 6d 79 3b 0a u32 dummy;.
1731a 20 20 20 20 20 20 20 20 20 20 70 43 65 6c 6c 20 pCell
1731b 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 70 += getVarint32(p
1731c 43 65 6c 6c 2c 20 64 75 6d 6d 79 29 3b 0a 20 20 Cell, dummy);.
1731d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1731e 67 65 74 56 61 72 69 6e 74 28 70 43 65 6c 6c 2c getVarint(pCell,
1731f 20 28 75 36 34 2a 29 26 6e 43 65 6c 6c 4b 65 79 (u64*)&nCellKey
17320 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e );. if( n
17321 43 65 6c 6c 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 CellKey==intKey
17322 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d ){. c =
17323 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 0;. }els
17324 65 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 69 e if( nCellKey<i
17325 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 ntKey ){.
17326 20 20 20 63 20 3d 20 2d 31 3b 0a 20 20 20 20 20 c = -1;.
17327 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
17328 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c assert( nCel
17329 6c 4b 65 79 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 lKey>intKey );.
1732a 20 20 20 20 20 20 20 20 20 63 20 3d 20 2b 31 3b c = +1;
1732b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1732c 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b pCur->validNK
1732d 65 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 ey = 1;.
1732e 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 20 pCur->info.nKey
1732f 3d 20 6e 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 = nCellKey;.
17330 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
17331 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 /* The maximum
17332 73 75 70 70 6f 72 74 65 64 20 70 61 67 65 2d 73 supported page-s
17333 69 7a 65 20 69 73 20 33 32 37 36 38 20 62 79 74 ize is 32768 byt
17334 65 73 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 74 es. This means t
17335 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 hat. ** t
17336 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 he maximum numbe
17337 72 20 6f 66 20 72 65 63 6f 72 64 20 62 79 74 65 r of record byte
17338 73 20 73 74 6f 72 65 64 20 6f 6e 20 61 6e 20 69 s stored on an i
17339 6e 64 65 78 20 42 2d 54 72 65 65 0a 20 20 20 20 ndex B-Tree.
1733a 20 20 20 20 2a 2a 20 70 61 67 65 20 69 73 20 61 ** page is a
1733b 74 20 6d 6f 73 74 20 38 31 39 38 20 62 79 74 65 t most 8198 byte
1733c 73 2c 20 77 68 69 63 68 20 6d 61 79 20 62 65 20 s, which may be
1733d 73 74 6f 72 65 64 20 61 73 20 61 20 32 2d 62 79 stored as a 2-by
1733e 74 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 76 61 te. ** va
1733f 72 69 6e 74 2e 20 54 68 69 73 20 69 6e 66 6f 72 rint. This infor
17340 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 mation is used t
17341 6f 20 61 74 74 65 6d 70 74 20 74 6f 20 61 76 6f o attempt to avo
17342 69 64 20 70 61 72 73 69 6e 67 20 0a 20 20 20 20 id parsing .
17343 20 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 69 72 ** the entir
17344 65 20 63 65 6c 6c 20 62 79 20 63 68 65 63 6b 69 e cell by checki
17345 6e 67 20 66 6f 72 20 74 68 65 20 63 61 73 65 73 ng for the cases
17346 20 77 68 65 72 65 20 74 68 65 20 72 65 63 6f 72 where the recor
17347 64 20 69 73 20 0a 20 20 20 20 20 20 20 20 2a 2a d is . **
17348 20 73 74 6f 72 65 64 20 65 6e 74 69 72 65 6c 79 stored entirely
17349 20 77 69 74 68 69 6e 20 74 68 65 20 62 2d 74 72 within the b-tr
1734a 65 65 20 70 61 67 65 20 62 79 20 69 6e 73 70 65 ee page by inspe
1734b 63 74 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 cting the first
1734c 0a 20 20 20 20 20 20 20 20 2a 2a 20 32 20 62 79 . ** 2 by
1734d 74 65 73 20 6f 66 20 74 68 65 20 63 65 6c 6c 2e tes of the cell.
1734e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
1734f 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 int nCell =
17350 70 43 65 6c 6c 5b 30 5d 3b 0a 20 20 20 20 20 20 pCell[0];.
17351 20 20 69 66 28 20 21 28 6e 43 65 6c 6c 20 26 20 if( !(nCell &
17352 30 78 38 30 29 20 26 26 20 6e 43 65 6c 6c 3c 3d 0x80) && nCell<=
17353 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 pPage->maxLocal
17354 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ){. /*
17355 54 68 69 73 20 62 72 61 6e 63 68 20 72 75 6e 73 This branch runs
17356 20 69 66 20 74 68 65 20 72 65 63 6f 72 64 2d 73 if the record-s
17357 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 74 68 65 ize field of the
17358 20 63 65 6c 6c 20 69 73 20 61 0a 20 20 20 20 20 cell is a.
17359 20 20 20 20 20 2a 2a 20 73 69 6e 67 6c 65 20 62 ** single b
1735a 79 74 65 20 76 61 72 69 6e 74 20 61 6e 64 20 74 yte varint and t
1735b 68 65 20 72 65 63 6f 72 64 20 66 69 74 73 20 65 he record fits e
1735c 6e 74 69 72 65 6c 79 20 6f 6e 20 74 68 65 20 6d ntirely on the m
1735d 61 69 6e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a ain. **
1735e 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 20 2a b-tree page. *
1735f 2f 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 /. c =
17360 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 sqlite3VdbeRecor
17361 64 43 6f 6d 70 61 72 65 28 6e 43 65 6c 6c 2c 20 dCompare(nCell,
17362 28 76 6f 69 64 2a 29 26 70 43 65 6c 6c 5b 31 5d (void*)&pCell[1]
17363 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 , pIdxKey);.
17364 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 28 }else if( !(
17365 70 43 65 6c 6c 5b 31 5d 20 26 20 30 78 38 30 29 pCell[1] & 0x80)
17366 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 28 . && (
17367 6e 43 65 6c 6c 20 3d 20 28 28 6e 43 65 6c 6c 26 nCell = ((nCell&
17368 30 78 37 66 29 3c 3c 37 29 20 2b 20 70 43 65 6c 0x7f)<<7) + pCel
17369 6c 5b 31 5d 29 3c 3d 70 50 61 67 65 2d 3e 6d 61 l[1])<=pPage->ma
1736a 78 4c 6f 63 61 6c 0a 20 20 20 20 20 20 20 20 29 xLocal. )
1736b 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 {. /* T
1736c 68 65 20 72 65 63 6f 72 64 2d 73 69 7a 65 20 66 he record-size f
1736d 69 65 6c 64 20 69 73 20 61 20 32 20 62 79 74 65 ield is a 2 byte
1736e 20 76 61 72 69 6e 74 20 61 6e 64 20 74 68 65 20 varint and the
1736f 72 65 63 6f 72 64 20 0a 20 20 20 20 20 20 20 20 record .
17370 20 20 2a 2a 20 66 69 74 73 20 65 6e 74 69 72 65 ** fits entire
17371 6c 79 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 62 ly on the main b
17372 2d 74 72 65 65 20 70 61 67 65 2e 20 20 2a 2f 0a -tree page. */.
17373 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 c = sq
17374 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 lite3VdbeRecordC
17375 6f 6d 70 61 72 65 28 6e 43 65 6c 6c 2c 20 28 76 ompare(nCell, (v
17376 6f 69 64 2a 29 26 70 43 65 6c 6c 5b 32 5d 2c 20 oid*)&pCell[2],
17377 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 20 20 pIdxKey);.
17378 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
17379 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 64 /* The record
1737a 20 66 6c 6f 77 73 20 6f 76 65 72 20 6f 6e 74 6f flows over onto
1737b 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65 one or more ove
1737c 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 49 6e 0a rflow pages. In.
1737d 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69 ** thi
1737e 73 20 63 61 73 65 20 74 68 65 20 77 68 6f 6c 65 s case the whole
1737f 20 63 65 6c 6c 20 6e 65 65 64 73 20 74 6f 20 62 cell needs to b
17380 65 20 70 61 72 73 65 64 2c 20 61 20 62 75 66 66 e parsed, a buff
17381 65 72 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20 er allocated.
17382 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 61 63 ** and ac
17383 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 75 73 cessPayload() us
17384 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 ed to retrieve t
17385 68 65 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74 he record into t
17386 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 he. **
17387 62 75 66 66 65 72 20 62 65 66 6f 72 65 20 56 64 buffer before Vd
17388 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 beRecordCompare(
17389 29 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 2e ) can be called.
1738a 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 76 6f */. vo
1738b 69 64 20 2a 70 43 65 6c 6c 4b 65 79 3b 0a 20 20 id *pCellKey;.
1738c 20 20 20 20 20 20 20 20 75 38 20 2a 20 63 6f 6e u8 * con
1738d 73 74 20 70 43 65 6c 6c 42 6f 64 79 20 3d 20 70 st pCellBody = p
1738e 43 65 6c 6c 20 2d 20 70 50 61 67 65 2d 3e 63 68 Cell - pPage->ch
1738f 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 20 20 ildPtrSize;.
17390 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73 65 btreeParse
17391 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 CellPtr(pPage, p
17392 43 65 6c 6c 42 6f 64 79 2c 20 26 70 43 75 72 2d CellBody, &pCur-
17393 3e 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 >info);.
17394 20 20 6e 43 65 6c 6c 20 3d 20 28 69 6e 74 29 70 nCell = (int)p
17395 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a Cur->info.nKey;.
17396 20 20 20 20 20 20 20 20 20 20 70 43 65 6c 6c 4b pCellK
17397 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c ey = sqlite3Mall
17398 6f 63 28 20 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 oc( nCell );.
17399 20 20 20 20 20 20 20 69 66 28 20 70 43 65 6c 6c if( pCell
1739a 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Key==0 ){.
1739b 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1739c 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 E_NOMEM;.
1739d 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f goto moveto
1739e 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 _finish;.
1739f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72 }. r
173a0 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c 6f 61 c = accessPayloa
173a1 64 28 70 43 75 72 2c 20 30 2c 20 6e 43 65 6c 6c d(pCur, 0, nCell
173a2 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 , (unsigned char
173a3 2a 29 70 43 65 6c 6c 4b 65 79 2c 20 30 29 3b 0a *)pCellKey, 0);.
173a4 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
173a5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
173a6 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 65 sqlite3_free(pCe
173a7 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 llKey);.
173a8 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f goto moveto_
173a9 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 20 finish;.
173aa 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 63 20 }. c
173ab 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 = sqlite3VdbeRec
173ac 6f 72 64 43 6f 6d 70 61 72 65 28 6e 43 65 6c 6c ordCompare(nCell
173ad 2c 20 70 43 65 6c 6c 4b 65 79 2c 20 70 49 64 78 , pCellKey, pIdx
173ae 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 Key);.
173af 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 65 sqlite3_free(pCe
173b0 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 llKey);.
173b1 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
173b2 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 if( c==0 ){.
173b3 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 if( pPage->i
173b4 6e 74 4b 65 79 20 26 26 20 21 70 50 61 67 65 2d ntKey && !pPage-
173b5 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 20 >leaf ){.
173b6 20 20 20 6c 77 72 20 3d 20 69 64 78 3b 0a 20 20 lwr = idx;.
173b7 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 6c 77 upr = lw
173b8 72 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 r - 1;.
173b9 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
173ba 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
173bb 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 *pRes = 0;.
173bc 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
173bd 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 E_OK;.
173be 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 goto moveto_fini
173bf 73 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 sh;. }.
173c0 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
173c1 63 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6c c<0 ){. l
173c2 77 72 20 3d 20 69 64 78 2b 31 3b 0a 20 20 20 20 wr = idx+1;.
173c3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
173c4 20 75 70 72 20 3d 20 69 64 78 2d 31 3b 0a 20 20 upr = idx-1;.
173c5 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
173c6 6c 77 72 3e 75 70 72 20 29 7b 0a 20 20 20 20 20 lwr>upr ){.
173c7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
173c8 7d 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 }. pCur->ai
173c9 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
173ca 20 3d 20 28 75 31 36 29 28 28 6c 77 72 2b 75 70 = (u16)((lwr+up
173cb 72 29 2f 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 r)/2);. }.
173cc 20 61 73 73 65 72 74 28 20 6c 77 72 3d 3d 75 70 assert( lwr==up
173cd 72 2b 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 r+1 );. asser
173ce 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 t( pPage->isInit
173cf 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 );. if( pPag
173d0 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 e->leaf ){.
173d1 20 63 68 6c 64 50 67 20 3d 20 30 3b 0a 20 20 20 chldPg = 0;.
173d2 20 7d 65 6c 73 65 20 69 66 28 20 6c 77 72 3e 3d }else if( lwr>=
173d3 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a pPage->nCell ){.
173d4 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67 chldPg = g
173d5 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e et4byte(&pPage->
173d6 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 aData[pPage->hdr
173d7 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 Offset+8]);.
173d8 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 68 6c }else{. chl
173d9 64 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 66 dPg = get4byte(f
173da 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 6c indCell(pPage, l
173db 77 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 wr));. }.
173dc 69 66 28 20 63 68 6c 64 50 67 3d 3d 30 20 29 7b if( chldPg==0 ){
173dd 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
173de 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
173df 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 >iPage]<pCur->ap
173e0 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
173e1 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 ]->nCell );.
173e2 20 20 2a 70 52 65 73 20 3d 20 63 3b 0a 20 20 20 *pRes = c;.
173e3 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
173e4 4b 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f K;. goto mo
173e5 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 veto_finish;.
173e6 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 }. pCur->aiI
173e7 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 dx[pCur->iPage]
173e8 3d 20 28 75 31 36 29 6c 77 72 3b 0a 20 20 20 20 = (u16)lwr;.
173e9 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
173ea 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e = 0;. pCur->
173eb 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 validNKey = 0;.
173ec 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 rc = moveToCh
173ed 69 6c 64 28 70 43 75 72 2c 20 63 68 6c 64 50 67 ild(pCur, chldPg
173ee 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 );. if( rc )
173ef 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 goto moveto_fini
173f0 73 68 3b 0a 20 20 7d 0a 6d 6f 76 65 74 6f 5f 66 sh;. }.moveto_f
173f1 69 6e 69 73 68 3a 0a 20 20 72 65 74 75 72 6e 20 inish:. return
173f2 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}.../*.** Re
173f3 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 turn TRUE if the
173f4 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 70 cursor is not p
173f5 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e ointing at an en
173f6 74 72 79 20 6f 66 20 74 68 65 20 74 61 62 6c 65 try of the table
173f7 2e 0a 2a 2a 0a 2a 2a 20 54 52 55 45 20 77 69 6c ..**.** TRUE wil
173f8 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 61 66 l be returned af
173f9 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 ter a call to sq
173fa 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 29 lite3BtreeNext()
173fb 20 6d 6f 76 65 73 0a 2a 2a 20 70 61 73 74 20 74 moves.** past t
173fc 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e he last entry in
173fd 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 73 71 the table or sq
173fe 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 28 29 lite3BtreePrev()
173ff 20 6d 6f 76 65 73 20 70 61 73 74 0a 2a 2a 20 74 moves past.** t
17400 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 2e 20 he first entry.
17401 20 54 52 55 45 20 69 73 20 61 6c 73 6f 20 72 65 TRUE is also re
17402 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 74 61 turned if the ta
17403 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f ble is empty..*/
17404 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
17405 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
17406 45 6f 66 28 42 74 43 75 72 73 6f 72 20 2a 70 43 Eof(BtCursor *pC
17407 75 72 29 7b 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 ur){. /* TODO:
17408 57 68 61 74 20 69 66 20 74 68 65 20 63 75 72 73 What if the curs
17409 6f 72 20 69 73 20 69 6e 20 43 55 52 53 4f 52 5f or is in CURSOR_
1740a 52 45 51 55 49 52 45 53 45 45 4b 20 62 75 74 20 REQUIRESEEK but
1740b 61 6c 6c 20 74 61 62 6c 65 20 65 6e 74 72 69 65 all table entrie
1740c 73 0a 20 20 2a 2a 20 68 61 76 65 20 62 65 65 6e s. ** have been
1740d 20 64 65 6c 65 74 65 64 3f 20 54 68 69 73 20 41 deleted? This A
1740e 50 49 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 PI will need to
1740f 63 68 61 6e 67 65 20 74 6f 20 72 65 74 75 72 6e change to return
17410 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 0a 20 an error code.
17411 20 2a 2a 20 61 73 20 77 65 6c 6c 20 61 73 20 74 ** as well as t
17412 68 65 20 62 6f 6f 6c 65 61 6e 20 72 65 73 75 6c he boolean resul
17413 74 20 76 61 6c 75 65 2e 0a 20 20 2a 2f 0a 20 20 t value.. */.
17414 72 65 74 75 72 6e 20 28 43 55 52 53 4f 52 5f 56 return (CURSOR_V
17415 41 4c 49 44 21 3d 70 43 75 72 2d 3e 65 53 74 61 ALID!=pCur->eSta
17416 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 te);.}../*.** Ad
17417 76 61 6e 63 65 20 74 68 65 20 63 75 72 73 6f 72 vance the cursor
17418 20 74 6f 20 74 68 65 20 6e 65 78 74 20 65 6e 74 to the next ent
17419 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ry in the databa
1741a 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63 65 se. If.** succe
1741b 73 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20 2a ssful then set *
1741c 70 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65 20 pRes=0. If the
1741d 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61 6c cursor.** was al
1741e 72 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20 74 ready pointing t
1741f 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 o the last entry
17420 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
17421 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73 20 before.** this
17422 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c routine was call
17423 65 64 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 ed, then set *pR
17424 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f es=1..*/.SQLITE_
17425 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
17426 74 65 33 42 74 72 65 65 4e 65 78 74 28 42 74 43 te3BtreeNext(BtC
17427 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 ursor *pCur, int
17428 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 *pRes){. int r
17429 63 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 c;. int idx;.
1742a 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a MemPage *pPage;.
1742b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
1742c 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
1742d 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 ) );. rc = rest
1742e 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
1742f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 n(pCur);. if( r
17430 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
17431 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
17432 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 52 65 }. assert( pRe
17433 73 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 43 55 s!=0 );. if( CU
17434 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 RSOR_INVALID==pC
17435 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 ur->eState ){.
17436 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 *pRes = 1;.
17437 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
17438 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75 K;. }. if( pCu
17439 72 2d 3e 73 6b 69 70 4e 65 78 74 3e 30 20 29 7b r->skipNext>0 ){
1743a 0a 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e . pCur->skipN
1743b 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 2a 70 52 ext = 0;. *pR
1743c 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 es = 0;. retu
1743d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
1743e 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 }. pCur->skipNe
1743f 78 74 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 65 xt = 0;.. pPage
17440 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
17441 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 pCur->iPage];.
17442 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d 3e 61 69 idx = ++pCur->ai
17443 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
17444 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
17445 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 e->isInit );. a
17446 73 73 65 72 74 28 20 69 64 78 3c 3d 70 50 61 67 ssert( idx<=pPag
17447 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 70 e->nCell );.. p
17448 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 Cur->info.nSize
17449 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c = 0;. pCur->val
1744a 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 idNKey = 0;. if
1744b 28 20 69 64 78 3e 3d 70 50 61 67 65 2d 3e 6e 43 ( idx>=pPage->nC
1744c 65 6c 6c 20 29 7b 0a 20 20 20 20 69 66 28 20 21 ell ){. if( !
1744d 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
1744e 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f rc = moveTo
1744f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34 Child(pCur, get4
17450 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 byte(&pPage->aDa
17451 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 ta[pPage->hdrOff
17452 73 65 74 2b 38 5d 29 29 3b 0a 20 20 20 20 20 20 set+8]));.
17453 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
17454 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d rc;. rc = m
17455 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 oveToLeftmost(pC
17456 75 72 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 ur);. *pRes
17457 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 = 0;. retu
17458 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
17459 20 64 6f 7b 0a 20 20 20 20 20 20 69 66 28 20 70 do{. if( p
1745a 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b Cur->iPage==0 ){
1745b 0a 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d . *pRes =
1745c 20 31 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 1;. pCur
1745d 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f ->eState = CURSO
1745e 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 R_INVALID;.
1745f 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
17460 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 _OK;. }.
17461 20 20 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 moveToParent(
17462 70 43 75 72 29 3b 0a 20 20 20 20 20 20 70 50 61 pCur);. pPa
17463 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
17464 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a e[pCur->iPage];.
17465 20 20 20 20 7d 77 68 69 6c 65 28 20 70 43 75 72 }while( pCur
17466 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
17467 61 67 65 5d 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 age]>=pPage->nCe
17468 6c 6c 20 29 3b 0a 20 20 20 20 2a 70 52 65 73 20 ll );. *pRes
17469 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 50 61 = 0;. if( pPa
1746a 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 ge->intKey ){.
1746b 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1746c 42 74 72 65 65 4e 65 78 74 28 70 43 75 72 2c 20 BtreeNext(pCur,
1746d 70 52 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 pRes);. }else
1746e 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
1746f 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 ITE_OK;. }.
17470 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
17471 0a 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 . *pRes = 0;.
17472 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 if( pPage->leaf
17473 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
17474 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 LITE_OK;. }. r
17475 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f c = moveToLeftmo
17476 73 74 28 70 43 75 72 29 3b 0a 20 20 72 65 74 75 st(pCur);. retu
17477 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a rn rc;.}.../*.**
17478 20 53 74 65 70 20 74 68 65 20 63 75 72 73 6f 72 Step the cursor
17479 20 74 6f 20 74 68 65 20 62 61 63 6b 20 74 6f 20 to the back to
1747a 74 68 65 20 70 72 65 76 69 6f 75 73 20 65 6e 74 the previous ent
1747b 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ry in the databa
1747c 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63 65 se. If.** succe
1747d 73 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20 2a ssful then set *
1747e 70 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65 20 pRes=0. If the
1747f 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61 6c cursor.** was al
17480 72 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20 74 ready pointing t
17481 6f 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 o the first entr
17482 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 y in the databas
17483 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73 e before.** this
17484 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c routine was cal
17485 6c 65 64 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 led, then set *p
17486 52 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 Res=1..*/.SQLITE
17487 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
17488 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 ite3BtreePreviou
17489 73 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 s(BtCursor *pCur
1748a 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 , int *pRes){.
1748b 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 int rc;. MemPag
1748c 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 e *pPage;.. ass
1748d 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
1748e 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
1748f 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 rc = restoreCur
17490 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 sorPosition(pCur
17491 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
17492 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 ITE_OK ){. re
17493 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 turn rc;. }. p
17494 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b Cur->atLast = 0;
17495 0a 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e . if( CURSOR_IN
17496 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 VALID==pCur->eSt
17497 61 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 ate ){. *pRes
17498 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e = 1;. return
17499 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
1749a 20 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 if( pCur->skip
1749b 4e 65 78 74 3c 30 20 29 7b 0a 20 20 20 20 70 43 Next<0 ){. pC
1749c 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 ur->skipNext = 0
1749d 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b ;. *pRes = 0;
1749e 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1749f 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 TE_OK;. }. pCu
174a0 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b r->skipNext = 0;
174a1 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 .. pPage = pCur
174a2 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
174a3 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 Page];. assert(
174a4 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 pPage->isInit )
174a5 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e ;. if( !pPage->
174a6 6c 65 61 66 20 29 7b 0a 20 20 20 20 69 6e 74 20 leaf ){. int
174a7 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 idx = pCur->aiId
174a8 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a x[pCur->iPage];.
174a9 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 rc = moveToC
174aa 68 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62 hild(pCur, get4b
174ab 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 yte(findCell(pPa
174ac 67 65 2c 20 69 64 78 29 29 29 3b 0a 20 20 20 20 ge, idx)));.
174ad 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
174ae 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
174af 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f . rc = moveTo
174b0 52 69 67 68 74 6d 6f 73 74 28 70 43 75 72 29 3b Rightmost(pCur);
174b1 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68 . }else{. wh
174b2 69 6c 65 28 20 70 43 75 72 2d 3e 61 69 49 64 78 ile( pCur->aiIdx
174b3 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3d 3d 30 [pCur->iPage]==0
174b4 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 ){. if( pC
174b5 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a ur->iPage==0 ){.
174b6 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 pCur->eS
174b7 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e tate = CURSOR_IN
174b8 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 20 20 2a VALID;. *
174b9 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 pRes = 1;.
174ba 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
174bb 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 OK;. }.
174bc 20 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 moveToParent(p
174bd 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Cur);. }.
174be 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
174bf 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e = 0;. pCur->
174c0 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 0a validNKey = 0;..
174c1 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b pCur->aiIdx[
174c2 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 2d 3b 0a pCur->iPage]--;.
174c3 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 pPage = pCur
174c4 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
174c5 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 70 Page];. if( p
174c6 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 Page->intKey &&
174c7 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
174c8 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
174c9 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 e3BtreePrevious(
174ca 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 20 pCur, pRes);.
174cb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 }else{. rc
174cc 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
174cd 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 65 73 20 }. }. *pRes
174ce 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 = 0;. return rc
174cf 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 ;.}../*.** Alloc
174d0 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 66 ate a new page f
174d1 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
174d2 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 file..**.** The
174d3 20 6e 65 77 20 70 61 67 65 20 69 73 20 6d 61 72 new page is mar
174d4 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 20 28 ked as dirty. (
174d5 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
174d6 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
174d7 65 28 29 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 e().** has alrea
174d8 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 6f dy been called o
174d9 6e 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e 29 n the new page.)
174da 20 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 68 The new page h
174db 61 73 20 61 6c 73 6f 0a 2a 2a 20 62 65 65 6e 20 as also.** been
174dc 72 65 66 65 72 65 6e 63 65 64 20 61 6e 64 20 74 referenced and t
174dd 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 he calling routi
174de 6e 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c ne is responsibl
174df 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67 0a 2a 2a e for calling.**
174e0 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
174e1 65 66 28 29 20 6f 6e 20 74 68 65 20 6e 65 77 20 ef() on the new
174e2 70 61 67 65 20 77 68 65 6e 20 69 74 20 69 73 20 page when it is
174e3 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 done..**.** SQLI
174e4 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
174e5 64 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 41 d on success. A
174e6 6e 79 20 6f 74 68 65 72 20 72 65 74 75 72 6e 20 ny other return
174e7 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 0a value indicates.
174e8 2a 2a 20 61 6e 20 65 72 72 6f 72 2e 20 20 2a 70 ** an error. *p
174e9 70 50 61 67 65 20 61 6e 64 20 2a 70 50 67 6e 6f pPage and *pPgno
174ea 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20 69 are undefined i
174eb 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 n the event of a
174ec 6e 20 65 72 72 6f 72 2e 0a 2a 2a 20 44 6f 20 6e n error..** Do n
174ed 6f 74 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 ot invoke sqlite
174ee 33 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 3PagerUnref() on
174ef 20 2a 70 70 50 61 67 65 20 69 66 20 61 6e 20 65 *ppPage if an e
174f0 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 rror is returned
174f1 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 ..**.** If the "
174f2 6e 65 61 72 62 79 22 20 70 61 72 61 6d 65 74 65 nearby" paramete
174f3 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e r is not 0, then
174f4 20 61 20 28 66 65 65 62 6c 65 29 20 65 66 66 6f a (feeble) effo
174f5 72 74 20 69 73 20 6d 61 64 65 20 74 6f 20 0a 2a rt is made to .*
174f6 2a 20 6c 6f 63 61 74 65 20 61 20 70 61 67 65 20 * locate a page
174f7 63 6c 6f 73 65 20 74 6f 20 74 68 65 20 70 61 67 close to the pag
174f8 65 20 6e 75 6d 62 65 72 20 22 6e 65 61 72 62 79 e number "nearby
174f9 22 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 ". This can be
174fa 75 73 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 61 74 used in an.** at
174fb 74 65 6d 70 74 20 74 6f 20 6b 65 65 70 20 72 65 tempt to keep re
174fc 6c 61 74 65 64 20 70 61 67 65 73 20 63 6c 6f 73 lated pages clos
174fd 65 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72 20 e to each other
174fe 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
174ff 66 69 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 69 file,.** which i
17500 6e 20 74 75 72 6e 20 63 61 6e 20 6d 61 6b 65 20 n turn can make
17501 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20 database access
17502 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 faster..**.** If
17503 20 74 68 65 20 22 65 78 61 63 74 22 20 70 61 72 the "exact" par
17504 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c ameter is not 0,
17505 20 61 6e 64 20 74 68 65 20 70 61 67 65 2d 6e 75 and the page-nu
17506 6d 62 65 72 20 6e 65 61 72 62 79 20 65 78 69 73 mber nearby exis
17507 74 73 20 0a 2a 2a 20 61 6e 79 77 68 65 72 65 20 ts .** anywhere
17508 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 on the free-list
17509 2c 20 74 68 65 6e 20 69 74 20 69 73 20 67 75 61 , then it is gua
1750a 72 65 6e 74 65 65 64 20 74 6f 20 62 65 20 72 65 renteed to be re
1750b 74 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a 2a 20 turned. This.**
1750c 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 is only used by
1750d 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 auto-vacuum data
1750e 62 61 73 65 73 20 77 68 65 6e 20 61 6c 6c 6f 63 bases when alloc
1750f 61 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62 6c ating a new tabl
17510 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
17511 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 allocateBtreePa
17512 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a ge(. BtShared *
17513 70 42 74 2c 20 0a 20 20 4d 65 6d 50 61 67 65 20 pBt, . MemPage
17514 2a 2a 70 70 50 61 67 65 2c 20 0a 20 20 50 67 6e **ppPage, . Pgn
17515 6f 20 2a 70 50 67 6e 6f 2c 20 0a 20 20 50 67 6e o *pPgno, . Pgn
17516 6f 20 6e 65 61 72 62 79 2c 0a 20 20 75 38 20 65 o nearby,. u8 e
17517 78 61 63 74 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 xact.){. MemPag
17518 65 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e 74 e *pPage1;. int
17519 20 72 63 3b 0a 20 20 75 33 32 20 6e 3b 20 20 20 rc;. u32 n;
1751a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 /* Number of p
1751b 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 ages on the free
1751c 6c 69 73 74 20 2a 2f 0a 20 20 75 33 32 20 6b 3b list */. u32 k;
1751d 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1751e 66 20 6c 65 61 76 65 73 20 6f 6e 20 74 68 65 20 f leaves on the
1751f 74 72 75 6e 6b 20 6f 66 20 74 68 65 20 66 72 65 trunk of the fre
17520 65 6c 69 73 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 elist */. MemPa
17521 67 65 20 2a 70 54 72 75 6e 6b 20 3d 20 30 3b 0a ge *pTrunk = 0;.
17522 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 72 65 76 MemPage *pPrev
17523 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 50 67 6e Trunk = 0;. Pgn
17524 6f 20 6d 78 50 61 67 65 3b 20 20 20 20 20 2f 2a o mxPage; /*
17525 20 54 6f 74 61 6c 20 73 69 7a 65 20 6f 66 20 74 Total size of t
17526 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
17527 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 */.. assert( s
17528 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
17529 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
1752a 0a 20 20 70 50 61 67 65 31 20 3d 20 70 42 74 2d . pPage1 = pBt-
1752b 3e 70 50 61 67 65 31 3b 0a 20 20 6d 78 50 61 67 >pPage1;. mxPag
1752c 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75 e = pagerPagecou
1752d 6e 74 28 70 42 74 29 3b 0a 20 20 6e 20 3d 20 67 nt(pBt);. n = g
1752e 65 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d et4byte(&pPage1-
1752f 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 74 >aData[36]);. t
17530 65 73 74 63 61 73 65 28 20 6e 3d 3d 6d 78 50 61 estcase( n==mxPa
17531 67 65 2d 31 20 29 3b 0a 20 20 69 66 28 20 6e 3e ge-1 );. if( n>
17532 3d 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 20 72 =mxPage ){. r
17533 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
17534 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 RUPT_BKPT;. }.
17535 20 69 66 28 20 6e 3e 30 20 29 7b 0a 20 20 20 20 if( n>0 ){.
17536 2f 2a 20 54 68 65 72 65 20 61 72 65 20 70 61 67 /* There are pag
17537 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 es on the freeli
17538 73 74 2e 20 20 52 65 75 73 65 20 6f 6e 65 20 6f st. Reuse one o
17539 66 20 74 68 6f 73 65 20 70 61 67 65 73 2e 20 2a f those pages. *
1753a 2f 0a 20 20 20 20 50 67 6e 6f 20 69 54 72 75 6e /. Pgno iTrun
1753b 6b 3b 0a 20 20 20 20 75 38 20 73 65 61 72 63 68 k;. u8 search
1753c 4c 69 73 74 20 3d 20 30 3b 20 2f 2a 20 49 66 20 List = 0; /* If
1753d 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 6d 75 the free-list mu
1753e 73 74 20 62 65 20 73 65 61 72 63 68 65 64 20 66 st be searched f
1753f 6f 72 20 27 6e 65 61 72 62 79 27 20 2a 2f 0a 20 or 'nearby' */.
17540 20 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 . /* If th
17541 65 20 27 65 78 61 63 74 27 20 70 61 72 61 6d 65 e 'exact' parame
17542 74 65 72 20 77 61 73 20 74 72 75 65 20 61 6e 64 ter was true and
17543 20 61 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 a query of the
17544 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 pointer-map.
17545 2a 2a 20 73 68 6f 77 73 20 74 68 61 74 20 74 68 ** shows that th
17546 65 20 70 61 67 65 20 27 6e 65 61 72 62 79 27 20 e page 'nearby'
17547 69 73 20 73 6f 6d 65 77 68 65 72 65 20 6f 6e 20 is somewhere on
17548 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 74 the free-list, t
17549 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 65 hen. ** the e
1754a 6e 74 69 72 65 2d 6c 69 73 74 20 77 69 6c 6c 20 ntire-list will
1754b 62 65 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 be searched for
1754c 74 68 61 74 20 70 61 67 65 2e 0a 20 20 20 20 2a that page.. *
1754d 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1754e 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
1754f 0a 20 20 20 20 69 66 28 20 65 78 61 63 74 20 26 . if( exact &
17550 26 20 6e 65 61 72 62 79 3c 3d 6d 78 50 61 67 65 & nearby<=mxPage
17551 20 29 7b 0a 20 20 20 20 20 20 75 38 20 65 54 79 ){. u8 eTy
17552 70 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 pe;. assert
17553 28 20 6e 65 61 72 62 79 3e 30 20 29 3b 0a 20 20 ( nearby>0 );.
17554 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d assert( pBt-
17555 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 >autoVacuum );.
17556 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 rc = ptrmap
17557 47 65 74 28 70 42 74 2c 20 6e 65 61 72 62 79 2c Get(pBt, nearby,
17558 20 26 65 54 79 70 65 2c 20 30 29 3b 0a 20 20 20 &eType, 0);.
17559 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
1755a 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 69 66 28 rn rc;. if(
1755b 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 eType==PTRMAP_F
1755c 52 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 REEPAGE ){.
1755d 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 searchList =
1755e 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 1;. }.
1755f 20 2a 70 50 67 6e 6f 20 3d 20 6e 65 61 72 62 79 *pPgno = nearby
17560 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a ;. }.#endif..
17561 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 /* Decrement
17562 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 63 the free-list c
17563 6f 75 6e 74 20 62 79 20 31 2e 20 53 65 74 20 69 ount by 1. Set i
17564 54 72 75 6e 6b 20 74 6f 20 74 68 65 20 69 6e 64 Trunk to the ind
17565 65 78 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a ex of the. **
17566 20 66 69 72 73 74 20 66 72 65 65 2d 6c 69 73 74 first free-list
17567 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 69 50 72 trunk page. iPr
17568 65 76 54 72 75 6e 6b 20 69 73 20 69 6e 69 74 69 evTrunk is initi
17569 61 6c 6c 79 20 31 2e 0a 20 20 20 20 2a 2f 0a 20 ally 1.. */.
1756a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
1756b 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 31 agerWrite(pPage1
1756c 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
1756d 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
1756e 72 63 3b 0a 20 20 20 20 70 75 74 34 62 79 74 65 rc;. put4byte
1756f 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b (&pPage1->aData[
17570 33 36 5d 2c 20 6e 2d 31 29 3b 0a 0a 20 20 20 20 36], n-1);..
17571 2f 2a 20 54 68 65 20 63 6f 64 65 20 77 69 74 68 /* The code with
17572 69 6e 20 74 68 69 73 20 6c 6f 6f 70 20 69 73 20 in this loop is
17573 72 75 6e 20 6f 6e 6c 79 20 6f 6e 63 65 20 69 66 run only once if
17574 20 74 68 65 20 27 73 65 61 72 63 68 4c 69 73 74 the 'searchList
17575 27 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20 2a ' variable. *
17576 2a 20 69 73 20 6e 6f 74 20 74 72 75 65 2e 20 4f * is not true. O
17577 74 68 65 72 77 69 73 65 2c 20 69 74 20 72 75 6e therwise, it run
17578 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 s once for each
17579 74 72 75 6e 6b 2d 70 61 67 65 20 6f 6e 20 74 68 trunk-page on th
1757a 65 0a 20 20 20 20 2a 2a 20 66 72 65 65 2d 6c 69 e. ** free-li
1757b 73 74 20 75 6e 74 69 6c 20 74 68 65 20 70 61 67 st until the pag
1757c 65 20 27 6e 65 61 72 62 79 27 20 69 73 20 6c 6f e 'nearby' is lo
1757d 63 61 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 cated.. */.
1757e 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 70 50 72 do {. pPr
1757f 65 76 54 72 75 6e 6b 20 3d 20 70 54 72 75 6e 6b evTrunk = pTrunk
17580 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 72 65 ;. if( pPre
17581 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 vTrunk ){.
17582 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 iTrunk = get4b
17583 79 74 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d yte(&pPrevTrunk-
17584 3e 61 44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 >aData[0]);.
17585 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
17586 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 iTrunk = get4by
17587 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 te(&pPage1->aDat
17588 61 5b 33 32 5d 29 3b 0a 20 20 20 20 20 20 7d 0a a[32]);. }.
17589 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1758a 69 54 72 75 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 iTrunk==mxPage )
1758b 3b 0a 20 20 20 20 20 20 69 66 28 20 69 54 72 75 ;. if( iTru
1758c 6e 6b 3e 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 nk>mxPage ){.
1758d 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1758e 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
1758f 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
17590 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 rc = btreeGe
17591 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 75 6e tPage(pBt, iTrun
17592 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b 0a k, &pTrunk, 0);.
17593 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
17594 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 ( rc ){.
17595 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 pTrunk = 0;.
17596 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c goto end_all
17597 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 ocate_page;.
17598 20 20 7d 0a 0a 20 20 20 20 20 20 6b 20 3d 20 67 }.. k = g
17599 65 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d et4byte(&pTrunk-
1759a 3e 61 44 61 74 61 5b 34 5d 29 3b 0a 20 20 20 20 >aData[4]);.
1759b 20 20 69 66 28 20 6b 3d 3d 30 20 26 26 20 21 73 if( k==0 && !s
1759c 65 61 72 63 68 4c 69 73 74 20 29 7b 0a 20 20 20 earchList ){.
1759d 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 75 6e /* The trun
1759e 6b 20 68 61 73 20 6e 6f 20 6c 65 61 76 65 73 20 k has no leaves
1759f 61 6e 64 20 74 68 65 20 6c 69 73 74 20 69 73 20 and the list is
175a0 6e 6f 74 20 62 65 69 6e 67 20 73 65 61 72 63 68 not being search
175a1 65 64 2e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 ed. . **
175a2 53 6f 20 65 78 74 72 61 63 74 20 74 68 65 20 74 So extract the t
175a3 72 75 6e 6b 20 70 61 67 65 20 69 74 73 65 6c 66 runk page itself
175a4 20 61 6e 64 20 75 73 65 20 69 74 20 61 73 20 74 and use it as t
175a5 68 65 20 6e 65 77 6c 79 20 0a 20 20 20 20 20 20 he newly .
175a6 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70 ** allocated p
175a7 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 age */. a
175a8 73 73 65 72 74 28 20 70 50 72 65 76 54 72 75 6e ssert( pPrevTrun
175a9 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 k==0 );.
175aa 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
175ab 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 rWrite(pTrunk->p
175ac 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
175ad 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
175ae 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c goto end_al
175af 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 locate_page;.
175b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a }. *
175b1 70 50 67 6e 6f 20 3d 20 69 54 72 75 6e 6b 3b 0a pPgno = iTrunk;.
175b2 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
175b3 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 pPage1->aData[32
175b4 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 ], &pTrunk->aDat
175b5 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 a[0], 4);.
175b6 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54 72 75 *ppPage = pTru
175b7 6e 6b 3b 0a 20 20 20 20 20 20 20 20 70 54 72 75 nk;. pTru
175b8 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 nk = 0;.
175b9 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 TRACE(("ALLOCATE
175ba 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 25 64 20 : %d trunk - %d
175bb 66 72 65 65 20 70 61 67 65 73 20 6c 65 66 74 5c free pages left\
175bc 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29 n", *pPgno, n-1)
175bd 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 );. }else i
175be 66 28 20 6b 3e 28 75 33 32 29 28 70 42 74 2d 3e f( k>(u32)(pBt->
175bf 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 usableSize/4 - 2
175c0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 ) ){. /*
175c1 56 61 6c 75 65 20 6f 66 20 6b 20 69 73 20 6f 75 Value of k is ou
175c2 74 20 6f 66 20 72 61 6e 67 65 2e 20 20 44 61 74 t of range. Dat
175c3 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e abase corruption
175c4 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d */. rc =
175c5 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
175c6 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 67 6f BKPT;. go
175c7 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f to end_allocate_
175c8 70 61 67 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 page;.#ifndef SQ
175c9 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
175ca 43 55 55 4d 0a 20 20 20 20 20 20 7d 65 6c 73 65 CUUM. }else
175cb 20 69 66 28 20 73 65 61 72 63 68 4c 69 73 74 20 if( searchList
175cc 26 26 20 6e 65 61 72 62 79 3d 3d 69 54 72 75 6e && nearby==iTrun
175cd 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 k ){. /*
175ce 54 68 65 20 6c 69 73 74 20 69 73 20 62 65 69 6e The list is bein
175cf 67 20 73 65 61 72 63 68 65 64 20 61 6e 64 20 74 g searched and t
175d0 68 69 73 20 74 72 75 6e 6b 20 70 61 67 65 20 69 his trunk page i
175d1 73 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 s the page.
175d2 20 20 20 2a 2a 20 74 6f 20 61 6c 6c 6f 63 61 74 ** to allocat
175d3 65 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 e, regardless of
175d4 20 77 68 65 74 68 65 72 20 69 74 20 68 61 73 20 whether it has
175d5 6c 65 61 76 65 73 2e 0a 20 20 20 20 20 20 20 20 leaves..
175d6 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 */. asser
175d7 74 28 20 2a 70 50 67 6e 6f 3d 3d 69 54 72 75 6e t( *pPgno==iTrun
175d8 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 k );. *pp
175d9 50 61 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 Page = pTrunk;.
175da 20 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 searchLis
175db 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72 t = 0;. r
175dc 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
175dd 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 Write(pTrunk->pD
175de 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
175df 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
175e0 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c goto end_all
175e1 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 ocate_page;.
175e2 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
175e3 28 20 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ( k==0 ){.
175e4 20 20 20 20 69 66 28 20 21 70 50 72 65 76 54 72 if( !pPrevTr
175e5 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 unk ){.
175e6 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 memcpy(&pPage
175e7 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 1->aData[32], &p
175e8 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c Trunk->aData[0],
175e9 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 4);. }
175ea 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
175eb 20 20 6d 65 6d 63 70 79 28 26 70 50 72 65 76 54 memcpy(&pPrevT
175ec 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 runk->aData[0],
175ed 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 &pTrunk->aData[0
175ee 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 ], 4);.
175ef 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
175f0 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 {. /* T
175f1 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 he trunk page is
175f2 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 required by the
175f3 20 63 61 6c 6c 65 72 20 62 75 74 20 69 74 20 63 caller but it c
175f4 6f 6e 74 61 69 6e 73 20 0a 20 20 20 20 20 20 20 ontains .
175f5 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 ** pointers t
175f6 6f 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 76 o free-list leav
175f7 65 73 2e 20 54 68 65 20 66 69 72 73 74 20 6c 65 es. The first le
175f8 61 66 20 62 65 63 6f 6d 65 73 20 61 20 74 72 75 af becomes a tru
175f9 6e 6b 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 nk. **
175fa 70 61 67 65 20 69 6e 20 74 68 69 73 20 63 61 73 page in this cas
175fb 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a e.. */.
175fc 20 20 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 MemPag
175fd 65 20 2a 70 4e 65 77 54 72 75 6e 6b 3b 0a 20 20 e *pNewTrunk;.
175fe 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 4e 65 Pgno iNe
175ff 77 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 wTrunk = get4byt
17600 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 e(&pTrunk->aData
17601 5b 38 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 [8]);.
17602 69 66 28 20 69 4e 65 77 54 72 75 6e 6b 3e 6d 78 if( iNewTrunk>mx
17603 50 61 67 65 20 29 7b 20 0a 20 20 20 20 20 20 20 Page ){ .
17604 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
17605 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
17606 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 goto
17607 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 end_allocate_pag
17608 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 e;. }.
17609 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 testcas
1760a 65 28 20 69 4e 65 77 54 72 75 6e 6b 3d 3d 6d 78 e( iNewTrunk==mx
1760b 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 20 20 Page );.
1760c 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 rc = btreeGetP
1760d 61 67 65 28 70 42 74 2c 20 69 4e 65 77 54 72 75 age(pBt, iNewTru
1760e 6e 6b 2c 20 26 70 4e 65 77 54 72 75 6e 6b 2c 20 nk, &pNewTrunk,
1760f 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 0);. if
17610 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
17611 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 ){. g
17612 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 oto end_allocate
17613 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 _page;.
17614 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 }. rc
17615 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
17616 69 74 65 28 70 4e 65 77 54 72 75 6e 6b 2d 3e 70 ite(pNewTrunk->p
17617 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
17618 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
17619 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1761a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
1761b 70 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 pNewTrunk);.
1761c 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 goto end
1761d 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a _allocate_page;.
1761e 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1761f 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e memcpy(&pN
17620 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 ewTrunk->aData[0
17621 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 ], &pTrunk->aDat
17622 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 a[0], 4);.
17623 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 4e put4byte(&pN
17624 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 ewTrunk->aData[4
17625 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20 20 ], k-1);.
17626 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 54 memcpy(&pNewT
17627 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 runk->aData[8],
17628 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 31 &pTrunk->aData[1
17629 32 5d 2c 20 28 6b 2d 31 29 2a 34 29 3b 0a 20 20 2], (k-1)*4);.
1762a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
1762b 61 67 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b 0a age(pNewTrunk);.
1762c 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70 if( !p
1762d 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 PrevTrunk ){.
1762e 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
1762f 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
17630 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 31 2d riteable(pPage1-
17631 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 >pDbPage) );.
17632 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 put4byt
17633 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
17634 5b 33 32 5d 2c 20 69 4e 65 77 54 72 75 6e 6b 29 [32], iNewTrunk)
17635 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 ;. }els
17636 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 e{. r
17637 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
17638 57 72 69 74 65 28 70 50 72 65 76 54 72 75 6e 6b Write(pPrevTrunk
17639 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
1763a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 if( rc )
1763b 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
1763c 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 goto end_allocat
1763d 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 e_page;.
1763e 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1763f 20 20 70 75 74 34 62 79 74 65 28 26 70 50 72 65 put4byte(&pPre
17640 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d vTrunk->aData[0]
17641 2c 20 69 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 , iNewTrunk);.
17642 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
17643 20 20 7d 0a 20 20 20 20 20 20 20 20 70 54 72 75 }. pTru
17644 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 nk = 0;.
17645 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 TRACE(("ALLOCATE
17646 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 25 64 20 : %d trunk - %d
17647 66 72 65 65 20 70 61 67 65 73 20 6c 65 66 74 5c free pages left\
17648 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29 n", *pPgno, n-1)
17649 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 );.#endif.
1764a 7d 65 6c 73 65 20 69 66 28 20 6b 3e 30 20 29 7b }else if( k>0 ){
1764b 0a 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 . /* Extr
1764c 61 63 74 20 61 20 6c 65 61 66 20 66 72 6f 6d 20 act a leaf from
1764d 74 68 65 20 74 72 75 6e 6b 20 2a 2f 0a 20 20 20 the trunk */.
1764e 20 20 20 20 20 75 33 32 20 63 6c 6f 73 65 73 74 u32 closest
1764f 3b 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 ;. Pgno i
17650 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 75 6e Page;. un
17651 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 44 61 signed char *aDa
17652 74 61 20 3d 20 70 54 72 75 6e 6b 2d 3e 61 44 61 ta = pTrunk->aDa
17653 74 61 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ta;. rc =
17654 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
17655 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 te(pTrunk->pDbPa
17656 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ge);. if(
17657 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 rc ){.
17658 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 goto end_alloca
17659 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 te_page;.
1765a 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e }. if( n
1765b 65 61 72 62 79 3e 30 20 29 7b 0a 20 20 20 20 20 earby>0 ){.
1765c 20 20 20 20 20 75 33 32 20 69 3b 0a 20 20 20 20 u32 i;.
1765d 20 20 20 20 20 20 69 6e 74 20 64 69 73 74 3b 0a int dist;.
1765e 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73 closes
1765f 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 t = 0;.
17660 20 64 69 73 74 20 3d 20 67 65 74 34 62 79 74 65 dist = get4byte
17661 28 26 61 44 61 74 61 5b 38 5d 29 20 2d 20 6e 65 (&aData[8]) - ne
17662 61 72 62 79 3b 0a 20 20 20 20 20 20 20 20 20 20 arby;.
17663 69 66 28 20 64 69 73 74 3c 30 20 29 20 64 69 73 if( dist<0 ) dis
17664 74 20 3d 20 2d 64 69 73 74 3b 0a 20 20 20 20 20 t = -dist;.
17665 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c for(i=1; i<
17666 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 k; i++){.
17667 20 20 20 20 20 69 6e 74 20 64 32 20 3d 20 67 65 int d2 = ge
17668 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 38 2b t4byte(&aData[8+
17669 69 2a 34 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a i*4]) - nearby;.
1766a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
1766b 64 32 3c 30 20 29 20 64 32 20 3d 20 2d 64 32 3b d2<0 ) d2 = -d2;
1766c 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
1766d 20 64 32 3c 64 69 73 74 20 29 7b 0a 20 20 20 20 d2<dist ){.
1766e 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73 closes
1766f 74 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 20 t = i;.
17670 20 20 20 20 20 64 69 73 74 20 3d 20 64 32 3b 0a dist = d2;.
17671 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
17672 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
17673 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
17674 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a closest = 0;.
17675 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 }..
17676 20 20 20 69 50 61 67 65 20 3d 20 67 65 74 34 62 iPage = get4b
17677 79 74 65 28 26 61 44 61 74 61 5b 38 2b 63 6c 6f yte(&aData[8+clo
17678 73 65 73 74 2a 34 5d 29 3b 0a 20 20 20 20 20 20 sest*4]);.
17679 20 20 74 65 73 74 63 61 73 65 28 20 69 50 61 67 testcase( iPag
1767a 65 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20 20 e==mxPage );.
1767b 20 20 20 20 20 69 66 28 20 69 50 61 67 65 3e 6d if( iPage>m
1767c 78 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 xPage ){.
1767d 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 rc = SQLITE_C
1767e 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
1767f 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
17680 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 allocate_page;.
17681 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
17682 20 74 65 73 74 63 61 73 65 28 20 69 50 61 67 65 testcase( iPage
17683 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20 20 20 ==mxPage );.
17684 20 20 20 20 69 66 28 20 21 73 65 61 72 63 68 4c if( !searchL
17685 69 73 74 20 7c 7c 20 69 50 61 67 65 3d 3d 6e 65 ist || iPage==ne
17686 61 72 62 79 20 29 7b 0a 20 20 20 20 20 20 20 20 arby ){.
17687 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 3b int noContent;
17688 0a 20 20 20 20 20 20 20 20 20 20 2a 70 50 67 6e . *pPgn
17689 6f 20 3d 20 69 50 61 67 65 3b 0a 20 20 20 20 20 o = iPage;.
1768a 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c TRACE(("ALL
1768b 4f 43 41 54 45 3a 20 25 64 20 77 61 73 20 6c 65 OCATE: %d was le
1768c 61 66 20 25 64 20 6f 66 20 25 64 20 6f 6e 20 74 af %d of %d on t
1768d 72 75 6e 6b 20 25 64 22 0a 20 20 20 20 20 20 20 runk %d".
1768e 20 20 20 20 20 20 20 20 20 20 22 3a 20 25 64 20 ": %d
1768f 6d 6f 72 65 20 66 72 65 65 20 70 61 67 65 73 5c more free pages\
17690 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
17691 20 20 20 20 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f *pPgno, clo
17692 73 65 73 74 2b 31 2c 20 6b 2c 20 70 54 72 75 6e sest+1, k, pTrun
17693 6b 2d 3e 70 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a k->pgno, n-1));.
17694 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 6c if( cl
17695 6f 73 65 73 74 3c 6b 2d 31 20 29 7b 0a 20 20 20 osest<k-1 ){.
17696 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
17697 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 &aData[8+closest
17698 2a 34 5d 2c 20 26 61 44 61 74 61 5b 34 2b 6b 2a *4], &aData[4+k*
17699 34 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 4], 4);.
1769a 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 75 }. pu
1769b 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 34 5d t4byte(&aData[4]
1769c 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 , k-1);.
1769d 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1769e 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
1769f 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 e(pTrunk->pDbPag
176a0 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 e) );.
176a1 6e 6f 43 6f 6e 74 65 6e 74 20 3d 20 21 62 74 72 noContent = !btr
176a2 65 65 47 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 eeGetHasContent(
176a3 70 42 74 2c 20 2a 70 50 67 6e 6f 29 3b 0a 20 20 pBt, *pPgno);.
176a4 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 rc = btr
176a5 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 2a eeGetPage(pBt, *
176a6 70 50 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20 6e pPgno, ppPage, n
176a7 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 20 oContent);.
176a8 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
176a9 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
176aa 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
176ab 65 33 50 61 67 65 72 57 72 69 74 65 28 28 2a 70 e3PagerWrite((*p
176ac 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29 pPage)->pDbPage)
176ad 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 ;. if
176ae 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
176af 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
176b0 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 releasePage(*pp
176b1 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Page);.
176b2 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d }. }
176b3 0a 20 20 20 20 20 20 20 20 20 20 73 65 61 72 63 . searc
176b4 68 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 hList = 0;.
176b5 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
176b6 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
176b7 50 72 65 76 54 72 75 6e 6b 29 3b 0a 20 20 20 20 PrevTrunk);.
176b8 20 20 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 30 pPrevTrunk = 0
176b9 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 73 65 ;. }while( se
176ba 61 72 63 68 4c 69 73 74 20 29 3b 0a 20 20 7d 65 archList );. }e
176bb 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 lse{. /* Ther
176bc 65 20 61 72 65 20 6e 6f 20 70 61 67 65 73 20 6f e are no pages o
176bd 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 2c 20 n the freelist,
176be 73 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 so create a new
176bf 70 61 67 65 20 61 74 20 74 68 65 0a 20 20 20 20 page at the.
176c0 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 ** end of the fi
176c1 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 le */. int nP
176c2 61 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 age = pagerPagec
176c3 6f 75 6e 74 28 70 42 74 29 3b 0a 20 20 20 20 2a ount(pBt);. *
176c4 70 50 67 6e 6f 20 3d 20 6e 50 61 67 65 20 2b 20 pPgno = nPage +
176c5 31 3b 0a 0a 20 20 20 20 69 66 28 20 2a 70 50 67 1;.. if( *pPg
176c6 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 no==PENDING_BYTE
176c7 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 _PAGE(pBt) ){.
176c8 20 20 20 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 0a (*pPgno)++;.
176c9 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 }..#ifndef S
176ca 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
176cb 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 ACUUM. if( pB
176cc 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 t->autoVacuum &&
176cd 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 PTRMAP_ISPAGE(p
176ce 42 74 2c 20 2a 70 50 67 6e 6f 29 20 29 7b 0a 20 Bt, *pPgno) ){.
176cf 20 20 20 20 20 2f 2a 20 49 66 20 2a 70 50 67 6e /* If *pPgn
176d0 6f 20 72 65 66 65 72 73 20 74 6f 20 61 20 70 6f o refers to a po
176d1 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 inter-map page,
176d2 61 6c 6c 6f 63 61 74 65 20 74 77 6f 20 6e 65 77 allocate two new
176d3 20 70 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 pages. **
176d4 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 at the end of th
176d5 65 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f e file instead o
176d6 66 20 6f 6e 65 2e 20 54 68 65 20 66 69 72 73 74 f one. The first
176d7 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 0a allocated page.
176d8 20 20 20 20 20 20 2a 2a 20 62 65 63 6f 6d 65 73 ** becomes
176d9 20 61 20 6e 65 77 20 70 6f 69 6e 74 65 72 2d 6d a new pointer-m
176da 61 70 20 70 61 67 65 2c 20 74 68 65 20 73 65 63 ap page, the sec
176db 6f 6e 64 20 69 73 20 75 73 65 64 20 62 79 20 74 ond is used by t
176dc 68 65 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20 20 he caller..
176dd 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 */. MemPag
176de 65 20 2a 70 50 67 20 3d 20 30 3b 0a 20 20 20 20 e *pPg = 0;.
176df 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 TRACE(("ALLOCA
176e0 54 45 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 TE: %d from end
176e1 6f 66 20 66 69 6c 65 20 28 70 6f 69 6e 74 65 72 of file (pointer
176e2 2d 6d 61 70 20 70 61 67 65 29 5c 6e 22 2c 20 2a -map page)\n", *
176e3 70 50 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 61 pPgno));. a
176e4 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 ssert( *pPgno!=P
176e5 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
176e6 28 70 42 74 29 20 29 3b 0a 20 20 20 20 20 20 72 (pBt) );. r
176e7 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
176e8 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 26 70 (pBt, *pPgno, &p
176e9 50 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 Pg, 0);. if
176ea 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
176eb 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
176ec 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
176ed 65 28 70 50 67 2d 3e 70 44 62 50 61 67 65 29 3b e(pPg->pDbPage);
176ee 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
176ef 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20 20 Page(pPg);.
176f0 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 }. if( rc
176f1 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
176f2 20 20 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 0a 20 (*pPgno)++;.
176f3 20 20 20 20 20 69 66 28 20 2a 70 50 67 6e 6f 3d if( *pPgno=
176f4 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 =PENDING_BYTE_PA
176f5 47 45 28 70 42 74 29 20 29 7b 20 28 2a 70 50 67 GE(pBt) ){ (*pPg
176f6 6e 6f 29 2b 2b 3b 20 7d 0a 20 20 20 20 7d 0a 23 no)++; }. }.#
176f7 65 6e 64 69 66 0a 0a 20 20 20 20 61 73 73 65 72 endif.. asser
176f8 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 t( *pPgno!=PENDI
176f9 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
176fa 29 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 74 ) );. rc = bt
176fb 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
176fc 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20 *pPgno, ppPage,
176fd 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 0);. if( rc )
176fe 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
176ff 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
17700 72 57 72 69 74 65 28 28 2a 70 70 50 61 67 65 29 rWrite((*ppPage)
17701 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
17702 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
17703 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 K ){. relea
17704 73 65 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b sePage(*ppPage);
17705 0a 20 20 20 20 7d 0a 20 20 20 20 54 52 41 43 45 . }. TRACE
17706 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 (("ALLOCATE: %d
17707 66 72 6f 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 from end of file
17708 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a 20 \n", *pPgno));.
17709 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 2a 70 }.. assert( *p
1770a 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 Pgno!=PENDING_BY
1770b 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a TE_PAGE(pBt) );.
1770c 0a 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 .end_allocate_pa
1770d 67 65 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 ge:. releasePag
1770e 65 28 70 54 72 75 6e 6b 29 3b 0a 20 20 72 65 6c e(pTrunk);. rel
1770f 65 61 73 65 50 61 67 65 28 70 50 72 65 76 54 72 easePage(pPrevTr
17710 75 6e 6b 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d unk);. if( rc==
17711 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
17712 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 if( sqlite3Page
17713 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 28 2a rPageRefcount((*
17714 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67 65 ppPage)->pDbPage
17715 29 3e 31 20 29 7b 0a 20 20 20 20 20 20 72 65 6c )>1 ){. rel
17716 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 67 65 easePage(*ppPage
17717 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
17718 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
17719 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 28 KPT;. }. (
1771a 2a 70 70 50 61 67 65 29 2d 3e 69 73 49 6e 69 74 *ppPage)->isInit
1771b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 0;. }else{.
1771c 20 20 20 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a *ppPage = 0;.
1771d 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1771e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
1771f 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 unction is used
17720 74 6f 20 61 64 64 20 70 61 67 65 20 69 50 61 67 to add page iPag
17721 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 e to the databas
17722 65 20 66 69 6c 65 20 66 72 65 65 2d 6c 69 73 74 e file free-list
17723 2e 20 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 75 . .** It is assu
17724 6d 65 64 20 74 68 61 74 20 74 68 65 20 70 61 67 med that the pag
17725 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 e is not already
17726 20 61 20 70 61 72 74 20 6f 66 20 74 68 65 20 66 a part of the f
17727 72 65 65 2d 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 ree-list..**.**
17728 54 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 The value passed
17729 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 as the second a
1772a 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 rgument to this
1772b 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 70 74 69 function is opti
1772c 6f 6e 61 6c 2e 0a 2a 2a 20 49 66 20 74 68 65 20 onal..** If the
1772d 63 61 6c 6c 65 72 20 68 61 70 70 65 6e 73 20 74 caller happens t
1772e 6f 20 68 61 76 65 20 61 20 70 6f 69 6e 74 65 72 o have a pointer
1772f 20 74 6f 20 74 68 65 20 4d 65 6d 50 61 67 65 20 to the MemPage
17730 6f 62 6a 65 63 74 20 0a 2a 2a 20 63 6f 72 72 65 object .** corre
17731 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 61 67 65 sponding to page
17732 20 69 50 61 67 65 20 68 61 6e 64 79 2c 20 69 74 iPage handy, it
17733 20 6d 61 79 20 70 61 73 73 20 69 74 20 61 73 20 may pass it as
17734 74 68 65 20 73 65 63 6f 6e 64 20 76 61 6c 75 65 the second value
17735 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c . .** Otherwise,
17736 20 69 74 20 6d 61 79 20 70 61 73 73 20 4e 55 4c it may pass NUL
17737 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 70 6f L..**.** If a po
17738 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 50 61 inter to a MemPa
17739 67 65 20 6f 62 6a 65 63 74 20 69 73 20 70 61 73 ge object is pas
1773a 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e sed as the secon
1773b 64 20 61 72 67 75 6d 65 6e 74 2c 0a 2a 2a 20 69 d argument,.** i
1773c 74 73 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 ts reference cou
1773d 6e 74 20 69 73 20 6e 6f 74 20 61 6c 74 65 72 65 nt is not altere
1773e 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 d by this functi
1773f 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e on..*/.static in
17740 74 20 66 72 65 65 50 61 67 65 32 28 42 74 53 68 t freePage2(BtSh
17741 61 72 65 64 20 2a 70 42 74 2c 20 4d 65 6d 50 61 ared *pBt, MemPa
17742 67 65 20 2a 70 4d 65 6d 50 61 67 65 2c 20 50 67 ge *pMemPage, Pg
17743 6e 6f 20 69 50 61 67 65 29 7b 0a 20 20 4d 65 6d no iPage){. Mem
17744 50 61 67 65 20 2a 70 54 72 75 6e 6b 20 3d 20 30 Page *pTrunk = 0
17745 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
17746 20 2f 2a 20 46 72 65 65 2d 6c 69 73 74 20 74 72 /* Free-list tr
17747 75 6e 6b 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 unk page */. Pg
17748 6e 6f 20 69 54 72 75 6e 6b 20 3d 20 30 3b 20 20 no iTrunk = 0;
17749 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1774a 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 /* Page number
1774b 20 6f 66 20 66 72 65 65 2d 6c 69 73 74 20 74 72 of free-list tr
1774c 75 6e 6b 20 70 61 67 65 20 2a 2f 20 0a 20 20 4d unk page */ . M
1774d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 20 3d emPage *pPage1 =
1774e 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 20 20 20 pBt->pPage1;
1774f 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 72 65 66 65 /* Local refe
17750 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 rence to page 1
17751 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 */. MemPage *pP
17752 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
17753 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
17754 20 62 65 69 6e 67 20 66 72 65 65 64 2e 20 4d 61 being freed. Ma
17755 79 20 62 65 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 y be NULL. */.
17756 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
17757 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17758 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 43 6f /* Return Co
17759 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 65 de */. int nFre
1775a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
1775b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
1775c 6e 69 74 69 61 6c 20 6e 75 6d 62 65 72 20 6f 66 nitial number of
1775d 20 70 61 67 65 73 20 6f 6e 20 66 72 65 65 2d 6c pages on free-l
1775e 69 73 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ist */.. assert
1775f 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
17760 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
17761 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 50 );. assert( iP
17762 61 67 65 3e 31 20 29 3b 0a 20 20 61 73 73 65 72 age>1 );. asser
17763 74 28 20 21 70 4d 65 6d 50 61 67 65 20 7c 7c 20 t( !pMemPage ||
17764 70 4d 65 6d 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d pMemPage->pgno==
17765 69 50 61 67 65 20 29 3b 0a 0a 20 20 69 66 28 20 iPage );.. if(
17766 70 4d 65 6d 50 61 67 65 20 29 7b 0a 20 20 20 20 pMemPage ){.
17767 70 50 61 67 65 20 3d 20 70 4d 65 6d 50 61 67 65 pPage = pMemPage
17768 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 ;. sqlite3Pag
17769 65 72 52 65 66 28 70 50 61 67 65 2d 3e 70 44 62 erRef(pPage->pDb
1776a 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a Page);. }else{.
1776b 20 20 20 20 70 50 61 67 65 20 3d 20 62 74 72 65 pPage = btre
1776c 65 50 61 67 65 4c 6f 6f 6b 75 70 28 70 42 74 2c ePageLookup(pBt,
1776d 20 69 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 iPage);. }..
1776e 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 /* Increment the
1776f 20 66 72 65 65 20 70 61 67 65 20 63 6f 75 6e 74 free page count
17770 20 6f 6e 20 70 50 61 67 65 31 20 2a 2f 0a 20 20 on pPage1 */.
17771 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
17772 72 57 72 69 74 65 28 70 50 61 67 65 31 2d 3e 70 rWrite(pPage1->p
17773 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 DbPage);. if( r
17774 63 20 29 20 67 6f 74 6f 20 66 72 65 65 70 61 67 c ) goto freepag
17775 65 5f 6f 75 74 3b 0a 20 20 6e 46 72 65 65 20 3d e_out;. nFree =
17776 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
17777 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 1->aData[36]);.
17778 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 put4byte(&pPage
17779 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e 46 1->aData[36], nF
1777a 72 65 65 2b 31 29 3b 0a 0a 23 69 66 64 65 66 20 ree+1);..#ifdef
1777b 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 SQLITE_SECURE_DE
1777c 4c 45 54 45 0a 20 20 2f 2a 20 49 66 20 74 68 65 LETE. /* If the
1777d 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 SQLITE_SECURE_D
1777e 45 4c 45 54 45 20 63 6f 6d 70 69 6c 65 2d 74 69 ELETE compile-ti
1777f 6d 65 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 me option is ena
17780 62 6c 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 bled, then. **
17781 61 6c 77 61 79 73 20 66 75 6c 6c 79 20 6f 76 65 always fully ove
17782 72 77 72 69 74 65 20 64 65 6c 65 74 65 64 20 69 rwrite deleted i
17783 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 nformation with
17784 7a 65 72 6f 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 zeros.. */. if
17785 28 20 28 21 70 50 61 67 65 20 26 26 20 28 72 63 ( (!pPage && (rc
17786 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
17787 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 61 pBt, iPage, &pPa
17788 67 65 2c 20 30 29 29 29 0a 20 20 20 7c 7c 20 20 ge, 0))). ||
17789 20 20 20 20 20 20 20 20 20 20 28 72 63 20 3d 20 (rc =
1778a 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
1778b 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
1778c 29 29 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 6f )). ){. goto
1778d 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 freepage_out;.
1778e 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 50 61 67 }. memset(pPag
1778f 65 2d 3e 61 44 61 74 61 2c 20 30 2c 20 70 50 61 e->aData, 0, pPa
17790 67 65 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a ge->pBt->pageSiz
17791 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a e);.#endif.. /*
17792 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 If the database
17793 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 supports auto-v
17794 61 63 75 75 6d 2c 20 77 72 69 74 65 20 61 6e 20 acuum, write an
17795 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70 6f 69 entry in the poi
17796 6e 74 65 72 2d 6d 61 70 0a 20 20 2a 2a 20 74 6f nter-map. ** to
17797 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 indicate that t
17798 68 65 20 70 61 67 65 20 69 73 20 66 72 65 65 2e he page is free.
17799 0a 20 20 2a 2f 0a 20 20 69 66 28 20 49 53 41 55 . */. if( ISAU
1779a 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 TOVACUUM ){.
1779b 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 69 ptrmapPut(pBt, i
1779c 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 Page, PTRMAP_FRE
1779d 45 50 41 47 45 2c 20 30 2c 20 26 72 63 29 3b 0a EPAGE, 0, &rc);.
1779e 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 if( rc ) got
1779f 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a o freepage_out;.
177a0 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 6d 61 }.. /* Now ma
177a1 6e 69 70 75 6c 61 74 65 20 74 68 65 20 61 63 74 nipulate the act
177a2 75 61 6c 20 64 61 74 61 62 61 73 65 20 66 72 65 ual database fre
177a3 65 2d 6c 69 73 74 20 73 74 72 75 63 74 75 72 65 e-list structure
177a4 2e 20 54 68 65 72 65 20 61 72 65 20 74 77 6f 0a . There are two.
177a5 20 20 2a 2a 20 70 6f 73 73 69 62 69 6c 69 74 69 ** possibiliti
177a6 65 73 2e 20 49 66 20 74 68 65 20 66 72 65 65 2d es. If the free-
177a7 6c 69 73 74 20 69 73 20 63 75 72 72 65 6e 74 6c list is currentl
177a8 79 20 65 6d 70 74 79 2c 20 6f 72 20 69 66 20 74 y empty, or if t
177a9 68 65 20 66 69 72 73 74 0a 20 20 2a 2a 20 74 72 he first. ** tr
177aa 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20 unk page in the
177ab 66 72 65 65 2d 6c 69 73 74 20 69 73 20 66 75 6c free-list is ful
177ac 6c 2c 20 74 68 65 6e 20 74 68 69 73 20 70 61 67 l, then this pag
177ad 65 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 61 0a e will become a.
177ae 20 20 2a 2a 20 6e 65 77 20 66 72 65 65 2d 6c 69 ** new free-li
177af 73 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 4f st trunk page. O
177b0 74 68 65 72 77 69 73 65 2c 20 69 74 20 77 69 6c therwise, it wil
177b1 6c 20 62 65 63 6f 6d 65 20 61 20 6c 65 61 66 20 l become a leaf
177b2 6f 66 20 74 68 65 0a 20 20 2a 2a 20 66 69 72 73 of the. ** firs
177b3 74 20 74 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 t trunk page in
177b4 74 68 65 20 63 75 72 72 65 6e 74 20 66 72 65 65 the current free
177b5 2d 6c 69 73 74 2e 20 54 68 69 73 20 62 6c 6f 63 -list. This bloc
177b6 6b 20 74 65 73 74 73 20 69 66 20 69 74 0a 20 20 k tests if it.
177b7 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 ** is possible t
177b8 6f 20 61 64 64 20 74 68 65 20 70 61 67 65 20 61 o add the page a
177b9 73 20 61 20 6e 65 77 20 66 72 65 65 2d 6c 69 73 s a new free-lis
177ba 74 20 6c 65 61 66 2e 0a 20 20 2a 2f 0a 20 20 69 t leaf.. */. i
177bb 66 28 20 6e 46 72 65 65 21 3d 30 20 29 7b 0a 20 f( nFree!=0 ){.
177bc 20 20 20 75 33 32 20 6e 4c 65 61 66 3b 20 20 20 u32 nLeaf;
177bd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
177be 49 6e 69 74 69 61 6c 20 6e 75 6d 62 65 72 20 6f Initial number o
177bf 66 20 6c 65 61 66 20 63 65 6c 6c 73 20 6f 6e 20 f leaf cells on
177c0 74 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0a 0a 20 trunk page */..
177c1 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 iTrunk = get4
177c2 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 byte(&pPage1->aD
177c3 61 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 72 63 ata[32]);. rc
177c4 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
177c5 70 42 74 2c 20 69 54 72 75 6e 6b 2c 20 26 70 54 pBt, iTrunk, &pT
177c6 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 69 66 runk, 0);. if
177c7 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
177c8 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72 ){. goto fr
177c9 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 eepage_out;.
177ca 7d 0a 0a 20 20 20 20 6e 4c 65 61 66 20 3d 20 67 }.. nLeaf = g
177cb 65 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d et4byte(&pTrunk-
177cc 3e 61 44 61 74 61 5b 34 5d 29 3b 0a 20 20 20 20 >aData[4]);.
177cd 61 73 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61 assert( pBt->usa
177ce 62 6c 65 53 69 7a 65 3e 33 32 20 29 3b 0a 20 20 bleSize>32 );.
177cf 20 20 69 66 28 20 6e 4c 65 61 66 20 3e 20 28 75 if( nLeaf > (u
177d0 33 32 29 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 32)pBt->usableSi
177d1 7a 65 2f 34 20 2d 20 32 20 29 7b 0a 20 20 20 20 ze/4 - 2 ){.
177d2 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f rc = SQLITE_CO
177d3 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
177d4 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f goto freepage_
177d5 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 out;. }. i
177d6 66 28 20 6e 4c 65 61 66 20 3c 20 28 75 33 32 29 f( nLeaf < (u32)
177d7 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f pBt->usableSize/
177d8 34 20 2d 20 38 20 29 7b 0a 20 20 20 20 20 20 2f 4 - 8 ){. /
177d9 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 * In this case t
177da 68 65 72 65 20 69 73 20 72 6f 6f 6d 20 6f 6e 20 here is room on
177db 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20 74 the trunk page t
177dc 6f 20 69 6e 73 65 72 74 20 74 68 65 20 70 61 67 o insert the pag
177dd 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 69 6e 67 e. ** being
177de 20 66 72 65 65 64 20 61 73 20 61 20 6e 65 77 20 freed as a new
177df 6c 65 61 66 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 leaf.. **.
177e0 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 ** Note tha
177e1 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65 t the trunk page
177e2 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 66 is not really f
177e3 75 6c 6c 20 75 6e 74 69 6c 20 69 74 20 63 6f 6e ull until it con
177e4 74 61 69 6e 73 0a 20 20 20 20 20 20 2a 2a 20 75 tains. ** u
177e5 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 20 sableSize/4 - 2
177e6 65 6e 74 72 69 65 73 2c 20 6e 6f 74 20 75 73 61 entries, not usa
177e7 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 65 6e bleSize/4 - 8 en
177e8 74 72 69 65 73 20 61 73 20 77 65 20 68 61 76 65 tries as we have
177e9 0a 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 64 2e . ** coded.
177ea 20 20 42 75 74 20 64 75 65 20 74 6f 20 61 20 63 But due to a c
177eb 6f 64 69 6e 67 20 65 72 72 6f 72 20 69 6e 20 76 oding error in v
177ec 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 ersions of SQLit
177ed 65 20 70 72 69 6f 72 20 74 6f 0a 20 20 20 20 20 e prior to.
177ee 20 2a 2a 20 33 2e 36 2e 30 2c 20 64 61 74 61 62 ** 3.6.0, datab
177ef 61 73 65 73 20 77 69 74 68 20 66 72 65 65 6c 69 ases with freeli
177f0 73 74 20 74 72 75 6e 6b 20 70 61 67 65 73 20 68 st trunk pages h
177f1 6f 6c 64 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e olding more than
177f2 0a 20 20 20 20 20 20 2a 2a 20 75 73 61 62 6c 65 . ** usable
177f3 53 69 7a 65 2f 34 20 2d 20 38 20 65 6e 74 72 69 Size/4 - 8 entri
177f4 65 73 20 77 69 6c 6c 20 62 65 20 72 65 70 6f 72 es will be repor
177f5 74 65 64 20 61 73 20 63 6f 72 72 75 70 74 2e 20 ted as corrupt.
177f6 20 49 6e 20 6f 72 64 65 72 0a 20 20 20 20 20 20 In order.
177f7 2a 2a 20 74 6f 20 6d 61 69 6e 74 61 69 6e 20 62 ** to maintain b
177f8 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 ackwards compati
177f9 62 69 6c 69 74 79 20 77 69 74 68 20 6f 6c 64 65 bility with olde
177fa 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 r versions of SQ
177fb 4c 69 74 65 2c 0a 20 20 20 20 20 20 2a 2a 20 77 Lite,. ** w
177fc 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 e will continue
177fd 74 6f 20 72 65 73 74 72 69 63 74 20 74 68 65 20 to restrict the
177fe 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 number of entrie
177ff 73 20 74 6f 20 75 73 61 62 6c 65 53 69 7a 65 2f s to usableSize/
17800 34 20 2d 20 38 0a 20 20 20 20 20 20 2a 2a 20 66 4 - 8. ** f
17801 6f 72 20 6e 6f 77 2e 20 20 41 74 20 73 6f 6d 65 or now. At some
17802 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 point in the fu
17803 74 75 72 65 20 28 6f 6e 63 65 20 65 76 65 72 79 ture (once every
17804 6f 6e 65 20 68 61 73 20 75 70 67 72 61 64 65 64 one has upgraded
17805 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 33 2e 36 . ** to 3.6
17806 2e 30 20 6f 72 20 6c 61 74 65 72 29 20 77 65 20 .0 or later) we
17807 73 68 6f 75 6c 64 20 63 6f 6e 73 69 64 65 72 20 should consider
17808 66 69 78 69 6e 67 20 74 68 65 20 63 6f 6e 64 69 fixing the condi
17809 74 69 6f 6e 61 6c 20 61 62 6f 76 65 0a 20 20 20 tional above.
1780a 20 20 20 2a 2a 20 74 6f 20 72 65 61 64 20 22 75 ** to read "u
1780b 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 22 20 69 sableSize/4-2" i
1780c 6e 73 74 65 61 64 20 6f 66 20 22 75 73 61 62 6c nstead of "usabl
1780d 65 53 69 7a 65 2f 34 2d 38 22 2e 0a 20 20 20 20 eSize/4-8"..
1780e 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 */. rc =
1780f 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
17810 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 e(pTrunk->pDbPag
17811 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 e);. if( rc
17812 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
17813 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 put4byte(
17814 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 &pTrunk->aData[4
17815 5d 2c 20 6e 4c 65 61 66 2b 31 29 3b 0a 20 20 20 ], nLeaf+1);.
17816 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 put4byte(&p
17817 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 2b 6e Trunk->aData[8+n
17818 4c 65 61 66 2a 34 5d 2c 20 69 50 61 67 65 29 3b Leaf*4], iPage);
17819 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1781a 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 SECURE_DELETE.
1781b 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 20 if( pPage
1781c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ){. sql
1781d 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 69 ite3PagerDontWri
1781e 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 te(pPage->pDbPag
1781f 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 e);. }.#e
17820 6e 64 69 66 0a 20 20 20 20 20 20 20 20 72 63 20 ndif. rc
17821 3d 20 62 74 72 65 65 53 65 74 48 61 73 43 6f 6e = btreeSetHasCon
17822 74 65 6e 74 28 70 42 74 2c 20 69 50 61 67 65 29 tent(pBt, iPage)
17823 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
17824 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 47 TRACE(("FREE-PAG
17825 45 3a 20 25 64 20 6c 65 61 66 20 6f 6e 20 74 72 E: %d leaf on tr
17826 75 6e 6b 20 70 61 67 65 20 25 64 5c 6e 22 2c 70 unk page %d\n",p
17827 50 61 67 65 2d 3e 70 67 6e 6f 2c 70 54 72 75 6e Page->pgno,pTrun
17828 6b 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 k->pgno));.
17829 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f goto freepage_o
1782a 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 ut;. }. }..
1782b 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 66 /* If control f
1782c 6c 6f 77 73 20 74 6f 20 74 68 69 73 20 70 6f 69 lows to this poi
1782d 6e 74 2c 20 74 68 65 6e 20 69 74 20 77 61 73 20 nt, then it was
1782e 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 not possible to
1782f 61 64 64 20 74 68 65 0a 20 20 2a 2a 20 74 68 65 add the. ** the
17830 20 70 61 67 65 20 62 65 69 6e 67 20 66 72 65 65 page being free
17831 64 20 61 73 20 61 20 6c 65 61 66 20 70 61 67 65 d as a leaf page
17832 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 74 72 of the first tr
17833 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 65 2d unk in the free-
17834 6c 69 73 74 2e 0a 20 20 2a 2a 20 50 6f 73 73 69 list.. ** Possi
17835 62 6c 79 20 62 65 63 61 75 73 65 20 74 68 65 20 bly because the
17836 66 72 65 65 2d 6c 69 73 74 20 69 73 20 65 6d 70 free-list is emp
17837 74 79 2c 20 6f 72 20 70 6f 73 73 69 62 6c 79 20 ty, or possibly
17838 62 65 63 61 75 73 65 20 74 68 65 20 0a 20 20 2a because the . *
17839 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20 69 6e * first trunk in
1783a 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 69 the free-list i
1783b 73 20 66 75 6c 6c 2e 20 45 69 74 68 65 72 20 77 s full. Either w
1783c 61 79 2c 20 74 68 65 20 70 61 67 65 20 62 65 69 ay, the page bei
1783d 6e 67 20 66 72 65 65 64 0a 20 20 2a 2a 20 77 69 ng freed. ** wi
1783e 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6e 65 ll become the ne
1783f 77 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70 61 w first trunk pa
17840 67 65 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c ge in the free-l
17841 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ist.. */. if(
17842 70 50 61 67 65 3d 3d 30 20 26 26 20 53 51 4c 49 pPage==0 && SQLI
17843 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 62 74 72 TE_OK!=(rc = btr
17844 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 eeGetPage(pBt, i
17845 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29 Page, &pPage, 0)
17846 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 72 ) ){. goto fr
17847 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a eepage_out;. }.
17848 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
17849 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e gerWrite(pPage->
1784a 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 pDbPage);. if(
1784b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1784c 0a 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 . goto freepa
1784d 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 70 75 ge_out;. }. pu
1784e 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 t4byte(pPage->aD
1784f 61 74 61 2c 20 69 54 72 75 6e 6b 29 3b 0a 20 20 ata, iTrunk);.
17850 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 2d put4byte(&pPage-
17851 3e 61 44 61 74 61 5b 34 5d 2c 20 30 29 3b 0a 20 >aData[4], 0);.
17852 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 put4byte(&pPage
17853 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 50 1->aData[32], iP
17854 61 67 65 29 3b 0a 20 20 54 52 41 43 45 28 28 22 age);. TRACE(("
17855 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 6e 65 FREE-PAGE: %d ne
17856 77 20 74 72 75 6e 6b 20 70 61 67 65 20 72 65 70 w trunk page rep
17857 6c 61 63 69 6e 67 20 25 64 5c 6e 22 2c 20 70 50 lacing %d\n", pP
17858 61 67 65 2d 3e 70 67 6e 6f 2c 20 69 54 72 75 6e age->pgno, iTrun
17859 6b 29 29 3b 0a 0a 66 72 65 65 70 61 67 65 5f 6f k));..freepage_o
1785a 75 74 3a 0a 20 20 69 66 28 20 70 50 61 67 65 20 ut:. if( pPage
1785b 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 ){. pPage->is
1785c 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 Init = 0;. }.
1785d 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 releasePage(pPag
1785e 65 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67 e);. releasePag
1785f 65 28 70 54 72 75 6e 6b 29 3b 0a 20 20 72 65 74 e(pTrunk);. ret
17860 75 72 6e 20 72 63 3b 0a 7d 0a 73 74 61 74 69 63 urn rc;.}.static
17861 20 76 6f 69 64 20 66 72 65 65 50 61 67 65 28 4d void freePage(M
17862 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 emPage *pPage, i
17863 6e 74 20 2a 70 52 43 29 7b 0a 20 20 69 66 28 20 nt *pRC){. if(
17864 28 2a 70 52 43 29 3d 3d 53 51 4c 49 54 45 5f 4f (*pRC)==SQLITE_O
17865 4b 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20 K ){. *pRC =
17866 66 72 65 65 50 61 67 65 32 28 70 50 61 67 65 2d freePage2(pPage-
17867 3e 70 42 74 2c 20 70 50 61 67 65 2c 20 70 50 61 >pBt, pPage, pPa
17868 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a 7d ge->pgno);. }.}
17869 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 79 ../*.** Free any
1786a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 overflow pages
1786b 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
1786c 74 68 65 20 67 69 76 65 6e 20 43 65 6c 6c 2e 0a the given Cell..
1786d 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c */.static int cl
1786e 65 61 72 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 earCell(MemPage
1786f 2a 70 50 61 67 65 2c 20 75 6e 73 69 67 6e 65 64 *pPage, unsigned
17870 20 63 68 61 72 20 2a 70 43 65 6c 6c 29 7b 0a 20 char *pCell){.
17871 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
17872 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 43 pPage->pBt;. C
17873 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 ellInfo info;.
17874 50 67 6e 6f 20 6f 76 66 6c 50 67 6e 6f 3b 0a 20 Pgno ovflPgno;.
17875 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e int rc;. int n
17876 4f 76 66 6c 3b 0a 20 20 75 31 36 20 6f 76 66 6c Ovfl;. u16 ovfl
17877 50 61 67 65 53 69 7a 65 3b 0a 0a 20 20 61 73 73 PageSize;.. ass
17878 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
17879 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
1787a 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
1787b 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 btreeParseCellPt
1787c 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 r(pPage, pCell,
1787d 26 69 6e 66 6f 29 3b 0a 20 20 69 66 28 20 69 6e &info);. if( in
1787e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 fo.iOverflow==0
1787f 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
17880 4c 49 54 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f 20 LITE_OK; /* No
17881 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 overflow pages.
17882 52 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 Return without d
17883 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 2a 2f oing anything */
17884 0a 20 20 7d 0a 20 20 6f 76 66 6c 50 67 6e 6f 20 . }. ovflPgno
17885 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65 6c = get4byte(&pCel
17886 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 l[info.iOverflow
17887 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 ]);. assert( pB
17888 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3e 20 t->usableSize >
17889 34 20 29 3b 0a 20 20 6f 76 66 6c 50 61 67 65 53 4 );. ovflPageS
1788a 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c ize = pBt->usabl
1788b 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 6e 4f 76 eSize - 4;. nOv
1788c 66 6c 20 3d 20 28 69 6e 66 6f 2e 6e 50 61 79 6c fl = (info.nPayl
1788d 6f 61 64 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 oad - info.nLoca
1788e 6c 20 2b 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 l + ovflPageSize
1788f 20 2d 20 31 29 2f 6f 76 66 6c 50 61 67 65 53 69 - 1)/ovflPageSi
17890 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 76 ze;. assert( ov
17891 66 6c 50 67 6e 6f 3d 3d 30 20 7c 7c 20 6e 4f 76 flPgno==0 || nOv
17892 66 6c 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 fl>0 );. while(
17893 20 6e 4f 76 66 6c 2d 2d 20 29 7b 0a 20 20 20 20 nOvfl-- ){.
17894 50 67 6e 6f 20 69 4e 65 78 74 20 3d 20 30 3b 0a Pgno iNext = 0;.
17895 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 76 MemPage *pOv
17896 66 6c 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 fl = 0;. if(
17897 6f 76 66 6c 50 67 6e 6f 3c 32 20 7c 7c 20 6f 76 ovflPgno<2 || ov
17898 66 6c 50 67 6e 6f 3e 70 61 67 65 72 50 61 67 65 flPgno>pagerPage
17899 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a 20 20 count(pBt) ){.
1789a 20 20 20 20 2f 2a 20 30 20 69 73 20 6e 6f 74 20 /* 0 is not
1789b 61 20 6c 65 67 61 6c 20 70 61 67 65 20 6e 75 6d a legal page num
1789c 62 65 72 20 61 6e 64 20 70 61 67 65 20 31 20 63 ber and page 1 c
1789d 61 6e 6e 6f 74 20 62 65 20 61 6e 20 0a 20 20 20 annot be an .
1789e 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 ** overflow p
1789f 61 67 65 2e 20 54 68 65 72 65 66 6f 72 65 20 69 age. Therefore i
178a0 66 20 6f 76 66 6c 50 67 6e 6f 3c 32 20 6f 72 20 f ovflPgno<2 or
178a1 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 past the end of
178a2 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 the . ** fi
178a3 6c 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 le the database
178a4 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70 74 2e must be corrupt.
178a5 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e */. return
178a6 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
178a7 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 BKPT;. }.
178a8 69 66 28 20 6e 4f 76 66 6c 20 29 7b 0a 20 20 20 if( nOvfl ){.
178a9 20 20 20 72 63 20 3d 20 67 65 74 4f 76 65 72 66 rc = getOverf
178aa 6c 6f 77 50 61 67 65 28 70 42 74 2c 20 6f 76 66 lowPage(pBt, ovf
178ab 6c 50 67 6e 6f 2c 20 26 70 4f 76 66 6c 2c 20 26 lPgno, &pOvfl, &
178ac 69 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 69 66 iNext);. if
178ad 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
178ae 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
178af 20 66 72 65 65 50 61 67 65 32 28 70 42 74 2c 20 freePage2(pBt,
178b0 70 4f 76 66 6c 2c 20 6f 76 66 6c 50 67 6e 6f 29 pOvfl, ovflPgno)
178b1 3b 0a 20 20 20 20 69 66 28 20 70 4f 76 66 6c 20 ;. if( pOvfl
178b2 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
178b3 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76 66 6c PagerUnref(pOvfl
178b4 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
178b5 7d 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 }. if( rc ) r
178b6 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 6f 76 eturn rc;. ov
178b7 66 6c 50 67 6e 6f 20 3d 20 69 4e 65 78 74 3b 0a flPgno = iNext;.
178b8 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
178b9 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
178ba 20 43 72 65 61 74 65 20 74 68 65 20 62 79 74 65 Create the byte
178bb 20 73 65 71 75 65 6e 63 65 20 75 73 65 64 20 74 sequence used t
178bc 6f 20 72 65 70 72 65 73 65 6e 74 20 61 20 63 65 o represent a ce
178bd 6c 6c 20 6f 6e 20 70 61 67 65 20 70 50 61 67 65 ll on page pPage
178be 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 74 68 .** and write th
178bf 61 74 20 62 79 74 65 20 73 65 71 75 65 6e 63 65 at byte sequence
178c0 20 69 6e 74 6f 20 70 43 65 6c 6c 5b 5d 2e 20 20 into pCell[].
178c1 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 Overflow pages a
178c2 72 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 re.** allocated
178c3 61 6e 64 20 66 69 6c 6c 65 64 20 69 6e 20 61 73 and filled in as
178c4 20 6e 65 63 65 73 73 61 72 79 2e 20 20 54 68 65 necessary. The
178c5 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 calling procedu
178c6 72 65 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 re.** is respons
178c7 69 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 ible for making
178c8 73 75 72 65 20 73 75 66 66 69 63 69 65 6e 74 20 sure sufficient
178c9 73 70 61 63 65 20 68 61 73 20 62 65 65 6e 20 61 space has been a
178ca 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 66 6f 72 20 llocated.** for
178cb 70 43 65 6c 6c 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 4e pCell[]..**.** N
178cc 6f 74 65 20 74 68 61 74 20 70 43 65 6c 6c 20 64 ote that pCell d
178cd 6f 65 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 oes not necessar
178ce 79 20 6e 65 65 64 20 74 6f 20 70 6f 69 6e 74 20 y need to point
178cf 74 6f 20 74 68 65 20 70 50 61 67 65 2d 3e 61 44 to the pPage->aD
178d0 61 74 61 0a 2a 2a 20 61 72 65 61 2e 20 20 70 43 ata.** area. pC
178d1 65 6c 6c 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 ell might point
178d2 74 6f 20 73 6f 6d 65 20 74 65 6d 70 6f 72 61 72 to some temporar
178d3 79 20 73 74 6f 72 61 67 65 2e 20 20 54 68 65 20 y storage. The
178d4 63 65 6c 6c 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 cell will.** be
178d5 63 6f 6e 73 74 72 75 63 74 65 64 20 69 6e 20 74 constructed in t
178d6 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20 61 72 his temporary ar
178d7 65 61 20 74 68 65 6e 20 63 6f 70 69 65 64 20 69 ea then copied i
178d8 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 nto pPage->aData
178d9 0a 2a 2a 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 .** later..*/.st
178da 61 74 69 63 20 69 6e 74 20 66 69 6c 6c 49 6e 43 atic int fillInC
178db 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a ell(. MemPage *
178dc 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 pPage,
178dd 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 /* The pag
178de 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 e that contains
178df 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 6e the cell */. un
178e0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 signed char *pCe
178e1 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ll, /*
178e2 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 Complete text of
178e3 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 63 the cell */. c
178e4 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c onst void *pKey,
178e5 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f 2a i64 nKey, /*
178e6 20 54 68 65 20 6b 65 79 20 2a 2f 0a 20 20 63 6f The key */. co
178e7 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c nst void *pData,
178e8 69 6e 74 20 6e 44 61 74 61 2c 20 20 20 2f 2a 20 int nData, /*
178e9 54 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e The data */. in
178ea 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 t nZero,
178eb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
178ec 45 78 74 72 61 20 7a 65 72 6f 20 62 79 74 65 73 Extra zero bytes
178ed 20 74 6f 20 61 70 70 65 6e 64 20 74 6f 20 70 44 to append to pD
178ee 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e ata */. int *pn
178ef 53 69 7a 65 20 20 20 20 20 20 20 20 20 20 20 20 Size
178f0 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 /* Write
178f1 20 63 65 6c 6c 20 73 69 7a 65 20 68 65 72 65 20 cell size here
178f2 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 50 61 79 */.){. int nPay
178f3 6c 6f 61 64 3b 0a 20 20 63 6f 6e 73 74 20 75 38 load;. const u8
178f4 20 2a 70 53 72 63 3b 0a 20 20 69 6e 74 20 6e 53 *pSrc;. int nS
178f5 72 63 2c 20 6e 2c 20 72 63 3b 0a 20 20 69 6e 74 rc, n, rc;. int
178f6 20 73 70 61 63 65 4c 65 66 74 3b 0a 20 20 4d 65 spaceLeft;. Me
178f7 6d 50 61 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30 mPage *pOvfl = 0
178f8 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 6f ;. MemPage *pTo
178f9 52 65 6c 65 61 73 65 20 3d 20 30 3b 0a 20 20 75 Release = 0;. u
178fa 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50 nsigned char *pP
178fb 72 69 6f 72 3b 0a 20 20 75 6e 73 69 67 6e 65 64 rior;. unsigned
178fc 20 63 68 61 72 20 2a 70 50 61 79 6c 6f 61 64 3b char *pPayload;
178fd 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
178fe 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 = pPage->pBt;.
178ff 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d Pgno pgnoOvfl =
17900 20 30 3b 0a 20 20 69 6e 74 20 6e 48 65 61 64 65 0;. int nHeade
17901 72 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e r;. CellInfo in
17902 66 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 fo;.. assert( s
17903 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
17904 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
17905 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 70 50 tex) );.. /* pP
17906 61 67 65 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 age is not neces
17907 73 61 72 69 6c 79 20 77 72 69 74 65 61 62 6c 65 sarily writeable
17908 20 73 69 6e 63 65 20 70 43 65 6c 6c 20 6d 69 67 since pCell mig
17909 68 74 20 62 65 20 61 75 78 69 6c 69 61 72 79 0a ht be auxiliary.
1790a 20 20 2a 2a 20 62 75 66 66 65 72 20 73 70 61 63 ** buffer spac
1790b 65 20 74 68 61 74 20 69 73 20 73 65 70 61 72 61 e that is separa
1790c 74 65 20 66 72 6f 6d 20 74 68 65 20 70 50 61 67 te from the pPag
1790d 65 20 62 75 66 66 65 72 20 61 72 65 61 20 2a 2f e buffer area */
1790e 0a 20 20 61 73 73 65 72 74 28 20 70 43 65 6c 6c . assert( pCell
1790f 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c <pPage->aData ||
17910 20 70 43 65 6c 6c 3e 3d 26 70 50 61 67 65 2d 3e pCell>=&pPage->
17911 61 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 aData[pBt->pageS
17912 69 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 ize].
17913 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 || sqlite3Pager
17914 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
17915 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a e->pDbPage) );..
17916 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 /* Fill in the
17917 20 68 65 61 64 65 72 2e 20 2a 2f 0a 20 20 6e 48 header. */. nH
17918 65 61 64 65 72 20 3d 20 30 3b 0a 20 20 69 66 28 eader = 0;. if(
17919 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
1791a 0a 20 20 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 . nHeader +=
1791b 34 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 4;. }. if( pPa
1791c 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a 20 ge->hasData ){.
1791d 20 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70 75 nHeader += pu
1791e 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e tVarint(&pCell[n
1791f 48 65 61 64 65 72 5d 2c 20 6e 44 61 74 61 2b 6e Header], nData+n
17920 5a 65 72 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a Zero);. }else{.
17921 20 20 20 20 6e 44 61 74 61 20 3d 20 6e 5a 65 72 nData = nZer
17922 6f 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 6e 48 65 o = 0;. }. nHe
17923 61 64 65 72 20 2b 3d 20 70 75 74 56 61 72 69 6e ader += putVarin
17924 74 28 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 t(&pCell[nHeader
17925 5d 2c 20 2a 28 75 36 34 2a 29 26 6e 4b 65 79 29 ], *(u64*)&nKey)
17926 3b 0a 20 20 62 74 72 65 65 50 61 72 73 65 43 65 ;. btreeParseCe
17927 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 llPtr(pPage, pCe
17928 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 61 73 ll, &info);. as
17929 73 65 72 74 28 20 69 6e 66 6f 2e 6e 48 65 61 64 sert( info.nHead
1792a 65 72 3d 3d 6e 48 65 61 64 65 72 20 29 3b 0a 20 er==nHeader );.
1792b 20 61 73 73 65 72 74 28 20 69 6e 66 6f 2e 6e 4b assert( info.nK
1792c 65 79 3d 3d 6e 4b 65 79 20 29 3b 0a 20 20 61 73 ey==nKey );. as
1792d 73 65 72 74 28 20 69 6e 66 6f 2e 6e 44 61 74 61 sert( info.nData
1792e 3d 3d 28 75 33 32 29 28 6e 44 61 74 61 2b 6e 5a ==(u32)(nData+nZ
1792f 65 72 6f 29 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 ero) );. . /*
17930 46 69 6c 6c 20 69 6e 20 74 68 65 20 70 61 79 6c Fill in the payl
17931 6f 61 64 20 2a 2f 0a 20 20 6e 50 61 79 6c 6f 61 oad */. nPayloa
17932 64 20 3d 20 6e 44 61 74 61 20 2b 20 6e 5a 65 72 d = nData + nZer
17933 6f 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e o;. if( pPage->
17934 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 70 53 intKey ){. pS
17935 72 63 20 3d 20 70 44 61 74 61 3b 0a 20 20 20 20 rc = pData;.
17936 6e 53 72 63 20 3d 20 6e 44 61 74 61 3b 0a 20 20 nSrc = nData;.
17937 20 20 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 7d nData = 0;. }
17938 65 6c 73 65 7b 20 0a 20 20 20 20 69 66 28 20 4e else{ . if( N
17939 45 56 45 52 28 6e 4b 65 79 3e 30 78 37 66 66 66 EVER(nKey>0x7fff
1793a 66 66 66 66 20 7c 7c 20 70 4b 65 79 3d 3d 30 29 ffff || pKey==0)
1793b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1793c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
1793d 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 BKPT;. }.
1793e 6e 50 61 79 6c 6f 61 64 20 2b 3d 20 28 69 6e 74 nPayload += (int
1793f 29 6e 4b 65 79 3b 0a 20 20 20 20 70 53 72 63 20 )nKey;. pSrc
17940 3d 20 70 4b 65 79 3b 0a 20 20 20 20 6e 53 72 63 = pKey;. nSrc
17941 20 3d 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 = (int)nKey;.
17942 7d 0a 20 20 2a 70 6e 53 69 7a 65 20 3d 20 69 6e }. *pnSize = in
17943 66 6f 2e 6e 53 69 7a 65 3b 0a 20 20 73 70 61 63 fo.nSize;. spac
17944 65 4c 65 66 74 20 3d 20 69 6e 66 6f 2e 6e 4c 6f eLeft = info.nLo
17945 63 61 6c 3b 0a 20 20 70 50 61 79 6c 6f 61 64 20 cal;. pPayload
17946 3d 20 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 = &pCell[nHeader
17947 5d 3b 0a 20 20 70 50 72 69 6f 72 20 3d 20 26 70 ];. pPrior = &p
17948 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 Cell[info.iOverf
17949 6c 6f 77 5d 3b 0a 0a 20 20 77 68 69 6c 65 28 20 low];.. while(
1794a 6e 50 61 79 6c 6f 61 64 3e 30 20 29 7b 0a 20 20 nPayload>0 ){.
1794b 20 20 69 66 28 20 73 70 61 63 65 4c 65 66 74 3d if( spaceLeft=
1794c 3d 30 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 =0 ){.#ifndef SQ
1794d 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
1794e 43 55 55 4d 0a 20 20 20 20 20 20 50 67 6e 6f 20 CUUM. Pgno
1794f 70 67 6e 6f 50 74 72 6d 61 70 20 3d 20 70 67 6e pgnoPtrmap = pgn
17950 6f 4f 76 66 6c 3b 20 2f 2a 20 4f 76 65 72 66 6c oOvfl; /* Overfl
17951 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 2d ow page pointer-
17952 6d 61 70 20 65 6e 74 72 79 20 70 61 67 65 20 2a map entry page *
17953 2f 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d /. if( pBt-
17954 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 >autoVacuum ){.
17955 20 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 do{.
17956 20 20 20 20 20 70 67 6e 6f 4f 76 66 6c 2b 2b 3b pgnoOvfl++;
17957 0a 20 20 20 20 20 20 20 20 7d 20 77 68 69 6c 65 . } while
17958 28 20 0a 20 20 20 20 20 20 20 20 20 20 50 54 52 ( . PTR
17959 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 MAP_ISPAGE(pBt,
1795a 70 67 6e 6f 4f 76 66 6c 29 20 7c 7c 20 70 67 6e pgnoOvfl) || pgn
1795b 6f 4f 76 66 6c 3d 3d 50 45 4e 44 49 4e 47 5f 42 oOvfl==PENDING_B
1795c 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 0a 20 YTE_PAGE(pBt) .
1795d 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 );.
1795e 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 }.#endif. r
1795f 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 c = allocateBtre
17960 65 50 61 67 65 28 70 42 74 2c 20 26 70 4f 76 66 ePage(pBt, &pOvf
17961 6c 2c 20 26 70 67 6e 6f 4f 76 66 6c 2c 20 70 67 l, &pgnoOvfl, pg
17962 6e 6f 4f 76 66 6c 2c 20 30 29 3b 0a 23 69 66 6e noOvfl, 0);.#ifn
17963 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
17964 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 AUTOVACUUM.
17965 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 /* If the datab
17966 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 ase supports aut
17967 6f 2d 76 61 63 75 75 6d 2c 20 61 6e 64 20 74 68 o-vacuum, and th
17968 65 20 73 65 63 6f 6e 64 20 6f 72 20 73 75 62 73 e second or subs
17969 65 71 75 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20 equent. **
1796a 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73 overflow page is
1796b 20 62 65 69 6e 67 20 61 6c 6c 6f 63 61 74 65 64 being allocated
1796c 2c 20 61 64 64 20 61 6e 20 65 6e 74 72 79 20 74 , add an entry t
1796d 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 o the pointer-ma
1796e 70 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 p. ** for t
1796f 68 61 74 20 70 61 67 65 20 6e 6f 77 2e 20 0a 20 hat page now. .
17970 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
17971 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 If this is the
17972 66 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 first overflow p
17973 61 67 65 2c 20 74 68 65 6e 20 77 72 69 74 65 20 age, then write
17974 61 20 70 61 72 74 69 61 6c 20 65 6e 74 72 79 20 a partial entry
17975 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 . ** to the
17976 20 70 6f 69 6e 74 65 72 2d 6d 61 70 2e 20 49 66 pointer-map. If
17977 20 77 65 20 77 72 69 74 65 20 6e 6f 74 68 69 6e we write nothin
17978 67 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 65 g to this pointe
17979 72 2d 6d 61 70 20 73 6c 6f 74 2c 0a 20 20 20 20 r-map slot,.
1797a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 6f 70 ** then the op
1797b 74 69 6d 69 73 74 69 63 20 6f 76 65 72 66 6c 6f timistic overflo
1797c 77 20 63 68 61 69 6e 20 70 72 6f 63 65 73 73 69 w chain processi
1797d 6e 67 20 69 6e 20 63 6c 65 61 72 43 65 6c 6c 28 ng in clearCell(
1797e 29 0a 20 20 20 20 20 20 2a 2a 20 6d 61 79 20 6d ). ** may m
1797f 69 73 69 6e 74 65 72 70 72 65 74 20 74 68 65 20 isinterpret the
17980 75 6e 69 6e 69 74 69 61 6c 69 73 65 64 20 76 61 uninitialised va
17981 6c 75 65 73 20 61 6e 64 20 64 65 6c 65 74 65 20 lues and delete
17982 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 77 72 6f the. ** wro
17983 6e 67 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 ng pages from th
17984 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 e database..
17985 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 */. if( p
17986 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 Bt->autoVacuum &
17987 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 & rc==SQLITE_OK
17988 29 7b 0a 20 20 20 20 20 20 20 20 75 38 20 65 54 ){. u8 eT
17989 79 70 65 20 3d 20 28 70 67 6e 6f 50 74 72 6d 61 ype = (pgnoPtrma
1798a 70 3f 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f p?PTRMAP_OVERFLO
1798b 57 32 3a 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c W2:PTRMAP_OVERFL
1798c 4f 57 31 29 3b 0a 20 20 20 20 20 20 20 20 70 74 OW1);. pt
1798d 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 67 6e rmapPut(pBt, pgn
1798e 6f 4f 76 66 6c 2c 20 65 54 79 70 65 2c 20 70 67 oOvfl, eType, pg
1798f 6e 6f 50 74 72 6d 61 70 2c 20 26 72 63 29 3b 0a noPtrmap, &rc);.
17990 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 if( rc )
17991 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 {. rele
17992 61 73 65 50 61 67 65 28 70 4f 76 66 6c 29 3b 0a asePage(pOvfl);.
17993 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
17994 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 }.#endif. i
17995 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 f( rc ){.
17996 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f releasePage(pTo
17997 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 20 Release);.
17998 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
17999 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 }.. /* I
1799a 66 20 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 f pToRelease is
1799b 6e 6f 74 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 not zero than pP
1799c 72 69 6f 72 20 70 6f 69 6e 74 73 20 69 6e 74 6f rior points into
1799d 20 74 68 65 20 64 61 74 61 20 61 72 65 61 0a 20 the data area.
1799e 20 20 20 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 ** of pToRe
1799f 6c 65 61 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 lease. Make sur
179a0 65 20 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 e pToRelease is
179a1 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e still writeable.
179a2 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 */. assert
179a3 28 20 70 54 6f 52 65 6c 65 61 73 65 3d 3d 30 20 ( pToRelease==0
179a4 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 || sqlite3PagerI
179a5 73 77 72 69 74 65 61 62 6c 65 28 70 54 6f 52 65 swriteable(pToRe
179a6 6c 65 61 73 65 2d 3e 70 44 62 50 61 67 65 29 20 lease->pDbPage)
179a7 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 );.. /* If
179a8 70 50 72 69 6f 72 20 69 73 20 70 61 72 74 20 6f pPrior is part o
179a9 66 20 74 68 65 20 64 61 74 61 20 61 72 65 61 20 f the data area
179aa 6f 66 20 70 50 61 67 65 2c 20 74 68 65 6e 20 6d of pPage, then m
179ab 61 6b 65 20 73 75 72 65 20 70 50 61 67 65 0a 20 ake sure pPage.
179ac 20 20 20 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c ** is still
179ad 20 77 72 69 74 65 61 62 6c 65 20 2a 2f 0a 20 20 writeable */.
179ae 20 20 20 20 61 73 73 65 72 74 28 20 70 50 72 69 assert( pPri
179af 6f 72 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20 or<pPage->aData
179b0 7c 7c 20 70 50 72 69 6f 72 3e 3d 26 70 50 61 67 || pPrior>=&pPag
179b1 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 70 61 e->aData[pBt->pa
179b2 67 65 53 69 7a 65 5d 0a 20 20 20 20 20 20 20 20 geSize].
179b3 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 || sqlite3Pa
179b4 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
179b5 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
179b6 3b 0a 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 ;.. put4byt
179b7 65 28 70 50 72 69 6f 72 2c 20 70 67 6e 6f 4f 76 e(pPrior, pgnoOv
179b8 66 6c 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 fl);. relea
179b9 73 65 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 sePage(pToReleas
179ba 65 29 3b 0a 20 20 20 20 20 20 70 54 6f 52 65 6c e);. pToRel
179bb 65 61 73 65 20 3d 20 70 4f 76 66 6c 3b 0a 20 20 ease = pOvfl;.
179bc 20 20 20 20 70 50 72 69 6f 72 20 3d 20 70 4f 76 pPrior = pOv
179bd 66 6c 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 20 fl->aData;.
179be 20 70 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 put4byte(pPrior
179bf 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 50 61 79 , 0);. pPay
179c0 6c 6f 61 64 20 3d 20 26 70 4f 76 66 6c 2d 3e 61 load = &pOvfl->a
179c1 44 61 74 61 5b 34 5d 3b 0a 20 20 20 20 20 20 73 Data[4];. s
179c2 70 61 63 65 4c 65 66 74 20 3d 20 70 42 74 2d 3e paceLeft = pBt->
179c3 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a usableSize - 4;.
179c4 20 20 20 20 7d 0a 20 20 20 20 6e 20 3d 20 6e 50 }. n = nP
179c5 61 79 6c 6f 61 64 3b 0a 20 20 20 20 69 66 28 20 ayload;. if(
179c6 6e 3e 73 70 61 63 65 4c 65 66 74 20 29 20 6e 20 n>spaceLeft ) n
179c7 3d 20 73 70 61 63 65 4c 65 66 74 3b 0a 0a 20 20 = spaceLeft;..
179c8 20 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65 61 /* If pToRelea
179c9 73 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20 74 se is not zero t
179ca 68 61 6e 20 70 50 61 79 6c 6f 61 64 20 70 6f 69 han pPayload poi
179cb 6e 74 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 nts into the dat
179cc 61 20 61 72 65 61 0a 20 20 20 20 2a 2a 20 6f 66 a area. ** of
179cd 20 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d 61 pToRelease. Ma
179ce 6b 65 20 73 75 72 65 20 70 54 6f 52 65 6c 65 61 ke sure pToRelea
179cf 73 65 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 se is still writ
179d0 65 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 61 73 eable. */. as
179d1 73 65 72 74 28 20 70 54 6f 52 65 6c 65 61 73 65 sert( pToRelease
179d2 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 ==0 || sqlite3Pa
179d3 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
179d4 54 6f 52 65 6c 65 61 73 65 2d 3e 70 44 62 50 61 ToRelease->pDbPa
179d5 67 65 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 ge) );.. /* I
179d6 66 20 70 50 61 79 6c 6f 61 64 20 69 73 20 70 61 f pPayload is pa
179d7 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 61 rt of the data a
179d8 72 65 61 20 6f 66 20 70 50 61 67 65 2c 20 74 68 rea of pPage, th
179d9 65 6e 20 6d 61 6b 65 20 73 75 72 65 20 70 50 61 en make sure pPa
179da 67 65 0a 20 20 20 20 2a 2a 20 69 73 20 73 74 69 ge. ** is sti
179db 6c 6c 20 77 72 69 74 65 61 62 6c 65 20 2a 2f 0a ll writeable */.
179dc 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 79 assert( pPay
179dd 6c 6f 61 64 3c 70 50 61 67 65 2d 3e 61 44 61 74 load<pPage->aDat
179de 61 20 7c 7c 20 70 50 61 79 6c 6f 61 64 3e 3d 26 a || pPayload>=&
179df 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 pPage->aData[pBt
179e0 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 20 ->pageSize].
179e1 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 || sqlit
179e2 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
179e3 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
179e4 65 29 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 6e e) );.. if( n
179e5 53 72 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 Src>0 ){. i
179e6 66 28 20 6e 3e 6e 53 72 63 20 29 20 6e 20 3d 20 f( n>nSrc ) n =
179e7 6e 53 72 63 3b 0a 20 20 20 20 20 20 61 73 73 65 nSrc;. asse
179e8 72 74 28 20 70 53 72 63 20 29 3b 0a 20 20 20 20 rt( pSrc );.
179e9 20 20 6d 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 memcpy(pPayloa
179ea 64 2c 20 70 53 72 63 2c 20 6e 29 3b 0a 20 20 20 d, pSrc, n);.
179eb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 }else{. me
179ec 6d 73 65 74 28 70 50 61 79 6c 6f 61 64 2c 20 30 mset(pPayload, 0
179ed 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 , n);. }.
179ee 6e 50 61 79 6c 6f 61 64 20 2d 3d 20 6e 3b 0a 20 nPayload -= n;.
179ef 20 20 20 70 50 61 79 6c 6f 61 64 20 2b 3d 20 6e pPayload += n
179f0 3b 0a 20 20 20 20 70 53 72 63 20 2b 3d 20 6e 3b ;. pSrc += n;
179f1 0a 20 20 20 20 6e 53 72 63 20 2d 3d 20 6e 3b 0a . nSrc -= n;.
179f2 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 2d 3d spaceLeft -=
179f3 20 6e 3b 0a 20 20 20 20 69 66 28 20 6e 53 72 63 n;. if( nSrc
179f4 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 53 72 ==0 ){. nSr
179f5 63 20 3d 20 6e 44 61 74 61 3b 0a 20 20 20 20 20 c = nData;.
179f6 20 70 53 72 63 20 3d 20 70 44 61 74 61 3b 0a 20 pSrc = pData;.
179f7 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 6c 65 61 }. }. relea
179f8 73 65 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 sePage(pToReleas
179f9 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c e);. return SQL
179fa 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
179fb 20 52 65 6d 6f 76 65 20 74 68 65 20 69 2d 74 68 Remove the i-th
179fc 20 63 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 cell from pPage
179fd 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
179fe 65 66 66 65 63 74 73 20 70 50 61 67 65 20 6f 6e effects pPage on
179ff 6c 79 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 ly..** The cell
17a00 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 66 content is not f
17a01 72 65 65 64 20 6f 72 20 64 65 61 6c 6c 6f 63 61 reed or dealloca
17a02 74 65 64 2e 20 20 49 74 20 69 73 20 61 73 73 75 ted. It is assu
17a03 6d 65 64 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 med that.** the
17a04 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 61 73 cell content has
17a05 20 62 65 65 6e 20 63 6f 70 69 65 64 20 73 6f 6d been copied som
17a06 65 70 6c 61 63 65 20 65 6c 73 65 2e 20 20 54 68 eplace else. Th
17a07 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 0a is routine just.
17a08 2a 2a 20 72 65 6d 6f 76 65 73 20 74 68 65 20 72 ** removes the r
17a09 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 eference to the
17a0a 63 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e cell from pPage.
17a0b 0a 2a 2a 0a 2a 2a 20 22 73 7a 22 20 6d 75 73 74 .**.** "sz" must
17a0c 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f be the number o
17a0d 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 f bytes in the c
17a0e 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ell..*/.static v
17a0f 6f 69 64 20 64 72 6f 70 43 65 6c 6c 28 4d 65 6d oid dropCell(Mem
17a10 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 Page *pPage, int
17a11 20 69 64 78 2c 20 69 6e 74 20 73 7a 2c 20 69 6e idx, int sz, in
17a12 74 20 2a 70 52 43 29 7b 0a 20 20 69 6e 74 20 69 t *pRC){. int i
17a13 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f ; /* Lo
17a14 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
17a15 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 int pc;
17a16 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 63 65 6c /* Offset to cel
17a17 6c 20 63 6f 6e 74 65 6e 74 20 6f 66 20 63 65 6c l content of cel
17a18 6c 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 20 l being deleted
17a19 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 */. u8 *data;
17a1a 20 20 20 20 20 2f 2a 20 70 50 61 67 65 2d 3e 61 /* pPage->a
17a1b 44 61 74 61 20 2a 2f 0a 20 20 75 38 20 2a 70 74 Data */. u8 *pt
17a1c 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 r; /* Use
17a1d 64 20 74 6f 20 6d 6f 76 65 20 62 79 74 65 73 20 d to move bytes
17a1e 61 72 6f 75 6e 64 20 77 69 74 68 69 6e 20 64 61 around within da
17a1f 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 72 63 ta[] */. int rc
17a20 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 ; /* The
17a21 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a return code */.
17a22 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20 int hdr;
17a23 20 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67 20 6f /* Beginning o
17a24 66 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 30 f the header. 0
17a25 20 6d 6f 73 74 20 70 61 67 65 73 2e 20 20 31 30 most pages. 10
17a26 30 20 70 61 67 65 20 31 20 2a 2f 0a 0a 20 20 69 0 page 1 */.. i
17a27 66 28 20 2a 70 52 43 20 29 20 72 65 74 75 72 6e f( *pRC ) return
17a28 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 ;.. assert( idx
17a29 3e 3d 30 20 26 26 20 69 64 78 3c 70 50 61 67 65 >=0 && idx<pPage
17a2a 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73 ->nCell );. ass
17a2b 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 69 7a ert( sz==cellSiz
17a2c 65 28 70 50 61 67 65 2c 20 69 64 78 29 20 29 3b e(pPage, idx) );
17a2d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
17a2e 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
17a2f 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
17a30 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 e) );. assert(
17a31 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
17a32 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
17a33 75 74 65 78 29 20 29 3b 0a 20 20 64 61 74 61 20 utex) );. data
17a34 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a = pPage->aData;.
17a35 20 20 70 74 72 20 3d 20 26 64 61 74 61 5b 70 50 ptr = &data[pP
17a36 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 age->cellOffset
17a37 2b 20 32 2a 69 64 78 5d 3b 0a 20 20 70 63 20 3d + 2*idx];. pc =
17a38 20 67 65 74 32 62 79 74 65 28 70 74 72 29 3b 0a get2byte(ptr);.
17a39 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 hdr = pPage->h
17a3a 64 72 4f 66 66 73 65 74 3b 0a 20 20 74 65 73 74 drOffset;. test
17a3b 63 61 73 65 28 20 70 63 3d 3d 67 65 74 32 62 79 case( pc==get2by
17a3c 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 te(&data[hdr+5])
17a3d 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
17a3e 70 63 2b 73 7a 3d 3d 70 50 61 67 65 2d 3e 70 42 pc+sz==pPage->pB
17a3f 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b t->usableSize );
17a40 0a 20 20 69 66 28 20 70 63 20 3c 20 67 65 74 32 . if( pc < get2
17a41 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
17a42 5d 29 20 7c 7c 20 70 63 2b 73 7a 20 3e 20 70 50 ]) || pc+sz > pP
17a43 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
17a44 53 69 7a 65 20 29 7b 0a 20 20 20 20 2a 70 52 43 Size ){. *pRC
17a45 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
17a46 54 5f 42 4b 50 54 3b 0a 20 20 20 20 72 65 74 75 T_BKPT;. retu
17a47 72 6e 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 66 rn;. }. rc = f
17a48 72 65 65 53 70 61 63 65 28 70 50 61 67 65 2c 20 reeSpace(pPage,
17a49 70 63 2c 20 73 7a 29 3b 0a 20 20 69 66 28 20 72 pc, sz);. if( r
17a4a 63 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20 c ){. *pRC =
17a4b 72 63 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a rc;. return;.
17a4c 20 20 7d 0a 20 20 66 6f 72 28 69 3d 69 64 78 2b }. for(i=idx+
17a4d 31 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 1; i<pPage->nCel
17a4e 6c 3b 20 69 2b 2b 2c 20 70 74 72 2b 3d 32 29 7b l; i++, ptr+=2){
17a4f 0a 20 20 20 20 70 74 72 5b 30 5d 20 3d 20 70 74 . ptr[0] = pt
17a50 72 5b 32 5d 3b 0a 20 20 20 20 70 74 72 5b 31 5d r[2];. ptr[1]
17a51 20 3d 20 70 74 72 5b 33 5d 3b 0a 20 20 7d 0a 20 = ptr[3];. }.
17a52 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 2d 3b pPage->nCell--;
17a53 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 . put2byte(&dat
17a54 61 5b 68 64 72 2b 33 5d 2c 20 70 50 61 67 65 2d a[hdr+3], pPage-
17a55 3e 6e 43 65 6c 6c 29 3b 0a 20 20 70 50 61 67 65 >nCell);. pPage
17a56 2d 3e 6e 46 72 65 65 20 2b 3d 20 32 3b 0a 7d 0a ->nFree += 2;.}.
17a57 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 ./*.** Insert a
17a58 6e 65 77 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67 new cell on pPag
17a59 65 20 61 74 20 63 65 6c 6c 20 69 6e 64 65 78 20 e at cell index
17a5a 22 69 22 2e 20 20 70 43 65 6c 6c 20 70 6f 69 6e "i". pCell poin
17a5b 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6e ts to the.** con
17a5c 74 65 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c tent of the cell
17a5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 ..**.** If the c
17a5e 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c ell content will
17a5f 20 66 69 74 20 6f 6e 20 74 68 65 20 70 61 67 65 fit on the page
17a60 2c 20 74 68 65 6e 20 70 75 74 20 69 74 20 74 68 , then put it th
17a61 65 72 65 2e 20 20 49 66 20 69 74 0a 2a 2a 20 77 ere. If it.** w
17a62 69 6c 6c 20 6e 6f 74 20 66 69 74 2c 20 74 68 65 ill not fit, the
17a63 6e 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 n make a copy of
17a64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
17a65 74 20 69 6e 74 6f 20 70 54 65 6d 70 20 69 66 0a t into pTemp if.
17a66 2a 2a 20 70 54 65 6d 70 20 69 73 20 6e 6f 74 20 ** pTemp is not
17a67 6e 75 6c 6c 2e 20 20 52 65 67 61 72 64 6c 65 73 null. Regardles
17a68 73 20 6f 66 20 70 54 65 6d 70 2c 20 61 6c 6c 6f s of pTemp, allo
17a69 63 61 74 65 20 61 20 6e 65 77 20 65 6e 74 72 79 cate a new entry
17a6a 0a 2a 2a 20 69 6e 20 70 50 61 67 65 2d 3e 61 4f .** in pPage->aO
17a6b 76 66 6c 5b 5d 20 61 6e 64 20 6d 61 6b 65 20 69 vfl[] and make i
17a6c 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 63 t point to the c
17a6d 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 28 65 69 74 ell content (eit
17a6e 68 65 72 0a 2a 2a 20 69 6e 20 70 54 65 6d 70 20 her.** in pTemp
17a6f 6f 72 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 or the original
17a70 70 43 65 6c 6c 29 20 61 6e 64 20 61 6c 73 6f 20 pCell) and also
17a71 72 65 63 6f 72 64 20 69 74 73 20 69 6e 64 65 78 record its index
17a72 2e 20 0a 2a 2a 20 41 6c 6c 6f 63 61 74 69 6e 67 . .** Allocating
17a73 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20 a new entry in
17a74 70 50 61 67 65 2d 3e 61 43 65 6c 6c 5b 5d 20 69 pPage->aCell[] i
17a75 6d 70 6c 69 65 73 20 74 68 61 74 20 0a 2a 2a 20 mplies that .**
17a76 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
17a77 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e is incremented.
17a78 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 53 6b 69 70 20 .**.** If nSkip
17a79 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
17a7a 6e 20 64 6f 20 6e 6f 74 20 63 6f 70 79 20 74 68 n do not copy th
17a7b 65 20 66 69 72 73 74 20 6e 53 6b 69 70 20 62 79 e first nSkip by
17a7c 74 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 63 65 tes of the.** ce
17a7d 6c 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 ll. The caller w
17a7e 69 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 74 68 ill overwrite th
17a7f 65 6d 20 61 66 74 65 72 20 74 68 69 73 20 66 75 em after this fu
17a80 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 20 nction returns.
17a81 49 66 0a 2a 2a 20 6e 53 6b 69 70 20 69 73 20 6e If.** nSkip is n
17a82 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 70 43 on-zero, then pC
17a83 65 6c 6c 20 6d 61 79 20 6e 6f 74 20 70 6f 69 6e ell may not poin
17a84 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20 t to an invalid
17a85 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 memory location
17a86 0a 2a 2a 20 28 62 75 74 20 70 43 65 6c 6c 2b 6e .** (but pCell+n
17a87 53 6b 69 70 20 69 73 20 61 6c 77 61 79 73 20 76 Skip is always v
17a88 61 6c 69 64 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 alid)..*/.static
17a89 20 76 6f 69 64 20 69 6e 73 65 72 74 43 65 6c 6c void insertCell
17a8a 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 (. MemPage *pPa
17a8b 67 65 2c 20 20 20 2f 2a 20 50 61 67 65 20 69 6e ge, /* Page in
17a8c 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 20 to which we are
17a8d 63 6f 70 79 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 copying */. int
17a8e 20 69 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f i, /
17a8f 2a 20 4e 65 77 20 63 65 6c 6c 20 62 65 63 6f 6d * New cell becom
17a90 65 73 20 74 68 65 20 69 2d 74 68 20 63 65 6c 6c es the i-th cell
17a91 20 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a of the page */.
17a92 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 u8 *pCell,
17a93 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f /* Content o
17a94 66 20 74 68 65 20 6e 65 77 20 63 65 6c 6c 20 2a f the new cell *
17a95 2f 0a 20 20 69 6e 74 20 73 7a 2c 20 20 20 20 20 /. int sz,
17a96 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f /* Bytes o
17a97 66 20 63 6f 6e 74 65 6e 74 20 69 6e 20 70 43 65 f content in pCe
17a98 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 54 65 6d ll */. u8 *pTem
17a99 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d p, /* Tem
17a9a 70 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 20 p storage space
17a9b 66 6f 72 20 70 43 65 6c 6c 2c 20 69 66 20 6e 65 for pCell, if ne
17a9c 65 64 65 64 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 eded */. Pgno i
17a9d 43 68 69 6c 64 2c 20 20 20 20 20 20 2f 2a 20 49 Child, /* I
17a9e 66 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 72 65 70 6c f non-zero, repl
17a9f 61 63 65 20 66 69 72 73 74 20 34 20 62 79 74 65 ace first 4 byte
17aa0 73 20 77 69 74 68 20 74 68 69 73 20 76 61 6c 75 s with this valu
17aa1 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 43 20 e */. int *pRC
17aa2 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 64 /* Read
17aa3 20 61 6e 64 20 77 72 69 74 65 20 72 65 74 75 72 and write retur
17aa4 6e 20 63 6f 64 65 20 66 72 6f 6d 20 68 65 72 65 n code from here
17aa5 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 64 78 */.){. int idx
17aa6 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 ; /* Wh
17aa7 65 72 65 20 74 6f 20 77 72 69 74 65 20 6e 65 77 ere to write new
17aa8 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e cell content in
17aa9 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 data[] */. int
17aaa 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f j; /
17aab 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
17aac 2f 0a 20 20 69 6e 74 20 65 6e 64 3b 20 20 20 20 /. int end;
17aad 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 /* First b
17aae 79 74 65 20 70 61 73 74 20 74 68 65 20 6c 61 73 yte past the las
17aaf 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69 t cell pointer i
17ab0 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e n data[] */. in
17ab1 74 20 69 6e 73 3b 20 20 20 20 20 20 20 20 20 20 t ins;
17ab2 2f 2a 20 49 6e 64 65 78 20 69 6e 20 64 61 74 61 /* Index in data
17ab3 5b 5d 20 77 68 65 72 65 20 6e 65 77 20 63 65 6c [] where new cel
17ab4 6c 20 70 6f 69 6e 74 65 72 20 69 73 20 69 6e 73 l pointer is ins
17ab5 65 72 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 63 erted */. int c
17ab6 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20 ellOffset; /*
17ab7 41 64 64 72 65 73 73 20 6f 66 20 66 69 72 73 74 Address of first
17ab8 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69 6e cell pointer in
17ab9 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 75 38 20 data[] */. u8
17aba 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 2f *data; /
17abb 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 * The content of
17abc 20 74 68 65 20 77 68 6f 6c 65 20 70 61 67 65 20 the whole page
17abd 2a 2f 0a 20 20 75 38 20 2a 70 74 72 3b 20 20 20 */. u8 *ptr;
17abe 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 66 /* Used f
17abf 6f 72 20 6d 6f 76 69 6e 67 20 69 6e 66 6f 72 6d or moving inform
17ac0 61 74 69 6f 6e 20 61 72 6f 75 6e 64 20 69 6e 20 ation around in
17ac1 64 61 74 61 5b 5d 20 2a 2f 0a 0a 20 20 69 6e 74 data[] */.. int
17ac2 20 6e 53 6b 69 70 20 3d 20 28 69 43 68 69 6c 64 nSkip = (iChild
17ac3 20 3f 20 34 20 3a 20 30 29 3b 0a 0a 20 20 69 66 ? 4 : 0);.. if
17ac4 28 20 2a 70 52 43 20 29 20 72 65 74 75 72 6e 3b ( *pRC ) return;
17ac5 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 .. assert( i>=0
17ac6 20 26 26 20 69 3c 3d 70 50 61 67 65 2d 3e 6e 43 && i<=pPage->nC
17ac7 65 6c 6c 2b 70 50 61 67 65 2d 3e 6e 4f 76 65 72 ell+pPage->nOver
17ac8 66 6c 6f 77 20 29 3b 0a 20 20 61 73 73 65 72 74 flow );. assert
17ac9 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 3d ( pPage->nCell<=
17aca 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 MX_CELL(pPage->p
17acb 42 74 29 20 26 26 20 4d 58 5f 43 45 4c 4c 28 70 Bt) && MX_CELL(p
17acc 50 61 67 65 2d 3e 70 42 74 29 3c 3d 35 34 36 30 Page->pBt)<=5460
17acd 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
17ace 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3c 3d age->nOverflow<=
17acf 41 72 72 61 79 53 69 7a 65 28 70 50 61 67 65 2d ArraySize(pPage-
17ad0 3e 61 4f 76 66 6c 29 20 29 3b 0a 20 20 61 73 73 >aOvfl) );. ass
17ad1 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 69 7a ert( sz==cellSiz
17ad2 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c ePtr(pPage, pCel
17ad3 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 l) );. assert(
17ad4 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
17ad5 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
17ad6 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 utex) );. if( p
17ad7 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 Page->nOverflow
17ad8 7c 7c 20 73 7a 2b 32 3e 70 50 61 67 65 2d 3e 6e || sz+2>pPage->n
17ad9 46 72 65 65 20 29 7b 0a 20 20 20 20 69 66 28 20 Free ){. if(
17ada 70 54 65 6d 70 20 29 7b 0a 20 20 20 20 20 20 6d pTemp ){. m
17adb 65 6d 63 70 79 28 70 54 65 6d 70 2b 6e 53 6b 69 emcpy(pTemp+nSki
17adc 70 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 p, pCell+nSkip,
17add 73 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 20 20 sz-nSkip);.
17ade 20 70 43 65 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a pCell = pTemp;.
17adf 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 43 }. if( iC
17ae0 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 75 hild ){. pu
17ae1 74 34 62 79 74 65 28 70 43 65 6c 6c 2c 20 69 43 t4byte(pCell, iC
17ae2 68 69 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 hild);. }.
17ae3 20 6a 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 65 j = pPage->nOve
17ae4 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20 20 61 73 73 rflow++;. ass
17ae5 65 72 74 28 20 6a 3c 28 69 6e 74 29 28 73 69 7a ert( j<(int)(siz
17ae6 65 6f 66 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c eof(pPage->aOvfl
17ae7 29 2f 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e )/sizeof(pPage->
17ae8 61 4f 76 66 6c 5b 30 5d 29 29 20 29 3b 0a 20 20 aOvfl[0])) );.
17ae9 20 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a pPage->aOvfl[j
17aea 5d 2e 70 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b ].pCell = pCell;
17aeb 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 4f 76 66 . pPage->aOvf
17aec 6c 5b 6a 5d 2e 69 64 78 20 3d 20 28 75 31 36 29 l[j].idx = (u16)
17aed 69 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 i;. }else{.
17aee 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 int rc = sqlite3
17aef 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 PagerWrite(pPage
17af0 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
17af1 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
17af2 4b 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 43 20 K ){. *pRC
17af3 3d 20 72 63 3b 0a 20 20 20 20 20 20 72 65 74 75 = rc;. retu
17af4 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 rn;. }. as
17af5 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
17af6 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
17af7 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b age->pDbPage) );
17af8 0a 20 20 20 20 64 61 74 61 20 3d 20 70 50 61 67 . data = pPag
17af9 65 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 63 65 e->aData;. ce
17afa 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 llOffset = pPage
17afb 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 ->cellOffset;.
17afc 20 20 65 6e 64 20 3d 20 63 65 6c 6c 4f 66 66 73 end = cellOffs
17afd 65 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 et + 2*pPage->nC
17afe 65 6c 6c 3b 0a 20 20 20 20 69 6e 73 20 3d 20 63 ell;. ins = c
17aff 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 3b ellOffset + 2*i;
17b00 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 . rc = alloca
17b01 74 65 53 70 61 63 65 28 70 50 61 67 65 2c 20 73 teSpace(pPage, s
17b02 7a 2c 20 26 69 64 78 29 3b 0a 20 20 20 20 69 66 z, &idx);. if
17b03 28 20 72 63 20 29 7b 20 2a 70 52 43 20 3d 20 72 ( rc ){ *pRC = r
17b04 63 3b 20 72 65 74 75 72 6e 3b 20 7d 0a 20 20 20 c; return; }.
17b05 20 2f 2a 20 54 68 65 20 61 6c 6c 6f 63 61 74 65 /* The allocate
17b06 53 70 61 63 65 28 29 20 72 6f 75 74 69 6e 65 20 Space() routine
17b07 67 75 61 72 61 6e 74 65 65 73 20 74 68 65 20 66 guarantees the f
17b08 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 70 72 6f ollowing two pro
17b09 70 65 72 74 69 65 73 0a 20 20 20 20 2a 2a 20 69 perties. ** i
17b0a 66 20 69 74 20 72 65 74 75 72 6e 73 20 73 75 63 f it returns suc
17b0b 63 65 73 73 20 2a 2f 0a 20 20 20 20 61 73 73 65 cess */. asse
17b0c 72 74 28 20 69 64 78 20 3e 3d 20 65 6e 64 2b 32 rt( idx >= end+2
17b0d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
17b0e 69 64 78 2b 73 7a 20 3c 3d 20 70 50 61 67 65 2d idx+sz <= pPage-
17b0f 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 >pBt->usableSize
17b10 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e );. pPage->n
17b11 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 70 50 61 67 Cell++;. pPag
17b12 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 75 31 36 e->nFree -= (u16
17b13 29 28 32 20 2b 20 73 7a 29 3b 0a 20 20 20 20 6d )(2 + sz);. m
17b14 65 6d 63 70 79 28 26 64 61 74 61 5b 69 64 78 2b emcpy(&data[idx+
17b15 6e 53 6b 69 70 5d 2c 20 70 43 65 6c 6c 2b 6e 53 nSkip], pCell+nS
17b16 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69 70 29 3b 0a kip, sz-nSkip);.
17b17 20 20 20 20 69 66 28 20 69 43 68 69 6c 64 20 29 if( iChild )
17b18 7b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 {. put4byte
17b19 28 26 64 61 74 61 5b 69 64 78 5d 2c 20 69 43 68 (&data[idx], iCh
17b1a 69 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ild);. }.
17b1b 66 6f 72 28 6a 3d 65 6e 64 2c 20 70 74 72 3d 26 for(j=end, ptr=&
17b1c 64 61 74 61 5b 6a 5d 3b 20 6a 3e 69 6e 73 3b 20 data[j]; j>ins;
17b1d 6a 2d 3d 32 2c 20 70 74 72 2d 3d 32 29 7b 0a 20 j-=2, ptr-=2){.
17b1e 20 20 20 20 20 70 74 72 5b 30 5d 20 3d 20 70 74 ptr[0] = pt
17b1f 72 5b 2d 32 5d 3b 0a 20 20 20 20 20 20 70 74 72 r[-2];. ptr
17b20 5b 31 5d 20 3d 20 70 74 72 5b 2d 31 5d 3b 0a 20 [1] = ptr[-1];.
17b21 20 20 20 7d 0a 20 20 20 20 70 75 74 32 62 79 74 }. put2byt
17b22 65 28 26 64 61 74 61 5b 69 6e 73 5d 2c 20 69 64 e(&data[ins], id
17b23 78 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 x);. put2byte
17b24 28 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 (&data[pPage->hd
17b25 72 4f 66 66 73 65 74 2b 33 5d 2c 20 70 50 61 67 rOffset+3], pPag
17b26 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 e->nCell);.#ifnd
17b27 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
17b28 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 UTOVACUUM. if
17b29 28 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 61 75 ( pPage->pBt->au
17b2a 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 toVacuum ){.
17b2b 20 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20 6d 61 /* The cell ma
17b2c 79 20 63 6f 6e 74 61 69 6e 20 61 20 70 6f 69 6e y contain a poin
17b2d 74 65 72 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c ter to an overfl
17b2e 6f 77 20 70 61 67 65 2e 20 49 66 20 73 6f 2c 20 ow page. If so,
17b2f 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 74 write. ** t
17b30 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 he entry for the
17b31 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 overflow page i
17b32 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 nto the pointer
17b33 6d 61 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 map.. */.
17b34 20 20 20 20 70 74 72 6d 61 70 50 75 74 4f 76 66 ptrmapPutOvf
17b35 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c lPtr(pPage, pCel
17b36 6c 2c 20 70 52 43 29 3b 0a 20 20 20 20 7d 0a 23 l, pRC);. }.#
17b37 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a endif. }.}../*.
17b38 2a 2a 20 41 64 64 20 61 20 6c 69 73 74 20 6f 66 ** Add a list of
17b39 20 63 65 6c 6c 73 20 74 6f 20 61 20 70 61 67 65 cells to a page
17b3a 2e 20 20 54 68 65 20 70 61 67 65 20 73 68 6f 75 . The page shou
17b3b 6c 64 20 62 65 20 69 6e 69 74 69 61 6c 6c 79 20 ld be initially
17b3c 65 6d 70 74 79 2e 0a 2a 2a 20 54 68 65 20 63 65 empty..** The ce
17b3d 6c 6c 73 20 61 72 65 20 67 75 61 72 61 6e 74 65 lls are guarante
17b3e 65 64 20 74 6f 20 66 69 74 20 6f 6e 20 74 68 65 ed to fit on the
17b3f 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 page..*/.static
17b40 20 76 6f 69 64 20 61 73 73 65 6d 62 6c 65 50 61 void assemblePa
17b41 67 65 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 ge(. MemPage *p
17b42 50 61 67 65 2c 20 20 20 2f 2a 20 54 68 65 20 70 Page, /* The p
17b43 61 67 65 20 74 6f 20 62 65 20 61 73 73 65 6d 62 age to be assemb
17b44 6c 69 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 lied */. int nC
17b45 65 6c 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 ell, /* T
17b46 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c he number of cel
17b47 6c 73 20 74 6f 20 61 64 64 20 74 6f 20 74 68 69 ls to add to thi
17b48 73 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a s page */. u8 *
17b49 2a 61 70 43 65 6c 6c 2c 20 20 20 20 20 20 2f 2a *apCell, /*
17b4a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 63 65 6c Pointers to cel
17b4b 6c 20 62 6f 64 69 65 73 20 2a 2f 0a 20 20 75 31 l bodies */. u1
17b4c 36 20 2a 61 53 69 7a 65 20 20 20 20 20 20 20 20 6 *aSize
17b4d 2f 2a 20 53 69 7a 65 73 20 6f 66 20 74 68 65 20 /* Sizes of the
17b4e 63 65 6c 6c 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e cells */.){. in
17b4f 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 t i;
17b50 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
17b51 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 70 74 */. u8 *pCellpt
17b52 72 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 r; /* Addres
17b53 73 20 6f 66 20 6e 65 78 74 20 63 65 6c 6c 20 70 s of next cell p
17b54 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 ointer */. int
17b55 63 65 6c 6c 62 6f 64 79 3b 20 20 20 20 20 2f 2a cellbody; /*
17b56 20 41 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 Address of next
17b57 20 63 65 6c 6c 20 62 6f 64 79 20 2a 2f 0a 20 20 cell body */.
17b58 75 38 20 2a 20 63 6f 6e 73 74 20 64 61 74 61 20 u8 * const data
17b59 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 20 = pPage->aData;
17b5a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
17b5b 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 61 20 66 ointer to data f
17b5c 6f 72 20 70 50 61 67 65 20 2a 2f 0a 20 20 63 6f or pPage */. co
17b5d 6e 73 74 20 69 6e 74 20 68 64 72 20 3d 20 70 50 nst int hdr = pP
17b5e 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 20 age->hdrOffset;
17b5f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 /* Off
17b60 73 65 74 20 6f 66 20 68 65 61 64 65 72 20 6f 6e set of header on
17b61 20 70 50 61 67 65 20 2a 2f 0a 20 20 63 6f 6e 73 pPage */. cons
17b62 74 20 69 6e 74 20 6e 55 73 61 62 6c 65 20 3d 20 t int nUsable =
17b63 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
17b64 6c 65 53 69 7a 65 3b 20 2f 2a 20 55 73 61 62 6c leSize; /* Usabl
17b65 65 20 73 69 7a 65 20 6f 66 20 70 61 67 65 20 2a e size of page *
17b66 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 /.. assert( pPa
17b67 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 ge->nOverflow==0
17b68 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
17b69 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
17b6a 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
17b6b 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
17b6c 20 6e 43 65 6c 6c 3e 3d 30 20 26 26 20 6e 43 65 nCell>=0 && nCe
17b6d 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c 28 70 50 61 67 ll<=MX_CELL(pPag
17b6e 65 2d 3e 70 42 74 29 20 26 26 20 4d 58 5f 43 45 e->pBt) && MX_CE
17b6f 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 3c 3d LL(pPage->pBt)<=
17b70 35 34 36 30 20 29 3b 0a 20 20 61 73 73 65 72 74 5460 );. assert
17b71 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
17b72 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d writeable(pPage-
17b73 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 >pDbPage) );..
17b74 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 /* Check that th
17b75 65 20 70 61 67 65 20 68 61 73 20 6a 75 73 74 20 e page has just
17b76 62 65 65 6e 20 7a 65 72 6f 65 64 20 62 79 20 7a been zeroed by z
17b77 65 72 6f 50 61 67 65 28 29 20 2a 2f 0a 20 20 61 eroPage() */. a
17b78 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 ssert( pPage->nC
17b79 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 ell==0 );. asse
17b7a 72 74 28 20 67 65 74 32 62 79 74 65 28 26 64 61 rt( get2byte(&da
17b7b 74 61 5b 68 64 72 2b 35 5d 29 3d 3d 6e 55 73 61 ta[hdr+5])==nUsa
17b7c 62 6c 65 20 29 3b 0a 0a 20 20 70 43 65 6c 6c 70 ble );.. pCellp
17b7d 74 72 20 3d 20 26 64 61 74 61 5b 70 50 61 67 65 tr = &data[pPage
17b7e 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 6e ->cellOffset + n
17b7f 43 65 6c 6c 2a 32 5d 3b 0a 20 20 63 65 6c 6c 62 Cell*2];. cellb
17b80 6f 64 79 20 3d 20 6e 55 73 61 62 6c 65 3b 0a 20 ody = nUsable;.
17b81 20 66 6f 72 28 69 3d 6e 43 65 6c 6c 2d 31 3b 20 for(i=nCell-1;
17b82 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 i>=0; i--){.
17b83 70 43 65 6c 6c 70 74 72 20 2d 3d 20 32 3b 0a 20 pCellptr -= 2;.
17b84 20 20 20 63 65 6c 6c 62 6f 64 79 20 2d 3d 20 61 cellbody -= a
17b85 53 69 7a 65 5b 69 5d 3b 0a 20 20 20 20 70 75 74 Size[i];. put
17b86 32 62 79 74 65 28 70 43 65 6c 6c 70 74 72 2c 20 2byte(pCellptr,
17b87 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 20 20 6d cellbody);. m
17b88 65 6d 63 70 79 28 26 64 61 74 61 5b 63 65 6c 6c emcpy(&data[cell
17b89 62 6f 64 79 5d 2c 20 61 70 43 65 6c 6c 5b 69 5d body], apCell[i]
17b8a 2c 20 61 53 69 7a 65 5b 69 5d 29 3b 0a 20 20 7d , aSize[i]);. }
17b8b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 . put2byte(&dat
17b8c 61 5b 68 64 72 2b 33 5d 2c 20 6e 43 65 6c 6c 29 a[hdr+3], nCell)
17b8d 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 ;. put2byte(&da
17b8e 74 61 5b 68 64 72 2b 35 5d 2c 20 63 65 6c 6c 62 ta[hdr+5], cellb
17b8f 6f 64 79 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e ody);. pPage->n
17b90 46 72 65 65 20 2d 3d 20 28 6e 43 65 6c 6c 2a 32 Free -= (nCell*2
17b91 20 2b 20 6e 55 73 61 62 6c 65 20 2d 20 63 65 6c + nUsable - cel
17b92 6c 62 6f 64 79 29 3b 0a 20 20 70 50 61 67 65 2d lbody);. pPage-
17b93 3e 6e 43 65 6c 6c 20 3d 20 28 75 31 36 29 6e 43 >nCell = (u16)nC
17b94 65 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 ell;.}../*.** Th
17b95 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 61 72 61 e following para
17b96 6d 65 74 65 72 73 20 64 65 74 65 72 6d 69 6e 65 meters determine
17b97 20 68 6f 77 20 6d 61 6e 79 20 61 64 6a 61 63 65 how many adjace
17b98 6e 74 20 70 61 67 65 73 20 67 65 74 20 69 6e 76 nt pages get inv
17b99 6f 6c 76 65 64 0a 2a 2a 20 69 6e 20 61 20 62 61 olved.** in a ba
17b9a 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f lancing operatio
17b9b 6e 2e 20 20 4e 4e 20 69 73 20 74 68 65 20 6e 75 n. NN is the nu
17b9c 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72 mber of neighbor
17b9d 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 s on either side
17b9e 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20 .** of the page
17b9f 74 68 61 74 20 70 61 72 74 69 63 69 70 61 74 65 that participate
17ba0 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 69 6e in the balancin
17ba1 67 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4e 42 g operation. NB
17ba2 20 69 73 20 74 68 65 0a 2a 2a 20 74 6f 74 61 6c is the.** total
17ba3 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
17ba4 20 74 68 61 74 20 70 61 72 74 69 63 69 70 61 74 that participat
17ba5 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 e, including the
17ba6 20 74 61 72 67 65 74 20 70 61 67 65 20 61 6e 64 target page and
17ba7 0a 2a 2a 20 4e 4e 20 6e 65 69 67 68 62 6f 72 73 .** NN neighbors
17ba8 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 2e on either side.
17ba9 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69 6e 69 6d .**.** The minim
17baa 75 6d 20 76 61 6c 75 65 20 6f 66 20 4e 4e 20 69 um value of NN i
17bab 73 20 31 20 28 6f 66 20 63 6f 75 72 73 65 29 2e s 1 (of course).
17bac 20 20 49 6e 63 72 65 61 73 69 6e 67 20 4e 4e 20 Increasing NN
17bad 61 62 6f 76 65 20 31 0a 2a 2a 20 28 74 6f 20 32 above 1.** (to 2
17bae 20 6f 72 20 33 29 20 67 69 76 65 73 20 61 20 6d or 3) gives a m
17baf 6f 64 65 73 74 20 69 6d 70 72 6f 76 65 6d 65 6e odest improvemen
17bb0 74 20 69 6e 20 53 45 4c 45 43 54 20 61 6e 64 20 t in SELECT and
17bb1 44 45 4c 45 54 45 20 70 65 72 66 6f 72 6d 61 6e DELETE performan
17bb2 63 65 0a 2a 2a 20 69 6e 20 65 78 63 68 61 6e 67 ce.** in exchang
17bb3 65 20 66 6f 72 20 61 20 6c 61 72 67 65 72 20 64 e for a larger d
17bb4 65 67 72 61 64 61 74 69 6f 6e 20 69 6e 20 49 4e egradation in IN
17bb5 53 45 52 54 20 61 6e 64 20 55 50 44 41 54 45 20 SERT and UPDATE
17bb6 70 65 72 66 6f 72 6d 61 6e 63 65 2e 0a 2a 2a 20 performance..**
17bb7 54 68 65 20 76 61 6c 75 65 20 6f 66 20 4e 4e 20 The value of NN
17bb8 61 70 70 65 61 72 73 20 74 6f 20 67 69 76 65 20 appears to give
17bb9 74 68 65 20 62 65 73 74 20 72 65 73 75 6c 74 73 the best results
17bba 20 6f 76 65 72 61 6c 6c 2e 0a 2a 2f 0a 23 64 65 overall..*/.#de
17bbb 66 69 6e 65 20 4e 4e 20 31 20 20 20 20 20 20 20 fine NN 1
17bbc 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
17bbd 6f 66 20 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20 of neighbors on
17bbe 65 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 70 either side of p
17bbf 50 61 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 Page */.#define
17bc0 4e 42 20 28 4e 4e 2a 32 2b 31 29 20 20 20 20 20 NB (NN*2+1)
17bc1 20 2f 2a 20 54 6f 74 61 6c 20 70 61 67 65 73 20 /* Total pages
17bc2 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74 68 65 20 involved in the
17bc3 62 61 6c 61 6e 63 65 20 2a 2f 0a 0a 0a 23 69 66 balance */...#if
17bc4 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
17bc5 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a 2f 2a _QUICKBALANCE./*
17bc6 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e .** This version
17bc7 20 6f 66 20 62 61 6c 61 6e 63 65 28 29 20 68 61 of balance() ha
17bc8 6e 64 6c 65 73 20 74 68 65 20 63 6f 6d 6d 6f 6e ndles the common
17bc9 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 77 68 special case wh
17bca 65 72 65 0a 2a 2a 20 61 20 6e 65 77 20 65 6e 74 ere.** a new ent
17bcb 72 79 20 69 73 20 62 65 69 6e 67 20 69 6e 73 65 ry is being inse
17bcc 72 74 65 64 20 6f 6e 20 74 68 65 20 65 78 74 72 rted on the extr
17bcd 65 6d 65 20 72 69 67 68 74 2d 65 6e 64 20 6f 66 eme right-end of
17bce 20 74 68 65 0a 2a 2a 20 74 72 65 65 2c 20 69 6e the.** tree, in
17bcf 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 77 68 other words, wh
17bd0 65 6e 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 en the new entry
17bd1 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 will become the
17bd2 20 6c 61 72 67 65 73 74 0a 2a 2a 20 65 6e 74 72 largest.** entr
17bd3 79 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a y in the tree..*
17bd4 2a 0a 2a 2a 20 49 6e 73 74 65 61 64 20 6f 66 20 *.** Instead of
17bd5 74 72 79 69 6e 67 20 74 6f 20 62 61 6c 61 6e 63 trying to balanc
17bd6 65 20 74 68 65 20 33 20 72 69 67 68 74 2d 6d 6f e the 3 right-mo
17bd7 73 74 20 6c 65 61 66 20 70 61 67 65 73 2c 20 6a st leaf pages, j
17bd8 75 73 74 20 61 64 64 0a 2a 2a 20 61 20 6e 65 77 ust add.** a new
17bd9 20 70 61 67 65 20 74 6f 20 74 68 65 20 72 69 67 page to the rig
17bda 68 74 2d 68 61 6e 64 20 73 69 64 65 20 61 6e 64 ht-hand side and
17bdb 20 70 75 74 20 74 68 65 20 6f 6e 65 20 6e 65 77 put the one new
17bdc 20 65 6e 74 72 79 20 69 6e 0a 2a 2a 20 74 68 61 entry in.** tha
17bdd 74 20 70 61 67 65 2e 20 20 54 68 69 73 20 6c 65 t page. This le
17bde 61 76 65 73 20 74 68 65 20 72 69 67 68 74 20 73 aves the right s
17bdf 69 64 65 20 6f 66 20 74 68 65 20 74 72 65 65 20 ide of the tree
17be0 73 6f 6d 65 77 68 61 74 0a 2a 2a 20 75 6e 62 61 somewhat.** unba
17be1 6c 61 6e 63 65 64 2e 20 20 42 75 74 20 6f 64 64 lanced. But odd
17be2 73 20 61 72 65 20 74 68 61 74 20 77 65 20 77 69 s are that we wi
17be3 6c 6c 20 62 65 20 69 6e 73 65 72 74 69 6e 67 20 ll be inserting
17be4 6e 65 77 20 65 6e 74 72 69 65 73 0a 2a 2a 20 61 new entries.** a
17be5 74 20 74 68 65 20 65 6e 64 20 73 6f 6f 6e 20 61 t the end soon a
17be6 66 74 65 72 77 61 72 64 73 20 73 6f 20 74 68 65 fterwards so the
17be7 20 6e 65 61 72 6c 79 20 65 6d 70 74 79 20 70 61 nearly empty pa
17be8 67 65 20 77 69 6c 6c 20 71 75 69 63 6b 6c 79 0a ge will quickly.
17be9 2a 2a 20 66 69 6c 6c 20 75 70 2e 20 20 4f 6e 20 ** fill up. On
17bea 61 76 65 72 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 average..**.** p
17beb 50 61 67 65 20 69 73 20 74 68 65 20 6c 65 61 66 Page is the leaf
17bec 20 70 61 67 65 20 77 68 69 63 68 20 69 73 20 74 page which is t
17bed 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 70 61 he right-most pa
17bee 67 65 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a ge in the tree..
17bef 2a 2a 20 70 50 61 72 65 6e 74 20 69 73 20 69 74 ** pParent is it
17bf0 73 20 70 61 72 65 6e 74 2e 20 20 70 50 61 67 65 s parent. pPage
17bf1 20 6d 75 73 74 20 68 61 76 65 20 61 20 73 69 6e must have a sin
17bf2 67 6c 65 20 6f 76 65 72 66 6c 6f 77 20 65 6e 74 gle overflow ent
17bf3 72 79 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 61 ry.** which is a
17bf4 6c 73 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f lso the right-mo
17bf5 73 74 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 st entry on the
17bf6 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 page..**.** The
17bf7 70 53 70 61 63 65 20 62 75 66 66 65 72 20 69 73 pSpace buffer is
17bf8 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 61 used to store a
17bf9 20 74 65 6d 70 6f 72 61 72 79 20 63 6f 70 79 20 temporary copy
17bfa 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 0a 2a of the divider.*
17bfb 2a 20 63 65 6c 6c 20 74 68 61 74 20 77 69 6c 6c * cell that will
17bfc 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 be inserted int
17bfd 6f 20 70 50 61 72 65 6e 74 2e 20 53 75 63 68 20 o pParent. Such
17bfe 61 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73 20 a cell consists
17bff 6f 66 20 61 20 34 0a 2a 2a 20 62 79 74 65 20 70 of a 4.** byte p
17c00 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 6c 6c 6f age number follo
17c01 77 65 64 20 62 79 20 61 20 76 61 72 69 61 62 6c wed by a variabl
17c02 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 e length integer
17c03 2e 20 49 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f . In other.** wo
17c04 72 64 73 2c 20 61 74 20 6d 6f 73 74 20 31 33 20 rds, at most 13
17c05 62 79 74 65 73 2e 20 48 65 6e 63 65 20 74 68 65 bytes. Hence the
17c06 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20 6d pSpace buffer m
17c07 75 73 74 20 62 65 20 61 74 0a 2a 2a 20 6c 65 61 ust be at.** lea
17c08 73 74 20 31 33 20 62 79 74 65 73 20 69 6e 20 73 st 13 bytes in s
17c09 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ize..*/.static i
17c0a 6e 74 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b nt balance_quick
17c0b 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e (MemPage *pParen
17c0c 74 2c 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 t, MemPage *pPag
17c0d 65 2c 20 75 38 20 2a 70 53 70 61 63 65 29 7b 0a e, u8 *pSpace){.
17c0e 20 20 42 74 53 68 61 72 65 64 20 2a 63 6f 6e 73 BtShared *cons
17c0f 74 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 t pBt = pPage->p
17c10 42 74 3b 20 20 20 20 2f 2a 20 42 2d 54 72 65 65 Bt; /* B-Tree
17c11 20 44 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 4d Database */. M
17c12 65 6d 50 61 67 65 20 2a 70 4e 65 77 3b 20 20 20 emPage *pNew;
17c13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17c14 20 20 20 20 2f 2a 20 4e 65 77 6c 79 20 61 6c 6c /* Newly all
17c15 6f 63 61 74 65 64 20 70 61 67 65 20 2a 2f 0a 20 ocated page */.
17c16 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
17c17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17c18 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
17c19 43 6f 64 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 Code */. Pgno p
17c1a 67 6e 6f 4e 65 77 3b 20 20 20 20 20 20 20 20 20 gnoNew;
17c1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
17c1c 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 * Page number of
17c1d 20 70 4e 65 77 20 2a 2f 0a 0a 20 20 61 73 73 65 pNew */.. asse
17c1e 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
17c1f 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
17c20 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
17c21 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
17c22 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
17c23 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 Parent->pDbPage)
17c24 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
17c25 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d age->nOverflow==
17c26 31 20 29 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 1 );.. if( pPag
17c27 65 2d 3e 6e 43 65 6c 6c 3c 3d 30 20 29 20 72 65 e->nCell<=0 ) re
17c28 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
17c29 55 50 54 5f 42 4b 50 54 3b 0a 0a 20 20 2f 2a 20 UPT_BKPT;.. /*
17c2a 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 Allocate a new p
17c2b 61 67 65 2e 20 54 68 69 73 20 70 61 67 65 20 77 age. This page w
17c2c 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 72 ill become the r
17c2d 69 67 68 74 2d 73 69 62 6c 69 6e 67 20 6f 66 20 ight-sibling of
17c2e 0a 20 20 2a 2a 20 70 50 61 67 65 2e 20 4d 61 6b . ** pPage. Mak
17c2f 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 e the parent pag
17c30 65 20 77 72 69 74 61 62 6c 65 2c 20 73 6f 20 74 e writable, so t
17c31 68 61 74 20 74 68 65 20 6e 65 77 20 64 69 76 69 hat the new divi
17c32 64 65 72 20 63 65 6c 6c 0a 20 20 2a 2a 20 6d 61 der cell. ** ma
17c33 79 20 62 65 20 69 6e 73 65 72 74 65 64 2e 20 49 y be inserted. I
17c34 66 20 62 6f 74 68 20 74 68 65 73 65 20 6f 70 65 f both these ope
17c35 72 61 74 69 6f 6e 73 20 61 72 65 20 73 75 63 63 rations are succ
17c36 65 73 73 66 75 6c 2c 20 70 72 6f 63 65 65 64 2e essful, proceed.
17c37 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 61 6c 6c . */. rc = all
17c38 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 ocateBtreePage(p
17c39 42 74 2c 20 26 70 4e 65 77 2c 20 26 70 67 6e 6f Bt, &pNew, &pgno
17c3a 4e 65 77 2c 20 30 2c 20 30 29 3b 0a 0a 20 20 69 New, 0, 0);.. i
17c3b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
17c3c 20 29 7b 0a 0a 20 20 20 20 75 38 20 2a 70 4f 75 ){.. u8 *pOu
17c3d 74 20 3d 20 26 70 53 70 61 63 65 5b 34 5d 3b 0a t = &pSpace[4];.
17c3e 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 u8 *pCell =
17c3f 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 2e pPage->aOvfl[0].
17c40 70 43 65 6c 6c 3b 0a 20 20 20 20 75 31 36 20 73 pCell;. u16 s
17c41 7a 43 65 6c 6c 20 3d 20 63 65 6c 6c 53 69 7a 65 zCell = cellSize
17c42 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c Ptr(pPage, pCell
17c43 29 3b 0a 20 20 20 20 75 38 20 2a 70 53 74 6f 70 );. u8 *pStop
17c44 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 ;.. assert( s
17c45 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
17c46 74 65 61 62 6c 65 28 70 4e 65 77 2d 3e 70 44 62 teable(pNew->pDb
17c47 50 61 67 65 29 20 29 3b 0a 20 20 20 20 61 73 73 Page) );. ass
17c48 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74 ert( pPage->aDat
17c49 61 5b 30 5d 3d 3d 28 50 54 46 5f 49 4e 54 4b 45 a[0]==(PTF_INTKE
17c4a 59 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 7c 50 Y|PTF_LEAFDATA|P
17c4b 54 46 5f 4c 45 41 46 29 20 29 3b 0a 20 20 20 20 TF_LEAF) );.
17c4c 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c 20 50 zeroPage(pNew, P
17c4d 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 TF_INTKEY|PTF_LE
17c4e 41 46 44 41 54 41 7c 50 54 46 5f 4c 45 41 46 29 AFDATA|PTF_LEAF)
17c4f 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c 65 50 61 ;. assemblePa
17c50 67 65 28 70 4e 65 77 2c 20 31 2c 20 26 70 43 65 ge(pNew, 1, &pCe
17c51 6c 6c 2c 20 26 73 7a 43 65 6c 6c 29 3b 0a 0a 20 ll, &szCell);..
17c52 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 /* If this is
17c53 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 an auto-vacuum
17c54 64 61 74 61 62 61 73 65 2c 20 75 70 64 61 74 65 database, update
17c55 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
17c56 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 65 6e 74 . ** with ent
17c57 72 69 65 73 20 66 6f 72 20 74 68 65 20 6e 65 77 ries for the new
17c58 20 70 61 67 65 2c 20 61 6e 64 20 61 6e 79 20 70 page, and any p
17c59 6f 69 6e 74 65 72 20 66 72 6f 6d 20 74 68 65 20 ointer from the
17c5a 0a 20 20 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 . ** cell on
17c5b 74 68 65 20 70 61 67 65 20 74 6f 20 61 6e 20 6f the page to an o
17c5c 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66 verflow page. If
17c5d 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 either of these
17c5e 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f . ** operatio
17c5f 6e 73 20 66 61 69 6c 73 2c 20 74 68 65 20 72 65 ns fails, the re
17c60 74 75 72 6e 20 63 6f 64 65 20 69 73 20 73 65 74 turn code is set
17c61 2c 20 62 75 74 20 74 68 65 20 63 6f 6e 74 65 6e , but the conten
17c62 74 73 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 ts. ** of the
17c63 20 70 61 72 65 6e 74 20 70 61 67 65 20 61 72 65 parent page are
17c64 20 73 74 69 6c 6c 20 6d 61 6e 69 70 75 6c 61 74 still manipulat
17c65 65 64 20 62 79 20 74 68 68 20 63 6f 64 65 20 62 ed by thh code b
17c66 65 6c 6f 77 2e 0a 20 20 20 20 2a 2a 20 54 68 61 elow.. ** Tha
17c67 74 20 69 73 20 4f 6b 2c 20 61 74 20 74 68 69 73 t is Ok, at this
17c68 20 70 6f 69 6e 74 20 74 68 65 20 70 61 72 65 6e point the paren
17c69 74 20 70 61 67 65 20 69 73 20 67 75 61 72 61 6e t page is guaran
17c6a 74 65 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 62 teed to. ** b
17c6b 65 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 e marked as dirt
17c6c 79 2e 20 52 65 74 75 72 6e 69 6e 67 20 61 6e 20 y. Returning an
17c6d 65 72 72 6f 72 20 63 6f 64 65 20 77 69 6c 6c 20 error code will
17c6e 63 61 75 73 65 20 61 0a 20 20 20 20 2a 2a 20 72 cause a. ** r
17c6f 6f 6c 6c 62 61 63 6b 2c 20 75 6e 64 6f 69 6e 67 ollback, undoing
17c70 20 61 6e 79 20 63 68 61 6e 67 65 73 20 6d 61 64 any changes mad
17c71 65 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 e to the parent
17c72 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 page.. */.
17c73 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 if( ISAUTOVACUU
17c74 4d 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 M ){. ptrma
17c75 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 4e 65 pPut(pBt, pgnoNe
17c76 77 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c w, PTRMAP_BTREE,
17c77 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 2c 20 pParent->pgno,
17c78 26 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 &rc);. if(
17c79 73 7a 43 65 6c 6c 3e 70 4e 65 77 2d 3e 6d 69 6e szCell>pNew->min
17c7a 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 Local ){.
17c7b 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 ptrmapPutOvflPt
17c7c 72 28 70 4e 65 77 2c 20 70 43 65 6c 6c 2c 20 26 r(pNew, pCell, &
17c7d 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc);. }.
17c7e 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 43 72 65 }. . /* Cre
17c7f 61 74 65 20 61 20 64 69 76 69 64 65 72 20 63 65 ate a divider ce
17c80 6c 6c 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 ll to insert int
17c81 6f 20 70 50 61 72 65 6e 74 2e 20 54 68 65 20 64 o pParent. The d
17c82 69 76 69 64 65 72 20 63 65 6c 6c 0a 20 20 20 20 ivider cell.
17c83 2a 2a 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 ** consists of a
17c84 20 34 2d 62 79 74 65 20 70 61 67 65 20 6e 75 6d 4-byte page num
17c85 62 65 72 20 28 74 68 65 20 70 61 67 65 20 6e 75 ber (the page nu
17c86 6d 62 65 72 20 6f 66 20 70 50 61 67 65 29 20 61 mber of pPage) a
17c87 6e 64 0a 20 20 20 20 2a 2a 20 61 20 76 61 72 69 nd. ** a vari
17c88 61 62 6c 65 20 6c 65 6e 67 74 68 20 6b 65 79 20 able length key
17c89 76 61 6c 75 65 20 28 77 68 69 63 68 20 6d 75 73 value (which mus
17c8a 74 20 62 65 20 74 68 65 20 73 61 6d 65 20 76 61 t be the same va
17c8b 6c 75 65 20 61 73 20 74 68 65 0a 20 20 20 20 2a lue as the. *
17c8c 2a 20 6c 61 72 67 65 73 74 20 6b 65 79 20 6f 6e * largest key on
17c8d 20 70 50 61 67 65 29 2e 0a 20 20 20 20 2a 2a 0a pPage).. **.
17c8e 20 20 20 20 2a 2a 20 54 6f 20 66 69 6e 64 20 74 ** To find t
17c8f 68 65 20 6c 61 72 67 65 73 74 20 6b 65 79 20 76 he largest key v
17c90 61 6c 75 65 20 6f 6e 20 70 50 61 67 65 2c 20 66 alue on pPage, f
17c91 69 72 73 74 20 66 69 6e 64 20 74 68 65 20 72 69 irst find the ri
17c92 67 68 74 2d 6d 6f 73 74 20 0a 20 20 20 20 2a 2a ght-most . **
17c93 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67 65 2e 20 cell on pPage.
17c94 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 66 69 The first two fi
17c95 65 6c 64 73 20 6f 66 20 74 68 69 73 20 63 65 6c elds of this cel
17c96 6c 20 61 72 65 20 74 68 65 20 0a 20 20 20 20 2a l are the . *
17c97 2a 20 72 65 63 6f 72 64 2d 6c 65 6e 67 74 68 20 * record-length
17c98 28 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 (a variable leng
17c99 74 68 20 69 6e 74 65 67 65 72 20 61 74 20 6d 6f th integer at mo
17c9a 73 74 20 33 32 2d 62 69 74 73 20 69 6e 20 73 69 st 32-bits in si
17c9b 7a 65 29 0a 20 20 20 20 2a 2a 20 61 6e 64 20 74 ze). ** and t
17c9c 68 65 20 6b 65 79 20 76 61 6c 75 65 20 28 61 20 he key value (a
17c9d 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 variable length
17c9e 69 6e 74 65 67 65 72 2c 20 6d 61 79 20 68 61 76 integer, may hav
17c9f 65 20 61 6e 79 20 76 61 6c 75 65 29 2e 0a 20 20 e any value)..
17ca0 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 6f ** The first o
17ca1 66 20 74 68 65 20 77 68 69 6c 65 28 2e 2e 2e 29 f the while(...)
17ca2 20 6c 6f 6f 70 73 20 62 65 6c 6f 77 20 73 6b 69 loops below ski
17ca3 70 73 20 6f 76 65 72 20 74 68 65 20 72 65 63 6f ps over the reco
17ca4 72 64 2d 6c 65 6e 67 74 68 0a 20 20 20 20 2a 2a rd-length. **
17ca5 20 66 69 65 6c 64 2e 20 54 68 65 20 73 65 63 6f field. The seco
17ca6 6e 64 20 77 68 69 6c 65 28 2e 2e 2e 29 20 6c 6f nd while(...) lo
17ca7 6f 70 20 63 6f 70 69 65 73 20 74 68 65 20 6b 65 op copies the ke
17ca8 79 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 y value from the
17ca9 0a 20 20 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 . ** cell on
17caa 70 50 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 pPage into the p
17cab 53 70 61 63 65 20 62 75 66 66 65 72 2e 0a 20 20 Space buffer..
17cac 20 20 2a 2f 0a 20 20 20 20 70 43 65 6c 6c 20 3d */. pCell =
17cad 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c findCell(pPage,
17cae 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 29 pPage->nCell-1)
17caf 3b 0a 20 20 20 20 70 53 74 6f 70 20 3d 20 26 70 ;. pStop = &p
17cb0 43 65 6c 6c 5b 39 5d 3b 0a 20 20 20 20 77 68 69 Cell[9];. whi
17cb1 6c 65 28 20 28 2a 28 70 43 65 6c 6c 2b 2b 29 26 le( (*(pCell++)&
17cb2 30 78 38 30 29 20 26 26 20 70 43 65 6c 6c 3c 70 0x80) && pCell<p
17cb3 53 74 6f 70 20 29 3b 0a 20 20 20 20 70 53 74 6f Stop );. pSto
17cb4 70 20 3d 20 26 70 43 65 6c 6c 5b 39 5d 3b 0a 20 p = &pCell[9];.
17cb5 20 20 20 77 68 69 6c 65 28 20 28 28 2a 28 70 4f while( ((*(pO
17cb6 75 74 2b 2b 29 20 3d 20 2a 28 70 43 65 6c 6c 2b ut++) = *(pCell+
17cb7 2b 29 29 26 30 78 38 30 29 20 26 26 20 70 43 65 +))&0x80) && pCe
17cb8 6c 6c 3c 70 53 74 6f 70 20 29 3b 0a 0a 20 20 20 ll<pStop );..
17cb9 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 65 20 6e /* Insert the n
17cba 65 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 ew divider cell
17cbb 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e 20 2a 2f into pParent. */
17cbc 0a 20 20 20 20 69 6e 73 65 72 74 43 65 6c 6c 28 . insertCell(
17cbd 70 50 61 72 65 6e 74 2c 20 70 50 61 72 65 6e 74 pParent, pParent
17cbe 2d 3e 6e 43 65 6c 6c 2c 20 70 53 70 61 63 65 2c ->nCell, pSpace,
17cbf 20 28 69 6e 74 29 28 70 4f 75 74 2d 70 53 70 61 (int)(pOut-pSpa
17cc0 63 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ce),.
17cc1 20 20 20 20 30 2c 20 70 50 61 67 65 2d 3e 70 67 0, pPage->pg
17cc2 6e 6f 2c 20 26 72 63 29 3b 0a 0a 20 20 20 20 2f no, &rc);.. /
17cc3 2a 20 53 65 74 20 74 68 65 20 72 69 67 68 74 2d * Set the right-
17cc4 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 20 6f 66 child pointer of
17cc5 20 70 50 61 72 65 6e 74 20 74 6f 20 70 6f 69 6e pParent to poin
17cc6 74 20 74 6f 20 74 68 65 20 6e 65 77 20 70 61 67 t to the new pag
17cc7 65 2e 20 2a 2f 0a 20 20 20 20 70 75 74 34 62 79 e. */. put4by
17cc8 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 te(&pParent->aDa
17cc9 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f ta[pParent->hdrO
17cca 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 4e 65 ffset+8], pgnoNe
17ccb 77 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 w);. . /* Re
17ccc 6c 65 61 73 65 20 74 68 65 20 72 65 66 65 72 65 lease the refere
17ccd 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 77 20 70 nce to the new p
17cce 61 67 65 2e 20 2a 2f 0a 20 20 20 20 72 65 6c 65 age. */. rele
17ccf 61 73 65 50 61 67 65 28 70 4e 65 77 29 3b 0a 20 asePage(pNew);.
17cd0 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }.. return rc;
17cd1 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
17cd2 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 ITE_OMIT_QUICKBA
17cd3 4c 41 4e 43 45 20 2a 2f 0a 0a 23 69 66 20 30 0a LANCE */..#if 0.
17cd4 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
17cd5 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e ion does not con
17cd6 74 72 69 62 75 74 65 20 61 6e 79 74 68 69 6e 67 tribute anything
17cd7 20 74 6f 20 74 68 65 20 6f 70 65 72 61 74 69 6f to the operatio
17cd8 6e 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 n of SQLite..**
17cd9 69 74 20 69 73 20 73 6f 6d 65 74 69 6d 65 73 20 it is sometimes
17cda 61 63 74 69 76 61 74 65 64 20 74 65 6d 70 6f 72 activated tempor
17cdb 61 72 69 6c 79 20 77 68 69 6c 65 20 64 65 62 75 arily while debu
17cdc 67 67 69 6e 67 20 63 6f 64 65 20 72 65 73 70 6f gging code respo
17cdd 6e 73 69 62 6c 65 20 0a 2a 2a 20 66 6f 72 20 73 nsible .** for s
17cde 65 74 74 69 6e 67 20 70 6f 69 6e 74 65 72 2d 6d etting pointer-m
17cdf 61 70 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 ap entries..*/.s
17ce0 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d 61 70 tatic int ptrmap
17ce1 43 68 65 63 6b 50 61 67 65 73 28 4d 65 6d 50 61 CheckPages(MemPa
17ce2 67 65 20 2a 2a 61 70 50 61 67 65 2c 20 69 6e 74 ge **apPage, int
17ce3 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 nPage){. int i
17ce4 2c 20 6a 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 , j;. for(i=0;
17ce5 69 3c 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 i<nPage; i++){.
17ce6 20 20 20 50 67 6e 6f 20 6e 3b 0a 20 20 20 20 75 Pgno n;. u
17ce7 38 20 65 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 8 e;. MemPage
17ce8 20 2a 70 50 61 67 65 20 3d 20 61 70 50 61 67 65 *pPage = apPage
17ce9 5b 69 5d 3b 0a 20 20 20 20 42 74 53 68 61 72 65 [i];. BtShare
17cea 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e d *pBt = pPage->
17ceb 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 pBt;. assert(
17cec 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 pPage->isInit )
17ced 3b 0a 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 ;.. for(j=0;
17cee 6a 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 j<pPage->nCell;
17cef 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 43 65 6c 6c j++){. Cell
17cf0 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20 Info info;.
17cf1 20 75 38 20 2a 7a 3b 0a 20 20 20 20 20 0a 20 20 u8 *z;. .
17cf2 20 20 20 20 7a 20 3d 20 66 69 6e 64 43 65 6c 6c z = findCell
17cf3 28 70 50 61 67 65 2c 20 6a 29 3b 0a 20 20 20 20 (pPage, j);.
17cf4 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c btreeParseCell
17cf5 50 74 72 28 70 50 61 67 65 2c 20 7a 2c 20 26 69 Ptr(pPage, z, &i
17cf6 6e 66 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 nfo);. if(
17cf7 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 29 info.iOverflow )
17cf8 7b 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 6f {. Pgno o
17cf9 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 26 vfl = get4byte(&
17cfa 7a 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 z[info.iOverflow
17cfb 5d 29 3b 0a 20 20 20 20 20 20 20 20 70 74 72 6d ]);. ptrm
17cfc 61 70 47 65 74 28 70 42 74 2c 20 6f 76 66 6c 2c apGet(pBt, ovfl,
17cfd 20 26 65 2c 20 26 6e 29 3b 0a 20 20 20 20 20 20 &e, &n);.
17cfe 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 70 50 61 assert( n==pPa
17cff 67 65 2d 3e 70 67 6e 6f 20 26 26 20 65 3d 3d 50 ge->pgno && e==P
17d00 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 TRMAP_OVERFLOW1
17d01 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
17d02 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
17d03 66 20 29 7b 0a 20 20 20 20 20 20 20 20 50 67 6e f ){. Pgn
17d04 6f 20 63 68 69 6c 64 20 3d 20 67 65 74 34 62 79 o child = get4by
17d05 74 65 28 7a 29 3b 0a 20 20 20 20 20 20 20 20 70 te(z);. p
17d06 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 63 68 trmapGet(pBt, ch
17d07 69 6c 64 2c 20 26 65 2c 20 26 6e 29 3b 0a 20 20 ild, &e, &n);.
17d08 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3d assert( n=
17d09 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 26 26 20 =pPage->pgno &&
17d0a 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 e==PTRMAP_BTREE
17d0b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
17d0c 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d . if( !pPage-
17d0d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 50 >leaf ){. P
17d0e 67 6e 6f 20 63 68 69 6c 64 20 3d 20 67 65 74 34 gno child = get4
17d0f 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 byte(&pPage->aDa
17d10 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 ta[pPage->hdrOff
17d11 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 20 20 70 set+8]);. p
17d12 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 63 68 trmapGet(pBt, ch
17d13 69 6c 64 2c 20 26 65 2c 20 26 6e 29 3b 0a 20 20 ild, &e, &n);.
17d14 20 20 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 70 assert( n==p
17d15 50 61 67 65 2d 3e 70 67 6e 6f 20 26 26 20 65 3d Page->pgno && e=
17d16 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 29 3b =PTRMAP_BTREE );
17d17 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
17d18 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a urn 1;.}.#endif.
17d19 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
17d1a 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 tion is used to
17d1b 63 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 copy the content
17d1c 73 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 20 s of the b-tree
17d1d 6e 6f 64 65 20 73 74 6f 72 65 64 20 0a 2a 2a 20 node stored .**
17d1e 6f 6e 20 70 61 67 65 20 70 46 72 6f 6d 20 74 6f on page pFrom to
17d1f 20 70 61 67 65 20 70 54 6f 2e 20 49 66 20 70 61 page pTo. If pa
17d20 67 65 20 70 46 72 6f 6d 20 77 61 73 20 6e 6f 74 ge pFrom was not
17d21 20 61 20 6c 65 61 66 20 70 61 67 65 2c 20 74 68 a leaf page, th
17d22 65 6e 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 en.** the pointe
17d23 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f r-map entries fo
17d24 72 20 65 61 63 68 20 63 68 69 6c 64 20 70 61 67 r each child pag
17d25 65 20 61 72 65 20 75 70 64 61 74 65 64 20 73 6f e are updated so
17d26 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 70 61 72 that the.** par
17d27 65 6e 74 20 70 61 67 65 20 73 74 6f 72 65 64 20 ent page stored
17d28 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d in the pointer m
17d29 61 70 20 69 73 20 70 61 67 65 20 70 54 6f 2e 20 ap is page pTo.
17d2a 49 66 20 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e If pFrom contain
17d2b 65 64 0a 2a 2a 20 61 6e 79 20 63 65 6c 6c 73 20 ed.** any cells
17d2c 77 69 74 68 20 6f 76 65 72 66 6c 6f 77 20 70 61 with overflow pa
17d2d 67 65 20 70 6f 69 6e 74 65 72 73 2c 20 74 68 65 ge pointers, the
17d2e 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 n the correspond
17d2f 69 6e 67 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 6d ing pointer.** m
17d30 61 70 20 65 6e 74 72 69 65 73 20 61 72 65 20 61 ap entries are a
17d31 6c 73 6f 20 75 70 64 61 74 65 64 20 73 6f 20 74 lso updated so t
17d32 68 61 74 20 74 68 65 20 70 61 72 65 6e 74 20 70 hat the parent p
17d33 61 67 65 20 69 73 20 70 61 67 65 20 70 54 6f 2e age is page pTo.
17d34 0a 2a 2a 0a 2a 2a 20 49 66 20 70 46 72 6f 6d 20 .**.** If pFrom
17d35 69 73 20 63 75 72 72 65 6e 74 6c 79 20 63 61 72 is currently car
17d36 72 79 69 6e 67 20 61 6e 79 20 6f 76 65 72 66 6c rying any overfl
17d37 6f 77 20 63 65 6c 6c 73 20 28 65 6e 74 72 69 65 ow cells (entrie
17d38 73 20 69 6e 20 74 68 65 0a 2a 2a 20 4d 65 6d 50 s in the.** MemP
17d39 61 67 65 2e 61 4f 76 66 6c 5b 5d 20 61 72 72 61 age.aOvfl[] arra
17d3a 79 29 2c 20 74 68 65 79 20 61 72 65 20 6e 6f 74 y), they are not
17d3b 20 63 6f 70 69 65 64 20 74 6f 20 70 54 6f 2e 20 copied to pTo.
17d3c 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72 65 20 72 65 .**.** Before re
17d3d 74 75 72 6e 69 6e 67 2c 20 70 61 67 65 20 70 54 turning, page pT
17d3e 6f 20 69 73 20 72 65 69 6e 69 74 69 61 6c 69 7a o is reinitializ
17d3f 65 64 20 75 73 69 6e 67 20 62 74 72 65 65 49 6e ed using btreeIn
17d40 69 74 50 61 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 itPage()..**.**
17d41 54 68 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 The performance
17d42 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e of this function
17d43 20 69 73 20 6e 6f 74 20 63 72 69 74 69 63 61 6c is not critical
17d44 2e 20 49 74 20 69 73 20 6f 6e 6c 79 20 75 73 65 . It is only use
17d45 64 20 62 79 20 0a 2a 2a 20 74 68 65 20 62 61 6c d by .** the bal
17d46 61 6e 63 65 5f 73 68 61 6c 6c 6f 77 65 72 28 29 ance_shallower()
17d47 20 61 6e 64 20 62 61 6c 61 6e 63 65 5f 64 65 65 and balance_dee
17d48 70 65 72 28 29 20 70 72 6f 63 65 64 75 72 65 73 per() procedures
17d49 2c 20 6e 65 69 74 68 65 72 20 6f 66 0a 2a 2a 20 , neither of.**
17d4a 77 68 69 63 68 20 61 72 65 20 63 61 6c 6c 65 64 which are called
17d4b 20 6f 66 74 65 6e 20 75 6e 64 65 72 20 6e 6f 72 often under nor
17d4c 6d 61 6c 20 63 69 72 63 75 6d 73 74 61 6e 63 65 mal circumstance
17d4d 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 s..*/.static voi
17d4e 64 20 63 6f 70 79 4e 6f 64 65 43 6f 6e 74 65 6e d copyNodeConten
17d4f 74 28 4d 65 6d 50 61 67 65 20 2a 70 46 72 6f 6d t(MemPage *pFrom
17d50 2c 20 4d 65 6d 50 61 67 65 20 2a 70 54 6f 2c 20 , MemPage *pTo,
17d51 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 69 66 28 int *pRC){. if(
17d52 20 28 2a 70 52 43 29 3d 3d 53 51 4c 49 54 45 5f (*pRC)==SQLITE_
17d53 4f 4b 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 OK ){. BtShar
17d54 65 64 20 2a 20 63 6f 6e 73 74 20 70 42 74 20 3d ed * const pBt =
17d55 20 70 46 72 6f 6d 2d 3e 70 42 74 3b 0a 20 20 20 pFrom->pBt;.
17d56 20 75 38 20 2a 20 63 6f 6e 73 74 20 61 46 72 6f u8 * const aFro
17d57 6d 20 3d 20 70 46 72 6f 6d 2d 3e 61 44 61 74 61 m = pFrom->aData
17d58 3b 0a 20 20 20 20 75 38 20 2a 20 63 6f 6e 73 74 ;. u8 * const
17d59 20 61 54 6f 20 3d 20 70 54 6f 2d 3e 61 44 61 74 aTo = pTo->aDat
17d5a 61 3b 0a 20 20 20 20 69 6e 74 20 63 6f 6e 73 74 a;. int const
17d5b 20 69 46 72 6f 6d 48 64 72 20 3d 20 70 46 72 6f iFromHdr = pFro
17d5c 6d 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 m->hdrOffset;.
17d5d 20 20 69 6e 74 20 63 6f 6e 73 74 20 69 54 6f 48 int const iToH
17d5e 64 72 20 3d 20 28 28 70 54 6f 2d 3e 70 67 6e 6f dr = ((pTo->pgno
17d5f 3d 3d 31 29 20 3f 20 31 30 30 20 3a 20 30 29 3b ==1) ? 100 : 0);
17d60 0a 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 69 6e . TESTONLY(in
17d61 74 20 72 63 3b 29 0a 20 20 20 20 69 6e 74 20 69 t rc;). int i
17d62 44 61 74 61 3b 0a 20 20 0a 20 20 0a 20 20 20 20 Data;. . .
17d63 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 69 assert( pFrom->i
17d64 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 61 73 73 sInit );. ass
17d65 65 72 74 28 20 70 46 72 6f 6d 2d 3e 6e 46 72 65 ert( pFrom->nFre
17d66 65 3e 3d 69 54 6f 48 64 72 20 29 3b 0a 20 20 20 e>=iToHdr );.
17d67 20 61 73 73 65 72 74 28 20 67 65 74 32 62 79 74 assert( get2byt
17d68 65 28 26 61 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 e(&aFrom[iFromHd
17d69 72 2b 35 5d 29 3c 3d 70 42 74 2d 3e 75 73 61 62 r+5])<=pBt->usab
17d6a 6c 65 53 69 7a 65 20 29 3b 0a 20 20 0a 20 20 20 leSize );. .
17d6b 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 62 2d 74 /* Copy the b-t
17d6c 72 65 65 20 6e 6f 64 65 20 63 6f 6e 74 65 6e 74 ree node content
17d6d 20 66 72 6f 6d 20 70 61 67 65 20 70 46 72 6f 6d from page pFrom
17d6e 20 74 6f 20 70 61 67 65 20 70 54 6f 2e 20 2a 2f to page pTo. */
17d6f 0a 20 20 20 20 69 44 61 74 61 20 3d 20 67 65 74 . iData = get
17d70 32 62 79 74 65 28 26 61 46 72 6f 6d 5b 69 46 72 2byte(&aFrom[iFr
17d71 6f 6d 48 64 72 2b 35 5d 29 3b 0a 20 20 20 20 6d omHdr+5]);. m
17d72 65 6d 63 70 79 28 26 61 54 6f 5b 69 44 61 74 61 emcpy(&aTo[iData
17d73 5d 2c 20 26 61 46 72 6f 6d 5b 69 44 61 74 61 5d ], &aFrom[iData]
17d74 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a , pBt->usableSiz
17d75 65 2d 69 44 61 74 61 29 3b 0a 20 20 20 20 6d 65 e-iData);. me
17d76 6d 63 70 79 28 26 61 54 6f 5b 69 54 6f 48 64 72 mcpy(&aTo[iToHdr
17d77 5d 2c 20 26 61 46 72 6f 6d 5b 69 46 72 6f 6d 48 ], &aFrom[iFromH
17d78 64 72 5d 2c 20 70 46 72 6f 6d 2d 3e 63 65 6c 6c dr], pFrom->cell
17d79 4f 66 66 73 65 74 20 2b 20 32 2a 70 46 72 6f 6d Offset + 2*pFrom
17d7a 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 0a 20 20 20 ->nCell);. .
17d7b 20 2f 2a 20 52 65 69 6e 69 74 69 61 6c 69 7a 65 /* Reinitialize
17d7c 20 70 61 67 65 20 70 54 6f 20 73 6f 20 74 68 61 page pTo so tha
17d7d 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f t the contents o
17d7e 66 20 74 68 65 20 4d 65 6d 50 61 67 65 20 73 74 f the MemPage st
17d7f 72 75 63 74 75 72 65 0a 20 20 20 20 2a 2a 20 6d ructure. ** m
17d80 61 74 63 68 20 74 68 65 20 6e 65 77 20 64 61 74 atch the new dat
17d81 61 2e 20 54 68 65 20 69 6e 69 74 69 61 6c 69 7a a. The initializ
17d82 61 74 69 6f 6e 20 6f 66 20 70 54 6f 20 22 63 61 ation of pTo "ca
17d83 6e 6e 6f 74 22 20 66 61 69 6c 2c 20 61 73 20 74 nnot" fail, as t
17d84 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 20 63 he. ** data c
17d85 6f 70 69 65 64 20 66 72 6f 6d 20 70 46 72 6f 6d opied from pFrom
17d86 20 69 73 20 6b 6e 6f 77 6e 20 74 6f 20 62 65 20 is known to be
17d87 76 61 6c 69 64 2e 20 20 2a 2f 0a 20 20 20 20 70 valid. */. p
17d88 54 6f 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a To->isInit = 0;.
17d89 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 72 63 20 TESTONLY(rc
17d8a 3d 20 29 20 62 74 72 65 65 49 6e 69 74 50 61 67 = ) btreeInitPag
17d8b 65 28 70 54 6f 29 3b 0a 20 20 20 20 61 73 73 65 e(pTo);. asse
17d8c 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rt( rc==SQLITE_O
17d8d 4b 20 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49 K );. . /* I
17d8e 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 f this is an aut
17d8f 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 o-vacuum databas
17d90 65 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 6f e, update the po
17d91 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 inter-map entrie
17d92 73 0a 20 20 20 20 2a 2a 20 66 6f 72 20 61 6e 79 s. ** for any
17d93 20 62 2d 74 72 65 65 20 6f 72 20 6f 76 65 72 66 b-tree or overf
17d94 6c 6f 77 20 70 61 67 65 73 20 74 68 61 74 20 70 low pages that p
17d95 54 6f 20 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 20 To now contains
17d96 74 68 65 20 70 6f 69 6e 74 65 72 73 20 74 6f 2e the pointers to.
17d97 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
17d98 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a ISAUTOVACUUM ){.
17d99 20 20 20 20 20 20 2a 70 52 43 20 3d 20 73 65 74 *pRC = set
17d9a 43 68 69 6c 64 50 74 72 6d 61 70 73 28 70 54 6f ChildPtrmaps(pTo
17d9b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a );. }. }.}..
17d9c 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
17d9d 6e 65 20 72 65 64 69 73 74 72 69 62 75 74 65 73 ne redistributes
17d9e 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 69 50 cells on the iP
17d9f 61 72 65 6e 74 49 64 78 27 74 68 20 63 68 69 6c arentIdx'th chil
17da0 64 20 6f 66 20 70 50 61 72 65 6e 74 0a 2a 2a 20 d of pParent.**
17da1 28 68 65 72 65 61 66 74 65 72 20 22 74 68 65 20 (hereafter "the
17da2 70 61 67 65 22 29 20 61 6e 64 20 75 70 20 74 6f page") and up to
17da3 20 32 20 73 69 62 6c 69 6e 67 73 20 73 6f 20 74 2 siblings so t
17da4 68 61 74 20 61 6c 6c 20 70 61 67 65 73 20 68 61 hat all pages ha
17da5 76 65 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a 20 ve about the.**
17da6 73 61 6d 65 20 61 6d 6f 75 6e 74 20 6f 66 20 66 same amount of f
17da7 72 65 65 20 73 70 61 63 65 2e 20 55 73 75 61 6c ree space. Usual
17da8 6c 79 20 61 20 73 69 6e 67 6c 65 20 73 69 62 6c ly a single sibl
17da9 69 6e 67 20 6f 6e 20 65 69 74 68 65 72 20 73 69 ing on either si
17daa 64 65 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 de of the.** pag
17dab 65 20 61 72 65 20 75 73 65 64 20 69 6e 20 74 68 e are used in th
17dac 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 6f e balancing, tho
17dad 75 67 68 20 62 6f 74 68 20 73 69 62 6c 69 6e 67 ugh both sibling
17dae 73 20 6d 69 67 68 74 20 63 6f 6d 65 20 66 72 6f s might come fro
17daf 6d 20 6f 6e 65 0a 2a 2a 20 73 69 64 65 20 69 66 m one.** side if
17db0 20 74 68 65 20 70 61 67 65 20 69 73 20 74 68 65 the page is the
17db1 20 66 69 72 73 74 20 6f 72 20 6c 61 73 74 20 63 first or last c
17db2 68 69 6c 64 20 6f 66 20 69 74 73 20 70 61 72 65 hild of its pare
17db3 6e 74 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 nt. If the page
17db4 0a 2a 2a 20 68 61 73 20 66 65 77 65 72 20 74 68 .** has fewer th
17db5 61 6e 20 32 20 73 69 62 6c 69 6e 67 73 20 28 73 an 2 siblings (s
17db6 6f 6d 65 74 68 69 6e 67 20 77 68 69 63 68 20 63 omething which c
17db7 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 an only happen i
17db8 66 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 69 73 f the page.** is
17db9 20 61 20 72 6f 6f 74 20 70 61 67 65 20 6f 72 20 a root page or
17dba 61 20 63 68 69 6c 64 20 6f 66 20 61 20 72 6f 6f a child of a roo
17dbb 74 20 70 61 67 65 29 20 74 68 65 6e 20 61 6c 6c t page) then all
17dbc 20 61 76 61 69 6c 61 62 6c 65 20 73 69 62 6c 69 available sibli
17dbd 6e 67 73 0a 2a 2a 20 70 61 72 74 69 63 69 70 61 ngs.** participa
17dbe 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 te in the balanc
17dbf 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e ing..**.** The n
17dc0 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 6e 67 umber of sibling
17dc1 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 6d 69 s of the page mi
17dc2 67 68 74 20 62 65 20 69 6e 63 72 65 61 73 65 64 ght be increased
17dc3 20 6f 72 20 64 65 63 72 65 61 73 65 64 20 62 79 or decreased by
17dc4 20 0a 2a 2a 20 6f 6e 65 20 6f 72 20 74 77 6f 20 .** one or two
17dc5 69 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 in an effort to
17dc6 6b 65 65 70 20 70 61 67 65 73 20 6e 65 61 72 6c keep pages nearl
17dc7 79 20 66 75 6c 6c 20 62 75 74 20 6e 6f 74 20 6f y full but not o
17dc8 76 65 72 20 66 75 6c 6c 2e 20 0a 2a 2a 0a 2a 2a ver full. .**.**
17dc9 20 4e 6f 74 65 20 74 68 61 74 20 77 68 65 6e 20 Note that when
17dca 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
17dcb 63 61 6c 6c 65 64 2c 20 73 6f 6d 65 20 6f 66 20 called, some of
17dcc 74 68 65 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 the cells on the
17dcd 20 70 61 67 65 0a 2a 2a 20 6d 69 67 68 74 20 6e page.** might n
17dce 6f 74 20 61 63 74 75 61 6c 6c 79 20 62 65 20 73 ot actually be s
17dcf 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50 61 67 65 tored in MemPage
17dd0 2e 61 44 61 74 61 5b 5d 2e 20 54 68 69 73 20 63 .aData[]. This c
17dd1 61 6e 20 68 61 70 70 65 6e 0a 2a 2a 20 69 66 20 an happen.** if
17dd2 74 68 65 20 70 61 67 65 20 69 73 20 6f 76 65 72 the page is over
17dd3 66 75 6c 6c 2e 20 54 68 69 73 20 72 6f 75 74 69 full. This routi
17dd4 6e 65 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 ne ensures that
17dd5 61 6c 6c 20 63 65 6c 6c 73 20 61 6c 6c 6f 63 61 all cells alloca
17dd6 74 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 70 61 ted.** to the pa
17dd7 67 65 20 61 6e 64 20 69 74 73 20 73 69 62 6c 69 ge and its sibli
17dd8 6e 67 73 20 66 69 74 20 69 6e 74 6f 20 4d 65 6d ngs fit into Mem
17dd9 50 61 67 65 2e 61 44 61 74 61 5b 5d 20 62 65 66 Page.aData[] bef
17dda 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a ore returning..*
17ddb 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 63 6f 75 72 *.** In the cour
17ddc 73 65 20 6f 66 20 62 61 6c 61 6e 63 69 6e 67 20 se of balancing
17ddd 74 68 65 20 70 61 67 65 20 61 6e 64 20 69 74 73 the page and its
17dde 20 73 69 62 6c 69 6e 67 73 2c 20 63 65 6c 6c 73 siblings, cells
17ddf 20 6d 61 79 20 62 65 0a 2a 2a 20 69 6e 73 65 72 may be.** inser
17de0 74 65 64 20 69 6e 74 6f 20 6f 72 20 72 65 6d 6f ted into or remo
17de1 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 72 ved from the par
17de2 65 6e 74 20 70 61 67 65 20 28 70 50 61 72 65 6e ent page (pParen
17de3 74 29 2e 20 44 6f 69 6e 67 20 73 6f 0a 2a 2a 20 t). Doing so.**
17de4 6d 61 79 20 63 61 75 73 65 20 74 68 65 20 70 61 may cause the pa
17de5 72 65 6e 74 20 70 61 67 65 20 74 6f 20 62 65 63 rent page to bec
17de6 6f 6d 65 20 6f 76 65 72 66 75 6c 6c 20 6f 72 20 ome overfull or
17de7 75 6e 64 65 72 66 75 6c 6c 2e 20 49 66 20 74 68 underfull. If th
17de8 69 73 0a 2a 2a 20 68 61 70 70 65 6e 73 2c 20 69 is.** happens, i
17de9 74 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73 t is the respons
17dea 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 ibility of the c
17deb 61 6c 6c 65 72 20 74 6f 20 69 6e 76 6f 6b 65 20 aller to invoke
17dec 74 68 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20 62 the correct.** b
17ded 61 6c 61 6e 63 69 6e 67 20 72 6f 75 74 69 6e 65 alancing routine
17dee 20 74 6f 20 66 69 78 20 74 68 69 73 20 70 72 6f to fix this pro
17def 62 6c 65 6d 20 28 73 65 65 20 74 68 65 20 62 61 blem (see the ba
17df0 6c 61 6e 63 65 28 29 20 72 6f 75 74 69 6e 65 29 lance() routine)
17df1 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 . .**.** If this
17df2 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 20 66 routine fails f
17df3 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 2c 20 69 or any reason, i
17df4 74 20 6d 69 67 68 74 20 6c 65 61 76 65 20 74 68 t might leave th
17df5 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 6e e database.** in
17df6 20 61 20 63 6f 72 72 75 70 74 65 64 20 73 74 61 a corrupted sta
17df7 74 65 2e 20 53 6f 20 69 66 20 74 68 69 73 20 72 te. So if this r
17df8 6f 75 74 69 6e 65 20 66 61 69 6c 73 2c 20 74 68 outine fails, th
17df9 65 20 64 61 74 61 62 61 73 65 20 73 68 6f 75 6c e database shoul
17dfa 64 0a 2a 2a 20 62 65 20 72 6f 6c 6c 65 64 20 62 d.** be rolled b
17dfb 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 ack..**.** The t
17dfc 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 74 6f hird argument to
17dfd 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 this function,
17dfe 61 4f 76 66 6c 53 70 61 63 65 2c 20 69 73 20 61 aOvflSpace, is a
17dff 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 0a 2a 2a pointer to a.**
17e00 20 62 75 66 66 65 72 20 62 69 67 20 65 6e 6f 75 buffer big enou
17e01 67 68 20 74 6f 20 68 6f 6c 64 20 6f 6e 65 20 70 gh to hold one p
17e02 61 67 65 2e 20 49 66 20 77 68 69 6c 65 20 69 6e age. If while in
17e03 73 65 72 74 69 6e 67 20 63 65 6c 6c 73 20 69 6e serting cells in
17e04 74 6f 20 74 68 65 20 70 61 72 65 6e 74 0a 2a 2a to the parent.**
17e05 20 70 61 67 65 20 28 70 50 61 72 65 6e 74 29 20 page (pParent)
17e06 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 the parent page
17e07 62 65 63 6f 6d 65 73 20 6f 76 65 72 66 75 6c 6c becomes overfull
17e08 2c 20 74 68 69 73 20 62 75 66 66 65 72 20 69 73 , this buffer is
17e09 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 .** used to stor
17e0a 65 20 74 68 65 20 70 61 72 65 6e 74 27 73 20 6f e the parent's o
17e0b 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 20 42 verflow cells. B
17e0c 65 63 61 75 73 65 20 74 68 69 73 20 66 75 6e 63 ecause this func
17e0d 74 69 6f 6e 20 69 6e 73 65 72 74 73 0a 2a 2a 20 tion inserts.**
17e0e 61 20 6d 61 78 69 6d 75 6d 20 6f 66 20 66 6f 75 a maximum of fou
17e0f 72 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 20 r divider cells
17e10 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 into the parent
17e11 70 61 67 65 2c 20 61 6e 64 20 74 68 65 20 6d 61 page, and the ma
17e12 78 69 6d 75 6d 0a 2a 2a 20 73 69 7a 65 20 6f 66 ximum.** size of
17e13 20 61 20 63 65 6c 6c 20 73 74 6f 72 65 64 20 77 a cell stored w
17e14 69 74 68 69 6e 20 61 6e 20 69 6e 74 65 72 6e 61 ithin an interna
17e15 6c 20 6e 6f 64 65 20 69 73 20 61 6c 77 61 79 73 l node is always
17e16 20 6c 65 73 73 20 74 68 61 6e 20 31 2f 34 0a 2a less than 1/4.*
17e17 2a 20 6f 66 20 74 68 65 20 70 61 67 65 2d 73 69 * of the page-si
17e18 7a 65 2c 20 74 68 65 20 61 4f 76 66 6c 53 70 61 ze, the aOvflSpa
17e19 63 65 5b 5d 20 62 75 66 66 65 72 20 69 73 20 67 ce[] buffer is g
17e1a 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 uaranteed to be
17e1b 6c 61 72 67 65 0a 2a 2a 20 65 6e 6f 75 67 68 20 large.** enough
17e1c 66 6f 72 20 61 6c 6c 20 6f 76 65 72 66 6c 6f 77 for all overflow
17e1d 20 63 65 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 cells..**.** If
17e1e 20 61 4f 76 66 6c 53 70 61 63 65 20 69 73 20 73 aOvflSpace is s
17e1f 65 74 20 74 6f 20 61 20 6e 75 6c 6c 20 70 6f 69 et to a null poi
17e20 6e 74 65 72 2c 20 74 68 69 73 20 66 75 6e 63 74 nter, this funct
17e21 69 6f 6e 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20 ion returns .**
17e22 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f SQLITE_NOMEM..*/
17e23 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 .static int bala
17e24 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 0a 20 20 4d nce_nonroot(. M
17e25 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 2c emPage *pParent,
17e26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
17e27 2a 20 50 61 72 65 6e 74 20 70 61 67 65 20 6f 66 * Parent page of
17e28 20 73 69 62 6c 69 6e 67 73 20 62 65 69 6e 67 20 siblings being
17e29 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 20 69 6e balanced */. in
17e2a 74 20 69 50 61 72 65 6e 74 49 64 78 2c 20 20 20 t iParentIdx,
17e2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17e2c 20 49 6e 64 65 78 20 6f 66 20 22 74 68 65 20 70 Index of "the p
17e2d 61 67 65 22 20 69 6e 20 70 50 61 72 65 6e 74 20 age" in pParent
17e2e 2a 2f 0a 20 20 75 38 20 2a 61 4f 76 66 6c 53 70 */. u8 *aOvflSp
17e2f 61 63 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ace,
17e30 20 20 20 20 20 2f 2a 20 70 61 67 65 2d 73 69 7a /* page-siz
17e31 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 e bytes of space
17e32 20 66 6f 72 20 70 61 72 65 6e 74 20 6f 76 66 6c for parent ovfl
17e33 20 2a 2f 0a 20 20 69 6e 74 20 69 73 52 6f 6f 74 */. int isRoot
17e34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17e35 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
17e36 20 70 50 61 72 65 6e 74 20 69 73 20 61 20 72 6f pParent is a ro
17e37 6f 74 2d 70 61 67 65 20 2a 2f 0a 29 7b 0a 20 20 ot-page */.){.
17e38 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 BtShared *pBt;
17e39 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17e3a 54 68 65 20 77 68 6f 6c 65 20 64 61 74 61 62 61 The whole databa
17e3b 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c se */. int nCel
17e3c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 l = 0;
17e3d 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
17e3e 66 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c f cells in apCel
17e3f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 l[] */. int nMa
17e40 78 43 65 6c 6c 73 20 3d 20 30 3b 20 20 20 20 20 xCells = 0;
17e41 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 /* Allocat
17e42 65 64 20 73 69 7a 65 20 6f 66 20 61 70 43 65 6c ed size of apCel
17e43 6c 2c 20 73 7a 43 65 6c 6c 2c 20 61 46 72 6f 6d l, szCell, aFrom
17e44 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 65 77 20 . */. int nNew
17e45 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
17e46 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
17e47 20 70 61 67 65 73 20 69 6e 20 61 70 4e 65 77 5b pages in apNew[
17e48 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b ] */. int nOld;
17e49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17e4a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
17e4b 20 70 61 67 65 73 20 69 6e 20 61 70 4f 6c 64 5b pages in apOld[
17e4c 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 2c ] */. int i, j,
17e4d 20 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 k;
17e4e 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
17e4f 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 78 ters */. int nx
17e50 44 69 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 Div;
17e51 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 64 /* Next d
17e52 69 76 69 64 65 72 20 73 6c 6f 74 20 69 6e 20 70 ivider slot in p
17e53 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c 5b 5d 20 Parent->aCell[]
17e54 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 */. int rc = SQ
17e55 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 LITE_OK;
17e56 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 /* The return
17e57 63 6f 64 65 20 2a 2f 0a 20 20 75 31 36 20 6c 65 code */. u16 le
17e58 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 20 20 20 afCorrection;
17e59 20 20 20 20 20 20 20 2f 2a 20 34 20 69 66 20 70 /* 4 if p
17e5a 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 2e 20 Page is a leaf.
17e5b 20 30 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 69 0 if not */. i
17e5c 6e 74 20 6c 65 61 66 44 61 74 61 3b 20 20 20 20 nt leafData;
17e5d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
17e5e 72 75 65 20 69 66 20 70 50 61 67 65 20 69 73 20 rue if pPage is
17e5f 61 20 6c 65 61 66 20 6f 66 20 61 20 4c 45 41 46 a leaf of a LEAF
17e60 44 41 54 41 20 74 72 65 65 20 2a 2f 0a 20 20 69 DATA tree */. i
17e61 6e 74 20 75 73 61 62 6c 65 53 70 61 63 65 3b 20 nt usableSpace;
17e62 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
17e63 79 74 65 73 20 69 6e 20 70 50 61 67 65 20 62 65 ytes in pPage be
17e64 79 6f 6e 64 20 74 68 65 20 68 65 61 64 65 72 20 yond the header
17e65 2a 2f 0a 20 20 69 6e 74 20 70 61 67 65 46 6c 61 */. int pageFla
17e66 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 gs;
17e67 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 70 50 /* Value of pP
17e68 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 20 2a 2f age->aData[0] */
17e69 0a 20 20 69 6e 74 20 73 75 62 74 6f 74 61 6c 3b . int subtotal;
17e6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17e6b 2f 2a 20 53 75 62 74 6f 74 61 6c 20 6f 66 20 62 /* Subtotal of b
17e6c 79 74 65 73 20 69 6e 20 63 65 6c 6c 73 20 6f 6e ytes in cells on
17e6d 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 one page */. i
17e6e 6e 74 20 69 53 70 61 63 65 31 20 3d 20 30 3b 20 nt iSpace1 = 0;
17e6f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
17e70 69 72 73 74 20 75 6e 75 73 65 64 20 62 79 74 65 irst unused byte
17e71 20 6f 66 20 61 53 70 61 63 65 31 5b 5d 20 2a 2f of aSpace1[] */
17e72 0a 20 20 69 6e 74 20 69 4f 76 66 6c 53 70 61 63 . int iOvflSpac
17e73 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 e = 0;
17e74 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64 20 /* First unused
17e75 62 79 74 65 20 6f 66 20 61 4f 76 66 6c 53 70 61 byte of aOvflSpa
17e76 63 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 73 7a ce[] */. int sz
17e77 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20 Scratch;
17e78 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
17e79 66 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 f scratch memory
17e7a 20 72 65 71 75 65 73 74 65 64 20 2a 2f 0a 20 20 requested */.
17e7b 4d 65 6d 50 61 67 65 20 2a 61 70 4f 6c 64 5b 4e MemPage *apOld[N
17e7c 42 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 B]; /*
17e7d 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20 pPage and up to
17e7e 74 77 6f 20 73 69 62 6c 69 6e 67 73 20 2a 2f 0a two siblings */.
17e7f 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 43 6f 70 MemPage *apCop
17e80 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 2f y[NB]; /
17e81 2a 20 50 72 69 76 61 74 65 20 63 6f 70 69 65 73 * Private copies
17e82 20 6f 66 20 61 70 4f 6c 64 5b 5d 20 70 61 67 65 of apOld[] page
17e83 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a s */. MemPage *
17e84 61 70 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 apNew[NB+2];
17e85 20 20 20 20 2f 2a 20 70 50 61 67 65 20 61 6e 64 /* pPage and
17e86 20 75 70 20 74 6f 20 4e 42 20 73 69 62 6c 69 6e up to NB siblin
17e87 67 73 20 61 66 74 65 72 20 62 61 6c 61 6e 63 69 gs after balanci
17e88 6e 67 20 2a 2f 0a 20 20 75 38 20 2a 70 52 69 67 ng */. u8 *pRig
17e89 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ht;
17e8a 20 20 20 20 20 2f 2a 20 4c 6f 63 61 74 69 6f 6e /* Location
17e8b 20 69 6e 20 70 61 72 65 6e 74 20 6f 66 20 72 69 in parent of ri
17e8c 67 68 74 2d 73 69 62 6c 69 6e 67 20 70 6f 69 6e ght-sibling poin
17e8d 74 65 72 20 2a 2f 0a 20 20 75 38 20 2a 61 70 44 ter */. u8 *apD
17e8e 69 76 5b 4e 42 2d 31 5d 3b 20 20 20 20 20 20 20 iv[NB-1];
17e8f 20 20 20 20 20 20 2f 2a 20 44 69 76 69 64 65 72 /* Divider
17e90 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e cells in pParen
17e91 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 4e 65 t */. int cntNe
17e92 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 w[NB+2];
17e93 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 /* Index in
17e94 61 43 65 6c 6c 5b 5d 20 6f 66 20 63 65 6c 6c 20 aCell[] of cell
17e95 61 66 74 65 72 20 69 2d 74 68 20 70 61 67 65 20 after i-th page
17e96 2a 2f 0a 20 20 69 6e 74 20 73 7a 4e 65 77 5b 4e */. int szNew[N
17e97 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 B+2];
17e98 20 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 73 69 /* Combined si
17e99 7a 65 20 6f 66 20 63 65 6c 6c 73 20 70 6c 61 63 ze of cells plac
17e9a 65 20 6f 6e 20 69 2d 74 68 20 70 61 67 65 20 2a e on i-th page *
17e9b 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 20 /. u8 **apCell
17e9c 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
17e9d 20 2f 2a 20 41 6c 6c 20 63 65 6c 6c 73 20 62 65 /* All cells be
17e9e 67 69 6e 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a gin balanced */.
17e9f 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20 u16 *szCell;
17ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
17ea1 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20 * Local size of
17ea2 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 all cells in apC
17ea3 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 ell[] */. u8 *a
17ea4 53 70 61 63 65 31 3b 20 20 20 20 20 20 20 20 20 Space1;
17ea5 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 /* Space
17ea6 20 66 6f 72 20 63 6f 70 69 65 73 20 6f 66 20 64 for copies of d
17ea7 69 76 69 64 65 72 73 20 63 65 6c 6c 73 20 2a 2f ividers cells */
17ea8 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 . Pgno pgno;
17ea9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17eaa 2f 2a 20 54 65 6d 70 20 76 61 72 20 74 6f 20 73 /* Temp var to s
17eab 74 6f 72 65 20 61 20 70 61 67 65 20 6e 75 6d 62 tore a page numb
17eac 65 72 20 69 6e 20 2a 2f 0a 0a 20 20 70 42 74 20 er in */.. pBt
17ead 3d 20 70 50 61 72 65 6e 74 2d 3e 70 42 74 3b 0a = pParent->pBt;.
17eae 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
17eaf 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
17eb0 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
17eb1 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
17eb2 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
17eb3 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 20 arent->pDbPage)
17eb4 29 3b 0a 0a 23 69 66 20 30 0a 20 20 54 52 41 43 );..#if 0. TRAC
17eb5 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 62 65 67 E(("BALANCE: beg
17eb6 69 6e 20 70 61 67 65 20 25 64 20 63 68 69 6c 64 in page %d child
17eb7 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 of %d\n", pPage
17eb8 2d 3e 70 67 6e 6f 2c 20 70 50 61 72 65 6e 74 2d ->pgno, pParent-
17eb9 3e 70 67 6e 6f 29 29 3b 0a 23 65 6e 64 69 66 0a >pgno));.#endif.
17eba 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f . /* At this po
17ebb 69 6e 74 20 70 50 61 72 65 6e 74 20 6d 61 79 20 int pParent may
17ebc 68 61 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65 have at most one
17ebd 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 2e 20 overflow cell.
17ebe 41 6e 64 20 69 66 0a 20 20 2a 2a 20 74 68 69 73 And if. ** this
17ebf 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 69 overflow cell i
17ec0 73 20 70 72 65 73 65 6e 74 2c 20 69 74 20 6d 75 s present, it mu
17ec1 73 74 20 62 65 20 74 68 65 20 63 65 6c 6c 20 77 st be the cell w
17ec2 69 74 68 20 0a 20 20 2a 2a 20 69 6e 64 65 78 20 ith . ** index
17ec3 69 50 61 72 65 6e 74 49 64 78 2e 20 54 68 69 73 iParentIdx. This
17ec4 20 73 63 65 6e 61 72 69 6f 20 63 6f 6d 65 73 20 scenario comes
17ec5 61 62 6f 75 74 20 77 68 65 6e 20 74 68 69 73 20 about when this
17ec6 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 function. ** is
17ec7 20 63 61 6c 6c 65 64 20 28 69 6e 64 69 72 65 63 called (indirec
17ec8 74 6c 79 29 20 66 72 6f 6d 20 73 71 6c 69 74 65 tly) from sqlite
17ec9 33 42 74 72 65 65 44 65 6c 65 74 65 28 29 2e 0a 3BtreeDelete()..
17eca 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
17ecb 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f Parent->nOverflo
17ecc 77 3d 3d 30 20 7c 7c 20 70 50 61 72 65 6e 74 2d w==0 || pParent-
17ecd 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 29 3b >nOverflow==1 );
17ece 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65 . assert( pPare
17ecf 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 nt->nOverflow==0
17ed0 20 7c 7c 20 70 50 61 72 65 6e 74 2d 3e 61 4f 76 || pParent->aOv
17ed1 66 6c 5b 30 5d 2e 69 64 78 3d 3d 69 50 61 72 65 fl[0].idx==iPare
17ed2 6e 74 49 64 78 20 29 3b 0a 0a 20 20 69 66 28 20 ntIdx );.. if(
17ed3 21 61 4f 76 66 6c 53 70 61 63 65 20 29 7b 0a 20 !aOvflSpace ){.
17ed4 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
17ed5 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f _NOMEM;. }.. /
17ed6 2a 20 46 69 6e 64 20 74 68 65 20 73 69 62 6c 69 * Find the sibli
17ed7 6e 67 20 70 61 67 65 73 20 74 6f 20 62 61 6c 61 ng pages to bala
17ed8 6e 63 65 2e 20 41 6c 73 6f 20 6c 6f 63 61 74 65 nce. Also locate
17ed9 20 74 68 65 20 63 65 6c 6c 73 20 69 6e 20 70 50 the cells in pP
17eda 61 72 65 6e 74 20 0a 20 20 2a 2a 20 74 68 61 74 arent . ** that
17edb 20 64 69 76 69 64 65 20 74 68 65 20 73 69 62 6c divide the sibl
17edc 69 6e 67 73 2e 20 41 6e 20 61 74 74 65 6d 70 74 ings. An attempt
17edd 20 69 73 20 6d 61 64 65 20 74 6f 20 66 69 6e 64 is made to find
17ede 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 NN siblings on
17edf 0a 20 20 2a 2a 20 65 69 74 68 65 72 20 73 69 64 . ** either sid
17ee0 65 20 6f 66 20 70 50 61 67 65 2e 20 4d 6f 72 65 e of pPage. More
17ee1 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 74 61 siblings are ta
17ee2 6b 65 6e 20 66 72 6f 6d 20 6f 6e 65 20 73 69 64 ken from one sid
17ee3 65 2c 20 68 6f 77 65 76 65 72 2c 20 0a 20 20 2a e, however, . *
17ee4 2a 20 69 66 20 74 68 65 72 65 20 61 72 65 20 66 * if there are f
17ee5 65 77 65 72 20 74 68 61 6e 20 4e 4e 20 73 69 62 ewer than NN sib
17ee6 6c 69 6e 67 73 20 6f 6e 20 74 68 65 20 6f 74 68 lings on the oth
17ee7 65 72 20 73 69 64 65 2e 20 49 66 20 70 50 61 72 er side. If pPar
17ee8 65 6e 74 0a 20 20 2a 2a 20 68 61 73 20 4e 42 20 ent. ** has NB
17ee9 6f 72 20 66 65 77 65 72 20 63 68 69 6c 64 72 65 or fewer childre
17eea 6e 20 74 68 65 6e 20 61 6c 6c 20 63 68 69 6c 64 n then all child
17eeb 72 65 6e 20 6f 66 20 70 50 61 72 65 6e 74 20 61 ren of pParent a
17eec 72 65 20 74 61 6b 65 6e 2e 20 20 0a 20 20 2a 2a re taken. . **
17eed 0a 20 20 2a 2a 20 54 68 69 73 20 6c 6f 6f 70 20 . ** This loop
17eee 61 6c 73 6f 20 64 72 6f 70 73 20 74 68 65 20 64 also drops the d
17eef 69 76 69 64 65 72 20 63 65 6c 6c 73 20 66 72 6f ivider cells fro
17ef0 6d 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 m the parent pag
17ef1 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20 77 61 79 e. This. ** way
17ef2 2c 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 , the remainder
17ef3 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 of the function
17ef4 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f does not have to
17ef5 20 64 65 61 6c 20 77 69 74 68 20 61 6e 79 0a 20 deal with any.
17ef6 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c ** overflow cel
17ef7 6c 73 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 ls in the parent
17ef8 20 70 61 67 65 2c 20 73 69 6e 63 65 20 69 66 20 page, since if
17ef9 61 6e 79 20 65 78 69 73 74 65 64 20 74 68 65 79 any existed they
17efa 20 77 69 6c 6c 0a 20 20 2a 2a 20 68 61 76 65 20 will. ** have
17efb 61 6c 72 65 61 64 79 20 62 65 65 6e 20 72 65 6d already been rem
17efc 6f 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 20 3d oved.. */. i =
17efd 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 pParent->nOverf
17efe 6c 6f 77 20 2b 20 70 50 61 72 65 6e 74 2d 3e 6e low + pParent->n
17eff 43 65 6c 6c 3b 0a 20 20 69 66 28 20 69 3c 32 20 Cell;. if( i<2
17f00 29 7b 0a 20 20 20 20 6e 78 44 69 76 20 3d 20 30 ){. nxDiv = 0
17f01 3b 0a 20 20 20 20 6e 4f 6c 64 20 3d 20 69 2b 31 ;. nOld = i+1
17f02 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e ;. }else{. n
17f03 4f 6c 64 20 3d 20 33 3b 0a 20 20 20 20 69 66 28 Old = 3;. if(
17f04 20 69 50 61 72 65 6e 74 49 64 78 3d 3d 30 20 29 iParentIdx==0 )
17f05 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
17f06 20 20 0a 20 20 20 20 20 20 6e 78 44 69 76 20 3d . nxDiv =
17f07 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 0;. }else if
17f08 28 20 69 50 61 72 65 6e 74 49 64 78 3d 3d 69 20 ( iParentIdx==i
17f09 29 7b 0a 20 20 20 20 20 20 6e 78 44 69 76 20 3d ){. nxDiv =
17f0a 20 69 2d 32 3b 0a 20 20 20 20 7d 65 6c 73 65 7b i-2;. }else{
17f0b 0a 20 20 20 20 20 20 6e 78 44 69 76 20 3d 20 69 . nxDiv = i
17f0c 50 61 72 65 6e 74 49 64 78 2d 31 3b 0a 20 20 20 ParentIdx-1;.
17f0d 20 7d 0a 20 20 20 20 69 20 3d 20 32 3b 0a 20 20 }. i = 2;.
17f0e 7d 0a 20 20 69 66 28 20 28 69 2b 6e 78 44 69 76 }. if( (i+nxDiv
17f0f 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 -pParent->nOverf
17f10 6c 6f 77 29 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e low)==pParent->n
17f11 43 65 6c 6c 20 29 7b 0a 20 20 20 20 70 52 69 67 Cell ){. pRig
17f12 68 74 20 3d 20 26 70 50 61 72 65 6e 74 2d 3e 61 ht = &pParent->a
17f13 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 Data[pParent->hd
17f14 72 4f 66 66 73 65 74 2b 38 5d 3b 0a 20 20 7d 65 rOffset+8];. }e
17f15 6c 73 65 7b 0a 20 20 20 20 70 52 69 67 68 74 20 lse{. pRight
17f16 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 65 = findCell(pPare
17f17 6e 74 2c 20 69 2b 6e 78 44 69 76 2d 70 50 61 72 nt, i+nxDiv-pPar
17f18 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29 3b ent->nOverflow);
17f19 0a 20 20 7d 0a 20 20 70 67 6e 6f 20 3d 20 67 65 . }. pgno = ge
17f1a 74 34 62 79 74 65 28 70 52 69 67 68 74 29 3b 0a t4byte(pRight);.
17f1b 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 while( 1 ){.
17f1c 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 rc = getAndIni
17f1d 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c tPage(pBt, pgno,
17f1e 20 26 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 &apOld[i]);.
17f1f 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
17f20 20 6d 65 6d 73 65 74 28 61 70 4f 6c 64 2c 20 30 memset(apOld, 0
17f21 2c 20 28 69 2b 31 29 2a 73 69 7a 65 6f 66 28 4d , (i+1)*sizeof(M
17f22 65 6d 50 61 67 65 2a 29 29 3b 0a 20 20 20 20 20 emPage*));.
17f23 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
17f24 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20 eanup;. }.
17f25 20 6e 4d 61 78 43 65 6c 6c 73 20 2b 3d 20 31 2b nMaxCells += 1+
17f26 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 43 65 6c 6c 2b apOld[i]->nCell+
17f27 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 4f 76 65 72 66 apOld[i]->nOverf
17f28 6c 6f 77 3b 0a 20 20 20 20 69 66 28 20 28 69 2d low;. if( (i-
17f29 2d 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 0a -)==0 ) break;..
17f2a 20 20 20 20 69 66 28 20 69 2b 6e 78 44 69 76 3d if( i+nxDiv=
17f2b 3d 70 50 61 72 65 6e 74 2d 3e 61 4f 76 66 6c 5b =pParent->aOvfl[
17f2c 30 5d 2e 69 64 78 20 26 26 20 70 50 61 72 65 6e 0].idx && pParen
17f2d 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a t->nOverflow ){.
17f2e 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20 3d apDiv[i] =
17f2f 20 70 50 61 72 65 6e 74 2d 3e 61 4f 76 66 6c 5b pParent->aOvfl[
17f30 30 5d 2e 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 0].pCell;.
17f31 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 pgno = get4byte(
17f32 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20 apDiv[i]);.
17f33 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 63 65 6c 6c szNew[i] = cell
17f34 53 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c SizePtr(pParent,
17f35 20 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 apDiv[i]);.
17f36 20 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 pParent->nOver
17f37 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 7d 65 flow = 0;. }e
17f38 6c 73 65 7b 0a 20 20 20 20 20 20 61 70 44 69 76 lse{. apDiv
17f39 5b 69 5d 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 [i] = findCell(p
17f3a 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69 76 2d Parent, i+nxDiv-
17f3b 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c pParent->nOverfl
17f3c 6f 77 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 ow);. pgno
17f3d 3d 20 67 65 74 34 62 79 74 65 28 61 70 44 69 76 = get4byte(apDiv
17f3e 5b 69 5d 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 [i]);. szNe
17f3f 77 5b 69 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50 w[i] = cellSizeP
17f40 74 72 28 70 50 61 72 65 6e 74 2c 20 61 70 44 69 tr(pParent, apDi
17f41 76 5b 69 5d 29 3b 0a 0a 20 20 20 20 20 20 2f 2a v[i]);.. /*
17f42 20 44 72 6f 70 20 74 68 65 20 63 65 6c 6c 20 66 Drop the cell f
17f43 72 6f 6d 20 74 68 65 20 70 61 72 65 6e 74 20 70 rom the parent p
17f44 61 67 65 2e 20 61 70 44 69 76 5b 69 5d 20 73 74 age. apDiv[i] st
17f45 69 6c 6c 20 70 6f 69 6e 74 73 20 74 6f 0a 20 20 ill points to.
17f46 20 20 20 20 2a 2a 20 74 68 65 20 63 65 6c 6c 20 ** the cell
17f47 77 69 74 68 69 6e 20 74 68 65 20 70 61 72 65 6e within the paren
17f48 74 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 69 t, even though i
17f49 74 20 68 61 73 20 62 65 65 6e 20 64 72 6f 70 70 t has been dropp
17f4a 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 ed.. ** Thi
17f4b 73 20 69 73 20 73 61 66 65 20 62 65 63 61 75 73 s is safe becaus
17f4c 65 20 64 72 6f 70 70 69 6e 67 20 61 20 63 65 6c e dropping a cel
17f4d 6c 20 6f 6e 6c 79 20 6f 76 65 72 77 72 69 74 65 l only overwrite
17f4e 73 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 20 s the first.
17f4f 20 20 2a 2a 20 66 6f 75 72 20 62 79 74 65 73 20 ** four bytes
17f50 6f 66 20 69 74 2c 20 61 6e 64 20 74 68 69 73 20 of it, and this
17f51 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f function does no
17f52 74 20 6e 65 65 64 20 74 68 65 20 66 69 72 73 74 t need the first
17f53 0a 20 20 20 20 20 20 2a 2a 20 66 6f 75 72 20 62 . ** four b
17f54 79 74 65 73 20 6f 66 20 74 68 65 20 64 69 76 69 ytes of the divi
17f55 64 65 72 20 63 65 6c 6c 2e 20 53 6f 20 74 68 65 der cell. So the
17f56 20 70 6f 69 6e 74 65 72 20 69 73 20 73 61 66 65 pointer is safe
17f57 20 74 6f 20 75 73 65 0a 20 20 20 20 20 20 2a 2a to use. **
17f58 20 6c 61 74 65 72 20 6f 6e 2e 20 20 0a 20 20 20 later on. .
17f59 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 55 **. ** U
17f5a 6e 6c 65 73 73 20 53 51 4c 69 74 65 20 69 73 20 nless SQLite is
17f5b 63 6f 6d 70 69 6c 65 64 20 69 6e 20 73 65 63 75 compiled in secu
17f5c 72 65 2d 64 65 6c 65 74 65 20 6d 6f 64 65 2e 20 re-delete mode.
17f5d 49 6e 20 74 68 69 73 20 63 61 73 65 2c 0a 20 20 In this case,.
17f5e 20 20 20 20 2a 2a 20 74 68 65 20 64 72 6f 70 43 ** the dropC
17f5f 65 6c 6c 28 29 20 72 6f 75 74 69 6e 65 20 77 69 ell() routine wi
17f60 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 74 68 65 ll overwrite the
17f61 20 65 6e 74 69 72 65 20 63 65 6c 6c 20 77 69 74 entire cell wit
17f62 68 20 7a 65 72 6f 65 73 2e 0a 20 20 20 20 20 20 h zeroes..
17f63 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c ** In this case,
17f64 20 74 65 6d 70 6f 72 61 72 69 6c 79 20 63 6f 70 temporarily cop
17f65 79 20 74 68 65 20 63 65 6c 6c 20 69 6e 74 6f 20 y the cell into
17f66 74 68 65 20 61 4f 76 66 6c 53 70 61 63 65 5b 5d the aOvflSpace[]
17f67 0a 20 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72 . ** buffer
17f68 2e 20 49 74 20 77 69 6c 6c 20 62 65 20 63 6f 70 . It will be cop
17f69 69 65 64 20 6f 75 74 20 61 67 61 69 6e 20 61 73 ied out again as
17f6a 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 61 53 70 soon as the aSp
17f6b 61 63 65 5b 5d 20 62 75 66 66 65 72 0a 20 20 20 ace[] buffer.
17f6c 20 20 20 2a 2a 20 69 73 20 61 6c 6c 6f 63 61 74 ** is allocat
17f6d 65 64 2e 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 ed. */.#ifdef S
17f6e 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c QLITE_SECURE_DEL
17f6f 45 54 45 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 ETE. memcpy
17f70 28 26 61 4f 76 66 6c 53 70 61 63 65 5b 61 70 44 (&aOvflSpace[apD
17f71 69 76 5b 69 5d 2d 70 50 61 72 65 6e 74 2d 3e 61 iv[i]-pParent->a
17f72 44 61 74 61 5d 2c 20 61 70 44 69 76 5b 69 5d 2c Data], apDiv[i],
17f73 20 73 7a 4e 65 77 5b 69 5d 29 3b 0a 20 20 20 20 szNew[i]);.
17f74 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 26 61 4f apDiv[i] = &aO
17f75 76 66 6c 53 70 61 63 65 5b 61 70 44 69 76 5b 69 vflSpace[apDiv[i
17f76 5d 2d 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 ]-pParent->aData
17f77 5d 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 ];.#endif.
17f78 64 72 6f 70 43 65 6c 6c 28 70 50 61 72 65 6e 74 dropCell(pParent
17f79 2c 20 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e , i+nxDiv-pParen
17f7a 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2c 20 73 7a t->nOverflow, sz
17f7b 4e 65 77 5b 69 5d 2c 20 26 72 63 29 3b 0a 20 20 New[i], &rc);.
17f7c 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 }. }.. /* Ma
17f7d 6b 65 20 6e 4d 61 78 43 65 6c 6c 73 20 61 20 6d ke nMaxCells a m
17f7e 75 6c 74 69 70 6c 65 20 6f 66 20 34 20 69 6e 20 ultiple of 4 in
17f7f 6f 72 64 65 72 20 74 6f 20 70 72 65 73 65 72 76 order to preserv
17f80 65 20 38 2d 62 79 74 65 0a 20 20 2a 2a 20 61 6c e 8-byte. ** al
17f81 69 67 6e 6d 65 6e 74 20 2a 2f 0a 20 20 6e 4d 61 ignment */. nMa
17f82 78 43 65 6c 6c 73 20 3d 20 28 6e 4d 61 78 43 65 xCells = (nMaxCe
17f83 6c 6c 73 20 2b 20 33 29 26 7e 33 3b 0a 0a 20 20 lls + 3)&~3;..
17f84 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 65 /*. ** Allocate
17f85 20 73 70 61 63 65 20 66 6f 72 20 6d 65 6d 6f 72 space for memor
17f86 79 20 73 74 72 75 63 74 75 72 65 73 0a 20 20 2a y structures. *
17f87 2f 0a 20 20 6b 20 3d 20 70 42 74 2d 3e 70 61 67 /. k = pBt->pag
17f88 65 53 69 7a 65 20 2b 20 52 4f 55 4e 44 38 28 73 eSize + ROUND8(s
17f89 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 3b izeof(MemPage));
17f8a 0a 20 20 73 7a 53 63 72 61 74 63 68 20 3d 0a 20 . szScratch =.
17f8b 20 20 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 2a nMaxCells*
17f8c 73 69 7a 65 6f 66 28 75 38 2a 29 20 20 20 20 20 sizeof(u8*)
17f8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17f8e 20 20 2f 2a 20 61 70 43 65 6c 6c 20 2a 2f 0a 20 /* apCell */.
17f8f 20 20 20 20 2b 20 6e 4d 61 78 43 65 6c 6c 73 2a + nMaxCells*
17f90 73 69 7a 65 6f 66 28 75 31 36 29 20 20 20 20 20 sizeof(u16)
17f91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17f92 20 20 2f 2a 20 73 7a 43 65 6c 6c 20 2a 2f 0a 20 /* szCell */.
17f93 20 20 20 20 2b 20 70 42 74 2d 3e 70 61 67 65 53 + pBt->pageS
17f94 69 7a 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ize
17f95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17f96 20 20 2f 2a 20 61 53 70 61 63 65 31 20 2a 2f 0a /* aSpace1 */.
17f97 20 20 20 20 20 2b 20 6b 2a 6e 4f 6c 64 3b 20 20 + k*nOld;
17f98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17f99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17f9a 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 70 69 65 /* Page copie
17f9b 73 20 28 61 70 43 6f 70 79 29 20 2a 2f 0a 20 20 s (apCopy) */.
17f9c 61 70 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33 apCell = sqlite3
17f9d 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 20 73 ScratchMalloc( s
17f9e 7a 53 63 72 61 74 63 68 20 29 3b 20 0a 20 20 69 zScratch ); . i
17f9f 66 28 20 61 70 43 65 6c 6c 3d 3d 30 20 29 7b 0a f( apCell==0 ){.
17fa0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
17fa1 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 NOMEM;. goto
17fa2 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b balance_cleanup;
17fa3 0a 20 20 7d 0a 20 20 73 7a 43 65 6c 6c 20 3d 20 . }. szCell =
17fa4 28 75 31 36 2a 29 26 61 70 43 65 6c 6c 5b 6e 4d (u16*)&apCell[nM
17fa5 61 78 43 65 6c 6c 73 5d 3b 0a 20 20 61 53 70 61 axCells];. aSpa
17fa6 63 65 31 20 3d 20 28 75 38 2a 29 26 73 7a 43 65 ce1 = (u8*)&szCe
17fa7 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b 0a 20 ll[nMaxCells];.
17fa8 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 assert( EIGHT_B
17fa9 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 61 53 YTE_ALIGNMENT(aS
17faa 70 61 63 65 31 29 20 29 3b 0a 0a 20 20 2f 2a 0a pace1) );.. /*.
17fab 20 20 2a 2a 20 4c 6f 61 64 20 70 6f 69 6e 74 65 ** Load pointe
17fac 72 73 20 74 6f 20 61 6c 6c 20 63 65 6c 6c 73 20 rs to all cells
17fad 6f 6e 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 on sibling pages
17fae 20 61 6e 64 20 74 68 65 20 64 69 76 69 64 65 72 and the divider
17faf 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f cells. ** into
17fb0 20 74 68 65 20 6c 6f 63 61 6c 20 61 70 43 65 6c the local apCel
17fb1 6c 5b 5d 20 61 72 72 61 79 2e 20 20 4d 61 6b 65 l[] array. Make
17fb2 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 64 copies of the d
17fb3 69 76 69 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a ivider cells. *
17fb4 2a 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 * into space obt
17fb5 61 69 6e 65 64 20 66 72 6f 6d 20 61 53 70 61 63 ained from aSpac
17fb6 65 31 5b 5d 20 61 6e 64 20 72 65 6d 6f 76 65 20 e1[] and remove
17fb7 74 68 65 20 74 68 65 20 64 69 76 69 64 65 72 20 the the divider
17fb8 43 65 6c 6c 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 Cells. ** from
17fb9 70 50 61 72 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20 pParent.. **.
17fba 2a 2a 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e ** If the siblin
17fbb 67 73 20 61 72 65 20 6f 6e 20 6c 65 61 66 20 70 gs are on leaf p
17fbc 61 67 65 73 2c 20 74 68 65 6e 20 74 68 65 20 63 ages, then the c
17fbd 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 20 6f 66 hild pointers of
17fbe 20 74 68 65 0a 20 20 2a 2a 20 64 69 76 69 64 65 the. ** divide
17fbf 72 20 63 65 6c 6c 73 20 61 72 65 20 73 74 72 69 r cells are stri
17fc0 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20 63 65 pped from the ce
17fc1 6c 6c 73 20 62 65 66 6f 72 65 20 74 68 65 79 20 lls before they
17fc2 61 72 65 20 63 6f 70 69 65 64 0a 20 20 2a 2a 20 are copied. **
17fc3 69 6e 74 6f 20 61 53 70 61 63 65 31 5b 5d 2e 20 into aSpace1[].
17fc4 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 61 6c In this way, al
17fc5 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c l cells in apCel
17fc6 6c 5b 5d 20 61 72 65 20 77 69 74 68 6f 75 74 0a l[] are without.
17fc7 20 20 2a 2a 20 63 68 69 6c 64 20 70 6f 69 6e 74 ** child point
17fc8 65 72 73 2e 20 20 49 66 20 73 69 62 6c 69 6e 67 ers. If sibling
17fc9 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 s are not leaves
17fca 2c 20 74 68 65 6e 20 61 6c 6c 20 63 65 6c 6c 20 , then all cell
17fcb 69 6e 0a 20 20 2a 2a 20 61 70 43 65 6c 6c 5b 5d in. ** apCell[]
17fcc 20 69 6e 63 6c 75 64 65 20 63 68 69 6c 64 20 70 include child p
17fcd 6f 69 6e 74 65 72 73 2e 20 20 45 69 74 68 65 72 ointers. Either
17fce 20 77 61 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 way, all cells
17fcf 69 6e 20 61 70 43 65 6c 6c 5b 5d 0a 20 20 2a 2a in apCell[]. **
17fd0 20 61 72 65 20 61 6c 69 6b 65 2e 0a 20 20 2a 2a are alike.. **
17fd1 0a 20 20 2a 2a 20 6c 65 61 66 43 6f 72 72 65 63 . ** leafCorrec
17fd2 74 69 6f 6e 3a 20 20 34 20 69 66 20 70 50 61 67 tion: 4 if pPag
17fd3 65 20 69 73 20 61 20 6c 65 61 66 2e 20 20 30 20 e is a leaf. 0
17fd4 69 66 20 70 50 61 67 65 20 69 73 20 6e 6f 74 20 if pPage is not
17fd5 61 20 6c 65 61 66 2e 0a 20 20 2a 2a 20 20 20 20 a leaf.. **
17fd6 20 20 20 6c 65 61 66 44 61 74 61 3a 20 20 31 20 leafData: 1
17fd7 69 66 20 70 50 61 67 65 20 68 6f 6c 64 73 20 6b if pPage holds k
17fd8 65 79 2b 64 61 74 61 20 61 6e 64 20 70 50 61 72 ey+data and pPar
17fd9 65 6e 74 20 68 6f 6c 64 73 20 6f 6e 6c 79 20 6b ent holds only k
17fda 65 79 73 2e 0a 20 20 2a 2f 0a 20 20 6c 65 61 66 eys.. */. leaf
17fdb 43 6f 72 72 65 63 74 69 6f 6e 20 3d 20 61 70 4f Correction = apO
17fdc 6c 64 5b 30 5d 2d 3e 6c 65 61 66 2a 34 3b 0a 20 ld[0]->leaf*4;.
17fdd 20 6c 65 61 66 44 61 74 61 20 3d 20 61 70 4f 6c leafData = apOl
17fde 64 5b 30 5d 2d 3e 68 61 73 44 61 74 61 3b 0a 20 d[0]->hasData;.
17fdf 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 for(i=0; i<nOld
17fe0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 ; i++){. int
17fe1 6c 69 6d 69 74 3b 0a 20 20 20 20 0a 20 20 20 20 limit;. .
17fe2 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 /* Before doing
17fe3 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c 20 74 anything else, t
17fe4 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 ake a copy of th
17fe5 65 20 69 27 74 68 20 6f 72 69 67 69 6e 61 6c 20 e i'th original
17fe6 73 69 62 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 54 sibling. ** T
17fe7 68 65 20 72 65 73 74 20 6f 66 20 74 68 69 73 20 he rest of this
17fe8 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 75 73 function will us
17fe9 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 e data from the
17fea 63 6f 70 69 65 73 20 72 61 74 68 65 72 0a 20 20 copies rather.
17feb 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6f 72 ** that the or
17fec 69 67 69 6e 61 6c 20 70 61 67 65 73 20 73 69 6e iginal pages sin
17fed 63 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 ce the original
17fee 70 61 67 65 73 20 77 69 6c 6c 20 62 65 20 69 6e pages will be in
17fef 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 72 6f 63 the. ** proc
17ff0 65 73 73 20 6f 66 20 62 65 69 6e 67 20 6f 76 65 ess of being ove
17ff1 72 77 72 69 74 74 65 6e 2e 20 20 2a 2f 0a 20 20 rwritten. */.
17ff2 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20 MemPage *pOld
17ff3 3d 20 61 70 43 6f 70 79 5b 69 5d 20 3d 20 28 4d = apCopy[i] = (M
17ff4 65 6d 50 61 67 65 2a 29 26 61 53 70 61 63 65 31 emPage*)&aSpace1
17ff5 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2b [pBt->pageSize +
17ff6 20 6b 2a 69 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 k*i];. memcp
17ff7 79 28 70 4f 6c 64 2c 20 61 70 4f 6c 64 5b 69 5d y(pOld, apOld[i]
17ff8 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 , sizeof(MemPage
17ff9 29 29 3b 0a 20 20 20 20 70 4f 6c 64 2d 3e 61 44 ));. pOld->aD
17ffa 61 74 61 20 3d 20 28 76 6f 69 64 2a 29 26 70 4f ata = (void*)&pO
17ffb 6c 64 5b 31 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 ld[1];. memcp
17ffc 79 28 70 4f 6c 64 2d 3e 61 44 61 74 61 2c 20 61 y(pOld->aData, a
17ffd 70 4f 6c 64 5b 69 5d 2d 3e 61 44 61 74 61 2c 20 pOld[i]->aData,
17ffe 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a pBt->pageSize);.
17fff 0a 20 20 20 20 6c 69 6d 69 74 20 3d 20 70 4f 6c . limit = pOl
18000 64 2d 3e 6e 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e d->nCell+pOld->n
18001 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 66 6f Overflow;. fo
18002 72 28 6a 3d 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20 r(j=0; j<limit;
18003 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 j++){. asse
18004 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 rt( nCell<nMaxCe
18005 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 61 70 43 lls );. apC
18006 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e ell[nCell] = fin
18007 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 4f dOverflowCell(pO
18008 6c 64 2c 20 6a 29 3b 0a 20 20 20 20 20 20 73 7a ld, j);. sz
18009 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 Cell[nCell] = ce
1800a 6c 6c 53 69 7a 65 50 74 72 28 70 4f 6c 64 2c 20 llSizePtr(pOld,
1800b 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a apCell[nCell]);.
1800c 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 20 nCell++;.
1800d 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3c 6e }. if( i<n
1800e 4f 6c 64 2d 31 20 26 26 20 21 6c 65 61 66 44 61 Old-1 && !leafDa
1800f 74 61 29 7b 0a 20 20 20 20 20 20 75 31 36 20 73 ta){. u16 s
18010 7a 20 3d 20 28 75 31 36 29 73 7a 4e 65 77 5b 69 z = (u16)szNew[i
18011 5d 3b 0a 20 20 20 20 20 20 75 38 20 2a 70 54 65 ];. u8 *pTe
18012 6d 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 mp;. assert
18013 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c ( nCell<nMaxCell
18014 73 20 29 3b 0a 20 20 20 20 20 20 73 7a 43 65 6c s );. szCel
18015 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a 3b 0a 20 l[nCell] = sz;.
18016 20 20 20 20 20 70 54 65 6d 70 20 3d 20 26 61 53 pTemp = &aS
18017 70 61 63 65 31 5b 69 53 70 61 63 65 31 5d 3b 0a pace1[iSpace1];.
18018 20 20 20 20 20 20 69 53 70 61 63 65 31 20 2b 3d iSpace1 +=
18019 20 73 7a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 sz;. asser
1801a 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 70 61 67 65 t( sz<=pBt->page
1801b 53 69 7a 65 2f 34 20 29 3b 0a 20 20 20 20 20 20 Size/4 );.
1801c 61 73 73 65 72 74 28 20 69 53 70 61 63 65 31 3c assert( iSpace1<
1801d 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 =pBt->pageSize )
1801e 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 ;. memcpy(p
1801f 54 65 6d 70 2c 20 61 70 44 69 76 5b 69 5d 2c 20 Temp, apDiv[i],
18020 73 7a 29 3b 0a 20 20 20 20 20 20 61 70 43 65 6c sz);. apCel
18021 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 70 54 65 6d 70 l[nCell] = pTemp
18022 2b 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b +leafCorrection;
18023 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c . assert( l
18024 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 eafCorrection==0
18025 20 7c 7c 20 6c 65 61 66 43 6f 72 72 65 63 74 69 || leafCorrecti
18026 6f 6e 3d 3d 34 20 29 3b 0a 20 20 20 20 20 20 73 on==4 );. s
18027 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 zCell[nCell] = s
18028 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 2d 20 6c zCell[nCell] - l
18029 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 eafCorrection;.
1802a 20 20 20 20 20 69 66 28 20 21 70 4f 6c 64 2d 3e if( !pOld->
1802b 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 20 20 leaf ){.
1802c 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 72 assert( leafCorr
1802d 65 63 74 69 6f 6e 3d 3d 30 20 29 3b 0a 20 20 20 ection==0 );.
1802e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 6c assert( pOl
1802f 64 2d 3e 68 64 72 4f 66 66 73 65 74 3d 3d 30 20 d->hdrOffset==0
18030 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 );. /* Th
18031 65 20 72 69 67 68 74 20 70 6f 69 6e 74 65 72 20 e right pointer
18032 6f 66 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 of the child pag
18033 65 20 70 4f 6c 64 20 62 65 63 6f 6d 65 73 20 74 e pOld becomes t
18034 68 65 20 6c 65 66 74 0a 20 20 20 20 20 20 20 20 he left.
18035 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 ** pointer of th
18036 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 2a e divider cell *
18037 2f 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 /. memcpy
18038 28 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 20 (apCell[nCell],
18039 26 70 4f 6c 64 2d 3e 61 44 61 74 61 5b 38 5d 2c &pOld->aData[8],
1803a 20 34 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 4);. }else
1803b 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
1803c 28 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e ( leafCorrection
1803d 3d 3d 34 20 29 3b 0a 20 20 20 20 20 20 20 20 69 ==4 );. i
1803e 66 28 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d f( szCell[nCell]
1803f 3c 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 <4 ){.
18040 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 /* Do not allow
18041 61 6e 79 20 63 65 6c 6c 73 20 73 6d 61 6c 6c 65 any cells smalle
18042 72 20 74 68 61 6e 20 34 20 62 79 74 65 73 2e 20 r than 4 bytes.
18043 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 7a 43 */. szC
18044 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 34 3b 0a ell[nCell] = 4;.
18045 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
18046 7d 0a 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b }. nCell++;
18047 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
18048 0a 20 20 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 . ** Figure out
18049 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
1804a 61 67 65 73 20 6e 65 65 64 65 64 20 74 6f 20 68 ages needed to h
1804b 6f 6c 64 20 61 6c 6c 20 6e 43 65 6c 6c 20 63 65 old all nCell ce
1804c 6c 6c 73 2e 0a 20 20 2a 2a 20 53 74 6f 72 65 20 lls.. ** Store
1804d 74 68 69 73 20 6e 75 6d 62 65 72 20 69 6e 20 22 this number in "
1804e 6b 22 2e 20 20 41 6c 73 6f 20 63 6f 6d 70 75 74 k". Also comput
1804f 65 20 73 7a 4e 65 77 5b 5d 20 77 68 69 63 68 20 e szNew[] which
18050 69 73 20 74 68 65 20 74 6f 74 61 6c 0a 20 20 2a is the total. *
18051 2a 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 * size of all ce
18052 6c 6c 73 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 lls on the i-th
18053 70 61 67 65 20 61 6e 64 20 63 6e 74 4e 65 77 5b page and cntNew[
18054 5d 20 77 68 69 63 68 20 69 73 20 74 68 65 20 69 ] which is the i
18055 6e 64 65 78 0a 20 20 2a 2a 20 69 6e 20 61 70 43 ndex. ** in apC
18056 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 63 65 6c ell[] of the cel
18057 6c 20 74 68 61 74 20 64 69 76 69 64 65 73 20 70 l that divides p
18058 61 67 65 20 69 20 66 72 6f 6d 20 70 61 67 65 20 age i from page
18059 69 2b 31 2e 20 20 0a 20 20 2a 2a 20 63 6e 74 4e i+1. . ** cntN
1805a 65 77 5b 6b 5d 20 73 68 6f 75 6c 64 20 65 71 75 ew[k] should equ
1805b 61 6c 20 6e 43 65 6c 6c 2e 0a 20 20 2a 2a 0a 20 al nCell.. **.
1805c 20 2a 2a 20 56 61 6c 75 65 73 20 63 6f 6d 70 75 ** Values compu
1805d 74 65 64 20 62 79 20 74 68 69 73 20 62 6c 6f 63 ted by this bloc
1805e 6b 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 k:. **. **
1805f 20 20 20 20 20 20 20 6b 3a 20 54 68 65 20 74 6f k: The to
18060 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 tal number of si
18061 62 6c 69 6e 67 20 70 61 67 65 73 0a 20 20 2a 2a bling pages. **
18062 20 20 20 20 73 7a 4e 65 77 5b 69 5d 3a 20 53 70 szNew[i]: Sp
18063 61 63 65 64 20 75 73 65 64 20 6f 6e 20 74 68 65 aced used on the
18064 20 69 2d 74 68 20 73 69 62 6c 69 6e 67 20 70 61 i-th sibling pa
18065 67 65 2e 0a 20 20 2a 2a 20 20 20 63 6e 74 4e 65 ge.. ** cntNe
18066 77 5b 69 5d 3a 20 49 6e 64 65 78 20 69 6e 20 61 w[i]: Index in a
18067 70 43 65 6c 6c 5b 5d 20 61 6e 64 20 73 7a 43 65 pCell[] and szCe
18068 6c 6c 5b 5d 20 66 6f 72 20 74 68 65 20 66 69 72 ll[] for the fir
18069 73 74 20 63 65 6c 6c 20 74 6f 0a 20 20 2a 2a 20 st cell to. **
1806a 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 the
1806b 20 72 69 67 68 74 20 6f 66 20 74 68 65 20 69 2d right of the i-
1806c 74 68 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e th sibling page.
1806d 0a 20 20 2a 2a 20 75 73 61 62 6c 65 53 70 61 63 . ** usableSpac
1806e 65 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 e: Number of byt
1806f 65 73 20 6f 66 20 73 70 61 63 65 20 61 76 61 69 es of space avai
18070 6c 61 62 6c 65 20 6f 6e 20 65 61 63 68 20 73 69 lable on each si
18071 62 6c 69 6e 67 2e 0a 20 20 2a 2a 20 0a 20 20 2a bling.. ** . *
18072 2f 0a 20 20 75 73 61 62 6c 65 53 70 61 63 65 20 /. usableSpace
18073 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
18074 65 20 2d 20 31 32 20 2b 20 6c 65 61 66 43 6f 72 e - 12 + leafCor
18075 72 65 63 74 69 6f 6e 3b 0a 20 20 66 6f 72 28 73 rection;. for(s
18076 75 62 74 6f 74 61 6c 3d 6b 3d 69 3d 30 3b 20 69 ubtotal=k=i=0; i
18077 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 <nCell; i++){.
18078 20 20 61 73 73 65 72 74 28 20 69 3c 6e 4d 61 78 assert( i<nMax
18079 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 73 75 62 Cells );. sub
1807a 74 6f 74 61 6c 20 2b 3d 20 73 7a 43 65 6c 6c 5b total += szCell[
1807b 69 5d 20 2b 20 32 3b 0a 20 20 20 20 69 66 28 20 i] + 2;. if(
1807c 73 75 62 74 6f 74 61 6c 20 3e 20 75 73 61 62 6c subtotal > usabl
1807d 65 53 70 61 63 65 20 29 7b 0a 20 20 20 20 20 20 eSpace ){.
1807e 73 7a 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f szNew[k] = subto
1807f 74 61 6c 20 2d 20 73 7a 43 65 6c 6c 5b 69 5d 3b tal - szCell[i];
18080 0a 20 20 20 20 20 20 63 6e 74 4e 65 77 5b 6b 5d . cntNew[k]
18081 20 3d 20 69 3b 0a 20 20 20 20 20 20 69 66 28 20 = i;. if(
18082 6c 65 61 66 44 61 74 61 20 29 7b 20 69 2d 2d 3b leafData ){ i--;
18083 20 7d 0a 20 20 20 20 20 20 73 75 62 74 6f 74 61 }. subtota
18084 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 6b 2b 2b l = 0;. k++
18085 3b 0a 20 20 20 20 20 20 69 66 28 20 6b 3e 4e 42 ;. if( k>NB
18086 2b 31 20 29 7b 20 72 63 20 3d 20 53 51 4c 49 54 +1 ){ rc = SQLIT
18087 45 5f 43 4f 52 52 55 50 54 3b 20 67 6f 74 6f 20 E_CORRUPT; goto
18088 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b balance_cleanup;
18089 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 }. }. }. s
1808a 7a 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 zNew[k] = subtot
1808b 61 6c 3b 0a 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 al;. cntNew[k]
1808c 3d 20 6e 43 65 6c 6c 3b 0a 20 20 6b 2b 2b 3b 0a = nCell;. k++;.
1808d 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 70 . /*. ** The p
1808e 61 63 6b 69 6e 67 20 63 6f 6d 70 75 74 65 64 20 acking computed
1808f 62 79 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 by the previous
18090 62 6c 6f 63 6b 20 69 73 20 62 69 61 73 65 64 20 block is biased
18091 74 6f 77 61 72 64 20 74 68 65 20 73 69 62 6c 69 toward the sibli
18092 6e 67 73 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 ngs. ** on the
18093 6c 65 66 74 20 73 69 64 65 2e 20 20 54 68 65 20 left side. The
18094 6c 65 66 74 20 73 69 62 6c 69 6e 67 73 20 61 72 left siblings ar
18095 65 20 61 6c 77 61 79 73 20 6e 65 61 72 6c 79 20 e always nearly
18096 66 75 6c 6c 2c 20 77 68 69 6c 65 20 74 68 65 0a full, while the.
18097 20 20 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 ** right-most
18098 73 69 62 6c 69 6e 67 20 6d 69 67 68 74 20 62 65 sibling might be
18099 20 6e 65 61 72 6c 79 20 65 6d 70 74 79 2e 20 20 nearly empty.
1809a 54 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f This block of co
1809b 64 65 20 61 74 74 65 6d 70 74 73 0a 20 20 2a 2a de attempts. **
1809c 20 74 6f 20 61 64 6a 75 73 74 20 74 68 65 20 70 to adjust the p
1809d 61 63 6b 69 6e 67 20 6f 66 20 73 69 62 6c 69 6e acking of siblin
1809e 67 73 20 74 6f 20 67 65 74 20 61 20 62 65 74 74 gs to get a bett
1809f 65 72 20 62 61 6c 61 6e 63 65 2e 0a 20 20 2a 2a er balance.. **
180a0 0a 20 20 2a 2a 20 54 68 69 73 20 61 64 6a 75 73 . ** This adjus
180a1 74 6d 65 6e 74 20 69 73 20 6d 6f 72 65 20 74 68 tment is more th
180a2 61 6e 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 an an optimizati
180a3 6f 6e 2e 20 20 54 68 65 20 70 61 63 6b 69 6e 67 on. The packing
180a4 20 61 62 6f 76 65 20 6d 69 67 68 74 0a 20 20 2a above might. *
180a5 2a 20 62 65 20 73 6f 20 6f 75 74 20 6f 66 20 62 * be so out of b
180a6 61 6c 61 6e 63 65 20 61 73 20 74 6f 20 62 65 20 alance as to be
180a7 69 6c 6c 65 67 61 6c 2e 20 20 46 6f 72 20 65 78 illegal. For ex
180a8 61 6d 70 6c 65 2c 20 74 68 65 20 72 69 67 68 74 ample, the right
180a9 2d 6d 6f 73 74 0a 20 20 2a 2a 20 73 69 62 6c 69 -most. ** sibli
180aa 6e 67 20 6d 69 67 68 74 20 62 65 20 63 6f 6d 70 ng might be comp
180ab 6c 65 74 65 6c 79 20 65 6d 70 74 79 2e 20 20 54 letely empty. T
180ac 68 69 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 his adjustment i
180ad 73 20 6e 6f 74 20 6f 70 74 69 6f 6e 61 6c 2e 0a s not optional..
180ae 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6b 2d 31 */. for(i=k-1
180af 3b 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 ; i>0; i--){.
180b0 20 69 6e 74 20 73 7a 52 69 67 68 74 20 3d 20 73 int szRight = s
180b1 7a 4e 65 77 5b 69 5d 3b 20 20 2f 2a 20 53 69 7a zNew[i]; /* Siz
180b2 65 20 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 e of sibling on
180b3 74 68 65 20 72 69 67 68 74 20 2a 2f 0a 20 20 20 the right */.
180b4 20 69 6e 74 20 73 7a 4c 65 66 74 20 3d 20 73 7a int szLeft = sz
180b5 4e 65 77 5b 69 2d 31 5d 3b 20 2f 2a 20 53 69 7a New[i-1]; /* Siz
180b6 65 20 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 e of sibling on
180b7 74 68 65 20 6c 65 66 74 20 2a 2f 0a 20 20 20 20 the left */.
180b8 69 6e 74 20 72 3b 20 20 20 20 20 20 20 20 20 20 int r;
180b9 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
180ba 72 69 67 68 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 right-most cell
180bb 69 6e 20 6c 65 66 74 20 73 69 62 6c 69 6e 67 20 in left sibling
180bc 2a 2f 0a 20 20 20 20 69 6e 74 20 64 3b 20 20 20 */. int d;
180bd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
180be 64 65 78 20 6f 66 20 66 69 72 73 74 20 63 65 6c dex of first cel
180bf 6c 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 l to the left of
180c0 20 72 69 67 68 74 20 73 69 62 6c 69 6e 67 20 2a right sibling *
180c1 2f 0a 0a 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 /.. r = cntNe
180c2 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 w[i-1] - 1;.
180c3 64 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 d = r + 1 - leaf
180c4 44 61 74 61 3b 0a 20 20 20 20 61 73 73 65 72 74 Data;. assert
180c5 28 20 64 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b ( d<nMaxCells );
180c6 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 3c 6e . assert( r<n
180c7 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 MaxCells );.
180c8 77 68 69 6c 65 28 20 73 7a 52 69 67 68 74 3d 3d while( szRight==
180c9 30 20 7c 7c 20 73 7a 52 69 67 68 74 2b 73 7a 43 0 || szRight+szC
180ca 65 6c 6c 5b 64 5d 2b 32 3c 3d 73 7a 4c 65 66 74 ell[d]+2<=szLeft
180cb 2d 28 73 7a 43 65 6c 6c 5b 72 5d 2b 32 29 20 29 -(szCell[r]+2) )
180cc 7b 0a 20 20 20 20 20 20 73 7a 52 69 67 68 74 20 {. szRight
180cd 2b 3d 20 73 7a 43 65 6c 6c 5b 64 5d 20 2b 20 32 += szCell[d] + 2
180ce 3b 0a 20 20 20 20 20 20 73 7a 4c 65 66 74 20 2d ;. szLeft -
180cf 3d 20 73 7a 43 65 6c 6c 5b 72 5d 20 2b 20 32 3b = szCell[r] + 2;
180d0 0a 20 20 20 20 20 20 63 6e 74 4e 65 77 5b 69 2d . cntNew[i-
180d1 31 5d 2d 2d 3b 0a 20 20 20 20 20 20 72 20 3d 20 1]--;. r =
180d2 63 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d 20 31 3b cntNew[i-1] - 1;
180d3 0a 20 20 20 20 20 20 64 20 3d 20 72 20 2b 20 31 . d = r + 1
180d4 20 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20 20 20 - leafData;.
180d5 20 7d 0a 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 }. szNew[i]
180d6 3d 20 73 7a 52 69 67 68 74 3b 0a 20 20 20 20 73 = szRight;. s
180d7 7a 4e 65 77 5b 69 2d 31 5d 20 3d 20 73 7a 4c 65 zNew[i-1] = szLe
180d8 66 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 69 ft;. }.. /* Ei
180d9 74 68 65 72 20 77 65 20 66 6f 75 6e 64 20 6f 6e ther we found on
180da 65 20 6f 72 20 6d 6f 72 65 20 63 65 6c 6c 73 20 e or more cells
180db 28 63 6e 74 6e 65 77 5b 30 5d 29 3e 30 29 20 6f (cntnew[0])>0) o
180dc 72 20 70 50 61 67 65 20 69 73 0a 20 20 2a 2a 20 r pPage is. **
180dd 61 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 a virtual root p
180de 61 67 65 2e 20 20 41 20 76 69 72 74 75 61 6c 20 age. A virtual
180df 72 6f 6f 74 20 70 61 67 65 20 69 73 20 77 68 65 root page is whe
180e0 6e 20 74 68 65 20 72 65 61 6c 20 72 6f 6f 74 0a n the real root.
180e1 20 20 2a 2a 20 70 61 67 65 20 69 73 20 70 61 67 ** page is pag
180e2 65 20 31 20 61 6e 64 20 77 65 20 61 72 65 20 74 e 1 and we are t
180e3 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 6f 66 he only child of
180e4 20 74 68 61 74 20 70 61 67 65 2e 0a 20 20 2a 2f that page.. */
180e5 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74 4e 65 . assert( cntNe
180e6 77 5b 30 5d 3e 30 20 7c 7c 20 28 70 50 61 72 65 w[0]>0 || (pPare
180e7 6e 74 2d 3e 70 67 6e 6f 3d 3d 31 20 26 26 20 70 nt->pgno==1 && p
180e8 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 Parent->nCell==0
180e9 29 20 29 3b 0a 0a 20 20 54 52 41 43 45 28 28 22 ) );.. TRACE(("
180ea 42 41 4c 41 4e 43 45 3a 20 6f 6c 64 3a 20 25 64 BALANCE: old: %d
180eb 20 25 64 20 25 64 20 20 22 2c 0a 20 20 20 20 61 %d %d ",. a
180ec 70 4f 6c 64 5b 30 5d 2d 3e 70 67 6e 6f 2c 20 0a pOld[0]->pgno, .
180ed 20 20 20 20 6e 4f 6c 64 3e 3d 32 20 3f 20 61 70 nOld>=2 ? ap
180ee 4f 6c 64 5b 31 5d 2d 3e 70 67 6e 6f 20 3a 20 30 Old[1]->pgno : 0
180ef 2c 0a 20 20 20 20 6e 4f 6c 64 3e 3d 33 20 3f 20 ,. nOld>=3 ?
180f0 61 70 4f 6c 64 5b 32 5d 2d 3e 70 67 6e 6f 20 3a apOld[2]->pgno :
180f1 20 30 0a 20 20 29 29 3b 0a 0a 20 20 2f 2a 0a 20 0. ));.. /*.
180f2 20 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6b 20 6e ** Allocate k n
180f3 65 77 20 70 61 67 65 73 2e 20 20 52 65 75 73 65 ew pages. Reuse
180f4 20 6f 6c 64 20 70 61 67 65 73 20 77 68 65 72 65 old pages where
180f5 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a possible.. */.
180f6 20 20 69 66 28 20 61 70 4f 6c 64 5b 30 5d 2d 3e if( apOld[0]->
180f7 70 67 6e 6f 3c 3d 31 20 29 7b 0a 20 20 20 20 72 pgno<=1 ){. r
180f8 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 c = SQLITE_CORRU
180f9 50 54 3b 0a 20 20 20 20 67 6f 74 6f 20 62 61 6c PT;. goto bal
180fa 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 ance_cleanup;.
180fb 7d 0a 20 20 70 61 67 65 46 6c 61 67 73 20 3d 20 }. pageFlags =
180fc 61 70 4f 6c 64 5b 30 5d 2d 3e 61 44 61 74 61 5b apOld[0]->aData[
180fd 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 0];. for(i=0; i
180fe 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 <k; i++){. Me
180ff 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20 20 mPage *pNew;.
18100 20 69 66 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20 if( i<nOld ){.
18101 20 20 20 20 20 70 4e 65 77 20 3d 20 61 70 4e 65 pNew = apNe
18102 77 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b 69 5d 3b w[i] = apOld[i];
18103 0a 20 20 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 . apOld[i]
18104 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = 0;. rc =
18105 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
18106 65 28 70 4e 65 77 2d 3e 70 44 62 50 61 67 65 29 e(pNew->pDbPage)
18107 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a ;. nNew++;.
18108 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 if( rc ) g
18109 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 oto balance_clea
1810a 6e 75 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a nup;. }else{.
1810b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3e assert( i>
1810c 30 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 );. rc =
1810d 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 allocateBtreePag
1810e 65 28 70 42 74 2c 20 26 70 4e 65 77 2c 20 26 70 e(pBt, &pNew, &p
1810f 67 6e 6f 2c 20 70 67 6e 6f 2c 20 30 29 3b 0a 20 gno, pgno, 0);.
18110 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f if( rc ) go
18111 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e to balance_clean
18112 75 70 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b up;. apNew[
18113 69 5d 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 i] = pNew;.
18114 20 6e 4e 65 77 2b 2b 3b 0a 0a 20 20 20 20 20 20 nNew++;..
18115 2f 2a 20 53 65 74 20 74 68 65 20 70 6f 69 6e 74 /* Set the point
18116 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 er-map entry for
18117 20 74 68 65 20 6e 65 77 20 73 69 62 6c 69 6e 67 the new sibling
18118 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 page. */.
18119 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d if( ISAUTOVACUUM
1811a 20 29 7b 0a 20 20 20 20 20 20 20 20 70 74 72 6d ){. ptrm
1811b 61 70 50 75 74 28 70 42 74 2c 20 70 4e 65 77 2d apPut(pBt, pNew-
1811c 3e 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 >pgno, PTRMAP_BT
1811d 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67 REE, pParent->pg
1811e 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 no, &rc);.
1811f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
18120 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
18121 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
18122 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 7d eanup;. }
18123 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
18124 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e }.. /* Free an
18125 79 20 6f 6c 64 20 70 61 67 65 73 20 74 68 61 74 y old pages that
18126 20 77 65 72 65 20 6e 6f 74 20 72 65 75 73 65 64 were not reused
18127 20 61 73 20 6e 65 77 20 70 61 67 65 73 2e 0a 20 as new pages..
18128 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e */. while( i<n
18129 4f 6c 64 20 29 7b 0a 20 20 20 20 66 72 65 65 50 Old ){. freeP
1812a 61 67 65 28 61 70 4f 6c 64 5b 69 5d 2c 20 26 72 age(apOld[i], &r
1812b 63 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 c);. if( rc )
1812c 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
1812d 65 61 6e 75 70 3b 0a 20 20 20 20 72 65 6c 65 61 eanup;. relea
1812e 73 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 sePage(apOld[i])
1812f 3b 0a 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d ;. apOld[i] =
18130 20 30 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0;. i++;. }
18131 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 75 74 20 .. /*. ** Put
18132 74 68 65 20 6e 65 77 20 70 61 67 65 73 20 69 6e the new pages in
18133 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 accending order
18134 2e 20 20 54 68 69 73 20 68 65 6c 70 73 20 74 6f . This helps to
18135 0a 20 20 2a 2a 20 6b 65 65 70 20 65 6e 74 72 69 . ** keep entri
18136 65 73 20 69 6e 20 74 68 65 20 64 69 73 6b 20 66 es in the disk f
18137 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 73 6f 20 ile in order so
18138 74 68 61 74 20 61 20 73 63 61 6e 0a 20 20 2a 2a that a scan. **
18139 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 of the table is
1813a 20 61 20 6c 69 6e 65 61 72 20 73 63 61 6e 20 74 a linear scan t
1813b 68 72 6f 75 67 68 20 74 68 65 20 66 69 6c 65 2e hrough the file.
1813c 20 20 54 68 61 74 0a 20 20 2a 2a 20 69 6e 20 74 That. ** in t
1813d 75 72 6e 20 68 65 6c 70 73 20 74 68 65 20 6f 70 urn helps the op
1813e 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 74 erating system t
1813f 6f 20 64 65 6c 69 76 65 72 20 70 61 67 65 73 0a o deliver pages.
18140 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 64 69 ** from the di
18141 73 6b 20 6d 6f 72 65 20 72 61 70 69 64 6c 79 2e sk more rapidly.
18142 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 4f 28 . **. ** An O(
18143 6e 5e 32 29 20 69 6e 73 65 72 74 69 6f 6e 20 73 n^2) insertion s
18144 6f 72 74 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 ort algorithm is
18145 20 75 73 65 64 2c 20 62 75 74 20 73 69 6e 63 65 used, but since
18146 0a 20 20 2a 2a 20 6e 20 69 73 20 6e 65 76 65 72 . ** n is never
18147 20 6d 6f 72 65 20 74 68 61 6e 20 4e 42 20 28 61 more than NB (a
18148 20 73 6d 61 6c 6c 20 63 6f 6e 73 74 61 6e 74 29 small constant)
18149 2c 20 74 68 61 74 20 73 68 6f 75 6c 64 0a 20 20 , that should.
1814a 2a 2a 20 6e 6f 74 20 62 65 20 61 20 70 72 6f 62 ** not be a prob
1814b 6c 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 lem.. **. ** W
1814c 68 65 6e 20 4e 42 3d 3d 33 2c 20 74 68 69 73 20 hen NB==3, this
1814d 6f 6e 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e one optimization
1814e 20 6d 61 6b 65 73 20 74 68 65 20 64 61 74 61 62 makes the datab
1814f 61 73 65 0a 20 20 2a 2a 20 61 62 6f 75 74 20 32 ase. ** about 2
18150 35 25 20 66 61 73 74 65 72 20 66 6f 72 20 6c 61 5% faster for la
18151 72 67 65 20 69 6e 73 65 72 74 69 6f 6e 73 20 61 rge insertions a
18152 6e 64 20 64 65 6c 65 74 69 6f 6e 73 2e 0a 20 20 nd deletions..
18153 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c */. for(i=0; i<
18154 6b 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 k-1; i++){. i
18155 6e 74 20 6d 69 6e 56 20 3d 20 61 70 4e 65 77 5b nt minV = apNew[
18156 69 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 69 6e i]->pgno;. in
18157 74 20 6d 69 6e 49 20 3d 20 69 3b 0a 20 20 20 20 t minI = i;.
18158 66 6f 72 28 6a 3d 69 2b 31 3b 20 6a 3c 6b 3b 20 for(j=i+1; j<k;
18159 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 j++){. if(
1815a 61 70 4e 65 77 5b 6a 5d 2d 3e 70 67 6e 6f 3c 28 apNew[j]->pgno<(
1815b 75 6e 73 69 67 6e 65 64 29 6d 69 6e 56 20 29 7b unsigned)minV ){
1815c 0a 20 20 20 20 20 20 20 20 6d 69 6e 49 20 3d 20 . minI =
1815d 6a 3b 0a 20 20 20 20 20 20 20 20 6d 69 6e 56 20 j;. minV
1815e 3d 20 61 70 4e 65 77 5b 6a 5d 2d 3e 70 67 6e 6f = apNew[j]->pgno
1815f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
18160 20 20 20 20 69 66 28 20 6d 69 6e 49 3e 69 20 29 if( minI>i )
18161 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 3b 0a 20 {. int t;.
18162 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 MemPage *pT
18163 3b 0a 20 20 20 20 20 20 74 20 3d 20 61 70 4e 65 ;. t = apNe
18164 77 5b 69 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 w[i]->pgno;.
18165 20 20 70 54 20 3d 20 61 70 4e 65 77 5b 69 5d 3b pT = apNew[i];
18166 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 69 5d 20 . apNew[i]
18167 3d 20 61 70 4e 65 77 5b 6d 69 6e 49 5d 3b 0a 20 = apNew[minI];.
18168 20 20 20 20 20 61 70 4e 65 77 5b 6d 69 6e 49 5d apNew[minI]
18169 20 3d 20 70 54 3b 0a 20 20 20 20 7d 0a 20 20 7d = pT;. }. }
1816a 0a 20 20 54 52 41 43 45 28 28 22 6e 65 77 3a 20 . TRACE(("new:
1816b 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25 64 %d(%d) %d(%d) %d
1816c 28 25 64 29 20 25 64 28 25 64 29 20 25 64 28 25 (%d) %d(%d) %d(%
1816d 64 29 5c 6e 22 2c 0a 20 20 20 20 61 70 4e 65 77 d)\n",. apNew
1816e 5b 30 5d 2d 3e 70 67 6e 6f 2c 20 73 7a 4e 65 77 [0]->pgno, szNew
1816f 5b 30 5d 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 32 [0],. nNew>=2
18170 20 3f 20 61 70 4e 65 77 5b 31 5d 2d 3e 70 67 6e ? apNew[1]->pgn
18171 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 32 20 3f o : 0, nNew>=2 ?
18172 20 73 7a 4e 65 77 5b 31 5d 20 3a 20 30 2c 0a 20 szNew[1] : 0,.
18173 20 20 20 6e 4e 65 77 3e 3d 33 20 3f 20 61 70 4e nNew>=3 ? apN
18174 65 77 5b 32 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c ew[2]->pgno : 0,
18175 20 6e 4e 65 77 3e 3d 33 20 3f 20 73 7a 4e 65 77 nNew>=3 ? szNew
18176 5b 32 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 [2] : 0,. nNe
18177 77 3e 3d 34 20 3f 20 61 70 4e 65 77 5b 33 5d 2d w>=4 ? apNew[3]-
18178 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e >pgno : 0, nNew>
18179 3d 34 20 3f 20 73 7a 4e 65 77 5b 33 5d 20 3a 20 =4 ? szNew[3] :
1817a 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 35 20 3f 0,. nNew>=5 ?
1817b 20 61 70 4e 65 77 5b 34 5d 2d 3e 70 67 6e 6f 20 apNew[4]->pgno
1817c 3a 20 30 2c 20 6e 4e 65 77 3e 3d 35 20 3f 20 73 : 0, nNew>=5 ? s
1817d 7a 4e 65 77 5b 34 5d 20 3a 20 30 29 29 3b 0a 0a zNew[4] : 0));..
1817e 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1817f 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
18180 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 e(pParent->pDbPa
18181 67 65 29 20 29 3b 0a 20 20 70 75 74 34 62 79 74 ge) );. put4byt
18182 65 28 70 52 69 67 68 74 2c 20 61 70 4e 65 77 5b e(pRight, apNew[
18183 6e 4e 65 77 2d 31 5d 2d 3e 70 67 6e 6f 29 3b 0a nNew-1]->pgno);.
18184 0a 20 20 2f 2a 0a 20 20 2a 2a 20 45 76 65 6e 6c . /*. ** Evenl
18185 79 20 64 69 73 74 72 69 62 75 74 65 20 74 68 65 y distribute the
18186 20 64 61 74 61 20 69 6e 20 61 70 43 65 6c 6c 5b data in apCell[
18187 5d 20 61 63 72 6f 73 73 20 74 68 65 20 6e 65 77 ] across the new
18188 20 70 61 67 65 73 2e 0a 20 20 2a 2a 20 49 6e 73 pages.. ** Ins
18189 65 72 74 20 64 69 76 69 64 65 72 20 63 65 6c 6c ert divider cell
1818a 73 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 20 61 s into pParent a
1818b 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20 2a s necessary.. *
1818c 2f 0a 20 20 6a 20 3d 20 30 3b 0a 20 20 66 6f 72 /. j = 0;. for
1818d 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b (i=0; i<nNew; i+
1818e 2b 29 7b 0a 20 20 20 20 2f 2a 20 41 73 73 65 6d +){. /* Assem
1818f 62 6c 65 20 74 68 65 20 6e 65 77 20 73 69 62 6c ble the new sibl
18190 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 ing page. */.
18191 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 20 3d MemPage *pNew =
18192 20 61 70 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 61 apNew[i];. a
18193 73 73 65 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c ssert( j<nMaxCel
18194 6c 73 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 ls );. zeroPa
18195 67 65 28 70 4e 65 77 2c 20 70 61 67 65 46 6c 61 ge(pNew, pageFla
18196 67 73 29 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c gs);. assembl
18197 65 50 61 67 65 28 70 4e 65 77 2c 20 63 6e 74 4e ePage(pNew, cntN
18198 65 77 5b 69 5d 2d 6a 2c 20 26 61 70 43 65 6c 6c ew[i]-j, &apCell
18199 5b 6a 5d 2c 20 26 73 7a 43 65 6c 6c 5b 6a 5d 29 [j], &szCell[j])
1819a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e ;. assert( pN
1819b 65 77 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 28 ew->nCell>0 || (
1819c 6e 4e 65 77 3d 3d 31 20 26 26 20 63 6e 74 4e 65 nNew==1 && cntNe
1819d 77 5b 30 5d 3d 3d 30 29 20 29 3b 0a 20 20 20 20 w[0]==0) );.
1819e 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 4f assert( pNew->nO
1819f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 20 verflow==0 );..
181a0 20 20 20 6a 20 3d 20 63 6e 74 4e 65 77 5b 69 5d j = cntNew[i]
181a1 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 ;.. /* If the
181a2 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20 61 73 sibling page as
181a3 73 65 6d 62 6c 65 64 20 61 62 6f 76 65 20 77 61 sembled above wa
181a4 73 20 6e 6f 74 20 74 68 65 20 72 69 67 68 74 2d s not the right-
181a5 6d 6f 73 74 20 73 69 62 6c 69 6e 67 2c 0a 20 20 most sibling,.
181a6 20 20 2a 2a 20 69 6e 73 65 72 74 20 61 20 64 69 ** insert a di
181a7 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20 vider cell into
181a8 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e the parent page.
181a9 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
181aa 72 74 28 20 69 3c 6e 4e 65 77 2d 31 20 7c 7c 20 rt( i<nNew-1 ||
181ab 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 j==nCell );.
181ac 69 66 28 20 6a 3c 6e 43 65 6c 6c 20 29 7b 0a 20 if( j<nCell ){.
181ad 20 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a u8 *pCell;.
181ae 20 20 20 20 20 20 75 38 20 2a 70 54 65 6d 70 3b u8 *pTemp;
181af 0a 20 20 20 20 20 20 69 6e 74 20 73 7a 3b 0a 0a . int sz;..
181b0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c assert( j<
181b1 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 nMaxCells );.
181b2 20 20 20 70 43 65 6c 6c 20 3d 20 61 70 43 65 6c pCell = apCel
181b3 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 73 7a 20 3d l[j];. sz =
181b4 20 73 7a 43 65 6c 6c 5b 6a 5d 20 2b 20 6c 65 61 szCell[j] + lea
181b5 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20 fCorrection;.
181b6 20 20 20 70 54 65 6d 70 20 3d 20 26 61 4f 76 66 pTemp = &aOvf
181b7 6c 53 70 61 63 65 5b 69 4f 76 66 6c 53 70 61 63 lSpace[iOvflSpac
181b8 65 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 e];. if( !p
181b9 4e 65 77 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 New->leaf ){.
181ba 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 memcpy(&pNe
181bb 77 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 70 43 65 w->aData[8], pCe
181bc 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 20 20 7d 65 ll, 4);. }e
181bd 6c 73 65 20 69 66 28 20 6c 65 61 66 44 61 74 61 lse if( leafData
181be 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 ){. /* I
181bf 66 20 74 68 65 20 74 72 65 65 20 69 73 20 61 20 f the tree is a
181c0 6c 65 61 66 2d 64 61 74 61 20 74 72 65 65 2c 20 leaf-data tree,
181c1 61 6e 64 20 74 68 65 20 73 69 62 6c 69 6e 67 73 and the siblings
181c2 20 61 72 65 20 6c 65 61 76 65 73 2c 20 0a 20 20 are leaves, .
181c3 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 ** then th
181c4 65 72 65 20 69 73 20 6e 6f 20 64 69 76 69 64 65 ere is no divide
181c5 72 20 63 65 6c 6c 20 69 6e 20 61 70 43 65 6c 6c r cell in apCell
181c6 5b 5d 2e 20 49 6e 73 74 65 61 64 2c 20 74 68 65 []. Instead, the
181c7 20 64 69 76 69 64 65 72 20 0a 20 20 20 20 20 20 divider .
181c8 20 20 2a 2a 20 63 65 6c 6c 20 63 6f 6e 73 69 73 ** cell consis
181c9 74 73 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 ts of the intege
181ca 72 20 6b 65 79 20 66 6f 72 20 74 68 65 20 72 69 r key for the ri
181cb 67 68 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 6f 66 ght-most cell of
181cc 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 . ** the
181cd 20 73 69 62 6c 69 6e 67 2d 70 61 67 65 20 61 73 sibling-page as
181ce 73 65 6d 62 6c 65 64 20 61 62 6f 76 65 20 6f 6e sembled above on
181cf 6c 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 ly.. */.
181d0 20 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 CellInfo
181d1 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 6a 2d info;. j-
181d2 2d 3b 0a 20 20 20 20 20 20 20 20 62 74 72 65 65 -;. btree
181d3 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 4e 65 ParseCellPtr(pNe
181d4 77 2c 20 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 69 w, apCell[j], &i
181d5 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 70 43 nfo);. pC
181d6 65 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 ell = pTemp;.
181d7 20 20 20 20 20 73 7a 20 3d 20 34 20 2b 20 70 75 sz = 4 + pu
181d8 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 34 tVarint(&pCell[4
181d9 5d 2c 20 69 6e 66 6f 2e 6e 4b 65 79 29 3b 0a 20 ], info.nKey);.
181da 20 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20 30 pTemp = 0
181db 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
181dc 20 20 20 20 20 20 20 70 43 65 6c 6c 20 2d 3d 20 pCell -=
181dd 34 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f 62 4;. /* Ob
181de 73 63 75 72 65 20 63 61 73 65 20 66 6f 72 20 6e scure case for n
181df 6f 6e 2d 6c 65 61 66 2d 64 61 74 61 20 74 72 65 on-leaf-data tre
181e0 65 73 3a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 es: If the cell
181e1 61 74 20 70 43 65 6c 6c 20 77 61 73 0a 20 20 20 at pCell was.
181e2 20 20 20 20 20 2a 2a 20 70 72 65 76 69 6f 75 73 ** previous
181e3 6c 79 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 6c ly stored on a l
181e4 65 61 66 20 6e 6f 64 65 2c 20 61 6e 64 20 69 74 eaf node, and it
181e5 73 20 72 65 70 6f 72 74 65 64 20 73 69 7a 65 20 s reported size
181e6 77 61 73 20 34 0a 20 20 20 20 20 20 20 20 2a 2a was 4. **
181e7 20 62 79 74 65 73 2c 20 74 68 65 6e 20 69 74 20 bytes, then it
181e8 6d 61 79 20 61 63 74 75 61 6c 6c 79 20 62 65 20 may actually be
181e9 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 69 smaller than thi
181ea 73 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 28 73 s . ** (s
181eb 65 65 20 62 74 72 65 65 50 61 72 73 65 43 65 6c ee btreeParseCel
181ec 6c 50 74 72 28 29 2c 20 34 20 62 79 74 65 73 20 lPtr(), 4 bytes
181ed 69 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 73 is the minimum s
181ee 69 7a 65 20 6f 66 0a 20 20 20 20 20 20 20 20 2a ize of. *
181ef 2a 20 61 6e 79 20 63 65 6c 6c 29 2e 20 42 75 74 * any cell). But
181f0 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 it is important
181f1 20 74 6f 20 70 61 73 73 20 74 68 65 20 63 6f 72 to pass the cor
181f2 72 65 63 74 20 73 69 7a 65 20 74 6f 20 0a 20 20 rect size to .
181f3 20 20 20 20 20 20 2a 2a 20 69 6e 73 65 72 74 43 ** insertC
181f4 65 6c 6c 28 29 2c 20 73 6f 20 72 65 70 61 72 73 ell(), so repars
181f5 65 20 74 68 65 20 63 65 6c 6c 20 6e 6f 77 2e 0a e the cell now..
181f6 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
181f7 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 ** Note that
181f8 74 68 69 73 20 63 61 6e 20 6e 65 76 65 72 20 68 this can never h
181f9 61 70 70 65 6e 20 69 6e 20 61 6e 20 53 51 4c 69 appen in an SQLi
181fa 74 65 20 64 61 74 61 20 66 69 6c 65 2c 20 61 73 te data file, as
181fb 20 61 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 all. **
181fc 63 65 6c 6c 73 20 61 72 65 20 61 74 20 6c 65 61 cells are at lea
181fd 73 74 20 34 20 62 79 74 65 73 2e 20 49 74 20 6f st 4 bytes. It o
181fe 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 6e 20 62 nly happens in b
181ff 2d 74 72 65 65 73 20 75 73 65 64 0a 20 20 20 20 -trees used.
18200 20 20 20 20 2a 2a 20 74 6f 20 65 76 61 6c 75 61 ** to evalua
18201 74 65 20 22 49 4e 20 28 53 45 4c 45 43 54 20 2e te "IN (SELECT .
18202 2e 2e 29 22 20 61 6e 64 20 73 69 6d 69 6c 61 72 ..)" and similar
18203 20 63 6c 61 75 73 65 73 2e 0a 20 20 20 20 20 20 clauses..
18204 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 */. if(
18205 20 73 7a 43 65 6c 6c 5b 6a 5d 3d 3d 34 20 29 7b szCell[j]==4 ){
18206 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 . asser
18207 74 28 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e t(leafCorrection
18208 3d 3d 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ==4);.
18209 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 sz = cellSizePtr
1820a 28 70 50 61 72 65 6e 74 2c 20 70 43 65 6c 6c 29 (pParent, pCell)
1820b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1820c 20 20 7d 0a 20 20 20 20 20 20 69 4f 76 66 6c 53 }. iOvflS
1820d 70 61 63 65 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 pace += sz;.
1820e 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d 70 42 assert( sz<=pB
1820f 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 29 3b t->pageSize/4 );
18210 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 . assert( i
18211 4f 76 66 6c 53 70 61 63 65 3c 3d 70 42 74 2d 3e OvflSpace<=pBt->
18212 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 pageSize );.
18213 20 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 insertCell(pPa
18214 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 70 43 65 rent, nxDiv, pCe
18215 6c 6c 2c 20 73 7a 2c 20 70 54 65 6d 70 2c 20 70 ll, sz, pTemp, p
18216 4e 65 77 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b New->pgno, &rc);
18217 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
18218 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 QLITE_OK ) goto
18219 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b balance_cleanup;
1821a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 . assert( s
1821b 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
1821c 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e teable(pParent->
1821d 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 pDbPage) );..
1821e 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 6e 78 j++;. nx
1821f 44 69 76 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d Div++;. }. }
18220 0a 20 20 61 73 73 65 72 74 28 20 6a 3d 3d 6e 43 . assert( j==nC
18221 65 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ell );. assert(
18222 20 6e 4f 6c 64 3e 30 20 29 3b 0a 20 20 61 73 73 nOld>0 );. ass
18223 65 72 74 28 20 6e 4e 65 77 3e 30 20 29 3b 0a 20 ert( nNew>0 );.
18224 20 69 66 28 20 28 70 61 67 65 46 6c 61 67 73 20 if( (pageFlags
18225 26 20 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20 29 & PTF_LEAF)==0 )
18226 7b 0a 20 20 20 20 75 38 20 2a 7a 43 68 69 6c 64 {. u8 *zChild
18227 20 3d 20 26 61 70 43 6f 70 79 5b 6e 4f 6c 64 2d = &apCopy[nOld-
18228 31 5d 2d 3e 61 44 61 74 61 5b 38 5d 3b 0a 20 20 1]->aData[8];.
18229 20 20 6d 65 6d 63 70 79 28 26 61 70 4e 65 77 5b memcpy(&apNew[
1822a 6e 4e 65 77 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 nNew-1]->aData[8
1822b 5d 2c 20 7a 43 68 69 6c 64 2c 20 34 29 3b 0a 20 ], zChild, 4);.
1822c 20 7d 0a 0a 20 20 69 66 28 20 69 73 52 6f 6f 74 }.. if( isRoot
1822d 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 && pParent->nCe
1822e 6c 6c 3d 3d 30 20 26 26 20 70 50 61 72 65 6e 74 ll==0 && pParent
1822f 2d 3e 68 64 72 4f 66 66 73 65 74 3c 3d 61 70 4e ->hdrOffset<=apN
18230 65 77 5b 30 5d 2d 3e 6e 46 72 65 65 20 29 7b 0a ew[0]->nFree ){.
18231 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 /* The root
18232 70 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72 page of the b-tr
18233 65 65 20 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 20 ee now contains
18234 6e 6f 20 63 65 6c 6c 73 2e 20 54 68 65 20 6f 6e no cells. The on
18235 6c 79 20 73 69 62 6c 69 6e 67 0a 20 20 20 20 2a ly sibling. *
18236 2a 20 70 61 67 65 20 69 73 20 74 68 65 20 72 69 * page is the ri
18237 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 74 68 65 ght-child of the
18238 20 70 61 72 65 6e 74 2e 20 43 6f 70 79 20 74 68 parent. Copy th
18239 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
1823a 65 0a 20 20 20 20 2a 2a 20 63 68 69 6c 64 20 70 e. ** child p
1823b 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 61 72 age into the par
1823c 65 6e 74 2c 20 64 65 63 72 65 61 73 69 6e 67 20 ent, decreasing
1823d 74 68 65 20 6f 76 65 72 61 6c 6c 20 68 65 69 67 the overall heig
1823e 68 74 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a ht of the. **
1823f 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 b-tree structur
18240 65 20 62 79 20 6f 6e 65 2e 20 54 68 69 73 20 69 e by one. This i
18241 73 20 64 65 73 63 72 69 62 65 64 20 61 73 20 74 s described as t
18242 68 65 20 22 62 61 6c 61 6e 63 65 2d 73 68 61 6c he "balance-shal
18243 6c 6f 77 65 72 22 0a 20 20 20 20 2a 2a 20 73 75 lower". ** su
18244 62 2d 61 6c 67 6f 72 69 74 68 6d 20 69 6e 20 73 b-algorithm in s
18245 6f 6d 65 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f ome documentatio
18246 6e 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a n.. **. **
18247 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 If this is an a
18248 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 uto-vacuum datab
18249 61 73 65 2c 20 74 68 65 20 63 61 6c 6c 20 74 6f ase, the call to
1824a 20 63 6f 70 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 copyNodeContent
1824b 28 29 20 0a 20 20 20 20 2a 2a 20 73 65 74 73 20 () . ** sets
1824c 61 6c 6c 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 all pointer-map
1824d 65 6e 74 72 69 65 73 20 63 6f 72 72 65 73 70 6f entries correspo
1824e 6e 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61 73 nding to databas
1824f 65 20 69 6d 61 67 65 20 70 61 67 65 73 20 0a 20 e image pages .
18250 20 20 20 2a 2a 20 66 6f 72 20 77 68 69 63 68 20 ** for which
18251 74 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 73 the pointer is s
18252 74 6f 72 65 64 20 77 69 74 68 69 6e 20 74 68 65 tored within the
18253 20 63 6f 6e 74 65 6e 74 20 62 65 69 6e 67 20 63 content being c
18254 6f 70 69 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 opied.. **.
18255 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 ** The second
18256 61 73 73 65 72 74 20 62 65 6c 6f 77 20 76 65 72 assert below ver
18257 69 66 69 65 73 20 74 68 61 74 20 74 68 65 20 63 ifies that the c
18258 68 69 6c 64 20 70 61 67 65 20 69 73 20 64 65 66 hild page is def
18259 72 61 67 6d 65 6e 74 65 64 0a 20 20 20 20 2a 2a ragmented. **
1825a 20 28 69 74 20 6d 75 73 74 20 62 65 2c 20 61 73 (it must be, as
1825b 20 69 74 20 77 61 73 20 6a 75 73 74 20 72 65 63 it was just rec
1825c 6f 6e 73 74 72 75 63 74 65 64 20 75 73 69 6e 67 onstructed using
1825d 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 29 29 assemblePage())
1825e 2e 20 54 68 69 73 0a 20 20 20 20 2a 2a 20 69 73 . This. ** is
1825f 20 69 6d 70 6f 72 74 61 6e 74 20 69 66 20 74 68 important if th
18260 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 68 61 e parent page ha
18261 70 70 65 6e 73 20 74 6f 20 62 65 20 70 61 67 65 ppens to be page
18262 20 31 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 1 of the databa
18263 73 65 0a 20 20 20 20 2a 2a 20 69 6d 61 67 65 2e se. ** image.
18264 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
18265 20 6e 4e 65 77 3d 3d 31 20 29 3b 0a 20 20 20 20 nNew==1 );.
18266 61 73 73 65 72 74 28 20 61 70 4e 65 77 5b 30 5d assert( apNew[0]
18267 2d 3e 6e 46 72 65 65 20 3d 3d 20 0a 20 20 20 20 ->nFree == .
18268 20 20 20 20 28 67 65 74 32 62 79 74 65 28 26 61 (get2byte(&a
18269 70 4e 65 77 5b 30 5d 2d 3e 61 44 61 74 61 5b 35 pNew[0]->aData[5
1826a 5d 29 2d 61 70 4e 65 77 5b 30 5d 2d 3e 63 65 6c ])-apNew[0]->cel
1826b 6c 4f 66 66 73 65 74 2d 61 70 4e 65 77 5b 30 5d lOffset-apNew[0]
1826c 2d 3e 6e 43 65 6c 6c 2a 32 29 20 0a 20 20 20 20 ->nCell*2) .
1826d 29 3b 0a 20 20 20 20 63 6f 70 79 4e 6f 64 65 43 );. copyNodeC
1826e 6f 6e 74 65 6e 74 28 61 70 4e 65 77 5b 30 5d 2c ontent(apNew[0],
1826f 20 70 50 61 72 65 6e 74 2c 20 26 72 63 29 3b 0a pParent, &rc);.
18270 20 20 20 20 66 72 65 65 50 61 67 65 28 61 70 4e freePage(apN
18271 65 77 5b 30 5d 2c 20 26 72 63 29 3b 0a 20 20 7d ew[0], &rc);. }
18272 65 6c 73 65 20 69 66 28 20 49 53 41 55 54 4f 56 else if( ISAUTOV
18273 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 ACUUM ){. /*
18274 46 69 78 20 74 68 65 20 70 6f 69 6e 74 65 72 2d Fix the pointer-
18275 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 map entries for
18276 61 6c 6c 20 74 68 65 20 63 65 6c 6c 73 20 74 68 all the cells th
18277 61 74 20 77 65 72 65 20 73 68 69 66 74 65 64 20 at were shifted
18278 61 72 6f 75 6e 64 2e 20 0a 20 20 20 20 2a 2a 20 around. . **
18279 54 68 65 72 65 20 61 72 65 20 73 65 76 65 72 61 There are severa
1827a 6c 20 64 69 66 66 65 72 65 6e 74 20 74 79 70 65 l different type
1827b 73 20 6f 66 20 70 6f 69 6e 74 65 72 2d 6d 61 70 s of pointer-map
1827c 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 6e 65 entries that ne
1827d 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20 ed to. ** be
1827e 64 65 61 6c 74 20 77 69 74 68 20 62 79 20 74 68 dealt with by th
1827f 69 73 20 72 6f 75 74 69 6e 65 2e 20 53 6f 6d 65 is routine. Some
18280 20 6f 66 20 74 68 65 73 65 20 68 61 76 65 20 62 of these have b
18281 65 65 6e 20 73 65 74 20 61 6c 72 65 61 64 79 2c een set already,
18282 20 62 75 74 0a 20 20 20 20 2a 2a 20 6d 61 6e 79 but. ** many
18283 20 68 61 76 65 20 6e 6f 74 2e 20 54 68 65 20 66 have not. The f
18284 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 20 73 75 ollowing is a su
18285 6d 6d 61 72 79 3a 0a 20 20 20 20 2a 2a 0a 20 20 mmary:. **.
18286 20 20 2a 2a 20 20 20 31 29 20 54 68 65 20 65 6e ** 1) The en
18287 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 tries associated
18288 20 77 69 74 68 20 6e 65 77 20 73 69 62 6c 69 6e with new siblin
18289 67 20 70 61 67 65 73 20 74 68 61 74 20 77 65 72 g pages that wer
1828a 65 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 20 20 20 e not. **
1828b 20 20 73 69 62 6c 69 6e 67 73 20 77 68 65 6e 20 siblings when
1828c 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 61 this function wa
1828d 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 73 65 20 s called. These
1828e 68 61 76 65 20 61 6c 72 65 61 64 79 0a 20 20 20 have already.
1828f 20 2a 2a 20 20 20 20 20 20 62 65 65 6e 20 73 65 ** been se
18290 74 2e 20 57 65 20 64 6f 6e 27 74 20 6e 65 65 64 t. We don't need
18291 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 to worry about
18292 6f 6c 64 20 73 69 62 6c 69 6e 67 73 20 74 68 61 old siblings tha
18293 74 20 77 65 72 65 0a 20 20 20 20 2a 2a 20 20 20 t were. **
18294 20 20 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 moved to the
18295 66 72 65 65 2d 6c 69 73 74 20 2d 20 74 68 65 20 free-list - the
18296 66 72 65 65 50 61 67 65 28 29 20 63 6f 64 65 20 freePage() code
18297 68 61 73 20 74 61 6b 65 6e 20 63 61 72 65 0a 20 has taken care.
18298 20 20 20 2a 2a 20 20 20 20 20 20 6f 66 20 74 68 ** of th
18299 6f 73 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ose.. **.
1829a 2a 2a 20 20 20 32 29 20 54 68 65 20 70 6f 69 6e ** 2) The poin
1829b 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 ter-map entries
1829c 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
1829d 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 6c the first overfl
1829e 6f 77 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 70 ow. ** p
1829f 61 67 65 20 69 6e 20 61 6e 79 20 6f 76 65 72 66 age in any overf
182a0 6c 6f 77 20 63 68 61 69 6e 73 20 75 73 65 64 20 low chains used
182a1 62 79 20 6e 65 77 20 64 69 76 69 64 65 72 20 63 by new divider c
182a2 65 6c 6c 73 2e 20 54 68 65 73 65 20 0a 20 20 20 ells. These .
182a3 20 2a 2a 20 20 20 20 20 20 68 61 76 65 20 61 6c ** have al
182a4 73 6f 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 so already been
182a5 74 61 6b 65 6e 20 63 61 72 65 20 6f 66 20 62 79 taken care of by
182a6 20 74 68 65 20 69 6e 73 65 72 74 43 65 6c 6c 28 the insertCell(
182a7 29 20 63 6f 64 65 2e 0a 20 20 20 20 2a 2a 0a 20 ) code.. **.
182a8 20 20 20 2a 2a 20 20 20 33 29 20 49 66 20 74 68 ** 3) If th
182a9 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 e sibling pages
182aa 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 are not leaves,
182ab 74 68 65 6e 20 74 68 65 20 63 68 69 6c 64 20 70 then the child p
182ac 61 67 65 73 20 6f 66 0a 20 20 20 20 2a 2a 20 20 ages of. **
182ad 20 20 20 20 63 65 6c 6c 73 20 73 74 6f 72 65 64 cells stored
182ae 20 6f 6e 20 74 68 65 20 73 69 62 6c 69 6e 67 20 on the sibling
182af 70 61 67 65 73 20 6d 61 79 20 6e 65 65 64 20 74 pages may need t
182b0 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20 20 o be updated..
182b1 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 34 29 **. ** 4)
182b2 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 20 If the sibling
182b3 70 61 67 65 73 20 61 72 65 20 6e 6f 74 20 69 6e pages are not in
182b4 74 65 72 6e 61 6c 20 69 6e 74 6b 65 79 20 6e 6f ternal intkey no
182b5 64 65 73 2c 20 74 68 65 6e 20 61 6e 79 0a 20 20 des, then any.
182b6 20 20 2a 2a 20 20 20 20 20 20 6f 76 65 72 66 6c ** overfl
182b7 6f 77 20 70 61 67 65 73 20 75 73 65 64 20 62 79 ow pages used by
182b8 20 74 68 65 73 65 20 63 65 6c 6c 73 20 6d 61 79 these cells may
182b9 20 6e 65 65 64 20 74 6f 20 62 65 20 75 70 64 61 need to be upda
182ba 74 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 ted. **
182bb 28 69 6e 74 65 72 6e 61 6c 20 69 6e 74 6b 65 79 (internal intkey
182bc 20 6e 6f 64 65 73 20 6e 65 76 65 72 20 63 6f 6e nodes never con
182bd 74 61 69 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f tain pointers to
182be 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 29 overflow pages)
182bf 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
182c0 20 20 35 29 20 49 66 20 74 68 65 20 73 69 62 6c 5) If the sibl
182c1 69 6e 67 20 70 61 67 65 73 20 61 72 65 20 6e 6f ing pages are no
182c2 74 20 6c 65 61 76 65 73 2c 20 74 68 65 6e 20 74 t leaves, then t
182c3 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 he pointer-map.
182c4 20 20 20 2a 2a 20 20 20 20 20 20 65 6e 74 72 69 ** entri
182c5 65 73 20 66 6f 72 20 74 68 65 20 72 69 67 68 74 es for the right
182c6 2d 63 68 69 6c 64 20 70 61 67 65 73 20 6f 66 20 -child pages of
182c7 65 61 63 68 20 73 69 62 6c 69 6e 67 20 6d 61 79 each sibling may
182c8 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 need. **
182c9 20 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e to be updated.
182ca 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 . **. ** C
182cb 61 73 65 73 20 31 20 61 6e 64 20 32 20 61 72 65 ases 1 and 2 are
182cc 20 64 65 61 6c 74 20 77 69 74 68 20 61 62 6f 76 dealt with abov
182cd 65 20 62 79 20 6f 74 68 65 72 20 63 6f 64 65 2e e by other code.
182ce 20 54 68 65 20 6e 65 78 74 0a 20 20 20 20 2a 2a The next. **
182cf 20 62 6c 6f 63 6b 20 64 65 61 6c 73 20 77 69 74 block deals wit
182d0 68 20 63 61 73 65 73 20 33 20 61 6e 64 20 34 20 h cases 3 and 4
182d1 61 6e 64 20 74 68 65 20 6f 6e 65 20 61 66 74 65 and the one afte
182d2 72 20 74 68 61 74 2c 20 63 61 73 65 20 35 2e 20 r that, case 5.
182d3 53 69 6e 63 65 0a 20 20 20 20 2a 2a 20 73 65 74 Since. ** set
182d4 74 69 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 6d ting a pointer m
182d5 61 70 20 65 6e 74 72 79 20 69 73 20 61 20 72 65 ap entry is a re
182d6 6c 61 74 69 76 65 6c 79 20 65 78 70 65 6e 73 69 latively expensi
182d7 76 65 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 ve operation, th
182d8 69 73 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20 6f is. ** code o
182d9 6e 6c 79 20 73 65 74 73 20 70 6f 69 6e 74 65 72 nly sets pointer
182da 20 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 map entries for
182db 20 63 68 69 6c 64 20 6f 72 20 6f 76 65 72 66 6c child or overfl
182dc 6f 77 20 70 61 67 65 73 20 74 68 61 74 20 68 61 ow pages that ha
182dd 76 65 0a 20 20 20 20 2a 2a 20 61 63 74 75 61 6c ve. ** actual
182de 6c 79 20 6d 6f 76 65 64 20 62 65 74 77 65 65 6e ly moved between
182df 20 70 61 67 65 73 2e 20 20 2a 2f 0a 20 20 20 20 pages. */.
182e0 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 20 3d 20 MemPage *pNew =
182e1 61 70 4e 65 77 5b 30 5d 3b 0a 20 20 20 20 4d 65 apNew[0];. Me
182e2 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20 61 70 mPage *pOld = ap
182e3 43 6f 70 79 5b 30 5d 3b 0a 20 20 20 20 69 6e 74 Copy[0];. int
182e4 20 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 70 4f 6c nOverflow = pOl
182e5 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 d->nOverflow;.
182e6 20 20 69 6e 74 20 69 4e 65 78 74 4f 6c 64 20 3d int iNextOld =
182e7 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 20 2b 20 6e pOld->nCell + n
182e8 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 69 6e Overflow;. in
182e9 74 20 69 4f 76 65 72 66 6c 6f 77 20 3d 20 28 6e t iOverflow = (n
182ea 4f 76 65 72 66 6c 6f 77 20 3f 20 70 4f 6c 64 2d Overflow ? pOld-
182eb 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 20 3a 20 >aOvfl[0].idx :
182ec 2d 31 29 3b 0a 20 20 20 20 6a 20 3d 20 30 3b 20 -1);. j = 0;
182ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
182ee 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
182ef 75 72 72 65 6e 74 20 27 6f 6c 64 27 20 73 69 62 urrent 'old' sib
182f0 6c 69 6e 67 20 70 61 67 65 20 2a 2f 0a 20 20 20 ling page */.
182f1 20 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 k = 0;
182f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
182f3 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 27 /* Current '
182f4 6e 65 77 27 20 73 69 62 6c 69 6e 67 20 70 61 67 new' sibling pag
182f5 65 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 e */. for(i=0
182f6 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b ; i<nCell; i++){
182f7 0a 20 20 20 20 20 20 69 6e 74 20 69 73 44 69 76 . int isDiv
182f8 69 64 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 ider = 0;.
182f9 77 68 69 6c 65 28 20 69 3d 3d 69 4e 65 78 74 4f while( i==iNextO
182fa 6c 64 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a ld ){. /*
182fb 20 43 65 6c 6c 20 69 20 69 73 20 74 68 65 20 63 Cell i is the c
182fc 65 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 ell immediately
182fd 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c 61 following the la
182fe 73 74 20 63 65 6c 6c 20 6f 6e 20 6f 6c 64 0a 20 st cell on old.
182ff 20 20 20 20 20 20 20 2a 2a 20 73 69 62 6c 69 6e ** siblin
18300 67 20 70 61 67 65 20 6a 2e 20 49 66 20 74 68 65 g page j. If the
18301 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6e 6f siblings are no
18302 74 20 6c 65 61 66 20 70 61 67 65 73 20 6f 66 20 t leaf pages of
18303 61 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e an. ** in
18304 74 6b 65 79 20 62 2d 74 72 65 65 2c 20 74 68 65 tkey b-tree, the
18305 6e 20 63 65 6c 6c 20 69 20 77 61 73 20 61 20 64 n cell i was a d
18306 69 76 69 64 65 72 20 63 65 6c 6c 2e 20 2a 2f 0a ivider cell. */.
18307 20 20 20 20 20 20 20 20 70 4f 6c 64 20 3d 20 61 pOld = a
18308 70 43 6f 70 79 5b 2b 2b 6a 5d 3b 0a 20 20 20 20 pCopy[++j];.
18309 20 20 20 20 69 4e 65 78 74 4f 6c 64 20 3d 20 69 iNextOld = i
1830a 20 2b 20 21 6c 65 61 66 44 61 74 61 20 2b 20 70 + !leafData + p
1830b 4f 6c 64 2d 3e 6e 43 65 6c 6c 20 2b 20 70 4f 6c Old->nCell + pOl
1830c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 d->nOverflow;.
1830d 20 20 20 20 20 20 69 66 28 20 70 4f 6c 64 2d 3e if( pOld->
1830e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 nOverflow ){.
1830f 20 20 20 20 20 20 20 6e 4f 76 65 72 66 6c 6f 77 nOverflow
18310 20 3d 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c = pOld->nOverfl
18311 6f 77 3b 0a 20 20 20 20 20 20 20 20 20 20 69 4f ow;. iO
18312 76 65 72 66 6c 6f 77 20 3d 20 69 20 2b 20 21 6c verflow = i + !l
18313 65 61 66 44 61 74 61 20 2b 20 70 4f 6c 64 2d 3e eafData + pOld->
18314 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3b 0a 20 20 aOvfl[0].idx;.
18315 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
18316 69 73 44 69 76 69 64 65 72 20 3d 20 21 6c 65 61 isDivider = !lea
18317 66 44 61 74 61 3b 20 20 0a 20 20 20 20 20 20 7d fData; . }
18318 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e .. assert(n
18319 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 69 4f Overflow>0 || iO
1831a 76 65 72 66 6c 6f 77 3c 69 20 29 3b 0a 20 20 20 verflow<i );.
1831b 20 20 20 61 73 73 65 72 74 28 6e 4f 76 65 72 66 assert(nOverf
1831c 6c 6f 77 3c 32 20 7c 7c 20 70 4f 6c 64 2d 3e 61 low<2 || pOld->a
1831d 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70 4f 6c Ovfl[0].idx==pOl
1831e 64 2d 3e 61 4f 76 66 6c 5b 31 5d 2e 69 64 78 2d d->aOvfl[1].idx-
1831f 31 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 1);. assert
18320 28 6e 4f 76 65 72 66 6c 6f 77 3c 33 20 7c 7c 20 (nOverflow<3 ||
18321 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 31 5d 2e 69 pOld->aOvfl[1].i
18322 64 78 3d 3d 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b dx==pOld->aOvfl[
18323 32 5d 2e 69 64 78 2d 31 29 3b 0a 20 20 20 20 20 2].idx-1);.
18324 20 69 66 28 20 69 3d 3d 69 4f 76 65 72 66 6c 6f if( i==iOverflo
18325 77 20 29 7b 0a 20 20 20 20 20 20 20 20 69 73 44 w ){. isD
18326 69 76 69 64 65 72 20 3d 20 31 3b 0a 20 20 20 20 ivider = 1;.
18327 20 20 20 20 69 66 28 20 28 2d 2d 6e 4f 76 65 72 if( (--nOver
18328 66 6c 6f 77 29 3e 30 20 29 7b 0a 20 20 20 20 20 flow)>0 ){.
18329 20 20 20 20 20 69 4f 76 65 72 66 6c 6f 77 2b 2b iOverflow++
1832a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1832b 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 69 }.. if( i
1832c 3d 3d 63 6e 74 4e 65 77 5b 6b 5d 20 29 7b 0a 20 ==cntNew[k] ){.
1832d 20 20 20 20 20 20 20 2f 2a 20 43 65 6c 6c 20 69 /* Cell i
1832e 20 69 73 20 74 68 65 20 63 65 6c 6c 20 69 6d 6d is the cell imm
1832f 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 ediately followi
18330 6e 67 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c ng the last cell
18331 20 6f 6e 20 6e 65 77 0a 20 20 20 20 20 20 20 20 on new.
18332 2a 2a 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20 ** sibling page
18333 6b 2e 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e k. If the siblin
18334 67 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 66 20 gs are not leaf
18335 70 61 67 65 73 20 6f 66 20 61 6e 0a 20 20 20 20 pages of an.
18336 20 20 20 20 2a 2a 20 69 6e 74 6b 65 79 20 62 2d ** intkey b-
18337 74 72 65 65 2c 20 74 68 65 6e 20 63 65 6c 6c 20 tree, then cell
18338 69 20 69 73 20 61 20 64 69 76 69 64 65 72 20 63 i is a divider c
18339 65 6c 6c 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 ell. */.
1833a 20 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 2b 2b pNew = apNew[++
1833b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 k];. if(
1833c 21 6c 65 61 66 44 61 74 61 20 29 20 63 6f 6e 74 !leafData ) cont
1833d 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 inue;. }.
1833e 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 6e 4f assert( j<nO
1833f 6c 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 ld );. asse
18340 72 74 28 20 6b 3c 6e 4e 65 77 20 29 3b 0a 0a 20 rt( k<nNew );..
18341 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 /* If the c
18342 65 6c 6c 20 77 61 73 20 6f 72 69 67 69 6e 61 6c ell was original
18343 6c 79 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 ly divider cell
18344 28 61 6e 64 20 69 73 20 6e 6f 74 20 6e 6f 77 29 (and is not now)
18345 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 or. ** an
18346 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 2c 20 6f overflow cell, o
18347 72 20 69 66 20 74 68 65 20 63 65 6c 6c 20 77 61 r if the cell wa
18348 73 20 6c 6f 63 61 74 65 64 20 6f 6e 20 61 20 64 s located on a d
18349 69 66 66 65 72 65 6e 74 20 73 69 62 6c 69 6e 67 ifferent sibling
1834a 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 62 . ** page b
1834b 65 66 6f 72 65 20 74 68 65 20 62 61 6c 61 6e 63 efore the balanc
1834c 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20 70 6f ing, then the po
1834d 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65 inter map entrie
1834e 73 20 61 73 73 6f 63 69 61 74 65 64 0a 20 20 20 s associated.
1834f 20 20 20 2a 2a 20 77 69 74 68 20 61 6e 79 20 63 ** with any c
18350 68 69 6c 64 20 6f 72 20 6f 76 65 72 66 6c 6f 77 hild or overflow
18351 20 70 61 67 65 73 20 6e 65 65 64 20 74 6f 20 62 pages need to b
18352 65 20 75 70 64 61 74 65 64 2e 20 20 2a 2f 0a 20 e updated. */.
18353 20 20 20 20 20 69 66 28 20 69 73 44 69 76 69 64 if( isDivid
18354 65 72 20 7c 7c 20 70 4f 6c 64 2d 3e 70 67 6e 6f er || pOld->pgno
18355 21 3d 70 4e 65 77 2d 3e 70 67 6e 6f 20 29 7b 0a !=pNew->pgno ){.
18356 20 20 20 20 20 20 20 20 69 66 28 20 21 6c 65 61 if( !lea
18357 66 43 6f 72 72 65 63 74 69 6f 6e 20 29 7b 0a 20 fCorrection ){.
18358 20 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 50 ptrmapP
18359 75 74 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 ut(pBt, get4byte
1835a 28 61 70 43 65 6c 6c 5b 69 5d 29 2c 20 50 54 52 (apCell[i]), PTR
1835b 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e 65 77 2d MAP_BTREE, pNew-
1835c 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 >pgno, &rc);.
1835d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
1835e 66 28 20 73 7a 43 65 6c 6c 5b 69 5d 3e 70 4e 65 f( szCell[i]>pNe
1835f 77 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29 7b 0a 20 w->minLocal ){.
18360 20 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 50 ptrmapP
18361 75 74 4f 76 66 6c 50 74 72 28 70 4e 65 77 2c 20 utOvflPtr(pNew,
18362 61 70 43 65 6c 6c 5b 69 5d 2c 20 26 72 63 29 3b apCell[i], &rc);
18363 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
18364 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 }. }.. if
18365 28 20 21 6c 65 61 66 43 6f 72 72 65 63 74 69 6f ( !leafCorrectio
18366 6e 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 n ){. for(i
18367 3d 30 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 =0; i<nNew; i++)
18368 7b 0a 20 20 20 20 20 20 20 20 75 33 32 20 6b 65 {. u32 ke
18369 79 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 70 y = get4byte(&ap
1836a 4e 65 77 5b 69 5d 2d 3e 61 44 61 74 61 5b 38 5d New[i]->aData[8]
1836b 29 3b 0a 20 20 20 20 20 20 20 20 70 74 72 6d 61 );. ptrma
1836c 70 50 75 74 28 70 42 74 2c 20 6b 65 79 2c 20 50 pPut(pBt, key, P
1836d 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 61 70 4e TRMAP_BTREE, apN
1836e 65 77 5b 69 5d 2d 3e 70 67 6e 6f 2c 20 26 72 63 ew[i]->pgno, &rc
1836f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
18370 0a 0a 23 69 66 20 30 0a 20 20 20 20 2f 2a 20 54 ..#if 0. /* T
18371 68 65 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 he ptrmapCheckPa
18372 67 65 73 28 29 20 63 6f 6e 74 61 69 6e 73 20 61 ges() contains a
18373 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e ssert() statemen
18374 74 73 20 74 68 61 74 20 76 65 72 69 66 79 20 74 ts that verify t
18375 68 61 74 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 70 hat. ** all p
18376 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 73 ointer map pages
18377 20 61 72 65 20 73 65 74 20 63 6f 72 72 65 63 74 are set correct
18378 6c 79 2e 20 54 68 69 73 20 69 73 20 68 65 6c 70 ly. This is help
18379 66 75 6c 20 77 68 69 6c 65 20 0a 20 20 20 20 2a ful while . *
1837a 2a 20 64 65 62 75 67 67 69 6e 67 2e 20 54 68 69 * debugging. Thi
1837b 73 20 69 73 20 75 73 75 61 6c 6c 79 20 64 69 73 s is usually dis
1837c 61 62 6c 65 64 20 62 65 63 61 75 73 65 20 61 20 abled because a
1837d 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 corrupt database
1837e 20 6d 61 79 0a 20 20 20 20 2a 2a 20 63 61 75 73 may. ** caus
1837f 65 20 61 6e 20 61 73 73 65 72 74 28 29 20 73 74 e an assert() st
18380 61 74 65 6d 65 6e 74 20 74 6f 20 66 61 69 6c 2e atement to fail.
18381 20 20 2a 2f 0a 20 20 20 20 70 74 72 6d 61 70 43 */. ptrmapC
18382 68 65 63 6b 50 61 67 65 73 28 61 70 4e 65 77 2c heckPages(apNew,
18383 20 6e 4e 65 77 29 3b 0a 20 20 20 20 70 74 72 6d nNew);. ptrm
18384 61 70 43 68 65 63 6b 50 61 67 65 73 28 26 70 50 apCheckPages(&pP
18385 61 72 65 6e 74 2c 20 31 29 3b 0a 23 65 6e 64 69 arent, 1);.#endi
18386 66 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 f. }.. assert(
18387 20 70 50 61 72 65 6e 74 2d 3e 69 73 49 6e 69 74 pParent->isInit
18388 20 29 3b 0a 20 20 54 52 41 43 45 28 28 22 42 41 );. TRACE(("BA
18389 4c 41 4e 43 45 3a 20 66 69 6e 69 73 68 65 64 3a LANCE: finished:
1838a 20 6f 6c 64 3d 25 64 20 6e 65 77 3d 25 64 20 63 old=%d new=%d c
1838b 65 6c 6c 73 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 ells=%d\n",.
1838c 20 20 20 20 20 20 6e 4f 6c 64 2c 20 6e 4e 65 77 nOld, nNew
1838d 2c 20 6e 43 65 6c 6c 29 29 3b 0a 0a 20 20 2f 2a , nCell));.. /*
1838e 0a 20 20 2a 2a 20 43 6c 65 61 6e 75 70 20 62 65 . ** Cleanup be
1838f 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a fore returning..
18390 20 20 2a 2f 0a 62 61 6c 61 6e 63 65 5f 63 6c 65 */.balance_cle
18391 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 anup:. sqlite3S
18392 63 72 61 74 63 68 46 72 65 65 28 61 70 43 65 6c cratchFree(apCel
18393 6c 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 l);. for(i=0; i
18394 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 <nOld; i++){.
18395 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 70 4f releasePage(apO
18396 6c 64 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 66 6f ld[i]);. }. fo
18397 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b 20 69 r(i=0; i<nNew; i
18398 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 ++){. release
18399 50 61 67 65 28 61 70 4e 65 77 5b 69 5d 29 3b 0a Page(apNew[i]);.
1839a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 }.. return rc
1839b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ;.}.../*.** This
1839c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
1839d 6c 65 64 20 77 68 65 6e 20 74 68 65 20 72 6f 6f led when the roo
1839e 74 20 70 61 67 65 20 6f 66 20 61 20 62 2d 74 72 t page of a b-tr
1839f 65 65 20 73 74 72 75 63 74 75 72 65 20 69 73 0a ee structure is.
183a0 2a 2a 20 6f 76 65 72 66 75 6c 6c 20 28 68 61 73 ** overfull (has
183a1 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65 one or more ove
183a2 72 66 6c 6f 77 20 70 61 67 65 73 29 2e 0a 2a 2a rflow pages)..**
183a3 0a 2a 2a 20 41 20 6e 65 77 20 63 68 69 6c 64 20 .** A new child
183a4 70 61 67 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 page is allocate
183a5 64 20 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e d and the conten
183a6 74 73 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e ts of the curren
183a7 74 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 2c 20 t root.** page,
183a8 69 6e 63 6c 75 64 69 6e 67 20 6f 76 65 72 66 6c including overfl
183a9 6f 77 20 63 65 6c 6c 73 2c 20 61 72 65 20 63 6f ow cells, are co
183aa 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 63 68 pied into the ch
183ab 69 6c 64 2e 20 54 68 65 20 72 6f 6f 74 0a 2a 2a ild. The root.**
183ac 20 70 61 67 65 20 69 73 20 74 68 65 6e 20 6f 76 page is then ov
183ad 65 72 77 72 69 74 74 65 6e 20 74 6f 20 6d 61 6b erwritten to mak
183ae 65 20 69 74 20 61 6e 20 65 6d 70 74 79 20 70 61 e it an empty pa
183af 67 65 20 77 69 74 68 20 74 68 65 20 72 69 67 68 ge with the righ
183b0 74 2d 63 68 69 6c 64 20 0a 2a 2a 20 70 6f 69 6e t-child .** poin
183b1 74 65 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 ter pointing to
183b2 74 68 65 20 6e 65 77 20 70 61 67 65 2e 0a 2a 2a the new page..**
183b3 0a 2a 2a 20 42 65 66 6f 72 65 20 72 65 74 75 72 .** Before retur
183b4 6e 69 6e 67 2c 20 61 6c 6c 20 70 6f 69 6e 74 65 ning, all pointe
183b5 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 63 6f r-map entries co
183b6 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 rresponding to p
183b7 61 67 65 73 20 0a 2a 2a 20 74 68 61 74 20 74 68 ages .** that th
183b8 65 20 6e 65 77 20 63 68 69 6c 64 2d 70 61 67 65 e new child-page
183b9 20 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 20 70 6f now contains po
183ba 69 6e 74 65 72 73 20 74 6f 20 61 72 65 20 75 70 inters to are up
183bb 64 61 74 65 64 2e 20 54 68 65 0a 2a 2a 20 65 6e dated. The.** en
183bc 74 72 79 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e try correspondin
183bd 67 20 74 6f 20 74 68 65 20 6e 65 77 20 72 69 67 g to the new rig
183be 68 74 2d 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 ht-child pointer
183bf 20 6f 66 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 of the root.**
183c0 70 61 67 65 20 69 73 20 61 6c 73 6f 20 75 70 64 page is also upd
183c1 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 ated..**.** If s
183c2 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 70 43 68 uccessful, *ppCh
183c3 69 6c 64 20 69 73 20 73 65 74 20 74 6f 20 63 6f ild is set to co
183c4 6e 74 61 69 6e 20 61 20 72 65 66 65 72 65 6e 63 ntain a referenc
183c5 65 20 74 6f 20 74 68 65 20 63 68 69 6c 64 20 0a e to the child .
183c6 2a 2a 20 70 61 67 65 20 61 6e 64 20 53 51 4c 49 ** page and SQLI
183c7 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
183c8 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 d. In this case
183c9 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 the caller is re
183ca 71 75 69 72 65 64 0a 2a 2a 20 74 6f 20 63 61 6c quired.** to cal
183cb 6c 20 72 65 6c 65 61 73 65 50 61 67 65 28 29 20 l releasePage()
183cc 6f 6e 20 2a 70 70 43 68 69 6c 64 20 65 78 61 63 on *ppChild exac
183cd 74 6c 79 20 6f 6e 63 65 2e 20 49 66 20 61 6e 20 tly once. If an
183ce 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a error occurs,.**
183cf 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 an error code i
183d0 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a s returned and *
183d1 70 70 43 68 69 6c 64 20 69 73 20 73 65 74 20 74 ppChild is set t
183d2 6f 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 o 0..*/.static i
183d3 6e 74 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 nt balance_deepe
183d4 72 28 4d 65 6d 50 61 67 65 20 2a 70 52 6f 6f 74 r(MemPage *pRoot
183d5 2c 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 43 68 , MemPage **ppCh
183d6 69 6c 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 ild){. int rc;
183d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
183d8 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
183d9 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 75 62 70 value from subp
183da 72 6f 63 65 64 75 72 65 73 20 2a 2f 0a 20 20 4d rocedures */. M
183db 65 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 20 3d emPage *pChild =
183dc 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 0; /*
183dd 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 6e 65 Pointer to a ne
183de 77 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a w child page */.
183df 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c 64 Pgno pgnoChild
183e0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
183e1 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 /* Page number
183e2 6f 66 20 74 68 65 20 6e 65 77 20 63 68 69 6c 64 of the new child
183e3 20 70 61 67 65 20 2a 2f 0a 20 20 42 74 53 68 61 page */. BtSha
183e4 72 65 64 20 2a 70 42 74 20 3d 20 70 52 6f 6f 74 red *pBt = pRoot
183e5 2d 3e 70 42 74 3b 20 20 20 20 2f 2a 20 54 68 65 ->pBt; /* The
183e6 20 42 54 72 65 65 20 2a 2f 0a 0a 20 20 61 73 73 BTree */.. ass
183e7 65 72 74 28 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65 ert( pRoot->nOve
183e8 72 66 6c 6f 77 3e 30 20 29 3b 0a 20 20 61 73 73 rflow>0 );. ass
183e9 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
183ea 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
183eb 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b ex) );.. /* Mak
183ec 65 20 70 52 6f 6f 74 2c 20 74 68 65 20 72 6f 6f e pRoot, the roo
183ed 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62 2d t page of the b-
183ee 74 72 65 65 2c 20 77 72 69 74 61 62 6c 65 2e 20 tree, writable.
183ef 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 0a Allocate a new .
183f0 20 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20 77 ** page that w
183f1 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6e ill become the n
183f2 65 77 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f ew right-child o
183f3 66 20 70 50 61 67 65 2e 20 43 6f 70 79 20 74 68 f pPage. Copy th
183f4 65 20 63 6f 6e 74 65 6e 74 73 0a 20 20 2a 2a 20 e contents. **
183f5 6f 66 20 74 68 65 20 6e 6f 64 65 20 73 74 6f 72 of the node stor
183f6 65 64 20 6f 6e 20 70 52 6f 6f 74 20 69 6e 74 6f ed on pRoot into
183f7 20 74 68 65 20 6e 65 77 20 63 68 69 6c 64 20 70 the new child p
183f8 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d age.. */. rc =
183f9 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
183fa 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 te(pRoot->pDbPag
183fb 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 e);. if( rc==SQ
183fc 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
183fd 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 c = allocateBtre
183fe 65 50 61 67 65 28 70 42 74 2c 26 70 43 68 69 6c ePage(pBt,&pChil
183ff 64 2c 26 70 67 6e 6f 43 68 69 6c 64 2c 70 52 6f d,&pgnoChild,pRo
18400 6f 74 2d 3e 70 67 6e 6f 2c 30 29 3b 0a 20 20 20 ot->pgno,0);.
18401 20 63 6f 70 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 copyNodeContent
18402 28 70 52 6f 6f 74 2c 20 70 43 68 69 6c 64 2c 20 (pRoot, pChild,
18403 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20 49 53 &rc);. if( IS
18404 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 AUTOVACUUM ){.
18405 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 ptrmapPut(pB
18406 74 2c 20 70 67 6e 6f 43 68 69 6c 64 2c 20 50 54 t, pgnoChild, PT
18407 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 52 6f 6f RMAP_BTREE, pRoo
18408 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 t->pgno, &rc);.
18409 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72 }. }. if( r
1840a 63 20 29 7b 0a 20 20 20 20 2a 70 70 43 68 69 6c c ){. *ppChil
1840b 64 20 3d 20 30 3b 0a 20 20 20 20 72 65 6c 65 61 d = 0;. relea
1840c 73 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a sePage(pChild);.
1840d 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1840e 20 7d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c }. assert( sql
1840f 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
18410 61 62 6c 65 28 70 43 68 69 6c 64 2d 3e 70 44 62 able(pChild->pDb
18411 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 Page) );. asser
18412 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
18413 73 77 72 69 74 65 61 62 6c 65 28 70 52 6f 6f 74 swriteable(pRoot
18414 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
18415 61 73 73 65 72 74 28 20 70 43 68 69 6c 64 2d 3e assert( pChild->
18416 6e 43 65 6c 6c 3d 3d 70 52 6f 6f 74 2d 3e 6e 43 nCell==pRoot->nC
18417 65 6c 6c 20 29 3b 0a 0a 20 20 54 52 41 43 45 28 ell );.. TRACE(
18418 28 22 42 41 4c 41 4e 43 45 3a 20 63 6f 70 79 20 ("BALANCE: copy
18419 72 6f 6f 74 20 25 64 20 69 6e 74 6f 20 25 64 5c root %d into %d\
1841a 6e 22 2c 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c n", pRoot->pgno,
1841b 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b pChild->pgno));
1841c 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 .. /* Copy the
1841d 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 66 overflow cells f
1841e 72 6f 6d 20 70 52 6f 6f 74 20 74 6f 20 70 43 68 rom pRoot to pCh
1841f 69 6c 64 20 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 ild */. memcpy(
18420 70 43 68 69 6c 64 2d 3e 61 4f 76 66 6c 2c 20 70 pChild->aOvfl, p
18421 52 6f 6f 74 2d 3e 61 4f 76 66 6c 2c 20 70 52 6f Root->aOvfl, pRo
18422 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2a 73 69 ot->nOverflow*si
18423 7a 65 6f 66 28 70 52 6f 6f 74 2d 3e 61 4f 76 66 zeof(pRoot->aOvf
18424 6c 5b 30 5d 29 29 3b 0a 20 20 70 43 68 69 6c 64 l[0]));. pChild
18425 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 70 52 ->nOverflow = pR
18426 6f 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a oot->nOverflow;.
18427 0a 20 20 2f 2a 20 5a 65 72 6f 20 74 68 65 20 63 . /* Zero the c
18428 6f 6e 74 65 6e 74 73 20 6f 66 20 70 52 6f 6f 74 ontents of pRoot
18429 2e 20 54 68 65 6e 20 69 6e 73 74 61 6c 6c 20 70 . Then install p
1842a 43 68 69 6c 64 20 61 73 20 74 68 65 20 72 69 67 Child as the rig
1842b 68 74 2d 63 68 69 6c 64 2e 20 2a 2f 0a 20 20 7a ht-child. */. z
1842c 65 72 6f 50 61 67 65 28 70 52 6f 6f 74 2c 20 70 eroPage(pRoot, p
1842d 43 68 69 6c 64 2d 3e 61 44 61 74 61 5b 30 5d 20 Child->aData[0]
1842e 26 20 7e 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 & ~PTF_LEAF);.
1842f 70 75 74 34 62 79 74 65 28 26 70 52 6f 6f 74 2d put4byte(&pRoot-
18430 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d 3e 68 64 >aData[pRoot->hd
18431 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f rOffset+8], pgno
18432 43 68 69 6c 64 29 3b 0a 0a 20 20 2a 70 70 43 68 Child);.. *ppCh
18433 69 6c 64 20 3d 20 70 43 68 69 6c 64 3b 0a 20 20 ild = pChild;.
18434 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
18435 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 ;.}../*.** The p
18436 61 67 65 20 74 68 61 74 20 70 43 75 72 20 63 75 age that pCur cu
18437 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 rrently points t
18438 6f 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 o has just been
18439 6d 6f 64 69 66 69 65 64 20 69 6e 0a 2a 2a 20 73 modified in.** s
1843a 6f 6d 65 20 77 61 79 2e 20 54 68 69 73 20 66 75 ome way. This fu
1843b 6e 63 74 69 6f 6e 20 66 69 67 75 72 65 73 20 6f nction figures o
1843c 75 74 20 69 66 20 74 68 69 73 20 6d 6f 64 69 66 ut if this modif
1843d 69 63 61 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68 ication means th
1843e 65 0a 2a 2a 20 74 72 65 65 20 6e 65 65 64 73 20 e.** tree needs
1843f 74 6f 20 62 65 20 62 61 6c 61 6e 63 65 64 2c 20 to be balanced,
18440 61 6e 64 20 69 66 20 73 6f 20 63 61 6c 6c 73 20 and if so calls
18441 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 the appropriate
18442 62 61 6c 61 6e 63 69 6e 67 20 0a 2a 2a 20 72 6f balancing .** ro
18443 75 74 69 6e 65 2e 20 42 61 6c 61 6e 63 69 6e 67 utine. Balancing
18444 20 72 6f 75 74 69 6e 65 73 20 61 72 65 3a 0a 2a routines are:.*
18445 2a 0a 2a 2a 20 20 20 62 61 6c 61 6e 63 65 5f 71 *.** balance_q
18446 75 69 63 6b 28 29 0a 2a 2a 20 20 20 62 61 6c 61 uick().** bala
18447 6e 63 65 5f 64 65 65 70 65 72 28 29 0a 2a 2a 20 nce_deeper().**
18448 20 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f balance_nonroo
18449 74 28 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e t().*/.static in
1844a 74 20 62 61 6c 61 6e 63 65 28 42 74 43 75 72 73 t balance(BtCurs
1844b 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 or *pCur){. int
1844c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1844d 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 4d 69 . const int nMi
1844e 6e 20 3d 20 70 43 75 72 2d 3e 70 42 74 2d 3e 75 n = pCur->pBt->u
1844f 73 61 62 6c 65 53 69 7a 65 20 2a 20 32 20 2f 20 sableSize * 2 /
18450 33 3b 0a 20 20 75 38 20 61 42 61 6c 61 6e 63 65 3;. u8 aBalance
18451 51 75 69 63 6b 53 70 61 63 65 5b 31 33 5d 3b 0a QuickSpace[13];.
18452 20 20 75 38 20 2a 70 46 72 65 65 20 3d 20 30 3b u8 *pFree = 0;
18453 0a 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 69 6e .. TESTONLY( in
18454 74 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 5f t balance_quick_
18455 63 61 6c 6c 65 64 20 3d 20 30 20 29 3b 0a 20 20 called = 0 );.
18456 54 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20 62 61 TESTONLY( int ba
18457 6c 61 6e 63 65 5f 64 65 65 70 65 72 5f 63 61 6c lance_deeper_cal
18458 6c 65 64 20 3d 20 30 20 29 3b 0a 0a 20 20 64 6f led = 0 );.. do
18459 20 7b 0a 20 20 20 20 69 6e 74 20 69 50 61 67 65 {. int iPage
1845a 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a = pCur->iPage;.
1845b 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 MemPage *pPa
1845c 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
1845d 65 5b 69 50 61 67 65 5d 3b 0a 0a 20 20 20 20 69 e[iPage];.. i
1845e 66 28 20 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 f( iPage==0 ){.
1845f 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e if( pPage->
18460 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 nOverflow ){.
18461 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 /* The root
18462 20 70 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74 page of the b-t
18463 72 65 65 20 69 73 20 6f 76 65 72 66 75 6c 6c 2e ree is overfull.
18464 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 63 61 In this case ca
18465 6c 6c 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a ll the. *
18466 2a 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 * balance_deeper
18467 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 () function to c
18468 72 65 61 74 65 20 61 20 6e 65 77 20 63 68 69 6c reate a new chil
18469 64 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 d for the root-p
1846a 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 age. ** a
1846b 6e 64 20 63 6f 70 79 20 74 68 65 20 63 75 72 72 nd copy the curr
1846c 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 ent contents of
1846d 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 74 6f the root-page to
1846e 20 69 74 2e 20 54 68 65 0a 20 20 20 20 20 20 20 it. The.
1846f 20 2a 2a 20 6e 65 78 74 20 69 74 65 72 61 74 69 ** next iterati
18470 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f on of the do-loo
18471 70 20 77 69 6c 6c 20 62 61 6c 61 6e 63 65 20 74 p will balance t
18472 68 65 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 20 he child page..
18473 20 20 20 20 20 20 20 2a 2f 20 0a 20 20 20 20 20 */ .
18474 20 20 20 61 73 73 65 72 74 28 20 28 62 61 6c 61 assert( (bala
18475 6e 63 65 5f 64 65 65 70 65 72 5f 63 61 6c 6c 65 nce_deeper_calle
18476 64 2b 2b 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 d++)==0 );.
18477 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f rc = balance_
18478 64 65 65 70 65 72 28 70 50 61 67 65 2c 20 26 70 deeper(pPage, &p
18479 43 75 72 2d 3e 61 70 50 61 67 65 5b 31 5d 29 3b Cur->apPage[1]);
1847a 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d . if( rc=
1847b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1847c 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69 50 pCur->iP
1847d 61 67 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 age = 1;.
1847e 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 30 pCur->aiIdx[0
1847f 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 ] = 0;.
18480 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 31 5d 20 pCur->aiIdx[1]
18481 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 = 0;. a
18482 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 ssert( pCur->apP
18483 61 67 65 5b 31 5d 2d 3e 6e 4f 76 65 72 66 6c 6f age[1]->nOverflo
18484 77 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 w );. }.
18485 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
18486 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
18487 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 }. }else if(
18488 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f pPage->nOverflo
18489 77 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 6e w==0 && pPage->n
1848a 46 72 65 65 3c 3d 6e 4d 69 6e 20 29 7b 0a 20 20 Free<=nMin ){.
1848b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1848c 65 6c 73 65 7b 0a 20 20 20 20 20 20 4d 65 6d 50 else{. MemP
1848d 61 67 65 20 2a 20 63 6f 6e 73 74 20 70 50 61 72 age * const pPar
1848e 65 6e 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 ent = pCur->apPa
1848f 67 65 5b 69 50 61 67 65 2d 31 5d 3b 0a 20 20 20 ge[iPage-1];.
18490 20 20 20 69 6e 74 20 63 6f 6e 73 74 20 69 49 64 int const iId
18491 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b x = pCur->aiIdx[
18492 69 50 61 67 65 2d 31 5d 3b 0a 0a 20 20 20 20 20 iPage-1];..
18493 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
18494 65 72 57 72 69 74 65 28 70 50 61 72 65 6e 74 2d erWrite(pParent-
18495 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
18496 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
18497 4f 4b 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 OK ){.#ifndef SQ
18498 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 LITE_OMIT_QUICKB
18499 41 4c 41 4e 43 45 0a 20 20 20 20 20 20 20 20 69 ALANCE. i
1849a 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 f( pPage->hasDat
1849b 61 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 50 a. && pP
1849c 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d age->nOverflow==
1849d 31 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 50 1. && pP
1849e 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 age->aOvfl[0].id
1849f 78 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 0a x==pPage->nCell.
184a0 20 20 20 20 20 20 20 20 20 26 26 20 70 50 61 72 && pPar
184a1 65 6e 74 2d 3e 70 67 6e 6f 21 3d 31 0a 20 20 20 ent->pgno!=1.
184a2 20 20 20 20 20 20 26 26 20 70 50 61 72 65 6e 74 && pParent
184a3 2d 3e 6e 43 65 6c 6c 3d 3d 69 49 64 78 0a 20 20 ->nCell==iIdx.
184a4 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 ){.
184a5 20 20 20 2f 2a 20 43 61 6c 6c 20 62 61 6c 61 6e /* Call balan
184a6 63 65 5f 71 75 69 63 6b 28 29 20 74 6f 20 63 72 ce_quick() to cr
184a7 65 61 74 65 20 61 20 6e 65 77 20 73 69 62 6c 69 eate a new sibli
184a8 6e 67 20 6f 66 20 70 50 61 67 65 20 6f 6e 20 77 ng of pPage on w
184a9 68 69 63 68 0a 20 20 20 20 20 20 20 20 20 20 2a hich. *
184aa 2a 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6f * to store the o
184ab 76 65 72 66 6c 6f 77 20 63 65 6c 6c 2e 20 62 61 verflow cell. ba
184ac 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 20 69 6e lance_quick() in
184ad 73 65 72 74 73 20 61 20 6e 65 77 20 63 65 6c 6c serts a new cell
184ae 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e . ** in
184af 74 6f 20 70 50 61 72 65 6e 74 2c 20 77 68 69 63 to pParent, whic
184b0 68 20 6d 61 79 20 63 61 75 73 65 20 70 50 61 72 h may cause pPar
184b1 65 6e 74 20 6f 76 65 72 66 6c 6f 77 2e 20 49 66 ent overflow. If
184b2 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 20 20 this.
184b3 2a 2a 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20 ** happens, the
184b4 6e 65 78 74 20 69 6e 74 65 72 61 74 69 6f 6e 20 next interation
184b5 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 20 77 of the do-loop w
184b6 69 6c 6c 20 62 61 6c 61 6e 63 65 20 70 50 61 72 ill balance pPar
184b7 65 6e 74 20 0a 20 20 20 20 20 20 20 20 20 20 2a ent . *
184b8 2a 20 75 73 65 20 65 69 74 68 65 72 20 62 61 6c * use either bal
184b9 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 6f ance_nonroot() o
184ba 72 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 r balance_deeper
184bb 28 29 2e 20 55 6e 74 69 6c 20 74 68 69 73 0a 20 (). Until this.
184bc 20 20 20 20 20 20 20 20 20 2a 2a 20 68 61 70 70 ** happ
184bd 65 6e 73 2c 20 74 68 65 20 6f 76 65 72 66 6c 6f ens, the overflo
184be 77 20 63 65 6c 6c 20 69 73 20 73 74 6f 72 65 64 w cell is stored
184bf 20 69 6e 20 74 68 65 20 61 42 61 6c 61 6e 63 65 in the aBalance
184c0 51 75 69 63 6b 53 70 61 63 65 5b 5d 0a 20 20 20 QuickSpace[].
184c1 20 20 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72 ** buffer
184c2 2e 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 0a . . **.
184c3 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 68 65 ** The
184c4 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20 purpose of the
184c5 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 following assert
184c6 28 29 20 69 73 20 74 6f 20 63 68 65 63 6b 20 74 () is to check t
184c7 68 61 74 20 6f 6e 6c 79 20 61 0a 20 20 20 20 20 hat only a.
184c8 20 20 20 20 20 2a 2a 20 73 69 6e 67 6c 65 20 63 ** single c
184c9 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65 5f 71 all to balance_q
184ca 75 69 63 6b 28 29 20 69 73 20 6d 61 64 65 20 66 uick() is made f
184cb 6f 72 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 or each call to
184cc 74 68 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a this. *
184cd 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 74 * function. If t
184ce 68 69 73 20 77 65 72 65 20 6e 6f 74 20 76 65 72 his were not ver
184cf 69 66 69 65 64 2c 20 61 20 73 75 62 74 6c 65 20 ified, a subtle
184d0 62 75 67 20 69 6e 76 6f 6c 76 69 6e 67 20 72 65 bug involving re
184d1 75 73 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a use. **
184d2 20 6f 66 20 74 68 65 20 61 42 61 6c 61 6e 63 65 of the aBalance
184d3 51 75 69 63 6b 53 70 61 63 65 5b 5d 20 6d 69 67 QuickSpace[] mig
184d4 68 74 20 73 6e 65 61 6b 20 69 6e 2e 0a 20 20 20 ht sneak in..
184d5 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
184d6 20 20 20 20 61 73 73 65 72 74 28 20 28 62 61 6c assert( (bal
184d7 61 6e 63 65 5f 71 75 69 63 6b 5f 63 61 6c 6c 65 ance_quick_calle
184d8 64 2b 2b 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 d++)==0 );.
184d9 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 rc = balanc
184da 65 5f 71 75 69 63 6b 28 70 50 61 72 65 6e 74 2c e_quick(pParent,
184db 20 70 50 61 67 65 2c 20 61 42 61 6c 61 6e 63 65 pPage, aBalance
184dc 51 75 69 63 6b 53 70 61 63 65 29 3b 0a 20 20 20 QuickSpace);.
184dd 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 }else.#endi
184de 66 0a 20 20 20 20 20 20 20 20 7b 0a 20 20 20 20 f. {.
184df 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 /* In this
184e0 20 63 61 73 65 2c 20 63 61 6c 6c 20 62 61 6c 61 case, call bala
184e1 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f nce_nonroot() to
184e2 20 72 65 64 69 73 74 72 69 62 75 74 65 20 63 65 redistribute ce
184e3 6c 6c 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a lls. **
184e4 20 62 65 74 77 65 65 6e 20 70 50 61 67 65 20 61 between pPage a
184e5 6e 64 20 75 70 20 74 6f 20 32 20 6f 66 20 69 74 nd up to 2 of it
184e6 73 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 2e s sibling pages.
184e7 20 54 68 69 73 20 69 6e 76 6f 6c 76 65 73 0a 20 This involves.
184e8 20 20 20 20 20 20 20 20 20 2a 2a 20 6d 6f 64 69 ** modi
184e9 66 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e fying the conten
184ea 74 73 20 6f 66 20 70 50 61 72 65 6e 74 2c 20 77 ts of pParent, w
184eb 68 69 63 68 20 6d 61 79 20 63 61 75 73 65 20 70 hich may cause p
184ec 50 61 72 65 6e 74 20 74 6f 0a 20 20 20 20 20 20 Parent to.
184ed 20 20 20 20 2a 2a 20 62 65 63 6f 6d 65 20 6f 76 ** become ov
184ee 65 72 66 75 6c 6c 20 6f 72 20 75 6e 64 65 72 66 erfull or underf
184ef 75 6c 6c 2e 20 54 68 65 20 6e 65 78 74 20 69 74 ull. The next it
184f0 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 eration of the d
184f1 6f 2d 6c 6f 6f 70 0a 20 20 20 20 20 20 20 20 20 o-loop.
184f2 20 2a 2a 20 77 69 6c 6c 20 62 61 6c 61 6e 63 65 ** will balance
184f3 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
184f4 20 74 6f 20 63 6f 72 72 65 63 74 20 74 68 69 73 to correct this
184f5 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 0a .. ** .
184f6 20 20 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20 ** If
184f7 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 the parent page
184f8 62 65 63 6f 6d 65 73 20 6f 76 65 72 66 75 6c 6c becomes overfull
184f9 2c 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 , the overflow c
184fa 65 6c 6c 20 6f 72 20 63 65 6c 6c 73 0a 20 20 20 ell or cells.
184fb 20 20 20 20 20 20 20 2a 2a 20 61 72 65 20 73 74 ** are st
184fc 6f 72 65 64 20 69 6e 20 74 68 65 20 70 53 70 61 ored in the pSpa
184fd 63 65 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 ce buffer alloca
184fe 74 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 ted immediately
184ff 62 65 6c 6f 77 2e 20 0a 20 20 20 20 20 20 20 20 below. .
18500 20 20 2a 2a 20 41 20 73 75 62 73 65 71 75 65 6e ** A subsequen
18501 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 t iteration of t
18502 68 65 20 64 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 he do-loop will
18503 64 65 61 6c 20 77 69 74 68 20 74 68 69 73 20 62 deal with this b
18504 79 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 y. ** c
18505 61 6c 6c 69 6e 67 20 62 61 6c 61 6e 63 65 5f 6e alling balance_n
18506 6f 6e 72 6f 6f 74 28 29 20 28 62 61 6c 61 6e 63 onroot() (balanc
18507 65 5f 64 65 65 70 65 72 28 29 20 6d 61 79 20 62 e_deeper() may b
18508 65 20 63 61 6c 6c 65 64 20 66 69 72 73 74 2c 0a e called first,.
18509 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 75 74 ** but
1850a 20 69 74 20 64 6f 65 73 6e 27 74 20 64 65 61 6c it doesn't deal
1850b 20 77 69 74 68 20 6f 76 65 72 66 6c 6f 77 20 63 with overflow c
1850c 65 6c 6c 73 20 2d 20 6a 75 73 74 20 6d 6f 76 65 ells - just move
1850d 73 20 74 68 65 6d 20 74 6f 20 61 0a 20 20 20 20 s them to a.
1850e 20 20 20 20 20 20 2a 2a 20 64 69 66 66 65 72 65 ** differe
1850f 6e 74 20 70 61 67 65 29 2e 20 4f 6e 63 65 20 74 nt page). Once t
18510 68 69 73 20 73 75 62 73 65 71 75 65 6e 74 20 63 his subsequent c
18511 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65 5f 6e all to balance_n
18512 6f 6e 72 6f 6f 74 28 29 20 0a 20 20 20 20 20 20 onroot() .
18513 20 20 20 20 2a 2a 20 68 61 73 20 63 6f 6d 70 6c ** has compl
18514 65 74 65 64 2c 20 69 74 20 69 73 20 73 61 66 65 eted, it is safe
18515 20 74 6f 20 72 65 6c 65 61 73 65 20 74 68 65 20 to release the
18516 70 53 70 61 63 65 20 62 75 66 66 65 72 20 75 73 pSpace buffer us
18517 65 64 20 62 79 0a 20 20 20 20 20 20 20 20 20 20 ed by.
18518 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 ** the previous
18519 63 61 6c 6c 2c 20 61 73 20 74 68 65 20 6f 76 65 call, as the ove
1851a 72 66 6c 6f 77 20 63 65 6c 6c 20 64 61 74 61 20 rflow cell data
1851b 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 0a will have been .
1851c 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 70 ** cop
1851d 69 65 64 20 65 69 74 68 65 72 20 69 6e 74 6f 20 ied either into
1851e 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 64 61 the body of a da
1851f 74 61 62 61 73 65 20 70 61 67 65 20 6f 72 20 69 tabase page or i
18520 6e 74 6f 20 74 68 65 20 6e 65 77 0a 20 20 20 20 nto the new.
18521 20 20 20 20 20 20 2a 2a 20 70 53 70 61 63 65 20 ** pSpace
18522 62 75 66 66 65 72 20 70 61 73 73 65 64 20 74 6f buffer passed to
18523 20 74 68 65 20 6c 61 74 74 65 72 20 63 61 6c 6c the latter call
18524 20 74 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 to balance_nonr
18525 6f 6f 74 28 29 2e 0a 20 20 20 20 20 20 20 20 20 oot()..
18526 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 75 38 */. u8
18527 20 2a 70 53 70 61 63 65 20 3d 20 73 71 6c 69 74 *pSpace = sqlit
18528 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 70 43 75 e3PageMalloc(pCu
18529 72 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 r->pBt->pageSize
1852a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 );. rc
1852b 3d 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f = balance_nonroo
1852c 74 28 70 50 61 72 65 6e 74 2c 20 69 49 64 78 2c t(pParent, iIdx,
1852d 20 70 53 70 61 63 65 2c 20 69 50 61 67 65 3d 3d pSpace, iPage==
1852e 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 1);. if
1852f 28 20 70 46 72 65 65 20 29 7b 0a 20 20 20 20 20 ( pFree ){.
18530 20 20 20 20 20 20 20 2f 2a 20 49 66 20 70 46 72 /* If pFr
18531 65 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 ee is not NULL,
18532 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 it points to the
18533 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20 75 pSpace buffer u
18534 73 65 64 20 0a 20 20 20 20 20 20 20 20 20 20 20 sed .
18535 20 2a 2a 20 62 79 20 61 20 70 72 65 76 69 6f 75 ** by a previou
18536 73 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 s call to balanc
18537 65 5f 6e 6f 6e 72 6f 6f 74 28 29 2e 20 49 74 73 e_nonroot(). Its
18538 20 63 6f 6e 74 65 6e 74 73 20 61 72 65 0a 20 20 contents are.
18539 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6e 6f 77 ** now
1853a 20 73 74 6f 72 65 64 20 65 69 74 68 65 72 20 6f stored either o
1853b 6e 20 72 65 61 6c 20 64 61 74 61 62 61 73 65 20 n real database
1853c 70 61 67 65 73 20 6f 72 20 77 69 74 68 69 6e 20 pages or within
1853d 74 68 65 20 0a 20 20 20 20 20 20 20 20 20 20 20 the .
1853e 20 2a 2a 20 6e 65 77 20 70 53 70 61 63 65 20 62 ** new pSpace b
1853f 75 66 66 65 72 2c 20 73 6f 20 69 74 20 6d 61 79 uffer, so it may
18540 20 62 65 20 73 61 66 65 6c 79 20 66 72 65 65 64 be safely freed
18541 20 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20 here. */.
18542 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
18543 65 46 72 65 65 28 70 46 72 65 65 29 3b 0a 20 20 eFree(pFree);.
18544 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 }..
18545 20 20 20 20 20 2f 2a 20 54 68 65 20 70 53 70 61 /* The pSpa
18546 63 65 20 62 75 66 66 65 72 20 77 69 6c 6c 20 62 ce buffer will b
18547 65 20 66 72 65 65 64 20 61 66 74 65 72 20 74 68 e freed after th
18548 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 0a 20 e next call to.
18549 20 20 20 20 20 20 20 20 20 2a 2a 20 62 61 6c 61 ** bala
1854a 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 2c 20 6f nce_nonroot(), o
1854b 72 20 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68 r just before th
1854c 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
1854d 72 6e 73 2c 20 77 68 69 63 68 65 76 65 72 0a 20 rns, whichever.
1854e 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d 65 ** come
1854f 73 20 66 69 72 73 74 2e 20 2a 2f 0a 20 20 20 20 s first. */.
18550 20 20 20 20 20 20 70 46 72 65 65 20 3d 20 70 53 pFree = pS
18551 70 61 63 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a pace;. }.
18552 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 70 }.. p
18553 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 Page->nOverflow
18554 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 = 0;.. /* T
18555 68 65 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f he next iteratio
18556 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 n of the do-loop
18557 20 62 61 6c 61 6e 63 65 73 20 74 68 65 20 70 61 balances the pa
18558 72 65 6e 74 20 70 61 67 65 2e 20 2a 2f 0a 20 20 rent page. */.
18559 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
1855a 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 70 43 pPage);. pC
1855b 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a 20 20 20 ur->iPage--;.
1855c 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d }. }while( rc=
1855d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 0a 20 =SQLITE_OK );..
1855e 20 69 66 28 20 70 46 72 65 65 20 29 7b 0a 20 20 if( pFree ){.
1855f 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 sqlite3PageFre
18560 65 28 70 46 72 65 65 29 3b 0a 20 20 7d 0a 20 20 e(pFree);. }.
18561 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f return rc;.}.../
18562 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 *.** Insert a ne
18563 77 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74 68 w record into th
18564 65 20 42 54 72 65 65 2e 20 20 54 68 65 20 6b 65 e BTree. The ke
18565 79 20 69 73 20 67 69 76 65 6e 20 62 79 20 28 70 y is given by (p
18566 4b 65 79 2c 6e 4b 65 79 29 0a 2a 2a 20 61 6e 64 Key,nKey).** and
18567 20 74 68 65 20 64 61 74 61 20 69 73 20 67 69 76 the data is giv
18568 65 6e 20 62 79 20 28 70 44 61 74 61 2c 6e 44 61 en by (pData,nDa
18569 74 61 29 2e 20 20 54 68 65 20 63 75 72 73 6f 72 ta). The cursor
1856a 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 74 6f is used only to
1856b 0a 2a 2a 20 64 65 66 69 6e 65 20 77 68 61 74 20 .** define what
1856c 74 61 62 6c 65 20 74 68 65 20 72 65 63 6f 72 64 table the record
1856d 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 72 should be inser
1856e 74 65 64 20 69 6e 74 6f 2e 20 20 54 68 65 20 63 ted into. The c
1856f 75 72 73 6f 72 0a 2a 2a 20 69 73 20 6c 65 66 74 ursor.** is left
18570 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 72 pointing at a r
18571 61 6e 64 6f 6d 20 6c 6f 63 61 74 69 6f 6e 2e 0a andom location..
18572 2a 2a 0a 2a 2a 20 46 6f 72 20 61 6e 20 49 4e 54 **.** For an INT
18573 4b 45 59 20 74 61 62 6c 65 2c 20 6f 6e 6c 79 20 KEY table, only
18574 74 68 65 20 6e 4b 65 79 20 76 61 6c 75 65 20 6f the nKey value o
18575 66 20 74 68 65 20 6b 65 79 20 69 73 20 75 73 65 f the key is use
18576 64 2e 20 20 70 4b 65 79 20 69 73 0a 2a 2a 20 69 d. pKey is.** i
18577 67 6e 6f 72 65 64 2e 20 20 46 6f 72 20 61 20 5a gnored. For a Z
18578 45 52 4f 44 41 54 41 20 74 61 62 6c 65 2c 20 74 ERODATA table, t
18579 68 65 20 70 44 61 74 61 20 61 6e 64 20 6e 44 61 he pData and nDa
1857a 74 61 20 61 72 65 20 62 6f 74 68 20 69 67 6e 6f ta are both igno
1857b 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 red..**.** If th
1857c 65 20 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72 e seekResult par
1857d 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 ameter is non-ze
1857e 72 6f 2c 20 74 68 65 6e 20 61 20 73 75 63 63 65 ro, then a succe
1857f 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 0a 2a 2a ssful call to.**
18580 20 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 MovetoUnpacked(
18581 29 20 74 6f 20 73 65 65 6b 20 63 75 72 73 6f 72 ) to seek cursor
18582 20 70 43 75 72 20 74 6f 20 28 70 4b 65 79 2c 20 pCur to (pKey,
18583 6e 4b 65 79 29 20 68 61 73 20 61 6c 72 65 61 64 nKey) has alread
18584 79 0a 2a 2a 20 62 65 65 6e 20 70 65 72 66 6f 72 y.** been perfor
18585 6d 65 64 2e 20 73 65 65 6b 52 65 73 75 6c 74 20 med. seekResult
18586 69 73 20 74 68 65 20 73 65 61 72 63 68 20 72 65 is the search re
18587 73 75 6c 74 20 72 65 74 75 72 6e 65 64 20 28 61 sult returned (a
18588 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6e 75 6d negative.** num
18589 62 65 72 20 69 66 20 70 43 75 72 20 70 6f 69 6e ber if pCur poin
1858a 74 73 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 ts at an entry t
1858b 68 61 74 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 hat is smaller t
1858c 68 61 6e 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 han (pKey, nKey)
1858d 2c 20 6f 72 0a 2a 2a 20 61 20 70 6f 73 69 74 69 , or.** a positi
1858e 76 65 20 76 61 6c 75 65 20 69 66 20 70 43 75 72 ve value if pCur
1858f 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 65 74 points at an et
18590 72 79 20 74 68 61 74 20 69 73 20 6c 61 72 67 65 ry that is large
18591 72 20 74 68 61 6e 20 0a 2a 2a 20 28 70 4b 65 79 r than .** (pKey
18592 2c 20 6e 4b 65 79 29 29 2e 20 0a 2a 2a 0a 2a 2a , nKey)). .**.**
18593 20 49 66 20 74 68 65 20 73 65 65 6b 52 65 73 75 If the seekResu
18594 6c 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 lt parameter is
18595 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 non-zero, then t
18596 68 65 20 63 61 6c 6c 65 72 20 67 75 61 72 61 6e he caller guaran
18597 74 65 65 73 20 74 68 61 74 0a 2a 2a 20 63 75 72 tees that.** cur
18598 73 6f 72 20 70 43 75 72 20 69 73 20 70 6f 69 6e sor pCur is poin
18599 74 69 6e 67 20 61 74 20 74 68 65 20 65 78 69 73 ting at the exis
1859a 74 69 6e 67 20 63 6f 70 79 20 6f 66 20 61 20 72 ting copy of a r
1859b 6f 77 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 ow that is to be
1859c 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 2e .** overwritten.
1859d 20 20 49 66 20 74 68 65 20 73 65 65 6b 52 65 73 If the seekRes
1859e 75 6c 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 ult parameter is
1859f 20 30 2c 20 74 68 65 6e 20 63 75 72 73 6f 72 20 0, then cursor
185a0 70 43 75 72 20 6d 61 79 0a 2a 2a 20 70 6f 69 6e pCur may.** poin
185a1 74 20 74 6f 20 61 6e 79 20 65 6e 74 72 79 20 6f t to any entry o
185a2 72 20 74 6f 20 6e 6f 20 65 6e 74 72 79 20 61 74 r to no entry at
185a3 20 61 6c 6c 20 61 6e 64 20 73 6f 20 74 68 69 73 all and so this
185a4 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 74 6f function has to
185a5 20 73 65 65 6b 0a 2a 2a 20 74 68 65 20 63 75 72 seek.** the cur
185a6 73 6f 72 20 62 65 66 6f 72 65 20 74 68 65 20 6e sor before the n
185a7 65 77 20 6b 65 79 20 63 61 6e 20 62 65 20 69 6e ew key can be in
185a8 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 serted..*/.SQLIT
185a9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
185aa 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 lite3BtreeInsert
185ab 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 (. BtCursor *pC
185ac 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ur,
185ad 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 64 61 74 /* Insert dat
185ae 61 20 69 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 a into the table
185af 20 6f 66 20 74 68 69 73 20 63 75 72 73 6f 72 20 of this cursor
185b0 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
185b1 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c *pKey, i64 nKey,
185b2 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 6f /* The key o
185b3 66 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 f the new record
185b4 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
185b5 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 *pData, int nDa
185b6 74 61 2c 20 20 2f 2a 20 54 68 65 20 64 61 74 61 ta, /* The data
185b7 20 6f 66 20 74 68 65 20 6e 65 77 20 72 65 63 6f of the new reco
185b8 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 rd */. int nZer
185b9 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 o,
185ba 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
185bb 20 6f 66 20 65 78 74 72 61 20 30 20 62 79 74 65 of extra 0 byte
185bc 73 20 74 6f 20 61 70 70 65 6e 64 20 74 6f 20 64 s to append to d
185bd 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 61 70 70 ata */. int app
185be 65 6e 64 42 69 61 73 2c 20 20 20 20 20 20 20 20 endBias,
185bf 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
185c0 69 66 20 74 68 69 73 20 69 73 20 6c 69 6b 65 6c if this is likel
185c1 79 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 20 y an append */.
185c2 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 20 int seekResult
185c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
185c4 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 70 72 69 /* Result of pri
185c5 6f 72 20 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 or MovetoUnpacke
185c6 64 28 29 20 63 61 6c 6c 20 2a 2f 0a 29 7b 0a 20 d() call */.){.
185c7 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6c int rc;. int l
185c8 6f 63 20 3d 20 73 65 65 6b 52 65 73 75 6c 74 3b oc = seekResult;
185c9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 2d 31 3a /* -1:
185ca 20 62 65 66 6f 72 65 20 64 65 73 69 72 65 64 20 before desired
185cb 6c 6f 63 61 74 69 6f 6e 20 20 2b 31 3a 20 61 66 location +1: af
185cc 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4e ter */. int szN
185cd 65 77 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 64 ew = 0;. int id
185ce 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 x;. MemPage *pP
185cf 61 67 65 3b 0a 20 20 42 74 72 65 65 20 2a 70 20 age;. Btree *p
185d0 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a = pCur->pBtree;.
185d1 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
185d2 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 75 6e 73 69 = p->pBt;. unsi
185d3 67 6e 65 64 20 63 68 61 72 20 2a 6f 6c 64 43 65 gned char *oldCe
185d4 6c 6c 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 ll;. unsigned c
185d5 68 61 72 20 2a 6e 65 77 43 65 6c 6c 20 3d 20 30 har *newCell = 0
185d6 3b 0a 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 ;.. if( pCur->e
185d7 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 State==CURSOR_FA
185d8 55 4c 54 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ULT ){. asser
185d9 74 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 t( pCur->skipNex
185da 74 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a t!=SQLITE_OK );.
185db 20 20 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d return pCur-
185dc 3e 73 6b 69 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a >skipNext;. }..
185dd 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
185de 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
185df 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
185e0 75 72 2d 3e 77 72 46 6c 61 67 20 26 26 20 70 42 ur->wrFlag && pB
185e1 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
185e2 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26 26 ==TRANS_WRITE &&
185e3 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 !pBt->readOnly
185e4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 61 73 );. assert( has
185e5 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
185e6 4c 6f 63 6b 28 70 2c 20 70 43 75 72 2d 3e 70 67 Lock(p, pCur->pg
185e7 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2d 3e 70 4b noRoot, pCur->pK
185e8 65 79 49 6e 66 6f 21 3d 30 2c 20 32 29 20 29 3b eyInfo!=0, 2) );
185e9 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 .. /* Assert th
185ea 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 at the caller ha
185eb 73 20 62 65 65 6e 20 63 6f 6e 73 69 73 74 65 6e s been consisten
185ec 74 2e 20 49 66 20 74 68 69 73 20 63 75 72 73 6f t. If this curso
185ed 72 20 77 61 73 20 6f 70 65 6e 65 64 0a 20 20 2a r was opened. *
185ee 2a 20 65 78 70 65 63 74 69 6e 67 20 61 6e 20 69 * expecting an i
185ef 6e 64 65 78 20 62 2d 74 72 65 65 2c 20 74 68 65 ndex b-tree, the
185f0 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 73 68 6f n the caller sho
185f1 75 6c 64 20 62 65 20 69 6e 73 65 72 74 69 6e 67 uld be inserting
185f2 20 62 6c 6f 62 0a 20 20 2a 2a 20 6b 65 79 73 20 blob. ** keys
185f3 77 69 74 68 20 6e 6f 20 61 73 73 6f 63 69 61 74 with no associat
185f4 65 64 20 64 61 74 61 2e 20 49 66 20 74 68 65 20 ed data. If the
185f5 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 cursor was opene
185f6 64 20 65 78 70 65 63 74 69 6e 67 20 61 6e 0a 20 d expecting an.
185f7 20 2a 2a 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 ** intkey table
185f8 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 73 68 6f , the caller sho
185f9 75 6c 64 20 62 65 20 69 6e 73 65 72 74 69 6e 67 uld be inserting
185fa 20 69 6e 74 65 67 65 72 20 6b 65 79 73 20 77 69 integer keys wi
185fb 74 68 20 61 0a 20 20 2a 2a 20 62 6c 6f 62 20 6f th a. ** blob o
185fc 66 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 f associated dat
185fd 61 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 a. */. assert(
185fe 20 28 70 4b 65 79 3d 3d 30 29 3d 3d 28 70 43 75 (pKey==0)==(pCu
185ff 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 29 20 r->pKeyInfo==0)
18600 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 );.. /* If this
18601 20 69 73 20 61 6e 20 69 6e 73 65 72 74 20 69 6e is an insert in
18602 74 6f 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 to a table b-tre
18603 65 2c 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e e, invalidate an
18604 79 20 69 6e 63 72 62 6c 6f 62 20 0a 20 20 2a 2a y incrblob . **
18605 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e cursors open on
18606 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20 72 the row being r
18607 65 70 6c 61 63 65 64 20 28 61 73 73 75 6d 69 6e eplaced (assumin
18608 67 20 74 68 69 73 20 69 73 20 61 20 72 65 70 6c g this is a repl
18609 61 63 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 ace. ** operati
1860a 6f 6e 20 2d 20 69 66 20 69 74 20 69 73 20 6e 6f on - if it is no
1860b 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 t, the following
1860c 20 69 73 20 61 20 6e 6f 2d 6f 70 29 2e 20 20 2a is a no-op). *
1860d 2f 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4b /. if( pCur->pK
1860e 65 79 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 eyInfo==0 ){.
1860f 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62 invalidateIncrb
18610 6c 6f 62 43 75 72 73 6f 72 73 28 70 2c 20 6e 4b lobCursors(p, nK
18611 65 79 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f ey, 0);. }.. /
18612 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 * Save the posit
18613 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 ions of any othe
18614 72 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f r cursors open o
18615 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 0a 20 20 n this table..
18616 2a 2a 0a 20 20 2a 2a 20 49 6e 20 73 6f 6d 65 20 **. ** In some
18617 63 61 73 65 73 2c 20 74 68 65 20 63 61 6c 6c 20 cases, the call
18618 74 6f 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 29 to btreeMoveto()
18619 20 62 65 6c 6f 77 20 69 73 20 61 20 6e 6f 2d 6f below is a no-o
1861a 70 2e 20 46 6f 72 0a 20 20 2a 2a 20 65 78 61 6d p. For. ** exam
1861b 70 6c 65 2c 20 77 68 65 6e 20 69 6e 73 65 72 74 ple, when insert
1861c 69 6e 67 20 64 61 74 61 20 69 6e 74 6f 20 61 20 ing data into a
1861d 74 61 62 6c 65 20 77 69 74 68 20 61 75 74 6f 2d table with auto-
1861e 67 65 6e 65 72 61 74 65 64 20 69 6e 74 65 67 65 generated intege
1861f 72 0a 20 20 2a 2a 20 6b 65 79 73 2c 20 74 68 65 r. ** keys, the
18620 20 56 44 42 45 20 6c 61 79 65 72 20 69 6e 76 6f VDBE layer invo
18621 6b 65 73 20 73 71 6c 69 74 65 33 42 74 72 65 65 kes sqlite3Btree
18622 4c 61 73 74 28 29 20 74 6f 20 66 69 67 75 72 65 Last() to figure
18623 20 6f 75 74 20 74 68 65 20 0a 20 20 2a 2a 20 69 out the . ** i
18624 6e 74 65 67 65 72 20 6b 65 79 20 74 6f 20 75 73 nteger key to us
18625 65 2e 20 49 74 20 74 68 65 6e 20 63 61 6c 6c 73 e. It then calls
18626 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 this function t
18627 6f 20 61 63 74 75 61 6c 6c 79 20 69 6e 73 65 72 o actually inser
18628 74 20 74 68 65 20 0a 20 20 2a 2a 20 64 61 74 61 t the . ** data
18629 20 69 6e 74 6f 20 74 68 65 20 69 6e 74 6b 65 79 into the intkey
1862a 20 42 2d 54 72 65 65 2e 20 49 6e 20 74 68 69 73 B-Tree. In this
1862b 20 63 61 73 65 20 62 74 72 65 65 4d 6f 76 65 74 case btreeMovet
1862c 6f 28 29 20 72 65 63 6f 67 6e 69 7a 65 73 0a 20 o() recognizes.
1862d 20 2a 2a 20 74 68 61 74 20 74 68 65 20 63 75 72 ** that the cur
1862e 73 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 77 sor is already w
1862f 68 65 72 65 20 69 74 20 6e 65 65 64 73 20 74 6f here it needs to
18630 20 62 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20 be and returns
18631 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20 64 6f 69 without. ** doi
18632 6e 67 20 61 6e 79 20 77 6f 72 6b 2e 20 54 6f 20 ng any work. To
18633 61 76 6f 69 64 20 74 68 77 61 72 74 69 6e 67 20 avoid thwarting
18634 74 68 65 73 65 20 6f 70 74 69 6d 69 7a 61 74 69 these optimizati
18635 6f 6e 73 2c 20 69 74 20 69 73 20 69 6d 70 6f 72 ons, it is impor
18636 74 61 6e 74 0a 20 20 2a 2a 20 6e 6f 74 20 74 6f tant. ** not to
18637 20 63 6c 65 61 72 20 74 68 65 20 63 75 72 73 6f clear the curso
18638 72 20 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 72 r here.. */. r
18639 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f c = saveAllCurso
1863a 72 73 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 rs(pBt, pCur->pg
1863b 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 3b 0a 20 noRoot, pCur);.
1863c 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
1863d 20 72 63 3b 0a 20 20 69 66 28 20 21 6c 6f 63 20 rc;. if( !loc
1863e 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 ){. rc = btre
1863f 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70 4b eMoveto(pCur, pK
18640 65 79 2c 20 6e 4b 65 79 2c 20 61 70 70 65 6e 64 ey, nKey, append
18641 42 69 61 73 2c 20 26 6c 6f 63 29 3b 0a 20 20 20 Bias, &loc);.
18642 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
18643 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 rc;. }. asser
18644 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
18645 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c =CURSOR_VALID ||
18646 20 28 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d (pCur->eState==
18647 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 26 CURSOR_INVALID &
18648 26 20 6c 6f 63 29 20 29 3b 0a 0a 20 20 70 50 61 & loc) );.. pPa
18649 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
1864a 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a e[pCur->iPage];.
1864b 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
1864c 3e 69 6e 74 4b 65 79 20 7c 7c 20 6e 4b 65 79 3e >intKey || nKey>
1864d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1864e 70 50 61 67 65 2d 3e 6c 65 61 66 20 7c 7c 20 21 pPage->leaf || !
1864f 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b pPage->intKey );
18650 0a 0a 20 20 54 52 41 43 45 28 28 22 49 4e 53 45 .. TRACE(("INSE
18651 52 54 3a 20 74 61 62 6c 65 3d 25 64 20 6e 6b 65 RT: table=%d nke
18652 79 3d 25 6c 6c 64 20 6e 64 61 74 61 3d 25 64 20 y=%lld ndata=%d
18653 70 61 67 65 3d 25 64 20 25 73 5c 6e 22 2c 0a 20 page=%d %s\n",.
18654 20 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 pCur->p
18655 67 6e 6f 52 6f 6f 74 2c 20 6e 4b 65 79 2c 20 6e gnoRoot, nKey, n
18656 44 61 74 61 2c 20 70 50 61 67 65 2d 3e 70 67 6e Data, pPage->pgn
18657 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 6c 6f 63 o,. loc
18658 3d 3d 30 20 3f 20 22 6f 76 65 72 77 72 69 74 65 ==0 ? "overwrite
18659 22 20 3a 20 22 6e 65 77 20 65 6e 74 72 79 22 29 " : "new entry")
1865a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
1865b 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 ge->isInit );.
1865c 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70 61 63 allocateTempSpac
1865d 65 28 70 42 74 29 3b 0a 20 20 6e 65 77 43 65 6c e(pBt);. newCel
1865e 6c 20 3d 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 l = pBt->pTmpSpa
1865f 63 65 3b 0a 20 20 69 66 28 20 6e 65 77 43 65 6c ce;. if( newCel
18660 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 l==0 ) return SQ
18661 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 72 63 LITE_NOMEM;. rc
18662 20 3d 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 70 50 = fillInCell(pP
18663 61 67 65 2c 20 6e 65 77 43 65 6c 6c 2c 20 70 4b age, newCell, pK
18664 65 79 2c 20 6e 4b 65 79 2c 20 70 44 61 74 61 2c ey, nKey, pData,
18665 20 6e 44 61 74 61 2c 20 6e 5a 65 72 6f 2c 20 26 nData, nZero, &
18666 73 7a 4e 65 77 29 3b 0a 20 20 69 66 28 20 72 63 szNew);. if( rc
18667 20 29 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 ) goto end_inse
18668 72 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a rt;. assert( sz
18669 4e 65 77 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 New==cellSizePtr
1866a 28 70 50 61 67 65 2c 20 6e 65 77 43 65 6c 6c 29 (pPage, newCell)
1866b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a );. assert( sz
1866c 4e 65 77 3c 3d 4d 58 5f 43 45 4c 4c 5f 53 49 5a New<=MX_CELL_SIZ
1866d 45 28 70 42 74 29 20 29 3b 0a 20 20 69 64 78 20 E(pBt) );. idx
1866e 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 = pCur->aiIdx[pC
1866f 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 69 66 ur->iPage];. if
18670 28 20 6c 6f 63 3d 3d 30 20 29 7b 0a 20 20 20 20 ( loc==0 ){.
18671 75 31 36 20 73 7a 4f 6c 64 3b 0a 20 20 20 20 61 u16 szOld;. a
18672 73 73 65 72 74 28 20 69 64 78 3c 70 50 61 67 65 ssert( idx<pPage
18673 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72 ->nCell );. r
18674 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
18675 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 Write(pPage->pDb
18676 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 Page);. if( r
18677 63 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 c ){. goto
18678 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 20 20 end_insert;.
18679 7d 0a 20 20 20 20 6f 6c 64 43 65 6c 6c 20 3d 20 }. oldCell =
1867a 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
1867b 69 64 78 29 3b 0a 20 20 20 20 69 66 28 20 21 70 idx);. if( !p
1867c 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
1867d 20 20 20 20 6d 65 6d 63 70 79 28 6e 65 77 43 65 memcpy(newCe
1867e 6c 6c 2c 20 6f 6c 64 43 65 6c 6c 2c 20 34 29 3b ll, oldCell, 4);
1867f 0a 20 20 20 20 7d 0a 20 20 20 20 73 7a 4f 6c 64 . }. szOld
18680 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 = cellSizePtr(p
18681 50 61 67 65 2c 20 6f 6c 64 43 65 6c 6c 29 3b 0a Page, oldCell);.
18682 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 43 65 rc = clearCe
18683 6c 6c 28 70 50 61 67 65 2c 20 6f 6c 64 43 65 6c ll(pPage, oldCel
18684 6c 29 3b 0a 20 20 20 20 64 72 6f 70 43 65 6c 6c l);. dropCell
18685 28 70 50 61 67 65 2c 20 69 64 78 2c 20 73 7a 4f (pPage, idx, szO
18686 6c 64 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 ld, &rc);. if
18687 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f ( rc ) goto end_
18688 69 6e 73 65 72 74 3b 0a 20 20 7d 65 6c 73 65 20 insert;. }else
18689 69 66 28 20 6c 6f 63 3c 30 20 26 26 20 70 50 61 if( loc<0 && pPa
1868a 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29 7b 0a 20 ge->nCell>0 ){.
1868b 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
1868c 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 20 20 69 64 ->leaf );. id
1868d 78 20 3d 20 2b 2b 70 43 75 72 2d 3e 61 69 49 64 x = ++pCur->aiId
1868e 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a x[pCur->iPage];.
1868f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
18690 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 ert( pPage->leaf
18691 20 29 3b 0a 20 20 7d 0a 20 20 69 6e 73 65 72 74 );. }. insert
18692 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c Cell(pPage, idx,
18693 20 6e 65 77 43 65 6c 6c 2c 20 73 7a 4e 65 77 2c newCell, szNew,
18694 20 30 2c 20 30 2c 20 26 72 63 29 3b 0a 20 20 61 0, 0, &rc);. a
18695 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
18696 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 2d 3e 6e E_OK || pPage->n
18697 43 65 6c 6c 3e 30 20 7c 7c 20 70 50 61 67 65 2d Cell>0 || pPage-
18698 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0a >nOverflow>0 );.
18699 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 65 72 72 6f . /* If no erro
1869a 72 20 68 61 73 20 6f 63 63 75 72 65 64 20 61 6e r has occured an
1869b 64 20 70 50 61 67 65 20 68 61 73 20 61 6e 20 6f d pPage has an o
1869c 76 65 72 66 6c 6f 77 20 63 65 6c 6c 2c 20 63 61 verflow cell, ca
1869d 6c 6c 20 62 61 6c 61 6e 63 65 28 29 20 0a 20 20 ll balance() .
1869e 2a 2a 20 74 6f 20 72 65 64 69 73 74 72 69 62 75 ** to redistribu
1869f 74 65 20 74 68 65 20 63 65 6c 6c 73 20 77 69 74 te the cells wit
186a0 68 69 6e 20 74 68 65 20 74 72 65 65 2e 20 53 69 hin the tree. Si
186a1 6e 63 65 20 62 61 6c 61 6e 63 65 28 29 20 6d 61 nce balance() ma
186a2 79 20 6d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 y move. ** the
186a3 63 75 72 73 6f 72 2c 20 7a 65 72 6f 20 74 68 65 cursor, zero the
186a4 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 2e 6e BtCursor.info.n
186a5 53 69 7a 65 20 61 6e 64 20 42 74 43 75 72 73 6f Size and BtCurso
186a6 72 2e 76 61 6c 69 64 4e 4b 65 79 0a 20 20 2a 2a r.validNKey. **
186a7 20 76 61 72 69 61 62 6c 65 73 2e 0a 20 20 2a 2a variables.. **
186a8 0a 20 20 2a 2a 20 50 72 65 76 69 6f 75 73 20 76 . ** Previous v
186a9 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 ersions of SQLit
186aa 65 20 63 61 6c 6c 65 64 20 6d 6f 76 65 54 6f 52 e called moveToR
186ab 6f 6f 74 28 29 20 74 6f 20 6d 6f 76 65 20 74 68 oot() to move th
186ac 65 20 63 75 72 73 6f 72 0a 20 20 2a 2a 20 62 61 e cursor. ** ba
186ad 63 6b 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 70 ck to the root p
186ae 61 67 65 20 61 73 20 62 61 6c 61 6e 63 65 28 29 age as balance()
186af 20 75 73 65 64 20 74 6f 20 69 6e 76 61 6c 69 64 used to invalid
186b0 61 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ate the contents
186b1 0a 20 20 2a 2a 20 6f 66 20 42 74 43 75 72 73 6f . ** of BtCurso
186b2 72 2e 61 70 50 61 67 65 5b 5d 20 61 6e 64 20 42 r.apPage[] and B
186b3 74 43 75 72 73 6f 72 2e 61 69 49 64 78 5b 5d 2e tCursor.aiIdx[].
186b4 20 49 6e 73 74 65 61 64 20 6f 66 20 64 6f 69 6e Instead of doin
186b5 67 20 74 68 61 74 2c 0a 20 20 2a 2a 20 73 65 74 g that,. ** set
186b6 20 74 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 the cursor stat
186b7 65 20 74 6f 20 22 69 6e 76 61 6c 69 64 22 2e 20 e to "invalid".
186b8 54 68 69 73 20 6d 61 6b 65 73 20 63 6f 6d 6d 6f This makes commo
186b9 6e 20 69 6e 73 65 72 74 20 6f 70 65 72 61 74 69 n insert operati
186ba 6f 6e 73 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c ons. ** slightl
186bb 79 20 66 61 73 74 65 72 2e 0a 20 20 2a 2a 0a 20 y faster.. **.
186bc 20 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 73 ** There is a s
186bd 75 62 74 6c 65 20 62 75 74 20 69 6d 70 6f 72 74 ubtle but import
186be 61 6e 74 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ant optimization
186bf 20 68 65 72 65 20 74 6f 6f 2e 20 57 68 65 6e 20 here too. When
186c0 69 6e 73 65 72 74 69 6e 67 0a 20 20 2a 2a 20 6d inserting. ** m
186c1 75 6c 74 69 70 6c 65 20 72 65 63 6f 72 64 73 20 ultiple records
186c2 69 6e 74 6f 20 61 6e 20 69 6e 74 6b 65 79 20 62 into an intkey b
186c3 2d 74 72 65 65 20 75 73 69 6e 67 20 61 20 73 69 -tree using a si
186c4 6e 67 6c 65 20 63 75 72 73 6f 72 20 28 61 73 20 ngle cursor (as
186c5 63 61 6e 0a 20 20 2a 2a 20 68 61 70 70 65 6e 20 can. ** happen
186c6 77 68 69 6c 65 20 70 72 6f 63 65 73 73 69 6e 67 while processing
186c7 20 61 6e 20 22 49 4e 53 45 52 54 20 49 4e 54 4f an "INSERT INTO
186c8 20 2e 2e 2e 20 53 45 4c 45 43 54 22 20 73 74 61 ... SELECT" sta
186c9 74 65 6d 65 6e 74 29 2c 20 69 74 0a 20 20 2a 2a tement), it. **
186ca 20 69 73 20 61 64 76 61 6e 74 61 67 65 6f 75 73 is advantageous
186cb 20 74 6f 20 6c 65 61 76 65 20 74 68 65 20 63 75 to leave the cu
186cc 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f rsor pointing to
186cd 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 the last entry
186ce 69 6e 0a 20 20 2a 2a 20 74 68 65 20 62 2d 74 72 in. ** the b-tr
186cf 65 65 20 69 66 20 70 6f 73 73 69 62 6c 65 2e 20 ee if possible.
186d0 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 If the cursor is
186d1 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 74 left pointing t
186d2 6f 20 74 68 65 20 6c 61 73 74 0a 20 20 2a 2a 20 o the last. **
186d3 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 entry in the tab
186d4 6c 65 2c 20 61 6e 64 20 74 68 65 20 6e 65 78 74 le, and the next
186d5 20 72 6f 77 20 69 6e 73 65 72 74 65 64 20 68 61 row inserted ha
186d6 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6b 65 79 s an integer key
186d7 0a 20 20 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 . ** larger tha
186d8 6e 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 78 n the largest ex
186d9 69 73 74 69 6e 67 20 6b 65 79 2c 20 69 74 20 69 isting key, it i
186da 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e s possible to in
186db 73 65 72 74 20 74 68 65 0a 20 20 2a 2a 20 72 6f sert the. ** ro
186dc 77 20 77 69 74 68 6f 75 74 20 73 65 65 6b 69 6e w without seekin
186dd 67 20 74 68 65 20 63 75 72 73 6f 72 2e 20 54 68 g the cursor. Th
186de 69 73 20 63 61 6e 20 62 65 20 61 20 62 69 67 20 is can be a big
186df 70 65 72 66 6f 72 6d 61 6e 63 65 20 62 6f 6f 73 performance boos
186e0 74 2e 0a 20 20 2a 2f 0a 20 20 70 43 75 72 2d 3e t.. */. pCur->
186e1 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a info.nSize = 0;.
186e2 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 pCur->validNKe
186e3 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 72 63 3d y = 0;. if( rc=
186e4 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 =SQLITE_OK && pP
186e5 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 age->nOverflow )
186e6 7b 0a 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e {. rc = balan
186e7 63 65 28 70 43 75 72 29 3b 0a 0a 20 20 20 20 2f ce(pCur);.. /
186e8 2a 20 4d 75 73 74 20 6d 61 6b 65 20 73 75 72 65 * Must make sure
186e9 20 6e 4f 76 65 72 66 6c 6f 77 20 69 73 20 72 65 nOverflow is re
186ea 73 65 74 20 74 6f 20 7a 65 72 6f 20 65 76 65 6e set to zero even
186eb 20 69 66 20 74 68 65 20 62 61 6c 61 6e 63 65 28 if the balance(
186ec 29 0a 20 20 20 20 2a 2a 20 66 61 69 6c 73 2e 20 ). ** fails.
186ed 49 6e 74 65 72 6e 61 6c 20 64 61 74 61 20 73 74 Internal data st
186ee 72 75 63 74 75 72 65 20 63 6f 72 72 75 70 74 69 ructure corrupti
186ef 6f 6e 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 6f on will result o
186f0 74 68 65 72 77 69 73 65 2e 20 0a 20 20 20 20 2a therwise. . *
186f1 2a 20 41 6c 73 6f 2c 20 73 65 74 20 74 68 65 20 * Also, set the
186f2 63 75 72 73 6f 72 20 73 74 61 74 65 20 74 6f 20 cursor state to
186f3 69 6e 76 61 6c 69 64 2e 20 54 68 69 73 20 73 74 invalid. This st
186f4 6f 70 73 20 73 61 76 65 43 75 72 73 6f 72 50 6f ops saveCursorPo
186f5 73 69 74 69 6f 6e 28 29 0a 20 20 20 20 2a 2a 20 sition(). **
186f6 66 72 6f 6d 20 74 72 79 69 6e 67 20 74 6f 20 73 from trying to s
186f7 61 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ave the current
186f8 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 position of the
186f9 63 75 72 73 6f 72 2e 20 20 2a 2f 0a 20 20 20 20 cursor. */.
186fa 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
186fb 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76 65 72 r->iPage]->nOver
186fc 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 70 43 flow = 0;. pC
186fd 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 ur->eState = CUR
186fe 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 7d SOR_INVALID;. }
186ff 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
18700 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
18701 61 67 65 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d age]->nOverflow=
18702 3d 30 20 29 3b 0a 0a 65 6e 64 5f 69 6e 73 65 72 =0 );..end_inser
18703 74 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a t:. return rc;.
18704 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 }../*.** Delete
18705 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 the entry that t
18706 68 65 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 he cursor is poi
18707 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 63 nting to. The c
18708 75 72 73 6f 72 0a 2a 2a 20 69 73 20 6c 65 66 74 ursor.** is left
18709 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 61 pointing at a a
1870a 72 62 69 74 72 61 72 79 20 6c 6f 63 61 74 69 6f rbitrary locatio
1870b 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
1870c 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1870d 42 74 72 65 65 44 65 6c 65 74 65 28 42 74 43 75 BtreeDelete(BtCu
1870e 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 42 rsor *pCur){. B
1870f 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e tree *p = pCur->
18710 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72 pBtree;. BtShar
18711 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
18712 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0a ; .
18713 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
18714 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18715 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
18716 20 63 6f 64 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 code */. MemPa
18717 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 ge *pPage;
18718 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18719 2f 2a 20 50 61 67 65 20 74 6f 20 64 65 6c 65 74 /* Page to delet
1871a 65 20 63 65 6c 6c 20 66 72 6f 6d 20 2a 2f 0a 20 e cell from */.
1871b 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
1871c 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 pCell;
1871d 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
1871e 20 74 6f 20 63 65 6c 6c 20 74 6f 20 64 65 6c 65 to cell to dele
1871f 74 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c te */. int iCel
18720 6c 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 lIdx;
18721 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
18722 49 6e 64 65 78 20 6f 66 20 63 65 6c 6c 20 74 6f Index of cell to
18723 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 delete */. int
18724 20 69 43 65 6c 6c 44 65 70 74 68 3b 20 20 20 20 iCellDepth;
18725 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18726 20 20 2f 2a 20 44 65 70 74 68 20 6f 66 20 6e 6f /* Depth of no
18727 64 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 43 de containing pC
18728 65 6c 6c 20 2a 2f 20 0a 0a 20 20 61 73 73 65 72 ell */ .. asser
18729 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
1872a 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 tex(pCur) );. a
1872b 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 ssert( pBt->inTr
1872c 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 ansaction==TRANS
1872d 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 _WRITE );. asse
1872e 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e rt( !pBt->readOn
1872f 6c 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ly );. assert(
18730 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 29 3b 0a pCur->wrFlag );.
18731 20 20 61 73 73 65 72 74 28 20 68 61 73 53 68 61 assert( hasSha
18732 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
18733 6b 28 70 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 k(p, pCur->pgnoR
18734 6f 6f 74 2c 20 70 43 75 72 2d 3e 70 4b 65 79 49 oot, pCur->pKeyI
18735 6e 66 6f 21 3d 30 2c 20 32 29 20 29 3b 0a 20 20 nfo!=0, 2) );.
18736 61 73 73 65 72 74 28 20 21 68 61 73 52 65 61 64 assert( !hasRead
18737 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 70 43 75 Conflicts(p, pCu
18738 72 2d 3e 70 67 6e 6f 52 6f 6f 74 29 20 29 3b 0a r->pgnoRoot) );.
18739 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 43 75 . if( NEVER(pCu
1873a 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
1873b 50 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70 50 Page]>=pCur->apP
1873c 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
1873d 2d 3e 6e 43 65 6c 6c 29 20 0a 20 20 20 7c 7c 20 ->nCell) . ||
1873e 4e 45 56 45 52 28 70 43 75 72 2d 3e 65 53 74 61 NEVER(pCur->eSta
1873f 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te!=CURSOR_VALID
18740 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ). ){. retur
18741 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 20 n SQLITE_ERROR;
18742 20 2f 2a 20 53 6f 6d 65 74 68 69 6e 67 20 68 61 /* Something ha
18743 73 20 67 6f 6e 65 20 61 77 72 79 2e 20 2a 2f 0a s gone awry. */.
18744 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 }.. /* If thi
18745 73 20 69 73 20 61 20 64 65 6c 65 74 65 20 6f 70 s is a delete op
18746 65 72 61 74 69 6f 6e 20 74 6f 20 72 65 6d 6f 76 eration to remov
18747 65 20 61 20 72 6f 77 20 66 72 6f 6d 20 61 20 74 e a row from a t
18748 61 62 6c 65 20 62 2d 74 72 65 65 2c 0a 20 20 2a able b-tree,. *
18749 2a 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 * invalidate any
1874a 20 69 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f 72 incrblob cursor
1874b 73 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 72 6f s open on the ro
1874c 77 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e w being deleted.
1874d 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 2d */. if( pCur-
1874e 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 20 29 7b 0a >pKeyInfo==0 ){.
1874f 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 49 6e invalidateIn
18750 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 70 2c crblobCursors(p,
18751 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 pCur->info.nKey
18752 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 69 43 65 , 0);. }.. iCe
18753 6c 6c 44 65 70 74 68 20 3d 20 70 43 75 72 2d 3e llDepth = pCur->
18754 69 50 61 67 65 3b 0a 20 20 69 43 65 6c 6c 49 64 iPage;. iCellId
18755 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b x = pCur->aiIdx[
18756 69 43 65 6c 6c 44 65 70 74 68 5d 3b 0a 20 20 70 iCellDepth];. p
18757 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 Page = pCur->apP
18758 61 67 65 5b 69 43 65 6c 6c 44 65 70 74 68 5d 3b age[iCellDepth];
18759 0a 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 . pCell = findC
1875a 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c ell(pPage, iCell
1875b 49 64 78 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 Idx);.. /* If t
1875c 68 65 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 69 he page containi
1875d 6e 67 20 74 68 65 20 65 6e 74 72 79 20 74 6f 20 ng the entry to
1875e 64 65 6c 65 74 65 20 69 73 20 6e 6f 74 20 61 20 delete is not a
1875f 6c 65 61 66 20 70 61 67 65 2c 20 6d 6f 76 65 0a leaf page, move.
18760 20 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20 ** the cursor
18761 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 to the largest e
18762 6e 74 72 79 20 69 6e 20 74 68 65 20 74 72 65 65 ntry in the tree
18763 20 74 68 61 74 20 69 73 20 73 6d 61 6c 6c 65 72 that is smaller
18764 20 74 68 61 6e 0a 20 20 2a 2a 20 74 68 65 20 65 than. ** the e
18765 6e 74 72 79 20 62 65 69 6e 67 20 64 65 6c 65 74 ntry being delet
18766 65 64 2e 20 54 68 69 73 20 63 65 6c 6c 20 77 69 ed. This cell wi
18767 6c 6c 20 72 65 70 6c 61 63 65 20 74 68 65 20 63 ll replace the c
18768 65 6c 6c 20 62 65 69 6e 67 20 64 65 6c 65 74 65 ell being delete
18769 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 d. ** from the
1876a 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 2e 20 54 internal node. T
1876b 68 65 20 27 70 72 65 76 69 6f 75 73 27 20 65 6e he 'previous' en
1876c 74 72 79 20 69 73 20 75 73 65 64 20 66 6f 72 20 try is used for
1876d 74 68 69 73 20 69 6e 73 74 65 61 64 0a 20 20 2a this instead. *
1876e 2a 20 6f 66 20 74 68 65 20 27 6e 65 78 74 27 20 * of the 'next'
1876f 65 6e 74 72 79 2c 20 61 73 20 74 68 65 20 70 72 entry, as the pr
18770 65 76 69 6f 75 73 20 65 6e 74 72 79 20 69 73 20 evious entry is
18771 61 6c 77 61 79 73 20 61 20 70 61 72 74 20 6f 66 always a part of
18772 20 74 68 65 0a 20 20 2a 2a 20 73 75 62 2d 74 72 the. ** sub-tr
18773 65 65 20 68 65 61 64 65 64 20 62 79 20 74 68 65 ee headed by the
18774 20 63 68 69 6c 64 20 70 61 67 65 20 6f 66 20 74 child page of t
18775 68 65 20 63 65 6c 6c 20 62 65 69 6e 67 20 64 65 he cell being de
18776 6c 65 74 65 64 2e 20 54 68 69 73 20 6d 61 6b 65 leted. This make
18777 73 0a 20 20 2a 2a 20 62 61 6c 61 6e 63 69 6e 67 s. ** balancing
18778 20 74 68 65 20 74 72 65 65 20 66 6f 6c 6c 6f 77 the tree follow
18779 69 6e 67 20 74 68 65 20 64 65 6c 65 74 65 20 6f ing the delete o
1877a 70 65 72 61 74 69 6f 6e 20 65 61 73 69 65 72 2e peration easier.
1877b 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 61 67 */. if( !pPag
1877c 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 69 e->leaf ){. i
1877d 6e 74 20 6e 6f 74 55 73 65 64 3b 0a 20 20 20 20 nt notUsed;.
1877e 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1877f 65 50 72 65 76 69 6f 75 73 28 70 43 75 72 2c 20 ePrevious(pCur,
18780 26 6e 6f 74 55 73 65 64 29 3b 0a 20 20 20 20 69 ¬Used);. i
18781 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
18782 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 61 76 c;. }.. /* Sav
18783 65 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 e the positions
18784 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 63 75 72 of any other cur
18785 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 sors open on thi
18786 73 20 74 61 62 6c 65 20 62 65 66 6f 72 65 0a 20 s table before.
18787 20 2a 2a 20 6d 61 6b 69 6e 67 20 61 6e 79 20 6d ** making any m
18788 6f 64 69 66 69 63 61 74 69 6f 6e 73 2e 20 4d 61 odifications. Ma
18789 6b 65 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74 ke the page cont
1878a 61 69 6e 69 6e 67 20 74 68 65 20 65 6e 74 72 79 aining the entry
1878b 20 74 6f 20 62 65 20 0a 20 20 2a 2a 20 64 65 6c to be . ** del
1878c 65 74 65 64 20 77 72 69 74 61 62 6c 65 2e 20 54 eted writable. T
1878d 68 65 6e 20 66 72 65 65 20 61 6e 79 20 6f 76 65 hen free any ove
1878e 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73 73 6f rflow pages asso
1878f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
18790 0a 20 20 2a 2a 20 65 6e 74 72 79 20 61 6e 64 20 . ** entry and
18791 66 69 6e 61 6c 6c 79 20 72 65 6d 6f 76 65 20 74 finally remove t
18792 68 65 20 63 65 6c 6c 20 69 74 73 65 6c 66 20 66 he cell itself f
18793 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 70 rom within the p
18794 61 67 65 2e 20 20 0a 20 20 2a 2f 0a 20 20 72 63 age. . */. rc
18795 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 = saveAllCursor
18796 73 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e s(pBt, pCur->pgn
18797 6f 52 6f 6f 74 2c 20 70 43 75 72 29 3b 0a 20 20 oRoot, pCur);.
18798 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
18799 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 rc;. rc = sqlit
1879a 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 e3PagerWrite(pPa
1879b 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 ge->pDbPage);.
1879c 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
1879d 72 63 3b 0a 20 20 72 63 20 3d 20 63 6c 65 61 72 rc;. rc = clear
1879e 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 6c Cell(pPage, pCel
1879f 6c 29 3b 0a 20 20 64 72 6f 70 43 65 6c 6c 28 70 l);. dropCell(p
187a0 50 61 67 65 2c 20 69 43 65 6c 6c 49 64 78 2c 20 Page, iCellIdx,
187a1 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 cellSizePtr(pPag
187a2 65 2c 20 70 43 65 6c 6c 29 2c 20 26 72 63 29 3b e, pCell), &rc);
187a3 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 . if( rc ) retu
187a4 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 rn rc;.. /* If
187a5 74 68 65 20 63 65 6c 6c 20 64 65 6c 65 74 65 64 the cell deleted
187a6 20 77 61 73 20 6e 6f 74 20 6c 6f 63 61 74 65 64 was not located
187a7 20 6f 6e 20 61 20 6c 65 61 66 20 70 61 67 65 2c on a leaf page,
187a8 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 then the cursor
187a9 0a 20 20 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 . ** is current
187aa 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 ly pointing to t
187ab 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79 he largest entry
187ac 20 69 6e 20 74 68 65 20 73 75 62 2d 74 72 65 65 in the sub-tree
187ad 20 68 65 61 64 65 64 0a 20 20 2a 2a 20 62 79 20 headed. ** by
187ae 74 68 65 20 63 68 69 6c 64 2d 70 61 67 65 20 6f the child-page o
187af 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 f the cell that
187b0 77 61 73 20 6a 75 73 74 20 64 65 6c 65 74 65 64 was just deleted
187b1 20 66 72 6f 6d 20 61 6e 20 69 6e 74 65 72 6e 61 from an interna
187b2 6c 0a 20 20 2a 2a 20 6e 6f 64 65 2e 20 54 68 65 l. ** node. The
187b3 20 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65 20 6c cell from the l
187b4 65 61 66 20 6e 6f 64 65 20 6e 65 65 64 73 20 74 eaf node needs t
187b5 6f 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 o be moved to th
187b6 65 20 69 6e 74 65 72 6e 61 6c 0a 20 20 2a 2a 20 e internal. **
187b7 6e 6f 64 65 20 74 6f 20 72 65 70 6c 61 63 65 20 node to replace
187b8 74 68 65 20 64 65 6c 65 74 65 64 20 63 65 6c 6c the deleted cell
187b9 2e 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 61 . */. if( !pPa
187ba 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 ge->leaf ){.
187bb 4d 65 6d 50 61 67 65 20 2a 70 4c 65 61 66 20 3d MemPage *pLeaf =
187bc 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
187bd 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 ur->iPage];.
187be 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 20 20 50 int nCell;. P
187bf 67 6e 6f 20 6e 20 3d 20 70 43 75 72 2d 3e 61 70 gno n = pCur->ap
187c0 50 61 67 65 5b 69 43 65 6c 6c 44 65 70 74 68 2b Page[iCellDepth+
187c1 31 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 75 6e 1]->pgno;. un
187c2 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 54 6d signed char *pTm
187c3 70 3b 0a 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 p;.. pCell =
187c4 66 69 6e 64 43 65 6c 6c 28 70 4c 65 61 66 2c 20 findCell(pLeaf,
187c5 70 4c 65 61 66 2d 3e 6e 43 65 6c 6c 2d 31 29 3b pLeaf->nCell-1);
187c6 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20 63 65 6c . nCell = cel
187c7 6c 53 69 7a 65 50 74 72 28 70 4c 65 61 66 2c 20 lSizePtr(pLeaf,
187c8 70 43 65 6c 6c 29 3b 0a 20 20 20 20 61 73 73 65 pCell);. asse
187c9 72 74 28 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 rt( MX_CELL_SIZE
187ca 28 70 42 74 29 3e 3d 6e 43 65 6c 6c 20 29 3b 0a (pBt)>=nCell );.
187cb 0a 20 20 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d . allocateTem
187cc 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 pSpace(pBt);.
187cd 20 70 54 6d 70 20 3d 20 70 42 74 2d 3e 70 54 6d pTmp = pBt->pTm
187ce 70 53 70 61 63 65 3b 0a 0a 20 20 20 20 72 63 20 pSpace;.. rc
187cf 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
187d0 69 74 65 28 70 4c 65 61 66 2d 3e 70 44 62 50 61 ite(pLeaf->pDbPa
187d1 67 65 29 3b 0a 20 20 20 20 69 6e 73 65 72 74 43 ge);. insertC
187d2 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c ell(pPage, iCell
187d3 49 64 78 2c 20 70 43 65 6c 6c 2d 34 2c 20 6e 43 Idx, pCell-4, nC
187d4 65 6c 6c 2b 34 2c 20 70 54 6d 70 2c 20 6e 2c 20 ell+4, pTmp, n,
187d5 26 72 63 29 3b 0a 20 20 20 20 64 72 6f 70 43 65 &rc);. dropCe
187d6 6c 6c 28 70 4c 65 61 66 2c 20 70 4c 65 61 66 2d ll(pLeaf, pLeaf-
187d7 3e 6e 43 65 6c 6c 2d 31 2c 20 6e 43 65 6c 6c 2c >nCell-1, nCell,
187d8 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20 72 &rc);. if( r
187d9 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
187da 20 7d 0a 0a 20 20 2f 2a 20 42 61 6c 61 6e 63 65 }.. /* Balance
187db 20 74 68 65 20 74 72 65 65 2e 20 49 66 20 74 68 the tree. If th
187dc 65 20 65 6e 74 72 79 20 64 65 6c 65 74 65 64 20 e entry deleted
187dd 77 61 73 20 6c 6f 63 61 74 65 64 20 6f 6e 20 61 was located on a
187de 20 6c 65 61 66 20 70 61 67 65 2c 0a 20 20 2a 2a leaf page,. **
187df 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 then the cursor
187e0 20 73 74 69 6c 6c 20 70 6f 69 6e 74 73 20 74 6f still points to
187e1 20 74 68 61 74 20 70 61 67 65 2e 20 49 6e 20 74 that page. In t
187e2 68 69 73 20 63 61 73 65 20 74 68 65 20 66 69 72 his case the fir
187e3 73 74 0a 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 st. ** call to
187e4 62 61 6c 61 6e 63 65 28 29 20 72 65 70 61 69 72 balance() repair
187e5 73 20 74 68 65 20 74 72 65 65 2c 20 61 6e 64 20 s the tree, and
187e6 74 68 65 20 69 66 28 2e 2e 2e 29 20 63 6f 6e 64 the if(...) cond
187e7 69 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 6e 65 ition is. ** ne
187e8 76 65 72 20 74 72 75 65 2e 0a 20 20 2a 2a 0a 20 ver true.. **.
187e9 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 ** Otherwise, i
187ea 66 20 74 68 65 20 65 6e 74 72 79 20 64 65 6c 65 f the entry dele
187eb 74 65 64 20 77 61 73 20 6f 6e 20 61 6e 20 69 6e ted was on an in
187ec 74 65 72 6e 61 6c 20 6e 6f 64 65 20 70 61 67 65 ternal node page
187ed 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 70 43 75 72 , then. ** pCur
187ee 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 is pointing to
187ef 74 68 65 20 6c 65 61 66 20 70 61 67 65 20 66 72 the leaf page fr
187f0 6f 6d 20 77 68 69 63 68 20 61 20 63 65 6c 6c 20 om which a cell
187f1 77 61 73 20 72 65 6d 6f 76 65 64 20 74 6f 0a 20 was removed to.
187f2 20 2a 2a 20 72 65 70 6c 61 63 65 20 74 68 65 20 ** replace the
187f3 63 65 6c 6c 20 64 65 6c 65 74 65 64 20 66 72 6f cell deleted fro
187f4 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e m the internal n
187f5 6f 64 65 2e 20 54 68 69 73 20 69 73 20 73 6c 69 ode. This is sli
187f6 67 68 74 6c 79 0a 20 20 2a 2a 20 74 72 69 63 6b ghtly. ** trick
187f7 79 20 61 73 20 74 68 65 20 6c 65 61 66 20 6e 6f y as the leaf no
187f8 64 65 20 6d 61 79 20 62 65 20 75 6e 64 65 72 66 de may be underf
187f9 75 6c 6c 2c 20 61 6e 64 20 74 68 65 20 69 6e 74 ull, and the int
187fa 65 72 6e 61 6c 20 6e 6f 64 65 20 6d 61 79 0a 20 ernal node may.
187fb 20 2a 2a 20 62 65 20 65 69 74 68 65 72 20 75 6e ** be either un
187fc 64 65 72 20 6f 72 20 6f 76 65 72 66 75 6c 6c 2e der or overfull.
187fd 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 72 75 In this case ru
187fe 6e 20 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 20 n the balancing
187ff 61 6c 67 6f 72 69 74 68 6d 0a 20 20 2a 2a 20 6f algorithm. ** o
18800 6e 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 n the leaf node
18801 66 69 72 73 74 2e 20 49 66 20 74 68 65 20 62 61 first. If the ba
18802 6c 61 6e 63 65 20 70 72 6f 63 65 65 64 73 20 66 lance proceeds f
18803 61 72 20 65 6e 6f 75 67 68 20 75 70 20 74 68 65 ar enough up the
18804 0a 20 20 2a 2a 20 74 72 65 65 20 74 68 61 74 20 . ** tree that
18805 77 65 20 63 61 6e 20 62 65 20 73 75 72 65 20 74 we can be sure t
18806 68 61 74 20 61 6e 79 20 70 72 6f 62 6c 65 6d 20 hat any problem
18807 69 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 in the internal
18808 6e 6f 64 65 20 68 61 73 0a 20 20 2a 2a 20 62 65 node has. ** be
18809 65 6e 20 63 6f 72 72 65 63 74 65 64 2c 20 73 6f en corrected, so
1880a 20 62 65 20 69 74 2e 20 4f 74 68 65 72 77 69 73 be it. Otherwis
1880b 65 2c 20 61 66 74 65 72 20 62 61 6c 61 6e 63 69 e, after balanci
1880c 6e 67 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 ng the leaf node
1880d 2c 0a 20 20 2a 2a 20 77 61 6c 6b 20 74 68 65 20 ,. ** walk the
1880e 63 75 72 73 6f 72 20 75 70 20 74 68 65 20 74 72 cursor up the tr
1880f 65 65 20 74 6f 20 74 68 65 20 69 6e 74 65 72 6e ee to the intern
18810 61 6c 20 6e 6f 64 65 20 61 6e 64 20 62 61 6c 61 al node and bala
18811 6e 63 65 20 69 74 20 61 73 20 0a 20 20 2a 2a 20 nce it as . **
18812 77 65 6c 6c 2e 20 20 2a 2f 0a 20 20 72 63 20 3d well. */. rc =
18813 20 62 61 6c 61 6e 63 65 28 70 43 75 72 29 3b 0a balance(pCur);.
18814 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
18815 5f 4f 4b 20 26 26 20 70 43 75 72 2d 3e 69 50 61 _OK && pCur->iPa
18816 67 65 3e 69 43 65 6c 6c 44 65 70 74 68 20 29 7b ge>iCellDepth ){
18817 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43 75 72 . while( pCur
18818 2d 3e 69 50 61 67 65 3e 69 43 65 6c 6c 44 65 70 ->iPage>iCellDep
18819 74 68 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 th ){. rele
1881a 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 asePage(pCur->ap
1881b 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
1881c 2d 2d 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 --]);. }.
1881d 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 rc = balance(pCu
1881e 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 r);. }.. if( r
1881f 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
18820 20 20 20 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 moveToRoot(p
18821 43 75 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 Cur);. }. retu
18822 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
18823 43 72 65 61 74 65 20 61 20 6e 65 77 20 42 54 72 Create a new BTr
18824 65 65 20 74 61 62 6c 65 2e 20 20 57 72 69 74 65 ee table. Write
18825 20 69 6e 74 6f 20 2a 70 69 54 61 62 6c 65 20 74 into *piTable t
18826 68 65 20 70 61 67 65 0a 2a 2a 20 6e 75 6d 62 65 he page.** numbe
18827 72 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 20 70 r for the root p
18828 61 67 65 20 6f 66 20 74 68 65 20 6e 65 77 20 74 age of the new t
18829 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 able..**.** The
1882a 74 79 70 65 20 6f 66 20 74 79 70 65 20 69 73 20 type of type is
1882b 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 determined by th
1882c 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 e flags paramete
1882d 72 2e 20 20 4f 6e 6c 79 20 74 68 65 0a 2a 2a 20 r. Only the.**
1882e 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 following values
1882f 20 6f 66 20 66 6c 61 67 73 20 61 72 65 20 63 75 of flags are cu
18830 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 20 rrently in use.
18831 20 4f 74 68 65 72 20 76 61 6c 75 65 73 20 66 6f Other values fo
18832 72 0a 2a 2a 20 66 6c 61 67 73 20 6d 69 67 68 74 r.** flags might
18833 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a 2a 0a 2a 2a not work:.**.**
18834 20 20 20 20 20 42 54 52 45 45 5f 49 4e 54 4b 45 BTREE_INTKE
18835 59 7c 42 54 52 45 45 5f 4c 45 41 46 44 41 54 41 Y|BTREE_LEAFDATA
18836 20 20 20 20 20 55 73 65 64 20 66 6f 72 20 53 51 Used for SQ
18837 4c 20 74 61 62 6c 65 73 20 77 69 74 68 20 72 6f L tables with ro
18838 77 69 64 20 6b 65 79 73 0a 2a 2a 20 20 20 20 20 wid keys.**
18839 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 20 20 BTREE_ZERODATA
1883a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1883b 55 73 65 64 20 66 6f 72 20 53 51 4c 20 69 6e 64 Used for SQL ind
1883c 69 63 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ices.*/.static i
1883d 6e 74 20 62 74 72 65 65 43 72 65 61 74 65 54 61 nt btreeCreateTa
1883e 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e ble(Btree *p, in
1883f 74 20 2a 70 69 54 61 62 6c 65 2c 20 69 6e 74 20 t *piTable, int
18840 66 6c 61 67 73 29 7b 0a 20 20 42 74 53 68 61 72 flags){. BtShar
18841 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
18842 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 52 6f ;. MemPage *pRo
18843 6f 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 52 ot;. Pgno pgnoR
18844 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a oot;. int rc;..
18845 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
18846 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 3BtreeHoldsMutex
18847 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 (p) );. assert(
18848 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
18849 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ion==TRANS_WRITE
1884a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 );. assert( !p
1884b 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a Bt->readOnly );.
1884c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
1884d 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
1884e 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 rc = allocateBt
1884f 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 52 reePage(pBt, &pR
18850 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f 6f 74 2c 20 oot, &pgnoRoot,
18851 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 1, 0);. if( rc
18852 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
18853 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 ;. }.#else. if
18854 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
18855 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 m ){. Pgno pg
18856 6e 6f 4d 6f 76 65 3b 20 20 20 20 20 20 2f 2a 20 noMove; /*
18857 4d 6f 76 65 20 61 20 70 61 67 65 20 68 65 72 65 Move a page here
18858 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f to make room fo
18859 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 r the root-page
1885a 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a */. MemPage *
1885b 70 50 61 67 65 4d 6f 76 65 3b 20 2f 2a 20 54 68 pPageMove; /* Th
1885c 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 74 e page to move t
1885d 6f 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 43 72 o. */.. /* Cr
1885e 65 61 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62 eating a new tab
1885f 6c 65 20 6d 61 79 20 70 72 6f 62 61 62 6c 79 20 le may probably
18860 72 65 71 75 69 72 65 20 6d 6f 76 69 6e 67 20 61 require moving a
18861 6e 20 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 n existing datab
18862 61 73 65 0a 20 20 20 20 2a 2a 20 74 6f 20 6d 61 ase. ** to ma
18863 6b 65 20 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20 ke room for the
18864 6e 65 77 20 74 61 62 6c 65 73 20 72 6f 6f 74 20 new tables root
18865 70 61 67 65 2e 20 49 6e 20 63 61 73 65 20 74 68 page. In case th
18866 69 73 20 70 61 67 65 20 74 75 72 6e 73 0a 20 20 is page turns.
18867 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65 20 61 ** out to be a
18868 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c n overflow page,
18869 20 64 65 6c 65 74 65 20 61 6c 6c 20 6f 76 65 72 delete all over
1886a 66 6c 6f 77 20 70 61 67 65 2d 6d 61 70 20 63 61 flow page-map ca
1886b 63 68 65 73 0a 20 20 20 20 2a 2a 20 68 65 6c 64 ches. ** held
1886c 20 62 79 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 by open cursors
1886d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 76 .. */. inv
1886e 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c alidateAllOverfl
1886f 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a 0a 20 owCache(pBt);..
18870 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 76 /* Read the v
18871 61 6c 75 65 20 6f 66 20 6d 65 74 61 5b 33 5d 20 alue of meta[3]
18872 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
18873 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 e to determine w
18874 68 65 72 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 here the. **
18875 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 root page of the
18876 20 6e 65 77 20 74 61 62 6c 65 20 73 68 6f 75 6c new table shoul
18877 64 20 67 6f 2e 20 6d 65 74 61 5b 33 5d 20 69 73 d go. meta[3] is
18878 20 74 68 65 20 6c 61 72 67 65 73 74 20 72 6f 6f the largest roo
18879 74 2d 70 61 67 65 0a 20 20 20 20 2a 2a 20 63 72 t-page. ** cr
1887a 65 61 74 65 64 20 73 6f 20 66 61 72 2c 20 73 6f eated so far, so
1887b 20 74 68 65 20 6e 65 77 20 72 6f 6f 74 2d 70 61 the new root-pa
1887c 67 65 20 69 73 20 28 6d 65 74 61 5b 33 5d 2b 31 ge is (meta[3]+1
1887d 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 ).. */. sq
1887e 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 lite3BtreeGetMet
1887f 61 28 70 2c 20 42 54 52 45 45 5f 4c 41 52 47 45 a(p, BTREE_LARGE
18880 53 54 5f 52 4f 4f 54 5f 50 41 47 45 2c 20 26 70 ST_ROOT_PAGE, &p
18881 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 70 67 gnoRoot);. pg
18882 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20 20 20 20 2f noRoot++;.. /
18883 2a 20 54 68 65 20 6e 65 77 20 72 6f 6f 74 2d 70 * The new root-p
18884 61 67 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 age may not be a
18885 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 61 20 70 6f llocated on a po
18886 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 inter-map page,
18887 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 50 45 or the. ** PE
18888 4e 44 49 4e 47 5f 42 59 54 45 20 70 61 67 65 2e NDING_BYTE page.
18889 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 69 6c . */. whil
1888a 65 28 20 70 67 6e 6f 52 6f 6f 74 3d 3d 50 54 52 e( pgnoRoot==PTR
1888b 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 MAP_PAGENO(pBt,
1888c 70 67 6e 6f 52 6f 6f 74 29 20 7c 7c 0a 20 20 20 pgnoRoot) ||.
1888d 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 3d 3d 50 pgnoRoot==P
1888e 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
1888f 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 70 (pBt) ){. p
18890 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20 20 20 20 7d gnoRoot++;. }
18891 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 67 6e . assert( pgn
18892 6f 52 6f 6f 74 3e 3d 33 20 29 3b 0a 0a 20 20 20 oRoot>=3 );..
18893 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 70 /* Allocate a p
18894 61 67 65 2e 20 54 68 65 20 70 61 67 65 20 74 68 age. The page th
18895 61 74 20 63 75 72 72 65 6e 74 6c 79 20 72 65 73 at currently res
18896 69 64 65 73 20 61 74 20 70 67 6e 6f 52 6f 6f 74 ides at pgnoRoot
18897 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65 20 will. ** be
18898 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 61 6c 6c moved to the all
18899 6f 63 61 74 65 64 20 70 61 67 65 20 28 75 6e 6c ocated page (unl
1889a 65 73 73 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 ess the allocate
1889b 64 20 70 61 67 65 20 68 61 70 70 65 6e 73 0a 20 d page happens.
1889c 20 20 20 2a 2a 20 74 6f 20 72 65 73 69 64 65 20 ** to reside
1889d 61 74 20 70 67 6e 6f 52 6f 6f 74 29 2e 0a 20 20 at pgnoRoot)..
1889e 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 61 6c */. rc = al
1889f 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 locateBtreePage(
188a0 70 42 74 2c 20 26 70 50 61 67 65 4d 6f 76 65 2c pBt, &pPageMove,
188a1 20 26 70 67 6e 6f 4d 6f 76 65 2c 20 70 67 6e 6f &pgnoMove, pgno
188a2 52 6f 6f 74 2c 20 31 29 3b 0a 20 20 20 20 69 66 Root, 1);. if
188a3 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
188a4 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
188a5 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 rc;. }.. i
188a6 66 28 20 70 67 6e 6f 4d 6f 76 65 21 3d 70 67 6e f( pgnoMove!=pgn
188a7 6f 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 20 2f oRoot ){. /
188a8 2a 20 70 67 6e 6f 52 6f 6f 74 20 69 73 20 74 68 * pgnoRoot is th
188a9 65 20 70 61 67 65 20 74 68 61 74 20 77 69 6c 6c e page that will
188aa 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 be used for the
188ab 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 0a 20 20 root-page of.
188ac 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 74 ** the new t
188ad 61 62 6c 65 20 28 61 73 73 75 6d 69 6e 67 20 61 able (assuming a
188ae 6e 20 65 72 72 6f 72 20 64 69 64 20 6e 6f 74 20 n error did not
188af 6f 63 63 75 72 29 2e 20 42 75 74 20 77 65 20 77 occur). But we w
188b0 65 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 6c 6c ere. ** all
188b1 6f 63 61 74 65 64 20 70 67 6e 6f 4d 6f 76 65 2e ocated pgnoMove.
188b2 20 49 66 20 72 65 71 75 69 72 65 64 20 28 69 2e If required (i.
188b3 65 2e 20 69 66 20 69 74 20 77 61 73 20 6e 6f 74 e. if it was not
188b4 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20 20 allocated.
188b5 20 2a 2a 20 62 79 20 65 78 74 65 6e 64 69 6e 67 ** by extending
188b6 20 74 68 65 20 66 69 6c 65 29 2c 20 74 68 65 20 the file), the
188b7 63 75 72 72 65 6e 74 20 70 61 67 65 20 61 74 20 current page at
188b8 70 6f 73 69 74 69 6f 6e 20 70 67 6e 6f 4d 6f 76 position pgnoMov
188b9 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 61 6c e. ** is al
188ba 72 65 61 64 79 20 6a 6f 75 72 6e 61 6c 65 64 2e ready journaled.
188bb 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
188bc 75 38 20 65 54 79 70 65 20 3d 20 30 3b 0a 20 20 u8 eType = 0;.
188bd 20 20 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 Pgno iPtrPag
188be 65 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 72 65 e = 0;.. re
188bf 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 4d leasePage(pPageM
188c0 6f 76 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 ove);.. /*
188c1 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20 63 75 Move the page cu
188c2 72 72 65 6e 74 6c 79 20 61 74 20 70 67 6e 6f 52 rrently at pgnoR
188c3 6f 6f 74 20 74 6f 20 70 67 6e 6f 4d 6f 76 65 2e oot to pgnoMove.
188c4 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 62 */. rc = b
188c5 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
188c6 20 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f pgnoRoot, &pRoo
188c7 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 t, 0);. if(
188c8 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
188c9 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
188ca 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc;. }.
188cb 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 rc = ptrmapGe
188cc 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c t(pBt, pgnoRoot,
188cd 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 50 61 &eType, &iPtrPa
188ce 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 ge);. if( e
188cf 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f Type==PTRMAP_ROO
188d0 54 50 41 47 45 20 7c 7c 20 65 54 79 70 65 3d 3d TPAGE || eType==
188d1 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 PTRMAP_FREEPAGE
188d2 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
188d3 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
188d4 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 KPT;. }.
188d5 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
188d6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
188d7 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f releasePage(pRoo
188d8 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 t);. retu
188d9 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 rn rc;. }.
188da 20 20 20 20 20 61 73 73 65 72 74 28 20 65 54 79 assert( eTy
188db 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 pe!=PTRMAP_ROOTP
188dc 41 47 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73 AGE );. ass
188dd 65 72 74 28 20 65 54 79 70 65 21 3d 50 54 52 4d ert( eType!=PTRM
188de 41 50 5f 46 52 45 45 50 41 47 45 20 29 3b 0a 20 AP_FREEPAGE );.
188df 20 20 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 rc = reloca
188e0 74 65 50 61 67 65 28 70 42 74 2c 20 70 52 6f 6f tePage(pBt, pRoo
188e1 74 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 t, eType, iPtrPa
188e2 67 65 2c 20 70 67 6e 6f 4d 6f 76 65 2c 20 30 29 ge, pgnoMove, 0)
188e3 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 ;. releaseP
188e4 61 67 65 28 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 age(pRoot);..
188e5 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65 /* Obtain the
188e6 20 70 61 67 65 20 61 74 20 70 67 6e 6f 52 6f 6f page at pgnoRoo
188e7 74 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 t */. if( r
188e8 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
188e9 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
188ea 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 c;. }.
188eb 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 rc = btreeGetPa
188ec 67 65 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 ge(pBt, pgnoRoot
188ed 2c 20 26 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 , &pRoot, 0);.
188ee 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
188ef 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
188f0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
188f1 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 }. rc = s
188f2 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
188f3 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 (pRoot->pDbPage)
188f4 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
188f5 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
188f6 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
188f7 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 (pRoot);.
188f8 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
188f9 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
188fa 20 20 20 20 20 70 52 6f 6f 74 20 3d 20 70 50 61 pRoot = pPa
188fb 67 65 4d 6f 76 65 3b 0a 20 20 20 20 7d 20 0a 0a geMove;. } ..
188fc 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 /* Update th
188fd 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 61 6e e pointer-map an
188fe 64 20 6d 65 74 61 2d 64 61 74 61 20 77 69 74 68 d meta-data with
188ff 20 74 68 65 20 6e 65 77 20 72 6f 6f 74 2d 70 61 the new root-pa
18900 67 65 20 6e 75 6d 62 65 72 2e 20 2a 2f 0a 20 20 ge number. */.
18901 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c ptrmapPut(pBt,
18902 20 70 67 6e 6f 52 6f 6f 74 2c 20 50 54 52 4d 41 pgnoRoot, PTRMA
18903 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30 2c 20 26 P_ROOTPAGE, 0, &
18904 72 63 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 rc);. if( rc
18905 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 ){. release
18906 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 Page(pRoot);.
18907 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
18908 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c }. rc = sql
18909 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d ite3BtreeUpdateM
1890a 65 74 61 28 70 2c 20 34 2c 20 70 67 6e 6f 52 6f eta(p, 4, pgnoRo
1890b 6f 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 ot);. if( rc
1890c 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 ){. release
1890d 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 Page(pRoot);.
1890e 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1890f 20 20 7d 0a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }.. }else{.
18910 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 rc = allocateB
18911 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 treePage(pBt, &p
18912 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f 6f 74 2c Root, &pgnoRoot,
18913 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 1, 0);. if(
18914 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
18915 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 }.#endif. ass
18916 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
18917 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 52 6f rIswriteable(pRo
18918 6f 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ot->pDbPage) );.
18919 20 20 7a 65 72 6f 50 61 67 65 28 70 52 6f 6f 74 zeroPage(pRoot
1891a 2c 20 66 6c 61 67 73 20 7c 20 50 54 46 5f 4c 45 , flags | PTF_LE
1891b 41 46 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 AF);. sqlite3Pa
1891c 67 65 72 55 6e 72 65 66 28 70 52 6f 6f 74 2d 3e gerUnref(pRoot->
1891d 70 44 62 50 61 67 65 29 3b 0a 20 20 2a 70 69 54 pDbPage);. *piT
1891e 61 62 6c 65 20 3d 20 28 69 6e 74 29 70 67 6e 6f able = (int)pgno
1891f 52 6f 6f 74 3b 0a 20 20 72 65 74 75 72 6e 20 53 Root;. return S
18920 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 QLITE_OK;.}.SQLI
18921 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
18922 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 qlite3BtreeCreat
18923 65 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c eTable(Btree *p,
18924 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20 69 int *piTable, i
18925 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 nt flags){. int
18926 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 rc;. sqlite3Bt
18927 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 reeEnter(p);. r
18928 63 20 3d 20 62 74 72 65 65 43 72 65 61 74 65 54 c = btreeCreateT
18929 61 62 6c 65 28 70 2c 20 70 69 54 61 62 6c 65 2c able(p, piTable,
1892a 20 66 6c 61 67 73 29 3b 0a 20 20 73 71 6c 69 74 flags);. sqlit
1892b 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
1892c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1892d 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 74 68 65 ./*.** Erase the
1892e 20 67 69 76 65 6e 20 64 61 74 61 62 61 73 65 20 given database
1892f 70 61 67 65 20 61 6e 64 20 61 6c 6c 20 69 74 73 page and all its
18930 20 63 68 69 6c 64 72 65 6e 2e 20 20 52 65 74 75 children. Retu
18931 72 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 74 rn.** the page t
18932 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a o the freelist..
18933 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c */.static int cl
18934 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28 earDatabasePage(
18935 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
18936 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 , /* T
18937 68 65 20 42 54 72 65 65 20 74 68 61 74 20 63 6f he BTree that co
18938 6e 74 61 69 6e 73 20 74 68 65 20 74 61 62 6c 65 ntains the table
18939 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c */. Pgno pgno,
1893a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
1893b 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 63 6c age number to cl
1893c 65 61 72 20 2a 2f 0a 20 20 69 6e 74 20 66 72 65 ear */. int fre
1893d 65 50 61 67 65 46 6c 61 67 2c 20 20 20 20 20 2f ePageFlag, /
1893e 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 70 61 67 * Deallocate pag
1893f 65 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 69 e if true */. i
18940 6e 74 20 2a 70 6e 43 68 61 6e 67 65 0a 29 7b 0a nt *pnChange.){.
18941 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
18942 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e ;. int rc;. un
18943 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 signed char *pCe
18944 6c 6c 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 ll;. int i;..
18945 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
18946 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
18947 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
18948 70 67 6e 6f 3e 70 61 67 65 72 50 61 67 65 63 6f pgno>pagerPageco
18949 75 6e 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 unt(pBt) ){.
1894a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
1894b 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a RRUPT_BKPT;. }.
1894c 0a 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e . rc = getAndIn
1894d 69 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f itPage(pBt, pgno
1894e 2c 20 26 70 50 61 67 65 29 3b 0a 20 20 69 66 28 , &pPage);. if(
1894f 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
18950 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 . for(i=0; i<pP
18951 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 age->nCell; i++)
18952 7b 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 {. pCell = fi
18953 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 ndCell(pPage, i)
18954 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 ;. if( !pPage
18955 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 ->leaf ){.
18956 72 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61 rc = clearDataba
18957 73 65 50 61 67 65 28 70 42 74 2c 20 67 65 74 34 sePage(pBt, get4
18958 62 79 74 65 28 70 43 65 6c 6c 29 2c 20 31 2c 20 byte(pCell), 1,
18959 70 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 20 pnChange);.
1895a 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63 if( rc ) goto c
1895b 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 67 65 leardatabasepage
1895c 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 _out;. }.
1895d 72 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 rc = clearCell(p
1895e 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 Page, pCell);.
1895f 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
18960 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 67 cleardatabasepag
18961 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28 e_out;. }. if(
18962 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
18963 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 . rc = clearD
18964 61 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c atabasePage(pBt,
18965 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
18966 2d 3e 61 44 61 74 61 5b 38 5d 29 2c 20 31 2c 20 ->aData[8]), 1,
18967 70 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 69 pnChange);. i
18968 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 f( rc ) goto cle
18969 61 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f ardatabasepage_o
1896a 75 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 ut;. }else if(
1896b 70 6e 43 68 61 6e 67 65 20 29 7b 0a 20 20 20 20 pnChange ){.
1896c 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 assert( pPage->i
1896d 6e 74 4b 65 79 20 29 3b 0a 20 20 20 20 2a 70 6e ntKey );. *pn
1896e 43 68 61 6e 67 65 20 2b 3d 20 70 50 61 67 65 2d Change += pPage-
1896f 3e 6e 43 65 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 >nCell;. }. if
18970 28 20 66 72 65 65 50 61 67 65 46 6c 61 67 20 29 ( freePageFlag )
18971 7b 0a 20 20 20 20 66 72 65 65 50 61 67 65 28 70 {. freePage(p
18972 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 7d 65 Page, &rc);. }e
18973 6c 73 65 20 69 66 28 20 28 72 63 20 3d 20 73 71 lse if( (rc = sq
18974 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
18975 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29 pPage->pDbPage))
18976 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 65 72 6f 50 ==0 ){. zeroP
18977 61 67 65 28 70 50 61 67 65 2c 20 70 50 61 67 65 age(pPage, pPage
18978 2d 3e 61 44 61 74 61 5b 30 5d 20 7c 20 50 54 46 ->aData[0] | PTF
18979 5f 4c 45 41 46 29 3b 0a 20 20 7d 0a 0a 63 6c 65 _LEAF);. }..cle
1897a 61 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f ardatabasepage_o
1897b 75 74 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 ut:. releasePag
1897c 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 74 75 e(pPage);. retu
1897d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1897e 44 65 6c 65 74 65 20 61 6c 6c 20 69 6e 66 6f 72 Delete all infor
1897f 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 73 69 mation from a si
18980 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 74 68 ngle table in th
18981 65 20 64 61 74 61 62 61 73 65 2e 20 20 69 54 61 e database. iTa
18982 62 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 70 61 ble is.** the pa
18983 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
18984 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 root of the tab
18985 6c 65 2e 20 20 41 66 74 65 72 20 74 68 69 73 20 le. After this
18986 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2c routine returns,
18987 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70 61 67 .** the root pag
18988 65 20 69 73 20 65 6d 70 74 79 2c 20 62 75 74 20 e is empty, but
18989 73 74 69 6c 6c 20 65 78 69 73 74 73 2e 0a 2a 2a still exists..**
1898a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1898b 20 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 will fail with
1898c 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 SQLITE_LOCKED if
1898d 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f there are any o
1898e 70 65 6e 0a 2a 2a 20 72 65 61 64 20 63 75 72 73 pen.** read curs
1898f 6f 72 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 ors on the table
18990 2e 20 20 4f 70 65 6e 20 77 72 69 74 65 20 63 75 . Open write cu
18991 72 73 6f 72 73 20 61 72 65 20 6d 6f 76 65 64 20 rsors are moved
18992 74 6f 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 6f to the.** root o
18993 66 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a f the table..**.
18994 2a 2a 20 49 66 20 70 6e 43 68 61 6e 67 65 20 69 ** If pnChange i
18995 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e s not NULL, then
18996 20 74 61 62 6c 65 20 69 54 61 62 6c 65 20 6d 75 table iTable mu
18997 73 74 20 62 65 20 61 6e 20 69 6e 74 6b 65 79 20 st be an intkey
18998 74 61 62 6c 65 2e 20 54 68 65 0a 2a 2a 20 69 6e table. The.** in
18999 74 65 67 65 72 20 76 61 6c 75 65 20 70 6f 69 6e teger value poin
1899a 74 65 64 20 74 6f 20 62 79 20 70 6e 43 68 61 6e ted to by pnChan
1899b 67 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 ge is incremente
1899c 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 d by the number
1899d 6f 66 0a 2a 2a 20 65 6e 74 72 69 65 73 20 69 6e of.** entries in
1899e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 the table..*/.S
1899f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
189a0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c t sqlite3BtreeCl
189a1 65 61 72 54 61 62 6c 65 28 42 74 72 65 65 20 2a earTable(Btree *
189a2 70 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 p, int iTable, i
189a3 6e 74 20 2a 70 6e 43 68 61 6e 67 65 29 7b 0a 20 nt *pnChange){.
189a4 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 int rc;. BtSha
189a5 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
189a6 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 t;. sqlite3Btre
189a7 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 eEnter(p);. ass
189a8 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d ert( p->inTrans=
189a9 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a =TRANS_WRITE );.
189aa 0a 20 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 . /* Invalidate
189ab 20 61 6c 6c 20 69 6e 63 72 62 6c 6f 62 20 63 75 all incrblob cu
189ac 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 61 rsors open on ta
189ad 62 6c 65 20 69 54 61 62 6c 65 20 28 61 73 73 75 ble iTable (assu
189ae 6d 69 6e 67 20 69 54 61 62 6c 65 0a 20 20 2a 2a ming iTable. **
189af 20 69 73 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 is the root of
189b0 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 20 2d a table b-tree -
189b1 20 69 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 74 if it is not, t
189b2 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c he following cal
189b3 6c 20 69 73 0a 20 20 2a 2a 20 61 20 6e 6f 2d 6f l is. ** a no-o
189b4 70 29 2e 20 20 2a 2f 0a 20 20 69 6e 76 61 6c 69 p). */. invali
189b5 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 dateIncrblobCurs
189b6 6f 72 73 28 70 2c 20 30 2c 20 31 29 3b 0a 0a 20 ors(p, 0, 1);..
189b7 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 rc = saveAllCur
189b8 73 6f 72 73 28 70 42 74 2c 20 28 50 67 6e 6f 29 sors(pBt, (Pgno)
189b9 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 69 66 iTable, 0);. if
189ba 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 ( SQLITE_OK==rc
189bb 29 7b 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 ){. rc = clea
189bc 72 44 61 74 61 62 61 73 65 50 61 67 65 28 70 42 rDatabasePage(pB
189bd 74 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c t, (Pgno)iTable,
189be 20 30 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20 0, pnChange);.
189bf 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 }. sqlite3Btre
189c0 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
189c1 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
189c2 20 45 72 61 73 65 20 61 6c 6c 20 69 6e 66 6f 72 Erase all infor
189c3 6d 61 74 69 6f 6e 20 69 6e 20 61 20 74 61 62 6c mation in a tabl
189c4 65 20 61 6e 64 20 61 64 64 20 74 68 65 20 72 6f e and add the ro
189c5 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 ot of the table
189c6 74 6f 0a 2a 2a 20 74 68 65 20 66 72 65 65 6c 69 to.** the freeli
189c7 73 74 2e 20 20 45 78 63 65 70 74 2c 20 74 68 65 st. Except, the
189c8 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 70 72 69 root of the pri
189c9 6e 63 69 70 6c 65 20 74 61 62 6c 65 20 28 74 68 nciple table (th
189ca 65 20 6f 6e 65 20 6f 6e 0a 2a 2a 20 70 61 67 65 e one on.** page
189cb 20 31 29 20 69 73 20 6e 65 76 65 72 20 61 64 64 1) is never add
189cc 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 ed to the freeli
189cd 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 st..**.** This r
189ce 6f 75 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c outine will fail
189cf 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43 with SQLITE_LOC
189d0 4b 45 44 20 69 66 20 74 68 65 72 65 20 61 72 65 KED if there are
189d1 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 63 75 72 any open.** cur
189d2 73 6f 72 73 20 6f 6e 20 74 68 65 20 74 61 62 6c sors on the tabl
189d3 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 54 4f e..**.** If AUTO
189d4 56 41 43 55 55 4d 20 69 73 20 65 6e 61 62 6c 65 VACUUM is enable
189d5 64 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 61 d and the page a
189d6 74 20 69 54 61 62 6c 65 20 69 73 20 6e 6f 74 20 t iTable is not
189d7 74 68 65 20 6c 61 73 74 0a 2a 2a 20 72 6f 6f 74 the last.** root
189d8 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 page in the dat
189d9 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e abase file, then
189da 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 the last root p
189db 61 67 65 20 0a 2a 2a 20 69 6e 20 74 68 65 20 64 age .** in the d
189dc 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 atabase file is
189dd 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 65 20 73 moved into the s
189de 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 20 6f 63 63 lot formerly occ
189df 75 70 69 65 64 20 62 79 0a 2a 2a 20 69 54 61 62 upied by.** iTab
189e0 6c 65 20 61 6e 64 20 74 68 61 74 20 6c 61 73 74 le and that last
189e1 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 20 6f slot formerly o
189e2 63 63 75 70 69 65 64 20 62 79 20 74 68 65 20 6c ccupied by the l
189e3 61 73 74 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a ast root page.**
189e4 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 is added to the
189e5 20 66 72 65 65 6c 69 73 74 20 69 6e 73 74 65 61 freelist instea
189e6 64 20 6f 66 20 69 54 61 62 6c 65 2e 20 20 49 6e d of iTable. In
189e7 20 74 68 69 73 20 73 61 79 2c 20 61 6c 6c 0a 2a this say, all.*
189e8 2a 20 72 6f 6f 74 20 70 61 67 65 73 20 61 72 65 * root pages are
189e9 20 6b 65 70 74 20 61 74 20 74 68 65 20 62 65 67 kept at the beg
189ea 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 61 inning of the da
189eb 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77 68 69 tabase file, whi
189ec 63 68 0a 2a 2a 20 69 73 20 6e 65 63 65 73 73 61 ch.** is necessa
189ed 72 79 20 66 6f 72 20 41 55 54 4f 56 41 43 55 55 ry for AUTOVACUU
189ee 4d 20 74 6f 20 77 6f 72 6b 20 72 69 67 68 74 2e M to work right.
189ef 20 20 2a 70 69 4d 6f 76 65 64 20 69 73 20 73 65 *piMoved is se
189f0 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 70 61 67 t to the .** pag
189f1 65 20 6e 75 6d 62 65 72 20 74 68 61 74 20 75 73 e number that us
189f2 65 64 20 74 6f 20 62 65 20 74 68 65 20 6c 61 73 ed to be the las
189f3 74 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 t root page in t
189f4 68 65 20 66 69 6c 65 20 62 65 66 6f 72 65 0a 2a he file before.*
189f5 2a 20 74 68 65 20 6d 6f 76 65 2e 20 20 49 66 20 * the move. If
189f6 6e 6f 20 70 61 67 65 20 67 65 74 73 20 6d 6f 76 no page gets mov
189f7 65 64 2c 20 2a 70 69 4d 6f 76 65 64 20 69 73 20 ed, *piMoved is
189f8 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 54 68 65 set to 0..** The
189f9 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20 last root page
189fa 69 73 20 72 65 63 6f 72 64 65 64 20 69 6e 20 6d is recorded in m
189fb 65 74 61 5b 33 5d 20 61 6e 64 20 74 68 65 20 76 eta[3] and the v
189fc 61 6c 75 65 20 6f 66 0a 2a 2a 20 6d 65 74 61 5b alue of.** meta[
189fd 33 5d 20 69 73 20 75 70 64 61 74 65 64 20 62 79 3] is updated by
189fe 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 2e this procedure.
189ff 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 .*/.static int b
18a00 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 treeDropTable(Bt
18a01 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 ree *p, Pgno iTa
18a02 62 6c 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76 65 ble, int *piMove
18a03 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 d){. int rc;.
18a04 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d MemPage *pPage =
18a05 20 30 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 0;. BtShared *
18a06 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 pBt = p->pBt;..
18a07 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
18a08 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
18a09 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 p) );. assert(
18a0a 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
18a0b 53 5f 57 52 49 54 45 20 29 3b 0a 0a 20 20 2f 2a S_WRITE );.. /*
18a0c 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 It is illegal t
18a0d 6f 20 64 72 6f 70 20 61 20 74 61 62 6c 65 20 69 o drop a table i
18a0e 66 20 61 6e 79 20 63 75 72 73 6f 72 73 20 61 72 f any cursors ar
18a0f 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65 0a 20 20 e open on the.
18a10 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54 68 69 ** database. Thi
18a11 73 20 69 73 20 62 65 63 61 75 73 65 20 69 6e 20 s is because in
18a12 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 auto-vacuum mode
18a13 20 74 68 65 20 62 61 63 6b 65 6e 64 20 6d 61 79 the backend may
18a14 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20 6d 6f . ** need to mo
18a15 76 65 20 61 6e 6f 74 68 65 72 20 72 6f 6f 74 2d ve another root-
18a16 70 61 67 65 20 74 6f 20 66 69 6c 6c 20 61 20 67 page to fill a g
18a17 61 70 20 6c 65 66 74 20 62 79 20 74 68 65 20 64 ap left by the d
18a18 65 6c 65 74 65 64 0a 20 20 2a 2a 20 72 6f 6f 74 eleted. ** root
18a19 20 70 61 67 65 2e 20 49 66 20 61 6e 20 6f 70 65 page. If an ope
18a1a 6e 20 63 75 72 73 6f 72 20 77 61 73 20 75 73 69 n cursor was usi
18a1b 6e 67 20 74 68 69 73 20 70 61 67 65 20 61 20 70 ng this page a p
18a1c 72 6f 62 6c 65 6d 20 77 6f 75 6c 64 20 0a 20 20 roblem would .
18a1d 2a 2a 20 6f 63 63 75 72 2e 0a 20 20 2a 2a 0a 20 ** occur.. **.
18a1e 20 2a 2a 20 54 68 69 73 20 65 72 72 6f 72 20 69 ** This error i
18a1f 73 20 63 61 75 67 68 74 20 6c 6f 6e 67 20 62 65 s caught long be
18a20 66 6f 72 65 20 63 6f 6e 74 72 6f 6c 20 72 65 61 fore control rea
18a21 63 68 65 73 20 74 68 69 73 20 70 6f 69 6e 74 2e ches this point.
18a22 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 . */. if( NEVE
18a23 52 28 70 42 74 2d 3e 70 43 75 72 73 6f 72 29 20 R(pBt->pCursor)
18a24 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f ){. sqlite3Co
18a25 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 nnectionBlocked(
18a26 70 2d 3e 64 62 2c 20 70 42 74 2d 3e 70 43 75 72 p->db, pBt->pCur
18a27 73 6f 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29 sor->pBtree->db)
18a28 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
18a29 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 ITE_LOCKED_SHARE
18a2a 44 43 41 43 48 45 3b 0a 20 20 7d 0a 0a 20 20 72 DCACHE;. }.. r
18a2b 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
18a2c 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61 62 (pBt, (Pgno)iTab
18a2d 6c 65 2c 20 26 70 50 61 67 65 2c 20 30 29 3b 0a le, &pPage, 0);.
18a2e 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
18a2f 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c n rc;. rc = sql
18a30 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 ite3BtreeClearTa
18a31 62 6c 65 28 70 2c 20 69 54 61 62 6c 65 2c 20 30 ble(p, iTable, 0
18a32 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 );. if( rc ){.
18a33 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
18a34 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 Page);. retur
18a35 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69 n rc;. }.. *pi
18a36 4d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 20 69 66 Moved = 0;.. if
18a37 28 20 69 54 61 62 6c 65 3e 31 20 29 7b 0a 23 69 ( iTable>1 ){.#i
18a38 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
18a39 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
18a3a 66 72 65 65 50 61 67 65 28 70 50 61 67 65 2c 20 freePage(pPage,
18a3b 26 72 63 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 &rc);. releas
18a3c 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 23 65 ePage(pPage);.#e
18a3d 6c 73 65 0a 20 20 20 20 69 66 28 20 70 42 74 2d lse. if( pBt-
18a3e 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 >autoVacuum ){.
18a3f 20 20 20 20 20 50 67 6e 6f 20 6d 61 78 52 6f 6f Pgno maxRoo
18a40 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20 73 71 6c tPgno;. sql
18a41 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 ite3BtreeGetMeta
18a42 28 70 2c 20 42 54 52 45 45 5f 4c 41 52 47 45 53 (p, BTREE_LARGES
18a43 54 5f 52 4f 4f 54 5f 50 41 47 45 2c 20 26 6d 61 T_ROOT_PAGE, &ma
18a44 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 0a 20 20 20 xRootPgno);..
18a45 20 20 20 69 66 28 20 69 54 61 62 6c 65 3d 3d 6d if( iTable==m
18a46 61 78 52 6f 6f 74 50 67 6e 6f 20 29 7b 0a 20 20 axRootPgno ){.
18a47 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
18a48 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 table being drop
18a49 70 65 64 20 69 73 20 74 68 65 20 74 61 62 6c 65 ped is the table
18a4a 20 77 69 74 68 20 74 68 65 20 6c 61 72 67 65 73 with the larges
18a4b 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 t root-page.
18a4c 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69 6e ** number in
18a4d 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 70 the database, p
18a4e 75 74 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 ut the root page
18a4f 20 6f 6e 20 74 68 65 20 66 72 65 65 20 6c 69 73 on the free lis
18a50 74 2e 20 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 t. . */.
18a51 20 20 20 20 20 20 20 66 72 65 65 50 61 67 65 28 freePage(
18a52 70 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 pPage, &rc);.
18a53 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
18a54 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 (pPage);.
18a55 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
18a56 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
18a57 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
18a58 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
18a59 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 {. /* The
18a5a 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f table being dro
18a5b 70 70 65 64 20 64 6f 65 73 20 6e 6f 74 20 68 61 pped does not ha
18a5c 76 65 20 74 68 65 20 6c 61 72 67 65 73 74 20 72 ve the largest r
18a5d 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 20 20 20 oot-page.
18a5e 20 2a 2a 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 ** number in th
18a5f 65 20 64 61 74 61 62 61 73 65 2e 20 53 6f 20 6d e database. So m
18a60 6f 76 65 20 74 68 65 20 70 61 67 65 20 74 68 61 ove the page tha
18a61 74 20 64 6f 65 73 20 69 6e 74 6f 20 74 68 65 20 t does into the
18a62 0a 20 20 20 20 20 20 20 20 2a 2a 20 67 61 70 20 . ** gap
18a63 6c 65 66 74 20 62 79 20 74 68 65 20 64 65 6c 65 left by the dele
18a64 74 65 64 20 72 6f 6f 74 2d 70 61 67 65 2e 0a 20 ted root-page..
18a65 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
18a66 20 20 4d 65 6d 50 61 67 65 20 2a 70 4d 6f 76 65 MemPage *pMove
18a67 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 ;. releas
18a68 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
18a69 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 rc = btree
18a6a 47 65 74 50 61 67 65 28 70 42 74 2c 20 6d 61 78 GetPage(pBt, max
18a6b 52 6f 6f 74 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 RootPgno, &pMove
18a6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 , 0);. if
18a6d 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
18a6e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 ){. ret
18a6f 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 urn rc;.
18a70 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 }. rc = r
18a71 65 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c elocatePage(pBt,
18a72 20 70 4d 6f 76 65 2c 20 50 54 52 4d 41 50 5f 52 pMove, PTRMAP_R
18a73 4f 4f 54 50 41 47 45 2c 20 30 2c 20 69 54 61 62 OOTPAGE, 0, iTab
18a74 6c 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 le, 0);.
18a75 72 65 6c 65 61 73 65 50 61 67 65 28 70 4d 6f 76 releasePage(pMov
18a76 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 e);. if(
18a77 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
18a78 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
18a79 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a n rc;. }.
18a7a 20 20 20 20 20 20 20 20 70 4d 6f 76 65 20 3d 20 pMove =
18a7b 30 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 0;. rc =
18a7c 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 btreeGetPage(pBt
18a7d 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 , maxRootPgno, &
18a7e 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 pMove, 0);.
18a7f 20 20 20 66 72 65 65 50 61 67 65 28 70 4d 6f 76 freePage(pMov
18a80 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 e, &rc);.
18a81 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4d 6f releasePage(pMo
18a82 76 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ve);. if(
18a83 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
18a84 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 {. retu
18a85 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d rn rc;. }
18a86 0a 20 20 20 20 20 20 20 20 2a 70 69 4d 6f 76 65 . *piMove
18a87 64 20 3d 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b d = maxRootPgno;
18a88 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
18a89 2f 2a 20 53 65 74 20 74 68 65 20 6e 65 77 20 27 /* Set the new '
18a8a 6d 61 78 2d 72 6f 6f 74 2d 70 61 67 65 27 20 76 max-root-page' v
18a8b 61 6c 75 65 20 69 6e 20 74 68 65 20 64 61 74 61 alue in the data
18a8c 62 61 73 65 20 68 65 61 64 65 72 2e 20 54 68 69 base header. Thi
18a8d 73 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 74 68 s. ** is th
18a8e 65 20 6f 6c 64 20 76 61 6c 75 65 20 6c 65 73 73 e old value less
18a8f 20 6f 6e 65 2c 20 6c 65 73 73 20 6f 6e 65 20 6d one, less one m
18a90 6f 72 65 20 69 66 20 74 68 61 74 20 68 61 70 70 ore if that happ
18a91 65 6e 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 ens to. **
18a92 62 65 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 6e be a root-page n
18a93 75 6d 62 65 72 2c 20 6c 65 73 73 20 6f 6e 65 20 umber, less one
18a94 61 67 61 69 6e 20 69 66 20 74 68 61 74 20 69 73 again if that is
18a95 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 50 45 the. ** PE
18a96 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 2e NDING_BYTE_PAGE.
18a97 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
18a98 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 maxRootPgno--;.
18a99 20 20 20 20 20 77 68 69 6c 65 28 20 6d 61 78 52 while( maxR
18a9a 6f 6f 74 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 ootPgno==PENDING
18a9b 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 0a _BYTE_PAGE(pBt).
18a9c 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
18a9d 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 PTRMAP_ISPAGE(pB
18a9e 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 20 t, maxRootPgno)
18a9f 29 7b 0a 20 20 20 20 20 20 20 20 6d 61 78 52 6f ){. maxRo
18aa0 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 otPgno--;.
18aa1 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
18aa2 6d 61 78 52 6f 6f 74 50 67 6e 6f 21 3d 50 45 4e maxRootPgno!=PEN
18aa3 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
18aa4 42 74 29 20 29 3b 0a 0a 20 20 20 20 20 20 72 63 Bt) );.. rc
18aa5 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 = sqlite3BtreeU
18aa6 70 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 pdateMeta(p, 4,
18aa7 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20 20 maxRootPgno);.
18aa8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 }else{. f
18aa9 72 65 65 50 61 67 65 28 70 50 61 67 65 2c 20 26 reePage(pPage, &
18aaa 72 63 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 rc);. relea
18aab 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 sePage(pPage);.
18aac 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 65 }.#endif. }e
18aad 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 73 lse{. /* If s
18aae 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 qlite3BtreeDropT
18aaf 61 62 6c 65 20 77 61 73 20 63 61 6c 6c 65 64 20 able was called
18ab0 6f 6e 20 70 61 67 65 20 31 2e 0a 20 20 20 20 2a on page 1.. *
18ab1 2a 20 54 68 69 73 20 72 65 61 6c 6c 79 20 6e 65 * This really ne
18ab2 76 65 72 20 73 68 6f 75 6c 64 20 68 61 70 70 65 ver should happe
18ab3 6e 20 65 78 63 65 70 74 20 69 6e 20 61 20 63 6f n except in a co
18ab4 72 72 75 70 74 0a 20 20 20 20 2a 2a 20 64 61 74 rrupt. ** dat
18ab5 61 62 61 73 65 2e 20 0a 20 20 20 20 2a 2f 0a 20 abase. . */.
18ab6 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67 zeroPage(pPag
18ab7 65 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 e, PTF_INTKEY|PT
18ab8 46 5f 4c 45 41 46 20 29 3b 0a 20 20 20 20 72 65 F_LEAF );. re
18ab9 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 leasePage(pPage)
18aba 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
18abb 63 3b 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 c; .}.SQLITE_PR
18abc 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
18abd 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 3BtreeDropTable(
18abe 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 Btree *p, int iT
18abf 61 62 6c 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76 able, int *piMov
18ac0 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ed){. int rc;.
18ac1 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
18ac2 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 62 74 er(p);. rc = bt
18ac3 72 65 65 44 72 6f 70 54 61 62 6c 65 28 70 2c 20 reeDropTable(p,
18ac4 69 54 61 62 6c 65 2c 20 70 69 4d 6f 76 65 64 29 iTable, piMoved)
18ac5 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
18ac6 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
18ac7 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a rn rc;.}.../*.**
18ac8 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d This function m
18ac9 61 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 ay only be calle
18aca 64 20 69 66 20 74 68 65 20 62 2d 74 72 65 65 20 d if the b-tree
18acb 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6c 72 65 61 connection alrea
18acc 64 79 0a 2a 2a 20 68 61 73 20 61 20 72 65 61 64 dy.** has a read
18acd 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 or write transa
18ace 63 74 69 6f 6e 20 6f 70 65 6e 20 6f 6e 20 74 68 ction open on th
18acf 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a e database..**.*
18ad0 2a 20 52 65 61 64 20 74 68 65 20 6d 65 74 61 2d * Read the meta-
18ad1 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 information out
18ad2 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 66 69 of a database fi
18ad3 6c 65 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 le. Meta[0].**
18ad4 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
18ad5 20 66 72 65 65 20 70 61 67 65 73 20 63 75 72 72 free pages curr
18ad6 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 64 61 74 ently in the dat
18ad7 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 31 5d 0a abase. Meta[1].
18ad8 2a 2a 20 74 68 72 6f 75 67 68 20 6d 65 74 61 5b ** through meta[
18ad9 31 35 5d 20 61 72 65 20 61 76 61 69 6c 61 62 6c 15] are availabl
18ada 65 20 66 6f 72 20 75 73 65 20 62 79 20 68 69 67 e for use by hig
18adb 68 65 72 20 6c 61 79 65 72 73 2e 20 20 4d 65 74 her layers. Met
18adc 61 5b 30 5d 0a 2a 2a 20 69 73 20 72 65 61 64 2d a[0].** is read-
18add 6f 6e 6c 79 2c 20 74 68 65 20 6f 74 68 65 72 73 only, the others
18ade 20 61 72 65 20 72 65 61 64 2f 77 72 69 74 65 2e are read/write.
18adf 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 73 63 68 65 .** .** The sche
18ae0 6d 61 20 6c 61 79 65 72 20 6e 75 6d 62 65 72 73 ma layer numbers
18ae1 20 6d 65 74 61 20 76 61 6c 75 65 73 20 64 69 66 meta values dif
18ae2 66 65 72 65 6e 74 6c 79 2e 20 20 41 74 20 74 68 ferently. At th
18ae3 65 20 73 63 68 65 6d 61 0a 2a 2a 20 6c 61 79 65 e schema.** laye
18ae4 72 20 28 61 6e 64 20 74 68 65 20 53 65 74 43 6f r (and the SetCo
18ae5 6f 6b 69 65 20 61 6e 64 20 52 65 61 64 43 6f 6f okie and ReadCoo
18ae6 6b 69 65 20 6f 70 63 6f 64 65 73 29 20 74 68 65 kie opcodes) the
18ae7 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 66 72 number of.** fr
18ae8 65 65 20 70 61 67 65 73 20 69 73 20 6e 6f 74 20 ee pages is not
18ae9 76 69 73 69 62 6c 65 2e 20 20 53 6f 20 43 6f 6f visible. So Coo
18aea 6b 69 65 5b 30 5d 20 69 73 20 74 68 65 20 73 61 kie[0] is the sa
18aeb 6d 65 20 61 73 20 4d 65 74 61 5b 31 5d 2e 0a 2a me as Meta[1]..*
18aec 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18aed 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
18aee 65 65 47 65 74 4d 65 74 61 28 42 74 72 65 65 20 eeGetMeta(Btree
18aef 2a 70 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 *p, int idx, u32
18af0 20 2a 70 4d 65 74 61 29 7b 0a 20 20 42 74 53 68 *pMeta){. BtSh
18af1 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
18af2 42 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 Bt;.. sqlite3Bt
18af3 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 reeEnter(p);. a
18af4 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e ssert( p->inTran
18af5 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a s>TRANS_NONE );.
18af6 20 20 61 73 73 65 72 74 28 20 53 51 4c 49 54 45 assert( SQLITE
18af7 5f 4f 4b 3d 3d 71 75 65 72 79 53 68 61 72 65 64 _OK==queryShared
18af8 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 CacheTableLock(p
18af9 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 52 , MASTER_ROOT, R
18afa 45 41 44 5f 4c 4f 43 4b 29 20 29 3b 0a 20 20 61 EAD_LOCK) );. a
18afb 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 ssert( pBt->pPag
18afc 65 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 e1 );. assert(
18afd 69 64 78 3e 3d 30 20 26 26 20 69 64 78 3c 3d 31 idx>=0 && idx<=1
18afe 35 20 29 3b 0a 0a 20 20 2a 70 4d 65 74 61 20 3d 5 );.. *pMeta =
18aff 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e get4byte(&pBt->
18b00 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 pPage1->aData[36
18b01 20 2b 20 69 64 78 2a 34 5d 29 3b 0a 0a 20 20 2f + idx*4]);.. /
18b02 2a 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d * If auto-vacuum
18b03 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e 20 is disabled in
18b04 74 68 69 73 20 62 75 69 6c 64 20 61 6e 64 20 74 this build and t
18b05 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 his is an auto-v
18b06 61 63 75 75 6d 0a 20 20 2a 2a 20 64 61 74 61 62 acuum. ** datab
18b07 61 73 65 2c 20 6d 61 72 6b 20 74 68 65 20 64 61 ase, mark the da
18b08 74 61 62 61 73 65 20 61 73 20 72 65 61 64 2d 6f tabase as read-o
18b09 6e 6c 79 2e 20 20 2a 2f 0a 23 69 66 64 65 66 20 nly. */.#ifdef
18b0a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
18b0b 56 41 43 55 55 4d 0a 20 20 69 66 28 20 69 64 78 VACUUM. if( idx
18b0c 3d 3d 42 54 52 45 45 5f 4c 41 52 47 45 53 54 5f ==BTREE_LARGEST_
18b0d 52 4f 4f 54 5f 50 41 47 45 20 26 26 20 2a 70 4d ROOT_PAGE && *pM
18b0e 65 74 61 3e 30 20 29 20 70 42 74 2d 3e 72 65 61 eta>0 ) pBt->rea
18b0f 64 4f 6e 6c 79 20 3d 20 31 3b 0a 23 65 6e 64 69 dOnly = 1;.#endi
18b10 66 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 f.. sqlite3Btre
18b11 65 4c 65 61 76 65 28 70 29 3b 0a 7d 0a 0a 2f 2a eLeave(p);.}../*
18b12 0a 2a 2a 20 57 72 69 74 65 20 6d 65 74 61 2d 69 .** Write meta-i
18b13 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 61 63 6b 20 nformation back
18b14 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 into the databas
18b15 65 2e 20 20 4d 65 74 61 5b 30 5d 20 69 73 0a 2a e. Meta[0] is.*
18b16 2a 20 72 65 61 64 2d 6f 6e 6c 79 20 61 6e 64 20 * read-only and
18b17 6d 61 79 20 6e 6f 74 20 62 65 20 77 72 69 74 74 may not be writt
18b18 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 en..*/.SQLITE_PR
18b19 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
18b1a 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 3BtreeUpdateMeta
18b1b 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 (Btree *p, int i
18b1c 64 78 2c 20 75 33 32 20 69 4d 65 74 61 29 7b 0a dx, u32 iMeta){.
18b1d 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
18b1e 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 75 6e 73 69 = p->pBt;. unsi
18b1f 67 6e 65 64 20 63 68 61 72 20 2a 70 50 31 3b 0a gned char *pP1;.
18b20 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 int rc;. asse
18b21 72 74 28 20 69 64 78 3e 3d 31 20 26 26 20 69 64 rt( idx>=1 && id
18b22 78 3c 3d 31 35 20 29 3b 0a 20 20 73 71 6c 69 74 x<=15 );. sqlit
18b23 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
18b24 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e . assert( p->in
18b25 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 Trans==TRANS_WRI
18b26 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 TE );. assert(
18b27 70 42 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 29 pBt->pPage1!=0 )
18b28 3b 0a 20 20 70 50 31 20 3d 20 70 42 74 2d 3e 70 ;. pP1 = pBt->p
18b29 50 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a 20 20 Page1->aData;.
18b2a 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
18b2b 72 57 72 69 74 65 28 70 42 74 2d 3e 70 50 61 67 rWrite(pBt->pPag
18b2c 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 e1->pDbPage);.
18b2d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
18b2e 4b 20 29 7b 0a 20 20 20 20 70 75 74 34 62 79 74 K ){. put4byt
18b2f 65 28 26 70 50 31 5b 33 36 20 2b 20 69 64 78 2a e(&pP1[36 + idx*
18b30 34 5d 2c 20 69 4d 65 74 61 29 3b 0a 23 69 66 6e 4], iMeta);.#ifn
18b31 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
18b32 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 AUTOVACUUM. i
18b33 66 28 20 69 64 78 3d 3d 42 54 52 45 45 5f 49 4e f( idx==BTREE_IN
18b34 43 52 5f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 CR_VACUUM ){.
18b35 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e assert( pBt->
18b36 61 75 74 6f 56 61 63 75 75 6d 20 7c 7c 20 69 4d autoVacuum || iM
18b37 65 74 61 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 eta==0 );.
18b38 61 73 73 65 72 74 28 20 69 4d 65 74 61 3d 3d 30 assert( iMeta==0
18b39 20 7c 7c 20 69 4d 65 74 61 3d 3d 31 20 29 3b 0a || iMeta==1 );.
18b3a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 pBt->incrV
18b3b 61 63 75 75 6d 20 3d 20 28 75 38 29 69 4d 65 74 acuum = (u8)iMet
18b3c 61 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a a;. }.#endif.
18b3d 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
18b3e 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
18b3f 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e turn rc;.}..#ifn
18b40 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
18b41 42 54 52 45 45 43 4f 55 4e 54 0a 2f 2a 0a 2a 2a BTREECOUNT./*.**
18b42 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d The first argum
18b43 65 6e 74 2c 20 70 43 75 72 2c 20 69 73 20 61 20 ent, pCur, is a
18b44 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 6f 6e cursor opened on
18b45 20 73 6f 6d 65 20 62 2d 74 72 65 65 2e 20 43 6f some b-tree. Co
18b46 75 6e 74 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 unt the.** numbe
18b47 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 r of entries in
18b48 74 68 65 20 62 2d 74 72 65 65 20 61 6e 64 20 77 the b-tree and w
18b49 72 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 20 rite the result
18b4a 74 6f 20 2a 70 6e 45 6e 74 72 79 2e 0a 2a 2a 0a to *pnEntry..**.
18b4b 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 ** SQLITE_OK is
18b4c 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 returned if the
18b4d 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 73 75 63 operation is suc
18b4e 63 65 73 73 66 75 6c 6c 79 20 65 78 65 63 75 74 cessfully execut
18b4f 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ed. .** Otherwis
18b50 65 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 69 e, if an error i
18b51 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 28 69 s encountered (i
18b52 2e 65 2e 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 .e. an IO error
18b53 6f 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 or database.** c
18b54 6f 72 72 75 70 74 69 6f 6e 29 20 61 6e 20 53 51 orruption) an SQ
18b55 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 Lite error code
18b56 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a is returned..*/.
18b57 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
18b58 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
18b59 6f 75 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 70 ount(BtCursor *p
18b5a 43 75 72 2c 20 69 36 34 20 2a 70 6e 45 6e 74 72 Cur, i64 *pnEntr
18b5b 79 29 7b 0a 20 20 69 36 34 20 6e 45 6e 74 72 79 y){. i64 nEntry
18b5c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
18b5d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 /* Va
18b5e 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 69 6e lue to return in
18b5f 20 2a 70 6e 45 6e 74 72 79 20 2a 2f 0a 20 20 69 *pnEntry */. i
18b60 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
18b61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18b62 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
18b63 64 65 20 2a 2f 0a 20 20 72 63 20 3d 20 6d 6f 76 de */. rc = mov
18b64 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 0a eToRoot(pCur);..
18b65 20 20 2f 2a 20 55 6e 6c 65 73 73 20 61 6e 20 65 /* Unless an e
18b66 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 rror occurs, the
18b67 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c 6f 6f 70 20 following loop
18b68 72 75 6e 73 20 6f 6e 65 20 69 74 65 72 61 74 69 runs one iterati
18b69 6f 6e 20 66 6f 72 20 65 61 63 68 0a 20 20 2a 2a on for each. **
18b6a 20 70 61 67 65 20 69 6e 20 74 68 65 20 42 2d 54 page in the B-T
18b6b 72 65 65 20 73 74 72 75 63 74 75 72 65 20 28 6e ree structure (n
18b6c 6f 74 20 69 6e 63 6c 75 64 69 6e 67 20 6f 76 65 ot including ove
18b6d 72 66 6c 6f 77 20 70 61 67 65 73 29 2e 20 0a 20 rflow pages). .
18b6e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 72 63 3d */. while( rc=
18b6f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
18b70 20 20 69 6e 74 20 69 49 64 78 3b 20 20 20 20 20 int iIdx;
18b71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18b72 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
18b73 20 63 68 69 6c 64 20 6e 6f 64 65 20 69 6e 20 70 child node in p
18b74 61 72 65 6e 74 20 2a 2f 0a 20 20 20 20 4d 65 6d arent */. Mem
18b75 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 Page *pPage;
18b76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18b77 2f 2a 20 43 75 72 72 65 6e 74 20 70 61 67 65 20 /* Current page
18b78 6f 66 20 74 68 65 20 62 2d 74 72 65 65 20 2a 2f of the b-tree */
18b79 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 .. /* If this
18b7a 20 69 73 20 61 20 6c 65 61 66 20 70 61 67 65 20 is a leaf page
18b7b 6f 72 20 74 68 65 20 74 72 65 65 20 69 73 20 6e or the tree is n
18b7c 6f 74 20 61 6e 20 69 6e 74 2d 6b 65 79 20 74 72 ot an int-key tr
18b7d 65 65 2c 20 74 68 65 6e 20 0a 20 20 20 20 2a 2a ee, then . **
18b7e 20 74 68 69 73 20 70 61 67 65 20 63 6f 6e 74 61 this page conta
18b7f 69 6e 73 20 63 6f 75 6e 74 61 62 6c 65 20 65 6e ins countable en
18b80 74 72 69 65 73 2e 20 49 6e 63 72 65 6d 65 6e 74 tries. Increment
18b81 20 74 68 65 20 65 6e 74 72 79 20 63 6f 75 6e 74 the entry count
18b82 65 72 0a 20 20 20 20 2a 2a 20 61 63 63 6f 72 64 er. ** accord
18b83 69 6e 67 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 ingly.. */.
18b84 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e pPage = pCur->
18b85 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
18b86 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 70 50 61 ge];. if( pPa
18b87 67 65 2d 3e 6c 65 61 66 20 7c 7c 20 21 70 50 61 ge->leaf || !pPa
18b88 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 ge->intKey ){.
18b89 20 20 20 20 6e 45 6e 74 72 79 20 2b 3d 20 70 50 nEntry += pP
18b8a 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 age->nCell;.
18b8b 7d 0a 0a 20 20 20 20 2f 2a 20 70 50 61 67 65 20 }.. /* pPage
18b8c 69 73 20 61 20 6c 65 61 66 20 6e 6f 64 65 2e 20 is a leaf node.
18b8d 54 68 69 73 20 6c 6f 6f 70 20 6e 61 76 69 67 61 This loop naviga
18b8e 74 65 73 20 74 68 65 20 63 75 72 73 6f 72 20 73 tes the cursor s
18b8f 6f 20 74 68 61 74 20 69 74 20 0a 20 20 20 20 2a o that it . *
18b90 2a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 * points to the
18b91 66 69 72 73 74 20 69 6e 74 65 72 69 6f 72 20 63 first interior c
18b92 65 6c 6c 20 74 68 61 74 20 69 74 20 70 6f 69 6e ell that it poin
18b93 74 73 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 ts to the parent
18b94 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 6e of. ** the n
18b95 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 ext page in the
18b96 74 72 65 65 20 74 68 61 74 20 68 61 73 20 6e 6f tree that has no
18b97 74 20 79 65 74 20 62 65 65 6e 20 76 69 73 69 74 t yet been visit
18b98 65 64 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 70 ed. The. ** p
18b99 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
18b9a 3e 69 50 61 67 65 5d 20 76 61 6c 75 65 20 69 73 >iPage] value is
18b9b 20 73 65 74 20 74 6f 20 74 68 65 20 69 6e 64 65 set to the inde
18b9c 78 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20 x of the parent
18b9d 63 65 6c 6c 0a 20 20 20 20 2a 2a 20 6f 66 20 74 cell. ** of t
18b9e 68 65 20 70 61 67 65 2c 20 6f 72 20 74 6f 20 74 he page, or to t
18b9f 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c he number of cel
18ba0 6c 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 69 ls in the page i
18ba1 66 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 0a f the next page.
18ba2 20 20 20 20 2a 2a 20 74 6f 20 76 69 73 69 74 20 ** to visit
18ba3 69 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 is the right-chi
18ba4 6c 64 20 6f 66 20 69 74 73 20 70 61 72 65 6e 74 ld of its parent
18ba5 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
18ba6 49 66 20 61 6c 6c 20 70 61 67 65 73 20 69 6e 20 If all pages in
18ba7 74 68 65 20 74 72 65 65 20 68 61 76 65 20 62 65 the tree have be
18ba8 65 6e 20 76 69 73 69 74 65 64 2c 20 72 65 74 75 en visited, retu
18ba9 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 74 6f 20 rn SQLITE_OK to
18baa 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 65 the. ** calle
18bab 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 r.. */. if
18bac 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b ( pPage->leaf ){
18bad 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 . do {.
18bae 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 if( pCur->iP
18baf 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 age==0 ){.
18bb0 20 20 20 20 2f 2a 20 41 6c 6c 20 70 61 67 65 73 /* All pages
18bb1 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 20 68 of the b-tree h
18bb2 61 76 65 20 62 65 65 6e 20 76 69 73 69 74 65 64 ave been visited
18bb3 2e 20 52 65 74 75 72 6e 20 73 75 63 63 65 73 73 . Return success
18bb4 66 75 6c 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 fully. */.
18bb5 20 20 20 20 2a 70 6e 45 6e 74 72 79 20 3d 20 6e *pnEntry = n
18bb6 45 6e 74 72 79 3b 0a 20 20 20 20 20 20 20 20 20 Entry;.
18bb7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
18bb8 4b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 K;. }.
18bb9 20 20 20 20 20 6d 6f 76 65 54 6f 50 61 72 65 6e moveToParen
18bba 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 7d t(pCur);. }
18bbb 77 68 69 6c 65 20 28 20 70 43 75 72 2d 3e 61 69 while ( pCur->ai
18bbc 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
18bbd 3e 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 >=pCur->apPage[p
18bbe 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 Cur->iPage]->nCe
18bbf 6c 6c 20 29 3b 0a 0a 20 20 20 20 20 20 70 43 75 ll );.. pCu
18bc0 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
18bc1 50 61 67 65 5d 2b 2b 3b 0a 20 20 20 20 20 20 70 Page]++;. p
18bc2 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 Page = pCur->apP
18bc3 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
18bc4 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
18bc5 44 65 73 63 65 6e 64 20 74 6f 20 74 68 65 20 63 Descend to the c
18bc6 68 69 6c 64 20 6e 6f 64 65 20 6f 66 20 74 68 65 hild node of the
18bc7 20 63 65 6c 6c 20 74 68 61 74 20 74 68 65 20 63 cell that the c
18bc8 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20 ursor currently
18bc9 0a 20 20 20 20 2a 2a 20 70 6f 69 6e 74 73 20 61 . ** points a
18bca 74 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 72 t. This is the r
18bcb 69 67 68 74 2d 63 68 69 6c 64 20 69 66 20 28 69 ight-child if (i
18bcc 49 64 78 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c Idx==pPage->nCel
18bcd 6c 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 l).. */. i
18bce 49 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 Idx = pCur->aiId
18bcf 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a x[pCur->iPage];.
18bd0 20 20 20 20 69 66 28 20 69 49 64 78 3d 3d 70 50 if( iIdx==pP
18bd1 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 age->nCell ){.
18bd2 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 rc = moveToC
18bd3 68 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62 hild(pCur, get4b
18bd4 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 yte(&pPage->aDat
18bd5 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 a[pPage->hdrOffs
18bd6 65 74 2b 38 5d 29 29 3b 0a 20 20 20 20 7d 65 6c et+8]));. }el
18bd7 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d se{. rc = m
18bd8 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c oveToChild(pCur,
18bd9 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 get4byte(findCe
18bda 6c 6c 28 70 50 61 67 65 2c 20 69 49 64 78 29 29 ll(pPage, iIdx))
18bdb 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
18bdc 2f 2a 20 41 6e 20 65 72 72 6f 72 20 68 61 73 20 /* An error has
18bdd 6f 63 63 75 72 72 65 64 2e 20 52 65 74 75 72 6e occurred. Return
18bde 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 an error code.
18bdf 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a */. return rc;.
18be0 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
18be1 52 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 72 Return the pager
18be2 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
18be3 20 61 20 42 54 72 65 65 2e 20 20 54 68 69 73 20 a BTree. This
18be4 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
18be5 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 for.** testing a
18be6 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c nd debugging onl
18be7 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
18be8 56 41 54 45 20 50 61 67 65 72 20 2a 73 71 6c 69 VATE Pager *sqli
18be9 74 65 33 42 74 72 65 65 50 61 67 65 72 28 42 74 te3BtreePager(Bt
18bea 72 65 65 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 ree *p){. retur
18beb 6e 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 n p->pBt->pPager
18bec 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ;.}..#ifndef SQL
18bed 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 ITE_OMIT_INTEGRI
18bee 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 41 TY_CHECK./*.** A
18bef 70 70 65 6e 64 20 61 20 6d 65 73 73 61 67 65 20 ppend a message
18bf0 74 6f 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 to the error mes
18bf1 73 61 67 65 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a sage string..*/.
18bf2 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 static void chec
18bf3 6b 41 70 70 65 6e 64 4d 73 67 28 0a 20 20 49 6e kAppendMsg(. In
18bf4 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63 tegrityCk *pChec
18bf5 6b 2c 0a 20 20 63 68 61 72 20 2a 7a 4d 73 67 31 k,. char *zMsg1
18bf6 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ,. const char *
18bf7 7a 46 6f 72 6d 61 74 2c 0a 20 20 2e 2e 2e 0a 29 zFormat,. ....)
18bf8 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a {. va_list ap;.
18bf9 20 20 69 66 28 20 21 70 43 68 65 63 6b 2d 3e 6d if( !pCheck->m
18bfa 78 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 20 xErr ) return;.
18bfb 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 2d 2d pCheck->mxErr--
18bfc 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6e 45 72 72 ;. pCheck->nErr
18bfd 2b 2b 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 ++;. va_start(a
18bfe 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 69 p, zFormat);. i
18bff 66 28 20 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 f( pCheck->errMs
18c00 67 2e 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 73 g.nChar ){. s
18c01 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 qlite3StrAccumAp
18c02 70 65 6e 64 28 26 70 43 68 65 63 6b 2d 3e 65 72 pend(&pCheck->er
18c03 72 4d 73 67 2c 20 22 5c 6e 22 2c 20 31 29 3b 0a rMsg, "\n", 1);.
18c04 20 20 7d 0a 20 20 69 66 28 20 7a 4d 73 67 31 20 }. if( zMsg1
18c05 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 ){. sqlite3St
18c06 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26 70 43 rAccumAppend(&pC
18c07 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 7a 4d heck->errMsg, zM
18c08 73 67 31 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20 20 sg1, -1);. }.
18c09 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 sqlite3VXPrintf(
18c0a 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c &pCheck->errMsg,
18c0b 20 31 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 1, zFormat, ap)
18c0c 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a ;. va_end(ap);.
18c0d 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 65 72 if( pCheck->er
18c0e 72 4d 73 67 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 rMsg.mallocFaile
18c0f 64 20 29 7b 0a 20 20 20 20 70 43 68 65 63 6b 2d d ){. pCheck-
18c10 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
18c11 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 1;. }.}.#endif
18c12 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 /* SQLITE_OMIT_I
18c13 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a NTEGRITY_CHECK *
18c14 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
18c15 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 E_OMIT_INTEGRITY
18c16 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 41 64 64 _CHECK./*.** Add
18c17 20 31 20 74 6f 20 74 68 65 20 72 65 66 65 72 65 1 to the refere
18c18 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20 70 61 nce count for pa
18c19 67 65 20 69 50 61 67 65 2e 20 20 49 66 20 74 68 ge iPage. If th
18c1a 69 73 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 is is the second
18c1b 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 20 74 6f .** reference to
18c1c 20 74 68 65 20 70 61 67 65 2c 20 61 64 64 20 61 the page, add a
18c1d 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 n error message
18c1e 74 6f 20 70 43 68 65 63 6b 2d 3e 7a 45 72 72 4d to pCheck->zErrM
18c1f 73 67 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 sg..** Return 1
18c20 69 66 20 74 68 65 72 65 20 61 72 65 20 32 20 6f if there are 2 o
18c21 72 65 20 6d 6f 72 65 20 72 65 66 65 72 65 6e 63 re more referenc
18c22 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 61 es to the page a
18c23 6e 64 20 30 20 69 66 0a 2a 2a 20 69 66 20 74 68 nd 0 if.** if th
18c24 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20 is is the first
18c25 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 reference to the
18c26 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 page..**.** Als
18c27 6f 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 o check that the
18c28 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 page number is
18c29 69 6e 20 62 6f 75 6e 64 73 2e 0a 2a 2f 0a 73 74 in bounds..*/.st
18c2a 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 52 65 atic int checkRe
18c2b 66 28 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 f(IntegrityCk *p
18c2c 43 68 65 63 6b 2c 20 50 67 6e 6f 20 69 50 61 67 Check, Pgno iPag
18c2d 65 2c 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 e, char *zContex
18c2e 74 29 7b 0a 20 20 69 66 28 20 69 50 61 67 65 3d t){. if( iPage=
18c2f 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 =0 ) return 1;.
18c30 20 69 66 28 20 69 50 61 67 65 3e 70 43 68 65 63 if( iPage>pChec
18c31 6b 2d 3e 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 k->nPage ){.
18c32 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 checkAppendMsg(p
18c33 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c Check, zContext,
18c34 20 22 69 6e 76 61 6c 69 64 20 70 61 67 65 20 6e "invalid page n
18c35 75 6d 62 65 72 20 25 64 22 2c 20 69 50 61 67 65 umber %d", iPage
18c36 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b );. return 1;
18c37 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 68 65 63 . }. if( pChec
18c38 6b 2d 3e 61 6e 52 65 66 5b 69 50 61 67 65 5d 3d k->anRef[iPage]=
18c39 3d 31 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 =1 ){. checkA
18c3a 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c ppendMsg(pCheck,
18c3b 20 7a 43 6f 6e 74 65 78 74 2c 20 22 32 6e 64 20 zContext, "2nd
18c3c 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 reference to pag
18c3d 65 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 e %d", iPage);.
18c3e 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d return 1;. }
18c3f 0a 20 20 72 65 74 75 72 6e 20 20 28 70 43 68 65 . return (pChe
18c40 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 61 67 65 5d ck->anRef[iPage]
18c41 2b 2b 29 3e 31 3b 0a 7d 0a 0a 23 69 66 6e 64 65 ++)>1;.}..#ifnde
18c42 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
18c43 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 TOVACUUM./*.** C
18c44 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 65 6e heck that the en
18c45 74 72 79 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 try in the point
18c46 65 72 2d 6d 61 70 20 66 6f 72 20 70 61 67 65 20 er-map for page
18c47 69 43 68 69 6c 64 20 6d 61 70 73 20 74 6f 20 0a iChild maps to .
18c48 2a 2a 20 70 61 67 65 20 69 50 61 72 65 6e 74 2c ** page iParent,
18c49 20 70 6f 69 6e 74 65 72 20 74 79 70 65 20 70 74 pointer type pt
18c4a 72 54 79 70 65 2e 20 49 66 20 6e 6f 74 2c 20 61 rType. If not, a
18c4b 70 70 65 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d ppend an error m
18c4c 65 73 73 61 67 65 0a 2a 2a 20 74 6f 20 70 43 68 essage.** to pCh
18c4d 65 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 eck..*/.static v
18c4e 6f 69 64 20 63 68 65 63 6b 50 74 72 6d 61 70 28 oid checkPtrmap(
18c4f 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a . IntegrityCk *
18c50 70 43 68 65 63 6b 2c 20 20 20 2f 2a 20 49 6e 74 pCheck, /* Int
18c51 65 67 72 69 74 79 20 63 68 65 63 6b 20 63 6f 6e egrity check con
18c52 74 65 78 74 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 text */. Pgno i
18c53 43 68 69 6c 64 2c 20 20 20 20 20 20 20 20 20 20 Child,
18c54 20 2f 2a 20 43 68 69 6c 64 20 70 61 67 65 20 6e /* Child page n
18c55 75 6d 62 65 72 20 2a 2f 0a 20 20 75 38 20 65 54 umber */. u8 eT
18c56 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ype,
18c57 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20 70 6f /* Expected po
18c58 69 6e 74 65 72 20 6d 61 70 20 74 79 70 65 20 2a inter map type *
18c59 2f 0a 20 20 50 67 6e 6f 20 69 50 61 72 65 6e 74 /. Pgno iParent
18c5a 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 , /* Ex
18c5b 70 65 63 74 65 64 20 70 6f 69 6e 74 65 72 20 6d pected pointer m
18c5c 61 70 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e ap parent page n
18c5d 75 6d 62 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 umber */. char
18c5e 2a 7a 43 6f 6e 74 65 78 74 20 20 20 20 20 20 20 *zContext
18c5f 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 64 65 73 /* Context des
18c60 63 72 69 70 74 69 6f 6e 20 28 75 73 65 64 20 66 cription (used f
18c61 6f 72 20 65 72 72 6f 72 20 6d 73 67 29 20 2a 2f or error msg) */
18c62 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 .){. int rc;.
18c63 75 38 20 65 50 74 72 6d 61 70 54 79 70 65 3b 0a u8 ePtrmapType;.
18c64 20 20 50 67 6e 6f 20 69 50 74 72 6d 61 70 50 61 Pgno iPtrmapPa
18c65 72 65 6e 74 3b 0a 0a 20 20 72 63 20 3d 20 70 74 rent;.. rc = pt
18c66 72 6d 61 70 47 65 74 28 70 43 68 65 63 6b 2d 3e rmapGet(pCheck->
18c67 70 42 74 2c 20 69 43 68 69 6c 64 2c 20 26 65 50 pBt, iChild, &eP
18c68 74 72 6d 61 70 54 79 70 65 2c 20 26 69 50 74 72 trmapType, &iPtr
18c69 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 69 66 mapParent);. if
18c6a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
18c6b 29 7b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 ){. if( rc==S
18c6c 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 QLITE_NOMEM || r
18c6d 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f c==SQLITE_IOERR_
18c6e 4e 4f 4d 45 4d 20 29 20 70 43 68 65 63 6b 2d 3e NOMEM ) pCheck->
18c6f 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
18c70 3b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e ;. checkAppen
18c71 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f dMsg(pCheck, zCo
18c72 6e 74 65 78 74 2c 20 22 46 61 69 6c 65 64 20 74 ntext, "Failed t
18c73 6f 20 72 65 61 64 20 70 74 72 6d 61 70 20 6b 65 o read ptrmap ke
18c74 79 3d 25 64 22 2c 20 69 43 68 69 6c 64 29 3b 0a y=%d", iChild);.
18c75 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
18c76 0a 20 20 69 66 28 20 65 50 74 72 6d 61 70 54 79 . if( ePtrmapTy
18c77 70 65 21 3d 65 54 79 70 65 20 7c 7c 20 69 50 74 pe!=eType || iPt
18c78 72 6d 61 70 50 61 72 65 6e 74 21 3d 69 50 61 72 rmapParent!=iPar
18c79 65 6e 74 20 29 7b 0a 20 20 20 20 63 68 65 63 6b ent ){. check
18c7a 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b AppendMsg(pCheck
18c7b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20 , zContext, .
18c7c 20 20 20 22 42 61 64 20 70 74 72 20 6d 61 70 20 "Bad ptr map
18c7d 65 6e 74 72 79 20 6b 65 79 3d 25 64 20 65 78 70 entry key=%d exp
18c7e 65 63 74 65 64 3d 28 25 64 2c 25 64 29 20 67 6f ected=(%d,%d) go
18c7f 74 3d 28 25 64 2c 25 64 29 22 2c 20 0a 20 20 20 t=(%d,%d)", .
18c80 20 20 20 69 43 68 69 6c 64 2c 20 65 54 79 70 65 iChild, eType
18c81 2c 20 69 50 61 72 65 6e 74 2c 20 65 50 74 72 6d , iParent, ePtrm
18c82 61 70 54 79 70 65 2c 20 69 50 74 72 6d 61 70 50 apType, iPtrmapP
18c83 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 23 65 arent);. }.}.#e
18c84 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 ndif../*.** Chec
18c85 6b 20 74 68 65 20 69 6e 74 65 67 72 69 74 79 20 k the integrity
18c86 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 of the freelist
18c87 6f 72 20 6f 66 20 61 6e 20 6f 76 65 72 66 6c 6f or of an overflo
18c88 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2a 20 w page list..**
18c89 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 Verify that the
18c8a 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
18c8b 6f 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20 4e on the list is N
18c8c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
18c8d 20 63 68 65 63 6b 4c 69 73 74 28 0a 20 20 49 6e checkList(. In
18c8e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63 tegrityCk *pChec
18c8f 6b 2c 20 20 2f 2a 20 49 6e 74 65 67 72 69 74 79 k, /* Integrity
18c90 20 63 68 65 63 6b 69 6e 67 20 63 6f 6e 74 65 78 checking contex
18c91 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 46 72 65 t */. int isFre
18c92 65 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a 20 eList, /*
18c93 54 72 75 65 20 66 6f 72 20 61 20 66 72 65 65 6c True for a freel
18c94 69 73 74 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 ist. False for
18c95 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 overflow page li
18c96 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67 st */. int iPag
18c97 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e, /*
18c98 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 Page number for
18c99 20 66 69 72 73 74 20 70 61 67 65 20 69 6e 20 74 first page in t
18c9a 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 he list */. int
18c9b 20 4e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 N,
18c9c 20 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20 6e /* Expected n
18c9d 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
18c9e 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 n the list */.
18c9f 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20 char *zContext
18ca0 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 /* Context
18ca1 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 for error messa
18ca2 67 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ges */.){. int
18ca3 69 3b 0a 20 20 69 6e 74 20 65 78 70 65 63 74 65 i;. int expecte
18ca4 64 20 3d 20 4e 3b 0a 20 20 69 6e 74 20 69 46 69 d = N;. int iFi
18ca5 72 73 74 20 3d 20 69 50 61 67 65 3b 0a 20 20 77 rst = iPage;. w
18ca6 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 30 20 26 26 hile( N-- > 0 &&
18ca7 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 20 29 pCheck->mxErr )
18ca8 7b 0a 20 20 20 20 44 62 50 61 67 65 20 2a 70 4f {. DbPage *pO
18ca9 76 66 6c 50 61 67 65 3b 0a 20 20 20 20 75 6e 73 vflPage;. uns
18caa 69 67 6e 65 64 20 63 68 61 72 20 2a 70 4f 76 66 igned char *pOvf
18cab 6c 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20 69 lData;. if( i
18cac 50 61 67 65 3c 31 20 29 7b 0a 20 20 20 20 20 20 Page<1 ){.
18cad 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 checkAppendMsg(p
18cae 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c Check, zContext,
18caf 0a 20 20 20 20 20 20 20 20 20 22 25 64 20 6f 66 . "%d of
18cb0 20 25 64 20 70 61 67 65 73 20 6d 69 73 73 69 6e %d pages missin
18cb1 67 20 66 72 6f 6d 20 6f 76 65 72 66 6c 6f 77 20 g from overflow
18cb2 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20 61 74 list starting at
18cb3 20 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 %d",.
18cb4 4e 2b 31 2c 20 65 78 70 65 63 74 65 64 2c 20 69 N+1, expected, i
18cb5 46 69 72 73 74 29 3b 0a 20 20 20 20 20 20 62 72 First);. br
18cb6 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 eak;. }. i
18cb7 66 28 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 f( checkRef(pChe
18cb8 63 6b 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 ck, iPage, zCont
18cb9 65 78 74 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 ext) ) break;.
18cba 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 if( sqlite3Pag
18cbb 65 72 47 65 74 28 70 43 68 65 63 6b 2d 3e 70 50 erGet(pCheck->pP
18cbc 61 67 65 72 2c 20 28 50 67 6e 6f 29 69 50 61 67 ager, (Pgno)iPag
18cbd 65 2c 20 26 70 4f 76 66 6c 50 61 67 65 29 20 29 e, &pOvflPage) )
18cbe 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 {. checkApp
18cbf 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a endMsg(pCheck, z
18cc0 43 6f 6e 74 65 78 74 2c 20 22 66 61 69 6c 65 64 Context, "failed
18cc1 20 74 6f 20 67 65 74 20 70 61 67 65 20 25 64 22 to get page %d"
18cc2 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 , iPage);.
18cc3 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
18cc4 20 70 4f 76 66 6c 44 61 74 61 20 3d 20 28 75 6e pOvflData = (un
18cc5 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 73 71 signed char *)sq
18cc6 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 lite3PagerGetDat
18cc7 61 28 70 4f 76 66 6c 50 61 67 65 29 3b 0a 20 20 a(pOvflPage);.
18cc8 20 20 69 66 28 20 69 73 46 72 65 65 4c 69 73 74 if( isFreeList
18cc9 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 ){. int n
18cca 3d 20 67 65 74 34 62 79 74 65 28 26 70 4f 76 66 = get4byte(&pOvf
18ccb 6c 44 61 74 61 5b 34 5d 29 3b 0a 23 69 66 6e 64 lData[4]);.#ifnd
18ccc 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
18ccd 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 UTOVACUUM.
18cce 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d if( pCheck->pBt-
18ccf 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 >autoVacuum ){.
18cd0 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d checkPtrm
18cd1 61 70 28 70 43 68 65 63 6b 2c 20 69 50 61 67 65 ap(pCheck, iPage
18cd2 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 , PTRMAP_FREEPAG
18cd3 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 78 74 29 3b E, 0, zContext);
18cd4 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
18cd5 20 20 20 20 20 20 69 66 28 20 6e 3e 70 43 68 65 if( n>pChe
18cd6 63 6b 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 ck->pBt->usableS
18cd7 69 7a 65 2f 34 2d 32 20 29 7b 0a 20 20 20 20 20 ize/4-2 ){.
18cd8 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 checkAppendMs
18cd9 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 g(pCheck, zConte
18cda 78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 22 xt,. "
18cdb 66 72 65 65 6c 69 73 74 20 6c 65 61 66 20 63 6f freelist leaf co
18cdc 75 6e 74 20 74 6f 6f 20 62 69 67 20 6f 6e 20 70 unt too big on p
18cdd 61 67 65 20 25 64 22 2c 20 69 50 61 67 65 29 3b age %d", iPage);
18cde 0a 20 20 20 20 20 20 20 20 4e 2d 2d 3b 0a 20 20 . N--;.
18cdf 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
18ce0 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b for(i=0; i<n;
18ce1 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 i++){.
18ce2 20 50 67 6e 6f 20 69 46 72 65 65 50 61 67 65 20 Pgno iFreePage
18ce3 3d 20 67 65 74 34 62 79 74 65 28 26 70 4f 76 66 = get4byte(&pOvf
18ce4 6c 44 61 74 61 5b 38 2b 69 2a 34 5d 29 3b 0a 23 lData[8+i*4]);.#
18ce5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
18ce6 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
18ce7 20 20 20 20 20 20 20 20 69 66 28 20 70 43 68 65 if( pChe
18ce8 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 ck->pBt->autoVac
18ce9 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 uum ){.
18cea 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 checkPtrmap(p
18ceb 43 68 65 63 6b 2c 20 69 46 72 65 65 50 61 67 65 Check, iFreePage
18cec 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 , PTRMAP_FREEPAG
18ced 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 78 74 29 3b E, 0, zContext);
18cee 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 23 65 6e . }.#en
18cef 64 69 66 0a 20 20 20 20 20 20 20 20 20 20 63 68 dif. ch
18cf0 65 63 6b 52 65 66 28 70 43 68 65 63 6b 2c 20 69 eckRef(pCheck, i
18cf1 46 72 65 65 50 61 67 65 2c 20 7a 43 6f 6e 74 65 FreePage, zConte
18cf2 78 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 xt);. }.
18cf3 20 20 20 20 20 20 20 4e 20 2d 3d 20 6e 3b 0a 20 N -= n;.
18cf4 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 }. }.#if
18cf5 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
18cf6 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
18cf7 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 else{. /* I
18cf8 66 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 f this database
18cf9 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 supports auto-va
18cfa 63 75 75 6d 20 61 6e 64 20 69 50 61 67 65 20 69 cuum and iPage i
18cfb 73 20 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a 20 s not the last.
18cfc 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 ** page in
18cfd 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 6c 69 this overflow li
18cfe 73 74 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74 st, check that t
18cff 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 he pointer-map e
18d00 6e 74 72 79 20 66 6f 72 0a 20 20 20 20 20 20 2a ntry for. *
18d01 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * the following
18d02 70 61 67 65 20 6d 61 74 63 68 65 73 20 69 50 61 page matches iPa
18d03 67 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ge.. */.
18d04 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 if( pCheck->p
18d05 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 Bt->autoVacuum &
18d06 26 20 4e 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 & N>0 ){.
18d07 20 69 20 3d 20 67 65 74 34 62 79 74 65 28 70 4f i = get4byte(pO
18d08 76 66 6c 44 61 74 61 29 3b 0a 20 20 20 20 20 20 vflData);.
18d09 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 checkPtrmap(pC
18d0a 68 65 63 6b 2c 20 69 2c 20 50 54 52 4d 41 50 5f heck, i, PTRMAP_
18d0b 4f 56 45 52 46 4c 4f 57 32 2c 20 69 50 61 67 65 OVERFLOW2, iPage
18d0c 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 , zContext);.
18d0d 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 }. }.#endi
18d0e 66 0a 20 20 20 20 69 50 61 67 65 20 3d 20 67 65 f. iPage = ge
18d0f 74 34 62 79 74 65 28 70 4f 76 66 6c 44 61 74 61 t4byte(pOvflData
18d10 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 );. sqlite3Pa
18d11 67 65 72 55 6e 72 65 66 28 70 4f 76 66 6c 50 61 gerUnref(pOvflPa
18d12 67 65 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 ge);. }.}.#endi
18d13 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
18d14 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b _INTEGRITY_CHECK
18d15 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
18d16 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 ITE_OMIT_INTEGRI
18d17 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 44 TY_CHECK./*.** D
18d18 6f 20 76 61 72 69 6f 75 73 20 73 61 6e 69 74 79 o various sanity
18d19 20 63 68 65 63 6b 73 20 6f 6e 20 61 20 73 69 6e checks on a sin
18d1a 67 6c 65 20 70 61 67 65 20 6f 66 20 61 20 74 72 gle page of a tr
18d1b 65 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 ee. Return.** t
18d1c 68 65 20 74 72 65 65 20 64 65 70 74 68 2e 20 20 he tree depth.
18d1d 52 6f 6f 74 20 70 61 67 65 73 20 72 65 74 75 72 Root pages retur
18d1e 6e 20 30 2e 20 20 50 61 72 65 6e 74 73 20 6f 66 n 0. Parents of
18d1f 20 72 6f 6f 74 20 70 61 67 65 73 0a 2a 2a 20 72 root pages.** r
18d20 65 74 75 72 6e 20 31 2c 20 61 6e 64 20 73 6f 20 eturn 1, and so
18d21 66 6f 72 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 forth..** .** Th
18d22 65 73 65 20 63 68 65 63 6b 73 20 61 72 65 20 64 ese checks are d
18d23 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 one:.**.**
18d24 31 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 1. Make sure th
18d25 61 74 20 63 65 6c 6c 73 20 61 6e 64 20 66 72 65 at cells and fre
18d26 65 62 6c 6f 63 6b 73 20 64 6f 20 6e 6f 74 20 6f eblocks do not o
18d27 76 65 72 6c 61 70 0a 2a 2a 20 20 20 20 20 20 20 verlap.**
18d28 20 20 20 62 75 74 20 63 6f 6d 62 69 6e 65 20 74 but combine t
18d29 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 20 63 6f 76 o completely cov
18d2a 65 72 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 20 er the page..**
18d2b 20 4e 4f 20 20 32 2e 20 20 4d 61 6b 65 20 73 75 NO 2. Make su
18d2c 72 65 20 63 65 6c 6c 20 6b 65 79 73 20 61 72 65 re cell keys are
18d2d 20 69 6e 20 6f 72 64 65 72 2e 0a 2a 2a 20 20 4e in order..** N
18d2e 4f 20 20 33 2e 20 20 4d 61 6b 65 20 73 75 72 65 O 3. Make sure
18d2f 20 6e 6f 20 6b 65 79 20 69 73 20 6c 65 73 73 20 no key is less
18d30 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f than or equal to
18d31 20 7a 4c 6f 77 65 72 42 6f 75 6e 64 2e 0a 2a 2a zLowerBound..**
18d32 20 20 4e 4f 20 20 34 2e 20 20 4d 61 6b 65 20 73 NO 4. Make s
18d33 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 67 72 ure no key is gr
18d34 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 eater than or eq
18d35 75 61 6c 20 74 6f 20 7a 55 70 70 65 72 42 6f 75 ual to zUpperBou
18d36 6e 64 2e 0a 2a 2a 20 20 20 20 20 20 35 2e 20 20 nd..** 5.
18d37 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 Check the integr
18d38 69 74 79 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 ity of overflow
18d39 70 61 67 65 73 2e 0a 2a 2a 20 20 20 20 20 20 36 pages..** 6
18d3a 2e 20 20 52 65 63 75 72 73 69 76 65 6c 79 20 63 . Recursively c
18d3b 61 6c 6c 20 63 68 65 63 6b 54 72 65 65 50 61 67 all checkTreePag
18d3c 65 20 6f 6e 20 61 6c 6c 20 63 68 69 6c 64 72 65 e on all childre
18d3d 6e 2e 0a 2a 2a 20 20 20 20 20 20 37 2e 20 20 56 n..** 7. V
18d3e 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 64 erify that the d
18d3f 65 70 74 68 20 6f 66 20 61 6c 6c 20 63 68 69 6c epth of all chil
18d40 64 72 65 6e 20 69 73 20 74 68 65 20 73 61 6d 65 dren is the same
18d41 2e 0a 2a 2a 20 20 20 20 20 20 38 2e 20 20 4d 61 ..** 8. Ma
18d42 6b 65 20 73 75 72 65 20 74 68 69 73 20 70 61 67 ke sure this pag
18d43 65 20 69 73 20 61 74 20 6c 65 61 73 74 20 33 33 e is at least 33
18d44 25 20 66 75 6c 6c 20 6f 72 20 65 6c 73 65 20 69 % full or else i
18d45 74 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 t is.**
18d46 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 the root of the
18d47 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 tree..*/.static
18d48 20 69 6e 74 20 63 68 65 63 6b 54 72 65 65 50 61 int checkTreePa
18d49 67 65 28 0a 20 20 49 6e 74 65 67 72 69 74 79 43 ge(. IntegrityC
18d4a 6b 20 2a 70 43 68 65 63 6b 2c 20 20 2f 2a 20 43 k *pCheck, /* C
18d4b 6f 6e 74 65 78 74 20 66 6f 72 20 74 68 65 20 73 ontext for the s
18d4c 61 6e 69 74 79 20 63 68 65 63 6b 20 2a 2f 0a 20 anity check */.
18d4d 20 69 6e 74 20 69 50 61 67 65 2c 20 20 20 20 20 int iPage,
18d4e 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e /* Page n
18d4f 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 umber of the pag
18d50 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 e to check */.
18d51 63 68 61 72 20 2a 7a 50 61 72 65 6e 74 43 6f 6e char *zParentCon
18d52 74 65 78 74 20 20 2f 2a 20 50 61 72 65 6e 74 20 text /* Parent
18d53 63 6f 6e 74 65 78 74 20 2a 2f 0a 29 7b 0a 20 20 context */.){.
18d54 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a MemPage *pPage;.
18d55 20 20 69 6e 74 20 69 2c 20 72 63 2c 20 64 65 70 int i, rc, dep
18d56 74 68 2c 20 64 32 2c 20 70 67 6e 6f 2c 20 63 6e th, d2, pgno, cn
18d57 74 3b 0a 20 20 69 6e 74 20 68 64 72 2c 20 63 65 t;. int hdr, ce
18d58 6c 6c 53 74 61 72 74 3b 0a 20 20 69 6e 74 20 6e llStart;. int n
18d59 43 65 6c 6c 3b 0a 20 20 75 38 20 2a 64 61 74 61 Cell;. u8 *data
18d5a 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
18d5b 74 3b 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 t;. int usableS
18d5c 69 7a 65 3b 0a 20 20 63 68 61 72 20 7a 43 6f 6e ize;. char zCon
18d5d 74 65 78 74 5b 31 30 30 5d 3b 0a 20 20 63 68 61 text[100];. cha
18d5e 72 20 2a 68 69 74 20 3d 20 30 3b 0a 0a 20 20 73 r *hit = 0;.. s
18d5f 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
18d60 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 sizeof(zContext)
18d61 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 50 61 67 , zContext, "Pag
18d62 65 20 25 64 3a 20 22 2c 20 69 50 61 67 65 29 3b e %d: ", iPage);
18d63 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 .. /* Check tha
18d64 74 20 74 68 65 20 70 61 67 65 20 65 78 69 73 74 t the page exist
18d65 73 0a 20 20 2a 2f 0a 20 20 70 42 74 20 3d 20 70 s. */. pBt = p
18d66 43 68 65 63 6b 2d 3e 70 42 74 3b 0a 20 20 75 73 Check->pBt;. us
18d67 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e ableSize = pBt->
18d68 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 69 66 usableSize;. if
18d69 28 20 69 50 61 67 65 3d 3d 30 20 29 20 72 65 74 ( iPage==0 ) ret
18d6a 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 63 68 65 urn 0;. if( che
18d6b 63 6b 52 65 66 28 70 43 68 65 63 6b 2c 20 69 50 ckRef(pCheck, iP
18d6c 61 67 65 2c 20 7a 50 61 72 65 6e 74 43 6f 6e 74 age, zParentCont
18d6d 65 78 74 29 20 29 20 72 65 74 75 72 6e 20 30 3b ext) ) return 0;
18d6e 0a 20 20 69 66 28 20 28 72 63 20 3d 20 62 74 72 . if( (rc = btr
18d6f 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 28 eeGetPage(pBt, (
18d70 50 67 6e 6f 29 69 50 61 67 65 2c 20 26 70 50 61 Pgno)iPage, &pPa
18d71 67 65 2c 20 30 29 29 21 3d 30 20 29 7b 0a 20 20 ge, 0))!=0 ){.
18d72 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 checkAppendMsg
18d73 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 (pCheck, zContex
18d74 74 2c 0a 20 20 20 20 20 20 20 22 75 6e 61 62 6c t,. "unabl
18d75 65 20 74 6f 20 67 65 74 20 74 68 65 20 70 61 67 e to get the pag
18d76 65 2e 20 65 72 72 6f 72 20 63 6f 64 65 3d 25 64 e. error code=%d
18d77 22 2c 20 72 63 29 3b 0a 20 20 20 20 72 65 74 75 ", rc);. retu
18d78 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 rn 0;. }.. /*
18d79 43 6c 65 61 72 20 4d 65 6d 50 61 67 65 2e 69 73 Clear MemPage.is
18d7a 49 6e 69 74 20 74 6f 20 6d 61 6b 65 20 73 75 72 Init to make sur
18d7b 65 20 74 68 65 20 63 6f 72 72 75 70 74 69 6f 6e e the corruption
18d7c 20 64 65 74 65 63 74 69 6f 6e 20 63 6f 64 65 20 detection code
18d7d 69 6e 0a 20 20 2a 2a 20 62 74 72 65 65 49 6e 69 in. ** btreeIni
18d7e 74 50 61 67 65 28 29 20 69 73 20 65 78 65 63 75 tPage() is execu
18d7f 74 65 64 2e 20 20 2a 2f 0a 20 20 70 50 61 67 65 ted. */. pPage
18d80 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 ->isInit = 0;.
18d81 69 66 28 20 28 72 63 20 3d 20 62 74 72 65 65 49 if( (rc = btreeI
18d82 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 29 21 nitPage(pPage))!
18d83 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 =0 ){. assert
18d84 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 52 ( rc==SQLITE_COR
18d85 52 55 50 54 20 29 3b 20 20 2f 2a 20 54 68 65 20 RUPT ); /* The
18d86 6f 6e 6c 79 20 70 6f 73 73 69 62 6c 65 20 65 72 only possible er
18d87 72 6f 72 20 66 72 6f 6d 20 49 6e 69 74 50 61 67 ror from InitPag
18d88 65 20 2a 2f 0a 20 20 20 20 63 68 65 63 6b 41 70 e */. checkAp
18d89 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 pendMsg(pCheck,
18d8a 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 zContext, .
18d8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 62 "b
18d8c 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 72 treeInitPage() r
18d8d 65 74 75 72 6e 73 20 65 72 72 6f 72 20 63 6f 64 eturns error cod
18d8e 65 20 25 64 22 2c 20 72 63 29 3b 0a 20 20 20 20 e %d", rc);.
18d8f 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 releasePage(pPag
18d90 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 e);. return 0
18d91 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 ;. }.. /* Chec
18d92 6b 20 6f 75 74 20 61 6c 6c 20 74 68 65 20 63 65 k out all the ce
18d93 6c 6c 73 2e 0a 20 20 2a 2f 0a 20 20 64 65 70 74 lls.. */. dept
18d94 68 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 h = 0;. for(i=0
18d95 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c ; i<pPage->nCell
18d96 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72 && pCheck->mxEr
18d97 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 r; i++){. u8
18d98 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 75 33 32 20 *pCell;. u32
18d99 73 7a 3b 0a 20 20 20 20 43 65 6c 6c 49 6e 66 6f sz;. CellInfo
18d9a 20 69 6e 66 6f 3b 0a 0a 20 20 20 20 2f 2a 20 43 info;.. /* C
18d9b 68 65 63 6b 20 70 61 79 6c 6f 61 64 20 6f 76 65 heck payload ove
18d9c 72 66 6c 6f 77 20 70 61 67 65 73 0a 20 20 20 20 rflow pages.
18d9d 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 */. sqlite3_s
18d9e 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a nprintf(sizeof(z
18d9f 43 6f 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 Context), zConte
18da0 78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 xt,.
18da1 20 22 4f 6e 20 74 72 65 65 20 70 61 67 65 20 25 "On tree page %
18da2 64 20 63 65 6c 6c 20 25 64 3a 20 22 2c 20 69 50 d cell %d: ", iP
18da3 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 70 43 65 age, i);. pCe
18da4 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 ll = findCell(pP
18da5 61 67 65 2c 69 29 3b 0a 20 20 20 20 62 74 72 65 age,i);. btre
18da6 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 eParseCellPtr(pP
18da7 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 age, pCell, &inf
18da8 6f 29 3b 0a 20 20 20 20 73 7a 20 3d 20 69 6e 66 o);. sz = inf
18da9 6f 2e 6e 44 61 74 61 3b 0a 20 20 20 20 69 66 28 o.nData;. if(
18daa 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 !pPage->intKey
18dab 29 20 73 7a 20 2b 3d 20 28 69 6e 74 29 69 6e 66 ) sz += (int)inf
18dac 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 61 73 73 65 o.nKey;. asse
18dad 72 74 28 20 73 7a 3d 3d 69 6e 66 6f 2e 6e 50 61 rt( sz==info.nPa
18dae 79 6c 6f 61 64 20 29 3b 0a 20 20 20 20 69 66 28 yload );. if(
18daf 20 28 73 7a 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c (sz>info.nLocal
18db0 29 20 0a 20 20 20 20 20 26 26 20 28 26 70 43 65 ) . && (&pCe
18db1 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
18db2 77 5d 3c 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 w]<=&pPage->aDat
18db3 61 5b 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a a[pBt->usableSiz
18db4 65 5d 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 e]). ){.
18db5 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 28 73 7a int nPage = (sz
18db6 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b - info.nLocal +
18db7 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 35 29 usableSize - 5)
18db8 2f 28 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 /(usableSize - 4
18db9 29 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67 );. Pgno pg
18dba 6e 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 noOvfl = get4byt
18dbb 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f e(&pCell[info.iO
18dbc 76 65 72 66 6c 6f 77 5d 29 3b 0a 23 69 66 6e 64 verflow]);.#ifnd
18dbd 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
18dbe 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 UTOVACUUM.
18dbf 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
18dc0 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 uum ){. c
18dc1 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 heckPtrmap(pChec
18dc2 6b 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 50 54 52 k, pgnoOvfl, PTR
18dc3 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 69 MAP_OVERFLOW1, i
18dc4 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b Page, zContext);
18dc5 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
18dc6 20 20 20 20 20 20 63 68 65 63 6b 4c 69 73 74 28 checkList(
18dc7 70 43 68 65 63 6b 2c 20 30 2c 20 70 67 6e 6f 4f pCheck, 0, pgnoO
18dc8 76 66 6c 2c 20 6e 50 61 67 65 2c 20 7a 43 6f 6e vfl, nPage, zCon
18dc9 74 65 78 74 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 text);. }..
18dca 20 20 2f 2a 20 43 68 65 63 6b 20 73 61 6e 69 74 /* Check sanit
18dcb 79 20 6f 66 20 6c 65 66 74 20 63 68 69 6c 64 20 y of left child
18dcc 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 page.. */.
18dcd 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
18dce 66 20 29 7b 0a 20 20 20 20 20 20 70 67 6e 6f 20 f ){. pgno
18dcf 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c = get4byte(pCell
18dd0 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
18dd1 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
18dd2 4d 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d M. if( pBt-
18dd3 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 >autoVacuum ){.
18dd4 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d checkPtrm
18dd5 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c ap(pCheck, pgno,
18dd6 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 69 PTRMAP_BTREE, i
18dd7 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b Page, zContext);
18dd8 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
18dd9 20 20 20 20 20 20 64 32 20 3d 20 63 68 65 63 6b d2 = check
18dda 54 72 65 65 50 61 67 65 28 70 43 68 65 63 6b 2c TreePage(pCheck,
18ddb 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 65 78 74 29 pgno, zContext)
18ddc 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3e 30 20 ;. if( i>0
18ddd 26 26 20 64 32 21 3d 64 65 70 74 68 20 29 7b 0a && d2!=depth ){.
18dde 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 checkApp
18ddf 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a endMsg(pCheck, z
18de0 43 6f 6e 74 65 78 74 2c 20 22 43 68 69 6c 64 20 Context, "Child
18de1 70 61 67 65 20 64 65 70 74 68 20 64 69 66 66 65 page depth diffe
18de2 72 73 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rs");. }.
18de3 20 20 20 20 64 65 70 74 68 20 3d 20 64 32 3b 0a depth = d2;.
18de4 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
18de5 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
18de6 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 pgno = get4b
18de7 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 yte(&pPage->aDat
18de8 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 a[pPage->hdrOffs
18de9 65 74 2b 38 5d 29 3b 0a 20 20 20 20 73 71 6c 69 et+8]);. sqli
18dea 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a te3_snprintf(siz
18deb 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a eof(zContext), z
18dec 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 Context, .
18ded 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 "
18dee 4f 6e 20 70 61 67 65 20 25 64 20 61 74 20 72 69 On page %d at ri
18def 67 68 74 20 63 68 69 6c 64 3a 20 22 2c 20 69 50 ght child: ", iP
18df0 61 67 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 age);.#ifndef SQ
18df1 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
18df2 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74 CUUM. if( pBt
18df3 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a ->autoVacuum ){.
18df4 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 checkPtrma
18df5 70 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 p(pCheck, pgno,
18df6 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 69 50 PTRMAP_BTREE, iP
18df7 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23 age, 0);. }.#
18df8 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b 54 endif. checkT
18df9 72 65 65 50 61 67 65 28 70 43 68 65 63 6b 2c 20 reePage(pCheck,
18dfa 70 67 6e 6f 2c 20 7a 43 6f 6e 74 65 78 74 29 3b pgno, zContext);
18dfb 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 43 68 65 63 . }. . /* Chec
18dfc 6b 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 20 63 k for complete c
18dfd 6f 76 65 72 61 67 65 20 6f 66 20 74 68 65 20 70 overage of the p
18dfe 61 67 65 0a 20 20 2a 2f 0a 20 20 64 61 74 61 20 age. */. data
18dff 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a = pPage->aData;.
18e00 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 hdr = pPage->h
18e01 64 72 4f 66 66 73 65 74 3b 0a 20 20 68 69 74 20 drOffset;. hit
18e02 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c = sqlite3PageMal
18e03 6c 6f 63 28 20 70 42 74 2d 3e 70 61 67 65 53 69 loc( pBt->pageSi
18e04 7a 65 20 29 3b 0a 20 20 69 66 28 20 68 69 74 3d ze );. if( hit=
18e05 3d 30 20 29 7b 0a 20 20 20 20 70 43 68 65 63 6b =0 ){. pCheck
18e06 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
18e07 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 1;. }else{.
18e08 20 75 31 36 20 63 6f 6e 74 65 6e 74 4f 66 66 73 u16 contentOffs
18e09 65 74 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 et = get2byte(&d
18e0a 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 ata[hdr+5]);.
18e0b 20 61 73 73 65 72 74 28 20 63 6f 6e 74 65 6e 74 assert( content
18e0c 4f 66 66 73 65 74 3c 3d 75 73 61 62 6c 65 53 69 Offset<=usableSi
18e0d 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 63 ze ); /* Enforc
18e0e 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 ed by btreeInitP
18e0f 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 6d 65 6d age() */. mem
18e10 73 65 74 28 68 69 74 2b 63 6f 6e 74 65 6e 74 4f set(hit+contentO
18e11 66 66 73 65 74 2c 20 30 2c 20 75 73 61 62 6c 65 ffset, 0, usable
18e12 53 69 7a 65 2d 63 6f 6e 74 65 6e 74 4f 66 66 73 Size-contentOffs
18e13 65 74 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 et);. memset(
18e14 68 69 74 2c 20 31 2c 20 63 6f 6e 74 65 6e 74 4f hit, 1, contentO
18e15 66 66 73 65 74 29 3b 0a 20 20 20 20 6e 43 65 6c ffset);. nCel
18e16 6c 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 l = get2byte(&da
18e17 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 20 20 ta[hdr+3]);.
18e18 63 65 6c 6c 53 74 61 72 74 20 3d 20 68 64 72 20 cellStart = hdr
18e19 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e + 12 - 4*pPage->
18e1a 6c 65 61 66 3b 0a 20 20 20 20 66 6f 72 28 69 3d leaf;. for(i=
18e1b 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 0; i<nCell; i++)
18e1c 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 63 20 3d {. int pc =
18e1d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
18e1e 63 65 6c 6c 53 74 61 72 74 2b 69 2a 32 5d 29 3b cellStart+i*2]);
18e1f 0a 20 20 20 20 20 20 75 31 36 20 73 69 7a 65 20 . u16 size
18e20 3d 20 31 30 32 34 3b 0a 20 20 20 20 20 20 69 6e = 1024;. in
18e21 74 20 6a 3b 0a 20 20 20 20 20 20 69 66 28 20 70 t j;. if( p
18e22 63 3c 3d 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 c<=usableSize-4
18e23 29 7b 0a 20 20 20 20 20 20 20 20 73 69 7a 65 20 ){. size
18e24 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 = cellSizePtr(pP
18e25 61 67 65 2c 20 26 64 61 74 61 5b 70 63 5d 29 3b age, &data[pc]);
18e26 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
18e27 66 28 20 28 70 63 2b 73 69 7a 65 2d 31 29 3e 3d f( (pc+size-1)>=
18e28 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 usableSize ){.
18e29 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e checkAppen
18e2a 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 dMsg(pCheck, 0,
18e2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 43 6f . "Co
18e2c 72 72 75 70 74 69 6f 6e 20 64 65 74 65 63 74 65 rruption detecte
18e2d 64 20 69 6e 20 63 65 6c 6c 20 25 64 20 6f 6e 20 d in cell %d on
18e2e 70 61 67 65 20 25 64 22 2c 69 2c 69 50 61 67 65 page %d",i,iPage
18e2f 2c 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 ,0);. }else
18e30 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d {. for(j=
18e31 70 63 2b 73 69 7a 65 2d 31 3b 20 6a 3e 3d 70 63 pc+size-1; j>=pc
18e32 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d 2b 2b 3b ; j--) hit[j]++;
18e33 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
18e34 20 20 20 69 20 3d 20 67 65 74 32 62 79 74 65 28 i = get2byte(
18e35 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 &data[hdr+1]);.
18e36 20 20 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b while( i>0 ){
18e37 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65 2c . int size,
18e38 20 6a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 j;. assert
18e39 28 20 69 3c 3d 75 73 61 62 6c 65 53 69 7a 65 2d ( i<=usableSize-
18e3a 34 20 29 3b 20 20 20 20 20 2f 2a 20 45 6e 66 6f 4 ); /* Enfo
18e3b 72 63 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 rced by btreeIni
18e3c 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 tPage() */.
18e3d 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 size = get2byte
18e3e 28 26 64 61 74 61 5b 69 2b 32 5d 29 3b 0a 20 20 (&data[i+2]);.
18e3f 20 20 20 20 61 73 73 65 72 74 28 20 69 2b 73 69 assert( i+si
18e40 7a 65 3c 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 ze<=usableSize )
18e41 3b 20 20 2f 2a 20 45 6e 66 6f 72 63 65 64 20 62 ; /* Enforced b
18e42 79 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 y btreeInitPage(
18e43 29 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 6a ) */. for(j
18e44 3d 69 2b 73 69 7a 65 2d 31 3b 20 6a 3e 3d 69 3b =i+size-1; j>=i;
18e45 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d 2b 2b 3b 0a j--) hit[j]++;.
18e46 20 20 20 20 20 20 6a 20 3d 20 67 65 74 32 62 79 j = get2by
18e47 74 65 28 26 64 61 74 61 5b 69 5d 29 3b 0a 20 20 te(&data[i]);.
18e48 20 20 20 20 61 73 73 65 72 74 28 20 6a 3d 3d 30 assert( j==0
18e49 20 7c 7c 20 6a 3e 69 2b 73 69 7a 65 20 29 3b 20 || j>i+size );
18e4a 20 2f 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 20 /* Enforced by
18e4b 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 btreeInitPage()
18e4c 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 */. assert(
18e4d 20 6a 3c 3d 75 73 61 62 6c 65 53 69 7a 65 2d 34 j<=usableSize-4
18e4e 20 29 3b 20 20 20 2f 2a 20 45 6e 66 6f 72 63 65 ); /* Enforce
18e4f 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 61 d by btreeInitPa
18e50 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 69 20 ge() */. i
18e51 3d 20 6a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 = j;. }. f
18e52 6f 72 28 69 3d 63 6e 74 3d 30 3b 20 69 3c 75 73 or(i=cnt=0; i<us
18e53 61 62 6c 65 53 69 7a 65 3b 20 69 2b 2b 29 7b 0a ableSize; i++){.
18e54 20 20 20 20 20 20 69 66 28 20 68 69 74 5b 69 5d if( hit[i]
18e55 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 ==0 ){. c
18e56 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 nt++;. }els
18e57 65 20 69 66 28 20 68 69 74 5b 69 5d 3e 31 20 29 e if( hit[i]>1 )
18e58 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 {. checkA
18e59 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c ppendMsg(pCheck,
18e5a 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 22 4d 0,. "M
18e5b 75 6c 74 69 70 6c 65 20 75 73 65 73 20 66 6f 72 ultiple uses for
18e5c 20 62 79 74 65 20 25 64 20 6f 66 20 70 61 67 65 byte %d of page
18e5d 20 25 64 22 2c 20 69 2c 20 69 50 61 67 65 29 3b %d", i, iPage);
18e5e 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
18e5f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
18e60 20 20 69 66 28 20 63 6e 74 21 3d 64 61 74 61 5b if( cnt!=data[
18e61 68 64 72 2b 37 5d 20 29 7b 0a 20 20 20 20 20 20 hdr+7] ){.
18e62 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 checkAppendMsg(p
18e63 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 Check, 0, .
18e64 20 20 20 20 20 22 46 72 61 67 6d 65 6e 74 61 74 "Fragmentat
18e65 69 6f 6e 20 6f 66 20 25 64 20 62 79 74 65 73 20 ion of %d bytes
18e66 72 65 70 6f 72 74 65 64 20 61 73 20 25 64 20 6f reported as %d o
18e67 6e 20 70 61 67 65 20 25 64 22 2c 0a 20 20 20 20 n page %d",.
18e68 20 20 20 20 20 20 63 6e 74 2c 20 64 61 74 61 5b cnt, data[
18e69 68 64 72 2b 37 5d 2c 20 69 50 61 67 65 29 3b 0a hdr+7], iPage);.
18e6a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 }. }. sqli
18e6b 74 65 33 50 61 67 65 46 72 65 65 28 68 69 74 29 te3PageFree(hit)
18e6c 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 ;. releasePage(
18e6d 70 50 61 67 65 29 3b 0a 20 20 72 65 74 75 72 6e pPage);. return
18e6e 20 64 65 70 74 68 2b 31 3b 0a 7d 0a 23 65 6e 64 depth+1;.}.#end
18e6f 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
18e70 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 T_INTEGRITY_CHEC
18e71 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 K */..#ifndef SQ
18e72 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 LITE_OMIT_INTEGR
18e73 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 ITY_CHECK./*.**
18e74 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 This routine doe
18e75 73 20 61 20 63 6f 6d 70 6c 65 74 65 20 63 68 65 s a complete che
18e76 63 6b 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20 ck of the given
18e77 42 54 72 65 65 20 66 69 6c 65 2e 20 20 61 52 6f BTree file. aRo
18e78 6f 74 5b 5d 20 69 73 0a 2a 2a 20 61 6e 20 61 72 ot[] is.** an ar
18e79 72 61 79 20 6f 66 20 70 61 67 65 73 20 6e 75 6d ray of pages num
18e7a 62 65 72 73 20 77 65 72 65 20 65 61 63 68 20 70 bers were each p
18e7b 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 74 68 age number is th
18e7c 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 0a 2a e root page of.*
18e7d 2a 20 61 20 74 61 62 6c 65 2e 20 20 6e 52 6f 6f * a table. nRoo
18e7e 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 t is the number
18e7f 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 52 of entries in aR
18e80 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 61 oot..**.** A rea
18e81 64 2d 6f 6e 6c 79 20 6f 72 20 72 65 61 64 2d 77 d-only or read-w
18e82 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
18e83 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20 must be opened
18e84 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 0a 2a before calling.*
18e85 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e * this function.
18e86 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 .**.** Write the
18e87 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 number of error
18e88 20 73 65 65 6e 20 69 6e 20 2a 70 6e 45 72 72 2e seen in *pnErr.
18e89 20 20 45 78 63 65 70 74 20 66 6f 72 20 73 6f 6d Except for som
18e8a 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f e memory.** allo
18e8b 63 61 74 69 6f 6e 20 65 72 72 6f 72 73 2c 20 20 cation errors,
18e8c 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
18e8d 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20 held in memory
18e8e 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a obtained from.**
18e8f 20 6d 61 6c 6c 6f 63 20 69 73 20 72 65 74 75 72 malloc is retur
18e90 6e 65 64 20 69 66 20 2a 70 6e 45 72 72 20 69 73 ned if *pnErr is
18e91 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 2a non-zero. If *
18e92 70 6e 45 72 72 3d 3d 30 20 74 68 65 6e 20 4e 55 pnErr==0 then NU
18e93 4c 4c 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 LL is.** returne
18e94 64 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 d. If a memory
18e95 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 allocation error
18e96 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73 occurs, NULL is
18e97 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 returned..*/.SQ
18e98 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
18e99 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 49 r *sqlite3BtreeI
18e9a 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 0a 20 ntegrityCheck(.
18e9b 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 2f Btree *p, /
18e9c 2a 20 54 68 65 20 62 74 72 65 65 20 74 6f 20 62 * The btree to b
18e9d 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a 20 20 69 e checked */. i
18e9e 6e 74 20 2a 61 52 6f 6f 74 2c 20 20 20 2f 2a 20 nt *aRoot, /*
18e9f 41 6e 20 61 72 72 61 79 20 6f 66 20 72 6f 6f 74 An array of root
18ea0 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20 66 pages numbers f
18ea1 6f 72 20 69 6e 64 69 76 69 64 75 61 6c 20 74 72 or individual tr
18ea2 65 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f ees */. int nRo
18ea3 6f 74 2c 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 ot, /* Number
18ea4 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 of entries in a
18ea5 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 Root[] */. int
18ea6 6d 78 45 72 72 2c 20 20 20 20 2f 2a 20 53 74 6f mxErr, /* Sto
18ea7 70 20 72 65 70 6f 72 74 69 6e 67 20 65 72 72 6f p reporting erro
18ea8 72 73 20 61 66 74 65 72 20 74 68 69 73 20 6d 61 rs after this ma
18ea9 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 45 ny */. int *pnE
18eaa 72 72 20 20 20 20 2f 2a 20 57 72 69 74 65 20 6e rr /* Write n
18eab 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 umber of errors
18eac 73 65 65 6e 20 74 6f 20 74 68 69 73 20 76 61 72 seen to this var
18ead 69 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 iable */.){. Pg
18eae 6e 6f 20 69 3b 0a 20 20 69 6e 74 20 6e 52 65 66 no i;. int nRef
18eaf 3b 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 ;. IntegrityCk
18eb0 73 43 68 65 63 6b 3b 0a 20 20 42 74 53 68 61 72 sCheck;. BtShar
18eb1 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
18eb2 3b 0a 20 20 63 68 61 72 20 7a 45 72 72 5b 31 30 ;. char zErr[10
18eb3 30 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 0];.. sqlite3Bt
18eb4 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 reeEnter(p);. a
18eb5 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e ssert( p->inTran
18eb6 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 s>TRANS_NONE &&
18eb7 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 pBt->inTransacti
18eb8 6f 6e 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b on>TRANS_NONE );
18eb9 0a 20 20 6e 52 65 66 20 3d 20 73 71 6c 69 74 65 . nRef = sqlite
18eba 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 3PagerRefcount(p
18ebb 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 73 Bt->pPager);. s
18ebc 43 68 65 63 6b 2e 70 42 74 20 3d 20 70 42 74 3b Check.pBt = pBt;
18ebd 0a 20 20 73 43 68 65 63 6b 2e 70 50 61 67 65 72 . sCheck.pPager
18ebe 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a = pBt->pPager;.
18ebf 20 20 73 43 68 65 63 6b 2e 6e 50 61 67 65 20 3d sCheck.nPage =
18ec0 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 pagerPagecount(
18ec1 73 43 68 65 63 6b 2e 70 42 74 29 3b 0a 20 20 73 sCheck.pBt);. s
18ec2 43 68 65 63 6b 2e 6d 78 45 72 72 20 3d 20 6d 78 Check.mxErr = mx
18ec3 45 72 72 3b 0a 20 20 73 43 68 65 63 6b 2e 6e 45 Err;. sCheck.nE
18ec4 72 72 20 3d 20 30 3b 0a 20 20 73 43 68 65 63 6b rr = 0;. sCheck
18ec5 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 .mallocFailed =
18ec6 30 3b 0a 20 20 2a 70 6e 45 72 72 20 3d 20 30 3b 0;. *pnErr = 0;
18ec7 0a 20 20 69 66 28 20 73 43 68 65 63 6b 2e 6e 50 . if( sCheck.nP
18ec8 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 age==0 ){. sq
18ec9 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
18eca 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 p);. return 0
18ecb 3b 0a 20 20 7d 0a 20 20 73 43 68 65 63 6b 2e 61 ;. }. sCheck.a
18ecc 6e 52 65 66 20 3d 20 73 71 6c 69 74 65 33 4d 61 nRef = sqlite3Ma
18ecd 6c 6c 6f 63 28 20 28 73 43 68 65 63 6b 2e 6e 50 lloc( (sCheck.nP
18ece 61 67 65 2b 31 29 2a 73 69 7a 65 6f 66 28 73 43 age+1)*sizeof(sC
18ecf 68 65 63 6b 2e 61 6e 52 65 66 5b 30 5d 29 20 29 heck.anRef[0]) )
18ed0 3b 0a 20 20 69 66 28 20 21 73 43 68 65 63 6b 2e ;. if( !sCheck.
18ed1 61 6e 52 65 66 20 29 7b 0a 20 20 20 20 2a 70 6e anRef ){. *pn
18ed2 45 72 72 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c Err = 1;. sql
18ed3 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
18ed4 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b );. return 0;
18ed5 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 . }. for(i=0;
18ed6 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 3b i<=sCheck.nPage;
18ed7 20 69 2b 2b 29 7b 20 73 43 68 65 63 6b 2e 61 6e i++){ sCheck.an
18ed8 52 65 66 5b 69 5d 20 3d 20 30 3b 20 7d 0a 20 20 Ref[i] = 0; }.
18ed9 69 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 i = PENDING_BYTE
18eda 5f 50 41 47 45 28 70 42 74 29 3b 0a 20 20 69 66 _PAGE(pBt);. if
18edb 28 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 ( i<=sCheck.nPag
18edc 65 20 29 7b 0a 20 20 20 20 73 43 68 65 63 6b 2e e ){. sCheck.
18edd 61 6e 52 65 66 5b 69 5d 20 3d 20 31 3b 0a 20 20 anRef[i] = 1;.
18ede 7d 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 }. sqlite3StrAc
18edf 63 75 6d 49 6e 69 74 28 26 73 43 68 65 63 6b 2e cumInit(&sCheck.
18ee0 65 72 72 4d 73 67 2c 20 7a 45 72 72 2c 20 73 69 errMsg, zErr, si
18ee1 7a 65 6f 66 28 7a 45 72 72 29 2c 20 32 30 30 30 zeof(zErr), 2000
18ee2 30 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 0);.. /* Check
18ee3 74 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 the integrity of
18ee4 20 74 68 65 20 66 72 65 65 6c 69 73 74 0a 20 20 the freelist.
18ee5 2a 2f 0a 20 20 63 68 65 63 6b 4c 69 73 74 28 26 */. checkList(&
18ee6 73 43 68 65 63 6b 2c 20 31 2c 20 67 65 74 34 62 sCheck, 1, get4b
18ee7 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 yte(&pBt->pPage1
18ee8 2d 3e 61 44 61 74 61 5b 33 32 5d 29 2c 0a 20 20 ->aData[32]),.
18ee9 20 20 20 20 20 20 20 20 20 20 67 65 74 34 62 79 get4by
18eea 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d te(&pBt->pPage1-
18eeb 3e 61 44 61 74 61 5b 33 36 5d 29 2c 20 22 4d 61 >aData[36]), "Ma
18eec 69 6e 20 66 72 65 65 6c 69 73 74 3a 20 22 29 3b in freelist: ");
18eed 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 61 6c 6c .. /* Check all
18eee 20 74 68 65 20 74 61 62 6c 65 73 2e 0a 20 20 2a the tables.. *
18eef 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 28 69 6e /. for(i=0; (in
18ef0 74 29 69 3c 6e 52 6f 6f 74 20 26 26 20 73 43 68 t)i<nRoot && sCh
18ef1 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b eck.mxErr; i++){
18ef2 0a 20 20 20 20 69 66 28 20 61 52 6f 6f 74 5b 69 . if( aRoot[i
18ef3 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b ]==0 ) continue;
18ef4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
18ef5 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
18ef6 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 if( pBt->aut
18ef7 6f 56 61 63 75 75 6d 20 26 26 20 61 52 6f 6f 74 oVacuum && aRoot
18ef8 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 63 [i]>1 ){. c
18ef9 68 65 63 6b 50 74 72 6d 61 70 28 26 73 43 68 65 heckPtrmap(&sChe
18efa 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 50 54 ck, aRoot[i], PT
18efb 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30 RMAP_ROOTPAGE, 0
18efc 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 , 0);. }.#end
18efd 69 66 0a 20 20 20 20 63 68 65 63 6b 54 72 65 65 if. checkTree
18efe 50 61 67 65 28 26 73 43 68 65 63 6b 2c 20 61 52 Page(&sCheck, aR
18eff 6f 6f 74 5b 69 5d 2c 20 22 4c 69 73 74 20 6f 66 oot[i], "List of
18f00 20 74 72 65 65 20 72 6f 6f 74 73 3a 20 22 29 3b tree roots: ");
18f01 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 . }.. /* Make
18f02 73 75 72 65 20 65 76 65 72 79 20 70 61 67 65 20 sure every page
18f03 69 6e 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 in the file is r
18f04 65 66 65 72 65 6e 63 65 64 0a 20 20 2a 2f 0a 20 eferenced. */.
18f05 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 73 43 68 for(i=1; i<=sCh
18f06 65 63 6b 2e 6e 50 61 67 65 20 26 26 20 73 43 68 eck.nPage && sCh
18f07 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b eck.mxErr; i++){
18f08 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
18f09 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
18f0a 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61 6e if( sCheck.an
18f0b 52 65 66 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 Ref[i]==0 ){.
18f0c 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 checkAppendMs
18f0d 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 g(&sCheck, 0, "P
18f0e 61 67 65 20 25 64 20 69 73 20 6e 65 76 65 72 20 age %d is never
18f0f 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d used", i);. }
18f10 0a 23 65 6c 73 65 0a 20 20 20 20 2f 2a 20 49 66 .#else. /* If
18f11 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 75 the database su
18f12 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 pports auto-vacu
18f13 75 6d 2c 20 6d 61 6b 65 20 73 75 72 65 20 6e 6f um, make sure no
18f14 20 74 61 62 6c 65 73 20 63 6f 6e 74 61 69 6e 0a tables contain.
18f15 20 20 20 20 2a 2a 20 72 65 66 65 72 65 6e 63 65 ** reference
18f16 73 20 74 6f 20 70 6f 69 6e 74 65 72 2d 6d 61 70 s to pointer-map
18f17 20 70 61 67 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 pages.. */.
18f18 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61 6e if( sCheck.an
18f19 52 65 66 5b 69 5d 3d 3d 30 20 26 26 20 0a 20 20 Ref[i]==0 && .
18f1a 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50 41 47 (PTRMAP_PAG
18f1b 45 4e 4f 28 70 42 74 2c 20 69 29 21 3d 69 20 7c ENO(pBt, i)!=i |
18f1c 7c 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 | !pBt->autoVacu
18f1d 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 um) ){. che
18f1e 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 ckAppendMsg(&sCh
18f1f 65 63 6b 2c 20 30 2c 20 22 50 61 67 65 20 25 64 eck, 0, "Page %d
18f20 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 22 2c is never used",
18f21 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 i);. }. i
18f22 66 28 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b f( sCheck.anRef[
18f23 69 5d 21 3d 30 20 26 26 20 0a 20 20 20 20 20 20 i]!=0 && .
18f24 20 28 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 (PTRMAP_PAGENO(
18f25 70 42 74 2c 20 69 29 3d 3d 69 20 26 26 20 70 42 pBt, i)==i && pB
18f26 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 20 29 t->autoVacuum) )
18f27 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 {. checkApp
18f28 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 endMsg(&sCheck,
18f29 30 2c 20 22 50 6f 69 6e 74 65 72 20 6d 61 70 20 0, "Pointer map
18f2a 70 61 67 65 20 25 64 20 69 73 20 72 65 66 65 72 page %d is refer
18f2b 65 6e 63 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 enced", i);.
18f2c 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 }.#endif. }..
18f2d 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 /* Make sure thi
18f2e 73 20 61 6e 61 6c 79 73 69 73 20 64 69 64 20 6e s analysis did n
18f2f 6f 74 20 6c 65 61 76 65 20 61 6e 79 20 75 6e 72 ot leave any unr
18f30 65 66 28 29 20 70 61 67 65 73 2e 0a 20 20 2a 2a ef() pages.. **
18f31 20 54 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65 This is an inte
18f32 72 6e 61 6c 20 63 6f 6e 73 69 73 74 65 6e 63 79 rnal consistency
18f33 20 63 68 65 63 6b 3b 20 61 6e 20 69 6e 74 65 67 check; an integ
18f34 72 69 74 79 20 63 68 65 63 6b 0a 20 20 2a 2a 20 rity check. **
18f35 6f 66 20 74 68 65 20 69 6e 74 65 67 72 69 74 79 of the integrity
18f36 20 63 68 65 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 check.. */. i
18f37 66 28 20 4e 45 56 45 52 28 6e 52 65 66 20 21 3d f( NEVER(nRef !=
18f38 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 sqlite3PagerRef
18f39 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 count(pBt->pPage
18f3a 72 29 29 20 29 7b 0a 20 20 20 20 63 68 65 63 6b r)) ){. check
18f3b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 AppendMsg(&sChec
18f3c 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 22 4f 75 k, 0, . "Ou
18f3d 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 63 tstanding page c
18f3e 6f 75 6e 74 20 67 6f 65 73 20 66 72 6f 6d 20 25 ount goes from %
18f3f 64 20 74 6f 20 25 64 20 64 75 72 69 6e 67 20 74 d to %d during t
18f40 68 69 73 20 61 6e 61 6c 79 73 69 73 22 2c 0a 20 his analysis",.
18f41 20 20 20 20 20 6e 52 65 66 2c 20 73 71 6c 69 74 nRef, sqlit
18f42 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 e3PagerRefcount(
18f43 70 42 74 2d 3e 70 50 61 67 65 72 29 0a 20 20 20 pBt->pPager).
18f44 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c );. }.. /* Cl
18f45 65 61 6e 20 20 75 70 20 61 6e 64 20 72 65 70 6f ean up and repo
18f46 72 74 20 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a rt errors.. */.
18f47 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
18f48 61 76 65 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 ave(p);. sqlite
18f49 33 5f 66 72 65 65 28 73 43 68 65 63 6b 2e 61 6e 3_free(sCheck.an
18f4a 52 65 66 29 3b 0a 20 20 69 66 28 20 73 43 68 65 Ref);. if( sChe
18f4b 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 ck.mallocFailed
18f4c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 ){. sqlite3St
18f4d 72 41 63 63 75 6d 52 65 73 65 74 28 26 73 43 68 rAccumReset(&sCh
18f4e 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a 20 20 20 eck.errMsg);.
18f4f 20 2a 70 6e 45 72 72 20 3d 20 73 43 68 65 63 6b *pnErr = sCheck
18f50 2e 6e 45 72 72 2b 31 3b 0a 20 20 20 20 72 65 74 .nErr+1;. ret
18f51 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 2a 70 6e urn 0;. }. *pn
18f52 45 72 72 20 3d 20 73 43 68 65 63 6b 2e 6e 45 72 Err = sCheck.nEr
18f53 72 3b 0a 20 20 69 66 28 20 73 43 68 65 63 6b 2e r;. if( sCheck.
18f54 6e 45 72 72 3d 3d 30 20 29 20 73 71 6c 69 74 65 nErr==0 ) sqlite
18f55 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 26 3StrAccumReset(&
18f56 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a sCheck.errMsg);.
18f57 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
18f58 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 StrAccumFinish(&
18f59 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a sCheck.errMsg);.
18f5a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
18f5b 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 TE_OMIT_INTEGRIT
18f5c 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 0a 2a Y_CHECK */../*.*
18f5d 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c * Return the ful
18f5e 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 l pathname of th
18f5f 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 e underlying dat
18f60 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a abase file..**.*
18f61 2a 20 54 68 65 20 70 61 67 65 72 20 66 69 6c 65 * The pager file
18f62 6e 61 6d 65 20 69 73 20 69 6e 76 61 72 69 61 6e name is invarian
18f63 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 t as long as the
18f64 20 70 61 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65 pager is.** ope
18f65 6e 20 73 6f 20 69 74 20 69 73 20 73 61 66 65 20 n so it is safe
18f66 74 6f 20 61 63 63 65 73 73 20 77 69 74 68 6f 75 to access withou
18f67 74 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6d t the BtShared m
18f68 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f utex..*/.SQLITE_
18f69 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 PRIVATE const ch
18f6a 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 ar *sqlite3Btree
18f6b 47 65 74 46 69 6c 65 6e 61 6d 65 28 42 74 72 65 GetFilename(Btre
18f6c 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 e *p){. assert(
18f6d 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 p->pBt->pPager!
18f6e 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 =0 );. return s
18f6f 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e qlite3PagerFilen
18f70 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 ame(p->pBt->pPag
18f71 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 er);.}../*.** Re
18f72 74 75 72 6e 20 74 68 65 20 70 61 74 68 6e 61 6d turn the pathnam
18f73 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c e of the journal
18f74 20 66 69 6c 65 20 66 6f 72 20 74 68 69 73 20 64 file for this d
18f75 61 74 61 62 61 73 65 2e 20 54 68 65 20 72 65 74 atabase. The ret
18f76 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 urn.** value of
18f77 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
18f78 74 68 65 20 73 61 6d 65 20 72 65 67 61 72 64 6c the same regardl
18f79 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 74 ess of whether t
18f7a 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a he journal file.
18f7b 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 ** has been crea
18f7c 74 65 64 20 6f 72 20 6e 6f 74 2e 0a 2a 2a 0a 2a ted or not..**.*
18f7d 2a 20 54 68 65 20 70 61 67 65 72 20 6a 6f 75 72 * The pager jour
18f7e 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 nal filename is
18f7f 69 6e 76 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e invariant as lon
18f80 67 20 61 73 20 74 68 65 20 70 61 67 65 72 20 69 g as the pager i
18f81 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20 s.** open so it
18f82 69 73 20 73 61 66 65 20 74 6f 20 61 63 63 65 73 is safe to acces
18f83 73 20 77 69 74 68 6f 75 74 20 74 68 65 20 42 74 s without the Bt
18f84 53 68 61 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f Shared mutex..*/
18f85 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
18f86 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
18f87 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72 6e te3BtreeGetJourn
18f88 61 6c 6e 61 6d 65 28 42 74 72 65 65 20 2a 70 29 alname(Btree *p)
18f89 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 {. assert( p->p
18f8a 42 74 2d 3e 70 50 61 67 65 72 21 3d 30 20 29 3b Bt->pPager!=0 );
18f8b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
18f8c 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 3PagerJournalnam
18f8d 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 e(p->pBt->pPager
18f8e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 );.}../*.** Retu
18f8f 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 rn non-zero if a
18f90 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
18f91 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 active..*/.SQLIT
18f92 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
18f93 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 lite3BtreeIsInTr
18f94 61 6e 73 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 ans(Btree *p){.
18f95 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c assert( p==0 ||
18f96 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
18f97 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 eld(p->db->mutex
18f98 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 ) );. return (p
18f99 20 26 26 20 28 70 2d 3e 69 6e 54 72 61 6e 73 3d && (p->inTrans=
18f9a 3d 54 52 41 4e 53 5f 57 52 49 54 45 29 29 3b 0a =TRANS_WRITE));.
18f9b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
18f9c 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 72 65 non-zero if a re
18f9d 61 64 20 28 6f 72 20 77 72 69 74 65 29 20 74 72 ad (or write) tr
18f9e 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 ansaction is act
18f9f 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ive..*/.SQLITE_P
18fa0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
18fa1 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 e3BtreeIsInReadT
18fa2 72 61 6e 73 28 42 74 72 65 65 20 2a 70 29 7b 0a rans(Btree *p){.
18fa3 20 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20 assert( p );.
18fa4 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
18fa5 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 _mutex_held(p->d
18fa6 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 b->mutex) );. r
18fa7 65 74 75 72 6e 20 70 2d 3e 69 6e 54 72 61 6e 73 eturn p->inTrans
18fa8 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 7d 0a !=TRANS_NONE;.}.
18fa9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
18faa 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
18fab 49 73 49 6e 42 61 63 6b 75 70 28 42 74 72 65 65 IsInBackup(Btree
18fac 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
18fad 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 p );. assert( s
18fae 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
18faf 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 d(p->db->mutex)
18fb0 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e );. return p->n
18fb1 42 61 63 6b 75 70 21 3d 30 3b 0a 7d 0a 0a 2f 2a Backup!=0;.}../*
18fb2 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
18fb3 6e 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e n returns a poin
18fb4 74 65 72 20 74 6f 20 61 20 62 6c 6f 62 20 6f 66 ter to a blob of
18fb5 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 memory associat
18fb6 65 64 20 77 69 74 68 0a 2a 2a 20 61 20 73 69 6e ed with.** a sin
18fb7 67 6c 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 gle shared-btree
18fb8 2e 20 54 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 . The memory is
18fb9 75 73 65 64 20 62 79 20 63 6c 69 65 6e 74 20 63 used by client c
18fba 6f 64 65 20 66 6f 72 20 69 74 73 20 6f 77 6e 0a ode for its own.
18fbb 2a 2a 20 70 75 72 70 6f 73 65 73 20 28 66 6f 72 ** purposes (for
18fbc 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 73 74 6f example, to sto
18fbd 72 65 20 61 20 68 69 67 68 2d 6c 65 76 65 6c 20 re a high-level
18fbe 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74 65 schema associate
18fbf 64 20 77 69 74 68 20 0a 2a 2a 20 74 68 65 20 73 d with .** the s
18fc0 68 61 72 65 64 2d 62 74 72 65 65 29 2e 20 54 68 hared-btree). Th
18fc1 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d 61 e btree layer ma
18fc2 6e 61 67 65 73 20 72 65 66 65 72 65 6e 63 65 20 nages reference
18fc3 63 6f 75 6e 74 69 6e 67 20 69 73 73 75 65 73 2e counting issues.
18fc4 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 .**.** The first
18fc5 20 74 69 6d 65 20 74 68 69 73 20 69 73 20 63 61 time this is ca
18fc6 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61 72 65 64 lled on a shared
18fc7 2d 62 74 72 65 65 2c 20 6e 42 79 74 65 73 20 62 -btree, nBytes b
18fc8 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a ytes of memory.*
18fc9 2a 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 2c * are allocated,
18fca 20 7a 65 72 6f 65 64 2c 20 61 6e 64 20 72 65 74 zeroed, and ret
18fcb 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c urned to the cal
18fcc 6c 65 72 2e 20 46 6f 72 20 65 61 63 68 20 73 75 ler. For each su
18fcd 62 73 65 71 75 65 6e 74 20 0a 2a 2a 20 63 61 6c bsequent .** cal
18fce 6c 20 74 68 65 20 6e 42 79 74 65 73 20 70 61 72 l the nBytes par
18fcf 61 6d 65 74 65 72 20 69 73 20 69 67 6e 6f 72 65 ameter is ignore
18fd0 64 20 61 6e 64 20 61 20 70 6f 69 6e 74 65 72 20 d and a pointer
18fd1 74 6f 20 74 68 65 20 73 61 6d 65 20 62 6c 6f 62 to the same blob
18fd2 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 .** of memory re
18fd3 74 75 72 6e 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 turned. .**.** I
18fd4 66 20 74 68 65 20 6e 42 79 74 65 73 20 70 61 72 f the nBytes par
18fd5 61 6d 65 74 65 72 20 69 73 20 30 20 61 6e 64 20 ameter is 0 and
18fd6 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f the blob of memo
18fd7 72 79 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 ry has not yet b
18fd8 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 een.** allocated
18fd9 2c 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 , a null pointer
18fda 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 is returned. If
18fdb 20 74 68 65 20 62 6c 6f 62 20 68 61 73 20 61 6c the blob has al
18fdc 72 65 61 64 79 20 62 65 65 6e 0a 2a 2a 20 61 6c ready been.** al
18fdd 6c 6f 63 61 74 65 64 2c 20 69 74 20 69 73 20 72 located, it is r
18fde 65 74 75 72 6e 65 64 20 61 73 20 6e 6f 72 6d 61 eturned as norma
18fdf 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75 73 74 20 62 65 l..**.** Just be
18fe0 66 6f 72 65 20 74 68 65 20 73 68 61 72 65 64 2d fore the shared-
18fe1 62 74 72 65 65 20 69 73 20 63 6c 6f 73 65 64 2c btree is closed,
18fe2 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 the function pa
18fe3 73 73 65 64 20 61 73 20 74 68 65 20 0a 2a 2a 20 ssed as the .**
18fe4 78 46 72 65 65 20 61 72 67 75 6d 65 6e 74 20 77 xFree argument w
18fe5 68 65 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 hen the memory a
18fe6 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20 6d 61 llocation was ma
18fe7 64 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e de is invoked on
18fe8 20 74 68 65 20 0a 2a 2a 20 62 6c 6f 62 20 6f 66 the .** blob of
18fe9 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 allocated memor
18fea 79 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e y. This function
18feb 20 73 68 6f 75 6c 64 20 6e 6f 74 20 63 61 6c 6c should not call
18fec 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 0a sqlite3_free().
18fed 2a 2a 20 6f 6e 20 74 68 65 20 6d 65 6d 6f 72 79 ** on the memory
18fee 2c 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 , the btree laye
18fef 72 20 64 6f 65 73 20 74 68 61 74 2e 0a 2a 2f 0a r does that..*/.
18ff0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
18ff1 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 oid *sqlite3Btre
18ff2 65 53 63 68 65 6d 61 28 42 74 72 65 65 20 2a 70 eSchema(Btree *p
18ff3 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 76 6f , int nBytes, vo
18ff4 69 64 28 2a 78 46 72 65 65 29 28 76 6f 69 64 20 id(*xFree)(void
18ff5 2a 29 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 *)){. BtShared
18ff6 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
18ff7 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
18ff8 65 72 28 70 29 3b 0a 20 20 69 66 28 20 21 70 42 er(p);. if( !pB
18ff9 74 2d 3e 70 53 63 68 65 6d 61 20 26 26 20 6e 42 t->pSchema && nB
18ffa 79 74 65 73 20 29 7b 0a 20 20 20 20 70 42 74 2d ytes ){. pBt-
18ffb 3e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 >pSchema = sqlit
18ffc 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 79 e3MallocZero(nBy
18ffd 74 65 73 29 3b 0a 20 20 20 20 70 42 74 2d 3e 78 tes);. pBt->x
18ffe 46 72 65 65 53 63 68 65 6d 61 20 3d 20 78 46 72 FreeSchema = xFr
18fff 65 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 ee;. }. sqlite
19000 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
19001 20 20 72 65 74 75 72 6e 20 70 42 74 2d 3e 70 53 return pBt->pS
19002 63 68 65 6d 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 chema;.}../*.**
19003 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f Return SQLITE_LO
19004 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 CKED_SHAREDCACHE
19005 20 69 66 20 61 6e 6f 74 68 65 72 20 75 73 65 72 if another user
19006 20 6f 66 20 74 68 65 20 73 61 6d 65 20 73 68 61 of the same sha
19007 72 65 64 20 0a 2a 2a 20 62 74 72 65 65 20 61 73 red .** btree as
19008 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 68 61 the argument ha
19009 6e 64 6c 65 20 68 6f 6c 64 73 20 61 6e 20 65 78 ndle holds an ex
1900a 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 clusive lock on
1900b 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 5f 6d the .** sqlite_m
1900c 61 73 74 65 72 20 74 61 62 6c 65 2e 20 4f 74 68 aster table. Oth
1900d 65 72 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b erwise SQLITE_OK
1900e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1900f 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
19010 74 72 65 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64 treeSchemaLocked
19011 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e (Btree *p){. in
19012 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 t rc;. assert(
19013 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
19014 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ld(p->db->mutex)
19015 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 );. sqlite3Btr
19016 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 eeEnter(p);. rc
19017 20 3d 20 71 75 65 72 79 53 68 61 72 65 64 43 61 = querySharedCa
19018 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 cheTableLock(p,
19019 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 52 45 41 MASTER_ROOT, REA
1901a 44 5f 4c 4f 43 4b 29 3b 0a 20 20 61 73 73 65 72 D_LOCK);. asser
1901b 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b t( rc==SQLITE_OK
1901c 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4c || rc==SQLITE_L
1901d 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 OCKED_SHAREDCACH
1901e 45 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 E );. sqlite3Bt
1901f 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
19020 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 69 eturn rc;.}...#i
19021 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
19022 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f T_SHARED_CACHE./
19023 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20 6c 6f *.** Obtain a lo
19024 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 ck on the table
19025 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 20 whose root page
19026 69 73 20 69 54 61 62 2e 20 20 54 68 65 0a 2a 2a is iTab. The.**
19027 20 6c 6f 63 6b 20 69 73 20 61 20 77 72 69 74 65 lock is a write
19028 20 6c 6f 63 6b 20 69 66 20 69 73 57 72 69 74 65 lock if isWrite
19029 6c 6f 63 6b 20 69 73 20 74 72 75 65 20 6f 72 20 lock is true or
1902a 61 20 72 65 61 64 20 6c 6f 63 6b 0a 2a 2a 20 69 a read lock.** i
1902b 66 20 69 74 20 69 73 20 66 61 6c 73 65 2e 0a 2a f it is false..*
1902c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1902d 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
1902e 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74 72 65 65 eLockTable(Btree
1902f 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 2c 20 75 *p, int iTab, u
19030 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 7b 0a 8 isWriteLock){.
19031 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
19032 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 E_OK;. assert(
19033 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e p->inTrans!=TRAN
19034 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 69 66 28 20 S_NONE );. if(
19035 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 p->sharable ){.
19036 20 20 20 75 38 20 6c 6f 63 6b 54 79 70 65 20 3d u8 lockType =
19037 20 52 45 41 44 5f 4c 4f 43 4b 20 2b 20 69 73 57 READ_LOCK + isW
19038 72 69 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 61 73 riteLock;. as
19039 73 65 72 74 28 20 52 45 41 44 5f 4c 4f 43 4b 2b sert( READ_LOCK+
1903a 31 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 1==WRITE_LOCK );
1903b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 57 . assert( isW
1903c 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69 riteLock==0 || i
1903d 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b sWriteLock==1 );
1903e 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 .. sqlite3Btr
1903f 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 eeEnter(p);.
19040 72 63 20 3d 20 71 75 65 72 79 53 68 61 72 65 64 rc = queryShared
19041 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 CacheTableLock(p
19042 2c 20 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65 , iTab, lockType
19043 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
19044 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
19045 20 20 72 63 20 3d 20 73 65 74 53 68 61 72 65 64 rc = setShared
19046 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 CacheTableLock(p
19047 2c 20 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65 , iTab, lockType
19048 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
19049 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
1904a 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1904b 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 rc;.}.#endif..#i
1904c 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1904d 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a T_INCRBLOB./*.**
1904e 20 41 72 67 75 6d 65 6e 74 20 70 43 73 72 20 6d Argument pCsr m
1904f 75 73 74 20 62 65 20 61 20 63 75 72 73 6f 72 20 ust be a cursor
19050 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 opened for writi
19051 6e 67 20 6f 6e 20 61 6e 20 0a 2a 2a 20 49 4e 54 ng on an .** INT
19052 4b 45 59 20 74 61 62 6c 65 20 63 75 72 72 65 6e KEY table curren
19053 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 tly pointing at
19054 61 20 76 61 6c 69 64 20 74 61 62 6c 65 20 65 6e a valid table en
19055 74 72 79 2e 20 0a 2a 2a 20 54 68 69 73 20 66 75 try. .** This fu
19056 6e 63 74 69 6f 6e 20 6d 6f 64 69 66 69 65 73 20 nction modifies
19057 74 68 65 20 64 61 74 61 20 73 74 6f 72 65 64 20 the data stored
19058 61 73 20 70 61 72 74 20 6f 66 20 74 68 61 74 20 as part of that
19059 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c entry..**.** Onl
1905a 79 20 74 68 65 20 64 61 74 61 20 63 6f 6e 74 65 y the data conte
1905b 6e 74 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6d nt may only be m
1905c 6f 64 69 66 69 65 64 2c 20 69 74 20 69 73 20 6e odified, it is n
1905d 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 0a ot possible to .
1905e 2a 2a 20 63 68 61 6e 67 65 20 74 68 65 20 6c 65 ** change the le
1905f 6e 67 74 68 20 6f 66 20 74 68 65 20 64 61 74 61 ngth of the data
19060 20 73 74 6f 72 65 64 2e 20 49 66 20 74 68 69 73 stored. If this
19061 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
19062 6c 65 64 20 77 69 74 68 0a 2a 2a 20 70 61 72 61 led with.** para
19063 6d 65 74 65 72 73 20 74 68 61 74 20 61 74 74 65 meters that atte
19064 6d 70 74 20 74 6f 20 77 72 69 74 65 20 70 61 73 mpt to write pas
19065 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
19066 20 65 78 69 73 74 69 6e 67 20 64 61 74 61 2c 0a existing data,.
19067 2a 2a 20 6e 6f 20 6d 6f 64 69 66 69 63 61 74 69 ** no modificati
19068 6f 6e 73 20 61 72 65 20 6d 61 64 65 20 61 6e 64 ons are made and
19069 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 SQLITE_CORRUPT
1906a 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a is returned..*/.
1906b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1906c 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 nt sqlite3BtreeP
1906d 75 74 44 61 74 61 28 42 74 43 75 72 73 6f 72 20 utData(BtCursor
1906e 2a 70 43 73 72 2c 20 75 33 32 20 6f 66 66 73 65 *pCsr, u32 offse
1906f 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 t, u32 amt, void
19070 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a *z){. int rc;.
19071 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
19072 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 73 72 29 HoldsMutex(pCsr)
19073 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
19074 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
19075 28 70 43 73 72 2d 3e 70 42 74 72 65 65 2d 3e 64 (pCsr->pBtree->d
19076 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 b->mutex) );. a
19077 73 73 65 72 74 28 20 70 43 73 72 2d 3e 69 73 49 ssert( pCsr->isI
19078 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 29 3b ncrblobHandle );
19079 0a 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 .. rc = restore
1907a 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 CursorPosition(p
1907b 43 73 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d Csr);. if( rc!=
1907c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1907d 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
1907e 20 20 61 73 73 65 72 74 28 20 70 43 73 72 2d 3e assert( pCsr->
1907f 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 52 eState!=CURSOR_R
19080 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 EQUIRESEEK );.
19081 69 66 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65 if( pCsr->eState
19082 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 !=CURSOR_VALID )
19083 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
19084 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 0a ITE_ABORT;. }..
19085 20 20 2f 2a 20 43 68 65 63 6b 20 73 6f 6d 65 20 /* Check some
19086 61 73 73 75 6d 70 74 69 6f 6e 73 3a 20 0a 20 20 assumptions: .
19087 2a 2a 20 20 20 28 61 29 20 74 68 65 20 63 75 72 ** (a) the cur
19088 73 6f 72 20 69 73 20 6f 70 65 6e 20 66 6f 72 20 sor is open for
19089 77 72 69 74 69 6e 67 2c 0a 20 20 2a 2a 20 20 20 writing,. **
1908a 28 62 29 20 74 68 65 72 65 20 69 73 20 61 20 72 (b) there is a r
1908b 65 61 64 2f 77 72 69 74 65 20 74 72 61 6e 73 61 ead/write transa
1908c 63 74 69 6f 6e 20 6f 70 65 6e 2c 0a 20 20 2a 2a ction open,. **
1908d 20 20 20 28 63 29 20 74 68 65 20 63 6f 6e 6e 65 (c) the conne
1908e 63 74 69 6f 6e 20 68 6f 6c 64 73 20 61 20 77 72 ction holds a wr
1908f 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 ite-lock on the
19090 74 61 62 6c 65 20 28 69 66 20 72 65 71 75 69 72 table (if requir
19091 65 64 29 2c 0a 20 20 2a 2a 20 20 20 28 64 29 20 ed),. ** (d)
19092 74 68 65 72 65 20 61 72 65 20 6e 6f 20 63 6f 6e there are no con
19093 66 6c 69 63 74 69 6e 67 20 72 65 61 64 2d 6c 6f flicting read-lo
19094 63 6b 73 2c 20 61 6e 64 0a 20 20 2a 2a 20 20 20 cks, and. **
19095 28 65 29 20 74 68 65 20 63 75 72 73 6f 72 20 70 (e) the cursor p
19096 6f 69 6e 74 73 20 61 74 20 61 20 76 61 6c 69 64 oints at a valid
19097 20 72 6f 77 20 6f 66 20 61 6e 20 69 6e 74 4b 65 row of an intKe
19098 79 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 y table.. */.
19099 69 66 28 20 21 70 43 73 72 2d 3e 77 72 46 6c 61 if( !pCsr->wrFla
1909a 67 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 g ){. return
1909b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b SQLITE_READONLY;
1909c 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 . }. assert( !
1909d 70 43 73 72 2d 3e 70 42 74 2d 3e 72 65 61 64 4f pCsr->pBt->readO
1909e 6e 6c 79 20 26 26 20 70 43 73 72 2d 3e 70 42 74 nly && pCsr->pBt
1909f 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d ->inTransaction=
190a0 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a =TRANS_WRITE );.
190a1 20 20 61 73 73 65 72 74 28 20 68 61 73 53 68 61 assert( hasSha
190a2 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
190a3 6b 28 70 43 73 72 2d 3e 70 42 74 72 65 65 2c 20 k(pCsr->pBtree,
190a4 70 43 73 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 pCsr->pgnoRoot,
190a5 30 2c 20 32 29 20 29 3b 0a 20 20 61 73 73 65 72 0, 2) );. asser
190a6 74 28 20 21 68 61 73 52 65 61 64 43 6f 6e 66 6c t( !hasReadConfl
190a7 69 63 74 73 28 70 43 73 72 2d 3e 70 42 74 72 65 icts(pCsr->pBtre
190a8 65 2c 20 70 43 73 72 2d 3e 70 67 6e 6f 52 6f 6f e, pCsr->pgnoRoo
190a9 74 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 t) );. assert(
190aa 70 43 73 72 2d 3e 61 70 50 61 67 65 5b 70 43 73 pCsr->apPage[pCs
190ab 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 6e 74 4b 65 r->iPage]->intKe
190ac 79 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 61 y );.. return a
190ad 63 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 73 ccessPayload(pCs
190ae 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 r, offset, amt,
190af 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a (unsigned char *
190b0 29 7a 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a )z, 1);.}../* .*
190b1 2a 20 53 65 74 20 61 20 66 6c 61 67 20 6f 6e 20 * Set a flag on
190b2 74 68 69 73 20 63 75 72 73 6f 72 20 74 6f 20 63 this cursor to c
190b3 61 63 68 65 20 74 68 65 20 6c 6f 63 61 74 69 6f ache the locatio
190b4 6e 73 20 6f 66 20 70 61 67 65 73 20 66 72 6f 6d ns of pages from
190b5 20 74 68 65 20 0a 2a 2a 20 6f 76 65 72 66 6c 6f the .** overflo
190b6 77 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20 63 w list for the c
190b7 75 72 72 65 6e 74 20 72 6f 77 2e 20 54 68 69 73 urrent row. This
190b8 20 69 73 20 75 73 65 64 20 62 79 20 63 75 72 73 is used by curs
190b9 6f 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f ors opened.** fo
190ba 72 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c r incremental bl
190bb 6f 62 20 49 4f 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a ob IO only..**.*
190bc 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
190bd 73 65 74 73 20 61 20 66 6c 61 67 20 6f 6e 6c 79 sets a flag only
190be 2e 20 54 68 65 20 61 63 74 75 61 6c 20 70 61 67 . The actual pag
190bf 65 20 6c 6f 63 61 74 69 6f 6e 20 63 61 63 68 65 e location cache
190c0 0a 2a 2a 20 28 73 74 6f 72 65 64 20 69 6e 20 42 .** (stored in B
190c1 74 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f tCursor.aOverflo
190c2 77 5b 5d 29 20 69 73 20 61 6c 6c 6f 63 61 74 65 w[]) is allocate
190c3 64 20 61 6e 64 20 75 73 65 64 20 62 79 20 66 75 d and used by fu
190c4 6e 63 74 69 6f 6e 0a 2a 2a 20 61 63 63 65 73 73 nction.** access
190c5 50 61 79 6c 6f 61 64 28 29 20 28 74 68 65 20 77 Payload() (the w
190c6 6f 72 6b 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 orker function f
190c7 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 or sqlite3BtreeD
190c8 61 74 61 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c ata() and.** sql
190c9 69 74 65 33 42 74 72 65 65 50 75 74 44 61 74 61 ite3BtreePutData
190ca 28 29 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ())..*/.SQLITE_P
190cb 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
190cc 74 65 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 te3BtreeCacheOve
190cd 72 66 6c 6f 77 28 42 74 43 75 72 73 6f 72 20 2a rflow(BtCursor *
190ce 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 pCur){. assert(
190cf 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
190d0 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 x(pCur) );. ass
190d1 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
190d2 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 ex_held(pCur->pB
190d3 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 tree->db->mutex)
190d4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 21 70 43 );. assert(!pC
190d5 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 ur->isIncrblobHa
190d6 6e 64 6c 65 29 3b 0a 20 20 61 73 73 65 72 74 28 ndle);. assert(
190d7 21 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 !pCur->aOverflow
190d8 29 3b 0a 20 20 70 43 75 72 2d 3e 69 73 49 6e 63 );. pCur->isInc
190d9 72 62 6c 6f 62 48 61 6e 64 6c 65 20 3d 20 31 3b rblobHandle = 1;
190da 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a .}.#endif../****
190db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
190dc 66 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a f btree.c ******
190dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
190de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
190df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
190e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
190e1 20 66 69 6c 65 20 62 61 63 6b 75 70 2e 63 20 2a file backup.c *
190e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
190e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
190e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
190e5 20 32 30 30 39 20 4a 61 6e 75 61 72 79 20 32 38 2009 January 28
190e6 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
190e7 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
190e8 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
190e9 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
190ea 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
190eb 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
190ec 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
190ed 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
190ee 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
190ef 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
190f0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
190f1 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
190f2 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
190f3 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
190f4 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
190f5 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
190f6 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
190f7 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
190f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
190f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
190fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
190fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
190fc 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
190fd 74 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d tains the implem
190fe 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
190ff 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 58 sqlite3_backup_X
19100 58 58 28 29 20 0a 2a 2a 20 41 50 49 20 66 75 6e XX() .** API fun
19101 63 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 72 ctions and the r
19102 65 6c 61 74 65 64 20 66 65 61 74 75 72 65 73 2e elated features.
19103 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 61 63 6b .**.** $Id: back
19104 75 70 2e 63 2c 76 20 31 2e 31 39 20 32 30 30 39 up.c,v 1.19 2009
19105 2f 30 37 2f 30 36 20 31 39 3a 30 33 3a 31 33 20 /07/06 19:03:13
19106 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a drh Exp $.*/../*
19107 20 4d 61 63 72 6f 20 74 6f 20 66 69 6e 64 20 74 Macro to find t
19108 68 65 20 6d 69 6e 69 6d 75 6d 20 6f 66 20 74 77 he minimum of tw
19109 6f 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 73 o numeric values
1910a 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4d 49 4e ..*/.#ifndef MIN
1910b 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e 28 78 2c .# define MIN(x,
1910c 79 29 20 28 28 78 29 3c 28 79 29 3f 28 78 29 3a y) ((x)<(y)?(x):
1910d 28 79 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a (y)).#endif../*.
1910e 2a 2a 20 53 74 72 75 63 74 75 72 65 20 61 6c 6c ** Structure all
1910f 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 ocated for each
19110 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
19111 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 ..*/.struct sqli
19112 74 65 33 5f 62 61 63 6b 75 70 20 7b 0a 20 20 73 te3_backup {. s
19113 71 6c 69 74 65 33 2a 20 70 44 65 73 74 44 62 3b qlite3* pDestDb;
19114 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69 /* Desti
19115 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 nation database
19116 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 handle */. Btre
19117 65 20 2a 70 44 65 73 74 3b 20 20 20 20 20 20 20 e *pDest;
19118 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 /* Destinat
19119 69 6f 6e 20 62 2d 74 72 65 65 20 66 69 6c 65 20 ion b-tree file
1911a 2a 2f 0a 20 20 75 33 32 20 69 44 65 73 74 53 63 */. u32 iDestSc
1911b 68 65 6d 61 3b 20 20 20 20 20 20 20 20 20 2f 2a hema; /*
1911c 20 4f 72 69 67 69 6e 61 6c 20 73 63 68 65 6d 61 Original schema
1911d 20 63 6f 6f 6b 69 65 20 69 6e 20 64 65 73 74 69 cookie in desti
1911e 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 nation */. int
1911f 62 44 65 73 74 4c 6f 63 6b 65 64 3b 20 20 20 20 bDestLocked;
19120 20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63 /* True onc
19121 65 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 e a write-transa
19122 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 20 6f 6e ction is open on
19123 20 70 44 65 73 74 20 2a 2f 0a 0a 20 20 50 67 6e pDest */.. Pgn
19124 6f 20 69 4e 65 78 74 3b 20 20 20 20 20 20 20 20 o iNext;
19125 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 /* Page nu
19126 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 mber of the next
19127 20 73 6f 75 72 63 65 20 70 61 67 65 20 74 6f 20 source page to
19128 63 6f 70 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 copy */. sqlite
19129 33 2a 20 70 53 72 63 44 62 3b 20 20 20 20 20 20 3* pSrcDb;
1912a 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 /* Source dat
1912b 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a abase handle */.
1912c 20 20 42 74 72 65 65 20 2a 70 53 72 63 3b 20 20 Btree *pSrc;
1912d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f /* So
1912e 75 72 63 65 20 62 2d 74 72 65 65 20 66 69 6c 65 urce b-tree file
1912f 20 2a 2f 0a 0a 20 20 69 6e 74 20 72 63 3b 20 20 */.. int rc;
19130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19131 2f 2a 20 42 61 63 6b 75 70 20 70 72 6f 63 65 73 /* Backup proces
19132 73 20 65 72 72 6f 72 20 63 6f 64 65 20 2a 2f 0a s error code */.
19133 0a 20 20 2f 2a 20 54 68 65 73 65 20 74 77 6f 20 . /* These two
19134 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 73 65 variables are se
19135 74 20 62 79 20 65 76 65 72 79 20 63 61 6c 6c 20 t by every call
19136 74 6f 20 62 61 63 6b 75 70 5f 73 74 65 70 28 29 to backup_step()
19137 2e 20 54 68 65 79 20 61 72 65 0a 20 20 2a 2a 20 . They are. **
19138 72 65 61 64 20 62 79 20 63 61 6c 6c 73 20 74 6f read by calls to
19139 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e backup_remainin
1913a 67 28 29 20 61 6e 64 20 62 61 63 6b 75 70 5f 70 g() and backup_p
1913b 61 67 65 63 6f 75 6e 74 28 29 2e 0a 20 20 2a 2f agecount().. */
1913c 0a 20 20 50 67 6e 6f 20 6e 52 65 6d 61 69 6e 69 . Pgno nRemaini
1913d 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ng; /* N
1913e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6c umber of pages l
1913f 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 eft to copy */.
19140 20 50 67 6e 6f 20 6e 50 61 67 65 63 6f 75 6e 74 Pgno nPagecount
19141 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 ; /* Tot
19142 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 al number of pag
19143 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 0a 20 es to copy */..
19144 20 69 6e 74 20 69 73 41 74 74 61 63 68 65 64 3b int isAttached;
19145 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
19146 65 20 6f 6e 63 65 20 62 61 63 6b 75 70 20 68 61 e once backup ha
19147 73 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 s been registere
19148 64 20 77 69 74 68 20 70 61 67 65 72 20 2a 2f 0a d with pager */.
19149 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 sqlite3_backup
1914a 20 2a 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65 *pNext; /* Ne
1914b 78 74 20 62 61 63 6b 75 70 20 61 73 73 6f 63 69 xt backup associ
1914c 61 74 65 64 20 77 69 74 68 20 73 6f 75 72 63 65 ated with source
1914d 20 70 61 67 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a pager */.};../*
1914e 0a 2a 2a 20 54 48 52 45 41 44 20 53 41 46 45 54 .** THREAD SAFET
1914f 59 20 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 20 Y NOTES:.**.**
19150 20 4f 6e 63 65 20 69 74 20 68 61 73 20 62 65 65 Once it has bee
19151 6e 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 n created using
19152 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2c 20 61 backup_init(), a
19153 20 73 69 6e 67 6c 65 20 73 71 6c 69 74 65 33 5f single sqlite3_
19154 62 61 63 6b 75 70 0a 2a 2a 20 20 20 73 74 72 75 backup.** stru
19155 63 74 75 72 65 20 6d 61 79 20 62 65 20 61 63 63 cture may be acc
19156 65 73 73 65 64 20 76 69 61 20 74 77 6f 20 67 72 essed via two gr
19157 6f 75 70 73 20 6f 66 20 74 68 72 65 61 64 2d 73 oups of thread-s
19158 61 66 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 afe entry points
19159 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69 :.**.** * Vi
1915a 61 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 a the sqlite3_ba
1915b 63 6b 75 70 5f 58 58 58 28 29 20 41 50 49 20 66 ckup_XXX() API f
1915c 75 6e 63 74 69 6f 6e 20 62 61 63 6b 75 70 5f 73 unction backup_s
1915d 74 65 70 28 29 20 61 6e 64 20 0a 2a 2a 20 20 20 tep() and .**
1915e 20 20 20 20 62 61 63 6b 75 70 5f 66 69 6e 69 73 backup_finis
1915f 68 28 29 2e 20 42 6f 74 68 20 74 68 65 73 65 20 h(). Both these
19160 66 75 6e 63 74 69 6f 6e 73 20 6f 62 74 61 69 6e functions obtain
19161 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
19162 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 68 61 base.** ha
19163 6e 64 6c 65 20 6d 75 74 65 78 20 61 6e 64 20 74 ndle mutex and t
19164 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 he mutex associa
19165 74 65 64 20 77 69 74 68 20 74 68 65 20 73 6f 75 ted with the sou
19166 72 63 65 20 42 74 53 68 61 72 65 64 20 0a 2a 2a rce BtShared .**
19167 20 20 20 20 20 20 20 73 74 72 75 63 74 75 72 65 structure
19168 2c 20 69 6e 20 74 68 61 74 20 6f 72 64 65 72 2e , in that order.
19169 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69 61 .**.** * Via
1916a 20 74 68 65 20 42 61 63 6b 75 70 55 70 64 61 74 the BackupUpdat
1916b 65 28 29 20 61 6e 64 20 42 61 63 6b 75 70 52 65 e() and BackupRe
1916c 73 74 61 72 74 28 29 20 66 75 6e 63 74 69 6f 6e start() function
1916d 73 2c 20 77 68 69 63 68 20 61 72 65 0a 2a 2a 20 s, which are.**
1916e 20 20 20 20 20 20 69 6e 76 6f 6b 65 64 20 62 79 invoked by
1916f 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 the pager layer
19170 20 74 6f 20 72 65 70 6f 72 74 20 76 61 72 69 6f to report vario
19171 75 73 20 73 74 61 74 65 20 63 68 61 6e 67 65 73 us state changes
19172 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 74 68 65 in.** the
19173 20 70 61 67 65 20 63 61 63 68 65 20 61 73 73 6f page cache asso
19174 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
19175 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 2e source database.
19176 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 20 20 The mutex.**
19177 20 20 20 20 61 73 73 6f 63 69 61 74 65 64 20 77 associated w
19178 69 74 68 20 74 68 65 20 73 6f 75 72 63 65 20 64 ith the source d
19179 61 74 61 62 61 73 65 20 42 74 53 68 61 72 65 64 atabase BtShared
1917a 20 73 74 72 75 63 74 75 72 65 20 77 69 6c 6c 20 structure will
1917b 61 6c 77 61 79 73 20 0a 2a 2a 20 20 20 20 20 20 always .**
1917c 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 65 69 be held when ei
1917d 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 66 75 ther of these fu
1917e 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 76 6f nctions are invo
1917f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 ked..**.** The
19180 20 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f 62 other sqlite3_b
19181 61 63 6b 75 70 5f 58 58 58 28 29 20 41 50 49 20 ackup_XXX() API
19182 66 75 6e 63 74 69 6f 6e 73 2c 20 62 61 63 6b 75 functions, backu
19183 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 6e p_remaining() an
19184 64 0a 2a 2a 20 20 20 62 61 63 6b 75 70 5f 70 61 d.** backup_pa
19185 67 65 63 6f 75 6e 74 28 29 20 61 72 65 20 6e 6f gecount() are no
19186 74 20 74 68 72 65 61 64 2d 73 61 66 65 20 66 75 t thread-safe fu
19187 6e 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 79 nctions. If they
19188 20 61 72 65 20 63 61 6c 6c 65 64 0a 2a 2a 20 20 are called.**
19189 20 77 68 69 6c 65 20 73 6f 6d 65 20 6f 74 68 65 while some othe
1918a 72 20 74 68 72 65 61 64 20 69 73 20 63 61 6c 6c r thread is call
1918b 69 6e 67 20 62 61 63 6b 75 70 5f 73 74 65 70 28 ing backup_step(
1918c 29 20 6f 72 20 62 61 63 6b 75 70 5f 66 69 6e 69 ) or backup_fini
1918d 73 68 28 29 2c 0a 2a 2a 20 20 20 74 68 65 20 76 sh(),.** the v
1918e 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 6d alues returned m
1918f 61 79 20 62 65 20 69 6e 76 61 6c 69 64 2e 20 54 ay be invalid. T
19190 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 66 here is no way f
19191 6f 72 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 or a call to.**
19192 20 20 42 61 63 6b 75 70 55 70 64 61 74 65 28 29 BackupUpdate()
19193 20 6f 72 20 42 61 63 6b 75 70 52 65 73 74 61 72 or BackupRestar
19194 74 28 29 20 74 6f 20 69 6e 74 65 72 66 65 72 65 t() to interfere
19195 20 77 69 74 68 20 62 61 63 6b 75 70 5f 72 65 6d with backup_rem
19196 61 69 6e 69 6e 67 28 29 0a 2a 2a 20 20 20 6f 72 aining().** or
19197 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e backup_pagecoun
19198 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 65 70 t()..**.** Dep
19199 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 53 51 ending on the SQ
1919a 4c 69 74 65 20 63 6f 6e 66 69 67 75 72 61 74 69 Lite configurati
1919b 6f 6e 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 on, the database
1919c 20 68 61 6e 64 6c 65 73 20 61 6e 64 2f 6f 72 0a handles and/or.
1919d 2a 2a 20 20 20 74 68 65 20 42 74 72 65 65 20 6f ** the Btree o
1919e 62 6a 65 63 74 73 20 6d 61 79 20 68 61 76 65 20 bjects may have
1919f 74 68 65 69 72 20 6f 77 6e 20 6d 75 74 65 78 65 their own mutexe
191a0 73 20 74 68 61 74 20 72 65 71 75 69 72 65 20 6c s that require l
191a1 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 20 4e 6f 6e ocking..** Non
191a2 2d 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 -sharable Btrees
191a3 20 28 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 (in-memory data
191a4 62 61 73 65 73 20 66 6f 72 20 65 78 61 6d 70 6c bases for exampl
191a5 65 29 2c 20 64 6f 20 6e 6f 74 20 68 61 76 65 0a e), do not have.
191a6 2a 2a 20 20 20 61 73 73 6f 63 69 61 74 65 64 20 ** associated
191a7 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a mutexes..*/../*.
191a8 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e ** Return a poin
191a9 74 65 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e ter correspondin
191aa 67 20 74 6f 20 64 61 74 61 62 61 73 65 20 7a 44 g to database zD
191ab 62 20 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 b (i.e. "main",
191ac 22 74 65 6d 70 22 29 0a 2a 2a 20 69 6e 20 63 6f "temp").** in co
191ad 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 nnection handle
191ae 70 44 62 2e 20 49 66 20 73 75 63 68 20 61 20 64 pDb. If such a d
191af 61 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 atabase cannot b
191b0 65 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 0a e found, return.
191b1 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 ** a NULL pointe
191b2 72 20 61 6e 64 20 77 72 69 74 65 20 61 6e 20 65 r and write an e
191b3 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 rror message to
191b4 70 45 72 72 6f 72 44 62 2e 0a 2a 2a 0a 2a 2a 20 pErrorDb..**.**
191b5 49 66 20 74 68 65 20 22 74 65 6d 70 22 20 64 61 If the "temp" da
191b6 74 61 62 61 73 65 20 69 73 20 72 65 71 75 65 73 tabase is reques
191b7 74 65 64 2c 20 69 74 20 6d 61 79 20 6e 65 65 64 ted, it may need
191b8 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 62 79 to be opened by
191b9 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 this .** functi
191ba 6f 6e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 on. If an error
191bb 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64 6f 69 occurs while doi
191bc 6e 67 20 73 6f 2c 20 72 65 74 75 72 6e 20 30 20 ng so, return 0
191bd 61 6e 64 20 77 72 69 74 65 20 61 6e 20 0a 2a 2a and write an .**
191be 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 error message t
191bf 6f 20 70 45 72 72 6f 72 44 62 2e 0a 2a 2f 0a 73 o pErrorDb..*/.s
191c0 74 61 74 69 63 20 42 74 72 65 65 20 2a 66 69 6e tatic Btree *fin
191c1 64 42 74 72 65 65 28 73 71 6c 69 74 65 33 20 2a dBtree(sqlite3 *
191c2 70 45 72 72 6f 72 44 62 2c 20 73 71 6c 69 74 65 pErrorDb, sqlite
191c3 33 20 2a 70 44 62 2c 20 63 6f 6e 73 74 20 63 68 3 *pDb, const ch
191c4 61 72 20 2a 7a 44 62 29 7b 0a 20 20 69 6e 74 20 ar *zDb){. int
191c5 69 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44 i = sqlite3FindD
191c6 62 4e 61 6d 65 28 70 44 62 2c 20 7a 44 62 29 3b bName(pDb, zDb);
191c7 0a 0a 20 20 69 66 28 20 69 3d 3d 31 20 29 7b 0a .. if( i==1 ){.
191c8 20 20 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 Parse *pPars
191c9 65 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 e;. int rc =
191ca 30 3b 0a 20 20 20 20 70 50 61 72 73 65 20 3d 20 0;. pParse =
191cb 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f sqlite3StackAllo
191cc 63 5a 65 72 6f 28 70 45 72 72 6f 72 44 62 2c 20 cZero(pErrorDb,
191cd 73 69 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29 sizeof(*pParse))
191ce 3b 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65 ;. if( pParse
191cf 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ==0 ){. sql
191d0 69 74 65 33 45 72 72 6f 72 28 70 45 72 72 6f 72 ite3Error(pError
191d1 44 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d Db, SQLITE_NOMEM
191d2 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 , "out of memory
191d3 22 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ");. rc = S
191d4 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
191d5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 }else{. pP
191d6 61 72 73 65 2d 3e 64 62 20 3d 20 70 44 62 3b 0a arse->db = pDb;.
191d7 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
191d8 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 3OpenTempDatabas
191d9 65 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 e(pParse) ){.
191da 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
191db 72 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a rClear(pParse);.
191dc 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
191dd 72 72 6f 72 28 70 45 72 72 6f 72 44 62 2c 20 70 rror(pErrorDb, p
191de 50 61 72 73 65 2d 3e 72 63 2c 20 22 25 73 22 2c Parse->rc, "%s",
191df 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 pParse->zErrMsg
191e0 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
191e1 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
191e2 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
191e3 74 65 33 53 74 61 63 6b 46 72 65 65 28 70 45 72 te3StackFree(pEr
191e4 72 6f 72 44 62 2c 20 70 50 61 72 73 65 29 3b 0a rorDb, pParse);.
191e5 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
191e6 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
191e7 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 0;. }. }..
191e8 20 69 66 28 20 69 3c 30 20 29 7b 0a 20 20 20 20 if( i<0 ){.
191e9 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 45 72 sqlite3Error(pEr
191ea 72 6f 72 44 62 2c 20 53 51 4c 49 54 45 5f 45 52 rorDb, SQLITE_ER
191eb 52 4f 52 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 61 ROR, "unknown da
191ec 74 61 62 61 73 65 20 25 73 22 2c 20 7a 44 62 29 tabase %s", zDb)
191ed 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
191ee 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 44 }.. return pD
191ef 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 7d b->aDb[i].pBt;.}
191f0 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
191f1 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 n sqlite3_backup
191f2 20 70 72 6f 63 65 73 73 20 74 6f 20 63 6f 70 79 process to copy
191f3 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
191f4 20 7a 53 72 63 44 62 20 66 72 6f 6d 0a 2a 2a 20 zSrcDb from.**
191f5 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c connection handl
191f6 65 20 70 53 72 63 44 62 20 74 6f 20 7a 44 65 73 e pSrcDb to zDes
191f7 74 44 62 20 69 6e 20 70 44 65 73 74 44 62 2e 20 tDb in pDestDb.
191f8 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 If successful, r
191f9 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 eturn.** a point
191fa 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73 71 er to the new sq
191fb 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a lite3_backup obj
191fc 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ect..**.** If an
191fd 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e error occurs, N
191fe 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 ULL is returned
191ff 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 and an error cod
19200 65 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 e and error mess
19201 61 67 65 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e age.** stored in
19202 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
19203 20 70 44 65 73 74 44 62 2e 0a 2a 2f 0a 53 51 4c pDestDb..*/.SQL
19204 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
19205 62 61 63 6b 75 70 20 2a 73 71 6c 69 74 65 33 5f backup *sqlite3_
19206 62 61 63 6b 75 70 5f 69 6e 69 74 28 0a 20 20 73 backup_init(. s
19207 71 6c 69 74 65 33 2a 20 70 44 65 73 74 44 62 2c qlite3* pDestDb,
19208 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19209 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
1920a 20 74 6f 20 77 72 69 74 65 20 74 6f 20 2a 2f 0a to write to */.
1920b 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 const char *zD
1920c 65 73 74 44 62 2c 20 20 20 20 20 20 20 20 20 20 estDb,
1920d 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
1920e 6f 66 20 64 61 74 61 62 61 73 65 20 77 69 74 68 of database with
1920f 69 6e 20 70 44 65 73 74 44 62 20 2a 2f 0a 20 20 in pDestDb */.
19210 73 71 6c 69 74 65 33 2a 20 70 53 72 63 44 62 2c sqlite3* pSrcDb,
19211 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19212 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
19213 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 e connection to
19214 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 63 read from */. c
19215 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 72 63 44 onst char *zSrcD
19216 62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b
19217 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
19218 64 61 74 61 62 61 73 65 20 77 69 74 68 69 6e 20 database within
19219 70 53 72 63 44 62 20 2a 2f 0a 29 7b 0a 20 20 73 pSrcDb */.){. s
1921a 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 qlite3_backup *p
1921b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1921c 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f /* Value to
1921d 20 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a return */.. /*
1921e 20 4c 6f 63 6b 20 74 68 65 20 73 6f 75 72 63 65 Lock the source
1921f 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
19220 2e 20 54 68 65 20 64 65 73 74 69 6e 61 74 69 6f . The destinatio
19221 6e 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 n database. **
19222 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20 6c 6f handle is not lo
19223 63 6b 65 64 20 69 6e 20 74 68 69 73 20 72 6f 75 cked in this rou
19224 74 69 6e 65 2c 20 62 75 74 20 69 74 20 69 73 20 tine, but it is
19225 6c 6f 63 6b 65 64 20 69 6e 0a 20 20 2a 2a 20 73 locked in. ** s
19226 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
19227 65 70 28 29 2e 20 54 68 65 20 75 73 65 72 20 69 ep(). The user i
19228 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 65 6e s required to en
19229 73 75 72 65 20 74 68 61 74 20 6e 6f 0a 20 20 2a sure that no. *
1922a 2a 20 6f 74 68 65 72 20 74 68 72 65 61 64 20 61 * other thread a
1922b 63 63 65 73 73 65 73 20 74 68 65 20 64 65 73 74 ccesses the dest
1922c 69 6e 61 74 69 6f 6e 20 68 61 6e 64 6c 65 20 66 ination handle f
1922d 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 0a or the duration.
1922e 20 20 2a 2a 20 6f 66 20 74 68 65 20 62 61 63 6b ** of the back
1922f 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 41 up operation. A
19230 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 ny attempt to us
19231 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f e the destinatio
19232 6e 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 n. ** database
19233 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 connection while
19234 20 61 20 62 61 63 6b 75 70 20 69 73 20 69 6e 20 a backup is in
19235 70 72 6f 67 72 65 73 73 20 6d 61 79 20 63 61 75 progress may cau
19236 73 65 0a 20 20 2a 2a 20 61 20 6d 61 6c 66 75 6e se. ** a malfun
19237 63 74 69 6f 6e 20 6f 72 20 61 20 64 65 61 64 6c ction or a deadl
19238 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 ock.. */. sqli
19239 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
1923a 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a pSrcDb->mutex);.
1923b 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1923c 65 6e 74 65 72 28 70 44 65 73 74 44 62 2d 3e 6d enter(pDestDb->m
1923d 75 74 65 78 29 3b 0a 0a 20 20 69 66 28 20 70 53 utex);.. if( pS
1923e 72 63 44 62 3d 3d 70 44 65 73 74 44 62 20 29 7b rcDb==pDestDb ){
1923f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
19240 72 28 0a 20 20 20 20 20 20 20 20 70 44 65 73 74 r(. pDest
19241 44 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 Db, SQLITE_ERROR
19242 2c 20 22 73 6f 75 72 63 65 20 61 6e 64 20 64 65 , "source and de
19243 73 74 69 6e 61 74 69 6f 6e 20 6d 75 73 74 20 62 stination must b
19244 65 20 64 69 73 74 69 6e 63 74 22 0a 20 20 20 20 e distinct".
19245 29 3b 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 );. p = 0;.
19246 7d 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 41 }else {. /* A
19247 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f llocate space fo
19248 72 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f r a new sqlite3_
19249 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 2a 2f backup object */
1924a 0a 20 20 20 20 70 20 3d 20 28 73 71 6c 69 74 65 . p = (sqlite
1924b 33 5f 62 61 63 6b 75 70 20 2a 29 73 71 6c 69 74 3_backup *)sqlit
1924c 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 e3_malloc(sizeof
1924d 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 29 (sqlite3_backup)
1924e 29 3b 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b );. if( !p ){
1924f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
19250 72 6f 72 28 70 44 65 73 74 44 62 2c 20 53 51 4c ror(pDestDb, SQL
19251 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 ITE_NOMEM, 0);.
19252 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 }. }.. /* I
19253 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e f the allocation
19254 20 73 75 63 63 65 65 64 65 64 2c 20 70 6f 70 75 succeeded, popu
19255 6c 61 74 65 20 74 68 65 20 6e 65 77 20 6f 62 6a late the new obj
19256 65 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 20 ect. */. if( p
19257 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c ){. memset(p,
19258 20 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 0, sizeof(sqlit
19259 65 33 5f 62 61 63 6b 75 70 29 29 3b 0a 20 20 20 e3_backup));.
1925a 20 70 2d 3e 70 53 72 63 20 3d 20 66 69 6e 64 42 p->pSrc = findB
1925b 74 72 65 65 28 70 44 65 73 74 44 62 2c 20 70 53 tree(pDestDb, pS
1925c 72 63 44 62 2c 20 7a 53 72 63 44 62 29 3b 0a 20 rcDb, zSrcDb);.
1925d 20 20 20 70 2d 3e 70 44 65 73 74 20 3d 20 66 69 p->pDest = fi
1925e 6e 64 42 74 72 65 65 28 70 44 65 73 74 44 62 2c ndBtree(pDestDb,
1925f 20 70 44 65 73 74 44 62 2c 20 7a 44 65 73 74 44 pDestDb, zDestD
19260 62 29 3b 0a 20 20 20 20 70 2d 3e 70 44 65 73 74 b);. p->pDest
19261 44 62 20 3d 20 70 44 65 73 74 44 62 3b 0a 20 20 Db = pDestDb;.
19262 20 20 70 2d 3e 70 53 72 63 44 62 20 3d 20 70 53 p->pSrcDb = pS
19263 72 63 44 62 3b 0a 20 20 20 20 70 2d 3e 69 4e 65 rcDb;. p->iNe
19264 78 74 20 3d 20 31 3b 0a 20 20 20 20 70 2d 3e 69 xt = 1;. p->i
19265 73 41 74 74 61 63 68 65 64 20 3d 20 30 3b 0a 0a sAttached = 0;..
19266 20 20 20 20 69 66 28 20 30 3d 3d 70 2d 3e 70 53 if( 0==p->pS
19267 72 63 20 7c 7c 20 30 3d 3d 70 2d 3e 70 44 65 73 rc || 0==p->pDes
19268 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e t ){. /* On
19269 65 20 28 6f 72 20 62 6f 74 68 29 20 6f 66 20 74 e (or both) of t
1926a 68 65 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73 he named databas
1926b 65 73 20 64 69 64 20 6e 6f 74 20 65 78 69 73 74 es did not exist
1926c 2e 20 41 6e 20 65 72 72 6f 72 20 68 61 73 0a 20 . An error has.
1926d 20 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 ** already
1926e 62 65 65 6e 20 77 72 69 74 74 65 6e 20 69 6e 74 been written int
1926f 6f 20 74 68 65 20 70 44 65 73 74 44 62 20 68 61 o the pDestDb ha
19270 6e 64 6c 65 2e 20 41 6c 6c 20 74 68 61 74 20 69 ndle. All that i
19271 73 20 6c 65 66 74 0a 20 20 20 20 20 20 2a 2a 20 s left. **
19272 74 6f 20 64 6f 20 68 65 72 65 20 69 73 20 66 72 to do here is fr
19273 65 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 ee the sqlite3_b
19274 61 63 6b 75 70 20 73 74 72 75 63 74 75 72 65 2e ackup structure.
19275 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
19276 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b sqlite3_free(p);
19277 0a 20 20 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 . p = 0;.
19278 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 20 }. }. if( p
19279 29 7b 0a 20 20 20 20 70 2d 3e 70 53 72 63 2d 3e ){. p->pSrc->
1927a 6e 42 61 63 6b 75 70 2b 2b 3b 0a 20 20 7d 0a 0a nBackup++;. }..
1927b 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1927c 6c 65 61 76 65 28 70 44 65 73 74 44 62 2d 3e 6d leave(pDestDb->m
1927d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 utex);. sqlite3
1927e 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 53 72 _mutex_leave(pSr
1927f 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 cDb->mutex);. r
19280 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
19281 2a 20 41 72 67 75 6d 65 6e 74 20 72 63 20 69 73 * Argument rc is
19282 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 an SQLite error
19283 20 63 6f 64 65 2e 20 52 65 74 75 72 6e 20 74 72 code. Return tr
19284 75 65 20 69 66 20 74 68 69 73 20 65 72 72 6f 72 ue if this error
19285 20 69 73 20 0a 2a 2a 20 63 6f 6e 73 69 64 65 72 is .** consider
19286 65 64 20 66 61 74 61 6c 20 69 66 20 65 6e 63 6f ed fatal if enco
19287 75 6e 74 65 72 65 64 20 64 75 72 69 6e 67 20 61 untered during a
19288 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f backup operatio
19289 6e 2e 20 41 6c 6c 20 65 72 72 6f 72 73 0a 2a 2a n. All errors.**
1928a 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 are considered
1928b 66 61 74 61 6c 20 65 78 63 65 70 74 20 66 6f 72 fatal except for
1928c 20 53 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 SQLITE_BUSY and
1928d 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a SQLITE_LOCKED..
1928e 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 */.static int is
1928f 46 61 74 61 6c 45 72 72 6f 72 28 69 6e 74 20 72 FatalError(int r
19290 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 72 63 c){. return (rc
19291 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 !=SQLITE_OK && r
19292 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 c!=SQLITE_BUSY &
19293 26 20 41 4c 57 41 59 53 28 72 63 21 3d 53 51 4c & ALWAYS(rc!=SQL
19294 49 54 45 5f 4c 4f 43 4b 45 44 29 29 3b 0a 7d 0a ITE_LOCKED));.}.
19295 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 ./*.** Parameter
19296 20 7a 53 72 63 44 61 74 61 20 70 6f 69 6e 74 73 zSrcData points
19297 20 74 6f 20 61 20 62 75 66 66 65 72 20 63 6f 6e to a buffer con
19298 74 61 69 6e 69 6e 67 20 74 68 65 20 64 61 74 61 taining the data
19299 20 66 6f 72 20 0a 2a 2a 20 70 61 67 65 20 69 53 for .** page iS
1929a 72 63 50 67 20 66 72 6f 6d 20 74 68 65 20 73 6f rcPg from the so
1929b 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 20 43 urce database. C
1929c 6f 70 79 20 74 68 69 73 20 64 61 74 61 20 69 6e opy this data in
1929d 74 6f 20 74 68 65 20 0a 2a 2a 20 64 65 73 74 69 to the .** desti
1929e 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e nation database.
1929f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 .*/.static int b
192a0 61 63 6b 75 70 4f 6e 65 50 61 67 65 28 73 71 6c ackupOnePage(sql
192a1 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20 ite3_backup *p,
192a2 50 67 6e 6f 20 69 53 72 63 50 67 2c 20 63 6f 6e Pgno iSrcPg, con
192a3 73 74 20 75 38 20 2a 7a 53 72 63 44 61 74 61 29 st u8 *zSrcData)
192a4 7b 0a 20 20 50 61 67 65 72 20 2a 20 63 6f 6e 73 {. Pager * cons
192a5 74 20 70 44 65 73 74 50 61 67 65 72 20 3d 20 73 t pDestPager = s
192a6 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 qlite3BtreePager
192a7 28 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 63 6f (p->pDest);. co
192a8 6e 73 74 20 69 6e 74 20 6e 53 72 63 50 67 73 7a nst int nSrcPgsz
192a9 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 = sqlite3BtreeG
192aa 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 53 etPageSize(p->pS
192ab 72 63 29 3b 0a 20 20 69 6e 74 20 6e 44 65 73 74 rc);. int nDest
192ac 50 67 73 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 Pgsz = sqlite3Bt
192ad 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 reeGetPageSize(p
192ae 2d 3e 70 44 65 73 74 29 3b 0a 20 20 63 6f 6e 73 ->pDest);. cons
192af 74 20 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49 t int nCopy = MI
192b0 4e 28 6e 53 72 63 50 67 73 7a 2c 20 6e 44 65 73 N(nSrcPgsz, nDes
192b1 74 50 67 73 7a 29 3b 0a 20 20 63 6f 6e 73 74 20 tPgsz);. const
192b2 69 36 34 20 69 45 6e 64 20 3d 20 28 69 36 34 29 i64 iEnd = (i64)
192b3 69 53 72 63 50 67 2a 28 69 36 34 29 6e 53 72 63 iSrcPg*(i64)nSrc
192b4 50 67 73 7a 3b 0a 0a 20 20 69 6e 74 20 72 63 20 Pgsz;.. int rc
192b5 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
192b6 36 34 20 69 4f 66 66 3b 0a 0a 20 20 61 73 73 65 64 iOff;.. asse
192b7 72 74 28 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b rt( p->bDestLock
192b8 65 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ed );. assert(
192b9 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28 70 2d !isFatalError(p-
192ba 3e 72 63 29 20 29 3b 0a 20 20 61 73 73 65 72 74 >rc) );. assert
192bb 28 20 69 53 72 63 50 67 21 3d 50 45 4e 44 49 4e ( iSrcPg!=PENDIN
192bc 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 G_BYTE_PAGE(p->p
192bd 53 72 63 2d 3e 70 42 74 29 20 29 3b 0a 20 20 61 Src->pBt) );. a
192be 73 73 65 72 74 28 20 7a 53 72 63 44 61 74 61 20 ssert( zSrcData
192bf 29 3b 0a 0a 20 20 2f 2a 20 43 61 74 63 68 20 74 );.. /* Catch t
192c0 68 65 20 63 61 73 65 20 77 68 65 72 65 20 74 68 he case where th
192c1 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 e destination is
192c2 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
192c3 74 61 62 61 73 65 20 61 6e 64 20 74 68 65 0a 20 tabase and the.
192c4 20 2a 2a 20 70 61 67 65 20 73 69 7a 65 73 20 6f ** page sizes o
192c5 66 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e 64 f the source and
192c6 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 69 66 destination dif
192c7 66 65 72 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 fer. . */. if(
192c8 20 6e 53 72 63 50 67 73 7a 21 3d 6e 44 65 73 74 nSrcPgsz!=nDest
192c9 50 67 73 7a 20 26 26 20 73 71 6c 69 74 65 33 50 Pgsz && sqlite3P
192ca 61 67 65 72 49 73 4d 65 6d 64 62 28 73 71 6c 69 agerIsMemdb(sqli
192cb 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d te3BtreePager(p-
192cc 3e 70 44 65 73 74 29 29 20 29 7b 0a 20 20 20 20 >pDest)) ){.
192cd 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 rc = SQLITE_READ
192ce 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 ONLY;. }.. /*
192cf 54 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 6f This loop runs o
192d0 6e 63 65 20 66 6f 72 20 65 61 63 68 20 64 65 73 nce for each des
192d1 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 20 73 70 tination page sp
192d2 61 6e 6e 65 64 20 62 79 20 74 68 65 20 73 6f 75 anned by the sou
192d3 72 63 65 20 0a 20 20 2a 2a 20 70 61 67 65 2e 20 rce . ** page.
192d4 46 6f 72 20 65 61 63 68 20 69 74 65 72 61 74 69 For each iterati
192d5 6f 6e 2c 20 76 61 72 69 61 62 6c 65 20 69 4f 66 on, variable iOf
192d6 66 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 f is set to the
192d7 62 79 74 65 20 6f 66 66 73 65 74 0a 20 20 2a 2a byte offset. **
192d8 20 6f 66 20 74 68 65 20 64 65 73 74 69 6e 61 74 of the destinat
192d9 69 6f 6e 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 ion page.. */.
192da 20 66 6f 72 28 69 4f 66 66 3d 69 45 6e 64 2d 28 for(iOff=iEnd-(
192db 69 36 34 29 6e 53 72 63 50 67 73 7a 3b 20 72 63 i64)nSrcPgsz; rc
192dc 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 ==SQLITE_OK && i
192dd 4f 66 66 3c 69 45 6e 64 3b 20 69 4f 66 66 2b 3d Off<iEnd; iOff+=
192de 6e 44 65 73 74 50 67 73 7a 29 7b 0a 20 20 20 20 nDestPgsz){.
192df 44 62 50 61 67 65 20 2a 70 44 65 73 74 50 67 20 DbPage *pDestPg
192e0 3d 20 30 3b 0a 20 20 20 20 50 67 6e 6f 20 69 44 = 0;. Pgno iD
192e1 65 73 74 20 3d 20 28 50 67 6e 6f 29 28 69 4f 66 est = (Pgno)(iOf
192e2 66 2f 6e 44 65 73 74 50 67 73 7a 29 2b 31 3b 0a f/nDestPgsz)+1;.
192e3 20 20 20 20 69 66 28 20 69 44 65 73 74 3d 3d 50 if( iDest==P
192e4 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
192e5 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 20 (p->pDest->pBt)
192e6 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
192e7 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 if( SQLITE_OK==(
192e8 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
192e9 72 47 65 74 28 70 44 65 73 74 50 61 67 65 72 2c rGet(pDestPager,
192ea 20 69 44 65 73 74 2c 20 26 70 44 65 73 74 50 67 iDest, &pDestPg
192eb 29 29 0a 20 20 20 20 20 26 26 20 53 51 4c 49 54 )). && SQLIT
192ec 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 E_OK==(rc = sqli
192ed 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 44 te3PagerWrite(pD
192ee 65 73 74 50 67 29 29 0a 20 20 20 20 29 7b 0a 20 estPg)). ){.
192ef 20 20 20 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a const u8 *z
192f0 49 6e 20 3d 20 26 7a 53 72 63 44 61 74 61 5b 69 In = &zSrcData[i
192f1 4f 66 66 25 6e 53 72 63 50 67 73 7a 5d 3b 0a 20 Off%nSrcPgsz];.
192f2 20 20 20 20 20 75 38 20 2a 7a 44 65 73 74 44 61 u8 *zDestDa
192f3 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 ta = sqlite3Page
192f4 72 47 65 74 44 61 74 61 28 70 44 65 73 74 50 67 rGetData(pDestPg
192f5 29 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 4f 75 );. u8 *zOu
192f6 74 20 3d 20 26 7a 44 65 73 74 44 61 74 61 5b 69 t = &zDestData[i
192f7 4f 66 66 25 6e 44 65 73 74 50 67 73 7a 5d 3b 0a Off%nDestPgsz];.
192f8 0a 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 . /* Copy t
192f9 68 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 he data from the
192fa 20 73 6f 75 72 63 65 20 70 61 67 65 20 69 6e 74 source page int
192fb 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f o the destinatio
192fc 6e 20 70 61 67 65 2e 0a 20 20 20 20 20 20 2a 2a n page.. **
192fd 20 54 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20 Then clear the
192fe 42 74 72 65 65 20 6c 61 79 65 72 20 4d 65 6d 50 Btree layer MemP
192ff 61 67 65 2e 69 73 49 6e 69 74 20 66 6c 61 67 2e age.isInit flag.
19300 20 42 6f 74 68 20 74 68 69 73 20 6d 6f 64 75 6c Both this modul
19301 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 74 e. ** and t
19302 68 65 20 70 61 67 65 72 20 63 6f 64 65 20 75 73 he pager code us
19303 65 20 74 68 69 73 20 74 72 69 63 6b 20 28 63 6c e this trick (cl
19304 65 61 72 69 6e 67 20 74 68 65 20 66 69 72 73 74 earing the first
19305 20 62 79 74 65 0a 20 20 20 20 20 20 2a 2a 20 6f byte. ** o
19306 66 20 74 68 65 20 70 61 67 65 20 27 65 78 74 72 f the page 'extr
19307 61 27 20 73 70 61 63 65 20 74 6f 20 69 6e 76 61 a' space to inva
19308 6c 69 64 61 74 65 20 74 68 65 20 42 74 72 65 65 lidate the Btree
19309 20 6c 61 79 65 72 73 0a 20 20 20 20 20 20 2a 2a layers. **
1930a 20 63 61 63 68 65 64 20 70 61 72 73 65 20 6f 66 cached parse of
1930b 20 74 68 65 20 70 61 67 65 29 2e 20 4d 65 6d 50 the page). MemP
1930c 61 67 65 2e 69 73 49 6e 69 74 20 69 73 20 6d 61 age.isInit is ma
1930d 72 6b 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 22 rked . ** "
1930e 4d 55 53 54 20 42 45 20 46 49 52 53 54 22 20 66 MUST BE FIRST" f
1930f 6f 72 20 74 68 69 73 20 70 75 72 70 6f 73 65 2e or this purpose.
19310 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
19311 6d 65 6d 63 70 79 28 7a 4f 75 74 2c 20 7a 49 6e memcpy(zOut, zIn
19312 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 20 20 , nCopy);.
19313 28 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61 ((u8 *)sqlite3Pa
19314 67 65 72 47 65 74 45 78 74 72 61 28 70 44 65 73 gerGetExtra(pDes
19315 74 50 67 29 29 5b 30 5d 20 3d 20 30 3b 0a 20 20 tPg))[0] = 0;.
19316 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 }. sqlite3P
19317 61 67 65 72 55 6e 72 65 66 28 70 44 65 73 74 50 agerUnref(pDestP
19318 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 g);. }.. retur
19319 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 n rc;.}../*.** I
1931a 66 20 70 46 69 6c 65 20 69 73 20 63 75 72 72 65 f pFile is curre
1931b 6e 74 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e ntly larger than
1931c 20 69 53 69 7a 65 20 62 79 74 65 73 2c 20 74 68 iSize bytes, th
1931d 65 6e 20 74 72 75 6e 63 61 74 65 20 69 74 20 74 en truncate it t
1931e 6f 0a 2a 2a 20 65 78 61 63 74 6c 79 20 69 53 69 o.** exactly iSi
1931f 7a 65 20 62 79 74 65 73 2e 20 49 66 20 70 46 69 ze bytes. If pFi
19320 6c 65 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 72 le is not larger
19321 20 74 68 61 6e 20 69 53 69 7a 65 20 62 79 74 65 than iSize byte
19322 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 s, then.** this
19323 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f function is a no
19324 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 -op..**.** Retur
19325 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 n SQLITE_OK if e
19326 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 verything is suc
19327 63 65 73 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53 cessful, or an S
19328 51 4c 69 74 65 20 65 72 72 6f 72 20 0a 2a 2a 20 QLite error .**
19329 63 6f 64 65 20 69 66 20 61 6e 20 65 72 72 6f 72 code if an error
1932a 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 occurs..*/.stat
1932b 69 63 20 69 6e 74 20 62 61 63 6b 75 70 54 72 75 ic int backupTru
1932c 6e 63 61 74 65 46 69 6c 65 28 73 71 6c 69 74 65 ncateFile(sqlite
1932d 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 69 3_file *pFile, i
1932e 36 34 20 69 53 69 7a 65 29 7b 0a 20 20 69 36 34 64 iSize){. i64
1932f 20 69 43 75 72 72 65 6e 74 3b 0a 20 20 69 6e 74 iCurrent;. int
19330 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 rc = sqlite3OsF
19331 69 6c 65 53 69 7a 65 28 70 46 69 6c 65 2c 20 26 ileSize(pFile, &
19332 69 43 75 72 72 65 6e 74 29 3b 0a 20 20 69 66 28 iCurrent);. if(
19333 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
19334 26 20 69 43 75 72 72 65 6e 74 3e 69 53 69 7a 65 & iCurrent>iSize
19335 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
19336 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 ite3OsTruncate(p
19337 46 69 6c 65 2c 20 69 53 69 7a 65 29 3b 0a 20 20 File, iSize);.
19338 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
19339 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 ../*.** Register
1933a 20 74 68 69 73 20 62 61 63 6b 75 70 20 6f 62 6a this backup obj
1933b 65 63 74 20 77 69 74 68 20 74 68 65 20 61 73 73 ect with the ass
1933c 6f 63 69 61 74 65 64 20 73 6f 75 72 63 65 20 70 ociated source p
1933d 61 67 65 72 20 66 6f 72 0a 2a 2a 20 63 61 6c 6c ager for.** call
1933e 62 61 63 6b 73 20 77 68 65 6e 20 70 61 67 65 73 backs when pages
1933f 20 61 72 65 20 63 68 61 6e 67 65 64 20 6f 72 20 are changed or
19340 74 68 65 20 63 61 63 68 65 20 69 6e 76 61 6c 69 the cache invali
19341 64 61 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 dated..*/.static
19342 20 76 6f 69 64 20 61 74 74 61 63 68 42 61 63 6b void attachBack
19343 75 70 4f 62 6a 65 63 74 28 73 71 6c 69 74 65 33 upObject(sqlite3
19344 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 73 _backup *p){. s
19345 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a qlite3_backup **
19346 70 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 pp;. assert( sq
19347 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d lite3BtreeHoldsM
19348 75 74 65 78 28 70 2d 3e 70 53 72 63 29 20 29 3b utex(p->pSrc) );
19349 0a 20 20 70 70 20 3d 20 73 71 6c 69 74 65 33 50 . pp = sqlite3P
1934a 61 67 65 72 42 61 63 6b 75 70 50 74 72 28 73 71 agerBackupPtr(sq
1934b 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 lite3BtreePager(
1934c 70 2d 3e 70 53 72 63 29 29 3b 0a 20 20 70 2d 3e p->pSrc));. p->
1934d 70 4e 65 78 74 20 3d 20 2a 70 70 3b 0a 20 20 2a pNext = *pp;. *
1934e 70 70 20 3d 20 70 3b 0a 20 20 70 2d 3e 69 73 41 pp = p;. p->isA
1934f 74 74 61 63 68 65 64 20 3d 20 31 3b 0a 7d 0a 0a ttached = 1;.}..
19350 2f 2a 0a 2a 2a 20 43 6f 70 79 20 6e 50 61 67 65 /*.** Copy nPage
19351 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 pages from the
19352 73 6f 75 72 63 65 20 62 2d 74 72 65 65 20 74 6f source b-tree to
19353 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
19354 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
19355 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b int sqlite3_back
19356 75 70 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f up_step(sqlite3_
19357 62 61 63 6b 75 70 20 2a 70 2c 20 69 6e 74 20 6e backup *p, int n
19358 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b Page){. int rc;
19359 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .. sqlite3_mute
1935a 78 5f 65 6e 74 65 72 28 70 2d 3e 70 53 72 63 44 x_enter(p->pSrcD
1935b 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c b->mutex);. sql
1935c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
1935d 2d 3e 70 53 72 63 29 3b 0a 20 20 69 66 28 20 70 ->pSrc);. if( p
1935e 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 ->pDestDb ){.
1935f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
19360 6e 74 65 72 28 70 2d 3e 70 44 65 73 74 44 62 2d nter(p->pDestDb-
19361 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 >mutex);. }..
19362 72 63 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 69 66 rc = p->rc;. if
19363 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28 ( !isFatalError(
19364 72 63 29 20 29 7b 0a 20 20 20 20 50 61 67 65 72 rc) ){. Pager
19365 20 2a 20 63 6f 6e 73 74 20 70 53 72 63 50 61 67 * const pSrcPag
19366 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 er = sqlite3Btre
19367 65 50 61 67 65 72 28 70 2d 3e 70 53 72 63 29 3b ePager(p->pSrc);
19368 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 /* Source p
19369 61 67 65 72 20 2a 2f 0a 20 20 20 20 50 61 67 65 ager */. Page
1936a 72 20 2a 20 63 6f 6e 73 74 20 70 44 65 73 74 50 r * const pDestP
1936b 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 ager = sqlite3Bt
1936c 72 65 65 50 61 67 65 72 28 70 2d 3e 70 44 65 73 reePager(p->pDes
1936d 74 29 3b 20 20 20 2f 2a 20 44 65 73 74 20 70 61 t); /* Dest pa
1936e 67 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 ger */. int i
1936f 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
19370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19371 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 Iterator variab
19372 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 le */. int nS
19373 72 63 50 61 67 65 20 3d 20 2d 31 3b 20 20 20 20 rcPage = -1;
19374 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19375 53 69 7a 65 20 6f 66 20 73 6f 75 72 63 65 20 64 Size of source d
19376 62 20 69 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20 b in pages */.
19377 20 20 69 6e 74 20 62 43 6c 6f 73 65 54 72 61 6e int bCloseTran
19378 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 s = 0;
19379 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
1937a 73 72 63 20 64 62 20 72 65 71 75 69 72 65 73 20 src db requires
1937b 75 6e 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 0a 20 20 unlocking */..
1937c 20 20 2f 2a 20 49 66 20 74 68 65 20 73 6f 75 72 /* If the sour
1937d 63 65 20 70 61 67 65 72 20 69 73 20 63 75 72 72 ce pager is curr
1937e 65 6e 74 6c 79 20 69 6e 20 61 20 77 72 69 74 65 ently in a write
1937f 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 72 65 -transaction, re
19380 74 75 72 6e 0a 20 20 20 20 2a 2a 20 53 51 4c 49 turn. ** SQLI
19381 54 45 5f 42 55 53 59 20 69 6d 6d 65 64 69 61 74 TE_BUSY immediat
19382 65 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ely.. */.
19383 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 26 if( p->pDestDb &
19384 26 20 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e & p->pSrc->pBt->
19385 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 inTransaction==T
19386 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 RANS_WRITE ){.
19387 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
19388 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b BUSY;. }else{
19389 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1938a 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 0a 20 20 TE_OK;. }..
1938b 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 64 65 /* Lock the de
1938c 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 stination databa
1938d 73 65 2c 20 69 66 20 69 74 20 69 73 20 6e 6f 74 se, if it is not
1938e 20 6c 6f 63 6b 65 64 20 61 6c 72 65 61 64 79 2e locked already.
1938f 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 4c 49 */. if( SQLI
19390 54 45 5f 4f 4b 3d 3d 72 63 20 26 26 20 70 2d 3e TE_OK==rc && p->
19391 62 44 65 73 74 4c 6f 63 6b 65 64 3d 3d 30 0a 20 bDestLocked==0.
19392 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b && SQLITE_OK
19393 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 ==(rc = sqlite3B
19394 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70 treeBeginTrans(p
19395 2d 3e 70 44 65 73 74 2c 20 32 29 29 20 0a 20 20 ->pDest, 2)) .
19396 20 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 62 44 ){. p->bD
19397 65 73 74 4c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 estLocked = 1;.
19398 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
19399 65 47 65 74 4d 65 74 61 28 70 2d 3e 70 44 65 73 eGetMeta(p->pDes
1939a 74 2c 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f t, BTREE_SCHEMA_
1939b 56 45 52 53 49 4f 4e 2c 20 26 70 2d 3e 69 44 65 VERSION, &p->iDe
1939c 73 74 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d stSchema);. }
1939d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 .. /* If ther
1939e 65 20 69 73 20 6e 6f 20 6f 70 65 6e 20 72 65 61 e is no open rea
1939f 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e d-transaction on
193a0 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
193a1 62 61 73 65 2c 20 6f 70 65 6e 0a 20 20 20 20 2a base, open. *
193a2 2a 20 6f 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20 * one now. If a
193a3 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f transaction is o
193a4 70 65 6e 65 64 20 68 65 72 65 2c 20 74 68 65 6e pened here, then
193a5 20 69 74 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73 it will be clos
193a6 65 64 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 ed. ** before
193a7 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 this function e
193a8 78 69 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 xits.. */.
193a9 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
193aa 4f 4b 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 OK && 0==sqlite3
193ab 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 BtreeIsInReadTra
193ac 6e 73 28 70 2d 3e 70 53 72 63 29 20 29 7b 0a 20 ns(p->pSrc) ){.
193ad 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
193ae 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 3BtreeBeginTrans
193af 28 70 2d 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20 (p->pSrc, 0);.
193b0 20 20 20 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 bCloseTrans
193b1 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 = 1;. }. .
193b2 20 20 2f 2a 20 4e 6f 77 20 74 68 61 74 20 74 68 /* Now that th
193b3 65 72 65 20 69 73 20 61 20 72 65 61 64 2d 6c 6f ere is a read-lo
193b4 63 6b 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 ck on the source
193b5 20 64 61 74 61 62 61 73 65 2c 20 71 75 65 72 79 database, query
193b6 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 6f 75 72 the. ** sour
193b7 63 65 20 70 61 67 65 72 20 66 6f 72 20 74 68 65 ce pager for the
193b8 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
193b9 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
193ba 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
193bb 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
193bc 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
193bd 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 ite3PagerPagecou
193be 6e 74 28 70 53 72 63 50 61 67 65 72 2c 20 26 6e nt(pSrcPager, &n
193bf 53 72 63 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a SrcPage);. }.
193c0 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 28 6e for(ii=0; (n
193c1 50 61 67 65 3c 30 20 7c 7c 20 69 69 3c 6e 50 61 Page<0 || ii<nPa
193c2 67 65 29 20 26 26 20 70 2d 3e 69 4e 65 78 74 3c ge) && p->iNext<
193c3 3d 28 50 67 6e 6f 29 6e 53 72 63 50 61 67 65 20 =(Pgno)nSrcPage
193c4 26 26 20 21 72 63 3b 20 69 69 2b 2b 29 7b 0a 20 && !rc; ii++){.
193c5 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 const Pgno
193c6 69 53 72 63 50 67 20 3d 20 70 2d 3e 69 4e 65 78 iSrcPg = p->iNex
193c7 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
193c8 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 /* Source pag
193c9 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 e number */.
193ca 20 20 69 66 28 20 69 53 72 63 50 67 21 3d 50 45 if( iSrcPg!=PE
193cb 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
193cc 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 20 29 7b p->pSrc->pBt) ){
193cd 0a 20 20 20 20 20 20 20 20 44 62 50 61 67 65 20 . DbPage
193ce 2a 70 53 72 63 50 67 3b 20 20 20 20 20 20 20 20 *pSrcPg;
193cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
193d0 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 /* Source p
193d1 61 67 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 age object */.
193d2 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
193d3 65 33 50 61 67 65 72 47 65 74 28 70 53 72 63 50 e3PagerGet(pSrcP
193d4 61 67 65 72 2c 20 69 53 72 63 50 67 2c 20 26 70 ager, iSrcPg, &p
193d5 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 SrcPg);.
193d6 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
193d7 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 K ){. r
193d8 63 20 3d 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 c = backupOnePag
193d9 65 28 70 2c 20 69 53 72 63 50 67 2c 20 73 71 6c e(p, iSrcPg, sql
193da 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 ite3PagerGetData
193db 28 70 53 72 63 50 67 29 29 3b 0a 20 20 20 20 20 (pSrcPg));.
193dc 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
193dd 72 55 6e 72 65 66 28 70 53 72 63 50 67 29 3b 0a rUnref(pSrcPg);.
193de 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
193df 7d 0a 20 20 20 20 20 20 70 2d 3e 69 4e 65 78 74 }. p->iNext
193e0 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ++;. }. if
193e1 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
193e2 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 50 61 67 ){. p->nPag
193e3 65 63 6f 75 6e 74 20 3d 20 6e 53 72 63 50 61 67 ecount = nSrcPag
193e4 65 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 6d e;. p->nRem
193e5 61 69 6e 69 6e 67 20 3d 20 6e 53 72 63 50 61 67 aining = nSrcPag
193e6 65 2b 31 2d 70 2d 3e 69 4e 65 78 74 3b 0a 20 20 e+1-p->iNext;.
193e7 20 20 20 20 69 66 28 20 70 2d 3e 69 4e 65 78 74 if( p->iNext
193e8 3e 28 50 67 6e 6f 29 6e 53 72 63 50 61 67 65 20 >(Pgno)nSrcPage
193e9 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
193ea 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 SQLITE_DONE;.
193eb 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 2d }else if( !p-
193ec 3e 69 73 41 74 74 61 63 68 65 64 20 29 7b 0a 20 >isAttached ){.
193ed 20 20 20 20 20 20 20 61 74 74 61 63 68 42 61 63 attachBac
193ee 6b 75 70 4f 62 6a 65 63 74 28 70 29 3b 0a 20 20 kupObject(p);.
193ef 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 }. }. .
193f0 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 /* Update the
193f1 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 schema version
193f2 66 69 65 6c 64 20 69 6e 20 74 68 65 20 64 65 73 field in the des
193f3 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 tination databas
193f4 65 2e 20 54 68 69 73 0a 20 20 20 20 2a 2a 20 69 e. This. ** i
193f5 73 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 s to make sure t
193f6 68 61 74 20 74 68 65 20 73 63 68 65 6d 61 2d 76 hat the schema-v
193f7 65 72 73 69 6f 6e 20 72 65 61 6c 6c 79 20 64 6f ersion really do
193f8 65 73 20 63 68 61 6e 67 65 20 69 6e 0a 20 20 20 es change in.
193f9 20 2a 2a 20 74 68 65 20 63 61 73 65 20 77 68 65 ** the case whe
193fa 72 65 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e re the source an
193fb 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 d destination da
193fc 74 61 62 61 73 65 73 20 68 61 76 65 20 74 68 65 tabases have the
193fd 0a 20 20 20 20 2a 2a 20 73 61 6d 65 20 73 63 68 . ** same sch
193fe 65 6d 61 20 76 65 72 73 69 6f 6e 2e 0a 20 20 20 ema version..
193ff 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d */. if( rc==
19400 53 51 4c 49 54 45 5f 44 4f 4e 45 20 0a 20 20 20 SQLITE_DONE .
19401 20 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74 && (rc = sqlit
19402 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 e3BtreeUpdateMet
19403 61 28 70 2d 3e 70 44 65 73 74 2c 31 2c 70 2d 3e a(p->pDest,1,p->
19404 69 44 65 73 74 53 63 68 65 6d 61 2b 31 29 29 3d iDestSchema+1))=
19405 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 20 20 29 =SQLITE_OK. )
19406 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e {. const in
19407 74 20 6e 53 72 63 50 61 67 65 73 69 7a 65 20 3d t nSrcPagesize =
19408 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
19409 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 53 72 63 PageSize(p->pSrc
1940a 29 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 );. const i
1940b 6e 74 20 6e 44 65 73 74 50 61 67 65 73 69 7a 65 nt nDestPagesize
1940c 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 = sqlite3BtreeG
1940d 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 44 etPageSize(p->pD
1940e 65 73 74 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 est);. int
1940f 6e 44 65 73 74 54 72 75 6e 63 61 74 65 3b 0a 20 nDestTruncate;.
19410 20 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 . if( p->p
19411 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 20 20 DestDb ){.
19412 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e sqlite3ResetIn
19413 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 70 2d 3e ternalSchema(p->
19414 70 44 65 73 74 44 62 2c 20 30 29 3b 0a 20 20 20 pDestDb, 0);.
19415 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 }.. /* S
19416 65 74 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 et nDestTruncate
19417 20 74 6f 20 74 68 65 20 66 69 6e 61 6c 20 6e 75 to the final nu
19418 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
19419 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
1941a 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 . ** databa
1941b 73 65 2e 20 54 68 65 20 63 6f 6d 70 6c 69 63 61 se. The complica
1941c 74 69 6f 6e 20 68 65 72 65 20 69 73 20 74 68 61 tion here is tha
1941d 74 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f t the destinatio
1941e 6e 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 n page. **
1941f 73 69 7a 65 20 6d 61 79 20 62 65 20 64 69 66 66 size may be diff
19420 65 72 65 6e 74 20 74 6f 20 74 68 65 20 73 6f 75 erent to the sou
19421 72 63 65 20 70 61 67 65 20 73 69 7a 65 2e 20 0a rce page size. .
19422 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
19423 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 * If the source
19424 70 61 67 65 20 73 69 7a 65 20 69 73 20 73 6d 61 page size is sma
19425 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 64 65 ller than the de
19426 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 20 73 stination page s
19427 69 7a 65 2c 20 0a 20 20 20 20 20 20 2a 2a 20 72 ize, . ** r
19428 6f 75 6e 64 20 75 70 2e 20 49 6e 20 74 68 69 73 ound up. In this
19429 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c 20 74 case the call t
1942a 6f 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 o sqlite3OsTrunc
1942b 61 74 65 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c ate() below will
1942c 0a 20 20 20 20 20 20 2a 2a 20 66 69 78 20 74 68 . ** fix th
1942d 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 e size of the fi
1942e 6c 65 2e 20 48 6f 77 65 76 65 72 20 69 74 20 69 le. However it i
1942f 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 63 s important to c
19430 61 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c all. ** sql
19431 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 ite3PagerTruncat
19432 65 49 6d 61 67 65 28 29 20 68 65 72 65 20 73 6f eImage() here so
19433 20 74 68 61 74 20 61 6e 79 20 70 61 67 65 73 20 that any pages
19434 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a in the . **
19435 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 66 69 6c destination fil
19436 65 20 74 68 61 74 20 6c 69 65 20 62 65 79 6f 6e e that lie beyon
19437 64 20 74 68 65 20 6e 44 65 73 74 54 72 75 6e 63 d the nDestTrunc
19438 61 74 65 20 70 61 67 65 20 6d 61 72 6b 20 61 72 ate page mark ar
19439 65 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e e. ** journ
1943a 61 6c 6c 65 64 20 62 79 20 50 61 67 65 72 43 6f alled by PagerCo
1943b 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 62 mmitPhaseOne() b
1943c 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20 64 efore they are d
1943d 65 73 74 72 6f 79 65 64 0a 20 20 20 20 20 20 2a estroyed. *
1943e 2a 20 62 79 20 74 68 65 20 66 69 6c 65 20 74 72 * by the file tr
1943f 75 6e 63 61 74 69 6f 6e 2e 0a 20 20 20 20 20 20 uncation..
19440 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 53 72 */. if( nSr
19441 63 50 61 67 65 73 69 7a 65 3c 6e 44 65 73 74 50 cPagesize<nDestP
19442 61 67 65 73 69 7a 65 20 29 7b 0a 20 20 20 20 20 agesize ){.
19443 20 20 20 69 6e 74 20 72 61 74 69 6f 20 3d 20 6e int ratio = n
19444 44 65 73 74 50 61 67 65 73 69 7a 65 2f 6e 53 72 DestPagesize/nSr
19445 63 50 61 67 65 73 69 7a 65 3b 0a 20 20 20 20 20 cPagesize;.
19446 20 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 nDestTruncate
19447 20 3d 20 28 6e 53 72 63 50 61 67 65 2b 72 61 74 = (nSrcPage+rat
19448 69 6f 2d 31 29 2f 72 61 74 69 6f 3b 0a 20 20 20 io-1)/ratio;.
19449 20 20 20 20 20 69 66 28 20 6e 44 65 73 74 54 72 if( nDestTr
1944a 75 6e 63 61 74 65 3d 3d 28 69 6e 74 29 50 45 4e uncate==(int)PEN
1944b 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
1944c 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 20 29 7b ->pDest->pBt) ){
1944d 0a 20 20 20 20 20 20 20 20 20 20 6e 44 65 73 74 . nDest
1944e 54 72 75 6e 63 61 74 65 2d 2d 3b 0a 20 20 20 20 Truncate--;.
1944f 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
19450 65 7b 0a 20 20 20 20 20 20 20 20 6e 44 65 73 74 e{. nDest
19451 54 72 75 6e 63 61 74 65 20 3d 20 6e 53 72 63 50 Truncate = nSrcP
19452 61 67 65 20 2a 20 28 6e 53 72 63 50 61 67 65 73 age * (nSrcPages
19453 69 7a 65 2f 6e 44 65 73 74 50 61 67 65 73 69 7a ize/nDestPagesiz
19454 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 e);. }.
19455 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 sqlite3PagerTr
19456 75 6e 63 61 74 65 49 6d 61 67 65 28 70 44 65 73 uncateImage(pDes
19457 74 50 61 67 65 72 2c 20 6e 44 65 73 74 54 72 75 tPager, nDestTru
19458 6e 63 61 74 65 29 3b 0a 0a 20 20 20 20 20 20 69 ncate);.. i
19459 66 28 20 6e 53 72 63 50 61 67 65 73 69 7a 65 3c f( nSrcPagesize<
1945a 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20 29 7b nDestPagesize ){
1945b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 . /* If t
1945c 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 2d 73 he source page-s
1945d 69 7a 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 ize is smaller t
1945e 68 61 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 han the destinat
1945f 69 6f 6e 20 70 61 67 65 2d 73 69 7a 65 2c 0a 20 ion page-size,.
19460 20 20 20 20 20 20 20 2a 2a 20 74 77 6f 20 65 78 ** two ex
19461 74 72 61 20 74 68 69 6e 67 73 20 6d 61 79 20 6e tra things may n
19462 65 65 64 20 74 6f 20 68 61 70 70 65 6e 3a 0a 20 eed to happen:.
19463 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 **.
19464 20 20 2a 2a 20 20 20 2a 20 54 68 65 20 64 65 73 ** * The des
19465 74 69 6e 61 74 69 6f 6e 20 6d 61 79 20 6e 65 65 tination may nee
19466 64 20 74 6f 20 62 65 20 74 72 75 6e 63 61 74 65 d to be truncate
19467 64 2c 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a d, and. *
19468 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2a *. ** *
19469 20 44 61 74 61 20 73 74 6f 72 65 64 20 6f 6e 20 Data stored on
1946a 74 68 65 20 70 61 67 65 73 20 69 6d 6d 65 64 69 the pages immedi
1946b 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 ately following
1946c 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 the . **
1946d 20 20 20 20 70 65 6e 64 69 6e 67 2d 62 79 74 65 pending-byte
1946e 20 70 61 67 65 20 69 6e 20 74 68 65 20 73 6f 75 page in the sou
1946f 72 63 65 20 64 61 74 61 62 61 73 65 20 6d 61 79 rce database may
19470 20 6e 65 65 64 20 74 6f 20 62 65 0a 20 20 20 20 need to be.
19471 20 20 20 20 2a 2a 20 20 20 20 20 63 6f 70 69 65 ** copie
19472 64 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69 d into the desti
19473 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e nation database.
19474 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
19475 20 20 20 20 63 6f 6e 73 74 20 69 36 34 20 69 53 const i64 iS
19476 69 7a 65 20 3d 20 28 69 36 34 29 6e 53 72 63 50 ize = (i64)nSrcP
19477 61 67 65 73 69 7a 65 20 2a 20 28 69 36 34 29 6e agesize * (i64)n
19478 53 72 63 50 61 67 65 3b 0a 20 20 20 20 20 20 20 SrcPage;.
19479 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 20 sqlite3_file *
1947a 63 6f 6e 73 74 20 70 46 69 6c 65 20 3d 20 73 71 const pFile = sq
1947b 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 70 lite3PagerFile(p
1947c 44 65 73 74 50 61 67 65 72 29 3b 0a 0a 20 20 20 DestPager);..
1947d 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 assert( pFi
1947e 6c 65 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 le );. as
1947f 73 65 72 74 28 20 28 69 36 34 29 6e 44 65 73 74 sert( (i64)nDest
19480 54 72 75 6e 63 61 74 65 2a 28 69 36 34 29 6e 44 Truncate*(i64)nD
19481 65 73 74 50 61 67 65 73 69 7a 65 20 3e 3d 20 69 estPagesize >= i
19482 53 69 7a 65 20 7c 7c 20 28 0a 20 20 20 20 20 20 Size || (.
19483 20 20 20 20 20 20 20 20 6e 44 65 73 74 54 72 75 nDestTru
19484 6e 63 61 74 65 3d 3d 28 69 6e 74 29 28 50 45 4e ncate==(int)(PEN
19485 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
19486 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 2d 31 29 ->pDest->pBt)-1)
19487 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 69 . && i
19488 53 69 7a 65 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 Size>=PENDING_BY
19489 54 45 20 26 26 20 69 53 69 7a 65 3c 3d 50 45 4e TE && iSize<=PEN
1948a 44 49 4e 47 5f 42 59 54 45 2b 6e 44 65 73 74 50 DING_BYTE+nDestP
1948b 61 67 65 73 69 7a 65 0a 20 20 20 20 20 20 20 20 agesize.
1948c 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ));. if(
1948d 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d SQLITE_OK==(rc =
1948e 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d sqlite3PagerCom
1948f 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 44 65 73 mitPhaseOne(pDes
19490 74 50 61 67 65 72 2c 20 30 2c 20 31 29 29 0a 20 tPager, 0, 1)).
19491 20 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 54 && SQLIT
19492 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 62 61 63 6b E_OK==(rc = back
19493 75 70 54 72 75 6e 63 61 74 65 46 69 6c 65 28 70 upTruncateFile(p
19494 46 69 6c 65 2c 20 69 53 69 7a 65 29 29 0a 20 20 File, iSize)).
19495 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 54 45 && SQLITE
19496 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 _OK==(rc = sqlit
19497 65 33 50 61 67 65 72 53 79 6e 63 28 70 44 65 73 e3PagerSync(pDes
19498 74 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 tPager)).
19499 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 36 ){. i6
1949a 34 20 69 4f 66 66 3b 0a 20 20 20 20 20 20 20 20 4 iOff;.
1949b 20 20 69 36 34 20 69 45 6e 64 20 3d 20 4d 49 4e i64 iEnd = MIN
1949c 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 2b 20 (PENDING_BYTE +
1949d 6e 44 65 73 74 50 61 67 65 73 69 7a 65 2c 20 69 nDestPagesize, i
1949e 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Size);.
1949f 20 66 6f 72 28 0a 20 20 20 20 20 20 20 20 20 20 for(.
194a0 20 20 69 4f 66 66 3d 50 45 4e 44 49 4e 47 5f 42 iOff=PENDING_B
194a1 59 54 45 2b 6e 53 72 63 50 61 67 65 73 69 7a 65 YTE+nSrcPagesize
194a2 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 ; . r
194a3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
194a4 69 4f 66 66 3c 69 45 6e 64 3b 20 0a 20 20 20 20 iOff<iEnd; .
194a5 20 20 20 20 20 20 20 20 69 4f 66 66 2b 3d 6e 53 iOff+=nS
194a6 72 63 50 61 67 65 73 69 7a 65 0a 20 20 20 20 20 rcPagesize.
194a7 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 ){.
194a8 20 20 20 20 50 67 48 64 72 20 2a 70 53 72 63 50 PgHdr *pSrcP
194a9 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 g = 0;.
194aa 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53 const Pgno iS
194ab 72 63 50 67 20 3d 20 28 50 67 6e 6f 29 28 28 69 rcPg = (Pgno)((i
194ac 4f 66 66 2f 6e 53 72 63 50 61 67 65 73 69 7a 65 Off/nSrcPagesize
194ad 29 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 )+1);.
194ae 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
194af 67 65 72 47 65 74 28 70 53 72 63 50 61 67 65 72 gerGet(pSrcPager
194b0 2c 20 69 53 72 63 50 67 2c 20 26 70 53 72 63 50 , iSrcPg, &pSrcP
194b1 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 g);.
194b2 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
194b3 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 K ){.
194b4 20 20 20 75 38 20 2a 7a 44 61 74 61 20 3d 20 73 u8 *zData = s
194b5 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
194b6 74 61 28 70 53 72 63 50 67 29 3b 0a 20 20 20 20 ta(pSrcPg);.
194b7 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
194b8 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 46 qlite3OsWrite(pF
194b9 69 6c 65 2c 20 7a 44 61 74 61 2c 20 6e 53 72 63 ile, zData, nSrc
194ba 50 61 67 65 73 69 7a 65 2c 20 69 4f 66 66 29 3b Pagesize, iOff);
194bb 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 . }.
194bc 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
194bd 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 53 72 e3PagerUnref(pSr
194be 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 cPg);.
194bf 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
194c0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
194c1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
194c2 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 erCommitPhaseOne
194c3 28 70 44 65 73 74 50 61 67 65 72 2c 20 30 2c 20 (pDestPager, 0,
194c4 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 0);. }. .
194c5 20 20 20 20 20 2f 2a 20 46 69 6e 69 73 68 20 63 /* Finish c
194c6 6f 6d 6d 69 74 74 69 6e 67 20 74 68 65 20 74 72 ommitting the tr
194c7 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 74 68 65 ansaction to the
194c8 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 destination dat
194c9 61 62 61 73 65 2e 20 2a 2f 0a 20 20 20 20 20 20 abase. */.
194ca 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 if( SQLITE_OK==r
194cb 63 0a 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 c. && SQLI
194cc 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c TE_OK==(rc = sql
194cd 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 ite3BtreeCommitP
194ce 68 61 73 65 54 77 6f 28 70 2d 3e 70 44 65 73 74 haseTwo(p->pDest
194cf 29 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 )). ){.
194d0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
194d1 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 DONE;. }.
194d2 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 }. . /* If
194d3 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 69 73 20 bCloseTrans is
194d4 74 72 75 65 2c 20 74 68 65 6e 20 74 68 69 73 20 true, then this
194d5 66 75 6e 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20 function opened
194d6 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 a read transacti
194d7 6f 6e 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 on. ** on the
194d8 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 source database
194d9 2e 20 43 6c 6f 73 65 20 74 68 65 20 72 65 61 64 . Close the read
194da 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 65 72 transaction her
194db 65 2e 20 54 68 65 72 65 20 69 73 0a 20 20 20 20 e. There is.
194dc 2a 2a 20 6e 6f 20 6e 65 65 64 20 74 6f 20 63 68 ** no need to ch
194dd 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 76 eck the return v
194de 61 6c 75 65 73 20 6f 66 20 74 68 65 20 62 74 72 alues of the btr
194df 65 65 20 6d 65 74 68 6f 64 73 20 68 65 72 65 2c ee methods here,
194e0 20 61 73 0a 20 20 20 20 2a 2a 20 22 63 6f 6d 6d as. ** "comm
194e1 69 74 74 69 6e 67 22 20 61 20 72 65 61 64 2d 6f itting" a read-o
194e2 6e 6c 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 nly transaction
194e3 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 0a 20 20 20 cannot fail..
194e4 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 43 6c 6f */. if( bClo
194e5 73 65 54 72 61 6e 73 20 29 7b 0a 20 20 20 20 20 seTrans ){.
194e6 20 54 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20 72 TESTONLY( int r
194e7 63 32 20 29 3b 0a 20 20 20 20 20 20 54 45 53 54 c2 );. TEST
194e8 4f 4e 4c 59 28 20 72 63 32 20 20 3d 20 29 20 73 ONLY( rc2 = ) s
194e9 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
194ea 74 50 68 61 73 65 4f 6e 65 28 70 2d 3e 70 53 72 tPhaseOne(p->pSr
194eb 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 54 45 53 c, 0);. TES
194ec 54 4f 4e 4c 59 28 20 72 63 32 20 7c 3d 20 29 20 TONLY( rc2 |= )
194ed 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
194ee 69 74 50 68 61 73 65 54 77 6f 28 70 2d 3e 70 53 itPhaseTwo(p->pS
194ef 72 63 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 rc);. asser
194f0 74 28 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 4f t( rc2==SQLITE_O
194f1 4b 20 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 K );. }. .
194f2 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 p->rc = rc;.
194f3 7d 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 }. if( p->pDest
194f4 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 Db ){. sqlite
194f5 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3_mutex_leave(p-
194f6 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 >pDestDb->mutex)
194f7 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 ;. }. sqlite3B
194f8 74 72 65 65 4c 65 61 76 65 28 70 2d 3e 70 53 72 treeLeave(p->pSr
194f9 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 c);. sqlite3_mu
194fa 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 53 72 tex_leave(p->pSr
194fb 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 cDb->mutex);. r
194fc 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
194fd 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 72 ** Release all r
194fe 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61 esources associa
194ff 74 65 64 20 77 69 74 68 20 61 6e 20 73 71 6c 69 ted with an sqli
19500 74 65 33 5f 62 61 63 6b 75 70 2a 20 68 61 6e 64 te3_backup* hand
19501 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 le..*/.SQLITE_AP
19502 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 I int sqlite3_ba
19503 63 6b 75 70 5f 66 69 6e 69 73 68 28 73 71 6c 69 ckup_finish(sqli
19504 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a te3_backup *p){.
19505 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 sqlite3_backup
19506 20 2a 2a 70 70 3b 20 20 20 20 20 20 20 20 20 20 **pp;
19507 20 20 20 20 20 20 20 2f 2a 20 50 74 72 20 74 6f /* Ptr to
19508 20 68 65 61 64 20 6f 66 20 70 61 67 65 72 73 20 head of pagers
19509 62 61 63 6b 75 70 20 6c 69 73 74 20 2a 2f 0a 20 backup list */.
1950a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
1950b 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 mutex;
1950c 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 /* Mutex t
1950d 6f 20 70 72 6f 74 65 63 74 20 73 6f 75 72 63 65 o protect source
1950e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 database */. i
1950f 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
19510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19511 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 /* Value to
19512 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 return */.. /*
19513 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 65 Enter the mutexe
19514 73 20 2a 2f 0a 20 20 69 66 28 20 70 3d 3d 30 20 s */. if( p==0
19515 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
19516 4f 4b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 OK;. sqlite3_mu
19517 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 53 72 tex_enter(p->pSr
19518 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 cDb->mutex);. s
19519 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1951a 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 6d 75 74 (p->pSrc);. mut
1951b 65 78 20 3d 20 70 2d 3e 70 53 72 63 44 62 2d 3e ex = p->pSrcDb->
1951c 6d 75 74 65 78 3b 0a 20 20 69 66 28 20 70 2d 3e mutex;. if( p->
1951d 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 pDestDb ){. s
1951e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
1951f 65 72 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d er(p->pDestDb->m
19520 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a utex);. }.. /*
19521 20 44 65 74 61 63 68 20 74 68 69 73 20 62 61 63 Detach this bac
19522 6b 75 70 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 kup from the sou
19523 72 63 65 20 70 61 67 65 72 2e 20 2a 2f 0a 20 20 rce pager. */.
19524 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 if( p->pDestDb )
19525 7b 0a 20 20 20 20 70 2d 3e 70 53 72 63 2d 3e 6e {. p->pSrc->n
19526 42 61 63 6b 75 70 2d 2d 3b 0a 20 20 7d 0a 20 20 Backup--;. }.
19527 69 66 28 20 70 2d 3e 69 73 41 74 74 61 63 68 65 if( p->isAttache
19528 64 20 29 7b 0a 20 20 20 20 70 70 20 3d 20 73 71 d ){. pp = sq
19529 6c 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 lite3PagerBackup
1952a 50 74 72 28 73 71 6c 69 74 65 33 42 74 72 65 65 Ptr(sqlite3Btree
1952b 50 61 67 65 72 28 70 2d 3e 70 53 72 63 29 29 3b Pager(p->pSrc));
1952c 0a 20 20 20 20 77 68 69 6c 65 28 20 2a 70 70 21 . while( *pp!
1952d 3d 70 20 29 7b 0a 20 20 20 20 20 20 70 70 20 3d =p ){. pp =
1952e 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a &(*pp)->pNext;.
1952f 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 20 3d 20 }. *pp =
19530 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20 p->pNext;. }..
19531 20 2f 2a 20 49 66 20 61 20 74 72 61 6e 73 61 63 /* If a transac
19532 74 69 6f 6e 20 69 73 20 73 74 69 6c 6c 20 6f 70 tion is still op
19533 65 6e 20 6f 6e 20 74 68 65 20 42 74 72 65 65 2c en on the Btree,
19534 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 2a roll it back. *
19535 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 /. sqlite3Btree
19536 52 6f 6c 6c 62 61 63 6b 28 70 2d 3e 70 44 65 73 Rollback(p->pDes
19537 74 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 t);.. /* Set th
19538 65 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 e error code of
19539 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
1953a 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e database handle.
1953b 20 2a 2f 0a 20 20 72 63 20 3d 20 28 70 2d 3e 72 */. rc = (p->r
1953c 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 29 20 c==SQLITE_DONE)
1953d 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 70 2d ? SQLITE_OK : p-
1953e 3e 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 45 72 >rc;. sqlite3Er
1953f 72 6f 72 28 70 2d 3e 70 44 65 73 74 44 62 2c 20 ror(p->pDestDb,
19540 72 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 45 78 rc, 0);.. /* Ex
19541 69 74 20 74 68 65 20 6d 75 74 65 78 65 73 20 61 it the mutexes a
19542 6e 64 20 66 72 65 65 20 74 68 65 20 62 61 63 6b nd free the back
19543 75 70 20 63 6f 6e 74 65 78 74 20 73 74 72 75 63 up context struc
19544 74 75 72 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 ture. */. if( p
19545 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 ->pDestDb ){.
19546 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
19547 65 61 76 65 28 70 2d 3e 70 44 65 73 74 44 62 2d eave(p->pDestDb-
19548 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 73 >mutex);. }. s
19549 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
1954a 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69 66 28 (p->pSrc);. if(
1954b 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 p->pDestDb ){.
1954c 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
1954d 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 p);. }. sqlite
1954e 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 3_mutex_leave(mu
1954f 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 tex);. return r
19550 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 c;.}../*.** Retu
19551 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
19552 20 70 61 67 65 73 20 73 74 69 6c 6c 20 74 6f 20 pages still to
19553 62 65 20 62 61 63 6b 65 64 20 75 70 20 61 73 20 be backed up as
19554 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 of the most rece
19555 6e 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 nt.** call to sq
19556 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
19557 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 p()..*/.SQLITE_A
19558 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
19559 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 ackup_remaining(
1955a 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
1955b 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e p){. return p->
1955c 6e 52 65 6d 61 69 6e 69 6e 67 3b 0a 7d 0a 0a 2f nRemaining;.}../
1955d 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1955e 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
1955f 70 61 67 65 73 20 69 6e 20 74 68 65 20 73 6f 75 pages in the sou
19560 72 63 65 20 64 61 74 61 62 61 73 65 20 61 73 20 rce database as
19561 6f 66 20 74 68 65 20 6d 6f 73 74 20 0a 2a 2a 20 of the most .**
19562 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 recent call to s
19563 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
19564 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ep()..*/.SQLITE_
19565 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
19566 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 backup_pagecount
19567 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
19568 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d *p){. return p-
19569 3e 6e 50 61 67 65 63 6f 75 6e 74 3b 0a 7d 0a 0a >nPagecount;.}..
1956a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
1956b 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 ion is called af
1956c 74 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ter the contents
1956d 20 6f 66 20 70 61 67 65 20 69 50 61 67 65 20 6f of page iPage o
1956e 66 20 74 68 65 0a 2a 2a 20 73 6f 75 72 63 65 20 f the.** source
1956f 64 61 74 61 62 61 73 65 20 68 61 76 65 20 62 65 database have be
19570 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 49 66 20 en modified. If
19571 70 61 67 65 20 69 50 61 67 65 20 68 61 73 20 61 page iPage has a
19572 6c 72 65 61 64 79 20 62 65 65 6e 20 0a 2a 2a 20 lready been .**
19573 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 copied into the
19574 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 destination data
19575 62 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 64 base, then the d
19576 61 74 61 20 77 72 69 74 74 65 6e 20 74 6f 20 74 ata written to t
19577 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f he.** destinatio
19578 6e 20 69 73 20 6e 6f 77 20 69 6e 76 61 6c 69 64 n is now invalid
19579 61 74 65 64 2e 20 54 68 65 20 64 65 73 74 69 6e ated. The destin
1957a 61 74 69 6f 6e 20 63 6f 70 79 20 6f 66 20 69 50 ation copy of iP
1957b 61 67 65 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 age needs.** to
1957c 62 65 20 75 70 64 61 74 65 64 20 77 69 74 68 20 be updated with
1957d 74 68 65 20 6e 65 77 20 64 61 74 61 20 62 65 66 the new data bef
1957e 6f 72 65 20 74 68 65 20 62 61 63 6b 75 70 20 6f ore the backup o
1957f 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 peration is.** c
19580 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 omplete..**.** I
19581 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 t is assumed tha
19582 74 20 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f t the mutex asso
19583 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
19584 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 0a BtShared object.
19585 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 ** corresponding
19586 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 64 to the source d
19587 61 74 61 62 61 73 65 20 69 73 20 68 65 6c 64 20 atabase is held
19588 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 when this functi
19589 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e on is.** called.
1958a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1958b 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
1958c 61 63 6b 75 70 55 70 64 61 74 65 28 73 71 6c 69 ackupUpdate(sqli
1958d 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 te3_backup *pBac
1958e 6b 75 70 2c 20 50 67 6e 6f 20 69 50 61 67 65 2c kup, Pgno iPage,
1958f 20 63 6f 6e 73 74 20 75 38 20 2a 61 44 61 74 61 const u8 *aData
19590 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 ){. sqlite3_bac
19591 6b 75 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20 kup *p;
19592 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 /* Ite
19593 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a rator variable *
19594 2f 0a 20 20 66 6f 72 28 70 3d 70 42 61 63 6b 75 /. for(p=pBacku
19595 70 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 p; p; p=p->pNext
19596 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 ){. assert( s
19597 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
19598 64 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e d(p->pSrc->pBt->
19599 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 69 66 mutex) );. if
1959a 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28 ( !isFatalError(
1959b 70 2d 3e 72 63 29 20 26 26 20 69 50 61 67 65 3c p->rc) && iPage<
1959c 70 2d 3e 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 p->iNext ){.
1959d 20 20 2f 2a 20 54 68 65 20 62 61 63 6b 75 70 20 /* The backup
1959e 70 72 6f 63 65 73 73 20 70 20 68 61 73 20 61 6c process p has al
1959f 72 65 61 64 79 20 63 6f 70 69 65 64 20 70 61 67 ready copied pag
195a0 65 20 69 50 61 67 65 2e 20 42 75 74 20 6e 6f 77 e iPage. But now
195a1 20 69 74 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 it. ** has
195a2 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 62 been modified b
195a3 79 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 y a transaction
195a4 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 on the source pa
195a5 67 65 72 2e 20 43 6f 70 79 0a 20 20 20 20 20 20 ger. Copy.
195a6 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61 20 ** the new data
195a7 69 6e 74 6f 20 74 68 65 20 62 61 63 6b 75 70 2e into the backup.
195a8 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
195a9 69 6e 74 20 72 63 20 3d 20 62 61 63 6b 75 70 4f int rc = backupO
195aa 6e 65 50 61 67 65 28 70 2c 20 69 50 61 67 65 2c nePage(p, iPage,
195ab 20 61 44 61 74 61 29 3b 0a 20 20 20 20 20 20 61 aData);. a
195ac 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
195ad 45 5f 42 55 53 59 20 26 26 20 72 63 21 3d 53 51 E_BUSY && rc!=SQ
195ae 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29 3b 0a 20 LITE_LOCKED );.
195af 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
195b0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
195b1 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 p->rc = rc;.
195b2 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
195b3 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 61 72 74 }../*.** Restart
195b4 20 74 68 65 20 62 61 63 6b 75 70 20 70 72 6f 63 the backup proc
195b5 65 73 73 2e 20 54 68 69 73 20 69 73 20 63 61 6c ess. This is cal
195b6 6c 65 64 20 77 68 65 6e 20 74 68 65 20 70 61 67 led when the pag
195b7 65 72 20 6c 61 79 65 72 0a 2a 2a 20 64 65 74 65 er layer.** dete
195b8 63 74 73 20 74 68 61 74 20 74 68 65 20 64 61 74 cts that the dat
195b9 61 62 61 73 65 20 68 61 73 20 62 65 65 6e 20 6d abase has been m
195ba 6f 64 69 66 69 65 64 20 62 79 20 61 6e 20 65 78 odified by an ex
195bb 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 0a ternal database.
195bc 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 ** connection. I
195bd 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 n this case ther
195be 65 20 69 73 20 6e 6f 20 77 61 79 20 6f 66 20 6b e is no way of k
195bf 6e 6f 77 69 6e 67 20 77 68 69 63 68 20 6f 66 20 nowing which of
195c0 74 68 65 0a 2a 2a 20 70 61 67 65 73 20 74 68 61 the.** pages tha
195c1 74 20 68 61 76 65 20 62 65 65 6e 20 63 6f 70 69 t have been copi
195c2 65 64 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 ed into the dest
195c3 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 ination database
195c4 20 61 72 65 20 73 74 69 6c 6c 20 0a 2a 2a 20 76 are still .** v
195c5 61 6c 69 64 20 61 6e 64 20 77 68 69 63 68 20 61 alid and which a
195c6 72 65 20 6e 6f 74 2c 20 73 6f 20 74 68 65 20 65 re not, so the e
195c7 6e 74 69 72 65 20 70 72 6f 63 65 73 73 20 6e 65 ntire process ne
195c8 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 61 72 eds to be restar
195c9 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 ted..**.** It is
195ca 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 assumed that th
195cb 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 e mutex associat
195cc 65 64 20 77 69 74 68 20 74 68 65 20 42 74 53 68 ed with the BtSh
195cd 61 72 65 64 20 6f 62 6a 65 63 74 0a 2a 2a 20 63 ared object.** c
195ce 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 orresponding to
195cf 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 the source datab
195d0 61 73 65 20 69 73 20 68 65 6c 64 20 77 68 65 6e ase is held when
195d1 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
195d2 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a s.** called..*/.
195d3 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
195d4 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75 oid sqlite3Backu
195d5 70 52 65 73 74 61 72 74 28 73 71 6c 69 74 65 33 pRestart(sqlite3
195d6 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 70 _backup *pBackup
195d7 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 ){. sqlite3_bac
195d8 6b 75 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20 kup *p;
195d9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 /* Ite
195da 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a rator variable *
195db 2f 0a 20 20 66 6f 72 28 70 3d 70 42 61 63 6b 75 /. for(p=pBacku
195dc 70 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 p; p; p=p->pNext
195dd 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 ){. assert( s
195de 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
195df 64 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e d(p->pSrc->pBt->
195e0 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 70 2d mutex) );. p-
195e1 3e 69 4e 65 78 74 20 3d 20 31 3b 0a 20 20 7d 0a >iNext = 1;. }.
195e2 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
195e3 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 0a 2f 2a E_OMIT_VACUUM./*
195e4 0a 2a 2a 20 43 6f 70 79 20 74 68 65 20 63 6f 6d .** Copy the com
195e5 70 6c 65 74 65 20 63 6f 6e 74 65 6e 74 20 6f 66 plete content of
195e6 20 70 42 74 46 72 6f 6d 20 69 6e 74 6f 20 70 42 pBtFrom into pB
195e7 74 54 6f 2e 20 20 41 20 74 72 61 6e 73 61 63 74 tTo. A transact
195e8 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 61 ion.** must be a
195e9 63 74 69 76 65 20 66 6f 72 20 62 6f 74 68 20 66 ctive for both f
195ea 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 iles..**.** The
195eb 73 69 7a 65 20 6f 66 20 66 69 6c 65 20 70 54 6f size of file pTo
195ec 20 6d 61 79 20 62 65 20 72 65 64 75 63 65 64 20 may be reduced
195ed 62 79 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f by this operatio
195ee 6e 2e 20 49 66 20 61 6e 79 74 68 69 6e 67 20 0a n. If anything .
195ef 2a 2a 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 74 ** goes wrong, t
195f0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f he transaction o
195f1 6e 20 70 54 6f 20 69 73 20 72 6f 6c 6c 65 64 20 n pTo is rolled
195f2 62 61 63 6b 2e 20 49 66 20 73 75 63 63 65 73 73 back. If success
195f3 66 75 6c 2c 20 74 68 65 20 0a 2a 2a 20 74 72 61 ful, the .** tra
195f4 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d nsaction is comm
195f5 69 74 74 65 64 20 62 65 66 6f 72 65 20 72 65 74 itted before ret
195f6 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 urning..*/.SQLIT
195f7 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
195f8 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 lite3BtreeCopyFi
195f9 6c 65 28 42 74 72 65 65 20 2a 70 54 6f 2c 20 42 le(Btree *pTo, B
195fa 74 72 65 65 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 tree *pFrom){.
195fb 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 int rc;. sqlite
195fc 33 5f 62 61 63 6b 75 70 20 62 3b 0a 20 20 73 71 3_backup b;. sq
195fd 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
195fe 70 54 6f 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 pTo);. sqlite3B
195ff 74 72 65 65 45 6e 74 65 72 28 70 46 72 6f 6d 29 treeEnter(pFrom)
19600 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 75 70 20 61 ;.. /* Set up a
19601 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 n sqlite3_backup
19602 20 6f 62 6a 65 63 74 2e 20 73 71 6c 69 74 65 33 object. sqlite3
19603 5f 62 61 63 6b 75 70 2e 70 44 65 73 74 44 62 20 _backup.pDestDb
19604 6d 75 73 74 20 62 65 20 73 65 74 0a 20 20 2a 2a must be set. **
19605 20 74 6f 20 30 2e 20 54 68 69 73 20 69 73 20 75 to 0. This is u
19606 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65 sed by the imple
19607 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 73 71 mentations of sq
19608 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
19609 70 28 29 0a 20 20 2a 2a 20 61 6e 64 20 73 71 6c p(). ** and sql
1960a 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 ite3_backup_fini
1960b 73 68 28 29 20 74 6f 20 64 65 74 65 63 74 20 74 sh() to detect t
1960c 68 61 74 20 74 68 65 79 20 61 72 65 20 62 65 69 hat they are bei
1960d 6e 67 20 63 61 6c 6c 65 64 0a 20 20 2a 2a 20 66 ng called. ** f
1960e 72 6f 6d 20 74 68 69 73 20 66 75 6e 63 74 69 6f rom this functio
1960f 6e 2c 20 6e 6f 74 20 64 69 72 65 63 74 6c 79 20 n, not directly
19610 62 79 20 74 68 65 20 75 73 65 72 2e 0a 20 20 2a by the user.. *
19611 2f 0a 20 20 6d 65 6d 73 65 74 28 26 62 2c 20 30 /. memset(&b, 0
19612 2c 20 73 69 7a 65 6f 66 28 62 29 29 3b 0a 20 20 , sizeof(b));.
19613 62 2e 70 53 72 63 44 62 20 3d 20 70 46 72 6f 6d b.pSrcDb = pFrom
19614 2d 3e 64 62 3b 0a 20 20 62 2e 70 53 72 63 20 3d ->db;. b.pSrc =
19615 20 70 46 72 6f 6d 3b 0a 20 20 62 2e 70 44 65 73 pFrom;. b.pDes
19616 74 20 3d 20 70 54 6f 3b 0a 20 20 62 2e 69 4e 65 t = pTo;. b.iNe
19617 78 74 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 30 78 xt = 1;.. /* 0x
19618 37 46 46 46 46 46 46 46 20 69 73 20 74 68 65 20 7FFFFFFF is the
19619 68 61 72 64 20 6c 69 6d 69 74 20 66 6f 72 20 74 hard limit for t
1961a 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
1961b 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 es in a database
1961c 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 42 79 20 70 . ** file. By p
1961d 61 73 73 69 6e 67 20 74 68 69 73 20 61 73 20 74 assing this as t
1961e 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
1961f 65 73 20 74 6f 20 63 6f 70 79 20 74 6f 0a 20 20 es to copy to.
19620 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ** sqlite3_backu
19621 70 5f 73 74 65 70 28 29 2c 20 77 65 20 63 61 6e p_step(), we can
19622 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 guarantee that
19623 74 68 65 20 63 6f 70 79 20 66 69 6e 69 73 68 65 the copy finishe
19624 73 20 0a 20 20 2a 2a 20 77 69 74 68 69 6e 20 61 s . ** within a
19625 20 73 69 6e 67 6c 65 20 63 61 6c 6c 20 28 75 6e single call (un
19626 6c 65 73 73 20 61 6e 20 65 72 72 6f 72 20 6f 63 less an error oc
19627 63 75 72 73 29 2e 20 54 68 65 20 61 73 73 65 72 curs). The asser
19628 74 28 29 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 t() statement.
19629 2a 2a 20 63 68 65 63 6b 73 20 74 68 69 73 20 61 ** checks this a
1962a 73 73 75 6d 70 74 69 6f 6e 20 2d 20 28 70 2d 3e ssumption - (p->
1962b 72 63 29 20 73 68 6f 75 6c 64 20 62 65 20 73 65 rc) should be se
1962c 74 20 74 6f 20 65 69 74 68 65 72 20 53 51 4c 49 t to either SQLI
1962d 54 45 5f 44 4f 4e 45 20 0a 20 20 2a 2a 20 6f 72 TE_DONE . ** or
1962e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a an error code..
1962f 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 */. sqlite3_b
19630 61 63 6b 75 70 5f 73 74 65 70 28 26 62 2c 20 30 ackup_step(&b, 0
19631 78 37 46 46 46 46 46 46 46 29 3b 0a 20 20 61 73 x7FFFFFFF);. as
19632 73 65 72 74 28 20 62 2e 72 63 21 3d 53 51 4c 49 sert( b.rc!=SQLI
19633 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d 20 TE_OK );. rc =
19634 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 sqlite3_backup_f
19635 69 6e 69 73 68 28 26 62 29 3b 0a 20 20 69 66 28 inish(&b);. if(
19636 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
19637 7b 0a 20 20 20 20 70 54 6f 2d 3e 70 42 74 2d 3e {. pTo->pBt->
19638 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 pageSizeFixed =
19639 30 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 0;. }.. sqlite
1963a 33 42 74 72 65 65 4c 65 61 76 65 28 70 46 72 6f 3BtreeLeave(pFro
1963b 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 m);. sqlite3Btr
1963c 65 65 4c 65 61 76 65 28 70 54 6f 29 3b 0a 20 20 eeLeave(pTo);.
1963d 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e return rc;.}.#en
1963e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1963f 49 54 5f 56 41 43 55 55 4d 20 2a 2f 0a 0a 2f 2a IT_VACUUM */../*
19640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
19641 64 20 6f 66 20 62 61 63 6b 75 70 2e 63 20 2a 2a d of backup.c **
19642 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19643 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19644 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
19645 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
19646 67 69 6e 20 66 69 6c 65 20 76 64 62 65 6d 65 6d gin file vdbemem
19647 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
19648 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19649 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1964a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 36 0a .** 2004 May 26.
1964b 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
1964c 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
1964d 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
1964e 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
1964f 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
19650 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
19651 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
19652 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
19653 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
19654 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
19655 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
19656 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
19657 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
19658 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
19659 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
1965a 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
1965b 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
1965c 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
1965d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1965e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1965f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
19661 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
19662 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 ontains code use
19663 20 74 6f 20 6d 61 6e 69 70 75 6c 61 74 65 20 22 to manipulate "
19664 4d 65 6d 22 20 73 74 72 75 63 74 75 72 65 2e 20 Mem" structure.
19665 20 41 20 22 4d 65 6d 22 0a 2a 2a 20 73 74 6f 72 A "Mem".** stor
19666 65 73 20 61 20 73 69 6e 67 6c 65 20 76 61 6c 75 es a single valu
19667 65 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20 e in the VDBE.
19668 4d 65 6d 20 69 73 20 61 6e 20 6f 70 61 71 75 65 Mem is an opaque
19669 20 73 74 72 75 63 74 75 72 65 20 76 69 73 69 62 structure visib
1966a 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 77 69 74 68 69 le.** only withi
1966b 6e 20 74 68 65 20 56 44 42 45 2e 20 20 49 6e 74 n the VDBE. Int
1966c 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65 73 20 erface routines
1966d 72 65 66 65 72 20 74 6f 20 61 20 4d 65 6d 20 75 refer to a Mem u
1966e 73 69 6e 67 20 74 68 65 0a 2a 2a 20 6e 61 6d 65 sing the.** name
1966f 20 73 71 6c 69 74 65 5f 76 61 6c 75 65 0a 2a 2a sqlite_value.**
19670 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 6d 65 6d .** $Id: vdbemem
19671 2e 63 2c 76 20 31 2e 31 35 32 20 32 30 30 39 2f .c,v 1.152 2009/
19672 30 37 2f 32 32 20 31 38 3a 30 37 3a 34 31 20 64 07/22 18:07:41 d
19673 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a rh Exp $.*/../*.
19674 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 ** Call sqlite3V
19675 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 dbeMemExpandBlob
19676 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70 6c 69 () on the suppli
19677 65 64 20 76 61 6c 75 65 20 28 74 79 70 65 20 4d ed value (type M
19678 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 71 em*).** P if req
19679 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e uired..*/.#defin
1967a 65 20 65 78 70 61 6e 64 42 6c 6f 62 28 50 29 20 e expandBlob(P)
1967b 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d (((P)->flags&MEM
1967c 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 64 _Zero)?sqlite3Vd
1967d 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 beMemExpandBlob(
1967e 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 P):0)../*.** If
1967f 70 4d 65 6d 20 69 73 20 61 6e 20 6f 62 6a 65 63 pMem is an objec
19680 74 20 77 69 74 68 20 61 20 76 61 6c 69 64 20 73 t with a valid s
19681 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 tring representa
19682 74 69 6f 6e 2c 20 74 68 69 73 20 72 6f 75 74 69 tion, this routi
19683 6e 65 0a 2a 2a 20 65 6e 73 75 72 65 73 20 74 68 ne.** ensures th
19684 65 20 69 6e 74 65 72 6e 61 6c 20 65 6e 63 6f 64 e internal encod
19685 69 6e 67 20 66 6f 72 20 74 68 65 20 73 74 72 69 ing for the stri
19686 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ng representatio
19687 6e 20 69 73 0a 2a 2a 20 27 64 65 73 69 72 65 64 n is.** 'desired
19688 45 6e 63 27 2c 20 6f 6e 65 20 6f 66 20 53 51 4c Enc', one of SQL
19689 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 ITE_UTF8, SQLITE
1968a 5f 55 54 46 31 36 4c 45 20 6f 72 20 53 51 4c 49 _UTF16LE or SQLI
1968b 54 45 5f 55 54 46 31 36 42 45 2e 0a 2a 2a 0a 2a TE_UTF16BE..**.*
1968c 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 6e 6f 74 * If pMem is not
1968d 20 61 20 73 74 72 69 6e 67 20 6f 62 6a 65 63 74 a string object
1968e 2c 20 6f 72 20 74 68 65 20 65 6e 63 6f 64 69 6e , or the encodin
1968f 67 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 0a g of the string.
19690 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ** representatio
19691 6e 20 69 73 20 61 6c 72 65 61 64 79 20 73 74 6f n is already sto
19692 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 72 65 red using the re
19693 71 75 65 73 74 65 64 20 65 6e 63 6f 64 69 6e 67 quested encoding
19694 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 , then this.** r
19695 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
19696 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f p..**.** SQLITE_
19697 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 OK is returned i
19698 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e f the conversion
19699 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 20 28 is successful (
1969a 6f 72 20 6e 6f 74 20 72 65 71 75 69 72 65 64 29 or not required)
1969b 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 ..** SQLITE_NOME
1969c 4d 20 6d 61 79 20 62 65 20 72 65 74 75 72 6e 65 M may be returne
1969d 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 d if a malloc()
1969e 66 61 69 6c 73 20 64 75 72 69 6e 67 20 63 6f 6e fails during con
1969f 76 65 72 73 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 version.** betwe
196a0 65 6e 20 66 6f 72 6d 61 74 73 2e 0a 2a 2f 0a 53 en formats..*/.S
196a1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
196a2 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 t sqlite3VdbeCha
196a3 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d 20 ngeEncoding(Mem
196a4 2a 70 4d 65 6d 2c 20 69 6e 74 20 64 65 73 69 72 *pMem, int desir
196a5 65 64 45 6e 63 29 7b 0a 20 20 69 6e 74 20 72 63 edEnc){. int rc
196a6 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 ;. assert( (pMe
196a7 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 m->flags&MEM_Row
196a8 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 Set)==0 );. ass
196a9 65 72 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d ert( desiredEnc=
196aa 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 =SQLITE_UTF8 ||
196ab 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 desiredEnc==SQLI
196ac 54 45 5f 55 54 46 31 36 4c 45 0a 20 20 20 20 20 TE_UTF16LE.
196ad 20 20 20 20 20 20 7c 7c 20 64 65 73 69 72 65 64 || desired
196ae 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 Enc==SQLITE_UTF1
196af 36 42 45 20 29 3b 0a 20 20 69 66 28 20 21 28 70 6BE );. if( !(p
196b0 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 Mem->flags&MEM_S
196b1 74 72 29 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 tr) || pMem->enc
196b2 3d 3d 64 65 73 69 72 65 64 45 6e 63 20 29 7b 0a ==desiredEnc ){.
196b3 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
196b4 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 E_OK;. }. asse
196b5 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 rt( pMem->db==0
196b6 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
196b7 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e _held(pMem->db->
196b8 6d 75 74 65 78 29 20 29 3b 0a 23 69 66 64 65 66 mutex) );.#ifdef
196b9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
196ba 31 36 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 16. return SQLI
196bb 54 45 5f 45 52 52 4f 52 3b 0a 23 65 6c 73 65 0a TE_ERROR;.#else.
196bc 0a 20 20 2f 2a 20 4d 65 6d 54 72 61 6e 73 6c 61 . /* MemTransla
196bd 74 65 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 te() may return
196be 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c SQLITE_OK or SQL
196bf 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 66 20 4e 4f ITE_NOMEM. If NO
196c0 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2c MEM is returned,
196c1 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 65 . ** then the e
196c2 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 76 ncoding of the v
196c3 61 6c 75 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 alue may not hav
196c4 65 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a e changed.. */.
196c5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
196c6 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 70 beMemTranslate(p
196c7 4d 65 6d 2c 20 28 75 38 29 64 65 73 69 72 65 64 Mem, (u8)desired
196c8 45 6e 63 29 3b 0a 20 20 61 73 73 65 72 74 28 72 Enc);. assert(r
196c9 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20 c==SQLITE_OK
196ca 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f || rc==SQLITE_NO
196cb 4d 45 4d 29 3b 0a 20 20 61 73 73 65 72 74 28 72 MEM);. assert(r
196cc 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20 c==SQLITE_OK
196cd 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 || pMem->enc!=de
196ce 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 73 siredEnc);. ass
196cf 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4e ert(rc==SQLITE_N
196d0 4f 4d 45 4d 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e OMEM || pMem->en
196d1 63 3d 3d 64 65 73 69 72 65 64 45 6e 63 29 3b 0a c==desiredEnc);.
196d2 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e return rc;.#en
196d3 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b dif.}../*.** Mak
196d4 65 20 73 75 72 65 20 70 4d 65 6d 2d 3e 7a 20 70 e sure pMem->z p
196d5 6f 69 6e 74 73 20 74 6f 20 61 20 77 72 69 74 61 oints to a writa
196d6 62 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f ble allocation o
196d7 66 20 61 74 20 6c 65 61 73 74 20 0a 2a 2a 20 6e f at least .** n
196d8 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 bytes..**.** If
196d9 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c the memory cell
196da 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 currently conta
196db 69 6e 73 20 73 74 72 69 6e 67 20 6f 72 20 62 6c ins string or bl
196dc 6f 62 20 64 61 74 61 0a 2a 2a 20 61 6e 64 20 74 ob data.** and t
196dd 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e he third argumen
196de 74 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 t passed to this
196df 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 72 75 function is tru
196e0 65 2c 20 74 68 65 20 0a 2a 2a 20 63 75 72 72 65 e, the .** curre
196e1 6e 74 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 nt content of th
196e2 65 20 63 65 6c 6c 20 69 73 20 70 72 65 73 65 72 e cell is preser
196e3 76 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ved. Otherwise,
196e4 69 74 20 6d 61 79 0a 2a 2a 20 62 65 20 64 69 73 it may.** be dis
196e5 63 61 72 64 65 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 carded. .**.**
196e6 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 This function se
196e7 74 73 20 74 68 65 20 4d 45 4d 5f 44 79 6e 20 66 ts the MEM_Dyn f
196e8 6c 61 67 20 61 6e 64 20 63 6c 65 61 72 73 20 61 lag and clears a
196e9 6e 79 20 78 44 65 6c 20 63 61 6c 6c 62 61 63 6b ny xDel callback
196ea 2e 0a 2a 2a 20 49 74 20 61 6c 73 6f 20 63 6c 65 ..** It also cle
196eb 61 72 73 20 4d 45 4d 5f 45 70 68 65 6d 20 61 6e ars MEM_Ephem an
196ec 64 20 4d 45 4d 5f 53 74 61 74 69 63 2e 20 49 66 d MEM_Static. If
196ed 20 74 68 65 20 70 72 65 73 65 72 76 65 20 66 6c the preserve fl
196ee 61 67 20 69 73 20 0a 2a 2a 20 6e 6f 74 20 73 65 ag is .** not se
196ef 74 2c 20 4d 65 6d 2e 6e 20 69 73 20 7a 65 72 6f t, Mem.n is zero
196f0 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
196f1 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
196f2 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 4d 65 6d 3VdbeMemGrow(Mem
196f3 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 2c 20 69 *pMem, int n, i
196f4 6e 74 20 70 72 65 73 65 72 76 65 29 7b 0a 20 20 nt preserve){.
196f5 61 73 73 65 72 74 28 20 31 20 3e 3d 0a 20 20 20 assert( 1 >=.
196f6 20 28 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 ((pMem->zMalloc
196f7 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f && pMem->zMallo
196f8 63 3d 3d 70 4d 65 6d 2d 3e 7a 29 20 3f 20 31 20 c==pMem->z) ? 1
196f9 3a 20 30 29 20 2b 0a 20 20 20 20 28 28 28 70 4d : 0) +. (((pM
196fa 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 em->flags&MEM_Dy
196fb 6e 29 26 26 70 4d 65 6d 2d 3e 78 44 65 6c 29 20 n)&&pMem->xDel)
196fc 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20 20 20 20 ? 1 : 0) + .
196fd 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 ((pMem->flags&ME
196fe 4d 5f 45 70 68 65 6d 29 20 3f 20 31 20 3a 20 30 M_Ephem) ? 1 : 0
196ff 29 20 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d 2d ) + . ((pMem-
19700 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69 >flags&MEM_Stati
19701 63 29 20 3f 20 31 20 3a 20 30 29 0a 20 20 29 3b c) ? 1 : 0). );
19702 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d . assert( (pMem
19703 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 ->flags&MEM_RowS
19704 65 74 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 et)==0 );.. if(
19705 20 6e 3c 33 32 20 29 20 6e 20 3d 20 33 32 3b 0a n<32 ) n = 32;.
19706 20 20 69 66 28 20 73 71 6c 69 74 65 33 44 62 4d if( sqlite3DbM
19707 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d 2d 3e allocSize(pMem->
19708 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f db, pMem->zMallo
19709 63 29 3c 6e 20 29 7b 0a 20 20 20 20 69 66 28 20 c)<n ){. if(
1970a 70 72 65 73 65 72 76 65 20 26 26 20 70 4d 65 6d preserve && pMem
1970b 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c ->z==pMem->zMall
1970c 6f 63 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d oc ){. pMem
1970d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 6c ->z = pMem->zMal
1970e 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 52 loc = sqlite3DbR
1970f 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 4d 65 eallocOrFree(pMe
19710 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 m->db, pMem->z,
19711 6e 29 3b 0a 20 20 20 20 20 20 70 72 65 73 65 72 n);. preser
19712 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 ve = 0;. }els
19713 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 e{. sqlite3
19714 44 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c DbFree(pMem->db,
19715 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b pMem->zMalloc);
19716 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 . pMem->zMa
19717 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 lloc = sqlite3Db
19718 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d 3e MallocRaw(pMem->
19719 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 db, n);. }.
1971a 7d 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a }.. if( pMem->z
1971b 20 26 26 20 70 72 65 73 65 72 76 65 20 26 26 20 && preserve &&
1971c 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 26 pMem->zMalloc &&
1971d 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e pMem->z!=pMem->
1971e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 6d zMalloc ){. m
1971f 65 6d 63 70 79 28 70 4d 65 6d 2d 3e 7a 4d 61 6c emcpy(pMem->zMal
19720 6c 6f 63 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 70 4d loc, pMem->z, pM
19721 65 6d 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 em->n);. }. if
19722 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 ( pMem->flags&ME
19723 4d 5f 44 79 6e 20 26 26 20 70 4d 65 6d 2d 3e 78 M_Dyn && pMem->x
19724 44 65 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d Del ){. pMem-
19725 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 28 70 >xDel((void *)(p
19726 4d 65 6d 2d 3e 7a 29 29 3b 0a 20 20 7d 0a 0a 20 Mem->z));. }..
19727 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d pMem->z = pMem-
19728 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 66 28 20 >zMalloc;. if(
19729 70 4d 65 6d 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 pMem->z==0 ){.
1972a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
1972b 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 MEM_Null;. }els
1972c 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 e{. pMem->fla
1972d 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 gs &= ~(MEM_Ephe
1972e 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 m|MEM_Static);.
1972f 20 7d 0a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 }. pMem->xDel
19730 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 = 0;. return (p
19731 4d 65 6d 2d 3e 7a 20 3f 20 53 51 4c 49 54 45 5f Mem->z ? SQLITE_
19732 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 OK : SQLITE_NOME
19733 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b M);.}../*.** Mak
19734 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 e the given Mem
19735 6f 62 6a 65 63 74 20 4d 45 4d 5f 44 79 6e 2e 20 object MEM_Dyn.
19736 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
19737 20 6d 61 6b 65 20 69 74 20 73 6f 0a 2a 2a 20 74 make it so.** t
19738 68 61 74 20 61 6e 79 20 54 45 58 54 20 6f 72 20 hat any TEXT or
19739 42 4c 4f 42 20 63 6f 6e 74 65 6e 74 20 69 73 20 BLOB content is
1973a 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 stored in memory
1973b 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a obtained from.*
1973c 2a 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 49 6e 20 * malloc(). In
1973d 74 68 69 73 20 77 61 79 2c 20 77 65 20 6b 6e 6f this way, we kno
1973e 77 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 w that the memor
1973f 79 20 69 73 20 73 61 66 65 20 74 6f 20 62 65 0a y is safe to be.
19740 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 6f ** overwritten o
19741 72 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a r altered..**.**
19742 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
19743 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 K on success or
19744 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 SQLITE_NOMEM if
19745 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f malloc fails..*/
19746 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
19747 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
19748 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 emMakeWriteable(
19749 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e Mem *pMem){. in
1974a 74 20 66 3b 0a 20 20 61 73 73 65 72 74 28 20 70 t f;. assert( p
1974b 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
1974c 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1974d 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
1974e 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 ) );. assert( (
1974f 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pMem->flags&MEM_
19750 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 RowSet)==0 );.
19751 65 78 70 61 6e 64 42 6c 6f 62 28 70 4d 65 6d 29 expandBlob(pMem)
19752 3b 0a 20 20 66 20 3d 20 70 4d 65 6d 2d 3e 66 6c ;. f = pMem->fl
19753 61 67 73 3b 0a 20 20 69 66 28 20 28 66 26 28 4d ags;. if( (f&(M
19754 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 EM_Str|MEM_Blob)
19755 29 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d ) && pMem->z!=pM
19756 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 em->zMalloc ){.
19757 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 if( sqlite3Vd
19758 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 beMemGrow(pMem,
19759 70 4d 65 6d 2d 3e 6e 20 2b 20 32 2c 20 31 29 20 pMem->n + 2, 1)
1975a 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1975b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
1975c 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b }. pMem->z[
1975d 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 pMem->n] = 0;.
1975e 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e pMem->z[pMem->
1975f 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d n+1] = 0;. pM
19760 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d em->flags |= MEM
19761 5f 54 65 72 6d 3b 0a 20 20 7d 0a 0a 20 20 72 65 _Term;. }.. re
19762 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
19763 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 }../*.** If the
19764 67 69 76 65 6e 20 4d 65 6d 2a 20 68 61 73 20 61 given Mem* has a
19765 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 zero-filled tai
19766 6c 2c 20 74 75 72 6e 20 69 74 20 69 6e 74 6f 20 l, turn it into
19767 61 6e 20 6f 72 64 69 6e 61 72 79 0a 2a 2a 20 62 an ordinary.** b
19768 6c 6f 62 20 73 74 6f 72 65 64 20 69 6e 20 64 79 lob stored in dy
19769 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 namically alloca
1976a 74 65 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 23 69 ted space..*/.#i
1976b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1976c 54 5f 49 4e 43 52 42 4c 4f 42 0a 53 51 4c 49 54 T_INCRBLOB.SQLIT
1976d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1976e 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 lite3VdbeMemExpa
1976f 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65 6d ndBlob(Mem *pMem
19770 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 ){. if( pMem->f
19771 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 lags & MEM_Zero
19772 29 7b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 ){. int nByte
19773 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d ;. assert( pM
19774 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c em->flags&MEM_Bl
19775 6f 62 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 ob );. assert
19776 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d ( (pMem->flags&M
19777 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b EM_RowSet)==0 );
19778 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 . assert( pMe
19779 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 m->db==0 || sqli
1977a 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1977b 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 Mem->db->mutex)
1977c 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 6e );.. /* Set n
1977d 42 79 74 65 20 74 6f 20 74 68 65 20 6e 75 6d 62 Byte to the numb
1977e 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 er of bytes requ
1977f 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 ired to store th
19780 65 20 65 78 70 61 6e 64 65 64 20 62 6c 6f 62 2e e expanded blob.
19781 20 2a 2f 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 */. nByte =
19782 70 4d 65 6d 2d 3e 6e 20 2b 20 70 4d 65 6d 2d 3e pMem->n + pMem->
19783 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 69 66 28 u.nZero;. if(
19784 20 6e 42 79 74 65 3c 3d 30 20 29 7b 0a 20 20 20 nByte<=0 ){.
19785 20 20 20 6e 42 79 74 65 20 3d 20 31 3b 0a 20 20 nByte = 1;.
19786 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 }. if( sqli
19787 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 te3VdbeMemGrow(p
19788 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 31 29 20 29 Mem, nByte, 1) )
19789 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
1978a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
1978b 20 7d 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 }.. memset(&
1978c 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d pMem->z[pMem->n]
1978d 2c 20 30 2c 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 , 0, pMem->u.nZe
1978e 72 6f 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e ro);. pMem->n
1978f 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 += pMem->u.nZer
19790 6f 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 o;. pMem->fla
19791 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 5a 65 72 6f gs &= ~(MEM_Zero
19792 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a 20 20 7d 0a |MEM_Term);. }.
19793 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
19794 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f OK;.}.#endif.../
19795 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 *.** Make sure t
19796 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 69 73 20 he given Mem is
19797 5c 75 30 30 30 30 20 74 65 72 6d 69 6e 61 74 65 \u0000 terminate
19798 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
19799 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1979a 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e VdbeMemNulTermin
1979b 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a ate(Mem *pMem){.
1979c 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
1979d 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
1979e 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d _mutex_held(pMem
1979f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
197a0 20 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 if( (pMem->fla
197a1 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21 3d gs & MEM_Term)!=
197a2 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 0 || (pMem->flag
197a3 73 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 s & MEM_Str)==0
197a4 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
197a5 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a 20 4e 6f LITE_OK; /* No
197a6 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a 20 thing to do */.
197a7 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
197a8 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d VdbeMemGrow(pMem
197a9 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 2c 20 31 29 20 , pMem->n+2, 1)
197aa 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
197ab 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
197ac 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e pMem->z[pMem->
197ad 6e 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e n] = 0;. pMem->
197ae 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 z[pMem->n+1] = 0
197af 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 ;. pMem->flags
197b0 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 72 |= MEM_Term;. r
197b1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
197b2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4d 45 .}../*.** Add ME
197b3 4d 5f 53 74 72 20 74 6f 20 74 68 65 20 73 65 74 M_Str to the set
197b4 20 6f 66 20 72 65 70 72 65 73 65 6e 74 61 74 69 of representati
197b5 6f 6e 73 20 66 6f 72 20 74 68 65 20 67 69 76 65 ons for the give
197b6 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62 65 72 73 0a n Mem. Numbers.
197b7 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64 ** are converted
197b8 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 73 using sqlite3_s
197b9 6e 70 72 69 6e 74 66 28 29 2e 20 20 43 6f 6e 76 nprintf(). Conv
197ba 65 72 74 69 6e 67 20 61 20 42 4c 4f 42 20 74 6f erting a BLOB to
197bb 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20 a string.** is
197bc 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45 a no-op..**.** E
197bd 78 69 73 74 69 6e 67 20 72 65 70 72 65 73 65 6e xisting represen
197be 74 61 74 69 6f 6e 73 20 4d 45 4d 5f 49 6e 74 20 tations MEM_Int
197bf 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 61 72 65 and MEM_Real are
197c0 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c 69 64 61 74 *not* invalidat
197c1 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d 45 4d 5f ed..**.** A MEM_
197c2 4e 75 6c 6c 20 76 61 6c 75 65 20 77 69 6c 6c 20 Null value will
197c3 6e 65 76 65 72 20 62 65 20 70 61 73 73 65 64 20 never be passed
197c4 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
197c5 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
197c6 69 73 0a 2a 2a 20 75 73 65 64 20 66 6f 72 20 63 is.** used for c
197c7 6f 6e 76 65 72 74 69 6e 67 20 76 61 6c 75 65 73 onverting values
197c8 20 74 6f 20 74 65 78 74 20 66 6f 72 20 72 65 74 to text for ret
197c9 75 72 6e 69 6e 67 20 74 6f 20 74 68 65 20 75 73 urning to the us
197ca 65 72 20 28 69 2e 65 2e 20 76 69 61 0a 2a 2a 20 er (i.e. via.**
197cb 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
197cc 78 74 28 29 29 2c 20 6f 72 20 66 6f 72 20 65 6e xt()), or for en
197cd 73 75 72 69 6e 67 20 74 68 61 74 20 76 61 6c 75 suring that valu
197ce 65 73 20 74 6f 20 62 65 20 75 73 65 64 20 61 73 es to be used as
197cf 20 62 74 72 65 65 0a 2a 2a 20 6b 65 79 73 20 61 btree.** keys a
197d0 72 65 20 73 74 72 69 6e 67 73 2e 20 49 6e 20 74 re strings. In t
197d1 68 65 20 66 6f 72 6d 65 72 20 63 61 73 65 20 61 he former case a
197d2 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 NULL pointer is
197d3 20 72 65 74 75 72 6e 65 64 20 74 68 65 0a 2a 2a returned the.**
197d4 20 75 73 65 72 20 61 6e 64 20 74 68 65 20 6c 61 user and the la
197d5 74 65 72 20 69 73 20 61 6e 20 69 6e 74 65 72 6e ter is an intern
197d6 61 6c 20 70 72 6f 67 72 61 6d 6d 69 6e 67 20 65 al programming e
197d7 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rror..*/.SQLITE_
197d8 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
197d9 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 te3VdbeMemString
197da 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 ify(Mem *pMem, i
197db 6e 74 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 72 nt enc){. int r
197dc 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
197dd 20 69 6e 74 20 66 67 20 3d 20 70 4d 65 6d 2d 3e int fg = pMem->
197de 66 6c 61 67 73 3b 0a 20 20 63 6f 6e 73 74 20 69 flags;. const i
197df 6e 74 20 6e 42 79 74 65 20 3d 20 33 32 3b 0a 0a nt nByte = 32;..
197e0 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
197e1 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
197e2 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d _mutex_held(pMem
197e3 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
197e4 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26 4d assert( !(fg&M
197e5 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a 20 20 61 73 EM_Zero) );. as
197e6 73 65 72 74 28 20 21 28 66 67 26 28 4d 45 4d 5f sert( !(fg&(MEM_
197e7 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 20 29 Str|MEM_Blob)) )
197e8 3b 0a 20 20 61 73 73 65 72 74 28 20 66 67 26 28 ;. assert( fg&(
197e9 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c MEM_Int|MEM_Real
197ea 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 ) );. assert( (
197eb 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pMem->flags&MEM_
197ec 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 RowSet)==0 );.
197ed 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 assert( EIGHT_BY
197ee 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 TE_ALIGNMENT(pMe
197ef 6d 29 20 29 3b 0a 0a 0a 20 20 69 66 28 20 73 71 m) );... if( sq
197f0 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
197f1 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 (pMem, nByte, 0)
197f2 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
197f3 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
197f4 0a 0a 20 20 2f 2a 20 46 6f 72 20 61 20 52 65 61 .. /* For a Rea
197f5 6c 20 6f 72 20 49 6e 74 65 67 65 72 2c 20 75 73 l or Integer, us
197f6 65 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 e sqlite3_mprint
197f7 66 28 29 20 74 6f 20 70 72 6f 64 75 63 65 20 74 f() to produce t
197f8 68 65 20 55 54 46 2d 38 0a 20 20 2a 2a 20 73 74 he UTF-8. ** st
197f9 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 ring representat
197fa 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 6c 75 65 ion of the value
197fb 2e 20 54 68 65 6e 2c 20 69 66 20 74 68 65 20 72 . Then, if the r
197fc 65 71 75 69 72 65 64 20 65 6e 63 6f 64 69 6e 67 equired encoding
197fd 0a 20 20 2a 2a 20 69 73 20 55 54 46 2d 31 36 6c . ** is UTF-16l
197fe 65 20 6f 72 20 55 54 46 2d 31 36 62 65 20 64 6f e or UTF-16be do
197ff 20 61 20 74 72 61 6e 73 6c 61 74 69 6f 6e 2e 0a a translation..
19800 20 20 2a 2a 20 0a 20 20 2a 2a 20 46 49 58 20 4d ** . ** FIX M
19801 45 3a 20 49 74 20 77 6f 75 6c 64 20 62 65 20 62 E: It would be b
19802 65 74 74 65 72 20 69 66 20 73 71 6c 69 74 65 33 etter if sqlite3
19803 5f 73 6e 70 72 69 6e 74 66 28 29 20 63 6f 75 6c _snprintf() coul
19804 64 20 64 6f 20 55 54 46 2d 31 36 2e 0a 20 20 2a d do UTF-16.. *
19805 2f 0a 20 20 69 66 28 20 66 67 20 26 20 4d 45 4d /. if( fg & MEM
19806 5f 49 6e 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 _Int ){. sqli
19807 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 79 te3_snprintf(nBy
19808 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 6c te, pMem->z, "%l
19809 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29 3b ld", pMem->u.i);
1980a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 . }else{. as
1980b 73 65 72 74 28 20 66 67 20 26 20 4d 45 4d 5f 52 sert( fg & MEM_R
1980c 65 61 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 eal );. sqlit
1980d 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 79 74 e3_snprintf(nByt
1980e 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 21 2e e, pMem->z, "%!.
1980f 31 35 67 22 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a 15g", pMem->r);.
19810 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 }. pMem->n =
19811 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
19812 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 70 4d 65 6d pMem->z);. pMem
19813 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 ->enc = SQLITE_U
19814 54 46 38 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 TF8;. pMem->fla
19815 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 gs |= MEM_Str|ME
19816 4d 5f 54 65 72 6d 3b 0a 20 20 73 71 6c 69 74 65 M_Term;. sqlite
19817 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 3VdbeChangeEncod
19818 69 6e 67 28 70 4d 65 6d 2c 20 65 6e 63 29 3b 0a ing(pMem, enc);.
19819 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1981a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63 65 6c /*.** Memory cel
1981b 6c 20 70 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20 l pMem contains
1981c 74 68 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 61 the context of a
1981d 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 n aggregate func
1981e 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f tion..** This ro
1981f 75 74 69 6e 65 20 63 61 6c 6c 73 20 74 68 65 20 utine calls the
19820 66 69 6e 61 6c 69 7a 65 20 6d 65 74 68 6f 64 20 finalize method
19821 66 6f 72 20 74 68 61 74 20 66 75 6e 63 74 69 6f for that functio
19822 6e 2e 20 20 54 68 65 0a 2a 2a 20 72 65 73 75 6c n. The.** resul
19823 74 20 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 t of the aggrega
19824 74 65 20 69 73 20 73 74 6f 72 65 64 20 62 61 63 te is stored bac
19825 6b 20 69 6e 74 6f 20 70 4d 65 6d 2e 0a 2a 2a 0a k into pMem..**.
19826 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 ** Return SQLITE
19827 5f 45 52 52 4f 52 20 69 66 20 74 68 65 20 66 69 _ERROR if the fi
19828 6e 61 6c 69 7a 65 72 20 72 65 70 6f 72 74 73 20 nalizer reports
19829 61 6e 20 65 72 72 6f 72 2e 20 20 53 51 4c 49 54 an error. SQLIT
1982a 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 65 72 77 69 73 E_OK.** otherwis
1982b 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1982c 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1982d 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 VdbeMemFinalize(
1982e 4d 65 6d 20 2a 70 4d 65 6d 2c 20 46 75 6e 63 44 Mem *pMem, FuncD
1982f 65 66 20 2a 70 46 75 6e 63 29 7b 0a 20 20 69 6e ef *pFunc){. in
19830 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
19831 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 ;. if( ALWAYS(p
19832 46 75 6e 63 20 26 26 20 70 46 75 6e 63 2d 3e 78 Func && pFunc->x
19833 46 69 6e 61 6c 69 7a 65 29 20 29 7b 0a 20 20 20 Finalize) ){.
19834 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
19835 20 63 74 78 3b 0a 20 20 20 20 61 73 73 65 72 74 ctx;. assert
19836 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 ( (pMem->flags &
19837 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 7c 7c MEM_Null)!=0 ||
19838 20 70 46 75 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 2e pFunc==pMem->u.
19839 70 44 65 66 20 29 3b 0a 20 20 20 20 61 73 73 65 pDef );. asse
1983a 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 rt( pMem->db==0
1983b 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
1983c 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e _held(pMem->db->
1983d 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 6d 65 mutex) );. me
1983e 6d 73 65 74 28 26 63 74 78 2c 20 30 2c 20 73 69 mset(&ctx, 0, si
1983f 7a 65 6f 66 28 63 74 78 29 29 3b 0a 20 20 20 20 zeof(ctx));.
19840 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 ctx.s.flags = ME
19841 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 63 74 78 2e M_Null;. ctx.
19842 73 2e 64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b s.db = pMem->db;
19843 0a 20 20 20 20 63 74 78 2e 70 4d 65 6d 20 3d 20 . ctx.pMem =
19844 70 4d 65 6d 3b 0a 20 20 20 20 63 74 78 2e 70 46 pMem;. ctx.pF
19845 75 6e 63 20 3d 20 70 46 75 6e 63 3b 0a 20 20 20 unc = pFunc;.
19846 20 70 46 75 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a pFunc->xFinaliz
19847 65 28 26 63 74 78 29 3b 0a 20 20 20 20 61 73 73 e(&ctx);. ass
19848 65 72 74 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66 ert( 0==(pMem->f
19849 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 20 26 26 lags&MEM_Dyn) &&
1984a 20 21 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a !pMem->xDel );.
1984b 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1984c 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d e(pMem->db, pMem
1984d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 ->zMalloc);.
1984e 6d 65 6d 63 70 79 28 70 4d 65 6d 2c 20 26 63 74 memcpy(pMem, &ct
1984f 78 2e 73 2c 20 73 69 7a 65 6f 66 28 63 74 78 2e x.s, sizeof(ctx.
19850 73 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 74 s));. rc = ct
19851 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 20 x.isError;. }.
19852 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
19853 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f *.** If the memo
19854 72 79 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 73 ry cell contains
19855 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 a string value
19856 74 68 61 74 20 6d 75 73 74 20 62 65 20 66 72 65 that must be fre
19857 65 64 20 62 79 0a 2a 2a 20 69 6e 76 6f 6b 69 6e ed by.** invokin
19858 67 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 63 61 g an external ca
19859 6c 6c 62 61 63 6b 2c 20 66 72 65 65 20 69 74 20 llback, free it
1985a 6e 6f 77 2e 20 43 61 6c 6c 69 6e 67 20 74 68 69 now. Calling thi
1985b 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 64 6f s function.** do
1985c 65 73 20 6e 6f 74 20 66 72 65 65 20 61 6e 79 20 es not free any
1985d 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 62 75 66 66 Mem.zMalloc buff
1985e 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
1985f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
19860 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
19861 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 29 External(Mem *p)
19862 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 {. assert( p->d
19863 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
19864 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 mutex_held(p->db
19865 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 65 ->mutex) );. te
19866 73 74 63 61 73 65 28 20 70 2d 3e 66 6c 61 67 73 stcase( p->flags
19867 20 26 20 4d 45 4d 5f 41 67 67 20 29 3b 0a 20 20 & MEM_Agg );.
19868 74 65 73 74 63 61 73 65 28 20 70 2d 3e 66 6c 61 testcase( p->fla
19869 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20 29 3b 0a gs & MEM_Dyn );.
1986a 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 66 testcase( p->f
1986b 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 lags & MEM_RowSe
1986c 74 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 t );. testcase(
1986d 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f p->flags & MEM_
1986e 46 72 61 6d 65 20 29 3b 0a 20 20 69 66 28 20 70 Frame );. if( p
1986f 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 41 67 67 ->flags&(MEM_Agg
19870 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 52 6f 77 |MEM_Dyn|MEM_Row
19871 53 65 74 7c 4d 45 4d 5f 46 72 61 6d 65 29 20 29 Set|MEM_Frame) )
19872 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 {. if( p->fla
19873 67 73 26 4d 45 4d 5f 41 67 67 20 29 7b 0a 20 20 gs&MEM_Agg ){.
19874 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
19875 65 6d 46 69 6e 61 6c 69 7a 65 28 70 2c 20 70 2d emFinalize(p, p-
19876 3e 75 2e 70 44 65 66 29 3b 0a 20 20 20 20 20 20 >u.pDef);.
19877 61 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 assert( (p->flag
19878 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 s & MEM_Agg)==0
19879 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1987a 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 VdbeMemRelease(p
1987b 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 );. }else if(
1987c 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 p->flags&MEM_Dy
1987d 6e 20 26 26 20 70 2d 3e 78 44 65 6c 20 29 7b 0a n && p->xDel ){.
1987e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 assert( (p
1987f 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 ->flags&MEM_RowS
19880 65 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 et)==0 );.
19881 70 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 p->xDel((void *)
19882 70 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 2d 3e p->z);. p->
19883 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 7d 65 xDel = 0;. }e
19884 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 lse if( p->flags
19885 26 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 &MEM_RowSet ){.
19886 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53 sqlite3RowS
19887 65 74 43 6c 65 61 72 28 70 2d 3e 75 2e 70 52 6f etClear(p->u.pRo
19888 77 53 65 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 wSet);. }else
19889 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 if( p->flags&ME
1988a 4d 5f 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 20 M_Frame ){.
1988b 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1988c 65 74 4e 75 6c 6c 28 70 29 3b 0a 20 20 20 20 7d etNull(p);. }
1988d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
1988e 6c 65 61 73 65 20 61 6e 79 20 6d 65 6d 6f 72 79 lease any memory
1988f 20 68 65 6c 64 20 62 79 20 74 68 65 20 4d 65 6d held by the Mem
19890 2e 20 54 68 69 73 20 6d 61 79 20 6c 65 61 76 65 . This may leave
19891 20 74 68 65 20 4d 65 6d 20 69 6e 20 61 6e 0a 2a the Mem in an.*
19892 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 * inconsistent s
19893 74 61 74 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c tate, for exampl
19894 65 20 77 69 74 68 20 28 4d 65 6d 2e 7a 3d 3d 30 e with (Mem.z==0
19895 29 20 61 6e 64 0a 2a 2a 20 28 4d 65 6d 2e 74 79 ) and.** (Mem.ty
19896 70 65 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54 29 pe==SQLITE_TEXT)
19897 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19898 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
19899 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d VdbeMemRelease(M
1989a 65 6d 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 em *p){. sqlite
1989b 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 3VdbeMemReleaseE
1989c 78 74 65 72 6e 61 6c 28 70 29 3b 0a 20 20 73 71 xternal(p);. sq
1989d 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 lite3DbFree(p->d
1989e 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a b, p->zMalloc);.
1989f 20 20 70 2d 3e 7a 20 3d 20 30 3b 0a 20 20 70 2d p->z = 0;. p-
198a0 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 >zMalloc = 0;.
198a1 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 7d 0a 0a p->xDel = 0;.}..
198a2 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 /*.** Convert a
198a3 36 34 2d 62 69 74 20 49 45 45 45 20 64 6f 75 62 64-bit IEEE doub
198a4 6c 65 20 69 6e 74 6f 20 61 20 36 34 2d 62 69 74 le into a 64-bit
198a5 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e signed integer.
198a6 0a 2a 2a 20 49 66 20 74 68 65 20 64 6f 75 62 6c .** If the doubl
198a7 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 2c 20 e is too large,
198a8 72 65 74 75 72 6e 20 30 78 38 30 30 30 30 30 30 return 0x8000000
198a9 30 30 30 30 30 30 30 30 30 2e 0a 2a 2a 0a 2a 2a 000000000..**.**
198aa 20 4d 6f 73 74 20 73 79 73 74 65 6d 73 20 61 70 Most systems ap
198ab 70 65 61 72 20 74 6f 20 64 6f 20 74 68 69 73 20 pear to do this
198ac 73 69 6d 70 6c 79 20 62 79 20 61 73 73 69 67 6e simply by assign
198ad 69 6e 67 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 ing.** variables
198ae 20 61 6e 64 20 77 69 74 68 6f 75 74 20 74 68 65 and without the
198af 20 65 78 74 72 61 20 72 61 6e 67 65 20 74 65 73 extra range tes
198b0 74 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65 72 ts. But.** ther
198b1 65 20 61 72 65 20 72 65 70 6f 72 74 73 20 74 68 e are reports th
198b2 61 74 20 77 69 6e 64 6f 77 73 20 74 68 72 6f 77 at windows throw
198b3 73 20 61 6e 20 65 78 70 65 63 74 69 6f 6e 0a 2a s an expection.*
198b4 2a 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e * if the floatin
198b5 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73 g point value is
198b6 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 28 out of range. (
198b7 53 65 65 20 74 69 63 6b 65 74 20 23 32 38 38 30 See ticket #2880
198b8 2e 29 0a 2a 2a 20 42 65 63 61 75 73 65 20 77 65 .).** Because we
198b9 20 64 6f 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 do not complete
198ba 6c 79 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 ly understand th
198bb 65 20 70 72 6f 62 6c 65 6d 2c 20 77 65 20 77 69 e problem, we wi
198bc 6c 6c 0a 2a 2a 20 74 61 6b 65 20 74 68 65 20 63 ll.** take the c
198bd 6f 6e 73 65 72 76 61 74 69 76 65 20 61 70 70 72 onservative appr
198be 6f 61 63 68 20 61 6e 64 20 61 6c 77 61 79 73 20 oach and always
198bf 64 6f 20 72 61 6e 67 65 20 74 65 73 74 73 0a 2a do range tests.*
198c0 2a 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74 * before attempt
198c1 69 6e 67 20 74 68 65 20 63 6f 6e 76 65 72 73 69 ing the conversi
198c2 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 on..*/.static i6
198c3 34 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 4 doubleToInt64(
198c4 64 6f 75 62 6c 65 20 72 29 7b 0a 20 20 2f 2a 0a double r){. /*.
198c5 20 20 2a 2a 20 4d 61 6e 79 20 63 6f 6d 70 69 6c ** Many compil
198c6 65 72 73 20 77 65 20 65 6e 63 6f 75 6e 74 65 72 ers we encounter
198c7 20 64 6f 20 6e 6f 74 20 64 65 66 69 6e 65 20 63 do not define c
198c8 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 74 68 65 onstants for the
198c9 0a 20 20 2a 2a 20 6d 69 6e 69 6d 75 6d 20 61 6e . ** minimum an
198ca 64 20 6d 61 78 69 6d 75 6d 20 36 34 2d 62 69 74 d maximum 64-bit
198cb 20 69 6e 74 65 67 65 72 73 2c 20 6f 72 20 74 68 integers, or th
198cc 65 79 20 64 65 66 69 6e 65 20 74 68 65 6d 0a 20 ey define them.
198cd 20 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 ** inconsistent
198ce 6c 79 2e 20 20 41 6e 64 20 6d 61 6e 79 20 64 6f ly. And many do
198cf 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20 not understand
198d0 74 68 65 20 22 4c 4c 22 20 6e 6f 74 61 74 69 6f the "LL" notatio
198d1 6e 2e 0a 20 20 2a 2a 20 53 6f 20 77 65 20 64 65 n.. ** So we de
198d2 66 69 6e 65 20 6f 75 72 20 6f 77 6e 20 73 74 61 fine our own sta
198d3 74 69 63 20 63 6f 6e 73 74 61 6e 74 73 20 68 65 tic constants he
198d4 72 65 20 75 73 69 6e 67 20 6e 6f 74 68 69 6e 67 re using nothing
198d5 0a 20 20 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 . ** larger tha
198d6 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 n a 32-bit integ
198d7 65 72 20 63 6f 6e 73 74 61 6e 74 2e 0a 20 20 2a er constant.. *
198d8 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 /. static const
198d9 20 69 36 34 20 6d 61 78 49 6e 74 20 3d 20 4c 41 i64 maxInt = LA
198da 52 47 45 53 54 5f 49 4e 54 36 34 3b 0a 20 20 73 RGEST_INT64;. s
198db 74 61 74 69 63 20 63 6f 6e 73 74 20 69 36 34 20 tatic const i64
198dc 6d 69 6e 49 6e 74 20 3d 20 53 4d 41 4c 4c 45 53 minInt = SMALLES
198dd 54 5f 49 4e 54 36 34 3b 0a 0a 20 20 69 66 28 20 T_INT64;.. if(
198de 72 3c 28 64 6f 75 62 6c 65 29 6d 69 6e 49 6e 74 r<(double)minInt
198df 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d ){. return m
198e0 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 69 inInt;. }else i
198e1 66 28 20 72 3e 28 64 6f 75 62 6c 65 29 6d 61 78 f( r>(double)max
198e2 49 6e 74 20 29 7b 0a 20 20 20 20 2f 2a 20 6d 69 Int ){. /* mi
198e3 6e 49 6e 74 20 69 73 20 63 6f 72 72 65 63 74 20 nInt is correct
198e4 68 65 72 65 20 2d 20 6e 6f 74 20 6d 61 78 49 6e here - not maxIn
198e5 74 2e 20 20 49 74 20 74 75 72 6e 73 20 6f 75 74 t. It turns out
198e6 20 74 68 61 74 20 61 73 73 69 67 6e 69 6e 67 0a that assigning.
198e7 20 20 20 20 2a 2a 20 61 20 76 65 72 79 20 6c 61 ** a very la
198e8 72 67 65 20 70 6f 73 69 74 69 76 65 20 6e 75 6d rge positive num
198e9 62 65 72 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 ber to an intege
198ea 72 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 76 r results in a v
198eb 65 72 79 20 6c 61 72 67 65 0a 20 20 20 20 2a 2a ery large. **
198ec 20 6e 65 67 61 74 69 76 65 20 69 6e 74 65 67 65 negative intege
198ed 72 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20 6e r. This makes n
198ee 6f 20 73 65 6e 73 65 2c 20 62 75 74 20 69 74 20 o sense, but it
198ef 69 73 20 77 68 61 74 20 78 38 36 20 68 61 72 64 is what x86 hard
198f0 77 61 72 65 0a 20 20 20 20 2a 2a 20 64 6f 65 73 ware. ** does
198f1 20 73 6f 20 66 6f 72 20 63 6f 6d 70 61 74 69 62 so for compatib
198f2 69 6c 69 74 79 20 77 65 20 77 69 6c 6c 20 64 6f ility we will do
198f3 20 74 68 65 20 73 61 6d 65 20 69 6e 20 73 6f 66 the same in sof
198f4 74 77 61 72 65 2e 20 2a 2f 0a 20 20 20 20 72 65 tware. */. re
198f5 74 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d turn minInt;. }
198f6 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
198f7 20 28 69 36 34 29 72 3b 0a 20 20 7d 0a 7d 0a 0a (i64)r;. }.}..
198f8 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 73 6f 6d /*.** Return som
198f9 65 20 6b 69 6e 64 20 6f 66 20 69 6e 74 65 67 65 e kind of intege
198fa 72 20 76 61 6c 75 65 20 77 68 69 63 68 20 69 73 r value which is
198fb 20 74 68 65 20 62 65 73 74 20 77 65 20 63 61 6e the best we can
198fc 20 64 6f 0a 2a 2a 20 61 74 20 72 65 70 72 65 73 do.** at repres
198fd 65 6e 74 69 6e 67 20 74 68 65 20 76 61 6c 75 65 enting the value
198fe 20 74 68 61 74 20 2a 70 4d 65 6d 20 64 65 73 63 that *pMem desc
198ff 72 69 62 65 73 20 61 73 20 61 6e 20 69 6e 74 65 ribes as an inte
19900 67 65 72 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 ger..** If pMem
19901 69 73 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 74 is an integer, t
19902 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 hen the value is
19903 20 65 78 61 63 74 2e 20 20 49 66 20 70 4d 65 6d exact. If pMem
19904 20 69 73 0a 2a 2a 20 61 20 66 6c 6f 61 74 69 6e is.** a floatin
19905 67 2d 70 6f 69 6e 74 20 74 68 65 6e 20 74 68 65 g-point then the
19906 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
19907 69 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 70 is the integer p
19908 61 72 74 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 art..** If pMem
19909 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 is a string or b
1990a 6c 6f 62 2c 20 74 68 65 6e 20 77 65 20 6d 61 6b lob, then we mak
1990b 65 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 e an attempt to
1990c 63 6f 6e 76 65 72 74 0a 2a 2a 20 69 74 20 69 6e convert.** it in
1990d 74 6f 20 61 20 69 6e 74 65 67 65 72 20 61 6e 64 to a integer and
1990e 20 72 65 74 75 72 6e 20 74 68 61 74 2e 20 20 49 return that. I
1990f 66 20 70 4d 65 6d 20 72 65 70 72 65 73 65 6e 74 f pMem represent
19910 73 20 61 6e 0a 2a 2a 20 61 6e 20 53 51 4c 2d 4e s an.** an SQL-N
19911 55 4c 4c 20 76 61 6c 75 65 2c 20 72 65 74 75 72 ULL value, retur
19912 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d n 0..**.** If pM
19913 65 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 em represents a
19914 73 74 72 69 6e 67 20 76 61 6c 75 65 2c 20 69 74 string value, it
19915 73 20 65 6e 63 6f 64 69 6e 67 20 6d 69 67 68 74 s encoding might
19916 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a be changed..*/.
19917 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
19918 36 34 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 64 sqlite3VdbeIn
19919 74 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d tValue(Mem *pMem
1991a 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a ){. int flags;.
1991b 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
1991c 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
1991d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d _mutex_held(pMem
1991e 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
1991f 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f assert( EIGHT_
19920 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 BYTE_ALIGNMENT(p
19921 4d 65 6d 29 20 29 3b 0a 20 20 66 6c 61 67 73 20 Mem) );. flags
19922 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 = pMem->flags;.
19923 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d if( flags & MEM
19924 5f 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75 _Int ){. retu
19925 72 6e 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 rn pMem->u.i;.
19926 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 }else if( flags
19927 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 & MEM_Real ){.
19928 20 20 72 65 74 75 72 6e 20 64 6f 75 62 6c 65 54 return doubleT
19929 6f 49 6e 74 36 34 28 70 4d 65 6d 2d 3e 72 29 3b oInt64(pMem->r);
1992a 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 . }else if( fla
1992b 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 gs & (MEM_Str|ME
1992c 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 M_Blob) ){. i
1992d 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 70 4d 64 value;. pM
1992e 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d em->flags |= MEM
1992f 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 20 73 71 _Str;. if( sq
19930 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
19931 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51 ncoding(pMem, SQ
19932 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 20 20 20 LITE_UTF8).
19933 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65 || sqlite3Vdbe
19934 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 MemNulTerminate(
19935 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20 20 72 pMem) ){. r
19936 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 eturn 0;. }.
19937 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d assert( pMem-
19938 3e 7a 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 >z );. sqlite
19939 33 41 74 6f 69 36 34 28 70 4d 65 6d 2d 3e 7a 2c 3Atoi64(pMem->z,
1993a 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 72 65 &value);. re
1993b 74 75 72 6e 20 76 61 6c 75 65 3b 0a 20 20 7d 65 turn value;. }e
1993c 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
1993d 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 0;. }.}../*.**
1993e 52 65 74 75 72 6e 20 74 68 65 20 62 65 73 74 20 Return the best
1993f 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f representation o
19940 66 20 70 4d 65 6d 20 74 68 61 74 20 77 65 20 63 f pMem that we c
19941 61 6e 20 67 65 74 20 69 6e 74 6f 20 61 0a 2a 2a an get into a.**
19942 20 64 6f 75 62 6c 65 2e 20 20 49 66 20 70 4d 65 double. If pMe
19943 6d 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 64 m is already a d
19944 6f 75 62 6c 65 20 6f 72 20 61 6e 20 69 6e 74 65 ouble or an inte
19945 67 65 72 2c 20 72 65 74 75 72 6e 20 69 74 73 0a ger, return its.
19946 2a 2a 20 76 61 6c 75 65 2e 20 20 49 66 20 69 74 ** value. If it
19947 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 is a string or
19948 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e blob, try to con
19949 76 65 72 74 20 69 74 20 74 6f 20 61 20 64 6f 75 vert it to a dou
1994a 62 6c 65 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 ble..** If it is
1994b 20 61 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 a NULL, return
1994c 30 2e 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 0.0..*/.SQLITE_P
1994d 52 49 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71 RIVATE double sq
1994e 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c lite3VdbeRealVal
1994f 75 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 ue(Mem *pMem){.
19950 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 assert( pMem->d
19951 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
19952 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d mutex_held(pMem-
19953 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
19954 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 assert( EIGHT_B
19955 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d YTE_ALIGNMENT(pM
19956 65 6d 29 20 29 3b 0a 20 20 69 66 28 20 70 4d 65 em) );. if( pMe
19957 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 m->flags & MEM_R
19958 65 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 eal ){. retur
19959 6e 20 70 4d 65 6d 2d 3e 72 3b 0a 20 20 7d 65 6c n pMem->r;. }el
1995a 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 se if( pMem->fla
1995b 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a gs & MEM_Int ){.
1995c 20 20 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62 return (doub
1995d 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 le)pMem->u.i;.
1995e 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e }else if( pMem->
1995f 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 flags & (MEM_Str
19960 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 |MEM_Blob) ){.
19961 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 /* (double)0 I
19962 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 n case of SQLITE
19963 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 _OMIT_FLOATING_P
19964 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 64 OINT... */. d
19965 6f 75 62 6c 65 20 76 61 6c 20 3d 20 28 64 6f 75 ouble val = (dou
19966 62 6c 65 29 30 3b 0a 20 20 20 20 70 4d 65 6d 2d ble)0;. pMem-
19967 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 >flags |= MEM_St
19968 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 r;. if( sqlit
19969 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f e3VdbeChangeEnco
1996a 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51 4c 49 54 ding(pMem, SQLIT
1996b 45 5f 55 54 46 38 29 0a 20 20 20 20 20 20 20 7c E_UTF8). |
1996c 7c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d | sqlite3VdbeMem
1996d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 4d 65 NulTerminate(pMe
1996e 6d 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 28 m) ){. /* (
1996f 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65 double)0 In case
19970 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f of SQLITE_OMIT_
19971 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e FLOATING_POINT..
19972 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 . */. retur
19973 6e 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 n (double)0;.
19974 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
19975 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71 Mem->z );. sq
19976 6c 69 74 65 33 41 74 6f 46 28 70 4d 65 6d 2d 3e lite3AtoF(pMem->
19977 7a 2c 20 26 76 61 6c 29 3b 0a 20 20 20 20 72 65 z, &val);. re
19978 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 65 6c 73 turn val;. }els
19979 65 7b 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c e{. /* (doubl
1997a 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 e)0 In case of S
1997b 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
1997c 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a ING_POINT... */.
1997d 20 20 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62 return (doub
1997e 6c 65 29 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a le)0;. }.}../*.
1997f 2a 2a 20 54 68 65 20 4d 45 4d 20 73 74 72 75 63 ** The MEM struc
19980 74 75 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 ture is already
19981 61 20 4d 45 4d 5f 52 65 61 6c 2e 20 20 54 72 79 a MEM_Real. Try
19982 20 74 6f 20 61 6c 73 6f 20 6d 61 6b 65 20 69 74 to also make it
19983 20 61 0a 2a 2a 20 4d 45 4d 5f 49 6e 74 20 69 66 a.** MEM_Int if
19984 20 77 65 20 63 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 we can..*/.SQLI
19985 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
19986 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 sqlite3VdbeInteg
19987 65 72 41 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a erAffinity(Mem *
19988 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 pMem){. assert(
19989 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d pMem->flags & M
1998a 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 61 73 73 EM_Real );. ass
1998b 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 ert( (pMem->flag
1998c 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d s & MEM_RowSet)=
1998d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1998e 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 pMem->db==0 || s
1998f 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
19990 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 d(pMem->db->mute
19991 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
19992 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e EIGHT_BYTE_ALIGN
19993 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 20 MENT(pMem) );..
19994 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 64 6f 75 pMem->u.i = dou
19995 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d bleToInt64(pMem-
19996 3e 72 29 3b 0a 0a 20 20 2f 2a 20 4f 6e 6c 79 20 >r);.. /* Only
19997 6d 61 72 6b 20 74 68 65 20 76 61 6c 75 65 20 61 mark the value a
19998 73 20 61 6e 20 69 6e 74 65 67 65 72 20 69 66 0a s an integer if.
19999 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 28 31 29 **. ** (1)
1999a 20 74 68 65 20 72 6f 75 6e 64 2d 74 72 69 70 20 the round-trip
1999b 63 6f 6e 76 65 72 73 69 6f 6e 20 72 65 61 6c 2d conversion real-
1999c 3e 69 6e 74 2d 3e 72 65 61 6c 20 69 73 20 61 20 >int->real is a
1999d 6e 6f 2d 6f 70 2c 20 61 6e 64 0a 20 20 2a 2a 20 no-op, and. **
1999e 20 20 20 28 32 29 20 54 68 65 20 69 6e 74 65 67 (2) The integ
1999f 65 72 20 69 73 20 6e 65 69 74 68 65 72 20 74 68 er is neither th
199a0 65 20 6c 61 72 67 65 73 74 20 6e 6f 72 20 74 68 e largest nor th
199a1 65 20 73 6d 61 6c 6c 65 73 74 0a 20 20 2a 2a 20 e smallest. **
199a2 20 20 20 20 20 20 20 70 6f 73 73 69 62 6c 65 20 possible
199a3 69 6e 74 65 67 65 72 20 28 74 69 63 6b 65 74 20 integer (ticket
199a4 23 33 39 32 32 29 0a 20 20 2a 2a 0a 20 20 2a 2a #3922). **. **
199a5 20 54 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 The second and
199a6 74 68 69 72 64 20 74 65 72 6d 73 20 69 6e 20 74 third terms in t
199a7 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e he following con
199a8 64 69 74 69 6f 6e 61 6c 20 65 6e 66 6f 72 63 65 ditional enforce
199a9 73 0a 20 20 2a 2a 20 74 68 65 20 73 65 63 6f 6e s. ** the secon
199aa 64 20 63 6f 6e 64 69 74 69 6f 6e 20 75 6e 64 65 d condition unde
199ab 72 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e r the assumption
199ac 20 74 68 61 74 20 61 64 64 69 74 69 6f 6e 20 6f that addition o
199ad 76 65 72 66 6c 6f 77 20 63 61 75 73 65 73 0a 20 verflow causes.
199ae 20 2a 2a 20 76 61 6c 75 65 73 20 74 6f 20 77 72 ** values to wr
199af 61 70 20 61 72 6f 75 6e 64 2e 20 20 4f 6e 20 78 ap around. On x
199b0 38 36 20 68 61 72 64 77 61 72 65 2c 20 74 68 65 86 hardware, the
199b1 20 74 68 69 72 64 20 74 65 72 6d 20 69 73 20 61 third term is a
199b2 6c 77 61 79 73 0a 20 20 2a 2a 20 74 72 75 65 20 lways. ** true
199b3 61 6e 64 20 63 6f 75 6c 64 20 62 65 20 6f 6d 69 and could be omi
199b4 74 74 65 64 2e 20 20 42 75 74 20 77 65 20 6c 65 tted. But we le
199b5 61 76 65 20 69 74 20 69 6e 20 62 65 63 61 75 73 ave it in becaus
199b6 65 20 6f 74 68 65 72 0a 20 20 2a 2a 20 61 72 63 e other. ** arc
199b7 68 69 74 65 63 74 75 72 65 73 20 6d 69 67 68 74 hitectures might
199b8 20 62 65 68 61 76 65 20 64 69 66 66 65 72 65 6e behave differen
199b9 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 tly.. */. if(
199ba 70 4d 65 6d 2d 3e 72 3d 3d 28 64 6f 75 62 6c 65 pMem->r==(double
199bb 29 70 4d 65 6d 2d 3e 75 2e 69 20 26 26 20 70 4d )pMem->u.i && pM
199bc 65 6d 2d 3e 75 2e 69 3e 53 4d 41 4c 4c 45 53 54 em->u.i>SMALLEST
199bd 5f 49 4e 54 36 34 0a 20 20 20 20 20 20 26 26 20 _INT64. &&
199be 41 4c 57 41 59 53 28 70 4d 65 6d 2d 3e 75 2e 69 ALWAYS(pMem->u.i
199bf 3c 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 29 20 <LARGEST_INT64)
199c0 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 ){. pMem->fla
199c1 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 gs |= MEM_Int;.
199c2 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 }.}../*.** Conv
199c3 65 72 74 20 70 4d 65 6d 20 74 6f 20 74 79 70 65 ert pMem to type
199c4 20 69 6e 74 65 67 65 72 2e 20 20 49 6e 76 61 6c integer. Inval
199c5 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 idate any prior
199c6 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e representations.
199c7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
199c8 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
199c9 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 beMemIntegerify(
199ca 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 Mem *pMem){. as
199cb 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d sert( pMem->db==
199cc 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
199cd 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 ex_held(pMem->db
199ce 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
199cf 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
199d0 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 gs & MEM_RowSet)
199d1 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
199d2 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 EIGHT_BYTE_ALIG
199d3 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a NMENT(pMem) );..
199d4 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 71 pMem->u.i = sq
199d5 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 lite3VdbeIntValu
199d6 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65 e(pMem);. MemSe
199d7 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 tTypeFlag(pMem,
199d8 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 72 65 74 75 MEM_Int);. retu
199d9 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
199da 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 ./*.** Convert p
199db 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74 20 69 Mem so that it i
199dc 73 20 6f 66 20 74 79 70 65 20 4d 45 4d 5f 52 65 s of type MEM_Re
199dd 61 6c 2e 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 al..** Invalidat
199de 65 20 61 6e 79 20 70 72 69 6f 72 20 72 65 70 72 e any prior repr
199df 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a esentations..*/.
199e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
199e1 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
199e2 6d 52 65 61 6c 69 66 79 28 4d 65 6d 20 2a 70 4d mRealify(Mem *pM
199e3 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 em){. assert( p
199e4 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
199e5 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
199e6 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
199e7 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 ) );. assert( E
199e8 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d IGHT_BYTE_ALIGNM
199e9 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 20 20 ENT(pMem) );..
199ea 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c 69 74 65 pMem->r = sqlite
199eb 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 3VdbeRealValue(p
199ec 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79 Mem);. MemSetTy
199ed 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d peFlag(pMem, MEM
199ee 5f 52 65 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e _Real);. return
199ef 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
199f0 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 4d 65 *.** Convert pMe
199f1 6d 20 73 6f 20 74 68 61 74 20 69 74 20 68 61 73 m so that it has
199f2 20 74 79 70 65 73 20 4d 45 4d 5f 52 65 61 6c 20 types MEM_Real
199f3 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f 72 20 62 6f or MEM_Int or bo
199f4 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 th..** Invalidat
199f5 65 20 61 6e 79 20 70 72 69 6f 72 20 72 65 70 72 e any prior repr
199f6 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a esentations..*/.
199f7 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
199f8 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
199f9 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d 20 2a 70 mNumerify(Mem *p
199fa 4d 65 6d 29 7b 0a 20 20 64 6f 75 62 6c 65 20 72 Mem){. double r
199fb 31 2c 20 72 32 3b 0a 20 20 69 36 34 20 69 3b 0a 1, r2;. i64 i;.
199fc 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
199fd 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e >flags & (MEM_In
199fe 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 4e t|MEM_Real|MEM_N
199ff 75 6c 6c 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73 ull))==0 );. as
19a00 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
19a01 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d gs & (MEM_Blob|M
19a02 45 4d 5f 53 74 72 29 29 21 3d 30 20 29 3b 0a 20 EM_Str))!=0 );.
19a03 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 assert( pMem->d
19a04 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
19a05 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d mutex_held(pMem-
19a06 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
19a07 20 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 r1 = sqlite3Vdb
19a08 65 52 65 61 6c 56 61 6c 75 65 28 70 4d 65 6d 29 eRealValue(pMem)
19a09 3b 0a 20 20 69 20 3d 20 64 6f 75 62 6c 65 54 6f ;. i = doubleTo
19a0a 49 6e 74 36 34 28 72 31 29 3b 0a 20 20 72 32 20 Int64(r1);. r2
19a0b 3d 20 28 64 6f 75 62 6c 65 29 69 3b 0a 20 20 69 = (double)i;. i
19a0c 66 28 20 72 31 3d 3d 72 32 20 29 7b 0a 20 20 20 f( r1==r2 ){.
19a0d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 sqlite3VdbeMemI
19a0e 6e 74 65 67 65 72 69 66 79 28 70 4d 65 6d 29 3b ntegerify(pMem);
19a0f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d . }else{. pM
19a10 65 6d 2d 3e 72 20 3d 20 72 31 3b 0a 20 20 20 20 em->r = r1;.
19a11 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 MemSetTypeFlag(p
19a12 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a Mem, MEM_Real);.
19a13 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
19a14 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
19a15 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 Delete any prev
19a16 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 ious value and s
19a17 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f et the value sto
19a18 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 red in *pMem to
19a19 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f NULL..*/.SQLITE_
19a1a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19a1b 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 ite3VdbeMemSetNu
19a1c 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 ll(Mem *pMem){.
19a1d 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 if( pMem->flags
19a1e 20 26 20 4d 45 4d 5f 46 72 61 6d 65 20 29 7b 0a & MEM_Frame ){.
19a1f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 sqlite3VdbeF
19a20 72 61 6d 65 44 65 6c 65 74 65 28 70 4d 65 6d 2d rameDelete(pMem-
19a21 3e 75 2e 70 46 72 61 6d 65 29 3b 0a 20 20 7d 0a >u.pFrame);. }.
19a22 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 if( pMem->flag
19a23 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 20 29 s & MEM_RowSet )
19a24 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 {. sqlite3Row
19a25 53 65 74 43 6c 65 61 72 28 70 4d 65 6d 2d 3e 75 SetClear(pMem->u
19a26 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 7d 0a 20 .pRowSet);. }.
19a27 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 MemSetTypeFlag(
19a28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b pMem, MEM_Null);
19a29 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 . pMem->type =
19a2a 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 7d 0a 0a SQLITE_NULL;.}..
19a2b 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 /*.** Delete any
19a2c 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 previous value
19a2d 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 and set the valu
19a2e 65 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20 6f e to be a BLOB o
19a2f 66 20 6c 65 6e 67 74 68 0a 2a 2a 20 6e 20 63 6f f length.** n co
19a30 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 7a 65 72 ntaining all zer
19a31 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 os..*/.SQLITE_PR
19a32 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
19a33 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f e3VdbeMemSetZero
19a34 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 Blob(Mem *pMem,
19a35 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c 69 74 65 int n){. sqlite
19a36 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
19a37 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 66 pMem);. pMem->f
19a38 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c lags = MEM_Blob|
19a39 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 70 4d 65 6d MEM_Zero;. pMem
19a3a 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
19a3b 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d 2d 3e 6e 20 BLOB;. pMem->n
19a3c 3d 20 30 3b 0a 20 20 69 66 28 20 6e 3c 30 20 29 = 0;. if( n<0 )
19a3d 20 6e 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e n = 0;. pMem->
19a3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e 3b 0a 20 20 70 u.nZero = n;. p
19a3f 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 Mem->enc = SQLIT
19a40 45 5f 55 54 46 38 3b 0a 0a 23 69 66 64 65 66 20 E_UTF8;..#ifdef
19a41 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 SQLITE_OMIT_INCR
19a42 42 4c 4f 42 0a 20 20 73 71 6c 69 74 65 33 56 64 BLOB. sqlite3Vd
19a43 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 beMemGrow(pMem,
19a44 6e 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 4d 65 n, 0);. if( pMe
19a45 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 70 4d 65 6d m->z ){. pMem
19a46 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 20 20 6d 65 6d ->n = n;. mem
19a47 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 set(pMem->z, 0,
19a48 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d n);. }.#endif.}
19a49 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 ../*.** Delete a
19a4a 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 ny previous valu
19a4b 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 e and set the va
19a4c 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 lue stored in *p
19a4d 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d Mem to val,.** m
19a4e 61 6e 69 66 65 73 74 20 74 79 70 65 20 49 4e 54 anifest type INT
19a4f 45 47 45 52 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f EGER..*/.SQLITE_
19a50 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19a51 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e ite3VdbeMemSetIn
19a52 74 36 34 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 t64(Mem *pMem, i
19a53 36 34 20 76 61 6c 29 7b 0a 20 20 73 71 6c 69 74 64 val){. sqlit
19a54 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
19a55 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e (pMem);. pMem->
19a56 75 2e 69 20 3d 20 76 61 6c 3b 0a 20 20 70 4d 65 u.i = val;. pMe
19a57 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 m->flags = MEM_I
19a58 6e 74 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 nt;. pMem->type
19a59 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 = SQLITE_INTEGE
19a5a 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 R;.}../*.** Dele
19a5b 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 te any previous
19a5c 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 value and set th
19a5d 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 e value stored i
19a5e 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a n *pMem to val,.
19a5f 2a 2a 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 ** manifest type
19a60 20 52 45 41 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 REAL..*/.SQLITE
19a61 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
19a62 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 lite3VdbeMemSetD
19a63 6f 75 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c ouble(Mem *pMem,
19a64 20 64 6f 75 62 6c 65 20 76 61 6c 29 7b 0a 20 20 double val){.
19a65 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e if( sqlite3IsNaN
19a66 28 76 61 6c 29 20 29 7b 0a 20 20 20 20 73 71 6c (val) ){. sql
19a67 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 ite3VdbeMemSetNu
19a68 6c 6c 28 70 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 ll(pMem);. }els
19a69 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 e{. sqlite3Vd
19a6a 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 beMemRelease(pMe
19a6b 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 72 20 m);. pMem->r
19a6c 3d 20 76 61 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d = val;. pMem-
19a6d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 65 61 >flags = MEM_Rea
19a6e 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 l;. pMem->typ
19a6f 65 20 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 e = SQLITE_FLOAT
19a70 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 ;. }.}../*.** D
19a71 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f elete any previo
19a72 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74 us value and set
19a73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 70 4d the value of pM
19a74 65 6d 20 74 6f 20 62 65 20 61 6e 0a 2a 2a 20 65 em to be an.** e
19a75 6d 70 74 79 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 mpty boolean ind
19a76 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ex..*/.SQLITE_PR
19a77 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
19a78 65 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 e3VdbeMemSetRowS
19a79 65 74 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 et(Mem *pMem){.
19a7a 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
19a7b 4d 65 6d 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 Mem->db;. asser
19a7c 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 t( db!=0 );. as
19a7d 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
19a7e 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 gs & MEM_RowSet)
19a7f 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ==0 );. sqlite3
19a80 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 VdbeMemRelease(p
19a81 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 4d Mem);. pMem->zM
19a82 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 alloc = sqlite3D
19a83 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 36 bMallocRaw(db, 6
19a84 34 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 4);. if( db->ma
19a85 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
19a86 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
19a87 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 MEM_Null;. }els
19a88 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e{. assert( p
19a89 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a Mem->zMalloc );.
19a8a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 pMem->u.pRow
19a8b 53 65 74 20 3d 20 73 71 6c 69 74 65 33 52 6f 77 Set = sqlite3Row
19a8c 53 65 74 49 6e 69 74 28 64 62 2c 20 70 4d 65 6d SetInit(db, pMem
19a8d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 0a 20 20 20 20 ->zMalloc, .
19a8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19a8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19a90 20 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c sqlite3DbMall
19a91 6f 63 53 69 7a 65 28 64 62 2c 20 70 4d 65 6d 2d ocSize(db, pMem-
19a92 3e 7a 4d 61 6c 6c 6f 63 29 29 3b 0a 20 20 20 20 >zMalloc));.
19a93 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 75 2e assert( pMem->u.
19a94 70 52 6f 77 53 65 74 21 3d 30 20 29 3b 0a 20 20 pRowSet!=0 );.
19a95 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
19a96 4d 45 4d 5f 52 6f 77 53 65 74 3b 0a 20 20 7d 0a MEM_RowSet;. }.
19a97 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
19a98 74 72 75 65 20 69 66 20 74 68 65 20 4d 65 6d 20 true if the Mem
19a99 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73 20 object contains
19a9a 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 74 a TEXT or BLOB t
19a9b 68 61 74 20 69 73 0a 2a 2a 20 74 6f 6f 20 6c 61 hat is.** too la
19a9c 72 67 65 20 2d 20 77 68 6f 73 65 20 73 69 7a 65 rge - whose size
19a9d 20 65 78 63 65 65 64 73 20 53 51 4c 49 54 45 5f exceeds SQLITE_
19a9e 4d 41 58 5f 4c 45 4e 47 54 48 2e 0a 2a 2f 0a 53 MAX_LENGTH..*/.S
19a9f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
19aa0 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
19aa1 54 6f 6f 42 69 67 28 4d 65 6d 20 2a 70 29 7b 0a TooBig(Mem *p){.
19aa2 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 21 assert( p->db!
19aa3 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66 =0 );. if( p->f
19aa4 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c lags & (MEM_Str|
19aa5 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 MEM_Blob) ){.
19aa6 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 3b 0a 20 int n = p->n;.
19aa7 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 if( p->flags
19aa8 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 & MEM_Zero ){.
19aa9 20 20 20 20 6e 20 2b 3d 20 70 2d 3e 75 2e 6e 5a n += p->u.nZ
19aaa 65 72 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 ero;. }. r
19aab 65 74 75 72 6e 20 6e 3e 70 2d 3e 64 62 2d 3e 61 eturn n>p->db->a
19aac 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
19aad 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a IT_LENGTH];. }.
19aae 20 20 72 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a return 0; .}..
19aaf 2f 2a 0a 2a 2a 20 53 69 7a 65 20 6f 66 20 73 74 /*.** Size of st
19ab0 72 75 63 74 20 4d 65 6d 20 6e 6f 74 20 69 6e 63 ruct Mem not inc
19ab1 6c 75 64 69 6e 67 20 74 68 65 20 4d 65 6d 2e 7a luding the Mem.z
19ab2 4d 61 6c 6c 6f 63 20 6d 65 6d 62 65 72 2e 0a 2a Malloc member..*
19ab3 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 43 45 4c /.#define MEMCEL
19ab4 4c 53 49 5a 45 20 28 73 69 7a 65 5f 74 29 28 26 LSIZE (size_t)(&
19ab5 28 28 28 4d 65 6d 20 2a 29 30 29 2d 3e 7a 4d 61 (((Mem *)0)->zMa
19ab6 6c 6c 6f 63 29 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 lloc))../*.** Ma
19ab7 6b 65 20 61 6e 20 73 68 61 6c 6c 6f 77 20 63 6f ke an shallow co
19ab8 70 79 20 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f py of pFrom into
19ab9 20 70 54 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e pTo. Prior con
19aba 74 65 6e 74 73 20 6f 66 0a 2a 2a 20 70 54 6f 20 tents of.** pTo
19abb 61 72 65 20 66 72 65 65 64 2e 20 20 54 68 65 20 are freed. The
19abc 70 46 72 6f 6d 2d 3e 7a 20 66 69 65 6c 64 20 69 pFrom->z field i
19abd 73 20 6e 6f 74 20 64 75 70 6c 69 63 61 74 65 64 s not duplicated
19abe 2e 20 20 49 66 0a 2a 2a 20 70 46 72 6f 6d 2d 3e . If.** pFrom->
19abf 7a 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 z is used, then
19ac0 70 54 6f 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f pTo->z points to
19ac1 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20 the same thing
19ac2 61 73 20 70 46 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61 as pFrom->z.** a
19ac3 6e 64 20 66 6c 61 67 73 20 67 65 74 73 20 73 72 nd flags gets sr
19ac4 63 54 79 70 65 20 28 65 69 74 68 65 72 20 4d 45 cType (either ME
19ac5 4d 5f 45 70 68 65 6d 20 6f 72 20 4d 45 4d 5f 53 M_Ephem or MEM_S
19ac6 74 61 74 69 63 29 2e 0a 2a 2f 0a 53 51 4c 49 54 tatic)..*/.SQLIT
19ac7 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
19ac8 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 qlite3VdbeMemSha
19ac9 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d 20 2a 70 54 llowCopy(Mem *pT
19aca 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 o, const Mem *pF
19acb 72 6f 6d 2c 20 69 6e 74 20 73 72 63 54 79 70 65 rom, int srcType
19acc 29 7b 0a 20 20 61 73 73 65 72 74 28 20 28 70 46 ){. assert( (pF
19acd 72 6f 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d rom->flags & MEM
19ace 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 _RowSet)==0 );.
19acf 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
19ad0 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 eleaseExternal(p
19ad1 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 To);. memcpy(pT
19ad2 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c o, pFrom, MEMCEL
19ad3 4c 53 49 5a 45 29 3b 0a 20 20 70 54 6f 2d 3e 78 LSIZE);. pTo->x
19ad4 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 28 Del = 0;. if( (
19ad5 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d pFrom->flags&MEM
19ad6 5f 44 79 6e 29 21 3d 30 20 7c 7c 20 70 46 72 6f _Dyn)!=0 || pFro
19ad7 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d 2d 3e 7a 4d 61 m->z==pFrom->zMa
19ad8 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 70 54 6f 2d lloc ){. pTo-
19ad9 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f >flags &= ~(MEM_
19ada 44 79 6e 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d Dyn|MEM_Static|M
19adb 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 61 EM_Ephem);. a
19adc 73 73 65 72 74 28 20 73 72 63 54 79 70 65 3d 3d ssert( srcType==
19add 4d 45 4d 5f 45 70 68 65 6d 20 7c 7c 20 73 72 63 MEM_Ephem || src
19ade 54 79 70 65 3d 3d 4d 45 4d 5f 53 74 61 74 69 63 Type==MEM_Static
19adf 20 29 3b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c 61 );. pTo->fla
19ae0 67 73 20 7c 3d 20 73 72 63 54 79 70 65 3b 0a 20 gs |= srcType;.
19ae1 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 }.}../*.** Make
19ae2 20 61 20 66 75 6c 6c 20 63 6f 70 79 20 6f 66 20 a full copy of
19ae3 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20 pFrom into pTo.
19ae4 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20 Prior contents
19ae5 6f 66 20 70 54 6f 20 61 72 65 0a 2a 2a 20 66 72 of pTo are.** fr
19ae6 65 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 63 eed before the c
19ae7 6f 70 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2f 0a opy is made..*/.
19ae8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
19ae9 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
19aea 6d 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 mCopy(Mem *pTo,
19aeb 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d const Mem *pFrom
19aec 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
19aed 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 LITE_OK;.. asse
19aee 72 74 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 rt( (pFrom->flag
19aef 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d s & MEM_RowSet)=
19af0 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 =0 );. sqlite3V
19af1 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 dbeMemReleaseExt
19af2 65 72 6e 61 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 ernal(pTo);. me
19af3 6d 63 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c mcpy(pTo, pFrom,
19af4 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 MEMCELLSIZE);.
19af5 20 70 54 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e pTo->flags &= ~
19af6 4d 45 4d 5f 44 79 6e 3b 0a 0a 20 20 69 66 28 20 MEM_Dyn;.. if(
19af7 70 54 6f 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f pTo->flags&(MEM_
19af8 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b Str|MEM_Blob) ){
19af9 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 70 46 72 . if( 0==(pFr
19afa 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 om->flags&MEM_St
19afb 61 74 69 63 29 20 29 7b 0a 20 20 20 20 20 20 70 atic) ){. p
19afc 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d To->flags |= MEM
19afd 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 72 63 _Ephem;. rc
19afe 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 = sqlite3VdbeMe
19aff 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 mMakeWriteable(p
19b00 54 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a To);. }. }..
19b01 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
19b02 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 74 /*.** Transfer t
19b03 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 he contents of p
19b04 46 72 6f 6d 20 74 6f 20 70 54 6f 2e 20 41 6e 79 From to pTo. Any
19b05 20 65 78 69 73 74 69 6e 67 20 76 61 6c 75 65 20 existing value
19b06 69 6e 20 70 54 6f 20 69 73 0a 2a 2a 20 66 72 65 in pTo is.** fre
19b07 65 64 2e 20 49 66 20 70 46 72 6f 6d 20 63 6f 6e ed. If pFrom con
19b08 74 61 69 6e 73 20 65 70 68 65 6d 65 72 61 6c 20 tains ephemeral
19b09 64 61 74 61 2c 20 61 20 63 6f 70 79 20 69 73 20 data, a copy is
19b0a 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a 20 70 46 72 6f made..**.** pFro
19b0b 6d 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 53 51 m contains an SQ
19b0c 4c 20 4e 55 4c 4c 20 77 68 65 6e 20 74 68 69 73 L NULL when this
19b0d 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
19b0e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19b0f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
19b10 56 64 62 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d 20 VdbeMemMove(Mem
19b11 2a 70 54 6f 2c 20 4d 65 6d 20 2a 70 46 72 6f 6d *pTo, Mem *pFrom
19b12 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 46 72 ){. assert( pFr
19b13 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c om->db==0 || sql
19b14 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
19b15 70 46 72 6f 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 pFrom->db->mutex
19b16 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
19b17 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c To->db==0 || sql
19b18 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
19b19 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 pTo->db->mutex)
19b1a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 72 );. assert( pFr
19b1b 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 54 6f om->db==0 || pTo
19b1c 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 46 72 6f 6d ->db==0 || pFrom
19b1d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e 64 62 20 29 3b ->db==pTo->db );
19b1e 0a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d .. sqlite3VdbeM
19b1f 65 6d 52 65 6c 65 61 73 65 28 70 54 6f 29 3b 0a emRelease(pTo);.
19b20 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 memcpy(pTo, pF
19b21 72 6f 6d 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 rom, sizeof(Mem)
19b22 29 3b 0a 20 20 70 46 72 6f 6d 2d 3e 66 6c 61 67 );. pFrom->flag
19b23 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
19b24 70 46 72 6f 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b pFrom->xDel = 0;
19b25 0a 20 20 70 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f . pFrom->zMallo
19b26 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 c = 0;.}../*.**
19b27 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 Change the value
19b28 20 6f 66 20 61 20 4d 65 6d 20 74 6f 20 62 65 20 of a Mem to be
19b29 61 20 73 74 72 69 6e 67 20 6f 72 20 61 20 42 4c a string or a BL
19b2a 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 OB..**.** The me
19b2b 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20 mory management
19b2c 73 74 72 61 74 65 67 79 20 64 65 70 65 6e 64 73 strategy depends
19b2d 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 on the value of
19b2e 20 74 68 65 20 78 44 65 6c 0a 2a 2a 20 70 61 72 the xDel.** par
19b2f 61 6d 65 74 65 72 2e 20 49 66 20 74 68 65 20 76 ameter. If the v
19b30 61 6c 75 65 20 70 61 73 73 65 64 20 69 73 20 53 alue passed is S
19b31 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2c QLITE_TRANSIENT,
19b32 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 73 74 then the .** st
19b33 72 69 6e 67 20 69 73 20 63 6f 70 69 65 64 20 69 ring is copied i
19b34 6e 74 6f 20 61 20 28 70 6f 73 73 69 62 6c 79 20 nto a (possibly
19b35 65 78 69 73 74 69 6e 67 29 20 62 75 66 66 65 72 existing) buffer
19b36 20 6d 61 6e 61 67 65 64 20 62 79 20 74 68 65 20 managed by the
19b37 0a 2a 2a 20 4d 65 6d 20 73 74 72 75 63 74 75 72 .** Mem structur
19b38 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e e. Otherwise, an
19b39 79 20 65 78 69 73 74 69 6e 67 20 62 75 66 66 65 y existing buffe
19b3a 72 20 69 73 20 66 72 65 65 64 20 61 6e 64 20 74 r is freed and t
19b3b 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 63 6f he.** pointer co
19b3c 70 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 pied..**.** If t
19b3d 68 65 20 73 74 72 69 6e 67 20 69 73 20 74 6f 6f he string is too
19b3e 20 6c 61 72 67 65 20 28 69 66 20 69 74 20 65 78 large (if it ex
19b3f 63 65 65 64 73 20 74 68 65 20 53 51 4c 49 54 45 ceeds the SQLITE
19b40 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 0a 2a 2a _LIMIT_LENGTH.**
19b41 20 73 69 7a 65 20 6c 69 6d 69 74 29 20 74 68 65 size limit) the
19b42 6e 20 6e 6f 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f n no memory allo
19b43 63 61 74 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20 cation occurs.
19b44 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 63 61 If the string ca
19b45 6e 20 62 65 0a 2a 2a 20 73 74 6f 72 65 64 20 77 n be.** stored w
19b46 69 74 68 6f 75 74 20 61 6c 6c 6f 63 61 74 69 6e ithout allocatin
19b47 67 20 6d 65 6d 6f 72 79 2c 20 74 68 65 6e 20 69 g memory, then i
19b48 74 20 69 73 2e 20 20 49 66 20 61 20 6d 65 6d 6f t is. If a memo
19b49 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a ry allocation.**
19b4a 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 is required to
19b4b 73 74 6f 72 65 20 74 68 65 20 73 74 72 69 6e 67 store the string
19b4c 2c 20 74 68 65 6e 20 76 61 6c 75 65 20 6f 66 20 , then value of
19b4d 70 4d 65 6d 20 69 73 20 75 6e 63 68 61 6e 67 65 pMem is unchange
19b4e 64 2e 20 20 49 6e 0a 2a 2a 20 65 69 74 68 65 72 d. In.** either
19b4f 20 63 61 73 65 2c 20 53 51 4c 49 54 45 5f 54 4f case, SQLITE_TO
19b50 4f 42 49 47 20 69 73 20 72 65 74 75 72 6e 65 64 OBIG is returned
19b51 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19b52 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
19b53 64 62 65 4d 65 6d 53 65 74 53 74 72 28 0a 20 20 dbeMemSetStr(.
19b54 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20 20 20 20 20 Mem *pMem,
19b55 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 /* Memory ce
19b56 6c 6c 20 74 6f 20 73 65 74 20 74 6f 20 73 74 72 ll to set to str
19b57 69 6e 67 20 76 61 6c 75 65 20 2a 2f 0a 20 20 63 ing value */. c
19b58 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 20 20 onst char *z,
19b59 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 70 6f 69 /* String poi
19b5a 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c nter */. int n,
19b5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19b5c 20 42 79 74 65 73 20 69 6e 20 73 74 72 69 6e 67 Bytes in string
19b5d 2c 20 6f 72 20 6e 65 67 61 74 69 76 65 20 2a 2f , or negative */
19b5e 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 . u8 enc,
19b5f 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 /* Encodi
19b60 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f 72 20 ng of z. 0 for
19b61 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 64 20 BLOBs */. void
19b62 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 2f (*xDel)(void*) /
19b63 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 75 6e * Destructor fun
19b64 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e ction */.){. in
19b65 74 20 6e 42 79 74 65 20 3d 20 6e 3b 20 20 20 20 t nByte = n;
19b66 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65 20 66 /* New value f
19b67 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f 0a 20 20 or pMem->n */.
19b68 69 6e 74 20 69 4c 69 6d 69 74 3b 20 20 20 20 20 int iLimit;
19b69 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 /* Maximum a
19b6a 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6f 72 llowed string or
19b6b 20 62 6c 6f 62 20 73 69 7a 65 20 2a 2f 0a 20 20 blob size */.
19b6c 75 31 36 20 66 6c 61 67 73 20 3d 20 30 3b 20 20 u16 flags = 0;
19b6d 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65 /* New value
19b6e 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 for pMem->flags
19b6f 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
19b70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
19b71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
19b72 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
19b73 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 ) );. assert( (
19b74 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pMem->flags & ME
19b75 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a M_RowSet)==0 );.
19b76 0a 20 20 2f 2a 20 49 66 20 7a 20 69 73 20 61 20 . /* If z is a
19b77 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 73 65 NULL pointer, se
19b78 74 20 70 4d 65 6d 20 74 6f 20 63 6f 6e 74 61 69 t pMem to contai
19b79 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 2a n an SQL NULL. *
19b7a 2f 0a 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 20 /. if( !z ){.
19b7b 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
19b7c 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 SetNull(pMem);.
19b7d 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
19b7e 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 _OK;. }.. if(
19b7f 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20 pMem->db ){.
19b80 69 4c 69 6d 69 74 20 3d 20 70 4d 65 6d 2d 3e 64 iLimit = pMem->d
19b81 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
19b82 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a _LIMIT_LENGTH];.
19b83 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69 }else{. iLi
19b84 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 mit = SQLITE_MAX
19b85 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d 0a 20 20 66 _LENGTH;. }. f
19b86 6c 61 67 73 20 3d 20 28 65 6e 63 3d 3d 30 3f 4d lags = (enc==0?M
19b87 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f 53 74 72 29 EM_Blob:MEM_Str)
19b88 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 30 20 ;. if( nByte<0
19b89 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 ){. assert( e
19b8a 6e 63 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 nc!=0 );. if(
19b8b 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 enc==SQLITE_UTF
19b8c 38 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6e 8 ){. for(n
19b8d 42 79 74 65 3d 30 3b 20 6e 42 79 74 65 3c 3d 69 Byte=0; nByte<=i
19b8e 4c 69 6d 69 74 20 26 26 20 7a 5b 6e 42 79 74 65 Limit && z[nByte
19b8f 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b 7d 0a 20 20 ]; nByte++){}.
19b90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 }else{. f
19b91 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 or(nByte=0; nByt
19b92 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 28 7a 5b e<=iLimit && (z[
19b93 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e 42 79 74 65 nByte] | z[nByte
19b94 2b 31 5d 29 3b 20 6e 42 79 74 65 2b 3d 32 29 7b +1]); nByte+=2){
19b95 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6c 61 67 }. }. flag
19b96 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 s |= MEM_Term;.
19b97 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c }.. /* The fol
19b98 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 73 65 74 lowing block set
19b99 73 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 73 s the new values
19b9a 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e 64 20 4d 65 of Mem.z and Me
19b9b 6d 2e 78 44 65 6c 2e 20 49 74 0a 20 20 2a 2a 20 m.xDel. It. **
19b9c 61 6c 73 6f 20 73 65 74 73 20 61 20 66 6c 61 67 also sets a flag
19b9d 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 in local variab
19b9e 6c 65 20 22 66 6c 61 67 73 22 20 74 6f 20 69 6e le "flags" to in
19b9f 64 69 63 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 dicate the memor
19ba0 79 0a 20 20 2a 2a 20 6d 61 6e 61 67 65 6d 65 6e y. ** managemen
19ba1 74 20 28 6f 6e 65 20 6f 66 20 4d 45 4d 5f 44 79 t (one of MEM_Dy
19ba2 6e 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 29 n or MEM_Static)
19ba3 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 78 44 65 .. */. if( xDe
19ba4 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 l==SQLITE_TRANSI
19ba5 45 4e 54 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e ENT ){. int n
19ba6 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 65 3b 0a 20 Alloc = nByte;.
19ba7 20 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d if( flags&MEM
19ba8 5f 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 6e _Term ){. n
19ba9 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63 3d 3d 53 Alloc += (enc==S
19baa 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b QLITE_UTF8?1:2);
19bab 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e . }. if( n
19bac 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 Byte>iLimit ){.
19bad 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
19bae 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 20 20 7d TE_TOOBIG;. }
19baf 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
19bb0 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d VdbeMemGrow(pMem
19bb1 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29 20 29 7b 0a , nAlloc, 0) ){.
19bb2 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
19bb3 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
19bb4 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d . memcpy(pMem
19bb5 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f 63 29 3b ->z, z, nAlloc);
19bb6 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78 44 65 . }else if( xDe
19bb7 6c 3d 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 l==SQLITE_DYNAMI
19bb8 43 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 C ){. sqlite3
19bb9 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 VdbeMemRelease(p
19bba 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e Mem);. pMem->
19bbb 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e zMalloc = pMem->
19bbc 7a 20 3d 20 28 63 68 61 72 20 2a 29 7a 3b 0a 20 z = (char *)z;.
19bbd 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 pMem->xDel =
19bbe 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 0;. }else{.
19bbf 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
19bc0 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 lease(pMem);.
19bc1 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 pMem->z = (char
19bc2 20 2a 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e *)z;. pMem->
19bc3 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20 xDel = xDel;.
19bc4 20 66 6c 61 67 73 20 7c 3d 20 28 28 78 44 65 6c flags |= ((xDel
19bc5 3d 3d 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 ==SQLITE_STATIC)
19bc6 3f 4d 45 4d 5f 53 74 61 74 69 63 3a 4d 45 4d 5f ?MEM_Static:MEM_
19bc7 44 79 6e 29 3b 0a 20 20 7d 0a 0a 20 20 70 4d 65 Dyn);. }.. pMe
19bc8 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b 0a 20 20 m->n = nByte;.
19bc9 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 66 6c pMem->flags = fl
19bca 61 67 73 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 ags;. pMem->enc
19bcb 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c = (enc==0 ? SQL
19bcc 49 54 45 5f 55 54 46 38 20 3a 20 65 6e 63 29 3b ITE_UTF8 : enc);
19bcd 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 . pMem->type =
19bce 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c 49 54 45 (enc==0 ? SQLITE
19bcf 5f 42 4c 4f 42 20 3a 20 53 51 4c 49 54 45 5f 54 _BLOB : SQLITE_T
19bd0 45 58 54 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 EXT);..#ifndef S
19bd1 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
19bd2 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 . if( pMem->enc
19bd3 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26 26 !=SQLITE_UTF8 &&
19bd4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 sqlite3VdbeMemH
19bd5 61 6e 64 6c 65 42 6f 6d 28 70 4d 65 6d 29 20 29 andleBom(pMem) )
19bd6 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
19bd7 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 ITE_NOMEM;. }.#
19bd8 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e 42 79 endif.. if( nBy
19bd9 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 te>iLimit ){.
19bda 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 54 return SQLITE_T
19bdb 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a 20 20 72 65 OOBIG;. }.. re
19bdc 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
19bdd 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 }../*.** Compare
19bde 20 74 68 65 20 76 61 6c 75 65 73 20 63 6f 6e 74 the values cont
19bdf 61 69 6e 65 64 20 62 79 20 74 68 65 20 74 77 6f ained by the two
19be0 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 2c 20 72 memory cells, r
19be1 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 6e 65 67 61 eturning.** nega
19be2 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f tive, zero or po
19be3 73 69 74 69 76 65 20 69 66 20 70 4d 65 6d 31 20 sitive if pMem1
19be4 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 is less than, eq
19be5 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 ual to, or great
19be6 65 72 0a 2a 2a 20 74 68 61 6e 20 70 4d 65 6d 32 er.** than pMem2
19be7 2e 20 53 6f 72 74 69 6e 67 20 6f 72 64 65 72 20 . Sorting order
19be8 69 73 20 4e 55 4c 4c 27 73 20 66 69 72 73 74 2c is NULL's first,
19be9 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e 75 6d followed by num
19bea 62 65 72 73 20 28 69 6e 74 65 67 65 72 73 0a 2a bers (integers.*
19beb 2a 20 61 6e 64 20 72 65 61 6c 73 29 20 73 6f 72 * and reals) sor
19bec 74 65 64 20 6e 75 6d 65 72 69 63 61 6c 6c 79 2c ted numerically,
19bed 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 74 65 78 followed by tex
19bee 74 20 6f 72 64 65 72 65 64 20 62 79 20 74 68 65 t ordered by the
19bef 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 collating.** se
19bf0 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20 61 6e 64 quence pColl and
19bf1 20 66 69 6e 61 6c 6c 79 20 62 6c 6f 62 27 73 20 finally blob's
19bf2 6f 72 64 65 72 65 64 20 62 79 20 6d 65 6d 63 6d ordered by memcm
19bf3 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 77 6f 20 4e p()..**.** Two N
19bf4 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20 63 ULL values are c
19bf5 6f 6e 73 69 64 65 72 65 64 20 65 71 75 61 6c 20 onsidered equal
19bf6 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e by this function
19bf7 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19bf8 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d ATE int sqlite3M
19bf9 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 emCompare(const
19bfa 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20 63 6f 6e 73 Mem *pMem1, cons
19bfb 74 20 4d 65 6d 20 2a 70 4d 65 6d 32 2c 20 63 6f t Mem *pMem2, co
19bfc 6e 73 74 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f nst CollSeq *pCo
19bfd 6c 6c 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ll){. int rc;.
19bfe 20 69 6e 74 20 66 31 2c 20 66 32 3b 0a 20 20 69 int f1, f2;. i
19bff 6e 74 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 nt combined_flag
19c00 73 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 72 63 68 s;.. /* Interch
19c01 61 6e 67 65 20 70 4d 65 6d 31 20 61 6e 64 20 70 ange pMem1 and p
19c02 4d 65 6d 32 20 69 66 20 74 68 65 20 63 6f 6c 6c Mem2 if the coll
19c03 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 73 ating sequence s
19c04 70 65 63 69 66 69 65 73 0a 20 20 2a 2a 20 44 45 pecifies. ** DE
19c05 53 43 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 SC order.. */.
19c06 20 66 31 20 3d 20 70 4d 65 6d 31 2d 3e 66 6c 61 f1 = pMem1->fla
19c07 67 73 3b 0a 20 20 66 32 20 3d 20 70 4d 65 6d 32 gs;. f2 = pMem2
19c08 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6d 62 69 ->flags;. combi
19c09 6e 65 64 5f 66 6c 61 67 73 20 3d 20 66 31 7c 66 ned_flags = f1|f
19c0a 32 3b 0a 20 20 61 73 73 65 72 74 28 20 28 63 6f 2;. assert( (co
19c0b 6d 62 69 6e 65 64 5f 66 6c 61 67 73 20 26 20 4d mbined_flags & M
19c0c 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b EM_RowSet)==0 );
19c0d 0a 20 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 . . /* If one v
19c0e 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20 69 74 alue is NULL, it
19c0f 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 is less than th
19c10 65 20 6f 74 68 65 72 2e 20 49 66 20 62 6f 74 68 e other. If both
19c11 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20 61 72 65 values. ** are
19c12 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e NULL, return 0.
19c13 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 . */. if( comb
19c14 69 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 4e ined_flags&MEM_N
19c15 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ull ){. retur
19c16 6e 20 28 66 32 26 4d 45 4d 5f 4e 75 6c 6c 29 20 n (f2&MEM_Null)
19c17 2d 20 28 66 31 26 4d 45 4d 5f 4e 75 6c 6c 29 3b - (f1&MEM_Null);
19c18 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e . }.. /* If on
19c19 65 20 76 61 6c 75 65 20 69 73 20 61 20 6e 75 6d e value is a num
19c1a 62 65 72 20 61 6e 64 20 74 68 65 20 6f 74 68 65 ber and the othe
19c1b 72 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 6e 75 r is not, the nu
19c1c 6d 62 65 72 20 69 73 20 6c 65 73 73 2e 0a 20 20 mber is less..
19c1d 2a 2a 20 49 66 20 62 6f 74 68 20 61 72 65 20 6e ** If both are n
19c1e 75 6d 62 65 72 73 2c 20 63 6f 6d 70 61 72 65 20 umbers, compare
19c1f 61 73 20 72 65 61 6c 73 20 69 66 20 6f 6e 65 20 as reals if one
19c20 69 73 20 61 20 72 65 61 6c 2c 20 6f 72 20 61 73 is a real, or as
19c21 20 69 6e 74 65 67 65 72 73 0a 20 20 2a 2a 20 69 integers. ** i
19c22 66 20 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 f both values ar
19c23 65 20 69 6e 74 65 67 65 72 73 2e 0a 20 20 2a 2f e integers.. */
19c24 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f . if( combined_
19c25 66 6c 61 67 73 26 28 4d 45 4d 5f 49 6e 74 7c 4d flags&(MEM_Int|M
19c26 45 4d 5f 52 65 61 6c 29 20 29 7b 0a 20 20 20 20 EM_Real) ){.
19c27 69 66 28 20 21 28 66 31 26 28 4d 45 4d 5f 49 6e if( !(f1&(MEM_In
19c28 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a t|MEM_Real)) ){.
19c29 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
19c2a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 28 }. if( !(
19c2b 66 32 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f f2&(MEM_Int|MEM_
19c2c 52 65 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 Real)) ){.
19c2d 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d return -1;. }
19c2e 0a 20 20 20 20 69 66 28 20 28 66 31 20 26 20 66 . if( (f1 & f
19c2f 32 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 2 & MEM_Int)==0
19c30 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 ){. double
19c31 72 31 2c 20 72 32 3b 0a 20 20 20 20 20 20 69 66 r1, r2;. if
19c32 28 20 28 66 31 26 4d 45 4d 5f 52 65 61 6c 29 3d ( (f1&MEM_Real)=
19c33 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 31 =0 ){. r1
19c34 20 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 31 = (double)pMem1
19c35 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c ->u.i;. }el
19c36 73 65 7b 0a 20 20 20 20 20 20 20 20 72 31 20 3d se{. r1 =
19c37 20 70 4d 65 6d 31 2d 3e 72 3b 0a 20 20 20 20 20 pMem1->r;.
19c38 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28 66 32 }. if( (f2
19c39 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b &MEM_Real)==0 ){
19c3a 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 28 64 . r2 = (d
19c3b 6f 75 62 6c 65 29 70 4d 65 6d 32 2d 3e 75 2e 69 ouble)pMem2->u.i
19c3c 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
19c3d 20 20 20 20 20 20 20 72 32 20 3d 20 70 4d 65 6d r2 = pMem
19c3e 32 2d 3e 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 2->r;. }.
19c3f 20 20 20 20 69 66 28 20 72 31 3c 72 32 20 29 20 if( r1<r2 )
19c40 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 return -1;.
19c41 20 69 66 28 20 72 31 3e 72 32 20 29 20 72 65 74 if( r1>r2 ) ret
19c42 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 urn 1;. ret
19c43 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 urn 0;. }else
19c44 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
19c45 66 31 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 f1&MEM_Int );.
19c46 20 20 20 20 61 73 73 65 72 74 28 20 66 32 26 4d assert( f2&M
19c47 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 EM_Int );.
19c48 69 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3c if( pMem1->u.i <
19c49 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 pMem2->u.i ) re
19c4a 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 69 turn -1;. i
19c4b 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3e 20 f( pMem1->u.i >
19c4c 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74 pMem2->u.i ) ret
19c4d 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 urn 1;. ret
19c4e 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d urn 0;. }. }
19c4f 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 .. /* If one va
19c50 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 lue is a string
19c51 61 6e 64 20 74 68 65 20 6f 74 68 65 72 20 69 73 and the other is
19c52 20 61 20 62 6c 6f 62 2c 20 74 68 65 20 73 74 72 a blob, the str
19c53 69 6e 67 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a ing is less.. *
19c54 2a 20 49 66 20 62 6f 74 68 20 61 72 65 20 73 74 * If both are st
19c55 72 69 6e 67 73 2c 20 63 6f 6d 70 61 72 65 20 75 rings, compare u
19c56 73 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 sing the collati
19c57 6e 67 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 ng functions..
19c58 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 */. if( combine
19c59 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 d_flags&MEM_Str
19c5a 29 7b 0a 20 20 20 20 69 66 28 20 28 66 31 20 26 ){. if( (f1 &
19c5b 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b 0a MEM_Str)==0 ){.
19c5c 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
19c5d 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66 }. if( (f
19c5e 32 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 2 & MEM_Str)==0
19c5f 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
19c60 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 -1;. }.. a
19c61 73 73 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e ssert( pMem1->en
19c62 63 3d 3d 70 4d 65 6d 32 2d 3e 65 6e 63 20 29 3b c==pMem2->enc );
19c63 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 . assert( pMe
19c64 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f m1->enc==SQLITE_
19c65 55 54 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 20 UTF8 || .
19c66 20 20 20 20 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d pMem1->enc=
19c67 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 =SQLITE_UTF16LE
19c68 7c 7c 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 || pMem1->enc==S
19c69 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b QLITE_UTF16BE );
19c6a 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c .. /* The col
19c6b 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
19c6c 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 must be defined
19c6d 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 65 at this point, e
19c6e 76 65 6e 20 69 66 0a 20 20 20 20 2a 2a 20 74 68 ven if. ** th
19c6f 65 20 75 73 65 72 20 64 65 6c 65 74 65 73 20 74 e user deletes t
19c70 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 he collation seq
19c71 75 65 6e 63 65 20 61 66 74 65 72 20 74 68 65 20 uence after the
19c72 76 64 62 65 20 70 72 6f 67 72 61 6d 20 69 73 0a vdbe program is.
19c73 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 ** compiled
19c74 28 74 68 69 73 20 77 61 73 20 6e 6f 74 20 61 6c (this was not al
19c75 77 61 79 73 20 74 68 65 20 63 61 73 65 29 2e 0a ways the case)..
19c76 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 */. asser
19c77 74 28 20 21 70 43 6f 6c 6c 20 7c 7c 20 70 43 6f t( !pColl || pCo
19c78 6c 6c 2d 3e 78 43 6d 70 20 29 3b 0a 0a 20 20 20 ll->xCmp );..
19c79 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 if( pColl ){.
19c7a 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 65 if( pMem1->e
19c7b 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29 nc==pColl->enc )
19c7c 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 {. /* The
19c7d 20 73 74 72 69 6e 67 73 20 61 72 65 20 61 6c 72 strings are alr
19c7e 65 61 64 79 20 69 6e 20 74 68 65 20 63 6f 72 72 eady in the corr
19c7f 65 63 74 20 65 6e 63 6f 64 69 6e 67 2e 20 20 43 ect encoding. C
19c80 61 6c 6c 20 74 68 65 0a 20 20 20 20 20 20 20 20 all the.
19c81 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 ** comparison fu
19c82 6e 63 74 69 6f 6e 20 64 69 72 65 63 74 6c 79 20 nction directly
19c83 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 */. retur
19c84 6e 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 n pColl->xCmp(pC
19c85 6f 6c 6c 2d 3e 70 55 73 65 72 2c 70 4d 65 6d 31 oll->pUser,pMem1
19c86 2d 3e 6e 2c 70 4d 65 6d 31 2d 3e 7a 2c 70 4d 65 ->n,pMem1->z,pMe
19c87 6d 32 2d 3e 6e 2c 70 4d 65 6d 32 2d 3e 7a 29 3b m2->n,pMem2->z);
19c88 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
19c89 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 const void
19c8a 20 2a 76 31 2c 20 2a 76 32 3b 0a 20 20 20 20 20 *v1, *v2;.
19c8b 20 20 20 69 6e 74 20 6e 31 2c 20 6e 32 3b 0a 20 int n1, n2;.
19c8c 20 20 20 20 20 20 20 4d 65 6d 20 63 31 3b 0a 20 Mem c1;.
19c8d 20 20 20 20 20 20 20 4d 65 6d 20 63 32 3b 0a 20 Mem c2;.
19c8e 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 63 memset(&c
19c8f 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63 31 29 1, 0, sizeof(c1)
19c90 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 );. memse
19c91 74 28 26 63 32 2c 20 30 2c 20 73 69 7a 65 6f 66 t(&c2, 0, sizeof
19c92 28 63 32 29 29 3b 0a 20 20 20 20 20 20 20 20 73 (c2));. s
19c93 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 qlite3VdbeMemSha
19c94 6c 6c 6f 77 43 6f 70 79 28 26 63 31 2c 20 70 4d llowCopy(&c1, pM
19c95 65 6d 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b em1, MEM_Ephem);
19c96 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
19c97 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f VdbeMemShallowCo
19c98 70 79 28 26 63 32 2c 20 70 4d 65 6d 32 2c 20 4d py(&c2, pMem2, M
19c99 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20 EM_Ephem);.
19c9a 20 20 20 76 31 20 3d 20 73 71 6c 69 74 65 33 56 v1 = sqlite3V
19c9b 61 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 alueText((sqlite
19c9c 33 5f 76 61 6c 75 65 2a 29 26 63 31 2c 20 70 43 3_value*)&c1, pC
19c9d 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 oll->enc);.
19c9e 20 20 20 6e 31 20 3d 20 76 31 3d 3d 30 20 3f 20 n1 = v1==0 ?
19c9f 30 20 3a 20 63 31 2e 6e 3b 0a 20 20 20 20 20 20 0 : c1.n;.
19ca0 20 20 76 32 20 3d 20 73 71 6c 69 74 65 33 56 61 v2 = sqlite3Va
19ca1 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 lueText((sqlite3
19ca2 5f 76 61 6c 75 65 2a 29 26 63 32 2c 20 70 43 6f _value*)&c2, pCo
19ca3 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 ll->enc);.
19ca4 20 20 6e 32 20 3d 20 76 32 3d 3d 30 20 3f 20 30 n2 = v2==0 ? 0
19ca5 20 3a 20 63 32 2e 6e 3b 0a 20 20 20 20 20 20 20 : c2.n;.
19ca6 20 72 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d rc = pColl->xCm
19ca7 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20 p(pColl->pUser,
19ca8 6e 31 2c 20 76 31 2c 20 6e 32 2c 20 76 32 29 3b n1, v1, n2, v2);
19ca9 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
19caa 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 VdbeMemRelease(&
19cab 63 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c c1);. sql
19cac 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
19cad 73 65 28 26 63 32 29 3b 0a 20 20 20 20 20 20 20 se(&c2);.
19cae 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
19caf 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a }. }. /*
19cb0 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 If a NULL point
19cb1 65 72 20 77 61 73 20 70 61 73 73 65 64 20 61 73 er was passed as
19cb2 20 74 68 65 20 63 6f 6c 6c 61 74 65 20 66 75 6e the collate fun
19cb3 63 74 69 6f 6e 2c 20 66 61 6c 6c 20 74 68 72 6f ction, fall thro
19cb4 75 67 68 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 ugh. ** to th
19cb5 65 20 62 6c 6f 62 20 63 61 73 65 20 61 6e 64 20 e blob case and
19cb6 75 73 65 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a use memcmp(). *
19cb7 2f 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 42 6f 74 /. }. . /* Bot
19cb8 68 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 h values must be
19cb9 20 62 6c 6f 62 73 2e 20 20 43 6f 6d 70 61 72 65 blobs. Compare
19cba 20 75 73 69 6e 67 20 6d 65 6d 63 6d 70 28 29 2e using memcmp().
19cbb 20 20 2a 2f 0a 20 20 72 63 20 3d 20 6d 65 6d 63 */. rc = memc
19cbc 6d 70 28 70 4d 65 6d 31 2d 3e 7a 2c 20 70 4d 65 mp(pMem1->z, pMe
19cbd 6d 32 2d 3e 7a 2c 20 28 70 4d 65 6d 31 2d 3e 6e m2->z, (pMem1->n
19cbe 3e 70 4d 65 6d 32 2d 3e 6e 29 3f 70 4d 65 6d 32 >pMem2->n)?pMem2
19cbf 2d 3e 6e 3a 70 4d 65 6d 31 2d 3e 6e 29 3b 0a 20 ->n:pMem1->n);.
19cc0 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 if( rc==0 ){.
19cc1 20 20 72 63 20 3d 20 70 4d 65 6d 31 2d 3e 6e 20 rc = pMem1->n
19cc2 2d 20 70 4d 65 6d 32 2d 3e 6e 3b 0a 20 20 7d 0a - pMem2->n;. }.
19cc3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
19cc4 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 64 61 74 61 20 /*.** Move data
19cc5 6f 75 74 20 6f 66 20 61 20 62 74 72 65 65 20 6b out of a btree k
19cc6 65 79 20 6f 72 20 64 61 74 61 20 66 69 65 6c 64 ey or data field
19cc7 20 61 6e 64 20 69 6e 74 6f 20 61 20 4d 65 6d 20 and into a Mem
19cc8 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 structure..** Th
19cc9 65 20 64 61 74 61 20 6f 72 20 6b 65 79 20 69 73 e data or key is
19cca 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 taken from the
19ccb 65 6e 74 72 79 20 74 68 61 74 20 70 43 75 72 20 entry that pCur
19ccc 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 is currently poi
19ccd 6e 74 69 6e 67 0a 2a 2a 20 74 6f 2e 20 20 6f 66 nting.** to. of
19cce 66 73 65 74 20 61 6e 64 20 61 6d 74 20 64 65 74 fset and amt det
19ccf 65 72 6d 69 6e 65 20 77 68 61 74 20 70 6f 72 74 ermine what port
19cd0 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 20 ion of the data
19cd1 6f 72 20 6b 65 79 20 74 6f 20 72 65 74 72 69 65 or key to retrie
19cd2 76 65 2e 0a 2a 2a 20 6b 65 79 20 69 73 20 74 72 ve..** key is tr
19cd3 75 65 20 74 6f 20 67 65 74 20 74 68 65 20 6b 65 ue to get the ke
19cd4 79 20 6f 72 20 66 61 6c 73 65 20 74 6f 20 67 65 y or false to ge
19cd5 74 20 64 61 74 61 2e 20 20 54 68 65 20 72 65 73 t data. The res
19cd6 75 6c 74 20 69 73 20 77 72 69 74 74 65 6e 0a 2a ult is written.*
19cd7 2a 20 69 6e 74 6f 20 74 68 65 20 70 4d 65 6d 20 * into the pMem
19cd8 65 6c 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 element..**.** T
19cd9 68 65 20 70 4d 65 6d 20 73 74 72 75 63 74 75 72 he pMem structur
19cda 65 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 e is assumed to
19cdb 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 be uninitialized
19cdc 2e 20 20 41 6e 79 20 70 72 69 6f 72 20 63 6f 6e . Any prior con
19cdd 74 65 6e 74 0a 2a 2a 20 69 73 20 6f 76 65 72 77 tent.** is overw
19cde 72 69 74 74 65 6e 20 77 69 74 68 6f 75 74 20 62 ritten without b
19cdf 65 69 6e 67 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a eing freed..**.*
19ce0 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e * If this routin
19ce1 65 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 e fails for any
19ce2 72 65 61 73 6f 6e 20 28 6d 61 6c 6c 6f 63 20 72 reason (malloc r
19ce3 65 74 75 72 6e 73 20 4e 55 4c 4c 20 6f 72 20 75 eturns NULL or u
19ce4 6e 61 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 61 64 nable.** to read
19ce5 20 66 72 6f 6d 20 74 68 65 20 64 69 73 6b 29 20 from the disk)
19ce6 74 68 65 6e 20 74 68 65 20 70 4d 65 6d 20 69 73 then the pMem is
19ce7 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f left in an inco
19ce8 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a nsistent state..
19ce9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19cea 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
19ceb 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 0a 20 eMemFromBtree(.
19cec 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c BtCursor *pCur,
19ced 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 /* Cursor poi
19cee 6e 74 69 6e 67 20 61 74 20 72 65 63 6f 72 64 20 nting at record
19cef 74 6f 20 72 65 74 72 69 65 76 65 2e 20 2a 2f 0a to retrieve. */.
19cf0 20 20 69 6e 74 20 6f 66 66 73 65 74 2c 20 20 20 int offset,
19cf1 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 /* Offset fr
19cf2 6f 6d 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 om the start of
19cf3 64 61 74 61 20 74 6f 20 72 65 74 75 72 6e 20 62 data to return b
19cf4 79 74 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20 20 ytes from. */.
19cf5 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 int amt,
19cf6 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
19cf7 79 74 65 73 20 74 6f 20 72 65 74 75 72 6e 2e 20 ytes to return.
19cf8 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 2c 20 20 20 */. int key,
19cf9 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 /* If tru
19cfa 65 2c 20 72 65 74 72 69 65 76 65 20 66 72 6f 6d e, retrieve from
19cfb 20 74 68 65 20 62 74 72 65 65 20 6b 65 79 2c 20 the btree key,
19cfc 6e 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20 4d not data. */. M
19cfd 65 6d 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 em *pMem
19cfe 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 /* OUT: Return
19cff 64 61 74 61 20 69 6e 20 74 68 69 73 20 4d 65 6d data in this Mem
19d00 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 29 structure. */.)
19d01 7b 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b {. char *zData;
19d02 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 /* Data
19d03 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20 6c from the btree l
19d04 61 79 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 76 ayer */. int av
19d05 61 69 6c 61 62 6c 65 20 3d 20 30 3b 20 20 2f 2a ailable = 0; /*
19d06 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
19d07 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 available on th
19d08 65 20 6c 6f 63 61 6c 20 62 74 72 65 65 20 70 61 e local btree pa
19d09 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d ge */. int rc =
19d0a 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 2f 2a 20 52 SQLITE_OK; /* R
19d0b 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 eturn code */..
19d0c 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
19d0d 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c BtreeCursorIsVal
19d0e 69 64 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 2f id(pCur) );.. /
19d0f 2a 20 4e 6f 74 65 3a 20 74 68 65 20 63 61 6c 6c * Note: the call
19d10 73 20 74 6f 20 42 74 72 65 65 4b 65 79 46 65 74 s to BtreeKeyFet
19d11 63 68 28 29 20 61 6e 64 20 44 61 74 61 46 65 74 ch() and DataFet
19d12 63 68 28 29 20 62 65 6c 6f 77 20 61 73 73 65 72 ch() below asser
19d13 74 28 29 20 0a 20 20 2a 2a 20 74 68 61 74 20 62 t() . ** that b
19d14 6f 74 68 20 74 68 65 20 42 74 53 68 61 72 65 64 oth the BtShared
19d15 20 61 6e 64 20 64 61 74 61 62 61 73 65 20 68 61 and database ha
19d16 6e 64 6c 65 20 6d 75 74 65 78 65 73 20 61 72 65 ndle mutexes are
19d17 20 68 65 6c 64 2e 20 2a 2f 0a 20 20 61 73 73 65 held. */. asse
19d18 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 rt( (pMem->flags
19d19 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d & MEM_RowSet)==
19d1a 30 20 29 3b 0a 20 20 69 66 28 20 6b 65 79 20 29 0 );. if( key )
19d1b 7b 0a 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63 {. zData = (c
19d1c 68 61 72 20 2a 29 73 71 6c 69 74 65 33 42 74 72 har *)sqlite3Btr
19d1d 65 65 4b 65 79 46 65 74 63 68 28 70 43 75 72 2c eeKeyFetch(pCur,
19d1e 20 26 61 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20 &available);.
19d1f 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 44 61 74 61 }else{. zData
19d20 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 = (char *)sqlit
19d21 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68 e3BtreeDataFetch
19d22 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c (pCur, &availabl
19d23 65 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 e);. }. assert
19d24 28 20 7a 44 61 74 61 21 3d 30 20 29 3b 0a 0a 20 ( zData!=0 );..
19d25 20 69 66 28 20 6f 66 66 73 65 74 2b 61 6d 74 3c if( offset+amt<
19d26 3d 61 76 61 69 6c 61 62 6c 65 20 26 26 20 28 70 =available && (p
19d27 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 Mem->flags&MEM_D
19d28 79 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 yn)==0 ){. sq
19d29 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
19d2a 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 ase(pMem);. p
19d2b 4d 65 6d 2d 3e 7a 20 3d 20 26 7a 44 61 74 61 5b Mem->z = &zData[
19d2c 6f 66 66 73 65 74 5d 3b 0a 20 20 20 20 70 4d 65 offset];. pMe
19d2d 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 m->flags = MEM_B
19d2e 6c 6f 62 7c 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 lob|MEM_Ephem;.
19d2f 20 7d 65 6c 73 65 20 69 66 28 20 53 51 4c 49 54 }else if( SQLIT
19d30 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 E_OK==(rc = sqli
19d31 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 te3VdbeMemGrow(p
19d32 4d 65 6d 2c 20 61 6d 74 2b 32 2c 20 30 29 29 20 Mem, amt+2, 0))
19d33 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 ){. pMem->fla
19d34 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 gs = MEM_Blob|ME
19d35 4d 5f 44 79 6e 7c 4d 45 4d 5f 54 65 72 6d 3b 0a M_Dyn|MEM_Term;.
19d36 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 pMem->enc =
19d37 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 0;. pMem->typ
19d38 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b e = SQLITE_BLOB;
19d39 0a 20 20 20 20 69 66 28 20 6b 65 79 20 29 7b 0a . if( key ){.
19d3a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
19d3b 65 33 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c e3BtreeKey(pCur,
19d3c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 4d offset, amt, pM
19d3d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 em->z);. }els
19d3e 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 e{. rc = sq
19d3f 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28 70 lite3BtreeData(p
19d40 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 Cur, offset, amt
19d41 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 , pMem->z);.
19d42 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d }. pMem->z[am
19d43 74 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d t] = 0;. pMem
19d44 2d 3e 7a 5b 61 6d 74 2b 31 5d 20 3d 20 30 3b 0a ->z[amt+1] = 0;.
19d45 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
19d46 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 TE_OK ){. s
19d47 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
19d48 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 ease(pMem);.
19d49 7d 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 }. }. pMem->n
19d4a 3d 20 61 6d 74 3b 0a 0a 20 20 72 65 74 75 72 6e = amt;.. return
19d4b 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 rc;.}../* This
19d4c 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 function is only
19d4d 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 72 available inter
19d4e 6e 61 6c 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74 nally, it is not
19d4f 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 part of the.**
19d50 65 78 74 65 72 6e 61 6c 20 41 50 49 2e 20 49 74 external API. It
19d51 20 77 6f 72 6b 73 20 69 6e 20 61 20 73 69 6d 69 works in a simi
19d52 6c 61 72 20 77 61 79 20 74 6f 20 73 71 6c 69 74 lar way to sqlit
19d53 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 2c e3_value_text(),
19d54 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 65 20 64 .** except the d
19d55 61 74 61 20 72 65 74 75 72 6e 65 64 20 69 73 20 ata returned is
19d56 69 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 in the encoding
19d57 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 specified by the
19d58 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 61 72 61 6d second.** param
19d59 65 74 65 72 2c 20 77 68 69 63 68 20 6d 75 73 74 eter, which must
19d5a 20 62 65 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 be one of SQLIT
19d5b 45 5f 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54 E_UTF16BE, SQLIT
19d5c 45 5f 55 54 46 31 36 4c 45 20 6f 72 0a 2a 2a 20 E_UTF16LE or.**
19d5d 53 51 4c 49 54 45 5f 55 54 46 38 2e 0a 2a 2a 0a SQLITE_UTF8..**.
19d5e 2a 2a 20 28 32 30 30 36 2d 30 32 2d 31 36 3a 29 ** (2006-02-16:)
19d5f 20 20 54 68 65 20 65 6e 63 20 76 61 6c 75 65 20 The enc value
19d60 63 61 6e 20 62 65 20 6f 72 2d 65 64 20 77 69 74 can be or-ed wit
19d61 68 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 h SQLITE_UTF16_A
19d62 4c 49 47 4e 45 44 2e 0a 2a 2a 20 49 66 20 74 68 LIGNED..** If th
19d63 61 74 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 at is the case,
19d64 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 then the result
19d65 6d 75 73 74 20 62 65 20 61 6c 69 67 6e 65 64 20 must be aligned
19d66 6f 6e 20 61 6e 20 65 76 65 6e 20 62 79 74 65 0a on an even byte.
19d67 2a 2a 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2f 0a ** boundary..*/.
19d68 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
19d69 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
19d6a 65 33 56 61 6c 75 65 54 65 78 74 28 73 71 6c 69 e3ValueText(sqli
19d6b 74 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c 2c te3_value* pVal,
19d6c 20 75 38 20 65 6e 63 29 7b 0a 20 20 69 66 28 20 u8 enc){. if(
19d6d 21 70 56 61 6c 20 29 20 72 65 74 75 72 6e 20 30 !pVal ) return 0
19d6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 56 61 ;.. assert( pVa
19d6f 6c 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 l->db==0 || sqli
19d70 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
19d71 56 61 6c 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 Val->db->mutex)
19d72 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 65 6e );. assert( (en
19d73 63 26 33 29 3d 3d 28 65 6e 63 26 7e 53 51 4c 49 c&3)==(enc&~SQLI
19d74 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 TE_UTF16_ALIGNED
19d75 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 ) );. assert( (
19d76 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pVal->flags & ME
19d77 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a M_RowSet)==0 );.
19d78 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61 . if( pVal->fla
19d79 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 gs&MEM_Null ){.
19d7a 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
19d7b 0a 20 20 61 73 73 65 72 74 28 20 28 4d 45 4d 5f . assert( (MEM_
19d7c 42 6c 6f 62 3e 3e 33 29 20 3d 3d 20 4d 45 4d 5f Blob>>3) == MEM_
19d7d 53 74 72 20 29 3b 0a 20 20 70 56 61 6c 2d 3e 66 Str );. pVal->f
19d7e 6c 61 67 73 20 7c 3d 20 28 70 56 61 6c 2d 3e 66 lags |= (pVal->f
19d7f 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 lags & MEM_Blob)
19d80 3e 3e 33 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f >>3;. expandBlo
19d81 62 28 70 56 61 6c 29 3b 0a 20 20 69 66 28 20 70 b(pVal);. if( p
19d82 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 Val->flags&MEM_S
19d83 74 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 tr ){. sqlite
19d84 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 3VdbeChangeEncod
19d85 69 6e 67 28 70 56 61 6c 2c 20 65 6e 63 20 26 20 ing(pVal, enc &
19d86 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c ~SQLITE_UTF16_AL
19d87 49 47 4e 45 44 29 3b 0a 20 20 20 20 69 66 28 20 IGNED);. if(
19d88 28 65 6e 63 20 26 20 53 51 4c 49 54 45 5f 55 54 (enc & SQLITE_UT
19d89 46 31 36 5f 41 4c 49 47 4e 45 44 29 21 3d 30 20 F16_ALIGNED)!=0
19d8a 26 26 20 31 3d 3d 28 31 26 53 51 4c 49 54 45 5f && 1==(1&SQLITE_
19d8b 50 54 52 5f 54 4f 5f 49 4e 54 28 70 56 61 6c 2d PTR_TO_INT(pVal-
19d8c 3e 7a 29 29 20 29 7b 0a 20 20 20 20 20 20 61 73 >z)) ){. as
19d8d 73 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 sert( (pVal->fla
19d8e 67 73 20 26 20 28 4d 45 4d 5f 45 70 68 65 6d 7c gs & (MEM_Ephem|
19d8f 4d 45 4d 5f 53 74 61 74 69 63 29 29 21 3d 30 20 MEM_Static))!=0
19d90 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c );. if( sql
19d91 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 ite3VdbeMemMakeW
19d92 72 69 74 65 61 62 6c 65 28 70 56 61 6c 29 21 3d riteable(pVal)!=
19d93 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
19d94 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 return 0;.
19d95 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
19d96 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e sqlite3VdbeMemN
19d97 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 56 61 6c ulTerminate(pVal
19d98 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
19d99 61 73 73 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 assert( (pVal->f
19d9a 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 29 3d 3d lags&MEM_Blob)==
19d9b 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 );. sqlite3
19d9c 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 VdbeMemStringify
19d9d 28 70 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 20 (pVal, enc);.
19d9e 20 61 73 73 65 72 74 28 20 30 3d 3d 28 31 26 53 assert( 0==(1&S
19d9f 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 QLITE_PTR_TO_INT
19da0 28 70 56 61 6c 2d 3e 7a 29 29 20 29 3b 0a 20 20 (pVal->z)) );.
19da1 7d 0a 20 20 61 73 73 65 72 74 28 70 56 61 6c 2d }. assert(pVal-
19da2 3e 65 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51 >enc==(enc & ~SQ
19da3 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e LITE_UTF16_ALIGN
19da4 45 44 29 20 7c 7c 20 70 56 61 6c 2d 3e 64 62 3d ED) || pVal->db=
19da5 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 =0.
19da6 20 7c 7c 20 70 56 61 6c 2d 3e 64 62 2d 3e 6d 61 || pVal->db->ma
19da7 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
19da8 69 66 28 20 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 if( pVal->enc==(
19da9 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 enc & ~SQLITE_UT
19daa 46 31 36 5f 41 4c 49 47 4e 45 44 29 20 29 7b 0a F16_ALIGNED) ){.
19dab 20 20 20 20 72 65 74 75 72 6e 20 70 56 61 6c 2d return pVal-
19dac 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 >z;. }else{.
19dad 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d return 0;. }.}
19dae 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
19daf 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 76 61 6c new sqlite3_val
19db0 75 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 ue object..*/.SQ
19db1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
19db2 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 ite3_value *sqli
19db3 74 65 33 56 61 6c 75 65 4e 65 77 28 73 71 6c 69 te3ValueNew(sqli
19db4 74 65 33 20 2a 64 62 29 7b 0a 20 20 4d 65 6d 20 te3 *db){. Mem
19db5 2a 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 *p = sqlite3DbMa
19db6 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a llocZero(db, siz
19db7 65 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 eof(*p));. if(
19db8 70 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 p ){. p->flag
19db9 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
19dba 20 20 70 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 p->type = SQLI
19dbb 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20 20 70 2d 3e TE_NULL;. p->
19dbc 64 62 20 3d 20 64 62 3b 0a 20 20 7d 0a 20 20 72 db = db;. }. r
19dbd 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
19dbe 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 * Create a new s
19dbf 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a qlite3_value obj
19dc0 65 63 74 2c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ect, containing
19dc1 74 68 65 20 76 61 6c 75 65 20 6f 66 20 70 45 78 the value of pEx
19dc2 70 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f pr..**.** This o
19dc3 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 76 65 nly works for ve
19dc4 72 79 20 73 69 6d 70 6c 65 20 65 78 70 72 65 73 ry simple expres
19dc5 73 69 6f 6e 73 20 74 68 61 74 20 63 6f 6e 73 69 sions that consi
19dc6 73 74 20 6f 66 20 6f 6e 65 20 63 6f 6e 73 74 61 st of one consta
19dc7 6e 74 0a 2a 2a 20 74 6f 6b 65 6e 20 28 69 2e 65 nt.** token (i.e
19dc8 2e 20 22 35 22 2c 20 22 35 2e 31 22 2c 20 22 27 . "5", "5.1", "'
19dc9 61 20 73 74 72 69 6e 67 27 22 29 2e 20 49 66 20 a string'"). If
19dca 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 63 the expression c
19dcb 61 6e 0a 2a 2a 20 62 65 20 63 6f 6e 76 65 72 74 an.** be convert
19dcc 65 64 20 64 69 72 65 63 74 6c 79 20 69 6e 74 6f ed directly into
19dcd 20 61 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 a value, then t
19dce 68 65 20 76 61 6c 75 65 20 69 73 20 61 6c 6c 6f he value is allo
19dcf 63 61 74 65 64 20 61 6e 64 0a 2a 2a 20 61 20 70 cated and.** a p
19dd0 6f 69 6e 74 65 72 20 77 72 69 74 74 65 6e 20 74 ointer written t
19dd1 6f 20 2a 70 70 56 61 6c 2e 20 54 68 65 20 63 61 o *ppVal. The ca
19dd2 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 ller is responsi
19dd3 62 6c 65 20 66 6f 72 20 64 65 61 6c 6c 6f 63 61 ble for dealloca
19dd4 74 69 6e 67 0a 2a 2a 20 74 68 65 20 76 61 6c 75 ting.** the valu
19dd5 65 20 62 79 20 70 61 73 73 69 6e 67 20 69 74 20 e by passing it
19dd6 74 6f 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 to sqlite3ValueF
19dd7 72 65 65 28 29 20 6c 61 74 65 72 20 6f 6e 2e 20 ree() later on.
19dd8 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f If the expressio
19dd9 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 63 n.** cannot be c
19dda 6f 6e 76 65 72 74 65 64 20 74 6f 20 61 20 76 61 onverted to a va
19ddb 6c 75 65 2c 20 74 68 65 6e 20 2a 70 70 56 61 6c lue, then *ppVal
19ddc 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e is set to NULL.
19ddd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19dde 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 TE int sqlite3Va
19ddf 6c 75 65 46 72 6f 6d 45 78 70 72 28 0a 20 20 73 lueFromExpr(. s
19de0 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 qlite3 *db,
19de1 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
19de2 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
19de3 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ion */. Expr *p
19de4 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 Expr,
19de5 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 /* The expres
19de6 73 69 6f 6e 20 74 6f 20 65 76 61 6c 75 61 74 65 sion to evaluate
19de7 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 */. u8 enc,
19de8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19de9 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75 /* Encoding to u
19dea 73 65 20 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e se */. u8 affin
19deb 69 74 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 ity,
19dec 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 74 6f /* Affinity to
19ded 20 75 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 use */. sqlite
19dee 33 5f 76 61 6c 75 65 20 2a 2a 70 70 56 61 6c 20 3_value **ppVal
19def 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 /* Write the
19df0 20 6e 65 77 20 76 61 6c 75 65 20 68 65 72 65 20 new value here
19df1 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6f 70 3b 0a */.){. int op;.
19df2 20 20 63 68 61 72 20 2a 7a 56 61 6c 20 3d 20 30 char *zVal = 0
19df3 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ;. sqlite3_valu
19df4 65 20 2a 70 56 61 6c 20 3d 20 30 3b 0a 0a 20 20 e *pVal = 0;..
19df5 69 66 28 20 21 70 45 78 70 72 20 29 7b 0a 20 20 if( !pExpr ){.
19df6 20 20 2a 70 70 56 61 6c 20 3d 20 30 3b 0a 20 20 *ppVal = 0;.
19df7 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
19df8 4f 4b 3b 0a 20 20 7d 0a 20 20 6f 70 20 3d 20 70 OK;. }. op = p
19df9 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 69 66 28 20 Expr->op;. if(
19dfa 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 op==TK_REGISTER
19dfb 29 7b 0a 20 20 20 20 6f 70 20 3d 20 70 45 78 70 ){. op = pExp
19dfc 72 2d 3e 6f 70 32 3b 0a 20 20 7d 0a 0a 20 20 69 r->op2;. }.. i
19dfd 66 28 20 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 f( op==TK_STRING
19dfe 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 || op==TK_FLOAT
19dff 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 || op==TK_INTEG
19e00 45 52 20 29 7b 0a 20 20 20 20 70 56 61 6c 20 3d ER ){. pVal =
19e01 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 sqlite3ValueNew
19e02 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 70 56 (db);. if( pV
19e03 61 6c 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f al==0 ) goto no_
19e04 6d 65 6d 3b 0a 20 20 20 20 69 66 28 20 45 78 70 mem;. if( Exp
19e05 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 rHasProperty(pEx
19e06 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 pr, EP_IntValue)
19e07 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
19e08 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 3VdbeMemSetInt64
19e09 28 70 56 61 6c 2c 20 28 69 36 34 29 70 45 78 70 (pVal, (i64)pExp
19e0a 72 2d 3e 75 2e 69 56 61 6c 75 65 29 3b 0a 20 20 r->u.iValue);.
19e0b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a }else{. z
19e0c 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 53 Val = sqlite3DbS
19e0d 74 72 44 75 70 28 64 62 2c 20 70 45 78 70 72 2d trDup(db, pExpr-
19e0e 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 >u.zToken);.
19e0f 20 20 69 66 28 20 7a 56 61 6c 3d 3d 30 20 29 20 if( zVal==0 )
19e10 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 goto no_mem;.
19e11 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 sqlite3ValueS
19e12 65 74 53 74 72 28 70 56 61 6c 2c 20 2d 31 2c 20 etStr(pVal, -1,
19e13 7a 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 zVal, SQLITE_UTF
19e14 38 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 8, SQLITE_DYNAMI
19e15 43 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 C);. if( op
19e16 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 70 56 61 ==TK_FLOAT ) pVa
19e17 6c 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 l->type = SQLITE
19e18 5f 46 4c 4f 41 54 3b 0a 20 20 20 20 7d 0a 20 20 _FLOAT;. }.
19e19 20 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 49 4e if( (op==TK_IN
19e1a 54 45 47 45 52 20 7c 7c 20 6f 70 3d 3d 54 4b 5f TEGER || op==TK_
19e1b 46 4c 4f 41 54 20 29 20 26 26 20 61 66 66 69 6e FLOAT ) && affin
19e1c 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f ity==SQLITE_AFF_
19e1d 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 73 71 NONE ){. sq
19e1e 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 lite3ValueApplyA
19e1f 66 66 69 6e 69 74 79 28 70 56 61 6c 2c 20 53 51 ffinity(pVal, SQ
19e20 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 LITE_AFF_NUMERIC
19e21 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a , SQLITE_UTF8);.
19e22 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
19e23 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70 sqlite3ValueApp
19e24 6c 79 41 66 66 69 6e 69 74 79 28 70 56 61 6c 2c lyAffinity(pVal,
19e25 20 61 66 66 69 6e 69 74 79 2c 20 53 51 4c 49 54 affinity, SQLIT
19e26 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 7d 0a 20 E_UTF8);. }.
19e27 20 20 20 69 66 28 20 65 6e 63 21 3d 53 51 4c 49 if( enc!=SQLI
19e28 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 20 TE_UTF8 ){.
19e29 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
19e2a 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61 6c 2c geEncoding(pVal,
19e2b 20 65 6e 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d enc);. }. }
19e2c 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f else if( op==TK_
19e2d 55 4d 49 4e 55 53 20 29 20 7b 0a 20 20 20 20 69 UMINUS ) {. i
19e2e 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 f( SQLITE_OK==sq
19e2f 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 lite3ValueFromEx
19e30 70 72 28 64 62 2c 70 45 78 70 72 2d 3e 70 4c 65 pr(db,pExpr->pLe
19e31 66 74 2c 65 6e 63 2c 61 66 66 69 6e 69 74 79 2c ft,enc,affinity,
19e32 26 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 20 20 &pVal) ){.
19e33 70 56 61 6c 2d 3e 75 2e 69 20 3d 20 2d 31 20 2a pVal->u.i = -1 *
19e34 20 70 56 61 6c 2d 3e 75 2e 69 3b 0a 20 20 20 20 pVal->u.i;.
19e35 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 2d 31 20 /* (double)-1
19e36 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 In case of SQLIT
19e37 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
19e38 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 POINT... */.
19e39 20 20 70 56 61 6c 2d 3e 72 20 3d 20 28 64 6f 75 pVal->r = (dou
19e3a 62 6c 65 29 2d 31 20 2a 20 70 56 61 6c 2d 3e 72 ble)-1 * pVal->r
19e3b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 6e ;. }. }.#ifn
19e3c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
19e3d 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20 20 65 BLOB_LITERAL. e
19e3e 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 42 lse if( op==TK_B
19e3f 4c 4f 42 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e LOB ){. int n
19e40 56 61 6c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 Val;. assert(
19e41 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e pExpr->u.zToken
19e42 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 70 45 78 70 [0]=='x' || pExp
19e43 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d r->u.zToken[0]==
19e44 27 58 27 20 29 3b 0a 20 20 20 20 61 73 73 65 72 'X' );. asser
19e45 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b t( pExpr->u.zTok
19e46 65 6e 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 en[1]=='\'' );.
19e47 20 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 pVal = sqlite
19e48 33 56 61 6c 75 65 4e 65 77 28 64 62 29 3b 0a 20 3ValueNew(db);.
19e49 20 20 20 69 66 28 20 21 70 56 61 6c 20 29 20 67 if( !pVal ) g
19e4a 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 oto no_mem;.
19e4b 7a 56 61 6c 20 3d 20 26 70 45 78 70 72 2d 3e 75 zVal = &pExpr->u
19e4c 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b 0a 20 20 20 20 .zToken[2];.
19e4d 6e 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 53 74 nVal = sqlite3St
19e4e 72 6c 65 6e 33 30 28 7a 56 61 6c 29 2d 31 3b 0a rlen30(zVal)-1;.
19e4f 20 20 20 20 61 73 73 65 72 74 28 20 7a 56 61 6c assert( zVal
19e50 5b 6e 56 61 6c 5d 3d 3d 27 5c 27 27 20 29 3b 0a [nVal]=='\'' );.
19e51 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
19e52 65 6d 53 65 74 53 74 72 28 70 56 61 6c 2c 20 73 emSetStr(pVal, s
19e53 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 qlite3HexToBlob(
19e54 64 62 2c 20 7a 56 61 6c 2c 20 6e 56 61 6c 29 2c db, zVal, nVal),
19e55 20 6e 56 61 6c 2f 32 2c 0a 20 20 20 20 20 20 20 nVal/2,.
19e56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19e57 20 20 30 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 0, SQLITE_DYNA
19e58 4d 49 43 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 MIC);. }.#endif
19e59 0a 0a 20 20 69 66 28 20 70 56 61 6c 20 29 7b 0a .. if( pVal ){.
19e5a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
19e5b 65 6d 53 74 6f 72 65 54 79 70 65 28 70 56 61 6c emStoreType(pVal
19e5c 29 3b 0a 20 20 7d 0a 20 20 2a 70 70 56 61 6c 20 );. }. *ppVal
19e5d 3d 20 70 56 61 6c 3b 0a 20 20 72 65 74 75 72 6e = pVal;. return
19e5e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 6e 6f 5f SQLITE_OK;..no_
19e5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f mem:. db->mallo
19e60 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 73 cFailed = 1;. s
19e61 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
19e62 20 7a 56 61 6c 29 3b 0a 20 20 73 71 6c 69 74 65 zVal);. sqlite
19e63 33 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3ValueFree(pVal)
19e64 3b 0a 20 20 2a 70 70 56 61 6c 20 3d 20 30 3b 0a ;. *ppVal = 0;.
19e65 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
19e66 4e 4f 4d 45 4d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 NOMEM;.}../*.**
19e67 43 68 61 6e 67 65 20 74 68 65 20 73 74 72 69 6e Change the strin
19e68 67 20 76 61 6c 75 65 20 6f 66 20 61 6e 20 73 71 g value of an sq
19e69 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 lite3_value obje
19e6a 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 ct.*/.SQLITE_PRI
19e6b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
19e6c 33 56 61 6c 75 65 53 65 74 53 74 72 28 0a 20 20 3ValueSetStr(.
19e6d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 76 sqlite3_value *v
19e6e 2c 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 , /* Value t
19e6f 6f 20 62 65 20 73 65 74 20 2a 2f 0a 20 20 69 6e o be set */. in
19e70 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 t n,
19e71 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 /* Length of
19e72 20 73 74 72 69 6e 67 20 7a 20 2a 2f 0a 20 20 63 string z */. c
19e73 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 20 20 onst void *z,
19e74 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 /* Text of
19e75 74 68 65 20 6e 65 77 20 73 74 72 69 6e 67 20 2a the new string *
19e76 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 /. u8 enc,
19e77 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 /* Enc
19e78 6f 64 69 6e 67 20 74 6f 20 75 73 65 20 2a 2f 0a oding to use */.
19e79 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 void (*xDel)(v
19e7a 6f 69 64 2a 29 20 20 20 2f 2a 20 44 65 73 74 72 oid*) /* Destr
19e7b 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 73 74 uctor for the st
19e7c 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 ring */.){. if(
19e7d 20 76 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 v ) sqlite3Vdbe
19e7e 4d 65 6d 53 65 74 53 74 72 28 28 4d 65 6d 20 2a MemSetStr((Mem *
19e7f 29 76 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 )v, z, n, enc, x
19e80 44 65 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 Del);.}../*.** F
19e81 72 65 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 ree an sqlite3_v
19e82 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 alue object.*/.S
19e83 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
19e84 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 id sqlite3ValueF
19e85 72 65 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 ree(sqlite3_valu
19e86 65 20 2a 76 29 7b 0a 20 20 69 66 28 20 21 76 20 e *v){. if( !v
19e87 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 ) return;. sqli
19e88 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
19e89 65 28 28 4d 65 6d 20 2a 29 76 29 3b 0a 20 20 73 e((Mem *)v);. s
19e8a 71 6c 69 74 65 33 44 62 46 72 65 65 28 28 28 4d qlite3DbFree(((M
19e8b 65 6d 2a 29 76 29 2d 3e 64 62 2c 20 76 29 3b 0a em*)v)->db, v);.
19e8c 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
19e8d 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
19e8e 74 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 tes in the sqlit
19e8f 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 e3_value object
19e90 61 73 73 75 6d 69 6e 67 0a 2a 2a 20 74 68 61 74 assuming.** that
19e91 20 69 74 20 75 73 65 73 20 74 68 65 20 65 6e 63 it uses the enc
19e92 6f 64 69 6e 67 20 22 65 6e 63 22 0a 2a 2f 0a 53 oding "enc".*/.S
19e93 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
19e94 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 t sqlite3ValueBy
19e95 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 tes(sqlite3_valu
19e96 65 20 2a 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 e *pVal, u8 enc)
19e97 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d 65 {. Mem *p = (Me
19e98 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 20 28 m*)pVal;. if( (
19e99 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 p->flags & MEM_B
19e9a 6c 6f 62 29 21 3d 30 20 7c 7c 20 73 71 6c 69 74 lob)!=0 || sqlit
19e9b 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c e3ValueText(pVal
19e9c 2c 20 65 6e 63 29 20 29 7b 0a 20 20 20 20 69 66 , enc) ){. if
19e9d 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d ( p->flags & MEM
19e9e 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 72 _Zero ){. r
19e9f 65 74 75 72 6e 20 70 2d 3e 6e 20 2b 20 70 2d 3e eturn p->n + p->
19ea0 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 65 6c u.nZero;. }el
19ea1 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e se{. return
19ea2 20 70 2d 3e 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d p->n;. }. }
19ea3 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
19ea4 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
19ea5 45 6e 64 20 6f 66 20 76 64 62 65 6d 65 6d 2e 63 End of vdbemem.c
19ea6 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
19ea7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19ea8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
19ea9 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
19eaa 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 61 Begin file vdbea
19eab 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ux.c ***********
19eac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19ead 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
19eae 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 65 /*.** 2003 Septe
19eaf 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 mber 6.**.** The
19eb0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
19eb1 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
19eb2 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
19eb3 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
19eb4 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
19eb5 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
19eb6 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
19eb7 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
19eb8 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
19eb9 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
19eba 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
19ebb 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
19ebc 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
19ebd 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
19ebe 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
19ebf 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
19ec0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
19ec1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19ec2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19ec3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19ec4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19ec5 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
19ec6 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 le contains code
19ec7 20 75 73 65 64 20 66 6f 72 20 63 72 65 61 74 69 used for creati
19ec8 6e 67 2c 20 64 65 73 74 72 6f 79 69 6e 67 2c 20 ng, destroying,
19ec9 61 6e 64 20 70 6f 70 75 6c 61 74 69 6e 67 0a 2a and populating.*
19eca 2a 20 61 20 56 44 42 45 20 28 6f 72 20 61 6e 20 * a VDBE (or an
19ecb 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 22 20 61 "sqlite3_stmt" a
19ecc 73 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20 74 6f s it is known to
19ecd 20 74 68 65 20 6f 75 74 73 69 64 65 20 77 6f 72 the outside wor
19ece 6c 64 2e 29 20 20 50 72 69 6f 72 0a 2a 2a 20 74 ld.) Prior.** t
19ecf 6f 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 37 2c o version 2.8.7,
19ed0 20 61 6c 6c 20 74 68 69 73 20 63 6f 64 65 20 77 all this code w
19ed1 61 73 20 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f as combined into
19ed2 20 74 68 65 20 76 64 62 65 2e 63 20 73 6f 75 72 the vdbe.c sour
19ed3 63 65 20 66 69 6c 65 2e 0a 2a 2a 20 42 75 74 20 ce file..** But
19ed4 74 68 61 74 20 66 69 6c 65 20 77 61 73 20 67 65 that file was ge
19ed5 74 74 69 6e 67 20 74 6f 6f 20 62 69 67 20 73 6f tting too big so
19ed6 20 74 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65 this subroutine
19ed7 73 20 77 65 72 65 20 73 70 6c 69 74 20 6f 75 74 s were split out
19ed8 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 ..**.** $Id: vdb
19ed9 65 61 75 78 2e 63 2c 76 20 31 2e 34 38 30 20 32 eaux.c,v 1.480 2
19eda 30 30 39 2f 30 38 2f 30 38 20 31 38 3a 30 31 3a 009/08/08 18:01:
19edb 30 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 08 drh Exp $.*/.
19edc 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 64 65 .../*.** When de
19edd 62 75 67 67 69 6e 67 20 74 68 65 20 63 6f 64 65 bugging the code
19ede 20 67 65 6e 65 72 61 74 6f 72 20 69 6e 20 61 20 generator in a
19edf 73 79 6d 62 6f 6c 69 63 20 64 65 62 75 67 67 65 symbolic debugge
19ee0 72 2c 20 6f 6e 65 20 63 61 6e 0a 2a 2a 20 73 65 r, one can.** se
19ee1 74 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 t the sqlite3Vdb
19ee2 65 41 64 64 6f 70 54 72 61 63 65 20 74 6f 20 31 eAddopTrace to 1
19ee3 20 61 6e 64 20 61 6c 6c 20 6f 70 63 6f 64 65 73 and all opcodes
19ee4 20 77 69 6c 6c 20 62 65 20 70 72 69 6e 74 65 64 will be printed
19ee5 0a 2a 2a 20 61 73 20 74 68 65 79 20 61 72 65 20 .** as they are
19ee6 61 64 64 65 64 20 74 6f 20 74 68 65 20 69 6e 73 added to the ins
19ee7 74 72 75 63 74 69 6f 6e 20 73 74 72 65 61 6d 2e truction stream.
19ee8 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
19ee9 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 E_DEBUG.SQLITE_P
19eea 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19eeb 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 e3VdbeAddopTrace
19eec 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f = 0;.#endif.../
19eed 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 *.** Create a ne
19eee 77 20 76 69 72 74 75 61 6c 20 64 61 74 61 62 61 w virtual databa
19eef 73 65 20 65 6e 67 69 6e 65 2e 0a 2a 2f 0a 53 51 se engine..*/.SQ
19ef0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 LITE_PRIVATE Vdb
19ef1 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 e *sqlite3VdbeCr
19ef2 65 61 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 eate(sqlite3 *db
19ef3 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 ){. Vdbe *p;.
19ef4 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c p = sqlite3DbMal
19ef5 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 locZero(db, size
19ef6 6f 66 28 56 64 62 65 29 20 29 3b 0a 20 20 69 66 of(Vdbe) );. if
19ef7 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( p==0 ) return
19ef8 30 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0;. p->db = db;
19ef9 0a 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62 65 . if( db->pVdbe
19efa 20 29 7b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 ){. db->pVdb
19efb 65 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 e->pPrev = p;.
19efc 7d 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 64 }. p->pNext = d
19efd 62 2d 3e 70 56 64 62 65 3b 0a 20 20 70 2d 3e 70 b->pVdbe;. p->p
19efe 50 72 65 76 20 3d 20 30 3b 0a 20 20 64 62 2d 3e Prev = 0;. db->
19eff 70 56 64 62 65 20 3d 20 70 3b 0a 20 20 70 2d 3e pVdbe = p;. p->
19f00 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 magic = VDBE_MAG
19f01 49 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 IC_INIT;. retur
19f02 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 n p;.}../*.** Re
19f03 6d 65 6d 62 65 72 20 74 68 65 20 53 51 4c 20 73 member the SQL s
19f04 74 72 69 6e 67 20 66 6f 72 20 61 20 70 72 65 70 tring for a prep
19f05 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e 0a ared statement..
19f06 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19f07 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
19f08 62 65 53 65 74 53 71 6c 28 56 64 62 65 20 2a 70 beSetSql(Vdbe *p
19f09 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c , const char *z,
19f0a 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 73 50 72 int n, int isPr
19f0b 65 70 61 72 65 56 32 29 7b 0a 20 20 61 73 73 65 epareV2){. asse
19f0c 72 74 28 20 69 73 50 72 65 70 61 72 65 56 32 3d rt( isPrepareV2=
19f0d 3d 31 20 7c 7c 20 69 73 50 72 65 70 61 72 65 56 =1 || isPrepareV
19f0e 32 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 3d 2==0 );. if( p=
19f0f 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 =0 ) return;.#if
19f10 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
19f11 54 52 41 43 45 0a 20 20 69 66 28 20 21 69 73 50 TRACE. if( !isP
19f12 72 65 70 61 72 65 56 32 20 29 20 72 65 74 75 72 repareV2 ) retur
19f13 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65 n;.#endif. asse
19f14 72 74 28 20 70 2d 3e 7a 53 71 6c 3d 3d 30 20 29 rt( p->zSql==0 )
19f15 3b 0a 20 20 70 2d 3e 7a 53 71 6c 20 3d 20 73 71 ;. p->zSql = sq
19f16 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 70 lite3DbStrNDup(p
19f17 2d 3e 64 62 2c 20 7a 2c 20 6e 29 3b 0a 20 20 70 ->db, z, n);. p
19f18 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 3d 20 ->isPrepareV2 =
19f19 69 73 50 72 65 70 61 72 65 56 32 3b 0a 7d 0a 0a isPrepareV2;.}..
19f1a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
19f1b 20 53 51 4c 20 61 73 73 6f 63 69 61 74 65 64 20 SQL associated
19f1c 77 69 74 68 20 61 20 70 72 65 70 61 72 65 64 20 with a prepared
19f1d 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c statement.*/.SQL
19f1e 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 ITE_API const ch
19f1f 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 ar *sqlite3_sql(
19f20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
19f21 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 tmt){. Vdbe *p
19f22 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b = (Vdbe *)pStmt;
19f23 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 69 73 . return (p->is
19f24 50 72 65 70 61 72 65 56 32 20 3f 20 70 2d 3e 7a PrepareV2 ? p->z
19f25 53 71 6c 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a Sql : 0);.}../*.
19f26 2a 2a 20 53 77 61 70 20 61 6c 6c 20 63 6f 6e 74 ** Swap all cont
19f27 65 6e 74 20 62 65 74 77 65 65 6e 20 74 77 6f 20 ent between two
19f28 56 44 42 45 20 73 74 72 75 63 74 75 72 65 73 2e VDBE structures.
19f29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19f2a 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
19f2b 64 62 65 53 77 61 70 28 56 64 62 65 20 2a 70 41 dbeSwap(Vdbe *pA
19f2c 2c 20 56 64 62 65 20 2a 70 42 29 7b 0a 20 20 56 , Vdbe *pB){. V
19f2d 64 62 65 20 74 6d 70 2c 20 2a 70 54 6d 70 3b 0a dbe tmp, *pTmp;.
19f2e 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b 0a 20 20 char *zTmp;.
19f2f 74 6d 70 20 3d 20 2a 70 41 3b 0a 20 20 2a 70 41 tmp = *pA;. *pA
19f30 20 3d 20 2a 70 42 3b 0a 20 20 2a 70 42 20 3d 20 = *pB;. *pB =
19f31 74 6d 70 3b 0a 20 20 70 54 6d 70 20 3d 20 70 41 tmp;. pTmp = pA
19f32 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 41 2d 3e 70 ->pNext;. pA->p
19f33 4e 65 78 74 20 3d 20 70 42 2d 3e 70 4e 65 78 74 Next = pB->pNext
19f34 3b 0a 20 20 70 42 2d 3e 70 4e 65 78 74 20 3d 20 ;. pB->pNext =
19f35 70 54 6d 70 3b 0a 20 20 70 54 6d 70 20 3d 20 70 pTmp;. pTmp = p
19f36 41 2d 3e 70 50 72 65 76 3b 0a 20 20 70 41 2d 3e A->pPrev;. pA->
19f37 70 50 72 65 76 20 3d 20 70 42 2d 3e 70 50 72 65 pPrev = pB->pPre
19f38 76 3b 0a 20 20 70 42 2d 3e 70 50 72 65 76 20 3d v;. pB->pPrev =
19f39 20 70 54 6d 70 3b 0a 20 20 7a 54 6d 70 20 3d 20 pTmp;. zTmp =
19f3a 70 41 2d 3e 7a 53 71 6c 3b 0a 20 20 70 41 2d 3e pA->zSql;. pA->
19f3b 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53 71 6c 3b zSql = pB->zSql;
19f3c 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d 20 7a 54 . pB->zSql = zT
19f3d 6d 70 3b 0a 20 20 70 42 2d 3e 69 73 50 72 65 70 mp;. pB->isPrep
19f3e 61 72 65 56 32 20 3d 20 70 41 2d 3e 69 73 50 72 areV2 = pA->isPr
19f3f 65 70 61 72 65 56 32 3b 0a 7d 0a 0a 23 69 66 64 epareV2;.}..#ifd
19f40 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
19f41 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72 61 63 69 /*.** Turn traci
19f42 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a 2a 2f 0a ng on or off.*/.
19f43 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
19f44 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 54 oid sqlite3VdbeT
19f45 72 61 63 65 28 56 64 62 65 20 2a 70 2c 20 46 49 race(Vdbe *p, FI
19f46 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20 20 70 2d LE *trace){. p-
19f47 3e 74 72 61 63 65 20 3d 20 74 72 61 63 65 3b 0a >trace = trace;.
19f48 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
19f49 52 65 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e Resize the Vdbe.
19f4a 61 4f 70 20 61 72 72 61 79 20 73 6f 20 74 68 61 aOp array so tha
19f4b 74 20 69 74 20 69 73 20 61 74 20 6c 65 61 73 74 t it is at least
19f4c 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65 72 20 74 one op larger t
19f4d 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61 73 2e 0a han .** it was..
19f4e 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f 75 74 2d **.** If an out-
19f4f 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72 20 of-memory error
19f50 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 73 occurs while res
19f51 69 7a 69 6e 67 20 74 68 65 20 61 72 72 61 79 2c izing the array,
19f52 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 return.** SQLIT
19f53 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74 68 69 73 E_NOMEM. In this
19f54 20 63 61 73 65 20 56 64 62 65 2e 61 4f 70 20 61 case Vdbe.aOp a
19f55 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c 6c 6f 63 nd Vdbe.nOpAlloc
19f56 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75 6e 63 68 remain .** unch
19f57 61 6e 67 65 64 20 28 74 68 69 73 20 69 73 20 73 anged (this is s
19f58 6f 20 74 68 61 74 20 61 6e 79 20 6f 70 63 6f 64 o that any opcod
19f59 65 73 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 es already alloc
19f5a 61 74 65 64 20 63 61 6e 20 62 65 20 0a 2a 2a 20 ated can be .**
19f5b 63 6f 72 72 65 63 74 6c 79 20 64 65 61 6c 6c 6f correctly deallo
19f5c 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 cated along with
19f5d 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 the rest of the
19f5e 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 Vdbe)..*/.stati
19f5f 63 20 69 6e 74 20 67 72 6f 77 4f 70 41 72 72 61 c int growOpArra
19f60 79 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 56 64 y(Vdbe *p){. Vd
19f61 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20 20 69 6e beOp *pNew;. in
19f62 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e 6e 4f 70 t nNew = (p->nOp
19f63 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f 70 41 6c Alloc ? p->nOpAl
19f64 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29 28 31 30 loc*2 : (int)(10
19f65 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29 29 29 3b 24/sizeof(Op)));
19f66 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 . pNew = sqlite
19f67 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d 3e 64 62 3DbRealloc(p->db
19f68 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65 77 2a 73 , p->aOp, nNew*s
19f69 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20 20 69 66 izeof(Op));. if
19f6a 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 70 2d ( pNew ){. p-
19f6b 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 >nOpAlloc = sqli
19f6c 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 te3DbMallocSize(
19f6d 70 2d 3e 64 62 2c 20 70 4e 65 77 29 2f 73 69 7a p->db, pNew)/siz
19f6e 65 6f 66 28 4f 70 29 3b 0a 20 20 20 20 70 2d 3e eof(Op);. p->
19f6f 61 4f 70 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a aOp = pNew;. }.
19f70 20 20 72 65 74 75 72 6e 20 28 70 4e 65 77 20 3f return (pNew ?
19f71 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c SQLITE_OK : SQL
19f72 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f ITE_NOMEM);.}../
19f73 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 69 *.** Add a new i
19f74 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 74 68 nstruction to th
19f75 65 20 6c 69 73 74 20 6f 66 20 69 6e 73 74 72 75 e list of instru
19f76 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 20 69 ctions current i
19f77 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e 20 20 n the.** VDBE.
19f78 52 65 74 75 72 6e 20 74 68 65 20 61 64 64 72 65 Return the addre
19f79 73 73 20 6f 66 20 74 68 65 20 6e 65 77 20 69 6e ss of the new in
19f7a 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a struction..**.**
19f7b 20 50 61 72 61 6d 65 74 65 72 73 3a 0a 2a 2a 0a Parameters:.**.
19f7c 2a 2a 20 20 20 20 70 20 20 20 20 20 20 20 20 20 ** p
19f7d 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20 74 6f Pointer to
19f7e 20 74 68 65 20 56 44 42 45 0a 2a 2a 0a 2a 2a 20 the VDBE.**.**
19f7f 20 20 20 6f 70 20 20 20 20 20 20 20 20 20 20 20 op
19f80 20 20 20 54 68 65 20 6f 70 63 6f 64 65 20 66 6f The opcode fo
19f81 72 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 r this instructi
19f82 6f 6e 0a 2a 2a 0a 2a 2a 20 20 20 20 70 31 2c 20 on.**.** p1,
19f83 70 32 2c 20 70 33 20 20 20 20 20 20 4f 70 65 72 p2, p3 Oper
19f84 61 6e 64 73 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 ands.**.** Use t
19f85 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 he sqlite3VdbeRe
19f86 73 6f 6c 76 65 4c 61 62 65 6c 28 29 20 66 75 6e solveLabel() fun
19f87 63 74 69 6f 6e 20 74 6f 20 66 69 78 20 61 6e 20 ction to fix an
19f88 61 64 64 72 65 73 73 20 61 6e 64 0a 2a 2a 20 74 address and.** t
19f89 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 he sqlite3VdbeCh
19f8a 61 6e 67 65 50 34 28 29 20 66 75 6e 63 74 69 6f angeP4() functio
19f8b 6e 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 n to change the
19f8c 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 34 0a value of the P4.
19f8d 2a 2a 20 6f 70 65 72 61 6e 64 2e 0a 2a 2f 0a 53 ** operand..*/.S
19f8e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
19f8f 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 t sqlite3VdbeAdd
19f90 4f 70 33 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 Op3(Vdbe *p, int
19f91 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 6e 74 op, int p1, int
19f92 20 70 32 2c 20 69 6e 74 20 70 33 29 7b 0a 20 20 p2, int p3){.
19f93 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 4f 70 20 int i;. VdbeOp
19f94 2a 70 4f 70 3b 0a 0a 20 20 69 20 3d 20 70 2d 3e *pOp;.. i = p->
19f95 6e 4f 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70 nOp;. assert( p
19f96 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 ->magic==VDBE_MA
19f97 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 61 73 GIC_INIT );. as
19f98 73 65 72 74 28 20 6f 70 3e 30 20 26 26 20 6f 70 sert( op>0 && op
19f99 3c 30 78 66 66 20 29 3b 0a 20 20 69 66 28 20 70 <0xff );. if( p
19f9a 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c 3d 69 20 29 7b ->nOpAlloc<=i ){
19f9b 0a 20 20 20 20 69 66 28 20 67 72 6f 77 4f 70 41 . if( growOpA
19f9c 72 72 61 79 28 70 29 20 29 7b 0a 20 20 20 20 20 rray(p) ){.
19f9d 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
19f9e 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f 70 2b 2b 3b . }. p->nOp++;
19f9f 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 . pOp = &p->aOp
19fa0 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f [i];. pOp->opco
19fa1 64 65 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20 70 de = (u8)op;. p
19fa2 4f 70 2d 3e 70 35 20 3d 20 30 3b 0a 20 20 70 4f Op->p5 = 0;. pO
19fa3 70 2d 3e 70 31 20 3d 20 70 31 3b 0a 20 20 70 4f p->p1 = p1;. pO
19fa4 70 2d 3e 70 32 20 3d 20 70 32 3b 0a 20 20 70 4f p->p2 = p2;. pO
19fa5 70 2d 3e 70 33 20 3d 20 70 33 3b 0a 20 20 70 4f p->p3 = p3;. pO
19fa6 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 70 p->p4.p = 0;. p
19fa7 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f Op->p4type = P4_
19fa8 4e 4f 54 55 53 45 44 3b 0a 20 20 70 2d 3e 65 78 NOTUSED;. p->ex
19fa9 70 69 72 65 64 20 3d 20 30 3b 0a 23 69 66 64 65 pired = 0;.#ifde
19faa 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
19fab 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d pOp->zComment =
19fac 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 0;. if( sqlite
19fad 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20 3VdbeAddopTrace
19fae 29 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 ) sqlite3VdbePri
19faf 6e 74 4f 70 28 30 2c 20 69 2c 20 26 70 2d 3e 61 ntOp(0, i, &p->a
19fb0 4f 70 5b 69 5d 29 3b 0a 23 65 6e 64 69 66 0a 23 Op[i]);.#endif.#
19fb1 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 ifdef VDBE_PROFI
19fb2 4c 45 0a 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73 LE. pOp->cycles
19fb3 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 63 6e 74 = 0;. pOp->cnt
19fb4 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 = 0;.#endif. r
19fb5 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c 49 54 eturn i;.}.SQLIT
19fb6 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
19fb7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 lite3VdbeAddOp0(
19fb8 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 29 Vdbe *p, int op)
19fb9 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
19fba 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 e3VdbeAddOp3(p,
19fbb 6f 70 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 7d 0a op, 0, 0, 0);.}.
19fbc 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
19fbd 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
19fbe 64 4f 70 31 28 56 64 62 65 20 2a 70 2c 20 69 6e dOp1(Vdbe *p, in
19fbf 74 20 6f 70 2c 20 69 6e 74 20 70 31 29 7b 0a 20 t op, int p1){.
19fc0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 return sqlite3V
19fc1 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c dbeAddOp3(p, op,
19fc2 20 70 31 2c 20 30 2c 20 30 29 3b 0a 7d 0a 53 51 p1, 0, 0);.}.SQ
19fc3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
19fc4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
19fc5 70 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 p2(Vdbe *p, int
19fc6 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 6e 74 20 op, int p1, int
19fc7 70 32 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 p2){. return sq
19fc8 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
19fc9 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20 30 p, op, p1, p2, 0
19fca 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 );.}.../*.** Add
19fcb 20 61 6e 20 6f 70 63 6f 64 65 20 74 68 61 74 20 an opcode that
19fcc 69 6e 63 6c 75 64 65 73 20 74 68 65 20 70 34 20 includes the p4
19fcd 76 61 6c 75 65 20 61 73 20 61 20 70 6f 69 6e 74 value as a point
19fce 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
19fcf 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
19fd0 33 56 64 62 65 41 64 64 4f 70 34 28 0a 20 20 56 3VdbeAddOp4(. V
19fd1 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 dbe *p,
19fd2 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6f 70 /* Add the op
19fd3 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56 4d 20 code to this VM
19fd4 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 */. int op,
19fd5 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
19fd6 6e 65 77 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 new opcode */.
19fd7 69 6e 74 20 70 31 2c 20 20 20 20 20 20 20 20 20 int p1,
19fd8 20 20 20 20 2f 2a 20 54 68 65 20 50 31 20 6f 70 /* The P1 op
19fd9 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 erand */. int p
19fda 32 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2, /
19fdb 2a 20 54 68 65 20 50 32 20 6f 70 65 72 61 6e 64 * The P2 operand
19fdc 20 2a 2f 0a 20 20 69 6e 74 20 70 33 2c 20 20 20 */. int p3,
19fdd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
19fde 20 50 33 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 P3 operand */.
19fdf 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 const char *zP4
19fe0 2c 20 20 20 20 2f 2a 20 54 68 65 20 50 34 20 6f , /* The P4 o
19fe1 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 perand */. int
19fe2 70 34 74 79 70 65 20 20 20 20 20 20 20 20 20 20 p4type
19fe3 2f 2a 20 50 34 20 6f 70 65 72 61 6e 64 20 74 79 /* P4 operand ty
19fe4 70 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 pe */.){. int a
19fe5 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 ddr = sqlite3Vdb
19fe6 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70 eAddOp3(p, op, p
19fe7 31 2c 20 70 32 2c 20 70 33 29 3b 0a 20 20 73 71 1, p2, p3);. sq
19fe8 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
19fe9 34 28 70 2c 20 61 64 64 72 2c 20 7a 50 34 2c 20 4(p, addr, zP4,
19fea 70 34 74 79 70 65 29 3b 0a 20 20 72 65 74 75 72 p4type);. retur
19feb 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n addr;.}../*.**
19fec 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 79 Create a new sy
19fed 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 66 6f 72 mbolic label for
19fee 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 an instruction
19fef 74 68 61 74 20 68 61 73 20 79 65 74 20 74 6f 20 that has yet to
19ff0 62 65 0a 2a 2a 20 63 6f 64 65 64 2e 20 20 54 68 be.** coded. Th
19ff1 65 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c e symbolic label
19ff2 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 is really just
19ff3 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 a negative numbe
19ff4 72 2e 20 20 54 68 65 0a 2a 2a 20 6c 61 62 65 6c r. The.** label
19ff5 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 can be used as
19ff6 74 68 65 20 50 32 20 76 61 6c 75 65 20 6f 66 20 the P2 value of
19ff7 61 6e 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4c an operation. L
19ff8 61 74 65 72 2c 20 77 68 65 6e 0a 2a 2a 20 74 68 ater, when.** th
19ff9 65 20 6c 61 62 65 6c 20 69 73 20 72 65 73 6f 6c e label is resol
19ffa 76 65 64 20 74 6f 20 61 20 73 70 65 63 69 66 69 ved to a specifi
19ffb 63 20 61 64 64 72 65 73 73 2c 20 74 68 65 20 56 c address, the V
19ffc 44 42 45 20 77 69 6c 6c 20 73 63 61 6e 0a 2a 2a DBE will scan.**
19ffd 20 74 68 72 6f 75 67 68 20 69 74 73 20 6f 70 65 through its ope
19ffe 72 61 74 69 6f 6e 20 6c 69 73 74 20 61 6e 64 20 ration list and
19fff 63 68 61 6e 67 65 20 61 6c 6c 20 76 61 6c 75 65 change all value
1a000 73 20 6f 66 20 50 32 20 77 68 69 63 68 20 6d 61 s of P2 which ma
1a001 74 63 68 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c tch.** the label
1a002 20 69 6e 74 6f 20 74 68 65 20 72 65 73 6f 6c 76 into the resolv
1a003 65 64 20 61 64 64 72 65 73 73 2e 0a 2a 2a 0a 2a ed address..**.*
1a004 2a 20 54 68 65 20 56 44 42 45 20 6b 6e 6f 77 73 * The VDBE knows
1a005 20 74 68 61 74 20 61 20 50 32 20 76 61 6c 75 65 that a P2 value
1a006 20 69 73 20 61 20 6c 61 62 65 6c 20 62 65 63 61 is a label beca
1a007 75 73 65 20 6c 61 62 65 6c 73 20 61 72 65 0a 2a use labels are.*
1a008 2a 20 61 6c 77 61 79 73 20 6e 65 67 61 74 69 76 * always negativ
1a009 65 20 61 6e 64 20 50 32 20 76 61 6c 75 65 73 20 e and P2 values
1a00a 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f 20 62 are suppose to b
1a00b 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a e non-negative..
1a00c 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6e 65 67 61 ** Hence, a nega
1a00d 74 69 76 65 20 50 32 20 76 61 6c 75 65 20 69 73 tive P2 value is
1a00e 20 61 20 6c 61 62 65 6c 20 74 68 61 74 20 68 61 a label that ha
1a00f 73 20 79 65 74 20 74 6f 20 62 65 20 72 65 73 6f s yet to be reso
1a010 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a 65 72 6f lved..**.** Zero
1a011 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
1a012 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 a malloc() fails
1a013 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1a014 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
1a015 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62 dbeMakeLabel(Vdb
1a016 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a e *p){. int i;.
1a017 20 20 69 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 2b i = p->nLabel+
1a018 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e +;. assert( p->
1a019 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 magic==VDBE_MAGI
1a01a 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 C_INIT );. if(
1a01b 69 3e 3d 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f i>=p->nLabelAllo
1a01c 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d c ){. int n =
1a01d 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 2a p->nLabelAlloc*
1a01e 32 20 2b 20 35 3b 0a 20 20 20 20 70 2d 3e 61 4c 2 + 5;. p->aL
1a01f 61 62 65 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 abel = sqlite3Db
1a020 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 2d ReallocOrFree(p-
1a021 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 2c 0a >db, p->aLabel,.
1a022 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a023 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a024 20 20 20 20 20 20 20 6e 2a 73 69 7a 65 6f 66 28 n*sizeof(
1a025 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 29 3b 0a p->aLabel[0]));.
1a026 20 20 20 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c p->nLabelAll
1a027 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 oc = sqlite3DbMa
1a028 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64 62 2c 20 llocSize(p->db,
1a029 70 2d 3e 61 4c 61 62 65 6c 29 2f 73 69 7a 65 6f p->aLabel)/sizeo
1a02a 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 3b f(p->aLabel[0]);
1a02b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61 4c . }. if( p->aL
1a02c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 61 abel ){. p->a
1a02d 4c 61 62 65 6c 5b 69 5d 20 3d 20 2d 31 3b 0a 20 Label[i] = -1;.
1a02e 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 2d 69 }. return -1-i
1a02f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c ;.}../*.** Resol
1a030 76 65 20 6c 61 62 65 6c 20 22 78 22 20 74 6f 20 ve label "x" to
1a031 62 65 20 74 68 65 20 61 64 64 72 65 73 73 20 6f be the address o
1a032 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 f the next instr
1a033 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20 62 65 20 uction to.** be
1a034 69 6e 73 65 72 74 65 64 2e 20 20 54 68 65 20 70 inserted. The p
1a035 61 72 61 6d 65 74 65 72 20 22 78 22 20 6d 75 73 arameter "x" mus
1a036 74 20 68 61 76 65 20 62 65 65 6e 20 6f 62 74 61 t have been obta
1a037 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 61 20 70 ined from.** a p
1a038 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c rior call to sql
1a039 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 ite3VdbeMakeLabe
1a03a 6c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 l()..*/.SQLITE_P
1a03b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1a03c 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 te3VdbeResolveLa
1a03d 62 65 6c 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 bel(Vdbe *p, int
1a03e 20 78 29 7b 0a 20 20 69 6e 74 20 6a 20 3d 20 2d x){. int j = -
1a03f 31 2d 78 3b 0a 20 20 61 73 73 65 72 74 28 20 70 1-x;. assert( p
1a040 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 ->magic==VDBE_MA
1a041 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 61 73 GIC_INIT );. as
1a042 73 65 72 74 28 20 6a 3e 3d 30 20 26 26 20 6a 3c sert( j>=0 && j<
1a043 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a 20 20 69 p->nLabel );. i
1a044 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b 0a f( p->aLabel ){.
1a045 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 6a 5d p->aLabel[j]
1a046 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 7d 0a 7d = p->nOp;. }.}
1a047 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
1a048 44 45 42 55 47 20 2f 2a 20 73 71 6c 69 74 65 33 DEBUG /* sqlite3
1a049 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28 29 AssertMayAbort()
1a04a 20 6c 6f 67 69 63 20 2a 2f 0a 0a 2f 2a 0a 2a 2a logic */../*.**
1a04b 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 The following t
1a04c 79 70 65 20 61 6e 64 20 66 75 6e 63 74 69 6f 6e ype and function
1a04d 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 74 65 are used to ite
1a04e 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 6c 6c rate through all
1a04f 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 69 6e 20 61 opcodes.** in a
1a050 20 56 64 62 65 20 6d 61 69 6e 20 70 72 6f 67 72 Vdbe main progr
1a051 61 6d 20 61 6e 64 20 65 61 63 68 20 6f 66 20 74 am and each of t
1a052 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 73 20 he sub-programs
1a053 28 74 72 69 67 67 65 72 73 29 20 69 74 20 6d 61 (triggers) it ma
1a054 79 20 0a 2a 2a 20 69 6e 76 6f 6b 65 20 64 69 72 y .** invoke dir
1a055 65 63 74 6c 79 20 6f 72 20 69 6e 64 69 72 65 63 ectly or indirec
1a056 74 6c 79 2e 20 49 74 20 73 68 6f 75 6c 64 20 62 tly. It should b
1a057 65 20 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f 77 e used as follow
1a058 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 70 20 2a 70 s:.**.** Op *p
1a059 4f 70 3b 0a 2a 2a 20 20 20 56 64 62 65 4f 70 49 Op;.** VdbeOpI
1a05a 74 65 72 20 73 49 74 65 72 3b 0a 2a 2a 0a 2a 2a ter sIter;.**.**
1a05b 20 20 20 6d 65 6d 73 65 74 28 26 73 49 74 65 72 memset(&sIter
1a05c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 49 74 65 , 0, sizeof(sIte
1a05d 72 29 29 3b 0a 2a 2a 20 20 20 73 49 74 65 72 2e r));.** sIter.
1a05e 76 20 3d 20 76 3b 20 20 20 20 20 20 20 20 20 20 v = v;
1a05f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a060 20 20 2f 2f 20 76 20 69 73 20 6f 66 20 74 79 70 // v is of typ
1a061 65 20 56 64 62 65 2a 20 0a 2a 2a 20 20 20 77 68 e Vdbe* .** wh
1a062 69 6c 65 28 20 28 70 4f 70 20 3d 20 6f 70 49 74 ile( (pOp = opIt
1a063 65 72 4e 65 78 74 28 26 73 49 74 65 72 29 29 20 erNext(&sIter))
1a064 29 7b 0a 2a 2a 20 20 20 20 20 2f 2f 20 44 6f 20 ){.** // Do
1a065 73 6f 6d 65 74 68 69 6e 67 20 77 69 74 68 20 70 something with p
1a066 4f 70 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20 73 Op.** }.** s
1a067 71 6c 69 74 65 33 44 62 46 72 65 65 28 76 2d 3e qlite3DbFree(v->
1a068 64 62 2c 20 73 49 74 65 72 2e 61 70 53 75 62 29 db, sIter.apSub)
1a069 3b 0a 2a 2a 20 0a 2a 2f 0a 74 79 70 65 64 65 66 ;.** .*/.typedef
1a06a 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 49 74 struct VdbeOpIt
1a06b 65 72 20 56 64 62 65 4f 70 49 74 65 72 3b 0a 73 er VdbeOpIter;.s
1a06c 74 72 75 63 74 20 56 64 62 65 4f 70 49 74 65 72 truct VdbeOpIter
1a06d 20 7b 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 {. Vdbe *v;
1a06e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a06f 2f 2a 20 56 64 62 65 20 74 6f 20 69 74 65 72 61 /* Vdbe to itera
1a070 74 65 20 74 68 72 6f 75 67 68 20 74 68 65 20 6f te through the o
1a071 70 63 6f 64 65 73 20 6f 66 20 2a 2f 0a 20 20 53 pcodes of */. S
1a072 75 62 50 72 6f 67 72 61 6d 20 2a 2a 61 70 53 75 ubProgram **apSu
1a073 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 b; /* Arr
1a074 61 79 20 6f 66 20 73 75 62 70 72 6f 67 72 61 6d ay of subprogram
1a075 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 75 62 3b s */. int nSub;
1a076 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a077 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
1a078 6e 74 72 69 65 73 20 69 6e 20 61 70 53 75 62 20 ntries in apSub
1a079 2a 2f 0a 20 20 69 6e 74 20 69 41 64 64 72 3b 20 */. int iAddr;
1a07a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a07b 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e 65 /* Address of ne
1a07c 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 xt instruction t
1a07d 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e o return */. in
1a07e 74 20 69 53 75 62 3b 20 20 20 20 20 20 20 20 20 t iSub;
1a07f 20 20 20 20 20 20 20 20 20 2f 2a 20 30 20 3d 20 /* 0 =
1a080 6d 61 69 6e 20 70 72 6f 67 72 61 6d 2c 20 31 20 main program, 1
1a081 3d 20 66 69 72 73 74 20 73 75 62 2d 70 72 6f 67 = first sub-prog
1a082 72 61 6d 20 65 74 63 2e 20 2a 2f 0a 7d 3b 0a 73 ram etc. */.};.s
1a083 74 61 74 69 63 20 4f 70 20 2a 6f 70 49 74 65 72 tatic Op *opIter
1a084 4e 65 78 74 28 56 64 62 65 4f 70 49 74 65 72 20 Next(VdbeOpIter
1a085 2a 70 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d *p){. Vdbe *v =
1a086 20 70 2d 3e 76 3b 0a 20 20 4f 70 20 2a 70 52 65 p->v;. Op *pRe
1a087 74 20 3d 20 30 3b 0a 20 20 4f 70 20 2a 61 4f 70 t = 0;. Op *aOp
1a088 3b 0a 20 20 69 6e 74 20 6e 4f 70 3b 0a 0a 20 20 ;. int nOp;..
1a089 69 66 28 20 70 2d 3e 69 53 75 62 3c 3d 70 2d 3e if( p->iSub<=p->
1a08a 6e 53 75 62 20 29 7b 0a 0a 20 20 20 20 69 66 28 nSub ){.. if(
1a08b 20 70 2d 3e 69 53 75 62 3d 3d 30 20 29 7b 0a 20 p->iSub==0 ){.
1a08c 20 20 20 20 20 61 4f 70 20 3d 20 76 2d 3e 61 4f aOp = v->aO
1a08d 70 3b 0a 20 20 20 20 20 20 6e 4f 70 20 3d 20 76 p;. nOp = v
1a08e 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 65 6c 73 65 ->nOp;. }else
1a08f 7b 0a 20 20 20 20 20 20 61 4f 70 20 3d 20 70 2d {. aOp = p-
1a090 3e 61 70 53 75 62 5b 70 2d 3e 69 53 75 62 2d 31 >apSub[p->iSub-1
1a091 5d 2d 3e 61 4f 70 3b 0a 20 20 20 20 20 20 6e 4f ]->aOp;. nO
1a092 70 20 3d 20 70 2d 3e 61 70 53 75 62 5b 70 2d 3e p = p->apSub[p->
1a093 69 53 75 62 2d 31 5d 2d 3e 6e 4f 70 3b 0a 20 20 iSub-1]->nOp;.
1a094 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
1a095 70 2d 3e 69 41 64 64 72 3c 6e 4f 70 20 29 3b 0a p->iAddr<nOp );.
1a096 0a 20 20 20 20 70 52 65 74 20 3d 20 26 61 4f 70 . pRet = &aOp
1a097 5b 70 2d 3e 69 41 64 64 72 5d 3b 0a 20 20 20 20 [p->iAddr];.
1a098 70 2d 3e 69 41 64 64 72 2b 2b 3b 0a 20 20 20 20 p->iAddr++;.
1a099 69 66 28 20 70 2d 3e 69 41 64 64 72 3d 3d 6e 4f if( p->iAddr==nO
1a09a 70 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 53 p ){. p->iS
1a09b 75 62 2b 2b 3b 0a 20 20 20 20 20 20 70 2d 3e 69 ub++;. p->i
1a09c 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 7d 0a Addr = 0;. }.
1a09d 20 20 0a 20 20 20 20 69 66 28 20 70 52 65 74 2d . if( pRet-
1a09e 3e 70 34 74 79 70 65 3d 3d 50 34 5f 53 55 42 50 >p4type==P4_SUBP
1a09f 52 4f 47 52 41 4d 20 29 7b 0a 20 20 20 20 20 20 ROGRAM ){.
1a0a0 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 70 2d 3e int nByte = (p->
1a0a1 6e 53 75 62 2b 31 29 2a 73 69 7a 65 6f 66 28 53 nSub+1)*sizeof(S
1a0a2 75 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 ubProgram*);.
1a0a3 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 int j;.
1a0a4 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 53 for(j=0; j<p->nS
1a0a5 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 ub; j++){.
1a0a6 20 20 69 66 28 20 70 2d 3e 61 70 53 75 62 5b 6a if( p->apSub[j
1a0a7 5d 3d 3d 70 52 65 74 2d 3e 70 34 2e 70 50 72 6f ]==pRet->p4.pPro
1a0a8 67 72 61 6d 20 29 20 62 72 65 61 6b 3b 0a 20 20 gram ) break;.
1a0a9 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
1a0aa 6a 3d 3d 70 2d 3e 6e 53 75 62 20 29 7b 0a 20 20 j==p->nSub ){.
1a0ab 20 20 20 20 20 20 70 2d 3e 61 70 53 75 62 20 3d p->apSub =
1a0ac 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f sqlite3DbReallo
1a0ad 63 4f 72 46 72 65 65 28 76 2d 3e 64 62 2c 20 70 cOrFree(v->db, p
1a0ae 2d 3e 61 70 53 75 62 2c 20 6e 42 79 74 65 29 3b ->apSub, nByte);
1a0af 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 2d . if( !p-
1a0b0 3e 61 70 53 75 62 20 29 7b 0a 20 20 20 20 20 20 >apSub ){.
1a0b1 20 20 20 20 70 52 65 74 20 3d 20 30 3b 0a 20 20 pRet = 0;.
1a0b2 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1a0b3 20 20 20 20 20 20 20 70 2d 3e 61 70 53 75 62 5b p->apSub[
1a0b4 70 2d 3e 6e 53 75 62 2b 2b 5d 20 3d 20 70 52 65 p->nSub++] = pRe
1a0b5 74 2d 3e 70 34 2e 70 50 72 6f 67 72 61 6d 3b 0a t->p4.pProgram;.
1a0b6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1a0b7 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 }. }. }.. r
1a0b8 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f eturn pRet;.}../
1a0b9 2a 0a 2a 2a 20 43 68 65 63 6b 20 69 66 20 74 68 *.** Check if th
1a0ba 65 20 70 72 6f 67 72 61 6d 20 73 74 6f 72 65 64 e program stored
1a0bb 20 69 6e 20 74 68 65 20 56 4d 20 61 73 73 6f 63 in the VM assoc
1a0bc 69 61 74 65 64 20 77 69 74 68 20 70 50 61 72 73 iated with pPars
1a0bd 65 20 6d 61 79 0a 2a 2a 20 74 68 72 6f 77 20 61 e may.** throw a
1a0be 6e 20 41 42 4f 52 54 20 65 78 63 65 70 74 69 6f n ABORT exceptio
1a0bf 6e 20 28 63 61 75 73 69 6e 67 20 74 68 65 20 73 n (causing the s
1a0c0 74 61 74 65 6d 65 6e 74 2c 20 62 75 74 20 6e 6f tatement, but no
1a0c1 74 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 t entire transac
1a0c2 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 72 6f tion.** to be ro
1a0c3 6c 6c 65 64 20 62 61 63 6b 29 2e 20 54 68 69 73 lled back). This
1a0c4 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20 74 72 condition is tr
1a0c5 75 65 20 69 66 20 74 68 65 20 6d 61 69 6e 20 70 ue if the main p
1a0c6 72 6f 67 72 61 6d 20 6f 72 20 61 6e 79 0a 2a 2a rogram or any.**
1a0c7 20 73 75 62 2d 70 72 6f 67 72 61 6d 73 20 63 6f sub-programs co
1a0c8 6e 74 61 69 6e 73 20 61 6e 79 20 6f 66 20 74 68 ntains any of th
1a0c9 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a e following:.**.
1a0ca 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 61 6c 74 20 ** * OP_Halt
1a0cb 77 69 74 68 20 50 31 3d 53 51 4c 49 54 45 5f 43 with P1=SQLITE_C
1a0cc 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20 50 32 ONSTRAINT and P2
1a0cd 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20 20 =OE_Abort..**
1a0ce 2a 20 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c * OP_HaltIfNull
1a0cf 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54 45 5f with P1=SQLITE_
1a0d0 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20 50 CONSTRAINT and P
1a0d1 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20 2=OE_Abort..**
1a0d2 20 2a 20 20 4f 50 5f 44 65 73 74 72 6f 79 0a 2a * OP_Destroy.*
1a0d3 2a 20 20 20 2a 20 20 4f 50 5f 56 55 70 64 61 74 * * OP_VUpdat
1a0d4 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 52 65 e.** * OP_VRe
1a0d5 6e 61 6d 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f name.** * OP_
1a0d6 46 6b 43 6f 75 6e 74 65 72 20 77 69 74 68 20 50 FkCounter with P
1a0d7 32 3d 3d 30 20 28 69 6d 6d 65 64 69 61 74 65 20 2==0 (immediate
1a0d8 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 foreign key cons
1a0d9 74 72 61 69 6e 74 29 0a 2a 2a 0a 2a 2a 20 54 68 traint).**.** Th
1a0da 65 6e 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 en check that th
1a0db 65 20 76 61 6c 75 65 20 6f 66 20 50 61 72 73 65 e value of Parse
1a0dc 2e 6d 61 79 41 62 6f 72 74 20 69 73 20 74 72 75 .mayAbort is tru
1a0dd 65 20 69 66 20 61 6e 0a 2a 2a 20 41 42 4f 52 54 e if an.** ABORT
1a0de 20 6d 61 79 20 62 65 20 74 68 72 6f 77 6e 2c 20 may be thrown,
1a0df 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69 or false otherwi
1a0e0 73 65 2e 20 52 65 74 75 72 6e 20 74 72 75 65 20 se. Return true
1a0e1 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6d 61 if it does.** ma
1a0e2 74 63 68 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 tch, or false ot
1a0e3 68 65 72 77 69 73 65 2e 20 54 68 69 73 20 66 75 herwise. This fu
1a0e4 6e 63 74 69 6f 6e 20 69 73 20 69 6e 74 65 6e 64 nction is intend
1a0e5 65 64 20 74 6f 20 62 65 20 75 73 65 64 20 61 73 ed to be used as
1a0e6 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 6e 20 61 .** part of an a
1a0e7 73 73 65 72 74 20 73 74 61 74 65 6d 65 6e 74 20 ssert statement
1a0e8 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e in the compiler.
1a0e9 20 53 69 6d 69 6c 61 72 20 74 6f 3a 0a 2a 2a 0a Similar to:.**.
1a0ea 2a 2a 20 20 20 61 73 73 65 72 74 28 20 73 71 6c ** assert( sql
1a0eb 69 74 65 33 56 64 62 65 41 73 73 65 72 74 4d 61 ite3VdbeAssertMa
1a0ec 79 41 62 6f 72 74 28 70 50 61 72 73 65 2d 3e 70 yAbort(pParse->p
1a0ed 56 64 62 65 2c 20 70 50 61 72 73 65 2d 3e 6d 61 Vdbe, pParse->ma
1a0ee 79 41 62 6f 72 74 29 20 29 3b 0a 2a 2f 0a 53 51 yAbort) );.*/.SQ
1a0ef 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1a0f0 20 73 71 6c 69 74 65 33 56 64 62 65 41 73 73 65 sqlite3VdbeAsse
1a0f1 72 74 4d 61 79 41 62 6f 72 74 28 56 64 62 65 20 rtMayAbort(Vdbe
1a0f2 2a 76 2c 20 69 6e 74 20 6d 61 79 41 62 6f 72 74 *v, int mayAbort
1a0f3 29 7b 0a 20 20 69 6e 74 20 68 61 73 41 62 6f 72 ){. int hasAbor
1a0f4 74 20 3d 20 30 3b 0a 20 20 4f 70 20 2a 70 4f 70 t = 0;. Op *pOp
1a0f5 3b 0a 20 20 56 64 62 65 4f 70 49 74 65 72 20 73 ;. VdbeOpIter s
1a0f6 49 74 65 72 3b 0a 20 20 6d 65 6d 73 65 74 28 26 Iter;. memset(&
1a0f7 73 49 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 sIter, 0, sizeof
1a0f8 28 73 49 74 65 72 29 29 3b 0a 20 20 73 49 74 65 (sIter));. sIte
1a0f9 72 2e 76 20 3d 20 76 3b 0a 0a 20 20 77 68 69 6c r.v = v;.. whil
1a0fa 65 28 20 28 70 4f 70 20 3d 20 6f 70 49 74 65 72 e( (pOp = opIter
1a0fb 4e 65 78 74 28 26 73 49 74 65 72 29 29 21 3d 30 Next(&sIter))!=0
1a0fc 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 63 6f ){. int opco
1a0fd 64 65 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 de = pOp->opcode
1a0fe 3b 0a 20 20 20 20 69 66 28 20 6f 70 63 6f 64 65 ;. if( opcode
1a0ff 3d 3d 4f 50 5f 44 65 73 74 72 6f 79 20 7c 7c 20 ==OP_Destroy ||
1a100 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61 opcode==OP_VUpda
1a101 74 65 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 te || opcode==OP
1a102 5f 56 52 65 6e 61 6d 65 20 0a 23 69 66 6e 64 65 _VRename .#ifnde
1a103 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f f SQLITE_OMIT_FO
1a104 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20 20 20 7c REIGN_KEY. |
1a105 7c 20 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6b | (opcode==OP_Fk
1a106 43 6f 75 6e 74 65 72 20 26 26 20 70 4f 70 2d 3e Counter && pOp->
1a107 70 31 3d 3d 30 20 26 26 20 70 4f 70 2d 3e 70 32 p1==0 && pOp->p2
1a108 3d 3d 31 29 20 0a 23 65 6e 64 69 66 0a 20 20 20 ==1) .#endif.
1a109 20 20 7c 7c 20 28 28 6f 70 63 6f 64 65 3d 3d 4f || ((opcode==O
1a10a 50 5f 48 61 6c 74 20 7c 7c 20 6f 70 63 6f 64 65 P_Halt || opcode
1a10b 3d 3d 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 29 ==OP_HaltIfNull)
1a10c 20 0a 20 20 20 20 20 20 26 26 20 28 70 4f 70 2d . && (pOp-
1a10d 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 >p1==SQLITE_CONS
1a10e 54 52 41 49 4e 54 20 26 26 20 70 4f 70 2d 3e 70 TRAINT && pOp->p
1a10f 32 3d 3d 4f 45 5f 41 62 6f 72 74 29 29 0a 20 20 2==OE_Abort)).
1a110 20 20 29 7b 0a 20 20 20 20 20 20 68 61 73 41 62 ){. hasAb
1a111 6f 72 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 62 ort = 1;. b
1a112 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
1a113 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1a114 76 2d 3e 64 62 2c 20 73 49 74 65 72 2e 61 70 53 v->db, sIter.apS
1a115 75 62 29 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 ub);.. /* Retur
1a116 6e 20 74 72 75 65 20 69 66 20 68 61 73 41 62 6f n true if hasAbo
1a117 72 74 3d 3d 6d 61 79 41 62 6f 72 74 2e 20 4f 72 rt==mayAbort. Or
1a118 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 if a malloc fai
1a119 6c 75 72 65 20 6f 63 63 75 72 65 64 2e 0a 20 20 lure occured..
1a11a 2a 2a 20 49 66 20 6d 61 6c 6c 6f 63 20 66 61 69 ** If malloc fai
1a11b 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 20 77 68 led, then the wh
1a11c 69 6c 65 28 29 20 6c 6f 6f 70 20 61 62 6f 76 65 ile() loop above
1a11d 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 69 74 may not have it
1a11e 65 72 61 74 65 64 0a 20 20 2a 2a 20 74 68 72 6f erated. ** thro
1a11f 75 67 68 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 ugh all opcodes
1a120 61 6e 64 20 68 61 73 41 62 6f 72 74 20 6d 61 79 and hasAbort may
1a121 20 62 65 20 73 65 74 20 69 6e 63 6f 72 72 65 63 be set incorrec
1a122 74 6c 79 2e 20 52 65 74 75 72 6e 0a 20 20 2a 2a tly. Return. **
1a123 20 74 72 75 65 20 66 6f 72 20 74 68 69 73 20 63 true for this c
1a124 61 73 65 20 74 6f 20 70 72 65 76 65 6e 74 20 74 ase to prevent t
1a125 68 65 20 61 73 73 65 72 74 28 29 20 69 6e 20 74 he assert() in t
1a126 68 65 20 63 61 6c 6c 65 72 73 20 66 72 61 6d 65 he callers frame
1a127 0a 20 20 2a 2a 20 66 72 6f 6d 20 66 61 69 6c 69 . ** from faili
1a128 6e 67 2e 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e ng. */. return
1a129 20 28 20 76 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 ( v->db->malloc
1a12a 46 61 69 6c 65 64 20 7c 7c 20 68 61 73 41 62 6f Failed || hasAbo
1a12b 72 74 3d 3d 6d 61 79 41 62 6f 72 74 20 29 3b 0a rt==mayAbort );.
1a12c 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1a12d 54 45 5f 44 45 42 55 47 20 2d 20 74 68 65 20 73 TE_DEBUG - the s
1a12e 71 6c 69 74 65 33 41 73 73 65 72 74 4d 61 79 41 qlite3AssertMayA
1a12f 62 6f 72 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 bort() function
1a130 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20 74 */../*.** Loop t
1a131 68 72 6f 75 67 68 20 74 68 65 20 70 72 6f 67 72 hrough the progr
1a132 61 6d 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 50 am looking for P
1a133 32 20 76 61 6c 75 65 73 20 74 68 61 74 20 61 72 2 values that ar
1a134 65 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6f 6e e negative.** on
1a135 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f jump instructio
1a136 6e 73 2e 20 20 45 61 63 68 20 73 75 63 68 20 76 ns. Each such v
1a137 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c 2e alue is a label.
1a138 20 20 52 65 73 6f 6c 76 65 20 74 68 65 0a 2a 2a Resolve the.**
1a139 20 6c 61 62 65 6c 20 62 79 20 73 65 74 74 69 6e label by settin
1a13a 67 20 74 68 65 20 50 32 20 76 61 6c 75 65 20 74 g the P2 value t
1a13b 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20 6e 6f o its correct no
1a13c 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a 2a n-zero value..**
1a13d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1a13e 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 is called once
1a13f 61 66 74 65 72 20 61 6c 6c 20 6f 70 63 6f 64 65 after all opcode
1a140 73 20 68 61 76 65 20 62 65 65 6e 20 69 6e 73 65 s have been inse
1a141 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69 rted..**.** Vari
1a142 61 62 6c 65 20 2a 70 4d 61 78 46 75 6e 63 41 72 able *pMaxFuncAr
1a143 67 73 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 gs is set to the
1a144 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f maximum value o
1a145 66 20 61 6e 79 20 50 32 20 61 72 67 75 6d 65 6e f any P2 argumen
1a146 74 20 0a 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f 46 t .** to an OP_F
1a147 75 6e 63 74 69 6f 6e 2c 20 4f 50 5f 41 67 67 53 unction, OP_AggS
1a148 74 65 70 20 6f 72 20 4f 50 5f 56 46 69 6c 74 65 tep or OP_VFilte
1a149 72 20 6f 70 63 6f 64 65 2e 20 54 68 69 73 20 69 r opcode. This i
1a14a 73 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 73 71 s used by .** sq
1a14b 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 lite3VdbeMakeRea
1a14c 64 79 28 29 20 74 6f 20 73 69 7a 65 20 74 68 65 dy() to size the
1a14d 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 72 Vdbe.apArg[] ar
1a14e 72 61 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ray..*/.static v
1a14f 6f 69 64 20 72 65 73 6f 6c 76 65 50 32 56 61 6c oid resolveP2Val
1a150 75 65 73 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 ues(Vdbe *p, int
1a151 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 29 7b *pMaxFuncArgs){
1a152 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 . int i;. int
1a153 6e 4d 61 78 41 72 67 73 20 3d 20 2a 70 4d 61 78 nMaxArgs = *pMax
1a154 46 75 6e 63 41 72 67 73 3b 0a 20 20 4f 70 20 2a FuncArgs;. Op *
1a155 70 4f 70 3b 0a 20 20 69 6e 74 20 2a 61 4c 61 62 pOp;. int *aLab
1a156 65 6c 20 3d 20 70 2d 3e 61 4c 61 62 65 6c 3b 0a el = p->aLabel;.
1a157 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 p->readOnly =
1a158 31 3b 0a 20 20 66 6f 72 28 70 4f 70 3d 70 2d 3e 1;. for(pOp=p->
1a159 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f 70 2d 31 3b aOp, i=p->nOp-1;
1a15a 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 70 2b i>=0; i--, pOp+
1a15b 2b 29 7b 0a 20 20 20 20 75 38 20 6f 70 63 6f 64 +){. u8 opcod
1a15c 65 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b e = pOp->opcode;
1a15d 0a 0a 20 20 20 20 69 66 28 20 6f 70 63 6f 64 65 .. if( opcode
1a15e 3d 3d 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 7c 7c ==OP_Function ||
1a15f 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 67 67 53 opcode==OP_AggS
1a160 74 65 70 20 29 7b 0a 20 20 20 20 20 20 69 66 28 tep ){. if(
1a161 20 70 4f 70 2d 3e 70 35 3e 6e 4d 61 78 41 72 67 pOp->p5>nMaxArg
1a162 73 20 29 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 s ) nMaxArgs = p
1a163 4f 70 2d 3e 70 35 3b 0a 23 69 66 6e 64 65 66 20 Op->p5;.#ifndef
1a164 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1a165 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c UALTABLE. }el
1a166 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f se if( opcode==O
1a167 50 5f 56 55 70 64 61 74 65 20 29 7b 0a 20 20 20 P_VUpdate ){.
1a168 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3e 6e if( pOp->p2>n
1a169 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72 MaxArgs ) nMaxAr
1a16a 67 73 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23 65 gs = pOp->p2;.#e
1a16b 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 20 69 ndif. }else i
1a16c 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 f( opcode==OP_Tr
1a16d 61 6e 73 61 63 74 69 6f 6e 20 26 26 20 70 4f 70 ansaction && pOp
1a16e 2d 3e 70 32 21 3d 30 20 29 7b 0a 20 20 20 20 20 ->p2!=0 ){.
1a16f 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 30 p->readOnly = 0
1a170 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
1a171 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1a172 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 LE. }else if(
1a173 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c opcode==OP_VFil
1a174 74 65 72 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 ter ){. int
1a175 20 6e 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 n;. assert
1a176 28 20 70 2d 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20 ( p->nOp - i >=
1a177 33 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 3 );. asser
1a178 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 t( pOp[-1].opcod
1a179 65 3d 3d 4f 50 5f 49 6e 74 65 67 65 72 20 29 3b e==OP_Integer );
1a17a 0a 20 20 20 20 20 20 6e 20 3d 20 70 4f 70 5b 2d . n = pOp[-
1a17b 31 5d 2e 70 31 3b 0a 20 20 20 20 20 20 69 66 28 1].p1;. if(
1a17c 20 6e 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d n>nMaxArgs ) nM
1a17d 61 78 41 72 67 73 20 3d 20 6e 3b 0a 23 65 6e 64 axArgs = n;.#end
1a17e 69 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 if. }.. if
1a17f 28 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63 ( sqlite3VdbeOpc
1a180 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79 28 6f odeHasProperty(o
1a181 70 63 6f 64 65 2c 20 4f 50 46 4c 47 5f 4a 55 4d pcode, OPFLG_JUM
1a182 50 29 20 26 26 20 70 4f 70 2d 3e 70 32 3c 30 20 P) && pOp->p2<0
1a183 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
1a184 20 2d 31 2d 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e -1-pOp->p2<p->n
1a185 4c 61 62 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 Label );. p
1a186 4f 70 2d 3e 70 32 20 3d 20 61 4c 61 62 65 6c 5b Op->p2 = aLabel[
1a187 2d 31 2d 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 -1-pOp->p2];.
1a188 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 }. }. sqlite3
1a189 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d DbFree(p->db, p-
1a18a 3e 61 4c 61 62 65 6c 29 3b 0a 20 20 70 2d 3e 61 >aLabel);. p->a
1a18b 4c 61 62 65 6c 20 3d 20 30 3b 0a 0a 20 20 2a 70 Label = 0;.. *p
1a18c 4d 61 78 46 75 6e 63 41 72 67 73 20 3d 20 6e 4d MaxFuncArgs = nM
1a18d 61 78 41 72 67 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a axArgs;.}../*.**
1a18e 20 52 65 74 75 72 6e 20 74 68 65 20 61 64 64 72 Return the addr
1a18f 65 73 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 ess of the next
1a190 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 instruction to b
1a191 65 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 e inserted..*/.S
1a192 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1a193 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 t sqlite3VdbeCur
1a194 72 65 6e 74 41 64 64 72 28 56 64 62 65 20 2a 70 rentAddr(Vdbe *p
1a195 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e ){. assert( p->
1a196 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 magic==VDBE_MAGI
1a197 43 5f 49 4e 49 54 20 29 3b 0a 20 20 72 65 74 75 C_INIT );. retu
1a198 72 6e 20 70 2d 3e 6e 4f 70 3b 0a 7d 0a 0a 2f 2a rn p->nOp;.}../*
1a199 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
1a19a 6e 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e n returns a poin
1a19b 74 65 72 20 74 6f 20 74 68 65 20 61 72 72 61 79 ter to the array
1a19c 20 6f 66 20 6f 70 63 6f 64 65 73 20 61 73 73 6f of opcodes asso
1a19d 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 74 ciated with.** t
1a19e 68 65 20 56 64 62 65 20 70 61 73 73 65 64 20 61 he Vdbe passed a
1a19f 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 s the first argu
1a1a0 6d 65 6e 74 2e 20 49 74 20 69 73 20 74 68 65 20 ment. It is the
1a1a1 63 61 6c 6c 65 72 73 20 72 65 73 70 6f 6e 73 69 callers responsi
1a1a2 62 69 6c 69 74 79 0a 2a 2a 20 74 6f 20 61 72 72 bility.** to arr
1a1a3 61 6e 67 65 20 66 6f 72 20 74 68 65 20 72 65 74 ange for the ret
1a1a4 75 72 6e 65 64 20 61 72 72 61 79 20 74 6f 20 62 urned array to b
1a1a5 65 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 e eventually fre
1a1a6 65 64 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a ed using the .**
1a1a7 20 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79 vdbeFreeOpArray
1a1a8 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a () function..**.
1a1a9 2a 2a 20 42 65 66 6f 72 65 20 72 65 74 75 72 6e ** Before return
1a1aa 69 6e 67 2c 20 2a 70 6e 4f 70 20 69 73 20 73 65 ing, *pnOp is se
1a1ab 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 t to the number
1a1ac 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 of entries in th
1a1ad 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 72 e returned.** ar
1a1ae 72 61 79 2e 20 41 6c 73 6f 2c 20 2a 70 6e 4d 61 ray. Also, *pnMa
1a1af 78 41 72 67 20 69 73 20 73 65 74 20 74 6f 20 74 xArg is set to t
1a1b0 68 65 20 6c 61 72 67 65 72 20 6f 66 20 69 74 73 he larger of its
1a1b1 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 61 current value a
1a1b2 6e 64 20 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 nd .** the numbe
1a1b3 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 r of entries in
1a1b4 74 68 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d the Vdbe.apArg[]
1a1b5 20 61 72 72 61 79 20 72 65 71 75 69 72 65 64 20 array required
1a1b6 74 6f 20 65 78 65 63 75 74 65 20 74 68 65 20 0a to execute the .
1a1b7 2a 2a 20 72 65 74 75 72 6e 65 64 20 70 72 6f 67 ** returned prog
1a1b8 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ram..*/.SQLITE_P
1a1b9 52 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73 RIVATE VdbeOp *s
1a1ba 71 6c 69 74 65 33 56 64 62 65 54 61 6b 65 4f 70 qlite3VdbeTakeOp
1a1bb 41 72 72 61 79 28 56 64 62 65 20 2a 70 2c 20 69 Array(Vdbe *p, i
1a1bc 6e 74 20 2a 70 6e 4f 70 2c 20 69 6e 74 20 2a 70 nt *pnOp, int *p
1a1bd 6e 4d 61 78 41 72 67 29 7b 0a 20 20 56 64 62 65 nMaxArg){. Vdbe
1a1be 4f 70 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 Op *aOp = p->aOp
1a1bf 3b 0a 20 20 61 73 73 65 72 74 28 20 61 4f 70 20 ;. assert( aOp
1a1c0 26 26 20 21 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f && !p->db->mallo
1a1c1 63 46 61 69 6c 65 64 20 29 3b 0a 0a 20 20 2f 2a cFailed );.. /*
1a1c2 20 43 68 65 63 6b 20 74 68 61 74 20 73 71 6c 69 Check that sqli
1a1c3 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 te3VdbeUsesBtree
1a1c4 28 29 20 77 61 73 20 6e 6f 74 20 63 61 6c 6c 65 () was not calle
1a1c5 64 20 6f 6e 20 74 68 69 73 20 56 4d 20 2a 2f 0a d on this VM */.
1a1c6 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4d 75 assert( p->aMu
1a1c7 74 65 78 2e 6e 4d 75 74 65 78 3d 3d 30 20 29 3b tex.nMutex==0 );
1a1c8 0a 0a 20 20 72 65 73 6f 6c 76 65 50 32 56 61 6c .. resolveP2Val
1a1c9 75 65 73 28 70 2c 20 70 6e 4d 61 78 41 72 67 29 ues(p, pnMaxArg)
1a1ca 3b 0a 20 20 2a 70 6e 4f 70 20 3d 20 70 2d 3e 6e ;. *pnOp = p->n
1a1cb 4f 70 3b 0a 20 20 70 2d 3e 61 4f 70 20 3d 20 30 Op;. p->aOp = 0
1a1cc 3b 0a 20 20 72 65 74 75 72 6e 20 61 4f 70 3b 0a ;. return aOp;.
1a1cd 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 77 }../*.** Add a w
1a1ce 68 6f 6c 65 20 6c 69 73 74 20 6f 66 20 6f 70 65 hole list of ope
1a1cf 72 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 6f rations to the o
1a1d0 70 65 72 61 74 69 6f 6e 20 73 74 61 63 6b 2e 20 peration stack.
1a1d1 20 52 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 61 Return the.** a
1a1d2 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 66 69 ddress of the fi
1a1d3 72 73 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 64 rst operation ad
1a1d4 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ded..*/.SQLITE_P
1a1d5 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1a1d6 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 e3VdbeAddOpList(
1a1d7 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 4f 70 Vdbe *p, int nOp
1a1d8 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f 6e , VdbeOpList con
1a1d9 73 74 20 2a 61 4f 70 29 7b 0a 20 20 69 6e 74 20 st *aOp){. int
1a1da 61 64 64 72 3b 0a 20 20 61 73 73 65 72 74 28 20 addr;. assert(
1a1db 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d p->magic==VDBE_M
1a1dc 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 AGIC_INIT );. i
1a1dd 66 28 20 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70 20 f( p->nOp + nOp
1a1de 3e 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 26 26 > p->nOpAlloc &&
1a1df 20 67 72 6f 77 4f 70 41 72 72 61 79 28 70 29 20 growOpArray(p)
1a1e0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
1a1e1 0a 20 20 7d 0a 20 20 61 64 64 72 20 3d 20 70 2d . }. addr = p-
1a1e2 3e 6e 4f 70 3b 0a 20 20 69 66 28 20 41 4c 57 41 >nOp;. if( ALWA
1a1e3 59 53 28 6e 4f 70 3e 30 29 20 29 7b 0a 20 20 20 YS(nOp>0) ){.
1a1e4 20 69 6e 74 20 69 3b 0a 20 20 20 20 56 64 62 65 int i;. Vdbe
1a1e5 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 70 49 OpList const *pI
1a1e6 6e 20 3d 20 61 4f 70 3b 0a 20 20 20 20 66 6f 72 n = aOp;. for
1a1e7 28 69 3d 30 3b 20 69 3c 6e 4f 70 3b 20 69 2b 2b (i=0; i<nOp; i++
1a1e8 2c 20 70 49 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 , pIn++){.
1a1e9 69 6e 74 20 70 32 20 3d 20 70 49 6e 2d 3e 70 32 int p2 = pIn->p2
1a1ea 3b 0a 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a ;. VdbeOp *
1a1eb 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 pOut = &p->aOp[i
1a1ec 2b 61 64 64 72 5d 3b 0a 20 20 20 20 20 20 70 4f +addr];. pO
1a1ed 75 74 2d 3e 6f 70 63 6f 64 65 20 3d 20 70 49 6e ut->opcode = pIn
1a1ee 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 20 20 ->opcode;.
1a1ef 70 4f 75 74 2d 3e 70 31 20 3d 20 70 49 6e 2d 3e pOut->p1 = pIn->
1a1f0 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70 32 p1;. if( p2
1a1f1 3c 30 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 <0 && sqlite3Vdb
1a1f2 65 4f 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72 eOpcodeHasProper
1a1f3 74 79 28 70 4f 75 74 2d 3e 6f 70 63 6f 64 65 2c ty(pOut->opcode,
1a1f4 20 4f 50 46 4c 47 5f 4a 55 4d 50 29 20 29 7b 0a OPFLG_JUMP) ){.
1a1f5 20 20 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 32 pOut->p2
1a1f6 20 3d 20 61 64 64 72 20 2b 20 41 44 44 52 28 70 = addr + ADDR(p
1a1f7 32 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 2);. }else{
1a1f8 0a 20 20 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 . pOut->p
1a1f9 32 20 3d 20 70 32 3b 0a 20 20 20 20 20 20 7d 0a 2 = p2;. }.
1a1fa 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 33 20 3d pOut->p3 =
1a1fb 20 70 49 6e 2d 3e 70 33 3b 0a 20 20 20 20 20 20 pIn->p3;.
1a1fc 70 4f 75 74 2d 3e 70 34 74 79 70 65 20 3d 20 50 pOut->p4type = P
1a1fd 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 20 20 20 4_NOTUSED;.
1a1fe 20 70 4f 75 74 2d 3e 70 34 2e 70 20 3d 20 30 3b pOut->p4.p = 0;
1a1ff 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 35 20 . pOut->p5
1a200 3d 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 = 0;.#ifdef SQLI
1a201 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 70 TE_DEBUG. p
1a202 4f 75 74 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d 20 Out->zComment =
1a203 30 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 0;. if( sql
1a204 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 ite3VdbeAddopTra
1a205 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 ce ){. sq
1a206 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 lite3VdbePrintOp
1a207 28 30 2c 20 69 2b 61 64 64 72 2c 20 26 70 2d 3e (0, i+addr, &p->
1a208 61 4f 70 5b 69 2b 61 64 64 72 5d 29 3b 0a 20 20 aOp[i+addr]);.
1a209 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 }.#endif.
1a20a 20 7d 0a 20 20 20 20 70 2d 3e 6e 4f 70 20 2b 3d }. p->nOp +=
1a20b 20 6e 4f 70 3b 0a 20 20 7d 0a 20 20 72 65 74 75 nOp;. }. retu
1a20c 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a rn addr;.}../*.*
1a20d 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c * Change the val
1a20e 75 65 20 6f 66 20 74 68 65 20 50 31 20 6f 70 65 ue of the P1 ope
1a20f 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 rand for a speci
1a210 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e fic instruction.
1a211 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1a212 20 69 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20 is useful when
1a213 61 20 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20 a large program
1a214 69 73 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 is loaded from a
1a215 0a 2a 2a 20 73 74 61 74 69 63 20 61 72 72 61 79 .** static array
1a216 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 using sqlite3Vd
1a217 62 65 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20 beAddOpList but
1a218 77 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 we want to make
1a219 61 0a 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63 a.** few minor c
1a21a 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72 hanges to the pr
1a21b 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ogram..*/.SQLITE
1a21c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1a21d 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
1a21e 31 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 1(Vdbe *p, int a
1a21f 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 ddr, int val){.
1a220 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b assert( p!=0 );
1a221 0a 20 20 61 73 73 65 72 74 28 20 61 64 64 72 3e . assert( addr>
1a222 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e =0 );. if( p->n
1a223 4f 70 3e 61 64 64 72 20 29 7b 0a 20 20 20 20 70 Op>addr ){. p
1a224 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 31 20 3d ->aOp[addr].p1 =
1a225 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a val;. }.}../*.
1a226 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 ** Change the va
1a227 6c 75 65 20 6f 66 20 74 68 65 20 50 32 20 6f 70 lue of the P2 op
1a228 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 erand for a spec
1a229 69 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e ific instruction
1a22a 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e ..** This routin
1a22b 65 20 69 73 20 75 73 65 66 75 6c 20 66 6f 72 20 e is useful for
1a22c 73 65 74 74 69 6e 67 20 61 20 6a 75 6d 70 20 64 setting a jump d
1a22d 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 estination..*/.S
1a22e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1a22f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 id sqlite3VdbeCh
1a230 61 6e 67 65 50 32 28 56 64 62 65 20 2a 70 2c 20 angeP2(Vdbe *p,
1a231 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 76 61 int addr, int va
1a232 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 21 l){. assert( p!
1a233 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1a234 61 64 64 72 3e 3d 30 20 29 3b 0a 20 20 69 66 28 addr>=0 );. if(
1a235 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 29 7b 0a p->nOp>addr ){.
1a236 20 20 20 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d p->aOp[addr]
1a237 2e 70 32 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d .p2 = val;. }.}
1a238 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
1a239 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
1a23a 50 33 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 P3 operand for a
1a23b 20 73 70 65 63 69 66 69 63 20 69 6e 73 74 72 75 specific instru
1a23c 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ction..*/.SQLITE
1a23d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1a23e 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
1a23f 33 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 3(Vdbe *p, int a
1a240 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 ddr, int val){.
1a241 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b assert( p!=0 );
1a242 0a 20 20 61 73 73 65 72 74 28 20 61 64 64 72 3e . assert( addr>
1a243 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e =0 );. if( p->n
1a244 4f 70 3e 61 64 64 72 20 29 7b 0a 20 20 20 20 70 Op>addr ){. p
1a245 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 33 20 3d ->aOp[addr].p3 =
1a246 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a val;. }.}../*.
1a247 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 ** Change the va
1a248 6c 75 65 20 6f 66 20 74 68 65 20 50 35 20 6f 70 lue of the P5 op
1a249 65 72 61 6e 64 20 66 6f 72 20 74 68 65 20 6d 6f erand for the mo
1a24a 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 61 st recently.** a
1a24b 64 64 65 64 20 6f 70 65 72 61 74 69 6f 6e 2e 0a dded operation..
1a24c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1a24d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1a24e 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 20 beChangeP5(Vdbe
1a24f 2a 70 2c 20 75 38 20 76 61 6c 29 7b 0a 20 20 61 *p, u8 val){. a
1a250 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 ssert( p!=0 );.
1a251 20 69 66 28 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 if( p->aOp ){.
1a252 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f assert( p->nO
1a253 70 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e 61 4f p>0 );. p->aO
1a254 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 35 20 3d p[p->nOp-1].p5 =
1a255 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a val;. }.}../*.
1a256 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 50 32 ** Change the P2
1a257 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e 73 74 operand of inst
1a258 72 75 63 74 69 6f 6e 20 61 64 64 72 20 73 6f 20 ruction addr so
1a259 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 that it points t
1a25a 6f 0a 2a 2a 20 74 68 65 20 61 64 64 72 65 73 73 o.** the address
1a25b 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 of the next ins
1a25c 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 truction to be c
1a25d 6f 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f oded..*/.SQLITE_
1a25e 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1a25f 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
1a260 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 (Vdbe *p, int ad
1a261 64 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 dr){. sqlite3Vd
1a262 62 65 43 68 61 6e 67 65 50 32 28 70 2c 20 61 64 beChangeP2(p, ad
1a263 64 72 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 7d 0a 0a dr, p->nOp);.}..
1a264 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e ./*.** If the in
1a265 70 75 74 20 46 75 6e 63 44 65 66 20 73 74 72 75 put FuncDef stru
1a266 63 74 75 72 65 20 69 73 20 65 70 68 65 6d 65 72 cture is ephemer
1a267 61 6c 2c 20 74 68 65 6e 20 66 72 65 65 20 69 74 al, then free it
1a268 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 46 75 6e . If.** the Fun
1a269 63 44 65 66 20 69 73 20 6e 6f 74 20 65 70 68 65 cDef is not ephe
1a26a 72 6d 61 6c 2c 20 74 68 65 6e 20 64 6f 20 6e 6f rmal, then do no
1a26b 74 68 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 thing..*/.static
1a26c 20 76 6f 69 64 20 66 72 65 65 45 70 68 65 6d 65 void freeEpheme
1a26d 72 61 6c 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 ralFunction(sqli
1a26e 74 65 33 20 2a 64 62 2c 20 46 75 6e 63 44 65 66 te3 *db, FuncDef
1a26f 20 2a 70 44 65 66 29 7b 0a 20 20 69 66 28 20 41 *pDef){. if( A
1a270 4c 57 41 59 53 28 70 44 65 66 29 20 26 26 20 28 LWAYS(pDef) && (
1a271 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 pDef->flags & SQ
1a272 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 29 LITE_FUNC_EPHEM)
1a273 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 !=0 ){. sqlit
1a274 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 65 e3DbFree(db, pDe
1a275 66 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a f);. }.}../*.**
1a276 20 44 65 6c 65 74 65 20 61 20 50 34 20 76 61 6c Delete a P4 val
1a277 75 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e ue if necessary.
1a278 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1a279 66 72 65 65 50 34 28 73 71 6c 69 74 65 33 20 2a freeP4(sqlite3 *
1a27a 64 62 2c 20 69 6e 74 20 70 34 74 79 70 65 2c 20 db, int p4type,
1a27b 76 6f 69 64 20 2a 70 34 29 7b 0a 20 20 69 66 28 void *p4){. if(
1a27c 20 70 34 20 29 7b 0a 20 20 20 20 73 77 69 74 63 p4 ){. switc
1a27d 68 28 20 70 34 74 79 70 65 20 29 7b 0a 20 20 20 h( p4type ){.
1a27e 20 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a case P4_REAL:
1a27f 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 49 . case P4_I
1a280 4e 54 36 34 3a 0a 20 20 20 20 20 20 63 61 73 65 NT64:. case
1a281 20 50 34 5f 4d 50 52 49 4e 54 46 3a 0a 20 20 20 P4_MPRINTF:.
1a282 20 20 20 63 61 73 65 20 50 34 5f 44 59 4e 41 4d case P4_DYNAM
1a283 49 43 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 IC:. case P
1a284 34 5f 4b 45 59 49 4e 46 4f 3a 0a 20 20 20 20 20 4_KEYINFO:.
1a285 20 63 61 73 65 20 50 34 5f 49 4e 54 41 52 52 41 case P4_INTARRA
1a286 59 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 Y:. case P4
1a287 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 _KEYINFO_HANDOFF
1a288 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 : {. sqli
1a289 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 34 te3DbFree(db, p4
1a28a 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b );. break
1a28b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1a28c 63 61 73 65 20 50 34 5f 56 44 42 45 46 55 4e 43 case P4_VDBEFUNC
1a28d 3a 20 7b 0a 20 20 20 20 20 20 20 20 56 64 62 65 : {. Vdbe
1a28e 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 20 Func *pVdbeFunc
1a28f 3d 20 28 56 64 62 65 46 75 6e 63 20 2a 29 70 34 = (VdbeFunc *)p4
1a290 3b 0a 20 20 20 20 20 20 20 20 66 72 65 65 45 70 ;. freeEp
1a291 68 65 6d 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 hemeralFunction(
1a292 64 62 2c 20 70 56 64 62 65 46 75 6e 63 2d 3e 70 db, pVdbeFunc->p
1a293 46 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 73 Func);. s
1a294 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 qlite3VdbeDelete
1a295 41 75 78 44 61 74 61 28 70 56 64 62 65 46 75 6e AuxData(pVdbeFun
1a296 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 c, 0);. s
1a297 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1a298 20 70 56 64 62 65 46 75 6e 63 29 3b 0a 20 20 20 pVdbeFunc);.
1a299 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1a29a 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 }. case P
1a29b 34 5f 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20 20 4_FUNCDEF: {.
1a29c 20 20 20 20 20 66 72 65 65 45 70 68 65 6d 65 72 freeEphemer
1a29d 61 6c 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 28 alFunction(db, (
1a29e 46 75 6e 63 44 65 66 2a 29 70 34 29 3b 0a 20 20 FuncDef*)p4);.
1a29f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1a2a0 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 }. case
1a2a1 50 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 P4_MEM: {.
1a2a2 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 sqlite3ValueFr
1a2a3 65 65 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 ee((sqlite3_valu
1a2a4 65 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 e*)p4);.
1a2a5 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
1a2a6 20 20 20 20 20 63 61 73 65 20 50 34 5f 56 54 41 case P4_VTA
1a2a7 42 20 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 B : {. sq
1a2a8 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 lite3VtabUnlock(
1a2a9 28 56 54 61 62 6c 65 20 2a 29 70 34 29 3b 0a 20 (VTable *)p4);.
1a2aa 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1a2ab 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 }. case
1a2ac 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 3a P4_SUBPROGRAM :
1a2ad 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 {. sqlit
1a2ae 65 33 56 64 62 65 50 72 6f 67 72 61 6d 44 65 6c e3VdbeProgramDel
1a2af 65 74 65 28 64 62 2c 20 28 53 75 62 50 72 6f 67 ete(db, (SubProg
1a2b0 72 61 6d 20 2a 29 70 34 2c 20 31 29 3b 0a 20 20 ram *)p4, 1);.
1a2b1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1a2b2 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d }. }. }.}
1a2b3 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 ../*.** Free the
1a2b4 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 space allocated
1a2b5 20 66 6f 72 20 61 4f 70 20 61 6e 64 20 61 6e 79 for aOp and any
1a2b6 20 70 34 20 76 61 6c 75 65 73 20 61 6c 6c 6f 63 p4 values alloc
1a2b7 61 74 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 ated for the.**
1a2b8 6f 70 63 6f 64 65 73 20 63 6f 6e 74 61 69 6e 65 opcodes containe
1a2b9 64 20 77 69 74 68 69 6e 2e 20 49 66 20 61 4f 70 d within. If aOp
1a2ba 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 69 74 20 is not NULL it
1a2bb 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 63 6f is assumed to co
1a2bc 6e 74 61 69 6e 20 0a 2a 2a 20 6e 4f 70 20 65 6e ntain .** nOp en
1a2bd 74 72 69 65 73 2e 20 0a 2a 2f 0a 73 74 61 74 69 tries. .*/.stati
1a2be 63 20 76 6f 69 64 20 76 64 62 65 46 72 65 65 4f c void vdbeFreeO
1a2bf 70 41 72 72 61 79 28 73 71 6c 69 74 65 33 20 2a pArray(sqlite3 *
1a2c0 64 62 2c 20 4f 70 20 2a 61 4f 70 2c 20 69 6e 74 db, Op *aOp, int
1a2c1 20 6e 4f 70 29 7b 0a 20 20 69 66 28 20 61 4f 70 nOp){. if( aOp
1a2c2 20 29 7b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 3b ){. Op *pOp;
1a2c3 0a 20 20 20 20 66 6f 72 28 70 4f 70 3d 61 4f 70 . for(pOp=aOp
1a2c4 3b 20 70 4f 70 3c 26 61 4f 70 5b 6e 4f 70 5d 3b ; pOp<&aOp[nOp];
1a2c5 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 66 pOp++){. f
1a2c6 72 65 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70 reeP4(db, pOp->p
1a2c7 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 4type, pOp->p4.p
1a2c8 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 );.#ifdef SQLITE
1a2c9 5f 44 45 42 55 47 0a 20 20 20 20 20 20 73 71 6c _DEBUG. sql
1a2ca 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1a2cb 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 29 3b 0a 23 Op->zComment);.#
1a2cc 65 6e 64 69 66 20 20 20 20 20 0a 20 20 20 20 7d endif . }
1a2cd 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 . }. sqlite3Db
1a2ce 46 72 65 65 28 64 62 2c 20 61 4f 70 29 3b 0a 7d Free(db, aOp);.}
1a2cf 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e ../*.** Decremen
1a2d0 74 20 74 68 65 20 72 65 66 2d 63 6f 75 6e 74 20 t the ref-count
1a2d1 6f 6e 20 74 68 65 20 53 75 62 50 72 6f 67 72 61 on the SubProgra
1a2d2 6d 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 m structure pass
1a2d3 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 73 65 63 ed as the.** sec
1a2d4 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 ond argument. If
1a2d5 20 74 68 65 20 72 65 66 2d 63 6f 75 6e 74 20 72 the ref-count r
1a2d6 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 66 72 65 eaches zero, fre
1a2d7 65 20 74 68 65 20 73 74 72 75 63 74 75 72 65 2e e the structure.
1a2d8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 72 61 79 .**.** The array
1a2d9 20 6f 66 20 56 44 42 45 20 6f 70 63 6f 64 65 73 of VDBE opcodes
1a2da 20 73 74 6f 72 65 64 20 61 73 20 53 75 62 50 72 stored as SubPr
1a2db 6f 67 72 61 6d 2e 61 4f 70 20 69 73 20 66 72 65 ogram.aOp is fre
1a2dc 65 64 20 69 66 0a 2a 2a 20 65 69 74 68 65 72 20 ed if.** either
1a2dd 74 68 65 20 72 65 66 2d 63 6f 75 6e 74 20 72 65 the ref-count re
1a2de 61 63 68 65 73 20 7a 65 72 6f 20 6f 72 20 70 61 aches zero or pa
1a2df 72 61 6d 65 74 65 72 20 66 72 65 65 6f 70 20 69 rameter freeop i
1a2e0 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a s non-zero..**.*
1a2e1 2a 20 53 69 6e 63 65 20 74 68 65 20 61 72 72 61 * Since the arra
1a2e2 79 20 6f 66 20 6f 70 63 6f 64 65 73 20 70 6f 69 y of opcodes poi
1a2e3 6e 74 65 64 20 74 6f 20 62 79 20 53 75 62 50 72 nted to by SubPr
1a2e4 6f 67 72 61 6d 2e 61 4f 70 20 6d 61 79 20 64 69 ogram.aOp may di
1a2e5 72 65 63 74 6c 79 0a 2a 2a 20 6f 72 20 69 6e 64 rectly.** or ind
1a2e6 69 72 65 63 74 6c 79 20 63 6f 6e 74 61 69 6e 20 irectly contain
1a2e7 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 a reference to t
1a2e8 68 65 20 53 75 62 50 72 6f 67 72 61 6d 20 73 74 he SubProgram st
1a2e9 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a ructure itself..
1a2ea 2a 2a 20 42 79 20 70 61 73 73 69 6e 67 20 61 20 ** By passing a
1a2eb 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65 65 6f 70 20 non-zero freeop
1a2ec 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 20 63 parameter, the c
1a2ed 61 6c 6c 65 72 20 6d 61 79 20 65 6e 73 75 72 65 aller may ensure
1a2ee 20 74 68 61 74 20 61 6c 6c 0a 2a 2a 20 53 75 62 that all.** Sub
1a2ef 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74 75 72 Program structur
1a2f0 65 73 20 61 6e 64 20 74 68 65 69 72 20 61 4f 70 es and their aOp
1a2f1 20 61 72 72 61 79 73 20 61 72 65 20 66 72 65 65 arrays are free
1a2f2 64 2c 20 65 76 65 6e 20 77 68 65 6e 20 74 68 65 d, even when the
1a2f3 72 65 0a 2a 2a 20 61 72 65 20 73 75 63 68 20 63 re.** are such c
1a2f4 69 72 63 75 6c 61 72 20 72 65 66 65 72 65 6e 63 ircular referenc
1a2f5 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 es..*/.SQLITE_PR
1a2f6 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1a2f7 65 33 56 64 62 65 50 72 6f 67 72 61 6d 44 65 6c e3VdbeProgramDel
1a2f8 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ete(sqlite3 *db,
1a2f9 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 70 2c 20 SubProgram *p,
1a2fa 69 6e 74 20 66 72 65 65 6f 70 29 7b 0a 20 20 69 int freeop){. i
1a2fb 66 28 20 70 20 29 7b 0a 20 20 20 20 61 73 73 65 f( p ){. asse
1a2fc 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b rt( p->nRef>0 );
1a2fd 0a 20 20 20 20 69 66 28 20 66 72 65 65 6f 70 20 . if( freeop
1a2fe 7c 7c 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 7b || p->nRef==1 ){
1a2ff 0a 20 20 20 20 20 20 4f 70 20 2a 61 4f 70 20 3d . Op *aOp =
1a300 20 70 2d 3e 61 4f 70 3b 0a 20 20 20 20 20 20 70 p->aOp;. p
1a301 2d 3e 61 4f 70 20 3d 20 30 3b 0a 20 20 20 20 20 ->aOp = 0;.
1a302 20 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79 vdbeFreeOpArray
1a303 28 64 62 2c 20 61 4f 70 2c 20 70 2d 3e 6e 4f 70 (db, aOp, p->nOp
1a304 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 4f 70 20 );. p->nOp
1a305 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 = 0;. }. p
1a306 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 ->nRef--;. if
1a307 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a ( p->nRef==0 ){.
1a308 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1a309 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 ree(db, p);.
1a30a 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 }. }.}.../*.**
1a30b 43 68 61 6e 67 65 20 4e 20 6f 70 63 6f 64 65 73 Change N opcodes
1a30c 20 73 74 61 72 74 69 6e 67 20 61 74 20 61 64 64 starting at add
1a30d 72 20 74 6f 20 4e 6f 2d 6f 70 73 2e 0a 2a 2f 0a r to No-ops..*/.
1a30e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1a30f 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 oid sqlite3VdbeC
1a310 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 56 64 62 65 hangeToNoop(Vdbe
1a311 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 *p, int addr, i
1a312 6e 74 20 4e 29 7b 0a 20 20 69 66 28 20 70 2d 3e nt N){. if( p->
1a313 61 4f 70 20 29 7b 0a 20 20 20 20 56 64 62 65 4f aOp ){. VdbeO
1a314 70 20 2a 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 p *pOp = &p->aOp
1a315 5b 61 64 64 72 5d 3b 0a 20 20 20 20 73 71 6c 69 [addr];. sqli
1a316 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b te3 *db = p->db;
1a317 0a 20 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 . while( N--
1a318 29 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 28 ){. freeP4(
1a319 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c db, pOp->p4type,
1a31a 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 20 pOp->p4.p);.
1a31b 20 20 20 6d 65 6d 73 65 74 28 70 4f 70 2c 20 30 memset(pOp, 0
1a31c 2c 20 73 69 7a 65 6f 66 28 70 4f 70 5b 30 5d 29 , sizeof(pOp[0])
1a31d 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 );. pOp->op
1a31e 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a code = OP_Noop;.
1a31f 20 20 20 20 20 20 70 4f 70 2b 2b 3b 0a 20 20 20 pOp++;.
1a320 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
1a321 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 Change the value
1a322 20 6f 66 20 74 68 65 20 50 34 20 6f 70 65 72 61 of the P4 opera
1a323 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 nd for a specifi
1a324 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a c instruction..*
1a325 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
1a326 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20 61 20 s useful when a
1a327 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20 69 73 large program is
1a328 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a 2a loaded from a.*
1a329 2a 20 73 74 61 74 69 63 20 61 72 72 61 79 20 75 * static array u
1a32a 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62 65 sing sqlite3Vdbe
1a32b 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20 77 65 AddOpList but we
1a32c 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61 0a want to make a.
1a32d 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63 68 61 ** few minor cha
1a32e 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72 6f 67 nges to the prog
1a32f 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3e ram..**.** If n>
1a330 3d 30 20 74 68 65 6e 20 74 68 65 20 50 34 20 6f =0 then the P4 o
1a331 70 65 72 61 6e 64 20 69 73 20 64 79 6e 61 6d 69 perand is dynami
1a332 63 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 c, meaning that
1a333 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 a copy of.** the
1a334 20 73 74 72 69 6e 67 20 69 73 20 6d 61 64 65 20 string is made
1a335 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 into memory obta
1a336 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
1a337 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 41 3_malloc()..** A
1a338 20 76 61 6c 75 65 20 6f 66 20 6e 3d 3d 30 20 6d value of n==0 m
1a339 65 61 6e 73 20 63 6f 70 79 20 62 79 74 65 73 20 eans copy bytes
1a33a 6f 66 20 7a 50 34 20 75 70 20 74 6f 20 61 6e 64 of zP4 up to and
1a33b 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 0a 2a including the.*
1a33c 2a 20 66 69 72 73 74 20 6e 75 6c 6c 20 62 79 74 * first null byt
1a33d 65 2e 20 20 49 66 20 6e 3e 30 20 74 68 65 6e 20 e. If n>0 then
1a33e 63 6f 70 79 20 6e 2b 31 20 62 79 74 65 73 20 6f copy n+1 bytes o
1a33f 66 20 7a 50 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 f zP4..**.** If
1a340 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 69 74 n==P4_KEYINFO it
1a341 20 6d 65 61 6e 73 20 74 68 61 74 20 7a 50 34 20 means that zP4
1a342 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
1a343 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 a KeyInfo struct
1a344 75 72 65 2e 0a 2a 2a 20 41 20 63 6f 70 79 20 69 ure..** A copy i
1a345 73 20 6d 61 64 65 20 6f 66 20 74 68 65 20 4b 65 s made of the Ke
1a346 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 yInfo structure
1a347 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 into memory obta
1a348 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c ined from.** sql
1a349 69 74 65 33 5f 6d 61 6c 6c 6f 63 2c 20 74 6f 20 ite3_malloc, to
1a34a 62 65 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 be freed when th
1a34b 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 e Vdbe is finali
1a34c 7a 65 64 2e 0a 2a 2a 20 6e 3d 3d 50 34 5f 4b 45 zed..** n==P4_KE
1a34d 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 69 6e YINFO_HANDOFF in
1a34e 64 69 63 61 74 65 73 20 74 68 61 74 20 7a 50 34 dicates that zP4
1a34f 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65 79 points to a Key
1a350 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a Info structure.*
1a351 2a 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f * stored in memo
1a352 72 79 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c ry that the call
1a353 65 72 20 68 61 73 20 6f 62 74 61 69 6e 65 64 20 er has obtained
1a354 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c from sqlite3_mal
1a355 6c 6f 63 2e 20 54 68 65 20 0a 2a 2a 20 63 61 6c loc. The .** cal
1a356 6c 65 72 20 73 68 6f 75 6c 64 20 6e 6f 74 20 66 ler should not f
1a357 72 65 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 ree the allocati
1a358 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 66 on, it will be f
1a359 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 reed when the Vd
1a35a 62 65 20 69 73 0a 2a 2a 20 66 69 6e 61 6c 69 7a be is.** finaliz
1a35b 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 4f 74 68 65 72 ed..** .** Other
1a35c 20 76 61 6c 75 65 73 20 6f 66 20 6e 20 28 50 34 values of n (P4
1a35d 5f 53 54 41 54 49 43 2c 20 50 34 5f 43 4f 4c 4c _STATIC, P4_COLL
1a35e 53 45 51 20 65 74 63 2e 29 20 69 6e 64 69 63 61 SEQ etc.) indica
1a35f 74 65 20 74 68 61 74 20 7a 50 34 20 70 6f 69 6e te that zP4 poin
1a360 74 73 0a 2a 2a 20 74 6f 20 61 20 73 74 72 69 6e ts.** to a strin
1a361 67 20 6f 72 20 73 74 72 75 63 74 75 72 65 20 74 g or structure t
1a362 68 61 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 hat is guarantee
1a363 64 20 74 6f 20 65 78 69 73 74 20 66 6f 72 20 74 d to exist for t
1a364 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a he lifetime of.*
1a365 2a 20 74 68 65 20 56 64 62 65 2e 20 49 6e 20 74 * the Vdbe. In t
1a366 68 65 73 65 20 63 61 73 65 73 20 77 65 20 63 61 hese cases we ca
1a367 6e 20 6a 75 73 74 20 63 6f 70 79 20 74 68 65 20 n just copy the
1a368 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 pointer..**.** I
1a369 66 20 61 64 64 72 3c 30 20 74 68 65 6e 20 63 68 f addr<0 then ch
1a36a 61 6e 67 65 20 50 34 20 6f 6e 20 74 68 65 20 6d ange P4 on the m
1a36b 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 ost recently ins
1a36c 65 72 74 65 64 20 69 6e 73 74 72 75 63 74 69 6f erted instructio
1a36d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
1a36e 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1a36f 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 56 64 3VdbeChangeP4(Vd
1a370 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c be *p, int addr,
1a371 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 const char *zP4
1a372 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 4f 70 20 2a , int n){. Op *
1a373 70 4f 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a pOp;. sqlite3 *
1a374 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 21 db;. assert( p!
1a375 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 70 2d 3e =0 );. db = p->
1a376 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d db;. assert( p-
1a377 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 >magic==VDBE_MAG
1a378 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 IC_INIT );. if(
1a379 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 64 62 p->aOp==0 || db
1a37a 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1a37b 7b 0a 20 20 20 20 69 66 20 28 20 6e 21 3d 50 34 {. if ( n!=P4
1a37c 5f 4b 45 59 49 4e 46 4f 20 26 26 20 6e 21 3d 50 _KEYINFO && n!=P
1a37d 34 5f 56 54 41 42 20 29 20 7b 0a 20 20 20 20 20 4_VTAB ) {.
1a37e 20 66 72 65 65 50 34 28 64 62 2c 20 6e 2c 20 28 freeP4(db, n, (
1a37f 76 6f 69 64 2a 29 2a 28 63 68 61 72 2a 2a 29 26 void*)*(char**)&
1a380 7a 50 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 zP4);. }.
1a381 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 return;. }. as
1a382 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 sert( p->nOp>0 )
1a383 3b 0a 20 20 61 73 73 65 72 74 28 20 61 64 64 72 ;. assert( addr
1a384 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 28 <p->nOp );. if(
1a385 20 61 64 64 72 3c 30 20 29 7b 0a 20 20 20 20 61 addr<0 ){. a
1a386 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31 ddr = p->nOp - 1
1a387 3b 0a 20 20 7d 0a 20 20 70 4f 70 20 3d 20 26 70 ;. }. pOp = &p
1a388 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 66 ->aOp[addr];. f
1a389 72 65 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70 reeP4(db, pOp->p
1a38a 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 4type, pOp->p4.p
1a38b 29 3b 0a 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d );. pOp->p4.p =
1a38c 20 30 3b 0a 20 20 69 66 28 20 6e 3d 3d 50 34 5f 0;. if( n==P4_
1a38d 49 4e 54 33 32 20 29 7b 0a 20 20 20 20 2f 2a 20 INT32 ){. /*
1a38e 4e 6f 74 65 3a 20 74 68 69 73 20 63 61 73 74 20 Note: this cast
1a38f 69 73 20 73 61 66 65 2c 20 62 65 63 61 75 73 65 is safe, because
1a390 20 74 68 65 20 6f 72 69 67 69 6e 20 64 61 74 61 the origin data
1a391 20 70 6f 69 6e 74 20 77 61 73 20 61 6e 20 69 6e point was an in
1a392 74 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 77 61 t. ** that wa
1a393 73 20 63 61 73 74 20 74 6f 20 61 20 28 63 6f 6e s cast to a (con
1a394 73 74 20 63 68 61 72 20 2a 29 2e 20 2a 2f 0a 20 st char *). */.
1a395 20 20 20 70 4f 70 2d 3e 70 34 2e 69 20 3d 20 53 pOp->p4.i = S
1a396 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 QLITE_PTR_TO_INT
1a397 28 7a 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e (zP4);. pOp->
1a398 70 34 74 79 70 65 20 3d 20 50 34 5f 49 4e 54 33 p4type = P4_INT3
1a399 32 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 2;. }else if( z
1a39a 50 34 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70 P4==0 ){. pOp
1a39b 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 ->p4.p = 0;.
1a39c 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 pOp->p4type = P4
1a39d 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 7d 65 6c 73 _NOTUSED;. }els
1a39e 65 20 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 e if( n==P4_KEYI
1a39f 4e 46 4f 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e NFO ){. KeyIn
1a3a0 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 fo *pKeyInfo;.
1a3a1 20 20 69 6e 74 20 6e 46 69 65 6c 64 2c 20 6e 42 int nField, nB
1a3a2 79 74 65 3b 0a 0a 20 20 20 20 6e 46 69 65 6c 64 yte;.. nField
1a3a3 20 3d 20 28 28 4b 65 79 49 6e 66 6f 2a 29 7a 50 = ((KeyInfo*)zP
1a3a4 34 29 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 20 4)->nField;.
1a3a5 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a nByte = sizeof(*
1a3a6 70 4b 65 79 49 6e 66 6f 29 20 2b 20 28 6e 46 69 pKeyInfo) + (nFi
1a3a7 65 6c 64 2d 31 29 2a 73 69 7a 65 6f 66 28 70 4b eld-1)*sizeof(pK
1a3a8 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 30 5d eyInfo->aColl[0]
1a3a9 29 20 2b 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 ) + nField;.
1a3aa 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74 pKeyInfo = sqlit
1a3ab 65 33 4d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 20 e3Malloc( nByte
1a3ac 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 );. pOp->p4.p
1a3ad 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e KeyInfo = pKeyIn
1a3ae 66 6f 3b 0a 20 20 20 20 69 66 28 20 70 4b 65 79 fo;. if( pKey
1a3af 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 75 38 Info ){. u8
1a3b0 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 *aSortOrder;.
1a3b1 20 20 20 20 6d 65 6d 63 70 79 28 70 4b 65 79 49 memcpy(pKeyI
1a3b2 6e 66 6f 2c 20 7a 50 34 2c 20 6e 42 79 74 65 29 nfo, zP4, nByte)
1a3b3 3b 0a 20 20 20 20 20 20 61 53 6f 72 74 4f 72 64 ;. aSortOrd
1a3b4 65 72 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 er = pKeyInfo->a
1a3b5 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 SortOrder;.
1a3b6 20 69 66 28 20 61 53 6f 72 74 4f 72 64 65 72 20 if( aSortOrder
1a3b7 29 7b 0a 20 20 20 20 20 20 20 20 70 4b 65 79 49 ){. pKeyI
1a3b8 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 nfo->aSortOrder
1a3b9 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 = (unsigned char
1a3ba 2a 29 26 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f *)&pKeyInfo->aCo
1a3bb 6c 6c 5b 6e 46 69 65 6c 64 5d 3b 0a 20 20 20 20 ll[nField];.
1a3bc 20 20 20 20 6d 65 6d 63 70 79 28 70 4b 65 79 49 memcpy(pKeyI
1a3bd 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 2c nfo->aSortOrder,
1a3be 20 61 53 6f 72 74 4f 72 64 65 72 2c 20 6e 46 69 aSortOrder, nFi
1a3bf 65 6c 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 eld);. }.
1a3c0 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 pOp->p4type
1a3c1 3d 20 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 = P4_KEYINFO;.
1a3c2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
1a3c3 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
1a3c4 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f ed = 1;. pO
1a3c5 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e p->p4type = P4_N
1a3c6 4f 54 55 53 45 44 3b 0a 20 20 20 20 7d 0a 20 20 OTUSED;. }.
1a3c7 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 34 5f }else if( n==P4_
1a3c8 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 KEYINFO_HANDOFF
1a3c9 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 ){. pOp->p4.p
1a3ca 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 = (void*)zP4;.
1a3cb 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d pOp->p4type =
1a3cc 20 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 7d P4_KEYINFO;. }
1a3cd 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 34 5f 56 else if( n==P4_V
1a3ce 54 41 42 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e TAB ){. pOp->
1a3cf 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 p4.p = (void*)zP
1a3d0 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 4;. pOp->p4ty
1a3d1 70 65 20 3d 20 50 34 5f 56 54 41 42 3b 0a 20 20 pe = P4_VTAB;.
1a3d2 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 sqlite3VtabLoc
1a3d3 6b 28 28 56 54 61 62 6c 65 20 2a 29 7a 50 34 29 k((VTable *)zP4)
1a3d4 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 28 ;. assert( ((
1a3d5 56 54 61 62 6c 65 20 2a 29 7a 50 34 29 2d 3e 64 VTable *)zP4)->d
1a3d6 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 7d 65 b==p->db );. }e
1a3d7 6c 73 65 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20 lse if( n<0 ){.
1a3d8 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 pOp->p4.p = (
1a3d9 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70 void*)zP4;. p
1a3da 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 28 73 69 Op->p4type = (si
1a3db 67 6e 65 64 20 63 68 61 72 29 6e 3b 0a 20 20 7d gned char)n;. }
1a3dc 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 6e 3d else{. if( n=
1a3dd 3d 30 20 29 20 6e 20 3d 20 73 71 6c 69 74 65 33 =0 ) n = sqlite3
1a3de 53 74 72 6c 65 6e 33 30 28 7a 50 34 29 3b 0a 20 Strlen30(zP4);.
1a3df 20 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 73 pOp->p4.z = s
1a3e0 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 qlite3DbStrNDup(
1a3e1 70 2d 3e 64 62 2c 20 7a 50 34 2c 20 6e 29 3b 0a p->db, zP4, n);.
1a3e2 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 pOp->p4type
1a3e3 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 = P4_DYNAMIC;.
1a3e4 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 }.}..#ifndef NDE
1a3e5 42 55 47 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 BUG./*.** Change
1a3e6 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 the comment on
1a3e7 74 68 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 the the most rec
1a3e8 65 6e 74 6c 79 20 63 6f 64 65 64 20 69 6e 73 74 ently coded inst
1a3e9 72 75 63 74 69 6f 6e 2e 20 20 4f 72 0a 2a 2a 20 ruction. Or.**
1a3ea 69 6e 73 65 72 74 20 61 20 4e 6f 2d 6f 70 20 61 insert a No-op a
1a3eb 6e 64 20 61 64 64 20 74 68 65 20 63 6f 6d 6d 65 nd add the comme
1a3ec 6e 74 20 74 6f 20 74 68 61 74 20 6e 65 77 20 69 nt to that new i
1a3ed 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 54 68 69 nstruction. Thi
1a3ee 73 0a 2a 2a 20 6d 61 6b 65 73 20 74 68 65 20 63 s.** makes the c
1a3ef 6f 64 65 20 65 61 73 69 65 72 20 74 6f 20 72 65 ode easier to re
1a3f0 61 64 20 64 75 72 69 6e 67 20 64 65 62 75 67 67 ad during debugg
1a3f1 69 6e 67 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 ing. None of th
1a3f2 69 73 20 68 61 70 70 65 6e 73 0a 2a 2a 20 69 6e is happens.** in
1a3f3 20 61 20 70 72 6f 64 75 63 74 69 6f 6e 20 62 75 a production bu
1a3f4 69 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ild..*/.SQLITE_P
1a3f5 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1a3f6 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 te3VdbeComment(V
1a3f7 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 dbe *p, const ch
1a3f8 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e ar *zFormat, ...
1a3f9 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b ){. va_list ap;
1a3fa 0a 20 20 69 66 28 20 21 70 20 29 20 72 65 74 75 . if( !p ) retu
1a3fb 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d rn;. assert( p-
1a3fc 3e 6e 4f 70 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70 >nOp>0 || p->aOp
1a3fd 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
1a3fe 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d p->aOp==0 || p-
1a3ff 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a >aOp[p->nOp-1].z
1a400 43 6f 6d 6d 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d Comment==0 || p-
1a401 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
1a402 64 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f d );. if( p->nO
1a403 70 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a p ){. char **
1a404 70 7a 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e pz = &p->aOp[p->
1a405 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b nOp-1].zComment;
1a406 0a 20 20 20 20 76 61 5f 73 74 61 72 74 28 61 70 . va_start(ap
1a407 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 , zFormat);.
1a408 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d sqlite3DbFree(p-
1a409 3e 64 62 2c 20 2a 70 7a 29 3b 0a 20 20 20 20 2a >db, *pz);. *
1a40a 70 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 pz = sqlite3VMPr
1a40b 69 6e 74 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 intf(p->db, zFor
1a40c 6d 61 74 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 mat, ap);. va
1a40d 5f 65 6e 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a _end(ap);. }.}.
1a40e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1a40f 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4e oid sqlite3VdbeN
1a410 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20 oopComment(Vdbe
1a411 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *p, const char *
1a412 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 zFormat, ...){.
1a413 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 va_list ap;. i
1a414 66 28 20 21 70 20 29 20 72 65 74 75 72 6e 3b 0a f( !p ) return;.
1a415 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1a416 4f 70 30 28 70 2c 20 4f 50 5f 4e 6f 6f 70 29 3b Op0(p, OP_Noop);
1a417 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f . assert( p->nO
1a418 70 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 p>0 || p->aOp==0
1a419 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
1a41a 3e 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f >aOp==0 || p->aO
1a41b 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d p[p->nOp-1].zCom
1a41c 6d 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 ment==0 || p->db
1a41d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1a41e 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 20 29 ;. if( p->nOp )
1a41f 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 70 7a 20 {. char **pz
1a420 3d 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 = &p->aOp[p->nOp
1a421 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 -1].zComment;.
1a422 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a va_start(ap, z
1a423 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c Format);. sql
1a424 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 ite3DbFree(p->db
1a425 2c 20 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 , *pz);. *pz
1a426 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 = sqlite3VMPrint
1a427 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 f(p->db, zFormat
1a428 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e , ap);. va_en
1a429 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e d(ap);. }.}.#en
1a42a 64 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a dif /* NDEBUG *
1a42b 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 /../*.** Return
1a42c 74 68 65 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 the opcode for a
1a42d 20 67 69 76 65 6e 20 61 64 64 72 65 73 73 2e 20 given address.
1a42e 20 49 66 20 74 68 65 20 61 64 64 72 65 73 73 20 If the address
1a42f 69 73 20 2d 31 2c 20 74 68 65 6e 0a 2a 2a 20 72 is -1, then.** r
1a430 65 74 75 72 6e 20 74 68 65 20 6d 6f 73 74 20 72 eturn the most r
1a431 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 ecently inserted
1a432 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 opcode..**.** I
1a433 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 f a memory alloc
1a434 61 74 69 6f 6e 20 65 72 72 6f 72 20 68 61 73 20 ation error has
1a435 6f 63 63 75 72 72 65 64 20 70 72 69 6f 72 20 74 occurred prior t
1a436 6f 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 6f 66 o the calling of
1a437 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 this.** routine
1a438 2c 20 74 68 65 6e 20 61 20 70 6f 69 6e 74 65 72 , then a pointer
1a439 20 74 6f 20 61 20 64 75 6d 6d 79 20 56 64 62 65 to a dummy Vdbe
1a43a 4f 70 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 Op will be retur
1a43b 6e 65 64 2e 20 20 54 68 61 74 20 6f 70 63 6f 64 ned. That opcod
1a43c 65 0a 2a 2a 20 69 73 20 72 65 61 64 61 62 6c 65 e.** is readable
1a43d 20 61 6e 64 20 77 72 69 74 61 62 6c 65 2c 20 62 and writable, b
1a43e 75 74 20 69 74 20 68 61 73 20 6e 6f 20 65 66 66 ut it has no eff
1a43f 65 63 74 2e 20 20 54 68 65 20 72 65 74 75 72 6e ect. The return
1a440 20 6f 66 20 61 20 64 75 6d 6d 79 0a 2a 2a 20 6f of a dummy.** o
1a441 70 63 6f 64 65 20 61 6c 6c 6f 77 73 20 74 68 65 pcode allows the
1a442 20 63 61 6c 6c 20 74 6f 20 63 6f 6e 74 69 6e 75 call to continu
1a443 65 20 66 75 6e 63 74 69 6f 6e 69 6e 67 20 61 66 e functioning af
1a444 74 65 72 20 61 20 4f 4f 4d 20 66 61 75 6c 74 20 ter a OOM fault
1a445 77 69 74 68 6f 75 74 0a 2a 2a 20 68 61 76 69 6e without.** havin
1a446 67 20 74 6f 20 63 68 65 63 6b 20 74 6f 20 73 65 g to check to se
1a447 65 20 69 66 20 74 68 65 20 72 65 74 75 72 6e 20 e if the return
1a448 66 72 6f 6d 20 74 68 69 73 20 72 6f 75 74 69 6e from this routin
1a449 65 20 69 73 20 61 20 76 61 6c 69 64 20 70 6f 69 e is a valid poi
1a44a 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 62 6f 75 nter..**.** Abou
1a44b 74 20 74 68 65 20 23 69 66 64 65 66 20 53 51 4c t the #ifdef SQL
1a44c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 3a 20 ITE_OMIT_TRACE:
1a44d 20 4e 6f 72 6d 61 6c 6c 79 2c 20 74 68 69 73 20 Normally, this
1a44e 72 6f 75 74 69 6e 65 20 69 73 20 6e 65 76 65 72 routine is never
1a44f 20 63 61 6c 6c 65 64 0a 2a 2a 20 75 6e 6c 65 73 called.** unles
1a450 73 20 70 2d 3e 6e 4f 70 3e 30 2e 20 20 54 68 69 s p->nOp>0. Thi
1a451 73 20 69 73 20 62 65 63 61 75 73 65 20 69 6e 20 s is because in
1a452 74 68 65 20 61 62 73 65 6e 73 65 20 6f 66 20 53 the absense of S
1a453 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 QLITE_OMIT_TRACE
1a454 2c 0a 2a 2a 20 61 6e 20 4f 50 5f 54 72 61 63 65 ,.** an OP_Trace
1a455 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 instruction is
1a456 61 6c 77 61 79 73 20 69 6e 73 65 72 74 65 64 20 always inserted
1a457 62 79 20 73 71 6c 69 74 65 33 56 64 62 65 47 65 by sqlite3VdbeGe
1a458 74 28 29 20 61 73 20 73 6f 6f 6e 20 61 73 0a 2a t() as soon as.*
1a459 2a 20 61 20 6e 65 77 20 56 44 42 45 20 69 73 20 * a new VDBE is
1a45a 63 72 65 61 74 65 64 2e 20 20 53 6f 20 77 65 20 created. So we
1a45b 61 72 65 20 66 72 65 65 20 74 6f 20 73 65 74 20 are free to set
1a45c 61 64 64 72 20 74 6f 20 70 2d 3e 6e 4f 70 2d 31 addr to p->nOp-1
1a45d 20 77 69 74 68 6f 75 74 0a 2a 2a 20 68 61 76 69 without.** havi
1a45e 6e 67 20 74 6f 20 64 6f 75 62 6c 65 2d 63 68 65 ng to double-che
1a45f 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 ck to make sure
1a460 74 68 61 74 20 74 68 65 20 72 65 73 75 6c 74 20 that the result
1a461 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e is non-negative.
1a462 20 42 75 74 0a 2a 2a 20 69 66 20 53 51 4c 49 54 But.** if SQLIT
1a463 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 69 73 20 E_OMIT_TRACE is
1a464 64 65 66 69 6e 65 64 2c 20 74 68 65 20 4f 50 5f defined, the OP_
1a465 54 72 61 63 65 20 69 73 20 6f 6d 69 74 74 65 64 Trace is omitted
1a466 20 61 6e 64 20 77 65 20 64 6f 20 6e 65 65 64 20 and we do need
1a467 74 6f 0a 2a 2a 20 63 68 65 63 6b 20 74 68 65 20 to.** check the
1a468 76 61 6c 75 65 20 6f 66 20 70 2d 3e 6e 4f 70 2d value of p->nOp-
1a469 31 20 62 65 66 6f 72 65 20 63 6f 6e 74 69 6e 75 1 before continu
1a46a 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ing..*/.SQLITE_P
1a46b 52 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73 RIVATE VdbeOp *s
1a46c 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 qlite3VdbeGetOp(
1a46d 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 Vdbe *p, int add
1a46e 72 29 7b 0a 20 20 73 74 61 74 69 63 20 56 64 62 r){. static Vdb
1a46f 65 4f 70 20 64 75 6d 6d 79 3b 0a 20 20 61 73 73 eOp dummy;. ass
1a470 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 ert( p->magic==V
1a471 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 DBE_MAGIC_INIT )
1a472 3b 0a 20 20 69 66 28 20 61 64 64 72 3c 30 20 29 ;. if( addr<0 )
1a473 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f {.#ifdef SQLITE_
1a474 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 20 20 69 OMIT_TRACE. i
1a475 66 28 20 70 2d 3e 6e 4f 70 3d 3d 30 20 29 20 72 f( p->nOp==0 ) r
1a476 65 74 75 72 6e 20 26 64 75 6d 6d 79 3b 0a 23 65 eturn &dummy;.#e
1a477 6e 64 69 66 0a 20 20 20 20 61 64 64 72 20 3d 20 ndif. addr =
1a478 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a p->nOp - 1;. }.
1a479 20 20 61 73 73 65 72 74 28 20 28 61 64 64 72 3e assert( (addr>
1a47a 3d 30 20 26 26 20 61 64 64 72 3c 70 2d 3e 6e 4f =0 && addr<p->nO
1a47b 70 29 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c p) || p->db->mal
1a47c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 locFailed );. i
1a47d 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 f( p->db->malloc
1a47e 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 Failed ){. re
1a47f 74 75 72 6e 20 26 64 75 6d 6d 79 3b 0a 20 20 7d turn &dummy;. }
1a480 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
1a481 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a &p->aOp[addr];.
1a482 20 20 7d 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 }.}..#if !defi
1a483 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
1a484 45 58 50 4c 41 49 4e 29 20 7c 7c 20 21 64 65 66 EXPLAIN) || !def
1a485 69 6e 65 64 28 4e 44 45 42 55 47 29 20 5c 0a 20 ined(NDEBUG) \.
1a486 20 20 20 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 || defined(V
1a487 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 DBE_PROFILE) ||
1a488 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
1a489 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 EBUG)./*.** Comp
1a48a 75 74 65 20 61 20 73 74 72 69 6e 67 20 74 68 61 ute a string tha
1a48b 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 t describes the
1a48c 50 34 20 70 61 72 61 6d 65 74 65 72 20 66 6f 72 P4 parameter for
1a48d 20 61 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 55 an opcode..** U
1a48e 73 65 20 7a 54 65 6d 70 20 66 6f 72 20 61 6e 79 se zTemp for any
1a48f 20 72 65 71 75 69 72 65 64 20 74 65 6d 70 6f 72 required tempor
1a490 61 72 79 20 62 75 66 66 65 72 20 73 70 61 63 65 ary buffer space
1a491 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 ..*/.static char
1a492 20 2a 64 69 73 70 6c 61 79 50 34 28 4f 70 20 2a *displayP4(Op *
1a493 70 4f 70 2c 20 63 68 61 72 20 2a 7a 54 65 6d 70 pOp, char *zTemp
1a494 2c 20 69 6e 74 20 6e 54 65 6d 70 29 7b 0a 20 20 , int nTemp){.
1a495 63 68 61 72 20 2a 7a 50 34 20 3d 20 7a 54 65 6d char *zP4 = zTem
1a496 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65 p;. assert( nTe
1a497 6d 70 3e 3d 32 30 20 29 3b 0a 20 20 73 77 69 74 mp>=20 );. swit
1a498 63 68 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 ch( pOp->p4type
1a499 29 7b 0a 20 20 20 20 63 61 73 65 20 50 34 5f 4b ){. case P4_K
1a49a 45 59 49 4e 46 4f 5f 53 54 41 54 49 43 3a 0a 20 EYINFO_STATIC:.
1a49b 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e case P4_KEYIN
1a49c 46 4f 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 FO: {. int
1a49d 69 2c 20 6a 3b 0a 20 20 20 20 20 20 4b 65 79 49 i, j;. KeyI
1a49e 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 nfo *pKeyInfo =
1a49f 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f pOp->p4.pKeyInfo
1a4a0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
1a4a1 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 snprintf(nTemp,
1a4a2 7a 54 65 6d 70 2c 20 22 6b 65 79 69 6e 66 6f 28 zTemp, "keyinfo(
1a4a3 25 64 22 2c 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e %d", pKeyInfo->n
1a4a4 46 69 65 6c 64 29 3b 0a 20 20 20 20 20 20 69 20 Field);. i
1a4a5 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
1a4a6 30 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 0(zTemp);.
1a4a7 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4b 65 79 49 for(j=0; j<pKeyI
1a4a8 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b 20 6a 2b 2b nfo->nField; j++
1a4a9 29 7b 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 ){. CollS
1a4aa 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 4b 65 79 eq *pColl = pKey
1a4ab 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6a 5d 3b 0a Info->aColl[j];.
1a4ac 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c if( pCol
1a4ad 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 l ){. i
1a4ae 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 nt n = sqlite3St
1a4af 72 6c 65 6e 33 30 28 70 43 6f 6c 6c 2d 3e 7a 4e rlen30(pColl->zN
1a4b0 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ame);.
1a4b1 69 66 28 20 69 2b 6e 3e 6e 54 65 6d 70 2d 36 20 if( i+n>nTemp-6
1a4b2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d ){. m
1a4b3 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b 69 5d 2c emcpy(&zTemp[i],
1a4b4 22 2c 2e 2e 2e 22 2c 34 29 3b 0a 20 20 20 20 20 ",...",4);.
1a4b5 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1a4b6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1a4b7 20 20 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d zTemp[i++] =
1a4b8 20 27 2c 27 3b 0a 20 20 20 20 20 20 20 20 20 20 ',';.
1a4b9 69 66 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 if( pKeyInfo->aS
1a4ba 6f 72 74 4f 72 64 65 72 20 26 26 20 70 4b 65 79 ortOrder && pKey
1a4bb 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 Info->aSortOrder
1a4bc 5b 6a 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 [j] ){.
1a4bd 20 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 zTemp[i++] =
1a4be 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d '-';. }
1a4bf 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 . memcp
1a4c0 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 20 70 43 6f y(&zTemp[i], pCo
1a4c1 6c 6c 2d 3e 7a 4e 61 6d 65 2c 6e 2b 31 29 3b 0a ll->zName,n+1);.
1a4c2 20 20 20 20 20 20 20 20 20 20 69 20 2b 3d 20 6e i += n
1a4c3 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 ;. }else
1a4c4 69 66 28 20 69 2b 34 3c 6e 54 65 6d 70 2d 36 20 if( i+4<nTemp-6
1a4c5 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d ){. mem
1a4c6 63 70 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c cpy(&zTemp[i],",
1a4c7 6e 69 6c 22 2c 34 29 3b 0a 20 20 20 20 20 20 20 nil",4);.
1a4c8 20 20 20 69 20 2b 3d 20 34 3b 0a 20 20 20 20 20 i += 4;.
1a4c9 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1a4ca 20 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 zTemp[i++] =
1a4cb 27 29 27 3b 0a 20 20 20 20 20 20 7a 54 65 6d 70 ')';. zTemp
1a4cc 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 [i] = 0;. a
1a4cd 73 73 65 72 74 28 20 69 3c 6e 54 65 6d 70 20 29 ssert( i<nTemp )
1a4ce 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1a4cf 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 }. case P4
1a4d0 5f 43 4f 4c 4c 53 45 51 3a 20 7b 0a 20 20 20 20 _COLLSEQ: {.
1a4d1 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
1a4d2 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c = pOp->p4.pColl
1a4d3 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
1a4d4 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 snprintf(nTemp,
1a4d5 7a 54 65 6d 70 2c 20 22 63 6f 6c 6c 73 65 71 28 zTemp, "collseq(
1a4d6 25 2e 32 30 73 29 22 2c 20 70 43 6f 6c 6c 2d 3e %.20s)", pColl->
1a4d7 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 62 72 zName);. br
1a4d8 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1a4d9 61 73 65 20 50 34 5f 46 55 4e 43 44 45 46 3a 20 ase P4_FUNCDEF:
1a4da 7b 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 20 {. FuncDef
1a4db 2a 70 44 65 66 20 3d 20 70 4f 70 2d 3e 70 34 2e *pDef = pOp->p4.
1a4dc 70 46 75 6e 63 3b 0a 20 20 20 20 20 20 73 71 6c pFunc;. sql
1a4dd 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 ite3_snprintf(nT
1a4de 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 73 28 emp, zTemp, "%s(
1a4df 25 64 29 22 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d %d)", pDef->zNam
1a4e0 65 2c 20 70 44 65 66 2d 3e 6e 41 72 67 29 3b 0a e, pDef->nArg);.
1a4e1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1a4e2 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49 }. case P4_I
1a4e3 4e 54 36 34 3a 20 7b 0a 20 20 20 20 20 20 73 71 NT64: {. sq
1a4e4 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
1a4e5 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 6c Temp, zTemp, "%l
1a4e6 6c 64 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 49 ld", *pOp->p4.pI
1a4e7 36 34 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 64);. break
1a4e8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1a4e9 20 50 34 5f 49 4e 54 33 32 3a 20 7b 0a 20 20 20 P4_INT32: {.
1a4ea 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1a4eb 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 ntf(nTemp, zTemp
1a4ec 2c 20 22 25 64 22 2c 20 70 4f 70 2d 3e 70 34 2e , "%d", pOp->p4.
1a4ed 69 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b i);. break;
1a4ee 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1a4ef 50 34 5f 52 45 41 4c 3a 20 7b 0a 20 20 20 20 20 P4_REAL: {.
1a4f0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1a4f1 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 f(nTemp, zTemp,
1a4f2 22 25 2e 31 36 67 22 2c 20 2a 70 4f 70 2d 3e 70 "%.16g", *pOp->p
1a4f3 34 2e 70 52 65 61 6c 29 3b 0a 20 20 20 20 20 20 4.pReal);.
1a4f4 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1a4f5 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a 20 7b 0a case P4_MEM: {.
1a4f6 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 Mem *pMem
1a4f7 3d 20 70 4f 70 2d 3e 70 34 2e 70 4d 65 6d 3b 0a = pOp->p4.pMem;.
1a4f8 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 assert( (p
1a4f9 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
1a4fa 5f 4e 75 6c 6c 29 3d 3d 30 20 29 3b 0a 20 20 20 _Null)==0 );.
1a4fb 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 if( pMem->fla
1a4fc 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a gs & MEM_Str ){.
1a4fd 20 20 20 20 20 20 20 20 7a 50 34 20 3d 20 70 4d zP4 = pM
1a4fe 65 6d 2d 3e 7a 3b 0a 20 20 20 20 20 20 7d 65 6c em->z;. }el
1a4ff 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 se if( pMem->fla
1a500 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a gs & MEM_Int ){.
1a501 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
1a502 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 snprintf(nTemp,
1a503 7a 54 65 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 70 zTemp, "%lld", p
1a504 4d 65 6d 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 20 Mem->u.i);.
1a505 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d }else if( pMem-
1a506 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 >flags & MEM_Rea
1a507 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c l ){. sql
1a508 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 ite3_snprintf(nT
1a509 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 emp, zTemp, "%.1
1a50a 36 67 22 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a 20 6g", pMem->r);.
1a50b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1a50c 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
1a50d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c ->flags & MEM_Bl
1a50e 6f 62 20 29 3b 0a 20 20 20 20 20 20 20 20 7a 50 ob );. zP
1a50f 34 20 3d 20 22 28 62 6c 6f 62 29 22 3b 0a 20 20 4 = "(blob)";.
1a510 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
1a511 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 k;. }.#ifndef
1a512 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1a513 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 63 61 TUALTABLE. ca
1a514 73 65 20 50 34 5f 56 54 41 42 3a 20 7b 0a 20 20 se P4_VTAB: {.
1a515 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 sqlite3_vtab
1a516 20 2a 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 *pVtab = pOp->p
1a517 34 2e 70 56 74 61 62 2d 3e 70 56 74 61 62 3b 0a 4.pVtab->pVtab;.
1a518 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
1a519 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 printf(nTemp, zT
1a51a 65 6d 70 2c 20 22 76 74 61 62 3a 25 70 3a 25 70 emp, "vtab:%p:%p
1a51b 22 2c 20 70 56 74 61 62 2c 20 70 56 74 61 62 2d ", pVtab, pVtab-
1a51c 3e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 20 20 20 >pModule);.
1a51d 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 break;. }.#e
1a51e 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 50 34 ndif. case P4
1a51f 5f 49 4e 54 41 52 52 41 59 3a 20 7b 0a 20 20 20 _INTARRAY: {.
1a520 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1a521 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 ntf(nTemp, zTemp
1a522 2c 20 22 69 6e 74 61 72 72 61 79 22 29 3b 0a 20 , "intarray");.
1a523 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1a524 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 53 55 }. case P4_SU
1a525 42 50 52 4f 47 52 41 4d 3a 20 7b 0a 20 20 20 20 BPROGRAM: {.
1a526 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
1a527 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c tf(nTemp, zTemp,
1a528 20 22 70 72 6f 67 72 61 6d 22 29 3b 0a 20 20 20 "program");.
1a529 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1a52a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
1a52b 20 20 20 20 20 7a 50 34 20 3d 20 70 4f 70 2d 3e zP4 = pOp->
1a52c 70 34 2e 7a 3b 0a 20 20 20 20 20 20 69 66 28 20 p4.z;. if(
1a52d 7a 50 34 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 zP4==0 ){.
1a52e 20 20 7a 50 34 20 3d 20 7a 54 65 6d 70 3b 0a 20 zP4 = zTemp;.
1a52f 20 20 20 20 20 20 20 7a 54 65 6d 70 5b 30 5d 20 zTemp[0]
1a530 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 0;. }.
1a531 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 }. }. assert(
1a532 20 7a 50 34 21 3d 30 20 29 3b 0a 20 20 72 65 74 zP4!=0 );. ret
1a533 75 72 6e 20 7a 50 34 3b 0a 7d 0a 23 65 6e 64 69 urn zP4;.}.#endi
1a534 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6c 61 72 65 f../*.** Declare
1a535 20 74 6f 20 74 68 65 20 56 64 62 65 20 74 68 61 to the Vdbe tha
1a536 74 20 74 68 65 20 42 54 72 65 65 20 6f 62 6a 65 t the BTree obje
1a537 63 74 20 61 74 20 64 62 2d 3e 61 44 62 5b 69 5d ct at db->aDb[i]
1a538 20 69 73 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c is used..*/.SQL
1a539 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1a53a 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 sqlite3VdbeUses
1a53b 42 74 72 65 65 28 56 64 62 65 20 2a 70 2c 20 69 Btree(Vdbe *p, i
1a53c 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 6d 61 73 nt i){. int mas
1a53d 6b 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d k;. assert( i>=
1a53e 30 20 26 26 20 69 3c 70 2d 3e 64 62 2d 3e 6e 44 0 && i<p->db->nD
1a53f 62 20 26 26 20 69 3c 73 69 7a 65 6f 66 28 75 33 b && i<sizeof(u3
1a540 32 29 2a 38 20 29 3b 0a 20 20 61 73 73 65 72 74 2)*8 );. assert
1a541 28 20 69 3c 28 69 6e 74 29 73 69 7a 65 6f 66 28 ( i<(int)sizeof(
1a542 70 2d 3e 62 74 72 65 65 4d 61 73 6b 29 2a 38 20 p->btreeMask)*8
1a543 29 3b 0a 20 20 6d 61 73 6b 20 3d 20 28 28 75 33 );. mask = ((u3
1a544 32 29 31 29 3c 3c 69 3b 0a 20 20 69 66 28 20 28 2)1)<<i;. if( (
1a545 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 6d p->btreeMask & m
1a546 61 73 6b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 ask)==0 ){. p
1a547 2d 3e 62 74 72 65 65 4d 61 73 6b 20 7c 3d 20 6d ->btreeMask |= m
1a548 61 73 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ask;. sqlite3
1a549 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 BtreeMutexArrayI
1a54a 6e 73 65 72 74 28 26 70 2d 3e 61 4d 75 74 65 78 nsert(&p->aMutex
1a54b 2c 20 70 2d 3e 64 62 2d 3e 61 44 62 5b 69 5d 2e , p->db->aDb[i].
1a54c 70 42 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 23 69 pBt);. }.}...#i
1a54d 66 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 f defined(VDBE_P
1a54e 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e ROFILE) || defin
1a54f 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
1a550 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 20 73 ./*.** Print a s
1a551 69 6e 67 6c 65 20 6f 70 63 6f 64 65 2e 20 20 54 ingle opcode. T
1a552 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
1a553 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e sed for debuggin
1a554 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 g only..*/.SQLIT
1a555 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1a556 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f qlite3VdbePrintO
1a557 70 28 46 49 4c 45 20 2a 70 4f 75 74 2c 20 69 6e p(FILE *pOut, in
1a558 74 20 70 63 2c 20 4f 70 20 2a 70 4f 70 29 7b 0a t pc, Op *pOp){.
1a559 20 20 63 68 61 72 20 2a 7a 50 34 3b 0a 20 20 63 char *zP4;. c
1a55a 68 61 72 20 7a 50 74 72 5b 35 30 5d 3b 0a 20 20 har zPtr[50];.
1a55b 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 static const cha
1a55c 72 20 2a 7a 46 6f 72 6d 61 74 31 20 3d 20 22 25 r *zFormat1 = "%
1a55d 34 64 20 25 2d 31 33 73 20 25 34 64 20 25 34 64 4d %-13s %4d %4d
1a55e 20 25 34 64 20 25 2d 34 73 20 25 2e 32 58 20 25 %4d %-4s %.2X %
1a55f 73 5c 6e 22 3b 0a 20 20 69 66 28 20 70 4f 75 74 s\n";. if( pOut
1a560 3d 3d 30 20 29 20 70 4f 75 74 20 3d 20 73 74 64 ==0 ) pOut = std
1a561 6f 75 74 3b 0a 20 20 7a 50 34 20 3d 20 64 69 73 out;. zP4 = dis
1a562 70 6c 61 79 50 34 28 70 4f 70 2c 20 7a 50 74 72 playP4(pOp, zPtr
1a563 2c 20 73 69 7a 65 6f 66 28 7a 50 74 72 29 29 3b , sizeof(zPtr));
1a564 0a 20 20 66 70 72 69 6e 74 66 28 70 4f 75 74 2c . fprintf(pOut,
1a565 20 7a 46 6f 72 6d 61 74 31 2c 20 70 63 2c 20 0a zFormat1, pc, .
1a566 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70 63 sqlite3Opc
1a567 6f 64 65 4e 61 6d 65 28 70 4f 70 2d 3e 6f 70 63 odeName(pOp->opc
1a568 6f 64 65 29 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 ode), pOp->p1, p
1a569 4f 70 2d 3e 70 32 2c 20 70 4f 70 2d 3e 70 33 2c Op->p2, pOp->p3,
1a56a 20 7a 50 34 2c 20 70 4f 70 2d 3e 70 35 2c 0a 23 zP4, pOp->p5,.#
1a56b 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
1a56c 55 47 0a 20 20 20 20 20 20 70 4f 70 2d 3e 7a 43 UG. pOp->zC
1a56d 6f 6d 6d 65 6e 74 20 3f 20 70 4f 70 2d 3e 7a 43 omment ? pOp->zC
1a56e 6f 6d 6d 65 6e 74 20 3a 20 22 22 0a 23 65 6c 73 omment : "".#els
1a56f 65 0a 20 20 20 20 20 20 22 22 0a 23 65 6e 64 69 e. "".#endi
1a570 66 0a 20 20 29 3b 0a 20 20 66 66 6c 75 73 68 28 f. );. fflush(
1a571 70 4f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a pOut);.}.#endif.
1a572 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 ./*.** Release a
1a573 6e 20 61 72 72 61 79 20 6f 66 20 4e 20 4d 65 6d n array of N Mem
1a574 20 65 6c 65 6d 65 6e 74 73 0a 2a 2f 0a 73 74 61 elements.*/.sta
1a575 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 tic void release
1a576 4d 65 6d 41 72 72 61 79 28 4d 65 6d 20 2a 70 2c MemArray(Mem *p,
1a577 20 69 6e 74 20 4e 29 7b 0a 20 20 69 66 28 20 70 int N){. if( p
1a578 20 26 26 20 4e 20 29 7b 0a 20 20 20 20 4d 65 6d && N ){. Mem
1a579 20 2a 70 45 6e 64 3b 0a 20 20 20 20 73 71 6c 69 *pEnd;. sqli
1a57a 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b te3 *db = p->db;
1a57b 0a 20 20 20 20 75 38 20 6d 61 6c 6c 6f 63 5f 66 . u8 malloc_f
1a57c 61 69 6c 65 64 20 3d 20 64 62 2d 3e 6d 61 6c 6c ailed = db->mall
1a57d 6f 63 46 61 69 6c 65 64 3b 0a 20 20 20 20 66 6f ocFailed;. fo
1a57e 72 28 70 45 6e 64 3d 26 70 5b 4e 5d 3b 20 70 3c r(pEnd=&p[N]; p<
1a57f 70 45 6e 64 3b 20 70 2b 2b 29 7b 0a 20 20 20 20 pEnd; p++){.
1a580 20 20 61 73 73 65 72 74 28 20 28 26 70 5b 31 5d assert( (&p[1]
1a581 29 3d 3d 70 45 6e 64 20 7c 7c 20 70 5b 30 5d 2e )==pEnd || p[0].
1a582 64 62 3d 3d 70 5b 31 5d 2e 64 62 20 29 3b 0a 0a db==p[1].db );..
1a583 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c /* This bl
1a584 6f 63 6b 20 69 73 20 72 65 61 6c 6c 79 20 61 6e ock is really an
1a585 20 69 6e 6c 69 6e 65 64 20 76 65 72 73 69 6f 6e inlined version
1a586 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65 4d of sqlite3VdbeM
1a587 65 6d 52 65 6c 65 61 73 65 28 29 0a 20 20 20 20 emRelease().
1a588 20 20 2a 2a 20 74 68 61 74 20 74 61 6b 65 73 20 ** that takes
1a589 61 64 76 61 6e 74 61 67 65 20 6f 66 20 74 68 65 advantage of the
1a58a 20 66 61 63 74 20 74 68 61 74 20 74 68 65 20 6d fact that the m
1a58b 65 6d 6f 72 79 20 63 65 6c 6c 20 76 61 6c 75 65 emory cell value
1a58c 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 62 65 is . ** be
1a58d 69 6e 67 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20 ing set to NULL
1a58e 61 66 74 65 72 20 72 65 6c 65 61 73 69 6e 67 20 after releasing
1a58f 61 6e 79 20 64 79 6e 61 6d 69 63 20 72 65 73 6f any dynamic reso
1a590 75 72 63 65 73 2e 0a 20 20 20 20 20 20 2a 2a 0a urces.. **.
1a591 20 20 20 20 20 20 2a 2a 20 54 68 65 20 6a 75 73 ** The jus
1a592 74 69 66 69 63 61 74 69 6f 6e 20 66 6f 72 20 64 tification for d
1a593 75 70 6c 69 63 61 74 69 6e 67 20 63 6f 64 65 20 uplicating code
1a594 69 73 20 74 68 61 74 20 61 63 63 6f 72 64 69 6e is that accordin
1a595 67 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 63 g to . ** c
1a596 61 6c 6c 67 72 69 6e 64 2c 20 74 68 69 73 20 63 allgrind, this c
1a597 61 75 73 65 73 20 61 20 63 65 72 74 61 69 6e 20 auses a certain
1a598 74 65 73 74 20 63 61 73 65 20 74 6f 20 68 69 74 test case to hit
1a599 20 74 68 65 20 43 50 55 20 34 2e 37 20 0a 20 20 the CPU 4.7 .
1a59a 20 20 20 20 2a 2a 20 70 65 72 63 65 6e 74 20 6c ** percent l
1a59b 65 73 73 20 28 78 38 36 20 6c 69 6e 75 78 2c 20 ess (x86 linux,
1a59c 67 63 63 20 76 65 72 73 69 6f 6e 20 34 2e 31 2e gcc version 4.1.
1a59d 32 2c 20 2d 4f 36 29 20 74 68 61 6e 20 69 66 20 2, -O6) than if
1a59e 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 . ** sqlite
1a59f 33 4d 65 6d 52 65 6c 65 61 73 65 28 29 20 77 65 3MemRelease() we
1a5a0 72 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 68 re called from h
1a5a1 65 72 65 2e 20 57 69 74 68 20 2d 4f 32 2c 20 74 ere. With -O2, t
1a5a2 68 69 73 20 6a 75 6d 70 73 0a 20 20 20 20 20 20 his jumps.
1a5a3 2a 2a 20 74 6f 20 36 2e 36 20 70 65 72 63 65 6e ** to 6.6 percen
1a5a4 74 2e 20 54 68 65 20 74 65 73 74 20 63 61 73 65 t. The test case
1a5a5 20 69 73 20 69 6e 73 65 72 74 69 6e 67 20 31 30 is inserting 10
1a5a6 30 30 20 72 6f 77 73 20 69 6e 74 6f 20 61 20 74 00 rows into a t
1a5a7 61 62 6c 65 20 0a 20 20 20 20 20 20 2a 2a 20 77 able . ** w
1a5a8 69 74 68 20 6e 6f 20 69 6e 64 65 78 65 73 20 75 ith no indexes u
1a5a9 73 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 70 72 sing a single pr
1a5aa 65 70 61 72 65 64 20 49 4e 53 45 52 54 20 73 74 epared INSERT st
1a5ab 61 74 65 6d 65 6e 74 2c 20 62 69 6e 64 28 29 20 atement, bind()
1a5ac 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 72 65 . ** and re
1a5ad 73 65 74 28 29 2e 20 49 6e 73 65 72 74 73 20 61 set(). Inserts a
1a5ae 72 65 20 67 72 6f 75 70 65 64 20 69 6e 74 6f 20 re grouped into
1a5af 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 a transaction..
1a5b0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
1a5b1 28 20 70 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f ( p->flags&(MEM_
1a5b2 41 67 67 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f Agg|MEM_Dyn|MEM_
1a5b3 46 72 61 6d 65 7c 4d 45 4d 5f 52 6f 77 53 65 74 Frame|MEM_RowSet
1a5b4 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c ) ){. sql
1a5b5 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
1a5b6 73 65 28 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c se(p);. }el
1a5b7 73 65 20 69 66 28 20 70 2d 3e 7a 4d 61 6c 6c 6f se if( p->zMallo
1a5b8 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c c ){. sql
1a5b9 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1a5ba 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 ->zMalloc);.
1a5bb 20 20 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d p->zMalloc =
1a5bc 20 30 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 0;. }..
1a5bd 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 p->flags = ME
1a5be 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 M_Null;. }.
1a5bf 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c db->mallocFail
1a5c0 65 64 20 3d 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c ed = malloc_fail
1a5c1 65 64 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a ed;. }.}../*.**
1a5c2 20 44 65 6c 65 74 65 20 61 20 56 64 62 65 46 72 Delete a VdbeFr
1a5c3 61 6d 65 20 6f 62 6a 65 63 74 20 61 6e 64 20 69 ame object and i
1a5c4 74 73 20 63 6f 6e 74 65 6e 74 73 2e 20 56 64 62 ts contents. Vdb
1a5c5 65 46 72 61 6d 65 20 6f 62 6a 65 63 74 73 20 61 eFrame objects a
1a5c6 72 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 re.** allocated
1a5c7 62 79 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 by the OP_Progra
1a5c8 6d 20 6f 70 63 6f 64 65 20 69 6e 20 73 71 6c 69 m opcode in sqli
1a5c9 74 65 33 56 64 62 65 45 78 65 63 28 29 2e 0a 2a te3VdbeExec()..*
1a5ca 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1a5cb 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
1a5cc 65 46 72 61 6d 65 44 65 6c 65 74 65 28 56 64 62 eFrameDelete(Vdb
1a5cd 65 46 72 61 6d 65 20 2a 70 29 7b 0a 20 20 69 6e eFrame *p){. in
1a5ce 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d t i;. Mem *aMem
1a5cf 20 3d 20 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 = VdbeFrameMem(
1a5d0 70 29 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 p);. VdbeCursor
1a5d1 20 2a 2a 61 70 43 73 72 20 3d 20 28 56 64 62 65 **apCsr = (Vdbe
1a5d2 43 75 72 73 6f 72 20 2a 2a 29 26 61 4d 65 6d 5b Cursor **)&aMem[
1a5d3 70 2d 3e 6e 43 68 69 6c 64 4d 65 6d 5d 3b 0a 20 p->nChildMem];.
1a5d4 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e for(i=0; i<p->n
1a5d5 43 68 69 6c 64 43 73 72 3b 20 69 2b 2b 29 7b 0a ChildCsr; i++){.
1a5d6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 sqlite3VdbeF
1a5d7 72 65 65 43 75 72 73 6f 72 28 70 2d 3e 76 2c 20 reeCursor(p->v,
1a5d8 61 70 43 73 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 apCsr[i]);. }.
1a5d9 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 releaseMemArray
1a5da 28 61 4d 65 6d 2c 20 70 2d 3e 6e 43 68 69 6c 64 (aMem, p->nChild
1a5db 4d 65 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 Mem);. sqlite3D
1a5dc 62 46 72 65 65 28 70 2d 3e 76 2d 3e 64 62 2c 20 bFree(p->v->db,
1a5dd 70 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 p);.}..#ifndef S
1a5de 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 QLITE_OMIT_EXPLA
1a5df 49 4e 0a 2f 2a 0a 2a 2a 20 47 69 76 65 20 61 20 IN./*.** Give a
1a5e0 6c 69 73 74 69 6e 67 20 6f 66 20 74 68 65 20 70 listing of the p
1a5e1 72 6f 67 72 61 6d 20 69 6e 20 74 68 65 20 76 69 rogram in the vi
1a5e2 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a rtual machine..*
1a5e3 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 *.** The interfa
1a5e4 63 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 ce is the same a
1a5e5 73 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 s sqlite3VdbeExe
1a5e6 63 28 29 2e 20 20 42 75 74 20 69 6e 73 74 65 61 c(). But instea
1a5e7 64 20 6f 66 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 d of.** running
1a5e8 74 68 65 20 63 6f 64 65 2c 20 69 74 20 69 6e 76 the code, it inv
1a5e9 6f 6b 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 okes the callbac
1a5ea 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 k once for each
1a5eb 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 instruction..**
1a5ec 54 68 69 73 20 66 65 61 74 75 72 65 20 69 73 20 This feature is
1a5ed 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e used to implemen
1a5ee 74 20 22 45 58 50 4c 41 49 4e 22 2e 0a 2a 2a 0a t "EXPLAIN"..**.
1a5ef 2a 2a 20 57 68 65 6e 20 70 2d 3e 65 78 70 6c 61 ** When p->expla
1a5f0 69 6e 3d 3d 31 2c 20 65 61 63 68 20 69 6e 73 74 in==1, each inst
1a5f1 72 75 63 74 69 6f 6e 20 69 73 20 6c 69 73 74 65 ruction is liste
1a5f2 64 2e 20 20 57 68 65 6e 0a 2a 2a 20 70 2d 3e 65 d. When.** p->e
1a5f3 78 70 6c 61 69 6e 3d 3d 32 2c 20 6f 6e 6c 79 20 xplain==2, only
1a5f4 4f 50 5f 45 78 70 6c 61 69 6e 20 69 6e 73 74 72 OP_Explain instr
1a5f5 75 63 74 69 6f 6e 73 20 61 72 65 20 6c 69 73 74 uctions are list
1a5f6 65 64 20 61 6e 64 20 74 68 65 73 65 0a 2a 2a 20 ed and these.**
1a5f7 61 72 65 20 73 68 6f 77 6e 20 69 6e 20 61 20 64 are shown in a d
1a5f8 69 66 66 65 72 65 6e 74 20 66 6f 72 6d 61 74 2e ifferent format.
1a5f9 20 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 p->explain==2
1a5fa 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 is used to imple
1a5fb 6d 65 6e 74 0a 2a 2a 20 45 58 50 4c 41 49 4e 20 ment.** EXPLAIN
1a5fc 51 55 45 52 59 20 50 4c 41 4e 2e 0a 2a 2f 0a 53 QUERY PLAN..*/.S
1a5fd 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1a5fe 74 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73 t sqlite3VdbeLis
1a5ff 74 28 0a 20 20 56 64 62 65 20 2a 70 20 20 20 20 t(. Vdbe *p
1a600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1a601 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0a 29 7b * The VDBE */.){
1a602 0a 20 20 69 6e 74 20 6e 52 6f 77 3b 20 20 20 20 . int nRow;
1a603 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a604 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
1a605 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 number of rows
1a606 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 to return */. i
1a607 6e 74 20 6e 53 75 62 20 3d 20 30 3b 20 20 20 20 nt nSub = 0;
1a608 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a609 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1a60a 20 73 75 62 2d 76 64 62 65 73 20 73 65 65 6e 20 sub-vdbes seen
1a60b 73 6f 20 66 61 72 20 2a 2f 0a 20 20 53 75 62 50 so far */. SubP
1a60c 72 6f 67 72 61 6d 20 2a 2a 61 70 53 75 62 20 3d rogram **apSub =
1a60d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1a60e 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 75 62 /* Array of sub
1a60f 2d 76 64 62 65 73 20 2a 2f 0a 20 20 4d 65 6d 20 -vdbes */. Mem
1a610 2a 70 53 75 62 20 3d 20 30 3b 0a 20 20 73 71 6c *pSub = 0;. sql
1a611 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 ite3 *db = p->db
1a612 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 ;. int i;. int
1a613 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1a614 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 . Mem *pMem = p
1a615 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 26 ->pResultSet = &
1a616 70 2d 3e 61 4d 65 6d 5b 31 5d 3b 0a 0a 20 20 61 p->aMem[1];.. a
1a617 73 73 65 72 74 28 20 70 2d 3e 65 78 70 6c 61 69 ssert( p->explai
1a618 6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 n );. assert( p
1a619 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 ->magic==VDBE_MA
1a61a 47 49 43 5f 52 55 4e 20 29 3b 0a 20 20 61 73 73 GIC_RUN );. ass
1a61b 65 72 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d ert( db->magic==
1a61c 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 SQLITE_MAGIC_BUS
1a61d 59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 Y );. assert( p
1a61e 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ->rc==SQLITE_OK
1a61f 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 || p->rc==SQLITE
1a620 5f 42 55 53 59 20 7c 7c 20 70 2d 3e 72 63 3d 3d _BUSY || p->rc==
1a621 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a SQLITE_NOMEM );.
1a622 0a 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 . /* Even thoug
1a623 68 20 74 68 69 73 20 6f 70 63 6f 64 65 20 64 6f h this opcode do
1a624 65 73 20 6e 6f 74 20 75 73 65 20 64 79 6e 61 6d es not use dynam
1a625 69 63 20 73 74 72 69 6e 67 73 20 66 6f 72 0a 20 ic strings for.
1a626 20 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2c 20 ** the result,
1a627 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 6d result columns m
1a628 61 79 20 62 65 63 6f 6d 65 20 64 79 6e 61 6d 69 ay become dynami
1a629 63 20 69 66 20 74 68 65 20 75 73 65 72 20 63 61 c if the user ca
1a62a 6c 6c 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 lls. ** sqlite3
1a62b 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 _column_text16()
1a62c 2c 20 63 61 75 73 69 6e 67 20 61 20 74 72 61 6e , causing a tran
1a62d 73 6c 61 74 69 6f 6e 20 74 6f 20 55 54 46 2d 31 slation to UTF-1
1a62e 36 20 65 6e 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f 6 encoding.. */
1a62f 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 . releaseMemArr
1a630 61 79 28 70 4d 65 6d 2c 20 38 29 3b 0a 0a 20 20 ay(pMem, 8);..
1a631 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 if( p->rc==SQLIT
1a632 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 2f E_NOMEM ){. /
1a633 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 69 * This happens i
1a634 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 f a malloc() ins
1a635 69 64 65 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 ide a call to sq
1a636 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
1a637 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a 20 73 71 t() or. ** sq
1a638 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
1a639 74 31 36 28 29 20 66 61 69 6c 65 64 2e 20 20 2a t16() failed. *
1a63a 2f 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 /. db->malloc
1a63b 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 Failed = 1;.
1a63c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
1a63d 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 ROR;. }.. /* F
1a63e 69 67 75 72 65 20 6f 75 74 20 74 6f 74 61 6c 20 igure out total
1a63f 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 number of rows t
1a640 68 61 74 20 77 69 6c 6c 20 62 65 20 72 65 74 75 hat will be retu
1a641 72 6e 65 64 20 62 79 20 74 68 69 73 20 0a 20 20 rned by this .
1a642 2a 2a 20 45 58 50 4c 41 49 4e 20 70 72 6f 67 72 ** EXPLAIN progr
1a643 61 6d 2e 20 20 2a 2f 0a 20 20 6e 52 6f 77 20 3d am. */. nRow =
1a644 20 70 2d 3e 6e 4f 70 3b 0a 20 20 69 66 28 20 70 p->nOp;. if( p
1a645 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a ->explain==1 ){.
1a646 20 20 20 20 70 53 75 62 20 3d 20 26 70 2d 3e 61 pSub = &p->a
1a647 4d 65 6d 5b 39 5d 3b 0a 20 20 20 20 69 66 28 20 Mem[9];. if(
1a648 70 53 75 62 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pSub->flags&MEM_
1a649 42 6c 6f 62 20 29 7b 0a 20 20 20 20 20 20 6e 53 Blob ){. nS
1a64a 75 62 20 3d 20 70 53 75 62 2d 3e 6e 2f 73 69 7a ub = pSub->n/siz
1a64b 65 6f 66 28 56 64 62 65 2a 29 3b 0a 20 20 20 20 eof(Vdbe*);.
1a64c 20 20 61 70 53 75 62 20 3d 20 28 53 75 62 50 72 apSub = (SubPr
1a64d 6f 67 72 61 6d 20 2a 2a 29 70 53 75 62 2d 3e 7a ogram **)pSub->z
1a64e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 ;. }. for(
1a64f 69 3d 30 3b 20 69 3c 6e 53 75 62 3b 20 69 2b 2b i=0; i<nSub; i++
1a650 29 7b 0a 20 20 20 20 20 20 6e 52 6f 77 20 2b 3d ){. nRow +=
1a651 20 61 70 53 75 62 5b 69 5d 2d 3e 6e 4f 70 3b 0a apSub[i]->nOp;.
1a652 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6f 7b }. }.. do{
1a653 0a 20 20 20 20 69 20 3d 20 70 2d 3e 70 63 2b 2b . i = p->pc++
1a654 3b 0a 20 20 7d 77 68 69 6c 65 28 20 69 3c 6e 52 ;. }while( i<nR
1a655 6f 77 20 26 26 20 70 2d 3e 65 78 70 6c 61 69 6e ow && p->explain
1a656 3d 3d 32 20 26 26 20 70 2d 3e 61 4f 70 5b 69 5d ==2 && p->aOp[i]
1a657 2e 6f 70 63 6f 64 65 21 3d 4f 50 5f 45 78 70 6c .opcode!=OP_Expl
1a658 61 69 6e 20 29 3b 0a 20 20 69 66 28 20 69 3e 3d ain );. if( i>=
1a659 6e 52 6f 77 20 29 7b 0a 20 20 20 20 70 2d 3e 72 nRow ){. p->r
1a65a 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1a65b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 rc = SQLITE_D
1a65c 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ONE;. }else if(
1a65d 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 db->u1.isInterr
1a65e 75 70 74 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e upted ){. p->
1a65f 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 rc = SQLITE_INTE
1a660 52 52 55 50 54 3b 0a 20 20 20 20 72 63 20 3d 20 RRUPT;. rc =
1a661 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1a662 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 sqlite3SetStri
1a663 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 ng(&p->zErrMsg,
1a664 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 db, "%s", sqlite
1a665 33 45 72 72 53 74 72 28 70 2d 3e 72 63 29 29 3b 3ErrStr(p->rc));
1a666 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 . }else{. ch
1a667 61 72 20 2a 7a 3b 0a 20 20 20 20 4f 70 20 2a 70 ar *z;. Op *p
1a668 4f 70 3b 0a 20 20 20 20 69 66 28 20 69 3c 70 2d Op;. if( i<p-
1a669 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 20 20 70 4f >nOp ){. pO
1a66a 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a p = &p->aOp[i];.
1a66b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1a66c 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69 20 int j;. i
1a66d 2d 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20 -= p->nOp;.
1a66e 20 66 6f 72 28 6a 3d 30 3b 20 69 3e 3d 61 70 53 for(j=0; i>=apS
1a66f 75 62 5b 6a 5d 2d 3e 6e 4f 70 3b 20 6a 2b 2b 29 ub[j]->nOp; j++)
1a670 7b 0a 20 20 20 20 20 20 20 20 69 20 2d 3d 20 61 {. i -= a
1a671 70 53 75 62 5b 6a 5d 2d 3e 6e 4f 70 3b 0a 20 20 pSub[j]->nOp;.
1a672 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 20 }. pOp
1a673 3d 20 26 61 70 53 75 62 5b 6a 5d 2d 3e 61 4f 70 = &apSub[j]->aOp
1a674 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 [i];. }. i
1a675 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 f( p->explain==1
1a676 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e ){. pMem->
1a677 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
1a678 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 . pMem->typ
1a679 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 e = SQLITE_INTEG
1a67a 45 52 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e ER;. pMem->
1a67b 75 2e 69 20 3d 20 69 3b 20 20 20 20 20 20 20 20 u.i = i;
1a67c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a67d 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 /* Progr
1a67e 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 am counter */.
1a67f 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 20 pMem++;. .
1a680 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
1a681 20 3d 20 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 = MEM_Static|ME
1a682 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a M_Str|MEM_Term;.
1a683 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 pMem->z =
1a684 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 4f 70 (char*)sqlite3Op
1a685 63 6f 64 65 4e 61 6d 65 28 70 4f 70 2d 3e 6f 70 codeName(pOp->op
1a686 63 6f 64 65 29 3b 20 20 2f 2a 20 4f 70 63 6f 64 code); /* Opcod
1a687 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 e */. asser
1a688 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b t( pMem->z!=0 );
1a689 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d . pMem->n =
1a68a 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
1a68b 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 (pMem->z);.
1a68c 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
1a68d 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 LITE_TEXT;.
1a68e 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c pMem->enc = SQL
1a68f 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 ITE_UTF8;.
1a690 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 20 20 69 pMem++;.. i
1a691 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d f( pOp->p4type==
1a692 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 29 7b P4_SUBPROGRAM ){
1a693 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 42 79 . int nBy
1a694 74 65 20 3d 20 28 6e 53 75 62 2b 31 29 2a 73 69 te = (nSub+1)*si
1a695 7a 65 6f 66 28 53 75 62 50 72 6f 67 72 61 6d 2a zeof(SubProgram*
1a696 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a );. int j
1a697 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d ;. for(j=
1a698 30 3b 20 6a 3c 6e 53 75 62 3b 20 6a 2b 2b 29 7b 0; j<nSub; j++){
1a699 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61 . if( a
1a69a 70 53 75 62 5b 6a 5d 3d 3d 70 4f 70 2d 3e 70 34 pSub[j]==pOp->p4
1a69b 2e 70 50 72 6f 67 72 61 6d 20 29 20 62 72 65 61 .pProgram ) brea
1a69c 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 k;. }.
1a69d 20 20 20 20 20 69 66 28 20 6a 3d 3d 6e 53 75 62 if( j==nSub
1a69e 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 && SQLITE_OK==s
1a69f 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f qlite3VdbeMemGro
1a6a0 77 28 70 53 75 62 2c 20 6e 42 79 74 65 2c 20 31 w(pSub, nByte, 1
1a6a1 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 ) ){. a
1a6a2 70 53 75 62 20 3d 20 28 53 75 62 50 72 6f 67 72 pSub = (SubProgr
1a6a3 61 6d 20 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 am **)pSub->z;.
1a6a4 20 20 20 20 20 20 20 20 20 61 70 53 75 62 5b 6e apSub[n
1a6a5 53 75 62 2b 2b 5d 20 3d 20 70 4f 70 2d 3e 70 34 Sub++] = pOp->p4
1a6a6 2e 70 50 72 6f 67 72 61 6d 3b 0a 20 20 20 20 20 .pProgram;.
1a6a7 20 20 20 20 20 70 53 75 62 2d 3e 66 6c 61 67 73 pSub->flags
1a6a8 20 7c 3d 20 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 |= MEM_Blob;.
1a6a9 20 20 20 20 20 20 20 20 70 53 75 62 2d 3e 6e 20 pSub->n
1a6aa 3d 20 6e 53 75 62 2a 73 69 7a 65 6f 66 28 53 75 = nSub*sizeof(Su
1a6ab 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20 bProgram*);.
1a6ac 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
1a6ad 20 20 7d 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 }.. pMem->f
1a6ae 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
1a6af 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 pMem->u.i =
1a6b0 70 4f 70 2d 3e 70 31 3b 20 20 20 20 20 20 20 20 pOp->p1;
1a6b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a6b2 20 20 2f 2a 20 50 31 20 2a 2f 0a 20 20 20 20 70 /* P1 */. p
1a6b3 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
1a6b4 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 TE_INTEGER;.
1a6b5 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 70 4d 65 pMem++;.. pMe
1a6b6 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 m->flags = MEM_I
1a6b7 6e 74 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e nt;. pMem->u.
1a6b8 69 20 3d 20 70 4f 70 2d 3e 70 32 3b 20 20 20 20 i = pOp->p2;
1a6b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a6ba 20 20 20 20 20 20 2f 2a 20 50 32 20 2a 2f 0a 20 /* P2 */.
1a6bb 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
1a6bc 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a SQLITE_INTEGER;.
1a6bd 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 pMem++;..
1a6be 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d if( p->explain=
1a6bf 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d =1 ){. pMem
1a6c0 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e ->flags = MEM_In
1a6c1 74 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 t;. pMem->u
1a6c2 2e 69 20 3d 20 70 4f 70 2d 3e 70 33 3b 20 20 20 .i = pOp->p3;
1a6c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a6c4 20 20 20 20 20 20 20 2f 2a 20 50 33 20 2a 2f 0a /* P3 */.
1a6c5 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 pMem->type
1a6c6 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 = SQLITE_INTEGE
1a6c7 52 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b R;. pMem++;
1a6c8 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
1a6c9 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
1a6ca 6f 77 28 70 4d 65 6d 2c 20 33 32 2c 20 30 29 20 ow(pMem, 32, 0)
1a6cb 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ){ /*
1a6cc 20 50 34 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 P4 */. ass
1a6cd 65 72 74 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c ert( p->db->mall
1a6ce 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 ocFailed );.
1a6cf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1a6d0 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 ERROR;. }.
1a6d1 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
1a6d2 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d EM_Dyn|MEM_Str|M
1a6d3 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 7a 20 3d EM_Term;. z =
1a6d4 20 64 69 73 70 6c 61 79 50 34 28 70 4f 70 2c 20 displayP4(pOp,
1a6d5 70 4d 65 6d 2d 3e 7a 2c 20 33 32 29 3b 0a 20 20 pMem->z, 32);.
1a6d6 20 20 69 66 28 20 7a 21 3d 70 4d 65 6d 2d 3e 7a if( z!=pMem->z
1a6d7 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1a6d8 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 3VdbeMemSetStr(p
1a6d9 4d 65 6d 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 Mem, z, -1, SQLI
1a6da 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 20 TE_UTF8, 0);.
1a6db 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 }else{. as
1a6dc 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 sert( pMem->z!=0
1a6dd 20 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e );. pMem->
1a6de 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 n = sqlite3Strle
1a6df 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 n30(pMem->z);.
1a6e0 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 pMem->enc =
1a6e1 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 SQLITE_UTF8;.
1a6e2 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 }. pMem->typ
1a6e3 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b e = SQLITE_TEXT;
1a6e4 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 . pMem++;..
1a6e5 20 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e if( p->explain
1a6e6 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ==1 ){. if(
1a6e7 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 sqlite3VdbeMemG
1a6e8 72 6f 77 28 70 4d 65 6d 2c 20 34 2c 20 30 29 20 row(pMem, 4, 0)
1a6e9 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 ){. asser
1a6ea 74 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 t( p->db->malloc
1a6eb 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 Failed );.
1a6ec 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1a6ed 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 ERROR;. }.
1a6ee 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
1a6ef 20 3d 20 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 = MEM_Dyn|MEM_S
1a6f0 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 tr|MEM_Term;.
1a6f1 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 32 3b 0a pMem->n = 2;.
1a6f2 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
1a6f3 70 72 69 6e 74 66 28 33 2c 20 70 4d 65 6d 2d 3e printf(3, pMem->
1a6f4 7a 2c 20 22 25 2e 32 78 22 2c 20 70 4f 70 2d 3e z, "%.2x", pOp->
1a6f5 70 35 29 3b 20 20 20 2f 2a 20 50 35 20 2a 2f 0a p5); /* P5 */.
1a6f6 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 pMem->type
1a6f7 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a = SQLITE_TEXT;.
1a6f8 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 pMem->enc
1a6f9 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 = SQLITE_UTF8;.
1a6fa 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a pMem++;. .
1a6fb 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
1a6fc 42 55 47 0a 20 20 20 20 20 20 69 66 28 20 70 4f BUG. if( pO
1a6fd 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 29 7b 0a 20 p->zComment ){.
1a6fe 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 pMem->fla
1a6ff 67 73 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d gs = MEM_Str|MEM
1a700 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 70 _Term;. p
1a701 4d 65 6d 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 7a 43 Mem->z = pOp->zC
1a702 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 20 20 20 20 omment;.
1a703 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 pMem->n = sqlite
1a704 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 3Strlen30(pMem->
1a705 7a 29 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d z);. pMem
1a706 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 ->enc = SQLITE_U
1a707 54 46 38 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 TF8;. pMe
1a708 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 m->type = SQLITE
1a709 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 7d 65 6c _TEXT;. }el
1a70a 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 se.#endif.
1a70b 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e {. pMem->
1a70c 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1a70d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1a70e 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 6d 65 /* Comme
1a70f 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4d nt */. pM
1a710 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 em->type = SQLIT
1a711 45 5f 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a E_NULL;. }.
1a712 20 20 20 20 7d 0a 0a 20 20 20 20 70 2d 3e 6e 52 }.. p->nR
1a713 65 73 43 6f 6c 75 6d 6e 20 3d 20 38 20 2d 20 35 esColumn = 8 - 5
1a714 2a 28 70 2d 3e 65 78 70 6c 61 69 6e 2d 31 29 3b *(p->explain-1);
1a715 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c . p->rc = SQL
1a716 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 63 20 3d ITE_OK;. rc =
1a717 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 7d SQLITE_ROW;. }
1a718 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1a719 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1a71a 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f _OMIT_EXPLAIN */
1a71b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
1a71c 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e DEBUG./*.** Prin
1a71d 74 20 74 68 65 20 53 51 4c 20 74 68 61 74 20 77 t the SQL that w
1a71e 61 73 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 as used to gener
1a71f 61 74 65 20 61 20 56 44 42 45 20 70 72 6f 67 72 ate a VDBE progr
1a720 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 am..*/.SQLITE_PR
1a721 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1a722 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 56 e3VdbePrintSql(V
1a723 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e dbe *p){. int n
1a724 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56 Op = p->nOp;. V
1a725 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 66 dbeOp *pOp;. if
1a726 28 20 6e 4f 70 3c 31 20 29 20 72 65 74 75 72 6e ( nOp<1 ) return
1a727 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f ;. pOp = &p->aO
1a728 70 5b 30 5d 3b 0a 20 20 69 66 28 20 70 4f 70 2d p[0];. if( pOp-
1a729 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 >opcode==OP_Trac
1a72a 65 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d e && pOp->p4.z!=
1a72b 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 0 ){. const c
1a72c 68 61 72 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70 34 har *z = pOp->p4
1a72d 2e 7a 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 .z;. while( s
1a72e 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a qlite3Isspace(*z
1a72f 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 70 72 69 ) ) z++;. pri
1a730 6e 74 66 28 22 53 51 4c 3a 20 5b 25 73 5d 5c 6e ntf("SQL: [%s]\n
1a731 22 2c 20 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e ", z);. }.}.#en
1a732 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 dif..#if !define
1a733 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 d(SQLITE_OMIT_TR
1a734 41 43 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 ACE) && defined(
1a735 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f SQLITE_ENABLE_IO
1a736 54 52 41 43 45 29 0a 2f 2a 0a 2a 2a 20 50 72 69 TRACE)./*.** Pri
1a737 6e 74 20 61 6e 20 49 4f 54 52 41 43 45 20 6d 65 nt an IOTRACE me
1a738 73 73 61 67 65 20 73 68 6f 77 69 6e 67 20 53 51 ssage showing SQ
1a739 4c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51 L content..*/.SQ
1a73a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1a73b 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 d sqlite3VdbeIOT
1a73c 72 61 63 65 53 71 6c 28 56 64 62 65 20 2a 70 29 raceSql(Vdbe *p)
1a73d 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70 2d {. int nOp = p-
1a73e 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20 2a >nOp;. VdbeOp *
1a73f 70 4f 70 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 pOp;. if( sqlit
1a740 65 33 49 6f 54 72 61 63 65 3d 3d 30 20 29 20 72 e3IoTrace==0 ) r
1a741 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 6e 4f 70 eturn;. if( nOp
1a742 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 <1 ) return;. p
1a743 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b Op = &p->aOp[0];
1a744 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f . if( pOp->opco
1a745 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 de==OP_Trace &&
1a746 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a pOp->p4.z!=0 ){.
1a747 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 int i, j;.
1a748 20 20 63 68 61 72 20 7a 5b 31 30 30 30 5d 3b 0a char z[1000];.
1a749 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1a74a 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 29 2c 20 intf(sizeof(z),
1a74b 7a 2c 20 22 25 73 22 2c 20 70 4f 70 2d 3e 70 34 z, "%s", pOp->p4
1a74c 2e 7a 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 .z);. for(i=0
1a74d 3b 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 ; sqlite3Isspace
1a74e 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 (z[i]); i++){}.
1a74f 20 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 5b 69 5d for(j=0; z[i]
1a750 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ; i++){. if
1a751 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 ( sqlite3Isspace
1a752 28 7a 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 (z[i]) ){.
1a753 20 20 69 66 28 20 7a 5b 69 2d 31 5d 21 3d 27 20 if( z[i-1]!='
1a754 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a ' ){. z
1a755 5b 6a 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20 [j++] = ' ';.
1a756 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
1a757 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b se{. z[j+
1a758 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 20 +] = z[i];.
1a759 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 5b 6a }. }. z[j
1a75a 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 ] = 0;. sqlit
1a75b 65 33 49 6f 54 72 61 63 65 28 22 53 51 4c 20 25 e3IoTrace("SQL %
1a75c 73 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d 0a 7d 0a s\n", z);. }.}.
1a75d 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 #endif /* !SQLIT
1a75e 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 26 26 20 E_OMIT_TRACE &&
1a75f 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f SQLITE_ENABLE_IO
1a760 54 52 41 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 TRACE */../*.**
1a761 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 Allocate space f
1a762 72 6f 6d 20 61 20 66 69 78 65 64 20 73 69 7a 65 rom a fixed size
1a763 20 62 75 66 66 65 72 2e 20 20 4d 61 6b 65 20 2a buffer. Make *
1a764 70 70 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a pp point to the.
1a765 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 ** allocated spa
1a766 63 65 2e 20 20 28 4e 6f 74 65 3a 20 20 70 70 20 ce. (Note: pp
1a767 69 73 20 61 20 63 68 61 72 2a 20 72 61 74 68 65 is a char* rathe
1a768 72 20 74 68 61 6e 20 61 20 76 6f 69 64 2a 2a 20 r than a void**
1a769 74 6f 0a 2a 2a 20 77 6f 72 6b 20 61 72 6f 75 6e to.** work aroun
1a76a 64 20 74 68 65 20 70 6f 69 6e 74 65 72 20 61 6c d the pointer al
1a76b 69 61 73 69 6e 67 20 72 75 6c 65 73 20 6f 66 20 iasing rules of
1a76c 43 2e 29 20 20 2a 70 70 20 73 68 6f 75 6c 64 20 C.) *pp should
1a76d 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a 20 62 65 20 initially.** be
1a76e 7a 65 72 6f 2e 20 20 49 66 20 2a 70 70 20 69 73 zero. If *pp is
1a76f 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 61 74 20 not zero, that
1a770 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 73 means that the s
1a771 70 61 63 65 20 68 61 73 20 61 6c 72 65 61 64 79 pace has already
1a772 0a 2a 2a 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 .** been allocat
1a773 65 64 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 ed and this rout
1a774 69 6e 65 20 69 73 20 61 20 6e 6f 6f 70 2e 0a 2a ine is a noop..*
1a775 2a 0a 2a 2a 20 6e 42 79 74 65 20 69 73 20 74 68 *.** nByte is th
1a776 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
1a777 73 20 6f 66 20 73 70 61 63 65 20 6e 65 65 64 65 s of space neede
1a778 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 46 72 6f 6d d..**.** *ppFrom
1a779 20 70 6f 69 6e 74 20 74 6f 20 61 76 61 69 6c 61 point to availa
1a77a 62 6c 65 20 73 70 61 63 65 20 61 6e 64 20 70 45 ble space and pE
1a77b 6e 64 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 nd points to the
1a77c 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 61 end of the.** a
1a77d 76 61 69 6c 61 62 6c 65 20 73 70 61 63 65 2e 0a vailable space..
1a77e 2a 2a 0a 2a 2a 20 2a 70 6e 42 79 74 65 20 69 73 **.** *pnByte is
1a77f 20 61 20 63 6f 75 6e 74 65 72 20 6f 66 20 74 68 a counter of th
1a780 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
1a781 73 20 6f 66 20 73 70 61 63 65 20 74 68 61 74 20 s of space that
1a782 68 61 76 65 20 66 61 69 6c 65 64 0a 2a 2a 20 74 have failed.** t
1a783 6f 20 61 6c 6c 6f 63 61 74 65 2e 20 20 49 66 20 o allocate. If
1a784 74 68 65 72 65 20 69 73 20 69 6e 73 75 66 66 69 there is insuffi
1a785 63 69 65 6e 74 20 73 70 61 63 65 20 69 6e 20 2a cient space in *
1a786 70 70 46 72 6f 6d 20 74 6f 20 73 61 74 69 73 66 ppFrom to satisf
1a787 79 20 74 68 65 0a 2a 2a 20 72 65 71 75 65 73 74 y the.** request
1a788 2c 20 74 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 , then increment
1a789 20 2a 70 6e 42 79 74 65 20 62 79 20 74 68 65 20 *pnByte by the
1a78a 61 6d 6f 75 6e 74 20 6f 66 20 74 68 65 20 72 65 amount of the re
1a78b 71 75 65 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 quest..*/.static
1a78c 20 76 6f 69 64 20 61 6c 6c 6f 63 53 70 61 63 65 void allocSpace
1a78d 28 0a 20 20 63 68 61 72 20 2a 70 70 2c 20 20 20 (. char *pp,
1a78e 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f /* IN/O
1a78f 55 54 3a 20 53 65 74 20 2a 70 70 20 74 6f 20 70 UT: Set *pp to p
1a790 6f 69 6e 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 oint to allocate
1a791 64 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e d buffer */. in
1a792 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 t nByte,
1a793 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1a794 62 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 bytes to allocat
1a795 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 70 70 46 72 e */. u8 **ppFr
1a796 6f 6d 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 49 om, /* I
1a797 4e 2f 4f 55 54 3a 20 41 6c 6c 6f 63 61 74 65 20 N/OUT: Allocate
1a798 66 72 6f 6d 20 2a 70 70 46 72 6f 6d 20 2a 2f 0a from *ppFrom */.
1a799 20 20 75 38 20 2a 70 45 6e 64 2c 20 20 20 20 20 u8 *pEnd,
1a79a 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
1a79b 72 20 74 6f 20 31 20 62 79 74 65 20 70 61 73 74 r to 1 byte past
1a79c 20 74 68 65 20 65 6e 64 20 6f 66 20 2a 70 70 46 the end of *ppF
1a79d 72 6f 6d 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 rom buffer */.
1a79e 69 6e 74 20 2a 70 6e 42 79 74 65 20 20 20 20 20 int *pnByte
1a79f 20 20 20 20 20 2f 2a 20 49 66 20 61 6c 6c 6f 63 /* If alloc
1a7a0 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 ation cannot be
1a7a1 6d 61 64 65 2c 20 69 6e 63 72 65 6d 65 6e 74 20 made, increment
1a7a2 2a 70 6e 42 79 74 65 20 2a 2f 0a 29 7b 0a 20 20 *pnByte */.){.
1a7a3 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 assert( EIGHT_BY
1a7a4 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 2a 70 70 TE_ALIGNMENT(*pp
1a7a5 46 72 6f 6d 29 20 29 3b 0a 20 20 69 66 28 20 28 From) );. if( (
1a7a6 2a 28 76 6f 69 64 2a 2a 29 70 70 29 3d 3d 30 20 *(void**)pp)==0
1a7a7 29 7b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 52 ){. nByte = R
1a7a8 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 OUND8(nByte);.
1a7a9 20 20 69 66 28 20 26 28 2a 70 70 46 72 6f 6d 29 if( &(*ppFrom)
1a7aa 5b 6e 42 79 74 65 5d 20 3c 3d 20 70 45 6e 64 20 [nByte] <= pEnd
1a7ab 29 7b 0a 20 20 20 20 20 20 2a 28 76 6f 69 64 2a ){. *(void*
1a7ac 2a 29 70 70 20 3d 20 28 76 6f 69 64 20 2a 29 2a *)pp = (void *)*
1a7ad 70 70 46 72 6f 6d 3b 0a 20 20 20 20 20 20 2a 70 ppFrom;. *p
1a7ae 70 46 72 6f 6d 20 2b 3d 20 6e 42 79 74 65 3b 0a pFrom += nByte;.
1a7af 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1a7b0 20 2a 70 6e 42 79 74 65 20 2b 3d 20 6e 42 79 74 *pnByte += nByt
1a7b1 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a e;. }. }.}..
1a7b2 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 61 20 /*.** Prepare a
1a7b3 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 virtual machine
1a7b4 66 6f 72 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 for execution.
1a7b5 54 68 69 73 20 69 6e 76 6f 6c 76 65 73 20 74 68 This involves th
1a7b6 69 6e 67 73 20 73 75 63 68 0a 2a 2a 20 61 73 20 ings such.** as
1a7b7 61 6c 6c 6f 63 61 74 69 6e 67 20 73 74 61 63 6b allocating stack
1a7b8 20 73 70 61 63 65 20 61 6e 64 20 69 6e 69 74 69 space and initi
1a7b9 61 6c 69 7a 69 6e 67 20 74 68 65 20 70 72 6f 67 alizing the prog
1a7ba 72 61 6d 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 20 ram counter..**
1a7bb 41 66 74 65 72 20 74 68 65 20 56 44 42 45 20 68 After the VDBE h
1a7bc 61 73 20 62 65 20 70 72 65 70 70 65 64 2c 20 69 as be prepped, i
1a7bd 74 20 63 61 6e 20 62 65 20 65 78 65 63 75 74 65 t can be execute
1a7be 64 20 62 79 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 d by one or more
1a7bf 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c .** calls to sql
1a7c0 69 74 65 33 56 64 62 65 45 78 65 63 28 29 2e 20 ite3VdbeExec().
1a7c1 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 .**.** This is
1a7c2 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 the only way to
1a7c3 6d 6f 76 65 20 61 20 56 44 42 45 20 66 72 6f 6d move a VDBE from
1a7c4 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 VDBE_MAGIC_INIT
1a7c5 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 to.** VDBE_MAGI
1a7c6 43 5f 52 55 4e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 C_RUN..**.** Thi
1a7c7 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 s function may b
1a7c8 65 20 63 61 6c 6c 65 64 20 6d 6f 72 65 20 74 68 e called more th
1a7c9 61 6e 20 6f 6e 63 65 20 6f 6e 20 61 20 73 69 6e an once on a sin
1a7ca 67 6c 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 gle virtual mach
1a7cb 69 6e 65 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 ine..** The firs
1a7cc 74 20 63 61 6c 6c 20 69 73 20 6d 61 64 65 20 77 t call is made w
1a7cd 68 69 6c 65 20 63 6f 6d 70 69 6c 69 6e 67 20 74 hile compiling t
1a7ce 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
1a7cf 2e 20 53 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 . Subsequent.**
1a7d0 63 61 6c 6c 73 20 61 72 65 20 6d 61 64 65 20 61 calls are made a
1a7d1 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 70 72 s part of the pr
1a7d2 6f 63 65 73 73 20 6f 66 20 72 65 73 65 74 74 69 ocess of resetti
1a7d3 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 ng a statement t
1a7d4 6f 20 62 65 0a 2a 2a 20 72 65 2d 65 78 65 63 75 o be.** re-execu
1a7d5 74 65 64 20 28 66 72 6f 6d 20 61 20 63 61 6c 6c ted (from a call
1a7d6 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 65 to sqlite3_rese
1a7d7 74 28 29 29 2e 20 54 68 65 20 6e 56 61 72 2c 20 t()). The nVar,
1a7d8 6e 4d 65 6d 2c 20 6e 43 75 72 73 6f 72 20 0a 2a nMem, nCursor .*
1a7d9 2a 20 61 6e 64 20 69 73 45 78 70 6c 61 69 6e 20 * and isExplain
1a7da 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 6f parameters are o
1a7db 6e 6c 79 20 70 61 73 73 65 64 20 63 6f 72 72 65 nly passed corre
1a7dc 63 74 20 76 61 6c 75 65 73 20 74 68 65 20 66 69 ct values the fi
1a7dd 72 73 74 20 74 69 6d 65 0a 2a 2a 20 74 68 65 20 rst time.** the
1a7de 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
1a7df 65 64 2e 20 4f 6e 20 73 75 62 73 65 71 75 65 6e ed. On subsequen
1a7e0 74 20 63 61 6c 6c 73 2c 20 66 72 6f 6d 20 73 71 t calls, from sq
1a7e1 6c 69 74 65 33 5f 72 65 73 65 74 28 29 2c 20 6e lite3_reset(), n
1a7e2 56 61 72 0a 2a 2a 20 69 73 20 70 61 73 73 65 64 Var.** is passed
1a7e3 20 2d 31 20 61 6e 64 20 6e 4d 65 6d 2c 20 6e 43 -1 and nMem, nC
1a7e4 75 72 73 6f 72 20 61 6e 64 20 69 73 45 78 70 6c ursor and isExpl
1a7e5 61 69 6e 20 61 72 65 20 61 6c 6c 20 70 61 73 73 ain are all pass
1a7e6 65 64 20 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 ed zero..*/.SQLI
1a7e7 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1a7e8 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 sqlite3VdbeMakeR
1a7e9 65 61 64 79 28 0a 20 20 56 64 62 65 20 2a 70 2c eady(. Vdbe *p,
1a7ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a7eb 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 /* The VD
1a7ec 42 45 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 BE */. int nVar
1a7ed 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1a7ee 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1a7ef 20 6f 66 20 27 3f 27 20 73 65 65 20 69 6e 20 74 of '?' see in t
1a7f0 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
1a7f1 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 2c 20 */. int nMem,
1a7f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a7f3 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1a7f4 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 74 f memory cells t
1a7f5 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 o allocate */.
1a7f6 69 6e 74 20 6e 43 75 72 73 6f 72 2c 20 20 20 20 int nCursor,
1a7f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1a7f8 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 * Number of curs
1a7f9 6f 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 ors to allocate
1a7fa 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20 */. int nArg,
1a7fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a7fc 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e /* Maximum n
1a7fd 75 6d 62 65 72 20 6f 66 20 61 72 67 73 20 69 6e umber of args in
1a7fe 20 53 75 62 50 72 6f 67 72 61 6d 73 20 2a 2f 0a SubPrograms */.
1a7ff 20 20 69 6e 74 20 69 73 45 78 70 6c 61 69 6e 2c int isExplain,
1a800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a801 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 /* True if the
1a802 45 58 50 4c 41 49 4e 20 6b 65 79 77 6f 72 64 73 EXPLAIN keywords
1a803 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 is present */.
1a804 20 69 6e 74 20 75 73 65 73 53 74 6d 74 4a 6f 75 int usesStmtJou
1a805 72 6e 61 6c 20 20 20 20 20 20 20 20 20 20 20 20 rnal
1a806 2f 2a 20 54 72 75 65 20 74 6f 20 73 65 74 20 56 /* True to set V
1a807 64 62 65 2e 75 73 65 73 53 74 6d 74 4a 6f 75 72 dbe.usesStmtJour
1a808 6e 61 6c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 nal */.){. int
1a809 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 n;. sqlite3 *db
1a80a 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 = p->db;.. ass
1a80b 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 ert( p!=0 );. a
1a80c 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d ssert( p->magic=
1a80d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 =VDBE_MAGIC_INIT
1a80e 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 72 65 20 );.. /* There
1a80f 73 68 6f 75 6c 64 20 62 65 20 61 74 20 6c 65 61 should be at lea
1a810 73 74 20 6f 6e 65 20 6f 70 63 6f 64 65 2e 0a 20 st one opcode..
1a811 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d */. assert( p-
1a812 3e 6e 4f 70 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 >nOp>0 );.. /*
1a813 53 65 74 20 74 68 65 20 6d 61 67 69 63 20 74 6f Set the magic to
1a814 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 VDBE_MAGIC_RUN
1a815 73 6f 6f 6e 65 72 20 72 61 74 68 65 72 20 74 68 sooner rather th
1a816 61 6e 20 6c 61 74 65 72 2e 20 2a 2f 0a 20 20 70 an later. */. p
1a817 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d ->magic = VDBE_M
1a818 41 47 49 43 5f 52 55 4e 3b 0a 0a 20 20 2f 2a 20 AGIC_RUN;.. /*
1a819 46 6f 72 20 65 61 63 68 20 63 75 72 73 6f 72 20 For each cursor
1a81a 72 65 71 75 69 72 65 64 2c 20 61 6c 73 6f 20 61 required, also a
1a81b 6c 6c 6f 63 61 74 65 20 61 20 6d 65 6d 6f 72 79 llocate a memory
1a81c 20 63 65 6c 6c 2e 20 4d 65 6d 6f 72 79 0a 20 20 cell. Memory.
1a81d 2a 2a 20 63 65 6c 6c 73 20 28 6e 4d 65 6d 2b 31 ** cells (nMem+1
1a81e 2d 6e 43 75 72 73 6f 72 29 2e 2e 6e 4d 65 6d 2c -nCursor)..nMem,
1a81f 20 69 6e 63 6c 75 73 69 76 65 2c 20 77 69 6c 6c inclusive, will
1a820 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 20 62 never be used b
1a821 79 0a 20 20 2a 2a 20 74 68 65 20 76 64 62 65 20 y. ** the vdbe
1a822 70 72 6f 67 72 61 6d 2e 20 49 6e 73 74 65 61 64 program. Instead
1a823 20 74 68 65 79 20 61 72 65 20 75 73 65 64 20 74 they are used t
1a824 6f 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 o allocate space
1a825 20 66 6f 72 0a 20 20 2a 2a 20 56 64 62 65 43 75 for. ** VdbeCu
1a826 72 73 6f 72 2f 42 74 43 75 72 73 6f 72 20 73 74 rsor/BtCursor st
1a827 72 75 63 74 75 72 65 73 2e 20 54 68 65 20 62 6c ructures. The bl
1a828 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 ob of memory ass
1a829 6f 63 69 61 74 65 64 20 77 69 74 68 20 0a 20 20 ociated with .
1a82a 2a 2a 20 63 75 72 73 6f 72 20 30 20 69 73 20 73 ** cursor 0 is s
1a82b 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 tored in memory
1a82c 63 65 6c 6c 20 6e 4d 65 6d 2e 20 4d 65 6d 6f 72 cell nMem. Memor
1a82d 79 20 63 65 6c 6c 20 28 6e 4d 65 6d 2d 31 29 0a y cell (nMem-1).
1a82e 20 20 2a 2a 20 73 74 6f 72 65 73 20 74 68 65 20 ** stores the
1a82f 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 blob of memory a
1a830 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 63 ssociated with c
1a831 75 72 73 6f 72 20 31 2c 20 65 74 63 2e 0a 20 20 ursor 1, etc..
1a832 2a 2a 0a 20 20 2a 2a 20 53 65 65 20 61 6c 73 6f **. ** See also
1a833 3a 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 : allocateCursor
1a834 28 29 2e 0a 20 20 2a 2f 0a 20 20 6e 4d 65 6d 20 ().. */. nMem
1a835 2b 3d 20 6e 43 75 72 73 6f 72 3b 0a 0a 20 20 2f += nCursor;.. /
1a836 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 * Allocate space
1a837 20 66 6f 72 20 6d 65 6d 6f 72 79 20 72 65 67 69 for memory regi
1a838 73 74 65 72 73 2c 20 53 51 4c 20 76 61 72 69 61 sters, SQL varia
1a839 62 6c 65 73 2c 20 56 44 42 45 20 63 75 72 73 6f bles, VDBE curso
1a83a 72 73 20 61 6e 64 20 0a 20 20 2a 2a 20 61 6e 20 rs and . ** an
1a83b 61 72 72 61 79 20 74 6f 20 6d 61 72 73 68 61 6c array to marshal
1a83c 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 61 72 SQL function ar
1a83d 67 75 6d 65 6e 74 73 20 69 6e 2e 20 54 68 69 73 guments in. This
1a83e 20 69 73 20 6f 6e 6c 79 20 64 6f 6e 65 20 74 68 is only done th
1a83f 65 0a 20 20 2a 2a 20 66 69 72 73 74 20 74 69 6d e. ** first tim
1a840 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 e this function
1a841 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 20 is called for a
1a842 67 69 76 65 6e 20 56 44 42 45 2c 20 6e 6f 74 20 given VDBE, not
1a843 77 68 65 6e 20 69 74 20 69 73 0a 20 20 2a 2a 20 when it is. **
1a844 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f being called fro
1a845 6d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 m sqlite3_reset(
1a846 29 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 76 ) to reset the v
1a847 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a irtual machine..
1a848 20 20 2a 2f 0a 20 20 69 66 28 20 6e 56 61 72 3e */. if( nVar>
1a849 3d 30 20 26 26 20 41 4c 57 41 59 53 28 64 62 2d =0 && ALWAYS(db-
1a84a 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 >mallocFailed==0
1a84b 29 20 29 7b 0a 20 20 20 20 75 38 20 2a 7a 43 73 ) ){. u8 *zCs
1a84c 72 20 3d 20 28 75 38 20 2a 29 26 70 2d 3e 61 4f r = (u8 *)&p->aO
1a84d 70 5b 70 2d 3e 6e 4f 70 5d 3b 0a 20 20 20 20 75 p[p->nOp];. u
1a84e 38 20 2a 7a 45 6e 64 20 3d 20 28 75 38 20 2a 29 8 *zEnd = (u8 *)
1a84f 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 41 6c &p->aOp[p->nOpAl
1a850 6c 6f 63 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 42 loc];. int nB
1a851 79 74 65 3b 0a 20 20 20 20 72 65 73 6f 6c 76 65 yte;. resolve
1a852 50 32 56 61 6c 75 65 73 28 70 2c 20 26 6e 41 72 P2Values(p, &nAr
1a853 67 29 3b 0a 20 20 20 20 70 2d 3e 75 73 65 73 53 g);. p->usesS
1a854 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 28 75 38 tmtJournal = (u8
1a855 29 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c )usesStmtJournal
1a856 3b 0a 20 20 20 20 69 66 28 20 69 73 45 78 70 6c ;. if( isExpl
1a857 61 69 6e 20 26 26 20 6e 4d 65 6d 3c 31 30 20 29 ain && nMem<10 )
1a858 7b 0a 20 20 20 20 20 20 6e 4d 65 6d 20 3d 20 31 {. nMem = 1
1a859 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 0;. }. mem
1a85a 73 65 74 28 7a 43 73 72 2c 20 30 2c 20 7a 45 6e set(zCsr, 0, zEn
1a85b 64 2d 7a 43 73 72 29 3b 0a 20 20 20 20 7a 43 73 d-zCsr);. zCs
1a85c 72 20 2b 3d 20 28 7a 43 73 72 20 2d 20 28 75 38 r += (zCsr - (u8
1a85d 2a 29 30 29 26 37 3b 0a 20 20 20 20 61 73 73 65 *)0)&7;. asse
1a85e 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 rt( EIGHT_BYTE_A
1a85f 4c 49 47 4e 4d 45 4e 54 28 7a 43 73 72 29 20 29 LIGNMENT(zCsr) )
1a860 3b 0a 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 ;.. do {.
1a861 20 20 6e 42 79 74 65 20 3d 20 30 3b 0a 20 20 20 nByte = 0;.
1a862 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 allocSpace((c
1a863 68 61 72 2a 29 26 70 2d 3e 61 4d 65 6d 2c 20 6e har*)&p->aMem, n
1a864 4d 65 6d 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 2c Mem*sizeof(Mem),
1a865 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e &zCsr, zEnd, &n
1a866 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c Byte);. all
1a867 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 26 ocSpace((char*)&
1a868 70 2d 3e 61 56 61 72 2c 20 6e 56 61 72 2a 73 69 p->aVar, nVar*si
1a869 7a 65 6f 66 28 4d 65 6d 29 2c 20 26 7a 43 73 72 zeof(Mem), &zCsr
1a86a 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b , zEnd, &nByte);
1a86b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 . allocSpac
1a86c 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 70 41 e((char*)&p->apA
1a86d 72 67 2c 20 6e 41 72 67 2a 73 69 7a 65 6f 66 28 rg, nArg*sizeof(
1a86e 4d 65 6d 2a 29 2c 20 26 7a 43 73 72 2c 20 7a 45 Mem*), &zCsr, zE
1a86f 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 nd, &nByte);.
1a870 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 allocSpace((c
1a871 68 61 72 2a 29 26 70 2d 3e 61 7a 56 61 72 2c 20 har*)&p->azVar,
1a872 6e 56 61 72 2a 73 69 7a 65 6f 66 28 63 68 61 72 nVar*sizeof(char
1a873 2a 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c *), &zCsr, zEnd,
1a874 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 &nByte);.
1a875 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72 allocSpace((char
1a876 2a 29 26 70 2d 3e 61 70 43 73 72 2c 20 0a 20 20 *)&p->apCsr, .
1a877 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e n
1a878 43 75 72 73 6f 72 2a 73 69 7a 65 6f 66 28 56 64 Cursor*sizeof(Vd
1a879 62 65 43 75 72 73 6f 72 2a 29 2c 20 26 7a 43 73 beCursor*), &zCs
1a87a 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 0a r, zEnd, &nByte.
1a87b 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 69 );. i
1a87c 66 28 20 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 f( nByte ){.
1a87d 20 20 20 20 70 2d 3e 70 46 72 65 65 20 3d 20 73 p->pFree = s
1a87e 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 qlite3DbMallocZe
1a87f 72 6f 28 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 ro(db, nByte);.
1a880 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 43 73 }. zCs
1a881 72 20 3d 20 70 2d 3e 70 46 72 65 65 3b 0a 20 20 r = p->pFree;.
1a882 20 20 20 20 7a 45 6e 64 20 3d 20 26 7a 43 73 72 zEnd = &zCsr
1a883 5b 6e 42 79 74 65 5d 3b 0a 20 20 20 20 7d 77 68 [nByte];. }wh
1a884 69 6c 65 28 20 6e 42 79 74 65 20 26 26 20 21 64 ile( nByte && !d
1a885 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1a886 29 3b 0a 0a 20 20 20 20 70 2d 3e 6e 43 75 72 73 );.. p->nCurs
1a887 6f 72 20 3d 20 28 75 31 36 29 6e 43 75 72 73 6f or = (u16)nCurso
1a888 72 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 61 56 r;. if( p->aV
1a889 61 72 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e ar ){. p->n
1a88a 56 61 72 20 3d 20 28 75 31 36 29 6e 56 61 72 3b Var = (u16)nVar;
1a88b 0a 20 20 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20 . for(n=0;
1a88c 6e 3c 6e 56 61 72 3b 20 6e 2b 2b 29 7b 0a 20 20 n<nVar; n++){.
1a88d 20 20 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d p->aVar[n]
1a88e 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c .flags = MEM_Nul
1a88f 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 56 l;. p->aV
1a890 61 72 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 ar[n].db = db;.
1a891 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1a892 20 69 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a if( p->aMem ){.
1a893 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d 2d 2d 3b p->aMem--;
1a894 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a895 20 20 20 20 20 20 2f 2a 20 61 4d 65 6d 5b 5d 20 /* aMem[]
1a896 67 6f 65 73 20 66 72 6f 6d 20 31 2e 2e 6e 4d 65 goes from 1..nMe
1a897 6d 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 6e 4d m */. p->nM
1a898 65 6d 20 3d 20 6e 4d 65 6d 3b 20 20 20 20 20 20 em = nMem;
1a899 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 /*
1a89a 20 20 20 20 6e 6f 74 20 66 72 6f 6d 20 30 2e 2e not from 0..
1a89b 6e 4d 65 6d 2d 31 20 2a 2f 0a 20 20 20 20 20 20 nMem-1 */.
1a89c 66 6f 72 28 6e 3d 31 3b 20 6e 3c 3d 6e 4d 65 6d for(n=1; n<=nMem
1a89d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; n++){.
1a89e 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 66 6c 61 67 73 p->aMem[n].flags
1a89f 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 = MEM_Null;.
1a8a0 20 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e p->aMem[n].
1a8a1 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d db = db;. }
1a8a2 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 64 65 . }. }.#ifde
1a8a3 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
1a8a4 20 66 6f 72 28 6e 3d 31 3b 20 6e 3c 70 2d 3e 6e for(n=1; n<p->n
1a8a5 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 61 Mem; n++){. a
1a8a6 73 73 65 72 74 28 20 70 2d 3e 61 4d 65 6d 5b 6e ssert( p->aMem[n
1a8a7 5d 2e 64 62 3d 3d 64 62 20 29 3b 0a 20 20 7d 0a ].db==db );. }.
1a8a8 23 65 6e 64 69 66 0a 0a 20 20 70 2d 3e 70 63 20 #endif.. p->pc
1a8a9 3d 20 2d 31 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 = -1;. p->rc =
1a8aa 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 2d 3e SQLITE_OK;. p->
1a8ab 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 errorAction = OE
1a8ac 5f 41 62 6f 72 74 3b 0a 20 20 70 2d 3e 65 78 70 _Abort;. p->exp
1a8ad 6c 61 69 6e 20 7c 3d 20 69 73 45 78 70 6c 61 69 lain |= isExplai
1a8ae 6e 3b 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 n;. p->magic =
1a8af 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a VDBE_MAGIC_RUN;.
1a8b0 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 p->nChange = 0
1a8b1 3b 0a 20 20 70 2d 3e 63 61 63 68 65 43 74 72 20 ;. p->cacheCtr
1a8b2 3d 20 31 3b 0a 20 20 70 2d 3e 6d 69 6e 57 72 69 = 1;. p->minWri
1a8b3 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 3d 20 32 teFileFormat = 2
1a8b4 35 35 3b 0a 20 20 70 2d 3e 69 53 74 61 74 65 6d 55;. p->iStatem
1a8b5 65 6e 74 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 ent = 0;.#ifdef
1a8b6 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b VDBE_PROFILE. {
1a8b7 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
1a8b8 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f for(i=0; i<p->nO
1a8b9 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 p; i++){. p
1a8ba 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 ->aOp[i].cnt = 0
1a8bb 3b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 ;. p->aOp[i
1a8bc 5d 2e 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20 ].cycles = 0;.
1a8bd 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d }. }.#endif.}
1a8be 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 ../*.** Close a
1a8bf 56 44 42 45 20 63 75 72 73 6f 72 20 61 6e 64 20 VDBE cursor and
1a8c0 72 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20 release all the
1a8c1 72 65 73 6f 75 72 63 65 73 20 74 68 61 74 20 63 resources that c
1a8c2 75 72 73 6f 72 20 0a 2a 2a 20 68 61 70 70 65 6e ursor .** happen
1a8c3 73 20 74 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 s to hold..*/.SQ
1a8c4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1a8c5 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 d sqlite3VdbeFre
1a8c6 65 43 75 72 73 6f 72 28 56 64 62 65 20 2a 70 2c eCursor(Vdbe *p,
1a8c7 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 VdbeCursor *pCx
1a8c8 29 7b 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 ){. if( pCx==0
1a8c9 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 ){. return;.
1a8ca 20 7d 0a 20 20 69 66 28 20 70 43 78 2d 3e 70 42 }. if( pCx->pB
1a8cb 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
1a8cc 42 74 72 65 65 43 6c 6f 73 65 28 70 43 78 2d 3e BtreeClose(pCx->
1a8cd 70 42 74 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 pBt);. /* The
1a8ce 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 77 69 pCx->pCursor wi
1a8cf 6c 6c 20 62 65 20 63 6c 6f 73 65 20 61 75 74 6f ll be close auto
1a8d0 6d 61 74 69 63 61 6c 6c 79 2c 20 69 66 20 69 74 matically, if it
1a8d1 20 65 78 69 73 74 73 2c 20 62 79 0a 20 20 20 20 exists, by.
1a8d2 2a 2a 20 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 ** the call abov
1a8d3 65 2e 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 e. */. }else if
1a8d4 28 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 29 ( pCx->pCursor )
1a8d5 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 {. sqlite3Btr
1a8d6 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 43 eeCloseCursor(pC
1a8d7 78 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d x->pCursor);. }
1a8d8 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1a8d9 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1a8da 45 0a 20 20 69 66 28 20 70 43 78 2d 3e 70 56 74 E. if( pCx->pVt
1a8db 61 62 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 abCursor ){.
1a8dc 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 sqlite3_vtab_cur
1a8dd 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 sor *pVtabCursor
1a8de 20 3d 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72 = pCx->pVtabCur
1a8df 73 6f 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 sor;. const s
1a8e0 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 qlite3_module *p
1a8e1 4d 6f 64 75 6c 65 20 3d 20 70 43 78 2d 3e 70 4d Module = pCx->pM
1a8e2 6f 64 75 6c 65 3b 0a 20 20 20 20 70 2d 3e 69 6e odule;. p->in
1a8e3 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a VtabMethod = 1;.
1a8e4 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 (void)sqlite
1a8e5 33 53 61 66 65 74 79 4f 66 66 28 70 2d 3e 64 62 3SafetyOff(p->db
1a8e6 29 3b 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e );. pModule->
1a8e7 78 43 6c 6f 73 65 28 70 56 74 61 62 43 75 72 73 xClose(pVtabCurs
1a8e8 6f 72 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 or);. (void)s
1a8e9 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 70 qlite3SafetyOn(p
1a8ea 2d 3e 64 62 29 3b 0a 20 20 20 20 70 2d 3e 69 6e ->db);. p->in
1a8eb 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a VtabMethod = 0;.
1a8ec 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a }.#endif.}../*
1a8ed 0a 2a 2a 20 43 6f 70 79 20 74 68 65 20 76 61 6c .** Copy the val
1a8ee 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 ues stored in th
1a8ef 65 20 56 64 62 65 46 72 61 6d 65 20 73 74 72 75 e VdbeFrame stru
1a8f0 63 74 75 72 65 20 74 6f 20 69 74 73 20 56 64 62 cture to its Vdb
1a8f1 65 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 e. This.** is us
1a8f2 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c ed, for example,
1a8f3 20 77 68 65 6e 20 61 20 74 72 69 67 67 65 72 20 when a trigger
1a8f4 73 75 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 68 sub-program is h
1a8f5 61 6c 74 65 64 20 74 6f 20 72 65 73 74 6f 72 65 alted to restore
1a8f6 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 .** control to t
1a8f7 68 65 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 2e he main program.
1a8f8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1a8f9 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
1a8fa 62 65 46 72 61 6d 65 52 65 73 74 6f 72 65 28 56 beFrameRestore(V
1a8fb 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 dbeFrame *pFrame
1a8fc 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 ){. Vdbe *v = p
1a8fd 46 72 61 6d 65 2d 3e 76 3b 0a 20 20 76 2d 3e 61 Frame->v;. v->a
1a8fe 4f 70 20 3d 20 70 46 72 61 6d 65 2d 3e 61 4f 70 Op = pFrame->aOp
1a8ff 3b 0a 20 20 76 2d 3e 6e 4f 70 20 3d 20 70 46 72 ;. v->nOp = pFr
1a900 61 6d 65 2d 3e 6e 4f 70 3b 0a 20 20 76 2d 3e 61 ame->nOp;. v->a
1a901 4d 65 6d 20 3d 20 70 46 72 61 6d 65 2d 3e 61 4d Mem = pFrame->aM
1a902 65 6d 3b 0a 20 20 76 2d 3e 6e 4d 65 6d 20 3d 20 em;. v->nMem =
1a903 70 46 72 61 6d 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 pFrame->nMem;.
1a904 76 2d 3e 61 70 43 73 72 20 3d 20 70 46 72 61 6d v->apCsr = pFram
1a905 65 2d 3e 61 70 43 73 72 3b 0a 20 20 76 2d 3e 6e e->apCsr;. v->n
1a906 43 75 72 73 6f 72 20 3d 20 70 46 72 61 6d 65 2d Cursor = pFrame-
1a907 3e 6e 43 75 72 73 6f 72 3b 0a 20 20 76 2d 3e 64 >nCursor;. v->d
1a908 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 b->lastRowid = p
1a909 46 72 61 6d 65 2d 3e 6c 61 73 74 52 6f 77 69 64 Frame->lastRowid
1a90a 3b 0a 20 20 76 2d 3e 6e 43 68 61 6e 67 65 20 3d ;. v->nChange =
1a90b 20 70 46 72 61 6d 65 2d 3e 6e 43 68 61 6e 67 65 pFrame->nChange
1a90c 3b 0a 20 20 72 65 74 75 72 6e 20 70 46 72 61 6d ;. return pFram
1a90d 65 2d 3e 70 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 e->pc;.}../*.**
1a90e 43 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 Close all cursor
1a90f 73 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 72 65 s..**.** Also re
1a910 6c 65 61 73 65 20 61 6e 79 20 64 79 6e 61 6d 69 lease any dynami
1a911 63 20 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 c memory held by
1a912 20 74 68 65 20 56 4d 20 69 6e 20 74 68 65 20 56 the VM in the V
1a913 64 62 65 2e 61 4d 65 6d 20 6d 65 6d 6f 72 79 20 dbe.aMem memory
1a914 0a 2a 2a 20 63 65 6c 6c 20 61 72 72 61 79 2e 20 .** cell array.
1a915 54 68 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 This is necessar
1a916 79 20 61 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 y as the memory
1a917 63 65 6c 6c 20 61 72 72 61 79 20 6d 61 79 20 63 cell array may c
1a918 6f 6e 74 61 69 6e 0a 2a 2a 20 70 6f 69 6e 74 65 ontain.** pointe
1a919 72 73 20 74 6f 20 56 64 62 65 46 72 61 6d 65 20 rs to VdbeFrame
1a91a 6f 62 6a 65 63 74 73 2c 20 77 68 69 63 68 20 6d objects, which m
1a91b 61 79 20 69 6e 20 74 75 72 6e 20 63 6f 6e 74 61 ay in turn conta
1a91c 69 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a in pointers to.*
1a91d 2a 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 2e 0a * open cursors..
1a91e 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
1a91f 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72 73 28 56 loseAllCursors(V
1a920 64 62 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 dbe *p){. if( p
1a921 2d 3e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 ->pFrame ){.
1a922 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d VdbeFrame *pFram
1a923 65 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 e = p->pFrame;.
1a924 20 20 20 66 6f 72 28 70 46 72 61 6d 65 3d 70 2d for(pFrame=p-
1a925 3e 70 46 72 61 6d 65 3b 20 70 46 72 61 6d 65 2d >pFrame; pFrame-
1a926 3e 70 50 61 72 65 6e 74 3b 20 70 46 72 61 6d 65 >pParent; pFrame
1a927 3d 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 =pFrame->pParent
1a928 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
1a929 62 65 46 72 61 6d 65 52 65 73 74 6f 72 65 28 70 beFrameRestore(p
1a92a 46 72 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 70 2d Frame);. }. p-
1a92b 3e 70 46 72 61 6d 65 20 3d 20 30 3b 0a 20 20 70 >pFrame = 0;. p
1a92c 2d 3e 6e 46 72 61 6d 65 20 3d 20 30 3b 0a 0a 20 ->nFrame = 0;..
1a92d 20 69 66 28 20 70 2d 3e 61 70 43 73 72 20 29 7b if( p->apCsr ){
1a92e 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
1a92f 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 for(i=0; i<p->nC
1a930 75 72 73 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 ursor; i++){.
1a931 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 VdbeCursor *p
1a932 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b C = p->apCsr[i];
1a933 0a 20 20 20 20 20 20 69 66 28 20 70 43 20 29 7b . if( pC ){
1a934 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1a935 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 VdbeFreeCursor(p
1a936 2c 20 70 43 29 3b 0a 20 20 20 20 20 20 20 20 70 , pC);. p
1a937 2d 3e 61 70 43 73 72 5b 69 5d 20 3d 20 30 3b 0a ->apCsr[i] = 0;.
1a938 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1a939 7d 0a 20 20 69 66 28 20 70 2d 3e 61 4d 65 6d 20 }. if( p->aMem
1a93a 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 4d 65 ){. releaseMe
1a93b 6d 41 72 72 61 79 28 26 70 2d 3e 61 4d 65 6d 5b mArray(&p->aMem[
1a93c 31 5d 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 1], p->nMem);.
1a93d 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e }.}../*.** Clean
1a93e 20 75 70 20 74 68 65 20 56 4d 20 61 66 74 65 72 up the VM after
1a93f 20 65 78 65 63 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a execution..**.*
1a940 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 * This routine w
1a941 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c ill automaticall
1a942 79 20 63 6c 6f 73 65 20 61 6e 79 20 63 75 72 73 y close any curs
1a943 6f 72 73 2c 20 6c 69 73 74 73 2c 20 61 6e 64 2f ors, lists, and/
1a944 6f 72 0a 2a 2a 20 73 6f 72 74 65 72 73 20 74 68 or.** sorters th
1a945 61 74 20 77 65 72 65 20 6c 65 66 74 20 6f 70 65 at were left ope
1a946 6e 2e 20 20 49 74 20 61 6c 73 6f 20 64 65 6c 65 n. It also dele
1a947 74 65 73 20 74 68 65 20 76 61 6c 75 65 73 20 6f tes the values o
1a948 66 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 69 f.** variables i
1a949 6e 20 74 68 65 20 61 56 61 72 5b 5d 20 61 72 72 n the aVar[] arr
1a94a 61 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ay..*/.static vo
1a94b 69 64 20 43 6c 65 61 6e 75 70 28 56 64 62 65 20 id Cleanup(Vdbe
1a94c 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a *p){. sqlite3 *
1a94d 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 23 69 66 db = p->db;..#if
1a94e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
1a94f 0a 20 20 2f 2a 20 45 78 65 63 75 74 65 20 61 73 . /* Execute as
1a950 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
1a951 73 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 s to ensure that
1a952 20 74 68 65 20 56 64 62 65 2e 61 70 43 73 72 5b the Vdbe.apCsr[
1a953 5d 20 61 6e 64 20 0a 20 20 2a 2a 20 56 64 62 65 ] and . ** Vdbe
1a954 2e 61 4d 65 6d 5b 5d 20 61 72 72 61 79 73 20 68 .aMem[] arrays h
1a955 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e ave already been
1a956 20 63 6c 65 61 6e 65 64 20 75 70 2e 20 20 2a 2f cleaned up. */
1a957 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 . int i;. for(
1a958 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 73 6f i=0; i<p->nCurso
1a959 72 3b 20 69 2b 2b 29 20 61 73 73 65 72 74 28 20 r; i++) assert(
1a95a 70 2d 3e 61 70 43 73 72 3d 3d 30 20 7c 7c 20 70 p->apCsr==0 || p
1a95b 2d 3e 61 70 43 73 72 5b 69 5d 3d 3d 30 20 29 3b ->apCsr[i]==0 );
1a95c 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 70 . for(i=1; i<=p
1a95d 2d 3e 6e 4d 65 6d 3b 20 69 2b 2b 29 20 61 73 73 ->nMem; i++) ass
1a95e 65 72 74 28 20 70 2d 3e 61 4d 65 6d 3d 3d 30 20 ert( p->aMem==0
1a95f 7c 7c 20 70 2d 3e 61 4d 65 6d 5b 69 5d 2e 66 6c || p->aMem[i].fl
1a960 61 67 73 3d 3d 4d 45 4d 5f 4e 75 6c 6c 20 29 3b ags==MEM_Null );
1a961 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 .#endif.. sqlit
1a962 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
1a963 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a zErrMsg);. p->z
1a964 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 70 2d ErrMsg = 0;. p-
1a965 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 30 3b >pResultSet = 0;
1a966 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 .}../*.** Set th
1a967 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 e number of resu
1a968 6c 74 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 lt columns that
1a969 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 will be returned
1a96a 20 62 79 20 74 68 69 73 20 53 51 4c 0a 2a 2a 20 by this SQL.**
1a96b 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 statement. This
1a96c 69 73 20 6e 6f 77 20 73 65 74 20 61 74 20 63 6f is now set at co
1a96d 6d 70 69 6c 65 20 74 69 6d 65 2c 20 72 61 74 68 mpile time, rath
1a96e 65 72 20 74 68 61 6e 20 64 75 72 69 6e 67 0a 2a er than during.*
1a96f 2a 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 * execution of t
1a970 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 he vdbe program
1a971 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f so that sqlite3_
1a972 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 29 20 63 column_count() c
1a973 61 6e 0a 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 an.** be called
1a974 6f 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d on an SQL statem
1a975 65 6e 74 20 62 65 66 6f 72 65 20 73 71 6c 69 74 ent before sqlit
1a976 65 33 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 e3_step()..*/.SQ
1a977 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1a978 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 d sqlite3VdbeSet
1a979 4e 75 6d 43 6f 6c 73 28 56 64 62 65 20 2a 70 2c NumCols(Vdbe *p,
1a97a 20 69 6e 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 29 int nResColumn)
1a97b 7b 0a 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d {. Mem *pColNam
1a97c 65 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 e;. int n;. sq
1a97d 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 lite3 *db = p->d
1a97e 62 3b 0a 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d b;.. releaseMem
1a97f 41 72 72 61 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d Array(p->aColNam
1a980 65 2c 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e e, p->nResColumn
1a981 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73 *COLNAME_N);. s
1a982 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1a983 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 p->aColName);.
1a984 20 6e 20 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e 2a n = nResColumn*
1a985 43 4f 4c 4e 41 4d 45 5f 4e 3b 0a 20 20 70 2d 3e COLNAME_N;. p->
1a986 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d 20 28 75 31 nResColumn = (u1
1a987 36 29 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 20 20 6)nResColumn;.
1a988 70 2d 3e 61 43 6f 6c 4e 61 6d 65 20 3d 20 70 43 p->aColName = pC
1a989 6f 6c 4e 61 6d 65 20 3d 20 28 4d 65 6d 2a 29 73 olName = (Mem*)s
1a98a 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 qlite3DbMallocZe
1a98b 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 4d 65 ro(db, sizeof(Me
1a98c 6d 29 2a 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d m)*n );. if( p-
1a98d 3e 61 43 6f 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 >aColName==0 ) r
1a98e 65 74 75 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20 eturn;. while(
1a98f 6e 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 70 n-- > 0 ){. p
1a990 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 3d ColName->flags =
1a991 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 MEM_Null;. p
1a992 43 6f 6c 4e 61 6d 65 2d 3e 64 62 20 3d 20 70 2d ColName->db = p-
1a993 3e 64 62 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d >db;. pColNam
1a994 65 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a e++;. }.}../*.*
1a995 2a 20 53 65 74 20 74 68 65 20 6e 61 6d 65 20 6f * Set the name o
1a996 66 20 74 68 65 20 69 64 78 27 74 68 20 63 6f 6c f the idx'th col
1a997 75 6d 6e 20 74 6f 20 62 65 20 72 65 74 75 72 6e umn to be return
1a998 65 64 20 62 79 20 74 68 65 20 53 51 4c 20 73 74 ed by the SQL st
1a999 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d atement..** zNam
1a99a 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e e must be a poin
1a99b 74 65 72 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 ter to a nul ter
1a99c 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 2e 0a minated string..
1a99d 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 **.** This call
1a99e 6d 75 73 74 20 62 65 20 6d 61 64 65 20 61 66 74 must be made aft
1a99f 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c er a call to sql
1a9a0 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f ite3VdbeSetNumCo
1a9a1 6c 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ls()..**.** The
1a9a2 66 69 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 2c final parameter,
1a9a3 20 78 44 65 6c 2c 20 6d 75 73 74 20 62 65 20 6f xDel, must be o
1a9a4 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 44 59 4e ne of SQLITE_DYN
1a9a5 41 4d 49 43 2c 20 53 51 4c 49 54 45 5f 53 54 41 AMIC, SQLITE_STA
1a9a6 54 49 43 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 TIC.** or SQLITE
1a9a7 5f 54 52 41 4e 53 49 45 4e 54 2e 20 49 66 20 69 _TRANSIENT. If i
1a9a8 74 20 69 73 20 53 51 4c 49 54 45 5f 44 59 4e 41 t is SQLITE_DYNA
1a9a9 4d 49 43 2c 20 74 68 65 6e 20 74 68 65 20 62 75 MIC, then the bu
1a9aa 66 66 65 72 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 ffer pointed.**
1a9ab 74 6f 20 62 79 20 7a 4e 61 6d 65 20 77 69 6c 6c to by zName will
1a9ac 20 62 65 20 66 72 65 65 64 20 62 79 20 73 71 6c be freed by sql
1a9ad 69 74 65 33 44 62 46 72 65 65 28 29 20 77 68 65 ite3DbFree() whe
1a9ae 6e 20 74 68 65 20 76 64 62 65 20 69 73 20 64 65 n the vdbe is de
1a9af 73 74 72 6f 79 65 64 2e 0a 2a 2f 0a 53 51 4c 49 stroyed..*/.SQLI
1a9b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1a9b1 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c qlite3VdbeSetCol
1a9b2 4e 61 6d 65 28 0a 20 20 56 64 62 65 20 2a 70 2c Name(. Vdbe *p,
1a9b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a9b4 20 20 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65 /* Vdbe
1a9b5 20 62 65 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 being configure
1a9b6 64 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 2c 20 d */. int idx,
1a9b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a9b8 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
1a9b9 20 6f 66 20 63 6f 6c 75 6d 6e 20 7a 4e 61 6d 65 of column zName
1a9ba 20 61 70 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 applies to */.
1a9bb 20 69 6e 74 20 76 61 72 2c 20 20 20 20 20 20 20 int var,
1a9bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a9bd 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 /* One of the
1a9be 43 4f 4c 4e 41 4d 45 5f 2a 20 63 6f 6e 73 74 61 COLNAME_* consta
1a9bf 6e 74 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 nts */. const c
1a9c0 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 har *zName,
1a9c1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
1a9c2 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72 20 63 nter to buffer c
1a9c3 6f 6e 74 61 69 6e 69 6e 67 20 6e 61 6d 65 20 2a ontaining name *
1a9c4 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 /. void (*xDel)
1a9c5 28 76 6f 69 64 2a 29 20 20 20 20 20 20 20 20 20 (void*)
1a9c6 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d /* Memory m
1a9c7 61 6e 61 67 65 6d 65 6e 74 20 73 74 72 61 74 65 anagement strate
1a9c8 67 79 20 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a gy for zName */.
1a9c9 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d ){. int rc;. M
1a9ca 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 em *pColName;.
1a9cb 61 73 73 65 72 74 28 20 69 64 78 3c 70 2d 3e 6e assert( idx<p->n
1a9cc 52 65 73 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 61 ResColumn );. a
1a9cd 73 73 65 72 74 28 20 76 61 72 3c 43 4f 4c 4e 41 ssert( var<COLNA
1a9ce 4d 45 5f 4e 20 29 3b 0a 20 20 69 66 28 20 70 2d ME_N );. if( p-
1a9cf 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
1a9d0 64 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 d ){. assert(
1a9d1 20 21 7a 4e 61 6d 65 20 7c 7c 20 78 44 65 6c 21 !zName || xDel!
1a9d2 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 =SQLITE_DYNAMIC
1a9d3 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
1a9d4 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
1a9d5 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 43 6f assert( p->aCo
1a9d6 6c 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 70 43 lName!=0 );. pC
1a9d7 6f 6c 4e 61 6d 65 20 3d 20 26 28 70 2d 3e 61 43 olName = &(p->aC
1a9d8 6f 6c 4e 61 6d 65 5b 69 64 78 2b 76 61 72 2a 70 olName[idx+var*p
1a9d9 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 5d 29 3b 0a ->nResColumn]);.
1a9da 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1a9db 62 65 4d 65 6d 53 65 74 53 74 72 28 70 43 6f 6c beMemSetStr(pCol
1a9dc 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c Name, zName, -1,
1a9dd 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 78 44 SQLITE_UTF8, xD
1a9de 65 6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 el);. assert( r
1a9df 63 21 3d 30 20 7c 7c 20 21 7a 4e 61 6d 65 20 7c c!=0 || !zName |
1a9e0 7c 20 28 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 | (pColName->fla
1a9e1 67 73 26 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20 gs&MEM_Term)!=0
1a9e2 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
1a9e3 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 72 65 61 64 20 }../*.** A read
1a9e4 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 or write transac
1a9e5 74 69 6f 6e 20 6d 61 79 20 6f 72 20 6d 61 79 20 tion may or may
1a9e6 6e 6f 74 20 62 65 20 61 63 74 69 76 65 20 6f 6e not be active on
1a9e7 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
1a9e8 0a 2a 2a 20 64 62 2e 20 49 66 20 61 20 74 72 61 .** db. If a tra
1a9e9 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 nsaction is acti
1a9ea 76 65 2c 20 63 6f 6d 6d 69 74 20 69 74 2e 20 49 ve, commit it. I
1a9eb 66 20 74 68 65 72 65 20 69 73 20 61 0a 2a 2a 20 f there is a.**
1a9ec 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
1a9ed 6e 20 73 70 61 6e 6e 69 6e 67 20 6d 6f 72 65 20 n spanning more
1a9ee 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 than one databas
1a9ef 65 20 66 69 6c 65 2c 20 74 68 69 73 20 72 6f 75 e file, this rou
1a9f0 74 69 6e 65 0a 2a 2a 20 74 61 6b 65 73 20 63 61 tine.** takes ca
1a9f1 72 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 re of the master
1a9f2 20 6a 6f 75 72 6e 61 6c 20 74 72 69 63 6b 65 72 journal tricker
1a9f3 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 y..*/.static int
1a9f4 20 76 64 62 65 43 6f 6d 6d 69 74 28 73 71 6c 69 vdbeCommit(sqli
1a9f5 74 65 33 20 2a 64 62 2c 20 56 64 62 65 20 2a 70 te3 *db, Vdbe *p
1a9f6 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e ){. int i;. in
1a9f7 74 20 6e 54 72 61 6e 73 20 3d 20 30 3b 20 20 2f t nTrans = 0; /
1a9f8 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 61 74 61 * Number of data
1a9f9 62 61 73 65 73 20 77 69 74 68 20 61 6e 20 61 63 bases with an ac
1a9fa 74 69 76 65 20 77 72 69 74 65 2d 74 72 61 6e 73 tive write-trans
1a9fb 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 action */. int
1a9fc 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1a9fd 20 20 69 6e 74 20 6e 65 65 64 58 63 6f 6d 6d 69 int needXcommi
1a9fe 74 20 3d 20 30 3b 0a 0a 23 69 66 64 65 66 20 53 t = 0;..#ifdef S
1a9ff 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1aa00 41 4c 54 41 42 4c 45 0a 20 20 2f 2a 20 57 69 74 ALTABLE. /* Wit
1aa01 68 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 73 h this option, s
1aa02 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 29 qlite3VtabSync()
1aa03 20 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62 is defined to b
1aa04 65 20 73 69 6d 70 6c 79 20 0a 20 20 2a 2a 20 53 e simply . ** S
1aa05 51 4c 49 54 45 5f 4f 4b 20 73 6f 20 70 20 69 73 QLITE_OK so p is
1aa06 20 6e 6f 74 20 75 73 65 64 2e 20 0a 20 20 2a 2f not used. . */
1aa07 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
1aa08 54 45 52 28 70 29 3b 0a 23 65 6e 64 69 66 0a 0a TER(p);.#endif..
1aa09 20 20 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e /* Before doin
1aa0a 67 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c g anything else,
1aa0b 20 63 61 6c 6c 20 74 68 65 20 78 53 79 6e 63 28 call the xSync(
1aa0c 29 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 ) callback for a
1aa0d 6e 79 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 ny. ** virtual
1aa0e 6d 6f 64 75 6c 65 20 74 61 62 6c 65 73 20 77 72 module tables wr
1aa0f 69 74 74 65 6e 20 69 6e 20 74 68 69 73 20 74 72 itten in this tr
1aa10 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 ansaction. This
1aa11 68 61 73 20 74 6f 0a 20 20 2a 2a 20 62 65 20 64 has to. ** be d
1aa12 6f 6e 65 20 62 65 66 6f 72 65 20 64 65 74 65 72 one before deter
1aa13 6d 69 6e 69 6e 67 20 77 68 65 74 68 65 72 20 61 mining whether a
1aa14 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1aa15 66 69 6c 65 20 69 73 20 0a 20 20 2a 2a 20 72 65 file is . ** re
1aa16 71 75 69 72 65 64 2c 20 61 73 20 61 6e 20 78 53 quired, as an xS
1aa17 79 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b 20 6d ync() callback m
1aa18 61 79 20 61 64 64 20 61 6e 20 61 74 74 61 63 68 ay add an attach
1aa19 65 64 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a ed database. **
1aa1a 20 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 to the transact
1aa1b 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d ion.. */. rc =
1aa1c 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 sqlite3VtabSync
1aa1d 28 64 62 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 (db, &p->zErrMsg
1aa1e 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
1aa1f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 ITE_OK ){. re
1aa20 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 turn rc;. }..
1aa21 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 64 65 74 /* This loop det
1aa22 65 72 6d 69 6e 65 73 20 28 61 29 20 69 66 20 74 ermines (a) if t
1aa23 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73 he commit hook s
1aa24 68 6f 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 hould be invoked
1aa25 20 61 6e 64 0a 20 20 2a 2a 20 28 62 29 20 68 6f and. ** (b) ho
1aa26 77 20 6d 61 6e 79 20 64 61 74 61 62 61 73 65 20 w many database
1aa27 66 69 6c 65 73 20 68 61 76 65 20 6f 70 65 6e 20 files have open
1aa28 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f write transactio
1aa29 6e 73 2c 20 6e 6f 74 20 0a 20 20 2a 2a 20 69 6e ns, not . ** in
1aa2a 63 6c 75 64 69 6e 67 20 74 68 65 20 74 65 6d 70 cluding the temp
1aa2b 20 64 61 74 61 62 61 73 65 2e 20 28 62 29 20 69 database. (b) i
1aa2c 73 20 69 6d 70 6f 72 74 61 6e 74 20 62 65 63 61 s important beca
1aa2d 75 73 65 20 69 66 20 6d 6f 72 65 20 74 68 61 6e use if more than
1aa2e 20 0a 20 20 2a 2a 20 6f 6e 65 20 64 61 74 61 62 . ** one datab
1aa2f 61 73 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20 ase file has an
1aa30 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 open write trans
1aa31 61 63 74 69 6f 6e 2c 20 61 20 6d 61 73 74 65 72 action, a master
1aa32 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 journal. ** fi
1aa33 6c 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 le is required f
1aa34 6f 72 20 61 6e 20 61 74 6f 6d 69 63 20 63 6f 6d or an atomic com
1aa35 6d 69 74 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 mit.. */ . for
1aa36 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b (i=0; i<db->nDb;
1aa37 20 69 2b 2b 29 7b 20 0a 20 20 20 20 42 74 72 65 i++){ . Btre
1aa38 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 e *pBt = db->aDb
1aa39 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 [i].pBt;. if(
1aa3a 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 sqlite3BtreeIsI
1aa3b 6e 54 72 61 6e 73 28 70 42 74 29 20 29 7b 0a 20 nTrans(pBt) ){.
1aa3c 20 20 20 20 20 6e 65 65 64 58 63 6f 6d 6d 69 74 needXcommit
1aa3d 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20 = 1;. if(
1aa3e 69 21 3d 31 20 29 20 6e 54 72 61 6e 73 2b 2b 3b i!=1 ) nTrans++;
1aa3f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
1aa40 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 6e If there are an
1aa41 79 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 y write-transact
1aa42 69 6f 6e 73 20 61 74 20 61 6c 6c 2c 20 69 6e 76 ions at all, inv
1aa43 6f 6b 65 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 oke the commit h
1aa44 6f 6f 6b 20 2a 2f 0a 20 20 69 66 28 20 6e 65 65 ook */. if( nee
1aa45 64 58 63 6f 6d 6d 69 74 20 26 26 20 64 62 2d 3e dXcommit && db->
1aa46 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20 xCommitCallback
1aa47 29 7b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c ){. (void)sql
1aa48 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 ite3SafetyOff(db
1aa49 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 62 2d 3e );. rc = db->
1aa4a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 28 xCommitCallback(
1aa4b 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 29 3b db->pCommitArg);
1aa4c 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 . (void)sqlit
1aa4d 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a e3SafetyOn(db);.
1aa4e 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
1aa4f 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1aa50 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 E_CONSTRAINT;.
1aa51 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 }. }.. /* Th
1aa52 65 20 73 69 6d 70 6c 65 20 63 61 73 65 20 2d 20 e simple case -
1aa53 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 no more than one
1aa54 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 database file (
1aa55 6e 6f 74 20 63 6f 75 6e 74 69 6e 67 20 74 68 65 not counting the
1aa56 0a 20 20 2a 2a 20 54 45 4d 50 20 64 61 74 61 62 . ** TEMP datab
1aa57 61 73 65 29 20 68 61 73 20 61 20 74 72 61 6e 73 ase) has a trans
1aa58 61 63 74 69 6f 6e 20 61 63 74 69 76 65 2e 20 20 action active.
1aa59 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 There is no nee
1aa5a 64 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d d for the. ** m
1aa5b 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 aster-journal..
1aa5c 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 **. ** If the
1aa5d 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 return value of
1aa5e 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 sqlite3BtreeGetF
1aa5f 69 6c 65 6e 61 6d 65 28 29 20 69 73 20 61 20 7a ilename() is a z
1aa60 65 72 6f 20 6c 65 6e 67 74 68 0a 20 20 2a 2a 20 ero length. **
1aa61 73 74 72 69 6e 67 2c 20 69 74 20 6d 65 61 6e 73 string, it means
1aa62 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 the main databa
1aa63 73 65 20 69 73 20 3a 6d 65 6d 6f 72 79 3a 20 6f se is :memory: o
1aa64 72 20 61 20 74 65 6d 70 20 66 69 6c 65 2e 20 20 r a temp file.
1aa65 49 6e 20 0a 20 20 2a 2a 20 74 68 61 74 20 63 61 In . ** that ca
1aa66 73 65 20 77 65 20 64 6f 20 6e 6f 74 20 73 75 70 se we do not sup
1aa67 70 6f 72 74 20 61 74 6f 6d 69 63 20 6d 75 6c 74 port atomic mult
1aa68 69 2d 66 69 6c 65 20 63 6f 6d 6d 69 74 73 2c 20 i-file commits,
1aa69 73 6f 20 75 73 65 20 74 68 65 20 0a 20 20 2a 2a so use the . **
1aa6a 20 73 69 6d 70 6c 65 20 63 61 73 65 20 74 68 65 simple case the
1aa6b 6e 20 74 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 n too.. */. if
1aa6c 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 6c ( 0==sqlite3Strl
1aa6d 65 6e 33 30 28 73 71 6c 69 74 65 33 42 74 72 65 en30(sqlite3Btre
1aa6e 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d eGetFilename(db-
1aa6f 3e 61 44 62 5b 30 5d 2e 70 42 74 29 29 0a 20 20 >aDb[0].pBt)).
1aa70 20 7c 7c 20 6e 54 72 61 6e 73 3c 3d 31 0a 20 20 || nTrans<=1.
1aa71 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 ){. for(i=0;
1aa72 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1aa73 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 i<db->nDb; i++)
1aa74 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 {. Btree *p
1aa75 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e Bt = db->aDb[i].
1aa76 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 pBt;. if( p
1aa77 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 Bt ){. rc
1aa78 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 = sqlite3BtreeC
1aa79 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 42 ommitPhaseOne(pB
1aa7a 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 t, 0);. }.
1aa7b 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 }.. /* Do
1aa7c 74 68 65 20 63 6f 6d 6d 69 74 20 6f 6e 6c 79 20 the commit only
1aa7d 69 66 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 if all databases
1aa7e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f successfully co
1aa7f 6d 70 6c 65 74 65 20 70 68 61 73 65 20 31 2e 20 mplete phase 1.
1aa80 0a 20 20 20 20 2a 2a 20 49 66 20 6f 6e 65 20 6f . ** If one o
1aa81 66 20 74 68 65 20 42 74 72 65 65 43 6f 6d 6d 69 f the BtreeCommi
1aa82 74 50 68 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c tPhaseOne() call
1aa83 73 20 66 61 69 6c 73 2c 20 74 68 69 73 20 69 6e s fails, this in
1aa84 64 69 63 61 74 65 73 20 61 6e 0a 20 20 20 20 2a dicates an. *
1aa85 2a 20 49 4f 20 65 72 72 6f 72 20 77 68 69 6c 65 * IO error while
1aa86 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 74 72 75 deleting or tru
1aa87 6e 63 61 74 69 6e 67 20 61 20 6a 6f 75 72 6e 61 ncating a journa
1aa88 6c 20 66 69 6c 65 2e 20 49 74 20 69 73 20 75 6e l file. It is un
1aa89 6c 69 6b 65 6c 79 2c 0a 20 20 20 20 2a 2a 20 62 likely,. ** b
1aa8a 75 74 20 63 6f 75 6c 64 20 68 61 70 70 65 6e 2e ut could happen.
1aa8b 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 62 In this case ab
1aa8c 61 6e 64 6f 6e 20 70 72 6f 63 65 73 73 69 6e 67 andon processing
1aa8d 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 and return the
1aa8e 65 72 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 error.. */.
1aa8f 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 for(i=0; rc==S
1aa90 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 QLITE_OK && i<db
1aa91 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nDb; i++){.
1aa92 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 Btree *pBt =
1aa93 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a db->aDb[i].pBt;.
1aa94 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b if( pBt ){
1aa95 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
1aa96 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 lite3BtreeCommit
1aa97 50 68 61 73 65 54 77 6f 28 70 42 74 29 3b 0a 20 PhaseTwo(pBt);.
1aa98 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1aa99 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1aa9a 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 OK ){. sqli
1aa9b 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 64 62 te3VtabCommit(db
1aa9c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
1aa9d 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 78 20 63 /* The complex c
1aa9e 61 73 65 20 2d 20 54 68 65 72 65 20 69 73 20 61 ase - There is a
1aa9f 20 6d 75 6c 74 69 2d 66 69 6c 65 20 77 72 69 74 multi-file writ
1aaa0 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 e-transaction ac
1aaa1 74 69 76 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 tive.. ** This
1aaa2 72 65 71 75 69 72 65 73 20 61 20 6d 61 73 74 65 requires a maste
1aaa3 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 r journal file t
1aaa4 6f 20 65 6e 73 75 72 65 20 74 68 65 20 74 72 61 o ensure the tra
1aaa5 6e 73 61 63 74 69 6f 6e 20 69 73 0a 20 20 2a 2a nsaction is. **
1aaa6 20 63 6f 6d 6d 69 74 74 65 64 20 61 74 6f 6d 69 committed atomi
1aaa7 63 6c 79 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 cly.. */.#ifnde
1aaa8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 f SQLITE_OMIT_DI
1aaa9 53 4b 49 4f 0a 20 20 65 6c 73 65 7b 0a 20 20 20 SKIO. else{.
1aaaa 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
1aaab 66 73 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a 20 fs = db->pVfs;.
1aaac 20 20 20 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 int needSync
1aaad 3d 20 30 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a = 0;. char *z
1aaae 4d 61 73 74 65 72 20 3d 20 30 3b 20 20 20 2f 2a Master = 0; /*
1aaaf 20 46 69 6c 65 2d 6e 61 6d 65 20 66 6f 72 20 74 File-name for t
1aab0 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
1aab1 6c 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 63 6f l */. char co
1aab2 6e 73 74 20 2a 7a 4d 61 69 6e 46 69 6c 65 20 3d nst *zMainFile =
1aab3 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
1aab4 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 Filename(db->aDb
1aab5 5b 30 5d 2e 70 42 74 29 3b 0a 20 20 20 20 73 71 [0].pBt);. sq
1aab6 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4d 61 73 lite3_file *pMas
1aab7 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 69 36 34 ter = 0;. i64
1aab8 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 offset = 0;.
1aab9 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 20 20 2f int res;.. /
1aaba 2a 20 53 65 6c 65 63 74 20 61 20 6d 61 73 74 65 * Select a maste
1aabb 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e r journal file n
1aabc 61 6d 65 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a ame */. do {.
1aabd 20 20 20 20 20 20 75 33 32 20 69 52 61 6e 64 6f u32 iRando
1aabe 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 m;. sqlite3
1aabf 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 DbFree(db, zMast
1aac0 65 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 er);. sqlit
1aac1 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 e3_randomness(si
1aac2 7a 65 6f 66 28 69 52 61 6e 64 6f 6d 29 2c 20 26 zeof(iRandom), &
1aac3 69 52 61 6e 64 6f 6d 29 3b 0a 20 20 20 20 20 20 iRandom);.
1aac4 7a 4d 61 73 74 65 72 20 3d 20 73 71 6c 69 74 65 zMaster = sqlite
1aac5 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 3MPrintf(db, "%s
1aac6 2d 6d 6a 25 30 38 58 22 2c 20 7a 4d 61 69 6e 46 -mj%08X", zMainF
1aac7 69 6c 65 2c 20 69 52 61 6e 64 6f 6d 26 30 78 37 ile, iRandom&0x7
1aac8 66 66 66 66 66 66 66 29 3b 0a 20 20 20 20 20 20 fffffff);.
1aac9 69 66 28 20 21 7a 4d 61 73 74 65 72 20 29 7b 0a if( !zMaster ){.
1aaca 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
1aacb 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
1aacc 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
1aacd 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 sqlite3OsAccess(
1aace 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 53 pVfs, zMaster, S
1aacf 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 QLITE_ACCESS_EXI
1aad0 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 STS, &res);.
1aad1 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 }while( rc==SQLI
1aad2 54 45 5f 4f 4b 20 26 26 20 72 65 73 20 29 3b 0a TE_OK && res );.
1aad3 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1aad4 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f TE_OK ){. /
1aad5 2a 20 4f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 * Open the maste
1aad6 72 20 6a 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 r journal. */.
1aad7 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1aad8 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 70 56 66 OsOpenMalloc(pVf
1aad9 73 2c 20 7a 4d 61 73 74 65 72 2c 20 26 70 4d 61 s, zMaster, &pMa
1aada 73 74 65 72 2c 20 0a 20 20 20 20 20 20 20 20 20 ster, .
1aadb 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
1aadc 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 DWRITE|SQLITE_OP
1aadd 45 4e 5f 43 52 45 41 54 45 7c 0a 20 20 20 20 20 EN_CREATE|.
1aade 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e SQLITE_OPEN
1aadf 5f 45 58 43 4c 55 53 49 56 45 7c 53 51 4c 49 54 _EXCLUSIVE|SQLIT
1aae0 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f E_OPEN_MASTER_JO
1aae1 55 52 4e 41 4c 2c 20 30 0a 20 20 20 20 20 20 29 URNAL, 0. )
1aae2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1aae3 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1aae4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
1aae5 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 Free(db, zMaster
1aae6 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1aae7 72 63 3b 0a 20 20 20 20 7d 0a 20 0a 20 20 20 20 rc;. }. .
1aae8 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 61 6d /* Write the nam
1aae9 65 20 6f 66 20 65 61 63 68 20 64 61 74 61 62 61 e of each databa
1aaea 73 65 20 66 69 6c 65 20 69 6e 20 74 68 65 20 74 se file in the t
1aaeb 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 74 6f 20 ransaction into
1aaec 74 68 65 20 6e 65 77 0a 20 20 20 20 2a 2a 20 6d the new. ** m
1aaed 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
1aaee 6c 65 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 le. If an error
1aaef 6f 63 63 75 72 73 20 61 74 20 74 68 69 73 20 70 occurs at this p
1aaf0 6f 69 6e 74 20 63 6c 6f 73 65 0a 20 20 20 20 2a oint close. *
1aaf1 2a 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 * and delete the
1aaf2 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1aaf3 66 69 6c 65 2e 20 41 6c 6c 20 74 68 65 20 69 6e file. All the in
1aaf4 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c dividual journal
1aaf5 20 66 69 6c 65 73 0a 20 20 20 20 2a 2a 20 73 74 files. ** st
1aaf6 69 6c 6c 20 68 61 76 65 20 27 6e 75 6c 6c 27 20 ill have 'null'
1aaf7 61 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f as the master jo
1aaf8 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 2c 20 73 urnal pointer, s
1aaf9 6f 20 74 68 65 79 20 77 69 6c 6c 20 72 6f 6c 6c o they will roll
1aafa 0a 20 20 20 20 2a 2a 20 62 61 63 6b 20 69 6e 64 . ** back ind
1aafb 65 70 65 6e 64 65 6e 74 6c 79 20 69 66 20 61 20 ependently if a
1aafc 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 2e 0a failure occurs..
1aafd 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 */. for(i
1aafe 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 =0; i<db->nDb; i
1aaff 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65 ++){. Btree
1ab00 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b *pBt = db->aDb[
1ab01 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 i].pBt;. if
1ab02 28 20 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 ( i==1 ) continu
1ab03 65 3b 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 e; /* Ignore t
1ab04 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 he TEMP database
1ab05 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 71 */. if( sq
1ab06 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 lite3BtreeIsInTr
1ab07 61 6e 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20 ans(pBt) ){.
1ab08 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a char const *
1ab09 7a 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 42 zFile = sqlite3B
1ab0a 74 72 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 treeGetJournalna
1ab0b 6d 65 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20 me(pBt);.
1ab0c 20 69 66 28 20 7a 46 69 6c 65 5b 30 5d 3d 3d 30 if( zFile[0]==0
1ab0d 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a ) continue; /*
1ab0e 20 49 67 6e 6f 72 65 20 3a 6d 65 6d 6f 72 79 3a Ignore :memory:
1ab0f 20 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 databases */.
1ab10 20 20 20 20 20 20 69 66 28 20 21 6e 65 65 64 53 if( !needS
1ab11 79 6e 63 20 26 26 20 21 73 71 6c 69 74 65 33 42 ync && !sqlite3B
1ab12 74 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64 treeSyncDisabled
1ab13 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 (pBt) ){.
1ab14 20 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b needSync = 1;
1ab15 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1ab16 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
1ab17 73 57 72 69 74 65 28 70 4d 61 73 74 65 72 2c 20 sWrite(pMaster,
1ab18 7a 46 69 6c 65 2c 20 73 71 6c 69 74 65 33 53 74 zFile, sqlite3St
1ab19 72 6c 65 6e 33 30 28 7a 46 69 6c 65 29 2b 31 2c rlen30(zFile)+1,
1ab1a 20 6f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 offset);.
1ab1b 20 20 6f 66 66 73 65 74 20 2b 3d 20 73 71 6c 69 offset += sqli
1ab1c 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 46 69 6c te3Strlen30(zFil
1ab1d 65 29 2b 31 3b 0a 20 20 20 20 20 20 20 20 69 66 e)+1;. if
1ab1e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1ab1f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ){. sql
1ab20 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 ite3OsCloseFree(
1ab21 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 pMaster);.
1ab22 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c sqlite3OsDel
1ab23 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 ete(pVfs, zMaste
1ab24 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 r, 0);.
1ab25 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1ab26 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 b, zMaster);.
1ab27 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
1ab28 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1ab29 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f }. }.. /
1ab2a 2a 20 53 79 6e 63 20 74 68 65 20 6d 61 73 74 65 * Sync the maste
1ab2b 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 r journal file.
1ab2c 49 66 20 74 68 65 20 49 4f 43 41 50 5f 53 45 51 If the IOCAP_SEQ
1ab2d 55 45 4e 54 49 41 4c 20 64 65 76 69 63 65 0a 20 UENTIAL device.
1ab2e 20 20 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 65 ** flag is se
1ab2f 74 20 74 68 69 73 20 69 73 20 6e 6f 74 20 72 65 t this is not re
1ab30 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 quired.. */.
1ab31 20 20 20 69 66 28 20 6e 65 65 64 53 79 6e 63 20 if( needSync
1ab32 0a 20 20 20 20 20 26 26 20 30 3d 3d 28 73 71 6c . && 0==(sql
1ab33 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 ite3OsDeviceChar
1ab34 61 63 74 65 72 69 73 74 69 63 73 28 70 4d 61 73 acteristics(pMas
1ab35 74 65 72 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 ter)&SQLITE_IOCA
1ab36 50 5f 53 45 51 55 45 4e 54 49 41 4c 29 0a 20 20 P_SEQUENTIAL).
1ab37 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 && SQLITE_OK!
1ab38 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 =(rc = sqlite3Os
1ab39 53 79 6e 63 28 70 4d 61 73 74 65 72 2c 20 53 51 Sync(pMaster, SQ
1ab3a 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c LITE_SYNC_NORMAL
1ab3b 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 )). ){.
1ab3c 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 sqlite3OsCloseFr
1ab3d 65 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 ee(pMaster);.
1ab3e 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 sqlite3OsDele
1ab3f 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 te(pVfs, zMaster
1ab40 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 , 0);. sqli
1ab41 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d te3DbFree(db, zM
1ab42 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 72 65 aster);. re
1ab43 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a turn rc;. }..
1ab44 20 20 20 20 2f 2a 20 53 79 6e 63 20 61 6c 6c 20 /* Sync all
1ab45 74 68 65 20 64 62 20 66 69 6c 65 73 20 69 6e 76 the db files inv
1ab46 6f 6c 76 65 64 20 69 6e 20 74 68 65 20 74 72 61 olved in the tra
1ab47 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 61 nsaction. The sa
1ab48 6d 65 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 73 me call. ** s
1ab49 65 74 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a ets the master j
1ab4a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 20 69 ournal pointer i
1ab4b 6e 20 65 61 63 68 20 69 6e 64 69 76 69 64 75 61 n each individua
1ab4c 6c 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a 20 20 l journal. If.
1ab4d 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 ** an error oc
1ab4e 63 75 72 73 20 68 65 72 65 2c 20 64 6f 20 6e 6f curs here, do no
1ab4f 74 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 t delete the mas
1ab50 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
1ab51 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
1ab52 49 66 20 74 68 65 20 65 72 72 6f 72 20 6f 63 63 If the error occ
1ab53 75 72 73 20 64 75 72 69 6e 67 20 74 68 65 20 66 urs during the f
1ab54 69 72 73 74 20 63 61 6c 6c 20 74 6f 0a 20 20 20 irst call to.
1ab55 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 ** sqlite3Btree
1ab56 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 CommitPhaseOne()
1ab57 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 , then there is
1ab58 61 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 a chance that th
1ab59 65 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 20 e. ** master
1ab5a 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 69 6c journal file wil
1ab5b 6c 20 62 65 20 6f 72 70 68 61 6e 65 64 2e 20 42 l be orphaned. B
1ab5c 75 74 20 77 65 20 63 61 6e 6e 6f 74 20 64 65 6c ut we cannot del
1ab5d 65 74 65 20 69 74 2c 0a 20 20 20 20 2a 2a 20 69 ete it,. ** i
1ab5e 6e 20 63 61 73 65 20 74 68 65 20 6d 61 73 74 65 n case the maste
1ab5f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e r journal file n
1ab60 61 6d 65 20 77 61 73 20 77 72 69 74 74 65 6e 20 ame was written
1ab61 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c into the journal
1ab62 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 62 65 66 . ** file bef
1ab63 6f 72 65 20 74 68 65 20 66 61 69 6c 75 72 65 20 ore the failure
1ab64 6f 63 63 75 72 72 65 64 2e 0a 20 20 20 20 2a 2f occurred.. */
1ab65 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 . for(i=0; rc
1ab66 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 ==SQLITE_OK && i
1ab67 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 <db->nDb; i++){
1ab68 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 . Btree *pB
1ab69 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 t = db->aDb[i].p
1ab6a 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 Bt;. if( pB
1ab6b 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 t ){. rc
1ab6c 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f = sqlite3BtreeCo
1ab6d 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 mmitPhaseOne(pBt
1ab6e 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 , zMaster);.
1ab6f 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 }. }. sq
1ab70 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 lite3OsCloseFree
1ab71 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 69 (pMaster);. i
1ab72 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1ab73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1ab74 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 3DbFree(db, zMas
1ab75 74 65 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ter);. retu
1ab76 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 rn rc;. }..
1ab77 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 /* Delete the
1ab78 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
1ab79 69 6c 65 2e 20 54 68 69 73 20 63 6f 6d 6d 69 74 ile. This commit
1ab7a 73 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f s the transactio
1ab7b 6e 2e 20 41 66 74 65 72 0a 20 20 20 20 2a 2a 20 n. After. **
1ab7c 64 6f 69 6e 67 20 74 68 69 73 20 74 68 65 20 64 doing this the d
1ab7d 69 72 65 63 74 6f 72 79 20 69 73 20 73 79 6e 63 irectory is sync
1ab7e 65 64 20 61 67 61 69 6e 20 62 65 66 6f 72 65 20 ed again before
1ab7f 61 6e 79 20 69 6e 64 69 76 69 64 75 61 6c 0a 20 any individual.
1ab80 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f ** transactio
1ab81 6e 20 66 69 6c 65 73 20 61 72 65 20 64 65 6c 65 n files are dele
1ab82 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ted.. */.
1ab83 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 rc = sqlite3OsDe
1ab84 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 lete(pVfs, zMast
1ab85 65 72 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 er, 1);. sqli
1ab86 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d te3DbFree(db, zM
1ab87 61 73 74 65 72 29 3b 0a 20 20 20 20 7a 4d 61 73 aster);. zMas
1ab88 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 ter = 0;. if(
1ab89 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 rc ){. ret
1ab8a 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 urn rc;. }..
1ab8b 20 20 20 2f 2a 20 41 6c 6c 20 66 69 6c 65 73 20 /* All files
1ab8c 61 6e 64 20 64 69 72 65 63 74 6f 72 69 65 73 20 and directories
1ab8d 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 have already bee
1ab8e 6e 20 73 79 6e 63 65 64 2c 20 73 6f 20 74 68 65 n synced, so the
1ab8f 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 2a following. *
1ab90 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 * calls to sqlit
1ab91 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
1ab92 73 65 54 77 6f 28 29 20 61 72 65 20 6f 6e 6c 79 seTwo() are only
1ab93 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 61 closing files a
1ab94 6e 64 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74 69 nd. ** deleti
1ab95 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 ng or truncating
1ab96 20 6a 6f 75 72 6e 61 6c 73 2e 20 49 66 20 73 6f journals. If so
1ab97 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f mething goes wro
1ab98 6e 67 20 77 68 69 6c 65 0a 20 20 20 20 2a 2a 20 ng while. **
1ab99 74 68 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e this is happenin
1ab9a 67 20 77 65 20 64 6f 6e 27 74 20 72 65 61 6c 6c g we don't reall
1ab9b 79 20 63 61 72 65 2e 20 54 68 65 20 69 6e 74 65 y care. The inte
1ab9c 67 72 69 74 79 20 6f 66 20 74 68 65 0a 20 20 20 grity of the.
1ab9d 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ** transaction
1ab9e 69 73 20 61 6c 72 65 61 64 79 20 67 75 61 72 61 is already guara
1ab9f 6e 74 65 65 64 2c 20 62 75 74 20 73 6f 6d 65 20 nteed, but some
1aba0 73 74 72 61 79 20 27 63 6f 6c 64 27 20 6a 6f 75 stray 'cold' jou
1aba1 72 6e 61 6c 73 0a 20 20 20 20 2a 2a 20 6d 61 79 rnals. ** may
1aba2 20 62 65 20 6c 79 69 6e 67 20 61 72 6f 75 6e 64 be lying around
1aba3 2e 20 52 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 . Returning an e
1aba4 72 72 6f 72 20 63 6f 64 65 20 77 6f 6e 27 74 20 rror code won't
1aba5 68 65 6c 70 20 6d 61 74 74 65 72 73 2e 0a 20 20 help matters..
1aba6 20 20 2a 2f 0a 20 20 20 20 64 69 73 61 62 6c 65 */. disable
1aba7 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 _simulated_io_er
1aba8 72 6f 72 73 28 29 3b 0a 20 20 20 20 73 71 6c 69 rors();. sqli
1aba9 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 te3BeginBenignMa
1abaa 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 66 6f 72 28 lloc();. for(
1abab 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 i=0; i<db->nDb;
1abac 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 72 i++){ . Btr
1abad 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 ee *pBt = db->aD
1abae 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 b[i].pBt;.
1abaf 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 if( pBt ){.
1abb0 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 sqlite3BtreeC
1abb1 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 42 ommitPhaseTwo(pB
1abb2 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 t);. }.
1abb3 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 }. sqlite3End
1abb4 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
1abb5 20 20 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c enable_simul
1abb6 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 ated_io_errors()
1abb7 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 ;.. sqlite3Vt
1abb8 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 abCommit(db);.
1abb9 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 }.#endif.. retu
1abba 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a rn rc;.}../* .**
1abbb 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
1abbc 65 63 6b 73 20 74 68 61 74 20 74 68 65 20 73 71 ecks that the sq
1abbd 6c 69 74 65 33 2e 61 63 74 69 76 65 56 64 62 65 lite3.activeVdbe
1abbe 43 6e 74 20 63 6f 75 6e 74 20 76 61 72 69 61 62 Cnt count variab
1abbf 6c 65 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 68 le.** matches th
1abc0 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 64 62 65 e number of vdbe
1abc1 27 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 73 's in the list s
1abc2 71 6c 69 74 65 33 2e 70 56 64 62 65 20 74 68 61 qlite3.pVdbe tha
1abc3 74 20 61 72 65 0a 2a 2a 20 63 75 72 72 65 6e 74 t are.** current
1abc4 6c 79 20 61 63 74 69 76 65 2e 20 41 6e 20 61 73 ly active. An as
1abc5 73 65 72 74 69 6f 6e 20 66 61 69 6c 73 20 69 66 sertion fails if
1abc6 20 74 68 65 20 74 77 6f 20 63 6f 75 6e 74 73 20 the two counts
1abc7 64 6f 20 6e 6f 74 20 6d 61 74 63 68 2e 0a 2a 2a do not match..**
1abc8 20 54 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65 This is an inte
1abc9 72 6e 61 6c 20 73 65 6c 66 2d 63 68 65 63 6b 20 rnal self-check
1abca 6f 6e 6c 79 20 2d 20 69 74 20 69 73 20 6e 6f 74 only - it is not
1abcb 20 61 6e 20 65 73 73 65 6e 74 69 61 6c 20 70 72 an essential pr
1abcc 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 73 74 65 70 ocessing.** step
1abcd 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 ..**.** This is
1abce 61 20 6e 6f 2d 6f 70 20 69 66 20 4e 44 45 42 55 a no-op if NDEBU
1abcf 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f G is defined..*/
1abd0 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
1abd1 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 static void chec
1abd2 6b 41 63 74 69 76 65 56 64 62 65 43 6e 74 28 73 kActiveVdbeCnt(s
1abd3 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 qlite3 *db){. V
1abd4 64 62 65 20 2a 70 3b 0a 20 20 69 6e 74 20 63 6e dbe *p;. int cn
1abd5 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 57 72 t = 0;. int nWr
1abd6 69 74 65 20 3d 20 30 3b 0a 20 20 70 20 3d 20 64 ite = 0;. p = d
1abd7 62 2d 3e 70 56 64 62 65 3b 0a 20 20 77 68 69 6c b->pVdbe;. whil
1abd8 65 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 e( p ){. if(
1abd9 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d p->magic==VDBE_M
1abda 41 47 49 43 5f 52 55 4e 20 26 26 20 70 2d 3e 70 AGIC_RUN && p->p
1abdb 63 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6e c>=0 ){. cn
1abdc 74 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 t++;. if( p
1abdd 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30 20 29 20 ->readOnly==0 )
1abde 6e 57 72 69 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a nWrite++;. }.
1abdf 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 p = p->pNext
1abe0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
1abe1 63 6e 74 3d 3d 64 62 2d 3e 61 63 74 69 76 65 56 cnt==db->activeV
1abe2 64 62 65 43 6e 74 20 29 3b 0a 20 20 61 73 73 65 dbeCnt );. asse
1abe3 72 74 28 20 6e 57 72 69 74 65 3d 3d 64 62 2d 3e rt( nWrite==db->
1abe4 77 72 69 74 65 56 64 62 65 43 6e 74 20 29 3b 0a writeVdbeCnt );.
1abe5 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 }.#else.#define
1abe6 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 checkActiveVdbeC
1abe7 6e 74 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a nt(x).#endif../*
1abe8 0a 2a 2a 20 46 6f 72 20 65 76 65 72 79 20 42 74 .** For every Bt
1abe9 72 65 65 20 74 68 61 74 20 69 6e 20 64 61 74 61 ree that in data
1abea 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
1abeb 64 62 20 77 68 69 63 68 20 0a 2a 2a 20 68 61 73 db which .** has
1abec 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2c 20 been modified,
1abed 22 74 72 69 70 22 20 6f 72 20 69 6e 76 61 6c 69 "trip" or invali
1abee 64 61 74 65 20 65 61 63 68 20 63 75 72 73 6f 72 date each cursor
1abef 20 69 6e 0a 2a 2a 20 74 68 61 74 20 42 74 72 65 in.** that Btre
1abf0 65 20 6d 69 67 68 74 20 68 61 76 65 20 62 65 65 e might have bee
1abf1 6e 20 6d 6f 64 69 66 69 65 64 20 73 6f 20 74 68 n modified so th
1abf2 61 74 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a at the cursor.**
1abf3 20 63 61 6e 20 6e 65 76 65 72 20 62 65 20 75 73 can never be us
1abf4 65 64 20 61 67 61 69 6e 2e 20 20 54 68 69 73 20 ed again. This
1abf5 68 61 70 70 65 6e 73 20 77 68 65 6e 20 61 20 72 happens when a r
1abf6 6f 6c 6c 62 61 63 6b 0a 2a 2a 2a 20 6f 63 63 75 ollback.*** occu
1abf7 72 73 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 rs. We have to
1abf8 74 72 69 70 20 61 6c 6c 20 74 68 65 20 6f 74 68 trip all the oth
1abf9 65 72 20 63 75 72 73 6f 72 73 2c 20 65 76 65 6e er cursors, even
1abfa 0a 2a 2a 20 63 75 72 73 6f 72 20 66 72 6f 6d 20 .** cursor from
1abfb 6f 74 68 65 72 20 56 4d 73 20 69 6e 20 64 69 66 other VMs in dif
1abfc 66 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 20 ferent database
1abfd 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20 connections,.**
1abfe 73 6f 20 74 68 61 74 20 6e 6f 6e 65 20 6f 66 20 so that none of
1abff 74 68 65 6d 20 74 72 79 20 74 6f 20 75 73 65 20 them try to use
1ac00 74 68 65 20 64 61 74 61 20 61 74 20 77 68 69 63 the data at whic
1ac01 68 20 74 68 65 79 0a 2a 2a 20 77 65 72 65 20 70 h they.** were p
1ac02 6f 69 6e 74 69 6e 67 20 61 6e 64 20 77 68 69 63 ointing and whic
1ac03 68 20 6e 6f 77 20 6d 61 79 20 68 61 76 65 20 62 h now may have b
1ac04 65 65 6e 20 63 68 61 6e 67 65 64 20 64 75 65 0a een changed due.
1ac05 2a 2a 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 ** to the rollba
1ac06 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 6d 65 6d 62 ck..**.** Rememb
1ac07 65 72 20 74 68 61 74 20 61 20 72 6f 6c 6c 62 61 er that a rollba
1ac08 63 6b 20 63 61 6e 20 64 65 6c 65 74 65 20 74 61 ck can delete ta
1ac09 62 6c 65 73 20 63 6f 6d 70 6c 65 74 65 20 61 6e bles complete an
1ac0a 64 0a 2a 2a 20 72 65 6f 72 64 65 72 20 72 6f 6f d.** reorder roo
1ac0b 74 70 61 67 65 73 2e 20 20 53 6f 20 69 74 20 69 tpages. So it i
1ac0c 73 20 6e 6f 74 20 73 75 66 66 69 63 69 65 6e 74 s not sufficient
1ac0d 20 6a 75 73 74 20 74 6f 20 73 61 76 65 0a 2a 2a just to save.**
1ac0e 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 the state of th
1ac0f 65 20 63 75 72 73 6f 72 2e 20 20 57 65 20 68 61 e cursor. We ha
1ac10 76 65 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 ve to invalidate
1ac11 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 73 the cursor.** s
1ac12 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e 65 76 o that it is nev
1ac13 65 72 20 75 73 65 64 20 61 67 61 69 6e 2e 0a 2a er used again..*
1ac14 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e /.static void in
1ac15 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f validateCursorsO
1ac16 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73 28 nModifiedBtrees(
1ac17 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
1ac18 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 int i;. for(i=0
1ac19 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
1ac1a 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 20 ){. Btree *p
1ac1b 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 = db->aDb[i].pBt
1ac1c 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 73 ;. if( p && s
1ac1d 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 qlite3BtreeIsInT
1ac1e 72 61 6e 73 28 70 29 20 29 7b 0a 20 20 20 20 20 rans(p) ){.
1ac1f 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 sqlite3BtreeTri
1ac20 70 41 6c 6c 43 75 72 73 6f 72 73 28 70 2c 20 53 pAllCursors(p, S
1ac21 51 4c 49 54 45 5f 41 42 4f 52 54 29 3b 0a 20 20 QLITE_ABORT);.
1ac22 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
1ac23 20 49 66 20 74 68 65 20 56 64 62 65 20 70 61 73 If the Vdbe pas
1ac24 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 sed as the first
1ac25 20 61 72 67 75 6d 65 6e 74 20 6f 70 65 6e 65 64 argument opened
1ac26 20 61 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 a statement-tra
1ac27 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 63 6c 6f nsaction,.** clo
1ac28 73 65 20 69 74 20 6e 6f 77 2e 20 41 72 67 75 6d se it now. Argum
1ac29 65 6e 74 20 65 4f 70 20 6d 75 73 74 20 62 65 20 ent eOp must be
1ac2a 65 69 74 68 65 72 20 53 41 56 45 50 4f 49 4e 54 either SAVEPOINT
1ac2b 5f 52 4f 4c 4c 42 41 43 4b 20 6f 72 0a 2a 2a 20 _ROLLBACK or.**
1ac2c 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 SAVEPOINT_RELEAS
1ac2d 45 2e 20 49 66 20 69 74 20 69 73 20 53 41 56 45 E. If it is SAVE
1ac2e 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 POINT_ROLLBACK,
1ac2f 74 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 then the stateme
1ac30 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f nt.** transactio
1ac31 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b n is rolled back
1ac32 2e 20 49 66 20 65 4f 70 20 69 73 20 53 41 56 45 . If eOp is SAVE
1ac33 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74 POINT_RELEASE, t
1ac34 68 65 6e 20 74 68 65 20 0a 2a 2a 20 73 74 61 74 hen the .** stat
1ac35 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
1ac36 6e 20 69 73 20 63 6f 6d 6d 74 74 65 64 2e 0a 2a n is commtted..*
1ac37 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 *.** If an IO er
1ac38 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 53 ror occurs, an S
1ac39 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58 58 20 QLITE_IOERR_XXX
1ac3a 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
1ac3b 74 75 72 6e 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 turned. .** Othe
1ac3c 72 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e rwise SQLITE_OK.
1ac3d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1ac3e 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
1ac3f 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 beCloseStatement
1ac40 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 65 4f (Vdbe *p, int eO
1ac41 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 63 p){. sqlite3 *c
1ac42 6f 6e 73 74 20 64 62 20 3d 20 70 2d 3e 64 62 3b onst db = p->db;
1ac43 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
1ac44 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 49 66 20 TE_OK;.. /* If
1ac45 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 69 73 p->iStatement is
1ac46 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 greater than ze
1ac47 72 6f 2c 20 74 68 65 6e 20 74 68 69 73 20 56 64 ro, then this Vd
1ac48 62 65 20 6f 70 65 6e 65 64 20 61 20 0a 20 20 2a be opened a . *
1ac49 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e * statement tran
1ac4a 73 61 63 74 69 6f 6e 20 74 68 61 74 20 73 68 6f saction that sho
1ac4b 75 6c 64 20 62 65 20 63 6c 6f 73 65 64 20 68 65 uld be closed he
1ac4c 72 65 2e 20 54 68 65 20 6f 6e 6c 79 20 65 78 63 re. The only exc
1ac4d 65 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 74 eption. ** is t
1ac4e 68 61 74 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 hat an IO error
1ac4f 6d 61 79 20 68 61 76 65 20 6f 63 63 75 72 65 64 may have occured
1ac50 2c 20 63 61 75 73 69 6e 67 20 61 6e 20 65 6d 65 , causing an eme
1ac51 72 67 65 6e 63 79 20 72 6f 6c 6c 62 61 63 6b 2e rgency rollback.
1ac52 0a 20 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 . ** In this ca
1ac53 73 65 20 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65 se (db->nStateme
1ac54 6e 74 3d 3d 30 29 2c 20 61 6e 64 20 74 68 65 72 nt==0), and ther
1ac55 65 20 69 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 e is nothing to
1ac56 64 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 do.. */. if( d
1ac57 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20 26 26 b->nStatement &&
1ac58 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 29 p->iStatement )
1ac59 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 {. int i;.
1ac5a 20 63 6f 6e 73 74 20 69 6e 74 20 69 53 61 76 65 const int iSave
1ac5b 70 6f 69 6e 74 20 3d 20 70 2d 3e 69 53 74 61 74 point = p->iStat
1ac5c 65 6d 65 6e 74 2d 31 3b 0a 0a 20 20 20 20 61 73 ement-1;.. as
1ac5d 73 65 72 74 28 20 65 4f 70 3d 3d 53 41 56 45 50 sert( eOp==SAVEP
1ac5e 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 7c 7c OINT_ROLLBACK ||
1ac5f 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f eOp==SAVEPOINT_
1ac60 52 45 4c 45 41 53 45 29 3b 0a 20 20 20 20 61 73 RELEASE);. as
1ac61 73 65 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 65 sert( db->nState
1ac62 6d 65 6e 74 3e 30 20 29 3b 0a 20 20 20 20 61 73 ment>0 );. as
1ac63 73 65 72 74 28 20 70 2d 3e 69 53 74 61 74 65 6d sert( p->iStatem
1ac64 65 6e 74 3d 3d 28 64 62 2d 3e 6e 53 74 61 74 65 ent==(db->nState
1ac65 6d 65 6e 74 2b 64 62 2d 3e 6e 53 61 76 65 70 6f ment+db->nSavepo
1ac66 69 6e 74 29 20 29 3b 0a 0a 20 20 20 20 66 6f 72 int) );.. for
1ac67 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b (i=0; i<db->nDb;
1ac68 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 69 6e i++){ . in
1ac69 74 20 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f t rc2 = SQLITE_O
1ac6a 4b 3b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a K;. Btree *
1ac6b 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d pBt = db->aDb[i]
1ac6c 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 .pBt;. if(
1ac6d 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 pBt ){. i
1ac6e 66 28 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e f( eOp==SAVEPOIN
1ac6f 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 7b 0a 20 20 T_ROLLBACK ){.
1ac70 20 20 20 20 20 20 20 20 72 63 32 20 3d 20 73 71 rc2 = sq
1ac71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f lite3BtreeSavepo
1ac72 69 6e 74 28 70 42 74 2c 20 53 41 56 45 50 4f 49 int(pBt, SAVEPOI
1ac73 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 69 53 61 NT_ROLLBACK, iSa
1ac74 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 vepoint);.
1ac75 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
1ac76 72 63 32 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc2==SQLITE_OK )
1ac77 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 32 20 {. rc2
1ac78 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 = sqlite3BtreeSa
1ac79 76 65 70 6f 69 6e 74 28 70 42 74 2c 20 53 41 56 vepoint(pBt, SAV
1ac7a 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 EPOINT_RELEASE,
1ac7b 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 iSavepoint);.
1ac7c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
1ac7d 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1ac7e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
1ac7f 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 20 20 = rc2;.
1ac80 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
1ac81 20 20 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 db->nStateme
1ac82 6e 74 2d 2d 3b 0a 20 20 20 20 70 2d 3e 69 53 74 nt--;. p->iSt
1ac83 61 74 65 6d 65 6e 74 20 3d 20 30 3b 0a 0a 20 20 atement = 0;..
1ac84 20 20 2f 2a 20 49 66 20 74 68 65 20 73 74 61 74 /* If the stat
1ac85 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
1ac86 6e 20 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 n is being rolle
1ac87 64 20 62 61 63 6b 2c 20 61 6c 73 6f 20 72 65 73 d back, also res
1ac88 74 6f 72 65 20 74 68 65 20 0a 20 20 20 20 2a 2a tore the . **
1ac89 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
1ac8a 73 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 s deferred const
1ac8b 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 20 74 6f raint counter to
1ac8c 20 74 68 65 20 76 61 6c 75 65 20 69 74 20 68 61 the value it ha
1ac8d 64 20 77 68 65 6e 20 0a 20 20 20 20 2a 2a 20 74 d when . ** t
1ac8e 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 he statement tra
1ac8f 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 6f 70 65 nsaction was ope
1ac90 6e 65 64 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 ned. */. if(
1ac91 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f eOp==SAVEPOINT_
1ac92 52 4f 4c 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20 ROLLBACK ){.
1ac93 20 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 43 db->nDeferredC
1ac94 6f 6e 73 20 3d 20 70 2d 3e 6e 53 74 6d 74 44 65 ons = p->nStmtDe
1ac95 66 43 6f 6e 73 3b 0a 20 20 20 20 7d 0a 20 20 7d fCons;. }. }
1ac96 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1ac97 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 ./*.** If SQLite
1ac98 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 is compiled to
1ac99 73 75 70 70 6f 72 74 20 73 68 61 72 65 64 2d 63 support shared-c
1ac9a 61 63 68 65 20 6d 6f 64 65 20 61 6e 64 20 74 6f ache mode and to
1ac9b 20 62 65 20 74 68 72 65 61 64 73 61 66 65 2c 0a be threadsafe,.
1ac9c 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ** this routine
1ac9d 6f 62 74 61 69 6e 73 20 74 68 65 20 6d 75 74 65 obtains the mute
1ac9e 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 x associated wit
1ac9f 68 20 65 61 63 68 20 42 74 53 68 61 72 65 64 20 h each BtShared
1aca0 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 74 68 61 structure.** tha
1aca1 74 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65 t may be accesse
1aca2 64 20 62 79 20 74 68 65 20 56 4d 20 70 61 73 73 d by the VM pass
1aca3 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e ed as an argumen
1aca4 74 2e 20 49 6e 20 64 6f 69 6e 67 20 73 6f 20 69 t. In doing so i
1aca5 74 0a 2a 2a 20 73 65 74 73 20 74 68 65 20 42 74 t.** sets the Bt
1aca6 53 68 61 72 65 64 2e 64 62 20 6d 65 6d 62 65 72 Shared.db member
1aca7 20 6f 66 20 65 61 63 68 20 6f 66 20 74 68 65 20 of each of the
1aca8 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 75 BtShared structu
1aca9 72 65 73 2c 20 65 6e 73 75 72 69 6e 67 0a 2a 2a res, ensuring.**
1acaa 20 74 68 61 74 20 74 68 65 20 63 6f 72 72 65 63 that the correc
1acab 74 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 t busy-handler c
1acac 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b allback is invok
1acad 65 64 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a ed if required..
1acae 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 **.** If SQLite
1acaf 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 is not threadsaf
1acb0 65 20 62 75 74 20 64 6f 65 73 20 73 75 70 70 6f e but does suppo
1acb1 72 74 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 rt shared-cache
1acb2 6d 6f 64 65 2c 20 74 68 65 6e 0a 2a 2a 20 73 71 mode, then.** sq
1acb3 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 lite3BtreeEnterA
1acb4 6c 6c 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 ll() is invoked
1acb5 74 6f 20 73 65 74 20 74 68 65 20 42 74 53 68 61 to set the BtSha
1acb6 72 65 64 2e 64 62 20 76 61 72 69 61 62 6c 65 73 red.db variables
1acb7 0a 2a 2a 20 6f 66 20 61 6c 6c 20 6f 66 20 42 74 .** of all of Bt
1acb8 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 Shared structure
1acb9 73 20 61 63 63 65 73 73 69 62 6c 65 20 76 69 61 s accessible via
1acba 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 the database ha
1acbb 6e 64 6c 65 20 0a 2a 2a 20 61 73 73 6f 63 69 61 ndle .** associa
1acbc 74 65 64 20 77 69 74 68 20 74 68 65 20 56 4d 2e ted with the VM.
1acbd 20 4f 66 20 63 6f 75 72 73 65 20 6f 6e 6c 79 20 Of course only
1acbe 61 20 73 75 62 73 65 74 20 6f 66 20 74 68 65 73 a subset of thes
1acbf 65 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 e structures.**
1acc0 77 69 6c 6c 20 62 65 20 61 63 63 65 73 73 65 64 will be accessed
1acc1 20 62 79 20 74 68 65 20 56 4d 2c 20 61 6e 64 20 by the VM, and
1acc2 77 65 20 63 6f 75 6c 64 20 75 73 65 20 56 64 62 we could use Vdb
1acc3 65 2e 62 74 72 65 65 4d 61 73 6b 20 74 6f 20 66 e.btreeMask to f
1acc4 69 67 75 72 65 0a 2a 2a 20 74 68 61 74 20 73 75 igure.** that su
1acc5 62 73 65 74 20 6f 75 74 2c 20 62 75 74 20 74 68 bset out, but th
1acc6 65 72 65 20 69 73 20 6e 6f 20 61 64 76 61 6e 74 ere is no advant
1acc7 61 67 65 20 74 6f 20 64 6f 69 6e 67 20 73 6f 2e age to doing so.
1acc8 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 .**.** If SQLite
1acc9 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 is not threadsa
1acca 66 65 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 fe and does not
1accb 73 75 70 70 6f 72 74 20 73 68 61 72 65 64 2d 63 support shared-c
1accc 61 63 68 65 20 6d 6f 64 65 2c 20 74 68 69 73 0a ache mode, this.
1accd 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 ** function is a
1acce 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 23 69 66 6e 64 no-op..*/.#ifnd
1accf 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
1acd0 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 HARED_CACHE.SQLI
1acd1 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1acd2 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 sqlite3VdbeMutex
1acd3 41 72 72 61 79 45 6e 74 65 72 28 56 64 62 65 20 ArrayEnter(Vdbe
1acd4 2a 70 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f *p){.#if SQLITE_
1acd5 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c THREADSAFE. sql
1acd6 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 ite3BtreeMutexAr
1acd7 72 61 79 45 6e 74 65 72 28 26 70 2d 3e 61 4d 75 rayEnter(&p->aMu
1acd8 74 65 78 29 3b 0a 23 65 6c 73 65 0a 20 20 73 71 tex);.#else. sq
1acd9 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 lite3BtreeEnterA
1acda 6c 6c 28 70 2d 3e 64 62 29 3b 0a 23 65 6e 64 69 ll(p->db);.#endi
1acdb 66 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a f.}.#endif../*.*
1acdc 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
1acdd 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 is called when a
1acde 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 transaction ope
1acdf 6e 65 64 20 62 79 20 74 68 65 20 64 61 74 61 62 ned by the datab
1ace0 61 73 65 20 0a 2a 2a 20 68 61 6e 64 6c 65 20 61 ase .** handle a
1ace1 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
1ace2 68 65 20 56 4d 20 70 61 73 73 65 64 20 61 73 20 he VM passed as
1ace3 61 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 an argument is a
1ace4 62 6f 75 74 20 74 6f 20 62 65 20 0a 2a 2a 20 63 bout to be .** c
1ace5 6f 6d 6d 69 74 74 65 64 2e 20 49 66 20 74 68 65 ommitted. If the
1ace6 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69 re are outstandi
1ace7 6e 67 20 64 65 66 65 72 72 65 64 20 66 6f 72 65 ng deferred fore
1ace8 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 ign key constrai
1ace9 6e 74 0a 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e 73 nt.** violations
1acea 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
1aceb 45 52 52 4f 52 2e 20 4f 74 68 65 72 77 69 73 65 ERROR. Otherwise
1acec 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a , SQLITE_OK..**.
1aced 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 ** If there are
1acee 6f 75 74 73 74 61 6e 64 69 6e 67 20 46 4b 20 76 outstanding FK v
1acef 69 6f 6c 61 74 69 6f 6e 73 20 61 6e 64 20 74 68 iolations and th
1acf0 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
1acf1 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 rns .** SQLITE_E
1acf2 52 52 4f 52 2c 20 73 65 74 20 74 68 65 20 72 65 RROR, set the re
1acf3 73 75 6c 74 20 6f 66 20 74 68 65 20 56 4d 20 74 sult of the VM t
1acf4 6f 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 o SQLITE_CONSTRA
1acf5 49 4e 54 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a INT and write.**
1acf6 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
1acf7 65 20 74 6f 20 69 74 2e 20 54 68 65 6e 20 72 65 e to it. Then re
1acf8 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
1acf9 52 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 R..*/.#ifndef SQ
1acfa 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 LITE_OMIT_FOREIG
1acfb 4e 5f 4b 45 59 0a 53 51 4c 49 54 45 5f 50 52 49 N_KEY.SQLITE_PRI
1acfc 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1acfd 56 64 62 65 43 68 65 63 6b 46 6b 28 56 64 62 65 VdbeCheckFk(Vdbe
1acfe 20 2a 70 2c 20 69 6e 74 20 64 65 66 65 72 72 65 *p, int deferre
1acff 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 d){. sqlite3 *d
1ad00 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 b = p->db;. if(
1ad01 20 28 64 65 66 65 72 72 65 64 20 26 26 20 64 62 (deferred && db
1ad02 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3e ->nDeferredCons>
1ad03 30 29 20 7c 7c 20 28 21 64 65 66 65 72 72 65 64 0) || (!deferred
1ad04 20 26 26 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 && p->nFkConstr
1ad05 61 69 6e 74 3e 30 29 20 29 7b 0a 20 20 20 20 70 aint>0) ){. p
1ad06 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f ->rc = SQLITE_CO
1ad07 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 70 2d NSTRAINT;. p-
1ad08 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f >errorAction = O
1ad09 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 73 71 6c E_Abort;. sql
1ad0a 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 ite3SetString(&p
1ad0b 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 ->zErrMsg, db, "
1ad0c 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 foreign key cons
1ad0d 74 72 61 69 6e 74 20 66 61 69 6c 65 64 22 29 3b traint failed");
1ad0e 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1ad0f 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 TE_ERROR;. }.
1ad10 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1ad11 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
1ad12 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
1ad13 73 20 63 61 6c 6c 65 64 20 74 68 65 20 77 68 65 s called the whe
1ad14 6e 20 61 20 56 44 42 45 20 74 72 69 65 73 20 74 n a VDBE tries t
1ad15 6f 20 68 61 6c 74 2e 20 20 49 66 20 74 68 65 20 o halt. If the
1ad16 56 44 42 45 0a 2a 2a 20 68 61 73 20 6d 61 64 65 VDBE.** has made
1ad17 20 63 68 61 6e 67 65 73 20 61 6e 64 20 69 73 20 changes and is
1ad18 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f in autocommit mo
1ad19 64 65 2c 20 74 68 65 6e 20 63 6f 6d 6d 69 74 20 de, then commit
1ad1a 74 68 6f 73 65 0a 2a 2a 20 63 68 61 6e 67 65 73 those.** changes
1ad1b 2e 20 20 49 66 20 61 20 72 6f 6c 6c 62 61 63 6b . If a rollback
1ad1c 20 69 73 20 6e 65 65 64 65 64 2c 20 74 68 65 6e is needed, then
1ad1d 20 64 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b do the rollback
1ad1e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1ad1f 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 tine is the only
1ad20 20 77 61 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 way to move the
1ad21 20 73 74 61 74 65 20 6f 66 20 61 20 56 4d 20 66 state of a VM f
1ad22 72 6f 6d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 rom.** SQLITE_MA
1ad23 47 49 43 5f 52 55 4e 20 74 6f 20 53 51 4c 49 54 GIC_RUN to SQLIT
1ad24 45 5f 4d 41 47 49 43 5f 48 41 4c 54 2e 20 20 49 E_MAGIC_HALT. I
1ad25 74 20 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f t is harmless to
1ad26 0a 2a 2a 20 63 61 6c 6c 20 74 68 69 73 20 6f 6e .** call this on
1ad27 20 61 20 56 4d 20 74 68 61 74 20 69 73 20 69 6e a VM that is in
1ad28 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 the SQLITE_MAGI
1ad29 43 5f 48 41 4c 54 20 73 74 61 74 65 2e 0a 2a 2a C_HALT state..**
1ad2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 .** Return an er
1ad2b 72 6f 72 20 63 6f 64 65 2e 20 20 49 66 20 74 68 ror code. If th
1ad2c 65 20 63 6f 6d 6d 69 74 20 63 6f 75 6c 64 20 6e e commit could n
1ad2d 6f 74 20 63 6f 6d 70 6c 65 74 65 20 62 65 63 61 ot complete beca
1ad2e 75 73 65 20 6f 66 0a 2a 2a 20 6c 6f 63 6b 20 63 use of.** lock c
1ad2f 6f 6e 74 65 6e 74 69 6f 6e 2c 20 72 65 74 75 72 ontention, retur
1ad30 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 n SQLITE_BUSY.
1ad31 49 66 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 If SQLITE_BUSY i
1ad32 73 20 72 65 74 75 72 6e 65 64 2c 20 69 74 0a 2a s returned, it.*
1ad33 2a 20 6d 65 61 6e 73 20 74 68 65 20 63 6c 6f 73 * means the clos
1ad34 65 20 64 69 64 20 6e 6f 74 20 68 61 70 70 65 6e e did not happen
1ad35 20 61 6e 64 20 6e 65 65 64 73 20 74 6f 20 62 65 and needs to be
1ad36 20 72 65 70 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 repeated..*/.SQ
1ad37 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1ad38 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 sqlite3VdbeHalt
1ad39 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 (Vdbe *p){. int
1ad3a 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
1ad3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1ad3c 55 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 72 Used to store tr
1ad3d 61 6e 73 69 65 6e 74 20 72 65 74 75 72 6e 20 63 ansient return c
1ad3e 6f 64 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 odes */. sqlite
1ad3f 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 3 *db = p->db;..
1ad40 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 /* This functi
1ad41 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 on contains the
1ad42 6c 6f 67 69 63 20 74 68 61 74 20 64 65 74 65 72 logic that deter
1ad43 6d 69 6e 65 73 20 69 66 20 61 20 73 74 61 74 65 mines if a state
1ad44 6d 65 6e 74 20 6f 72 0a 20 20 2a 2a 20 74 72 61 ment or. ** tra
1ad45 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 nsaction will be
1ad46 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f committed or ro
1ad47 6c 6c 65 64 20 62 61 63 6b 20 61 73 20 61 20 72 lled back as a r
1ad48 65 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 2a esult of the. *
1ad49 2a 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 * execution of t
1ad4a 68 69 73 20 76 69 72 74 75 61 6c 20 6d 61 63 68 his virtual mach
1ad4b 69 6e 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 ine. . **. **
1ad4c 49 66 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f If any of the fo
1ad4d 6c 6c 6f 77 69 6e 67 20 65 72 72 6f 72 73 20 6f llowing errors o
1ad4e 63 63 75 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 ccur:. **. **
1ad4f 20 20 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d SQLITE_NOMEM
1ad50 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 . ** SQLITE
1ad51 5f 49 4f 45 52 52 0a 20 20 2a 2a 20 20 20 20 20 _IOERR. **
1ad52 53 51 4c 49 54 45 5f 46 55 4c 4c 0a 20 20 2a 2a SQLITE_FULL. **
1ad53 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 45 SQLITE_INTE
1ad54 52 52 55 50 54 0a 20 20 2a 2a 0a 20 20 2a 2a 20 RRUPT. **. **
1ad55 54 68 65 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 Then the interna
1ad56 6c 20 63 61 63 68 65 20 6d 69 67 68 74 20 68 61 l cache might ha
1ad57 76 65 20 62 65 65 6e 20 6c 65 66 74 20 69 6e 20 ve been left in
1ad58 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 0a an inconsistent.
1ad59 20 20 2a 2a 20 73 74 61 74 65 2e 20 20 57 65 20 ** state. We
1ad5a 6e 65 65 64 20 74 6f 20 72 6f 6c 6c 62 61 63 6b need to rollback
1ad5b 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 the statement t
1ad5c 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 66 20 74 ransaction, if t
1ad5d 68 65 72 65 20 69 73 0a 20 20 2a 2a 20 6f 6e 65 here is. ** one
1ad5e 2c 20 6f 72 20 74 68 65 20 63 6f 6d 70 6c 65 74 , or the complet
1ad5f 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 e transaction if
1ad60 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 74 61 there is no sta
1ad61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
1ad62 6f 6e 2e 0a 20 20 2a 2f 0a 0a 20 20 69 66 28 20 on.. */.. if(
1ad63 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 p->db->mallocFai
1ad64 6c 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 led ){. p->rc
1ad65 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
1ad66 0a 20 20 7d 0a 20 20 63 6c 6f 73 65 41 6c 6c 43 . }. closeAllC
1ad67 75 72 73 6f 72 73 28 70 29 3b 0a 20 20 69 66 28 ursors(p);. if(
1ad68 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f p->magic!=VDBE_
1ad69 4d 41 47 49 43 5f 52 55 4e 20 29 7b 0a 20 20 20 MAGIC_RUN ){.
1ad6a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1ad6b 4b 3b 0a 20 20 7d 0a 20 20 63 68 65 63 6b 41 63 K;. }. checkAc
1ad6c 74 69 76 65 56 64 62 65 43 6e 74 28 64 62 29 3b tiveVdbeCnt(db);
1ad6d 0a 0a 20 20 2f 2a 20 4e 6f 20 63 6f 6d 6d 69 74 .. /* No commit
1ad6e 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 65 or rollback nee
1ad6f 64 65 64 20 69 66 20 74 68 65 20 70 72 6f 67 72 ded if the progr
1ad70 61 6d 20 6e 65 76 65 72 20 73 74 61 72 74 65 64 am never started
1ad71 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e */. if( p->pc>
1ad72 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 72 =0 ){. int mr
1ad73 63 3b 20 20 20 2f 2a 20 50 72 69 6d 61 72 79 20 c; /* Primary
1ad74 65 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 error code from
1ad75 70 2d 3e 72 63 20 2a 2f 0a 20 20 20 20 69 6e 74 p->rc */. int
1ad76 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 3d 20 eStatementOp =
1ad77 30 3b 0a 20 20 20 20 69 6e 74 20 69 73 53 70 65 0;. int isSpe
1ad78 63 69 61 6c 45 72 72 6f 72 3b 20 20 20 20 20 20 cialError;
1ad79 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 /* Set to
1ad7a 74 72 75 65 20 69 66 20 61 20 27 73 70 65 63 69 true if a 'speci
1ad7b 61 6c 27 20 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 al' error */..
1ad7c 20 20 2f 2a 20 4c 6f 63 6b 20 61 6c 6c 20 62 74 /* Lock all bt
1ad7d 72 65 65 73 20 75 73 65 64 20 62 79 20 74 68 65 rees used by the
1ad7e 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 statement */.
1ad7f 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 sqlite3VdbeMut
1ad80 65 78 41 72 72 61 79 45 6e 74 65 72 28 70 29 3b exArrayEnter(p);
1ad81 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 .. /* Check f
1ad82 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 or one of the sp
1ad83 65 63 69 61 6c 20 65 72 72 6f 72 73 20 2a 2f 0a ecial errors */.
1ad84 20 20 20 20 6d 72 63 20 3d 20 70 2d 3e 72 63 20 mrc = p->rc
1ad85 26 20 30 78 66 66 3b 0a 20 20 20 20 61 73 73 65 & 0xff;. asse
1ad86 72 74 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 rt( p->rc!=SQLIT
1ad87 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 E_IOERR_BLOCKED
1ad88 29 3b 20 20 2f 2a 20 54 68 69 73 20 65 72 72 6f ); /* This erro
1ad89 72 20 6e 6f 20 6c 6f 6e 67 65 72 20 65 78 69 73 r no longer exis
1ad8a 74 73 20 2a 2f 0a 20 20 20 20 69 73 53 70 65 63 ts */. isSpec
1ad8b 69 61 6c 45 72 72 6f 72 20 3d 20 6d 72 63 3d 3d ialError = mrc==
1ad8c 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 SQLITE_NOMEM ||
1ad8d 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 mrc==SQLITE_IOER
1ad8e 52 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 R.
1ad8f 20 20 20 20 20 20 20 7c 7c 20 6d 72 63 3d 3d 53 || mrc==S
1ad90 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 QLITE_INTERRUPT
1ad91 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 || mrc==SQLITE_F
1ad92 55 4c 4c 3b 0a 20 20 20 20 69 66 28 20 69 73 53 ULL;. if( isS
1ad93 70 65 63 69 61 6c 45 72 72 6f 72 20 29 7b 0a 20 pecialError ){.
1ad94 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 71 /* If the q
1ad95 75 65 72 79 20 77 61 73 20 72 65 61 64 2d 6f 6e uery was read-on
1ad96 6c 79 2c 20 77 65 20 6e 65 65 64 20 64 6f 20 6e ly, we need do n
1ad97 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6c o rollback at al
1ad98 6c 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 20 20 l. Otherwise,.
1ad99 20 20 20 20 2a 2a 20 70 72 6f 63 65 65 64 20 77 ** proceed w
1ad9a 69 74 68 20 74 68 65 20 73 70 65 63 69 61 6c 20 ith the special
1ad9b 68 61 6e 64 6c 69 6e 67 2e 0a 20 20 20 20 20 20 handling..
1ad9c 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21 70 2d */. if( !p-
1ad9d 3e 72 65 61 64 4f 6e 6c 79 20 7c 7c 20 6d 72 63 >readOnly || mrc
1ad9e 21 3d 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 !=SQLITE_INTERRU
1ad9f 50 54 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 PT ){. if
1ada0 28 20 28 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e ( (mrc==SQLITE_N
1ada1 4f 4d 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c OMEM || mrc==SQL
1ada2 49 54 45 5f 46 55 4c 4c 29 20 26 26 20 70 2d 3e ITE_FULL) && p->
1ada3 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 usesStmtJournal
1ada4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 65 53 74 ){. eSt
1ada5 61 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 atementOp = SAVE
1ada6 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a POINT_ROLLBACK;.
1ada7 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
1ada8 20 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 61 /* We a
1ada9 72 65 20 66 6f 72 63 65 64 20 74 6f 20 72 6f 6c re forced to rol
1adaa 6c 20 62 61 63 6b 20 74 68 65 20 61 63 74 69 76 l back the activ
1adab 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 42 e transaction. B
1adac 65 66 6f 72 65 20 64 6f 69 6e 67 0a 20 20 20 20 efore doing.
1adad 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20 61 62 6f ** so, abo
1adae 72 74 20 61 6e 79 20 6f 74 68 65 72 20 73 74 61 rt any other sta
1adaf 74 65 6d 65 6e 74 73 20 74 68 69 73 20 68 61 6e tements this han
1adb0 64 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 68 61 dle currently ha
1adb1 73 20 61 63 74 69 76 65 2e 0a 20 20 20 20 20 20 s active..
1adb2 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 */.
1adb3 20 69 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f invalidateCurso
1adb4 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 rsOnModifiedBtre
1adb5 65 73 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 es(db);.
1adb6 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 sqlite3Rollbac
1adb7 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 kAll(db);.
1adb8 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 sqlite3Close
1adb9 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a Savepoints(db);.
1adba 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 db->au
1adbb 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 toCommit = 1;.
1adbc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1adbd 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 }.. /* Ch
1adbe 65 63 6b 20 66 6f 72 20 69 6d 6d 65 64 69 61 74 eck for immediat
1adbf 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 69 e foreign key vi
1adc0 6f 6c 61 74 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20 olations. */.
1adc1 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 if( p->rc==SQLI
1adc2 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 TE_OK ){. s
1adc3 71 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 qlite3VdbeCheckF
1adc4 6b 28 70 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 k(p, 0);. }.
1adc5 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 . /* If the
1adc6 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 auto-commit flag
1adc7 20 69 73 20 73 65 74 20 61 6e 64 20 74 68 69 73 is set and this
1adc8 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 61 63 74 is the only act
1adc9 69 76 65 20 77 72 69 74 65 72 20 0a 20 20 20 20 ive writer .
1adca 2a 2a 20 56 4d 2c 20 74 68 65 6e 20 77 65 20 64 ** VM, then we d
1adcb 6f 20 65 69 74 68 65 72 20 61 20 63 6f 6d 6d 69 o either a commi
1adcc 74 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 t or rollback of
1adcd 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 the current tra
1adce 6e 73 61 63 74 69 6f 6e 2e 20 0a 20 20 20 20 2a nsaction. . *
1adcf 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 *. ** Note: T
1add0 68 69 73 20 62 6c 6f 63 6b 20 61 6c 73 6f 20 72 his block also r
1add1 75 6e 73 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 uns if one of th
1add2 65 20 73 70 65 63 69 61 6c 20 65 72 72 6f 72 73 e special errors
1add3 20 68 61 6e 64 6c 65 64 20 0a 20 20 20 20 2a 2a handled . **
1add4 20 61 62 6f 76 65 20 68 61 73 20 6f 63 63 75 72 above has occur
1add5 72 65 64 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 red. . */.
1add6 20 69 66 28 20 21 73 71 6c 69 74 65 33 56 74 61 if( !sqlite3Vta
1add7 62 49 6e 53 79 6e 63 28 64 62 29 20 0a 20 20 20 bInSync(db) .
1add8 20 20 26 26 20 64 62 2d 3e 61 75 74 6f 43 6f 6d && db->autoCom
1add9 6d 69 74 20 0a 20 20 20 20 20 26 26 20 64 62 2d mit . && db-
1adda 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3d 3d 28 >writeVdbeCnt==(
1addb 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30 29 20 p->readOnly==0)
1addc 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66 . ){. if
1addd 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f ( p->rc==SQLITE_
1adde 4f 4b 20 7c 7c 20 28 70 2d 3e 65 72 72 6f 72 41 OK || (p->errorA
1addf 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26 ction==OE_Fail &
1ade0 26 20 21 69 73 53 70 65 63 69 61 6c 45 72 72 6f & !isSpecialErro
1ade1 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 r) ){. if
1ade2 28 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 65 ( sqlite3VdbeChe
1ade3 63 6b 46 6b 28 70 2c 20 31 29 20 29 7b 0a 20 20 ckFk(p, 1) ){.
1ade4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 sqlite3B
1ade5 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 treeMutexArrayLe
1ade6 61 76 65 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b ave(&p->aMutex);
1ade7 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
1ade8 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
1ade9 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1adea 20 20 2f 2a 20 54 68 65 20 61 75 74 6f 2d 63 6f /* The auto-co
1adeb 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 74 72 75 mmit flag is tru
1adec 65 2c 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 e, the vdbe prog
1aded 72 61 6d 20 77 61 73 20 73 75 63 63 65 73 73 66 ram was successf
1adee 75 6c 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f ul . ** o
1adef 72 20 68 69 74 20 61 6e 20 27 4f 52 20 46 41 49 r hit an 'OR FAI
1adf0 4c 27 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 6e L' constraint an
1adf1 64 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 64 d there are no d
1adf2 65 66 65 72 72 65 64 20 66 6f 72 65 69 67 6e 0a eferred foreign.
1adf3 20 20 20 20 20 20 20 20 2a 2a 20 6b 65 79 20 63 ** key c
1adf4 6f 6e 73 74 72 61 69 6e 74 73 20 74 6f 20 68 6f onstraints to ho
1adf5 6c 64 20 75 70 20 74 68 65 20 74 72 61 6e 73 61 ld up the transa
1adf6 63 74 69 6f 6e 2e 20 54 68 69 73 20 6d 65 61 6e ction. This mean
1adf7 73 20 61 20 63 6f 6d 6d 69 74 20 0a 20 20 20 20 s a commit .
1adf8 20 20 20 20 2a 2a 20 69 73 20 72 65 71 75 69 72 ** is requir
1adf9 65 64 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 ed. */.
1adfa 72 63 20 3d 20 76 64 62 65 43 6f 6d 6d 69 74 28 rc = vdbeCommit(
1adfb 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20 db, p);.
1adfc 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 if( rc==SQLITE_B
1adfd 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 USY ){.
1adfe 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 sqlite3BtreeMut
1adff 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d exArrayLeave(&p-
1ae00 3e 61 4d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 >aMutex);.
1ae01 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1ae02 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 E_BUSY;.
1ae03 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 }else if( rc!=SQ
1ae04 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1ae05 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b p->rc = rc;
1ae06 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1ae07 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 e3RollbackAll(db
1ae08 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 );. }else
1ae09 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e {. db->
1ae0a 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 20 3d 20 nDeferredCons =
1ae0b 30 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 0;. sql
1ae0c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e ite3CommitIntern
1ae0d 61 6c 43 68 61 6e 67 65 73 28 64 62 29 3b 0a 20 alChanges(db);.
1ae0e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1ae0f 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 else{. sq
1ae10 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c lite3RollbackAll
1ae11 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 (db);. }.
1ae12 20 20 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 db->nStateme
1ae13 6e 74 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 nt = 0;. }els
1ae14 65 20 69 66 28 20 65 53 74 61 74 65 6d 65 6e 74 e if( eStatement
1ae15 4f 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 Op==0 ){. i
1ae16 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 f( p->rc==SQLITE
1ae17 5f 4f 4b 20 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 _OK || p->errorA
1ae18 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 ction==OE_Fail )
1ae19 7b 0a 20 20 20 20 20 20 20 20 65 53 74 61 74 65 {. eState
1ae1a 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 mentOp = SAVEPOI
1ae1b 4e 54 5f 52 45 4c 45 41 53 45 3b 0a 20 20 20 20 NT_RELEASE;.
1ae1c 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 }else if( p->e
1ae1d 72 72 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41 rrorAction==OE_A
1ae1e 62 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 bort ){.
1ae1f 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 3d 20 53 eStatementOp = S
1ae20 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
1ae21 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a K;. }else{.
1ae22 20 20 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61 invalida
1ae23 74 65 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 teCursorsOnModif
1ae24 69 65 64 42 74 72 65 65 73 28 64 62 29 3b 0a 20 iedBtrees(db);.
1ae25 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f sqlite3Ro
1ae26 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 llbackAll(db);.
1ae27 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6c sqlite3Cl
1ae28 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62 oseSavepoints(db
1ae29 29 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 );. db->a
1ae2a 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 utoCommit = 1;.
1ae2b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a }. }. .
1ae2c 20 20 20 20 2f 2a 20 49 66 20 65 53 74 61 74 65 /* If eState
1ae2d 6d 65 6e 74 4f 70 20 69 73 20 6e 6f 6e 2d 7a 65 mentOp is non-ze
1ae2e 72 6f 2c 20 74 68 65 6e 20 61 20 73 74 61 74 65 ro, then a state
1ae2f 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
1ae30 20 6e 65 65 64 73 20 74 6f 0a 20 20 20 20 2a 2a needs to. **
1ae31 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 be committed or
1ae32 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 43 61 rolled back. Ca
1ae33 6c 6c 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c ll sqlite3VdbeCl
1ae34 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 29 20 74 oseStatement() t
1ae35 6f 0a 20 20 20 20 2a 2a 20 64 6f 20 73 6f 2e 20 o. ** do so.
1ae36 49 66 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f If this operatio
1ae37 6e 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 n returns an err
1ae38 6f 72 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 or, and the curr
1ae39 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 ent statement.
1ae3a 20 20 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 ** error code
1ae3b 69 73 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 is SQLITE_OK or
1ae3c 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e SQLITE_CONSTRAIN
1ae3d 54 2c 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 T, then set the
1ae3e 65 72 72 6f 72 0a 20 20 20 20 2a 2a 20 63 6f 64 error. ** cod
1ae3f 65 20 74 6f 20 74 68 65 20 6e 65 77 20 76 61 6c e to the new val
1ae40 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ue.. */. i
1ae41 66 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 f( eStatementOp
1ae42 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
1ae43 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 lite3VdbeCloseSt
1ae44 61 74 65 6d 65 6e 74 28 70 2c 20 65 53 74 61 74 atement(p, eStat
1ae45 65 6d 65 6e 74 4f 70 29 3b 0a 20 20 20 20 20 20 ementOp);.
1ae46 69 66 28 20 72 63 20 26 26 20 28 70 2d 3e 72 63 if( rc && (p->rc
1ae47 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 ==SQLITE_OK || p
1ae48 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e ->rc==SQLITE_CON
1ae49 53 54 52 41 49 4e 54 29 20 29 7b 0a 20 20 20 20 STRAINT) ){.
1ae4a 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a p->rc = rc;.
1ae4b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
1ae4c 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 bFree(db, p->zEr
1ae4d 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 70 rMsg);. p
1ae4e 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 ->zErrMsg = 0;.
1ae4f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a }. }. .
1ae50 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 /* If this w
1ae51 61 73 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 as an INSERT, UP
1ae52 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 61 DATE or DELETE a
1ae53 6e 64 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 nd no statement
1ae54 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 transaction.
1ae55 2a 2a 20 68 61 73 20 62 65 65 6e 20 72 6f 6c 6c ** has been roll
1ae56 65 64 20 62 61 63 6b 2c 20 75 70 64 61 74 65 20 ed back, update
1ae57 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
1ae58 6e 65 63 74 69 6f 6e 20 63 68 61 6e 67 65 2d 63 nection change-c
1ae59 6f 75 6e 74 65 72 2e 20 0a 20 20 20 20 2a 2f 0a ounter. . */.
1ae5a 20 20 20 20 69 66 28 20 70 2d 3e 63 68 61 6e 67 if( p->chang
1ae5b 65 43 6e 74 4f 6e 20 29 7b 0a 20 20 20 20 20 20 eCntOn ){.
1ae5c 69 66 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 if( eStatementOp
1ae5d 21 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c !=SAVEPOINT_ROLL
1ae5e 42 41 43 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 BACK ){.
1ae5f 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 sqlite3VdbeSetCh
1ae60 61 6e 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 anges(db, p->nCh
1ae61 61 6e 67 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c ange);. }el
1ae62 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 se{. sqli
1ae63 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 te3VdbeSetChange
1ae64 73 28 64 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 s(db, 0);.
1ae65 7d 0a 20 20 20 20 20 20 70 2d 3e 6e 43 68 61 6e }. p->nChan
1ae66 67 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 ge = 0;. }.
1ae67 0a 20 20 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b . /* Rollback
1ae68 20 6f 72 20 63 6f 6d 6d 69 74 20 61 6e 79 20 73 or commit any s
1ae69 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20 74 68 chema changes th
1ae6a 61 74 20 6f 63 63 75 72 72 65 64 2e 20 2a 2f 0a at occurred. */.
1ae6b 20 20 20 20 69 66 28 20 70 2d 3e 72 63 21 3d 53 if( p->rc!=S
1ae6c 51 4c 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e QLITE_OK && db->
1ae6d 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 flags&SQLITE_Int
1ae6e 65 72 6e 43 68 61 6e 67 65 73 20 29 7b 0a 20 20 ernChanges ){.
1ae6f 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 sqlite3Reset
1ae70 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 InternalSchema(d
1ae71 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 64 62 2d b, 0);. db-
1ae72 3e 66 6c 61 67 73 20 3d 20 28 64 62 2d 3e 66 6c >flags = (db->fl
1ae73 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 49 6e 74 ags | SQLITE_Int
1ae74 65 72 6e 43 68 61 6e 67 65 73 29 3b 0a 20 20 20 ernChanges);.
1ae75 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 }.. /* Relea
1ae76 73 65 20 74 68 65 20 6c 6f 63 6b 73 20 2a 2f 0a se the locks */.
1ae77 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1ae78 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 MutexArrayLeave(
1ae79 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 7d &p->aMutex);. }
1ae7a 0a 0a 20 20 2f 2a 20 57 65 20 68 61 76 65 20 73 .. /* We have s
1ae7b 75 63 63 65 73 73 66 75 6c 6c 79 20 68 61 6c 74 uccessfully halt
1ae7c 65 64 20 61 6e 64 20 63 6c 6f 73 65 64 20 74 68 ed and closed th
1ae7d 65 20 56 4d 2e 20 20 52 65 63 6f 72 64 20 74 68 e VM. Record th
1ae7e 69 73 20 66 61 63 74 2e 20 2a 2f 0a 20 20 69 66 is fact. */. if
1ae7f 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 ( p->pc>=0 ){.
1ae80 20 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 db->activeVdbe
1ae81 43 6e 74 2d 2d 3b 0a 20 20 20 20 69 66 28 20 21 Cnt--;. if( !
1ae82 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 p->readOnly ){.
1ae83 20 20 20 20 20 64 62 2d 3e 77 72 69 74 65 56 64 db->writeVd
1ae84 62 65 43 6e 74 2d 2d 3b 0a 20 20 20 20 7d 0a 20 beCnt--;. }.
1ae85 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 assert( db->a
1ae86 63 74 69 76 65 56 64 62 65 43 6e 74 3e 3d 64 62 ctiveVdbeCnt>=db
1ae87 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 20 29 ->writeVdbeCnt )
1ae88 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6d 61 67 69 63 ;. }. p->magic
1ae89 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 = VDBE_MAGIC_HA
1ae8a 4c 54 3b 0a 20 20 63 68 65 63 6b 41 63 74 69 76 LT;. checkActiv
1ae8b 65 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 20 20 eVdbeCnt(db);.
1ae8c 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f if( p->db->mallo
1ae8d 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 cFailed ){. p
1ae8e 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f ->rc = SQLITE_NO
1ae8f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 MEM;. }.. /* I
1ae90 66 20 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 f the auto-commi
1ae91 74 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 6f t flag is set to
1ae92 20 74 72 75 65 2c 20 74 68 65 6e 20 61 6e 79 20 true, then any
1ae93 6c 6f 63 6b 73 20 74 68 61 74 20 77 65 72 65 20 locks that were
1ae94 68 65 6c 64 0a 20 20 2a 2a 20 62 79 20 63 6f 6e held. ** by con
1ae95 6e 65 63 74 69 6f 6e 20 64 62 20 68 61 76 65 20 nection db have
1ae96 6e 6f 77 20 62 65 65 6e 20 72 65 6c 65 61 73 65 now been release
1ae97 64 2e 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 43 d. Call sqlite3C
1ae98 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 onnectionUnlocke
1ae99 64 28 29 20 0a 20 20 2a 2a 20 74 6f 20 69 6e 76 d() . ** to inv
1ae9a 6f 6b 65 20 61 6e 79 20 72 65 71 75 69 72 65 64 oke any required
1ae9b 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 unlock-notify c
1ae9c 61 6c 6c 62 61 63 6b 73 2e 0a 20 20 2a 2f 0a 20 allbacks.. */.
1ae9d 20 69 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d if( db->autoCom
1ae9e 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 mit ){. sqlit
1ae9f 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f e3ConnectionUnlo
1aea0 63 6b 65 64 28 64 62 29 3b 0a 20 20 7d 0a 0a 20 cked(db);. }..
1aea1 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 63 74 assert( db->act
1aea2 69 76 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20 iveVdbeCnt>0 ||
1aea3 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d db->autoCommit==
1aea4 30 20 7c 7c 20 64 62 2d 3e 6e 53 74 61 74 65 6d 0 || db->nStatem
1aea5 65 6e 74 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 ent==0 );. retu
1aea6 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1aea7 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 56 44 42 ../*.** Each VDB
1aea8 45 20 68 6f 6c 64 73 20 74 68 65 20 72 65 73 75 E holds the resu
1aea9 6c 74 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 lt of the most r
1aeaa 65 63 65 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 ecent sqlite3_st
1aeab 65 70 28 29 20 63 61 6c 6c 0a 2a 2a 20 69 6e 20 ep() call.** in
1aeac 70 2d 3e 72 63 2e 20 20 54 68 69 73 20 72 6f 75 p->rc. This rou
1aead 74 69 6e 65 20 73 65 74 73 20 74 68 61 74 20 72 tine sets that r
1aeae 65 73 75 6c 74 20 62 61 63 6b 20 74 6f 20 53 51 esult back to SQ
1aeaf 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 LITE_OK..*/.SQLI
1aeb0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1aeb1 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 sqlite3VdbeReset
1aeb2 53 74 65 70 52 65 73 75 6c 74 28 56 64 62 65 20 StepResult(Vdbe
1aeb3 2a 70 29 7b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 *p){. p->rc = S
1aeb4 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
1aeb5 2a 2a 20 43 6c 65 61 6e 20 75 70 20 61 20 56 44 ** Clean up a VD
1aeb6 42 45 20 61 66 74 65 72 20 65 78 65 63 75 74 69 BE after executi
1aeb7 6f 6e 20 62 75 74 20 64 6f 20 6e 6f 74 20 64 65 on but do not de
1aeb8 6c 65 74 65 20 74 68 65 20 56 44 42 45 20 6a 75 lete the VDBE ju
1aeb9 73 74 20 79 65 74 2e 0a 2a 2a 20 57 72 69 74 65 st yet..** Write
1aeba 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 any error messa
1aebb 67 65 73 20 69 6e 74 6f 20 2a 70 7a 45 72 72 4d ges into *pzErrM
1aebc 73 67 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 sg. Return the
1aebd 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a 2a 2a 0a result code..**.
1aebe 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f ** After this ro
1aebf 75 74 69 6e 65 20 69 73 20 72 75 6e 2c 20 74 68 utine is run, th
1aec0 65 20 56 44 42 45 20 73 68 6f 75 6c 64 20 62 65 e VDBE should be
1aec1 20 72 65 61 64 79 20 74 6f 20 62 65 20 65 78 65 ready to be exe
1aec2 63 75 74 65 64 0a 2a 2a 20 61 67 61 69 6e 2e 0a cuted.** again..
1aec3 2a 2a 0a 2a 2a 20 54 6f 20 6c 6f 6f 6b 20 61 74 **.** To look at
1aec4 20 69 74 20 61 6e 6f 74 68 65 72 20 77 61 79 2c it another way,
1aec5 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 this routine re
1aec6 73 65 74 73 20 74 68 65 20 73 74 61 74 65 20 6f sets the state o
1aec7 66 20 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c f the.** virtual
1aec8 20 6d 61 63 68 69 6e 65 20 66 72 6f 6d 20 56 44 machine from VD
1aec9 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 6f 72 20 BE_MAGIC_RUN or
1aeca 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 VDBE_MAGIC_HALT
1aecb 62 61 63 6b 20 74 6f 0a 2a 2a 20 56 44 42 45 5f back to.** VDBE_
1aecc 4d 41 47 49 43 5f 49 4e 49 54 2e 0a 2a 2f 0a 53 MAGIC_INIT..*/.S
1aecd 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1aece 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 t sqlite3VdbeRes
1aecf 65 74 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 et(Vdbe *p){. s
1aed0 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 64 62 qlite3 *db;. db
1aed1 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 = p->db;.. /*
1aed2 49 66 20 74 68 65 20 56 4d 20 64 69 64 20 6e 6f If the VM did no
1aed3 74 20 72 75 6e 20 74 6f 20 63 6f 6d 70 6c 65 74 t run to complet
1aed4 69 6f 6e 20 6f 72 20 69 66 20 69 74 20 65 6e 63 ion or if it enc
1aed5 6f 75 6e 74 65 72 65 64 20 61 6e 0a 20 20 2a 2a ountered an. **
1aed6 20 65 72 72 6f 72 2c 20 74 68 65 6e 20 69 74 20 error, then it
1aed7 6d 69 67 68 74 20 6e 6f 74 20 68 61 76 65 20 62 might not have b
1aed8 65 65 6e 20 68 61 6c 74 65 64 20 70 72 6f 70 65 een halted prope
1aed9 72 6c 79 2e 20 20 53 6f 20 68 61 6c 74 0a 20 20 rly. So halt.
1aeda 2a 2a 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a ** it now.. */.
1aedb 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 (void)sqlite3S
1aedc 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 73 afetyOn(db);. s
1aedd 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 qlite3VdbeHalt(p
1aede 29 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 );. (void)sqlit
1aedf 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b e3SafetyOff(db);
1aee0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 56 44 .. /* If the VD
1aee1 42 45 20 68 61 73 20 62 65 20 72 75 6e 20 65 76 BE has be run ev
1aee2 65 6e 20 70 61 72 74 69 61 6c 6c 79 2c 20 74 68 en partially, th
1aee3 65 6e 20 74 72 61 6e 73 66 65 72 20 74 68 65 20 en transfer the
1aee4 65 72 72 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 error code. **
1aee5 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 and error messag
1aee6 65 20 66 72 6f 6d 20 74 68 65 20 56 44 42 45 20 e from the VDBE
1aee7 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 64 61 into the main da
1aee8 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 tabase structure
1aee9 2e 20 20 42 75 74 0a 20 20 2a 2a 20 69 66 20 74 . But. ** if t
1aeea 68 65 20 56 44 42 45 20 68 61 73 20 6a 75 73 74 he VDBE has just
1aeeb 20 62 65 65 6e 20 73 65 74 20 74 6f 20 72 75 6e been set to run
1aeec 20 62 75 74 20 68 61 73 20 6e 6f 74 20 61 63 74 but has not act
1aeed 75 61 6c 6c 79 20 65 78 65 63 75 74 65 64 20 61 ually executed a
1aeee 6e 79 0a 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 ny. ** instruct
1aeef 69 6f 6e 73 20 79 65 74 2c 20 6c 65 61 76 65 20 ions yet, leave
1aef0 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
1aef1 65 20 65 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74 e error informat
1aef2 69 6f 6e 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20 ion unchanged..
1aef3 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e */. if( p->pc>
1aef4 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d =0 ){. if( p-
1aef5 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 >zErrMsg ){.
1aef6 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 sqlite3BeginBe
1aef7 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 nignMalloc();.
1aef8 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 sqlite3Value
1aef9 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c SetStr(db->pErr,
1aefa 2d 31 2c 70 2d 3e 7a 45 72 72 4d 73 67 2c 53 51 -1,p->zErrMsg,SQ
1aefb 4c 49 54 45 5f 55 54 46 38 2c 53 51 4c 49 54 45 LITE_UTF8,SQLITE
1aefc 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 _TRANSIENT);.
1aefd 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e sqlite3EndBen
1aefe 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 ignMalloc();.
1aeff 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d db->errCode =
1af00 20 70 2d 3e 72 63 3b 0a 20 20 20 20 20 20 73 71 p->rc;. sq
1af01 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1af02 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 p->zErrMsg);.
1af03 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 p->zErrMsg =
1af04 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 0;. }else if(
1af05 20 70 2d 3e 72 63 20 29 7b 0a 20 20 20 20 20 20 p->rc ){.
1af06 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c sqlite3Error(db,
1af07 20 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 p->rc, 0);.
1af08 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c }else{. sql
1af09 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 ite3Error(db, SQ
1af0a 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 LITE_OK, 0);.
1af0b 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 }. }else if( p
1af0c 2d 3e 72 63 20 26 26 20 70 2d 3e 65 78 70 69 72 ->rc && p->expir
1af0d 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 ed ){. /* The
1af0e 20 65 78 70 69 72 65 64 20 66 6c 61 67 20 77 61 expired flag wa
1af0f 73 20 73 65 74 20 6f 6e 20 74 68 65 20 56 44 42 s set on the VDB
1af10 45 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 72 E before the fir
1af11 73 74 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 st call. ** t
1af12 6f 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 o sqlite3_step()
1af13 2e 20 46 6f 72 20 63 6f 6e 73 69 73 74 65 6e 63 . For consistenc
1af14 79 20 28 73 69 6e 63 65 20 73 71 6c 69 74 65 33 y (since sqlite3
1af15 5f 73 74 65 70 28 29 20 77 61 73 0a 20 20 20 20 _step() was.
1af16 2a 2a 20 63 61 6c 6c 65 64 29 2c 20 73 65 74 20 ** called), set
1af17 74 68 65 20 64 61 74 61 62 61 73 65 20 65 72 72 the database err
1af18 6f 72 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 or in this case
1af19 61 73 20 77 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a as well.. */.
1af1a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1af1b 28 64 62 2c 20 70 2d 3e 72 63 2c 20 30 29 3b 0a (db, p->rc, 0);.
1af1c 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 sqlite3Value
1af1d 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c SetStr(db->pErr,
1af1e 20 2d 31 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 2c -1, p->zErrMsg,
1af1f 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 SQLITE_UTF8, SQ
1af20 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b LITE_TRANSIENT);
1af21 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1af22 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 ee(db, p->zErrMs
1af23 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d g);. p->zErrM
1af24 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f sg = 0;. }.. /
1af25 2a 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 * Reclaim all me
1af26 6d 6f 72 79 20 75 73 65 64 20 62 79 20 74 68 65 mory used by the
1af27 20 56 44 42 45 0a 20 20 2a 2f 0a 20 20 43 6c 65 VDBE. */. Cle
1af28 61 6e 75 70 28 70 29 3b 0a 0a 20 20 2f 2a 20 53 anup(p);.. /* S
1af29 61 76 65 20 70 72 6f 66 69 6c 69 6e 67 20 69 6e ave profiling in
1af2a 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 formation from t
1af2b 68 69 73 20 56 44 42 45 20 72 75 6e 2e 0a 20 20 his VDBE run..
1af2c 2a 2f 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 */.#ifdef VDBE_P
1af2d 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20 46 ROFILE. {. F
1af2e 49 4c 45 20 2a 6f 75 74 20 3d 20 66 6f 70 65 6e ILE *out = fopen
1af2f 28 22 76 64 62 65 5f 70 72 6f 66 69 6c 65 2e 6f ("vdbe_profile.o
1af30 75 74 22 2c 20 22 61 22 29 3b 0a 20 20 20 20 69 ut", "a");. i
1af31 66 28 20 6f 75 74 20 29 7b 0a 20 20 20 20 20 20 f( out ){.
1af32 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 66 70 72 int i;. fpr
1af33 69 6e 74 66 28 6f 75 74 2c 20 22 2d 2d 2d 2d 20 intf(out, "----
1af34 22 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d ");. for(i=
1af35 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 0; i<p->nOp; i++
1af36 29 7b 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e ){. fprin
1af37 74 66 28 6f 75 74 2c 20 22 25 30 32 78 22 2c 20 tf(out, "%02x",
1af38 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 p->aOp[i].opcode
1af39 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1af3a 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c fprintf(out, "\
1af3b 6e 22 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 n");. for(i
1af3c 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b =0; i<p->nOp; i+
1af3d 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 70 72 69 +){. fpri
1af3e 6e 74 66 28 6f 75 74 2c 20 22 25 36 64 20 25 31 ntf(out, "%6d %1
1af3f 30 6c 6c 64 20 25 38 6c 6c 64 20 22 2c 0a 20 20 0lld %8lld ",.
1af40 20 20 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b p->aOp[
1af41 69 5d 2e 63 6e 74 2c 0a 20 20 20 20 20 20 20 20 i].cnt,.
1af42 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 p->aOp[i].cyc
1af43 6c 65 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 les,.
1af44 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 3e 30 20 p->aOp[i].cnt>0
1af45 3f 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c ? p->aOp[i].cycl
1af46 65 73 2f 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 es/p->aOp[i].cnt
1af47 20 3a 20 30 0a 20 20 20 20 20 20 20 20 29 3b 0a : 0. );.
1af48 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1af49 64 62 65 50 72 69 6e 74 4f 70 28 6f 75 74 2c 20 dbePrintOp(out,
1af4a 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a i, &p->aOp[i]);.
1af4b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 63 }. fc
1af4c 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20 20 20 7d lose(out);. }
1af4d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 2d . }.#endif. p-
1af4e 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 >magic = VDBE_MA
1af4f 47 49 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 GIC_INIT;. retu
1af50 72 6e 20 70 2d 3e 72 63 20 26 20 64 62 2d 3e 65 rn p->rc & db->e
1af51 72 72 4d 61 73 6b 3b 0a 7d 0a 20 0a 2f 2a 0a 2a rrMask;.}. ./*.*
1af52 2a 20 43 6c 65 61 6e 20 75 70 20 61 6e 64 20 64 * Clean up and d
1af53 65 6c 65 74 65 20 61 20 56 44 42 45 20 61 66 74 elete a VDBE aft
1af54 65 72 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 52 er execution. R
1af55 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 eturn an integer
1af56 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 which is.** the
1af57 20 72 65 73 75 6c 74 20 63 6f 64 65 2e 20 20 57 result code. W
1af58 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d rite any error m
1af59 65 73 73 61 67 65 20 74 65 78 74 20 69 6e 74 6f essage text into
1af5a 20 2a 70 7a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 *pzErrMsg..*/.S
1af5b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1af5c 74 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e t sqlite3VdbeFin
1af5d 61 6c 69 7a 65 28 56 64 62 65 20 2a 70 29 7b 0a alize(Vdbe *p){.
1af5e 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1af5f 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 6d E_OK;. if( p->m
1af60 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 agic==VDBE_MAGIC
1af61 5f 52 55 4e 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 _RUN || p->magic
1af62 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c ==VDBE_MAGIC_HAL
1af63 54 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 T ){. rc = sq
1af64 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28 70 lite3VdbeReset(p
1af65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 );. assert( (
1af66 72 63 20 26 20 70 2d 3e 64 62 2d 3e 65 72 72 4d rc & p->db->errM
1af67 61 73 6b 29 3d 3d 72 63 20 29 3b 0a 20 20 7d 0a ask)==rc );. }.
1af68 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c sqlite3VdbeDel
1af69 65 74 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ete(p);. return
1af6a 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 rc;.}../*.** Ca
1af6b 6c 6c 20 74 68 65 20 64 65 73 74 72 75 63 74 6f ll the destructo
1af6c 72 20 66 6f 72 20 65 61 63 68 20 61 75 78 64 61 r for each auxda
1af6d 74 61 20 65 6e 74 72 79 20 69 6e 20 70 56 64 62 ta entry in pVdb
1af6e 65 46 75 6e 63 20 66 6f 72 20 77 68 69 63 68 0a eFunc for which.
1af6f 2a 2a 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e ** the correspon
1af70 64 69 6e 67 20 62 69 74 20 69 6e 20 6d 61 73 6b ding bit in mask
1af71 20 69 73 20 63 6c 65 61 72 2e 20 20 41 75 78 64 is clear. Auxd
1af72 61 74 61 20 65 6e 74 72 69 65 73 20 62 65 79 6f ata entries beyo
1af73 6e 64 20 33 31 0a 2a 2a 20 61 72 65 20 61 6c 77 nd 31.** are alw
1af74 61 79 73 20 64 65 73 74 72 6f 79 65 64 2e 20 20 ays destroyed.
1af75 54 6f 20 64 65 73 74 72 6f 79 20 61 6c 6c 20 61 To destroy all a
1af76 75 78 64 61 74 61 20 65 6e 74 72 69 65 73 2c 20 uxdata entries,
1af77 63 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 call this.** rou
1af78 74 69 6e 65 20 77 69 74 68 20 6d 61 73 6b 3d 3d tine with mask==
1af79 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 0..*/.SQLITE_PRI
1af7a 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1af7b 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 3VdbeDeleteAuxDa
1af7c 74 61 28 56 64 62 65 46 75 6e 63 20 2a 70 56 64 ta(VdbeFunc *pVd
1af7d 62 65 46 75 6e 63 2c 20 69 6e 74 20 6d 61 73 6b beFunc, int mask
1af7e 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f ){. int i;. fo
1af7f 72 28 69 3d 30 3b 20 69 3c 70 56 64 62 65 46 75 r(i=0; i<pVdbeFu
1af80 6e 63 2d 3e 6e 41 75 78 3b 20 69 2b 2b 29 7b 0a nc->nAux; i++){.
1af81 20 20 20 20 73 74 72 75 63 74 20 41 75 78 44 61 struct AuxDa
1af82 74 61 20 2a 70 41 75 78 20 3d 20 26 70 56 64 62 ta *pAux = &pVdb
1af83 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69 5d 3b eFunc->apAux[i];
1af84 0a 20 20 20 20 69 66 28 20 28 69 3e 33 31 20 7c . if( (i>31 |
1af85 7c 20 21 28 6d 61 73 6b 26 28 28 28 75 33 32 29 | !(mask&(((u32)
1af86 31 29 3c 3c 69 29 29 29 20 26 26 20 70 41 75 78 1)<<i))) && pAux
1af87 2d 3e 70 41 75 78 20 29 7b 0a 20 20 20 20 20 20 ->pAux ){.
1af88 69 66 28 20 70 41 75 78 2d 3e 78 44 65 6c 65 74 if( pAux->xDelet
1af89 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 41 75 e ){. pAu
1af8a 78 2d 3e 78 44 65 6c 65 74 65 28 70 41 75 78 2d x->xDelete(pAux-
1af8b 3e 70 41 75 78 29 3b 0a 20 20 20 20 20 20 7d 0a >pAux);. }.
1af8c 20 20 20 20 20 20 70 41 75 78 2d 3e 70 41 75 78 pAux->pAux
1af8d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 0;. }. }.
1af8e 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 }../*.** Delete
1af8f 61 6e 20 65 6e 74 69 72 65 20 56 44 42 45 2e 0a an entire VDBE..
1af90 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1af91 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1af92 62 65 44 65 6c 65 74 65 28 56 64 62 65 20 2a 70 beDelete(Vdbe *p
1af93 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 ){. sqlite3 *db
1af94 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 ;.. if( NEVER(p
1af95 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 ==0) ) return;.
1af96 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 db = p->db;. i
1af97 66 28 20 70 2d 3e 70 50 72 65 76 20 29 7b 0a 20 f( p->pPrev ){.
1af98 20 20 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 p->pPrev->pNe
1af99 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 xt = p->pNext;.
1af9a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
1af9b 72 74 28 20 64 62 2d 3e 70 56 64 62 65 3d 3d 70 rt( db->pVdbe==p
1af9c 20 29 3b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 );. db->pVdb
1af9d 65 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 e = p->pNext;.
1af9e 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 }. if( p->pNext
1af9f 20 29 7b 0a 20 20 20 20 70 2d 3e 70 4e 65 78 74 ){. p->pNext
1afa0 2d 3e 70 50 72 65 76 20 3d 20 70 2d 3e 70 50 72 ->pPrev = p->pPr
1afa1 65 76 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 ev;. }. releas
1afa2 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 56 61 eMemArray(p->aVa
1afa3 72 2c 20 70 2d 3e 6e 56 61 72 29 3b 0a 20 20 72 r, p->nVar);. r
1afa4 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 eleaseMemArray(p
1afa5 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e ->aColName, p->n
1afa6 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d ResColumn*COLNAM
1afa7 45 5f 4e 29 3b 0a 20 20 76 64 62 65 46 72 65 65 E_N);. vdbeFree
1afa8 4f 70 41 72 72 61 79 28 64 62 2c 20 70 2d 3e 61 OpArray(db, p->a
1afa9 4f 70 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 20 20 73 Op, p->nOp);. s
1afaa 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1afab 20 70 2d 3e 61 4c 61 62 65 6c 29 3b 0a 20 20 73 p->aLabel);. s
1afac 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1afad 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 p->aColName);.
1afae 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1afaf 62 2c 20 70 2d 3e 7a 53 71 6c 29 3b 0a 20 20 70 b, p->zSql);. p
1afb0 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d ->magic = VDBE_M
1afb1 41 47 49 43 5f 44 45 41 44 3b 0a 20 20 73 71 6c AGIC_DEAD;. sql
1afb2 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1afb3 2d 3e 70 46 72 65 65 29 3b 0a 20 20 73 71 6c 69 ->pFree);. sqli
1afb4 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 te3DbFree(db, p)
1afb5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 ;.}../*.** Make
1afb6 73 75 72 65 20 74 68 65 20 63 75 72 73 6f 72 20 sure the cursor
1afb7 70 20 69 73 20 72 65 61 64 79 20 74 6f 20 72 65 p is ready to re
1afb8 61 64 20 6f 72 20 77 72 69 74 65 20 74 68 65 20 ad or write the
1afb9 72 6f 77 20 74 6f 20 77 68 69 63 68 20 69 74 0a row to which it.
1afba 2a 2a 20 77 61 73 20 6c 61 73 74 20 70 6f 73 69 ** was last posi
1afbb 74 69 6f 6e 65 64 2e 20 20 52 65 74 75 72 6e 20 tioned. Return
1afbc 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 an error code if
1afbd 20 61 6e 20 4f 4f 4d 20 66 61 75 6c 74 20 6f 72 an OOM fault or
1afbe 20 49 2f 4f 20 65 72 72 6f 72 0a 2a 2a 20 70 72 I/O error.** pr
1afbf 65 76 65 6e 74 73 20 75 73 20 66 72 6f 6d 20 70 events us from p
1afc0 6f 73 69 74 69 6f 6e 69 6e 67 20 74 68 65 20 63 ositioning the c
1afc1 75 72 73 6f 72 20 74 6f 20 69 74 73 20 63 6f 72 ursor to its cor
1afc2 72 65 63 74 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a rect position..*
1afc3 2a 0a 2a 2a 20 49 66 20 61 20 4d 6f 76 65 54 6f *.** If a MoveTo
1afc4 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 70 65 operation is pe
1afc5 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 67 69 76 nding on the giv
1afc6 65 6e 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20 en cursor, then
1afc7 64 6f 20 74 68 61 74 0a 2a 2a 20 4d 6f 76 65 54 do that.** MoveT
1afc8 6f 20 6e 6f 77 2e 20 20 49 66 20 6e 6f 20 6d 6f o now. If no mo
1afc9 76 65 20 69 73 20 70 65 6e 64 69 6e 67 2c 20 63 ve is pending, c
1afca 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 heck to see if t
1afcb 68 65 20 72 6f 77 20 68 61 73 20 62 65 65 6e 0a he row has been.
1afcc 2a 2a 20 64 65 6c 65 74 65 64 20 6f 75 74 20 66 ** deleted out f
1afcd 72 6f 6d 20 75 6e 64 65 72 20 74 68 65 20 63 75 rom under the cu
1afce 72 73 6f 72 20 61 6e 64 20 69 66 20 69 74 20 68 rsor and if it h
1afcf 61 73 2c 20 6d 61 72 6b 20 74 68 65 20 72 6f 77 as, mark the row
1afd0 20 61 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 72 6f as.** a NULL ro
1afd1 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 w..**.** If the
1afd2 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61 64 cursor is alread
1afd3 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 y pointing to th
1afd4 65 20 63 6f 72 72 65 63 74 20 72 6f 77 20 61 6e e correct row an
1afd5 64 20 74 68 61 74 20 72 6f 77 20 68 61 73 0a 2a d that row has.*
1afd6 2a 20 6e 6f 74 20 62 65 65 6e 20 64 65 6c 65 74 * not been delet
1afd7 65 64 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 ed out from unde
1afd8 72 20 74 68 65 20 63 75 72 73 6f 72 2c 20 74 68 r the cursor, th
1afd9 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 en this routine
1afda 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 is a no-op..*/.S
1afdb 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1afdc 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 t sqlite3VdbeCur
1afdd 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 43 75 sorMoveto(VdbeCu
1afde 72 73 6f 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 rsor *p){. if(
1afdf 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 p->deferredMovet
1afe0 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 o ){. int res
1afe1 2c 20 72 63 3b 0a 23 69 66 64 65 66 20 53 51 4c , rc;.#ifdef SQL
1afe2 49 54 45 5f 54 45 53 54 0a 20 20 20 20 65 78 74 ITE_TEST. ext
1afe3 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f ern int sqlite3_
1afe4 73 65 61 72 63 68 5f 63 6f 75 6e 74 3b 0a 23 65 search_count;.#e
1afe5 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72 74 28 ndif. assert(
1afe6 20 70 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 p->isTable );.
1afe7 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1afe8 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b treeMovetoUnpack
1afe9 65 64 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 30 ed(p->pCursor, 0
1afea 2c 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 , p->movetoTarge
1afeb 74 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 t, 0, &res);.
1afec 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
1afed 20 72 63 3b 0a 20 20 20 20 70 2d 3e 6c 61 73 74 rc;. p->last
1afee 52 6f 77 69 64 20 3d 20 70 2d 3e 6d 6f 76 65 74 Rowid = p->movet
1afef 6f 54 61 72 67 65 74 3b 0a 20 20 20 20 70 2d 3e oTarget;. p->
1aff0 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 41 rowidIsValid = A
1aff1 4c 57 41 59 53 28 72 65 73 3d 3d 30 29 20 3f 31 LWAYS(res==0) ?1
1aff2 3a 30 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 :0;. if( NEVE
1aff3 52 28 72 65 73 3c 30 29 20 29 7b 0a 20 20 20 20 R(res<0) ){.
1aff4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1aff5 72 65 65 4e 65 78 74 28 70 2d 3e 70 43 75 72 73 reeNext(p->pCurs
1aff6 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 or, &res);.
1aff7 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
1aff8 20 72 63 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 rc;. }.#ifde
1aff9 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 f SQLITE_TEST.
1affa 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 sqlite3_search
1affb 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 _count++;.#endif
1affc 0a 20 20 20 20 70 2d 3e 64 65 66 65 72 72 65 64 . p->deferred
1affd 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 Moveto = 0;.
1affe 70 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d p->cacheStatus =
1afff 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 CACHE_STALE;.
1b000 7d 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53 }else if( ALWAYS
1b001 28 70 2d 3e 70 43 75 72 73 6f 72 29 20 29 7b 0a (p->pCursor) ){.
1b002 20 20 20 20 69 6e 74 20 68 61 73 4d 6f 76 65 64 int hasMoved
1b003 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 ;. int rc = s
1b004 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
1b005 72 48 61 73 4d 6f 76 65 64 28 70 2d 3e 70 43 75 rHasMoved(p->pCu
1b006 72 73 6f 72 2c 20 26 68 61 73 4d 6f 76 65 64 29 rsor, &hasMoved)
1b007 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 ;. if( rc ) r
1b008 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 eturn rc;. if
1b009 28 20 68 61 73 4d 6f 76 65 64 20 29 7b 0a 20 20 ( hasMoved ){.
1b00a 20 20 20 20 70 2d 3e 63 61 63 68 65 53 74 61 74 p->cacheStat
1b00b 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 us = CACHE_STALE
1b00c 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 75 6c 6c 52 ;. p->nullR
1b00d 6f 77 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 ow = 1;. }.
1b00e 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
1b00f 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK;.}../*.** T
1b010 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e he following fun
1b011 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 ctions:.**.** sq
1b012 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 lite3VdbeSerialT
1b013 79 70 65 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 ype().** sqlite3
1b014 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 VdbeSerialTypeLe
1b015 6e 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 n().** sqlite3Vd
1b016 62 65 53 65 72 69 61 6c 4c 65 6e 28 29 0a 2a 2a beSerialLen().**
1b017 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1b018 61 6c 50 75 74 28 29 0a 2a 2a 20 73 71 6c 69 74 alPut().** sqlit
1b019 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 e3VdbeSerialGet(
1b01a 29 0a 2a 2a 0a 2a 2a 20 65 6e 63 61 70 73 75 6c ).**.** encapsul
1b01b 61 74 65 20 74 68 65 20 63 6f 64 65 20 74 68 61 ate the code tha
1b01c 74 20 73 65 72 69 61 6c 69 7a 65 73 20 76 61 6c t serializes val
1b01d 75 65 73 20 66 6f 72 20 73 74 6f 72 61 67 65 20 ues for storage
1b01e 69 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 64 61 74 in SQLite.** dat
1b01f 61 20 61 6e 64 20 69 6e 64 65 78 20 72 65 63 6f a and index reco
1b020 72 64 73 2e 20 45 61 63 68 20 73 65 72 69 61 6c rds. Each serial
1b021 69 7a 65 64 20 76 61 6c 75 65 20 63 6f 6e 73 69 ized value consi
1b022 73 74 73 20 6f 66 20 61 0a 2a 2a 20 27 73 65 72 sts of a.** 'ser
1b023 69 61 6c 2d 74 79 70 65 27 20 61 6e 64 20 61 20 ial-type' and a
1b024 62 6c 6f 62 20 6f 66 20 64 61 74 61 2e 20 54 68 blob of data. Th
1b025 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 69 73 e serial type is
1b026 20 61 6e 20 38 2d 62 79 74 65 20 75 6e 73 69 67 an 8-byte unsig
1b027 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 ned.** integer,
1b028 73 74 6f 72 65 64 20 61 73 20 61 20 76 61 72 69 stored as a vari
1b029 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 20 nt..**.** In an
1b02a 53 51 4c 69 74 65 20 69 6e 64 65 78 20 72 65 63 SQLite index rec
1b02b 6f 72 64 2c 20 74 68 65 20 73 65 72 69 61 6c 20 ord, the serial
1b02c 74 79 70 65 20 69 73 20 73 74 6f 72 65 64 20 64 type is stored d
1b02d 69 72 65 63 74 6c 79 20 62 65 66 6f 72 65 0a 2a irectly before.*
1b02e 2a 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 64 61 * the blob of da
1b02f 74 61 20 74 68 61 74 20 69 74 20 63 6f 72 72 65 ta that it corre
1b030 73 70 6f 6e 64 73 20 74 6f 2e 20 49 6e 20 61 20 sponds to. In a
1b031 74 61 62 6c 65 20 72 65 63 6f 72 64 2c 20 61 6c table record, al
1b032 6c 20 73 65 72 69 61 6c 0a 2a 2a 20 74 79 70 65 l serial.** type
1b033 73 20 61 72 65 20 73 74 6f 72 65 64 20 61 74 20 s are stored at
1b034 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 the start of the
1b035 20 72 65 63 6f 72 64 2c 20 61 6e 64 20 74 68 65 record, and the
1b036 20 62 6c 6f 62 73 20 6f 66 20 64 61 74 61 20 61 blobs of data a
1b037 74 0a 2a 2a 20 74 68 65 20 65 6e 64 2e 20 48 65 t.** the end. He
1b038 6e 63 65 20 74 68 65 73 65 20 66 75 6e 63 74 69 nce these functi
1b039 6f 6e 73 20 61 6c 6c 6f 77 20 74 68 65 20 63 61 ons allow the ca
1b03a 6c 6c 65 72 20 74 6f 20 68 61 6e 64 6c 65 20 74 ller to handle t
1b03b 68 65 0a 2a 2a 20 73 65 72 69 61 6c 2d 74 79 70 he.** serial-typ
1b03c 65 20 61 6e 64 20 64 61 74 61 20 62 6c 6f 62 20 e and data blob
1b03d 73 65 70 65 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a seperately..**.*
1b03e 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
1b03f 74 61 62 6c 65 20 64 65 73 63 72 69 62 65 73 20 table describes
1b040 74 68 65 20 76 61 72 69 6f 75 73 20 73 74 6f 72 the various stor
1b041 61 67 65 20 63 6c 61 73 73 65 73 20 66 6f 72 20 age classes for
1b042 64 61 74 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 65 data:.**.** se
1b043 72 69 61 6c 20 74 79 70 65 20 20 20 20 20 20 20 rial type
1b044 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 20 bytes of data
1b045 20 20 20 20 74 79 70 65 0a 2a 2a 20 20 20 2d 2d type.** --
1b046 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 ------------
1b047 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
1b048 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ------------
1b049 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 ---.** 0
1b04a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b04b 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 4e 0 N
1b04c 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 31 20 20 20 ULL.** 1
1b04d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b04e 20 20 31 20 20 20 20 20 20 20 20 20 20 20 20 73 1 s
1b04f 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a igned integer.**
1b050 20 20 20 20 20 20 32 20 20 20 20 20 20 20 20 20 2
1b051 20 20 20 20 20 20 20 20 20 20 20 20 32 20 20 20 2
1b052 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 signed
1b053 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 integer.**
1b054 33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3
1b055 20 20 20 20 20 20 33 20 20 20 20 20 20 20 20 20 3
1b056 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 signed intege
1b057 72 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 r.** 4
1b058 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b059 34 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 4 sig
1b05a 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 ned integer.**
1b05b 20 20 20 20 35 20 20 20 20 20 20 20 20 20 20 20 5
1b05c 20 20 20 20 20 20 20 20 20 20 36 20 20 20 20 20 6
1b05d 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e signed in
1b05e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 36 20 teger.** 6
1b05f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b060 20 20 20 20 38 20 20 20 20 20 20 20 20 20 20 20 8
1b061 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a signed integer.
1b062 2a 2a 20 20 20 20 20 20 37 20 20 20 20 20 20 20 ** 7
1b063 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 20 8
1b064 20 20 20 20 20 20 20 20 20 20 20 49 45 45 45 20 IEEE
1b065 66 6c 6f 61 74 0a 2a 2a 20 20 20 20 20 20 38 20 float.** 8
1b066 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b067 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 0
1b068 20 49 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e Integer constan
1b069 74 20 30 0a 2a 2a 20 20 20 20 20 20 39 20 20 20 t 0.** 9
1b06a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b06b 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 49 0 I
1b06c 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 nteger constant
1b06d 31 0a 2a 2a 20 20 20 20 20 31 30 2c 31 31 20 20 1.** 10,11
1b06e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b06f 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 73 res
1b070 65 72 76 65 64 20 66 6f 72 20 65 78 70 61 6e 73 erved for expans
1b071 69 6f 6e 0a 2a 2a 20 20 20 20 4e 3e 3d 31 32 20 ion.** N>=12
1b072 61 6e 64 20 65 76 65 6e 20 20 20 20 20 20 20 28 and even (
1b073 4e 2d 31 32 29 2f 32 20 20 20 20 20 20 20 20 42 N-12)/2 B
1b074 4c 4f 42 0a 2a 2a 20 20 20 20 4e 3e 3d 31 33 20 LOB.** N>=13
1b075 61 6e 64 20 6f 64 64 20 20 20 20 20 20 20 20 28 and odd (
1b076 4e 2d 31 33 29 2f 32 20 20 20 20 20 20 20 20 74 N-13)/2 t
1b077 65 78 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 38 20 ext.**.** The 8
1b078 61 6e 64 20 39 20 74 79 70 65 73 20 77 65 72 65 and 9 types were
1b079 20 61 64 64 65 64 20 69 6e 20 33 2e 33 2e 30 2c added in 3.3.0,
1b07a 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 34 2e 20 file format 4.
1b07b 20 50 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 0a Prior versions.
1b07c 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 77 69 6c ** of SQLite wil
1b07d 6c 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 l not understand
1b07e 20 74 68 6f 73 65 20 73 65 72 69 61 6c 20 74 79 those serial ty
1b07f 70 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 pes..*/../*.** R
1b080 65 74 75 72 6e 20 74 68 65 20 73 65 72 69 61 6c eturn the serial
1b081 2d 74 79 70 65 20 66 6f 72 20 74 68 65 20 76 61 -type for the va
1b082 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 70 4d lue stored in pM
1b083 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 em..*/.SQLITE_PR
1b084 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 IVATE u32 sqlite
1b085 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 3VdbeSerialType(
1b086 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 Mem *pMem, int f
1b087 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a 20 20 69 ile_format){. i
1b088 6e 74 20 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d nt flags = pMem-
1b089 3e 66 6c 61 67 73 3b 0a 20 20 69 6e 74 20 6e 3b >flags;. int n;
1b08a 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 .. if( flags&ME
1b08b 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 M_Null ){. re
1b08c 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 turn 0;. }. if
1b08d 28 20 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 ( flags&MEM_Int
1b08e 29 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 ){. /* Figure
1b08f 20 6f 75 74 20 77 68 65 74 68 65 72 20 74 6f 20 out whether to
1b090 75 73 65 20 31 2c 20 32 2c 20 34 2c 20 36 20 6f use 1, 2, 4, 6 o
1b091 72 20 38 20 62 79 74 65 73 2e 20 2a 2f 0a 23 20 r 8 bytes. */.#
1b092 20 20 64 65 66 69 6e 65 20 4d 41 58 5f 36 42 59 define MAX_6BY
1b093 54 45 20 28 28 28 28 69 36 34 29 30 78 30 30 30 TE ((((i64)0x000
1b094 30 38 30 30 30 29 3c 3c 33 32 29 2d 31 29 0a 20 08000)<<32)-1).
1b095 20 20 20 69 36 34 20 69 20 3d 20 70 4d 65 6d 2d i64 i = pMem-
1b096 3e 75 2e 69 3b 0a 20 20 20 20 75 36 34 20 75 3b >u.i;. u64 u;
1b097 0a 20 20 20 20 69 66 28 20 66 69 6c 65 5f 66 6f . if( file_fo
1b098 72 6d 61 74 3e 3d 34 20 26 26 20 28 69 26 31 29 rmat>=4 && (i&1)
1b099 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ==i ){. ret
1b09a 75 72 6e 20 38 2b 28 75 33 32 29 69 3b 0a 20 20 urn 8+(u32)i;.
1b09b 20 20 7d 0a 20 20 20 20 75 20 3d 20 69 3c 30 20 }. u = i<0
1b09c 3f 20 2d 69 20 3a 20 69 3b 0a 20 20 20 20 69 66 ? -i : i;. if
1b09d 28 20 75 3c 3d 31 32 37 20 29 20 72 65 74 75 72 ( u<=127 ) retur
1b09e 6e 20 31 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d n 1;. if( u<=
1b09f 33 32 37 36 37 20 29 20 72 65 74 75 72 6e 20 32 32767 ) return 2
1b0a0 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 38 33 38 ;. if( u<=838
1b0a1 38 36 30 37 20 29 20 72 65 74 75 72 6e 20 33 3b 8607 ) return 3;
1b0a2 0a 20 20 20 20 69 66 28 20 75 3c 3d 32 31 34 37 . if( u<=2147
1b0a3 34 38 33 36 34 37 20 29 20 72 65 74 75 72 6e 20 483647 ) return
1b0a4 34 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 4d 41 4;. if( u<=MA
1b0a5 58 5f 36 42 59 54 45 20 29 20 72 65 74 75 72 6e X_6BYTE ) return
1b0a6 20 35 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 36 5;. return 6
1b0a7 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 ;. }. if( flag
1b0a8 73 26 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 s&MEM_Real ){.
1b0a9 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 20 7d 0a return 7;. }.
1b0aa 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
1b0ab 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1b0ac 20 7c 7c 20 66 6c 61 67 73 26 28 4d 45 4d 5f 53 || flags&(MEM_S
1b0ad 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 3b 0a tr|MEM_Blob) );.
1b0ae 20 20 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 n = pMem->n;.
1b0af 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d if( flags & MEM
1b0b0 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 6e 20 2b _Zero ){. n +
1b0b1 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b = pMem->u.nZero;
1b0b2 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e . }. assert( n
1b0b3 3e 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 >=0 );. return
1b0b4 28 28 6e 2a 32 29 20 2b 20 31 32 20 2b 20 28 28 ((n*2) + 12 + ((
1b0b5 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 21 3d flags&MEM_Str)!=
1b0b6 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 0));.}../*.** Re
1b0b7 74 75 72 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 turn the length
1b0b8 6f 66 20 74 68 65 20 64 61 74 61 20 63 6f 72 72 of the data corr
1b0b9 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 esponding to the
1b0ba 20 73 75 70 70 6c 69 65 64 20 73 65 72 69 61 6c supplied serial
1b0bb 2d 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 -type..*/.SQLITE
1b0bc 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c _PRIVATE u32 sql
1b0bd 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 ite3VdbeSerialTy
1b0be 70 65 4c 65 6e 28 75 33 32 20 73 65 72 69 61 6c peLen(u32 serial
1b0bf 5f 74 79 70 65 29 7b 0a 20 20 69 66 28 20 73 65 _type){. if( se
1b0c0 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b rial_type>=12 ){
1b0c1 0a 20 20 20 20 72 65 74 75 72 6e 20 28 73 65 72 . return (ser
1b0c2 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f 32 3b 0a ial_type-12)/2;.
1b0c3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 74 61 }else{. sta
1b0c4 74 69 63 20 63 6f 6e 73 74 20 75 38 20 61 53 69 tic const u8 aSi
1b0c5 7a 65 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c 20 32 ze[] = { 0, 1, 2
1b0c6 2c 20 33 2c 20 34 2c 20 36 2c 20 38 2c 20 38 2c , 3, 4, 6, 8, 8,
1b0c7 20 30 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 20 0, 0, 0, 0 };.
1b0c8 20 20 20 72 65 74 75 72 6e 20 61 53 69 7a 65 5b return aSize[
1b0c9 73 65 72 69 61 6c 5f 74 79 70 65 5d 3b 0a 20 20 serial_type];.
1b0ca 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 }.}../*.** If we
1b0cb 20 61 72 65 20 6f 6e 20 61 6e 20 61 72 63 68 69 are on an archi
1b0cc 74 65 63 74 75 72 65 20 77 69 74 68 20 6d 69 78 tecture with mix
1b0cd 65 64 2d 65 6e 64 69 61 6e 20 66 6c 6f 61 74 69 ed-endian floati
1b0ce 6e 67 20 0a 2a 2a 20 70 6f 69 6e 74 73 20 28 65 ng .** points (e
1b0cf 78 3a 20 41 52 4d 37 29 20 74 68 65 6e 20 73 77 x: ARM7) then sw
1b0d0 61 70 20 74 68 65 20 6c 6f 77 65 72 20 34 20 62 ap the lower 4 b
1b0d1 79 74 65 73 20 77 69 74 68 20 74 68 65 20 0a 2a ytes with the .*
1b0d2 2a 20 75 70 70 65 72 20 34 20 62 79 74 65 73 2e * upper 4 bytes.
1b0d3 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 73 Return the res
1b0d4 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 6d ult..**.** For m
1b0d5 6f 73 74 20 61 72 63 68 69 74 65 63 74 75 72 65 ost architecture
1b0d6 73 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d s, this is a no-
1b0d7 6f 70 2e 0a 2a 2a 0a 2a 2a 20 28 6c 61 74 65 72 op..**.** (later
1b0d8 29 3a 20 20 49 74 20 69 73 20 72 65 70 6f 72 74 ): It is report
1b0d9 65 64 20 74 6f 20 6d 65 20 74 68 61 74 20 74 68 ed to me that th
1b0da 65 20 6d 69 78 65 64 2d 65 6e 64 69 61 6e 20 70 e mixed-endian p
1b0db 72 6f 62 6c 65 6d 0a 2a 2a 20 6f 6e 20 41 52 4d roblem.** on ARM
1b0dc 37 20 69 73 20 61 6e 20 69 73 73 75 65 20 77 69 7 is an issue wi
1b0dd 74 68 20 47 43 43 2c 20 6e 6f 74 20 77 69 74 68 th GCC, not with
1b0de 20 74 68 65 20 41 52 4d 37 20 63 68 69 70 2e 20 the ARM7 chip.
1b0df 20 49 74 20 73 65 65 6d 73 0a 2a 2a 20 74 68 61 It seems.** tha
1b0e0 74 20 65 61 72 6c 79 20 76 65 72 73 69 6f 6e 73 t early versions
1b0e1 20 6f 66 20 47 43 43 20 73 74 6f 72 65 64 20 74 of GCC stored t
1b0e2 68 65 20 74 77 6f 20 77 6f 72 64 73 20 6f 66 20 he two words of
1b0e3 61 20 36 34 2d 62 69 74 0a 2a 2a 20 66 6c 6f 61 a 64-bit.** floa
1b0e4 74 20 69 6e 20 74 68 65 20 77 72 6f 6e 67 20 6f t in the wrong o
1b0e5 72 64 65 72 2e 20 20 41 6e 64 20 74 68 61 74 20 rder. And that
1b0e6 65 72 72 6f 72 20 68 61 73 20 62 65 65 6e 20 70 error has been p
1b0e7 72 6f 70 61 67 61 74 65 64 0a 2a 2a 20 65 76 65 ropagated.** eve
1b0e8 72 20 73 69 6e 63 65 2e 20 20 54 68 65 20 62 6c r since. The bl
1b0e9 61 6d 65 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 ame is not neces
1b0ea 73 61 72 69 6c 79 20 77 69 74 68 20 47 43 43 2c sarily with GCC,
1b0eb 20 74 68 6f 75 67 68 2e 0a 2a 2a 20 47 43 43 20 though..** GCC
1b0ec 6d 69 67 68 74 20 68 61 76 65 20 6a 75 73 74 20 might have just
1b0ed 63 6f 70 79 69 6e 67 20 74 68 65 20 70 72 6f 62 copying the prob
1b0ee 6c 65 6d 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 lem from a prior
1b0ef 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2a 20 49 20 compiler..** I
1b0f0 61 6d 20 61 6c 73 6f 20 74 6f 6c 64 20 74 68 61 am also told tha
1b0f1 74 20 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73 t newer versions
1b0f2 20 6f 66 20 47 43 43 20 74 68 61 74 20 66 6f 6c of GCC that fol
1b0f3 6c 6f 77 20 61 20 64 69 66 66 65 72 65 6e 74 0a low a different.
1b0f4 2a 2a 20 41 42 49 20 67 65 74 20 74 68 65 20 62 ** ABI get the b
1b0f5 79 74 65 20 6f 72 64 65 72 20 72 69 67 68 74 2e yte order right.
1b0f6 0a 2a 2a 0a 2a 2a 20 44 65 76 65 6c 6f 70 65 72 .**.** Developer
1b0f7 73 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 6f s using SQLite o
1b0f8 6e 20 61 6e 20 41 52 4d 37 20 73 68 6f 75 6c 64 n an ARM7 should
1b0f9 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e compile and run
1b0fa 20 74 68 65 69 72 0a 2a 2a 20 61 70 70 6c 69 63 their.** applic
1b0fb 61 74 69 6f 6e 20 75 73 69 6e 67 20 2d 44 53 51 ation using -DSQ
1b0fc 4c 49 54 45 5f 44 45 42 55 47 3d 31 20 61 74 20 LITE_DEBUG=1 at
1b0fd 6c 65 61 73 74 20 6f 6e 63 65 2e 20 20 57 69 74 least once. Wit
1b0fe 68 20 44 45 42 55 47 0a 2a 2a 20 65 6e 61 62 6c h DEBUG.** enabl
1b0ff 65 64 2c 20 73 6f 6d 65 20 61 73 73 65 72 74 73 ed, some asserts
1b100 20 62 65 6c 6f 77 20 77 69 6c 6c 20 65 6e 73 75 below will ensu
1b101 72 65 20 74 68 61 74 20 74 68 65 20 62 79 74 65 re that the byte
1b102 20 6f 72 64 65 72 20 6f 66 0a 2a 2a 20 66 6c 6f order of.** flo
1b103 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 ating point valu
1b104 65 73 20 69 73 20 63 6f 72 72 65 63 74 2e 0a 2a es is correct..*
1b105 2a 0a 2a 2a 20 28 32 30 30 37 2d 30 38 2d 33 30 *.** (2007-08-30
1b106 29 20 20 46 72 61 6e 6b 20 76 61 6e 20 56 75 67 ) Frank van Vug
1b107 74 20 68 61 73 20 73 74 75 64 69 65 64 20 74 68 t has studied th
1b108 69 73 20 70 72 6f 62 6c 65 6d 20 63 6c 6f 73 65 is problem close
1b109 6c 79 0a 2a 2a 20 61 6e 64 20 68 61 73 20 73 65 ly.** and has se
1b10a 6e 64 20 68 69 73 20 66 69 6e 64 69 6e 67 73 20 nd his findings
1b10b 74 6f 20 74 68 65 20 53 51 4c 69 74 65 20 64 65 to the SQLite de
1b10c 76 65 6c 6f 70 65 72 73 2e 20 20 46 72 61 6e 6b velopers. Frank
1b10d 0a 2a 2a 20 77 72 69 74 65 73 20 74 68 61 74 20 .** writes that
1b10e 73 6f 6d 65 20 4c 69 6e 75 78 20 6b 65 72 6e 65 some Linux kerne
1b10f 6c 73 20 6f 66 66 65 72 20 66 6c 6f 61 74 69 6e ls offer floatin
1b110 67 20 70 6f 69 6e 74 20 68 61 72 64 77 61 72 65 g point hardware
1b111 0a 2a 2a 20 65 6d 75 6c 61 74 69 6f 6e 20 74 68 .** emulation th
1b112 61 74 20 75 73 65 73 20 6f 6e 6c 79 20 33 32 2d at uses only 32-
1b113 62 69 74 20 6d 61 6e 74 69 73 73 61 73 20 69 6e bit mantissas in
1b114 73 74 65 61 64 20 6f 66 20 61 20 66 75 6c 6c 20 stead of a full
1b115 0a 2a 2a 20 34 38 2d 62 69 74 73 20 61 73 20 72 .** 48-bits as r
1b116 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 49 equired by the I
1b117 45 45 45 20 73 74 61 6e 64 61 72 64 2e 20 20 28 EEE standard. (
1b118 54 68 69 73 20 69 73 20 74 68 65 0a 2a 2a 20 43 This is the.** C
1b119 4f 4e 46 49 47 5f 46 50 45 5f 46 41 53 54 46 50 ONFIG_FPE_FASTFP
1b11a 45 20 6f 70 74 69 6f 6e 2e 29 20 20 4f 6e 20 73 E option.) On s
1b11b 75 63 68 20 73 79 73 74 65 6d 73 2c 20 66 6c 6f uch systems, flo
1b11c 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 62 ating point.** b
1b11d 79 74 65 20 73 77 61 70 70 69 6e 67 20 62 65 63 yte swapping bec
1b11e 6f 6d 65 73 20 76 65 72 79 20 63 6f 6d 70 6c 69 omes very compli
1b11f 63 61 74 65 64 2e 20 20 54 6f 20 61 76 6f 69 64 cated. To avoid
1b120 20 70 72 6f 62 6c 65 6d 73 2c 0a 2a 2a 20 74 68 problems,.** th
1b121 65 20 6e 65 63 65 73 73 61 72 79 20 62 79 74 65 e necessary byte
1b122 20 73 77 61 70 70 69 6e 67 20 69 73 20 63 61 72 swapping is car
1b123 72 69 65 64 20 6f 75 74 20 75 73 69 6e 67 20 61 ried out using a
1b124 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 0a 64-bit integer.
1b125 2a 2a 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 ** rather than a
1b126 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 2e 20 20 64-bit float.
1b127 46 72 61 6e 6b 20 61 73 73 75 72 65 73 20 75 73 Frank assures us
1b128 20 74 68 61 74 20 74 68 65 20 63 6f 64 65 20 68 that the code h
1b129 65 72 65 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 ere.** works for
1b12a 20 68 69 6d 2e 20 20 57 65 2c 20 74 68 65 20 64 him. We, the d
1b12b 65 76 65 6c 6f 70 65 72 73 2c 20 68 61 76 65 20 evelopers, have
1b12c 6e 6f 20 77 61 79 20 74 6f 20 69 6e 64 65 70 65 no way to indepe
1b12d 6e 64 65 6e 74 6c 79 0a 2a 2a 20 76 65 72 69 66 ndently.** verif
1b12e 79 20 74 68 69 73 2c 20 62 75 74 20 46 72 61 6e y this, but Fran
1b12f 6b 20 73 65 65 6d 73 20 74 6f 20 6b 6e 6f 77 20 k seems to know
1b130 77 68 61 74 20 68 65 20 69 73 20 74 61 6c 6b 69 what he is talki
1b131 6e 67 20 61 62 6f 75 74 0a 2a 2a 20 73 6f 20 77 ng about.** so w
1b132 65 20 74 72 75 73 74 20 68 69 6d 2e 0a 2a 2f 0a e trust him..*/.
1b133 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 49 #ifdef SQLITE_MI
1b134 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 XED_ENDIAN_64BIT
1b135 5f 46 4c 4f 41 54 0a 73 74 61 74 69 63 20 75 36 _FLOAT.static u6
1b136 34 20 66 6c 6f 61 74 53 77 61 70 28 75 36 34 20 4 floatSwap(u64
1b137 69 6e 29 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 in){. union {.
1b138 20 20 20 75 36 34 20 72 3b 0a 20 20 20 20 75 33 u64 r;. u3
1b139 32 20 69 5b 32 5d 3b 0a 20 20 7d 20 75 3b 0a 20 2 i[2];. } u;.
1b13a 20 75 33 32 20 74 3b 0a 0a 20 20 75 2e 72 20 3d u32 t;.. u.r =
1b13b 20 69 6e 3b 0a 20 20 74 20 3d 20 75 2e 69 5b 30 in;. t = u.i[0
1b13c 5d 3b 0a 20 20 75 2e 69 5b 30 5d 20 3d 20 75 2e ];. u.i[0] = u.
1b13d 69 5b 31 5d 3b 0a 20 20 75 2e 69 5b 31 5d 20 3d i[1];. u.i[1] =
1b13e 20 74 3b 0a 20 20 72 65 74 75 72 6e 20 75 2e 72 t;. return u.r
1b13f 3b 0a 7d 0a 23 20 64 65 66 69 6e 65 20 73 77 61 ;.}.# define swa
1b140 70 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 pMixedEndianFloa
1b141 74 28 58 29 20 20 58 20 3d 20 66 6c 6f 61 74 53 t(X) X = floatS
1b142 77 61 70 28 58 29 0a 23 65 6c 73 65 0a 23 20 64 wap(X).#else.# d
1b143 65 66 69 6e 65 20 73 77 61 70 4d 69 78 65 64 45 efine swapMixedE
1b144 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29 0a 23 65 ndianFloat(X).#e
1b145 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 ndif../*.** Writ
1b146 65 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 e the serialized
1b147 20 64 61 74 61 20 62 6c 6f 62 20 66 6f 72 20 74 data blob for t
1b148 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 he value stored
1b149 69 6e 20 70 4d 65 6d 20 69 6e 74 6f 20 0a 2a 2a in pMem into .**
1b14a 20 62 75 66 2e 20 49 74 20 69 73 20 61 73 73 75 buf. It is assu
1b14b 6d 65 64 20 74 68 61 74 20 74 68 65 20 63 61 6c med that the cal
1b14c 6c 65 72 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 ler has allocate
1b14d 64 20 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 d sufficient spa
1b14e 63 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ce..** Return th
1b14f 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
1b150 73 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a s written..**.**
1b151 20 6e 42 75 66 20 69 73 20 74 68 65 20 61 6d 6f nBuf is the amo
1b152 75 6e 74 20 6f 66 20 73 70 61 63 65 20 6c 65 66 unt of space lef
1b153 74 20 69 6e 20 62 75 66 5b 5d 2e 20 20 6e 42 75 t in buf[]. nBu
1b154 66 20 6d 75 73 74 20 61 6c 77 61 79 73 20 62 65 f must always be
1b155 0a 2a 2a 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 .** large enough
1b156 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 to hold the ent
1b157 69 72 65 20 66 69 65 6c 64 2e 20 20 45 78 63 65 ire field. Exce
1b158 70 74 2c 20 69 66 20 74 68 65 20 66 69 65 6c 64 pt, if the field
1b159 20 69 73 0a 2a 2a 20 61 20 62 6c 6f 62 20 77 69 is.** a blob wi
1b15a 74 68 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 th a zero-filled
1b15b 20 74 61 69 6c 2c 20 74 68 65 6e 20 62 75 66 5b tail, then buf[
1b15c 5d 20 6d 69 67 68 74 20 62 65 20 6a 75 73 74 20 ] might be just
1b15d 74 68 65 20 72 69 67 68 74 0a 2a 2a 20 73 69 7a the right.** siz
1b15e 65 20 74 6f 20 68 6f 6c 64 20 65 76 65 72 79 74 e to hold everyt
1b15f 68 69 6e 67 20 65 78 63 65 70 74 20 66 6f 72 20 hing except for
1b160 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 the zero-filled
1b161 74 61 69 6c 2e 20 20 49 66 20 62 75 66 5b 5d 0a tail. If buf[].
1b162 2a 2a 20 69 73 20 6f 6e 6c 79 20 62 69 67 20 65 ** is only big e
1b163 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 nough to hold th
1b164 65 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 65 66 69 e non-zero prefi
1b165 78 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 77 72 69 x, then only wri
1b166 74 65 20 74 68 61 74 0a 2a 2a 20 70 72 65 66 69 te that.** prefi
1b167 78 20 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 42 x into buf[]. B
1b168 75 74 20 69 66 20 62 75 66 5b 5d 20 69 73 20 6c ut if buf[] is l
1b169 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 68 arge enough to h
1b16a 6f 6c 64 20 62 6f 74 68 20 74 68 65 0a 2a 2a 20 old both the.**
1b16b 70 72 65 66 69 78 20 61 6e 64 20 74 68 65 20 74 prefix and the t
1b16c 61 69 6c 20 74 68 65 6e 20 77 72 69 74 65 20 74 ail then write t
1b16d 68 65 20 70 72 65 66 69 78 20 61 6e 64 20 73 65 he prefix and se
1b16e 74 20 74 68 65 20 74 61 69 6c 20 74 6f 20 61 6c t the tail to al
1b16f 6c 0a 2a 2a 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a l.** zeros..**.*
1b170 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
1b171 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 ber of bytes act
1b172 75 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e ually written in
1b173 74 6f 20 62 75 66 5b 5d 2e 20 20 54 68 65 20 6e to buf[]. The n
1b174 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 umber.** of byte
1b175 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d 66 69 s in the zero-fi
1b176 6c 6c 65 64 20 74 61 69 6c 20 69 73 20 69 6e 63 lled tail is inc
1b177 6c 75 64 65 64 20 69 6e 20 74 68 65 20 72 65 74 luded in the ret
1b178 75 72 6e 20 76 61 6c 75 65 20 6f 6e 6c 79 0a 2a urn value only.*
1b179 2a 20 69 66 20 74 68 6f 73 65 20 62 79 74 65 73 * if those bytes
1b17a 20 77 65 72 65 20 7a 65 72 6f 65 64 20 69 6e 20 were zeroed in
1b17b 62 75 66 5b 5d 2e 0a 2a 2f 20 0a 53 51 4c 49 54 buf[]..*/ .SQLIT
1b17c 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 E_PRIVATE u32 sq
1b17d 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 lite3VdbeSerialP
1b17e 75 74 28 75 38 20 2a 62 75 66 2c 20 69 6e 74 20 ut(u8 *buf, int
1b17f 6e 42 75 66 2c 20 4d 65 6d 20 2a 70 4d 65 6d 2c nBuf, Mem *pMem,
1b180 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 int file_format
1b181 29 7b 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f ){. u32 serial_
1b182 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64 type = sqlite3Vd
1b183 62 65 53 65 72 69 61 6c 54 79 70 65 28 70 4d 65 beSerialType(pMe
1b184 6d 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b m, file_format);
1b185 0a 20 20 75 33 32 20 6c 65 6e 3b 0a 0a 20 20 2f . u32 len;.. /
1b186 2a 20 49 6e 74 65 67 65 72 20 61 6e 64 20 52 65 * Integer and Re
1b187 61 6c 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 al */. if( seri
1b188 61 6c 5f 74 79 70 65 3c 3d 37 20 26 26 20 73 65 al_type<=7 && se
1b189 72 69 61 6c 5f 74 79 70 65 3e 30 20 29 7b 0a 20 rial_type>0 ){.
1b18a 20 20 20 75 36 34 20 76 3b 0a 20 20 20 20 75 33 u64 v;. u3
1b18b 32 20 69 3b 0a 20 20 20 20 69 66 28 20 73 65 72 2 i;. if( ser
1b18c 69 61 6c 5f 74 79 70 65 3d 3d 37 20 29 7b 0a 20 ial_type==7 ){.
1b18d 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a assert( siz
1b18e 65 6f 66 28 76 29 3d 3d 73 69 7a 65 6f 66 28 70 eof(v)==sizeof(p
1b18f 4d 65 6d 2d 3e 72 29 20 29 3b 0a 20 20 20 20 20 Mem->r) );.
1b190 20 6d 65 6d 63 70 79 28 26 76 2c 20 26 70 4d 65 memcpy(&v, &pMe
1b191 6d 2d 3e 72 2c 20 73 69 7a 65 6f 66 28 76 29 29 m->r, sizeof(v))
1b192 3b 0a 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 ;. swapMixe
1b193 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 76 29 3b dEndianFloat(v);
1b194 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1b195 20 20 76 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b v = pMem->u.i;
1b196 0a 20 20 20 20 7d 0a 20 20 20 20 6c 65 6e 20 3d . }. len =
1b197 20 69 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 i = sqlite3Vdbe
1b198 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 SerialTypeLen(se
1b199 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 rial_type);.
1b19a 61 73 73 65 72 74 28 20 6c 65 6e 3c 3d 28 75 33 assert( len<=(u3
1b19b 32 29 6e 42 75 66 20 29 3b 0a 20 20 20 20 77 68 2)nBuf );. wh
1b19c 69 6c 65 28 20 69 2d 2d 20 29 7b 0a 20 20 20 20 ile( i-- ){.
1b19d 20 20 62 75 66 5b 69 5d 20 3d 20 28 75 38 29 28 buf[i] = (u8)(
1b19e 76 26 30 78 46 46 29 3b 0a 20 20 20 20 20 20 76 v&0xFF);. v
1b19f 20 3e 3e 3d 20 38 3b 0a 20 20 20 20 7d 0a 20 20 >>= 8;. }.
1b1a0 20 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 return len;.
1b1a1 7d 0a 0a 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f }.. /* String o
1b1a2 72 20 62 6c 6f 62 20 2a 2f 0a 20 20 69 66 28 20 r blob */. if(
1b1a3 73 65 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20 serial_type>=12
1b1a4 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
1b1a5 4d 65 6d 2d 3e 6e 20 2b 20 28 28 70 4d 65 6d 2d Mem->n + ((pMem-
1b1a6 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 >flags & MEM_Zer
1b1a7 6f 29 3f 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f o)?pMem->u.nZero
1b1a8 3a 30 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 :0).
1b1a9 20 3d 3d 20 28 69 6e 74 29 73 71 6c 69 74 65 33 == (int)sqlite3
1b1aa 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 VdbeSerialTypeLe
1b1ab 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 29 20 29 n(serial_type) )
1b1ac 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d ;. assert( pM
1b1ad 65 6d 2d 3e 6e 3c 3d 6e 42 75 66 20 29 3b 0a 20 em->n<=nBuf );.
1b1ae 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e len = pMem->n
1b1af 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 62 75 66 ;. memcpy(buf
1b1b0 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6c 65 6e 29 3b , pMem->z, len);
1b1b1 0a 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 . if( pMem->f
1b1b2 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 lags & MEM_Zero
1b1b3 29 7b 0a 20 20 20 20 20 20 6c 65 6e 20 2b 3d 20 ){. len +=
1b1b4 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 pMem->u.nZero;.
1b1b5 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 42 75 assert( nBu
1b1b6 66 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 f>=0 );. if
1b1b7 28 20 6c 65 6e 20 3e 20 28 75 33 32 29 6e 42 75 ( len > (u32)nBu
1b1b8 66 20 29 7b 0a 20 20 20 20 20 20 20 20 6c 65 6e f ){. len
1b1b9 20 3d 20 28 75 33 32 29 6e 42 75 66 3b 0a 20 20 = (u32)nBuf;.
1b1ba 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 }. mems
1b1bb 65 74 28 26 62 75 66 5b 70 4d 65 6d 2d 3e 6e 5d et(&buf[pMem->n]
1b1bc 2c 20 30 2c 20 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e , 0, len-pMem->n
1b1bd 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 );. }. ret
1b1be 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 urn len;. }..
1b1bf 2f 2a 20 4e 55 4c 4c 20 6f 72 20 63 6f 6e 73 74 /* NULL or const
1b1c0 61 6e 74 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 ants 0 or 1 */.
1b1c1 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
1b1c2 0a 2a 2a 20 44 65 73 65 72 69 61 6c 69 7a 65 20 .** Deserialize
1b1c3 74 68 65 20 64 61 74 61 20 62 6c 6f 62 20 70 6f the data blob po
1b1c4 69 6e 74 65 64 20 74 6f 20 62 79 20 62 75 66 20 inted to by buf
1b1c5 61 73 20 73 65 72 69 61 6c 20 74 79 70 65 20 73 as serial type s
1b1c6 65 72 69 61 6c 5f 74 79 70 65 0a 2a 2a 20 61 6e erial_type.** an
1b1c7 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 d store the resu
1b1c8 6c 74 20 69 6e 20 70 4d 65 6d 2e 20 20 52 65 74 lt in pMem. Ret
1b1c9 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
1b1ca 66 20 62 79 74 65 73 20 72 65 61 64 2e 0a 2a 2f f bytes read..*/
1b1cb 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 .SQLITE_PRIVATE
1b1cc 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 u32 sqlite3Vdbe
1b1cd 53 65 72 69 61 6c 47 65 74 28 0a 20 20 63 6f 6e SerialGet(. con
1b1ce 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
1b1cf 20 2a 62 75 66 2c 20 20 20 20 20 2f 2a 20 42 75 *buf, /* Bu
1b1d0 66 66 65 72 20 74 6f 20 64 65 73 65 72 69 61 6c ffer to deserial
1b1d1 69 7a 65 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 ize from */. u3
1b1d2 32 20 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 20 2 serial_type,
1b1d3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
1b1d4 65 72 69 61 6c 20 74 79 70 65 20 74 6f 20 64 65 erial type to de
1b1d5 73 65 72 69 61 6c 69 7a 65 20 2a 2f 0a 20 20 4d serialize */. M
1b1d6 65 6d 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 em *pMem
1b1d7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1b1d8 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 77 Memory cell to w
1b1d9 72 69 74 65 20 76 61 6c 75 65 20 69 6e 74 6f 20 rite value into
1b1da 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 63 68 28 20 */.){. switch(
1b1db 73 65 72 69 61 6c 5f 74 79 70 65 20 29 7b 0a 20 serial_type ){.
1b1dc 20 20 20 63 61 73 65 20 31 30 3a 20 20 20 2f 2a case 10: /*
1b1dd 20 52 65 73 65 72 76 65 64 20 66 6f 72 20 66 75 Reserved for fu
1b1de 74 75 72 65 20 75 73 65 20 2a 2f 0a 20 20 20 20 ture use */.
1b1df 63 61 73 65 20 31 31 3a 20 20 20 2f 2a 20 52 65 case 11: /* Re
1b1e0 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 served for futur
1b1e1 65 20 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 e use */. cas
1b1e2 65 20 30 3a 20 7b 20 20 2f 2a 20 4e 55 4c 4c 20 e 0: { /* NULL
1b1e3 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 */. pMem->f
1b1e4 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b lags = MEM_Null;
1b1e5 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1b1e6 20 20 7d 0a 20 20 20 20 63 61 73 65 20 31 3a 20 }. case 1:
1b1e7 7b 20 2f 2a 20 31 2d 62 79 74 65 20 73 69 67 6e { /* 1-byte sign
1b1e8 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 ed integer */.
1b1e9 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 pMem->u.i =
1b1ea 28 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 (signed char)buf
1b1eb 5b 30 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d [0];. pMem-
1b1ec 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 >flags = MEM_Int
1b1ed 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 ;. return 1
1b1ee 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1b1ef 20 32 3a 20 7b 20 2f 2a 20 32 2d 62 79 74 65 20 2: { /* 2-byte
1b1f0 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a signed integer *
1b1f1 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e /. pMem->u.
1b1f2 69 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 i = (((signed ch
1b1f3 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c ar)buf[0])<<8) |
1b1f4 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20 70 buf[1];. p
1b1f5 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
1b1f6 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 _Int;. retu
1b1f7 72 6e 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 2;. }.
1b1f8 63 61 73 65 20 33 3a 20 7b 20 2f 2a 20 33 2d 62 case 3: { /* 3-b
1b1f9 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 yte signed integ
1b1fa 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d er */. pMem
1b1fb 2d 3e 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 ->u.i = (((signe
1b1fc 64 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c d char)buf[0])<<
1b1fd 31 36 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 38 16) | (buf[1]<<8
1b1fe 29 20 7c 20 62 75 66 5b 32 5d 3b 0a 20 20 20 20 ) | buf[2];.
1b1ff 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
1b200 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 MEM_Int;. r
1b201 65 74 75 72 6e 20 33 3b 0a 20 20 20 20 7d 0a 20 eturn 3;. }.
1b202 20 20 20 63 61 73 65 20 34 3a 20 7b 20 2f 2a 20 case 4: { /*
1b203 34 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 4-byte signed in
1b204 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 teger */. p
1b205 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 62 75 66 5b Mem->u.i = (buf[
1b206 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 0]<<24) | (buf[1
1b207 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 5d ]<<16) | (buf[2]
1b208 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 <<8) | buf[3];.
1b209 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
1b20a 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 = MEM_Int;.
1b20b 20 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 20 20 return 4;.
1b20c 7d 0a 20 20 20 20 63 61 73 65 20 35 3a 20 7b 20 }. case 5: {
1b20d 2f 2a 20 36 2d 62 79 74 65 20 73 69 67 6e 65 64 /* 6-byte signed
1b20e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 integer */.
1b20f 20 20 75 36 34 20 78 20 3d 20 28 28 28 73 69 67 u64 x = (((sig
1b210 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30 5d 29 ned char)buf[0])
1b211 3c 3c 38 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 <<8) | buf[1];.
1b212 20 20 20 20 20 75 33 32 20 79 20 3d 20 28 62 75 u32 y = (bu
1b213 66 5b 32 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 f[2]<<24) | (buf
1b214 5b 33 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b [3]<<16) | (buf[
1b215 34 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 35 5d 3b 4]<<8) | buf[5];
1b216 0a 20 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 . x = (x<<3
1b217 32 29 20 7c 20 79 3b 0a 20 20 20 20 20 20 70 4d 2) | y;. pM
1b218 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a em->u.i = *(i64*
1b219 29 26 78 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d )&x;. pMem-
1b21a 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 >flags = MEM_Int
1b21b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 36 ;. return 6
1b21c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1b21d 20 36 3a 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 6: /* 8-byte
1b21e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a signed integer *
1b21f 2f 0a 20 20 20 20 63 61 73 65 20 37 3a 20 7b 20 /. case 7: {
1b220 2f 2a 20 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 /* IEEE floating
1b221 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 point */.
1b222 75 36 34 20 78 3b 0a 20 20 20 20 20 20 75 33 32 u64 x;. u32
1b223 20 79 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 y;.#if !defined
1b224 28 4e 44 45 42 55 47 29 20 26 26 20 21 64 65 66 (NDEBUG) && !def
1b225 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1b226 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 _FLOATING_POINT)
1b227 0a 20 20 20 20 20 20 2f 2a 20 56 65 72 69 66 79 . /* Verify
1b228 20 74 68 61 74 20 69 6e 74 65 67 65 72 73 20 61 that integers a
1b229 6e 64 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e nd floating poin
1b22a 74 20 76 61 6c 75 65 73 20 75 73 65 20 74 68 65 t values use the
1b22b 20 73 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 same. ** b
1b22c 79 74 65 20 6f 72 64 65 72 2e 20 20 4f 72 2c 20 yte order. Or,
1b22d 74 68 61 74 20 69 66 20 53 51 4c 49 54 45 5f 4d that if SQLITE_M
1b22e 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 IXED_ENDIAN_64BI
1b22f 54 5f 46 4c 4f 41 54 20 69 73 0a 20 20 20 20 20 T_FLOAT is.
1b230 20 2a 2a 20 64 65 66 69 6e 65 64 20 74 68 61 74 ** defined that
1b231 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 64-bit floating
1b232 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 72 65 point values re
1b233 61 6c 6c 79 20 61 72 65 20 6d 69 78 65 64 0a 20 ally are mixed.
1b234 20 20 20 20 20 2a 2a 20 65 6e 64 69 61 6e 2e 0a ** endian..
1b235 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 */. s
1b236 74 61 74 69 63 20 63 6f 6e 73 74 20 75 36 34 20 tatic const u64
1b237 74 31 20 3d 20 28 28 75 36 34 29 30 78 33 66 66 t1 = ((u64)0x3ff
1b238 30 30 30 30 30 29 3c 3c 33 32 3b 0a 20 20 20 20 00000)<<32;.
1b239 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 64 static const d
1b23a 6f 75 62 6c 65 20 72 31 20 3d 20 31 2e 30 3b 0a ouble r1 = 1.0;.
1b23b 20 20 20 20 20 20 75 36 34 20 74 32 20 3d 20 74 u64 t2 = t
1b23c 31 3b 0a 20 20 20 20 20 20 73 77 61 70 4d 69 78 1;. swapMix
1b23d 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 74 32 edEndianFloat(t2
1b23e 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
1b23f 20 73 69 7a 65 6f 66 28 72 31 29 3d 3d 73 69 7a sizeof(r1)==siz
1b240 65 6f 66 28 74 32 29 20 26 26 20 6d 65 6d 63 6d eof(t2) && memcm
1b241 70 28 26 72 31 2c 20 26 74 32 2c 20 73 69 7a 65 p(&r1, &t2, size
1b242 6f 66 28 72 31 29 29 3d 3d 30 20 29 3b 0a 23 65 of(r1))==0 );.#e
1b243 6e 64 69 66 0a 0a 20 20 20 20 20 20 78 20 3d 20 ndif.. x =
1b244 28 62 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 (buf[0]<<24) | (
1b245 62 75 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 buf[1]<<16) | (b
1b246 75 66 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b uf[2]<<8) | buf[
1b247 33 5d 3b 0a 20 20 20 20 20 20 79 20 3d 20 28 62 3];. y = (b
1b248 75 66 5b 34 5d 3c 3c 32 34 29 20 7c 20 28 62 75 uf[4]<<24) | (bu
1b249 66 5b 35 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 f[5]<<16) | (buf
1b24a 5b 36 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 37 5d [6]<<8) | buf[7]
1b24b 3b 0a 20 20 20 20 20 20 78 20 3d 20 28 78 3c 3c ;. x = (x<<
1b24c 33 32 29 20 7c 20 79 3b 0a 20 20 20 20 20 20 69 32) | y;. i
1b24d 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d f( serial_type==
1b24e 36 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6 ){. pMe
1b24f 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 m->u.i = *(i64*)
1b250 26 78 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d &x;. pMem
1b251 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e ->flags = MEM_In
1b252 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a t;. }else{.
1b253 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1b254 73 69 7a 65 6f 66 28 78 29 3d 3d 38 20 26 26 20 sizeof(x)==8 &&
1b255 73 69 7a 65 6f 66 28 70 4d 65 6d 2d 3e 72 29 3d sizeof(pMem->r)=
1b256 3d 38 20 29 3b 0a 20 20 20 20 20 20 20 20 73 77 =8 );. sw
1b257 61 70 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f apMixedEndianFlo
1b258 61 74 28 78 29 3b 0a 20 20 20 20 20 20 20 20 6d at(x);. m
1b259 65 6d 63 70 79 28 26 70 4d 65 6d 2d 3e 72 2c 20 emcpy(&pMem->r,
1b25a 26 78 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a &x, sizeof(x));.
1b25b 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c pMem->fl
1b25c 61 67 73 20 3d 20 73 71 6c 69 74 65 33 49 73 4e ags = sqlite3IsN
1b25d 61 4e 28 70 4d 65 6d 2d 3e 72 29 20 3f 20 4d 45 aN(pMem->r) ? ME
1b25e 4d 5f 4e 75 6c 6c 20 3a 20 4d 45 4d 5f 52 65 61 M_Null : MEM_Rea
1b25f 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 l;. }.
1b260 20 72 65 74 75 72 6e 20 38 3b 0a 20 20 20 20 7d return 8;. }
1b261 0a 20 20 20 20 63 61 73 65 20 38 3a 20 20 20 20 . case 8:
1b262 2f 2a 20 49 6e 74 65 67 65 72 20 30 20 2a 2f 0a /* Integer 0 */.
1b263 20 20 20 20 63 61 73 65 20 39 3a 20 7b 20 20 2f case 9: { /
1b264 2a 20 49 6e 74 65 67 65 72 20 31 20 2a 2f 0a 20 * Integer 1 */.
1b265 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d pMem->u.i =
1b266 20 73 65 72 69 61 6c 5f 74 79 70 65 2d 38 3b 0a serial_type-8;.
1b267 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
1b268 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 s = MEM_Int;.
1b269 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 return 0;.
1b26a 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 }. default:
1b26b 7b 0a 20 20 20 20 20 20 75 33 32 20 6c 65 6e 20 {. u32 len
1b26c 3d 20 28 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 = (serial_type-1
1b26d 32 29 2f 32 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2)/2;. pMem
1b26e 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a 29 62 75 ->z = (char *)bu
1b26f 66 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e f;. pMem->n
1b270 20 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 70 4d = len;. pM
1b271 65 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 em->xDel = 0;.
1b272 20 20 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 if( serial_t
1b273 79 70 65 26 30 78 30 31 20 29 7b 0a 20 20 20 20 ype&0x01 ){.
1b274 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
1b275 3d 20 4d 45 4d 5f 53 74 72 20 7c 20 4d 45 4d 5f = MEM_Str | MEM_
1b276 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 7d 65 6c Ephem;. }el
1b277 73 65 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d se{. pMem
1b278 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c ->flags = MEM_Bl
1b279 6f 62 20 7c 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a ob | MEM_Ephem;.
1b27a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 }. re
1b27b 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 20 20 7d 0a turn len;. }.
1b27c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
1b27d 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 }.../*.** Given
1b27e 74 68 65 20 6e 4b 65 79 2d 62 79 74 65 20 65 6e the nKey-byte en
1b27f 63 6f 64 69 6e 67 20 6f 66 20 61 20 72 65 63 6f coding of a reco
1b280 72 64 20 69 6e 20 70 4b 65 79 5b 5d 2c 20 70 61 rd in pKey[], pa
1b281 72 73 65 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 rse the.** recor
1b282 64 20 69 6e 74 6f 20 61 20 55 6e 70 61 63 6b 65 d into a Unpacke
1b283 64 52 65 63 6f 72 64 20 73 74 72 75 63 74 75 72 dRecord structur
1b284 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 e. Return a poi
1b285 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68 61 74 20 nter to.** that
1b286 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a structure..**.**
1b287 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e The calling fun
1b288 63 74 69 6f 6e 20 6d 69 67 68 74 20 70 72 6f 76 ction might prov
1b289 69 64 65 20 73 7a 53 70 61 63 65 20 62 79 74 65 ide szSpace byte
1b28a 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 s of memory.** s
1b28b 70 61 63 65 20 61 74 20 70 53 70 61 63 65 2e 20 pace at pSpace.
1b28c 20 54 68 69 73 20 73 70 61 63 65 20 63 61 6e 20 This space can
1b28d 62 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 be used to hold
1b28e 74 68 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 the returned.**
1b28f 56 44 62 65 50 61 72 73 65 64 52 65 63 6f 72 64 VDbeParsedRecord
1b290 20 73 74 72 75 63 74 75 72 65 20 69 66 20 69 74 structure if it
1b291 20 69 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 is large enough
1b292 2e 20 20 49 66 20 69 74 20 69 73 0a 2a 2a 20 6e . If it is.** n
1b293 6f 74 20 62 69 67 20 65 6e 6f 75 67 68 2c 20 73 ot big enough, s
1b294 70 61 63 65 20 69 73 20 6f 62 74 61 69 6e 65 64 pace is obtained
1b295 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 from sqlite3_ma
1b296 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 lloc()..**.** Th
1b297 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 75 63 e returned struc
1b298 74 75 72 65 20 73 68 6f 75 6c 64 20 62 65 20 63 ture should be c
1b299 6c 6f 73 65 64 20 62 79 20 61 20 63 61 6c 6c 20 losed by a call
1b29a 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 to.** sqlite3Vdb
1b29b 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 eDeleteUnpackedR
1b29c 65 63 6f 72 64 28 29 2e 0a 2a 2f 20 0a 53 51 4c ecord()..*/ .SQL
1b29d 49 54 45 5f 50 52 49 56 41 54 45 20 55 6e 70 61 ITE_PRIVATE Unpa
1b29e 63 6b 65 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 ckedRecord *sqli
1b29f 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 te3VdbeRecordUnp
1b2a0 61 63 6b 28 0a 20 20 4b 65 79 49 6e 66 6f 20 2a ack(. KeyInfo *
1b2a1 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 2f 2a pKeyInfo, /*
1b2a2 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f Information abo
1b2a3 75 74 20 74 68 65 20 72 65 63 6f 72 64 20 66 6f ut the record fo
1b2a4 72 6d 61 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b rmat */. int nK
1b2a5 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ey,
1b2a6 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 /* Size of the
1b2a7 62 69 6e 61 72 79 20 72 65 63 6f 72 64 20 2a 2f binary record */
1b2a8 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 . const void *p
1b2a9 4b 65 79 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 Key, /* The
1b2aa 20 62 69 6e 61 72 79 20 72 65 63 6f 72 64 20 2a binary record *
1b2ab 2f 0a 20 20 63 68 61 72 20 2a 70 53 70 61 63 65 /. char *pSpace
1b2ac 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e , /* Un
1b2ad 61 6c 69 67 6e 65 64 20 73 70 61 63 65 20 61 76 aligned space av
1b2ae 61 69 6c 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20 ailable to hold
1b2af 74 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 the object */.
1b2b0 69 6e 74 20 73 7a 53 70 61 63 65 20 20 20 20 20 int szSpace
1b2b1 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
1b2b2 66 20 70 53 70 61 63 65 5b 5d 20 69 6e 20 62 79 f pSpace[] in by
1b2b3 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 tes */.){. cons
1b2b4 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
1b2b5 2a 61 4b 65 79 20 3d 20 28 63 6f 6e 73 74 20 75 *aKey = (const u
1b2b6 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 nsigned char *)p
1b2b7 4b 65 79 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 Key;. UnpackedR
1b2b8 65 63 6f 72 64 20 2a 70 3b 20 20 2f 2a 20 54 68 ecord *p; /* Th
1b2b9 65 20 75 6e 70 61 63 6b 65 64 20 72 65 63 6f 72 e unpacked recor
1b2ba 64 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 72 d that we will r
1b2bb 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e eturn */. int n
1b2bc 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 2f Byte; /
1b2bd 2a 20 4d 65 6d 6f 72 79 20 73 70 61 63 65 20 6e * Memory space n
1b2be 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 70 2c eeded to hold p,
1b2bf 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 in bytes */. i
1b2c0 6e 74 20 64 3b 0a 20 20 75 33 32 20 69 64 78 3b nt d;. u32 idx;
1b2c1 0a 20 20 75 31 36 20 75 3b 20 20 20 20 20 20 20 . u16 u;
1b2c2 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e /* Unsign
1b2c3 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 ed loop counter
1b2c4 2a 2f 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 0a */. u32 szHdr;.
1b2c5 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 Mem *pMem;. i
1b2c6 6e 74 20 6e 4f 66 66 3b 20 20 20 20 20 20 20 20 nt nOff;
1b2c7 20 20 20 2f 2a 20 49 6e 63 72 65 61 73 65 20 70 /* Increase p
1b2c8 53 70 61 63 65 20 62 79 20 74 68 69 73 20 6d 75 Space by this mu
1b2c9 63 68 20 74 6f 20 38 2d 62 79 74 65 20 61 6c 69 ch to 8-byte ali
1b2ca 67 6e 20 69 74 20 2a 2f 0a 20 20 0a 20 20 2f 2a gn it */. . /*
1b2cb 0a 20 20 2a 2a 20 57 65 20 77 61 6e 74 20 74 6f . ** We want to
1b2cc 20 73 68 69 66 74 20 74 68 65 20 70 6f 69 6e 74 shift the point
1b2cd 65 72 20 70 53 70 61 63 65 20 75 70 20 73 75 63 er pSpace up suc
1b2ce 68 20 74 68 61 74 20 69 74 20 69 73 20 38 2d 62 h that it is 8-b
1b2cf 79 74 65 20 61 6c 69 67 6e 65 64 2e 0a 20 20 2a yte aligned.. *
1b2d0 2a 20 54 68 75 73 2c 20 77 65 20 6e 65 65 64 20 * Thus, we need
1b2d1 74 6f 20 63 61 6c 63 75 6c 61 74 65 20 61 20 76 to calculate a v
1b2d2 61 6c 75 65 2c 20 6e 4f 66 66 2c 20 62 65 74 77 alue, nOff, betw
1b2d3 65 65 6e 20 30 20 61 6e 64 20 37 2c 20 74 6f 20 een 0 and 7, to
1b2d4 73 68 69 66 74 20 0a 20 20 2a 2a 20 69 74 20 62 shift . ** it b
1b2d5 79 2e 20 20 49 66 20 70 53 70 61 63 65 20 69 73 y. If pSpace is
1b2d6 20 61 6c 72 65 61 64 79 20 38 2d 62 79 74 65 20 already 8-byte
1b2d7 61 6c 69 67 6e 65 64 2c 20 6e 4f 66 66 20 73 68 aligned, nOff sh
1b2d8 6f 75 6c 64 20 62 65 20 7a 65 72 6f 2e 0a 20 20 ould be zero..
1b2d9 2a 2f 0a 20 20 6e 4f 66 66 20 3d 20 28 38 20 2d */. nOff = (8 -
1b2da 20 28 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f (SQLITE_PTR_TO_
1b2db 49 4e 54 28 70 53 70 61 63 65 29 20 26 20 37 29 INT(pSpace) & 7)
1b2dc 29 20 26 20 37 3b 0a 20 20 70 53 70 61 63 65 20 ) & 7;. pSpace
1b2dd 2b 3d 20 6e 4f 66 66 3b 0a 20 20 73 7a 53 70 61 += nOff;. szSpa
1b2de 63 65 20 2d 3d 20 6e 4f 66 66 3b 0a 20 20 6e 42 ce -= nOff;. nB
1b2df 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 73 69 7a yte = ROUND8(siz
1b2e0 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63 6f eof(UnpackedReco
1b2e1 72 64 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d 65 rd)) + sizeof(Me
1b2e2 6d 29 2a 28 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 m)*(pKeyInfo->nF
1b2e3 69 65 6c 64 2b 31 29 3b 0a 20 20 69 66 28 20 6e ield+1);. if( n
1b2e4 42 79 74 65 3e 73 7a 53 70 61 63 65 20 29 7b 0a Byte>szSpace ){.
1b2e5 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 44 p = sqlite3D
1b2e6 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4b 65 79 49 bMallocRaw(pKeyI
1b2e7 6e 66 6f 2d 3e 64 62 2c 20 6e 42 79 74 65 29 3b nfo->db, nByte);
1b2e8 0a 20 20 20 20 69 66 28 20 70 3d 3d 30 20 29 20 . if( p==0 )
1b2e9 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 70 2d return 0;. p-
1b2ea 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 >flags = UNPACKE
1b2eb 44 5f 4e 45 45 44 5f 46 52 45 45 20 7c 20 55 4e D_NEED_FREE | UN
1b2ec 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 PACKED_NEED_DEST
1b2ed 52 4f 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ROY;. }else{.
1b2ee 20 20 70 20 3d 20 28 55 6e 70 61 63 6b 65 64 52 p = (UnpackedR
1b2ef 65 63 6f 72 64 2a 29 70 53 70 61 63 65 3b 0a 20 ecord*)pSpace;.
1b2f0 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 55 4e p->flags = UN
1b2f1 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 PACKED_NEED_DEST
1b2f2 52 4f 59 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4b ROY;. }. p->pK
1b2f3 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 eyInfo = pKeyInf
1b2f4 6f 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d o;. p->nField =
1b2f5 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c pKeyInfo->nFiel
1b2f6 64 20 2b 20 31 3b 0a 20 20 70 2d 3e 61 4d 65 6d d + 1;. p->aMem
1b2f7 20 3d 20 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29 = pMem = (Mem*)
1b2f8 26 28 28 63 68 61 72 2a 29 70 29 5b 52 4f 55 4e &((char*)p)[ROUN
1b2f9 44 38 28 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b D8(sizeof(Unpack
1b2fa 65 64 52 65 63 6f 72 64 29 29 5d 3b 0a 20 20 61 edRecord))];. a
1b2fb 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 ssert( EIGHT_BYT
1b2fc 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d E_ALIGNMENT(pMem
1b2fd 29 20 29 3b 0a 20 20 69 64 78 20 3d 20 67 65 74 ) );. idx = get
1b2fe 56 61 72 69 6e 74 33 32 28 61 4b 65 79 2c 20 73 Varint32(aKey, s
1b2ff 7a 48 64 72 29 3b 0a 20 20 64 20 3d 20 73 7a 48 zHdr);. d = szH
1b300 64 72 3b 0a 20 20 75 20 3d 20 30 3b 0a 20 20 77 dr;. u = 0;. w
1b301 68 69 6c 65 28 20 69 64 78 3c 73 7a 48 64 72 20 hile( idx<szHdr
1b302 26 26 20 75 3c 70 2d 3e 6e 46 69 65 6c 64 20 26 && u<p->nField &
1b303 26 20 64 3c 3d 6e 4b 65 79 20 29 7b 0a 20 20 20 & d<=nKey ){.
1b304 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 u32 serial_type
1b305 3b 0a 0a 20 20 20 20 69 64 78 20 2b 3d 20 67 65 ;.. idx += ge
1b306 74 56 61 72 69 6e 74 33 32 28 26 61 4b 65 79 5b tVarint32(&aKey[
1b307 69 64 78 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 idx], serial_typ
1b308 65 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e e);. pMem->en
1b309 63 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e c = pKeyInfo->en
1b30a 63 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 64 62 20 c;. pMem->db
1b30b 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a = pKeyInfo->db;.
1b30c 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
1b30d 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a = 0;. pMem->z
1b30e 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 Malloc = 0;.
1b30f 64 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 d += sqlite3Vdbe
1b310 53 65 72 69 61 6c 47 65 74 28 26 61 4b 65 79 5b SerialGet(&aKey[
1b311 64 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 2c d], serial_type,
1b312 20 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d pMem);. pMem
1b313 2b 2b 3b 0a 20 20 20 20 75 2b 2b 3b 0a 20 20 7d ++;. u++;. }
1b314 0a 20 20 61 73 73 65 72 74 28 20 75 3c 3d 70 4b . assert( u<=pK
1b315 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b eyInfo->nField +
1b316 20 31 20 29 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c 1 );. p->nFiel
1b317 64 20 3d 20 75 3b 0a 20 20 72 65 74 75 72 6e 20 d = u;. return
1b318 28 76 6f 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a (void*)p;.}../*.
1b319 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1b31a 64 65 73 74 72 6f 79 73 20 61 20 55 6e 70 61 63 destroys a Unpac
1b31b 6b 65 64 52 65 63 6f 72 64 20 6f 62 6a 65 63 74 kedRecord object
1b31c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1b31d 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1b31e 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b VdbeDeleteUnpack
1b31f 65 64 52 65 63 6f 72 64 28 55 6e 70 61 63 6b 65 edRecord(Unpacke
1b320 64 52 65 63 6f 72 64 20 2a 70 29 7b 0a 20 20 69 dRecord *p){. i
1b321 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 nt i;. Mem *pMe
1b322 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 m;.. assert( p!
1b323 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1b324 70 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 p->flags & UNPAC
1b325 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 KED_NEED_DESTROY
1b326 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 );. for(i=0, p
1b327 4d 65 6d 3d 70 2d 3e 61 4d 65 6d 3b 20 69 3c 70 Mem=p->aMem; i<p
1b328 2d 3e 6e 46 69 65 6c 64 3b 20 69 2b 2b 2c 20 70 ->nField; i++, p
1b329 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 54 Mem++){. /* T
1b32a 68 65 20 75 6e 70 61 63 6b 65 64 20 72 65 63 6f he unpacked reco
1b32b 72 64 20 69 73 20 61 6c 77 61 79 73 20 63 6f 6e rd is always con
1b32c 73 74 72 75 63 74 65 64 20 62 79 20 74 68 65 0a structed by the.
1b32d 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 ** sqlite3Vd
1b32e 62 65 55 6e 70 61 63 6b 52 65 63 6f 72 64 28 29 beUnpackRecord()
1b32f 20 66 75 6e 63 74 69 6f 6e 20 61 62 6f 76 65 2c function above,
1b330 20 77 68 69 63 68 20 6d 61 6b 65 73 20 61 6c 6c which makes all
1b331 0a 20 20 20 20 2a 2a 20 73 74 72 69 6e 67 73 20 . ** strings
1b332 61 6e 64 20 62 6c 6f 62 73 20 73 74 61 74 69 63 and blobs static
1b333 2e 20 20 41 6e 64 20 6e 6f 6e 65 20 6f 66 20 74 . And none of t
1b334 68 65 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 0a he elements are.
1b335 20 20 20 20 2a 2a 20 65 76 65 72 20 74 72 61 6e ** ever tran
1b336 73 66 6f 72 6d 65 64 2c 20 73 6f 20 74 68 65 72 sformed, so ther
1b337 65 20 69 73 20 6e 65 76 65 72 20 61 6e 79 74 68 e is never anyth
1b338 69 6e 67 20 74 6f 20 64 65 6c 65 74 65 2e 0a 20 ing to delete..
1b339 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 4e 45 */. if( NE
1b33a 56 45 52 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f VER(pMem->zMallo
1b33b 63 29 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 c) ) sqlite3Vdbe
1b33c 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 MemRelease(pMem)
1b33d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 66 ;. }. if( p->f
1b33e 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f lags & UNPACKED_
1b33f 4e 45 45 44 5f 46 52 45 45 20 29 7b 0a 20 20 20 NEED_FREE ){.
1b340 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 sqlite3DbFree(p
1b341 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 ->pKeyInfo->db,
1b342 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a p);. }.}../*.**
1b343 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 This function c
1b344 6f 6d 70 61 72 65 73 20 74 68 65 20 74 77 6f 20 ompares the two
1b345 74 61 62 6c 65 20 72 6f 77 73 20 6f 72 20 69 6e table rows or in
1b346 64 65 78 20 72 65 63 6f 72 64 73 0a 2a 2a 20 73 dex records.** s
1b347 70 65 63 69 66 69 65 64 20 62 79 20 7b 6e 4b 65 pecified by {nKe
1b348 79 31 2c 20 70 4b 65 79 31 7d 20 61 6e 64 20 70 y1, pKey1} and p
1b349 50 4b 65 79 32 2e 20 20 49 74 20 72 65 74 75 72 PKey2. It retur
1b34a 6e 73 20 61 20 6e 65 67 61 74 69 76 65 2c 20 7a ns a negative, z
1b34b 65 72 6f 0a 2a 2a 20 6f 72 20 70 6f 73 69 74 69 ero.** or positi
1b34c 76 65 20 69 6e 74 65 67 65 72 20 69 66 20 6b 65 ve integer if ke
1b34d 79 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c y1 is less than,
1b34e 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 0a 2a 2a equal to or .**
1b34f 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6b 65 greater than ke
1b350 79 32 2e 20 20 54 68 65 20 7b 6e 4b 65 79 31 2c y2. The {nKey1,
1b351 20 70 4b 65 79 31 7d 20 6b 65 79 20 6d 75 73 74 pKey1} key must
1b352 20 62 65 20 61 20 62 6c 6f 62 0a 2a 2a 20 63 72 be a blob.** cr
1b353 65 61 74 65 64 20 62 79 20 74 68 20 4f 50 5f 4d eated by th OP_M
1b354 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 akeRecord opcode
1b355 20 6f 66 20 74 68 65 20 56 44 42 45 2e 20 20 54 of the VDBE. T
1b356 68 65 20 70 50 4b 65 79 32 0a 2a 2a 20 6b 65 79 he pPKey2.** key
1b357 20 6d 75 73 74 20 62 65 20 61 20 70 61 72 73 65 must be a parse
1b358 64 20 6b 65 79 20 73 75 63 68 20 61 73 20 6f 62 d key such as ob
1b359 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 tained from.** s
1b35a 71 6c 69 74 65 33 56 64 62 65 50 61 72 73 65 52 qlite3VdbeParseR
1b35b 65 63 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 4b 65 79 ecord..**.** Key
1b35c 31 20 61 6e 64 20 4b 65 79 32 20 64 6f 20 6e 6f 1 and Key2 do no
1b35d 74 20 68 61 76 65 20 74 6f 20 63 6f 6e 74 61 69 t have to contai
1b35e 6e 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 n the same numbe
1b35f 72 20 6f 66 20 66 69 65 6c 64 73 2e 0a 2a 2a 20 r of fields..**
1b360 54 68 65 20 6b 65 79 20 77 69 74 68 20 66 65 77 The key with few
1b361 65 72 20 66 69 65 6c 64 73 20 69 73 20 75 73 75 er fields is usu
1b362 61 6c 6c 79 20 63 6f 6d 70 61 72 65 73 20 6c 65 ally compares le
1b363 73 73 20 74 68 61 6e 20 74 68 65 20 0a 2a 2a 20 ss than the .**
1b364 6c 6f 6e 67 65 72 20 6b 65 79 2e 20 20 48 6f 77 longer key. How
1b365 65 76 65 72 20 69 66 20 74 68 65 20 55 4e 50 41 ever if the UNPA
1b366 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 66 6c 61 CKED_INCRKEY fla
1b367 67 73 20 69 6e 20 70 50 4b 65 79 32 20 69 73 20 gs in pPKey2 is
1b368 73 65 74 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 set.** and the c
1b369 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 65 73 20 61 ommon prefixes a
1b36a 72 65 20 65 71 75 61 6c 2c 20 74 68 65 6e 20 6b re equal, then k
1b36b 65 79 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e ey1 is less than
1b36c 20 6b 65 79 32 2e 0a 2a 2a 20 4f 72 20 69 66 20 key2..** Or if
1b36d 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 4d 41 54 the UNPACKED_MAT
1b36e 43 48 5f 50 52 45 46 49 58 20 66 6c 61 67 20 69 CH_PREFIX flag i
1b36f 73 20 73 65 74 20 61 6e 64 20 74 68 65 20 70 72 s set and the pr
1b370 65 66 69 78 65 73 20 61 72 65 0a 2a 2a 20 65 71 efixes are.** eq
1b371 75 61 6c 2c 20 74 68 65 6e 20 74 68 65 20 6b 65 ual, then the ke
1b372 79 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 ys are considere
1b373 64 20 74 6f 20 62 65 20 65 71 75 61 6c 20 61 6e d to be equal an
1b374 64 0a 2a 2a 20 74 68 65 20 70 61 72 74 73 20 62 d.** the parts b
1b375 65 79 6f 6e 64 20 74 68 65 20 63 6f 6d 6d 6f 6e eyond the common
1b376 20 70 72 65 66 69 78 20 61 72 65 20 69 67 6e 6f prefix are igno
1b377 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 red..**.** If th
1b378 65 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 e UNPACKED_IGNOR
1b379 45 5f 52 4f 57 49 44 20 66 6c 61 67 20 69 73 20 E_ROWID flag is
1b37a 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 6c 61 set, then the la
1b37b 73 74 20 62 79 74 65 20 6f 66 0a 2a 2a 20 74 68 st byte of.** th
1b37c 65 20 68 65 61 64 65 72 20 6f 66 20 70 4b 65 79 e header of pKey
1b37d 31 20 69 73 20 69 67 6e 6f 72 65 64 2e 20 20 49 1 is ignored. I
1b37e 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 t is assumed tha
1b37f 74 20 70 4b 65 79 31 20 69 73 0a 2a 2a 20 61 6e t pKey1 is.** an
1b380 20 69 6e 64 65 78 20 6b 65 79 2c 20 61 6e 64 20 index key, and
1b381 74 68 75 73 20 65 6e 64 73 20 77 69 74 68 20 61 thus ends with a
1b382 20 72 6f 77 69 64 20 76 61 6c 75 65 2e 20 20 54 rowid value. T
1b383 68 65 20 6c 61 73 74 20 62 79 74 65 0a 2a 2a 20 he last byte.**
1b384 6f 66 20 74 68 65 20 68 65 61 64 65 72 20 77 69 of the header wi
1b385 6c 6c 20 74 68 65 72 65 66 6f 72 65 20 62 65 20 ll therefore be
1b386 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 the serial type
1b387 6f 66 20 74 68 65 20 72 6f 77 69 64 3a 0a 2a 2a of the rowid:.**
1b388 20 6f 6e 65 20 6f 66 20 31 2c 20 32 2c 20 33 2c one of 1, 2, 3,
1b389 20 34 2c 20 35 2c 20 36 2c 20 38 2c 20 6f 72 20 4, 5, 6, 8, or
1b38a 39 20 2d 20 74 68 65 20 69 6e 74 65 67 65 72 20 9 - the integer
1b38b 73 65 72 69 61 6c 20 74 79 70 65 73 2e 0a 2a 2a serial types..**
1b38c 20 54 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 The serial type
1b38d 20 6f 66 20 74 68 65 20 66 69 6e 61 6c 20 72 6f of the final ro
1b38e 77 69 64 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 wid will always
1b38f 62 65 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 be a single byte
1b390 2e 0a 2a 2a 20 42 79 20 69 67 6e 6f 72 69 6e 67 ..** By ignoring
1b391 20 74 68 69 73 20 6c 61 73 74 20 62 79 74 65 20 this last byte
1b392 6f 66 20 74 68 65 20 68 65 61 64 65 72 2c 20 77 of the header, w
1b393 65 20 66 6f 72 63 65 20 74 68 65 20 63 6f 6d 70 e force the comp
1b394 61 72 69 73 6f 6e 0a 2a 2a 20 74 6f 20 69 67 6e arison.** to ign
1b395 6f 72 65 20 74 68 65 20 72 6f 77 69 64 20 61 74 ore the rowid at
1b396 20 74 68 65 20 65 6e 64 20 6f 66 20 6b 65 79 31 the end of key1
1b397 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1b398 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
1b399 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 dbeRecordCompare
1b39a 28 0a 20 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63 (. int nKey1, c
1b39b 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 onst void *pKey1
1b39c 2c 20 2f 2a 20 4c 65 66 74 20 6b 65 79 20 2a 2f , /* Left key */
1b39d 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 . UnpackedRecor
1b39e 64 20 2a 70 50 4b 65 79 32 20 20 20 20 20 20 20 d *pPKey2
1b39f 20 2f 2a 20 52 69 67 68 74 20 6b 65 79 20 2a 2f /* Right key */
1b3a0 0a 29 7b 0a 20 20 69 6e 74 20 64 31 3b 20 20 20 .){. int d1;
1b3a1 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
1b3a2 65 74 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f et into aKey[] o
1b3a3 66 20 6e 65 78 74 20 64 61 74 61 20 65 6c 65 6d f next data elem
1b3a4 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20 69 64 78 ent */. u32 idx
1b3a5 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 1; /* O
1b3a6 66 66 73 65 74 20 69 6e 74 6f 20 61 4b 65 79 5b ffset into aKey[
1b3a7 5d 20 6f 66 20 6e 65 78 74 20 68 65 61 64 65 72 ] of next header
1b3a8 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 75 33 element */. u3
1b3a9 32 20 73 7a 48 64 72 31 3b 20 20 20 20 20 20 20 2 szHdr1;
1b3aa 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1b3ab 74 65 73 20 69 6e 20 68 65 61 64 65 72 20 2a 2f tes in header */
1b3ac 0a 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 . int i = 0;.
1b3ad 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20 69 6e int nField;. in
1b3ae 74 20 72 63 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 t rc = 0;. cons
1b3af 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
1b3b0 2a 61 4b 65 79 31 20 3d 20 28 63 6f 6e 73 74 20 *aKey1 = (const
1b3b1 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 unsigned char *)
1b3b2 70 4b 65 79 31 3b 0a 20 20 4b 65 79 49 6e 66 6f pKey1;. KeyInfo
1b3b3 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 4d 65 *pKeyInfo;. Me
1b3b4 6d 20 6d 65 6d 31 3b 0a 0a 20 20 70 4b 65 79 49 m mem1;.. pKeyI
1b3b5 6e 66 6f 20 3d 20 70 50 4b 65 79 32 2d 3e 70 4b nfo = pPKey2->pK
1b3b6 65 79 49 6e 66 6f 3b 0a 20 20 6d 65 6d 31 2e 65 eyInfo;. mem1.e
1b3b7 6e 63 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 nc = pKeyInfo->e
1b3b8 6e 63 3b 0a 20 20 6d 65 6d 31 2e 64 62 20 3d 20 nc;. mem1.db =
1b3b9 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 pKeyInfo->db;.
1b3ba 6d 65 6d 31 2e 66 6c 61 67 73 20 3d 20 30 3b 0a mem1.flags = 0;.
1b3bb 20 20 6d 65 6d 31 2e 75 2e 69 20 3d 20 30 3b 20 mem1.u.i = 0;
1b3bc 20 2f 2a 20 6e 6f 74 20 6e 65 65 64 65 64 2c 20 /* not needed,
1b3bd 68 65 72 65 20 74 6f 20 73 69 6c 65 6e 63 65 20 here to silence
1b3be 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 compiler warning
1b3bf 20 2a 2f 0a 20 20 6d 65 6d 31 2e 7a 4d 61 6c 6c */. mem1.zMall
1b3c0 6f 63 20 3d 20 30 3b 0a 20 20 0a 20 20 69 64 78 oc = 0;. . idx
1b3c1 31 20 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 1 = getVarint32(
1b3c2 61 4b 65 79 31 2c 20 73 7a 48 64 72 31 29 3b 0a aKey1, szHdr1);.
1b3c3 20 20 64 31 20 3d 20 73 7a 48 64 72 31 3b 0a 20 d1 = szHdr1;.
1b3c4 20 69 66 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 if( pPKey2->fla
1b3c5 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 49 47 gs & UNPACKED_IG
1b3c6 4e 4f 52 45 5f 52 4f 57 49 44 20 29 7b 0a 20 20 NORE_ROWID ){.
1b3c7 20 20 73 7a 48 64 72 31 2d 2d 3b 0a 20 20 7d 0a szHdr1--;. }.
1b3c8 20 20 6e 46 69 65 6c 64 20 3d 20 70 4b 65 79 49 nField = pKeyI
1b3c9 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 77 nfo->nField;. w
1b3ca 68 69 6c 65 28 20 69 64 78 31 3c 73 7a 48 64 72 hile( idx1<szHdr
1b3cb 31 20 26 26 20 69 3c 70 50 4b 65 79 32 2d 3e 6e 1 && i<pPKey2->n
1b3cc 46 69 65 6c 64 20 29 7b 0a 20 20 20 20 75 33 32 Field ){. u32
1b3cd 20 73 65 72 69 61 6c 5f 74 79 70 65 31 3b 0a 0a serial_type1;..
1b3ce 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 /* Read the
1b3cf 73 65 72 69 61 6c 20 74 79 70 65 73 20 66 6f 72 serial types for
1b3d0 20 74 68 65 20 6e 65 78 74 20 65 6c 65 6d 65 6e the next elemen
1b3d1 74 20 69 6e 20 65 61 63 68 20 6b 65 79 2e 20 2a t in each key. *
1b3d2 2f 0a 20 20 20 20 69 64 78 31 20 2b 3d 20 67 65 /. idx1 += ge
1b3d3 74 56 61 72 69 6e 74 33 32 28 20 61 4b 65 79 31 tVarint32( aKey1
1b3d4 2b 69 64 78 31 2c 20 73 65 72 69 61 6c 5f 74 79 +idx1, serial_ty
1b3d5 70 65 31 20 29 3b 0a 20 20 20 20 69 66 28 20 64 pe1 );. if( d
1b3d6 31 3e 3d 6e 4b 65 79 31 20 26 26 20 73 71 6c 69 1>=nKey1 && sqli
1b3d7 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
1b3d8 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 eLen(serial_type
1b3d9 31 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a 0a 20 1)>0 ) break;..
1b3da 20 20 20 2f 2a 20 45 78 74 72 61 63 74 20 74 68 /* Extract th
1b3db 65 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 63 e values to be c
1b3dc 6f 6d 70 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a ompared.. */.
1b3dd 20 20 20 20 64 31 20 2b 3d 20 73 71 6c 69 74 65 d1 += sqlite
1b3de 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 26 3VdbeSerialGet(&
1b3df 61 4b 65 79 31 5b 64 31 5d 2c 20 73 65 72 69 61 aKey1[d1], seria
1b3e0 6c 5f 74 79 70 65 31 2c 20 26 6d 65 6d 31 29 3b l_type1, &mem1);
1b3e1 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 .. /* Do the
1b3e2 63 6f 6d 70 61 72 69 73 6f 6e 0a 20 20 20 20 2a comparison. *
1b3e3 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
1b3e4 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 26 6d 65 e3MemCompare(&me
1b3e5 6d 31 2c 20 26 70 50 4b 65 79 32 2d 3e 61 4d 65 m1, &pPKey2->aMe
1b3e6 6d 5b 69 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 m[i],.
1b3e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b3e8 20 69 3c 6e 46 69 65 6c 64 20 3f 20 70 4b 65 79 i<nField ? pKey
1b3e9 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3a Info->aColl[i] :
1b3ea 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 0);. if( rc!
1b3eb 3d 30 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61 =0 ){. brea
1b3ec 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 2b 2b k;. }. i++
1b3ed 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 20 6d ;. }.. /* No m
1b3ee 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
1b3ef 20 69 73 20 65 76 65 72 20 75 73 65 64 20 6f 6e is ever used on
1b3f0 20 6d 65 6d 31 2e 20 2a 2f 0a 20 20 69 66 28 20 mem1. */. if(
1b3f1 4e 45 56 45 52 28 6d 65 6d 31 2e 7a 4d 61 6c 6c NEVER(mem1.zMall
1b3f2 6f 63 29 20 29 20 73 71 6c 69 74 65 33 56 64 62 oc) ) sqlite3Vdb
1b3f3 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 65 6d eMemRelease(&mem
1b3f4 31 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 1);.. /* If the
1b3f5 20 50 52 45 46 49 58 5f 53 45 41 52 43 48 20 66 PREFIX_SEARCH f
1b3f6 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64 20 61 lag is set and a
1b3f7 6c 6c 20 66 69 65 6c 64 73 20 65 78 63 65 70 74 ll fields except
1b3f8 20 74 68 65 20 66 69 6e 61 6c 0a 20 20 2a 2a 20 the final. **
1b3f9 72 6f 77 69 64 20 66 69 65 6c 64 20 77 65 72 65 rowid field were
1b3fa 20 65 71 75 61 6c 2c 20 74 68 65 6e 20 63 6c 65 equal, then cle
1b3fb 61 72 20 74 68 65 20 50 52 45 46 49 58 5f 53 45 ar the PREFIX_SE
1b3fc 41 52 43 48 20 66 6c 61 67 20 61 6e 64 20 73 65 ARCH flag and se
1b3fd 74 20 0a 20 20 2a 2a 20 70 50 4b 65 79 32 2d 3e t . ** pPKey2->
1b3fe 72 6f 77 69 64 20 74 6f 20 74 68 65 20 76 61 6c rowid to the val
1b3ff 75 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 ue of the rowid
1b400 66 69 65 6c 64 20 69 6e 20 28 70 4b 65 79 31 2c field in (pKey1,
1b401 20 6e 4b 65 79 31 29 2e 0a 20 20 2a 2a 20 54 68 nKey1).. ** Th
1b402 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74 68 is is used by th
1b403 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 6f 70 e OP_IsUnique op
1b404 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 code.. */. if(
1b405 20 28 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 (pPKey2->flags
1b406 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 & UNPACKED_PREFI
1b407 58 5f 53 45 41 52 43 48 29 20 26 26 20 69 3d 3d X_SEARCH) && i==
1b408 28 70 50 4b 65 79 32 2d 3e 6e 46 69 65 6c 64 2d (pPKey2->nField-
1b409 31 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 1) ){. assert
1b40a 28 20 69 64 78 31 3d 3d 73 7a 48 64 72 31 20 26 ( idx1==szHdr1 &
1b40b 26 20 72 63 20 29 3b 0a 20 20 20 20 61 73 73 65 & rc );. asse
1b40c 72 74 28 20 6d 65 6d 31 2e 66 6c 61 67 73 20 26 rt( mem1.flags &
1b40d 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 MEM_Int );.
1b40e 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 3d pPKey2->flags &=
1b40f 20 7e 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 ~UNPACKED_PREFI
1b410 58 5f 53 45 41 52 43 48 3b 0a 20 20 20 20 70 50 X_SEARCH;. pP
1b411 4b 65 79 32 2d 3e 72 6f 77 69 64 20 3d 20 6d 65 Key2->rowid = me
1b412 6d 31 2e 75 2e 69 3b 0a 20 20 7d 0a 0a 20 20 69 m1.u.i;. }.. i
1b413 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 f( rc==0 ){.
1b414 2f 2a 20 72 63 3d 3d 30 20 68 65 72 65 20 6d 65 /* rc==0 here me
1b415 61 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 ans that one of
1b416 74 68 65 20 6b 65 79 73 20 72 61 6e 20 6f 75 74 the keys ran out
1b417 20 6f 66 20 66 69 65 6c 64 73 20 61 6e 64 0a 20 of fields and.
1b418 20 20 20 2a 2a 20 61 6c 6c 20 74 68 65 20 66 69 ** all the fi
1b419 65 6c 64 73 20 75 70 20 74 6f 20 74 68 61 74 20 elds up to that
1b41a 70 6f 69 6e 74 20 77 65 72 65 20 65 71 75 61 6c point were equal
1b41b 2e 20 49 66 20 74 68 65 20 55 4e 50 41 43 4b 45 . If the UNPACKE
1b41c 44 5f 49 4e 43 52 4b 45 59 0a 20 20 20 20 2a 2a D_INCRKEY. **
1b41d 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 flag is set, th
1b41e 65 6e 20 62 72 65 61 6b 20 74 68 65 20 74 69 65 en break the tie
1b41f 20 62 79 20 74 72 65 61 74 69 6e 67 20 6b 65 79 by treating key
1b420 32 20 61 73 20 6c 61 72 67 65 72 2e 0a 20 20 20 2 as larger..
1b421 20 2a 2a 20 49 66 20 74 68 65 20 55 50 41 43 4b ** If the UPACK
1b422 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20 ED_PREFIX_MATCH
1b423 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 flag is set, the
1b424 6e 20 6b 65 79 73 20 77 69 74 68 20 63 6f 6d 6d n keys with comm
1b425 6f 6e 20 70 72 65 66 69 78 65 73 0a 20 20 20 20 on prefixes.
1b426 2a 2a 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 ** are considere
1b427 64 20 74 6f 20 62 65 20 65 71 75 61 6c 2e 20 20 d to be equal.
1b428 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 6c Otherwise, the l
1b429 6f 6e 67 65 72 20 6b 65 79 20 69 73 20 74 68 65 onger key is the
1b42a 20 0a 20 20 20 20 2a 2a 20 6c 61 72 67 65 72 2e . ** larger.
1b42b 20 20 41 73 20 69 74 20 68 61 70 70 65 6e 73 2c As it happens,
1b42c 20 74 68 65 20 70 50 4b 65 79 32 20 77 69 6c 6c the pPKey2 will
1b42d 20 61 6c 77 61 79 73 20 62 65 20 74 68 65 20 6c always be the l
1b42e 6f 6e 67 65 72 0a 20 20 20 20 2a 2a 20 69 66 20 onger. ** if
1b42f 74 68 65 72 65 20 69 73 20 61 20 64 69 66 66 65 there is a diffe
1b430 72 65 6e 63 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 rence.. */.
1b431 20 20 69 66 28 20 70 50 4b 65 79 32 2d 3e 66 6c if( pPKey2->fl
1b432 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 49 ags & UNPACKED_I
1b433 4e 43 52 4b 45 59 20 29 7b 0a 20 20 20 20 20 20 NCRKEY ){.
1b434 72 63 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 65 6c rc = -1;. }el
1b435 73 65 20 69 66 28 20 70 50 4b 65 79 32 2d 3e 66 se if( pPKey2->f
1b436 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f lags & UNPACKED_
1b437 50 52 45 46 49 58 5f 4d 41 54 43 48 20 29 7b 0a PREFIX_MATCH ){.
1b438 20 20 20 20 20 20 2f 2a 20 4c 65 61 76 65 20 72 /* Leave r
1b439 63 3d 3d 30 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 c==0 */. }els
1b43a 65 20 69 66 28 20 69 64 78 31 3c 73 7a 48 64 72 e if( idx1<szHdr
1b43b 31 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 1 ){. rc =
1b43c 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 1;. }. }else
1b43d 20 69 66 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 if( pKeyInfo->a
1b43e 53 6f 72 74 4f 72 64 65 72 20 26 26 20 69 3c 70 SortOrder && i<p
1b43f 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 0a KeyInfo->nField.
1b440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 &
1b441 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 & pKeyInfo->aSor
1b442 74 4f 72 64 65 72 5b 69 5d 20 29 7b 0a 20 20 20 tOrder[i] ){.
1b443 20 72 63 20 3d 20 2d 72 63 3b 0a 20 20 7d 0a 0a rc = -rc;. }..
1b444 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 20 return rc;.}.
1b445 0a 0a 2f 2a 0a 2a 2a 20 70 43 75 72 20 70 6f 69 ../*.** pCur poi
1b446 6e 74 73 20 61 74 20 61 6e 20 69 6e 64 65 78 20 nts at an index
1b447 65 6e 74 72 79 20 63 72 65 61 74 65 64 20 75 73 entry created us
1b448 69 6e 67 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52 ing the OP_MakeR
1b449 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 0a 2a 2a ecord opcode..**
1b44a 20 52 65 61 64 20 74 68 65 20 72 6f 77 69 64 20 Read the rowid
1b44b 28 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 (the last field
1b44c 69 6e 20 74 68 65 20 72 65 63 6f 72 64 29 20 61 in the record) a
1b44d 6e 64 20 73 74 6f 72 65 20 69 74 20 69 6e 20 2a nd store it in *
1b44e 72 6f 77 69 64 2e 0a 2a 2a 20 52 65 74 75 72 6e rowid..** Return
1b44f 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 SQLITE_OK if ev
1b450 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 73 2c 20 erything works,
1b451 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 or an error code
1b452 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a otherwise..**.*
1b453 2a 20 70 43 75 72 20 6d 69 67 68 74 20 62 65 20 * pCur might be
1b454 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 65 78 74 pointing to text
1b455 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 obtained from a
1b456 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 corrupt databas
1b457 65 20 66 69 6c 65 2e 0a 2a 2a 20 53 6f 20 74 68 e file..** So th
1b458 65 20 63 6f 6e 74 65 6e 74 20 63 61 6e 6e 6f 74 e content cannot
1b459 20 62 65 20 74 72 75 73 74 65 64 2e 20 20 44 6f be trusted. Do
1b45a 20 61 70 70 72 6f 70 72 69 61 74 65 20 63 68 65 appropriate che
1b45b 63 6b 73 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65 cks on the conte
1b45c 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 nt..*/.SQLITE_PR
1b45d 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1b45e 33 56 64 62 65 49 64 78 52 6f 77 69 64 28 73 71 3VdbeIdxRowid(sq
1b45f 6c 69 74 65 33 20 2a 64 62 2c 20 42 74 43 75 72 lite3 *db, BtCur
1b460 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a sor *pCur, i64 *
1b461 72 6f 77 69 64 29 7b 0a 20 20 69 36 34 20 6e 43 rowid){. i64 nC
1b462 65 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 20 69 6e ellKey = 0;. in
1b463 74 20 72 63 3b 0a 20 20 75 33 32 20 73 7a 48 64 t rc;. u32 szHd
1b464 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a r; /* Siz
1b465 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 20 e of the header
1b466 2a 2f 0a 20 20 75 33 32 20 74 79 70 65 52 6f 77 */. u32 typeRow
1b467 69 64 3b 20 20 20 20 2f 2a 20 53 65 72 69 61 6c id; /* Serial
1b468 20 74 79 70 65 20 6f 66 20 74 68 65 20 72 6f 77 type of the row
1b469 69 64 20 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 52 id */. u32 lenR
1b46a 6f 77 69 64 3b 20 20 20 20 20 2f 2a 20 53 69 7a owid; /* Siz
1b46b 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 2a e of the rowid *
1b46c 2f 0a 20 20 4d 65 6d 20 6d 2c 20 76 3b 0a 0a 20 /. Mem m, v;..
1b46d 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
1b46e 52 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 R(db);.. /* Get
1b46f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
1b470 20 69 6e 64 65 78 20 65 6e 74 72 79 2e 20 20 4f index entry. O
1b471 6e 6c 79 20 69 6e 64 69 63 65 73 20 65 6e 74 72 nly indices entr
1b472 69 65 73 20 6f 66 20 6c 65 73 73 0a 20 20 2a 2a ies of less. **
1b473 20 74 68 61 6e 20 32 47 69 42 20 61 72 65 20 73 than 2GiB are s
1b474 75 70 70 6f 72 74 20 2d 20 61 6e 79 74 68 69 6e upport - anythin
1b475 67 20 6c 61 72 67 65 20 6d 75 73 74 20 62 65 20 g large must be
1b476 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 database corrupt
1b477 69 6f 6e 2e 0a 20 20 2a 2a 20 41 6e 79 20 63 6f ion.. ** Any co
1b478 72 72 75 70 74 69 6f 6e 20 69 73 20 64 65 74 65 rruption is dete
1b479 63 74 65 64 20 69 6e 20 73 71 6c 69 74 65 33 42 cted in sqlite3B
1b47a 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 treeParseCellPtr
1b47b 28 29 2c 20 74 68 6f 75 67 68 2c 20 73 6f 0a 20 (), though, so.
1b47c 20 2a 2a 20 74 68 69 73 20 63 6f 64 65 20 63 61 ** this code ca
1b47d 6e 20 73 61 66 65 6c 79 20 61 73 73 75 6d 65 20 n safely assume
1b47e 74 68 61 74 20 6e 43 65 6c 6c 4b 65 79 20 69 73 that nCellKey is
1b47f 20 33 32 2d 62 69 74 73 20 20 0a 20 20 2a 2f 0a 32-bits . */.
1b480 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1b481 33 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 3BtreeCursorIsVa
1b482 6c 69 64 28 70 43 75 72 29 20 29 3b 0a 20 20 72 lid(pCur) );. r
1b483 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1b484 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 6e KeySize(pCur, &n
1b485 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 61 73 73 65 CellKey);. asse
1b486 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rt( rc==SQLITE_O
1b487 4b 20 29 3b 20 20 20 20 20 2f 2a 20 70 43 75 72 K ); /* pCur
1b488 20 69 73 20 61 6c 77 61 79 73 20 76 61 6c 69 64 is always valid
1b489 20 73 6f 20 4b 65 79 53 69 7a 65 20 63 61 6e 6e so KeySize cann
1b48a 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 61 73 73 ot fail */. ass
1b48b 65 72 74 28 20 28 6e 43 65 6c 6c 4b 65 79 20 26 ert( (nCellKey &
1b48c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29 SQLITE_MAX_U32)
1b48d 3d 3d 28 75 36 34 29 6e 43 65 6c 6c 4b 65 79 20 ==(u64)nCellKey
1b48e 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 69 6e );.. /* Read in
1b48f 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 63 6f the complete co
1b490 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 69 6e 64 ntent of the ind
1b491 65 78 20 65 6e 74 72 79 20 2a 2f 0a 20 20 6d 65 ex entry */. me
1b492 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65 mset(&m, 0, size
1b493 6f 66 28 6d 29 29 3b 0a 20 20 72 63 20 3d 20 73 of(m));. rc = s
1b494 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f qlite3VdbeMemFro
1b495 6d 42 74 72 65 65 28 70 43 75 72 2c 20 30 2c 20 mBtree(pCur, 0,
1b496 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 79 2c 20 31 (int)nCellKey, 1
1b497 2c 20 26 6d 29 3b 0a 20 20 69 66 28 20 72 63 20 , &m);. if( rc
1b498 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
1b499 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 ;. }.. /* The
1b49a 69 6e 64 65 78 20 65 6e 74 72 79 20 6d 75 73 74 index entry must
1b49b 20 62 65 67 69 6e 20 77 69 74 68 20 61 20 68 65 begin with a he
1b49c 61 64 65 72 20 73 69 7a 65 20 2a 2f 0a 20 20 28 ader size */. (
1b49d 76 6f 69 64 29 67 65 74 56 61 72 69 6e 74 33 32 void)getVarint32
1b49e 28 28 75 38 2a 29 6d 2e 7a 2c 20 73 7a 48 64 72 ((u8*)m.z, szHdr
1b49f 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 73 );. testcase( s
1b4a0 7a 48 64 72 3d 3d 33 20 29 3b 0a 20 20 74 65 73 zHdr==3 );. tes
1b4a1 74 63 61 73 65 28 20 73 7a 48 64 72 3d 3d 6d 2e tcase( szHdr==m.
1b4a2 6e 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6b n );. if( unlik
1b4a3 65 6c 79 28 73 7a 48 64 72 3c 33 20 7c 7c 20 28 ely(szHdr<3 || (
1b4a4 69 6e 74 29 73 7a 48 64 72 3e 6d 2e 6e 29 20 29 int)szHdr>m.n) )
1b4a5 7b 0a 20 20 20 20 67 6f 74 6f 20 69 64 78 5f 72 {. goto idx_r
1b4a6 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3b owid_corruption;
1b4a7 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6c . }.. /* The l
1b4a8 61 73 74 20 66 69 65 6c 64 20 6f 66 20 74 68 65 ast field of the
1b4a9 20 69 6e 64 65 78 20 73 68 6f 75 6c 64 20 62 65 index should be
1b4aa 20 61 6e 20 69 6e 74 65 67 65 72 20 2d 20 74 68 an integer - th
1b4ab 65 20 52 4f 57 49 44 2e 0a 20 20 2a 2a 20 56 65 e ROWID.. ** Ve
1b4ac 72 69 66 79 20 74 68 61 74 20 74 68 65 20 6c 61 rify that the la
1b4ad 73 74 20 65 6e 74 72 79 20 72 65 61 6c 6c 79 20 st entry really
1b4ae 69 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 2a is an integer. *
1b4af 2f 0a 20 20 28 76 6f 69 64 29 67 65 74 56 61 72 /. (void)getVar
1b4b0 69 6e 74 33 32 28 28 75 38 2a 29 26 6d 2e 7a 5b int32((u8*)&m.z[
1b4b1 73 7a 48 64 72 2d 31 5d 2c 20 74 79 70 65 52 6f szHdr-1], typeRo
1b4b2 77 69 64 29 3b 0a 20 20 74 65 73 74 63 61 73 65 wid);. testcase
1b4b3 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 31 20 29 ( typeRowid==1 )
1b4b4 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 ;. testcase( ty
1b4b5 70 65 52 6f 77 69 64 3d 3d 32 20 29 3b 0a 20 20 peRowid==2 );.
1b4b6 74 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f testcase( typeRo
1b4b7 77 69 64 3d 3d 33 20 29 3b 0a 20 20 74 65 73 74 wid==3 );. test
1b4b8 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d case( typeRowid=
1b4b9 3d 34 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 =4 );. testcase
1b4ba 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 35 20 29 ( typeRowid==5 )
1b4bb 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 ;. testcase( ty
1b4bc 70 65 52 6f 77 69 64 3d 3d 36 20 29 3b 0a 20 20 peRowid==6 );.
1b4bd 74 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f testcase( typeRo
1b4be 77 69 64 3d 3d 38 20 29 3b 0a 20 20 74 65 73 74 wid==8 );. test
1b4bf 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d case( typeRowid=
1b4c0 3d 39 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 =9 );. if( unli
1b4c1 6b 65 6c 79 28 74 79 70 65 52 6f 77 69 64 3c 31 kely(typeRowid<1
1b4c2 20 7c 7c 20 74 79 70 65 52 6f 77 69 64 3e 39 20 || typeRowid>9
1b4c3 7c 7c 20 74 79 70 65 52 6f 77 69 64 3d 3d 37 29 || typeRowid==7)
1b4c4 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 64 78 ){. goto idx
1b4c5 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 6f _rowid_corruptio
1b4c6 6e 3b 0a 20 20 7d 0a 20 20 6c 65 6e 52 6f 77 69 n;. }. lenRowi
1b4c7 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 d = sqlite3VdbeS
1b4c8 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 74 79 70 erialTypeLen(typ
1b4c9 65 52 6f 77 69 64 29 3b 0a 20 20 74 65 73 74 63 eRowid);. testc
1b4ca 61 73 65 28 20 28 75 33 32 29 6d 2e 6e 3d 3d 73 ase( (u32)m.n==s
1b4cb 7a 48 64 72 2b 6c 65 6e 52 6f 77 69 64 20 29 3b zHdr+lenRowid );
1b4cc 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 . if( unlikely(
1b4cd 28 75 33 32 29 6d 2e 6e 3c 73 7a 48 64 72 2b 6c (u32)m.n<szHdr+l
1b4ce 65 6e 52 6f 77 69 64 29 20 29 7b 0a 20 20 20 20 enRowid) ){.
1b4cf 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 goto idx_rowid_c
1b4d0 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a orruption;. }..
1b4d1 20 20 2f 2a 20 46 65 74 63 68 20 74 68 65 20 69 /* Fetch the i
1b4d2 6e 74 65 67 65 72 20 6f 66 66 20 74 68 65 20 65 nteger off the e
1b4d3 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 nd of the index
1b4d4 72 65 63 6f 72 64 20 2a 2f 0a 20 20 73 71 6c 69 record */. sqli
1b4d5 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 te3VdbeSerialGet
1b4d6 28 28 75 38 2a 29 26 6d 2e 7a 5b 6d 2e 6e 2d 6c ((u8*)&m.z[m.n-l
1b4d7 65 6e 52 6f 77 69 64 5d 2c 20 74 79 70 65 52 6f enRowid], typeRo
1b4d8 77 69 64 2c 20 26 76 29 3b 0a 20 20 2a 72 6f 77 wid, &v);. *row
1b4d9 69 64 20 3d 20 76 2e 75 2e 69 3b 0a 20 20 73 71 id = v.u.i;. sq
1b4da 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
1b4db 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 74 75 72 ase(&m);. retur
1b4dc 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 n SQLITE_OK;..
1b4dd 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20 /* Jump here if
1b4de 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 database corrupt
1b4df 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65 64 20 ion is detected
1b4e0 61 66 74 65 72 20 6d 20 68 61 73 20 62 65 65 6e after m has been
1b4e1 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2e . ** allocated.
1b4e2 20 20 46 72 65 65 20 74 68 65 20 6d 20 6f 62 6a Free the m obj
1b4e3 65 63 74 20 61 6e 64 20 72 65 74 75 72 6e 20 53 ect and return S
1b4e4 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e 20 2a QLITE_CORRUPT. *
1b4e5 2f 0a 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 /.idx_rowid_corr
1b4e6 75 70 74 69 6f 6e 3a 0a 20 20 74 65 73 74 63 61 uption:. testca
1b4e7 73 65 28 20 6d 2e 7a 4d 61 6c 6c 6f 63 21 3d 30 se( m.zMalloc!=0
1b4e8 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 );. sqlite3Vdb
1b4e9 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b eMemRelease(&m);
1b4ea 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1b4eb 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 7d _CORRUPT_BKPT;.}
1b4ec 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 ../*.** Compare
1b4ed 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 69 the key of the i
1b4ee 6e 64 65 78 20 65 6e 74 72 79 20 74 68 61 74 20 ndex entry that
1b4ef 63 75 72 73 6f 72 20 70 43 20 69 73 20 70 6f 69 cursor pC is poi
1b4f0 6e 74 69 6e 67 20 74 6f 20 61 67 61 69 6e 73 74 nting to against
1b4f1 0a 2a 2a 20 74 68 65 20 6b 65 79 20 73 74 72 69 .** the key stri
1b4f2 6e 67 20 69 6e 20 70 55 6e 70 61 63 6b 65 64 2e ng in pUnpacked.
1b4f3 20 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 52 Write into *pR
1b4f4 65 73 20 61 20 6e 75 6d 62 65 72 0a 2a 2a 20 74 es a number.** t
1b4f5 68 61 74 20 69 73 20 6e 65 67 61 74 69 76 65 2c hat is negative,
1b4f6 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 69 74 69 zero, or positi
1b4f7 76 65 20 69 66 20 70 43 20 69 73 20 6c 65 73 73 ve if pC is less
1b4f8 20 74 68 61 6e 2c 20 65 71 75 61 6c 20 74 6f 2c than, equal to,
1b4f9 0a 2a 2a 20 6f 72 20 67 72 65 61 74 65 72 20 74 .** or greater t
1b4fa 68 61 6e 20 70 55 6e 70 61 63 6b 65 64 2e 20 20 han pUnpacked.
1b4fb 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
1b4fc 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 0a on success..**.
1b4fd 2a 2a 20 70 55 6e 70 61 63 6b 65 64 20 69 73 20 ** pUnpacked is
1b4fe 65 69 74 68 65 72 20 63 72 65 61 74 65 64 20 77 either created w
1b4ff 69 74 68 6f 75 74 20 61 20 72 6f 77 69 64 20 6f ithout a rowid o
1b500 72 20 69 73 20 74 72 75 6e 63 61 74 65 64 20 73 r is truncated s
1b501 6f 20 74 68 61 74 20 69 74 0a 2a 2a 20 6f 6d 69 o that it.** omi
1b502 74 73 20 74 68 65 20 72 6f 77 69 64 20 61 74 20 ts the rowid at
1b503 74 68 65 20 65 6e 64 2e 20 20 54 68 65 20 72 6f the end. The ro
1b504 77 69 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f wid at the end o
1b505 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 f the index entr
1b506 79 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 65 64 20 y.** is ignored
1b507 61 73 20 77 65 6c 6c 2e 20 20 48 65 6e 63 65 2c as well. Hence,
1b508 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e this routine on
1b509 6c 79 20 63 6f 6d 70 61 72 65 73 20 74 68 65 20 ly compares the
1b50a 70 72 65 66 69 78 65 73 20 0a 2a 2a 20 6f 66 20 prefixes .** of
1b50b 74 68 65 20 6b 65 79 73 20 70 72 69 6f 72 20 74 the keys prior t
1b50c 6f 20 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 o the final rowi
1b50d 64 2c 20 6e 6f 74 20 74 68 65 20 65 6e 74 69 72 d, not the entir
1b50e 65 20 6b 65 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 e key..*/.SQLITE
1b50f 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1b510 69 74 65 33 56 64 62 65 49 64 78 4b 65 79 43 6f ite3VdbeIdxKeyCo
1b511 6d 70 61 72 65 28 0a 20 20 56 64 62 65 43 75 72 mpare(. VdbeCur
1b512 73 6f 72 20 2a 70 43 2c 20 20 20 20 20 20 20 20 sor *pC,
1b513 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 /* The curs
1b514 6f 72 20 74 6f 20 63 6f 6d 70 61 72 65 20 61 67 or to compare ag
1b515 61 69 6e 73 74 20 2a 2f 0a 20 20 55 6e 70 61 63 ainst */. Unpac
1b516 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 6e 70 61 kedRecord *pUnpa
1b517 63 6b 65 64 2c 20 20 2f 2a 20 55 6e 70 61 63 6b cked, /* Unpack
1b518 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 6b 65 ed version of ke
1b519 79 20 74 6f 20 63 6f 6d 70 61 72 65 20 61 67 61 y to compare aga
1b51a 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 72 inst */. int *r
1b51b 65 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 es
1b51c 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 /* Write t
1b51d 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 72 65 he comparison re
1b51e 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a sult here */.){.
1b51f 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d i64 nCellKey =
1b520 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 0;. int rc;.
1b521 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d BtCursor *pCur =
1b522 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 pC->pCursor;.
1b523 4d 65 6d 20 6d 3b 0a 0a 20 20 61 73 73 65 72 74 Mem m;.. assert
1b524 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 ( sqlite3BtreeCu
1b525 72 73 6f 72 49 73 56 61 6c 69 64 28 70 43 75 72 rsorIsValid(pCur
1b526 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 ) );. rc = sqli
1b527 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 te3BtreeKeySize(
1b528 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 pCur, &nCellKey)
1b529 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d ;. assert( rc==
1b52a 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20 SQLITE_OK );
1b52b 2f 2a 20 70 43 75 72 20 69 73 20 61 6c 77 61 79 /* pCur is alway
1b52c 73 20 76 61 6c 69 64 20 73 6f 20 4b 65 79 53 69 s valid so KeySi
1b52d 7a 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20 2a ze cannot fail *
1b52e 2f 0a 20 20 2f 2a 20 6e 43 65 6c 6c 4b 65 79 20 /. /* nCellKey
1b52f 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 62 will always be b
1b530 65 74 77 65 65 6e 20 30 20 61 6e 64 20 30 78 66 etween 0 and 0xf
1b531 66 66 66 66 66 66 66 20 62 65 63 61 75 73 65 20 fffffff because
1b532 6f 66 20 74 68 65 20 73 61 79 0a 20 20 2a 2a 20 of the say. **
1b533 74 68 61 74 20 62 74 72 65 65 50 61 72 73 65 43 that btreeParseC
1b534 65 6c 6c 50 74 72 28 29 20 61 6e 64 20 73 71 6c ellPtr() and sql
1b535 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 ite3GetVarint32(
1b536 29 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 ) are implemente
1b537 64 20 2a 2f 0a 20 20 69 66 28 20 6e 43 65 6c 6c d */. if( nCell
1b538 4b 65 79 3c 3d 30 20 7c 7c 20 6e 43 65 6c 6c 4b Key<=0 || nCellK
1b539 65 79 3e 30 78 37 66 66 66 66 66 66 66 20 29 7b ey>0x7fffffff ){
1b53a 0a 20 20 20 20 2a 72 65 73 20 3d 20 30 3b 0a 20 . *res = 0;.
1b53b 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1b53c 5f 43 4f 52 52 55 50 54 3b 0a 20 20 7d 0a 20 20 _CORRUPT;. }.
1b53d 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 memset(&m, 0, si
1b53e 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 72 63 20 3d zeof(m));. rc =
1b53f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 sqlite3VdbeMemF
1b540 72 6f 6d 42 74 72 65 65 28 70 43 2d 3e 70 43 75 romBtree(pC->pCu
1b541 72 73 6f 72 2c 20 30 2c 20 28 69 6e 74 29 6e 43 rsor, 0, (int)nC
1b542 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0a ellKey, 1, &m);.
1b543 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
1b544 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 return rc;. }.
1b545 20 61 73 73 65 72 74 28 20 70 55 6e 70 61 63 6b assert( pUnpack
1b546 65 64 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 ed->flags & UNPA
1b547 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 CKED_IGNORE_ROWI
1b548 44 20 29 3b 0a 20 20 2a 72 65 73 20 3d 20 73 71 D );. *res = sq
1b549 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 lite3VdbeRecordC
1b54a 6f 6d 70 61 72 65 28 6d 2e 6e 2c 20 6d 2e 7a 2c ompare(m.n, m.z,
1b54b 20 70 55 6e 70 61 63 6b 65 64 29 3b 0a 20 20 73 pUnpacked);. s
1b54c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1b54d 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 74 75 ease(&m);. retu
1b54e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1b54f 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
1b550 69 6e 65 20 73 65 74 73 20 74 68 65 20 76 61 6c ine sets the val
1b551 75 65 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 ue to be returne
1b552 64 20 62 79 20 73 75 62 73 65 71 75 65 6e 74 20 d by subsequent
1b553 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 calls to.** sqli
1b554 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 6f 6e te3_changes() on
1b555 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 the database ha
1b556 6e 64 6c 65 20 27 64 62 27 2e 20 0a 2a 2f 0a 53 ndle 'db'. .*/.S
1b557 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1b558 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 id sqlite3VdbeSe
1b559 74 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 tChanges(sqlite3
1b55a 20 2a 64 62 2c 20 69 6e 74 20 6e 43 68 61 6e 67 *db, int nChang
1b55b 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 e){. assert( sq
1b55c 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1b55d 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 (db->mutex) );.
1b55e 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 6e db->nChange = n
1b55f 43 68 61 6e 67 65 3b 0a 20 20 64 62 2d 3e 6e 54 Change;. db->nT
1b560 6f 74 61 6c 43 68 61 6e 67 65 20 2b 3d 20 6e 43 otalChange += nC
1b561 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 hange;.}../*.**
1b562 53 65 74 20 61 20 66 6c 61 67 20 69 6e 20 74 68 Set a flag in th
1b563 65 20 76 64 62 65 20 74 6f 20 75 70 64 61 74 65 e vdbe to update
1b564 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e the change coun
1b565 74 65 72 20 77 68 65 6e 20 69 74 20 69 73 20 66 ter when it is f
1b566 69 6e 61 6c 69 73 65 64 0a 2a 2a 20 6f 72 20 72 inalised.** or r
1b567 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f eset..*/.SQLITE_
1b568 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1b569 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61 ite3VdbeCountCha
1b56a 6e 67 65 73 28 56 64 62 65 20 2a 76 29 7b 0a 20 nges(Vdbe *v){.
1b56b 20 76 2d 3e 63 68 61 6e 67 65 43 6e 74 4f 6e 20 v->changeCntOn
1b56c 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 = 1;.}../*.** Ma
1b56d 72 6b 20 65 76 65 72 79 20 70 72 65 70 61 72 65 rk every prepare
1b56e 64 20 73 74 61 74 65 6d 65 6e 74 20 61 73 73 6f d statement asso
1b56f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61 ciated with a da
1b570 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1b571 6e 0a 2a 2a 20 61 73 20 65 78 70 69 72 65 64 2e n.** as expired.
1b572 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 69 72 65 .**.** An expire
1b573 64 20 73 74 61 74 65 6d 65 6e 74 20 6d 65 61 6e d statement mean
1b574 73 20 74 68 61 74 20 72 65 63 6f 6d 70 69 6c 61 s that recompila
1b575 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74 tion of the stat
1b576 65 6d 65 6e 74 20 69 73 0a 2a 2a 20 72 65 63 6f ement is.** reco
1b577 6d 6d 65 6e 64 2e 20 20 53 74 61 74 65 6d 65 6e mmend. Statemen
1b578 74 73 20 65 78 70 69 72 65 20 77 68 65 6e 20 74 ts expire when t
1b579 68 69 6e 67 73 20 68 61 70 70 65 6e 20 74 68 61 hings happen tha
1b57a 74 20 6d 61 6b 65 20 74 68 65 69 72 0a 2a 2a 20 t make their.**
1b57b 70 72 6f 67 72 61 6d 73 20 6f 62 73 6f 6c 65 74 programs obsolet
1b57c 65 2e 20 20 52 65 6d 6f 76 69 6e 67 20 75 73 65 e. Removing use
1b57d 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 r-defined functi
1b57e 6f 6e 73 20 6f 72 20 63 6f 6c 6c 61 74 69 6e 67 ons or collating
1b57f 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73 2c 20 6f .** sequences, o
1b580 72 20 63 68 61 6e 67 69 6e 67 20 61 6e 20 61 75 r changing an au
1b581 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 thorization func
1b582 74 69 6f 6e 20 61 72 65 20 74 68 65 20 74 79 70 tion are the typ
1b583 65 73 20 6f 66 0a 2a 2a 20 74 68 69 6e 67 73 20 es of.** things
1b584 74 68 61 74 20 6d 61 6b 65 20 70 72 65 70 61 72 that make prepar
1b585 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 62 ed statements ob
1b586 73 6f 6c 65 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 solete..*/.SQLIT
1b587 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1b588 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 qlite3ExpirePrep
1b589 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 73 aredStatements(s
1b58a 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 qlite3 *db){. V
1b58b 64 62 65 20 2a 70 3b 0a 20 20 66 6f 72 28 70 20 dbe *p;. for(p
1b58c 3d 20 64 62 2d 3e 70 56 64 62 65 3b 20 70 3b 20 = db->pVdbe; p;
1b58d 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 p=p->pNext){.
1b58e 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b p->expired = 1;
1b58f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
1b590 74 75 72 6e 20 74 68 65 20 64 61 74 61 62 61 73 turn the databas
1b591 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
1b592 68 20 74 68 65 20 56 64 62 65 2e 0a 2a 2f 0a 53 h the Vdbe..*/.S
1b593 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 QLITE_PRIVATE sq
1b594 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 56 64 lite3 *sqlite3Vd
1b595 62 65 44 62 28 56 64 62 65 20 2a 76 29 7b 0a 20 beDb(Vdbe *v){.
1b596 20 72 65 74 75 72 6e 20 76 2d 3e 64 62 3b 0a 7d return v->db;.}
1b597 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
1b598 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 pointer to an s
1b599 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 73 74 72 qlite3_value str
1b59a 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e ucture containin
1b59b 67 20 74 68 65 20 76 61 6c 75 65 20 62 6f 75 6e g the value boun
1b59c 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 d.** parameter i
1b59d 56 61 72 20 6f 66 20 56 4d 20 76 2e 20 45 78 63 Var of VM v. Exc
1b59e 65 70 74 2c 20 69 66 20 74 68 65 20 76 61 6c 75 ept, if the valu
1b59f 65 20 69 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c e is an SQL NULL
1b5a0 2c 20 72 65 74 75 72 6e 20 0a 2a 2a 20 30 20 69 , return .** 0 i
1b5a1 6e 73 74 65 61 64 2e 20 55 6e 6c 65 73 73 20 69 nstead. Unless i
1b5a2 74 20 69 73 20 4e 55 4c 4c 2c 20 61 70 70 6c 79 t is NULL, apply
1b5a3 20 61 66 66 69 6e 69 74 79 20 61 66 66 20 28 6f affinity aff (o
1b5a4 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 ne of the SQLITE
1b5a5 5f 41 46 46 5f 2a 0a 2a 2a 20 63 6f 6e 73 74 61 _AFF_*.** consta
1b5a6 6e 74 73 29 20 74 6f 20 74 68 65 20 76 61 6c 75 nts) to the valu
1b5a7 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 e before returni
1b5a8 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ng it..**.** The
1b5a9 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 returned value
1b5aa 6d 75 73 74 20 62 65 20 66 72 65 65 64 20 62 79 must be freed by
1b5ab 20 74 68 65 20 63 61 6c 6c 65 72 20 75 73 69 6e the caller usin
1b5ac 67 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 g sqlite3ValueFr
1b5ad 65 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ee()..*/.SQLITE_
1b5ae 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f PRIVATE sqlite3_
1b5af 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 64 value *sqlite3Vd
1b5b0 62 65 47 65 74 56 61 6c 75 65 28 56 64 62 65 20 beGetValue(Vdbe
1b5b1 2a 76 2c 20 69 6e 74 20 69 56 61 72 2c 20 75 38 *v, int iVar, u8
1b5b2 20 61 66 66 29 7b 0a 20 20 61 73 73 65 72 74 28 aff){. assert(
1b5b3 20 69 56 61 72 3e 30 20 29 3b 0a 20 20 69 66 28 iVar>0 );. if(
1b5b4 20 76 20 29 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 v ){. Mem *p
1b5b5 4d 65 6d 20 3d 20 26 76 2d 3e 61 56 61 72 5b 69 Mem = &v->aVar[i
1b5b6 56 61 72 2d 31 5d 3b 0a 20 20 20 20 69 66 28 20 Var-1];. if(
1b5b7 30 3d 3d 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 0==(pMem->flags
1b5b8 26 20 4d 45 4d 5f 4e 75 6c 6c 29 20 29 7b 0a 20 & MEM_Null) ){.
1b5b9 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c sqlite3_val
1b5ba 75 65 20 2a 70 52 65 74 20 3d 20 73 71 6c 69 74 ue *pRet = sqlit
1b5bb 65 33 56 61 6c 75 65 4e 65 77 28 76 2d 3e 64 62 e3ValueNew(v->db
1b5bc 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 52 65 );. if( pRe
1b5bd 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c t ){. sql
1b5be 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 ite3VdbeMemCopy(
1b5bf 28 4d 65 6d 20 2a 29 70 52 65 74 2c 20 70 4d 65 (Mem *)pRet, pMe
1b5c0 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 m);. sqli
1b5c1 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 te3ValueApplyAff
1b5c2 69 6e 69 74 79 28 70 52 65 74 2c 20 61 66 66 2c inity(pRet, aff,
1b5c3 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 SQLITE_UTF8);.
1b5c4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1b5c5 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 28 beMemStoreType((
1b5c6 4d 65 6d 20 2a 29 70 52 65 74 29 3b 0a 20 20 20 Mem *)pRet);.
1b5c7 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 }. retur
1b5c8 6e 20 70 52 65 74 3b 0a 20 20 20 20 7d 0a 20 20 n pRet;. }.
1b5c9 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
1b5ca 0a 2f 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 65 ./*.** Configure
1b5cb 20 53 51 4c 20 76 61 72 69 61 62 6c 65 20 69 56 SQL variable iV
1b5cc 61 72 20 73 6f 20 74 68 61 74 20 62 69 6e 64 69 ar so that bindi
1b5cd 6e 67 20 61 20 6e 65 77 20 76 61 6c 75 65 20 74 ng a new value t
1b5ce 6f 20 69 74 20 73 69 67 6e 61 6c 73 0a 2a 2a 20 o it signals.**
1b5cf 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 6f 70 74 to sqlite3_reopt
1b5d0 69 6d 69 7a 65 28 29 20 74 68 61 74 20 72 65 2d imize() that re-
1b5d1 70 72 65 70 61 72 69 6e 67 20 74 68 65 20 73 74 preparing the st
1b5d2 61 74 65 6d 65 6e 74 20 6d 61 79 20 72 65 73 75 atement may resu
1b5d3 6c 74 0a 2a 2a 20 69 6e 20 61 20 62 65 74 74 65 lt.** in a bette
1b5d4 72 20 71 75 65 72 79 20 70 6c 61 6e 2e 0a 2a 2f r query plan..*/
1b5d5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1b5d6 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
1b5d7 53 65 74 56 61 72 6d 61 73 6b 28 56 64 62 65 20 SetVarmask(Vdbe
1b5d8 2a 76 2c 20 69 6e 74 20 69 56 61 72 29 7b 0a 20 *v, int iVar){.
1b5d9 20 61 73 73 65 72 74 28 20 69 56 61 72 3e 30 20 assert( iVar>0
1b5da 29 3b 0a 20 20 69 66 28 20 69 56 61 72 3e 33 32 );. if( iVar>32
1b5db 20 29 7b 0a 20 20 20 20 76 2d 3e 65 78 70 6d 61 ){. v->expma
1b5dc 73 6b 20 3d 20 30 78 66 66 66 66 66 66 66 66 3b sk = 0xffffffff;
1b5dd 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 76 2d . }else{. v-
1b5de 3e 65 78 70 6d 61 73 6b 20 7c 3d 20 28 28 75 33 >expmask |= ((u3
1b5df 32 29 31 20 3c 3c 20 28 69 56 61 72 2d 31 29 29 2)1 << (iVar-1))
1b5e0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a ;. }.}../******
1b5e1 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
1b5e2 76 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a vdbeaux.c ******
1b5e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5e5 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
1b5e6 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
1b5e7 69 6c 65 20 76 64 62 65 61 70 69 2e 63 20 2a 2a ile vdbeapi.c **
1b5e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5ea 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
1b5eb 30 30 34 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 004 May 26.**.**
1b5ec 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1b5ed 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1b5ee 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1b5ef 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1b5f0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
1b5f1 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
1b5f2 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
1b5f3 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
1b5f4 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
1b5f5 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
1b5f6 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
1b5f7 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
1b5f8 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
1b5f9 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
1b5fa 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1b5fb 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
1b5fc 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
1b5fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b601 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
1b602 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
1b603 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f 20 69 ns code use to i
1b604 6d 70 6c 65 6d 65 6e 74 20 41 50 49 73 20 74 68 mplement APIs th
1b605 61 74 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 at are part of t
1b606 68 65 0a 2a 2a 20 56 44 42 45 2e 0a 2a 2a 0a 2a he.** VDBE..**.*
1b607 2a 20 24 49 64 3a 20 76 64 62 65 61 70 69 2e 63 * $Id: vdbeapi.c
1b608 2c 76 20 31 2e 31 36 37 20 32 30 30 39 2f 30 36 ,v 1.167 2009/06
1b609 2f 32 35 20 30 31 3a 34 37 3a 31 32 20 64 72 68 /25 01:47:12 drh
1b60a 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 Exp $.*/..#ifnd
1b60b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 ef SQLITE_OMIT_D
1b60c 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 EPRECATED./*.**
1b60d 52 65 74 75 72 6e 20 54 52 55 45 20 28 6e 6f 6e Return TRUE (non
1b60e 2d 7a 65 72 6f 29 20 6f 66 20 74 68 65 20 73 74 -zero) of the st
1b60f 61 74 65 6d 65 6e 74 20 73 75 70 70 6c 69 65 64 atement supplied
1b610 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 as an argument
1b611 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20 72 needs.** to be r
1b612 65 63 6f 6d 70 69 6c 65 64 2e 20 20 41 20 73 74 ecompiled. A st
1b613 61 74 65 6d 65 6e 74 20 6e 65 65 64 73 20 74 6f atement needs to
1b614 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20 77 be recompiled w
1b615 68 65 6e 65 76 65 72 20 74 68 65 0a 2a 2a 20 65 henever the.** e
1b616 78 65 63 75 74 69 6f 6e 20 65 6e 76 69 72 6f 6e xecution environ
1b617 6d 65 6e 74 20 63 68 61 6e 67 65 73 20 69 6e 20 ment changes in
1b618 61 20 77 61 79 20 74 68 61 74 20 77 6f 75 6c 64 a way that would
1b619 20 61 6c 74 65 72 20 74 68 65 20 70 72 6f 67 72 alter the progr
1b61a 61 6d 0a 2a 2a 20 74 68 61 74 20 73 71 6c 69 74 am.** that sqlit
1b61b 65 33 5f 70 72 65 70 61 72 65 28 29 20 67 65 6e e3_prepare() gen
1b61c 65 72 61 74 65 73 2e 20 20 46 6f 72 20 65 78 61 erates. For exa
1b61d 6d 70 6c 65 2c 20 69 66 20 6e 65 77 20 66 75 6e mple, if new fun
1b61e 63 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 63 6f 6c ctions or.** col
1b61f 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 lating sequences
1b620 20 61 72 65 20 72 65 67 69 73 74 65 72 65 64 20 are registered
1b621 6f 72 20 69 66 20 61 6e 20 61 75 74 68 6f 72 69 or if an authori
1b622 7a 65 72 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a zer function is.
1b623 2a 2a 20 61 64 64 65 64 20 6f 72 20 63 68 61 6e ** added or chan
1b624 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ged..*/.SQLITE_A
1b625 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 PI int sqlite3_e
1b626 78 70 69 72 65 64 28 73 71 6c 69 74 65 33 5f 73 xpired(sqlite3_s
1b627 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 tmt *pStmt){. V
1b628 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 dbe *p = (Vdbe*)
1b629 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 pStmt;. return
1b62a 70 3d 3d 30 20 7c 7c 20 70 2d 3e 65 78 70 69 72 p==0 || p->expir
1b62b 65 64 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a ed;.}.#endif../*
1b62c 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
1b62d 67 20 72 6f 75 74 69 6e 65 20 64 65 73 74 72 6f g routine destro
1b62e 79 73 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63 ys a virtual mac
1b62f 68 69 6e 65 20 74 68 61 74 20 69 73 20 63 72 65 hine that is cre
1b630 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 73 ated by.** the s
1b631 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29 qlite3_compile()
1b632 20 72 6f 75 74 69 6e 65 2e 20 54 68 65 20 69 6e routine. The in
1b633 74 65 67 65 72 20 72 65 74 75 72 6e 65 64 20 69 teger returned i
1b634 73 20 61 6e 20 53 51 4c 49 54 45 5f 0a 2a 2a 20 s an SQLITE_.**
1b635 73 75 63 63 65 73 73 2f 66 61 69 6c 75 72 65 20 success/failure
1b636 63 6f 64 65 20 74 68 61 74 20 64 65 73 63 72 69 code that descri
1b637 62 65 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f bes the result o
1b638 66 20 65 78 65 63 75 74 69 6e 67 20 74 68 65 20 f executing the
1b639 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 virtual.** machi
1b63a 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ne..**.** This r
1b63b 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 outine sets the
1b63c 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 73 error code and s
1b63d 74 72 69 6e 67 20 72 65 74 75 72 6e 65 64 20 62 tring returned b
1b63e 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 y.** sqlite3_err
1b63f 63 6f 64 65 28 29 2c 20 73 71 6c 69 74 65 33 5f code(), sqlite3_
1b640 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71 6c errmsg() and sql
1b641 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 2e ite3_errmsg16().
1b642 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
1b643 6e 74 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c nt sqlite3_final
1b644 69 7a 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ize(sqlite3_stmt
1b645 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 *pStmt){. int
1b646 72 63 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 3d rc;. if( pStmt=
1b647 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 =0 ){. rc = S
1b648 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 QLITE_OK;. }els
1b649 65 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d e{. Vdbe *v =
1b64a 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe*)pStmt;.
1b64b 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d sqlite3 *db =
1b64c 20 76 2d 3e 64 62 3b 0a 23 69 66 20 53 51 4c 49 v->db;.#if SQLI
1b64d 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 TE_THREADSAFE.
1b64e 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
1b64f 2a 6d 75 74 65 78 20 3d 20 76 2d 3e 64 62 2d 3e *mutex = v->db->
1b650 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20 mutex;.#endif.
1b651 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1b652 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 enter(mutex);.
1b653 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1b654 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a 20 beFinalize(v);.
1b655 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 rc = sqlite3A
1b656 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a piExit(db, rc);.
1b657 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1b658 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a x_leave(mutex);.
1b659 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1b65a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 72 6d 69 6e .}../*.** Termin
1b65b 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ate the current
1b65c 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 61 6e 20 execution of an
1b65d 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 6e SQL statement an
1b65e 64 20 72 65 73 65 74 20 69 74 0a 2a 2a 20 62 61 d reset it.** ba
1b65f 63 6b 20 74 6f 20 69 74 73 20 73 74 61 72 74 69 ck to its starti
1b660 6e 67 20 73 74 61 74 65 20 73 6f 20 74 68 61 74 ng state so that
1b661 20 69 74 20 63 61 6e 20 62 65 20 72 65 75 73 65 it can be reuse
1b662 64 2e 20 41 20 73 75 63 63 65 73 73 20 63 6f 64 d. A success cod
1b663 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 70 72 e from.** the pr
1b664 69 6f 72 20 65 78 65 63 75 74 69 6f 6e 20 69 73 ior execution is
1b665 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
1b666 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 This routine se
1b667 74 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 ts the error cod
1b668 65 20 61 6e 64 20 73 74 72 69 6e 67 20 72 65 74 e and string ret
1b669 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 urned by.** sqli
1b66a 74 65 33 5f 65 72 72 63 6f 64 65 28 29 2c 20 73 te3_errcode(), s
1b66b 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 qlite3_errmsg()
1b66c 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 6d and sqlite3_errm
1b66d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 sg16()..*/.SQLIT
1b66e 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1b66f 33 5f 72 65 73 65 74 28 73 71 6c 69 74 65 33 5f 3_reset(sqlite3_
1b670 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 stmt *pStmt){.
1b671 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 53 int rc;. if( pS
1b672 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 tmt==0 ){. rc
1b673 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1b674 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 62 65 20 }else{. Vdbe
1b675 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d *v = (Vdbe*)pStm
1b676 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d t;. sqlite3_m
1b677 75 74 65 78 5f 65 6e 74 65 72 28 76 2d 3e 64 62 utex_enter(v->db
1b678 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 ->mutex);. rc
1b679 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 = sqlite3VdbeRe
1b67a 73 65 74 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 set(v);. sqli
1b67b 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 te3VdbeMakeReady
1b67c 28 76 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 30 2c (v, -1, 0, 0, 0,
1b67d 20 30 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65 0, 0);. asse
1b67e 72 74 28 20 28 72 63 20 26 20 28 76 2d 3e 64 62 rt( (rc & (v->db
1b67f 2d 3e 65 72 72 4d 61 73 6b 29 29 3d 3d 72 63 20 ->errMask))==rc
1b680 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 );. rc = sqli
1b681 74 65 33 41 70 69 45 78 69 74 28 76 2d 3e 64 62 te3ApiExit(v->db
1b682 2c 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 , rc);. sqlit
1b683 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 76 e3_mutex_leave(v
1b684 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 ->db->mutex);.
1b685 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1b686 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61 6c 6c 20 ../*.** Set all
1b687 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 69 the parameters i
1b688 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 53 n the compiled S
1b689 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 QL statement to
1b68a 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f NULL..*/.SQLITE_
1b68b 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1b68c 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 73 clear_bindings(s
1b68d 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1b68e 6d 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 mt){. int i;.
1b68f 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1b690 4f 4b 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 OK;. Vdbe *p =
1b691 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 23 69 (Vdbe*)pStmt;.#i
1b692 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 f SQLITE_THREADS
1b693 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 AFE. sqlite3_mu
1b694 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 28 28 56 tex *mutex = ((V
1b695 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 64 62 2d dbe*)pStmt)->db-
1b696 3e 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a 20 >mutex;.#endif.
1b697 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1b698 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 66 nter(mutex);. f
1b699 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 or(i=0; i<p->nVa
1b69a 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c r; i++){. sql
1b69b 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
1b69c 73 65 28 26 70 2d 3e 61 56 61 72 5b 69 5d 29 3b se(&p->aVar[i]);
1b69d 0a 20 20 20 20 70 2d 3e 61 56 61 72 5b 69 5d 2e . p->aVar[i].
1b69e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1b69f 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 ;. }. if( p->i
1b6a0 73 50 72 65 70 61 72 65 56 32 20 26 26 20 70 2d sPrepareV2 && p-
1b6a1 3e 65 78 70 6d 61 73 6b 20 29 7b 0a 20 20 20 20 >expmask ){.
1b6a2 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0a p->expired = 1;.
1b6a3 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 }. sqlite3_mu
1b6a4 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 tex_leave(mutex)
1b6a5 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
1b6a6 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .../************
1b6a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b6a8 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 20 sqlite3_value_
1b6a9 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1b6aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b6ab 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
1b6ac 67 20 72 6f 75 74 69 6e 65 73 20 65 78 74 72 61 g routines extra
1b6ad 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 ct information f
1b6ae 72 6f 6d 20 61 20 4d 65 6d 20 6f 72 20 73 71 6c rom a Mem or sql
1b6af 69 74 65 33 5f 76 61 6c 75 65 0a 2a 2a 20 73 74 ite3_value.** st
1b6b0 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 ructure..*/.SQLI
1b6b1 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 TE_API const voi
1b6b2 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 d *sqlite3_value
1b6b3 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 76 61 _blob(sqlite3_va
1b6b4 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65 lue *pVal){. Me
1b6b5 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 56 61 m *p = (Mem*)pVa
1b6b6 6c 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 l;. if( p->flag
1b6b7 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 s & (MEM_Blob|ME
1b6b8 4d 5f 53 74 72 29 20 29 7b 0a 20 20 20 20 73 71 M_Str) ){. sq
1b6b9 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 lite3VdbeMemExpa
1b6ba 6e 64 42 6c 6f 62 28 70 29 3b 0a 20 20 20 20 70 ndBlob(p);. p
1b6bb 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f ->flags &= ~MEM_
1b6bc 53 74 72 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 Str;. p->flag
1b6bd 73 20 7c 3d 20 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 s |= MEM_Blob;.
1b6be 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 7a 3b 0a return p->z;.
1b6bf 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
1b6c0 75 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 urn sqlite3_valu
1b6c1 65 5f 74 65 78 74 28 70 56 61 6c 29 3b 0a 20 20 e_text(pVal);.
1b6c2 7d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 }.}.SQLITE_API i
1b6c3 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 nt sqlite3_value
1b6c4 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 _bytes(sqlite3_v
1b6c5 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 alue *pVal){. r
1b6c6 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c eturn sqlite3Val
1b6c7 75 65 42 79 74 65 73 28 70 56 61 6c 2c 20 53 51 ueBytes(pVal, SQ
1b6c8 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 53 51 LITE_UTF8);.}.SQ
1b6c9 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1b6ca 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 ite3_value_bytes
1b6cb 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 16(sqlite3_value
1b6cc 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 *pVal){. retur
1b6cd 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 n sqlite3ValueBy
1b6ce 74 65 73 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 tes(pVal, SQLITE
1b6cf 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b 0a 7d _UTF16NATIVE);.}
1b6d0 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 .SQLITE_API doub
1b6d1 6c 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 le sqlite3_value
1b6d2 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f _double(sqlite3_
1b6d3 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 value *pVal){.
1b6d4 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 return sqlite3Vd
1b6d5 62 65 52 65 61 6c 56 61 6c 75 65 28 28 4d 65 6d beRealValue((Mem
1b6d6 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 *)pVal);.}.SQLIT
1b6d7 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1b6d8 33 5f 76 61 6c 75 65 5f 69 6e 74 28 73 71 6c 69 3_value_int(sqli
1b6d9 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 te3_value *pVal)
1b6da 7b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 {. return (int)
1b6db 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 sqlite3VdbeIntVa
1b6dc 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b lue((Mem*)pVal);
1b6dd 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 .}.SQLITE_API sq
1b6de 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 lite_int64 sqlit
1b6df 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 e3_value_int64(s
1b6e0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 qlite3_value *pV
1b6e1 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 al){. return sq
1b6e2 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 lite3VdbeIntValu
1b6e3 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d e((Mem*)pVal);.}
1b6e4 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
1b6e5 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
1b6e6 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 *sqlite3_value_t
1b6e7 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 ext(sqlite3_valu
1b6e8 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 e *pVal){. retu
1b6e9 72 6e 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e rn (const unsign
1b6ea 65 64 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 ed char *)sqlite
1b6eb 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 3ValueText(pVal,
1b6ec 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d SQLITE_UTF8);.}
1b6ed 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1b6ee 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 OMIT_UTF16.SQLIT
1b6ef 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
1b6f0 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f *sqlite3_value_
1b6f1 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 76 text16(sqlite3_v
1b6f2 61 6c 75 65 2a 20 70 56 61 6c 29 7b 0a 20 20 72 alue* pVal){. r
1b6f3 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c eturn sqlite3Val
1b6f4 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c ueText(pVal, SQL
1b6f5 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29 ITE_UTF16NATIVE)
1b6f6 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 ;.}.SQLITE_API c
1b6f7 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
1b6f8 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 e3_value_text16b
1b6f9 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 e(sqlite3_value
1b6fa 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e *pVal){. return
1b6fb 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 sqlite3ValueTex
1b6fc 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 t(pVal, SQLITE_U
1b6fd 54 46 31 36 42 45 29 3b 0a 7d 0a 53 51 4c 49 54 TF16BE);.}.SQLIT
1b6fe 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
1b6ff 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f *sqlite3_value_
1b700 74 65 78 74 31 36 6c 65 28 73 71 6c 69 74 65 33 text16le(sqlite3
1b701 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 _value *pVal){.
1b702 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 return sqlite3V
1b703 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 alueText(pVal, S
1b704 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29 3b 0a QLITE_UTF16LE);.
1b705 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1b706 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f TE_OMIT_UTF16 */
1b707 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1b708 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 sqlite3_value_ty
1b709 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 pe(sqlite3_value
1b70a 2a 20 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 * pVal){. retur
1b70b 6e 20 70 56 61 6c 2d 3e 74 79 70 65 3b 0a 7d 0a n pVal->type;.}.
1b70c 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1b70d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 ************** s
1b70e 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 20 20 qlite3_result_
1b70f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1b711 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
1b712 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 routines are us
1b713 65 64 20 62 79 20 75 73 65 72 2d 64 65 66 69 6e ed by user-defin
1b714 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 ed functions to
1b715 73 70 65 63 69 66 79 0a 2a 2a 20 74 68 65 20 66 specify.** the f
1b716 75 6e 63 74 69 6f 6e 20 72 65 73 75 6c 74 2e 0a unction result..
1b717 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 74 53 74 72 **.** The setStr
1b718 4f 72 45 72 72 6f 72 28 29 20 66 75 6e 74 69 6f OrError() funtio
1b719 6e 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33 56 n calls sqlite3V
1b71a 64 62 65 4d 65 6d 53 65 74 53 74 72 28 29 20 74 dbeMemSetStr() t
1b71b 6f 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 72 o store the.** r
1b71c 65 73 75 6c 74 20 61 73 20 61 20 73 74 72 69 6e esult as a strin
1b71d 67 20 6f 72 20 62 6c 6f 62 20 62 75 74 20 69 66 g or blob but if
1b71e 20 74 68 65 20 73 74 72 69 6e 67 20 6f 72 20 62 the string or b
1b71f 6c 6f 62 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 lob is too large
1b720 2c 20 69 74 0a 2a 2a 20 74 68 65 6e 20 73 65 74 , it.** then set
1b721 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 s the error code
1b722 20 74 6f 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 to SQLITE_TOOBI
1b723 47 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 G.*/.static void
1b724 20 73 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 setResultStrOrE
1b725 72 72 6f 72 28 0a 20 20 73 71 6c 69 74 65 33 5f rror(. sqlite3_
1b726 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 20 context *pCtx,
1b727 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 /* Function cont
1b728 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ext */. const c
1b729 68 61 72 20 2a 7a 2c 20 20 20 20 20 20 20 20 20 har *z,
1b72a 20 2f 2a 20 53 74 72 69 6e 67 20 70 6f 69 6e 74 /* String point
1b72b 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20 20 er */. int n,
1b72c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b72d 2f 2a 20 42 79 74 65 73 20 69 6e 20 73 74 72 69 /* Bytes in stri
1b72e 6e 67 2c 20 6f 72 20 6e 65 67 61 74 69 76 65 20 ng, or negative
1b72f 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 */. u8 enc,
1b730 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1b731 45 6e 63 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20 Encoding of z.
1b732 30 20 66 6f 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 0 for BLOBs */.
1b733 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f void (*xDel)(vo
1b734 69 64 2a 29 20 20 20 20 20 2f 2a 20 44 65 73 74 id*) /* Dest
1b735 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 ructor function
1b736 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 */.){. if( sqli
1b737 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 te3VdbeMemSetStr
1b738 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c (&pCtx->s, z, n,
1b739 20 65 6e 63 2c 20 78 44 65 6c 29 3d 3d 53 51 4c enc, xDel)==SQL
1b73a 49 54 45 5f 54 4f 4f 42 49 47 20 29 7b 0a 20 20 ITE_TOOBIG ){.
1b73b 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
1b73c 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 70 43 _error_toobig(pC
1b73d 74 78 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 tx);. }.}.SQLIT
1b73e 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
1b73f 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 0a e3_result_blob(.
1b740 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
1b741 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 t *pCtx, . cons
1b742 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e t void *z, . in
1b743 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 t n, . void (*x
1b744 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a Del)(void *).){.
1b745 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 29 assert( n>=0 )
1b746 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
1b747 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1b748 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 Ctx->s.db->mutex
1b749 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 ) );. setResult
1b74a 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c StrOrError(pCtx,
1b74b 20 7a 2c 20 6e 2c 20 30 2c 20 78 44 65 6c 29 3b z, n, 0, xDel);
1b74c 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f .}.SQLITE_API vo
1b74d 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
1b74e 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 t_double(sqlite3
1b74f 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 _context *pCtx,
1b750 64 6f 75 62 6c 65 20 72 56 61 6c 29 7b 0a 20 20 double rVal){.
1b751 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1b752 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d mutex_held(pCtx-
1b753 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b >s.db->mutex) );
1b754 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1b755 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 43 74 78 mSetDouble(&pCtx
1b756 2d 3e 73 2c 20 72 56 61 6c 29 3b 0a 7d 0a 53 51 ->s, rVal);.}.SQ
1b757 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
1b758 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
1b759 6f 72 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 or(sqlite3_conte
1b75a 78 74 20 2a 70 43 74 78 2c 20 63 6f 6e 73 74 20 xt *pCtx, const
1b75b 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b char *z, int n){
1b75c 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1b75d 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
1b75e 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 tx->s.db->mutex)
1b75f 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 );. pCtx->isEr
1b760 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 ror = SQLITE_ERR
1b761 4f 52 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 OR;. sqlite3Vdb
1b762 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 eMemSetStr(&pCtx
1b763 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 ->s, z, n, SQLIT
1b764 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54 E_UTF8, SQLITE_T
1b765 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 69 66 RANSIENT);.}.#if
1b766 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1b767 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 _UTF16.SQLITE_AP
1b768 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
1b769 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 73 71 esult_error16(sq
1b76a 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1b76b 43 74 78 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 Ctx, const void
1b76c 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 *z, int n){. as
1b76d 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1b76e 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
1b76f 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1b770 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d pCtx->isError =
1b771 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
1b772 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1b773 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 etStr(&pCtx->s,
1b774 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 z, n, SQLITE_UTF
1b775 31 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 16NATIVE, SQLITE
1b776 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 _TRANSIENT);.}.#
1b777 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 41 50 49 endif.SQLITE_API
1b778 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
1b779 73 75 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 33 sult_int(sqlite3
1b77a 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 _context *pCtx,
1b77b 69 6e 74 20 69 56 61 6c 29 7b 0a 20 20 61 73 73 int iVal){. ass
1b77c 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1b77d 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e ex_held(pCtx->s.
1b77e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
1b77f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1b780 74 49 6e 74 36 34 28 26 70 43 74 78 2d 3e 73 2c tInt64(&pCtx->s,
1b781 20 28 69 36 34 29 69 56 61 6c 29 3b 0a 7d 0a 53 (i64)iVal);.}.S
1b782 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
1b783 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e qlite3_result_in
1b784 74 36 34 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 t64(sqlite3_cont
1b785 65 78 74 20 2a 70 43 74 78 2c 20 69 36 34 20 69 ext *pCtx, i64 i
1b786 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 Val){. assert(
1b787 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1b788 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d ld(pCtx->s.db->m
1b789 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
1b78a 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 e3VdbeMemSetInt6
1b78b 34 28 26 70 43 74 78 2d 3e 73 2c 20 69 56 61 6c 4(&pCtx->s, iVal
1b78c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
1b78d 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
1b78e 75 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 ult_null(sqlite3
1b78f 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 7b _context *pCtx){
1b790 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1b791 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
1b792 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 tx->s.db->mutex)
1b793 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 );. sqlite3Vdb
1b794 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70 43 74 eMemSetNull(&pCt
1b795 78 2d 3e 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f x->s);.}.SQLITE_
1b796 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
1b797 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 0a 20 20 _result_text(.
1b798 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1b799 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 *pCtx, . const
1b79a 63 68 61 72 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 char *z, . int
1b79b 6e 2c 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c n,. void (*xDel
1b79c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 )(void *).){. a
1b79d 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1b79e 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e utex_held(pCtx->
1b79f 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a s.db->mutex) );.
1b7a0 20 20 73 65 74 52 65 73 75 6c 74 53 74 72 4f 72 setResultStrOr
1b7a1 45 72 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e Error(pCtx, z, n
1b7a2 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 78 , SQLITE_UTF8, x
1b7a3 44 65 6c 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 Del);.}.#ifndef
1b7a4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
1b7a5 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 6.SQLITE_API voi
1b7a6 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
1b7a7 5f 74 65 78 74 31 36 28 0a 20 20 73 71 6c 69 74 _text16(. sqlit
1b7a8 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
1b7a9 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 , . const void
1b7aa 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 *z, . int n, .
1b7ab 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f void (*xDel)(vo
1b7ac 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 id *).){. asser
1b7ad 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1b7ae 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 _held(pCtx->s.db
1b7af 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65 ->mutex) );. se
1b7b0 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f tResultStrOrErro
1b7b1 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 r(pCtx, z, n, SQ
1b7b2 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 LITE_UTF16NATIVE
1b7b3 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 , xDel);.}.SQLIT
1b7b4 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
1b7b5 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 e3_result_text16
1b7b6 62 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f be(. sqlite3_co
1b7b7 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 ntext *pCtx, .
1b7b8 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a const void *z, .
1b7b9 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 int n, . void
1b7ba 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 (*xDel)(void *)
1b7bb 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 .){. assert( sq
1b7bc 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1b7bd 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 (pCtx->s.db->mut
1b7be 65 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75 ex) );. setResu
1b7bf 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74 ltStrOrError(pCt
1b7c0 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f x, z, n, SQLITE_
1b7c1 55 54 46 31 36 42 45 2c 20 78 44 65 6c 29 3b 0a UTF16BE, xDel);.
1b7c2 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 }.SQLITE_API voi
1b7c3 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
1b7c4 5f 74 65 78 74 31 36 6c 65 28 0a 20 20 73 71 6c _text16le(. sql
1b7c5 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 ite3_context *pC
1b7c6 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 tx, . const voi
1b7c7 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 d *z, . int n,
1b7c8 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 . void (*xDel)(
1b7c9 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 void *).){. ass
1b7ca 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1b7cb 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e ex_held(pCtx->s.
1b7cc 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
1b7cd 73 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 setResultStrOrEr
1b7ce 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 ror(pCtx, z, n,
1b7cf 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 SQLITE_UTF16LE,
1b7d0 78 44 65 6c 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 xDel);.}.#endif
1b7d1 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 /* SQLITE_OMIT_U
1b7d2 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 TF16 */.SQLITE_A
1b7d3 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
1b7d4 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 73 71 6c result_value(sql
1b7d5 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 ite3_context *pC
1b7d6 74 78 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 tx, sqlite3_valu
1b7d7 65 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 61 73 e *pValue){. as
1b7d8 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1b7d9 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
1b7da 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1b7db 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 sqlite3VdbeMemC
1b7dc 6f 70 79 28 26 70 43 74 78 2d 3e 73 2c 20 70 56 opy(&pCtx->s, pV
1b7dd 61 6c 75 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f alue);.}.SQLITE_
1b7de 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
1b7df 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 _result_zeroblob
1b7e0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
1b7e1 20 2a 70 43 74 78 2c 20 69 6e 74 20 6e 29 7b 0a *pCtx, int n){.
1b7e2 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1b7e3 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
1b7e4 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
1b7e5 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
1b7e6 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28 26 MemSetZeroBlob(&
1b7e7 70 43 74 78 2d 3e 73 2c 20 6e 29 3b 0a 7d 0a 53 pCtx->s, n);.}.S
1b7e8 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
1b7e9 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
1b7ea 72 6f 72 5f 63 6f 64 65 28 73 71 6c 69 74 65 33 ror_code(sqlite3
1b7eb 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 _context *pCtx,
1b7ec 69 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a 20 20 int errCode){.
1b7ed 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 pCtx->isError =
1b7ee 65 72 72 43 6f 64 65 3b 0a 20 20 69 66 28 20 70 errCode;. if( p
1b7ef 43 74 78 2d 3e 73 2e 66 6c 61 67 73 20 26 20 4d Ctx->s.flags & M
1b7f0 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 EM_Null ){. s
1b7f1 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
1b7f2 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 73 71 Str(&pCtx->s, sq
1b7f3 6c 69 74 65 33 45 72 72 53 74 72 28 65 72 72 43 lite3ErrStr(errC
1b7f4 6f 64 65 29 2c 20 2d 31 2c 20 0a 20 20 20 20 20 ode), -1, .
1b7f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b7f6 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c SQLITE_UTF8,
1b7f7 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b SQLITE_STATIC);
1b7f8 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 46 6f 72 63 65 . }.}../* Force
1b7f9 20 61 6e 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 an SQLITE_TOOBI
1b7fa 47 20 65 72 72 6f 72 2e 20 2a 2f 0a 53 51 4c 49 G error. */.SQLI
1b7fb 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
1b7fc 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
1b7fd 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 33 5f _toobig(sqlite3_
1b7fe 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 7b 0a context *pCtx){.
1b7ff 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1b800 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
1b801 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
1b802 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 );. pCtx->isErr
1b803 6f 72 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 or = SQLITE_TOOB
1b804 49 47 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 IG;. sqlite3Vdb
1b805 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 eMemSetStr(&pCtx
1b806 2d 3e 73 2c 20 22 73 74 72 69 6e 67 20 6f 72 20 ->s, "string or
1b807 62 6c 6f 62 20 74 6f 6f 20 62 69 67 22 2c 20 2d blob too big", -
1b808 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 1, .
1b809 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 SQLIT
1b80a 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 E_UTF8, SQLITE_S
1b80b 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 20 41 6e TATIC);.}../* An
1b80c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 65 72 SQLITE_NOMEM er
1b80d 72 6f 72 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 ror. */.SQLITE_A
1b80e 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
1b80f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d result_error_nom
1b810 65 6d 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 em(sqlite3_conte
1b811 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 xt *pCtx){. ass
1b812 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1b813 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e ex_held(pCtx->s.
1b814 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
1b815 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1b816 74 4e 75 6c 6c 28 26 70 43 74 78 2d 3e 73 29 3b tNull(&pCtx->s);
1b817 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 . pCtx->isError
1b818 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
1b819 0a 20 20 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d . pCtx->s.db->m
1b81a 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
1b81b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 .}../*.** Execut
1b81c 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 e the statement
1b81d 70 53 74 6d 74 2c 20 65 69 74 68 65 72 20 75 6e pStmt, either un
1b81e 74 69 6c 20 61 20 72 6f 77 20 6f 66 20 64 61 74 til a row of dat
1b81f 61 20 69 73 20 72 65 61 64 79 2c 20 74 68 65 0a a is ready, the.
1b820 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 ** statement is
1b821 63 6f 6d 70 6c 65 74 65 6c 79 20 65 78 65 63 75 completely execu
1b822 74 65 64 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 ted or an error
1b823 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 occurs..**.** Th
1b824 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 is routine imple
1b825 6d 65 6e 74 73 20 74 68 65 20 62 75 6c 6b 20 6f ments the bulk o
1b826 66 20 74 68 65 20 6c 6f 67 69 63 20 62 65 68 69 f the logic behi
1b827 6e 64 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 nd the sqlite_st
1b828 65 70 28 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 ep().** API. Th
1b829 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 6f 6d 69 e only thing omi
1b82a 74 74 65 64 20 69 73 20 74 68 65 20 61 75 74 6f tted is the auto
1b82b 6d 61 74 69 63 20 72 65 63 6f 6d 70 69 6c 65 20 matic recompile
1b82c 69 66 20 61 20 0a 2a 2a 20 73 63 68 65 6d 61 20 if a .** schema
1b82d 63 68 61 6e 67 65 20 68 61 73 20 6f 63 63 75 72 change has occur
1b82e 72 65 64 2e 20 20 54 68 61 74 20 64 65 74 61 69 red. That detai
1b82f 6c 20 69 73 20 68 61 6e 64 6c 65 64 20 62 79 20 l is handled by
1b830 74 68 65 0a 2a 2a 20 6f 75 74 65 72 20 73 71 6c the.** outer sql
1b831 69 74 65 33 5f 73 74 65 70 28 29 20 77 72 61 70 ite3_step() wrap
1b832 70 65 72 20 70 72 6f 63 65 64 75 72 65 2e 0a 2a per procedure..*
1b833 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c /.static int sql
1b834 69 74 65 33 53 74 65 70 28 56 64 62 65 20 2a 70 ite3Step(Vdbe *p
1b835 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 ){. sqlite3 *db
1b836 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 ;. int rc;.. a
1b837 73 73 65 72 74 28 70 29 3b 0a 20 20 69 66 28 20 ssert(p);. if(
1b838 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d p->magic!=VDBE_M
1b839 41 47 49 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 AGIC_RUN ){.
1b83a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 return SQLITE_MI
1b83b 53 55 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 SUSE;. }.. /*
1b83c 41 73 73 65 72 74 20 74 68 61 74 20 6d 61 6c 6c Assert that mall
1b83d 6f 63 28 29 20 68 61 73 20 6e 6f 74 20 66 61 69 oc() has not fai
1b83e 6c 65 64 20 2a 2f 0a 20 20 64 62 20 3d 20 70 2d led */. db = p-
1b83f 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d >db;. if( db->m
1b840 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1b841 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1b842 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 _NOMEM;. }.. i
1b843 66 28 20 70 2d 3e 70 63 3c 3d 30 20 26 26 20 70 f( p->pc<=0 && p
1b844 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a 20 20 20 ->expired ){.
1b845 20 69 66 28 20 41 4c 57 41 59 53 28 70 2d 3e 72 if( ALWAYS(p->r
1b846 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c==SQLITE_OK ||
1b847 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 p->rc==SQLITE_SC
1b848 48 45 4d 41 29 20 29 7b 0a 20 20 20 20 20 20 70 HEMA) ){. p
1b849 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 53 43 ->rc = SQLITE_SC
1b84a 48 45 4d 41 3b 0a 20 20 20 20 7d 0a 20 20 20 20 HEMA;. }.
1b84b 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
1b84c 52 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f R;. goto end_
1b84d 6f 66 5f 73 74 65 70 3b 0a 20 20 7d 0a 20 20 69 of_step;. }. i
1b84e 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1b84f 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 70 2d On(db) ){. p-
1b850 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 >rc = SQLITE_MIS
1b851 55 53 45 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 USE;. return
1b852 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
1b853 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 63 3c 30 }. if( p->pc<0
1b854 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 ){. /* If th
1b855 65 72 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 ere are no other
1b856 20 73 74 61 74 65 6d 65 6e 74 73 20 63 75 72 72 statements curr
1b857 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 2c 20 74 ently running, t
1b858 68 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 65 74 hen. ** reset
1b859 20 74 68 65 20 69 6e 74 65 72 72 75 70 74 20 66 the interrupt f
1b85a 6c 61 67 2e 20 20 54 68 69 73 20 70 72 65 76 65 lag. This preve
1b85b 6e 74 73 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 nts a call to sq
1b85c 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 0a lite3_interrupt.
1b85d 20 20 20 20 2a 2a 20 66 72 6f 6d 20 69 6e 74 65 ** from inte
1b85e 72 72 75 70 74 69 6e 67 20 61 20 73 74 61 74 65 rrupting a state
1b85f 6d 65 6e 74 20 74 68 61 74 20 68 61 73 20 6e 6f ment that has no
1b860 74 20 79 65 74 20 73 74 61 72 74 65 64 2e 0a 20 t yet started..
1b861 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 */. if( db
1b862 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3d ->activeVdbeCnt=
1b863 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e =0 ){. db->
1b864 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 u1.isInterrupted
1b865 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 = 0;. }..
1b866 20 61 73 73 65 72 74 28 20 64 62 2d 3e 77 72 69 assert( db->wri
1b867 74 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20 64 teVdbeCnt>0 || d
1b868 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 b->autoCommit==0
1b869 20 7c 7c 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 || db->nDeferre
1b86a 64 43 6f 6e 73 3d 3d 30 20 29 3b 0a 0a 23 69 66 dCons==0 );..#if
1b86b 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1b86c 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28 20 64 _TRACE. if( d
1b86d 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26 20 21 b->xProfile && !
1b86e 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b db->init.busy ){
1b86f 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 4e . double rN
1b870 6f 77 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ow;. sqlite
1b871 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64 3OsCurrentTime(d
1b872 62 2d 3e 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b b->pVfs, &rNow);
1b873 0a 20 20 20 20 20 20 70 2d 3e 73 74 61 72 74 54 . p->startT
1b874 69 6d 65 20 3d 20 28 75 36 34 29 28 28 72 4e 6f ime = (u64)((rNo
1b875 77 20 2d 20 28 69 6e 74 29 72 4e 6f 77 29 2a 33 w - (int)rNow)*3
1b876 36 30 30 2e 30 2a 32 34 2e 30 2a 31 30 30 30 30 600.0*24.0*10000
1b877 30 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 7d 0a 00000.0);. }.
1b878 23 65 6e 64 69 66 0a 0a 20 20 20 20 64 62 2d 3e #endif.. db->
1b879 61 63 74 69 76 65 56 64 62 65 43 6e 74 2b 2b 3b activeVdbeCnt++;
1b87a 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 65 61 64 . if( p->read
1b87b 4f 6e 6c 79 3d 3d 30 20 29 20 64 62 2d 3e 77 72 Only==0 ) db->wr
1b87c 69 74 65 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 iteVdbeCnt++;.
1b87d 20 20 70 2d 3e 70 63 20 3d 20 30 3b 0a 20 20 7d p->pc = 0;. }
1b87e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1b87f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 69 OMIT_EXPLAIN. i
1b880 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 7b f( p->explain ){
1b881 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1b882 33 56 64 62 65 4c 69 73 74 28 70 29 3b 0a 20 20 3VdbeList(p);.
1b883 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 }else.#endif /*
1b884 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c SQLITE_OMIT_EXPL
1b885 41 49 4e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 72 AIN */. {. r
1b886 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 45 c = sqlite3VdbeE
1b887 78 65 63 28 70 29 3b 0a 20 20 7d 0a 0a 20 20 69 xec(p);. }.. i
1b888 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1b889 4f 66 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72 Off(db) ){. r
1b88a 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 c = SQLITE_MISUS
1b88b 45 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 E;. }..#ifndef
1b88c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 SQLITE_OMIT_TRAC
1b88d 45 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 E. /* Invoke th
1b88e 65 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 e profile callba
1b88f 63 6b 20 69 66 20 74 68 65 72 65 20 69 73 20 6f ck if there is o
1b890 6e 65 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 ne. */. if( rc
1b891 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 !=SQLITE_ROW &&
1b892 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26 20 db->xProfile &&
1b893 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 !db->init.busy &
1b894 26 20 70 2d 3e 7a 53 71 6c 20 29 7b 0a 20 20 20 & p->zSql ){.
1b895 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 double rNow;.
1b896 20 20 75 36 34 20 65 6c 61 70 73 65 54 69 6d 65 u64 elapseTime
1b897 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 ;.. sqlite3Os
1b898 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e CurrentTime(db->
1b899 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 pVfs, &rNow);.
1b89a 20 20 65 6c 61 70 73 65 54 69 6d 65 20 3d 20 28 elapseTime = (
1b89b 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 69 6e u64)((rNow - (in
1b89c 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a 32 t)rNow)*3600.0*2
1b89d 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e 30 4.0*1000000000.0
1b89e 29 3b 0a 20 20 20 20 65 6c 61 70 73 65 54 69 6d );. elapseTim
1b89f 65 20 2d 3d 20 70 2d 3e 73 74 61 72 74 54 69 6d e -= p->startTim
1b8a0 65 3b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 66 e;. db->xProf
1b8a1 69 6c 65 28 64 62 2d 3e 70 50 72 6f 66 69 6c 65 ile(db->pProfile
1b8a2 41 72 67 2c 20 70 2d 3e 7a 53 71 6c 2c 20 65 6c Arg, p->zSql, el
1b8a3 61 70 73 65 54 69 6d 65 29 3b 0a 20 20 7d 0a 23 apseTime);. }.#
1b8a4 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 65 72 72 endif.. db->err
1b8a5 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 69 66 28 Code = rc;. if(
1b8a6 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 SQLITE_NOMEM==s
1b8a7 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 2d qlite3ApiExit(p-
1b8a8 3e 64 62 2c 20 70 2d 3e 72 63 29 20 29 7b 0a 20 >db, p->rc) ){.
1b8a9 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 p->rc = SQLIT
1b8aa 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 65 6e 64 E_NOMEM;. }.end
1b8ab 5f 6f 66 5f 73 74 65 70 3a 0a 20 20 2f 2a 20 41 _of_step:. /* A
1b8ac 74 20 74 68 69 73 20 70 6f 69 6e 74 20 6c 6f 63 t this point loc
1b8ad 61 6c 20 76 61 72 69 61 62 6c 65 20 72 63 20 68 al variable rc h
1b8ae 6f 6c 64 73 20 74 68 65 20 76 61 6c 75 65 20 74 olds the value t
1b8af 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 0a 20 hat should be .
1b8b0 20 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 66 20 ** returned if
1b8b1 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 77 this statement w
1b8b2 61 73 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e as compiled usin
1b8b3 67 20 74 68 65 20 6c 65 67 61 63 79 20 0a 20 20 g the legacy .
1b8b4 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 ** sqlite3_prepa
1b8b5 72 65 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20 re() interface.
1b8b6 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 According to the
1b8b7 20 64 6f 63 73 2c 20 74 68 69 73 20 63 61 6e 20 docs, this can
1b8b8 6f 6e 6c 79 0a 20 20 2a 2a 20 62 65 20 6f 6e 65 only. ** be one
1b8b9 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 of the values i
1b8ba 6e 20 74 68 65 20 66 69 72 73 74 20 61 73 73 65 n the first asse
1b8bb 72 74 28 29 20 62 65 6c 6f 77 2e 20 56 61 72 69 rt() below. Vari
1b8bc 61 62 6c 65 20 70 2d 3e 72 63 20 0a 20 20 2a 2a able p->rc . **
1b8bd 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 61 contains the va
1b8be 6c 75 65 20 74 68 61 74 20 77 6f 75 6c 64 20 62 lue that would b
1b8bf 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 73 71 e returned if sq
1b8c0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 lite3_finalize()
1b8c1 20 0a 20 20 2a 2a 20 77 65 72 65 20 63 61 6c 6c . ** were call
1b8c2 65 64 20 6f 6e 20 73 74 61 74 65 6d 65 6e 74 20 ed on statement
1b8c3 70 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 p.. */. assert
1b8c4 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 ( rc==SQLITE_ROW
1b8c5 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f || rc==SQLITE_
1b8c6 44 4f 4e 45 20 20 20 7c 7c 20 72 63 3d 3d 53 51 DONE || rc==SQ
1b8c7 4c 49 54 45 5f 45 52 52 4f 52 20 0a 20 20 20 20 LITE_ERROR .
1b8c8 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 || rc==SQLITE
1b8c9 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d 53 51 4c _BUSY || rc==SQL
1b8ca 49 54 45 5f 4d 49 53 55 53 45 0a 20 20 29 3b 0a ITE_MISUSE. );.
1b8cb 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 21 assert( p->rc!
1b8cc 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 70 =SQLITE_ROW && p
1b8cd 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e ->rc!=SQLITE_DON
1b8ce 45 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 73 E );. if( p->is
1b8cf 50 72 65 70 61 72 65 56 32 20 26 26 20 72 63 21 PrepareV2 && rc!
1b8d0 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 72 =SQLITE_ROW && r
1b8d1 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 c!=SQLITE_DONE )
1b8d2 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 {. /* If this
1b8d3 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 70 statement was p
1b8d4 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 73 71 repared using sq
1b8d5 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
1b8d6 28 29 2c 20 61 6e 64 20 61 6e 0a 20 20 20 20 2a (), and an. *
1b8d7 2a 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 * error has occu
1b8d8 72 65 64 2c 20 74 68 65 6e 20 72 65 74 75 72 6e red, then return
1b8d9 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 the error code
1b8da 69 6e 20 70 2d 3e 72 63 20 74 6f 20 74 68 65 0a in p->rc to the.
1b8db 20 20 20 20 2a 2a 20 63 61 6c 6c 65 72 2e 20 53 ** caller. S
1b8dc 65 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 et the error cod
1b8dd 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 e in the databas
1b8de 65 20 68 61 6e 64 6c 65 20 74 6f 20 74 68 65 20 e handle to the
1b8df 73 61 6d 65 20 76 61 6c 75 65 2e 0a 20 20 20 20 same value..
1b8e0 2a 2f 20 0a 20 20 20 20 72 63 20 3d 20 64 62 2d */ . rc = db-
1b8e1 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63 >errCode = p->rc
1b8e2 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 ;. }. return (
1b8e3 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3b rc&db->errMask);
1b8e4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 .}../*.** This i
1b8e5 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 s the top-level
1b8e6 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
1b8e7 66 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 f sqlite3_step()
1b8e8 2e 20 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74 . Call.** sqlit
1b8e9 65 33 53 74 65 70 28 29 20 74 6f 20 64 6f 20 6d e3Step() to do m
1b8ea 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b 2e ost of the work.
1b8eb 20 20 49 66 20 61 20 73 63 68 65 6d 61 20 65 72 If a schema er
1b8ec 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 63 ror occurs,.** c
1b8ed 61 6c 6c 20 73 71 6c 69 74 65 33 52 65 70 72 65 all sqlite3Repre
1b8ee 70 61 72 65 28 29 20 61 6e 64 20 74 72 79 20 61 pare() and try a
1b8ef 67 61 69 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f gain..*/.SQLITE_
1b8f0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1b8f1 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d step(sqlite3_stm
1b8f2 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 t *pStmt){. int
1b8f3 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 rc = SQLITE_MIS
1b8f4 55 53 45 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 USE;. if( pStmt
1b8f5 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 ){. int cnt
1b8f6 3d 20 30 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 = 0;. Vdbe *v
1b8f7 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b = (Vdbe*)pStmt;
1b8f8 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 . sqlite3 *db
1b8f9 20 3d 20 76 2d 3e 64 62 3b 0a 20 20 20 20 73 71 = v->db;. sq
1b8fa 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
1b8fb 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 r(db->mutex);.
1b8fc 20 20 77 68 69 6c 65 28 20 28 72 63 20 3d 20 73 while( (rc = s
1b8fd 71 6c 69 74 65 33 53 74 65 70 28 76 29 29 3d 3d qlite3Step(v))==
1b8fe 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 0a 20 20 SQLITE_SCHEMA.
1b8ff 20 20 20 20 20 20 20 20 20 26 26 20 63 6e 74 2b && cnt+
1b900 2b 20 3c 20 35 0a 20 20 20 20 20 20 20 20 20 20 + < 5.
1b901 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 && (rc = sqlite
1b902 33 52 65 70 72 65 70 61 72 65 28 76 29 29 3d 3d 3Reprepare(v))==
1b903 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1b904 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 sqlite3_reset
1b905 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 76 (pStmt);. v
1b906 2d 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 20 ->expired = 0;.
1b907 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d }. if( rc=
1b908 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 26 =SQLITE_SCHEMA &
1b909 26 20 41 4c 57 41 59 53 28 76 2d 3e 69 73 50 72 & ALWAYS(v->isPr
1b90a 65 70 61 72 65 56 32 29 20 26 26 20 41 4c 57 41 epareV2) && ALWA
1b90b 59 53 28 64 62 2d 3e 70 45 72 72 29 20 29 7b 0a YS(db->pErr) ){.
1b90c 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 /* This ca
1b90d 73 65 20 6f 63 63 75 72 73 20 61 66 74 65 72 20 se occurs after
1b90e 66 61 69 6c 69 6e 67 20 74 6f 20 72 65 63 6f 6d failing to recom
1b90f 70 69 6c 65 20 61 6e 20 73 71 6c 20 73 74 61 74 pile an sql stat
1b910 65 6d 65 6e 74 2e 20 0a 20 20 20 20 20 20 2a 2a ement. . **
1b911 20 54 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 The error messa
1b912 67 65 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 20 ge from the SQL
1b913 63 6f 6d 70 69 6c 65 72 20 68 61 73 20 61 6c 72 compiler has alr
1b914 65 61 64 79 20 62 65 65 6e 20 6c 6f 61 64 65 64 eady been loaded
1b915 20 0a 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 . ** into
1b916 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e the database han
1b917 64 6c 65 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 dle. This block
1b918 63 6f 70 69 65 73 20 74 68 65 20 65 72 72 6f 72 copies the error
1b919 20 6d 65 73 73 61 67 65 20 0a 20 20 20 20 20 20 message .
1b91a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 ** from the data
1b91b 62 61 73 65 20 68 61 6e 64 6c 65 20 69 6e 74 6f base handle into
1b91c 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 61 the statement a
1b91d 6e 64 20 73 65 74 73 20 74 68 65 20 73 74 61 74 nd sets the stat
1b91e 65 6d 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20 70 ement. ** p
1b91f 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 74 rogram counter t
1b920 6f 20 30 20 74 6f 20 65 6e 73 75 72 65 20 74 68 o 0 to ensure th
1b921 61 74 20 77 68 65 6e 20 74 68 65 20 73 74 61 74 at when the stat
1b922 65 6d 65 6e 74 20 69 73 20 0a 20 20 20 20 20 20 ement is .
1b923 2a 2a 20 66 69 6e 61 6c 69 7a 65 64 20 6f 72 20 ** finalized or
1b924 72 65 73 65 74 20 74 68 65 20 70 61 72 73 65 72 reset the parser
1b925 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 error message i
1b926 73 20 61 76 61 69 6c 61 62 6c 65 20 76 69 61 0a s available via.
1b927 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 ** sqlite3
1b928 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71 _errmsg() and sq
1b929 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 2e lite3_errcode().
1b92a 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
1b92b 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72 const char *zErr
1b92c 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a = (const char *
1b92d 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 )sqlite3_value_t
1b92e 65 78 74 28 64 62 2d 3e 70 45 72 72 29 3b 20 0a ext(db->pErr); .
1b92f 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1b930 72 65 65 28 64 62 2c 20 76 2d 3e 7a 45 72 72 4d ree(db, v->zErrM
1b931 73 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 sg);. if( !
1b932 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1b933 20 29 7b 0a 20 20 20 20 20 20 20 20 76 2d 3e 7a ){. v->z
1b934 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 ErrMsg = sqlite3
1b935 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 45 72 DbStrDup(db, zEr
1b936 72 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 r);. } else
1b937 20 7b 0a 20 20 20 20 20 20 20 20 76 2d 3e 7a 45 {. v->zE
1b938 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 rrMsg = 0;.
1b939 20 20 20 76 2d 3e 72 63 20 3d 20 53 51 4c 49 54 v->rc = SQLIT
1b93a 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d E_NOMEM;. }
1b93b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
1b93c 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 sqlite3ApiExit(d
1b93d 62 2c 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 b, rc);. sqli
1b93e 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
1b93f 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a db->mutex);. }.
1b940 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1b941 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 /*.** Extract th
1b942 65 20 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d e user data from
1b943 20 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 a sqlite3_conte
1b944 78 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 xt structure and
1b945 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 return a.** poi
1b946 6e 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 nter to it..*/.S
1b947 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a QLITE_API void *
1b948 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 sqlite3_user_dat
1b949 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 a(sqlite3_contex
1b94a 74 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 t *p){. assert(
1b94b 20 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 29 p && p->pFunc )
1b94c 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 46 ;. return p->pF
1b94d 75 6e 63 2d 3e 70 55 73 65 72 44 61 74 61 3b 0a unc->pUserData;.
1b94e 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 }../*.** Extract
1b94f 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20 66 the user data f
1b950 72 6f 6d 20 61 20 73 71 6c 69 74 65 33 5f 63 6f rom a sqlite3_co
1b951 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65 20 ntext structure
1b952 61 6e 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 and return a.**
1b953 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 0a 2a pointer to it..*
1b954 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c /.SQLITE_API sql
1b955 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 63 6f ite3 *sqlite3_co
1b956 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 ntext_db_handle(
1b957 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1b958 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
1b959 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a && p->pFunc );.
1b95a 20 20 72 65 74 75 72 6e 20 70 2d 3e 73 2e 64 62 return p->s.db
1b95b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 ;.}../*.** The f
1b95c 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 68 65 20 ollowing is the
1b95d 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
1b95e 66 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f f an SQL functio
1b95f 6e 20 74 68 61 74 20 61 6c 77 61 79 73 0a 2a 2a n that always.**
1b960 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 20 65 fails with an e
1b961 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 61 rror message sta
1b962 74 69 6e 67 20 74 68 61 74 20 74 68 65 20 66 75 ting that the fu
1b963 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 nction is used i
1b964 6e 20 74 68 65 0a 2a 2a 20 77 72 6f 6e 67 20 63 n the.** wrong c
1b965 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 73 71 6c ontext. The sql
1b966 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 ite3_overload_fu
1b967 6e 63 74 69 6f 6e 28 29 20 41 50 49 20 6d 69 67 nction() API mig
1b968 68 74 20 63 6f 6e 73 74 72 75 63 74 0a 2a 2a 20 ht construct.**
1b969 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 SQL function tha
1b96a 74 20 75 73 65 20 74 68 69 73 20 72 6f 75 74 69 t use this routi
1b96b 6e 65 20 73 6f 20 74 68 61 74 20 74 68 65 20 66 ne so that the f
1b96c 75 6e 63 74 69 6f 6e 73 20 77 69 6c 6c 20 65 78 unctions will ex
1b96d 69 73 74 0a 2a 2a 20 66 6f 72 20 6e 61 6d 65 20 ist.** for name
1b96e 72 65 73 6f 6c 75 74 69 6f 6e 20 62 75 74 20 61 resolution but a
1b96f 72 65 20 61 63 74 75 61 6c 6c 79 20 6f 76 65 72 re actually over
1b970 6c 6f 61 64 65 64 20 62 79 20 74 68 65 20 78 46 loaded by the xF
1b971 69 6e 64 46 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d indFunction.** m
1b972 65 74 68 6f 64 20 6f 66 20 76 69 72 74 75 61 6c ethod of virtual
1b973 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 tables..*/.SQLI
1b974 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1b975 73 71 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 75 sqlite3InvalidFu
1b976 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 nction(. sqlite
1b977 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
1b978 78 74 2c 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 xt, /* The func
1b979 74 69 6f 6e 20 63 61 6c 6c 69 6e 67 20 63 6f 6e tion calling con
1b97a 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 4e 6f text */. int No
1b97b 74 55 73 65 64 2c 20 20 20 20 20 20 20 20 20 20 tUsed,
1b97c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1b97d 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 f arguments to t
1b97e 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 he function */.
1b97f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
1b980 2a 4e 6f 74 55 73 65 64 32 20 20 20 2f 2a 20 56 *NotUsed2 /* V
1b981 61 6c 75 65 20 6f 66 20 65 61 63 68 20 61 72 67 alue of each arg
1b982 75 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 63 6f ument */.){. co
1b983 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 nst char *zName
1b984 3d 20 63 6f 6e 74 65 78 74 2d 3e 70 46 75 6e 63 = context->pFunc
1b985 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 63 68 61 72 20 ->zName;. char
1b986 2a 7a 45 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f *zErr;. UNUSED_
1b987 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 PARAMETER2(NotUs
1b988 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 ed, NotUsed2);.
1b989 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f zErr = sqlite3_
1b98a 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 20 20 22 mprintf(. "
1b98b 75 6e 61 62 6c 65 20 74 6f 20 75 73 65 20 66 75 unable to use fu
1b98c 6e 63 74 69 6f 6e 20 25 73 20 69 6e 20 74 68 65 nction %s in the
1b98d 20 72 65 71 75 65 73 74 65 64 20 63 6f 6e 74 65 requested conte
1b98e 78 74 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 xt", zName);. s
1b98f 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
1b990 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 ror(context, zEr
1b991 72 2c 20 2d 31 29 3b 0a 20 20 73 71 6c 69 74 65 r, -1);. sqlite
1b992 33 5f 66 72 65 65 28 7a 45 72 72 29 3b 0a 7d 0a 3_free(zErr);.}.
1b993 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 ./*.** Allocate
1b994 6f 72 20 72 65 74 75 72 6e 20 74 68 65 20 61 67 or return the ag
1b995 67 72 65 67 61 74 65 20 63 6f 6e 74 65 78 74 20 gregate context
1b996 66 6f 72 20 61 20 75 73 65 72 20 66 75 6e 63 74 for a user funct
1b997 69 6f 6e 2e 20 20 41 20 6e 65 77 0a 2a 2a 20 63 ion. A new.** c
1b998 6f 6e 74 65 78 74 20 69 73 20 61 6c 6c 6f 63 61 ontext is alloca
1b999 74 65 64 20 6f 6e 20 74 68 65 20 66 69 72 73 74 ted on the first
1b99a 20 63 61 6c 6c 2e 20 20 53 75 62 73 65 71 75 65 call. Subseque
1b99b 6e 74 20 63 61 6c 6c 73 20 72 65 74 75 72 6e 20 nt calls return
1b99c 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63 6f 6e 74 the.** same cont
1b99d 65 78 74 20 74 68 61 74 20 77 61 73 20 72 65 74 ext that was ret
1b99e 75 72 6e 65 64 20 6f 6e 20 70 72 69 6f 72 20 63 urned on prior c
1b99f 61 6c 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f alls..*/.SQLITE_
1b9a0 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 API void *sqlite
1b9a1 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 3_aggregate_cont
1b9a2 65 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ext(sqlite3_cont
1b9a3 65 78 74 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 ext *p, int nByt
1b9a4 65 29 7b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b e){. Mem *pMem;
1b9a5 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20 . assert( p &&
1b9a6 70 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 p->pFunc && p->p
1b9a7 46 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 Func->xStep );.
1b9a8 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1b9a9 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 73 _mutex_held(p->s
1b9aa 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1b9ab 20 70 4d 65 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b pMem = p->pMem;
1b9ac 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c . if( (pMem->fl
1b9ad 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d ags & MEM_Agg)==
1b9ae 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 42 79 0 ){. if( nBy
1b9af 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 te==0 ){. s
1b9b0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1b9b1 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 4d 65 easeExternal(pMe
1b9b2 6d 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e m);. pMem->
1b9b3 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1b9b4 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 ;. pMem->z
1b9b5 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a = 0;. }else{.
1b9b6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1b9b7 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e eMemGrow(pMem, n
1b9b8 42 79 74 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 Byte, 0);.
1b9b9 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
1b9ba 4d 5f 41 67 67 3b 0a 20 20 20 20 20 20 70 4d 65 M_Agg;. pMe
1b9bb 6d 2d 3e 75 2e 70 44 65 66 20 3d 20 70 2d 3e 70 m->u.pDef = p->p
1b9bc 46 75 6e 63 3b 0a 20 20 20 20 20 20 69 66 28 20 Func;. if(
1b9bd 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 pMem->z ){.
1b9be 20 20 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e memset(pMem->
1b9bf 7a 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 z, 0, nByte);.
1b9c0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1b9c1 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 return (void*)
1b9c2 70 4d 65 6d 2d 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a pMem->z;.}../*.*
1b9c3 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61 75 78 * Return the aux
1b9c4 69 6c 61 72 79 20 64 61 74 61 20 70 6f 69 6e 74 ilary data point
1b9c5 65 72 2c 20 69 66 20 61 6e 79 2c 20 66 6f 72 20 er, if any, for
1b9c6 74 68 65 20 69 41 72 67 27 74 68 20 61 72 67 75 the iArg'th argu
1b9c7 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 75 ment to.** the u
1b9c8 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 ser-function def
1b9c9 69 6e 65 64 20 62 79 20 70 43 74 78 2e 0a 2a 2f ined by pCtx..*/
1b9ca 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
1b9cb 20 2a 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 *sqlite3_get_au
1b9cc 78 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f xdata(sqlite3_co
1b9cd 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 ntext *pCtx, int
1b9ce 20 69 41 72 67 29 7b 0a 20 20 56 64 62 65 46 75 iArg){. VdbeFu
1b9cf 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 0a 0a nc *pVdbeFunc;..
1b9d0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1b9d1 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
1b9d2 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
1b9d3 29 3b 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d );. pVdbeFunc =
1b9d4 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 pCtx->pVdbeFunc
1b9d5 3b 0a 20 20 69 66 28 20 21 70 56 64 62 65 46 75 ;. if( !pVdbeFu
1b9d6 6e 63 20 7c 7c 20 69 41 72 67 3e 3d 70 56 64 62 nc || iArg>=pVdb
1b9d7 65 46 75 6e 63 2d 3e 6e 41 75 78 20 7c 7c 20 69 eFunc->nAux || i
1b9d8 41 72 67 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 Arg<0 ){. ret
1b9d9 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 urn 0;. }. ret
1b9da 75 72 6e 20 70 56 64 62 65 46 75 6e 63 2d 3e 61 urn pVdbeFunc->a
1b9db 70 41 75 78 5b 69 41 72 67 5d 2e 70 41 75 78 3b pAux[iArg].pAux;
1b9dc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 .}../*.** Set th
1b9dd 65 20 61 75 78 69 6c 61 72 79 20 64 61 74 61 20 e auxilary data
1b9de 70 6f 69 6e 74 65 72 20 61 6e 64 20 64 65 6c 65 pointer and dele
1b9df 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 66 6f 72 te function, for
1b9e0 20 74 68 65 20 69 41 72 67 27 74 68 0a 2a 2a 20 the iArg'th.**
1b9e1 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
1b9e2 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65 user-function de
1b9e3 66 69 6e 65 64 20 62 79 20 70 43 74 78 2e 20 41 fined by pCtx. A
1b9e4 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 ny previous valu
1b9e5 65 20 69 73 0a 2a 2a 20 64 65 6c 65 74 65 64 20 e is.** deleted
1b9e6 62 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 64 by calling the d
1b9e7 65 6c 65 74 65 20 66 75 6e 63 74 69 6f 6e 20 73 elete function s
1b9e8 70 65 63 69 66 69 65 64 20 77 68 65 6e 20 69 74 pecified when it
1b9e9 20 77 61 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c was set..*/.SQL
1b9ea 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
1b9eb 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 ite3_set_auxdata
1b9ec 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
1b9ed 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 69 6e ext *pCtx, . in
1b9ee 74 20 69 41 72 67 2c 20 0a 20 20 76 6f 69 64 20 t iArg, . void
1b9ef 2a 70 41 75 78 2c 20 0a 20 20 76 6f 69 64 20 28 *pAux, . void (
1b9f0 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64 2a 29 *xDelete)(void*)
1b9f1 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 41 75 78 .){. struct Aux
1b9f2 44 61 74 61 20 2a 70 41 75 78 44 61 74 61 3b 0a Data *pAuxData;.
1b9f3 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 VdbeFunc *pVdb
1b9f4 65 46 75 6e 63 3b 0a 20 20 69 66 28 20 69 41 72 eFunc;. if( iAr
1b9f5 67 3c 30 20 29 20 67 6f 74 6f 20 66 61 69 6c 65 g<0 ) goto faile
1b9f6 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 d;.. assert( sq
1b9f7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1b9f8 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 (pCtx->s.db->mut
1b9f9 65 78 29 20 29 3b 0a 20 20 70 56 64 62 65 46 75 ex) );. pVdbeFu
1b9fa 6e 63 20 3d 20 70 43 74 78 2d 3e 70 56 64 62 65 nc = pCtx->pVdbe
1b9fb 46 75 6e 63 3b 0a 20 20 69 66 28 20 21 70 56 64 Func;. if( !pVd
1b9fc 62 65 46 75 6e 63 20 7c 7c 20 70 56 64 62 65 46 beFunc || pVdbeF
1b9fd 75 6e 63 2d 3e 6e 41 75 78 3c 3d 69 41 72 67 20 unc->nAux<=iArg
1b9fe 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 75 78 20 ){. int nAux
1b9ff 3d 20 28 70 56 64 62 65 46 75 6e 63 20 3f 20 70 = (pVdbeFunc ? p
1ba00 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 20 3a VdbeFunc->nAux :
1ba01 20 30 29 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 0);. int nMa
1ba02 6c 6c 6f 63 20 3d 20 73 69 7a 65 6f 66 28 56 64 lloc = sizeof(Vd
1ba03 62 65 46 75 6e 63 29 20 2b 20 73 69 7a 65 6f 66 beFunc) + sizeof
1ba04 28 73 74 72 75 63 74 20 41 75 78 44 61 74 61 29 (struct AuxData)
1ba05 2a 69 41 72 67 3b 0a 20 20 20 20 70 56 64 62 65 *iArg;. pVdbe
1ba06 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 44 62 Func = sqlite3Db
1ba07 52 65 61 6c 6c 6f 63 28 70 43 74 78 2d 3e 73 2e Realloc(pCtx->s.
1ba08 64 62 2c 20 70 56 64 62 65 46 75 6e 63 2c 20 6e db, pVdbeFunc, n
1ba09 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 69 66 28 Malloc);. if(
1ba0a 20 21 70 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 !pVdbeFunc ){.
1ba0b 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 goto failed
1ba0c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 74 78 ;. }. pCtx
1ba0d 2d 3e 70 56 64 62 65 46 75 6e 63 20 3d 20 70 56 ->pVdbeFunc = pV
1ba0e 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 6d 65 6d dbeFunc;. mem
1ba0f 73 65 74 28 26 70 56 64 62 65 46 75 6e 63 2d 3e set(&pVdbeFunc->
1ba10 61 70 41 75 78 5b 6e 41 75 78 5d 2c 20 30 2c 20 apAux[nAux], 0,
1ba11 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 41 75 sizeof(struct Au
1ba12 78 44 61 74 61 29 2a 28 69 41 72 67 2b 31 2d 6e xData)*(iArg+1-n
1ba13 41 75 78 29 29 3b 0a 20 20 20 20 70 56 64 62 65 Aux));. pVdbe
1ba14 46 75 6e 63 2d 3e 6e 41 75 78 20 3d 20 69 41 72 Func->nAux = iAr
1ba15 67 2b 31 3b 0a 20 20 20 20 70 56 64 62 65 46 75 g+1;. pVdbeFu
1ba16 6e 63 2d 3e 70 46 75 6e 63 20 3d 20 70 43 74 78 nc->pFunc = pCtx
1ba17 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 ->pFunc;. }..
1ba18 70 41 75 78 44 61 74 61 20 3d 20 26 70 56 64 62 pAuxData = &pVdb
1ba19 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72 eFunc->apAux[iAr
1ba1a 67 5d 3b 0a 20 20 69 66 28 20 70 41 75 78 44 61 g];. if( pAuxDa
1ba1b 74 61 2d 3e 70 41 75 78 20 26 26 20 70 41 75 78 ta->pAux && pAux
1ba1c 44 61 74 61 2d 3e 78 44 65 6c 65 74 65 20 29 7b Data->xDelete ){
1ba1d 0a 20 20 20 20 70 41 75 78 44 61 74 61 2d 3e 78 . pAuxData->x
1ba1e 44 65 6c 65 74 65 28 70 41 75 78 44 61 74 61 2d Delete(pAuxData-
1ba1f 3e 70 41 75 78 29 3b 0a 20 20 7d 0a 20 20 70 41 >pAux);. }. pA
1ba20 75 78 44 61 74 61 2d 3e 70 41 75 78 20 3d 20 70 uxData->pAux = p
1ba21 41 75 78 3b 0a 20 20 70 41 75 78 44 61 74 61 2d Aux;. pAuxData-
1ba22 3e 78 44 65 6c 65 74 65 20 3d 20 78 44 65 6c 65 >xDelete = xDele
1ba23 74 65 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 66 te;. return;..f
1ba24 61 69 6c 65 64 3a 0a 20 20 69 66 28 20 78 44 65 ailed:. if( xDe
1ba25 6c 65 74 65 20 29 7b 0a 20 20 20 20 78 44 65 6c lete ){. xDel
1ba26 65 74 65 28 70 41 75 78 29 3b 0a 20 20 7d 0a 7d ete(pAux);. }.}
1ba27 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1ba28 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 _OMIT_DEPRECATED
1ba29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
1ba2a 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 e number of time
1ba2b 73 20 74 68 65 20 53 74 65 70 20 66 75 6e 63 74 s the Step funct
1ba2c 69 6f 6e 20 6f 66 20 61 20 61 67 67 72 65 67 61 ion of a aggrega
1ba2d 74 65 20 68 61 73 20 62 65 65 6e 20 0a 2a 2a 20 te has been .**
1ba2e 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 called..**.** Th
1ba2f 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 is function is d
1ba30 65 70 72 65 63 61 74 65 64 2e 20 20 44 6f 20 6e eprecated. Do n
1ba31 6f 74 20 75 73 65 20 69 74 20 66 6f 72 20 6e 65 ot use it for ne
1ba32 77 20 63 6f 64 65 2e 20 20 49 74 20 69 73 0a 2a w code. It is.*
1ba33 2a 20 70 72 6f 76 69 64 65 20 6f 6e 6c 79 20 74 * provide only t
1ba34 6f 20 61 76 6f 69 64 20 62 72 65 61 6b 69 6e 67 o avoid breaking
1ba35 20 6c 65 67 61 63 79 20 63 6f 64 65 2e 20 20 4e legacy code. N
1ba36 65 77 20 61 67 67 72 65 67 61 74 65 20 66 75 6e ew aggregate fun
1ba37 63 74 69 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 ction.** impleme
1ba38 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 ntations should
1ba39 6b 65 65 70 20 74 68 65 69 72 20 6f 77 6e 20 63 keep their own c
1ba3a 6f 75 6e 74 73 20 77 69 74 68 69 6e 20 74 68 65 ounts within the
1ba3b 69 72 20 61 67 67 72 65 67 61 74 65 0a 2a 2a 20 ir aggregate.**
1ba3c 63 6f 6e 74 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 context..*/.SQLI
1ba3d 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1ba3e 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75 e3_aggregate_cou
1ba3f 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 nt(sqlite3_conte
1ba40 78 74 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 xt *p){. assert
1ba41 28 20 70 20 26 26 20 70 2d 3e 70 4d 65 6d 20 26 ( p && p->pMem &
1ba42 26 20 70 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d & p->pFunc && p-
1ba43 3e 70 46 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b >pFunc->xStep );
1ba44 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 4d 65 . return p->pMe
1ba45 6d 2d 3e 6e 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a m->n;.}.#endif..
1ba46 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
1ba47 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
1ba48 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 ns in the result
1ba49 20 73 65 74 20 66 6f 72 20 74 68 65 20 73 74 61 set for the sta
1ba4a 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f tement pStmt..*/
1ba4b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1ba4c 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 sqlite3_column_c
1ba4d 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d ount(sqlite3_stm
1ba4e 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 t *pStmt){. Vdb
1ba4f 65 20 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a e *pVm = (Vdbe *
1ba50 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e )pStmt;. return
1ba51 20 70 56 6d 20 3f 20 70 56 6d 2d 3e 6e 52 65 73 pVm ? pVm->nRes
1ba52 43 6f 6c 75 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f Column : 0;.}../
1ba53 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1ba54 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 number of values
1ba55 20 61 76 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20 available from
1ba56 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 the current row
1ba57 6f 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e of the.** curren
1ba58 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 73 74 tly executing st
1ba59 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a atement pStmt..*
1ba5a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1ba5b 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f sqlite3_data_co
1ba5c 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 unt(sqlite3_stmt
1ba5d 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 *pStmt){. Vdbe
1ba5e 20 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 *pVm = (Vdbe *)
1ba5f 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d pStmt;. if( pVm
1ba60 3d 3d 30 20 7c 7c 20 70 56 6d 2d 3e 70 52 65 73 ==0 || pVm->pRes
1ba61 75 6c 74 53 65 74 3d 3d 30 20 29 20 72 65 74 75 ultSet==0 ) retu
1ba62 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 rn 0;. return p
1ba63 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a Vm->nResColumn;.
1ba64 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 }.../*.** Check
1ba65 74 6f 20 73 65 65 20 69 66 20 63 6f 6c 75 6d 6e to see if column
1ba66 20 69 43 6f 6c 20 6f 66 20 74 68 65 20 67 69 76 iCol of the giv
1ba67 65 6e 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 en statement is
1ba68 76 61 6c 69 64 2e 20 20 49 66 0a 2a 2a 20 69 74 valid. If.** it
1ba69 20 69 73 2c 20 72 65 74 75 72 6e 20 61 20 70 6f is, return a po
1ba6a 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4d 65 6d inter to the Mem
1ba6b 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20 6f for the value o
1ba6c 66 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 2a f that column..*
1ba6d 2a 20 49 66 20 69 43 6f 6c 20 69 73 20 6e 6f 74 * If iCol is not
1ba6e 20 76 61 6c 69 64 2c 20 72 65 74 75 72 6e 20 61 valid, return a
1ba6f 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 pointer to a Me
1ba70 6d 20 77 68 69 63 68 20 68 61 73 20 61 20 76 61 m which has a va
1ba71 6c 75 65 0a 2a 2a 20 6f 66 20 4e 55 4c 4c 2e 0a lue.** of NULL..
1ba72 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 20 2a 63 */.static Mem *c
1ba73 6f 6c 75 6d 6e 4d 65 6d 28 73 71 6c 69 74 65 33 olumnMem(sqlite3
1ba74 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1ba75 74 20 69 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 t i){. Vdbe *pV
1ba76 6d 3b 0a 20 20 69 6e 74 20 76 61 6c 73 3b 0a 20 m;. int vals;.
1ba77 20 4d 65 6d 20 2a 70 4f 75 74 3b 0a 0a 20 20 70 Mem *pOut;.. p
1ba78 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 Vm = (Vdbe *)pSt
1ba79 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d 20 26 26 mt;. if( pVm &&
1ba7a 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 pVm->pResultSet
1ba7b 21 3d 30 20 26 26 20 69 3c 70 56 6d 2d 3e 6e 52 !=0 && i<pVm->nR
1ba7c 65 73 43 6f 6c 75 6d 6e 20 26 26 20 69 3e 3d 30 esColumn && i>=0
1ba7d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
1ba7e 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d mutex_enter(pVm-
1ba7f 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 >db->mutex);.
1ba80 20 76 61 6c 73 20 3d 20 73 71 6c 69 74 65 33 5f vals = sqlite3_
1ba81 64 61 74 61 5f 63 6f 75 6e 74 28 70 53 74 6d 74 data_count(pStmt
1ba82 29 3b 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 70 );. pOut = &p
1ba83 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 5b 69 Vm->pResultSet[i
1ba84 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 ];. }else{.
1ba85 2f 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 /* If the value
1ba86 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 passed as the se
1ba87 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 cond argument is
1ba88 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 72 out of range, r
1ba89 65 74 75 72 6e 0a 20 20 20 20 2a 2a 20 61 20 70 eturn. ** a p
1ba8a 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 6f ointer to the fo
1ba8b 6c 6c 6f 77 69 6e 67 20 73 74 61 74 69 63 20 4d llowing static M
1ba8c 65 6d 20 6f 62 6a 65 63 74 20 77 68 69 63 68 20 em object which
1ba8d 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 20 20 20 contains the.
1ba8e 20 2a 2a 20 76 61 6c 75 65 20 53 51 4c 20 4e 55 ** value SQL NU
1ba8f 4c 4c 2e 20 45 76 65 6e 20 74 68 6f 75 67 68 20 LL. Even though
1ba90 74 68 65 20 4d 65 6d 20 73 74 72 75 63 74 75 72 the Mem structur
1ba91 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6c e contains an el
1ba92 65 6d 65 6e 74 0a 20 20 20 20 2a 2a 20 6f 66 20 ement. ** of
1ba93 74 79 70 65 20 69 36 34 2c 20 6f 6e 20 63 65 72 type i64, on cer
1ba94 74 61 69 6e 20 61 72 63 68 69 74 65 63 74 75 72 tain architectur
1ba95 65 20 28 78 38 36 29 20 77 69 74 68 20 63 65 72 e (x86) with cer
1ba96 74 61 69 6e 20 63 6f 6d 70 69 6c 65 72 0a 20 20 tain compiler.
1ba97 20 20 2a 2a 20 73 77 69 74 63 68 65 73 20 28 2d ** switches (-
1ba98 4f 73 29 2c 20 67 63 63 20 6d 61 79 20 61 6c 69 Os), gcc may ali
1ba99 67 6e 20 74 68 69 73 20 4d 65 6d 20 6f 62 6a 65 gn this Mem obje
1ba9a 63 74 20 6f 6e 20 61 20 34 2d 62 79 74 65 20 62 ct on a 4-byte b
1ba9b 6f 75 6e 64 61 72 79 0a 20 20 20 20 2a 2a 20 69 oundary. ** i
1ba9c 6e 73 74 65 61 64 20 6f 66 20 61 6e 20 38 2d 62 nstead of an 8-b
1ba9d 79 74 65 20 6f 6e 65 2e 20 54 68 69 73 20 61 6c yte one. This al
1ba9e 6c 20 77 6f 72 6b 73 20 66 69 6e 65 2c 20 65 78 l works fine, ex
1ba9f 63 65 70 74 20 74 68 61 74 20 77 68 65 6e 0a 20 cept that when.
1baa0 20 20 20 2a 2a 20 72 75 6e 6e 69 6e 67 20 77 69 ** running wi
1baa1 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 th SQLITE_DEBUG
1baa2 64 65 66 69 6e 65 64 20 74 68 65 20 53 51 4c 69 defined the SQLi
1baa3 74 65 20 63 6f 64 65 20 73 6f 6d 65 74 69 6d 65 te code sometime
1baa4 73 20 61 73 73 65 72 74 28 29 73 0a 20 20 20 20 s assert()s.
1baa5 2a 2a 20 74 68 61 74 20 61 20 4d 65 6d 20 73 74 ** that a Mem st
1baa6 72 75 63 74 75 72 65 20 69 73 20 6c 6f 63 61 74 ructure is locat
1baa7 65 64 20 6f 6e 20 61 6e 20 38 2d 62 79 74 65 20 ed on an 8-byte
1baa8 62 6f 75 6e 64 61 72 79 2e 20 54 6f 20 70 72 65 boundary. To pre
1baa9 76 65 6e 74 0a 20 20 20 20 2a 2a 20 74 68 69 73 vent. ** this
1baaa 20 61 73 73 65 72 74 28 29 20 66 72 6f 6d 20 66 assert() from f
1baab 61 69 6c 69 6e 67 2c 20 77 68 65 6e 20 62 75 69 ailing, when bui
1baac 6c 64 69 6e 67 20 77 69 74 68 20 53 51 4c 49 54 lding with SQLIT
1baad 45 5f 44 45 42 55 47 20 64 65 66 69 6e 65 64 0a E_DEBUG defined.
1baae 20 20 20 20 2a 2a 20 75 73 69 6e 67 20 67 63 63 ** using gcc
1baaf 2c 20 66 6f 72 63 65 20 6e 75 6c 6c 4d 65 6d 20 , force nullMem
1bab0 74 6f 20 62 65 20 38 2d 62 79 74 65 20 61 6c 69 to be 8-byte ali
1bab1 67 6e 65 64 20 75 73 69 6e 67 20 74 68 65 20 6d gned using the m
1bab2 61 67 69 63 61 6c 0a 20 20 20 20 2a 2a 20 5f 5f agical. ** __
1bab3 61 74 74 72 69 62 75 74 65 5f 5f 28 28 61 6c 69 attribute__((ali
1bab4 67 6e 65 64 28 38 29 29 29 20 6d 61 63 72 6f 2e gned(8))) macro.
1bab5 20 20 2a 2f 0a 20 20 20 20 73 74 61 74 69 63 20 */. static
1bab6 63 6f 6e 73 74 20 4d 65 6d 20 6e 75 6c 6c 4d 65 const Mem nullMe
1bab7 6d 20 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 m .#if defined(S
1bab8 51 4c 49 54 45 5f 44 45 42 55 47 29 20 26 26 20 QLITE_DEBUG) &&
1bab9 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f defined(__GNUC__
1baba 29 0a 20 20 20 20 20 20 5f 5f 61 74 74 72 69 62 ). __attrib
1babb 75 74 65 5f 5f 28 28 61 6c 69 67 6e 65 64 28 38 ute__((aligned(8
1babc 29 29 29 20 0a 23 65 6e 64 69 66 0a 20 20 20 20 ))) .#endif.
1babd 20 20 3d 20 7b 7b 30 7d 2c 20 28 64 6f 75 62 6c = {{0}, (doubl
1babe 65 29 30 2c 20 30 2c 20 22 22 2c 20 30 2c 20 4d e)0, 0, "", 0, M
1babf 45 4d 5f 4e 75 6c 6c 2c 20 53 51 4c 49 54 45 5f EM_Null, SQLITE_
1bac0 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 20 7d 3b NULL, 0, 0, 0 };
1bac1 0a 0a 20 20 20 20 69 66 28 20 70 56 6d 20 26 26 .. if( pVm &&
1bac2 20 41 4c 57 41 59 53 28 70 56 6d 2d 3e 64 62 29 ALWAYS(pVm->db)
1bac3 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1bac4 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 56 3_mutex_enter(pV
1bac5 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 m->db->mutex);.
1bac6 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
1bac7 72 28 70 56 6d 2d 3e 64 62 2c 20 53 51 4c 49 54 r(pVm->db, SQLIT
1bac8 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20 E_RANGE, 0);.
1bac9 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20 28 4d }. pOut = (M
1baca 65 6d 2a 29 26 6e 75 6c 6c 4d 65 6d 3b 0a 20 20 em*)&nullMem;.
1bacb 7d 0a 20 20 72 65 74 75 72 6e 20 70 4f 75 74 3b }. return pOut;
1bacc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
1bacd 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
1bace 64 20 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e 67 d after invoking
1bacf 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 an sqlite3_valu
1bad0 65 5f 58 58 58 20 66 75 6e 63 74 69 6f 6e 20 6f e_XXX function o
1bad1 6e 20 61 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 76 n a .** column v
1bad2 61 6c 75 65 20 28 69 2e 65 2e 20 61 20 76 61 6c alue (i.e. a val
1bad3 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 65 ue returned by e
1bad4 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 53 51 4c valuating an SQL
1bad5 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 expression in t
1bad6 68 65 0a 2a 2a 20 73 65 6c 65 63 74 20 6c 69 73 he.** select lis
1bad7 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 t of a SELECT st
1bad8 61 74 65 6d 65 6e 74 29 20 74 68 61 74 20 6d 61 atement) that ma
1bad9 79 20 63 61 75 73 65 20 61 20 6d 61 6c 6c 6f 63 y cause a malloc
1bada 28 29 20 66 61 69 6c 75 72 65 2e 20 49 66 20 0a () failure. If .
1badb 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 ** malloc() has
1badc 66 61 69 6c 65 64 2c 20 74 68 65 20 74 68 72 65 failed, the thre
1badd 61 64 73 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 ads mallocFailed
1bade 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65 64 flag is cleared
1badf 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 0a and the result.
1bae0 2a 2a 20 63 6f 64 65 20 6f 66 20 73 74 61 74 65 ** code of state
1bae1 6d 65 6e 74 20 70 53 74 6d 74 20 73 65 74 20 74 ment pStmt set t
1bae2 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a o SQLITE_NOMEM..
1bae3 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 69 63 61 6c **.** Specifical
1bae4 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c ly, this is call
1bae5 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 3a 0a ed from within:.
1bae6 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 **.** sqlite
1bae7 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 29 0a 2a 3_column_int().*
1bae8 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f * sqlite3_co
1bae9 6c 75 6d 6e 5f 69 6e 74 36 34 28 29 0a 2a 2a 20 lumn_int64().**
1baea 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 sqlite3_colu
1baeb 6d 6e 5f 74 65 78 74 28 29 0a 2a 2a 20 20 20 20 mn_text().**
1baec 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1baed 74 65 78 74 31 36 28 29 0a 2a 2a 20 20 20 20 20 text16().**
1baee 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 72 sqlite3_column_r
1baef 65 61 6c 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c eal().** sql
1baf0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
1baf1 73 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 s().** sqlit
1baf2 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 e3_column_bytes1
1baf3 36 28 29 0a 2a 2a 0a 2a 2a 20 42 75 74 20 6e 6f 6().**.** But no
1baf4 74 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f t for sqlite3_co
1baf5 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 77 68 69 lumn_blob(), whi
1baf6 63 68 20 6e 65 76 65 72 20 63 61 6c 6c 73 20 6d ch never calls m
1baf7 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 alloc()..*/.stat
1baf8 69 63 20 76 6f 69 64 20 63 6f 6c 75 6d 6e 4d 61 ic void columnMa
1baf9 6c 6c 6f 63 46 61 69 6c 75 72 65 28 73 71 6c 69 llocFailure(sqli
1bafa 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 te3_stmt *pStmt)
1bafb 0a 7b 0a 20 20 2f 2a 20 49 66 20 6d 61 6c 6c 6f .{. /* If mallo
1bafc 63 28 29 20 66 61 69 6c 65 64 20 64 75 72 69 6e c() failed durin
1bafd 67 20 61 6e 20 65 6e 63 6f 64 69 6e 67 20 63 6f g an encoding co
1bafe 6e 76 65 72 73 69 6f 6e 20 77 69 74 68 69 6e 20 nversion within
1baff 61 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f an. ** sqlite3_
1bb00 63 6f 6c 75 6d 6e 5f 58 58 58 20 41 50 49 2c 20 column_XXX API,
1bb01 74 68 65 6e 20 73 65 74 20 74 68 65 20 72 65 74 then set the ret
1bb02 75 72 6e 20 63 6f 64 65 20 6f 66 20 74 68 65 20 urn code of the
1bb03 73 74 61 74 65 6d 65 6e 74 20 74 6f 0a 20 20 2a statement to. *
1bb04 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 * SQLITE_NOMEM.
1bb05 54 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f The next call to
1bb06 20 5f 73 74 65 70 28 29 20 28 69 66 20 61 6e 79 _step() (if any
1bb07 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51 ) will return SQ
1bb08 4c 49 54 45 5f 45 52 52 4f 52 0a 20 20 2a 2a 20 LITE_ERROR. **
1bb09 61 6e 64 20 5f 66 69 6e 61 6c 69 7a 65 28 29 20 and _finalize()
1bb0a 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e 4f 4d 45 will return NOME
1bb0b 4d 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 20 2a M.. */. Vdbe *
1bb0c 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d p = (Vdbe *)pStm
1bb0d 74 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 t;. if( p ){.
1bb0e 20 20 70 2d 3e 72 63 20 3d 20 73 71 6c 69 74 65 p->rc = sqlite
1bb0f 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 3ApiExit(p->db,
1bb10 70 2d 3e 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 p->rc);. sqli
1bb11 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
1bb12 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 p->db->mutex);.
1bb13 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a }.}../*********
1bb14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bb15 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 *** sqlite3_colu
1bb16 6d 6e 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a mn_ ***********
1bb17 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bb18 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ****.** The foll
1bb19 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 owing routines a
1bb1a 72 65 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 re used to acces
1bb1b 73 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 s elements of th
1bb1c 65 20 63 75 72 72 65 6e 74 20 72 6f 77 0a 2a 2a e current row.**
1bb1d 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
1bb1e 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 et..*/.SQLITE_AP
1bb1f 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
1bb20 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f lite3_column_blo
1bb21 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a b(sqlite3_stmt *
1bb22 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 pStmt, int i){.
1bb23 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c const void *val
1bb24 3b 0a 20 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 ;. val = sqlite
1bb25 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 20 63 6f 3_value_blob( co
1bb26 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 lumnMem(pStmt,i)
1bb27 20 29 3b 0a 20 20 2f 2a 20 45 76 65 6e 20 74 68 );. /* Even th
1bb28 6f 75 67 68 20 74 68 65 72 65 20 69 73 20 6e 6f ough there is no
1bb29 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72 encoding conver
1bb2a 73 69 6f 6e 2c 20 76 61 6c 75 65 5f 62 6c 6f 62 sion, value_blob
1bb2b 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 6e 65 () might. ** ne
1bb2c 65 64 20 74 6f 20 63 61 6c 6c 20 6d 61 6c 6c 6f ed to call mallo
1bb2d 63 28 29 20 74 6f 20 65 78 70 61 6e 64 20 74 68 c() to expand th
1bb2e 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 7a 65 e result of a ze
1bb2f 72 6f 62 6c 6f 62 28 29 20 0a 20 20 2a 2a 20 65 roblob() . ** e
1bb30 78 70 72 65 73 73 69 6f 6e 2e 20 0a 20 20 2a 2f xpression. . */
1bb31 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 . columnMallocF
1bb32 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 ailure(pStmt);.
1bb33 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 return val;.}.S
1bb34 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1bb35 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
1bb36 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 es(sqlite3_stmt
1bb37 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a *pStmt, int i){.
1bb38 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 int val = sqli
1bb39 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 te3_value_bytes(
1bb3a 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 columnMem(pStmt
1bb3b 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d ,i) );. columnM
1bb3c 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 allocFailure(pSt
1bb3d 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 mt);. return va
1bb3e 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 l;.}.SQLITE_API
1bb3f 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 int sqlite3_colu
1bb40 6d 6e 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 mn_bytes16(sqlit
1bb41 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1bb42 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 int i){. int va
1bb43 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 l = sqlite3_valu
1bb44 65 5f 62 79 74 65 73 31 36 28 20 63 6f 6c 75 6d e_bytes16( colum
1bb45 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b nMem(pStmt,i) );
1bb46 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 . columnMallocF
1bb47 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 ailure(pStmt);.
1bb48 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 return val;.}.S
1bb49 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 QLITE_API double
1bb4a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1bb4b 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 double(sqlite3_s
1bb4c 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1bb4d 69 29 7b 0a 20 20 64 6f 75 62 6c 65 20 76 61 6c i){. double val
1bb4e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
1bb4f 5f 64 6f 75 62 6c 65 28 20 63 6f 6c 75 6d 6e 4d _double( columnM
1bb50 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 em(pStmt,i) );.
1bb51 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 columnMallocFai
1bb52 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 lure(pStmt);. r
1bb53 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c eturn val;.}.SQL
1bb54 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1bb55 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 te3_column_int(s
1bb56 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1bb57 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e mt, int i){. in
1bb58 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f t val = sqlite3_
1bb59 76 61 6c 75 65 5f 69 6e 74 28 20 63 6f 6c 75 6d value_int( colum
1bb5a 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b nMem(pStmt,i) );
1bb5b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 . columnMallocF
1bb5c 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 ailure(pStmt);.
1bb5d 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 return val;.}.S
1bb5e 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
1bb5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 63 _int64 sqlite3_c
1bb60 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c 69 olumn_int64(sqli
1bb61 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bb62 20 69 6e 74 20 69 29 7b 0a 20 20 73 71 6c 69 74 int i){. sqlit
1bb63 65 5f 69 6e 74 36 34 20 76 61 6c 20 3d 20 73 71 e_int64 val = sq
1bb64 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 lite3_value_int6
1bb65 34 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 4( columnMem(pSt
1bb66 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d mt,i) );. colum
1bb67 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 nMallocFailure(p
1bb68 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 Stmt);. return
1bb69 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 val;.}.SQLITE_AP
1bb6a 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 I const unsigned
1bb6b 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
1bb6c 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74 olumn_text(sqlit
1bb6d 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1bb6e 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20 int i){. const
1bb6f 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 76 unsigned char *v
1bb70 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c al = sqlite3_val
1bb71 75 65 5f 74 65 78 74 28 20 63 6f 6c 75 6d 6e 4d ue_text( columnM
1bb72 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 em(pStmt,i) );.
1bb73 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 columnMallocFai
1bb74 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 lure(pStmt);. r
1bb75 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c eturn val;.}.SQL
1bb76 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
1bb77 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 5f 63 value *sqlite3_c
1bb78 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69 olumn_value(sqli
1bb79 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bb7a 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d 20 2a int i){. Mem *
1bb7b 70 4f 75 74 20 3d 20 63 6f 6c 75 6d 6e 4d 65 6d pOut = columnMem
1bb7c 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 69 66 (pStmt, i);. if
1bb7d 28 20 70 4f 75 74 2d 3e 66 6c 61 67 73 26 4d 45 ( pOut->flags&ME
1bb7e 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 M_Static ){.
1bb7f 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e pOut->flags &= ~
1bb80 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20 20 20 20 MEM_Static;.
1bb81 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d pOut->flags |= M
1bb82 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 0a 20 20 EM_Ephem;. }.
1bb83 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c columnMallocFail
1bb84 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 ure(pStmt);. re
1bb85 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f 76 61 turn (sqlite3_va
1bb86 6c 75 65 20 2a 29 70 4f 75 74 3b 0a 7d 0a 23 69 lue *)pOut;.}.#i
1bb87 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1bb88 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 T_UTF16.SQLITE_A
1bb89 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
1bb8a 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
1bb8b 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d xt16(sqlite3_stm
1bb8c 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
1bb8d 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a {. const void *
1bb8e 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 val = sqlite3_va
1bb8f 6c 75 65 5f 74 65 78 74 31 36 28 20 63 6f 6c 75 lue_text16( colu
1bb90 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 mnMem(pStmt,i) )
1bb91 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 ;. columnMalloc
1bb92 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a Failure(pStmt);.
1bb93 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a return val;.}.
1bb94 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1bb95 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 _OMIT_UTF16 */.S
1bb96 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1bb97 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 lite3_column_typ
1bb98 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a e(sqlite3_stmt *
1bb99 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 pStmt, int i){.
1bb9a 20 69 6e 74 20 69 54 79 70 65 20 3d 20 73 71 6c int iType = sql
1bb9b 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
1bb9c 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 columnMem(pStmt
1bb9d 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d ,i) );. columnM
1bb9e 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 allocFailure(pSt
1bb9f 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 54 mt);. return iT
1bba0 79 70 65 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 ype;.}../* The f
1bba1 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f ollowing functio
1bba2 6e 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74 61 n is experimenta
1bba3 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20 74 6f l and subject to
1bba4 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 change or.** re
1bba5 6d 6f 76 61 6c 20 2a 2f 0a 2f 2a 69 6e 74 20 73 moval */./*int s
1bba6 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 75 qlite3_column_nu
1bba7 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74 meric_type(sqlit
1bba8 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1bba9 69 6e 74 20 69 29 7b 0a 2a 2a 20 20 72 65 74 75 int i){.** retu
1bbaa 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 rn sqlite3_value
1bbab 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 20 63 _numeric_type( c
1bbac 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 olumnMem(pStmt,i
1bbad 29 20 29 3b 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a ) );.**}.*/../*.
1bbae 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 4e ** Convert the N
1bbaf 2d 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 70 -th element of p
1bbb0 53 74 6d 74 2d 3e 70 43 6f 6c 4e 61 6d 65 5b 5d Stmt->pColName[]
1bbb1 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 75 into a string u
1bbb2 73 69 6e 67 0a 2a 2a 20 78 46 75 6e 63 28 29 20 sing.** xFunc()
1bbb3 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68 61 74 then return that
1bbb4 20 73 74 72 69 6e 67 2e 20 20 49 66 20 4e 20 69 string. If N i
1bbb5 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 s out of range,
1bbb6 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 return 0..**.**
1bbb7 54 68 65 72 65 20 61 72 65 20 75 70 20 74 6f 20 There are up to
1bbb8 35 20 6e 61 6d 65 73 20 66 6f 72 20 65 61 63 68 5 names for each
1bbb9 20 63 6f 6c 75 6d 6e 2e 20 20 75 73 65 54 79 70 column. useTyp
1bbba 65 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69 e determines whi
1bbbb 63 68 0a 2a 2a 20 6e 61 6d 65 20 69 73 20 72 65 ch.** name is re
1bbbc 74 75 72 6e 65 64 2e 20 20 48 65 72 65 20 61 72 turned. Here ar
1bbbd 65 20 74 68 65 20 6e 61 6d 65 73 3a 0a 2a 2a 0a e the names:.**.
1bbbe 2a 2a 20 20 20 20 30 20 20 20 20 20 20 54 68 65 ** 0 The
1bbbf 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 73 20 column name as
1bbc0 69 74 20 73 68 6f 75 6c 64 20 62 65 20 64 69 73 it should be dis
1bbc1 70 6c 61 79 65 64 20 66 6f 72 20 6f 75 74 70 75 played for outpu
1bbc2 74 0a 2a 2a 20 20 20 20 31 20 20 20 20 20 20 54 t.** 1 T
1bbc3 68 65 20 64 61 74 61 74 79 70 65 20 6e 61 6d 65 he datatype name
1bbc4 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 0a for the column.
1bbc5 2a 2a 20 20 20 20 32 20 20 20 20 20 20 54 68 65 ** 2 The
1bbc6 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 name of the dat
1bbc7 61 62 61 73 65 20 74 68 61 74 20 74 68 65 20 63 abase that the c
1bbc8 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 olumn derives fr
1bbc9 6f 6d 0a 2a 2a 20 20 20 20 33 20 20 20 20 20 20 om.** 3
1bbca 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 The name of the
1bbcb 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 63 table that the c
1bbcc 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 olumn derives fr
1bbcd 6f 6d 0a 2a 2a 20 20 20 20 34 20 20 20 20 20 20 om.** 4
1bbce 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 The name of the
1bbcf 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 table column tha
1bbd0 74 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c t the result col
1bbd1 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d umn derives from
1bbd2 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 .**.** If the re
1bbd3 73 75 6c 74 20 69 73 20 6e 6f 74 20 61 20 73 69 sult is not a si
1bbd4 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 mple column refe
1bbd5 72 65 6e 63 65 20 28 69 66 20 69 74 20 69 73 20 rence (if it is
1bbd6 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a an expression.**
1bbd7 20 6f 72 20 61 20 63 6f 6e 73 74 61 6e 74 29 20 or a constant)
1bbd8 74 68 65 6e 20 75 73 65 54 79 70 65 73 20 32 2c then useTypes 2,
1bbd9 20 33 2c 20 61 6e 64 20 34 20 72 65 74 75 72 6e 3, and 4 return
1bbda 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 NULL..*/.static
1bbdb 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 63 6f 6c const void *col
1bbdc 75 6d 6e 4e 61 6d 65 28 0a 20 20 73 71 6c 69 74 umnName(. sqlit
1bbdd 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 0a e3_stmt *pStmt,.
1bbde 20 20 69 6e 74 20 4e 2c 0a 20 20 63 6f 6e 73 74 int N,. const
1bbdf 20 76 6f 69 64 20 2a 28 2a 78 46 75 6e 63 29 28 void *(*xFunc)(
1bbe0 4d 65 6d 2a 29 2c 0a 20 20 69 6e 74 20 75 73 65 Mem*),. int use
1bbe1 54 79 70 65 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 Type.){. const
1bbe2 76 6f 69 64 20 2a 72 65 74 20 3d 20 30 3b 0a 20 void *ret = 0;.
1bbe3 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 Vdbe *p = (Vdbe
1bbe4 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 *)pStmt;. int
1bbe5 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 n;. sqlite3 *db
1bbe6 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 0a 20 20 61 = p->db;. . a
1bbe7 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a ssert( db!=0 );.
1bbe8 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f n = sqlite3_co
1bbe9 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d 74 lumn_count(pStmt
1bbea 29 3b 0a 20 20 69 66 28 20 4e 3c 6e 20 26 26 20 );. if( N<n &&
1bbeb 4e 3e 3d 30 20 29 7b 0a 20 20 20 20 4e 20 2b 3d N>=0 ){. N +=
1bbec 20 75 73 65 54 79 70 65 2a 6e 3b 0a 20 20 20 20 useType*n;.
1bbed 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
1bbee 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a ter(db->mutex);.
1bbef 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e assert( db->
1bbf0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 mallocFailed==0
1bbf1 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 78 46 75 );. ret = xFu
1bbf2 6e 63 28 26 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b nc(&p->aColName[
1bbf3 4e 5d 29 3b 0a 20 20 20 20 20 2f 2a 20 41 20 6d N]);. /* A m
1bbf4 61 6c 6c 6f 63 20 6d 61 79 20 68 61 76 65 20 66 alloc may have f
1bbf5 61 69 6c 65 64 20 69 6e 73 69 64 65 20 6f 66 20 ailed inside of
1bbf6 74 68 65 20 78 46 75 6e 63 28 29 20 63 61 6c 6c the xFunc() call
1bbf7 2e 20 49 66 20 74 68 69 73 0a 20 20 20 20 2a 2a . If this. **
1bbf8 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 63 6c is the case, cl
1bbf9 65 61 72 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 ear the mallocFa
1bbfa 69 6c 65 64 20 66 6c 61 67 20 61 6e 64 20 72 65 iled flag and re
1bbfb 74 75 72 6e 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a turn NULL.. *
1bbfc 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 /. if( db->ma
1bbfd 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
1bbfe 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 db->mallocFa
1bbff 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 iled = 0;.
1bc00 72 65 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 ret = 0;. }.
1bc01 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
1bc02 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 _leave(db->mutex
1bc03 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1bc04 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ret;.}../*.** Re
1bc05 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 turn the name of
1bc06 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 the Nth column
1bc07 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 of the result se
1bc08 74 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 t returned by SQ
1bc09 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 70 L.** statement p
1bc0a 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f Stmt..*/.SQLITE_
1bc0b 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a API const char *
1bc0c 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e sqlite3_column_n
1bc0d 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ame(sqlite3_stmt
1bc0e 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b *pStmt, int N){
1bc0f 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e . return column
1bc10 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d Name(. pStm
1bc11 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 t, N, (const voi
1bc12 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 d*(*)(Mem*))sqli
1bc13 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 te3_value_text,
1bc14 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d COLNAME_NAME);.}
1bc15 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1bc16 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 OMIT_UTF16.SQLIT
1bc17 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
1bc18 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
1bc19 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f _name16(sqlite3_
1bc1a 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1bc1b 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f N){. return co
1bc1c 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 lumnName(.
1bc1d 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 pStmt, N, (const
1bc1e 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 void*(*)(Mem*))
1bc1f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1bc20 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 xt16, COLNAME_NA
1bc21 4d 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f ME);.}.#endif../
1bc22 2a 0a 2a 2a 20 43 6f 6e 73 74 72 61 69 6e 74 3a *.** Constraint:
1bc23 20 20 49 66 20 79 6f 75 20 68 61 76 65 20 45 4e If you have EN
1bc24 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 ABLE_COLUMN_META
1bc25 44 41 54 41 20 74 68 65 6e 20 79 6f 75 20 6d 75 DATA then you mu
1bc26 73 74 0a 2a 2a 20 6e 6f 74 20 64 65 66 69 6e 65 st.** not define
1bc27 20 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 2e 0a OMIT_DECLTYPE..
1bc28 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 */.#if defined(S
1bc29 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 QLITE_OMIT_DECLT
1bc2a 59 50 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 YPE) && defined(
1bc2b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f SQLITE_ENABLE_CO
1bc2c 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 29 0a 23 LUMN_METADATA).#
1bc2d 20 65 72 72 6f 72 20 22 4d 75 73 74 20 6e 6f 74 error "Must not
1bc2e 20 64 65 66 69 6e 65 20 62 6f 74 68 20 53 51 4c define both SQL
1bc2f 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 ITE_OMIT_DECLTYP
1bc30 45 20 5c 0a 20 20 20 20 20 20 20 20 20 61 6e 64 E \. and
1bc31 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 SQLITE_ENABLE_C
1bc32 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 22 0a OLUMN_METADATA".
1bc33 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 #endif..#ifndef
1bc34 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c SQLITE_OMIT_DECL
1bc35 54 59 50 45 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 TYPE./*.** Retur
1bc36 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 63 n the column dec
1bc37 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 28 69 laration type (i
1bc38 66 20 61 70 70 6c 69 63 61 62 6c 65 29 20 6f 66 f applicable) of
1bc39 20 74 68 65 20 27 69 27 74 68 20 63 6f 6c 75 6d the 'i'th colum
1bc3a 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 72 65 73 75 n.** of the resu
1bc3b 6c 74 20 73 65 74 20 6f 66 20 53 51 4c 20 73 74 lt set of SQL st
1bc3c 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a atement pStmt..*
1bc3d 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
1bc3e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
1bc3f 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 _column_decltype
1bc40 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1bc41 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 Stmt, int N){.
1bc42 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d return columnNam
1bc43 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 e(. pStmt,
1bc44 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 N, (const void*(
1bc45 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 *)(Mem*))sqlite3
1bc46 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c _value_text, COL
1bc47 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a NAME_DECLTYPE);.
1bc48 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
1bc49 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 _OMIT_UTF16.SQLI
1bc4a 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 TE_API const voi
1bc4b 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d d *sqlite3_colum
1bc4c 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73 71 6c n_decltype16(sql
1bc4d 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bc4e 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 , int N){. retu
1bc4f 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 rn columnName(.
1bc50 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 pStmt, N, (
1bc51 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d const void*(*)(M
1bc52 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c em*))sqlite3_val
1bc53 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 ue_text16, COLNA
1bc54 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a 7d 0a ME_DECLTYPE);.}.
1bc55 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1bc56 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 23 _OMIT_UTF16 */.#
1bc57 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1bc58 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 2a 2f OMIT_DECLTYPE */
1bc59 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
1bc5a 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 ENABLE_COLUMN_ME
1bc5b 54 41 44 41 54 41 0a 2f 2a 0a 2a 2a 20 52 65 74 TADATA./*.** Ret
1bc5c 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 urn the name of
1bc5d 74 68 65 20 64 61 74 61 62 61 73 65 20 66 72 6f the database fro
1bc5e 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c 74 m which a result
1bc5f 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e column derives.
1bc60 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 .** NULL is retu
1bc61 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73 75 rned if the resu
1bc62 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 lt column is an
1bc63 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f expression or co
1bc64 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 nstant or.** any
1bc65 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68 thing else which
1bc66 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 is not an unabi
1bc67 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20 guous reference
1bc68 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63 6f to a database co
1bc69 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f lumn..*/.SQLITE_
1bc6a 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a API const char *
1bc6b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 sqlite3_column_d
1bc6c 61 74 61 62 61 73 65 5f 6e 61 6d 65 28 73 71 6c atabase_name(sql
1bc6d 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bc6e 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 , int N){. retu
1bc6f 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 rn columnName(.
1bc70 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 pStmt, N, (
1bc71 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d const void*(*)(M
1bc72 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c em*))sqlite3_val
1bc73 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 ue_text, COLNAME
1bc74 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d 0a 23 69 _DATABASE);.}.#i
1bc75 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1bc76 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 T_UTF16.SQLITE_A
1bc77 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
1bc78 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 qlite3_column_da
1bc79 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 73 71 tabase_name16(sq
1bc7a 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1bc7b 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 t, int N){. ret
1bc7c 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a urn columnName(.
1bc7d 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 pStmt, N,
1bc7e 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 (const void*(*)(
1bc7f 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 Mem*))sqlite3_va
1bc80 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e lue_text16, COLN
1bc81 41 4d 45 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d AME_DATABASE);.}
1bc82 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1bc83 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a E_OMIT_UTF16 */.
1bc84 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
1bc85 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 e name of the ta
1bc86 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 ble from which a
1bc87 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 result column d
1bc88 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 erives..** NULL
1bc89 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 is returned if t
1bc8a 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e he result column
1bc8b 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f is an expressio
1bc8c 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72 n or constant or
1bc8d 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 .** anything els
1bc8e 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61 e which is not a
1bc8f 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 65 66 n unabiguous ref
1bc90 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 74 61 erence to a data
1bc91 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a base column..*/.
1bc92 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
1bc93 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
1bc94 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 olumn_table_name
1bc95 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1bc96 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 Stmt, int N){.
1bc97 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d return columnNam
1bc98 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 e(. pStmt,
1bc99 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 N, (const void*(
1bc9a 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 *)(Mem*))sqlite3
1bc9b 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c _value_text, COL
1bc9c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 NAME_TABLE);.}.#
1bc9d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1bc9e 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f IT_UTF16.SQLITE_
1bc9f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
1bca0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
1bca1 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 able_name16(sqli
1bca2 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bca3 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 int N){. retur
1bca4 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 n columnName(.
1bca5 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 pStmt, N, (c
1bca6 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 onst void*(*)(Me
1bca7 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 m*))sqlite3_valu
1bca8 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d e_text16, COLNAM
1bca9 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 65 6e 64 E_TABLE);.}.#end
1bcaa 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1bcab 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a T_UTF16 */../*.*
1bcac 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d * Return the nam
1bcad 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 e of the table c
1bcae 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69 63 68 olumn from which
1bcaf 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e a result column
1bcb0 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c derives..** NUL
1bcb1 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 L is returned if
1bcb2 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 the result colu
1bcb3 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 mn is an express
1bcb4 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 ion or constant
1bcb5 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 or.** anything e
1bcb6 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 lse which is not
1bcb7 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 an unabiguous r
1bcb8 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 eference to a da
1bcb9 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a tabase column..*
1bcba 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
1bcbb 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
1bcbc 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e _column_origin_n
1bcbd 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ame(sqlite3_stmt
1bcbe 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b *pStmt, int N){
1bcbf 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e . return column
1bcc0 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d Name(. pStm
1bcc1 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 t, N, (const voi
1bcc2 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 d*(*)(Mem*))sqli
1bcc3 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 te3_value_text,
1bcc4 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b COLNAME_COLUMN);
1bcc5 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 .}.#ifndef SQLIT
1bcc6 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c E_OMIT_UTF16.SQL
1bcc7 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
1bcc8 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 id *sqlite3_colu
1bcc9 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 mn_origin_name16
1bcca 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1bccb 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 Stmt, int N){.
1bccc 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d return columnNam
1bccd 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 e(. pStmt,
1bcce 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 N, (const void*(
1bccf 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 *)(Mem*))sqlite3
1bcd0 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 _value_text16, C
1bcd1 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0a OLNAME_COLUMN);.
1bcd2 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1bcd3 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f TE_OMIT_UTF16 */
1bcd4 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1bcd5 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f E_ENABLE_COLUMN_
1bcd6 4d 45 54 41 44 41 54 41 20 2a 2f 0a 0a 0a 2f 2a METADATA */.../*
1bcd7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bcd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 ************** s
1bcd9 71 6c 69 74 65 33 5f 62 69 6e 64 5f 20 20 2a 2a qlite3_bind_ **
1bcda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bcdb 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a 2a *********.** .**
1bcdc 20 52 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74 Routines used t
1bcdd 6f 20 61 74 74 61 63 68 20 76 61 6c 75 65 73 20 o attach values
1bcde 74 6f 20 77 69 6c 64 63 61 72 64 73 20 69 6e 20 to wildcards in
1bcdf 61 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 a compiled SQL s
1bce0 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 2f 2a 0a tatement..*/./*.
1bce1 2a 2a 20 55 6e 62 69 6e 64 20 74 68 65 20 76 61 ** Unbind the va
1bce2 6c 75 65 20 62 6f 75 6e 64 20 74 6f 20 76 61 72 lue bound to var
1bce3 69 61 62 6c 65 20 69 20 69 6e 20 76 69 72 74 75 iable i in virtu
1bce4 61 6c 20 6d 61 63 68 69 6e 65 20 70 2e 20 54 68 al machine p. Th
1bce5 69 73 20 69 73 20 74 68 65 20 0a 2a 2a 20 74 68 is is the .** th
1bce6 65 20 73 61 6d 65 20 61 73 20 62 69 6e 64 69 6e e same as bindin
1bce7 67 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 74 g a NULL value t
1bce8 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 49 66 o the column. If
1bce9 20 74 68 65 20 22 69 22 20 70 61 72 61 6d 65 74 the "i" paramet
1bcea 65 72 20 69 73 0a 2a 2a 20 6f 75 74 20 6f 66 20 er is.** out of
1bceb 72 61 6e 67 65 2c 20 74 68 65 6e 20 53 51 4c 49 range, then SQLI
1bcec 54 45 5f 52 41 4e 47 45 20 69 73 20 72 65 74 75 TE_RANGE is retu
1bced 72 6e 65 64 2e 20 4f 74 68 65 77 69 73 65 20 53 rned. Othewise S
1bcee 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 QLITE_OK..**.**
1bcef 41 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 61 A successful eva
1bcf0 6c 75 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 luation of this
1bcf1 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72 65 73 routine acquires
1bcf2 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 70 2e the mutex on p.
1bcf3 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20 69 73 .** the mutex is
1bcf4 20 72 65 6c 65 61 73 65 64 20 69 66 20 61 6e 79 released if any
1bcf5 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20 6f kind of error o
1bcf6 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ccurs..**.** The
1bcf7 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74 6f 72 error code stor
1bcf8 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 70 ed in database p
1bcf9 2d 3e 64 62 20 69 73 20 6f 76 65 72 77 72 69 74 ->db is overwrit
1bcfa 74 65 6e 20 77 69 74 68 20 74 68 65 20 72 65 74 ten with the ret
1bcfb 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 urn.** value in
1bcfc 61 6e 79 20 63 61 73 65 2e 0a 2a 2f 0a 73 74 61 any case..*/.sta
1bcfd 74 69 63 20 69 6e 74 20 76 64 62 65 55 6e 62 69 tic int vdbeUnbi
1bcfe 6e 64 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 nd(Vdbe *p, int
1bcff 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b i){. Mem *pVar;
1bd00 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
1bd01 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 turn SQLITE_MISU
1bd02 53 45 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 SE;. sqlite3_mu
1bd03 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62 2d tex_enter(p->db-
1bd04 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70 >mutex);. if( p
1bd05 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 ->magic!=VDBE_MA
1bd06 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 70 63 GIC_RUN || p->pc
1bd07 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 >=0 ){. sqlit
1bd08 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 e3Error(p->db, S
1bd09 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 30 29 QLITE_MISUSE, 0)
1bd0a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1bd0b 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d tex_leave(p->db-
1bd0c 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74 >mutex);. ret
1bd0d 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 urn SQLITE_MISUS
1bd0e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 3c 31 E;. }. if( i<1
1bd0f 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b || i>p->nVar ){
1bd10 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
1bd11 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f r(p->db, SQLITE_
1bd12 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20 20 73 RANGE, 0);. s
1bd13 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1bd14 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ve(p->db->mutex)
1bd15 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
1bd16 49 54 45 5f 52 41 4e 47 45 3b 0a 20 20 7d 0a 20 ITE_RANGE;. }.
1bd17 20 69 2d 2d 3b 0a 20 20 70 56 61 72 20 3d 20 26 i--;. pVar = &
1bd18 70 2d 3e 61 56 61 72 5b 69 5d 3b 0a 20 20 73 71 p->aVar[i];. sq
1bd19 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
1bd1a 61 73 65 28 70 56 61 72 29 3b 0a 20 20 70 56 61 ase(pVar);. pVa
1bd1b 72 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e r->flags = MEM_N
1bd1c 75 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 45 72 ull;. sqlite3Er
1bd1d 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 ror(p->db, SQLIT
1bd1e 45 5f 4f 4b 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 E_OK, 0);.. /*
1bd1f 49 66 20 74 68 65 20 62 69 74 20 63 6f 72 72 65 If the bit corre
1bd20 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 69 73 sponding to this
1bd21 20 76 61 72 69 61 62 6c 65 20 69 6e 20 56 64 62 variable in Vdb
1bd22 65 2e 65 78 70 6d 61 73 6b 20 69 73 20 73 65 74 e.expmask is set
1bd23 2c 20 74 68 65 6e 20 0a 20 20 2a 2a 20 62 69 6e , then . ** bin
1bd24 64 69 6e 67 20 61 20 6e 65 77 20 76 61 6c 75 65 ding a new value
1bd25 20 74 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c to this variabl
1bd26 65 20 69 6e 76 61 6c 69 64 61 74 65 73 20 74 68 e invalidates th
1bd27 65 20 63 75 72 72 65 6e 74 20 71 75 65 72 79 20 e current query
1bd28 70 6c 61 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 plan.. */. if(
1bd29 20 70 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 p->isPrepareV2
1bd2a 26 26 0a 20 20 20 20 20 28 28 69 3c 33 32 20 26 &&. ((i<32 &
1bd2b 26 20 70 2d 3e 65 78 70 6d 61 73 6b 20 26 20 28 & p->expmask & (
1bd2c 28 75 33 32 29 31 20 3c 3c 20 69 29 29 20 7c 7c (u32)1 << i)) ||
1bd2d 20 70 2d 3e 65 78 70 6d 61 73 6b 3d 3d 30 78 66 p->expmask==0xf
1bd2e 66 66 66 66 66 66 66 29 0a 20 20 29 7b 0a 20 20 fffffff). ){.
1bd2f 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 p->expired = 1
1bd30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
1bd31 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
1bd32 2a 2a 20 42 69 6e 64 20 61 20 74 65 78 74 20 6f ** Bind a text o
1bd33 72 20 42 4c 4f 42 20 76 61 6c 75 65 2e 0a 2a 2f r BLOB value..*/
1bd34 0a 73 74 61 74 69 63 20 69 6e 74 20 62 69 6e 64 .static int bind
1bd35 54 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33 5f Text(. sqlite3_
1bd36 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 20 20 2f stmt *pStmt, /
1bd37 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 * The statement
1bd38 74 6f 20 62 69 6e 64 20 61 67 61 69 6e 73 74 20 to bind against
1bd39 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 20 */. int i,
1bd3a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
1bd3b 6e 64 65 78 20 6f 66 20 74 68 65 20 70 61 72 61 ndex of the para
1bd3c 6d 65 74 65 72 20 74 6f 20 62 69 6e 64 20 2a 2f meter to bind */
1bd3d 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a . const void *z
1bd3e 44 61 74 61 2c 20 20 20 20 20 2f 2a 20 50 6f 69 Data, /* Poi
1bd3f 6e 74 65 72 20 74 6f 20 74 68 65 20 64 61 74 61 nter to the data
1bd40 20 74 6f 20 62 65 20 62 6f 75 6e 64 20 2a 2f 0a to be bound */.
1bd41 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 20 20 20 int nData,
1bd42 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1bd43 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 er of bytes of d
1bd44 61 74 61 20 74 6f 20 62 65 20 62 6f 75 6e 64 20 ata to be bound
1bd45 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c */. void (*xDel
1bd46 29 28 76 6f 69 64 2a 29 2c 20 20 20 2f 2a 20 44 )(void*), /* D
1bd47 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 estructor for th
1bd48 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 20 65 e data */. u8 e
1bd49 6e 63 6f 64 69 6e 67 20 20 20 20 20 20 20 20 20 ncoding
1bd4a 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 66 /* Encoding f
1bd4b 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 29 or the data */.)
1bd4c 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 {. Vdbe *p = (V
1bd4d 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 4d dbe *)pStmt;. M
1bd4e 65 6d 20 2a 70 56 61 72 3b 0a 20 20 69 6e 74 20 em *pVar;. int
1bd4f 72 63 3b 0a 0a 20 20 72 63 20 3d 20 76 64 62 65 rc;.. rc = vdbe
1bd50 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 Unbind(p, i);.
1bd51 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1bd52 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 7a 44 61 K ){. if( zDa
1bd53 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 ta!=0 ){. p
1bd54 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 5b 69 Var = &p->aVar[i
1bd55 2d 31 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 -1];. rc =
1bd56 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1bd57 74 53 74 72 28 70 56 61 72 2c 20 7a 44 61 74 61 tStr(pVar, zData
1bd58 2c 20 6e 44 61 74 61 2c 20 65 6e 63 6f 64 69 6e , nData, encodin
1bd59 67 2c 20 78 44 65 6c 29 3b 0a 20 20 20 20 20 20 g, xDel);.
1bd5a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1bd5b 4b 20 26 26 20 65 6e 63 6f 64 69 6e 67 21 3d 30 K && encoding!=0
1bd5c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
1bd5d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1bd5e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61 72 2c geEncoding(pVar,
1bd5f 20 45 4e 43 28 70 2d 3e 64 62 29 29 3b 0a 20 20 ENC(p->db));.
1bd60 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
1bd61 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 te3Error(p->db,
1bd62 72 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63 rc, 0);. rc
1bd63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 = sqlite3ApiExi
1bd64 74 28 70 2d 3e 64 62 2c 20 72 63 29 3b 0a 20 20 t(p->db, rc);.
1bd65 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f }. sqlite3_
1bd66 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 mutex_leave(p->d
1bd67 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 b->mutex);. }.
1bd68 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a return rc;.}...
1bd69 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 20 62 6c 6f /*.** Bind a blo
1bd6a 62 20 76 61 6c 75 65 20 74 6f 20 61 6e 20 53 51 b value to an SQ
1bd6b 4c 20 73 74 61 74 65 6d 65 6e 74 20 76 61 72 69 L statement vari
1bd6c 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f able..*/.SQLITE_
1bd6d 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1bd6e 62 69 6e 64 5f 62 6c 6f 62 28 0a 20 20 73 71 6c bind_blob(. sql
1bd6f 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bd70 2c 20 0a 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 , . int i, . c
1bd71 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 44 61 74 61 onst void *zData
1bd72 2c 20 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 , . int nData,
1bd73 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 . void (*xDel)(
1bd74 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 void*).){. retu
1bd75 72 6e 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d rn bindText(pStm
1bd76 74 2c 20 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 t, i, zData, nDa
1bd77 74 61 2c 20 78 44 65 6c 2c 20 30 29 3b 0a 7d 0a ta, xDel, 0);.}.
1bd78 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1bd79 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 qlite3_bind_doub
1bd7a 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 le(sqlite3_stmt
1bd7b 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 64 *pStmt, int i, d
1bd7c 6f 75 62 6c 65 20 72 56 61 6c 75 65 29 7b 0a 20 ouble rValue){.
1bd7d 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 65 20 int rc;. Vdbe
1bd7e 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 *p = (Vdbe *)pSt
1bd7f 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 55 mt;. rc = vdbeU
1bd80 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 nbind(p, i);. i
1bd81 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1bd82 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1bd83 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 dbeMemSetDouble(
1bd84 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 72 &p->aVar[i-1], r
1bd85 56 61 6c 75 65 29 3b 0a 20 20 20 20 73 71 6c 69 Value);. sqli
1bd86 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
1bd87 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 p->db->mutex);.
1bd88 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1bd89 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 }.SQLITE_API int
1bd8a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e sqlite3_bind_in
1bd8b 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a t(sqlite3_stmt *
1bd8c 70 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20 69 56 p, int i, int iV
1bd8d 61 6c 75 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 alue){. return
1bd8e 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 sqlite3_bind_int
1bd8f 36 34 28 70 2c 20 69 2c 20 28 69 36 34 29 69 56 64(p, i, (i64)iV
1bd90 61 6c 75 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f alue);.}.SQLITE_
1bd91 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1bd92 62 69 6e 64 5f 69 6e 74 36 34 28 73 71 6c 69 74 bind_int64(sqlit
1bd93 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1bd94 69 6e 74 20 69 2c 20 73 71 6c 69 74 65 5f 69 6e int i, sqlite_in
1bd95 74 36 34 20 69 56 61 6c 75 65 29 7b 0a 20 20 69 t64 iValue){. i
1bd96 6e 74 20 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 nt rc;. Vdbe *p
1bd97 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 = (Vdbe *)pStmt
1bd98 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62 ;. rc = vdbeUnb
1bd99 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 ind(p, i);. if(
1bd9a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1bd9b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1bd9c 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 70 2d eMemSetInt64(&p-
1bd9d 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 69 56 61 6c >aVar[i-1], iVal
1bd9e 75 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ue);. sqlite3
1bd9f 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e _mutex_leave(p->
1bda0 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a db->mutex);. }.
1bda1 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 return rc;.}.S
1bda2 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1bda3 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 lite3_bind_null(
1bda4 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1bda5 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 tmt, int i){. i
1bda6 6e 74 20 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 nt rc;. Vdbe *p
1bda7 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b = (Vdbe*)pStmt;
1bda8 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 . rc = vdbeUnbi
1bda9 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 nd(p, i);. if(
1bdaa 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1bdab 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
1bdac 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e ex_leave(p->db->
1bdad 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 mutex);. }. re
1bdae 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 turn rc;.}.SQLIT
1bdaf 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1bdb0 33 5f 62 69 6e 64 5f 74 65 78 74 28 20 0a 20 20 3_bind_text( .
1bdb1 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1bdb2 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c 20 0a tmt, . int i, .
1bdb3 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 const char *zD
1bdb4 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44 61 74 ata, . int nDat
1bdb5 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 a, . void (*xDe
1bdb6 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 72 l)(void*).){. r
1bdb7 65 74 75 72 6e 20 62 69 6e 64 54 65 78 74 28 70 eturn bindText(p
1bdb8 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61 2c 20 Stmt, i, zData,
1bdb9 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 53 51 4c nData, xDel, SQL
1bdba 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 23 69 66 ITE_UTF8);.}.#if
1bdbb 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1bdbc 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 _UTF16.SQLITE_AP
1bdbd 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
1bdbe 6e 64 5f 74 65 78 74 31 36 28 0a 20 20 73 71 6c nd_text16(. sql
1bdbf 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bdc0 2c 20 0a 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 , . int i, . c
1bdc1 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 44 61 74 61 onst void *zData
1bdc2 2c 20 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 , . int nData,
1bdc3 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 . void (*xDel)(
1bdc4 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 void*).){. retu
1bdc5 72 6e 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d rn bindText(pStm
1bdc6 74 2c 20 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 t, i, zData, nDa
1bdc7 74 61 2c 20 78 44 65 6c 2c 20 53 51 4c 49 54 45 ta, xDel, SQLITE
1bdc8 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b 0a 7d _UTF16NATIVE);.}
1bdc9 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1bdca 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a E_OMIT_UTF16 */.
1bdcb 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1bdcc 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 qlite3_bind_valu
1bdcd 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a e(sqlite3_stmt *
1bdce 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 63 6f pStmt, int i, co
1bdcf 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 nst sqlite3_valu
1bdd0 65 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 69 6e e *pValue){. in
1bdd1 74 20 72 63 3b 0a 20 20 73 77 69 74 63 68 28 20 t rc;. switch(
1bdd2 70 56 61 6c 75 65 2d 3e 74 79 70 65 20 29 7b 0a pValue->type ){.
1bdd3 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
1bdd4 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 INTEGER: {.
1bdd5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 rc = sqlite3_bi
1bdd6 6e 64 5f 69 6e 74 36 34 28 70 53 74 6d 74 2c 20 nd_int64(pStmt,
1bdd7 69 2c 20 70 56 61 6c 75 65 2d 3e 75 2e 69 29 3b i, pValue->u.i);
1bdd8 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1bdd9 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c }. case SQL
1bdda 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 ITE_FLOAT: {.
1bddb 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f rc = sqlite3_
1bddc 62 69 6e 64 5f 64 6f 75 62 6c 65 28 70 53 74 6d bind_double(pStm
1bddd 74 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e 72 29 t, i, pValue->r)
1bdde 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1bddf 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 }. case SQ
1bde0 4c 49 54 45 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 LITE_BLOB: {.
1bde1 20 20 20 69 66 28 20 70 56 61 6c 75 65 2d 3e 66 if( pValue->f
1bde2 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 lags & MEM_Zero
1bde3 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
1bde4 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 sqlite3_bind_zer
1bde5 6f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 69 2c 20 oblob(pStmt, i,
1bde6 70 56 61 6c 75 65 2d 3e 75 2e 6e 5a 65 72 6f 29 pValue->u.nZero)
1bde7 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1bde8 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1bde9 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 70 53 te3_bind_blob(pS
1bdea 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e tmt, i, pValue->
1bdeb 7a 2c 20 70 56 61 6c 75 65 2d 3e 6e 2c 53 51 4c z, pValue->n,SQL
1bdec 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a ITE_TRANSIENT);.
1bded 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
1bdee 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1bdef 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a ase SQLITE_TEXT:
1bdf0 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 62 69 {. rc = bi
1bdf1 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 69 2c 20 ndText(pStmt,i,
1bdf2 20 70 56 61 6c 75 65 2d 3e 7a 2c 20 70 56 61 6c pValue->z, pVal
1bdf3 75 65 2d 3e 6e 2c 20 53 51 4c 49 54 45 5f 54 52 ue->n, SQLITE_TR
1bdf4 41 4e 53 49 45 4e 54 2c 0a 20 20 20 20 20 20 20 ANSIENT,.
1bdf5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bdf6 20 20 20 20 20 20 20 70 56 61 6c 75 65 2d 3e 65 pValue->e
1bdf7 6e 63 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b nc);. break
1bdf8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 ;. }. defa
1bdf9 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 ult: {. rc
1bdfa 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e = sqlite3_bind_n
1bdfb 75 6c 6c 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 ull(pStmt, i);.
1bdfc 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1bdfd 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
1bdfe 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 c;.}.SQLITE_API
1bdff 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
1be00 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 _zeroblob(sqlite
1be01 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
1be02 6e 74 20 69 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 nt i, int n){.
1be03 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 65 20 2a int rc;. Vdbe *
1be04 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d p = (Vdbe *)pStm
1be05 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e t;. rc = vdbeUn
1be06 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 bind(p, i);. if
1be07 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1be08 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1be09 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 beMemSetZeroBlob
1be0a 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 (&p->aVar[i-1],
1be0b 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f n);. sqlite3_
1be0c 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 mutex_leave(p->d
1be0d 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 b->mutex);. }.
1be0e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1be0f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1be10 6e 75 6d 62 65 72 20 6f 66 20 77 69 6c 64 63 61 number of wildca
1be11 72 64 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 rds that can be
1be12 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 6f 75 6e potentially boun
1be13 64 20 74 6f 2e 0a 2a 2a 20 54 68 69 73 20 72 6f d to..** This ro
1be14 75 74 69 6e 65 20 69 73 20 61 64 64 65 64 20 74 utine is added t
1be15 6f 20 73 75 70 70 6f 72 74 20 44 42 44 3a 3a 53 o support DBD::S
1be16 51 4c 69 74 65 2e 20 20 0a 2a 2f 0a 53 51 4c 49 QLite. .*/.SQLI
1be17 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1be18 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 e3_bind_paramete
1be19 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f r_count(sqlite3_
1be1a 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 stmt *pStmt){.
1be1b 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a Vdbe *p = (Vdbe*
1be1c 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e )pStmt;. return
1be1d 20 70 20 3f 20 70 2d 3e 6e 56 61 72 20 3a 20 30 p ? p->nVar : 0
1be1e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 ;.}../*.** Creat
1be1f 65 20 61 20 6d 61 70 70 69 6e 67 20 66 72 6f 6d e a mapping from
1be20 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 variable number
1be21 73 20 74 6f 20 76 61 72 69 61 62 6c 65 20 6e 61 s to variable na
1be22 6d 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 56 64 mes.** in the Vd
1be23 62 65 2e 61 7a 56 61 72 5b 5d 20 61 72 72 61 79 be.azVar[] array
1be24 2c 20 69 66 20 73 75 63 68 20 61 20 6d 61 70 70 , if such a mapp
1be25 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 ing does not alr
1be26 65 61 64 79 0a 2a 2a 20 65 78 69 73 74 2e 0a 2a eady.** exist..*
1be27 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 72 /.static void cr
1be28 65 61 74 65 56 61 72 4d 61 70 28 56 64 62 65 20 eateVarMap(Vdbe
1be29 2a 70 29 7b 0a 20 20 69 66 28 20 21 70 2d 3e 6f *p){. if( !p->o
1be2a 6b 56 61 72 20 29 7b 0a 20 20 20 20 69 6e 74 20 kVar ){. int
1be2b 6a 3b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a j;. Op *pOp;.
1be2c 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1be2d 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62 2d 3e 6d x_enter(p->db->m
1be2e 75 74 65 78 29 3b 0a 20 20 20 20 2f 2a 20 54 68 utex);. /* Th
1be2f 65 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e e race condition
1be30 20 68 65 72 65 20 69 73 20 68 61 72 6d 6c 65 73 here is harmles
1be31 73 2e 20 20 49 66 20 74 77 6f 20 74 68 72 65 61 s. If two threa
1be32 64 73 20 63 61 6c 6c 20 74 68 69 73 0a 20 20 20 ds call this.
1be33 20 2a 2a 20 72 6f 75 74 69 6e 65 20 6f 6e 20 74 ** routine on t
1be34 68 65 20 73 61 6d 65 20 56 64 62 65 20 61 74 20 he same Vdbe at
1be35 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 74 the same time, t
1be36 68 65 79 20 62 6f 74 68 20 6d 69 67 68 74 20 65 hey both might e
1be37 6e 64 0a 20 20 20 20 2a 2a 20 75 70 20 69 6e 69 nd. ** up ini
1be38 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 56 64 tializing the Vd
1be39 62 65 2e 61 7a 56 61 72 5b 5d 20 61 72 72 61 79 be.azVar[] array
1be3a 2e 20 20 54 68 61 74 20 69 73 20 61 20 6c 69 74 . That is a lit
1be3b 74 6c 65 20 65 78 74 72 61 0a 20 20 20 20 2a 2a tle extra. **
1be3c 20 77 6f 72 6b 20 62 75 74 20 69 74 20 72 65 73 work but it res
1be3d 75 6c 74 73 20 69 6e 20 74 68 65 20 73 61 6d 65 ults in the same
1be3e 20 61 6e 73 77 65 72 2e 0a 20 20 20 20 2a 2f 0a answer.. */.
1be3f 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 4f 70 for(j=0, pOp
1be40 3d 70 2d 3e 61 4f 70 3b 20 6a 3c 70 2d 3e 6e 4f =p->aOp; j<p->nO
1be41 70 3b 20 6a 2b 2b 2c 20 70 4f 70 2b 2b 29 7b 0a p; j++, pOp++){.
1be42 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f if( pOp->o
1be43 70 63 6f 64 65 3d 3d 4f 50 5f 56 61 72 69 61 62 pcode==OP_Variab
1be44 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 le ){. as
1be45 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 sert( pOp->p1>0
1be46 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e && pOp->p1<=p->n
1be47 56 61 72 20 29 3b 0a 20 20 20 20 20 20 20 20 70 Var );. p
1be48 2d 3e 61 7a 56 61 72 5b 70 4f 70 2d 3e 70 31 2d ->azVar[pOp->p1-
1be49 31 5d 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 1] = pOp->p4.z;.
1be4a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1be4b 20 20 70 2d 3e 6f 6b 56 61 72 20 3d 20 31 3b 0a p->okVar = 1;.
1be4c 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1be4d 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d x_leave(p->db->m
1be4e 75 74 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a utex);. }.}../*
1be4f 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
1be50 61 6d 65 20 6f 66 20 61 20 77 69 6c 64 63 61 72 ame of a wildcar
1be51 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 52 65 d parameter. Re
1be52 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 turn NULL if the
1be53 20 69 6e 64 65 78 0a 2a 2a 20 69 73 20 6f 75 74 index.** is out
1be54 20 6f 66 20 72 61 6e 67 65 20 6f 72 20 69 66 20 of range or if
1be55 74 68 65 20 77 69 6c 64 63 61 72 64 20 69 73 20 the wildcard is
1be56 75 6e 6e 61 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 unnamed..**.** T
1be57 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c 77 he result is alw
1be58 61 79 73 20 55 54 46 2d 38 2e 0a 2a 2f 0a 53 51 ays UTF-8..*/.SQ
1be59 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 LITE_API const c
1be5a 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 62 69 6e har *sqlite3_bin
1be5b 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 d_parameter_name
1be5c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1be5d 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 Stmt, int i){.
1be5e 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a Vdbe *p = (Vdbe*
1be5f 29 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 3d )pStmt;. if( p=
1be60 3d 30 20 7c 7c 20 69 3c 31 20 7c 7c 20 69 3e 70 =0 || i<1 || i>p
1be61 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 72 65 ->nVar ){. re
1be62 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 63 72 turn 0;. }. cr
1be63 65 61 74 65 56 61 72 4d 61 70 28 70 29 3b 0a 20 eateVarMap(p);.
1be64 20 72 65 74 75 72 6e 20 70 2d 3e 61 7a 56 61 72 return p->azVar
1be65 5b 69 2d 31 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 [i-1];.}../*.**
1be66 47 69 76 65 6e 20 61 20 77 69 6c 64 63 61 72 64 Given a wildcard
1be67 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 2c parameter name,
1be68 20 72 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 return the inde
1be69 78 20 6f 66 20 74 68 65 20 76 61 72 69 61 62 6c x of the variabl
1be6a 65 0a 2a 2a 20 77 69 74 68 20 74 68 61 74 20 6e e.** with that n
1be6b 61 6d 65 2e 20 20 49 66 20 74 68 65 72 65 20 69 ame. If there i
1be6c 73 20 6e 6f 20 76 61 72 69 61 62 6c 65 20 77 69 s no variable wi
1be6d 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e 61 6d th the given nam
1be6e 65 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 0a e,.** return 0..
1be6f 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1be70 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 t sqlite3_bind_p
1be71 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 73 arameter_index(s
1be72 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1be73 6d 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a mt, const char *
1be74 7a 4e 61 6d 65 29 7b 0a 20 20 56 64 62 65 20 2a zName){. Vdbe *
1be75 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 p = (Vdbe*)pStmt
1be76 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 ;. int i;. if(
1be77 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 p==0 ){. ret
1be78 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 63 72 65 urn 0;. }. cre
1be79 61 74 65 56 61 72 4d 61 70 28 70 29 3b 20 0a 20 ateVarMap(p); .
1be7a 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 if( zName ){.
1be7b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e for(i=0; i<p->
1be7c 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nVar; i++){.
1be7d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 const char *z
1be7e 3d 20 70 2d 3e 61 7a 56 61 72 5b 69 5d 3b 0a 20 = p->azVar[i];.
1be7f 20 20 20 20 20 69 66 28 20 7a 20 26 26 20 73 74 if( z && st
1be80 72 63 6d 70 28 7a 2c 7a 4e 61 6d 65 29 3d 3d 30 rcmp(z,zName)==0
1be81 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
1be82 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d 0a rn i+1;. }.
1be83 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1be84 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 rn 0;.}../*.** T
1be85 72 61 6e 73 66 65 72 20 61 6c 6c 20 62 69 6e 64 ransfer all bind
1be86 69 6e 67 73 20 66 72 6f 6d 20 74 68 65 20 66 69 ings from the fi
1be87 72 73 74 20 73 74 61 74 65 6d 65 6e 74 20 6f 76 rst statement ov
1be88 65 72 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 er to the second
1be89 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1be8a 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 ATE int sqlite3T
1be8b 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 28 ransferBindings(
1be8c 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 sqlite3_stmt *pF
1be8d 72 6f 6d 53 74 6d 74 2c 20 73 71 6c 69 74 65 33 romStmt, sqlite3
1be8e 5f 73 74 6d 74 20 2a 70 54 6f 53 74 6d 74 29 7b _stmt *pToStmt){
1be8f 0a 20 20 56 64 62 65 20 2a 70 46 72 6f 6d 20 3d . Vdbe *pFrom =
1be90 20 28 56 64 62 65 2a 29 70 46 72 6f 6d 53 74 6d (Vdbe*)pFromStm
1be91 74 3b 0a 20 20 56 64 62 65 20 2a 70 54 6f 20 3d t;. Vdbe *pTo =
1be92 20 28 56 64 62 65 2a 29 70 54 6f 53 74 6d 74 3b (Vdbe*)pToStmt;
1be93 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 . int i;. asse
1be94 72 74 28 20 70 54 6f 2d 3e 64 62 3d 3d 70 46 72 rt( pTo->db==pFr
1be95 6f 6d 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 om->db );. asse
1be96 72 74 28 20 70 54 6f 2d 3e 6e 56 61 72 3d 3d 70 rt( pTo->nVar==p
1be97 46 72 6f 6d 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 From->nVar );.
1be98 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
1be99 74 65 72 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 ter(pTo->db->mut
1be9a 65 78 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 ex);. for(i=0;
1be9b 69 3c 70 46 72 6f 6d 2d 3e 6e 56 61 72 3b 20 69 i<pFrom->nVar; i
1be9c 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ++){. sqlite3
1be9d 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 70 54 6f VdbeMemMove(&pTo
1be9e 2d 3e 61 56 61 72 5b 69 5d 2c 20 26 70 46 72 6f ->aVar[i], &pFro
1be9f 6d 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 7d m->aVar[i]);. }
1bea0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
1bea1 5f 6c 65 61 76 65 28 70 54 6f 2d 3e 64 62 2d 3e _leave(pTo->db->
1bea2 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e mutex);. return
1bea3 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 SQLITE_OK;.}..#
1bea4 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1bea5 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a IT_DEPRECATED./*
1bea6 0a 2a 2a 20 44 65 70 72 65 63 61 74 65 64 20 65 .** Deprecated e
1bea7 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 61 63 xternal interfac
1bea8 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f 63 6f 72 e. Internal/cor
1bea9 65 20 53 51 4c 69 74 65 20 63 6f 64 65 0a 2a 2a e SQLite code.**
1beaa 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 71 6c should call sql
1beab 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64 ite3TransferBind
1beac 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 49 73 20 69 ings..**.** Is i
1bead 73 20 6d 69 73 75 73 65 20 74 6f 20 63 61 6c 6c s misuse to call
1beae 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 this routine wi
1beaf 74 68 20 73 74 61 74 65 6d 65 6e 74 73 20 66 72 th statements fr
1beb0 6f 6d 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 om different.**
1beb1 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1beb2 69 6f 6e 73 2e 20 20 42 75 74 20 61 73 20 74 68 ions. But as th
1beb3 69 73 20 69 73 20 61 20 64 65 70 72 65 63 61 74 is is a deprecat
1beb4 65 64 20 69 6e 74 65 72 66 61 63 65 2c 20 77 65 ed interface, we
1beb5 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 6f 74 .** will not bot
1beb6 68 65 72 20 74 6f 20 63 68 65 63 6b 20 66 6f 72 her to check for
1beb7 20 74 68 61 74 20 63 6f 6e 64 69 74 69 6f 6e 2e that condition.
1beb8 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 74 77 .**.** If the tw
1beb9 6f 20 73 74 61 74 65 6d 65 6e 74 73 20 63 6f 6e o statements con
1beba 74 61 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 tain a different
1bebb 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 6e 64 69 number of bindi
1bebc 6e 67 73 2c 20 74 68 65 6e 0a 2a 2a 20 61 6e 20 ngs, then.** an
1bebd 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 20 SQLITE_ERROR is
1bebe 72 65 74 75 72 6e 65 64 2e 20 20 4e 6f 74 68 69 returned. Nothi
1bebf 6e 67 20 65 6c 73 65 20 63 61 6e 20 67 6f 20 77 ng else can go w
1bec0 72 6f 6e 67 2c 20 73 6f 20 6f 74 68 65 72 77 69 rong, so otherwi
1bec1 73 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 se.** SQLITE_OK
1bec2 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a is returned..*/.
1bec3 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1bec4 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f qlite3_transfer_
1bec5 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 bindings(sqlite3
1bec6 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 74 6d 74 _stmt *pFromStmt
1bec7 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a , sqlite3_stmt *
1bec8 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 pToStmt){. Vdbe
1bec9 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64 62 65 2a *pFrom = (Vdbe*
1beca 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20 20 56 64 )pFromStmt;. Vd
1becb 62 65 20 2a 70 54 6f 20 3d 20 28 56 64 62 65 2a be *pTo = (Vdbe*
1becc 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69 66 28 20 )pToStmt;. if(
1becd 70 46 72 6f 6d 2d 3e 6e 56 61 72 21 3d 70 54 6f pFrom->nVar!=pTo
1bece 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 72 65 ->nVar ){. re
1becf 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
1bed0 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 54 6f R;. }. if( pTo
1bed1 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 26 26 ->isPrepareV2 &&
1bed2 20 70 54 6f 2d 3e 65 78 70 6d 61 73 6b 20 29 7b pTo->expmask ){
1bed3 0a 20 20 20 20 70 54 6f 2d 3e 65 78 70 69 72 65 . pTo->expire
1bed4 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 d = 1;. }. if(
1bed5 20 70 46 72 6f 6d 2d 3e 69 73 50 72 65 70 61 72 pFrom->isPrepar
1bed6 65 56 32 20 26 26 20 70 46 72 6f 6d 2d 3e 65 78 eV2 && pFrom->ex
1bed7 70 6d 61 73 6b 20 29 7b 0a 20 20 20 20 70 46 72 pmask ){. pFr
1bed8 6f 6d 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b om->expired = 1;
1bed9 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 . }. return sq
1beda 6c 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e lite3TransferBin
1bedb 64 69 6e 67 73 28 70 46 72 6f 6d 53 74 6d 74 2c dings(pFromStmt,
1bedc 20 70 54 6f 53 74 6d 74 29 3b 0a 7d 0a 23 65 6e pToStmt);.}.#en
1bedd 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 dif../*.** Retur
1bede 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 64 n the sqlite3* d
1bedf 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 atabase handle t
1bee0 6f 20 77 68 69 63 68 20 74 68 65 20 70 72 65 70 o which the prep
1bee1 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 67 ared statement g
1bee2 69 76 65 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 61 iven.** in the a
1bee3 72 67 75 6d 65 6e 74 20 62 65 6c 6f 6e 67 73 2e rgument belongs.
1bee4 20 20 54 68 69 73 20 69 73 20 74 68 65 20 73 61 This is the sa
1bee5 6d 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 me database hand
1bee6 6c 65 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 74 le that was.** t
1bee7 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
1bee8 74 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 t to the sqlite3
1bee9 5f 70 72 65 70 61 72 65 28 29 20 74 68 61 74 20 _prepare() that
1beea 77 61 73 20 75 73 65 64 20 74 6f 20 63 72 65 61 was used to crea
1beeb 74 65 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 6d te.** the statem
1beec 65 6e 74 20 69 6e 20 74 68 65 20 66 69 72 73 74 ent in the first
1beed 20 70 6c 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 place..*/.SQLIT
1beee 45 5f 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 E_API sqlite3 *s
1beef 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 qlite3_db_handle
1bef0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1bef1 53 74 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 Stmt){. return
1bef2 70 53 74 6d 74 20 3f 20 28 28 56 64 62 65 2a 29 pStmt ? ((Vdbe*)
1bef3 70 53 74 6d 74 29 2d 3e 64 62 20 3a 20 30 3b 0a pStmt)->db : 0;.
1bef4 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1bef5 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
1bef6 20 6e 65 78 74 20 70 72 65 70 61 72 65 64 20 73 next prepared s
1bef7 74 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20 70 tatement after p
1bef8 53 74 6d 74 20 61 73 73 6f 63 69 61 74 65 64 0a Stmt associated.
1bef9 2a 2a 20 77 69 74 68 20 64 61 74 61 62 61 73 65 ** with database
1befa 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 44 62 2e connection pDb.
1befb 20 20 49 66 20 70 53 74 6d 74 20 69 73 20 4e 55 If pStmt is NU
1befc 4c 4c 2c 20 72 65 74 75 72 6e 20 74 68 65 20 66 LL, return the f
1befd 69 72 73 74 0a 2a 2a 20 70 72 65 70 61 72 65 64 irst.** prepared
1befe 20 73 74 61 74 65 6d 65 6e 74 20 66 6f 72 20 74 statement for t
1beff 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
1bf00 65 63 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 ection. Return
1bf01 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 0a 2a 2a NULL if there.**
1bf02 20 61 72 65 20 6e 6f 20 6d 6f 72 65 2e 0a 2a 2f are no more..*/
1bf03 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
1bf04 74 65 33 5f 73 74 6d 74 20 2a 73 71 6c 69 74 65 te3_stmt *sqlite
1bf05 33 5f 6e 65 78 74 5f 73 74 6d 74 28 73 71 6c 69 3_next_stmt(sqli
1bf06 74 65 33 20 2a 70 44 62 2c 20 73 71 6c 69 74 65 te3 *pDb, sqlite
1bf07 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 3_stmt *pStmt){.
1bf08 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a sqlite3_stmt *
1bf09 70 4e 65 78 74 3b 0a 20 20 73 71 6c 69 74 65 33 pNext;. sqlite3
1bf0a 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 44 62 _mutex_enter(pDb
1bf0b 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 ->mutex);. if(
1bf0c 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 pStmt==0 ){.
1bf0d 70 4e 65 78 74 20 3d 20 28 73 71 6c 69 74 65 33 pNext = (sqlite3
1bf0e 5f 73 74 6d 74 2a 29 70 44 62 2d 3e 70 56 64 62 _stmt*)pDb->pVdb
1bf0f 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 e;. }else{.
1bf10 70 4e 65 78 74 20 3d 20 28 73 71 6c 69 74 65 33 pNext = (sqlite3
1bf11 5f 73 74 6d 74 2a 29 28 28 56 64 62 65 2a 29 70 _stmt*)((Vdbe*)p
1bf12 53 74 6d 74 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 Stmt)->pNext;.
1bf13 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 }. sqlite3_mute
1bf14 78 5f 6c 65 61 76 65 28 70 44 62 2d 3e 6d 75 74 x_leave(pDb->mut
1bf15 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e ex);. return pN
1bf16 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ext;.}../*.** Re
1bf17 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f turn the value o
1bf18 66 20 61 20 73 74 61 74 75 73 20 63 6f 75 6e 74 f a status count
1bf19 65 72 20 66 6f 72 20 61 20 70 72 65 70 61 72 65 er for a prepare
1bf1a 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 d statement.*/.S
1bf1b 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1bf1c 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 lite3_stmt_statu
1bf1d 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a s(sqlite3_stmt *
1bf1e 70 53 74 6d 74 2c 20 69 6e 74 20 6f 70 2c 20 69 pStmt, int op, i
1bf1f 6e 74 20 72 65 73 65 74 46 6c 61 67 29 7b 0a 20 nt resetFlag){.
1bf20 20 56 64 62 65 20 2a 70 56 64 62 65 20 3d 20 28 Vdbe *pVdbe = (
1bf21 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69 Vdbe*)pStmt;. i
1bf22 6e 74 20 76 20 3d 20 70 56 64 62 65 2d 3e 61 43 nt v = pVdbe->aC
1bf23 6f 75 6e 74 65 72 5b 6f 70 2d 31 5d 3b 0a 20 20 ounter[op-1];.
1bf24 69 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 20 if( resetFlag )
1bf25 70 56 64 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b pVdbe->aCounter[
1bf26 6f 70 2d 31 5d 20 3d 20 30 3b 0a 20 20 72 65 74 op-1] = 0;. ret
1bf27 75 72 6e 20 76 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a urn v;.}../*****
1bf28 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
1bf29 20 76 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a vdbeapi.c *****
1bf2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf2c 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
1bf2d 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
1bf2e 66 69 6c 65 20 76 64 62 65 2e 63 20 2a 2a 2a 2a file vdbe.c ****
1bf2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf31 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
1bf32 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
1bf33 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
1bf34 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
1bf35 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
1bf36 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
1bf37 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
1bf38 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
1bf39 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
1bf3a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
1bf3b 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
1bf3c 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
1bf3d 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
1bf3e 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
1bf3f 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
1bf40 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
1bf41 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
1bf42 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
1bf43 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
1bf44 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
1bf45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf49 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 .** The code in
1bf4a 74 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d this file implem
1bf4b 65 6e 74 73 20 65 78 65 63 75 74 69 6f 6e 20 6d ents execution m
1bf4c 65 74 68 6f 64 20 6f 66 20 74 68 65 20 0a 2a 2a ethod of the .**
1bf4d 20 56 69 72 74 75 61 6c 20 44 61 74 61 62 61 73 Virtual Databas
1bf4e 65 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29 2e e Engine (VDBE).
1bf4f 20 20 41 20 73 65 70 61 72 61 74 65 20 66 69 6c A separate fil
1bf50 65 20 28 22 76 64 62 65 61 75 78 2e 63 22 29 0a e ("vdbeaux.c").
1bf51 2a 2a 20 68 61 6e 64 6c 65 73 20 68 6f 75 73 65 ** handles house
1bf52 6b 65 65 70 69 6e 67 20 64 65 74 61 69 6c 73 20 keeping details
1bf53 73 75 63 68 20 61 73 20 63 72 65 61 74 69 6e 67 such as creating
1bf54 20 61 6e 64 20 64 65 6c 65 74 69 6e 67 0a 2a 2a and deleting.**
1bf55 20 56 44 42 45 20 69 6e 73 74 61 6e 63 65 73 2e VDBE instances.
1bf56 20 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 73 This file is s
1bf57 6f 6c 65 6c 79 20 69 6e 74 65 72 65 73 74 65 64 olely interested
1bf58 20 69 6e 20 65 78 65 63 75 74 69 6e 67 0a 2a 2a in executing.**
1bf59 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61 the VDBE progra
1bf5a 6d 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 m..**.** In the
1bf5b 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 61 external interfa
1bf5c 63 65 2c 20 61 6e 20 22 73 71 6c 69 74 65 33 5f ce, an "sqlite3_
1bf5d 73 74 6d 74 2a 22 20 69 73 20 61 6e 20 6f 70 61 stmt*" is an opa
1bf5e 71 75 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 que pointer.** t
1bf5f 6f 20 61 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 o a VDBE..**.**
1bf60 54 68 65 20 53 51 4c 20 70 61 72 73 65 72 20 67 The SQL parser g
1bf61 65 6e 65 72 61 74 65 73 20 61 20 70 72 6f 67 72 enerates a progr
1bf62 61 6d 20 77 68 69 63 68 20 69 73 20 74 68 65 6e am which is then
1bf63 20 65 78 65 63 75 74 65 64 20 62 79 0a 2a 2a 20 executed by.**
1bf64 74 68 65 20 56 44 42 45 20 74 6f 20 64 6f 20 74 the VDBE to do t
1bf65 68 65 20 77 6f 72 6b 20 6f 66 20 74 68 65 20 53 he work of the S
1bf66 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 56 QL statement. V
1bf67 44 42 45 20 70 72 6f 67 72 61 6d 73 20 61 72 65 DBE programs are
1bf68 20 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 69 6e 20 .** similar in
1bf69 66 6f 72 6d 20 74 6f 20 61 73 73 65 6d 62 6c 79 form to assembly
1bf6a 20 6c 61 6e 67 75 61 67 65 2e 20 20 54 68 65 20 language. The
1bf6b 70 72 6f 67 72 61 6d 20 63 6f 6e 73 69 73 74 73 program consists
1bf6c 20 6f 66 0a 2a 2a 20 61 20 6c 69 6e 65 61 72 20 of.** a linear
1bf6d 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 70 65 72 sequence of oper
1bf6e 61 74 69 6f 6e 73 2e 20 20 45 61 63 68 20 6f 70 ations. Each op
1bf6f 65 72 61 74 69 6f 6e 20 68 61 73 20 61 6e 20 6f eration has an o
1bf70 70 63 6f 64 65 20 0a 2a 2a 20 61 6e 64 20 35 20 pcode .** and 5
1bf71 6f 70 65 72 61 6e 64 73 2e 20 20 4f 70 65 72 61 operands. Opera
1bf72 6e 64 73 20 50 31 2c 20 50 32 2c 20 61 6e 64 20 nds P1, P2, and
1bf73 50 33 20 61 72 65 20 69 6e 74 65 67 65 72 73 2e P3 are integers.
1bf74 20 20 4f 70 65 72 61 6e 64 20 50 34 20 0a 2a 2a Operand P4 .**
1bf75 20 69 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 is a null-termi
1bf76 6e 61 74 65 64 20 73 74 72 69 6e 67 2e 20 20 4f nated string. O
1bf77 70 65 72 61 6e 64 20 50 35 20 69 73 20 61 6e 20 perand P5 is an
1bf78 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63 74 unsigned charact
1bf79 65 72 2e 0a 2a 2a 20 46 65 77 20 6f 70 63 6f 64 er..** Few opcod
1bf7a 65 73 20 75 73 65 20 61 6c 6c 20 35 20 6f 70 65 es use all 5 ope
1bf7b 72 61 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d rands..**.** Com
1bf7c 70 75 74 61 74 69 6f 6e 20 72 65 73 75 6c 74 73 putation results
1bf7d 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61 are stored on a
1bf7e 20 73 65 74 20 6f 66 20 72 65 67 69 73 74 65 72 set of register
1bf7f 73 20 6e 75 6d 62 65 72 65 64 20 62 65 67 69 6e s numbered begin
1bf80 6e 69 6e 67 0a 2a 2a 20 77 69 74 68 20 31 20 61 ning.** with 1 a
1bf81 6e 64 20 67 6f 69 6e 67 20 75 70 20 74 6f 20 56 nd going up to V
1bf82 64 62 65 2e 6e 4d 65 6d 2e 20 20 45 61 63 68 20 dbe.nMem. Each
1bf83 72 65 67 69 73 74 65 72 20 63 61 6e 20 73 74 6f register can sto
1bf84 72 65 0a 2a 2a 20 65 69 74 68 65 72 20 61 6e 20 re.** either an
1bf85 69 6e 74 65 67 65 72 2c 20 61 20 6e 75 6c 6c 2d integer, a null-
1bf86 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e terminated strin
1bf87 67 2c 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f g, a floating po
1bf88 69 6e 74 0a 2a 2a 20 6e 75 6d 62 65 72 2c 20 6f int.** number, o
1bf89 72 20 74 68 65 20 53 51 4c 20 22 4e 55 4c 4c 22 r the SQL "NULL"
1bf8a 20 76 61 6c 75 65 2e 20 20 41 6e 20 69 6d 70 6c value. An impl
1bf8b 69 63 69 74 20 63 6f 6e 76 65 72 73 69 6f 6e 20 icit conversion
1bf8c 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 74 79 70 65 from one.** type
1bf8d 20 74 6f 20 74 68 65 20 6f 74 68 65 72 20 6f 63 to the other oc
1bf8e 63 75 72 73 20 61 73 20 6e 65 63 65 73 73 61 72 curs as necessar
1bf8f 79 2e 0a 2a 2a 20 0a 2a 2a 20 4d 6f 73 74 20 6f y..** .** Most o
1bf90 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 f the code in th
1bf91 69 73 20 66 69 6c 65 20 69 73 20 74 61 6b 65 6e is file is taken
1bf92 20 75 70 20 62 79 20 74 68 65 20 73 71 6c 69 74 up by the sqlit
1bf93 65 33 56 64 62 65 45 78 65 63 28 29 0a 2a 2a 20 e3VdbeExec().**
1bf94 66 75 6e 63 74 69 6f 6e 20 77 68 69 63 68 20 64 function which d
1bf95 6f 65 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 oes the work of
1bf96 69 6e 74 65 72 70 72 65 74 69 6e 67 20 61 20 56 interpreting a V
1bf97 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 DBE program..**
1bf98 42 75 74 20 6f 74 68 65 72 20 72 6f 75 74 69 6e But other routin
1bf99 65 73 20 61 72 65 20 61 6c 73 6f 20 70 72 6f 76 es are also prov
1bf9a 69 64 65 64 20 74 6f 20 68 65 6c 70 20 69 6e 20 ided to help in
1bf9b 62 75 69 6c 64 69 6e 67 20 75 70 0a 2a 2a 20 61 building up.** a
1bf9c 20 70 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 program instruc
1bf9d 74 69 6f 6e 20 62 79 20 69 6e 73 74 72 75 63 74 tion by instruct
1bf9e 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69 6f ion..**.** Vario
1bf9f 75 73 20 73 63 72 69 70 74 73 20 73 63 61 6e 20 us scripts scan
1bfa0 74 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 this source file
1bfa1 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 6e in order to gen
1bfa2 65 72 61 74 65 20 48 54 4d 4c 0a 2a 2a 20 64 6f erate HTML.** do
1bfa3 63 75 6d 65 6e 74 61 74 69 6f 6e 2c 20 68 65 61 cumentation, hea
1bfa4 64 65 72 73 20 66 69 6c 65 73 2c 20 6f 72 20 6f ders files, or o
1bfa5 74 68 65 72 20 64 65 72 69 76 65 64 20 66 69 6c ther derived fil
1bfa6 65 73 2e 20 20 54 68 65 20 66 6f 72 6d 61 74 74 es. The formatt
1bfa7 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 65 20 63 6f ing.** of the co
1bfa8 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 de in this file
1bfa9 69 73 2c 20 74 68 65 72 65 66 6f 72 65 2c 20 69 is, therefore, i
1bfaa 6d 70 6f 72 74 61 6e 74 2e 20 20 53 65 65 20 6f mportant. See o
1bfab 74 68 65 72 20 63 6f 6d 6d 65 6e 74 73 0a 2a 2a ther comments.**
1bfac 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 66 6f in this file fo
1bfad 72 20 64 65 74 61 69 6c 73 2e 20 20 49 66 20 69 r details. If i
1bfae 6e 20 64 6f 75 62 74 2c 20 64 6f 20 6e 6f 74 20 n doubt, do not
1bfaf 64 65 76 69 61 74 65 20 66 72 6f 6d 20 65 78 69 deviate from exi
1bfb0 73 74 69 6e 67 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 sting.** comment
1bfb1 69 6e 67 20 61 6e 64 20 69 6e 64 65 6e 74 61 74 ing and indentat
1bfb2 69 6f 6e 20 70 72 61 63 74 69 63 65 73 20 77 68 ion practices wh
1bfb3 65 6e 20 63 68 61 6e 67 69 6e 67 20 6f 72 20 61 en changing or a
1bfb4 64 64 69 6e 67 20 63 6f 64 65 2e 0a 2a 2a 0a 2a dding code..**.*
1bfb5 2a 20 24 49 64 3a 20 76 64 62 65 2e 63 2c 76 20 * $Id: vdbe.c,v
1bfb6 31 2e 38 37 34 20 32 30 30 39 2f 30 37 2f 32 34 1.874 2009/07/24
1bfb7 20 31 37 3a 35 38 3a 35 33 20 64 61 6e 69 65 6c 17:58:53 daniel
1bfb8 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a k1977 Exp $.*/..
1bfb9 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
1bfba 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 ing global varia
1bfbb 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 ble is increment
1bfbc 65 64 20 65 76 65 72 79 20 74 69 6d 65 20 61 20 ed every time a
1bfbd 63 75 72 73 6f 72 0a 2a 2a 20 6d 6f 76 65 73 2c cursor.** moves,
1bfbe 20 65 69 74 68 65 72 20 62 79 20 74 68 65 20 4f either by the O
1bfbf 50 5f 53 65 65 6b 58 58 2c 20 4f 50 5f 4e 65 78 P_SeekXX, OP_Nex
1bfc0 74 2c 20 6f 72 20 4f 50 5f 50 72 65 76 20 6f 70 t, or OP_Prev op
1bfc1 63 6f 64 65 73 2e 20 20 54 68 65 20 74 65 73 74 codes. The test
1bfc2 0a 2a 2a 20 70 72 6f 63 65 64 75 72 65 73 20 75 .** procedures u
1bfc3 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 se this informat
1bfc4 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 ion to make sure
1bfc5 20 74 68 61 74 20 69 6e 64 69 63 65 73 20 61 72 that indices ar
1bfc6 65 0a 2a 2a 20 77 6f 72 6b 69 6e 67 20 63 6f 72 e.** working cor
1bfc7 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20 76 61 rectly. This va
1bfc8 72 69 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 riable has no fu
1bfc9 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 nction other tha
1bfca 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 n to.** help ver
1bfcb 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 ify the correct
1bfcc 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 operation of the
1bfcd 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 library..*/.#if
1bfce 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
1bfcf 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1bfd0 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f qlite3_search_co
1bfd1 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a unt = 0;.#endif.
1bfd2 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 ./*.** When this
1bfd3 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 global variable
1bfd4 20 69 73 20 70 6f 73 69 74 69 76 65 2c 20 69 74 is positive, it
1bfd5 20 67 65 74 73 20 64 65 63 72 65 6d 65 6e 74 65 gets decremente
1bfd6 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 0a 2a 2a d once before.**
1bfd7 20 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f each instructio
1bfd8 6e 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20 n in the VDBE.
1bfd9 57 68 65 6e 20 72 65 61 63 68 65 73 20 7a 65 72 When reaches zer
1bfda 6f 2c 20 74 68 65 20 75 31 2e 69 73 49 6e 74 65 o, the u1.isInte
1bfdb 72 72 75 70 74 65 64 0a 2a 2a 20 66 69 65 6c 64 rrupted.** field
1bfdc 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 20 of the sqlite3
1bfdd 73 74 72 75 63 74 75 72 65 20 69 73 20 73 65 74 structure is set
1bfde 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 69 6d in order to sim
1bfdf 75 6c 61 74 65 20 61 6e 64 20 69 6e 74 65 72 72 ulate and interr
1bfe0 75 70 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 upt..**.** This
1bfe1 66 61 63 69 6c 69 74 79 20 69 73 20 75 73 65 64 facility is used
1bfe2 20 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 for testing pur
1bfe3 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 poses only. It
1bfe4 64 6f 65 73 20 6e 6f 74 20 66 75 6e 63 74 69 6f does not functio
1bfe5 6e 0a 2a 2a 20 69 6e 20 61 6e 20 6f 72 64 69 6e n.** in an ordin
1bfe6 61 72 79 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 ary build..*/.#i
1bfe7 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
1bfe8 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1bfe9 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 sqlite3_interrup
1bfea 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e t_count = 0;.#en
1bfeb 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e dif../*.** The n
1bfec 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 ext global varia
1bfed 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 ble is increment
1bfee 65 64 20 65 61 63 68 20 74 79 70 65 20 74 68 65 ed each type the
1bfef 20 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65 0a OP_Sort opcode.
1bff0 2a 2a 20 69 73 20 65 78 65 63 75 74 65 64 2e 20 ** is executed.
1bff1 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 65 64 The test proced
1bff2 75 72 65 73 20 75 73 65 20 74 68 69 73 20 69 6e ures use this in
1bff3 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b formation to mak
1bff4 65 20 73 75 72 65 20 74 68 61 74 0a 2a 2a 20 73 e sure that.** s
1bff5 6f 72 74 69 6e 67 20 69 73 20 6f 63 63 75 72 72 orting is occurr
1bff6 69 6e 67 20 6f 72 20 6e 6f 74 20 6f 63 63 75 72 ing or not occur
1bff7 72 69 6e 67 20 61 74 20 61 70 70 72 6f 70 72 69 ring at appropri
1bff8 61 74 65 20 74 69 6d 65 73 2e 20 20 20 54 68 69 ate times. Thi
1bff9 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 68 61 s variable.** ha
1bffa 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 s no function ot
1bffb 68 65 72 20 74 68 61 6e 20 74 6f 20 68 65 6c 70 her than to help
1bffc 20 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 verify the corr
1bffd 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 ect operation of
1bffe 20 74 68 65 0a 2a 2a 20 6c 69 62 72 61 72 79 2e the.** library.
1bfff 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
1c000 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 E_TEST.SQLITE_AP
1c001 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6f I int sqlite3_so
1c002 72 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 rt_count = 0;.#e
1c003 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
1c004 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 next global vari
1c005 61 62 6c 65 20 72 65 63 6f 72 64 73 20 74 68 65 able records the
1c006 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 size of the lar
1c007 67 65 73 74 20 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a gest MEM_Blob.**
1c008 20 6f 72 20 4d 45 4d 5f 53 74 72 20 74 68 61 74 or MEM_Str that
1c009 20 68 61 73 20 62 65 65 6e 20 75 73 65 64 20 62 has been used b
1c00a 79 20 61 20 56 44 42 45 20 6f 70 63 6f 64 65 2e y a VDBE opcode.
1c00b 20 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 65 The test proce
1c00c 64 75 72 65 73 0a 2a 2a 20 75 73 65 20 74 68 69 dures.** use thi
1c00d 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f s information to
1c00e 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 make sure that
1c00f 74 68 65 20 7a 65 72 6f 2d 62 6c 6f 62 20 66 75 the zero-blob fu
1c010 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 2a 2a 20 69 nctionality.** i
1c011 73 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63 s working correc
1c012 74 6c 79 2e 20 20 20 54 68 69 73 20 76 61 72 69 tly. This vari
1c013 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 able has no func
1c014 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 tion other than
1c015 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 to.** help verif
1c016 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 y the correct op
1c017 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c eration of the l
1c018 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 ibrary..*/.#ifde
1c019 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
1c01a 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1c01b 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a ite3_max_blobsiz
1c01c 65 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 76 6f e = 0;.static vo
1c01d 69 64 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 id updateMaxBlob
1c01e 73 69 7a 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 size(Mem *p){.
1c01f 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 if( (p->flags &
1c020 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f (MEM_Str|MEM_Blo
1c021 62 29 29 21 3d 30 20 26 26 20 70 2d 3e 6e 3e 73 b))!=0 && p->n>s
1c022 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 qlite3_max_blobs
1c023 69 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ize ){. sqlit
1c024 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 e3_max_blobsize
1c025 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 65 = p->n;. }.}.#e
1c026 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
1c027 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 next global vari
1c028 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e able is incremen
1c029 74 65 64 20 65 61 63 68 20 74 79 70 65 20 74 68 ted each type th
1c02a 65 20 4f 50 5f 46 6f 75 6e 64 20 6f 70 63 6f 64 e OP_Found opcod
1c02b 65 0a 2a 2a 20 69 73 20 65 78 65 63 75 74 65 64 e.** is executed
1c02c 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 . This is used t
1c02d 6f 20 74 65 73 74 20 77 68 65 74 68 65 72 20 6f o test whether o
1c02e 72 20 6e 6f 74 20 74 68 65 20 66 6f 72 65 69 67 r not the foreig
1c02f 6e 20 6b 65 79 0a 2a 2a 20 6f 70 65 72 61 74 69 n key.** operati
1c030 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 75 on implemented u
1c031 73 69 6e 67 20 4f 50 5f 46 6b 49 73 5a 65 72 6f sing OP_FkIsZero
1c032 20 69 73 20 77 6f 72 6b 69 6e 67 2e 20 54 68 69 is working. Thi
1c033 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 68 61 s variable.** ha
1c034 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 s no function ot
1c035 68 65 72 20 74 68 61 6e 20 74 6f 20 68 65 6c 70 her than to help
1c036 20 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 verify the corr
1c037 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 ect operation of
1c038 20 74 68 65 0a 2a 2a 20 6c 69 62 72 61 72 79 2e the.** library.
1c039 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
1c03a 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 E_TEST.SQLITE_AP
1c03b 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 6f I int sqlite3_fo
1c03c 75 6e 64 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 und_count = 0;.#
1c03d 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 endif../*.** Tes
1c03e 74 20 61 20 72 65 67 69 73 74 65 72 20 74 6f 20 t a register to
1c03f 73 65 65 20 69 66 20 69 74 20 65 78 63 65 65 64 see if it exceed
1c040 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 s the current ma
1c041 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e ximum blob size.
1c042 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 2c 20 .** If it does,
1c043 72 65 63 6f 72 64 20 74 68 65 20 6e 65 77 20 6d record the new m
1c044 61 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 aximum blob size
1c045 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 ..*/.#if defined
1c046 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 (SQLITE_TEST) &&
1c047 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1c048 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 _OMIT_BUILTIN_TE
1c049 53 54 29 0a 23 20 64 65 66 69 6e 65 20 55 50 44 ST).# define UPD
1c04a 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 ATE_MAX_BLOBSIZE
1c04b 28 50 29 20 20 75 70 64 61 74 65 4d 61 78 42 6c (P) updateMaxBl
1c04c 6f 62 73 69 7a 65 28 50 29 0a 23 65 6c 73 65 0a obsize(P).#else.
1c04d 23 20 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f # define UPDATE_
1c04e 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 0a MAX_BLOBSIZE(P).
1c04f 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f #endif../*.** Co
1c050 6e 76 65 72 74 20 74 68 65 20 67 69 76 65 6e 20 nvert the given
1c051 72 65 67 69 73 74 65 72 20 69 6e 74 6f 20 61 20 register into a
1c052 73 74 72 69 6e 67 20 69 66 20 69 74 20 69 73 6e string if it isn
1c053 27 74 20 6f 6e 65 0a 2a 2a 20 61 6c 72 65 61 64 't one.** alread
1c054 79 2e 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 y. Return non-ze
1c055 72 6f 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 ro if a malloc()
1c056 20 66 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 fails..*/.#defi
1c057 6e 65 20 53 74 72 69 6e 67 69 66 79 28 50 2c 20 ne Stringify(P,
1c058 65 6e 63 29 20 5c 0a 20 20 20 69 66 28 28 28 50 enc) \. if(((P
1c059 29 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 )->flags&(MEM_St
1c05a 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 r|MEM_Blob))==0
1c05b 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 && sqlite3VdbeMe
1c05c 6d 53 74 72 69 6e 67 69 66 79 28 50 2c 65 6e 63 mStringify(P,enc
1c05d 29 29 20 5c 0a 20 20 20 20 20 7b 20 67 6f 74 6f )) \. { goto
1c05e 20 6e 6f 5f 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a no_mem; }../*.*
1c05f 2a 20 41 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 * An ephemeral s
1c060 74 72 69 6e 67 20 76 61 6c 75 65 20 28 73 69 67 tring value (sig
1c061 6e 69 66 69 65 64 20 62 79 20 74 68 65 20 4d 45 nified by the ME
1c062 4d 5f 45 70 68 65 6d 20 66 6c 61 67 29 20 63 6f M_Ephem flag) co
1c063 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e ntains.** a poin
1c064 74 65 72 20 74 6f 20 61 20 64 79 6e 61 6d 69 63 ter to a dynamic
1c065 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 ally allocated s
1c066 74 72 69 6e 67 20 77 68 65 72 65 20 73 6f 6d 65 tring where some
1c067 20 6f 74 68 65 72 20 65 6e 74 69 74 79 0a 2a 2a other entity.**
1c068 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 is responsible
1c069 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 for deallocating
1c06a 20 74 68 61 74 20 73 74 72 69 6e 67 2e 20 20 42 that string. B
1c06b 65 63 61 75 73 65 20 74 68 65 20 72 65 67 69 73 ecause the regis
1c06c 74 65 72 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 ter.** does not
1c06d 63 6f 6e 74 72 6f 6c 20 74 68 65 20 73 74 72 69 control the stri
1c06e 6e 67 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 ng, it might be
1c06f 64 65 6c 65 74 65 64 20 77 69 74 68 6f 75 74 20 deleted without
1c070 74 68 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 the register.**
1c071 6b 6e 6f 77 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a knowing it..**.*
1c072 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
1c073 6f 6e 76 65 72 74 73 20 61 6e 20 65 70 68 65 6d onverts an ephem
1c074 65 72 61 6c 20 73 74 72 69 6e 67 20 69 6e 74 6f eral string into
1c075 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 a dynamically a
1c076 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 73 74 72 69 llocated.** stri
1c077 6e 67 20 74 68 61 74 20 74 68 65 20 72 65 67 69 ng that the regi
1c078 73 74 65 72 20 69 74 73 65 6c 66 20 63 6f 6e 74 ster itself cont
1c079 72 6f 6c 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 rols. In other
1c07a 77 6f 72 64 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e words, it.** con
1c07b 76 65 72 74 73 20 61 6e 20 4d 45 4d 5f 45 70 68 verts an MEM_Eph
1c07c 65 6d 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 em string into a
1c07d 6e 20 4d 45 4d 5f 44 79 6e 20 73 74 72 69 6e 67 n MEM_Dyn string
1c07e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 65 65 ..*/.#define Dee
1c07f 70 68 65 6d 65 72 61 6c 69 7a 65 28 50 29 20 5c phemeralize(P) \
1c080 0a 20 20 20 69 66 28 20 28 28 50 29 2d 3e 66 6c . if( ((P)->fl
1c081 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29 21 3d ags&MEM_Ephem)!=
1c082 30 20 5c 0a 20 20 20 20 20 20 20 26 26 20 73 71 0 \. && sq
1c083 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 lite3VdbeMemMake
1c084 57 72 69 74 65 61 62 6c 65 28 50 29 20 29 7b 20 Writeable(P) ){
1c085 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f goto no_mem;}../
1c086 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 *.** Call sqlite
1c087 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 3VdbeMemExpandBl
1c088 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70 ob() on the supp
1c089 6c 69 65 64 20 76 61 6c 75 65 20 28 74 79 70 65 lied value (type
1c08a 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72 Mem*).** P if r
1c08b 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 equired..*/.#def
1c08c 69 6e 65 20 45 78 70 61 6e 64 42 6c 6f 62 28 50 ine ExpandBlob(P
1c08d 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d ) (((P)->flags&M
1c08e 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 EM_Zero)?sqlite3
1c08f 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f VdbeMemExpandBlo
1c090 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 b(P):0)../*.** A
1c091 72 67 75 6d 65 6e 74 20 70 4d 65 6d 20 70 6f 69 rgument pMem poi
1c092 6e 74 73 20 61 74 20 61 20 72 65 67 69 73 74 65 nts at a registe
1c093 72 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 70 r that will be p
1c094 61 73 73 65 64 20 74 6f 20 61 0a 2a 2a 20 75 73 assed to a.** us
1c095 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 er-defined funct
1c096 69 6f 6e 20 6f 72 20 72 65 74 75 72 6e 65 64 20 ion or returned
1c097 74 6f 20 74 68 65 20 75 73 65 72 20 61 73 20 74 to the user as t
1c098 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 71 he result of a q
1c099 75 65 72 79 2e 0a 2a 2a 20 54 68 69 73 20 72 6f uery..** This ro
1c09a 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 70 utine sets the p
1c09b 4d 65 6d 2d 3e 74 79 70 65 20 76 61 72 69 61 62 Mem->type variab
1c09c 6c 65 20 75 73 65 64 20 62 79 20 74 68 65 20 73 le used by the s
1c09d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 2a 28 29 qlite3_value_*()
1c09e 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2e 0a 2a .** routines..*
1c09f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1c0a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
1c0a1 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 4d 65 eMemStoreType(Me
1c0a2 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 m *pMem){. int
1c0a3 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c flags = pMem->fl
1c0a4 61 67 73 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 ags;. if( flags
1c0a5 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 & MEM_Null ){.
1c0a6 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
1c0a7 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 7d SQLITE_NULL;. }
1c0a8 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 . else if( flag
1c0a9 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 s & MEM_Int ){.
1c0aa 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
1c0ab 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a SQLITE_INTEGER;.
1c0ac 20 20 7d 0a 20 20 65 6c 73 65 20 69 66 28 20 66 }. else if( f
1c0ad 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 lags & MEM_Real
1c0ae 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 ){. pMem->typ
1c0af 65 20 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 e = SQLITE_FLOAT
1c0b0 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20 69 66 28 ;. }. else if(
1c0b1 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 flags & MEM_Str
1c0b2 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 ){. pMem->ty
1c0b3 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 pe = SQLITE_TEXT
1c0b4 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
1c0b5 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
1c0b6 54 45 5f 42 4c 4f 42 3b 0a 20 20 7d 0a 7d 0a 0a TE_BLOB;. }.}..
1c0b7 2f 2a 0a 2a 2a 20 50 72 6f 70 65 72 74 69 65 73 /*.** Properties
1c0b8 20 6f 66 20 6f 70 63 6f 64 65 73 2e 20 20 54 68 of opcodes. Th
1c0b9 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 e OPFLG_INITIALI
1c0ba 5a 45 52 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20 ZER macro is.**
1c0bb 63 72 65 61 74 65 64 20 62 79 20 6d 6b 6f 70 63 created by mkopc
1c0bc 6f 64 65 68 2e 61 77 6b 20 64 75 72 69 6e 67 20 odeh.awk during
1c0bd 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e 20 20 44 61 compilation. Da
1c0be 74 61 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a ta is obtained.*
1c0bf 2a 20 66 72 6f 6d 20 74 68 65 20 63 6f 6d 6d 65 * from the comme
1c0c0 6e 74 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 nts following th
1c0c1 65 20 22 63 61 73 65 20 4f 50 5f 78 78 78 78 3a e "case OP_xxxx:
1c0c2 22 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 0a " statements in.
1c0c3 2a 2a 20 74 68 69 73 20 66 69 6c 65 2e 20 20 0a ** this file. .
1c0c4 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 */.static const
1c0c5 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f 70 unsigned char op
1c0c6 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 5d 20 3d codeProperty[] =
1c0c7 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a OPFLG_INITIALIZ
1c0c8 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ER;../*.** Retur
1c0c9 6e 20 74 72 75 65 20 69 66 20 61 6e 20 6f 70 63 n true if an opc
1c0ca 6f 64 65 20 68 61 73 20 61 6e 79 20 6f 66 20 74 ode has any of t
1c0cb 68 65 20 4f 50 46 4c 47 5f 78 78 78 20 70 72 6f he OPFLG_xxx pro
1c0cc 70 65 72 74 69 65 73 0a 2a 2a 20 73 70 65 63 69 perties.** speci
1c0cd 66 69 65 64 20 62 79 20 6d 61 73 6b 2e 0a 2a 2f fied by mask..*/
1c0ce 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1c0cf 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4f int sqlite3VdbeO
1c0d0 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79 pcodeHasProperty
1c0d1 28 69 6e 74 20 6f 70 63 6f 64 65 2c 20 69 6e 74 (int opcode, int
1c0d2 20 6d 61 73 6b 29 7b 0a 20 20 61 73 73 65 72 74 mask){. assert
1c0d3 28 20 6f 70 63 6f 64 65 3e 30 20 26 26 20 6f 70 ( opcode>0 && op
1c0d4 63 6f 64 65 3c 28 69 6e 74 29 73 69 7a 65 6f 66 code<(int)sizeof
1c0d5 28 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 29 (opcodeProperty)
1c0d6 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 6f 70 );. return (op
1c0d7 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 6f 70 63 codeProperty[opc
1c0d8 6f 64 65 5d 26 6d 61 73 6b 29 21 3d 30 3b 0a 7d ode]&mask)!=0;.}
1c0d9 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 ../*.** Allocate
1c0da 20 56 64 62 65 43 75 72 73 6f 72 20 6e 75 6d 62 VdbeCursor numb
1c0db 65 72 20 69 43 75 72 2e 20 20 52 65 74 75 72 6e er iCur. Return
1c0dc 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 a pointer to it
1c0dd 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 0a 2a . Return NULL.*
1c0de 2a 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20 * if we run out
1c0df 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 of memory..*/.st
1c0e0 61 74 69 63 20 56 64 62 65 43 75 72 73 6f 72 20 atic VdbeCursor
1c0e1 2a 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 *allocateCursor(
1c0e2 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 . Vdbe *p,
1c0e3 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1c0e4 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 virtual machine
1c0e5 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 */. int iCur,
1c0e6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
1c0e7 64 65 78 20 6f 66 20 74 68 65 20 6e 65 77 20 56 dex of the new V
1c0e8 64 62 65 43 75 72 73 6f 72 20 2a 2f 0a 20 20 69 dbeCursor */. i
1c0e9 6e 74 20 6e 46 69 65 6c 64 2c 20 20 20 20 20 20 nt nField,
1c0ea 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1c0eb 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 f fields in the
1c0ec 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 2a table or index *
1c0ed 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 /. int iDb,
1c0ee 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 /* Whe
1c0ef 6e 20 64 61 74 61 62 61 73 65 20 74 68 65 20 63 n database the c
1c0f0 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 74 6f ursor belongs to
1c0f1 2c 20 6f 72 20 2d 31 20 2a 2f 0a 20 20 69 6e 74 , or -1 */. int
1c0f2 20 69 73 42 74 72 65 65 43 75 72 73 6f 72 20 20 isBtreeCursor
1c0f3 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 42 /* True for B
1c0f4 2d 54 72 65 65 2e 20 20 46 61 6c 73 65 20 66 6f -Tree. False fo
1c0f5 72 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 6f r pseudo-table o
1c0f6 72 20 76 74 61 62 20 2a 2f 0a 29 7b 0a 20 20 2f r vtab */.){. /
1c0f7 2a 20 46 69 6e 64 20 74 68 65 20 6d 65 6d 6f 72 * Find the memor
1c0f8 79 20 63 65 6c 6c 20 74 68 61 74 20 77 69 6c 6c y cell that will
1c0f9 20 62 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 be used to stor
1c0fa 65 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 e the blob of me
1c0fb 6d 6f 72 79 0a 20 20 2a 2a 20 72 65 71 75 69 72 mory. ** requir
1c0fc 65 64 20 66 6f 72 20 74 68 69 73 20 56 64 62 65 ed for this Vdbe
1c0fd 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 Cursor structure
1c0fe 2e 20 49 74 20 69 73 20 63 6f 6e 76 65 6e 69 65 . It is convenie
1c0ff 6e 74 20 74 6f 20 75 73 65 20 61 20 0a 20 20 2a nt to use a . *
1c100 2a 20 76 64 62 65 20 6d 65 6d 6f 72 79 20 63 65 * vdbe memory ce
1c101 6c 6c 20 74 6f 20 6d 61 6e 61 67 65 20 74 68 65 ll to manage the
1c102 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
1c103 6f 6e 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 on required for
1c104 61 0a 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f a. ** VdbeCurso
1c105 72 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 r structure for
1c106 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 the following re
1c107 61 73 6f 6e 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a asons:. **. **
1c108 20 20 20 2a 20 53 6f 6d 65 74 69 6d 65 73 20 63 * Sometimes c
1c109 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 72 ursor numbers ar
1c10a 65 20 75 73 65 64 20 66 6f 72 20 61 20 63 6f 75 e used for a cou
1c10b 70 6c 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74 ple of different
1c10c 0a 20 20 2a 2a 20 20 20 20 20 70 75 72 70 6f 73 . ** purpos
1c10d 65 73 20 69 6e 20 61 20 76 64 62 65 20 70 72 6f es in a vdbe pro
1c10e 67 72 61 6d 2e 20 54 68 65 20 64 69 66 66 65 72 gram. The differ
1c10f 65 6e 74 20 75 73 65 73 20 6d 69 67 68 74 20 72 ent uses might r
1c110 65 71 75 69 72 65 0a 20 20 2a 2a 20 20 20 20 20 equire. **
1c111 64 69 66 66 65 72 65 6e 74 20 73 69 7a 65 64 20 different sized
1c112 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 20 4d 65 6d allocations. Mem
1c113 6f 72 79 20 63 65 6c 6c 73 20 70 72 6f 76 69 64 ory cells provid
1c114 65 20 67 72 6f 77 61 62 6c 65 0a 20 20 2a 2a 20 e growable. **
1c115 20 20 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e allocations.
1c116 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 . **. ** * W
1c117 68 65 6e 20 75 73 69 6e 67 20 45 4e 41 42 4c 45 hen using ENABLE
1c118 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 _MEMORY_MANAGEME
1c119 4e 54 2c 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 NT, memory cell
1c11a 62 75 66 66 65 72 73 20 63 61 6e 0a 20 20 2a 2a buffers can. **
1c11b 20 20 20 20 20 62 65 20 66 72 65 65 64 20 6c 61 be freed la
1c11c 7a 69 6c 79 20 76 69 61 20 74 68 65 20 73 71 6c zily via the sql
1c11d 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d ite3_release_mem
1c11e 6f 72 79 28 29 20 41 50 49 2e 20 54 68 69 73 0a ory() API. This.
1c11f 20 20 2a 2a 20 20 20 20 20 6d 69 6e 69 6d 69 7a ** minimiz
1c120 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 es the number of
1c121 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 6d 61 malloc calls ma
1c122 64 65 20 62 79 20 74 68 65 20 73 79 73 74 65 6d de by the system
1c123 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 65 6d 6f .. **. ** Memo
1c124 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 63 75 72 ry cells for cur
1c125 73 6f 72 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 sors are allocat
1c126 65 64 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 ed at the top of
1c127 20 74 68 65 20 61 64 64 72 65 73 73 0a 20 20 2a the address. *
1c128 2a 20 73 70 61 63 65 2e 20 4d 65 6d 6f 72 79 20 * space. Memory
1c129 63 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 29 20 63 cell (p->nMem) c
1c12a 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 63 75 orresponds to cu
1c12b 72 73 6f 72 20 30 2e 20 53 70 61 63 65 20 66 6f rsor 0. Space fo
1c12c 72 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 31 20 r. ** cursor 1
1c12d 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 6d 65 is managed by me
1c12e 6d 6f 72 79 20 63 65 6c 6c 20 28 70 2d 3e 6e 4d mory cell (p->nM
1c12f 65 6d 2d 31 29 2c 20 65 74 63 2e 0a 20 20 2a 2f em-1), etc.. */
1c130 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 . Mem *pMem = &
1c131 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2d p->aMem[p->nMem-
1c132 69 43 75 72 5d 3b 0a 0a 20 20 69 6e 74 20 6e 42 iCur];.. int nB
1c133 79 74 65 3b 0a 20 20 56 64 62 65 43 75 72 73 6f yte;. VdbeCurso
1c134 72 20 2a 70 43 78 20 3d 20 30 3b 0a 20 20 6e 42 r *pCx = 0;. nB
1c135 79 74 65 20 3d 20 0a 20 20 20 20 20 20 73 69 7a yte = . siz
1c136 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 20 eof(VdbeCursor)
1c137 2b 20 0a 20 20 20 20 20 20 28 69 73 42 74 72 65 + . (isBtre
1c138 65 43 75 72 73 6f 72 3f 73 71 6c 69 74 65 33 42 eCursor?sqlite3B
1c139 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 28 29 treeCursorSize()
1c13a 3a 30 29 20 2b 20 0a 20 20 20 20 20 20 32 2a 6e :0) + . 2*n
1c13b 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75 33 32 Field*sizeof(u32
1c13c 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43 );.. assert( iC
1c13d 75 72 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b ur<p->nCursor );
1c13e 0a 20 20 69 66 28 20 70 2d 3e 61 70 43 73 72 5b . if( p->apCsr[
1c13f 69 43 75 72 5d 20 29 7b 0a 20 20 20 20 73 71 6c iCur] ){. sql
1c140 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 ite3VdbeFreeCurs
1c141 6f 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69 or(p, p->apCsr[i
1c142 43 75 72 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 70 Cur]);. p->ap
1c143 43 73 72 5b 69 43 75 72 5d 20 3d 20 30 3b 0a 20 Csr[iCur] = 0;.
1c144 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f }. if( SQLITE_
1c145 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 64 62 65 4d OK==sqlite3VdbeM
1c146 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 emGrow(pMem, nBy
1c147 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 70 2d te, 0) ){. p-
1c148 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 3d 20 70 >apCsr[iCur] = p
1c149 43 78 20 3d 20 28 56 64 62 65 43 75 72 73 6f 72 Cx = (VdbeCursor
1c14a 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 6d *)pMem->z;. m
1c14b 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 emset(pMem->z, 0
1c14c 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 70 43 , nByte);. pC
1c14d 78 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 x->iDb = iDb;.
1c14e 20 20 70 43 78 2d 3e 6e 46 69 65 6c 64 20 3d 20 pCx->nField =
1c14f 6e 46 69 65 6c 64 3b 0a 20 20 20 20 69 66 28 20 nField;. if(
1c150 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20 20 20 20 nField ){.
1c151 70 43 78 2d 3e 61 54 79 70 65 20 3d 20 28 75 33 pCx->aType = (u3
1c152 32 20 2a 29 26 70 4d 65 6d 2d 3e 7a 5b 73 69 7a 2 *)&pMem->z[siz
1c153 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 5d eof(VdbeCursor)]
1c154 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1c155 69 73 42 74 72 65 65 43 75 72 73 6f 72 20 29 7b isBtreeCursor ){
1c156 0a 20 20 20 20 20 20 70 43 78 2d 3e 70 43 75 72 . pCx->pCur
1c157 73 6f 72 20 3d 20 28 42 74 43 75 72 73 6f 72 2a sor = (BtCursor*
1c158 29 0a 20 20 20 20 20 20 20 20 20 20 26 70 4d 65 ). &pMe
1c159 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 28 56 64 62 65 m->z[sizeof(Vdbe
1c15a 43 75 72 73 6f 72 29 2b 32 2a 6e 46 69 65 6c 64 Cursor)+2*nField
1c15b 2a 73 69 7a 65 6f 66 28 75 33 32 29 5d 3b 0a 20 *sizeof(u32)];.
1c15c 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
1c15d 6e 20 70 43 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n pCx;.}../*.**
1c15e 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 61 Try to convert a
1c15f 20 76 61 6c 75 65 20 69 6e 74 6f 20 61 20 6e 75 value into a nu
1c160 6d 65 72 69 63 20 72 65 70 72 65 73 65 6e 74 61 meric representa
1c161 74 69 6f 6e 20 69 66 20 77 65 20 63 61 6e 0a 2a tion if we can.*
1c162 2a 20 64 6f 20 73 6f 20 77 69 74 68 6f 75 74 20 * do so without
1c163 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74 loss of informat
1c164 69 6f 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 ion. In other w
1c165 6f 72 64 73 2c 20 69 66 20 74 68 65 20 73 74 72 ords, if the str
1c166 69 6e 67 0a 2a 2a 20 6c 6f 6f 6b 73 20 6c 69 6b ing.** looks lik
1c167 65 20 61 20 6e 75 6d 62 65 72 2c 20 63 6f 6e 76 e a number, conv
1c168 65 72 74 20 69 74 20 69 6e 74 6f 20 61 20 6e 75 ert it into a nu
1c169 6d 62 65 72 2e 20 20 49 66 20 69 74 20 64 6f 65 mber. If it doe
1c16a 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 6f 6b 20 6c 69 s not.** look li
1c16b 6b 65 20 61 20 6e 75 6d 62 65 72 2c 20 6c 65 61 ke a number, lea
1c16c 76 65 20 69 74 20 61 6c 6f 6e 65 2e 0a 2a 2f 0a ve it alone..*/.
1c16d 73 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 6c static void appl
1c16e 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 yNumericAffinity
1c16f 28 4d 65 6d 20 2a 70 52 65 63 29 7b 0a 20 20 69 (Mem *pRec){. i
1c170 66 28 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 20 f( (pRec->flags
1c171 26 20 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f & (MEM_Real|MEM_
1c172 49 6e 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 Int))==0 ){.
1c173 69 6e 74 20 72 65 61 6c 6e 75 6d 3b 0a 20 20 20 int realnum;.
1c174 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e sqlite3VdbeMemN
1c175 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 52 65 63 ulTerminate(pRec
1c176 29 3b 0a 20 20 20 20 69 66 28 20 28 70 52 65 63 );. if( (pRec
1c177 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 ->flags&MEM_Str)
1c178 0a 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c . && sql
1c179 69 74 65 33 49 73 4e 75 6d 62 65 72 28 70 52 65 ite3IsNumber(pRe
1c17a 63 2d 3e 7a 2c 20 26 72 65 61 6c 6e 75 6d 2c 20 c->z, &realnum,
1c17b 70 52 65 63 2d 3e 65 6e 63 29 20 29 7b 0a 20 20 pRec->enc) ){.
1c17c 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 i64 value;.
1c17d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1c17e 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 ChangeEncoding(p
1c17f 52 65 63 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 Rec, SQLITE_UTF8
1c180 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 72 65 );. if( !re
1c181 61 6c 6e 75 6d 20 26 26 20 73 71 6c 69 74 65 33 alnum && sqlite3
1c182 41 74 6f 69 36 34 28 70 52 65 63 2d 3e 7a 2c 20 Atoi64(pRec->z,
1c183 26 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 &value) ){.
1c184 20 20 20 70 52 65 63 2d 3e 75 2e 69 20 3d 20 76 pRec->u.i = v
1c185 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 20 4d 65 alue;. Me
1c186 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 52 65 mSetTypeFlag(pRe
1c187 63 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 c, MEM_Int);.
1c188 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1c189 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1c18a 52 65 61 6c 69 66 79 28 70 52 65 63 29 3b 0a 20 Realify(pRec);.
1c18b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1c18c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 .}../*.** Proces
1c18d 73 69 6e 67 20 69 73 20 64 65 74 65 72 6d 69 6e sing is determin
1c18e 65 20 62 79 20 74 68 65 20 61 66 66 69 6e 69 74 e by the affinit
1c18f 79 20 70 61 72 61 6d 65 74 65 72 3a 0a 2a 2a 0a y parameter:.**.
1c190 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e ** SQLITE_AFF_IN
1c191 54 45 47 45 52 3a 0a 2a 2a 20 53 51 4c 49 54 45 TEGER:.** SQLITE
1c192 5f 41 46 46 5f 52 45 41 4c 3a 0a 2a 2a 20 53 51 _AFF_REAL:.** SQ
1c193 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 LITE_AFF_NUMERIC
1c194 3a 0a 2a 2a 20 20 20 20 54 72 79 20 74 6f 20 63 :.** Try to c
1c195 6f 6e 76 65 72 74 20 70 52 65 63 20 74 6f 20 61 onvert pRec to a
1c196 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 n integer repres
1c197 65 6e 74 61 74 69 6f 6e 20 6f 72 20 61 20 0a 2a entation or a .*
1c198 2a 20 20 20 20 66 6c 6f 61 74 69 6e 67 2d 70 6f * floating-po
1c199 69 6e 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 int representati
1c19a 6f 6e 20 69 66 20 61 6e 20 69 6e 74 65 67 65 72 on if an integer
1c19b 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a representation.
1c19c 2a 2a 20 20 20 20 69 73 20 6e 6f 74 20 70 6f 73 ** is not pos
1c19d 73 69 62 6c 65 2e 20 20 4e 6f 74 65 20 74 68 61 sible. Note tha
1c19e 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 72 65 t the integer re
1c19f 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 73 0a presentation is.
1c1a0 2a 2a 20 20 20 20 61 6c 77 61 79 73 20 70 72 65 ** always pre
1c1a1 66 65 72 72 65 64 2c 20 65 76 65 6e 20 69 66 20 ferred, even if
1c1a2 74 68 65 20 61 66 66 69 6e 69 74 79 20 69 73 20 the affinity is
1c1a3 52 45 41 4c 2c 20 62 65 63 61 75 73 65 0a 2a 2a REAL, because.**
1c1a4 20 20 20 20 61 6e 20 69 6e 74 65 67 65 72 20 72 an integer r
1c1a5 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 73 epresentation is
1c1a6 20 6d 6f 72 65 20 73 70 61 63 65 20 65 66 66 69 more space effi
1c1a7 63 69 65 6e 74 20 6f 6e 20 64 69 73 6b 2e 0a 2a cient on disk..*
1c1a8 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f *.** SQLITE_AFF_
1c1a9 54 45 58 54 3a 0a 2a 2a 20 20 20 20 43 6f 6e 76 TEXT:.** Conv
1c1aa 65 72 74 20 70 52 65 63 20 74 6f 20 61 20 74 65 ert pRec to a te
1c1ab 78 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f xt representatio
1c1ac 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f n..**.** SQLITE_
1c1ad 41 46 46 5f 4e 4f 4e 45 3a 0a 2a 2a 20 20 20 20 AFF_NONE:.**
1c1ae 4e 6f 2d 6f 70 2e 20 20 70 52 65 63 20 69 73 20 No-op. pRec is
1c1af 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 unchanged..*/.st
1c1b0 61 74 69 63 20 76 6f 69 64 20 61 70 70 6c 79 41 atic void applyA
1c1b1 66 66 69 6e 69 74 79 28 0a 20 20 4d 65 6d 20 2a ffinity(. Mem *
1c1b2 70 52 65 63 2c 20 20 20 20 20 20 20 20 20 20 2f pRec, /
1c1b3 2a 20 54 68 65 20 76 61 6c 75 65 20 74 6f 20 61 * The value to a
1c1b4 70 70 6c 79 20 61 66 66 69 6e 69 74 79 20 74 6f pply affinity to
1c1b5 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e */. char affin
1c1b6 69 74 79 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 ity, /* The
1c1b7 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65 20 affinity to be
1c1b8 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 75 38 20 applied */. u8
1c1b9 65 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20 enc
1c1ba 20 2f 2a 20 55 73 65 20 74 68 69 73 20 74 65 78 /* Use this tex
1c1bb 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 29 7b t encoding */.){
1c1bc 0a 20 20 69 66 28 20 61 66 66 69 6e 69 74 79 3d . if( affinity=
1c1bd 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 =SQLITE_AFF_TEXT
1c1be 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 ){. /* Only
1c1bf 61 74 74 65 6d 70 74 20 74 68 65 20 63 6f 6e 76 attempt the conv
1c1c0 65 72 73 69 6f 6e 20 74 6f 20 54 45 58 54 20 69 ersion to TEXT i
1c1c1 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 69 6e f there is an in
1c1c2 74 65 67 65 72 20 6f 72 20 72 65 61 6c 0a 20 20 teger or real.
1c1c3 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 ** representat
1c1c4 69 6f 6e 20 28 62 6c 6f 62 20 61 6e 64 20 4e 55 ion (blob and NU
1c1c5 4c 4c 20 64 6f 20 6e 6f 74 20 67 65 74 20 63 6f LL do not get co
1c1c6 6e 76 65 72 74 65 64 29 20 62 75 74 20 6e 6f 20 nverted) but no
1c1c7 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65 string. ** re
1c1c8 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 20 20 presentation..
1c1c9 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 30 3d 3d */. if( 0==
1c1ca 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d (pRec->flags&MEM
1c1cb 5f 53 74 72 29 20 26 26 20 28 70 52 65 63 2d 3e _Str) && (pRec->
1c1cc 66 6c 61 67 73 26 28 4d 45 4d 5f 52 65 61 6c 7c flags&(MEM_Real|
1c1cd 4d 45 4d 5f 49 6e 74 29 29 20 29 7b 0a 20 20 20 MEM_Int)) ){.
1c1ce 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1c1cf 6d 53 74 72 69 6e 67 69 66 79 28 70 52 65 63 2c mStringify(pRec,
1c1d0 20 65 6e 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 enc);. }.
1c1d1 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 3d 20 pRec->flags &=
1c1d2 7e 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 ~(MEM_Real|MEM_I
1c1d3 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 nt);. }else if(
1c1d4 20 61 66 66 69 6e 69 74 79 21 3d 53 51 4c 49 54 affinity!=SQLIT
1c1d5 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 E_AFF_NONE ){.
1c1d6 20 20 61 73 73 65 72 74 28 20 61 66 66 69 6e 69 assert( affini
1c1d7 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49 ty==SQLITE_AFF_I
1c1d8 4e 54 45 47 45 52 20 7c 7c 20 61 66 66 69 6e 69 NTEGER || affini
1c1d9 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 ty==SQLITE_AFF_R
1c1da 45 41 4c 0a 20 20 20 20 20 20 20 20 20 20 20 20 EAL.
1c1db 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 || affinity==SQ
1c1dc 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 LITE_AFF_NUMERIC
1c1dd 20 29 3b 0a 20 20 20 20 61 70 70 6c 79 4e 75 6d );. applyNum
1c1de 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 52 65 ericAffinity(pRe
1c1df 63 29 3b 0a 20 20 20 20 69 66 28 20 70 52 65 63 c);. if( pRec
1c1e0 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 ->flags & MEM_Re
1c1e1 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 al ){. sqli
1c1e2 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 41 66 te3VdbeIntegerAf
1c1e3 66 69 6e 69 74 79 28 70 52 65 63 29 3b 0a 20 20 finity(pRec);.
1c1e4 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
1c1e5 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 Try to convert
1c1e6 74 68 65 20 74 79 70 65 20 6f 66 20 61 20 66 75 the type of a fu
1c1e7 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 nction argument
1c1e8 6f 72 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 or a result colu
1c1e9 6d 6e 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e 75 6d mn.** into a num
1c1ea 65 72 69 63 20 72 65 70 72 65 73 65 6e 74 61 74 eric representat
1c1eb 69 6f 6e 2e 20 20 55 73 65 20 65 69 74 68 65 72 ion. Use either
1c1ec 20 49 4e 54 45 47 45 52 20 6f 72 20 52 45 41 4c INTEGER or REAL
1c1ed 20 77 68 69 63 68 65 76 65 72 0a 2a 2a 20 69 73 whichever.** is
1c1ee 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 20 42 appropriate. B
1c1ef 75 74 20 6f 6e 6c 79 20 64 6f 20 74 68 65 20 63 ut only do the c
1c1f0 6f 6e 76 65 72 73 69 6f 6e 20 69 66 20 69 74 20 onversion if it
1c1f1 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74 68 is possible with
1c1f2 6f 75 74 0a 2a 2a 20 6c 6f 73 73 20 6f 66 20 69 out.** loss of i
1c1f3 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20 72 nformation and r
1c1f4 65 74 75 72 6e 20 74 68 65 20 72 65 76 69 73 65 eturn the revise
1c1f5 64 20 74 79 70 65 20 6f 66 20 74 68 65 20 61 72 d type of the ar
1c1f6 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 gument..**.** Th
1c1f7 69 73 20 69 73 20 61 6e 20 45 58 50 45 52 49 4d is is an EXPERIM
1c1f8 45 4e 54 41 4c 20 61 70 69 20 61 6e 64 20 69 73 ENTAL api and is
1c1f9 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e subject to chan
1c1fa 67 65 20 6f 72 20 72 65 6d 6f 76 61 6c 2e 0a 2a ge or removal..*
1c1fb 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1c1fc 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e sqlite3_value_n
1c1fd 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 umeric_type(sqli
1c1fe 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 te3_value *pVal)
1c1ff 7b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 {. Mem *pMem =
1c200 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 61 70 (Mem*)pVal;. ap
1c201 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 plyNumericAffini
1c202 74 79 28 70 4d 65 6d 29 3b 0a 20 20 73 71 6c 69 ty(pMem);. sqli
1c203 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 te3VdbeMemStoreT
1c204 79 70 65 28 70 4d 65 6d 29 3b 0a 20 20 72 65 74 ype(pMem);. ret
1c205 75 72 6e 20 70 4d 65 6d 2d 3e 74 79 70 65 3b 0a urn pMem->type;.
1c206 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 6f 72 74 65 }../*.** Exporte
1c207 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 61 70 70 d version of app
1c208 6c 79 41 66 66 69 6e 69 74 79 28 29 2e 20 54 68 lyAffinity(). Th
1c209 69 73 20 6f 6e 65 20 77 6f 72 6b 73 20 6f 6e 20 is one works on
1c20a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 sqlite3_value*,
1c20b 0a 2a 2a 20 6e 6f 74 20 74 68 65 20 69 6e 74 65 .** not the inte
1c20c 72 6e 61 6c 20 4d 65 6d 2a 20 74 79 70 65 2e 0a rnal Mem* type..
1c20d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1c20e 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 E void sqlite3Va
1c20f 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 lueApplyAffinity
1c210 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 (. sqlite3_valu
1c211 65 20 2a 70 56 61 6c 2c 20 0a 20 20 75 38 20 61 e *pVal, . u8 a
1c212 66 66 69 6e 69 74 79 2c 20 0a 20 20 75 38 20 65 ffinity, . u8 e
1c213 6e 63 0a 29 7b 0a 20 20 61 70 70 6c 79 41 66 66 nc.){. applyAff
1c214 69 6e 69 74 79 28 28 4d 65 6d 20 2a 29 70 56 61 inity((Mem *)pVa
1c215 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 l, affinity, enc
1c216 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c );.}..#ifdef SQL
1c217 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 ITE_DEBUG./*.**
1c218 57 72 69 74 65 20 61 20 6e 69 63 65 20 73 74 72 Write a nice str
1c219 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 ing representati
1c21a 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e on of the conten
1c21b 74 73 20 6f 66 20 63 65 6c 6c 20 70 4d 65 6d 0a ts of cell pMem.
1c21c 2a 2a 20 69 6e 74 6f 20 62 75 66 66 65 72 20 7a ** into buffer z
1c21d 42 75 66 2c 20 6c 65 6e 67 74 68 20 6e 42 75 66 Buf, length nBuf
1c21e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1c21f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1c220 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 VdbeMemPrettyPri
1c221 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68 nt(Mem *pMem, ch
1c222 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 63 68 61 ar *zBuf){. cha
1c223 72 20 2a 7a 43 73 72 20 3d 20 7a 42 75 66 3b 0a r *zCsr = zBuf;.
1c224 20 20 69 6e 74 20 66 20 3d 20 70 4d 65 6d 2d 3e int f = pMem->
1c225 66 6c 61 67 73 3b 0a 0a 20 20 73 74 61 74 69 63 flags;.. static
1c226 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e const char *con
1c227 73 74 20 65 6e 63 6e 61 6d 65 73 5b 5d 20 3d 20 st encnames[] =
1c228 7b 22 28 58 29 22 2c 20 22 28 38 29 22 2c 20 22 {"(X)", "(8)", "
1c229 28 31 36 4c 45 29 22 2c 20 22 28 31 36 42 45 29 (16LE)", "(16BE)
1c22a 22 7d 3b 0a 0a 20 20 69 66 28 20 66 26 4d 45 4d "};.. if( f&MEM
1c22b 5f 42 6c 6f 62 20 29 7b 0a 20 20 20 20 69 6e 74 _Blob ){. int
1c22c 20 69 3b 0a 20 20 20 20 63 68 61 72 20 63 3b 0a i;. char c;.
1c22d 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f if( f & MEM_
1c22e 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 63 20 3d Dyn ){. c =
1c22f 20 27 7a 27 3b 0a 20 20 20 20 20 20 61 73 73 65 'z';. asse
1c230 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 rt( (f & (MEM_St
1c231 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 atic|MEM_Ephem))
1c232 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 ==0 );. }else
1c233 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 53 74 61 if( f & MEM_Sta
1c234 74 69 63 20 29 7b 0a 20 20 20 20 20 20 63 20 3d tic ){. c =
1c235 20 27 74 27 3b 0a 20 20 20 20 20 20 61 73 73 65 't';. asse
1c236 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 44 79 rt( (f & (MEM_Dy
1c237 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 n|MEM_Ephem))==0
1c238 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 );. }else if
1c239 28 20 66 20 26 20 4d 45 4d 5f 45 70 68 65 6d 20 ( f & MEM_Ephem
1c23a 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 65 27 ){. c = 'e'
1c23b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1c23c 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 (f & (MEM_Static
1c23d 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20 29 3b |MEM_Dyn))==0 );
1c23e 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1c23f 20 20 63 20 3d 20 27 73 27 3b 0a 20 20 20 20 7d c = 's';. }
1c240 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e .. sqlite3_sn
1c241 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 printf(100, zCsr
1c242 2c 20 22 25 63 22 2c 20 63 29 3b 0a 20 20 20 20 , "%c", c);.
1c243 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 zCsr += sqlite3S
1c244 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 trlen30(zCsr);.
1c245 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1c246 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 ntf(100, zCsr, "
1c247 25 64 5b 22 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a %d[", pMem->n);.
1c248 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 zCsr += sqli
1c249 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 te3Strlen30(zCsr
1c24a 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 );. for(i=0;
1c24b 69 3c 31 36 20 26 26 20 69 3c 70 4d 65 6d 2d 3e i<16 && i<pMem->
1c24c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 n; i++){. s
1c24d 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1c24e 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 30 32 58 100, zCsr, "%02X
1c24f 22 2c 20 28 28 69 6e 74 29 70 4d 65 6d 2d 3e 7a ", ((int)pMem->z
1c250 5b 69 5d 20 26 20 30 78 46 46 29 29 3b 0a 20 20 [i] & 0xFF));.
1c251 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 zCsr += sqli
1c252 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 te3Strlen30(zCsr
1c253 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 );. }. for
1c254 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c (i=0; i<16 && i<
1c255 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 pMem->n; i++){.
1c256 20 20 20 20 20 63 68 61 72 20 7a 20 3d 20 70 4d char z = pM
1c257 65 6d 2d 3e 7a 5b 69 5d 3b 0a 20 20 20 20 20 20 em->z[i];.
1c258 69 66 28 20 7a 3c 33 32 20 7c 7c 20 7a 3e 31 32 if( z<32 || z>12
1c259 36 20 29 20 2a 7a 43 73 72 2b 2b 20 3d 20 27 2e 6 ) *zCsr++ = '.
1c25a 27 3b 0a 20 20 20 20 20 20 65 6c 73 65 20 2a 7a ';. else *z
1c25b 43 73 72 2b 2b 20 3d 20 7a 3b 0a 20 20 20 20 7d Csr++ = z;. }
1c25c 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e .. sqlite3_sn
1c25d 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 printf(100, zCsr
1c25e 2c 20 22 5d 25 73 22 2c 20 65 6e 63 6e 61 6d 65 , "]%s", encname
1c25f 73 5b 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 s[pMem->enc]);.
1c260 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 zCsr += sqlit
1c261 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 e3Strlen30(zCsr)
1c262 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 ;. if( f & ME
1c263 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 M_Zero ){.
1c264 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1c265 28 31 30 30 2c 20 7a 43 73 72 2c 22 2b 25 64 7a (100, zCsr,"+%dz
1c266 22 2c 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 29 ",pMem->u.nZero)
1c267 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 ;. zCsr +=
1c268 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1c269 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 zCsr);. }.
1c26a 20 2a 7a 43 73 72 20 3d 20 27 5c 30 27 3b 0a 20 *zCsr = '\0';.
1c26b 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d }else if( f & M
1c26c 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 69 6e EM_Str ){. in
1c26d 74 20 6a 2c 20 6b 3b 0a 20 20 20 20 7a 42 75 66 t j, k;. zBuf
1c26e 5b 30 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 69 [0] = ' ';. i
1c26f 66 28 20 66 20 26 20 4d 45 4d 5f 44 79 6e 20 29 f( f & MEM_Dyn )
1c270 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 {. zBuf[1]
1c271 3d 20 27 7a 27 3b 0a 20 20 20 20 20 20 61 73 73 = 'z';. ass
1c272 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 ert( (f & (MEM_S
1c273 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 tatic|MEM_Ephem)
1c274 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 )==0 );. }els
1c275 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 53 74 e if( f & MEM_St
1c276 61 74 69 63 20 29 7b 0a 20 20 20 20 20 20 7a 42 atic ){. zB
1c277 75 66 5b 31 5d 20 3d 20 27 74 27 3b 0a 20 20 20 uf[1] = 't';.
1c278 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 assert( (f &
1c279 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 (MEM_Dyn|MEM_Eph
1c27a 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d em))==0 );. }
1c27b 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d else if( f & MEM
1c27c 5f 45 70 68 65 6d 20 29 7b 0a 20 20 20 20 20 20 _Ephem ){.
1c27d 7a 42 75 66 5b 31 5d 20 3d 20 27 65 27 3b 0a 20 zBuf[1] = 'e';.
1c27e 20 20 20 20 20 61 73 73 65 72 74 28 20 28 66 20 assert( (f
1c27f 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 & (MEM_Static|ME
1c280 4d 5f 44 79 6e 29 29 3d 3d 30 20 29 3b 0a 20 20 M_Dyn))==0 );.
1c281 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a }else{. z
1c282 42 75 66 5b 31 5d 20 3d 20 27 73 27 3b 0a 20 20 Buf[1] = 's';.
1c283 20 20 7d 0a 20 20 20 20 6b 20 3d 20 32 3b 0a 20 }. k = 2;.
1c284 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1c285 6e 74 66 28 31 30 30 2c 20 26 7a 42 75 66 5b 6b ntf(100, &zBuf[k
1c286 5d 2c 20 22 25 64 22 2c 20 70 4d 65 6d 2d 3e 6e ], "%d", pMem->n
1c287 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73 71 6c 69 );. k += sqli
1c288 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 42 75 te3Strlen30(&zBu
1c289 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42 75 66 5b f[k]);. zBuf[
1c28a 6b 2b 2b 5d 20 3d 20 27 5b 27 3b 0a 20 20 20 20 k++] = '[';.
1c28b 66 6f 72 28 6a 3d 30 3b 20 6a 3c 31 35 20 26 26 for(j=0; j<15 &&
1c28c 20 6a 3c 70 4d 65 6d 2d 3e 6e 3b 20 6a 2b 2b 29 j<pMem->n; j++)
1c28d 7b 0a 20 20 20 20 20 20 75 38 20 63 20 3d 20 70 {. u8 c = p
1c28e 4d 65 6d 2d 3e 7a 5b 6a 5d 3b 0a 20 20 20 20 20 Mem->z[j];.
1c28f 20 69 66 28 20 63 3e 3d 30 78 32 30 20 26 26 20 if( c>=0x20 &&
1c290 63 3c 30 78 37 66 20 29 7b 0a 20 20 20 20 20 20 c<0x7f ){.
1c291 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 63 3b zBuf[k++] = c;
1c292 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1c293 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 zBuf[k++]
1c294 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 7d 0a 20 = '.';. }.
1c295 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b 6b 2b }. zBuf[k+
1c296 2b 5d 20 3d 20 27 5d 27 3b 0a 20 20 20 20 73 71 +] = ']';. sq
1c297 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 lite3_snprintf(1
1c298 30 30 2c 26 7a 42 75 66 5b 6b 5d 2c 20 65 6e 63 00,&zBuf[k], enc
1c299 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e 63 5d names[pMem->enc]
1c29a 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73 71 6c 69 );. k += sqli
1c29b 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 42 75 te3Strlen30(&zBu
1c29c 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42 75 66 5b f[k]);. zBuf[
1c29d 6b 2b 2b 5d 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a k++] = 0;. }.}.
1c29e 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
1c29f 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a QLITE_DEBUG./*.*
1c2a0 2a 20 50 72 69 6e 74 20 74 68 65 20 76 61 6c 75 * Print the valu
1c2a1 65 20 6f 66 20 61 20 72 65 67 69 73 74 65 72 20 e of a register
1c2a2 66 6f 72 20 74 72 61 63 69 6e 67 20 70 75 72 70 for tracing purp
1c2a3 6f 73 65 73 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 oses:.*/.static
1c2a4 76 6f 69 64 20 6d 65 6d 54 72 61 63 65 50 72 69 void memTracePri
1c2a5 6e 74 28 46 49 4c 45 20 2a 6f 75 74 2c 20 4d 65 nt(FILE *out, Me
1c2a6 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e m *p){. if( p->
1c2a7 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1c2a8 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 ){. fprintf(
1c2a9 6f 75 74 2c 20 22 20 4e 55 4c 4c 22 29 3b 0a 20 out, " NULL");.
1c2aa 20 7d 65 6c 73 65 20 69 66 28 20 28 70 2d 3e 66 }else if( (p->f
1c2ab 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c lags & (MEM_Int|
1c2ac 4d 45 4d 5f 53 74 72 29 29 3d 3d 28 4d 45 4d 5f MEM_Str))==(MEM_
1c2ad 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 20 29 7b 0a Int|MEM_Str) ){.
1c2ae 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c fprintf(out,
1c2af 20 22 20 73 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e " si:%lld", p->
1c2b0 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 u.i);. }else if
1c2b1 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d ( p->flags & MEM
1c2b2 5f 49 6e 74 20 29 7b 0a 20 20 20 20 66 70 72 69 _Int ){. fpri
1c2b3 6e 74 66 28 6f 75 74 2c 20 22 20 69 3a 25 6c 6c ntf(out, " i:%ll
1c2b4 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 23 69 66 d", p->u.i);.#if
1c2b5 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1c2b6 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a _FLOATING_POINT.
1c2b7 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 }else if( p->f
1c2b8 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 lags & MEM_Real
1c2b9 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f ){. fprintf(o
1c2ba 75 74 2c 20 22 20 72 3a 25 67 22 2c 20 70 2d 3e ut, " r:%g", p->
1c2bb 72 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c r);.#endif. }el
1c2bc 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 se if( p->flags
1c2bd 26 20 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a & MEM_RowSet ){.
1c2be 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c fprintf(out,
1c2bf 20 22 20 28 72 6f 77 73 65 74 29 22 29 3b 0a 20 " (rowset)");.
1c2c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 }else{. char
1c2c1 20 7a 42 75 66 5b 32 30 30 5d 3b 0a 20 20 20 20 zBuf[200];.
1c2c2 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 sqlite3VdbeMemPr
1c2c3 65 74 74 79 50 72 69 6e 74 28 70 2c 20 7a 42 75 ettyPrint(p, zBu
1c2c4 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 f);. fprintf(
1c2c5 6f 75 74 2c 20 22 20 22 29 3b 0a 20 20 20 20 66 out, " ");. f
1c2c6 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 73 22 printf(out, "%s"
1c2c7 2c 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 7d 0a 73 , zBuf);. }.}.s
1c2c8 74 61 74 69 63 20 76 6f 69 64 20 72 65 67 69 73 tatic void regis
1c2c9 74 65 72 54 72 61 63 65 28 46 49 4c 45 20 2a 6f terTrace(FILE *o
1c2ca 75 74 2c 20 69 6e 74 20 69 52 65 67 2c 20 4d 65 ut, int iReg, Me
1c2cb 6d 20 2a 70 29 7b 0a 20 20 66 70 72 69 6e 74 66 m *p){. fprintf
1c2cc 28 6f 75 74 2c 20 22 52 45 47 5b 25 64 5d 20 3d (out, "REG[%d] =
1c2cd 20 22 2c 20 69 52 65 67 29 3b 0a 20 20 6d 65 6d ", iReg);. mem
1c2ce 54 72 61 63 65 50 72 69 6e 74 28 6f 75 74 2c 20 TracePrint(out,
1c2cf 70 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 p);. fprintf(ou
1c2d0 74 2c 20 22 5c 6e 22 29 3b 0a 7d 0a 23 65 6e 64 t, "\n");.}.#end
1c2d1 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
1c2d2 45 5f 44 45 42 55 47 0a 23 20 20 64 65 66 69 6e E_DEBUG.# defin
1c2d3 65 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 e REGISTER_TRACE
1c2d4 28 52 2c 4d 29 20 69 66 28 70 2d 3e 74 72 61 63 (R,M) if(p->trac
1c2d5 65 29 72 65 67 69 73 74 65 72 54 72 61 63 65 28 e)registerTrace(
1c2d6 70 2d 3e 74 72 61 63 65 2c 52 2c 4d 29 0a 23 65 p->trace,R,M).#e
1c2d7 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 52 45 lse.# define RE
1c2d8 47 49 53 54 45 52 5f 54 52 41 43 45 28 52 2c 4d GISTER_TRACE(R,M
1c2d9 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 64 65 ).#endif...#ifde
1c2da 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 0a f VDBE_PROFILE..
1c2db 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20 /* .** hwtime.h
1c2dc 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 contains inline
1c2dd 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66 assembler code f
1c2de 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 or implementing
1c2df 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d .** high-perform
1c2e0 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74 ance timing rout
1c2e1 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a ines..*/./******
1c2e2 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 ******** Include
1c2e3 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65 hwtime.h in the
1c2e4 20 6d 69 64 64 6c 65 20 6f 66 20 76 64 62 65 2e middle of vdbe.
1c2e5 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1c2e6 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
1c2e7 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
1c2e8 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a ile hwtime.h ***
1c2e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c2ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c2eb 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
1c2ec 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 008 May 27.**.**
1c2ed 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1c2ee 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1c2ef 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1c2f0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1c2f1 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
1c2f2 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
1c2f3 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
1c2f4 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
1c2f5 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
1c2f6 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
1c2f7 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
1c2f8 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
1c2f9 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
1c2fa 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
1c2fb 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1c2fc 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
1c2fd 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
1c2fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c2ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c301 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c302 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
1c303 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
1c304 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 ontains inline a
1c305 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72 sm code for retr
1c306 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72 ieving "high-per
1c307 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 formance".** cou
1c308 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c nters for x86 cl
1c309 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 ass CPUs..**.**
1c30a 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20 $Id: hwtime.h,v
1c30b 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31 20 31 1.3 2008/08/01 1
1c30c 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20 45 78 4:33:15 shane Ex
1c30d 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f p $.*/.#ifndef _
1c30e 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e HWTIME_H_.#defin
1c30f 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a e _HWTIME_H_../*
1c310 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
1c311 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 g routine only w
1c312 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d orks on pentium-
1c313 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29 class (or newer)
1c314 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 processors..**
1c315 49 74 20 75 73 65 73 20 74 68 65 20 52 44 54 53 It uses the RDTS
1c316 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64 C opcode to read
1c317 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 the cycle count
1c318 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 value out of th
1c319 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 e.** processor a
1c31a 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 nd returns that
1c31b 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e value. This can
1c31c 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 69 67 be used for hig
1c31d 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 h-res.** profili
1c31e 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 ng..*/.#if (defi
1c31f 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c ned(__GNUC__) ||
1c320 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 defined(_MSC_VE
1c321 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28 R)) && \. (
1c322 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c defined(i386) ||
1c323 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f defined(__i386_
1c324 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d _) || defined(_M
1c325 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64 _IX86)).. #if d
1c326 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 efined(__GNUC__)
1c327 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 .. __inline__ s
1c328 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
1c329 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
1c32a 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 {. unsigned
1c32b 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 int lo, hi;.
1c32c 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 __asm__ __volat
1c32d 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a ile__ ("rdtsc" :
1c32e 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 "=a" (lo), "=d"
1c32f 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74 (hi));. ret
1c330 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 urn (sqlite_uint
1c331 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 64)hi << 32 | lo
1c332 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 ;. }.. #elif d
1c333 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 efined(_MSC_VER)
1c334 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e .. __declspec(n
1c335 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 aked) __inline s
1c336 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 qlite_uint64 __c
1c337 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69 decl sqlite3Hwti
1c338 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f me(void){. _
1c339 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 _asm {. r
1c33a 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 65 74 dtsc. ret
1c33b 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20 ; return
1c33c 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58 value at EDX:EAX
1c33d 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 . }. }.. #
1c33e 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 endif..#elif (de
1c33f 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 fined(__GNUC__)
1c340 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 && defined(__x86
1c341 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c _64__)).. __inl
1c342 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e ine__ sqlite_uin
1c343 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d t64 sqlite3Hwtim
1c344 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 e(void){. u
1c345 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c nsigned long val
1c346 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 ;. __asm__
1c347 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 __volatile__ ("r
1c348 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61 dtsc" : "=A" (va
1c349 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 l));. retur
1c34a 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c n val;. }. .#el
1c34b 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e if (defined(__GN
1c34c 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 UC__) && defined
1c34d 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f (__ppc__)).. __
1c34e 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f inline__ sqlite_
1c34f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 uint64 sqlite3Hw
1c350 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 time(void){.
1c351 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 unsigned long
1c352 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 long retval;.
1c353 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 unsigned long
1c354 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 junk;. __a
1c355 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f sm__ __volatile_
1c356 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 _ ("\n\.
1c357 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20 1: mftbu
1c358 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 %1\n\.
1c359 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 20 20 mftb
1c35a 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 %L0\n\.
1c35b 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 75 mftbu
1c35c 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 %0\n\.
1c35d 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 77 20 cmpw
1c35e 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 %0,%1\n\.
1c35f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6e bn
1c360 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 20 20 e 1b".
1c361 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d : "=
1c362 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 r" (retval), "=r
1c363 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 " (junk));.
1c364 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a return retval;.
1c365 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 }..#else.. #e
1c366 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d rror Need implem
1c367 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 entation of sqli
1c368 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 te3Hwtime() for
1c369 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a your platform...
1c36a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d /*. ** To com
1c36b 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70 pile without imp
1c36c 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 lementing sqlite
1c36d 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 3Hwtime() for yo
1c36e 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a ur platform,. *
1c36f 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 * you can remove
1c370 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f the above #erro
1c371 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f r and use the fo
1c372 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 llowing. ** stu
1c373 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 b function. You
1c374 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e will lose timin
1c375 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 g support for ma
1c376 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 ny. ** of the d
1c377 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73 ebugging and tes
1c378 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20 ting utilities,
1c379 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74 but it should at
1c37a 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 . ** least comp
1c37b 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a ile and run.. *
1c37c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1c37d 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 sqlite_uint64
1c37e 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 sqlite3Hwtime(v
1c37f 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73 oid){ return ((s
1c380 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b qlite_uint64)0);
1c381 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 }..#endif..#end
1c382 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f if /* !defined(_
1c383 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f HWTIME_H_) */../
1c384 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
1c385 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a nd of hwtime.h *
1c386 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c387 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c388 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1c389 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 ************** C
1c38a 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 ontinuing where
1c38b 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 76 we left off in v
1c38c 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dbe.c **********
1c38d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
1c38e 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
1c38f 65 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 e CHECK_FOR_INTE
1c390 52 52 55 50 54 20 6d 61 63 72 6f 20 64 65 66 69 RRUPT macro defi
1c391 6e 65 64 20 68 65 72 65 20 6c 6f 6f 6b 73 20 74 ned here looks t
1c392 6f 20 73 65 65 20 69 66 20 74 68 65 0a 2a 2a 20 o see if the.**
1c393 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 sqlite3_interrup
1c394 74 28 29 20 72 6f 75 74 69 6e 65 20 68 61 73 20 t() routine has
1c395 62 65 65 6e 20 63 61 6c 6c 65 64 2e 20 20 49 66 been called. If
1c396 20 69 74 20 68 61 73 20 62 65 65 6e 2c 20 74 68 it has been, th
1c397 65 6e 0a 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 en.** processing
1c398 20 6f 66 20 74 68 65 20 56 44 42 45 20 70 72 6f of the VDBE pro
1c399 67 72 61 6d 20 69 73 20 69 6e 74 65 72 72 75 70 gram is interrup
1c39a 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ted..**.** This
1c39b 6d 61 63 72 6f 20 61 64 64 65 64 20 74 6f 20 65 macro added to e
1c39c 76 65 72 79 20 69 6e 73 74 72 75 63 74 69 6f 6e very instruction
1c39d 20 74 68 61 74 20 64 6f 65 73 20 61 20 6a 75 6d that does a jum
1c39e 70 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a p in order to.**
1c39f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 6c 6f 6f implement a loo
1c3a0 70 2e 20 20 54 68 69 73 20 74 65 73 74 20 75 73 p. This test us
1c3a1 65 64 20 74 6f 20 62 65 20 6f 6e 20 65 76 65 72 ed to be on ever
1c3a2 79 20 73 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 y single instruc
1c3a3 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74 20 74 68 61 tion,.** but tha
1c3a4 74 20 6d 65 61 6e 74 20 77 65 20 6d 6f 72 65 20 t meant we more
1c3a5 74 65 73 74 69 6e 67 20 74 68 61 74 20 77 65 20 testing that we
1c3a6 6e 65 65 64 65 64 2e 20 20 42 79 20 6f 6e 6c 79 needed. By only
1c3a7 20 74 65 73 74 69 6e 67 20 74 68 65 0a 2a 2a 20 testing the.**
1c3a8 66 6c 61 67 20 6f 6e 20 6a 75 6d 70 20 69 6e 73 flag on jump ins
1c3a9 74 72 75 63 74 69 6f 6e 73 2c 20 77 65 20 67 65 tructions, we ge
1c3aa 74 20 61 20 28 73 6d 61 6c 6c 29 20 73 70 65 65 t a (small) spee
1c3ab 64 20 69 6d 70 72 6f 76 65 6d 65 6e 74 2e 0a 2a d improvement..*
1c3ac 2f 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f /.#define CHECK_
1c3ad 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 20 5c 0a FOR_INTERRUPT \.
1c3ae 20 20 20 69 66 28 20 64 62 2d 3e 75 31 2e 69 73 if( db->u1.is
1c3af 49 6e 74 65 72 72 75 70 74 65 64 20 29 20 67 6f Interrupted ) go
1c3b0 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1c3b1 69 6e 74 65 72 72 75 70 74 3b 0a 0a 23 69 66 64 interrupt;..#ifd
1c3b2 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
1c3b3 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65 45 static int fileE
1c3b4 78 69 73 74 73 28 73 71 6c 69 74 65 33 20 2a 64 xists(sqlite3 *d
1c3b5 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a b, const char *z
1c3b6 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65 73 File){. int res
1c3b7 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d = 0;. int rc =
1c3b8 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 69 66 64 SQLITE_OK;.#ifd
1c3b9 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 ef SQLITE_TEST.
1c3ba 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 63 75 /* If we are cu
1c3bb 72 72 65 6e 74 6c 79 20 74 65 73 74 69 6e 67 20 rrently testing
1c3bc 49 4f 20 65 72 72 6f 72 73 2c 20 74 68 65 6e 20 IO errors, then
1c3bd 64 6f 20 6e 6f 74 20 63 61 6c 6c 20 4f 73 41 63 do not call OsAc
1c3be 63 65 73 73 28 29 20 74 6f 0a 20 20 2a 2a 20 74 cess() to. ** t
1c3bf 65 73 74 20 66 6f 72 20 74 68 65 20 70 72 65 73 est for the pres
1c3c0 65 6e 63 65 20 6f 66 20 7a 46 69 6c 65 2e 20 54 ence of zFile. T
1c3c1 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 61 his is because a
1c3c2 6e 79 20 49 4f 20 65 72 72 6f 72 20 74 68 61 74 ny IO error that
1c3c3 0a 20 20 2a 2a 20 6f 63 63 75 72 73 20 68 65 72 . ** occurs her
1c3c4 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65 e will not be re
1c3c5 70 6f 72 74 65 64 2c 20 63 61 75 73 69 6e 67 20 ported, causing
1c3c6 74 68 65 20 74 65 73 74 20 74 6f 20 66 61 69 6c the test to fail
1c3c7 2e 0a 20 20 2a 2f 0a 20 20 65 78 74 65 72 6e 20 .. */. extern
1c3c8 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 int sqlite3_io_e
1c3c9 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 rror_pending;.
1c3ca 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 if( sqlite3_io_e
1c3cb 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3c 3d 30 20 rror_pending<=0
1c3cc 29 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 63 20 ).#endif. rc
1c3cd 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 = sqlite3OsAcces
1c3ce 73 28 64 62 2d 3e 70 56 66 73 2c 20 7a 46 69 6c s(db->pVfs, zFil
1c3cf 65 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 e, SQLITE_ACCESS
1c3d0 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29 3b 0a _EXISTS, &res);.
1c3d1 20 20 72 65 74 75 72 6e 20 28 72 65 73 20 26 26 return (res &&
1c3d2 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 3b rc==SQLITE_OK);
1c3d3 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 .}.#endif..#ifnd
1c3d4 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 ef NDEBUG./*.**
1c3d5 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
1c3d6 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f only called fro
1c3d7 6d 20 77 69 74 68 69 6e 20 61 6e 20 61 73 73 65 m within an asse
1c3d8 72 74 28 29 20 65 78 70 72 65 73 73 69 6f 6e 2e rt() expression.
1c3d9 20 49 74 0a 2a 2a 20 63 68 65 63 6b 73 20 74 68 It.** checks th
1c3da 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 2e 6e at the sqlite3.n
1c3db 54 72 61 6e 73 61 63 74 69 6f 6e 20 76 61 72 69 Transaction vari
1c3dc 61 62 6c 65 20 69 73 20 63 6f 72 72 65 63 74 6c able is correctl
1c3dd 79 20 73 65 74 20 74 6f 0a 2a 2a 20 74 68 65 20 y set to.** the
1c3de 6e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 74 72 number of non-tr
1c3df 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f ansaction savepo
1c3e0 69 6e 74 73 20 63 75 72 72 65 6e 74 6c 79 20 69 ints currently i
1c3e1 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65 64 n the .** linked
1c3e2 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20 61 list starting a
1c3e3 74 20 73 71 6c 69 74 65 33 2e 70 53 61 76 65 70 t sqlite3.pSavep
1c3e4 6f 69 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 55 73 61 oint..** .** Usa
1c3e5 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 73 ge:.**.** as
1c3e6 73 65 72 74 28 20 63 68 65 63 6b 53 61 76 65 70 sert( checkSavep
1c3e7 6f 69 6e 74 43 6f 75 6e 74 28 64 62 29 20 29 3b ointCount(db) );
1c3e8 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 .*/.static int c
1c3e9 68 65 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 heckSavepointCou
1c3ea 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b nt(sqlite3 *db){
1c3eb 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 . int n = 0;.
1c3ec 53 61 76 65 70 6f 69 6e 74 20 2a 70 3b 0a 20 20 Savepoint *p;.
1c3ed 66 6f 72 28 70 3d 64 62 2d 3e 70 53 61 76 65 70 for(p=db->pSavep
1c3ee 6f 69 6e 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e oint; p; p=p->pN
1c3ef 65 78 74 29 20 6e 2b 2b 3b 0a 20 20 61 73 73 65 ext) n++;. asse
1c3f0 72 74 28 20 6e 3d 3d 28 64 62 2d 3e 6e 53 61 76 rt( n==(db->nSav
1c3f1 65 70 6f 69 6e 74 20 2b 20 64 62 2d 3e 69 73 54 epoint + db->isT
1c3f2 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f ransactionSavepo
1c3f3 69 6e 74 29 20 29 3b 0a 20 20 72 65 74 75 72 6e int) );. return
1c3f4 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 1;.}.#endif../*
1c3f5 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 73 20 6d .** Execute as m
1c3f6 75 63 68 20 6f 66 20 61 20 56 44 42 45 20 70 72 uch of a VDBE pr
1c3f7 6f 67 72 61 6d 20 61 73 20 77 65 20 63 61 6e 20 ogram as we can
1c3f8 74 68 65 6e 20 72 65 74 75 72 6e 2e 0a 2a 2a 0a then return..**.
1c3f9 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 ** sqlite3VdbeMa
1c3fa 6b 65 52 65 61 64 79 28 29 20 6d 75 73 74 20 62 keReady() must b
1c3fb 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 e called before
1c3fc 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6e 20 this routine in
1c3fd 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 6c 6f 73 order to.** clos
1c3fe 65 20 74 68 65 20 70 72 6f 67 72 61 6d 20 77 69 e the program wi
1c3ff 74 68 20 61 20 66 69 6e 61 6c 20 4f 50 5f 48 61 th a final OP_Ha
1c400 6c 74 20 61 6e 64 20 74 6f 20 73 65 74 20 75 70 lt and to set up
1c401 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 73 0a 2a the callbacks.*
1c402 2a 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 * and the error
1c403 6d 65 73 73 61 67 65 20 70 6f 69 6e 74 65 72 2e message pointer.
1c404 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 65 76 65 72 20 .**.** Whenever
1c405 61 20 72 6f 77 20 6f 72 20 72 65 73 75 6c 74 20 a row or result
1c406 64 61 74 61 20 69 73 20 61 76 61 69 6c 61 62 6c data is availabl
1c407 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 e, this routine
1c408 77 69 6c 6c 20 65 69 74 68 65 72 0a 2a 2a 20 69 will either.** i
1c409 6e 76 6f 6b 65 20 74 68 65 20 72 65 73 75 6c 74 nvoke the result
1c40a 20 63 61 6c 6c 62 61 63 6b 20 28 69 66 20 74 68 callback (if th
1c40b 65 72 65 20 69 73 20 6f 6e 65 29 20 6f 72 20 72 ere is one) or r
1c40c 65 74 75 72 6e 20 77 69 74 68 0a 2a 2a 20 53 51 eturn with.** SQ
1c40d 4c 49 54 45 5f 52 4f 57 2e 0a 2a 2a 0a 2a 2a 20 LITE_ROW..**.**
1c40e 49 66 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 If an attempt is
1c40f 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 61 20 made to open a
1c410 6c 6f 63 6b 65 64 20 64 61 74 61 62 61 73 65 2c locked database,
1c411 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 then this routi
1c412 6e 65 0a 2a 2a 20 77 69 6c 6c 20 65 69 74 68 65 ne.** will eithe
1c413 72 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 r invoke the bus
1c414 79 20 63 61 6c 6c 62 61 63 6b 20 28 69 66 20 74 y callback (if t
1c415 68 65 72 65 20 69 73 20 6f 6e 65 29 20 6f 72 20 here is one) or
1c416 69 74 20 77 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 it will.** retur
1c417 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a 2a n SQLITE_BUSY..*
1c418 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 *.** If an error
1c419 20 6f 63 63 75 72 73 2c 20 61 6e 20 65 72 72 6f occurs, an erro
1c41a 72 20 6d 65 73 73 61 67 65 20 69 73 20 77 72 69 r message is wri
1c41b 74 74 65 6e 20 74 6f 20 6d 65 6d 6f 72 79 20 6f tten to memory o
1c41c 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 btained.** from
1c41d 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
1c41e 20 61 6e 64 20 70 2d 3e 7a 45 72 72 4d 73 67 20 and p->zErrMsg
1c41f 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 is made to point
1c420 20 74 6f 20 74 68 61 74 20 6d 65 6d 6f 72 79 2e to that memory.
1c421 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 63 6f .** The error co
1c422 64 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 de is stored in
1c423 70 2d 3e 72 63 20 61 6e 64 20 74 68 69 73 20 72 p->rc and this r
1c424 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 outine returns S
1c425 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a QLITE_ERROR..**.
1c426 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 ** If the callba
1c427 63 6b 20 65 76 65 72 20 72 65 74 75 72 6e 73 20 ck ever returns
1c428 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 non-zero, then t
1c429 68 65 20 70 72 6f 67 72 61 6d 20 65 78 69 74 73 he program exits
1c42a 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e .** immediately.
1c42b 20 20 54 68 65 72 65 20 77 69 6c 6c 20 62 65 20 There will be
1c42c 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 no error message
1c42d 20 62 75 74 20 74 68 65 20 70 2d 3e 72 63 20 66 but the p->rc f
1c42e 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 74 20 74 ield is.** set t
1c42f 6f 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 61 o SQLITE_ABORT a
1c430 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 nd this routine
1c431 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 will return SQLI
1c432 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 TE_ERROR..**.**
1c433 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 A memory allocat
1c434 69 6f 6e 20 65 72 72 6f 72 20 63 61 75 73 65 73 ion error causes
1c435 20 70 2d 3e 72 63 20 74 6f 20 62 65 20 73 65 74 p->rc to be set
1c436 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d to SQLITE_NOMEM
1c437 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72 6f 75 and this.** rou
1c438 74 69 6e 65 20 74 6f 20 72 65 74 75 72 6e 20 53 tine to return S
1c439 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a QLITE_ERROR..**.
1c43a 2a 2a 20 4f 74 68 65 72 20 66 61 74 61 6c 20 65 ** Other fatal e
1c43b 72 72 6f 72 73 20 72 65 74 75 72 6e 20 53 51 4c rrors return SQL
1c43c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a ITE_ERROR..**.**
1c43d 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 After this rout
1c43e 69 6e 65 20 68 61 73 20 66 69 6e 69 73 68 65 64 ine has finished
1c43f 2c 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e , sqlite3VdbeFin
1c440 61 6c 69 7a 65 28 29 20 73 68 6f 75 6c 64 20 62 alize() should b
1c441 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 6c 65 e.** used to cle
1c442 61 6e 20 75 70 20 74 68 65 20 6d 65 73 73 20 74 an up the mess t
1c443 68 61 74 20 77 61 73 20 6c 65 66 74 20 62 65 68 hat was left beh
1c444 69 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ind..*/.SQLITE_P
1c445 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1c446 65 33 56 64 62 65 45 78 65 63 28 0a 20 20 56 64 e3VdbeExec(. Vd
1c447 62 65 20 2a 70 20 20 20 20 20 20 20 20 20 20 20 be *p
1c448 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1c449 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 VDBE */.){. int
1c44a 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 pc;
1c44b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 /* The p
1c44c 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a rogram counter *
1c44d 2f 0a 20 20 4f 70 20 2a 70 4f 70 3b 20 20 20 20 /. Op *pOp;
1c44e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1c44f 2a 20 43 75 72 72 65 6e 74 20 6f 70 65 72 61 74 * Current operat
1c450 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 ion */. int rc
1c451 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
1c452 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 /* Value to
1c453 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 return */. sqli
1c454 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b te3 *db = p->db;
1c455 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 /* The da
1c456 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 65 tabase */. u8 e
1c457 6e 63 6f 64 69 6e 67 20 3d 20 45 4e 43 28 64 62 ncoding = ENC(db
1c458 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 ); /* The da
1c459 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 tabase encoding
1c45a 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 31 20 3d */. Mem *pIn1 =
1c45b 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1c45c 2f 2a 20 31 73 74 20 69 6e 70 75 74 20 6f 70 65 /* 1st input ope
1c45d 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 rand */. Mem *p
1c45e 49 6e 32 20 3d 20 30 3b 20 20 20 20 20 20 20 20 In2 = 0;
1c45f 20 20 20 20 20 2f 2a 20 32 6e 64 20 69 6e 70 75 /* 2nd inpu
1c460 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d t operand */. M
1c461 65 6d 20 2a 70 49 6e 33 20 3d 20 30 3b 20 20 20 em *pIn3 = 0;
1c462 20 20 20 20 20 20 20 20 20 20 2f 2a 20 33 72 64 /* 3rd
1c463 20 69 6e 70 75 74 20 6f 70 65 72 61 6e 64 20 2a input operand *
1c464 2f 0a 20 20 4d 65 6d 20 2a 70 4f 75 74 20 3d 20 /. Mem *pOut =
1c465 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0; /
1c466 2a 20 4f 75 74 70 75 74 20 6f 70 65 72 61 6e 64 * Output operand
1c467 20 2a 2f 0a 20 20 75 38 20 6f 70 50 72 6f 70 65 */. u8 opPrope
1c468 72 74 79 3b 0a 20 20 69 6e 74 20 69 43 6f 6d 70 rty;. int iComp
1c469 61 72 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 are = 0;
1c46a 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 6c /* Result of l
1c46b 61 73 74 20 4f 50 5f 43 6f 6d 70 61 72 65 20 6f ast OP_Compare o
1c46c 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e peration */. in
1c46d 74 20 2a 61 50 65 72 6d 75 74 65 20 3d 20 30 3b t *aPermute = 0;
1c46e 20 20 20 20 20 20 20 20 20 2f 2a 20 50 65 72 6d /* Perm
1c46f 75 74 61 74 69 6f 6e 20 6f 66 20 63 6f 6c 75 6d utation of colum
1c470 6e 73 20 66 6f 72 20 4f 50 5f 43 6f 6d 70 61 72 ns for OP_Compar
1c471 65 20 2a 2f 0a 23 69 66 64 65 66 20 56 44 42 45 e */.#ifdef VDBE
1c472 5f 50 52 4f 46 49 4c 45 0a 20 20 75 36 34 20 73 _PROFILE. u64 s
1c473 74 61 72 74 3b 20 20 20 20 20 20 20 20 20 20 20 tart;
1c474 20 20 20 20 20 20 2f 2a 20 43 50 55 20 63 6c 6f /* CPU clo
1c475 63 6b 20 63 6f 75 6e 74 20 61 74 20 73 74 61 72 ck count at star
1c476 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 t of opcode */.
1c477 20 69 6e 74 20 6f 72 69 67 50 63 3b 20 20 20 20 int origPc;
1c478 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
1c479 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 61 rogram counter a
1c47a 74 20 73 74 61 72 74 20 6f 66 20 6f 70 63 6f 64 t start of opcod
1c47b 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e e */.#endif.#ifn
1c47c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1c47d 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 PROGRESS_CALLBAC
1c47e 4b 0a 20 20 69 6e 74 20 6e 50 72 6f 67 72 65 73 K. int nProgres
1c47f 73 4f 70 73 20 3d 20 30 3b 20 20 20 20 20 20 2f sOps = 0; /
1c480 2a 20 4f 70 63 6f 64 65 73 20 65 78 65 63 75 74 * Opcodes execut
1c481 65 64 20 73 69 6e 63 65 20 70 72 6f 67 72 65 73 ed since progres
1c482 73 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23 s callback. */.#
1c483 65 6e 64 69 66 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a endif. /*******
1c484 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c485 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c486 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c487 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 *************.
1c488 2a 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ** Automatically
1c489 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 0a generated code.
1c48a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 6f **. ** The fo
1c48b 6c 6c 6f 77 69 6e 67 20 75 6e 69 6f 6e 20 69 73 llowing union is
1c48c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 automatically g
1c48d 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 0a enerated by the.
1c48e 20 20 2a 2a 20 76 64 62 65 2d 63 6f 6d 70 72 65 ** vdbe-compre
1c48f 73 73 2e 74 63 6c 20 73 63 72 69 70 74 2e 20 20 ss.tcl script.
1c490 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 The purpose of t
1c491 68 69 73 20 75 6e 69 6f 6e 20 69 73 20 74 6f 0a his union is to.
1c492 20 20 2a 2a 20 72 65 64 75 63 65 20 74 68 65 20 ** reduce the
1c493 61 6d 6f 75 6e 74 20 6f 66 20 73 74 61 63 6b 20 amount of stack
1c494 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20 62 space required b
1c495 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e y this function.
1c496 0a 20 20 2a 2a 20 53 65 65 20 63 6f 6d 6d 65 6e . ** See commen
1c497 74 73 20 69 6e 20 74 68 65 20 76 64 62 65 2d 63 ts in the vdbe-c
1c498 6f 6d 70 72 65 73 73 2e 74 63 6c 20 73 63 72 69 ompress.tcl scri
1c499 70 74 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a pt for details..
1c49a 20 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 76 64 62 */. union vdb
1c49b 65 45 78 65 63 55 6e 69 6f 6e 20 7b 0a 20 20 20 eExecUnion {.
1c49c 20 73 74 72 75 63 74 20 4f 50 5f 59 69 65 6c 64 struct OP_Yield
1c49d 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c49e 20 20 20 20 69 6e 74 20 70 63 44 65 73 74 3b 0a int pcDest;.
1c49f 20 20 20 20 7d 20 61 61 3b 0a 20 20 20 20 73 74 } aa;. st
1c4a0 72 75 63 74 20 4f 50 5f 56 61 72 69 61 62 6c 65 ruct OP_Variable
1c4a1 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c4a2 20 20 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 int p1;
1c4a3 20 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 /* Variable
1c4a4 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a 2f to copy from */
1c4a5 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b 20 20 . int p2;
1c4a6 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 /* Regis
1c4a7 74 65 72 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a ter to copy to *
1c4a8 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 20 20 /. int n;
1c4a9 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1c4aa 65 72 20 6f 66 20 76 61 6c 75 65 73 20 6c 65 66 er of values lef
1c4ab 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 20 t to copy */.
1c4ac 20 20 20 4d 65 6d 20 2a 70 56 61 72 3b 20 20 20 Mem *pVar;
1c4ad 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 62 65 69 /* Value bei
1c4ae 6e 67 20 74 72 61 6e 73 66 65 72 72 65 64 20 2a ng transferred *
1c4af 2f 0a 20 20 20 20 7d 20 61 62 3b 0a 20 20 20 20 /. } ab;.
1c4b0 73 74 72 75 63 74 20 4f 50 5f 4d 6f 76 65 5f 73 struct OP_Move_s
1c4b1 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c4b2 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b char *zMalloc;
1c4b3 20 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 76 61 /* Holding va
1c4b4 72 69 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 riable for alloc
1c4b5 61 74 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 ated memory */.
1c4b6 20 20 20 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 int n;
1c4b7 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1c4b8 6f 66 20 72 65 67 69 73 74 65 72 73 20 6c 65 66 of registers lef
1c4b9 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 20 t to copy */.
1c4ba 20 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 int p1;
1c4bb 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 /* Register
1c4bc 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a to copy from */.
1c4bd 20 20 20 20 20 20 69 6e 74 20 70 32 3b 20 20 20 int p2;
1c4be 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
1c4bf 65 72 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f er to copy to */
1c4c0 0a 20 20 20 20 7d 20 61 63 3b 0a 20 20 20 20 73 . } ac;. s
1c4c1 74 72 75 63 74 20 4f 50 5f 52 65 73 75 6c 74 52 truct OP_ResultR
1c4c2 6f 77 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ow_stack_vars {.
1c4c3 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b Mem *pMem;
1c4c4 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 . int i;.
1c4c5 20 20 7d 20 61 64 3b 0a 20 20 20 20 73 74 72 75 } ad;. stru
1c4c6 63 74 20 4f 50 5f 43 6f 6e 63 61 74 5f 73 74 61 ct OP_Concat_sta
1c4c7 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c4c8 69 36 34 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d i64 nByte;. }
1c4c9 20 61 65 3b 0a 20 20 20 20 73 74 72 75 63 74 20 ae;. struct
1c4ca 4f 50 5f 52 65 6d 61 69 6e 64 65 72 5f 73 74 61 OP_Remainder_sta
1c4cb 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c4cc 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 int flags;
1c4cd 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 4d 45 4d 5f /* Combined MEM_
1c4ce 2a 20 66 6c 61 67 73 20 66 72 6f 6d 20 62 6f 74 * flags from bot
1c4cf 68 20 69 6e 70 75 74 73 20 2a 2f 0a 20 20 20 20 h inputs */.
1c4d0 20 20 69 36 34 20 69 41 3b 20 20 20 20 20 20 20 i64 iA;
1c4d1 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c /* Integer val
1c4d2 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 ue of left opera
1c4d3 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 nd */. i64
1c4d4 69 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 iB; /* I
1c4d5 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f 66 20 nteger value of
1c4d6 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f right operand */
1c4d7 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 41 . double rA
1c4d8 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 ; /* Real v
1c4d9 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 alue of left ope
1c4da 72 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 64 6f rand */. do
1c4db 75 62 6c 65 20 72 42 3b 20 20 20 20 20 20 2f 2a uble rB; /*
1c4dc 20 52 65 61 6c 20 76 61 6c 75 65 20 6f 66 20 72 Real value of r
1c4dd 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a ight operand */.
1c4de 20 20 20 20 7d 20 61 66 3b 0a 20 20 20 20 73 74 } af;. st
1c4df 72 75 63 74 20 4f 50 5f 46 75 6e 63 74 69 6f 6e ruct OP_Function
1c4e0 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c4e1 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 int i;.
1c4e2 20 4d 65 6d 20 2a 70 41 72 67 3b 0a 20 20 20 20 Mem *pArg;.
1c4e3 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
1c4e4 74 20 63 74 78 3b 0a 20 20 20 20 20 20 73 71 6c t ctx;. sql
1c4e5 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 ite3_value **apV
1c4e6 61 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b al;. int n;
1c4e7 0a 20 20 20 20 7d 20 61 67 3b 0a 20 20 20 20 73 . } ag;. s
1c4e8 74 72 75 63 74 20 4f 50 5f 53 68 69 66 74 52 69 truct OP_ShiftRi
1c4e9 67 68 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b ght_stack_vars {
1c4ea 0a 20 20 20 20 20 20 69 36 34 20 61 3b 0a 20 20 . i64 a;.
1c4eb 20 20 20 20 69 36 34 20 62 3b 0a 20 20 20 20 7d i64 b;. }
1c4ec 20 61 68 3b 0a 20 20 20 20 73 74 72 75 63 74 20 ah;. struct
1c4ed 4f 50 5f 47 65 5f 73 74 61 63 6b 5f 76 61 72 73 OP_Ge_stack_vars
1c4ee 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 {. int res
1c4ef 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
1c4f0 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20 63 6f Result of the co
1c4f1 6d 70 61 72 69 73 6f 6e 20 6f 66 20 70 49 6e 31 mparison of pIn1
1c4f2 20 61 67 61 69 6e 73 74 20 70 49 6e 33 20 2a 2f against pIn3 */
1c4f3 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 66 69 . char affi
1c4f4 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 66 nity; /* Af
1c4f5 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20 66 6f finity to use fo
1c4f6 72 20 63 6f 6d 70 61 72 69 73 6f 6e 20 2a 2f 0a r comparison */.
1c4f7 20 20 20 20 7d 20 61 69 3b 0a 20 20 20 20 73 74 } ai;. st
1c4f8 72 75 63 74 20 4f 50 5f 43 6f 6d 70 61 72 65 5f ruct OP_Compare_
1c4f9 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c4fa 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20 int n;.
1c4fb 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 6e 74 int i;. int
1c4fc 20 70 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 70 p1;. int p
1c4fd 32 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 4b 2;. const K
1c4fe 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f eyInfo *pKeyInfo
1c4ff 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 64 78 3b ;. int idx;
1c500 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a . CollSeq *
1c501 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43 6f 6c pColl; /* Col
1c502 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
1c503 74 6f 20 75 73 65 20 6f 6e 20 74 68 69 73 20 74 to use on this t
1c504 65 72 6d 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 erm */. int
1c505 20 62 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 bRev;
1c506 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 /* True for DESC
1c507 45 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 65 ENDING sort orde
1c508 72 20 2a 2f 0a 20 20 20 20 7d 20 61 6a 3b 0a 20 r */. } aj;.
1c509 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 72 5f struct OP_Or_
1c50a 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c50b 20 20 20 69 6e 74 20 76 31 3b 20 20 20 20 2f 2a int v1; /*
1c50c 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 3a 20 20 Left operand:
1c50d 30 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 52 55 0==FALSE, 1==TRU
1c50e 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 E, 2==UNKNOWN or
1c50f 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 69 NULL */. i
1c510 6e 74 20 76 32 3b 20 20 20 20 2f 2a 20 52 69 67 nt v2; /* Rig
1c511 68 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d 3d 46 ht operand: 0==F
1c512 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 ALSE, 1==TRUE, 2
1c513 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c ==UNKNOWN or NUL
1c514 4c 20 2a 2f 0a 20 20 20 20 7d 20 61 6b 3b 0a 20 L */. } ak;.
1c515 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 66 4e struct OP_IfN
1c516 6f 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ot_stack_vars {.
1c517 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 int c;.
1c518 20 7d 20 61 6c 3b 0a 20 20 20 20 73 74 72 75 63 } al;. struc
1c519 74 20 4f 50 5f 43 6f 6c 75 6d 6e 5f 73 74 61 63 t OP_Column_stac
1c51a 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 75 k_vars {. u
1c51b 33 32 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 32 payloadSize;
1c51c 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
1c51d 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f ytes in the reco
1c51e 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 rd */. i64
1c51f 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 20 2f payloadSize64; /
1c520 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
1c521 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 s in the record
1c522 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 31 3b */. int p1;
1c523 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
1c524 31 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 1 value of the o
1c525 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 69 pcode */. i
1c526 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 nt p2;
1c527 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 /* column numb
1c528 65 72 20 74 6f 20 72 65 74 72 69 65 76 65 20 2a er to retrieve *
1c529 2f 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 /. VdbeCurs
1c52a 6f 72 20 2a 70 43 3b 20 20 20 20 2f 2a 20 54 68 or *pC; /* Th
1c52b 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 2a 2f e VDBE cursor */
1c52c 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 52 65 . char *zRe
1c52d 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 c; /* Poi
1c52e 6e 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 nter to complete
1c52f 20 72 65 63 6f 72 64 2d 64 61 74 61 20 2a 2f 0a record-data */.
1c530 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a BtCursor *
1c531 70 43 72 73 72 3b 20 20 20 2f 2a 20 54 68 65 20 pCrsr; /* The
1c532 42 54 72 65 65 20 63 75 72 73 6f 72 20 2a 2f 0a BTree cursor */.
1c533 20 20 20 20 20 20 75 33 32 20 2a 61 54 79 70 65 u32 *aType
1c534 3b 20 20 20 20 20 20 20 20 2f 2a 20 61 54 79 70 ; /* aTyp
1c535 65 5b 69 5d 20 68 6f 6c 64 73 20 74 68 65 20 6e e[i] holds the n
1c536 75 6d 65 72 69 63 20 74 79 70 65 20 6f 66 20 74 umeric type of t
1c537 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a he i-th column *
1c538 2f 0a 20 20 20 20 20 20 75 33 32 20 2a 61 4f 66 /. u32 *aOf
1c539 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 61 4f fset; /* aO
1c53a 66 66 73 65 74 5b 69 5d 20 69 73 20 6f 66 66 73 ffset[i] is offs
1c53b 65 74 20 74 6f 20 73 74 61 72 74 20 6f 66 20 64 et to start of d
1c53c 61 74 61 20 66 6f 72 20 69 2d 74 68 20 63 6f 6c ata for i-th col
1c53d 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 umn */. int
1c53e 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 nField;
1c53f 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 /* number of fie
1c540 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 lds in the recor
1c541 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6c d */. int l
1c542 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a en; /*
1c543 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 The length of t
1c544 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 he serialized da
1c545 74 61 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d ta for the colum
1c546 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 n */. int i
1c547 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1c548 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
1c549 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 44 61 . char *zDa
1c54a 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 72 ta; /* Par
1c54b 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 t of the record
1c54c 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f being decoded */
1c54d 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 44 65 73 . Mem *pDes
1c54e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 t; /* Whe
1c54f 72 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 re to write the
1c550 65 78 74 72 61 63 74 65 64 20 76 61 6c 75 65 20 extracted value
1c551 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 73 4d 65 */. Mem sMe
1c552 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 m; /* F
1c553 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 20 72 or storing the r
1c554 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f ecord being deco
1c555 64 65 64 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 ded */. u8
1c556 2a 7a 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 *zIdx;
1c557 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 68 65 /* Index into he
1c558 61 64 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 38 ader */. u8
1c559 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20 20 20 20 *zEndHdr;
1c55a 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 /* Pointer to f
1c55b 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72 20 irst byte after
1c55c 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 the header */.
1c55d 20 20 20 20 75 33 32 20 6f 66 66 73 65 74 3b 20 u32 offset;
1c55e 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
1c55f 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 2a into the data *
1c560 2f 0a 20 20 20 20 20 20 75 36 34 20 6f 66 66 73 /. u64 offs
1c561 65 74 36 34 3b 20 20 20 20 20 20 2f 2a 20 36 34 et64; /* 64
1c562 2d 62 69 74 20 6f 66 66 73 65 74 2e 20 20 36 34 -bit offset. 64
1c563 20 62 69 74 73 20 6e 65 65 64 65 64 20 74 6f 20 bits needed to
1c564 63 61 74 63 68 20 6f 76 65 72 66 6c 6f 77 20 2a catch overflow *
1c565 2f 0a 20 20 20 20 20 20 69 6e 74 20 73 7a 48 64 /. int szHd
1c566 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 r; /* Si
1c567 7a 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 ze of the header
1c568 20 73 69 7a 65 20 66 69 65 6c 64 20 61 74 20 73 size field at s
1c569 74 61 72 74 20 6f 66 20 72 65 63 6f 72 64 20 2a tart of record *
1c56a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 76 61 69 /. int avai
1c56b 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 l; /* Nu
1c56c 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
1c56d 20 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 20 available data
1c56e 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 */. Mem *pR
1c56f 65 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 eg; /* P
1c570 73 65 75 64 6f 54 61 62 6c 65 20 69 6e 70 75 74 seudoTable input
1c571 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 register */.
1c572 20 7d 20 61 6d 3b 0a 20 20 20 20 73 74 72 75 63 } am;. struc
1c573 74 20 4f 50 5f 41 66 66 69 6e 69 74 79 5f 73 74 t OP_Affinity_st
1c574 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c575 20 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 char *zAffinity
1c576 3b 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e ; /* The affin
1c577 69 74 79 20 74 6f 20 62 65 20 61 70 70 6c 69 65 ity to be applie
1c578 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a d */. Mem *
1c579 70 44 61 74 61 30 3b 20 20 20 20 20 20 20 2f 2a pData0; /*
1c57a 20 46 69 72 73 74 20 72 65 67 69 73 74 65 72 20 First register
1c57b 74 6f 20 77 68 69 63 68 20 74 6f 20 61 70 70 6c to which to appl
1c57c 79 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a 20 20 y affinity */.
1c57d 20 20 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 Mem *pLast;
1c57e 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 72 /* Last r
1c57f 65 67 69 73 74 65 72 20 74 6f 20 77 68 69 63 68 egister to which
1c580 20 74 6f 20 61 70 70 6c 79 20 61 66 66 69 6e 69 to apply affini
1c581 74 79 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 ty */. Mem
1c582 2a 70 52 65 63 3b 20 20 20 20 20 20 20 20 20 2f *pRec; /
1c583 2a 20 43 75 72 72 65 6e 74 20 72 65 67 69 73 74 * Current regist
1c584 65 72 20 2a 2f 0a 20 20 20 20 7d 20 61 6e 3b 0a er */. } an;.
1c585 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4d 61 struct OP_Ma
1c586 6b 65 52 65 63 6f 72 64 5f 73 74 61 63 6b 5f 76 keRecord_stack_v
1c587 61 72 73 20 7b 0a 20 20 20 20 20 20 75 38 20 2a ars {. u8 *
1c588 7a 4e 65 77 52 65 63 6f 72 64 3b 20 20 20 20 20 zNewRecord;
1c589 20 20 20 2f 2a 20 41 20 62 75 66 66 65 72 20 74 /* A buffer t
1c58a 6f 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20 o hold the data
1c58b 66 6f 72 20 74 68 65 20 6e 65 77 20 72 65 63 6f for the new reco
1c58c 72 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 rd */. Mem
1c58d 2a 70 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 *pRec;
1c58e 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 65 /* The new re
1c58f 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 75 36 cord */. u6
1c590 34 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20 4 nData;
1c591 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1c592 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 f bytes of data
1c593 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 69 space */. i
1c594 6e 74 20 6e 48 64 72 3b 20 20 20 20 20 20 20 20 nt nHdr;
1c595 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1c596 6f 66 20 62 79 74 65 73 20 6f 66 20 68 65 61 64 of bytes of head
1c597 65 72 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 er space */.
1c598 20 20 69 36 34 20 6e 42 79 74 65 3b 20 20 20 20 i64 nByte;
1c599 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
1c59a 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20 space required
1c59b 66 6f 72 20 74 68 69 73 20 72 65 63 6f 72 64 20 for this record
1c59c 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 5a 65 */. int nZe
1c59d 72 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ro;
1c59e 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 /* Number of zer
1c59f 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 20 65 o bytes at the e
1c5a0 6e 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 nd of the record
1c5a1 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 56 */. int nV
1c5a2 61 72 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 arint;
1c5a3 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1c5a4 74 65 73 20 69 6e 20 61 20 76 61 72 69 6e 74 20 tes in a varint
1c5a5 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 73 65 72 */. u32 ser
1c5a6 69 61 6c 5f 74 79 70 65 3b 20 20 20 20 20 20 20 ial_type;
1c5a7 2f 2a 20 54 79 70 65 20 66 69 65 6c 64 20 2a 2f /* Type field */
1c5a8 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 44 61 74 . Mem *pDat
1c5a9 61 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a a0; /*
1c5aa 20 46 69 72 73 74 20 66 69 65 6c 64 20 74 6f 20 First field to
1c5ab 62 65 20 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f be combined into
1c5ac 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 the record */.
1c5ad 20 20 20 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b Mem *pLast;
1c5ae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
1c5af 61 73 74 20 66 69 65 6c 64 20 6f 66 20 74 68 65 ast field of the
1c5b0 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 record */.
1c5b1 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 int nField;
1c5b2 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1c5b3 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 r of fields in t
1c5b4 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 he record */.
1c5b5 20 20 20 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 char *zAffini
1c5b6 74 79 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 ty; /* The
1c5b7 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 affinity string
1c5b8 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 for the record
1c5b9 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 66 69 6c */. int fil
1c5ba 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20 20 20 e_format;
1c5bb 2f 2a 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 74 /* File format t
1c5bc 6f 20 75 73 65 20 66 6f 72 20 65 6e 63 6f 64 69 o use for encodi
1c5bd 6e 67 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 ng */. int
1c5be 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
1c5bf 20 20 20 2f 2a 20 53 70 61 63 65 20 75 73 65 64 /* Space used
1c5c0 20 69 6e 20 7a 4e 65 77 52 65 63 6f 72 64 5b 5d in zNewRecord[]
1c5c1 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6c 65 */. int le
1c5c2 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
1c5c3 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61 20 /* Length of a
1c5c4 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20 7d 20 61 field */. } a
1c5c5 6f 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 o;. struct OP
1c5c6 5f 43 6f 75 6e 74 5f 73 74 61 63 6b 5f 76 61 72 _Count_stack_var
1c5c7 73 20 7b 0a 20 20 20 20 20 20 69 36 34 20 6e 45 s {. i64 nE
1c5c8 6e 74 72 79 3b 0a 20 20 20 20 20 20 42 74 43 75 ntry;. BtCu
1c5c9 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 rsor *pCrsr;.
1c5ca 20 7d 20 61 70 3b 0a 20 20 20 20 73 74 72 75 63 } ap;. struc
1c5cb 74 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 5f 73 t OP_Savepoint_s
1c5cc 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c5cd 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 int p1;
1c5ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c5cf 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 50 31 /* Value of P1
1c5d0 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 operand */.
1c5d1 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 char *zName;
1c5d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c5d3 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 73 61 76 /* Name of sav
1c5d4 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 epoint */.
1c5d5 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 20 int nName;.
1c5d6 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 4e 65 77 Savepoint *pNew
1c5d7 3b 0a 20 20 20 20 20 20 53 61 76 65 70 6f 69 6e ;. Savepoin
1c5d8 74 20 2a 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 t *pSavepoint;.
1c5d9 20 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a Savepoint *
1c5da 70 54 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 20 pTmp;. int
1c5db 69 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 iSavepoint;.
1c5dc 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 7d 20 int ii;. }
1c5dd 61 71 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f aq;. struct O
1c5de 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 5f 73 74 61 P_AutoCommit_sta
1c5df 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c5e0 69 6e 74 20 64 65 73 69 72 65 64 41 75 74 6f 43 int desiredAutoC
1c5e1 6f 6d 6d 69 74 3b 0a 20 20 20 20 20 20 69 6e 74 ommit;. int
1c5e2 20 69 52 6f 6c 6c 62 61 63 6b 3b 0a 20 20 20 20 iRollback;.
1c5e3 20 20 69 6e 74 20 74 75 72 6e 4f 6e 41 43 3b 0a int turnOnAC;.
1c5e4 20 20 20 20 7d 20 61 72 3b 0a 20 20 20 20 73 74 } ar;. st
1c5e5 72 75 63 74 20 4f 50 5f 54 72 61 6e 73 61 63 74 ruct OP_Transact
1c5e6 69 6f 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b ion_stack_vars {
1c5e7 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 . Btree *pB
1c5e8 74 3b 0a 20 20 20 20 7d 20 61 73 3b 0a 20 20 20 t;. } as;.
1c5e9 20 73 74 72 75 63 74 20 4f 50 5f 52 65 61 64 43 struct OP_ReadC
1c5ea 6f 6f 6b 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 ookie_stack_vars
1c5eb 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65 {. int iMe
1c5ec 74 61 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 ta;. int iD
1c5ed 62 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f b;. int iCo
1c5ee 6f 6b 69 65 3b 0a 20 20 20 20 7d 20 61 74 3b 0a okie;. } at;.
1c5ef 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65 struct OP_Se
1c5f0 74 43 6f 6f 6b 69 65 5f 73 74 61 63 6b 5f 76 61 tCookie_stack_va
1c5f1 72 73 20 7b 0a 20 20 20 20 20 20 44 62 20 2a 70 rs {. Db *p
1c5f2 44 62 3b 0a 20 20 20 20 7d 20 61 75 3b 0a 20 20 Db;. } au;.
1c5f3 20 20 73 74 72 75 63 74 20 4f 50 5f 56 65 72 69 struct OP_Veri
1c5f4 66 79 43 6f 6f 6b 69 65 5f 73 74 61 63 6b 5f 76 fyCookie_stack_v
1c5f5 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 ars {. int
1c5f6 69 4d 65 74 61 3b 0a 20 20 20 20 20 20 42 74 72 iMeta;. Btr
1c5f7 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 7d 20 61 ee *pBt;. } a
1c5f8 76 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 v;. struct OP
1c5f9 5f 4f 70 65 6e 57 72 69 74 65 5f 73 74 61 63 6b _OpenWrite_stack
1c5fa 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e _vars {. in
1c5fb 74 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 20 t nField;.
1c5fc 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 KeyInfo *pKeyInf
1c5fd 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b o;. int p2;
1c5fe 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a . int iDb;.
1c5ff 20 20 20 20 20 20 69 6e 74 20 77 72 46 6c 61 67 int wrFlag
1c600 3b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 ;. Btree *p
1c601 58 3b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 X;. VdbeCur
1c602 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 20 20 20 sor *pCur;.
1c603 20 44 62 20 2a 70 44 62 3b 0a 20 20 20 20 7d 20 Db *pDb;. }
1c604 61 77 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f aw;. struct O
1c605 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 5f P_OpenEphemeral_
1c606 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c607 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 VdbeCursor *p
1c608 43 78 3b 0a 20 20 20 20 7d 20 61 78 3b 0a 20 20 Cx;. } ax;.
1c609 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e struct OP_Open
1c60a 50 73 65 75 64 6f 5f 73 74 61 63 6b 5f 76 61 72 Pseudo_stack_var
1c60b 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 s {. VdbeCu
1c60c 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 20 20 7d rsor *pCx;. }
1c60d 20 61 79 3b 0a 20 20 20 20 73 74 72 75 63 74 20 ay;. struct
1c60e 4f 50 5f 53 65 65 6b 47 74 5f 73 74 61 63 6b 5f OP_SeekGt_stack_
1c60f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 vars {. int
1c610 20 72 65 73 3b 0a 20 20 20 20 20 20 69 6e 74 20 res;. int
1c611 6f 63 3b 0a 20 20 20 20 20 20 56 64 62 65 43 75 oc;. VdbeCu
1c612 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 rsor *pC;.
1c613 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 UnpackedRecord r
1c614 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69 65 ;. int nFie
1c615 6c 64 3b 0a 20 20 20 20 20 20 69 36 34 20 69 4b ld;. i64 iK
1c616 65 79 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ey; /* The
1c617 72 6f 77 69 64 20 77 65 20 61 72 65 20 74 6f 20 rowid we are to
1c618 73 65 65 6b 20 74 6f 20 2a 2f 0a 20 20 20 20 7d seek to */. }
1c619 20 61 7a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 az;. struct
1c61a 4f 50 5f 53 65 65 6b 5f 73 74 61 63 6b 5f 76 61 OP_Seek_stack_va
1c61b 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 rs {. VdbeC
1c61c 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 7d ursor *pC;. }
1c61d 20 62 61 3b 0a 20 20 20 20 73 74 72 75 63 74 20 ba;. struct
1c61e 4f 50 5f 46 6f 75 6e 64 5f 73 74 61 63 6b 5f 76 OP_Found_stack_v
1c61f 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 ars {. int
1c620 61 6c 72 65 61 64 79 45 78 69 73 74 73 3b 0a 20 alreadyExists;.
1c621 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 VdbeCursor
1c622 2a 70 43 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 *pC;. int r
1c623 65 73 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b es;. Unpack
1c624 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 edRecord *pIdxKe
1c625 79 3b 0a 20 20 20 20 20 20 63 68 61 72 20 61 54 y;. char aT
1c626 65 6d 70 52 65 63 5b 52 4f 55 4e 44 38 28 73 69 empRec[ROUND8(si
1c627 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63 zeof(UnpackedRec
1c628 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d ord)) + sizeof(M
1c629 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a 20 20 20 20 em)*3 + 7];.
1c62a 7d 20 62 62 3b 0a 20 20 20 20 73 74 72 75 63 74 } bb;. struct
1c62b 20 4f 50 5f 49 73 55 6e 69 71 75 65 5f 73 74 61 OP_IsUnique_sta
1c62c 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c62d 75 31 36 20 69 69 3b 0a 20 20 20 20 20 20 56 64 u16 ii;. Vd
1c62e 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 beCursor *pCx;.
1c62f 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 BtCursor *p
1c630 43 72 73 72 3b 0a 20 20 20 20 20 20 75 31 36 20 Crsr;. u16
1c631 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 20 4d 65 nField;. Me
1c632 6d 20 2a 61 4d 65 6d 3b 0a 20 20 20 20 20 20 55 m *aMem;. U
1c633 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b npackedRecord r;
1c634 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c635 20 20 2f 2a 20 42 2d 54 72 65 65 20 69 6e 64 65 /* B-Tree inde
1c636 78 20 73 65 61 72 63 68 20 6b 65 79 20 2a 2f 0a x search key */.
1c637 20 20 20 20 20 20 69 36 34 20 52 3b 20 20 20 20 i64 R;
1c638 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c639 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 /* Rowi
1c63a 64 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 d stored in regi
1c63b 73 74 65 72 20 50 33 20 2a 2f 0a 20 20 20 20 7d ster P3 */. }
1c63c 20 62 63 3b 0a 20 20 20 20 73 74 72 75 63 74 20 bc;. struct
1c63d 4f 50 5f 4e 6f 74 45 78 69 73 74 73 5f 73 74 61 OP_NotExists_sta
1c63e 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c63f 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1c640 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a BtCursor *
1c641 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 pCrsr;. int
1c642 20 72 65 73 3b 0a 20 20 20 20 20 20 75 36 34 20 res;. u64
1c643 69 4b 65 79 3b 0a 20 20 20 20 7d 20 62 64 3b 0a iKey;. } bd;.
1c644 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 65 struct OP_Ne
1c645 77 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76 61 72 wRowid_stack_var
1c646 73 20 7b 0a 20 20 20 20 20 20 69 36 34 20 76 3b s {. i64 v;
1c647 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c648 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 77 69 /* The new rowi
1c649 64 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 43 d */. VdbeC
1c64a 75 72 73 6f 72 20 2a 70 43 3b 20 20 20 20 20 20 ursor *pC;
1c64b 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 66 20 74 /* Cursor of t
1c64c 61 62 6c 65 20 74 6f 20 67 65 74 20 74 68 65 20 able to get the
1c64d 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 20 new rowid */.
1c64e 20 20 20 69 6e 74 20 72 65 73 3b 20 20 20 20 20 int res;
1c64f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 /* Res
1c650 75 6c 74 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 ult of an sqlite
1c651 33 42 74 72 65 65 4c 61 73 74 28 29 20 2a 2f 0a 3BtreeLast() */.
1c652 20 20 20 20 20 20 69 6e 74 20 63 6e 74 3b 20 20 int cnt;
1c653 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c654 43 6f 75 6e 74 65 72 20 74 6f 20 6c 69 6d 69 74 Counter to limit
1c655 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 the number of s
1c656 65 61 72 63 68 65 73 20 2a 2f 0a 20 20 20 20 20 earches */.
1c657 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 Mem *pMem;
1c658 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 /* Regis
1c659 74 65 72 20 68 6f 6c 64 69 6e 67 20 6c 61 72 67 ter holding larg
1c65a 65 73 74 20 72 6f 77 69 64 20 66 6f 72 20 41 55 est rowid for AU
1c65b 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 20 TOINCREMENT */.
1c65c 20 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a VdbeFrame *
1c65d 70 46 72 61 6d 65 3b 20 20 20 20 20 2f 2a 20 52 pFrame; /* R
1c65e 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20 56 44 42 oot frame of VDB
1c65f 45 20 2a 2f 0a 20 20 20 20 7d 20 62 65 3b 0a 20 E */. } be;.
1c660 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 6e 73 struct OP_Ins
1c661 65 72 74 49 6e 74 5f 73 74 61 63 6b 5f 76 61 72 ertInt_stack_var
1c662 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 s {. Mem *p
1c663 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 4d Data; /* M
1c664 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 20 EM cell holding
1c665 64 61 74 61 20 66 6f 72 20 74 68 65 20 72 65 63 data for the rec
1c666 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72 74 ord to be insert
1c667 65 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 ed */. Mem
1c668 2a 70 4b 65 79 3b 20 20 20 20 20 20 20 20 2f 2a *pKey; /*
1c669 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e MEM cell holdin
1c66a 67 20 6b 65 79 20 20 66 6f 72 20 74 68 65 20 72 g key for the r
1c66b 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 ecord */. i
1c66c 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 20 20 64 iKey;
1c66d 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20 /* The integer
1c66e 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 66 6f 72 ROWID or key for
1c66f 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 62 the record to b
1c670 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 e inserted */.
1c671 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a VdbeCursor *
1c672 70 43 3b 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 pC; /* Cursor
1c673 74 6f 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 to table into wh
1c674 69 63 68 20 69 6e 73 65 72 74 20 69 73 20 77 72 ich insert is wr
1c675 69 74 74 65 6e 20 2a 2f 0a 20 20 20 20 20 20 69 itten */. i
1c676 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 nt nZero;
1c677 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 /* Number of ze
1c678 72 6f 2d 62 79 74 65 73 20 74 6f 20 61 70 70 65 ro-bytes to appe
1c679 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 nd */. int
1c67a 73 65 65 6b 52 65 73 75 6c 74 3b 20 20 20 2f 2a seekResult; /*
1c67b 20 52 65 73 75 6c 74 20 6f 66 20 70 72 69 6f 72 Result of prior
1c67c 20 73 65 65 6b 20 6f 72 20 30 20 69 66 20 6e 6f seek or 0 if no
1c67d 20 55 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66 USESEEKRESULT f
1c67e 6c 61 67 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e lag */. con
1c67f 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 2f st char *zDb; /
1c680 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 * database name
1c681 2d 20 75 73 65 64 20 62 79 20 74 68 65 20 75 70 - used by the up
1c682 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20 20 date hook */.
1c683 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1c684 54 62 6c 3b 20 2f 2a 20 54 61 62 6c 65 20 6e 61 Tbl; /* Table na
1c685 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 65 me - used by the
1c686 20 6f 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a opdate hook */.
1c687 20 20 20 20 20 20 69 6e 74 20 6f 70 3b 20 20 20 int op;
1c688 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 /* Opcod
1c689 65 20 66 6f 72 20 75 70 64 61 74 65 20 68 6f 6f e for update hoo
1c68a 6b 3a 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 k: SQLITE_UPDATE
1c68b 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 or SQLITE_INSER
1c68c 54 20 2a 2f 0a 20 20 20 20 7d 20 62 66 3b 0a 20 T */. } bf;.
1c68d 20 20 20 73 74 72 75 63 74 20 4f 50 5f 44 65 6c struct OP_Del
1c68e 65 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b ete_stack_vars {
1c68f 0a 20 20 20 20 20 20 69 36 34 20 69 4b 65 79 3b . i64 iKey;
1c690 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f . VdbeCurso
1c691 72 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62 67 3b r *pC;. } bg;
1c692 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 . struct OP_R
1c693 6f 77 44 61 74 61 5f 73 74 61 63 6b 5f 76 61 72 owData_stack_var
1c694 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 s {. VdbeCu
1c695 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 rsor *pC;.
1c696 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b BtCursor *pCrsr;
1c697 0a 20 20 20 20 20 20 75 33 32 20 6e 3b 0a 20 20 . u32 n;.
1c698 20 20 20 20 69 36 34 20 6e 36 34 3b 0a 20 20 20 i64 n64;.
1c699 20 7d 20 62 68 3b 0a 20 20 20 20 73 74 72 75 63 } bh;. struc
1c69a 74 20 4f 50 5f 52 6f 77 69 64 5f 73 74 61 63 6b t OP_Rowid_stack
1c69b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 _vars {. Vd
1c69c 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 beCursor *pC;.
1c69d 20 20 20 20 69 36 34 20 76 3b 0a 20 20 20 20 20 i64 v;.
1c69e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
1c69f 56 74 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e 73 Vtab;. cons
1c6a0 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 t sqlite3_module
1c6a1 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d *pModule;. }
1c6a2 20 62 69 3b 0a 20 20 20 20 73 74 72 75 63 74 20 bi;. struct
1c6a3 4f 50 5f 4e 75 6c 6c 52 6f 77 5f 73 74 61 63 6b OP_NullRow_stack
1c6a4 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 _vars {. Vd
1c6a5 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 beCursor *pC;.
1c6a6 20 20 7d 20 62 6a 3b 0a 20 20 20 20 73 74 72 75 } bj;. stru
1c6a7 63 74 20 4f 50 5f 4c 61 73 74 5f 73 74 61 63 6b ct OP_Last_stack
1c6a8 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 _vars {. Vd
1c6a9 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 beCursor *pC;.
1c6aa 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 BtCursor *pC
1c6ab 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 rsr;. int r
1c6ac 65 73 3b 0a 20 20 20 20 7d 20 62 6b 3b 0a 20 20 es;. } bk;.
1c6ad 20 20 73 74 72 75 63 74 20 4f 50 5f 52 65 77 69 struct OP_Rewi
1c6ae 6e 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a nd_stack_vars {.
1c6af 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c6b0 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75 *pC;. BtCu
1c6b1 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 rsor *pCrsr;.
1c6b2 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 int res;.
1c6b3 7d 20 62 6c 3b 0a 20 20 20 20 73 74 72 75 63 74 } bl;. struct
1c6b4 20 4f 50 5f 4e 65 78 74 5f 73 74 61 63 6b 5f 76 OP_Next_stack_v
1c6b5 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 ars {. Vdbe
1c6b6 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 Cursor *pC;.
1c6b7 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 BtCursor *pCrs
1c6b8 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 r;. int res
1c6b9 3b 0a 20 20 20 20 7d 20 62 6d 3b 0a 20 20 20 20 ;. } bm;.
1c6ba 73 74 72 75 63 74 20 4f 50 5f 49 64 78 49 6e 73 struct OP_IdxIns
1c6bb 65 72 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b ert_stack_vars {
1c6bc 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f . VdbeCurso
1c6bd 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 r *pC;. BtC
1c6be 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 ursor *pCrsr;.
1c6bf 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20 20 int nKey;.
1c6c0 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
1c6c1 7a 4b 65 79 3b 0a 20 20 20 20 7d 20 62 6e 3b 0a zKey;. } bn;.
1c6c2 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 struct OP_Id
1c6c3 78 44 65 6c 65 74 65 5f 73 74 61 63 6b 5f 76 61 xDelete_stack_va
1c6c4 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 rs {. VdbeC
1c6c5 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 ursor *pC;.
1c6c6 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 BtCursor *pCrsr
1c6c7 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b ;. int res;
1c6c8 0a 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 . UnpackedR
1c6c9 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 7d 20 62 ecord r;. } b
1c6ca 6f 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 o;. struct OP
1c6cb 5f 49 64 78 52 6f 77 69 64 5f 73 74 61 63 6b 5f _IdxRowid_stack_
1c6cc 76 61 72 73 20 7b 0a 20 20 20 20 20 20 42 74 43 vars {. BtC
1c6cd 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 ursor *pCrsr;.
1c6ce 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a VdbeCursor *
1c6cf 70 43 3b 0a 20 20 20 20 20 20 69 36 34 20 72 6f pC;. i64 ro
1c6d0 77 69 64 3b 0a 20 20 20 20 7d 20 62 70 3b 0a 20 wid;. } bp;.
1c6d1 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 78 struct OP_Idx
1c6d2 47 45 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a GE_stack_vars {.
1c6d3 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c6d4 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 6e 74 20 *pC;. int
1c6d5 72 65 73 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 res;. Unpac
1c6d6 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 kedRecord r;.
1c6d7 20 7d 20 62 71 3b 0a 20 20 20 20 73 74 72 75 63 } bq;. struc
1c6d8 74 20 4f 50 5f 44 65 73 74 72 6f 79 5f 73 74 61 t OP_Destroy_sta
1c6d9 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c6da 69 6e 74 20 69 4d 6f 76 65 64 3b 0a 20 20 20 20 int iMoved;.
1c6db 20 20 69 6e 74 20 69 43 6e 74 3b 0a 20 20 20 20 int iCnt;.
1c6dc 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b 0a 20 Vdbe *pVdbe;.
1c6dd 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 int iDb;.
1c6de 20 20 7d 20 62 72 3b 0a 20 20 20 20 73 74 72 75 } br;. stru
1c6df 63 74 20 4f 50 5f 43 6c 65 61 72 5f 73 74 61 63 ct OP_Clear_stac
1c6e0 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 k_vars {. i
1c6e1 6e 74 20 6e 43 68 61 6e 67 65 3b 0a 20 20 20 20 nt nChange;.
1c6e2 7d 20 62 73 3b 0a 20 20 20 20 73 74 72 75 63 74 } bs;. struct
1c6e3 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 5f OP_CreateTable_
1c6e4 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c6e5 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 20 20 int pgno;.
1c6e6 20 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 int flags;.
1c6e7 20 20 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 20 Db *pDb;.
1c6e8 20 7d 20 62 74 3b 0a 20 20 20 20 73 74 72 75 63 } bt;. struc
1c6e9 74 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 t OP_ParseSchema
1c6ea 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c6eb 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 int iDb;.
1c6ec 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1c6ed 4d 61 73 74 65 72 3b 0a 20 20 20 20 20 20 63 68 Master;. ch
1c6ee 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 20 20 20 ar *zSql;.
1c6ef 49 6e 69 74 44 61 74 61 20 69 6e 69 74 44 61 74 InitData initDat
1c6f0 61 3b 0a 20 20 20 20 7d 20 62 75 3b 0a 20 20 20 a;. } bu;.
1c6f1 20 73 74 72 75 63 74 20 4f 50 5f 49 6e 74 65 67 struct OP_Integ
1c6f2 72 69 74 79 43 6b 5f 73 74 61 63 6b 5f 76 61 72 rityCk_stack_var
1c6f3 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 52 s {. int nR
1c6f4 6f 6f 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d oot; /* Num
1c6f5 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 74 6f ber of tables to
1c6f6 20 63 68 65 63 6b 2e 20 20 28 4e 75 6d 62 65 72 check. (Number
1c6f7 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 2e 29 of root pages.)
1c6f8 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 2a 61 */. int *a
1c6f9 52 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 41 72 72 Root; /* Arr
1c6fa 61 79 20 6f 66 20 72 6f 6f 74 70 61 67 65 20 6e ay of rootpage n
1c6fb 75 6d 62 65 72 73 20 66 6f 72 20 74 61 62 6c 65 umbers for table
1c6fc 73 20 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 s to be checked
1c6fd 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 20 */. int j;
1c6fe 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
1c6ff 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 counter */.
1c700 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 int nErr;
1c701 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
1c702 72 72 6f 72 73 20 72 65 70 6f 72 74 65 64 20 2a rrors reported *
1c703 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 3b /. char *z;
1c704 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 /* Text
1c705 6f 66 20 74 68 65 20 65 72 72 6f 72 20 72 65 70 of the error rep
1c706 6f 72 74 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d ort */. Mem
1c707 20 2a 70 6e 45 72 72 3b 20 20 20 20 20 2f 2a 20 *pnErr; /*
1c708 52 65 67 69 73 74 65 72 20 6b 65 65 70 69 6e 67 Register keeping
1c709 20 74 72 61 63 6b 20 6f 66 20 65 72 72 6f 72 73 track of errors
1c70a 20 72 65 6d 61 69 6e 69 6e 67 20 2a 2f 0a 20 20 remaining */.
1c70b 20 20 7d 20 62 76 3b 0a 20 20 20 20 73 74 72 75 } bv;. stru
1c70c 63 74 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 5f ct OP_RowSetAdd_
1c70d 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c70e 20 20 20 4d 65 6d 20 2a 70 49 64 78 3b 0a 20 20 Mem *pIdx;.
1c70f 20 20 20 20 4d 65 6d 20 2a 70 56 61 6c 3b 0a 20 Mem *pVal;.
1c710 20 20 20 7d 20 62 77 3b 0a 20 20 20 20 73 74 72 } bw;. str
1c711 75 63 74 20 4f 50 5f 52 6f 77 53 65 74 52 65 61 uct OP_RowSetRea
1c712 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 d_stack_vars {.
1c713 20 20 20 20 20 4d 65 6d 20 2a 70 49 64 78 3b 0a Mem *pIdx;.
1c714 20 20 20 20 20 20 69 36 34 20 76 61 6c 3b 0a 20 i64 val;.
1c715 20 20 20 7d 20 62 78 3b 0a 20 20 20 20 73 74 72 } bx;. str
1c716 75 63 74 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 uct OP_RowSetTes
1c717 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 t_stack_vars {.
1c718 20 20 20 20 20 69 6e 74 20 69 53 65 74 3b 0a 20 int iSet;.
1c719 20 20 20 20 20 69 6e 74 20 65 78 69 73 74 73 3b int exists;
1c71a 0a 20 20 20 20 7d 20 62 79 3b 0a 20 20 20 20 73 . } by;. s
1c71b 74 72 75 63 74 20 4f 50 5f 50 72 6f 67 72 61 6d truct OP_Program
1c71c 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c71d 20 20 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 int nMem;
1c71e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1c71f 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 umber of memory
1c720 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 73 75 registers for su
1c721 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20 b-program */.
1c722 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 int nByte;
1c723 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 /* By
1c724 74 65 73 20 6f 66 20 72 75 6e 74 69 6d 65 20 73 tes of runtime s
1c725 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f pace required fo
1c726 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f r sub-program */
1c727 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 74 3b . Mem *pRt;
1c728 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1c729 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 61 6c * Register to al
1c72a 6c 6f 63 61 74 65 20 72 75 6e 74 69 6d 65 20 73 locate runtime s
1c72b 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 4d 65 pace */. Me
1c72c 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 m *pMem;
1c72d 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f /* Used to
1c72e 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 iterate through
1c72f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 2a 2f memory cells */
1c730 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64 . Mem *pEnd
1c731 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1c732 2a 20 4c 61 73 74 20 6d 65 6d 6f 72 79 20 63 65 * Last memory ce
1c733 6c 6c 20 69 6e 20 6e 65 77 20 61 72 72 61 79 20 ll in new array
1c734 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 46 72 61 */. VdbeFra
1c735 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 me *pFrame;
1c736 20 2f 2a 20 4e 65 77 20 76 64 62 65 20 66 72 61 /* New vdbe fra
1c737 6d 65 20 74 6f 20 65 78 65 63 75 74 65 20 69 6e me to execute in
1c738 20 2a 2f 0a 20 20 20 20 20 20 53 75 62 50 72 6f */. SubPro
1c739 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 gram *pProgram;
1c73a 20 20 2f 2a 20 53 75 62 2d 70 72 6f 67 72 61 6d /* Sub-program
1c73b 20 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 20 to execute */.
1c73c 20 20 20 20 20 76 6f 69 64 20 2a 74 3b 20 20 20 void *t;
1c73d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c73e 54 6f 6b 65 6e 20 69 64 65 6e 74 69 66 79 69 6e Token identifyin
1c73f 67 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 20 g trigger */.
1c740 20 7d 20 62 7a 3b 0a 20 20 20 20 73 74 72 75 63 } bz;. struc
1c741 74 20 4f 50 5f 50 61 72 61 6d 5f 73 74 61 63 6b t OP_Param_stack
1c742 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 _vars {. Vd
1c743 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b beFrame *pFrame;
1c744 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 49 6e 3b . Mem *pIn;
1c745 0a 20 20 20 20 7d 20 63 61 3b 0a 20 20 20 20 73 . } ca;. s
1c746 74 72 75 63 74 20 4f 50 5f 4d 65 6d 4d 61 78 5f truct OP_MemMax_
1c747 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c748 20 20 20 4d 65 6d 20 2a 70 49 6e 31 3b 0a 20 20 Mem *pIn1;.
1c749 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 VdbeFrame *p
1c74a 46 72 61 6d 65 3b 0a 20 20 20 20 7d 20 63 62 3b Frame;. } cb;
1c74b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 41 . struct OP_A
1c74c 67 67 53 74 65 70 5f 73 74 61 63 6b 5f 76 61 72 ggStep_stack_var
1c74d 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b s {. int n;
1c74e 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 . int i;.
1c74f 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 Mem *pMem;.
1c750 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a Mem *pRec;.
1c751 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f sqlite3_co
1c752 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20 20 ntext ctx;.
1c753 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
1c754 2a 61 70 56 61 6c 3b 0a 20 20 20 20 7d 20 63 63 *apVal;. } cc
1c755 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c756 41 67 67 46 69 6e 61 6c 5f 73 74 61 63 6b 5f 76 AggFinal_stack_v
1c757 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 ars {. Mem
1c758 2a 70 4d 65 6d 3b 0a 20 20 20 20 7d 20 63 64 3b *pMem;. } cd;
1c759 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 . struct OP_I
1c75a 6e 63 72 56 61 63 75 75 6d 5f 73 74 61 63 6b 5f ncrVacuum_stack_
1c75b 76 61 72 73 20 7b 0a 20 20 20 20 20 20 42 74 72 vars {. Btr
1c75c 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 7d 20 63 ee *pBt;. } c
1c75d 65 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 e;. struct OP
1c75e 5f 56 42 65 67 69 6e 5f 73 74 61 63 6b 5f 76 61 _VBegin_stack_va
1c75f 72 73 20 7b 0a 20 20 20 20 20 20 56 54 61 62 6c rs {. VTabl
1c760 65 20 2a 70 56 54 61 62 3b 0a 20 20 20 20 7d 20 e *pVTab;. }
1c761 63 66 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f cf;. struct O
1c762 50 5f 56 4f 70 65 6e 5f 73 74 61 63 6b 5f 76 61 P_VOpen_stack_va
1c763 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 rs {. VdbeC
1c764 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 20 ursor *pCur;.
1c765 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f sqlite3_vtab_
1c766 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 cursor *pVtabCur
1c767 73 6f 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 sor;. sqlit
1c768 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a e3_vtab *pVtab;.
1c769 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 6f sqlite3_mo
1c76a 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 dule *pModule;.
1c76b 20 20 20 7d 20 63 67 3b 0a 20 20 20 20 73 74 72 } cg;. str
1c76c 75 63 74 20 4f 50 5f 56 46 69 6c 74 65 72 5f 73 uct OP_VFilter_s
1c76d 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c76e 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20 int nArg;.
1c76f 20 20 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 20 int iQuery;.
1c770 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 const sqlite
1c771 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 3_module *pModul
1c772 65 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 51 e;. Mem *pQ
1c773 75 65 72 79 3b 0a 20 20 20 20 20 20 4d 65 6d 20 uery;. Mem
1c774 2a 70 41 72 67 63 3b 0a 20 20 20 20 20 20 73 71 *pArgc;. sq
1c775 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
1c776 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a r *pVtabCursor;.
1c777 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 sqlite3_vt
1c778 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 ab *pVtab;.
1c779 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 VdbeCursor *pCu
1c77a 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 r;. int res
1c77b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 ;. int i;.
1c77c 20 20 20 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 Mem **apArg
1c77d 3b 0a 20 20 20 20 7d 20 63 68 3b 0a 20 20 20 20 ;. } ch;.
1c77e 73 74 72 75 63 74 20 4f 50 5f 56 43 6f 6c 75 6d struct OP_VColum
1c77f 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 n_stack_vars {.
1c780 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 sqlite3_vta
1c781 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 b *pVtab;.
1c782 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f const sqlite3_mo
1c783 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 dule *pModule;.
1c784 20 20 20 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b Mem *pDest;
1c785 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 . sqlite3_c
1c786 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b ontext sContext;
1c787 0a 20 20 20 20 7d 20 63 69 3b 0a 20 20 20 20 73 . } ci;. s
1c788 74 72 75 63 74 20 4f 50 5f 56 4e 65 78 74 5f 73 truct OP_VNext_s
1c789 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c78a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
1c78b 70 56 74 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e pVtab;. con
1c78c 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c st sqlite3_modul
1c78d 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 e *pModule;.
1c78e 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 int res;.
1c78f 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 VdbeCursor *pCu
1c790 72 3b 0a 20 20 20 20 7d 20 63 6a 3b 0a 20 20 20 r;. } cj;.
1c791 20 73 74 72 75 63 74 20 4f 50 5f 56 52 65 6e 61 struct OP_VRena
1c792 6d 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a me_stack_vars {.
1c793 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 sqlite3_vt
1c794 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 ab *pVtab;.
1c795 20 4d 65 6d 20 2a 70 4e 61 6d 65 3b 0a 20 20 20 Mem *pName;.
1c796 20 7d 20 63 6b 3b 0a 20 20 20 20 73 74 72 75 63 } ck;. struc
1c797 74 20 4f 50 5f 56 55 70 64 61 74 65 5f 73 74 61 t OP_VUpdate_sta
1c798 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c799 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
1c79a 74 61 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 tab;. sqlit
1c79b 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 e3_module *pModu
1c79c 6c 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 41 le;. int nA
1c79d 72 67 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b rg;. int i;
1c79e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e . sqlite_in
1c79f 74 36 34 20 72 6f 77 69 64 3b 0a 20 20 20 20 20 t64 rowid;.
1c7a0 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 20 20 Mem **apArg;.
1c7a1 20 20 20 20 4d 65 6d 20 2a 70 58 3b 0a 20 20 20 Mem *pX;.
1c7a2 20 7d 20 63 6c 3b 0a 20 20 20 20 73 74 72 75 63 } cl;. struc
1c7a3 74 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 5f 73 t OP_Pagecount_s
1c7a4 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c7a5 20 20 69 6e 74 20 70 31 3b 0a 20 20 20 20 20 20 int p1;.
1c7a6 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 int nPage;.
1c7a7 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a Pager *pPager;.
1c7a8 20 20 20 20 7d 20 63 6d 3b 0a 20 20 20 20 73 74 } cm;. st
1c7a9 72 75 63 74 20 4f 50 5f 54 72 61 63 65 5f 73 74 ruct OP_Trace_st
1c7aa 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c7ab 20 63 68 61 72 20 2a 7a 54 72 61 63 65 3b 0a 20 char *zTrace;.
1c7ac 20 20 20 7d 20 63 6e 3b 0a 20 20 7d 20 75 3b 0a } cn;. } u;.
1c7ad 20 20 2f 2a 20 45 6e 64 20 61 75 74 6f 6d 61 74 /* End automat
1c7ae 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 ically generated
1c7af 20 63 6f 64 65 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a code. ********
1c7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c7b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c7b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c7b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 ************/..
1c7b4 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 assert( p->magi
1c7b5 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 c==VDBE_MAGIC_RU
1c7b6 4e 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 N ); /* sqlite3
1c7b7 5f 73 74 65 70 28 29 20 76 65 72 69 66 69 65 73 _step() verifies
1c7b8 20 74 68 69 73 20 2a 2f 0a 20 20 61 73 73 65 72 this */. asser
1c7b9 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 t( db->magic==SQ
1c7ba 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 LITE_MAGIC_BUSY
1c7bb 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
1c7bc 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 MutexArrayEnter(
1c7bd 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 72 63 3d p);. if( p->rc=
1c7be 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b =SQLITE_NOMEM ){
1c7bf 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 . /* This hap
1c7c0 70 65 6e 73 20 69 66 20 61 20 6d 61 6c 6c 6f 63 pens if a malloc
1c7c1 28 29 20 69 6e 73 69 64 65 20 61 20 63 61 6c 6c () inside a call
1c7c2 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 to sqlite3_colu
1c7c3 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 20 20 20 mn_text() or.
1c7c4 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ** sqlite3_colu
1c7c5 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 61 69 6c mn_text16() fail
1c7c6 65 64 2e 20 20 2a 2f 0a 20 20 20 20 67 6f 74 6f ed. */. goto
1c7c7 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 61 no_mem;. }. a
1c7c8 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 ssert( p->rc==SQ
1c7c9 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 LITE_OK || p->rc
1c7ca 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 3b ==SQLITE_BUSY );
1c7cb 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 . p->rc = SQLIT
1c7cc 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 E_OK;. assert(
1c7cd 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 30 20 29 3b p->explain==0 );
1c7ce 0a 20 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 . p->pResultSet
1c7cf 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 62 75 73 79 = 0;. db->busy
1c7d0 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20 Handler.nBusy =
1c7d1 30 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 0;. CHECK_FOR_I
1c7d2 4e 54 45 52 52 55 50 54 3b 0a 20 20 73 71 6c 69 NTERRUPT;. sqli
1c7d3 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 te3VdbeIOTraceSq
1c7d4 6c 28 70 29 3b 0a 23 69 66 64 65 66 20 53 51 4c l(p);.#ifdef SQL
1c7d5 49 54 45 5f 44 45 42 55 47 0a 20 20 73 71 6c 69 ITE_DEBUG. sqli
1c7d6 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 te3BeginBenignMa
1c7d7 6c 6c 6f 63 28 29 3b 0a 20 20 69 66 28 20 70 2d lloc();. if( p-
1c7d8 3e 70 63 3d 3d 30 20 0a 20 20 20 26 26 20 28 28 >pc==0 . && ((
1c7d9 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 p->db->flags & S
1c7da 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e QLITE_VdbeListin
1c7db 67 29 20 7c 7c 20 66 69 6c 65 45 78 69 73 74 73 g) || fileExists
1c7dc 28 64 62 2c 20 22 76 64 62 65 5f 65 78 70 6c 61 (db, "vdbe_expla
1c7dd 69 6e 22 29 29 0a 20 20 29 7b 0a 20 20 20 20 69 in")). ){. i
1c7de 6e 74 20 69 3b 0a 20 20 20 20 70 72 69 6e 74 66 nt i;. printf
1c7df 28 22 56 44 42 45 20 50 72 6f 67 72 61 6d 20 4c ("VDBE Program L
1c7e0 69 73 74 69 6e 67 3a 5c 6e 22 29 3b 0a 20 20 20 isting:\n");.
1c7e1 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e sqlite3VdbePrin
1c7e2 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 66 6f 72 tSql(p);. for
1c7e3 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 (i=0; i<p->nOp;
1c7e4 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 i++){. sqli
1c7e5 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 73 te3VdbePrintOp(s
1c7e6 74 64 6f 75 74 2c 20 69 2c 20 26 70 2d 3e 61 4f tdout, i, &p->aO
1c7e7 70 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d p[i]);. }. }
1c7e8 0a 20 20 69 66 28 20 66 69 6c 65 45 78 69 73 74 . if( fileExist
1c7e9 73 28 64 62 2c 20 22 76 64 62 65 5f 74 72 61 63 s(db, "vdbe_trac
1c7ea 65 22 29 20 29 7b 0a 20 20 20 20 70 2d 3e 74 72 e") ){. p->tr
1c7eb 61 63 65 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 ace = stdout;.
1c7ec 7d 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 }. sqlite3EndBe
1c7ed 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 23 65 nignMalloc();.#e
1c7ee 6e 64 69 66 0a 20 20 66 6f 72 28 70 63 3d 70 2d ndif. for(pc=p-
1c7ef 3e 70 63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f >pc; rc==SQLITE_
1c7f0 4f 4b 3b 20 70 63 2b 2b 29 7b 0a 20 20 20 20 61 OK; pc++){. a
1c7f1 73 73 65 72 74 28 20 70 63 3e 3d 30 20 26 26 20 ssert( pc>=0 &&
1c7f2 70 63 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 20 pc<p->nOp );.
1c7f3 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
1c7f4 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f ailed ) goto no_
1c7f5 6d 65 6d 3b 0a 23 69 66 64 65 66 20 56 44 42 45 mem;.#ifdef VDBE
1c7f6 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20 6f 72 69 _PROFILE. ori
1c7f7 67 50 63 20 3d 20 70 63 3b 0a 20 20 20 20 73 74 gPc = pc;. st
1c7f8 61 72 74 20 3d 20 73 71 6c 69 74 65 33 48 77 74 art = sqlite3Hwt
1c7f9 69 6d 65 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 ime();.#endif.
1c7fa 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b pOp = &p->aOp[
1c7fb 70 63 5d 3b 0a 0a 20 20 20 20 2f 2a 20 4f 6e 6c pc];.. /* Onl
1c7fc 79 20 61 6c 6c 6f 77 20 74 72 61 63 69 6e 67 20 y allow tracing
1c7fd 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 if SQLITE_DEBUG
1c7fe 69 73 20 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 is defined..
1c7ff 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
1c800 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 20 70 _DEBUG. if( p
1c801 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 ->trace ){.
1c802 20 69 66 28 20 70 63 3d 3d 30 20 29 7b 0a 20 20 if( pc==0 ){.
1c803 20 20 20 20 20 20 70 72 69 6e 74 66 28 22 56 44 printf("VD
1c804 42 45 20 45 78 65 63 75 74 69 6f 6e 20 54 72 61 BE Execution Tra
1c805 63 65 3a 5c 6e 22 29 3b 0a 20 20 20 20 20 20 20 ce:\n");.
1c806 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e sqlite3VdbePrin
1c807 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 20 20 7d tSql(p);. }
1c808 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1c809 62 65 50 72 69 6e 74 4f 70 28 70 2d 3e 74 72 61 bePrintOp(p->tra
1c80a 63 65 2c 20 70 63 2c 20 70 4f 70 29 3b 0a 20 20 ce, pc, pOp);.
1c80b 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 }. if( p->t
1c80c 72 61 63 65 3d 3d 30 20 26 26 20 70 63 3d 3d 30 race==0 && pc==0
1c80d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1c80e 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 3BeginBenignMall
1c80f 6f 63 28 29 3b 0a 20 20 20 20 20 20 69 66 28 20 oc();. if(
1c810 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c 20 22 fileExists(db, "
1c811 76 64 62 65 5f 73 71 6c 74 72 61 63 65 22 29 20 vdbe_sqltrace")
1c812 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1c813 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 70 e3VdbePrintSql(p
1c814 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1c815 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
1c816 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 7d nMalloc();. }
1c817 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 0a 0a .#endif. ..
1c818 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 /* Check to
1c819 73 65 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 see if we need t
1c81a 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6e o simulate an in
1c81b 74 65 72 72 75 70 74 2e 20 20 54 68 69 73 20 6f terrupt. This o
1c81c 6e 6c 79 20 68 61 70 70 65 6e 73 0a 20 20 20 20 nly happens.
1c81d 2a 2a 20 69 66 20 77 65 20 68 61 76 65 20 61 20 ** if we have a
1c81e 73 70 65 63 69 61 6c 20 74 65 73 74 20 62 75 69 special test bui
1c81f 6c 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65 ld.. */.#ifde
1c820 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 f SQLITE_TEST.
1c821 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e if( sqlite3_in
1c822 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 3e 30 20 terrupt_count>0
1c823 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1c824 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 _interrupt_count
1c825 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 --;. if( sq
1c826 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f lite3_interrupt_
1c827 63 6f 75 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 count==0 ){.
1c828 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 sqlite3_inte
1c829 72 72 75 70 74 28 64 62 29 3b 0a 20 20 20 20 20 rrupt(db);.
1c82a 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
1c82b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1c82c 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 OMIT_PROGRESS_CA
1c82d 4c 4c 42 41 43 4b 0a 20 20 20 20 2f 2a 20 43 61 LLBACK. /* Ca
1c82e 6c 6c 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 ll the progress
1c82f 63 61 6c 6c 62 61 63 6b 20 69 66 20 69 74 20 69 callback if it i
1c830 73 20 63 6f 6e 66 69 67 75 72 65 64 20 61 6e 64 s configured and
1c831 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6e 75 the required nu
1c832 6d 62 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20 56 mber. ** of V
1c833 44 42 45 20 6f 70 73 20 68 61 76 65 20 62 65 65 DBE ops have bee
1c834 6e 20 65 78 65 63 75 74 65 64 20 28 65 69 74 68 n executed (eith
1c835 65 72 20 73 69 6e 63 65 20 74 68 69 73 20 69 6e er since this in
1c836 76 6f 63 61 74 69 6f 6e 20 6f 66 0a 20 20 20 20 vocation of.
1c837 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 ** sqlite3VdbeEx
1c838 65 63 28 29 20 6f 72 20 73 69 6e 63 65 20 6c 61 ec() or since la
1c839 73 74 20 74 69 6d 65 20 74 68 65 20 70 72 6f 67 st time the prog
1c83a 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 77 61 ress callback wa
1c83b 73 20 63 61 6c 6c 65 64 29 2e 0a 20 20 20 20 2a s called).. *
1c83c 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72 65 73 * If the progres
1c83d 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 s callback retur
1c83e 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 65 78 69 ns non-zero, exi
1c83f 74 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 t the virtual ma
1c840 63 68 69 6e 65 20 77 69 74 68 0a 20 20 20 20 2a chine with. *
1c841 2a 20 61 20 72 65 74 75 72 6e 20 63 6f 64 65 20 * a return code
1c842 53 51 4c 49 54 45 5f 41 42 4f 52 54 2e 0a 20 20 SQLITE_ABORT..
1c843 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d */. if( db-
1c844 3e 78 50 72 6f 67 72 65 73 73 20 29 7b 0a 20 20 >xProgress ){.
1c845 20 20 20 20 69 66 28 20 64 62 2d 3e 6e 50 72 6f if( db->nPro
1c846 67 72 65 73 73 4f 70 73 3d 3d 6e 50 72 6f 67 72 gressOps==nProgr
1c847 65 73 73 4f 70 73 20 29 7b 0a 20 20 20 20 20 20 essOps ){.
1c848 20 20 69 6e 74 20 70 72 63 3b 0a 20 20 20 20 20 int prc;.
1c849 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 if( sqlite3Sa
1c84a 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f fetyOff(db) ) go
1c84b 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1c84c 6d 69 73 75 73 65 3b 0a 20 20 20 20 20 20 20 20 misuse;.
1c84d 70 72 63 20 3d 64 62 2d 3e 78 50 72 6f 67 72 65 prc =db->xProgre
1c84e 73 73 28 64 62 2d 3e 70 50 72 6f 67 72 65 73 73 ss(db->pProgress
1c84f 41 72 67 29 3b 0a 20 20 20 20 20 20 20 20 69 66 Arg);. if
1c850 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1c851 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f n(db) ) goto abo
1c852 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1c853 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 ;. if( pr
1c854 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 c!=0 ){.
1c855 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e rc = SQLITE_IN
1c856 54 45 52 52 55 50 54 3b 0a 20 20 20 20 20 20 20 TERRUPT;.
1c857 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 goto vdbe_err
1c858 6f 72 5f 68 61 6c 74 3b 0a 20 20 20 20 20 20 20 or_halt;.
1c859 20 7d 0a 20 20 20 20 20 20 20 20 6e 50 72 6f 67 }. nProg
1c85a 72 65 73 73 4f 70 73 20 3d 20 30 3b 0a 20 20 20 ressOps = 0;.
1c85b 20 20 20 7d 0a 20 20 20 20 20 20 6e 50 72 6f 67 }. nProg
1c85c 72 65 73 73 4f 70 73 2b 2b 3b 0a 20 20 20 20 7d ressOps++;. }
1c85d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 .#endif.. /*
1c85e 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65 74 75 70 20 Do common setup
1c85f 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 61 processing for a
1c860 6e 79 20 6f 70 63 6f 64 65 20 74 68 61 74 20 69 ny opcode that i
1c861 73 20 6d 61 72 6b 65 64 0a 20 20 20 20 2a 2a 20 s marked. **
1c862 77 69 74 68 20 74 68 65 20 22 6f 75 74 32 2d 70 with the "out2-p
1c863 72 65 72 65 6c 65 61 73 65 22 20 74 61 67 2e 20 rerelease" tag.
1c864 20 53 75 63 68 20 6f 70 63 6f 64 65 73 20 68 61 Such opcodes ha
1c865 76 65 20 61 20 73 69 6e 67 6c 65 0a 20 20 20 20 ve a single.
1c866 2a 2a 20 6f 75 74 70 75 74 20 77 68 69 63 68 20 ** output which
1c867 69 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 is specified by
1c868 74 68 65 20 50 32 20 70 61 72 61 6d 65 74 65 72 the P2 parameter
1c869 2e 20 20 54 68 65 20 50 32 20 72 65 67 69 73 74 . The P2 regist
1c86a 65 72 0a 20 20 20 20 2a 2a 20 69 73 20 69 6e 69 er. ** is ini
1c86b 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 20 4e 55 tialized to a NU
1c86c 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6f LL.. */. o
1c86d 70 50 72 6f 70 65 72 74 79 20 3d 20 6f 70 63 6f pProperty = opco
1c86e 64 65 50 72 6f 70 65 72 74 79 5b 70 4f 70 2d 3e deProperty[pOp->
1c86f 6f 70 63 6f 64 65 5d 3b 0a 20 20 20 20 69 66 28 opcode];. if(
1c870 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f (opProperty & O
1c871 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c PFLG_OUT2_PREREL
1c872 45 41 53 45 29 21 3d 30 20 29 7b 0a 20 20 20 20 EASE)!=0 ){.
1c873 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1c874 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 2>0 );. ass
1c875 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d ert( pOp->p2<=p-
1c876 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 >nMem );. p
1c877 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Out = &p->aMem[p
1c878 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 73 Op->p2];. s
1c879 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1c87a 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 4f 75 easeExternal(pOu
1c87b 74 29 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e t);. pOut->
1c87c 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1c87d 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6e 20 ;. pOut->n
1c87e 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 20 = 0;. }else.
1c87f 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f 6d 6d 6f . /* Do commo
1c880 6e 20 73 65 74 75 70 20 66 6f 72 20 6f 70 63 6f n setup for opco
1c881 64 65 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 des marked with
1c882 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f one of the follo
1c883 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63 6f 6d 62 wing. ** comb
1c884 69 6e 61 74 69 6f 6e 73 20 6f 66 20 70 72 6f 70 inations of prop
1c885 65 72 74 69 65 73 2e 0a 20 20 20 20 2a 2a 0a 20 erties.. **.
1c886 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **
1c887 69 6e 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 in1. **
1c888 20 20 20 20 20 69 6e 31 20 69 6e 32 0a 20 20 20 in1 in2.
1c889 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69 6e ** in
1c88a 31 20 69 6e 32 20 6f 75 74 33 0a 20 20 20 20 2a 1 in2 out3. *
1c88b 2a 20 20 20 20 20 20 20 20 20 20 20 69 6e 31 20 * in1
1c88c 69 6e 33 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a in3. **. *
1c88d 2a 20 56 61 72 69 61 62 6c 65 73 20 70 49 6e 31 * Variables pIn1
1c88e 2c 20 70 49 6e 32 2c 20 61 6e 64 20 70 49 6e 33 , pIn2, and pIn3
1c88f 20 61 72 65 20 6d 61 64 65 20 74 6f 20 70 6f 69 are made to poi
1c890 6e 74 20 74 6f 20 61 70 70 72 6f 70 72 69 61 74 nt to appropriat
1c891 65 0a 20 20 20 20 2a 2a 20 72 65 67 69 73 74 65 e. ** registe
1c892 72 73 20 66 6f 72 20 69 6e 70 75 74 73 2e 20 20 rs for inputs.
1c893 56 61 72 69 61 62 6c 65 20 70 4f 75 74 20 70 6f Variable pOut po
1c894 69 6e 74 73 20 74 6f 20 74 68 65 20 6f 75 74 70 ints to the outp
1c895 75 74 20 72 65 67 69 73 74 65 72 2e 0a 20 20 20 ut register..
1c896 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 6f 70 50 */. if( (opP
1c897 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f roperty & OPFLG_
1c898 49 4e 31 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 IN1)!=0 ){.
1c899 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 assert( pOp->p1
1c89a 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 >0 );. asse
1c89b 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e rt( pOp->p1<=p->
1c89c 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 49 nMem );. pI
1c89d 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f n1 = &p->aMem[pO
1c89e 70 2d 3e 70 31 5d 3b 0a 20 20 20 20 20 20 52 45 p->p1];. RE
1c89f 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
1c8a0 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 20 ->p1, pIn1);.
1c8a1 20 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 if( (opProper
1c8a2 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 32 29 21 ty & OPFLG_IN2)!
1c8a3 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 =0 ){. as
1c8a4 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 sert( pOp->p2>0
1c8a5 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 );. asser
1c8a6 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e t( pOp->p2<=p->n
1c8a7 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 70 Mem );. p
1c8a8 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 In2 = &p->aMem[p
1c8a9 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 20 Op->p2];.
1c8aa 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1c8ab 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b 0a pOp->p2, pIn2);.
1c8ac 20 20 20 20 20 20 20 20 2f 2a 20 41 73 20 63 75 /* As cu
1c8ad 72 72 65 6e 74 6c 79 20 69 6d 70 6c 65 6d 65 6e rrently implemen
1c8ae 74 65 64 2c 20 69 6e 32 20 69 6d 70 6c 69 65 73 ted, in2 implies
1c8af 20 6f 75 74 33 2e 20 20 54 68 65 72 65 20 69 73 out3. There is
1c8b0 20 6e 6f 20 72 65 61 73 6f 6e 0a 20 20 20 20 20 no reason.
1c8b1 20 20 20 2a 2a 20 77 68 79 20 74 68 69 73 20 68 ** why this h
1c8b2 61 73 20 74 6f 20 62 65 2c 20 69 74 20 6a 75 73 as to be, it jus
1c8b3 74 20 77 6f 72 6b 65 64 20 6f 75 74 20 74 68 61 t worked out tha
1c8b4 74 20 77 61 79 2e 20 2a 2f 0a 20 20 20 20 20 20 t way. */.
1c8b5 20 20 61 73 73 65 72 74 28 20 28 6f 70 50 72 6f assert( (opPro
1c8b6 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55 perty & OPFLG_OU
1c8b7 54 33 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 T3)!=0 );.
1c8b8 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1c8b9 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 3>0 );. a
1c8ba 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d ssert( pOp->p3<=
1c8bb 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 p->nMem );.
1c8bc 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d pOut = &p->aM
1c8bd 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 em[pOp->p3];.
1c8be 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f 70 }else if( (op
1c8bf 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 Property & OPFLG
1c8c0 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 _IN3)!=0 ){.
1c8c1 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1c8c2 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 >p3>0 );.
1c8c3 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 assert( pOp->p3
1c8c4 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 <=p->nMem );.
1c8c5 20 20 20 20 20 70 49 6e 33 20 3d 20 26 70 2d 3e pIn3 = &p->
1c8c6 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 aMem[pOp->p3];.
1c8c7 20 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f REGISTER_
1c8c8 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 TRACE(pOp->p3, p
1c8c9 49 6e 33 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 In3);. }.
1c8ca 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f 70 50 }else if( (opP
1c8cb 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f roperty & OPFLG_
1c8cc 49 4e 32 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 IN2)!=0 ){.
1c8cd 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 assert( pOp->p2
1c8ce 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 >0 );. asse
1c8cf 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e rt( pOp->p2<=p->
1c8d0 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 49 nMem );. pI
1c8d1 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f n2 = &p->aMem[pO
1c8d2 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 52 45 p->p2];. RE
1c8d3 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
1c8d4 2d 3e 70 32 2c 20 70 49 6e 32 29 3b 0a 20 20 20 ->p2, pIn2);.
1c8d5 20 7d 65 6c 73 65 20 69 66 28 20 28 6f 70 50 72 }else if( (opPr
1c8d6 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 49 operty & OPFLG_I
1c8d7 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 N3)!=0 ){.
1c8d8 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e assert( pOp->p3>
1c8d9 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 0 );. asser
1c8da 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e t( pOp->p3<=p->n
1c8db 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 49 6e Mem );. pIn
1c8dc 33 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 3 = &p->aMem[pOp
1c8dd 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 52 45 47 ->p3];. REG
1c8de 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1c8df 3e 70 33 2c 20 70 49 6e 33 29 3b 0a 20 20 20 20 >p3, pIn3);.
1c8e0 7d 0a 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 }.. switch( p
1c8e1 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 0a 2f Op->opcode ){../
1c8e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c8e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c8e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c8e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c8e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
1c8e7 20 57 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 What follows is
1c8e8 20 61 20 6d 61 73 73 69 76 65 20 73 77 69 74 63 a massive switc
1c8e9 68 20 73 74 61 74 65 6d 65 6e 74 20 77 68 65 72 h statement wher
1c8ea 65 20 65 61 63 68 20 63 61 73 65 20 69 6d 70 6c e each case impl
1c8eb 65 6d 65 6e 74 73 20 61 0a 2a 2a 20 73 65 70 61 ements a.** sepa
1c8ec 72 61 74 65 20 69 6e 73 74 72 75 63 74 69 6f 6e rate instruction
1c8ed 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 in the virtual
1c8ee 6d 61 63 68 69 6e 65 2e 20 20 49 66 20 77 65 20 machine. If we
1c8ef 66 6f 6c 6c 6f 77 20 74 68 65 20 75 73 75 61 6c follow the usual
1c8f0 0a 2a 2a 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 .** indentation
1c8f1 63 6f 6e 76 65 6e 74 69 6f 6e 73 2c 20 65 61 63 conventions, eac
1c8f2 68 20 63 61 73 65 20 73 68 6f 75 6c 64 20 62 65 h case should be
1c8f3 20 69 6e 64 65 6e 74 65 64 20 62 79 20 36 20 73 indented by 6 s
1c8f4 70 61 63 65 73 2e 20 20 42 75 74 0a 2a 2a 20 74 paces. But.** t
1c8f5 68 61 74 20 69 73 20 61 20 6c 6f 74 20 6f 66 20 hat is a lot of
1c8f6 77 61 73 74 65 64 20 73 70 61 63 65 20 6f 6e 20 wasted space on
1c8f7 74 68 65 20 6c 65 66 74 20 6d 61 72 67 69 6e 2e the left margin.
1c8f8 20 20 53 6f 20 74 68 65 20 63 6f 64 65 20 77 69 So the code wi
1c8f9 74 68 69 6e 0a 2a 2a 20 74 68 65 20 73 77 69 74 thin.** the swit
1c8fa 63 68 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c ch statement wil
1c8fb 6c 20 62 72 65 61 6b 20 77 69 74 68 20 63 6f 6e l break with con
1c8fc 76 65 6e 74 69 6f 6e 20 61 6e 64 20 62 65 20 66 vention and be f
1c8fd 6c 75 73 68 2d 6c 65 66 74 2e 20 41 6e 6f 74 68 lush-left. Anoth
1c8fe 65 72 0a 2a 2a 20 62 69 67 20 63 6f 6d 6d 65 6e er.** big commen
1c8ff 74 20 28 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 t (similar to th
1c900 69 73 20 6f 6e 65 29 20 77 69 6c 6c 20 6d 61 72 is one) will mar
1c901 6b 20 74 68 65 20 70 6f 69 6e 74 20 69 6e 20 74 k the point in t
1c902 68 65 20 63 6f 64 65 20 77 68 65 72 65 0a 2a 2a he code where.**
1c903 20 77 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 62 we transition b
1c904 61 63 6b 20 74 6f 20 6e 6f 72 6d 61 6c 20 69 6e ack to normal in
1c905 64 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a dentation..**.**
1c906 20 54 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 The formatting
1c907 6f 66 20 65 61 63 68 20 63 61 73 65 20 69 73 20 of each case is
1c908 69 6d 70 6f 72 74 61 6e 74 2e 20 20 54 68 65 20 important. The
1c909 6d 61 6b 65 66 69 6c 65 20 66 6f 72 20 53 51 4c makefile for SQL
1c90a 69 74 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 73 ite.** generates
1c90b 20 74 77 6f 20 43 20 66 69 6c 65 73 20 22 6f 70 two C files "op
1c90c 63 6f 64 65 73 2e 68 22 20 61 6e 64 20 22 6f 70 codes.h" and "op
1c90d 63 6f 64 65 73 2e 63 22 20 62 79 20 73 63 61 6e codes.c" by scan
1c90e 6e 69 6e 67 20 74 68 69 73 0a 2a 2a 20 66 69 6c ning this.** fil
1c90f 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6c 69 e looking for li
1c910 6e 65 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 nes that begin w
1c911 69 74 68 20 22 63 61 73 65 20 4f 50 5f 22 2e 20 ith "case OP_".
1c912 20 54 68 65 20 6f 70 63 6f 64 65 73 2e 68 20 66 The opcodes.h f
1c913 69 6c 65 73 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 iles.** will be
1c914 66 69 6c 6c 65 64 20 77 69 74 68 20 23 64 65 66 filled with #def
1c915 69 6e 65 73 20 74 68 61 74 20 67 69 76 65 20 75 ines that give u
1c916 6e 69 71 75 65 20 69 6e 74 65 67 65 72 20 76 61 nique integer va
1c917 6c 75 65 73 20 74 6f 20 65 61 63 68 0a 2a 2a 20 lues to each.**
1c918 6f 70 63 6f 64 65 20 61 6e 64 20 74 68 65 20 6f opcode and the o
1c919 70 63 6f 64 65 73 2e 63 20 66 69 6c 65 20 69 73 pcodes.c file is
1c91a 20 66 69 6c 6c 65 64 20 77 69 74 68 20 61 6e 20 filled with an
1c91b 61 72 72 61 79 20 6f 66 20 73 74 72 69 6e 67 73 array of strings
1c91c 20 77 68 65 72 65 0a 2a 2a 20 65 61 63 68 20 73 where.** each s
1c91d 74 72 69 6e 67 20 69 73 20 74 68 65 20 73 79 6d tring is the sym
1c91e 62 6f 6c 69 63 20 6e 61 6d 65 20 66 6f 72 20 74 bolic name for t
1c91f 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
1c920 20 6f 70 63 6f 64 65 2e 20 20 49 66 20 74 68 65 opcode. If the
1c921 0a 2a 2a 20 63 61 73 65 20 73 74 61 74 65 6d 65 .** case stateme
1c922 6e 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20 62 nt is followed b
1c923 79 20 61 20 63 6f 6d 6d 65 6e 74 20 6f 66 20 74 y a comment of t
1c924 68 65 20 66 6f 72 6d 20 22 2f 23 20 73 61 6d 65 he form "/# same
1c925 20 61 73 20 2e 2e 2e 20 23 2f 22 0a 2a 2a 20 74 as ... #/".** t
1c926 68 61 74 20 63 6f 6d 6d 65 6e 74 20 69 73 20 75 hat comment is u
1c927 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 sed to determine
1c928 20 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 the particular
1c929 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70 63 value of the opc
1c92a 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 ode..**.** Other
1c92b 20 6b 65 79 77 6f 72 64 73 20 69 6e 20 74 68 65 keywords in the
1c92c 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20 66 6f comment that fo
1c92d 6c 6c 6f 77 73 20 65 61 63 68 20 63 61 73 65 20 llows each case
1c92e 61 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 are used to.** c
1c92f 6f 6e 73 74 72 75 63 74 20 74 68 65 20 4f 50 46 onstruct the OPF
1c930 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 76 LG_INITIALIZER v
1c931 61 6c 75 65 20 74 68 61 74 20 69 6e 69 74 69 61 alue that initia
1c932 6c 69 7a 65 73 20 6f 70 63 6f 64 65 50 72 6f 70 lizes opcodeProp
1c933 65 72 74 79 5b 5d 2e 0a 2a 2a 20 4b 65 79 77 6f erty[]..** Keywo
1c934 72 64 73 20 69 6e 63 6c 75 64 65 3a 20 69 6e 31 rds include: in1
1c935 2c 20 69 6e 32 2c 20 69 6e 33 2c 20 6f 75 74 32 , in2, in3, out2
1c936 5f 70 72 65 72 65 6c 65 61 73 65 2c 20 6f 75 74 _prerelease, out
1c937 32 2c 20 6f 75 74 33 2e 20 20 53 65 65 0a 2a 2a 2, out3. See.**
1c938 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 the mkopcodeh.a
1c939 77 6b 20 73 63 72 69 70 74 20 66 6f 72 20 61 64 wk script for ad
1c93a 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
1c93b 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 6f 63 75 tion..**.** Docu
1c93c 6d 65 6e 74 61 74 69 6f 6e 20 61 62 6f 75 74 20 mentation about
1c93d 56 44 42 45 20 6f 70 63 6f 64 65 73 20 69 73 20 VDBE opcodes is
1c93e 67 65 6e 65 72 61 74 65 64 20 62 79 20 73 63 61 generated by sca
1c93f 6e 6e 69 6e 67 20 74 68 69 73 20 66 69 6c 65 0a nning this file.
1c940 2a 2a 20 66 6f 72 20 6c 69 6e 65 73 20 6f 66 20 ** for lines of
1c941 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 22 4f 70 that contain "Op
1c942 63 6f 64 65 3a 22 2e 20 20 54 68 61 74 20 6c 69 code:". That li
1c943 6e 65 20 61 6e 64 20 61 6c 6c 20 73 75 62 73 65 ne and all subse
1c944 71 75 65 6e 74 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 quent.** comment
1c945 20 6c 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 lines are used
1c946 69 6e 20 74 68 65 20 67 65 6e 65 72 61 74 69 6f in the generatio
1c947 6e 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e n of the opcode.
1c948 68 74 6d 6c 20 64 6f 63 75 6d 65 6e 74 61 74 69 html documentati
1c949 6f 6e 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2a 0a 2a on.** file..**.*
1c94a 2a 20 53 55 4d 4d 41 52 59 3a 0a 2a 2a 0a 2a 2a * SUMMARY:.**.**
1c94b 20 20 20 20 20 46 6f 72 6d 61 74 74 69 6e 67 20 Formatting
1c94c 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 is important to
1c94d 73 63 72 69 70 74 73 20 74 68 61 74 20 73 63 61 scripts that sca
1c94e 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 n this file..**
1c94f 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 76 69 61 Do not devia
1c950 74 65 20 66 72 6f 6d 20 74 68 65 20 66 6f 72 6d te from the form
1c951 61 74 74 69 6e 67 20 73 74 79 6c 65 20 63 75 72 atting style cur
1c952 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a rently in use..*
1c953 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
1c954 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c955 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c956 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c957 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1c958 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f ../* Opcode: Go
1c959 74 6f 20 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a to * P2 * * *.**
1c95a 0a 2a 2a 20 41 6e 20 75 6e 63 6f 6e 64 69 74 69 .** An unconditi
1c95b 6f 6e 61 6c 20 6a 75 6d 70 20 74 6f 20 61 64 64 onal jump to add
1c95c 72 65 73 73 20 50 32 2e 0a 2a 2a 20 54 68 65 20 ress P2..** The
1c95d 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e next instruction
1c95e 20 65 78 65 63 75 74 65 64 20 77 69 6c 6c 20 62 executed will b
1c95f 65 20 0a 2a 2a 20 74 68 65 20 6f 6e 65 20 61 74 e .** the one at
1c960 20 69 6e 64 65 78 20 50 32 20 66 72 6f 6d 20 74 index P2 from t
1c961 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 0a he beginning of.
1c962 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a ** the program..
1c963 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 74 6f 3a */.case OP_Goto:
1c964 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f { /
1c965 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 43 48 45 43 * jump */. CHEC
1c966 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b K_FOR_INTERRUPT;
1c967 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 . pc = pOp->p2
1c968 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a - 1;. break;.}.
1c969 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f 73 ./* Opcode: Gos
1c96a 75 62 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a ub P1 P2 * * *.*
1c96b 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 63 *.** Write the c
1c96c 75 72 72 65 6e 74 20 61 64 64 72 65 73 73 20 6f urrent address o
1c96d 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 31 0a nto register P1.
1c96e 2a 2a 20 61 6e 64 20 74 68 65 6e 20 6a 75 6d 70 ** and then jump
1c96f 20 74 6f 20 61 64 64 72 65 73 73 20 50 32 2e 0a to address P2..
1c970 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 73 75 62 */.case OP_Gosub
1c971 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f : { /
1c972 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 61 73 73 65 * jump */. asse
1c973 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b rt( pOp->p1>0 );
1c974 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1c975 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 p1<=p->nMem );.
1c976 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d pIn1 = &p->aMem
1c977 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 [pOp->p1];. ass
1c978 65 72 74 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 ert( (pIn1->flag
1c979 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 s & MEM_Dyn)==0
1c97a 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 );. pIn1->flags
1c97b 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 49 = MEM_Int;. pI
1c97c 6e 31 2d 3e 75 2e 69 20 3d 20 70 63 3b 0a 20 20 n1->u.i = pc;.
1c97d 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1c97e 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 Op->p1, pIn1);.
1c97f 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1c980 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 1;. break;.}../
1c981 2a 20 4f 70 63 6f 64 65 3a 20 20 52 65 74 75 72 * Opcode: Retur
1c982 6e 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a n P1 * * * *.**.
1c983 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 6e ** Jump to the n
1c984 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ext instruction
1c985 61 66 74 65 72 20 74 68 65 20 61 64 64 72 65 73 after the addres
1c986 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 s in register P1
1c987 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 74 ..*/.case OP_Ret
1c988 75 72 6e 3a 20 7b 20 20 20 20 20 20 20 20 20 20 urn: {
1c989 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 /* in1 */. ass
1c98a 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 ert( pIn1->flags
1c98b 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 & MEM_Int );.
1c98c 70 63 20 3d 20 28 69 6e 74 29 70 49 6e 31 2d 3e pc = (int)pIn1->
1c98d 75 2e 69 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a u.i;. break;.}.
1c98e 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 59 69 65 ./* Opcode: Yie
1c98f 6c 64 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a ld P1 * * * *.**
1c990 0a 2a 2a 20 53 77 61 70 20 74 68 65 20 70 72 6f .** Swap the pro
1c991 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 77 69 74 gram counter wit
1c992 68 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 h the value in r
1c993 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f 0a 63 egister P1..*/.c
1c994 61 73 65 20 4f 50 5f 59 69 65 6c 64 3a 20 7b 20 ase OP_Yield: {
1c995 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e /* in
1c996 31 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 1 */.#if 0 /* l
1c997 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1c998 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 61 20 2a oved into u.aa *
1c999 2f 0a 20 20 69 6e 74 20 70 63 44 65 73 74 3b 0a /. int pcDest;.
1c99a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1c99b 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1c99c 69 6e 74 6f 20 75 2e 61 61 20 2a 2f 0a 20 20 61 into u.aa */. a
1c99d 73 73 65 72 74 28 20 28 70 49 6e 31 2d 3e 66 6c ssert( (pIn1->fl
1c99e 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d ags & MEM_Dyn)==
1c99f 30 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 0 );. pIn1->fla
1c9a0 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 gs = MEM_Int;.
1c9a1 75 2e 61 61 2e 70 63 44 65 73 74 20 3d 20 28 69 u.aa.pcDest = (i
1c9a2 6e 74 29 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 nt)pIn1->u.i;.
1c9a3 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 63 3b 0a pIn1->u.i = pc;.
1c9a4 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1c9a5 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b (pOp->p1, pIn1);
1c9a6 0a 20 20 70 63 20 3d 20 75 2e 61 61 2e 70 63 44 . pc = u.aa.pcD
1c9a7 65 73 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a est;. break;.}.
1c9a8 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 48 61 6c ./* Opcode: Hal
1c9a9 74 49 66 4e 75 6c 6c 20 20 50 31 20 50 32 20 50 tIfNull P1 P2 P
1c9aa 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 3 P4 *.**.** Che
1c9ab 63 6b 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 ck the value in
1c9ac 72 65 67 69 73 74 65 72 20 50 33 2e 20 20 49 66 register P3. If
1c9ad 20 69 73 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e is is NULL then
1c9ae 20 48 61 6c 74 20 75 73 69 6e 67 0a 2a 2a 20 70 Halt using.** p
1c9af 61 72 61 6d 65 74 65 72 20 50 31 2c 20 50 32 2c arameter P1, P2,
1c9b0 20 61 6e 64 20 50 34 20 61 73 20 69 66 20 74 68 and P4 as if th
1c9b1 69 73 20 77 65 72 65 20 61 20 48 61 6c 74 20 69 is were a Halt i
1c9b2 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 49 66 20 nstruction. If
1c9b3 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 the.** value in
1c9b4 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20 6e register P3 is n
1c9b5 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 ot NULL, then th
1c9b6 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
1c9b7 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f no-op..*/.case O
1c9b8 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 3a 20 7b 20 P_HaltIfNull: {
1c9b9 20 20 20 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 20 /* in3 */.
1c9ba 20 69 66 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 if( (pIn3->flag
1c9bb 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 s & MEM_Null)==0
1c9bc 20 29 20 62 72 65 61 6b 3b 0a 20 20 2f 2a 20 46 ) break;. /* F
1c9bd 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f all through into
1c9be 20 4f 50 5f 48 61 6c 74 20 2a 2f 0a 7d 0a 0a 2f OP_Halt */.}../
1c9bf 2a 20 4f 70 63 6f 64 65 3a 20 20 48 61 6c 74 20 * Opcode: Halt
1c9c0 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a P1 P2 * P4 *.**.
1c9c1 2a 2a 20 45 78 69 74 20 69 6d 6d 65 64 69 61 74 ** Exit immediat
1c9c2 65 6c 79 2e 20 20 41 6c 6c 20 6f 70 65 6e 20 63 ely. All open c
1c9c3 75 72 73 6f 72 73 2c 20 65 74 63 20 61 72 65 20 ursors, etc are
1c9c4 63 6c 6f 73 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 closed.** automa
1c9c5 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 tically..**.** P
1c9c6 31 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 1 is the result
1c9c7 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20 62 79 code returned by
1c9c8 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2c sqlite3_exec(),
1c9c9 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 sqlite3_reset()
1c9ca 2c 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f ,.** or sqlite3_
1c9cb 66 69 6e 61 6c 69 7a 65 28 29 2e 20 20 46 6f 72 finalize(). For
1c9cc 20 61 20 6e 6f 72 6d 61 6c 20 68 61 6c 74 2c 20 a normal halt,
1c9cd 74 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 53 this should be S
1c9ce 51 4c 49 54 45 5f 4f 4b 20 28 30 29 2e 0a 2a 2a QLITE_OK (0)..**
1c9cf 20 46 6f 72 20 65 72 72 6f 72 73 2c 20 69 74 20 For errors, it
1c9d0 63 61 6e 20 62 65 20 73 6f 6d 65 20 6f 74 68 65 can be some othe
1c9d1 72 20 76 61 6c 75 65 2e 20 20 49 66 20 50 31 21 r value. If P1!
1c9d2 3d 30 20 74 68 65 6e 20 50 32 20 77 69 6c 6c 20 =0 then P2 will
1c9d3 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 65 determine.** whe
1c9d4 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20 72 ther or not to r
1c9d5 6f 6c 6c 62 61 63 6b 20 74 68 65 20 63 75 72 72 ollback the curr
1c9d6 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ent transaction.
1c9d7 20 20 44 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61 63 Do not rollbac
1c9d8 6b 0a 2a 2a 20 69 66 20 50 32 3d 3d 4f 45 5f 46 k.** if P2==OE_F
1c9d9 61 69 6c 2e 20 44 6f 20 74 68 65 20 72 6f 6c 6c ail. Do the roll
1c9da 62 61 63 6b 20 69 66 20 50 32 3d 3d 4f 45 5f 52 back if P2==OE_R
1c9db 6f 6c 6c 62 61 63 6b 2e 20 20 49 66 20 50 32 3d ollback. If P2=
1c9dc 3d 4f 45 5f 41 62 6f 72 74 2c 0a 2a 2a 20 74 68 =OE_Abort,.** th
1c9dd 65 6e 20 62 61 63 6b 20 6f 75 74 20 61 6c 6c 20 en back out all
1c9de 63 68 61 6e 67 65 73 20 74 68 61 74 20 68 61 76 changes that hav
1c9df 65 20 6f 63 63 75 72 72 65 64 20 64 75 72 69 6e e occurred durin
1c9e0 67 20 74 68 69 73 20 65 78 65 63 75 74 69 6f 6e g this execution
1c9e1 20 6f 66 20 74 68 65 0a 2a 2a 20 56 44 42 45 2c of the.** VDBE,
1c9e2 20 62 75 74 20 64 6f 20 6e 6f 74 20 72 6f 6c 6c but do not roll
1c9e3 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 back the transac
1c9e4 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 tion. .**.** If
1c9e5 50 34 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 20 74 P4 is not null t
1c9e6 68 65 6e 20 69 74 20 69 73 20 61 6e 20 65 72 72 hen it is an err
1c9e7 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e or message strin
1c9e8 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 g..**.** There i
1c9e9 73 20 61 6e 20 69 6d 70 6c 69 65 64 20 22 48 61 s an implied "Ha
1c9ea 6c 74 20 30 20 30 20 30 22 20 69 6e 73 74 72 75 lt 0 0 0" instru
1c9eb 63 74 69 6f 6e 20 69 6e 73 65 72 74 65 64 20 61 ction inserted a
1c9ec 74 20 74 68 65 20 76 65 72 79 20 65 6e 64 20 6f t the very end o
1c9ed 66 0a 2a 2a 20 65 76 65 72 79 20 70 72 6f 67 72 f.** every progr
1c9ee 61 6d 2e 20 20 53 6f 20 61 20 6a 75 6d 70 20 70 am. So a jump p
1c9ef 61 73 74 20 74 68 65 20 6c 61 73 74 20 69 6e 73 ast the last ins
1c9f0 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 truction of the
1c9f1 70 72 6f 67 72 61 6d 0a 2a 2a 20 69 73 20 74 68 program.** is th
1c9f2 65 20 73 61 6d 65 20 61 73 20 65 78 65 63 75 74 e same as execut
1c9f3 69 6e 67 20 48 61 6c 74 2e 0a 2a 2f 0a 63 61 73 ing Halt..*/.cas
1c9f4 65 20 4f 50 5f 48 61 6c 74 3a 20 7b 0a 20 20 69 e OP_Halt: {. i
1c9f5 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 53 51 4c 49 f( pOp->p1==SQLI
1c9f6 54 45 5f 4f 4b 20 26 26 20 70 2d 3e 70 46 72 61 TE_OK && p->pFra
1c9f7 6d 65 20 29 7b 0a 20 20 20 20 2f 2a 20 48 61 6c me ){. /* Hal
1c9f8 74 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 t the sub-progra
1c9f9 6d 2e 20 52 65 74 75 72 6e 20 63 6f 6e 74 72 6f m. Return contro
1c9fa 6c 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 l to the parent
1c9fb 66 72 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 56 64 frame. */. Vd
1c9fc 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 20 beFrame *pFrame
1c9fd 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 20 = p->pFrame;.
1c9fe 20 70 2d 3e 70 46 72 61 6d 65 20 3d 20 70 46 72 p->pFrame = pFr
1c9ff 61 6d 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 ame->pParent;.
1ca00 20 20 70 2d 3e 6e 46 72 61 6d 65 2d 2d 3b 0a 20 p->nFrame--;.
1ca01 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 sqlite3VdbeSe
1ca02 74 43 68 61 6e 67 65 73 28 64 62 2c 20 70 2d 3e tChanges(db, p->
1ca03 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 70 63 nChange);. pc
1ca04 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 = sqlite3VdbeFr
1ca05 61 6d 65 52 65 73 74 6f 72 65 28 70 46 72 61 6d ameRestore(pFram
1ca06 65 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d e);. if( pOp-
1ca07 3e 70 32 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 >p2==OE_Ignore )
1ca08 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 73 74 72 {. /* Instr
1ca09 75 63 74 69 6f 6e 20 70 63 20 69 73 20 74 68 65 uction pc is the
1ca0a 20 4f 50 5f 50 72 6f 67 72 61 6d 20 74 68 61 74 OP_Program that
1ca0b 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 73 75 62 invoked the sub
1ca0c 2d 70 72 6f 67 72 61 6d 20 0a 20 20 20 20 20 20 -program .
1ca0d 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69 ** currently bei
1ca0e 6e 67 20 68 61 6c 74 65 64 2e 20 49 66 20 74 68 ng halted. If th
1ca0f 65 20 70 32 20 69 6e 73 74 72 75 63 74 69 6f 6e e p2 instruction
1ca10 20 6f 66 20 74 68 69 73 20 4f 50 5f 48 61 6c 74 of this OP_Halt
1ca11 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 72 75 . ** instru
1ca12 63 74 69 6f 6e 20 69 73 20 73 65 74 20 74 6f 20 ction is set to
1ca13 4f 45 5f 49 67 6e 6f 72 65 2c 20 74 68 65 6e 20 OE_Ignore, then
1ca14 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 the sub-program
1ca15 69 73 20 74 68 72 6f 77 69 6e 67 0a 20 20 20 20 is throwing.
1ca16 20 20 2a 2a 20 61 6e 20 49 47 4e 4f 52 45 20 65 ** an IGNORE e
1ca17 78 63 65 70 74 69 6f 6e 2e 20 49 6e 20 74 68 69 xception. In thi
1ca18 73 20 63 61 73 65 20 6a 75 6d 70 20 74 6f 20 74 s case jump to t
1ca19 68 65 20 61 64 64 72 65 73 73 20 73 70 65 63 69 he address speci
1ca1a 66 69 65 64 0a 20 20 20 20 20 20 2a 2a 20 61 73 fied. ** as
1ca1b 20 74 68 65 20 70 32 20 6f 66 20 74 68 65 20 63 the p2 of the c
1ca1c 61 6c 6c 69 6e 67 20 4f 50 5f 50 72 6f 67 72 61 alling OP_Progra
1ca1d 6d 2e 20 20 2a 2f 0a 20 20 20 20 20 20 70 63 20 m. */. pc
1ca1e 3d 20 70 2d 3e 61 4f 70 5b 70 63 5d 2e 70 32 2d = p->aOp[pc].p2-
1ca1f 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62 72 65 1;. }. bre
1ca20 61 6b 3b 0a 20 20 7d 0a 0a 20 20 70 2d 3e 72 63 ak;. }.. p->rc
1ca21 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 70 2d = pOp->p1;. p-
1ca22 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 28 >errorAction = (
1ca23 75 38 29 70 4f 70 2d 3e 70 32 3b 0a 20 20 70 2d u8)pOp->p2;. p-
1ca24 3e 70 63 20 3d 20 70 63 3b 0a 20 20 69 66 28 20 >pc = pc;. if(
1ca25 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 pOp->p4.z ){.
1ca26 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e sqlite3SetStrin
1ca27 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 g(&p->zErrMsg, d
1ca28 62 2c 20 22 25 73 22 2c 20 70 4f 70 2d 3e 70 34 b, "%s", pOp->p4
1ca29 2e 7a 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 .z);. }. rc =
1ca2a 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 sqlite3VdbeHalt(
1ca2b 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 p);. assert( rc
1ca2c 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c ==SQLITE_BUSY ||
1ca2d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc==SQLITE_OK |
1ca2e 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 | rc==SQLITE_ERR
1ca2f 4f 52 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d OR );. if( rc==
1ca30 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 SQLITE_BUSY ){.
1ca31 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 p->rc = rc =
1ca32 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d SQLITE_BUSY;. }
1ca33 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
1ca34 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1ca35 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 || p->rc==SQLITE
1ca36 5f 43 4f 4e 53 54 52 41 49 4e 54 20 29 3b 0a 20 _CONSTRAINT );.
1ca37 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 assert( rc==S
1ca38 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e QLITE_OK || db->
1ca39 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3e 30 20 nDeferredCons>0
1ca3a 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 2d 3e 72 );. rc = p->r
1ca3b 63 20 3f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c ? SQLITE_ERROR
1ca3c 20 3a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a : SQLITE_DONE;.
1ca3d 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f }. goto vdbe_
1ca3e 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 return;.}../* Op
1ca3f 63 6f 64 65 3a 20 49 6e 74 65 67 65 72 20 50 31 code: Integer P1
1ca40 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1ca41 54 68 65 20 33 32 2d 62 69 74 20 69 6e 74 65 67 The 32-bit integ
1ca42 65 72 20 76 61 6c 75 65 20 50 31 20 69 73 20 77 er value P1 is w
1ca43 72 69 74 74 65 6e 20 69 6e 74 6f 20 72 65 67 69 ritten into regi
1ca44 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 ster P2..*/.case
1ca45 20 4f 50 5f 49 6e 74 65 67 65 72 3a 20 7b 20 20 OP_Integer: {
1ca46 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 /* out2-p
1ca47 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 rerelease */. p
1ca48 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Out->flags = MEM
1ca49 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e _Int;. pOut->u.
1ca4a 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 62 i = pOp->p1;. b
1ca4b 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1ca4c 64 65 3a 20 49 6e 74 36 34 20 2a 20 50 32 20 2a de: Int64 * P2 *
1ca4d 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 P4 *.**.** P4 i
1ca4e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
1ca4f 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 64-bit integer
1ca50 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 69 74 65 20 value..** Write
1ca51 74 68 61 74 20 76 61 6c 75 65 20 69 6e 74 6f 20 that value into
1ca52 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a register P2..*/.
1ca53 63 61 73 65 20 4f 50 5f 49 6e 74 36 34 3a 20 7b case OP_Int64: {
1ca54 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 /* ou
1ca55 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f t2-prerelease */
1ca56 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1ca57 70 34 2e 70 49 36 34 21 3d 30 20 29 3b 0a 20 20 p4.pI64!=0 );.
1ca58 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pOut->flags = ME
1ca59 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d 3e 75 M_Int;. pOut->u
1ca5a 2e 69 20 3d 20 2a 70 4f 70 2d 3e 70 34 2e 70 49 .i = *pOp->p4.pI
1ca5b 36 34 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 64;. break;.}..
1ca5c 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 6c 20 /* Opcode: Real
1ca5d 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a * P2 * P4 *.**.*
1ca5e 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 * P4 is a pointe
1ca5f 72 20 74 6f 20 61 20 36 34 2d 62 69 74 20 66 6c r to a 64-bit fl
1ca60 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c oating point val
1ca61 75 65 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 61 ue..** Write tha
1ca62 74 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 t value into reg
1ca63 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 ister P2..*/.cas
1ca64 65 20 4f 50 5f 52 65 61 6c 3a 20 7b 20 20 20 20 e OP_Real: {
1ca65 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
1ca66 61 73 20 54 4b 5f 46 4c 4f 41 54 2c 20 6f 75 74 as TK_FLOAT, out
1ca67 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 2-prerelease */.
1ca68 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 pOut->flags =
1ca69 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 61 73 73 65 MEM_Real;. asse
1ca6a 72 74 28 20 21 73 71 6c 69 74 65 33 49 73 4e 61 rt( !sqlite3IsNa
1ca6b 4e 28 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c N(*pOp->p4.pReal
1ca6c 29 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 72 20 3d ) );. pOut->r =
1ca6d 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 3b *pOp->p4.pReal;
1ca6e 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1ca6f 4f 70 63 6f 64 65 3a 20 53 74 72 69 6e 67 38 20 Opcode: String8
1ca70 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a * P2 * P4 *.**.*
1ca71 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 * P4 points to a
1ca72 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 20 nul terminated
1ca73 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 20 54 68 UTF-8 string. Th
1ca74 69 73 20 6f 70 63 6f 64 65 20 69 73 20 74 72 61 is opcode is tra
1ca75 6e 73 66 6f 72 6d 65 64 20 0a 2a 2a 20 69 6e 74 nsformed .** int
1ca76 6f 20 61 6e 20 4f 50 5f 53 74 72 69 6e 67 20 62 o an OP_String b
1ca77 65 66 6f 72 65 20 69 74 20 69 73 20 65 78 65 63 efore it is exec
1ca78 75 74 65 64 20 66 6f 72 20 74 68 65 20 66 69 72 uted for the fir
1ca79 73 74 20 74 69 6d 65 2e 0a 2a 2f 0a 63 61 73 65 st time..*/.case
1ca7a 20 4f 50 5f 53 74 72 69 6e 67 38 3a 20 7b 20 20 OP_String8: {
1ca7b 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1ca7c 73 20 54 4b 5f 53 54 52 49 4e 47 2c 20 6f 75 74 s TK_STRING, out
1ca7d 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 2-prerelease */.
1ca7e 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1ca7f 34 2e 7a 21 3d 30 20 29 3b 0a 20 20 70 4f 70 2d 4.z!=0 );. pOp-
1ca80 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 53 74 72 >opcode = OP_Str
1ca81 69 6e 67 3b 0a 20 20 70 4f 70 2d 3e 70 31 20 3d ing;. pOp->p1 =
1ca82 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
1ca83 28 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 0a 23 69 (pOp->p4.z);..#i
1ca84 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1ca85 54 5f 55 54 46 31 36 0a 20 20 69 66 28 20 65 6e T_UTF16. if( en
1ca86 63 6f 64 69 6e 67 21 3d 53 51 4c 49 54 45 5f 55 coding!=SQLITE_U
1ca87 54 46 38 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 TF8 ){. rc =
1ca88 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1ca89 74 53 74 72 28 70 4f 75 74 2c 20 70 4f 70 2d 3e tStr(pOut, pOp->
1ca8a 70 34 2e 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 p4.z, -1, SQLITE
1ca8b 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 _UTF8, SQLITE_ST
1ca8c 41 54 49 43 29 3b 0a 20 20 20 20 69 66 28 20 72 ATIC);. if( r
1ca8d 63 3d 3d 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 c==SQLITE_TOOBIG
1ca8e 20 29 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b ) goto too_big;
1ca8f 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f . if( SQLITE_
1ca90 4f 4b 21 3d 73 71 6c 69 74 65 33 56 64 62 65 43 OK!=sqlite3VdbeC
1ca91 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4f hangeEncoding(pO
1ca92 75 74 2c 20 65 6e 63 6f 64 69 6e 67 29 20 29 20 ut, encoding) )
1ca93 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 goto no_mem;.
1ca94 20 61 73 73 65 72 74 28 20 70 4f 75 74 2d 3e 7a assert( pOut->z
1ca95 4d 61 6c 6c 6f 63 3d 3d 70 4f 75 74 2d 3e 7a 20 Malloc==pOut->z
1ca96 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
1ca97 4f 75 74 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Out->flags & MEM
1ca98 5f 44 79 6e 20 29 3b 0a 20 20 20 20 70 4f 75 74 _Dyn );. pOut
1ca99 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 ->zMalloc = 0;.
1ca9a 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c pOut->flags |
1ca9b 3d 20 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20 20 = MEM_Static;.
1ca9c 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26 3d pOut->flags &=
1ca9d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 20 20 20 20 69 ~MEM_Dyn;. i
1ca9e 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d f( pOp->p4type==
1ca9f 50 34 5f 44 59 4e 41 4d 49 43 20 29 7b 0a 20 20 P4_DYNAMIC ){.
1caa0 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1caa1 65 28 64 62 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 e(db, pOp->p4.z)
1caa2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 70 2d ;. }. pOp-
1caa3 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 44 59 4e >p4type = P4_DYN
1caa4 41 4d 49 43 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 AMIC;. pOp->p
1caa5 34 2e 7a 20 3d 20 70 4f 75 74 2d 3e 7a 3b 0a 20 4.z = pOut->z;.
1caa6 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70 4f 75 pOp->p1 = pOu
1caa7 74 2d 3e 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 t->n;. }.#endif
1caa8 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 3e 64 . if( pOp->p1>d
1caa9 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
1caaa 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 _LIMIT_LENGTH] )
1caab 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 {. goto too_b
1caac 69 67 3b 0a 20 20 7d 0a 20 20 2f 2a 20 46 61 6c ig;. }. /* Fal
1caad 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 l through to the
1caae 20 6e 65 78 74 20 63 61 73 65 2c 20 4f 50 5f 53 next case, OP_S
1caaf 74 72 69 6e 67 20 2a 2f 0a 7d 0a 20 20 0a 2f 2a tring */.}. ./*
1cab0 20 4f 70 63 6f 64 65 3a 20 53 74 72 69 6e 67 20 Opcode: String
1cab1 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a P1 P2 * P4 *.**.
1cab2 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 76 61 ** The string va
1cab3 6c 75 65 20 50 34 20 6f 66 20 6c 65 6e 67 74 68 lue P4 of length
1cab4 20 50 31 20 28 62 79 74 65 73 29 20 69 73 20 73 P1 (bytes) is s
1cab5 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 tored in registe
1cab6 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 r P2..*/.case OP
1cab7 5f 53 74 72 69 6e 67 3a 20 7b 20 20 20 20 20 20 _String: {
1cab8 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 /* out2-prer
1cab9 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 elease */. asse
1caba 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 rt( pOp->p4.z!=0
1cabb 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 );. pOut->flag
1cabc 73 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f s = MEM_Str|MEM_
1cabd 53 74 61 74 69 63 7c 4d 45 4d 5f 54 65 72 6d 3b Static|MEM_Term;
1cabe 0a 20 20 70 4f 75 74 2d 3e 7a 20 3d 20 70 4f 70 . pOut->z = pOp
1cabf 2d 3e 70 34 2e 7a 3b 0a 20 20 70 4f 75 74 2d 3e ->p4.z;. pOut->
1cac0 6e 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 70 n = pOp->p1;. p
1cac1 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 Out->enc = encod
1cac2 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 ing;. UPDATE_MA
1cac3 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 X_BLOBSIZE(pOut)
1cac4 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1cac5 20 4f 70 63 6f 64 65 3a 20 4e 75 6c 6c 20 2a 20 Opcode: Null *
1cac6 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 P2 * * *.**.** W
1cac7 72 69 74 65 20 61 20 4e 55 4c 4c 20 69 6e 74 6f rite a NULL into
1cac8 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f register P2..*/
1cac9 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 3a 20 7b .case OP_Null: {
1caca 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 /* ou
1cacb 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f t2-prerelease */
1cacc 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a . break;.}.../*
1cacd 20 4f 70 63 6f 64 65 3a 20 42 6c 6f 62 20 50 31 Opcode: Blob P1
1cace 20 50 32 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 P2 * P4.**.** P
1cacf 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 62 6c 4 points to a bl
1cad0 6f 62 20 6f 66 20 64 61 74 61 20 50 31 20 62 79 ob of data P1 by
1cad1 74 65 73 20 6c 6f 6e 67 2e 20 20 53 74 6f 72 65 tes long. Store
1cad2 20 74 68 69 73 0a 2a 2a 20 62 6c 6f 62 20 69 6e this.** blob in
1cad3 20 72 65 67 69 73 74 65 72 20 50 32 2e 20 54 68 register P2. Th
1cad4 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 is instruction i
1cad5 73 20 6e 6f 74 20 63 6f 64 65 64 20 64 69 72 65 s not coded dire
1cad6 63 74 6c 79 0a 2a 2a 20 62 79 20 74 68 65 20 63 ctly.** by the c
1cad7 6f 6d 70 69 6c 65 72 2e 20 49 6e 73 74 65 61 64 ompiler. Instead
1cad8 2c 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 6c , the compiler l
1cad9 61 79 65 72 20 73 70 65 63 69 66 69 65 73 0a 2a ayer specifies.*
1cada 2a 20 61 6e 20 4f 50 5f 48 65 78 42 6c 6f 62 20 * an OP_HexBlob
1cadb 6f 70 63 6f 64 65 2c 20 77 69 74 68 20 74 68 65 opcode, with the
1cadc 20 68 65 78 20 73 74 72 69 6e 67 20 72 65 70 72 hex string repr
1cadd 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 0a 2a 2a esentation of.**
1cade 20 74 68 65 20 62 6c 6f 62 20 61 73 20 50 34 2e the blob as P4.
1cadf 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 This opcode is
1cae0 74 72 61 6e 73 66 6f 72 6d 65 64 20 74 6f 20 61 transformed to a
1cae1 6e 20 4f 50 5f 42 6c 6f 62 0a 2a 2a 20 74 68 65 n OP_Blob.** the
1cae2 20 66 69 72 73 74 20 74 69 6d 65 20 69 74 20 69 first time it i
1cae3 73 20 65 78 65 63 75 74 65 64 2e 0a 2a 2f 0a 63 s executed..*/.c
1cae4 61 73 65 20 4f 50 5f 42 6c 6f 62 3a 20 7b 20 20 ase OP_Blob: {
1cae5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1cae6 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 out2-prerelease
1cae7 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f */. assert( pO
1cae8 70 2d 3e 70 31 20 3c 3d 20 53 51 4c 49 54 45 5f p->p1 <= SQLITE_
1cae9 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20 MAX_LENGTH );.
1caea 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1caeb 74 53 74 72 28 70 4f 75 74 2c 20 70 4f 70 2d 3e tStr(pOut, pOp->
1caec 70 34 2e 7a 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 p4.z, pOp->p1, 0
1caed 2c 20 30 29 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e , 0);. pOut->en
1caee 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 c = encoding;.
1caef 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 UPDATE_MAX_BLOBS
1caf0 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 IZE(pOut);. bre
1caf1 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1caf2 3a 20 56 61 72 69 61 62 6c 65 20 50 31 20 50 32 : Variable P1 P2
1caf3 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 P3 P4 *.**.** T
1caf4 72 61 6e 73 66 65 72 20 74 68 65 20 76 61 6c 75 ransfer the valu
1caf5 65 73 20 6f 66 20 62 6f 75 6e 64 20 70 61 72 61 es of bound para
1caf6 6d 65 74 65 72 73 20 50 31 2e 2e 50 31 2b 50 33 meters P1..P1+P3
1caf7 2d 31 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 -1 into register
1caf8 73 0a 2a 2a 20 50 32 2e 2e 50 32 2b 50 33 2d 31 s.** P2..P2+P3-1
1caf9 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 ..**.** If the p
1cafa 61 72 61 6d 65 74 65 72 20 69 73 20 6e 61 6d 65 arameter is name
1cafb 64 2c 20 74 68 65 6e 20 69 74 73 20 6e 61 6d 65 d, then its name
1cafc 20 61 70 70 65 61 72 73 20 69 6e 20 50 34 20 61 appears in P4 a
1cafd 6e 64 20 50 33 3d 3d 31 2e 0a 2a 2a 20 54 68 65 nd P3==1..** The
1cafe 20 50 34 20 76 61 6c 75 65 20 69 73 20 75 73 65 P4 value is use
1caff 64 20 62 79 20 73 71 6c 69 74 65 33 5f 62 69 6e d by sqlite3_bin
1cb00 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 d_parameter_name
1cb01 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 ()..*/.case OP_V
1cb02 61 72 69 61 62 6c 65 3a 20 7b 0a 23 69 66 20 30 ariable: {.#if 0
1cb03 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1cb04 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1cb05 75 2e 61 62 20 2a 2f 0a 20 20 69 6e 74 20 70 31 u.ab */. int p1
1cb06 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 ; /* Va
1cb07 72 69 61 62 6c 65 20 74 6f 20 63 6f 70 79 20 66 riable to copy f
1cb08 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 70 32 3b rom */. int p2;
1cb09 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 /* Reg
1cb0a 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 74 6f ister to copy to
1cb0b 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 */. int n;
1cb0c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1cb0d 20 6f 66 20 76 61 6c 75 65 73 20 6c 65 66 74 20 of values left
1cb0e 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 4d 65 6d to copy */. Mem
1cb0f 20 2a 70 56 61 72 3b 20 20 20 20 20 20 20 2f 2a *pVar; /*
1cb10 20 56 61 6c 75 65 20 62 65 69 6e 67 20 74 72 61 Value being tra
1cb11 6e 73 66 65 72 72 65 64 20 2a 2f 0a 23 65 6e 64 nsferred */.#end
1cb12 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1cb13 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1cb14 20 75 2e 61 62 20 2a 2f 0a 0a 20 20 75 2e 61 62 u.ab */.. u.ab
1cb15 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 20 2d 20 .p1 = pOp->p1 -
1cb16 31 3b 0a 20 20 75 2e 61 62 2e 70 32 20 3d 20 70 1;. u.ab.p2 = p
1cb17 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 62 2e 6e Op->p2;. u.ab.n
1cb18 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 = pOp->p3;. as
1cb19 73 65 72 74 28 20 75 2e 61 62 2e 70 31 3e 3d 30 sert( u.ab.p1>=0
1cb1a 20 26 26 20 75 2e 61 62 2e 70 31 2b 75 2e 61 62 && u.ab.p1+u.ab
1cb1b 2e 6e 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20 .n<=p->nVar );.
1cb1c 20 61 73 73 65 72 74 28 20 75 2e 61 62 2e 70 32 assert( u.ab.p2
1cb1d 3e 3d 31 20 26 26 20 75 2e 61 62 2e 70 32 2b 75 >=1 && u.ab.p2+u
1cb1e 2e 61 62 2e 6e 2d 31 3c 3d 70 2d 3e 6e 4d 65 6d .ab.n-1<=p->nMem
1cb1f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
1cb20 70 2d 3e 70 34 2e 7a 3d 3d 30 20 7c 7c 20 70 4f p->p4.z==0 || pO
1cb21 70 2d 3e 70 33 3d 3d 31 20 7c 7c 20 70 4f 70 2d p->p3==1 || pOp-
1cb22 3e 70 33 3d 3d 30 20 29 3b 0a 0a 20 20 77 68 69 >p3==0 );.. whi
1cb23 6c 65 28 20 75 2e 61 62 2e 6e 2d 2d 20 3e 20 30 le( u.ab.n-- > 0
1cb24 20 29 7b 0a 20 20 20 20 75 2e 61 62 2e 70 56 61 ){. u.ab.pVa
1cb25 72 20 3d 20 26 70 2d 3e 61 56 61 72 5b 75 2e 61 r = &p->aVar[u.a
1cb26 62 2e 70 31 2b 2b 5d 3b 0a 20 20 20 20 69 66 28 b.p1++];. if(
1cb27 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 sqlite3VdbeMemT
1cb28 6f 6f 42 69 67 28 75 2e 61 62 2e 70 56 61 72 29 ooBig(u.ab.pVar)
1cb29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 ){. goto t
1cb2a 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 oo_big;. }.
1cb2b 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 pOut = &p->aMe
1cb2c 6d 5b 75 2e 61 62 2e 70 32 2b 2b 5d 3b 0a 20 20 m[u.ab.p2++];.
1cb2d 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1cb2e 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 ReleaseExternal(
1cb2f 70 4f 75 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d pOut);. pOut-
1cb30 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c >flags = MEM_Nul
1cb31 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 l;. sqlite3Vd
1cb32 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 beMemShallowCopy
1cb33 28 70 4f 75 74 2c 20 75 2e 61 62 2e 70 56 61 72 (pOut, u.ab.pVar
1cb34 2c 20 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 , MEM_Static);.
1cb35 20 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c UPDATE_MAX_BL
1cb36 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 OBSIZE(pOut);.
1cb37 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1cb38 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 20 50 31 Opcode: Move P1
1cb39 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1cb3a 20 4d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 73 Move the values
1cb3b 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e in register P1.
1cb3c 2e 50 31 2b 50 33 2d 31 20 6f 76 65 72 20 69 6e .P1+P3-1 over in
1cb3d 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 to.** registers
1cb3e 50 32 2e 2e 50 32 2b 50 33 2d 31 2e 20 20 52 65 P2..P2+P3-1. Re
1cb3f 67 69 73 74 65 72 73 20 50 31 2e 2e 50 31 2b 50 gisters P1..P1+P
1cb40 31 2d 31 20 61 72 65 0a 2a 2a 20 6c 65 66 74 20 1-1 are.** left
1cb41 68 6f 6c 64 69 6e 67 20 61 20 4e 55 4c 4c 2e 20 holding a NULL.
1cb42 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 It is an error
1cb43 66 6f 72 20 72 65 67 69 73 74 65 72 20 72 61 6e for register ran
1cb44 67 65 73 0a 2a 2a 20 50 31 2e 2e 50 31 2b 50 33 ges.** P1..P1+P3
1cb45 2d 31 20 61 6e 64 20 50 32 2e 2e 50 32 2b 50 33 -1 and P2..P2+P3
1cb46 2d 31 20 74 6f 20 6f 76 65 72 6c 61 70 2e 0a 2a -1 to overlap..*
1cb47 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 3a 20 /.case OP_Move:
1cb48 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 {.#if 0 /* loca
1cb49 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1cb4a 64 20 69 6e 74 6f 20 75 2e 61 63 20 2a 2f 0a 20 d into u.ac */.
1cb4b 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 char *zMalloc;
1cb4c 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 76 61 72 /* Holding var
1cb4d 69 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 iable for alloca
1cb4e 74 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 ted memory */.
1cb4f 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 int n;
1cb50 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 /* Number of re
1cb51 67 69 73 74 65 72 73 20 6c 65 66 74 20 74 6f 20 gisters left to
1cb52 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20 70 31 copy */. int p1
1cb53 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 ; /* Re
1cb54 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 66 gister to copy f
1cb55 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 70 32 3b rom */. int p2;
1cb56 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 /* Reg
1cb57 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 74 6f ister to copy to
1cb58 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f */.#endif /* lo
1cb59 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1cb5a 76 65 64 20 69 6e 74 6f 20 75 2e 61 63 20 2a 2f ved into u.ac */
1cb5b 0a 0a 20 20 75 2e 61 63 2e 6e 20 3d 20 70 4f 70 .. u.ac.n = pOp
1cb5c 2d 3e 70 33 3b 0a 20 20 75 2e 61 63 2e 70 31 20 ->p3;. u.ac.p1
1cb5d 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 = pOp->p1;. u.a
1cb5e 63 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a c.p2 = pOp->p2;.
1cb5f 20 20 61 73 73 65 72 74 28 20 75 2e 61 63 2e 6e assert( u.ac.n
1cb60 3e 30 20 26 26 20 75 2e 61 63 2e 70 31 3e 30 20 >0 && u.ac.p1>0
1cb61 26 26 20 75 2e 61 63 2e 70 32 3e 30 20 29 3b 0a && u.ac.p2>0 );.
1cb62 20 20 61 73 73 65 72 74 28 20 75 2e 61 63 2e 70 assert( u.ac.p
1cb63 31 2b 75 2e 61 63 2e 6e 3c 3d 75 2e 61 63 2e 70 1+u.ac.n<=u.ac.p
1cb64 32 20 7c 7c 20 75 2e 61 63 2e 70 32 2b 75 2e 61 2 || u.ac.p2+u.a
1cb65 63 2e 6e 3c 3d 75 2e 61 63 2e 70 31 20 29 3b 0a c.n<=u.ac.p1 );.
1cb66 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d . pIn1 = &p->aM
1cb67 65 6d 5b 75 2e 61 63 2e 70 31 5d 3b 0a 20 20 70 em[u.ac.p1];. p
1cb68 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 Out = &p->aMem[u
1cb69 2e 61 63 2e 70 32 5d 3b 0a 20 20 77 68 69 6c 65 .ac.p2];. while
1cb6a 28 20 75 2e 61 63 2e 6e 2d 2d 20 29 7b 0a 20 20 ( u.ac.n-- ){.
1cb6b 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 3c 3d assert( pOut<=
1cb6c 26 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d &p->aMem[p->nMem
1cb6d 5d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ] );. assert(
1cb6e 20 70 49 6e 31 3c 3d 26 70 2d 3e 61 4d 65 6d 5b pIn1<=&p->aMem[
1cb6f 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 20 20 20 p->nMem] );.
1cb70 75 2e 61 63 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 u.ac.zMalloc = p
1cb71 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 Out->zMalloc;.
1cb72 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 pOut->zMalloc
1cb73 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 = 0;. sqlite3
1cb74 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70 4f 75 74 VdbeMemMove(pOut
1cb75 2c 20 70 49 6e 31 29 3b 0a 20 20 20 20 70 49 6e , pIn1);. pIn
1cb76 31 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 75 2e 61 1->zMalloc = u.a
1cb77 63 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20 52 c.zMalloc;. R
1cb78 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 75 2e EGISTER_TRACE(u.
1cb79 61 63 2e 70 32 2b 2b 2c 20 70 4f 75 74 29 3b 0a ac.p2++, pOut);.
1cb7a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a 20 20 20 20 pIn1++;.
1cb7b 70 4f 75 74 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 pOut++;. }. br
1cb7c 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1cb7d 65 3a 20 43 6f 70 79 20 50 31 20 50 32 20 2a 20 e: Copy P1 P2 *
1cb7e 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61 6b 65 20 61 * *.**.** Make a
1cb7f 20 63 6f 70 79 20 6f 66 20 72 65 67 69 73 74 65 copy of registe
1cb80 72 20 50 31 20 69 6e 74 6f 20 72 65 67 69 73 74 r P1 into regist
1cb81 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 er P2..**.** Thi
1cb82 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6d 61 s instruction ma
1cb83 6b 65 73 20 61 20 64 65 65 70 20 63 6f 70 79 20 kes a deep copy
1cb84 6f 66 20 74 68 65 20 76 61 6c 75 65 2e 20 20 41 of the value. A
1cb85 20 64 75 70 6c 69 63 61 74 65 0a 2a 2a 20 69 73 duplicate.** is
1cb86 20 6d 61 64 65 20 6f 66 20 61 6e 79 20 73 74 72 made of any str
1cb87 69 6e 67 20 6f 72 20 62 6c 6f 62 20 63 6f 6e 73 ing or blob cons
1cb88 74 61 6e 74 2e 20 20 53 65 65 20 61 6c 73 6f 20 tant. See also
1cb89 4f 50 5f 53 43 6f 70 79 2e 0a 2a 2f 0a 63 61 73 OP_SCopy..*/.cas
1cb8a 65 20 4f 50 5f 43 6f 70 79 3a 20 7b 20 20 20 20 e OP_Copy: {
1cb8b 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 /* in1
1cb8c 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 */. assert( pOp
1cb8d 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 61 73 73 65 ->p2>0 );. asse
1cb8e 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e rt( pOp->p2<=p->
1cb8f 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 20 3d nMem );. pOut =
1cb90 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1cb91 32 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 2];. assert( pO
1cb92 75 74 21 3d 70 49 6e 31 20 29 3b 0a 20 20 73 71 ut!=pIn1 );. sq
1cb93 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c lite3VdbeMemShal
1cb94 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c 20 70 49 lowCopy(pOut, pI
1cb95 6e 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a n1, MEM_Ephem);.
1cb96 20 20 44 65 65 70 68 65 6d 65 72 61 6c 69 7a 65 Deephemeralize
1cb97 28 70 4f 75 74 29 3b 0a 20 20 52 45 47 49 53 54 (pOut);. REGIST
1cb98 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 ER_TRACE(pOp->p2
1cb99 2c 20 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b , pOut);. break
1cb9a 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1cb9b 53 43 6f 70 79 20 50 31 20 50 32 20 2a 20 2a 20 SCopy P1 P2 * *
1cb9c 2a 0a 2a 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 73 *.**.** Make a s
1cb9d 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f 66 20 72 hallow copy of r
1cb9e 65 67 69 73 74 65 72 20 50 31 20 69 6e 74 6f 20 egister P1 into
1cb9f 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a register P2..**.
1cba0 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 ** This instruct
1cba1 69 6f 6e 20 6d 61 6b 65 73 20 61 20 73 68 61 6c ion makes a shal
1cba2 6c 6f 77 20 63 6f 70 79 20 6f 66 20 74 68 65 20 low copy of the
1cba3 76 61 6c 75 65 2e 20 20 49 66 20 74 68 65 20 76 value. If the v
1cba4 61 6c 75 65 0a 2a 2a 20 69 73 20 61 20 73 74 72 alue.** is a str
1cba5 69 6e 67 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 ing or blob, the
1cba6 6e 20 74 68 65 20 63 6f 70 79 20 69 73 20 6f 6e n the copy is on
1cba7 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ly a pointer to
1cba8 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 the.** original
1cba9 61 6e 64 20 68 65 6e 63 65 20 69 66 20 74 68 65 and hence if the
1cbaa 20 6f 72 69 67 69 6e 61 6c 20 63 68 61 6e 67 65 original change
1cbab 73 20 73 6f 20 77 69 6c 6c 20 74 68 65 20 63 6f s so will the co
1cbac 70 79 2e 0a 2a 2a 20 57 6f 72 73 65 2c 20 69 66 py..** Worse, if
1cbad 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 69 73 the original is
1cbae 20 64 65 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68 deallocated, th
1cbaf 65 20 63 6f 70 79 20 62 65 63 6f 6d 65 73 20 69 e copy becomes i
1cbb0 6e 76 61 6c 69 64 2e 0a 2a 2a 20 54 68 75 73 20 nvalid..** Thus
1cbb1 74 68 65 20 70 72 6f 67 72 61 6d 20 6d 75 73 74 the program must
1cbb2 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 guarantee that
1cbb3 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 77 69 6c the original wil
1cbb4 6c 20 6e 6f 74 20 63 68 61 6e 67 65 0a 2a 2a 20 l not change.**
1cbb5 64 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 74 during the lifet
1cbb6 69 6d 65 20 6f 66 20 74 68 65 20 63 6f 70 79 2e ime of the copy.
1cbb7 20 20 55 73 65 20 4f 50 5f 43 6f 70 79 20 74 6f Use OP_Copy to
1cbb8 20 6d 61 6b 65 20 61 20 63 6f 6d 70 6c 65 74 65 make a complete
1cbb9 0a 2a 2a 20 63 6f 70 79 2e 0a 2a 2f 0a 63 61 73 .** copy..*/.cas
1cbba 65 20 4f 50 5f 53 43 6f 70 79 3a 20 7b 20 20 20 e OP_SCopy: {
1cbbb 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 /* in1
1cbbc 2a 2f 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 */. REGISTER_TR
1cbbd 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e ACE(pOp->p1, pIn
1cbbe 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 1);. assert( pO
1cbbf 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 61 73 73 p->p2>0 );. ass
1cbc0 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d ert( pOp->p2<=p-
1cbc1 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 20 >nMem );. pOut
1cbc2 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
1cbc3 70 32 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 p2];. assert( p
1cbc4 4f 75 74 21 3d 70 49 6e 31 20 29 3b 0a 20 20 73 Out!=pIn1 );. s
1cbc5 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 qlite3VdbeMemSha
1cbc6 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c 20 70 llowCopy(pOut, p
1cbc7 49 6e 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b In1, MEM_Ephem);
1cbc8 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 . REGISTER_TRAC
1cbc9 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 E(pOp->p2, pOut)
1cbca 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1cbcb 20 4f 70 63 6f 64 65 3a 20 52 65 73 75 6c 74 52 Opcode: ResultR
1cbcc 6f 77 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a ow P1 P2 * * *.*
1cbcd 2a 0a 2a 2a 20 54 68 65 20 72 65 67 69 73 74 65 *.** The registe
1cbce 72 73 20 50 31 20 74 68 72 6f 75 67 68 20 50 31 rs P1 through P1
1cbcf 2b 50 32 2d 31 20 63 6f 6e 74 61 69 6e 20 61 20 +P2-1 contain a
1cbd0 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 0a 2a 2a single row of.**
1cbd1 20 72 65 73 75 6c 74 73 2e 20 54 68 69 73 20 6f results. This o
1cbd2 70 63 6f 64 65 20 63 61 75 73 65 73 20 74 68 65 pcode causes the
1cbd3 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 sqlite3_step()
1cbd4 63 61 6c 6c 20 74 6f 20 74 65 72 6d 69 6e 61 74 call to terminat
1cbd5 65 0a 2a 2a 20 77 69 74 68 20 61 6e 20 53 51 4c e.** with an SQL
1cbd6 49 54 45 5f 52 4f 57 20 72 65 74 75 72 6e 20 63 ITE_ROW return c
1cbd7 6f 64 65 20 61 6e 64 20 69 74 20 73 65 74 73 20 ode and it sets
1cbd8 75 70 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 up the sqlite3_s
1cbd9 74 6d 74 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 tmt.** structure
1cbda 20 74 6f 20 70 72 6f 76 69 64 65 20 61 63 63 65 to provide acce
1cbdb 73 73 20 74 6f 20 74 68 65 20 74 6f 70 20 50 31 ss to the top P1
1cbdc 20 76 61 6c 75 65 73 20 61 73 20 74 68 65 20 72 values as the r
1cbdd 65 73 75 6c 74 0a 2a 2a 20 72 6f 77 2e 0a 2a 2f esult.** row..*/
1cbde 0a 63 61 73 65 20 4f 50 5f 52 65 73 75 6c 74 52 .case OP_ResultR
1cbdf 6f 77 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 ow: {.#if 0 /*
1cbe0 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1cbe1 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 64 20 moved into u.ad
1cbe2 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a */. Mem *pMem;.
1cbe3 20 20 69 6e 74 20 69 3b 0a 23 65 6e 64 69 66 20 int i;.#endif
1cbe4 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1cbe5 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1cbe6 61 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 ad */. assert(
1cbe7 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70 p->nResColumn==p
1cbe8 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 61 73 73 65 Op->p2 );. asse
1cbe9 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b rt( pOp->p1>0 );
1cbea 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1cbeb 70 31 2b 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e p1+pOp->p2<=p->n
1cbec 4d 65 6d 2b 31 20 29 3b 0a 0a 20 20 2f 2a 20 49 Mem+1 );.. /* I
1cbed 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 f this statement
1cbee 20 68 61 73 20 76 69 6f 6c 61 74 65 64 20 69 6d has violated im
1cbef 6d 65 64 69 61 74 65 20 66 6f 72 65 69 67 6e 20 mediate foreign
1cbf0 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c key constraints,
1cbf1 20 64 6f 0a 20 20 2a 2a 20 6e 6f 74 20 72 65 74 do. ** not ret
1cbf2 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
1cbf3 66 20 72 6f 77 73 20 6d 6f 64 69 66 69 65 64 2e f rows modified.
1cbf4 20 41 6e 64 20 64 6f 20 6e 6f 74 20 52 45 4c 45 And do not RELE
1cbf5 41 53 45 20 74 68 65 20 73 74 61 74 65 6d 65 6e ASE the statemen
1cbf6 74 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 t. ** transacti
1cbf7 6f 6e 2e 20 49 74 20 6e 65 65 64 73 20 74 6f 20 on. It needs to
1cbf8 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 be rolled back.
1cbf9 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 */. if( SQLITE
1cbfa 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 _OK!=(rc = sqlit
1cbfb 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c e3VdbeCheckFk(p,
1cbfc 20 30 29 29 20 29 7b 0a 20 20 20 20 61 73 73 65 0)) ){. asse
1cbfd 72 74 28 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 rt( db->flags&SQ
1cbfe 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 LITE_CountRows )
1cbff 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d ;. assert( p-
1cc00 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c >usesStmtJournal
1cc01 20 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 );. break;.
1cc02 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }.. /* If the
1cc03 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 SQLITE_CountRows
1cc04 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 6e 20 flag is set in
1cc05 73 71 6c 69 74 65 33 2e 66 6c 61 67 73 20 6d 61 sqlite3.flags ma
1cc06 73 6b 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 44 4d sk, then. ** DM
1cc07 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 76 L statements inv
1cc08 6f 6b 65 20 74 68 69 73 20 6f 70 63 6f 64 65 20 oke this opcode
1cc09 74 6f 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 to return the nu
1cc0a 6d 62 65 72 20 6f 66 20 72 6f 77 73 0a 20 20 2a mber of rows. *
1cc0b 2a 20 6d 6f 64 69 66 69 65 64 20 74 6f 20 74 68 * modified to th
1cc0c 65 20 75 73 65 72 2e 20 54 68 69 73 20 69 73 20 e user. This is
1cc0d 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 68 61 the only way tha
1cc0e 74 20 61 20 56 4d 20 74 68 61 74 0a 20 20 2a 2a t a VM that. **
1cc0f 20 6f 70 65 6e 73 20 61 20 73 74 61 74 65 6d 65 opens a stateme
1cc10 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d nt transaction m
1cc11 61 79 20 69 6e 76 6f 6b 65 20 74 68 69 73 20 6f ay invoke this o
1cc12 70 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a pcode.. **. **
1cc13 20 49 6e 20 63 61 73 65 20 74 68 69 73 20 69 73 In case this is
1cc14 20 73 75 63 68 20 61 20 73 74 61 74 65 6d 65 6e such a statemen
1cc15 74 2c 20 63 6c 6f 73 65 20 61 6e 79 20 73 74 61 t, close any sta
1cc16 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
1cc17 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e 65 64 20 62 on. ** opened b
1cc18 79 20 74 68 69 73 20 56 4d 20 62 65 66 6f 72 65 y this VM before
1cc19 20 72 65 74 75 72 6e 69 6e 67 20 63 6f 6e 74 72 returning contr
1cc1a 6f 6c 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 ol to the user.
1cc1b 54 68 69 73 20 69 73 20 74 6f 0a 20 20 2a 2a 20 This is to. **
1cc1c 65 6e 73 75 72 65 20 74 68 61 74 20 73 74 61 74 ensure that stat
1cc1d 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f ement-transactio
1cc1e 6e 73 20 61 72 65 20 61 6c 77 61 79 73 20 6e 65 ns are always ne
1cc1f 73 74 65 64 2c 20 6e 6f 74 20 6f 76 65 72 6c 61 sted, not overla
1cc20 70 70 69 6e 67 2e 0a 20 20 2a 2a 20 49 66 20 74 pping.. ** If t
1cc21 68 65 20 6f 70 65 6e 20 73 74 61 74 65 6d 65 6e he open statemen
1cc22 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 t-transaction is
1cc23 20 6e 6f 74 20 63 6c 6f 73 65 64 20 68 65 72 65 not closed here
1cc24 2c 20 74 68 65 6e 20 74 68 65 20 75 73 65 72 0a , then the user.
1cc25 20 20 2a 2a 20 6d 61 79 20 73 74 65 70 20 61 6e ** may step an
1cc26 6f 74 68 65 72 20 56 4d 20 74 68 61 74 20 6f 70 other VM that op
1cc27 65 6e 73 20 69 74 73 20 6f 77 6e 20 73 74 61 74 ens its own stat
1cc28 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
1cc29 6e 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6d 61 79 n. This. ** may
1cc2a 20 6c 65 61 64 20 74 6f 20 6f 76 65 72 6c 61 70 lead to overlap
1cc2b 70 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 74 ping statement t
1cc2c 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 20 20 2a ransactions.. *
1cc2d 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74 61 74 65 *. ** The state
1cc2e 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
1cc2f 20 69 73 20 6e 65 76 65 72 20 61 20 74 6f 70 2d is never a top-
1cc30 6c 65 76 65 6c 20 74 72 61 6e 73 61 63 74 69 6f level transactio
1cc31 6e 2e 20 20 48 65 6e 63 65 0a 20 20 2a 2a 20 74 n. Hence. ** t
1cc32 68 65 20 52 45 4c 45 41 53 45 20 63 61 6c 6c 20 he RELEASE call
1cc33 62 65 6c 6f 77 20 63 61 6e 20 6e 65 76 65 72 20 below can never
1cc34 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 fail.. */. ass
1cc35 65 72 74 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 ert( p->iStateme
1cc36 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 66 6c 61 nt==0 || db->fla
1cc37 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 gs&SQLITE_CountR
1cc38 6f 77 73 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 ows );. rc = sq
1cc39 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 lite3VdbeCloseSt
1cc3a 61 74 65 6d 65 6e 74 28 70 2c 20 53 41 56 45 50 atement(p, SAVEP
1cc3b 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 3b 0a 20 OINT_RELEASE);.
1cc3c 20 69 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 if( NEVER(rc!=S
1cc3d 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 QLITE_OK) ){.
1cc3e 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f break;. }.. /
1cc3f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c * Invalidate all
1cc40 20 65 70 68 65 6d 65 72 61 6c 20 63 75 72 73 6f ephemeral curso
1cc41 72 20 72 6f 77 20 63 61 63 68 65 73 20 2a 2f 0a r row caches */.
1cc42 20 20 70 2d 3e 63 61 63 68 65 43 74 72 20 3d 20 p->cacheCtr =
1cc43 28 70 2d 3e 63 61 63 68 65 43 74 72 20 2b 20 32 (p->cacheCtr + 2
1cc44 29 7c 31 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 )|1;.. /* Make
1cc45 73 75 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 sure the results
1cc46 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 of the current
1cc47 72 6f 77 20 61 72 65 20 5c 30 30 30 20 74 65 72 row are \000 ter
1cc48 6d 69 6e 61 74 65 64 0a 20 20 2a 2a 20 61 6e 64 minated. ** and
1cc49 20 68 61 76 65 20 61 6e 20 61 73 73 69 67 6e 65 have an assigne
1cc4a 64 20 74 79 70 65 2e 20 20 54 68 65 20 72 65 73 d type. The res
1cc4b 75 6c 74 73 20 61 72 65 20 64 65 2d 65 70 68 65 ults are de-ephe
1cc4c 6d 65 72 61 6c 69 7a 65 64 20 61 73 0a 20 20 2a meralized as. *
1cc4d 2a 20 61 73 20 73 69 64 65 20 65 66 66 65 63 74 * as side effect
1cc4e 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 64 2e 70 4d .. */. u.ad.pM
1cc4f 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75 6c 74 53 em = p->pResultS
1cc50 65 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f et = &p->aMem[pO
1cc51 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 75 2e p->p1];. for(u.
1cc52 61 64 2e 69 3d 30 3b 20 75 2e 61 64 2e 69 3c 70 ad.i=0; u.ad.i<p
1cc53 4f 70 2d 3e 70 32 3b 20 75 2e 61 64 2e 69 2b 2b Op->p2; u.ad.i++
1cc54 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1cc55 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 beMemNulTerminat
1cc56 65 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e 61 e(&u.ad.pMem[u.a
1cc57 64 2e 69 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 d.i]);. sqlit
1cc58 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 e3VdbeMemStoreTy
1cc59 70 65 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e pe(&u.ad.pMem[u.
1cc5a 61 64 2e 69 5d 29 3b 0a 20 20 20 20 52 45 47 49 ad.i]);. REGI
1cc5b 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
1cc5c 70 31 2b 75 2e 61 64 2e 69 2c 20 26 75 2e 61 64 p1+u.ad.i, &u.ad
1cc5d 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d 29 3b 0a .pMem[u.ad.i]);.
1cc5e 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 }. if( db->ma
1cc5f 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 llocFailed ) got
1cc60 6f 20 6e 6f 5f 6d 65 6d 3b 0a 0a 20 20 2f 2a 20 o no_mem;.. /*
1cc61 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 4f Return SQLITE_RO
1cc62 57 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 63 20 3d W. */. p->pc =
1cc63 20 70 63 20 2b 20 31 3b 0a 20 20 72 63 20 3d 20 pc + 1;. rc =
1cc64 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 67 6f SQLITE_ROW;. go
1cc65 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a to vdbe_return;.
1cc66 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f }../* Opcode: Co
1cc67 6e 63 61 74 20 50 31 20 50 32 20 50 33 20 2a 20 ncat P1 P2 P3 *
1cc68 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 *.**.** Add the
1cc69 74 65 78 74 20 69 6e 20 72 65 67 69 73 74 65 72 text in register
1cc6a 20 50 31 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 P1 onto the end
1cc6b 20 6f 66 20 74 68 65 20 74 65 78 74 20 69 6e 0a of the text in.
1cc6c 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32 20 61 ** register P2 a
1cc6d 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 nd store the res
1cc6e 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 ult in register
1cc6f 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 P3..** If either
1cc70 20 74 68 65 20 50 31 20 6f 72 20 50 32 20 74 65 the P1 or P2 te
1cc71 78 74 20 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e xt are NULL then
1cc72 20 73 74 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 50 store NULL in P
1cc73 33 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 33 20 3d 20 3..**.** P3 =
1cc74 50 32 20 7c 7c 20 50 31 0a 2a 2a 0a 2a 2a 20 49 P2 || P1.**.** I
1cc75 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 66 6f 72 t is illegal for
1cc76 20 50 31 20 61 6e 64 20 50 33 20 74 6f 20 62 65 P1 and P3 to be
1cc77 20 74 68 65 20 73 61 6d 65 20 72 65 67 69 73 74 the same regist
1cc78 65 72 2e 20 53 6f 6d 65 74 69 6d 65 73 2c 0a 2a er. Sometimes,.*
1cc79 2a 20 69 66 20 50 33 20 69 73 20 74 68 65 20 73 * if P3 is the s
1cc7a 61 6d 65 20 72 65 67 69 73 74 65 72 20 61 73 20 ame register as
1cc7b 50 32 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e P2, the implemen
1cc7c 74 61 74 69 6f 6e 20 69 73 20 61 62 6c 65 0a 2a tation is able.*
1cc7d 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d * to avoid a mem
1cc7e 63 70 79 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f cpy()..*/.case O
1cc7f 50 5f 43 6f 6e 63 61 74 3a 20 7b 20 20 20 20 20 P_Concat: {
1cc80 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1cc81 20 54 4b 5f 43 4f 4e 43 41 54 2c 20 69 6e 31 2c TK_CONCAT, in1,
1cc82 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 in2, out3 */.#i
1cc83 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1cc84 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1cc85 74 6f 20 75 2e 61 65 20 2a 2f 0a 20 20 69 36 34 to u.ae */. i64
1cc86 20 6e 42 79 74 65 3b 0a 23 65 6e 64 69 66 20 2f nByte;.#endif /
1cc87 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1cc88 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1cc89 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 e */.. assert(
1cc8a 70 49 6e 31 21 3d 70 4f 75 74 20 29 3b 0a 20 20 pIn1!=pOut );.
1cc8b 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 if( (pIn1->flags
1cc8c 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20 | pIn2->flags)
1cc8d 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 & MEM_Null ){.
1cc8e 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1cc8f 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 SetNull(pOut);.
1cc90 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 break;. }.
1cc91 69 66 28 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 if( ExpandBlob(p
1cc92 49 6e 31 29 20 7c 7c 20 45 78 70 61 6e 64 42 6c In1) || ExpandBl
1cc93 6f 62 28 70 49 6e 32 29 20 29 20 67 6f 74 6f 20 ob(pIn2) ) goto
1cc94 6e 6f 5f 6d 65 6d 3b 0a 20 20 53 74 72 69 6e 67 no_mem;. String
1cc95 69 66 79 28 70 49 6e 31 2c 20 65 6e 63 6f 64 69 ify(pIn1, encodi
1cc96 6e 67 29 3b 0a 20 20 53 74 72 69 6e 67 69 66 79 ng);. Stringify
1cc97 28 70 49 6e 32 2c 20 65 6e 63 6f 64 69 6e 67 29 (pIn2, encoding)
1cc98 3b 0a 20 20 75 2e 61 65 2e 6e 42 79 74 65 20 3d ;. u.ae.nByte =
1cc99 20 70 49 6e 31 2d 3e 6e 20 2b 20 70 49 6e 32 2d pIn1->n + pIn2-
1cc9a 3e 6e 3b 0a 20 20 69 66 28 20 75 2e 61 65 2e 6e >n;. if( u.ae.n
1cc9b 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b Byte>db->aLimit[
1cc9c 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e SQLITE_LIMIT_LEN
1cc9d 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f GTH] ){. goto
1cc9e 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 too_big;. }.
1cc9f 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 MemSetTypeFlag(p
1cca0 4f 75 74 2c 20 4d 45 4d 5f 53 74 72 29 3b 0a 20 Out, MEM_Str);.
1cca1 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
1cca2 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 28 69 MemGrow(pOut, (i
1cca3 6e 74 29 75 2e 61 65 2e 6e 42 79 74 65 2b 32 2c nt)u.ae.nByte+2,
1cca4 20 70 4f 75 74 3d 3d 70 49 6e 32 29 20 29 7b 0a pOut==pIn2) ){.
1cca5 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b goto no_mem;
1cca6 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75 74 21 . }. if( pOut!
1cca7 3d 70 49 6e 32 20 29 7b 0a 20 20 20 20 6d 65 6d =pIn2 ){. mem
1cca8 63 70 79 28 70 4f 75 74 2d 3e 7a 2c 20 70 49 6e cpy(pOut->z, pIn
1cca9 32 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 6e 29 3b 0a 2->z, pIn2->n);.
1ccaa 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70 4f }. memcpy(&pO
1ccab 75 74 2d 3e 7a 5b 70 49 6e 32 2d 3e 6e 5d 2c 20 ut->z[pIn2->n],
1ccac 70 49 6e 31 2d 3e 7a 2c 20 70 49 6e 31 2d 3e 6e pIn1->z, pIn1->n
1ccad 29 3b 0a 20 20 70 4f 75 74 2d 3e 7a 5b 75 2e 61 );. pOut->z[u.a
1ccae 65 2e 6e 42 79 74 65 5d 20 3d 20 30 3b 0a 20 20 e.nByte] = 0;.
1ccaf 70 4f 75 74 2d 3e 7a 5b 75 2e 61 65 2e 6e 42 79 pOut->z[u.ae.nBy
1ccb0 74 65 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 4f 75 te+1] = 0;. pOu
1ccb1 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f t->flags |= MEM_
1ccb2 54 65 72 6d 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 Term;. pOut->n
1ccb3 3d 20 28 69 6e 74 29 75 2e 61 65 2e 6e 42 79 74 = (int)u.ae.nByt
1ccb4 65 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d e;. pOut->enc =
1ccb5 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44 encoding;. UPD
1ccb6 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 ATE_MAX_BLOBSIZE
1ccb7 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b (pOut);. break;
1ccb8 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 .}../* Opcode: A
1ccb9 64 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a dd P1 P2 P3 * *.
1ccba 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 76 61 **.** Add the va
1ccbb 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1ccbc 50 31 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 P1 to the value
1ccbd 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a in register P2.*
1ccbe 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 * and store the
1ccbf 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 result in regist
1ccc0 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 er P3..** If eit
1ccc1 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c her input is NUL
1ccc2 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 L, the result is
1ccc3 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 NULL..*/./* Opc
1ccc4 6f 64 65 3a 20 4d 75 6c 74 69 70 6c 79 20 50 31 ode: Multiply P1
1ccc5 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1ccc6 0a 2a 2a 20 4d 75 6c 74 69 70 6c 79 20 74 68 65 .** Multiply the
1ccc7 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1ccc8 65 72 20 50 31 20 62 79 20 74 68 65 20 76 61 6c er P1 by the val
1ccc9 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1ccca 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 2.** and store t
1cccb 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 he result in reg
1cccc 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 ister P3..** If
1cccd 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 either input is
1ccce 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 NULL, the result
1cccf 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 is NULL..*/./*
1ccd0 4f 70 63 6f 64 65 3a 20 53 75 62 74 72 61 63 74 Opcode: Subtract
1ccd1 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1ccd2 0a 2a 2a 20 53 75 62 74 72 61 63 74 20 74 68 65 .** Subtract the
1ccd3 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1ccd4 65 72 20 50 31 20 66 72 6f 6d 20 74 68 65 20 76 er P1 from the v
1ccd5 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1ccd6 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 P2.** and store
1ccd7 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 the result in r
1ccd8 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 egister P3..** I
1ccd9 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 f either input i
1ccda 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 s NULL, the resu
1ccdb 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f lt is NULL..*/./
1ccdc 2a 20 4f 70 63 6f 64 65 3a 20 44 69 76 69 64 65 * Opcode: Divide
1ccdd 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1ccde 0a 2a 2a 20 44 69 76 69 64 65 20 74 68 65 20 76 .** Divide the v
1ccdf 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1cce0 20 50 31 20 62 79 20 74 68 65 20 76 61 6c 75 65 P1 by the value
1cce1 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a in register P2.
1cce2 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 ** and store the
1cce3 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 result in regis
1cce4 74 65 72 20 50 33 20 28 50 33 3d 50 32 2f 50 31 ter P3 (P3=P2/P1
1cce5 29 2e 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 ). If the value
1cce6 69 6e 20 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 in .** register
1cce7 50 31 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e P1 is zero, then
1cce8 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e the result is N
1cce9 55 4c 4c 2e 20 49 66 20 65 69 74 68 65 72 20 69 ULL. If either i
1ccea 6e 70 75 74 20 69 73 20 0a 2a 2a 20 4e 55 4c 4c nput is .** NULL
1cceb 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 , the result is
1ccec 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f NULL..*/./* Opco
1cced 64 65 3a 20 52 65 6d 61 69 6e 64 65 72 20 50 31 de: Remainder P1
1ccee 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1ccef 20 43 6f 6d 70 75 74 65 20 74 68 65 20 72 65 6d Compute the rem
1ccf0 61 69 6e 64 65 72 20 61 66 74 65 72 20 69 6e 74 ainder after int
1ccf1 65 67 65 72 20 64 69 76 69 73 69 6f 6e 20 6f 66 eger division of
1ccf2 20 74 68 65 20 76 61 6c 75 65 20 69 6e 0a 2a 2a the value in.**
1ccf3 20 72 65 67 69 73 74 65 72 20 50 31 20 62 79 20 register P1 by
1ccf4 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1ccf5 69 73 74 65 72 20 50 32 20 61 6e 64 20 73 74 6f ister P2 and sto
1ccf6 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e re the result in
1ccf7 20 50 33 2e 20 0a 2a 2a 20 49 66 20 74 68 65 20 P3. .** If the
1ccf8 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1ccf9 72 20 50 32 20 69 73 20 7a 65 72 6f 20 74 68 65 r P2 is zero the
1ccfa 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e result is NULL.
1ccfb 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 6f 70 .** If either op
1ccfc 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c 20 74 erand is NULL, t
1ccfd 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c he result is NUL
1ccfe 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64 L..*/.case OP_Ad
1ccff 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d:
1cd00 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1cd01 54 4b 5f 50 4c 55 53 2c 20 69 6e 31 2c 20 69 6e TK_PLUS, in1, in
1cd02 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 2, out3 */.case
1cd03 4f 50 5f 53 75 62 74 72 61 63 74 3a 20 20 20 20 OP_Subtract:
1cd04 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1cd05 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53 2c 20 69 e as TK_MINUS, i
1cd06 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f n1, in2, out3 */
1cd07 0a 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c .case OP_Multipl
1cd08 79 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y:
1cd09 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 /* same as TK_ST
1cd0a 41 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 AR, in1, in2, ou
1cd0b 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 69 t3 */.case OP_Di
1cd0c 76 69 64 65 3a 20 20 20 20 20 20 20 20 20 20 20 vide:
1cd0d 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1cd0e 54 4b 5f 53 4c 41 53 48 2c 20 69 6e 31 2c 20 69 TK_SLASH, in1, i
1cd0f 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 n2, out3 */.case
1cd10 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 3a 20 7b OP_Remainder: {
1cd11 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1cd12 6d 65 20 61 73 20 54 4b 5f 52 45 4d 2c 20 69 6e me as TK_REM, in
1cd13 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 1, in2, out3 */.
1cd14 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1cd15 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1cd16 69 6e 74 6f 20 75 2e 61 66 20 2a 2f 0a 20 20 69 into u.af */. i
1cd17 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 2f nt flags; /
1cd18 2a 20 43 6f 6d 62 69 6e 65 64 20 4d 45 4d 5f 2a * Combined MEM_*
1cd19 20 66 6c 61 67 73 20 66 72 6f 6d 20 62 6f 74 68 flags from both
1cd1a 20 69 6e 70 75 74 73 20 2a 2f 0a 20 20 69 36 34 inputs */. i64
1cd1b 20 69 41 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 iA; /*
1cd1c 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f 66 Integer value of
1cd1d 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f left operand */
1cd1e 0a 20 20 69 36 34 20 69 42 3b 20 20 20 20 20 20 . i64 iB;
1cd1f 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 /* Integer va
1cd20 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f 70 65 lue of right ope
1cd21 72 61 6e 64 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 rand */. double
1cd22 20 72 41 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 rA; /* Rea
1cd23 6c 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 l value of left
1cd24 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 64 6f 75 operand */. dou
1cd25 62 6c 65 20 72 42 3b 20 20 20 20 20 20 2f 2a 20 ble rB; /*
1cd26 52 65 61 6c 20 76 61 6c 75 65 20 6f 66 20 72 69 Real value of ri
1cd27 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 23 ght operand */.#
1cd28 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1cd29 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1cd2a 6e 74 6f 20 75 2e 61 66 20 2a 2f 0a 0a 20 20 61 nto u.af */.. a
1cd2b 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e pplyNumericAffin
1cd2c 69 74 79 28 70 49 6e 31 29 3b 0a 20 20 61 70 70 ity(pIn1);. app
1cd2d 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 lyNumericAffinit
1cd2e 79 28 70 49 6e 32 29 3b 0a 20 20 75 2e 61 66 2e y(pIn2);. u.af.
1cd2f 66 6c 61 67 73 20 3d 20 70 49 6e 31 2d 3e 66 6c flags = pIn1->fl
1cd30 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 ags | pIn2->flag
1cd31 73 3b 0a 20 20 69 66 28 20 28 75 2e 61 66 2e 66 s;. if( (u.af.f
1cd32 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 lags & MEM_Null)
1cd33 21 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 !=0 ) goto arith
1cd34 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f metic_result_is_
1cd35 6e 75 6c 6c 3b 0a 20 20 69 66 28 20 28 70 49 6e null;. if( (pIn
1cd36 31 2d 3e 66 6c 61 67 73 20 26 20 70 49 6e 32 2d 1->flags & pIn2-
1cd37 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 >flags & MEM_Int
1cd38 29 3d 3d 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 )==MEM_Int ){.
1cd39 20 20 75 2e 61 66 2e 69 41 20 3d 20 70 49 6e 31 u.af.iA = pIn1
1cd3a 2d 3e 75 2e 69 3b 0a 20 20 20 20 75 2e 61 66 2e ->u.i;. u.af.
1cd3b 69 42 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a iB = pIn2->u.i;.
1cd3c 20 20 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d switch( pOp-
1cd3d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 >opcode ){.
1cd3e 20 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 case OP_Add:
1cd3f 20 20 20 20 20 20 75 2e 61 66 2e 69 42 20 2b 3d u.af.iB +=
1cd40 20 75 2e 61 66 2e 69 41 3b 20 20 20 20 20 20 20 u.af.iA;
1cd41 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 break;. cas
1cd42 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20 20 e OP_Subtract:
1cd43 20 20 75 2e 61 66 2e 69 42 20 2d 3d 20 75 2e 61 u.af.iB -= u.a
1cd44 66 2e 69 41 3b 20 20 20 20 20 20 20 62 72 65 61 f.iA; brea
1cd45 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 k;. case OP
1cd46 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 75 2e _Multiply: u.
1cd47 61 66 2e 69 42 20 2a 3d 20 75 2e 61 66 2e 69 41 af.iB *= u.af.iA
1cd48 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ; break;.
1cd49 20 20 20 20 20 63 61 73 65 20 4f 50 5f 44 69 76 case OP_Div
1cd4a 69 64 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 ide: {. i
1cd4b 66 28 20 75 2e 61 66 2e 69 41 3d 3d 30 20 29 20 f( u.af.iA==0 )
1cd4c 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f goto arithmetic_
1cd4d 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a result_is_null;.
1cd4e 20 20 20 20 20 20 20 20 2f 2a 20 44 69 76 69 64 /* Divid
1cd4f 69 6e 67 20 74 68 65 20 6c 61 72 67 65 73 74 20 ing the largest
1cd50 70 6f 73 73 69 62 6c 65 20 6e 65 67 61 74 69 76 possible negativ
1cd51 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 e 64-bit integer
1cd52 20 28 31 3c 3c 36 33 29 20 62 79 0a 20 20 20 20 (1<<63) by.
1cd53 20 20 20 20 2a 2a 20 2d 31 20 72 65 74 75 72 6e ** -1 return
1cd54 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 6f 6f s an integer too
1cd55 20 6c 61 72 67 65 20 74 6f 20 73 74 6f 72 65 20 large to store
1cd56 69 6e 20 61 20 36 34 2d 62 69 74 20 64 61 74 61 in a 64-bit data
1cd57 2d 74 79 70 65 2e 20 4f 6e 0a 20 20 20 20 20 20 -type. On.
1cd58 20 20 2a 2a 20 73 6f 6d 65 20 61 72 63 68 69 74 ** some archit
1cd59 65 63 74 75 72 65 73 2c 20 74 68 65 20 76 61 6c ectures, the val
1cd5a 75 65 20 6f 76 65 72 66 6c 6f 77 73 20 74 6f 20 ue overflows to
1cd5b 28 31 3c 3c 36 33 29 2e 20 4f 6e 20 6f 74 68 65 (1<<63). On othe
1cd5c 72 73 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 rs,. ** a
1cd5d 20 53 49 47 46 50 45 20 69 73 20 69 73 73 75 65 SIGFPE is issue
1cd5e 64 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 d. The following
1cd5f 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f 72 6d 61 statement norma
1cd60 6c 69 7a 65 73 20 74 68 69 73 0a 20 20 20 20 20 lizes this.
1cd61 20 20 20 2a 2a 20 62 65 68 61 76 69 6f 72 20 73 ** behavior s
1cd62 6f 20 74 68 61 74 20 61 6c 6c 20 61 72 63 68 69 o that all archi
1cd63 74 65 63 74 75 72 65 73 20 62 65 68 61 76 65 20 tectures behave
1cd64 61 73 20 69 66 20 69 6e 74 65 67 65 72 0a 20 20 as if integer.
1cd65 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f ** overflo
1cd66 77 20 6f 63 63 75 72 72 65 64 2e 0a 20 20 20 20 w occurred..
1cd67 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 */. i
1cd68 66 28 20 75 2e 61 66 2e 69 41 3d 3d 2d 31 20 26 f( u.af.iA==-1 &
1cd69 26 20 75 2e 61 66 2e 69 42 3d 3d 53 4d 41 4c 4c & u.af.iB==SMALL
1cd6a 45 53 54 5f 49 4e 54 36 34 20 29 20 75 2e 61 66 EST_INT64 ) u.af
1cd6b 2e 69 41 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 .iA = 1;.
1cd6c 20 75 2e 61 66 2e 69 42 20 2f 3d 20 75 2e 61 66 u.af.iB /= u.af
1cd6d 2e 69 41 3b 0a 20 20 20 20 20 20 20 20 62 72 65 .iA;. bre
1cd6e 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
1cd6f 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 default: {.
1cd70 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41 if( u.af.iA
1cd71 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 ==0 ) goto arith
1cd72 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f metic_result_is_
1cd73 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 66 null;. if
1cd74 28 20 75 2e 61 66 2e 69 41 3d 3d 2d 31 20 29 20 ( u.af.iA==-1 )
1cd75 75 2e 61 66 2e 69 41 20 3d 20 31 3b 0a 20 20 20 u.af.iA = 1;.
1cd76 20 20 20 20 20 75 2e 61 66 2e 69 42 20 25 3d 20 u.af.iB %=
1cd77 75 2e 61 66 2e 69 41 3b 0a 20 20 20 20 20 20 20 u.af.iA;.
1cd78 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
1cd79 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 2d 3e }. pOut->
1cd7a 75 2e 69 20 3d 20 75 2e 61 66 2e 69 42 3b 0a 20 u.i = u.af.iB;.
1cd7b 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 MemSetTypeFla
1cd7c 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 g(pOut, MEM_Int)
1cd7d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 ;. }else{. u
1cd7e 2e 61 66 2e 72 41 20 3d 20 73 71 6c 69 74 65 33 .af.rA = sqlite3
1cd7f 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49 VdbeRealValue(pI
1cd80 6e 31 29 3b 0a 20 20 20 20 75 2e 61 66 2e 72 42 n1);. u.af.rB
1cd81 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 = sqlite3VdbeRe
1cd82 61 6c 56 61 6c 75 65 28 70 49 6e 32 29 3b 0a 20 alValue(pIn2);.
1cd83 20 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e switch( pOp->
1cd84 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 opcode ){.
1cd85 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 case OP_Add:
1cd86 20 20 20 20 20 75 2e 61 66 2e 72 42 20 2b 3d 20 u.af.rB +=
1cd87 75 2e 61 66 2e 72 41 3b 20 20 20 20 20 20 20 62 u.af.rA; b
1cd88 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 reak;. case
1cd89 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20 20 20 OP_Subtract:
1cd8a 20 75 2e 61 66 2e 72 42 20 2d 3d 20 75 2e 61 66 u.af.rB -= u.af
1cd8b 2e 72 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b .rA; break
1cd8c 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f ;. case OP_
1cd8d 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 75 2e 61 Multiply: u.a
1cd8e 66 2e 72 42 20 2a 3d 20 75 2e 61 66 2e 72 41 3b f.rB *= u.af.rA;
1cd8f 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1cd90 20 20 20 20 63 61 73 65 20 4f 50 5f 44 69 76 69 case OP_Divi
1cd91 64 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a de: {. /*
1cd92 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 (double)0 In ca
1cd93 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 se of SQLITE_OMI
1cd94 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
1cd95 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 ... */. i
1cd96 66 28 20 75 2e 61 66 2e 72 41 3d 3d 28 64 6f 75 f( u.af.rA==(dou
1cd97 62 6c 65 29 30 20 29 20 67 6f 74 6f 20 61 72 69 ble)0 ) goto ari
1cd98 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 thmetic_result_i
1cd99 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 s_null;.
1cd9a 75 2e 61 66 2e 72 42 20 2f 3d 20 75 2e 61 66 2e u.af.rB /= u.af.
1cd9b 72 41 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 rA;. brea
1cd9c 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
1cd9d 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 default: {.
1cd9e 20 20 20 20 75 2e 61 66 2e 69 41 20 3d 20 28 69 u.af.iA = (i
1cd9f 36 34 29 75 2e 61 66 2e 72 41 3b 0a 20 20 20 20 64)u.af.rA;.
1cda0 20 20 20 20 75 2e 61 66 2e 69 42 20 3d 20 28 69 u.af.iB = (i
1cda1 36 34 29 75 2e 61 66 2e 72 42 3b 0a 20 20 20 20 64)u.af.rB;.
1cda2 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41 3d if( u.af.iA=
1cda3 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d =0 ) goto arithm
1cda4 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e etic_result_is_n
1cda5 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ull;. if(
1cda6 20 75 2e 61 66 2e 69 41 3d 3d 2d 31 20 29 20 75 u.af.iA==-1 ) u
1cda7 2e 61 66 2e 69 41 20 3d 20 31 3b 0a 20 20 20 20 .af.iA = 1;.
1cda8 20 20 20 20 75 2e 61 66 2e 72 42 20 3d 20 28 64 u.af.rB = (d
1cda9 6f 75 62 6c 65 29 28 75 2e 61 66 2e 69 42 20 25 ouble)(u.af.iB %
1cdaa 20 75 2e 61 66 2e 69 41 29 3b 0a 20 20 20 20 20 u.af.iA);.
1cdab 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1cdac 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
1cdad 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 75 2e 61 sqlite3IsNaN(u.a
1cdae 66 2e 72 42 29 20 29 7b 0a 20 20 20 20 20 20 67 f.rB) ){. g
1cdaf 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 oto arithmetic_r
1cdb0 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 esult_is_null;.
1cdb1 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 2d 3e 72 }. pOut->r
1cdb2 20 3d 20 75 2e 61 66 2e 72 42 3b 0a 20 20 20 20 = u.af.rB;.
1cdb3 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 MemSetTypeFlag(p
1cdb4 4f 75 74 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a Out, MEM_Real);.
1cdb5 20 20 20 20 69 66 28 20 28 75 2e 61 66 2e 66 6c if( (u.af.fl
1cdb6 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 3d ags & MEM_Real)=
1cdb7 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
1cdb8 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 41 66 te3VdbeIntegerAf
1cdb9 66 69 6e 69 74 79 28 70 4f 75 74 29 3b 0a 20 20 finity(pOut);.
1cdba 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b }. }. break;
1cdbb 0a 0a 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 ..arithmetic_res
1cdbc 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3a 0a 20 20 73 ult_is_null:. s
1cdbd 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
1cdbe 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 62 72 Null(pOut);. br
1cdbf 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1cdc0 65 3a 20 43 6f 6c 6c 53 65 71 20 2a 20 2a 20 50 e: CollSeq * * P
1cdc1 34 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 4.**.** P4 is a
1cdc2 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 43 6f 6c pointer to a Col
1cdc3 6c 53 65 71 20 73 74 72 75 63 74 2e 20 49 66 20 lSeq struct. If
1cdc4 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f the next call to
1cdc5 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e a user function
1cdc6 0a 2a 2a 20 6f 72 20 61 67 67 72 65 67 61 74 65 .** or aggregate
1cdc7 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33 47 65 calls sqlite3Ge
1cdc8 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 29 2c 20 tFuncCollSeq(),
1cdc9 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 this collation s
1cdca 65 71 75 65 6e 63 65 20 77 69 6c 6c 0a 2a 2a 20 equence will.**
1cdcb 62 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 be returned. Thi
1cdcc 73 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 s is used by the
1cdcd 20 62 75 69 6c 74 2d 69 6e 20 6d 69 6e 28 29 2c built-in min(),
1cdce 20 6d 61 78 28 29 20 61 6e 64 20 6e 75 6c 6c 69 max() and nulli
1cdcf 66 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 f().** functions
1cdd0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 ..**.** The inte
1cdd1 72 66 61 63 65 20 75 73 65 64 20 62 79 20 74 68 rface used by th
1cdd2 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
1cdd3 20 6f 66 20 74 68 65 20 61 66 6f 72 65 6d 65 6e of the aforemen
1cdd4 74 69 6f 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 tioned functions
1cdd5 0a 2a 2a 20 74 6f 20 72 65 74 72 69 65 76 65 20 .** to retrieve
1cdd6 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 the collation se
1cdd7 71 75 65 6e 63 65 20 73 65 74 20 62 79 20 74 68 quence set by th
1cdd8 69 73 20 6f 70 63 6f 64 65 20 69 73 20 6e 6f 74 is opcode is not
1cdd9 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 70 75 available.** pu
1cdda 62 6c 69 63 6c 79 2c 20 6f 6e 6c 79 20 74 6f 20 blicly, only to
1cddb 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 64 user functions d
1cddc 65 66 69 6e 65 64 20 69 6e 20 66 75 6e 63 2e 63 efined in func.c
1cddd 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6c ..*/.case OP_Col
1cdde 6c 53 65 71 3a 20 7b 0a 20 20 61 73 73 65 72 74 lSeq: {. assert
1cddf 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1cde0 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 62 4_COLLSEQ );. b
1cde1 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1cde2 64 65 3a 20 46 75 6e 63 74 69 6f 6e 20 50 31 20 de: Function P1
1cde3 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a P2 P3 P4 P5.**.*
1cde4 2a 20 49 6e 76 6f 6b 65 20 61 20 75 73 65 72 20 * Invoke a user
1cde5 66 75 6e 63 74 69 6f 6e 20 28 50 34 20 69 73 20 function (P4 is
1cde6 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 46 a pointer to a F
1cde7 75 6e 63 74 69 6f 6e 20 73 74 72 75 63 74 75 72 unction structur
1cde8 65 20 74 68 61 74 0a 2a 2a 20 64 65 66 69 6e 65 e that.** define
1cde9 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 29 20 s the function)
1cdea 77 69 74 68 20 50 35 20 61 72 67 75 6d 65 6e 74 with P5 argument
1cdeb 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72 65 67 s taken from reg
1cdec 69 73 74 65 72 20 50 32 20 61 6e 64 0a 2a 2a 20 ister P2 and.**
1cded 73 75 63 63 65 73 73 6f 72 73 2e 20 20 54 68 65 successors. The
1cdee 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 66 result of the f
1cdef 75 6e 63 74 69 6f 6e 20 69 73 20 73 74 6f 72 65 unction is store
1cdf0 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 d in register P3
1cdf1 2e 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 33 ..** Register P3
1cdf2 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 6f 6e 65 must not be one
1cdf3 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e of the function
1cdf4 20 69 6e 70 75 74 73 2e 0a 2a 2a 0a 2a 2a 20 50 inputs..**.** P
1cdf5 31 20 69 73 20 61 20 33 32 2d 62 69 74 20 62 69 1 is a 32-bit bi
1cdf6 74 6d 61 73 6b 20 69 6e 64 69 63 61 74 69 6e 67 tmask indicating
1cdf7 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
1cdf8 65 61 63 68 20 61 72 67 75 6d 65 6e 74 20 74 6f each argument to
1cdf9 20 74 68 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f the .** functio
1cdfa 6e 20 77 61 73 20 64 65 74 65 72 6d 69 6e 65 64 n was determined
1cdfb 20 74 6f 20 62 65 20 63 6f 6e 73 74 61 6e 74 20 to be constant
1cdfc 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e at compile time.
1cdfd 20 49 66 20 74 68 65 20 66 69 72 73 74 0a 2a 2a If the first.**
1cdfe 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20 63 6f argument was co
1cdff 6e 73 74 61 6e 74 20 74 68 65 6e 20 62 69 74 20 nstant then bit
1ce00 30 20 6f 66 20 50 31 20 69 73 20 73 65 74 2e 20 0 of P1 is set.
1ce01 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 This is used to
1ce02 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 65 determine.** whe
1ce03 74 68 65 72 20 6d 65 74 61 20 64 61 74 61 20 61 ther meta data a
1ce04 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 ssociated with a
1ce05 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 61 user function a
1ce06 72 67 75 6d 65 6e 74 20 75 73 69 6e 67 20 74 68 rgument using th
1ce07 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 65 74 e.** sqlite3_set
1ce08 5f 61 75 78 64 61 74 61 28 29 20 41 50 49 20 6d _auxdata() API m
1ce09 61 79 20 62 65 20 73 61 66 65 6c 79 20 72 65 74 ay be safely ret
1ce0a 61 69 6e 65 64 20 75 6e 74 69 6c 20 74 68 65 20 ained until the
1ce0b 6e 65 78 74 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 next.** invocati
1ce0c 6f 6e 20 6f 66 20 74 68 69 73 20 6f 70 63 6f 64 on of this opcod
1ce0d 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 e..**.** See als
1ce0e 6f 3a 20 41 67 67 53 74 65 70 20 61 6e 64 20 41 o: AggStep and A
1ce0f 67 67 46 69 6e 61 6c 0a 2a 2f 0a 63 61 73 65 20 ggFinal.*/.case
1ce10 4f 50 5f 46 75 6e 63 74 69 6f 6e 3a 20 7b 0a 23 OP_Function: {.#
1ce11 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1ce12 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1ce13 6e 74 6f 20 75 2e 61 67 20 2a 2f 0a 20 20 69 6e nto u.ag */. in
1ce14 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 41 72 67 t i;. Mem *pArg
1ce15 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ;. sqlite3_cont
1ce16 65 78 74 20 63 74 78 3b 0a 20 20 73 71 6c 69 74 ext ctx;. sqlit
1ce17 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c e3_value **apVal
1ce18 3b 0a 20 20 69 6e 74 20 6e 3b 0a 23 65 6e 64 69 ;. int n;.#endi
1ce19 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1ce1a 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1ce1b 75 2e 61 67 20 2a 2f 0a 0a 20 20 75 2e 61 67 2e u.ag */.. u.ag.
1ce1c 6e 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 75 n = pOp->p5;. u
1ce1d 2e 61 67 2e 61 70 56 61 6c 20 3d 20 70 2d 3e 61 .ag.apVal = p->a
1ce1e 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 28 20 pArg;. assert(
1ce1f 75 2e 61 67 2e 61 70 56 61 6c 20 7c 7c 20 75 2e u.ag.apVal || u.
1ce20 61 67 2e 6e 3d 3d 30 20 29 3b 0a 0a 20 20 61 73 ag.n==0 );.. as
1ce21 73 65 72 74 28 20 75 2e 61 67 2e 6e 3d 3d 30 20 sert( u.ag.n==0
1ce22 7c 7c 20 28 70 4f 70 2d 3e 70 32 3e 30 20 26 26 || (pOp->p2>0 &&
1ce23 20 70 4f 70 2d 3e 70 32 2b 75 2e 61 67 2e 6e 3c pOp->p2+u.ag.n<
1ce24 3d 70 2d 3e 6e 4d 65 6d 2b 31 29 20 29 3b 0a 20 =p->nMem+1) );.
1ce25 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 assert( pOp->p3
1ce26 3c 70 4f 70 2d 3e 70 32 20 7c 7c 20 70 4f 70 2d <pOp->p2 || pOp-
1ce27 3e 70 33 3e 3d 70 4f 70 2d 3e 70 32 2b 75 2e 61 >p3>=pOp->p2+u.a
1ce28 67 2e 6e 20 29 3b 0a 20 20 75 2e 61 67 2e 70 41 g.n );. u.ag.pA
1ce29 72 67 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f rg = &p->aMem[pO
1ce2a 70 2d 3e 70 32 5d 3b 0a 20 20 66 6f 72 28 75 2e p->p2];. for(u.
1ce2b 61 67 2e 69 3d 30 3b 20 75 2e 61 67 2e 69 3c 75 ag.i=0; u.ag.i<u
1ce2c 2e 61 67 2e 6e 3b 20 75 2e 61 67 2e 69 2b 2b 2c .ag.n; u.ag.i++,
1ce2d 20 75 2e 61 67 2e 70 41 72 67 2b 2b 29 7b 0a 20 u.ag.pArg++){.
1ce2e 20 20 20 75 2e 61 67 2e 61 70 56 61 6c 5b 75 2e u.ag.apVal[u.
1ce2f 61 67 2e 69 5d 20 3d 20 75 2e 61 67 2e 70 41 72 ag.i] = u.ag.pAr
1ce30 67 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 g;. sqlite3Vd
1ce31 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 75 beMemStoreType(u
1ce32 2e 61 67 2e 70 41 72 67 29 3b 0a 20 20 20 20 52 .ag.pArg);. R
1ce33 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
1ce34 70 2d 3e 70 32 2c 20 75 2e 61 67 2e 70 41 72 67 p->p2, u.ag.pArg
1ce35 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 );. }.. assert
1ce36 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1ce37 34 5f 46 55 4e 43 44 45 46 20 7c 7c 20 70 4f 70 4_FUNCDEF || pOp
1ce38 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 44 42 ->p4type==P4_VDB
1ce39 45 46 55 4e 43 20 29 3b 0a 20 20 69 66 28 20 70 EFUNC );. if( p
1ce3a 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 46 Op->p4type==P4_F
1ce3b 55 4e 43 44 45 46 20 29 7b 0a 20 20 20 20 75 2e UNCDEF ){. u.
1ce3c 61 67 2e 63 74 78 2e 70 46 75 6e 63 20 3d 20 70 ag.ctx.pFunc = p
1ce3d 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 Op->p4.pFunc;.
1ce3e 20 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 u.ag.ctx.pVdbe
1ce3f 46 75 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 Func = 0;. }els
1ce40 65 7b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e e{. u.ag.ctx.
1ce41 70 56 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62 pVdbeFunc = (Vdb
1ce42 65 46 75 6e 63 2a 29 70 4f 70 2d 3e 70 34 2e 70 eFunc*)pOp->p4.p
1ce43 56 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 75 2e VdbeFunc;. u.
1ce44 61 67 2e 63 74 78 2e 70 46 75 6e 63 20 3d 20 75 ag.ctx.pFunc = u
1ce45 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e .ag.ctx.pVdbeFun
1ce46 63 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 c->pFunc;. }..
1ce47 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 assert( pOp->p3
1ce48 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 >0 && pOp->p3<=p
1ce49 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 ->nMem );. pOut
1ce4a 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1ce4b 3e 70 33 5d 3b 0a 20 20 75 2e 61 67 2e 63 74 78 >p3];. u.ag.ctx
1ce4c 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e .s.flags = MEM_N
1ce4d 75 6c 6c 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e ull;. u.ag.ctx.
1ce4e 73 2e 64 62 20 3d 20 64 62 3b 0a 20 20 75 2e 61 s.db = db;. u.a
1ce4f 67 2e 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30 g.ctx.s.xDel = 0
1ce50 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e 7a ;. u.ag.ctx.s.z
1ce51 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 0a 20 20 2f Malloc = 0;.. /
1ce52 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63 65 6c * The output cel
1ce53 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68 61 l may already ha
1ce54 76 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f ve a buffer allo
1ce55 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a cated. Move. **
1ce56 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 the pointer to
1ce57 75 2e 61 67 2e 63 74 78 2e 73 20 73 6f 20 69 6e u.ag.ctx.s so in
1ce58 20 63 61 73 65 20 74 68 65 20 75 73 65 72 2d 66 case the user-f
1ce59 75 6e 63 74 69 6f 6e 20 63 61 6e 20 75 73 65 0a unction can use.
1ce5a 20 20 2a 2a 20 74 68 65 20 61 6c 72 65 61 64 79 ** the already
1ce5b 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 allocated buffe
1ce5c 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c r instead of all
1ce5d 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 6f 6e ocating a new on
1ce5e 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 e.. */. sqlite
1ce5f 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 3VdbeMemMove(&u.
1ce60 61 67 2e 63 74 78 2e 73 2c 20 70 4f 75 74 29 3b ag.ctx.s, pOut);
1ce61 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 . MemSetTypeFla
1ce62 67 28 26 75 2e 61 67 2e 63 74 78 2e 73 2c 20 4d g(&u.ag.ctx.s, M
1ce63 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 75 2e 61 EM_Null);.. u.a
1ce64 67 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 g.ctx.isError =
1ce65 30 3b 0a 20 20 69 66 28 20 75 2e 61 67 2e 63 74 0;. if( u.ag.ct
1ce66 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 x.pFunc->flags &
1ce67 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 SQLITE_FUNC_NEE
1ce68 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73 73 DCOLL ){. ass
1ce69 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61 4f 70 20 ert( pOp>p->aOp
1ce6a 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
1ce6b 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 65 3d 3d 50 Op[-1].p4type==P
1ce6c 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 20 4_COLLSEQ );.
1ce6d 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d assert( pOp[-1]
1ce6e 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 6c .opcode==OP_Coll
1ce6f 53 65 71 20 29 3b 0a 20 20 20 20 75 2e 61 67 2e Seq );. u.ag.
1ce70 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b ctx.pColl = pOp[
1ce71 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 -1].p4.pColl;.
1ce72 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 }. if( sqlite3S
1ce73 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 afetyOff(db) ) g
1ce74 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1ce75 5f 6d 69 73 75 73 65 3b 0a 20 20 28 2a 75 2e 61 _misuse;. (*u.a
1ce76 67 2e 63 74 78 2e 70 46 75 6e 63 2d 3e 78 46 75 g.ctx.pFunc->xFu
1ce77 6e 63 29 28 26 75 2e 61 67 2e 63 74 78 2c 20 75 nc)(&u.ag.ctx, u
1ce78 2e 61 67 2e 6e 2c 20 75 2e 61 67 2e 61 70 56 61 .ag.n, u.ag.apVa
1ce79 6c 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 l);. if( sqlite
1ce7a 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 3SafetyOn(db) ){
1ce7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1ce7c 4d 65 6d 52 65 6c 65 61 73 65 28 26 75 2e 61 67 MemRelease(&u.ag
1ce7d 2e 63 74 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74 .ctx.s);. got
1ce7e 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1ce7f 69 73 75 73 65 3b 0a 20 20 7d 0a 20 20 69 66 28 isuse;. }. if(
1ce80 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1ce81 64 20 29 7b 0a 20 20 20 20 2f 2a 20 45 76 65 6e d ){. /* Even
1ce82 20 74 68 6f 75 67 68 20 61 20 6d 61 6c 6c 6f 63 though a malloc
1ce83 28 29 20 68 61 73 20 66 61 69 6c 65 64 2c 20 74 () has failed, t
1ce84 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
1ce85 6e 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 n of the. **
1ce86 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 6d 61 user function ma
1ce87 79 20 68 61 76 65 20 63 61 6c 6c 65 64 20 61 6e y have called an
1ce88 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
1ce89 58 58 58 28 29 20 66 75 6e 63 74 69 6f 6e 0a 20 XXX() function.
1ce8a 20 20 20 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20 ** to return
1ce8b 61 20 76 61 6c 75 65 2e 20 54 68 65 20 66 6f 6c a value. The fol
1ce8c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 72 65 6c 65 lowing call rele
1ce8d 61 73 65 73 20 61 6e 79 20 72 65 73 6f 75 72 63 ases any resourc
1ce8e 65 73 0a 20 20 20 20 2a 2a 20 61 73 73 6f 63 69 es. ** associ
1ce8f 61 74 65 64 20 77 69 74 68 20 73 75 63 68 20 61 ated with such a
1ce90 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2a 0a 20 value.. **.
1ce91 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 4d 61 79 62 ** Note: Mayb
1ce92 65 20 4d 65 6d 52 65 6c 65 61 73 65 28 29 20 73 e MemRelease() s
1ce93 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 hould be called
1ce94 69 66 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 if sqlite3Safety
1ce95 4f 6e 28 29 0a 20 20 20 20 2a 2a 20 66 61 69 6c On(). ** fail
1ce96 73 20 61 6c 73 6f 20 28 74 68 65 20 69 66 28 2e s also (the if(.
1ce97 2e 2e 29 20 73 74 61 74 65 6d 65 6e 74 20 61 62 ..) statement ab
1ce98 6f 76 65 29 2e 20 42 75 74 20 69 66 20 70 65 6f ove). But if peo
1ce99 70 6c 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 6d ple are. ** m
1ce9a 69 73 75 73 69 6e 67 20 73 71 6c 69 74 65 2c 20 isusing sqlite,
1ce9b 74 68 65 79 20 68 61 76 65 20 62 69 67 67 65 72 they have bigger
1ce9c 20 70 72 6f 62 6c 65 6d 73 20 74 68 61 6e 20 61 problems than a
1ce9d 20 6c 65 61 6b 65 64 20 76 61 6c 75 65 2e 0a 20 leaked value..
1ce9e 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 */. sqlite
1ce9f 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
1cea0 26 75 2e 61 67 2e 63 74 78 2e 73 29 3b 0a 20 20 &u.ag.ctx.s);.
1cea1 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 goto no_mem;.
1cea2 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20 }.. /* If any
1cea3 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 20 66 auxiliary data f
1cea4 75 6e 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65 unctions have be
1cea5 65 6e 20 63 61 6c 6c 65 64 20 62 79 20 74 68 69 en called by thi
1cea6 73 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2c s user function,
1cea7 0a 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c . ** immediatel
1cea8 79 20 63 61 6c 6c 20 74 68 65 20 64 65 73 74 72 y call the destr
1cea9 75 63 74 6f 72 20 66 6f 72 20 61 6e 79 20 6e 6f uctor for any no
1ceaa 6e 2d 73 74 61 74 69 63 20 76 61 6c 75 65 73 2e n-static values.
1ceab 0a 20 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61 67 . */. if( u.ag
1ceac 2e 63 74 78 2e 70 56 64 62 65 46 75 6e 63 20 29 .ctx.pVdbeFunc )
1cead 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1ceae 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 75 eDeleteAuxData(u
1ceaf 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e .ag.ctx.pVdbeFun
1ceb0 63 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 c, pOp->p1);.
1ceb1 20 70 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46 75 pOp->p4.pVdbeFu
1ceb2 6e 63 20 3d 20 75 2e 61 67 2e 63 74 78 2e 70 56 nc = u.ag.ctx.pV
1ceb3 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 70 4f 70 dbeFunc;. pOp
1ceb4 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 56 44 ->p4type = P4_VD
1ceb5 42 45 46 55 4e 43 3b 0a 20 20 7d 0a 0a 20 20 2f BEFUNC;. }.. /
1ceb6 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f * If the functio
1ceb7 6e 20 72 65 74 75 72 6e 65 64 20 61 6e 20 65 72 n returned an er
1ceb8 72 6f 72 2c 20 74 68 72 6f 77 20 61 6e 20 65 78 ror, throw an ex
1ceb9 63 65 70 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 ception */. if(
1ceba 20 75 2e 61 67 2e 63 74 78 2e 69 73 45 72 72 6f u.ag.ctx.isErro
1cebb 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 r ){. sqlite3
1cebc 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 SetString(&p->zE
1cebd 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c rrMsg, db, "%s",
1cebe 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
1cebf 65 78 74 28 26 75 2e 61 67 2e 63 74 78 2e 73 29 ext(&u.ag.ctx.s)
1cec0 29 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 61 67 );. rc = u.ag
1cec1 2e 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 .ctx.isError;.
1cec2 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 }.. /* Copy the
1cec3 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 66 result of the f
1cec4 75 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 72 65 67 unction into reg
1cec5 69 73 74 65 72 20 50 33 20 2a 2f 0a 20 20 73 71 ister P3 */. sq
1cec6 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
1cec7 6e 63 6f 64 69 6e 67 28 26 75 2e 61 67 2e 63 74 ncoding(&u.ag.ct
1cec8 78 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a x.s, encoding);.
1cec9 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1ceca 4d 6f 76 65 28 70 4f 75 74 2c 20 26 75 2e 61 67 Move(pOut, &u.ag
1cecb 2e 63 74 78 2e 73 29 3b 0a 20 20 69 66 28 20 73 .ctx.s);. if( s
1cecc 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f qlite3VdbeMemToo
1cecd 42 69 67 28 70 4f 75 74 29 20 29 7b 0a 20 20 20 Big(pOut) ){.
1cece 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 goto too_big;.
1cecf 20 7d 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 }. REGISTER_TR
1ced0 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 ACE(pOp->p3, pOu
1ced1 74 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 t);. UPDATE_MAX
1ced2 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b _BLOBSIZE(pOut);
1ced3 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1ced4 4f 70 63 6f 64 65 3a 20 42 69 74 41 6e 64 20 50 Opcode: BitAnd P
1ced5 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
1ced6 2a 20 54 61 6b 65 20 74 68 65 20 62 69 74 2d 77 * Take the bit-w
1ced7 69 73 65 20 41 4e 44 20 6f 66 20 74 68 65 20 76 ise AND of the v
1ced8 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 alues in registe
1ced9 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a r P1 and P2 and.
1ceda 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 ** store the res
1cedb 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 ult in register
1cedc 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 P3..** If either
1cedd 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 input is NULL,
1cede 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 the result is NU
1cedf 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 LL..*/./* Opcode
1cee0 3a 20 42 69 74 4f 72 20 50 31 20 50 32 20 50 33 : BitOr P1 P2 P3
1cee1 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 * *.**.** Take
1cee2 74 68 65 20 62 69 74 2d 77 69 73 65 20 4f 52 20 the bit-wise OR
1cee3 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e of the values in
1cee4 20 72 65 67 69 73 74 65 72 20 50 31 20 61 6e 64 register P1 and
1cee5 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65 P2 and.** store
1cee6 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 the result in r
1cee7 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 egister P3..** I
1cee8 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 f either input i
1cee9 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 s NULL, the resu
1ceea 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f lt is NULL..*/./
1ceeb 2a 20 4f 70 63 6f 64 65 3a 20 53 68 69 66 74 4c * Opcode: ShiftL
1ceec 65 66 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a eft P1 P2 P3 * *
1ceed 0a 2a 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68 65 .**.** Shift the
1ceee 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 integer value i
1ceef 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 74 6f n register P2 to
1cef0 20 74 68 65 20 6c 65 66 74 20 62 79 20 74 68 65 the left by the
1cef1 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 .** number of bi
1cef2 74 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 ts specified by
1cef3 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e 20 72 the integer in r
1cef4 65 67 69 73 65 72 20 50 31 2e 0a 2a 2a 20 53 74 egiser P1..** St
1cef5 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 ore the result i
1cef6 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a n register P3..*
1cef7 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 * If either inpu
1cef8 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 t is NULL, the r
1cef9 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a esult is NULL..*
1cefa 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 68 69 /./* Opcode: Shi
1cefb 66 74 52 69 67 68 74 20 50 31 20 50 32 20 50 33 ftRight P1 P2 P3
1cefc 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 68 69 66 74 * *.**.** Shift
1cefd 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c the integer val
1cefe 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1ceff 32 20 74 6f 20 74 68 65 20 72 69 67 68 74 20 62 2 to the right b
1cf00 79 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 y the.** number
1cf01 6f 66 20 62 69 74 73 20 73 70 65 63 69 66 69 65 of bits specifie
1cf02 64 20 62 79 20 74 68 65 20 69 6e 74 65 67 65 72 d by the integer
1cf03 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e in register P1.
1cf04 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 72 65 .** Store the re
1cf05 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 sult in register
1cf06 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 P3..** If eithe
1cf07 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c r input is NULL,
1cf08 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e the result is N
1cf09 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ULL..*/.case OP_
1cf0a 42 69 74 41 6e 64 3a 20 20 20 20 20 20 20 20 20 BitAnd:
1cf0b 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
1cf0c 61 73 20 54 4b 5f 42 49 54 41 4e 44 2c 20 69 6e as TK_BITAND, in
1cf0d 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 1, in2, out3 */.
1cf0e 63 61 73 65 20 4f 50 5f 42 69 74 4f 72 3a 20 20 case OP_BitOr:
1cf0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cf10 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 /* same as TK_BI
1cf11 54 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f TOR, in1, in2, o
1cf12 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 ut3 */.case OP_S
1cf13 68 69 66 74 4c 65 66 74 3a 20 20 20 20 20 20 20 hiftLeft:
1cf14 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1cf15 73 20 54 4b 5f 4c 53 48 49 46 54 2c 20 69 6e 31 s TK_LSHIFT, in1
1cf16 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 , in2, out3 */.c
1cf17 61 73 65 20 4f 50 5f 53 68 69 66 74 52 69 67 68 ase OP_ShiftRigh
1cf18 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f t: { /
1cf19 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 53 48 * same as TK_RSH
1cf1a 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f IFT, in1, in2, o
1cf1b 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a ut3 */.#if 0 /*
1cf1c 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1cf1d 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 68 moved into u.ah
1cf1e 20 2a 2f 0a 20 20 69 36 34 20 61 3b 0a 20 20 69 */. i64 a;. i
1cf1f 36 34 20 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 64 b;.#endif /*
1cf20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1cf21 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 68 20 moved into u.ah
1cf22 2a 2f 0a 0a 20 20 69 66 28 20 28 70 49 6e 31 2d */.. if( (pIn1-
1cf23 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 >flags | pIn2->f
1cf24 6c 61 67 73 29 20 26 20 4d 45 4d 5f 4e 75 6c 6c lags) & MEM_Null
1cf25 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1cf26 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f dbeMemSetNull(pO
1cf27 75 74 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a ut);. break;.
1cf28 20 20 7d 0a 20 20 75 2e 61 68 2e 61 20 3d 20 73 }. u.ah.a = s
1cf29 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c qlite3VdbeIntVal
1cf2a 75 65 28 70 49 6e 32 29 3b 0a 20 20 75 2e 61 68 ue(pIn2);. u.ah
1cf2b 2e 62 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 .b = sqlite3Vdbe
1cf2c 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 3b 0a IntValue(pIn1);.
1cf2d 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f switch( pOp->o
1cf2e 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 63 61 73 pcode ){. cas
1cf2f 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20 20 20 20 e OP_BitAnd:
1cf30 20 20 75 2e 61 68 2e 61 20 26 3d 20 75 2e 61 68 u.ah.a &= u.ah
1cf31 2e 62 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 .b; break;.
1cf32 20 20 20 63 61 73 65 20 4f 50 5f 42 69 74 4f 72 case OP_BitOr
1cf33 3a 20 20 20 20 20 20 20 75 2e 61 68 2e 61 20 7c : u.ah.a |
1cf34 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20 20 62 72 = u.ah.b; br
1cf35 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 eak;. case OP
1cf36 5f 53 68 69 66 74 4c 65 66 74 3a 20 20 20 75 2e _ShiftLeft: u.
1cf37 61 68 2e 61 20 3c 3c 3d 20 75 2e 61 68 2e 62 3b ah.a <<= u.ah.b;
1cf38 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 break;. d
1cf39 65 66 61 75 6c 74 3a 20 20 61 73 73 65 72 74 28 efault: assert(
1cf3a 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
1cf3b 5f 53 68 69 66 74 52 69 67 68 74 20 29 3b 0a 20 _ShiftRight );.
1cf3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cf3d 20 20 20 20 20 20 20 20 75 2e 61 68 2e 61 20 3e u.ah.a >
1cf3e 3e 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20 62 72 >= u.ah.b; br
1cf3f 65 61 6b 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d eak;. }. pOut-
1cf40 3e 75 2e 69 20 3d 20 75 2e 61 68 2e 61 3b 0a 20 >u.i = u.ah.a;.
1cf41 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 MemSetTypeFlag(
1cf42 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a pOut, MEM_Int);.
1cf43 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1cf44 70 63 6f 64 65 3a 20 41 64 64 49 6d 6d 20 20 50 pcode: AddImm P
1cf45 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a 1 P2 * * *.** .*
1cf46 2a 20 41 64 64 20 74 68 65 20 63 6f 6e 73 74 61 * Add the consta
1cf47 6e 74 20 50 32 20 74 6f 20 74 68 65 20 76 61 6c nt P2 to the val
1cf48 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1cf49 31 2e 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 1..** The result
1cf4a 20 69 73 20 61 6c 77 61 79 73 20 61 6e 20 69 6e is always an in
1cf4b 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 teger..**.** To
1cf4c 66 6f 72 63 65 20 61 6e 79 20 72 65 67 69 73 74 force any regist
1cf4d 65 72 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 er to be an inte
1cf4e 67 65 72 2c 20 6a 75 73 74 20 61 64 64 20 30 2e ger, just add 0.
1cf4f 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64 64 49 .*/.case OP_AddI
1cf50 6d 6d 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 mm: {
1cf51 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 73 71 6c /* in1 */. sql
1cf52 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 ite3VdbeMemInteg
1cf53 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 70 erify(pIn1);. p
1cf54 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 2d In1->u.i += pOp-
1cf55 3e 70 32 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a >p2;. break;.}.
1cf56 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 73 74 ./* Opcode: Must
1cf57 42 65 49 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 BeInt P1 P2 * *
1cf58 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f 72 63 65 20 74 *.** .** Force t
1cf59 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1cf5a 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 61 6e ster P1 to be an
1cf5b 20 69 6e 74 65 67 65 72 2e 20 20 49 66 20 74 68 integer. If th
1cf5c 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 e value.** in P1
1cf5d 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 is not an integ
1cf5e 65 72 20 61 6e 64 20 63 61 6e 6e 6f 74 20 62 65 er and cannot be
1cf5f 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 converted into
1cf60 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 77 69 an integer.** wi
1cf61 74 68 6f 75 74 20 64 61 74 61 20 6c 6f 73 73 2c thout data loss,
1cf62 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 then jump immed
1cf63 69 61 74 65 6c 79 20 74 6f 20 50 32 2c 20 6f 72 iately to P2, or
1cf64 20 69 66 20 50 32 3d 3d 30 0a 2a 2a 20 72 61 69 if P2==0.** rai
1cf65 73 65 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 se an SQLITE_MIS
1cf66 4d 41 54 43 48 20 65 78 63 65 70 74 69 6f 6e 2e MATCH exception.
1cf67 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 73 74 .*/.case OP_Must
1cf68 42 65 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 BeInt: {
1cf69 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 /* jump, in1
1cf6a 20 2a 2f 0a 20 20 61 70 70 6c 79 41 66 66 69 6e */. applyAffin
1cf6b 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 ity(pIn1, SQLITE
1cf6c 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c 20 65 6e _AFF_NUMERIC, en
1cf6d 63 6f 64 69 6e 67 29 3b 0a 20 20 69 66 28 20 28 coding);. if( (
1cf6e 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1cf6f 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20 M_Int)==0 ){.
1cf70 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 30 20 if( pOp->p2==0
1cf71 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
1cf72 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 3b 0a 20 LITE_MISMATCH;.
1cf73 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f goto abort_
1cf74 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
1cf75 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
1cf76 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1cf77 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
1cf78 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c MemSetTypeFl
1cf79 61 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 49 6e 74 ag(pIn1, MEM_Int
1cf7a 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a );. }. break;.
1cf7b 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 }../* Opcode: Re
1cf7c 61 6c 41 66 66 69 6e 69 74 79 20 50 31 20 2a 20 alAffinity P1 *
1cf7d 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 72 * * *.**.** If r
1cf7e 65 67 69 73 74 65 72 20 50 31 20 68 6f 6c 64 73 egister P1 holds
1cf7f 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6e 76 an integer conv
1cf80 65 72 74 20 69 74 20 74 6f 20 61 20 72 65 61 6c ert it to a real
1cf81 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 value..**.** Th
1cf82 69 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 is opcode is use
1cf83 64 20 77 68 65 6e 20 65 78 74 72 61 63 74 69 6e d when extractin
1cf84 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 g information fr
1cf85 6f 6d 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 om a column that
1cf86 0a 2a 2a 20 68 61 73 20 52 45 41 4c 20 61 66 66 .** has REAL aff
1cf87 69 6e 69 74 79 2e 20 20 53 75 63 68 20 63 6f 6c inity. Such col
1cf88 75 6d 6e 20 76 61 6c 75 65 73 20 6d 61 79 20 73 umn values may s
1cf89 74 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 61 till be stored a
1cf8a 73 0a 2a 2a 20 69 6e 74 65 67 65 72 73 2c 20 66 s.** integers, f
1cf8b 6f 72 20 73 70 61 63 65 20 65 66 66 69 63 69 65 or space efficie
1cf8c 6e 63 79 2c 20 62 75 74 20 61 66 74 65 72 20 65 ncy, but after e
1cf8d 78 74 72 61 63 74 69 6f 6e 20 77 65 20 77 61 6e xtraction we wan
1cf8e 74 20 74 68 65 6d 0a 2a 2a 20 74 6f 20 68 61 76 t them.** to hav
1cf8f 65 20 6f 6e 6c 79 20 61 20 72 65 61 6c 20 76 61 e only a real va
1cf90 6c 75 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f lue..*/.case OP_
1cf91 52 65 61 6c 41 66 66 69 6e 69 74 79 3a 20 7b 20 RealAffinity: {
1cf92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cf93 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 /* in1 */. if(
1cf94 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d pIn1->flags & M
1cf95 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 73 71 EM_Int ){. sq
1cf96 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c lite3VdbeMemReal
1cf97 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 ify(pIn1);. }.
1cf98 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 break;.}..#ifnd
1cf99 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 ef SQLITE_OMIT_C
1cf9a 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 AST./* Opcode: T
1cf9b 6f 54 65 78 74 20 50 31 20 2a 20 2a 20 2a 20 2a oText P1 * * * *
1cf9c 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 .**.** Force the
1cf9d 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1cf9e 65 72 20 50 31 20 74 6f 20 62 65 20 74 65 78 74 er P1 to be text
1cf9f 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 ..** If the valu
1cfa0 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63 6f e is numeric, co
1cfa1 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 73 74 nvert it to a st
1cfa2 72 69 6e 67 20 75 73 69 6e 67 20 74 68 65 0a 2a ring using the.*
1cfa3 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 * equivalent of
1cfa4 70 72 69 6e 74 66 28 29 2e 20 20 42 6c 6f 62 20 printf(). Blob
1cfa5 76 61 6c 75 65 73 20 61 72 65 20 75 6e 63 68 61 values are uncha
1cfa6 6e 67 65 64 20 61 6e 64 0a 2a 2a 20 61 72 65 20 nged and.** are
1cfa7 61 66 74 65 72 77 61 72 64 73 20 73 69 6d 70 6c afterwards simpl
1cfa8 79 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 y interpreted as
1cfa9 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e text..**.** A N
1cfaa 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 ULL value is not
1cfab 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 changed by this
1cfac 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 routine. It re
1cfad 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 mains NULL..*/.c
1cfae 61 73 65 20 4f 50 5f 54 6f 54 65 78 74 3a 20 7b ase OP_ToText: {
1cfaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cfb0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1cfb1 54 4f 5f 54 45 58 54 2c 20 69 6e 31 20 2a 2f 0a TO_TEXT, in1 */.
1cfb2 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 if( pIn1->flag
1cfb3 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62 s & MEM_Null ) b
1cfb4 72 65 61 6b 3b 0a 20 20 61 73 73 65 72 74 28 20 reak;. assert(
1cfb5 4d 45 4d 5f 53 74 72 3d 3d 28 4d 45 4d 5f 42 6c MEM_Str==(MEM_Bl
1cfb6 6f 62 3e 3e 33 29 20 29 3b 0a 20 20 70 49 6e 31 ob>>3) );. pIn1
1cfb7 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 49 6e 31 ->flags |= (pIn1
1cfb8 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 ->flags&MEM_Blob
1cfb9 29 3e 3e 33 3b 0a 20 20 61 70 70 6c 79 41 66 66 )>>3;. applyAff
1cfba 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 inity(pIn1, SQLI
1cfbb 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 65 6e 63 TE_AFF_TEXT, enc
1cfbc 6f 64 69 6e 67 29 3b 0a 20 20 72 63 20 3d 20 45 oding);. rc = E
1cfbd 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 3b xpandBlob(pIn1);
1cfbe 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d . assert( pIn1-
1cfbf 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 >flags & MEM_Str
1cfc0 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 || db->mallocFa
1cfc1 69 6c 65 64 20 29 3b 0a 20 20 70 49 6e 31 2d 3e iled );. pIn1->
1cfc2 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 49 flags &= ~(MEM_I
1cfc3 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f nt|MEM_Real|MEM_
1cfc4 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 29 3b 0a Blob|MEM_Zero);.
1cfc5 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
1cfc6 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 62 BSIZE(pIn1);. b
1cfc7 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1cfc8 64 65 3a 20 54 6f 42 6c 6f 62 20 50 31 20 2a 20 de: ToBlob P1 *
1cfc9 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 * * *.**.** Forc
1cfca 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 e the value in r
1cfcb 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 egister P1 to be
1cfcc 20 61 20 42 4c 4f 42 2e 0a 2a 2a 20 49 66 20 74 a BLOB..** If t
1cfcd 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75 6d 65 he value is nume
1cfce 72 69 63 2c 20 63 6f 6e 76 65 72 74 20 69 74 20 ric, convert it
1cfcf 74 6f 20 61 20 73 74 72 69 6e 67 20 66 69 72 73 to a string firs
1cfd0 74 2e 0a 2a 2a 20 53 74 72 69 6e 67 73 20 61 72 t..** Strings ar
1cfd1 65 20 73 69 6d 70 6c 79 20 72 65 69 6e 74 65 72 e simply reinter
1cfd2 70 72 65 74 65 64 20 61 73 20 62 6c 6f 62 73 20 preted as blobs
1cfd3 77 69 74 68 20 6e 6f 20 63 68 61 6e 67 65 0a 2a with no change.*
1cfd4 2a 20 74 6f 20 74 68 65 20 75 6e 64 65 72 6c 79 * to the underly
1cfd5 69 6e 67 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 ing data..**.**
1cfd6 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 A NULL value is
1cfd7 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 not changed by t
1cfd8 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 his routine. It
1cfd9 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a remains NULL..*
1cfda 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 42 6c 6f 62 /.case OP_ToBlob
1cfdb 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 : {
1cfdc 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1cfdd 54 4b 5f 54 4f 5f 42 4c 4f 42 2c 20 69 6e 31 20 TK_TO_BLOB, in1
1cfde 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 */. if( pIn1->f
1cfdf 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 lags & MEM_Null
1cfe0 29 20 62 72 65 61 6b 3b 0a 20 20 69 66 28 20 28 ) break;. if( (
1cfe1 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1cfe2 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 7b 0a 20 20 M_Blob)==0 ){.
1cfe3 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 applyAffinity(
1cfe4 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 pIn1, SQLITE_AFF
1cfe5 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29 _TEXT, encoding)
1cfe6 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 ;. assert( pI
1cfe7 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n1->flags & MEM_
1cfe8 53 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f Str || db->mallo
1cfe9 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 4d cFailed );. M
1cfea 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 49 emSetTypeFlag(pI
1cfeb 6e 31 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 n1, MEM_Blob);.
1cfec 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 31 }else{. pIn1
1cfed 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d ->flags &= ~(MEM
1cfee 5f 54 79 70 65 4d 61 73 6b 26 7e 4d 45 4d 5f 42 _TypeMask&~MEM_B
1cfef 6c 6f 62 29 3b 0a 20 20 7d 0a 20 20 55 50 44 41 lob);. }. UPDA
1cff0 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 TE_MAX_BLOBSIZE(
1cff1 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b 0a pIn1);. break;.
1cff2 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f }../* Opcode: To
1cff3 4e 75 6d 65 72 69 63 20 50 31 20 2a 20 2a 20 2a Numeric P1 * * *
1cff4 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 *.**.** Force t
1cff5 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1cff6 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 6e 75 ster P1 to be nu
1cff7 6d 65 72 69 63 20 28 65 69 74 68 65 72 20 61 6e meric (either an
1cff8 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6f 72 20 61 .** integer or a
1cff9 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 floating-point
1cffa 6e 75 6d 62 65 72 2e 29 0a 2a 2a 20 49 66 20 74 number.).** If t
1cffb 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 he value is text
1cffc 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f or blob, try to
1cffd 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 convert it to a
1cffe 6e 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 n using the.** e
1cfff 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f quivalent of ato
1d000 69 28 29 20 6f 72 20 61 74 6f 66 28 29 20 61 6e i() or atof() an
1d001 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e 6f 20 d store 0 if no
1d002 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 such conversion
1d003 0a 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 2e .** is possible.
1d004 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 .**.** A NULL va
1d005 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 lue is not chang
1d006 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 ed by this routi
1d007 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 ne. It remains
1d008 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 NULL..*/.case OP
1d009 5f 54 6f 4e 75 6d 65 72 69 63 3a 20 7b 20 20 20 _ToNumeric: {
1d00a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1d00b 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f * same as TK_TO_
1d00c 4e 55 4d 45 52 49 43 2c 20 69 6e 31 20 2a 2f 0a NUMERIC, in1 */.
1d00d 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 if( (pIn1->fla
1d00e 67 73 20 26 20 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d gs & (MEM_Null|M
1d00f 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 EM_Int|MEM_Real)
1d010 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 )==0 ){. sqli
1d011 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 te3VdbeMemNumeri
1d012 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 fy(pIn1);. }.
1d013 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
1d014 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 /* SQLITE_OMIT_C
1d015 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 AST */../* Opcod
1d016 65 3a 20 54 6f 49 6e 74 20 50 31 20 2a 20 2a 20 e: ToInt P1 * *
1d017 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 * *.**.** Force
1d018 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1d019 69 73 74 65 72 20 50 31 20 62 65 20 61 6e 20 69 ister P1 be an i
1d01a 6e 74 65 67 65 72 2e 20 20 49 66 0a 2a 2a 20 54 nteger. If.** T
1d01b 68 65 20 76 61 6c 75 65 20 69 73 20 63 75 72 72 he value is curr
1d01c 65 6e 74 6c 79 20 61 20 72 65 61 6c 20 6e 75 6d ently a real num
1d01d 62 65 72 2c 20 64 72 6f 70 20 69 74 73 20 66 72 ber, drop its fr
1d01e 61 63 74 69 6f 6e 61 6c 20 70 61 72 74 2e 0a 2a actional part..*
1d01f 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 * If the value i
1d020 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 s text or blob,
1d021 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 try to convert i
1d022 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 t to an integer
1d023 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 using the.** equ
1d024 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 28 ivalent of atoi(
1d025 29 20 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66 ) and store 0 if
1d026 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73 no such convers
1d027 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 2e ion is possible.
1d028 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 .**.** A NULL va
1d029 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 lue is not chang
1d02a 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 ed by this routi
1d02b 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 ne. It remains
1d02c 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 NULL..*/.case OP
1d02d 5f 54 6f 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 _ToInt: {
1d02e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1d02f 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 2c me as TK_TO_INT,
1d030 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 in1 */. if( (p
1d031 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
1d032 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 _Null)==0 ){.
1d033 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 sqlite3VdbeMemI
1d034 6e 74 65 67 65 72 69 66 79 28 70 49 6e 31 29 3b ntegerify(pIn1);
1d035 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1d036 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1d037 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a 20 4f 70 63 OMIT_CAST./* Opc
1d038 6f 64 65 3a 20 54 6f 52 65 61 6c 20 50 31 20 2a ode: ToReal P1 *
1d039 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 * * *.**.** For
1d03a 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 ce the value in
1d03b 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 register P1 to b
1d03c 65 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 e a floating poi
1d03d 6e 74 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 49 66 nt number..** If
1d03e 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 63 75 The value is cu
1d03f 72 72 65 6e 74 6c 79 20 61 6e 20 69 6e 74 65 67 rrently an integ
1d040 65 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 2e 0a er, convert it..
1d041 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 ** If the value
1d042 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c is text or blob,
1d043 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 try to convert
1d044 69 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 it to an integer
1d045 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 using the.** eq
1d046 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 uivalent of atoi
1d047 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 2e 30 () and store 0.0
1d048 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 if no such conv
1d049 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 ersion is possib
1d04a 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c le..**.** A NULL
1d04b 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 value is not ch
1d04c 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f anged by this ro
1d04d 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 utine. It remai
1d04e 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 ns NULL..*/.case
1d04f 20 4f 50 5f 54 6f 52 65 61 6c 3a 20 7b 20 20 20 OP_ToReal: {
1d050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1d051 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f * same as TK_TO_
1d052 52 45 41 4c 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 REAL, in1 */. i
1d053 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 f( (pIn1->flags
1d054 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 & MEM_Null)==0 )
1d055 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1d056 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 6e 31 eMemRealify(pIn1
1d057 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a );. }. break;.
1d058 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1d059 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a TE_OMIT_CAST */.
1d05a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 74 20 50 ./* Opcode: Lt P
1d05b 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 1 P2 P3 P4 P5.**
1d05c 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 .** Compare the
1d05d 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 values in regist
1d05e 65 72 20 50 31 20 61 6e 64 20 50 33 2e 20 20 49 er P1 and P3. I
1d05f 66 20 72 65 67 28 50 33 29 3c 72 65 67 28 50 31 f reg(P3)<reg(P1
1d060 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 74 ) then.** jump t
1d061 6f 20 61 64 64 72 65 73 73 20 50 32 2e 20 20 0a o address P2. .
1d062 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c **.** If the SQL
1d063 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62 ITE_JUMPIFNULL b
1d064 69 74 20 6f 66 20 50 35 20 69 73 20 73 65 74 20 it of P5 is set
1d065 61 6e 64 20 65 69 74 68 65 72 20 72 65 67 28 50 and either reg(P
1d066 31 29 20 6f 72 0a 2a 2a 20 72 65 67 28 50 33 29 1) or.** reg(P3)
1d067 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 is NULL then ta
1d068 6b 65 20 74 68 65 20 6a 75 6d 70 2e 20 20 49 66 ke the jump. If
1d069 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 the SQLITE_JUMP
1d06a 49 46 4e 55 4c 4c 20 0a 2a 2a 20 62 69 74 20 69 IFNULL .** bit i
1d06b 73 20 63 6c 65 61 72 20 74 68 65 6e 20 66 61 6c s clear then fal
1d06c 6c 20 74 68 72 75 20 69 66 20 65 69 74 68 65 72 l thru if either
1d06d 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c operand is NULL
1d06e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 ..**.** The SQLI
1d06f 54 45 5f 41 46 46 5f 4d 41 53 4b 20 70 6f 72 74 TE_AFF_MASK port
1d070 69 6f 6e 20 6f 66 20 50 35 20 6d 75 73 74 20 62 ion of P5 must b
1d071 65 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 e an affinity ch
1d072 61 72 61 63 74 65 72 20 2d 0a 2a 2a 20 53 51 4c aracter -.** SQL
1d073 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 53 51 ITE_AFF_TEXT, SQ
1d074 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 LITE_AFF_INTEGER
1d075 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 , and so forth.
1d076 41 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 An attempt is ma
1d077 64 65 20 0a 2a 2a 20 74 6f 20 63 6f 65 72 63 65 de .** to coerce
1d078 20 62 6f 74 68 20 69 6e 70 75 74 73 20 61 63 63 both inputs acc
1d079 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20 61 ording to this a
1d07a 66 66 69 6e 69 74 79 20 62 65 66 6f 72 65 20 74 ffinity before t
1d07b 68 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e he.** comparison
1d07c 20 69 73 20 6d 61 64 65 2e 20 49 66 20 74 68 65 is made. If the
1d07d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b SQLITE_AFF_MASK
1d07e 20 69 73 20 30 78 30 30 2c 20 74 68 65 6e 20 6e is 0x00, then n
1d07f 75 6d 65 72 69 63 0a 2a 2a 20 61 66 66 69 6e 69 umeric.** affini
1d080 74 79 20 69 73 20 75 73 65 64 2e 20 4e 6f 74 65 ty is used. Note
1d081 20 74 68 61 74 20 74 68 65 20 61 66 66 69 6e 69 that the affini
1d082 74 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 ty conversions a
1d083 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 62 61 63 re stored.** bac
1d084 6b 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 75 74 k into the input
1d085 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e registers P1 an
1d086 64 20 50 33 2e 20 20 53 6f 20 74 68 69 73 20 6f d P3. So this o
1d087 70 63 6f 64 65 20 63 61 6e 20 63 61 75 73 65 0a pcode can cause.
1d088 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 20 63 68 ** persistent ch
1d089 61 6e 67 65 73 20 74 6f 20 72 65 67 69 73 74 65 anges to registe
1d08a 72 73 20 50 31 20 61 6e 64 20 50 33 2e 0a 2a 2a rs P1 and P3..**
1d08b 0a 2a 2a 20 4f 6e 63 65 20 61 6e 79 20 63 6f 6e .** Once any con
1d08c 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 74 61 versions have ta
1d08d 6b 65 6e 20 70 6c 61 63 65 2c 20 61 6e 64 20 6e ken place, and n
1d08e 65 69 74 68 65 72 20 76 61 6c 75 65 20 69 73 20 either value is
1d08f 4e 55 4c 4c 2c 20 0a 2a 2a 20 74 68 65 20 76 61 NULL, .** the va
1d090 6c 75 65 73 20 61 72 65 20 63 6f 6d 70 61 72 65 lues are compare
1d091 64 2e 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 d. If both value
1d092 73 20 61 72 65 20 62 6c 6f 62 73 20 74 68 65 6e s are blobs then
1d093 20 6d 65 6d 63 6d 70 28 29 20 69 73 0a 2a 2a 20 memcmp() is.**
1d094 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e used to determin
1d095 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 e the results of
1d096 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e the comparison.
1d097 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 If both values
1d098 0a 2a 2a 20 61 72 65 20 74 65 78 74 2c 20 74 68 .** are text, th
1d099 65 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 en the appropria
1d09a 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e te collating fun
1d09b 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 ction specified
1d09c 69 6e 0a 2a 2a 20 50 34 20 69 73 20 20 75 73 65 in.** P4 is use
1d09d 64 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70 d to do the comp
1d09e 61 72 69 73 6f 6e 2e 20 20 49 66 20 50 34 20 69 arison. If P4 i
1d09f 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20 s not specified
1d0a0 74 68 65 6e 0a 2a 2a 20 6d 65 6d 63 6d 70 28 29 then.** memcmp()
1d0a1 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 is used to comp
1d0a2 61 72 65 20 74 65 78 74 20 73 74 72 69 6e 67 2e are text string.
1d0a3 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 If both values
1d0a4 20 61 72 65 0a 2a 2a 20 6e 75 6d 65 72 69 63 2c are.** numeric,
1d0a5 20 74 68 65 6e 20 61 20 6e 75 6d 65 72 69 63 20 then a numeric
1d0a6 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 75 73 comparison is us
1d0a7 65 64 2e 20 49 66 20 74 68 65 20 74 77 6f 20 76 ed. If the two v
1d0a8 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 6f 66 20 alues.** are of
1d0a9 64 69 66 66 65 72 65 6e 74 20 74 79 70 65 73 2c different types,
1d0aa 20 74 68 65 6e 20 6e 75 6d 62 65 72 73 20 61 72 then numbers ar
1d0ab 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73 e considered les
1d0ac 73 20 74 68 61 6e 0a 2a 2a 20 73 74 72 69 6e 67 s than.** string
1d0ad 73 20 61 6e 64 20 73 74 72 69 6e 67 73 20 61 72 s and strings ar
1d0ae 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73 e considered les
1d0af 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2a s than blobs..**
1d0b0 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 .** If the SQLIT
1d0b1 45 5f 53 54 4f 52 45 50 32 20 62 69 74 20 6f 66 E_STOREP2 bit of
1d0b2 20 50 35 20 69 73 20 73 65 74 2c 20 74 68 65 6e P5 is set, then
1d0b3 20 64 6f 20 6e 6f 74 20 6a 75 6d 70 2e 20 20 49 do not jump. I
1d0b4 6e 73 74 65 61 64 2c 0a 2a 2a 20 73 74 6f 72 65 nstead,.** store
1d0b5 20 61 20 62 6f 6f 6c 65 61 6e 20 72 65 73 75 6c a boolean resul
1d0b6 74 20 28 65 69 74 68 65 72 20 30 2c 20 6f 72 20 t (either 0, or
1d0b7 31 2c 20 6f 72 20 4e 55 4c 4c 29 20 69 6e 20 72 1, or NULL) in r
1d0b8 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 2f egister P2..*/./
1d0b9 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 20 50 31 20 * Opcode: Ne P1
1d0ba 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a P2 P3 P4 P5.**.*
1d0bb 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 * This works jus
1d0bc 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 t like the Lt op
1d0bd 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74 code except that
1d0be 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b the jump is tak
1d0bf 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65 en if.** the ope
1d0c0 72 61 6e 64 73 20 69 6e 20 72 65 67 69 73 74 65 rands in registe
1d0c1 72 73 20 50 31 20 61 6e 64 20 50 33 20 61 72 65 rs P1 and P3 are
1d0c2 20 6e 6f 74 20 65 71 75 61 6c 2e 20 20 53 65 65 not equal. See
1d0c3 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 the Lt opcode f
1d0c4 6f 72 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c or.** additional
1d0c5 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a information..**
1d0c6 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e 55 .** If SQLITE_NU
1d0c7 4c 4c 45 51 20 69 73 20 73 65 74 20 69 6e 20 50 LLEQ is set in P
1d0c8 35 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 5 then the resul
1d0c9 74 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e 20 t of comparison
1d0ca 69 73 20 61 6c 77 61 79 73 20 65 69 74 68 65 72 is always either
1d0cb 0a 2a 2a 20 74 72 75 65 20 6f 72 20 66 61 6c 73 .** true or fals
1d0cc 65 20 61 6e 64 20 69 73 20 6e 65 76 65 72 20 4e e and is never N
1d0cd 55 4c 4c 2e 20 20 49 66 20 62 6f 74 68 20 6f 70 ULL. If both op
1d0ce 65 72 61 6e 64 73 20 61 72 65 20 4e 55 4c 4c 20 erands are NULL
1d0cf 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 0a then the result.
1d0d0 2a 2a 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e ** of comparison
1d0d1 20 69 73 20 66 61 6c 73 65 2e 20 20 49 66 20 65 is false. If e
1d0d2 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 ither operand is
1d0d3 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72 NULL then the r
1d0d4 65 73 75 6c 74 20 69 73 20 74 72 75 65 2e 0a 2a esult is true..*
1d0d5 2a 20 49 66 20 6e 65 69 74 68 65 72 20 6f 70 65 * If neither ope
1d0d6 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 65 rand is NULL the
1d0d7 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 74 the result is t
1d0d8 68 65 20 73 61 6d 65 20 61 73 20 69 74 20 77 6f he same as it wo
1d0d9 75 6c 64 20 62 65 20 69 66 0a 2a 2a 20 74 68 65 uld be if.** the
1d0da 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 66 SQLITE_NULLEQ f
1d0db 6c 61 67 20 77 65 72 65 20 6f 6d 69 74 74 65 64 lag were omitted
1d0dc 20 66 72 6f 6d 20 50 35 2e 0a 2a 2f 0a 2f 2a 20 from P5..*/./*
1d0dd 4f 70 63 6f 64 65 3a 20 45 71 20 50 31 20 50 32 Opcode: Eq P1 P2
1d0de 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 P3 P4 P5.**.**
1d0df 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 This works just
1d0e0 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f like the Lt opco
1d0e1 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74 de except that t
1d0e2 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e he jump is taken
1d0e3 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 if.** the opera
1d0e4 6e 64 73 20 69 6e 20 72 65 67 69 73 74 65 72 73 nds in registers
1d0e5 20 50 31 20 61 6e 64 20 50 33 20 61 72 65 20 65 P1 and P3 are e
1d0e6 71 75 61 6c 2e 0a 2a 2a 20 53 65 65 20 74 68 65 qual..** See the
1d0e7 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 Lt opcode for a
1d0e8 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
1d0e9 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ation..**.** If
1d0ea 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 69 73 SQLITE_NULLEQ is
1d0eb 20 73 65 74 20 69 6e 20 50 35 20 74 68 65 6e 20 set in P5 then
1d0ec 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 63 6f the result of co
1d0ed 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 6c 77 61 mparison is alwa
1d0ee 79 73 20 65 69 74 68 65 72 0a 2a 2a 20 74 72 75 ys either.** tru
1d0ef 65 20 6f 72 20 66 61 6c 73 65 20 61 6e 64 20 69 e or false and i
1d0f0 73 20 6e 65 76 65 72 20 4e 55 4c 4c 2e 20 20 49 s never NULL. I
1d0f1 66 20 62 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 f both operands
1d0f2 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 are NULL then th
1d0f3 65 20 72 65 73 75 6c 74 0a 2a 2a 20 6f 66 20 63 e result.** of c
1d0f4 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 72 75 omparison is tru
1d0f5 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 70 e. If either op
1d0f6 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 erand is NULL th
1d0f7 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 en the result is
1d0f8 20 66 61 6c 73 65 2e 0a 2a 2a 20 49 66 20 6e 65 false..** If ne
1d0f9 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 ither operand is
1d0fa 20 4e 55 4c 4c 20 74 68 65 20 74 68 65 20 72 65 NULL the the re
1d0fb 73 75 6c 74 20 69 73 20 74 68 65 20 73 61 6d 65 sult is the same
1d0fc 20 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20 as it would be
1d0fd 69 66 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 if.** the SQLITE
1d0fe 5f 4e 55 4c 4c 45 51 20 66 6c 61 67 20 77 65 72 _NULLEQ flag wer
1d0ff 65 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 50 e omitted from P
1d100 35 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 5..*/./* Opcode:
1d101 20 4c 65 20 50 31 20 50 32 20 50 33 20 50 34 20 Le P1 P2 P3 P4
1d102 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f P5.**.** This wo
1d103 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 rks just like th
1d104 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 e Lt opcode exce
1d105 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 pt that the jump
1d106 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 is taken if.**
1d107 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 the content of r
1d108 65 67 69 73 74 65 72 20 50 33 20 69 73 20 6c 65 egister P3 is le
1d109 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c ss than or equal
1d10a 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 to the content
1d10b 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 of.** register P
1d10c 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 1. See the Lt o
1d10d 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 pcode for additi
1d10e 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
1d10f 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1d110 47 74 20 50 31 20 50 32 20 50 33 20 50 34 20 50 Gt P1 P2 P3 P4 P
1d111 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 5.**.** This wor
1d112 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 ks just like the
1d113 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 Lt opcode excep
1d114 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 t that the jump
1d115 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 is taken if.** t
1d116 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 he content of re
1d117 67 69 73 74 65 72 20 50 33 20 69 73 20 67 72 65 gister P3 is gre
1d118 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 6f ater than the co
1d119 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 ntent of.** regi
1d11a 73 74 65 72 20 50 31 2e 20 20 53 65 65 20 74 68 ster P1. See th
1d11b 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 e Lt opcode for
1d11c 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 additional infor
1d11d 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 mation..*/./* Op
1d11e 63 6f 64 65 3a 20 47 65 20 50 31 20 50 32 20 50 code: Ge P1 P2 P
1d11f 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 3 P4 P5.**.** Th
1d120 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 is works just li
1d121 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 ke the Lt opcode
1d122 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 except that the
1d123 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 jump is taken i
1d124 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 f.** the content
1d125 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 of register P3
1d126 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
1d127 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 or equal to the
1d128 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 content of.** re
1d129 67 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 20 gister P1. See
1d12a 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f the Lt opcode fo
1d12b 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 r additional inf
1d12c 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 ormation..*/.cas
1d12d 65 20 4f 50 5f 45 71 3a 20 20 20 20 20 20 20 20 e OP_Eq:
1d12e 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1d12f 73 20 54 4b 5f 45 51 2c 20 6a 75 6d 70 2c 20 69 s TK_EQ, jump, i
1d130 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 n1, in3 */.case
1d131 4f 50 5f 4e 65 3a 20 20 20 20 20 20 20 20 20 20 OP_Ne:
1d132 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1d133 54 4b 5f 4e 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 TK_NE, jump, in1
1d134 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 , in3 */.case OP
1d135 5f 4c 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 _Lt:
1d136 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1d137 5f 4c 54 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 _LT, jump, in1,
1d138 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c in3 */.case OP_L
1d139 65 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e:
1d13a 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c /* same as TK_L
1d13b 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e E, jump, in1, in
1d13c 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 74 3a 3 */.case OP_Gt:
1d13d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1d13e 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 2c * same as TK_GT,
1d13f 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 jump, in1, in3
1d140 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 65 3a 20 7b */.case OP_Ge: {
1d141 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1d142 73 61 6d 65 20 61 73 20 54 4b 5f 47 45 2c 20 6a same as TK_GE, j
1d143 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f ump, in1, in3 */
1d144 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1d145 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1d146 20 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a 20 20 into u.ai */.
1d147 69 6e 74 20 72 65 73 3b 20 20 20 20 20 20 20 20 int res;
1d148 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 /* Result of
1d149 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 the comparison
1d14a 6f 66 20 70 49 6e 31 20 61 67 61 69 6e 73 74 20 of pIn1 against
1d14b 70 49 6e 33 20 2a 2f 0a 20 20 63 68 61 72 20 61 pIn3 */. char a
1d14c 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a ffinity; /*
1d14d 20 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 Affinity to use
1d14e 20 66 6f 72 20 63 6f 6d 70 61 72 69 73 6f 6e 20 for comparison
1d14f 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 */.#endif /* loc
1d150 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1d151 65 64 20 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a ed into u.ai */.
1d152 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c . if( (pIn1->fl
1d153 61 67 73 20 7c 20 70 49 6e 33 2d 3e 66 6c 61 67 ags | pIn3->flag
1d154 73 29 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 s)&MEM_Null ){.
1d155 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 62 6f 74 /* One or bot
1d156 68 20 6f 70 65 72 61 6e 64 73 20 61 72 65 20 4e h operands are N
1d157 55 4c 4c 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 ULL */. if( p
1d158 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f Op->p5 & SQLITE_
1d159 4e 55 4c 4c 45 51 20 29 7b 0a 20 20 20 20 20 20 NULLEQ ){.
1d15a 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e 55 4c /* If SQLITE_NUL
1d15b 4c 45 51 20 69 73 20 73 65 74 20 28 77 68 69 63 LEQ is set (whic
1d15c 68 20 77 69 6c 6c 20 6f 6e 6c 79 20 68 61 70 70 h will only happ
1d15d 65 6e 20 69 66 20 74 68 65 20 6f 70 65 72 61 74 en if the operat
1d15e 6f 72 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 4f or is. ** O
1d15f 50 5f 45 71 20 6f 72 20 4f 50 5f 4e 65 29 20 74 P_Eq or OP_Ne) t
1d160 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d hen take the jum
1d161 70 20 6f 72 20 6e 6f 74 20 64 65 70 65 6e 64 69 p or not dependi
1d162 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 0a 20 20 ng on whether.
1d163 20 20 20 20 2a 2a 20 6f 72 20 6e 6f 74 20 62 6f ** or not bo
1d164 74 68 20 6f 70 65 72 61 6e 64 73 20 61 72 65 20 th operands are
1d165 6e 75 6c 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 null.. */.
1d166 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1d167 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 45 71 20 ->opcode==OP_Eq
1d168 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d || pOp->opcode==
1d169 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 75 OP_Ne );. u
1d16a 2e 61 69 2e 72 65 73 20 3d 20 28 70 49 6e 31 2d .ai.res = (pIn1-
1d16b 3e 66 6c 61 67 73 20 26 20 70 49 6e 33 2d 3e 66 >flags & pIn3->f
1d16c 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 lags & MEM_Null)
1d16d 3d 3d 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ==0;. }else{.
1d16e 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f /* SQLITE_
1d16f 4e 55 4c 4c 45 51 20 69 73 20 63 6c 65 61 72 20 NULLEQ is clear
1d170 61 6e 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 and at least one
1d171 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c operand is NULL
1d172 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 ,. ** then
1d173 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c the result is al
1d174 77 61 79 73 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 ways NULL..
1d175 20 2a 2a 20 54 68 65 20 6a 75 6d 70 20 69 73 20 ** The jump is
1d176 74 61 6b 65 6e 20 69 66 20 74 68 65 20 53 51 4c taken if the SQL
1d177 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62 ITE_JUMPIFNULL b
1d178 69 74 20 69 73 20 73 65 74 2e 0a 20 20 20 20 20 it is set..
1d179 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 4f */. if( pO
1d17a 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 53 p->p5 & SQLITE_S
1d17b 54 4f 52 45 50 32 20 29 7b 0a 20 20 20 20 20 20 TOREP2 ){.
1d17c 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 pOut = &p->aMe
1d17d 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 m[pOp->p2];.
1d17e 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c MemSetTypeFl
1d17f 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c ag(pOut, MEM_Nul
1d180 6c 29 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49 l);. REGI
1d181 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
1d182 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 20 p2, pOut);.
1d183 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e }else if( pOp->
1d184 70 35 20 26 20 53 51 4c 49 54 45 5f 4a 55 4d 50 p5 & SQLITE_JUMP
1d185 49 46 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 IFNULL ){.
1d186 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 pc = pOp->p2-1
1d187 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1d188 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d break;. }. }
1d189 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 else{. /* Nei
1d18a 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 ther operand is
1d18b 4e 55 4c 4c 2e 20 20 44 6f 20 61 20 63 6f 6d 70 NULL. Do a comp
1d18c 61 72 69 73 6f 6e 2e 20 2a 2f 0a 20 20 20 20 75 arison. */. u
1d18d 2e 61 69 2e 61 66 66 69 6e 69 74 79 20 3d 20 70 .ai.affinity = p
1d18e 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f Op->p5 & SQLITE_
1d18f 41 46 46 5f 4d 41 53 4b 3b 0a 20 20 20 20 69 66 AFF_MASK;. if
1d190 28 20 75 2e 61 69 2e 61 66 66 69 6e 69 74 79 20 ( u.ai.affinity
1d191 29 7b 0a 20 20 20 20 20 20 61 70 70 6c 79 41 66 ){. applyAf
1d192 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 75 2e 61 finity(pIn1, u.a
1d193 69 2e 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f i.affinity, enco
1d194 64 69 6e 67 29 3b 0a 20 20 20 20 20 20 61 70 70 ding);. app
1d195 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e 33 2c lyAffinity(pIn3,
1d196 20 75 2e 61 69 2e 61 66 66 69 6e 69 74 79 2c 20 u.ai.affinity,
1d197 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 20 encoding);.
1d198 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
1d199 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f ailed ) goto no_
1d19a 6d 65 6d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 mem;. }..
1d19b 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 assert( pOp->p4t
1d19c 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 ype==P4_COLLSEQ
1d19d 7c 7c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c || pOp->p4.pColl
1d19e 3d 3d 30 20 29 3b 0a 20 20 20 20 45 78 70 61 6e ==0 );. Expan
1d19f 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 20 dBlob(pIn1);.
1d1a0 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33 ExpandBlob(pIn3
1d1a1 29 3b 0a 20 20 20 20 75 2e 61 69 2e 72 65 73 20 );. u.ai.res
1d1a2 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 = sqlite3MemComp
1d1a3 61 72 65 28 70 49 6e 33 2c 20 70 49 6e 31 2c 20 are(pIn3, pIn1,
1d1a4 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 29 3b 0a pOp->p4.pColl);.
1d1a5 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20 70 4f }. switch( pO
1d1a6 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 p->opcode ){.
1d1a7 20 63 61 73 65 20 4f 50 5f 45 71 3a 20 20 20 20 case OP_Eq:
1d1a8 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e u.ai.res = u.ai.
1d1a9 72 65 73 3d 3d 30 3b 20 20 20 20 20 62 72 65 61 res==0; brea
1d1aa 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4e k;. case OP_N
1d1ab 65 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d e: u.ai.res =
1d1ac 20 75 2e 61 69 2e 72 65 73 21 3d 30 3b 20 20 20 u.ai.res!=0;
1d1ad 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 break;. cas
1d1ae 65 20 4f 50 5f 4c 74 3a 20 20 20 20 75 2e 61 69 e OP_Lt: u.ai
1d1af 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3c .res = u.ai.res<
1d1b0 30 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 0; break;.
1d1b1 20 20 20 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20 case OP_Le:
1d1b2 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 u.ai.res = u.a
1d1b3 69 2e 72 65 73 3c 3d 30 3b 20 20 20 20 20 62 72 i.res<=0; br
1d1b4 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 eak;. case OP
1d1b5 5f 47 74 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 _Gt: u.ai.res
1d1b6 20 3d 20 75 2e 61 69 2e 72 65 73 3e 30 3b 20 20 = u.ai.res>0;
1d1b7 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 break;. d
1d1b8 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20 75 2e efault: u.
1d1b9 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 ai.res = u.ai.re
1d1ba 73 3e 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b s>=0; break;
1d1bb 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4f 70 2d . }.. if( pOp-
1d1bc 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 53 54 4f >p5 & SQLITE_STO
1d1bd 52 45 50 32 20 29 7b 0a 20 20 20 20 70 4f 75 74 REP2 ){. pOut
1d1be 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1d1bf 3e 70 32 5d 3b 0a 20 20 20 20 4d 65 6d 53 65 74 >p2];. MemSet
1d1c0 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d TypeFlag(pOut, M
1d1c1 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 70 4f 75 EM_Int);. pOu
1d1c2 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 69 2e 72 65 t->u.i = u.ai.re
1d1c3 73 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f s;. REGISTER_
1d1c4 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 TRACE(pOp->p2, p
1d1c5 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 Out);. }else if
1d1c6 28 20 75 2e 61 69 2e 72 65 73 20 29 7b 0a 20 20 ( u.ai.res ){.
1d1c7 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 pc = pOp->p2-1
1d1c8 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1d1c9 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 65 72 ../* Opcode: Per
1d1ca 6d 75 74 61 74 69 6f 6e 20 2a 20 2a 20 2a 20 50 mutation * * * P
1d1cb 34 20 2a 0a 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 4 *.**.** Set th
1d1cc 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 75 73 e permutation us
1d1cd 65 64 20 62 79 20 74 68 65 20 4f 50 5f 43 6f 6d ed by the OP_Com
1d1ce 70 61 72 65 20 6f 70 65 72 61 74 6f 72 20 74 6f pare operator to
1d1cf 20 62 65 20 74 68 65 20 61 72 72 61 79 0a 2a 2a be the array.**
1d1d0 20 6f 66 20 69 6e 74 65 67 65 72 73 20 69 6e 20 of integers in
1d1d1 50 34 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 P4..**.** The pe
1d1d2 72 6d 75 74 61 74 69 6f 6e 20 69 73 20 6f 6e 6c rmutation is onl
1d1d3 79 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 y valid until th
1d1d4 65 20 6e 65 78 74 20 4f 50 5f 50 65 72 6d 75 74 e next OP_Permut
1d1d5 61 74 69 6f 6e 2c 20 4f 50 5f 43 6f 6d 70 61 72 ation, OP_Compar
1d1d6 65 2c 0a 2a 2a 20 4f 50 5f 48 61 6c 74 2c 20 6f e,.** OP_Halt, o
1d1d7 72 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 20 r OP_ResultRow.
1d1d8 20 54 79 70 69 63 61 6c 6c 79 20 74 68 65 20 4f Typically the O
1d1d9 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20 73 68 P_Permutation sh
1d1da 6f 75 6c 64 20 6f 63 63 75 72 0a 2a 2a 20 69 6d ould occur.** im
1d1db 6d 65 64 69 61 74 65 6c 79 20 70 72 69 6f 72 20 mediately prior
1d1dc 74 6f 20 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72 to the OP_Compar
1d1dd 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 65 e..*/.case OP_Pe
1d1de 72 6d 75 74 61 74 69 6f 6e 3a 20 7b 0a 20 20 61 rmutation: {. a
1d1df 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 ssert( pOp->p4ty
1d1e0 70 65 3d 3d 50 34 5f 49 4e 54 41 52 52 41 59 20 pe==P4_INTARRAY
1d1e1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1d1e2 2d 3e 70 34 2e 61 69 20 29 3b 0a 20 20 61 50 65 ->p4.ai );. aPe
1d1e3 72 6d 75 74 65 20 3d 20 70 4f 70 2d 3e 70 34 2e rmute = pOp->p4.
1d1e4 61 69 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a ai;. break;.}..
1d1e5 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6d 70 61 /* Opcode: Compa
1d1e6 72 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a re P1 P2 P3 P4 *
1d1e7 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 .**.** Compare t
1d1e8 6f 20 76 65 63 74 6f 72 73 20 6f 66 20 72 65 67 o vectors of reg
1d1e9 69 73 74 65 72 73 20 69 6e 20 72 65 67 28 50 31 isters in reg(P1
1d1ea 29 2e 2e 72 65 67 28 50 31 2b 50 33 2d 31 29 20 )..reg(P1+P3-1)
1d1eb 28 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 (all this.** one
1d1ec 20 22 41 22 29 20 61 6e 64 20 69 6e 20 72 65 67 "A") and in reg
1d1ed 28 50 32 29 2e 2e 72 65 67 28 50 32 2b 50 33 2d (P2)..reg(P2+P3-
1d1ee 31 29 20 28 22 42 22 29 2e 20 20 53 61 76 65 20 1) ("B"). Save
1d1ef 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a the result of.**
1d1f0 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 the comparison
1d1f1 66 6f 72 20 75 73 65 20 62 79 20 74 68 65 20 6e for use by the n
1d1f2 65 78 74 20 4f 50 5f 4a 75 6d 70 20 69 6e 73 74 ext OP_Jump inst
1d1f3 72 75 63 74 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 ruct..**.** P4 i
1d1f4 73 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 s a KeyInfo stru
1d1f5 63 74 75 72 65 20 74 68 61 74 20 64 65 66 69 6e cture that defin
1d1f6 65 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 es collating seq
1d1f7 75 65 6e 63 65 73 20 61 6e 64 20 73 6f 72 74 0a uences and sort.
1d1f8 2a 2a 20 6f 72 64 65 72 73 20 66 6f 72 20 74 68 ** orders for th
1d1f9 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54 e comparison. T
1d1fa 68 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 61 he permutation a
1d1fb 70 70 6c 69 65 73 20 74 6f 20 72 65 67 69 73 74 pplies to regist
1d1fc 65 72 73 0a 2a 2a 20 6f 6e 6c 79 2e 20 20 54 68 ers.** only. Th
1d1fd 65 20 4b 65 79 49 6e 66 6f 20 65 6c 65 6d 65 6e e KeyInfo elemen
1d1fe 74 73 20 61 72 65 20 75 73 65 64 20 73 65 71 75 ts are used sequ
1d1ff 65 6e 74 69 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 entially..**.**
1d200 54 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 The comparison i
1d201 73 20 61 20 73 6f 72 74 20 63 6f 6d 70 61 72 69 s a sort compari
1d202 73 6f 6e 2c 20 73 6f 20 4e 55 4c 4c 73 20 63 6f son, so NULLs co
1d203 6d 70 61 72 65 20 65 71 75 61 6c 2c 0a 2a 2a 20 mpare equal,.**
1d204 4e 55 4c 4c 73 20 61 72 65 20 6c 65 73 73 20 74 NULLs are less t
1d205 68 61 6e 20 6e 75 6d 62 65 72 73 2c 20 6e 75 6d han numbers, num
1d206 62 65 72 73 20 61 72 65 20 6c 65 73 73 20 74 68 bers are less th
1d207 61 6e 20 73 74 72 69 6e 67 73 2c 0a 2a 2a 20 61 an strings,.** a
1d208 6e 64 20 73 74 72 69 6e 67 73 20 61 72 65 20 6c nd strings are l
1d209 65 73 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a ess than blobs..
1d20a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6d 70 61 */.case OP_Compa
1d20b 72 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 re: {.#if 0 /*
1d20c 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1d20d 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6a 20 moved into u.aj
1d20e 2a 2f 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 69 6e */. int n;. in
1d20f 74 20 69 3b 0a 20 20 69 6e 74 20 70 31 3b 0a 20 t i;. int p1;.
1d210 20 69 6e 74 20 70 32 3b 0a 20 20 63 6f 6e 73 74 int p2;. const
1d211 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
1d212 66 6f 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 fo;. int idx;.
1d213 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b CollSeq *pColl;
1d214 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 /* Collating
1d215 20 73 65 71 75 65 6e 63 65 20 74 6f 20 75 73 65 sequence to use
1d216 20 6f 6e 20 74 68 69 73 20 74 65 72 6d 20 2a 2f on this term */
1d217 0a 20 20 69 6e 74 20 62 52 65 76 3b 20 20 20 20 . int bRev;
1d218 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f /* True fo
1d219 72 20 44 45 53 43 45 4e 44 49 4e 47 20 73 6f 72 r DESCENDING sor
1d21a 74 20 6f 72 64 65 72 20 2a 2f 0a 23 65 6e 64 69 t order */.#endi
1d21b 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1d21c 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1d21d 75 2e 61 6a 20 2a 2f 0a 0a 20 20 75 2e 61 6a 2e u.aj */.. u.aj.
1d21e 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 75 n = pOp->p3;. u
1d21f 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 .aj.pKeyInfo = p
1d220 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b Op->p4.pKeyInfo;
1d221 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6a 2e . assert( u.aj.
1d222 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 n>0 );. assert(
1d223 20 75 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 21 3d u.aj.pKeyInfo!=
1d224 30 20 29 3b 0a 20 20 75 2e 61 6a 2e 70 31 20 3d 0 );. u.aj.p1 =
1d225 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 6a pOp->p1;. u.aj
1d226 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23 .p2 = pOp->p2;.#
1d227 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a if SQLITE_DEBUG.
1d228 20 20 69 66 28 20 61 50 65 72 6d 75 74 65 20 29 if( aPermute )
1d229 7b 0a 20 20 20 20 69 6e 74 20 6b 2c 20 6d 78 20 {. int k, mx
1d22a 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 6b 3d 30 = 0;. for(k=0
1d22b 3b 20 6b 3c 75 2e 61 6a 2e 6e 3b 20 6b 2b 2b 29 ; k<u.aj.n; k++)
1d22c 20 69 66 28 20 61 50 65 72 6d 75 74 65 5b 6b 5d if( aPermute[k]
1d22d 3e 6d 78 20 29 20 6d 78 20 3d 20 61 50 65 72 6d >mx ) mx = aPerm
1d22e 75 74 65 5b 6b 5d 3b 0a 20 20 20 20 61 73 73 65 ute[k];. asse
1d22f 72 74 28 20 75 2e 61 6a 2e 70 31 3e 30 20 26 26 rt( u.aj.p1>0 &&
1d230 20 75 2e 61 6a 2e 70 31 2b 6d 78 3c 3d 70 2d 3e u.aj.p1+mx<=p->
1d231 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 20 20 61 73 nMem+1 );. as
1d232 73 65 72 74 28 20 75 2e 61 6a 2e 70 32 3e 30 20 sert( u.aj.p2>0
1d233 26 26 20 75 2e 61 6a 2e 70 32 2b 6d 78 3c 3d 70 && u.aj.p2+mx<=p
1d234 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 7d 65 ->nMem+1 );. }e
1d235 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 lse{. assert(
1d236 20 75 2e 61 6a 2e 70 31 3e 30 20 26 26 20 75 2e u.aj.p1>0 && u.
1d237 61 6a 2e 70 31 2b 75 2e 61 6a 2e 6e 3c 3d 70 2d aj.p1+u.aj.n<=p-
1d238 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 20 20 61 >nMem+1 );. a
1d239 73 73 65 72 74 28 20 75 2e 61 6a 2e 70 32 3e 30 ssert( u.aj.p2>0
1d23a 20 26 26 20 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a && u.aj.p2+u.aj
1d23b 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b .n<=p->nMem+1 );
1d23c 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 . }.#endif /* S
1d23d 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 20 QLITE_DEBUG */.
1d23e 20 66 6f 72 28 75 2e 61 6a 2e 69 3d 30 3b 20 75 for(u.aj.i=0; u
1d23f 2e 61 6a 2e 69 3c 75 2e 61 6a 2e 6e 3b 20 75 2e .aj.i<u.aj.n; u.
1d240 61 6a 2e 69 2b 2b 29 7b 0a 20 20 20 20 75 2e 61 aj.i++){. u.a
1d241 6a 2e 69 64 78 20 3d 20 61 50 65 72 6d 75 74 65 j.idx = aPermute
1d242 20 3f 20 61 50 65 72 6d 75 74 65 5b 75 2e 61 6a ? aPermute[u.aj
1d243 2e 69 5d 20 3a 20 75 2e 61 6a 2e 69 3b 0a 20 20 .i] : u.aj.i;.
1d244 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1d245 28 75 2e 61 6a 2e 70 31 2b 75 2e 61 6a 2e 69 64 (u.aj.p1+u.aj.id
1d246 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 6a x, &p->aMem[u.aj
1d247 2e 70 31 2b 75 2e 61 6a 2e 69 64 78 5d 29 3b 0a .p1+u.aj.idx]);.
1d248 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 REGISTER_TRA
1d249 43 45 28 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e CE(u.aj.p2+u.aj.
1d24a 69 64 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e idx, &p->aMem[u.
1d24b 61 6a 2e 70 32 2b 75 2e 61 6a 2e 69 64 78 5d 29 aj.p2+u.aj.idx])
1d24c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e ;. assert( u.
1d24d 61 6a 2e 69 3c 75 2e 61 6a 2e 70 4b 65 79 49 6e aj.i<u.aj.pKeyIn
1d24e 66 6f 2d 3e 6e 46 69 65 6c 64 20 29 3b 0a 20 20 fo->nField );.
1d24f 20 20 75 2e 61 6a 2e 70 43 6f 6c 6c 20 3d 20 75 u.aj.pColl = u
1d250 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 .aj.pKeyInfo->aC
1d251 6f 6c 6c 5b 75 2e 61 6a 2e 69 5d 3b 0a 20 20 20 oll[u.aj.i];.
1d252 20 75 2e 61 6a 2e 62 52 65 76 20 3d 20 75 2e 61 u.aj.bRev = u.a
1d253 6a 2e 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 j.pKeyInfo->aSor
1d254 74 4f 72 64 65 72 5b 75 2e 61 6a 2e 69 5d 3b 0a tOrder[u.aj.i];.
1d255 20 20 20 20 69 43 6f 6d 70 61 72 65 20 3d 20 73 iCompare = s
1d256 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 qlite3MemCompare
1d257 28 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 6a 2e 70 (&p->aMem[u.aj.p
1d258 31 2b 75 2e 61 6a 2e 69 64 78 5d 2c 20 26 70 2d 1+u.aj.idx], &p-
1d259 3e 61 4d 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e >aMem[u.aj.p2+u.
1d25a 61 6a 2e 69 64 78 5d 2c 20 75 2e 61 6a 2e 70 43 aj.idx], u.aj.pC
1d25b 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 43 oll);. if( iC
1d25c 6f 6d 70 61 72 65 20 29 7b 0a 20 20 20 20 20 20 ompare ){.
1d25d 69 66 28 20 75 2e 61 6a 2e 62 52 65 76 20 29 20 if( u.aj.bRev )
1d25e 69 43 6f 6d 70 61 72 65 20 3d 20 2d 69 43 6f 6d iCompare = -iCom
1d25f 70 61 72 65 3b 0a 20 20 20 20 20 20 62 72 65 61 pare;. brea
1d260 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 k;. }. }. a
1d261 50 65 72 6d 75 74 65 20 3d 20 30 3b 0a 20 20 62 Permute = 0;. b
1d262 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1d263 64 65 3a 20 4a 75 6d 70 20 50 31 20 50 32 20 50 de: Jump P1 P2 P
1d264 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 3 * *.**.** Jump
1d265 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74 to the instruct
1d266 69 6f 6e 20 61 74 20 61 64 64 72 65 73 73 20 50 ion at address P
1d267 31 2c 20 50 32 2c 20 6f 72 20 50 33 20 64 65 70 1, P2, or P3 dep
1d268 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 ending on whethe
1d269 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 6f 73 74 r.** in the most
1d26a 20 72 65 63 65 6e 74 20 4f 50 5f 43 6f 6d 70 61 recent OP_Compa
1d26b 72 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 re instruction t
1d26c 68 65 20 50 31 20 76 65 63 74 6f 72 20 77 61 73 he P1 vector was
1d26d 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 65 71 less than.** eq
1d26e 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 ual to, or great
1d26f 65 72 20 74 68 61 6e 20 74 68 65 20 50 32 20 76 er than the P2 v
1d270 65 63 74 6f 72 2c 20 72 65 73 70 65 63 74 69 76 ector, respectiv
1d271 65 6c 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ely..*/.case OP_
1d272 4a 75 6d 70 3a 20 7b 20 20 20 20 20 20 20 20 20 Jump: {
1d273 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 /* jump */.
1d274 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3c 30 20 if( iCompare<0
1d275 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d ){. pc = pOp-
1d276 3e 70 31 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 >p1 - 1;. }else
1d277 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3d 3d 30 if( iCompare==0
1d278 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 ){. pc = pOp
1d279 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 ->p2 - 1;. }els
1d27a 65 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d e{. pc = pOp-
1d27b 3e 70 33 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p3 - 1;. }. b
1d27c 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1d27d 64 65 3a 20 41 6e 64 20 50 31 20 50 32 20 50 33 de: And P1 P2 P3
1d27e 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 * *.**.** Take
1d27f 74 68 65 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20 the logical AND
1d280 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e of the values in
1d281 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e registers P1 an
1d282 64 20 50 32 20 61 6e 64 0a 2a 2a 20 77 72 69 74 d P2 and.** writ
1d283 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 e the result int
1d284 6f 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a o register P3..*
1d285 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50 *.** If either P
1d286 31 20 6f 72 20 50 32 20 69 73 20 30 20 28 66 61 1 or P2 is 0 (fa
1d287 6c 73 65 29 20 74 68 65 6e 20 74 68 65 20 72 65 lse) then the re
1d288 73 75 6c 74 20 69 73 20 30 20 65 76 65 6e 20 69 sult is 0 even i
1d289 66 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 69 f.** the other i
1d28a 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41 nput is NULL. A
1d28b 20 4e 55 4c 4c 20 61 6e 64 20 74 72 75 65 20 6f NULL and true o
1d28c 72 20 74 77 6f 20 4e 55 4c 4c 73 20 67 69 76 65 r two NULLs give
1d28d 0a 2a 2a 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 .** a NULL outpu
1d28e 74 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a t..*/./* Opcode:
1d28f 20 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a Or P1 P2 P3 * *
1d290 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 .**.** Take the
1d291 6c 6f 67 69 63 61 6c 20 4f 52 20 6f 66 20 74 68 logical OR of th
1d292 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 e values in regi
1d293 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61 ster P1 and P2 a
1d294 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 nd.** store the
1d295 61 6e 73 77 65 72 20 69 6e 20 72 65 67 69 73 74 answer in regist
1d296 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 er P3..**.** If
1d297 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20 either P1 or P2
1d298 69 73 20 6e 6f 6e 7a 65 72 6f 20 28 74 72 75 65 is nonzero (true
1d299 29 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c ) then the resul
1d29a 74 20 69 73 20 31 20 28 74 72 75 65 29 0a 2a 2a t is 1 (true).**
1d29b 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f 74 68 even if the oth
1d29c 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c er input is NULL
1d29d 2e 20 20 41 20 4e 55 4c 4c 20 61 6e 64 20 66 61 . A NULL and fa
1d29e 6c 73 65 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73 lse or two NULLs
1d29f 0a 2a 2a 20 67 69 76 65 20 61 20 4e 55 4c 4c 20 .** give a NULL
1d2a0 6f 75 74 70 75 74 2e 0a 2a 2f 0a 63 61 73 65 20 output..*/.case
1d2a1 4f 50 5f 41 6e 64 3a 20 20 20 20 20 20 20 20 20 OP_And:
1d2a2 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1d2a3 54 4b 5f 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 TK_AND, in1, in2
1d2a4 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f , out3 */.case O
1d2a5 50 5f 4f 72 3a 20 7b 20 20 20 20 20 20 20 20 20 P_Or: {
1d2a6 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1d2a7 4b 5f 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 K_OR, in1, in2,
1d2a8 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f out3 */.#if 0 /
1d2a9 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1d2aa 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1d2ab 6b 20 2a 2f 0a 20 20 69 6e 74 20 76 31 3b 20 20 k */. int v1;
1d2ac 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e /* Left operan
1d2ad 64 3a 20 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d d: 0==FALSE, 1=
1d2ae 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 =TRUE, 2==UNKNOW
1d2af 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 N or NULL */. i
1d2b0 6e 74 20 76 32 3b 20 20 20 20 2f 2a 20 52 69 67 nt v2; /* Rig
1d2b1 68 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d 3d 46 ht operand: 0==F
1d2b2 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 ALSE, 1==TRUE, 2
1d2b3 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c ==UNKNOWN or NUL
1d2b4 4c 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c L */.#endif /* l
1d2b5 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1d2b6 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6b 20 2a oved into u.ak *
1d2b7 2f 0a 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 /.. if( pIn1->f
1d2b8 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 lags & MEM_Null
1d2b9 29 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d ){. u.ak.v1 =
1d2ba 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 2;. }else{.
1d2bb 20 75 2e 61 6b 2e 76 31 20 3d 20 73 71 6c 69 74 u.ak.v1 = sqlit
1d2bc 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 e3VdbeIntValue(p
1d2bd 49 6e 31 29 21 3d 30 3b 0a 20 20 7d 0a 20 20 69 In1)!=0;. }. i
1d2be 66 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 f( pIn2->flags &
1d2bf 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 MEM_Null ){.
1d2c0 20 75 2e 61 6b 2e 76 32 20 3d 20 32 3b 0a 20 20 u.ak.v2 = 2;.
1d2c1 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 6b 2e }else{. u.ak.
1d2c2 76 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 v2 = sqlite3Vdbe
1d2c3 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 21 3d IntValue(pIn2)!=
1d2c4 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 0;. }. if( pOp
1d2c5 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 6e 64 ->opcode==OP_And
1d2c6 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 ){. static c
1d2c7 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
1d2c8 61 72 20 61 6e 64 5f 6c 6f 67 69 63 5b 5d 20 3d ar and_logic[] =
1d2c9 20 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 { 0, 0, 0, 0, 1
1d2ca 2c 20 32 2c 20 30 2c 20 32 2c 20 32 20 7d 3b 0a , 2, 0, 2, 2 };.
1d2cb 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20 61 6e u.ak.v1 = an
1d2cc 64 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 31 2a d_logic[u.ak.v1*
1d2cd 33 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 7d 65 3+u.ak.v2];. }e
1d2ce 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20 lse{. static
1d2cf 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
1d2d0 68 61 72 20 6f 72 5f 6c 6f 67 69 63 5b 5d 20 3d har or_logic[] =
1d2d1 20 7b 20 30 2c 20 31 2c 20 32 2c 20 31 2c 20 31 { 0, 1, 2, 1, 1
1d2d2 2c 20 31 2c 20 32 2c 20 31 2c 20 32 20 7d 3b 0a , 1, 2, 1, 2 };.
1d2d3 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20 6f 72 u.ak.v1 = or
1d2d4 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 31 2a 33 _logic[u.ak.v1*3
1d2d5 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 7d 0a 20 +u.ak.v2];. }.
1d2d6 20 69 66 28 20 75 2e 61 6b 2e 76 31 3d 3d 32 20 if( u.ak.v1==2
1d2d7 29 7b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 ){. MemSetTyp
1d2d8 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f eFlag(pOut, MEM_
1d2d9 4e 75 6c 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a Null);. }else{.
1d2da 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 pOut->u.i =
1d2db 75 2e 61 6b 2e 76 31 3b 0a 20 20 20 20 4d 65 6d u.ak.v1;. Mem
1d2dc 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 SetTypeFlag(pOut
1d2dd 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a , MEM_Int);. }.
1d2de 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1d2df 70 63 6f 64 65 3a 20 4e 6f 74 20 50 31 20 50 32 pcode: Not P1 P2
1d2e0 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74 * * *.**.** Int
1d2e1 65 72 70 72 65 74 20 74 68 65 20 76 61 6c 75 65 erpret the value
1d2e2 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1d2e3 61 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 6c as a boolean val
1d2e4 75 65 2e 20 20 53 74 6f 72 65 20 74 68 65 0a 2a ue. Store the.*
1d2e5 2a 20 62 6f 6f 6c 65 61 6e 20 63 6f 6d 70 6c 65 * boolean comple
1d2e6 6d 65 6e 74 20 69 6e 20 72 65 67 69 73 74 65 72 ment in register
1d2e7 20 50 32 2e 20 20 49 66 20 74 68 65 20 76 61 6c P2. If the val
1d2e8 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1d2e9 31 20 69 73 20 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 1 is .** NULL, t
1d2ea 68 65 6e 20 61 20 4e 55 4c 4c 20 69 73 20 73 74 hen a NULL is st
1d2eb 6f 72 65 64 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63 ored in P2..*/.c
1d2ec 61 73 65 20 4f 50 5f 4e 6f 74 3a 20 7b 20 20 20 ase OP_Not: {
1d2ed 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1d2ee 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 2c 20 same as TK_NOT,
1d2ef 69 6e 31 20 2a 2f 0a 20 20 70 4f 75 74 20 3d 20 in1 */. pOut =
1d2f0 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 &p->aMem[pOp->p2
1d2f1 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 ];. if( pIn1->f
1d2f2 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 lags & MEM_Null
1d2f3 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1d2f4 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 beMemSetNull(pOu
1d2f5 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 t);. }else{.
1d2f6 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1d2f7 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 21 73 etInt64(pOut, !s
1d2f8 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c qlite3VdbeIntVal
1d2f9 75 65 28 70 49 6e 31 29 29 3b 0a 20 20 7d 0a 20 ue(pIn1));. }.
1d2fa 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1d2fb 63 6f 64 65 3a 20 42 69 74 4e 6f 74 20 50 31 20 code: BitNot P1
1d2fc 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 P2 * * *.**.** I
1d2fd 6e 74 65 72 70 72 65 74 20 74 68 65 20 63 6f 6e nterpret the con
1d2fe 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 tent of register
1d2ff 20 50 31 20 61 73 20 61 6e 20 69 6e 74 65 67 65 P1 as an intege
1d300 72 2e 20 20 53 74 6f 72 65 20 74 68 65 0a 2a 2a r. Store the.**
1d301 20 6f 6e 65 73 2d 63 6f 6d 70 6c 65 6d 65 6e 74 ones-complement
1d302 20 6f 66 20 74 68 65 20 50 31 20 76 61 6c 75 65 of the P1 value
1d303 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 into register P
1d304 32 2e 20 20 49 66 20 50 31 20 68 6f 6c 64 73 0a 2. If P1 holds.
1d305 2a 2a 20 61 20 4e 55 4c 4c 20 74 68 65 6e 20 73 ** a NULL then s
1d306 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e 20 50 tore a NULL in P
1d307 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69 2..*/.case OP_Bi
1d308 74 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20 tNot: {
1d309 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1d30a 4b 5f 42 49 54 4e 4f 54 2c 20 69 6e 31 20 2a 2f K_BITNOT, in1 */
1d30b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d . pOut = &p->aM
1d30c 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 em[pOp->p2];. i
1d30d 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 f( pIn1->flags &
1d30e 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 MEM_Null ){.
1d30f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1d310 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 etNull(pOut);.
1d311 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 }else{. sqlit
1d312 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 e3VdbeMemSetInt6
1d313 34 28 70 4f 75 74 2c 20 7e 73 71 6c 69 74 65 33 4(pOut, ~sqlite3
1d314 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e VdbeIntValue(pIn
1d315 31 29 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 1));. }. break
1d316 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1d317 49 66 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a If P1 P2 P3 * *.
1d318 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 **.** Jump to P2
1d319 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e if the value in
1d31a 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 register P1 is
1d31b 74 72 75 65 2e 20 20 54 68 65 20 76 61 6c 75 65 true. The value
1d31c 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 is.** is consid
1d31d 65 72 65 64 20 74 72 75 65 20 69 66 20 69 74 20 ered true if it
1d31e 69 73 20 6e 75 6d 65 72 69 63 20 61 6e 64 20 6e is numeric and n
1d31f 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 74 68 65 on-zero. If the
1d320 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20 value.** in P1
1d321 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b is NULL then tak
1d322 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33 e the jump if P3
1d323 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 2f 2a 20 is true..*/./*
1d324 4f 70 63 6f 64 65 3a 20 49 66 4e 6f 74 20 50 31 Opcode: IfNot P1
1d325 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1d326 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74 Jump to P2 if t
1d327 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1d328 73 74 65 72 20 50 31 20 69 73 20 46 61 6c 73 65 ster P1 is False
1d329 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 0a . The value is.
1d32a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 ** is considered
1d32b 20 74 72 75 65 20 69 66 20 69 74 20 68 61 73 20 true if it has
1d32c 61 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20 a numeric value
1d32d 6f 66 20 7a 65 72 6f 2e 20 20 49 66 20 74 68 65 of zero. If the
1d32e 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20 value.** in P1
1d32f 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b is NULL then tak
1d330 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33 e the jump if P3
1d331 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 63 61 73 is true..*/.cas
1d332 65 20 4f 50 5f 49 66 3a 20 20 20 20 20 20 20 20 e OP_If:
1d333 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 /* jump
1d334 2c 20 69 6e 31 20 2a 2f 0a 63 61 73 65 20 4f 50 , in1 */.case OP
1d335 5f 49 66 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 _IfNot: {
1d336 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1d337 31 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 1 */.#if 0 /* l
1d338 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1d339 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6c 20 2a oved into u.al *
1d33a 2f 0a 20 20 69 6e 74 20 63 3b 0a 23 65 6e 64 69 /. int c;.#endi
1d33b 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1d33c 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1d33d 75 2e 61 6c 20 2a 2f 0a 20 20 69 66 28 20 70 49 u.al */. if( pI
1d33e 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n1->flags & MEM_
1d33f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 2e 61 6c Null ){. u.al
1d340 2e 63 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 .c = pOp->p3;.
1d341 7d 65 6c 73 65 7b 0a 23 69 66 64 65 66 20 53 51 }else{.#ifdef SQ
1d342 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
1d343 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 75 2e 61 NG_POINT. u.a
1d344 6c 2e 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 l.c = sqlite3Vdb
1d345 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 21 eIntValue(pIn1)!
1d346 3d 30 3b 0a 23 65 6c 73 65 0a 20 20 20 20 75 2e =0;.#else. u.
1d347 61 6c 2e 63 20 3d 20 73 71 6c 69 74 65 33 56 64 al.c = sqlite3Vd
1d348 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49 6e 31 beRealValue(pIn1
1d349 29 21 3d 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 20 )!=0.0;.#endif.
1d34a 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f if( pOp->opco
1d34b 64 65 3d 3d 4f 50 5f 49 66 4e 6f 74 20 29 20 75 de==OP_IfNot ) u
1d34c 2e 61 6c 2e 63 20 3d 20 21 75 2e 61 6c 2e 63 3b .al.c = !u.al.c;
1d34d 0a 20 20 7d 0a 20 20 69 66 28 20 75 2e 61 6c 2e . }. if( u.al.
1d34e 63 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f c ){. pc = pO
1d34f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 p->p2-1;. }. b
1d350 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1d351 64 65 3a 20 49 73 4e 75 6c 6c 20 50 31 20 50 32 de: IsNull P1 P2
1d352 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d * * *.**.** Jum
1d353 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76 p to P2 if the v
1d354 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1d355 20 50 31 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a P1 is NULL..*/.
1d356 63 61 73 65 20 4f 50 5f 49 73 4e 75 6c 6c 3a 20 case OP_IsNull:
1d357 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1d358 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e 55 4c same as TK_ISNUL
1d359 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a L, jump, in1 */.
1d35a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 if( (pIn1->fla
1d35b 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d gs & MEM_Null)!=
1d35c 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 0 ){. pc = pO
1d35d 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 p->p2 - 1;. }.
1d35e 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1d35f 63 6f 64 65 3a 20 4e 6f 74 4e 75 6c 6c 20 50 31 code: NotNull P1
1d360 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1d361 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74 68 Jump to P2 if th
1d362 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1d363 74 65 72 20 50 31 20 69 73 20 6e 6f 74 20 4e 55 ter P1 is not NU
1d364 4c 4c 2e 20 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 LL. .*/.case OP
1d365 5f 4e 6f 74 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 _NotNull: {
1d366 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1d367 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 6a 75 s TK_NOTNULL, ju
1d368 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 mp, in1 */. if(
1d369 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 (pIn1->flags &
1d36a 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a MEM_Null)==0 ){.
1d36b 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 pc = pOp->p2
1d36c 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 - 1;. }. brea
1d36d 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1d36e 20 43 6f 6c 75 6d 6e 20 50 31 20 50 32 20 50 33 Column P1 P2 P3
1d36f 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 49 6e 74 P4 P5.**.** Int
1d370 65 72 70 72 65 74 20 74 68 65 20 64 61 74 61 20 erpret the data
1d371 74 68 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 that cursor P1 p
1d372 6f 69 6e 74 73 20 74 6f 20 61 73 20 61 20 73 74 oints to as a st
1d373 72 75 63 74 75 72 65 20 62 75 69 6c 74 20 75 73 ructure built us
1d374 69 6e 67 0a 2a 2a 20 74 68 65 20 4d 61 6b 65 52 ing.** the MakeR
1d375 65 63 6f 72 64 20 69 6e 73 74 72 75 63 74 69 6f ecord instructio
1d376 6e 2e 20 20 28 53 65 65 20 74 68 65 20 4d 61 6b n. (See the Mak
1d377 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 66 eRecord opcode f
1d378 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a or additional.**
1d379 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
1d37a 75 74 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 ut the format of
1d37b 20 74 68 65 20 64 61 74 61 2e 29 20 20 45 78 74 the data.) Ext
1d37c 72 61 63 74 20 74 68 65 20 50 32 2d 74 68 20 63 ract the P2-th c
1d37d 6f 6c 75 6d 6e 0a 2a 2a 20 66 72 6f 6d 20 74 68 olumn.** from th
1d37e 69 73 20 72 65 63 6f 72 64 2e 20 20 49 66 20 74 is record. If t
1d37f 68 65 72 65 20 61 72 65 20 6c 65 73 73 20 74 68 here are less th
1d380 61 74 20 28 50 32 2b 31 29 20 0a 2a 2a 20 76 61 at (P2+1) .** va
1d381 6c 75 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f lues in the reco
1d382 72 64 2c 20 65 78 74 72 61 63 74 20 61 20 4e 55 rd, extract a NU
1d383 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 LL..**.** The va
1d384 6c 75 65 20 65 78 74 72 61 63 74 65 64 20 69 73 lue extracted is
1d385 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
1d386 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 ter P3..**.** If
1d387 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 the column cont
1d388 61 69 6e 73 20 66 65 77 65 72 20 74 68 61 6e 20 ains fewer than
1d389 50 32 20 66 69 65 6c 64 73 2c 20 74 68 65 6e 20 P2 fields, then
1d38a 65 78 74 72 61 63 74 20 61 20 4e 55 4c 4c 2e 20 extract a NULL.
1d38b 20 4f 72 2c 0a 2a 2a 20 69 66 20 74 68 65 20 50 Or,.** if the P
1d38c 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 4 argument is a
1d38d 50 34 5f 4d 45 4d 20 75 73 65 20 74 68 65 20 76 P4_MEM use the v
1d38e 61 6c 75 65 20 6f 66 20 74 68 65 20 50 34 20 61 alue of the P4 a
1d38f 72 67 75 6d 65 6e 74 20 61 73 0a 2a 2a 20 74 68 rgument as.** th
1d390 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 e result..**.**
1d391 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 43 4c If the OPFLAG_CL
1d392 45 41 52 43 41 43 48 45 20 62 69 74 20 69 73 20 EARCACHE bit is
1d393 73 65 74 20 6f 6e 20 50 35 20 61 6e 64 20 50 31 set on P5 and P1
1d394 20 69 73 20 61 20 70 73 65 75 64 6f 2d 74 61 62 is a pseudo-tab
1d395 6c 65 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74 68 le cursor,.** th
1d396 65 6e 20 74 68 65 20 63 61 63 68 65 20 6f 66 20 en the cache of
1d397 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 72 65 the cursor is re
1d398 73 65 74 20 70 72 69 6f 72 20 74 6f 20 65 78 74 set prior to ext
1d399 72 61 63 74 69 6e 67 20 74 68 65 20 63 6f 6c 75 racting the colu
1d39a 6d 6e 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 mn..** The first
1d39b 20 4f 50 5f 43 6f 6c 75 6d 6e 20 61 67 61 69 6e OP_Column again
1d39c 73 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c st a pseudo-tabl
1d39d 65 20 61 66 74 65 72 20 74 68 65 20 76 61 6c 75 e after the valu
1d39e 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 e of the content
1d39f 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 68 61 73 .** register has
1d3a0 20 63 68 61 6e 67 65 64 20 73 68 6f 75 6c 64 20 changed should
1d3a1 68 61 76 65 20 74 68 69 73 20 62 69 74 20 73 65 have this bit se
1d3a2 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f t..*/.case OP_Co
1d3a3 6c 75 6d 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f lumn: {.#if 0 /
1d3a4 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1d3a5 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1d3a6 6d 20 2a 2f 0a 20 20 75 33 32 20 70 61 79 6c 6f m */. u32 paylo
1d3a7 61 64 53 69 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d adSize; /* Num
1d3a8 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
1d3a9 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 the record */.
1d3aa 69 36 34 20 70 61 79 6c 6f 61 64 53 69 7a 65 36 i64 payloadSize6
1d3ab 34 3b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4; /* Number of
1d3ac 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 bytes in the rec
1d3ad 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b ord */. int p1;
1d3ae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
1d3af 31 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 1 value of the o
1d3b0 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 pcode */. int p
1d3b1 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 2; /*
1d3b2 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 74 column number t
1d3b3 6f 20 72 65 74 72 69 65 76 65 20 2a 2f 0a 20 20 o retrieve */.
1d3b4 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 VdbeCursor *pC;
1d3b5 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 /* The VDBE c
1d3b6 75 72 73 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20 ursor */. char
1d3b7 2a 7a 52 65 63 3b 20 20 20 20 20 20 20 20 2f 2a *zRec; /*
1d3b8 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 6f 6d 70 Pointer to comp
1d3b9 6c 65 74 65 20 72 65 63 6f 72 64 2d 64 61 74 61 lete record-data
1d3ba 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a */. BtCursor *
1d3bb 70 43 72 73 72 3b 20 20 20 2f 2a 20 54 68 65 20 pCrsr; /* The
1d3bc 42 54 72 65 65 20 63 75 72 73 6f 72 20 2a 2f 0a BTree cursor */.
1d3bd 20 20 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 u32 *aType;
1d3be 20 20 20 20 20 2f 2a 20 61 54 79 70 65 5b 69 5d /* aType[i]
1d3bf 20 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d 65 72 holds the numer
1d3c0 69 63 20 74 79 70 65 20 6f 66 20 74 68 65 20 69 ic type of the i
1d3c1 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 -th column */.
1d3c2 75 33 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 u32 *aOffset;
1d3c3 20 20 20 2f 2a 20 61 4f 66 66 73 65 74 5b 69 5d /* aOffset[i]
1d3c4 20 69 73 20 6f 66 66 73 65 74 20 74 6f 20 73 74 is offset to st
1d3c5 61 72 74 20 6f 66 20 64 61 74 61 20 66 6f 72 20 art of data for
1d3c6 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 i-th column */.
1d3c7 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 int nField;
1d3c8 20 20 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 /* number of
1d3c9 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 fields in the r
1d3ca 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6c ecord */. int l
1d3cb 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a en; /*
1d3cc 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 The length of t
1d3cd 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 he serialized da
1d3ce 74 61 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d ta for the colum
1d3cf 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 n */. int i;
1d3d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
1d3d1 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 63 p counter */. c
1d3d2 68 61 72 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 har *zData;
1d3d3 20 20 2f 2a 20 50 61 72 74 20 6f 66 20 74 68 65 /* Part of the
1d3d4 20 72 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65 record being de
1d3d5 63 6f 64 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a coded */. Mem *
1d3d6 70 44 65 73 74 3b 20 20 20 20 20 20 20 20 2f 2a pDest; /*
1d3d7 20 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 Where to write
1d3d8 74 68 65 20 65 78 74 72 61 63 74 65 64 20 76 61 the extracted va
1d3d9 6c 75 65 20 2a 2f 0a 20 20 4d 65 6d 20 73 4d 65 lue */. Mem sMe
1d3da 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 m; /* F
1d3db 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 20 72 or storing the r
1d3dc 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f ecord being deco
1d3dd 64 65 64 20 2a 2f 0a 20 20 75 38 20 2a 7a 49 64 ded */. u8 *zId
1d3de 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 x; /* I
1d3df 6e 64 65 78 20 69 6e 74 6f 20 68 65 61 64 65 72 ndex into header
1d3e0 20 2a 2f 0a 20 20 75 38 20 2a 7a 45 6e 64 48 64 */. u8 *zEndHd
1d3e1 72 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e r; /* Poin
1d3e2 74 65 72 20 74 6f 20 66 69 72 73 74 20 62 79 74 ter to first byt
1d3e3 65 20 61 66 74 65 72 20 74 68 65 20 68 65 61 64 e after the head
1d3e4 65 72 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73 er */. u32 offs
1d3e5 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 et; /* Of
1d3e6 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 64 61 fset into the da
1d3e7 74 61 20 2a 2f 0a 20 20 75 36 34 20 6f 66 66 73 ta */. u64 offs
1d3e8 65 74 36 34 3b 20 20 20 20 20 20 2f 2a 20 36 34 et64; /* 64
1d3e9 2d 62 69 74 20 6f 66 66 73 65 74 2e 20 20 36 34 -bit offset. 64
1d3ea 20 62 69 74 73 20 6e 65 65 64 65 64 20 74 6f 20 bits needed to
1d3eb 63 61 74 63 68 20 6f 76 65 72 66 6c 6f 77 20 2a catch overflow *
1d3ec 2f 0a 20 20 69 6e 74 20 73 7a 48 64 72 3b 20 20 /. int szHdr;
1d3ed 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
1d3ee 66 20 74 68 65 20 68 65 61 64 65 72 20 73 69 7a f the header siz
1d3ef 65 20 66 69 65 6c 64 20 61 74 20 73 74 61 72 74 e field at start
1d3f0 20 6f 66 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 of record */.
1d3f1 69 6e 74 20 61 76 61 69 6c 3b 20 20 20 20 20 20 int avail;
1d3f2 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1d3f3 62 79 74 65 73 20 6f 66 20 61 76 61 69 6c 61 62 bytes of availab
1d3f4 6c 65 20 64 61 74 61 20 2a 2f 0a 20 20 4d 65 6d le data */. Mem
1d3f5 20 2a 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 *pReg;
1d3f6 2f 2a 20 50 73 65 75 64 6f 54 61 62 6c 65 20 69 /* PseudoTable i
1d3f7 6e 70 75 74 20 72 65 67 69 73 74 65 72 20 2a 2f nput register */
1d3f8 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1d3f9 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1d3fa 20 69 6e 74 6f 20 75 2e 61 6d 20 2a 2f 0a 0a 0a into u.am */...
1d3fb 20 20 75 2e 61 6d 2e 70 31 20 3d 20 70 4f 70 2d u.am.p1 = pOp-
1d3fc 3e 70 31 3b 0a 20 20 75 2e 61 6d 2e 70 32 20 3d >p1;. u.am.p2 =
1d3fd 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 6d pOp->p2;. u.am
1d3fe 2e 70 43 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 65 .pC = 0;. memse
1d3ff 74 28 26 75 2e 61 6d 2e 73 4d 65 6d 2c 20 30 2c t(&u.am.sMem, 0,
1d400 20 73 69 7a 65 6f 66 28 75 2e 61 6d 2e 73 4d 65 sizeof(u.am.sMe
1d401 6d 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 m));. assert( u
1d402 2e 61 6d 2e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f .am.p1<p->nCurso
1d403 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 r );. assert( p
1d404 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d Op->p3>0 && pOp-
1d405 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p3<=p->nMem );.
1d406 20 20 75 2e 61 6d 2e 70 44 65 73 74 20 3d 20 26 u.am.pDest = &
1d407 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d p->aMem[pOp->p3]
1d408 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c ;. MemSetTypeFl
1d409 61 67 28 75 2e 61 6d 2e 70 44 65 73 74 2c 20 4d ag(u.am.pDest, M
1d40a 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 75 2e 61 6d EM_Null);. u.am
1d40b 2e 7a 52 65 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a .zRec = 0;.. /*
1d40c 20 54 68 69 73 20 62 6c 6f 63 6b 20 73 65 74 73 This block sets
1d40d 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 75 2e the variable u.
1d40e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 74 am.payloadSize t
1d40f 6f 20 62 65 20 74 68 65 20 74 6f 74 61 6c 20 6e o be the total n
1d410 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 62 79 umber of. ** by
1d411 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 tes in the recor
1d412 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 75 2e 61 d.. **. ** u.a
1d413 6d 2e 7a 52 65 63 20 69 73 20 73 65 74 20 74 6f m.zRec is set to
1d414 20 62 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 be the complete
1d415 20 74 65 78 74 20 6f 66 20 74 68 65 20 72 65 63 text of the rec
1d416 6f 72 64 20 69 66 20 69 74 20 69 73 20 61 76 61 ord if it is ava
1d417 69 6c 61 62 6c 65 2e 0a 20 20 2a 2a 20 54 68 65 ilable.. ** The
1d418 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 complete record
1d419 20 74 65 78 74 20 69 73 20 61 6c 77 61 79 73 20 text is always
1d41a 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 70 73 available for ps
1d41b 65 75 64 6f 2d 74 61 62 6c 65 73 0a 20 20 2a 2a eudo-tables. **
1d41c 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 If the record i
1d41d 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 63 75 s stored in a cu
1d41e 72 73 6f 72 2c 20 74 68 65 20 63 6f 6d 70 6c 65 rsor, the comple
1d41f 74 65 20 72 65 63 6f 72 64 20 74 65 78 74 0a 20 te record text.
1d420 20 2a 2a 20 6d 69 67 68 74 20 62 65 20 61 76 61 ** might be ava
1d421 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 20 75 ilable in the u
1d422 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77 20 63 61 63 .am.pC->aRow cac
1d423 68 65 2e 20 20 4f 72 20 69 74 20 6d 69 67 68 74 he. Or it might
1d424 20 6e 6f 74 20 62 65 2e 0a 20 20 2a 2a 20 49 66 not be.. ** If
1d425 20 74 68 65 20 64 61 74 61 20 69 73 20 75 6e 61 the data is una
1d426 76 61 69 6c 61 62 6c 65 2c 20 20 75 2e 61 6d 2e vailable, u.am.
1d427 7a 52 65 63 20 69 73 20 73 65 74 20 74 6f 20 4e zRec is set to N
1d428 55 4c 4c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 ULL.. **. ** W
1d429 65 20 61 6c 73 6f 20 63 6f 6d 70 75 74 65 20 74 e also compute t
1d42a 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c he number of col
1d42b 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 63 6f umns in the reco
1d42c 72 64 2e 20 20 46 6f 72 20 63 75 72 73 6f 72 73 rd. For cursors
1d42d 2c 0a 20 20 2a 2a 20 74 68 65 20 6e 75 6d 62 65 ,. ** the numbe
1d42e 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 73 20 r of columns is
1d42f 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56 64 stored in the Vd
1d430 62 65 43 75 72 73 6f 72 2e 6e 46 69 65 6c 64 20 beCursor.nField
1d431 65 6c 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 element.. */.
1d432 75 2e 61 6d 2e 70 43 20 3d 20 70 2d 3e 61 70 43 u.am.pC = p->apC
1d433 73 72 5b 75 2e 61 6d 2e 70 31 5d 3b 0a 20 20 61 sr[u.am.p1];. a
1d434 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 43 21 3d ssert( u.am.pC!=
1d435 30 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 0 );.#ifndef SQL
1d436 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1d437 54 41 42 4c 45 0a 20 20 61 73 73 65 72 74 28 20 TABLE. assert(
1d438 75 2e 61 6d 2e 70 43 2d 3e 70 56 74 61 62 43 75 u.am.pC->pVtabCu
1d439 72 73 6f 72 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 rsor==0 );.#endi
1d43a 66 0a 20 20 75 2e 61 6d 2e 70 43 72 73 72 20 3d f. u.am.pCrsr =
1d43b 20 75 2e 61 6d 2e 70 43 2d 3e 70 43 75 72 73 6f u.am.pC->pCurso
1d43c 72 3b 0a 20 20 69 66 28 20 75 2e 61 6d 2e 70 43 r;. if( u.am.pC
1d43d 72 73 72 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a rsr!=0 ){. /*
1d43e 20 54 68 65 20 72 65 63 6f 72 64 20 69 73 20 73 The record is s
1d43f 74 6f 72 65 64 20 69 6e 20 61 20 42 2d 54 72 65 tored in a B-Tre
1d440 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 e */. rc = sq
1d441 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d lite3VdbeCursorM
1d442 6f 76 65 74 6f 28 75 2e 61 6d 2e 70 43 29 3b 0a oveto(u.am.pC);.
1d443 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 if( rc ) got
1d444 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
1d445 72 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 75 2e rror;. if( u.
1d446 61 6d 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 am.pC->nullRow )
1d447 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 70 61 79 {. u.am.pay
1d448 6c 6f 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20 loadSize = 0;.
1d449 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 6d }else if( u.am
1d44a 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 .pC->cacheStatus
1d44b 3d 3d 70 2d 3e 63 61 63 68 65 43 74 72 20 29 7b ==p->cacheCtr ){
1d44c 0a 20 20 20 20 20 20 75 2e 61 6d 2e 70 61 79 6c . u.am.payl
1d44d 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70 oadSize = u.am.p
1d44e 43 2d 3e 70 61 79 6c 6f 61 64 53 69 7a 65 3b 0a C->payloadSize;.
1d44f 20 20 20 20 20 20 75 2e 61 6d 2e 7a 52 65 63 20 u.am.zRec
1d450 3d 20 28 63 68 61 72 2a 29 75 2e 61 6d 2e 70 43 = (char*)u.am.pC
1d451 2d 3e 61 52 6f 77 3b 0a 20 20 20 20 7d 65 6c 73 ->aRow;. }els
1d452 65 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 69 e if( u.am.pC->i
1d453 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20 sIndex ){.
1d454 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
1d455 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 treeCursorIsVali
1d456 64 28 75 2e 61 6d 2e 70 43 72 73 72 29 20 29 3b d(u.am.pCrsr) );
1d457 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1d458 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 te3BtreeKeySize(
1d459 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e 61 u.am.pCrsr, &u.a
1d45a 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 29 m.payloadSize64)
1d45b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1d45c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc==SQLITE_OK );
1d45d 20 20 20 2f 2a 20 54 72 75 65 20 62 65 63 61 75 /* True becau
1d45e 73 65 20 6f 66 20 43 75 72 73 6f 72 4d 6f 76 65 se of CursorMove
1d45f 74 6f 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 20 to() call above
1d460 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 */. /* sqli
1d461 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c te3BtreeParseCel
1d462 6c 50 74 72 28 29 20 75 73 65 73 20 67 65 74 56 lPtr() uses getV
1d463 61 72 69 6e 74 33 32 28 29 20 74 6f 20 65 78 74 arint32() to ext
1d464 72 61 63 74 20 74 68 65 0a 20 20 20 20 20 20 2a ract the. *
1d465 2a 20 70 61 79 6c 6f 61 64 20 73 69 7a 65 2c 20 * payload size,
1d466 73 6f 20 69 74 20 69 73 20 69 6d 70 6f 73 73 69 so it is impossi
1d467 62 6c 65 20 66 6f 72 20 75 2e 61 6d 2e 70 61 79 ble for u.am.pay
1d468 6c 6f 61 64 53 69 7a 65 36 34 20 74 6f 20 62 65 loadSize64 to be
1d469 0a 20 20 20 20 20 20 2a 2a 20 6c 61 72 67 65 72 . ** larger
1d46a 20 74 68 61 6e 20 33 32 20 62 69 74 73 2e 20 2a than 32 bits. *
1d46b 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 /. assert(
1d46c 28 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a (u.am.payloadSiz
1d46d 65 36 34 20 26 20 53 51 4c 49 54 45 5f 4d 41 58 e64 & SQLITE_MAX
1d46e 5f 55 33 32 29 3d 3d 28 75 36 34 29 75 2e 61 6d _U32)==(u64)u.am
1d46f 2e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 20 29 .payloadSize64 )
1d470 3b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 70 61 79 ;. u.am.pay
1d471 6c 6f 61 64 53 69 7a 65 20 3d 20 28 75 33 32 29 loadSize = (u32)
1d472 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d473 36 34 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 64;. }else{.
1d474 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c assert( sql
1d475 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 ite3BtreeCursorI
1d476 73 56 61 6c 69 64 28 75 2e 61 6d 2e 70 43 72 73 sValid(u.am.pCrs
1d477 72 29 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d r) );. rc =
1d478 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 sqlite3BtreeDat
1d479 61 53 69 7a 65 28 75 2e 61 6d 2e 70 43 72 73 72 aSize(u.am.pCrsr
1d47a 2c 20 26 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 , &u.am.payloadS
1d47b 69 7a 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65 ize);. asse
1d47c 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rt( rc==SQLITE_O
1d47d 4b 20 29 3b 20 20 20 2f 2a 20 44 61 74 61 53 69 K ); /* DataSi
1d47e 7a 65 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c ze() cannot fail
1d47f 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 */. }. }els
1d480 65 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 70 e if( u.am.pC->p
1d481 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3e 30 20 seudoTableReg>0
1d482 29 7b 0a 20 20 20 20 75 2e 61 6d 2e 70 52 65 67 ){. u.am.pReg
1d483 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 6d = &p->aMem[u.am
1d484 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 .pC->pseudoTable
1d485 52 65 67 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 Reg];. assert
1d486 28 20 75 2e 61 6d 2e 70 52 65 67 2d 3e 66 6c 61 ( u.am.pReg->fla
1d487 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b gs & MEM_Blob );
1d488 0a 20 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 . u.am.payloa
1d489 64 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70 52 65 dSize = u.am.pRe
1d48a 67 2d 3e 6e 3b 0a 20 20 20 20 75 2e 61 6d 2e 7a g->n;. u.am.z
1d48b 52 65 63 20 3d 20 75 2e 61 6d 2e 70 52 65 67 2d Rec = u.am.pReg-
1d48c 3e 7a 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 43 2d >z;. u.am.pC-
1d48d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 28 >cacheStatus = (
1d48e 70 4f 70 2d 3e 70 35 26 4f 50 46 4c 41 47 5f 43 pOp->p5&OPFLAG_C
1d48f 4c 45 41 52 43 41 43 48 45 29 20 3f 20 43 41 43 LEARCACHE) ? CAC
1d490 48 45 5f 53 54 41 4c 45 20 3a 20 70 2d 3e 63 61 HE_STALE : p->ca
1d491 63 68 65 43 74 72 3b 0a 20 20 20 20 61 73 73 65 cheCtr;. asse
1d492 72 74 28 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 rt( u.am.payload
1d493 53 69 7a 65 3d 3d 30 20 7c 7c 20 75 2e 61 6d 2e Size==0 || u.am.
1d494 7a 52 65 63 21 3d 30 20 29 3b 0a 20 20 7d 65 6c zRec!=0 );. }el
1d495 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 6e 73 69 se{. /* Consi
1d496 64 65 72 20 74 68 65 20 72 6f 77 20 74 6f 20 62 der the row to b
1d497 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 75 2e e NULL */. u.
1d498 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d am.payloadSize =
1d499 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 0;. }.. /* If
1d49a 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a u.am.payloadSiz
1d49b 65 20 69 73 20 30 2c 20 74 68 65 6e 20 6a 75 73 e is 0, then jus
1d49c 74 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 2a t store a NULL *
1d49d 2f 0a 20 20 69 66 28 20 75 2e 61 6d 2e 70 61 79 /. if( u.am.pay
1d49e 6c 6f 61 64 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 loadSize==0 ){.
1d49f 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e assert( u.am.
1d4a0 70 44 65 73 74 2d 3e 66 6c 61 67 73 26 4d 45 4d pDest->flags&MEM
1d4a1 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 67 6f 74 _Null );. got
1d4a2 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b o op_column_out;
1d4a3 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 64 . }. assert( d
1d4a4 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
1d4a5 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3e 3d _LIMIT_LENGTH]>=
1d4a6 30 20 29 3b 0a 20 20 69 66 28 20 75 2e 61 6d 2e 0 );. if( u.am.
1d4a7 70 61 79 6c 6f 61 64 53 69 7a 65 20 3e 20 28 75 payloadSize > (u
1d4a8 33 32 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 32)db->aLimit[SQ
1d4a9 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
1d4aa 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 H] ){. goto t
1d4ab 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 75 oo_big;. }.. u
1d4ac 2e 61 6d 2e 6e 46 69 65 6c 64 20 3d 20 75 2e 61 .am.nField = u.a
1d4ad 6d 2e 70 43 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 m.pC->nField;.
1d4ae 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 32 3c assert( u.am.p2<
1d4af 75 2e 61 6d 2e 6e 46 69 65 6c 64 20 29 3b 0a 0a u.am.nField );..
1d4b0 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 70 61 /* Read and pa
1d4b1 72 73 65 20 74 68 65 20 74 61 62 6c 65 20 68 65 rse the table he
1d4b2 61 64 65 72 2e 20 20 53 74 6f 72 65 20 74 68 65 ader. Store the
1d4b3 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 results of the
1d4b4 70 61 72 73 65 0a 20 20 2a 2a 20 69 6e 74 6f 20 parse. ** into
1d4b5 74 68 65 20 72 65 63 6f 72 64 20 68 65 61 64 65 the record heade
1d4b6 72 20 63 61 63 68 65 20 66 69 65 6c 64 73 20 6f r cache fields o
1d4b7 66 20 74 68 65 20 63 75 72 73 6f 72 2e 0a 20 20 f the cursor..
1d4b8 2a 2f 0a 20 20 75 2e 61 6d 2e 61 54 79 70 65 20 */. u.am.aType
1d4b9 3d 20 75 2e 61 6d 2e 70 43 2d 3e 61 54 79 70 65 = u.am.pC->aType
1d4ba 3b 0a 20 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d ;. if( u.am.pC-
1d4bb 3e 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d >cacheStatus==p-
1d4bc 3e 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20 >cacheCtr ){.
1d4bd 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 20 3d 20 u.am.aOffset =
1d4be 75 2e 61 6d 2e 70 43 2d 3e 61 4f 66 66 73 65 74 u.am.pC->aOffset
1d4bf 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
1d4c0 73 73 65 72 74 28 75 2e 61 6d 2e 61 54 79 70 65 ssert(u.am.aType
1d4c1 29 3b 0a 20 20 20 20 75 2e 61 6d 2e 61 76 61 69 );. u.am.avai
1d4c2 6c 20 3d 20 30 3b 0a 20 20 20 20 75 2e 61 6d 2e l = 0;. u.am.
1d4c3 70 43 2d 3e 61 4f 66 66 73 65 74 20 3d 20 75 2e pC->aOffset = u.
1d4c4 61 6d 2e 61 4f 66 66 73 65 74 20 3d 20 26 75 2e am.aOffset = &u.
1d4c5 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 6e 46 am.aType[u.am.nF
1d4c6 69 65 6c 64 5d 3b 0a 20 20 20 20 75 2e 61 6d 2e ield];. u.am.
1d4c7 70 43 2d 3e 70 61 79 6c 6f 61 64 53 69 7a 65 20 pC->payloadSize
1d4c8 3d 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 = u.am.payloadSi
1d4c9 7a 65 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 43 2d ze;. u.am.pC-
1d4ca 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 70 >cacheStatus = p
1d4cb 2d 3e 63 61 63 68 65 43 74 72 3b 0a 0a 20 20 20 ->cacheCtr;..
1d4cc 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 /* Figure out h
1d4cd 6f 77 20 6d 61 6e 79 20 62 79 74 65 73 20 61 72 ow many bytes ar
1d4ce 65 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 20 e in the header
1d4cf 2a 2f 0a 20 20 20 20 69 66 28 20 75 2e 61 6d 2e */. if( u.am.
1d4d0 7a 52 65 63 20 29 7b 0a 20 20 20 20 20 20 75 2e zRec ){. u.
1d4d1 61 6d 2e 7a 44 61 74 61 20 3d 20 75 2e 61 6d 2e am.zData = u.am.
1d4d2 7a 52 65 63 3b 0a 20 20 20 20 7d 65 6c 73 65 7b zRec;. }else{
1d4d3 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 6d 2e . if( u.am.
1d4d4 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 pC->isIndex ){.
1d4d5 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 u.am.zDat
1d4d6 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 a = (char*)sqlit
1d4d7 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 e3BtreeKeyFetch(
1d4d8 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e 61 u.am.pCrsr, &u.a
1d4d9 6d 2e 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 20 m.avail);.
1d4da 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 75 }else{. u
1d4db 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 28 63 68 61 .am.zData = (cha
1d4dc 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 44 r*)sqlite3BtreeD
1d4dd 61 74 61 46 65 74 63 68 28 75 2e 61 6d 2e 70 43 ataFetch(u.am.pC
1d4de 72 73 72 2c 20 26 75 2e 61 6d 2e 61 76 61 69 6c rsr, &u.am.avail
1d4df 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1d4e0 20 2f 2a 20 49 66 20 4b 65 79 46 65 74 63 68 28 /* If KeyFetch(
1d4e1 29 2f 44 61 74 61 46 65 74 63 68 28 29 20 6d 61 )/DataFetch() ma
1d4e2 6e 61 67 65 64 20 74 6f 20 67 65 74 20 74 68 65 naged to get the
1d4e3 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 2c entire payload,
1d4e4 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 20 74 . ** save t
1d4e5 68 65 20 70 61 79 6c 6f 61 64 20 69 6e 20 74 68 he payload in th
1d4e6 65 20 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77 20 e u.am.pC->aRow
1d4e7 63 61 63 68 65 2e 20 20 54 68 61 74 20 77 69 6c cache. That wil
1d4e8 6c 20 73 61 76 65 20 75 73 20 66 72 6f 6d 0a 20 l save us from.
1d4e9 20 20 20 20 20 2a 2a 20 68 61 76 69 6e 67 20 74 ** having t
1d4ea 6f 20 6d 61 6b 65 20 61 64 64 69 74 69 6f 6e 61 o make additiona
1d4eb 6c 20 63 61 6c 6c 73 20 74 6f 20 66 65 74 63 68 l calls to fetch
1d4ec 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 70 6f 72 the content por
1d4ed 74 69 6f 6e 20 6f 66 0a 20 20 20 20 20 20 2a 2a tion of. **
1d4ee 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 20 the record..
1d4ef 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 */. asse
1d4f0 72 74 28 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d rt( u.am.avail>=
1d4f1 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 75 0 );. if( u
1d4f2 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 .am.payloadSize
1d4f3 3c 3d 20 28 75 33 32 29 75 2e 61 6d 2e 61 76 61 <= (u32)u.am.ava
1d4f4 69 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e il ){. u.
1d4f5 61 6d 2e 7a 52 65 63 20 3d 20 75 2e 61 6d 2e 7a am.zRec = u.am.z
1d4f6 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 75 2e Data;. u.
1d4f7 61 6d 2e 70 43 2d 3e 61 52 6f 77 20 3d 20 28 75 am.pC->aRow = (u
1d4f8 38 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 3b 0a 20 8*)u.am.zData;.
1d4f9 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1d4fa 20 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f u.am.pC->aRo
1d4fb 77 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 w = 0;. }.
1d4fc 20 20 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20 }. /* The
1d4fd 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 following assert
1d4fe 20 69 73 20 74 72 75 65 20 69 6e 20 61 6c 6c 20 is true in all
1d4ff 63 61 73 65 73 20 61 63 63 65 70 74 20 77 68 65 cases accept whe
1d500 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 n. ** the dat
1d501 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 62 abase file has b
1d502 65 65 6e 20 63 6f 72 72 75 70 74 65 64 20 65 78 een corrupted ex
1d503 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 20 20 2a 2a ternally.. **
1d504 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d assert( u.am
1d505 2e 7a 52 65 63 21 3d 30 20 7c 7c 20 75 2e 61 6d .zRec!=0 || u.am
1d506 2e 61 76 61 69 6c 3e 3d 75 2e 61 6d 2e 70 61 79 .avail>=u.am.pay
1d507 6c 6f 61 64 53 69 7a 65 20 7c 7c 20 75 2e 61 6d loadSize || u.am
1d508 2e 61 76 61 69 6c 3e 3d 39 20 29 3b 20 2a 2f 0a .avail>=9 ); */.
1d509 20 20 20 20 75 2e 61 6d 2e 73 7a 48 64 72 20 3d u.am.szHdr =
1d50a 20 67 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 getVarint32((u8
1d50b 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 2c 20 75 2e *)u.am.zData, u.
1d50c 61 6d 2e 6f 66 66 73 65 74 29 3b 0a 0a 20 20 20 am.offset);..
1d50d 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 20 /* Make sure a
1d50e 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 corrupt database
1d50f 20 68 61 73 20 6e 6f 74 20 67 69 76 65 6e 20 75 has not given u
1d510 73 20 61 6e 20 6f 76 65 72 73 69 7a 65 20 68 65 s an oversize he
1d511 61 64 65 72 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 ader.. ** Do
1d512 74 68 69 73 20 6e 6f 77 20 74 6f 20 61 76 6f 69 this now to avoi
1d513 64 20 61 6e 20 6f 76 65 72 73 69 7a 65 20 6d 65 d an oversize me
1d514 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e mory allocation.
1d515 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
1d516 79 70 65 20 65 6e 74 72 69 65 73 20 63 61 6e 20 ype entries can
1d517 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 be between 1 and
1d518 20 35 20 62 79 74 65 73 20 65 61 63 68 2e 20 20 5 bytes each.
1d519 42 75 74 20 34 20 61 6e 64 20 35 20 62 79 74 65 But 4 and 5 byte
1d51a 0a 20 20 20 20 2a 2a 20 74 79 70 65 73 20 75 73 . ** types us
1d51b 65 20 73 6f 20 6d 75 63 68 20 64 61 74 61 20 73 e so much data s
1d51c 70 61 63 65 20 74 68 61 74 20 74 68 65 72 65 20 pace that there
1d51d 63 61 6e 20 6f 6e 6c 79 20 62 65 20 34 30 39 36 can only be 4096
1d51e 20 61 6e 64 20 33 32 20 6f 66 0a 20 20 20 20 2a and 32 of. *
1d51f 2a 20 74 68 65 6d 2c 20 72 65 73 70 65 63 74 69 * them, respecti
1d520 76 65 6c 79 2e 20 20 53 6f 20 74 68 65 20 6d 61 vely. So the ma
1d521 78 69 6d 75 6d 20 68 65 61 64 65 72 20 6c 65 6e ximum header len
1d522 67 74 68 20 72 65 73 75 6c 74 73 20 66 72 6f 6d gth results from
1d523 20 61 0a 20 20 20 20 2a 2a 20 33 2d 62 79 74 65 a. ** 3-byte
1d524 20 74 79 70 65 20 66 6f 72 20 65 61 63 68 20 6f type for each o
1d525 66 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 f the maximum of
1d526 20 33 32 37 36 38 20 63 6f 6c 75 6d 6e 73 20 70 32768 columns p
1d527 6c 75 73 20 74 68 72 65 65 0a 20 20 20 20 2a 2a lus three. **
1d528 20 65 78 74 72 61 20 62 79 74 65 73 20 66 6f 72 extra bytes for
1d529 20 74 68 65 20 68 65 61 64 65 72 20 6c 65 6e 67 the header leng
1d52a 74 68 20 69 74 73 65 6c 66 2e 20 20 33 32 37 36 th itself. 3276
1d52b 38 2a 33 20 2b 20 33 20 3d 20 39 38 33 30 37 2e 8*3 + 3 = 98307.
1d52c 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1d52d 75 2e 61 6d 2e 6f 66 66 73 65 74 20 3e 20 39 38 u.am.offset > 98
1d52e 33 30 37 20 29 7b 0a 20 20 20 20 20 20 72 63 20 307 ){. rc
1d52f 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
1d530 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 _BKPT;. got
1d531 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b o op_column_out;
1d532 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 . }.. /* C
1d533 6f 6d 70 75 74 65 20 69 6e 20 75 2e 61 6d 2e 6c ompute in u.am.l
1d534 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 en the number of
1d535 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 77 bytes of data w
1d536 65 20 6e 65 65 64 20 74 6f 20 72 65 61 64 20 69 e need to read i
1d537 6e 20 6f 72 64 65 72 0a 20 20 20 20 2a 2a 20 74 n order. ** t
1d538 6f 20 67 65 74 20 75 2e 61 6d 2e 6e 46 69 65 6c o get u.am.nFiel
1d539 64 20 74 79 70 65 20 76 61 6c 75 65 73 2e 20 20 d type values.
1d53a 75 2e 61 6d 2e 6f 66 66 73 65 74 20 69 73 20 61 u.am.offset is a
1d53b 6e 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e n upper bound on
1d53c 20 74 68 69 73 2e 20 20 42 75 74 0a 20 20 20 20 this. But.
1d53d 2a 2a 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 20 6d ** u.am.nField m
1d53e 69 67 68 74 20 62 65 20 73 69 67 6e 69 66 69 63 ight be signific
1d53f 61 6e 74 6c 79 20 6c 65 73 73 20 74 68 61 6e 20 antly less than
1d540 74 68 65 20 74 72 75 65 20 6e 75 6d 62 65 72 20 the true number
1d541 6f 66 20 63 6f 6c 75 6d 6e 73 0a 20 20 20 20 2a of columns. *
1d542 2a 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20 * in the table,
1d543 61 6e 64 20 69 6e 20 74 68 61 74 20 63 61 73 65 and in that case
1d544 2c 20 35 2a 75 2e 61 6d 2e 6e 46 69 65 6c 64 2b , 5*u.am.nField+
1d545 33 20 6d 69 67 68 74 20 62 65 20 73 6d 61 6c 6c 3 might be small
1d546 65 72 20 74 68 61 6e 20 75 2e 61 6d 2e 6f 66 66 er than u.am.off
1d547 73 65 74 2e 0a 20 20 20 20 2a 2a 20 57 65 20 77 set.. ** We w
1d548 61 6e 74 20 74 6f 20 6d 69 6e 69 6d 69 7a 65 20 ant to minimize
1d549 75 2e 61 6d 2e 6c 65 6e 20 69 6e 20 6f 72 64 65 u.am.len in orde
1d54a 72 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 20 73 r to limit the s
1d54b 69 7a 65 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ize of the memor
1d54c 79 0a 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 y. ** allocat
1d54d 69 6f 6e 2c 20 65 73 70 65 63 69 61 6c 6c 79 20 ion, especially
1d54e 69 66 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 if a corrupt dat
1d54f 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 63 abase file has c
1d550 61 75 73 65 64 20 75 2e 61 6d 2e 6f 66 66 73 65 aused u.am.offse
1d551 74 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 6f t. ** to be o
1d552 76 65 72 73 69 7a 65 64 2e 20 4f 66 66 73 65 74 versized. Offset
1d553 20 69 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 39 is limited to 9
1d554 38 33 30 37 20 61 62 6f 76 65 2e 20 20 42 75 74 8307 above. But
1d555 20 39 38 33 30 37 20 6d 69 67 68 74 0a 20 20 20 98307 might.
1d556 20 2a 2a 20 73 74 69 6c 6c 20 65 78 63 65 65 64 ** still exceed
1d557 20 52 6f 62 73 6f 6e 20 6d 65 6d 6f 72 79 20 61 Robson memory a
1d558 6c 6c 6f 63 61 74 69 6f 6e 20 6c 69 6d 69 74 73 llocation limits
1d559 20 6f 6e 20 73 6f 6d 65 20 63 6f 6e 66 69 67 75 on some configu
1d55a 72 61 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2a 20 rations.. **
1d55b 4f 6e 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 On systems that
1d55c 63 61 6e 6e 6f 74 20 74 6f 6c 65 72 61 74 65 20 cannot tolerate
1d55d 6c 61 72 67 65 20 6d 65 6d 6f 72 79 20 61 6c 6c large memory all
1d55e 6f 63 61 74 69 6f 6e 73 2c 20 75 2e 61 6d 2e 6e ocations, u.am.n
1d55f 46 69 65 6c 64 2a 35 2b 33 0a 20 20 20 20 2a 2a Field*5+3. **
1d560 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 62 65 20 will likely be
1d561 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 73 69 6e much smaller sin
1d562 63 65 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 20 77 ce u.am.nField w
1d563 69 6c 6c 20 6c 69 6b 65 6c 79 20 62 65 20 6c 65 ill likely be le
1d564 73 73 20 74 68 61 6e 0a 20 20 20 20 2a 2a 20 32 ss than. ** 2
1d565 30 20 6f 72 20 73 6f 2e 20 20 54 68 69 73 20 69 0 or so. This i
1d566 6e 73 75 72 65 73 20 74 68 61 74 20 52 6f 62 73 nsures that Robs
1d567 6f 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 on memory alloca
1d568 74 69 6f 6e 20 6c 69 6d 69 74 73 20 61 72 65 0a tion limits are.
1d569 20 20 20 20 2a 2a 20 6e 6f 74 20 65 78 63 65 65 ** not excee
1d56a 64 65 64 20 65 76 65 6e 20 66 6f 72 20 63 6f 72 ded even for cor
1d56b 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 rupt database fi
1d56c 6c 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 les.. */.
1d56d 75 2e 61 6d 2e 6c 65 6e 20 3d 20 75 2e 61 6d 2e u.am.len = u.am.
1d56e 6e 46 69 65 6c 64 2a 35 20 2b 20 33 3b 0a 20 20 nField*5 + 3;.
1d56f 20 20 69 66 28 20 75 2e 61 6d 2e 6c 65 6e 20 3e if( u.am.len >
1d570 20 28 69 6e 74 29 75 2e 61 6d 2e 6f 66 66 73 65 (int)u.am.offse
1d571 74 20 29 20 75 2e 61 6d 2e 6c 65 6e 20 3d 20 28 t ) u.am.len = (
1d572 69 6e 74 29 75 2e 61 6d 2e 6f 66 66 73 65 74 3b int)u.am.offset;
1d573 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 4b 65 79 .. /* The Key
1d574 46 65 74 63 68 28 29 20 6f 72 20 44 61 74 61 46 Fetch() or DataF
1d575 65 74 63 68 28 29 20 61 62 6f 76 65 20 61 72 65 etch() above are
1d576 20 66 61 73 74 20 61 6e 64 20 77 69 6c 6c 20 67 fast and will g
1d577 65 74 20 74 68 65 20 65 6e 74 69 72 65 0a 20 20 et the entire.
1d578 20 20 2a 2a 20 72 65 63 6f 72 64 20 68 65 61 64 ** record head
1d579 65 72 20 69 6e 20 6d 6f 73 74 20 63 61 73 65 73 er in most cases
1d57a 2e 20 20 42 75 74 20 74 68 65 79 20 77 69 6c 6c . But they will
1d57b 20 66 61 69 6c 20 74 6f 20 67 65 74 20 74 68 65 fail to get the
1d57c 20 63 6f 6d 70 6c 65 74 65 0a 20 20 20 20 2a 2a complete. **
1d57d 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 69 record header i
1d57e 66 20 74 68 65 20 72 65 63 6f 72 64 20 68 65 61 f the record hea
1d57f 64 65 72 20 64 6f 65 73 20 6e 6f 74 20 66 69 74 der does not fit
1d580 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 on a single pag
1d581 65 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 e. ** in the
1d582 42 2d 54 72 65 65 2e 20 20 57 68 65 6e 20 74 68 B-Tree. When th
1d583 61 74 20 68 61 70 70 65 6e 73 2c 20 75 73 65 20 at happens, use
1d584 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 sqlite3VdbeMemFr
1d585 6f 6d 42 74 72 65 65 28 29 20 74 6f 0a 20 20 20 omBtree() to.
1d586 20 2a 2a 20 61 63 71 75 69 72 65 20 74 68 65 20 ** acquire the
1d587 63 6f 6d 70 6c 65 74 65 20 68 65 61 64 65 72 20 complete header
1d588 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 text.. */.
1d589 20 69 66 28 20 21 75 2e 61 6d 2e 7a 52 65 63 20 if( !u.am.zRec
1d58a 26 26 20 75 2e 61 6d 2e 61 76 61 69 6c 3c 75 2e && u.am.avail<u.
1d58b 61 6d 2e 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 am.len ){.
1d58c 75 2e 61 6d 2e 73 4d 65 6d 2e 66 6c 61 67 73 20 u.am.sMem.flags
1d58d 3d 20 30 3b 0a 20 20 20 20 20 20 75 2e 61 6d 2e = 0;. u.am.
1d58e 73 4d 65 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 20 sMem.db = 0;.
1d58f 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
1d590 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 dbeMemFromBtree(
1d591 75 2e 61 6d 2e 70 43 72 73 72 2c 20 30 2c 20 75 u.am.pCrsr, 0, u
1d592 2e 61 6d 2e 6c 65 6e 2c 20 75 2e 61 6d 2e 70 43 .am.len, u.am.pC
1d593 2d 3e 69 73 49 6e 64 65 78 2c 20 26 75 2e 61 6d ->isIndex, &u.am
1d594 2e 73 4d 65 6d 29 3b 0a 20 20 20 20 20 20 69 66 .sMem);. if
1d595 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1d596 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 ){. goto
1d597 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 op_column_out;.
1d598 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75 2e 61 }. u.a
1d599 6d 2e 7a 44 61 74 61 20 3d 20 75 2e 61 6d 2e 73 m.zData = u.am.s
1d59a 4d 65 6d 2e 7a 3b 0a 20 20 20 20 7d 0a 20 20 20 Mem.z;. }.
1d59b 20 75 2e 61 6d 2e 7a 45 6e 64 48 64 72 20 3d 20 u.am.zEndHdr =
1d59c 28 75 38 20 2a 29 26 75 2e 61 6d 2e 7a 44 61 74 (u8 *)&u.am.zDat
1d59d 61 5b 75 2e 61 6d 2e 6c 65 6e 5d 3b 0a 20 20 20 a[u.am.len];.
1d59e 20 75 2e 61 6d 2e 7a 49 64 78 20 3d 20 28 75 38 u.am.zIdx = (u8
1d59f 20 2a 29 26 75 2e 61 6d 2e 7a 44 61 74 61 5b 75 *)&u.am.zData[u
1d5a0 2e 61 6d 2e 73 7a 48 64 72 5d 3b 0a 0a 20 20 20 .am.szHdr];..
1d5a1 20 2f 2a 20 53 63 61 6e 20 74 68 65 20 68 65 61 /* Scan the hea
1d5a2 64 65 72 20 61 6e 64 20 75 73 65 20 69 74 20 74 der and use it t
1d5a3 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 75 2e o fill in the u.
1d5a4 61 6d 2e 61 54 79 70 65 5b 5d 20 61 6e 64 20 75 am.aType[] and u
1d5a5 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 5d 0a 20 20 .am.aOffset[].
1d5a6 20 20 2a 2a 20 61 72 72 61 79 73 2e 20 20 75 2e ** arrays. u.
1d5a7 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 69 5d am.aType[u.am.i]
1d5a8 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 will contain th
1d5a9 65 20 74 79 70 65 20 69 6e 74 65 67 65 72 20 66 e type integer f
1d5aa 6f 72 20 74 68 65 20 75 2e 61 6d 2e 69 2d 74 68 or the u.am.i-th
1d5ab 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 61 . ** column a
1d5ac 6e 64 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b nd u.am.aOffset[
1d5ad 75 2e 61 6d 2e 69 5d 20 77 69 6c 6c 20 63 6f 6e u.am.i] will con
1d5ae 74 61 69 6e 20 74 68 65 20 75 2e 61 6d 2e 6f 66 tain the u.am.of
1d5af 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 62 65 fset from the be
1d5b0 67 69 6e 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 6f ginning. ** o
1d5b1 66 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 f the record to
1d5b2 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 the start of the
1d5b3 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 75 2e data for the u.
1d5b4 61 6d 2e 69 2d 74 68 20 63 6f 6c 75 6d 6e 0a 20 am.i-th column.
1d5b5 20 20 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e 6f */. u.am.o
1d5b6 66 66 73 65 74 36 34 20 3d 20 75 2e 61 6d 2e 6f ffset64 = u.am.o
1d5b7 66 66 73 65 74 3b 0a 20 20 20 20 66 6f 72 28 75 ffset;. for(u
1d5b8 2e 61 6d 2e 69 3d 30 3b 20 75 2e 61 6d 2e 69 3c .am.i=0; u.am.i<
1d5b9 75 2e 61 6d 2e 6e 46 69 65 6c 64 3b 20 75 2e 61 u.am.nField; u.a
1d5ba 6d 2e 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 m.i++){. if
1d5bb 28 20 75 2e 61 6d 2e 7a 49 64 78 3c 75 2e 61 6d ( u.am.zIdx<u.am
1d5bc 2e 7a 45 6e 64 48 64 72 20 29 7b 0a 20 20 20 20 .zEndHdr ){.
1d5bd 20 20 20 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 u.am.aOffset
1d5be 5b 75 2e 61 6d 2e 69 5d 20 3d 20 28 75 33 32 29 [u.am.i] = (u32)
1d5bf 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 3b 0a 20 u.am.offset64;.
1d5c0 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 49 64 78 u.am.zIdx
1d5c1 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 += getVarint32(
1d5c2 75 2e 61 6d 2e 7a 49 64 78 2c 20 75 2e 61 6d 2e u.am.zIdx, u.am.
1d5c3 61 54 79 70 65 5b 75 2e 61 6d 2e 69 5d 29 3b 0a aType[u.am.i]);.
1d5c4 20 20 20 20 20 20 20 20 75 2e 61 6d 2e 6f 66 66 u.am.off
1d5c5 73 65 74 36 34 20 2b 3d 20 73 71 6c 69 74 65 33 set64 += sqlite3
1d5c6 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 VdbeSerialTypeLe
1d5c7 6e 28 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e 61 n(u.am.aType[u.a
1d5c8 6d 2e 69 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c m.i]);. }el
1d5c9 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 se{. /* I
1d5ca 66 20 75 2e 61 6d 2e 69 20 69 73 20 6c 65 73 73 f u.am.i is less
1d5cb 20 74 68 61 74 20 75 2e 61 6d 2e 6e 46 69 65 6c that u.am.nFiel
1d5cc 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 61 72 d, then there ar
1d5cd 65 20 6c 65 73 73 20 66 69 65 6c 64 73 20 69 6e e less fields in
1d5ce 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a this. **
1d5cf 20 72 65 63 6f 72 64 20 74 68 61 6e 20 53 65 74 record than Set
1d5d0 4e 75 6d 43 6f 6c 75 6d 6e 73 20 69 6e 64 69 63 NumColumns indic
1d5d1 61 74 65 64 20 74 68 65 72 65 20 61 72 65 20 63 ated there are c
1d5d2 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 0a 20 20 olumns in the.
1d5d3 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 2e 20 ** table.
1d5d4 53 65 74 20 74 68 65 20 75 2e 61 6d 2e 6f 66 66 Set the u.am.off
1d5d5 73 65 74 20 66 6f 72 20 61 6e 79 20 65 78 74 72 set for any extr
1d5d6 61 20 63 6f 6c 75 6d 6e 73 20 6e 6f 74 20 70 72 a columns not pr
1d5d7 65 73 65 6e 74 20 69 6e 0a 20 20 20 20 20 20 20 esent in.
1d5d8 20 2a 2a 20 74 68 65 20 72 65 63 6f 72 64 20 74 ** the record t
1d5d9 6f 20 30 2e 20 54 68 69 73 20 74 65 6c 6c 73 20 o 0. This tells
1d5da 63 6f 64 65 20 62 65 6c 6f 77 20 74 6f 20 73 74 code below to st
1d5db 6f 72 65 20 61 20 4e 55 4c 4c 0a 20 20 20 20 20 ore a NULL.
1d5dc 20 20 20 2a 2a 20 69 6e 73 74 65 61 64 20 6f 66 ** instead of
1d5dd 20 64 65 73 65 72 69 61 6c 69 7a 69 6e 67 20 61 deserializing a
1d5de 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 value from the
1d5df 72 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 20 20 record..
1d5e0 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e 61 6d 2e */. u.am.
1d5e1 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d 20 aOffset[u.am.i]
1d5e2 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 0;. }.
1d5e3 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 }. sqlite3Vd
1d5e4 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 75 2e beMemRelease(&u.
1d5e5 61 6d 2e 73 4d 65 6d 29 3b 0a 20 20 20 20 75 2e am.sMem);. u.
1d5e6 61 6d 2e 73 4d 65 6d 2e 66 6c 61 67 73 20 3d 20 am.sMem.flags =
1d5e7 4d 45 4d 5f 4e 75 6c 6c 3b 0a 0a 20 20 20 20 2f MEM_Null;.. /
1d5e8 2a 20 49 66 20 77 65 20 68 61 76 65 20 72 65 61 * If we have rea
1d5e9 64 20 6d 6f 72 65 20 68 65 61 64 65 72 20 64 61 d more header da
1d5ea 74 61 20 74 68 61 6e 20 77 61 73 20 63 6f 6e 74 ta than was cont
1d5eb 61 69 6e 65 64 20 69 6e 20 74 68 65 20 68 65 61 ained in the hea
1d5ec 64 65 72 2c 0a 20 20 20 20 2a 2a 20 6f 72 20 69 der,. ** or i
1d5ed 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 f the end of the
1d5ee 20 6c 61 73 74 20 66 69 65 6c 64 20 61 70 70 65 last field appe
1d5ef 61 72 73 20 74 6f 20 62 65 20 70 61 73 74 20 74 ars to be past t
1d5f0 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 20 he end of the.
1d5f1 20 20 2a 2a 20 72 65 63 6f 72 64 2c 20 6f 72 20 ** record, or
1d5f2 69 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 if the end of th
1d5f3 65 20 6c 61 73 74 20 66 69 65 6c 64 20 61 70 70 e last field app
1d5f4 65 61 72 73 20 74 6f 20 62 65 20 62 65 66 6f 72 ears to be befor
1d5f5 65 20 74 68 65 20 65 6e 64 0a 20 20 20 20 2a 2a e the end. **
1d5f6 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 28 of the record (
1d5f7 77 68 65 6e 20 61 6c 6c 20 66 69 65 6c 64 73 20 when all fields
1d5f8 70 72 65 73 65 6e 74 29 2c 20 74 68 65 6e 20 77 present), then w
1d5f9 65 20 6d 75 73 74 20 62 65 20 64 65 61 6c 69 6e e must be dealin
1d5fa 67 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 61 20 g. ** with a
1d5fb 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 corrupt database
1d5fc 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1d5fd 20 28 75 2e 61 6d 2e 7a 49 64 78 20 3e 20 75 2e (u.am.zIdx > u.
1d5fe 61 6d 2e 7a 45 6e 64 48 64 72 29 7c 7c 20 28 75 am.zEndHdr)|| (u
1d5ff 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 3e 20 75 .am.offset64 > u
1d600 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 29 .am.payloadSize)
1d601 0a 20 20 20 20 20 7c 7c 20 28 75 2e 61 6d 2e 7a . || (u.am.z
1d602 49 64 78 3d 3d 75 2e 61 6d 2e 7a 45 6e 64 48 64 Idx==u.am.zEndHd
1d603 72 20 26 26 20 75 2e 61 6d 2e 6f 66 66 73 65 74 r && u.am.offset
1d604 36 34 21 3d 28 75 36 34 29 75 2e 61 6d 2e 70 61 64!=(u64)u.am.pa
1d605 79 6c 6f 61 64 53 69 7a 65 29 20 29 7b 0a 20 20 yloadSize) ){.
1d606 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1d607 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1d608 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 goto op_colu
1d609 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 mn_out;. }.
1d60a 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20 }.. /* Get the
1d60b 63 6f 6c 75 6d 6e 20 69 6e 66 6f 72 6d 61 74 69 column informati
1d60c 6f 6e 2e 20 49 66 20 75 2e 61 6d 2e 61 4f 66 66 on. If u.am.aOff
1d60d 73 65 74 5b 75 2e 61 6d 2e 70 32 5d 20 69 73 20 set[u.am.p2] is
1d60e 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 0a 20 non-zero, then.
1d60f 20 2a 2a 20 64 65 73 65 72 69 61 6c 69 7a 65 20 ** deserialize
1d610 74 68 65 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 the value from t
1d611 68 65 20 72 65 63 6f 72 64 2e 20 49 66 20 75 2e he record. If u.
1d612 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e am.aOffset[u.am.
1d613 70 32 5d 20 69 73 20 7a 65 72 6f 2c 0a 20 20 2a p2] is zero,. *
1d614 2a 20 74 68 65 6e 20 74 68 65 72 65 20 61 72 65 * then there are
1d615 20 6e 6f 74 20 65 6e 6f 75 67 68 20 66 69 65 6c not enough fiel
1d616 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 ds in the record
1d617 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a to satisfy the.
1d618 20 20 2a 2a 20 72 65 71 75 65 73 74 2e 20 20 49 ** request. I
1d619 6e 20 74 68 69 73 20 63 61 73 65 2c 20 73 65 74 n this case, set
1d61a 20 74 68 65 20 76 61 6c 75 65 20 4e 55 4c 4c 20 the value NULL
1d61b 6f 72 20 74 6f 20 50 34 20 69 66 20 50 34 20 69 or to P4 if P4 i
1d61c 73 0a 20 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72 s. ** a pointer
1d61d 20 74 6f 20 61 20 4d 65 6d 20 6f 62 6a 65 63 74 to a Mem object
1d61e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61 .. */. if( u.a
1d61f 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70 m.aOffset[u.am.p
1d620 32 5d 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 2] ){. assert
1d621 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1d622 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 6d 2e );. if( u.am.
1d623 7a 52 65 63 20 29 7b 0a 20 20 20 20 20 20 73 71 zRec ){. sq
1d624 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
1d625 61 73 65 45 78 74 65 72 6e 61 6c 28 75 2e 61 6d aseExternal(u.am
1d626 2e 70 44 65 73 74 29 3b 0a 20 20 20 20 20 20 73 .pDest);. s
1d627 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
1d628 47 65 74 28 28 75 38 20 2a 29 26 75 2e 61 6d 2e Get((u8 *)&u.am.
1d629 7a 52 65 63 5b 75 2e 61 6d 2e 61 4f 66 66 73 65 zRec[u.am.aOffse
1d62a 74 5b 75 2e 61 6d 2e 70 32 5d 5d 2c 20 75 2e 61 t[u.am.p2]], u.a
1d62b 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 5d m.aType[u.am.p2]
1d62c 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a 20 , u.am.pDest);.
1d62d 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1d62e 75 2e 61 6d 2e 6c 65 6e 20 3d 20 73 71 6c 69 74 u.am.len = sqlit
1d62f 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
1d630 4c 65 6e 28 75 2e 61 6d 2e 61 54 79 70 65 5b 75 Len(u.am.aType[u
1d631 2e 61 6d 2e 70 32 5d 29 3b 0a 20 20 20 20 20 20 .am.p2]);.
1d632 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f sqlite3VdbeMemMo
1d633 76 65 28 26 75 2e 61 6d 2e 73 4d 65 6d 2c 20 75 ve(&u.am.sMem, u
1d634 2e 61 6d 2e 70 44 65 73 74 29 3b 0a 20 20 20 20 .am.pDest);.
1d635 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1d636 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 75 beMemFromBtree(u
1d637 2e 61 6d 2e 70 43 72 73 72 2c 20 75 2e 61 6d 2e .am.pCrsr, u.am.
1d638 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70 32 5d aOffset[u.am.p2]
1d639 2c 20 75 2e 61 6d 2e 6c 65 6e 2c 20 75 2e 61 6d , u.am.len, u.am
1d63a 2e 70 43 2d 3e 69 73 49 6e 64 65 78 2c 20 26 75 .pC->isIndex, &u
1d63b 2e 61 6d 2e 73 4d 65 6d 29 3b 0a 20 20 20 20 20 .am.sMem);.
1d63c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1d63d 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
1d63e 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 to op_column_out
1d63f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1d640 75 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 75 2e 61 u.am.zData = u.a
1d641 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 20 20 20 20 20 m.sMem.z;.
1d642 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1d643 6c 47 65 74 28 28 75 38 2a 29 75 2e 61 6d 2e 7a lGet((u8*)u.am.z
1d644 44 61 74 61 2c 20 75 2e 61 6d 2e 61 54 79 70 65 Data, u.am.aType
1d645 5b 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e [u.am.p2], u.am.
1d646 70 44 65 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 pDest);. }.
1d647 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 65 6e u.am.pDest->en
1d648 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 c = encoding;.
1d649 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 }else{. if( p
1d64a 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4d Op->p4type==P4_M
1d64b 45 4d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 EM ){. sqli
1d64c 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f te3VdbeMemShallo
1d64d 77 43 6f 70 79 28 75 2e 61 6d 2e 70 44 65 73 74 wCopy(u.am.pDest
1d64e 2c 20 70 4f 70 2d 3e 70 34 2e 70 4d 65 6d 2c 20 , pOp->p4.pMem,
1d64f 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 MEM_Static);.
1d650 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 }else{. as
1d651 73 65 72 74 28 20 75 2e 61 6d 2e 70 44 65 73 74 sert( u.am.pDest
1d652 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c ->flags&MEM_Null
1d653 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 );. }. }..
1d654 20 2f 2a 20 49 66 20 77 65 20 64 79 6e 61 6d 69 /* If we dynami
1d655 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 cally allocated
1d656 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 space to hold th
1d657 65 20 64 61 74 61 20 28 69 6e 20 74 68 65 0a 20 e data (in the.
1d658 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 4d ** sqlite3VdbeM
1d659 65 6d 46 72 6f 6d 42 74 72 65 65 28 29 20 63 61 emFromBtree() ca
1d65a 6c 6c 20 61 62 6f 76 65 29 20 74 68 65 6e 20 74 ll above) then t
1d65b 72 61 6e 73 66 65 72 20 63 6f 6e 74 72 6f 6c 20 ransfer control
1d65c 6f 66 20 74 68 61 74 0a 20 20 2a 2a 20 64 79 6e of that. ** dyn
1d65d 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 amically allocat
1d65e 65 64 20 73 70 61 63 65 20 6f 76 65 72 20 74 6f ed space over to
1d65f 20 74 68 65 20 75 2e 61 6d 2e 70 44 65 73 74 20 the u.am.pDest
1d660 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20 structure.. **
1d661 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20 This prevents a
1d662 6d 65 6d 6f 72 79 20 63 6f 70 79 2e 0a 20 20 2a memory copy.. *
1d663 2f 0a 20 20 69 66 28 20 75 2e 61 6d 2e 73 4d 65 /. if( u.am.sMe
1d664 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 m.zMalloc ){.
1d665 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 73 4d assert( u.am.sM
1d666 65 6d 2e 7a 3d 3d 75 2e 61 6d 2e 73 4d 65 6d 2e em.z==u.am.sMem.
1d667 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 61 zMalloc );. a
1d668 73 73 65 72 74 28 20 21 28 75 2e 61 6d 2e 70 44 ssert( !(u.am.pD
1d669 65 73 74 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d est->flags & MEM
1d66a 5f 44 79 6e 29 20 29 3b 0a 20 20 20 20 61 73 73 _Dyn) );. ass
1d66b 65 72 74 28 20 21 28 75 2e 61 6d 2e 70 44 65 73 ert( !(u.am.pDes
1d66c 74 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f t->flags & (MEM_
1d66d 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 20 7c Blob|MEM_Str)) |
1d66e 7c 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 3d | u.am.pDest->z=
1d66f 3d 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 20 29 3b 0a =u.am.sMem.z );.
1d670 20 20 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e u.am.pDest->
1d671 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 flags &= ~(MEM_E
1d672 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 phem|MEM_Static)
1d673 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 44 65 73 74 ;. u.am.pDest
1d674 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 ->flags |= MEM_T
1d675 65 72 6d 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 44 erm;. u.am.pD
1d676 65 73 74 2d 3e 7a 20 3d 20 75 2e 61 6d 2e 73 4d est->z = u.am.sM
1d677 65 6d 2e 7a 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 em.z;. u.am.p
1d678 44 65 73 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 Dest->zMalloc =
1d679 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f u.am.sMem.zMallo
1d67a 63 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 c;. }.. rc = s
1d67b 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b qlite3VdbeMemMak
1d67c 65 57 72 69 74 65 61 62 6c 65 28 75 2e 61 6d 2e eWriteable(u.am.
1d67d 70 44 65 73 74 29 3b 0a 0a 6f 70 5f 63 6f 6c 75 pDest);..op_colu
1d67e 6d 6e 5f 6f 75 74 3a 0a 20 20 55 50 44 41 54 45 mn_out:. UPDATE
1d67f 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 75 2e _MAX_BLOBSIZE(u.
1d680 61 6d 2e 70 44 65 73 74 29 3b 0a 20 20 52 45 47 am.pDest);. REG
1d681 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1d682 3e 70 33 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29 >p3, u.am.pDest)
1d683 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1d684 20 4f 70 63 6f 64 65 3a 20 41 66 66 69 6e 69 74 Opcode: Affinit
1d685 79 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a y P1 P2 * P4 *.*
1d686 2a 0a 2a 2a 20 41 70 70 6c 79 20 61 66 66 69 6e *.** Apply affin
1d687 69 74 69 65 73 20 74 6f 20 61 20 72 61 6e 67 65 ities to a range
1d688 20 6f 66 20 50 32 20 72 65 67 69 73 74 65 72 73 of P2 registers
1d689 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 50 starting with P
1d68a 31 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 1..**.** P4 is a
1d68b 20 73 74 72 69 6e 67 20 74 68 61 74 20 69 73 20 string that is
1d68c 50 32 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f P2 characters lo
1d68d 6e 67 2e 20 54 68 65 20 6e 74 68 20 63 68 61 72 ng. The nth char
1d68e 61 63 74 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 acter of the.**
1d68f 73 74 72 69 6e 67 20 69 6e 64 69 63 61 74 65 73 string indicates
1d690 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 the column affi
1d691 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64 nity that should
1d692 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 be used for the
1d693 20 6e 74 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 nth.** memory c
1d694 65 6c 6c 20 69 6e 20 74 68 65 20 72 61 6e 67 65 ell in the range
1d695 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 66 66 ..*/.case OP_Aff
1d696 69 6e 69 74 79 3a 20 7b 0a 23 69 66 20 30 20 20 inity: {.#if 0
1d697 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1d698 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1d699 61 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 41 an */. char *zA
1d69a 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54 68 ffinity; /* Th
1d69b 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65 e affinity to be
1d69c 20 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 4d 65 applied */. Me
1d69d 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20 20 m *pData0;
1d69e 20 2f 2a 20 46 69 72 73 74 20 72 65 67 69 73 74 /* First regist
1d69f 65 72 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 er to which to a
1d6a0 70 70 6c 79 20 61 66 66 69 6e 69 74 79 20 2a 2f pply affinity */
1d6a1 0a 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20 . Mem *pLast;
1d6a2 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 72 65 /* Last re
1d6a3 67 69 73 74 65 72 20 74 6f 20 77 68 69 63 68 20 gister to which
1d6a4 74 6f 20 61 70 70 6c 79 20 61 66 66 69 6e 69 74 to apply affinit
1d6a5 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 63 y */. Mem *pRec
1d6a6 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 ; /* Cur
1d6a7 72 65 6e 74 20 72 65 67 69 73 74 65 72 20 2a 2f rent register */
1d6a8 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1d6a9 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1d6aa 20 69 6e 74 6f 20 75 2e 61 6e 20 2a 2f 0a 0a 20 into u.an */..
1d6ab 20 75 2e 61 6e 2e 7a 41 66 66 69 6e 69 74 79 20 u.an.zAffinity
1d6ac 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 75 = pOp->p4.z;. u
1d6ad 2e 61 6e 2e 70 44 61 74 61 30 20 3d 20 26 70 2d .an.pData0 = &p-
1d6ae 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a >aMem[pOp->p1];.
1d6af 20 20 75 2e 61 6e 2e 70 4c 61 73 74 20 3d 20 26 u.an.pLast = &
1d6b0 75 2e 61 6e 2e 70 44 61 74 61 30 5b 70 4f 70 2d u.an.pData0[pOp-
1d6b1 3e 70 32 2d 31 5d 3b 0a 20 20 66 6f 72 28 75 2e >p2-1];. for(u.
1d6b2 61 6e 2e 70 52 65 63 3d 75 2e 61 6e 2e 70 44 61 an.pRec=u.an.pDa
1d6b3 74 61 30 3b 20 75 2e 61 6e 2e 70 52 65 63 3c 3d ta0; u.an.pRec<=
1d6b4 75 2e 61 6e 2e 70 4c 61 73 74 3b 20 75 2e 61 6e u.an.pLast; u.an
1d6b5 2e 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 45 78 .pRec++){. Ex
1d6b6 70 61 6e 64 42 6c 6f 62 28 75 2e 61 6e 2e 70 52 pandBlob(u.an.pR
1d6b7 65 63 29 3b 0a 20 20 20 20 61 70 70 6c 79 41 66 ec);. applyAf
1d6b8 66 69 6e 69 74 79 28 75 2e 61 6e 2e 70 52 65 63 finity(u.an.pRec
1d6b9 2c 20 75 2e 61 6e 2e 7a 41 66 66 69 6e 69 74 79 , u.an.zAffinity
1d6ba 5b 75 2e 61 6e 2e 70 52 65 63 2d 75 2e 61 6e 2e [u.an.pRec-u.an.
1d6bb 70 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64 69 6e pData0], encodin
1d6bc 67 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b g);. }. break;
1d6bd 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d .}../* Opcode: M
1d6be 61 6b 65 52 65 63 6f 72 64 20 50 31 20 50 32 20 akeRecord P1 P2
1d6bf 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f P3 P4 *.**.** Co
1d6c0 6e 76 65 72 74 20 50 32 20 72 65 67 69 73 74 65 nvert P2 registe
1d6c1 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 rs beginning wit
1d6c2 68 20 50 31 20 69 6e 74 6f 20 61 20 73 69 6e 67 h P1 into a sing
1d6c3 6c 65 20 65 6e 74 72 79 0a 2a 2a 20 73 75 69 74 le entry.** suit
1d6c4 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73 20 able for use as
1d6c5 61 20 64 61 74 61 20 72 65 63 6f 72 64 20 69 6e a data record in
1d6c6 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c a database tabl
1d6c7 65 20 6f 72 20 61 73 20 61 20 6b 65 79 0a 2a 2a e or as a key.**
1d6c8 20 69 6e 20 61 6e 20 69 6e 64 65 78 2e 20 20 54 in an index. T
1d6c9 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 he details of th
1d6ca 65 20 66 6f 72 6d 61 74 20 61 72 65 20 69 72 72 e format are irr
1d6cb 65 6c 65 76 61 6e 74 20 61 73 20 6c 6f 6e 67 20 elevant as long
1d6cc 61 73 0a 2a 2a 20 74 68 65 20 4f 50 5f 43 6f 6c as.** the OP_Col
1d6cd 75 6d 6e 20 6f 70 63 6f 64 65 20 63 61 6e 20 64 umn opcode can d
1d6ce 65 63 6f 64 65 20 74 68 65 20 72 65 63 6f 72 64 ecode the record
1d6cf 20 6c 61 74 65 72 2e 0a 2a 2a 20 52 65 66 65 72 later..** Refer
1d6d0 20 74 6f 20 73 6f 75 72 63 65 20 63 6f 64 65 20 to source code
1d6d1 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 74 68 65 comments for the
1d6d2 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 details of the
1d6d3 72 65 63 6f 72 64 0a 2a 2a 20 66 6f 72 6d 61 74 record.** format
1d6d4 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 62 ..**.** P4 may b
1d6d5 65 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 e a string that
1d6d6 69 73 20 50 32 20 63 68 61 72 61 63 74 65 72 73 is P2 characters
1d6d7 20 6c 6f 6e 67 2e 20 20 54 68 65 20 6e 74 68 20 long. The nth
1d6d8 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 character of the
1d6d9 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 63 .** string indic
1d6da 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 ates the column
1d6db 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 affinity that sh
1d6dc 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 ould be used for
1d6dd 20 74 68 65 20 6e 74 68 0a 2a 2a 20 66 69 65 6c the nth.** fiel
1d6de 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b d of the index k
1d6df 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 ey..**.** The ma
1d6e0 70 70 69 6e 67 20 66 72 6f 6d 20 63 68 61 72 61 pping from chara
1d6e1 63 74 65 72 20 74 6f 20 61 66 66 69 6e 69 74 79 cter to affinity
1d6e2 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65 is given by the
1d6e3 20 53 51 4c 49 54 45 5f 41 46 46 5f 0a 2a 2a 20 SQLITE_AFF_.**
1d6e4 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 64 20 69 macros defined i
1d6e5 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a n sqliteInt.h..*
1d6e6 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 4e 55 *.** If P4 is NU
1d6e7 4c 4c 20 74 68 65 6e 20 61 6c 6c 20 69 6e 64 65 LL then all inde
1d6e8 78 20 66 69 65 6c 64 73 20 68 61 76 65 20 74 68 x fields have th
1d6e9 65 20 61 66 66 69 6e 69 74 79 20 4e 4f 4e 45 2e e affinity NONE.
1d6ea 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 61 6b 65 .*/.case OP_Make
1d6eb 52 65 63 6f 72 64 3a 20 7b 0a 23 69 66 20 30 20 Record: {.#if 0
1d6ec 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1d6ed 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1d6ee 2e 61 6f 20 2a 2f 0a 20 20 75 38 20 2a 7a 4e 65 .ao */. u8 *zNe
1d6ef 77 52 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20 wRecord;
1d6f0 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 /* A buffer to h
1d6f1 6f 6c 64 20 74 68 65 20 64 61 74 61 20 66 6f 72 old the data for
1d6f2 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 the new record
1d6f3 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 63 3b 20 */. Mem *pRec;
1d6f4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1d6f5 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f he new record */
1d6f6 0a 20 20 75 36 34 20 6e 44 61 74 61 3b 20 20 20 . u64 nData;
1d6f7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
1d6f8 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
1d6f9 64 61 74 61 20 73 70 61 63 65 20 2a 2f 0a 20 20 data space */.
1d6fa 69 6e 74 20 6e 48 64 72 3b 20 20 20 20 20 20 20 int nHdr;
1d6fb 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1d6fc 20 6f 66 20 62 79 74 65 73 20 6f 66 20 68 65 61 of bytes of hea
1d6fd 64 65 72 20 73 70 61 63 65 20 2a 2f 0a 20 20 69 der space */. i
1d6fe 36 34 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 64 nByte;
1d6ff 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 73 70 /* Data sp
1d700 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 ace required for
1d701 20 74 68 69 73 20 72 65 63 6f 72 64 20 2a 2f 0a this record */.
1d702 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 int nZero;
1d703 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1d704 65 72 20 6f 66 20 7a 65 72 6f 20 62 79 74 65 73 er of zero bytes
1d705 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
1d706 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 he record */. i
1d707 6e 74 20 6e 56 61 72 69 6e 74 3b 20 20 20 20 20 nt nVarint;
1d708 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1d709 6f 66 20 62 79 74 65 73 20 69 6e 20 61 20 76 61 of bytes in a va
1d70a 72 69 6e 74 20 2a 2f 0a 20 20 75 33 32 20 73 65 rint */. u32 se
1d70b 72 69 61 6c 5f 74 79 70 65 3b 20 20 20 20 20 20 rial_type;
1d70c 20 2f 2a 20 54 79 70 65 20 66 69 65 6c 64 20 2a /* Type field *
1d70d 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61 30 3b /. Mem *pData0;
1d70e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
1d70f 72 73 74 20 66 69 65 6c 64 20 74 6f 20 62 65 20 rst field to be
1d710 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74 68 combined into th
1d711 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 e record */. Me
1d712 6d 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 20 20 m *pLast;
1d713 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 66 69 65 /* Last fie
1d714 6c 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 ld of the record
1d715 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 */. int nField
1d716 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
1d717 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 Number of fields
1d718 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a in the record *
1d719 2f 0a 20 20 63 68 61 72 20 2a 7a 41 66 66 69 6e /. char *zAffin
1d71a 69 74 79 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 ity; /* Th
1d71b 65 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e e affinity strin
1d71c 67 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 g for the record
1d71d 20 2a 2f 0a 20 20 69 6e 74 20 66 69 6c 65 5f 66 */. int file_f
1d71e 6f 72 6d 61 74 3b 20 20 20 20 20 20 20 2f 2a 20 ormat; /*
1d71f 46 69 6c 65 20 66 6f 72 6d 61 74 20 74 6f 20 75 File format to u
1d720 73 65 20 66 6f 72 20 65 6e 63 6f 64 69 6e 67 20 se for encoding
1d721 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 */. int i;
1d722 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
1d723 70 61 63 65 20 75 73 65 64 20 69 6e 20 7a 4e 65 pace used in zNe
1d724 77 52 65 63 6f 72 64 5b 5d 20 2a 2f 0a 20 20 69 wRecord[] */. i
1d725 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 nt len;
1d726 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 /* Length
1d727 6f 66 20 61 20 66 69 65 6c 64 20 2a 2f 0a 23 65 of a field */.#e
1d728 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1d729 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1d72a 74 6f 20 75 2e 61 6f 20 2a 2f 0a 0a 20 20 2f 2a to u.ao */.. /*
1d72b 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20 72 65 Assuming the re
1d72c 63 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20 4e 20 cord contains N
1d72d 66 69 65 6c 64 73 2c 20 74 68 65 20 72 65 63 6f fields, the reco
1d72e 72 64 20 66 6f 72 6d 61 74 20 6c 6f 6f 6b 73 0a rd format looks.
1d72f 20 20 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a ** like this:.
1d730 20 20 2a 2a 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d **. ** ------
1d731 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d732 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d733 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d734 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d735 2d 2d 0a 20 20 2a 2a 20 7c 20 68 64 72 2d 73 69 --. ** | hdr-si
1d736 7a 65 20 7c 20 74 79 70 65 20 30 20 7c 20 74 79 ze | type 0 | ty
1d737 70 65 20 31 20 7c 20 2e 2e 2e 20 7c 20 74 79 70 pe 1 | ... | typ
1d738 65 20 4e 2d 31 20 7c 20 64 61 74 61 30 20 7c 20 e N-1 | data0 |
1d739 2e 2e 2e 20 7c 20 64 61 74 61 20 4e 2d 31 20 7c ... | data N-1 |
1d73a 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d . ** ----------
1d73b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d73c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d73d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d73e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 --------------.
1d73f 20 2a 2a 0a 20 20 2a 2a 20 44 61 74 61 28 30 29 **. ** Data(0)
1d740 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72 is taken from r
1d741 65 67 69 73 74 65 72 20 50 31 2e 20 20 44 61 74 egister P1. Dat
1d742 61 28 31 29 20 63 6f 6d 65 73 20 66 72 6f 6d 20 a(1) comes from
1d743 72 65 67 69 73 74 65 72 20 50 31 2b 31 0a 20 20 register P1+1.
1d744 2a 2a 20 61 6e 64 20 73 6f 20 66 72 6f 74 68 2e ** and so froth.
1d745 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 63 68 20 . **. ** Each
1d746 74 79 70 65 20 66 69 65 6c 64 20 69 73 20 61 20 type field is a
1d747 76 61 72 69 6e 74 20 72 65 70 72 65 73 65 6e 74 varint represent
1d748 69 6e 67 20 74 68 65 20 73 65 72 69 61 6c 20 74 ing the serial t
1d749 79 70 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 ype of the. **
1d74a 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 64 61 corresponding da
1d74b 74 61 20 65 6c 65 6d 65 6e 74 20 28 73 65 65 20 ta element (see
1d74c 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1d74d 6c 54 79 70 65 28 29 29 2e 20 54 68 65 0a 20 20 lType()). The.
1d74e 2a 2a 20 68 64 72 2d 73 69 7a 65 20 66 69 65 6c ** hdr-size fiel
1d74f 64 20 69 73 20 61 6c 73 6f 20 61 20 76 61 72 69 d is also a vari
1d750 6e 74 20 77 68 69 63 68 20 69 73 20 74 68 65 20 nt which is the
1d751 6f 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 offset from the
1d752 62 65 67 69 6e 6e 69 6e 67 0a 20 20 2a 2a 20 6f beginning. ** o
1d753 66 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 f the record to
1d754 64 61 74 61 30 2e 0a 20 20 2a 2f 0a 20 20 75 2e data0.. */. u.
1d755 61 6f 2e 6e 44 61 74 61 20 3d 20 30 3b 20 20 20 ao.nData = 0;
1d756 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1d757 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 of bytes of data
1d758 20 73 70 61 63 65 20 2a 2f 0a 20 20 75 2e 61 6f space */. u.ao
1d759 2e 6e 48 64 72 20 3d 20 30 3b 20 20 20 20 20 20 .nHdr = 0;
1d75a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1d75b 20 62 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 bytes of header
1d75c 20 73 70 61 63 65 20 2a 2f 0a 20 20 75 2e 61 6f space */. u.ao
1d75d 2e 6e 42 79 74 65 20 3d 20 30 3b 20 20 20 20 20 .nByte = 0;
1d75e 20 20 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63 /* Data spac
1d75f 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 e required for t
1d760 68 69 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 his record */.
1d761 75 2e 61 6f 2e 6e 5a 65 72 6f 20 3d 20 30 3b 20 u.ao.nZero = 0;
1d762 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1d763 72 20 6f 66 20 7a 65 72 6f 20 62 79 74 65 73 20 r of zero bytes
1d764 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 at the end of th
1d765 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 2e e record */. u.
1d766 61 6f 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d ao.nField = pOp-
1d767 3e 70 31 3b 0a 20 20 75 2e 61 6f 2e 7a 41 66 66 >p1;. u.ao.zAff
1d768 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 34 2e inity = pOp->p4.
1d769 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 z;. assert( u.a
1d76a 6f 2e 6e 46 69 65 6c 64 3e 30 20 26 26 20 70 4f o.nField>0 && pO
1d76b 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e p->p2>0 && pOp->
1d76c 70 32 2b 75 2e 61 6f 2e 6e 46 69 65 6c 64 3c 3d p2+u.ao.nField<=
1d76d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 75 p->nMem+1 );. u
1d76e 2e 61 6f 2e 70 44 61 74 61 30 20 3d 20 26 70 2d .ao.pData0 = &p-
1d76f 3e 61 4d 65 6d 5b 75 2e 61 6f 2e 6e 46 69 65 6c >aMem[u.ao.nFiel
1d770 64 5d 3b 0a 20 20 75 2e 61 6f 2e 6e 46 69 65 6c d];. u.ao.nFiel
1d771 64 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 d = pOp->p2;. u
1d772 2e 61 6f 2e 70 4c 61 73 74 20 3d 20 26 75 2e 61 .ao.pLast = &u.a
1d773 6f 2e 70 44 61 74 61 30 5b 75 2e 61 6f 2e 6e 46 o.pData0[u.ao.nF
1d774 69 65 6c 64 2d 31 5d 3b 0a 20 20 75 2e 61 6f 2e ield-1];. u.ao.
1d775 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 70 2d file_format = p-
1d776 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 >minWriteFileFor
1d777 6d 61 74 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20 mat;.. /* Loop
1d778 74 68 72 6f 75 67 68 20 74 68 65 20 65 6c 65 6d through the elem
1d779 65 6e 74 73 20 74 68 61 74 20 77 69 6c 6c 20 6d ents that will m
1d77a 61 6b 65 20 75 70 20 74 68 65 20 72 65 63 6f 72 ake up the recor
1d77b 64 20 74 6f 20 66 69 67 75 72 65 0a 20 20 2a 2a d to figure. **
1d77c 20 6f 75 74 20 68 6f 77 20 6d 75 63 68 20 73 70 out how much sp
1d77d 61 63 65 20 69 73 20 72 65 71 75 69 72 65 64 20 ace is required
1d77e 66 6f 72 20 74 68 65 20 6e 65 77 20 72 65 63 6f for the new reco
1d77f 72 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 75 rd.. */. for(u
1d780 2e 61 6f 2e 70 52 65 63 3d 75 2e 61 6f 2e 70 44 .ao.pRec=u.ao.pD
1d781 61 74 61 30 3b 20 75 2e 61 6f 2e 70 52 65 63 3c ata0; u.ao.pRec<
1d782 3d 75 2e 61 6f 2e 70 4c 61 73 74 3b 20 75 2e 61 =u.ao.pLast; u.a
1d783 6f 2e 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 69 o.pRec++){. i
1d784 66 28 20 75 2e 61 6f 2e 7a 41 66 66 69 6e 69 74 f( u.ao.zAffinit
1d785 79 20 29 7b 0a 20 20 20 20 20 20 61 70 70 6c 79 y ){. apply
1d786 41 66 66 69 6e 69 74 79 28 75 2e 61 6f 2e 70 52 Affinity(u.ao.pR
1d787 65 63 2c 20 75 2e 61 6f 2e 7a 41 66 66 69 6e 69 ec, u.ao.zAffini
1d788 74 79 5b 75 2e 61 6f 2e 70 52 65 63 2d 75 2e 61 ty[u.ao.pRec-u.a
1d789 6f 2e 70 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64 o.pData0], encod
1d78a 69 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ing);. }.
1d78b 69 66 28 20 75 2e 61 6f 2e 70 52 65 63 2d 3e 66 if( u.ao.pRec->f
1d78c 6c 61 67 73 26 4d 45 4d 5f 5a 65 72 6f 20 26 26 lags&MEM_Zero &&
1d78d 20 75 2e 61 6f 2e 70 52 65 63 2d 3e 6e 3e 30 20 u.ao.pRec->n>0
1d78e 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1d78f 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f VdbeMemExpandBlo
1d790 62 28 75 2e 61 6f 2e 70 52 65 63 29 3b 0a 20 20 b(u.ao.pRec);.
1d791 20 20 7d 0a 20 20 20 20 75 2e 61 6f 2e 73 65 72 }. u.ao.ser
1d792 69 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 ial_type = sqlit
1d793 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
1d794 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e 61 6f (u.ao.pRec, u.ao
1d795 2e 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 .file_format);.
1d796 20 20 20 75 2e 61 6f 2e 6c 65 6e 20 3d 20 73 71 u.ao.len = sq
1d797 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 lite3VdbeSerialT
1d798 79 70 65 4c 65 6e 28 75 2e 61 6f 2e 73 65 72 69 ypeLen(u.ao.seri
1d799 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 75 2e al_type);. u.
1d79a 61 6f 2e 6e 44 61 74 61 20 2b 3d 20 75 2e 61 6f ao.nData += u.ao
1d79b 2e 6c 65 6e 3b 0a 20 20 20 20 75 2e 61 6f 2e 6e .len;. u.ao.n
1d79c 48 64 72 20 2b 3d 20 73 71 6c 69 74 65 33 56 61 Hdr += sqlite3Va
1d79d 72 69 6e 74 4c 65 6e 28 75 2e 61 6f 2e 73 65 72 rintLen(u.ao.ser
1d79e 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 69 ial_type);. i
1d79f 66 28 20 75 2e 61 6f 2e 70 52 65 63 2d 3e 66 6c f( u.ao.pRec->fl
1d7a0 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 ags & MEM_Zero )
1d7a1 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 {. /* Only
1d7a2 70 75 72 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 pure zero-filled
1d7a3 20 42 4c 4f 42 73 20 63 61 6e 20 62 65 20 69 6e BLOBs can be in
1d7a4 70 75 74 20 74 6f 20 74 68 69 73 20 4f 70 63 6f put to this Opco
1d7a5 64 65 2e 0a 20 20 20 20 20 20 2a 2a 20 57 65 20 de.. ** We
1d7a6 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 62 6c 6f do not allow blo
1d7a7 62 73 20 77 69 74 68 20 61 20 70 72 65 66 69 78 bs with a prefix
1d7a8 20 61 6e 64 20 61 20 7a 65 72 6f 2d 66 69 6c 6c and a zero-fill
1d7a9 65 64 20 74 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 ed tail. */.
1d7aa 20 20 75 2e 61 6f 2e 6e 5a 65 72 6f 20 2b 3d 20 u.ao.nZero +=
1d7ab 75 2e 61 6f 2e 70 52 65 63 2d 3e 75 2e 6e 5a 65 u.ao.pRec->u.nZe
1d7ac 72 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 ro;. }else if
1d7ad 28 20 75 2e 61 6f 2e 6c 65 6e 20 29 7b 0a 20 20 ( u.ao.len ){.
1d7ae 20 20 20 20 75 2e 61 6f 2e 6e 5a 65 72 6f 20 3d u.ao.nZero =
1d7af 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 0;. }. }..
1d7b0 20 2f 2a 20 41 64 64 20 74 68 65 20 69 6e 69 74 /* Add the init
1d7b1 69 61 6c 20 68 65 61 64 65 72 20 76 61 72 69 6e ial header varin
1d7b2 74 20 61 6e 64 20 74 6f 74 61 6c 20 74 68 65 20 t and total the
1d7b3 73 69 7a 65 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e size */. u.ao.n
1d7b4 48 64 72 20 2b 3d 20 75 2e 61 6f 2e 6e 56 61 72 Hdr += u.ao.nVar
1d7b5 69 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 61 72 int = sqlite3Var
1d7b6 69 6e 74 4c 65 6e 28 75 2e 61 6f 2e 6e 48 64 72 intLen(u.ao.nHdr
1d7b7 29 3b 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e 56 );. if( u.ao.nV
1d7b8 61 72 69 6e 74 3c 73 71 6c 69 74 65 33 56 61 72 arint<sqlite3Var
1d7b9 69 6e 74 4c 65 6e 28 75 2e 61 6f 2e 6e 48 64 72 intLen(u.ao.nHdr
1d7ba 29 20 29 7b 0a 20 20 20 20 75 2e 61 6f 2e 6e 48 ) ){. u.ao.nH
1d7bb 64 72 2b 2b 3b 0a 20 20 7d 0a 20 20 75 2e 61 6f dr++;. }. u.ao
1d7bc 2e 6e 42 79 74 65 20 3d 20 75 2e 61 6f 2e 6e 48 .nByte = u.ao.nH
1d7bd 64 72 2b 75 2e 61 6f 2e 6e 44 61 74 61 2d 75 2e dr+u.ao.nData-u.
1d7be 61 6f 2e 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20 ao.nZero;. if(
1d7bf 75 2e 61 6f 2e 6e 42 79 74 65 3e 64 62 2d 3e 61 u.ao.nByte>db->a
1d7c0 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
1d7c1 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 IT_LENGTH] ){.
1d7c2 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a goto too_big;.
1d7c3 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 }.. /* Make s
1d7c4 75 72 65 20 74 68 65 20 6f 75 74 70 75 74 20 72 ure the output r
1d7c5 65 67 69 73 74 65 72 20 68 61 73 20 61 20 62 75 egister has a bu
1d7c6 66 66 65 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 ffer large enoug
1d7c7 68 20 74 6f 20 73 74 6f 72 65 0a 20 20 2a 2a 20 h to store. **
1d7c8 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 2e 20 the new record.
1d7c9 54 68 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 The output regis
1d7ca 74 65 72 20 28 70 4f 70 2d 3e 70 33 29 20 69 73 ter (pOp->p3) is
1d7cb 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 0a not allowed to.
1d7cc 20 20 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20 74 ** be one of t
1d7cd 68 65 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 he input registe
1d7ce 72 73 20 28 62 65 63 61 75 73 65 20 74 68 65 20 rs (because the
1d7cf 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 74 following call t
1d7d0 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 o. ** sqlite3Vd
1d7d1 62 65 4d 65 6d 47 72 6f 77 28 29 20 63 6f 75 6c beMemGrow() coul
1d7d2 64 20 63 6c 6f 62 62 65 72 20 74 68 65 20 76 61 d clobber the va
1d7d3 6c 75 65 20 62 65 66 6f 72 65 20 69 74 20 69 73 lue before it is
1d7d4 20 75 73 65 64 29 2e 0a 20 20 2a 2f 0a 20 20 61 used).. */. a
1d7d5 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 70 ssert( pOp->p3<p
1d7d6 4f 70 2d 3e 70 31 20 7c 7c 20 70 4f 70 2d 3e 70 Op->p1 || pOp->p
1d7d7 33 3e 3d 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 3>=pOp->p1+pOp->
1d7d8 70 32 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 p2 );. pOut = &
1d7d9 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d p->aMem[pOp->p3]
1d7da 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 ;. if( sqlite3V
1d7db 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c dbeMemGrow(pOut,
1d7dc 20 28 69 6e 74 29 75 2e 61 6f 2e 6e 42 79 74 65 (int)u.ao.nByte
1d7dd 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f , 0) ){. goto
1d7de 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 75 no_mem;. }. u
1d7df 2e 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64 20 3d .ao.zNewRecord =
1d7e0 20 28 75 38 20 2a 29 70 4f 75 74 2d 3e 7a 3b 0a (u8 *)pOut->z;.
1d7e1 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 . /* Write the
1d7e2 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f record */. u.ao
1d7e3 2e 69 20 3d 20 70 75 74 56 61 72 69 6e 74 33 32 .i = putVarint32
1d7e4 28 75 2e 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64 (u.ao.zNewRecord
1d7e5 2c 20 75 2e 61 6f 2e 6e 48 64 72 29 3b 0a 20 20 , u.ao.nHdr);.
1d7e6 66 6f 72 28 75 2e 61 6f 2e 70 52 65 63 3d 75 2e for(u.ao.pRec=u.
1d7e7 61 6f 2e 70 44 61 74 61 30 3b 20 75 2e 61 6f 2e ao.pData0; u.ao.
1d7e8 70 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c 61 73 74 pRec<=u.ao.pLast
1d7e9 3b 20 75 2e 61 6f 2e 70 52 65 63 2b 2b 29 7b 0a ; u.ao.pRec++){.
1d7ea 20 20 20 20 75 2e 61 6f 2e 73 65 72 69 61 6c 5f u.ao.serial_
1d7eb 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64 type = sqlite3Vd
1d7ec 62 65 53 65 72 69 61 6c 54 79 70 65 28 75 2e 61 beSerialType(u.a
1d7ed 6f 2e 70 52 65 63 2c 20 75 2e 61 6f 2e 66 69 6c o.pRec, u.ao.fil
1d7ee 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 20 20 75 e_format);. u
1d7ef 2e 61 6f 2e 69 20 2b 3d 20 70 75 74 56 61 72 69 .ao.i += putVari
1d7f0 6e 74 33 32 28 26 75 2e 61 6f 2e 7a 4e 65 77 52 nt32(&u.ao.zNewR
1d7f1 65 63 6f 72 64 5b 75 2e 61 6f 2e 69 5d 2c 20 75 ecord[u.ao.i], u
1d7f2 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 29 .ao.serial_type)
1d7f3 3b 20 20 20 20 20 20 2f 2a 20 73 65 72 69 61 6c ; /* serial
1d7f4 20 74 79 70 65 20 2a 2f 0a 20 20 7d 0a 20 20 66 type */. }. f
1d7f5 6f 72 28 75 2e 61 6f 2e 70 52 65 63 3d 75 2e 61 or(u.ao.pRec=u.a
1d7f6 6f 2e 70 44 61 74 61 30 3b 20 75 2e 61 6f 2e 70 o.pData0; u.ao.p
1d7f7 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c 61 73 74 3b Rec<=u.ao.pLast;
1d7f8 20 75 2e 61 6f 2e 70 52 65 63 2b 2b 29 7b 20 20 u.ao.pRec++){
1d7f9 2f 2a 20 73 65 72 69 61 6c 20 64 61 74 61 20 2a /* serial data *
1d7fa 2f 0a 20 20 20 20 75 2e 61 6f 2e 69 20 2b 3d 20 /. u.ao.i +=
1d7fb 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1d7fc 6c 50 75 74 28 26 75 2e 61 6f 2e 7a 4e 65 77 52 lPut(&u.ao.zNewR
1d7fd 65 63 6f 72 64 5b 75 2e 61 6f 2e 69 5d 2c 20 28 ecord[u.ao.i], (
1d7fe 69 6e 74 29 28 75 2e 61 6f 2e 6e 42 79 74 65 2d int)(u.ao.nByte-
1d7ff 75 2e 61 6f 2e 69 29 2c 20 75 2e 61 6f 2e 70 52 u.ao.i), u.ao.pR
1d800 65 63 2c 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72 ec,u.ao.file_for
1d801 6d 61 74 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 mat);. }. asse
1d802 72 74 28 20 75 2e 61 6f 2e 69 3d 3d 75 2e 61 6f rt( u.ao.i==u.ao
1d803 2e 6e 42 79 74 65 20 29 3b 0a 0a 20 20 61 73 73 .nByte );.. ass
1d804 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 ert( pOp->p3>0 &
1d805 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d & pOp->p3<=p->nM
1d806 65 6d 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 em );. pOut->n
1d807 3d 20 28 69 6e 74 29 75 2e 61 6f 2e 6e 42 79 74 = (int)u.ao.nByt
1d808 65 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 e;. pOut->flags
1d809 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45 = MEM_Blob | ME
1d80a 4d 5f 44 79 6e 3b 0a 20 20 70 4f 75 74 2d 3e 78 M_Dyn;. pOut->x
1d80b 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 75 Del = 0;. if( u
1d80c 2e 61 6f 2e 6e 5a 65 72 6f 20 29 7b 0a 20 20 20 .ao.nZero ){.
1d80d 20 70 4f 75 74 2d 3e 75 2e 6e 5a 65 72 6f 20 3d pOut->u.nZero =
1d80e 20 75 2e 61 6f 2e 6e 5a 65 72 6f 3b 0a 20 20 20 u.ao.nZero;.
1d80f 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 pOut->flags |=
1d810 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 7d 0a 20 20 MEM_Zero;. }.
1d811 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 pOut->enc = SQLI
1d812 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20 49 6e 20 TE_UTF8; /* In
1d813 63 61 73 65 20 74 68 65 20 62 6c 6f 62 20 69 73 case the blob is
1d814 20 65 76 65 72 20 63 6f 6e 76 65 72 74 65 64 20 ever converted
1d815 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20 52 45 47 to text */. REG
1d816 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1d817 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50 >p3, pOut);. UP
1d818 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a DATE_MAX_BLOBSIZ
1d819 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b E(pOut);. break
1d81a 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1d81b 43 6f 75 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 Count P1 P2 * *
1d81c 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68 *.**.** Store th
1d81d 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 e number of entr
1d81e 69 65 73 20 28 61 6e 20 69 6e 74 65 67 65 72 20 ies (an integer
1d81f 76 61 6c 75 65 29 20 69 6e 20 74 68 65 20 74 61 value) in the ta
1d820 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 0a 2a 2a ble or index .**
1d821 20 6f 70 65 6e 65 64 20 62 79 20 63 75 72 73 6f opened by curso
1d822 72 20 50 31 20 69 6e 20 72 65 67 69 73 74 65 72 r P1 in register
1d823 20 50 32 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 P2.*/.#ifndef S
1d824 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 QLITE_OMIT_BTREE
1d825 43 4f 55 4e 54 0a 63 61 73 65 20 4f 50 5f 43 6f COUNT.case OP_Co
1d826 75 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 2f unt: { /
1d827 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 * out2-prereleas
1d828 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c e */.#if 0 /* l
1d829 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1d82a 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 70 20 2a oved into u.ap *
1d82b 2f 0a 20 20 69 36 34 20 6e 45 6e 74 72 79 3b 0a /. i64 nEntry;.
1d82c 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 BtCursor *pCrs
1d82d 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 r;.#endif /* loc
1d82e 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1d82f 65 64 20 69 6e 74 6f 20 75 2e 61 70 20 2a 2f 0a ed into u.ap */.
1d830 0a 20 20 75 2e 61 70 2e 70 43 72 73 72 20 3d 20 . u.ap.pCrsr =
1d831 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 p->apCsr[pOp->p1
1d832 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 ]->pCursor;. if
1d833 28 20 75 2e 61 70 2e 70 43 72 73 72 20 29 7b 0a ( u.ap.pCrsr ){.
1d834 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1d835 42 74 72 65 65 43 6f 75 6e 74 28 75 2e 61 70 2e BtreeCount(u.ap.
1d836 70 43 72 73 72 2c 20 26 75 2e 61 70 2e 6e 45 6e pCrsr, &u.ap.nEn
1d837 74 72 79 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 try);. }else{.
1d838 20 20 20 75 2e 61 70 2e 6e 45 6e 74 72 79 20 3d u.ap.nEntry =
1d839 20 30 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 0;. }. pOut->
1d83a 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
1d83b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 . pOut->u.i = u
1d83c 2e 61 70 2e 6e 45 6e 74 72 79 3b 0a 20 20 62 72 .ap.nEntry;. br
1d83d 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f eak;.}.#endif../
1d83e 2a 20 4f 70 63 6f 64 65 3a 20 53 61 76 65 70 6f * Opcode: Savepo
1d83f 69 6e 74 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a int P1 * * P4 *.
1d840 2a 2a 0a 2a 2a 20 4f 70 65 6e 2c 20 72 65 6c 65 **.** Open, rele
1d841 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 ase or rollback
1d842 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 6e 61 the savepoint na
1d843 6d 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 med by parameter
1d844 20 50 34 2c 20 64 65 70 65 6e 64 69 6e 67 0a 2a P4, depending.*
1d845 2a 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f * on the value o
1d846 66 20 50 31 2e 20 54 6f 20 6f 70 65 6e 20 61 20 f P1. To open a
1d847 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 2c 20 50 new savepoint, P
1d848 31 3d 3d 30 2e 20 54 6f 20 72 65 6c 65 61 73 65 1==0. To release
1d849 20 28 63 6f 6d 6d 69 74 29 20 61 6e 0a 2a 2a 20 (commit) an.**
1d84a 65 78 69 73 74 69 6e 67 20 73 61 76 65 70 6f 69 existing savepoi
1d84b 6e 74 2c 20 50 31 3d 3d 31 2c 20 6f 72 20 74 6f nt, P1==1, or to
1d84c 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 20 65 78 69 rollback an exi
1d84d 73 74 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 20 sting savepoint
1d84e 50 31 3d 3d 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f P1==2..*/.case O
1d84f 50 5f 53 61 76 65 70 6f 69 6e 74 3a 20 7b 0a 23 P_Savepoint: {.#
1d850 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1d851 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1d852 6e 74 6f 20 75 2e 61 71 20 2a 2f 0a 20 20 69 6e nto u.aq */. in
1d853 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 20 t p1;
1d854 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d855 20 56 61 6c 75 65 20 6f 66 20 50 31 20 6f 70 65 Value of P1 ope
1d856 72 61 6e 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a rand */. char *
1d857 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 zName;
1d858 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
1d859 65 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 20 2a e of savepoint *
1d85a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 /. int nName;.
1d85b 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 4e 65 77 Savepoint *pNew
1d85c 3b 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 ;. Savepoint *p
1d85d 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 53 61 76 Savepoint;. Sav
1d85e 65 70 6f 69 6e 74 20 2a 70 54 6d 70 3b 0a 20 20 epoint *pTmp;.
1d85f 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 3b 0a int iSavepoint;.
1d860 20 20 69 6e 74 20 69 69 3b 0a 23 65 6e 64 69 66 int ii;.#endif
1d861 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1d862 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1d863 2e 61 71 20 2a 2f 0a 0a 20 20 75 2e 61 71 2e 70 .aq */.. u.aq.p
1d864 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 1 = pOp->p1;. u
1d865 2e 61 71 2e 7a 4e 61 6d 65 20 3d 20 70 4f 70 2d .aq.zName = pOp-
1d866 3e 70 34 2e 7a 3b 0a 0a 20 20 2f 2a 20 41 73 73 >p4.z;.. /* Ass
1d867 65 72 74 20 74 68 61 74 20 74 68 65 20 75 2e 61 ert that the u.a
1d868 71 2e 70 31 20 70 61 72 61 6d 65 74 65 72 20 69 q.p1 parameter i
1d869 73 20 76 61 6c 69 64 2e 20 41 6c 73 6f 20 74 68 s valid. Also th
1d86a 61 74 20 69 66 20 74 68 65 72 65 20 69 73 20 6e at if there is n
1d86b 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 74 72 61 6e o open. ** tran
1d86c 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 saction, then th
1d86d 65 72 65 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e ere cannot be an
1d86e 79 20 73 61 76 65 70 6f 69 6e 74 73 2e 0a 20 20 y savepoints..
1d86f 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d */. assert( db-
1d870 3e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c >pSavepoint==0 |
1d871 7c 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 | db->autoCommit
1d872 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
1d873 20 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f u.aq.p1==SAVEPO
1d874 49 4e 54 5f 42 45 47 49 4e 7c 7c 75 2e 61 71 2e INT_BEGIN||u.aq.
1d875 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 p1==SAVEPOINT_RE
1d876 4c 45 41 53 45 7c 7c 75 2e 61 71 2e 70 31 3d 3d LEASE||u.aq.p1==
1d877 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
1d878 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 CK );. assert(
1d879 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 7c db->pSavepoint |
1d87a 7c 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 | db->isTransact
1d87b 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 ionSavepoint==0
1d87c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 68 65 );. assert( che
1d87d 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 ckSavepointCount
1d87e 28 64 62 29 20 29 3b 0a 0a 20 20 69 66 28 20 75 (db) );.. if( u
1d87f 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e .aq.p1==SAVEPOIN
1d880 54 5f 42 45 47 49 4e 20 29 7b 0a 20 20 20 20 69 T_BEGIN ){. i
1d881 66 28 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 f( db->writeVdbe
1d882 43 6e 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f Cnt>0 ){. /
1d883 2a 20 41 20 6e 65 77 20 73 61 76 65 70 6f 69 6e * A new savepoin
1d884 74 20 63 61 6e 6e 6f 74 20 62 65 20 63 72 65 61 t cannot be crea
1d885 74 65 64 20 69 66 20 74 68 65 72 65 20 61 72 65 ted if there are
1d886 20 61 63 74 69 76 65 20 77 72 69 74 65 0a 20 20 active write.
1d887 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 ** statement
1d888 73 20 28 69 2e 65 2e 20 6f 70 65 6e 20 72 65 61 s (i.e. open rea
1d889 64 2f 77 72 69 74 65 20 69 6e 63 72 65 6d 65 6e d/write incremen
1d88a 74 61 6c 20 62 6c 6f 62 20 68 61 6e 64 6c 65 73 tal blob handles
1d88b 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 ).. */.
1d88c 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 sqlite3SetStri
1d88d 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 ng(&p->zErrMsg,
1d88e 64 62 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e db, "cannot open
1d88f 20 73 61 76 65 70 6f 69 6e 74 20 2d 20 22 0a 20 savepoint - ".
1d890 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61 74 "SQL stat
1d891 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 ements in progre
1d892 73 73 22 29 3b 0a 20 20 20 20 20 20 72 63 20 3d ss");. rc =
1d893 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
1d894 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 }else{. u
1d895 2e 61 71 2e 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 .aq.nName = sqli
1d896 74 65 33 53 74 72 6c 65 6e 33 30 28 75 2e 61 71 te3Strlen30(u.aq
1d897 2e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20 20 20 .zName);..
1d898 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 /* Create a new
1d899 73 61 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74 savepoint struct
1d89a 75 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e ure. */. u.
1d89b 61 71 2e 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 aq.pNew = sqlite
1d89c 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
1d89d 20 73 69 7a 65 6f 66 28 53 61 76 65 70 6f 69 6e sizeof(Savepoin
1d89e 74 29 2b 75 2e 61 71 2e 6e 4e 61 6d 65 2b 31 29 t)+u.aq.nName+1)
1d89f 3b 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 71 ;. if( u.aq
1d8a0 2e 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 .pNew ){.
1d8a1 20 75 2e 61 71 2e 70 4e 65 77 2d 3e 7a 4e 61 6d u.aq.pNew->zNam
1d8a2 65 20 3d 20 28 63 68 61 72 20 2a 29 26 75 2e 61 e = (char *)&u.a
1d8a3 71 2e 70 4e 65 77 5b 31 5d 3b 0a 20 20 20 20 20 q.pNew[1];.
1d8a4 20 20 20 6d 65 6d 63 70 79 28 75 2e 61 71 2e 70 memcpy(u.aq.p
1d8a5 4e 65 77 2d 3e 7a 4e 61 6d 65 2c 20 75 2e 61 71 New->zName, u.aq
1d8a6 2e 7a 4e 61 6d 65 2c 20 75 2e 61 71 2e 6e 4e 61 .zName, u.aq.nNa
1d8a7 6d 65 2b 31 29 3b 0a 0a 20 20 20 20 20 20 20 20 me+1);..
1d8a8 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e /* If there is n
1d8a9 6f 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 o open transacti
1d8aa 6f 6e 2c 20 74 68 65 6e 20 6d 61 72 6b 20 74 68 on, then mark th
1d8ab 69 73 20 61 73 20 61 20 73 70 65 63 69 61 6c 0a is as a special.
1d8ac 20 20 20 20 20 20 20 20 2a 2a 20 22 74 72 61 6e ** "tran
1d8ad 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e saction savepoin
1d8ae 74 22 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 t". */. i
1d8af 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 f( db->autoCommi
1d8b0 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 t ){. d
1d8b1 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 b->autoCommit =
1d8b2 30 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 0;. db-
1d8b3 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 >isTransactionSa
1d8b4 76 65 70 6f 69 6e 74 20 3d 20 31 3b 0a 20 20 20 vepoint = 1;.
1d8b5 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1d8b6 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 db->nSavep
1d8b7 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 oint++;.
1d8b8 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 }.. /* Li
1d8b9 6e 6b 20 74 68 65 20 6e 65 77 20 73 61 76 65 70 nk the new savep
1d8ba 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 20 64 61 oint into the da
1d8bb 74 61 62 61 73 65 20 68 61 6e 64 6c 65 27 73 20 tabase handle's
1d8bc 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 list. */.
1d8bd 20 75 2e 61 71 2e 70 4e 65 77 2d 3e 70 4e 65 78 u.aq.pNew->pNex
1d8be 74 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 t = db->pSavepoi
1d8bf 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e nt;. db->
1d8c0 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61 pSavepoint = u.a
1d8c1 71 2e 70 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 q.pNew;.
1d8c2 75 2e 61 71 2e 70 4e 65 77 2d 3e 6e 44 65 66 65 u.aq.pNew->nDefe
1d8c3 72 72 65 64 43 6f 6e 73 20 3d 20 64 62 2d 3e 6e rredCons = db->n
1d8c4 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20 DeferredCons;.
1d8c5 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 }. }. }e
1d8c6 6c 73 65 7b 0a 20 20 20 20 75 2e 61 71 2e 69 53 lse{. u.aq.iS
1d8c7 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 0a 20 avepoint = 0;..
1d8c8 20 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 6e /* Find the n
1d8c9 61 6d 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 amed savepoint.
1d8ca 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 If there is no s
1d8cb 75 63 68 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 uch savepoint, t
1d8cc 68 65 6e 20 61 6e 0a 20 20 20 20 2a 2a 20 61 6e hen an. ** an
1d8cd 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e error is return
1d8ce 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 ed to the user.
1d8cf 20 2a 2f 0a 20 20 20 20 66 6f 72 28 0a 20 20 20 */. for(.
1d8d0 20 20 20 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 u.aq.pSavepoi
1d8d1 6e 74 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f nt = db->pSavepo
1d8d2 69 6e 74 3b 0a 20 20 20 20 20 20 75 2e 61 71 2e int;. u.aq.
1d8d3 70 53 61 76 65 70 6f 69 6e 74 20 26 26 20 73 71 pSavepoint && sq
1d8d4 6c 69 74 65 33 53 74 72 49 43 6d 70 28 75 2e 61 lite3StrICmp(u.a
1d8d5 71 2e 70 53 61 76 65 70 6f 69 6e 74 2d 3e 7a 4e q.pSavepoint->zN
1d8d6 61 6d 65 2c 20 75 2e 61 71 2e 7a 4e 61 6d 65 29 ame, u.aq.zName)
1d8d7 3b 0a 20 20 20 20 20 20 75 2e 61 71 2e 70 53 61 ;. u.aq.pSa
1d8d8 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 vepoint = u.aq.p
1d8d9 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 Savepoint->pNext
1d8da 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 75 2e . ){. u.
1d8db 61 71 2e 69 53 61 76 65 70 6f 69 6e 74 2b 2b 3b aq.iSavepoint++;
1d8dc 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 . }. if( !
1d8dd 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 u.aq.pSavepoint
1d8de 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1d8df 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 SetString(&p->zE
1d8e0 72 72 4d 73 67 2c 20 64 62 2c 20 22 6e 6f 20 73 rrMsg, db, "no s
1d8e1 75 63 68 20 73 61 76 65 70 6f 69 6e 74 3a 20 25 uch savepoint: %
1d8e2 73 22 2c 20 75 2e 61 71 2e 7a 4e 61 6d 65 29 3b s", u.aq.zName);
1d8e3 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1d8e4 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65 TE_ERROR;. }e
1d8e5 6c 73 65 20 69 66 28 0a 20 20 20 20 20 20 20 20 lse if(.
1d8e6 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 db->writeVdbeCnt
1d8e7 3e 30 20 7c 7c 20 28 75 2e 61 71 2e 70 31 3d 3d >0 || (u.aq.p1==
1d8e8 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
1d8e9 43 4b 20 26 26 20 64 62 2d 3e 61 63 74 69 76 65 CK && db->active
1d8ea 56 64 62 65 43 6e 74 3e 31 29 0a 20 20 20 20 29 VdbeCnt>1). )
1d8eb 7b 0a 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73 {. /* It is
1d8ec 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f not possible to
1d8ed 20 72 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 release (commit
1d8ee 29 20 61 20 73 61 76 65 70 6f 69 6e 74 20 69 66 ) a savepoint if
1d8ef 20 74 68 65 72 65 20 61 72 65 0a 20 20 20 20 20 there are.
1d8f0 20 2a 2a 20 61 63 74 69 76 65 20 77 72 69 74 65 ** active write
1d8f1 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 statements. It
1d8f2 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
1d8f3 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 73 61 to rollback a sa
1d8f4 76 65 70 6f 69 6e 74 0a 20 20 20 20 20 20 2a 2a vepoint. **
1d8f5 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e if there are an
1d8f6 79 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 y active stateme
1d8f7 6e 74 73 20 61 74 20 61 6c 6c 2e 0a 20 20 20 20 nts at all..
1d8f8 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 */. sqlit
1d8f9 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1d8fa 7a 45 72 72 4d 73 67 2c 20 64 62 2c 0a 20 20 20 zErrMsg, db,.
1d8fb 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 25 73 20 "cannot %s
1d8fc 73 61 76 65 70 6f 69 6e 74 20 2d 20 53 51 4c 20 savepoint - SQL
1d8fd 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 statements in pr
1d8fe 6f 67 72 65 73 73 22 2c 0a 20 20 20 20 20 20 20 ogress",.
1d8ff 20 28 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 (u.aq.p1==SAVEP
1d900 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 3f 20 OINT_ROLLBACK ?
1d901 22 72 6f 6c 6c 62 61 63 6b 22 3a 20 22 72 65 6c "rollback": "rel
1d902 65 61 73 65 22 29 0a 20 20 20 20 20 20 29 3b 0a ease"). );.
1d903 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1d904 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 E_BUSY;. }els
1d905 65 7b 0a 0a 20 20 20 20 20 20 2f 2a 20 44 65 74 e{.. /* Det
1d906 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f ermine whether o
1d907 72 20 6e 6f 74 20 74 68 69 73 20 69 73 20 61 20 r not this is a
1d908 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 transaction save
1d909 70 6f 69 6e 74 2e 20 49 66 20 73 6f 2c 0a 20 20 point. If so,.
1d90a 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 69 73 20 ** and this
1d90b 69 73 20 61 20 52 45 4c 45 41 53 45 20 63 6f 6d is a RELEASE com
1d90c 6d 61 6e 64 2c 20 74 68 65 6e 20 74 68 65 20 63 mand, then the c
1d90d 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 urrent transacti
1d90e 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 63 on. ** is c
1d90f 6f 6d 6d 69 74 74 65 64 2e 0a 20 20 20 20 20 20 ommitted..
1d910 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 73 54 */. int isT
1d911 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 75 2e 61 ransaction = u.a
1d912 71 2e 70 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e q.pSavepoint->pN
1d913 65 78 74 3d 3d 30 20 26 26 20 64 62 2d 3e 69 73 ext==0 && db->is
1d914 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 TransactionSavep
1d915 6f 69 6e 74 3b 0a 20 20 20 20 20 20 69 66 28 20 oint;. if(
1d916 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 20 26 26 isTransaction &&
1d917 20 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f u.aq.p1==SAVEPO
1d918 49 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 INT_RELEASE ){.
1d919 20 20 20 20 20 20 20 69 66 28 20 28 72 63 20 3d if( (rc =
1d91a 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 65 63 sqlite3VdbeChec
1d91b 6b 46 6b 28 70 2c 20 31 29 29 21 3d 53 51 4c 49 kFk(p, 1))!=SQLI
1d91c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1d91d 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 goto vdbe_ret
1d91e 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 urn;. }.
1d91f 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 db->autoC
1d920 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 ommit = 1;.
1d921 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 if( sqlite3Vd
1d922 62 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c 49 54 beHalt(p)==SQLIT
1d923 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 E_BUSY ){.
1d924 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a p->pc = pc;.
1d925 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 db->au
1d926 74 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 toCommit = 0;.
1d927 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 p->rc =
1d928 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
1d929 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f ;. goto
1d92a 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 vdbe_return;.
1d92b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1d92c 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 6f db->isTransactio
1d92d 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a nSavepoint = 0;.
1d92e 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 2d 3e rc = p->
1d92f 72 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b rc;. }else{
1d930 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e 69 53 . u.aq.iS
1d931 61 76 65 70 6f 69 6e 74 20 3d 20 64 62 2d 3e 6e avepoint = db->n
1d932 53 61 76 65 70 6f 69 6e 74 20 2d 20 75 2e 61 71 Savepoint - u.aq
1d933 2e 69 53 61 76 65 70 6f 69 6e 74 20 2d 20 31 3b .iSavepoint - 1;
1d934 0a 20 20 20 20 20 20 20 20 66 6f 72 28 75 2e 61 . for(u.a
1d935 71 2e 69 69 3d 30 3b 20 75 2e 61 71 2e 69 69 3c q.ii=0; u.aq.ii<
1d936 64 62 2d 3e 6e 44 62 3b 20 75 2e 61 71 2e 69 69 db->nDb; u.aq.ii
1d937 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 ++){. r
1d938 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1d939 53 61 76 65 70 6f 69 6e 74 28 64 62 2d 3e 61 44 Savepoint(db->aD
1d93a 62 5b 75 2e 61 71 2e 69 69 5d 2e 70 42 74 2c 20 b[u.aq.ii].pBt,
1d93b 75 2e 61 71 2e 70 31 2c 20 75 2e 61 71 2e 69 53 u.aq.p1, u.aq.iS
1d93c 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 avepoint);.
1d93d 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1d93e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1d93f 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 goto abort
1d940 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 _due_to_error;.
1d941 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1d942 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
1d943 20 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f u.aq.p1==SAVEPO
1d944 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20 INT_ROLLBACK &&
1d945 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 (db->flags&SQLIT
1d946 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 29 E_InternChanges)
1d947 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 !=0 ){.
1d948 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 sqlite3ExpirePr
1d949 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 eparedStatements
1d94a 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 (db);.
1d94b 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 sqlite3ResetInte
1d94c 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 rnalSchema(db, 0
1d94d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
1d94e 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 52 }.. /* R
1d94f 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 egardless of whe
1d950 74 68 65 72 20 74 68 69 73 20 69 73 20 61 20 52 ther this is a R
1d951 45 4c 45 41 53 45 20 6f 72 20 52 4f 4c 4c 42 41 ELEASE or ROLLBA
1d952 43 4b 2c 20 64 65 73 74 72 6f 79 20 61 6c 6c 0a CK, destroy all.
1d953 20 20 20 20 20 20 2a 2a 20 73 61 76 65 70 6f 69 ** savepoi
1d954 6e 74 73 20 6e 65 73 74 65 64 20 69 6e 73 69 64 nts nested insid
1d955 65 20 6f 66 20 74 68 65 20 73 61 76 65 70 6f 69 e of the savepoi
1d956 6e 74 20 62 65 69 6e 67 20 6f 70 65 72 61 74 65 nt being operate
1d957 64 20 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 77 d on. */. w
1d958 68 69 6c 65 28 20 64 62 2d 3e 70 53 61 76 65 70 hile( db->pSavep
1d959 6f 69 6e 74 21 3d 75 2e 61 71 2e 70 53 61 76 65 oint!=u.aq.pSave
1d95a 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 point ){.
1d95b 20 75 2e 61 71 2e 70 54 6d 70 20 3d 20 64 62 2d u.aq.pTmp = db-
1d95c 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 >pSavepoint;.
1d95d 20 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70 6f db->pSavepo
1d95e 69 6e 74 20 3d 20 75 2e 61 71 2e 70 54 6d 70 2d int = u.aq.pTmp-
1d95f 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 >pNext;.
1d960 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1d961 2c 20 75 2e 61 71 2e 70 54 6d 70 29 3b 0a 20 20 , u.aq.pTmp);.
1d962 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 db->nSavep
1d963 6f 69 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a oint--;. }.
1d964 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 69 74 20 . /* If it
1d965 69 73 20 61 20 52 45 4c 45 41 53 45 2c 20 74 68 is a RELEASE, th
1d966 65 6e 20 64 65 73 74 72 6f 79 20 74 68 65 20 73 en destroy the s
1d967 61 76 65 70 6f 69 6e 74 20 62 65 69 6e 67 20 6f avepoint being o
1d968 70 65 72 61 74 65 64 20 6f 6e 0a 20 20 20 20 20 perated on.
1d969 20 2a 2a 20 74 6f 6f 2e 20 49 66 20 69 74 20 69 ** too. If it i
1d96a 73 20 61 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 2c s a ROLLBACK TO,
1d96b 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 6e 75 then set the nu
1d96c 6d 62 65 72 20 6f 66 20 64 65 66 65 72 72 65 64 mber of deferred
1d96d 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 . ** constr
1d96e 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 73 20 aint violations
1d96f 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 64 present in the d
1d970 61 74 61 62 61 73 65 20 74 6f 20 74 68 65 20 76 atabase to the v
1d971 61 6c 75 65 20 73 74 6f 72 65 64 0a 20 20 20 20 alue stored.
1d972 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 73 61 ** when the sa
1d973 76 65 70 6f 69 6e 74 20 77 61 73 20 63 72 65 61 vepoint was crea
1d974 74 65 64 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 ted. */. i
1d975 66 28 20 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 f( u.aq.p1==SAVE
1d976 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b POINT_RELEASE ){
1d977 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1d978 20 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 u.aq.pSavepoint
1d979 3d 3d 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 ==db->pSavepoint
1d97a 20 29 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e );. db->
1d97b 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61 pSavepoint = u.a
1d97c 71 2e 70 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e q.pSavepoint->pN
1d97d 65 78 74 3b 0a 20 20 20 20 20 20 20 20 73 71 6c ext;. sql
1d97e 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 75 ite3DbFree(db, u
1d97f 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 29 3b .aq.pSavepoint);
1d980 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73 . if( !is
1d981 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 Transaction ){.
1d982 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 db->nSa
1d983 76 65 70 6f 69 6e 74 2d 2d 3b 0a 20 20 20 20 20 vepoint--;.
1d984 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
1d985 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 44 {. db->nD
1d986 65 66 65 72 72 65 64 43 6f 6e 73 20 3d 20 75 2e eferredCons = u.
1d987 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e aq.pSavepoint->n
1d988 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20 DeferredCons;.
1d989 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1d98a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1d98b 4f 70 63 6f 64 65 3a 20 41 75 74 6f 43 6f 6d 6d Opcode: AutoComm
1d98c 69 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a it P1 P2 * * *.*
1d98d 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 64 61 74 *.** Set the dat
1d98e 61 62 61 73 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 abase auto-commi
1d98f 74 20 66 6c 61 67 20 74 6f 20 50 31 20 28 31 20 t flag to P1 (1
1d990 6f 72 20 30 29 2e 20 49 66 20 50 32 20 69 73 20 or 0). If P2 is
1d991 74 72 75 65 2c 20 72 6f 6c 6c 0a 2a 2a 20 62 61 true, roll.** ba
1d992 63 6b 20 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 ck any currently
1d993 20 61 63 74 69 76 65 20 62 74 72 65 65 20 74 72 active btree tr
1d994 61 6e 73 61 63 74 69 6f 6e 73 2e 20 49 66 20 74 ansactions. If t
1d995 68 65 72 65 20 61 72 65 20 61 6e 79 20 61 63 74 here are any act
1d996 69 76 65 0a 2a 2a 20 56 4d 73 20 28 61 70 61 72 ive.** VMs (apar
1d997 74 20 66 72 6f 6d 20 74 68 69 73 20 6f 6e 65 29 t from this one)
1d998 2c 20 74 68 65 6e 20 61 20 52 4f 4c 4c 42 41 43 , then a ROLLBAC
1d999 4b 20 66 61 69 6c 73 2e 20 20 41 20 43 4f 4d 4d K fails. A COMM
1d99a 49 54 20 66 61 69 6c 73 20 69 66 0a 2a 2a 20 74 IT fails if.** t
1d99b 68 65 72 65 20 61 72 65 20 61 63 74 69 76 65 20 here are active
1d99c 77 72 69 74 69 6e 67 20 56 4d 73 20 6f 72 20 61 writing VMs or a
1d99d 63 74 69 76 65 20 56 4d 73 20 74 68 61 74 20 75 ctive VMs that u
1d99e 73 65 20 73 68 61 72 65 64 20 63 61 63 68 65 2e se shared cache.
1d99f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 .**.** This inst
1d9a0 72 75 63 74 69 6f 6e 20 63 61 75 73 65 73 20 74 ruction causes t
1d9a1 68 65 20 56 4d 20 74 6f 20 68 61 6c 74 2e 0a 2a he VM to halt..*
1d9a2 2f 0a 63 61 73 65 20 4f 50 5f 41 75 74 6f 43 6f /.case OP_AutoCo
1d9a3 6d 6d 69 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f mmit: {.#if 0 /
1d9a4 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1d9a5 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1d9a6 72 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73 69 72 r */. int desir
1d9a7 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 edAutoCommit;.
1d9a8 69 6e 74 20 69 52 6f 6c 6c 62 61 63 6b 3b 0a 20 int iRollback;.
1d9a9 20 69 6e 74 20 74 75 72 6e 4f 6e 41 43 3b 0a 23 int turnOnAC;.#
1d9aa 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1d9ab 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1d9ac 6e 74 6f 20 75 2e 61 72 20 2a 2f 0a 0a 20 20 75 nto u.ar */.. u
1d9ad 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 .ar.desiredAutoC
1d9ae 6f 6d 6d 69 74 20 3d 20 70 4f 70 2d 3e 70 31 3b ommit = pOp->p1;
1d9af 0a 20 20 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 . u.ar.iRollbac
1d9b0 6b 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 k = pOp->p2;. u
1d9b1 2e 61 72 2e 74 75 72 6e 4f 6e 41 43 20 3d 20 75 .ar.turnOnAC = u
1d9b2 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 .ar.desiredAutoC
1d9b3 6f 6d 6d 69 74 20 26 26 20 21 64 62 2d 3e 61 75 ommit && !db->au
1d9b4 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 61 73 73 65 toCommit;. asse
1d9b5 72 74 28 20 75 2e 61 72 2e 64 65 73 69 72 65 64 rt( u.ar.desired
1d9b6 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c AutoCommit==1 ||
1d9b7 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 u.ar.desiredAut
1d9b8 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 3b 0a 20 20 oCommit==0 );.
1d9b9 61 73 73 65 72 74 28 20 75 2e 61 72 2e 64 65 73 assert( u.ar.des
1d9ba 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d iredAutoCommit==
1d9bb 31 20 7c 7c 20 75 2e 61 72 2e 69 52 6f 6c 6c 62 1 || u.ar.iRollb
1d9bc 61 63 6b 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 ack==0 );. asse
1d9bd 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 rt( db->activeVd
1d9be 62 65 43 6e 74 3e 30 20 29 3b 20 20 2f 2a 20 41 beCnt>0 ); /* A
1d9bf 74 20 6c 65 61 73 74 20 74 68 69 73 20 6f 6e 65 t least this one
1d9c0 20 56 4d 20 69 73 20 61 63 74 69 76 65 20 2a 2f VM is active */
1d9c1 0a 0a 20 20 69 66 28 20 75 2e 61 72 2e 74 75 72 .. if( u.ar.tur
1d9c2 6e 4f 6e 41 43 20 26 26 20 75 2e 61 72 2e 69 52 nOnAC && u.ar.iR
1d9c3 6f 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d 3e 61 ollback && db->a
1d9c4 63 74 69 76 65 56 64 62 65 43 6e 74 3e 31 20 29 ctiveVdbeCnt>1 )
1d9c5 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 {. /* If this
1d9c6 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 instruction imp
1d9c7 6c 65 6d 65 6e 74 73 20 61 20 52 4f 4c 4c 42 41 lements a ROLLBA
1d9c8 43 4b 20 61 6e 64 20 6f 74 68 65 72 20 56 4d 73 CK and other VMs
1d9c9 20 61 72 65 0a 20 20 20 20 2a 2a 20 73 74 69 6c are. ** stil
1d9ca 6c 20 72 75 6e 6e 69 6e 67 2c 20 61 6e 64 20 61 l running, and a
1d9cb 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
1d9cc 61 63 74 69 76 65 2c 20 72 65 74 75 72 6e 20 61 active, return a
1d9cd 6e 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 69 n error indicati
1d9ce 6e 67 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74 ng. ** that t
1d9cf 68 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 he other VMs mus
1d9d0 74 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 t complete first
1d9d1 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c .. */. sql
1d9d2 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 ite3SetString(&p
1d9d3 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 ->zErrMsg, db, "
1d9d4 63 61 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 cannot rollback
1d9d5 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a transaction - ".
1d9d6 20 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61 "SQL sta
1d9d7 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 tements in progr
1d9d8 65 73 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 ess");. rc =
1d9d9 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d SQLITE_BUSY;. }
1d9da 65 6c 73 65 20 69 66 28 20 75 2e 61 72 2e 74 75 else if( u.ar.tu
1d9db 72 6e 4f 6e 41 43 20 26 26 20 21 75 2e 61 72 2e rnOnAC && !u.ar.
1d9dc 69 52 6f 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d iRollback && db-
1d9dd 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e 30 20 >writeVdbeCnt>0
1d9de 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 ){. /* If thi
1d9df 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6d s instruction im
1d9e0 70 6c 65 6d 65 6e 74 73 20 61 20 43 4f 4d 4d 49 plements a COMMI
1d9e1 54 20 61 6e 64 20 6f 74 68 65 72 20 56 4d 73 20 T and other VMs
1d9e2 61 72 65 20 77 72 69 74 69 6e 67 0a 20 20 20 20 are writing.
1d9e3 2a 2a 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 ** return an err
1d9e4 6f 72 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 or indicating th
1d9e5 61 74 20 74 68 65 20 6f 74 68 65 72 20 56 4d 73 at the other VMs
1d9e6 20 6d 75 73 74 20 63 6f 6d 70 6c 65 74 65 20 66 must complete f
1d9e7 69 72 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 irst.. */.
1d9e8 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e sqlite3SetStrin
1d9e9 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 g(&p->zErrMsg, d
1d9ea 62 2c 20 22 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69 b, "cannot commi
1d9eb 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20 t transaction -
1d9ec 22 0a 20 20 20 20 20 20 20 20 22 53 51 4c 20 73 ". "SQL s
1d9ed 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f tatements in pro
1d9ee 67 72 65 73 73 22 29 3b 0a 20 20 20 20 72 63 20 gress");. rc
1d9ef 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 = SQLITE_BUSY;.
1d9f0 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 72 2e }else if( u.ar.
1d9f1 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 desiredAutoCommi
1d9f2 74 21 3d 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 t!=db->autoCommi
1d9f3 74 20 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 61 t ){. if( u.a
1d9f4 72 2e 69 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 r.iRollback ){.
1d9f5 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 assert( u.a
1d9f6 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d r.desiredAutoCom
1d9f7 6d 69 74 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 mit==1 );.
1d9f8 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 sqlite3RollbackA
1d9f9 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 64 62 ll(db);. db
1d9fa 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 ->autoCommit = 1
1d9fb 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
1d9fc 28 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 (rc = sqlite3Vdb
1d9fd 65 43 68 65 63 6b 46 6b 28 70 2c 20 31 29 29 21 eCheckFk(p, 1))!
1d9fe 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1d9ff 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 goto vdbe_re
1da00 74 75 72 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b turn;. }else{
1da01 0a 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 . db->autoC
1da02 6f 6d 6d 69 74 20 3d 20 28 75 38 29 75 2e 61 72 ommit = (u8)u.ar
1da03 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d .desiredAutoComm
1da04 69 74 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 it;. if( sq
1da05 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 lite3VdbeHalt(p)
1da06 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b ==SQLITE_BUSY ){
1da07 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 63 20 3d . p->pc =
1da08 20 70 63 3b 0a 20 20 20 20 20 20 20 20 64 62 2d pc;. db-
1da09 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 >autoCommit = (u
1da0a 38 29 28 31 2d 75 2e 61 72 2e 64 65 73 69 72 65 8)(1-u.ar.desire
1da0b 64 41 75 74 6f 43 6f 6d 6d 69 74 29 3b 0a 20 20 dAutoCommit);.
1da0c 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 p->rc = rc
1da0d 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a = SQLITE_BUSY;.
1da0e 20 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 goto vdb
1da0f 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 e_return;.
1da10 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 }. }. asse
1da11 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 rt( db->nStateme
1da12 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c nt==0 );. sql
1da13 69 74 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 ite3CloseSavepoi
1da14 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 69 66 28 nts(db);. if(
1da15 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f p->rc==SQLITE_O
1da16 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 K ){. rc =
1da17 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 SQLITE_DONE;.
1da18 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 }else{. rc
1da19 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b = SQLITE_ERROR;
1da1a 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 . }. goto
1da1b 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 7d vdbe_return;. }
1da1c 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 else{. sqlite
1da1d 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 3SetString(&p->z
1da1e 45 72 72 4d 73 67 2c 20 64 62 2c 0a 20 20 20 20 ErrMsg, db,.
1da1f 20 20 20 20 28 21 75 2e 61 72 2e 64 65 73 69 72 (!u.ar.desir
1da20 65 64 41 75 74 6f 43 6f 6d 6d 69 74 29 3f 22 63 edAutoCommit)?"c
1da21 61 6e 6e 6f 74 20 73 74 61 72 74 20 61 20 74 72 annot start a tr
1da22 61 6e 73 61 63 74 69 6f 6e 20 77 69 74 68 69 6e ansaction within
1da23 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 3a a transaction":
1da24 28 0a 20 20 20 20 20 20 20 20 28 75 2e 61 72 2e (. (u.ar.
1da25 69 52 6f 6c 6c 62 61 63 6b 29 3f 22 63 61 6e 6e iRollback)?"cann
1da26 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e 6f ot rollback - no
1da27 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
1da28 61 63 74 69 76 65 22 3a 0a 20 20 20 20 20 20 20 active":.
1da29 20 20 20 20 20 20 20 20 20 20 20 20 22 63 61 6e "can
1da2a 6e 6f 74 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f 20 not commit - no
1da2b 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 transaction is a
1da2c 63 74 69 76 65 22 29 29 3b 0a 0a 20 20 20 20 72 ctive"));.. r
1da2d 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
1da2e 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1da2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61 ../* Opcode: Tra
1da30 6e 73 61 63 74 69 6f 6e 20 50 31 20 50 32 20 2a nsaction P1 P2 *
1da31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e * *.**.** Begin
1da32 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 a transaction.
1da33 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e The transaction
1da34 20 65 6e 64 73 20 77 68 65 6e 20 61 20 43 6f 6d ends when a Com
1da35 6d 69 74 20 6f 72 20 52 6f 6c 6c 62 61 63 6b 0a mit or Rollback.
1da36 2a 2a 20 6f 70 63 6f 64 65 20 69 73 20 65 6e 63 ** opcode is enc
1da37 6f 75 6e 74 65 72 65 64 2e 20 20 44 65 70 65 6e ountered. Depen
1da38 64 69 6e 67 20 6f 6e 20 74 68 65 20 4f 4e 20 43 ding on the ON C
1da39 4f 4e 46 4c 49 43 54 20 73 65 74 74 69 6e 67 2c ONFLICT setting,
1da3a 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 the.** transact
1da3b 69 6f 6e 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 ion might also b
1da3c 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66 e rolled back if
1da3d 20 61 6e 20 65 72 72 6f 72 20 69 73 20 65 6e 63 an error is enc
1da3e 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 ountered..**.**
1da3f 50 31 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 P1 is the index
1da40 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1da41 66 69 6c 65 20 6f 6e 20 77 68 69 63 68 20 74 68 file on which th
1da42 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 e transaction is
1da43 0a 2a 2a 20 73 74 61 72 74 65 64 2e 20 20 49 6e .** started. In
1da44 64 65 78 20 30 20 69 73 20 74 68 65 20 6d 61 69 dex 0 is the mai
1da45 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 n database file
1da46 61 6e 64 20 69 6e 64 65 78 20 31 20 69 73 20 74 and index 1 is t
1da47 68 65 0a 2a 2a 20 66 69 6c 65 20 75 73 65 64 20 he.** file used
1da48 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 74 61 for temporary ta
1da49 62 6c 65 73 2e 20 20 49 6e 64 69 63 65 73 20 6f bles. Indices o
1da4a 66 20 32 20 6f 72 20 6d 6f 72 65 20 61 72 65 20 f 2 or more are
1da4b 75 73 65 64 20 66 6f 72 0a 2a 2a 20 61 74 74 61 used for.** atta
1da4c 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2e 0a ched databases..
1da4d 2a 2a 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 6e **.** If P2 is n
1da4e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 on-zero, then a
1da4f 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
1da50 6e 20 69 73 20 73 74 61 72 74 65 64 2e 20 20 41 n is started. A
1da51 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 RESERVED lock i
1da52 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 6f 6e s.** obtained on
1da53 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1da54 6c 65 20 77 68 65 6e 20 61 20 77 72 69 74 65 2d le when a write-
1da55 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 transaction is s
1da56 74 61 72 74 65 64 2e 20 20 4e 6f 0a 2a 2a 20 6f tarted. No.** o
1da57 74 68 65 72 20 70 72 6f 63 65 73 73 20 63 61 6e ther process can
1da58 20 73 74 61 72 74 20 61 6e 6f 74 68 65 72 20 77 start another w
1da59 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
1da5a 20 77 68 69 6c 65 20 74 68 69 73 20 74 72 61 6e while this tran
1da5b 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 6e saction is.** un
1da5c 64 65 72 77 61 79 2e 20 20 53 74 61 72 74 69 6e derway. Startin
1da5d 67 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 g a write transa
1da5e 63 74 69 6f 6e 20 61 6c 73 6f 20 63 72 65 61 74 ction also creat
1da5f 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f es a rollback jo
1da60 75 72 6e 61 6c 2e 20 41 0a 2a 2a 20 77 72 69 74 urnal. A.** writ
1da61 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 e transaction mu
1da62 73 74 20 62 65 20 73 74 61 72 74 65 64 20 62 65 st be started be
1da63 66 6f 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 fore any changes
1da64 20 63 61 6e 20 62 65 20 6d 61 64 65 20 74 6f 20 can be made to
1da65 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e the.** database.
1da66 20 20 49 66 20 50 32 20 69 73 20 32 20 6f 72 20 If P2 is 2 or
1da67 67 72 65 61 74 65 72 20 74 68 65 6e 20 61 6e 20 greater then an
1da68 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 69 EXCLUSIVE lock i
1da69 73 20 61 6c 73 6f 20 6f 62 74 61 69 6e 65 64 0a s also obtained.
1da6a 2a 2a 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 0a ** on the file..
1da6b 2a 2a 0a 2a 2a 20 49 66 20 61 20 77 72 69 74 65 **.** If a write
1da6c 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 -transaction is
1da6d 73 74 61 72 74 65 64 20 61 6e 64 20 74 68 65 20 started and the
1da6e 56 64 62 65 2e 75 73 65 73 53 74 6d 74 4a 6f 75 Vdbe.usesStmtJou
1da6f 72 6e 61 6c 20 66 6c 61 67 20 69 73 0a 2a 2a 20 rnal flag is.**
1da70 74 72 75 65 20 28 74 68 69 73 20 66 6c 61 67 20 true (this flag
1da71 69 73 20 73 65 74 20 69 66 20 74 68 65 20 56 64 is set if the Vd
1da72 62 65 20 6d 61 79 20 6d 6f 64 69 66 79 20 6d 6f be may modify mo
1da73 72 65 20 74 68 61 6e 20 6f 6e 65 20 72 6f 77 20 re than one row
1da74 61 6e 64 20 6d 61 79 0a 2a 2a 20 74 68 72 6f 77 and may.** throw
1da75 20 61 6e 20 41 42 4f 52 54 20 65 78 63 65 70 74 an ABORT except
1da76 69 6f 6e 29 2c 20 61 20 73 74 61 74 65 6d 65 6e ion), a statemen
1da77 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 t transaction ma
1da78 79 20 61 6c 73 6f 20 62 65 20 6f 70 65 6e 65 64 y also be opened
1da79 2e 0a 2a 2a 20 4d 6f 72 65 20 73 70 65 63 69 66 ..** More specif
1da7a 69 63 61 6c 6c 79 2c 20 61 20 73 74 61 74 65 6d ically, a statem
1da7b 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
1da7c 69 73 20 6f 70 65 6e 65 64 20 69 66 66 20 74 68 is opened iff th
1da7d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f e database.** co
1da7e 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 75 72 72 nnection is curr
1da7f 65 6e 74 6c 79 20 6e 6f 74 20 69 6e 20 61 75 74 ently not in aut
1da80 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 20 6f 72 ocommit mode, or
1da81 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6f 74 if there are ot
1da82 68 65 72 0a 2a 2a 20 61 63 74 69 76 65 20 73 74 her.** active st
1da83 61 74 65 6d 65 6e 74 73 2e 20 41 20 73 74 61 74 atements. A stat
1da84 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
1da85 6e 20 61 6c 6c 6f 77 73 20 74 68 65 20 61 66 66 n allows the aff
1da86 65 63 74 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 ects of this.**
1da87 56 44 42 45 20 74 6f 20 62 65 20 72 6f 6c 6c 65 VDBE to be rolle
1da88 64 20 62 61 63 6b 20 61 66 74 65 72 20 61 6e 20 d back after an
1da89 65 72 72 6f 72 20 77 69 74 68 6f 75 74 20 68 61 error without ha
1da8a 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 20 62 61 63 ving to roll bac
1da8b 6b 20 74 68 65 0a 2a 2a 20 65 6e 74 69 72 65 20 k the.** entire
1da8c 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 transaction. If
1da8d 6e 6f 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f no error is enco
1da8e 75 6e 74 65 72 65 64 2c 20 74 68 65 20 73 74 61 untered, the sta
1da8f 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
1da90 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 61 75 74 6f 6d on.** will autom
1da91 61 74 69 63 61 6c 6c 79 20 63 6f 6d 6d 69 74 20 atically commit
1da92 77 68 65 6e 20 74 68 65 20 56 44 42 45 20 68 61 when the VDBE ha
1da93 6c 74 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 lts..**.** If P2
1da94 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 61 is zero, then a
1da95 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 read-lock is ob
1da96 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 tained on the da
1da97 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a tabase file..*/.
1da98 63 61 73 65 20 4f 50 5f 54 72 61 6e 73 61 63 74 case OP_Transact
1da99 69 6f 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a ion: {.#if 0 /*
1da9a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1da9b 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 73 moved into u.as
1da9c 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 */. Btree *pBt
1da9d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1da9e 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1da9f 64 20 69 6e 74 6f 20 75 2e 61 73 20 2a 2f 0a 0a d into u.as */..
1daa0 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1daa1 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 1>=0 && pOp->p1<
1daa2 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 db->nDb );. ass
1daa3 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 ert( (p->btreeMa
1daa4 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 sk & (1<<pOp->p1
1daa5 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e 61 73 2e ))!=0 );. u.as.
1daa6 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 70 4f pBt = db->aDb[pO
1daa7 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a 0a 20 20 69 p->p1].pBt;.. i
1daa8 66 28 20 75 2e 61 73 2e 70 42 74 20 29 7b 0a 20 f( u.as.pBt ){.
1daa9 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1daaa 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 75 treeBeginTrans(u
1daab 2e 61 73 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 32 .as.pBt, pOp->p2
1daac 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
1daad 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 QLITE_BUSY ){.
1daae 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a p->pc = pc;.
1daaf 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 p->rc = rc
1dab0 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a = SQLITE_BUSY;.
1dab1 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f goto vdbe_
1dab2 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 return;. }.
1dab3 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1dab4 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 _OK ){. got
1dab5 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
1dab6 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 rror;. }..
1dab7 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 26 26 20 if( pOp->p2 &&
1dab8 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e p->usesStmtJourn
1dab9 61 6c 0a 20 20 20 20 20 26 26 20 28 64 62 2d 3e al. && (db->
1daba 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c autoCommit==0 ||
1dabb 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 db->activeVdbeC
1dabc 6e 74 3e 31 29 0a 20 20 20 20 29 7b 0a 20 20 20 nt>1). ){.
1dabd 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
1dabe 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 e3BtreeIsInTrans
1dabf 28 75 2e 61 73 2e 70 42 74 29 20 29 3b 0a 20 20 (u.as.pBt) );.
1dac0 20 20 20 20 69 66 28 20 70 2d 3e 69 53 74 61 74 if( p->iStat
1dac1 65 6d 65 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 ement==0 ){.
1dac2 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e assert( db->
1dac3 6e 53 74 61 74 65 6d 65 6e 74 3e 3d 30 20 26 26 nStatement>=0 &&
1dac4 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3e db->nSavepoint>
1dac5 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 64 62 =0 );. db
1dac6 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2b 2b 3b 0a ->nStatement++;.
1dac7 20 20 20 20 20 20 20 20 70 2d 3e 69 53 74 61 74 p->iStat
1dac8 65 6d 65 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76 ement = db->nSav
1dac9 65 70 6f 69 6e 74 20 2b 20 64 62 2d 3e 6e 53 74 epoint + db->nSt
1daca 61 74 65 6d 65 6e 74 3b 0a 20 20 20 20 20 20 7d atement;. }
1dacb 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1dacc 74 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d te3BtreeBeginStm
1dacd 74 28 75 2e 61 73 2e 70 42 74 2c 20 70 2d 3e 69 t(u.as.pBt, p->i
1dace 53 74 61 74 65 6d 65 6e 74 29 3b 0a 0a 20 20 20 Statement);..
1dacf 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 /* Store the
1dad0 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 current value of
1dad1 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 the database ha
1dad2 6e 64 6c 65 73 20 64 65 66 65 72 72 65 64 20 63 ndles deferred c
1dad3 6f 6e 73 74 72 61 69 6e 74 0a 20 20 20 20 20 20 onstraint.
1dad4 2a 2a 20 63 6f 75 6e 74 65 72 2e 20 49 66 20 74 ** counter. If t
1dad5 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 he statement tra
1dad6 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 nsaction needs t
1dad7 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b o be rolled back
1dad8 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 76 ,. ** the v
1dad9 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 75 alue of this cou
1dada 6e 74 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65 nter needs to be
1dadb 20 72 65 73 74 6f 72 65 64 20 74 6f 6f 2e 20 20 restored too.
1dadc 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 6e 53 74 6d */. p->nStm
1dadd 74 44 65 66 43 6f 6e 73 20 3d 20 64 62 2d 3e 6e tDefCons = db->n
1dade 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20 DeferredCons;.
1dadf 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b }. }. break;
1dae0 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 .}../* Opcode: R
1dae1 65 61 64 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 eadCookie P1 P2
1dae2 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 P3 * *.**.** Rea
1dae3 64 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 d cookie number
1dae4 50 33 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 P3 from database
1dae5 20 50 31 20 61 6e 64 20 77 72 69 74 65 20 69 74 P1 and write it
1dae6 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 into register P
1dae7 32 2e 0a 2a 2a 20 50 33 3d 3d 31 20 69 73 20 74 2..** P3==1 is t
1dae8 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f he schema versio
1dae9 6e 2e 20 20 50 33 3d 3d 32 20 69 73 20 74 68 65 n. P3==2 is the
1daea 20 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 database format
1daeb 2e 0a 2a 2a 20 50 33 3d 3d 33 20 69 73 20 74 68 ..** P3==3 is th
1daec 65 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61 e recommended pa
1daed 67 65 72 20 63 61 63 68 65 20 73 69 7a 65 2c 20 ger cache size,
1daee 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 and so forth. P
1daef 31 3d 3d 30 20 69 73 0a 2a 2a 20 74 68 65 20 6d 1==0 is.** the m
1daf0 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c ain database fil
1daf1 65 20 61 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 e and P1==1 is t
1daf2 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1daf3 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a used to store.*
1daf4 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c * temporary tabl
1daf5 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 es..**.** There
1daf6 6d 75 73 74 20 62 65 20 61 20 72 65 61 64 2d 6c must be a read-l
1daf7 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
1daf8 61 73 65 20 28 65 69 74 68 65 72 20 61 20 74 72 ase (either a tr
1daf9 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 ansaction.** mus
1dafa 74 20 62 65 20 73 74 61 72 74 65 64 20 6f 72 20 t be started or
1dafb 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e there must be an
1dafc 20 6f 70 65 6e 20 63 75 72 73 6f 72 29 20 62 65 open cursor) be
1dafd 66 6f 72 65 0a 2a 2a 20 65 78 65 63 75 74 69 6e fore.** executin
1dafe 67 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 g this instructi
1daff 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 on..*/.case OP_R
1db00 65 61 64 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 eadCookie: {
1db01 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 /* ou
1db02 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f t2-prerelease */
1db03 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1db04 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1db05 20 69 6e 74 6f 20 75 2e 61 74 20 2a 2f 0a 20 20 into u.at */.
1db06 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 69 6e 74 int iMeta;. int
1db07 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 43 6f 6f iDb;. int iCoo
1db08 6b 69 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c kie;.#endif /* l
1db09 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1db0a 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 74 20 2a oved into u.at *
1db0b 2f 0a 0a 20 20 75 2e 61 74 2e 69 44 62 20 3d 20 /.. u.at.iDb =
1db0c 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 74 2e pOp->p1;. u.at.
1db0d 69 43 6f 6f 6b 69 65 20 3d 20 70 4f 70 2d 3e 70 iCookie = pOp->p
1db0e 33 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 3;. assert( pOp
1db0f 2d 3e 70 33 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 ->p3<SQLITE_N_BT
1db10 52 45 45 5f 4d 45 54 41 20 29 3b 0a 20 20 61 73 REE_META );. as
1db11 73 65 72 74 28 20 75 2e 61 74 2e 69 44 62 3e 3d sert( u.at.iDb>=
1db12 30 20 26 26 20 75 2e 61 74 2e 69 44 62 3c 64 62 0 && u.at.iDb<db
1db13 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 ->nDb );. asser
1db14 74 28 20 64 62 2d 3e 61 44 62 5b 75 2e 61 74 2e t( db->aDb[u.at.
1db15 69 44 62 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20 iDb].pBt!=0 );.
1db16 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 assert( (p->btr
1db17 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 75 2e 61 eeMask & (1<<u.a
1db18 74 2e 69 44 62 29 29 21 3d 30 20 29 3b 0a 0a 20 t.iDb))!=0 );..
1db19 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
1db1a 4d 65 74 61 28 64 62 2d 3e 61 44 62 5b 75 2e 61 Meta(db->aDb[u.a
1db1b 74 2e 69 44 62 5d 2e 70 42 74 2c 20 75 2e 61 74 t.iDb].pBt, u.at
1db1c 2e 69 43 6f 6f 6b 69 65 2c 20 28 75 33 32 20 2a .iCookie, (u32 *
1db1d 29 26 75 2e 61 74 2e 69 4d 65 74 61 29 3b 0a 20 )&u.at.iMeta);.
1db1e 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 pOut->u.i = u.a
1db1f 74 2e 69 4d 65 74 61 3b 0a 20 20 4d 65 6d 53 65 t.iMeta;. MemSe
1db20 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
1db21 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 MEM_Int);. brea
1db22 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1db23 20 53 65 74 43 6f 6f 6b 69 65 20 50 31 20 50 32 SetCookie P1 P2
1db24 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 P3 * *.**.** Wr
1db25 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 ite the content
1db26 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 28 of register P3 (
1db27 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61 interpreted as a
1db28 6e 20 69 6e 74 65 67 65 72 29 0a 2a 2a 20 69 6e n integer).** in
1db29 74 6f 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 to cookie number
1db2a 20 50 32 20 6f 66 20 64 61 74 61 62 61 73 65 20 P2 of database
1db2b 50 31 2e 20 20 50 32 3d 3d 31 20 69 73 20 74 68 P1. P2==1 is th
1db2c 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e e schema version
1db2d 2e 20 20 0a 2a 2a 20 50 32 3d 3d 32 20 69 73 20 . .** P2==2 is
1db2e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 the database for
1db2f 6d 61 74 2e 20 50 32 3d 3d 33 20 69 73 20 74 68 mat. P2==3 is th
1db30 65 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61 e recommended pa
1db31 67 65 72 20 63 61 63 68 65 20 0a 2a 2a 20 73 69 ger cache .** si
1db32 7a 65 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 ze, and so forth
1db33 2e 20 20 50 31 3d 3d 30 20 69 73 20 74 68 65 20 . P1==0 is the
1db34 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 main database fi
1db35 6c 65 20 61 6e 64 20 50 31 3d 3d 31 20 69 73 20 le and P1==1 is
1db36 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 the .** database
1db37 20 66 69 6c 65 20 75 73 65 64 20 74 6f 20 73 74 file used to st
1db38 6f 72 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 ore temporary ta
1db39 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 bles..**.** A tr
1db3a 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 ansaction must b
1db3b 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 e started before
1db3c 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 executing this
1db3d 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 opcode..*/.case
1db3e 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 3a 20 7b 20 OP_SetCookie: {
1db3f 20 20 20 20 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a /* in3 */.
1db40 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1db41 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1db42 69 6e 74 6f 20 75 2e 61 75 20 2a 2f 0a 20 20 44 into u.au */. D
1db43 62 20 2a 70 44 62 3b 0a 23 65 6e 64 69 66 20 2f b *pDb;.#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 75 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 u */. assert( p
1db47 4f 70 2d 3e 70 32 3c 53 51 4c 49 54 45 5f 4e 5f Op->p2<SQLITE_N_
1db48 42 54 52 45 45 5f 4d 45 54 41 20 29 3b 0a 20 20 BTREE_META );.
1db49 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1db4a 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 =0 && pOp->p1<db
1db4b 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 ->nDb );. asser
1db4c 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b t( (p->btreeMask
1db4d 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 & (1<<pOp->p1))
1db4e 21 3d 30 20 29 3b 0a 20 20 75 2e 61 75 2e 70 44 !=0 );. u.au.pD
1db4f 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70 b = &db->aDb[pOp
1db50 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1db51 20 75 2e 61 75 2e 70 44 62 2d 3e 70 42 74 21 3d u.au.pDb->pBt!=
1db52 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 0 );. sqlite3Vd
1db53 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 beMemIntegerify(
1db54 70 49 6e 33 29 3b 0a 20 20 2f 2a 20 53 65 65 20 pIn3);. /* See
1db55 6e 6f 74 65 20 61 62 6f 75 74 20 69 6e 64 65 78 note about index
1db56 20 73 68 69 66 74 69 6e 67 20 6f 6e 20 4f 50 5f shifting on OP_
1db57 52 65 61 64 43 6f 6f 6b 69 65 20 2a 2f 0a 20 20 ReadCookie */.
1db58 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1db59 65 55 70 64 61 74 65 4d 65 74 61 28 75 2e 61 75 eUpdateMeta(u.au
1db5a 2e 70 44 62 2d 3e 70 42 74 2c 20 70 4f 70 2d 3e .pDb->pBt, pOp->
1db5b 70 32 2c 20 28 69 6e 74 29 70 49 6e 33 2d 3e 75 p2, (int)pIn3->u
1db5c 2e 69 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e .i);. if( pOp->
1db5d 70 32 3d 3d 42 54 52 45 45 5f 53 43 48 45 4d 41 p2==BTREE_SCHEMA
1db5e 5f 56 45 52 53 49 4f 4e 20 29 7b 0a 20 20 20 20 _VERSION ){.
1db5f 2f 2a 20 57 68 65 6e 20 74 68 65 20 73 63 68 65 /* When the sche
1db60 6d 61 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67 65 ma cookie change
1db61 73 2c 20 72 65 63 6f 72 64 20 74 68 65 20 6e 65 s, record the ne
1db62 77 20 63 6f 6f 6b 69 65 20 69 6e 74 65 72 6e 61 w cookie interna
1db63 6c 6c 79 20 2a 2f 0a 20 20 20 20 75 2e 61 75 2e lly */. u.au.
1db64 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 pDb->pSchema->sc
1db65 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 3d 20 28 69 hema_cookie = (i
1db66 6e 74 29 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 nt)pIn3->u.i;.
1db67 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 db->flags |= S
1db68 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e QLITE_InternChan
1db69 67 65 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ges;. }else if(
1db6a 20 70 4f 70 2d 3e 70 32 3d 3d 42 54 52 45 45 5f pOp->p2==BTREE_
1db6b 46 49 4c 45 5f 46 4f 52 4d 41 54 20 29 7b 0a 20 FILE_FORMAT ){.
1db6c 20 20 20 2f 2a 20 52 65 63 6f 72 64 20 63 68 61 /* Record cha
1db6d 6e 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 nges in the file
1db6e 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 20 20 75 format */. u
1db6f 2e 61 75 2e 70 44 62 2d 3e 70 53 63 68 65 6d 61 .au.pDb->pSchema
1db70 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 ->file_format =
1db71 28 75 38 29 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 (u8)pIn3->u.i;.
1db72 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 }. if( pOp->p1
1db73 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e ==1 ){. /* In
1db74 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 72 65 validate all pre
1db75 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 pared statements
1db76 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 54 45 whenever the TE
1db77 4d 50 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 MP database.
1db78 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 63 68 61 ** schema is cha
1db79 6e 67 65 64 2e 20 20 54 69 63 6b 65 74 20 23 31 nged. Ticket #1
1db7a 36 34 34 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 644 */. sqlit
1db7b 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 e3ExpirePrepared
1db7c 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a Statements(db);.
1db7d 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d p->expired =
1db7e 20 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0;. }. break;
1db7f 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 .}../* Opcode: V
1db80 65 72 69 66 79 43 6f 6f 6b 69 65 20 50 31 20 50 erifyCookie P1 P
1db81 32 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 2 *.**.** Check
1db82 74 68 65 20 76 61 6c 75 65 20 6f 66 20 67 6c 6f the value of glo
1db83 62 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 72 bal database par
1db84 61 6d 65 74 65 72 20 6e 75 6d 62 65 72 20 30 20 ameter number 0
1db85 28 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 76 (the.** schema v
1db86 65 72 73 69 6f 6e 29 20 61 6e 64 20 6d 61 6b 65 ersion) and make
1db87 20 73 75 72 65 20 69 74 20 69 73 20 65 71 75 61 sure it is equa
1db88 6c 20 74 6f 20 50 32 2e 20 20 0a 2a 2a 20 50 31 l to P2. .** P1
1db89 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 is the database
1db8a 20 6e 75 6d 62 65 72 20 77 68 69 63 68 20 69 73 number which is
1db8b 20 30 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 0 for the main
1db8c 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a database file.**
1db8d 20 61 6e 64 20 31 20 66 6f 72 20 74 68 65 20 66 and 1 for the f
1db8e 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 65 6d 70 ile holding temp
1db8f 6f 72 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64 orary tables and
1db90 20 73 6f 6d 65 20 68 69 67 68 65 72 20 6e 75 6d some higher num
1db91 62 65 72 0a 2a 2a 20 66 6f 72 20 61 75 78 69 6c ber.** for auxil
1db92 69 61 72 79 20 64 61 74 61 62 61 73 65 73 2e 0a iary databases..
1db93 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6f 6b 69 65 **.** The cookie
1db94 20 63 68 61 6e 67 65 73 20 69 74 73 20 76 61 6c changes its val
1db95 75 65 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 ue whenever the
1db96 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 database schema
1db97 63 68 61 6e 67 65 73 2e 0a 2a 2a 20 54 68 69 73 changes..** This
1db98 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 75 73 operation is us
1db99 65 64 20 74 6f 20 64 65 74 65 63 74 20 77 68 65 ed to detect whe
1db9a 6e 20 74 68 61 74 20 74 68 65 20 63 6f 6f 6b 69 n that the cooki
1db9b 65 20 68 61 73 20 63 68 61 6e 67 65 64 0a 2a 2a e has changed.**
1db9c 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 63 75 and that the cu
1db9d 72 72 65 6e 74 20 70 72 6f 63 65 73 73 20 6e 65 rrent process ne
1db9e 65 64 73 20 74 6f 20 72 65 72 65 61 64 20 74 68 eds to reread th
1db9f 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 e schema..**.**
1dba0 45 69 74 68 65 72 20 61 20 74 72 61 6e 73 61 63 Either a transac
1dba1 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 68 61 tion needs to ha
1dba2 76 65 20 62 65 65 6e 20 73 74 61 72 74 65 64 20 ve been started
1dba3 6f 72 20 61 6e 20 4f 50 5f 4f 70 65 6e 20 6e 65 or an OP_Open ne
1dba4 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20 65 78 65 eds.** to be exe
1dba5 63 75 74 65 64 20 28 74 6f 20 65 73 74 61 62 6c cuted (to establ
1dba6 69 73 68 20 61 20 72 65 61 64 20 6c 6f 63 6b 29 ish a read lock)
1dba7 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f 70 63 before this opc
1dba8 6f 64 65 20 69 73 0a 2a 2a 20 69 6e 76 6f 6b 65 ode is.** invoke
1dba9 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 65 d..*/.case OP_Ve
1dbaa 72 69 66 79 43 6f 6f 6b 69 65 3a 20 7b 0a 23 69 rifyCookie: {.#i
1dbab 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1dbac 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1dbad 74 6f 20 75 2e 61 76 20 2a 2f 0a 20 20 69 6e 74 to u.av */. int
1dbae 20 69 4d 65 74 61 3b 0a 20 20 42 74 72 65 65 20 iMeta;. Btree
1dbaf 2a 70 42 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 *pBt;.#endif /*
1dbb0 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1dbb1 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 76 20 moved into u.av
1dbb2 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 */. assert( pOp
1dbb3 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e ->p1>=0 && pOp->
1dbb4 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 p1<db->nDb );.
1dbb5 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 assert( (p->btre
1dbb6 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d eMask & (1<<pOp-
1dbb7 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e >p1))!=0 );. u.
1dbb8 61 76 2e 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 av.pBt = db->aDb
1dbb9 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a 20 [pOp->p1].pBt;.
1dbba 20 69 66 28 20 75 2e 61 76 2e 70 42 74 20 29 7b if( u.av.pBt ){
1dbbb 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1dbbc 65 47 65 74 4d 65 74 61 28 75 2e 61 76 2e 70 42 eGetMeta(u.av.pB
1dbbd 74 2c 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f t, BTREE_SCHEMA_
1dbbe 56 45 52 53 49 4f 4e 2c 20 28 75 33 32 20 2a 29 VERSION, (u32 *)
1dbbf 26 75 2e 61 76 2e 69 4d 65 74 61 29 3b 0a 20 20 &u.av.iMeta);.
1dbc0 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 76 2e }else{. u.av.
1dbc1 69 4d 65 74 61 20 3d 20 30 3b 0a 20 20 7d 0a 20 iMeta = 0;. }.
1dbc2 20 69 66 28 20 75 2e 61 76 2e 69 4d 65 74 61 21 if( u.av.iMeta!
1dbc3 3d 70 4f 70 2d 3e 70 32 20 29 7b 0a 20 20 20 20 =pOp->p2 ){.
1dbc4 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1dbc5 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 , p->zErrMsg);.
1dbc6 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 p->zErrMsg =
1dbc7 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
1dbc8 64 62 2c 20 22 64 61 74 61 62 61 73 65 20 73 63 db, "database sc
1dbc9 68 65 6d 61 20 68 61 73 20 63 68 61 6e 67 65 64 hema has changed
1dbca 22 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 ");. /* If th
1dbcb 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 e schema-cookie
1dbcc 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
1dbcd 65 20 66 69 6c 65 20 6d 61 74 63 68 65 73 20 74 e file matches t
1dbce 68 65 20 63 6f 6f 6b 69 65 0a 20 20 20 20 2a 2a he cookie. **
1dbcf 20 73 74 6f 72 65 64 20 77 69 74 68 20 74 68 65 stored with the
1dbd0 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 in-memory repre
1dbd1 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 sentation of the
1dbd2 20 73 63 68 65 6d 61 2c 20 64 6f 0a 20 20 20 20 schema, do.
1dbd3 2a 2a 20 6e 6f 74 20 72 65 6c 6f 61 64 20 74 68 ** not reload th
1dbd4 65 20 73 63 68 65 6d 61 20 66 72 6f 6d 20 74 68 e schema from th
1dbd5 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
1dbd6 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 . **. ** I
1dbd7 66 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 73 f virtual-tables
1dbd8 20 61 72 65 20 69 6e 20 75 73 65 2c 20 74 68 69 are in use, thi
1dbd9 73 20 69 73 20 6e 6f 74 20 6a 75 73 74 20 61 6e s is not just an
1dbda 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 0a 20 optimization..
1dbdb 20 20 20 2a 2a 20 4f 66 74 65 6e 2c 20 76 2d 74 ** Often, v-t
1dbdc 61 62 6c 65 73 20 73 74 6f 72 65 20 74 68 65 69 ables store thei
1dbdd 72 20 64 61 74 61 20 69 6e 20 6f 74 68 65 72 20 r data in other
1dbde 53 51 4c 69 74 65 20 74 61 62 6c 65 73 2c 20 77 SQLite tables, w
1dbdf 68 69 63 68 0a 20 20 20 20 2a 2a 20 61 72 65 20 hich. ** are
1dbe0 71 75 65 72 69 65 64 20 66 72 6f 6d 20 77 69 74 queried from wit
1dbe1 68 69 6e 20 78 4e 65 78 74 28 29 20 61 6e 64 20 hin xNext() and
1dbe2 6f 74 68 65 72 20 76 2d 74 61 62 6c 65 20 6d 65 other v-table me
1dbe3 74 68 6f 64 73 20 75 73 69 6e 67 0a 20 20 20 20 thods using.
1dbe4 2a 2a 20 70 72 65 70 61 72 65 64 20 71 75 65 72 ** prepared quer
1dbe5 69 65 73 2e 20 49 66 20 73 75 63 68 20 61 20 71 ies. If such a q
1dbe6 75 65 72 79 20 69 73 20 6f 75 74 2d 6f 66 2d 64 uery is out-of-d
1dbe7 61 74 65 2c 20 77 65 20 64 6f 20 6e 6f 74 20 77 ate, we do not w
1dbe8 61 6e 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64 69 ant to. ** di
1dbe9 73 63 61 72 64 20 74 68 65 20 64 61 74 61 62 61 scard the databa
1dbea 73 65 20 73 63 68 65 6d 61 2c 20 61 73 20 74 68 se schema, as th
1dbeb 65 20 75 73 65 72 20 63 6f 64 65 20 69 6d 70 6c e user code impl
1dbec 65 6d 65 6e 74 69 6e 67 20 74 68 65 0a 20 20 20 ementing the.
1dbed 20 2a 2a 20 76 2d 74 61 62 6c 65 20 77 6f 75 6c ** v-table woul
1dbee 64 20 68 61 76 65 20 74 6f 20 62 65 20 72 65 61 d have to be rea
1dbef 64 79 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 dy for the sqlit
1dbf0 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 e3_vtab structur
1dbf1 65 20 69 74 73 65 6c 66 0a 20 20 20 20 2a 2a 20 e itself. **
1dbf2 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 to be invalidate
1dbf3 64 20 77 68 65 6e 65 76 65 72 20 73 71 6c 69 74 d whenever sqlit
1dbf4 65 33 5f 73 74 65 70 28 29 20 69 73 20 63 61 6c e3_step() is cal
1dbf5 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 0a led from within.
1dbf6 20 20 20 20 2a 2a 20 61 20 76 2d 74 61 62 6c 65 ** a v-table
1dbf7 20 6d 65 74 68 6f 64 2e 0a 20 20 20 20 2a 2f 0a method.. */.
1dbf8 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b if( db->aDb[
1dbf9 70 4f 70 2d 3e 70 31 5d 2e 70 53 63 68 65 6d 61 pOp->p1].pSchema
1dbfa 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 21 ->schema_cookie!
1dbfb 3d 75 2e 61 76 2e 69 4d 65 74 61 20 29 7b 0a 20 =u.av.iMeta ){.
1dbfc 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 sqlite3Rese
1dbfd 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 tInternalSchema(
1dbfe 64 62 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 db, pOp->p1);.
1dbff 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 }.. sqlite3
1dc00 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74 ExpirePreparedSt
1dc01 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 atements(db);.
1dc02 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 53 43 rc = SQLITE_SC
1dc03 48 45 4d 41 3b 0a 20 20 7d 0a 20 20 62 72 65 61 HEMA;. }. brea
1dc04 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1dc05 20 4f 70 65 6e 52 65 61 64 20 50 31 20 50 32 20 OpenRead P1 P2
1dc06 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f P3 P4 P5.**.** O
1dc07 70 65 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 pen a read-only
1dc08 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 64 cursor for the d
1dc09 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 77 68 atabase table wh
1dc0a 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 ose root page is
1dc0b 0a 2a 2a 20 50 32 20 69 6e 20 61 20 64 61 74 61 .** P2 in a data
1dc0c 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 base file. The
1dc0d 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 database file is
1dc0e 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 50 determined by P
1dc0f 33 2e 20 0a 2a 2a 20 50 33 3d 3d 30 20 6d 65 61 3. .** P3==0 mea
1dc10 6e 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 ns the main data
1dc11 62 61 73 65 2c 20 50 33 3d 3d 31 20 6d 65 61 6e base, P3==1 mean
1dc12 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 75 s the database u
1dc13 73 65 64 20 66 6f 72 20 0a 2a 2a 20 74 65 6d 70 sed for .** temp
1dc14 6f 72 61 72 79 20 74 61 62 6c 65 73 2c 20 61 6e orary tables, an
1dc15 64 20 50 33 3e 31 20 6d 65 61 6e 73 20 75 73 65 d P3>1 means use
1dc16 64 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 d the correspond
1dc17 69 6e 67 20 61 74 74 61 63 68 65 64 0a 2a 2a 20 ing attached.**
1dc18 64 61 74 61 62 61 73 65 2e 20 20 47 69 76 65 20 database. Give
1dc19 74 68 65 20 6e 65 77 20 63 75 72 73 6f 72 20 61 the new cursor a
1dc1a 6e 20 69 64 65 6e 74 69 66 69 65 72 20 6f 66 20 n identifier of
1dc1b 50 31 2e 20 20 54 68 65 20 50 31 0a 2a 2a 20 76 P1. The P1.** v
1dc1c 61 6c 75 65 73 20 6e 65 65 64 20 6e 6f 74 20 62 alues need not b
1dc1d 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 62 75 74 e contiguous but
1dc1e 20 61 6c 6c 20 50 31 20 76 61 6c 75 65 73 20 73 all P1 values s
1dc1f 68 6f 75 6c 64 20 62 65 20 73 6d 61 6c 6c 20 69 hould be small i
1dc20 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 49 74 20 69 ntegers..** It i
1dc21 73 20 61 6e 20 65 72 72 6f 72 20 66 6f 72 20 50 s an error for P
1dc22 31 20 74 6f 20 62 65 20 6e 65 67 61 74 69 76 65 1 to be negative
1dc23 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 21 3d 30 ..**.** If P5!=0
1dc24 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 63 6f then use the co
1dc25 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 ntent of registe
1dc26 72 20 50 32 20 61 73 20 74 68 65 20 72 6f 6f 74 r P2 as the root
1dc27 20 70 61 67 65 2c 20 6e 6f 74 0a 2a 2a 20 74 68 page, not.** th
1dc28 65 20 76 61 6c 75 65 20 6f 66 20 50 32 20 69 74 e value of P2 it
1dc29 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 self..**.** Ther
1dc2a 65 20 77 69 6c 6c 20 62 65 20 61 20 72 65 61 64 e will be a read
1dc2b 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
1dc2c 61 62 61 73 65 20 77 68 65 6e 65 76 65 72 20 74 abase whenever t
1dc2d 68 65 72 65 20 69 73 20 61 6e 0a 2a 2a 20 6f 70 here is an.** op
1dc2e 65 6e 20 63 75 72 73 6f 72 2e 20 20 49 66 20 74 en cursor. If t
1dc2f 68 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20 he database was
1dc30 75 6e 6c 6f 63 6b 65 64 20 70 72 69 6f 72 20 74 unlocked prior t
1dc31 6f 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 o this instructi
1dc32 6f 6e 0a 2a 2a 20 74 68 65 6e 20 61 20 72 65 61 on.** then a rea
1dc33 64 20 6c 6f 63 6b 20 69 73 20 61 63 71 75 69 72 d lock is acquir
1dc34 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 ed as part of th
1dc35 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 is instruction.
1dc36 20 41 20 72 65 61 64 0a 2a 2a 20 6c 6f 63 6b 20 A read.** lock
1dc37 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 70 72 6f allows other pro
1dc38 63 65 73 73 65 73 20 74 6f 20 72 65 61 64 20 74 cesses to read t
1dc39 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20 he database but
1dc3a 70 72 6f 68 69 62 69 74 73 0a 2a 2a 20 61 6e 79 prohibits.** any
1dc3b 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 66 other process f
1dc3c 72 6f 6d 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 rom modifying th
1dc3d 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 e database. The
1dc3e 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 0a 2a 2a read lock is.**
1dc3f 20 72 65 6c 65 61 73 65 64 20 77 68 65 6e 20 61 released when a
1dc40 6c 6c 20 63 75 72 73 6f 72 73 20 61 72 65 20 63 ll cursors are c
1dc41 6c 6f 73 65 64 2e 20 20 49 66 20 74 68 69 73 20 losed. If this
1dc42 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 74 74 65 instruction atte
1dc43 6d 70 74 73 0a 2a 2a 20 74 6f 20 67 65 74 20 61 mpts.** to get a
1dc44 20 72 65 61 64 20 6c 6f 63 6b 20 62 75 74 20 66 read lock but f
1dc45 61 69 6c 73 2c 20 74 68 65 20 73 63 72 69 70 74 ails, the script
1dc46 20 74 65 72 6d 69 6e 61 74 65 73 20 77 69 74 68 terminates with
1dc47 20 61 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 an.** SQLITE_BU
1dc48 53 59 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a SY error code..*
1dc49 2a 0a 2a 2a 20 54 68 65 20 50 34 20 76 61 6c 75 *.** The P4 valu
1dc4a 65 20 6d 61 79 20 62 65 20 65 69 74 68 65 72 20 e may be either
1dc4b 61 6e 20 69 6e 74 65 67 65 72 20 28 50 34 5f 49 an integer (P4_I
1dc4c 4e 54 33 32 29 20 6f 72 20 61 20 70 6f 69 6e 74 NT32) or a point
1dc4d 65 72 20 74 6f 0a 2a 2a 20 61 20 4b 65 79 49 6e er to.** a KeyIn
1dc4e 66 6f 20 73 74 72 75 63 74 75 72 65 20 28 50 34 fo structure (P4
1dc4f 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 66 20 69 74 _KEYINFO). If it
1dc50 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
1dc51 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a 2a 20 73 a KeyInfo .** s
1dc52 74 72 75 63 74 75 72 65 2c 20 74 68 65 6e 20 73 tructure, then s
1dc53 61 69 64 20 73 74 72 75 63 74 75 72 65 20 64 65 aid structure de
1dc54 66 69 6e 65 73 20 74 68 65 20 63 6f 6e 74 65 6e fines the conten
1dc55 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 t and collating
1dc56 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 6f 66 20 .** sequence of
1dc57 74 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20 the index being
1dc58 6f 70 65 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 opened. Otherwis
1dc59 65 2c 20 69 66 20 50 34 20 69 73 20 61 6e 20 69 e, if P4 is an i
1dc5a 6e 74 65 67 65 72 20 0a 2a 2a 20 76 61 6c 75 65 nteger .** value
1dc5b 2c 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 74 , it is set to t
1dc5c 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c he number of col
1dc5d 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c umns in the tabl
1dc5e 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 e..**.** See als
1dc5f 6f 20 4f 70 65 6e 57 72 69 74 65 2e 0a 2a 2f 0a o OpenWrite..*/.
1dc60 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 57 /* Opcode: OpenW
1dc61 72 69 74 65 20 50 31 20 50 32 20 50 33 20 50 34 rite P1 P2 P3 P4
1dc62 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 P5.**.** Open a
1dc63 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 read/write curs
1dc64 6f 72 20 6e 61 6d 65 64 20 50 31 20 6f 6e 20 74 or named P1 on t
1dc65 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 he table or inde
1dc66 78 20 77 68 6f 73 65 20 72 6f 6f 74 0a 2a 2a 20 x whose root.**
1dc67 70 61 67 65 20 69 73 20 50 32 2e 20 20 4f 72 20 page is P2. Or
1dc68 69 66 20 50 35 21 3d 30 20 75 73 65 20 74 68 65 if P5!=0 use the
1dc69 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 content of regi
1dc6a 73 74 65 72 20 50 32 20 74 6f 20 66 69 6e 64 20 ster P2 to find
1dc6b 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 the.** root page
1dc6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 76 ..**.** The P4 v
1dc6d 61 6c 75 65 20 6d 61 79 20 62 65 20 65 69 74 68 alue may be eith
1dc6e 65 72 20 61 6e 20 69 6e 74 65 67 65 72 20 28 50 er an integer (P
1dc6f 34 5f 49 4e 54 33 32 29 20 6f 72 20 61 20 70 6f 4_INT32) or a po
1dc70 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 20 4b 65 inter to.** a Ke
1dc71 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 yInfo structure
1dc72 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 66 (P4_KEYINFO). If
1dc73 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 it is a pointer
1dc74 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a to a KeyInfo .*
1dc75 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 * structure, the
1dc76 6e 20 73 61 69 64 20 73 74 72 75 63 74 75 72 65 n said structure
1dc77 20 64 65 66 69 6e 65 73 20 74 68 65 20 63 6f 6e defines the con
1dc78 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69 tent and collati
1dc79 6e 67 20 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 ng .** sequence
1dc7a 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62 65 69 of the index bei
1dc7b 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74 68 65 72 ng opened. Other
1dc7c 77 69 73 65 2c 20 69 66 20 50 34 20 69 73 20 61 wise, if P4 is a
1dc7d 6e 20 69 6e 74 65 67 65 72 20 0a 2a 2a 20 76 61 n integer .** va
1dc7e 6c 75 65 2c 20 69 74 20 69 73 20 73 65 74 20 74 lue, it is set t
1dc7f 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 o the number of
1dc80 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 columns in the t
1dc81 61 62 6c 65 2c 20 6f 72 20 74 6f 20 74 68 65 0a able, or to the.
1dc82 2a 2a 20 6c 61 72 67 65 73 74 20 69 6e 64 65 78 ** largest index
1dc83 20 6f 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6f of any column o
1dc84 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 f the table that
1dc85 20 69 73 20 61 63 74 75 61 6c 6c 79 20 75 73 65 is actually use
1dc86 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e d..**.** This in
1dc87 73 74 72 75 63 74 69 6f 6e 20 77 6f 72 6b 73 20 struction works
1dc88 6a 75 73 74 20 6c 69 6b 65 20 4f 70 65 6e 52 65 just like OpenRe
1dc89 61 64 20 65 78 63 65 70 74 20 74 68 61 74 20 69 ad except that i
1dc8a 74 20 6f 70 65 6e 73 20 74 68 65 20 63 75 72 73 t opens the curs
1dc8b 6f 72 0a 2a 2a 20 69 6e 20 72 65 61 64 2f 77 72 or.** in read/wr
1dc8c 69 74 65 20 6d 6f 64 65 2e 20 20 46 6f 72 20 61 ite mode. For a
1dc8d 20 67 69 76 65 6e 20 74 61 62 6c 65 2c 20 74 68 given table, th
1dc8e 65 72 65 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f ere can be one o
1dc8f 72 20 6d 6f 72 65 20 72 65 61 64 2d 6f 6e 6c 79 r more read-only
1dc90 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 72 20 61 .** cursors or a
1dc91 20 73 69 6e 67 6c 65 20 72 65 61 64 2f 77 72 69 single read/wri
1dc92 74 65 20 63 75 72 73 6f 72 20 62 75 74 20 6e 6f te cursor but no
1dc93 74 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 53 65 t both..**.** Se
1dc94 65 20 61 6c 73 6f 20 4f 70 65 6e 52 65 61 64 2e e also OpenRead.
1dc95 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e .*/.case OP_Open
1dc96 52 65 61 64 3a 0a 63 61 73 65 20 4f 50 5f 4f 70 Read:.case OP_Op
1dc97 65 6e 57 72 69 74 65 3a 20 7b 0a 23 69 66 20 30 enWrite: {.#if 0
1dc98 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1dc99 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1dc9a 75 2e 61 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 u.aw */. int nF
1dc9b 69 65 6c 64 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 ield;. KeyInfo
1dc9c 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 69 6e 74 *pKeyInfo;. int
1dc9d 20 70 32 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a p2;. int iDb;.
1dc9e 20 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a 20 20 int wrFlag;.
1dc9f 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 56 64 62 Btree *pX;. Vdb
1dca0 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 eCursor *pCur;.
1dca1 20 44 62 20 2a 70 44 62 3b 0a 23 65 6e 64 69 66 Db *pDb;.#endif
1dca2 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1dca3 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1dca4 2e 61 77 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d .aw */.. if( p-
1dca5 3e 65 78 70 69 72 65 64 20 29 7b 0a 20 20 20 20 >expired ){.
1dca6 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52 rc = SQLITE_ABOR
1dca7 54 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 T;. break;.
1dca8 7d 0a 0a 20 20 75 2e 61 77 2e 6e 46 69 65 6c 64 }.. u.aw.nField
1dca9 20 3d 20 30 3b 0a 20 20 75 2e 61 77 2e 70 4b 65 = 0;. u.aw.pKe
1dcaa 79 49 6e 66 6f 20 3d 20 30 3b 0a 20 20 75 2e 61 yInfo = 0;. u.a
1dcab 77 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a w.p2 = pOp->p2;.
1dcac 20 20 75 2e 61 77 2e 69 44 62 20 3d 20 70 4f 70 u.aw.iDb = pOp
1dcad 2d 3e 70 33 3b 0a 20 20 61 73 73 65 72 74 28 20 ->p3;. assert(
1dcae 75 2e 61 77 2e 69 44 62 3e 3d 30 20 26 26 20 75 u.aw.iDb>=0 && u
1dcaf 2e 61 77 2e 69 44 62 3c 64 62 2d 3e 6e 44 62 20 .aw.iDb<db->nDb
1dcb0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d );. assert( (p-
1dcb1 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c >btreeMask & (1<
1dcb2 3c 75 2e 61 77 2e 69 44 62 29 29 21 3d 30 20 29 <u.aw.iDb))!=0 )
1dcb3 3b 0a 20 20 75 2e 61 77 2e 70 44 62 20 3d 20 26 ;. u.aw.pDb = &
1dcb4 64 62 2d 3e 61 44 62 5b 75 2e 61 77 2e 69 44 62 db->aDb[u.aw.iDb
1dcb5 5d 3b 0a 20 20 75 2e 61 77 2e 70 58 20 3d 20 75 ];. u.aw.pX = u
1dcb6 2e 61 77 2e 70 44 62 2d 3e 70 42 74 3b 0a 20 20 .aw.pDb->pBt;.
1dcb7 61 73 73 65 72 74 28 20 75 2e 61 77 2e 70 58 21 assert( u.aw.pX!
1dcb8 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d =0 );. if( pOp-
1dcb9 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e >opcode==OP_Open
1dcba 57 72 69 74 65 20 29 7b 0a 20 20 20 20 75 2e 61 Write ){. u.a
1dcbb 77 2e 77 72 46 6c 61 67 20 3d 20 31 3b 0a 20 20 w.wrFlag = 1;.
1dcbc 20 20 69 66 28 20 75 2e 61 77 2e 70 44 62 2d 3e if( u.aw.pDb->
1dcbd 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f pSchema->file_fo
1dcbe 72 6d 61 74 20 3c 20 70 2d 3e 6d 69 6e 57 72 69 rmat < p->minWri
1dcbf 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 29 7b 0a teFileFormat ){.
1dcc0 20 20 20 20 20 20 70 2d 3e 6d 69 6e 57 72 69 74 p->minWrit
1dcc1 65 46 69 6c 65 46 6f 72 6d 61 74 20 3d 20 75 2e eFileFormat = u.
1dcc2 61 77 2e 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d aw.pDb->pSchema-
1dcc3 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 0a 20 20 >file_format;.
1dcc4 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
1dcc5 20 75 2e 61 77 2e 77 72 46 6c 61 67 20 3d 20 30 u.aw.wrFlag = 0
1dcc6 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d ;. }. if( pOp-
1dcc7 3e 70 35 20 29 7b 0a 20 20 20 20 61 73 73 65 72 >p5 ){. asser
1dcc8 74 28 20 75 2e 61 77 2e 70 32 3e 30 20 29 3b 0a t( u.aw.p2>0 );.
1dcc9 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 77 assert( u.aw
1dcca 2e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a .p2<=p->nMem );.
1dccb 20 20 20 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 pIn2 = &p->a
1dccc 4d 65 6d 5b 75 2e 61 77 2e 70 32 5d 3b 0a 20 20 Mem[u.aw.p2];.
1dccd 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1dcce 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 32 29 Integerify(pIn2)
1dccf 3b 0a 20 20 20 20 75 2e 61 77 2e 70 32 20 3d 20 ;. u.aw.p2 =
1dcd0 28 69 6e 74 29 70 49 6e 32 2d 3e 75 2e 69 3b 0a (int)pIn2->u.i;.
1dcd1 20 20 20 20 2f 2a 20 54 68 65 20 75 2e 61 77 2e /* The u.aw.
1dcd2 70 32 20 76 61 6c 75 65 20 61 6c 77 61 79 73 20 p2 value always
1dcd3 63 6f 6d 65 73 20 66 72 6f 6d 20 61 20 70 72 69 comes from a pri
1dcd4 6f 72 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c or OP_CreateTabl
1dcd5 65 20 6f 70 63 6f 64 65 20 61 6e 64 0a 20 20 20 e opcode and.
1dcd6 20 2a 2a 20 74 68 61 74 20 6f 70 63 6f 64 65 20 ** that opcode
1dcd7 77 69 6c 6c 20 61 6c 77 61 79 73 20 73 65 74 20 will always set
1dcd8 74 68 65 20 75 2e 61 77 2e 70 32 20 76 61 6c 75 the u.aw.p2 valu
1dcd9 65 20 74 6f 20 32 20 6f 72 20 6d 6f 72 65 20 6f e to 2 or more o
1dcda 72 20 65 6c 73 65 20 66 61 69 6c 2e 0a 20 20 20 r else fail..
1dcdb 20 2a 2a 20 49 66 20 74 68 65 72 65 20 77 65 72 ** If there wer
1dcdc 65 20 61 20 66 61 69 6c 75 72 65 2c 20 74 68 65 e a failure, the
1dcdd 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
1dcde 65 6e 74 20 77 6f 75 6c 64 20 68 61 76 65 20 68 ent would have h
1dcdf 61 6c 74 65 64 0a 20 20 20 20 2a 2a 20 62 65 66 alted. ** bef
1dce0 6f 72 65 20 72 65 61 63 68 69 6e 67 20 74 68 69 ore reaching thi
1dce1 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 2a s instruction. *
1dce2 2f 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 /. if( NEVER(
1dce3 75 2e 61 77 2e 70 32 3c 32 29 20 29 20 7b 0a 20 u.aw.p2<2) ) {.
1dce4 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1dce5 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
1dce6 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f goto abort_
1dce7 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
1dce8 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f }. }. if( pO
1dce9 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 p->p4type==P4_KE
1dcea 59 49 4e 46 4f 20 29 7b 0a 20 20 20 20 75 2e 61 YINFO ){. u.a
1dceb 77 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 w.pKeyInfo = pOp
1dcec 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 ->p4.pKeyInfo;.
1dced 20 20 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f u.aw.pKeyInfo
1dcee 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 ->enc = ENC(p->d
1dcef 62 29 3b 0a 20 20 20 20 75 2e 61 77 2e 6e 46 69 b);. u.aw.nFi
1dcf0 65 6c 64 20 3d 20 75 2e 61 77 2e 70 4b 65 79 49 eld = u.aw.pKeyI
1dcf1 6e 66 6f 2d 3e 6e 46 69 65 6c 64 2b 31 3b 0a 20 nfo->nField+1;.
1dcf2 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e }else if( pOp->
1dcf3 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 p4type==P4_INT32
1dcf4 20 29 7b 0a 20 20 20 20 75 2e 61 77 2e 6e 46 69 ){. u.aw.nFi
1dcf5 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b eld = pOp->p4.i;
1dcf6 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
1dcf7 4f 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75 Op->p1>=0 );. u
1dcf8 2e 61 77 2e 70 43 75 72 20 3d 20 61 6c 6c 6f 63 .aw.pCur = alloc
1dcf9 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 ateCursor(p, pOp
1dcfa 2d 3e 70 31 2c 20 75 2e 61 77 2e 6e 46 69 65 6c ->p1, u.aw.nFiel
1dcfb 64 2c 20 75 2e 61 77 2e 69 44 62 2c 20 31 29 3b d, u.aw.iDb, 1);
1dcfc 0a 20 20 69 66 28 20 75 2e 61 77 2e 70 43 75 72 . if( u.aw.pCur
1dcfd 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 ==0 ) goto no_me
1dcfe 6d 3b 0a 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e m;. u.aw.pCur->
1dcff 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72 nullRow = 1;. r
1dd00 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1dd01 43 75 72 73 6f 72 28 75 2e 61 77 2e 70 58 2c 20 Cursor(u.aw.pX,
1dd02 75 2e 61 77 2e 70 32 2c 20 75 2e 61 77 2e 77 72 u.aw.p2, u.aw.wr
1dd03 46 6c 61 67 2c 20 75 2e 61 77 2e 70 4b 65 79 49 Flag, u.aw.pKeyI
1dd04 6e 66 6f 2c 20 75 2e 61 77 2e 70 43 75 72 2d 3e nfo, u.aw.pCur->
1dd05 70 43 75 72 73 6f 72 29 3b 0a 20 20 75 2e 61 77 pCursor);. u.aw
1dd06 2e 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 .pCur->pKeyInfo
1dd07 3d 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 3b = u.aw.pKeyInfo;
1dd08 0a 0a 20 20 2f 2a 20 53 69 6e 63 65 20 69 74 20 .. /* Since it
1dd09 70 65 72 66 6f 72 6d 73 20 6e 6f 20 6d 65 6d 6f performs no memo
1dd0a 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 72 ry allocation or
1dd0b 20 49 4f 2c 20 74 68 65 20 6f 6e 6c 79 20 76 61 IO, the only va
1dd0c 6c 75 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 73 lues that. ** s
1dd0d 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
1dd0e 72 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 61 r() may return a
1dd0f 72 65 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 re SQLITE_EMPTY
1dd10 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20 and SQLITE_OK..
1dd11 20 2a 2a 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 ** SQLITE_EMPTY
1dd12 20 69 73 20 6f 6e 6c 79 20 72 65 74 75 72 6e 65 is only returne
1dd13 64 20 77 68 65 6e 20 61 74 74 65 6d 70 74 69 6e d when attemptin
1dd14 67 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 74 61 g to open the ta
1dd15 62 6c 65 0a 20 20 2a 2a 20 72 6f 6f 74 65 64 20 ble. ** rooted
1dd16 61 74 20 70 61 67 65 20 31 20 6f 66 20 61 20 7a at page 1 of a z
1dd17 65 72 6f 2d 62 79 74 65 20 64 61 74 61 62 61 73 ero-byte databas
1dd18 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 e. */. assert(
1dd19 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 4d 50 54 rc==SQLITE_EMPT
1dd1a 59 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f Y || rc==SQLITE_
1dd1b 4f 4b 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d OK );. if( rc==
1dd1c 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 29 7b 0a SQLITE_EMPTY ){.
1dd1d 20 20 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e 70 u.aw.pCur->p
1dd1e 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20 Cursor = 0;.
1dd1f 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1dd20 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 }.. /* Set th
1dd21 65 20 56 64 62 65 43 75 72 73 6f 72 2e 69 73 54 e VdbeCursor.isT
1dd22 61 62 6c 65 20 61 6e 64 20 69 73 49 6e 64 65 78 able and isIndex
1dd23 20 76 61 72 69 61 62 6c 65 73 2e 20 50 72 65 76 variables. Prev
1dd24 69 6f 75 73 20 76 65 72 73 69 6f 6e 73 20 6f 66 ious versions of
1dd25 0a 20 20 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 . ** SQLite use
1dd26 64 20 74 6f 20 63 68 65 63 6b 20 69 66 20 74 68 d to check if th
1dd27 65 20 72 6f 6f 74 2d 70 61 67 65 20 66 6c 61 67 e root-page flag
1dd28 73 20 77 65 72 65 20 73 61 6e 65 20 61 74 20 74 s were sane at t
1dd29 68 69 73 20 70 6f 69 6e 74 0a 20 20 2a 2a 20 61 his point. ** a
1dd2a 6e 64 20 72 65 70 6f 72 74 20 64 61 74 61 62 61 nd report databa
1dd2b 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 66 se corruption if
1dd2c 20 74 68 65 79 20 77 65 72 65 20 6e 6f 74 2c 20 they were not,
1dd2d 62 75 74 20 74 68 69 73 20 63 68 65 63 6b 20 68 but this check h
1dd2e 61 73 0a 20 20 2a 2a 20 73 69 6e 63 65 20 6d 6f as. ** since mo
1dd2f 76 65 64 20 69 6e 74 6f 20 74 68 65 20 62 74 72 ved into the btr
1dd30 65 65 20 6c 61 79 65 72 2e 20 20 2a 2f 0a 20 20 ee layer. */.
1dd31 75 2e 61 77 2e 70 43 75 72 2d 3e 69 73 54 61 62 u.aw.pCur->isTab
1dd32 6c 65 20 3d 20 70 4f 70 2d 3e 70 34 74 79 70 65 le = pOp->p4type
1dd33 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 !=P4_KEYINFO;.
1dd34 75 2e 61 77 2e 70 43 75 72 2d 3e 69 73 49 6e 64 u.aw.pCur->isInd
1dd35 65 78 20 3d 20 21 75 2e 61 77 2e 70 43 75 72 2d ex = !u.aw.pCur-
1dd36 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 72 65 61 >isTable;. brea
1dd37 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1dd38 20 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 50 OpenEphemeral P
1dd39 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 1 P2 * P4 *.**.*
1dd3a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 63 75 72 * Open a new cur
1dd3b 73 6f 72 20 50 31 20 74 6f 20 61 20 74 72 61 6e sor P1 to a tran
1dd3c 73 69 65 6e 74 20 74 61 62 6c 65 2e 0a 2a 2a 20 sient table..**
1dd3d 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c The cursor is al
1dd3e 77 61 79 73 20 6f 70 65 6e 65 64 20 72 65 61 64 ways opened read
1dd3f 2f 77 72 69 74 65 20 65 76 65 6e 20 69 66 20 0a /write even if .
1dd40 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 ** the main data
1dd41 62 61 73 65 20 69 73 20 72 65 61 64 2d 6f 6e 6c base is read-onl
1dd42 79 2e 20 20 54 68 65 20 74 72 61 6e 73 69 65 6e y. The transien
1dd43 74 20 6f 72 20 76 69 72 74 75 61 6c 0a 2a 2a 20 t or virtual.**
1dd44 74 61 62 6c 65 20 69 73 20 64 65 6c 65 74 65 64 table is deleted
1dd45 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77 automatically w
1dd46 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 hen the cursor i
1dd47 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 s closed..**.**
1dd48 50 32 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 P2 is the number
1dd49 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 of columns in t
1dd4a 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 he virtual table
1dd4b 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 ..** The cursor
1dd4c 70 6f 69 6e 74 73 20 74 6f 20 61 20 42 54 72 65 points to a BTre
1dd4d 65 20 74 61 62 6c 65 20 69 66 20 50 34 3d 3d 30 e table if P4==0
1dd4e 20 61 6e 64 20 74 6f 20 61 20 42 54 72 65 65 20 and to a BTree
1dd4f 69 6e 64 65 78 0a 2a 2a 20 69 66 20 50 34 20 69 index.** if P4 i
1dd50 73 20 6e 6f 74 20 30 2e 20 20 49 66 20 50 34 20 s not 0. If P4
1dd51 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 74 20 is not NULL, it
1dd52 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65 79 49 points to a KeyI
1dd53 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a 2a nfo structure.**
1dd54 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 that defines th
1dd55 65 20 66 6f 72 6d 61 74 20 6f 66 20 6b 65 79 73 e format of keys
1dd56 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a in the index..*
1dd57 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 *.** This opcode
1dd58 20 77 61 73 20 6f 6e 63 65 20 63 61 6c 6c 65 64 was once called
1dd59 20 4f 70 65 6e 54 65 6d 70 2e 20 20 42 75 74 20 OpenTemp. But
1dd5a 74 68 61 74 20 63 72 65 61 74 65 64 0a 2a 2a 20 that created.**
1dd5b 63 6f 6e 66 75 73 69 6f 6e 20 62 65 63 61 75 73 confusion becaus
1dd5c 65 20 74 68 65 20 74 65 72 6d 20 22 74 65 6d 70 e the term "temp
1dd5d 20 74 61 62 6c 65 22 2c 20 6d 69 67 68 74 20 72 table", might r
1dd5e 65 66 65 72 20 65 69 74 68 65 72 0a 2a 2a 20 74 efer either.** t
1dd5f 6f 20 61 20 54 45 4d 50 20 74 61 62 6c 65 20 61 o a TEMP table a
1dd60 74 20 74 68 65 20 53 51 4c 20 6c 65 76 65 6c 2c t the SQL level,
1dd61 20 6f 72 20 74 6f 20 61 20 74 61 62 6c 65 20 6f or to a table o
1dd62 70 65 6e 65 64 20 62 79 0a 2a 2a 20 74 68 69 73 pened by.** this
1dd63 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 6e 20 74 opcode. Then t
1dd64 68 69 73 20 6f 70 63 6f 64 65 20 77 61 73 20 63 his opcode was c
1dd65 61 6c 6c 20 4f 70 65 6e 56 69 72 74 75 61 6c 2e all OpenVirtual.
1dd66 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 20 63 72 But.** that cr
1dd67 65 61 74 65 64 20 63 6f 6e 66 75 73 69 6f 6e 20 eated confusion
1dd68 77 69 74 68 20 74 68 65 20 77 68 6f 6c 65 20 76 with the whole v
1dd69 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 69 64 65 irtual-table ide
1dd6a 61 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 a..*/.case OP_Op
1dd6b 65 6e 45 70 68 65 6d 65 72 61 6c 3a 20 7b 0a 23 enEphemeral: {.#
1dd6c 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1dd6d 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1dd6e 6e 74 6f 20 75 2e 61 78 20 2a 2f 0a 20 20 56 64 nto u.ax */. Vd
1dd6f 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 23 beCursor *pCx;.#
1dd70 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1dd71 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1dd72 6e 74 6f 20 75 2e 61 78 20 2a 2f 0a 20 20 73 74 nto u.ax */. st
1dd73 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 6f atic const int o
1dd74 70 65 6e 46 6c 61 67 73 20 3d 0a 20 20 20 20 20 penFlags =.
1dd75 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
1dd76 44 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20 53 DWRITE |. S
1dd77 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 QLITE_OPEN_CREAT
1dd78 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 E |. SQLITE
1dd79 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 _OPEN_EXCLUSIVE
1dd7a 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f |. SQLITE_O
1dd7b 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 PEN_DELETEONCLOS
1dd7c 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 E |. SQLITE
1dd7d 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f _OPEN_TRANSIENT_
1dd7e 44 42 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 DB;.. assert( p
1dd7f 4f 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75 Op->p1>=0 );. u
1dd80 2e 61 78 2e 70 43 78 20 3d 20 61 6c 6c 6f 63 61 .ax.pCx = alloca
1dd81 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d teCursor(p, pOp-
1dd82 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 2d 31 >p1, pOp->p2, -1
1dd83 2c 20 31 29 3b 0a 20 20 69 66 28 20 75 2e 61 78 , 1);. if( u.ax
1dd84 2e 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e .pCx==0 ) goto n
1dd85 6f 5f 6d 65 6d 3b 0a 20 20 75 2e 61 78 2e 70 43 o_mem;. u.ax.pC
1dd86 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a x->nullRow = 1;.
1dd87 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1dd88 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20 30 reeFactory(db, 0
1dd89 2c 20 31 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 , 1, SQLITE_DEFA
1dd8a 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53 ULT_TEMP_CACHE_S
1dd8b 49 5a 45 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 0a IZE, openFlags,.
1dd8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dd8d 20 20 20 20 20 20 20 20 20 20 20 26 75 2e 61 78 &u.ax
1dd8e 2e 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20 69 66 .pCx->pBt);. if
1dd8f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1dd90 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
1dd91 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 te3BtreeBeginTra
1dd92 6e 73 28 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 ns(u.ax.pCx->pBt
1dd93 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 , 1);. }. if(
1dd94 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1dd95 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74 72 61 . /* If a tra
1dd96 6e 73 69 65 6e 74 20 69 6e 64 65 78 20 69 73 20 nsient index is
1dd97 72 65 71 75 69 72 65 64 2c 20 63 72 65 61 74 65 required, create
1dd98 20 69 74 20 62 79 20 63 61 6c 6c 69 6e 67 0a 20 it by calling.
1dd99 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 ** sqlite3Btr
1dd9a 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 29 20 eeCreateTable()
1dd9b 77 69 74 68 20 74 68 65 20 42 54 52 45 45 5f 5a with the BTREE_Z
1dd9c 45 52 4f 44 41 54 41 20 66 6c 61 67 20 62 65 66 ERODATA flag bef
1dd9d 6f 72 65 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69 ore. ** openi
1dd9e 6e 67 20 69 74 2e 20 49 66 20 61 20 74 72 61 6e ng it. If a tran
1dd9f 73 69 65 6e 74 20 74 61 62 6c 65 20 69 73 20 72 sient table is r
1dda0 65 71 75 69 72 65 64 2c 20 6a 75 73 74 20 75 73 equired, just us
1dda1 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 61 75 74 e the. ** aut
1dda2 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74 omatically creat
1dda3 65 64 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f ed table with ro
1dda4 6f 74 2d 70 61 67 65 20 31 20 28 61 6e 20 49 4e ot-page 1 (an IN
1dda5 54 4b 45 59 20 74 61 62 6c 65 29 2e 0a 20 20 20 TKEY table)..
1dda6 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 70 2d */. if( pOp-
1dda7 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 29 7b 0a >p4.pKeyInfo ){.
1dda8 20 20 20 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a int pgno;.
1dda9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1ddaa 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 p->p4type==P4_KE
1ddab 59 49 4e 46 4f 20 29 3b 0a 20 20 20 20 20 20 72 YINFO );. r
1ddac 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1ddad 43 72 65 61 74 65 54 61 62 6c 65 28 75 2e 61 78 CreateTable(u.ax
1ddae 2e 70 43 78 2d 3e 70 42 74 2c 20 26 70 67 6e 6f .pCx->pBt, &pgno
1ddaf 2c 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 , BTREE_ZERODATA
1ddb0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
1ddb1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1ddb2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 67 assert( pg
1ddb3 6e 6f 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 2b no==MASTER_ROOT+
1ddb4 31 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 1 );. rc
1ddb5 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 = sqlite3BtreeCu
1ddb6 72 73 6f 72 28 75 2e 61 78 2e 70 43 78 2d 3e 70 rsor(u.ax.pCx->p
1ddb7 42 74 2c 20 70 67 6e 6f 2c 20 31 2c 0a 20 20 20 Bt, pgno, 1,.
1ddb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ddb9 20 20 20 20 20 20 20 20 20 20 20 20 20 28 4b 65 (Ke
1ddba 79 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34 2e 7a yInfo*)pOp->p4.z
1ddbb 2c 20 75 2e 61 78 2e 70 43 78 2d 3e 70 43 75 72 , u.ax.pCx->pCur
1ddbc 73 6f 72 29 3b 0a 20 20 20 20 20 20 20 20 75 2e sor);. u.
1ddbd 61 78 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f ax.pCx->pKeyInfo
1ddbe 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 = pOp->p4.pKeyI
1ddbf 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 nfo;. u.a
1ddc0 78 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d x.pCx->pKeyInfo-
1ddc1 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62 >enc = ENC(p->db
1ddc2 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1ddc3 20 75 2e 61 78 2e 70 43 78 2d 3e 69 73 54 61 62 u.ax.pCx->isTab
1ddc4 6c 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 le = 0;. }els
1ddc5 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 e{. rc = sq
1ddc6 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 lite3BtreeCursor
1ddc7 28 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 (u.ax.pCx->pBt,
1ddc8 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 31 2c 20 MASTER_ROOT, 1,
1ddc9 30 2c 20 75 2e 61 78 2e 70 43 78 2d 3e 70 43 75 0, u.ax.pCx->pCu
1ddca 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 75 2e 61 rsor);. u.a
1ddcb 78 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d x.pCx->isTable =
1ddcc 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1;. }. }.
1ddcd 75 2e 61 78 2e 70 43 78 2d 3e 69 73 49 6e 64 65 u.ax.pCx->isInde
1ddce 78 20 3d 20 21 75 2e 61 78 2e 70 43 78 2d 3e 69 x = !u.ax.pCx->i
1ddcf 73 54 61 62 6c 65 3b 0a 20 20 62 72 65 61 6b 3b sTable;. break;
1ddd0 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f .}../* Opcode: O
1ddd1 70 65 6e 50 73 65 75 64 6f 20 50 31 20 50 32 20 penPseudo P1 P2
1ddd2 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 P3 * *.**.** Ope
1ddd3 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20 74 n a new cursor t
1ddd4 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 hat points to a
1ddd5 66 61 6b 65 20 74 61 62 6c 65 20 74 68 61 74 20 fake table that
1ddd6 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c contains a singl
1ddd7 65 0a 2a 2a 20 72 6f 77 20 6f 66 20 64 61 74 61 e.** row of data
1ddd8 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f . The content o
1ddd9 66 20 74 68 61 74 20 6f 6e 65 20 72 6f 77 20 69 f that one row i
1ddda 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 n the content of
1dddb 20 6d 65 6d 6f 72 79 0a 2a 2a 20 72 65 67 69 73 memory.** regis
1dddc 74 65 72 20 50 32 2e 20 20 49 6e 20 6f 74 68 65 ter P2. In othe
1dddd 72 20 77 6f 72 64 73 2c 20 63 75 72 73 6f 72 20 r words, cursor
1ddde 50 31 20 62 65 63 6f 6d 65 73 20 61 6e 20 61 6c P1 becomes an al
1dddf 69 61 73 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 ias for the .**
1dde0 4d 45 4d 5f 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 MEM_Blob content
1dde1 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 72 65 contained in re
1dde2 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a gister P2..**.**
1dde3 20 41 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 A pseudo-table
1dde4 63 72 65 61 74 65 64 20 62 79 20 74 68 69 73 20 created by this
1dde5 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 74 opcode is used t
1dde6 6f 20 68 6f 6c 64 20 74 68 65 20 61 20 73 69 6e o hold the a sin
1dde7 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 75 74 70 75 gle.** row outpu
1dde8 74 20 66 72 6f 6d 20 74 68 65 20 73 6f 72 74 65 t from the sorte
1dde9 72 20 73 6f 20 74 68 61 74 20 74 68 65 20 72 6f r so that the ro
1ddea 77 20 63 61 6e 20 62 65 20 64 65 63 6f 6d 70 6f w can be decompo
1ddeb 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64 69 sed into.** indi
1ddec 76 69 64 75 61 6c 20 63 6f 6c 75 6d 6e 73 20 75 vidual columns u
1dded 73 69 6e 67 20 74 68 65 20 4f 50 5f 43 6f 6c 75 sing the OP_Colu
1ddee 6d 6e 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 20 mn opcode. The
1ddef 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 OP_Column opcode
1ddf0 0a 2a 2a 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 .** is the only
1ddf1 63 75 72 73 6f 72 20 6f 70 63 6f 64 65 20 74 68 cursor opcode th
1ddf2 61 74 20 77 6f 72 6b 73 20 77 69 74 68 20 61 20 at works with a
1ddf3 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a pseudo-table..**
1ddf4 0a 2a 2a 20 50 33 20 69 73 20 74 68 65 20 6e 75 .** P3 is the nu
1ddf5 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 mber of fields i
1ddf6 6e 20 74 68 65 20 72 65 63 6f 72 64 73 20 74 68 n the records th
1ddf7 61 74 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 at will be store
1ddf8 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 73 65 75 d by.** the pseu
1ddf9 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 do-table..*/.cas
1ddfa 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 3a e OP_OpenPseudo:
1ddfb 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1ddfc 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1ddfd 65 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a 2f 0a ed into u.ay */.
1ddfe 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1ddff 78 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 x;.#endif /* loc
1de00 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1de01 65 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a 2f 0a ed into u.ay */.
1de02 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1de03 70 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 61 79 2e p1>=0 );. u.ay.
1de04 70 43 78 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 pCx = allocateCu
1de05 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c rsor(p, pOp->p1,
1de06 20 70 4f 70 2d 3e 70 33 2c 20 2d 31 2c 20 30 29 pOp->p3, -1, 0)
1de07 3b 0a 20 20 69 66 28 20 75 2e 61 79 2e 70 43 78 ;. if( u.ay.pCx
1de08 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 ==0 ) goto no_me
1de09 6d 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d 3e 6e m;. u.ay.pCx->n
1de0a 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 75 2e ullRow = 1;. u.
1de0b 61 79 2e 70 43 78 2d 3e 70 73 65 75 64 6f 54 61 ay.pCx->pseudoTa
1de0c 62 6c 65 52 65 67 20 3d 20 70 4f 70 2d 3e 70 32 bleReg = pOp->p2
1de0d 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d 3e 69 73 ;. u.ay.pCx->is
1de0e 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 75 2e 61 Table = 1;. u.a
1de0f 79 2e 70 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d y.pCx->isIndex =
1de10 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0;. break;.}..
1de11 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6c 6f 73 65 /* Opcode: Close
1de12 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 * * * *.**.*
1de13 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 * Close a cursor
1de14 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e previously open
1de15 65 64 20 61 73 20 50 31 2e 20 20 49 66 20 50 31 ed as P1. If P1
1de16 20 69 73 20 6e 6f 74 0a 2a 2a 20 63 75 72 72 65 is not.** curre
1de17 6e 74 6c 79 20 6f 70 65 6e 2c 20 74 68 69 73 20 ntly open, this
1de18 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 instruction is a
1de19 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20 no-op..*/.case
1de1a 4f 50 5f 43 6c 6f 73 65 3a 20 7b 0a 20 20 61 73 OP_Close: {. as
1de1b 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1de1c 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e && pOp->p1<p->n
1de1d 43 75 72 73 6f 72 20 29 3b 0a 20 20 73 71 6c 69 Cursor );. sqli
1de1e 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f te3VdbeFreeCurso
1de1f 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b 70 4f r(p, p->apCsr[pO
1de20 70 2d 3e 70 31 5d 29 3b 0a 20 20 70 2d 3e 61 70 p->p1]);. p->ap
1de21 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 20 3d 20 30 Csr[pOp->p1] = 0
1de22 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1de23 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47 65 20 Opcode: SeekGe
1de24 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a P1 P2 P3 P4 *.**
1de25 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 .** If cursor P1
1de26 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 refers to an SQ
1de27 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 L table (B-Tree
1de28 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 that uses intege
1de29 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 r keys), .** use
1de2a 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1de2b 67 69 73 74 65 72 20 50 33 20 61 73 20 74 68 65 gister P3 as the
1de2c 20 6b 65 79 2e 20 20 49 66 20 63 75 72 73 6f 72 key. If cursor
1de2d 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 P1 refers .** t
1de2e 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 o an SQL index,
1de2f 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 then P3 is the f
1de30 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 irst in an array
1de31 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73 of P4 registers
1de32 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 .** that are us
1de33 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 ed as an unpacke
1de34 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a d index key. .**
1de35 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 .** Reposition c
1de36 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 ursor P1 so that
1de37 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 it points to t
1de38 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72 he smallest entr
1de39 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 67 72 y that .** is gr
1de3a 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 eater than or eq
1de3b 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 76 ual to the key v
1de3c 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 61 alue. If there a
1de3d 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 0a 2a re no records .*
1de3e 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f * greater than o
1de3f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b r equal to the k
1de40 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 ey and P2 is not
1de41 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 zero, then jump
1de42 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 to P2..**.** Se
1de43 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e e also: Found, N
1de44 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 otFound, Distinc
1de45 74 2c 20 53 65 65 6b 4c 74 2c 20 53 65 65 6b 47 t, SeekLt, SeekG
1de46 74 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 t, SeekLe.*/./*
1de47 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47 74 20 50 Opcode: SeekGt P
1de48 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 1 P2 P3 P4 *.**.
1de49 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 ** If cursor P1
1de4a 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c refers to an SQL
1de4b 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74 table (B-Tree t
1de4c 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72 hat uses integer
1de4d 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 keys), .** use
1de4e 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1de4f 69 73 74 65 72 20 50 33 20 61 73 20 61 20 6b 65 ister P3 as a ke
1de50 79 2e 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 y. If cursor P1
1de51 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e refers .** to an
1de52 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e SQL index, then
1de53 20 50 33 20 69 73 20 74 68 65 20 66 69 72 73 74 P3 is the first
1de54 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 in an array of
1de55 50 34 20 72 65 67 69 73 74 65 72 73 20 0a 2a 2a P4 registers .**
1de56 20 74 68 61 74 20 61 72 65 20 75 73 65 64 20 61 that are used a
1de57 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e s an unpacked in
1de58 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 dex key. .**.**
1de59 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f Reposition curso
1de5a 72 20 50 31 20 73 6f 20 74 68 61 74 20 20 69 74 r P1 so that it
1de5b 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 points to the s
1de5c 6d 61 6c 6c 65 73 74 20 65 6e 74 72 79 20 74 68 mallest entry th
1de5d 61 74 20 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 at .** is greate
1de5e 72 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20 76 r than the key v
1de5f 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 61 alue. If there a
1de60 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 67 72 re no records gr
1de61 65 61 74 65 72 20 74 68 61 6e 20 0a 2a 2a 20 74 eater than .** t
1de62 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 he key and P2 is
1de63 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 not zero, then
1de64 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a jump to P2..**.*
1de65 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e * See also: Foun
1de66 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 d, NotFound, Dis
1de67 74 69 6e 63 74 2c 20 53 65 65 6b 4c 74 2c 20 53 tinct, SeekLt, S
1de68 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a 2a 2f eekGe, SeekLe.*/
1de69 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b ./* Opcode: Seek
1de6a 4c 74 20 50 31 20 50 32 20 50 33 20 50 34 20 2a Lt P1 P2 P3 P4 *
1de6b 20 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f .**.** If curso
1de6c 72 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 r P1 refers to a
1de6d 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 n SQL table (B-T
1de6e 72 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e ree that uses in
1de6f 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a teger keys), .**
1de70 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 use the value i
1de71 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 n register P3 as
1de72 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f a key. If curso
1de73 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 r P1 refers .**
1de74 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c to an SQL index,
1de75 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 then P3 is the
1de76 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 first in an arra
1de77 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 y of P4 register
1de78 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 s .** that are u
1de79 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b sed as an unpack
1de7a 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a ed index key. .*
1de7b 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 *.** Reposition
1de7c 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 cursor P1 so tha
1de7d 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 t it points to
1de7e 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 the largest entr
1de7f 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65 y that .** is le
1de80 73 73 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20 ss than the key
1de81 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 value. If there
1de82 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 6c are no records l
1de83 65 73 73 20 74 68 61 6e 20 0a 2a 2a 20 74 68 65 ess than .** the
1de84 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e key and P2 is n
1de85 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 ot zero, then ju
1de86 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 mp to P2..**.**
1de87 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c See also: Found,
1de88 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 NotFound, Disti
1de89 6e 63 74 2c 20 53 65 65 6b 47 74 2c 20 53 65 65 nct, SeekGt, See
1de8a 6b 47 65 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f kGe, SeekLe.*/./
1de8b 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 4c 65 * Opcode: SeekLe
1de8c 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a P1 P2 P3 P4 *.*
1de8d 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 *.** If cursor P
1de8e 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 1 refers to an S
1de8f 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 QL table (B-Tree
1de90 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 that uses integ
1de91 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 er keys), .** us
1de92 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 e the value in r
1de93 65 67 69 73 74 65 72 20 50 33 20 61 73 20 61 20 egister P3 as a
1de94 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 20 50 key. If cursor P
1de95 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 1 refers .** to
1de96 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 an SQL index, th
1de97 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 69 72 en P3 is the fir
1de98 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f st in an array o
1de99 66 20 50 34 20 72 65 67 69 73 74 65 72 73 20 0a f P4 registers .
1de9a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 65 64 ** that are used
1de9b 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 as an unpacked
1de9c 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a index key. .**.*
1de9d 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 * Reposition cur
1de9e 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 69 sor P1 so that i
1de9f 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 t points to the
1dea0 6c 61 72 67 65 73 74 20 65 6e 74 72 79 20 74 68 largest entry th
1dea1 61 74 20 0a 2a 2a 20 69 73 20 6c 65 73 73 20 74 at .** is less t
1dea2 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 han or equal to
1dea3 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 the key value. I
1dea4 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 f there are no r
1dea5 65 63 6f 72 64 73 20 0a 2a 2a 20 6c 65 73 73 20 ecords .** less
1dea6 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f than or equal to
1dea7 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 the key and P2
1dea8 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 is not zero, the
1dea9 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a n jump to P2..**
1deaa 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f .** See also: Fo
1deab 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 und, NotFound, D
1deac 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 47 74 2c istinct, SeekGt,
1dead 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c 74 0a SeekGe, SeekLt.
1deae 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b 4c */.case OP_SeekL
1deaf 74 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 t: /* ju
1deb0 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 mp, in3 */.case
1deb1 4f 50 5f 53 65 65 6b 4c 65 3a 20 20 20 20 20 20 OP_SeekLe:
1deb2 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 /* jump, in3
1deb3 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b 47 */.case OP_SeekG
1deb4 65 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 e: /* ju
1deb5 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 mp, in3 */.case
1deb6 4f 50 5f 53 65 65 6b 47 74 3a 20 7b 20 20 20 20 OP_SeekGt: {
1deb7 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 /* jump, in3
1deb8 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1deb9 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1deba 65 64 20 69 6e 74 6f 20 75 2e 61 7a 20 2a 2f 0a ed into u.az */.
1debb 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 6e 74 int res;. int
1debc 20 6f 63 3b 0a 20 20 56 64 62 65 43 75 72 73 6f oc;. VdbeCurso
1debd 72 20 2a 70 43 3b 0a 20 20 55 6e 70 61 63 6b 65 r *pC;. Unpacke
1debe 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 69 6e 74 dRecord r;. int
1debf 20 6e 46 69 65 6c 64 3b 0a 20 20 69 36 34 20 69 nField;. i64 i
1dec0 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 Key; /* The
1dec1 20 72 6f 77 69 64 20 77 65 20 61 72 65 20 74 6f rowid we are to
1dec2 20 73 65 65 6b 20 74 6f 20 2a 2f 0a 23 65 6e 64 seek to */.#end
1dec3 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1dec4 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1dec5 20 75 2e 61 7a 20 2a 2f 0a 0a 20 20 61 73 73 65 u.az */.. asse
1dec6 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1dec7 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 & pOp->p1<p->nCu
1dec8 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 rsor );. assert
1dec9 28 20 70 4f 70 2d 3e 70 32 21 3d 30 20 29 3b 0a ( pOp->p2!=0 );.
1deca 20 20 75 2e 61 7a 2e 70 43 20 3d 20 70 2d 3e 61 u.az.pC = p->a
1decb 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 pCsr[pOp->p1];.
1decc 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 70 43 assert( u.az.pC
1decd 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
1dece 20 75 2e 61 7a 2e 70 43 2d 3e 70 73 65 75 64 6f u.az.pC->pseudo
1decf 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 TableReg==0 );.
1ded0 20 69 66 28 20 75 2e 61 7a 2e 70 43 2d 3e 70 43 if( u.az.pC->pC
1ded1 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20 20 ursor!=0 ){.
1ded2 75 2e 61 7a 2e 6f 63 20 3d 20 70 4f 70 2d 3e 6f u.az.oc = pOp->o
1ded3 70 63 6f 64 65 3b 0a 20 20 20 20 75 2e 61 7a 2e pcode;. u.az.
1ded4 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b pC->nullRow = 0;
1ded5 0a 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 70 43 . if( u.az.pC
1ded6 2d 3e 69 73 54 61 62 6c 65 20 29 7b 0a 20 20 20 ->isTable ){.
1ded7 20 20 20 2f 2a 20 54 68 65 20 69 6e 70 75 74 20 /* The input
1ded8 76 61 6c 75 65 20 69 6e 20 50 33 20 6d 69 67 68 value in P3 migh
1ded9 74 20 62 65 20 6f 66 20 61 6e 79 20 74 79 70 65 t be of any type
1deda 3a 20 69 6e 74 65 67 65 72 2c 20 72 65 61 6c 2c : integer, real,
1dedb 20 73 74 72 69 6e 67 2c 0a 20 20 20 20 20 20 2a string,. *
1dedc 2a 20 62 6c 6f 62 2c 20 6f 72 20 4e 55 4c 4c 2e * blob, or NULL.
1dedd 20 20 42 75 74 20 69 74 20 6e 65 65 64 73 20 74 But it needs t
1dede 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 20 o be an integer
1dedf 62 65 66 6f 72 65 20 77 65 20 63 61 6e 20 64 6f before we can do
1dee0 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73 65 . ** the se
1dee1 65 6b 2c 20 73 6f 20 63 6f 76 65 72 74 20 69 74 ek, so covert it
1dee2 2e 20 2a 2f 0a 20 20 20 20 20 20 61 70 70 6c 79 . */. apply
1dee3 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 NumericAffinity(
1dee4 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 75 2e 61 pIn3);. u.a
1dee5 7a 2e 69 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 z.iKey = sqlite3
1dee6 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e VdbeIntValue(pIn
1dee7 33 29 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e 70 3);. u.az.p
1dee8 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 C->rowidIsValid
1dee9 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 = 0;.. /* I
1deea 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 63 f the P3 value c
1deeb 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6f 6e 76 ould not be conv
1deec 65 72 74 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e erted into an in
1deed 74 65 67 65 72 20 77 69 74 68 6f 75 74 0a 20 20 teger without.
1deee 20 20 20 20 2a 2a 20 6c 6f 73 73 20 6f 66 20 69 ** loss of i
1deef 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65 6e nformation, then
1def0 20 73 70 65 63 69 61 6c 20 70 72 6f 63 65 73 73 special process
1def1 69 6e 67 20 69 73 20 72 65 71 75 69 72 65 64 2e ing is required.
1def2 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 .. */. if(
1def3 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d (pIn3->flags & M
1def4 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 EM_Int)==0 ){.
1def5 20 20 20 20 20 20 69 66 28 20 28 70 49 6e 33 2d if( (pIn3-
1def6 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 >flags & MEM_Rea
1def7 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 l)==0 ){.
1def8 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 33 20 /* If the P3
1def9 76 61 6c 75 65 20 63 61 6e 6e 6f 74 20 62 65 20 value cannot be
1defa 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 converted into a
1defb 6e 79 20 6b 69 6e 64 20 6f 66 20 61 20 6e 75 6d ny kind of a num
1defc 62 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 2a ber,. *
1defd 2a 20 74 68 65 6e 20 74 68 65 20 73 65 65 6b 20 * then the seek
1defe 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2c is not possible,
1deff 20 73 6f 20 6a 75 6d 70 20 74 6f 20 50 32 20 2a so jump to P2 *
1df00 2f 0a 20 20 20 20 20 20 20 20 20 20 70 63 20 3d /. pc =
1df01 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1df02 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
1df03 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1df04 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68 20 /* If we reach
1df05 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e this point, then
1df06 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 6d 75 the P3 value mu
1df07 73 74 20 62 65 20 61 20 66 6c 6f 61 74 69 6e 67 st be a floating
1df08 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e . ** poin
1df09 74 20 6e 75 6d 62 65 72 2e 20 2a 2f 0a 20 20 20 t number. */.
1df0a 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 49 assert( (pI
1df0b 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n3->flags & MEM_
1df0c 52 65 61 6c 29 21 3d 30 20 29 3b 0a 0a 20 20 20 Real)!=0 );..
1df0d 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 69 4b if( u.az.iK
1df0e 65 79 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 ey==SMALLEST_INT
1df0f 36 34 20 26 26 20 28 70 49 6e 33 2d 3e 72 3c 28 64 && (pIn3->r<(
1df10 64 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 double)u.az.iKey
1df11 20 7c 7c 20 70 49 6e 33 2d 3e 72 3e 30 29 20 29 || pIn3->r>0) )
1df12 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 {. /* T
1df13 68 65 20 50 33 20 76 61 6c 75 65 20 69 73 20 74 he P3 value is t
1df14 6f 6f 20 6c 61 72 67 65 20 69 6e 20 6d 61 67 6e oo large in magn
1df15 69 74 75 64 65 20 74 6f 20 62 65 20 65 78 70 72 itude to be expr
1df16 65 73 73 65 64 20 61 73 20 61 6e 0a 20 20 20 20 essed as an.
1df17 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 ** integer
1df18 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 75 . */. u
1df19 2e 61 7a 2e 72 65 73 20 3d 20 31 3b 0a 20 20 20 .az.res = 1;.
1df1a 20 20 20 20 20 20 20 69 66 28 20 70 49 6e 33 2d if( pIn3-
1df1b 3e 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 >r<0 ){.
1df1c 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d if( u.az.oc=
1df1d 3d 4f 50 5f 53 65 65 6b 47 74 20 7c 7c 20 75 2e =OP_SeekGt || u.
1df1e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 az.oc==OP_SeekGe
1df1f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1df20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1df21 72 65 65 46 69 72 73 74 28 75 2e 61 7a 2e 70 43 reeFirst(u.az.pC
1df22 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a ->pCursor, &u.az
1df23 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 20 .res);.
1df24 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1df25 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 ITE_OK ) goto ab
1df26 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1df27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
1df28 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
1df29 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
1df2a 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 u.az.oc==OP_See
1df2b 6b 4c 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d kLt || u.az.oc==
1df2c 4f 50 5f 53 65 65 6b 4c 65 20 29 7b 0a 20 20 20 OP_SeekLe ){.
1df2d 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
1df2e 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 sqlite3BtreeLast
1df2f 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f (u.az.pC->pCurso
1df30 72 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 r, &u.az.res);.
1df31 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 if(
1df32 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1df33 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1df34 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 to_error;.
1df35 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1df36 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 }. if
1df37 28 20 75 2e 61 7a 2e 72 65 73 20 29 7b 0a 20 20 ( u.az.res ){.
1df38 20 20 20 20 20 20 20 20 20 20 70 63 20 3d 20 70 pc = p
1df39 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 Op->p2 - 1;.
1df3a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1df3b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
1df3c 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 7a 2e }else if( u.az.
1df3d 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c oc==OP_SeekLt ||
1df3e 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 u.az.oc==OP_See
1df3f 6b 47 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 kGe ){.
1df40 20 2f 2a 20 55 73 65 20 74 68 65 20 63 65 69 6c /* Use the ceil
1df41 69 6e 67 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 ing() function t
1df42 6f 20 63 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e o convert real->
1df43 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 int */.
1df44 20 69 66 28 20 70 49 6e 33 2d 3e 72 20 3e 20 28 if( pIn3->r > (
1df45 64 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 double)u.az.iKey
1df46 20 29 20 75 2e 61 7a 2e 69 4b 65 79 2b 2b 3b 0a ) u.az.iKey++;.
1df47 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
1df48 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 /* Use
1df49 74 68 65 20 66 6c 6f 6f 72 28 29 20 66 75 6e 63 the floor() func
1df4a 74 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 tion to convert
1df4b 72 65 61 6c 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20 real->int */.
1df4c 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 assert( u
1df4d 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c .az.oc==OP_SeekL
1df4e 65 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 e || u.az.oc==OP
1df4f 5f 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 20 _SeekGt );.
1df50 20 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 if( pIn3->r
1df51 20 3c 20 28 64 6f 75 62 6c 65 29 75 2e 61 7a 2e < (double)u.az.
1df52 69 4b 65 79 20 29 20 75 2e 61 7a 2e 69 4b 65 79 iKey ) u.az.iKey
1df53 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 --;. }.
1df54 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d }. rc =
1df55 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
1df56 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 61 7a etoUnpacked(u.az
1df57 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c .pC->pCursor, 0,
1df58 20 28 75 36 34 29 75 2e 61 7a 2e 69 4b 65 79 2c (u64)u.az.iKey,
1df59 20 30 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 0, &u.az.res);.
1df5a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1df5b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1df5c 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 goto abort_du
1df5d 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1df5e 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 75 2e }. if( u.
1df5f 61 7a 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 az.res==0 ){.
1df60 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f u.az.pC->ro
1df61 77 69 64 49 73 56 61 6c 69 64 20 3d 20 31 3b 0a widIsValid = 1;.
1df62 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d u.az.pC-
1df63 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 75 2e 61 >lastRowid = u.a
1df64 7a 2e 69 4b 65 79 3b 0a 20 20 20 20 20 20 7d 0a z.iKey;. }.
1df65 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1df66 20 75 2e 61 7a 2e 6e 46 69 65 6c 64 20 3d 20 70 u.az.nField = p
1df67 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 20 20 Op->p4.i;.
1df68 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 assert( pOp->p4t
1df69 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b ype==P4_INT32 );
1df6a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 . assert( u
1df6b 2e 61 7a 2e 6e 46 69 65 6c 64 3e 30 20 29 3b 0a .az.nField>0 );.
1df6c 20 20 20 20 20 20 75 2e 61 7a 2e 72 2e 70 4b 65 u.az.r.pKe
1df6d 79 49 6e 66 6f 20 3d 20 75 2e 61 7a 2e 70 43 2d yInfo = u.az.pC-
1df6e 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 >pKeyInfo;.
1df6f 20 75 2e 61 7a 2e 72 2e 6e 46 69 65 6c 64 20 3d u.az.r.nField =
1df70 20 28 75 31 36 29 75 2e 61 7a 2e 6e 46 69 65 6c (u16)u.az.nFiel
1df71 64 3b 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 d;. if( u.a
1df72 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 z.oc==OP_SeekGt
1df73 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 || u.az.oc==OP_S
1df74 65 65 6b 4c 65 20 29 7b 0a 20 20 20 20 20 20 20 eekLe ){.
1df75 20 75 2e 61 7a 2e 72 2e 66 6c 61 67 73 20 3d 20 u.az.r.flags =
1df76 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 UNPACKED_INCRKEY
1df77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1df78 20 20 20 20 20 20 20 75 2e 61 7a 2e 72 2e 66 6c u.az.r.fl
1df79 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d ags = 0;. }
1df7a 0a 20 20 20 20 20 20 75 2e 61 7a 2e 72 2e 61 4d . u.az.r.aM
1df7b 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f em = &p->aMem[pO
1df7c 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 72 63 p->p3];. rc
1df7d 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d = sqlite3BtreeM
1df7e 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e ovetoUnpacked(u.
1df7f 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 az.pC->pCursor,
1df80 26 75 2e 61 7a 2e 72 2c 20 30 2c 20 30 2c 20 26 &u.az.r, 0, 0, &
1df81 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 u.az.res);.
1df82 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1df83 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
1df84 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1df85 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 error;. }.
1df86 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f u.az.pC->ro
1df87 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a widIsValid = 0;.
1df88 20 20 20 20 7d 0a 20 20 20 20 75 2e 61 7a 2e 70 }. u.az.p
1df89 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 C->deferredMovet
1df8a 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e 61 7a 2e o = 0;. u.az.
1df8b 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 pC->cacheStatus
1df8c 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 23 = CACHE_STALE;.#
1df8d 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
1df8e 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65 T. sqlite3_se
1df8f 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 arch_count++;.#e
1df90 6e 64 69 66 0a 20 20 20 20 69 66 28 20 75 2e 61 ndif. if( u.a
1df91 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 z.oc==OP_SeekGe
1df92 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 || u.az.oc==OP_S
1df93 65 65 6b 47 74 20 29 7b 0a 20 20 20 20 20 20 69 eekGt ){. i
1df94 66 28 20 75 2e 61 7a 2e 72 65 73 3c 30 20 7c 7c f( u.az.res<0 ||
1df95 20 28 75 2e 61 7a 2e 72 65 73 3d 3d 30 20 26 26 (u.az.res==0 &&
1df96 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 u.az.oc==OP_See
1df97 6b 47 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 kGt) ){.
1df98 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1df99 65 4e 65 78 74 28 75 2e 61 7a 2e 70 43 2d 3e 70 eNext(u.az.pC->p
1df9a 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 65 Cursor, &u.az.re
1df9b 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 s);. if(
1df9c 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
1df9d 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1df9e 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 o_error;.
1df9f 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 u.az.pC->rowidI
1dfa0 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 sValid = 0;.
1dfa1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1dfa2 20 75 2e 61 7a 2e 72 65 73 20 3d 20 30 3b 0a 20 u.az.res = 0;.
1dfa3 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
1dfa4 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1dfa5 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b u.az.oc==OP_Seek
1dfa6 4c 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f Lt || u.az.oc==O
1dfa7 50 5f 53 65 65 6b 4c 65 20 29 3b 0a 20 20 20 20 P_SeekLe );.
1dfa8 20 20 69 66 28 20 75 2e 61 7a 2e 72 65 73 3e 30 if( u.az.res>0
1dfa9 20 7c 7c 20 28 75 2e 61 7a 2e 72 65 73 3d 3d 30 || (u.az.res==0
1dfaa 20 26 26 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f && u.az.oc==OP_
1dfab 53 65 65 6b 4c 74 29 20 29 7b 0a 20 20 20 20 20 SeekLt) ){.
1dfac 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1dfad 74 72 65 65 50 72 65 76 69 6f 75 73 28 75 2e 61 treePrevious(u.a
1dfae 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 z.pC->pCursor, &
1dfaf 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 u.az.res);.
1dfb0 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1dfb1 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 E_OK ) goto abor
1dfb2 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1dfb3 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d u.az.pC-
1dfb4 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 >rowidIsValid =
1dfb5 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 0;. }else{.
1dfb6 20 20 20 20 20 20 20 20 2f 2a 20 75 2e 61 7a 2e /* u.az.
1dfb7 72 65 73 20 6d 69 67 68 74 20 62 65 20 6e 65 67 res might be neg
1dfb8 61 74 69 76 65 20 62 65 63 61 75 73 65 20 74 68 ative because th
1dfb9 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 e table is empty
1dfba 2e 20 20 43 68 65 63 6b 20 74 6f 0a 20 20 20 20 . Check to.
1dfbb 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20 74 68 ** see if th
1dfbc 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a is is the case..
1dfbd 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
1dfbe 20 20 20 75 2e 61 7a 2e 72 65 73 20 3d 20 73 71 u.az.res = sq
1dfbf 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28 75 2e lite3BtreeEof(u.
1dfc0 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b az.pC->pCursor);
1dfc1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1dfc2 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1dfc3 70 32 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 p2>0 );. if(
1dfc4 75 2e 61 7a 2e 72 65 73 20 29 7b 0a 20 20 20 20 u.az.res ){.
1dfc5 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1dfc6 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 1;. }. }els
1dfc7 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 e{. /* This h
1dfc8 61 70 70 65 6e 73 20 77 68 65 6e 20 61 74 74 65 appens when atte
1dfc9 6d 70 74 69 6e 67 20 74 6f 20 6f 70 65 6e 20 74 mpting to open t
1dfca 68 65 20 73 71 6c 69 74 65 33 5f 6d 61 73 74 65 he sqlite3_maste
1dfcb 72 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 66 r table. ** f
1dfcc 6f 72 20 72 65 61 64 20 61 63 63 65 73 73 20 72 or read access r
1dfcd 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 4d eturns SQLITE_EM
1dfce 50 54 59 2e 20 49 6e 20 74 68 69 73 20 63 61 73 PTY. In this cas
1dfcf 65 20 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20 e always. **
1dfd0 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 28 73 take the jump (s
1dfd1 69 6e 63 65 20 74 68 65 72 65 20 61 72 65 20 6e ince there are n
1dfd2 6f 20 72 65 63 6f 72 64 73 20 69 6e 20 74 68 65 o records in the
1dfd3 20 74 61 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a table).. */.
1dfd4 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 pc = pOp->p2
1dfd5 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 - 1;. }. brea
1dfd6 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1dfd7 20 53 65 65 6b 20 50 31 20 50 32 20 2a 20 2a 20 Seek P1 P2 * *
1dfd8 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 6e *.**.** P1 is an
1dfd9 20 6f 70 65 6e 20 74 61 62 6c 65 20 63 75 72 73 open table curs
1dfda 6f 72 20 61 6e 64 20 50 32 20 69 73 20 61 20 72 or and P2 is a r
1dfdb 6f 77 69 64 20 69 6e 74 65 67 65 72 2e 20 20 41 owid integer. A
1dfdc 72 72 61 6e 67 65 0a 2a 2a 20 66 6f 72 20 50 31 rrange.** for P1
1dfdd 20 74 6f 20 6d 6f 76 65 20 73 6f 20 74 68 61 74 to move so that
1dfde 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 it points to th
1dfdf 65 20 72 6f 77 69 64 20 67 69 76 65 6e 20 62 79 e rowid given by
1dfe0 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 P2..**.** This
1dfe1 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20 64 65 is actually a de
1dfe2 66 65 72 72 65 64 20 73 65 65 6b 2e 20 20 4e 6f ferred seek. No
1dfe3 74 68 69 6e 67 20 61 63 74 75 61 6c 6c 79 20 68 thing actually h
1dfe4 61 70 70 65 6e 73 20 75 6e 74 69 6c 0a 2a 2a 20 appens until.**
1dfe5 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 the cursor is us
1dfe6 65 64 20 74 6f 20 72 65 61 64 20 61 20 72 65 63 ed to read a rec
1dfe7 6f 72 64 2e 20 20 54 68 61 74 20 77 61 79 2c 20 ord. That way,
1dfe8 69 66 20 6e 6f 20 72 65 61 64 73 0a 2a 2a 20 6f if no reads.** o
1dfe9 63 63 75 72 2c 20 6e 6f 20 75 6e 6e 65 63 65 73 ccur, no unneces
1dfea 73 61 72 79 20 49 2f 4f 20 68 61 70 70 65 6e 73 sary I/O happens
1dfeb 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 ..*/.case OP_See
1dfec 6b 3a 20 7b 20 20 20 20 2f 2a 20 69 6e 32 20 2a k: { /* in2 *
1dfed 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 /.#if 0 /* loca
1dfee 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1dfef 64 20 69 6e 74 6f 20 75 2e 62 61 20 2a 2f 0a 20 d into u.ba */.
1dff0 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1dff1 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1dff2 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1dff3 20 69 6e 74 6f 20 75 2e 62 61 20 2a 2f 0a 0a 20 into u.ba */..
1dff4 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 assert( pOp->p1
1dff5 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 >=0 && pOp->p1<p
1dff6 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 ->nCursor );. u
1dff7 2e 62 61 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 .ba.pC = p->apCs
1dff8 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 r[pOp->p1];. as
1dff9 73 65 72 74 28 20 75 2e 62 61 2e 70 43 21 3d 30 sert( u.ba.pC!=0
1dffa 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 );. if( ALWAYS
1dffb 28 75 2e 62 61 2e 70 43 2d 3e 70 43 75 72 73 6f (u.ba.pC->pCurso
1dffc 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 61 73 73 r!=0) ){. ass
1dffd 65 72 74 28 20 75 2e 62 61 2e 70 43 2d 3e 69 73 ert( u.ba.pC->is
1dffe 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 75 2e 62 Table );. u.b
1dfff 61 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 a.pC->nullRow =
1e000 30 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e 0;. u.ba.pC->
1e001 6d 6f 76 65 74 6f 54 61 72 67 65 74 20 3d 20 73 movetoTarget = s
1e002 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c qlite3VdbeIntVal
1e003 75 65 28 70 49 6e 32 29 3b 0a 20 20 20 20 75 2e ue(pIn2);. u.
1e004 62 61 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 ba.pC->rowidIsVa
1e005 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 lid = 0;. u.b
1e006 61 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f a.pC->deferredMo
1e007 76 65 74 6f 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 veto = 1;. }.
1e008 62 72 65 61 6b 3b 0a 7d 0a 20 20 0a 0a 2f 2a 20 break;.}. ../*
1e009 4f 70 63 6f 64 65 3a 20 46 6f 75 6e 64 20 50 31 Opcode: Found P1
1e00a 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1e00b 20 52 65 67 69 73 74 65 72 20 50 33 20 68 6f 6c Register P3 hol
1e00c 64 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 ds a blob constr
1e00d 75 63 74 65 64 20 62 79 20 4d 61 6b 65 52 65 63 ucted by MakeRec
1e00e 6f 72 64 2e 20 20 50 31 20 69 73 20 61 6e 20 69 ord. P1 is an i
1e00f 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 ndex..** If an e
1e010 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 65 ntry that matche
1e011 73 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 s the value in r
1e012 65 67 69 73 74 65 72 20 70 33 20 65 78 69 73 74 egister p3 exist
1e013 73 20 69 6e 20 50 31 20 74 68 65 6e 0a 2a 2a 20 s in P1 then.**
1e014 6a 75 6d 70 20 74 6f 20 50 32 2e 20 20 49 66 20 jump to P2. If
1e015 74 68 65 20 50 33 20 76 61 6c 75 65 20 64 6f 65 the P3 value doe
1e016 73 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 s not match any
1e017 65 6e 74 72 79 20 69 6e 20 50 31 0a 2a 2a 20 74 entry in P1.** t
1e018 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 hen fall thru.
1e019 54 68 65 20 50 31 20 63 75 72 73 6f 72 20 69 73 The P1 cursor is
1e01a 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 left pointing a
1e01b 74 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65 t the matching e
1e01c 6e 74 72 79 0a 2a 2a 20 69 66 20 69 74 20 65 78 ntry.** if it ex
1e01d 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ists..**.** This
1e01e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 instruction is
1e01f 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e used to implemen
1e020 74 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f t the IN operato
1e021 72 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 6c r where the.** l
1e022 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73 eft-hand side is
1e023 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d a SELECT statem
1e024 65 6e 74 2e 20 20 50 31 20 6d 61 79 20 62 65 20 ent. P1 may be
1e025 61 20 74 72 75 65 20 69 6e 64 65 78 2c 20 6f 72 a true index, or
1e026 20 69 74 0a 2a 2a 20 6d 61 79 20 62 65 20 61 20 it.** may be a
1e027 74 65 6d 70 6f 72 61 72 79 20 69 6e 64 65 78 20 temporary index
1e028 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 72 that holds the r
1e029 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 53 45 esults of the SE
1e02a 4c 45 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e LECT.** statemen
1e02b 74 2e 20 20 20 54 68 69 73 20 69 6e 73 74 72 75 t. This instru
1e02c 63 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 75 73 ction is also us
1e02d 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 ed to implement
1e02e 74 68 65 0a 2a 2a 20 44 49 53 54 49 4e 43 54 20 the.** DISTINCT
1e02f 6b 65 79 77 6f 72 64 20 69 6e 20 53 45 4c 45 43 keyword in SELEC
1e030 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a T statements..**
1e031 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 .** This instruc
1e032 74 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 20 69 tion checks if i
1e033 6e 64 65 78 20 50 31 20 63 6f 6e 74 61 69 6e 73 ndex P1 contains
1e034 20 61 20 72 65 63 6f 72 64 20 66 6f 72 20 77 68 a record for wh
1e035 69 63 68 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 ich .** the firs
1e036 74 20 4e 20 73 65 72 69 61 6c 69 7a 65 64 20 76 t N serialized v
1e037 61 6c 75 65 73 20 65 78 61 63 74 6c 79 20 6d 61 alues exactly ma
1e038 74 63 68 20 74 68 65 20 4e 20 73 65 72 69 61 6c tch the N serial
1e039 69 7a 65 64 20 76 61 6c 75 65 73 0a 2a 2a 20 69 ized values.** i
1e03a 6e 20 74 68 65 20 72 65 63 6f 72 64 20 69 6e 20 n the record in
1e03b 72 65 67 69 73 74 65 72 20 50 33 2c 20 77 68 65 register P3, whe
1e03c 72 65 20 4e 20 69 73 20 74 68 65 20 74 6f 74 61 re N is the tota
1e03d 6c 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 l number of valu
1e03e 65 73 20 69 6e 0a 2a 2a 20 74 68 65 20 50 33 20 es in.** the P3
1e03f 72 65 63 6f 72 64 20 28 74 68 65 20 50 33 20 72 record (the P3 r
1e040 65 63 6f 72 64 20 69 73 20 61 20 70 72 65 66 69 ecord is a prefi
1e041 78 20 6f 66 20 74 68 65 20 50 31 20 72 65 63 6f x of the P1 reco
1e042 72 64 29 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 rd). .**.** See
1e043 61 6c 73 6f 3a 20 4e 6f 74 46 6f 75 6e 64 2c 20 also: NotFound,
1e044 49 73 55 6e 69 71 75 65 2c 20 4e 6f 74 45 78 69 IsUnique, NotExi
1e045 73 74 73 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 sts.*/./* Opcode
1e046 3a 20 4e 6f 74 46 6f 75 6e 64 20 50 31 20 50 32 : NotFound P1 P2
1e047 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 P3 * *.**.** Re
1e048 67 69 73 74 65 72 20 50 33 20 68 6f 6c 64 73 20 gister P3 holds
1e049 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 a blob construct
1e04a 65 64 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 ed by MakeRecord
1e04b 2e 20 20 50 31 20 69 73 0a 2a 2a 20 61 6e 20 69 . P1 is.** an i
1e04c 6e 64 65 78 2e 20 20 49 66 20 6e 6f 20 65 6e 74 ndex. If no ent
1e04d 72 79 20 65 78 69 73 74 73 20 69 6e 20 50 31 20 ry exists in P1
1e04e 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 that matches the
1e04f 20 62 6c 6f 62 20 74 68 65 6e 20 6a 75 6d 70 0a blob then jump.
1e050 2a 2a 20 74 6f 20 50 32 2e 20 20 49 66 20 61 6e ** to P2. If an
1e051 20 65 6e 74 72 79 20 64 6f 65 73 20 65 78 69 73 entry does exis
1e052 74 69 6e 67 2c 20 66 61 6c 6c 20 74 68 72 6f 75 ting, fall throu
1e053 67 68 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 gh. The cursor
1e054 69 73 20 6c 65 66 74 0a 2a 2a 20 70 6f 69 6e 74 is left.** point
1e055 69 6e 67 20 74 6f 20 74 68 65 20 65 6e 74 72 79 ing to the entry
1e056 20 74 68 61 74 20 6d 61 74 63 68 65 73 2e 0a 2a that matches..*
1e057 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 *.** See also: F
1e058 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c ound, NotExists,
1e059 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 IsUnique.*/.cas
1e05a 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 3a 20 20 e OP_NotFound:
1e05b 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1e05c 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 6f 75 3 */.case OP_Fou
1e05d 6e 64 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 nd: { /*
1e05e 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 jump, in3 */.#if
1e05f 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1e060 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e061 6f 20 75 2e 62 62 20 2a 2f 0a 20 20 69 6e 74 20 o u.bb */. int
1e062 61 6c 72 65 61 64 79 45 78 69 73 74 73 3b 0a 20 alreadyExists;.
1e063 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1e064 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 55 6e . int res;. Un
1e065 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 packedRecord *pI
1e066 64 78 4b 65 79 3b 0a 20 20 63 68 61 72 20 61 54 dxKey;. char aT
1e067 65 6d 70 52 65 63 5b 52 4f 55 4e 44 38 28 73 69 empRec[ROUND8(si
1e068 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63 zeof(UnpackedRec
1e069 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d ord)) + sizeof(M
1e06a 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a 23 65 6e 64 em)*3 + 7];.#end
1e06b 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1e06c 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e06d 20 75 2e 62 62 20 2a 2f 0a 0a 23 69 66 64 65 66 u.bb */..#ifdef
1e06e 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 SQLITE_TEST. s
1e06f 71 6c 69 74 65 33 5f 66 6f 75 6e 64 5f 63 6f 75 qlite3_found_cou
1e070 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 nt++;.#endif..
1e071 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69 73 u.bb.alreadyExis
1e072 74 73 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 ts = 0;. assert
1e073 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1e074 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 pOp->p1<p->nCurs
1e075 6f 72 20 29 3b 0a 20 20 75 2e 62 62 2e 70 43 20 or );. u.bb.pC
1e076 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e = p->apCsr[pOp->
1e077 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1e078 2e 62 62 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69 .bb.pC!=0 );. i
1e079 66 28 20 41 4c 57 41 59 53 28 75 2e 62 62 2e 70 f( ALWAYS(u.bb.p
1e07a 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 29 20 29 C->pCursor!=0) )
1e07b 7b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 {.. assert( u
1e07c 2e 62 62 2e 70 43 2d 3e 69 73 54 61 62 6c 65 3d .bb.pC->isTable=
1e07d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =0 );. assert
1e07e 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 ( pIn3->flags &
1e07f 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 MEM_Blob );.
1e080 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33 29 ExpandBlob(pIn3)
1e081 3b 0a 20 20 20 20 75 2e 62 62 2e 70 49 64 78 4b ;. u.bb.pIdxK
1e082 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 ey = sqlite3Vdbe
1e083 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 75 2e 62 RecordUnpack(u.b
1e084 62 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20 b.pC->pKeyInfo,
1e085 70 49 6e 33 2d 3e 6e 2c 20 70 49 6e 33 2d 3e 7a pIn3->n, pIn3->z
1e086 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1e087 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e088 20 20 20 20 20 20 20 20 75 2e 62 62 2e 61 54 65 u.bb.aTe
1e089 6d 70 52 65 63 2c 20 73 69 7a 65 6f 66 28 75 2e mpRec, sizeof(u.
1e08a 62 62 2e 61 54 65 6d 70 52 65 63 29 29 3b 0a 20 bb.aTempRec));.
1e08b 20 20 20 69 66 28 20 75 2e 62 62 2e 70 49 64 78 if( u.bb.pIdx
1e08c 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Key==0 ){.
1e08d 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 goto no_mem;.
1e08e 20 7d 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e }. if( pOp->
1e08f 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75 6e 64 opcode==OP_Found
1e090 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 62 2e 70 ){. u.bb.p
1e091 49 64 78 4b 65 79 2d 3e 66 6c 61 67 73 20 7c 3d IdxKey->flags |=
1e092 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 UNPACKED_PREFIX
1e093 5f 4d 41 54 43 48 3b 0a 20 20 20 20 7d 0a 20 20 _MATCH;. }.
1e094 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1e095 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 reeMovetoUnpacke
1e096 64 28 75 2e 62 62 2e 70 43 2d 3e 70 43 75 72 73 d(u.bb.pC->pCurs
1e097 6f 72 2c 20 75 2e 62 62 2e 70 49 64 78 4b 65 79 or, u.bb.pIdxKey
1e098 2c 20 30 2c 20 30 2c 20 26 75 2e 62 62 2e 72 65 , 0, 0, &u.bb.re
1e099 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 s);. sqlite3V
1e09a 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 dbeDeleteUnpacke
1e09b 64 52 65 63 6f 72 64 28 75 2e 62 62 2e 70 49 64 dRecord(u.bb.pId
1e09c 78 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20 72 xKey);. if( r
1e09d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1e09e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1e09f 20 7d 0a 20 20 20 20 75 2e 62 62 2e 61 6c 72 65 }. u.bb.alre
1e0a0 61 64 79 45 78 69 73 74 73 20 3d 20 28 75 2e 62 adyExists = (u.b
1e0a1 62 2e 72 65 73 3d 3d 30 29 3b 0a 20 20 20 20 75 b.res==0);. u
1e0a2 2e 62 62 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 .bb.pC->deferred
1e0a3 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 Moveto = 0;.
1e0a4 75 2e 62 62 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.bb.pC->cacheSt
1e0a5 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 atus = CACHE_STA
1e0a6 4c 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f LE;. }. if( pO
1e0a7 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6f p->opcode==OP_Fo
1e0a8 75 6e 64 20 29 7b 0a 20 20 20 20 69 66 28 20 75 und ){. if( u
1e0a9 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69 73 74 .bb.alreadyExist
1e0aa 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 s ) pc = pOp->p2
1e0ab 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 - 1;. }else{.
1e0ac 20 20 20 69 66 28 20 21 75 2e 62 62 2e 61 6c 72 if( !u.bb.alr
1e0ad 65 61 64 79 45 78 69 73 74 73 20 29 20 70 63 20 eadyExists ) pc
1e0ae 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1e0af 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1e0b0 2a 20 4f 70 63 6f 64 65 3a 20 49 73 55 6e 69 71 * Opcode: IsUniq
1e0b1 75 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a ue P1 P2 P3 P4 *
1e0b2 0a 2a 2a 0a 2a 2a 20 43 75 72 73 6f 72 20 50 31 .**.** Cursor P1
1e0b3 20 69 73 20 6f 70 65 6e 20 6f 6e 20 61 6e 20 69 is open on an i
1e0b4 6e 64 65 78 2e 20 20 53 6f 20 69 74 20 68 61 73 ndex. So it has
1e0b5 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 69 74 73 no data and its
1e0b6 20 6b 65 79 20 63 6f 6e 73 69 73 74 73 20 0a 2a key consists .*
1e0b7 2a 20 6f 66 20 61 20 72 65 63 6f 72 64 20 67 65 * of a record ge
1e0b8 6e 65 72 61 74 65 64 20 62 79 20 4f 50 5f 4d 61 nerated by OP_Ma
1e0b9 6b 65 52 65 63 6f 72 64 20 77 68 65 72 65 20 74 keRecord where t
1e0ba 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 69 73 he last field is
1e0bb 20 74 68 65 20 0a 2a 2a 20 72 6f 77 69 64 20 6f the .** rowid o
1e0bc 66 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 f the entry that
1e0bd 20 74 68 65 20 69 6e 64 65 78 20 72 65 66 65 72 the index refer
1e0be 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 s to..**.** The
1e0bf 50 33 20 72 65 67 69 73 74 65 72 20 63 6f 6e 74 P3 register cont
1e0c0 61 69 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 20 ains an integer
1e0c1 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 43 record number. C
1e0c2 61 6c 6c 20 74 68 69 73 20 72 65 63 6f 72 64 20 all this record
1e0c3 0a 2a 2a 20 6e 75 6d 62 65 72 20 52 2e 20 52 65 .** number R. Re
1e0c4 67 69 73 74 65 72 20 50 34 20 69 73 20 74 68 65 gister P4 is the
1e0c5 20 66 69 72 73 74 20 69 6e 20 61 20 73 65 74 20 first in a set
1e0c6 6f 66 20 4e 20 63 6f 6e 74 69 67 75 6f 75 73 20 of N contiguous
1e0c7 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 74 68 61 registers.** tha
1e0c8 74 20 6d 61 6b 65 20 75 70 20 61 6e 20 75 6e 70 t make up an unp
1e0c9 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20 acked index key
1e0ca 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 that can be used
1e0cb 20 77 69 74 68 20 63 75 72 73 6f 72 20 50 31 2e with cursor P1.
1e0cc 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 .** The value of
1e0cd 20 4e 20 63 61 6e 20 62 65 20 69 6e 66 65 72 72 N can be inferr
1e0ce 65 64 20 66 72 6f 6d 20 74 68 65 20 63 75 72 73 ed from the curs
1e0cf 6f 72 2e 20 4e 20 69 6e 63 6c 75 64 65 73 20 74 or. N includes t
1e0d0 68 65 20 72 6f 77 69 64 0a 2a 2a 20 76 61 6c 75 he rowid.** valu
1e0d1 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 e appended to th
1e0d2 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 e end of the ind
1e0d3 65 78 20 72 65 63 6f 72 64 2e 20 54 68 69 73 20 ex record. This
1e0d4 72 6f 77 69 64 20 76 61 6c 75 65 20 6d 61 79 0a rowid value may.
1e0d5 2a 2a 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 ** or may not be
1e0d6 20 74 68 65 20 73 61 6d 65 20 61 73 20 52 2e 0a the same as R..
1e0d7 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 **.** If any of
1e0d8 74 68 65 20 4e 20 72 65 67 69 73 74 65 72 73 20 the N registers
1e0d9 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 72 beginning with r
1e0da 65 67 69 73 74 65 72 20 50 34 20 63 6f 6e 74 61 egister P4 conta
1e0db 69 6e 73 20 61 20 4e 55 4c 4c 0a 2a 2a 20 76 61 ins a NULL.** va
1e0dc 6c 75 65 2c 20 6a 75 6d 70 20 69 6d 6d 65 64 69 lue, jump immedi
1e0dd 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a ately to P2..**.
1e0de 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 ** Otherwise, th
1e0df 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 is instruction c
1e0e0 68 65 63 6b 73 20 69 66 20 63 75 72 73 6f 72 20 hecks if cursor
1e0e1 50 31 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 P1 contains an e
1e0e2 6e 74 72 79 0a 2a 2a 20 77 68 65 72 65 20 74 68 ntry.** where th
1e0e3 65 20 66 69 72 73 74 20 28 4e 2d 31 29 20 66 69 e first (N-1) fi
1e0e4 65 6c 64 73 20 6d 61 74 63 68 20 62 75 74 20 74 elds match but t
1e0e5 68 65 20 72 6f 77 69 64 20 76 61 6c 75 65 20 61 he rowid value a
1e0e6 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 t the end.** of
1e0e7 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 the index entry
1e0e8 69 73 20 6e 6f 74 20 52 2e 20 49 66 20 74 68 65 is not R. If the
1e0e9 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20 65 6e re is no such en
1e0ea 74 72 79 2c 20 63 6f 6e 74 72 6f 6c 20 6a 75 6d try, control jum
1e0eb 70 73 0a 2a 2a 20 74 6f 20 69 6e 73 74 72 75 63 ps.** to instruc
1e0ec 74 69 6f 6e 20 50 32 2e 20 4f 74 68 65 72 77 69 tion P2. Otherwi
1e0ed 73 65 2c 20 74 68 65 20 72 6f 77 69 64 20 6f 66 se, the rowid of
1e0ee 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 69 6e 67 the conflicting
1e0ef 20 69 6e 64 65 78 0a 2a 2a 20 65 6e 74 72 79 20 index.** entry
1e0f0 69 73 20 63 6f 70 69 65 64 20 74 6f 20 72 65 67 is copied to reg
1e0f1 69 73 74 65 72 20 50 33 20 61 6e 64 20 63 6f 6e ister P3 and con
1e0f2 74 72 6f 6c 20 66 61 6c 6c 73 20 74 68 72 6f 75 trol falls throu
1e0f3 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 0a 2a gh to the next.*
1e0f4 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a * instruction..*
1e0f5 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 4e *.** See also: N
1e0f6 6f 74 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 73 otFound, NotExis
1e0f7 74 73 2c 20 46 6f 75 6e 64 0a 2a 2f 0a 63 61 73 ts, Found.*/.cas
1e0f8 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 3a 20 7b e OP_IsUnique: {
1e0f9 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c /* jump,
1e0fa 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f in3 */.#if 0 /
1e0fb 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1e0fc 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1e0fd 63 20 2a 2f 0a 20 20 75 31 36 20 69 69 3b 0a 20 c */. u16 ii;.
1e0fe 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 VdbeCursor *pCx
1e0ff 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 ;. BtCursor *pC
1e100 72 73 72 3b 0a 20 20 75 31 36 20 6e 46 69 65 6c rsr;. u16 nFiel
1e101 64 3b 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 0a d;. Mem *aMem;.
1e102 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 UnpackedRecord
1e103 20 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
1e104 20 20 20 20 20 2f 2a 20 42 2d 54 72 65 65 20 69 /* B-Tree i
1e105 6e 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 20 ndex search key
1e106 2a 2f 0a 20 20 69 36 34 20 52 3b 20 20 20 20 20 */. i64 R;
1e107 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e108 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 /* Rowid
1e109 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
1e10a 74 65 72 20 50 33 20 2a 2f 0a 23 65 6e 64 69 66 ter P3 */.#endif
1e10b 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1e10c 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1e10d 2e 62 63 20 2a 2f 0a 0a 20 20 75 2e 62 63 2e 61 .bc */.. u.bc.a
1e10e 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Mem = &p->aMem[p
1e10f 4f 70 2d 3e 70 34 2e 69 5d 3b 0a 20 20 2f 2a 20 Op->p4.i];. /*
1e110 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 Assert that the
1e111 76 61 6c 75 65 73 20 6f 66 20 70 61 72 61 6d 65 values of parame
1e112 74 65 72 73 20 50 31 20 61 6e 64 20 50 34 20 61 ters P1 and P4 a
1e113 72 65 20 69 6e 20 72 61 6e 67 65 2e 20 2a 2f 0a re in range. */.
1e114 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1e115 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 4type==P4_INT32
1e116 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1e117 2d 3e 70 34 2e 69 3e 30 20 26 26 20 70 4f 70 2d ->p4.i>0 && pOp-
1e118 3e 70 34 2e 69 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 >p4.i<=p->nMem )
1e119 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1e11a 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 >p1>=0 && pOp->p
1e11b 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 1<p->nCursor );.
1e11c 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 69 . /* Find the i
1e11d 6e 64 65 78 20 63 75 72 73 6f 72 2e 20 2a 2f 0a ndex cursor. */.
1e11e 20 20 75 2e 62 63 2e 70 43 78 20 3d 20 70 2d 3e u.bc.pCx = p->
1e11f 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a apCsr[pOp->p1];.
1e120 20 20 61 73 73 65 72 74 28 20 75 2e 62 63 2e 70 assert( u.bc.p
1e121 43 78 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 Cx->deferredMove
1e122 74 6f 3d 3d 30 20 29 3b 0a 20 20 75 2e 62 63 2e to==0 );. u.bc.
1e123 70 43 78 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 pCx->seekResult
1e124 3d 20 30 3b 0a 20 20 75 2e 62 63 2e 70 43 78 2d = 0;. u.bc.pCx-
1e125 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 >cacheStatus = C
1e126 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 75 2e ACHE_STALE;. u.
1e127 62 63 2e 70 43 72 73 72 20 3d 20 75 2e 62 63 2e bc.pCrsr = u.bc.
1e128 70 43 78 2d 3e 70 43 75 72 73 6f 72 3b 0a 0a 20 pCx->pCursor;..
1e129 20 2f 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 /* If any of th
1e12a 65 20 76 61 6c 75 65 73 20 61 72 65 20 4e 55 4c e values are NUL
1e12b 4c 2c 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 L, take the jump
1e12c 2e 20 2a 2f 0a 20 20 75 2e 62 63 2e 6e 46 69 65 . */. u.bc.nFie
1e12d 6c 64 20 3d 20 75 2e 62 63 2e 70 43 78 2d 3e 70 ld = u.bc.pCx->p
1e12e 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b KeyInfo->nField;
1e12f 0a 20 20 66 6f 72 28 75 2e 62 63 2e 69 69 3d 30 . for(u.bc.ii=0
1e130 3b 20 75 2e 62 63 2e 69 69 3c 75 2e 62 63 2e 6e ; u.bc.ii<u.bc.n
1e131 46 69 65 6c 64 3b 20 75 2e 62 63 2e 69 69 2b 2b Field; u.bc.ii++
1e132 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 62 63 2e ){. if( u.bc.
1e133 61 4d 65 6d 5b 75 2e 62 63 2e 69 69 5d 2e 66 6c aMem[u.bc.ii].fl
1e134 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 ags & MEM_Null )
1e135 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 {. pc = pOp
1e136 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 ->p2 - 1;.
1e137 75 2e 62 63 2e 70 43 72 73 72 20 3d 20 30 3b 0a u.bc.pCrsr = 0;.
1e138 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1e139 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 }. }. assert(
1e13a 20 28 75 2e 62 63 2e 61 4d 65 6d 5b 75 2e 62 63 (u.bc.aMem[u.bc
1e13b 2e 6e 46 69 65 6c 64 5d 2e 66 6c 61 67 73 20 26 .nField].flags &
1e13c 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 3b MEM_Null)==0 );
1e13d 0a 0a 20 20 69 66 28 20 75 2e 62 63 2e 70 43 72 .. if( u.bc.pCr
1e13e 73 72 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 sr!=0 ){. /*
1e13f 50 6f 70 75 6c 61 74 65 20 74 68 65 20 69 6e 64 Populate the ind
1e140 65 78 20 73 65 61 72 63 68 20 6b 65 79 2e 20 2a ex search key. *
1e141 2f 0a 20 20 20 20 75 2e 62 63 2e 72 2e 70 4b 65 /. u.bc.r.pKe
1e142 79 49 6e 66 6f 20 3d 20 75 2e 62 63 2e 70 43 78 yInfo = u.bc.pCx
1e143 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 ->pKeyInfo;.
1e144 75 2e 62 63 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 u.bc.r.nField =
1e145 75 2e 62 63 2e 6e 46 69 65 6c 64 20 2b 20 31 3b u.bc.nField + 1;
1e146 0a 20 20 20 20 75 2e 62 63 2e 72 2e 66 6c 61 67 . u.bc.r.flag
1e147 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 s = UNPACKED_PRE
1e148 46 49 58 5f 53 45 41 52 43 48 3b 0a 20 20 20 20 FIX_SEARCH;.
1e149 75 2e 62 63 2e 72 2e 61 4d 65 6d 20 3d 20 75 2e u.bc.r.aMem = u.
1e14a 62 63 2e 61 4d 65 6d 3b 0a 0a 20 20 20 20 2f 2a bc.aMem;.. /*
1e14b 20 45 78 74 72 61 63 74 20 74 68 65 20 76 61 6c Extract the val
1e14c 75 65 20 6f 66 20 75 2e 62 63 2e 52 20 66 72 6f ue of u.bc.R fro
1e14d 6d 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 2a m register P3. *
1e14e 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 /. sqlite3Vdb
1e14f 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 eMemIntegerify(p
1e150 49 6e 33 29 3b 0a 20 20 20 20 75 2e 62 63 2e 52 In3);. u.bc.R
1e151 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 0a 20 = pIn3->u.i;..
1e152 20 20 20 2f 2a 20 53 65 61 72 63 68 20 74 68 65 /* Search the
1e153 20 42 2d 54 72 65 65 20 69 6e 64 65 78 2e 20 49 B-Tree index. I
1e154 66 20 6e 6f 20 63 6f 6e 66 6c 69 63 74 69 6e 67 f no conflicting
1e155 20 72 65 63 6f 72 64 20 69 73 20 66 6f 75 6e 64 record is found
1e156 2c 20 6a 75 6d 70 0a 20 20 20 20 2a 2a 20 74 6f , jump. ** to
1e157 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 P2. Otherwise,
1e158 63 6f 70 79 20 74 68 65 20 72 6f 77 69 64 20 6f copy the rowid o
1e159 66 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 69 6e f the conflictin
1e15a 67 20 72 65 63 6f 72 64 20 74 6f 0a 20 20 20 20 g record to.
1e15b 2a 2a 20 72 65 67 69 73 74 65 72 20 50 33 20 61 ** register P3 a
1e15c 6e 64 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 nd fall through
1e15d 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 to the next inst
1e15e 72 75 63 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 ruction. */.
1e15f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1e160 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 eeMovetoUnpacked
1e161 28 75 2e 62 63 2e 70 43 72 73 72 2c 20 26 75 2e (u.bc.pCrsr, &u.
1e162 62 63 2e 72 2c 20 30 2c 20 30 2c 20 26 75 2e 62 bc.r, 0, 0, &u.b
1e163 63 2e 70 43 78 2d 3e 73 65 65 6b 52 65 73 75 6c c.pCx->seekResul
1e164 74 29 3b 0a 20 20 20 20 69 66 28 20 28 75 2e 62 t);. if( (u.b
1e165 63 2e 72 2e 66 6c 61 67 73 20 26 20 55 4e 50 41 c.r.flags & UNPA
1e166 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52 CKED_PREFIX_SEAR
1e167 43 48 29 20 7c 7c 20 75 2e 62 63 2e 72 2e 72 6f CH) || u.bc.r.ro
1e168 77 69 64 3d 3d 75 2e 62 63 2e 52 20 29 7b 0a 20 wid==u.bc.R ){.
1e169 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 pc = pOp->p
1e16a 32 20 2d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 2 - 1;. }else
1e16b 7b 0a 20 20 20 20 20 20 70 49 6e 33 2d 3e 75 2e {. pIn3->u.
1e16c 69 20 3d 20 75 2e 62 63 2e 72 2e 72 6f 77 69 64 i = u.bc.r.rowid
1e16d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 ;. }. }. br
1e16e 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1e16f 65 3a 20 4e 6f 74 45 78 69 73 74 73 20 50 31 20 e: NotExists P1
1e170 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 P3 * *.**.**
1e171 55 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 Use the content
1e172 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 61 of register P3 a
1e173 73 20 61 20 69 6e 74 65 67 65 72 20 6b 65 79 2e s a integer key.
1e174 20 20 49 66 20 61 20 72 65 63 6f 72 64 20 0a 2a If a record .*
1e175 2a 20 77 69 74 68 20 74 68 61 74 20 6b 65 79 20 * with that key
1e176 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 does not exist i
1e177 6e 20 74 61 62 6c 65 20 6f 66 20 50 31 2c 20 74 n table of P1, t
1e178 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 hen jump to P2.
1e179 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 .** If the recor
1e17a 64 20 64 6f 65 73 20 65 78 69 73 74 2c 20 74 68 d does exist, th
1e17b 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 54 en fall thru. T
1e17c 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 he cursor is lef
1e17d 74 20 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 t .** pointing t
1e17e 6f 20 74 68 65 20 72 65 63 6f 72 64 20 69 66 20 o the record if
1e17f 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a it exists..**.**
1e180 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 The difference
1e181 62 65 74 77 65 65 6e 20 74 68 69 73 20 6f 70 65 between this ope
1e182 72 61 74 69 6f 6e 20 61 6e 64 20 4e 6f 74 46 6f ration and NotFo
1e183 75 6e 64 20 69 73 20 74 68 61 74 20 74 68 69 73 und is that this
1e184 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 61 73 .** operation as
1e185 73 75 6d 65 73 20 74 68 65 20 6b 65 79 20 69 73 sumes the key is
1e186 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 20 an integer and
1e187 74 68 61 74 20 50 31 20 69 73 20 61 20 74 61 62 that P1 is a tab
1e188 6c 65 20 77 68 65 72 65 61 73 0a 2a 2a 20 4e 6f le whereas.** No
1e189 74 46 6f 75 6e 64 20 61 73 73 75 6d 65 73 20 6b tFound assumes k
1e18a 65 79 20 69 73 20 61 20 62 6c 6f 62 20 63 6f 6e ey is a blob con
1e18b 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20 4d 61 structed from Ma
1e18c 6b 65 52 65 63 6f 72 64 20 61 6e 64 0a 2a 2a 20 keRecord and.**
1e18d 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78 2e 0a P1 is an index..
1e18e 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
1e18f 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c Found, NotFound,
1e190 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 IsUnique.*/.cas
1e191 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 3a 20 e OP_NotExists:
1e192 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 { /* jump
1e193 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 , in3 */.#if 0
1e194 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1e195 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1e196 62 64 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 bd */. VdbeCurs
1e197 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 or *pC;. BtCurs
1e198 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 or *pCrsr;. int
1e199 20 72 65 73 3b 0a 20 20 75 36 34 20 69 4b 65 79 res;. u64 iKey
1e19a 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1e19b 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e19c 64 20 69 6e 74 6f 20 75 2e 62 64 20 2a 2f 0a 0a d into u.bd */..
1e19d 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e assert( pIn3->
1e19e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 flags & MEM_Int
1e19f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1e1a0 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e ->p1>=0 && pOp->
1e1a1 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b p1<p->nCursor );
1e1a2 0a 20 20 75 2e 62 64 2e 70 43 20 3d 20 70 2d 3e . u.bd.pC = p->
1e1a3 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a apCsr[pOp->p1];.
1e1a4 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e 70 assert( u.bd.p
1e1a5 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 C!=0 );. assert
1e1a6 28 20 75 2e 62 64 2e 70 43 2d 3e 69 73 54 61 62 ( u.bd.pC->isTab
1e1a7 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 le );. assert(
1e1a8 75 2e 62 64 2e 70 43 2d 3e 70 73 65 75 64 6f 54 u.bd.pC->pseudoT
1e1a9 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20 ableReg==0 );.
1e1aa 75 2e 62 64 2e 70 43 72 73 72 20 3d 20 75 2e 62 u.bd.pCrsr = u.b
1e1ab 64 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 d.pC->pCursor;.
1e1ac 20 69 66 28 20 75 2e 62 64 2e 70 43 72 73 72 21 if( u.bd.pCrsr!
1e1ad 3d 30 20 29 7b 0a 20 20 20 20 75 2e 62 64 2e 72 =0 ){. u.bd.r
1e1ae 65 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 64 es = 0;. u.bd
1e1af 2e 69 4b 65 79 20 3d 20 70 49 6e 33 2d 3e 75 2e .iKey = pIn3->u.
1e1b0 69 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 i;. rc = sqli
1e1b1 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e te3BtreeMovetoUn
1e1b2 70 61 63 6b 65 64 28 75 2e 62 64 2e 70 43 72 73 packed(u.bd.pCrs
1e1b3 72 2c 20 30 2c 20 75 2e 62 64 2e 69 4b 65 79 2c r, 0, u.bd.iKey,
1e1b4 20 30 2c 20 26 75 2e 62 64 2e 72 65 73 29 3b 0a 0, &u.bd.res);.
1e1b5 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 6c 61 73 u.bd.pC->las
1e1b6 74 52 6f 77 69 64 20 3d 20 70 49 6e 33 2d 3e 75 tRowid = pIn3->u
1e1b7 2e 69 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d .i;. u.bd.pC-
1e1b8 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 >rowidIsValid =
1e1b9 75 2e 62 64 2e 72 65 73 3d 3d 30 20 3f 31 3a 30 u.bd.res==0 ?1:0
1e1ba 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 6e ;. u.bd.pC->n
1e1bb 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 ullRow = 0;.
1e1bc 75 2e 62 64 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.bd.pC->cacheSt
1e1bd 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 atus = CACHE_STA
1e1be 4c 45 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d LE;. u.bd.pC-
1e1bf 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 >deferredMoveto
1e1c0 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 75 2e 62 = 0;. if( u.b
1e1c1 64 2e 72 65 73 21 3d 30 20 29 7b 0a 20 20 20 20 d.res!=0 ){.
1e1c2 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1e1c3 20 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 1;. assert
1e1c4 28 20 75 2e 62 64 2e 70 43 2d 3e 72 6f 77 69 64 ( u.bd.pC->rowid
1e1c5 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 IsValid==0 );.
1e1c6 20 20 7d 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d }. u.bd.pC-
1e1c7 3e 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 75 2e >seekResult = u.
1e1c8 62 64 2e 72 65 73 3b 0a 20 20 7d 65 6c 73 65 7b bd.res;. }else{
1e1c9 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 . /* This hap
1e1ca 70 65 6e 73 20 77 68 65 6e 20 61 6e 20 61 74 74 pens when an att
1e1cb 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20 61 20 72 empt to open a r
1e1cc 65 61 64 20 63 75 72 73 6f 72 20 6f 6e 20 74 68 ead cursor on th
1e1cd 65 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 5f e. ** sqlite_
1e1ce 6d 61 73 74 65 72 20 74 61 62 6c 65 20 72 65 74 master table ret
1e1cf 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 4d 50 54 urns SQLITE_EMPT
1e1d0 59 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 63 Y.. */. pc
1e1d1 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1e1d2 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 assert( u.bd
1e1d3 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 .pC->rowidIsVali
1e1d4 64 3d 3d 30 20 29 3b 0a 20 20 20 20 75 2e 62 64 d==0 );. u.bd
1e1d5 2e 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 .pC->seekResult
1e1d6 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b = 0;. }. break
1e1d7 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1e1d8 53 65 71 75 65 6e 63 65 20 50 31 20 50 32 20 2a Sequence P1 P2 *
1e1d9 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 20 * *.**.** Find
1e1da 74 68 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62 the next availab
1e1db 6c 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 le sequence numb
1e1dc 65 72 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 er for cursor P1
1e1dd 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73 ..** Write the s
1e1de 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 69 equence number i
1e1df 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e nto register P2.
1e1e0 0a 2a 2a 20 54 68 65 20 73 65 71 75 65 6e 63 65 .** The sequence
1e1e1 20 6e 75 6d 62 65 72 20 6f 6e 20 74 68 65 20 63 number on the c
1e1e2 75 72 73 6f 72 20 69 73 20 69 6e 63 72 65 6d 65 ursor is increme
1e1e3 6e 74 65 64 20 61 66 74 65 72 20 74 68 69 73 0a nted after this.
1e1e4 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 ** instruction.
1e1e5 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 71 .*/.case OP_Seq
1e1e6 75 65 6e 63 65 3a 20 7b 20 20 20 20 20 20 20 20 uence: {
1e1e7 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 /* out2-prere
1e1e8 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 lease */. asser
1e1e9 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 t( pOp->p1>=0 &&
1e1ea 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 pOp->p1<p->nCur
1e1eb 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 sor );. assert(
1e1ec 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 p->apCsr[pOp->p
1e1ed 31 5d 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d 1]!=0 );. pOut-
1e1ee 3e 75 2e 69 20 3d 20 70 2d 3e 61 70 43 73 72 5b >u.i = p->apCsr[
1e1ef 70 4f 70 2d 3e 70 31 5d 2d 3e 73 65 71 43 6f 75 pOp->p1]->seqCou
1e1f0 6e 74 2b 2b 3b 0a 20 20 4d 65 6d 53 65 74 54 79 nt++;. MemSetTy
1e1f1 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d peFlag(pOut, MEM
1e1f2 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a _Int);. break;.
1e1f3 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e }.../* Opcode: N
1e1f4 65 77 52 6f 77 69 64 20 50 31 20 50 32 20 50 33 ewRowid P1 P2 P3
1e1f5 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 47 65 74 20 61 * *.**.** Get a
1e1f6 20 6e 65 77 20 69 6e 74 65 67 65 72 20 72 65 63 new integer rec
1e1f7 6f 72 64 20 6e 75 6d 62 65 72 20 28 61 2e 6b 2e ord number (a.k.
1e1f8 61 20 22 72 6f 77 69 64 22 29 20 75 73 65 64 20 a "rowid") used
1e1f9 61 73 20 74 68 65 20 6b 65 79 20 74 6f 20 61 20 as the key to a
1e1fa 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 72 65 table..** The re
1e1fb 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 6e cord number is n
1e1fc 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 75 73 ot previously us
1e1fd 65 64 20 61 73 20 61 20 6b 65 79 20 69 6e 20 74 ed as a key in t
1e1fe 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 he database.** t
1e1ff 61 62 6c 65 20 74 68 61 74 20 63 75 72 73 6f 72 able that cursor
1e200 20 50 31 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 P1 points to.
1e201 54 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 6e The new record n
1e202 75 6d 62 65 72 20 69 73 20 77 72 69 74 74 65 6e umber is written
1e203 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 72 .** written to r
1e204 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a egister P2..**.*
1e205 2a 20 49 66 20 50 33 3e 30 20 74 68 65 6e 20 50 * If P3>0 then P
1e206 33 20 69 73 20 61 20 72 65 67 69 73 74 65 72 20 3 is a register
1e207 69 6e 20 74 68 65 20 72 6f 6f 74 20 66 72 61 6d in the root fram
1e208 65 20 6f 66 20 74 68 69 73 20 56 44 42 45 20 74 e of this VDBE t
1e209 68 61 74 20 68 6f 6c 64 73 20 0a 2a 2a 20 74 68 hat holds .** th
1e20a 65 20 6c 61 72 67 65 73 74 20 70 72 65 76 69 6f e largest previo
1e20b 75 73 6c 79 20 67 65 6e 65 72 61 74 65 64 20 72 usly generated r
1e20c 65 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 4e 6f ecord number. No
1e20d 20 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 new record numb
1e20e 65 72 73 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 77 ers are.** allow
1e20f 65 64 20 74 6f 20 62 65 20 6c 65 73 73 20 74 68 ed to be less th
1e210 61 6e 20 74 68 69 73 20 76 61 6c 75 65 2e 20 57 an this value. W
1e211 68 65 6e 20 74 68 69 73 20 76 61 6c 75 65 20 72 hen this value r
1e212 65 61 63 68 65 73 20 69 74 73 20 6d 61 78 69 6d eaches its maxim
1e213 75 6d 2c 20 0a 2a 2a 20 61 20 53 51 4c 49 54 45 um, .** a SQLITE
1e214 5f 46 55 4c 4c 20 65 72 72 6f 72 20 69 73 20 67 _FULL error is g
1e215 65 6e 65 72 61 74 65 64 2e 20 54 68 65 20 50 33 enerated. The P3
1e216 20 72 65 67 69 73 74 65 72 20 69 73 20 75 70 64 register is upd
1e217 61 74 65 64 20 77 69 74 68 20 74 68 65 20 27 0a ated with the '.
1e218 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 72 65 63 ** generated rec
1e219 6f 72 64 20 6e 75 6d 62 65 72 2e 20 54 68 69 73 ord number. This
1e21a 20 50 33 20 6d 65 63 68 61 6e 69 73 6d 20 69 73 P3 mechanism is
1e21b 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 69 6d used to help im
1e21c 70 6c 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 41 plement the.** A
1e21d 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 66 65 61 UTOINCREMENT fea
1e21e 74 75 72 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 ture..*/.case OP
1e21f 5f 4e 65 77 52 6f 77 69 64 3a 20 7b 20 20 20 20 _NewRowid: {
1e220 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 /* out2-p
1e221 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 rerelease */.#if
1e222 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1e223 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e224 6f 20 75 2e 62 65 20 2a 2f 0a 20 20 69 36 34 20 o u.be */. i64
1e225 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 v;
1e226 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f /* The new ro
1e227 77 69 64 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 wid */. VdbeCur
1e228 73 6f 72 20 2a 70 43 3b 20 20 20 20 20 20 20 20 sor *pC;
1e229 2f 2a 20 43 75 72 73 6f 72 20 6f 66 20 74 61 62 /* Cursor of tab
1e22a 6c 65 20 74 6f 20 67 65 74 20 74 68 65 20 6e 65 le to get the ne
1e22b 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 w rowid */. int
1e22c 20 72 65 73 3b 20 20 20 20 20 20 20 20 20 20 20 res;
1e22d 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 /* Result of
1e22e 20 61 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65 an sqlite3Btree
1e22f 4c 61 73 74 28 29 20 2a 2f 0a 20 20 69 6e 74 20 Last() */. int
1e230 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 cnt;
1e231 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 74 6f /* Counter to
1e232 20 6c 69 6d 69 74 20 74 68 65 20 6e 75 6d 62 65 limit the numbe
1e233 72 20 6f 66 20 73 65 61 72 63 68 65 73 20 2a 2f r of searches */
1e234 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 . Mem *pMem;
1e235 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 /* Reg
1e236 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6c 61 ister holding la
1e237 72 67 65 73 74 20 72 6f 77 69 64 20 66 6f 72 20 rgest rowid for
1e238 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f AUTOINCREMENT */
1e239 0a 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 . VdbeFrame *pF
1e23a 72 61 6d 65 3b 20 20 20 20 20 2f 2a 20 52 6f 6f rame; /* Roo
1e23b 74 20 66 72 61 6d 65 20 6f 66 20 56 44 42 45 20 t frame of VDBE
1e23c 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 */.#endif /* loc
1e23d 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e23e 65 64 20 69 6e 74 6f 20 75 2e 62 65 20 2a 2f 0a ed into u.be */.
1e23f 0a 20 20 75 2e 62 65 2e 76 20 3d 20 30 3b 0a 20 . u.be.v = 0;.
1e240 20 75 2e 62 65 2e 72 65 73 20 3d 20 30 3b 0a 20 u.be.res = 0;.
1e241 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 assert( pOp->p1
1e242 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 >=0 && pOp->p1<p
1e243 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 ->nCursor );. u
1e244 2e 62 65 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 .be.pC = p->apCs
1e245 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 r[pOp->p1];. as
1e246 73 65 72 74 28 20 75 2e 62 65 2e 70 43 21 3d 30 sert( u.be.pC!=0
1e247 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 );. if( NEVER(
1e248 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 u.be.pC->pCursor
1e249 3d 3d 30 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 ==0) ){. /* T
1e24a 68 65 20 7a 65 72 6f 20 69 6e 69 74 69 61 6c 69 he zero initiali
1e24b 7a 61 74 69 6f 6e 20 61 62 6f 76 65 20 69 73 20 zation above is
1e24c 61 6c 6c 20 74 68 61 74 20 69 73 20 6e 65 65 64 all that is need
1e24d 65 64 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 ed */. }else{.
1e24e 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 72 /* The next r
1e24f 6f 77 69 64 20 6f 72 20 72 65 63 6f 72 64 20 6e owid or record n
1e250 75 6d 62 65 72 20 28 64 69 66 66 65 72 65 6e 74 umber (different
1e251 20 74 65 72 6d 73 20 66 6f 72 20 74 68 65 20 73 terms for the s
1e252 61 6d 65 0a 20 20 20 20 2a 2a 20 74 68 69 6e 67 ame. ** thing
1e253 29 20 69 73 20 6f 62 74 61 69 6e 65 64 20 69 6e ) is obtained in
1e254 20 61 20 74 77 6f 2d 73 74 65 70 20 61 6c 67 6f a two-step algo
1e255 72 69 74 68 6d 2e 0a 20 20 20 20 2a 2a 0a 20 20 rithm.. **.
1e256 20 20 2a 2a 20 46 69 72 73 74 20 77 65 20 61 74 ** First we at
1e257 74 65 6d 70 74 20 74 6f 20 66 69 6e 64 20 74 68 tempt to find th
1e258 65 20 6c 61 72 67 65 73 74 20 65 78 69 73 74 69 e largest existi
1e259 6e 67 20 72 6f 77 69 64 20 61 6e 64 20 61 64 64 ng rowid and add
1e25a 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 74 6f 20 74 one. ** to t
1e25b 68 61 74 2e 20 20 42 75 74 20 69 66 20 74 68 65 hat. But if the
1e25c 20 6c 61 72 67 65 73 74 20 65 78 69 73 74 69 6e largest existin
1e25d 67 20 72 6f 77 69 64 20 69 73 20 61 6c 72 65 61 g rowid is alrea
1e25e 64 79 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 20 dy the maximum.
1e25f 20 20 20 2a 2a 20 70 6f 73 69 74 69 76 65 20 69 ** positive i
1e260 6e 74 65 67 65 72 2c 20 77 65 20 68 61 76 65 20 nteger, we have
1e261 74 6f 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 to fall through
1e262 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 0a 20 20 to the second.
1e263 20 20 2a 2a 20 70 72 6f 62 61 62 69 6c 69 73 74 ** probabilist
1e264 69 63 20 61 6c 67 6f 72 69 74 68 6d 0a 20 20 20 ic algorithm.
1e265 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 **. ** The s
1e266 65 63 6f 6e 64 20 61 6c 67 6f 72 69 74 68 6d 20 econd algorithm
1e267 69 73 20 74 6f 20 73 65 6c 65 63 74 20 61 20 72 is to select a r
1e268 6f 77 69 64 20 61 74 20 72 61 6e 64 6f 6d 20 61 owid at random a
1e269 6e 64 20 73 65 65 20 69 66 0a 20 20 20 20 2a 2a nd see if. **
1e26a 20 69 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 it already exis
1e26b 74 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e ts in the table.
1e26c 20 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 If it does not
1e26d 20 65 78 69 73 74 2c 20 77 65 20 68 61 76 65 0a exist, we have.
1e26e 20 20 20 20 2a 2a 20 73 75 63 63 65 65 64 65 64 ** succeeded
1e26f 2e 20 20 49 66 20 74 68 65 20 72 61 6e 64 6f 6d . If the random
1e270 20 72 6f 77 69 64 20 64 6f 65 73 20 65 78 69 73 rowid does exis
1e271 74 2c 20 77 65 20 73 65 6c 65 63 74 20 61 20 6e t, we select a n
1e272 65 77 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 61 6e ew one. ** an
1e273 64 20 74 72 79 20 61 67 61 69 6e 2c 20 75 70 20 d try again, up
1e274 74 6f 20 31 30 30 20 74 69 6d 65 73 2e 0a 20 20 to 100 times..
1e275 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
1e276 20 75 2e 62 65 2e 70 43 2d 3e 69 73 54 61 62 6c u.be.pC->isTabl
1e277 65 20 29 3b 0a 20 20 20 20 75 2e 62 65 2e 63 6e e );. u.be.cn
1e278 74 20 3d 20 30 3b 0a 0a 23 69 66 64 65 66 20 53 t = 0;..#ifdef S
1e279 51 4c 49 54 45 5f 33 32 42 49 54 5f 52 4f 57 49 QLITE_32BIT_ROWI
1e27a 44 0a 23 20 20 20 64 65 66 69 6e 65 20 4d 41 58 D.# define MAX
1e27b 5f 52 4f 57 49 44 20 30 78 37 66 66 66 66 66 66 _ROWID 0x7ffffff
1e27c 66 0a 23 65 6c 73 65 0a 20 20 20 20 2f 2a 20 53 f.#else. /* S
1e27d 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f ome compilers co
1e27e 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 63 6f 6e mplain about con
1e27f 73 74 61 6e 74 73 20 6f 66 20 74 68 65 20 66 6f stants of the fo
1e280 72 6d 20 30 78 37 66 66 66 66 66 66 66 66 66 66 rm 0x7ffffffffff
1e281 66 66 66 66 66 2e 0a 20 20 20 20 2a 2a 20 4f 74 fffff.. ** Ot
1e282 68 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 61 62 hers complain ab
1e283 6f 75 74 20 30 78 37 66 66 66 66 66 66 66 66 66 out 0x7fffffffff
1e284 66 66 66 66 66 66 66 4c 4c 2e 20 20 54 68 65 20 fffffffLL. The
1e285 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 following macro
1e286 73 65 65 6d 73 0a 20 20 20 20 2a 2a 20 74 6f 20 seems. ** to
1e287 70 72 6f 76 69 64 65 20 74 68 65 20 63 6f 6e 73 provide the cons
1e288 74 61 6e 74 20 77 68 69 6c 65 20 6d 61 6b 69 6e tant while makin
1e289 67 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 72 73 20 g all compilers
1e28a 68 61 70 70 79 2e 0a 20 20 20 20 2a 2f 0a 23 20 happy.. */.#
1e28b 20 20 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 define MAX_ROW
1e28c 49 44 20 20 28 69 36 34 29 28 20 28 28 28 75 36 ID (i64)( (((u6
1e28d 34 29 30 78 37 66 66 66 66 66 66 66 29 3c 3c 33 4)0x7fffffff)<<3
1e28e 32 29 20 7c 20 28 75 36 34 29 30 78 66 66 66 66 2) | (u64)0xffff
1e28f 66 66 66 66 20 29 0a 23 65 6e 64 69 66 0a 0a 20 ffff ).#endif..
1e290 20 20 20 69 66 28 20 21 75 2e 62 65 2e 70 43 2d if( !u.be.pC-
1e291 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 20 >useRandomRowid
1e292 29 7b 0a 20 20 20 20 20 20 75 2e 62 65 2e 76 20 ){. u.be.v
1e293 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
1e294 74 43 61 63 68 65 64 52 6f 77 69 64 28 75 2e 62 tCachedRowid(u.b
1e295 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a e.pC->pCursor);.
1e296 20 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e 76 if( u.be.v
1e297 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ==0 ){. r
1e298 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1e299 4c 61 73 74 28 75 2e 62 65 2e 70 43 2d 3e 70 43 Last(u.be.pC->pC
1e29a 75 72 73 6f 72 2c 20 26 75 2e 62 65 2e 72 65 73 ursor, &u.be.res
1e29b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
1e29c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1e29d 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 goto a
1e29e 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1e29f 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 r;. }.
1e2a0 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e 72 65 if( u.be.re
1e2a1 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 s ){. u
1e2a2 2e 62 65 2e 76 20 3d 20 31 3b 0a 20 20 20 20 20 .be.v = 1;.
1e2a3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1e2a4 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
1e2a5 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 73 te3BtreeCursorIs
1e2a6 56 61 6c 69 64 28 75 2e 62 65 2e 70 43 2d 3e 70 Valid(u.be.pC->p
1e2a7 43 75 72 73 6f 72 29 20 29 3b 0a 20 20 20 20 20 Cursor) );.
1e2a8 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1e2a9 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 75 2e 3BtreeKeySize(u.
1e2aa 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 be.pC->pCursor,
1e2ab 26 75 2e 62 65 2e 76 29 3b 0a 20 20 20 20 20 20 &u.be.v);.
1e2ac 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
1e2ad 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f SQLITE_OK ); /
1e2ae 2a 20 43 61 6e 6e 6f 74 20 66 61 69 6c 20 66 6f * Cannot fail fo
1e2af 6c 6c 6f 77 69 6e 67 20 42 74 72 65 65 4c 61 73 llowing BtreeLas
1e2b0 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 t() */.
1e2b1 20 69 66 28 20 75 2e 62 65 2e 76 3d 3d 4d 41 58 if( u.be.v==MAX
1e2b2 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 20 20 _ROWID ){.
1e2b3 20 20 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e 75 u.be.pC->u
1e2b4 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 20 3d 20 seRandomRowid =
1e2b5 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 1;. }el
1e2b6 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
1e2b7 75 2e 62 65 2e 76 2b 2b 3b 0a 20 20 20 20 20 20 u.be.v++;.
1e2b8 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
1e2b9 20 20 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 }..#ifndef
1e2ba 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1e2bb 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 20 OINCREMENT.
1e2bc 20 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b 0a if( pOp->p3 ){.
1e2bd 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 65 72 /* Asser
1e2be 74 20 74 68 61 74 20 50 33 20 69 73 20 61 20 76 t that P3 is a v
1e2bf 61 6c 69 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c alid memory cell
1e2c0 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 . */. ass
1e2c1 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 ert( pOp->p3>0 )
1e2c2 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 2d ;. if( p-
1e2c3 3e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 20 >pFrame ){.
1e2c4 20 20 20 20 20 66 6f 72 28 75 2e 62 65 2e 70 46 for(u.be.pF
1e2c5 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b 20 rame=p->pFrame;
1e2c6 75 2e 62 65 2e 70 46 72 61 6d 65 2d 3e 70 50 61 u.be.pFrame->pPa
1e2c7 72 65 6e 74 3b 20 75 2e 62 65 2e 70 46 72 61 6d rent; u.be.pFram
1e2c8 65 3d 75 2e 62 65 2e 70 46 72 61 6d 65 2d 3e 70 e=u.be.pFrame->p
1e2c9 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 20 Parent);.
1e2ca 20 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 /* Assert tha
1e2cb 74 20 50 33 20 69 73 20 61 20 76 61 6c 69 64 20 t P3 is a valid
1e2cc 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a 2f 0a memory cell. */.
1e2cd 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
1e2ce 28 20 70 4f 70 2d 3e 70 33 3c 3d 75 2e 62 65 2e ( pOp->p3<=u.be.
1e2cf 70 46 72 61 6d 65 2d 3e 6e 4d 65 6d 20 29 3b 0a pFrame->nMem );.
1e2d0 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 70 u.be.p
1e2d1 4d 65 6d 20 3d 20 26 75 2e 62 65 2e 70 46 72 61 Mem = &u.be.pFra
1e2d2 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 me->aMem[pOp->p3
1e2d3 5d 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 ];. }else
1e2d4 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 {. /* A
1e2d5 73 73 65 72 74 20 74 68 61 74 20 50 33 20 69 73 ssert that P3 is
1e2d6 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 a valid memory
1e2d7 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 cell. */.
1e2d8 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1e2d9 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 p3<=p->nMem );.
1e2da 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 70 4d u.be.pM
1e2db 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f em = &p->aMem[pO
1e2dc 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 20 20 p->p3];.
1e2dd 7d 0a 0a 20 20 20 20 20 20 20 20 52 45 47 49 53 }.. REGIS
1e2de 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1e2df 33 2c 20 75 2e 62 65 2e 70 4d 65 6d 29 3b 0a 20 3, u.be.pMem);.
1e2e0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1e2e1 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 beMemIntegerify(
1e2e2 75 2e 62 65 2e 70 4d 65 6d 29 3b 0a 20 20 20 20 u.be.pMem);.
1e2e3 20 20 20 20 61 73 73 65 72 74 28 20 28 75 2e 62 assert( (u.b
1e2e4 65 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 e.pMem->flags &
1e2e5 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 20 20 MEM_Int)!=0 );
1e2e6 2f 2a 20 6d 65 6d 28 50 33 29 20 68 6f 6c 64 73 /* mem(P3) holds
1e2e7 20 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 an integer */.
1e2e8 20 20 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e if( u.be.
1e2e9 70 4d 65 6d 2d 3e 75 2e 69 3d 3d 4d 41 58 5f 52 pMem->u.i==MAX_R
1e2ea 4f 57 49 44 20 7c 7c 20 75 2e 62 65 2e 70 43 2d OWID || u.be.pC-
1e2eb 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 20 >useRandomRowid
1e2ec 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
1e2ed 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 = SQLITE_FULL;.
1e2ee 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 goto ab
1e2ef 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1e2f0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1e2f1 20 20 20 20 69 66 28 20 75 2e 62 65 2e 76 3c 75 if( u.be.v<u
1e2f2 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 2b 31 20 .be.pMem->u.i+1
1e2f3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 2e 62 ){. u.b
1e2f4 65 2e 76 20 3d 20 75 2e 62 65 2e 70 4d 65 6d 2d e.v = u.be.pMem-
1e2f5 3e 75 2e 69 20 2b 20 31 3b 0a 20 20 20 20 20 20 >u.i + 1;.
1e2f6 20 20 7d 0a 20 20 20 20 20 20 20 20 75 2e 62 65 }. u.be
1e2f7 2e 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 75 2e 62 .pMem->u.i = u.b
1e2f8 65 2e 76 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e e.v;. }.#en
1e2f9 64 69 66 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 dif.. sqlit
1e2fa 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 64 e3BtreeSetCached
1e2fb 52 6f 77 69 64 28 75 2e 62 65 2e 70 43 2d 3e 70 Rowid(u.be.pC->p
1e2fc 43 75 72 73 6f 72 2c 20 75 2e 62 65 2e 76 3c 4d Cursor, u.be.v<M
1e2fd 41 58 5f 52 4f 57 49 44 20 3f 20 75 2e 62 65 2e AX_ROWID ? u.be.
1e2fe 76 2b 31 20 3a 20 30 29 3b 0a 20 20 20 20 7d 0a v+1 : 0);. }.
1e2ff 20 20 20 20 69 66 28 20 75 2e 62 65 2e 70 43 2d if( u.be.pC-
1e300 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 20 >useRandomRowid
1e301 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
1e302 20 70 4f 70 2d 3e 70 33 3d 3d 30 20 29 3b 20 20 pOp->p3==0 );
1e303 2f 2a 20 57 65 20 63 61 6e 6e 6f 74 20 62 65 20 /* We cannot be
1e304 69 6e 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 in random rowid
1e305 6d 6f 64 65 20 69 66 20 74 68 69 73 20 69 73 0a mode if this is.
1e306 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e307 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 **
1e308 61 6e 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 an AUTOINCREMENT
1e309 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 table. */.
1e30a 20 75 2e 62 65 2e 76 20 3d 20 64 62 2d 3e 6c 61 u.be.v = db->la
1e30b 73 74 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 75 stRowid;. u
1e30c 2e 62 65 2e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 .be.cnt = 0;.
1e30d 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 20 69 do{. i
1e30e 66 28 20 75 2e 62 65 2e 63 6e 74 3d 3d 30 20 26 f( u.be.cnt==0 &
1e30f 26 20 28 75 2e 62 65 2e 76 26 30 78 66 66 66 66 & (u.be.v&0xffff
1e310 66 66 29 3d 3d 75 2e 62 65 2e 76 20 29 7b 0a 20 ff)==u.be.v ){.
1e311 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 2b u.be.v+
1e312 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 +;. }else
1e313 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 {. sqli
1e314 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 te3_randomness(s
1e315 69 7a 65 6f 66 28 75 2e 62 65 2e 76 29 2c 20 26 izeof(u.be.v), &
1e316 75 2e 62 65 2e 76 29 3b 0a 20 20 20 20 20 20 20 u.be.v);.
1e317 20 20 20 69 66 28 20 75 2e 62 65 2e 63 6e 74 3c if( u.be.cnt<
1e318 35 20 29 20 75 2e 62 65 2e 76 20 26 3d 20 30 78 5 ) u.be.v &= 0x
1e319 66 66 66 66 66 66 3b 0a 20 20 20 20 20 20 20 20 ffffff;.
1e31a 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 }. rc = s
1e31b 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 qlite3BtreeMovet
1e31c 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 65 2e 70 oUnpacked(u.be.p
1e31d 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 C->pCursor, 0, (
1e31e 75 36 34 29 75 2e 62 65 2e 76 2c 20 30 2c 20 26 u64)u.be.v, 0, &
1e31f 75 2e 62 65 2e 72 65 73 29 3b 0a 20 20 20 20 20 u.be.res);.
1e320 20 20 20 75 2e 62 65 2e 63 6e 74 2b 2b 3b 0a 20 u.be.cnt++;.
1e321 20 20 20 20 20 7d 77 68 69 6c 65 28 20 75 2e 62 }while( u.b
1e322 65 2e 63 6e 74 3c 31 30 30 20 26 26 20 72 63 3d e.cnt<100 && rc=
1e323 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75 2e =SQLITE_OK && u.
1e324 62 65 2e 72 65 73 3d 3d 30 20 29 3b 0a 20 20 20 be.res==0 );.
1e325 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
1e326 45 5f 4f 4b 20 26 26 20 75 2e 62 65 2e 72 65 73 E_OK && u.be.res
1e327 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ==0 ){. r
1e328 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b c = SQLITE_FULL;
1e329 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 . goto ab
1e32a 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1e32b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1e32c 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e 72 6f 77 u.be.pC->row
1e32d 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 idIsValid = 0;.
1e32e 20 20 20 75 2e 62 65 2e 70 43 2d 3e 64 65 66 65 u.be.pC->defe
1e32f 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a rredMoveto = 0;.
1e330 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e 63 61 63 u.be.pC->cac
1e331 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 heStatus = CACHE
1e332 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 4d 65 _STALE;. }. Me
1e333 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 mSetTypeFlag(pOu
1e334 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 70 t, MEM_Int);. p
1e335 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 65 2e Out->u.i = u.be.
1e336 76 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f v;. break;.}../
1e337 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 73 65 72 74 * Opcode: Insert
1e338 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a P1 P2 P3 P4 P5.
1e339 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 **.** Write an e
1e33a 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 74 61 ntry into the ta
1e33b 62 6c 65 20 6f 66 20 63 75 72 73 6f 72 20 50 31 ble of cursor P1
1e33c 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69 . A new entry i
1e33d 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 69 66 20 s.** created if
1e33e 69 74 20 64 6f 65 73 6e 27 74 20 61 6c 72 65 61 it doesn't alrea
1e33f 64 79 20 65 78 69 73 74 20 6f 72 20 74 68 65 20 dy exist or the
1e340 64 61 74 61 20 66 6f 72 20 61 6e 20 65 78 69 73 data for an exis
1e341 74 69 6e 67 0a 2a 2a 20 65 6e 74 72 79 20 69 73 ting.** entry is
1e342 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 54 overwritten. T
1e343 68 65 20 64 61 74 61 20 69 73 20 74 68 65 20 76 he data is the v
1e344 61 6c 75 65 20 4d 45 4d 5f 42 6c 6f 62 20 73 74 alue MEM_Blob st
1e345 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 ored in register
1e346 0a 2a 2a 20 6e 75 6d 62 65 72 20 50 32 2e 20 54 .** number P2. T
1e347 68 65 20 6b 65 79 20 69 73 20 73 74 6f 72 65 64 he key is stored
1e348 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e in register P3.
1e349 20 54 68 65 20 6b 65 79 20 6d 75 73 74 0a 2a 2a The key must.**
1e34a 20 62 65 20 61 20 4d 45 4d 5f 49 6e 74 2e 0a 2a be a MEM_Int..*
1e34b 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c *.** If the OPFL
1e34c 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67 20 AG_NCHANGE flag
1e34d 6f 66 20 50 35 20 69 73 20 73 65 74 2c 20 74 68 of P5 is set, th
1e34e 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 en the row chang
1e34f 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69 6e e count is.** in
1e350 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68 65 72 cremented (other
1e351 77 69 73 65 20 6e 6f 74 29 2e 20 20 49 66 20 74 wise not). If t
1e352 68 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f he OPFLAG_LASTRO
1e353 57 49 44 20 66 6c 61 67 20 6f 66 20 50 35 20 69 WID flag of P5 i
1e354 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 72 s set,.** then r
1e355 6f 77 69 64 20 69 73 20 73 74 6f 72 65 64 20 66 owid is stored f
1e356 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 72 65 or subsequent re
1e357 74 75 72 6e 20 62 79 20 74 68 65 0a 2a 2a 20 73 turn by the.** s
1e358 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 qlite3_last_inse
1e359 72 74 5f 72 6f 77 69 64 28 29 20 66 75 6e 63 74 rt_rowid() funct
1e35a 69 6f 6e 20 28 6f 74 68 65 72 77 69 73 65 20 69 ion (otherwise i
1e35b 74 20 69 73 20 75 6e 6d 6f 64 69 66 69 65 64 29 t is unmodified)
1e35c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f ..**.** If the O
1e35d 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 PFLAG_USESEEKRES
1e35e 55 4c 54 20 66 6c 61 67 20 6f 66 20 50 35 20 69 ULT flag of P5 i
1e35f 73 20 73 65 74 20 61 6e 64 20 69 66 20 74 68 65 s set and if the
1e360 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 result of.** th
1e361 65 20 6c 61 73 74 20 73 65 65 6b 20 6f 70 65 72 e last seek oper
1e362 61 74 69 6f 6e 20 28 4f 50 5f 4e 6f 74 45 78 69 ation (OP_NotExi
1e363 73 74 73 29 20 77 61 73 20 61 20 73 75 63 63 65 sts) was a succe
1e364 73 73 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a ss, then this.**
1e365 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20 operation will
1e366 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 66 not attempt to f
1e367 69 6e 64 20 74 68 65 20 61 70 70 72 6f 70 72 69 ind the appropri
1e368 61 74 65 20 72 6f 77 20 62 65 66 6f 72 65 20 64 ate row before d
1e369 6f 69 6e 67 0a 2a 2a 20 74 68 65 20 69 6e 73 65 oing.** the inse
1e36a 72 74 20 62 75 74 20 77 69 6c 6c 20 69 6e 73 74 rt but will inst
1e36b 65 61 64 20 6f 76 65 72 77 72 69 74 65 20 74 68 ead overwrite th
1e36c 65 20 72 6f 77 20 74 68 61 74 20 74 68 65 20 63 e row that the c
1e36d 75 72 73 6f 72 20 69 73 0a 2a 2a 20 63 75 72 72 ursor is.** curr
1e36e 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 ently pointing t
1e36f 6f 2e 20 20 50 72 65 73 75 6d 61 62 6c 79 2c 20 o. Presumably,
1e370 74 68 65 20 70 72 69 6f 72 20 4f 50 5f 4e 6f 74 the prior OP_Not
1e371 45 78 69 73 74 73 20 6f 70 63 6f 64 65 0a 2a 2a Exists opcode.**
1e372 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 6f 73 has already pos
1e373 69 74 69 6f 6e 65 64 20 74 68 65 20 63 75 72 73 itioned the curs
1e374 6f 72 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54 or correctly. T
1e375 68 69 73 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 his is an optimi
1e376 7a 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 62 zation.** that b
1e377 6f 6f 73 74 73 20 70 65 72 66 6f 72 6d 61 6e 63 oosts performanc
1e378 65 20 62 79 20 61 76 6f 69 64 69 6e 67 20 72 65 e by avoiding re
1e379 64 75 6e 64 61 6e 74 20 73 65 65 6b 73 2e 0a 2a dundant seeks..*
1e37a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c *.** If the OPFL
1e37b 41 47 5f 49 53 55 50 44 41 54 45 20 66 6c 61 67 AG_ISUPDATE flag
1e37c 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 is set, then th
1e37d 69 73 20 6f 70 63 6f 64 65 20 69 73 20 70 61 72 is opcode is par
1e37e 74 20 6f 66 20 61 6e 0a 2a 2a 20 55 50 44 41 54 t of an.** UPDAT
1e37f 45 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4f 74 E operation. Ot
1e380 68 65 72 77 69 73 65 20 28 69 66 20 74 68 65 20 herwise (if the
1e381 66 6c 61 67 20 69 73 20 63 6c 65 61 72 29 20 74 flag is clear) t
1e382 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 0a hen this opcode.
1e383 2a 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e ** is part of an
1e384 20 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69 6f INSERT operatio
1e385 6e 2e 20 20 54 68 65 20 64 69 66 66 65 72 65 6e n. The differen
1e386 63 65 20 69 73 20 6f 6e 6c 79 20 69 6d 70 6f 72 ce is only impor
1e387 74 61 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 75 tant to.** the u
1e388 70 64 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 0a 2a pdate hook..**.*
1e389 2a 20 50 61 72 61 6d 65 74 65 72 20 50 34 20 6d * Parameter P4 m
1e38a 61 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 ay point to a st
1e38b 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ring containing
1e38c 74 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65 2c 20 the table-name,
1e38d 6f 72 0a 2a 2a 20 6d 61 79 20 62 65 20 4e 55 4c or.** may be NUL
1e38e 4c 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 L. If it is not
1e38f 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 75 NULL, then the u
1e390 70 64 61 74 65 2d 68 6f 6f 6b 20 0a 2a 2a 20 28 pdate-hook .** (
1e391 73 71 6c 69 74 65 33 2e 78 55 70 64 61 74 65 43 sqlite3.xUpdateC
1e392 61 6c 6c 62 61 63 6b 29 20 69 73 20 69 6e 76 6f allback) is invo
1e393 6b 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 ked following a
1e394 73 75 63 63 65 73 73 66 75 6c 20 69 6e 73 65 72 successful inser
1e395 74 2e 0a 2a 2a 0a 2a 2a 20 28 57 41 52 4e 49 4e t..**.** (WARNIN
1e396 47 2f 54 4f 44 4f 3a 20 49 66 20 50 31 20 69 73 G/TODO: If P1 is
1e397 20 61 20 70 73 65 75 64 6f 2d 63 75 72 73 6f 72 a pseudo-cursor
1e398 20 61 6e 64 20 50 32 20 69 73 20 64 79 6e 61 6d and P2 is dynam
1e399 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 ically.** alloca
1e39a 74 65 64 2c 20 74 68 65 6e 20 6f 77 6e 65 72 73 ted, then owners
1e39b 68 69 70 20 6f 66 20 50 32 20 69 73 20 74 72 61 hip of P2 is tra
1e39c 6e 73 66 65 72 72 65 64 20 74 6f 20 74 68 65 20 nsferred to the
1e39d 70 73 65 75 64 6f 2d 63 75 72 73 6f 72 0a 2a 2a pseudo-cursor.**
1e39e 20 61 6e 64 20 72 65 67 69 73 74 65 72 20 50 32 and register P2
1e39f 20 62 65 63 6f 6d 65 73 20 65 70 68 65 6d 65 72 becomes ephemer
1e3a0 61 6c 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 al. If the curs
1e3a1 6f 72 20 69 73 20 63 68 61 6e 67 65 64 2c 20 74 or is changed, t
1e3a2 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 72 he.** value of r
1e3a3 65 67 69 73 74 65 72 20 50 32 20 77 69 6c 6c 20 egister P2 will
1e3a4 74 68 65 6e 20 63 68 61 6e 67 65 2e 20 20 4d 61 then change. Ma
1e3a5 6b 65 20 73 75 72 65 20 74 68 69 73 20 64 6f 65 ke sure this doe
1e3a6 73 20 6e 6f 74 0a 2a 2a 20 63 61 75 73 65 20 61 s not.** cause a
1e3a7 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 29 0a 2a 2a ny problems.).**
1e3a8 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 .** This instruc
1e3a9 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 tion only works
1e3aa 6f 6e 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20 on tables. The
1e3ab 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 73 74 72 equivalent instr
1e3ac 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 69 6e uction.** for in
1e3ad 64 69 63 65 73 20 69 73 20 4f 50 5f 49 64 78 49 dices is OP_IdxI
1e3ae 6e 73 65 72 74 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 nsert..*/./* Opc
1e3af 6f 64 65 3a 20 49 6e 73 65 72 74 49 6e 74 20 50 ode: InsertInt P
1e3b0 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 1 P2 P3 P4 P5.**
1e3b1 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 65 .** This works e
1e3b2 78 61 63 74 6c 79 20 6c 69 6b 65 20 4f 50 5f 49 xactly like OP_I
1e3b3 6e 73 65 72 74 20 65 78 63 65 70 74 20 74 68 61 nsert except tha
1e3b4 74 20 74 68 65 20 6b 65 79 20 69 73 20 74 68 65 t the key is the
1e3b5 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 .** integer valu
1e3b6 65 20 50 33 2c 20 6e 6f 74 20 74 68 65 20 76 61 e P3, not the va
1e3b7 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 lue of the integ
1e3b8 65 72 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 er stored in reg
1e3b9 69 73 74 65 72 20 50 33 2e 0a 2a 2f 0a 63 61 73 ister P3..*/.cas
1e3ba 65 20 4f 50 5f 49 6e 73 65 72 74 3a 20 0a 63 61 e OP_Insert: .ca
1e3bb 73 65 20 4f 50 5f 49 6e 73 65 72 74 49 6e 74 3a se OP_InsertInt:
1e3bc 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1e3bd 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e3be 65 64 20 69 6e 74 6f 20 75 2e 62 66 20 2a 2f 0a ed into u.bf */.
1e3bf 20 20 4d 65 6d 20 2a 70 44 61 74 61 3b 20 20 20 Mem *pData;
1e3c0 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20 /* MEM cell
1e3c1 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 66 6f 72 holding data for
1e3c2 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 62 the record to b
1e3c3 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 e inserted */.
1e3c4 4d 65 6d 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 Mem *pKey;
1e3c5 20 20 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20 68 6f /* MEM cell ho
1e3c6 6c 64 69 6e 67 20 6b 65 79 20 20 66 6f 72 20 74 lding key for t
1e3c7 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 he record */. i
1e3c8 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 20 20 64 iKey;
1e3c9 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20 /* The integer
1e3ca 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 66 6f 72 ROWID or key for
1e3cb 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 62 the record to b
1e3cc 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 e inserted */.
1e3cd 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 VdbeCursor *pC;
1e3ce 20 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f 20 74 /* Cursor to t
1e3cf 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 able into which
1e3d0 69 6e 73 65 72 74 20 69 73 20 77 72 69 74 74 65 insert is writte
1e3d1 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f n */. int nZero
1e3d2 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 ; /* Numb
1e3d3 65 72 20 6f 66 20 7a 65 72 6f 2d 62 79 74 65 73 er of zero-bytes
1e3d4 20 74 6f 20 61 70 70 65 6e 64 20 2a 2f 0a 20 20 to append */.
1e3d5 69 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 3b 20 int seekResult;
1e3d6 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 70 /* Result of p
1e3d7 72 69 6f 72 20 73 65 65 6b 20 6f 72 20 30 20 69 rior seek or 0 i
1e3d8 66 20 6e 6f 20 55 53 45 53 45 45 4b 52 45 53 55 f no USESEEKRESU
1e3d9 4c 54 20 66 6c 61 67 20 2a 2f 0a 20 20 63 6f 6e LT flag */. con
1e3da 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 2f st char *zDb; /
1e3db 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 * database name
1e3dc 2d 20 75 73 65 64 20 62 79 20 74 68 65 20 75 70 - used by the up
1e3dd 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20 63 date hook */. c
1e3de 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 3b onst char *zTbl;
1e3df 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2d /* Table name -
1e3e0 20 75 73 65 64 20 62 79 20 74 68 65 20 6f 70 64 used by the opd
1e3e1 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20 69 6e ate hook */. in
1e3e2 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 t op;
1e3e3 2f 2a 20 4f 70 63 6f 64 65 20 66 6f 72 20 75 70 /* Opcode for up
1e3e4 64 61 74 65 20 68 6f 6f 6b 3a 20 53 51 4c 49 54 date hook: SQLIT
1e3e5 45 5f 55 50 44 41 54 45 20 6f 72 20 53 51 4c 49 E_UPDATE or SQLI
1e3e6 54 45 5f 49 4e 53 45 52 54 20 2a 2f 0a 23 65 6e TE_INSERT */.#en
1e3e7 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1e3e8 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e3e9 6f 20 75 2e 62 66 20 2a 2f 0a 0a 20 20 75 2e 62 o u.bf */.. u.b
1e3ea 66 2e 70 44 61 74 61 20 3d 20 26 70 2d 3e 61 4d f.pData = &p->aM
1e3eb 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 em[pOp->p2];. a
1e3ec 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d ssert( pOp->p1>=
1e3ed 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 0 && pOp->p1<p->
1e3ee 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 nCursor );. u.b
1e3ef 66 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b f.pC = p->apCsr[
1e3f0 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 pOp->p1];. asse
1e3f1 72 74 28 20 75 2e 62 66 2e 70 43 21 3d 30 20 29 rt( u.bf.pC!=0 )
1e3f2 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 ;. assert( u.bf
1e3f3 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 .pC->pCursor!=0
1e3f4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 );. assert( u.b
1e3f5 66 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c f.pC->pseudoTabl
1e3f6 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20 61 73 73 eReg==0 );. ass
1e3f7 65 72 74 28 20 75 2e 62 66 2e 70 43 2d 3e 69 73 ert( u.bf.pC->is
1e3f8 54 61 62 6c 65 20 29 3b 0a 20 20 52 45 47 49 53 Table );. REGIS
1e3f9 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1e3fa 32 2c 20 75 2e 62 66 2e 70 44 61 74 61 29 3b 0a 2, u.bf.pData);.
1e3fb 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f . if( pOp->opco
1e3fc 64 65 3d 3d 4f 50 5f 49 6e 73 65 72 74 20 29 7b de==OP_Insert ){
1e3fd 0a 20 20 20 20 75 2e 62 66 2e 70 4b 65 79 20 3d . u.bf.pKey =
1e3fe 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1e3ff 33 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 3];. assert(
1e400 75 2e 62 66 2e 70 4b 65 79 2d 3e 66 6c 61 67 73 u.bf.pKey->flags
1e401 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 & MEM_Int );.
1e402 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1e403 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 62 66 2e 70 (pOp->p3, u.bf.p
1e404 4b 65 79 29 3b 0a 20 20 20 20 75 2e 62 66 2e 69 Key);. u.bf.i
1e405 4b 65 79 20 3d 20 75 2e 62 66 2e 70 4b 65 79 2d Key = u.bf.pKey-
1e406 3e 75 2e 69 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 >u.i;. }else{.
1e407 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1e408 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e 73 65 72 opcode==OP_Inser
1e409 74 49 6e 74 20 29 3b 0a 20 20 20 20 75 2e 62 66 tInt );. u.bf
1e40a 2e 69 4b 65 79 20 3d 20 70 4f 70 2d 3e 70 33 3b .iKey = pOp->p3;
1e40b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4f 70 2d . }.. if( pOp-
1e40c 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 4e 43 48 >p5 & OPFLAG_NCH
1e40d 41 4e 47 45 20 29 20 70 2d 3e 6e 43 68 61 6e 67 ANGE ) p->nChang
1e40e 65 2b 2b 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e e++;. if( pOp->
1e40f 70 35 20 26 20 4f 50 46 4c 41 47 5f 4c 41 53 54 p5 & OPFLAG_LAST
1e410 52 4f 57 49 44 20 29 20 64 62 2d 3e 6c 61 73 74 ROWID ) db->last
1e411 52 6f 77 69 64 20 3d 20 75 2e 62 66 2e 69 4b 65 Rowid = u.bf.iKe
1e412 79 3b 0a 20 20 69 66 28 20 75 2e 62 66 2e 70 44 y;. if( u.bf.pD
1e413 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d ata->flags & MEM
1e414 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 2e 62 _Null ){. u.b
1e415 66 2e 70 44 61 74 61 2d 3e 7a 20 3d 20 30 3b 0a f.pData->z = 0;.
1e416 20 20 20 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e u.bf.pData->
1e417 6e 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a n = 0;. }else{.
1e418 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 assert( u.bf
1e419 2e 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 .pData->flags &
1e41a 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 (MEM_Blob|MEM_St
1e41b 72 29 20 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 66 r) );. }. u.bf
1e41c 2e 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 28 28 .seekResult = ((
1e41d 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 pOp->p5 & OPFLAG
1e41e 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 20 _USESEEKRESULT)
1e41f 3f 20 75 2e 62 66 2e 70 43 2d 3e 73 65 65 6b 52 ? u.bf.pC->seekR
1e420 65 73 75 6c 74 20 3a 20 30 29 3b 0a 20 20 69 66 esult : 0);. if
1e421 28 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 66 6c ( u.bf.pData->fl
1e422 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 ags & MEM_Zero )
1e423 7b 0a 20 20 20 20 75 2e 62 66 2e 6e 5a 65 72 6f {. u.bf.nZero
1e424 20 3d 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 75 = u.bf.pData->u
1e425 2e 6e 5a 65 72 6f 3b 0a 20 20 7d 65 6c 73 65 7b .nZero;. }else{
1e426 0a 20 20 20 20 75 2e 62 66 2e 6e 5a 65 72 6f 20 . u.bf.nZero
1e427 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 = 0;. }. sqlit
1e428 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 64 e3BtreeSetCached
1e429 52 6f 77 69 64 28 75 2e 62 66 2e 70 43 2d 3e 70 Rowid(u.bf.pC->p
1e42a 43 75 72 73 6f 72 2c 20 30 29 3b 0a 20 20 72 63 Cursor, 0);. rc
1e42b 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 = sqlite3BtreeI
1e42c 6e 73 65 72 74 28 75 2e 62 66 2e 70 43 2d 3e 70 nsert(u.bf.pC->p
1e42d 43 75 72 73 6f 72 2c 20 30 2c 20 75 2e 62 66 2e Cursor, 0, u.bf.
1e42e 69 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 iKey,.
1e42f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e430 75 2e 62 66 2e 70 44 61 74 61 2d 3e 7a 2c 20 75 u.bf.pData->z, u
1e431 2e 62 66 2e 70 44 61 74 61 2d 3e 6e 2c 20 75 2e .bf.pData->n, u.
1e432 62 66 2e 6e 5a 65 72 6f 2c 0a 20 20 20 20 20 20 bf.nZero,.
1e433 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e434 20 20 20 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 pOp->p5 & OP
1e435 46 4c 41 47 5f 41 50 50 45 4e 44 2c 20 75 2e 62 FLAG_APPEND, u.b
1e436 66 2e 73 65 65 6b 52 65 73 75 6c 74 0a 20 20 29 f.seekResult. )
1e437 3b 0a 20 20 75 2e 62 66 2e 70 43 2d 3e 72 6f 77 ;. u.bf.pC->row
1e438 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 idIsValid = 0;.
1e439 20 75 2e 62 66 2e 70 43 2d 3e 64 65 66 65 72 72 u.bf.pC->deferr
1e43a 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 edMoveto = 0;.
1e43b 75 2e 62 66 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.bf.pC->cacheSt
1e43c 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 atus = CACHE_STA
1e43d 4c 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 LE;.. /* Invoke
1e43e 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b the update-hook
1e43f 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f if required. */
1e440 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1e441 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 E_OK && db->xUpd
1e442 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 ateCallback && p
1e443 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 Op->p4.z ){.
1e444 75 2e 62 66 2e 7a 44 62 20 3d 20 64 62 2d 3e 61 u.bf.zDb = db->a
1e445 44 62 5b 75 2e 62 66 2e 70 43 2d 3e 69 44 62 5d Db[u.bf.pC->iDb]
1e446 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 75 2e 62 66 .zName;. u.bf
1e447 2e 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e .zTbl = pOp->p4.
1e448 7a 3b 0a 20 20 20 20 75 2e 62 66 2e 6f 70 20 3d z;. u.bf.op =
1e449 20 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 ((pOp->p5 & OPF
1e44a 4c 41 47 5f 49 53 55 50 44 41 54 45 29 20 3f 20 LAG_ISUPDATE) ?
1e44b 53 51 4c 49 54 45 5f 55 50 44 41 54 45 20 3a 20 SQLITE_UPDATE :
1e44c 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 29 3b 0a SQLITE_INSERT);.
1e44d 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 assert( u.bf
1e44e 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a .pC->isTable );.
1e44f 20 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43 db->xUpdateC
1e450 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 64 allback(db->pUpd
1e451 61 74 65 41 72 67 2c 20 75 2e 62 66 2e 6f 70 2c ateArg, u.bf.op,
1e452 20 75 2e 62 66 2e 7a 44 62 2c 20 75 2e 62 66 2e u.bf.zDb, u.bf.
1e453 7a 54 62 6c 2c 20 75 2e 62 66 2e 69 4b 65 79 29 zTbl, u.bf.iKey)
1e454 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e ;. assert( u.
1e455 62 66 2e 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b bf.pC->iDb>=0 );
1e456 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1e457 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 6c 65 ./* Opcode: Dele
1e458 74 65 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a te P1 P2 * P4 *.
1e459 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 **.** Delete the
1e45a 20 72 65 63 6f 72 64 20 61 74 20 77 68 69 63 68 record at which
1e45b 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 69 the P1 cursor i
1e45c 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e s currently poin
1e45d 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ting..**.** The
1e45e 63 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 6c cursor will be l
1e45f 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 eft pointing at
1e460 65 69 74 68 65 72 20 74 68 65 20 6e 65 78 74 20 either the next
1e461 6f 72 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a or the previous.
1e462 2a 2a 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 ** record in the
1e463 20 74 61 62 6c 65 2e 20 49 66 20 69 74 20 69 73 table. If it is
1e464 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 left pointing a
1e465 74 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72 t the next recor
1e466 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e d, then.** the n
1e467 65 78 74 20 4e 65 78 74 20 69 6e 73 74 72 75 63 ext Next instruc
1e468 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 61 20 6e tion will be a n
1e469 6f 2d 6f 70 2e 20 20 48 65 6e 63 65 20 69 74 20 o-op. Hence it
1e46a 69 73 20 4f 4b 20 74 6f 20 64 65 6c 65 74 65 0a is OK to delete.
1e46b 2a 2a 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d ** a record from
1e46c 20 77 69 74 68 69 6e 20 61 6e 20 4e 65 78 74 20 within an Next
1e46d 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 loop..**.** If t
1e46e 68 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 he OPFLAG_NCHANG
1e46f 45 20 66 6c 61 67 20 6f 66 20 50 32 20 69 73 20 E flag of P2 is
1e470 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 6f set, then the ro
1e471 77 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 w change count i
1e472 73 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 s.** incremented
1e473 20 28 6f 74 68 65 72 77 69 73 65 20 6e 6f 74 29 (otherwise not)
1e474 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 6d 75 73 74 20 ..**.** P1 must
1e475 6e 6f 74 20 62 65 20 70 73 65 75 64 6f 2d 74 61 not be pseudo-ta
1e476 62 6c 65 2e 20 20 49 74 20 68 61 73 20 74 6f 20 ble. It has to
1e477 62 65 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 be a real table
1e478 77 69 74 68 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 with.** multiple
1e479 20 72 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 rows..**.** If
1e47a 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 P4 is not NULL,
1e47b 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 6e then it is the n
1e47c 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
1e47d 20 74 68 61 74 20 50 31 20 69 73 0a 2a 2a 20 70 that P1 is.** p
1e47e 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 ointing to. The
1e47f 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 77 69 6c update hook wil
1e480 6c 20 62 65 20 69 6e 76 6f 6b 65 64 2c 20 69 66 l be invoked, if
1e481 20 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 20 49 it exists..** I
1e482 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c f P4 is not NULL
1e483 20 74 68 65 6e 20 74 68 65 20 50 31 20 63 75 72 then the P1 cur
1e484 73 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62 65 sor must have be
1e485 65 6e 20 70 6f 73 69 74 69 6f 6e 65 64 0a 2a 2a en positioned.**
1e486 20 75 73 69 6e 67 20 4f 50 5f 4e 6f 74 46 6f 75 using OP_NotFou
1e487 6e 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f nd prior to invo
1e488 6b 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 king this opcode
1e489 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 65 6c ..*/.case OP_Del
1e48a 65 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a ete: {.#if 0 /*
1e48b 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e48c 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 67 moved into u.bg
1e48d 20 2a 2f 0a 20 20 69 36 34 20 69 4b 65 79 3b 0a */. i64 iKey;.
1e48e 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1e48f 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1e490 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e491 64 20 69 6e 74 6f 20 75 2e 62 67 20 2a 2f 0a 0a d into u.bg */..
1e492 20 20 75 2e 62 67 2e 69 4b 65 79 20 3d 20 30 3b u.bg.iKey = 0;
1e493 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1e494 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1e495 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1e496 20 75 2e 62 67 2e 70 43 20 3d 20 70 2d 3e 61 70 u.bg.pC = p->ap
1e497 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1e498 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 21 assert( u.bg.pC!
1e499 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1e49a 75 2e 62 67 2e 70 43 2d 3e 70 43 75 72 73 6f 72 u.bg.pC->pCursor
1e49b 21 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20 !=0 ); /* Only
1e49c 76 61 6c 69 64 20 66 6f 72 20 72 65 61 6c 20 74 valid for real t
1e49d 61 62 6c 65 73 2c 20 6e 6f 20 70 73 65 75 64 6f ables, no pseudo
1e49e 74 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 tables */.. /*
1e49f 49 66 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f If the update-ho
1e4a0 6f 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b ok will be invok
1e4a1 65 64 2c 20 73 65 74 20 75 2e 62 67 2e 69 4b 65 ed, set u.bg.iKe
1e4a2 79 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20 6f y to the rowid o
1e4a3 66 20 74 68 65 0a 20 20 2a 2a 20 72 6f 77 20 62 f the. ** row b
1e4a4 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 20 20 eing deleted..
1e4a5 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 78 55 70 */. if( db->xUp
1e4a6 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 dateCallback &&
1e4a7 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 pOp->p4.z ){.
1e4a8 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 assert( u.bg.pC
1e4a9 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 ->isTable );.
1e4aa 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 assert( u.bg.pC
1e4ab 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29 ->rowidIsValid )
1e4ac 3b 20 20 2f 2a 20 6c 61 73 74 52 6f 77 69 64 20 ; /* lastRowid
1e4ad 73 65 74 20 62 79 20 70 72 65 76 69 6f 75 73 20 set by previous
1e4ae 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a 2f 0a 20 OP_NotFound */.
1e4af 20 20 20 75 2e 62 67 2e 69 4b 65 79 20 3d 20 75 u.bg.iKey = u
1e4b0 2e 62 67 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69 .bg.pC->lastRowi
1e4b1 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 d;. }.. /* The
1e4b2 20 4f 50 5f 44 65 6c 65 74 65 20 6f 70 63 6f 64 OP_Delete opcod
1e4b3 65 20 61 6c 77 61 79 73 20 66 6f 6c 6c 6f 77 73 e always follows
1e4b4 20 61 6e 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 an OP_NotExists
1e4b5 20 6f 72 20 4f 50 5f 4c 61 73 74 20 6f 72 0a 20 or OP_Last or.
1e4b6 20 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 6e ** OP_Column on
1e4b7 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 20 the same table
1e4b8 77 69 74 68 6f 75 74 20 61 6e 79 20 69 6e 74 65 without any inte
1e4b9 72 76 65 6e 69 6e 67 20 6f 70 65 72 61 74 69 6f rvening operatio
1e4ba 6e 73 20 74 68 61 74 0a 20 20 2a 2a 20 6d 69 67 ns that. ** mig
1e4bb 68 74 20 6d 6f 76 65 20 6f 72 20 69 6e 76 61 6c ht move or inval
1e4bc 69 64 61 74 65 20 74 68 65 20 63 75 72 73 6f 72 idate the cursor
1e4bd 2e 20 20 48 65 6e 63 65 20 63 75 72 73 6f 72 20 . Hence cursor
1e4be 75 2e 62 67 2e 70 43 20 69 73 20 61 6c 77 61 79 u.bg.pC is alway
1e4bf 73 20 70 6f 69 6e 74 69 6e 67 0a 20 20 2a 2a 20 s pointing. **
1e4c0 74 6f 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65 to the row to be
1e4c1 20 64 65 6c 65 74 65 64 20 61 6e 64 20 74 68 65 deleted and the
1e4c2 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 sqlite3VdbeCurs
1e4c3 6f 72 4d 6f 76 65 74 6f 28 29 20 6f 70 65 72 61 orMoveto() opera
1e4c4 74 69 6f 6e 0a 20 20 2a 2a 20 62 65 6c 6f 77 20 tion. ** below
1e4c5 69 73 20 61 6c 77 61 79 73 20 61 20 6e 6f 2d 6f is always a no-o
1e4c6 70 20 61 6e 64 20 63 61 6e 6e 6f 74 20 66 61 69 p and cannot fai
1e4c7 6c 2e 20 20 57 65 20 77 69 6c 6c 20 72 75 6e 20 l. We will run
1e4c8 69 74 20 61 6e 79 68 6f 77 2c 20 74 68 6f 75 67 it anyhow, thoug
1e4c9 68 2c 0a 20 20 2a 2a 20 74 6f 20 67 75 61 72 64 h,. ** to guard
1e4ca 20 61 67 61 69 6e 73 74 20 66 75 74 75 72 65 20 against future
1e4cb 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 63 changes to the c
1e4cc 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a 20 ode generator..
1e4cd 20 2a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 75 **/. assert( u
1e4ce 2e 62 67 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 .bg.pC->deferred
1e4cf 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 Moveto==0 );. r
1e4d0 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 c = sqlite3VdbeC
1e4d1 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 67 ursorMoveto(u.bg
1e4d2 2e 70 43 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 .pC);. if( NEVE
1e4d3 52 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 R(rc!=SQLITE_OK)
1e4d4 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1e4d5 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 0a 20 20 73 e_to_error;.. s
1e4d6 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 qlite3BtreeSetCa
1e4d7 63 68 65 64 52 6f 77 69 64 28 75 2e 62 67 2e 70 chedRowid(u.bg.p
1e4d8 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 29 3b 0a C->pCursor, 0);.
1e4d9 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1e4da 72 65 65 44 65 6c 65 74 65 28 75 2e 62 67 2e 70 reeDelete(u.bg.p
1e4db 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 75 C->pCursor);. u
1e4dc 2e 62 67 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 .bg.pC->cacheSta
1e4dd 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c tus = CACHE_STAL
1e4de 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 E;.. /* Invoke
1e4df 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 the update-hook
1e4e0 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a if required. */.
1e4e1 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1e4e2 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61 _OK && db->xUpda
1e4e3 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f teCallback && pO
1e4e4 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 63 p->p4.z ){. c
1e4e5 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d onst char *zDb =
1e4e6 20 64 62 2d 3e 61 44 62 5b 75 2e 62 67 2e 70 43 db->aDb[u.bg.pC
1e4e7 2d 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 ->iDb].zName;.
1e4e8 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 const char *zT
1e4e9 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a bl = pOp->p4.z;.
1e4ea 20 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43 db->xUpdateC
1e4eb 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 64 allback(db->pUpd
1e4ec 61 74 65 41 72 67 2c 20 53 51 4c 49 54 45 5f 44 ateArg, SQLITE_D
1e4ed 45 4c 45 54 45 2c 20 7a 44 62 2c 20 7a 54 62 6c ELETE, zDb, zTbl
1e4ee 2c 20 75 2e 62 67 2e 69 4b 65 79 29 3b 0a 20 20 , u.bg.iKey);.
1e4ef 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 assert( u.bg.p
1e4f0 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d C->iDb>=0 );. }
1e4f1 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 26 . if( pOp->p2 &
1e4f2 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 OPFLAG_NCHANGE
1e4f3 29 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a ) p->nChange++;.
1e4f4 20 20 62 72 65 61 6b 3b 0a 7d 0a 2f 2a 20 4f 70 break;.}./* Op
1e4f5 63 6f 64 65 3a 20 52 65 73 65 74 43 6f 75 6e 74 code: ResetCount
1e4f6 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a * * * * *.**.**
1e4f7 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 The value of th
1e4f8 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 e change counter
1e4f9 20 69 73 20 63 6f 70 69 65 64 20 74 6f 20 74 68 is copied to th
1e4fa 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c e database handl
1e4fb 65 0a 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e e.** change coun
1e4fc 74 65 72 20 28 72 65 74 75 72 6e 65 64 20 62 79 ter (returned by
1e4fd 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c subsequent call
1e4fe 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 68 61 s to sqlite3_cha
1e4ff 6e 67 65 73 28 29 29 2e 0a 2a 2a 20 54 68 65 6e nges())..** Then
1e500 20 74 68 65 20 56 4d 73 20 69 6e 74 65 72 6e 61 the VMs interna
1e501 6c 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 l change counter
1e502 20 72 65 73 65 74 73 20 74 6f 20 30 2e 0a 2a 2a resets to 0..**
1e503 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 This is used by
1e504 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d trigger program
1e505 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 s..*/.case OP_Re
1e506 73 65 74 43 6f 75 6e 74 3a 20 7b 0a 20 20 73 71 setCount: {. sq
1e507 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e lite3VdbeSetChan
1e508 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e ges(db, p->nChan
1e509 67 65 29 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 ge);. p->nChang
1e50a 65 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a e = 0;. break;.
1e50b 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f }../* Opcode: Ro
1e50c 77 44 61 74 61 20 50 31 20 50 32 20 2a 20 2a 20 wData P1 P2 * *
1e50d 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e *.**.** Write in
1e50e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 74 to register P2 t
1e50f 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20 he complete row
1e510 64 61 74 61 20 66 6f 72 20 63 75 72 73 6f 72 20 data for cursor
1e511 50 31 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 P1..** There is
1e512 6e 6f 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f no interpretatio
1e513 6e 20 6f 66 20 74 68 65 20 64 61 74 61 2e 20 20 n of the data.
1e514 0a 2a 2a 20 49 74 20 69 73 20 6a 75 73 74 20 63 .** It is just c
1e515 6f 70 69 65 64 20 6f 6e 74 6f 20 74 68 65 20 50 opied onto the P
1e516 32 20 72 65 67 69 73 74 65 72 20 65 78 61 63 74 2 register exact
1e517 6c 79 20 61 73 20 0a 2a 2a 20 69 74 20 69 73 20 ly as .** it is
1e518 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74 found in the dat
1e519 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a abase file..**.*
1e51a 2a 20 49 66 20 74 68 65 20 50 31 20 63 75 72 73 * If the P1 curs
1e51b 6f 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 or must be point
1e51c 69 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20 72 ing to a valid r
1e51d 6f 77 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 ow (not a NULL r
1e51e 6f 77 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61 6c ow).** of a real
1e51f 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 table, not a ps
1e520 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 2f eudo-table..*/./
1e521 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 4b 65 79 * Opcode: RowKey
1e522 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1e523 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72 65 ** Write into re
1e524 67 69 73 74 65 72 20 50 32 20 74 68 65 20 63 6f gister P2 the co
1e525 6d 70 6c 65 74 65 20 72 6f 77 20 6b 65 79 20 66 mplete row key f
1e526 6f 72 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a or cursor P1..**
1e527 20 54 68 65 72 65 20 69 73 20 6e 6f 20 69 6e 74 There is no int
1e528 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 erpretation of t
1e529 68 65 20 64 61 74 61 2e 20 20 0a 2a 2a 20 54 68 he data. .** Th
1e52a 65 20 6b 65 79 20 69 73 20 63 6f 70 69 65 64 20 e key is copied
1e52b 6f 6e 74 6f 20 74 68 65 20 50 33 20 72 65 67 69 onto the P3 regi
1e52c 73 74 65 72 20 65 78 61 63 74 6c 79 20 61 73 20 ster exactly as
1e52d 0a 2a 2a 20 69 74 20 69 73 20 66 6f 75 6e 64 20 .** it is found
1e52e 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
1e52f 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 file..**.** If t
1e530 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 he P1 cursor mus
1e531 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f t be pointing to
1e532 20 61 20 76 61 6c 69 64 20 72 6f 77 20 28 6e 6f a valid row (no
1e533 74 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a 2a t a NULL row).**
1e534 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c 65 of a real table
1e535 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 , not a pseudo-t
1e536 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 able..*/.case OP
1e537 5f 52 6f 77 4b 65 79 3a 0a 63 61 73 65 20 4f 50 _RowKey:.case OP
1e538 5f 52 6f 77 44 61 74 61 3a 20 7b 0a 23 69 66 20 _RowData: {.#if
1e539 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1e53a 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e53b 20 75 2e 62 68 20 2a 2f 0a 20 20 56 64 62 65 43 u.bh */. VdbeC
1e53c 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 ursor *pC;. BtC
1e53d 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 ursor *pCrsr;.
1e53e 75 33 32 20 6e 3b 0a 20 20 69 36 34 20 6e 36 34 u32 n;. i64 n64
1e53f 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1e540 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e541 64 20 69 6e 74 6f 20 75 2e 62 68 20 2a 2f 0a 0a d into u.bh */..
1e542 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 pOut = &p->aMe
1e543 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 0a 20 20 2f m[pOp->p2];.. /
1e544 2a 20 4e 6f 74 65 20 74 68 61 74 20 52 6f 77 4b * Note that RowK
1e545 65 79 20 61 6e 64 20 52 6f 77 44 61 74 61 20 61 ey and RowData a
1e546 72 65 20 72 65 61 6c 6c 79 20 65 78 61 63 74 6c re really exactl
1e547 79 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 72 y the same instr
1e548 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 uction */. asse
1e549 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1e54a 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 & pOp->p1<p->nCu
1e54b 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 68 2e 70 rsor );. u.bh.p
1e54c 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 C = p->apCsr[pOp
1e54d 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1e54e 20 75 2e 62 68 2e 70 43 2d 3e 69 73 54 61 62 6c u.bh.pC->isTabl
1e54f 65 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 e || pOp->opcode
1e550 3d 3d 4f 50 5f 52 6f 77 4b 65 79 20 29 3b 0a 20 ==OP_RowKey );.
1e551 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43 assert( u.bh.pC
1e552 2d 3e 69 73 49 6e 64 65 78 20 7c 7c 20 70 4f 70 ->isIndex || pOp
1e553 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 ->opcode==OP_Row
1e554 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 Data );. assert
1e555 28 20 75 2e 62 68 2e 70 43 21 3d 30 20 29 3b 0a ( u.bh.pC!=0 );.
1e556 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 assert( u.bh.p
1e557 43 2d 3e 6e 75 6c 6c 52 6f 77 3d 3d 30 20 29 3b C->nullRow==0 );
1e558 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e . assert( u.bh.
1e559 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 pC->pseudoTableR
1e55a 65 67 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 eg==0 );. asser
1e55b 74 28 20 75 2e 62 68 2e 70 43 2d 3e 70 43 75 72 t( u.bh.pC->pCur
1e55c 73 6f 72 21 3d 30 20 29 3b 0a 20 20 75 2e 62 68 sor!=0 );. u.bh
1e55d 2e 70 43 72 73 72 20 3d 20 75 2e 62 68 2e 70 43 .pCrsr = u.bh.pC
1e55e 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 61 73 73 ->pCursor;. ass
1e55f 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1e560 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75 eCursorIsValid(u
1e561 2e 62 68 2e 70 43 72 73 72 29 20 29 3b 0a 0a 20 .bh.pCrsr) );..
1e562 20 2f 2a 20 54 68 65 20 4f 50 5f 52 6f 77 4b 65 /* The OP_RowKe
1e563 79 20 61 6e 64 20 4f 50 5f 52 6f 77 44 61 74 61 y and OP_RowData
1e564 20 6f 70 63 6f 64 65 73 20 61 6c 77 61 79 73 20 opcodes always
1e565 66 6f 6c 6c 6f 77 20 4f 50 5f 4e 6f 74 45 78 69 follow OP_NotExi
1e566 73 74 73 20 6f 72 0a 20 20 2a 2a 20 4f 50 5f 52 sts or. ** OP_R
1e567 65 77 69 6e 64 2f 4f 70 5f 4e 65 78 74 20 77 69 ewind/Op_Next wi
1e568 74 68 20 6e 6f 20 69 6e 74 65 72 76 65 6e 69 6e th no intervenin
1e569 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 g instructions t
1e56a 68 61 74 20 6d 69 67 68 74 20 69 6e 76 61 6c 69 hat might invali
1e56b 64 61 74 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 date. ** the cu
1e56c 72 73 6f 72 2e 20 20 48 65 6e 63 65 20 74 68 65 rsor. Hence the
1e56d 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 71 6c 69 74 following sqlit
1e56e 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 e3VdbeCursorMove
1e56f 74 6f 28 29 20 63 61 6c 6c 20 69 73 20 61 6c 77 to() call is alw
1e570 61 79 73 0a 20 20 2a 2a 20 61 20 6e 6f 2d 6f 70 ays. ** a no-op
1e571 20 61 6e 64 20 63 61 6e 20 6e 65 76 65 72 20 66 and can never f
1e572 61 69 6c 2e 20 20 42 75 74 20 77 65 20 6c 65 61 ail. But we lea
1e573 76 65 20 69 74 20 69 6e 20 70 6c 61 63 65 20 61 ve it in place a
1e574 73 20 61 20 73 61 66 65 74 79 2e 0a 20 20 2a 2f s a safety.. */
1e575 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e . assert( u.bh.
1e576 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 pC->deferredMove
1e577 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 to==0 );. rc =
1e578 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f sqlite3VdbeCurso
1e579 72 4d 6f 76 65 74 6f 28 75 2e 62 68 2e 70 43 29 rMoveto(u.bh.pC)
1e57a 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 ;. if( NEVER(rc
1e57b 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 20 67 !=SQLITE_OK) ) g
1e57c 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1e57d 5f 65 72 72 6f 72 3b 0a 0a 20 20 69 66 28 20 75 _error;.. if( u
1e57e 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20 .bh.pC->isIndex
1e57f 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 ){. assert( !
1e580 75 2e 62 68 2e 70 43 2d 3e 69 73 54 61 62 6c 65 u.bh.pC->isTable
1e581 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c );. rc = sql
1e582 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 ite3BtreeKeySize
1e583 28 75 2e 62 68 2e 70 43 72 73 72 2c 20 26 75 2e (u.bh.pCrsr, &u.
1e584 62 68 2e 6e 36 34 29 3b 0a 20 20 20 20 61 73 73 bh.n64);. ass
1e585 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
1e586 4f 4b 20 29 3b 20 20 20 20 2f 2a 20 54 72 75 65 OK ); /* True
1e587 20 62 65 63 61 75 73 65 20 6f 66 20 43 75 72 73 because of Curs
1e588 6f 72 4d 6f 76 65 74 6f 28 29 20 63 61 6c 6c 20 orMoveto() call
1e589 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 69 66 28 above */. if(
1e58a 20 75 2e 62 68 2e 6e 36 34 3e 64 62 2d 3e 61 4c u.bh.n64>db->aL
1e58b 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
1e58c 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 T_LENGTH] ){.
1e58d 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b goto too_big;
1e58e 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 68 2e . }. u.bh.
1e58f 6e 20 3d 20 28 75 33 32 29 75 2e 62 68 2e 6e 36 n = (u32)u.bh.n6
1e590 34 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4;. }else{.
1e591 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1e592 65 44 61 74 61 53 69 7a 65 28 75 2e 62 68 2e 70 eDataSize(u.bh.p
1e593 43 72 73 72 2c 20 26 75 2e 62 68 2e 6e 29 3b 0a Crsr, &u.bh.n);.
1e594 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
1e595 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20 SQLITE_OK );
1e596 2f 2a 20 44 61 74 61 53 69 7a 65 28 29 20 63 61 /* DataSize() ca
1e597 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 20 nnot fail */.
1e598 20 69 66 28 20 75 2e 62 68 2e 6e 3e 28 75 33 32 if( u.bh.n>(u32
1e599 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 )db->aLimit[SQLI
1e59a 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
1e59b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 ){. goto t
1e59c 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 oo_big;. }.
1e59d 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 }. if( sqlite3V
1e59e 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c dbeMemGrow(pOut,
1e59f 20 75 2e 62 68 2e 6e 2c 20 30 29 20 29 7b 0a 20 u.bh.n, 0) ){.
1e5a0 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a goto no_mem;.
1e5a1 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 }. pOut->n =
1e5a2 75 2e 62 68 2e 6e 3b 0a 20 20 4d 65 6d 53 65 74 u.bh.n;. MemSet
1e5a3 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d TypeFlag(pOut, M
1e5a4 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 69 66 28 20 EM_Blob);. if(
1e5a5 75 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65 78 u.bh.pC->isIndex
1e5a6 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
1e5a7 69 74 65 33 42 74 72 65 65 4b 65 79 28 75 2e 62 ite3BtreeKey(u.b
1e5a8 68 2e 70 43 72 73 72 2c 20 30 2c 20 75 2e 62 68 h.pCrsr, 0, u.bh
1e5a9 2e 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 .n, pOut->z);.
1e5aa 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
1e5ab 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 sqlite3BtreeData
1e5ac 28 75 2e 62 68 2e 70 43 72 73 72 2c 20 30 2c 20 (u.bh.pCrsr, 0,
1e5ad 75 2e 62 68 2e 6e 2c 20 70 4f 75 74 2d 3e 7a 29 u.bh.n, pOut->z)
1e5ae 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e ;. }. pOut->en
1e5af 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b c = SQLITE_UTF8;
1e5b0 20 20 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 /* In case the
1e5b1 20 62 6c 6f 62 20 69 73 20 65 76 65 72 20 63 61 blob is ever ca
1e5b2 73 74 20 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20 st to text */.
1e5b3 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 UPDATE_MAX_BLOBS
1e5b4 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 IZE(pOut);. bre
1e5b5 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1e5b6 3a 20 52 6f 77 69 64 20 50 31 20 50 32 20 2a 20 : Rowid P1 P2 *
1e5b7 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 * *.**.** Store
1e5b8 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 61 in register P2 a
1e5b9 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 n integer which
1e5ba 69 73 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68 is the key of th
1e5bb 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 74 68 e table entry th
1e5bc 61 74 0a 2a 2a 20 50 31 20 69 73 20 63 75 72 72 at.** P1 is curr
1e5bd 65 6e 74 6c 79 20 70 6f 69 6e 74 20 74 6f 2e 0a ently point to..
1e5be 2a 2a 0a 2a 2a 20 50 31 20 63 61 6e 20 62 65 20 **.** P1 can be
1e5bf 65 69 74 68 65 72 20 61 6e 20 6f 72 64 69 6e 61 either an ordina
1e5c0 72 79 20 74 61 62 6c 65 20 6f 72 20 61 20 76 69 ry table or a vi
1e5c1 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 rtual table. Th
1e5c2 65 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 62 ere used to.** b
1e5c3 65 20 61 20 73 65 70 61 72 61 74 65 20 4f 50 5f e a separate OP_
1e5c4 56 52 6f 77 69 64 20 6f 70 63 6f 64 65 20 66 6f VRowid opcode fo
1e5c5 72 20 75 73 65 20 77 69 74 68 20 76 69 72 74 75 r use with virtu
1e5c6 61 6c 20 74 61 62 6c 65 73 2c 20 62 75 74 20 74 al tables, but t
1e5c7 68 69 73 0a 2a 2a 20 6f 6e 65 20 6f 70 63 6f 64 his.** one opcod
1e5c8 65 20 6e 6f 77 20 77 6f 72 6b 73 20 66 6f 72 20 e now works for
1e5c9 62 6f 74 68 20 74 61 62 6c 65 20 74 79 70 65 73 both table types
1e5ca 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 ..*/.case OP_Row
1e5cb 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 id: {
1e5cc 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1e5cd 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 erelease */.#if
1e5ce 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1e5cf 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e5d0 20 75 2e 62 69 20 2a 2f 0a 20 20 56 64 62 65 43 u.bi */. VdbeC
1e5d1 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 36 34 ursor *pC;. i64
1e5d2 20 76 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 v;. sqlite3_vt
1e5d3 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f 6e ab *pVtab;. con
1e5d4 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c st sqlite3_modul
1e5d5 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 23 65 6e 64 e *pModule;.#end
1e5d6 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1e5d7 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e5d8 20 75 2e 62 69 20 2a 2f 0a 0a 20 20 61 73 73 65 u.bi */.. asse
1e5d9 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1e5da 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 & pOp->p1<p->nCu
1e5db 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 69 2e 70 rsor );. u.bi.p
1e5dc 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 C = p->apCsr[pOp
1e5dd 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1e5de 20 75 2e 62 69 2e 70 43 21 3d 30 20 29 3b 0a 20 u.bi.pC!=0 );.
1e5df 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70 43 assert( u.bi.pC
1e5e0 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 ->pseudoTableReg
1e5e1 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 ==0 );. if( u.b
1e5e2 69 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b i.pC->nullRow ){
1e5e3 0a 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 . /* Do nothi
1e5e4 6e 67 20 73 6f 20 74 68 61 74 20 72 65 67 5b 50 ng so that reg[P
1e5e5 32 5d 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 20 2] remains NULL
1e5e6 2a 2f 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 */. break;.
1e5e7 7d 65 6c 73 65 20 69 66 28 20 75 2e 62 69 2e 70 }else if( u.bi.p
1e5e8 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 C->deferredMovet
1e5e9 6f 20 29 7b 0a 20 20 20 20 75 2e 62 69 2e 76 20 o ){. u.bi.v
1e5ea 3d 20 75 2e 62 69 2e 70 43 2d 3e 6d 6f 76 65 74 = u.bi.pC->movet
1e5eb 6f 54 61 72 67 65 74 3b 0a 23 69 66 6e 64 65 66 oTarget;.#ifndef
1e5ec 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1e5ed 54 55 41 4c 54 41 42 4c 45 0a 20 20 7d 65 6c 73 TUALTABLE. }els
1e5ee 65 20 69 66 28 20 75 2e 62 69 2e 70 43 2d 3e 70 e if( u.bi.pC->p
1e5ef 56 74 61 62 43 75 72 73 6f 72 20 29 7b 0a 20 20 VtabCursor ){.
1e5f0 20 20 75 2e 62 69 2e 70 56 74 61 62 20 3d 20 75 u.bi.pVtab = u
1e5f1 2e 62 69 2e 70 43 2d 3e 70 56 74 61 62 43 75 72 .bi.pC->pVtabCur
1e5f2 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20 sor->pVtab;.
1e5f3 75 2e 62 69 2e 70 4d 6f 64 75 6c 65 20 3d 20 75 u.bi.pModule = u
1e5f4 2e 62 69 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 .bi.pVtab->pModu
1e5f5 6c 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 le;. assert(
1e5f6 75 2e 62 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 52 u.bi.pModule->xR
1e5f7 6f 77 69 64 20 29 3b 0a 20 20 20 20 69 66 28 20 owid );. if(
1e5f8 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 sqlite3SafetyOff
1e5f9 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 (db) ) goto abor
1e5fa 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b t_due_to_misuse;
1e5fb 0a 20 20 20 20 72 63 20 3d 20 75 2e 62 69 2e 70 . rc = u.bi.p
1e5fc 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 28 75 Module->xRowid(u
1e5fd 2e 62 69 2e 70 43 2d 3e 70 56 74 61 62 43 75 72 .bi.pC->pVtabCur
1e5fe 73 6f 72 2c 20 26 75 2e 62 69 2e 76 29 3b 0a 20 sor, &u.bi.v);.
1e5ff 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1e600 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 (db, p->zErrMsg)
1e601 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 ;. p->zErrMsg
1e602 20 3d 20 75 2e 62 69 2e 70 56 74 61 62 2d 3e 7a = u.bi.pVtab->z
1e603 45 72 72 4d 73 67 3b 0a 20 20 20 20 75 2e 62 69 ErrMsg;. u.bi
1e604 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 .pVtab->zErrMsg
1e605 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c = 0;. if( sql
1e606 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 ite3SafetyOn(db)
1e607 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1e608 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 23 65 6e e_to_misuse;.#en
1e609 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1e60a 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 IT_VIRTUALTABLE
1e60b 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 */. }else{.
1e60c 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70 43 2d assert( u.bi.pC-
1e60d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 >pCursor!=0 );.
1e60e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
1e60f 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 dbeCursorMoveto(
1e610 75 2e 62 69 2e 70 43 29 3b 0a 20 20 20 20 69 66 u.bi.pC);. if
1e611 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 ( rc ) goto abor
1e612 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1e613 20 20 20 20 69 66 28 20 75 2e 62 69 2e 70 43 2d if( u.bi.pC-
1e614 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29 7b >rowidIsValid ){
1e615 0a 20 20 20 20 20 20 75 2e 62 69 2e 76 20 3d 20 . u.bi.v =
1e616 75 2e 62 69 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 u.bi.pC->lastRow
1e617 69 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 id;. }else{.
1e618 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1e619 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 75 2e 3BtreeKeySize(u.
1e61a 62 69 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 bi.pC->pCursor,
1e61b 26 75 2e 62 69 2e 76 29 3b 0a 20 20 20 20 20 20 &u.bi.v);.
1e61c 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
1e61d 54 45 5f 4f 4b 20 29 3b 20 20 2f 2a 20 41 6c 77 TE_OK ); /* Alw
1e61e 61 79 73 20 73 6f 20 62 65 63 61 75 73 65 20 6f ays so because o
1e61f 66 20 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 f CursorMoveto()
1e620 20 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 7d 0a above */. }.
1e621 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 }. pOut->u.i
1e622 3d 20 75 2e 62 69 2e 76 3b 0a 20 20 4d 65 6d 53 = u.bi.v;. MemS
1e623 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c etTypeFlag(pOut,
1e624 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 MEM_Int);. bre
1e625 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1e626 3a 20 4e 75 6c 6c 52 6f 77 20 50 31 20 2a 20 2a : NullRow P1 * *
1e627 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 * *.**.** Move
1e628 74 68 65 20 63 75 72 73 6f 72 20 50 31 20 74 6f the cursor P1 to
1e629 20 61 20 6e 75 6c 6c 20 72 6f 77 2e 20 20 41 6e a null row. An
1e62a 79 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 65 72 y OP_Column oper
1e62b 61 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 6f ations.** that o
1e62c 63 63 75 72 20 77 68 69 6c 65 20 74 68 65 20 63 ccur while the c
1e62d 75 72 73 6f 72 20 69 73 20 6f 6e 20 74 68 65 20 ursor is on the
1e62e 6e 75 6c 6c 20 72 6f 77 20 77 69 6c 6c 20 61 6c null row will al
1e62f 77 61 79 73 0a 2a 2a 20 77 72 69 74 65 20 61 20 ways.** write a
1e630 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 NULL..*/.case OP
1e631 5f 4e 75 6c 6c 52 6f 77 3a 20 7b 0a 23 69 66 20 _NullRow: {.#if
1e632 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1e633 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e634 20 75 2e 62 6a 20 2a 2f 0a 20 20 56 64 62 65 43 u.bj */. VdbeC
1e635 75 72 73 6f 72 20 2a 70 43 3b 0a 23 65 6e 64 69 ursor *pC;.#endi
1e636 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1e637 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e638 75 2e 62 6a 20 2a 2f 0a 0a 20 20 61 73 73 65 72 u.bj */.. asser
1e639 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 t( pOp->p1>=0 &&
1e63a 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 pOp->p1<p->nCur
1e63b 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6a 2e 70 43 sor );. u.bj.pC
1e63c 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d = p->apCsr[pOp-
1e63d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 >p1];. assert(
1e63e 75 2e 62 6a 2e 70 43 21 3d 30 20 29 3b 0a 20 20 u.bj.pC!=0 );.
1e63f 75 2e 62 6a 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 u.bj.pC->nullRow
1e640 20 3d 20 31 3b 0a 20 20 75 2e 62 6a 2e 70 43 2d = 1;. u.bj.pC-
1e641 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 >rowidIsValid =
1e642 30 3b 0a 20 20 69 66 28 20 75 2e 62 6a 2e 70 43 0;. if( u.bj.pC
1e643 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 ->pCursor ){.
1e644 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 sqlite3BtreeCle
1e645 61 72 43 75 72 73 6f 72 28 75 2e 62 6a 2e 70 43 arCursor(u.bj.pC
1e646 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a ->pCursor);. }.
1e647 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1e648 70 63 6f 64 65 3a 20 4c 61 73 74 20 50 31 20 50 pcode: Last P1 P
1e649 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 2 * * *.**.** Th
1e64a 65 20 6e 65 78 74 20 75 73 65 20 6f 66 20 74 68 e next use of th
1e64b 65 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c 75 6d e Rowid or Colum
1e64c 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 72 75 n or Next instru
1e64d 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a 2a 2a ction for P1 .**
1e64e 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f 20 74 will refer to t
1e64f 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e he last entry in
1e650 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61 the database ta
1e651 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a ble or index..**
1e652 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 If the table or
1e653 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74 79 20 index is empty
1e654 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e 20 6a and P2>0, then j
1e655 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 ump immediately
1e656 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50 32 20 to P2..** If P2
1e657 69 73 20 30 20 6f 72 20 69 66 20 74 68 65 20 74 is 0 or if the t
1e658 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 able or index is
1e659 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61 6c 6c not empty, fall
1e65a 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 through.** to t
1e65b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 he following ins
1e65c 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 truction..*/.cas
1e65d 65 20 4f 50 5f 4c 61 73 74 3a 20 7b 20 20 20 20 e OP_Last: {
1e65e 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 /* jump */.#
1e65f 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1e660 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1e661 6e 74 6f 20 75 2e 62 6b 20 2a 2f 0a 20 20 56 64 nto u.bk */. Vd
1e662 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 beCursor *pC;.
1e663 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b BtCursor *pCrsr;
1e664 0a 20 20 69 6e 74 20 72 65 73 3b 0a 23 65 6e 64 . int res;.#end
1e665 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1e666 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e667 20 75 2e 62 6b 20 2a 2f 0a 0a 20 20 61 73 73 65 u.bk */.. asse
1e668 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1e669 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 & pOp->p1<p->nCu
1e66a 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6b 2e 70 rsor );. u.bk.p
1e66b 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 C = p->apCsr[pOp
1e66c 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1e66d 20 75 2e 62 6b 2e 70 43 21 3d 30 20 29 3b 0a 20 u.bk.pC!=0 );.
1e66e 20 75 2e 62 6b 2e 70 43 72 73 72 20 3d 20 75 2e u.bk.pCrsr = u.
1e66f 62 6b 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a bk.pC->pCursor;.
1e670 20 20 69 66 28 20 75 2e 62 6b 2e 70 43 72 73 72 if( u.bk.pCrsr
1e671 3d 3d 30 20 29 7b 0a 20 20 20 20 75 2e 62 6b 2e ==0 ){. u.bk.
1e672 72 65 73 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 res = 1;. }else
1e673 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1e674 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e 62 6b e3BtreeLast(u.bk
1e675 2e 70 43 72 73 72 2c 20 26 75 2e 62 6b 2e 72 65 .pCrsr, &u.bk.re
1e676 73 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 6b 2e 70 s);. }. u.bk.p
1e677 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 38 C->nullRow = (u8
1e678 29 75 2e 62 6b 2e 72 65 73 3b 0a 20 20 75 2e 62 )u.bk.res;. u.b
1e679 6b 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f k.pC->deferredMo
1e67a 76 65 74 6f 20 3d 20 30 3b 0a 20 20 75 2e 62 6b veto = 0;. u.bk
1e67b 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 .pC->rowidIsVali
1e67c 64 20 3d 20 30 3b 0a 20 20 75 2e 62 6b 2e 70 43 d = 0;. u.bk.pC
1e67d 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 ->cacheStatus =
1e67e 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 69 CACHE_STALE;. i
1e67f 66 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 f( pOp->p2>0 &&
1e680 75 2e 62 6b 2e 72 65 73 20 29 7b 0a 20 20 20 20 u.bk.res ){.
1e681 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1e682 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1e683 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 6f .../* Opcode: So
1e684 72 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a rt P1 P2 * * *.*
1e685 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 *.** This opcode
1e686 20 64 6f 65 73 20 65 78 61 63 74 6c 79 20 74 68 does exactly th
1e687 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 e same thing as
1e688 4f 50 5f 52 65 77 69 6e 64 20 65 78 63 65 70 74 OP_Rewind except
1e689 20 74 68 61 74 0a 2a 2a 20 69 74 20 69 6e 63 72 that.** it incr
1e68a 65 6d 65 6e 74 73 20 61 6e 20 75 6e 64 6f 63 75 ements an undocu
1e68b 6d 65 6e 74 65 64 20 67 6c 6f 62 61 6c 20 76 61 mented global va
1e68c 72 69 61 62 6c 65 20 75 73 65 64 20 66 6f 72 20 riable used for
1e68d 74 65 73 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 testing..**.** S
1e68e 6f 72 74 69 6e 67 20 69 73 20 61 63 63 6f 6d 70 orting is accomp
1e68f 6c 69 73 68 65 64 20 62 79 20 77 72 69 74 69 6e lished by writin
1e690 67 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 g records into a
1e691 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c 0a sorting index,.
1e692 2a 2a 20 74 68 65 6e 20 72 65 77 69 6e 64 69 6e ** then rewindin
1e693 67 20 74 68 61 74 20 69 6e 64 65 78 20 61 6e 64 g that index and
1e694 20 70 6c 61 79 69 6e 67 20 69 74 20 62 61 63 6b playing it back
1e695 20 66 72 6f 6d 20 62 65 67 69 6e 6e 69 6e 67 20 from beginning
1e696 74 6f 0a 2a 2a 20 65 6e 64 2e 20 20 57 65 20 75 to.** end. We u
1e697 73 65 20 74 68 65 20 4f 50 5f 53 6f 72 74 20 6f se the OP_Sort o
1e698 70 63 6f 64 65 20 69 6e 73 74 65 61 64 20 6f 66 pcode instead of
1e699 20 4f 50 5f 52 65 77 69 6e 64 20 74 6f 20 64 6f OP_Rewind to do
1e69a 20 74 68 65 0a 2a 2a 20 72 65 77 69 6e 64 69 6e the.** rewindin
1e69b 67 20 73 6f 20 74 68 61 74 20 74 68 65 20 67 6c g so that the gl
1e69c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 77 69 obal variable wi
1e69d 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e 74 65 ll be incremente
1e69e 64 20 61 6e 64 0a 2a 2a 20 72 65 67 72 65 73 73 d and.** regress
1e69f 69 6f 6e 20 74 65 73 74 73 20 63 61 6e 20 64 65 ion tests can de
1e6a0 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 termine whether
1e6a1 6f 72 20 6e 6f 74 20 74 68 65 20 6f 70 74 69 6d or not the optim
1e6a2 69 7a 65 72 20 69 73 0a 2a 2a 20 63 6f 72 72 65 izer is.** corre
1e6a3 63 74 6c 79 20 6f 70 74 69 6d 69 7a 69 6e 67 20 ctly optimizing
1e6a4 6f 75 74 20 73 6f 72 74 73 2e 0a 2a 2f 0a 63 61 out sorts..*/.ca
1e6a5 73 65 20 4f 50 5f 53 6f 72 74 3a 20 7b 20 20 20 se OP_Sort: {
1e6a6 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a /* jump */.
1e6a7 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
1e6a8 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 73 6f 72 ST. sqlite3_sor
1e6a9 74 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c t_count++;. sql
1e6aa 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e ite3_search_coun
1e6ab 74 2d 2d 3b 0a 23 65 6e 64 69 66 0a 20 20 70 2d t--;.#endif. p-
1e6ac 3e 61 43 6f 75 6e 74 65 72 5b 53 51 4c 49 54 45 >aCounter[SQLITE
1e6ad 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 _STMTSTATUS_SORT
1e6ae 2d 31 5d 2b 2b 3b 0a 20 20 2f 2a 20 46 61 6c 6c -1]++;. /* Fall
1e6af 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f 50 through into OP
1e6b0 5f 52 65 77 69 6e 64 20 2a 2f 0a 7d 0a 2f 2a 20 _Rewind */.}./*
1e6b1 4f 70 63 6f 64 65 3a 20 52 65 77 69 6e 64 20 50 Opcode: Rewind P
1e6b2 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1e6b3 20 54 68 65 20 6e 65 78 74 20 75 73 65 20 6f 66 The next use of
1e6b4 20 74 68 65 20 52 6f 77 69 64 20 6f 72 20 43 6f the Rowid or Co
1e6b5 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 lumn or Next ins
1e6b6 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 truction for P1
1e6b7 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 74 .** will refer t
1e6b8 6f 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 o the first entr
1e6b9 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 y in the databas
1e6ba 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 e table or index
1e6bb 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c ..** If the tabl
1e6bc 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20 65 6d e or index is em
1e6bd 70 74 79 20 61 6e 64 20 50 32 3e 30 2c 20 74 68 pty and P2>0, th
1e6be 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 en jump immediat
1e6bf 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 ely to P2..** If
1e6c0 20 50 32 20 69 73 20 30 20 6f 72 20 69 66 20 74 P2 is 0 or if t
1e6c1 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 he table or inde
1e6c2 78 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 x is not empty,
1e6c3 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 fall through.**
1e6c4 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 to the following
1e6c5 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f instruction..*/
1e6c6 0a 63 61 73 65 20 4f 50 5f 52 65 77 69 6e 64 3a .case OP_Rewind:
1e6c7 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1e6c8 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c p */.#if 0 /* l
1e6c9 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e6ca 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6c 20 2a oved into u.bl *
1e6cb 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
1e6cc 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a pC;. BtCursor *
1e6cd 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 pCrsr;. int res
1e6ce 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1e6cf 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e6d0 64 20 69 6e 74 6f 20 75 2e 62 6c 20 2a 2f 0a 0a d into u.bl */..
1e6d1 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1e6d2 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 1>=0 && pOp->p1<
1e6d3 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1e6d4 75 2e 62 6c 2e 70 43 20 3d 20 70 2d 3e 61 70 43 u.bl.pC = p->apC
1e6d5 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 sr[pOp->p1];. a
1e6d6 73 73 65 72 74 28 20 75 2e 62 6c 2e 70 43 21 3d ssert( u.bl.pC!=
1e6d7 30 20 29 3b 0a 20 20 69 66 28 20 28 75 2e 62 6c 0 );. if( (u.bl
1e6d8 2e 70 43 72 73 72 20 3d 20 75 2e 62 6c 2e 70 43 .pCrsr = u.bl.pC
1e6d9 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b ->pCursor)!=0 ){
1e6da 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1e6db 33 42 74 72 65 65 46 69 72 73 74 28 75 2e 62 6c 3BtreeFirst(u.bl
1e6dc 2e 70 43 72 73 72 2c 20 26 75 2e 62 6c 2e 72 65 .pCrsr, &u.bl.re
1e6dd 73 29 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 43 2d s);. u.bl.pC-
1e6de 3e 61 74 46 69 72 73 74 20 3d 20 75 2e 62 6c 2e >atFirst = u.bl.
1e6df 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 res==0 ?1:0;.
1e6e0 20 75 2e 62 6c 2e 70 43 2d 3e 64 65 66 65 72 72 u.bl.pC->deferr
1e6e1 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 edMoveto = 0;.
1e6e2 20 20 75 2e 62 6c 2e 70 43 2d 3e 63 61 63 68 65 u.bl.pC->cache
1e6e3 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 Status = CACHE_S
1e6e4 54 41 4c 45 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 TALE;. u.bl.p
1e6e5 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 C->rowidIsValid
1e6e6 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 0;. }else{.
1e6e7 20 20 75 2e 62 6c 2e 72 65 73 20 3d 20 31 3b 0a u.bl.res = 1;.
1e6e8 20 20 7d 0a 20 20 75 2e 62 6c 2e 70 43 2d 3e 6e }. u.bl.pC->n
1e6e9 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 75 2e 62 ullRow = (u8)u.b
1e6ea 6c 2e 72 65 73 3b 0a 20 20 61 73 73 65 72 74 28 l.res;. assert(
1e6eb 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f pOp->p2>0 && pO
1e6ec 70 2d 3e 70 32 3c 70 2d 3e 6e 4f 70 20 29 3b 0a p->p2<p->nOp );.
1e6ed 20 20 69 66 28 20 75 2e 62 6c 2e 72 65 73 20 29 if( u.bl.res )
1e6ee 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e {. pc = pOp->
1e6ef 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 p2 - 1;. }. br
1e6f0 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1e6f1 65 3a 20 4e 65 78 74 20 50 31 20 50 32 20 2a 20 e: Next P1 P2 *
1e6f2 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 * *.**.** Advanc
1e6f3 65 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 e cursor P1 so t
1e6f4 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f hat it points to
1e6f5 20 74 68 65 20 6e 65 78 74 20 6b 65 79 2f 64 61 the next key/da
1e6f6 74 61 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a ta pair in its.*
1e6f7 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 * table or index
1e6f8 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20 . If there are
1e6f9 6e 6f 20 6d 6f 72 65 20 6b 65 79 2f 76 61 6c 75 no more key/valu
1e6fa 65 20 70 61 69 72 73 20 74 68 65 6e 20 66 61 6c e pairs then fal
1e6fb 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 l through.** to
1e6fc 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e the following in
1e6fd 73 74 72 75 63 74 69 6f 6e 2e 20 20 42 75 74 20 struction. But
1e6fe 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 64 if the cursor ad
1e6ff 76 61 6e 63 65 20 77 61 73 20 73 75 63 63 65 73 vance was succes
1e700 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d sful,.** jump im
1e701 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e mediately to P2.
1e702 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 75 .**.** The P1 cu
1e703 72 73 6f 72 20 6d 75 73 74 20 62 65 20 66 6f 72 rsor must be for
1e704 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e a real table, n
1e705 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c ot a pseudo-tabl
1e706 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 e..**.** See als
1e707 6f 3a 20 50 72 65 76 0a 2a 2f 0a 2f 2a 20 4f 70 o: Prev.*/./* Op
1e708 63 6f 64 65 3a 20 50 72 65 76 20 50 31 20 50 32 code: Prev P1 P2
1e709 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 61 63 * * *.**.** Bac
1e70a 6b 20 75 70 20 63 75 72 73 6f 72 20 50 31 20 73 k up cursor P1 s
1e70b 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 o that it points
1e70c 20 74 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 to the previous
1e70d 20 6b 65 79 2f 64 61 74 61 20 70 61 69 72 20 69 key/data pair i
1e70e 6e 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f n its.** table o
1e70f 72 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65 r index. If the
1e710 72 65 20 69 73 20 6e 6f 20 70 72 65 76 69 6f 75 re is no previou
1e711 73 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 72 s key/value pair
1e712 73 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f s then fall thro
1e713 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f ugh.** to the fo
1e714 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 llowing instruct
1e715 69 6f 6e 2e 20 20 42 75 74 20 69 66 20 74 68 65 ion. But if the
1e716 20 63 75 72 73 6f 72 20 62 61 63 6b 75 70 20 77 cursor backup w
1e717 61 73 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a as successful,.*
1e718 2a 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 * jump immediate
1e719 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 ly to P2..**.**
1e71a 54 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 The P1 cursor mu
1e71b 73 74 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c st be for a real
1e71c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 table, not a ps
1e71d 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 eudo-table..*/.c
1e71e 61 73 65 20 4f 50 5f 50 72 65 76 3a 20 20 20 20 ase OP_Prev:
1e71f 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f /* jump */
1e720 0a 63 61 73 65 20 4f 50 5f 4e 65 78 74 3a 20 7b .case OP_Next: {
1e721 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 /* jump
1e722 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1e723 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e724 65 64 20 69 6e 74 6f 20 75 2e 62 6d 20 2a 2f 0a ed into u.bm */.
1e725 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1e726 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 ;. BtCursor *pC
1e727 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a rsr;. int res;.
1e728 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1e729 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1e72a 69 6e 74 6f 20 75 2e 62 6d 20 2a 2f 0a 0a 20 20 into u.bm */..
1e72b 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 CHECK_FOR_INTERR
1e72c 55 50 54 3b 0a 20 20 61 73 73 65 72 74 28 20 70 UPT;. assert( p
1e72d 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 Op->p1>=0 && pOp
1e72e 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 ->p1<p->nCursor
1e72f 29 3b 0a 20 20 75 2e 62 6d 2e 70 43 20 3d 20 70 );. u.bm.pC = p
1e730 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d ->apCsr[pOp->p1]
1e731 3b 0a 20 20 69 66 28 20 75 2e 62 6d 2e 70 43 3d ;. if( u.bm.pC=
1e732 3d 30 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b =0 ){. break;
1e733 20 20 2f 2a 20 53 65 65 20 74 69 63 6b 65 74 20 /* See ticket
1e734 23 32 32 37 33 20 2a 2f 0a 20 20 7d 0a 20 20 75 #2273 */. }. u
1e735 2e 62 6d 2e 70 43 72 73 72 20 3d 20 75 2e 62 6d .bm.pCrsr = u.bm
1e736 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 .pC->pCursor;.
1e737 69 66 28 20 75 2e 62 6d 2e 70 43 72 73 72 3d 3d if( u.bm.pCrsr==
1e738 30 20 29 7b 0a 20 20 20 20 75 2e 62 6d 2e 70 43 0 ){. u.bm.pC
1e739 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 ->nullRow = 1;.
1e73a 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 break;. }.
1e73b 75 2e 62 6d 2e 72 65 73 20 3d 20 31 3b 0a 20 20 u.bm.res = 1;.
1e73c 61 73 73 65 72 74 28 20 75 2e 62 6d 2e 70 43 2d assert( u.bm.pC-
1e73d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d >deferredMoveto=
1e73e 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 70 4f 70 =0 );. rc = pOp
1e73f 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 65 78 ->opcode==OP_Nex
1e740 74 20 3f 20 73 71 6c 69 74 65 33 42 74 72 65 65 t ? sqlite3Btree
1e741 4e 65 78 74 28 75 2e 62 6d 2e 70 43 72 73 72 2c Next(u.bm.pCrsr,
1e742 20 26 75 2e 62 6d 2e 72 65 73 29 20 3a 0a 20 20 &u.bm.res) :.
1e743 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e744 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
1e745 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 te3BtreePrevious
1e746 28 75 2e 62 6d 2e 70 43 72 73 72 2c 20 26 75 2e (u.bm.pCrsr, &u.
1e747 62 6d 2e 72 65 73 29 3b 0a 20 20 75 2e 62 6d 2e bm.res);. u.bm.
1e748 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 pC->nullRow = (u
1e749 38 29 75 2e 62 6d 2e 72 65 73 3b 0a 20 20 75 2e 8)u.bm.res;. u.
1e74a 62 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 bm.pC->cacheStat
1e74b 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 us = CACHE_STALE
1e74c 3b 0a 20 20 69 66 28 20 75 2e 62 6d 2e 72 65 73 ;. if( u.bm.res
1e74d 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 ==0 ){. pc =
1e74e 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 pOp->p2 - 1;.
1e74f 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 20 70 if( pOp->p5 ) p
1e750 2d 3e 61 43 6f 75 6e 74 65 72 5b 70 4f 70 2d 3e ->aCounter[pOp->
1e751 70 35 2d 31 5d 2b 2b 3b 0a 23 69 66 64 65 66 20 p5-1]++;.#ifdef
1e752 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 SQLITE_TEST.
1e753 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 sqlite3_search_c
1e754 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 ount++;.#endif.
1e755 20 7d 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e 72 6f }. u.bm.pC->ro
1e756 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a widIsValid = 0;.
1e757 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1e758 70 63 6f 64 65 3a 20 49 64 78 49 6e 73 65 72 74 pcode: IdxInsert
1e759 20 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a 2a P1 P2 P3 * P5.*
1e75a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 32 *.** Register P2
1e75b 20 68 6f 6c 64 73 20 61 20 53 51 4c 20 69 6e 64 holds a SQL ind
1e75c 65 78 20 6b 65 79 20 6d 61 64 65 20 75 73 69 6e ex key made usin
1e75d 67 20 74 68 65 0a 2a 2a 20 4d 61 6b 65 52 65 63 g the.** MakeRec
1e75e 6f 72 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 ord instructions
1e75f 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 . This opcode w
1e760 72 69 74 65 73 20 74 68 61 74 20 6b 65 79 0a 2a rites that key.*
1e761 2a 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 65 78 * into the index
1e762 20 50 31 2e 20 20 44 61 74 61 20 66 6f 72 20 74 P1. Data for t
1e763 68 65 20 65 6e 74 72 79 20 69 73 20 6e 69 6c 2e he entry is nil.
1e764 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 61 20 66 .**.** P3 is a f
1e765 6c 61 67 20 74 68 61 74 20 70 72 6f 76 69 64 65 lag that provide
1e766 73 20 61 20 68 69 6e 74 20 74 6f 20 74 68 65 20 s a hint to the
1e767 62 2d 74 72 65 65 20 6c 61 79 65 72 20 74 68 61 b-tree layer tha
1e768 74 20 74 68 69 73 0a 2a 2a 20 69 6e 73 65 72 74 t this.** insert
1e769 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 is likely to be
1e76a 20 61 6e 20 61 70 70 65 6e 64 2e 0a 2a 2a 0a 2a an append..**.*
1e76b 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 * This instructi
1e76c 6f 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f on only works fo
1e76d 72 20 69 6e 64 69 63 65 73 2e 20 20 54 68 65 20 r indices. The
1e76e 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 73 74 72 equivalent instr
1e76f 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 61 uction.** for ta
1e770 62 6c 65 73 20 69 73 20 4f 50 5f 49 6e 73 65 72 bles is OP_Inser
1e771 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 t..*/.case OP_Id
1e772 78 49 6e 73 65 72 74 3a 20 7b 20 20 20 20 20 20 xInsert: {
1e773 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69 66 20 /* in2 */.#if
1e774 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1e775 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e776 20 75 2e 62 6e 20 2a 2f 0a 20 20 56 64 62 65 43 u.bn */. VdbeC
1e777 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 ursor *pC;. BtC
1e778 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 ursor *pCrsr;.
1e779 69 6e 74 20 6e 4b 65 79 3b 0a 20 20 63 6f 6e 73 int nKey;. cons
1e77a 74 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 23 65 t char *zKey;.#e
1e77b 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1e77c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e77d 74 6f 20 75 2e 62 6e 20 2a 2f 0a 0a 20 20 61 73 to u.bn */.. as
1e77e 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1e77f 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e && pOp->p1<p->n
1e780 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6e Cursor );. u.bn
1e781 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 .pC = p->apCsr[p
1e782 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1e783 74 28 20 75 2e 62 6e 2e 70 43 21 3d 30 20 29 3b t( u.bn.pC!=0 );
1e784 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 32 2d . assert( pIn2-
1e785 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f >flags & MEM_Blo
1e786 62 20 29 3b 0a 20 20 75 2e 62 6e 2e 70 43 72 73 b );. u.bn.pCrs
1e787 72 20 3d 20 75 2e 62 6e 2e 70 43 2d 3e 70 43 75 r = u.bn.pC->pCu
1e788 72 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c 57 41 rsor;. if( ALWA
1e789 59 53 28 75 2e 62 6e 2e 70 43 72 73 72 21 3d 30 YS(u.bn.pCrsr!=0
1e78a 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 ) ){. assert(
1e78b 20 75 2e 62 6e 2e 70 43 2d 3e 69 73 54 61 62 6c u.bn.pC->isTabl
1e78c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d e==0 );. rc =
1e78d 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 32 ExpandBlob(pIn2
1e78e 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
1e78f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1e790 20 20 75 2e 62 6e 2e 6e 4b 65 79 20 3d 20 70 49 u.bn.nKey = pI
1e791 6e 32 2d 3e 6e 3b 0a 20 20 20 20 20 20 75 2e 62 n2->n;. u.b
1e792 6e 2e 7a 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 7a n.zKey = pIn2->z
1e793 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
1e794 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 ite3BtreeInsert(
1e795 75 2e 62 6e 2e 70 43 72 73 72 2c 20 75 2e 62 6e u.bn.pCrsr, u.bn
1e796 2e 7a 4b 65 79 2c 20 75 2e 62 6e 2e 6e 4b 65 79 .zKey, u.bn.nKey
1e797 2c 20 22 22 2c 20 30 2c 20 30 2c 20 70 4f 70 2d , "", 0, 0, pOp-
1e798 3e 70 33 2c 0a 20 20 20 20 20 20 20 20 20 20 28 >p3,. (
1e799 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 (pOp->p5 & OPFLA
1e79a 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 G_USESEEKRESULT)
1e79b 20 3f 20 75 2e 62 6e 2e 70 43 2d 3e 73 65 65 6b ? u.bn.pC->seek
1e79c 52 65 73 75 6c 74 20 3a 20 30 29 0a 20 20 20 20 Result : 0).
1e79d 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 );. asser
1e79e 74 28 20 75 2e 62 6e 2e 70 43 2d 3e 64 65 66 65 t( u.bn.pC->defe
1e79f 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b rredMoveto==0 );
1e7a0 0a 20 20 20 20 20 20 75 2e 62 6e 2e 70 43 2d 3e . u.bn.pC->
1e7a1 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 cacheStatus = CA
1e7a2 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 7d CHE_STALE;. }
1e7a3 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1e7a4 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 44 ./* Opcode: IdxD
1e7a5 65 6c 65 74 65 20 50 31 20 50 32 20 50 33 20 2a elete P1 P2 P3 *
1e7a6 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e *.**.** The con
1e7a7 74 65 6e 74 20 6f 66 20 50 33 20 72 65 67 69 73 tent of P3 regis
1e7a8 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 61 74 ters starting at
1e7a9 20 72 65 67 69 73 74 65 72 20 50 32 20 66 6f 72 register P2 for
1e7aa 6d 0a 2a 2a 20 61 6e 20 75 6e 70 61 63 6b 65 64 m.** an unpacked
1e7ab 20 69 6e 64 65 78 20 6b 65 79 2e 20 54 68 69 73 index key. This
1e7ac 20 6f 70 63 6f 64 65 20 72 65 6d 6f 76 65 73 20 opcode removes
1e7ad 74 68 61 74 20 65 6e 74 72 79 20 66 72 6f 6d 20 that entry from
1e7ae 74 68 65 20 0a 2a 2a 20 69 6e 64 65 78 20 6f 70 the .** index op
1e7af 65 6e 65 64 20 62 79 20 63 75 72 73 6f 72 20 50 ened by cursor P
1e7b0 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 1..*/.case OP_Id
1e7b1 78 44 65 6c 65 74 65 3a 20 7b 0a 23 69 66 20 30 xDelete: {.#if 0
1e7b2 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1e7b3 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e7b4 75 2e 62 6f 20 2a 2f 0a 20 20 56 64 62 65 43 75 u.bo */. VdbeCu
1e7b5 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 rsor *pC;. BtCu
1e7b6 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 rsor *pCrsr;. i
1e7b7 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b nt res;. Unpack
1e7b8 65 64 52 65 63 6f 72 64 20 72 3b 0a 23 65 6e 64 edRecord r;.#end
1e7b9 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1e7ba 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e7bb 20 75 2e 62 6f 20 2a 2f 0a 0a 20 20 61 73 73 65 u.bo */.. asse
1e7bc 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b rt( pOp->p3>0 );
1e7bd 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1e7be 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b p2>0 && pOp->p2+
1e7bf 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p3<=p->nMem
1e7c0 2b 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 +1 );. assert(
1e7c1 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f pOp->p1>=0 && pO
1e7c2 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 p->p1<p->nCursor
1e7c3 20 29 3b 0a 20 20 75 2e 62 6f 2e 70 43 20 3d 20 );. u.bo.pC =
1e7c4 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 p->apCsr[pOp->p1
1e7c5 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 ];. assert( u.b
1e7c6 6f 2e 70 43 21 3d 30 20 29 3b 0a 20 20 75 2e 62 o.pC!=0 );. u.b
1e7c7 6f 2e 70 43 72 73 72 20 3d 20 75 2e 62 6f 2e 70 o.pCrsr = u.bo.p
1e7c8 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 C->pCursor;. if
1e7c9 28 20 41 4c 57 41 59 53 28 75 2e 62 6f 2e 70 43 ( ALWAYS(u.bo.pC
1e7ca 72 73 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 75 rsr!=0) ){. u
1e7cb 2e 62 6f 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d .bo.r.pKeyInfo =
1e7cc 20 75 2e 62 6f 2e 70 43 2d 3e 70 4b 65 79 49 6e u.bo.pC->pKeyIn
1e7cd 66 6f 3b 0a 20 20 20 20 75 2e 62 6f 2e 72 2e 6e fo;. u.bo.r.n
1e7ce 46 69 65 6c 64 20 3d 20 28 75 31 36 29 70 4f 70 Field = (u16)pOp
1e7cf 2d 3e 70 33 3b 0a 20 20 20 20 75 2e 62 6f 2e 72 ->p3;. u.bo.r
1e7d0 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 .flags = 0;.
1e7d1 75 2e 62 6f 2e 72 2e 61 4d 65 6d 20 3d 20 26 70 u.bo.r.aMem = &p
1e7d2 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b ->aMem[pOp->p2];
1e7d3 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1e7d4 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 3BtreeMovetoUnpa
1e7d5 63 6b 65 64 28 75 2e 62 6f 2e 70 43 72 73 72 2c cked(u.bo.pCrsr,
1e7d6 20 26 75 2e 62 6f 2e 72 2c 20 30 2c 20 30 2c 20 &u.bo.r, 0, 0,
1e7d7 26 75 2e 62 6f 2e 72 65 73 29 3b 0a 20 20 20 20 &u.bo.res);.
1e7d8 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1e7d9 4b 20 26 26 20 75 2e 62 6f 2e 72 65 73 3d 3d 30 K && u.bo.res==0
1e7da 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
1e7db 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 qlite3BtreeDelet
1e7dc 65 28 75 2e 62 6f 2e 70 43 72 73 72 29 3b 0a 20 e(u.bo.pCrsr);.
1e7dd 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
1e7de 20 75 2e 62 6f 2e 70 43 2d 3e 64 65 66 65 72 72 u.bo.pC->deferr
1e7df 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 edMoveto==0 );.
1e7e0 20 20 20 75 2e 62 6f 2e 70 43 2d 3e 63 61 63 68 u.bo.pC->cach
1e7e1 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f eStatus = CACHE_
1e7e2 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 62 72 65 STALE;. }. bre
1e7e3 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1e7e4 3a 20 49 64 78 52 6f 77 69 64 20 50 31 20 50 32 : IdxRowid P1 P2
1e7e5 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 * * *.**.** Wri
1e7e6 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 te into register
1e7e7 20 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20 77 P2 an integer w
1e7e8 68 69 63 68 20 69 73 20 74 68 65 20 6c 61 73 74 hich is the last
1e7e9 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72 65 entry in the re
1e7ea 63 6f 72 64 20 61 74 0a 2a 2a 20 74 68 65 20 65 cord at.** the e
1e7eb 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 nd of the index
1e7ec 6b 65 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 key pointed to b
1e7ed 79 20 63 75 72 73 6f 72 20 50 31 2e 20 20 54 68 y cursor P1. Th
1e7ee 69 73 20 69 6e 74 65 67 65 72 20 73 68 6f 75 6c is integer shoul
1e7ef 64 20 62 65 0a 2a 2a 20 74 68 65 20 72 6f 77 69 d be.** the rowi
1e7f0 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65 d of the table e
1e7f1 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 68 ntry to which th
1e7f2 69 73 20 69 6e 64 65 78 20 65 6e 74 72 79 20 70 is index entry p
1e7f3 6f 69 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 oints..**.** See
1e7f4 20 61 6c 73 6f 3a 20 52 6f 77 69 64 2c 20 4d 61 also: Rowid, Ma
1e7f5 6b 65 52 65 63 6f 72 64 2e 0a 2a 2f 0a 63 61 73 keRecord..*/.cas
1e7f6 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 3a 20 7b e OP_IdxRowid: {
1e7f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1e7f8 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 out2-prerelease
1e7f9 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f */.#if 0 /* lo
1e7fa 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1e7fb 76 65 64 20 69 6e 74 6f 20 75 2e 62 70 20 2a 2f ved into u.bp */
1e7fc 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 . BtCursor *pCr
1e7fd 73 72 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 sr;. VdbeCursor
1e7fe 20 2a 70 43 3b 0a 20 20 69 36 34 20 72 6f 77 69 *pC;. i64 rowi
1e7ff 64 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 d;.#endif /* loc
1e800 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e801 65 64 20 69 6e 74 6f 20 75 2e 62 70 20 2a 2f 0a ed into u.bp */.
1e802 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1e803 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1e804 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1e805 20 75 2e 62 70 2e 70 43 20 3d 20 70 2d 3e 61 70 u.bp.pC = p->ap
1e806 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1e807 61 73 73 65 72 74 28 20 75 2e 62 70 2e 70 43 21 assert( u.bp.pC!
1e808 3d 30 20 29 3b 0a 20 20 75 2e 62 70 2e 70 43 72 =0 );. u.bp.pCr
1e809 73 72 20 3d 20 75 2e 62 70 2e 70 43 2d 3e 70 43 sr = u.bp.pC->pC
1e80a 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c 57 ursor;. if( ALW
1e80b 41 59 53 28 75 2e 62 70 2e 70 43 72 73 72 21 3d AYS(u.bp.pCrsr!=
1e80c 30 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 0) ){. rc = s
1e80d 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 qlite3VdbeCursor
1e80e 4d 6f 76 65 74 6f 28 75 2e 62 70 2e 70 43 29 3b Moveto(u.bp.pC);
1e80f 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 . if( NEVER(r
1e810 63 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f c) ) goto abort_
1e811 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
1e812 20 20 61 73 73 65 72 74 28 20 75 2e 62 70 2e 70 assert( u.bp.p
1e813 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 C->deferredMovet
1e814 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 o==0 );. asse
1e815 72 74 28 20 75 2e 62 70 2e 70 43 2d 3e 69 73 54 rt( u.bp.pC->isT
1e816 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 able==0 );. i
1e817 66 28 20 21 75 2e 62 70 2e 70 43 2d 3e 6e 75 6c f( !u.bp.pC->nul
1e818 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 72 63 lRow ){. rc
1e819 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 = sqlite3VdbeId
1e81a 78 52 6f 77 69 64 28 64 62 2c 20 75 2e 62 70 2e xRowid(db, u.bp.
1e81b 70 43 72 73 72 2c 20 26 75 2e 62 70 2e 72 6f 77 pCrsr, &u.bp.row
1e81c 69 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 id);. if( r
1e81d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1e81e 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f goto abo
1e81f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b rt_due_to_error;
1e820 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 4d . }. M
1e821 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f emSetTypeFlag(pO
1e822 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 ut, MEM_Int);.
1e823 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 pOut->u.i =
1e824 75 2e 62 70 2e 72 6f 77 69 64 3b 0a 20 20 20 20 u.bp.rowid;.
1e825 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d }. }. break;.}
1e826 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 ../* Opcode: Idx
1e827 47 45 20 50 31 20 50 32 20 50 33 20 50 34 20 50 GE P1 P2 P3 P4 P
1e828 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 72 5.**.** The P4 r
1e829 65 67 69 73 74 65 72 20 76 61 6c 75 65 73 20 62 egister values b
1e82a 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 33 eginning with P3
1e82b 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65 form an unpacke
1e82c 64 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 79 20 d index .** key
1e82d 74 68 61 74 20 6f 6d 69 74 73 20 74 68 65 20 52 that omits the R
1e82e 4f 57 49 44 2e 20 20 43 6f 6d 70 61 72 65 20 74 OWID. Compare t
1e82f 68 69 73 20 6b 65 79 20 76 61 6c 75 65 20 61 67 his key value ag
1e830 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78 20 ainst the index
1e831 0a 2a 2a 20 74 68 61 74 20 50 31 20 69 73 20 63 .** that P1 is c
1e832 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e urrently pointin
1e833 67 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 20 74 g to, ignoring t
1e834 68 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 65 20 he ROWID on the
1e835 50 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 P1 index..**.**
1e836 49 66 20 74 68 65 20 50 31 20 69 6e 64 65 78 20 If the P1 index
1e837 65 6e 74 72 79 20 69 73 20 67 72 65 61 74 65 72 entry is greater
1e838 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
1e839 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 0a o the key value.
1e83a 2a 2a 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 ** then jump to
1e83b 50 32 2e 20 20 4f 74 68 65 72 77 69 73 65 20 66 P2. Otherwise f
1e83c 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 all through to t
1e83d 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 he next instruct
1e83e 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 ion..**.** If P5
1e83f 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 is non-zero the
1e840 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20 n the key value
1e841 69 73 20 69 6e 63 72 65 61 73 65 64 20 62 79 20 is increased by
1e842 61 6e 20 65 70 73 69 6c 6f 6e 20 0a 2a 2a 20 70 an epsilon .** p
1e843 72 69 6f 72 20 74 6f 20 74 68 65 20 63 6f 6d 70 rior to the comp
1e844 61 72 69 73 6f 6e 2e 20 20 54 68 69 73 20 6d 61 arison. This ma
1e845 6b 65 20 74 68 65 20 6f 70 63 6f 64 65 20 77 6f ke the opcode wo
1e846 72 6b 20 6c 69 6b 65 20 49 64 78 47 54 20 65 78 rk like IdxGT ex
1e847 63 65 70 74 0a 2a 2a 20 74 68 61 74 20 69 66 20 cept.** that if
1e848 74 68 65 20 6b 65 79 20 66 72 6f 6d 20 72 65 67 the key from reg
1e849 69 73 74 65 72 20 50 33 20 69 73 20 61 20 70 72 ister P3 is a pr
1e84a 65 66 69 78 20 6f 66 20 74 68 65 20 6b 65 79 20 efix of the key
1e84b 69 6e 20 74 68 65 20 63 75 72 73 6f 72 2c 0a 2a in the cursor,.*
1e84c 2a 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 * the result is
1e84d 66 61 6c 73 65 20 77 68 65 72 65 61 73 20 69 74 false whereas it
1e84e 20 77 6f 75 6c 64 20 62 65 20 74 72 75 65 20 77 would be true w
1e84f 69 74 68 20 49 64 78 47 54 2e 0a 2a 2f 0a 2f 2a ith IdxGT..*/./*
1e850 20 4f 70 63 6f 64 65 3a 20 49 64 78 4c 54 20 50 Opcode: IdxLT P
1e851 31 20 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 1 P2 P3 * P5.**.
1e852 2a 2a 20 54 68 65 20 50 34 20 72 65 67 69 73 74 ** The P4 regist
1e853 65 72 20 76 61 6c 75 65 73 20 62 65 67 69 6e 6e er values beginn
1e854 69 6e 67 20 77 69 74 68 20 50 33 20 66 6f 72 6d ing with P3 form
1e855 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 an unpacked ind
1e856 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74 20 ex .** key that
1e857 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44 2e omits the ROWID.
1e858 20 20 43 6f 6d 70 61 72 65 20 74 68 69 73 20 6b Compare this k
1e859 65 79 20 76 61 6c 75 65 20 61 67 61 69 6e 73 74 ey value against
1e85a 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 74 the index .** t
1e85b 68 61 74 20 50 31 20 69 73 20 63 75 72 72 65 6e hat P1 is curren
1e85c 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2c tly pointing to,
1e85d 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 52 4f ignoring the RO
1e85e 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69 6e WID on the P1 in
1e85f 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 dex..**.** If th
1e860 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72 79 e P1 index entry
1e861 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 is less than th
1e862 65 20 6b 65 79 20 76 61 6c 75 65 20 74 68 65 6e e key value then
1e863 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 20 jump to P2..**
1e864 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 74 Otherwise fall t
1e865 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 hrough to the ne
1e866 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a xt instruction..
1e867 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 6e **.** If P5 is n
1e868 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 on-zero then the
1e869 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 69 6e key value is in
1e86a 63 72 65 61 73 65 64 20 62 79 20 61 6e 20 65 70 creased by an ep
1e86b 73 69 6c 6f 6e 20 70 72 69 6f 72 20 0a 2a 2a 20 silon prior .**
1e86c 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f to the compariso
1e86d 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74 n. This makes t
1e86e 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20 6c he opcode work l
1e86f 69 6b 65 20 49 64 78 4c 45 2e 0a 2a 2f 0a 63 61 ike IdxLE..*/.ca
1e870 73 65 20 4f 50 5f 49 64 78 4c 54 3a 20 20 20 20 se OP_IdxLT:
1e871 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 /* jump, i
1e872 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 n3 */.case OP_Id
1e873 78 47 45 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a xGE: { /*
1e874 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 jump, in3 */.#i
1e875 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1e876 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e877 74 6f 20 75 2e 62 71 20 2a 2f 0a 20 20 56 64 62 to u.bq */. Vdb
1e878 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 eCursor *pC;. i
1e879 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b nt res;. Unpack
1e87a 65 64 52 65 63 6f 72 64 20 72 3b 0a 23 65 6e 64 edRecord r;.#end
1e87b 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1e87c 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e87d 20 75 2e 62 71 20 2a 2f 0a 0a 20 20 61 73 73 65 u.bq */.. asse
1e87e 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1e87f 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 & pOp->p1<p->nCu
1e880 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 71 2e 70 rsor );. u.bq.p
1e881 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 C = p->apCsr[pOp
1e882 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1e883 20 75 2e 62 71 2e 70 43 21 3d 30 20 29 3b 0a 20 u.bq.pC!=0 );.
1e884 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 71 if( ALWAYS(u.bq
1e885 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 29 .pC->pCursor!=0)
1e886 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1e887 75 2e 62 71 2e 70 43 2d 3e 64 65 66 65 72 72 65 u.bq.pC->deferre
1e888 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 dMoveto==0 );.
1e889 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1e88a 35 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 35 3d 5==0 || pOp->p5=
1e88b 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =1 );. assert
1e88c 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1e88d 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 75 4_INT32 );. u
1e88e 2e 62 71 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d .bq.r.pKeyInfo =
1e88f 20 75 2e 62 71 2e 70 43 2d 3e 70 4b 65 79 49 6e u.bq.pC->pKeyIn
1e890 66 6f 3b 0a 20 20 20 20 75 2e 62 71 2e 72 2e 6e fo;. u.bq.r.n
1e891 46 69 65 6c 64 20 3d 20 28 75 31 36 29 70 4f 70 Field = (u16)pOp
1e892 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 69 66 28 20 ->p4.i;. if(
1e893 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 20 pOp->p5 ){.
1e894 20 75 2e 62 71 2e 72 2e 66 6c 61 67 73 20 3d 20 u.bq.r.flags =
1e895 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 UNPACKED_INCRKEY
1e896 20 7c 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f | UNPACKED_IGNO
1e897 52 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 65 RE_ROWID;. }e
1e898 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e 62 71 2e lse{. u.bq.
1e899 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b r.flags = UNPACK
1e89a 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b ED_IGNORE_ROWID;
1e89b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 71 2e . }. u.bq.
1e89c 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 r.aMem = &p->aMe
1e89d 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 m[pOp->p3];.
1e89e 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
1e89f 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 75 2e IdxKeyCompare(u.
1e8a0 62 71 2e 70 43 2c 20 26 75 2e 62 71 2e 72 2c 20 bq.pC, &u.bq.r,
1e8a1 26 75 2e 62 71 2e 72 65 73 29 3b 0a 20 20 20 20 &u.bq.res);.
1e8a2 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d if( pOp->opcode=
1e8a3 3d 4f 50 5f 49 64 78 4c 54 20 29 7b 0a 20 20 20 =OP_IdxLT ){.
1e8a4 20 20 20 75 2e 62 71 2e 72 65 73 20 3d 20 2d 75 u.bq.res = -u
1e8a5 2e 62 71 2e 72 65 73 3b 0a 20 20 20 20 7d 65 6c .bq.res;. }el
1e8a6 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 se{. assert
1e8a7 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f ( pOp->opcode==O
1e8a8 50 5f 49 64 78 47 45 20 29 3b 0a 20 20 20 20 20 P_IdxGE );.
1e8a9 20 75 2e 62 71 2e 72 65 73 2b 2b 3b 0a 20 20 20 u.bq.res++;.
1e8aa 20 7d 0a 20 20 20 20 69 66 28 20 75 2e 62 71 2e }. if( u.bq.
1e8ab 72 65 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 res>0 ){. p
1e8ac 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 20 c = pOp->p2 - 1
1e8ad 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 ;. }. }. br
1e8ae 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1e8af 65 3a 20 44 65 73 74 72 6f 79 20 50 31 20 50 32 e: Destroy P1 P2
1e8b0 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 P3 * *.**.** De
1e8b1 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 64 lete an entire d
1e8b2 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 atabase table or
1e8b3 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f index whose roo
1e8b4 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 t page in the da
1e8b5 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 tabase.** file i
1e8b6 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e 0a 2a s given by P1..*
1e8b7 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 62 *.** The table b
1e8b8 65 69 6e 67 20 64 65 73 74 72 6f 79 65 64 20 69 eing destroyed i
1e8b9 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 s in the main da
1e8ba 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 tabase file if P
1e8bb 33 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 33 3d 3==0. If.** P3=
1e8bc 3d 31 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c =1 then the tabl
1e8bd 65 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73 e to be clear is
1e8be 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 in the auxiliar
1e8bf 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a y database file.
1e8c0 2a 2a 20 74 68 61 74 20 69 73 20 75 73 65 64 20 ** that is used
1e8c1 74 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20 to store tables
1e8c2 63 72 65 61 74 65 20 75 73 69 6e 67 20 43 52 45 create using CRE
1e8c3 41 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41 ATE TEMPORARY TA
1e8c4 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 BLE..**.** If AU
1e8c5 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e 61 62 TOVACUUM is enab
1e8c6 6c 65 64 20 74 68 65 6e 20 69 74 20 69 73 20 70 led then it is p
1e8c7 6f 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f ossible that ano
1e8c8 74 68 65 72 20 72 6f 6f 74 20 70 61 67 65 0a 2a ther root page.*
1e8c9 2a 20 6d 69 67 68 74 20 62 65 20 6d 6f 76 65 64 * might be moved
1e8ca 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 6c 79 20 into the newly
1e8cb 64 65 6c 65 74 65 64 20 72 6f 6f 74 20 70 61 67 deleted root pag
1e8cc 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 e in order to ke
1e8cd 65 70 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 ep all.** root p
1e8ce 61 67 65 73 20 63 6f 6e 74 69 67 75 6f 75 73 20 ages contiguous
1e8cf 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 at the beginning
1e8d0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1e8d1 2e 20 20 54 68 65 20 66 6f 72 6d 65 72 0a 2a 2a . The former.**
1e8d2 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f value of the ro
1e8d3 6f 74 20 70 61 67 65 20 74 68 61 74 20 6d 6f 76 ot page that mov
1e8d4 65 64 20 2d 20 69 74 73 20 76 61 6c 75 65 20 62 ed - its value b
1e8d5 65 66 6f 72 65 20 74 68 65 20 6d 6f 76 65 20 6f efore the move o
1e8d6 63 63 75 72 72 65 64 20 2d 0a 2a 2a 20 69 73 20 ccurred -.** is
1e8d7 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 stored in regist
1e8d8 65 72 20 50 32 2e 20 20 49 66 20 6e 6f 20 70 61 er P2. If no pa
1e8d9 67 65 20 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74 20 ge .** movement
1e8da 77 61 73 20 72 65 71 75 69 72 65 64 20 28 62 65 was required (be
1e8db 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65 20 cause the table
1e8dc 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20 77 61 being dropped wa
1e8dd 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 74 68 s already .** th
1e8de 65 20 6c 61 73 74 20 6f 6e 65 20 69 6e 20 74 68 e last one in th
1e8df 65 20 64 61 74 61 62 61 73 65 29 20 74 68 65 6e e database) then
1e8e0 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 65 a zero is store
1e8e1 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 d in register P2
1e8e2 2e 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55 ..** If AUTOVACU
1e8e3 55 4d 20 69 73 20 64 69 73 61 62 6c 65 64 20 74 UM is disabled t
1e8e4 68 65 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74 hen a zero is st
1e8e5 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 ored in register
1e8e6 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 P2..**.** See a
1e8e7 6c 73 6f 3a 20 43 6c 65 61 72 0a 2a 2f 0a 63 61 lso: Clear.*/.ca
1e8e8 73 65 20 4f 50 5f 44 65 73 74 72 6f 79 3a 20 7b se OP_Destroy: {
1e8e9 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 /* out2-pre
1e8ea 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 release */.#if 0
1e8eb 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1e8ec 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e8ed 75 2e 62 72 20 2a 2f 0a 20 20 69 6e 74 20 69 4d u.br */. int iM
1e8ee 6f 76 65 64 3b 0a 20 20 69 6e 74 20 69 43 6e 74 oved;. int iCnt
1e8ef 3b 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b ;. Vdbe *pVdbe;
1e8f0 0a 20 20 69 6e 74 20 69 44 62 3b 0a 23 65 6e 64 . int iDb;.#end
1e8f1 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1e8f2 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e8f3 20 75 2e 62 72 20 2a 2f 0a 23 69 66 6e 64 65 66 u.br */.#ifndef
1e8f4 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1e8f5 54 55 41 4c 54 41 42 4c 45 0a 20 20 75 2e 62 72 TUALTABLE. u.br
1e8f6 2e 69 43 6e 74 20 3d 20 30 3b 0a 20 20 66 6f 72 .iCnt = 0;. for
1e8f7 28 75 2e 62 72 2e 70 56 64 62 65 3d 64 62 2d 3e (u.br.pVdbe=db->
1e8f8 70 56 64 62 65 3b 20 75 2e 62 72 2e 70 56 64 62 pVdbe; u.br.pVdb
1e8f9 65 3b 20 75 2e 62 72 2e 70 56 64 62 65 20 3d 20 e; u.br.pVdbe =
1e8fa 75 2e 62 72 2e 70 56 64 62 65 2d 3e 70 4e 65 78 u.br.pVdbe->pNex
1e8fb 74 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 62 72 t){. if( u.br
1e8fc 2e 70 56 64 62 65 2d 3e 6d 61 67 69 63 3d 3d 56 .pVdbe->magic==V
1e8fd 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 26 DBE_MAGIC_RUN &&
1e8fe 20 75 2e 62 72 2e 70 56 64 62 65 2d 3e 69 6e 56 u.br.pVdbe->inV
1e8ff 74 61 62 4d 65 74 68 6f 64 3c 32 20 26 26 20 75 tabMethod<2 && u
1e900 2e 62 72 2e 70 56 64 62 65 2d 3e 70 63 3e 3d 30 .br.pVdbe->pc>=0
1e901 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 72 2e 69 ){. u.br.i
1e902 43 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d Cnt++;. }. }
1e903 0a 23 65 6c 73 65 0a 20 20 75 2e 62 72 2e 69 43 .#else. u.br.iC
1e904 6e 74 20 3d 20 64 62 2d 3e 61 63 74 69 76 65 56 nt = db->activeV
1e905 64 62 65 43 6e 74 3b 0a 23 65 6e 64 69 66 0a 20 dbeCnt;.#endif.
1e906 20 69 66 28 20 75 2e 62 72 2e 69 43 6e 74 3e 31 if( u.br.iCnt>1
1e907 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
1e908 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 ITE_LOCKED;.
1e909 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d p->errorAction =
1e90a 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 7d 65 6c OE_Abort;. }el
1e90b 73 65 7b 0a 20 20 20 20 75 2e 62 72 2e 69 44 62 se{. u.br.iDb
1e90c 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 = pOp->p3;.
1e90d 61 73 73 65 72 74 28 20 75 2e 62 72 2e 69 43 6e assert( u.br.iCn
1e90e 74 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 t==1 );. asse
1e90f 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 rt( (p->btreeMas
1e910 6b 20 26 20 28 31 3c 3c 75 2e 62 72 2e 69 44 62 k & (1<<u.br.iDb
1e911 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 ))!=0 );. rc
1e912 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 = sqlite3BtreeDr
1e913 6f 70 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 5b opTable(db->aDb[
1e914 75 2e 62 72 2e 69 44 62 5d 2e 70 42 74 2c 20 70 u.br.iDb].pBt, p
1e915 4f 70 2d 3e 70 31 2c 20 26 75 2e 62 72 2e 69 4d Op->p1, &u.br.iM
1e916 6f 76 65 64 29 3b 0a 20 20 20 20 4d 65 6d 53 65 oved);. MemSe
1e917 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
1e918 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 70 4f MEM_Int);. pO
1e919 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 72 2e 69 ut->u.i = u.br.i
1e91a 4d 6f 76 65 64 3b 0a 23 69 66 6e 64 65 66 20 53 Moved;.#ifndef S
1e91b 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
1e91c 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 72 63 ACUUM. if( rc
1e91d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75 ==SQLITE_OK && u
1e91e 2e 62 72 2e 69 4d 6f 76 65 64 21 3d 30 20 29 7b .br.iMoved!=0 ){
1e91f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f . sqlite3Ro
1e920 6f 74 50 61 67 65 4d 6f 76 65 64 28 26 64 62 2d otPageMoved(&db-
1e921 3e 61 44 62 5b 75 2e 62 72 2e 69 44 62 5d 2c 20 >aDb[u.br.iDb],
1e922 75 2e 62 72 2e 69 4d 6f 76 65 64 2c 20 70 4f 70 u.br.iMoved, pOp
1e923 2d 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 23 65 6e ->p1);. }.#en
1e924 64 69 66 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b dif. }. break;
1e925 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 .}../* Opcode: C
1e926 6c 65 61 72 20 50 31 20 50 32 20 50 33 0a 2a 2a lear P1 P2 P3.**
1e927 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 63 .** Delete all c
1e928 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64 ontents of the d
1e929 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 atabase table or
1e92a 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f index whose roo
1e92b 74 20 70 61 67 65 0a 2a 2a 20 69 6e 20 74 68 65 t page.** in the
1e92c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
1e92d 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e 20 20 s given by P1.
1e92e 42 75 74 2c 20 75 6e 6c 69 6b 65 20 44 65 73 74 But, unlike Dest
1e92f 72 6f 79 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20 72 roy, do not.** r
1e930 65 6d 6f 76 65 20 74 68 65 20 74 61 62 6c 65 20 emove the table
1e931 6f 72 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 68 or index from th
1e932 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
1e933 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 .**.** The table
1e934 20 62 65 69 6e 67 20 63 6c 65 61 72 20 69 73 20 being clear is
1e935 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 in the main data
1e936 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 32 3d base file if P2=
1e937 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 32 3d 3d 31 =0. If.** P2==1
1e938 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 then the table
1e939 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73 20 69 to be clear is i
1e93a 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 n the auxiliary
1e93b 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a database file.**
1e93c 20 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f that is used to
1e93d 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20 63 72 store tables cr
1e93e 65 61 74 65 20 75 73 69 6e 67 20 43 52 45 41 54 eate using CREAT
1e93f 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41 42 4c E TEMPORARY TABL
1e940 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 E..**.** If the
1e941 50 33 20 76 61 6c 75 65 20 69 73 20 6e 6f 6e 2d P3 value is non-
1e942 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 74 zero, then the t
1e943 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f able referred to
1e944 20 6d 75 73 74 20 62 65 20 61 6e 0a 2a 2a 20 69 must be an.** i
1e945 6e 74 6b 65 79 20 74 61 62 6c 65 20 28 61 6e 20 ntkey table (an
1e946 53 51 4c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 SQL table, not a
1e947 6e 20 69 6e 64 65 78 29 2e 20 49 6e 20 74 68 69 n index). In thi
1e948 73 20 63 61 73 65 20 74 68 65 20 72 6f 77 20 63 s case the row c
1e949 68 61 6e 67 65 20 0a 2a 2a 20 63 6f 75 6e 74 20 hange .** count
1e94a 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 is incremented b
1e94b 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 y the number of
1e94c 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c rows in the tabl
1e94d 65 20 62 65 69 6e 67 20 63 6c 65 61 72 65 64 2e e being cleared.
1e94e 20 0a 2a 2a 20 49 66 20 50 33 20 69 73 20 67 72 .** If P3 is gr
1e94f 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c eater than zero,
1e950 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 then the value
1e951 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 stored in regist
1e952 65 72 20 50 33 20 69 73 0a 2a 2a 20 61 6c 73 6f er P3 is.** also
1e953 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 incremented by
1e954 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f the number of ro
1e955 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 ws in the table
1e956 62 65 69 6e 67 20 63 6c 65 61 72 65 64 2e 0a 2a being cleared..*
1e957 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 44 *.** See also: D
1e958 65 73 74 72 6f 79 0a 2a 2f 0a 63 61 73 65 20 4f estroy.*/.case O
1e959 50 5f 43 6c 65 61 72 3a 20 7b 0a 23 69 66 20 30 P_Clear: {.#if 0
1e95a 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1e95b 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e95c 75 2e 62 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 u.bs */. int nC
1e95d 68 61 6e 67 65 3b 0a 23 65 6e 64 69 66 20 2f 2a hange;.#endif /*
1e95e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e95f 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 73 moved into u.bs
1e960 20 2a 2f 0a 0a 20 20 75 2e 62 73 2e 6e 43 68 61 */.. u.bs.nCha
1e961 6e 67 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 nge = 0;. asser
1e962 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b t( (p->btreeMask
1e963 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 32 29 29 & (1<<pOp->p2))
1e964 21 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 !=0 );. rc = sq
1e965 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 lite3BtreeClearT
1e966 61 62 6c 65 28 0a 20 20 20 20 20 20 64 62 2d 3e able(. db->
1e967 61 44 62 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42 74 aDb[pOp->p2].pBt
1e968 2c 20 70 4f 70 2d 3e 70 31 2c 20 28 70 4f 70 2d , pOp->p1, (pOp-
1e969 3e 70 33 20 3f 20 26 75 2e 62 73 2e 6e 43 68 61 >p3 ? &u.bs.nCha
1e96a 6e 67 65 20 3a 20 30 29 0a 20 20 29 3b 0a 20 20 nge : 0). );.
1e96b 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 if( pOp->p3 ){.
1e96c 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 2b 3d p->nChange +=
1e96d 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65 3b 0a 20 u.bs.nChange;.
1e96e 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 3e 30 if( pOp->p3>0
1e96f 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 4d 65 ){. p->aMe
1e970 6d 5b 70 4f 70 2d 3e 70 33 5d 2e 75 2e 69 20 2b m[pOp->p3].u.i +
1e971 3d 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65 3b 0a = u.bs.nChange;.
1e972 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 }. }. brea
1e973 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1e974 20 43 72 65 61 74 65 54 61 62 6c 65 20 50 31 20 CreateTable P1
1e975 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 P2 * * *.**.** A
1e976 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 74 61 llocate a new ta
1e977 62 6c 65 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 ble in the main
1e978 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 database file if
1e979 20 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 P1==0 or in the
1e97a 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61 .** auxiliary da
1e97b 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 tabase file if P
1e97c 31 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 1==1 or in an at
1e97d 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20 tached database
1e97e 69 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 if.** P1>1. Wri
1e97f 74 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 te the root page
1e980 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e number of the n
1e981 65 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a ew table into.**
1e982 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 0a register P2.**.
1e983 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 ** The differenc
1e984 65 20 62 65 74 77 65 65 6e 20 61 20 74 61 62 6c e between a tabl
1e985 65 20 61 6e 64 20 61 6e 20 69 6e 64 65 78 20 69 e and an index i
1e986 73 20 74 68 69 73 3a 20 20 41 20 74 61 62 6c 65 s this: A table
1e987 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20 61 20 must.** have a
1e988 34 2d 62 79 74 65 20 69 6e 74 65 67 65 72 20 6b 4-byte integer k
1e989 65 79 20 61 6e 64 20 63 61 6e 20 68 61 76 65 20 ey and can have
1e98a 61 72 62 69 74 72 61 72 79 20 64 61 74 61 2e 20 arbitrary data.
1e98b 20 41 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 61 73 An index.** has
1e98c 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 6b 65 an arbitrary ke
1e98d 79 20 62 75 74 20 6e 6f 20 64 61 74 61 2e 0a 2a y but no data..*
1e98e 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 *.** See also: C
1e98f 72 65 61 74 65 49 6e 64 65 78 0a 2a 2f 0a 2f 2a reateIndex.*/./*
1e990 20 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65 49 Opcode: CreateI
1e991 6e 64 65 78 20 50 31 20 50 32 20 2a 20 2a 20 2a ndex P1 P2 * * *
1e992 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 .**.** Allocate
1e993 61 20 6e 65 77 20 69 6e 64 65 78 20 69 6e 20 74 a new index in t
1e994 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
1e995 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f file if P1==0 o
1e996 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 r in the.** auxi
1e997 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 liary database f
1e998 69 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 20 ile if P1==1 or
1e999 69 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 in an attached d
1e99a 61 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 31 atabase if.** P1
1e99b 3e 31 2e 20 20 57 72 69 74 65 20 74 68 65 20 72 >1. Write the r
1e99c 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 oot page number
1e99d 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 of the new table
1e99e 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 into.** registe
1e99f 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 r P2..**.** See
1e9a0 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e documentation on
1e9a1 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 OP_CreateTable
1e9a2 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 for additional i
1e9a3 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 nformation..*/.c
1e9a4 61 73 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 ase OP_CreateInd
1e9a5 65 78 3a 20 20 20 20 20 20 20 20 20 20 20 20 2f ex: /
1e9a6 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 * out2-prereleas
1e9a7 65 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 72 65 e */.case OP_Cre
1e9a8 61 74 65 54 61 62 6c 65 3a 20 7b 20 20 20 20 20 ateTable: {
1e9a9 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 /* out2-pre
1e9aa 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 release */.#if 0
1e9ab 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1e9ac 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e9ad 75 2e 62 74 20 2a 2f 0a 20 20 69 6e 74 20 70 67 u.bt */. int pg
1e9ae 6e 6f 3b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b no;. int flags;
1e9af 0a 20 20 44 62 20 2a 70 44 62 3b 0a 23 65 6e 64 . Db *pDb;.#end
1e9b0 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1e9b1 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e9b2 20 75 2e 62 74 20 2a 2f 0a 0a 20 20 75 2e 62 74 u.bt */.. u.bt
1e9b3 2e 70 67 6e 6f 20 3d 20 30 3b 0a 20 20 61 73 73 .pgno = 0;. ass
1e9b4 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 ert( pOp->p1>=0
1e9b5 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e && pOp->p1<db->n
1e9b6 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Db );. assert(
1e9b7 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 (p->btreeMask &
1e9b8 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 (1<<pOp->p1))!=0
1e9b9 20 29 3b 0a 20 20 75 2e 62 74 2e 70 44 62 20 3d );. u.bt.pDb =
1e9ba 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 &db->aDb[pOp->p
1e9bb 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 1];. assert( u.
1e9bc 62 74 2e 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 bt.pDb->pBt!=0 )
1e9bd 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 ;. if( pOp->opc
1e9be 6f 64 65 3d 3d 4f 50 5f 43 72 65 61 74 65 54 61 ode==OP_CreateTa
1e9bf 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 75 2e ble ){. /* u.
1e9c0 62 74 2e 66 6c 61 67 73 20 3d 20 42 54 52 45 45 bt.flags = BTREE
1e9c1 5f 49 4e 54 4b 45 59 3b 20 2a 2f 0a 20 20 20 20 _INTKEY; */.
1e9c2 75 2e 62 74 2e 66 6c 61 67 73 20 3d 20 42 54 52 u.bt.flags = BTR
1e9c3 45 45 5f 4c 45 41 46 44 41 54 41 7c 42 54 52 45 EE_LEAFDATA|BTRE
1e9c4 45 5f 49 4e 54 4b 45 59 3b 0a 20 20 7d 65 6c 73 E_INTKEY;. }els
1e9c5 65 7b 0a 20 20 20 20 75 2e 62 74 2e 66 6c 61 67 e{. u.bt.flag
1e9c6 73 20 3d 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 s = BTREE_ZERODA
1e9c7 54 41 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 TA;. }. rc = s
1e9c8 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 qlite3BtreeCreat
1e9c9 65 54 61 62 6c 65 28 75 2e 62 74 2e 70 44 62 2d eTable(u.bt.pDb-
1e9ca 3e 70 42 74 2c 20 26 75 2e 62 74 2e 70 67 6e 6f >pBt, &u.bt.pgno
1e9cb 2c 20 75 2e 62 74 2e 66 6c 61 67 73 29 3b 0a 20 , u.bt.flags);.
1e9cc 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 pOut->u.i = u.b
1e9cd 74 2e 70 67 6e 6f 3b 0a 20 20 4d 65 6d 53 65 74 t.pgno;. MemSet
1e9ce 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d TypeFlag(pOut, M
1e9cf 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b EM_Int);. break
1e9d0 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1e9d1 50 61 72 73 65 53 63 68 65 6d 61 20 50 31 20 50 ParseSchema P1 P
1e9d2 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 2 * P4 *.**.** R
1e9d3 65 61 64 20 61 6e 64 20 70 61 72 73 65 20 61 6c ead and parse al
1e9d4 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 l entries from t
1e9d5 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 he SQLITE_MASTER
1e9d6 20 74 61 62 6c 65 20 6f 66 20 64 61 74 61 62 61 table of databa
1e9d7 73 65 20 50 31 0a 2a 2a 20 74 68 61 74 20 6d 61 se P1.** that ma
1e9d8 74 63 68 20 74 68 65 20 57 48 45 52 45 20 63 6c tch the WHERE cl
1e9d9 61 75 73 65 20 50 34 2e 20 20 50 32 20 69 73 20 ause P4. P2 is
1e9da 74 68 65 20 22 66 6f 72 63 65 22 20 66 6c 61 67 the "force" flag
1e9db 2e 20 20 20 41 6c 77 61 79 73 20 64 6f 0a 2a 2a . Always do.**
1e9dc 20 74 68 65 20 70 61 72 73 69 6e 67 20 69 66 20 the parsing if
1e9dd 50 32 20 69 73 20 74 72 75 65 2e 20 20 49 66 20 P2 is true. If
1e9de 50 32 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 P2 is false, the
1e9df 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 n this routine i
1e9e0 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 20 69 66 20 s a.** no-op if
1e9e1 74 68 65 20 73 63 68 65 6d 61 20 69 73 20 6e 6f the schema is no
1e9e2 74 20 63 75 72 72 65 6e 74 6c 79 20 6c 6f 61 64 t currently load
1e9e3 65 64 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f ed. In other wo
1e9e4 72 64 73 2c 20 69 66 20 50 32 0a 2a 2a 20 69 73 rds, if P2.** is
1e9e5 20 66 61 6c 73 65 2c 20 74 68 65 20 53 51 4c 49 false, the SQLI
1e9e6 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 TE_MASTER table
1e9e7 69 73 20 6f 6e 6c 79 20 70 61 72 73 65 64 20 69 is only parsed i
1e9e8 66 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 f the rest of th
1e9e9 65 0a 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 61 e.** schema is a
1e9ea 6c 72 65 61 64 79 20 6c 6f 61 64 65 64 20 69 6e lready loaded in
1e9eb 74 6f 20 74 68 65 20 73 79 6d 62 6f 6c 20 74 61 to the symbol ta
1e9ec 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ble..**.** This
1e9ed 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 opcode invokes t
1e9ee 68 65 20 70 61 72 73 65 72 20 74 6f 20 63 72 65 he parser to cre
1e9ef 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 ate a new virtua
1e9f0 6c 20 6d 61 63 68 69 6e 65 2c 0a 2a 2a 20 74 68 l machine,.** th
1e9f1 65 6e 20 72 75 6e 73 20 74 68 65 20 6e 65 77 20 en runs the new
1e9f2 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e virtual machine.
1e9f3 20 20 49 74 20 69 73 20 74 68 75 73 20 61 20 72 It is thus a r
1e9f4 65 2d 65 6e 74 72 61 6e 74 20 6f 70 63 6f 64 65 e-entrant opcode
1e9f5 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 72 ..*/.case OP_Par
1e9f6 73 65 53 63 68 65 6d 61 3a 20 7b 0a 23 69 66 20 seSchema: {.#if
1e9f7 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1e9f8 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e9f9 20 75 2e 62 75 20 2a 2f 0a 20 20 69 6e 74 20 69 u.bu */. int i
1e9fa 44 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 Db;. const char
1e9fb 20 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 63 68 61 *zMaster;. cha
1e9fc 72 20 2a 7a 53 71 6c 3b 0a 20 20 49 6e 69 74 44 r *zSql;. InitD
1e9fd 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 23 65 ata initData;.#e
1e9fe 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1e9ff 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1ea00 74 6f 20 75 2e 62 75 20 2a 2f 0a 0a 20 20 75 2e to u.bu */.. u.
1ea01 62 75 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 bu.iDb = pOp->p1
1ea02 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 75 ;. assert( u.bu
1ea03 2e 69 44 62 3e 3d 30 20 26 26 20 75 2e 62 75 2e .iDb>=0 && u.bu.
1ea04 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a iDb<db->nDb );..
1ea05 20 20 2f 2a 20 49 66 20 70 4f 70 2d 3e 70 32 20 /* If pOp->p2
1ea06 69 73 20 30 2c 20 74 68 65 6e 20 74 68 69 73 20 is 0, then this
1ea07 6f 70 63 6f 64 65 20 69 73 20 62 65 69 6e 67 20 opcode is being
1ea08 65 78 65 63 75 74 65 64 20 74 6f 20 72 65 61 64 executed to read
1ea09 20 61 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 20 72 a. ** single r
1ea0a 6f 77 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 20 ow, for example
1ea0b 74 68 65 20 72 6f 77 20 63 6f 72 72 65 73 70 6f the row correspo
1ea0c 6e 64 69 6e 67 20 74 6f 20 61 20 6e 65 77 20 69 nding to a new i
1ea0d 6e 64 65 78 0a 20 20 2a 2a 20 63 72 65 61 74 65 ndex. ** create
1ea0e 64 20 62 79 20 74 68 69 73 20 56 44 42 45 2c 20 d by this VDBE,
1ea0f 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f from the sqlite_
1ea10 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 49 74 master table. It
1ea11 20 6f 6e 6c 79 0a 20 20 2a 2a 20 64 6f 65 73 20 only. ** does
1ea12 74 68 69 73 20 69 66 20 74 68 65 20 63 6f 72 72 this if the corr
1ea13 65 73 70 6f 6e 64 69 6e 67 20 69 6e 2d 6d 65 6d esponding in-mem
1ea14 6f 72 79 20 73 63 68 65 6d 61 20 69 73 20 63 75 ory schema is cu
1ea15 72 72 65 6e 74 6c 79 0a 20 20 2a 2a 20 6c 6f 61 rrently. ** loa
1ea16 64 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ded. Otherwise,
1ea17 74 68 65 20 6e 65 77 20 69 6e 64 65 78 20 64 65 the new index de
1ea18 66 69 6e 69 74 69 6f 6e 20 63 61 6e 20 62 65 20 finition can be
1ea19 6c 6f 61 64 65 64 20 61 6c 6f 6e 67 0a 20 20 2a loaded along. *
1ea1a 2a 20 77 69 74 68 20 74 68 65 20 72 65 73 74 20 * with the rest
1ea1b 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 77 68 of the schema wh
1ea1c 65 6e 20 69 74 20 69 73 20 72 65 71 75 69 72 65 en it is require
1ea1d 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6c 74 d.. **. ** Alt
1ea1e 68 6f 75 67 68 20 74 68 65 20 6d 75 74 65 78 20 hough the mutex
1ea1f 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 20 on the BtShared
1ea20 6f 62 6a 65 63 74 20 74 68 61 74 20 63 6f 72 72 object that corr
1ea21 65 73 70 6f 6e 64 73 20 74 6f 0a 20 20 2a 2a 20 esponds to. **
1ea22 64 61 74 61 62 61 73 65 20 75 2e 62 75 2e 69 44 database u.bu.iD
1ea23 62 20 28 74 68 65 20 64 61 74 61 62 61 73 65 20 b (the database
1ea24 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 73 containing the s
1ea25 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 qlite_master tab
1ea26 6c 65 0a 20 20 2a 2a 20 72 65 61 64 20 62 79 20 le. ** read by
1ea27 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e this instruction
1ea28 29 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 68 ) is currently h
1ea29 65 6c 64 2c 20 69 74 20 69 73 20 6e 65 63 65 73 eld, it is neces
1ea2a 73 61 72 79 20 74 6f 0a 20 20 2a 2a 20 6f 62 74 sary to. ** obt
1ea2b 61 69 6e 20 74 68 65 20 6d 75 74 65 78 65 73 20 ain the mutexes
1ea2c 6f 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 on all attached
1ea2d 64 61 74 61 62 61 73 65 73 20 62 65 66 6f 72 65 databases before
1ea2e 20 63 68 65 63 6b 69 6e 67 20 69 66 0a 20 20 2a checking if. *
1ea2f 2a 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66 20 * the schema of
1ea30 75 2e 62 75 2e 69 44 62 20 69 73 20 6c 6f 61 64 u.bu.iDb is load
1ea31 65 64 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 ed. This is beca
1ea32 75 73 65 2c 20 61 74 20 74 68 65 20 73 74 61 72 use, at the star
1ea33 74 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 73 71 t of. ** the sq
1ea34 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c lite3_exec() cal
1ea35 6c 20 62 65 6c 6f 77 2c 20 53 51 4c 69 74 65 20 l below, SQLite
1ea36 77 69 6c 6c 20 69 6e 76 6f 6b 65 0a 20 20 2a 2a will invoke. **
1ea37 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
1ea38 65 72 41 6c 6c 28 29 2e 20 49 66 20 61 6c 6c 20 erAll(). If all
1ea39 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74 20 mutexes are not
1ea3a 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 74 68 already held, th
1ea3b 65 0a 20 20 2a 2a 20 75 2e 62 75 2e 69 44 62 20 e. ** u.bu.iDb
1ea3c 6d 75 74 65 78 20 6d 61 79 20 62 65 20 74 65 6d mutex may be tem
1ea3d 70 6f 72 61 72 69 6c 79 20 72 65 6c 65 61 73 65 porarily release
1ea3e 64 20 74 6f 20 61 76 6f 69 64 20 64 65 61 64 6c d to avoid deadl
1ea3f 6f 63 6b 2e 20 49 66 0a 20 20 2a 2a 20 74 68 69 ock. If. ** thi
1ea40 73 20 68 61 70 70 65 6e 73 2c 20 74 68 65 6e 20 s happens, then
1ea41 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65 61 some other threa
1ea42 64 20 6d 61 79 20 64 65 6c 65 74 65 20 74 68 65 d may delete the
1ea43 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 in-memory. **
1ea44 73 63 68 65 6d 61 20 6f 66 20 64 61 74 61 62 61 schema of databa
1ea45 73 65 20 75 2e 62 75 2e 69 44 62 20 62 65 66 6f se u.bu.iDb befo
1ea46 72 65 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 re the SQL state
1ea47 6d 65 6e 74 20 72 75 6e 73 2e 20 54 68 65 20 73 ment runs. The s
1ea48 63 68 65 6d 61 0a 20 20 2a 2a 20 77 69 6c 6c 20 chema. ** will
1ea49 6e 6f 74 20 62 65 20 72 65 6c 6f 61 64 65 64 20 not be reloaded
1ea4a 62 65 63 75 61 73 65 20 74 68 65 20 64 62 2d 3e becuase the db->
1ea4b 69 6e 69 74 2e 62 75 73 79 20 66 6c 61 67 20 69 init.busy flag i
1ea4c 73 20 73 65 74 2e 20 54 68 69 73 0a 20 20 2a 2a s set. This. **
1ea4d 20 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 61 can result in a
1ea4e 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 3a "no such table:
1ea4f 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 20 sqlite_master"
1ea50 6f 72 20 22 6d 61 6c 66 6f 72 6d 65 64 0a 20 20 or "malformed.
1ea51 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 65 ** database sche
1ea52 6d 61 22 20 65 72 72 6f 72 20 62 65 69 6e 67 20 ma" error being
1ea53 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 returned to the
1ea54 75 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 user.. */. ass
1ea55 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1ea56 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d 3e eHoldsMutex(db->
1ea57 61 44 62 5b 75 2e 62 75 2e 69 44 62 5d 2e 70 42 aDb[u.bu.iDb].pB
1ea58 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 t) );. sqlite3B
1ea59 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 treeEnterAll(db)
1ea5a 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 ;. if( pOp->p2
1ea5b 7c 7c 20 44 62 48 61 73 50 72 6f 70 65 72 74 79 || DbHasProperty
1ea5c 28 64 62 2c 20 75 2e 62 75 2e 69 44 62 2c 20 44 (db, u.bu.iDb, D
1ea5d 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 B_SchemaLoaded)
1ea5e 29 7b 0a 20 20 20 20 75 2e 62 75 2e 7a 4d 61 73 ){. u.bu.zMas
1ea5f 74 65 72 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 ter = SCHEMA_TAB
1ea60 4c 45 28 75 2e 62 75 2e 69 44 62 29 3b 0a 20 20 LE(u.bu.iDb);.
1ea61 20 20 75 2e 62 75 2e 69 6e 69 74 44 61 74 61 2e u.bu.initData.
1ea62 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 75 2e 62 db = db;. u.b
1ea63 75 2e 69 6e 69 74 44 61 74 61 2e 69 44 62 20 3d u.initData.iDb =
1ea64 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 20 20 75 2e pOp->p1;. u.
1ea65 62 75 2e 69 6e 69 74 44 61 74 61 2e 70 7a 45 72 bu.initData.pzEr
1ea66 72 4d 73 67 20 3d 20 26 70 2d 3e 7a 45 72 72 4d rMsg = &p->zErrM
1ea67 73 67 3b 0a 20 20 20 20 75 2e 62 75 2e 7a 53 71 sg;. u.bu.zSq
1ea68 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e l = sqlite3MPrin
1ea69 74 66 28 64 62 2c 0a 20 20 20 20 20 20 20 22 53 tf(db,. "S
1ea6a 45 4c 45 43 54 20 6e 61 6d 65 2c 20 72 6f 6f 74 ELECT name, root
1ea6b 70 61 67 65 2c 20 73 71 6c 20 46 52 4f 4d 20 27 page, sql FROM '
1ea6c 25 71 27 2e 25 73 20 57 48 45 52 45 20 25 73 22 %q'.%s WHERE %s"
1ea6d 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 ,. db->aDb
1ea6e 5b 75 2e 62 75 2e 69 44 62 5d 2e 7a 4e 61 6d 65 [u.bu.iDb].zName
1ea6f 2c 20 75 2e 62 75 2e 7a 4d 61 73 74 65 72 2c 20 , u.bu.zMaster,
1ea70 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 pOp->p4.z);.
1ea71 69 66 28 20 75 2e 62 75 2e 7a 53 71 6c 3d 3d 30 if( u.bu.zSql==0
1ea72 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
1ea73 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
1ea74 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 76 }else{. (v
1ea75 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 oid)sqlite3Safet
1ea76 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 yOff(db);.
1ea77 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 assert( db->init
1ea78 2e 62 75 73 79 3d 3d 30 20 29 3b 0a 20 20 20 20 .busy==0 );.
1ea79 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 db->init.busy
1ea7a 3d 20 31 3b 0a 20 20 20 20 20 20 75 2e 62 75 2e = 1;. u.bu.
1ea7b 69 6e 69 74 44 61 74 61 2e 72 63 20 3d 20 53 51 initData.rc = SQ
1ea7c 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 61 LITE_OK;. a
1ea7d 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c ssert( !db->mall
1ea7e 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 ocFailed );.
1ea7f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 rc = sqlite3_e
1ea80 78 65 63 28 64 62 2c 20 75 2e 62 75 2e 7a 53 71 xec(db, u.bu.zSq
1ea81 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 l, sqlite3InitCa
1ea82 6c 6c 62 61 63 6b 2c 20 26 75 2e 62 75 2e 69 6e llback, &u.bu.in
1ea83 69 74 44 61 74 61 2c 20 30 29 3b 0a 20 20 20 20 itData, 0);.
1ea84 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1ea85 5f 4f 4b 20 29 20 72 63 20 3d 20 75 2e 62 75 2e _OK ) rc = u.bu.
1ea86 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20 20 initData.rc;.
1ea87 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1ea88 28 64 62 2c 20 75 2e 62 75 2e 7a 53 71 6c 29 3b (db, u.bu.zSql);
1ea89 0a 20 20 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e . db->init.
1ea8a 62 75 73 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 busy = 0;.
1ea8b 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 (void)sqlite3Saf
1ea8c 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 7d etyOn(db);. }
1ea8d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 . }. sqlite3Bt
1ea8e 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b reeLeaveAll(db);
1ea8f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1ea90 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 67 E_NOMEM ){. g
1ea91 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a oto no_mem;. }.
1ea92 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 20 break;.}..#if
1ea93 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
1ea94 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 0a 2f 2a OMIT_ANALYZE)./*
1ea95 20 4f 70 63 6f 64 65 3a 20 4c 6f 61 64 41 6e 61 Opcode: LoadAna
1ea96 6c 79 73 69 73 20 50 31 20 2a 20 2a 20 2a 20 2a lysis P1 * * * *
1ea97 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 .**.** Read the
1ea98 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 sqlite_stat1 tab
1ea99 6c 65 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 le for database
1ea9a 50 31 20 61 6e 64 20 6c 6f 61 64 20 74 68 65 20 P1 and load the
1ea9b 63 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 content.** of th
1ea9c 61 74 20 74 61 62 6c 65 20 69 6e 74 6f 20 74 68 at table into th
1ea9d 65 20 69 6e 74 65 72 6e 61 6c 20 69 6e 64 65 78 e internal index
1ea9e 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 hash table. Th
1ea9f 69 73 20 77 69 6c 6c 20 63 61 75 73 65 0a 2a 2a is will cause.**
1eaa0 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 74 6f the analysis to
1eaa1 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 70 72 be used when pr
1eaa2 65 70 61 72 69 6e 67 20 61 6c 6c 20 73 75 62 73 eparing all subs
1eaa3 65 71 75 65 6e 74 20 71 75 65 72 69 65 73 2e 0a equent queries..
1eaa4 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 6f 61 64 41 */.case OP_LoadA
1eaa5 6e 61 6c 79 73 69 73 3a 20 7b 0a 20 20 61 73 73 nalysis: {. ass
1eaa6 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 ert( pOp->p1>=0
1eaa7 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e && pOp->p1<db->n
1eaa8 44 62 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c Db );. rc = sql
1eaa9 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 ite3AnalysisLoad
1eaaa 28 64 62 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 (db, pOp->p1);.
1eaab 20 62 72 65 61 6b 3b 20 20 0a 7d 0a 23 65 6e 64 break; .}.#end
1eaac 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 if /* !defined(S
1eaad 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 QLITE_OMIT_ANALY
1eaae 5a 45 29 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 ZE) */../* Opcod
1eaaf 65 3a 20 44 72 6f 70 54 61 62 6c 65 20 50 31 20 e: DropTable P1
1eab0 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 * * P4 *.**.** R
1eab1 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e emove the intern
1eab2 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 al (in-memory) d
1eab3 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74 ata structures t
1eab4 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 hat describe.**
1eab5 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 the table named
1eab6 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 P4 in database P
1eab7 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c 1. This is call
1eab8 65 64 20 61 66 74 65 72 20 61 20 74 61 62 6c 65 ed after a table
1eab9 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69 .** is dropped i
1eaba 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 n order to keep
1eabb 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 the internal rep
1eabc 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 resentation of t
1eabd 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e he.** schema con
1eabe 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61 sistent with wha
1eabf 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f t is on disk..*/
1eac0 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 61 62 .case OP_DropTab
1eac1 6c 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 55 le: {. sqlite3U
1eac2 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 nlinkAndDeleteTa
1eac3 62 6c 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c ble(db, pOp->p1,
1eac4 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 pOp->p4.z);. b
1eac5 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1eac6 64 65 3a 20 44 72 6f 70 49 6e 64 65 78 20 50 31 de: DropIndex P1
1eac7 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 * * P4 *.**.**
1eac8 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 Remove the inter
1eac9 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 nal (in-memory)
1eaca 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 data structures
1eacb 74 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a that describe.**
1eacc 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 64 the index named
1eacd 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 P4 in database
1eace 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c P1. This is cal
1eacf 6c 65 64 20 61 66 74 65 72 20 61 6e 20 69 6e 64 led after an ind
1ead0 65 78 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 ex.** is dropped
1ead1 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 in order to kee
1ead2 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 p the internal r
1ead3 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 epresentation of
1ead4 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 the.** schema c
1ead5 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 onsistent with w
1ead6 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a hat is on disk..
1ead7 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 49 */.case OP_DropI
1ead8 6e 64 65 78 3a 20 7b 0a 20 20 73 71 6c 69 74 65 ndex: {. sqlite
1ead9 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 3UnlinkAndDelete
1eada 49 6e 64 65 78 28 64 62 2c 20 70 4f 70 2d 3e 70 Index(db, pOp->p
1eadb 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 1, pOp->p4.z);.
1eadc 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1eadd 63 6f 64 65 3a 20 44 72 6f 70 54 72 69 67 67 65 code: DropTrigge
1eade 72 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a r P1 * * P4 *.**
1eadf 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 69 .** Remove the i
1eae0 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f nternal (in-memo
1eae1 72 79 29 20 64 61 74 61 20 73 74 72 75 63 74 75 ry) data structu
1eae2 72 65 73 20 74 68 61 74 20 64 65 73 63 72 69 62 res that describ
1eae3 65 0a 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72 e.** the trigger
1eae4 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74 named P4 in dat
1eae5 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 20 abase P1. This
1eae6 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 is called after
1eae7 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 73 20 a trigger.** is
1eae8 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 dropped in order
1eae9 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 to keep the int
1eaea 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 ernal representa
1eaeb 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 tion of the.** s
1eaec 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 chema consistent
1eaed 20 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e with what is on
1eaee 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f disk..*/.case O
1eaef 50 5f 44 72 6f 70 54 72 69 67 67 65 72 3a 20 7b P_DropTrigger: {
1eaf0 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b . sqlite3Unlink
1eaf1 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72 AndDeleteTrigger
1eaf2 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f (db, pOp->p1, pO
1eaf3 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 p->p4.z);. brea
1eaf4 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 k;.}...#ifndef S
1eaf5 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 QLITE_OMIT_INTEG
1eaf6 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 20 4f 70 RITY_CHECK./* Op
1eaf7 63 6f 64 65 3a 20 49 6e 74 65 67 72 69 74 79 43 code: IntegrityC
1eaf8 6b 20 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a k P1 P2 P3 * P5.
1eaf9 2a 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 6e 61 6c **.** Do an anal
1eafa 79 73 69 73 20 6f 66 20 74 68 65 20 63 75 72 72 ysis of the curr
1eafb 65 6e 74 6c 79 20 6f 70 65 6e 20 64 61 74 61 62 ently open datab
1eafc 61 73 65 2e 20 20 53 74 6f 72 65 20 69 6e 0a 2a ase. Store in.*
1eafd 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 74 68 * register P1 th
1eafe 65 20 74 65 78 74 20 6f 66 20 61 6e 20 65 72 72 e text of an err
1eaff 6f 72 20 6d 65 73 73 61 67 65 20 64 65 73 63 72 or message descr
1eb00 69 62 69 6e 67 20 61 6e 79 20 70 72 6f 62 6c 65 ibing any proble
1eb01 6d 73 2e 0a 2a 2a 20 49 66 20 6e 6f 20 70 72 6f ms..** If no pro
1eb02 62 6c 65 6d 73 20 61 72 65 20 66 6f 75 6e 64 2c blems are found,
1eb03 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e store a NULL in
1eb04 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a register P1..**
1eb05 0a 2a 2a 20 54 68 65 20 72 65 67 69 73 74 65 72 .** The register
1eb06 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 P3 contains the
1eb07 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 maximum number
1eb08 6f 66 20 61 6c 6c 6f 77 65 64 20 65 72 72 6f 72 of allowed error
1eb09 73 2e 0a 2a 2a 20 41 74 20 6d 6f 73 74 20 72 65 s..** At most re
1eb0a 67 28 50 33 29 20 65 72 72 6f 72 73 20 77 69 6c g(P3) errors wil
1eb0b 6c 20 62 65 20 72 65 70 6f 72 74 65 64 2e 0a 2a l be reported..*
1eb0c 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 * In other words
1eb0d 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 73 , the analysis s
1eb0e 74 6f 70 73 20 61 73 20 73 6f 6f 6e 20 61 73 20 tops as soon as
1eb0f 72 65 67 28 50 31 29 20 65 72 72 6f 72 73 20 61 reg(P1) errors a
1eb10 72 65 20 0a 2a 2a 20 73 65 65 6e 2e 20 20 52 65 re .** seen. Re
1eb11 67 28 50 31 29 20 69 73 20 75 70 64 61 74 65 64 g(P1) is updated
1eb12 20 77 69 74 68 20 74 68 65 20 6e 75 6d 62 65 72 with the number
1eb13 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61 69 of errors remai
1eb14 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ning..**.** The
1eb15 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 root page number
1eb16 73 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 s of all tables
1eb17 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
1eb18 61 72 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 73 are integer.** s
1eb19 74 6f 72 65 64 20 69 6e 20 72 65 67 28 50 31 29 tored in reg(P1)
1eb1a 2c 20 72 65 67 28 50 31 2b 31 29 2c 20 72 65 67 , reg(P1+1), reg
1eb1b 28 50 31 2b 32 29 2c 20 2e 2e 2e 2e 20 20 54 68 (P1+2), .... Th
1eb1c 65 72 65 20 61 72 65 20 50 32 20 74 61 62 6c 65 ere are P2 table
1eb1d 73 0a 2a 2a 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a s.** total..**.*
1eb1e 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 74 20 7a * If P5 is not z
1eb1f 65 72 6f 2c 20 74 68 65 20 63 68 65 63 6b 20 69 ero, the check i
1eb20 73 20 64 6f 6e 65 20 6f 6e 20 74 68 65 20 61 75 s done on the au
1eb21 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 xiliary database
1eb22 0a 2a 2a 20 66 69 6c 65 2c 20 6e 6f 74 20 74 68 .** file, not th
1eb23 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
1eb24 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 file..**.** This
1eb25 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 opcode is used
1eb26 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 to implement the
1eb27 20 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b integrity_check
1eb28 20 70 72 61 67 6d 61 2e 0a 2a 2f 0a 63 61 73 65 pragma..*/.case
1eb29 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 3a OP_IntegrityCk:
1eb2a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1eb2b 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1eb2c 65 64 20 69 6e 74 6f 20 75 2e 62 76 20 2a 2f 0a ed into u.bv */.
1eb2d 20 20 69 6e 74 20 6e 52 6f 6f 74 3b 20 20 20 20 int nRoot;
1eb2e 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 /* Number of t
1eb2f 61 62 6c 65 73 20 74 6f 20 63 68 65 63 6b 2e 20 ables to check.
1eb30 20 28 4e 75 6d 62 65 72 20 6f 66 20 72 6f 6f 74 (Number of root
1eb31 20 70 61 67 65 73 2e 29 20 2a 2f 0a 20 20 69 6e pages.) */. in
1eb32 74 20 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f 2a t *aRoot; /*
1eb33 20 41 72 72 61 79 20 6f 66 20 72 6f 6f 74 70 61 Array of rootpa
1eb34 67 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 74 ge numbers for t
1eb35 61 62 6c 65 73 20 74 6f 20 62 65 20 63 68 65 63 ables to be chec
1eb36 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 ked */. int j;
1eb37 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
1eb38 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e counter */. in
1eb39 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 2f 2a t nErr; /*
1eb3a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 Number of error
1eb3b 73 20 72 65 70 6f 72 74 65 64 20 2a 2f 0a 20 20 s reported */.
1eb3c 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 char *z;
1eb3d 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 65 /* Text of the e
1eb3e 72 72 6f 72 20 72 65 70 6f 72 74 20 2a 2f 0a 20 rror report */.
1eb3f 20 4d 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20 20 Mem *pnErr;
1eb40 20 2f 2a 20 52 65 67 69 73 74 65 72 20 6b 65 65 /* Register kee
1eb41 70 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 65 72 ping track of er
1eb42 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 20 2a rors remaining *
1eb43 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 /.#endif /* loca
1eb44 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1eb45 64 20 69 6e 74 6f 20 75 2e 62 76 20 2a 2f 0a 0a d into u.bv */..
1eb46 20 20 75 2e 62 76 2e 6e 52 6f 6f 74 20 3d 20 70 u.bv.nRoot = p
1eb47 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 Op->p2;. assert
1eb48 28 20 75 2e 62 76 2e 6e 52 6f 6f 74 3e 30 20 29 ( u.bv.nRoot>0 )
1eb49 3b 0a 20 20 75 2e 62 76 2e 61 52 6f 6f 74 20 3d ;. u.bv.aRoot =
1eb4a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
1eb4b 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 Raw(db, sizeof(i
1eb4c 6e 74 29 2a 28 75 2e 62 76 2e 6e 52 6f 6f 74 2b nt)*(u.bv.nRoot+
1eb4d 31 29 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 76 1) );. if( u.bv
1eb4e 2e 61 52 6f 6f 74 3d 3d 30 20 29 20 67 6f 74 6f .aRoot==0 ) goto
1eb4f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 61 73 73 65 72 no_mem;. asser
1eb50 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 t( pOp->p3>0 &&
1eb51 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p3<=p->nMem
1eb52 20 29 3b 0a 20 20 75 2e 62 76 2e 70 6e 45 72 72 );. u.bv.pnErr
1eb53 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1eb54 3e 70 33 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 >p3];. assert(
1eb55 28 75 2e 62 76 2e 70 6e 45 72 72 2d 3e 66 6c 61 (u.bv.pnErr->fla
1eb56 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 gs & MEM_Int)!=0
1eb57 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 75 );. assert( (u
1eb58 2e 62 76 2e 70 6e 45 72 72 2d 3e 66 6c 61 67 73 .bv.pnErr->flags
1eb59 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f & (MEM_Str|MEM_
1eb5a 42 6c 6f 62 29 29 3d 3d 30 20 29 3b 0a 20 20 70 Blob))==0 );. p
1eb5b 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 In1 = &p->aMem[p
1eb5c 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 75 Op->p1];. for(u
1eb5d 2e 62 76 2e 6a 3d 30 3b 20 75 2e 62 76 2e 6a 3c .bv.j=0; u.bv.j<
1eb5e 75 2e 62 76 2e 6e 52 6f 6f 74 3b 20 75 2e 62 76 u.bv.nRoot; u.bv
1eb5f 2e 6a 2b 2b 29 7b 0a 20 20 20 20 75 2e 62 76 2e .j++){. u.bv.
1eb60 61 52 6f 6f 74 5b 75 2e 62 76 2e 6a 5d 20 3d 20 aRoot[u.bv.j] =
1eb61 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62 65 (int)sqlite3Vdbe
1eb62 49 6e 74 56 61 6c 75 65 28 26 70 49 6e 31 5b 75 IntValue(&pIn1[u
1eb63 2e 62 76 2e 6a 5d 29 3b 0a 20 20 7d 0a 20 20 75 .bv.j]);. }. u
1eb64 2e 62 76 2e 61 52 6f 6f 74 5b 75 2e 62 76 2e 6a .bv.aRoot[u.bv.j
1eb65 5d 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 ] = 0;. assert(
1eb66 20 70 4f 70 2d 3e 70 35 3c 64 62 2d 3e 6e 44 62 pOp->p5<db->nDb
1eb67 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 );. assert( (p
1eb68 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 ->btreeMask & (1
1eb69 3c 3c 70 4f 70 2d 3e 70 35 29 29 21 3d 30 20 29 <<pOp->p5))!=0 )
1eb6a 3b 0a 20 20 75 2e 62 76 2e 7a 20 3d 20 73 71 6c ;. u.bv.z = sql
1eb6b 69 74 65 33 42 74 72 65 65 49 6e 74 65 67 72 69 ite3BtreeIntegri
1eb6c 74 79 43 68 65 63 6b 28 64 62 2d 3e 61 44 62 5b tyCheck(db->aDb[
1eb6d 70 4f 70 2d 3e 70 35 5d 2e 70 42 74 2c 20 75 2e pOp->p5].pBt, u.
1eb6e 62 76 2e 61 52 6f 6f 74 2c 20 75 2e 62 76 2e 6e bv.aRoot, u.bv.n
1eb6f 52 6f 6f 74 2c 0a 20 20 20 20 20 20 20 20 20 20 Root,.
1eb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1eb71 20 20 20 20 20 20 20 28 69 6e 74 29 75 2e 62 76 (int)u.bv
1eb72 2e 70 6e 45 72 72 2d 3e 75 2e 69 2c 20 26 75 2e .pnErr->u.i, &u.
1eb73 62 76 2e 6e 45 72 72 29 3b 0a 20 20 73 71 6c 69 bv.nErr);. sqli
1eb74 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e te3DbFree(db, u.
1eb75 62 76 2e 61 52 6f 6f 74 29 3b 0a 20 20 75 2e 62 bv.aRoot);. u.b
1eb76 76 2e 70 6e 45 72 72 2d 3e 75 2e 69 20 2d 3d 20 v.pnErr->u.i -=
1eb77 75 2e 62 76 2e 6e 45 72 72 3b 0a 20 20 73 71 6c u.bv.nErr;. sql
1eb78 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 ite3VdbeMemSetNu
1eb79 6c 6c 28 70 49 6e 31 29 3b 0a 20 20 69 66 28 20 ll(pIn1);. if(
1eb7a 75 2e 62 76 2e 6e 45 72 72 3d 3d 30 20 29 7b 0a u.bv.nErr==0 ){.
1eb7b 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 76 assert( u.bv
1eb7c 2e 7a 3d 3d 30 20 29 3b 0a 20 20 7d 65 6c 73 65 .z==0 );. }else
1eb7d 20 69 66 28 20 75 2e 62 76 2e 7a 3d 3d 30 20 29 if( u.bv.z==0 )
1eb7e 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 {. goto no_me
1eb7f 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 m;. }else{.
1eb80 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1eb81 74 53 74 72 28 70 49 6e 31 2c 20 75 2e 62 76 2e tStr(pIn1, u.bv.
1eb82 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 z, -1, SQLITE_UT
1eb83 46 38 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 F8, sqlite3_free
1eb84 29 3b 0a 20 20 7d 0a 20 20 55 50 44 41 54 45 5f );. }. UPDATE_
1eb85 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 49 6e MAX_BLOBSIZE(pIn
1eb86 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 1);. sqlite3Vdb
1eb87 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 eChangeEncoding(
1eb88 70 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b pIn1, encoding);
1eb89 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 . break;.}.#end
1eb8a 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1eb8b 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 T_INTEGRITY_CHEC
1eb8c 4b 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a K */../* Opcode:
1eb8d 20 52 6f 77 53 65 74 41 64 64 20 50 31 20 50 32 RowSetAdd P1 P2
1eb8e 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 73 * * *.**.** Ins
1eb8f 65 72 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 ert the integer
1eb90 76 61 6c 75 65 20 68 65 6c 64 20 62 79 20 72 65 value held by re
1eb91 67 69 73 74 65 72 20 50 32 20 69 6e 74 6f 20 61 gister P2 into a
1eb92 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 0a 2a boolean index.*
1eb93 2a 20 68 65 6c 64 20 69 6e 20 72 65 67 69 73 74 * held in regist
1eb94 65 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 er P1..**.** An
1eb95 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c 73 20 assertion fails
1eb96 69 66 20 50 32 20 69 73 20 6e 6f 74 20 61 6e 20 if P2 is not an
1eb97 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63 61 73 65 integer..*/.case
1eb98 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 3a 20 7b OP_RowSetAdd: {
1eb99 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f /* in2 */
1eb9a 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1eb9b 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1eb9c 20 69 6e 74 6f 20 75 2e 62 77 20 2a 2f 0a 20 20 into u.bw */.
1eb9d 4d 65 6d 20 2a 70 49 64 78 3b 0a 20 20 4d 65 6d Mem *pIdx;. Mem
1eb9e 20 2a 70 56 61 6c 3b 0a 23 65 6e 64 69 66 20 2f *pVal;.#endif /
1eb9f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1eba0 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1eba1 77 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 w */. assert( p
1eba2 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d Op->p1>0 && pOp-
1eba3 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p1<=p->nMem );.
1eba4 20 20 75 2e 62 77 2e 70 49 64 78 20 3d 20 26 70 u.bw.pIdx = &p
1eba5 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b ->aMem[pOp->p1];
1eba6 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1eba7 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3c p2>0 && pOp->p2<
1eba8 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e =p->nMem );. u.
1eba9 62 77 2e 70 56 61 6c 20 3d 20 26 70 2d 3e 61 4d bw.pVal = &p->aM
1ebaa 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 em[pOp->p2];. a
1ebab 73 73 65 72 74 28 20 28 75 2e 62 77 2e 70 56 61 ssert( (u.bw.pVa
1ebac 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 l->flags & MEM_I
1ebad 6e 74 29 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 nt)!=0 );. if(
1ebae 28 75 2e 62 77 2e 70 49 64 78 2d 3e 66 6c 61 67 (u.bw.pIdx->flag
1ebaf 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d s & MEM_RowSet)=
1ebb0 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
1ebb1 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 3VdbeMemSetRowSe
1ebb2 74 28 75 2e 62 77 2e 70 49 64 78 29 3b 0a 20 20 t(u.bw.pIdx);.
1ebb3 20 20 69 66 28 20 28 75 2e 62 77 2e 70 49 64 78 if( (u.bw.pIdx
1ebb4 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f ->flags & MEM_Ro
1ebb5 77 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20 wSet)==0 ) goto
1ebb6 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 73 71 no_mem;. }. sq
1ebb7 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 lite3RowSetInser
1ebb8 74 28 75 2e 62 77 2e 70 49 64 78 2d 3e 75 2e 70 t(u.bw.pIdx->u.p
1ebb9 52 6f 77 53 65 74 2c 20 75 2e 62 77 2e 70 56 61 RowSet, u.bw.pVa
1ebba 6c 2d 3e 75 2e 69 29 3b 0a 20 20 62 72 65 61 6b l->u.i);. break
1ebbb 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1ebbc 52 6f 77 53 65 74 52 65 61 64 20 50 31 20 50 32 RowSetRead P1 P2
1ebbd 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 P3 * *.**.** Ex
1ebbe 74 72 61 63 74 20 74 68 65 20 73 6d 61 6c 6c 65 tract the smalle
1ebbf 73 74 20 76 61 6c 75 65 20 66 72 6f 6d 20 62 6f st value from bo
1ebc0 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20 61 olean index P1 a
1ebc1 6e 64 20 70 75 74 20 74 68 61 74 20 76 61 6c 75 nd put that valu
1ebc2 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 e into.** regist
1ebc3 65 72 20 50 33 2e 20 20 4f 72 2c 20 69 66 20 62 er P3. Or, if b
1ebc4 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20 oolean index P1
1ebc5 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 65 6d 70 is initially emp
1ebc6 74 79 2c 20 6c 65 61 76 65 20 50 33 0a 2a 2a 20 ty, leave P3.**
1ebc7 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 6a 75 unchanged and ju
1ebc8 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f mp to instructio
1ebc9 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 n P2..*/.case OP
1ebca 5f 52 6f 77 53 65 74 52 65 61 64 3a 20 7b 20 20 _RowSetRead: {
1ebcb 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 6f 75 /* jump, ou
1ebcc 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 t3 */.#if 0 /*
1ebcd 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1ebce 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 78 20 moved into u.bx
1ebcf 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 64 78 3b 0a */. Mem *pIdx;.
1ebd0 20 20 69 36 34 20 76 61 6c 3b 0a 23 65 6e 64 69 i64 val;.#endi
1ebd1 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1ebd2 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1ebd3 75 2e 62 78 20 2a 2f 0a 20 20 61 73 73 65 72 74 u.bx */. assert
1ebd4 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 ( pOp->p1>0 && p
1ebd5 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p1<=p->nMem
1ebd6 29 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 );. CHECK_FOR_I
1ebd7 4e 54 45 52 52 55 50 54 3b 0a 20 20 75 2e 62 78 NTERRUPT;. u.bx
1ebd8 2e 70 49 64 78 20 3d 20 26 70 2d 3e 61 4d 65 6d .pIdx = &p->aMem
1ebd9 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 4f 75 [pOp->p1];. pOu
1ebda 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 t = &p->aMem[pOp
1ebdb 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 28 75 2e ->p3];. if( (u.
1ebdc 62 78 2e 70 49 64 78 2d 3e 66 6c 61 67 73 20 26 bx.pIdx->flags &
1ebdd 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 0a MEM_RowSet)==0.
1ebde 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 52 6f 77 || sqlite3Row
1ebdf 53 65 74 4e 65 78 74 28 75 2e 62 78 2e 70 49 64 SetNext(u.bx.pId
1ebe0 78 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 26 75 x->u.pRowSet, &u
1ebe1 2e 62 78 2e 76 61 6c 29 3d 3d 30 0a 20 20 29 7b .bx.val)==0. ){
1ebe2 0a 20 20 20 20 2f 2a 20 54 68 65 20 62 6f 6f 6c . /* The bool
1ebe3 65 61 6e 20 69 6e 64 65 78 20 69 73 20 65 6d 70 ean index is emp
1ebe4 74 79 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 ty */. sqlite
1ebe5 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 3VdbeMemSetNull(
1ebe6 75 2e 62 78 2e 70 49 64 78 29 3b 0a 20 20 20 20 u.bx.pIdx);.
1ebe7 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1ebe8 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
1ebe9 2a 20 41 20 76 61 6c 75 65 20 77 61 73 20 70 75 * A value was pu
1ebea 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 69 6e lled from the in
1ebeb 64 65 78 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 dex */. asser
1ebec 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 t( pOp->p3>0 &&
1ebed 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p3<=p->nMem
1ebee 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 );. sqlite3V
1ebef 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 dbeMemSetInt64(p
1ebf0 4f 75 74 2c 20 75 2e 62 78 2e 76 61 6c 29 3b 0a Out, u.bx.val);.
1ebf1 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1ebf2 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 /* Opcode: RowSe
1ebf3 74 54 65 73 74 20 50 31 20 50 32 20 50 33 20 50 tTest P1 P2 P3 P
1ebf4 34 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 4.**.** Register
1ebf5 20 50 33 20 69 73 20 61 73 73 75 6d 65 64 20 74 P3 is assumed t
1ebf6 6f 20 68 6f 6c 64 20 61 20 36 34 2d 62 69 74 20 o hold a 64-bit
1ebf7 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 20 49 integer value. I
1ebf8 66 20 72 65 67 69 73 74 65 72 20 50 31 0a 2a 2a f register P1.**
1ebf9 20 63 6f 6e 74 61 69 6e 73 20 61 20 52 6f 77 53 contains a RowS
1ebfa 65 74 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 et object and th
1ebfb 61 74 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 at RowSet object
1ebfc 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 contains.** the
1ebfd 20 76 61 6c 75 65 20 68 65 6c 64 20 69 6e 20 50 value held in P
1ebfe 33 2c 20 6a 75 6d 70 20 74 6f 20 72 65 67 69 73 3, jump to regis
1ebff 74 65 72 20 50 32 2e 20 4f 74 68 65 72 77 69 73 ter P2. Otherwis
1ec00 65 2c 20 69 6e 73 65 72 74 20 74 68 65 0a 2a 2a e, insert the.**
1ec01 20 69 6e 74 65 67 65 72 20 69 6e 20 50 33 20 69 integer in P3 i
1ec02 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 74 20 61 nto the RowSet a
1ec03 6e 64 20 63 6f 6e 74 69 6e 75 65 20 6f 6e 20 74 nd continue on t
1ec04 6f 20 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6f 70 o the.** next op
1ec05 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 code..**.** The
1ec06 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 69 73 RowSet object is
1ec07 20 6f 70 74 69 6d 69 7a 65 64 20 66 6f 72 20 74 optimized for t
1ec08 68 65 20 63 61 73 65 20 77 68 65 72 65 20 73 75 he case where su
1ec09 63 63 65 73 73 69 76 65 20 73 65 74 73 0a 2a 2a ccessive sets.**
1ec0a 20 6f 66 20 69 6e 74 65 67 65 72 73 2c 20 77 68 of integers, wh
1ec0b 65 72 65 20 65 61 63 68 20 73 65 74 20 63 6f 6e ere each set con
1ec0c 74 61 69 6e 73 20 6e 6f 20 64 75 70 6c 69 63 61 tains no duplica
1ec0d 74 65 73 2e 20 45 61 63 68 20 73 65 74 0a 2a 2a tes. Each set.**
1ec0e 20 6f 66 20 76 61 6c 75 65 73 20 69 73 20 69 64 of values is id
1ec0f 65 6e 74 69 66 69 65 64 20 62 79 20 61 20 75 6e entified by a un
1ec10 69 71 75 65 20 50 34 20 76 61 6c 75 65 2e 20 54 ique P4 value. T
1ec11 68 65 20 66 69 72 73 74 20 73 65 74 0a 2a 2a 20 he first set.**
1ec12 6d 75 73 74 20 68 61 76 65 20 50 34 3d 3d 30 2c must have P4==0,
1ec13 20 74 68 65 20 66 69 6e 61 6c 20 73 65 74 20 50 the final set P
1ec14 34 3d 2d 31 2e 20 20 50 34 20 6d 75 73 74 20 62 4=-1. P4 must b
1ec15 65 20 65 69 74 68 65 72 20 2d 31 20 6f 72 0a 2a e either -1 or.*
1ec16 2a 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 * non-negative.
1ec17 20 46 6f 72 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 For non-negativ
1ec18 65 20 76 61 6c 75 65 73 20 6f 66 20 50 34 20 6f e values of P4 o
1ec19 6e 6c 79 20 74 68 65 20 6c 6f 77 65 72 20 34 0a nly the lower 4.
1ec1a 2a 2a 20 62 69 74 73 20 61 72 65 20 73 69 67 6e ** bits are sign
1ec1b 69 66 69 63 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 ificant..**.** T
1ec1c 68 69 73 20 61 6c 6c 6f 77 73 20 6f 70 74 69 6d his allows optim
1ec1d 69 7a 61 74 69 6f 6e 73 3a 20 28 61 29 20 77 68 izations: (a) wh
1ec1e 65 6e 20 50 34 3d 3d 30 20 74 68 65 72 65 20 69 en P4==0 there i
1ec1f 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 74 65 73 s no need to tes
1ec20 74 0a 2a 2a 20 74 68 65 20 72 6f 77 73 65 74 20 t.** the rowset
1ec21 6f 62 6a 65 63 74 20 66 6f 72 20 50 33 2c 20 61 object for P3, a
1ec22 73 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 s it is guarante
1ec23 65 64 20 6e 6f 74 20 74 6f 20 63 6f 6e 74 61 69 ed not to contai
1ec24 6e 20 69 74 2c 0a 2a 2a 20 28 62 29 20 77 68 65 n it,.** (b) whe
1ec25 6e 20 50 34 3d 3d 2d 31 20 74 68 65 72 65 20 69 n P4==-1 there i
1ec26 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 69 6e 73 s no need to ins
1ec27 65 72 74 20 74 68 65 20 76 61 6c 75 65 2c 20 61 ert the value, a
1ec28 73 20 69 74 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 s it will.** nev
1ec29 65 72 20 62 65 20 74 65 73 74 65 64 20 66 6f 72 er be tested for
1ec2a 2c 20 61 6e 64 20 28 63 29 20 77 68 65 6e 20 61 , and (c) when a
1ec2b 20 76 61 6c 75 65 20 74 68 61 74 20 69 73 20 70 value that is p
1ec2c 61 72 74 20 6f 66 20 73 65 74 20 58 20 69 73 0a art of set X is.
1ec2d 2a 2a 20 69 6e 73 65 72 74 65 64 2c 20 74 68 65 ** inserted, the
1ec2e 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f re is no need to
1ec2f 20 73 65 61 72 63 68 20 74 6f 20 73 65 65 20 69 search to see i
1ec30 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 f the same value
1ec31 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 was.** previous
1ec32 6c 79 20 69 6e 73 65 72 74 65 64 20 61 73 20 70 ly inserted as p
1ec33 61 72 74 20 6f 66 20 73 65 74 20 58 20 28 6f 6e art of set X (on
1ec34 6c 79 20 69 66 20 69 74 20 77 61 73 20 70 72 65 ly if it was pre
1ec35 76 69 6f 75 73 6c 79 0a 2a 2a 20 69 6e 73 65 72 viously.** inser
1ec36 74 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 73 ted as part of s
1ec37 6f 6d 65 20 6f 74 68 65 72 20 73 65 74 29 2e 0a ome other set)..
1ec38 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 */.case OP_RowSe
1ec39 74 54 65 73 74 3a 20 7b 20 20 20 20 20 20 20 20 tTest: {
1ec3a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1ec3b 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a jump, in1, in3 *
1ec3c 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 /.#if 0 /* loca
1ec3d 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1ec3e 64 20 69 6e 74 6f 20 75 2e 62 79 20 2a 2f 0a 20 d into u.by */.
1ec3f 20 69 6e 74 20 69 53 65 74 3b 0a 20 20 69 6e 74 int iSet;. int
1ec40 20 65 78 69 73 74 73 3b 0a 23 65 6e 64 69 66 20 exists;.#endif
1ec41 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1ec42 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1ec43 62 79 20 2a 2f 0a 0a 20 20 75 2e 62 79 2e 69 53 by */.. u.by.iS
1ec44 65 74 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a et = pOp->p4.i;.
1ec45 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e assert( pIn3->
1ec46 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b flags&MEM_Int );
1ec47 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 .. /* If there
1ec48 69 73 20 61 6e 79 74 68 69 6e 67 20 6f 74 68 65 is anything othe
1ec49 72 20 74 68 61 6e 20 61 20 72 6f 77 73 65 74 20 r than a rowset
1ec4a 6f 62 6a 65 63 74 20 69 6e 20 6d 65 6d 6f 72 79 object in memory
1ec4b 20 63 65 6c 6c 20 50 31 2c 0a 20 20 2a 2a 20 64 cell P1,. ** d
1ec4c 65 6c 65 74 65 20 69 74 20 6e 6f 77 20 61 6e 64 elete it now and
1ec4d 20 69 6e 69 74 69 61 6c 69 7a 65 20 50 31 20 77 initialize P1 w
1ec4e 69 74 68 20 61 6e 20 65 6d 70 74 79 20 72 6f 77 ith an empty row
1ec4f 73 65 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 set. */. if( (
1ec50 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1ec51 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b 0a M_RowSet)==0 ){.
1ec52 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1ec53 65 6d 53 65 74 52 6f 77 53 65 74 28 70 49 6e 31 emSetRowSet(pIn1
1ec54 29 3b 0a 20 20 20 20 69 66 28 20 28 70 49 6e 31 );. if( (pIn1
1ec55 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f ->flags & MEM_Ro
1ec56 77 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20 wSet)==0 ) goto
1ec57 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 61 no_mem;. }.. a
1ec58 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 ssert( pOp->p4ty
1ec59 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a pe==P4_INT32 );.
1ec5a 20 20 61 73 73 65 72 74 28 20 75 2e 62 79 2e 69 assert( u.by.i
1ec5b 53 65 74 3d 3d 2d 31 20 7c 7c 20 75 2e 62 79 2e Set==-1 || u.by.
1ec5c 69 53 65 74 3e 3d 30 20 29 3b 0a 20 20 69 66 28 iSet>=0 );. if(
1ec5d 20 75 2e 62 79 2e 69 53 65 74 20 29 7b 0a 20 20 u.by.iSet ){.
1ec5e 20 20 75 2e 62 79 2e 65 78 69 73 74 73 20 3d 20 u.by.exists =
1ec5f 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73 sqlite3RowSetTes
1ec60 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 t(pIn1->u.pRowSe
1ec61 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 t,.
1ec62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ec63 20 20 28 75 38 29 28 75 2e 62 79 2e 69 53 65 74 (u8)(u.by.iSet
1ec64 3e 3d 30 20 3f 20 75 2e 62 79 2e 69 53 65 74 20 >=0 ? u.by.iSet
1ec65 26 20 30 78 66 20 3a 20 30 78 66 66 29 2c 0a 20 & 0xf : 0xff),.
1ec66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ec67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 pI
1ec68 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 69 66 n3->u.i);. if
1ec69 28 20 75 2e 62 79 2e 65 78 69 73 74 73 20 29 7b ( u.by.exists ){
1ec6a 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d . pc = pOp-
1ec6b 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 62 >p2 - 1;. b
1ec6c 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
1ec6d 20 20 69 66 28 20 75 2e 62 79 2e 69 53 65 74 3e if( u.by.iSet>
1ec6e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
1ec6f 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 70 49 3RowSetInsert(pI
1ec70 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 70 n1->u.pRowSet, p
1ec71 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 7d 0a 20 In3->u.i);. }.
1ec72 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e break;.}...#ifn
1ec73 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1ec74 54 52 49 47 47 45 52 0a 0a 2f 2a 20 4f 70 63 6f TRIGGER../* Opco
1ec75 64 65 3a 20 50 72 6f 67 72 61 6d 20 50 31 20 50 de: Program P1 P
1ec76 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 2 P3 P4 *.**.**
1ec77 45 78 65 63 75 74 65 20 74 68 65 20 74 72 69 67 Execute the trig
1ec78 67 65 72 20 70 72 6f 67 72 61 6d 20 70 61 73 73 ger program pass
1ec79 65 64 20 61 73 20 50 34 20 28 74 79 70 65 20 50 ed as P4 (type P
1ec7a 34 5f 53 55 42 50 52 4f 47 52 41 4d 29 2e 20 0a 4_SUBPROGRAM). .
1ec7b 2a 2a 0a 2a 2a 20 50 31 20 63 6f 6e 74 61 69 6e **.** P1 contain
1ec7c 73 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 s the address of
1ec7d 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c the memory cell
1ec7e 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 that contains t
1ec7f 68 65 20 66 69 72 73 74 20 6d 65 6d 6f 72 79 20 he first memory
1ec80 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 61 6e 20 61 .** cell in an a
1ec81 72 72 61 79 20 6f 66 20 76 61 6c 75 65 73 20 75 rray of values u
1ec82 73 65 64 20 61 73 20 61 72 67 75 6d 65 6e 74 73 sed as arguments
1ec83 20 74 6f 20 74 68 65 20 73 75 62 2d 70 72 6f 67 to the sub-prog
1ec84 72 61 6d 2e 20 50 32 20 0a 2a 2a 20 63 6f 6e 74 ram. P2 .** cont
1ec85 61 69 6e 73 20 74 68 65 20 61 64 64 72 65 73 73 ains the address
1ec86 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66 20 74 to jump to if t
1ec87 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 74 he sub-program t
1ec88 68 72 6f 77 73 20 61 6e 20 49 47 4e 4f 52 45 20 hrows an IGNORE
1ec89 0a 2a 2a 20 65 78 63 65 70 74 69 6f 6e 20 75 73 .** exception us
1ec8a 69 6e 67 20 74 68 65 20 52 41 49 53 45 28 29 20 ing the RAISE()
1ec8b 66 75 6e 63 74 69 6f 6e 2e 20 52 65 67 69 73 74 function. Regist
1ec8c 65 72 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 74 er P3 contains t
1ec8d 68 65 20 61 64 64 72 65 73 73 20 0a 2a 2a 20 6f he address .** o
1ec8e 66 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 f a memory cell
1ec8f 69 6e 20 74 68 69 73 20 28 74 68 65 20 70 61 72 in this (the par
1ec90 65 6e 74 29 20 56 4d 20 74 68 61 74 20 69 73 20 ent) VM that is
1ec91 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 used to allocate
1ec92 20 74 68 65 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 the .** memory
1ec93 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 required by the
1ec94 73 75 62 2d 76 64 62 65 20 61 74 20 72 75 6e 74 sub-vdbe at runt
1ec95 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 ime..**.** P4 is
1ec96 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
1ec97 65 20 56 4d 20 63 6f 6e 74 61 69 6e 69 6e 67 20 e VM containing
1ec98 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 the trigger prog
1ec99 72 61 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ram..*/.case OP_
1ec9a 50 72 6f 67 72 61 6d 3a 20 7b 20 20 20 20 20 20 Program: {
1ec9b 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 /* jump */.#if
1ec9c 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1ec9d 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1ec9e 6f 20 75 2e 62 7a 20 2a 2f 0a 20 20 69 6e 74 20 o u.bz */. int
1ec9f 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 nMem;
1eca0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1eca1 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 memory register
1eca2 73 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 61 s for sub-progra
1eca3 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 m */. int nByte
1eca4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1eca5 2a 20 42 79 74 65 73 20 6f 66 20 72 75 6e 74 69 * Bytes of runti
1eca6 6d 65 20 73 70 61 63 65 20 72 65 71 75 69 72 65 me space require
1eca7 64 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 61 d for sub-progra
1eca8 6d 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 74 3b m */. Mem *pRt;
1eca9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1ecaa 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 61 6c * Register to al
1ecab 6c 6f 63 61 74 65 20 72 75 6e 74 69 6d 65 20 73 locate runtime s
1ecac 70 61 63 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 pace */. Mem *p
1ecad 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 Mem;
1ecae 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65 /* Used to ite
1ecaf 72 61 74 65 20 74 68 72 6f 75 67 68 20 6d 65 6d rate through mem
1ecb0 6f 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 4d ory cells */. M
1ecb1 65 6d 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 em *pEnd;
1ecb2 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 6d /* Last m
1ecb3 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 6e 65 emory cell in ne
1ecb4 77 20 61 72 72 61 79 20 2a 2f 0a 20 20 56 64 62 w array */. Vdb
1ecb5 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 eFrame *pFrame;
1ecb6 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 64 62 65 /* New vdbe
1ecb7 20 66 72 61 6d 65 20 74 6f 20 65 78 65 63 75 74 frame to execut
1ecb8 65 20 69 6e 20 2a 2f 0a 20 20 53 75 62 50 72 6f e in */. SubPro
1ecb9 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 gram *pProgram;
1ecba 20 20 2f 2a 20 53 75 62 2d 70 72 6f 67 72 61 6d /* Sub-program
1ecbb 20 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 20 to execute */.
1ecbc 20 76 6f 69 64 20 2a 74 3b 20 20 20 20 20 20 20 void *t;
1ecbd 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 /* Toke
1ecbe 6e 20 69 64 65 6e 74 69 66 79 69 6e 67 20 74 72 n identifying tr
1ecbf 69 67 67 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20 igger */.#endif
1ecc0 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1ecc1 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1ecc2 62 7a 20 2a 2f 0a 0a 20 20 75 2e 62 7a 2e 70 50 bz */.. u.bz.pP
1ecc3 72 6f 67 72 61 6d 20 3d 20 70 4f 70 2d 3e 70 34 rogram = pOp->p4
1ecc4 2e 70 50 72 6f 67 72 61 6d 3b 0a 20 20 75 2e 62 .pProgram;. u.b
1ecc5 7a 2e 70 52 74 20 3d 20 26 70 2d 3e 61 4d 65 6d z.pRt = &p->aMem
1ecc6 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 [pOp->p3];. ass
1ecc7 65 72 74 28 20 75 2e 62 7a 2e 70 50 72 6f 67 72 ert( u.bz.pProgr
1ecc8 61 6d 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 0a 20 20 am->nOp>0 );..
1ecc9 2f 2a 20 49 66 20 74 68 65 20 70 35 20 66 6c 61 /* If the p5 fla
1ecca 67 20 69 73 20 63 6c 65 61 72 2c 20 74 68 65 6e g is clear, then
1eccb 20 72 65 63 75 72 73 69 76 65 20 69 6e 76 6f 63 recursive invoc
1eccc 61 74 69 6f 6e 20 6f 66 20 74 72 69 67 67 65 72 ation of trigger
1eccd 73 20 69 73 0a 20 20 2a 2a 20 64 69 73 61 62 6c s is. ** disabl
1ecce 65 64 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73 ed for backwards
1eccf 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 28 compatibility (
1ecd0 70 35 20 69 73 20 73 65 74 20 69 66 20 74 68 69 p5 is set if thi
1ecd1 73 20 73 75 62 2d 70 72 6f 67 72 61 6d 0a 20 20 s sub-program.
1ecd2 2a 2a 20 69 73 20 72 65 61 6c 6c 79 20 61 20 74 ** is really a t
1ecd3 72 69 67 67 65 72 2c 20 6e 6f 74 20 61 20 66 6f rigger, not a fo
1ecd4 72 65 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e reign key action
1ecd5 2c 20 61 6e 64 20 74 68 65 20 66 6c 61 67 20 73 , and the flag s
1ecd6 65 74 0a 20 20 2a 2a 20 61 6e 64 20 63 6c 65 61 et. ** and clea
1ecd7 72 65 64 20 62 79 20 74 68 65 20 22 50 52 41 47 red by the "PRAG
1ecd8 4d 41 20 72 65 63 75 72 73 69 76 65 5f 74 72 69 MA recursive_tri
1ecd9 67 67 65 72 73 22 20 63 6f 6d 6d 61 6e 64 20 69 ggers" command i
1ecda 73 20 63 6c 65 61 72 29 2e 0a 20 20 2a 2a 0a 20 s clear).. **.
1ecdb 20 2a 2a 20 49 74 20 69 73 20 72 65 63 75 72 73 ** It is recurs
1ecdc 69 76 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f ive invocation o
1ecdd 66 20 74 72 69 67 67 65 72 73 2c 20 61 74 20 74 f triggers, at t
1ecde 68 65 20 53 51 4c 20 6c 65 76 65 6c 2c 20 74 68 he SQL level, th
1ecdf 61 74 20 69 73 0a 20 20 2a 2a 20 64 69 73 61 62 at is. ** disab
1ece0 6c 65 64 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 led. In some cas
1ece1 65 73 20 61 20 73 69 6e 67 6c 65 20 74 72 69 67 es a single trig
1ece2 67 65 72 20 6d 61 79 20 67 65 6e 65 72 61 74 65 ger may generate
1ece3 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 0a 20 more than one.
1ece4 20 2a 2a 20 53 75 62 50 72 6f 67 72 61 6d 20 28 ** SubProgram (
1ece5 69 66 20 74 68 65 20 74 72 69 67 67 65 72 20 6d if the trigger m
1ece6 61 79 20 62 65 20 65 78 65 63 75 74 65 64 20 77 ay be executed w
1ece7 69 74 68 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e ith more than on
1ece8 65 20 64 69 66 66 65 72 65 6e 74 0a 20 20 2a 2a e different. **
1ece9 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 ON CONFLICT alg
1ecea 6f 72 69 74 68 6d 29 2e 20 53 75 62 50 72 6f 67 orithm). SubProg
1eceb 72 61 6d 20 73 74 72 75 63 74 75 72 65 73 20 61 ram structures a
1ecec 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 ssociated with a
1eced 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 20 74 72 69 . ** single tri
1ecee 67 67 65 72 20 61 6c 6c 20 68 61 76 65 20 74 68 gger all have th
1ecef 65 20 73 61 6d 65 20 76 61 6c 75 65 20 66 6f 72 e same value for
1ecf0 20 74 68 65 20 53 75 62 50 72 6f 67 72 61 6d 2e the SubProgram.
1ecf1 74 6f 6b 65 6e 0a 20 20 2a 2a 20 76 61 72 69 61 token. ** varia
1ecf2 62 6c 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 ble. */. if( p
1ecf3 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 75 2e Op->p5 ){. u.
1ecf4 62 7a 2e 74 20 3d 20 75 2e 62 7a 2e 70 50 72 6f bz.t = u.bz.pPro
1ecf5 67 72 61 6d 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 gram->token;.
1ecf6 20 66 6f 72 28 75 2e 62 7a 2e 70 46 72 61 6d 65 for(u.bz.pFrame
1ecf7 3d 70 2d 3e 70 46 72 61 6d 65 3b 20 75 2e 62 7a =p->pFrame; u.bz
1ecf8 2e 70 46 72 61 6d 65 20 26 26 20 75 2e 62 7a 2e .pFrame && u.bz.
1ecf9 70 46 72 61 6d 65 2d 3e 74 6f 6b 65 6e 21 3d 75 pFrame->token!=u
1ecfa 2e 62 7a 2e 74 3b 20 75 2e 62 7a 2e 70 46 72 61 .bz.t; u.bz.pFra
1ecfb 6d 65 3d 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e me=u.bz.pFrame->
1ecfc 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 69 66 pParent);. if
1ecfd 28 20 75 2e 62 7a 2e 70 46 72 61 6d 65 20 29 20 ( u.bz.pFrame )
1ecfe 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66 break;. }.. if
1ecff 28 20 70 2d 3e 6e 46 72 61 6d 65 3e 3d 64 62 2d ( p->nFrame>=db-
1ed00 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
1ed01 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50 IMIT_TRIGGER_DEP
1ed02 54 48 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 TH] ){. rc =
1ed03 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1ed04 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 sqlite3SetStri
1ed05 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 ng(&p->zErrMsg,
1ed06 64 62 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 6c 65 db, "too many le
1ed07 76 65 6c 73 20 6f 66 20 74 72 69 67 67 65 72 20 vels of trigger
1ed08 72 65 63 75 72 73 69 6f 6e 22 29 3b 0a 20 20 20 recursion");.
1ed09 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f break;. }.. /
1ed0a 2a 20 52 65 67 69 73 74 65 72 20 75 2e 62 7a 2e * Register u.bz.
1ed0b 70 52 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 pRt is used to s
1ed0c 74 6f 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 tore the memory
1ed0d 72 65 71 75 69 72 65 64 20 74 6f 20 73 61 76 65 required to save
1ed0e 20 74 68 65 20 73 74 61 74 65 0a 20 20 2a 2a 20 the state. **
1ed0f 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 of the current p
1ed10 72 6f 67 72 61 6d 2c 20 61 6e 64 20 74 68 65 20 rogram, and the
1ed11 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 65 64 20 memory required
1ed12 61 74 20 72 75 6e 74 69 6d 65 20 74 6f 20 65 78 at runtime to ex
1ed13 65 63 75 74 65 0a 20 20 2a 2a 20 74 68 65 20 74 ecute. ** the t
1ed14 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 20 rigger program.
1ed15 49 66 20 74 68 69 73 20 74 72 69 67 67 65 72 20 If this trigger
1ed16 68 61 73 20 62 65 65 6e 20 66 69 72 65 64 20 62 has been fired b
1ed17 65 66 6f 72 65 2c 20 74 68 65 6e 20 75 2e 62 7a efore, then u.bz
1ed18 2e 70 52 74 0a 20 20 2a 2a 20 69 73 20 61 6c 72 .pRt. ** is alr
1ed19 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 eady allocated.
1ed1a 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 6d 75 Otherwise, it mu
1ed1b 73 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 st be initialize
1ed1c 64 2e 20 20 2a 2f 0a 20 20 69 66 28 20 28 75 2e d. */. if( (u.
1ed1d 62 7a 2e 70 52 74 2d 3e 66 6c 61 67 73 26 4d 45 bz.pRt->flags&ME
1ed1e 4d 5f 46 72 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 M_Frame)==0 ){.
1ed1f 20 20 20 2f 2a 20 53 75 62 50 72 6f 67 72 61 6d /* SubProgram
1ed20 2e 6e 4d 65 6d 20 69 73 20 73 65 74 20 74 6f 20 .nMem is set to
1ed21 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 65 the number of me
1ed22 6d 6f 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 mory cells used
1ed23 62 79 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 72 by the. ** pr
1ed24 6f 67 72 61 6d 20 73 74 6f 72 65 64 20 69 6e 20 ogram stored in
1ed25 53 75 62 50 72 6f 67 72 61 6d 2e 61 4f 70 2e 20 SubProgram.aOp.
1ed26 41 73 20 77 65 6c 6c 20 61 73 20 74 68 65 73 65 As well as these
1ed27 2c 20 6f 6e 65 20 6d 65 6d 6f 72 79 0a 20 20 20 , one memory.
1ed28 20 2a 2a 20 63 65 6c 6c 20 69 73 20 72 65 71 75 ** cell is requ
1ed29 69 72 65 64 20 66 6f 72 20 65 61 63 68 20 63 75 ired for each cu
1ed2a 72 73 6f 72 20 75 73 65 64 20 62 79 20 74 68 65 rsor used by the
1ed2b 20 70 72 6f 67 72 61 6d 2e 20 53 65 74 20 6c 6f program. Set lo
1ed2c 63 61 6c 0a 20 20 20 20 2a 2a 20 76 61 72 69 61 cal. ** varia
1ed2d 62 6c 65 20 75 2e 62 7a 2e 6e 4d 65 6d 20 28 61 ble u.bz.nMem (a
1ed2e 6e 64 20 6c 61 74 65 72 2c 20 56 64 62 65 46 72 nd later, VdbeFr
1ed2f 61 6d 65 2e 6e 43 68 69 6c 64 4d 65 6d 29 20 74 ame.nChildMem) t
1ed30 6f 20 74 68 69 73 20 76 61 6c 75 65 2e 0a 20 20 o this value..
1ed31 20 20 2a 2f 0a 20 20 20 20 75 2e 62 7a 2e 6e 4d */. u.bz.nM
1ed32 65 6d 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 em = u.bz.pProgr
1ed33 61 6d 2d 3e 6e 4d 65 6d 20 2b 20 75 2e 62 7a 2e am->nMem + u.bz.
1ed34 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3b 0a pProgram->nCsr;.
1ed35 20 20 20 20 75 2e 62 7a 2e 6e 42 79 74 65 20 3d u.bz.nByte =
1ed36 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 56 ROUND8(sizeof(V
1ed37 64 62 65 46 72 61 6d 65 29 29 0a 20 20 20 20 20 dbeFrame)).
1ed38 20 20 20 20 20 20 20 20 20 2b 20 75 2e 62 7a 2e + u.bz.
1ed39 6e 4d 65 6d 20 2a 20 73 69 7a 65 6f 66 28 4d 65 nMem * sizeof(Me
1ed3a 6d 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 m).
1ed3b 20 2b 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d + u.bz.pProgram
1ed3c 2d 3e 6e 43 73 72 20 2a 20 73 69 7a 65 6f 66 28 ->nCsr * sizeof(
1ed3d 56 64 62 65 43 75 72 73 6f 72 20 2a 29 3b 0a 20 VdbeCursor *);.
1ed3e 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 20 3d u.bz.pFrame =
1ed3f 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
1ed40 5a 65 72 6f 28 64 62 2c 20 75 2e 62 7a 2e 6e 42 Zero(db, u.bz.nB
1ed41 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 21 75 yte);. if( !u
1ed42 2e 62 7a 2e 70 46 72 61 6d 65 20 29 7b 0a 20 20 .bz.pFrame ){.
1ed43 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b goto no_mem;
1ed44 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
1ed45 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
1ed46 28 75 2e 62 7a 2e 70 52 74 29 3b 0a 20 20 20 20 (u.bz.pRt);.
1ed47 75 2e 62 7a 2e 70 52 74 2d 3e 66 6c 61 67 73 20 u.bz.pRt->flags
1ed48 3d 20 4d 45 4d 5f 46 72 61 6d 65 3b 0a 20 20 20 = MEM_Frame;.
1ed49 20 75 2e 62 7a 2e 70 52 74 2d 3e 75 2e 70 46 72 u.bz.pRt->u.pFr
1ed4a 61 6d 65 20 3d 20 75 2e 62 7a 2e 70 46 72 61 6d ame = u.bz.pFram
1ed4b 65 3b 0a 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 e;.. u.bz.pFr
1ed4c 61 6d 65 2d 3e 76 20 3d 20 70 3b 0a 20 20 20 20 ame->v = p;.
1ed4d 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 u.bz.pFrame->nCh
1ed4e 69 6c 64 4d 65 6d 20 3d 20 75 2e 62 7a 2e 6e 4d ildMem = u.bz.nM
1ed4f 65 6d 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 em;. u.bz.pFr
1ed50 61 6d 65 2d 3e 6e 43 68 69 6c 64 43 73 72 20 3d ame->nChildCsr =
1ed51 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e u.bz.pProgram->
1ed52 6e 43 73 72 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 nCsr;. u.bz.p
1ed53 46 72 61 6d 65 2d 3e 70 63 20 3d 20 70 63 3b 0a Frame->pc = pc;.
1ed54 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d u.bz.pFrame-
1ed55 3e 61 4d 65 6d 20 3d 20 70 2d 3e 61 4d 65 6d 3b >aMem = p->aMem;
1ed56 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 . u.bz.pFrame
1ed57 2d 3e 6e 4d 65 6d 20 3d 20 70 2d 3e 6e 4d 65 6d ->nMem = p->nMem
1ed58 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d ;. u.bz.pFram
1ed59 65 2d 3e 61 70 43 73 72 20 3d 20 70 2d 3e 61 70 e->apCsr = p->ap
1ed5a 43 73 72 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 Csr;. u.bz.pF
1ed5b 72 61 6d 65 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 rame->nCursor =
1ed5c 70 2d 3e 6e 43 75 72 73 6f 72 3b 0a 20 20 20 20 p->nCursor;.
1ed5d 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 61 4f 70 u.bz.pFrame->aOp
1ed5e 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 20 20 75 = p->aOp;. u
1ed5f 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 4f 70 20 .bz.pFrame->nOp
1ed60 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 20 20 75 2e = p->nOp;. u.
1ed61 62 7a 2e 70 46 72 61 6d 65 2d 3e 74 6f 6b 65 6e bz.pFrame->token
1ed62 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d = u.bz.pProgram
1ed63 2d 3e 74 6f 6b 65 6e 3b 0a 0a 20 20 20 20 75 2e ->token;.. u.
1ed64 62 7a 2e 70 45 6e 64 20 3d 20 26 56 64 62 65 46 bz.pEnd = &VdbeF
1ed65 72 61 6d 65 4d 65 6d 28 75 2e 62 7a 2e 70 46 72 rameMem(u.bz.pFr
1ed66 61 6d 65 29 5b 75 2e 62 7a 2e 70 46 72 61 6d 65 ame)[u.bz.pFrame
1ed67 2d 3e 6e 43 68 69 6c 64 4d 65 6d 5d 3b 0a 20 20 ->nChildMem];.
1ed68 20 20 66 6f 72 28 75 2e 62 7a 2e 70 4d 65 6d 3d for(u.bz.pMem=
1ed69 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 75 2e 62 VdbeFrameMem(u.b
1ed6a 7a 2e 70 46 72 61 6d 65 29 3b 20 75 2e 62 7a 2e z.pFrame); u.bz.
1ed6b 70 4d 65 6d 21 3d 75 2e 62 7a 2e 70 45 6e 64 3b pMem!=u.bz.pEnd;
1ed6c 20 75 2e 62 7a 2e 70 4d 65 6d 2b 2b 29 7b 0a 20 u.bz.pMem++){.
1ed6d 20 20 20 20 20 75 2e 62 7a 2e 70 4d 65 6d 2d 3e u.bz.pMem->
1ed6e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1ed6f 3b 0a 20 20 20 20 20 20 75 2e 62 7a 2e 70 4d 65 ;. u.bz.pMe
1ed70 6d 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 m->db = db;.
1ed71 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 }. }else{. u
1ed72 2e 62 7a 2e 70 46 72 61 6d 65 20 3d 20 75 2e 62 .bz.pFrame = u.b
1ed73 7a 2e 70 52 74 2d 3e 75 2e 70 46 72 61 6d 65 3b z.pRt->u.pFrame;
1ed74 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 . assert( u.b
1ed75 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4d 65 6d z.pProgram->nMem
1ed76 2b 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e +u.bz.pProgram->
1ed77 6e 43 73 72 3d 3d 75 2e 62 7a 2e 70 46 72 61 6d nCsr==u.bz.pFram
1ed78 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d 20 29 3b 0a e->nChildMem );.
1ed79 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 7a assert( u.bz
1ed7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3d .pProgram->nCsr=
1ed7b 3d 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 =u.bz.pFrame->nC
1ed7c 68 69 6c 64 43 73 72 20 29 3b 0a 20 20 20 20 61 hildCsr );. a
1ed7d 73 73 65 72 74 28 20 70 63 3d 3d 75 2e 62 7a 2e ssert( pc==u.bz.
1ed7e 70 46 72 61 6d 65 2d 3e 70 63 20 29 3b 0a 20 20 pFrame->pc );.
1ed7f 7d 0a 0a 20 20 70 2d 3e 6e 46 72 61 6d 65 2b 2b }.. p->nFrame++
1ed80 3b 0a 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d ;. u.bz.pFrame-
1ed81 3e 70 50 61 72 65 6e 74 20 3d 20 70 2d 3e 70 46 >pParent = p->pF
1ed82 72 61 6d 65 3b 0a 20 20 75 2e 62 7a 2e 70 46 72 rame;. u.bz.pFr
1ed83 61 6d 65 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d ame->lastRowid =
1ed84 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a db->lastRowid;.
1ed85 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e u.bz.pFrame->n
1ed86 43 68 61 6e 67 65 20 3d 20 70 2d 3e 6e 43 68 61 Change = p->nCha
1ed87 6e 67 65 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 nge;. p->nChang
1ed88 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 46 72 61 e = 0;. p->pFra
1ed89 6d 65 20 3d 20 75 2e 62 7a 2e 70 46 72 61 6d 65 me = u.bz.pFrame
1ed8a 3b 0a 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 26 56 ;. p->aMem = &V
1ed8b 64 62 65 46 72 61 6d 65 4d 65 6d 28 75 2e 62 7a dbeFrameMem(u.bz
1ed8c 2e 70 46 72 61 6d 65 29 5b 2d 31 5d 3b 0a 20 20 .pFrame)[-1];.
1ed8d 70 2d 3e 6e 4d 65 6d 20 3d 20 75 2e 62 7a 2e 70 p->nMem = u.bz.p
1ed8e 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d Frame->nChildMem
1ed8f 3b 0a 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d ;. p->nCursor =
1ed90 20 28 75 31 36 29 75 2e 62 7a 2e 70 46 72 61 6d (u16)u.bz.pFram
1ed91 65 2d 3e 6e 43 68 69 6c 64 43 73 72 3b 0a 20 20 e->nChildCsr;.
1ed92 70 2d 3e 61 70 43 73 72 20 3d 20 28 56 64 62 65 p->apCsr = (Vdbe
1ed93 43 75 72 73 6f 72 20 2a 2a 29 26 70 2d 3e 61 4d Cursor **)&p->aM
1ed94 65 6d 5b 70 2d 3e 6e 4d 65 6d 2b 31 5d 3b 0a 20 em[p->nMem+1];.
1ed95 20 70 2d 3e 61 4f 70 20 3d 20 75 2e 62 7a 2e 70 p->aOp = u.bz.p
1ed96 50 72 6f 67 72 61 6d 2d 3e 61 4f 70 3b 0a 20 20 Program->aOp;.
1ed97 70 2d 3e 6e 4f 70 20 3d 20 75 2e 62 7a 2e 70 50 p->nOp = u.bz.pP
1ed98 72 6f 67 72 61 6d 2d 3e 6e 4f 70 3b 0a 20 20 70 rogram->nOp;. p
1ed99 63 20 3d 20 2d 31 3b 0a 0a 20 20 62 72 65 61 6b c = -1;.. break
1ed9a 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1ed9b 50 61 72 61 6d 20 50 31 20 50 32 20 2a 20 2a 20 Param P1 P2 * *
1ed9c 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 *.**.** This opc
1ed9d 6f 64 65 20 69 73 20 6f 6e 6c 79 20 65 76 65 72 ode is only ever
1ed9e 20 70 72 65 73 65 6e 74 20 69 6e 20 73 75 62 2d present in sub-
1ed9f 70 72 6f 67 72 61 6d 73 20 63 61 6c 6c 65 64 20 programs called
1eda0 76 69 61 20 74 68 65 20 0a 2a 2a 20 4f 50 5f 50 via the .** OP_P
1eda1 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 rogram instructi
1eda2 6f 6e 2e 20 43 6f 70 79 20 61 20 76 61 6c 75 65 on. Copy a value
1eda3 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 currently store
1eda4 64 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 0a 2a d in a memory .*
1eda5 2a 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 63 61 * cell of the ca
1eda6 6c 6c 69 6e 67 20 28 70 61 72 65 6e 74 29 20 66 lling (parent) f
1eda7 72 61 6d 65 20 74 6f 20 63 65 6c 6c 20 50 32 20 rame to cell P2
1eda8 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 in the current f
1eda9 72 61 6d 65 73 20 0a 2a 2a 20 61 64 64 72 65 73 rames .** addres
1edaa 73 20 73 70 61 63 65 2e 20 54 68 69 73 20 69 73 s space. This is
1edab 20 75 73 65 64 20 62 79 20 74 72 69 67 67 65 72 used by trigger
1edac 20 70 72 6f 67 72 61 6d 73 20 74 6f 20 61 63 63 programs to acc
1edad 65 73 73 20 74 68 65 20 6e 65 77 2e 2a 20 0a 2a ess the new.* .*
1edae 2a 20 61 6e 64 20 6f 6c 64 2e 2a 20 76 61 6c 75 * and old.* valu
1edaf 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 64 es..**.** The ad
1edb0 64 72 65 73 73 20 6f 66 20 74 68 65 20 63 65 6c dress of the cel
1edb1 6c 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 l in the parent
1edb2 66 72 61 6d 65 20 69 73 20 64 65 74 65 72 6d 69 frame is determi
1edb3 6e 65 64 20 62 79 20 61 64 64 69 6e 67 0a 2a 2a ned by adding.**
1edb4 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
1edb5 65 20 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f e P1 argument to
1edb6 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
1edb7 65 20 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f e P1 argument to
1edb8 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 the.** calling
1edb9 4f 50 5f 50 72 6f 67 72 61 6d 20 69 6e 73 74 72 OP_Program instr
1edba 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 uction..*/.case
1edbb 4f 50 5f 50 61 72 61 6d 3a 20 7b 20 20 20 20 20 OP_Param: {
1edbc 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1edbd 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 erelease */.#if
1edbe 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1edbf 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1edc0 20 75 2e 63 61 20 2a 2f 0a 20 20 56 64 62 65 46 u.ca */. VdbeF
1edc1 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20 rame *pFrame;.
1edc2 4d 65 6d 20 2a 70 49 6e 3b 0a 23 65 6e 64 69 66 Mem *pIn;.#endif
1edc3 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1edc4 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1edc5 2e 63 61 20 2a 2f 0a 20 20 75 2e 63 61 2e 70 46 .ca */. u.ca.pF
1edc6 72 61 6d 65 20 3d 20 70 2d 3e 70 46 72 61 6d 65 rame = p->pFrame
1edc7 3b 0a 20 20 75 2e 63 61 2e 70 49 6e 20 3d 20 26 ;. u.ca.pIn = &
1edc8 75 2e 63 61 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 u.ca.pFrame->aMe
1edc9 6d 5b 70 4f 70 2d 3e 70 31 20 2b 20 75 2e 63 61 m[pOp->p1 + u.ca
1edca 2e 70 46 72 61 6d 65 2d 3e 61 4f 70 5b 75 2e 63 .pFrame->aOp[u.c
1edcb 61 2e 70 46 72 61 6d 65 2d 3e 70 63 5d 2e 70 31 a.pFrame->pc].p1
1edcc 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 ];. sqlite3Vdbe
1edcd 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 MemShallowCopy(p
1edce 4f 75 74 2c 20 75 2e 63 61 2e 70 49 6e 2c 20 4d Out, u.ca.pIn, M
1edcf 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 62 72 65 EM_Ephem);. bre
1edd0 61 6b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a ak;.}..#endif /*
1edd1 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f #ifndef SQLITE_
1edd2 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a OMIT_TRIGGER */.
1edd3 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1edd4 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 OMIT_FOREIGN_KEY
1edd5 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6b 43 6f ./* Opcode: FkCo
1edd6 75 6e 74 65 72 20 50 31 20 50 32 20 2a 20 2a 20 unter P1 P2 * *
1edd7 2a 0a 2a 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e *.**.** Incremen
1edd8 74 20 61 20 22 63 6f 6e 73 74 72 61 69 6e 74 20 t a "constraint
1edd9 63 6f 75 6e 74 65 72 22 20 62 79 20 50 32 20 28 counter" by P2 (
1edda 50 32 20 6d 61 79 20 62 65 20 6e 65 67 61 74 69 P2 may be negati
1eddb 76 65 20 6f 72 20 70 6f 73 69 74 69 76 65 29 2e ve or positive).
1eddc 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 6e 6f 6e .** If P1 is non
1eddd 2d 7a 65 72 6f 2c 20 74 68 65 20 64 61 74 61 62 -zero, the datab
1edde 61 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 ase constraint c
1eddf 6f 75 6e 74 65 72 20 69 73 20 69 6e 63 72 65 6d ounter is increm
1ede0 65 6e 74 65 64 20 0a 2a 2a 20 28 64 65 66 65 72 ented .** (defer
1ede1 72 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 red foreign key
1ede2 63 6f 6e 73 74 72 61 69 6e 74 73 29 2e 20 4f 74 constraints). Ot
1ede3 68 65 72 77 69 73 65 2c 20 69 66 20 50 31 20 69 herwise, if P1 i
1ede4 73 20 7a 65 72 6f 2c 20 74 68 65 20 0a 2a 2a 20 s zero, the .**
1ede5 73 74 61 74 65 6d 65 6e 74 20 63 6f 75 6e 74 65 statement counte
1ede6 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 r is incremented
1ede7 20 28 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 (immediate fore
1ede8 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 ign key constrai
1ede9 6e 74 73 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 nts)..*/.case OP
1edea 5f 46 6b 43 6f 75 6e 74 65 72 3a 20 7b 0a 20 20 _FkCounter: {.
1edeb 69 66 28 20 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 if( pOp->p1 ){.
1edec 20 20 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 db->nDeferred
1eded 43 6f 6e 73 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b Cons += pOp->p2;
1edee 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d . }else{. p-
1edef 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 20 2b >nFkConstraint +
1edf0 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 7d 0a 20 = pOp->p2;. }.
1edf1 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1edf2 63 6f 64 65 3a 20 46 6b 49 66 5a 65 72 6f 20 50 code: FkIfZero P
1edf3 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1edf4 20 54 68 69 73 20 6f 70 63 6f 64 65 20 74 65 73 This opcode tes
1edf5 74 73 20 69 66 20 61 20 66 6f 72 65 69 67 6e 20 ts if a foreign
1edf6 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 key constraint-c
1edf7 6f 75 6e 74 65 72 20 69 73 20 63 75 72 72 65 6e ounter is curren
1edf8 74 6c 79 20 7a 65 72 6f 2e 0a 2a 2a 20 49 66 20 tly zero..** If
1edf9 73 6f 2c 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 so, jump to inst
1edfa 72 75 63 74 69 6f 6e 20 50 32 2e 20 4f 74 68 65 ruction P2. Othe
1edfb 72 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 6f rwise, fall thro
1edfc 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ugh to the next
1edfd 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e .** instruction.
1edfe 0a 2a 2a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 .**.** If P1 is
1edff 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 non-zero, then t
1ee00 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e he jump is taken
1ee01 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 if the database
1ee02 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e constraint-coun
1ee03 74 65 72 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 28 ter.** is zero (
1ee04 74 68 65 20 6f 6e 65 20 74 68 61 74 20 63 6f 75 the one that cou
1ee05 6e 74 73 20 64 65 66 65 72 72 65 64 20 63 6f 6e nts deferred con
1ee06 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f straint violatio
1ee07 6e 73 29 2e 20 49 66 20 50 31 20 69 73 0a 2a 2a ns). If P1 is.**
1ee08 20 7a 65 72 6f 2c 20 74 68 65 20 6a 75 6d 70 20 zero, the jump
1ee09 69 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20 is taken if the
1ee0a 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e 73 74 72 statement constr
1ee0b 61 69 6e 74 2d 63 6f 75 6e 74 65 72 20 69 73 20 aint-counter is
1ee0c 7a 65 72 6f 0a 2a 2a 20 28 69 6d 6d 65 64 69 61 zero.** (immedia
1ee0d 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 te foreign key c
1ee0e 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 onstraint violat
1ee0f 69 6f 6e 73 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f ions)..*/.case O
1ee10 50 5f 46 6b 49 66 5a 65 72 6f 3a 20 7b 20 20 20 P_FkIfZero: {
1ee11 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f /* jump */
1ee12 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 20 29 . if( pOp->p1 )
1ee13 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6e 44 {. if( db->nD
1ee14 65 66 65 72 72 65 64 43 6f 6e 73 3d 3d 30 20 29 eferredCons==0 )
1ee15 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b pc = pOp->p2-1;
1ee16 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 . }else{. if
1ee17 28 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 ( p->nFkConstrai
1ee18 6e 74 3d 3d 30 20 29 20 70 63 20 3d 20 70 4f 70 nt==0 ) pc = pOp
1ee19 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 ->p2-1;. }. br
1ee1a 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a eak;.}.#endif /*
1ee1b 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f #ifndef SQLITE_
1ee1c 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 OMIT_FOREIGN_KEY
1ee1d 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
1ee1e 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 ITE_OMIT_AUTOINC
1ee1f 52 45 4d 45 4e 54 0a 2f 2a 20 4f 70 63 6f 64 65 REMENT./* Opcode
1ee20 3a 20 4d 65 6d 4d 61 78 20 50 31 20 50 32 20 2a : MemMax P1 P2 *
1ee21 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 * *.**.** P1 is
1ee22 20 61 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 a register in t
1ee23 68 65 20 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66 he root frame of
1ee24 20 74 68 69 73 20 56 4d 20 28 74 68 65 20 72 6f this VM (the ro
1ee25 6f 74 20 66 72 61 6d 65 20 69 73 0a 2a 2a 20 64 ot frame is.** d
1ee26 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 ifferent from th
1ee27 65 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 20 e current frame
1ee28 69 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 if this instruct
1ee29 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 65 78 65 ion is being exe
1ee2a 63 75 74 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 cuted.** within
1ee2b 61 20 73 75 62 2d 70 72 6f 67 72 61 6d 29 2e 20 a sub-program).
1ee2c 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 Set the value of
1ee2d 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 register P1 to
1ee2e 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 0a the maximum of .
1ee2f 2a 2a 20 69 74 73 20 63 75 72 72 65 6e 74 20 76 ** its current v
1ee30 61 6c 75 65 20 61 6e 64 20 74 68 65 20 76 61 6c alue and the val
1ee31 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1ee32 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 2..**.** This in
1ee33 73 74 72 75 63 74 69 6f 6e 20 74 68 72 6f 77 73 struction throws
1ee34 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 an error if the
1ee35 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20 memory cell is
1ee36 6e 6f 74 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a not initially.**
1ee37 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a an integer..*/.
1ee38 63 61 73 65 20 4f 50 5f 4d 65 6d 4d 61 78 3a 20 case OP_MemMax:
1ee39 7b 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 { /* in2
1ee3a 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1ee3b 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1ee3c 65 64 20 69 6e 74 6f 20 75 2e 63 62 20 2a 2f 0a ed into u.cb */.
1ee3d 20 20 4d 65 6d 20 2a 70 49 6e 31 3b 0a 20 20 56 Mem *pIn1;. V
1ee3e 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 dbeFrame *pFrame
1ee3f 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1ee40 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1ee41 64 20 69 6e 74 6f 20 75 2e 63 62 20 2a 2f 0a 20 d into u.cb */.
1ee42 20 69 66 28 20 70 2d 3e 70 46 72 61 6d 65 20 29 if( p->pFrame )
1ee43 7b 0a 20 20 20 20 66 6f 72 28 75 2e 63 62 2e 70 {. for(u.cb.p
1ee44 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b Frame=p->pFrame;
1ee45 20 75 2e 63 62 2e 70 46 72 61 6d 65 2d 3e 70 50 u.cb.pFrame->pP
1ee46 61 72 65 6e 74 3b 20 75 2e 63 62 2e 70 46 72 61 arent; u.cb.pFra
1ee47 6d 65 3d 75 2e 63 62 2e 70 46 72 61 6d 65 2d 3e me=u.cb.pFrame->
1ee48 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 75 2e pParent);. u.
1ee49 63 62 2e 70 49 6e 31 20 3d 20 26 75 2e 63 62 2e cb.pIn1 = &u.cb.
1ee4a 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70 pFrame->aMem[pOp
1ee4b 2d 3e 70 31 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a ->p1];. }else{.
1ee4c 20 20 20 20 75 2e 63 62 2e 70 49 6e 31 20 3d 20 u.cb.pIn1 =
1ee4d 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 &p->aMem[pOp->p1
1ee4e 5d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 ];. }. sqlite3
1ee4f 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 VdbeMemIntegerif
1ee50 79 28 75 2e 63 62 2e 70 49 6e 31 29 3b 0a 20 20 y(u.cb.pIn1);.
1ee51 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e sqlite3VdbeMemIn
1ee52 74 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b 0a tegerify(pIn2);.
1ee53 20 20 69 66 28 20 75 2e 63 62 2e 70 49 6e 31 2d if( u.cb.pIn1-
1ee54 3e 75 2e 69 3c 70 49 6e 32 2d 3e 75 2e 69 29 7b >u.i<pIn2->u.i){
1ee55 0a 20 20 20 20 75 2e 63 62 2e 70 49 6e 31 2d 3e . u.cb.pIn1->
1ee56 75 2e 69 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b u.i = pIn2->u.i;
1ee57 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1ee58 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1ee59 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d _OMIT_AUTOINCREM
1ee5a 45 4e 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 ENT */../* Opcod
1ee5b 65 3a 20 49 66 50 6f 73 20 50 31 20 50 32 20 2a e: IfPos P1 P2 *
1ee5c 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 * *.**.** If th
1ee5d 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 e value of regis
1ee5e 74 65 72 20 50 31 20 69 73 20 31 20 6f 72 20 67 ter P1 is 1 or g
1ee5f 72 65 61 74 65 72 2c 20 6a 75 6d 70 20 74 6f 20 reater, jump to
1ee60 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 P2..**.** It is
1ee61 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74 illegal to use t
1ee62 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1ee63 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68 on a register th
1ee64 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 at does.** not c
1ee65 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 ontain an intege
1ee66 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e r. An assertion
1ee67 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 fault will resu
1ee68 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a lt if you try..*
1ee69 2f 0a 63 61 73 65 20 4f 50 5f 49 66 50 6f 73 3a /.case OP_IfPos:
1ee6a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1ee6b 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65 p, in1 */. asse
1ee6c 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26 rt( pIn1->flags&
1ee6d 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28 MEM_Int );. if(
1ee6e 20 70 49 6e 31 2d 3e 75 2e 69 3e 30 20 29 7b 0a pIn1->u.i>0 ){.
1ee6f 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 pc = pOp->p
1ee70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 2 - 1;. }. bre
1ee71 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1ee72 3a 20 49 66 4e 65 67 20 50 31 20 50 32 20 2a 20 : IfNeg P1 P2 *
1ee73 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 * *.**.** If the
1ee74 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 value of regist
1ee75 65 72 20 50 31 20 69 73 20 6c 65 73 73 20 74 68 er P1 is less th
1ee76 61 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70 20 74 6f an zero, jump to
1ee77 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 P2. .**.** It i
1ee78 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 s illegal to use
1ee79 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f this instructio
1ee7a 6e 20 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 n on a register
1ee7b 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 that does.** not
1ee7c 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 contain an inte
1ee7d 67 65 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 ger. An asserti
1ee7e 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 on fault will re
1ee7f 73 75 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e sult if you try.
1ee80 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e 65 .*/.case OP_IfNe
1ee81 67 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a g: { /* j
1ee82 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 ump, in1 */. as
1ee83 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 sert( pIn1->flag
1ee84 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 s&MEM_Int );. i
1ee85 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3c 30 20 29 f( pIn1->u.i<0 )
1ee86 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d {. pc = pOp-
1ee87 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p2 - 1;. }. b
1ee88 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1ee89 64 65 3a 20 49 66 5a 65 72 6f 20 50 31 20 50 32 de: IfZero P1 P2
1ee8a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 * * *.**.** If
1ee8b 74 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 the value of reg
1ee8c 69 73 74 65 72 20 50 31 20 69 73 20 65 78 61 63 ister P1 is exac
1ee8d 74 6c 79 20 30 2c 20 6a 75 6d 70 20 74 6f 20 50 tly 0, jump to P
1ee8e 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 2. .**.** It is
1ee8f 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74 illegal to use t
1ee90 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1ee91 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68 on a register th
1ee92 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 at does.** not c
1ee93 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 ontain an intege
1ee94 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e r. An assertion
1ee95 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 fault will resu
1ee96 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a lt if you try..*
1ee97 2f 0a 63 61 73 65 20 4f 50 5f 49 66 5a 65 72 6f /.case OP_IfZero
1ee98 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 : { /* ju
1ee99 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 mp, in1 */. ass
1ee9a 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 ert( pIn1->flags
1ee9b 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 &MEM_Int );. if
1ee9c 28 20 70 49 6e 31 2d 3e 75 2e 69 3d 3d 30 20 29 ( pIn1->u.i==0 )
1ee9d 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d {. pc = pOp-
1ee9e 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p2 - 1;. }. b
1ee9f 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1eea0 64 65 3a 20 41 67 67 53 74 65 70 20 2a 20 50 32 de: AggStep * P2
1eea1 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 P3 P4 P5.**.**
1eea2 45 78 65 63 75 74 65 20 74 68 65 20 73 74 65 70 Execute the step
1eea3 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e function for an
1eea4 20 61 67 67 72 65 67 61 74 65 2e 20 20 54 68 65 aggregate. The
1eea5 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 .** function has
1eea6 20 50 35 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 P5 arguments.
1eea7 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 P4 is a pointer
1eea8 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65 66 0a to the FuncDef.
1eea9 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 61 ** structure tha
1eeaa 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 t specifies the
1eeab 66 75 6e 63 74 69 6f 6e 2e 20 20 55 73 65 20 72 function. Use r
1eeac 65 67 69 73 74 65 72 0a 2a 2a 20 50 33 20 61 73 egister.** P3 as
1eead 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 the accumulator
1eeae 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 35 20 61 ..**.** The P5 a
1eeaf 72 67 75 6d 65 6e 74 73 20 61 72 65 20 74 61 6b rguments are tak
1eeb0 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 en from register
1eeb1 20 50 32 20 61 6e 64 20 69 74 73 0a 2a 2a 20 73 P2 and its.** s
1eeb2 75 63 63 65 73 73 6f 72 73 2e 0a 2a 2f 0a 63 61 uccessors..*/.ca
1eeb3 73 65 20 4f 50 5f 41 67 67 53 74 65 70 3a 20 7b se OP_AggStep: {
1eeb4 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1eeb5 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1eeb6 20 69 6e 74 6f 20 75 2e 63 63 20 2a 2f 0a 20 20 into u.cc */.
1eeb7 69 6e 74 20 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a int n;. int i;.
1eeb8 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 4d Mem *pMem;. M
1eeb9 65 6d 20 2a 70 52 65 63 3b 0a 20 20 73 71 6c 69 em *pRec;. sqli
1eeba 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b te3_context ctx;
1eebb 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
1eebc 20 2a 2a 61 70 56 61 6c 3b 0a 23 65 6e 64 69 66 **apVal;.#endif
1eebd 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1eebe 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1eebf 2e 63 63 20 2a 2f 0a 0a 20 20 75 2e 63 63 2e 6e .cc */.. u.cc.n
1eec0 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 61 73 = pOp->p5;. as
1eec1 73 65 72 74 28 20 75 2e 63 63 2e 6e 3e 3d 30 20 sert( u.cc.n>=0
1eec2 29 3b 0a 20 20 75 2e 63 63 2e 70 52 65 63 20 3d );. u.cc.pRec =
1eec3 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1eec4 32 5d 3b 0a 20 20 75 2e 63 63 2e 61 70 56 61 6c 2];. u.cc.apVal
1eec5 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61 = p->apArg;. a
1eec6 73 73 65 72 74 28 20 75 2e 63 63 2e 61 70 56 61 ssert( u.cc.apVa
1eec7 6c 20 7c 7c 20 75 2e 63 63 2e 6e 3d 3d 30 20 29 l || u.cc.n==0 )
1eec8 3b 0a 20 20 66 6f 72 28 75 2e 63 63 2e 69 3d 30 ;. for(u.cc.i=0
1eec9 3b 20 75 2e 63 63 2e 69 3c 75 2e 63 63 2e 6e 3b ; u.cc.i<u.cc.n;
1eeca 20 75 2e 63 63 2e 69 2b 2b 2c 20 75 2e 63 63 2e u.cc.i++, u.cc.
1eecb 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 75 2e 63 pRec++){. u.c
1eecc 63 2e 61 70 56 61 6c 5b 75 2e 63 63 2e 69 5d 20 c.apVal[u.cc.i]
1eecd 3d 20 75 2e 63 63 2e 70 52 65 63 3b 0a 20 20 20 = u.cc.pRec;.
1eece 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1eecf 74 6f 72 65 54 79 70 65 28 75 2e 63 63 2e 70 52 toreType(u.cc.pR
1eed0 65 63 29 3b 0a 20 20 7d 0a 20 20 75 2e 63 63 2e ec);. }. u.cc.
1eed1 63 74 78 2e 70 46 75 6e 63 20 3d 20 70 4f 70 2d ctx.pFunc = pOp-
1eed2 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 61 73 73 >p4.pFunc;. ass
1eed3 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 ert( pOp->p3>0 &
1eed4 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d & pOp->p3<=p->nM
1eed5 65 6d 20 29 3b 0a 20 20 75 2e 63 63 2e 63 74 78 em );. u.cc.ctx
1eed6 2e 70 4d 65 6d 20 3d 20 75 2e 63 63 2e 70 4d 65 .pMem = u.cc.pMe
1eed7 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 m = &p->aMem[pOp
1eed8 2d 3e 70 33 5d 3b 0a 20 20 75 2e 63 63 2e 70 4d ->p3];. u.cc.pM
1eed9 65 6d 2d 3e 6e 2b 2b 3b 0a 20 20 75 2e 63 63 2e em->n++;. u.cc.
1eeda 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 ctx.s.flags = ME
1eedb 4d 5f 4e 75 6c 6c 3b 0a 20 20 75 2e 63 63 2e 63 M_Null;. u.cc.c
1eedc 74 78 2e 73 2e 7a 20 3d 20 30 3b 0a 20 20 75 2e tx.s.z = 0;. u.
1eedd 63 63 2e 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63 cc.ctx.s.zMalloc
1eede 20 3d 20 30 3b 0a 20 20 75 2e 63 63 2e 63 74 78 = 0;. u.cc.ctx
1eedf 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 75 .s.xDel = 0;. u
1eee0 2e 63 63 2e 63 74 78 2e 73 2e 64 62 20 3d 20 64 .cc.ctx.s.db = d
1eee1 62 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e 69 73 b;. u.cc.ctx.is
1eee2 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 75 2e 63 Error = 0;. u.c
1eee3 63 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 30 3b c.ctx.pColl = 0;
1eee4 0a 20 20 69 66 28 20 75 2e 63 63 2e 63 74 78 2e . if( u.cc.ctx.
1eee5 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53 pFunc->flags & S
1eee6 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 QLITE_FUNC_NEEDC
1eee7 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 OLL ){. asser
1eee8 74 28 20 70 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b t( pOp>p->aOp );
1eee9 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 . assert( pOp
1eeea 5b 2d 31 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f [-1].p4type==P4_
1eeeb 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 20 20 61 COLLSEQ );. a
1eeec 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f ssert( pOp[-1].o
1eeed 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 pcode==OP_CollSe
1eeee 71 20 29 3b 0a 20 20 20 20 75 2e 63 63 2e 63 74 q );. u.cc.ct
1eeef 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 x.pColl = pOp[-1
1eef0 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a ].p4.pColl;. }.
1eef1 20 20 28 75 2e 63 63 2e 63 74 78 2e 70 46 75 6e (u.cc.ctx.pFun
1eef2 63 2d 3e 78 53 74 65 70 29 28 26 75 2e 63 63 2e c->xStep)(&u.cc.
1eef3 63 74 78 2c 20 75 2e 63 63 2e 6e 2c 20 75 2e 63 ctx, u.cc.n, u.c
1eef4 63 2e 61 70 56 61 6c 29 3b 0a 20 20 69 66 28 20 c.apVal);. if(
1eef5 75 2e 63 63 2e 63 74 78 2e 69 73 45 72 72 6f 72 u.cc.ctx.isError
1eef6 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 ){. sqlite3S
1eef7 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 etString(&p->zEr
1eef8 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 rMsg, db, "%s",
1eef9 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1eefa 78 74 28 26 75 2e 63 63 2e 63 74 78 2e 73 29 29 xt(&u.cc.ctx.s))
1eefb 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 63 2e ;. rc = u.cc.
1eefc 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d ctx.isError;. }
1eefd 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1eefe 6d 52 65 6c 65 61 73 65 28 26 75 2e 63 63 2e 63 mRelease(&u.cc.c
1eeff 74 78 2e 73 29 3b 0a 20 20 62 72 65 61 6b 3b 0a tx.s);. break;.
1ef00 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 }../* Opcode: Ag
1ef01 67 46 69 6e 61 6c 20 50 31 20 50 32 20 2a 20 50 gFinal P1 P2 * P
1ef02 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 4 *.**.** Execut
1ef03 65 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 e the finalizer
1ef04 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20 function for an
1ef05 61 67 67 72 65 67 61 74 65 2e 20 20 50 31 20 69 aggregate. P1 i
1ef06 73 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 s.** the memory
1ef07 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73 location that is
1ef08 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 the accumulator
1ef09 20 66 6f 72 20 74 68 65 20 61 67 67 72 65 67 61 for the aggrega
1ef0a 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 te..**.** P2 is
1ef0b 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 the number of ar
1ef0c 67 75 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65 guments that the
1ef0d 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 74 step function t
1ef0e 61 6b 65 73 20 61 6e 64 0a 2a 2a 20 50 34 20 69 akes and.** P4 i
1ef0f 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 s a pointer to t
1ef10 68 65 20 46 75 6e 63 44 65 66 20 66 6f 72 20 74 he FuncDef for t
1ef11 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 his function. T
1ef12 68 65 20 50 32 0a 2a 2a 20 61 72 67 75 6d 65 6e he P2.** argumen
1ef13 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79 t is not used by
1ef14 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 49 this opcode. I
1ef15 74 20 69 73 20 6f 6e 6c 79 20 74 68 65 72 65 20 t is only there
1ef16 74 6f 20 64 69 73 61 6d 62 69 67 75 61 74 65 0a to disambiguate.
1ef17 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 ** functions tha
1ef18 74 20 63 61 6e 20 74 61 6b 65 20 76 61 72 79 69 t can take varyi
1ef19 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 72 ng numbers of ar
1ef1a 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a guments. The.**
1ef1b 20 50 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20 P4 argument is
1ef1c 6f 6e 6c 79 20 6e 65 65 64 65 64 20 66 6f 72 20 only needed for
1ef1d 74 68 65 20 64 65 67 65 6e 65 72 61 74 65 20 63 the degenerate c
1ef1e 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 65 ase where.** the
1ef1f 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 77 step function w
1ef20 61 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c as not previousl
1ef21 79 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 63 61 73 y called..*/.cas
1ef22 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b e OP_AggFinal: {
1ef23 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1ef24 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1ef25 20 69 6e 74 6f 20 75 2e 63 64 20 2a 2f 0a 20 20 into u.cd */.
1ef26 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 23 65 6e 64 69 Mem *pMem;.#endi
1ef27 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1ef28 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1ef29 75 2e 63 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 u.cd */. assert
1ef2a 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 ( pOp->p1>0 && p
1ef2b 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p1<=p->nMem
1ef2c 29 3b 0a 20 20 75 2e 63 64 2e 70 4d 65 6d 20 3d );. u.cd.pMem =
1ef2d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1ef2e 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 75 1];. assert( (u
1ef2f 2e 63 64 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 .cd.pMem->flags
1ef30 26 20 7e 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d & ~(MEM_Null|MEM
1ef31 5f 41 67 67 29 29 3d 3d 30 20 29 3b 0a 20 20 72 _Agg))==0 );. r
1ef32 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d c = sqlite3VdbeM
1ef33 65 6d 46 69 6e 61 6c 69 7a 65 28 75 2e 63 64 2e emFinalize(u.cd.
1ef34 70 4d 65 6d 2c 20 70 4f 70 2d 3e 70 34 2e 70 46 pMem, pOp->p4.pF
1ef35 75 6e 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29 unc);. if( rc )
1ef36 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 {. sqlite3Set
1ef37 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d String(&p->zErrM
1ef38 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 sg, db, "%s", sq
1ef39 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
1ef3a 28 75 2e 63 64 2e 70 4d 65 6d 29 29 3b 0a 20 20 (u.cd.pMem));.
1ef3b 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 }. sqlite3VdbeC
1ef3c 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 75 2e hangeEncoding(u.
1ef3d 63 64 2e 70 4d 65 6d 2c 20 65 6e 63 6f 64 69 6e cd.pMem, encodin
1ef3e 67 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 g);. UPDATE_MAX
1ef3f 5f 42 4c 4f 42 53 49 5a 45 28 75 2e 63 64 2e 70 _BLOBSIZE(u.cd.p
1ef40 4d 65 6d 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 Mem);. if( sqli
1ef41 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 te3VdbeMemTooBig
1ef42 28 75 2e 63 64 2e 70 4d 65 6d 29 20 29 7b 0a 20 (u.cd.pMem) ){.
1ef43 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b goto too_big;
1ef44 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1ef45 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
1ef46 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 QLITE_OMIT_VACUU
1ef47 4d 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 M) && !defined(S
1ef48 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 QLITE_OMIT_ATTAC
1ef49 48 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 H)./* Opcode: Va
1ef4a 63 75 75 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a cuum * * * * *.*
1ef4b 2a 0a 2a 2a 20 56 61 63 75 75 6d 20 74 68 65 20 *.** Vacuum the
1ef4c 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 2e entire database.
1ef4d 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 69 This opcode wi
1ef4e 6c 6c 20 63 61 75 73 65 20 6f 74 68 65 72 20 76 ll cause other v
1ef4f 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e irtual.** machin
1ef50 65 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 es to be created
1ef51 20 61 6e 64 20 72 75 6e 2e 20 20 49 74 20 6d 61 and run. It ma
1ef52 79 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 y not be called
1ef53 66 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 61 from within.** a
1ef54 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f transaction..*/
1ef55 0a 63 61 73 65 20 4f 50 5f 56 61 63 75 75 6d 3a .case OP_Vacuum:
1ef56 20 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 {. if( sqlite3
1ef57 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 SafetyOff(db) )
1ef58 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1ef59 6f 5f 6d 69 73 75 73 65 3b 20 0a 20 20 72 63 20 o_misuse; . rc
1ef5a 3d 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75 = sqlite3RunVacu
1ef5b 75 6d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 um(&p->zErrMsg,
1ef5c 64 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 db);. if( sqlit
1ef5d 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 e3SafetyOn(db) )
1ef5e 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1ef5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 62 72 65 to_misuse;. bre
1ef60 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 ak;.}.#endif..#i
1ef61 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
1ef62 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
1ef63 4d 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e M)./* Opcode: In
1ef64 63 72 56 61 63 75 75 6d 20 50 31 20 50 32 20 2a crVacuum P1 P2 *
1ef65 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66 6f * *.**.** Perfo
1ef66 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 rm a single step
1ef67 20 6f 66 20 74 68 65 20 69 6e 63 72 65 6d 65 6e of the incremen
1ef68 74 61 6c 20 76 61 63 75 75 6d 20 70 72 6f 63 65 tal vacuum proce
1ef69 64 75 72 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 50 dure on.** the P
1ef6a 31 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 1 database. If t
1ef6b 68 65 20 76 61 63 75 75 6d 20 68 61 73 20 66 69 he vacuum has fi
1ef6c 6e 69 73 68 65 64 2c 20 6a 75 6d 70 20 74 6f 20 nished, jump to
1ef6d 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 50 instruction.** P
1ef6e 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 2. Otherwise, fa
1ef6f 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 ll through to th
1ef70 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 e next instructi
1ef71 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 on..*/.case OP_I
1ef72 6e 63 72 56 61 63 75 75 6d 3a 20 7b 20 20 20 20 ncrVacuum: {
1ef73 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 /* jump */.#
1ef74 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1ef75 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1ef76 6e 74 6f 20 75 2e 63 65 20 2a 2f 0a 20 20 42 74 nto u.ce */. Bt
1ef77 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66 ree *pBt;.#endif
1ef78 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1ef79 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1ef7a 2e 63 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 .ce */.. assert
1ef7b 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1ef7c 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 pOp->p1<db->nDb
1ef7d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d );. assert( (p-
1ef7e 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c >btreeMask & (1<
1ef7f 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b <pOp->p1))!=0 );
1ef80 0a 20 20 75 2e 63 65 2e 70 42 74 20 3d 20 64 62 . u.ce.pBt = db
1ef81 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 ->aDb[pOp->p1].p
1ef82 42 74 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 Bt;. rc = sqlit
1ef83 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 e3BtreeIncrVacuu
1ef84 6d 28 75 2e 63 65 2e 70 42 74 29 3b 0a 20 20 69 m(u.ce.pBt);. i
1ef85 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f f( rc==SQLITE_DO
1ef86 4e 45 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 NE ){. pc = p
1ef87 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 Op->p2 - 1;.
1ef88 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1ef89 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 }. break;.}.#
1ef8a 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65 endif../* Opcode
1ef8b 3a 20 45 78 70 69 72 65 20 50 31 20 2a 20 2a 20 : Expire P1 * *
1ef8c 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 61 75 73 65 20 * *.**.** Cause
1ef8d 70 72 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 precompiled stat
1ef8e 65 6d 65 6e 74 73 20 74 6f 20 62 65 63 6f 6d 65 ements to become
1ef8f 20 65 78 70 69 72 65 64 2e 20 41 6e 20 65 78 70 expired. An exp
1ef90 69 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a ired statement.*
1ef91 2a 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 20 * fails with an
1ef92 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 53 51 error code of SQ
1ef93 4c 49 54 45 5f 53 43 48 45 4d 41 20 69 66 20 69 LITE_SCHEMA if i
1ef94 74 20 69 73 20 65 76 65 72 20 65 78 65 63 75 74 t is ever execut
1ef95 65 64 20 0a 2a 2a 20 28 76 69 61 20 73 71 6c 69 ed .** (via sqli
1ef96 74 65 33 5f 73 74 65 70 28 29 29 2e 0a 2a 2a 20 te3_step())..**
1ef97 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 30 2c 20 .** If P1 is 0,
1ef98 74 68 65 6e 20 61 6c 6c 20 53 51 4c 20 73 74 61 then all SQL sta
1ef99 74 65 6d 65 6e 74 73 20 62 65 63 6f 6d 65 20 65 tements become e
1ef9a 78 70 69 72 65 64 2e 20 49 66 20 50 31 20 69 73 xpired. If P1 is
1ef9b 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 non-zero,.** th
1ef9c 65 6e 20 6f 6e 6c 79 20 74 68 65 20 63 75 72 72 en only the curr
1ef9d 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 ently executing
1ef9e 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 66 66 statement is aff
1ef9f 65 63 74 65 64 2e 20 0a 2a 2f 0a 63 61 73 65 20 ected. .*/.case
1efa0 4f 50 5f 45 78 70 69 72 65 3a 20 7b 0a 20 20 69 OP_Expire: {. i
1efa1 66 28 20 21 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 f( !pOp->p1 ){.
1efa2 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 sqlite3Expire
1efa3 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e PreparedStatemen
1efa4 74 73 28 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b ts(db);. }else{
1efa5 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 . p->expired
1efa6 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b = 1;. }. break
1efa7 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ;.}..#ifndef SQL
1efa8 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
1efa9 43 41 43 48 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a CACHE./* Opcode:
1efaa 20 54 61 62 6c 65 4c 6f 63 6b 20 50 31 20 50 32 TableLock P1 P2
1efab 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f P3 P4 *.**.** O
1efac 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 btain a lock on
1efad 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 a particular tab
1efae 6c 65 2e 20 54 68 69 73 20 69 6e 73 74 72 75 63 le. This instruc
1efaf 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 tion is only use
1efb0 64 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 73 68 d when.** the sh
1efb1 61 72 65 64 2d 63 61 63 68 65 20 66 65 61 74 75 ared-cache featu
1efb2 72 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 0a re is enabled. .
1efb3 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 **.** P1 is the
1efb4 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 index of the dat
1efb5 61 62 61 73 65 20 69 6e 20 73 71 6c 69 74 65 33 abase in sqlite3
1efb6 2e 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 .aDb[] of the da
1efb7 74 61 62 61 73 65 0a 2a 2a 20 6f 6e 20 77 68 69 tabase.** on whi
1efb8 63 68 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 ch the lock is a
1efb9 63 71 75 69 72 65 64 2e 20 20 41 20 72 65 61 64 cquired. A read
1efba 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 lock is obtained
1efbb 20 69 66 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a 20 if P3==0 or.**
1efbc 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 a write lock if
1efbd 50 33 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 P3==1..**.** P2
1efbe 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 6f contains the roo
1efbf 74 2d 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 t-page of the ta
1efc0 62 6c 65 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a ble to lock..**.
1efc1 2a 2a 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 ** P4 contains a
1efc2 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
1efc3 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c name of the tabl
1efc4 65 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 20 e being locked.
1efc5 54 68 69 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 This is only.**
1efc6 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 used to generate
1efc7 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
1efc8 65 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 e if the lock ca
1efc9 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 nnot be obtained
1efca 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 61 62 ..*/.case OP_Tab
1efcb 6c 65 4c 6f 63 6b 3a 20 7b 0a 20 20 75 38 20 69 leLock: {. u8 i
1efcc 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28 75 38 sWriteLock = (u8
1efcd 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 66 28 20 )pOp->p3;. if(
1efce 69 73 57 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 30 isWriteLock || 0
1efcf 3d 3d 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c ==(db->flags&SQL
1efd0 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 ITE_ReadUncommit
1efd1 74 65 64 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 ted) ){. int
1efd2 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20 p1 = pOp->p1; .
1efd3 20 20 20 61 73 73 65 72 74 28 20 70 31 3e 3d 30 assert( p1>=0
1efd4 20 26 26 20 70 31 3c 64 62 2d 3e 6e 44 62 20 29 && p1<db->nDb )
1efd5 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 ;. assert( (p
1efd6 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 ->btreeMask & (1
1efd7 3c 3c 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 20 <<p1))!=0 );.
1efd8 20 61 73 73 65 72 74 28 20 69 73 57 72 69 74 65 assert( isWrite
1efd9 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69 Lock==0 || isWri
1efda 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 teLock==1 );.
1efdb 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1efdc 65 65 4c 6f 63 6b 54 61 62 6c 65 28 64 62 2d 3e eeLockTable(db->
1efdd 61 44 62 5b 70 31 5d 2e 70 42 74 2c 20 70 4f 70 aDb[p1].pBt, pOp
1efde 2d 3e 70 32 2c 20 69 73 57 72 69 74 65 4c 6f 63 ->p2, isWriteLoc
1efdf 6b 29 3b 0a 20 20 20 20 69 66 28 20 28 72 63 26 k);. if( (rc&
1efe0 30 78 46 46 29 3d 3d 53 51 4c 49 54 45 5f 4c 4f 0xFF)==SQLITE_LO
1efe1 43 4b 45 44 20 29 7b 0a 20 20 20 20 20 20 63 6f CKED ){. co
1efe2 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f nst char *z = pO
1efe3 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 73 p->p4.z;. s
1efe4 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
1efe5 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c &p->zErrMsg, db,
1efe6 20 22 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 "database table
1efe7 20 69 73 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c is locked: %s",
1efe8 20 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 z);. }. }.
1efe9 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1efea 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1efeb 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a SHARED_CACHE */.
1efec 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1efed 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1efee 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 42 65 E./* Opcode: VBe
1efef 67 69 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a gin * * * P4 *.*
1eff0 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 62 65 20 61 *.** P4 may be a
1eff1 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 pointer to an s
1eff2 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 qlite3_vtab stru
1eff3 63 74 75 72 65 2e 20 49 66 20 73 6f 2c 20 63 61 cture. If so, ca
1eff4 6c 6c 20 74 68 65 20 0a 2a 2a 20 78 42 65 67 69 ll the .** xBegi
1eff5 6e 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61 n method for tha
1eff6 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 t table..**.** A
1eff7 6c 73 6f 2c 20 77 68 65 74 68 65 72 20 6f 72 20 lso, whether or
1eff8 6e 6f 74 20 50 34 20 69 73 20 73 65 74 2c 20 63 not P4 is set, c
1eff9 68 65 63 6b 20 74 68 61 74 20 74 68 69 73 20 69 heck that this i
1effa 73 20 6e 6f 74 20 62 65 69 6e 67 20 63 61 6c 6c s not being call
1effb 65 64 20 66 72 6f 6d 0a 2a 2a 20 77 69 74 68 69 ed from.** withi
1effc 6e 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 n a callback to
1effd 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 a virtual table
1effe 78 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 2e 20 xSync() method.
1efff 49 66 20 69 74 20 69 73 2c 20 74 68 65 20 65 72 If it is, the er
1f000 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c ror.** code will
1f001 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 be set to SQLIT
1f002 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 63 61 73 E_LOCKED..*/.cas
1f003 65 20 4f 50 5f 56 42 65 67 69 6e 3a 20 7b 0a 23 e OP_VBegin: {.#
1f004 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1f005 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1f006 6e 74 6f 20 75 2e 63 66 20 2a 2f 0a 20 20 56 54 nto u.cf */. VT
1f007 61 62 6c 65 20 2a 70 56 54 61 62 3b 0a 23 65 6e able *pVTab;.#en
1f008 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1f009 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1f00a 6f 20 75 2e 63 66 20 2a 2f 0a 20 20 75 2e 63 66 o u.cf */. u.cf
1f00b 2e 70 56 54 61 62 20 3d 20 70 4f 70 2d 3e 70 34 .pVTab = pOp->p4
1f00c 2e 70 56 74 61 62 3b 0a 20 20 72 63 20 3d 20 73 .pVtab;. rc = s
1f00d 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 28 qlite3VtabBegin(
1f00e 64 62 2c 20 75 2e 63 66 2e 70 56 54 61 62 29 3b db, u.cf.pVTab);
1f00f 0a 20 20 69 66 28 20 75 2e 63 66 2e 70 56 54 61 . if( u.cf.pVTa
1f010 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 b ){. sqlite3
1f011 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 DbFree(db, p->zE
1f012 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a rrMsg);. p->z
1f013 45 72 72 4d 73 67 20 3d 20 75 2e 63 66 2e 70 56 ErrMsg = u.cf.pV
1f014 54 61 62 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72 Tab->pVtab->zErr
1f015 4d 73 67 3b 0a 20 20 20 20 75 2e 63 66 2e 70 56 Msg;. u.cf.pV
1f016 54 61 62 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72 Tab->pVtab->zErr
1f017 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 Msg = 0;. }. b
1f018 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f reak;.}.#endif /
1f019 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 * SQLITE_OMIT_VI
1f01a 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 RTUALTABLE */..#
1f01b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1f01c 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
1f01d 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43 72 65 61 /* Opcode: VCrea
1f01e 74 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a te P1 * * P4 *.*
1f01f 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e *.** P4 is the n
1f020 61 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c ame of a virtual
1f021 20 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61 table in databa
1f022 73 65 20 50 31 2e 20 43 61 6c 6c 20 74 68 65 20 se P1. Call the
1f023 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a xCreate method.*
1f024 2a 20 66 6f 72 20 74 68 61 74 20 74 61 62 6c 65 * for that table
1f025 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 43 72 ..*/.case OP_VCr
1f026 65 61 74 65 3a 20 7b 0a 20 20 72 63 20 3d 20 73 eate: {. rc = s
1f027 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72 qlite3VtabCallCr
1f028 65 61 74 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 eate(db, pOp->p1
1f029 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 26 70 2d , pOp->p4.z, &p-
1f02a 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 62 72 65 >zErrMsg);. bre
1f02b 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ak;.}.#endif /*
1f02c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1f02d 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 UALTABLE */..#if
1f02e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1f02f 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a _VIRTUALTABLE./*
1f030 20 4f 70 63 6f 64 65 3a 20 56 44 65 73 74 72 6f Opcode: VDestro
1f031 79 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a y P1 * * P4 *.**
1f032 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e 61 .** P4 is the na
1f033 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 me of a virtual
1f034 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61 73 table in databas
1f035 65 20 50 31 2e 20 20 43 61 6c 6c 20 74 68 65 20 e P1. Call the
1f036 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a xDestroy method.
1f037 2a 2a 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 ** of that table
1f038 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 44 65 ..*/.case OP_VDe
1f039 73 74 72 6f 79 3a 20 7b 0a 20 20 70 2d 3e 69 6e stroy: {. p->in
1f03a 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 32 3b 0a VtabMethod = 2;.
1f03b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 rc = sqlite3Vt
1f03c 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28 64 62 abCallDestroy(db
1f03d 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e , pOp->p1, pOp->
1f03e 70 34 2e 7a 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 p4.z);. p->inVt
1f03f 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 abMethod = 0;.
1f040 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
1f041 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 /* SQLITE_OMIT_V
1f042 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a IRTUALTABLE */..
1f043 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1f044 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1f045 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4f 70 65 ./* Opcode: VOpe
1f046 6e 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a n P1 * * P4 *.**
1f047 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e .** P4 is a poin
1f048 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c ter to a virtual
1f049 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 table object, a
1f04a 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 n sqlite3_vtab s
1f04b 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 50 31 20 tructure..** P1
1f04c 69 73 20 61 20 63 75 72 73 6f 72 20 6e 75 6d 62 is a cursor numb
1f04d 65 72 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 er. This opcode
1f04e 20 6f 70 65 6e 73 20 61 20 63 75 72 73 6f 72 20 opens a cursor
1f04f 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a to the virtual.*
1f050 2a 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 * table and stor
1f051 65 73 20 74 68 61 74 20 63 75 72 73 6f 72 20 69 es that cursor i
1f052 6e 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 n P1..*/.case OP
1f053 5f 56 4f 70 65 6e 3a 20 7b 0a 23 69 66 20 30 20 _VOpen: {.#if 0
1f054 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1f055 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1f056 2e 63 67 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 .cg */. VdbeCur
1f057 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 73 71 6c sor *pCur;. sql
1f058 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
1f059 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 *pVtabCursor;.
1f05a 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
1f05b 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f Vtab;. sqlite3_
1f05c 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b module *pModule;
1f05d 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1f05e 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1f05f 20 69 6e 74 6f 20 75 2e 63 67 20 2a 2f 0a 0a 20 into u.cg */..
1f060 20 75 2e 63 67 2e 70 43 75 72 20 3d 20 30 3b 0a u.cg.pCur = 0;.
1f061 20 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 u.cg.pVtabCurs
1f062 6f 72 20 3d 20 30 3b 0a 20 20 75 2e 63 67 2e 70 or = 0;. u.cg.p
1f063 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 Vtab = pOp->p4.p
1f064 56 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 75 Vtab->pVtab;. u
1f065 2e 63 67 2e 70 4d 6f 64 75 6c 65 20 3d 20 28 73 .cg.pModule = (s
1f066 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 qlite3_module *)
1f067 75 2e 63 67 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 u.cg.pVtab->pMod
1f068 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 75 2e ule;. assert(u.
1f069 63 67 2e 70 56 74 61 62 20 26 26 20 75 2e 63 67 cg.pVtab && u.cg
1f06a 2e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28 .pModule);. if(
1f06b 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
1f06c 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f f(db) ) goto abo
1f06d 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1f06e 3b 0a 20 20 72 63 20 3d 20 75 2e 63 67 2e 70 4d ;. rc = u.cg.pM
1f06f 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 75 2e 63 odule->xOpen(u.c
1f070 67 2e 70 56 74 61 62 2c 20 26 75 2e 63 67 2e 70 g.pVtab, &u.cg.p
1f071 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 73 VtabCursor);. s
1f072 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1f073 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 p->zErrMsg);.
1f074 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 p->zErrMsg = u.c
1f075 67 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 g.pVtab->zErrMsg
1f076 3b 0a 20 20 75 2e 63 67 2e 70 56 74 61 62 2d 3e ;. u.cg.pVtab->
1f077 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 zErrMsg = 0;. i
1f078 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1f079 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 On(db) ) goto ab
1f07a 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1f07b 65 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f e;. if( SQLITE_
1f07c 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 2f 2a OK==rc ){. /*
1f07d 20 49 6e 69 74 69 61 6c 69 7a 65 20 73 71 6c 69 Initialize sqli
1f07e 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 te3_vtab_cursor
1f07f 62 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a 20 20 base class */.
1f080 20 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 u.cg.pVtabCurs
1f081 6f 72 2d 3e 70 56 74 61 62 20 3d 20 75 2e 63 67 or->pVtab = u.cg
1f082 2e 70 56 74 61 62 3b 0a 0a 20 20 20 20 2f 2a 20 .pVtab;.. /*
1f083 49 6e 69 74 69 61 6c 69 73 65 20 76 64 62 65 20 Initialise vdbe
1f084 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f cursor object */
1f085 0a 20 20 20 20 75 2e 63 67 2e 70 43 75 72 20 3d . u.cg.pCur =
1f086 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 allocateCursor(
1f087 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 2d p, pOp->p1, 0, -
1f088 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 75 1, 0);. if( u
1f089 2e 63 67 2e 70 43 75 72 20 29 7b 0a 20 20 20 20 .cg.pCur ){.
1f08a 20 20 75 2e 63 67 2e 70 43 75 72 2d 3e 70 56 74 u.cg.pCur->pVt
1f08b 61 62 43 75 72 73 6f 72 20 3d 20 75 2e 63 67 2e abCursor = u.cg.
1f08c 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 pVtabCursor;.
1f08d 20 20 20 75 2e 63 67 2e 70 43 75 72 2d 3e 70 4d u.cg.pCur->pM
1f08e 6f 64 75 6c 65 20 3d 20 75 2e 63 67 2e 70 56 74 odule = u.cg.pVt
1f08f 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 2d abCursor->pVtab-
1f090 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 65 >pModule;. }e
1f091 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d lse{. db->m
1f092 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
1f093 0a 20 20 20 20 20 20 75 2e 63 67 2e 70 4d 6f 64 . u.cg.pMod
1f094 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 75 2e 63 67 ule->xClose(u.cg
1f095 2e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 .pVtabCursor);.
1f096 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b }. }. break
1f097 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
1f098 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1f099 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 LTABLE */..#ifnd
1f09a 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1f09b 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f IRTUALTABLE./* O
1f09c 70 63 6f 64 65 3a 20 56 46 69 6c 74 65 72 20 50 pcode: VFilter P
1f09d 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 1 P2 P3 P4 *.**.
1f09e 2a 2a 20 50 31 20 69 73 20 61 20 63 75 72 73 6f ** P1 is a curso
1f09f 72 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 56 r opened using V
1f0a0 4f 70 65 6e 2e 20 20 50 32 20 69 73 20 61 6e 20 Open. P2 is an
1f0a1 61 64 64 72 65 73 73 20 74 6f 20 6a 75 6d 70 20 address to jump
1f0a2 74 6f 20 69 66 0a 2a 2a 20 74 68 65 20 66 69 6c to if.** the fil
1f0a3 74 65 72 65 64 20 72 65 73 75 6c 74 20 73 65 74 tered result set
1f0a4 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a is empty..**.**
1f0a5 20 50 34 20 69 73 20 65 69 74 68 65 72 20 4e 55 P4 is either NU
1f0a6 4c 4c 20 6f 72 20 61 20 73 74 72 69 6e 67 20 74 LL or a string t
1f0a7 68 61 74 20 77 61 73 20 67 65 6e 65 72 61 74 65 hat was generate
1f0a8 64 20 62 79 20 74 68 65 20 78 42 65 73 74 49 6e d by the xBestIn
1f0a9 64 65 78 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 dex.** method of
1f0aa 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 the module. Th
1f0ab 65 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e e interpretation
1f0ac 20 6f 66 20 74 68 65 20 50 34 20 73 74 72 69 6e of the P4 strin
1f0ad 67 20 69 73 20 6c 65 66 74 0a 2a 2a 20 74 6f 20 g is left.** to
1f0ae 74 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 the module imple
1f0af 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a mentation..**.**
1f0b0 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 This opcode inv
1f0b1 6f 6b 65 73 20 74 68 65 20 78 46 69 6c 74 65 72 okes the xFilter
1f0b2 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68 65 20 76 method on the v
1f0b3 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 70 65 irtual table spe
1f0b4 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 50 31 2e cified.** by P1.
1f0b5 20 20 54 68 65 20 69 6e 74 65 67 65 72 20 71 75 The integer qu
1f0b6 65 72 79 20 70 6c 61 6e 20 70 61 72 61 6d 65 74 ery plan paramet
1f0b7 65 72 20 74 6f 20 78 46 69 6c 74 65 72 20 69 73 er to xFilter is
1f0b8 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
1f0b9 74 65 72 0a 2a 2a 20 50 33 2e 20 52 65 67 69 73 ter.** P3. Regis
1f0ba 74 65 72 20 50 33 2b 31 20 73 74 6f 72 65 73 20 ter P3+1 stores
1f0bb 74 68 65 20 61 72 67 63 20 70 61 72 61 6d 65 74 the argc paramet
1f0bc 65 72 20 74 6f 20 62 65 20 70 61 73 73 65 64 20 er to be passed
1f0bd 74 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c 74 65 to the.** xFilte
1f0be 72 20 6d 65 74 68 6f 64 2e 20 52 65 67 69 73 74 r method. Regist
1f0bf 65 72 73 20 50 33 2b 32 2e 2e 50 33 2b 31 2b 61 ers P3+2..P3+1+a
1f0c0 72 67 63 20 61 72 65 20 74 68 65 20 61 72 67 63 rgc are the argc
1f0c1 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 70 .** additional p
1f0c2 61 72 61 6d 65 74 65 72 73 20 77 68 69 63 68 20 arameters which
1f0c3 61 72 65 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a are passed to.**
1f0c4 20 78 46 69 6c 74 65 72 20 61 73 20 61 72 67 76 xFilter as argv
1f0c5 2e 20 52 65 67 69 73 74 65 72 20 50 33 2b 32 20 . Register P3+2
1f0c6 62 65 63 6f 6d 65 73 20 61 72 67 76 5b 30 5d 20 becomes argv[0]
1f0c7 77 68 65 6e 20 70 61 73 73 65 64 20 74 6f 20 78 when passed to x
1f0c8 46 69 6c 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 Filter..**.** A
1f0c9 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 74 6f 20 jump is made to
1f0ca 50 32 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 P2 if the result
1f0cb 20 73 65 74 20 61 66 74 65 72 20 66 69 6c 74 65 set after filte
1f0cc 72 69 6e 67 20 77 6f 75 6c 64 20 62 65 20 65 6d ring would be em
1f0cd 70 74 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f pty..*/.case OP_
1f0ce 56 46 69 6c 74 65 72 3a 20 7b 20 20 20 2f 2a 20 VFilter: { /*
1f0cf 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f jump */.#if 0 /
1f0d0 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1f0d1 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 s moved into u.c
1f0d2 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 3b h */. int nArg;
1f0d3 0a 20 20 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 . int iQuery;.
1f0d4 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d const sqlite3_m
1f0d5 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a odule *pModule;.
1f0d6 20 20 4d 65 6d 20 2a 70 51 75 65 72 79 3b 0a 20 Mem *pQuery;.
1f0d7 20 4d 65 6d 20 2a 70 41 72 67 63 3b 0a 20 20 73 Mem *pArgc;. s
1f0d8 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
1f0d9 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b or *pVtabCursor;
1f0da 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 . sqlite3_vtab
1f0db 2a 70 56 74 61 62 3b 0a 20 20 56 64 62 65 43 75 *pVtab;. VdbeCu
1f0dc 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e rsor *pCur;. in
1f0dd 74 20 72 65 73 3b 0a 20 20 69 6e 74 20 69 3b 0a t res;. int i;.
1f0de 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 23 Mem **apArg;.#
1f0df 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1f0e0 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1f0e1 6e 74 6f 20 75 2e 63 68 20 2a 2f 0a 0a 20 20 75 nto u.ch */.. u
1f0e2 2e 63 68 2e 70 51 75 65 72 79 20 3d 20 26 70 2d .ch.pQuery = &p-
1f0e3 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a >aMem[pOp->p3];.
1f0e4 20 20 75 2e 63 68 2e 70 41 72 67 63 20 3d 20 26 u.ch.pArgc = &
1f0e5 75 2e 63 68 2e 70 51 75 65 72 79 5b 31 5d 3b 0a u.ch.pQuery[1];.
1f0e6 20 20 75 2e 63 68 2e 70 43 75 72 20 3d 20 70 2d u.ch.pCur = p-
1f0e7 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b >apCsr[pOp->p1];
1f0e8 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 . REGISTER_TRAC
1f0e9 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 63 68 2e E(pOp->p3, u.ch.
1f0ea 70 51 75 65 72 79 29 3b 0a 20 20 61 73 73 65 72 pQuery);. asser
1f0eb 74 28 20 75 2e 63 68 2e 70 43 75 72 2d 3e 70 56 t( u.ch.pCur->pV
1f0ec 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 tabCursor );. u
1f0ed 2e 63 68 2e 70 56 74 61 62 43 75 72 73 6f 72 20 .ch.pVtabCursor
1f0ee 3d 20 75 2e 63 68 2e 70 43 75 72 2d 3e 70 56 74 = u.ch.pCur->pVt
1f0ef 61 62 43 75 72 73 6f 72 3b 0a 20 20 75 2e 63 68 abCursor;. u.ch
1f0f0 2e 70 56 74 61 62 20 3d 20 75 2e 63 68 2e 70 56 .pVtab = u.ch.pV
1f0f1 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 tabCursor->pVtab
1f0f2 3b 0a 20 20 75 2e 63 68 2e 70 4d 6f 64 75 6c 65 ;. u.ch.pModule
1f0f3 20 3d 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e 70 = u.ch.pVtab->p
1f0f4 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20 47 72 Module;.. /* Gr
1f0f5 61 62 20 74 68 65 20 69 6e 64 65 78 20 6e 75 6d ab the index num
1f0f6 62 65 72 20 61 6e 64 20 61 72 67 63 20 70 61 72 ber and argc par
1f0f7 61 6d 65 74 65 72 73 20 2a 2f 0a 20 20 61 73 73 ameters */. ass
1f0f8 65 72 74 28 20 28 75 2e 63 68 2e 70 51 75 65 72 ert( (u.ch.pQuer
1f0f9 79 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 y->flags&MEM_Int
1f0fa 29 21 3d 30 20 26 26 20 75 2e 63 68 2e 70 41 72 )!=0 && u.ch.pAr
1f0fb 67 63 2d 3e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 49 gc->flags==MEM_I
1f0fc 6e 74 20 29 3b 0a 20 20 75 2e 63 68 2e 6e 41 72 nt );. u.ch.nAr
1f0fd 67 20 3d 20 28 69 6e 74 29 75 2e 63 68 2e 70 41 g = (int)u.ch.pA
1f0fe 72 67 63 2d 3e 75 2e 69 3b 0a 20 20 75 2e 63 68 rgc->u.i;. u.ch
1f0ff 2e 69 51 75 65 72 79 20 3d 20 28 69 6e 74 29 75 .iQuery = (int)u
1f100 2e 63 68 2e 70 51 75 65 72 79 2d 3e 75 2e 69 3b .ch.pQuery->u.i;
1f101 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 .. /* Invoke th
1f102 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 e xFilter method
1f103 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 2e 63 68 */. {. u.ch
1f104 2e 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e .res = 0;. u.
1f105 63 68 2e 61 70 41 72 67 20 3d 20 70 2d 3e 61 70 ch.apArg = p->ap
1f106 41 72 67 3b 0a 20 20 20 20 66 6f 72 28 75 2e 63 Arg;. for(u.c
1f107 68 2e 69 20 3d 20 30 3b 20 75 2e 63 68 2e 69 3c h.i = 0; u.ch.i<
1f108 75 2e 63 68 2e 6e 41 72 67 3b 20 75 2e 63 68 2e u.ch.nArg; u.ch.
1f109 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 2e 63 68 i++){. u.ch
1f10a 2e 61 70 41 72 67 5b 75 2e 63 68 2e 69 5d 20 3d .apArg[u.ch.i] =
1f10b 20 26 75 2e 63 68 2e 70 41 72 67 63 5b 75 2e 63 &u.ch.pArgc[u.c
1f10c 68 2e 69 2b 31 5d 3b 0a 20 20 20 20 20 20 73 71 h.i+1];. sq
1f10d 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 lite3VdbeMemStor
1f10e 65 54 79 70 65 28 75 2e 63 68 2e 61 70 41 72 67 eType(u.ch.apArg
1f10f 5b 75 2e 63 68 2e 69 5d 29 3b 0a 20 20 20 20 7d [u.ch.i]);. }
1f110 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 .. if( sqlite
1f111 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 3SafetyOff(db) )
1f112 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1f113 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 70 to_misuse;. p
1f114 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d ->inVtabMethod =
1f115 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 1;. rc = u.c
1f116 68 2e 70 4d 6f 64 75 6c 65 2d 3e 78 46 69 6c 74 h.pModule->xFilt
1f117 65 72 28 75 2e 63 68 2e 70 56 74 61 62 43 75 72 er(u.ch.pVtabCur
1f118 73 6f 72 2c 20 75 2e 63 68 2e 69 51 75 65 72 79 sor, u.ch.iQuery
1f119 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 75 2e 63 , pOp->p4.z, u.c
1f11a 68 2e 6e 41 72 67 2c 20 75 2e 63 68 2e 61 70 41 h.nArg, u.ch.apA
1f11b 72 67 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 rg);. p->inVt
1f11c 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 abMethod = 0;.
1f11d 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1f11e 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b db, p->zErrMsg);
1f11f 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 . p->zErrMsg
1f120 3d 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e 7a 45 = u.ch.pVtab->zE
1f121 72 72 4d 73 67 3b 0a 20 20 20 20 75 2e 63 68 2e rrMsg;. u.ch.
1f122 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d pVtab->zErrMsg =
1f123 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 0;. if( rc==
1f124 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1f125 20 20 20 75 2e 63 68 2e 72 65 73 20 3d 20 75 2e u.ch.res = u.
1f126 63 68 2e 70 4d 6f 64 75 6c 65 2d 3e 78 45 6f 66 ch.pModule->xEof
1f127 28 75 2e 63 68 2e 70 56 74 61 62 43 75 72 73 6f (u.ch.pVtabCurso
1f128 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 r);. }. if
1f129 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1f12a 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f n(db) ) goto abo
1f12b 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1f12c 3b 0a 0a 20 20 20 20 69 66 28 20 75 2e 63 68 2e ;.. if( u.ch.
1f12d 72 65 73 20 29 7b 0a 20 20 20 20 20 20 70 63 20 res ){. pc
1f12e 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1f12f 20 20 20 7d 0a 20 20 7d 0a 20 20 75 2e 63 68 2e }. }. u.ch.
1f130 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 pCur->nullRow =
1f131 30 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 0;.. break;.}.#
1f132 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1f133 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1f134 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 E */..#ifndef SQ
1f135 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1f136 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 LTABLE./* Opcode
1f137 3a 20 56 43 6f 6c 75 6d 6e 20 50 31 20 50 32 20 : VColumn P1 P2
1f138 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f P3 * *.**.** Sto
1f139 72 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 re the value of
1f13a 74 68 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e the P2-th column
1f13b 20 6f 66 0a 2a 2a 20 74 68 65 20 72 6f 77 20 6f of.** the row o
1f13c 66 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 f the virtual-ta
1f13d 62 6c 65 20 74 68 61 74 20 74 68 65 20 0a 2a 2a ble that the .**
1f13e 20 50 31 20 63 75 72 73 6f 72 20 69 73 20 70 6f P1 cursor is po
1f13f 69 6e 74 69 6e 67 20 74 6f 20 69 6e 74 6f 20 72 inting to into r
1f140 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2f 0a 63 egister P3..*/.c
1f141 61 73 65 20 4f 50 5f 56 43 6f 6c 75 6d 6e 3a 20 ase OP_VColumn:
1f142 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 {.#if 0 /* loca
1f143 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1f144 64 20 69 6e 74 6f 20 75 2e 63 69 20 2a 2f 0a 20 d into u.ci */.
1f145 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
1f146 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 Vtab;. const sq
1f147 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d lite3_module *pM
1f148 6f 64 75 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70 44 odule;. Mem *pD
1f149 65 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 est;. sqlite3_c
1f14a 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b ontext sContext;
1f14b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1f14c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1f14d 20 69 6e 74 6f 20 75 2e 63 69 20 2a 2f 0a 0a 20 into u.ci */..
1f14e 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 VdbeCursor *pCu
1f14f 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 r = p->apCsr[pOp
1f150 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1f151 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 pCur->pVtabCurs
1f152 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 or );. assert(
1f153 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 pOp->p3>0 && pOp
1f154 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p3<=p->nMem );
1f155 0a 20 20 75 2e 63 69 2e 70 44 65 73 74 20 3d 20 . u.ci.pDest =
1f156 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 &p->aMem[pOp->p3
1f157 5d 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 6e ];. if( pCur->n
1f158 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 73 71 ullRow ){. sq
1f159 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e lite3VdbeMemSetN
1f15a 75 6c 6c 28 75 2e 63 69 2e 70 44 65 73 74 29 3b ull(u.ci.pDest);
1f15b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a . break;. }.
1f15c 20 20 75 2e 63 69 2e 70 56 74 61 62 20 3d 20 70 u.ci.pVtab = p
1f15d 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 Cur->pVtabCursor
1f15e 2d 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 69 2e ->pVtab;. u.ci.
1f15f 70 4d 6f 64 75 6c 65 20 3d 20 75 2e 63 69 2e 70 pModule = u.ci.p
1f160 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 Vtab->pModule;.
1f161 20 61 73 73 65 72 74 28 20 75 2e 63 69 2e 70 4d assert( u.ci.pM
1f162 6f 64 75 6c 65 2d 3e 78 43 6f 6c 75 6d 6e 20 29 odule->xColumn )
1f163 3b 0a 20 20 6d 65 6d 73 65 74 28 26 75 2e 63 69 ;. memset(&u.ci
1f164 2e 73 43 6f 6e 74 65 78 74 2c 20 30 2c 20 73 69 .sContext, 0, si
1f165 7a 65 6f 66 28 75 2e 63 69 2e 73 43 6f 6e 74 65 zeof(u.ci.sConte
1f166 78 74 29 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 xt));.. /* The
1f167 6f 75 74 70 75 74 20 63 65 6c 6c 20 6d 61 79 20 output cell may
1f168 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 62 already have a b
1f169 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 2e uffer allocated.
1f16a 20 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 Move. ** the c
1f16b 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 urrent contents
1f16c 74 6f 20 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74 to u.ci.sContext
1f16d 2e 73 20 73 6f 20 69 6e 20 63 61 73 65 20 74 68 .s so in case th
1f16e 65 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 0a e user-function.
1f16f 20 20 2a 2a 20 63 61 6e 20 75 73 65 20 74 68 65 ** can use the
1f170 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 already allocat
1f171 65 64 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 ed buffer instea
1f172 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 d of allocating
1f173 61 0a 20 20 2a 2a 20 6e 65 77 20 6f 6e 65 2e 0a a. ** new one..
1f174 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 */. sqlite3Vd
1f175 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 63 69 2e beMemMove(&u.ci.
1f176 73 43 6f 6e 74 65 78 74 2e 73 2c 20 75 2e 63 69 sContext.s, u.ci
1f177 2e 70 44 65 73 74 29 3b 0a 20 20 4d 65 6d 53 65 .pDest);. MemSe
1f178 74 54 79 70 65 46 6c 61 67 28 26 75 2e 63 69 2e tTypeFlag(&u.ci.
1f179 73 43 6f 6e 74 65 78 74 2e 73 2c 20 4d 45 4d 5f sContext.s, MEM_
1f17a 4e 75 6c 6c 29 3b 0a 0a 20 20 69 66 28 20 73 71 Null);.. if( sq
1f17b 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 lite3SafetyOff(d
1f17c 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f b) ) goto abort_
1f17d 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 due_to_misuse;.
1f17e 20 72 63 20 3d 20 75 2e 63 69 2e 70 4d 6f 64 75 rc = u.ci.pModu
1f17f 6c 65 2d 3e 78 43 6f 6c 75 6d 6e 28 70 43 75 72 le->xColumn(pCur
1f180 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2c 20 26 ->pVtabCursor, &
1f181 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74 2c 20 70 u.ci.sContext, p
1f182 4f 70 2d 3e 70 32 29 3b 0a 20 20 73 71 6c 69 74 Op->p2);. sqlit
1f183 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
1f184 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a zErrMsg);. p->z
1f185 45 72 72 4d 73 67 20 3d 20 75 2e 63 69 2e 70 56 ErrMsg = u.ci.pV
1f186 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 tab->zErrMsg;.
1f187 75 2e 63 69 2e 70 56 74 61 62 2d 3e 7a 45 72 72 u.ci.pVtab->zErr
1f188 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 75 Msg = 0;. if( u
1f189 2e 63 69 2e 73 43 6f 6e 74 65 78 74 2e 69 73 45 .ci.sContext.isE
1f18a 72 72 6f 72 20 29 7b 0a 20 20 20 20 72 63 20 3d rror ){. rc =
1f18b 20 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74 2e 69 u.ci.sContext.i
1f18c 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f sError;. }.. /
1f18d 2a 20 43 6f 70 79 20 74 68 65 20 72 65 73 75 6c * Copy the resul
1f18e 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f t of the functio
1f18f 6e 20 74 6f 20 74 68 65 20 50 33 20 72 65 67 69 n to the P3 regi
1f190 73 74 65 72 2e 20 57 65 0a 20 20 2a 2a 20 64 6f ster. We. ** do
1f191 20 74 68 69 73 20 72 65 67 61 72 64 6c 65 73 73 this regardless
1f192 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e of whether or n
1f193 6f 74 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 ot an error occu
1f194 72 72 65 64 20 74 6f 20 65 6e 73 75 72 65 20 61 rred to ensure a
1f195 6e 79 0a 20 20 2a 2a 20 64 79 6e 61 6d 69 63 20 ny. ** dynamic
1f196 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 75 2e allocation in u.
1f197 63 69 2e 73 43 6f 6e 74 65 78 74 2e 73 20 28 61 ci.sContext.s (a
1f198 20 4d 65 6d 20 73 74 72 75 63 74 29 20 69 73 20 Mem struct) is
1f199 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a released.. */.
1f19a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1f19b 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 75 2e 63 ngeEncoding(&u.c
1f19c 69 2e 73 43 6f 6e 74 65 78 74 2e 73 2c 20 65 6e i.sContext.s, en
1f19d 63 6f 64 69 6e 67 29 3b 0a 20 20 52 45 47 49 53 coding);. REGIS
1f19e 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1f19f 33 2c 20 75 2e 63 69 2e 70 44 65 73 74 29 3b 0a 3, u.ci.pDest);.
1f1a0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1f1a1 4d 6f 76 65 28 75 2e 63 69 2e 70 44 65 73 74 2c Move(u.ci.pDest,
1f1a2 20 26 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74 2e &u.ci.sContext.
1f1a3 73 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 s);. UPDATE_MAX
1f1a4 5f 42 4c 4f 42 53 49 5a 45 28 75 2e 63 69 2e 70 _BLOBSIZE(u.ci.p
1f1a5 44 65 73 74 29 3b 0a 0a 20 20 69 66 28 20 73 71 Dest);.. if( sq
1f1a6 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 lite3SafetyOn(db
1f1a7 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 61 62 ) ){. goto ab
1f1a8 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1f1a9 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c e;. }. if( sql
1f1aa 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 ite3VdbeMemTooBi
1f1ab 67 28 75 2e 63 69 2e 70 44 65 73 74 29 20 29 7b g(u.ci.pDest) ){
1f1ac 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 . goto too_bi
1f1ad 67 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a g;. }. break;.
1f1ae 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1f1af 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
1f1b0 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 ABLE */..#ifndef
1f1b1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1f1b2 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 TUALTABLE./* Opc
1f1b3 6f 64 65 3a 20 56 4e 65 78 74 20 50 31 20 50 32 ode: VNext P1 P2
1f1b4 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 * * *.**.** Adv
1f1b5 61 6e 63 65 20 76 69 72 74 75 61 6c 20 74 61 62 ance virtual tab
1f1b6 6c 65 20 50 31 20 74 6f 20 74 68 65 20 6e 65 78 le P1 to the nex
1f1b7 74 20 72 6f 77 20 69 6e 20 69 74 73 20 72 65 73 t row in its res
1f1b8 75 6c 74 20 73 65 74 20 61 6e 64 0a 2a 2a 20 6a ult set and.** j
1f1b9 75 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 ump to instructi
1f1ba 6f 6e 20 50 32 2e 20 20 4f 72 2c 20 69 66 20 74 on P2. Or, if t
1f1bb 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 he virtual table
1f1bc 20 68 61 73 20 72 65 61 63 68 65 64 0a 2a 2a 20 has reached.**
1f1bd 74 68 65 20 65 6e 64 20 6f 66 20 69 74 73 20 72 the end of its r
1f1be 65 73 75 6c 74 20 73 65 74 2c 20 74 68 65 6e 20 esult set, then
1f1bf 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 fall through to
1f1c0 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 the next instruc
1f1c1 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 tion..*/.case OP
1f1c2 5f 56 4e 65 78 74 3a 20 7b 20 20 20 2f 2a 20 6a _VNext: { /* j
1f1c3 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a ump */.#if 0 /*
1f1c4 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1f1c5 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6a moved into u.cj
1f1c6 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 */. sqlite3_vt
1f1c7 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f 6e ab *pVtab;. con
1f1c8 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c st sqlite3_modul
1f1c9 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e e *pModule;. in
1f1ca 74 20 72 65 73 3b 0a 20 20 56 64 62 65 43 75 72 t res;. VdbeCur
1f1cb 73 6f 72 20 2a 70 43 75 72 3b 0a 23 65 6e 64 69 sor *pCur;.#endi
1f1cc 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1f1cd 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1f1ce 75 2e 63 6a 20 2a 2f 0a 0a 20 20 75 2e 63 6a 2e u.cj */.. u.cj.
1f1cf 72 65 73 20 3d 20 30 3b 0a 20 20 75 2e 63 6a 2e res = 0;. u.cj.
1f1d0 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b pCur = p->apCsr[
1f1d1 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 pOp->p1];. asse
1f1d2 72 74 28 20 75 2e 63 6a 2e 70 43 75 72 2d 3e 70 rt( u.cj.pCur->p
1f1d3 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 VtabCursor );.
1f1d4 69 66 28 20 75 2e 63 6a 2e 70 43 75 72 2d 3e 6e if( u.cj.pCur->n
1f1d5 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 62 72 ullRow ){. br
1f1d6 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e 63 6a 2e eak;. }. u.cj.
1f1d7 70 56 74 61 62 20 3d 20 75 2e 63 6a 2e 70 43 75 pVtab = u.cj.pCu
1f1d8 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e r->pVtabCursor->
1f1d9 70 56 74 61 62 3b 0a 20 20 75 2e 63 6a 2e 70 4d pVtab;. u.cj.pM
1f1da 6f 64 75 6c 65 20 3d 20 75 2e 63 6a 2e 70 56 74 odule = u.cj.pVt
1f1db 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 ab->pModule;. a
1f1dc 73 73 65 72 74 28 20 75 2e 63 6a 2e 70 4d 6f 64 ssert( u.cj.pMod
1f1dd 75 6c 65 2d 3e 78 4e 65 78 74 20 29 3b 0a 0a 20 ule->xNext );..
1f1de 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 /* Invoke the x
1f1df 4e 65 78 74 28 29 20 6d 65 74 68 6f 64 20 6f 66 Next() method of
1f1e0 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 54 68 65 the module. The
1f1e1 72 65 20 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 re is no way for
1f1e2 20 74 68 65 0a 20 20 2a 2a 20 75 6e 64 65 72 6c the. ** underl
1f1e3 79 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 ying implementat
1f1e4 69 6f 6e 20 74 6f 20 72 65 74 75 72 6e 20 61 6e ion to return an
1f1e5 20 65 72 72 6f 72 20 69 66 20 6f 6e 65 20 6f 63 error if one oc
1f1e6 63 75 72 73 20 64 75 72 69 6e 67 0a 20 20 2a 2a curs during. **
1f1e7 20 78 4e 65 78 74 28 29 2e 20 49 6e 73 74 65 61 xNext(). Instea
1f1e8 64 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f d, if an error o
1f1e9 63 63 75 72 73 2c 20 74 72 75 65 20 69 73 20 72 ccurs, true is r
1f1ea 65 74 75 72 6e 65 64 20 28 69 6e 64 69 63 61 74 eturned (indicat
1f1eb 69 6e 67 20 74 68 61 74 0a 20 20 2a 2a 20 64 61 ing that. ** da
1f1ec 74 61 20 69 73 20 61 76 61 69 6c 61 62 6c 65 29 ta is available)
1f1ed 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63 and the error c
1f1ee 6f 64 65 20 72 65 74 75 72 6e 65 64 20 77 68 65 ode returned whe
1f1ef 6e 20 78 43 6f 6c 75 6d 6e 20 6f 72 0a 20 20 2a n xColumn or. *
1f1f0 2a 20 73 6f 6d 65 20 6f 74 68 65 72 20 6d 65 74 * some other met
1f1f1 68 6f 64 20 69 73 20 6e 65 78 74 20 69 6e 76 6f hod is next invo
1f1f2 6b 65 64 20 6f 6e 20 74 68 65 20 73 61 76 65 20 ked on the save
1f1f3 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 virtual table cu
1f1f4 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 rsor.. */. if(
1f1f5 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
1f1f6 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f f(db) ) goto abo
1f1f7 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1f1f8 3b 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 ;. p->inVtabMet
1f1f9 68 6f 64 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 hod = 1;. rc =
1f1fa 75 2e 63 6a 2e 70 4d 6f 64 75 6c 65 2d 3e 78 4e u.cj.pModule->xN
1f1fb 65 78 74 28 75 2e 63 6a 2e 70 43 75 72 2d 3e 70 ext(u.cj.pCur->p
1f1fc 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 70 VtabCursor);. p
1f1fd 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d ->inVtabMethod =
1f1fe 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 0;. sqlite3DbF
1f1ff 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d ree(db, p->zErrM
1f200 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 sg);. p->zErrMs
1f201 67 20 3d 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e g = u.cj.pVtab->
1f202 7a 45 72 72 4d 73 67 3b 0a 20 20 75 2e 63 6a 2e zErrMsg;. u.cj.
1f203 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d pVtab->zErrMsg =
1f204 20 30 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 0;. if( rc==SQ
1f205 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 LITE_OK ){. u
1f206 2e 63 6a 2e 72 65 73 20 3d 20 75 2e 63 6a 2e 70 .cj.res = u.cj.p
1f207 4d 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 75 2e 63 Module->xEof(u.c
1f208 6a 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 j.pCur->pVtabCur
1f209 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 sor);. }. if(
1f20a 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 sqlite3SafetyOn(
1f20b 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 db) ) goto abort
1f20c 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a _due_to_misuse;.
1f20d 0a 20 20 69 66 28 20 21 75 2e 63 6a 2e 72 65 73 . if( !u.cj.res
1f20e 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 ){. /* If th
1f20f 65 72 65 20 69 73 20 64 61 74 61 2c 20 6a 75 6d ere is data, jum
1f210 70 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20 20 70 p to P2 */. p
1f211 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1f212 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1f213 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1f214 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1f215 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 LE */..#ifndef S
1f216 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1f217 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 ALTABLE./* Opcod
1f218 65 3a 20 56 52 65 6e 61 6d 65 20 50 31 20 2a 20 e: VRename P1 *
1f219 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 * P4 *.**.** P4
1f21a 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
1f21b 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 a virtual table
1f21c 6f 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 object, an sqlit
1f21d 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 e3_vtab structur
1f21e 65 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 e..** This opcod
1f21f 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f e invokes the co
1f220 72 72 65 73 70 6f 6e 64 69 6e 67 20 78 52 65 6e rresponding xRen
1f221 61 6d 65 20 6d 65 74 68 6f 64 2e 20 54 68 65 20 ame method. The
1f222 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 72 65 67 69 value.** in regi
1f223 73 74 65 72 20 50 31 20 69 73 20 70 61 73 73 65 ster P1 is passe
1f224 64 20 61 73 20 74 68 65 20 7a 4e 61 6d 65 20 61 d as the zName a
1f225 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 78 rgument to the x
1f226 52 65 6e 61 6d 65 20 6d 65 74 68 6f 64 2e 0a 2a Rename method..*
1f227 2f 0a 63 61 73 65 20 4f 50 5f 56 52 65 6e 61 6d /.case OP_VRenam
1f228 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c e: {.#if 0 /* l
1f229 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1f22a 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6b 20 2a oved into u.ck *
1f22b 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 /. sqlite3_vtab
1f22c 20 2a 70 56 74 61 62 3b 0a 20 20 4d 65 6d 20 2a *pVtab;. Mem *
1f22d 70 4e 61 6d 65 3b 0a 23 65 6e 64 69 66 20 2f 2a pName;.#endif /*
1f22e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1f22f 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6b moved into u.ck
1f230 20 2a 2f 0a 0a 20 20 75 2e 63 6b 2e 70 56 74 61 */.. u.ck.pVta
1f231 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 b = pOp->p4.pVta
1f232 62 2d 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 6b b->pVtab;. u.ck
1f233 2e 70 4e 61 6d 65 20 3d 20 26 70 2d 3e 61 4d 65 .pName = &p->aMe
1f234 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 m[pOp->p1];. as
1f235 73 65 72 74 28 20 75 2e 63 6b 2e 70 56 74 61 62 sert( u.ck.pVtab
1f236 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 ->pModule->xRena
1f237 6d 65 20 29 3b 0a 20 20 52 45 47 49 53 54 45 52 me );. REGISTER
1f238 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 _TRACE(pOp->p1,
1f239 75 2e 63 6b 2e 70 4e 61 6d 65 29 3b 0a 20 20 61 u.ck.pName);. a
1f23a 73 73 65 72 74 28 20 75 2e 63 6b 2e 70 4e 61 6d ssert( u.ck.pNam
1f23b 65 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 e->flags & MEM_S
1f23c 74 72 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 tr );. if( sqli
1f23d 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 te3SafetyOff(db)
1f23e 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1f23f 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72 e_to_misuse;. r
1f240 63 20 3d 20 75 2e 63 6b 2e 70 56 74 61 62 2d 3e c = u.ck.pVtab->
1f241 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 pModule->xRename
1f242 28 75 2e 63 6b 2e 70 56 74 61 62 2c 20 75 2e 63 (u.ck.pVtab, u.c
1f243 6b 2e 70 4e 61 6d 65 2d 3e 7a 29 3b 0a 20 20 73 k.pName->z);. s
1f244 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1f245 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 p->zErrMsg);.
1f246 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 p->zErrMsg = u.c
1f247 6b 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 k.pVtab->zErrMsg
1f248 3b 0a 20 20 75 2e 63 6b 2e 70 56 74 61 62 2d 3e ;. u.ck.pVtab->
1f249 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 zErrMsg = 0;. i
1f24a 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1f24b 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 On(db) ) goto ab
1f24c 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1f24d 65 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 e;.. break;.}.#
1f24e 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
1f24f 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1f250 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 ALTABLE./* Opcod
1f251 65 3a 20 56 55 70 64 61 74 65 20 50 31 20 50 32 e: VUpdate P1 P2
1f252 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 P3 P4 *.**.** P
1f253 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 4 is a pointer t
1f254 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c o a virtual tabl
1f255 65 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c e object, an sql
1f256 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 ite3_vtab struct
1f257 75 72 65 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63 ure..** This opc
1f258 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 ode invokes the
1f259 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 78 55 corresponding xU
1f25a 70 64 61 74 65 20 6d 65 74 68 6f 64 2e 20 50 32 pdate method. P2
1f25b 20 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 63 values.** are c
1f25c 6f 6e 74 69 67 75 6f 75 73 20 6d 65 6d 6f 72 79 ontiguous memory
1f25d 20 63 65 6c 6c 73 20 73 74 61 72 74 69 6e 67 20 cells starting
1f25e 61 74 20 50 33 20 74 6f 20 70 61 73 73 20 74 6f at P3 to pass to
1f25f 20 74 68 65 20 78 55 70 64 61 74 65 20 0a 2a 2a the xUpdate .**
1f260 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20 54 68 65 invocation. The
1f261 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1f262 65 72 20 28 50 33 2b 50 32 2d 31 29 20 63 6f 72 er (P3+P2-1) cor
1f263 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 responds to the
1f264 0a 2a 2a 20 70 32 74 68 20 65 6c 65 6d 65 6e 74 .** p2th element
1f265 20 6f 66 20 74 68 65 20 61 72 67 76 20 61 72 72 of the argv arr
1f266 61 79 20 70 61 73 73 65 64 20 74 6f 20 78 55 70 ay passed to xUp
1f267 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 date..**.** The
1f268 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64 20 77 xUpdate method w
1f269 69 6c 6c 20 64 6f 20 61 20 44 45 4c 45 54 45 20 ill do a DELETE
1f26a 6f 72 20 61 6e 20 49 4e 53 45 52 54 20 6f 72 20 or an INSERT or
1f26b 62 6f 74 68 2e 0a 2a 2a 20 54 68 65 20 61 72 67 both..** The arg
1f26c 76 5b 30 5d 20 65 6c 65 6d 65 6e 74 20 28 77 68 v[0] element (wh
1f26d 69 63 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 ich corresponds
1f26e 74 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 to memory cell P
1f26f 33 29 0a 2a 2a 20 69 73 20 74 68 65 20 72 6f 77 3).** is the row
1f270 69 64 20 6f 66 20 61 20 72 6f 77 20 74 6f 20 64 id of a row to d
1f271 65 6c 65 74 65 2e 20 20 49 66 20 61 72 67 76 5b elete. If argv[
1f272 30 5d 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 0] is NULL then
1f273 6e 6f 20 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e 20 no .** deletion
1f274 6f 63 63 75 72 73 2e 20 20 54 68 65 20 61 72 67 occurs. The arg
1f275 76 5b 31 5d 20 65 6c 65 6d 65 6e 74 20 69 73 20 v[1] element is
1f276 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 the rowid of the
1f277 20 6e 65 77 20 0a 2a 2a 20 72 6f 77 2e 20 20 54 new .** row. T
1f278 68 69 73 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 his can be NULL
1f279 74 6f 20 68 61 76 65 20 74 68 65 20 76 69 72 74 to have the virt
1f27a 75 61 6c 20 74 61 62 6c 65 20 73 65 6c 65 63 74 ual table select
1f27b 20 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72 6f 77 the new .** row
1f27c 69 64 20 66 6f 72 20 69 74 73 65 6c 66 2e 20 20 id for itself.
1f27d 54 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 65 The subsequent e
1f27e 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 61 lements in the a
1f27f 72 72 61 79 20 61 72 65 20 0a 2a 2a 20 74 68 65 rray are .** the
1f280 20 76 61 6c 75 65 73 20 6f 66 20 63 6f 6c 75 6d values of colum
1f281 6e 73 20 69 6e 20 74 68 65 20 6e 65 77 20 72 6f ns in the new ro
1f282 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 3d 3d w..**.** If P2==
1f283 31 20 74 68 65 6e 20 6e 6f 20 69 6e 73 65 72 74 1 then no insert
1f284 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 20 is performed.
1f285 61 72 67 76 5b 30 5d 20 69 73 20 74 68 65 20 72 argv[0] is the r
1f286 6f 77 69 64 20 6f 66 0a 2a 2a 20 61 20 72 6f 77 owid of.** a row
1f287 20 74 6f 20 64 65 6c 65 74 65 2e 0a 2a 2a 0a 2a to delete..**.*
1f288 2a 20 50 31 20 69 73 20 61 20 62 6f 6f 6c 65 61 * P1 is a boolea
1f289 6e 20 66 6c 61 67 2e 20 49 66 20 69 74 20 69 73 n flag. If it is
1f28a 20 73 65 74 20 74 6f 20 74 72 75 65 20 61 6e 64 set to true and
1f28b 20 74 68 65 20 78 55 70 64 61 74 65 20 63 61 6c the xUpdate cal
1f28c 6c 0a 2a 2a 20 69 73 20 73 75 63 63 65 73 73 66 l.** is successf
1f28d 75 6c 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c ul, then the val
1f28e 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 ue returned by s
1f28f 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 qlite3_last_inse
1f290 72 74 5f 72 6f 77 69 64 28 29 20 0a 2a 2a 20 69 rt_rowid() .** i
1f291 73 20 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c s set to the val
1f292 75 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 ue of the rowid
1f293 66 6f 72 20 74 68 65 20 72 6f 77 20 6a 75 73 74 for the row just
1f294 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 63 61 inserted..*/.ca
1f295 73 65 20 4f 50 5f 56 55 70 64 61 74 65 3a 20 7b se OP_VUpdate: {
1f296 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1f297 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1f298 20 69 6e 74 6f 20 75 2e 63 6c 20 2a 2f 0a 20 20 into u.cl */.
1f299 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
1f29a 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d tab;. sqlite3_m
1f29b 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a odule *pModule;.
1f29c 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 69 6e int nArg;. in
1f29d 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e t i;. sqlite_in
1f29e 74 36 34 20 72 6f 77 69 64 3b 0a 20 20 4d 65 6d t64 rowid;. Mem
1f29f 20 2a 2a 61 70 41 72 67 3b 0a 20 20 4d 65 6d 20 **apArg;. Mem
1f2a0 2a 70 58 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c *pX;.#endif /* l
1f2a1 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1f2a2 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6c 20 2a oved into u.cl *
1f2a3 2f 0a 0a 20 20 75 2e 63 6c 2e 70 56 74 61 62 20 /.. u.cl.pVtab
1f2a4 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 2d = pOp->p4.pVtab-
1f2a5 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 6c 2e 70 >pVtab;. u.cl.p
1f2a6 4d 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69 74 65 Module = (sqlite
1f2a7 33 5f 6d 6f 64 75 6c 65 20 2a 29 75 2e 63 6c 2e 3_module *)u.cl.
1f2a8 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a pVtab->pModule;.
1f2a9 20 20 75 2e 63 6c 2e 6e 41 72 67 20 3d 20 70 4f u.cl.nArg = pO
1f2aa 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 28 p->p2;. assert(
1f2ab 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 pOp->p4type==P4
1f2ac 5f 56 54 41 42 20 29 3b 0a 20 20 69 66 28 20 41 _VTAB );. if( A
1f2ad 4c 57 41 59 53 28 75 2e 63 6c 2e 70 4d 6f 64 75 LWAYS(u.cl.pModu
1f2ae 6c 65 2d 3e 78 55 70 64 61 74 65 29 20 29 7b 0a le->xUpdate) ){.
1f2af 20 20 20 20 75 2e 63 6c 2e 61 70 41 72 67 20 3d u.cl.apArg =
1f2b0 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 20 20 75 p->apArg;. u
1f2b1 2e 63 6c 2e 70 58 20 3d 20 26 70 2d 3e 61 4d 65 .cl.pX = &p->aMe
1f2b2 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 m[pOp->p3];.
1f2b3 66 6f 72 28 75 2e 63 6c 2e 69 3d 30 3b 20 75 2e for(u.cl.i=0; u.
1f2b4 63 6c 2e 69 3c 75 2e 63 6c 2e 6e 41 72 67 3b 20 cl.i<u.cl.nArg;
1f2b5 75 2e 63 6c 2e 69 2b 2b 29 7b 0a 20 20 20 20 20 u.cl.i++){.
1f2b6 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1f2b7 74 6f 72 65 54 79 70 65 28 75 2e 63 6c 2e 70 58 toreType(u.cl.pX
1f2b8 29 3b 0a 20 20 20 20 20 20 75 2e 63 6c 2e 61 70 );. u.cl.ap
1f2b9 41 72 67 5b 75 2e 63 6c 2e 69 5d 20 3d 20 75 2e Arg[u.cl.i] = u.
1f2ba 63 6c 2e 70 58 3b 0a 20 20 20 20 20 20 75 2e 63 cl.pX;. u.c
1f2bb 6c 2e 70 58 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 l.pX++;. }.
1f2bc 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 if( sqlite3Saf
1f2bd 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 etyOff(db) ) got
1f2be 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1f2bf 69 73 75 73 65 3b 0a 20 20 20 20 72 63 20 3d 20 isuse;. rc =
1f2c0 75 2e 63 6c 2e 70 4d 6f 64 75 6c 65 2d 3e 78 55 u.cl.pModule->xU
1f2c1 70 64 61 74 65 28 75 2e 63 6c 2e 70 56 74 61 62 pdate(u.cl.pVtab
1f2c2 2c 20 75 2e 63 6c 2e 6e 41 72 67 2c 20 75 2e 63 , u.cl.nArg, u.c
1f2c3 6c 2e 61 70 41 72 67 2c 20 26 75 2e 63 6c 2e 72 l.apArg, &u.cl.r
1f2c4 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 owid);. sqlit
1f2c5 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
1f2c6 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d zErrMsg);. p-
1f2c7 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 6c 2e >zErrMsg = u.cl.
1f2c8 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a pVtab->zErrMsg;.
1f2c9 20 20 20 20 75 2e 63 6c 2e 70 56 74 61 62 2d 3e u.cl.pVtab->
1f2ca 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 zErrMsg = 0;.
1f2cb 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1f2cc 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 tyOn(db) ) goto
1f2cd 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 abort_due_to_mis
1f2ce 75 73 65 3b 0a 20 20 20 20 69 66 28 20 72 63 3d use;. if( rc=
1f2cf 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 4f =SQLITE_OK && pO
1f2d0 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 20 20 61 p->p1 ){. a
1f2d1 73 73 65 72 74 28 20 75 2e 63 6c 2e 6e 41 72 67 ssert( u.cl.nArg
1f2d2 3e 31 20 26 26 20 75 2e 63 6c 2e 61 70 41 72 67 >1 && u.cl.apArg
1f2d3 5b 30 5d 20 26 26 20 28 75 2e 63 6c 2e 61 70 41 [0] && (u.cl.apA
1f2d4 72 67 5b 30 5d 2d 3e 66 6c 61 67 73 26 4d 45 4d rg[0]->flags&MEM
1f2d5 5f 4e 75 6c 6c 29 20 29 3b 0a 20 20 20 20 20 20 _Null) );.
1f2d6 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 db->lastRowid =
1f2d7 75 2e 63 6c 2e 72 6f 77 69 64 3b 0a 20 20 20 20 u.cl.rowid;.
1f2d8 7d 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 }. p->nChange
1f2d9 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b ++;. }. break;
1f2da 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1f2db 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1f2dc 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 TABLE */..#ifnde
1f2dd 66 20 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 f SQLITE_OMIT_P
1f2de 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a 20 AGER_PRAGMAS./*
1f2df 4f 70 63 6f 64 65 3a 20 50 61 67 65 63 6f 75 6e Opcode: Pagecoun
1f2e0 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a t P1 P2 * * *.**
1f2e1 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 63 75 .** Write the cu
1f2e2 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 rrent number of
1f2e3 70 61 67 65 73 20 69 6e 20 64 61 74 61 62 61 73 pages in databas
1f2e4 65 20 50 31 20 74 6f 20 6d 65 6d 6f 72 79 20 63 e P1 to memory c
1f2e5 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 ell P2..*/.case
1f2e6 4f 50 5f 50 61 67 65 63 6f 75 6e 74 3a 20 7b 20 OP_Pagecount: {
1f2e7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 /* ou
1f2e8 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f t2-prerelease */
1f2e9 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1f2ea 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1f2eb 20 69 6e 74 6f 20 75 2e 63 6d 20 2a 2f 0a 20 20 into u.cm */.
1f2ec 69 6e 74 20 70 31 3b 0a 20 20 69 6e 74 20 6e 50 int p1;. int nP
1f2ed 61 67 65 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 age;. Pager *pP
1f2ee 61 67 65 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 ager;.#endif /*
1f2ef 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1f2f0 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6d 20 moved into u.cm
1f2f1 2a 2f 0a 0a 20 20 75 2e 63 6d 2e 70 31 20 3d 20 */.. u.cm.p1 =
1f2f2 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 63 6d 2e pOp->p1;. u.cm.
1f2f3 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 pPager = sqlite3
1f2f4 42 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61 BtreePager(db->a
1f2f5 44 62 5b 75 2e 63 6d 2e 70 31 5d 2e 70 42 74 29 Db[u.cm.p1].pBt)
1f2f6 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1f2f7 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 75 PagerPagecount(u
1f2f8 2e 63 6d 2e 70 50 61 67 65 72 2c 20 26 75 2e 63 .cm.pPager, &u.c
1f2f9 6d 2e 6e 50 61 67 65 29 3b 0a 20 20 2f 2a 20 4f m.nPage);. /* O
1f2fa 50 5f 50 61 67 65 63 6f 75 6e 74 20 69 73 20 61 P_Pagecount is a
1f2fb 6c 77 61 79 73 20 63 61 6c 6c 65 64 20 66 72 6f lways called fro
1f2fc 6d 20 77 69 74 68 69 6e 20 61 20 72 65 61 64 20 m within a read
1f2fd 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68 transaction. Th
1f2fe 65 0a 20 20 2a 2a 20 70 61 67 65 20 63 6f 75 6e e. ** page coun
1f2ff 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 t has already be
1f300 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 en successfully
1f301 72 65 61 64 20 61 6e 64 20 63 61 63 68 65 64 2e read and cached.
1f302 20 20 53 6f 20 74 68 65 0a 20 20 2a 2a 20 73 71 So the. ** sq
1f303 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f lite3PagerPageco
1f304 75 6e 74 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 unt() call above
1f305 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 2a 2f cannot fail. */
1f306 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 72 63 . if( ALWAYS(rc
1f307 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a ==SQLITE_OK) ){.
1f308 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 pOut->flags
1f309 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 = MEM_Int;. p
1f30a 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 63 6d 2e Out->u.i = u.cm.
1f30b 6e 50 61 67 65 3b 0a 20 20 7d 0a 20 20 62 72 65 nPage;. }. bre
1f30c 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 ak;.}.#endif..#i
1f30d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1f30e 54 5f 54 52 41 43 45 0a 2f 2a 20 4f 70 63 6f 64 T_TRACE./* Opcod
1f30f 65 3a 20 54 72 61 63 65 20 2a 20 2a 20 2a 20 50 e: Trace * * * P
1f310 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 72 61 4 *.**.** If tra
1f311 63 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20 cing is enabled
1f312 28 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f (by the sqlite3_
1f313 74 72 61 63 65 28 29 29 20 69 6e 74 65 72 66 61 trace()) interfa
1f314 63 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 ce, then.** the
1f315 55 54 46 2d 38 20 73 74 72 69 6e 67 20 63 6f 6e UTF-8 string con
1f316 74 61 69 6e 65 64 20 69 6e 20 50 34 20 69 73 20 tained in P4 is
1f317 65 6d 69 74 74 65 64 20 6f 6e 20 74 68 65 20 74 emitted on the t
1f318 72 61 63 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a race callback..*
1f319 2f 0a 63 61 73 65 20 4f 50 5f 54 72 61 63 65 3a /.case OP_Trace:
1f31a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1f31b 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1f31c 65 64 20 69 6e 74 6f 20 75 2e 63 6e 20 2a 2f 0a ed into u.cn */.
1f31d 20 20 63 68 61 72 20 2a 7a 54 72 61 63 65 3b 0a char *zTrace;.
1f31e 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1f31f 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1f320 69 6e 74 6f 20 75 2e 63 6e 20 2a 2f 0a 0a 20 20 into u.cn */..
1f321 75 2e 63 6e 2e 7a 54 72 61 63 65 20 3d 20 28 70 u.cn.zTrace = (p
1f322 4f 70 2d 3e 70 34 2e 7a 20 3f 20 70 4f 70 2d 3e Op->p4.z ? pOp->
1f323 70 34 2e 7a 20 3a 20 70 2d 3e 7a 53 71 6c 29 3b p4.z : p->zSql);
1f324 0a 20 20 69 66 28 20 75 2e 63 6e 2e 7a 54 72 61 . if( u.cn.zTra
1f325 63 65 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 ce ){. if( db
1f326 2d 3e 78 54 72 61 63 65 20 29 7b 0a 20 20 20 20 ->xTrace ){.
1f327 20 20 64 62 2d 3e 78 54 72 61 63 65 28 64 62 2d db->xTrace(db-
1f328 3e 70 54 72 61 63 65 41 72 67 2c 20 75 2e 63 6e >pTraceArg, u.cn
1f329 2e 7a 54 72 61 63 65 29 3b 0a 20 20 20 20 7d 0a .zTrace);. }.
1f32a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
1f32b 42 55 47 0a 20 20 20 20 69 66 28 20 28 64 62 2d BUG. if( (db-
1f32c 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f >flags & SQLITE_
1f32d 53 71 6c 54 72 61 63 65 29 21 3d 30 20 29 7b 0a SqlTrace)!=0 ){.
1f32e 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 sqlite3Deb
1f32f 75 67 50 72 69 6e 74 66 28 22 53 51 4c 2d 74 72 ugPrintf("SQL-tr
1f330 61 63 65 3a 20 25 73 5c 6e 22 2c 20 75 2e 63 6e ace: %s\n", u.cn
1f331 2e 7a 54 72 61 63 65 29 3b 0a 20 20 20 20 7d 0a .zTrace);. }.
1f332 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1f333 5f 44 45 42 55 47 20 2a 2f 0a 20 20 7d 0a 20 20 _DEBUG */. }.
1f334 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a break;.}.#endif.
1f335 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 6f ../* Opcode: Noo
1f336 70 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a p * * * * *.**.*
1f337 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54 * Do nothing. T
1f338 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1f339 69 73 20 6f 66 74 65 6e 20 75 73 65 66 75 6c 20 is often useful
1f33a 61 73 20 61 20 6a 75 6d 70 0a 2a 2a 20 64 65 73 as a jump.** des
1f33b 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 0a tination..*/./*.
1f33c 2a 2a 20 54 68 65 20 6d 61 67 69 63 20 45 78 70 ** The magic Exp
1f33d 6c 61 69 6e 20 6f 70 63 6f 64 65 20 61 72 65 20 lain opcode are
1f33e 6f 6e 6c 79 20 69 6e 73 65 72 74 65 64 20 77 68 only inserted wh
1f33f 65 6e 20 65 78 70 6c 61 69 6e 3d 3d 32 20 28 77 en explain==2 (w
1f340 68 69 63 68 0a 2a 2a 20 69 73 20 74 6f 20 73 61 hich.** is to sa
1f341 79 20 77 68 65 6e 20 74 68 65 20 45 58 50 4c 41 y when the EXPLA
1f342 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 20 73 79 IN QUERY PLAN sy
1f343 6e 74 61 78 20 69 73 20 75 73 65 64 2e 29 0a 2a ntax is used.).*
1f344 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72 65 * This opcode re
1f345 63 6f 72 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f cords informatio
1f346 6e 20 66 72 6f 6d 20 74 68 65 20 6f 70 74 69 6d n from the optim
1f347 69 7a 65 72 2e 20 20 49 74 20 69 73 20 74 68 65 izer. It is the
1f348 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20 .** the same as
1f349 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 69 73 20 6f a no-op. This o
1f34a 70 63 6f 64 65 73 6e 65 76 65 72 20 61 70 70 65 pcodesnever appe
1f34b 61 72 73 20 69 6e 20 61 20 72 65 61 6c 20 56 4d ars in a real VM
1f34c 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 64 65 66 program..*/.def
1f34d 61 75 6c 74 3a 20 7b 20 20 20 20 20 20 20 20 20 ault: {
1f34e 20 2f 2a 20 54 68 69 73 20 69 73 20 72 65 61 6c /* This is real
1f34f 6c 79 20 4f 50 5f 4e 6f 6f 70 20 61 6e 64 20 4f ly OP_Noop and O
1f350 50 5f 45 78 70 6c 61 69 6e 20 2a 2f 0a 20 20 62 P_Explain */. b
1f351 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a reak;.}../******
1f352 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f353 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f354 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f355 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f356 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63 *******.** The c
1f357 61 73 65 73 20 6f 66 20 74 68 65 20 73 77 69 74 ases of the swit
1f358 63 68 20 73 74 61 74 65 6d 65 6e 74 20 61 62 6f ch statement abo
1f359 76 65 20 74 68 69 73 20 6c 69 6e 65 20 73 68 6f ve this line sho
1f35a 75 6c 64 20 61 6c 6c 20 62 65 20 69 6e 64 65 6e uld all be inden
1f35b 74 65 64 0a 2a 2a 20 62 79 20 36 20 73 70 61 63 ted.** by 6 spac
1f35c 65 73 2e 20 20 42 75 74 20 74 68 65 20 6c 65 66 es. But the lef
1f35d 74 2d 6d 6f 73 74 20 36 20 73 70 61 63 65 73 20 t-most 6 spaces
1f35e 68 61 76 65 20 62 65 65 6e 20 72 65 6d 6f 76 65 have been remove
1f35f 64 20 74 6f 20 69 6d 70 72 6f 76 65 20 74 68 65 d to improve the
1f360 0a 2a 2a 20 72 65 61 64 61 62 69 6c 69 74 79 2e .** readability.
1f361 20 20 46 72 6f 6d 20 74 68 69 73 20 70 6f 69 6e From this poin
1f362 74 20 6f 6e 20 64 6f 77 6e 2c 20 74 68 65 20 6e t on down, the n
1f363 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 74 69 6f ormal indentatio
1f364 6e 20 72 75 6c 65 73 20 61 72 65 0a 2a 2a 20 72 n rules are.** r
1f365 65 73 74 6f 72 65 64 2e 0a 2a 2a 2a 2a 2a 2a 2a estored..*******
1f366 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f367 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f368 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f369 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f36a 2a 2a 2a 2a 2a 2a 2f 0a 20 20 20 20 7d 0a 0a 23 ******/. }..#
1f36b 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 ifdef VDBE_PROFI
1f36c 4c 45 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 75 LE. {. u
1f36d 36 34 20 65 6c 61 70 73 65 64 20 3d 20 73 71 6c 64 elapsed = sql
1f36e 69 74 65 33 48 77 74 69 6d 65 28 29 20 2d 20 73 ite3Hwtime() - s
1f36f 74 61 72 74 3b 0a 20 20 20 20 20 20 70 4f 70 2d tart;. pOp-
1f370 3e 63 79 63 6c 65 73 20 2b 3d 20 65 6c 61 70 73 >cycles += elaps
1f371 65 64 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 63 ed;. pOp->c
1f372 6e 74 2b 2b 3b 0a 23 69 66 20 30 0a 20 20 20 20 nt++;.#if 0.
1f373 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 6f fprintf(stdo
1f374 75 74 2c 20 22 25 31 30 6c 6c 75 20 22 2c 20 65 ut, "%10llu ", e
1f375 6c 61 70 73 65 64 29 3b 0a 20 20 20 20 20 20 20 lapsed);.
1f376 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e sqlite3VdbePrin
1f377 74 4f 70 28 73 74 64 6f 75 74 2c 20 6f 72 69 67 tOp(stdout, orig
1f378 50 63 2c 20 26 70 2d 3e 61 4f 70 5b 6f 72 69 67 Pc, &p->aOp[orig
1f379 50 63 5d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 Pc]);.#endif.
1f37a 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f }.#endif.. /
1f37b 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
1f37c 63 6f 64 65 20 61 64 64 73 20 6e 6f 74 68 69 6e code adds nothin
1f37d 67 20 74 6f 20 74 68 65 20 61 63 74 75 61 6c 20 g to the actual
1f37e 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 20 20 functionality.
1f37f 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 72 6f 67 ** of the prog
1f380 72 61 6d 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 ram. It is only
1f381 20 68 65 72 65 20 66 6f 72 20 74 65 73 74 69 6e here for testin
1f382 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 2e g and debugging.
1f383 0a 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f . ** On the o
1f384 74 68 65 72 20 68 61 6e 64 2c 20 69 74 20 64 6f ther hand, it do
1f385 65 73 20 62 75 72 6e 20 43 50 55 20 63 79 63 6c es burn CPU cycl
1f386 65 73 20 65 76 65 72 79 20 74 69 6d 65 20 74 68 es every time th
1f387 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 74 68 65 rough. ** the
1f388 20 65 76 61 6c 75 61 74 6f 72 20 6c 6f 6f 70 2e evaluator loop.
1f389 20 20 53 6f 20 77 65 20 63 61 6e 20 6c 65 61 76 So we can leav
1f38a 65 20 69 74 20 6f 75 74 20 77 68 65 6e 20 4e 44 e it out when ND
1f38b 45 42 55 47 20 69 73 20 64 65 66 69 6e 65 64 2e EBUG is defined.
1f38c 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 . */.#ifndef
1f38d 4e 44 45 42 55 47 0a 20 20 20 20 61 73 73 65 72 NDEBUG. asser
1f38e 74 28 20 70 63 3e 3d 2d 31 20 26 26 20 70 63 3c t( pc>=-1 && pc<
1f38f 70 2d 3e 6e 4f 70 20 29 3b 0a 0a 23 69 66 64 65 p->nOp );..#ifde
1f390 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
1f391 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20 if( p->trace
1f392 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 ){. if( rc!
1f393 3d 30 20 29 20 66 70 72 69 6e 74 66 28 70 2d 3e =0 ) fprintf(p->
1f394 74 72 61 63 65 2c 22 72 63 3d 25 64 5c 6e 22 2c trace,"rc=%d\n",
1f395 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6f rc);. if( o
1f396 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c pProperty & OPFL
1f397 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 G_OUT2_PRERELEAS
1f398 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 67 E ){. reg
1f399 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72 isterTrace(p->tr
1f39a 61 63 65 2c 20 70 4f 70 2d 3e 70 32 2c 20 70 4f ace, pOp->p2, pO
1f39b 75 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ut);. }.
1f39c 20 20 20 69 66 28 20 6f 70 50 72 6f 70 65 72 74 if( opPropert
1f39d 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 20 29 y & OPFLG_OUT3 )
1f39e 7b 0a 20 20 20 20 20 20 20 20 72 65 67 69 73 74 {. regist
1f39f 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65 erTrace(p->trace
1f3a0 2c 20 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 , pOp->p3, pOut)
1f3a1 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1f3a2 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 #endif /* SQLIT
1f3a3 45 5f 44 45 42 55 47 20 2a 2f 0a 23 65 6e 64 69 E_DEBUG */.#endi
1f3a4 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a f /* NDEBUG */.
1f3a5 20 20 7d 20 20 2f 2a 20 54 68 65 20 65 6e 64 20 } /* The end
1f3a6 6f 66 20 74 68 65 20 66 6f 72 28 3b 3b 29 20 6c of the for(;;) l
1f3a7 6f 6f 70 20 74 68 65 20 6c 6f 6f 70 73 20 74 68 oop the loops th
1f3a8 72 6f 75 67 68 20 6f 70 63 6f 64 65 73 20 2a 2f rough opcodes */
1f3a9 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61 .. /* If we rea
1f3aa 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 ch this point, i
1f3ab 74 20 6d 65 61 6e 73 20 74 68 61 74 20 65 78 65 t means that exe
1f3ac 63 75 74 69 6f 6e 20 69 73 20 66 69 6e 69 73 68 cution is finish
1f3ad 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 61 6e 20 ed with. ** an
1f3ae 65 72 72 6f 72 20 6f 66 20 73 6f 6d 65 20 6b 69 error of some ki
1f3af 6e 64 2e 0a 20 20 2a 2f 0a 76 64 62 65 5f 65 72 nd.. */.vdbe_er
1f3b0 72 6f 72 5f 68 61 6c 74 3a 0a 20 20 61 73 73 65 ror_halt:. asse
1f3b1 72 74 28 20 72 63 20 29 3b 0a 20 20 70 2d 3e 72 rt( rc );. p->r
1f3b2 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 c = rc;. sqlite
1f3b3 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 3VdbeHalt(p);.
1f3b4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 if( rc==SQLITE_I
1f3b5 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 64 62 2d OERR_NOMEM ) db-
1f3b6 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
1f3b7 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 1;. rc = SQLITE
1f3b8 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a 20 54 68 _ERROR;.. /* Th
1f3b9 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 is is the only w
1f3ba 61 79 20 6f 75 74 20 6f 66 20 74 68 69 73 20 70 ay out of this p
1f3bb 72 6f 63 65 64 75 72 65 2e 20 20 57 65 20 68 61 rocedure. We ha
1f3bc 76 65 20 74 6f 0a 20 20 2a 2a 20 72 65 6c 65 61 ve to. ** relea
1f3bd 73 65 20 74 68 65 20 6d 75 74 65 78 65 73 20 6f se the mutexes o
1f3be 6e 20 62 74 72 65 65 73 20 74 68 61 74 20 77 65 n btrees that we
1f3bf 72 65 20 61 63 71 75 69 72 65 64 20 61 74 20 74 re acquired at t
1f3c0 68 65 0a 20 20 2a 2a 20 74 6f 70 2e 20 2a 2f 0a he. ** top. */.
1f3c1 76 64 62 65 5f 72 65 74 75 72 6e 3a 0a 20 20 73 vdbe_return:. s
1f3c2 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
1f3c3 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 ArrayLeave(&p->a
1f3c4 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e Mutex);. return
1f3c5 20 72 63 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 rc;.. /* Jump
1f3c6 74 6f 20 68 65 72 65 20 69 66 20 61 20 73 74 72 to here if a str
1f3c7 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c 61 72 67 ing or blob larg
1f3c8 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4d er than SQLITE_M
1f3c9 41 58 5f 4c 45 4e 47 54 48 0a 20 20 2a 2a 20 69 AX_LENGTH. ** i
1f3ca 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 20 s encountered..
1f3cb 20 2a 2f 0a 74 6f 6f 5f 62 69 67 3a 0a 20 20 73 */.too_big:. s
1f3cc 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
1f3cd 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c &p->zErrMsg, db,
1f3ce 20 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 "string or blob
1f3cf 20 74 6f 6f 20 62 69 67 22 29 3b 0a 20 20 72 63 too big");. rc
1f3d0 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 = SQLITE_TOOBIG
1f3d1 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 ;. goto vdbe_er
1f3d2 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 ror_halt;.. /*
1f3d3 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20 Jump to here if
1f3d4 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 a malloc() fails
1f3d5 2e 0a 20 20 2a 2f 0a 6e 6f 5f 6d 65 6d 3a 0a 20 .. */.no_mem:.
1f3d6 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1f3d7 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 d = 1;. sqlite3
1f3d8 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 SetString(&p->zE
1f3d9 72 72 4d 73 67 2c 20 64 62 2c 20 22 6f 75 74 20 rrMsg, db, "out
1f3da 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 72 of memory");. r
1f3db 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
1f3dc 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 ;. goto vdbe_er
1f3dd 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 ror_halt;.. /*
1f3de 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f 72 Jump to here for
1f3df 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 an SQLITE_MISUS
1f3e0 45 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 61 62 E error.. */.ab
1f3e1 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1f3e2 65 3a 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 e:. rc = SQLITE
1f3e3 5f 4d 49 53 55 53 45 3b 0a 20 20 2f 2a 20 46 61 _MISUSE;. /* Fa
1f3e4 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 61 62 6f ll thru into abo
1f3e5 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 20 rt_due_to_error
1f3e6 2a 2f 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f */.. /* Jump to
1f3e7 20 68 65 72 65 20 66 6f 72 20 61 6e 79 20 6f 74 here for any ot
1f3e8 68 65 72 20 6b 69 6e 64 20 6f 66 20 66 61 74 61 her kind of fata
1f3e9 6c 20 65 72 72 6f 72 2e 20 20 54 68 65 20 22 72 l error. The "r
1f3ea 63 22 20 76 61 72 69 61 62 6c 65 0a 20 20 2a 2a c" variable. **
1f3eb 20 73 68 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 should hold the
1f3ec 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 2e 0a 20 error number..
1f3ed 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f */.abort_due_to
1f3ee 5f 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72 74 _error:. assert
1f3ef 28 20 70 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 ( p->zErrMsg==0
1f3f0 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c );. if( db->mal
1f3f1 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 63 20 3d locFailed ) rc =
1f3f2 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1f3f3 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1f3f4 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 IOERR_NOMEM ){.
1f3f5 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 sqlite3SetStr
1f3f6 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c ing(&p->zErrMsg,
1f3f7 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 db, "%s", sqlit
1f3f8 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 e3ErrStr(rc));.
1f3f9 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 }. goto vdbe_e
1f3fa 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a rror_halt;.. /*
1f3fb 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66 Jump to here if
1f3fc 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 the sqlite3_int
1f3fd 65 72 72 75 70 74 28 29 20 41 50 49 20 73 65 74 errupt() API set
1f3fe 73 20 74 68 65 20 69 6e 74 65 72 72 75 70 74 0a s the interrupt.
1f3ff 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 2a 2f 0a ** flag.. */.
1f400 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 abort_due_to_int
1f401 65 72 72 75 70 74 3a 0a 20 20 61 73 73 65 72 74 errupt:. assert
1f402 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 ( db->u1.isInter
1f403 72 75 70 74 65 64 20 29 3b 0a 20 20 72 63 20 3d rupted );. rc =
1f404 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 SQLITE_INTERRUP
1f405 54 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b T;. p->rc = rc;
1f406 0a 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 . sqlite3SetStr
1f407 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c ing(&p->zErrMsg,
1f408 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 db, "%s", sqlit
1f409 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 e3ErrStr(rc));.
1f40a 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 goto vdbe_error
1f40b 5f 68 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a _halt;.}../*****
1f40c 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
1f40d 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a vdbe.c ********
1f40e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f40f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f410 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
1f411 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
1f412 66 69 6c 65 20 76 64 62 65 62 6c 6f 62 2e 63 20 file vdbeblob.c
1f413 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f414 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f415 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
1f416 32 30 30 37 20 4d 61 79 20 31 0a 2a 2a 0a 2a 2a 2007 May 1.**.**
1f417 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1f418 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1f419 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1f41a 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1f41b 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
1f41c 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
1f41d 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
1f41e 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
1f41f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
1f420 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
1f421 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
1f422 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
1f423 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
1f424 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
1f425 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1f426 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
1f427 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
1f428 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f429 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f42a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f42b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f42c 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
1f42d 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
1f42e 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 ns code used to
1f42f 69 6d 70 6c 65 6d 65 6e 74 20 69 6e 63 72 65 6d implement increm
1f430 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 2e 0a ental BLOB I/O..
1f431 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 62 **.** $Id: vdbeb
1f432 6c 6f 62 2e 63 2c 76 20 31 2e 33 35 20 32 30 30 lob.c,v 1.35 200
1f433 39 2f 30 37 2f 30 32 20 30 37 3a 34 37 3a 33 33 9/07/02 07:47:33
1f434 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 danielk1977 Exp
1f435 20 24 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 $.*/...#ifndef
1f436 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 SQLITE_OMIT_INCR
1f437 42 4c 4f 42 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 BLOB../*.** Vali
1f438 64 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 20 d sqlite3_blob*
1f439 68 61 6e 64 6c 65 73 20 70 6f 69 6e 74 20 74 6f handles point to
1f43a 20 49 6e 63 72 62 6c 6f 62 20 73 74 72 75 63 74 Incrblob struct
1f43b 75 72 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ures..*/.typedef
1f43c 20 73 74 72 75 63 74 20 49 6e 63 72 62 6c 6f 62 struct Incrblob
1f43d 20 49 6e 63 72 62 6c 6f 62 3b 0a 73 74 72 75 63 Incrblob;.struc
1f43e 74 20 49 6e 63 72 62 6c 6f 62 20 7b 0a 20 20 69 t Incrblob {. i
1f43f 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 nt flags;
1f440 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f /* Copy o
1f441 66 20 22 66 6c 61 67 73 22 20 70 61 73 73 65 64 f "flags" passed
1f442 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 to sqlite3_blob
1f443 5f 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 _open() */. int
1f444 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 nByte;
1f445 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
1f446 6f 70 65 6e 20 62 6c 6f 62 2c 20 69 6e 20 62 79 open blob, in by
1f447 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 tes */. int iOf
1f448 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 fset;
1f449 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 /* Byte offset
1f44a 6f 66 20 62 6c 6f 62 20 69 6e 20 63 75 72 73 6f of blob in curso
1f44b 72 20 64 61 74 61 20 2a 2f 0a 20 20 42 74 43 75 r data */. BtCu
1f44c 72 73 6f 72 20 2a 70 43 73 72 3b 20 20 20 20 20 rsor *pCsr;
1f44d 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f /* Cursor po
1f44e 69 6e 74 69 6e 67 20 61 74 20 62 6c 6f 62 20 72 inting at blob r
1f44f 6f 77 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f ow */. sqlite3_
1f450 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 20 stmt *pStmt;
1f451 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 68 6f 6c /* Statement hol
1f452 64 69 6e 67 20 63 75 72 73 6f 72 20 6f 70 65 6e ding cursor open
1f453 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */. sqlite3 *d
1f454 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a b; /*
1f455 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 The associated
1f456 64 61 74 61 62 61 73 65 20 2a 2f 0a 7d 3b 0a 0a database */.};..
1f457 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 62 6c 6f /*.** Open a blo
1f458 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c b handle..*/.SQL
1f459 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1f45a 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 te3_blob_open(.
1f45b 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 20 20 sqlite3* db,
1f45c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1f45d 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1f45e 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ion */. const c
1f45f 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 20 20 har *zDb,
1f460 20 2f 2a 20 54 68 65 20 61 74 74 61 63 68 65 64 /* The attached
1f461 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 database contai
1f462 6e 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f ning the blob */
1f463 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1f464 54 61 62 6c 65 2c 20 20 20 20 20 2f 2a 20 54 68 Table, /* Th
1f465 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 e table containi
1f466 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 ng the blob */.
1f467 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f const char *zCo
1f468 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20 54 68 65 20 lumn, /* The
1f469 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 69 6e column containin
1f46a 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 g the blob */.
1f46b 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 52 6f sqlite_int64 iRo
1f46c 77 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 w, /* The r
1f46d 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 ow containing th
1f46e 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 69 6e 74 20 e glob */. int
1f46f 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 flags,
1f470 20 20 20 20 2f 2a 20 54 72 75 65 20 2d 3e 20 72 /* True -> r
1f471 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 ead/write access
1f472 2c 20 66 61 6c 73 65 20 2d 3e 20 72 65 61 64 2d , false -> read-
1f473 6f 6e 6c 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 only */. sqlite
1f474 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 20 3_blob **ppBlob
1f475 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 /* Handle for
1f476 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 62 6c accessing the bl
1f477 6f 62 20 72 65 74 75 72 6e 65 64 20 68 65 72 65 ob returned here
1f478 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 41 74 */.){. int nAt
1f479 74 65 6d 70 74 20 3d 20 30 3b 0a 20 20 69 6e 74 tempt = 0;. int
1f47a 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 iCol;
1f47b 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
1f47c 20 7a 43 6f 6c 75 6d 6e 20 69 6e 20 72 6f 77 2d zColumn in row-
1f47d 72 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 2f 2a 20 record */.. /*
1f47e 54 68 69 73 20 56 44 42 45 20 70 72 6f 67 72 61 This VDBE progra
1f47f 6d 20 73 65 65 6b 73 20 61 20 62 74 72 65 65 20 m seeks a btree
1f480 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 69 64 cursor to the id
1f481 65 6e 74 69 66 69 65 64 20 0a 20 20 2a 2a 20 64 entified . ** d
1f482 62 2f 74 61 62 6c 65 2f 72 6f 77 20 65 6e 74 72 b/table/row entr
1f483 79 2e 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f y. The reason fo
1f484 72 20 75 73 69 6e 67 20 61 20 76 64 62 65 20 70 r using a vdbe p
1f485 72 6f 67 72 61 6d 20 69 6e 73 74 65 61 64 0a 20 rogram instead.
1f486 20 2a 2a 20 6f 66 20 77 72 69 74 69 6e 67 20 63 ** of writing c
1f487 6f 64 65 20 74 6f 20 75 73 65 20 74 68 65 20 62 ode to use the b
1f488 2d 74 72 65 65 20 6c 61 79 65 72 20 64 69 72 65 -tree layer dire
1f489 63 74 6c 79 20 69 73 20 74 68 61 74 20 74 68 65 ctly is that the
1f48a 0a 20 20 2a 2a 20 76 64 62 65 20 70 72 6f 67 72 . ** vdbe progr
1f48b 61 6d 20 77 69 6c 6c 20 74 61 6b 65 20 61 64 76 am will take adv
1f48c 61 6e 74 61 67 65 20 6f 66 20 74 68 65 20 76 61 antage of the va
1f48d 72 69 6f 75 73 20 74 72 61 6e 73 61 63 74 69 6f rious transactio
1f48e 6e 2c 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 20 n,. ** locking
1f48f 61 6e 64 20 65 72 72 6f 72 20 68 61 6e 64 6c 69 and error handli
1f490 6e 67 20 69 6e 66 72 61 73 74 72 75 63 74 75 72 ng infrastructur
1f491 65 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 e built into the
1f492 20 76 64 62 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a vdbe.. **. **
1f493 20 41 66 74 65 72 20 73 65 65 6b 69 6e 67 20 74 After seeking t
1f494 68 65 20 63 75 72 73 6f 72 2c 20 74 68 65 20 76 he cursor, the v
1f495 64 62 65 20 65 78 65 63 75 74 65 73 20 61 6e 20 dbe executes an
1f496 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 0a 20 20 OP_ResultRow..
1f497 2a 2a 20 43 6f 64 65 20 65 78 74 65 72 6e 61 6c ** Code external
1f498 20 74 6f 20 74 68 65 20 56 64 62 65 20 74 68 65 to the Vdbe the
1f499 6e 20 22 62 6f 72 72 6f 77 73 22 20 74 68 65 20 n "borrows" the
1f49a 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 61 6e b-tree cursor an
1f49b 64 0a 20 20 2a 2a 20 75 73 65 73 20 69 74 20 74 d. ** uses it t
1f49c 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 o implement the
1f49d 62 6c 6f 62 5f 72 65 61 64 28 29 2c 20 62 6c 6f blob_read(), blo
1f49e 62 5f 77 72 69 74 65 28 29 20 61 6e 64 20 0a 20 b_write() and .
1f49f 20 2a 2a 20 62 6c 6f 62 5f 62 79 74 65 73 28 29 ** blob_bytes()
1f4a0 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a functions.. **
1f4a1 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 . ** The sqlite
1f4a2 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 20 66 3_blob_close() f
1f4a3 75 6e 63 74 69 6f 6e 20 66 69 6e 61 6c 69 7a 65 unction finalize
1f4a4 73 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 s the vdbe progr
1f4a5 61 6d 2c 0a 20 20 2a 2a 20 77 68 69 63 68 20 63 am,. ** which c
1f4a6 6c 6f 73 65 73 20 74 68 65 20 62 2d 74 72 65 65 loses the b-tree
1f4a7 20 63 75 72 73 6f 72 20 61 6e 64 20 28 70 6f 73 cursor and (pos
1f4a8 73 69 62 6c 79 29 20 63 6f 6d 6d 69 74 73 20 74 sibly) commits t
1f4a9 68 65 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 he . ** transac
1f4aa 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 tion.. */. sta
1f4ab 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 tic const VdbeOp
1f4ac 4c 69 73 74 20 6f 70 65 6e 42 6c 6f 62 5b 5d 20 List openBlob[]
1f4ad 3d 20 7b 0a 20 20 20 20 7b 4f 50 5f 54 72 61 6e = {. {OP_Tran
1f4ae 73 61 63 74 69 6f 6e 2c 20 30 2c 20 30 2c 20 30 saction, 0, 0, 0
1f4af 7d 2c 20 20 20 20 20 2f 2a 20 30 3a 20 53 74 61 }, /* 0: Sta
1f4b0 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e rt a transaction
1f4b1 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 56 65 72 69 */. {OP_Veri
1f4b2 66 79 43 6f 6f 6b 69 65 2c 20 30 2c 20 30 2c 20 fyCookie, 0, 0,
1f4b3 30 7d 2c 20 20 20 20 2f 2a 20 31 3a 20 43 68 65 0}, /* 1: Che
1f4b4 63 6b 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f ck the schema co
1f4b5 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f okie */. {OP_
1f4b6 54 61 62 6c 65 4c 6f 63 6b 2c 20 30 2c 20 30 2c TableLock, 0, 0,
1f4b7 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 32 3a 0}, /* 2:
1f4b8 20 41 63 71 75 69 72 65 20 61 20 72 65 61 64 20 Acquire a read
1f4b9 6f 72 20 77 72 69 74 65 20 6c 6f 63 6b 20 2a 2f or write lock */
1f4ba 0a 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 .. /* One of
1f4bb 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 the following tw
1f4bc 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 o instructions i
1f4bd 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 61 6e s replaced by an
1f4be 20 4f 50 5f 4e 6f 6f 70 2e 20 2a 2f 0a 20 20 20 OP_Noop. */.
1f4bf 20 7b 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 30 {OP_OpenRead, 0
1f4c0 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 , 0, 0},
1f4c1 2f 2a 20 33 3a 20 4f 70 65 6e 20 63 75 72 73 6f /* 3: Open curso
1f4c2 72 20 30 20 66 6f 72 20 72 65 61 64 69 6e 67 20 r 0 for reading
1f4c3 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70 65 6e 57 */. {OP_OpenW
1f4c4 72 69 74 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 rite, 0, 0, 0},
1f4c5 20 20 20 20 20 20 2f 2a 20 34 3a 20 4f 70 65 6e /* 4: Open
1f4c6 20 63 75 72 73 6f 72 20 30 20 66 6f 72 20 72 65 cursor 0 for re
1f4c7 61 64 2f 77 72 69 74 65 20 2a 2f 0a 0a 20 20 20 ad/write */..
1f4c8 20 7b 4f 50 5f 56 61 72 69 61 62 6c 65 2c 20 31 {OP_Variable, 1
1f4c9 2c 20 31 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 , 1, 1},
1f4ca 2f 2a 20 35 3a 20 50 75 73 68 20 74 68 65 20 72 /* 5: Push the r
1f4cb 6f 77 69 64 20 74 6f 20 74 68 65 20 73 74 61 63 owid to the stac
1f4cc 6b 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4e 6f 74 k */. {OP_Not
1f4cd 45 78 69 73 74 73 2c 20 30 2c 20 39 2c 20 31 7d Exists, 0, 9, 1}
1f4ce 2c 20 20 20 20 20 20 20 2f 2a 20 36 3a 20 53 65 , /* 6: Se
1f4cf 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20 2a 2f ek the cursor */
1f4d0 0a 20 20 20 20 7b 4f 50 5f 43 6f 6c 75 6d 6e 2c . {OP_Column,
1f4d1 20 30 2c 20 30 2c 20 31 7d 2c 20 20 20 20 20 20 0, 0, 1},
1f4d2 20 20 20 20 2f 2a 20 37 20 20 2a 2f 0a 20 20 20 /* 7 */.
1f4d3 20 7b 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 {OP_ResultRow,
1f4d4 31 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 1, 0, 0},
1f4d5 2f 2a 20 38 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 /* 8 */. {OP
1f4d6 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 2c 20 30 7d _Close, 0, 0, 0}
1f4d7 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 39 , /* 9
1f4d8 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 48 61 6c */. {OP_Hal
1f4d9 74 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 t, 0, 0, 0},
1f4da 20 20 20 20 20 20 20 20 2f 2a 20 31 30 20 2a 2f /* 10 */
1f4db 0a 20 20 7d 3b 0a 0a 20 20 56 64 62 65 20 2a 76 . };.. Vdbe *v
1f4dc 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d = 0;. int rc =
1f4dd 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 68 SQLITE_OK;. ch
1f4de 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 20 20 ar *zErr = 0;.
1f4df 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 50 Table *pTab;. P
1f4e0 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20 arse *pParse;..
1f4e1 20 2a 70 70 42 6c 6f 62 20 3d 20 30 3b 0a 20 20 *ppBlob = 0;.
1f4e2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
1f4e3 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a ter(db->mutex);.
1f4e4 20 20 70 50 61 72 73 65 20 3d 20 73 71 6c 69 74 pParse = sqlit
1f4e5 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77 28 e3StackAllocRaw(
1f4e6 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 50 61 72 db, sizeof(*pPar
1f4e7 73 65 29 29 3b 0a 20 20 69 66 28 20 70 50 61 72 se));. if( pPar
1f4e8 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 se==0 ){. rc
1f4e9 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
1f4ea 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 goto blob_op
1f4eb 65 6e 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 64 6f en_out;. }. do
1f4ec 20 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 {. memset(pP
1f4ed 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 arse, 0, sizeof(
1f4ee 50 61 72 73 65 29 29 3b 0a 20 20 20 20 70 50 61 Parse));. pPa
1f4ef 72 73 65 2d 3e 64 62 20 3d 20 64 62 3b 0a 0a 20 rse->db = db;..
1f4f0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 if( sqlite3Sa
1f4f1 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 fetyOn(db) ){.
1f4f2 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1f4f3 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 e(db, zErr);.
1f4f4 20 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 sqlite3StackF
1f4f5 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b ree(db, pParse);
1f4f6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
1f4f7 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d utex_leave(db->m
1f4f8 75 74 65 78 29 3b 0a 20 20 20 20 20 20 72 65 74 utex);. ret
1f4f9 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 urn SQLITE_MISUS
1f4fa 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 E;. }.. sq
1f4fb 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 lite3BtreeEnterA
1f4fc 6c 6c 28 64 62 29 3b 0a 20 20 20 20 70 54 61 62 ll(db);. pTab
1f4fd 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 = sqlite3Locate
1f4fe 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c Table(pParse, 0,
1f4ff 20 7a 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0a 20 zTable, zDb);.
1f500 20 20 20 69 66 28 20 70 54 61 62 20 26 26 20 49 if( pTab && I
1f501 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 sVirtual(pTab) )
1f502 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 30 {. pTab = 0
1f503 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
1f504 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1f505 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 69 72 "cannot open vir
1f506 74 75 61 6c 20 74 61 62 6c 65 3a 20 25 73 22 2c tual table: %s",
1f507 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a zTable);. }.
1f508 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1f509 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 69 66 28 MIT_VIEW. if(
1f50a 20 70 54 61 62 20 26 26 20 70 54 61 62 2d 3e 70 pTab && pTab->p
1f50b 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 Select ){.
1f50c 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 pTab = 0;.
1f50d 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1f50e 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 pParse, "cannot
1f50f 6f 70 65 6e 20 76 69 65 77 3a 20 25 73 22 2c 20 open view: %s",
1f510 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 23 zTable);. }.#
1f511 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 21 70 endif. if( !p
1f512 54 61 62 20 29 7b 0a 20 20 20 20 20 20 69 66 28 Tab ){. if(
1f513 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 pParse->zErrMsg
1f514 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1f515 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 te3DbFree(db, zE
1f516 72 72 29 3b 0a 20 20 20 20 20 20 20 20 7a 45 72 rr);. zEr
1f517 72 20 3d 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 r = pParse->zErr
1f518 4d 73 67 3b 0a 20 20 20 20 20 20 20 20 70 50 61 Msg;. pPa
1f519 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 rse->zErrMsg = 0
1f51a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1f51b 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
1f51c 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 R;. (void)s
1f51d 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 qlite3SafetyOff(
1f51e 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 db);. sqlit
1f51f 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 e3BtreeLeaveAll(
1f520 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 db);. goto
1f521 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 blob_open_out;.
1f522 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4e 6f 77 }.. /* Now
1f523 20 73 65 61 72 63 68 20 70 54 61 62 20 66 6f 72 search pTab for
1f524 20 74 68 65 20 65 78 61 63 74 20 63 6f 6c 75 6d the exact colum
1f525 6e 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 43 n. */. for(iC
1f526 6f 6c 3d 30 3b 20 69 43 6f 6c 20 3c 20 70 54 61 ol=0; iCol < pTa
1f527 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 b->nCol; iCol++)
1f528 20 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c {. if( sql
1f529 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 61 62 ite3StrICmp(pTab
1f52a 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 ->aCol[iCol].zNa
1f52b 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 29 3d 3d 30 20 me, zColumn)==0
1f52c 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b ){. break
1f52d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1f52e 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54 if( iCol==pT
1f52f 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 ab->nCol ){.
1f530 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1f531 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 db, zErr);.
1f532 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d zErr = sqlite3M
1f533 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73 Printf(db, "no s
1f534 75 63 68 20 63 6f 6c 75 6d 6e 3a 20 5c 22 25 73 uch column: \"%s
1f535 5c 22 22 2c 20 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 \"", zColumn);.
1f536 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1f537 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 28 76 _ERROR;. (v
1f538 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 oid)sqlite3Safet
1f539 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 yOff(db);.
1f53a 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
1f53b 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 eAll(db);.
1f53c 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f goto blob_open_o
1f53d 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f ut;. }.. /
1f53e 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 * If the value i
1f53f 73 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 66 s being opened f
1f540 6f 72 20 77 72 69 74 69 6e 67 2c 20 63 68 65 63 or writing, chec
1f541 6b 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a k that the. *
1f542 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 * column is not
1f543 69 6e 64 65 78 65 64 2c 20 61 6e 64 20 74 68 61 indexed, and tha
1f544 74 20 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 t it is not part
1f545 20 6f 66 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 of a foreign ke
1f546 79 2e 20 0a 20 20 20 20 2a 2a 20 49 74 20 69 73 y. . ** It is
1f547 20 61 67 61 69 6e 73 74 20 74 68 65 20 72 75 6c against the rul
1f548 65 73 20 74 6f 20 6f 70 65 6e 20 61 20 63 6f 6c es to open a col
1f549 75 6d 6e 20 74 6f 20 77 68 69 63 68 20 65 69 74 umn to which eit
1f54a 68 65 72 20 6f 66 20 74 68 65 73 65 0a 20 20 20 her of these.
1f54b 20 2a 2a 20 64 65 73 63 72 69 70 74 69 6f 6e 73 ** descriptions
1f54c 20 61 70 70 6c 69 65 73 20 66 6f 72 20 77 72 69 applies for wri
1f54d 74 69 6e 67 2e 20 20 2a 2f 0a 20 20 20 20 69 66 ting. */. if
1f54e 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 ( flags ){.
1f54f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 61 const char *zFa
1f550 75 6c 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 49 ult = 0;. I
1f551 6e 64 65 78 20 2a 70 49 64 78 3b 0a 23 69 66 6e ndex *pIdx;.#ifn
1f552 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1f553 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20 20 FOREIGN_KEY.
1f554 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 26 if( db->flags&
1f555 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 SQLITE_ForeignKe
1f556 79 73 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a ys ){. /*
1f557 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 Check that the
1f558 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 70 61 column is not pa
1f559 72 74 20 6f 66 20 61 6e 20 46 4b 20 63 68 69 6c rt of an FK chil
1f55a 64 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e d key definition
1f55b 2e 20 49 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 . It. **
1f55c 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 is not necessary
1f55d 20 74 6f 20 63 68 65 63 6b 20 69 66 20 69 74 20 to check if it
1f55e 69 73 20 70 61 72 74 20 6f 66 20 61 20 70 61 72 is part of a par
1f55f 65 6e 74 20 6b 65 79 2c 20 61 73 20 70 61 72 65 ent key, as pare
1f560 6e 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 6b 65 nt. ** ke
1f561 79 20 63 6f 6c 75 6d 6e 73 20 6d 75 73 74 20 62 y columns must b
1f562 65 20 69 6e 64 65 78 65 64 2e 20 54 68 65 20 63 e indexed. The c
1f563 68 65 63 6b 20 62 65 6c 6f 77 20 77 69 6c 6c 20 heck below will
1f564 70 69 63 6b 20 75 70 20 74 68 69 73 20 0a 20 20 pick up this .
1f565 20 20 20 20 20 20 2a 2a 20 63 61 73 65 2e 20 20 ** case.
1f566 2a 2f 0a 20 20 20 20 20 20 20 20 46 4b 65 79 20 */. FKey
1f567 2a 70 46 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 *pFKey;.
1f568 66 6f 72 28 70 46 4b 65 79 3d 70 54 61 62 2d 3e for(pFKey=pTab->
1f569 70 46 4b 65 79 3b 20 70 46 4b 65 79 3b 20 70 46 pFKey; pFKey; pF
1f56a 4b 65 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78 74 Key=pFKey->pNext
1f56b 46 72 6f 6d 29 7b 0a 20 20 20 20 20 20 20 20 20 From){.
1f56c 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 int j;.
1f56d 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46 4b for(j=0; j<pFK
1f56e 65 79 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a ey->nCol; j++){.
1f56f 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
1f570 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 6a 5d 2e 69 pFKey->aCol[j].i
1f571 46 72 6f 6d 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 From==iCol ){.
1f572 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 61 75 zFau
1f573 6c 74 20 3d 20 22 66 6f 72 65 69 67 6e 20 6b 65 lt = "foreign ke
1f574 79 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 y";.
1f575 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
1f576 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1f577 23 65 6e 64 69 66 0a 20 20 20 20 20 20 66 6f 72 #endif. for
1f578 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 (pIdx=pTab->pInd
1f579 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 ex; pIdx; pIdx=p
1f57a 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 Idx->pNext){.
1f57b 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 int j;.
1f57c 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 for(j=0; j<p
1f57d 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b Idx->nColumn; j+
1f57e 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 +){. if
1f57f 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e ( pIdx->aiColumn
1f580 5b 6a 5d 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 [j]==iCol ){.
1f581 20 20 20 20 20 20 20 20 20 7a 46 61 75 6c 74 20 zFault
1f582 3d 20 22 69 6e 64 65 78 65 64 22 3b 0a 20 20 20 = "indexed";.
1f583 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1f584 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
1f585 20 69 66 28 20 7a 46 61 75 6c 74 20 29 7b 0a 20 if( zFault ){.
1f586 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 sqlite3Db
1f587 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a Free(db, zErr);.
1f588 20 20 20 20 20 20 20 20 7a 45 72 72 20 3d 20 73 zErr = s
1f589 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 qlite3MPrintf(db
1f58a 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 25 , "cannot open %
1f58b 73 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 69 s column for wri
1f58c 74 69 6e 67 22 2c 20 7a 46 61 75 6c 74 29 3b 0a ting", zFault);.
1f58d 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
1f58e 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 ITE_ERROR;.
1f58f 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 (void)sqlite3
1f590 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 SafetyOff(db);.
1f591 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 sqlite3Bt
1f592 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b reeLeaveAll(db);
1f593 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 6c . goto bl
1f594 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 ob_open_out;.
1f595 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
1f596 76 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 v = sqlite3VdbeC
1f597 72 65 61 74 65 28 64 62 29 3b 0a 20 20 20 20 69 reate(db);. i
1f598 66 28 20 76 20 29 7b 0a 20 20 20 20 20 20 69 6e f( v ){. in
1f599 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 t iDb = sqlite3S
1f59a 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c chemaToIndex(db,
1f59b 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b pTab->pSchema);
1f59c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1f59d 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 73 beAddOpList(v, s
1f59e 69 7a 65 6f 66 28 6f 70 65 6e 42 6c 6f 62 29 2f izeof(openBlob)/
1f59f 73 69 7a 65 6f 66 28 56 64 62 65 4f 70 4c 69 73 sizeof(VdbeOpLis
1f5a0 74 29 2c 20 6f 70 65 6e 42 6c 6f 62 29 3b 0a 20 t), openBlob);.
1f5a1 20 20 20 20 20 66 6c 61 67 73 20 3d 20 21 21 66 flags = !!f
1f5a2 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 lags;
1f5a3 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 3d /* flags =
1f5a4 20 28 66 6c 61 67 73 20 3f 20 31 20 3a 20 30 29 (flags ? 1 : 0)
1f5a5 3b 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 43 ; */.. /* C
1f5a6 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f onfigure the OP_
1f5a7 54 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 Transaction */.
1f5a8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f5a9 43 68 61 6e 67 65 50 31 28 76 2c 20 30 2c 20 69 ChangeP1(v, 0, i
1f5aa 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 Db);. sqlit
1f5ab 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 e3VdbeChangeP2(v
1f5ac 2c 20 30 2c 20 66 6c 61 67 73 29 3b 0a 0a 20 20 , 0, flags);..
1f5ad 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 /* Configure
1f5ae 20 74 68 65 20 4f 50 5f 56 65 72 69 66 79 43 6f the OP_VerifyCo
1f5af 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 okie */. sq
1f5b0 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
1f5b1 31 28 76 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 1(v, 1, iDb);.
1f5b2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
1f5b3 68 61 6e 67 65 50 32 28 76 2c 20 31 2c 20 70 54 hangeP2(v, 1, pT
1f5b4 61 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 ab->pSchema->sch
1f5b5 65 6d 61 5f 63 6f 6f 6b 69 65 29 3b 0a 0a 20 20 ema_cookie);..
1f5b6 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 /* Make sure
1f5b7 20 61 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 a mutex is held
1f5b8 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f on the table to
1f5b9 20 62 65 20 61 63 63 65 73 73 65 64 20 2a 2f 0a be accessed */.
1f5ba 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f5bb 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 eUsesBtree(v, iD
1f5bc 62 29 3b 20 0a 0a 20 20 20 20 20 20 2f 2a 20 43 b); .. /* C
1f5bd 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f onfigure the OP_
1f5be 54 61 62 6c 65 4c 6f 63 6b 20 69 6e 73 74 72 75 TableLock instru
1f5bf 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 ction */. s
1f5c0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1f5c1 50 31 28 76 2c 20 32 2c 20 69 44 62 29 3b 0a 20 P1(v, 2, iDb);.
1f5c2 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f5c3 43 68 61 6e 67 65 50 32 28 76 2c 20 32 2c 20 70 ChangeP2(v, 2, p
1f5c4 54 61 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 Tab->tnum);.
1f5c5 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1f5c6 6e 67 65 50 33 28 76 2c 20 32 2c 20 66 6c 61 67 ngeP3(v, 2, flag
1f5c7 73 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 s);. sqlite
1f5c8 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 3VdbeChangeP4(v,
1f5c9 20 32 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 2, pTab->zName,
1f5ca 20 50 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a P4_TRANSIENT);.
1f5cb 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 . /* Remove
1f5cc 20 65 69 74 68 65 72 20 74 68 65 20 4f 50 5f 4f either the OP_O
1f5cd 70 65 6e 57 72 69 74 65 20 6f 72 20 4f 70 65 6e penWrite or Open
1f5ce 52 65 61 64 2e 20 53 65 74 20 74 68 65 20 50 32 Read. Set the P2
1f5cf 20 0a 20 20 20 20 20 20 2a 2a 20 70 61 72 61 6d . ** param
1f5d0 65 74 65 72 20 6f 66 20 74 68 65 20 6f 74 68 65 eter of the othe
1f5d1 72 20 74 6f 20 70 54 61 62 2d 3e 74 6e 75 6d 2e r to pTab->tnum.
1f5d2 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 */. sqlit
1f5d3 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f e3VdbeChangeToNo
1f5d4 6f 70 28 76 2c 20 34 20 2d 20 66 6c 61 67 73 2c op(v, 4 - flags,
1f5d5 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 1);. sqlit
1f5d6 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 e3VdbeChangeP2(v
1f5d7 2c 20 33 20 2b 20 66 6c 61 67 73 2c 20 70 54 61 , 3 + flags, pTa
1f5d8 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20 b->tnum);.
1f5d9 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1f5da 65 50 33 28 76 2c 20 33 20 2b 20 66 6c 61 67 73 eP3(v, 3 + flags
1f5db 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 20 20 2f , iDb);.. /
1f5dc 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 * Configure the
1f5dd 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e number of column
1f5de 73 2e 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 s. Configure the
1f5df 20 63 75 72 73 6f 72 20 74 6f 0a 20 20 20 20 20 cursor to.
1f5e0 20 2a 2a 20 74 68 69 6e 6b 20 74 68 61 74 20 74 ** think that t
1f5e1 68 65 20 74 61 62 6c 65 20 68 61 73 20 6f 6e 65 he table has one
1f5e2 20 6d 6f 72 65 20 63 6f 6c 75 6d 6e 20 74 68 61 more column tha
1f5e3 6e 20 69 74 20 72 65 61 6c 6c 79 0a 20 20 20 20 n it really.
1f5e4 20 20 2a 2a 20 64 6f 65 73 2e 20 41 6e 20 4f 50 ** does. An OP
1f5e5 5f 43 6f 6c 75 6d 6e 20 74 6f 20 72 65 74 72 69 _Column to retri
1f5e6 65 76 65 20 74 68 69 73 20 69 6d 61 67 69 6e 61 eve this imagina
1f5e7 72 79 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 0a 20 ry column will.
1f5e8 20 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 72 ** always r
1f5e9 65 74 75 72 6e 20 61 6e 20 53 51 4c 20 4e 55 4c eturn an SQL NUL
1f5ea 4c 2e 20 54 68 69 73 20 69 73 20 75 73 65 66 75 L. This is usefu
1f5eb 6c 20 62 65 63 61 75 73 65 20 69 74 20 6d 65 61 l because it mea
1f5ec 6e 73 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 63 ns. ** we c
1f5ed 61 6e 20 69 6e 76 6f 6b 65 20 4f 50 5f 43 6f 6c an invoke OP_Col
1f5ee 75 6d 6e 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 umn to fill in t
1f5ef 68 65 20 76 64 62 65 20 63 75 72 73 6f 72 73 20 he vdbe cursors
1f5f0 74 79 70 65 20 0a 20 20 20 20 20 20 2a 2a 20 61 type . ** a
1f5f1 6e 64 20 6f 66 66 73 65 74 20 63 61 63 68 65 20 nd offset cache
1f5f2 77 69 74 68 6f 75 74 20 63 61 75 73 69 6e 67 20 without causing
1f5f3 61 6e 79 20 49 4f 2e 0a 20 20 20 20 20 20 2a 2f any IO.. */
1f5f4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1f5f5 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 33 2b beChangeP4(v, 3+
1f5f6 66 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 4e flags, SQLITE_IN
1f5f7 54 5f 54 4f 5f 50 54 52 28 70 54 61 62 2d 3e 6e T_TO_PTR(pTab->n
1f5f8 43 6f 6c 2b 31 29 2c 50 34 5f 49 4e 54 33 32 29 Col+1),P4_INT32)
1f5f9 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1f5fa 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 37 dbeChangeP2(v, 7
1f5fb 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 , pTab->nCol);.
1f5fc 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 if( !db->ma
1f5fd 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
1f5fe 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f5ff 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 31 2c eMakeReady(v, 1,
1f600 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 29 3b 1, 1, 0, 0, 0);
1f601 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1f602 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 . sqlite3Bt
1f603 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b reeLeaveAll(db);
1f604 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1f605 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 3SafetyOff(db);.
1f606 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 if( NEVER(rc
1f607 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 7c 7c 20 !=SQLITE_OK) ||
1f608 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1f609 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 ){. goto b
1f60a 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 lob_open_out;.
1f60b 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 }.. sqlite3
1f60c 5f 62 69 6e 64 5f 69 6e 74 36 34 28 28 73 71 6c _bind_int64((sql
1f60d 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 2c 20 31 ite3_stmt *)v, 1
1f60e 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 20 , iRow);. rc
1f60f 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 28 = sqlite3_step((
1f610 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 sqlite3_stmt *)v
1f611 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
1f612 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 QLITE_ROW ){.
1f613 20 20 20 6e 41 74 74 65 6d 70 74 2b 2b 3b 0a 20 nAttempt++;.
1f614 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1f615 33 5f 66 69 6e 61 6c 69 7a 65 28 28 73 71 6c 69 3_finalize((sqli
1f616 74 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a 20 te3_stmt *)v);.
1f617 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1f618 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 ee(db, zErr);.
1f619 20 20 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 zErr = sqlit
1f61a 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 73 71 e3MPrintf(db, sq
1f61b 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29 lite3_errmsg(db)
1f61c 29 3b 0a 20 20 20 20 20 20 76 20 3d 20 30 3b 0a );. v = 0;.
1f61d 20 20 20 20 7d 0a 20 20 7d 20 77 68 69 6c 65 28 }. } while(
1f61e 20 6e 41 74 74 65 6d 70 74 3c 35 20 26 26 20 72 nAttempt<5 && r
1f61f 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 c==SQLITE_SCHEMA
1f620 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 );.. if( rc==S
1f621 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 QLITE_ROW ){.
1f622 20 2f 2a 20 54 68 65 20 72 6f 77 2d 72 65 63 6f /* The row-reco
1f623 72 64 20 68 61 73 20 62 65 65 6e 20 6f 70 65 6e rd has been open
1f624 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e ed successfully.
1f625 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 0a Check that the.
1f626 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e ** column in
1f627 20 71 75 65 73 74 69 6f 6e 20 63 6f 6e 74 61 69 question contai
1f628 6e 73 20 74 65 78 74 20 6f 72 20 61 20 62 6c 6f ns text or a blo
1f629 62 2e 20 49 66 20 69 74 20 63 6f 6e 74 61 69 6e b. If it contain
1f62a 73 0a 20 20 20 20 2a 2a 20 74 65 78 74 2c 20 69 s. ** text, i
1f62b 74 20 69 73 20 75 70 20 74 6f 20 74 68 65 20 63 t is up to the c
1f62c 61 6c 6c 65 72 20 74 6f 20 67 65 74 20 74 68 65 aller to get the
1f62d 20 65 6e 63 6f 64 69 6e 67 20 72 69 67 68 74 2e encoding right.
1f62e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 63 72 . */. Incr
1f62f 62 6c 6f 62 20 2a 70 42 6c 6f 62 3b 0a 20 20 20 blob *pBlob;.
1f630 20 75 33 32 20 74 79 70 65 20 3d 20 76 2d 3e 61 u32 type = v->a
1f631 70 43 73 72 5b 30 5d 2d 3e 61 54 79 70 65 5b 69 pCsr[0]->aType[i
1f632 43 6f 6c 5d 3b 0a 0a 20 20 20 20 69 66 28 20 74 Col];.. if( t
1f633 79 70 65 3c 31 32 20 29 7b 0a 20 20 20 20 20 20 ype<12 ){.
1f634 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1f635 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7a , zErr);. z
1f636 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 Err = sqlite3MPr
1f637 69 6e 74 66 28 64 62 2c 20 22 63 61 6e 6e 6f 74 intf(db, "cannot
1f638 20 6f 70 65 6e 20 76 61 6c 75 65 20 6f 66 20 74 open value of t
1f639 79 70 65 20 25 73 22 2c 0a 20 20 20 20 20 20 20 ype %s",.
1f63a 20 20 20 74 79 70 65 3d 3d 30 3f 22 6e 75 6c 6c type==0?"null
1f63b 22 3a 20 74 79 70 65 3d 3d 37 3f 22 72 65 61 6c ": type==7?"real
1f63c 22 3a 20 22 69 6e 74 65 67 65 72 22 0a 20 20 20 ": "integer".
1f63d 20 20 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d );. rc =
1f63e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
1f63f 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f goto blob_o
1f640 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 pen_out;. }.
1f641 20 20 20 70 42 6c 6f 62 20 3d 20 28 49 6e 63 72 pBlob = (Incr
1f642 62 6c 6f 62 20 2a 29 73 71 6c 69 74 65 33 44 62 blob *)sqlite3Db
1f643 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 MallocZero(db, s
1f644 69 7a 65 6f 66 28 49 6e 63 72 62 6c 6f 62 29 29 izeof(Incrblob))
1f645 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 ;. if( db->ma
1f646 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
1f647 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1f648 65 28 64 62 2c 20 70 42 6c 6f 62 29 3b 0a 20 20 e(db, pBlob);.
1f649 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 goto blob_op
1f64a 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 en_out;. }.
1f64b 20 20 70 42 6c 6f 62 2d 3e 66 6c 61 67 73 20 3d pBlob->flags =
1f64c 20 66 6c 61 67 73 3b 0a 20 20 20 20 70 42 6c 6f flags;. pBlo
1f64d 62 2d 3e 70 43 73 72 20 3d 20 20 76 2d 3e 61 70 b->pCsr = v->ap
1f64e 43 73 72 5b 30 5d 2d 3e 70 43 75 72 73 6f 72 3b Csr[0]->pCursor;
1f64f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1f650 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 42 6c eEnterCursor(pBl
1f651 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 73 ob->pCsr);. s
1f652 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 65 qlite3BtreeCache
1f653 4f 76 65 72 66 6c 6f 77 28 70 42 6c 6f 62 2d 3e Overflow(pBlob->
1f654 70 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 pCsr);. sqlit
1f655 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 e3BtreeLeaveCurs
1f656 6f 72 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 3b or(pBlob->pCsr);
1f657 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 53 74 6d . pBlob->pStm
1f658 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d t = (sqlite3_stm
1f659 74 20 2a 29 76 3b 0a 20 20 20 20 70 42 6c 6f 62 t *)v;. pBlob
1f65a 2d 3e 69 4f 66 66 73 65 74 20 3d 20 76 2d 3e 61 ->iOffset = v->a
1f65b 70 43 73 72 5b 30 5d 2d 3e 61 4f 66 66 73 65 74 pCsr[0]->aOffset
1f65c 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 70 42 6c 6f [iCol];. pBlo
1f65d 62 2d 3e 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 b->nByte = sqlit
1f65e 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
1f65f 4c 65 6e 28 74 79 70 65 29 3b 0a 20 20 20 20 70 Len(type);. p
1f660 42 6c 6f 62 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 Blob->db = db;.
1f661 20 20 20 2a 70 70 42 6c 6f 62 20 3d 20 28 73 71 *ppBlob = (sq
1f662 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 70 42 6c lite3_blob *)pBl
1f663 6f 62 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ob;. rc = SQL
1f664 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 20 ITE_OK;. }else
1f665 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1f666 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 K ){. sqlite3
1f667 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 DbFree(db, zErr)
1f668 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 73 71 6c ;. zErr = sql
1f669 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 ite3MPrintf(db,
1f66a 22 6e 6f 20 73 75 63 68 20 72 6f 77 69 64 3a 20 "no such rowid:
1f66b 25 6c 6c 64 22 2c 20 69 52 6f 77 29 3b 0a 20 20 %lld", iRow);.
1f66c 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 rc = SQLITE_ER
1f66d 52 4f 52 3b 0a 20 20 7d 0a 0a 62 6c 6f 62 5f 6f ROR;. }..blob_o
1f66e 70 65 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20 76 pen_out:. if( v
1f66f 20 26 26 20 28 72 63 21 3d 53 51 4c 49 54 45 5f && (rc!=SQLITE_
1f670 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 OK || db->malloc
1f671 46 61 69 6c 65 64 29 20 29 7b 0a 20 20 20 20 73 Failed) ){. s
1f672 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 qlite3VdbeFinali
1f673 7a 65 28 76 29 3b 0a 20 20 7d 0a 20 20 73 71 6c ze(v);. }. sql
1f674 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 ite3Error(db, rc
1f675 2c 20 7a 45 72 72 29 3b 0a 20 20 73 71 6c 69 74 , zErr);. sqlit
1f676 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 e3DbFree(db, zEr
1f677 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 61 r);. sqlite3Sta
1f678 63 6b 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 ckFree(db, pPars
1f679 65 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 e);. rc = sqlit
1f67a 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 e3ApiExit(db, rc
1f67b 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 );. sqlite3_mut
1f67c 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 ex_leave(db->mut
1f67d 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 ex);. return rc
1f67e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 ;.}../*.** Close
1f67f 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 74 a blob handle t
1f680 68 61 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 hat was previous
1f681 6c 79 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 ly created using
1f682 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 .** sqlite3_blob
1f683 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 53 51 4c 49 _open()..*/.SQLI
1f684 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1f685 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 73 71 e3_blob_close(sq
1f686 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f lite3_blob *pBlo
1f687 62 29 7b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a b){. Incrblob *
1f688 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 p = (Incrblob *)
1f689 70 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 72 63 3b pBlob;. int rc;
1f68a 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a . sqlite3 *db;.
1f68b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
1f68c 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 db = p->db;.
1f68d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
1f68e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a ter(db->mutex);.
1f68f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1f690 5f 66 69 6e 61 6c 69 7a 65 28 70 2d 3e 70 53 74 _finalize(p->pSt
1f691 6d 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 mt);. sqlite3
1f692 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 DbFree(db, p);.
1f693 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
1f694 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 _leave(db->mutex
1f695 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1f696 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1f697 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1f698 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 .}../*.** Perfor
1f699 6d 20 61 20 72 65 61 64 20 6f 72 20 77 72 69 74 m a read or writ
1f69a 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20 61 e operation on a
1f69b 20 62 6c 6f 62 0a 2a 2f 0a 73 74 61 74 69 63 20 blob.*/.static
1f69c 69 6e 74 20 62 6c 6f 62 52 65 61 64 57 72 69 74 int blobReadWrit
1f69d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f e(. sqlite3_blo
1f69e 62 20 2a 70 42 6c 6f 62 2c 20 0a 20 20 76 6f 69 b *pBlob, . voi
1f69f 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 d *z, . int n,
1f6a0 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 2c 20 . int iOffset,
1f6a1 0a 20 20 69 6e 74 20 28 2a 78 43 61 6c 6c 29 28 . int (*xCall)(
1f6a2 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 2c 20 BtCursor*, u32,
1f6a3 75 33 32 2c 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 u32, void*).){.
1f6a4 20 69 6e 74 20 72 63 3b 0a 20 20 49 6e 63 72 62 int rc;. Incrb
1f6a5 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c lob *p = (Incrbl
1f6a6 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 56 64 ob *)pBlob;. Vd
1f6a7 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 be *v;. sqlite3
1f6a8 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70 3d 3d *db;.. if( p==
1f6a9 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 0 ) return SQLIT
1f6aa 45 5f 4d 49 53 55 53 45 3b 0a 20 20 64 62 20 3d E_MISUSE;. db =
1f6ab 20 70 2d 3e 64 62 3b 0a 20 20 73 71 6c 69 74 65 p->db;. sqlite
1f6ac 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 3_mutex_enter(db
1f6ad 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 76 20 3d 20 ->mutex);. v =
1f6ae 28 56 64 62 65 2a 29 70 2d 3e 70 53 74 6d 74 3b (Vdbe*)p->pStmt;
1f6af 0a 0a 20 20 69 66 28 20 6e 3c 30 20 7c 7c 20 69 .. if( n<0 || i
1f6b0 4f 66 66 73 65 74 3c 30 20 7c 7c 20 28 69 4f 66 Offset<0 || (iOf
1f6b1 66 73 65 74 2b 6e 29 3e 70 2d 3e 6e 42 79 74 65 fset+n)>p->nByte
1f6b2 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65 71 75 65 ){. /* Reque
1f6b3 73 74 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e st is out of ran
1f6b4 67 65 2e 20 52 65 74 75 72 6e 20 61 20 74 72 61 ge. Return a tra
1f6b5 6e 73 69 65 6e 74 20 65 72 72 6f 72 2e 20 2a 2f nsient error. */
1f6b6 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1f6b7 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 _ERROR;. sqli
1f6b8 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c te3Error(db, SQL
1f6b9 49 54 45 5f 45 52 52 4f 52 2c 20 30 29 3b 0a 20 ITE_ERROR, 0);.
1f6ba 20 7d 20 65 6c 73 65 20 69 66 28 20 76 3d 3d 30 } else if( v==0
1f6bb 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 ){. /* If th
1f6bc 65 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d ere is no statem
1f6bd 65 6e 74 20 68 61 6e 64 6c 65 2c 20 74 68 65 6e ent handle, then
1f6be 20 74 68 65 20 62 6c 6f 62 2d 68 61 6e 64 6c 65 the blob-handle
1f6bf 20 68 61 73 0a 20 20 20 20 2a 2a 20 61 6c 72 65 has. ** alre
1f6c0 61 64 79 20 62 65 65 6e 20 69 6e 76 61 6c 69 64 ady been invalid
1f6c1 61 74 65 64 2e 20 52 65 74 75 72 6e 20 53 51 4c ated. Return SQL
1f6c2 49 54 45 5f 41 42 4f 52 54 20 69 6e 20 74 68 69 ITE_ABORT in thi
1f6c3 73 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 s case.. */.
1f6c4 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 rc = SQLITE_A
1f6c5 42 4f 52 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 BORT;. }else{.
1f6c6 20 20 20 2f 2a 20 43 61 6c 6c 20 65 69 74 68 65 /* Call eithe
1f6c7 72 20 42 74 72 65 65 44 61 74 61 28 29 20 6f 72 r BtreeData() or
1f6c8 20 42 74 72 65 65 50 75 74 44 61 74 61 28 29 2e BtreePutData().
1f6c9 20 49 66 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 If SQLITE_ABORT
1f6ca 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 is. ** retur
1f6cb 6e 65 64 2c 20 63 6c 65 61 6e 2d 75 70 20 74 68 ned, clean-up th
1f6cc 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 e statement hand
1f6cd 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 le.. */. a
1f6ce 73 73 65 72 74 28 20 64 62 20 3d 3d 20 76 2d 3e ssert( db == v->
1f6cf 64 62 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 db );. sqlite
1f6d0 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 3BtreeEnterCurso
1f6d1 72 28 70 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 r(p->pCsr);.
1f6d2 72 63 20 3d 20 78 43 61 6c 6c 28 70 2d 3e 70 43 rc = xCall(p->pC
1f6d3 73 72 2c 20 69 4f 66 66 73 65 74 2b 70 2d 3e 69 sr, iOffset+p->i
1f6d4 4f 66 66 73 65 74 2c 20 6e 2c 20 7a 29 3b 0a 20 Offset, n, z);.
1f6d5 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
1f6d6 65 61 76 65 43 75 72 73 6f 72 28 70 2d 3e 70 43 eaveCursor(p->pC
1f6d7 73 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d sr);. if( rc=
1f6d8 3d 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 7b =SQLITE_ABORT ){
1f6d9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1f6da 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a 20 beFinalize(v);.
1f6db 20 20 20 20 20 70 2d 3e 70 53 74 6d 74 20 3d 20 p->pStmt =
1f6dc 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 0;. }else{.
1f6dd 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 db->errCode
1f6de 3d 20 72 63 3b 0a 20 20 20 20 20 20 76 2d 3e 72 = rc;. v->r
1f6df 63 20 3d 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 c = rc;. }.
1f6e0 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 }. rc = sqlite3
1f6e1 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b ApiExit(db, rc);
1f6e2 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
1f6e3 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 _leave(db->mutex
1f6e4 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
1f6e5 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 }../*.** Read da
1f6e6 74 61 20 66 72 6f 6d 20 61 20 62 6c 6f 62 20 68 ta from a blob h
1f6e7 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 andle..*/.SQLITE
1f6e8 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1f6e9 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69 74 _blob_read(sqlit
1f6ea 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 e3_blob *pBlob,
1f6eb 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 void *z, int n,
1f6ec 69 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 int iOffset){.
1f6ed 72 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 return blobReadW
1f6ee 72 69 74 65 28 70 42 6c 6f 62 2c 20 7a 2c 20 6e rite(pBlob, z, n
1f6ef 2c 20 69 4f 66 66 73 65 74 2c 20 73 71 6c 69 74 , iOffset, sqlit
1f6f0 65 33 42 74 72 65 65 44 61 74 61 29 3b 0a 7d 0a e3BtreeData);.}.
1f6f1 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 ./*.** Write dat
1f6f2 61 20 74 6f 20 61 20 62 6c 6f 62 20 68 61 6e 64 a to a blob hand
1f6f3 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 le..*/.SQLITE_AP
1f6f4 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
1f6f5 6f 62 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33 ob_write(sqlite3
1f6f6 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f _blob *pBlob, co
1f6f7 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 nst void *z, int
1f6f8 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29 n, int iOffset)
1f6f9 7b 0a 20 20 72 65 74 75 72 6e 20 62 6c 6f 62 52 {. return blobR
1f6fa 65 61 64 57 72 69 74 65 28 70 42 6c 6f 62 2c 20 eadWrite(pBlob,
1f6fb 28 76 6f 69 64 20 2a 29 7a 2c 20 6e 2c 20 69 4f (void *)z, n, iO
1f6fc 66 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74 ffset, sqlite3Bt
1f6fd 72 65 65 50 75 74 44 61 74 61 29 3b 0a 7d 0a 0a reePutData);.}..
1f6fe 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 61 20 62 6c /*.** Query a bl
1f6ff 6f 62 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 ob handle for th
1f700 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 e size of the da
1f701 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e ta..**.** The In
1f702 63 72 62 6c 6f 62 2e 6e 42 79 74 65 20 66 69 65 crblob.nByte fie
1f703 6c 64 20 69 73 20 66 69 78 65 64 20 66 6f 72 20 ld is fixed for
1f704 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 the lifetime of
1f705 74 68 65 20 49 6e 63 72 62 6c 6f 62 0a 2a 2a 20 the Incrblob.**
1f706 73 6f 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 72 so no mutex is r
1f707 65 71 75 69 72 65 64 20 66 6f 72 20 61 63 63 65 equired for acce
1f708 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ss..*/.SQLITE_AP
1f709 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
1f70a 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 ob_bytes(sqlite3
1f70b 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 _blob *pBlob){.
1f70c 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 Incrblob *p = (
1f70d 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 Incrblob *)pBlob
1f70e 3b 0a 20 20 72 65 74 75 72 6e 20 70 20 3f 20 70 ;. return p ? p
1f70f 2d 3e 6e 42 79 74 65 20 3a 20 30 3b 0a 7d 0a 0a ->nByte : 0;.}..
1f710 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 #endif /* #ifnde
1f711 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
1f712 43 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 2a 2a 2a CRBLOB */../****
1f713 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
1f714 66 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a 2a f vdbeblob.c ***
1f715 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f716 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f717 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
1f718 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
1f719 20 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63 20 file journal.c
1f71a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f71b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f71c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
1f71d 20 32 30 30 37 20 41 75 67 75 73 74 20 32 32 0a 2007 August 22.
1f71e 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
1f71f 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
1f720 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
1f721 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
1f722 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
1f723 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
1f724 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
1f725 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
1f726 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
1f727 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
1f728 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
1f729 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
1f72a 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
1f72b 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
1f72c 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
1f72d 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
1f72e 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
1f72f 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
1f730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f731 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f732 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f733 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
1f734 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 6a *.** @(#) $Id: j
1f735 6f 75 72 6e 61 6c 2e 63 2c 76 20 31 2e 39 20 32 ournal.c,v 1.9 2
1f736 30 30 39 2f 30 31 2f 32 30 20 31 37 3a 30 36 3a 009/01/20 17:06:
1f737 32 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 27 danielk1977 E
1f738 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 xp $.*/..#ifdef
1f739 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 SQLITE_ENABLE_AT
1f73a 4f 4d 49 43 5f 57 52 49 54 45 0a 0a 2f 2a 0a 2a OMIC_WRITE../*.*
1f73b 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c * This file impl
1f73c 65 6d 65 6e 74 73 20 61 20 73 70 65 63 69 61 6c ements a special
1f73d 20 6b 69 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 kind of sqlite3
1f73e 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 75 73 65 _file object use
1f73f 64 0a 2a 2a 20 62 79 20 53 51 4c 69 74 65 20 74 d.** by SQLite t
1f740 6f 20 63 72 65 61 74 65 20 6a 6f 75 72 6e 61 6c o create journal
1f741 20 66 69 6c 65 73 20 69 66 20 74 68 65 20 61 74 files if the at
1f742 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d omic-write optim
1f743 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 65 6e ization.** is en
1f744 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 abled..**.** The
1f745 20 64 69 73 74 69 6e 63 74 69 76 65 20 63 68 61 distinctive cha
1f746 72 61 63 74 65 72 69 73 74 69 63 20 6f 66 20 74 racteristic of t
1f747 68 69 73 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 his sqlite3_file
1f748 20 69 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 is that the.**
1f749 61 63 74 75 61 6c 20 6f 6e 20 64 69 73 6b 20 66 actual on disk f
1f74a 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 20 6c ile is created l
1f74b 61 7a 69 6c 79 2e 20 57 68 65 6e 20 74 68 65 20 azily. When the
1f74c 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 2c file is created,
1f74d 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 73 .** the caller s
1f74e 70 65 63 69 66 69 65 73 20 61 20 62 75 66 66 65 pecifies a buffe
1f74f 72 20 73 69 7a 65 20 66 6f 72 20 61 6e 20 69 6e r size for an in
1f750 2d 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 -memory buffer t
1f751 6f 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 o.** be used to
1f752 73 65 72 76 69 63 65 20 72 65 61 64 28 29 20 61 service read() a
1f753 6e 64 20 77 72 69 74 65 28 29 20 72 65 71 75 65 nd write() reque
1f754 73 74 73 2e 20 54 68 65 20 61 63 74 75 61 6c 20 sts. The actual
1f755 66 69 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20 file.** on disk
1f756 69 73 20 6e 6f 74 20 63 72 65 61 74 65 64 20 6f is not created o
1f757 72 20 70 6f 70 75 6c 61 74 65 64 20 75 6e 74 69 r populated unti
1f758 6c 20 65 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 l either:.**.**
1f759 20 20 31 29 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 1) The in-memo
1f75a 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ry representatio
1f75b 6e 20 67 72 6f 77 73 20 74 6f 6f 20 6c 61 72 67 n grows too larg
1f75c 65 20 66 6f 72 20 74 68 65 20 61 6c 6c 6f 63 61 e for the alloca
1f75d 74 65 64 20 0a 2a 2a 20 20 20 20 20 20 62 75 66 ted .** buf
1f75e 66 65 72 2c 20 6f 72 0a 2a 2a 20 20 20 32 29 20 fer, or.** 2)
1f75f 54 68 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e The sqlite3Journ
1f760 61 6c 43 72 65 61 74 65 28 29 20 66 75 6e 63 74 alCreate() funct
1f761 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a ion is called..*
1f762 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 /..../*.** A Jou
1f763 72 6e 61 6c 46 69 6c 65 20 6f 62 6a 65 63 74 20 rnalFile object
1f764 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66 is a subclass of
1f765 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 75 73 sqlite3_file us
1f766 65 64 20 62 79 0a 2a 2a 20 61 73 20 61 6e 20 6f ed by.** as an o
1f767 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 pen file handle
1f768 66 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 for journal file
1f769 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4a 6f 75 s..*/.struct Jou
1f76a 72 6e 61 6c 46 69 6c 65 20 7b 0a 20 20 73 71 6c rnalFile {. sql
1f76b 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
1f76c 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 *pMethod; /*
1f76d 49 2f 4f 20 6d 65 74 68 6f 64 73 20 6f 6e 20 6a I/O methods on j
1f76e 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a ournal files */.
1f76f 20 20 69 6e 74 20 6e 42 75 66 3b 20 20 20 20 20 int nBuf;
1f770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f771 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 42 75 /* Size of zBu
1f772 66 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a f[] in bytes */.
1f773 20 20 63 68 61 72 20 2a 7a 42 75 66 3b 20 20 20 char *zBuf;
1f774 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f775 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 62 75 /* Space to bu
1f776 66 66 65 72 20 6a 6f 75 72 6e 61 6c 20 77 72 69 ffer journal wri
1f777 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 53 69 tes */. int iSi
1f778 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ze;
1f779 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 /* Amou
1f77a 6e 74 20 6f 66 20 7a 42 75 66 5b 5d 20 63 75 72 nt of zBuf[] cur
1f77b 72 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20 rently used */.
1f77c 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 int flags;
1f77d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f77e 20 2f 2a 20 78 4f 70 65 6e 20 66 6c 61 67 73 20 /* xOpen flags
1f77f 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 */. sqlite3_vfs
1f780 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 *pVfs;
1f781 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65 61 /* The "rea
1f782 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 56 46 l" underlying VF
1f783 53 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 S */. sqlite3_f
1f784 69 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 ile *pReal;
1f785 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72 /* The "r
1f786 65 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 eal" underlying
1f787 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
1f788 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
1f789 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 *zJournal;
1f78a 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
1f78b 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
1f78c 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 */.};.typedef s
1f78d 74 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 69 6c truct JournalFil
1f78e 65 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 3b 0a 0a e JournalFile;..
1f78f 2f 2a 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 /*.** If it does
1f790 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 not already exi
1f791 73 74 73 2c 20 63 72 65 61 74 65 20 61 6e 64 20 sts, create and
1f792 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 6e 2d populate the on-
1f793 64 69 73 6b 20 66 69 6c 65 20 0a 2a 2a 20 66 6f disk file .** fo
1f794 72 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 70 2e r JournalFile p.
1f795 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 .*/.static int c
1f796 72 65 61 74 65 46 69 6c 65 28 4a 6f 75 72 6e 61 reateFile(Journa
1f797 6c 46 69 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 lFile *p){. int
1f798 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1f799 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c . if( !p->pReal
1f79a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
1f79b 66 69 6c 65 20 2a 70 52 65 61 6c 20 3d 20 28 73 file *pReal = (s
1f79c 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 26 70 qlite3_file *)&p
1f79d 5b 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 [1];. rc = sq
1f79e 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 2d 3e 70 lite3OsOpen(p->p
1f79f 56 66 73 2c 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c Vfs, p->zJournal
1f7a0 2c 20 70 52 65 61 6c 2c 20 70 2d 3e 66 6c 61 67 , pReal, p->flag
1f7a1 73 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 s, 0);. if( r
1f7a2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1f7a3 20 20 20 20 20 20 70 2d 3e 70 52 65 61 6c 20 3d p->pReal =
1f7a4 20 70 52 65 61 6c 3b 0a 20 20 20 20 20 20 69 66 pReal;. if
1f7a5 28 20 70 2d 3e 69 53 69 7a 65 3e 30 20 29 7b 0a ( p->iSize>0 ){.
1f7a6 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 assert(p
1f7a7 2d 3e 69 53 69 7a 65 3c 3d 70 2d 3e 6e 42 75 66 ->iSize<=p->nBuf
1f7a8 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
1f7a9 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 sqlite3OsWrite(p
1f7aa 2d 3e 70 52 65 61 6c 2c 20 70 2d 3e 7a 42 75 66 ->pReal, p->zBuf
1f7ab 2c 20 70 2d 3e 69 53 69 7a 65 2c 20 30 29 3b 0a , p->iSize, 0);.
1f7ac 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1f7ad 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1f7ae 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 ../*.** Close th
1f7af 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 e file..*/.stati
1f7b0 63 20 69 6e 74 20 6a 72 6e 6c 43 6c 6f 73 65 28 c int jrnlClose(
1f7b1 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a sqlite3_file *pJ
1f7b2 66 64 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 fd){. JournalFi
1f7b3 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c le *p = (Journal
1f7b4 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 File *)pJfd;. i
1f7b5 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 f( p->pReal ){.
1f7b6 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 sqlite3OsClos
1f7b7 65 28 70 2d 3e 70 52 65 61 6c 29 3b 0a 20 20 7d e(p->pReal);. }
1f7b8 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
1f7b9 70 2d 3e 7a 42 75 66 29 3b 0a 20 20 72 65 74 75 p->zBuf);. retu
1f7ba 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1f7bb 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 ./*.** Read data
1f7bc 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e 0a from the file..
1f7bd 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 */.static int jr
1f7be 6e 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 nlRead(. sqlite
1f7bf 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 3_file *pJfd,
1f7c0 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 /* The journal
1f7c1 66 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 file from which
1f7c2 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 to read */. voi
1f7c3 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 d *zBuf,
1f7c4 20 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72 /* Put the r
1f7c5 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 esults here */.
1f7c6 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 int iAmt,
1f7c7 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1f7c8 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 r of bytes to re
1f7c9 61 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 ad */. sqlite_i
1f7ca 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f nt64 iOfst /
1f7cb 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 * Begin reading
1f7cc 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a at this offset *
1f7cd 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 /.){. int rc =
1f7ce 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 SQLITE_OK;. Jou
1f7cf 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a rnalFile *p = (J
1f7d0 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 ournalFile *)pJf
1f7d1 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 d;. if( p->pRea
1f7d2 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 l ){. rc = sq
1f7d3 6c 69 74 65 33 4f 73 52 65 61 64 28 70 2d 3e 70 lite3OsRead(p->p
1f7d4 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d 74 Real, zBuf, iAmt
1f7d5 2c 20 69 4f 66 73 74 29 3b 0a 20 20 7d 65 6c 73 , iOfst);. }els
1f7d6 65 20 69 66 28 20 28 69 41 6d 74 2b 69 4f 66 73 e if( (iAmt+iOfs
1f7d7 74 29 3e 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 t)>p->iSize ){.
1f7d8 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 rc = SQLITE_I
1f7d9 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b OERR_SHORT_READ;
1f7da 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 . }else{. me
1f7db 6d 63 70 79 28 7a 42 75 66 2c 20 26 70 2d 3e 7a mcpy(zBuf, &p->z
1f7dc 42 75 66 5b 69 4f 66 73 74 5d 2c 20 69 41 6d 74 Buf[iOfst], iAmt
1f7dd 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1f7de 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 rc;.}../*.** Wri
1f7df 74 65 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 te data to the f
1f7e0 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ile..*/.static i
1f7e1 6e 74 20 6a 72 6e 6c 57 72 69 74 65 28 0a 20 20 nt jrnlWrite(.
1f7e2 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a sqlite3_file *pJ
1f7e3 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f fd, /* The jo
1f7e4 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 urnal file into
1f7e5 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 2a which to write *
1f7e6 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a /. const void *
1f7e7 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 61 zBuf, /* Ta
1f7e8 6b 65 20 64 61 74 61 20 74 6f 20 62 65 20 77 72 ke data to be wr
1f7e9 69 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 20 itten from here
1f7ea 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 */. int iAmt,
1f7eb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1f7ec 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 umber of bytes t
1f7ed 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c o write */. sql
1f7ee 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20 ite_int64 iOfst
1f7ef 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72 69 /* Begin wri
1f7f0 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 ting at this off
1f7f1 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c set into the fil
1f7f2 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
1f7f3 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1f7f4 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d JournalFile *p =
1f7f5 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 (JournalFile *)
1f7f6 70 4a 66 64 3b 0a 20 20 69 66 28 20 21 70 2d 3e pJfd;. if( !p->
1f7f7 70 52 65 61 6c 20 26 26 20 28 69 4f 66 73 74 2b pReal && (iOfst+
1f7f8 69 41 6d 74 29 3e 70 2d 3e 6e 42 75 66 20 29 7b iAmt)>p->nBuf ){
1f7f9 0a 20 20 20 20 72 63 20 3d 20 63 72 65 61 74 65 . rc = create
1f7fa 46 69 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20 69 File(p);. }. i
1f7fb 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1f7fc 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 ){. if( p->p
1f7fd 52 65 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 Real ){. rc
1f7fe 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
1f7ff 65 28 70 2d 3e 70 52 65 61 6c 2c 20 7a 42 75 66 e(p->pReal, zBuf
1f800 2c 20 69 41 6d 74 2c 20 69 4f 66 73 74 29 3b 0a , iAmt, iOfst);.
1f801 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1f802 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a 42 75 66 memcpy(&p->zBuf
1f803 5b 69 4f 66 73 74 5d 2c 20 7a 42 75 66 2c 20 69 [iOfst], zBuf, i
1f804 41 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 Amt);. if(
1f805 70 2d 3e 69 53 69 7a 65 3c 28 69 4f 66 73 74 2b p->iSize<(iOfst+
1f806 69 41 6d 74 29 20 29 7b 0a 20 20 20 20 20 20 20 iAmt) ){.
1f807 20 70 2d 3e 69 53 69 7a 65 20 3d 20 28 69 4f 66 p->iSize = (iOf
1f808 73 74 2b 69 41 6d 74 29 3b 0a 20 20 20 20 20 20 st+iAmt);.
1f809 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 }. }. }. re
1f80a 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1f80b 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 66 * Truncate the f
1f80c 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ile..*/.static i
1f80d 6e 74 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28 nt jrnlTruncate(
1f80e 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a sqlite3_file *pJ
1f80f 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 fd, sqlite_int64
1f810 20 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 size){. int rc
1f811 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1f812 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d JournalFile *p =
1f813 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 (JournalFile *)
1f814 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 pJfd;. if( p->p
1f815 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d Real ){. rc =
1f816 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 sqlite3OsTrunca
1f817 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 73 69 7a te(p->pReal, siz
1f818 65 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 e);. }else if(
1f819 73 69 7a 65 3c 70 2d 3e 69 53 69 7a 65 20 29 7b size<p->iSize ){
1f81a 0a 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 . p->iSize =
1f81b 73 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 size;. }. retu
1f81c 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1f81d 53 79 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a Sync the file..*
1f81e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e /.static int jrn
1f81f 6c 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 lSync(sqlite3_fi
1f820 6c 65 20 2a 70 4a 66 64 2c 20 69 6e 74 20 66 6c le *pJfd, int fl
1f821 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a ags){. int rc;.
1f822 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 JournalFile *p
1f823 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 = (JournalFile
1f824 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d *)pJfd;. if( p-
1f825 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 >pReal ){. rc
1f826 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 = sqlite3OsSync
1f827 28 70 2d 3e 70 52 65 61 6c 2c 20 66 6c 61 67 73 (p->pReal, flags
1f828 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1f829 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1f82a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1f82b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 .}../*.** Query
1f82c 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
1f82d 66 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a 2a file in bytes..*
1f82e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e /.static int jrn
1f82f 6c 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 lFileSize(sqlite
1f830 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 3_file *pJfd, sq
1f831 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a lite_int64 *pSiz
1f832 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 e){. int rc = S
1f833 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 QLITE_OK;. Jour
1f834 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f nalFile *p = (Jo
1f835 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 urnalFile *)pJfd
1f836 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c ;. if( p->pReal
1f837 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
1f838 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 ite3OsFileSize(p
1f839 2d 3e 70 52 65 61 6c 2c 20 70 53 69 7a 65 29 3b ->pReal, pSize);
1f83a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 . }else{. *p
1f83b 53 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f 69 Size = (sqlite_i
1f83c 6e 74 36 34 29 20 70 2d 3e 69 53 69 7a 65 3b 0a nt64) p->iSize;.
1f83d 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1f83e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 .}../*.** Table
1f83f 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 4a of methods for J
1f840 6f 75 72 6e 61 6c 46 69 6c 65 20 73 71 6c 69 74 ournalFile sqlit
1f841 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 0a e3_file object..
1f842 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 */.static struct
1f843 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
1f844 6f 64 73 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d ods JournalFileM
1f845 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c 20 ethods = {. 1,
1f846 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 /* i
1f847 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6a 72 6e Version */. jrn
1f848 6c 43 6c 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78 lClose, /* x
1f849 43 6c 6f 73 65 20 2a 2f 0a 20 20 6a 72 6e 6c 52 Close */. jrnlR
1f84a 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65 ead, /* xRe
1f84b 61 64 20 2a 2f 0a 20 20 6a 72 6e 6c 57 72 69 74 ad */. jrnlWrit
1f84c 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 69 74 65 e, /* xWrite
1f84d 20 2a 2f 0a 20 20 6a 72 6e 6c 54 72 75 6e 63 61 */. jrnlTrunca
1f84e 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 te, /* xTruncat
1f84f 65 20 2a 2f 0a 20 20 6a 72 6e 6c 53 79 6e 63 2c e */. jrnlSync,
1f850 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a /* xSync *
1f851 2f 0a 20 20 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 /. jrnlFileSize
1f852 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 , /* xFileSize
1f853 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 */. 0,
1f854 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a /* xLock */.
1f855 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
1f856 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 /* xUnlock */.
1f857 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1f858 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 /* xCheckReserve
1f859 64 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 dLock */. 0,
1f85a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 /* xFi
1f85b 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 leControl */. 0
1f85c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
1f85d 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 0a xSectorSize */.
1f85e 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 0
1f85f 20 2f 2a 20 78 44 65 76 69 63 65 43 68 61 72 61 /* xDeviceChara
1f860 63 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b cteristics */.};
1f861 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 ../* .** Open a
1f862 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f journal file..*/
1f863 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1f864 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e int sqlite3Journ
1f865 61 6c 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 alOpen(. sqlite
1f866 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 3_vfs *pVfs,
1f867 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 /* The VFS
1f868 74 6f 20 75 73 65 20 66 6f 72 20 61 63 74 75 61 to use for actua
1f869 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20 20 l file I/O */.
1f86a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
1f86b 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 e, /* Na
1f86c 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 me of the journa
1f86d 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 l file */. sqli
1f86e 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 te3_file *pJfd,
1f86f 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c /* Preall
1f870 6f 63 61 74 65 64 2c 20 62 6c 61 6e 6b 20 66 69 ocated, blank fi
1f871 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 le handle */. i
1f872 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 nt flags,
1f873 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 /* Ope
1f874 6e 69 6e 67 20 66 6c 61 67 73 20 2a 2f 0a 20 20 ning flags */.
1f875 69 6e 74 20 6e 42 75 66 20 20 20 20 20 20 20 20 int nBuf
1f876 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 /* By
1f877 74 65 73 20 62 75 66 66 65 72 65 64 20 62 65 66 tes buffered bef
1f878 6f 72 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 ore opening the
1f879 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4a 6f 75 file */.){. Jou
1f87a 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a rnalFile *p = (J
1f87b 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 ournalFile *)pJf
1f87c 64 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 d;. memset(p, 0
1f87d 2c 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c , sqlite3Journal
1f87e 53 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 69 Size(pVfs));. i
1f87f 66 28 20 6e 42 75 66 3e 30 20 29 7b 0a 20 20 20 f( nBuf>0 ){.
1f880 20 70 2d 3e 7a 42 75 66 20 3d 20 73 71 6c 69 74 p->zBuf = sqlit
1f881 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 75 e3MallocZero(nBu
1f882 66 29 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e f);. if( !p->
1f883 7a 42 75 66 20 29 7b 0a 20 20 20 20 20 20 72 65 zBuf ){. re
1f884 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
1f885 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 M;. }. }else
1f886 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c {. return sql
1f887 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c ite3OsOpen(pVfs,
1f888 20 7a 4e 61 6d 65 2c 20 70 4a 66 64 2c 20 66 6c zName, pJfd, fl
1f889 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 ags, 0);. }. p
1f88a 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4a 6f 75 ->pMethod = &Jou
1f88b 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73 3b rnalFileMethods;
1f88c 0a 20 20 70 2d 3e 6e 42 75 66 20 3d 20 6e 42 75 . p->nBuf = nBu
1f88d 66 3b 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 f;. p->flags =
1f88e 66 6c 61 67 73 3b 0a 20 20 70 2d 3e 7a 4a 6f 75 flags;. p->zJou
1f88f 72 6e 61 6c 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 rnal = zName;.
1f890 70 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a p->pVfs = pVfs;.
1f891 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1f892 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 OK;.}../*.** If
1f893 74 68 65 20 61 72 67 75 6d 65 6e 74 20 70 20 70 the argument p p
1f894 6f 69 6e 74 73 20 74 6f 20 61 20 4a 6f 75 72 6e oints to a Journ
1f895 61 6c 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 alFile structure
1f896 2c 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72 6c , and the underl
1f897 79 69 6e 67 0a 2a 2a 20 66 69 6c 65 20 68 61 73 ying.** file has
1f898 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63 72 not yet been cr
1f899 65 61 74 65 64 2c 20 63 72 65 61 74 65 20 69 74 eated, create it
1f89a 20 6e 6f 77 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f now..*/.SQLITE_
1f89b 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1f89c 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 te3JournalCreate
1f89d 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
1f89e 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4d 65 74 ){. if( p->pMet
1f89f 68 6f 64 73 21 3d 26 4a 6f 75 72 6e 61 6c 46 69 hods!=&JournalFi
1f8a0 6c 65 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 leMethods ){.
1f8a1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1f8a2 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 K;. }. return
1f8a3 63 72 65 61 74 65 46 69 6c 65 28 28 4a 6f 75 72 createFile((Jour
1f8a4 6e 61 6c 46 69 6c 65 20 2a 29 70 29 3b 0a 7d 0a nalFile *)p);.}.
1f8a5 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ./* .** Return t
1f8a6 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
1f8a7 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 es required to s
1f8a8 74 6f 72 65 20 61 20 4a 6f 75 72 6e 61 6c 46 69 tore a JournalFi
1f8a9 6c 65 20 74 68 61 74 20 75 73 65 73 20 76 66 73 le that uses vfs
1f8aa 0a 2a 2a 20 70 56 66 73 20 74 6f 20 63 72 65 61 .** pVfs to crea
1f8ab 74 65 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e te the underlyin
1f8ac 67 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 73 2e g on-disk files.
1f8ad 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1f8ae 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f TE int sqlite3Jo
1f8af 75 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 urnalSize(sqlite
1f8b0 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 3_vfs *pVfs){.
1f8b1 72 65 74 75 72 6e 20 28 70 56 66 73 2d 3e 73 7a return (pVfs->sz
1f8b2 4f 73 46 69 6c 65 2b 73 69 7a 65 6f 66 28 4a 6f OsFile+sizeof(Jo
1f8b3 75 72 6e 61 6c 46 69 6c 65 29 29 3b 0a 7d 0a 23 urnalFile));.}.#
1f8b4 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a endif../********
1f8b5 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6a 6f ****** End of jo
1f8b6 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a urnal.c ********
1f8b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8b9 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
1f8ba 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
1f8bb 65 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a e memjournal.c *
1f8bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8be 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
1f8bf 38 20 4f 63 74 6f 62 65 72 20 37 0a 2a 2a 0a 2a 8 October 7.**.*
1f8c0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
1f8c1 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
1f8c2 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
1f8c3 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
1f8c4 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
1f8c5 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
1f8c6 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
1f8c7 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
1f8c8 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
1f8c9 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
1f8ca 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
1f8cb 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
1f8cc 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
1f8cd 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
1f8ce 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
1f8cf 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
1f8d0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
1f8d1 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
1f8d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
1f8d6 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
1f8d7 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f 20 ins code use to
1f8d8 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e 2d implement an in-
1f8d9 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 memory rollback
1f8da 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 journal..** The
1f8db 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 in-memory rollba
1f8dc 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75 73 ck journal is us
1f8dd 65 64 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 74 72 ed to journal tr
1f8de 61 6e 73 61 63 74 69 6f 6e 73 20 66 6f 72 0a 2a ansactions for.*
1f8df 2a 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 64 61 74 * ":memory:" dat
1f8e0 61 62 61 73 65 73 20 61 6e 64 20 77 68 65 6e 20 abases and when
1f8e1 74 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 the journal_mode
1f8e2 3d 4d 45 4d 4f 52 59 20 70 72 61 67 6d 61 20 69 =MEMORY pragma i
1f8e3 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 40 28 s used..**.** @(
1f8e4 23 29 20 24 49 64 3a 20 6d 65 6d 6a 6f 75 72 6e #) $Id: memjourn
1f8e5 61 6c 2e 63 2c 76 20 31 2e 31 32 20 32 30 30 39 al.c,v 1.12 2009
1f8e6 2f 30 35 2f 30 34 20 31 31 3a 34 32 3a 33 30 20 /05/04 11:42:30
1f8e7 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 danielk1977 Exp
1f8e8 24 0a 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 $.*/../* Forward
1f8e9 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 69 references to i
1f8ea 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 74 75 72 nternal structur
1f8eb 65 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 es */.typedef st
1f8ec 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 ruct MemJournal
1f8ed 4d 65 6d 4a 6f 75 72 6e 61 6c 3b 0a 74 79 70 65 MemJournal;.type
1f8ee 64 65 66 20 73 74 72 75 63 74 20 46 69 6c 65 50 def struct FileP
1f8ef 6f 69 6e 74 20 46 69 6c 65 50 6f 69 6e 74 3b 0a oint FilePoint;.
1f8f0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 typedef struct F
1f8f1 69 6c 65 43 68 75 6e 6b 20 46 69 6c 65 43 68 75 ileChunk FileChu
1f8f2 6e 6b 3b 0a 0a 2f 2a 20 53 70 61 63 65 20 74 6f nk;../* Space to
1f8f3 20 68 6f 6c 64 20 74 68 65 20 72 6f 6c 6c 62 61 hold the rollba
1f8f4 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c ck journal is al
1f8f5 6c 6f 63 61 74 65 64 20 69 6e 20 69 6e 63 72 65 located in incre
1f8f6 6d 65 6e 74 73 20 6f 66 0a 2a 2a 20 74 68 69 73 ments of.** this
1f8f7 20 6d 61 6e 79 20 62 79 74 65 73 2e 0a 2a 2a 0a many bytes..**.
1f8f8 2a 2a 20 54 68 65 20 73 69 7a 65 20 63 68 6f 73 ** The size chos
1f8f9 65 6e 20 69 73 20 61 20 6c 69 74 74 6c 65 20 6c en is a little l
1f8fa 65 73 73 20 74 68 61 6e 20 61 20 70 6f 77 65 72 ess than a power
1f8fb 20 6f 66 20 74 77 6f 2e 20 20 54 68 61 74 20 77 of two. That w
1f8fc 61 79 2c 0a 2a 2a 20 74 68 65 20 46 69 6c 65 43 ay,.** the FileC
1f8fd 68 75 6e 6b 20 6f 62 6a 65 63 74 20 77 69 6c 6c hunk object will
1f8fe 20 68 61 76 65 20 61 20 73 69 7a 65 20 74 68 61 have a size tha
1f8ff 74 20 61 6c 6d 6f 73 74 20 65 78 61 63 74 6c 79 t almost exactly
1f900 20 66 69 6c 6c 73 0a 2a 2a 20 61 20 70 6f 77 65 fills.** a powe
1f901 72 2d 6f 66 2d 74 77 6f 20 61 6c 6c 6f 63 61 74 r-of-two allocat
1f902 69 6f 6e 2e 20 20 54 68 69 73 20 6d 69 6d 69 6d ion. This mimim
1f903 69 7a 65 73 20 77 61 73 74 65 64 20 73 70 61 63 izes wasted spac
1f904 65 20 69 6e 20 70 6f 77 65 72 2d 6f 66 2d 74 77 e in power-of-tw
1f905 6f 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f o.** memory allo
1f906 63 61 74 6f 72 73 2e 0a 2a 2f 0a 23 64 65 66 69 cators..*/.#defi
1f907 6e 65 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b ne JOURNAL_CHUNK
1f908 53 49 5a 45 20 28 28 69 6e 74 29 28 31 30 32 34 SIZE ((int)(1024
1f909 2d 73 69 7a 65 6f 66 28 46 69 6c 65 43 68 75 6e -sizeof(FileChun
1f90a 6b 2a 29 29 29 0a 0a 2f 2a 20 4d 61 63 72 6f 20 k*)))../* Macro
1f90b 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 69 6e 69 to find the mini
1f90c 6d 75 6d 20 6f 66 20 74 77 6f 20 6e 75 6d 65 72 mum of two numer
1f90d 69 63 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 69 ic values..*/.#i
1f90e 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64 65 66 69 fndef MIN.# defi
1f90f 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28 28 78 29 ne MIN(x,y) ((x)
1f910 3c 28 79 29 3f 28 78 29 3a 28 79 29 29 0a 23 65 <(y)?(x):(y)).#e
1f911 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
1f912 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
1f913 20 69 73 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20 is composed of
1f914 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 a linked list of
1f915 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65 these structure
1f916 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 69 6c s..*/.struct Fil
1f917 65 43 68 75 6e 6b 20 7b 0a 20 20 46 69 6c 65 43 eChunk {. FileC
1f918 68 75 6e 6b 20 2a 70 4e 65 78 74 3b 20 20 20 20 hunk *pNext;
1f919 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 /* Ne
1f91a 78 74 20 63 68 75 6e 6b 20 69 6e 20 74 68 65 20 xt chunk in the
1f91b 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 journal */. u8
1f91c 7a 43 68 75 6e 6b 5b 4a 4f 55 52 4e 41 4c 5f 43 zChunk[JOURNAL_C
1f91d 48 55 4e 4b 53 49 5a 45 5d 3b 20 20 20 2f 2a 20 HUNKSIZE]; /*
1f91e 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20 Content of this
1f91f 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a chunk */.};../*.
1f920 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
1f921 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 65 f this object se
1f922 72 76 65 73 20 61 73 20 61 20 63 75 72 73 6f 72 rves as a cursor
1f923 20 69 6e 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 into the rollba
1f924 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 ck journal..** T
1f925 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62 65 he cursor can be
1f926 20 65 69 74 68 65 72 20 66 6f 72 20 72 65 61 64 either for read
1f927 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 2e 0a ing or writing..
1f928 2a 2f 0a 73 74 72 75 63 74 20 46 69 6c 65 50 6f */.struct FilePo
1f929 69 6e 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f int {. sqlite3_
1f92a 69 6e 74 36 34 20 69 4f 66 66 73 65 74 3b 20 20 int64 iOffset;
1f92b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
1f92c 74 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e t from the begin
1f92d 6e 69 6e 67 20 6f 66 20 74 68 65 20 66 69 6c 65 ning of the file
1f92e 20 2a 2f 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 */. FileChunk
1f92f 2a 70 43 68 75 6e 6b 3b 20 20 20 20 20 20 20 20 *pChunk;
1f930 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66 69 /* Specifi
1f931 63 20 63 68 75 6e 6b 20 69 6e 74 6f 20 77 68 69 c chunk into whi
1f932 63 68 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 ch cursor points
1f933 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 */.};../*.** Th
1f934 69 73 20 73 75 62 63 6c 61 73 73 20 69 73 20 61 is subclass is a
1f935 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c subclass of sql
1f936 69 74 65 33 5f 66 69 6c 65 2e 20 20 45 61 63 68 ite3_file. Each
1f937 20 6f 70 65 6e 20 6d 65 6d 6f 72 79 2d 6a 6f 75 open memory-jou
1f938 72 6e 61 6c 0a 2a 2a 20 69 73 20 61 6e 20 69 6e rnal.** is an in
1f939 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 63 stance of this c
1f93a 6c 61 73 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 lass..*/.struct
1f93b 4d 65 6d 4a 6f 75 72 6e 61 6c 20 7b 0a 20 20 73 MemJournal {. s
1f93c 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
1f93d 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f s *pMethod; /
1f93e 2a 20 50 61 72 65 6e 74 20 63 6c 61 73 73 2e 20 * Parent class.
1f93f 4d 55 53 54 20 42 45 20 46 49 52 53 54 20 2a 2f MUST BE FIRST */
1f940 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 46 . FileChunk *pF
1f941 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 irst;
1f942 20 20 20 2f 2a 20 48 65 61 64 20 6f 66 20 69 6e /* Head of in
1f943 2d 6d 65 6d 6f 72 79 20 63 68 75 6e 6b 2d 6c 69 -memory chunk-li
1f944 73 74 20 2a 2f 0a 20 20 46 69 6c 65 50 6f 69 6e st */. FilePoin
1f945 74 20 65 6e 64 70 6f 69 6e 74 3b 20 20 20 20 20 t endpoint;
1f946 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
1f947 65 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 er to the end of
1f948 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 46 the file */. F
1f949 69 6c 65 50 6f 69 6e 74 20 72 65 61 64 70 6f 69 ilePoint readpoi
1f94a 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f nt; /
1f94b 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 * Pointer to the
1f94c 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 end of the last
1f94d 20 78 52 65 61 64 28 29 20 2a 2f 0a 7d 3b 0a 0a xRead() */.};..
1f94e 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 /*.** Read data
1f94f 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d 6f from the in-memo
1f950 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e ry journal file.
1f951 20 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d This is the im
1f952 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 plementation.**
1f953 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 of the sqlite3_v
1f954 66 73 2e 78 52 65 61 64 20 6d 65 74 68 6f 64 2e fs.xRead method.
1f955 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
1f956 65 6d 6a 72 6e 6c 52 65 61 64 28 0a 20 20 73 71 emjrnlRead(. sq
1f957 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 lite3_file *pJfd
1f958 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 , /* The jour
1f959 6e 61 6c 20 66 69 6c 65 20 66 72 6f 6d 20 77 68 nal file from wh
1f95a 69 63 68 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 ich to read */.
1f95b 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 void *zBuf,
1f95c 20 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20 74 /* Put t
1f95d 68 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 he results here
1f95e 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 */. int iAmt,
1f95f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1f960 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 umber of bytes t
1f961 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 o read */. sqli
1f962 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20 20 te_int64 iOfst
1f963 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 /* Begin read
1f964 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 ing at this offs
1f965 65 74 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a 6f et */.){. MemJo
1f966 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a urnal *p = (MemJ
1f967 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 ournal *)pJfd;.
1f968 20 75 38 20 2a 7a 4f 75 74 20 3d 20 7a 42 75 66 u8 *zOut = zBuf
1f969 3b 0a 20 20 69 6e 74 20 6e 52 65 61 64 20 3d 20 ;. int nRead =
1f96a 69 41 6d 74 3b 0a 20 20 69 6e 74 20 69 43 68 75 iAmt;. int iChu
1f96b 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 46 69 6c 65 nkOffset;. File
1f96c 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 0a 0a Chunk *pChunk;..
1f96d 20 20 2f 2a 20 53 51 4c 69 74 65 20 6e 65 76 65 /* SQLite neve
1f96e 72 20 74 72 69 65 73 20 74 6f 20 72 65 61 64 20 r tries to read
1f96f 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 past the end of
1f970 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e a rollback journ
1f971 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 61 73 73 al file */. ass
1f972 65 72 74 28 20 69 4f 66 73 74 2b 69 41 6d 74 3c ert( iOfst+iAmt<
1f973 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 =p->endpoint.iOf
1f974 66 73 65 74 20 29 3b 0a 0a 20 20 69 66 28 20 70 fset );.. if( p
1f975 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66 66 ->readpoint.iOff
1f976 73 65 74 21 3d 69 4f 66 73 74 20 7c 7c 20 69 4f set!=iOfst || iO
1f977 66 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 fst==0 ){. sq
1f978 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 66 lite3_int64 iOff
1f979 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70 43 = 0;. for(pC
1f97a 68 75 6e 6b 3d 70 2d 3e 70 46 69 72 73 74 3b 20 hunk=p->pFirst;
1f97b 0a 20 20 20 20 20 20 20 20 41 4c 57 41 59 53 28 . ALWAYS(
1f97c 70 43 68 75 6e 6b 29 20 26 26 20 28 69 4f 66 66 pChunk) && (iOff
1f97d 2b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 +JOURNAL_CHUNKSI
1f97e 5a 45 29 3c 3d 69 4f 66 73 74 3b 0a 20 20 20 20 ZE)<=iOfst;.
1f97f 20 20 20 20 70 43 68 75 6e 6b 3d 70 43 68 75 6e pChunk=pChun
1f980 6b 2d 3e 70 4e 65 78 74 0a 20 20 20 20 29 7b 0a k->pNext. ){.
1f981 20 20 20 20 20 20 69 4f 66 66 20 2b 3d 20 4a 4f iOff += JO
1f982 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 3b URNAL_CHUNKSIZE;
1f983 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
1f984 20 20 20 20 70 43 68 75 6e 6b 20 3d 20 70 2d 3e pChunk = p->
1f985 72 65 61 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b readpoint.pChunk
1f986 3b 0a 20 20 7d 0a 0a 20 20 69 43 68 75 6e 6b 4f ;. }.. iChunkO
1f987 66 66 73 65 74 20 3d 20 28 69 6e 74 29 28 69 4f ffset = (int)(iO
1f988 66 73 74 25 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e fst%JOURNAL_CHUN
1f989 4b 53 49 5a 45 29 3b 0a 20 20 64 6f 20 7b 0a 20 KSIZE);. do {.
1f98a 20 20 20 69 6e 74 20 69 53 70 61 63 65 20 3d 20 int iSpace =
1f98b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a JOURNAL_CHUNKSIZ
1f98c 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 E - iChunkOffset
1f98d 3b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 70 79 20 ;. int nCopy
1f98e 3d 20 4d 49 4e 28 6e 52 65 61 64 2c 20 28 4a 4f = MIN(nRead, (JO
1f98f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 URNAL_CHUNKSIZE
1f990 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 29 29 - iChunkOffset))
1f991 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f 75 ;. memcpy(zOu
1f992 74 2c 20 26 70 43 68 75 6e 6b 2d 3e 7a 43 68 75 t, &pChunk->zChu
1f993 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74 5d nk[iChunkOffset]
1f994 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 7a 4f , nCopy);. zO
1f995 75 74 20 2b 3d 20 6e 43 6f 70 79 3b 0a 20 20 20 ut += nCopy;.
1f996 20 6e 52 65 61 64 20 2d 3d 20 69 53 70 61 63 65 nRead -= iSpace
1f997 3b 0a 20 20 20 20 69 43 68 75 6e 6b 4f 66 66 73 ;. iChunkOffs
1f998 65 74 20 3d 20 30 3b 0a 20 20 7d 20 77 68 69 6c et = 0;. } whil
1f999 65 28 20 6e 52 65 61 64 3e 3d 30 20 26 26 20 28 e( nRead>=0 && (
1f99a 70 43 68 75 6e 6b 3d 70 43 68 75 6e 6b 2d 3e 70 pChunk=pChunk->p
1f99b 4e 65 78 74 29 21 3d 30 20 26 26 20 6e 52 65 61 Next)!=0 && nRea
1f99c 64 3e 30 20 29 3b 0a 20 20 70 2d 3e 72 65 61 64 d>0 );. p->read
1f99d 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 3d 20 point.iOffset =
1f99e 69 4f 66 73 74 2b 69 41 6d 74 3b 0a 20 20 70 2d iOfst+iAmt;. p-
1f99f 3e 72 65 61 64 70 6f 69 6e 74 2e 70 43 68 75 6e >readpoint.pChun
1f9a0 6b 20 3d 20 70 43 68 75 6e 6b 3b 0a 0a 20 20 72 k = pChunk;.. r
1f9a1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1f9a2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 .}../*.** Write
1f9a3 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65 data to the file
1f9a4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1f9a5 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 28 0a 20 20 memjrnlWrite(.
1f9a6 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a sqlite3_file *pJ
1f9a7 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f fd, /* The jo
1f9a8 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 urnal file into
1f9a9 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 2a which to write *
1f9aa 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a /. const void *
1f9ab 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 61 zBuf, /* Ta
1f9ac 6b 65 20 64 61 74 61 20 74 6f 20 62 65 20 77 72 ke data to be wr
1f9ad 69 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 20 itten from here
1f9ae 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 */. int iAmt,
1f9af 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1f9b0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 umber of bytes t
1f9b1 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c o write */. sql
1f9b2 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20 ite_int64 iOfst
1f9b3 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72 69 /* Begin wri
1f9b4 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 ting at this off
1f9b5 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c set into the fil
1f9b6 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a 6f 75 e */.){. MemJou
1f9b7 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f rnal *p = (MemJo
1f9b8 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 urnal *)pJfd;.
1f9b9 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 69 41 6d int nWrite = iAm
1f9ba 74 3b 0a 20 20 75 38 20 2a 7a 57 72 69 74 65 20 t;. u8 *zWrite
1f9bb 3d 20 28 75 38 20 2a 29 7a 42 75 66 3b 0a 0a 20 = (u8 *)zBuf;..
1f9bc 20 2f 2a 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79 /* An in-memory
1f9bd 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 68 journal file sh
1f9be 6f 75 6c 64 20 6f 6e 6c 79 20 65 76 65 72 20 62 ould only ever b
1f9bf 65 20 61 70 70 65 6e 64 65 64 20 74 6f 2e 20 52 e appended to. R
1f9c0 61 6e 64 6f 6d 0a 20 20 2a 2a 20 61 63 63 65 73 andom. ** acces
1f9c1 73 20 77 72 69 74 65 73 20 61 72 65 20 6e 6f 74 s writes are not
1f9c2 20 72 65 71 75 69 72 65 64 20 62 79 20 73 71 6c required by sql
1f9c3 69 74 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 ite.. */. asse
1f9c4 72 74 28 20 69 4f 66 73 74 3d 3d 70 2d 3e 65 6e rt( iOfst==p->en
1f9c5 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 29 dpoint.iOffset )
1f9c6 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
1f9c7 45 54 45 52 28 69 4f 66 73 74 29 3b 0a 0a 20 20 ETER(iOfst);..
1f9c8 77 68 69 6c 65 28 20 6e 57 72 69 74 65 3e 30 20 while( nWrite>0
1f9c9 29 7b 0a 20 20 20 20 46 69 6c 65 43 68 75 6e 6b ){. FileChunk
1f9ca 20 2a 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 65 6e *pChunk = p->en
1f9cb 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a 20 dpoint.pChunk;.
1f9cc 20 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66 int iChunkOff
1f9cd 73 65 74 20 3d 20 28 69 6e 74 29 28 70 2d 3e 65 set = (int)(p->e
1f9ce 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 25 ndpoint.iOffset%
1f9cf 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a JOURNAL_CHUNKSIZ
1f9d0 45 29 3b 0a 20 20 20 20 69 6e 74 20 69 53 70 61 E);. int iSpa
1f9d1 63 65 20 3d 20 4d 49 4e 28 6e 57 72 69 74 65 2c ce = MIN(nWrite,
1f9d2 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 JOURNAL_CHUNKSI
1f9d3 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65 ZE - iChunkOffse
1f9d4 74 29 3b 0a 0a 20 20 20 20 69 66 28 20 69 43 68 t);.. if( iCh
1f9d5 75 6e 6b 4f 66 66 73 65 74 3d 3d 30 20 29 7b 0a unkOffset==0 ){.
1f9d6 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 68 75 /* New chu
1f9d7 6e 6b 20 69 73 20 72 65 71 75 69 72 65 64 20 74 nk is required t
1f9d8 6f 20 65 78 74 65 6e 64 20 74 68 65 20 66 69 6c o extend the fil
1f9d9 65 2e 20 2a 2f 0a 20 20 20 20 20 20 46 69 6c 65 e. */. File
1f9da 43 68 75 6e 6b 20 2a 70 4e 65 77 20 3d 20 73 71 Chunk *pNew = sq
1f9db 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a lite3_malloc(siz
1f9dc 65 6f 66 28 46 69 6c 65 43 68 75 6e 6b 29 29 3b eof(FileChunk));
1f9dd 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 77 . if( !pNew
1f9de 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
1f9df 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
1f9e0 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 NOMEM;. }.
1f9e1 20 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74 pNew->pNext
1f9e2 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 = 0;. if(
1f9e3 70 43 68 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 pChunk ){.
1f9e4 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 46 69 assert( p->pFi
1f9e5 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 70 rst );. p
1f9e6 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 Chunk->pNext = p
1f9e7 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 New;. }else
1f9e8 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
1f9e9 28 20 21 70 2d 3e 70 46 69 72 73 74 20 29 3b 0a ( !p->pFirst );.
1f9ea 20 20 20 20 20 20 20 20 70 2d 3e 70 46 69 72 73 p->pFirs
1f9eb 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 t = pNew;.
1f9ec 7d 0a 20 20 20 20 20 20 70 2d 3e 65 6e 64 70 6f }. p->endpo
1f9ed 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70 4e 65 int.pChunk = pNe
1f9ee 77 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d 65 w;. }.. me
1f9ef 6d 63 70 79 28 26 70 2d 3e 65 6e 64 70 6f 69 6e mcpy(&p->endpoin
1f9f0 74 2e 70 43 68 75 6e 6b 2d 3e 7a 43 68 75 6e 6b t.pChunk->zChunk
1f9f1 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74 5d 2c 20 [iChunkOffset],
1f9f2 7a 57 72 69 74 65 2c 20 69 53 70 61 63 65 29 3b zWrite, iSpace);
1f9f3 0a 20 20 20 20 7a 57 72 69 74 65 20 2b 3d 20 69 . zWrite += i
1f9f4 53 70 61 63 65 3b 0a 20 20 20 20 6e 57 72 69 74 Space;. nWrit
1f9f5 65 20 2d 3d 20 69 53 70 61 63 65 3b 0a 20 20 20 e -= iSpace;.
1f9f6 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 p->endpoint.iOf
1f9f7 66 73 65 74 20 2b 3d 20 69 53 70 61 63 65 3b 0a fset += iSpace;.
1f9f8 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 }.. return SQ
1f9f9 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
1f9fa 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 66 * Truncate the f
1f9fb 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ile..*/.static i
1f9fc 6e 74 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 nt memjrnlTrunca
1f9fd 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 te(sqlite3_file
1f9fe 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e *pJfd, sqlite_in
1f9ff 74 36 34 20 73 69 7a 65 29 7b 0a 20 20 4d 65 6d t64 size){. Mem
1fa00 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 Journal *p = (Me
1fa01 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b mJournal *)pJfd;
1fa02 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 . FileChunk *pC
1fa03 68 75 6e 6b 3b 0a 20 20 61 73 73 65 72 74 28 73 hunk;. assert(s
1fa04 69 7a 65 3d 3d 30 29 3b 0a 20 20 55 4e 55 53 45 ize==0);. UNUSE
1fa05 44 5f 50 41 52 41 4d 45 54 45 52 28 73 69 7a 65 D_PARAMETER(size
1fa06 29 3b 0a 20 20 70 43 68 75 6e 6b 20 3d 20 70 2d );. pChunk = p-
1fa07 3e 70 46 69 72 73 74 3b 0a 20 20 77 68 69 6c 65 >pFirst;. while
1fa08 28 20 70 43 68 75 6e 6b 20 29 7b 0a 20 20 20 20 ( pChunk ){.
1fa09 46 69 6c 65 43 68 75 6e 6b 20 2a 70 54 6d 70 20 FileChunk *pTmp
1fa0a 3d 20 70 43 68 75 6e 6b 3b 0a 20 20 20 20 70 43 = pChunk;. pC
1fa0b 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 2d 3e 70 hunk = pChunk->p
1fa0c 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 Next;. sqlite
1fa0d 33 5f 66 72 65 65 28 70 54 6d 70 29 3b 0a 20 20 3_free(pTmp);.
1fa0e 7d 0a 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f }. sqlite3MemJo
1fa0f 75 72 6e 61 6c 4f 70 65 6e 28 70 4a 66 64 29 3b urnalOpen(pJfd);
1fa10 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1fa11 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c _OK;.}../*.** Cl
1fa12 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f ose the file..*/
1fa13 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a .static int memj
1fa14 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 rnlClose(sqlite3
1fa15 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20 _file *pJfd){.
1fa16 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28 memjrnlTruncate(
1fa17 70 4a 66 64 2c 20 30 29 3b 0a 20 20 72 65 74 75 pJfd, 0);. retu
1fa18 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1fa19 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 ../*.** Sync the
1fa1a 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 79 6e file..**.** Syn
1fa1b 63 69 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 cing an in-memor
1fa1c 79 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 20 6e y journal is a n
1fa1d 6f 2d 6f 70 2e 20 20 41 6e 64 2c 20 69 6e 20 66 o-op. And, in f
1fa1e 61 63 74 2c 20 74 68 69 73 20 72 6f 75 74 69 6e act, this routin
1fa1f 65 0a 2a 2a 20 69 73 20 6e 65 76 65 72 20 63 61 e.** is never ca
1fa20 6c 6c 65 64 20 69 6e 20 61 20 77 6f 72 6b 69 6e lled in a workin
1fa21 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e g implementation
1fa22 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e . This implemen
1fa23 74 61 74 69 6f 6e 0a 2a 2a 20 65 78 69 73 74 73 tation.** exists
1fa24 20 70 75 72 65 6c 79 20 61 73 20 61 20 63 6f 6e purely as a con
1fa25 74 69 6e 67 65 6e 63 79 2c 20 69 6e 20 63 61 73 tingency, in cas
1fa26 65 20 73 6f 6d 65 20 6d 61 6c 66 75 6e 63 74 69 e some malfuncti
1fa27 6f 6e 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 on in some other
1fa28 0a 2a 2a 20 70 61 72 74 20 6f 66 20 53 51 4c 69 .** part of SQLi
1fa29 74 65 20 63 61 75 73 65 73 20 53 79 6e 63 20 74 te causes Sync t
1fa2a 6f 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20 6d o be called by m
1fa2b 69 73 74 61 6b 65 2e 0a 2a 2f 0a 73 74 61 74 69 istake..*/.stati
1fa2c 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 53 79 6e c int memjrnlSyn
1fa2d 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a c(sqlite3_file *
1fa2e 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 NotUsed, int Not
1fa2f 55 73 65 64 32 29 7b 20 20 20 2f 2a 4e 4f 5f 54 Used2){ /*NO_T
1fa30 45 53 54 2a 2f 0a 20 20 55 4e 55 53 45 44 5f 50 EST*/. UNUSED_P
1fa31 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 ARAMETER2(NotUse
1fa32 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 20 20 20 d, NotUsed2);
1fa33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa34 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f /*NO_TEST*/
1fa35 0a 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 20 . assert( 0 );
1fa36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa39 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 72 65 /*NO_TEST*/. re
1fa3a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
1fa3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa3d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f /*NO_
1fa3e 54 45 53 54 2a 2f 0a 7d 20 20 20 20 20 20 20 20 TEST*/.}
1fa3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa42 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a /*NO_TEST*
1fa43 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 /../*.** Query t
1fa44 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 he size of the f
1fa45 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f ile in bytes..*/
1fa46 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a .static int memj
1fa47 72 6e 6c 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 rnlFileSize(sqli
1fa48 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 te3_file *pJfd,
1fa49 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 53 sqlite_int64 *pS
1fa4a 69 7a 65 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e ize){. MemJourn
1fa4b 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 al *p = (MemJour
1fa4c 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 2a 70 nal *)pJfd;. *p
1fa4d 53 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f 69 Size = (sqlite_i
1fa4e 6e 74 36 34 29 20 70 2d 3e 65 6e 64 70 6f 69 6e nt64) p->endpoin
1fa4f 74 2e 69 4f 66 66 73 65 74 3b 0a 20 20 72 65 74 t.iOffset;. ret
1fa50 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1fa51 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f 66 ../*.** Table of
1fa52 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 4d 65 6d methods for Mem
1fa53 4a 6f 75 72 6e 61 6c 20 73 71 6c 69 74 65 33 5f Journal sqlite3_
1fa54 66 69 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a file object..*/.
1fa55 73 74 61 74 69 63 20 73 74 72 75 63 74 20 73 71 static struct sq
1fa56 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
1fa57 20 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 6f MemJournalMetho
1fa58 64 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 ds = {. 1,
1fa59 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 /* iV
1fa5a 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6d 65 6d 6a ersion */. memj
1fa5b 72 6e 6c 43 6c 6f 73 65 2c 20 20 20 20 20 2f 2a rnlClose, /*
1fa5c 20 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 6d 65 6d xClose */. mem
1fa5d 6a 72 6e 6c 52 65 61 64 2c 20 20 20 20 20 20 2f jrnlRead, /
1fa5e 2a 20 78 52 65 61 64 20 2a 2f 0a 20 20 6d 65 6d * xRead */. mem
1fa5f 6a 72 6e 6c 57 72 69 74 65 2c 20 20 20 20 20 2f jrnlWrite, /
1fa60 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20 20 6d 65 * xWrite */. me
1fa61 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20 20 mjrnlTruncate,
1fa62 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a /* xTruncate */.
1fa63 20 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63 2c 20 20 memjrnlSync,
1fa64 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 0a /* xSync */.
1fa65 20 20 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 69 7a memjrnlFileSiz
1fa66 65 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 e, /* xFileSize
1fa67 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
1fa68 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b /* xLock
1fa69 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
1fa6a 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f /* xUnlo
1fa6b 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 ck */. 0,
1fa6c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 68 /* xCh
1fa6d 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 eckReservedLock
1fa6e 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 */. 0,
1fa6f 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 /* xFileC
1fa70 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20 20 ontrol */. 0,
1fa71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1fa72 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 0a xSectorSize */.
1fa73 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 0
1fa74 20 20 20 20 2f 2a 20 78 44 65 76 69 63 65 43 68 /* xDeviceCh
1fa75 61 72 61 63 74 65 72 69 73 74 69 63 73 20 2a 2f aracteristics */
1fa76 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 6e .};../* .** Open
1fa77 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e a journal file.
1fa78 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1fa79 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d TE void sqlite3M
1fa7a 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71 emJournalOpen(sq
1fa7b 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 lite3_file *pJfd
1fa7c 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 ){. MemJournal
1fa7d 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c *p = (MemJournal
1fa7e 20 2a 29 70 4a 66 64 3b 0a 20 20 61 73 73 65 72 *)pJfd;. asser
1fa7f 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c t( EIGHT_BYTE_AL
1fa80 49 47 4e 4d 45 4e 54 28 70 29 20 29 3b 0a 20 20 IGNMENT(p) );.
1fa81 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71 6c memset(p, 0, sql
1fa82 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 ite3MemJournalSi
1fa83 7a 65 28 29 29 3b 0a 20 20 70 2d 3e 70 4d 65 74 ze());. p->pMet
1fa84 68 6f 64 20 3d 20 26 4d 65 6d 4a 6f 75 72 6e 61 hod = &MemJourna
1fa85 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a 0a lMethods;.}../*.
1fa86 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 ** Return true i
1fa87 66 20 74 68 65 20 66 69 6c 65 2d 68 61 6e 64 6c f the file-handl
1fa88 65 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 e passed as an a
1fa89 72 67 75 6d 65 6e 74 20 69 73 20 0a 2a 2a 20 61 rgument is .** a
1fa8a 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 n in-memory jour
1fa8b 6e 61 6c 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 nal .*/.SQLITE_P
1fa8c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1fa8d 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 73 e3IsMemJournal(s
1fa8e 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 qlite3_file *pJf
1fa8f 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 4a 66 d){. return pJf
1fa90 64 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 26 4d 65 d->pMethods==&Me
1fa91 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 3b mJournalMethods;
1fa92 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 .}../* .** Retur
1fa93 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
1fa94 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 74 bytes required t
1fa95 6f 20 73 74 6f 72 65 20 61 20 4d 65 6d 4a 6f 75 o store a MemJou
1fa96 72 6e 61 6c 20 74 68 61 74 20 75 73 65 73 20 76 rnal that uses v
1fa97 66 73 0a 2a 2a 20 70 56 66 73 20 74 6f 20 63 72 fs.** pVfs to cr
1fa98 65 61 74 65 20 74 68 65 20 75 6e 64 65 72 6c 79 eate the underly
1fa99 69 6e 67 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 ing on-disk file
1fa9a 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
1fa9b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1fa9c 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 76 MemJournalSize(v
1fa9d 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 oid){. return s
1fa9e 69 7a 65 6f 66 28 4d 65 6d 4a 6f 75 72 6e 61 6c izeof(MemJournal
1fa9f 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a );.}../*********
1faa0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d ***** End of mem
1faa1 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a journal.c ******
1faa2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1faa3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1faa4 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1faa5 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
1faa6 20 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a 2a walker.c ******
1faa7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1faa8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1faa9 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 ****/./*.** 2008
1faaa 20 41 75 67 75 73 74 20 31 36 0a 2a 2a 0a 2a 2a August 16.**.**
1faab 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1faac 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1faad 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1faae 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1faaf 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
1fab0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
1fab1 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
1fab2 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
1fab3 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
1fab4 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
1fab5 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
1fab6 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
1fab7 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
1fab8 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
1fab9 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1faba 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
1fabb 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
1fabc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fabd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fabe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fabf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
1fac1 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
1fac2 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66 6f routines used fo
1fac3 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70 61 r walking the pa
1fac4 72 73 65 72 20 74 72 65 65 20 66 6f 72 0a 2a 2a rser tree for.**
1fac5 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e an SQL statemen
1fac6 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 77 61 t..**.** $Id: wa
1fac7 6c 6b 65 72 2e 63 2c 76 20 31 2e 37 20 32 30 30 lker.c,v 1.7 200
1fac8 39 2f 30 36 2f 31 35 20 32 33 3a 31 35 3a 35 39 9/06/15 23:15:59
1fac9 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a drh Exp $.*/...
1faca 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 /*.** Walk an ex
1facb 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 pression tree.
1facc 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 Invoke the callb
1facd 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 ack once for eac
1face 68 20 6e 6f 64 65 0a 2a 2a 20 6f 66 20 74 68 65 h node.** of the
1facf 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 77 68 69 expression, whi
1fad0 6c 65 20 64 65 63 65 6e 64 69 6e 67 2e 20 20 28 le decending. (
1fad1 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
1fad2 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 the callback.**
1fad3 69 73 20 69 6e 76 6f 6b 65 64 20 62 65 66 6f 72 is invoked befor
1fad4 65 20 76 69 73 69 74 69 6e 67 20 63 68 69 6c 64 e visiting child
1fad5 72 65 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ren.).**.** The
1fad6 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f return value fro
1fad7 6d 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 73 m the callback s
1fad8 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 6f 66 20 hould be one of
1fad9 74 68 65 20 57 52 43 5f 2a 0a 2a 2a 20 63 6f 6e the WRC_*.** con
1fada 73 74 61 6e 74 73 20 74 6f 20 73 70 65 63 69 66 stants to specif
1fadb 79 20 68 6f 77 20 74 6f 20 70 72 6f 63 65 65 64 y how to proceed
1fadc 20 77 69 74 68 20 74 68 65 20 77 61 6c 6b 2e 0a with the walk..
1fadd 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 43 6f 6e **.** WRC_Con
1fade 74 69 6e 75 65 20 20 20 20 20 20 43 6f 6e 74 69 tinue Conti
1fadf 6e 75 65 20 64 65 73 63 65 6e 64 69 6e 67 20 64 nue descending d
1fae0 6f 77 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a own the tree..**
1fae1 0a 2a 2a 20 20 20 20 57 52 43 5f 50 72 75 6e 65 .** WRC_Prune
1fae2 20 20 20 20 20 20 20 20 20 44 6f 20 6e 6f 74 20 Do not
1fae3 64 65 73 63 65 6e 64 20 69 6e 74 6f 20 63 68 69 descend into chi
1fae4 6c 64 20 6e 6f 64 65 73 2e 20 20 42 75 74 20 61 ld nodes. But a
1fae5 6c 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 llow.**
1fae6 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 the
1fae7 20 77 61 6c 6b 20 74 6f 20 63 6f 6e 74 69 6e 75 walk to continu
1fae8 65 20 77 69 74 68 20 73 69 62 6c 69 6e 67 20 6e e with sibling n
1fae9 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 57 odes..**.** W
1faea 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 20 RC_Abort
1faeb 20 44 6f 20 6e 6f 20 6d 6f 72 65 20 63 61 6c 6c Do no more call
1faec 62 61 63 6b 73 2e 20 20 55 6e 77 69 6e 64 20 74 backs. Unwind t
1faed 68 65 20 73 74 61 63 6b 20 61 6e 64 0a 2a 2a 20 he stack and.**
1faee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1faef 20 20 20 20 20 72 65 74 75 72 6e 20 74 68 65 20 return the
1faf0 74 6f 70 2d 6c 65 76 65 6c 20 77 61 6c 6b 20 63 top-level walk c
1faf1 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 all..**.** The r
1faf2 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d eturn value from
1faf3 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
1faf4 20 57 52 43 5f 41 62 6f 72 74 20 74 6f 20 61 62 WRC_Abort to ab
1faf5 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 65 20 77 andon the tree w
1faf6 61 6c 6b 0a 2a 2a 20 61 6e 64 20 57 52 43 5f 43 alk.** and WRC_C
1faf7 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6e 74 69 ontinue to conti
1faf8 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 nue..*/.SQLITE_P
1faf9 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1fafa 65 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 e3WalkExpr(Walke
1fafb 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 r *pWalker, Expr
1fafc 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20 *pExpr){. int
1fafd 72 63 3b 0a 20 20 69 66 28 20 70 45 78 70 72 3d rc;. if( pExpr=
1fafe 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f =0 ) return WRC_
1faff 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 74 65 73 74 Continue;. test
1fb00 63 61 73 65 28 20 45 78 70 72 48 61 73 50 72 6f case( ExprHasPro
1fb01 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
1fb02 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 3b 0a 20 20 TokenOnly) );.
1fb03 74 65 73 74 63 61 73 65 28 20 45 78 70 72 48 61 testcase( ExprHa
1fb04 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c sProperty(pExpr,
1fb05 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a EP_Reduced) );.
1fb06 20 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e rc = pWalker->
1fb07 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 28 70 57 xExprCallback(pW
1fb08 61 6c 6b 65 72 2c 20 70 45 78 70 72 29 3b 0a 20 alker, pExpr);.
1fb09 20 69 66 28 20 72 63 3d 3d 57 52 43 5f 43 6f 6e if( rc==WRC_Con
1fb0a 74 69 6e 75 65 0a 20 20 20 20 20 20 20 20 20 20 tinue.
1fb0b 20 20 20 20 26 26 20 21 45 78 70 72 48 61 73 41 && !ExprHasA
1fb0c 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 nyProperty(pExpr
1fb0d 2c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 ,EP_TokenOnly) )
1fb0e 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 {. if( sqlite
1fb0f 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 3WalkExpr(pWalke
1fb10 72 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 r, pExpr->pLeft)
1fb11 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 ) return WRC_Ab
1fb12 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c ort;. if( sql
1fb13 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 ite3WalkExpr(pWa
1fb14 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 52 69 lker, pExpr->pRi
1fb15 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 ght) ) return WR
1fb16 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 C_Abort;. if(
1fb17 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
1fb18 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 (pExpr, EP_xIsSe
1fb19 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 69 lect) ){. i
1fb1a 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 f( sqlite3WalkSe
1fb1b 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 lect(pWalker, pE
1fb1c 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 20 xpr->x.pSelect)
1fb1d 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f ) return WRC_Abo
1fb1e 72 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 rt;. }else{.
1fb1f 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1fb20 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 WalkExprList(pWa
1fb21 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70 lker, pExpr->x.p
1fb22 4c 69 73 74 29 20 29 20 72 65 74 75 72 6e 20 57 List) ) return W
1fb23 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a RC_Abort;. }.
1fb24 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 }. return rc
1fb25 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a & WRC_Abort;.}..
1fb26 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 /*.** Call sqlit
1fb27 65 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 e3WalkExpr() for
1fb28 20 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f every expressio
1fb29 6e 20 69 6e 20 6c 69 73 74 20 70 20 6f 72 20 75 n in list p or u
1fb2a 6e 74 69 6c 0a 2a 2a 20 61 6e 20 61 62 6f 72 74 ntil.** an abort
1fb2b 20 72 65 71 75 65 73 74 20 69 73 20 73 65 65 6e request is seen
1fb2c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1fb2d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
1fb2e 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c 6b alkExprList(Walk
1fb2f 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 er *pWalker, Exp
1fb30 72 4c 69 73 74 20 2a 70 29 7b 0a 20 20 69 6e 74 rList *p){. int
1fb31 20 69 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 i;. struct Exp
1fb32 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 rList_item *pIte
1fb33 6d 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 m;. if( p ){.
1fb34 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 45 78 70 72 for(i=p->nExpr
1fb35 2c 20 70 49 74 65 6d 3d 70 2d 3e 61 3b 20 69 3e , pItem=p->a; i>
1fb36 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 0; i--, pItem++)
1fb37 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 {. if( sqli
1fb38 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c te3WalkExpr(pWal
1fb39 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 ker, pItem->pExp
1fb3a 72 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f r) ) return WRC_
1fb3b 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d Abort;. }. }
1fb3c 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f . return WRC_Co
1fb3d 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ntinue;.}../*.**
1fb3e 20 57 61 6c 6b 20 61 6c 6c 20 65 78 70 72 65 73 Walk all expres
1fb3f 73 69 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 64 sions associated
1fb40 20 77 69 74 68 20 53 45 4c 45 43 54 20 73 74 61 with SELECT sta
1fb41 74 65 6d 65 6e 74 20 70 2e 20 20 44 6f 0a 2a 2a tement p. Do.**
1fb42 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 74 68 65 20 not invoke the
1fb43 53 45 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b 20 SELECT callback
1fb44 6f 6e 20 70 2c 20 62 75 74 20 64 6f 20 28 6f 66 on p, but do (of
1fb45 20 63 6f 75 72 73 65 29 20 69 6e 76 6f 6b 65 0a course) invoke.
1fb46 2a 2a 20 61 6e 79 20 65 78 70 72 20 63 61 6c 6c ** any expr call
1fb47 62 61 63 6b 73 20 61 6e 64 20 53 45 4c 45 43 54 backs and SELECT
1fb48 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 callbacks that
1fb49 63 6f 6d 65 20 66 72 6f 6d 20 73 75 62 71 75 65 come from subque
1fb4a 72 69 65 73 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 ries..** Return
1fb4b 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 WRC_Abort or WRC
1fb4c 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 _Continue..*/.SQ
1fb4d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1fb4e 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 sqlite3WalkSele
1fb4f 63 74 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 ctExpr(Walker *p
1fb50 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a Walker, Select *
1fb51 70 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 p){. if( sqlite
1fb52 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 3WalkExprList(pW
1fb53 61 6c 6b 65 72 2c 20 70 2d 3e 70 45 4c 69 73 74 alker, p->pEList
1fb54 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1fb55 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 bort;. if( sqli
1fb56 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c te3WalkExpr(pWal
1fb57 6b 65 72 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 ker, p->pWhere)
1fb58 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f ) return WRC_Abo
1fb59 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 rt;. if( sqlite
1fb5a 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 3WalkExprList(pW
1fb5b 61 6c 6b 65 72 2c 20 70 2d 3e 70 47 72 6f 75 70 alker, p->pGroup
1fb5c 42 79 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 By) ) return WRC
1fb5d 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 _Abort;. if( sq
1fb5e 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 lite3WalkExpr(pW
1fb5f 61 6c 6b 65 72 2c 20 70 2d 3e 70 48 61 76 69 6e alker, p->pHavin
1fb60 67 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f g) ) return WRC_
1fb61 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c Abort;. if( sql
1fb62 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 ite3WalkExprList
1fb63 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 72 (pWalker, p->pOr
1fb64 64 65 72 42 79 29 20 29 20 72 65 74 75 72 6e 20 derBy) ) return
1fb65 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 WRC_Abort;. if(
1fb66 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 sqlite3WalkExpr
1fb67 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c 69 (pWalker, p->pLi
1fb68 6d 69 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 mit) ) return WR
1fb69 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 C_Abort;. if( s
1fb6a 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 qlite3WalkExpr(p
1fb6b 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 66 66 73 Walker, p->pOffs
1fb6c 65 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 et) ) return WRC
1fb6d 5f 41 62 6f 72 74 3b 0a 20 20 72 65 74 75 72 6e _Abort;. return
1fb6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d WRC_Continue;.}
1fb6f 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 74 68 65 ../*.** Walk the
1fb70 20 70 61 72 73 65 20 74 72 65 65 73 20 61 73 73 parse trees ass
1fb71 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6c 6c ociated with all
1fb72 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 subqueries in t
1fb73 68 65 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 he.** FROM claus
1fb74 65 20 6f 66 20 53 45 4c 45 43 54 20 73 74 61 74 e of SELECT stat
1fb75 65 6d 65 6e 74 20 70 2e 20 20 44 6f 20 6e 6f 74 ement p. Do not
1fb76 20 69 6e 76 6f 6b 65 20 74 68 65 20 73 65 6c 65 invoke the sele
1fb77 63 74 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f ct.** callback o
1fb78 6e 20 70 2c 20 62 75 74 20 64 6f 20 69 6e 76 6f n p, but do invo
1fb79 6b 65 20 69 74 20 6f 6e 20 65 61 63 68 20 46 52 ke it on each FR
1fb7a 4f 4d 20 63 6c 61 75 73 65 20 73 75 62 71 75 65 OM clause subque
1fb7b 72 79 0a 2a 2a 20 61 6e 64 20 6f 6e 20 61 6e 79 ry.** and on any
1fb7c 20 73 75 62 71 75 65 72 69 65 73 20 66 75 72 74 subqueries furt
1fb7d 68 65 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20 her down in the
1fb7e 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 0a 2a tree. Return .*
1fb7f 2a 20 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 * WRC_Abort or W
1fb80 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 2a 2f 0a RC_Continue;.*/.
1fb81 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1fb82 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 nt sqlite3WalkSe
1fb83 6c 65 63 74 46 72 6f 6d 28 57 61 6c 6b 65 72 20 lectFrom(Walker
1fb84 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 *pWalker, Select
1fb85 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 *p){. SrcList
1fb86 2a 70 53 72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a *pSrc;. int i;.
1fb87 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 struct SrcList
1fb88 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 _item *pItem;..
1fb89 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b pSrc = p->pSrc;
1fb8a 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 53 . if( ALWAYS(pS
1fb8b 72 63 29 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 rc) ){. for(i
1fb8c 3d 70 53 72 63 2d 3e 6e 53 72 63 2c 20 70 49 74 =pSrc->nSrc, pIt
1fb8d 65 6d 3d 70 53 72 63 2d 3e 61 3b 20 69 3e 30 3b em=pSrc->a; i>0;
1fb8e 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a i--, pItem++){.
1fb8f 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
1fb90 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 3WalkSelect(pWal
1fb91 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c ker, pItem->pSel
1fb92 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 ect) ){.
1fb93 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1fb94 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1fb95 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 }. return WRC
1fb96 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 20 0a 0a 2f _Continue;.} ../
1fb97 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 *.** Call sqlite
1fb98 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 3WalkExpr() for
1fb99 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e every expression
1fb9a 20 69 6e 20 53 65 6c 65 63 74 20 73 74 61 74 65 in Select state
1fb9b 6d 65 6e 74 20 70 2e 0a 2a 2a 20 49 6e 76 6f 6b ment p..** Invok
1fb9c 65 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c e sqlite3WalkSel
1fb9d 65 63 74 28 29 20 66 6f 72 20 73 75 62 71 75 65 ect() for subque
1fb9e 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d ries in the FROM
1fb9f 20 63 6c 61 75 73 65 20 61 6e 64 0a 2a 2a 20 6f clause and.** o
1fba0 6e 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 n the compound s
1fba1 65 6c 65 63 74 20 63 68 61 69 6e 2c 20 70 2d 3e elect chain, p->
1fba2 70 50 72 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 pPrior..**.** Re
1fba3 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 turn WRC_Continu
1fba4 65 20 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 63 e under normal c
1fba5 6f 6e 64 69 74 69 6f 6e 73 2e 20 20 52 65 74 75 onditions. Retu
1fba6 72 6e 20 57 52 43 5f 41 62 6f 72 74 20 69 66 0a rn WRC_Abort if.
1fba7 2a 2a 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 ** there is an a
1fba8 62 6f 72 74 20 72 65 71 75 65 73 74 2e 0a 2a 2a bort request..**
1fba9 0a 2a 2a 20 49 66 20 74 68 65 20 57 61 6c 6b 65 .** If the Walke
1fbaa 72 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 r does not have
1fbab 61 6e 20 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 an xSelectCallba
1fbac 63 6b 28 29 20 74 68 65 6e 20 74 68 69 73 20 72 ck() then this r
1fbad 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e outine.** is a n
1fbae 6f 2d 6f 70 20 72 65 74 75 72 6e 69 6e 67 20 57 o-op returning W
1fbaf 52 43 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a RC_Continue..*/.
1fbb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1fbb1 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 nt sqlite3WalkSe
1fbb2 6c 65 63 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 lect(Walker *pWa
1fbb3 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 lker, Select *p)
1fbb4 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 {. int rc;. if
1fbb5 28 20 70 3d 3d 30 20 7c 7c 20 70 57 61 6c 6b 65 ( p==0 || pWalke
1fbb6 72 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 r->xSelectCallba
1fbb7 63 6b 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 ck==0 ) return W
1fbb8 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 72 RC_Continue;. r
1fbb9 63 20 3d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 c = WRC_Continue
1fbba 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 20 29 7b ;. while( p ){
1fbbb 0a 20 20 20 20 72 63 20 3d 20 70 57 61 6c 6b 65 . rc = pWalke
1fbbc 72 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 r->xSelectCallba
1fbbd 63 6b 28 70 57 61 6c 6b 65 72 2c 20 70 29 3b 0a ck(pWalker, p);.
1fbbe 20 20 20 20 69 66 28 20 72 63 20 29 20 62 72 65 if( rc ) bre
1fbbf 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 ak;. if( sqli
1fbc0 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70 te3WalkSelectExp
1fbc1 72 28 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 r(pWalker, p) )
1fbc2 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1fbc3 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
1fbc4 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 3WalkSelectFrom(
1fbc5 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 pWalker, p) ) re
1fbc6 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1fbc7 20 20 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f p = p->pPrio
1fbc8 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 r;. }. return
1fbc9 72 63 20 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a rc & WRC_Abort;.
1fbca 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
1fbcb 2a 2a 20 45 6e 64 20 6f 66 20 77 61 6c 6b 65 72 ** End of walker
1fbcc 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1fbcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbcf 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
1fbd0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 65 ** Begin file re
1fbd1 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a solve.c ********
1fbd2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbd3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbd4 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 */./*.** 2008 Au
1fbd5 67 75 73 74 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 gust 18.**.** Th
1fbd6 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
1fbd7 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
1fbd8 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
1fbd9 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
1fbda 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
1fbdb 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
1fbdc 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
1fbdd 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
1fbde 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
1fbdf 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
1fbe0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
1fbe1 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
1fbe2 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
1fbe3 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
1fbe4 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
1fbe5 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
1fbe6 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
1fbe7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbe8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbe9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbeb 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
1fbec 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
1fbed 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66 6f routines used fo
1fbee 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70 61 r walking the pa
1fbef 72 73 65 72 20 74 72 65 65 20 61 6e 64 0a 2a 2a rser tree and.**
1fbf0 20 72 65 73 6f 6c 76 65 20 61 6c 6c 20 69 64 65 resolve all ide
1fbf1 6e 74 69 66 69 65 72 73 20 62 79 20 61 73 73 6f ntifiers by asso
1fbf2 63 69 61 74 69 6e 67 20 74 68 65 6d 20 77 69 74 ciating them wit
1fbf3 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a h a particular.*
1fbf4 2a 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 * table and colu
1fbf5 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 mn..**.** $Id: r
1fbf6 65 73 6f 6c 76 65 2e 63 2c 76 20 31 2e 33 30 20 esolve.c,v 1.30
1fbf7 32 30 30 39 2f 30 36 2f 31 35 20 32 33 3a 31 35 2009/06/15 23:15
1fbf8 3a 35 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :59 drh Exp $.*/
1fbf9 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 68 65 ../*.** Turn the
1fbfa 20 70 45 78 70 72 20 65 78 70 72 65 73 73 69 6f pExpr expressio
1fbfb 6e 20 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73 20 n into an alias
1fbfc 66 6f 72 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 for the iCol-th
1fbfd 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a column of the.**
1fbfe 20 72 65 73 75 6c 74 20 73 65 74 20 69 6e 20 70 result set in p
1fbff 45 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 EList..**.** If
1fc00 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 63 the result set c
1fc01 6f 6c 75 6d 6e 20 69 73 20 61 20 73 69 6d 70 6c olumn is a simpl
1fc02 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e e column referen
1fc03 63 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f ce, then this ro
1fc04 75 74 69 6e 65 0a 2a 2a 20 6d 61 6b 65 73 20 61 utine.** makes a
1fc05 6e 20 65 78 61 63 74 20 63 6f 70 79 2e 20 20 42 n exact copy. B
1fc06 75 74 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 72 ut for any other
1fc07 20 6b 69 6e 64 20 6f 66 20 65 78 70 72 65 73 73 kind of express
1fc08 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 ion, this.** rou
1fc09 74 69 6e 65 20 6d 61 6b 65 20 61 20 63 6f 70 79 tine make a copy
1fc0a 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 of the result s
1fc0b 65 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 et column as the
1fc0c 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 argument to the
1fc0d 0a 2a 2a 20 54 4b 5f 41 53 20 6f 70 65 72 61 74 .** TK_AS operat
1fc0e 6f 72 2e 20 20 54 68 65 20 54 4b 5f 41 53 20 6f or. The TK_AS o
1fc0f 70 65 72 61 74 6f 72 20 63 61 75 73 65 73 20 74 perator causes t
1fc10 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f he expression to
1fc11 20 62 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 be.** evaluated
1fc12 20 6a 75 73 74 20 6f 6e 63 65 20 61 6e 64 20 74 just once and t
1fc13 68 65 6e 20 72 65 75 73 65 64 20 66 6f 72 20 65 hen reused for e
1fc14 61 63 68 20 61 6c 69 61 73 2e 0a 2a 2a 0a 2a 2a ach alias..**.**
1fc15 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 The reason for
1fc16 73 75 70 70 72 65 73 73 69 6e 67 20 74 68 65 20 suppressing the
1fc17 54 4b 5f 41 53 20 74 65 72 6d 20 77 68 65 6e 20 TK_AS term when
1fc18 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
1fc19 73 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 63 6f s a simple.** co
1fc1a 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 69 lumn reference i
1fc1b 73 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f s so that the co
1fc1c 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 77 lumn reference w
1fc1d 69 6c 6c 20 62 65 20 72 65 63 6f 67 6e 69 7a 65 ill be recognize
1fc1e 64 20 61 73 0a 2a 2a 20 75 73 61 62 6c 65 20 62 d as.** usable b
1fc1f 79 20 69 6e 64 69 63 65 73 20 77 69 74 68 69 6e y indices within
1fc20 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 the WHERE claus
1fc21 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f 67 e processing log
1fc22 69 63 2e 20 0a 2a 2a 0a 2a 2a 20 48 61 63 6b 3a ic. .**.** Hack:
1fc23 20 20 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 72 The TK_AS oper
1fc24 61 74 6f 72 20 69 73 20 69 6e 68 69 62 69 74 65 ator is inhibite
1fc25 64 20 69 66 20 7a 54 79 70 65 5b 30 5d 3d 3d 27 d if zType[0]=='
1fc26 47 27 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 0a G'. This means.
1fc27 2a 2a 20 74 68 61 74 20 69 6e 20 61 20 47 52 4f ** that in a GRO
1fc28 55 50 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 UP BY clause, th
1fc29 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
1fc2a 65 76 61 6c 75 61 74 65 64 20 74 77 69 63 65 2e evaluated twice.
1fc2b 20 20 48 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20 20 Hence:.**.**
1fc2c 20 20 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d SELECT random
1fc2d 28 29 25 35 20 41 53 20 78 2c 20 63 6f 75 6e 74 ()%5 AS x, count
1fc2e 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 47 52 4f (*) FROM tab GRO
1fc2f 55 50 20 42 59 20 78 0a 2a 2a 0a 2a 2a 20 49 73 UP BY x.**.** Is
1fc30 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a 0a equivalent to:.
1fc31 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 **.** SELECT
1fc32 20 72 61 6e 64 6f 6d 28 29 25 35 20 41 53 20 78 random()%5 AS x
1fc33 2c 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 , count(*) FROM
1fc34 74 61 62 20 47 52 4f 55 50 20 42 59 20 72 61 6e tab GROUP BY ran
1fc35 64 6f 6d 28 29 25 35 0a 2a 2a 0a 2a 2a 20 54 68 dom()%5.**.** Th
1fc36 65 20 72 65 73 75 6c 74 20 6f 66 20 72 61 6e 64 e result of rand
1fc37 6f 6d 28 29 25 35 20 69 6e 20 74 68 65 20 47 52 om()%5 in the GR
1fc38 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 73 OUP BY clause is
1fc39 20 70 72 6f 62 61 62 6c 79 20 64 69 66 66 65 72 probably differ
1fc3a 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 ent.** from the
1fc3b 72 65 73 75 6c 74 20 69 6e 20 74 68 65 20 72 65 result in the re
1fc3c 73 75 6c 74 2d 73 65 74 2e 20 20 57 65 20 6d 69 sult-set. We mi
1fc3d 67 68 74 20 66 69 78 20 74 68 69 73 20 73 6f 6d ght fix this som
1fc3e 65 64 61 79 2e 20 20 4f 72 0a 2a 2a 20 74 68 65 eday. Or.** the
1fc3f 6e 20 61 67 61 69 6e 2c 20 77 65 20 6d 69 67 68 n again, we migh
1fc40 74 20 6e 6f 74 2e 2e 2e 0a 2a 2f 0a 73 74 61 74 t not....*/.stat
1fc41 69 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 41 ic void resolveA
1fc42 6c 69 61 73 28 0a 20 20 50 61 72 73 65 20 2a 70 lias(. Parse *p
1fc43 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f Parse, /
1fc44 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 * Parsing contex
1fc45 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 t */. ExprList
1fc46 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 2f 2a *pEList, /*
1fc47 20 41 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f A result set */
1fc48 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20 20 . int iCol,
1fc49 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 63 /* A c
1fc4a 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 olumn in the res
1fc4b 75 6c 74 20 73 65 74 2e 20 20 30 2e 2e 70 45 4c ult set. 0..pEL
1fc4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20 2a 2f 0a ist->nExpr-1 */.
1fc4d 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 Expr *pExpr,
1fc4e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 61 6e /* Tran
1fc4f 73 66 6f 72 6d 20 74 68 69 73 20 69 6e 74 6f 20 sform this into
1fc50 61 6e 20 61 6c 69 61 73 20 74 6f 20 74 68 65 20 an alias to the
1fc51 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 result set */.
1fc52 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 const char *zTyp
1fc53 65 20 20 20 20 20 20 2f 2a 20 22 47 52 4f 55 50 e /* "GROUP
1fc54 22 20 6f 72 20 22 4f 52 44 45 52 22 20 6f 72 20 " or "ORDER" or
1fc55 22 22 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 "" */.){. Expr
1fc56 2a 70 4f 72 69 67 3b 20 20 20 20 20 20 20 20 20 *pOrig;
1fc57 20 20 2f 2a 20 54 68 65 20 69 43 6f 6c 2d 74 68 /* The iCol-th
1fc58 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 column of the r
1fc59 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 45 esult set */. E
1fc5a 78 70 72 20 2a 70 44 75 70 3b 20 20 20 20 20 20 xpr *pDup;
1fc5b 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 /* Copy of
1fc5c 20 70 4f 72 69 67 20 2a 2f 0a 20 20 73 71 6c 69 pOrig */. sqli
1fc5d 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
1fc5e 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 /* The databa
1fc5f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
1fc60 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c .. assert( iCol
1fc61 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 45 4c 69 >=0 && iCol<pELi
1fc62 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 70 st->nExpr );. p
1fc63 4f 72 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 Orig = pEList->a
1fc64 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20 20 [iCol].pExpr;.
1fc65 61 73 73 65 72 74 28 20 70 4f 72 69 67 21 3d 30 assert( pOrig!=0
1fc66 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
1fc67 72 69 67 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f rig->flags & EP_
1fc68 52 65 73 6f 6c 76 65 64 20 29 3b 0a 20 20 64 62 Resolved );. db
1fc69 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
1fc6a 20 69 66 28 20 70 4f 72 69 67 2d 3e 6f 70 21 3d if( pOrig->op!=
1fc6b 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 7a 54 79 TK_COLUMN && zTy
1fc6c 70 65 5b 30 5d 21 3d 27 47 27 20 29 7b 0a 20 20 pe[0]!='G' ){.
1fc6d 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 pDup = sqlite3
1fc6e 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 69 ExprDup(db, pOri
1fc6f 67 2c 20 30 29 3b 0a 20 20 20 20 70 44 75 70 20 g, 0);. pDup
1fc70 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 = sqlite3PExpr(p
1fc71 50 61 72 73 65 2c 20 54 4b 5f 41 53 2c 20 70 44 Parse, TK_AS, pD
1fc72 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 up, 0, 0);. i
1fc73 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65 74 f( pDup==0 ) ret
1fc74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 70 45 4c urn;. if( pEL
1fc75 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c ist->a[iCol].iAl
1fc76 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ias==0 ){.
1fc77 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e pEList->a[iCol].
1fc78 69 41 6c 69 61 73 20 3d 20 28 75 31 36 29 28 2b iAlias = (u16)(+
1fc79 2b 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 29 +pParse->nAlias)
1fc7a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 44 75 70 ;. }. pDup
1fc7b 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 4c 69 73 ->iTable = pELis
1fc7c 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61 t->a[iCol].iAlia
1fc7d 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 45 s;. }else if( E
1fc7e 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
1fc7f 4f 72 69 67 2c 20 45 50 5f 49 6e 74 56 61 6c 75 Orig, EP_IntValu
1fc80 65 29 20 7c 7c 20 70 4f 72 69 67 2d 3e 75 2e 7a e) || pOrig->u.z
1fc81 54 6f 6b 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 Token==0 ){.
1fc82 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78 pDup = sqlite3Ex
1fc83 70 72 44 75 70 28 64 62 2c 20 70 4f 72 69 67 2c prDup(db, pOrig,
1fc84 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 44 75 0);. if( pDu
1fc85 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 p==0 ) return;.
1fc86 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 }else{. char
1fc87 20 2a 7a 54 6f 6b 65 6e 20 3d 20 70 4f 72 69 67 *zToken = pOrig
1fc88 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 ->u.zToken;.
1fc89 61 73 73 65 72 74 28 20 7a 54 6f 6b 65 6e 21 3d assert( zToken!=
1fc8a 30 20 29 3b 0a 20 20 20 20 70 4f 72 69 67 2d 3e 0 );. pOrig->
1fc8b 75 2e 7a 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 u.zToken = 0;.
1fc8c 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 pDup = sqlite3
1fc8d 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 69 ExprDup(db, pOri
1fc8e 67 2c 20 30 29 3b 0a 20 20 20 20 70 4f 72 69 67 g, 0);. pOrig
1fc8f 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 7a 54 6f ->u.zToken = zTo
1fc90 6b 65 6e 3b 0a 20 20 20 20 69 66 28 20 70 44 75 ken;. if( pDu
1fc91 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 p==0 ) return;.
1fc92 20 20 20 61 73 73 65 72 74 28 20 28 70 44 75 70 assert( (pDup
1fc93 2d 3e 66 6c 61 67 73 20 26 20 28 45 50 5f 52 65 ->flags & (EP_Re
1fc94 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e duced|EP_TokenOn
1fc95 6c 79 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 ly))==0 );. p
1fc96 44 75 70 2d 3e 66 6c 61 67 73 32 20 7c 3d 20 45 Dup->flags2 |= E
1fc97 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e P2_MallocedToken
1fc98 3b 0a 20 20 20 20 70 44 75 70 2d 3e 75 2e 7a 54 ;. pDup->u.zT
1fc99 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 44 62 oken = sqlite3Db
1fc9a 53 74 72 44 75 70 28 64 62 2c 20 7a 54 6f 6b 65 StrDup(db, zToke
1fc9b 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 45 n);. }. if( pE
1fc9c 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f xpr->flags & EP_
1fc9d 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 ExpCollate ){.
1fc9e 20 20 70 44 75 70 2d 3e 70 43 6f 6c 6c 20 3d 20 pDup->pColl =
1fc9f 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 pExpr->pColl;.
1fca0 20 20 70 44 75 70 2d 3e 66 6c 61 67 73 20 7c 3d pDup->flags |=
1fca1 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a EP_ExpCollate;.
1fca2 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 }. sqlite3Exp
1fca3 72 43 6c 65 61 72 28 64 62 2c 20 70 45 78 70 72 rClear(db, pExpr
1fca4 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 45 78 70 );. memcpy(pExp
1fca5 72 2c 20 70 44 75 70 2c 20 73 69 7a 65 6f 66 28 r, pDup, sizeof(
1fca6 2a 70 45 78 70 72 29 29 3b 0a 20 20 73 71 6c 69 *pExpr));. sqli
1fca7 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 te3DbFree(db, pD
1fca8 75 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 up);.}../*.** Gi
1fca9 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 ven the name of
1fcaa 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 a column of the
1fcab 66 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 2e form X.Y.Z or Y.
1fcac 5a 20 6f 72 20 6a 75 73 74 20 5a 2c 20 6c 6f 6f Z or just Z, loo
1fcad 6b 20 75 70 0a 2a 2a 20 74 68 61 74 20 6e 61 6d k up.** that nam
1fcae 65 20 69 6e 20 74 68 65 20 73 65 74 20 6f 66 20 e in the set of
1fcaf 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20 69 6e source tables in
1fcb0 20 70 53 72 63 4c 69 73 74 20 61 6e 64 20 6d 61 pSrcList and ma
1fcb1 6b 65 20 74 68 65 20 70 45 78 70 72 20 0a 2a 2a ke the pExpr .**
1fcb2 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 expression node
1fcb3 20 72 65 66 65 72 20 62 61 63 6b 20 74 6f 20 74 refer back to t
1fcb4 68 61 74 20 73 6f 75 72 63 65 20 63 6f 6c 75 6d hat source colum
1fcb5 6e 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e n. The followin
1fcb6 67 20 63 68 61 6e 67 65 73 0a 2a 2a 20 61 72 65 g changes.** are
1fcb7 20 6d 61 64 65 20 74 6f 20 70 45 78 70 72 3a 0a made to pExpr:.
1fcb8 2a 2a 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e **.** pExpr->
1fcb9 69 44 62 20 20 20 20 20 20 20 20 20 20 20 53 65 iDb Se
1fcba 74 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 64 t the index in d
1fcbb 62 2d 3e 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 b->aDb[] of the
1fcbc 64 61 74 61 62 61 73 65 20 58 0a 2a 2a 20 20 20 database X.**
1fcbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fcbe 20 20 20 20 20 20 28 65 76 65 6e 20 69 66 20 58 (even if X
1fcbf 20 69 73 20 69 6d 70 6c 69 65 64 29 2e 0a 2a 2a is implied)..**
1fcc0 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c pExpr->iTabl
1fcc1 65 20 20 20 20 20 20 20 20 53 65 74 20 74 6f 20 e Set to
1fcc2 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 the cursor numbe
1fcc3 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 r for the table
1fcc4 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 20 20 20 20 obtained.**
1fcc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fcc6 20 20 20 20 66 72 6f 6d 20 70 53 72 63 4c 69 73 from pSrcLis
1fcc7 74 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e t..** pExpr->
1fcc8 70 54 61 62 20 20 20 20 20 20 20 20 20 20 50 6f pTab Po
1fcc9 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62 6c ints to the Tabl
1fcca 65 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 58 e structure of X
1fccb 2e 59 20 28 65 76 65 6e 20 69 66 0a 2a 2a 20 20 .Y (even if.**
1fccc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fccd 20 20 20 20 20 20 20 58 20 61 6e 64 2f 6f 72 20 X and/or
1fcce 59 20 61 72 65 20 69 6d 70 6c 69 65 64 2e 29 0a Y are implied.).
1fccf 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f ** pExpr->iCo
1fcd0 6c 75 6d 6e 20 20 20 20 20 20 20 53 65 74 20 74 lumn Set t
1fcd1 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d o the column num
1fcd2 62 65 72 20 77 69 74 68 69 6e 20 74 68 65 20 74 ber within the t
1fcd3 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 70 45 78 70 able..** pExp
1fcd4 72 2d 3e 6f 70 20 20 20 20 20 20 20 20 20 20 20 r->op
1fcd5 20 53 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d Set to TK_COLUM
1fcd6 4e 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e N..** pExpr->
1fcd7 70 4c 65 66 74 20 20 20 20 20 20 20 20 20 41 6e pLeft An
1fcd8 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 69 y expression thi
1fcd9 73 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 64 s points to is d
1fcda 65 6c 65 74 65 64 0a 2a 2a 20 20 20 20 70 45 78 eleted.** pEx
1fcdb 70 72 2d 3e 70 52 69 67 68 74 20 20 20 20 20 20 pr->pRight
1fcdc 20 20 41 6e 79 20 65 78 70 72 65 73 73 69 6f 6e Any expression
1fcdd 20 74 68 69 73 20 70 6f 69 6e 74 73 20 74 6f 20 this points to
1fcde 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a is deleted..**.*
1fcdf 2a 20 54 68 65 20 7a 44 62 20 76 61 72 69 61 62 * The zDb variab
1fce0 6c 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f le is the name o
1fce1 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 f the database (
1fce2 74 68 65 20 22 58 22 29 2e 20 20 54 68 69 73 20 the "X"). This
1fce3 76 61 6c 75 65 20 6d 61 79 20 62 65 0a 2a 2a 20 value may be.**
1fce4 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67 20 74 68 61 NULL meaning tha
1fce5 74 20 6e 61 6d 65 20 69 73 20 6f 66 20 74 68 65 t name is of the
1fce6 20 66 6f 72 6d 20 59 2e 5a 20 6f 72 20 5a 2e 20 form Y.Z or Z.
1fce7 20 41 6e 79 20 61 76 61 69 6c 61 62 6c 65 20 64 Any available d
1fce8 61 74 61 62 61 73 65 0a 2a 2a 20 63 61 6e 20 62 atabase.** can b
1fce9 65 20 75 73 65 64 2e 20 20 54 68 65 20 7a 54 61 e used. The zTa
1fcea 62 6c 65 20 76 61 72 69 61 62 6c 65 20 69 73 20 ble variable is
1fceb 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
1fcec 74 61 62 6c 65 20 28 74 68 65 20 22 59 22 29 2e table (the "Y").
1fced 20 20 54 68 69 73 0a 2a 2a 20 76 61 6c 75 65 20 This.** value
1fcee 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66 20 7a can be NULL if z
1fcef 44 62 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e Db is also NULL.
1fcf0 20 20 49 66 20 7a 54 61 62 6c 65 20 69 73 20 4e If zTable is N
1fcf1 55 4c 4c 20 69 74 0a 2a 2a 20 6d 65 61 6e 73 20 ULL it.** means
1fcf2 74 68 61 74 20 74 68 65 20 66 6f 72 6d 20 6f 66 that the form of
1fcf3 20 74 68 65 20 6e 61 6d 65 20 69 73 20 5a 20 61 the name is Z a
1fcf4 6e 64 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73 20 nd that columns
1fcf5 66 72 6f 6d 20 61 6e 79 20 74 61 62 6c 65 0a 2a from any table.*
1fcf6 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a 2a * can be used..*
1fcf7 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 61 6d 65 *.** If the name
1fcf8 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 73 6f 6c cannot be resol
1fcf9 76 65 64 20 75 6e 61 6d 62 69 67 75 6f 75 73 6c ved unambiguousl
1fcfa 79 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f y, leave an erro
1fcfb 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 6e 20 r message.** in
1fcfc 70 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 pParse and retur
1fcfd 6e 20 57 52 43 5f 41 62 6f 72 74 2e 20 20 52 65 n WRC_Abort. Re
1fcfe 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 20 6f turn WRC_Prune o
1fcff 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74 n success..*/.st
1fd00 61 74 69 63 20 69 6e 74 20 6c 6f 6f 6b 75 70 4e atic int lookupN
1fd01 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 ame(. Parse *pP
1fd02 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 arse, /* T
1fd03 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 he parsing conte
1fd04 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 xt */. const ch
1fd05 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 2f 2a 20 ar *zDb, /*
1fd06 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 Name of the data
1fd07 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 base containing
1fd08 74 61 62 6c 65 2c 20 6f 72 20 4e 55 4c 4c 20 2a table, or NULL *
1fd09 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1fd0a 7a 54 61 62 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 zTab, /* Name
1fd0b 20 6f 66 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 of table contai
1fd0c 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c 20 6f 72 20 ning column, or
1fd0d 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 NULL */. const
1fd0e 63 68 61 72 20 2a 7a 43 6f 6c 2c 20 20 20 20 2f char *zCol, /
1fd0f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f * Name of the co
1fd10 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43 lumn. */. NameC
1fd11 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 ontext *pNC,
1fd12 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e 74 /* The name cont
1fd13 65 78 74 20 75 73 65 64 20 74 6f 20 72 65 73 6f ext used to reso
1fd14 6c 76 65 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a lve the name */.
1fd15 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 Expr *pExpr
1fd16 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 74 /* Make t
1fd17 68 69 73 20 45 58 50 52 20 6e 6f 64 65 20 70 6f his EXPR node po
1fd18 69 6e 74 20 74 6f 20 74 68 65 20 73 65 6c 65 63 int to the selec
1fd19 74 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 29 7b ted column */.){
1fd1a 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 . int i, j;
1fd1b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
1fd1c 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e counters */. in
1fd1d 74 20 63 6e 74 20 3d 20 30 3b 20 20 20 20 20 20 t cnt = 0;
1fd1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fd1f 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 74 /* Number of mat
1fd20 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 6e 61 6d ching column nam
1fd21 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 54 es */. int cntT
1fd22 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ab = 0;
1fd23 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
1fd24 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 ber of matching
1fd25 74 61 62 6c 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 table names */.
1fd26 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
1fd27 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 Parse->db;
1fd28 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 /* The databa
1fd29 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
1fd2a 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 . struct SrcLis
1fd2b 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 t_item *pItem;
1fd2c 20 20 20 20 20 2f 2a 20 55 73 65 20 66 6f 72 20 /* Use for
1fd2d 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 70 53 72 looping over pSr
1fd2e 63 4c 69 73 74 20 69 74 65 6d 73 20 2a 2f 0a 20 cList items */.
1fd2f 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f struct SrcList_
1fd30 69 74 65 6d 20 2a 70 4d 61 74 63 68 20 3d 20 30 item *pMatch = 0
1fd31 3b 20 20 2f 2a 20 54 68 65 20 6d 61 74 63 68 69 ; /* The matchi
1fd32 6e 67 20 70 53 72 63 4c 69 73 74 20 69 74 65 6d ng pSrcList item
1fd33 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 */. NameContex
1fd34 74 20 2a 70 54 6f 70 4e 43 20 3d 20 70 4e 43 3b t *pTopNC = pNC;
1fd35 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
1fd36 20 6e 61 6d 65 63 6f 6e 74 65 78 74 20 69 6e 20 namecontext in
1fd37 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 53 63 the list */. Sc
1fd38 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 hema *pSchema =
1fd39 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1fd3a 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 74 68 65 /* Schema of the
1fd3b 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 expression */.
1fd3c 20 69 6e 74 20 69 73 54 72 69 67 67 65 72 20 3d int isTrigger =
1fd3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 0;.. assert( p
1fd3e 4e 43 20 29 3b 20 20 20 20 20 2f 2a 20 74 68 65 NC ); /* the
1fd3f 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 63 61 name context ca
1fd40 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 20 2a 2f nnot be NULL. */
1fd41 0a 20 20 61 73 73 65 72 74 28 20 7a 43 6f 6c 20 . assert( zCol
1fd42 29 3b 20 20 20 20 2f 2a 20 54 68 65 20 5a 20 69 ); /* The Z i
1fd43 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e 6f 74 20 62 n X.Y.Z cannot b
1fd44 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 61 73 73 65 e NULL */. asse
1fd45 72 74 28 20 7e 45 78 70 72 48 61 73 41 6e 79 50 rt( ~ExprHasAnyP
1fd46 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
1fd47 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 P_TokenOnly|EP_R
1fd48 65 64 75 63 65 64 29 20 29 3b 0a 0a 20 20 2f 2a educed) );.. /*
1fd49 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 Initialize the
1fd4a 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d 61 74 63 68 node to no-match
1fd4b 20 2a 2f 0a 20 20 70 45 78 70 72 2d 3e 69 54 61 */. pExpr->iTa
1fd4c 62 6c 65 20 3d 20 2d 31 3b 0a 20 20 70 45 78 70 ble = -1;. pExp
1fd4d 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20 45 r->pTab = 0;. E
1fd4e 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62 6c xprSetIrreducibl
1fd4f 65 28 70 45 78 70 72 29 3b 0a 0a 20 20 2f 2a 20 e(pExpr);.. /*
1fd50 53 74 61 72 74 20 61 74 20 74 68 65 20 69 6e 6e Start at the inn
1fd51 65 72 2d 6d 6f 73 74 20 63 6f 6e 74 65 78 74 20 er-most context
1fd52 61 6e 64 20 6d 6f 76 65 20 6f 75 74 77 61 72 64 and move outward
1fd53 20 75 6e 74 69 6c 20 61 20 6d 61 74 63 68 20 69 until a match i
1fd54 73 20 66 6f 75 6e 64 20 2a 2f 0a 20 20 77 68 69 s found */. whi
1fd55 6c 65 28 20 70 4e 43 20 26 26 20 63 6e 74 3d 3d le( pNC && cnt==
1fd56 30 20 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 73 0 ){. ExprLis
1fd57 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 20 20 53 t *pEList;. S
1fd58 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 rcList *pSrcList
1fd59 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 = pNC->pSrcList
1fd5a 3b 0a 0a 20 20 20 20 69 66 28 20 70 53 72 63 4c ;.. if( pSrcL
1fd5b 69 73 74 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 ist ){. for
1fd5c 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 53 72 63 (i=0, pItem=pSrc
1fd5d 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 53 72 63 4c List->a; i<pSrcL
1fd5e 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 ist->nSrc; i++,
1fd5f 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 pItem++){.
1fd60 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 Table *pTab;.
1fd61 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a int iDb;.
1fd62 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a Column *
1fd63 70 43 6f 6c 3b 0a 20 20 0a 20 20 20 20 20 20 20 pCol;. .
1fd64 20 70 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e 70 pTab = pItem->p
1fd65 54 61 62 3b 0a 20 20 20 20 20 20 20 20 61 73 73 Tab;. ass
1fd66 65 72 74 28 20 70 54 61 62 21 3d 30 20 26 26 20 ert( pTab!=0 &&
1fd67 70 54 61 62 2d 3e 7a 4e 61 6d 65 21 3d 30 20 29 pTab->zName!=0 )
1fd68 3b 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d 20 ;. iDb =
1fd69 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 sqlite3SchemaToI
1fd6a 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 ndex(db, pTab->p
1fd6b 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 20 Schema);.
1fd6c 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 6e assert( pTab->n
1fd6d 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 Col>0 );.
1fd6e 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20 20 if( zTab ){.
1fd6f 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d if( pItem
1fd70 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 ->zAlias ){.
1fd71 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 char *zT
1fd72 61 62 4e 61 6d 65 20 3d 20 70 49 74 65 6d 2d 3e abName = pItem->
1fd73 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20 20 20 20 zAlias;.
1fd74 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1fd75 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c trICmp(zTabName,
1fd76 20 7a 54 61 62 29 21 3d 30 20 29 20 63 6f 6e 74 zTab)!=0 ) cont
1fd77 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 inue;.
1fd78 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
1fd79 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d char *zTabNam
1fd7a 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b e = pTab->zName;
1fd7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
1fd7c 20 4e 45 56 45 52 28 7a 54 61 62 4e 61 6d 65 3d NEVER(zTabName=
1fd7d 3d 30 29 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 =0) || sqlite3St
1fd7e 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c 20 rICmp(zTabName,
1fd7f 7a 54 61 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 zTab)!=0 ){.
1fd80 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e contin
1fd81 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ue;.
1fd82 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 }. if
1fd83 28 20 7a 44 62 21 3d 30 20 26 26 20 73 71 6c 69 ( zDb!=0 && sqli
1fd84 74 65 33 53 74 72 49 43 6d 70 28 64 62 2d 3e 61 te3StrICmp(db->a
1fd85 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a Db[iDb].zName, z
1fd86 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 Db)!=0 ){.
1fd87 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 continue
1fd88 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
1fd89 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1fd8a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
1fd8b 28 20 30 3d 3d 28 63 6e 74 54 61 62 2b 2b 29 20 ( 0==(cntTab++)
1fd8c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 ){. pEx
1fd8d 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 74 pr->iTable = pIt
1fd8e 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 em->iCursor;.
1fd8f 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 54 pExpr->pT
1fd90 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 20 ab = pTab;.
1fd91 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d 20 70 pSchema = p
1fd92 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 Tab->pSchema;.
1fd93 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 20 3d pMatch =
1fd94 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 pItem;.
1fd95 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d }. for(j=
1fd96 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d 3e 61 43 0, pCol=pTab->aC
1fd97 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c ol; j<pTab->nCol
1fd98 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a ; j++, pCol++){.
1fd99 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 if( sq
1fd9a 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f lite3StrICmp(pCo
1fd9b 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d l->zName, zCol)=
1fd9c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
1fd9d 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69 6e 67 IdList *pUsing
1fd9e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 6e ;. cn
1fd9f 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 t++;.
1fda0 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d pExpr->iTable =
1fda1 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b pItem->iCursor;
1fda2 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 . pEx
1fda3 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 3b pr->pTab = pTab;
1fda4 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4d 61 . pMa
1fda5 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20 tch = pItem;.
1fda6 20 20 20 20 20 20 20 20 20 70 53 63 68 65 6d 61 pSchema
1fda7 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 = pTab->pSchema
1fda8 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ;. /*
1fda9 20 53 75 62 73 74 69 74 75 74 65 20 74 68 65 20 Substitute the
1fdaa 72 6f 77 69 64 20 28 63 6f 6c 75 6d 6e 20 2d 31 rowid (column -1
1fdab 29 20 66 6f 72 20 74 68 65 20 49 4e 54 45 47 45 ) for the INTEGE
1fdac 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f R PRIMARY KEY */
1fdad 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 . pEx
1fdae 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 6a 3d pr->iColumn = j=
1fdaf 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f 20 2d =pTab->iPKey ? -
1fdb0 31 20 3a 20 28 69 31 36 29 6a 3b 0a 20 20 20 20 1 : (i16)j;.
1fdb1 20 20 20 20 20 20 20 20 69 66 28 20 69 3c 70 53 if( i<pS
1fdb2 72 63 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 20 29 rcList->nSrc-1 )
1fdb3 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
1fdb4 69 66 28 20 70 49 74 65 6d 5b 31 5d 2e 6a 6f 69 if( pItem[1].joi
1fdb5 6e 74 79 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 ntype & JT_NATUR
1fdb6 41 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 AL ){.
1fdb7 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 /* If this
1fdb8 20 6d 61 74 63 68 20 6f 63 63 75 72 72 65 64 20 match occurred
1fdb9 69 6e 20 74 68 65 20 6c 65 66 74 20 74 61 62 6c in the left tabl
1fdba 65 20 6f 66 20 61 20 6e 61 74 75 72 61 6c 20 6a e of a natural j
1fdbb 6f 69 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 oin,.
1fdbc 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 73 6b 69 ** then ski
1fdbd 70 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c p the right tabl
1fdbe 65 20 74 6f 20 61 76 6f 69 64 20 61 20 64 75 70 e to avoid a dup
1fdbf 6c 69 63 61 74 65 20 6d 61 74 63 68 20 2a 2f 0a licate match */.
1fdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fdc1 70 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 pItem++;.
1fdc2 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 i++;.
1fdc3 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 }els
1fdc4 65 20 69 66 28 20 28 70 55 73 69 6e 67 20 3d 20 e if( (pUsing =
1fdc5 70 49 74 65 6d 5b 31 5d 2e 70 55 73 69 6e 67 29 pItem[1].pUsing)
1fdc6 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 !=0 ){.
1fdc7 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 /* If thi
1fdc8 73 20 6d 61 74 63 68 20 6f 63 63 75 72 73 20 6f s match occurs o
1fdc9 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 n a column that
1fdca 69 73 20 69 6e 20 74 68 65 20 55 53 49 4e 47 20 is in the USING
1fdcb 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20 20 clause.
1fdcc 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 61 20 6a ** of a j
1fdcd 6f 69 6e 2c 20 73 6b 69 70 20 74 68 65 20 73 65 oin, skip the se
1fdce 61 72 63 68 20 6f 66 20 74 68 65 20 72 69 67 68 arch of the righ
1fdcf 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 6a t table of the j
1fdd0 6f 69 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 oin.
1fdd1 20 20 20 20 2a 2a 20 74 6f 20 61 76 6f 69 64 20 ** to avoid
1fdd2 61 20 64 75 70 6c 69 63 61 74 65 20 6d 61 74 63 a duplicate matc
1fdd3 68 20 74 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20 h there. */.
1fdd4 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 int
1fdd5 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 k;.
1fdd6 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 55 for(k=0; k<pU
1fdd7 73 69 6e 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29 7b sing->nId; k++){
1fdd8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1fdd9 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 if( sqlite3St
1fdda 72 49 43 6d 70 28 70 55 73 69 6e 67 2d 3e 61 5b rICmp(pUsing->a[
1fddb 6b 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d k].zName, zCol)=
1fddc 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
1fddd 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2b pItem+
1fdde 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 +;.
1fddf 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 i++;.
1fde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fde1 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 break;.
1fde2 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1fde3 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
1fde4 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
1fde5 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1fde6 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1fde7 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1fde8 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1fde9 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
1fdea 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 E_OMIT_TRIGGER.
1fdeb 20 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 /* If we have
1fdec 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 72 65 73 not already res
1fded 6f 6c 76 65 64 20 74 68 65 20 6e 61 6d 65 2c 20 olved the name,
1fdee 74 68 65 6e 20 6d 61 79 62 65 20 0a 20 20 20 20 then maybe .
1fdef 2a 2a 20 69 74 20 69 73 20 61 20 6e 65 77 2e 2a ** it is a new.*
1fdf0 20 6f 72 20 6f 6c 64 2e 2a 20 74 72 69 67 67 65 or old.* trigge
1fdf1 72 20 61 72 67 75 6d 65 6e 74 20 72 65 66 65 72 r argument refer
1fdf2 65 6e 63 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ence. */.
1fdf3 69 66 28 20 7a 44 62 3d 3d 30 20 26 26 20 7a 54 if( zDb==0 && zT
1fdf4 61 62 21 3d 30 20 26 26 20 63 6e 74 3d 3d 30 20 ab!=0 && cnt==0
1fdf5 26 26 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 && pParse->pTrig
1fdf6 67 65 72 54 61 62 21 3d 30 20 29 7b 0a 20 20 20 gerTab!=0 ){.
1fdf7 20 20 20 69 6e 74 20 6f 70 20 3d 20 70 50 61 72 int op = pPar
1fdf8 73 65 2d 3e 65 54 72 69 67 67 65 72 4f 70 3b 0a se->eTriggerOp;.
1fdf9 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 Table *pTa
1fdfa 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73 b = 0;. ass
1fdfb 65 72 74 28 20 6f 70 3d 3d 54 4b 5f 44 45 4c 45 ert( op==TK_DELE
1fdfc 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 50 44 TE || op==TK_UPD
1fdfd 41 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 4e ATE || op==TK_IN
1fdfe 53 45 52 54 20 29 3b 0a 20 20 20 20 20 20 69 66 SERT );. if
1fdff 28 20 6f 70 21 3d 54 4b 5f 44 45 4c 45 54 45 20 ( op!=TK_DELETE
1fe00 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d && sqlite3StrICm
1fe01 70 28 22 6e 65 77 22 2c 7a 54 61 62 29 20 3d 3d p("new",zTab) ==
1fe02 20 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45 0 ){. pE
1fe03 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 31 3b xpr->iTable = 1;
1fe04 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 . pTab =
1fe05 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 pParse->pTrigger
1fe06 54 61 62 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 Tab;. }else
1fe07 20 69 66 28 20 6f 70 21 3d 54 4b 5f 49 4e 53 45 if( op!=TK_INSE
1fe08 52 54 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 RT && sqlite3Str
1fe09 49 43 6d 70 28 22 6f 6c 64 22 2c 7a 54 61 62 29 ICmp("old",zTab)
1fe0a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ==0 ){. p
1fe0b 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 30 Expr->iTable = 0
1fe0c 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d ;. pTab =
1fe0d 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 pParse->pTrigge
1fe0e 72 54 61 62 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 rTab;. }..
1fe0f 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b if( pTab ){
1fe10 20 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 . int iC
1fe11 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 53 63 68 ol;. pSch
1fe12 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 ema = pTab->pSch
1fe13 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 63 6e 74 ema;. cnt
1fe14 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69 Tab++;. i
1fe15 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 f( sqlite3IsRowi
1fe16 64 28 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 d(zCol) ){.
1fe17 20 20 20 20 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a iCol = -1;.
1fe18 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
1fe19 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 43 6f for(iCo
1fe1a 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e l=0; iCol<pTab->
1fe1b 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 nCol; iCol++){.
1fe1c 20 20 20 20 20 20 20 20 20 20 20 43 6f 6c 75 6d Colum
1fe1d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 54 61 62 2d n *pCol = &pTab-
1fe1e 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20 >aCol[iCol];.
1fe1f 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c if( sql
1fe20 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c ite3StrICmp(pCol
1fe21 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d ->zName, zCol)==
1fe22 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 0 ){.
1fe23 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54 61 if( iCol==pTa
1fe24 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 b->iPKey ){.
1fe25 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f 6c iCol
1fe26 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 = -1;.
1fe27 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
1fe28 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1fe29 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1fe2a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
1fe2b 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c if( iCol
1fe2c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 <pTab->nCol ){.
1fe2d 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a cnt++;.
1fe2e 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 43 if( iC
1fe2f 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 ol<0 ){.
1fe30 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69 6e pExpr->affin
1fe31 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 ity = SQLITE_AFF
1fe32 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 _INTEGER;.
1fe33 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 45 }else if( pE
1fe34 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 30 20 29 xpr->iTable==0 )
1fe35 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 65 {. te
1fe36 73 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d 33 31 stcase( iCol==31
1fe37 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 );.
1fe38 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d testcase( iCol==
1fe39 33 32 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 32 );.
1fe3a 20 20 70 50 61 72 73 65 2d 3e 6f 6c 64 6d 61 73 pParse->oldmas
1fe3b 6b 20 7c 3d 20 28 69 43 6f 6c 3e 3d 33 32 20 3f k |= (iCol>=32 ?
1fe3c 20 30 78 66 66 66 66 66 66 66 66 20 3a 20 28 28 0xffffffff : ((
1fe3d 28 75 33 32 29 31 29 3c 3c 69 43 6f 6c 29 29 3b (u32)1)<<iCol));
1fe3e 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1fe3f 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 pExpr->iC
1fe40 6f 6c 75 6d 6e 20 3d 20 28 69 31 36 29 69 43 6f olumn = (i16)iCo
1fe41 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 l;. pEx
1fe42 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 3b pr->pTab = pTab;
1fe43 0a 20 20 20 20 20 20 20 20 20 20 69 73 54 72 69 . isTri
1fe44 67 67 65 72 20 3d 20 31 3b 0a 20 20 20 20 20 20 gger = 1;.
1fe45 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1fe46 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 }.#endif /* !def
1fe47 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1fe48 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a 20 20 _TRIGGER) */..
1fe49 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 50 65 72 68 /*. ** Perh
1fe4a 61 70 73 20 74 68 65 20 6e 61 6d 65 20 69 73 20 aps the name is
1fe4b 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 a reference to t
1fe4c 68 65 20 52 4f 57 49 44 0a 20 20 20 20 2a 2f 0a he ROWID. */.
1fe4d 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 26 if( cnt==0 &
1fe4e 26 20 63 6e 74 54 61 62 3d 3d 31 20 26 26 20 73 & cntTab==1 && s
1fe4f 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 7a 43 qlite3IsRowid(zC
1fe50 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 ol) ){. cnt
1fe51 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 45 78 70 = 1;. pExp
1fe52 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b r->iColumn = -1;
1fe53 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 . pExpr->af
1fe54 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f finity = SQLITE_
1fe55 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 AFF_INTEGER;.
1fe56 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a }.. /*. *
1fe57 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 69 * If the input i
1fe58 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5a 20 s of the form Z
1fe59 28 6e 6f 74 20 59 2e 5a 20 6f 72 20 58 2e 59 2e (not Y.Z or X.Y.
1fe5a 5a 29 20 74 68 65 6e 20 74 68 65 20 6e 61 6d 65 Z) then the name
1fe5b 20 5a 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20 Z. ** might
1fe5c 72 65 66 65 72 20 74 6f 20 61 6e 20 72 65 73 75 refer to an resu
1fe5d 6c 74 2d 73 65 74 20 61 6c 69 61 73 2e 20 20 54 lt-set alias. T
1fe5e 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66 6f 72 his happens, for
1fe5f 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0a 20 example, when.
1fe60 20 20 20 2a 2a 20 77 65 20 61 72 65 20 72 65 73 ** we are res
1fe61 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20 olving names in
1fe62 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 the WHERE clause
1fe63 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
1fe64 67 20 63 6f 6d 6d 61 6e 64 3a 0a 20 20 20 20 2a g command:. *
1fe65 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 53 45 4c *. ** SEL
1fe66 45 43 54 20 61 2b 62 20 41 53 20 78 20 46 52 4f ECT a+b AS x FRO
1fe67 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 78 3c M table WHERE x<
1fe68 31 30 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 10;. **. *
1fe69 2a 20 49 6e 20 63 61 73 65 73 20 6c 69 6b 65 20 * In cases like
1fe6a 74 68 69 73 2c 20 72 65 70 6c 61 63 65 20 70 45 this, replace pE
1fe6b 78 70 72 20 77 69 74 68 20 61 20 63 6f 70 79 20 xpr with a copy
1fe6c 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f of the expressio
1fe6d 6e 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 66 6f n that. ** fo
1fe6e 72 6d 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 rms the result s
1fe6f 65 74 20 65 6e 74 72 79 20 28 22 61 2b 62 22 20 et entry ("a+b"
1fe70 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 29 20 in the example)
1fe71 61 6e 64 20 72 65 74 75 72 6e 20 69 6d 6d 65 64 and return immed
1fe72 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 4e iately.. ** N
1fe73 6f 74 65 20 74 68 61 74 20 74 68 65 20 65 78 70 ote that the exp
1fe74 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 72 ression in the r
1fe75 65 73 75 6c 74 20 73 65 74 20 73 68 6f 75 6c 64 esult set should
1fe76 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 have already be
1fe77 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c 76 en. ** resolv
1fe78 65 64 20 62 79 20 74 68 65 20 74 69 6d 65 20 74 ed by the time t
1fe79 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
1fe7a 69 73 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20 20 is resolved..
1fe7b 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d */. if( cnt=
1fe7c 3d 30 20 26 26 20 28 70 45 4c 69 73 74 20 3d 20 =0 && (pEList =
1fe7d 70 4e 43 2d 3e 70 45 4c 69 73 74 29 21 3d 30 20 pNC->pEList)!=0
1fe7e 26 26 20 7a 54 61 62 3d 3d 30 20 29 7b 0a 20 20 && zTab==0 ){.
1fe7f 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 for(j=0; j<p
1fe80 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6a 2b EList->nExpr; j+
1fe81 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 +){. char
1fe82 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 2d 3e *zAs = pEList->
1fe83 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 a[j].zName;.
1fe84 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30 20 26 if( zAs!=0 &
1fe85 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 & sqlite3StrICmp
1fe86 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 (zAs, zCol)==0 )
1fe87 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 {. Expr
1fe88 20 2a 70 4f 72 69 67 3b 0a 20 20 20 20 20 20 20 *pOrig;.
1fe89 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
1fe8a 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 70 45 ->pLeft==0 && pE
1fe8b 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 xpr->pRight==0 )
1fe8c 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 ;. asse
1fe8d 72 74 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 rt( pExpr->x.pLi
1fe8e 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 st==0 );.
1fe8f 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
1fe90 2d 3e 78 2e 70 53 65 6c 65 63 74 3d 3d 30 20 29 ->x.pSelect==0 )
1fe91 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72 69 ;. pOri
1fe92 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d g = pEList->a[j]
1fe93 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 .pExpr;.
1fe94 20 20 69 66 28 20 21 70 4e 43 2d 3e 61 6c 6c 6f if( !pNC->allo
1fe95 77 41 67 67 20 26 26 20 45 78 70 72 48 61 73 50 wAgg && ExprHasP
1fe96 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c 20 45 roperty(pOrig, E
1fe97 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20 P_Agg) ){.
1fe98 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1fe99 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d orMsg(pParse, "m
1fe9a 69 73 75 73 65 20 6f 66 20 61 6c 69 61 73 65 64 isuse of aliased
1fe9b 20 61 67 67 72 65 67 61 74 65 20 25 73 22 2c 20 aggregate %s",
1fe9c 7a 41 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 zAs);.
1fe9d 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f return WRC_Abo
1fe9e 72 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a rt;. }.
1fe9f 20 20 20 20 20 20 20 20 20 20 72 65 73 6f 6c 76 resolv
1fea0 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c 20 70 eAlias(pParse, p
1fea1 45 4c 69 73 74 2c 20 6a 2c 20 70 45 78 70 72 2c EList, j, pExpr,
1fea2 20 22 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 "");.
1fea3 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 cnt = 1;.
1fea4 20 20 20 70 4d 61 74 63 68 20 3d 20 30 3b 0a 20 pMatch = 0;.
1fea5 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
1fea6 20 7a 54 61 62 3d 3d 30 20 26 26 20 7a 44 62 3d zTab==0 && zDb=
1fea7 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 =0 );.
1fea8 67 6f 74 6f 20 6c 6f 6f 6b 75 70 6e 61 6d 65 5f goto lookupname_
1fea9 65 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 end;. }.
1feaa 20 20 20 20 20 7d 20 0a 20 20 20 20 7d 0a 0a 20 } . }..
1feab 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f /* Advance to
1feac 20 74 68 65 20 6e 65 78 74 20 6e 61 6d 65 20 63 the next name c
1fead 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 6c 6f 6f ontext. The loo
1feae 70 20 77 69 6c 6c 20 65 78 69 74 20 77 68 65 6e p will exit when
1feaf 20 65 69 74 68 65 72 0a 20 20 20 20 2a 2a 20 77 either. ** w
1feb0 65 20 68 61 76 65 20 61 20 6d 61 74 63 68 20 28 e have a match (
1feb1 63 6e 74 3e 30 29 20 6f 72 20 77 68 65 6e 20 77 cnt>0) or when w
1feb2 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6e 61 6d e run out of nam
1feb3 65 20 63 6f 6e 74 65 78 74 73 2e 0a 20 20 20 20 e contexts..
1feb4 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d */. if( cnt==
1feb5 30 20 29 7b 0a 20 20 20 20 20 20 70 4e 43 20 3d 0 ){. pNC =
1feb6 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 pNC->pNext;.
1feb7 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a }. }.. /*. *
1feb8 2a 20 49 66 20 58 20 61 6e 64 20 59 20 61 72 65 * If X and Y are
1feb9 20 4e 55 4c 4c 20 28 69 6e 20 6f 74 68 65 72 20 NULL (in other
1feba 77 6f 72 64 73 20 69 66 20 6f 6e 6c 79 20 74 68 words if only th
1febb 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 5a 20 e column name Z
1febc 69 73 0a 20 20 2a 2a 20 73 75 70 70 6c 69 65 64 is. ** supplied
1febd 29 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 ) and the value
1febe 6f 66 20 5a 20 69 73 20 65 6e 63 6c 6f 73 65 64 of Z is enclosed
1febf 20 69 6e 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 in double-quote
1fec0 73 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 5a 20 69 s, then. ** Z i
1fec1 73 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 s a string liter
1fec2 61 6c 20 69 66 20 69 74 20 64 6f 65 73 6e 27 74 al if it doesn't
1fec3 20 6d 61 74 63 68 20 61 6e 79 20 63 6f 6c 75 6d match any colum
1fec4 6e 20 6e 61 6d 65 73 2e 20 20 49 6e 20 74 68 61 n names. In tha
1fec5 74 0a 20 20 2a 2a 20 63 61 73 65 2c 20 77 65 20 t. ** case, we
1fec6 6e 65 65 64 20 74 6f 20 72 65 74 75 72 6e 20 72 need to return r
1fec7 69 67 68 74 20 61 77 61 79 20 61 6e 64 20 6e 6f ight away and no
1fec8 74 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e 67 t make any chang
1fec9 65 73 20 74 6f 0a 20 20 2a 2a 20 70 45 78 70 72 es to. ** pExpr
1feca 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 65 63 61 .. **. ** Beca
1fecb 75 73 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 use no reference
1fecc 20 77 61 73 20 6d 61 64 65 20 74 6f 20 6f 75 74 was made to out
1fecd 65 72 20 63 6f 6e 74 65 78 74 73 2c 20 74 68 65 er contexts, the
1fece 20 70 4e 43 2d 3e 6e 52 65 66 0a 20 20 2a 2a 20 pNC->nRef. **
1fecf 66 69 65 6c 64 73 20 61 72 65 20 6e 6f 74 20 63 fields are not c
1fed0 68 61 6e 67 65 64 20 69 6e 20 61 6e 79 20 63 6f hanged in any co
1fed1 6e 74 65 78 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 ntext.. */. if
1fed2 28 20 63 6e 74 3d 3d 30 20 26 26 20 7a 54 61 62 ( cnt==0 && zTab
1fed3 3d 3d 30 20 26 26 20 45 78 70 72 48 61 73 50 72 ==0 && ExprHasPr
1fed4 6f 70 65 72 74 79 28 70 45 78 70 72 2c 45 50 5f operty(pExpr,EP_
1fed5 44 62 6c 51 75 6f 74 65 64 29 20 29 7b 0a 20 20 DblQuoted) ){.
1fed6 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b pExpr->op = TK
1fed7 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 70 45 78 _STRING;. pEx
1fed8 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20 pr->pTab = 0;.
1fed9 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 return WRC_Pru
1feda 6e 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 ne;. }.. /*.
1fedb 2a 2a 20 63 6e 74 3d 3d 30 20 6d 65 61 6e 73 20 ** cnt==0 means
1fedc 74 68 65 72 65 20 77 61 73 20 6e 6f 74 20 6d 61 there was not ma
1fedd 74 63 68 2e 20 20 63 6e 74 3e 31 20 6d 65 61 6e tch. cnt>1 mean
1fede 73 20 74 68 65 72 65 20 77 65 72 65 20 74 77 6f s there were two
1fedf 20 6f 72 0a 20 20 2a 2a 20 6d 6f 72 65 20 6d 61 or. ** more ma
1fee0 74 63 68 65 73 2e 20 20 45 69 74 68 65 72 20 77 tches. Either w
1fee1 61 79 2c 20 77 65 20 68 61 76 65 20 61 6e 20 65 ay, we have an e
1fee2 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 rror.. */. if(
1fee3 20 63 6e 74 21 3d 31 20 29 7b 0a 20 20 20 20 63 cnt!=1 ){. c
1fee4 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72 3b onst char *zErr;
1fee5 0a 20 20 20 20 7a 45 72 72 20 3d 20 63 6e 74 3d . zErr = cnt=
1fee6 3d 30 20 3f 20 22 6e 6f 20 73 75 63 68 20 63 6f =0 ? "no such co
1fee7 6c 75 6d 6e 22 20 3a 20 22 61 6d 62 69 67 75 6f lumn" : "ambiguo
1fee8 75 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 22 3b us column name";
1fee9 0a 20 20 20 20 69 66 28 20 7a 44 62 20 29 7b 0a . if( zDb ){.
1feea 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1feeb 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 orMsg(pParse, "%
1feec 73 3a 20 25 73 2e 25 73 2e 25 73 22 2c 20 7a 45 s: %s.%s.%s", zE
1feed 72 72 2c 20 7a 44 62 2c 20 7a 54 61 62 2c 20 7a rr, zDb, zTab, z
1feee 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 Col);. }else
1feef 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20 20 20 if( zTab ){.
1fef0 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1fef1 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 g(pParse, "%s: %
1fef2 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a 54 61 s.%s", zErr, zTa
1fef3 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 b, zCol);. }e
1fef4 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 lse{. sqlit
1fef5 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
1fef6 65 2c 20 22 25 73 3a 20 25 73 22 2c 20 7a 45 72 e, "%s: %s", zEr
1fef7 72 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a r, zCol);. }.
1fef8 20 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 45 72 72 pTopNC->nErr
1fef9 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 ++;. }.. /* If
1fefa 20 61 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61 a column from a
1fefb 20 74 61 62 6c 65 20 69 6e 20 70 53 72 63 4c 69 table in pSrcLi
1fefc 73 74 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 st is referenced
1fefd 2c 20 74 68 65 6e 20 72 65 63 6f 72 64 0a 20 20 , then record.
1fefe 2a 2a 20 74 68 69 73 20 66 61 63 74 20 69 6e 20 ** this fact in
1feff 74 68 65 20 70 53 72 63 4c 69 73 74 2e 61 5b 5d the pSrcList.a[]
1ff00 2e 63 6f 6c 55 73 65 64 20 62 69 74 6d 61 73 6b .colUsed bitmask
1ff01 2e 20 20 43 6f 6c 75 6d 6e 20 30 20 63 61 75 73 . Column 0 caus
1ff02 65 73 0a 20 20 2a 2a 20 62 69 74 20 30 20 74 6f es. ** bit 0 to
1ff03 20 62 65 20 73 65 74 2e 20 20 43 6f 6c 75 6d 6e be set. Column
1ff04 20 31 20 73 65 74 73 20 62 69 74 20 31 2e 20 20 1 sets bit 1.
1ff05 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49 And so forth. I
1ff06 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6c 75 6d f the. ** colum
1ff07 6e 20 6e 75 6d 62 65 72 20 69 73 20 67 72 65 61 n number is grea
1ff08 74 65 72 20 74 68 61 6e 20 74 68 65 20 6e 75 6d ter than the num
1ff09 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e 20 74 ber of bits in t
1ff0a 68 65 20 62 69 74 6d 61 73 6b 0a 20 20 2a 2a 20 he bitmask. **
1ff0b 74 68 65 6e 20 73 65 74 20 74 68 65 20 68 69 67 then set the hig
1ff0c 68 2d 6f 72 64 65 72 20 62 69 74 20 6f 66 20 74 h-order bit of t
1ff0d 68 65 20 62 69 74 6d 61 73 6b 2e 0a 20 20 2a 2f he bitmask.. */
1ff0e 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43 . if( pExpr->iC
1ff0f 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70 4d 61 74 olumn>=0 && pMat
1ff10 63 68 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 ch!=0 ){. int
1ff11 20 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c n = pExpr->iCol
1ff12 75 6d 6e 3b 0a 20 20 20 20 74 65 73 74 63 61 73 umn;. testcas
1ff13 65 28 20 6e 3d 3d 42 4d 53 2d 31 20 29 3b 0a 20 e( n==BMS-1 );.
1ff14 20 20 20 69 66 28 20 6e 3e 3d 42 4d 53 20 29 7b if( n>=BMS ){
1ff15 0a 20 20 20 20 20 20 6e 20 3d 20 42 4d 53 2d 31 . n = BMS-1
1ff16 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 ;. }. asse
1ff17 72 74 28 20 70 4d 61 74 63 68 2d 3e 69 43 75 72 rt( pMatch->iCur
1ff18 73 6f 72 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 sor==pExpr->iTab
1ff19 6c 65 20 29 3b 0a 20 20 20 20 70 4d 61 74 63 68 le );. pMatch
1ff1a 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 28 28 42 ->colUsed |= ((B
1ff1b 69 74 6d 61 73 6b 29 31 29 3c 3c 6e 3b 0a 20 20 itmask)1)<<n;.
1ff1c 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70 }.. /* Clean up
1ff1d 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20 2a 2f and return. */
1ff1e 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 . sqlite3ExprDe
1ff1f 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 2d 3e lete(db, pExpr->
1ff20 70 4c 65 66 74 29 3b 0a 20 20 70 45 78 70 72 2d pLeft);. pExpr-
1ff21 3e 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20 73 71 >pLeft = 0;. sq
1ff22 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
1ff23 64 62 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 db, pExpr->pRigh
1ff24 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 69 t);. pExpr->pRi
1ff25 67 68 74 20 3d 20 30 3b 0a 20 20 70 45 78 70 72 ght = 0;. pExpr
1ff26 2d 3e 6f 70 20 3d 20 28 69 73 54 72 69 67 67 65 ->op = (isTrigge
1ff27 72 20 3f 20 54 4b 5f 54 52 49 47 47 45 52 20 3a r ? TK_TRIGGER :
1ff28 20 54 4b 5f 43 4f 4c 55 4d 4e 29 3b 0a 6c 6f 6f TK_COLUMN);.loo
1ff29 6b 75 70 6e 61 6d 65 5f 65 6e 64 3a 0a 20 20 69 kupname_end:. i
1ff2a 66 28 20 63 6e 74 3d 3d 31 20 29 7b 0a 20 20 20 f( cnt==1 ){.
1ff2b 20 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30 20 assert( pNC!=0
1ff2c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 );. sqlite3Au
1ff2d 74 68 52 65 61 64 28 70 50 61 72 73 65 2c 20 70 thRead(pParse, p
1ff2e 45 78 70 72 2c 20 70 53 63 68 65 6d 61 2c 20 70 Expr, pSchema, p
1ff2f 4e 43 2d 3e 70 53 72 63 4c 69 73 74 29 3b 0a 20 NC->pSrcList);.
1ff30 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 /* Increment
1ff31 74 68 65 20 6e 52 65 66 20 76 61 6c 75 65 20 6f the nRef value o
1ff32 6e 20 61 6c 6c 20 6e 61 6d 65 20 63 6f 6e 74 65 n all name conte
1ff33 78 74 73 20 66 72 6f 6d 20 54 6f 70 4e 43 20 75 xts from TopNC u
1ff34 70 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 p to. ** the
1ff35 70 6f 69 6e 74 20 77 68 65 72 65 20 74 68 65 20 point where the
1ff36 6e 61 6d 65 20 6d 61 74 63 68 65 64 2e 20 2a 2f name matched. */
1ff37 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 . for(;;){.
1ff38 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 70 assert( pTop
1ff39 4e 43 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 NC!=0 );. p
1ff3a 54 6f 70 4e 43 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 TopNC->nRef++;.
1ff3b 20 20 20 20 20 69 66 28 20 70 54 6f 70 4e 43 3d if( pTopNC=
1ff3c 3d 70 4e 43 20 29 20 62 72 65 61 6b 3b 0a 20 20 =pNC ) break;.
1ff3d 20 20 20 20 70 54 6f 70 4e 43 20 3d 20 70 54 6f pTopNC = pTo
1ff3e 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 pNC->pNext;.
1ff3f 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 }. return WRC
1ff40 5f 50 72 75 6e 65 3b 0a 20 20 7d 20 65 6c 73 65 _Prune;. } else
1ff41 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 {. return WR
1ff42 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 7d 0a 0a C_Abort;. }.}..
1ff43 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 /*.** Allocate a
1ff44 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e nd return a poin
1ff45 74 65 72 20 74 6f 20 61 6e 20 65 78 70 72 65 73 ter to an expres
1ff46 73 69 6f 6e 20 74 6f 20 6c 6f 61 64 20 74 68 65 sion to load the
1ff47 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 0a 2a 2a 20 column iCol.**
1ff48 66 72 6f 6d 20 64 61 74 61 73 6f 75 72 63 65 20 from datasource
1ff49 69 53 72 63 20 64 61 74 61 73 6f 75 72 63 65 20 iSrc datasource
1ff4a 69 6e 20 53 72 63 4c 69 73 74 20 70 53 72 63 2e in SrcList pSrc.
1ff4b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1ff4c 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 TE Expr *sqlite3
1ff4d 43 72 65 61 74 65 43 6f 6c 75 6d 6e 45 78 70 72 CreateColumnExpr
1ff4e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 72 (sqlite3 *db, Sr
1ff4f 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 69 6e 74 cList *pSrc, int
1ff50 20 69 53 72 63 2c 20 69 6e 74 20 69 43 6f 6c 29 iSrc, int iCol)
1ff51 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 73 71 {. Expr *p = sq
1ff52 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 64 lite3ExprAlloc(d
1ff53 62 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20 30 2c b, TK_COLUMN, 0,
1ff54 20 30 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 0);. if( p ){.
1ff55 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 struct SrcLi
1ff56 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d st_item *pItem =
1ff57 20 26 70 53 72 63 2d 3e 61 5b 69 53 72 63 5d 3b &pSrc->a[iSrc];
1ff58 0a 20 20 20 20 70 2d 3e 70 54 61 62 20 3d 20 70 . p->pTab = p
1ff59 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 Item->pTab;.
1ff5a 70 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 74 65 p->iTable = pIte
1ff5b 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 m->iCursor;.
1ff5c 69 66 28 20 70 2d 3e 70 54 61 62 2d 3e 69 50 4b if( p->pTab->iPK
1ff5d 65 79 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 ey==iCol ){.
1ff5e 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d p->iColumn = -
1ff5f 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 1;. }else{.
1ff60 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d p->iColumn =
1ff61 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 70 49 74 iCol;. pIt
1ff62 65 6d 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 28 em->colUsed |= (
1ff63 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 28 69 43 (Bitmask)1)<<(iC
1ff64 6f 6c 3e 3d 42 4d 53 20 3f 20 42 4d 53 2d 31 20 ol>=BMS ? BMS-1
1ff65 3a 20 69 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20 : iCol);. }.
1ff66 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 ExprSetProper
1ff67 74 79 28 70 2c 20 45 50 5f 52 65 73 6f 6c 76 65 ty(p, EP_Resolve
1ff68 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e d);. }. return
1ff69 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 p;.}../*.** Thi
1ff6a 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
1ff6b 6c 62 61 63 6b 20 66 6f 72 20 73 71 6c 69 74 65 lback for sqlite
1ff6c 33 57 61 6c 6b 45 78 70 72 28 29 2e 0a 2a 2a 0a 3WalkExpr()..**.
1ff6d 2a 2a 20 52 65 73 6f 6c 76 65 20 73 79 6d 62 6f ** Resolve symbo
1ff6e 6c 69 63 20 6e 61 6d 65 73 20 69 6e 74 6f 20 54 lic names into T
1ff6f 4b 5f 43 4f 4c 55 4d 4e 20 6f 70 65 72 61 74 6f K_COLUMN operato
1ff70 72 73 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 rs for the curre
1ff71 6e 74 0a 2a 2a 20 6e 6f 64 65 20 69 6e 20 74 68 nt.** node in th
1ff72 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 e expression tre
1ff73 65 2e 20 20 52 65 74 75 72 6e 20 30 20 74 6f 20 e. Return 0 to
1ff74 63 6f 6e 74 69 6e 75 65 20 74 68 65 20 73 65 61 continue the sea
1ff75 72 63 68 20 64 6f 77 6e 0a 2a 2a 20 74 68 65 20 rch down.** the
1ff76 74 72 65 65 20 6f 72 20 32 20 74 6f 20 61 62 6f tree or 2 to abo
1ff77 72 74 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b rt the tree walk
1ff78 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1ff79 74 69 6e 65 20 61 6c 73 6f 20 64 6f 65 73 20 65 tine also does e
1ff7a 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 61 6e rror checking an
1ff7b 64 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f d name resolutio
1ff7c 6e 20 66 6f 72 0a 2a 2a 20 66 75 6e 63 74 69 6f n for.** functio
1ff7d 6e 20 6e 61 6d 65 73 2e 20 20 54 68 65 20 6f 70 n names. The op
1ff7e 65 72 61 74 6f 72 20 66 6f 72 20 61 67 67 72 65 erator for aggre
1ff7f 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 gate functions i
1ff80 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 74 6f 20 s changed.** to
1ff81 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e TK_AGG_FUNCTION.
1ff82 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 .*/.static int r
1ff83 65 73 6f 6c 76 65 45 78 70 72 53 74 65 70 28 57 esolveExprStep(W
1ff84 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 alker *pWalker,
1ff85 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 Expr *pExpr){.
1ff86 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 NameContext *pNC
1ff87 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 ;. Parse *pPars
1ff88 65 3b 0a 0a 20 20 70 4e 43 20 3d 20 70 57 61 6c e;.. pNC = pWal
1ff89 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 61 73 ker->u.pNC;. as
1ff8a 73 65 72 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a sert( pNC!=0 );.
1ff8b 20 20 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e pParse = pNC->
1ff8c 70 50 61 72 73 65 3b 0a 20 20 61 73 73 65 72 74 pParse;. assert
1ff8d 28 20 70 50 61 72 73 65 3d 3d 70 57 61 6c 6b 65 ( pParse==pWalke
1ff8e 72 2d 3e 70 50 61 72 73 65 20 29 3b 0a 0a 20 20 r->pParse );..
1ff8f 69 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 72 if( ExprHasAnyPr
1ff90 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
1ff91 5f 52 65 73 6f 6c 76 65 64 29 20 29 20 72 65 74 _Resolved) ) ret
1ff92 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 urn WRC_Prune;.
1ff93 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 ExprSetProperty
1ff94 28 70 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c (pExpr, EP_Resol
1ff95 76 65 64 29 3b 0a 23 69 66 6e 64 65 66 20 4e 44 ved);.#ifndef ND
1ff96 45 42 55 47 0a 20 20 69 66 28 20 70 4e 43 2d 3e EBUG. if( pNC->
1ff97 70 53 72 63 4c 69 73 74 20 26 26 20 70 4e 43 2d pSrcList && pNC-
1ff98 3e 70 53 72 63 4c 69 73 74 2d 3e 6e 41 6c 6c 6f >pSrcList->nAllo
1ff99 63 3e 30 20 29 7b 0a 20 20 20 20 53 72 63 4c 69 c>0 ){. SrcLi
1ff9a 73 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 st *pSrcList = p
1ff9b 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20 NC->pSrcList;.
1ff9c 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 int i;. for
1ff9d 28 69 3d 30 3b 20 69 3c 70 4e 43 2d 3e 70 53 72 (i=0; i<pNC->pSr
1ff9e 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b cList->nSrc; i++
1ff9f 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
1ffa0 20 70 53 72 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e pSrcList->a[i].
1ffa1 69 43 75 72 73 6f 72 3e 3d 30 20 26 26 20 70 53 iCursor>=0 && pS
1ffa2 72 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 rcList->a[i].iCu
1ffa3 72 73 6f 72 3c 70 50 61 72 73 65 2d 3e 6e 54 61 rsor<pParse->nTa
1ffa4 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 b);. }. }.#e
1ffa5 6e 64 69 66 0a 20 20 73 77 69 74 63 68 28 20 70 ndif. switch( p
1ffa6 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 0a 23 69 66 Expr->op ){..#if
1ffa7 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
1ffa8 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 ENABLE_UPDATE_DE
1ffa9 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 LETE_LIMIT) && !
1ffaa 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
1ffab 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a 20 20 MIT_SUBQUERY).
1ffac 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69 61 6c /* The special
1ffad 20 6f 70 65 72 61 74 6f 72 20 54 4b 5f 52 4f 57 operator TK_ROW
1ffae 20 6d 65 61 6e 73 20 75 73 65 20 74 68 65 20 72 means use the r
1ffaf 6f 77 69 64 20 66 6f 72 20 74 68 65 20 66 69 72 owid for the fir
1ffb0 73 74 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e st. ** column
1ffb1 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 in the FROM cla
1ffb2 75 73 65 2e 20 20 54 68 69 73 20 69 73 20 75 73 use. This is us
1ffb3 65 64 20 62 79 20 74 68 65 20 4c 49 4d 49 54 20 ed by the LIMIT
1ffb4 61 6e 64 20 4f 52 44 45 52 20 42 59 0a 20 20 20 and ORDER BY.
1ffb5 20 2a 2a 20 63 6c 61 75 73 65 20 70 72 6f 63 65 ** clause proce
1ffb6 73 73 69 6e 67 20 6f 6e 20 55 50 44 41 54 45 20 ssing on UPDATE
1ffb7 61 6e 64 20 44 45 4c 45 54 45 20 73 74 61 74 65 and DELETE state
1ffb8 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 ments.. */.
1ffb9 20 20 63 61 73 65 20 54 4b 5f 52 4f 57 3a 20 7b case TK_ROW: {
1ffba 0a 20 20 20 20 20 20 53 72 63 4c 69 73 74 20 2a . SrcList *
1ffbb 70 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e pSrcList = pNC->
1ffbc 70 53 72 63 4c 69 73 74 3b 0a 20 20 20 20 20 20 pSrcList;.
1ffbd 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 struct SrcList_i
1ffbe 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 tem *pItem;.
1ffbf 20 20 61 73 73 65 72 74 28 20 70 53 72 63 4c 69 assert( pSrcLi
1ffc0 73 74 20 26 26 20 70 53 72 63 4c 69 73 74 2d 3e st && pSrcList->
1ffc1 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 20 20 20 nSrc==1 );.
1ffc2 20 70 49 74 65 6d 20 3d 20 70 53 72 63 4c 69 73 pItem = pSrcLis
1ffc3 74 2d 3e 61 3b 20 0a 20 20 20 20 20 20 70 45 78 t->a; . pEx
1ffc4 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55 pr->op = TK_COLU
1ffc5 4d 4e 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d MN;. pExpr-
1ffc6 3e 70 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e 70 >pTab = pItem->p
1ffc7 54 61 62 3b 0a 20 20 20 20 20 20 70 45 78 70 72 Tab;. pExpr
1ffc8 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 74 65 6d ->iTable = pItem
1ffc9 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 ->iCursor;.
1ffca 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 pExpr->iColumn
1ffcb 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 45 78 70 = -1;. pExp
1ffcc 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 r->affinity = SQ
1ffcd 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 LITE_AFF_INTEGER
1ffce 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1ffcf 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 }.#endif /* d
1ffd0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e efined(SQLITE_EN
1ffd1 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 ABLE_UPDATE_DELE
1ffd2 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65 TE_LIMIT) && !de
1ffd3 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1ffd4 54 5f 53 55 42 51 55 45 52 59 29 20 2a 2f 0a 0a T_SUBQUERY) */..
1ffd5 20 20 20 20 2f 2a 20 41 20 6c 6f 6e 65 20 69 64 /* A lone id
1ffd6 65 6e 74 69 66 69 65 72 20 69 73 20 74 68 65 20 entifier is the
1ffd7 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e name of a column
1ffd8 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 .. */. cas
1ffd9 65 20 54 4b 5f 49 44 3a 20 7b 0a 20 20 20 20 20 e TK_ID: {.
1ffda 20 72 65 74 75 72 6e 20 6c 6f 6f 6b 75 70 4e 61 return lookupNa
1ffdb 6d 65 28 70 50 61 72 73 65 2c 20 30 2c 20 30 2c me(pParse, 0, 0,
1ffdc 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e pExpr->u.zToken
1ffdd 2c 20 70 4e 43 2c 20 70 45 78 70 72 29 3b 0a 20 , pNC, pExpr);.
1ffde 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 41 }. . /* A
1ffdf 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20 table name and
1ffe0 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20 20 20 20 column name:
1ffe1 20 49 44 2e 49 44 0a 20 20 20 20 2a 2a 20 4f 72 ID.ID. ** Or
1ffe2 20 61 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 a database, tab
1ffe3 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 3a 20 20 le and column:
1ffe4 49 44 2e 49 44 2e 49 44 0a 20 20 20 20 2a 2f 0a ID.ID.ID. */.
1ffe5 20 20 20 20 63 61 73 65 20 54 4b 5f 44 4f 54 3a case TK_DOT:
1ffe6 20 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 {. const c
1ffe7 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 3b 0a 20 20 har *zColumn;.
1ffe8 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
1ffe9 7a 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 63 6f zTable;. co
1ffea 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 0a 20 nst char *zDb;.
1ffeb 20 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 Expr *pRigh
1ffec 74 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 69 66 28 t;.. /* if(
1ffed 20 70 53 72 63 4c 69 73 74 3d 3d 30 20 29 20 62 pSrcList==0 ) b
1ffee 72 65 61 6b 3b 20 2a 2f 0a 20 20 20 20 20 20 70 reak; */. p
1ffef 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 70 Right = pExpr->p
1fff0 52 69 67 68 74 3b 0a 20 20 20 20 20 20 69 66 28 Right;. if(
1fff1 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f pRight->op==TK_
1fff2 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 44 ID ){. zD
1fff3 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7a b = 0;. z
1fff4 54 61 62 6c 65 20 3d 20 70 45 78 70 72 2d 3e 70 Table = pExpr->p
1fff5 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a Left->u.zToken;.
1fff6 20 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d 6e 20 zColumn
1fff7 3d 20 70 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b = pRight->u.zTok
1fff8 65 6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b en;. }else{
1fff9 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1fffa 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f pRight->op==TK_
1fffb 44 4f 54 20 29 3b 0a 20 20 20 20 20 20 20 20 7a DOT );. z
1fffc 44 62 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 Db = pExpr->pLef
1fffd 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 t->u.zToken;.
1fffe 20 20 20 20 20 7a 54 61 62 6c 65 20 3d 20 70 52 zTable = pR
1ffff 69 67 68 74 2d 3e 70 4c 65 66 74 2d 3e 75 2e 7a ight->pLeft->u.z
20000 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 7a Token;. z
20001 43 6f 6c 75 6d 6e 20 3d 20 70 52 69 67 68 74 2d Column = pRight-
20002 3e 70 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 >pRight->u.zToke
20003 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 n;. }.
20004 20 72 65 74 75 72 6e 20 6c 6f 6f 6b 75 70 4e 61 return lookupNa
20005 6d 65 28 70 50 61 72 73 65 2c 20 7a 44 62 2c 20 me(pParse, zDb,
20006 7a 54 61 62 6c 65 2c 20 7a 43 6f 6c 75 6d 6e 2c zTable, zColumn,
20007 20 70 4e 43 2c 20 70 45 78 70 72 29 3b 0a 20 20 pNC, pExpr);.
20008 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f }.. /* Reso
20009 6c 76 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d lve function nam
2000a 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 es. */. ca
2000b 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 se TK_CONST_FUNC
2000c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 :. case TK_FU
2000d 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 NCTION: {.
2000e 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 ExprList *pList
2000f 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 = pExpr->x.pList
20010 3b 20 20 20 20 2f 2a 20 54 68 65 20 61 72 67 75 ; /* The argu
20011 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 20 ment list */.
20012 20 20 20 69 6e 74 20 6e 20 3d 20 70 4c 69 73 74 int n = pList
20013 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 ? pList->nExpr
20014 3a 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 : 0; /* Numbe
20015 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a r of arguments *
20016 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 6f 5f 73 /. int no_s
20017 75 63 68 5f 66 75 6e 63 20 3d 20 30 3b 20 20 20 uch_func = 0;
20018 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e /* True if n
20019 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 20 o such function
2001a 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 20 20 exists */.
2001b 69 6e 74 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 int wrong_num_ar
2001c 67 73 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 gs = 0; /* T
2001d 72 75 65 20 69 66 20 77 72 6f 6e 67 20 6e 75 6d rue if wrong num
2001e 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 ber of arguments
2001f 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 73 */. int is
20020 5f 61 67 67 20 3d 20 30 3b 20 20 20 20 20 20 20 _agg = 0;
20021 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
20022 20 69 73 20 61 6e 20 61 67 67 72 65 67 61 74 65 is an aggregate
20023 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 function */.
20024 20 20 20 69 6e 74 20 61 75 74 68 3b 20 20 20 20 int auth;
20025 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
20026 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 * Authorization
20027 74 6f 20 75 73 65 20 74 68 65 20 66 75 6e 63 74 to use the funct
20028 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ion */. int
20029 20 6e 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 nId;
2002a 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
2002b 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 er of characters
2002c 20 69 6e 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d in function nam
2002d 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 e */. const
2002e 20 63 68 61 72 20 2a 7a 49 64 3b 20 20 20 20 20 char *zId;
2002f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 /* The fu
20030 6e 63 74 69 6f 6e 20 6e 61 6d 65 2e 20 2a 2f 0a nction name. */.
20031 20 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 FuncDef *p
20032 44 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Def;
20033 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e /* Information
20034 20 61 62 6f 75 74 20 74 68 65 20 66 75 6e 63 74 about the funct
20035 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 ion */. u8
20036 65 6e 63 20 3d 20 45 4e 43 28 70 50 61 72 73 65 enc = ENC(pParse
20037 2d 3e 64 62 29 3b 20 20 20 2f 2a 20 54 68 65 20 ->db); /* The
20038 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e database encodin
20039 67 20 2a 2f 0a 0a 20 20 20 20 20 20 74 65 73 74 g */.. test
2003a 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d case( pExpr->op=
2003b 3d 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 29 =TK_CONST_FUNC )
2003c 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
2003d 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 !ExprHasProperty
2003e 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 (pExpr, EP_xIsSe
2003f 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 7a lect) );. z
20040 49 64 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54 Id = pExpr->u.zT
20041 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 6e 49 64 20 oken;. nId
20042 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
20043 30 28 7a 49 64 29 3b 0a 20 20 20 20 20 20 70 44 0(zId);. pD
20044 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 ef = sqlite3Find
20045 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d Function(pParse-
20046 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 6e >db, zId, nId, n
20047 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 , enc, 0);.
20048 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a if( pDef==0 ){.
20049 20 20 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 pDef = s
2004a 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 qlite3FindFuncti
2004b 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a on(pParse->db, z
2004c 49 64 2c 20 6e 49 64 2c 20 2d 31 2c 20 65 6e 63 Id, nId, -1, enc
2004d 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 , 0);. if
2004e 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20 ( pDef==0 ){.
2004f 20 20 20 20 20 20 20 6e 6f 5f 73 75 63 68 5f 66 no_such_f
20050 75 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 unc = 1;.
20051 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
20052 20 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 wrong_num_args
20053 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 1;. }.
20054 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
20055 20 20 20 20 20 69 73 5f 61 67 67 20 3d 20 70 44 is_agg = pD
20056 65 66 2d 3e 78 46 75 6e 63 3d 3d 30 3b 0a 20 20 ef->xFunc==0;.
20057 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }.#ifndef SQ
20058 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 LITE_OMIT_AUTHOR
20059 49 5a 41 54 49 4f 4e 0a 20 20 20 20 20 20 69 66 IZATION. if
2005a 28 20 70 44 65 66 20 29 7b 0a 20 20 20 20 20 20 ( pDef ){.
2005b 20 20 61 75 74 68 20 3d 20 73 71 6c 69 74 65 33 auth = sqlite3
2005c 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 AuthCheck(pParse
2005d 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f , SQLITE_FUNCTIO
2005e 4e 2c 20 30 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d N, 0, pDef->zNam
2005f 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 e, 0);. i
20060 66 28 20 61 75 74 68 21 3d 53 51 4c 49 54 45 5f f( auth!=SQLITE_
20061 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
20062 69 66 28 20 61 75 74 68 3d 3d 53 51 4c 49 54 45 if( auth==SQLITE
20063 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 20 20 20 _DENY ){.
20064 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
20065 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f rMsg(pParse, "no
20066 74 20 61 75 74 68 6f 72 69 7a 65 64 20 74 6f 20 t authorized to
20067 75 73 65 20 66 75 6e 63 74 69 6f 6e 3a 20 25 73 use function: %s
20068 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
20069 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2006a 20 20 20 20 20 20 20 70 44 65 66 2d 3e 7a 4e 61 pDef->zNa
2006b 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 me);.
2006c 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 pNC->nErr++;.
2006d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2006e 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 pExpr->op =
2006f 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 TK_NULL;.
20070 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 return WRC_Pr
20071 75 6e 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 une;. }.
20072 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }.#endif.
20073 20 20 20 20 69 66 28 20 69 73 5f 61 67 67 20 26 if( is_agg &
20074 26 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 & !pNC->allowAgg
20075 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
20076 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
20077 73 65 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 61 se, "misuse of a
20078 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
20079 6e 20 25 2e 2a 73 28 29 22 2c 20 6e 49 64 2c 7a n %.*s()", nId,z
2007a 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 Id);. pNC
2007b 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 ->nErr++;.
2007c 20 20 69 73 5f 61 67 67 20 3d 20 30 3b 0a 20 20 is_agg = 0;.
2007d 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 6f }else if( no
2007e 5f 73 75 63 68 5f 66 75 6e 63 20 29 7b 0a 20 20 _such_func ){.
2007f 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
20080 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e orMsg(pParse, "n
20081 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 3a o such function:
20082 20 25 2e 2a 73 22 2c 20 6e 49 64 2c 20 7a 49 64 %.*s", nId, zId
20083 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e );. pNC->
20084 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 nErr++;. }e
20085 6c 73 65 20 69 66 28 20 77 72 6f 6e 67 5f 6e 75 lse if( wrong_nu
20086 6d 5f 61 72 67 73 20 29 7b 0a 20 20 20 20 20 20 m_args ){.
20087 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
20088 67 28 70 50 61 72 73 65 2c 22 77 72 6f 6e 67 20 g(pParse,"wrong
20089 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 number of argume
2008a 6e 74 73 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 nts to function
2008b 25 2e 2a 73 28 29 22 2c 0a 20 20 20 20 20 20 20 %.*s()",.
2008c 20 20 20 20 20 20 6e 49 64 2c 20 7a 49 64 29 3b nId, zId);
2008d 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 . pNC->nE
2008e 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rr++;. }.
2008f 20 20 20 20 69 66 28 20 69 73 5f 61 67 67 20 29 if( is_agg )
20090 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d {. pExpr-
20091 3e 6f 70 20 3d 20 54 4b 5f 41 47 47 5f 46 55 4e >op = TK_AGG_FUN
20092 43 54 49 4f 4e 3b 0a 20 20 20 20 20 20 20 20 70 CTION;. p
20093 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 31 3b 0a NC->hasAgg = 1;.
20094 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
20095 28 20 69 73 5f 61 67 67 20 29 20 70 4e 43 2d 3e ( is_agg ) pNC->
20096 61 6c 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20 allowAgg = 0;.
20097 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 sqlite3WalkE
20098 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c xprList(pWalker,
20099 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 69 pList);. i
2009a 66 28 20 69 73 5f 61 67 67 20 29 20 70 4e 43 2d f( is_agg ) pNC-
2009b 3e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 20 >allowAgg = 1;.
2009c 20 20 20 20 20 2f 2a 20 46 49 58 20 4d 45 3a 20 /* FIX ME:
2009d 20 43 6f 6d 70 75 74 65 20 70 45 78 70 72 2d 3e Compute pExpr->
2009e 61 66 66 69 6e 69 74 79 20 62 61 73 65 64 20 6f affinity based o
2009f 6e 20 74 68 65 20 65 78 70 65 63 74 65 64 20 72 n the expected r
200a0 65 74 75 72 6e 0a 20 20 20 20 20 20 2a 2a 20 74 eturn. ** t
200a1 79 70 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 ype of the funct
200a2 69 6f 6e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ion . */.
200a3 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 return WRC_P
200a4 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 23 69 66 6e rune;. }.#ifn
200a5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
200a6 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73 SUBQUERY. cas
200a7 65 20 54 4b 5f 53 45 4c 45 43 54 3a 0a 20 20 20 e TK_SELECT:.
200a8 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a case TK_EXISTS:
200a9 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 testcase( pExp
200aa 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 r->op==TK_EXISTS
200ab 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 );.#endif. c
200ac 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 ase TK_IN: {.
200ad 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 testcase( pEx
200ae 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 29 3b pr->op==TK_IN );
200af 0a 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 . if( ExprH
200b0 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 asProperty(pExpr
200b1 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 , EP_xIsSelect)
200b2 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e ){. int n
200b3 52 65 66 20 3d 20 70 4e 43 2d 3e 6e 52 65 66 3b Ref = pNC->nRef;
200b4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
200b5 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 20 OMIT_CHECK.
200b6 20 20 20 69 66 28 20 70 4e 43 2d 3e 69 73 43 68 if( pNC->isCh
200b7 65 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 eck ){.
200b8 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
200b9 28 70 50 61 72 73 65 2c 22 73 75 62 71 75 65 72 (pParse,"subquer
200ba 69 65 73 20 70 72 6f 68 69 62 69 74 65 64 20 69 ies prohibited i
200bb 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 n CHECK constrai
200bc 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 20 20 7d nts");. }
200bd 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 .#endif.
200be 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 sqlite3WalkSelec
200bf 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 t(pWalker, pExpr
200c0 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 ->x.pSelect);.
200c1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4e assert( pN
200c2 43 2d 3e 6e 52 65 66 3e 3d 6e 52 65 66 20 29 3b C->nRef>=nRef );
200c3 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 52 65 . if( nRe
200c4 66 21 3d 70 4e 43 2d 3e 6e 52 65 66 20 29 7b 0a f!=pNC->nRef ){.
200c5 20 20 20 20 20 20 20 20 20 20 45 78 70 72 53 65 ExprSe
200c6 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c tProperty(pExpr,
200c7 20 45 50 5f 56 61 72 53 65 6c 65 63 74 29 3b 0a EP_VarSelect);.
200c8 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
200c9 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
200ca 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c }.#ifndef SQL
200cb 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 ITE_OMIT_CHECK.
200cc 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49 41 case TK_VARIA
200cd 42 4c 45 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 BLE: {. if(
200ce 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29 7b pNC->isCheck ){
200cf 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
200d0 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
200d1 22 70 61 72 61 6d 65 74 65 72 73 20 70 72 6f 68 "parameters proh
200d2 69 62 69 74 65 64 20 69 6e 20 43 48 45 43 4b 20 ibited in CHECK
200d3 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a 20 constraints");.
200d4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
200d5 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 ak;. }.#endif
200d6 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 . }. return (p
200d7 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 70 Parse->nErr || p
200d8 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f Parse->db->mallo
200d9 63 46 61 69 6c 65 64 29 20 3f 20 57 52 43 5f 41 cFailed) ? WRC_A
200da 62 6f 72 74 20 3a 20 57 52 43 5f 43 6f 6e 74 69 bort : WRC_Conti
200db 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 nue;.}../*.** pE
200dc 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f List is a list o
200dd 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 77 68 f expressions wh
200de 69 63 68 20 61 72 65 20 72 65 61 6c 6c 79 20 74 ich are really t
200df 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 he result set of
200e0 20 74 68 65 0a 2a 2a 20 61 20 53 45 4c 45 43 54 the.** a SELECT
200e1 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 70 45 20 statement. pE
200e2 69 73 20 61 20 74 65 72 6d 20 69 6e 20 61 6e 20 is a term in an
200e3 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 ORDER BY or GROU
200e4 50 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 P BY clause..**
200e5 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 This routine che
200e6 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 70 45 cks to see if pE
200e7 20 69 73 20 61 20 73 69 6d 70 6c 65 20 69 64 65 is a simple ide
200e8 6e 74 69 66 69 65 72 20 77 68 69 63 68 20 63 6f ntifier which co
200e9 72 72 65 73 70 6f 6e 64 73 0a 2a 2a 20 74 6f 20 rresponds.** to
200ea 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f 66 20 6f the AS-name of o
200eb 6e 65 20 6f 66 20 74 68 65 20 74 65 72 6d 73 20 ne of the terms
200ec 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f of the expressio
200ed 6e 20 6c 69 73 74 2e 20 20 49 66 20 69 74 20 69 n list. If it i
200ee 73 2c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 s,.** this routi
200ef 6e 65 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 ne return an int
200f0 65 67 65 72 20 62 65 74 77 65 65 6e 20 31 20 61 eger between 1 a
200f1 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69 73 20 nd N where N is
200f2 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a the number of.**
200f3 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 70 45 4c elements in pEL
200f4 69 73 74 2c 20 63 6f 72 72 65 73 70 6f 6e 64 69 ist, correspondi
200f5 6e 67 20 74 6f 20 74 68 65 20 6d 61 74 63 68 69 ng to the matchi
200f6 6e 67 20 65 6e 74 72 79 2e 20 20 49 66 20 74 68 ng entry. If th
200f7 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 20 6d 61 74 ere is.** no mat
200f8 63 68 2c 20 6f 72 20 69 66 20 70 45 20 69 73 20 ch, or if pE is
200f9 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20 69 64 65 not a simple ide
200fa 6e 74 69 66 69 65 72 2c 20 74 68 65 6e 20 74 68 ntifier, then th
200fb 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 is routine.** re
200fc 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 70 45 turn 0..**.** pE
200fd 4c 69 73 74 20 68 61 73 20 62 65 65 6e 20 72 65 List has been re
200fe 73 6f 6c 76 65 64 2e 20 20 70 45 20 68 61 73 20 solved. pE has
200ff 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 not..*/.static i
20100 6e 74 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d 65 nt resolveAsName
20101 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
20102 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e e, /* Parsin
20103 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 65 72 g context for er
20104 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a ror messages */.
20105 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 ExprList *pELi
20106 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 st, /* List of
20107 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f 20 73 expressions to s
20108 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 can */. Expr *p
20109 45 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 E /* E
2010a 78 70 72 65 73 73 69 6f 6e 20 77 65 20 61 72 65 xpression we are
2010b 20 74 72 79 69 6e 67 20 74 6f 20 6d 61 74 63 68 trying to match
2010c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 */.){. int i;
2010d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
2010e 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a oop counter */..
2010f 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
20110 45 52 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 69 ER(pParse);.. i
20111 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 f( pE->op==TK_ID
20112 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 ){. char *zC
20113 6f 6c 20 3d 20 70 45 2d 3e 75 2e 7a 54 6f 6b 65 ol = pE->u.zToke
20114 6e 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 n;. for(i=0;
20115 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b i<pEList->nExpr;
20116 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 i++){. cha
20117 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 2d r *zAs = pEList-
20118 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 >a[i].zName;.
20119 20 20 20 69 66 28 20 7a 41 73 21 3d 30 20 26 26 if( zAs!=0 &&
2011a 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
2011b 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b zAs, zCol)==0 ){
2011c 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
2011d 69 2b 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 i+1;. }.
2011e 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
2011f 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 20 69 0;.}../*.** pE i
20120 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
20121 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77 68 69 n expression whi
20122 63 68 20 69 73 20 61 20 73 69 6e 67 6c 65 20 74 ch is a single t
20123 65 72 6d 20 69 6e 20 74 68 65 0a 2a 2a 20 4f 52 erm in the.** OR
20124 44 45 52 20 42 59 20 6f 66 20 61 20 63 6f 6d 70 DER BY of a comp
20125 6f 75 6e 64 20 53 45 4c 45 43 54 2e 20 20 54 68 ound SELECT. Th
20126 65 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 e expression has
20127 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a 20 6e 61 6d not been.** nam
20128 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a e resolved..**.*
20129 2a 20 41 74 20 74 68 65 20 70 6f 69 6e 74 20 74 * At the point t
2012a 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
2012b 61 6c 6c 65 64 2c 20 77 65 20 61 6c 72 65 61 64 alled, we alread
2012c 79 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 0a y know that the.
2012d 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d ** ORDER BY term
2012e 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 is not an integ
2012f 65 72 20 69 6e 64 65 78 20 69 6e 74 6f 20 74 68 er index into th
20130 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54 e result set. T
20131 68 61 74 0a 2a 2a 20 63 61 73 65 20 69 73 20 68 hat.** case is h
20132 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20 63 61 andled by the ca
20133 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a lling routine..*
20134 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 *.** Attempt to
20135 6d 61 74 63 68 20 70 45 20 61 67 61 69 6e 73 74 match pE against
20136 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 result set colu
20137 6d 6e 73 20 69 6e 20 74 68 65 20 6c 65 66 74 2d mns in the left-
20138 6d 6f 73 74 0a 2a 2a 20 53 45 4c 45 43 54 20 73 most.** SELECT s
20139 74 61 74 65 6d 65 6e 74 2e 20 20 52 65 74 75 72 tatement. Retur
2013a 6e 20 74 68 65 20 69 6e 64 65 78 20 69 20 6f 66 n the index i of
2013b 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 63 6f the matching co
2013c 6c 75 6d 6e 2c 0a 2a 2a 20 61 73 20 61 6e 20 69 lumn,.** as an i
2013d 6e 64 69 63 61 74 69 6f 6e 20 74 6f 20 74 68 65 ndication to the
2013e 20 63 61 6c 6c 65 72 20 74 68 61 74 20 69 74 20 caller that it
2013f 73 68 6f 75 6c 64 20 73 6f 72 74 20 62 79 20 74 should sort by t
20140 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 2e 0a he i-th column..
20141 2a 2a 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73 74 ** The left-most
20142 20 63 6f 6c 75 6d 6e 20 69 73 20 31 2e 20 20 49 column is 1. I
20143 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 n other words, t
20144 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
20145 64 20 69 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65 d is the.** same
20146 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 74 integer value t
20147 68 61 74 20 77 6f 75 6c 64 20 62 65 20 75 73 65 hat would be use
20148 64 20 69 6e 20 74 68 65 20 53 51 4c 20 73 74 61 d in the SQL sta
20149 74 65 6d 65 6e 74 20 74 6f 20 69 6e 64 69 63 61 tement to indica
2014a 74 65 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e te.** the column
2014b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 ..**.** If there
2014c 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c 20 72 65 is no match, re
2014d 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 turn 0. Return
2014e 2d 31 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f -1 if an error o
2014f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 ccurs..*/.static
20150 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f 72 64 65 int resolveOrde
20151 72 42 79 54 65 72 6d 54 6f 45 78 70 72 4c 69 73 rByTermToExprLis
20152 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 t(. Parse *pPar
20153 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 se, /* Parsi
20154 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 65 ng context for e
20155 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f rror messages */
20156 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 . Select *pSele
20157 63 74 2c 20 20 20 2f 2a 20 54 68 65 20 53 45 4c ct, /* The SEL
20158 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 77 69 ECT statement wi
20159 74 68 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 th the ORDER BY
2015a 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 clause */. Expr
2015b 20 2a 70 45 20 20 20 20 20 20 20 20 20 20 20 2f *pE /
2015c 2a 20 54 68 65 20 73 70 65 63 69 66 69 63 20 4f * The specific O
2015d 52 44 45 52 20 42 59 20 74 65 72 6d 20 2a 2f 0a RDER BY term */.
2015e 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 ){. int i;
2015f 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
20160 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70 counter */. Exp
20161 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 rList *pEList;
20162 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 6f /* The columns o
20163 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 f the result set
20164 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 */. NameContex
20165 74 20 6e 63 3b 20 20 20 20 2f 2a 20 4e 61 6d 65 t nc; /* Name
20166 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 72 65 73 context for res
20167 6f 6c 76 69 6e 67 20 70 45 20 2a 2f 0a 0a 20 20 olving pE */..
20168 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 45 assert( sqlite3E
20169 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 45 2c xprIsInteger(pE,
2016a 20 26 69 29 3d 3d 30 20 29 3b 0a 20 20 70 45 4c &i)==0 );. pEL
2016b 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 ist = pSelect->p
2016c 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 52 65 73 EList;.. /* Res
2016d 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73 20 69 olve all names i
2016e 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 74 n the ORDER BY t
2016f 65 72 6d 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 erm expression.
20170 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 6e 63 */. memset(&nc
20171 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6e 63 29 29 , 0, sizeof(nc))
20172 3b 0a 20 20 6e 63 2e 70 50 61 72 73 65 20 3d 20 ;. nc.pParse =
20173 70 50 61 72 73 65 3b 0a 20 20 6e 63 2e 70 53 72 pParse;. nc.pSr
20174 63 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d cList = pSelect-
20175 3e 70 53 72 63 3b 0a 20 20 6e 63 2e 70 45 4c 69 >pSrc;. nc.pELi
20176 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 6e st = pEList;. n
20177 63 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a c.allowAgg = 1;.
20178 20 20 6e 63 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 nc.nErr = 0;.
20179 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f if( sqlite3Reso
2017a 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 6e 63 lveExprNames(&nc
2017b 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 73 71 6c , pE) ){. sql
2017c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 70 ite3ErrorClear(p
2017d 50 61 72 73 65 29 3b 0a 20 20 20 20 72 65 74 75 Parse);. retu
2017e 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 rn 0;. }.. /*
2017f 54 72 79 20 74 6f 20 6d 61 74 63 68 20 74 68 65 Try to match the
20180 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 65 73 ORDER BY expres
20181 73 69 6f 6e 20 61 67 61 69 6e 73 74 20 61 6e 20 sion against an
20182 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 expression. **
20183 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 in the result se
20184 74 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 31 2d t. Return an 1-
20185 62 61 73 65 64 20 69 6e 64 65 78 20 6f 66 20 74 based index of t
20186 68 65 20 6d 61 74 63 68 69 6e 67 0a 20 20 2a 2a he matching. **
20187 20 72 65 73 75 6c 74 2d 73 65 74 20 65 6e 74 72 result-set entr
20188 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d y.. */. for(i=
20189 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 0; i<pEList->nEx
2018a 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 pr; i++){. if
2018b 28 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d ( sqlite3ExprCom
2018c 70 61 72 65 28 70 45 4c 69 73 74 2d 3e 61 5b 69 pare(pEList->a[i
2018d 5d 2e 70 45 78 70 72 2c 20 70 45 29 20 29 7b 0a ].pExpr, pE) ){.
2018e 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31 return i+1
2018f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
20190 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68 2c 20 72 * If no match, r
20191 65 74 75 72 6e 20 30 2e 20 2a 2f 0a 20 20 72 65 eturn 0. */. re
20192 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
20193 20 47 65 6e 65 72 61 74 65 20 61 6e 20 4f 52 44 Generate an ORD
20194 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 ER BY or GROUP B
20195 59 20 74 65 72 6d 20 6f 75 74 2d 6f 66 2d 72 61 Y term out-of-ra
20196 6e 67 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 nge error..*/.st
20197 61 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 atic void resolv
20198 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 eOutOfRangeError
20199 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
2019a 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 e, /* Th
2019b 65 20 65 72 72 6f 72 20 63 6f 6e 74 65 78 74 20 e error context
2019c 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72 into which to wr
2019d 69 74 65 20 74 68 65 20 65 72 72 6f 72 20 2a 2f ite the error */
2019e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
2019f 54 79 70 65 2c 20 20 20 20 20 2f 2a 20 22 4f 52 Type, /* "OR
201a0 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 20 DER" or "GROUP"
201a1 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 20 */. int i,
201a2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
201a3 68 65 20 69 6e 64 65 78 20 28 31 2d 62 61 73 65 he index (1-base
201a4 64 29 20 6f 66 20 74 68 65 20 74 65 72 6d 20 6f d) of the term o
201a5 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f 0a 20 ut of range */.
201a6 20 69 6e 74 20 6d 78 20 20 20 20 20 20 20 20 20 int mx
201a7 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 /* Large
201a8 73 74 20 70 65 72 6d 69 73 73 69 62 6c 65 20 76 st permissible v
201a9 61 6c 75 65 20 6f 66 20 69 20 2a 2f 0a 29 7b 0a alue of i */.){.
201aa 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
201ab 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 22 g(pParse, . "
201ac 25 72 20 25 73 20 42 59 20 74 65 72 6d 20 6f 75 %r %s BY term ou
201ad 74 20 6f 66 20 72 61 6e 67 65 20 2d 20 73 68 6f t of range - sho
201ae 75 6c 64 20 62 65 20 22 0a 20 20 20 20 22 62 65 uld be ". "be
201af 74 77 65 65 6e 20 31 20 61 6e 64 20 25 64 22 2c tween 1 and %d",
201b0 20 69 2c 20 7a 54 79 70 65 2c 20 6d 78 29 3b 0a i, zType, mx);.
201b1 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 }../*.** Analyze
201b2 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c the ORDER BY cl
201b3 61 75 73 65 20 69 6e 20 61 20 63 6f 6d 70 6f 75 ause in a compou
201b4 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d nd SELECT statem
201b5 65 6e 74 2e 20 20 20 4d 6f 64 69 66 79 0a 2a 2a ent. Modify.**
201b6 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68 each term of th
201b7 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 e ORDER BY claus
201b8 65 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 e is a constant
201b9 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 integer between
201ba 31 0a 2a 2a 20 61 6e 64 20 4e 20 77 68 65 72 65 1.** and N where
201bb 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 N is the number
201bc 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 of columns in t
201bd 68 65 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 he compound SELE
201be 43 54 2e 0a 2a 2a 0a 2a 2a 20 4f 52 44 45 52 20 CT..**.** ORDER
201bf 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 61 72 BY terms that ar
201c0 65 20 61 6c 72 65 61 64 79 20 61 6e 20 69 6e 74 e already an int
201c1 65 67 65 72 20 62 65 74 77 65 65 6e 20 31 20 61 eger between 1 a
201c2 6e 64 20 4e 20 61 72 65 0a 2a 2a 20 75 6e 6d 6f nd N are.** unmo
201c3 64 69 66 69 65 64 2e 20 20 4f 52 44 45 52 20 42 dified. ORDER B
201c4 59 20 74 65 72 6d 73 20 74 68 61 74 20 61 72 65 Y terms that are
201c5 20 69 6e 74 65 67 65 72 73 20 6f 75 74 73 69 64 integers outsid
201c6 65 20 74 68 65 20 72 61 6e 67 65 20 6f 66 0a 2a e the range of.*
201c7 2a 20 31 20 74 68 72 6f 75 67 68 20 4e 20 67 65 * 1 through N ge
201c8 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 2e nerate an error.
201c9 20 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 ORDER BY terms
201ca 20 74 68 61 74 20 61 72 65 20 65 78 70 72 65 73 that are expres
201cb 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 74 sions.** are mat
201cc 63 68 65 64 20 61 67 61 69 6e 73 74 20 72 65 73 ched against res
201cd 75 6c 74 20 73 65 74 20 65 78 70 72 65 73 73 69 ult set expressi
201ce 6f 6e 73 20 6f 66 20 63 6f 6d 70 6f 75 6e 64 20 ons of compound
201cf 53 45 4c 45 43 54 0a 2a 2a 20 62 65 67 69 6e 6e SELECT.** beginn
201d0 69 6e 67 20 77 69 74 68 20 74 68 65 20 6c 65 66 ing with the lef
201d1 74 2d 6d 6f 73 74 20 53 45 4c 45 43 54 20 61 6e t-most SELECT an
201d2 64 20 77 6f 72 6b 69 6e 67 20 74 6f 77 61 72 64 d working toward
201d3 20 74 68 65 20 72 69 67 68 74 2e 0a 2a 2a 20 41 the right..** A
201d4 74 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 t the first matc
201d5 68 2c 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 h, the ORDER BY
201d6 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 72 expression is tr
201d7 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f 0a 2a ansformed into.*
201d8 2a 20 74 68 65 20 69 6e 74 65 67 65 72 20 63 6f * the integer co
201d9 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a lumn number..**.
201da 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
201db 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 73 mber of errors s
201dc 65 65 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 een..*/.static i
201dd 6e 74 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f 75 nt resolveCompou
201de 6e 64 4f 72 64 65 72 42 79 28 0a 20 20 50 61 72 ndOrderBy(. Par
201df 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
201e0 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f /* Parsing co
201e1 6e 74 65 78 74 2e 20 20 4c 65 61 76 65 20 65 72 ntext. Leave er
201e2 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65 72 ror messages her
201e3 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 e */. Select *p
201e4 53 65 6c 65 63 74 20 20 20 20 20 20 20 2f 2a 20 Select /*
201e5 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 The SELECT state
201e6 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ment containing
201e7 74 68 65 20 4f 52 44 45 52 20 42 59 20 2a 2f 0a the ORDER BY */.
201e8 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 45 78 ){. int i;. Ex
201e9 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 prList *pOrderBy
201ea 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 ;. ExprList *pE
201eb 4c 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 List;. sqlite3
201ec 2a 64 62 3b 0a 20 20 69 6e 74 20 6d 6f 72 65 54 *db;. int moreT
201ed 6f 44 6f 20 3d 20 31 3b 0a 0a 20 20 70 4f 72 64 oDo = 1;.. pOrd
201ee 65 72 42 79 20 3d 20 70 53 65 6c 65 63 74 2d 3e erBy = pSelect->
201ef 70 4f 72 64 65 72 42 79 3b 0a 20 20 69 66 28 20 pOrderBy;. if(
201f0 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 20 72 65 pOrderBy==0 ) re
201f1 74 75 72 6e 20 30 3b 0a 20 20 64 62 20 3d 20 70 turn 0;. db = p
201f2 50 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66 20 53 Parse->db;.#if S
201f3 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e QLITE_MAX_COLUMN
201f4 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 2d . if( pOrderBy-
201f5 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d 69 >nExpr>db->aLimi
201f6 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 t[SQLITE_LIMIT_C
201f7 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71 OLUMN] ){. sq
201f8 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
201f9 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 arse, "too many
201fa 74 65 72 6d 73 20 69 6e 20 4f 52 44 45 52 20 42 terms in ORDER B
201fb 59 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 20 Y clause");.
201fc 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 return 1;. }.#e
201fd 6e 64 69 66 0a 20 20 66 6f 72 28 69 3d 30 3b 20 ndif. for(i=0;
201fe 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 i<pOrderBy->nExp
201ff 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 4f 72 r; i++){. pOr
20200 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 derBy->a[i].done
20201 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 53 65 6c = 0;. }. pSel
20202 65 63 74 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a ect->pNext = 0;.
20203 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 while( pSelect
20204 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 ->pPrior ){.
20205 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 2d pSelect->pPrior-
20206 3e 70 4e 65 78 74 20 3d 20 70 53 65 6c 65 63 74 >pNext = pSelect
20207 3b 0a 20 20 20 20 70 53 65 6c 65 63 74 20 3d 20 ;. pSelect =
20208 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 3b pSelect->pPrior;
20209 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70 53 . }. while( pS
2020a 65 6c 65 63 74 20 26 26 20 6d 6f 72 65 54 6f 44 elect && moreToD
2020b 6f 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 o ){. struct
2020c 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 ExprList_item *p
2020d 49 74 65 6d 3b 0a 20 20 20 20 6d 6f 72 65 54 6f Item;. moreTo
2020e 44 6f 20 3d 20 30 3b 0a 20 20 20 20 70 45 4c 69 Do = 0;. pELi
2020f 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 st = pSelect->pE
20210 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 65 72 74 List;. assert
20211 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a 20 ( pEList!=0 );.
20212 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 for(i=0, pIte
20213 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 m=pOrderBy->a; i
20214 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 <pOrderBy->nExpr
20215 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b ; i++, pItem++){
20216 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 . int iCol
20217 3d 20 2d 31 3b 0a 20 20 20 20 20 20 45 78 70 72 = -1;. Expr
20218 20 2a 70 45 2c 20 2a 70 44 75 70 3b 0a 20 20 20 *pE, *pDup;.
20219 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 64 6f if( pItem->do
2021a 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 ne ) continue;.
2021b 20 20 20 20 20 70 45 20 3d 20 70 49 74 65 6d 2d pE = pItem-
2021c 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66 >pExpr;. if
2021d 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 ( sqlite3ExprIsI
2021e 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43 6f 6c nteger(pE, &iCol
2021f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 ) ){. if(
20220 20 69 43 6f 6c 3c 3d 30 20 7c 7c 20 69 43 6f 6c iCol<=0 || iCol
20221 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 >pEList->nExpr )
20222 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 6f {. reso
20223 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 lveOutOfRangeErr
20224 6f 72 28 70 50 61 72 73 65 2c 20 22 4f 52 44 45 or(pParse, "ORDE
20225 52 22 2c 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d R", i+1, pEList-
20226 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 >nExpr);.
20227 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
20228 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
20229 73 65 7b 0a 20 20 20 20 20 20 20 20 69 43 6f 6c se{. iCol
2022a 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d 65 = resolveAsName
2022b 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c (pParse, pEList,
2022c 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 69 66 pE);. if
2022d 28 20 69 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 ( iCol==0 ){.
2022e 20 20 20 20 20 20 20 70 44 75 70 20 3d 20 73 71 pDup = sq
2022f 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c lite3ExprDup(db,
20230 20 70 45 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 pE, 0);.
20231 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c if( !db->mall
20232 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
20233 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 assert(p
20234 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 Dup);.
20235 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 iCol = resolve
20236 4f 72 64 65 72 42 79 54 65 72 6d 54 6f 45 78 70 OrderByTermToExp
20237 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 53 rList(pParse, pS
20238 65 6c 65 63 74 2c 20 70 44 75 70 29 3b 0a 20 20 elect, pDup);.
20239 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2023a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 sqlite3ExprD
2023b 65 6c 65 74 65 28 64 62 2c 20 70 44 75 70 29 3b elete(db, pDup);
2023c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
2023d 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f }. if( iCo
2023e 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 43 l>0 ){. C
2023f 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 ollSeq *pColl =
20240 70 45 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 pE->pColl;.
20241 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 int flags = p
20242 45 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 E->flags & EP_Ex
20243 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 pCollate;.
20244 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c sqlite3ExprDel
20245 65 74 65 28 64 62 2c 20 70 45 29 3b 0a 20 20 20 ete(db, pE);.
20246 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 pItem->pExp
20247 72 20 3d 20 70 45 20 3d 20 73 71 6c 69 74 65 33 r = pE = sqlite3
20248 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 4e 54 45 Expr(db, TK_INTE
20249 47 45 52 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 GER, 0);.
2024a 20 69 66 28 20 70 45 3d 3d 30 20 29 20 72 65 74 if( pE==0 ) ret
2024b 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 70 urn 1;. p
2024c 45 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c E->pColl = pColl
2024d 3b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 66 6c ;. pE->fl
2024e 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c ags |= EP_IntVal
2024f 75 65 20 7c 20 66 6c 61 67 73 3b 0a 20 20 20 20 ue | flags;.
20250 20 20 20 20 70 45 2d 3e 75 2e 69 56 61 6c 75 65 pE->u.iValue
20251 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 = iCol;.
20252 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 pItem->iCol = (
20253 75 31 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 u16)iCol;.
20254 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 pItem->done =
20255 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 1;. }else{.
20256 20 20 20 20 20 20 20 20 6d 6f 72 65 54 6f 44 6f moreToDo
20257 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 1;. }.
20258 20 20 7d 0a 20 20 20 20 70 53 65 6c 65 63 74 20 }. pSelect
20259 3d 20 70 53 65 6c 65 63 74 2d 3e 70 4e 65 78 74 = pSelect->pNext
2025a 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b ;. }. for(i=0;
2025b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 i<pOrderBy->nEx
2025c 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 pr; i++){. if
2025d 28 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d ( pOrderBy->a[i]
2025e 2e 64 6f 6e 65 3d 3d 30 20 29 7b 0a 20 20 20 20 .done==0 ){.
2025f 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
20260 67 28 70 50 61 72 73 65 2c 20 22 25 72 20 4f 52 g(pParse, "%r OR
20261 44 45 52 20 42 59 20 74 65 72 6d 20 64 6f 65 73 DER BY term does
20262 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 22 not match any "
20263 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 63 6f . "co
20264 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 lumn in the resu
20265 6c 74 20 73 65 74 22 2c 20 69 2b 31 29 3b 0a 20 lt set", i+1);.
20266 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
20267 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
20268 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 n 0;.}../*.** Ch
20269 65 63 6b 20 65 76 65 72 79 20 74 65 72 6d 20 69 eck every term i
2026a 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 6f n the ORDER BY o
2026b 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 r GROUP BY claus
2026c 65 20 70 4f 72 64 65 72 42 79 20 6f 66 0a 2a 2a e pOrderBy of.**
2026d 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 the SELECT stat
2026e 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e 20 20 ement pSelect.
2026f 49 66 20 61 6e 79 20 74 65 72 6d 20 69 73 20 72 If any term is r
20270 65 66 65 72 65 6e 63 65 20 74 6f 20 61 0a 2a 2a eference to a.**
20271 20 72 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 result set expr
20272 65 73 73 69 6f 6e 20 28 61 73 20 64 65 74 65 72 ession (as deter
20273 6d 69 6e 65 64 20 62 79 20 74 68 65 20 45 78 70 mined by the Exp
20274 72 4c 69 73 74 2e 61 2e 69 43 6f 6c 20 66 69 65 rList.a.iCol fie
20275 6c 64 29 0a 2a 2a 20 74 68 65 6e 20 63 6f 6e 76 ld).** then conv
20276 65 72 74 20 74 68 61 74 20 74 65 72 6d 20 69 6e ert that term in
20277 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 to a copy of the
20278 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 72 corresponding r
20279 65 73 75 6c 74 20 73 65 74 0a 2a 2a 20 63 6f 6c esult set.** col
2027a 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e umn..**.** If an
2027b 79 20 65 72 72 6f 72 73 20 61 72 65 20 64 65 74 y errors are det
2027c 65 63 74 65 64 2c 20 61 64 64 20 61 6e 20 65 72 ected, add an er
2027d 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 ror message to p
2027e 50 61 72 73 65 20 61 6e 64 0a 2a 2a 20 72 65 74 Parse and.** ret
2027f 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 52 urn non-zero. R
20280 65 74 75 72 6e 20 7a 65 72 6f 20 69 66 20 6e 6f eturn zero if no
20281 20 65 72 72 6f 72 73 20 61 72 65 20 73 65 65 6e errors are seen
20282 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
20283 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 ATE int sqlite3R
20284 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 esolveOrderGroup
20285 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 By(. Parse *pPa
20286 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 rse, /* P
20287 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 arsing context.
20288 20 4c 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73 Leave error mes
20289 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 sages here */.
2028a 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c Select *pSelect,
2028b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c /* The SEL
2028c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f ECT statement co
2028d 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 6c 61 ntaining the cla
2028e 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 use */. ExprLis
2028f 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f t *pOrderBy, /
20290 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 6f * The ORDER BY o
20291 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 r GROUP BY claus
20292 65 20 74 6f 20 62 65 20 70 72 6f 63 65 73 73 65 e to be processe
20293 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 d */. const cha
20294 72 20 2a 7a 54 79 70 65 20 20 20 20 20 2f 2a 20 r *zType /*
20295 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 "ORDER" or "GROU
20296 50 22 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 P" */.){. int i
20297 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
20298 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
20299 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 ExprList *pEList
2029a 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c ;. struct ExprL
2029b 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b ist_item *pItem;
2029c 0a 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 .. if( pOrderBy
2029d 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 ==0 || pParse->d
2029e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
2029f 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20 ) return 0;.#if
202a0 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d SQLITE_MAX_COLUM
202a1 4e 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 N. if( pOrderBy
202a2 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d ->nExpr>db->aLim
202a3 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
202a4 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 COLUMN] ){. s
202a5 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
202a6 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 Parse, "too many
202a7 20 74 65 72 6d 73 20 69 6e 20 25 73 20 42 59 20 terms in %s BY
202a8 63 6c 61 75 73 65 22 2c 20 7a 54 79 70 65 29 3b clause", zType);
202a9 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 . return 1;.
202aa 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 45 4c 69 }.#endif. pELi
202ab 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 st = pSelect->pE
202ac 4c 69 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 List;. assert(
202ad 70 45 4c 69 73 74 21 3d 30 20 29 3b 20 20 2f 2a pEList!=0 ); /*
202ae 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 sqlite3SelectNe
202af 77 28 29 20 67 75 61 72 61 6e 74 65 65 73 20 74 w() guarantees t
202b0 68 69 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 his */. for(i=0
202b1 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 , pItem=pOrderBy
202b2 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d ->a; i<pOrderBy-
202b3 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 >nExpr; i++, pIt
202b4 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 em++){. if( p
202b5 49 74 65 6d 2d 3e 69 43 6f 6c 20 29 7b 0a 20 20 Item->iCol ){.
202b6 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 if( pItem->i
202b7 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 Col>pEList->nExp
202b8 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73 r ){. res
202b9 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 olveOutOfRangeEr
202ba 72 6f 72 28 70 50 61 72 73 65 2c 20 7a 54 79 70 ror(pParse, zTyp
202bb 65 2c 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d 3e e, i+1, pEList->
202bc 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 nExpr);.
202bd 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 return 1;.
202be 7d 0a 20 20 20 20 20 20 72 65 73 6f 6c 76 65 41 }. resolveA
202bf 6c 69 61 73 28 70 50 61 72 73 65 2c 20 70 45 4c lias(pParse, pEL
202c0 69 73 74 2c 20 70 49 74 65 6d 2d 3e 69 43 6f 6c ist, pItem->iCol
202c1 2d 31 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 -1, pItem->pExpr
202c2 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a , zType);. }.
202c3 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
202c4 7d 0a 0a 2f 2a 0a 2a 2a 20 70 4f 72 64 65 72 42 }../*.** pOrderB
202c5 79 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 y is an ORDER BY
202c6 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 or GROUP BY cla
202c7 75 73 65 20 69 6e 20 53 45 4c 45 43 54 20 73 74 use in SELECT st
202c8 61 74 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e atement pSelect.
202c9 0a 2a 2a 20 54 68 65 20 4e 61 6d 65 20 63 6f 6e .** The Name con
202ca 74 65 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45 text of the SELE
202cb 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 CT statement is
202cc 70 4e 43 2e 20 20 7a 54 79 70 65 20 69 73 20 65 pNC. zType is e
202cd 69 74 68 65 72 0a 2a 2a 20 22 4f 52 44 45 52 22 ither.** "ORDER"
202ce 20 6f 72 20 22 47 52 4f 55 50 22 20 64 65 70 65 or "GROUP" depe
202cf 6e 64 69 6e 67 20 6f 6e 20 77 68 69 63 68 20 74 nding on which t
202d0 79 70 65 20 6f 66 20 63 6c 61 75 73 65 20 70 4f ype of clause pO
202d1 72 64 65 72 42 79 20 69 73 2e 0a 2a 2a 0a 2a 2a rderBy is..**.**
202d2 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 This routine re
202d3 73 6f 6c 76 65 73 20 65 61 63 68 20 74 65 72 6d solves each term
202d4 20 6f 66 20 74 68 65 20 63 6c 61 75 73 65 20 69 of the clause i
202d5 6e 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f nto an expressio
202d6 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6f 72 64 n..** If the ord
202d7 65 72 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e er-by term is an
202d8 20 69 6e 74 65 67 65 72 20 49 20 62 65 74 77 65 integer I betwe
202d9 65 6e 20 31 20 61 6e 64 20 4e 20 28 77 68 65 72 en 1 and N (wher
202da 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 e N is the.** nu
202db 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
202dc 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 in the result se
202dd 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 29 t of the SELECT)
202de 20 74 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 then the expres
202df 73 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 72 sion.** in the r
202e0 65 73 6f 6c 75 74 69 6f 6e 20 69 73 20 61 20 63 esolution is a c
202e1 6f 70 79 20 6f 66 20 74 68 65 20 49 2d 74 68 20 opy of the I-th
202e2 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 result-set expre
202e3 73 73 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 ssion. If.** th
202e4 65 20 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20 e order-by term
202e5 69 73 20 61 6e 20 69 64 65 6e 74 69 66 79 20 74 is an identify t
202e6 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 hat corresponds
202e7 74 6f 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f to the AS-name o
202e8 66 0a 2a 2a 20 61 20 72 65 73 75 6c 74 2d 73 65 f.** a result-se
202e9 74 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 t expression, th
202ea 65 6e 20 74 68 65 20 74 65 72 6d 20 72 65 73 6f en the term reso
202eb 6c 76 65 73 20 74 6f 20 61 20 63 6f 70 79 20 6f lves to a copy o
202ec 66 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d f the.** result-
202ed 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 set expression.
202ee 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 Otherwise, the
202ef 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 72 65 expression is re
202f0 73 6f 6c 76 65 64 20 69 6e 0a 2a 2a 20 74 68 65 solved in.** the
202f1 20 75 73 75 61 6c 20 77 61 79 20 2d 20 75 73 69 usual way - usi
202f2 6e 67 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 ng sqlite3Resolv
202f3 65 45 78 70 72 4e 61 6d 65 73 28 29 2e 0a 2a 2a eExprNames()..**
202f4 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
202f5 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
202f6 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 ber of errors.
202f7 49 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c If errors occur,
202f8 20 74 68 65 6e 0a 2a 2a 20 61 6e 20 61 70 70 72 then.** an appr
202f9 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65 opriate error me
202fa 73 73 61 67 65 20 6d 69 67 68 74 20 62 65 20 6c ssage might be l
202fb 65 66 74 20 69 6e 20 70 50 61 72 73 65 2e 20 20 eft in pParse.
202fc 28 4f 4f 4d 20 65 72 72 6f 72 73 0a 2a 2a 20 65 (OOM errors.** e
202fd 78 63 65 70 74 65 64 2e 29 0a 2a 2f 0a 73 74 61 xcepted.).*/.sta
202fe 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f tic int resolveO
202ff 72 64 65 72 47 72 6f 75 70 42 79 28 0a 20 20 4e rderGroupBy(. N
20300 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c ameContext *pNC,
20301 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 /* The name
20302 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 context of the
20303 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
20304 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 */. Select *pS
20305 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 54 elect, /* T
20306 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d he SELECT statem
20307 65 6e 74 20 68 6f 6c 64 69 6e 67 20 70 4f 72 64 ent holding pOrd
20308 65 72 42 79 20 2a 2f 0a 20 20 45 78 70 72 4c 69 erBy */. ExprLi
20309 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 st *pOrderBy,
2030a 2f 2a 20 41 6e 20 4f 52 44 45 52 20 42 59 20 6f /* An ORDER BY o
2030b 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 r GROUP BY claus
2030c 65 20 74 6f 20 72 65 73 6f 6c 76 65 20 2a 2f 0a e to resolve */.
2030d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 const char *zT
2030e 79 70 65 20 20 20 20 20 2f 2a 20 45 69 74 68 65 ype /* Eithe
2030f 72 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 r "ORDER" or "GR
20310 4f 55 50 22 2c 20 61 73 20 61 70 70 72 6f 70 72 OUP", as appropr
20311 69 61 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 iate */.){. int
20312 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
20313 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
20314 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
20315 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 int iCol;
20316 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20317 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 /* Column number
20318 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 */. struct Exp
20319 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 rList_item *pIte
2031a 6d 3b 20 20 20 2f 2a 20 41 20 74 65 72 6d 20 6f m; /* A term o
2031b 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 f the ORDER BY c
2031c 6c 61 75 73 65 20 2a 2f 0a 20 20 50 61 72 73 65 lause */. Parse
2031d 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 *pParse;
2031e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 /* Par
2031f 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a sing context */.
20320 20 20 69 6e 74 20 6e 52 65 73 75 6c 74 3b 20 20 int nResult;
20321 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20322 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 /* Number of te
20323 72 6d 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c rms in the resul
20324 74 20 73 65 74 20 2a 2f 0a 0a 20 20 69 66 28 20 t set */.. if(
20325 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 20 72 65 pOrderBy==0 ) re
20326 74 75 72 6e 20 30 3b 0a 20 20 6e 52 65 73 75 6c turn 0;. nResul
20327 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c t = pSelect->pEL
20328 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 50 ist->nExpr;. pP
20329 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 arse = pNC->pPar
2032a 73 65 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 se;. for(i=0, p
2032b 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 Item=pOrderBy->a
2032c 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 ; i<pOrderBy->nE
2032d 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b xpr; i++, pItem+
2032e 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 +){. Expr *pE
2032f 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b = pItem->pExpr;
20330 0a 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f . iCol = reso
20331 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72 73 65 lveAsName(pParse
20332 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 , pSelect->pELis
20333 74 2c 20 70 45 29 3b 0a 20 20 20 20 69 66 28 20 t, pE);. if(
20334 69 43 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20 iCol>0 ){.
20335 2f 2a 20 49 66 20 61 6e 20 41 53 2d 6e 61 6d 65 /* If an AS-name
20336 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 2c match is found,
20337 20 6d 61 72 6b 20 74 68 69 73 20 4f 52 44 45 52 mark this ORDER
20338 20 42 59 20 63 6f 6c 75 6d 6e 20 61 73 20 62 65 BY column as be
20339 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 61 20 63 ing. ** a c
2033a 6f 70 79 20 6f 66 20 74 68 65 20 69 43 6f 6c 2d opy of the iCol-
2033b 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20 63 6f th result-set co
2033c 6c 75 6d 6e 2e 20 20 54 68 65 20 73 75 62 73 65 lumn. The subse
2033d 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 20 20 quent call to.
2033e 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 52 65 ** sqlite3Re
2033f 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 solveOrderGroupB
20340 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72 74 y() will convert
20341 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
20342 74 6f 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f to a. ** co
20343 70 79 20 6f 66 20 74 68 65 20 69 43 6f 6c 2d 74 py of the iCol-t
20344 68 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 h result-set exp
20345 72 65 73 73 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 ression. */.
20346 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 pItem->iCol =
20347 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 (u16)iCol;.
20348 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d continue;. }
20349 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
2034a 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 45 ExprIsInteger(pE
2034b 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20 20 20 20 , &iCol) ){.
2034c 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 /* The ORDER B
2034d 59 20 74 65 72 6d 20 69 73 20 61 6e 20 69 6e 74 Y term is an int
2034e 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 2e 20 20 eger constant.
2034f 41 67 61 69 6e 2c 20 73 65 74 20 74 68 65 20 63 Again, set the c
20350 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a 20 6e olumn. ** n
20351 75 6d 62 65 72 20 73 6f 20 74 68 61 74 20 73 71 umber so that sq
20352 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 lite3ResolveOrde
20353 72 47 72 6f 75 70 42 79 28 29 20 77 69 6c 6c 20 rGroupBy() will
20354 63 6f 6e 76 65 72 74 20 74 68 65 0a 20 20 20 20 convert the.
20355 20 20 2a 2a 20 6f 72 64 65 72 2d 62 79 20 74 65 ** order-by te
20356 72 6d 20 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 rm to a copy of
20357 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20 65 the result-set e
20358 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 xpression */.
20359 20 20 20 69 66 28 20 69 43 6f 6c 3c 31 20 29 7b if( iCol<1 ){
2035a 0a 20 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65 . resolve
2035b 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 OutOfRangeError(
2035c 70 50 61 72 73 65 2c 20 7a 54 79 70 65 2c 20 69 pParse, zType, i
2035d 2b 31 2c 20 6e 52 65 73 75 6c 74 29 3b 0a 20 20 +1, nResult);.
2035e 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
2035f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 49 }. pI
20360 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 tem->iCol = (u16
20361 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e )iCol;. con
20362 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 tinue;. }..
20363 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 /* Otherwise,
20364 74 72 65 61 74 20 74 68 65 20 4f 52 44 45 52 20 treat the ORDER
20365 42 59 20 74 65 72 6d 20 61 73 20 61 6e 20 6f 72 BY term as an or
20366 64 69 6e 61 72 79 20 65 78 70 72 65 73 73 69 6f dinary expressio
20367 6e 20 2a 2f 0a 20 20 20 20 70 49 74 65 6d 2d 3e n */. pItem->
20368 69 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 69 66 iCol = 0;. if
20369 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 ( sqlite3Resolve
2036a 45 78 70 72 4e 61 6d 65 73 28 70 4e 43 2c 20 70 ExprNames(pNC, p
2036b 45 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 E) ){. retu
2036c 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a rn 1;. }. }.
2036d 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
2036e 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 ResolveOrderGrou
2036f 70 42 79 28 70 50 61 72 73 65 2c 20 70 53 65 6c pBy(pParse, pSel
20370 65 63 74 2c 20 70 4f 72 64 65 72 42 79 2c 20 7a ect, pOrderBy, z
20371 54 79 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Type);.}../*.**
20372 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e Resolve names in
20373 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 the SELECT stat
20374 65 6d 65 6e 74 20 70 20 61 6e 64 20 61 6c 6c 20 ement p and all
20375 6f 66 20 69 74 73 20 64 65 73 63 65 6e 64 65 6e of its descenden
20376 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ts..*/.static in
20377 74 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 53 t resolveSelectS
20378 74 65 70 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c tep(Walker *pWal
20379 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b ker, Select *p){
2037a 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a . NameContext *
2037b 70 4f 75 74 65 72 4e 43 3b 20 20 2f 2a 20 43 6f pOuterNC; /* Co
2037c 6e 74 65 78 74 20 74 68 61 74 20 63 6f 6e 74 61 ntext that conta
2037d 69 6e 73 20 74 68 69 73 20 53 45 4c 45 43 54 20 ins this SELECT
2037e 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 */. NameContext
2037f 20 73 4e 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 sNC; /*
20380 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 Name context of
20381 74 68 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 this SELECT */.
20382 20 69 6e 74 20 69 73 43 6f 6d 70 6f 75 6e 64 3b int isCompound;
20383 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
20384 20 69 66 20 70 20 69 73 20 61 20 63 6f 6d 70 6f if p is a compo
20385 75 6e 64 20 73 65 6c 65 63 74 20 2a 2f 0a 20 20 und select */.
20386 69 6e 74 20 6e 43 6f 6d 70 6f 75 6e 64 3b 20 20 int nCompound;
20387 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
20388 72 20 6f 66 20 63 6f 6d 70 6f 75 6e 64 20 74 65 r of compound te
20389 72 6d 73 20 70 72 6f 63 65 73 73 65 64 20 73 6f rms processed so
2038a 20 66 61 72 20 2a 2f 0a 20 20 50 61 72 73 65 20 far */. Parse
2038b 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 *pParse;
2038c 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e /* Parsing con
2038d 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 text */. ExprLi
2038e 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 st *pEList;
2038f 20 20 2f 2a 20 52 65 73 75 6c 74 20 73 65 74 20 /* Result set
20390 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 expression list
20391 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 */. int i;
20392 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
20393 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a Loop counter */.
20394 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f ExprList *pGro
20395 75 70 42 79 3b 20 20 20 20 20 2f 2a 20 54 68 65 upBy; /* The
20396 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 GROUP BY clause
20397 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 4c */. Select *pL
20398 65 66 74 6d 6f 73 74 3b 20 20 20 20 20 20 2f 2a eftmost; /*
20399 20 4c 65 66 74 2d 6d 6f 73 74 20 6f 66 20 53 45 Left-most of SE
2039a 4c 45 43 54 20 6f 66 20 61 20 63 6f 6d 70 6f 75 LECT of a compou
2039b 6e 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 nd */. sqlite3
2039c 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 *db;
2039d 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e /* Database conn
2039e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 0a 0a 20 20 ection */. ..
2039f 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a assert( p!=0 );.
203a0 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 if( p->selFlag
203a1 73 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 s & SF_Resolved
203a2 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 ){. return WR
203a3 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 70 C_Prune;. }. p
203a4 4f 75 74 65 72 4e 43 20 3d 20 70 57 61 6c 6b 65 OuterNC = pWalke
203a5 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 70 50 61 72 r->u.pNC;. pPar
203a6 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50 se = pWalker->pP
203a7 61 72 73 65 3b 0a 20 20 64 62 20 3d 20 70 50 61 arse;. db = pPa
203a8 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4e rse->db;.. /* N
203a9 6f 72 6d 61 6c 6c 79 20 73 71 6c 69 74 65 33 53 ormally sqlite3S
203aa 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 77 69 electExpand() wi
203ab 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 66 69 72 ll be called fir
203ac 73 74 20 61 6e 64 20 77 69 6c 6c 20 68 61 76 65 st and will have
203ad 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 65 78 . ** already ex
203ae 70 61 6e 64 65 64 20 74 68 69 73 20 53 45 4c 45 panded this SELE
203af 43 54 2e 20 20 48 6f 77 65 76 65 72 2c 20 69 66 CT. However, if
203b0 20 74 68 69 73 20 69 73 20 61 20 73 75 62 71 75 this is a subqu
203b1 65 72 79 20 77 69 74 68 69 6e 0a 20 20 2a 2a 20 ery within. **
203b2 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 73 an expression, s
203b3 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 qlite3ResolveExp
203b4 72 4e 61 6d 65 73 28 29 20 77 69 6c 6c 20 62 65 rNames() will be
203b5 20 63 61 6c 6c 65 64 20 77 69 74 68 6f 75 74 20 called without
203b6 61 0a 20 20 2a 2a 20 70 72 69 6f 72 20 63 61 6c a. ** prior cal
203b7 6c 20 74 6f 20 73 71 6c 69 74 65 33 53 65 6c 65 l to sqlite3Sele
203b8 63 74 45 78 70 61 6e 64 28 29 2e 20 20 57 68 65 ctExpand(). Whe
203b9 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c 20 n that happens,
203ba 6c 65 74 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 let. ** sqlite3
203bb 53 65 6c 65 63 74 50 72 65 70 28 29 20 64 6f 20 SelectPrep() do
203bc 61 6c 6c 20 6f 66 20 74 68 65 20 70 72 6f 63 65 all of the proce
203bd 73 73 69 6e 67 20 66 6f 72 20 74 68 69 73 20 53 ssing for this S
203be 45 4c 45 43 54 2e 0a 20 20 2a 2a 20 73 71 6c 69 ELECT.. ** sqli
203bf 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 29 20 te3SelectPrep()
203c0 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 62 6f 74 68 will invoke both
203c1 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 sqlite3SelectEx
203c2 70 61 6e 64 28 29 20 61 6e 64 0a 20 20 2a 2a 20 pand() and. **
203c3 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6e 20 this routine in
203c4 74 68 65 20 63 6f 72 72 65 63 74 20 6f 72 64 65 the correct orde
203c5 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 r.. */. if( (p
203c6 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f ->selFlags & SF_
203c7 45 78 70 61 6e 64 65 64 29 3d 3d 30 20 29 7b 0a Expanded)==0 ){.
203c8 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 sqlite3Selec
203c9 74 50 72 65 70 28 70 50 61 72 73 65 2c 20 70 2c tPrep(pParse, p,
203ca 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20 20 20 pOuterNC);.
203cb 72 65 74 75 72 6e 20 28 70 50 61 72 73 65 2d 3e return (pParse->
203cc 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c nErr || db->mall
203cd 6f 63 46 61 69 6c 65 64 29 20 3f 20 57 52 43 5f ocFailed) ? WRC_
203ce 41 62 6f 72 74 20 3a 20 57 52 43 5f 50 72 75 6e Abort : WRC_Prun
203cf 65 3b 0a 20 20 7d 0a 0a 20 20 69 73 43 6f 6d 70 e;. }.. isComp
203d0 6f 75 6e 64 20 3d 20 70 2d 3e 70 50 72 69 6f 72 ound = p->pPrior
203d1 21 3d 30 3b 0a 20 20 6e 43 6f 6d 70 6f 75 6e 64 !=0;. nCompound
203d2 20 3d 20 30 3b 0a 20 20 70 4c 65 66 74 6d 6f 73 = 0;. pLeftmos
203d3 74 20 3d 20 70 3b 0a 20 20 77 68 69 6c 65 28 20 t = p;. while(
203d4 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 p ){. assert(
203d5 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 (p->selFlags &
203d6 53 46 5f 45 78 70 61 6e 64 65 64 29 21 3d 30 20 SF_Expanded)!=0
203d7 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 );. assert( (
203d8 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 p->selFlags & SF
203d9 5f 52 65 73 6f 6c 76 65 64 29 3d 3d 30 20 29 3b _Resolved)==0 );
203da 0a 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 . p->selFlags
203db 20 7c 3d 20 53 46 5f 52 65 73 6f 6c 76 65 64 3b |= SF_Resolved;
203dc 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 .. /* Resolve
203dd 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 the expressions
203de 20 69 6e 20 74 68 65 20 4c 49 4d 49 54 20 61 6e in the LIMIT an
203df 64 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73 d OFFSET clauses
203e0 2e 20 54 68 65 73 65 0a 20 20 20 20 2a 2a 20 61 . These. ** a
203e1 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 re not allowed t
203e2 6f 20 72 65 66 65 72 20 74 6f 20 61 6e 79 20 6e o refer to any n
203e3 61 6d 65 73 2c 20 73 6f 20 70 61 73 73 20 61 6e ames, so pass an
203e4 20 65 6d 70 74 79 20 4e 61 6d 65 43 6f 6e 74 65 empty NameConte
203e5 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d xt.. */. m
203e6 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 emset(&sNC, 0, s
203e7 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 20 izeof(sNC));.
203e8 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 sNC.pParse = pP
203e9 61 72 73 65 3b 0a 20 20 20 20 69 66 28 20 73 71 arse;. if( sq
203ea 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 lite3ResolveExpr
203eb 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 Names(&sNC, p->p
203ec 4c 69 6d 69 74 29 20 7c 7c 0a 20 20 20 20 20 20 Limit) ||.
203ed 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 sqlite3Resolve
203ee 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 ExprNames(&sNC,
203ef 70 2d 3e 70 4f 66 66 73 65 74 29 20 29 7b 0a 20 p->pOffset) ){.
203f0 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f return WRC_
203f1 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a Abort;. }. .
203f2 20 20 20 20 2f 2a 20 53 65 74 20 75 70 20 74 68 /* Set up th
203f3 65 20 6c 6f 63 61 6c 20 6e 61 6d 65 2d 63 6f 6e e local name-con
203f4 74 65 78 74 20 74 6f 20 70 61 73 73 20 74 6f 20 text to pass to
203f5 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 sqlite3ResolveEx
203f6 70 72 4e 61 6d 65 73 28 29 20 74 6f 0a 20 20 20 prNames() to.
203f7 20 2a 2a 20 72 65 73 6f 6c 76 65 20 74 68 65 20 ** resolve the
203f8 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 result-set expre
203f9 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 20 20 20 20 ssion list..
203fa 2a 2f 0a 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 */. sNC.allow
203fb 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 73 4e 43 Agg = 1;. sNC
203fc 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 2d 3e 70 .pSrcList = p->p
203fd 53 72 63 3b 0a 20 20 20 20 73 4e 43 2e 70 4e 65 Src;. sNC.pNe
203fe 78 74 20 3d 20 70 4f 75 74 65 72 4e 43 3b 0a 20 xt = pOuterNC;.
203ff 20 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 . /* Resolve
20400 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 72 65 names in the re
20401 73 75 6c 74 20 73 65 74 2e 20 2a 2f 0a 20 20 20 sult set. */.
20402 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c pEList = p->pEL
20403 69 73 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ist;. assert(
20404 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 pEList!=0 );.
20405 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c for(i=0; i<pEL
20406 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 ist->nExpr; i++)
20407 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 58 {. Expr *pX
20408 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e = pEList->a[i].
20409 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66 28 pExpr;. if(
2040a 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
2040b 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 xprNames(&sNC, p
2040c 58 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 X) ){. re
2040d 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
2040e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
2040f 0a 20 20 20 20 2f 2a 20 52 65 63 75 72 73 69 76 . /* Recursiv
20410 65 6c 79 20 72 65 73 6f 6c 76 65 20 6e 61 6d 65 ely resolve name
20411 73 20 69 6e 20 61 6c 6c 20 73 75 62 71 75 65 72 s in all subquer
20412 69 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 ies. */. f
20413 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 70 53 72 or(i=0; i<p->pSr
20414 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 c->nSrc; i++){.
20415 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c struct SrcL
20416 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 ist_item *pItem
20417 3d 20 26 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 5d = &p->pSrc->a[i]
20418 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 ;. if( pIte
20419 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 m->pSelect ){.
2041a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
2041b 20 2a 7a 53 61 76 65 64 43 6f 6e 74 65 78 74 20 *zSavedContext
2041c 3d 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 = pParse->zAuthC
2041d 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20 ontext;.
2041e 69 66 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 if( pItem->zName
2041f 20 29 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 ) pParse->zAuth
20420 43 6f 6e 74 65 78 74 20 3d 20 70 49 74 65 6d 2d Context = pItem-
20421 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 >zName;.
20422 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 sqlite3ResolveSe
20423 6c 65 63 74 4e 61 6d 65 73 28 70 50 61 72 73 65 lectNames(pParse
20424 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 , pItem->pSelect
20425 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20 20 , pOuterNC);.
20426 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 pParse->zAu
20427 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 53 61 76 thContext = zSav
20428 65 64 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 edContext;.
20429 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e if( pParse->n
2042a 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f Err || db->mallo
2042b 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e cFailed ) return
2042c 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 WRC_Abort;.
2042d 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 }. }. .
2042e 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 /* If there are
2042f 20 6e 6f 20 61 67 67 72 65 67 61 74 65 20 66 75 no aggregate fu
20430 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 72 nctions in the r
20431 65 73 75 6c 74 2d 73 65 74 2c 20 61 6e 64 20 6e esult-set, and n
20432 6f 20 47 52 4f 55 50 20 42 59 20 0a 20 20 20 20 o GROUP BY .
20433 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 64 ** expression, d
20434 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 67 67 72 o not allow aggr
20435 65 67 61 74 65 73 20 69 6e 20 61 6e 79 20 6f 66 egates in any of
20436 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 72 65 the other expre
20437 73 73 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 ssions.. */.
20438 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73 assert( (p->s
20439 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 41 67 67 elFlags & SF_Agg
2043a 72 65 67 61 74 65 29 3d 3d 30 20 29 3b 0a 20 20 regate)==0 );.
2043b 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e pGroupBy = p->
2043c 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 69 66 pGroupBy;. if
2043d 28 20 70 47 72 6f 75 70 42 79 20 7c 7c 20 73 4e ( pGroupBy || sN
2043e 43 2e 68 61 73 41 67 67 20 29 7b 0a 20 20 20 20 C.hasAgg ){.
2043f 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d p->selFlags |=
20440 20 53 46 5f 41 67 67 72 65 67 61 74 65 3b 0a 20 SF_Aggregate;.
20441 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
20442 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 30 sNC.allowAgg = 0
20443 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f ;. }. . /
20444 2a 20 49 66 20 61 20 48 41 56 49 4e 47 20 63 6c * If a HAVING cl
20445 61 75 73 65 20 69 73 20 70 72 65 73 65 6e 74 2c ause is present,
20446 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75 73 74 then there must
20447 20 62 65 20 61 20 47 52 4f 55 50 20 42 59 20 63 be a GROUP BY c
20448 6c 61 75 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 lause.. */.
20449 20 20 69 66 28 20 70 2d 3e 70 48 61 76 69 6e 67 if( p->pHaving
2044a 20 26 26 20 21 70 47 72 6f 75 70 42 79 20 29 7b && !pGroupBy ){
2044b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
2044c 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
2044d 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 a GROUP BY claus
2044e 65 20 69 73 20 72 65 71 75 69 72 65 64 20 62 65 e is required be
2044f 66 6f 72 65 20 48 41 56 49 4e 47 22 29 3b 0a 20 fore HAVING");.
20450 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f return WRC_
20451 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a Abort;. }. .
20452 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 65 /* Add the e
20453 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 74 xpression list t
20454 6f 20 74 68 65 20 6e 61 6d 65 2d 63 6f 6e 74 65 o the name-conte
20455 78 74 20 62 65 66 6f 72 65 20 70 61 72 73 69 6e xt before parsin
20456 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f 74 68 g the. ** oth
20457 65 72 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 er expressions i
20458 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 n the SELECT sta
20459 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20 tement. This is
2045a 73 6f 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 65 so that. ** e
2045b 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 xpressions in th
2045c 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 28 e WHERE clause (
2045d 65 74 63 2e 29 20 63 61 6e 20 72 65 66 65 72 20 etc.) can refer
2045e 74 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 20 62 to expressions b
2045f 79 0a 20 20 20 20 2a 2a 20 61 6c 69 61 73 65 73 y. ** aliases
20460 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
20461 65 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a et.. **. *
20462 2a 20 4d 69 6e 6f 72 20 70 6f 69 6e 74 3a 20 49 * Minor point: I
20463 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61 f this is the ca
20464 73 65 2c 20 74 68 65 6e 20 74 68 65 20 65 78 70 se, then the exp
20465 72 65 73 73 69 6f 6e 20 77 69 6c 6c 20 62 65 0a ression will be.
20466 20 20 20 20 2a 2a 20 72 65 2d 65 76 61 6c 75 61 ** re-evalua
20467 74 65 64 20 66 6f 72 20 65 61 63 68 20 72 65 66 ted for each ref
20468 65 72 65 6e 63 65 20 74 6f 20 69 74 2e 0a 20 20 erence to it..
20469 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 45 4c */. sNC.pEL
2046a 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b ist = p->pEList;
2046b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
2046c 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 ResolveExprNames
2046d 28 26 73 4e 43 2c 20 70 2d 3e 70 57 68 65 72 65 (&sNC, p->pWhere
2046e 29 20 7c 7c 0a 20 20 20 20 20 20 20 73 71 6c 69 ) ||. sqli
2046f 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 te3ResolveExprNa
20470 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 48 61 mes(&sNC, p->pHa
20471 76 69 6e 67 29 0a 20 20 20 20 29 7b 0a 20 20 20 ving). ){.
20472 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 return WRC_Ab
20473 6f 72 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 ort;. }..
20474 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 /* The ORDER BY
20475 61 6e 64 20 47 52 4f 55 50 20 42 59 20 63 6c 61 and GROUP BY cla
20476 75 73 65 73 20 6d 61 79 20 6e 6f 74 20 72 65 66 uses may not ref
20477 65 72 20 74 6f 20 74 65 72 6d 73 20 69 6e 0a 20 er to terms in.
20478 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 65 72 ** outer quer
20479 69 65 73 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ies . */.
2047a 73 4e 43 2e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 sNC.pNext = 0;.
2047b 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 sNC.allowAgg
2047c 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f = 1;.. /* Pro
2047d 63 65 73 73 20 74 68 65 20 4f 52 44 45 52 20 42 cess the ORDER B
2047e 59 20 63 6c 61 75 73 65 20 66 6f 72 20 73 69 6e Y clause for sin
2047f 67 6c 65 74 6f 6e 20 53 45 4c 45 43 54 20 73 74 gleton SELECT st
20480 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2a atements.. **
20481 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c The ORDER BY cl
20482 61 75 73 65 20 66 6f 72 20 63 6f 6d 70 6f 75 6e ause for compoun
20483 64 73 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d ds SELECT statem
20484 65 6e 74 73 20 69 73 20 68 61 6e 64 6c 65 64 0a ents is handled.
20485 20 20 20 20 2a 2a 20 62 65 6c 6f 77 2c 20 61 66 ** below, af
20486 74 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 72 ter all of the r
20487 65 73 75 6c 74 2d 73 65 74 73 20 66 6f 72 20 61 esult-sets for a
20488 6c 6c 20 6f 66 20 74 68 65 20 65 6c 65 6d 65 6e ll of the elemen
20489 74 73 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 ts of. ** the
2048a 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65 20 62 compound have b
2048b 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20 een resolved..
2048c 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 73 */. if( !is
2048d 43 6f 6d 70 6f 75 6e 64 20 26 26 20 72 65 73 6f Compound && reso
2048e 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 lveOrderGroupBy(
2048f 26 73 4e 43 2c 20 70 2c 20 70 2d 3e 70 4f 72 64 &sNC, p, p->pOrd
20490 65 72 42 79 2c 20 22 4f 52 44 45 52 22 29 20 29 erBy, "ORDER") )
20491 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 {. return W
20492 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a RC_Abort;. }.
20493 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c if( db->mall
20494 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
20495 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f return WRC_Abo
20496 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 rt;. }. .
20497 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 /* Resolve the
20498 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e GROUP BY clause.
20499 20 20 41 74 20 74 68 65 20 73 61 6d 65 20 74 69 At the same ti
2049a 6d 65 2c 20 6d 61 6b 65 20 73 75 72 65 20 0a 20 me, make sure .
2049b 20 20 20 2a 2a 20 74 68 65 20 47 52 4f 55 50 20 ** the GROUP
2049c 42 59 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e BY clause does n
2049d 6f 74 20 63 6f 6e 74 61 69 6e 20 61 67 67 72 65 ot contain aggre
2049e 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a gate functions..
2049f 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
204a0 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20 GroupBy ){.
204a1 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 struct ExprList
204a2 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 _item *pItem;.
204a3 20 20 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 . if( res
204a4 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 olveOrderGroupBy
204a5 28 26 73 4e 43 2c 20 70 2c 20 70 47 72 6f 75 70 (&sNC, p, pGroup
204a6 42 79 2c 20 22 47 52 4f 55 50 22 29 20 7c 7c 20 By, "GROUP") ||
204a7 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
204a8 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
204a9 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
204aa 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 }. for(
204ab 69 3d 30 2c 20 70 49 74 65 6d 3d 70 47 72 6f 75 i=0, pItem=pGrou
204ac 70 42 79 2d 3e 61 3b 20 69 3c 70 47 72 6f 75 70 pBy->a; i<pGroup
204ad 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 By->nExpr; i++,
204ae 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 pItem++){.
204af 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f if( ExprHasPro
204b0 70 65 72 74 79 28 70 49 74 65 6d 2d 3e 70 45 78 perty(pItem->pEx
204b1 70 72 2c 20 45 50 5f 41 67 67 29 20 29 7b 0a 20 pr, EP_Agg) ){.
204b2 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
204b3 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
204b4 20 22 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 "aggregate func
204b5 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61 6c tions are not al
204b6 6c 6f 77 65 64 20 69 6e 20 22 0a 20 20 20 20 20 lowed in ".
204b7 20 20 20 20 20 20 20 20 20 22 74 68 65 20 47 52 "the GR
204b8 4f 55 50 20 42 59 20 63 6c 61 75 73 65 22 29 3b OUP BY clause");
204b9 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
204ba 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 n WRC_Abort;.
204bb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
204bc 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 76 }.. /* Adv
204bd 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78 74 ance to the next
204be 20 74 65 72 6d 20 6f 66 20 74 68 65 20 63 6f 6d term of the com
204bf 70 6f 75 6e 64 0a 20 20 20 20 2a 2f 0a 20 20 20 pound. */.
204c0 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a p = p->pPrior;.
204c1 20 20 20 20 6e 43 6f 6d 70 6f 75 6e 64 2b 2b 3b nCompound++;
204c2 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c . }.. /* Resol
204c3 76 65 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 ve the ORDER BY
204c4 6f 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 on a compound SE
204c5 4c 45 43 54 20 61 66 74 65 72 20 61 6c 6c 20 74 LECT after all t
204c6 65 72 6d 73 20 6f 66 0a 20 20 2a 2a 20 74 68 65 erms of. ** the
204c7 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65 20 62 compound have b
204c8 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20 een resolved..
204c9 2a 2f 0a 20 20 69 66 28 20 69 73 43 6f 6d 70 6f */. if( isCompo
204ca 75 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 43 6f und && resolveCo
204cb 6d 70 6f 75 6e 64 4f 72 64 65 72 42 79 28 70 50 mpoundOrderBy(pP
204cc 61 72 73 65 2c 20 70 4c 65 66 74 6d 6f 73 74 29 arse, pLeftmost)
204cd 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 ){. return W
204ce 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 RC_Abort;. }..
204cf 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e return WRC_Prun
204d0 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 e;.}../*.** This
204d1 20 72 6f 75 74 69 6e 65 20 77 61 6c 6b 73 20 61 routine walks a
204d2 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 n expression tre
204d3 65 20 61 6e 64 20 72 65 73 6f 6c 76 65 73 20 72 e and resolves r
204d4 65 66 65 72 65 6e 63 65 73 20 74 6f 0a 2a 2a 20 eferences to.**
204d5 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 61 6e table columns an
204d6 64 20 72 65 73 75 6c 74 2d 73 65 74 20 63 6f 6c d result-set col
204d7 75 6d 6e 73 2e 20 20 41 74 20 74 68 65 20 73 61 umns. At the sa
204d8 6d 65 20 74 69 6d 65 2c 20 64 6f 20 65 72 72 6f me time, do erro
204d9 72 0a 2a 2a 20 63 68 65 63 6b 69 6e 67 20 6f 6e r.** checking on
204da 20 66 75 6e 63 74 69 6f 6e 20 75 73 61 67 65 20 function usage
204db 61 6e 64 20 73 65 74 20 61 20 66 6c 61 67 20 69 and set a flag i
204dc 66 20 61 6e 79 20 61 67 67 72 65 67 61 74 65 20 f any aggregate
204dd 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 functions.** are
204de 20 73 65 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 seen..**.** To
204df 72 65 73 6f 6c 76 65 20 74 61 62 6c 65 20 63 6f resolve table co
204e0 6c 75 6d 6e 73 20 72 65 66 65 72 65 6e 63 65 73 lumns references
204e1 20 77 65 20 6c 6f 6f 6b 20 66 6f 72 20 6e 6f 64 we look for nod
204e2 65 73 20 28 6f 72 20 73 75 62 74 72 65 65 73 29 es (or subtrees)
204e3 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 6f 72 6d of the .** form
204e4 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 6f 72 X.Y.Z or Y.Z or
204e5 20 6a 75 73 74 20 5a 20 77 68 65 72 65 0a 2a 2a just Z where.**
204e6 0a 2a 2a 20 20 20 20 20 20 58 3a 20 20 20 54 68 .** X: Th
204e7 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61 e name of a data
204e8 62 61 73 65 2e 20 20 45 78 3a 20 20 22 6d 61 69 base. Ex: "mai
204e9 6e 22 20 6f 72 20 22 74 65 6d 70 22 20 6f 72 0a n" or "temp" or.
204ea 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 68 65 ** the
204eb 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 61 symbolic name a
204ec 73 73 69 67 6e 65 64 20 74 6f 20 61 6e 20 41 54 ssigned to an AT
204ed 54 41 43 48 2d 65 64 20 64 61 74 61 62 61 73 65 TACH-ed database
204ee 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 3a 20 ..**.** Y:
204ef 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 The name of a
204f0 74 61 62 6c 65 20 69 6e 20 61 20 46 52 4f 4d 20 table in a FROM
204f1 63 6c 61 75 73 65 2e 20 20 4f 72 20 69 6e 20 61 clause. Or in a
204f2 20 74 72 69 67 67 65 72 0a 2a 2a 20 20 20 20 20 trigger.**
204f3 20 20 20 20 20 20 6f 6e 65 20 6f 66 20 74 68 65 one of the
204f4 20 73 70 65 63 69 61 6c 20 6e 61 6d 65 73 20 22 special names "
204f5 6f 6c 64 22 20 6f 72 20 22 6e 65 77 22 2e 0a 2a old" or "new"..*
204f6 2a 0a 2a 2a 20 20 20 20 20 20 5a 3a 20 20 20 54 *.** Z: T
204f7 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c he name of a col
204f8 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20 59 2e 0a umn in table Y..
204f9 2a 2a 0a 2a 2a 20 54 68 65 20 6e 6f 64 65 20 61 **.** The node a
204fa 74 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 t the root of th
204fb 65 20 73 75 62 74 72 65 65 20 69 73 20 6d 6f 64 e subtree is mod
204fc 69 66 69 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 ified as follows
204fd 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 45 78 70 72 2e :.**.** Expr.
204fe 6f 70 20 20 20 20 20 20 20 20 43 68 61 6e 67 65 op Change
204ff 64 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 0a 2a d to TK_COLUMN.*
20500 2a 20 20 20 20 45 78 70 72 2e 70 54 61 62 20 20 * Expr.pTab
20501 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20 74 68 Points to th
20502 65 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 66 e Table object f
20503 6f 72 20 58 2e 59 0a 2a 2a 20 20 20 20 45 78 70 or X.Y.** Exp
20504 72 2e 69 43 6f 6c 75 6d 6e 20 20 20 54 68 65 20 r.iColumn The
20505 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 69 6e 20 column index in
20506 58 2e 59 2e 20 20 2d 31 20 66 6f 72 20 74 68 65 X.Y. -1 for the
20507 20 72 6f 77 69 64 2e 0a 2a 2a 20 20 20 20 45 78 rowid..** Ex
20508 70 72 2e 69 54 61 62 6c 65 20 20 20 20 54 68 65 pr.iTable The
20509 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d VDBE cursor num
2050a 62 65 72 20 66 6f 72 20 58 2e 59 0a 2a 2a 0a 2a ber for X.Y.**.*
2050b 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 65 20 *.** To resolve
2050c 72 65 73 75 6c 74 2d 73 65 74 20 72 65 66 65 72 result-set refer
2050d 65 6e 63 65 73 2c 20 6c 6f 6f 6b 20 66 6f 72 20 ences, look for
2050e 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 73 expression nodes
2050f 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d 20 of the.** form
20510 5a 20 28 77 69 74 68 20 6e 6f 20 58 20 61 6e 64 Z (with no X and
20511 20 59 20 70 72 65 66 69 78 29 20 77 68 65 72 65 Y prefix) where
20512 20 74 68 65 20 5a 20 6d 61 74 63 68 65 73 20 74 the Z matches t
20513 68 65 20 72 69 67 68 74 2d 68 61 6e 64 0a 2a 2a he right-hand.**
20514 20 73 69 7a 65 20 6f 66 20 61 6e 20 41 53 20 63 size of an AS c
20515 6c 61 75 73 65 20 69 6e 20 74 68 65 20 72 65 73 lause in the res
20516 75 6c 74 2d 73 65 74 20 6f 66 20 61 20 53 45 4c ult-set of a SEL
20517 45 43 54 2e 20 20 54 68 65 20 5a 20 65 78 70 72 ECT. The Z expr
20518 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 72 65 70 ession.** is rep
20519 6c 61 63 65 64 20 62 79 20 61 20 63 6f 70 79 20 laced by a copy
2051a 6f 66 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64 of the left-hand
2051b 20 73 69 64 65 20 6f 66 20 74 68 65 20 72 65 73 side of the res
2051c 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 ult-set expressi
2051d 6f 6e 2e 0a 2a 2a 20 54 61 62 6c 65 2d 6e 61 6d on..** Table-nam
2051e 65 20 61 6e 64 20 66 75 6e 63 74 69 6f 6e 20 72 e and function r
2051f 65 73 6f 6c 75 74 69 6f 6e 20 6f 63 63 75 72 73 esolution occurs
20520 20 6f 6e 20 74 68 65 20 73 75 62 73 74 69 74 75 on the substitu
20521 74 65 64 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a ted expression.*
20522 2a 20 74 72 65 65 2e 20 20 46 6f 72 20 65 78 61 * tree. For exa
20523 6d 70 6c 65 2c 20 69 6e 3a 0a 2a 2a 0a 2a 2a 20 mple, in:.**.**
20524 20 20 20 20 20 53 45 4c 45 43 54 20 61 2b 62 20 SELECT a+b
20525 41 53 20 78 2c 20 63 2b 64 20 41 53 20 79 20 46 AS x, c+d AS y F
20526 52 4f 4d 20 74 31 20 4f 52 44 45 52 20 42 59 20 ROM t1 ORDER BY
20527 78 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 78 22 x;.**.** The "x"
20528 20 74 65 72 6d 20 6f 66 20 74 68 65 20 6f 72 64 term of the ord
20529 65 72 20 62 79 20 69 73 20 72 65 70 6c 61 63 65 er by is replace
2052a 64 20 62 79 20 22 61 2b 62 22 20 74 6f 20 72 65 d by "a+b" to re
2052b 6e 64 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 nder:.**.**
2052c 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 SELECT a+b AS x
2052d 2c 20 63 2b 64 20 41 53 20 79 20 46 52 4f 4d 20 , c+d AS y FROM
2052e 74 31 20 4f 52 44 45 52 20 42 59 20 61 2b 62 3b t1 ORDER BY a+b;
2052f 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 .**.** Function
20530 63 61 6c 6c 73 20 61 72 65 20 63 68 65 63 6b 65 calls are checke
20531 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 d to make sure t
20532 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e hat the function
20533 20 69 73 20 0a 2a 2a 20 64 65 66 69 6e 65 64 20 is .** defined
20534 61 6e 64 20 74 68 61 74 20 74 68 65 20 63 6f 72 and that the cor
20535 72 65 63 74 20 6e 75 6d 62 65 72 20 6f 66 20 61 rect number of a
20536 72 67 75 6d 65 6e 74 73 20 61 72 65 20 73 70 65 rguments are spe
20537 63 69 66 69 65 64 2e 0a 2a 2a 20 49 66 20 74 68 cified..** If th
20538 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6e e function is an
20539 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
2053a 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 70 4e ion, then the pN
2053b 43 2d 3e 68 61 73 41 67 67 20 69 73 0a 2a 2a 20 C->hasAgg is.**
2053c 73 65 74 20 61 6e 64 20 74 68 65 20 6f 70 63 6f set and the opco
2053d 64 65 20 69 73 20 63 68 61 6e 67 65 64 20 66 72 de is changed fr
2053e 6f 6d 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 74 om TK_FUNCTION t
2053f 6f 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f o TK_AGG_FUNCTIO
20540 4e 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 78 70 72 N..** If an expr
20541 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 ession contains
20542 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
20543 6f 6e 73 20 74 68 65 6e 20 74 68 65 20 45 50 5f ons then the EP_
20544 41 67 67 0a 2a 2a 20 70 72 6f 70 65 72 74 79 20 Agg.** property
20545 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f on the expressio
20546 6e 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 n is set..**.**
20547 41 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 An error message
20548 20 69 73 20 6c 65 66 74 20 69 6e 20 70 50 61 72 is left in pPar
20549 73 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 69 se if anything i
2054a 73 20 61 6d 69 73 73 2e 20 20 54 68 65 20 6e 75 s amiss. The nu
2054b 6d 62 65 72 0a 2a 2a 20 69 66 20 65 72 72 6f 72 mber.** if error
2054c 73 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a s is returned..*
2054d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
2054e 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f int sqlite3Reso
2054f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 20 0a 20 lveExprNames( .
20550 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e NameContext *pN
20551 43 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 C, /* Name
20552 73 70 61 63 65 20 74 6f 20 72 65 73 6f 6c 76 65 space to resolve
20553 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 2e expressions in.
20554 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 */. Expr *pExp
20555 72 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a r /*
20556 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 The expression
20557 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e 20 to be analyzed.
20558 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 73 61 76 65 */.){. int save
20559 64 48 61 73 41 67 67 3b 0a 20 20 57 61 6c 6b 65 dHasAgg;. Walke
2055a 72 20 77 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 r w;.. if( pExp
2055b 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b r==0 ) return 0;
2055c 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f .#if SQLITE_MAX_
2055d 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 7b EXPR_DEPTH>0. {
2055e 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50 61 72 . Parse *pPar
2055f 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 se = pNC->pParse
20560 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
20561 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 3ExprCheckHeight
20562 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
20563 6e 48 65 69 67 68 74 2b 70 4e 43 2d 3e 70 50 61 nHeight+pNC->pPa
20564 72 73 65 2d 3e 6e 48 65 69 67 68 74 29 20 29 7b rse->nHeight) ){
20565 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
20566 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 . }. pPars
20567 65 2d 3e 6e 48 65 69 67 68 74 20 2b 3d 20 70 45 e->nHeight += pE
20568 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 20 xpr->nHeight;.
20569 7d 0a 23 65 6e 64 69 66 0a 20 20 73 61 76 65 64 }.#endif. saved
2056a 48 61 73 41 67 67 20 3d 20 70 4e 43 2d 3e 68 61 HasAgg = pNC->ha
2056b 73 41 67 67 3b 0a 20 20 70 4e 43 2d 3e 68 61 73 sAgg;. pNC->has
2056c 41 67 67 20 3d 20 30 3b 0a 20 20 77 2e 78 45 78 Agg = 0;. w.xEx
2056d 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 prCallback = res
2056e 6f 6c 76 65 45 78 70 72 53 74 65 70 3b 0a 20 20 olveExprStep;.
2056f 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 w.xSelectCallbac
20570 6b 20 3d 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 k = resolveSelec
20571 74 53 74 65 70 3b 0a 20 20 77 2e 70 50 61 72 73 tStep;. w.pPars
20572 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b e = pNC->pParse;
20573 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4e 43 . w.u.pNC = pNC
20574 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 ;. sqlite3WalkE
20575 78 70 72 28 26 77 2c 20 70 45 78 70 72 29 3b 0a xpr(&w, pExpr);.
20576 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 #if SQLITE_MAX_E
20577 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 70 4e XPR_DEPTH>0. pN
20578 43 2d 3e 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 C->pParse->nHeig
20579 68 74 20 2d 3d 20 70 45 78 70 72 2d 3e 6e 48 65 ht -= pExpr->nHe
2057a 69 67 68 74 3b 0a 23 65 6e 64 69 66 0a 20 20 69 ight;.#endif. i
2057b 66 28 20 70 4e 43 2d 3e 6e 45 72 72 3e 30 20 7c f( pNC->nErr>0 |
2057c 7c 20 77 2e 70 50 61 72 73 65 2d 3e 6e 45 72 72 | w.pParse->nErr
2057d 3e 30 20 29 7b 0a 20 20 20 20 45 78 70 72 53 65 >0 ){. ExprSe
2057e 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c tProperty(pExpr,
2057f 20 45 50 5f 45 72 72 6f 72 29 3b 0a 20 20 7d 0a EP_Error);. }.
20580 20 20 69 66 28 20 70 4e 43 2d 3e 68 61 73 41 67 if( pNC->hasAg
20581 67 20 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74 g ){. ExprSet
20582 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
20583 45 50 5f 41 67 67 29 3b 0a 20 20 7d 65 6c 73 65 EP_Agg);. }else
20584 20 69 66 28 20 73 61 76 65 64 48 61 73 41 67 67 if( savedHasAgg
20585 20 29 7b 0a 20 20 20 20 70 4e 43 2d 3e 68 61 73 ){. pNC->has
20586 41 67 67 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 Agg = 1;. }. r
20587 65 74 75 72 6e 20 45 78 70 72 48 61 73 50 72 6f eturn ExprHasPro
20588 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
20589 45 72 72 6f 72 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a Error);.}.../*.*
2058a 2a 20 52 65 73 6f 6c 76 65 20 61 6c 6c 20 6e 61 * Resolve all na
2058b 6d 65 73 20 69 6e 20 61 6c 6c 20 65 78 70 72 65 mes in all expre
2058c 73 73 69 6f 6e 73 20 6f 66 20 61 20 53 45 4c 45 ssions of a SELE
2058d 43 54 20 61 6e 64 20 69 6e 20 61 6c 6c 0a 2a 2a CT and in all.**
2058e 20 64 65 63 65 6e 64 65 6e 74 73 20 6f 66 20 74 decendents of t
2058f 68 65 20 53 45 4c 45 43 54 2c 20 69 6e 63 6c 75 he SELECT, inclu
20590 64 69 6e 67 20 63 6f 6d 70 6f 75 6e 64 73 20 6f ding compounds o
20591 66 66 20 6f 66 20 70 2d 3e 70 50 72 69 6f 72 2c ff of p->pPrior,
20592 0a 2a 2a 20 73 75 62 71 75 65 72 69 65 73 20 69 .** subqueries i
20593 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 61 n expressions, a
20594 6e 64 20 73 75 62 71 75 65 72 69 65 73 20 75 73 nd subqueries us
20595 65 64 20 61 73 20 46 52 4f 4d 20 63 6c 61 75 73 ed as FROM claus
20596 65 0a 2a 2a 20 74 65 72 6d 73 2e 0a 2a 2a 0a 2a e.** terms..**.*
20597 2a 20 53 65 65 20 73 71 6c 69 74 65 33 52 65 73 * See sqlite3Res
20598 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20 olveExprNames()
20599 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 69 6f for a descriptio
2059a 6e 20 6f 66 20 74 68 65 20 6b 69 6e 64 73 20 6f n of the kinds o
2059b 66 0a 2a 2a 20 74 72 61 6e 73 66 6f 72 6d 61 74 f.** transformat
2059c 69 6f 6e 73 20 74 68 61 74 20 6f 63 63 75 72 2e ions that occur.
2059d 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 53 45 4c 45 43 .**.** All SELEC
2059e 54 20 73 74 61 74 65 6d 65 6e 74 73 20 73 68 6f T statements sho
2059f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 65 78 uld have been ex
205a0 70 61 6e 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 panded using.**
205a1 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 sqlite3SelectExp
205a2 61 6e 64 28 29 20 70 72 69 6f 72 20 74 6f 20 69 and() prior to i
205a3 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 nvoking this rou
205a4 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tine..*/.SQLITE_
205a5 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
205a6 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 ite3ResolveSelec
205a7 74 4e 61 6d 65 73 28 0a 20 20 50 61 72 73 65 20 tNames(. Parse
205a8 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 *pParse,
205a9 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 /* The parser c
205aa 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 ontext */. Sele
205ab 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 ct *p,
205ac 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 /* The SELECT
205ad 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 statement being
205ae 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 4e 61 6d coded. */. Nam
205af 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 eContext *pOuter
205b0 4e 43 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 NC /* Name cont
205b1 65 78 74 20 66 6f 72 20 70 61 72 65 6e 74 20 53 ext for parent S
205b2 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 ELECT statement
205b3 2a 2f 0a 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 */.){. Walker w
205b4 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d ;.. assert( p!=
205b5 30 20 29 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 0 );. w.xExprCa
205b6 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 llback = resolve
205b7 45 78 70 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 ExprStep;. w.xS
205b8 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 electCallback =
205b9 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 resolveSelectSte
205ba 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 p;. w.pParse =
205bb 70 50 61 72 73 65 3b 0a 20 20 77 2e 75 2e 70 4e pParse;. w.u.pN
205bc 43 20 3d 20 70 4f 75 74 65 72 4e 43 3b 0a 20 20 C = pOuterNC;.
205bd 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 sqlite3WalkSelec
205be 74 28 26 77 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 2a t(&w, p);.}../**
205bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
205c0 20 6f 66 20 72 65 73 6f 6c 76 65 2e 63 20 2a 2a of resolve.c **
205c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
205c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
205c5 69 6e 20 66 69 6c 65 20 65 78 70 72 2e 63 20 2a in file expr.c *
205c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
205c9 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 ** 2001 Septembe
205ca 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 15.**.** The a
205cb 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
205cc 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
205cd 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
205ce 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
205cf 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
205d0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
205d1 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
205d2 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
205d3 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
205d4 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
205d5 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
205d6 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
205d7 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
205d8 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
205d9 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
205da 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
205db 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
205dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205e0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
205e1 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69 6e contains routin
205e2 65 73 20 75 73 65 64 20 66 6f 72 20 61 6e 61 6c es used for anal
205e3 79 7a 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e yzing expression
205e4 73 20 61 6e 64 0a 2a 2a 20 66 6f 72 20 67 65 6e s and.** for gen
205e5 65 72 61 74 69 6e 67 20 56 44 42 45 20 63 6f 64 erating VDBE cod
205e6 65 20 74 68 61 74 20 65 76 61 6c 75 61 74 65 73 e that evaluates
205e7 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 expressions in
205e8 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a SQLite..*/../*.*
205e9 2a 20 52 65 74 75 72 6e 20 74 68 65 20 27 61 66 * Return the 'af
205ea 66 69 6e 69 74 79 27 20 6f 66 20 74 68 65 20 65 finity' of the e
205eb 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 xpression pExpr
205ec 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 if any..**.** If
205ed 20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6c 75 pExpr is a colu
205ee 6d 6e 2c 20 61 20 72 65 66 65 72 65 6e 63 65 20 mn, a reference
205ef 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 76 69 61 20 to a column via
205f0 61 6e 20 27 41 53 27 20 61 6c 69 61 73 2c 0a 2a an 'AS' alias,.*
205f1 2a 20 6f 72 20 61 20 73 75 62 2d 73 65 6c 65 63 * or a sub-selec
205f2 74 20 77 69 74 68 20 61 20 63 6f 6c 75 6d 6e 20 t with a column
205f3 61 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 as the return va
205f4 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a lue, then the .*
205f5 2a 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 * affinity of th
205f6 61 74 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 at column is ret
205f7 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 urned. Otherwise
205f8 2c 20 30 78 30 30 20 69 73 20 72 65 74 75 72 6e , 0x00 is return
205f9 65 64 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e ed,.** indicatin
205fa 67 20 6e 6f 20 61 66 66 69 6e 69 74 79 20 66 6f g no affinity fo
205fb 72 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e r the expression
205fc 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 ..**.** i.e. the
205fd 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 65 78 WHERE clause ex
205fe 70 72 65 73 73 73 69 6f 6e 73 20 69 6e 20 74 68 presssions in th
205ff 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 e following stat
20600 65 6d 65 6e 74 73 20 61 6c 6c 0a 2a 2a 20 68 61 ements all.** ha
20601 76 65 20 61 6e 20 61 66 66 69 6e 69 74 79 3a 0a ve an affinity:.
20602 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 **.** CREATE TAB
20603 4c 45 20 74 31 28 61 29 3b 0a 2a 2a 20 53 45 4c LE t1(a);.** SEL
20604 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 ECT * FROM t1 WH
20605 45 52 45 20 61 3b 0a 2a 2a 20 53 45 4c 45 43 54 ERE a;.** SELECT
20606 20 61 20 41 53 20 62 20 46 52 4f 4d 20 74 31 20 a AS b FROM t1
20607 57 48 45 52 45 20 62 3b 0a 2a 2a 20 53 45 4c 45 WHERE b;.** SELE
20608 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 CT * FROM t1 WHE
20609 52 45 20 28 73 65 6c 65 63 74 20 61 20 66 72 6f RE (select a fro
2060a 6d 20 74 31 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 m t1);.*/.SQLITE
2060b 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 _PRIVATE char sq
2060c 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 lite3ExprAffinit
2060d 79 28 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a y(Expr *pExpr){.
2060e 20 20 69 6e 74 20 6f 70 20 3d 20 70 45 78 70 72 int op = pExpr
2060f 2d 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d ->op;. if( op==
20610 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 TK_SELECT ){.
20611 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
20612 66 6c 61 67 73 26 45 50 5f 78 49 73 53 65 6c 65 flags&EP_xIsSele
20613 63 74 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ct );. return
20614 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 sqlite3ExprAffi
20615 6e 69 74 79 28 70 45 78 70 72 2d 3e 78 2e 70 53 nity(pExpr->x.pS
20616 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 61 elect->pEList->a
20617 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 7d 0a [0].pExpr);. }.
20618 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
20619 4d 49 54 5f 43 41 53 54 0a 20 20 69 66 28 20 6f MIT_CAST. if( o
2061a 70 3d 3d 54 4b 5f 43 41 53 54 20 29 7b 0a 20 20 p==TK_CAST ){.
2061b 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 assert( !ExprH
2061c 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 asProperty(pExpr
2061d 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 , EP_IntValue) )
2061e 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c ;. return sql
2061f 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 ite3AffinityType
20620 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e (pExpr->u.zToken
20621 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 );. }.#endif.
20622 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 41 47 47 5f if( (op==TK_AGG_
20623 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b COLUMN || op==TK
20624 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 _COLUMN || op==T
20625 4b 5f 52 45 47 49 53 54 45 52 29 20 0a 20 20 20 K_REGISTER) .
20626 26 26 20 70 45 78 70 72 2d 3e 70 54 61 62 21 3d && pExpr->pTab!=
20627 30 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20 6f 70 0. ){. /* op
20628 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 26 26 ==TK_REGISTER &&
20629 20 70 45 78 70 72 2d 3e 70 54 61 62 21 3d 30 20 pExpr->pTab!=0
2062a 68 61 70 70 65 6e 73 20 77 68 65 6e 20 70 45 78 happens when pEx
2062b 70 72 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c pr was originall
2062c 79 0a 20 20 20 20 2a 2a 20 61 20 54 4b 5f 43 4f y. ** a TK_CO
2062d 4c 55 4d 4e 20 62 75 74 20 77 61 73 20 70 72 65 LUMN but was pre
2062e 76 69 6f 75 73 6c 79 20 65 76 61 6c 75 61 74 65 viously evaluate
2062f 64 20 61 6e 64 20 63 61 63 68 65 64 20 69 6e 20 d and cached in
20630 61 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 a register */.
20631 20 20 69 6e 74 20 6a 20 3d 20 70 45 78 70 72 2d int j = pExpr-
20632 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 69 66 >iColumn;. if
20633 28 20 6a 3c 30 20 29 20 72 65 74 75 72 6e 20 53 ( j<0 ) return S
20634 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 QLITE_AFF_INTEGE
20635 52 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 R;. assert( p
20636 45 78 70 72 2d 3e 70 54 61 62 20 26 26 20 6a 3c Expr->pTab && j<
20637 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e 6e 43 6f pExpr->pTab->nCo
20638 6c 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 l );. return
20639 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e 61 43 6f pExpr->pTab->aCo
2063a 6c 5b 6a 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20 l[j].affinity;.
2063b 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 45 78 70 }. return pExp
2063c 72 2d 3e 61 66 66 69 6e 69 74 79 3b 0a 7d 0a 0a r->affinity;.}..
2063d 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 6f /*.** Set the co
2063e 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
2063f 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 20 for expression
20640 70 45 78 70 72 20 74 6f 20 62 65 20 74 68 65 20 pExpr to be the
20641 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 collating.** seq
20642 75 65 6e 63 65 20 6e 61 6d 65 64 20 62 79 20 70 uence named by p
20643 54 6f 6b 65 6e 2e 20 20 20 52 65 74 75 72 6e 20 Token. Return
20644 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
20645 20 72 65 76 69 73 65 64 20 65 78 70 72 65 73 73 revised express
20646 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 63 6f 6c 6c ion..** The coll
20647 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 ating sequence i
20648 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65 78 70 s marked as "exp
20649 6c 69 63 69 74 22 20 75 73 69 6e 67 20 74 68 65 licit" using the
2064a 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 0a 2a EP_ExpCollate.*
2064b 2a 20 66 6c 61 67 2e 20 20 41 6e 20 65 78 70 6c * flag. An expl
2064c 69 63 69 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 icit collating s
2064d 65 71 75 65 6e 63 65 20 77 69 6c 6c 20 6f 76 65 equence will ove
2064e 72 72 69 64 65 20 69 6d 70 6c 69 63 69 74 0a 2a rride implicit.*
2064f 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 * collating sequ
20650 65 6e 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ences..*/.SQLITE
20651 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 _PRIVATE Expr *s
20652 71 6c 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c qlite3ExprSetCol
20653 6c 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c l(Parse *pParse,
20654 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 54 6f Expr *pExpr, To
20655 6b 65 6e 20 2a 70 43 6f 6c 6c 4e 61 6d 65 29 7b ken *pCollName){
20656 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d . char *zColl =
20657 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 0; /
20658 2a 20 44 65 71 75 6f 74 65 64 20 6e 61 6d 65 20 * Dequoted name
20659 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 of collation seq
2065a 75 65 6e 63 65 20 2a 2f 0a 20 20 43 6f 6c 6c 53 uence */. CollS
2065b 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 73 71 6c eq *pColl;. sql
2065c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
2065d 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c 6c 20 3d e->db;. zColl =
2065e 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d sqlite3NameFrom
2065f 54 6f 6b 65 6e 28 64 62 2c 20 70 43 6f 6c 6c 4e Token(db, pCollN
20660 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 45 78 70 ame);. if( pExp
20661 72 20 26 26 20 7a 43 6f 6c 6c 20 29 7b 0a 20 20 r && zColl ){.
20662 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 pColl = sqlite
20663 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 3LocateCollSeq(p
20664 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 Parse, zColl);.
20665 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a if( pColl ){.
20666 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 43 6f pExpr->pCo
20667 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 ll = pColl;.
20668 20 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c pExpr->flags |
20669 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b = EP_ExpCollate;
2066a 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c . }. }. sql
2066b 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
2066c 43 6f 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 Coll);. return
2066d 70 45 78 70 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pExpr;.}../*.**
2066e 52 65 74 75 72 6e 20 74 68 65 20 64 65 66 61 75 Return the defau
2066f 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 lt collation seq
20670 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 65 78 uence for the ex
20671 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 pression pExpr.
20672 49 66 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e If.** there is n
20673 6f 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 o default collat
20674 69 6f 6e 20 74 79 70 65 2c 20 72 65 74 75 72 6e ion type, return
20675 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 0..*/.SQLITE_PR
20676 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 IVATE CollSeq *s
20677 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 qlite3ExprCollSe
20678 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c q(Parse *pParse,
20679 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 Expr *pExpr){.
2067a 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 CollSeq *pColl
2067b 3d 20 30 3b 0a 20 20 45 78 70 72 20 2a 70 20 3d = 0;. Expr *p =
2067c 20 70 45 78 70 72 3b 0a 20 20 77 68 69 6c 65 28 pExpr;. while(
2067d 20 41 4c 57 41 59 53 28 70 29 20 29 7b 0a 20 20 ALWAYS(p) ){.
2067e 20 20 69 6e 74 20 6f 70 3b 0a 20 20 20 20 70 43 int op;. pC
2067f 6f 6c 6c 20 3d 20 70 2d 3e 70 43 6f 6c 6c 3b 0a oll = p->pColl;.
20680 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 20 if( pColl )
20681 62 72 65 61 6b 3b 0a 20 20 20 20 6f 70 20 3d 20 break;. op =
20682 70 2d 3e 6f 70 3b 0a 20 20 20 20 69 66 28 20 70 p->op;. if( p
20683 2d 3e 70 54 61 62 21 3d 30 20 26 26 20 28 0a 20 ->pTab!=0 && (.
20684 20 20 20 20 20 20 20 6f 70 3d 3d 54 4b 5f 41 47 op==TK_AG
20685 47 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d G_COLUMN || op==
20686 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d TK_COLUMN || op=
20687 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 7c 7c 20 =TK_REGISTER ||
20688 6f 70 3d 3d 54 4b 5f 54 52 49 47 47 45 52 0a 20 op==TK_TRIGGER.
20689 20 20 20 29 29 7b 0a 20 20 20 20 20 20 2f 2a 20 )){. /*
2068a 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 op==TK_REGISTER
2068b 26 26 20 70 2d 3e 70 54 61 62 21 3d 30 20 68 61 && p->pTab!=0 ha
2068c 70 70 65 6e 73 20 77 68 65 6e 20 70 45 78 70 72 ppens when pExpr
2068d 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 0a was originally.
2068e 20 20 20 20 20 20 2a 2a 20 61 20 54 4b 5f 43 4f ** a TK_CO
2068f 4c 55 4d 4e 20 62 75 74 20 77 61 73 20 70 72 65 LUMN but was pre
20690 76 69 6f 75 73 6c 79 20 65 76 61 6c 75 61 74 65 viously evaluate
20691 64 20 61 6e 64 20 63 61 63 68 65 64 20 69 6e 20 d and cached in
20692 61 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 a register */.
20693 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
20694 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 69 6e 74 zColl;. int
20695 20 6a 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3b j = p->iColumn;
20696 0a 20 20 20 20 20 20 69 66 28 20 6a 3e 3d 30 20 . if( j>=0
20697 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
20698 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
20699 3e 64 62 3b 0a 20 20 20 20 20 20 20 20 7a 43 6f >db;. zCo
2069a 6c 6c 20 3d 20 70 2d 3e 70 54 61 62 2d 3e 61 43 ll = p->pTab->aC
2069b 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 ol[j].zColl;.
2069c 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c pColl = sql
2069d 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 ite3FindCollSeq(
2069e 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a 43 6f db, ENC(db), zCo
2069f 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 ll, 0);.
206a0 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 70 pExpr->pColl = p
206a1 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Coll;. }.
206a2 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
206a3 0a 20 20 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f . if( op!=TK_
206a4 43 41 53 54 20 26 26 20 6f 70 21 3d 54 4b 5f 55 CAST && op!=TK_U
206a5 50 4c 55 53 20 29 7b 0a 20 20 20 20 20 20 62 72 PLUS ){. br
206a6 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 eak;. }. p
206a7 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d = p->pLeft;. }
206a8 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 43 68 . if( sqlite3Ch
206a9 65 63 6b 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 eckCollSeq(pPars
206aa 65 2c 20 70 43 6f 6c 6c 29 20 29 7b 20 0a 20 20 e, pColl) ){ .
206ab 20 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 7d pColl = 0;. }
206ac 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b . return pColl;
206ad 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 .}../*.** pExpr
206ae 69 73 20 61 6e 20 6f 70 65 72 61 6e 64 20 6f 66 is an operand of
206af 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 a comparison op
206b0 65 72 61 74 6f 72 2e 20 20 61 66 66 32 20 69 73 erator. aff2 is
206b1 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 61 66 66 the.** type aff
206b2 69 6e 69 74 79 20 6f 66 20 74 68 65 20 6f 74 68 inity of the oth
206b3 65 72 20 6f 70 65 72 61 6e 64 2e 20 20 54 68 69 er operand. Thi
206b4 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e s routine return
206b5 73 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 61 66 s the.** type af
206b6 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 finity that shou
206b7 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 ld be used for t
206b8 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 he comparison op
206b9 65 72 61 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 erator..*/.SQLIT
206ba 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 E_PRIVATE char s
206bb 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 qlite3CompareAff
206bc 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 inity(Expr *pExp
206bd 72 2c 20 63 68 61 72 20 61 66 66 32 29 7b 0a 20 r, char aff2){.
206be 20 63 68 61 72 20 61 66 66 31 20 3d 20 73 71 6c char aff1 = sql
206bf 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 ite3ExprAffinity
206c0 28 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20 61 (pExpr);. if( a
206c1 66 66 31 20 26 26 20 61 66 66 32 20 29 7b 0a 20 ff1 && aff2 ){.
206c2 20 20 20 2f 2a 20 42 6f 74 68 20 73 69 64 65 73 /* Both sides
206c3 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 of the comparis
206c4 6f 6e 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2e 20 on are columns.
206c5 49 66 20 6f 6e 65 20 68 61 73 20 6e 75 6d 65 72 If one has numer
206c6 69 63 0a 20 20 20 20 2a 2a 20 61 66 66 69 6e 69 ic. ** affini
206c7 74 79 2c 20 75 73 65 20 74 68 61 74 2e 20 4f 74 ty, use that. Ot
206c8 68 65 72 77 69 73 65 20 75 73 65 20 6e 6f 20 61 herwise use no a
206c9 66 66 69 6e 69 74 79 2e 0a 20 20 20 20 2a 2f 0a ffinity.. */.
206ca 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 if( sqlite3I
206cb 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 sNumericAffinity
206cc 28 61 66 66 31 29 20 7c 7c 20 73 71 6c 69 74 65 (aff1) || sqlite
206cd 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 3IsNumericAffini
206ce 74 79 28 61 66 66 32 29 20 29 7b 0a 20 20 20 20 ty(aff2) ){.
206cf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
206d0 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 20 AFF_NUMERIC;.
206d1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 }else{. re
206d2 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f turn SQLITE_AFF_
206d3 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 NONE;. }. }e
206d4 6c 73 65 20 69 66 28 20 21 61 66 66 31 20 26 26 lse if( !aff1 &&
206d5 20 21 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a !aff2 ){. /*
206d6 20 4e 65 69 74 68 65 72 20 73 69 64 65 20 6f 66 Neither side of
206d7 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 the comparison
206d8 69 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 43 6f is a column. Co
206d9 6d 70 61 72 65 20 74 68 65 0a 20 20 20 20 2a 2a mpare the. **
206da 20 72 65 73 75 6c 74 73 20 64 69 72 65 63 74 6c results directl
206db 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 y.. */. re
206dc 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f turn SQLITE_AFF_
206dd 4e 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 NONE;. }else{.
206de 20 20 20 2f 2a 20 4f 6e 65 20 73 69 64 65 20 69 /* One side i
206df 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 20 s a column, the
206e0 6f 74 68 65 72 20 69 73 20 6e 6f 74 2e 20 55 73 other is not. Us
206e1 65 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 61 66 e the columns af
206e2 66 69 6e 69 74 79 2e 20 2a 2f 0a 20 20 20 20 61 finity. */. a
206e3 73 73 65 72 74 28 20 61 66 66 31 3d 3d 30 20 7c ssert( aff1==0 |
206e4 7c 20 61 66 66 32 3d 3d 30 20 29 3b 0a 20 20 20 | aff2==0 );.
206e5 20 72 65 74 75 72 6e 20 28 61 66 66 31 20 2b 20 return (aff1 +
206e6 61 66 66 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a aff2);. }.}../*
206e7 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 20 63 .** pExpr is a c
206e8 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 omparison operat
206e9 6f 72 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 or. Return the
206ea 74 79 70 65 20 61 66 66 69 6e 69 74 79 20 74 68 type affinity th
206eb 61 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 at should.** be
206ec 61 70 70 6c 69 65 64 20 74 6f 20 62 6f 74 68 20 applied to both
206ed 6f 70 65 72 61 6e 64 73 20 70 72 69 6f 72 20 74 operands prior t
206ee 6f 20 64 6f 69 6e 67 20 74 68 65 20 63 6f 6d 70 o doing the comp
206ef 61 72 69 73 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 arison..*/.stati
206f0 63 20 63 68 61 72 20 63 6f 6d 70 61 72 69 73 6f c char compariso
206f1 6e 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a nAffinity(Expr *
206f2 70 45 78 70 72 29 7b 0a 20 20 63 68 61 72 20 61 pExpr){. char a
206f3 66 66 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 ff;. assert( pE
206f4 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 51 20 7c xpr->op==TK_EQ |
206f5 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f | pExpr->op==TK_
206f6 49 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d IN || pExpr->op=
206f7 3d 54 4b 5f 4c 54 20 7c 7c 0a 20 20 20 20 20 20 =TK_LT ||.
206f8 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 pExpr->op==T
206f9 4b 5f 47 54 20 7c 7c 20 70 45 78 70 72 2d 3e 6f K_GT || pExpr->o
206fa 70 3d 3d 54 4b 5f 47 45 20 7c 7c 20 70 45 78 70 p==TK_GE || pExp
206fb 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 45 20 7c 7c 0a r->op==TK_LE ||.
206fc 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d pExpr-
206fd 3e 6f 70 3d 3d 54 4b 5f 4e 45 20 7c 7c 20 70 45 >op==TK_NE || pE
206fe 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 20 7c xpr->op==TK_IS |
206ff 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f | pExpr->op==TK_
20700 49 53 4e 4f 54 20 29 3b 0a 20 20 61 73 73 65 72 ISNOT );. asser
20701 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 t( pExpr->pLeft
20702 29 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c 69 74 );. aff = sqlit
20703 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 e3ExprAffinity(p
20704 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 Expr->pLeft);.
20705 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 if( pExpr->pRigh
20706 74 20 29 7b 0a 20 20 20 20 61 66 66 20 3d 20 73 t ){. aff = s
20707 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 qlite3CompareAff
20708 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 52 69 inity(pExpr->pRi
20709 67 68 74 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c ght, aff);. }el
2070a 73 65 20 69 66 28 20 45 78 70 72 48 61 73 50 72 se if( ExprHasPr
2070b 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
2070c 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 _xIsSelect) ){.
2070d 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 aff = sqlite3
2070e 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 CompareAffinity(
2070f 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 pExpr->x.pSelect
20710 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 ->pEList->a[0].p
20711 45 78 70 72 2c 20 61 66 66 29 3b 0a 20 20 7d 65 Expr, aff);. }e
20712 6c 73 65 20 69 66 28 20 21 61 66 66 20 29 7b 0a lse if( !aff ){.
20713 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 aff = SQLITE
20714 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20 _AFF_NONE;. }.
20715 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a return aff;.}..
20716 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 /*.** pExpr is a
20717 20 63 6f 6d 70 61 72 69 73 6f 6e 20 65 78 70 72 comparison expr
20718 65 73 73 69 6f 6e 2c 20 65 67 2e 20 27 3d 27 2c ession, eg. '=',
20719 20 27 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 65 74 '<', IN(...) et
2071a 63 2e 0a 2a 2a 20 69 64 78 5f 61 66 66 69 6e 69 c..** idx_affini
2071b 74 79 20 69 73 20 74 68 65 20 61 66 66 69 6e 69 ty is the affini
2071c 74 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64 ty of an indexed
2071d 20 63 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 6e 20 column. Return
2071e 74 72 75 65 0a 2a 2a 20 69 66 20 74 68 65 20 69 true.** if the i
2071f 6e 64 65 78 20 77 69 74 68 20 61 66 66 69 6e 69 ndex with affini
20720 74 79 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20 ty idx_affinity
20721 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 69 may be used to i
20722 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 65 20 mplement.** the
20723 63 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 20 70 45 comparison in pE
20724 78 70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 xpr..*/.SQLITE_P
20725 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
20726 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f e3IndexAffinityO
20727 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 k(Expr *pExpr, c
20728 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79 har idx_affinity
20729 29 7b 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20 ){. char aff =
2072a 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 comparisonAffini
2072b 74 79 28 70 45 78 70 72 29 3b 0a 20 20 73 77 69 ty(pExpr);. swi
2072c 74 63 68 28 20 61 66 66 20 29 7b 0a 20 20 20 20 tch( aff ){.
2072d 63 61 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f case SQLITE_AFF_
2072e 4e 4f 4e 45 3a 0a 20 20 20 20 20 20 72 65 74 75 NONE:. retu
2072f 72 6e 20 31 3b 0a 20 20 20 20 63 61 73 65 20 53 rn 1;. case S
20730 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a QLITE_AFF_TEXT:.
20731 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 64 78 return idx
20732 5f 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 _affinity==SQLIT
20733 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 E_AFF_TEXT;.
20734 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 72 default:. r
20735 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49 73 4e eturn sqlite3IsN
20736 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 69 umericAffinity(i
20737 64 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 dx_affinity);.
20738 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 }.}../*.** Retur
20739 6e 20 74 68 65 20 50 35 20 76 61 6c 75 65 20 74 n the P5 value t
2073a 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 hat should be us
2073b 65 64 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20 ed for a binary
2073c 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 6f 70 comparison.** op
2073d 63 6f 64 65 20 28 4f 50 5f 45 71 2c 20 4f 50 5f code (OP_Eq, OP_
2073e 47 65 20 65 74 63 2e 29 20 75 73 65 64 20 74 6f Ge etc.) used to
2073f 20 63 6f 6d 70 61 72 65 20 70 45 78 70 72 31 20 compare pExpr1
20740 61 6e 64 20 70 45 78 70 72 32 2e 0a 2a 2f 0a 73 and pExpr2..*/.s
20741 74 61 74 69 63 20 75 38 20 62 69 6e 61 72 79 43 tatic u8 binaryC
20742 6f 6d 70 61 72 65 50 35 28 45 78 70 72 20 2a 70 ompareP5(Expr *p
20743 45 78 70 72 31 2c 20 45 78 70 72 20 2a 70 45 78 Expr1, Expr *pEx
20744 70 72 32 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e pr2, int jumpIfN
20745 75 6c 6c 29 7b 0a 20 20 75 38 20 61 66 66 20 3d ull){. u8 aff =
20746 20 28 63 68 61 72 29 73 71 6c 69 74 65 33 45 78 (char)sqlite3Ex
20747 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 prAffinity(pExpr
20748 32 29 3b 0a 20 20 61 66 66 20 3d 20 28 75 38 29 2);. aff = (u8)
20749 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 sqlite3CompareAf
2074a 66 69 6e 69 74 79 28 70 45 78 70 72 31 2c 20 61 finity(pExpr1, a
2074b 66 66 29 20 7c 20 28 75 38 29 6a 75 6d 70 49 66 ff) | (u8)jumpIf
2074c 4e 75 6c 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 Null;. return a
2074d 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ff;.}../*.** Ret
2074e 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
2074f 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 the collation s
20750 65 71 75 65 6e 63 65 20 74 68 61 74 20 73 68 6f equence that sho
20751 75 6c 64 20 62 65 20 75 73 65 64 20 62 79 0a 2a uld be used by.*
20752 2a 20 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 * a binary compa
20753 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 20 63 rison operator c
20754 6f 6d 70 61 72 69 6e 67 20 70 4c 65 66 74 20 61 omparing pLeft a
20755 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2a 0a 2a 2a nd pRight..**.**
20756 20 49 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e If the left han
20757 64 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 d expression has
20758 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 a collating seq
20759 75 65 6e 63 65 20 74 79 70 65 2c 20 74 68 65 6e uence type, then
2075a 20 69 74 20 69 73 0a 2a 2a 20 75 73 65 64 2e 20 it is.** used.
2075b 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 63 6f Otherwise the co
2075c 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
2075d 20 66 6f 72 20 74 68 65 20 72 69 67 68 74 20 68 for the right h
2075e 61 6e 64 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a and expression.*
2075f 2a 20 69 73 20 75 73 65 64 2c 20 6f 72 20 74 68 * is used, or th
20760 65 20 64 65 66 61 75 6c 74 20 28 42 49 4e 41 52 e default (BINAR
20761 59 29 20 69 66 20 6e 65 69 74 68 65 72 20 65 78 Y) if neither ex
20762 70 72 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63 pression has a c
20763 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 74 79 70 65 ollating.** type
20764 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 ..**.** Argument
20765 20 70 52 69 67 68 74 20 28 62 75 74 20 6e 6f 74 pRight (but not
20766 20 70 4c 65 66 74 29 20 6d 61 79 20 62 65 20 61 pLeft) may be a
20767 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2e 20 49 null pointer. I
20768 6e 20 74 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20 n this case,.**
20769 69 74 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 it is not consid
2076a 65 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ered..*/.SQLITE_
2076b 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 PRIVATE CollSeq
2076c 2a 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f *sqlite3BinaryCo
2076d 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 0a 20 20 mpareCollSeq(.
2076e 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a Parse *pParse, .
2076f 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 0a Expr *pLeft, .
20770 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29 Expr *pRight.)
20771 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f {. CollSeq *pCo
20772 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c ll;. assert( pL
20773 65 66 74 20 29 3b 0a 20 20 69 66 28 20 70 4c 65 eft );. if( pLe
20774 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 ft->flags & EP_E
20775 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 xpCollate ){.
20776 20 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e assert( pLeft->
20777 70 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f pColl );. pCo
20778 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43 6f 6c ll = pLeft->pCol
20779 6c 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 l;. }else if( p
2077a 52 69 67 68 74 20 26 26 20 70 52 69 67 68 74 2d Right && pRight-
2077b 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 >flags & EP_ExpC
2077c 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 73 ollate ){. as
2077d 73 65 72 74 28 20 70 52 69 67 68 74 2d 3e 70 43 sert( pRight->pC
2077e 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c oll );. pColl
2077f 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c = pRight->pColl
20780 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
20781 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 Coll = sqlite3Ex
20782 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 prCollSeq(pParse
20783 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66 , pLeft);. if
20784 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 ( !pColl ){.
20785 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 pColl = sqlite
20786 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 3ExprCollSeq(pPa
20787 72 73 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 rse, pRight);.
20788 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
20789 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pColl;.}../*.**
2078a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 6f 70 Generate the op
2078b 65 72 61 6e 64 73 20 66 6f 72 20 61 20 63 6f 6d erands for a com
2078c 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 69 6f parison operatio
2078d 6e 2e 20 20 42 65 66 6f 72 65 0a 2a 2a 20 67 65 n. Before.** ge
2078e 6e 65 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64 nerating the cod
2078f 65 20 66 6f 72 20 65 61 63 68 20 6f 70 65 72 61 e for each opera
20790 6e 64 2c 20 73 65 74 20 74 68 65 20 45 50 5f 41 nd, set the EP_A
20791 6e 79 41 66 66 0a 2a 2a 20 66 6c 61 67 20 6f 6e nyAff.** flag on
20792 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
20793 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 so that it will
20794 62 65 20 61 62 6c 65 20 74 6f 20 75 73 65 64 20 be able to used
20795 61 0a 2a 2a 20 63 61 63 68 65 64 20 63 6f 6c 75 a.** cached colu
20796 6d 6e 20 76 61 6c 75 65 20 74 68 61 74 20 68 61 mn value that ha
20797 73 20 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 64 s previously und
20798 65 72 67 6f 6e 65 20 61 6e 0a 2a 2a 20 61 66 66 ergone an.** aff
20799 69 6e 69 74 79 20 63 68 61 6e 67 65 2e 0a 2a 2f inity change..*/
2079a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 .static void cod
2079b 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 eCompareOperands
2079c 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
2079d 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 e, /* Parsing
2079e 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 and code genera
2079f 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a ting context */.
207a0 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 Expr *pLeft,
207a1 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 /* The left
207a2 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 operand */. int
207a3 20 2a 70 52 65 67 4c 65 66 74 2c 20 20 20 20 2f *pRegLeft, /
207a4 2a 20 52 65 67 69 73 74 65 72 20 77 68 65 72 65 * Register where
207a5 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 69 73 left operand is
207a6 20 73 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 stored */. int
207a7 20 2a 70 46 72 65 65 4c 65 66 74 2c 20 20 20 2f *pFreeLeft, /
207a8 2a 20 46 72 65 65 20 74 68 69 73 20 72 65 67 69 * Free this regi
207a9 73 74 65 72 20 77 68 65 6e 20 64 6f 6e 65 20 2a ster when done *
207aa 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 /. Expr *pRight
207ab 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 , /* The rig
207ac 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 ht operand */.
207ad 69 6e 74 20 2a 70 52 65 67 52 69 67 68 74 2c 20 int *pRegRight,
207ae 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 77 68 /* Register wh
207af 65 72 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e ere right operan
207b0 64 20 69 73 20 73 74 6f 72 65 64 20 2a 2f 0a 20 d is stored */.
207b1 20 69 6e 74 20 2a 70 46 72 65 65 52 69 67 68 74 int *pFreeRight
207b2 20 20 20 2f 2a 20 57 72 69 74 65 20 74 65 6d 70 /* Write temp
207b3 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 72 69 register for ri
207b4 67 68 74 20 6f 70 65 72 61 6e 64 20 74 68 65 72 ght operand ther
207b5 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 e */.){. while(
207b6 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 pLeft->op==TK_U
207b7 50 4c 55 53 20 29 20 70 4c 65 66 74 20 3d 20 70 PLUS ) pLeft = p
207b8 4c 65 66 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 Left->pLeft;. p
207b9 4c 65 66 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 Left->flags |= E
207ba 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65 P_AnyAff;. *pRe
207bb 67 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 gLeft = sqlite3E
207bc 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 xprCodeTemp(pPar
207bd 73 65 2c 20 70 4c 65 66 74 2c 20 70 46 72 65 65 se, pLeft, pFree
207be 4c 65 66 74 29 3b 0a 20 20 77 68 69 6c 65 28 20 Left);. while(
207bf 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 pRight->op==TK_U
207c0 50 4c 55 53 20 29 20 70 52 69 67 68 74 20 3d 20 PLUS ) pRight =
207c1 70 52 69 67 68 74 2d 3e 70 4c 65 66 74 3b 0a 20 pRight->pLeft;.
207c2 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 7c pRight->flags |
207c3 3d 20 45 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a = EP_AnyAff;. *
207c4 70 52 65 67 52 69 67 68 74 20 3d 20 73 71 6c 69 pRegRight = sqli
207c5 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 te3ExprCodeTemp(
207c6 70 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 pParse, pRight,
207c7 70 46 72 65 65 52 69 67 68 74 29 3b 0a 7d 0a 0a pFreeRight);.}..
207c8 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 /*.** Generate c
207c9 6f 64 65 20 66 6f 72 20 61 20 63 6f 6d 70 61 72 ode for a compar
207ca 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 0a 2a ison operator..*
207cb 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 /.static int cod
207cc 65 43 6f 6d 70 61 72 65 28 0a 20 20 50 61 72 73 eCompare(. Pars
207cd 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a e *pParse, /*
207ce 20 54 68 65 20 70 61 72 73 69 6e 67 20 28 61 6e The parsing (an
207cf 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e d code generatin
207d0 67 29 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 g) context */.
207d1 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 Expr *pLeft,
207d2 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70 /* The left op
207d3 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 erand */. Expr
207d4 2a 70 52 69 67 68 74 2c 20 20 20 20 20 2f 2a 20 *pRight, /*
207d5 54 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e The right operan
207d6 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f 64 d */. int opcod
207d7 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 e, /* The
207d8 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 63 6f 64 comparison opcod
207d9 65 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 31 2c 20 e */. int in1,
207da 69 6e 74 20 69 6e 32 2c 20 2f 2a 20 52 65 67 69 int in2, /* Regi
207db 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6f 70 65 ster holding ope
207dc 72 61 6e 64 73 20 2a 2f 0a 20 20 69 6e 74 20 64 rands */. int d
207dd 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 est, /*
207de 4a 75 6d 70 20 68 65 72 65 20 69 66 20 74 72 75 Jump here if tru
207df 65 2e 20 20 2a 2f 0a 20 20 69 6e 74 20 6a 75 6d e. */. int jum
207e0 70 49 66 4e 75 6c 6c 20 20 20 20 2f 2a 20 49 66 pIfNull /* If
207e1 20 74 72 75 65 2c 20 6a 75 6d 70 20 69 66 20 65 true, jump if e
207e2 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 ither operand is
207e3 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 69 6e NULL */.){. in
207e4 74 20 70 35 3b 0a 20 20 69 6e 74 20 61 64 64 72 t p5;. int addr
207e5 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 34 3b ;. CollSeq *p4;
207e6 0a 0a 20 20 70 34 20 3d 20 73 71 6c 69 74 65 33 .. p4 = sqlite3
207e7 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c BinaryCompareCol
207e8 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 lSeq(pParse, pLe
207e9 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 70 ft, pRight);. p
207ea 35 20 3d 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 5 = binaryCompar
207eb 65 50 35 28 70 4c 65 66 74 2c 20 70 52 69 67 68 eP5(pLeft, pRigh
207ec 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a t, jumpIfNull);.
207ed 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 addr = sqlite3
207ee 56 64 62 65 41 64 64 4f 70 34 28 70 50 61 72 73 VdbeAddOp4(pPars
207ef 65 2d 3e 70 56 64 62 65 2c 20 6f 70 63 6f 64 65 e->pVdbe, opcode
207f0 2c 20 69 6e 32 2c 20 64 65 73 74 2c 20 69 6e 31 , in2, dest, in1
207f1 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
207f2 20 20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f (vo
207f3 69 64 2a 29 70 34 2c 20 50 34 5f 43 4f 4c 4c 53 id*)p4, P4_COLLS
207f4 45 51 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 EQ);. sqlite3Vd
207f5 62 65 43 68 61 6e 67 65 50 35 28 70 50 61 72 73 beChangeP5(pPars
207f6 65 2d 3e 70 56 64 62 65 2c 20 28 75 38 29 70 35 e->pVdbe, (u8)p5
207f7 29 3b 0a 20 20 69 66 28 20 28 70 35 20 26 20 53 );. if( (p5 & S
207f8 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 29 21 QLITE_AFF_MASK)!
207f9 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 =SQLITE_AFF_NONE
207fa 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
207fb 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 xprCacheAffinity
207fc 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 Change(pParse, i
207fd 6e 31 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 n1, 1);. sqli
207fe 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 te3ExprCacheAffi
207ff 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 nityChange(pPars
20800 65 2c 20 69 6e 32 2c 20 31 29 3b 0a 20 20 7d 0a e, in2, 1);. }.
20801 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d return addr;.}
20802 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 ..#if SQLITE_MAX
20803 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 2f 2a _EXPR_DEPTH>0./*
20804 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 61 .** Check that a
20805 72 67 75 6d 65 6e 74 20 6e 48 65 69 67 68 74 20 rgument nHeight
20806 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 is less than or
20807 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 equal to the max
20808 69 6d 75 6d 0a 2a 2a 20 65 78 70 72 65 73 73 69 imum.** expressi
20809 6f 6e 20 64 65 70 74 68 20 61 6c 6c 6f 77 65 64 on depth allowed
2080a 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 . If it is not,
2080b 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d leave an error m
2080c 65 73 73 61 67 65 20 69 6e 0a 2a 2a 20 70 50 61 essage in.** pPa
2080d 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 rse..*/.SQLITE_P
2080e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
2080f 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 e3ExprCheckHeigh
20810 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c t(Parse *pParse,
20811 20 69 6e 74 20 6e 48 65 69 67 68 74 29 7b 0a 20 int nHeight){.
20812 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
20813 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6d 78 48 65 69 _OK;. int mxHei
20814 67 68 74 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 ght = pParse->db
20815 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
20816 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48 LIMIT_EXPR_DEPTH
20817 5d 3b 0a 20 20 69 66 28 20 6e 48 65 69 67 68 74 ];. if( nHeight
20818 3e 6d 78 48 65 69 67 68 74 20 29 7b 0a 20 20 20 >mxHeight ){.
20819 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
2081a 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 (pParse, .
2081b 20 22 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 "Expression tre
2081c 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 28 e is too large (
2081d 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 25 64 maximum depth %d
2081e 29 22 2c 20 6d 78 48 65 69 67 68 74 0a 20 20 20 )", mxHeight.
2081f 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c );. rc = SQL
20820 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 ITE_ERROR;. }.
20821 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
20822 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
20823 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 2c three functions,
20824 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 29 2c heightOfExpr(),
20825 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 heightOfExprLis
20826 74 28 29 0a 2a 2a 20 61 6e 64 20 68 65 69 67 68 t().** and heigh
20827 74 4f 66 53 65 6c 65 63 74 28 29 2c 20 61 72 65 tOfSelect(), are
20828 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 used to determi
20829 6e 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 ne the maximum h
2082a 65 69 67 68 74 0a 2a 2a 20 6f 66 20 61 6e 79 20 eight.** of any
2082b 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 expression tree
2082c 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 74 68 referenced by th
2082d 65 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 e structure pass
2082e 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 ed as the.** fir
2082f 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a st argument..**.
20830 2a 2a 20 49 66 20 74 68 69 73 20 6d 61 78 69 6d ** If this maxim
20831 75 6d 20 68 65 69 67 68 74 20 69 73 20 67 72 65 um height is gre
20832 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 ater than the cu
20833 72 72 65 6e 74 20 76 61 6c 75 65 20 70 6f 69 6e rrent value poin
20834 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 70 6e 48 ted.** to by pnH
20835 65 69 67 68 74 2c 20 74 68 65 20 73 65 63 6f 6e eight, the secon
20836 64 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 d parameter, the
20837 6e 20 73 65 74 20 2a 70 6e 48 65 69 67 68 74 20 n set *pnHeight
20838 74 6f 20 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65 to that.** value
20839 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
2083a 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 45 78 heightOfExpr(Ex
2083b 70 72 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 pr *p, int *pnHe
2083c 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 ight){. if( p )
2083d 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 48 65 {. if( p->nHe
2083e 69 67 68 74 3e 2a 70 6e 48 65 69 67 68 74 20 29 ight>*pnHeight )
2083f 7b 0a 20 20 20 20 20 20 2a 70 6e 48 65 69 67 68 {. *pnHeigh
20840 74 20 3d 20 70 2d 3e 6e 48 65 69 67 68 74 3b 0a t = p->nHeight;.
20841 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 }. }.}.stat
20842 69 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66 ic void heightOf
20843 45 78 70 72 4c 69 73 74 28 45 78 70 72 4c 69 73 ExprList(ExprLis
20844 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 t *p, int *pnHei
20845 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b ght){. if( p ){
20846 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
20847 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 for(i=0; i<p->nE
20848 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 xpr; i++){.
20849 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d heightOfExpr(p-
2084a 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 70 6e 48 >a[i].pExpr, pnH
2084b 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 eight);. }.
2084c 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 }.}.static void
2084d 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 53 heightOfSelect(S
2084e 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 2a 70 elect *p, int *p
2084f 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20 nHeight){. if(
20850 70 20 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f p ){. heightO
20851 66 45 78 70 72 28 70 2d 3e 70 57 68 65 72 65 2c fExpr(p->pWhere,
20852 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 pnHeight);.
20853 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e heightOfExpr(p->
20854 70 48 61 76 69 6e 67 2c 20 70 6e 48 65 69 67 68 pHaving, pnHeigh
20855 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 t);. heightOf
20856 45 78 70 72 28 70 2d 3e 70 4c 69 6d 69 74 2c 20 Expr(p->pLimit,
20857 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 pnHeight);. h
20858 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 eightOfExpr(p->p
20859 4f 66 66 73 65 74 2c 20 70 6e 48 65 69 67 68 74 Offset, pnHeight
2085a 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 );. heightOfE
2085b 78 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 xprList(p->pELis
2085c 74 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 t, pnHeight);.
2085d 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 heightOfExprLi
2085e 73 74 28 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 st(p->pGroupBy,
2085f 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 pnHeight);. h
20860 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 eightOfExprList(
20861 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 70 6e 48 p->pOrderBy, pnH
20862 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 eight);. heig
20863 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e 70 50 htOfSelect(p->pP
20864 72 69 6f 72 2c 20 70 6e 48 65 69 67 68 74 29 3b rior, pnHeight);
20865 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 . }.}../*.** Se
20866 74 20 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67 t the Expr.nHeig
20867 68 74 20 76 61 72 69 61 62 6c 65 20 69 6e 20 74 ht variable in t
20868 68 65 20 73 74 72 75 63 74 75 72 65 20 70 61 73 he structure pas
20869 73 65 64 20 61 73 20 61 6e 20 0a 2a 2a 20 61 72 sed as an .** ar
2086a 67 75 6d 65 6e 74 2e 20 41 6e 20 65 78 70 72 65 gument. An expre
2086b 73 73 69 6f 6e 20 77 69 74 68 20 6e 6f 20 63 68 ssion with no ch
2086c 69 6c 64 72 65 6e 2c 20 45 78 70 72 2e 70 4c 69 ildren, Expr.pLi
2086d 73 74 20 6f 72 20 0a 2a 2a 20 45 78 70 72 2e 70 st or .** Expr.p
2086e 53 65 6c 65 63 74 20 6d 65 6d 62 65 72 20 68 61 Select member ha
2086f 73 20 61 20 68 65 69 67 68 74 20 6f 66 20 31 2e s a height of 1.
20870 20 41 6e 79 20 6f 74 68 65 72 20 65 78 70 72 65 Any other expre
20871 73 73 69 6f 6e 0a 2a 2a 20 68 61 73 20 61 20 68 ssion.** has a h
20872 65 69 67 68 74 20 65 71 75 61 6c 20 74 6f 20 74 eight equal to t
20873 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 he maximum heigh
20874 74 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 0a t of any other .
20875 2a 2a 20 72 65 66 65 72 65 6e 63 65 64 20 45 78 ** referenced Ex
20876 70 72 20 70 6c 75 73 20 6f 6e 65 2e 0a 2a 2f 0a pr plus one..*/.
20877 73 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 static void expr
20878 53 65 74 48 65 69 67 68 74 28 45 78 70 72 20 2a SetHeight(Expr *
20879 70 29 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 p){. int nHeigh
2087a 74 20 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f t = 0;. heightO
2087b 66 45 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20 fExpr(p->pLeft,
2087c 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 68 65 69 &nHeight);. hei
2087d 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 52 69 ghtOfExpr(p->pRi
2087e 67 68 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a ght, &nHeight);.
2087f 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f if( ExprHasPro
20880 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 perty(p, EP_xIsS
20881 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 68 65 elect) ){. he
20882 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e ightOfSelect(p->
20883 78 2e 70 53 65 6c 65 63 74 2c 20 26 6e 48 65 69 x.pSelect, &nHei
20884 67 68 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ght);. }else{.
20885 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c heightOfExprL
20886 69 73 74 28 70 2d 3e 78 2e 70 4c 69 73 74 2c 20 ist(p->x.pList,
20887 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20 &nHeight);. }.
20888 20 70 2d 3e 6e 48 65 69 67 68 74 20 3d 20 6e 48 p->nHeight = nH
20889 65 69 67 68 74 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a eight + 1;.}../*
2088a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 .** Set the Expr
2088b 2e 6e 48 65 69 67 68 74 20 76 61 72 69 61 62 6c .nHeight variabl
2088c 65 20 75 73 69 6e 67 20 74 68 65 20 65 78 70 72 e using the expr
2088d 53 65 74 48 65 69 67 68 74 28 29 20 66 75 6e 63 SetHeight() func
2088e 74 69 6f 6e 2e 20 49 66 0a 2a 2a 20 74 68 65 20 tion. If.** the
2088f 68 65 69 67 68 74 20 69 73 20 67 72 65 61 74 65 height is greate
20890 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d r than the maxim
20891 75 6d 20 61 6c 6c 6f 77 65 64 20 65 78 70 72 65 um allowed expre
20892 73 73 69 6f 6e 20 64 65 70 74 68 2c 0a 2a 2a 20 ssion depth,.**
20893 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 69 leave an error i
20894 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c n pParse..*/.SQL
20895 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
20896 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 sqlite3ExprSetH
20897 65 69 67 68 74 28 50 61 72 73 65 20 2a 70 50 61 eight(Parse *pPa
20898 72 73 65 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 rse, Expr *p){.
20899 20 65 78 70 72 53 65 74 48 65 69 67 68 74 28 70 exprSetHeight(p
2089a 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 );. sqlite3Expr
2089b 43 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 CheckHeight(pPar
2089c 73 65 2c 20 70 2d 3e 6e 48 65 69 67 68 74 29 3b se, p->nHeight);
2089d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
2089e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 the maximum hei
2089f 67 68 74 20 6f 66 20 61 6e 79 20 65 78 70 72 65 ght of any expre
208a0 73 73 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 ssion tree refer
208a1 65 6e 63 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 enced.** by the
208a2 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 select statement
208a3 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 passed as an ar
208a4 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 gument..*/.SQLIT
208a5 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
208a6 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 lite3SelectExprH
208a7 65 69 67 68 74 28 53 65 6c 65 63 74 20 2a 70 29 eight(Select *p)
208a8 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20 {. int nHeight
208a9 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 53 = 0;. heightOfS
208aa 65 6c 65 63 74 28 70 2c 20 26 6e 48 65 69 67 68 elect(p, &nHeigh
208ab 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 48 65 t);. return nHe
208ac 69 67 68 74 3b 0a 7d 0a 23 65 6c 73 65 0a 20 20 ight;.}.#else.
208ad 23 64 65 66 69 6e 65 20 65 78 70 72 53 65 74 48 #define exprSetH
208ae 65 69 67 68 74 28 79 29 0a 23 65 6e 64 69 66 20 eight(y).#endif
208af 2f 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 /* SQLITE_MAX_EX
208b0 50 52 5f 44 45 50 54 48 3e 30 20 2a 2f 0a 0a 2f PR_DEPTH>0 */../
208b1 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
208b2 65 20 69 73 20 74 68 65 20 63 6f 72 65 20 61 6c e is the core al
208b3 6c 6f 63 61 74 6f 72 20 66 6f 72 20 45 78 70 72 locator for Expr
208b4 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f nodes..**.** Co
208b5 6e 73 74 72 75 63 74 20 61 20 6e 65 77 20 65 78 nstruct a new ex
208b6 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 61 6e pression node an
208b7 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 d return a point
208b8 65 72 20 74 6f 20 69 74 2e 20 20 4d 65 6d 6f 72 er to it. Memor
208b9 79 0a 2a 2a 20 66 6f 72 20 74 68 69 73 20 6e 6f y.** for this no
208ba 64 65 20 61 6e 64 20 66 6f 72 20 74 68 65 20 70 de and for the p
208bb 54 6f 6b 65 6e 20 61 72 67 75 6d 65 6e 74 20 69 Token argument i
208bc 73 20 61 20 73 69 6e 67 6c 65 20 61 6c 6c 6f 63 s a single alloc
208bd 61 74 69 6f 6e 0a 2a 2a 20 6f 62 74 61 69 6e 65 ation.** obtaine
208be 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 44 62 d from sqlite3Db
208bf 4d 61 6c 6c 6f 63 28 29 2e 20 20 54 68 65 20 63 Malloc(). The c
208c0 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a alling function.
208c1 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c ** is responsibl
208c2 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72 e for making sur
208c3 65 20 74 68 65 20 6e 6f 64 65 20 65 76 65 6e 74 e the node event
208c4 75 61 6c 6c 79 20 67 65 74 73 20 66 72 65 65 64 ually gets freed
208c5 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 65 71 75 6f ..**.** If dequo
208c6 74 65 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e te is true, then
208c7 20 74 68 65 20 74 6f 6b 65 6e 20 28 69 66 20 69 the token (if i
208c8 74 20 65 78 69 73 74 73 29 20 69 73 20 64 65 71 t exists) is deq
208c9 75 6f 74 65 64 2e 0a 2a 2a 20 49 66 20 64 65 71 uoted..** If deq
208ca 75 6f 74 65 20 69 73 20 66 61 6c 73 65 2c 20 6e uote is false, n
208cb 6f 20 64 65 71 75 6f 74 69 6e 67 20 69 73 20 70 o dequoting is p
208cc 65 72 66 6f 72 6d 61 6e 63 65 2e 20 20 54 68 65 erformance. The
208cd 20 64 65 51 75 6f 74 65 0a 2a 2a 20 70 61 72 61 deQuote.** para
208ce 6d 65 74 65 72 20 69 73 20 69 67 6e 6f 72 65 64 meter is ignored
208cf 20 69 66 20 70 54 6f 6b 65 6e 20 69 73 20 4e 55 if pToken is NU
208d0 4c 4c 20 6f 72 20 69 66 20 74 68 65 20 74 6f 6b LL or if the tok
208d1 65 6e 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 61 en does not.** a
208d2 70 70 65 61 72 20 74 6f 20 62 65 20 71 75 6f 74 ppear to be quot
208d3 65 64 2e 20 20 49 66 20 74 68 65 20 71 75 6f 74 ed. If the quot
208d4 65 73 20 77 65 72 65 20 6f 66 20 74 68 65 20 66 es were of the f
208d5 6f 72 6d 20 22 2e 2e 2e 22 20 28 64 6f 75 62 6c orm "..." (doubl
208d6 65 2d 71 75 6f 74 65 73 29 0a 2a 2a 20 74 68 65 e-quotes).** the
208d7 6e 20 74 68 65 20 45 50 5f 44 62 6c 51 75 6f 74 n the EP_DblQuot
208d8 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 6f ed flag is set o
208d9 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e n the expression
208da 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 node..**.** Spe
208db 63 69 61 6c 20 63 61 73 65 3a 20 20 49 66 20 6f cial case: If o
208dc 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 61 6e p==TK_INTEGER an
208dd 64 20 70 54 6f 6b 65 6e 20 70 6f 69 6e 74 73 20 d pToken points
208de 74 6f 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 to a string that
208df 0a 2a 2a 20 63 61 6e 20 62 65 20 74 72 61 6e 73 .** can be trans
208e0 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 33 32 2d lated into a 32-
208e1 62 69 74 20 69 6e 74 65 67 65 72 2c 20 74 68 65 bit integer, the
208e2 6e 20 74 68 65 20 74 6f 6b 65 6e 20 69 73 20 6e n the token is n
208e3 6f 74 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 ot.** stored in
208e4 75 2e 7a 54 6f 6b 65 6e 2e 20 20 49 6e 73 74 65 u.zToken. Inste
208e5 61 64 2c 20 74 68 65 20 69 6e 74 65 67 65 72 20 ad, the integer
208e6 76 61 6c 75 65 73 20 69 73 20 77 72 69 74 74 65 values is writte
208e7 6e 0a 2a 2a 20 69 6e 74 6f 20 75 2e 69 56 61 6c n.** into u.iVal
208e8 75 65 20 61 6e 64 20 74 68 65 20 45 50 5f 49 6e ue and the EP_In
208e9 74 56 61 6c 75 65 20 66 6c 61 67 20 69 73 20 73 tValue flag is s
208ea 65 74 2e 20 20 4e 6f 20 65 78 74 72 61 20 73 74 et. No extra st
208eb 6f 72 61 67 65 0a 2a 2a 20 69 73 20 61 6c 6c 6f orage.** is allo
208ec 63 61 74 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 cated to hold th
208ed 65 20 69 6e 74 65 67 65 72 20 74 65 78 74 20 61 e integer text a
208ee 6e 64 20 74 68 65 20 64 65 71 75 6f 74 65 20 66 nd the dequote f
208ef 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a lag is ignored..
208f0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
208f1 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 E Expr *sqlite3E
208f2 78 70 72 41 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 xprAlloc(. sqli
208f3 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 te3 *db,
208f4 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f /* Handle fo
208f5 72 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f r sqlite3DbMallo
208f6 63 5a 65 72 6f 28 29 20 28 6d 61 79 20 62 65 20 cZero() (may be
208f7 6e 75 6c 6c 29 20 2a 2f 0a 20 20 69 6e 74 20 6f null) */. int o
208f8 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p,
208f9 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e /* Expression
208fa 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e opcode */. con
208fb 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e st Token *pToken
208fc 2c 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 72 , /* Token ar
208fd 67 75 6d 65 6e 74 2e 20 20 4d 69 67 68 74 20 62 gument. Might b
208fe 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 e NULL */. int
208ff 64 65 71 75 6f 74 65 20 20 20 20 20 20 20 20 20 dequote
20900 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 /* True to d
20901 65 71 75 6f 74 65 20 2a 2f 0a 29 7b 0a 20 20 45 equote */.){. E
20902 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 xpr *pNew;. int
20903 20 6e 45 78 74 72 61 20 3d 20 30 3b 0a 20 20 69 nExtra = 0;. i
20904 6e 74 20 69 56 61 6c 75 65 20 3d 20 30 3b 0a 0a nt iValue = 0;..
20905 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a if( pToken ){.
20906 20 20 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f 49 if( op!=TK_I
20907 4e 54 45 47 45 52 20 7c 7c 20 70 54 6f 6b 65 6e NTEGER || pToken
20908 2d 3e 7a 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 ->z==0.
20909 20 7c 7c 20 73 71 6c 69 74 65 33 47 65 74 49 6e || sqlite3GetIn
2090a 74 33 32 28 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 26 t32(pToken->z, &
2090b 69 56 61 6c 75 65 29 3d 3d 30 20 29 7b 0a 20 20 iValue)==0 ){.
2090c 20 20 20 20 6e 45 78 74 72 61 20 3d 20 70 54 6f nExtra = pTo
2090d 6b 65 6e 2d 3e 6e 2b 31 3b 0a 20 20 20 20 7d 0a ken->n+1;. }.
2090e 20 20 7d 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c }. pNew = sql
2090f 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
20910 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70 72 (db, sizeof(Expr
20911 29 2b 6e 45 78 74 72 61 29 3b 0a 20 20 69 66 28 )+nExtra);. if(
20912 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 70 4e 65 pNew ){. pNe
20913 77 2d 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0a w->op = (u8)op;.
20914 20 20 20 20 70 4e 65 77 2d 3e 69 41 67 67 20 3d pNew->iAgg =
20915 20 2d 31 3b 0a 20 20 20 20 69 66 28 20 70 54 6f -1;. if( pTo
20916 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ken ){. if(
20917 20 6e 45 78 74 72 61 3d 3d 30 20 29 7b 0a 20 20 nExtra==0 ){.
20918 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 pNew->flag
20919 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 s |= EP_IntValue
2091a 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e ;. pNew->
2091b 75 2e 69 56 61 6c 75 65 20 3d 20 69 56 61 6c 75 u.iValue = iValu
2091c 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a e;. }else{.
2091d 20 20 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 int c;.
2091e 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 7a pNew->u.z
2091f 54 6f 6b 65 6e 20 3d 20 28 63 68 61 72 2a 29 26 Token = (char*)&
20920 70 4e 65 77 5b 31 5d 3b 0a 20 20 20 20 20 20 20 pNew[1];.
20921 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 75 2e memcpy(pNew->u.
20922 7a 54 6f 6b 65 6e 2c 20 70 54 6f 6b 65 6e 2d 3e zToken, pToken->
20923 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20 z, pToken->n);.
20924 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 7a pNew->u.z
20925 54 6f 6b 65 6e 5b 70 54 6f 6b 65 6e 2d 3e 6e 5d Token[pToken->n]
20926 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 = 0;. if
20927 28 20 64 65 71 75 6f 74 65 20 26 26 20 6e 45 78 ( dequote && nEx
20928 74 72 61 3e 3d 33 20 0a 20 20 20 20 20 20 20 20 tra>=3 .
20929 20 20 20 20 20 26 26 20 28 28 63 20 3d 20 70 54 && ((c = pT
2092a 6f 6b 65 6e 2d 3e 7a 5b 30 5d 29 3d 3d 27 5c 27 oken->z[0])=='\'
2092b 27 20 7c 7c 20 63 3d 3d 27 22 27 20 7c 7c 20 63 ' || c=='"' || c
2092c 3d 3d 27 5b 27 20 7c 7c 20 63 3d 3d 27 60 27 29 =='[' || c=='`')
2092d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 ){. sq
2092e 6c 69 74 65 33 44 65 71 75 6f 74 65 28 70 4e 65 lite3Dequote(pNe
2092f 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 w->u.zToken);.
20930 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 if( c=='
20931 22 27 20 29 20 70 4e 65 77 2d 3e 66 6c 61 67 73 "' ) pNew->flags
20932 20 7c 3d 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 |= EP_DblQuoted
20933 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
20934 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 20 53 51 }. }.#if SQ
20935 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 LITE_MAX_EXPR_DE
20936 50 54 48 3e 30 0a 20 20 20 20 70 4e 65 77 2d 3e PTH>0. pNew->
20937 6e 48 65 69 67 68 74 20 3d 20 31 3b 0a 23 65 6e nHeight = 1;.#en
20938 64 69 66 20 20 0a 20 20 7d 0a 20 20 72 65 74 75 dif . }. retu
20939 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a rn pNew;.}../*.*
2093a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 * Allocate a new
2093b 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 expression node
2093c 20 66 72 6f 6d 20 61 20 7a 65 72 6f 2d 74 65 72 from a zero-ter
2093d 6d 69 6e 61 74 65 64 20 74 6f 6b 65 6e 20 74 68 minated token th
2093e 61 74 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 at has.** alread
2093f 79 20 62 65 65 6e 20 64 65 71 75 6f 74 65 64 2e y been dequoted.
20940 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20941 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 TE Expr *sqlite3
20942 45 78 70 72 28 0a 20 20 73 71 6c 69 74 65 33 20 Expr(. sqlite3
20943 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 *db,
20944 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 73 71 /* Handle for sq
20945 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 lite3DbMallocZer
20946 6f 28 29 20 28 6d 61 79 20 62 65 20 6e 75 6c 6c o() (may be null
20947 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 ) */. int op,
20948 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
20949 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 * Expression opc
2094a 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ode */. const c
2094b 68 61 72 20 2a 7a 54 6f 6b 65 6e 20 20 20 20 20 har *zToken
2094c 20 2f 2a 20 54 6f 6b 65 6e 20 61 72 67 75 6d 65 /* Token argume
2094d 6e 74 2e 20 20 4d 69 67 68 74 20 62 65 20 4e 55 nt. Might be NU
2094e 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 54 6f 6b 65 6e LL */.){. Token
2094f 20 78 3b 0a 20 20 78 2e 7a 20 3d 20 7a 54 6f 6b x;. x.z = zTok
20950 65 6e 3b 0a 20 20 78 2e 6e 20 3d 20 7a 54 6f 6b en;. x.n = zTok
20951 65 6e 20 3f 20 73 71 6c 69 74 65 33 53 74 72 6c en ? sqlite3Strl
20952 65 6e 33 30 28 7a 54 6f 6b 65 6e 29 20 3a 20 30 en30(zToken) : 0
20953 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 ;. return sqlit
20954 65 33 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20 e3ExprAlloc(db,
20955 6f 70 2c 20 26 78 2c 20 30 29 3b 0a 7d 0a 0a 2f op, &x, 0);.}../
20956 2a 0a 2a 2a 20 41 74 74 61 63 68 20 73 75 62 74 *.** Attach subt
20957 72 65 65 73 20 70 4c 65 66 74 20 61 6e 64 20 70 rees pLeft and p
20958 52 69 67 68 74 20 74 6f 20 74 68 65 20 45 78 70 Right to the Exp
20959 72 20 6e 6f 64 65 20 70 52 6f 6f 74 2e 0a 2a 2a r node pRoot..**
2095a 0a 2a 2a 20 49 66 20 70 52 6f 6f 74 3d 3d 4e 55 .** If pRoot==NU
2095b 4c 4c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 LL that means th
2095c 61 74 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f at a memory allo
2095d 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 68 61 73 cation error has
2095e 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20 49 6e occurred..** In
2095f 20 74 68 61 74 20 63 61 73 65 2c 20 64 65 6c 65 that case, dele
20960 74 65 20 74 68 65 20 73 75 62 74 72 65 65 73 20 te the subtrees
20961 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 pLeft and pRight
20962 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
20963 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
20964 45 78 70 72 41 74 74 61 63 68 53 75 62 74 72 65 ExprAttachSubtre
20965 65 73 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 es(. sqlite3 *d
20966 62 2c 0a 20 20 45 78 70 72 20 2a 70 52 6f 6f 74 b,. Expr *pRoot
20967 2c 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c ,. Expr *pLeft,
20968 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 0a . Expr *pRight.
20969 29 7b 0a 20 20 69 66 28 20 70 52 6f 6f 74 3d 3d ){. if( pRoot==
2096a 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 0 ){. assert(
2096b 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
2096c 64 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 d );. sqlite3
2096d 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
2096e 4c 65 66 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 Left);. sqlit
2096f 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c e3ExprDelete(db,
20970 20 70 52 69 67 68 74 29 3b 0a 20 20 7d 65 6c 73 pRight);. }els
20971 65 7b 0a 20 20 20 20 69 66 28 20 70 52 69 67 68 e{. if( pRigh
20972 74 20 29 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 t ){. pRoot
20973 2d 3e 70 52 69 67 68 74 20 3d 20 70 52 69 67 68 ->pRight = pRigh
20974 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 52 69 t;. if( pRi
20975 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f ght->flags & EP_
20976 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 ExpCollate ){.
20977 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 66 6c 61 pRoot->fla
20978 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c gs |= EP_ExpColl
20979 61 74 65 3b 0a 20 20 20 20 20 20 20 20 70 52 6f ate;. pRo
2097a 6f 74 2d 3e 70 43 6f 6c 6c 20 3d 20 70 52 69 67 ot->pColl = pRig
2097b 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 ht->pColl;.
2097c 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
2097d 20 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 pLeft ){.
2097e 70 52 6f 6f 74 2d 3e 70 4c 65 66 74 20 3d 20 70 pRoot->pLeft = p
2097f 4c 65 66 74 3b 0a 20 20 20 20 20 20 69 66 28 20 Left;. if(
20980 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 pLeft->flags & E
20981 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a P_ExpCollate ){.
20982 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 66 pRoot->f
20983 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f lags |= EP_ExpCo
20984 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 20 70 llate;. p
20985 52 6f 6f 74 2d 3e 70 43 6f 6c 6c 20 3d 20 70 4c Root->pColl = pL
20986 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 eft->pColl;.
20987 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 65 78 }. }. ex
20988 70 72 53 65 74 48 65 69 67 68 74 28 70 52 6f 6f prSetHeight(pRoo
20989 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a t);. }.}../*.**
2098a 20 41 6c 6c 6f 63 61 74 65 20 61 20 45 78 70 72 Allocate a Expr
2098b 20 6e 6f 64 65 20 77 68 69 63 68 20 6a 6f 69 6e node which join
2098c 73 20 61 73 20 6d 61 6e 79 20 61 73 20 74 77 6f s as many as two
2098d 20 73 75 62 74 72 65 65 73 2e 0a 2a 2a 0a 2a 2a subtrees..**.**
2098e 20 4f 6e 65 20 6f 72 20 62 6f 74 68 20 6f 66 20 One or both of
2098f 74 68 65 20 73 75 62 74 72 65 65 73 20 63 61 6e the subtrees can
20990 20 62 65 20 4e 55 4c 4c 2e 20 20 52 65 74 75 72 be NULL. Retur
20991 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 n a pointer to t
20992 68 65 20 6e 65 77 0a 2a 2a 20 45 78 70 72 20 6e he new.** Expr n
20993 6f 64 65 2e 20 20 4f 72 2c 20 69 66 20 61 6e 20 ode. Or, if an
20994 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75 72 73 OOM error occurs
20995 2c 20 73 65 74 20 70 50 61 72 73 65 2d 3e 64 62 , set pParse->db
20996 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 2c 0a ->mallocFailed,.
20997 2a 2a 20 66 72 65 65 20 74 68 65 20 73 75 62 74 ** free the subt
20998 72 65 65 73 20 61 6e 64 20 72 65 74 75 72 6e 20 rees and return
20999 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f NULL..*/.SQLITE_
2099a 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 PRIVATE Expr *sq
2099b 6c 69 74 65 33 50 45 78 70 72 28 0a 20 20 50 61 lite3PExpr(. Pa
2099c 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
2099d 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 /* Parsing
2099e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e context */. in
2099f 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 t op,
209a0 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 /* Express
209a1 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 ion opcode */.
209a2 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 Expr *pLeft,
209a3 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 /* Left
209a4 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 operand */. Exp
209a5 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20 20 r *pRight,
209a6 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 /* Right op
209a7 65 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 erand */. const
209a8 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 20 Token *pToken
209a9 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 /* Argument t
209aa 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 oken */.){. Exp
209ab 72 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 45 78 r *p = sqlite3Ex
209ac 70 72 41 6c 6c 6f 63 28 70 50 61 72 73 65 2d 3e prAlloc(pParse->
209ad 64 62 2c 20 6f 70 2c 20 70 54 6f 6b 65 6e 2c 20 db, op, pToken,
209ae 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 1);. sqlite3Exp
209af 72 41 74 74 61 63 68 53 75 62 74 72 65 65 73 28 rAttachSubtrees(
209b0 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 2c 20 70 pParse->db, p, p
209b1 4c 65 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 Left, pRight);.
209b2 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
209b3 0a 2a 2a 20 4a 6f 69 6e 20 74 77 6f 20 65 78 70 .** Join two exp
209b4 72 65 73 73 69 6f 6e 73 20 75 73 69 6e 67 20 61 ressions using a
209b5 6e 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 20 n AND operator.
209b6 20 49 66 20 65 69 74 68 65 72 20 65 78 70 72 65 If either expre
209b7 73 73 69 6f 6e 20 69 73 0a 2a 2a 20 4e 55 4c 4c ssion is.** NULL
209b8 2c 20 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75 , then just retu
209b9 72 6e 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 rn the other exp
209ba 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 ression..*/.SQLI
209bb 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
209bc 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 *sqlite3ExprAnd(
209bd 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 sqlite3 *db, Exp
209be 72 20 2a 70 4c 65 66 74 2c 20 45 78 70 72 20 2a r *pLeft, Expr *
209bf 70 52 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 pRight){. if( p
209c0 4c 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 Left==0 ){. r
209c1 65 74 75 72 6e 20 70 52 69 67 68 74 3b 0a 20 20 eturn pRight;.
209c2 7d 65 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 }else if( pRight
209c3 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
209c4 6e 20 70 4c 65 66 74 3b 0a 20 20 7d 65 6c 73 65 n pLeft;. }else
209c5 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 {. Expr *pNew
209c6 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6c = sqlite3ExprAl
209c7 6c 6f 63 28 64 62 2c 20 54 4b 5f 41 4e 44 2c 20 loc(db, TK_AND,
209c8 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 0, 0);. sqlit
209c9 65 33 45 78 70 72 41 74 74 61 63 68 53 75 62 74 e3ExprAttachSubt
209ca 72 65 65 73 28 64 62 2c 20 70 4e 65 77 2c 20 70 rees(db, pNew, p
209cb 4c 65 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 Left, pRight);.
209cc 20 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a return pNew;.
209cd 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e }.}../*.** Con
209ce 73 74 72 75 63 74 20 61 20 6e 65 77 20 65 78 70 struct a new exp
209cf 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 ression node for
209d0 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 a function with
209d1 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 61 72 67 multiple.** arg
209d2 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 uments..*/.SQLIT
209d3 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a E_PRIVATE Expr *
209d4 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 sqlite3ExprFunct
209d5 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 ion(Parse *pPars
209d6 65 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 e, ExprList *pLi
209d7 73 74 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 st, Token *pToke
209d8 6e 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 n){. Expr *pNew
209d9 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
209da 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
209db 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 20 29 assert( pToken )
209dc 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 ;. pNew = sqlit
209dd 65 33 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20 e3ExprAlloc(db,
209de 54 4b 5f 46 55 4e 43 54 49 4f 4e 2c 20 70 54 6f TK_FUNCTION, pTo
209df 6b 65 6e 2c 20 31 29 3b 0a 20 20 69 66 28 20 70 ken, 1);. if( p
209e0 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 New==0 ){. sq
209e1 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c lite3ExprListDel
209e2 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 20 ete(db, pList);
209e3 2f 2a 20 41 76 6f 69 64 20 6d 65 6d 6f 72 79 20 /* Avoid memory
209e4 6c 65 61 6b 20 77 68 65 6e 20 6d 61 6c 6c 6f 63 leak when malloc
209e5 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 72 65 fails */. re
209e6 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e turn 0;. }. pN
209e7 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 70 4c ew->x.pList = pL
209e8 69 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 21 ist;. assert( !
209e9 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
209ea 70 4e 65 77 2c 20 45 50 5f 78 49 73 53 65 6c 65 pNew, EP_xIsSele
209eb 63 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ct) );. sqlite3
209ec 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50 ExprSetHeight(pP
209ed 61 72 73 65 2c 20 70 4e 65 77 29 3b 0a 20 20 72 arse, pNew);. r
209ee 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f eturn pNew;.}../
209ef 2a 0a 2a 2a 20 41 73 73 69 67 6e 20 61 20 76 61 *.** Assign a va
209f0 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 74 6f riable number to
209f1 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 an expression t
209f2 68 61 74 20 65 6e 63 6f 64 65 73 20 61 20 77 69 hat encodes a wi
209f3 6c 64 63 61 72 64 0a 2a 2a 20 69 6e 20 74 68 65 ldcard.** in the
209f4 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 original SQL st
209f5 61 74 65 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a atement. .**.**
209f6 20 57 69 6c 64 63 61 72 64 73 20 63 6f 6e 73 69 Wildcards consi
209f7 73 74 69 6e 67 20 6f 66 20 61 20 73 69 6e 67 6c sting of a singl
209f8 65 20 22 3f 22 20 61 72 65 20 61 73 73 69 67 6e e "?" are assign
209f9 65 64 20 74 68 65 20 6e 65 78 74 20 73 65 71 75 ed the next sequ
209fa 65 6e 74 69 61 6c 0a 2a 2a 20 76 61 72 69 61 62 ential.** variab
209fb 6c 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a le number..**.**
209fc 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68 Wildcards of th
209fd 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 20 61 72 e form "?nnn" ar
209fe 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 20 6e e assigned the n
209ff 75 6d 62 65 72 20 22 6e 6e 6e 22 2e 20 20 57 65 umber "nnn". We
20a00 20 6d 61 6b 65 0a 2a 2a 20 73 75 72 65 20 22 6e make.** sure "n
20a01 6e 6e 22 20 69 73 20 6e 6f 74 20 74 6f 6f 20 62 nn" is not too b
20a02 65 20 74 6f 20 61 76 6f 69 64 20 61 20 64 65 6e e to avoid a den
20a03 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65 20 61 ial of service a
20a04 74 74 61 63 6b 20 77 68 65 6e 0a 2a 2a 20 74 68 ttack when.** th
20a05 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 e SQL statement
20a06 63 6f 6d 65 73 20 66 72 6f 6d 20 61 6e 20 65 78 comes from an ex
20a07 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 2e 0a 2a ternal source..*
20a08 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f *.** Wildcards o
20a09 66 20 74 68 65 20 66 6f 72 6d 20 22 3a 61 61 61 f the form ":aaa
20a0a 22 2c 20 22 40 61 61 61 22 2c 20 6f 72 20 22 24 ", "@aaa", or "$
20a0b 61 61 61 22 20 61 72 65 20 61 73 73 69 67 6e 65 aaa" are assigne
20a0c 64 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 d the same numbe
20a0d 72 0a 2a 2a 20 61 73 20 74 68 65 20 70 72 65 76 r.** as the prev
20a0e 69 6f 75 73 20 69 6e 73 74 61 6e 63 65 20 6f 66 ious instance of
20a0f 20 74 68 65 20 73 61 6d 65 20 77 69 6c 64 63 61 the same wildca
20a10 72 64 2e 20 20 4f 72 20 69 66 20 74 68 69 73 20 rd. Or if this
20a11 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 is the first.**
20a12 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
20a13 77 69 6c 64 63 61 72 64 2c 20 74 68 65 20 6e 65 wildcard, the ne
20a14 78 74 20 73 65 71 75 65 6e 69 61 6c 20 76 61 72 xt sequenial var
20a15 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 69 73 0a iable number is.
20a16 2a 2a 20 61 73 73 69 67 6e 65 64 2e 0a 2a 2f 0a ** assigned..*/.
20a17 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
20a18 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 oid sqlite3ExprA
20a19 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 50 ssignVarNumber(P
20a1a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 arse *pParse, Ex
20a1b 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 73 71 pr *pExpr){. sq
20a1c 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
20a1d 73 65 2d 3e 64 62 3b 0a 20 20 63 6f 6e 73 74 20 se->db;. const
20a1e 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 69 66 28 20 char *z;.. if(
20a1f 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 pExpr==0 ) retur
20a20 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 n;. assert( !Ex
20a21 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 prHasAnyProperty
20a22 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 (pExpr, EP_IntVa
20a23 6c 75 65 7c 45 50 5f 52 65 64 75 63 65 64 7c 45 lue|EP_Reduced|E
20a24 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 3b 0a P_TokenOnly) );.
20a25 20 20 7a 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a z = pExpr->u.z
20a26 54 6f 6b 65 6e 3b 0a 20 20 61 73 73 65 72 74 28 Token;. assert(
20a27 20 7a 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 z!=0 );. asser
20a28 74 28 20 7a 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 t( z[0]!=0 );.
20a29 69 66 28 20 7a 5b 31 5d 3d 3d 30 20 29 7b 0a 20 if( z[1]==0 ){.
20a2a 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f /* Wildcard o
20a2b 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 2e 20 f the form "?".
20a2c 20 41 73 73 69 67 6e 20 74 68 65 20 6e 65 78 74 Assign the next
20a2d 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 variable number
20a2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
20a2f 7a 5b 30 5d 3d 3d 27 3f 27 20 29 3b 0a 20 20 20 z[0]=='?' );.
20a30 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 pExpr->iColumn
20a31 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72 = ++pParse->nVar
20a32 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 5b ;. }else if( z[
20a33 30 5d 3d 3d 27 3f 27 20 29 7b 0a 20 20 20 20 2f 0]=='?' ){. /
20a34 2a 20 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68 * Wildcard of th
20a35 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 2e 20 20 e form "?nnn".
20a36 43 6f 6e 76 65 72 74 20 22 6e 6e 6e 22 20 74 6f Convert "nnn" to
20a37 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 0a an integer and.
20a38 20 20 20 20 2a 2a 20 75 73 65 20 69 74 20 61 73 ** use it as
20a39 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 6e 75 the variable nu
20a3a 6d 62 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 mber */. int
20a3b 69 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 43 i;. pExpr->iC
20a3c 6f 6c 75 6d 6e 20 3d 20 69 20 3d 20 61 74 6f 69 olumn = i = atoi
20a3d 28 28 63 68 61 72 2a 29 26 7a 5b 31 5d 29 3b 0a ((char*)&z[1]);.
20a3e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d testcase( i=
20a3f 3d 30 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 =0 );. testca
20a40 73 65 28 20 69 3d 3d 31 20 29 3b 0a 20 20 20 20 se( i==1 );.
20a41 74 65 73 74 63 61 73 65 28 20 69 3d 3d 64 62 2d testcase( i==db-
20a42 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
20a43 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 IMIT_VARIABLE_NU
20a44 4d 42 45 52 5d 2d 31 20 29 3b 0a 20 20 20 20 74 MBER]-1 );. t
20a45 65 73 74 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e estcase( i==db->
20a46 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
20a47 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d MIT_VARIABLE_NUM
20a48 42 45 52 5d 20 29 3b 0a 20 20 20 20 69 66 28 20 BER] );. if(
20a49 69 3c 31 20 7c 7c 20 69 3e 64 62 2d 3e 61 4c 69 i<1 || i>db->aLi
20a4a 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
20a4b 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 _VARIABLE_NUMBER
20a4c 5d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ] ){. sqlit
20a4d 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
20a4e 65 2c 20 22 76 61 72 69 61 62 6c 65 20 6e 75 6d e, "variable num
20a4f 62 65 72 20 6d 75 73 74 20 62 65 20 62 65 74 77 ber must be betw
20a50 65 65 6e 20 3f 31 20 61 6e 64 20 3f 25 64 22 2c een ?1 and ?%d",
20a51 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 . db->a
20a52 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
20a53 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 IT_VARIABLE_NUMB
20a54 45 52 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ER]);. }.
20a55 69 66 28 20 69 3e 70 50 61 72 73 65 2d 3e 6e 56 if( i>pParse->nV
20a56 61 72 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 ar ){. pPar
20a57 73 65 2d 3e 6e 56 61 72 20 3d 20 69 3b 0a 20 20 se->nVar = i;.
20a58 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
20a59 20 2f 2a 20 57 69 6c 64 63 61 72 64 73 20 6c 69 /* Wildcards li
20a5a 6b 65 20 22 3a 61 61 61 22 2c 20 22 24 61 61 61 ke ":aaa", "$aaa
20a5b 22 20 6f 72 20 22 40 61 61 61 22 2e 20 20 52 65 " or "@aaa". Re
20a5c 75 73 65 20 74 68 65 20 73 61 6d 65 20 76 61 72 use the same var
20a5d 69 61 62 6c 65 0a 20 20 20 20 2a 2a 20 6e 75 6d iable. ** num
20a5e 62 65 72 20 61 73 20 74 68 65 20 70 72 69 6f 72 ber as the prior
20a5f 20 61 70 70 65 61 72 61 6e 63 65 20 6f 66 20 74 appearance of t
20a60 68 65 20 73 61 6d 65 20 6e 61 6d 65 2c 20 6f 72 he same name, or
20a61 20 69 66 20 74 68 65 20 6e 61 6d 65 0a 20 20 20 if the name.
20a62 20 2a 2a 20 68 61 73 20 6e 65 76 65 72 20 61 70 ** has never ap
20a63 70 65 61 72 65 64 20 62 65 66 6f 72 65 2c 20 72 peared before, r
20a64 65 75 73 65 20 74 68 65 20 73 61 6d 65 20 76 61 euse the same va
20a65 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 0a 20 20 riable number.
20a66 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 0a */. int i;.
20a67 20 20 20 20 75 33 32 20 6e 3b 0a 20 20 20 20 6e u32 n;. n
20a68 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
20a69 33 30 28 7a 29 3b 0a 20 20 20 20 66 6f 72 28 69 30(z);. for(i
20a6a 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 56 =0; i<pParse->nV
20a6b 61 72 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 arExpr; i++){.
20a6c 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70 Expr *pE = p
20a6d 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 Parse->apVarExpr
20a6e 5b 69 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 [i];. asser
20a6f 74 28 20 70 45 21 3d 30 20 29 3b 0a 20 20 20 20 t( pE!=0 );.
20a70 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 45 2d if( memcmp(pE-
20a71 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 7a 2c 20 6e 29 >u.zToken, z, n)
20a72 3d 3d 30 20 26 26 20 70 45 2d 3e 75 2e 7a 54 6f ==0 && pE->u.zTo
20a73 6b 65 6e 5b 6e 5d 3d 3d 30 20 29 7b 0a 20 20 20 ken[n]==0 ){.
20a74 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c pExpr->iCol
20a75 75 6d 6e 20 3d 20 70 45 2d 3e 69 43 6f 6c 75 6d umn = pE->iColum
20a76 6e 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b n;. break
20a77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
20a78 20 20 20 20 69 66 28 20 69 3e 3d 70 50 61 72 73 if( i>=pPars
20a79 65 2d 3e 6e 56 61 72 45 78 70 72 20 29 7b 0a 20 e->nVarExpr ){.
20a7a 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c pExpr->iCol
20a7b 75 6d 6e 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e umn = ++pParse->
20a7c 6e 56 61 72 3b 0a 20 20 20 20 20 20 69 66 28 20 nVar;. if(
20a7d 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 pParse->nVarExpr
20a7e 3e 3d 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 >=pParse->nVarEx
20a7f 70 72 41 6c 6c 6f 63 2d 31 20 29 7b 0a 20 20 20 prAlloc-1 ){.
20a80 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 pParse->nVa
20a81 72 45 78 70 72 41 6c 6c 6f 63 20 2b 3d 20 70 50 rExprAlloc += pP
20a82 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c arse->nVarExprAl
20a83 6c 6f 63 20 2b 20 31 30 3b 0a 20 20 20 20 20 20 loc + 10;.
20a84 20 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 pParse->apVarE
20a85 78 70 72 20 3d 0a 20 20 20 20 20 20 20 20 20 20 xpr =.
20a86 20 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c sqlite3DbReall
20a87 6f 63 4f 72 46 72 65 65 28 0a 20 20 20 20 20 20 ocOrFree(.
20a88 20 20 20 20 20 20 20 20 64 62 2c 0a 20 20 20 20 db,.
20a89 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 pParse
20a8a 2d 3e 61 70 56 61 72 45 78 70 72 2c 0a 20 20 20 ->apVarExpr,.
20a8b 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 pPars
20a8c 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 e->nVarExprAlloc
20a8d 2a 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e *sizeof(pParse->
20a8e 61 70 56 61 72 45 78 70 72 5b 30 5d 29 0a 20 20 apVarExpr[0]).
20a8f 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 );.
20a90 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 }. if( !
20a91 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
20a92 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
20a93 72 74 28 20 70 50 61 72 73 65 2d 3e 61 70 56 61 rt( pParse->apVa
20a94 72 45 78 70 72 21 3d 30 20 29 3b 0a 20 20 20 20 rExpr!=0 );.
20a95 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56 61 pParse->apVa
20a96 72 45 78 70 72 5b 70 50 61 72 73 65 2d 3e 6e 56 rExpr[pParse->nV
20a97 61 72 45 78 70 72 2b 2b 5d 20 3d 20 70 45 78 70 arExpr++] = pExp
20a98 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d r;. }. }
20a99 0a 20 20 7d 20 0a 20 20 69 66 28 20 21 70 50 61 . } . if( !pPa
20a9a 72 73 65 2d 3e 6e 45 72 72 20 26 26 20 70 50 61 rse->nErr && pPa
20a9b 72 73 65 2d 3e 6e 56 61 72 3e 64 62 2d 3e 61 4c rse->nVar>db->aL
20a9c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
20a9d 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 T_VARIABLE_NUMBE
20a9e 52 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 R] ){. sqlite
20a9f 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
20aa0 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 53 51 4c 20 , "too many SQL
20aa1 76 61 72 69 61 62 6c 65 73 22 29 3b 0a 20 20 7d variables");. }
20aa2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 .}../*.** Clear
20aa3 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73 74 an expression st
20aa4 72 75 63 74 75 72 65 20 77 69 74 68 6f 75 74 20 ructure without
20aa5 64 65 6c 65 74 69 6e 67 20 74 68 65 20 73 74 72 deleting the str
20aa6 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a 2a ucture itself..*
20aa7 2a 20 53 75 62 73 74 72 75 63 74 75 72 65 20 69 * Substructure i
20aa8 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 53 51 s deleted..*/.SQ
20aa9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
20aaa 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 d sqlite3ExprCle
20aab 61 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ar(sqlite3 *db,
20aac 45 78 70 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 Expr *p){. asse
20aad 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 rt( p!=0 );. if
20aae 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f ( !ExprHasAnyPro
20aaf 70 65 72 74 79 28 70 2c 20 45 50 5f 54 6f 6b 65 perty(p, EP_Toke
20ab0 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 73 71 nOnly) ){. sq
20ab1 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
20ab2 64 62 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 db, p->pLeft);.
20ab3 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 sqlite3ExprDe
20ab4 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 52 69 67 lete(db, p->pRig
20ab5 68 74 29 3b 0a 20 20 20 20 69 66 28 20 21 45 78 ht);. if( !Ex
20ab6 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c prHasProperty(p,
20ab7 20 45 50 5f 52 65 64 75 63 65 64 29 20 26 26 20 EP_Reduced) &&
20ab8 28 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32 (p->flags2 & EP2
20ab9 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 21 _MallocedToken)!
20aba 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
20abb 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
20abc 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 >u.zToken);.
20abd 7d 0a 20 20 20 20 69 66 28 20 45 78 70 72 48 61 }. if( ExprHa
20abe 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f sProperty(p, EP_
20abf 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 xIsSelect) ){.
20ac0 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 sqlite3Selec
20ac1 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 78 tDelete(db, p->x
20ac2 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 7d .pSelect);. }
20ac3 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 else{. sqli
20ac4 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 te3ExprListDelet
20ac5 65 28 64 62 2c 20 70 2d 3e 78 2e 70 4c 69 73 74 e(db, p->x.pList
20ac6 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a );. }. }.}..
20ac7 2f 2a 0a 2a 2a 20 52 65 63 75 72 73 69 76 65 6c /*.** Recursivel
20ac8 79 20 64 65 6c 65 74 65 20 61 6e 20 65 78 70 72 y delete an expr
20ac9 65 73 73 69 6f 6e 20 74 72 65 65 2e 0a 2a 2f 0a ession tree..*/.
20aca 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
20acb 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 44 oid sqlite3ExprD
20acc 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 elete(sqlite3 *d
20acd 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 b, Expr *p){. i
20ace 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
20acf 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 ;. sqlite3ExprC
20ad0 6c 65 61 72 28 64 62 2c 20 70 29 3b 0a 20 20 69 lear(db, p);. i
20ad1 66 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 f( !ExprHasPrope
20ad2 72 74 79 28 70 2c 20 45 50 5f 53 74 61 74 69 63 rty(p, EP_Static
20ad3 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) ){. sqlite3
20ad4 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 DbFree(db, p);.
20ad5 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 }.}../*.** Retu
20ad6 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
20ad7 20 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 bytes allocated
20ad8 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 73 for the express
20ad9 69 6f 6e 20 73 74 72 75 63 74 75 72 65 20 0a 2a ion structure .*
20ada 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 * passed as the
20adb 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 first argument.
20adc 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 6f This is always o
20add 6e 65 20 6f 66 20 45 58 50 52 5f 46 55 4c 4c 53 ne of EXPR_FULLS
20ade 49 5a 45 2c 0a 2a 2a 20 45 58 50 52 5f 52 45 44 IZE,.** EXPR_RED
20adf 55 43 45 44 53 49 5a 45 20 6f 72 20 45 58 50 52 UCEDSIZE or EXPR
20ae0 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 2e 0a _TOKENONLYSIZE..
20ae1 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78 */.static int ex
20ae2 70 72 53 74 72 75 63 74 53 69 7a 65 28 45 78 70 prStructSize(Exp
20ae3 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 45 78 70 r *p){. if( Exp
20ae4 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 rHasProperty(p,
20ae5 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 20 EP_TokenOnly) )
20ae6 72 65 74 75 72 6e 20 45 58 50 52 5f 54 4f 4b 45 return EXPR_TOKE
20ae7 4e 4f 4e 4c 59 53 49 5a 45 3b 0a 20 20 69 66 28 NONLYSIZE;. if(
20ae8 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
20ae9 28 70 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 (p, EP_Reduced)
20aea 29 20 72 65 74 75 72 6e 20 45 58 50 52 5f 52 45 ) return EXPR_RE
20aeb 44 55 43 45 44 53 49 5a 45 3b 0a 20 20 72 65 74 DUCEDSIZE;. ret
20aec 75 72 6e 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a urn EXPR_FULLSIZ
20aed 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 E;.}../*.** The
20aee 64 75 70 65 64 45 78 70 72 2a 53 69 7a 65 28 29 dupedExpr*Size()
20aef 20 72 6f 75 74 69 6e 65 73 20 65 61 63 68 20 72 routines each r
20af0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
20af1 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 72 of bytes requir
20af2 65 64 0a 2a 2a 20 74 6f 20 73 74 6f 72 65 20 61 ed.** to store a
20af3 20 63 6f 70 79 20 6f 66 20 61 6e 20 65 78 70 72 copy of an expr
20af4 65 73 73 69 6f 6e 20 6f 72 20 65 78 70 72 65 73 ession or expres
20af5 73 69 6f 6e 20 74 72 65 65 2e 20 20 54 68 65 79 sion tree. They
20af6 20 64 69 66 66 65 72 20 69 6e 0a 2a 2a 20 68 6f differ in.** ho
20af7 77 20 6d 75 63 68 20 6f 66 20 74 68 65 20 74 72 w much of the tr
20af8 65 65 20 69 73 20 6d 65 61 73 75 72 65 64 2e 0a ee is measured..
20af9 2a 2a 0a 2a 2a 20 20 20 20 20 64 75 70 65 64 45 **.** dupedE
20afa 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 29 20 xprStructSize()
20afb 20 20 20 20 53 69 7a 65 20 6f 66 20 6f 6e 6c 79 Size of only
20afc 20 74 68 65 20 45 78 70 72 20 73 74 72 75 63 74 the Expr struct
20afd 75 72 65 20 0a 2a 2a 20 20 20 20 20 64 75 70 65 ure .** dupe
20afe 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 29 20 dExprNodeSize()
20aff 20 20 20 20 20 20 53 69 7a 65 20 6f 66 20 45 78 Size of Ex
20b00 70 72 20 2b 20 73 70 61 63 65 20 66 6f 72 20 74 pr + space for t
20b01 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 64 75 70 65 oken.** dupe
20b02 64 45 78 70 72 53 69 7a 65 28 29 20 20 20 20 20 dExprSize()
20b03 20 20 20 20 20 20 45 78 70 72 20 2b 20 74 6f 6b Expr + tok
20b04 65 6e 20 2b 20 73 75 62 74 72 65 65 20 63 6f 6d en + subtree com
20b05 70 6f 6e 65 6e 74 73 0a 2a 2a 0a 2a 2a 2a 2a 2a ponents.**.*****
20b06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b0a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 ******.**.** The
20b0b 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63 74 dupedExprStruct
20b0c 53 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 Size() function
20b0d 72 65 74 75 72 6e 73 20 74 77 6f 20 76 61 6c 75 returns two valu
20b0e 65 73 20 4f 52 2d 65 64 20 74 6f 67 65 74 68 65 es OR-ed togethe
20b0f 72 3a 20 20 0a 2a 2a 20 28 31 29 20 74 68 65 20 r: .** (1) the
20b10 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66 space required f
20b11 6f 72 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 or a copy of the
20b12 20 45 78 70 72 20 73 74 72 75 63 74 75 72 65 20 Expr structure
20b13 6f 6e 6c 79 20 61 6e 64 20 0a 2a 2a 20 28 32 29 only and .** (2)
20b14 20 74 68 65 20 45 50 5f 78 78 78 20 66 6c 61 67 the EP_xxx flag
20b15 73 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 20 s that indicate
20b16 77 68 61 74 20 74 68 65 20 73 74 72 75 63 74 75 what the structu
20b17 72 65 20 73 69 7a 65 20 73 68 6f 75 6c 64 20 62 re size should b
20b18 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e e..** The return
20b19 20 76 61 6c 75 65 73 20 69 73 20 61 6c 77 61 79 values is alway
20b1a 73 20 6f 6e 65 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20 s one of:.**.**
20b1b 20 20 20 20 20 45 58 50 52 5f 46 55 4c 4c 53 49 EXPR_FULLSI
20b1c 5a 45 0a 2a 2a 20 20 20 20 20 20 45 58 50 52 5f ZE.** EXPR_
20b1d 52 45 44 55 43 45 44 53 49 5a 45 20 20 20 7c 20 REDUCEDSIZE |
20b1e 45 50 5f 52 65 64 75 63 65 64 0a 2a 2a 20 20 20 EP_Reduced.**
20b1f 20 20 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c EXPR_TOKENONL
20b20 59 53 49 5a 45 20 7c 20 45 50 5f 54 6f 6b 65 6e YSIZE | EP_Token
20b21 4f 6e 6c 79 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 Only.**.** The s
20b22 69 7a 65 20 6f 66 20 74 68 65 20 73 74 72 75 63 ize of the struc
20b23 74 75 72 65 20 63 61 6e 20 62 65 20 66 6f 75 6e ture can be foun
20b24 64 20 62 79 20 6d 61 73 6b 69 6e 67 20 74 68 65 d by masking the
20b25 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a return value.**
20b26 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 of this routine
20b27 20 77 69 74 68 20 30 78 66 66 66 2e 20 20 54 68 with 0xfff. Th
20b28 65 20 66 6c 61 67 73 20 63 61 6e 20 62 65 20 66 e flags can be f
20b29 6f 75 6e 64 20 62 79 20 6d 61 73 6b 69 6e 67 20 ound by masking
20b2a 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 the.** return va
20b2b 6c 75 65 20 77 69 74 68 20 45 50 5f 52 65 64 75 lue with EP_Redu
20b2c 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 ced|EP_TokenOnly
20b2d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
20b2e 74 20 77 69 74 68 20 66 6c 61 67 73 3d 3d 45 58 t with flags==EX
20b2f 50 52 44 55 50 5f 52 45 44 55 43 45 2c 20 74 68 PRDUP_REDUCE, th
20b30 69 73 20 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b is routines work
20b31 73 20 6f 6e 20 66 75 6c 6c 2d 73 69 7a 65 0a 2a s on full-size.*
20b32 2a 20 28 75 6e 72 65 64 75 63 65 64 29 20 45 78 * (unreduced) Ex
20b33 70 72 20 6f 62 6a 65 63 74 73 20 61 73 20 74 68 pr objects as th
20b34 65 79 20 6f 72 20 6f 72 69 67 69 6e 61 6c 6c 79 ey or originally
20b35 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 constructed by
20b36 74 68 65 20 70 61 72 73 65 72 2e 0a 2a 2a 20 44 the parser..** D
20b37 75 72 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e uring expression
20b38 20 61 6e 61 6c 79 73 69 73 2c 20 65 78 74 72 61 analysis, extra
20b39 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 information is
20b3a 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 6d 6f 76 computed and mov
20b3b 65 64 20 69 6e 74 6f 0a 2a 2a 20 6c 61 74 65 72 ed into.** later
20b3c 20 70 61 72 74 73 20 6f 66 20 74 65 68 20 45 78 parts of teh Ex
20b3d 70 72 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 pr object and th
20b3e 61 74 20 65 78 74 72 61 20 69 6e 66 6f 72 6d 61 at extra informa
20b3f 74 69 6f 6e 20 6d 69 67 68 74 20 67 65 74 20 63 tion might get c
20b40 68 6f 70 70 65 64 0a 2a 2a 20 6f 66 66 20 69 66 hopped.** off if
20b41 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
20b42 69 73 20 72 65 64 75 63 65 64 2e 20 20 4e 6f 74 is reduced. Not
20b43 65 20 61 6c 73 6f 20 74 68 61 74 20 69 74 20 64 e also that it d
20b44 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 74 6f 0a oes not work to.
20b45 2a 2a 20 6d 61 6b 65 20 61 20 45 58 50 52 44 55 ** make a EXPRDU
20b46 50 5f 52 45 44 55 43 45 20 63 6f 70 79 20 6f 66 P_REDUCE copy of
20b47 20 61 20 72 65 64 75 63 65 64 20 65 78 70 72 65 a reduced expre
20b48 73 73 69 6f 6e 2e 20 20 49 74 20 69 73 20 6f 6e ssion. It is on
20b49 6c 79 20 6c 65 67 61 6c 0a 2a 2a 20 74 6f 20 72 ly legal.** to r
20b4a 65 64 75 63 65 20 61 20 70 72 69 73 74 69 6e 65 educe a pristine
20b4b 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 expression tree
20b4c 20 66 72 6f 6d 20 74 68 65 20 70 61 72 73 65 72 from the parser
20b4d 2e 20 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 . The implement
20b4e 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 64 75 70 65 ation.** of dupe
20b4f 64 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 dExprStructSize(
20b50 29 20 63 6f 6e 74 61 69 6e 20 6d 75 6c 74 69 70 ) contain multip
20b51 6c 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 le assert() stat
20b52 65 6d 65 6e 74 73 20 74 68 61 74 20 61 74 74 65 ements that atte
20b53 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 66 6f 72 63 mpt.** to enforc
20b54 65 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e e this constrain
20b55 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
20b56 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63 74 dupedExprStruct
20b57 53 69 7a 65 28 45 78 70 72 20 2a 70 2c 20 69 6e Size(Expr *p, in
20b58 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 t flags){. int
20b59 6e 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 nSize;. assert(
20b5a 20 66 6c 61 67 73 3d 3d 45 58 50 52 44 55 50 5f flags==EXPRDUP_
20b5b 52 45 44 55 43 45 20 7c 7c 20 66 6c 61 67 73 3d REDUCE || flags=
20b5c 3d 30 20 29 3b 20 2f 2a 20 4f 6e 6c 79 20 6f 6e =0 ); /* Only on
20b5d 65 20 66 6c 61 67 20 76 61 6c 75 65 20 61 6c 6c e flag value all
20b5e 6f 77 65 64 20 2a 2f 0a 20 20 69 66 28 20 30 3d owed */. if( 0=
20b5f 3d 28 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f =(flags&EXPRDUP_
20b60 52 45 44 55 43 45 29 20 29 7b 0a 20 20 20 20 6e REDUCE) ){. n
20b61 53 69 7a 65 20 3d 20 45 58 50 52 5f 46 55 4c 4c Size = EXPR_FULL
20b62 53 49 5a 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 SIZE;. }else{.
20b63 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 assert( !Expr
20b64 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 HasAnyProperty(p
20b65 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 , EP_TokenOnly|E
20b66 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20 20 P_Reduced) );.
20b67 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 assert( !ExprH
20b68 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 asProperty(p, EP
20b69 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 3b 20 0a 20 _FromJoin) ); .
20b6a 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66 assert( (p->f
20b6b 6c 61 67 73 32 20 26 20 45 50 32 5f 4d 61 6c 6c lags2 & EP2_Mall
20b6c 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d 30 20 29 3b ocedToken)==0 );
20b6d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d . assert( (p-
20b6e 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f 49 72 >flags2 & EP2_Ir
20b6f 72 65 64 75 63 69 62 6c 65 29 3d 3d 30 20 29 3b reducible)==0 );
20b70 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 65 66 . if( p->pLef
20b71 74 20 7c 7c 20 70 2d 3e 70 52 69 67 68 74 20 7c t || p->pRight |
20b72 7c 20 70 2d 3e 70 43 6f 6c 6c 20 7c 7c 20 70 2d | p->pColl || p-
20b73 3e 78 2e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 >x.pList ){.
20b74 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f 52 nSize = EXPR_R
20b75 45 44 55 43 45 44 53 49 5a 45 20 7c 20 45 50 5f EDUCEDSIZE | EP_
20b76 52 65 64 75 63 65 64 3b 0a 20 20 20 20 7d 65 6c Reduced;. }el
20b77 73 65 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 se{. nSize
20b78 3d 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 = EXPR_TOKENONLY
20b79 53 49 5a 45 20 7c 20 45 50 5f 54 6f 6b 65 6e 4f SIZE | EP_TokenO
20b7a 6e 6c 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 nly;. }. }.
20b7b 20 72 65 74 75 72 6e 20 6e 53 69 7a 65 3b 0a 7d return nSize;.}
20b7c 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
20b7d 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 ction returns th
20b7e 65 20 73 70 61 63 65 20 69 6e 20 62 79 74 65 73 e space in bytes
20b7f 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f required to sto
20b80 72 65 20 74 68 65 20 63 6f 70 79 20 0a 2a 2a 20 re the copy .**
20b81 6f 66 20 74 68 65 20 45 78 70 72 20 73 74 72 75 of the Expr stru
20b82 63 74 75 72 65 20 61 6e 64 20 61 20 63 6f 70 79 cture and a copy
20b83 20 6f 66 20 74 68 65 20 45 78 70 72 2e 75 2e 7a of the Expr.u.z
20b84 54 6f 6b 65 6e 20 73 74 72 69 6e 67 20 28 69 66 Token string (if
20b85 20 74 68 61 74 0a 2a 2a 20 73 74 72 69 6e 67 20 that.** string
20b86 69 73 20 64 65 66 69 6e 65 64 2e 29 0a 2a 2f 0a is defined.).*/.
20b87 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 65 64 static int duped
20b88 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 45 78 70 ExprNodeSize(Exp
20b89 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 r *p, int flags)
20b8a 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 {. int nByte =
20b8b 64 75 70 65 64 45 78 70 72 53 74 72 75 63 74 53 dupedExprStructS
20b8c 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 20 26 20 ize(p, flags) &
20b8d 30 78 66 66 66 3b 0a 20 20 69 66 28 20 21 45 78 0xfff;. if( !Ex
20b8e 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c prHasProperty(p,
20b8f 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 26 26 EP_IntValue) &&
20b90 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 29 7b 0a p->u.zToken ){.
20b91 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 73 71 6c nByte += sql
20b92 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 2d 3e ite3Strlen30(p->
20b93 75 2e 7a 54 6f 6b 65 6e 29 2b 31 3b 0a 20 20 7d u.zToken)+1;. }
20b94 0a 20 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 . return ROUND8
20b95 28 6e 42 79 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a (nByte);.}../*.*
20b96 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
20b97 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 ber of bytes req
20b98 75 69 72 65 64 20 74 6f 20 63 72 65 61 74 65 20 uired to create
20b99 61 20 64 75 70 6c 69 63 61 74 65 20 6f 66 20 74 a duplicate of t
20b9a 68 65 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f he .** expressio
20b9b 6e 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 n passed as the
20b9c 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 first argument.
20b9d 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d The second argum
20b9e 65 6e 74 20 69 73 20 61 0a 2a 2a 20 6d 61 73 6b ent is a.** mask
20b9f 20 63 6f 6e 74 61 69 6e 69 6e 67 20 45 58 50 52 containing EXPR
20ba0 44 55 50 5f 58 58 58 20 66 6c 61 67 73 2e 0a 2a DUP_XXX flags..*
20ba1 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72 *.** The value r
20ba2 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64 65 73 eturned includes
20ba3 20 73 70 61 63 65 20 74 6f 20 63 72 65 61 74 65 space to create
20ba4 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 45 a copy of the E
20ba5 78 70 72 20 73 74 72 75 63 74 0a 2a 2a 20 69 74 xpr struct.** it
20ba6 73 65 6c 66 20 61 6e 64 20 74 68 65 20 62 75 66 self and the buf
20ba7 66 65 72 20 72 65 66 65 72 72 65 64 20 74 6f 20 fer referred to
20ba8 62 79 20 45 78 70 72 2e 75 2e 7a 54 6f 6b 65 6e by Expr.u.zToken
20ba9 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 , if any..**.**
20baa 49 66 20 74 68 65 20 45 58 50 52 44 55 50 5f 52 If the EXPRDUP_R
20bab 45 44 55 43 45 20 66 6c 61 67 20 69 73 20 73 65 EDUCE flag is se
20bac 74 2c 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 t, then the retu
20bad 72 6e 20 76 61 6c 75 65 20 69 6e 63 6c 75 64 65 rn value include
20bae 73 20 0a 2a 2a 20 73 70 61 63 65 20 74 6f 20 64 s .** space to d
20baf 75 70 6c 69 63 61 74 65 20 61 6c 6c 20 45 78 70 uplicate all Exp
20bb0 72 20 6e 6f 64 65 73 20 69 6e 20 74 68 65 20 74 r nodes in the t
20bb1 72 65 65 20 66 6f 72 6d 65 64 20 62 79 20 45 78 ree formed by Ex
20bb2 70 72 2e 70 4c 65 66 74 20 0a 2a 2a 20 61 6e 64 pr.pLeft .** and
20bb3 20 45 78 70 72 2e 70 52 69 67 68 74 20 76 61 72 Expr.pRight var
20bb4 69 61 62 6c 65 73 20 28 62 75 74 20 6e 6f 74 20 iables (but not
20bb5 66 6f 72 20 61 6e 79 20 73 74 72 75 63 74 75 72 for any structur
20bb6 65 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 6f 72 es pointed to or
20bb7 20 0a 2a 2a 20 64 65 73 63 65 6e 64 65 64 20 66 .** descended f
20bb8 72 6f 6d 20 74 68 65 20 45 78 70 72 2e 78 2e 70 rom the Expr.x.p
20bb9 4c 69 73 74 20 6f 72 20 45 78 70 72 2e 78 2e 70 List or Expr.x.p
20bba 53 65 6c 65 63 74 20 76 61 72 69 61 62 6c 65 73 Select variables
20bbb 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
20bbc 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 28 45 dupedExprSize(E
20bbd 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 xpr *p, int flag
20bbe 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 s){. int nByte
20bbf 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a = 0;. if( p ){.
20bc0 20 20 20 20 6e 42 79 74 65 20 3d 20 64 75 70 65 nByte = dupe
20bc1 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 70 2c dExprNodeSize(p,
20bc2 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28 flags);. if(
20bc3 20 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f 52 flags&EXPRDUP_R
20bc4 45 44 55 43 45 20 29 7b 0a 20 20 20 20 20 20 6e EDUCE ){. n
20bc5 42 79 74 65 20 2b 3d 20 64 75 70 65 64 45 78 70 Byte += dupedExp
20bc6 72 53 69 7a 65 28 70 2d 3e 70 4c 65 66 74 2c 20 rSize(p->pLeft,
20bc7 66 6c 61 67 73 29 20 2b 20 64 75 70 65 64 45 78 flags) + dupedEx
20bc8 70 72 53 69 7a 65 28 70 2d 3e 70 52 69 67 68 74 prSize(p->pRight
20bc9 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a , flags);. }.
20bca 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 42 79 }. return nBy
20bcb 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 te;.}../*.** Thi
20bcc 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 69 s function is si
20bcd 6d 69 6c 61 72 20 74 6f 20 73 71 6c 69 74 65 33 milar to sqlite3
20bce 45 78 70 72 44 75 70 28 29 2c 20 65 78 63 65 70 ExprDup(), excep
20bcf 74 20 74 68 61 74 20 69 66 20 70 7a 42 75 66 66 t that if pzBuff
20bd0 65 72 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 er .** is not NU
20bd1 4c 4c 20 74 68 65 6e 20 2a 70 7a 42 75 66 66 65 LL then *pzBuffe
20bd2 72 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 r is assumed to
20bd3 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 point to a buffe
20bd4 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 0a r large enough .
20bd5 2a 2a 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 ** to store the
20bd6 63 6f 70 79 20 6f 66 20 65 78 70 72 65 73 73 69 copy of expressi
20bd7 6f 6e 20 70 2c 20 74 68 65 20 63 6f 70 69 65 73 on p, the copies
20bd8 20 6f 66 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 0a of p->u.zToken.
20bd9 2a 2a 20 28 69 66 20 61 70 70 6c 69 63 61 62 6c ** (if applicabl
20bda 65 29 2c 20 61 6e 64 20 74 68 65 20 63 6f 70 69 e), and the copi
20bdb 65 73 20 6f 66 20 74 68 65 20 70 2d 3e 70 4c 65 es of the p->pLe
20bdc 66 74 20 61 6e 64 20 70 2d 3e 70 52 69 67 68 74 ft and p->pRight
20bdd 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a 2a 2a expressions,.**
20bde 20 69 66 20 61 6e 79 2e 20 42 65 66 6f 72 65 20 if any. Before
20bdf 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 7a 42 75 returning, *pzBu
20be0 66 66 65 72 20 69 73 20 73 65 74 20 74 6f 20 74 ffer is set to t
20be1 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61 he first byte pa
20be2 73 73 65 64 20 74 68 65 0a 2a 2a 20 70 6f 72 74 ssed the.** port
20be3 69 6f 6e 20 6f 66 20 74 68 65 20 62 75 66 66 65 ion of the buffe
20be4 72 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 62 79 r copied into by
20be5 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a this function..
20be6 2a 2f 0a 73 74 61 74 69 63 20 45 78 70 72 20 2a */.static Expr *
20be7 65 78 70 72 44 75 70 28 73 71 6c 69 74 65 33 20 exprDup(sqlite3
20be8 2a 64 62 2c 20 45 78 70 72 20 2a 70 2c 20 69 6e *db, Expr *p, in
20be9 74 20 66 6c 61 67 73 2c 20 75 38 20 2a 2a 70 7a t flags, u8 **pz
20bea 42 75 66 66 65 72 29 7b 0a 20 20 45 78 70 72 20 Buffer){. Expr
20beb 2a 70 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 20 *pNew = 0;
20bec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20bed 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 /* Value to retu
20bee 72 6e 20 2a 2f 0a 20 20 69 66 28 20 70 20 29 7b rn */. if( p ){
20bef 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 . const int i
20bf0 73 52 65 64 75 63 65 64 20 3d 20 28 66 6c 61 67 sReduced = (flag
20bf1 73 26 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 s&EXPRDUP_REDUCE
20bf2 29 3b 0a 20 20 20 20 75 38 20 2a 7a 41 6c 6c 6f );. u8 *zAllo
20bf3 63 3b 0a 20 20 20 20 75 33 32 20 73 74 61 74 69 c;. u32 stati
20bf4 63 46 6c 61 67 20 3d 20 30 3b 0a 0a 20 20 20 20 cFlag = 0;..
20bf5 61 73 73 65 72 74 28 20 70 7a 42 75 66 66 65 72 assert( pzBuffer
20bf6 3d 3d 30 20 7c 7c 20 69 73 52 65 64 75 63 65 64 ==0 || isReduced
20bf7 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 );.. /* Figu
20bf8 72 65 20 6f 75 74 20 77 68 65 72 65 20 74 6f 20 re out where to
20bf9 77 72 69 74 65 20 74 68 65 20 6e 65 77 20 45 78 write the new Ex
20bfa 70 72 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f pr structure. */
20bfb 0a 20 20 20 20 69 66 28 20 70 7a 42 75 66 66 65 . if( pzBuffe
20bfc 72 20 29 7b 0a 20 20 20 20 20 20 7a 41 6c 6c 6f r ){. zAllo
20bfd 63 20 3d 20 2a 70 7a 42 75 66 66 65 72 3b 0a 20 c = *pzBuffer;.
20bfe 20 20 20 20 20 73 74 61 74 69 63 46 6c 61 67 20 staticFlag
20bff 3d 20 45 50 5f 53 74 61 74 69 63 3b 0a 20 20 20 = EP_Static;.
20c00 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 41 }else{. zA
20c01 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 lloc = sqlite3Db
20c02 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 64 75 MallocRaw(db, du
20c03 70 65 64 45 78 70 72 53 69 7a 65 28 70 2c 20 66 pedExprSize(p, f
20c04 6c 61 67 73 29 29 3b 0a 20 20 20 20 7d 0a 20 20 lags));. }.
20c05 20 20 70 4e 65 77 20 3d 20 28 45 78 70 72 20 2a pNew = (Expr *
20c06 29 7a 41 6c 6c 6f 63 3b 0a 0a 20 20 20 20 69 66 )zAlloc;.. if
20c07 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 ( pNew ){.
20c08 2f 2a 20 53 65 74 20 6e 4e 65 77 53 69 7a 65 20 /* Set nNewSize
20c09 74 6f 20 74 68 65 20 73 69 7a 65 20 61 6c 6c 6f to the size allo
20c0a 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 73 74 cated for the st
20c0b 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64 20 ructure pointed
20c0c 74 6f 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 70 to. ** by p
20c0d 4e 65 77 2e 20 54 68 69 73 20 69 73 20 65 69 74 New. This is eit
20c0e 68 65 72 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a her EXPR_FULLSIZ
20c0f 45 2c 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 E, EXPR_REDUCEDS
20c10 49 5a 45 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20 IZE or. **
20c11 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 EXPR_TOKENONLYSI
20c12 5a 45 2e 20 6e 54 6f 6b 65 6e 20 69 73 20 73 65 ZE. nToken is se
20c13 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 t to the number
20c14 6f 66 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65 of bytes consume
20c15 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 74 68 d. ** by th
20c16 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 70 2d e copy of the p-
20c17 3e 75 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 >u.zToken string
20c18 20 28 69 66 20 61 6e 79 29 2e 0a 20 20 20 20 20 (if any)..
20c19 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 */. const
20c1a 75 6e 73 69 67 6e 65 64 20 6e 53 74 72 75 63 74 unsigned nStruct
20c1b 53 69 7a 65 20 3d 20 64 75 70 65 64 45 78 70 72 Size = dupedExpr
20c1c 53 74 72 75 63 74 53 69 7a 65 28 70 2c 20 66 6c StructSize(p, fl
20c1d 61 67 73 29 3b 0a 20 20 20 20 20 20 63 6f 6e 73 ags);. cons
20c1e 74 20 69 6e 74 20 6e 4e 65 77 53 69 7a 65 20 3d t int nNewSize =
20c1f 20 6e 53 74 72 75 63 74 53 69 7a 65 20 26 20 30 nStructSize & 0
20c20 78 66 66 66 3b 0a 20 20 20 20 20 20 69 6e 74 20 xfff;. int
20c21 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 69 66 nToken;. if
20c22 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
20c23 74 79 28 70 2c 20 45 50 5f 49 6e 74 56 61 6c 75 ty(p, EP_IntValu
20c24 65 29 20 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b 65 e) && p->u.zToke
20c25 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 54 6f n ){. nTo
20c26 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 ken = sqlite3Str
20c27 6c 65 6e 33 30 28 70 2d 3e 75 2e 7a 54 6f 6b 65 len30(p->u.zToke
20c28 6e 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 7d 65 n) + 1;. }e
20c29 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e 54 6f lse{. nTo
20c2a 6b 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d ken = 0;. }
20c2b 0a 20 20 20 20 20 20 69 66 28 20 69 73 52 65 64 . if( isRed
20c2c 75 63 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 uced ){.
20c2d 61 73 73 65 72 74 28 20 45 78 70 72 48 61 73 50 assert( ExprHasP
20c2e 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65 roperty(p, EP_Re
20c2f 64 75 63 65 64 29 3d 3d 30 20 29 3b 0a 20 20 20 duced)==0 );.
20c30 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 41 6c 6c memcpy(zAll
20c31 6f 63 2c 20 70 2c 20 6e 4e 65 77 53 69 7a 65 29 oc, p, nNewSize)
20c32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
20c33 20 20 20 20 20 20 20 69 6e 74 20 6e 53 69 7a 65 int nSize
20c34 20 3d 20 65 78 70 72 53 74 72 75 63 74 53 69 7a = exprStructSiz
20c35 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 e(p);. me
20c36 6d 63 70 79 28 7a 41 6c 6c 6f 63 2c 20 70 2c 20 mcpy(zAlloc, p,
20c37 6e 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 nSize);.
20c38 6d 65 6d 73 65 74 28 26 7a 41 6c 6c 6f 63 5b 6e memset(&zAlloc[n
20c39 53 69 7a 65 5d 2c 20 30 2c 20 45 58 50 52 5f 46 Size], 0, EXPR_F
20c3a 55 4c 4c 53 49 5a 45 2d 6e 53 69 7a 65 29 3b 0a ULLSIZE-nSize);.
20c3b 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f }.. /
20c3c 2a 20 53 65 74 20 74 68 65 20 45 50 5f 52 65 64 * Set the EP_Red
20c3d 75 63 65 64 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e uced, EP_TokenOn
20c3e 6c 79 2c 20 61 6e 64 20 45 50 5f 53 74 61 74 69 ly, and EP_Stati
20c3f 63 20 66 6c 61 67 73 20 61 70 70 72 6f 70 72 69 c flags appropri
20c40 61 74 65 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 ately. */.
20c41 70 4e 65 77 2d 3e 66 6c 61 67 73 20 26 3d 20 7e pNew->flags &= ~
20c42 28 45 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 (EP_Reduced|EP_T
20c43 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53 74 61 74 okenOnly|EP_Stat
20c44 69 63 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d ic);. pNew-
20c45 3e 66 6c 61 67 73 20 7c 3d 20 6e 53 74 72 75 63 >flags |= nStruc
20c46 74 53 69 7a 65 20 26 20 28 45 50 5f 52 65 64 75 tSize & (EP_Redu
20c47 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 ced|EP_TokenOnly
20c48 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 );. pNew->f
20c49 6c 61 67 73 20 7c 3d 20 73 74 61 74 69 63 46 6c lags |= staticFl
20c4a 61 67 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f ag;.. /* Co
20c4b 70 79 20 74 68 65 20 70 2d 3e 75 2e 7a 54 6f 6b py the p->u.zTok
20c4c 65 6e 20 73 74 72 69 6e 67 2c 20 69 66 20 61 6e en string, if an
20c4d 79 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 y. */. if(
20c4e 6e 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 nToken ){.
20c4f 20 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 20 3d char *zToken =
20c50 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 pNew->u.zToken
20c51 3d 20 28 63 68 61 72 2a 29 26 7a 41 6c 6c 6f 63 = (char*)&zAlloc
20c52 5b 6e 4e 65 77 53 69 7a 65 5d 3b 0a 20 20 20 20 [nNewSize];.
20c53 20 20 20 20 6d 65 6d 63 70 79 28 7a 54 6f 6b 65 memcpy(zToke
20c54 6e 2c 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 n, p->u.zToken,
20c55 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d nToken);. }
20c56 0a 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 .. if( 0==(
20c57 28 70 2d 3e 66 6c 61 67 73 7c 70 4e 65 77 2d 3e (p->flags|pNew->
20c58 66 6c 61 67 73 29 20 26 20 45 50 5f 54 6f 6b 65 flags) & EP_Toke
20c59 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 nOnly) ){.
20c5a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 /* Fill in the
20c5b 20 70 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63 74 pNew->x.pSelect
20c5c 20 6f 72 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 or pNew->x.pLis
20c5d 74 20 6d 65 6d 62 65 72 2e 20 2a 2f 0a 20 20 20 t member. */.
20c5e 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 if( ExprHas
20c5f 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 78 Property(p, EP_x
20c60 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 IsSelect) ){.
20c61 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e 70 pNew->x.p
20c62 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 Select = sqlite3
20c63 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2d SelectDup(db, p-
20c64 3e 78 2e 70 53 65 6c 65 63 74 2c 20 69 73 52 65 >x.pSelect, isRe
20c65 64 75 63 65 64 29 3b 0a 20 20 20 20 20 20 20 20 duced);.
20c66 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
20c67 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d pNew->x.pList =
20c68 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
20c69 44 75 70 28 64 62 2c 20 70 2d 3e 78 2e 70 4c 69 Dup(db, p->x.pLi
20c6a 73 74 2c 20 69 73 52 65 64 75 63 65 64 29 3b 0a st, isReduced);.
20c6b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
20c6c 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c }.. /* Fill
20c6d 20 69 6e 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 in pNew->pLeft
20c6e 61 6e 64 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 and pNew->pRight
20c6f 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 45 . */. if( E
20c70 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 xprHasAnyPropert
20c71 79 28 70 4e 65 77 2c 20 45 50 5f 52 65 64 75 63 y(pNew, EP_Reduc
20c72 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 ed|EP_TokenOnly)
20c73 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 41 6c 6c ){. zAll
20c74 6f 63 20 2b 3d 20 64 75 70 65 64 45 78 70 72 4e oc += dupedExprN
20c75 6f 64 65 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 odeSize(p, flags
20c76 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 45 );. if( E
20c77 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
20c78 4e 65 77 2c 20 45 50 5f 52 65 64 75 63 65 64 29 New, EP_Reduced)
20c79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e ){. pN
20c7a 65 77 2d 3e 70 4c 65 66 74 20 3d 20 65 78 70 72 ew->pLeft = expr
20c7b 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 Dup(db, p->pLeft
20c7c 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 , EXPRDUP_REDUCE
20c7d 2c 20 26 7a 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 , &zAlloc);.
20c7e 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 52 69 67 pNew->pRig
20c7f 68 74 20 3d 20 65 78 70 72 44 75 70 28 64 62 2c ht = exprDup(db,
20c80 20 70 2d 3e 70 52 69 67 68 74 2c 20 45 58 50 52 p->pRight, EXPR
20c81 44 55 50 5f 52 45 44 55 43 45 2c 20 26 7a 41 6c DUP_REDUCE, &zAl
20c82 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a loc);. }.
20c83 20 20 20 20 20 20 20 20 69 66 28 20 70 7a 42 75 if( pzBu
20c84 66 66 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20 ffer ){.
20c85 20 20 2a 70 7a 42 75 66 66 65 72 20 3d 20 7a 41 *pzBuffer = zA
20c86 6c 6c 6f 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a lloc;. }.
20c87 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
20c88 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 pNew->flags
20c89 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 2 = 0;. i
20c8a 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 f( !ExprHasAnyPr
20c8b 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 6f 6b operty(p, EP_Tok
20c8c 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 enOnly) ){.
20c8d 20 20 20 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 pNew->pLeft
20c8e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 = sqlite3ExprDu
20c8f 70 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 2c 20 p(db, p->pLeft,
20c90 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 0);. pN
20c91 65 77 2d 3e 70 52 69 67 68 74 20 3d 20 73 71 6c ew->pRight = sql
20c92 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 ite3ExprDup(db,
20c93 70 2d 3e 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 p->pRight, 0);.
20c94 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
20c95 0a 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 .. }. }. re
20c96 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a turn pNew;.}../*
20c97 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
20c98 67 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 g group of routi
20c99 6e 65 73 20 6d 61 6b 65 20 64 65 65 70 20 63 6f nes make deep co
20c9a 70 69 65 73 20 6f 66 20 65 78 70 72 65 73 73 69 pies of expressi
20c9b 6f 6e 73 2c 0a 2a 2a 20 65 78 70 72 65 73 73 69 ons,.** expressi
20c9c 6f 6e 20 6c 69 73 74 73 2c 20 49 44 20 6c 69 73 on lists, ID lis
20c9d 74 73 2c 20 61 6e 64 20 73 65 6c 65 63 74 20 73 ts, and select s
20c9e 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 tatements. The
20c9f 63 6f 70 69 65 73 20 63 61 6e 0a 2a 2a 20 62 65 copies can.** be
20ca0 20 64 65 6c 65 74 65 64 20 28 62 79 20 62 65 69 deleted (by bei
20ca1 6e 67 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 ng passed to the
20ca2 69 72 20 72 65 73 70 65 63 74 69 76 65 20 2e 2e ir respective ..
20ca3 2e 44 65 6c 65 74 65 28 29 20 72 6f 75 74 69 6e .Delete() routin
20ca4 65 73 29 0a 2a 2a 20 77 69 74 68 6f 75 74 20 65 es).** without e
20ca5 66 66 65 63 74 69 6e 67 20 74 68 65 20 6f 72 69 ffecting the ori
20ca6 67 69 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ginals..**.** Th
20ca7 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 e expression lis
20ca8 74 2c 20 49 44 2c 20 61 6e 64 20 73 6f 75 72 63 t, ID, and sourc
20ca9 65 20 6c 69 73 74 73 20 72 65 74 75 72 6e 20 62 e lists return b
20caa 79 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 y sqlite3ExprLis
20cab 74 44 75 70 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 tDup(),.** sqlit
20cac 65 33 49 64 4c 69 73 74 44 75 70 28 29 2c 20 61 e3IdListDup(), a
20cad 6e 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 nd sqlite3SrcLis
20cae 74 44 75 70 28 29 20 63 61 6e 20 6e 6f 74 20 62 tDup() can not b
20caf 65 20 66 75 72 74 68 65 72 20 65 78 70 61 6e 64 e further expand
20cb0 65 64 20 0a 2a 2a 20 62 79 20 73 75 62 73 65 71 ed .** by subseq
20cb1 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 uent calls to sq
20cb2 6c 69 74 65 2a 4c 69 73 74 41 70 70 65 6e 64 28 lite*ListAppend(
20cb3 29 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a ) routines..**.*
20cb4 2a 20 41 6e 79 20 74 61 62 6c 65 73 20 74 68 61 * Any tables tha
20cb5 74 20 74 68 65 20 53 72 63 4c 69 73 74 20 6d 69 t the SrcList mi
20cb6 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 61 72 65 ght point to are
20cb7 20 6e 6f 74 20 64 75 70 6c 69 63 61 74 65 64 2e not duplicated.
20cb8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 .**.** The flags
20cb9 20 70 61 72 61 6d 65 74 65 72 20 63 6f 6e 74 61 parameter conta
20cba 69 6e 73 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f ins a combinatio
20cbb 6e 20 6f 66 20 74 68 65 20 45 58 50 52 44 55 50 n of the EXPRDUP
20cbc 5f 58 58 58 20 66 6c 61 67 73 2e 0a 2a 2a 20 49 _XXX flags..** I
20cbd 66 20 74 68 65 20 45 58 50 52 44 55 50 5f 52 45 f the EXPRDUP_RE
20cbe 44 55 43 45 20 66 6c 61 67 20 69 73 20 73 65 74 DUCE flag is set
20cbf 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72 75 63 , then the struc
20cc0 74 75 72 65 20 72 65 74 75 72 6e 65 64 20 69 73 ture returned is
20cc1 20 61 0a 2a 2a 20 74 72 75 6e 63 61 74 65 64 20 a.** truncated
20cc2 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 75 version of the u
20cc3 73 75 61 6c 20 45 78 70 72 20 73 74 72 75 63 74 sual Expr struct
20cc4 75 72 65 20 74 68 61 74 20 77 69 6c 6c 20 62 65 ure that will be
20cc5 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 70 61 stored as.** pa
20cc6 72 74 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d rt of the in-mem
20cc7 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 ory representati
20cc8 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 on of the databa
20cc9 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2f 0a 53 51 se schema..*/.SQ
20cca 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
20ccb 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75 r *sqlite3ExprDu
20ccc 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 p(sqlite3 *db, E
20ccd 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 xpr *p, int flag
20cce 73 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 s){. return exp
20ccf 72 44 75 70 28 64 62 2c 20 70 2c 20 66 6c 61 67 rDup(db, p, flag
20cd0 73 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f s, 0);.}.SQLITE_
20cd1 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 PRIVATE ExprList
20cd2 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 *sqlite3ExprLis
20cd3 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 tDup(sqlite3 *db
20cd4 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 2c 20 69 , ExprList *p, i
20cd5 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 45 78 70 nt flags){. Exp
20cd6 72 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 73 rList *pNew;. s
20cd7 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 truct ExprList_i
20cd8 74 65 6d 20 2a 70 49 74 65 6d 2c 20 2a 70 4f 6c tem *pItem, *pOl
20cd9 64 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a dItem;. int i;.
20cda 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
20cdb 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 urn 0;. pNew =
20cdc 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
20cdd 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 aw(db, sizeof(*p
20cde 4e 65 77 29 20 29 3b 0a 20 20 69 66 28 20 70 4e New) );. if( pN
20cdf 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 ew==0 ) return 0
20ce0 3b 0a 20 20 70 4e 65 77 2d 3e 69 45 43 75 72 73 ;. pNew->iECurs
20ce1 6f 72 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e or = 0;. pNew->
20ce2 6e 45 78 70 72 20 3d 20 70 4e 65 77 2d 3e 6e 41 nExpr = pNew->nA
20ce3 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 45 78 70 72 3b lloc = p->nExpr;
20ce4 0a 20 20 70 4e 65 77 2d 3e 61 20 3d 20 70 49 74 . pNew->a = pIt
20ce5 65 6d 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 em = sqlite3DbMa
20ce6 6c 6c 6f 63 52 61 77 28 64 62 2c 20 20 70 2d 3e llocRaw(db, p->
20ce7 6e 45 78 70 72 2a 73 69 7a 65 6f 66 28 70 2d 3e nExpr*sizeof(p->
20ce8 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 70 a[0]) );. if( p
20ce9 49 74 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 73 Item==0 ){. s
20cea 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
20ceb 20 70 4e 65 77 29 3b 0a 20 20 20 20 72 65 74 75 pNew);. retu
20cec 72 6e 20 30 3b 0a 20 20 7d 20 0a 20 20 70 4f 6c rn 0;. } . pOl
20ced 64 49 74 65 6d 20 3d 20 70 2d 3e 61 3b 0a 20 20 dItem = p->a;.
20cee 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 for(i=0; i<p->nE
20cef 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b xpr; i++, pItem+
20cf0 2b 2c 20 70 4f 6c 64 49 74 65 6d 2b 2b 29 7b 0a +, pOldItem++){.
20cf1 20 20 20 20 45 78 70 72 20 2a 70 4f 6c 64 45 78 Expr *pOldEx
20cf2 70 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 pr = pOldItem->p
20cf3 45 78 70 72 3b 0a 20 20 20 20 70 49 74 65 6d 2d Expr;. pItem-
20cf4 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 >pExpr = sqlite3
20cf5 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 ExprDup(db, pOld
20cf6 45 78 70 72 2c 20 66 6c 61 67 73 29 3b 0a 20 20 Expr, flags);.
20cf7 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d pItem->zName =
20cf8 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 sqlite3DbStrDup
20cf9 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a (db, pOldItem->z
20cfa 4e 61 6d 65 29 3b 0a 20 20 20 20 70 49 74 65 6d Name);. pItem
20cfb 2d 3e 7a 53 70 61 6e 20 3d 20 73 71 6c 69 74 65 ->zSpan = sqlite
20cfc 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 3DbStrDup(db, pO
20cfd 6c 64 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a ldItem->zSpan);.
20cfe 20 20 20 20 70 49 74 65 6d 2d 3e 73 6f 72 74 4f pItem->sortO
20cff 72 64 65 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d rder = pOldItem-
20d00 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 >sortOrder;.
20d01 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 30 3b pItem->done = 0;
20d02 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c . pItem->iCol
20d03 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 6f = pOldItem->iCo
20d04 6c 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 41 l;. pItem->iA
20d05 6c 69 61 73 20 3d 20 70 4f 6c 64 49 74 65 6d 2d lias = pOldItem-
20d06 3e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 72 >iAlias;. }. r
20d07 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f eturn pNew;.}../
20d08 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 73 2c *.** If cursors,
20d09 20 74 72 69 67 67 65 72 73 2c 20 76 69 65 77 73 triggers, views
20d0a 20 61 6e 64 20 73 75 62 71 75 65 72 69 65 73 20 and subqueries
20d0b 61 72 65 20 61 6c 6c 20 6f 6d 69 74 74 65 64 20 are all omitted
20d0c 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 62 75 69 6c from.** the buil
20d0d 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 d, then none of
20d0e 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f the following ro
20d0f 75 74 69 6e 65 73 2c 20 65 78 63 65 70 74 20 66 utines, except f
20d10 6f 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33 53 65 or .** sqlite3Se
20d11 6c 65 63 74 44 75 70 28 29 2c 20 63 61 6e 20 62 lectDup(), can b
20d12 65 20 63 61 6c 6c 65 64 2e 20 73 71 6c 69 74 65 e called. sqlite
20d13 33 53 65 6c 65 63 74 44 75 70 28 29 20 69 73 20 3SelectDup() is
20d14 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 63 61 6c sometimes.** cal
20d15 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 led with a NULL
20d16 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 69 66 argument..*/.#if
20d17 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
20d18 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 _OMIT_VIEW) || !
20d19 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
20d1a 4d 49 54 5f 54 52 49 47 47 45 52 29 20 5c 0a 20 MIT_TRIGGER) \.
20d1b 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 || !defined(SQLI
20d1c 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 TE_OMIT_SUBQUERY
20d1d 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ).SQLITE_PRIVATE
20d1e 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 SrcList *sqlite
20d1f 33 53 72 63 4c 69 73 74 44 75 70 28 73 71 6c 69 3SrcListDup(sqli
20d20 74 65 33 20 2a 64 62 2c 20 53 72 63 4c 69 73 74 te3 *db, SrcList
20d21 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b *p, int flags){
20d22 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 4e 65 77 . SrcList *pNew
20d23 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 ;. int i;. int
20d24 20 6e 42 79 74 65 3b 0a 20 20 69 66 28 20 70 3d nByte;. if( p=
20d25 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
20d26 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 nByte = sizeof(
20d27 2a 70 29 20 2b 20 28 70 2d 3e 6e 53 72 63 3e 30 *p) + (p->nSrc>0
20d28 20 3f 20 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 ? sizeof(p->a[0
20d29 5d 29 20 2a 20 28 70 2d 3e 6e 53 72 63 2d 31 29 ]) * (p->nSrc-1)
20d2a 20 3a 20 30 29 3b 0a 20 20 70 4e 65 77 20 3d 20 : 0);. pNew =
20d2b 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
20d2c 61 77 28 64 62 2c 20 6e 42 79 74 65 20 29 3b 0a aw(db, nByte );.
20d2d 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 if( pNew==0 )
20d2e 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 return 0;. pNew
20d2f 2d 3e 6e 53 72 63 20 3d 20 70 4e 65 77 2d 3e 6e ->nSrc = pNew->n
20d30 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 53 72 63 3b Alloc = p->nSrc;
20d31 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d . for(i=0; i<p-
20d32 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 >nSrc; i++){.
20d33 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f struct SrcList_
20d34 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 6d 20 3d item *pNewItem =
20d35 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a 20 20 &pNew->a[i];.
20d36 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 struct SrcList
20d37 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d 20 _item *pOldItem
20d38 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 = &p->a[i];.
20d39 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 Table *pTab;.
20d3a 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 44 61 74 61 pNewItem->zData
20d3b 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 44 62 base = sqlite3Db
20d3c 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 StrDup(db, pOldI
20d3d 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b tem->zDatabase);
20d3e 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a . pNewItem->z
20d3f 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 Name = sqlite3Db
20d40 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 StrDup(db, pOldI
20d41 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 tem->zName);.
20d42 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 41 6c 69 61 pNewItem->zAlia
20d43 73 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 s = sqlite3DbStr
20d44 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d Dup(db, pOldItem
20d45 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 70 ->zAlias);. p
20d46 4e 65 77 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 NewItem->jointyp
20d47 65 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6a 6f e = pOldItem->jo
20d48 69 6e 74 79 70 65 3b 0a 20 20 20 20 70 4e 65 77 intype;. pNew
20d49 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d 20 Item->iCursor =
20d4a 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 75 72 73 6f pOldItem->iCurso
20d4b 72 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d r;. pNewItem-
20d4c 3e 69 73 50 6f 70 75 6c 61 74 65 64 20 3d 20 70 >isPopulated = p
20d4d 4f 6c 64 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c OldItem->isPopul
20d4e 61 74 65 64 3b 0a 20 20 20 20 70 4e 65 77 49 74 ated;. pNewIt
20d4f 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20 73 71 6c em->zIndex = sql
20d50 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c ite3DbStrDup(db,
20d51 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 49 6e 64 65 pOldItem->zInde
20d52 78 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d x);. pNewItem
20d53 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20 3d 20 70 ->notIndexed = p
20d54 4f 6c 64 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 OldItem->notInde
20d55 78 65 64 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 xed;. pNewIte
20d56 6d 2d 3e 70 49 6e 64 65 78 20 3d 20 70 4f 6c 64 m->pIndex = pOld
20d57 49 74 65 6d 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 Item->pIndex;.
20d58 20 20 70 54 61 62 20 3d 20 70 4e 65 77 49 74 65 pTab = pNewIte
20d59 6d 2d 3e 70 54 61 62 20 3d 20 70 4f 6c 64 49 74 m->pTab = pOldIt
20d5a 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69 66 em->pTab;. if
20d5b 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 ( pTab ){.
20d5c 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 pTab->nRef++;.
20d5d 20 20 7d 0a 20 20 20 20 70 4e 65 77 49 74 65 6d }. pNewItem
20d5e 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 ->pSelect = sqli
20d5f 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c te3SelectDup(db,
20d60 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 53 65 6c 65 pOldItem->pSele
20d61 63 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 ct, flags);.
20d62 70 4e 65 77 49 74 65 6d 2d 3e 70 4f 6e 20 3d 20 pNewItem->pOn =
20d63 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 sqlite3ExprDup(d
20d64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 4f 6e b, pOldItem->pOn
20d65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 70 4e , flags);. pN
20d66 65 77 49 74 65 6d 2d 3e 70 55 73 69 6e 67 20 3d ewItem->pUsing =
20d67 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 sqlite3IdListDu
20d68 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e p(db, pOldItem->
20d69 70 55 73 69 6e 67 29 3b 0a 20 20 20 20 70 4e 65 pUsing);. pNe
20d6a 77 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 20 3d wItem->colUsed =
20d6b 20 70 4f 6c 64 49 74 65 6d 2d 3e 63 6f 6c 55 73 pOldItem->colUs
20d6c 65 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ed;. }. return
20d6d 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f pNew;.}.SQLITE_
20d6e 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 20 2a PRIVATE IdList *
20d6f 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 sqlite3IdListDup
20d70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 49 64 (sqlite3 *db, Id
20d71 4c 69 73 74 20 2a 70 29 7b 0a 20 20 49 64 4c 69 List *p){. IdLi
20d72 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 st *pNew;. int
20d73 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 i;. if( p==0 )
20d74 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 return 0;. pNew
20d75 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
20d76 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 ocRaw(db, sizeof
20d77 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 69 66 28 (*pNew) );. if(
20d78 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 pNew==0 ) retur
20d79 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 49 64 n 0;. pNew->nId
20d7a 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20 = pNew->nAlloc
20d7b 3d 20 70 2d 3e 6e 49 64 3b 0a 20 20 70 4e 65 77 = p->nId;. pNew
20d7c 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33 44 62 4d ->a = sqlite3DbM
20d7d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 70 2d 3e allocRaw(db, p->
20d7e 6e 49 64 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 5b nId*sizeof(p->a[
20d7f 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 0]) );. if( pNe
20d80 77 2d 3e 61 3d 3d 30 20 29 7b 0a 20 20 20 20 73 w->a==0 ){. s
20d81 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
20d82 20 70 4e 65 77 29 3b 0a 20 20 20 20 72 65 74 75 pNew);. retu
20d83 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 rn 0;. }. for(
20d84 69 3d 30 3b 20 69 3c 70 2d 3e 6e 49 64 3b 20 69 i=0; i<p->nId; i
20d85 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 ++){. struct
20d86 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4e 65 IdList_item *pNe
20d87 77 49 74 65 6d 20 3d 20 26 70 4e 65 77 2d 3e 61 wItem = &pNew->a
20d88 5b 69 5d 3b 0a 20 20 20 20 73 74 72 75 63 74 20 [i];. struct
20d89 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4f 6c IdList_item *pOl
20d8a 64 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d dItem = &p->a[i]
20d8b 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e ;. pNewItem->
20d8c 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 zName = sqlite3D
20d8d 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 bStrDup(db, pOld
20d8e 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 Item->zName);.
20d8f 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 64 78 20 pNewItem->idx
20d90 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 64 78 3b = pOldItem->idx;
20d91 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e . }. return pN
20d92 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 ew;.}.SQLITE_PRI
20d93 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c VATE Select *sql
20d94 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 73 71 ite3SelectDup(sq
20d95 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 lite3 *db, Selec
20d96 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 t *p, int flags)
20d97 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 77 {. Select *pNew
20d98 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
20d99 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20 eturn 0;. pNew
20d9a 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
20d9b 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 cRaw(db, sizeof(
20d9c 2a 70 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 *p) );. if( pNe
20d9d 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b w==0 ) return 0;
20d9e 0a 20 20 70 4e 65 77 2d 3e 70 45 4c 69 73 74 20 . pNew->pEList
20d9f 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 = sqlite3ExprLis
20da0 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 45 4c 69 tDup(db, p->pELi
20da1 73 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e st, flags);. pN
20da2 65 77 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69 74 ew->pSrc = sqlit
20da3 65 33 53 72 63 4c 69 73 74 44 75 70 28 64 62 2c e3SrcListDup(db,
20da4 20 70 2d 3e 70 53 72 63 2c 20 66 6c 61 67 73 29 p->pSrc, flags)
20da5 3b 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65 72 65 ;. pNew->pWhere
20da6 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 = sqlite3ExprDu
20da7 70 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 2c p(db, p->pWhere,
20da8 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d flags);. pNew-
20da9 3e 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c 69 >pGroupBy = sqli
20daa 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 te3ExprListDup(d
20dab 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 b, p->pGroupBy,
20dac 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e flags);. pNew->
20dad 70 48 61 76 69 6e 67 20 3d 20 73 71 6c 69 74 65 pHaving = sqlite
20dae 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 3ExprDup(db, p->
20daf 70 48 61 76 69 6e 67 2c 20 66 6c 61 67 73 29 3b pHaving, flags);
20db0 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 64 65 72 42 . pNew->pOrderB
20db1 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c y = sqlite3ExprL
20db2 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f istDup(db, p->pO
20db3 72 64 65 72 42 79 2c 20 66 6c 61 67 73 29 3b 0a rderBy, flags);.
20db4 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 70 2d 3e pNew->op = p->
20db5 6f 70 3b 0a 20 20 70 4e 65 77 2d 3e 70 50 72 69 op;. pNew->pPri
20db6 6f 72 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 or = sqlite3Sele
20db7 63 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 50 72 ctDup(db, p->pPr
20db8 69 6f 72 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 ior, flags);. p
20db9 4e 65 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71 New->pLimit = sq
20dba 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c lite3ExprDup(db,
20dbb 20 70 2d 3e 70 4c 69 6d 69 74 2c 20 66 6c 61 67 p->pLimit, flag
20dbc 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 66 66 s);. pNew->pOff
20dbd 73 65 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 set = sqlite3Exp
20dbe 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f 66 66 rDup(db, p->pOff
20dbf 73 65 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 set, flags);. p
20dc0 4e 65 77 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b New->iLimit = 0;
20dc1 0a 20 20 70 4e 65 77 2d 3e 69 4f 66 66 73 65 74 . pNew->iOffset
20dc2 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 73 65 = 0;. pNew->se
20dc3 6c 46 6c 61 67 73 20 3d 20 70 2d 3e 73 65 6c 46 lFlags = p->selF
20dc4 6c 61 67 73 20 26 20 7e 53 46 5f 55 73 65 73 45 lags & ~SF_UsesE
20dc5 70 68 65 6d 65 72 61 6c 3b 0a 20 20 70 4e 65 77 phemeral;. pNew
20dc6 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 30 ->pRightmost = 0
20dc7 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 ;. pNew->addrOp
20dc8 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b 0a enEphm[0] = -1;.
20dc9 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e pNew->addrOpen
20dca 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0a 20 20 Ephm[1] = -1;.
20dcb 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 pNew->addrOpenEp
20dcc 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20 72 65 hm[2] = -1;. re
20dcd 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 23 65 6c turn pNew;.}.#el
20dce 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 se.SQLITE_PRIVAT
20dcf 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 E Select *sqlite
20dd0 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69 74 3SelectDup(sqlit
20dd1 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a e3 *db, Select *
20dd2 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 p, int flags){.
20dd3 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 29 3b assert( p==0 );
20dd4 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 . return 0;.}.#
20dd5 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 endif.../*.** Ad
20dd6 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 d a new element
20dd7 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 61 6e to the end of an
20dd8 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 expression list
20dd9 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 0a 2a . If pList is.*
20dda 2a 20 69 6e 69 74 69 61 6c 6c 79 20 4e 55 4c 4c * initially NULL
20ddb 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 20 , then create a
20ddc 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6c new expression l
20ddd 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 ist..**.** If a
20dde 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
20ddf 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
20de0 74 68 65 20 65 6e 74 69 72 65 20 6c 69 73 74 20 the entire list
20de1 69 73 20 66 72 65 65 64 20 61 6e 64 0a 2a 2a 20 is freed and.**
20de2 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 NULL is returned
20de3 2e 20 20 49 66 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 . If non-NULL i
20de4 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e s returned, then
20de5 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 it is guarantee
20de6 64 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6e 65 d.** that the ne
20de7 77 20 65 6e 74 72 79 20 77 61 73 20 73 75 63 63 w entry was succ
20de8 65 73 73 66 75 6c 6c 79 20 61 70 70 65 6e 64 65 essfully appende
20de9 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
20dea 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 VATE ExprList *s
20deb 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 qlite3ExprListAp
20dec 70 65 6e 64 28 0a 20 20 50 61 72 73 65 20 2a 70 pend(. Parse *p
20ded 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 Parse,
20dee 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
20def 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 xt */. ExprList
20df0 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 20 20 *pList,
20df1 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68 69 63 68 /* List to which
20df2 20 74 6f 20 61 70 70 65 6e 64 2e 20 4d 69 67 68 to append. Migh
20df3 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 t be NULL */. E
20df4 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 xpr *pExpr
20df5 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 /* Expres
20df6 73 69 6f 6e 20 74 6f 20 62 65 20 61 70 70 65 6e sion to be appen
20df7 64 65 64 2e 20 4d 69 67 68 74 20 62 65 20 4e 55 ded. Might be NU
20df8 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 LL */.){. sqlit
20df9 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
20dfa 3e 64 62 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 >db;. if( pList
20dfb 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 ==0 ){. pList
20dfc 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
20dfd 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f ocZero(db, sizeo
20dfe 66 28 45 78 70 72 4c 69 73 74 29 20 29 3b 0a 20 f(ExprList) );.
20dff 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 if( pList==0
20e00 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f ){. goto no
20e01 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 _mem;. }.
20e02 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e assert( pList->n
20e03 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 7d 0a Alloc==0 );. }.
20e04 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e 41 6c if( pList->nAl
20e05 6c 6f 63 3c 3d 70 4c 69 73 74 2d 3e 6e 45 78 70 loc<=pList->nExp
20e06 72 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 r ){. struct
20e07 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 ExprList_item *a
20e08 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 4c ;. int n = pL
20e09 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20 ist->nAlloc*2 +
20e0a 34 3b 0a 20 20 20 20 61 20 3d 20 73 71 6c 69 74 4;. a = sqlit
20e0b 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 e3DbRealloc(db,
20e0c 70 4c 69 73 74 2d 3e 61 2c 20 6e 2a 73 69 7a 65 pList->a, n*size
20e0d 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 5d 29 29 of(pList->a[0]))
20e0e 3b 0a 20 20 20 20 69 66 28 20 61 3d 3d 30 20 29 ;. if( a==0 )
20e0f 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f {. goto no_
20e10 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 mem;. }. p
20e11 4c 69 73 74 2d 3e 61 20 3d 20 61 3b 0a 20 20 20 List->a = a;.
20e12 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d pList->nAlloc =
20e13 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
20e14 53 69 7a 65 28 64 62 2c 20 61 29 2f 73 69 7a 65 Size(db, a)/size
20e15 6f 66 28 61 5b 30 5d 29 3b 0a 20 20 7d 0a 20 20 of(a[0]);. }.
20e16 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 61 assert( pList->a
20e17 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 31 20 29 !=0 );. if( 1 )
20e18 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 70 {. struct Exp
20e19 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 rList_item *pIte
20e1a 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c m = &pList->a[pL
20e1b 69 73 74 2d 3e 6e 45 78 70 72 2b 2b 5d 3b 0a 20 ist->nExpr++];.
20e1c 20 20 20 6d 65 6d 73 65 74 28 70 49 74 65 6d 2c memset(pItem,
20e1d 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 49 74 65 0, sizeof(*pIte
20e1e 6d 29 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e m));. pItem->
20e1f 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 pExpr = pExpr;.
20e20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 73 }. return pLis
20e21 74 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 20 20 20 20 20 t;..no_mem:
20e22 0a 20 20 2f 2a 20 41 76 6f 69 64 20 6c 65 61 6b . /* Avoid leak
20e23 69 6e 67 20 6d 65 6d 6f 72 79 20 69 66 20 6d 61 ing memory if ma
20e24 6c 6c 6f 63 20 68 61 73 20 66 61 69 6c 65 64 2e lloc has failed.
20e25 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 */. sqlite3Exp
20e26 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 rDelete(db, pExp
20e27 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 r);. sqlite3Exp
20e28 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 rListDelete(db,
20e29 70 4c 69 73 74 29 3b 0a 20 20 72 65 74 75 72 6e pList);. return
20e2a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 0;.}../*.** Set
20e2b 20 74 68 65 20 45 78 70 72 4c 69 73 74 2e 61 5b the ExprList.a[
20e2c 5d 2e 7a 4e 61 6d 65 20 65 6c 65 6d 65 6e 74 20 ].zName element
20e2d 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 of the most rece
20e2e 6e 74 6c 79 20 61 64 64 65 64 20 69 74 65 6d 0a ntly added item.
20e2f 2a 2a 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 ** on the expres
20e30 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a sion list..**.**
20e31 20 70 4c 69 73 74 20 6d 69 67 68 74 20 62 65 20 pList might be
20e32 4e 55 4c 4c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 NULL following a
20e33 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 42 75 n OOM error. Bu
20e34 74 20 70 4e 61 6d 65 20 73 68 6f 75 6c 64 20 6e t pName should n
20e35 65 76 65 72 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2e ever be.** NULL.
20e36 20 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c If a memory al
20e37 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 location fails,
20e38 74 68 65 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e the pParse->db->
20e39 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 mallocFailed fla
20e3a 67 0a 2a 2a 20 69 73 20 73 65 74 2e 0a 2a 2f 0a g.** is set..*/.
20e3b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
20e3c 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c oid sqlite3ExprL
20e3d 69 73 74 53 65 74 4e 61 6d 65 28 0a 20 20 50 61 istSetName(. Pa
20e3e 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
20e3f 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 /* Parsing
20e40 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 context */. Ex
20e41 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 prList *pList,
20e42 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f /* List to
20e43 20 77 68 69 63 68 20 74 6f 20 61 64 64 20 74 68 which to add th
20e44 65 20 73 70 61 6e 2e 20 2a 2f 0a 20 20 54 6f 6b e span. */. Tok
20e45 65 6e 20 2a 70 4e 61 6d 65 2c 20 20 20 20 20 20 en *pName,
20e46 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 74 6f 20 /* Name to
20e47 62 65 20 61 64 64 65 64 20 2a 2f 0a 20 20 69 6e be added */. in
20e48 74 20 64 65 71 75 6f 74 65 20 20 20 20 20 20 20 t dequote
20e49 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f /* True to
20e4a 20 63 61 75 73 65 20 74 68 65 20 6e 61 6d 65 20 cause the name
20e4b 74 6f 20 62 65 20 64 65 71 75 6f 74 65 64 20 2a to be dequoted *
20e4c 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 /.){. assert( p
20e4d 4c 69 73 74 21 3d 30 20 7c 7c 20 70 50 61 72 73 List!=0 || pPars
20e4e 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 e->db->mallocFai
20e4f 6c 65 64 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 led!=0 );. if(
20e50 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 73 74 72 pList ){. str
20e51 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 uct ExprList_ite
20e52 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 61 73 m *pItem;. as
20e53 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 78 sert( pList->nEx
20e54 70 72 3e 30 20 29 3b 0a 20 20 20 20 70 49 74 65 pr>0 );. pIte
20e55 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c m = &pList->a[pL
20e56 69 73 74 2d 3e 6e 45 78 70 72 2d 31 5d 3b 0a 20 ist->nExpr-1];.
20e57 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d assert( pItem
20e58 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 29 3b 0a 20 20 ->zName==0 );.
20e59 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d pItem->zName =
20e5a 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 sqlite3DbStrNDu
20e5b 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4e p(pParse->db, pN
20e5c 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e ame->z, pName->n
20e5d 29 3b 0a 20 20 20 20 69 66 28 20 64 65 71 75 6f );. if( dequo
20e5e 74 65 20 26 26 20 70 49 74 65 6d 2d 3e 7a 4e 61 te && pItem->zNa
20e5f 6d 65 20 29 20 73 71 6c 69 74 65 33 44 65 71 75 me ) sqlite3Dequ
20e60 6f 74 65 28 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 ote(pItem->zName
20e61 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
20e62 53 65 74 20 74 68 65 20 45 78 70 72 4c 69 73 74 Set the ExprList
20e63 2e 61 5b 5d 2e 7a 53 70 61 6e 20 65 6c 65 6d 65 .a[].zSpan eleme
20e64 6e 74 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 nt of the most r
20e65 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 69 74 ecently added it
20e66 65 6d 0a 2a 2a 20 6f 6e 20 74 68 65 20 65 78 70 em.** on the exp
20e67 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a ression list..**
20e68 0a 2a 2a 20 70 4c 69 73 74 20 6d 69 67 68 74 20 .** pList might
20e69 62 65 20 4e 55 4c 4c 20 66 6f 6c 6c 6f 77 69 6e be NULL followin
20e6a 67 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 g an OOM error.
20e6b 20 42 75 74 20 70 53 70 61 6e 20 73 68 6f 75 6c But pSpan shoul
20e6c 64 20 6e 65 76 65 72 20 62 65 0a 2a 2a 20 4e 55 d never be.** NU
20e6d 4c 4c 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72 79 LL. If a memory
20e6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c allocation fail
20e6f 73 2c 20 74 68 65 20 70 50 61 72 73 65 2d 3e 64 s, the pParse->d
20e70 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
20e71 66 6c 61 67 0a 2a 2a 20 69 73 20 73 65 74 2e 0a flag.** is set..
20e72 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
20e73 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
20e74 70 72 4c 69 73 74 53 65 74 53 70 61 6e 28 0a 20 prListSetSpan(.
20e75 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
20e76 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 /* Pars
20e77 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 ing context */.
20e78 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 ExprList *pList
20e79 2c 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 , /* List
20e7a 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 64 64 to which to add
20e7b 20 74 68 65 20 73 70 61 6e 2e 20 2a 2f 0a 20 20 the span. */.
20e7c 45 78 70 72 53 70 61 6e 20 2a 70 53 70 61 6e 20 ExprSpan *pSpan
20e7d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 /* The s
20e7e 70 61 6e 20 74 6f 20 62 65 20 61 64 64 65 64 20 pan to be added
20e7f 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 */.){. sqlite3
20e80 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
20e81 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 ;. assert( pLis
20e82 74 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c t!=0 || db->mall
20e83 6f 63 46 61 69 6c 65 64 21 3d 30 20 29 3b 0a 20 ocFailed!=0 );.
20e84 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 if( pList ){.
20e85 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
20e86 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 t_item *pItem =
20e87 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d &pList->a[pList-
20e88 3e 6e 45 78 70 72 2d 31 5d 3b 0a 20 20 20 20 61 >nExpr-1];. a
20e89 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 ssert( pList->nE
20e8a 78 70 72 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 xpr>0 );. ass
20e8b 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 ert( db->mallocF
20e8c 61 69 6c 65 64 20 7c 7c 20 70 49 74 65 6d 2d 3e ailed || pItem->
20e8d 70 45 78 70 72 3d 3d 70 53 70 61 6e 2d 3e 70 45 pExpr==pSpan->pE
20e8e 78 70 72 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 xpr );. sqlit
20e8f 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 e3DbFree(db, pIt
20e90 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 20 20 em->zSpan);.
20e91 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 20 3d 20 73 pItem->zSpan = s
20e92 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 qlite3DbStrNDup(
20e93 64 62 2c 20 28 63 68 61 72 2a 29 70 53 70 61 6e db, (char*)pSpan
20e94 2d 3e 7a 53 74 61 72 74 2c 0a 20 20 20 20 20 20 ->zStart,.
20e95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20e96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 (i
20e97 6e 74 29 28 70 53 70 61 6e 2d 3e 7a 45 6e 64 20 nt)(pSpan->zEnd
20e98 2d 20 70 53 70 61 6e 2d 3e 7a 53 74 61 72 74 29 - pSpan->zStart)
20e99 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
20e9a 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f If the expressio
20e9b 6e 20 6c 69 73 74 20 70 45 4c 69 73 74 20 63 6f n list pEList co
20e9c 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e ntains more than
20e9d 20 69 4c 69 6d 69 74 20 65 6c 65 6d 65 6e 74 73 iLimit elements
20e9e 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20 65 72 ,.** leave an er
20e9f 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 ror message in p
20ea0 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 Parse..*/.SQLITE
20ea1 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
20ea2 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 lite3ExprListChe
20ea3 63 6b 4c 65 6e 67 74 68 28 0a 20 20 50 61 72 73 ckLength(. Pars
20ea4 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 45 78 70 e *pParse,. Exp
20ea5 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 0a 20 rList *pEList,.
20ea6 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 62 const char *zOb
20ea7 6a 65 63 74 0a 29 7b 0a 20 20 69 6e 74 20 6d 78 ject.){. int mx
20ea8 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 = pParse->db->a
20ea9 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
20eaa 49 54 5f 43 4f 4c 55 4d 4e 5d 3b 0a 20 20 74 65 IT_COLUMN];. te
20eab 73 74 63 61 73 65 28 20 70 45 4c 69 73 74 20 26 stcase( pEList &
20eac 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d & pEList->nExpr=
20ead 3d 6d 78 20 29 3b 0a 20 20 74 65 73 74 63 61 73 =mx );. testcas
20eae 65 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c e( pEList && pEL
20eaf 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 2b 31 ist->nExpr==mx+1
20eb0 20 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 );. if( pEList
20eb1 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 && pEList->nExp
20eb2 72 3e 6d 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 r>mx ){. sqli
20eb3 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
20eb4 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f se, "too many co
20eb5 6c 75 6d 6e 73 20 69 6e 20 25 73 22 2c 20 7a 4f lumns in %s", zO
20eb6 62 6a 65 63 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f bject);. }.}../
20eb7 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 *.** Delete an e
20eb8 6e 74 69 72 65 20 65 78 70 72 65 73 73 69 6f 6e ntire expression
20eb9 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 list..*/.SQLITE
20eba 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
20ebb 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c lite3ExprListDel
20ebc 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ete(sqlite3 *db,
20ebd 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 ExprList *pList
20ebe 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 ){. int i;. st
20ebf 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 ruct ExprList_it
20ec0 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 em *pItem;. if(
20ec1 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 pList==0 ) retu
20ec2 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c rn;. assert( pL
20ec3 69 73 74 2d 3e 61 21 3d 30 20 7c 7c 20 28 70 4c ist->a!=0 || (pL
20ec4 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 30 20 26 26 ist->nExpr==0 &&
20ec5 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d pList->nAlloc==
20ec6 30 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 0) );. assert(
20ec7 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3c 3d 70 4c pList->nExpr<=pL
20ec8 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 ist->nAlloc );.
20ec9 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 for(pItem=pList
20eca 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73 ->a, i=0; i<pLis
20ecb 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 t->nExpr; i++, p
20ecc 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 71 6c Item++){. sql
20ecd 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 ite3ExprDelete(d
20ece 62 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 b, pItem->pExpr)
20ecf 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 ;. sqlite3DbF
20ed0 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a ree(db, pItem->z
20ed1 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 Name);. sqlit
20ed2 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 e3DbFree(db, pIt
20ed3 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 7d 0a em->zSpan);. }.
20ed4 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
20ed5 64 62 2c 20 70 4c 69 73 74 2d 3e 61 29 3b 0a 20 db, pList->a);.
20ed6 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
20ed7 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a b, pList);.}../*
20ed8 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e .** These routin
20ed9 65 73 20 61 72 65 20 57 61 6c 6b 65 72 20 63 61 es are Walker ca
20eda 6c 6c 62 61 63 6b 73 2e 20 20 57 61 6c 6b 65 72 llbacks. Walker
20edb 2e 75 2e 70 69 20 69 73 20 61 20 70 6f 69 6e 74 .u.pi is a point
20edc 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 69 6e 74 65 er.** to an inte
20edd 67 65 72 2e 20 20 54 68 65 73 65 20 72 6f 75 74 ger. These rout
20ede 69 6e 65 73 20 61 72 65 20 63 68 65 63 6b 69 6e ines are checkin
20edf 67 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 g an expression
20ee0 74 6f 20 73 65 65 0a 2a 2a 20 69 66 20 69 74 20 to see.** if it
20ee1 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 2e 20 20 is a constant.
20ee2 53 65 74 20 2a 57 61 6c 6b 65 72 2e 75 2e 70 69 Set *Walker.u.pi
20ee3 20 74 6f 20 30 20 69 66 20 74 68 65 20 65 78 70 to 0 if the exp
20ee4 72 65 73 73 69 6f 6e 20 69 73 0a 2a 2a 20 6e 6f ression is.** no
20ee5 74 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2a 0a 2a t constant..**.*
20ee6 2a 20 54 68 65 73 65 20 63 61 6c 6c 62 61 63 6b * These callback
20ee7 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 routines are us
20ee8 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 ed to implement
20ee9 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a the following:.*
20eea 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 *.** sqlite3
20eeb 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 29 ExprIsConstant()
20eec 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 45 .** sqlite3E
20eed 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 xprIsConstantNot
20eee 4a 6f 69 6e 28 29 0a 2a 2a 20 20 20 20 20 73 71 Join().** sq
20eef 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
20ef0 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 29 0a antOrFunction().
20ef1 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 **.*/.static int
20ef2 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74 exprNodeIsConst
20ef3 61 6e 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c ant(Walker *pWal
20ef4 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72 ker, Expr *pExpr
20ef5 29 7b 0a 0a 20 20 2f 2a 20 49 66 20 70 57 61 6c ){.. /* If pWal
20ef6 6b 65 72 2d 3e 75 2e 69 20 69 73 20 33 20 74 68 ker->u.i is 3 th
20ef7 65 6e 20 61 6e 79 20 74 65 72 6d 20 6f 66 20 74 en any term of t
20ef8 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 he expression th
20ef9 61 74 20 63 6f 6d 65 73 20 66 72 6f 6d 0a 20 20 at comes from.
20efa 2a 2a 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 ** the ON or USI
20efb 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 NG clauses of a
20efc 6a 6f 69 6e 20 64 69 73 71 75 61 6c 69 66 69 65 join disqualifie
20efd 73 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e s the expression
20efe 0a 20 20 2a 2a 20 66 72 6f 6d 20 62 65 69 6e 67 . ** from being
20eff 20 63 6f 6e 73 69 64 65 72 65 64 20 63 6f 6e 73 considered cons
20f00 74 61 6e 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 tant. */. if( p
20f01 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 33 20 26 Walker->u.i==3 &
20f02 26 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 & ExprHasAnyProp
20f03 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 46 erty(pExpr, EP_F
20f04 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20 romJoin) ){.
20f05 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 pWalker->u.i = 0
20f06 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 ;. return WRC
20f07 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 73 _Abort;. }.. s
20f08 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 witch( pExpr->op
20f09 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 6e 73 69 ){. /* Consi
20f0a 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f der functions to
20f0b 20 62 65 20 63 6f 6e 73 74 61 6e 74 20 69 66 20 be constant if
20f0c 61 6c 6c 20 74 68 65 69 72 20 61 72 67 75 6d 65 all their argume
20f0d 6e 74 73 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 nts are constant
20f0e 0a 20 20 20 20 2a 2a 20 61 6e 64 20 70 57 61 6c . ** and pWal
20f0f 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20 2a 2f 0a 20 ker->u.i==2 */.
20f10 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 case TK_FUNCT
20f11 49 4f 4e 3a 0a 20 20 20 20 20 20 69 66 28 20 70 ION:. if( p
20f12 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20 29 Walker->u.i==2 )
20f13 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 return 0;.
20f14 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 /* Fall through
20f15 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f */. case TK_
20f16 49 44 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ID:. case TK_
20f17 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65 COLUMN:. case
20f18 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e TK_AGG_FUNCTION
20f19 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 :. case TK_AG
20f1a 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 20 20 G_COLUMN:.
20f1b 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d testcase( pExpr-
20f1c 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 3b 0a 20 20 >op==TK_ID );.
20f1d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 testcase( pE
20f1e 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 xpr->op==TK_COLU
20f1f 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 MN );. test
20f20 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d case( pExpr->op=
20f21 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e =TK_AGG_FUNCTION
20f22 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
20f23 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 se( pExpr->op==T
20f24 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a K_AGG_COLUMN );.
20f25 20 20 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e 75 pWalker->u
20f26 2e 69 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 .i = 0;. re
20f27 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
20f28 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 default:.
20f29 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 testcase( pEx
20f2a 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 pr->op==TK_SELEC
20f2b 54 20 29 3b 20 2f 2a 20 73 65 6c 65 63 74 4e 6f T ); /* selectNo
20f2c 64 65 49 73 43 6f 6e 73 74 61 6e 74 20 77 69 6c deIsConstant wil
20f2d 6c 20 64 69 73 61 6c 6c 6f 77 20 2a 2f 0a 20 20 l disallow */.
20f2e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 testcase( pE
20f2f 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 xpr->op==TK_EXIS
20f30 54 53 20 29 3b 20 2f 2a 20 73 65 6c 65 63 74 4e TS ); /* selectN
20f31 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 20 77 69 odeIsConstant wi
20f32 6c 6c 20 64 69 73 61 6c 6c 6f 77 20 2a 2f 0a 20 ll disallow */.
20f33 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f return WRC_
20f34 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 7d 0a 7d 0a Continue;. }.}.
20f35 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63 static int selec
20f36 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 28 tNodeIsConstant(
20f37 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c Walker *pWalker,
20f38 20 53 65 6c 65 63 74 20 2a 4e 6f 74 55 73 65 64 Select *NotUsed
20f39 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
20f3a 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
20f3b 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d pWalker->u.i =
20f3c 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43 0;. return WRC
20f3d 5f 41 62 6f 72 74 3b 0a 7d 0a 73 74 61 74 69 63 _Abort;.}.static
20f3e 20 69 6e 74 20 65 78 70 72 49 73 43 6f 6e 73 74 int exprIsConst
20f3f 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 69 6e (Expr *p, int in
20f40 69 74 46 6c 61 67 29 7b 0a 20 20 57 61 6c 6b 65 itFlag){. Walke
20f41 72 20 77 3b 0a 20 20 77 2e 75 2e 69 20 3d 20 69 r w;. w.u.i = i
20f42 6e 69 74 46 6c 61 67 3b 0a 20 20 77 2e 78 45 78 nitFlag;. w.xEx
20f43 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78 70 prCallback = exp
20f44 72 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b rNodeIsConstant;
20f45 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c . w.xSelectCall
20f46 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 4e 6f 64 back = selectNod
20f47 65 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 73 eIsConstant;. s
20f48 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 qlite3WalkExpr(&
20f49 77 2c 20 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 w, p);. return
20f4a 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 w.u.i;.}../*.**
20f4b 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69 Walk an expressi
20f4c 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e on tree. Return
20f4d 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65 73 1 if the expres
20f4e 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74 sion is constant
20f4f 0a 2a 2a 20 61 6e 64 20 30 20 69 66 20 69 74 20 .** and 0 if it
20f50 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 62 6c involves variabl
20f51 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e 20 63 es or function c
20f52 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 alls..**.** For
20f53 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 the purposes of
20f54 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 this function, a
20f55 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 double-quoted s
20f56 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62 63 22 tring (ex: "abc"
20f57 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 ).** is consider
20f58 65 64 20 61 20 76 61 72 69 61 62 6c 65 20 62 75 ed a variable bu
20f59 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 t a single-quote
20f5a 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27 61 d string (ex: 'a
20f5b 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e bc') is.** a con
20f5c 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 stant..*/.SQLITE
20f5d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
20f5e 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 ite3ExprIsConsta
20f5f 6e 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 72 nt(Expr *p){. r
20f60 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e 73 eturn exprIsCons
20f61 74 28 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a t(p, 1);.}../*.*
20f62 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 * Walk an expres
20f63 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 sion tree. Retu
20f64 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72 rn 1 if the expr
20f65 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 ession is consta
20f66 6e 74 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 nt.** that does
20f67 6e 6f 20 6f 72 69 67 69 6e 61 74 65 20 66 72 6f no originate fro
20f68 6d 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e m the ON or USIN
20f69 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 6a G clauses of a j
20f6a 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30 oin..** Return 0
20f6b 20 69 66 20 69 74 20 69 6e 76 6f 6c 76 65 73 20 if it involves
20f6c 76 61 72 69 61 62 6c 65 73 20 6f 72 20 66 75 6e variables or fun
20f6d 63 74 69 6f 6e 20 63 61 6c 6c 73 20 6f 72 20 74 ction calls or t
20f6e 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 erms from.** an
20f6f 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 ON or USING clau
20f70 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 se..*/.SQLITE_PR
20f71 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
20f72 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 3ExprIsConstantN
20f73 6f 74 4a 6f 69 6e 28 45 78 70 72 20 2a 70 29 7b otJoin(Expr *p){
20f74 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49 73 . return exprIs
20f75 43 6f 6e 73 74 28 70 2c 20 33 29 3b 0a 7d 0a 0a Const(p, 3);.}..
20f76 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 /*.** Walk an ex
20f77 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 pression tree.
20f78 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 Return 1 if the
20f79 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f expression is co
20f7a 6e 73 74 61 6e 74 0a 2a 2a 20 6f 72 20 61 20 66 nstant.** or a f
20f7b 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 77 69 74 unction call wit
20f7c 68 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d h constant argum
20f7d 65 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 61 6e ents. Return an
20f7e 64 20 30 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 d 0 if there.**
20f7f 61 72 65 20 61 6e 79 20 76 61 72 69 61 62 6c 65 are any variable
20f80 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 s..**.** For the
20f81 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 purposes of thi
20f82 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f s function, a do
20f83 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 uble-quoted stri
20f84 6e 67 20 28 65 78 3a 20 22 61 62 63 22 29 0a 2a ng (ex: "abc").*
20f85 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 * is considered
20f86 61 20 76 61 72 69 61 62 6c 65 20 62 75 74 20 61 a variable but a
20f87 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73 single-quoted s
20f88 74 72 69 6e 67 20 28 65 78 3a 20 27 61 62 63 27 tring (ex: 'abc'
20f89 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61 ) is.** a consta
20f8a 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 nt..*/.SQLITE_PR
20f8b 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
20f8c 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 3ExprIsConstantO
20f8d 72 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 20 2a rFunction(Expr *
20f8e 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 p){. return exp
20f8f 72 49 73 43 6f 6e 73 74 28 70 2c 20 32 29 3b 0a rIsConst(p, 2);.
20f90 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 }../*.** If the
20f91 65 78 70 72 65 73 73 69 6f 6e 20 70 20 63 6f 64 expression p cod
20f92 65 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 69 6e es a constant in
20f93 74 65 67 65 72 20 74 68 61 74 20 69 73 20 73 6d teger that is sm
20f94 61 6c 6c 20 65 6e 6f 75 67 68 0a 2a 2a 20 74 6f all enough.** to
20f95 20 66 69 74 20 69 6e 20 61 20 33 32 2d 62 69 74 fit in a 32-bit
20f96 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e integer, return
20f97 20 31 20 61 6e 64 20 70 75 74 20 74 68 65 20 76 1 and put the v
20f98 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 alue of the inte
20f99 67 65 72 0a 2a 2a 20 69 6e 20 2a 70 56 61 6c 75 ger.** in *pValu
20f9a 65 2e 20 20 49 66 20 74 68 65 20 65 78 70 72 65 e. If the expre
20f9b 73 73 69 6f 6e 20 69 73 20 6e 6f 74 20 61 6e 20 ssion is not an
20f9c 69 6e 74 65 67 65 72 20 6f 72 20 69 66 20 69 74 integer or if it
20f9d 20 69 73 20 74 6f 6f 20 62 69 67 0a 2a 2a 20 74 is too big.** t
20f9e 6f 20 66 69 74 20 69 6e 20 61 20 73 69 67 6e 65 o fit in a signe
20f9f 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 d 32-bit integer
20fa0 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20 6c , return 0 and l
20fa1 65 61 76 65 20 2a 70 56 61 6c 75 65 20 75 6e 63 eave *pValue unc
20fa2 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 hanged..*/.SQLIT
20fa3 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
20fa4 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 lite3ExprIsInteg
20fa5 65 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 er(Expr *p, int
20fa6 2a 70 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 *pValue){. int
20fa7 72 63 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 2d rc = 0;. if( p-
20fa8 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56 >flags & EP_IntV
20fa9 61 6c 75 65 20 29 7b 0a 20 20 20 20 2a 70 56 61 alue ){. *pVa
20faa 6c 75 65 20 3d 20 70 2d 3e 75 2e 69 56 61 6c 75 lue = p->u.iValu
20fab 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b e;. return 1;
20fac 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20 70 . }. switch( p
20fad 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 ->op ){. case
20fae 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 TK_INTEGER: {.
20faf 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
20fb0 33 47 65 74 49 6e 74 33 32 28 70 2d 3e 75 2e 7a 3GetInt32(p->u.z
20fb1 54 6f 6b 65 6e 2c 20 70 56 61 6c 75 65 29 3b 0a Token, pValue);.
20fb2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 assert( rc
20fb3 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 ==0 );. bre
20fb4 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
20fb5 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 se TK_UPLUS: {.
20fb6 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
20fb7 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 3ExprIsInteger(p
20fb8 2d 3e 70 4c 65 66 74 2c 20 70 56 61 6c 75 65 29 ->pLeft, pValue)
20fb9 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
20fba 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b }. case TK
20fbb 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 _UMINUS: {.
20fbc 20 69 6e 74 20 76 3b 0a 20 20 20 20 20 20 69 66 int v;. if
20fbd 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 ( sqlite3ExprIsI
20fbe 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 66 74 2c nteger(p->pLeft,
20fbf 20 26 76 29 20 29 7b 0a 20 20 20 20 20 20 20 20 &v) ){.
20fc0 2a 70 56 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 *pValue = -v;.
20fc1 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 rc = 1;.
20fc2 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
20fc3 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 k;. }. def
20fc4 61 75 6c 74 3a 20 62 72 65 61 6b 3b 0a 20 20 7d ault: break;. }
20fc5 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 . if( rc ){.
20fc6 20 61 73 73 65 72 74 28 20 45 78 70 72 48 61 73 assert( ExprHas
20fc7 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 AnyProperty(p, E
20fc8 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b P_Reduced|EP_Tok
20fc9 65 6e 4f 6e 6c 79 29 0a 20 20 20 20 20 20 20 20 enOnly).
20fca 20 20 20 20 20 20 20 7c 7c 20 28 70 2d 3e 66 6c || (p->fl
20fcb 61 67 73 32 20 26 20 45 50 32 5f 4d 61 6c 6c 6f ags2 & EP2_Mallo
20fcc 63 65 64 54 6f 6b 65 6e 29 3d 3d 30 20 29 3b 0a cedToken)==0 );.
20fcd 20 20 20 20 70 2d 3e 6f 70 20 3d 20 54 4b 5f 49 p->op = TK_I
20fce 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 2d 3e 66 NTEGER;. p->f
20fcf 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 lags |= EP_IntVa
20fd0 6c 75 65 3b 0a 20 20 20 20 70 2d 3e 75 2e 69 56 lue;. p->u.iV
20fd1 61 6c 75 65 20 3d 20 2a 70 56 61 6c 75 65 3b 0a alue = *pValue;.
20fd2 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
20fd3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
20fd4 20 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76 TRUE if the giv
20fd5 65 6e 20 73 74 72 69 6e 67 20 69 73 20 61 20 72 en string is a r
20fd6 6f 77 2d 69 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d ow-id column nam
20fd7 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
20fd8 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
20fd9 49 73 52 6f 77 69 64 28 63 6f 6e 73 74 20 63 68 IsRowid(const ch
20fda 61 72 20 2a 7a 29 7b 0a 20 20 69 66 28 20 73 71 ar *z){. if( sq
20fdb 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 lite3StrICmp(z,
20fdc 22 5f 52 4f 57 49 44 5f 22 29 3d 3d 30 20 29 20 "_ROWID_")==0 )
20fdd 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 return 1;. if(
20fde 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a sqlite3StrICmp(z
20fdf 2c 20 22 52 4f 57 49 44 22 29 3d 3d 30 20 29 20 , "ROWID")==0 )
20fe0 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 return 1;. if(
20fe1 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a sqlite3StrICmp(z
20fe2 2c 20 22 4f 49 44 22 29 3d 3d 30 20 29 20 72 65 , "OID")==0 ) re
20fe3 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 75 72 6e turn 1;. return
20fe4 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 0;.}../*.** Ret
20fe5 75 72 6e 20 74 72 75 65 20 69 66 20 77 65 20 61 urn true if we a
20fe6 72 65 20 61 62 6c 65 20 74 6f 20 74 68 65 20 49 re able to the I
20fe7 4e 20 6f 70 65 72 61 74 6f 72 20 6f 70 74 69 6d N operator optim
20fe8 69 7a 61 74 69 6f 6e 20 6f 6e 20 61 0a 2a 2a 20 ization on a.**
20fe9 71 75 65 72 79 20 6f 66 20 74 68 65 20 66 6f 72 query of the for
20fea 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 78 20 m.**.** x
20feb 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 0a IN (SELECT ...).
20fec 2a 2a 0a 2a 2a 20 57 68 65 72 65 20 74 68 65 20 **.** Where the
20fed 53 45 4c 45 43 54 2e 2e 2e 20 63 6c 61 75 73 65 SELECT... clause
20fee 20 69 73 20 61 73 20 73 70 65 63 69 66 69 65 64 is as specified
20fef 20 62 79 20 74 68 65 20 70 61 72 61 6d 65 74 65 by the paramete
20ff0 72 20 74 6f 20 74 68 69 73 0a 2a 2a 20 72 6f 75 r to this.** rou
20ff1 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 tine..**.** The
20ff2 53 65 6c 65 63 74 20 6f 62 6a 65 63 74 20 70 61 Select object pa
20ff3 73 73 65 64 20 69 6e 20 68 61 73 20 61 6c 72 65 ssed in has alre
20ff4 61 64 79 20 62 65 65 6e 20 70 72 65 70 72 6f 63 ady been preproc
20ff5 65 73 73 65 64 20 61 6e 64 20 6e 6f 0a 2a 2a 20 essed and no.**
20ff6 65 72 72 6f 72 73 20 68 61 76 65 20 62 65 65 6e errors have been
20ff7 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 23 69 66 6e 64 found..*/.#ifnd
20ff8 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
20ff9 55 42 51 55 45 52 59 0a 73 74 61 74 69 63 20 69 UBQUERY.static i
20ffa 6e 74 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f nt isCandidateFo
20ffb 72 49 6e 4f 70 74 28 53 65 6c 65 63 74 20 2a 70 rInOpt(Select *p
20ffc 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 ){. SrcList *pS
20ffd 72 63 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a rc;. ExprList *
20ffe 70 45 4c 69 73 74 3b 0a 20 20 54 61 62 6c 65 20 pEList;. Table
20fff 2a 70 54 61 62 3b 0a 20 20 69 66 28 20 70 3d 3d *pTab;. if( p==
21000 30 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 0 ) return 0;
21001 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21002 2f 2a 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 /* right-hand si
21003 64 65 20 6f 66 20 49 4e 20 69 73 20 53 45 4c 45 de of IN is SELE
21004 43 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 CT */. if( p->p
21005 50 72 69 6f 72 20 29 20 72 65 74 75 72 6e 20 30 Prior ) return 0
21006 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
21007 2a 20 4e 6f 74 20 61 20 63 6f 6d 70 6f 75 6e 64 * Not a compound
21008 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 66 28 SELECT */. if(
21009 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 p->selFlags & (
2100a 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 SF_Distinct|SF_A
2100b 67 67 72 65 67 61 74 65 29 20 29 7b 0a 20 20 20 ggregate) ){.
2100c 20 74 65 73 74 63 61 73 65 28 20 28 70 2d 3e 73 testcase( (p->s
2100d 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 elFlags & (SF_Di
2100e 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 stinct|SF_Aggreg
2100f 61 74 65 29 29 3d 3d 53 46 5f 44 69 73 74 69 6e ate))==SF_Distin
21010 63 74 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 ct );. testca
21011 73 65 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 se( (p->selFlags
21012 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c & (SF_Distinct|
21013 53 46 5f 41 67 67 72 65 67 61 74 65 29 29 3d 3d SF_Aggregate))==
21014 53 46 5f 41 67 67 72 65 67 61 74 65 20 29 3b 0a SF_Aggregate );.
21015 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a return 0; /*
21016 20 4e 6f 20 44 49 53 54 49 4e 43 54 20 6b 65 79 No DISTINCT key
21017 77 6f 72 64 20 61 6e 64 20 6e 6f 20 61 67 67 72 word and no aggr
21018 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 egate functions
21019 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 */. }. assert(
2101a 20 70 2d 3e 70 47 72 6f 75 70 42 79 3d 3d 30 20 p->pGroupBy==0
2101b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
2101c 2f 2a 20 48 61 73 20 6e 6f 20 47 52 4f 55 50 20 /* Has no GROUP
2101d 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 BY clause */. i
2101e 66 28 20 70 2d 3e 70 4c 69 6d 69 74 20 29 20 72 f( p->pLimit ) r
2101f 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 eturn 0;
21020 20 20 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 /* Has no
21021 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 2a 2f 0a LIMIT clause */.
21022 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f 66 assert( p->pOf
21023 66 73 65 74 3d 3d 30 20 29 3b 20 20 20 20 20 20 fset==0 );
21024 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20 4c /* No L
21025 49 4d 49 54 20 6d 65 61 6e 73 20 6e 6f 20 4f 46 IMIT means no OF
21026 46 53 45 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d FSET */. if( p-
21027 3e 70 57 68 65 72 65 20 29 20 72 65 74 75 72 6e >pWhere ) return
21028 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
21029 20 2f 2a 20 48 61 73 20 6e 6f 20 57 48 45 52 45 /* Has no WHERE
2102a 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 70 53 72 clause */. pSr
2102b 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 61 c = p->pSrc;. a
2102c 73 73 65 72 74 28 20 70 53 72 63 21 3d 30 20 29 ssert( pSrc!=0 )
2102d 3b 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 6e 53 ;. if( pSrc->nS
2102e 72 63 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30 rc!=1 ) return 0
2102f 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 ; /* Si
21030 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20 46 52 4f ngle term in FRO
21031 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 M clause */. if
21032 28 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 ( pSrc->a[0].pSe
21033 6c 65 63 74 20 29 20 72 65 74 75 72 6e 20 30 3b lect ) return 0;
21034 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 69 73 20 /* FROM is
21035 6e 6f 74 20 61 20 73 75 62 71 75 65 72 79 20 6f not a subquery o
21036 72 20 76 69 65 77 20 2a 2f 0a 20 20 70 54 61 62 r view */. pTab
21037 20 3d 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 = pSrc->a[0].pT
21038 61 62 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 ab;. if( NEVER(
21039 70 54 61 62 3d 3d 30 29 20 29 20 72 65 74 75 72 pTab==0) ) retur
2103a 6e 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 n 0;. assert( p
2103b 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 Tab->pSelect==0
2103c 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ); /*
2103d 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 FROM clause is
2103e 6e 6f 74 20 61 20 76 69 65 77 20 2a 2f 0a 20 20 not a view */.
2103f 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 if( IsVirtual(pT
21040 61 62 29 20 29 20 72 65 74 75 72 6e 20 30 3b 20 ab) ) return 0;
21041 20 20 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 /* FROM c
21042 6c 61 75 73 65 20 6e 6f 74 20 61 20 76 69 72 74 lause not a virt
21043 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 70 ual table */. p
21044 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 EList = p->pELis
21045 74 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d t;. if( pEList-
21046 3e 6e 45 78 70 72 21 3d 31 20 29 20 72 65 74 75 >nExpr!=1 ) retu
21047 72 6e 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f rn 0; /* O
21048 6e 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 ne column in the
21049 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 result set */.
2104a 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b 30 if( pEList->a[0
2104b 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f ].pExpr->op!=TK_
2104c 43 4f 4c 55 4d 4e 20 29 20 72 65 74 75 72 6e 20 COLUMN ) return
2104d 30 3b 20 2f 2a 20 52 65 73 75 6c 74 20 69 73 20 0; /* Result is
2104e 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 72 65 a column */. re
2104f 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 turn 1;.}.#endif
21050 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
21051 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a SUBQUERY */../*.
21052 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
21053 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 is used by the
21054 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
21055 66 20 74 68 65 20 49 4e 20 28 2e 2e 2e 29 20 6f f the IN (...) o
21056 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 49 74 27 73 perator..** It's
21057 20 6a 6f 62 20 69 73 20 74 6f 20 66 69 6e 64 20 job is to find
21058 6f 72 20 63 72 65 61 74 65 20 61 20 62 2d 74 72 or create a b-tr
21059 65 65 20 73 74 72 75 63 74 75 72 65 20 74 68 61 ee structure tha
2105a 74 20 6d 61 79 20 62 65 20 75 73 65 64 0a 2a 2a t may be used.**
2105b 20 65 69 74 68 65 72 20 74 6f 20 74 65 73 74 20 either to test
2105c 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70 20 6f for membership o
2105d 66 20 74 68 65 20 28 2e 2e 2e 29 20 73 65 74 20 f the (...) set
2105e 6f 72 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 or to iterate th
2105f 72 6f 75 67 68 0a 2a 2a 20 69 74 73 20 6d 65 6d rough.** its mem
21060 62 65 72 73 2c 20 73 6b 69 70 70 69 6e 67 20 64 bers, skipping d
21061 75 70 6c 69 63 61 74 65 73 2e 0a 2a 2a 0a 2a 2a uplicates..**.**
21062 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 The index of th
21063 65 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 e cursor opened
21064 6f 6e 20 74 68 65 20 62 2d 74 72 65 65 20 28 64 on the b-tree (d
21065 61 74 61 62 61 73 65 20 74 61 62 6c 65 2c 20 64 atabase table, d
21066 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 0a 2a atabase index .*
21067 2a 20 6f 72 20 65 70 68 65 72 6d 61 6c 20 74 61 * or ephermal ta
21068 62 6c 65 29 20 69 73 20 73 74 6f 72 65 64 20 69 ble) is stored i
21069 6e 20 70 58 2d 3e 69 54 61 62 6c 65 20 62 65 66 n pX->iTable bef
2106a 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f ore this functio
2106b 6e 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 20 54 68 n returns..** Th
2106c 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 e returned value
2106d 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f of this functio
2106e 6e 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 20 n indicates the
2106f 62 2d 74 72 65 65 20 74 79 70 65 2c 20 61 73 20 b-tree type, as
21070 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 follows:.**.**
21071 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 IN_INDEX_ROWID
21072 2d 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 73 - The cursor was
21073 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 74 opened on a dat
21074 61 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 abase table..**
21075 20 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 IN_INDEX_INDEX
21076 20 2d 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 - The cursor wa
21077 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 s opened on a da
21078 74 61 62 61 73 65 20 69 6e 64 65 78 2e 0a 2a 2a tabase index..**
21079 20 20 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20 IN_INDEX_EPH
2107a 2d 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 77 - The cursor w
2107b 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 73 as opened on a s
2107c 70 65 63 69 61 6c 6c 79 20 63 72 65 61 74 65 64 pecially created
2107d 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 and.**
2107e 20 20 20 20 20 20 20 20 20 20 20 70 6f 70 75 6c popul
2107f 61 74 65 64 20 65 70 68 65 72 65 6d 61 6c 20 74 ated epheremal t
21080 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 able..**.** An e
21081 78 69 73 74 69 6e 67 20 62 2d 74 72 65 65 20 6d xisting b-tree m
21082 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 ay only be used
21083 69 66 20 74 68 65 20 53 45 4c 45 43 54 20 69 73 if the SELECT is
21084 20 6f 66 20 74 68 65 20 73 69 6d 70 6c 65 0a 2a of the simple.*
21085 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 * form:.**.**
21086 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c 75 6d 6e SELECT <column
21087 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65 3e 0a 2a > FROM <table>.*
21088 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 4e 6f *.** If the prNo
21089 74 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 tFound parameter
2108a 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68 65 20 is 0, then the
2108b 62 2d 74 72 65 65 20 77 69 6c 6c 20 62 65 20 75 b-tree will be u
2108c 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 0a 2a sed to iterate.*
2108d 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 73 65 * through the se
2108e 74 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70 70 t members, skipp
2108f 69 6e 67 20 61 6e 79 20 64 75 70 6c 69 63 61 74 ing any duplicat
21090 65 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 es. In this case
21091 20 61 6e 0a 2a 2a 20 65 70 68 65 72 65 6d 61 6c an.** epheremal
21092 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 75 table must be u
21093 73 65 64 20 75 6e 6c 65 73 73 20 74 68 65 20 73 sed unless the s
21094 65 6c 65 63 74 65 64 20 3c 63 6f 6c 75 6d 6e 3e elected <column>
21095 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 0a 2a is guaranteed.*
21096 2a 20 74 6f 20 62 65 20 75 6e 69 71 75 65 20 2d * to be unique -
21097 20 65 69 74 68 65 72 20 62 65 63 61 75 73 65 20 either because
21098 69 74 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52 it is an INTEGER
21099 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 PRIMARY KEY or
2109a 69 74 0a 2a 2a 20 68 61 73 20 61 20 55 4e 49 51 it.** has a UNIQ
2109b 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 72 UE constraint or
2109c 20 55 4e 49 51 55 45 20 69 6e 64 65 78 2e 0a 2a UNIQUE index..*
2109d 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 4e 6f *.** If the prNo
2109e 74 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 tFound parameter
2109f 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 is not 0, then
210a0 74 68 65 20 62 2d 74 72 65 65 20 77 69 6c 6c 20 the b-tree will
210a1 62 65 20 75 73 65 64 20 0a 2a 2a 20 66 6f 72 20 be used .** for
210a2 66 61 73 74 20 73 65 74 20 6d 65 6d 62 65 72 73 fast set members
210a3 68 69 70 20 74 65 73 74 73 2e 20 49 6e 20 74 68 hip tests. In th
210a4 69 73 20 63 61 73 65 20 61 6e 20 65 70 68 65 72 is case an epher
210a5 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 74 20 emal table must
210a6 0a 2a 2a 20 62 65 20 75 73 65 64 20 75 6e 6c 65 .** be used unle
210a7 73 73 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73 20 61 ss <column> is a
210a8 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 n INTEGER PRIMAR
210a9 59 20 4b 45 59 20 6f 72 20 61 6e 20 69 6e 64 65 Y KEY or an inde
210aa 78 20 63 61 6e 20 0a 2a 2a 20 62 65 20 66 6f 75 x can .** be fou
210ab 6e 64 20 77 69 74 68 20 3c 63 6f 6c 75 6d 6e 3e nd with <column>
210ac 20 61 73 20 69 74 73 20 6c 65 66 74 2d 6d 6f 73 as its left-mos
210ad 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 t column..**.**
210ae 57 68 65 6e 20 74 68 65 20 62 2d 74 72 65 65 20 When the b-tree
210af 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 66 6f is being used fo
210b0 72 20 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73 r membership tes
210b1 74 73 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 ts, the calling
210b2 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6e 65 65 64 function.** need
210b3 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 74 68 65 s to know whethe
210b4 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 73 74 72 r or not the str
210b5 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 ucture contains
210b6 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 0a 2a 2a 20 an SQL NULL .**
210b7 76 61 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74 value in order t
210b8 6f 20 63 6f 72 72 65 63 74 6c 79 20 65 76 61 6c o correctly eval
210b9 75 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e 73 uate expressions
210ba 20 6c 69 6b 65 20 22 58 20 49 4e 20 28 59 2c 20 like "X IN (Y,
210bb 5a 29 22 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 Z)"..** If there
210bc 20 69 73 20 61 20 63 68 61 6e 63 65 20 74 68 61 is a chance tha
210bd 74 20 74 68 65 20 62 2d 74 72 65 65 20 6d 69 67 t the b-tree mig
210be 68 74 20 63 6f 6e 74 61 69 6e 20 61 20 4e 55 4c ht contain a NUL
210bf 4c 20 76 61 6c 75 65 20 61 74 0a 2a 2a 20 72 75 L value at.** ru
210c0 6e 74 69 6d 65 2c 20 74 68 65 6e 20 61 20 72 65 ntime, then a re
210c1 67 69 73 74 65 72 20 69 73 20 61 6c 6c 6f 63 61 gister is alloca
210c2 74 65 64 20 61 6e 64 20 74 68 65 20 72 65 67 69 ted and the regi
210c3 73 74 65 72 20 6e 75 6d 62 65 72 20 77 72 69 74 ster number writ
210c4 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 72 4e 6f 74 ten.** to *prNot
210c5 46 6f 75 6e 64 2e 20 49 66 20 74 68 65 72 65 20 Found. If there
210c6 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 61 is no chance tha
210c7 74 20 74 68 65 20 62 2d 74 72 65 65 20 63 6f 6e t the b-tree con
210c8 74 61 69 6e 73 20 61 0a 2a 2a 20 4e 55 4c 4c 20 tains a.** NULL
210c9 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a 70 72 4e value, then *prN
210ca 6f 74 46 6f 75 6e 64 20 69 73 20 6c 65 66 74 20 otFound is left
210cb 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a unchanged..**.**
210cc 20 49 66 20 61 20 72 65 67 69 73 74 65 72 20 69 If a register i
210cd 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 s allocated and
210ce 69 74 73 20 6c 6f 63 61 74 69 6f 6e 20 73 74 6f its location sto
210cf 72 65 64 20 69 6e 20 2a 70 72 4e 6f 74 46 6f 75 red in *prNotFou
210d0 6e 64 2c 20 74 68 65 6e 0a 2a 2a 20 69 74 73 20 nd, then.** its
210d1 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 69 73 initial value is
210d2 20 4e 55 4c 4c 2e 20 49 66 20 74 68 65 20 62 2d NULL. If the b-
210d3 74 72 65 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 tree does not re
210d4 6d 61 69 6e 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a main constant.**
210d5 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f for the duratio
210d6 6e 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 28 n of the query (
210d7 69 2e 65 2e 20 74 68 65 20 53 45 4c 45 43 54 20 i.e. the SELECT
210d8 74 68 61 74 20 67 65 6e 65 72 61 74 65 73 20 74 that generates t
210d9 68 65 20 62 2d 74 72 65 65 0a 2a 2a 20 69 73 20 he b-tree.** is
210da 61 20 63 6f 72 72 65 6c 61 74 65 64 20 73 75 62 a correlated sub
210db 71 75 65 72 79 29 20 74 68 65 6e 20 74 68 65 20 query) then the
210dc 76 61 6c 75 65 20 6f 66 20 74 68 65 20 61 6c 6c value of the all
210dd 6f 63 61 74 65 64 20 72 65 67 69 73 74 65 72 20 ocated register
210de 69 73 0a 2a 2a 20 72 65 73 65 74 20 74 6f 20 4e is.** reset to N
210df 55 4c 4c 20 65 61 63 68 20 74 69 6d 65 20 74 68 ULL each time th
210e0 65 20 62 2d 74 72 65 65 20 69 73 20 72 65 70 6f e b-tree is repo
210e1 70 75 6c 61 74 65 64 2e 20 54 68 69 73 20 61 6c pulated. This al
210e2 6c 6f 77 73 20 74 68 65 0a 2a 2a 20 63 61 6c 6c lows the.** call
210e3 65 72 20 74 6f 20 75 73 65 20 76 64 62 65 20 63 er to use vdbe c
210e4 6f 64 65 20 65 71 75 69 76 61 6c 65 6e 74 20 74 ode equivalent t
210e5 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a o the following:
210e6 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 72 65 67 .**.** if( reg
210e7 69 73 74 65 72 3d 3d 4e 55 4c 4c 20 29 7b 0a 2a ister==NULL ){.*
210e8 2a 20 20 20 20 20 68 61 73 5f 6e 75 6c 6c 20 3d * has_null =
210e9 20 3c 74 65 73 74 20 69 66 20 64 61 74 61 20 73 <test if data s
210ea 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e tructure contain
210eb 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 20 20 20 20 72 s null>.** r
210ec 65 67 69 73 74 65 72 20 3d 20 31 0a 2a 2a 20 20 egister = 1.**
210ed 20 7d 0a 2a 2a 0a 2a 2a 20 69 6e 20 6f 72 64 65 }.**.** in orde
210ee 72 20 74 6f 20 61 76 6f 69 64 20 72 75 6e 6e 69 r to avoid runni
210ef 6e 67 20 74 68 65 20 3c 74 65 73 74 20 69 66 20 ng the <test if
210f0 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63 data structure c
210f1 6f 6e 74 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a ontains null>.**
210f2 20 74 65 73 74 20 6d 6f 72 65 20 6f 66 74 65 6e test more often
210f3 20 74 68 61 6e 20 69 73 20 6e 65 63 65 73 73 61 than is necessa
210f4 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ry..*/.#ifndef S
210f5 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
210f6 45 52 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ERY.SQLITE_PRIVA
210f7 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 TE int sqlite3Fi
210f8 6e 64 49 6e 49 6e 64 65 78 28 50 61 72 73 65 20 ndInIndex(Parse
210f9 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 *pParse, Expr *p
210fa 58 2c 20 69 6e 74 20 2a 70 72 4e 6f 74 46 6f 75 X, int *prNotFou
210fb 6e 64 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70 nd){. Select *p
210fc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
210fd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
210fe 53 45 4c 45 43 54 20 74 6f 20 74 68 65 20 72 69 SELECT to the ri
210ff 67 68 74 20 6f 66 20 49 4e 20 6f 70 65 72 61 74 ght of IN operat
21100 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 70 or */. int eTyp
21101 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 e = 0;
21102 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
21103 20 54 79 70 65 20 6f 66 20 52 48 53 20 74 61 62 Type of RHS tab
21104 6c 65 2e 20 49 4e 5f 49 4e 44 45 58 5f 2a 20 2a le. IN_INDEX_* *
21105 2f 0a 20 20 69 6e 74 20 69 54 61 62 20 3d 20 70 /. int iTab = p
21106 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20 Parse->nTab++;
21107 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 /* Cur
21108 73 6f 72 20 6f 66 20 74 68 65 20 52 48 53 20 74 sor of the RHS t
21109 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 75 able */. int mu
2110a 73 74 42 65 55 6e 69 71 75 65 20 3d 20 28 70 72 stBeUnique = (pr
2110b 4e 6f 74 46 6f 75 6e 64 3d 3d 30 29 3b 20 20 20 NotFound==0);
2110c 2f 2a 20 54 72 75 65 20 69 66 20 52 48 53 20 6d /* True if RHS m
2110d 75 73 74 20 62 65 20 75 6e 69 71 75 65 20 2a 2f ust be unique */
2110e 0a 0a 20 20 61 73 73 65 72 74 28 20 70 58 2d 3e .. assert( pX->
2110f 6f 70 3d 3d 54 4b 5f 49 4e 20 29 3b 0a 0a 20 20 op==TK_IN );..
21110 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 /* Check to see
21111 69 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 74 if an existing t
21112 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 63 61 able or index ca
21113 6e 20 62 65 20 75 73 65 64 20 74 6f 0a 20 20 2a n be used to. *
21114 2a 20 73 61 74 69 73 66 79 20 74 68 65 20 71 75 * satisfy the qu
21115 65 72 79 2e 20 20 54 68 69 73 20 69 73 20 70 72 ery. This is pr
21116 65 66 65 72 61 62 6c 65 20 74 6f 20 67 65 6e 65 eferable to gene
21117 72 61 74 69 6e 67 20 61 20 6e 65 77 20 0a 20 20 rating a new .
21118 2a 2a 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 ** ephemeral tab
21119 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20 28 le.. */. p = (
2111a 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
2111b 70 58 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 pX, EP_xIsSelect
2111c 29 20 3f 20 70 58 2d 3e 78 2e 70 53 65 6c 65 63 ) ? pX->x.pSelec
2111d 74 20 3a 20 30 29 3b 0a 20 20 69 66 28 20 41 4c t : 0);. if( AL
2111e 57 41 59 53 28 70 50 61 72 73 65 2d 3e 6e 45 72 WAYS(pParse->nEr
2111f 72 3d 3d 30 29 20 26 26 20 69 73 43 61 6e 64 69 r==0) && isCandi
21120 64 61 74 65 46 6f 72 49 6e 4f 70 74 28 70 29 20 dateForInOpt(p)
21121 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a ){. sqlite3 *
21122 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
21123 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
21124 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 Database connec
21125 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 45 78 70 72 tion */. Expr
21126 20 2a 70 45 78 70 72 20 3d 20 70 2d 3e 70 45 4c *pExpr = p->pEL
21127 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b ist->a[0].pExpr;
21128 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e /* Expression
21129 20 3c 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a 20 20 20 <column> */.
2112a 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 45 78 70 int iCol = pExp
2112b 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 r->iColumn;
2112c 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
2112d 20 6f 66 20 63 6f 6c 75 6d 6e 20 3c 63 6f 6c 75 of column <colu
2112e 6d 6e 3e 20 2a 2f 0a 20 20 20 20 56 64 62 65 20 mn> */. Vdbe
2112f 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 *v = sqlite3GetV
21130 64 62 65 28 70 50 61 72 73 65 29 3b 20 20 20 20 dbe(pParse);
21131 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 6d 61 63 /* Virtual mac
21132 68 69 6e 65 20 62 65 69 6e 67 20 63 6f 64 65 64 hine being coded
21133 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 */. Table *p
21134 54 61 62 20 3d 20 70 2d 3e 70 53 72 63 2d 3e 61 Tab = p->pSrc->a
21135 5b 30 5d 2e 70 54 61 62 3b 20 20 20 20 20 20 2f [0].pTab; /
21136 2a 20 54 61 62 6c 65 20 3c 74 61 62 6c 65 3e 2e * Table <table>.
21137 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62 3b */. int iDb;
21138 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21139 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
2113a 2a 20 44 61 74 61 62 61 73 65 20 69 64 78 20 66 * Database idx f
2113b 6f 72 20 70 54 61 62 20 2a 2f 0a 20 20 20 0a 20 or pTab */. .
2113c 20 20 20 2f 2a 20 43 6f 64 65 20 61 6e 20 4f 50 /* Code an OP
2113d 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 61 6e _VerifyCookie an
2113e 64 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 66 d OP_TableLock f
2113f 6f 72 20 3c 74 61 62 6c 65 3e 2e 20 2a 2f 0a 20 or <table>. */.
21140 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 iDb = sqlite3
21141 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 SchemaToIndex(db
21142 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 , pTab->pSchema)
21143 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 ;. sqlite3Cod
21144 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 eVerifySchema(pP
21145 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 arse, iDb);.
21146 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b sqlite3TableLock
21147 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54 (pParse, iDb, pT
21148 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61 ab->tnum, 0, pTa
21149 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20 b->zName);..
2114a 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e /* This function
2114b 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 is only called
2114c 66 72 6f 6d 20 74 77 6f 20 70 6c 61 63 65 73 2e from two places.
2114d 20 49 6e 20 62 6f 74 68 20 63 61 73 65 73 20 74 In both cases t
2114e 68 65 20 76 64 62 65 0a 20 20 20 20 2a 2a 20 68 he vdbe. ** h
2114f 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
21150 61 6c 6c 6f 63 61 74 65 64 2e 20 53 6f 20 61 73 allocated. So as
21151 73 75 6d 65 20 73 71 6c 69 74 65 33 47 65 74 56 sume sqlite3GetV
21152 64 62 65 28 29 20 69 73 20 61 6c 77 61 79 73 0a dbe() is always.
21153 20 20 20 20 2a 2a 20 73 75 63 63 65 73 73 66 75 ** successfu
21154 6c 20 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 l here.. */.
21155 20 20 20 61 73 73 65 72 74 28 76 29 3b 0a 20 20 assert(v);.
21156 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a if( iCol<0 ){.
21157 20 20 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d int iMem =
21158 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
21159 0a 20 20 20 20 20 20 69 6e 74 20 69 41 64 64 72 . int iAddr
2115a 3b 0a 0a 20 20 20 20 20 20 69 41 64 64 72 20 3d ;.. iAddr =
2115b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
2115c 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 p1(v, OP_If, iMe
2115d 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 m);. sqlite
2115e 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
2115f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 4d P_Integer, 1, iM
21160 65 6d 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 em);.. sqli
21161 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 te3OpenTable(pPa
21162 72 73 65 2c 20 69 54 61 62 2c 20 69 44 62 2c 20 rse, iTab, iDb,
21163 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 pTab, OP_OpenRea
21164 64 29 3b 0a 20 20 20 20 20 20 65 54 79 70 65 20 d);. eType
21165 3d 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 = IN_INDEX_ROWID
21166 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ;.. sqlite3
21167 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 VdbeJumpHere(v,
21168 69 41 64 64 72 29 3b 0a 20 20 20 20 7d 65 6c 73 iAddr);. }els
21169 65 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a e{. Index *
2116a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 pIdx;
2116b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
2116c 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 Iterator variab
2116d 6c 65 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 le */.. /*
2116e 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 The collation se
2116f 71 75 65 6e 63 65 20 75 73 65 64 20 62 79 20 74 quence used by t
21170 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 49 he comparison. I
21171 66 20 61 6e 20 69 6e 64 65 78 20 69 73 20 74 6f f an index is to
21172 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 75 73 65 . ** be use
21173 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 d in place of a
21174 74 65 6d 70 2d 74 61 62 6c 65 2c 20 69 74 20 6d temp-table, it m
21175 75 73 74 20 62 65 20 6f 72 64 65 72 65 64 20 61 ust be ordered a
21176 63 63 6f 72 64 69 6e 67 0a 20 20 20 20 20 20 2a ccording. *
21177 2a 20 74 6f 20 74 68 69 73 20 63 6f 6c 6c 61 74 * to this collat
21178 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e 20 20 2a ion sequence. *
21179 2f 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 /. CollSeq
2117a 2a 70 52 65 71 20 3d 20 73 71 6c 69 74 65 33 42 *pReq = sqlite3B
2117b 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c inaryCompareColl
2117c 53 65 71 28 70 50 61 72 73 65 2c 20 70 58 2d 3e Seq(pParse, pX->
2117d 70 4c 65 66 74 2c 20 70 45 78 70 72 29 3b 0a 0a pLeft, pExpr);..
2117e 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 /* Check t
2117f 68 61 74 20 74 68 65 20 61 66 66 69 6e 69 74 79 hat the affinity
21180 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73 that will be us
21181 65 64 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 ed to perform th
21182 65 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 e . ** comp
21183 61 72 69 73 6f 6e 20 69 73 20 74 68 65 20 73 61 arison is the sa
21184 6d 65 20 61 73 20 74 68 65 20 61 66 66 69 6e 69 me as the affini
21185 74 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e ty of the column
21186 2e 20 49 66 0a 20 20 20 20 20 20 2a 2a 20 69 74 . If. ** it
21187 20 69 73 20 6e 6f 74 2c 20 69 74 20 69 73 20 6e is not, it is n
21188 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75 ot possible to u
21189 73 65 20 61 6e 79 20 69 6e 64 65 78 2e 0a 20 20 se any index..
2118a 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 */. cha
2118b 72 20 61 66 66 20 3d 20 63 6f 6d 70 61 72 69 73 r aff = comparis
2118c 6f 6e 41 66 66 69 6e 69 74 79 28 70 58 29 3b 0a onAffinity(pX);.
2118d 20 20 20 20 20 20 69 6e 74 20 61 66 66 69 6e 69 int affini
2118e 74 79 5f 6f 6b 20 3d 20 28 70 54 61 62 2d 3e 61 ty_ok = (pTab->a
2118f 43 6f 6c 5b 69 43 6f 6c 5d 2e 61 66 66 69 6e 69 Col[iCol].affini
21190 74 79 3d 3d 61 66 66 7c 7c 61 66 66 3d 3d 53 51 ty==aff||aff==SQ
21191 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 29 3b 0a LITE_AFF_NONE);.
21192 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d . for(pIdx=
21193 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 pTab->pIndex; pI
21194 64 78 20 26 26 20 65 54 79 70 65 3d 3d 30 20 26 dx && eType==0 &
21195 26 20 61 66 66 69 6e 69 74 79 5f 6f 6b 3b 20 70 & affinity_ok; p
21196 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 Idx=pIdx->pNext)
21197 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 70 {. if( (p
21198 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d Idx->aiColumn[0]
21199 3d 3d 69 43 6f 6c 29 0a 20 20 20 20 20 20 20 20 ==iCol).
2119a 20 26 26 20 73 71 6c 69 74 65 33 46 69 6e 64 43 && sqlite3FindC
2119b 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 ollSeq(db, ENC(d
2119c 62 29 2c 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c b), pIdx->azColl
2119d 5b 30 5d 2c 20 30 29 3d 3d 70 52 65 71 0a 20 20 [0], 0)==pReq.
2119e 20 20 20 20 20 20 20 26 26 20 28 21 6d 75 73 74 && (!must
2119f 42 65 55 6e 69 71 75 65 20 7c 7c 20 28 70 49 64 BeUnique || (pId
211a0 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 26 26 x->nColumn==1 &&
211a1 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d pIdx->onError!=
211a2 4f 45 5f 4e 6f 6e 65 29 29 0a 20 20 20 20 20 20 OE_None)).
211a3 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 ){. i
211a4 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 nt iMem = ++pPar
211a5 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 se->nMem;.
211a6 20 20 20 20 69 6e 74 20 69 41 64 64 72 3b 0a 20 int iAddr;.
211a7 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 70 char *p
211a8 4b 65 79 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 Key;. .
211a9 20 20 70 4b 65 79 20 3d 20 28 63 68 61 72 20 2a pKey = (char *
211aa 29 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 )sqlite3IndexKey
211ab 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64 info(pParse, pId
211ac 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 41 x);. iA
211ad 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 ddr = sqlite3Vdb
211ae 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 eAddOp1(v, OP_If
211af 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 , iMem);.
211b0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
211b1 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 dOp2(v, OP_Integ
211b2 65 72 2c 20 31 2c 20 69 4d 65 6d 29 3b 0a 20 20 er, 1, iMem);.
211b3 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
211b4 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
211b5 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 54 61 OP_OpenRead, iTa
211b6 62 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 b, pIdx->tnum, i
211b7 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 Db,.
211b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
211b9 20 20 20 70 4b 65 79 2c 50 34 5f 4b 45 59 49 4e pKey,P4_KEYIN
211ba 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 FO_HANDOFF);.
211bb 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 VdbeComme
211bc 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49 64 nt((v, "%s", pId
211bd 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 x->zName));.
211be 20 20 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e eType = IN
211bf 5f 49 4e 44 45 58 5f 49 4e 44 45 58 3b 0a 0a 20 _INDEX_INDEX;..
211c0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
211c1 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 VdbeJumpHere(v,
211c2 69 41 64 64 72 29 3b 0a 20 20 20 20 20 20 20 20 iAddr);.
211c3 20 20 69 66 28 20 70 72 4e 6f 74 46 6f 75 6e 64 if( prNotFound
211c4 20 26 26 20 21 70 54 61 62 2d 3e 61 43 6f 6c 5b && !pTab->aCol[
211c5 69 43 6f 6c 5d 2e 6e 6f 74 4e 75 6c 6c 20 29 7b iCol].notNull ){
211c6 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 72 . *pr
211c7 4e 6f 74 46 6f 75 6e 64 20 3d 20 2b 2b 70 50 61 NotFound = ++pPa
211c8 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 rse->nMem;.
211c9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
211ca 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
211cb 20 7d 0a 0a 20 20 69 66 28 20 65 54 79 70 65 3d }.. if( eType=
211cc 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 75 =0 ){. /* Cou
211cd 6c 64 20 6e 6f 74 20 66 6f 75 6e 64 20 61 6e 20 ld not found an
211ce 65 78 69 73 74 69 6e 67 20 74 61 62 6c 65 20 6f existing table o
211cf 72 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20 61 r index to use a
211d0 73 20 74 68 65 20 52 48 53 20 62 2d 74 72 65 65 s the RHS b-tree
211d1 2e 0a 20 20 20 20 2a 2a 20 57 65 20 77 69 6c 6c .. ** We will
211d2 20 68 61 76 65 20 74 6f 20 67 65 6e 65 72 61 74 have to generat
211d3 65 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 74 e an ephemeral t
211d4 61 62 6c 65 20 74 6f 20 64 6f 20 74 68 65 20 6a able to do the j
211d5 6f 62 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ob.. */. i
211d6 6e 74 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 nt rMayHaveNull
211d7 3d 20 30 3b 0a 20 20 20 20 65 54 79 70 65 20 3d = 0;. eType =
211d8 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 3b 0a 20 IN_INDEX_EPH;.
211d9 20 20 20 69 66 28 20 70 72 4e 6f 74 46 6f 75 6e if( prNotFoun
211da 64 20 29 7b 0a 20 20 20 20 20 20 2a 70 72 4e 6f d ){. *prNo
211db 74 46 6f 75 6e 64 20 3d 20 72 4d 61 79 48 61 76 tFound = rMayHav
211dc 65 4e 75 6c 6c 20 3d 20 2b 2b 70 50 61 72 73 65 eNull = ++pParse
211dd 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d 65 6c 73 ->nMem;. }els
211de 65 20 69 66 28 20 70 58 2d 3e 70 4c 65 66 74 2d e if( pX->pLeft-
211df 3e 69 43 6f 6c 75 6d 6e 3c 30 20 26 26 20 21 45 >iColumn<0 && !E
211e0 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 xprHasAnyPropert
211e1 79 28 70 58 2c 20 45 50 5f 78 49 73 53 65 6c 65 y(pX, EP_xIsSele
211e2 63 74 29 20 29 7b 0a 20 20 20 20 20 20 65 54 79 ct) ){. eTy
211e3 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 52 4f pe = IN_INDEX_RO
211e4 57 49 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 WID;. }. s
211e5 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c qlite3CodeSubsel
211e6 65 63 74 28 70 50 61 72 73 65 2c 20 70 58 2c 20 ect(pParse, pX,
211e7 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 65 54 rMayHaveNull, eT
211e8 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f ype==IN_INDEX_RO
211e9 57 49 44 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 WID);. }else{.
211ea 20 20 20 70 58 2d 3e 69 54 61 62 6c 65 20 3d 20 pX->iTable =
211eb 69 54 61 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75 iTab;. }. retu
211ec 72 6e 20 65 54 79 70 65 3b 0a 7d 0a 23 65 6e 64 rn eType;.}.#end
211ed 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 if../*.** Genera
211ee 74 65 20 63 6f 64 65 20 66 6f 72 20 73 63 61 6c te code for scal
211ef 61 72 20 73 75 62 71 75 65 72 69 65 73 20 75 73 ar subqueries us
211f0 65 64 20 61 73 20 61 6e 20 65 78 70 72 65 73 73 ed as an express
211f1 69 6f 6e 0a 2a 2a 20 61 6e 64 20 49 4e 20 6f 70 ion.** and IN op
211f2 65 72 61 74 6f 72 73 2e 20 20 45 78 61 6d 70 6c erators. Exampl
211f3 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 53 es:.**.** (S
211f4 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 62 29 20 ELECT a FROM b)
211f5 20 20 20 20 20 20 20 20 20 2d 2d 20 73 75 62 71 -- subq
211f6 75 65 72 79 0a 2a 2a 20 20 20 20 20 45 58 49 53 uery.** EXIS
211f7 54 53 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f TS (SELECT a FRO
211f8 4d 20 62 29 20 20 20 2d 2d 20 45 58 49 53 54 53 M b) -- EXISTS
211f9 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20 20 20 subquery.**
211fa 20 78 20 49 4e 20 28 34 2c 35 2c 31 31 29 20 20 x IN (4,5,11)
211fb 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 49 -- I
211fc 4e 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20 N operator with
211fd 6c 69 73 74 20 6f 6e 20 72 69 67 68 74 2d 68 61 list on right-ha
211fe 6e 64 20 73 69 64 65 0a 2a 2a 20 20 20 20 20 78 nd side.** x
211ff 20 49 4e 20 28 53 45 4c 45 43 54 20 61 20 46 52 IN (SELECT a FR
21200 4f 4d 20 62 29 20 20 20 20 20 2d 2d 20 49 4e 20 OM b) -- IN
21201 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20 73 75 operator with su
21202 62 71 75 65 72 79 20 6f 6e 20 74 68 65 20 72 69 bquery on the ri
21203 67 68 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 45 ght.**.** The pE
21204 78 70 72 20 70 61 72 61 6d 65 74 65 72 20 64 65 xpr parameter de
21205 73 63 72 69 62 65 73 20 74 68 65 20 65 78 70 72 scribes the expr
21206 65 73 73 69 6f 6e 20 74 68 61 74 20 63 6f 6e 74 ession that cont
21207 61 69 6e 73 20 74 68 65 20 49 4e 0a 2a 2a 20 6f ains the IN.** o
21208 70 65 72 61 74 6f 72 20 6f 72 20 73 75 62 71 75 perator or subqu
21209 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 ery..**.** If pa
2120a 72 61 6d 65 74 65 72 20 69 73 52 6f 77 69 64 20 rameter isRowid
2120b 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
2120c 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 n expression pEx
2120d 70 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 pr is guaranteed
2120e 0a 2a 2a 20 74 6f 20 62 65 20 6f 66 20 74 68 65 .** to be of the
2120f 20 66 6f 72 6d 20 22 3c 72 6f 77 69 64 3e 20 49 form "<rowid> I
21210 4e 20 28 3f 2c 20 3f 2c 20 3f 29 22 2c 20 77 68 N (?, ?, ?)", wh
21211 65 72 65 20 3c 72 6f 77 69 64 3e 20 69 73 20 61 ere <rowid> is a
21212 20 72 65 66 65 72 65 6e 63 65 0a 2a 2a 20 74 6f reference.** to
21213 20 73 6f 6d 65 20 69 6e 74 65 67 65 72 20 6b 65 some integer ke
21214 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 74 61 y column of a ta
21215 62 6c 65 20 42 2d 54 72 65 65 2e 20 49 6e 20 74 ble B-Tree. In t
21216 68 69 73 20 63 61 73 65 2c 20 75 73 65 20 61 6e his case, use an
21217 0a 2a 2a 20 69 6e 74 6b 65 79 20 42 2d 54 72 65 .** intkey B-Tre
21218 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 e to store the s
21219 65 74 20 6f 66 20 49 4e 28 2e 2e 2e 29 20 76 61 et of IN(...) va
2121a 6c 75 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20 lues instead of
2121b 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 28 73 6c the usual.** (sl
2121c 6f 77 65 72 29 20 76 61 72 69 61 62 6c 65 20 6c ower) variable l
2121d 65 6e 67 74 68 20 6b 65 79 73 20 42 2d 54 72 65 ength keys B-Tre
2121e 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 4d 61 79 e..**.** If rMay
2121f 48 61 76 65 4e 75 6c 6c 20 69 73 20 6e 6f 6e 2d HaveNull is non-
21220 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 zero, that means
21221 20 74 68 61 74 20 74 68 65 20 6f 70 65 72 61 74 that the operat
21222 69 6f 6e 20 69 73 20 61 6e 20 49 4e 0a 2a 2a 20 ion is an IN.**
21223 28 6e 6f 74 20 61 20 53 45 4c 45 43 54 20 6f 72 (not a SELECT or
21224 20 45 58 49 53 54 53 29 20 61 6e 64 20 74 68 61 EXISTS) and tha
21225 74 20 74 68 65 20 52 48 53 20 6d 69 67 68 74 20 t the RHS might
21226 63 6f 6e 74 61 69 6e 73 20 4e 55 4c 4c 73 2e 0a contains NULLs..
21227 2a 2a 20 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 ** Furthermore,
21228 74 68 65 20 49 4e 20 69 73 20 69 6e 20 61 20 57 the IN is in a W
21229 48 45 52 45 20 63 6c 61 75 73 65 20 61 6e 64 20 HERE clause and
2122a 74 68 61 74 20 77 65 20 72 65 61 6c 6c 79 20 77 that we really w
2122b 61 6e 74 0a 2a 2a 20 74 6f 20 69 74 65 72 61 74 ant.** to iterat
2122c 65 20 6f 76 65 72 20 74 68 65 20 52 48 53 20 6f e over the RHS o
2122d 66 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f f the IN operato
2122e 72 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 71 75 r in order to qu
2122f 69 63 6b 6c 79 20 6c 6f 63 61 74 65 0a 2a 2a 20 ickly locate.**
21230 61 6c 6c 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e all correspondin
21231 67 20 4c 48 53 20 65 6c 65 6d 65 6e 74 73 2e 20 g LHS elements.
21232 20 41 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e All this routin
21233 65 20 64 6f 65 73 20 69 73 20 69 6e 69 74 69 61 e does is initia
21234 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 72 65 67 69 lize.** the regi
21235 73 74 65 72 20 67 69 76 65 6e 20 62 79 20 72 4d ster given by rM
21236 61 79 48 61 76 65 4e 75 6c 6c 20 74 6f 20 4e 55 ayHaveNull to NU
21237 4c 4c 2e 20 20 43 61 6c 6c 69 6e 67 20 72 6f 75 LL. Calling rou
21238 74 69 6e 65 73 20 77 69 6c 6c 20 74 61 6b 65 0a tines will take.
21239 2a 2a 20 63 61 72 65 20 6f 66 20 63 68 61 6e 67 ** care of chang
2123a 69 6e 67 20 74 68 69 73 20 72 65 67 69 73 74 65 ing this registe
2123b 72 20 76 61 6c 75 65 20 74 6f 20 6e 6f 6e 2d 4e r value to non-N
2123c 55 4c 4c 20 69 66 20 74 68 65 20 52 48 53 20 69 ULL if the RHS i
2123d 73 20 4e 55 4c 4c 2d 66 72 65 65 2e 0a 2a 2a 0a s NULL-free..**.
2123e 2a 2a 20 49 66 20 72 4d 61 79 48 61 76 65 4e 75 ** If rMayHaveNu
2123f 6c 6c 20 69 73 20 7a 65 72 6f 2c 20 74 68 61 74 ll is zero, that
21240 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
21241 73 75 62 71 75 65 72 79 20 69 73 20 62 65 69 6e subquery is bein
21242 67 20 75 73 65 64 0a 2a 2a 20 66 6f 72 20 6d 65 g used.** for me
21243 6d 62 65 72 73 68 69 70 20 74 65 73 74 69 6e 67 mbership testing
21244 20 6f 6e 6c 79 2e 20 20 54 68 65 72 65 20 69 73 only. There is
21245 20 6e 6f 20 6e 65 65 64 20 74 6f 20 69 6e 69 74 no need to init
21246 69 61 6c 69 7a 65 20 61 6e 79 0a 2a 2a 20 72 65 ialize any.** re
21247 67 69 73 74 65 72 73 20 74 6f 20 69 6e 64 69 63 gisters to indic
21248 61 74 65 20 74 68 65 20 70 72 65 73 65 6e 73 65 ate the presense
21249 20 6f 72 20 61 62 73 65 6e 63 65 20 6f 66 20 4e or absence of N
2124a 55 4c 4c 73 20 6f 6e 20 74 68 65 20 52 48 53 2e ULLs on the RHS.
2124b 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 53 45 4c .**.** For a SEL
2124c 45 43 54 20 6f 72 20 45 58 49 53 54 53 20 6f 70 ECT or EXISTS op
2124d 65 72 61 74 6f 72 2c 20 72 65 74 75 72 6e 20 74 erator, return t
2124e 68 65 20 72 65 67 69 73 74 65 72 20 74 68 61 74 he register that
2124f 20 68 6f 6c 64 73 20 74 68 65 0a 2a 2a 20 72 65 holds the.** re
21250 73 75 6c 74 2e 20 20 46 6f 72 20 49 4e 20 6f 70 sult. For IN op
21251 65 72 61 74 6f 72 73 20 6f 72 20 69 66 20 61 6e erators or if an
21252 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 error occurs, t
21253 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
21254 69 73 20 30 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 is 0..*/.#ifndef
21255 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 SQLITE_OMIT_SUB
21256 51 55 45 52 59 0a 53 51 4c 49 54 45 5f 50 52 49 QUERY.SQLITE_PRI
21257 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
21258 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28 0a 20 CodeSubselect(.
21259 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
2125a 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 /* Pars
2125b 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 ing context */.
2125c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 Expr *pExpr,
2125d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
2125e 49 4e 2c 20 53 45 4c 45 43 54 2c 20 6f 72 20 45 IN, SELECT, or E
2125f 58 49 53 54 53 20 6f 70 65 72 61 74 6f 72 20 2a XISTS operator *
21260 2f 0a 20 20 69 6e 74 20 72 4d 61 79 48 61 76 65 /. int rMayHave
21261 4e 75 6c 6c 2c 20 20 20 20 20 20 20 2f 2a 20 52 Null, /* R
21262 65 67 69 73 74 65 72 20 74 68 61 74 20 72 65 63 egister that rec
21263 6f 72 64 73 20 77 68 65 74 68 65 72 20 4e 55 4c ords whether NUL
21264 4c 73 20 65 78 69 73 74 20 69 6e 20 52 48 53 20 Ls exist in RHS
21265 2a 2f 0a 20 20 69 6e 74 20 69 73 52 6f 77 69 64 */. int isRowid
21266 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
21267 49 66 20 74 72 75 65 2c 20 4c 48 53 20 6f 66 20 If true, LHS of
21268 49 4e 20 6f 70 65 72 61 74 6f 72 20 69 73 20 61 IN operator is a
21269 20 72 6f 77 69 64 20 2a 2f 0a 29 7b 0a 20 20 69 rowid */.){. i
2126a 6e 74 20 74 65 73 74 41 64 64 72 20 3d 20 30 3b nt testAddr = 0;
2126b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2126c 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 2d 74 69 /* One-ti
2126d 6d 65 20 74 65 73 74 20 61 64 64 72 65 73 73 20 me test address
2126e 2a 2f 0a 20 20 69 6e 74 20 72 52 65 67 20 3d 20 */. int rReg =
2126f 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
21270 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
21271 52 65 67 69 73 74 65 72 20 73 74 6f 72 69 6e 67 Register storing
21272 20 72 65 73 75 6c 74 69 6e 67 20 2a 2f 0a 20 20 resulting */.
21273 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 Vdbe *v = sqlite
21274 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3GetVdbe(pParse)
21275 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 76 3d ;. if( NEVER(v=
21276 3d 30 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a =0) ) return 0;.
21277 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
21278 68 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a hePush(pParse);.
21279 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f 64 65 20 . /* This code
2127a 6d 75 73 74 20 62 65 20 72 75 6e 20 69 6e 20 69 must be run in i
2127b 74 73 20 65 6e 74 69 72 65 74 79 20 65 76 65 72 ts entirety ever
2127c 79 20 74 69 6d 65 20 69 74 20 69 73 20 65 6e 63 y time it is enc
2127d 6f 75 6e 74 65 72 65 64 0a 20 20 2a 2a 20 69 66 ountered. ** if
2127e 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c any of the foll
2127f 6f 77 69 6e 67 20 69 73 20 74 72 75 65 3a 0a 20 owing is true:.
21280 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a 20 20 54 **. ** * T
21281 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 he right-hand si
21282 64 65 20 69 73 20 61 20 63 6f 72 72 65 6c 61 74 de is a correlat
21283 65 64 20 73 75 62 71 75 65 72 79 0a 20 20 2a 2a ed subquery. **
21284 20 20 20 20 2a 20 20 54 68 65 20 72 69 67 68 74 * The right
21285 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20 61 6e -hand side is an
21286 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 expression list
21287 20 63 6f 6e 74 61 69 6e 69 6e 67 20 76 61 72 69 containing vari
21288 61 62 6c 65 73 0a 20 20 2a 2a 20 20 20 20 2a 20 ables. ** *
21289 20 57 65 20 61 72 65 20 69 6e 73 69 64 65 20 61 We are inside a
2128a 20 74 72 69 67 67 65 72 0a 20 20 2a 2a 0a 20 20 trigger. **.
2128b 2a 2a 20 49 66 20 61 6c 6c 20 6f 66 20 74 68 65 ** If all of the
2128c 20 61 62 6f 76 65 20 61 72 65 20 66 61 6c 73 65 above are false
2128d 2c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 72 75 , then we can ru
2128e 6e 20 74 68 69 73 20 63 6f 64 65 20 6a 75 73 74 n this code just
2128f 20 6f 6e 63 65 0a 20 20 2a 2a 20 73 61 76 65 20 once. ** save
21290 74 68 65 20 72 65 73 75 6c 74 73 2c 20 61 6e 64 the results, and
21291 20 72 65 75 73 65 20 74 68 65 20 73 61 6d 65 20 reuse the same
21292 72 65 73 75 6c 74 20 6f 6e 20 73 75 62 73 65 71 result on subseq
21293 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 73 uent invocations
21294 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 45 78 .. */. if( !Ex
21295 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 prHasAnyProperty
21296 28 70 45 78 70 72 2c 20 45 50 5f 56 61 72 53 65 (pExpr, EP_VarSe
21297 6c 65 63 74 29 20 26 26 20 21 70 50 61 72 73 65 lect) && !pParse
21298 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20 29 7b ->pTriggerTab ){
21299 0a 20 20 20 20 69 6e 74 20 6d 65 6d 20 3d 20 2b . int mem = +
2129a 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 +pParse->nMem;.
2129b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
2129c 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 6d dOp1(v, OP_If, m
2129d 65 6d 29 3b 0a 20 20 20 20 74 65 73 74 41 64 64 em);. testAdd
2129e 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 r = sqlite3VdbeA
2129f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 ddOp2(v, OP_Inte
212a0 67 65 72 2c 20 31 2c 20 6d 65 6d 29 3b 0a 20 20 ger, 1, mem);.
212a1 20 20 61 73 73 65 72 74 28 20 74 65 73 74 41 64 assert( testAd
212a2 64 72 3e 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e dr>0 || pParse->
212a3 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
212a4 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63 );. }.. switc
212a5 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a h( pExpr->op ){.
212a6 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20 case TK_IN:
212a7 7b 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 66 {. char aff
212a8 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 4b 65 79 inity;. Key
212a9 49 6e 66 6f 20 6b 65 79 49 6e 66 6f 3b 0a 20 20 Info keyInfo;.
212aa 20 20 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 int addr;
212ab 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 /* Address
212ac 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 of OP_OpenEpheme
212ad 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ral instruction
212ae 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 */. Expr *p
212af 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c Left = pExpr->pL
212b0 65 66 74 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 eft;.. if(
212b1 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 29 7b 0a rMayHaveNull ){.
212b2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
212b3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
212b4 4e 75 6c 6c 2c 20 30 2c 20 72 4d 61 79 48 61 76 Null, 0, rMayHav
212b5 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 7d 0a eNull);. }.
212b6 0a 20 20 20 20 20 20 61 66 66 69 6e 69 74 79 20 . affinity
212b7 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 = sqlite3ExprAff
212b8 69 6e 69 74 79 28 70 4c 65 66 74 29 3b 0a 0a 20 inity(pLeft);..
212b9 20 20 20 20 20 2f 2a 20 57 68 65 74 68 65 72 20 /* Whether
212ba 74 68 69 73 20 69 73 20 61 6e 20 27 78 20 49 4e this is an 'x IN
212bb 28 53 45 4c 45 43 54 2e 2e 2e 29 27 20 6f 72 20 (SELECT...)' or
212bc 61 6e 20 27 78 20 49 4e 28 3c 65 78 70 72 6c 69 an 'x IN(<exprli
212bd 73 74 3e 29 27 0a 20 20 20 20 20 20 2a 2a 20 65 st>)'. ** e
212be 78 70 72 65 73 73 69 6f 6e 20 69 74 20 69 73 20 xpression it is
212bf 68 61 6e 64 6c 65 64 20 74 68 65 20 73 61 6d 65 handled the same
212c0 20 77 61 79 2e 20 41 20 76 69 72 74 75 61 6c 20 way. A virtual
212c1 74 61 62 6c 65 20 69 73 20 0a 20 20 20 20 20 20 table is .
212c2 2a 2a 20 66 69 6c 6c 65 64 20 77 69 74 68 20 73 ** filled with s
212c3 69 6e 67 6c 65 2d 66 69 65 6c 64 20 69 6e 64 65 ingle-field inde
212c4 78 20 6b 65 79 73 20 72 65 70 72 65 73 65 6e 74 x keys represent
212c5 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 73 0a ing the results.
212c6 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 ** from th
212c7 65 20 53 45 4c 45 43 54 20 6f 72 20 74 68 65 20 e SELECT or the
212c8 3c 65 78 70 72 6c 69 73 74 3e 2e 0a 20 20 20 20 <exprlist>..
212c9 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 **. ** If
212ca 20 74 68 65 20 27 78 27 20 65 78 70 72 65 73 73 the 'x' express
212cb 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 ion is a column
212cc 76 61 6c 75 65 2c 20 6f 72 20 74 68 65 20 53 45 value, or the SE
212cd 4c 45 43 54 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a LECT.... **
212ce 20 73 74 61 74 65 6d 65 6e 74 20 72 65 74 75 72 statement retur
212cf 6e 73 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 ns a column valu
212d0 65 2c 20 74 68 65 6e 20 74 68 65 20 61 66 66 69 e, then the affi
212d1 6e 69 74 79 20 6f 66 20 74 68 61 74 0a 20 20 20 nity of that.
212d2 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 ** column is
212d3 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20 74 68 used to build th
212d4 65 20 69 6e 64 65 78 20 6b 65 79 73 2e 20 49 66 e index keys. If
212d5 20 62 6f 74 68 20 27 78 27 20 61 6e 64 20 74 68 both 'x' and th
212d6 65 0a 20 20 20 20 20 20 2a 2a 20 53 45 4c 45 43 e. ** SELEC
212d7 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 20 61 T... statement a
212d8 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e re columns, then
212d9 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 74 numeric affinit
212da 79 20 69 73 20 75 73 65 64 0a 20 20 20 20 20 20 y is used.
212db 2a 2a 20 69 66 20 65 69 74 68 65 72 20 63 6f 6c ** if either col
212dc 75 6d 6e 20 68 61 73 20 4e 55 4d 45 52 49 43 20 umn has NUMERIC
212dd 6f 72 20 49 4e 54 45 47 45 52 20 61 66 66 69 6e or INTEGER affin
212de 69 74 79 2e 20 49 66 20 6e 65 69 74 68 65 72 0a ity. If neither.
212df 20 20 20 20 20 20 2a 2a 20 27 78 27 20 6e 6f 72 ** 'x' nor
212e0 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 20 73 the SELECT... s
212e1 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f 6c tatement are col
212e2 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d 65 72 umns, then numer
212e3 69 63 20 61 66 66 69 6e 69 74 79 0a 20 20 20 20 ic affinity.
212e4 20 20 2a 2a 20 69 73 20 75 73 65 64 2e 0a 20 20 ** is used..
212e5 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 45 78 */. pEx
212e6 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 50 61 pr->iTable = pPa
212e7 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 rse->nTab++;.
212e8 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 addr = sqlite
212e9 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
212ea 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c P_OpenEphemeral,
212eb 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 pExpr->iTable,
212ec 21 69 73 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 !isRowid);.
212ed 20 6d 65 6d 73 65 74 28 26 6b 65 79 49 6e 66 6f memset(&keyInfo
212ee 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6b 65 79 49 , 0, sizeof(keyI
212ef 6e 66 6f 29 29 3b 0a 20 20 20 20 20 20 6b 65 79 nfo));. key
212f0 49 6e 66 6f 2e 6e 46 69 65 6c 64 20 3d 20 31 3b Info.nField = 1;
212f1 0a 0a 20 20 20 20 20 20 69 66 28 20 45 78 70 72 .. if( Expr
212f2 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 HasProperty(pExp
212f3 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 r, EP_xIsSelect)
212f4 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 ){. /* C
212f5 61 73 65 20 31 3a 20 20 20 20 20 65 78 70 72 20 ase 1: expr
212f6 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 0a IN (SELECT ...).
212f7 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
212f8 20 20 20 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 ** Generate c
212f9 6f 64 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 ode to write the
212fa 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 results of the
212fb 73 65 6c 65 63 74 20 69 6e 74 6f 20 74 68 65 20 select into the
212fc 74 65 6d 70 6f 72 61 72 79 0a 20 20 20 20 20 20 temporary.
212fd 20 20 2a 2a 20 74 61 62 6c 65 20 61 6c 6c 6f 63 ** table alloc
212fe 61 74 65 64 20 61 6e 64 20 6f 70 65 6e 65 64 20 ated and opened
212ff 61 62 6f 76 65 2e 0a 20 20 20 20 20 20 20 20 2a above.. *
21300 2f 0a 20 20 20 20 20 20 20 20 53 65 6c 65 63 74 /. Select
21301 44 65 73 74 20 64 65 73 74 3b 0a 20 20 20 20 20 Dest dest;.
21302 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c ExprList *pEL
21303 69 73 74 3b 0a 0a 20 20 20 20 20 20 20 20 61 73 ist;.. as
21304 73 65 72 74 28 20 21 69 73 52 6f 77 69 64 20 29 sert( !isRowid )
21305 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
21306 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 3SelectDestInit(
21307 26 64 65 73 74 2c 20 53 52 54 5f 53 65 74 2c 20 &dest, SRT_Set,
21308 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 29 3b 0a pExpr->iTable);.
21309 20 20 20 20 20 20 20 20 64 65 73 74 2e 61 66 66 dest.aff
2130a 69 6e 69 74 79 20 3d 20 28 75 38 29 61 66 66 69 inity = (u8)affi
2130b 6e 69 74 79 3b 0a 20 20 20 20 20 20 20 20 61 73 nity;. as
2130c 73 65 72 74 28 20 28 70 45 78 70 72 2d 3e 69 54 sert( (pExpr->iT
2130d 61 62 6c 65 26 30 78 30 30 30 30 46 46 46 46 29 able&0x0000FFFF)
2130e 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 ==pExpr->iTable
2130f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 );. if( s
21310 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 qlite3Select(pPa
21311 72 73 65 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 rse, pExpr->x.pS
21312 65 6c 65 63 74 2c 20 26 64 65 73 74 29 20 29 7b elect, &dest) ){
21313 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
21314 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 n 0;. }.
21315 20 20 20 20 20 20 20 70 45 4c 69 73 74 20 3d 20 pEList =
21316 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 pExpr->x.pSelect
21317 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 20 20 ->pEList;.
21318 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 45 4c if( ALWAYS(pEL
21319 69 73 74 21 3d 30 20 26 26 20 70 45 4c 69 73 74 ist!=0 && pEList
2131a 2d 3e 6e 45 78 70 72 3e 30 29 20 29 7b 20 0a 20 ->nExpr>0) ){ .
2131b 20 20 20 20 20 20 20 20 20 6b 65 79 49 6e 66 6f keyInfo
2131c 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69 .aColl[0] = sqli
2131d 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 te3BinaryCompare
2131e 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 CollSeq(pParse,
2131f 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 0a 20 20 pExpr->pLeft,.
21320 20 20 20 20 20 20 20 20 20 20 20 20 70 45 4c 69 pELi
21321 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b st->a[0].pExpr);
21322 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
21323 20 7d 65 6c 73 65 20 69 66 28 20 70 45 78 70 72 }else if( pExpr
21324 2d 3e 78 2e 70 4c 69 73 74 21 3d 30 20 29 7b 0a ->x.pList!=0 ){.
21325 20 20 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 /* Case
21326 32 3a 20 20 20 20 20 65 78 70 72 20 49 4e 20 28 2: expr IN (
21327 65 78 70 72 6c 69 73 74 29 0a 20 20 20 20 20 20 exprlist).
21328 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 **. **
21329 46 6f 72 20 65 61 63 68 20 65 78 70 72 65 73 73 For each express
2132a 69 6f 6e 2c 20 62 75 69 6c 64 20 61 6e 20 69 6e ion, build an in
2132b 64 65 78 20 6b 65 79 20 66 72 6f 6d 20 74 68 65 dex key from the
2132c 20 65 76 61 6c 75 61 74 69 6f 6e 20 61 6e 64 0a evaluation and.
2132d 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 ** store
2132e 20 69 74 20 69 6e 20 74 68 65 20 74 65 6d 70 6f it in the tempo
2132f 72 61 72 79 20 74 61 62 6c 65 2e 20 49 66 20 3c rary table. If <
21330 65 78 70 72 3e 20 69 73 20 61 20 63 6f 6c 75 6d expr> is a colum
21331 6e 2c 20 74 68 65 6e 20 75 73 65 0a 20 20 20 20 n, then use.
21332 20 20 20 20 2a 2a 20 74 68 61 74 20 63 6f 6c 75 ** that colu
21333 6d 6e 73 20 61 66 66 69 6e 69 74 79 20 77 68 65 mns affinity whe
21334 6e 20 62 75 69 6c 64 69 6e 67 20 69 6e 64 65 78 n building index
21335 20 6b 65 79 73 2e 20 49 66 20 3c 65 78 70 72 3e keys. If <expr>
21336 20 69 73 20 6e 6f 74 0a 20 20 20 20 20 20 20 20 is not.
21337 2a 2a 20 61 20 63 6f 6c 75 6d 6e 2c 20 75 73 65 ** a column, use
21338 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 74 numeric affinit
21339 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 y.. */.
2133a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 int i;.
2133b 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 ExprList *p
2133c 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e List = pExpr->x.
2133d 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 73 pList;. s
2133e 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 truct ExprList_i
2133f 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 tem *pItem;.
21340 20 20 20 20 69 6e 74 20 72 31 2c 20 72 32 2c 20 int r1, r2,
21341 72 33 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28 r3;.. if(
21342 20 21 61 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 !affinity ){.
21343 20 20 20 20 20 20 20 20 61 66 66 69 6e 69 74 79 affinity
21344 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f = SQLITE_AFF_NO
21345 4e 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 NE;. }.
21346 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 keyInfo.aC
21347 6f 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 oll[0] = sqlite3
21348 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 ExprCollSeq(pPar
21349 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 se, pExpr->pLeft
2134a 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4c );.. /* L
2134b 6f 6f 70 20 74 68 72 6f 75 67 68 20 65 61 63 68 oop through each
2134c 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 3c expression in <
2134d 65 78 70 72 6c 69 73 74 3e 2e 20 2a 2f 0a 20 20 exprlist>. */.
2134e 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 r1 = sqlit
2134f 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 e3GetTempReg(pPa
21350 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 72 32 rse);. r2
21351 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d = sqlite3GetTem
21352 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 pReg(pParse);.
21353 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21354 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 eAddOp2(v, OP_Nu
21355 6c 6c 2c 20 30 2c 20 72 32 29 3b 0a 20 20 20 20 ll, 0, r2);.
21356 20 20 20 20 66 6f 72 28 69 3d 70 4c 69 73 74 2d for(i=pList-
21357 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d 70 4c >nExpr, pItem=pL
21358 69 73 74 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d ist->a; i>0; i--
21359 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 , pItem++){.
2135a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 32 20 Expr *pE2
2135b 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a = pItem->pExpr;.
2135c 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 56 int iV
2135d 61 6c 54 6f 49 6e 73 3b 0a 0a 20 20 20 20 20 20 alToIns;..
2135e 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65 78 /* If the ex
2135f 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74 20 pression is not
21360 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20 77 65 constant then we
21361 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 0a 20 20 will need to.
21362 20 20 20 20 20 20 20 20 2a 2a 20 64 69 73 61 62 ** disab
21363 6c 65 20 74 68 65 20 74 65 73 74 20 74 68 61 74 le the test that
21364 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 61 was generated a
21365 62 6f 76 65 20 74 68 61 74 20 6d 61 6b 65 73 20 bove that makes
21366 73 75 72 65 0a 20 20 20 20 20 20 20 20 20 20 2a sure. *
21367 2a 20 74 68 69 73 20 63 6f 64 65 20 6f 6e 6c 79 * this code only
21368 20 65 78 65 63 75 74 65 73 20 6f 6e 63 65 2e 20 executes once.
21369 20 42 65 63 61 75 73 65 20 66 6f 72 20 61 20 6e Because for a n
2136a 6f 6e 2d 63 6f 6e 73 74 61 6e 74 0a 20 20 20 20 on-constant.
2136b 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 ** express
2136c 69 6f 6e 20 77 65 20 6e 65 65 64 20 74 6f 20 72 ion we need to r
2136d 65 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20 65 erun this code e
2136e 61 63 68 20 74 69 6d 65 2e 0a 20 20 20 20 20 20 ach time..
2136f 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 */.
21370 20 69 66 28 20 74 65 73 74 41 64 64 72 20 26 26 if( testAddr &&
21371 20 21 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 !sqlite3ExprIsC
21372 6f 6e 73 74 61 6e 74 28 70 45 32 29 20 29 7b 0a onstant(pE2) ){.
21373 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
21374 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e te3VdbeChangeToN
21375 6f 6f 70 28 76 2c 20 74 65 73 74 41 64 64 72 2d oop(v, testAddr-
21376 31 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 20 1, 2);.
21377 20 20 20 74 65 73 74 41 64 64 72 20 3d 20 30 3b testAddr = 0;
21378 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 . }..
21379 20 20 20 20 20 20 20 20 2f 2a 20 45 76 61 6c 75 /* Evalu
2137a 61 74 65 20 74 68 65 20 65 78 70 72 65 73 73 69 ate the expressi
2137b 6f 6e 20 61 6e 64 20 69 6e 73 65 72 74 20 69 74 on and insert it
2137c 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 20 74 into the temp t
2137d 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 able */.
2137e 20 20 69 66 28 20 69 73 52 6f 77 69 64 20 26 26 if( isRowid &&
2137f 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e sqlite3ExprIsIn
21380 74 65 67 65 72 28 70 45 32 2c 20 26 69 56 61 6c teger(pE2, &iVal
21381 54 6f 49 6e 73 29 20 29 7b 0a 20 20 20 20 20 20 ToIns) ){.
21382 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21383 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e eAddOp3(v, OP_In
21384 73 65 72 74 49 6e 74 2c 20 70 45 78 70 72 2d 3e sertInt, pExpr->
21385 69 54 61 62 6c 65 2c 20 72 32 2c 20 69 56 61 6c iTable, r2, iVal
21386 54 6f 49 6e 73 29 3b 0a 20 20 20 20 20 20 20 20 ToIns);.
21387 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
21388 20 20 20 20 20 72 33 20 3d 20 73 71 6c 69 74 65 r3 = sqlite
21389 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 3ExprCodeTarget(
2138a 70 50 61 72 73 65 2c 20 70 45 32 2c 20 72 31 29 pParse, pE2, r1)
2138b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 ;. if
2138c 28 20 69 73 52 6f 77 69 64 20 29 7b 0a 20 20 20 ( isRowid ){.
2138d 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
2138e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
2138f 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72 33 OP_MustBeInt, r3
21390 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
21391 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21392 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 sqlite3VdbeCur
21393 72 65 6e 74 41 64 64 72 28 76 29 2b 32 29 3b 0a rentAddr(v)+2);.
21394 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 sq
21395 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
21396 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 70 45 v, OP_Insert, pE
21397 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 32 2c xpr->iTable, r2,
21398 20 72 33 29 3b 0a 20 20 20 20 20 20 20 20 20 20 r3);.
21399 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
2139a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
2139b 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d beAddOp4(v, OP_M
2139c 61 6b 65 52 65 63 6f 72 64 2c 20 72 33 2c 20 31 akeRecord, r3, 1
2139d 2c 20 72 32 2c 20 26 61 66 66 69 6e 69 74 79 2c , r2, &affinity,
2139e 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 1);.
2139f 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
213a0 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 cheAffinityChang
213a1 65 28 70 50 61 72 73 65 2c 20 72 33 2c 20 31 29 e(pParse, r3, 1)
213a2 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
213a3 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
213a4 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 2(v, OP_IdxInser
213a5 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 t, pExpr->iTable
213a6 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 20 , r2);.
213a7 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d }. }
213a8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
213a9 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 sqlite3Releas
213aa 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c eTempReg(pParse,
213ab 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 r1);. sq
213ac 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
213ad 52 65 67 28 70 50 61 72 73 65 2c 20 72 32 29 3b Reg(pParse, r2);
213ae 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
213af 66 28 20 21 69 73 52 6f 77 69 64 20 29 7b 0a 20 f( !isRowid ){.
213b0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
213b1 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 beChangeP4(v, ad
213b2 64 72 2c 20 28 76 6f 69 64 20 2a 29 26 6b 65 79 dr, (void *)&key
213b3 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f Info, P4_KEYINFO
213b4 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
213b5 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 break;. }..
213b6 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54 case TK_EXIST
213b7 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 S:. case TK_S
213b8 45 4c 45 43 54 3a 0a 20 20 20 20 64 65 66 61 75 ELECT:. defau
213b9 6c 74 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 49 lt: {. /* I
213ba 66 20 74 68 69 73 20 68 61 73 20 74 6f 20 62 65 f this has to be
213bb 20 61 20 73 63 61 6c 61 72 20 53 45 4c 45 43 54 a scalar SELECT
213bc 2e 20 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 . Generate code
213bd 20 74 6f 20 70 75 74 20 74 68 65 0a 20 20 20 20 to put the.
213be 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 ** value of th
213bf 69 73 20 73 65 6c 65 63 74 20 69 6e 20 61 20 6d is select in a m
213c0 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64 20 72 emory cell and r
213c1 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65 72 ecord the number
213c2 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 . ** of the
213c3 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 memory cell in
213c4 69 43 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 68 69 iColumn. If thi
213c5 73 20 69 73 20 61 6e 20 45 58 49 53 54 53 2c 20 s is an EXISTS,
213c6 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 61 write. ** a
213c7 6e 20 69 6e 74 65 67 65 72 20 30 20 28 6e 6f 74 n integer 0 (not
213c8 20 65 78 69 73 74 73 29 20 6f 72 20 31 20 28 65 exists) or 1 (e
213c9 78 69 73 74 73 29 20 69 6e 74 6f 20 61 20 6d 65 xists) into a me
213ca 6d 6f 72 79 20 63 65 6c 6c 0a 20 20 20 20 20 20 mory cell.
213cb 2a 2a 20 61 6e 64 20 72 65 63 6f 72 64 20 74 68 ** and record th
213cc 61 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 at memory cell i
213cd 6e 20 69 43 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 n iColumn..
213ce 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63 */. static
213cf 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 6f 6e 65 const Token one
213d0 20 3d 20 7b 20 22 31 22 2c 20 31 20 7d 3b 20 20 = { "1", 1 };
213d1 2f 2a 20 54 6f 6b 65 6e 20 66 6f 72 20 6c 69 74 /* Token for lit
213d2 65 72 61 6c 20 76 61 6c 75 65 20 31 20 2a 2f 0a eral value 1 */.
213d3 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 Select *pS
213d4 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 el;
213d5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
213d6 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 ELECT statement
213d7 74 6f 20 65 6e 63 6f 64 65 20 2a 2f 0a 20 20 20 to encode */.
213d8 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 SelectDest de
213d9 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 st;
213da 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 /* How
213db 74 6f 20 64 65 61 6c 20 77 69 74 68 20 53 45 4c to deal with SEL
213dc 45 43 74 20 72 65 73 75 6c 74 20 2a 2f 0a 0a 20 ECt result */..
213dd 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 testcase( p
213de 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 Expr->op==TK_EXI
213df 53 54 53 20 29 3b 0a 20 20 20 20 20 20 74 65 73 STS );. tes
213e0 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 tcase( pExpr->op
213e1 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 20 ==TK_SELECT );.
213e2 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 assert( pEx
213e3 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 pr->op==TK_EXIST
213e4 53 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d S || pExpr->op==
213e5 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 0a 20 20 TK_SELECT );..
213e6 20 20 20 20 61 73 73 65 72 74 28 20 45 78 70 72 assert( Expr
213e7 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 HasProperty(pExp
213e8 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 r, EP_xIsSelect)
213e9 20 29 3b 0a 20 20 20 20 20 20 70 53 65 6c 20 3d );. pSel =
213ea 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 pExpr->x.pSelec
213eb 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 t;. sqlite3
213ec 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 SelectDestInit(&
213ed 64 65 73 74 2c 20 30 2c 20 2b 2b 70 50 61 72 73 dest, 0, ++pPars
213ee 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20 e->nMem);.
213ef 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 if( pExpr->op==T
213f0 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20 K_SELECT ){.
213f1 20 20 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d dest.eDest =
213f2 20 53 52 54 5f 4d 65 6d 3b 0a 20 20 20 20 20 20 SRT_Mem;.
213f3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
213f4 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 Op2(v, OP_Null,
213f5 30 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a 0, dest.iParm);.
213f6 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d VdbeComm
213f7 65 6e 74 28 28 76 2c 20 22 49 6e 69 74 20 73 75 ent((v, "Init su
213f8 62 71 75 65 72 79 20 72 65 73 75 6c 74 22 29 29 bquery result"))
213f9 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
213fa 20 20 20 20 20 20 20 64 65 73 74 2e 65 44 65 73 dest.eDes
213fb 74 20 3d 20 53 52 54 5f 45 78 69 73 74 73 3b 0a t = SRT_Exists;.
213fc 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
213fd 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
213fe 49 6e 74 65 67 65 72 2c 20 30 2c 20 64 65 73 74 Integer, 0, dest
213ff 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 20 .iParm);.
21400 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c VdbeComment((v,
21401 20 22 49 6e 69 74 20 45 58 49 53 54 53 20 72 65 "Init EXISTS re
21402 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 20 7d sult"));. }
21403 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
21404 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d prDelete(pParse-
21405 3e 64 62 2c 20 70 53 65 6c 2d 3e 70 4c 69 6d 69 >db, pSel->pLimi
21406 74 29 3b 0a 20 20 20 20 20 20 70 53 65 6c 2d 3e t);. pSel->
21407 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33 pLimit = sqlite3
21408 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b PExpr(pParse, TK
21409 5f 49 4e 54 45 47 45 52 2c 20 30 2c 20 30 2c 20 _INTEGER, 0, 0,
2140a 26 6f 6e 65 29 3b 0a 20 20 20 20 20 20 69 66 28 &one);. if(
2140b 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 sqlite3Select(p
2140c 50 61 72 73 65 2c 20 70 53 65 6c 2c 20 26 64 65 Parse, pSel, &de
2140d 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 st) ){. r
2140e 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d eturn 0;. }
2140f 0a 20 20 20 20 20 20 72 52 65 67 20 3d 20 64 65 . rReg = de
21410 73 74 2e 69 50 61 72 6d 3b 0a 20 20 20 20 20 20 st.iParm;.
21411 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62 ExprSetIrreducib
21412 6c 65 28 70 45 78 70 72 29 3b 0a 20 20 20 20 20 le(pExpr);.
21413 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
21414 7d 0a 0a 20 20 69 66 28 20 74 65 73 74 41 64 64 }.. if( testAdd
21415 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 r ){. sqlite3
21416 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 VdbeJumpHere(v,
21417 74 65 73 74 41 64 64 72 2d 31 29 3b 0a 20 20 7d testAddr-1);. }
21418 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 . sqlite3ExprCa
21419 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 chePop(pParse, 1
2141a 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 52 65 );.. return rRe
2141b 67 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 g;.}.#endif /* S
2141c 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
2141d 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 75 ERY */../*.** Du
2141e 70 6c 69 63 61 74 65 20 61 6e 20 38 2d 62 79 74 plicate an 8-byt
2141f 65 20 76 61 6c 75 65 0a 2a 2f 0a 73 74 61 74 69 e value.*/.stati
21420 63 20 63 68 61 72 20 2a 64 75 70 38 62 79 74 65 c char *dup8byte
21421 73 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e 73 74 s(Vdbe *v, const
21422 20 63 68 61 72 20 2a 69 6e 29 7b 0a 20 20 63 68 char *in){. ch
21423 61 72 20 2a 6f 75 74 20 3d 20 73 71 6c 69 74 65 ar *out = sqlite
21424 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 3DbMallocRaw(sql
21425 69 74 65 33 56 64 62 65 44 62 28 76 29 2c 20 38 ite3VdbeDb(v), 8
21426 29 3b 0a 20 20 69 66 28 20 6f 75 74 20 29 7b 0a );. if( out ){.
21427 20 20 20 20 6d 65 6d 63 70 79 28 6f 75 74 2c 20 memcpy(out,
21428 69 6e 2c 20 38 29 3b 0a 20 20 7d 0a 20 20 72 65 in, 8);. }. re
21429 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a turn out;.}../*.
2142a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20 69 ** Generate an i
2142b 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 nstruction that
2142c 77 69 6c 6c 20 70 75 74 20 74 68 65 20 66 6c 6f will put the flo
2142d 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 76 ating point.** v
2142e 61 6c 75 65 20 64 65 73 63 72 69 62 65 64 20 62 alue described b
2142f 79 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e 74 6f y z[0..n-1] into
21430 20 72 65 67 69 73 74 65 72 20 69 4d 65 6d 2e 0a register iMem..
21431 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 73 74 **.** The z[] st
21432 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62 61 62 ring will probab
21433 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f 2d 74 ly not be zero-t
21434 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 75 74 20 erminated. But
21435 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 68 61 the .** z[n] cha
21436 72 61 63 74 65 72 20 69 73 20 67 75 61 72 61 6e racter is guaran
21437 74 65 65 64 20 74 6f 20 62 65 20 73 6f 6d 65 74 teed to be somet
21438 68 69 6e 67 20 74 68 61 74 20 64 6f 65 73 20 6e hing that does n
21439 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b 65 20 ot look.** like
2143a 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e the continuation
2143b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 2e 0a of the number..
2143c 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
2143d 6f 64 65 52 65 61 6c 28 56 64 62 65 20 2a 76 2c odeReal(Vdbe *v,
2143e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 const char *z,
2143f 69 6e 74 20 6e 65 67 61 74 65 46 6c 61 67 2c 20 int negateFlag,
21440 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 69 66 28 int iMem){. if(
21441 20 41 4c 57 41 59 53 28 7a 21 3d 30 29 20 29 7b ALWAYS(z!=0) ){
21442 0a 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 75 . double valu
21443 65 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 56 3b e;. char *zV;
21444 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 . sqlite3AtoF
21445 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 (z, &value);.
21446 20 61 73 73 65 72 74 28 20 21 73 71 6c 69 74 65 assert( !sqlite
21447 33 49 73 4e 61 4e 28 76 61 6c 75 65 29 20 29 3b 3IsNaN(value) );
21448 20 2f 2a 20 54 68 65 20 6e 65 77 20 41 74 6f 46 /* The new AtoF
21449 20 6e 65 76 65 72 20 72 65 74 75 72 6e 73 20 4e never returns N
2144a 61 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 65 aN */. if( ne
2144b 67 61 74 65 46 6c 61 67 20 29 20 76 61 6c 75 65 gateFlag ) value
2144c 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 20 20 7a = -value;. z
2144d 56 20 3d 20 64 75 70 38 62 79 74 65 73 28 76 2c V = dup8bytes(v,
2144e 20 28 63 68 61 72 2a 29 26 76 61 6c 75 65 29 3b (char*)&value);
2144f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
21450 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 52 65 61 AddOp4(v, OP_Rea
21451 6c 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a l, 0, iMem, 0, z
21452 56 2c 20 50 34 5f 52 45 41 4c 29 3b 0a 20 20 7d V, P4_REAL);. }
21453 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 .}.../*.** Gener
21454 61 74 65 20 61 6e 20 69 6e 73 74 72 75 63 74 69 ate an instructi
21455 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 70 75 74 on that will put
21456 20 74 68 65 20 69 6e 74 65 67 65 72 20 64 65 73 the integer des
21457 63 72 69 62 65 20 62 79 0a 2a 2a 20 74 65 78 74 cribe by.** text
21458 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e 74 6f 20 z[0..n-1] into
21459 72 65 67 69 73 74 65 72 20 69 4d 65 6d 2e 0a 2a register iMem..*
2145a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 73 74 72 *.** The z[] str
2145b 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62 61 62 6c ing will probabl
2145c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f 2d 74 65 y not be zero-te
2145d 72 6d 69 6e 61 74 65 64 2e 20 20 42 75 74 20 74 rminated. But t
2145e 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 68 61 72 he .** z[n] char
2145f 61 63 74 65 72 20 69 73 20 67 75 61 72 61 6e 74 acter is guarant
21460 65 65 64 20 74 6f 20 62 65 20 73 6f 6d 65 74 68 eed to be someth
21461 69 6e 67 20 74 68 61 74 20 64 6f 65 73 20 6e 6f ing that does no
21462 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b 65 20 74 t look.** like t
21463 68 65 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 he continuation
21464 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 2e 0a 2a of the number..*
21465 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f /.static void co
21466 64 65 49 6e 74 65 67 65 72 28 56 64 62 65 20 2a deInteger(Vdbe *
21467 76 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 v, Expr *pExpr,
21468 69 6e 74 20 6e 65 67 46 6c 61 67 2c 20 69 6e 74 int negFlag, int
21469 20 69 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 45 iMem){. if( pE
2146a 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f xpr->flags & EP_
2146b 49 6e 74 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 IntValue ){.
2146c 69 6e 74 20 69 20 3d 20 70 45 78 70 72 2d 3e 75 int i = pExpr->u
2146d 2e 69 56 61 6c 75 65 3b 0a 20 20 20 20 69 66 28 .iValue;. if(
2146e 20 6e 65 67 46 6c 61 67 20 29 20 69 20 3d 20 2d negFlag ) i = -
2146f 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 i;. sqlite3Vd
21470 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 beAddOp2(v, OP_I
21471 6e 74 65 67 65 72 2c 20 69 2c 20 69 4d 65 6d 29 nteger, i, iMem)
21472 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 ;. }else{. c
21473 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 onst char *z = p
21474 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a Expr->u.zToken;.
21475 20 20 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30 assert( z!=0
21476 20 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 );. if( sqli
21477 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 te3FitsIn64Bits(
21478 7a 2c 20 6e 65 67 46 6c 61 67 29 20 29 7b 0a 20 z, negFlag) ){.
21479 20 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a i64 value;.
2147a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 56 3b 0a char *zV;.
2147b 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f sqlite3Ato
2147c 69 36 34 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a i64(z, &value);.
2147d 20 20 20 20 20 20 69 66 28 20 6e 65 67 46 6c 61 if( negFla
2147e 67 20 29 20 76 61 6c 75 65 20 3d 20 2d 76 61 6c g ) value = -val
2147f 75 65 3b 0a 20 20 20 20 20 20 7a 56 20 3d 20 64 ue;. zV = d
21480 75 70 38 62 79 74 65 73 28 76 2c 20 28 63 68 61 up8bytes(v, (cha
21481 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 r*)&value);.
21482 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21483 4f 70 34 28 76 2c 20 4f 50 5f 49 6e 74 36 34 2c Op4(v, OP_Int64,
21484 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 56 2c 0, iMem, 0, zV,
21485 20 50 34 5f 49 4e 54 36 34 29 3b 0a 20 20 20 20 P4_INT64);.
21486 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 64 }else{. cod
21487 65 52 65 61 6c 28 76 2c 20 7a 2c 20 6e 65 67 46 eReal(v, z, negF
21488 6c 61 67 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 lag, iMem);.
21489 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 }. }.}../*.** C
2148a 6c 65 61 72 20 61 20 63 61 63 68 65 20 65 6e 74 lear a cache ent
2148b 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ry..*/.static vo
2148c 69 64 20 63 61 63 68 65 45 6e 74 72 79 43 6c 65 id cacheEntryCle
2148d 61 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ar(Parse *pParse
2148e 2c 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 , struct yColCac
2148f 68 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d he *p){. if( p-
21490 3e 74 65 6d 70 52 65 67 20 29 7b 0a 20 20 20 20 >tempReg ){.
21491 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d if( pParse->nTem
21492 70 52 65 67 3c 41 72 72 61 79 53 69 7a 65 28 70 pReg<ArraySize(p
21493 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 29 Parse->aTempReg)
21494 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 ){. pParse
21495 2d 3e 61 54 65 6d 70 52 65 67 5b 70 50 61 72 73 ->aTempReg[pPars
21496 65 2d 3e 6e 54 65 6d 70 52 65 67 2b 2b 5d 20 3d e->nTempReg++] =
21497 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 7d 0a p->iReg;. }.
21498 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d p->tempReg =
21499 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 0;. }.}.../*.*
2149a 2a 20 52 65 63 6f 72 64 20 69 6e 20 74 68 65 20 * Record in the
2149b 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 74 68 61 column cache tha
2149c 74 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 63 t a particular c
2149d 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61 0a 2a 2a 20 olumn from a.**
2149e 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 particular table
2149f 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 is stored in a
214a0 70 61 72 74 69 63 75 6c 61 72 20 72 65 67 69 73 particular regis
214a1 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ter..*/.SQLITE_P
214a2 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
214a3 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f 72 te3ExprCacheStor
214a4 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c e(Parse *pParse,
214a5 20 69 6e 74 20 69 54 61 62 2c 20 69 6e 74 20 69 int iTab, int i
214a6 43 6f 6c 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a Col, int iReg){.
214a7 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6d int i;. int m
214a8 69 6e 4c 72 75 3b 0a 20 20 69 6e 74 20 69 64 78 inLru;. int idx
214a9 4c 72 75 3b 0a 20 20 73 74 72 75 63 74 20 79 43 Lru;. struct yC
214aa 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 61 olCache *p;.. a
214ab 73 73 65 72 74 28 20 69 52 65 67 3e 30 20 29 3b ssert( iReg>0 );
214ac 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 6e 75 /* Register nu
214ad 6d 62 65 72 73 20 61 72 65 20 61 6c 77 61 79 73 mbers are always
214ae 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20 20 61 positive */. a
214af 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 2d 31 20 ssert( iCol>=-1
214b0 26 26 20 69 43 6f 6c 3c 33 32 37 36 38 20 29 3b && iCol<32768 );
214b1 20 20 2f 2a 20 46 69 6e 69 74 65 20 63 6f 6c 75 /* Finite colu
214b2 6d 6e 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 0a 20 mn numbers */..
214b3 20 2f 2a 20 46 69 72 73 74 20 72 65 70 6c 61 63 /* First replac
214b4 65 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 65 e any existing e
214b5 6e 74 72 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d ntry */. for(i=
214b6 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 0, p=pParse->aCo
214b7 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 lCache; i<SQLITE
214b8 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b _N_COLCACHE; i++
214b9 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 , p++){. if(
214ba 70 2d 3e 69 52 65 67 20 26 26 20 70 2d 3e 69 54 p->iReg && p->iT
214bb 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 70 2d able==iTab && p-
214bc 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 20 29 >iColumn==iCol )
214bd 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e 74 {. cacheEnt
214be 72 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20 ryClear(pParse,
214bf 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4c 65 p);. p->iLe
214c0 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 vel = pParse->iC
214c1 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 20 20 acheLevel;.
214c2 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b p->iReg = iReg;
214c3 0a 20 20 20 20 20 20 70 2d 3e 61 66 66 43 68 61 . p->affCha
214c4 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 nge = 0;. p
214c5 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e ->lru = pParse->
214c6 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 iCacheCnt++;.
214c7 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
214c8 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 . }.. /* Find
214c9 61 6e 20 65 6d 70 74 79 20 73 6c 6f 74 20 61 6e an empty slot an
214ca 64 20 72 65 70 6c 61 63 65 20 69 74 20 2a 2f 0a d replace it */.
214cb 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 for(i=0, p=pPa
214cc 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 rse->aColCache;
214cd 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 i<SQLITE_N_COLCA
214ce 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a CHE; i++, p++){.
214cf 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3d if( p->iReg=
214d0 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 =0 ){. p->i
214d1 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e Level = pParse->
214d2 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 iCacheLevel;.
214d3 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 69 p->iTable = i
214d4 54 61 62 3b 0a 20 20 20 20 20 20 70 2d 3e 69 43 Tab;. p->iC
214d5 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 olumn = iCol;.
214d6 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 p->iReg = iR
214d7 65 67 3b 0a 20 20 20 20 20 20 70 2d 3e 61 66 66 eg;. p->aff
214d8 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20 Change = 0;.
214d9 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20 30 p->tempReg = 0
214da 3b 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75 20 3d ;. p->lru =
214db 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 43 pParse->iCacheC
214dc 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 nt++;. retu
214dd 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 rn;. }. }..
214de 20 2f 2a 20 52 65 70 6c 61 63 65 20 74 68 65 20 /* Replace the
214df 6c 61 73 74 20 72 65 63 65 6e 74 6c 79 20 75 73 last recently us
214e0 65 64 20 2a 2f 0a 20 20 6d 69 6e 4c 72 75 20 3d ed */. minLru =
214e1 20 30 78 37 66 66 66 66 66 66 66 3b 0a 20 20 69 0x7fffffff;. i
214e2 64 78 4c 72 75 20 3d 20 2d 31 3b 0a 20 20 66 6f dxLru = -1;. fo
214e3 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d r(i=0, p=pParse-
214e4 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 >aColCache; i<SQ
214e5 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b LITE_N_COLCACHE;
214e6 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 i++, p++){.
214e7 69 66 28 20 70 2d 3e 6c 72 75 3c 6d 69 6e 4c 72 if( p->lru<minLr
214e8 75 20 29 7b 0a 20 20 20 20 20 20 69 64 78 4c 72 u ){. idxLr
214e9 75 20 3d 20 69 3b 0a 20 20 20 20 20 20 6d 69 6e u = i;. min
214ea 4c 72 75 20 3d 20 70 2d 3e 6c 72 75 3b 0a 20 20 Lru = p->lru;.
214eb 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 41 4c }. }. if( AL
214ec 57 41 59 53 28 69 64 78 4c 72 75 3e 3d 30 29 20 WAYS(idxLru>=0)
214ed 29 7b 0a 20 20 20 20 70 20 3d 20 26 70 50 61 72 ){. p = &pPar
214ee 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 64 se->aColCache[id
214ef 78 4c 72 75 5d 3b 0a 20 20 20 20 70 2d 3e 69 4c xLru];. p->iL
214f0 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 evel = pParse->i
214f1 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 20 CacheLevel;.
214f2 70 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 p->iTable = iTab
214f3 3b 0a 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e ;. p->iColumn
214f4 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 70 2d 3e = iCol;. p->
214f5 69 52 65 67 20 3d 20 69 52 65 67 3b 0a 20 20 20 iReg = iReg;.
214f6 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 p->affChange =
214f7 30 3b 0a 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 0;. p->tempRe
214f8 67 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 6c 72 g = 0;. p->lr
214f9 75 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 u = pParse->iCac
214fa 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 72 65 74 heCnt++;. ret
214fb 75 72 6e 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a urn;. }.}../*.*
214fc 2a 20 49 6e 64 69 63 61 74 65 20 74 68 61 74 20 * Indicate that
214fd 61 20 72 65 67 69 73 74 65 72 20 69 73 20 62 65 a register is be
214fe 69 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e ing overwritten.
214ff 20 20 50 75 72 67 65 20 74 68 65 20 72 65 67 69 Purge the regi
21500 73 74 65 72 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 ster.** from the
21501 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2e 0a 2a column cache..*
21502 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
21503 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
21504 72 43 61 63 68 65 52 65 6d 6f 76 65 28 50 61 72 rCacheRemove(Par
21505 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
21506 69 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a iReg){. int i;.
21507 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 struct yColCac
21508 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 he *p;. for(i=0
21509 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c , p=pParse->aCol
2150a 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f Cache; i<SQLITE_
2150b 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c N_COLCACHE; i++,
2150c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 p++){. if( p
2150d 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a ->iReg==iReg ){.
2150e 20 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 cacheEntry
2150f 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 Clear(pParse, p)
21510 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 ;. p->iReg
21511 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d = 0;. }. }.}
21512 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 ../*.** Remember
21513 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6c the current col
21514 75 6d 6e 20 63 61 63 68 65 20 63 6f 6e 74 65 78 umn cache contex
21515 74 2e 20 20 41 6e 79 20 6e 65 77 20 65 6e 74 72 t. Any new entr
21516 69 65 73 20 61 64 64 65 64 0a 2a 2a 20 61 64 64 ies added.** add
21517 65 64 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e ed to the column
21518 20 63 61 63 68 65 20 61 66 74 65 72 20 74 68 69 cache after thi
21519 73 20 63 61 6c 6c 20 61 72 65 20 72 65 6d 6f 76 s call are remov
2151a 65 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 63 ed when the.** c
2151b 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 70 6f 70 orresponding pop
2151c 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 occurs..*/.SQLI
2151d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
2151e 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
2151f 50 75 73 68 28 50 61 72 73 65 20 2a 70 50 61 72 Push(Parse *pPar
21520 73 65 29 7b 0a 20 20 70 50 61 72 73 65 2d 3e 69 se){. pParse->i
21521 43 61 63 68 65 4c 65 76 65 6c 2b 2b 3b 0a 7d 0a CacheLevel++;.}.
21522 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 66 72 ./*.** Remove fr
21523 6f 6d 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 om the column ca
21524 63 68 65 20 61 6e 79 20 65 6e 74 72 69 65 73 20 che any entries
21525 74 68 61 74 20 77 65 72 65 20 61 64 64 65 64 20 that were added
21526 73 69 6e 63 65 20 74 68 65 0a 2a 2a 20 74 68 65 since the.** the
21527 20 70 72 65 76 69 6f 75 73 20 4e 20 50 75 73 68 previous N Push
21528 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 49 6e operations. In
21529 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 72 65 other words, re
2152a 73 74 6f 72 65 20 74 68 65 20 63 61 63 68 65 0a store the cache.
2152b 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 74 65 20 ** to the state
2152c 69 74 20 77 61 73 20 69 6e 20 4e 20 50 75 73 68 it was in N Push
2152d 65 73 20 61 67 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 es ago..*/.SQLIT
2152e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
2152f 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 qlite3ExprCacheP
21530 6f 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 op(Parse *pParse
21531 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 69 6e 74 20 , int N){. int
21532 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c i;. struct yCol
21533 43 61 63 68 65 20 2a 70 3b 0a 20 20 61 73 73 65 Cache *p;. asse
21534 72 74 28 20 4e 3e 30 20 29 3b 0a 20 20 61 73 73 rt( N>0 );. ass
21535 65 72 74 28 20 70 50 61 72 73 65 2d 3e 69 43 61 ert( pParse->iCa
21536 63 68 65 4c 65 76 65 6c 3e 3d 4e 20 29 3b 0a 20 cheLevel>=N );.
21537 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c pParse->iCacheL
21538 65 76 65 6c 20 2d 3d 20 4e 3b 0a 20 20 66 6f 72 evel -= N;. for
21539 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e (i=0, p=pParse->
2153a 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c aColCache; i<SQL
2153b 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 ITE_N_COLCACHE;
2153c 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 i++, p++){. i
2153d 66 28 20 70 2d 3e 69 52 65 67 20 26 26 20 70 2d f( p->iReg && p-
2153e 3e 69 4c 65 76 65 6c 3e 70 50 61 72 73 65 2d 3e >iLevel>pParse->
2153f 69 43 61 63 68 65 4c 65 76 65 6c 20 29 7b 0a 20 iCacheLevel ){.
21540 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 43 cacheEntryC
21541 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b lear(pParse, p);
21542 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d . p->iReg =
21543 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0;. }. }.}.
21544 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 63 61 ./*.** When a ca
21545 63 68 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20 72 ched column is r
21546 65 75 73 65 64 2c 20 6d 61 6b 65 20 73 75 72 65 eused, make sure
21547 20 74 68 61 74 20 69 74 73 20 72 65 67 69 73 74 that its regist
21548 65 72 20 69 73 0a 2a 2a 20 6e 6f 20 6c 6f 6e 67 er is.** no long
21549 65 72 20 61 76 61 69 6c 61 62 6c 65 20 61 73 20 er available as
2154a 61 20 74 65 6d 70 20 72 65 67 69 73 74 65 72 2e a temp register.
2154b 20 20 74 69 63 6b 65 74 20 23 33 38 37 39 3a 20 ticket #3879:
2154c 20 74 68 61 74 20 73 61 6d 65 0a 2a 2a 20 72 65 that same.** re
2154d 67 69 73 74 65 72 20 6d 69 67 68 74 20 62 65 20 gister might be
2154e 69 6e 20 74 68 65 20 63 61 63 68 65 20 69 6e 20 in the cache in
2154f 6d 75 6c 74 69 70 6c 65 20 70 6c 61 63 65 73 2c multiple places,
21550 20 73 6f 20 62 65 20 73 75 72 65 20 74 6f 0a 2a so be sure to.*
21551 2a 20 67 65 74 20 74 68 65 6d 20 61 6c 6c 2e 0a * get them all..
21552 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 */.static void s
21553 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 qlite3ExprCacheP
21554 69 6e 52 65 67 69 73 74 65 72 28 50 61 72 73 65 inRegister(Parse
21555 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 *pParse, int iR
21556 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 eg){. int i;.
21557 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 struct yColCache
21558 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 *p;. for(i=0,
21559 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 p=pParse->aColCa
2155a 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f che; i<SQLITE_N_
2155b 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 COLCACHE; i++, p
2155c 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e ++){. if( p->
2155d 69 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a 20 20 iReg==iReg ){.
2155e 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d p->tempReg =
2155f 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0;. }. }.}.
21560 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
21561 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 code that will e
21562 78 74 72 61 63 74 20 74 68 65 20 69 43 6f 6c 75 xtract the iColu
21563 6d 6e 2d 74 68 20 63 6f 6c 75 6d 6e 20 66 72 6f mn-th column fro
21564 6d 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20 m.** table pTab
21565 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 63 6f and store the co
21566 6c 75 6d 6e 20 76 61 6c 75 65 20 69 6e 20 61 20 lumn value in a
21567 72 65 67 69 73 74 65 72 2e 20 20 41 6e 20 65 66 register. An ef
21568 66 6f 72 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 fort.** is made
21569 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6c to store the col
2156a 75 6d 6e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 umn value in reg
2156b 69 73 74 65 72 20 69 52 65 67 2c 20 62 75 74 20 ister iReg, but
2156c 74 68 69 73 20 69 73 0a 2a 2a 20 6e 6f 74 20 67 this is.** not g
2156d 75 61 72 61 6e 74 65 65 64 2e 20 20 54 68 65 20 uaranteed. The
2156e 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 location of the
2156f 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 73 20 column value is
21570 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
21571 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e There must be an
21572 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 74 6f 20 open cursor to
21573 70 54 61 62 20 69 6e 20 69 54 61 62 6c 65 20 77 pTab in iTable w
21574 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
21575 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 .** is called.
21576 49 66 20 69 43 6f 6c 75 6d 6e 3c 30 20 74 68 65 If iColumn<0 the
21577 6e 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61 n code is genera
21578 74 65 64 20 74 68 61 74 20 65 78 74 72 61 63 74 ted that extract
21579 73 20 74 68 65 20 72 6f 77 69 64 2e 0a 2a 2a 0a s the rowid..**.
2157a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
2157b 6d 69 67 68 74 20 61 74 74 65 6d 70 74 20 74 6f might attempt to
2157c 20 72 65 75 73 65 20 74 68 65 20 76 61 6c 75 65 reuse the value
2157d 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 of the column t
2157e 68 61 74 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 hat.** has alrea
2157f 64 79 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 69 dy been loaded i
21580 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72 2e 20 nto a register.
21581 20 54 68 65 20 76 61 6c 75 65 20 77 69 6c 6c 20 The value will
21582 61 6c 77 61 79 73 0a 2a 2a 20 62 65 20 75 73 65 always.** be use
21583 64 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 d if it has not
21584 75 6e 64 65 72 67 6f 6e 65 20 61 6e 79 20 61 66 undergone any af
21585 66 69 6e 69 74 79 20 63 68 61 6e 67 65 73 2e 20 finity changes.
21586 20 42 75 74 20 69 66 0a 2a 2a 20 61 6e 20 61 66 But if.** an af
21587 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61 finity change ha
21588 73 20 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e s occurred, then
21589 20 74 68 65 20 63 61 63 68 65 64 20 76 61 6c 75 the cached valu
2158a 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 0a 2a e will only be.*
2158b 2a 20 75 73 65 64 20 69 66 20 61 6c 6c 6f 77 41 * used if allowA
2158c 66 66 43 68 6e 67 20 69 73 20 74 72 75 65 2e 0a ffChng is true..
2158d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
2158e 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
2158f 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 0a rCodeGetColumn(.
21590 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
21591 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e /* Parsing an
21592 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e d code generatin
21593 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 g context */. T
21594 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 able *pTab,
21595 2f 2a 20 44 65 73 63 72 69 70 74 69 6f 6e 20 6f /* Description o
21596 66 20 74 68 65 20 74 61 62 6c 65 20 77 65 20 61 f the table we a
21597 72 65 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 re reading from
21598 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e */. int iColumn
21599 2c 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f , /* Index o
2159a 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 f the table colu
2159b 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 mn */. int iTab
2159c 6c 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 le, /* The
2159d 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 cursor pointing
2159e 74 6f 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a to the table */.
2159f 20 20 69 6e 74 20 69 52 65 67 2c 20 20 20 20 20 int iReg,
215a0 20 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 /* Store resu
215a1 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e lts here */. in
215a2 74 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 20 2f t allowAffChng /
215a3 2a 20 54 72 75 65 20 69 66 20 70 72 69 6f 72 20 * True if prior
215a4 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 73 affinity changes
215a5 20 61 72 65 20 4f 4b 20 2a 2f 0a 29 7b 0a 20 20 are OK */.){.
215a6 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 Vdbe *v = pParse
215a7 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 ->pVdbe;. int i
215a8 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 ;. struct yColC
215a9 61 63 68 65 20 2a 70 3b 0a 0a 20 20 66 6f 72 28 ache *p;.. for(
215aa 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 i=0, p=pParse->a
215ab 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 ColCache; i<SQLI
215ac 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 TE_N_COLCACHE; i
215ad 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 ++, p++){. if
215ae 28 20 70 2d 3e 69 52 65 67 3e 30 20 26 26 20 70 ( p->iReg>0 && p
215af 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 ->iTable==iTable
215b0 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d && p->iColumn==
215b1 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20 20 iColumn.
215b2 20 20 20 26 26 20 28 21 70 2d 3e 61 66 66 43 68 && (!p->affCh
215b3 61 6e 67 65 20 7c 7c 20 61 6c 6c 6f 77 41 66 66 ange || allowAff
215b4 43 68 6e 67 29 20 29 7b 0a 20 20 20 20 20 20 70 Chng) ){. p
215b5 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e ->lru = pParse->
215b6 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 iCacheCnt++;.
215b7 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
215b8 63 68 65 50 69 6e 52 65 67 69 73 74 65 72 28 70 chePinRegister(p
215b9 50 61 72 73 65 2c 20 70 2d 3e 69 52 65 67 29 3b Parse, p->iReg);
215ba 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d . return p-
215bb 3e 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20 20 7d >iReg;. }. }
215bc 20 20 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d . assert( v!=
215bd 30 20 29 3b 0a 20 20 69 66 28 20 69 43 6f 6c 75 0 );. if( iColu
215be 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 mn<0 ){. sqli
215bf 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
215c0 20 4f 50 5f 52 6f 77 69 64 2c 20 69 54 61 62 6c OP_Rowid, iTabl
215c1 65 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65 6c 73 e, iReg);. }els
215c2 65 20 69 66 28 20 41 4c 57 41 59 53 28 70 54 61 e if( ALWAYS(pTa
215c3 62 21 3d 30 29 20 29 7b 0a 20 20 20 20 69 6e 74 b!=0) ){. int
215c4 20 6f 70 20 3d 20 49 73 56 69 72 74 75 61 6c 28 op = IsVirtual(
215c5 70 54 61 62 29 20 3f 20 4f 50 5f 56 43 6f 6c 75 pTab) ? OP_VColu
215c6 6d 6e 20 3a 20 4f 50 5f 43 6f 6c 75 6d 6e 3b 0a mn : OP_Column;.
215c7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
215c8 64 64 4f 70 33 28 76 2c 20 6f 70 2c 20 69 54 61 ddOp3(v, op, iTa
215c9 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 ble, iColumn, iR
215ca 65 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 eg);. sqlite3
215cb 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c ColumnDefault(v,
215cc 20 70 54 61 62 2c 20 69 43 6f 6c 75 6d 6e 2c 20 pTab, iColumn,
215cd 69 52 65 67 29 3b 0a 20 20 7d 0a 20 20 73 71 6c iReg);. }. sql
215ce 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f ite3ExprCacheSto
215cf 72 65 28 70 50 61 72 73 65 2c 20 69 54 61 62 6c re(pParse, iTabl
215d0 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 e, iColumn, iReg
215d1 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 52 65 67 );. return iReg
215d2 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 ;.}../*.** Clear
215d3 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20 63 61 63 68 all column cach
215d4 65 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 53 51 e entries..*/.SQ
215d5 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
215d6 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 d sqlite3ExprCac
215d7 68 65 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70 heClear(Parse *p
215d8 50 61 72 73 65 29 7b 0a 20 20 69 6e 74 20 69 3b Parse){. int i;
215d9 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 . struct yColCa
215da 63 68 65 20 2a 70 3b 0a 0a 20 20 66 6f 72 28 69 che *p;.. for(i
215db 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 =0, p=pParse->aC
215dc 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 olCache; i<SQLIT
215dd 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b E_N_COLCACHE; i+
215de 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 +, p++){. if(
215df 20 70 2d 3e 69 52 65 67 20 29 7b 0a 20 20 20 20 p->iReg ){.
215e0 20 20 63 61 63 68 65 45 6e 74 72 79 43 6c 65 61 cacheEntryClea
215e1 72 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 r(pParse, p);.
215e2 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 30 3b p->iReg = 0;
215e3 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
215e4 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20 66 .** Record the f
215e5 61 63 74 20 74 68 61 74 20 61 6e 20 61 66 66 69 act that an affi
215e6 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61 73 20 nity change has
215e7 6f 63 63 75 72 72 65 64 20 6f 6e 20 69 43 6f 75 occurred on iCou
215e8 6e 74 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 nt.** registers
215e9 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 69 53 starting with iS
215ea 74 61 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tart..*/.SQLITE_
215eb 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
215ec 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 ite3ExprCacheAff
215ed 69 6e 69 74 79 43 68 61 6e 67 65 28 50 61 72 73 inityChange(Pars
215ee 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 e *pParse, int i
215ef 53 74 61 72 74 2c 20 69 6e 74 20 69 43 6f 75 6e Start, int iCoun
215f0 74 29 7b 0a 20 20 69 6e 74 20 69 45 6e 64 20 3d t){. int iEnd =
215f1 20 69 53 74 61 72 74 20 2b 20 69 43 6f 75 6e 74 iStart + iCount
215f2 20 2d 20 31 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 - 1;. int i;.
215f3 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 struct yColCach
215f4 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c e *p;. for(i=0,
215f5 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 p=pParse->aColC
215f6 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e ache; i<SQLITE_N
215f7 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 _COLCACHE; i++,
215f8 70 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 20 p++){. int r
215f9 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 69 = p->iReg;. i
215fa 66 28 20 72 3e 3d 69 53 74 61 72 74 20 26 26 20 f( r>=iStart &&
215fb 72 3c 3d 69 45 6e 64 20 29 7b 0a 20 20 20 20 20 r<=iEnd ){.
215fc 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 p->affChange =
215fd 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 1;. }. }.}..
215fe 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 /*.** Generate c
215ff 6f 64 65 20 74 6f 20 6d 6f 76 65 20 63 6f 6e 74 ode to move cont
21600 65 6e 74 20 66 72 6f 6d 20 72 65 67 69 73 74 65 ent from registe
21601 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 46 72 6f 6d rs iFrom...iFrom
21602 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f 76 65 72 20 +nReg-1.** over
21603 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e 52 65 67 to iTo..iTo+nReg
21604 2d 31 2e 20 4b 65 65 70 20 74 68 65 20 63 6f 6c -1. Keep the col
21605 75 6d 6e 20 63 61 63 68 65 20 75 70 2d 74 6f 2d umn cache up-to-
21606 64 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f date..*/.SQLITE_
21607 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
21608 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 ite3ExprCodeMove
21609 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
2160a 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69 int iFrom, int i
2160b 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 To, int nReg){.
2160c 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 int i;. struct
2160d 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 yColCache *p;.
2160e 20 69 66 28 20 4e 45 56 45 52 28 69 46 72 6f 6d if( NEVER(iFrom
2160f 3d 3d 69 54 6f 29 20 29 20 72 65 74 75 72 6e 3b ==iTo) ) return;
21610 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 . sqlite3VdbeAd
21611 64 4f 70 33 28 70 50 61 72 73 65 2d 3e 70 56 64 dOp3(pParse->pVd
21612 62 65 2c 20 4f 50 5f 4d 6f 76 65 2c 20 69 46 72 be, OP_Move, iFr
21613 6f 6d 2c 20 69 54 6f 2c 20 6e 52 65 67 29 3b 0a om, iTo, nReg);.
21614 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 for(i=0, p=pPa
21615 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 rse->aColCache;
21616 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 i<SQLITE_N_COLCA
21617 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a CHE; i++, p++){.
21618 20 20 20 20 69 6e 74 20 78 20 3d 20 70 2d 3e 69 int x = p->i
21619 52 65 67 3b 0a 20 20 20 20 69 66 28 20 78 3e 3d Reg;. if( x>=
2161a 69 46 72 6f 6d 20 26 26 20 78 3c 69 46 72 6f 6d iFrom && x<iFrom
2161b 2b 6e 52 65 67 20 29 7b 0a 20 20 20 20 20 20 70 +nReg ){. p
2161c 2d 3e 69 52 65 67 20 2b 3d 20 69 54 6f 2d 69 46 ->iReg += iTo-iF
2161d 72 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d rom;. }. }.}
2161e 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
2161f 20 63 6f 64 65 20 74 6f 20 63 6f 70 79 20 63 6f code to copy co
21620 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 67 69 73 ntent from regis
21621 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 46 72 ters iFrom...iFr
21622 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f 76 65 om+nReg-1.** ove
21623 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e 52 r to iTo..iTo+nR
21624 65 67 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f eg-1..*/.SQLITE_
21625 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
21626 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70 79 ite3ExprCodeCopy
21627 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
21628 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69 int iFrom, int i
21629 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 To, int nReg){.
2162a 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 4e 45 int i;. if( NE
2162b 56 45 52 28 69 46 72 6f 6d 3d 3d 69 54 6f 29 20 VER(iFrom==iTo)
2162c 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 ) return;. for(
2162d 69 3d 30 3b 20 69 3c 6e 52 65 67 3b 20 69 2b 2b i=0; i<nReg; i++
2162e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
2162f 62 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d beAddOp2(pParse-
21630 3e 70 56 64 62 65 2c 20 4f 50 5f 43 6f 70 79 2c >pVdbe, OP_Copy,
21631 20 69 46 72 6f 6d 2b 69 2c 20 69 54 6f 2b 69 29 iFrom+i, iTo+i)
21632 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ;. }.}../*.** R
21633 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 61 6e eturn true if an
21634 79 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 y register in th
21635 65 20 72 61 6e 67 65 20 69 46 72 6f 6d 2e 2e 69 e range iFrom..i
21636 54 6f 20 28 69 6e 63 6c 75 73 69 76 65 29 0a 2a To (inclusive).*
21637 2a 20 69 73 20 75 73 65 64 20 61 73 20 70 61 72 * is used as par
21638 74 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 t of the column
21639 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 cache..*/.static
2163a 20 69 6e 74 20 75 73 65 64 41 73 43 6f 6c 75 6d int usedAsColum
2163b 6e 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 nCache(Parse *pP
2163c 61 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c arse, int iFrom,
2163d 20 69 6e 74 20 69 54 6f 29 7b 0a 20 20 69 6e 74 int iTo){. int
2163e 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f i;. struct yCo
2163f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 lCache *p;. for
21640 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e (i=0, p=pParse->
21641 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c aColCache; i<SQL
21642 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 ITE_N_COLCACHE;
21643 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 i++, p++){. i
21644 6e 74 20 72 20 3d 20 70 2d 3e 69 52 65 67 3b 0a nt r = p->iReg;.
21645 20 20 20 20 69 66 28 20 72 3e 3d 69 46 72 6f 6d if( r>=iFrom
21646 20 26 26 20 72 3c 3d 69 54 6f 20 29 20 72 65 74 && r<=iTo ) ret
21647 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 urn 1;. }. ret
21648 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
21649 49 66 20 74 68 65 20 6c 61 73 74 20 69 6e 73 74 If the last inst
2164a 72 75 63 74 69 6f 6e 20 63 6f 64 65 64 20 69 73 ruction coded is
2164b 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 63 6f an ephemeral co
2164c 70 79 20 6f 66 20 61 6e 79 20 6f 66 0a 2a 2a 20 py of any of.**
2164d 74 68 65 20 72 65 67 69 73 74 65 72 73 20 69 6e the registers in
2164e 20 74 68 65 20 6e 52 65 67 20 72 65 67 69 73 74 the nReg regist
2164f 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 ers beginning wi
21650 74 68 20 69 52 65 67 2c 20 74 68 65 6e 0a 2a 2a th iReg, then.**
21651 20 63 6f 6e 76 65 72 74 20 74 68 65 20 6c 61 73 convert the las
21652 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 72 t instruction fr
21653 6f 6d 20 4f 50 5f 53 43 6f 70 79 20 74 6f 20 4f om OP_SCopy to O
21654 50 5f 43 6f 70 79 2e 0a 2a 2f 0a 53 51 4c 49 54 P_Copy..*/.SQLIT
21655 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
21656 71 6c 69 74 65 33 45 78 70 72 48 61 72 64 43 6f qlite3ExprHardCo
21657 70 79 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 py(Parse *pParse
21658 2c 20 69 6e 74 20 69 52 65 67 2c 20 69 6e 74 20 , int iReg, int
21659 6e 52 65 67 29 7b 0a 20 20 56 64 62 65 4f 70 20 nReg){. VdbeOp
2165a 2a 70 4f 70 3b 0a 20 20 56 64 62 65 20 2a 76 3b *pOp;. Vdbe *v;
2165b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 .. assert( pPar
2165c 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 se->db->mallocFa
2165d 69 6c 65 64 3d 3d 30 20 29 3b 0a 20 20 76 20 3d iled==0 );. v =
2165e 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a pParse->pVdbe;.
2165f 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 assert( v!=0 )
21660 3b 0a 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65 ;. pOp = sqlite
21661 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 2d 31 3VdbeGetOp(v, -1
21662 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
21663 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 !=0 );. if( pOp
21664 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 43 6f ->opcode==OP_SCo
21665 70 79 20 26 26 20 70 4f 70 2d 3e 70 31 3e 3d 69 py && pOp->p1>=i
21666 52 65 67 20 26 26 20 70 4f 70 2d 3e 70 31 3c 69 Reg && pOp->p1<i
21667 52 65 67 2b 6e 52 65 67 20 29 7b 0a 20 20 20 20 Reg+nReg ){.
21668 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 pOp->opcode = OP
21669 5f 43 6f 70 79 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a _Copy;. }.}../*
2166a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 .** Generate cod
2166b 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 76 e to store the v
2166c 61 6c 75 65 20 6f 66 20 74 68 65 20 69 41 6c 69 alue of the iAli
2166d 61 73 2d 74 68 20 61 6c 69 61 73 20 69 6e 20 72 as-th alias in r
2166e 65 67 69 73 74 65 72 0a 2a 2a 20 74 61 72 67 65 egister.** targe
2166f 74 2e 20 20 54 68 65 20 66 69 72 73 74 20 74 69 t. The first ti
21670 6d 65 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 me this is calle
21671 64 2c 20 70 45 78 70 72 20 69 73 20 65 76 61 6c d, pExpr is eval
21672 75 61 74 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 uated to compute
21673 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 .** the value of
21674 20 74 68 65 20 61 6c 69 61 73 2e 20 20 54 68 65 the alias. The
21675 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 value is stored
21676 20 69 6e 20 61 6e 20 61 75 78 69 6c 69 61 72 79 in an auxiliary
21677 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 61 6e 64 register.** and
21678 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 the number of t
21679 68 61 74 20 72 65 67 69 73 74 65 72 20 69 73 20 hat register is
2167a 72 65 74 75 72 6e 65 64 2e 20 20 4f 6e 20 73 75 returned. On su
2167b 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 2c 0a bsequent calls,.
2167c 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 20 ** the register
2167d 6e 75 6d 62 65 72 20 69 73 20 72 65 74 75 72 6e number is return
2167e 65 64 20 77 69 74 68 6f 75 74 20 67 65 6e 65 72 ed without gener
2167f 61 74 69 6e 67 20 61 6e 79 20 63 6f 64 65 2e 0a ating any code..
21680 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 **.** Note that
21681 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 74 68 69 in order for thi
21682 73 20 74 6f 20 77 6f 72 6b 2c 20 63 6f 64 65 20 s to work, code
21683 6d 75 73 74 20 62 65 20 67 65 6e 65 72 61 74 65 must be generate
21684 64 20 69 6e 20 74 68 65 0a 2a 2a 20 73 61 6d 65 d in the.** same
21685 20 6f 72 64 65 72 20 74 68 61 74 20 69 74 20 69 order that it i
21686 73 20 65 78 65 63 75 74 65 64 2e 0a 2a 2a 0a 2a s executed..**.*
21687 2a 20 41 6c 69 61 73 65 73 20 61 72 65 20 6e 75 * Aliases are nu
21688 6d 62 65 72 65 64 20 73 74 61 72 74 69 6e 67 20 mbered starting
21689 77 69 74 68 20 31 2e 20 20 53 6f 20 69 41 6c 69 with 1. So iAli
2168a 61 73 20 69 73 20 69 6e 20 74 68 65 20 72 61 6e as is in the ran
2168b 67 65 0a 2a 2a 20 6f 66 20 31 20 74 6f 20 70 50 ge.** of 1 to pP
2168c 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 69 6e 63 arse->nAlias inc
2168d 6c 75 73 69 76 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 lusive. .**.**
2168e 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 69 pParse->aAlias[i
2168f 41 6c 69 61 73 2d 31 5d 20 72 65 63 6f 72 64 73 Alias-1] records
21690 20 74 68 65 20 72 65 67 69 73 74 65 72 20 6e 75 the register nu
21691 6d 62 65 72 20 77 68 65 72 65 20 74 68 65 20 76 mber where the v
21692 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 69 alue.** of the i
21693 41 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 20 69 Alias-th alias i
21694 73 20 73 74 6f 72 65 64 2e 20 20 49 66 20 7a 65 s stored. If ze
21695 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 ro, that means t
21696 68 61 74 20 74 68 65 0a 2a 2a 20 61 6c 69 61 73 hat the.** alias
21697 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 has not yet bee
21698 6e 20 63 6f 6d 70 75 74 65 64 2e 0a 2a 2f 0a 73 n computed..*/.s
21699 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 41 6c tatic int codeAl
2169a 69 61 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 ias(Parse *pPars
2169b 65 2c 20 69 6e 74 20 69 41 6c 69 61 73 2c 20 45 e, int iAlias, E
2169c 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 xpr *pExpr, int
2169d 74 61 72 67 65 74 29 7b 0a 23 69 66 20 30 0a 20 target){.#if 0.
2169e 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
2169f 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 Parse->db;. int
216a0 20 69 52 65 67 3b 0a 20 20 69 66 28 20 70 50 61 iReg;. if( pPa
216a1 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 rse->nAliasAlloc
216a2 3c 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 <pParse->nAlias
216a3 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61 ){. pParse->a
216a4 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44 Alias = sqlite3D
216a5 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 64 bReallocOrFree(d
216a6 62 2c 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 b, pParse->aAlia
216a7 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 s,.
216a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
216a9 20 20 20 20 73 69 7a 65 6f 66 28 70 50 61 72 73 sizeof(pPars
216aa 65 2d 3e 61 41 6c 69 61 73 5b 30 5d 29 2a 70 50 e->aAlias[0])*pP
216ab 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a arse->nAlias );.
216ac 20 20 20 20 74 65 73 74 63 61 73 65 28 20 64 62 testcase( db
216ad 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 ->mallocFailed &
216ae 26 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 & pParse->nAlias
216af 41 6c 6c 6f 63 3e 30 20 29 3b 0a 20 20 20 20 69 Alloc>0 );. i
216b0 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
216b1 6c 65 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0a led ) return 0;.
216b2 20 20 20 20 6d 65 6d 73 65 74 28 26 70 50 61 72 memset(&pPar
216b3 73 65 2d 3e 61 41 6c 69 61 73 5b 70 50 61 72 73 se->aAlias[pPars
216b4 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 5d 2c e->nAliasAlloc],
216b5 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 0,. (
216b6 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 2d 70 pParse->nAlias-p
216b7 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c Parse->nAliasAll
216b8 6f 63 29 2a 73 69 7a 65 6f 66 28 70 50 61 72 73 oc)*sizeof(pPars
216b9 65 2d 3e 61 41 6c 69 61 73 5b 30 5d 29 29 3b 0a e->aAlias[0]));.
216ba 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 pParse->nAli
216bb 61 73 41 6c 6c 6f 63 20 3d 20 70 50 61 72 73 65 asAlloc = pParse
216bc 2d 3e 6e 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 ->nAlias;. }.
216bd 61 73 73 65 72 74 28 20 69 41 6c 69 61 73 3e 30 assert( iAlias>0
216be 20 26 26 20 69 41 6c 69 61 73 3c 3d 70 50 61 72 && iAlias<=pPar
216bf 73 65 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 20 se->nAlias );.
216c0 69 52 65 67 20 3d 20 70 50 61 72 73 65 2d 3e 61 iReg = pParse->a
216c1 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d 3b Alias[iAlias-1];
216c2 0a 20 20 69 66 28 20 69 52 65 67 3d 3d 30 20 29 . if( iReg==0 )
216c3 7b 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65 {. if( pParse
216c4 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3e 30 20 ->iCacheLevel>0
216c5 29 7b 0a 20 20 20 20 20 20 69 52 65 67 20 3d 20 ){. iReg =
216c6 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
216c7 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 arget(pParse, pE
216c8 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 xpr, target);.
216c9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 }else{. i
216ca 52 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e Reg = ++pParse->
216cb 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 nMem;. sqli
216cc 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 te3ExprCode(pPar
216cd 73 65 2c 20 70 45 78 70 72 2c 20 69 52 65 67 29 se, pExpr, iReg)
216ce 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e ;. pParse->
216cf 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d aAlias[iAlias-1]
216d0 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20 = iReg;. }.
216d1 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 52 65 67 }. return iReg
216d2 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 ;.#else. UNUSED
216d3 5f 50 41 52 41 4d 45 54 45 52 28 69 41 6c 69 61 _PARAMETER(iAlia
216d4 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c s);. return sql
216d5 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 ite3ExprCodeTarg
216d6 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 et(pParse, pExpr
216d7 2c 20 74 61 72 67 65 74 29 3b 0a 23 65 6e 64 69 , target);.#endi
216d8 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 f.}../*.** Gener
216d9 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 ate code into th
216da 65 20 63 75 72 72 65 6e 74 20 56 64 62 65 20 74 e current Vdbe t
216db 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 67 o evaluate the g
216dc 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 iven.** expressi
216dd 6f 6e 2e 20 20 41 74 74 65 6d 70 74 20 74 6f 20 on. Attempt to
216de 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 store the result
216df 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 22 74 s in register "t
216e0 61 72 67 65 74 22 2e 0a 2a 2a 20 52 65 74 75 72 arget"..** Retur
216e1 6e 20 74 68 65 20 72 65 67 69 73 74 65 72 20 77 n the register w
216e2 68 65 72 65 20 72 65 73 75 6c 74 73 20 61 72 65 here results are
216e3 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 stored..**.** W
216e4 69 74 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ith this routine
216e5 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 67 75 , there is no gu
216e6 61 72 61 6e 74 65 65 20 74 68 61 74 20 72 65 73 arantee that res
216e7 75 6c 74 73 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 ults will.** be
216e8 73 74 6f 72 65 64 20 69 6e 20 74 61 72 67 65 74 stored in target
216e9 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 6d 69 . The result mi
216ea 67 68 74 20 62 65 20 73 74 6f 72 65 64 20 69 6e ght be stored in
216eb 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 72 some other.** r
216ec 65 67 69 73 74 65 72 20 69 66 20 69 74 20 69 73 egister if it is
216ed 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 64 convenient to d
216ee 6f 20 73 6f 2e 20 20 54 68 65 20 63 61 6c 6c 69 o so. The calli
216ef 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d ng function.** m
216f0 75 73 74 20 63 68 65 63 6b 20 74 68 65 20 72 65 ust check the re
216f1 74 75 72 6e 20 63 6f 64 65 20 61 6e 64 20 6d 6f turn code and mo
216f2 76 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 74 ve the results t
216f3 6f 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a 2a o the desired.**
216f4 20 72 65 67 69 73 74 65 72 2e 0a 2a 2f 0a 53 51 register..*/.SQ
216f5 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
216f6 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
216f7 54 61 72 67 65 74 28 50 61 72 73 65 20 2a 70 50 Target(Parse *pP
216f8 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 arse, Expr *pExp
216f9 72 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a r, int target){.
216fa 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 Vdbe *v = pPar
216fb 73 65 2d 3e 70 56 64 62 65 3b 20 20 2f 2a 20 54 se->pVdbe; /* T
216fc 68 65 20 56 4d 20 75 6e 64 65 72 20 63 6f 6e 73 he VM under cons
216fd 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e truction */. in
216fe 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 t op;
216ff 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f /* The o
21700 70 63 6f 64 65 20 62 65 69 6e 67 20 63 6f 64 65 pcode being code
21701 64 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 52 65 67 d */. int inReg
21702 20 3d 20 74 61 72 67 65 74 3b 20 20 20 20 20 20 = target;
21703 20 2f 2a 20 52 65 73 75 6c 74 73 20 73 74 6f 72 /* Results stor
21704 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 ed in register i
21705 6e 52 65 67 20 2a 2f 0a 20 20 69 6e 74 20 72 65 nReg */. int re
21706 67 46 72 65 65 31 20 3d 20 30 3b 20 20 20 20 20 gFree1 = 0;
21707 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 /* If non-ze
21708 72 6f 20 66 72 65 65 20 74 68 69 73 20 74 65 6d ro free this tem
21709 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 20 porary register
2170a 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 */. int regFree
2170b 32 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2 = 0; /
2170c 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72 * If non-zero fr
2170d 65 65 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 ee this temporar
2170e 79 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 y register */.
2170f 69 6e 74 20 72 31 2c 20 72 32 2c 20 72 33 2c 20 int r1, r2, r3,
21710 72 34 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 72 r4; /* Var
21711 69 6f 75 73 20 72 65 67 69 73 74 65 72 20 6e 75 ious register nu
21712 6d 62 65 72 73 20 2a 2f 0a 20 20 73 71 6c 69 74 mbers */. sqlit
21713 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
21714 3e 64 62 3b 20 2f 2a 20 54 68 65 20 64 61 74 61 >db; /* The data
21715 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
21716 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 74 61 */.. assert( ta
21717 72 67 65 74 3e 30 20 26 26 20 74 61 72 67 65 74 rget>0 && target
21718 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 <=pParse->nMem )
21719 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a ;. if( v==0 ){.
2171a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 assert( pPar
2171b 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 se->db->mallocFa
2171c 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 74 75 iled );. retu
2171d 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 rn 0;. }.. if(
2171e 20 70 45 78 70 72 3d 3d 30 20 29 7b 0a 20 20 20 pExpr==0 ){.
2171f 20 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 op = TK_NULL;.
21720 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 70 20 3d }else{. op =
21721 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 7d 0a pExpr->op;. }.
21722 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a switch( op ){.
21723 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f case TK_AGG_
21724 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 COLUMN: {.
21725 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 AggInfo *pAggInf
21726 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 67 49 o = pExpr->pAggI
21727 6e 66 6f 3b 0a 20 20 20 20 20 20 73 74 72 75 63 nfo;. struc
21728 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 t AggInfo_col *p
21729 43 6f 6c 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d Col = &pAggInfo-
2172a 3e 61 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 41 67 >aCol[pExpr->iAg
2172b 67 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 g];. if( !p
2172c 41 67 67 49 6e 66 6f 2d 3e 64 69 72 65 63 74 4d AggInfo->directM
2172d 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 20 20 61 ode ){. a
2172e 73 73 65 72 74 28 20 70 43 6f 6c 2d 3e 69 4d 65 ssert( pCol->iMe
2172f 6d 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 m>0 );. i
21730 6e 52 65 67 20 3d 20 70 43 6f 6c 2d 3e 69 4d 65 nReg = pCol->iMe
21731 6d 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b m;. break
21732 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
21733 28 20 70 41 67 67 49 6e 66 6f 2d 3e 75 73 65 53 ( pAggInfo->useS
21734 6f 72 74 69 6e 67 49 64 78 20 29 7b 0a 20 20 20 ortingIdx ){.
21735 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21736 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c AddOp3(v, OP_Col
21737 75 6d 6e 2c 20 70 41 67 67 49 6e 66 6f 2d 3e 73 umn, pAggInfo->s
21738 6f 72 74 69 6e 67 49 64 78 2c 0a 20 20 20 20 20 ortingIdx,.
21739 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2173a 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 pCol->i
2173b 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 2c 20 74 61 SorterColumn, ta
2173c 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 62 rget);. b
2173d 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
2173e 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 /* Otherwise
2173f 2c 20 66 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f , fall thru into
21740 20 74 68 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 63 the TK_COLUMN c
21741 61 73 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 ase */. }.
21742 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a case TK_COLUMN:
21743 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 {. if( pEx
21744 70 72 2d 3e 69 54 61 62 6c 65 3c 30 20 29 7b 0a pr->iTable<0 ){.
21745 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 /* This
21746 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65 only happens whe
21747 6e 20 63 6f 64 69 6e 67 20 63 68 65 63 6b 20 63 n coding check c
21748 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 onstraints */.
21749 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
2174a 61 72 73 65 2d 3e 63 6b 42 61 73 65 3e 30 20 29 arse->ckBase>0 )
2174b 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 ;. inReg
2174c 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e = pExpr->iColumn
2174d 20 2b 20 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 + pParse->ckBas
2174e 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a e;. }else{.
2174f 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
21750 28 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 ( (pExpr->flags
21751 26 20 45 50 5f 41 6e 79 41 66 66 29 21 3d 30 20 & EP_AnyAff)!=0
21752 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 );. inReg
21753 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
21754 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72 deGetColumn(pPar
21755 73 65 2c 20 70 45 78 70 72 2d 3e 70 54 61 62 2c se, pExpr->pTab,
21756 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
21757 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21758 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e pExpr->iColumn
21759 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c , pExpr->iTable,
2175a 20 74 61 72 67 65 74 2c 0a 20 20 20 20 20 20 20 target,.
2175b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2175c 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d pExpr-
2175d 3e 66 6c 61 67 73 20 26 20 45 50 5f 41 6e 79 41 >flags & EP_AnyA
2175e 66 66 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ff);. }.
2175f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
21760 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 case TK_INTE
21761 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 63 6f 64 GER: {. cod
21762 65 49 6e 74 65 67 65 72 28 76 2c 20 70 45 78 70 eInteger(v, pExp
21763 72 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 r, 0, target);.
21764 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
21765 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 4c }. case TK_FL
21766 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 OAT: {. ass
21767 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f ert( !ExprHasPro
21768 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
21769 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 IntValue) );.
2176a 20 20 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 70 codeReal(v, p
2176b 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 Expr->u.zToken,
2176c 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 0, target);.
2176d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
2176e 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49 4e case TK_STRIN
2176f 47 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 G: {. asser
21770 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 t( !ExprHasPrope
21771 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e rty(pExpr, EP_In
21772 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 tValue) );.
21773 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21774 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 p4(v, OP_String8
21775 2c 20 30 2c 20 74 61 72 67 65 74 2c 20 30 2c 20 , 0, target, 0,
21776 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c pExpr->u.zToken,
21777 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 0);. break
21778 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
21779 20 54 4b 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 TK_NULL: {.
2177a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
2177b 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 Op2(v, OP_Null,
2177c 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 0, target);.
2177d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 break;. }.#
2177e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
2177f 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a IT_BLOB_LITERAL.
21780 20 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 case TK_BLOB
21781 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b : {. int n;
21782 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 . const cha
21783 72 20 2a 7a 3b 0a 20 20 20 20 20 20 63 68 61 72 r *z;. char
21784 20 2a 7a 42 6c 6f 62 3b 0a 20 20 20 20 20 20 61 *zBlob;. a
21785 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 ssert( !ExprHasP
21786 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
21787 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 P_IntValue) );.
21788 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 assert( pEx
21789 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d pr->u.zToken[0]=
2178a 3d 27 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 75 ='x' || pExpr->u
2178b 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 58 27 20 .zToken[0]=='X'
2178c 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
2178d 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e pExpr->u.zToken
2178e 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 [1]=='\'' );.
2178f 20 20 20 7a 20 3d 20 26 70 45 78 70 72 2d 3e 75 z = &pExpr->u
21790 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b 0a 20 20 20 20 .zToken[2];.
21791 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 n = sqlite3Str
21792 6c 65 6e 33 30 28 7a 29 20 2d 20 31 3b 0a 20 20 len30(z) - 1;.
21793 20 20 20 20 61 73 73 65 72 74 28 20 7a 5b 6e 5d assert( z[n]
21794 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 20 20 =='\'' );.
21795 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 48 zBlob = sqlite3H
21796 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 exToBlob(sqlite3
21797 56 64 62 65 44 62 28 76 29 2c 20 7a 2c 20 6e 29 VdbeDb(v), z, n)
21798 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
21799 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
2179a 42 6c 6f 62 2c 20 6e 2f 32 2c 20 74 61 72 67 65 Blob, n/2, targe
2179b 74 2c 20 30 2c 20 7a 42 6c 6f 62 2c 20 50 34 5f t, 0, zBlob, P4_
2179c 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 DYNAMIC);.
2179d 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e break;. }.#en
2179e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54 4b 5f dif. case TK_
2179f 56 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20 20 20 VARIABLE: {.
217a0 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 VdbeOp *pOp;.
217a1 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 assert( !Ex
217a2 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 prHasProperty(pE
217a3 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 xpr, EP_IntValue
217a4 29 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 ) );. asser
217a5 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b t( pExpr->u.zTok
217a6 65 6e 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 en!=0 );. a
217a7 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e ssert( pExpr->u.
217a8 7a 54 6f 6b 65 6e 5b 30 5d 21 3d 30 20 29 3b 0a zToken[0]!=0 );.
217a9 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d if( pExpr-
217aa 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 30 0a >u.zToken[1]==0.
217ab 20 20 20 20 20 20 20 20 20 26 26 20 28 70 4f 70 && (pOp
217ac 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 47 65 = sqlite3VdbeGe
217ad 74 4f 70 28 76 2c 20 2d 31 29 29 2d 3e 6f 70 63 tOp(v, -1))->opc
217ae 6f 64 65 3d 3d 4f 50 5f 56 61 72 69 61 62 6c 65 ode==OP_Variable
217af 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 4f 70 . && pOp
217b0 2d 3e 70 31 2b 70 4f 70 2d 3e 70 33 3d 3d 70 45 ->p1+pOp->p3==pE
217b1 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 0a 20 20 20 xpr->iColumn.
217b2 20 20 20 20 20 20 26 26 20 70 4f 70 2d 3e 70 32 && pOp->p2
217b3 2b 70 4f 70 2d 3e 70 33 3d 3d 74 61 72 67 65 74 +pOp->p3==target
217b4 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 4f 70 . && pOp
217b5 2d 3e 70 34 2e 7a 3d 3d 30 0a 20 20 20 20 20 20 ->p4.z==0.
217b6 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 ){. /* If
217b7 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 69 6e the previous in
217b8 73 74 72 75 63 74 69 6f 6e 20 77 61 73 20 61 20 struction was a
217b9 63 6f 70 79 20 6f 66 20 74 68 65 20 70 72 65 76 copy of the prev
217ba 69 6f 75 73 20 75 6e 6e 61 6d 65 64 0a 20 20 20 ious unnamed.
217bb 20 20 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 ** paramete
217bc 72 20 69 6e 74 6f 20 74 68 65 20 70 72 65 76 69 r into the previ
217bd 6f 75 73 20 72 65 67 69 73 74 65 72 2c 20 74 68 ous register, th
217be 65 6e 20 73 69 6d 70 6c 79 20 69 6e 63 72 65 6d en simply increm
217bf 65 6e 74 20 74 68 65 0a 20 20 20 20 20 20 20 20 ent the.
217c0 2a 2a 20 72 65 70 65 61 74 20 63 6f 75 6e 74 20 ** repeat count
217c1 6f 6e 20 74 68 65 20 70 72 69 6f 72 20 69 6e 73 on the prior ins
217c2 74 72 75 63 74 69 6f 6e 20 72 61 74 68 65 72 20 truction rather
217c3 74 68 61 6e 20 6d 61 6b 69 6e 67 20 61 20 6e 65 than making a ne
217c4 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 73 w. ** ins
217c5 74 72 75 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 truction..
217c6 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4f 70 */. pOp
217c7 2d 3e 70 33 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 ->p3++;. }e
217c8 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c lse{. sql
217c9 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
217ca 2c 20 4f 50 5f 56 61 72 69 61 62 6c 65 2c 20 70 , OP_Variable, p
217cb 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 74 Expr->iColumn, t
217cc 61 72 67 65 74 2c 20 31 29 3b 0a 20 20 20 20 20 arget, 1);.
217cd 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 75 2e if( pExpr->u.
217ce 7a 54 6f 6b 65 6e 5b 31 5d 21 3d 30 20 29 7b 0a zToken[1]!=0 ){.
217cf 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
217d0 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 3VdbeChangeP4(v,
217d1 20 2d 31 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 -1, pExpr->u.zT
217d2 6f 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 oken, 0);.
217d3 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
217d4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
217d5 20 20 20 63 61 73 65 20 54 4b 5f 52 45 47 49 53 case TK_REGIS
217d6 54 45 52 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 TER: {. inR
217d7 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 eg = pExpr->iTab
217d8 6c 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b le;. break;
217d9 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
217da 54 4b 5f 41 53 3a 20 7b 0a 20 20 20 20 20 20 69 TK_AS: {. i
217db 6e 52 65 67 20 3d 20 63 6f 64 65 41 6c 69 61 73 nReg = codeAlias
217dc 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
217dd 69 54 61 62 6c 65 2c 20 70 45 78 70 72 2d 3e 70 iTable, pExpr->p
217de 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 Left, target);.
217df 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
217e0 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
217e1 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 20 20 63 _OMIT_CAST. c
217e2 61 73 65 20 54 4b 5f 43 41 53 54 3a 20 7b 0a 20 ase TK_CAST: {.
217e3 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 /* Expressi
217e4 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a ons of the form:
217e5 20 20 20 43 41 53 54 28 70 4c 65 66 74 20 41 53 CAST(pLeft AS
217e6 20 74 6f 6b 65 6e 29 20 2a 2f 0a 20 20 20 20 20 token) */.
217e7 20 69 6e 74 20 61 66 66 2c 20 74 6f 5f 6f 70 3b int aff, to_op;
217e8 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73 . inReg = s
217e9 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 qlite3ExprCodeTa
217ea 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 rget(pParse, pEx
217eb 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 pr->pLeft, targe
217ec 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 t);. assert
217ed 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
217ee 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 ty(pExpr, EP_Int
217ef 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 Value) );.
217f0 61 66 66 20 3d 20 73 71 6c 69 74 65 33 41 66 66 aff = sqlite3Aff
217f1 69 6e 69 74 79 54 79 70 65 28 70 45 78 70 72 2d inityType(pExpr-
217f2 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 >u.zToken);.
217f3 20 20 74 6f 5f 6f 70 20 3d 20 61 66 66 20 2d 20 to_op = aff -
217f4 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 SQLITE_AFF_TEXT
217f5 2b 20 4f 50 5f 54 6f 54 65 78 74 3b 0a 20 20 20 + OP_ToText;.
217f6 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 assert( to_op
217f7 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 7c ==OP_ToText |
217f8 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 | aff!=SQLITE_AF
217f9 46 5f 54 45 58 54 20 20 20 20 29 3b 0a 20 20 20 F_TEXT );.
217fa 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 assert( to_op
217fb 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 7c ==OP_ToBlob |
217fc 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 | aff!=SQLITE_AF
217fd 46 5f 4e 4f 4e 45 20 20 20 20 29 3b 0a 20 20 20 F_NONE );.
217fe 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 assert( to_op
217ff 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20 7c ==OP_ToNumeric |
21800 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 | aff!=SQLITE_AF
21801 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 20 F_NUMERIC );.
21802 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 assert( to_op
21803 3d 3d 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 7c ==OP_ToInt |
21804 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 | aff!=SQLITE_AF
21805 46 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 20 20 F_INTEGER );.
21806 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 assert( to_op
21807 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 7c ==OP_ToReal |
21808 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 | aff!=SQLITE_AF
21809 46 5f 52 45 41 4c 20 20 20 20 29 3b 0a 20 20 20 F_REAL );.
2180a 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f testcase( to_
2180b 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 29 3b op==OP_ToText );
2180c 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
2180d 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f to_op==OP_ToBlo
2180e 62 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 b );. testc
2180f 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 ase( to_op==OP_T
21810 6f 4e 75 6d 65 72 69 63 20 29 3b 0a 20 20 20 20 oNumeric );.
21811 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f testcase( to_o
21812 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 29 3b 0a 20 p==OP_ToInt );.
21813 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74 testcase( t
21814 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 o_op==OP_ToReal
21815 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 6e 52 );. if( inR
21816 65 67 21 3d 74 61 72 67 65 74 20 29 7b 0a 20 20 eg!=target ){.
21817 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21818 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 eAddOp2(v, OP_SC
21819 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 72 67 opy, inReg, targ
2181a 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 et);. inR
2181b 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 20 20 eg = target;.
2181c 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
2181d 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 e3VdbeAddOp1(v,
2181e 74 6f 5f 6f 70 2c 20 69 6e 52 65 67 29 3b 0a 20 to_op, inReg);.
2181f 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 75 testcase( u
21820 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65 sedAsColumnCache
21821 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c 20 (pParse, inReg,
21822 69 6e 52 65 67 29 20 29 3b 0a 20 20 20 20 20 20 inReg) );.
21823 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
21824 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 AffinityChange(p
21825 50 61 72 73 65 2c 20 69 6e 52 65 67 2c 20 31 29 Parse, inReg, 1)
21826 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
21827 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 }.#endif /* S
21828 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 QLITE_OMIT_CAST
21829 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c */. case TK_L
2182a 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c T:. case TK_L
2182b 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 E:. case TK_G
2182c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 T:. case TK_G
2182d 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e E:. case TK_N
2182e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 E:. case TK_E
2182f 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 Q: {. asser
21830 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 20 t( TK_LT==OP_Lt
21831 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
21832 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29 3b TK_LE==OP_Le );
21833 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
21834 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 K_GT==OP_Gt );.
21835 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
21836 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 20 GE==OP_Ge );.
21837 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 45 51 assert( TK_EQ
21838 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20 20 20 ==OP_Eq );.
21839 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 45 3d 3d assert( TK_NE==
2183a 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 74 OP_Ne );. t
2183b 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
2183c 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 LT );. test
2183d 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20 case( op==TK_LE
2183e 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
2183f 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a e( op==TK_GT );.
21840 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21841 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 20 op==TK_GE );.
21842 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
21843 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 =TK_EQ );.
21844 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
21845 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 _NE );. cod
21846 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 eCompareOperands
21847 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
21848 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 67 pLeft, &r1, ®
21849 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20 20 Free1,.
2184a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2184b 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e pExpr->
2184c 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72 65 pRight, &r2, &re
2184d 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 63 gFree2);. c
2184e 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 odeCompare(pPars
2184f 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c e, pExpr->pLeft,
21850 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 pExpr->pRight,
21851 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 op,.
21852 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 69 6e r1, r2, in
21853 52 65 67 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 Reg, SQLITE_STOR
21854 45 50 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74 EP2);. test
21855 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d case( regFree1==
21856 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 0 );. testc
21857 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 ase( regFree2==0
21858 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b );. break;
21859 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
2185a 54 4b 5f 49 53 3a 0a 20 20 20 20 63 61 73 65 20 TK_IS:. case
2185b 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20 20 20 20 TK_ISNOT: {.
2185c 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
2185d 54 4b 5f 49 53 20 29 3b 0a 20 20 20 20 20 20 74 TK_IS );. t
2185e 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
2185f 49 53 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 63 ISNOT );. c
21860 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e odeCompareOperan
21861 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 ds(pParse, pExpr
21862 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 ->pLeft, &r1, &r
21863 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 egFree1,.
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 20 20 20 70 45 78 70 72 pExpr
21866 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 ->pRight, &r2, &
21867 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 regFree2);.
21868 20 6f 70 20 3d 20 28 6f 70 3d 3d 54 4b 5f 49 53 op = (op==TK_IS
21869 29 20 3f 20 54 4b 5f 45 51 20 3a 20 54 4b 5f 4e ) ? TK_EQ : TK_N
2186a 45 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d E;. codeCom
2186b 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 pare(pParse, pEx
2186c 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 pr->pLeft, pExpr
2186d 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 ->pRight, op,.
2186e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2186f 72 31 2c 20 72 32 2c 20 69 6e 52 65 67 2c 20 53 r1, r2, inReg, S
21870 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 7c 20 QLITE_STOREP2 |
21871 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 29 3b 0a SQLITE_NULLEQ);.
21872 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21873 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 regFree1==0 );.
21874 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
21875 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 egFree2==0 );.
21876 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
21877 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44 . case TK_AND
21878 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 :. case TK_OR
21879 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 50 4c :. case TK_PL
2187a 55 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f US:. case TK_
2187b 53 54 41 52 3a 0a 20 20 20 20 63 61 73 65 20 54 STAR:. case T
2187c 4b 5f 4d 49 4e 55 53 3a 0a 20 20 20 20 63 61 73 K_MINUS:. cas
2187d 65 20 54 4b 5f 52 45 4d 3a 0a 20 20 20 20 63 61 e TK_REM:. ca
2187e 73 65 20 54 4b 5f 42 49 54 41 4e 44 3a 0a 20 20 se TK_BITAND:.
2187f 20 20 63 61 73 65 20 54 4b 5f 42 49 54 4f 52 3a case TK_BITOR:
21880 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 4c 41 . case TK_SLA
21881 53 48 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f SH:. case TK_
21882 4c 53 48 49 46 54 3a 0a 20 20 20 20 63 61 73 65 LSHIFT:. case
21883 20 54 4b 5f 52 53 48 49 46 54 3a 20 0a 20 20 20 TK_RSHIFT: .
21884 20 63 61 73 65 20 54 4b 5f 43 4f 4e 43 41 54 3a case TK_CONCAT:
21885 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 {. assert(
21886 20 54 4b 5f 41 4e 44 3d 3d 4f 50 5f 41 6e 64 20 TK_AND==OP_And
21887 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
21888 20 54 4b 5f 4f 52 3d 3d 4f 50 5f 4f 72 20 29 3b TK_OR==OP_Or );
21889 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
2188a 4b 5f 50 4c 55 53 3d 3d 4f 50 5f 41 64 64 20 29 K_PLUS==OP_Add )
2188b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
2188c 54 4b 5f 4d 49 4e 55 53 3d 3d 4f 50 5f 53 75 62 TK_MINUS==OP_Sub
2188d 74 72 61 63 74 20 29 3b 0a 20 20 20 20 20 20 61 tract );. a
2188e 73 73 65 72 74 28 20 54 4b 5f 52 45 4d 3d 3d 4f ssert( TK_REM==O
2188f 50 5f 52 65 6d 61 69 6e 64 65 72 20 29 3b 0a 20 P_Remainder );.
21890 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
21891 42 49 54 41 4e 44 3d 3d 4f 50 5f 42 69 74 41 6e BITAND==OP_BitAn
21892 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 d );. asser
21893 74 28 20 54 4b 5f 42 49 54 4f 52 3d 3d 4f 50 5f t( TK_BITOR==OP_
21894 42 69 74 4f 72 20 29 3b 0a 20 20 20 20 20 20 61 BitOr );. a
21895 73 73 65 72 74 28 20 54 4b 5f 53 4c 41 53 48 3d ssert( TK_SLASH=
21896 3d 4f 50 5f 44 69 76 69 64 65 20 29 3b 0a 20 20 =OP_Divide );.
21897 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c assert( TK_L
21898 53 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66 74 4c SHIFT==OP_ShiftL
21899 65 66 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 eft );. ass
2189a 65 72 74 28 20 54 4b 5f 52 53 48 49 46 54 3d 3d ert( TK_RSHIFT==
2189b 4f 50 5f 53 68 69 66 74 52 69 67 68 74 20 29 3b OP_ShiftRight );
2189c 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
2189d 4b 5f 43 4f 4e 43 41 54 3d 3d 4f 50 5f 43 6f 6e K_CONCAT==OP_Con
2189e 63 61 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 cat );. tes
2189f 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 41 4e tcase( op==TK_AN
218a0 44 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 D );. testc
218a1 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4f 52 20 29 ase( op==TK_OR )
218a2 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
218a3 28 20 6f 70 3d 3d 54 4b 5f 50 4c 55 53 20 29 3b ( op==TK_PLUS );
218a4 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
218a5 20 6f 70 3d 3d 54 4b 5f 4d 49 4e 55 53 20 29 3b op==TK_MINUS );
218a6 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
218a7 20 6f 70 3d 3d 54 4b 5f 52 45 4d 20 29 3b 0a 20 op==TK_REM );.
218a8 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
218a9 70 3d 3d 54 4b 5f 42 49 54 41 4e 44 20 29 3b 0a p==TK_BITAND );.
218aa 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
218ab 6f 70 3d 3d 54 4b 5f 42 49 54 4f 52 20 29 3b 0a op==TK_BITOR );.
218ac 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
218ad 6f 70 3d 3d 54 4b 5f 53 4c 41 53 48 20 29 3b 0a op==TK_SLASH );.
218ae 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
218af 6f 70 3d 3d 54 4b 5f 4c 53 48 49 46 54 20 29 3b op==TK_LSHIFT );
218b0 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
218b1 20 6f 70 3d 3d 54 4b 5f 52 53 48 49 46 54 20 29 op==TK_RSHIFT )
218b2 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
218b3 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 43 41 54 20 ( op==TK_CONCAT
218b4 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 );. r1 = sq
218b5 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d lite3ExprCodeTem
218b6 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d p(pParse, pExpr-
218b7 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 >pLeft, ®Free
218b8 31 29 3b 0a 20 20 20 20 20 20 72 32 20 3d 20 73 1);. r2 = s
218b9 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 qlite3ExprCodeTe
218ba 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 mp(pParse, pExpr
218bb 2d 3e 70 52 69 67 68 74 2c 20 26 72 65 67 46 72 ->pRight, ®Fr
218bc 65 65 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 ee2);. sqli
218bd 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
218be 20 6f 70 2c 20 72 32 2c 20 72 31 2c 20 74 61 72 op, r2, r1, tar
218bf 67 65 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74 get);. test
218c0 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d case( regFree1==
218c1 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 0 );. testc
218c2 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 ase( regFree2==0
218c3 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b );. break;
218c4 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
218c5 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 TK_UMINUS: {.
218c6 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d Expr *pLeft =
218c7 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 pExpr->pLeft;.
218c8 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 assert( pLe
218c9 66 74 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ft );. if(
218ca 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c pLeft->op==TK_FL
218cb 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 20 20 61 OAT ){. a
218cc 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 ssert( !ExprHasP
218cd 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
218ce 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 P_IntValue) );.
218cf 20 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28 codeReal(
218d0 76 2c 20 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b v, pLeft->u.zTok
218d1 65 6e 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a en, 1, target);.
218d2 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
218d3 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e pLeft->op==TK_IN
218d4 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20 20 20 TEGER ){.
218d5 20 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20 codeInteger(v,
218d6 70 4c 65 66 74 2c 20 31 2c 20 74 61 72 67 65 74 pLeft, 1, target
218d7 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
218d8 20 20 20 20 20 20 20 20 72 65 67 46 72 65 65 31 regFree1
218d9 20 3d 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 = r1 = sqlite3G
218da 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 etTempReg(pParse
218db 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
218dc 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
218dd 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 OP_Integer, 0, r
218de 31 29 3b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 1);. r2 =
218df 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
218e0 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 Temp(pParse, pEx
218e1 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 pr->pLeft, ®F
218e2 72 65 65 32 29 3b 0a 20 20 20 20 20 20 20 20 73 ree2);. s
218e3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
218e4 28 76 2c 20 4f 50 5f 53 75 62 74 72 61 63 74 2c (v, OP_Subtract,
218e5 20 72 32 2c 20 72 31 2c 20 74 61 72 67 65 74 29 r2, r1, target)
218e6 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 ;. testca
218e7 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 se( regFree2==0
218e8 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
218e9 20 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b inReg = target;
218ea 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
218eb 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
218ec 42 49 54 4e 4f 54 3a 0a 20 20 20 20 63 61 73 65 BITNOT:. case
218ed 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 TK_NOT: {.
218ee 20 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 4e assert( TK_BITN
218ef 4f 54 3d 3d 4f 50 5f 42 69 74 4e 6f 74 20 29 3b OT==OP_BitNot );
218f0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
218f1 4b 5f 4e 4f 54 3d 3d 4f 50 5f 4e 6f 74 20 29 3b K_NOT==OP_Not );
218f2 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
218f3 20 6f 70 3d 3d 54 4b 5f 42 49 54 4e 4f 54 20 29 op==TK_BITNOT )
218f4 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
218f5 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 20 29 3b 0a ( op==TK_NOT );.
218f6 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 r1 = sqlit
218f7 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 e3ExprCodeTemp(p
218f8 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c Parse, pExpr->pL
218f9 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b eft, ®Free1);
218fa 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
218fb 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a regFree1==0 );.
218fc 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61 inReg = ta
218fd 72 67 65 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 rget;. sqli
218fe 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
218ff 20 6f 70 2c 20 72 31 2c 20 69 6e 52 65 67 29 3b op, r1, inReg);
21900 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
21901 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
21902 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 ISNULL:. case
21903 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 TK_NOTNULL: {.
21904 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 int addr;.
21905 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
21906 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c ISNULL==OP_IsNul
21907 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 l );. asser
21908 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f t( TK_NOTNULL==O
21909 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 20 P_NotNull );.
2190a 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
2190b 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 =TK_ISNULL );.
2190c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
2190d 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a ==TK_NOTNULL );.
2190e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
2190f 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e eAddOp2(v, OP_In
21910 74 65 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 teger, 1, target
21911 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 );. r1 = sq
21912 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d lite3ExprCodeTem
21913 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d p(pParse, pExpr-
21914 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 >pLeft, ®Free
21915 31 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 1);. testca
21916 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 se( regFree1==0
21917 29 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 );. addr =
21918 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21919 31 28 76 2c 20 6f 70 2c 20 72 31 29 3b 0a 20 20 1(v, op, r1);.
2191a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
2191b 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 ddOp2(v, OP_AddI
2191c 6d 6d 2c 20 74 61 72 67 65 74 2c 20 2d 31 29 3b mm, target, -1);
2191d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
2191e 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 beJumpHere(v, ad
2191f 64 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b dr);. break
21920 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
21921 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e TK_AGG_FUNCTION
21922 3a 20 7b 0a 20 20 20 20 20 20 41 67 67 49 6e 66 : {. AggInf
21923 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 45 78 70 72 o *pInfo = pExpr
21924 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 ->pAggInfo;.
21925 20 20 69 66 28 20 70 49 6e 66 6f 3d 3d 30 20 29 if( pInfo==0 )
21926 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
21927 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
21928 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 ty(pExpr, EP_Int
21929 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 Value) );.
2192a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
2192b 67 28 70 50 61 72 73 65 2c 20 22 6d 69 73 75 73 g(pParse, "misus
2192c 65 20 6f 66 20 61 67 67 72 65 67 61 74 65 3a 20 e of aggregate:
2192d 25 73 28 29 22 2c 20 70 45 78 70 72 2d 3e 75 2e %s()", pExpr->u.
2192e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d zToken);. }
2192f 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e else{. in
21930 52 65 67 20 3d 20 70 49 6e 66 6f 2d 3e 61 46 75 Reg = pInfo->aFu
21931 6e 63 5b 70 45 78 70 72 2d 3e 69 41 67 67 5d 2e nc[pExpr->iAgg].
21932 69 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 iMem;. }.
21933 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
21934 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e . case TK_CON
21935 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20 63 61 73 ST_FUNC:. cas
21936 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b e TK_FUNCTION: {
21937 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 . ExprList
21938 2a 70 46 61 72 67 3b 20 20 20 20 20 20 20 2f 2a *pFarg; /*
21939 20 4c 69 73 74 20 6f 66 20 66 75 6e 63 74 69 6f List of functio
2193a 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 n arguments */.
2193b 20 20 20 20 20 69 6e 74 20 6e 46 61 72 67 3b 20 int nFarg;
2193c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
2193d 75 6d 62 65 72 20 6f 66 20 66 75 6e 63 74 69 6f umber of functio
2193e 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 n arguments */.
2193f 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 FuncDef *pD
21940 65 66 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ef; /* T
21941 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 he function defi
21942 6e 69 74 69 6f 6e 20 6f 62 6a 65 63 74 20 2a 2f nition object */
21943 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20 . int nId;
21944 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
21945 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 66 Length of the f
21946 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 69 6e 20 unction name in
21947 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 20 20 63 bytes */. c
21948 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 3b 20 onst char *zId;
21949 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e /* The fun
2194a 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 ction name */.
2194b 20 20 20 20 69 6e 74 20 63 6f 6e 73 74 4d 61 73 int constMas
2194c 6b 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4d 61 k = 0; /* Ma
2194d 73 6b 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 61 sk of function a
2194e 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 61 72 rguments that ar
2194f 65 20 63 6f 6e 73 74 61 6e 74 20 2a 2f 0a 20 20 e constant */.
21950 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 int i;
21951 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
21952 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
21953 20 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 u8 enc = ENC
21954 28 64 62 29 3b 20 20 20 20 20 20 2f 2a 20 54 68 (db); /* Th
21955 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 e text encoding
21956 75 73 65 64 20 62 79 20 74 68 69 73 20 64 61 74 used by this dat
21957 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 43 abase */. C
21958 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 ollSeq *pColl =
21959 30 3b 20 20 20 20 2f 2a 20 41 20 63 6f 6c 6c 61 0; /* A colla
2195a 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f ting sequence */
2195b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 .. assert(
2195c 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 !ExprHasProperty
2195d 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 (pExpr, EP_xIsSe
2195e 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 74 lect) );. t
2195f 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
21960 43 4f 4e 53 54 5f 46 55 4e 43 20 29 3b 0a 20 20 CONST_FUNC );.
21961 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
21962 3d 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 29 3b ==TK_FUNCTION );
21963 0a 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 . if( ExprH
21964 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 asAnyProperty(pE
21965 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c xpr, EP_TokenOnl
21966 79 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 y) ){. pF
21967 61 72 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d arg = 0;. }
21968 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 46 else{. pF
21969 61 72 67 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 arg = pExpr->x.p
2196a 4c 69 73 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 List;. }.
2196b 20 20 20 20 6e 46 61 72 67 20 3d 20 70 46 61 72 nFarg = pFar
2196c 67 20 3f 20 70 46 61 72 67 2d 3e 6e 45 78 70 72 g ? pFarg->nExpr
2196d 20 3a 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 : 0;. asse
2196e 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 rt( !ExprHasProp
2196f 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 erty(pExpr, EP_I
21970 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 ntValue) );.
21971 20 20 7a 49 64 20 3d 20 70 45 78 70 72 2d 3e 75 zId = pExpr->u
21972 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 6e .zToken;. n
21973 49 64 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c Id = sqlite3Strl
21974 65 6e 33 30 28 7a 49 64 29 3b 0a 20 20 20 20 20 en30(zId);.
21975 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 pDef = sqlite3F
21976 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 indFunction(db,
21977 7a 49 64 2c 20 6e 49 64 2c 20 6e 46 61 72 67 2c zId, nId, nFarg,
21978 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 enc, 0);.
21979 69 66 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a 20 if( pDef==0 ){.
2197a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 sqlite3Er
2197b 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
2197c 75 6e 6b 6e 6f 77 6e 20 66 75 6e 63 74 69 6f 6e unknown function
2197d 3a 20 25 2e 2a 73 28 29 22 2c 20 6e 49 64 2c 20 : %.*s()", nId,
2197e 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 20 62 72 zId);. br
2197f 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
21980 20 20 20 69 66 28 20 70 46 61 72 67 20 29 7b 0a if( pFarg ){.
21981 20 20 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c r1 = sql
21982 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 ite3GetTempRange
21983 28 70 50 61 72 73 65 2c 20 6e 46 61 72 67 29 3b (pParse, nFarg);
21984 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
21985 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50 ExprCachePush(pP
21986 61 72 73 65 29 3b 20 20 20 20 20 2f 2a 20 54 69 arse); /* Ti
21987 63 6b 65 74 20 32 65 61 32 34 32 35 64 33 34 62 cket 2ea2425d34b
21988 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c e */. sql
21989 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 ite3ExprCodeExpr
2198a 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 46 61 List(pParse, pFa
2198b 72 67 2c 20 72 31 2c 20 31 29 3b 0a 20 20 20 20 rg, r1, 1);.
2198c 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
2198d 61 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 achePop(pParse,
2198e 31 29 3b 20 20 20 2f 2a 20 54 69 63 6b 65 74 20 1); /* Ticket
2198f 32 65 61 32 34 32 35 64 33 34 62 65 20 2a 2f 0a 2ea2425d34be */.
21990 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
21991 20 20 20 20 20 72 31 20 3d 20 30 3b 0a 20 20 20 r1 = 0;.
21992 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c }.#ifndef SQL
21993 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
21994 54 41 42 4c 45 0a 20 20 20 20 20 20 2f 2a 20 50 TABLE. /* P
21995 6f 73 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64 ossibly overload
21996 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 66 the function if
21997 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
21998 65 6e 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 ent is. **
21999 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 a virtual table
2199a 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2a column.. **
2199b 0a 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 69 6e . ** For in
2199c 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 4c fix functions (L
2199d 49 4b 45 2c 20 47 4c 4f 42 2c 20 52 45 47 45 58 IKE, GLOB, REGEX
2199e 50 2c 20 61 6e 64 20 4d 41 54 43 48 29 20 75 73 P, and MATCH) us
2199f 65 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 73 e the. ** s
219a0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 econd argument,
219a1 6e 6f 74 20 74 68 65 20 66 69 72 73 74 2c 20 61 not the first, a
219a2 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 s the argument t
219a3 6f 20 74 65 73 74 20 74 6f 0a 20 20 20 20 20 20 o test to.
219a4 2a 2a 20 73 65 65 20 69 66 20 69 74 20 69 73 20 ** see if it is
219a5 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69 a column in a vi
219a6 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 rtual table. Th
219a7 69 73 20 69 73 20 64 6f 6e 65 20 62 65 63 61 75 is is done becau
219a8 73 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 se. ** the
219a9 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 left operand of
219aa 69 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 infix functions
219ab 28 74 68 65 20 6f 70 65 72 61 6e 64 20 77 65 20 (the operand we
219ac 77 61 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a want to. **
219ad 20 63 6f 6e 74 72 6f 6c 20 6f 76 65 72 6c 6f 61 control overloa
219ae 64 69 6e 67 29 20 65 6e 64 73 20 75 70 20 61 73 ding) ends up as
219af 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
219b0 6d 65 6e 74 20 74 6f 20 74 68 65 0a 20 20 20 20 ment to the.
219b1 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 20 ** function.
219b2 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 22 The expression "
219b3 41 20 67 6c 6f 62 20 42 22 20 69 73 20 65 71 75 A glob B" is equ
219b4 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 ivalent to .
219b5 20 20 2a 2a 20 22 67 6c 6f 62 28 42 2c 41 29 2e ** "glob(B,A).
219b6 20 20 57 65 20 77 61 6e 74 20 74 6f 20 75 73 65 We want to use
219b7 20 74 68 65 20 41 20 69 6e 20 22 41 20 67 6c 6f the A in "A glo
219b8 62 20 42 22 20 74 6f 20 74 65 73 74 0a 20 20 20 b B" to test.
219b9 20 20 20 2a 2a 20 66 6f 72 20 66 75 6e 63 74 69 ** for functi
219ba 6f 6e 20 6f 76 65 72 6c 6f 61 64 69 6e 67 2e 20 on overloading.
219bb 20 42 75 74 20 77 65 20 75 73 65 20 74 68 65 20 But we use the
219bc 42 20 74 65 72 6d 20 69 6e 20 22 67 6c 6f 62 28 B term in "glob(
219bd 42 2c 41 29 22 2e 0a 20 20 20 20 20 20 2a 2f 0a B,A)".. */.
219be 20 20 20 20 20 20 69 66 28 20 6e 46 61 72 67 3e if( nFarg>
219bf 3d 32 20 26 26 20 28 70 45 78 70 72 2d 3e 66 6c =2 && (pExpr->fl
219c0 61 67 73 20 26 20 45 50 5f 49 6e 66 69 78 46 75 ags & EP_InfixFu
219c1 6e 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 nc) ){. p
219c2 44 65 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61 Def = sqlite3Vta
219c3 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f bOverloadFunctio
219c4 6e 28 64 62 2c 20 70 44 65 66 2c 20 6e 46 61 72 n(db, pDef, nFar
219c5 67 2c 20 70 46 61 72 67 2d 3e 61 5b 31 5d 2e 70 g, pFarg->a[1].p
219c6 45 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c Expr);. }el
219c7 73 65 20 69 66 28 20 6e 46 61 72 67 3e 30 20 29 se if( nFarg>0 )
219c8 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66 20 3d {. pDef =
219c9 20 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 sqlite3VtabOver
219ca 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c loadFunction(db,
219cb 20 70 44 65 66 2c 20 6e 46 61 72 67 2c 20 70 46 pDef, nFarg, pF
219cc 61 72 67 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 arg->a[0].pExpr)
219cd 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 ;. }.#endif
219ce 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 . for(i=0;
219cf 69 3c 6e 46 61 72 67 3b 20 69 2b 2b 29 7b 0a 20 i<nFarg; i++){.
219d0 20 20 20 20 20 20 20 69 66 28 20 69 3c 33 32 20 if( i<32
219d1 26 26 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 && sqlite3ExprIs
219d2 43 6f 6e 73 74 61 6e 74 28 70 46 61 72 67 2d 3e Constant(pFarg->
219d3 61 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 a[i].pExpr) ){.
219d4 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 4d 61 constMa
219d5 73 6b 20 7c 3d 20 28 31 3c 3c 69 29 3b 0a 20 20 sk |= (1<<i);.
219d6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
219d7 69 66 28 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 if( (pDef->flags
219d8 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e & SQLITE_FUNC_N
219d9 45 45 44 43 4f 4c 4c 29 21 3d 30 20 26 26 20 21 EEDCOLL)!=0 && !
219da 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 pColl ){.
219db 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 pColl = sqlit
219dc 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 e3ExprCollSeq(pP
219dd 61 72 73 65 2c 20 70 46 61 72 67 2d 3e 61 5b 69 arse, pFarg->a[i
219de 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 ].pExpr);.
219df 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
219e0 20 20 69 66 28 20 70 44 65 66 2d 3e 66 6c 61 67 if( pDef->flag
219e1 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f s & SQLITE_FUNC_
219e2 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 NEEDCOLL ){.
219e3 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 if( !pColl )
219e4 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 pColl = db->pDf
219e5 6c 74 43 6f 6c 6c 3b 20 0a 20 20 20 20 20 20 20 ltColl; .
219e6 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
219e7 70 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71 p4(v, OP_CollSeq
219e8 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 , 0, 0, 0, (char
219e9 20 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c *)pColl, P4_COL
219ea 4c 53 45 51 29 3b 0a 20 20 20 20 20 20 7d 0a 20 LSEQ);. }.
219eb 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
219ec 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 46 75 6e AddOp4(v, OP_Fun
219ed 63 74 69 6f 6e 2c 20 63 6f 6e 73 74 4d 61 73 6b ction, constMask
219ee 2c 20 72 31 2c 20 74 61 72 67 65 74 2c 0a 20 20 , r1, target,.
219ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
219f0 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 44 65 (char*)pDe
219f1 66 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a f, P4_FUNCDEF);.
219f2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
219f3 65 43 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38 eChangeP5(v, (u8
219f4 29 6e 46 61 72 67 29 3b 0a 20 20 20 20 20 20 69 )nFarg);. i
219f5 66 28 20 6e 46 61 72 67 20 29 7b 0a 20 20 20 20 f( nFarg ){.
219f6 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 sqlite3Relea
219f7 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 seTempRange(pPar
219f8 73 65 2c 20 72 31 2c 20 6e 46 61 72 67 29 3b 0a se, r1, nFarg);.
219f9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 }. sq
219fa 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 lite3ExprCacheAf
219fb 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 finityChange(pPa
219fc 72 73 65 2c 20 72 31 2c 20 6e 46 61 72 67 29 3b rse, r1, nFarg);
219fd 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
219fe 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
219ff 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 TE_OMIT_SUBQUERY
21a00 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 . case TK_EXI
21a01 53 54 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b STS:. case TK
21a02 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 _SELECT: {.
21a03 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
21a04 4b 5f 45 58 49 53 54 53 20 29 3b 0a 20 20 20 20 K_EXISTS );.
21a05 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
21a06 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 TK_SELECT );.
21a07 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 inReg = sqlit
21a08 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28 e3CodeSubselect(
21a09 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 30 pParse, pExpr, 0
21a0a 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61 , 0);. brea
21a0b 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
21a0c 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 e TK_IN: {.
21a0d 20 69 6e 74 20 72 4e 6f 74 46 6f 75 6e 64 20 3d int rNotFound =
21a0e 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 4d 0;. int rM
21a0f 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a ayHaveNull = 0;.
21a10 20 20 20 20 20 20 69 6e 74 20 6a 32 2c 20 6a 33 int j2, j3
21a11 2c 20 6a 34 2c 20 6a 35 3b 0a 20 20 20 20 20 20 , j4, j5;.
21a12 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20 char affinity;.
21a13 20 20 20 20 20 69 6e 74 20 65 54 79 70 65 3b 0a int eType;.
21a14 0a 20 20 20 20 20 20 56 64 62 65 4e 6f 6f 70 43 . VdbeNoopC
21a15 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 62 65 67 69 omment((v, "begi
21a16 6e 20 49 4e 20 65 78 70 72 20 72 25 64 22 2c 20 n IN expr r%d",
21a17 74 61 72 67 65 74 29 29 3b 0a 20 20 20 20 20 20 target));.
21a18 65 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 46 eType = sqlite3F
21a19 69 6e 64 49 6e 49 6e 64 65 78 28 70 50 61 72 73 indInIndex(pPars
21a1a 65 2c 20 70 45 78 70 72 2c 20 26 72 4d 61 79 48 e, pExpr, &rMayH
21a1b 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 aveNull);.
21a1c 69 66 28 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c if( rMayHaveNull
21a1d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 4e 6f 74 ){. rNot
21a1e 46 6f 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73 65 Found = ++pParse
21a1f 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a ->nMem;. }.
21a20 0a 20 20 20 20 20 20 2f 2a 20 46 69 67 75 72 65 . /* Figure
21a21 20 6f 75 74 20 74 68 65 20 61 66 66 69 6e 69 74 out the affinit
21a22 79 20 74 6f 20 75 73 65 20 74 6f 20 63 72 65 61 y to use to crea
21a23 74 65 20 61 20 6b 65 79 20 66 72 6f 6d 20 74 68 te a key from th
21a24 65 20 72 65 73 75 6c 74 73 0a 20 20 20 20 20 20 e results.
21a25 2a 2a 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 ** of the expres
21a26 73 69 6f 6e 2e 20 61 66 66 69 6e 69 74 79 53 74 sion. affinitySt
21a27 72 20 73 74 6f 72 65 73 20 61 20 73 74 61 74 69 r stores a stati
21a28 63 20 73 74 72 69 6e 67 20 73 75 69 74 61 62 6c c string suitabl
21a29 65 20 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20 50 e for. ** P
21a2a 34 20 6f 66 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 4 of OP_MakeReco
21a2b 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 rd.. */.
21a2c 20 20 20 61 66 66 69 6e 69 74 79 20 3d 20 63 6f affinity = co
21a2d 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 79 mparisonAffinity
21a2e 28 70 45 78 70 72 29 3b 0a 0a 0a 20 20 20 20 20 (pExpr);...
21a2f 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 3c 65 78 /* Code the <ex
21a30 70 72 3e 20 66 72 6f 6d 20 22 3c 65 78 70 72 3e pr> from "<expr>
21a31 20 49 4e 20 28 2e 2e 2e 29 22 2e 20 54 68 65 20 IN (...)". The
21a32 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 0a temporary table.
21a33 20 20 20 20 20 20 2a 2a 20 70 45 78 70 72 2d 3e ** pExpr->
21a34 69 54 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 iTable contains
21a35 74 68 65 20 76 61 6c 75 65 73 20 74 68 61 74 20 the values that
21a36 6d 61 6b 65 20 75 70 20 74 68 65 20 28 2e 2e 2e make up the (...
21a37 29 20 73 65 74 2e 0a 20 20 20 20 20 20 2a 2f 0a ) set.. */.
21a38 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
21a39 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73 rCachePush(pPars
21a3a 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 e);. sqlite
21a3b 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 3ExprCode(pParse
21a3c 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 , pExpr->pLeft,
21a3d 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 6a target);. j
21a3e 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 2 = sqlite3VdbeA
21a3f 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 73 4e 75 ddOp1(v, OP_IsNu
21a40 6c 6c 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 ll, target);.
21a41 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 49 4e if( eType==IN
21a42 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 29 7b 0a _INDEX_ROWID ){.
21a43 20 20 20 20 20 20 20 20 6a 33 20 3d 20 73 71 6c j3 = sql
21a44 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
21a45 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 , OP_MustBeInt,
21a46 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 target);.
21a47 20 6a 34 20 3d 20 73 71 6c 69 74 65 33 56 64 62 j4 = sqlite3Vdb
21a48 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f eAddOp3(v, OP_No
21a49 74 45 78 69 73 74 73 2c 20 70 45 78 70 72 2d 3e tExists, pExpr->
21a4a 69 54 61 62 6c 65 2c 20 30 2c 20 74 61 72 67 65 iTable, 0, targe
21a4b 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 t);. sqli
21a4c 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
21a4d 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 OP_Integer, 1,
21a4e 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 target);.
21a4f 20 6a 35 20 3d 20 73 71 6c 69 74 65 33 56 64 62 j5 = sqlite3Vdb
21a50 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f eAddOp0(v, OP_Go
21a51 74 6f 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c to);. sql
21a52 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
21a53 28 76 2c 20 6a 33 29 3b 0a 20 20 20 20 20 20 20 (v, j3);.
21a54 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 sqlite3VdbeJump
21a55 48 65 72 65 28 76 2c 20 6a 34 29 3b 0a 20 20 20 Here(v, j4);.
21a56 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21a57 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 AddOp2(v, OP_Int
21a58 65 67 65 72 2c 20 30 2c 20 74 61 72 67 65 74 29 eger, 0, target)
21a59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
21a5a 20 20 20 20 20 20 20 72 32 20 3d 20 72 65 67 46 r2 = regF
21a5b 72 65 65 32 20 3d 20 73 71 6c 69 74 65 33 47 65 ree2 = sqlite3Ge
21a5c 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 tTempReg(pParse)
21a5d 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 72 ;.. /* Cr
21a5e 65 61 74 65 20 61 20 72 65 63 6f 72 64 20 61 6e eate a record an
21a5f 64 20 74 65 73 74 20 66 6f 72 20 73 65 74 20 6d d test for set m
21a60 65 6d 62 65 72 73 68 69 70 2e 20 49 66 20 74 68 embership. If th
21a61 65 20 73 65 74 20 63 6f 6e 74 61 69 6e 73 0a 20 e set contains.
21a62 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 76 61 ** the va
21a63 6c 75 65 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 lue, then jump t
21a64 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 o the end of the
21a65 20 74 65 73 74 20 63 6f 64 65 2e 20 54 68 65 20 test code. The
21a66 74 61 72 67 65 74 0a 20 20 20 20 20 20 20 20 2a target. *
21a67 2a 20 72 65 67 69 73 74 65 72 20 73 74 69 6c 6c * register still
21a68 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 72 contains the tr
21a69 75 65 20 28 31 29 20 76 61 6c 75 65 20 77 72 69 ue (1) value wri
21a6a 74 74 65 6e 20 74 6f 20 69 74 20 65 61 72 6c 69 tten to it earli
21a6b 65 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 er.. */.
21a6c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
21a6d 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d beAddOp4(v, OP_M
21a6e 61 6b 65 52 65 63 6f 72 64 2c 20 74 61 72 67 65 akeRecord, targe
21a6f 74 2c 20 31 2c 20 72 32 2c 20 26 61 66 66 69 6e t, 1, r2, &affin
21a70 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 ity, 1);.
21a71 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21a72 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 p2(v, OP_Integer
21a73 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 , 1, target);.
21a74 20 20 20 20 20 20 6a 35 20 3d 20 73 71 6c 69 74 j5 = sqlit
21a75 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
21a76 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 70 72 2d OP_Found, pExpr-
21a77 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 32 29 3b >iTable, 0, r2);
21a78 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 .. /* If
21a79 74 68 65 20 73 65 74 20 6d 65 6d 62 65 72 73 68 the set membersh
21a7a 69 70 20 74 65 73 74 20 66 61 69 6c 73 2c 20 74 ip test fails, t
21a7b 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f hen the result o
21a7c 66 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a f the . *
21a7d 2a 20 22 78 20 49 4e 20 28 2e 2e 2e 29 22 20 65 * "x IN (...)" e
21a7e 78 70 72 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 xpression must b
21a7f 65 20 65 69 74 68 65 72 20 30 20 6f 72 20 4e 55 e either 0 or NU
21a80 4c 4c 2e 20 49 66 20 74 68 65 20 73 65 74 0a 20 LL. If the set.
21a81 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 ** contai
21a82 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 ns no NULL value
21a83 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 s, then the resu
21a84 6c 74 20 69 73 20 30 2e 20 49 66 20 74 68 65 20 lt is 0. If the
21a85 73 65 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 set . **
21a86 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 contains one or
21a87 6d 6f 72 65 20 4e 55 4c 4c 20 76 61 6c 75 65 73 more NULL values
21a88 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c , then the resul
21a89 74 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 t of the.
21a8a 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 69 ** expression i
21a8b 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e 0a 20 20 20 s also NULL..
21a8c 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
21a8d 69 66 28 20 72 4e 6f 74 46 6f 75 6e 64 3d 3d 30 if( rNotFound==0
21a8e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a ){. /*
21a8f 20 54 68 69 73 20 62 72 61 6e 63 68 20 72 75 6e This branch run
21a90 73 20 69 66 20 69 74 20 69 73 20 6b 6e 6f 77 6e s if it is known
21a91 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 at compile time
21a92 20 28 6e 6f 77 29 20 74 68 61 74 20 0a 20 20 20 (now) that .
21a93 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73 65 ** the se
21a94 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 4e 55 t contains no NU
21a95 4c 4c 20 76 61 6c 75 65 73 2e 20 54 68 69 73 20 LL values. This
21a96 68 61 70 70 65 6e 73 20 61 73 20 74 68 65 20 72 happens as the r
21a97 65 73 75 6c 74 0a 20 20 20 20 20 20 20 20 20 20 esult.
21a98 2a 2a 20 6f 66 20 61 20 22 4e 4f 54 20 4e 55 4c ** of a "NOT NUL
21a99 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 6e L" constraint in
21a9a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 the database sc
21a9b 68 65 6d 61 2e 20 4e 6f 20 6e 65 65 64 0a 20 20 hema. No need.
21a9c 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 65 ** to te
21a9d 73 74 20 74 68 65 20 64 61 74 61 20 73 74 72 75 st the data stru
21a9e 63 74 75 72 65 20 61 74 20 72 75 6e 74 69 6d 65 cture at runtime
21a9f 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20 in this case..
21aa0 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 */.
21aa1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21aa2 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e eAddOp2(v, OP_In
21aa3 74 65 67 65 72 2c 20 30 2c 20 74 61 72 67 65 74 teger, 0, target
21aa4 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 );. }else
21aa5 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 {. /* T
21aa6 68 69 73 20 62 6c 6f 63 6b 20 70 6f 70 75 6c 61 his block popula
21aa7 74 65 73 20 74 68 65 20 72 4e 6f 74 46 6f 75 6e tes the rNotFoun
21aa8 64 20 72 65 67 69 73 74 65 72 20 77 69 74 68 20 d register with
21aa9 65 69 74 68 65 72 20 4e 55 4c 4c 0a 20 20 20 20 either NULL.
21aaa 20 20 20 20 20 20 2a 2a 20 6f 72 20 30 20 28 61 ** or 0 (a
21aab 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 29 n integer value)
21aac 2e 20 49 66 20 74 68 65 20 64 61 74 61 20 73 74 . If the data st
21aad 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 ructure contains
21aae 20 6f 6e 65 0a 20 20 20 20 20 20 20 20 20 20 2a one. *
21aaf 2a 20 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 73 2c * or more NULLs,
21ab0 20 74 68 65 6e 20 73 65 74 20 72 4e 6f 74 46 6f then set rNotFo
21ab1 75 6e 64 20 74 6f 20 4e 55 4c 4c 2e 20 4f 74 68 und to NULL. Oth
21ab2 65 72 77 69 73 65 2c 20 73 65 74 20 69 74 0a 20 erwise, set it.
21ab3 20 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 30 ** to 0
21ab4 2e 20 49 66 20 72 65 67 69 73 74 65 72 20 72 4d . If register rM
21ab5 61 79 48 61 76 65 4e 75 6c 6c 20 69 73 20 61 6c ayHaveNull is al
21ab6 72 65 61 64 79 20 73 65 74 20 74 6f 20 73 6f 6d ready set to som
21ab7 65 20 76 61 6c 75 65 0a 20 20 20 20 20 20 20 20 e value.
21ab8 20 20 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20 ** other than
21ab9 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 74 NULL, then the t
21aba 65 73 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 est has already
21abb 62 65 65 6e 20 72 75 6e 20 61 6e 64 20 0a 20 20 been run and .
21abc 20 20 20 20 20 20 20 20 2a 2a 20 72 4e 6f 74 46 ** rNotF
21abd 6f 75 6e 64 20 69 73 20 61 6c 72 65 61 64 79 20 ound is already
21abe 70 6f 70 75 6c 61 74 65 64 2e 0a 20 20 20 20 20 populated..
21abf 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
21ac0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 static const c
21ac1 68 61 72 20 6e 75 6c 6c 52 65 63 6f 72 64 5b 5d har nullRecord[]
21ac2 20 3d 20 7b 20 30 78 30 32 2c 20 30 78 30 30 20 = { 0x02, 0x00
21ac3 7d 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 33 20 };. j3
21ac4 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 = sqlite3VdbeAdd
21ac5 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c Op1(v, OP_NotNul
21ac6 6c 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29 l, rMayHaveNull)
21ac7 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
21ac8 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
21ac9 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 4e 6f OP_Null, 0, rNo
21aca 74 46 6f 75 6e 64 29 3b 0a 20 20 20 20 20 20 20 tFound);.
21acb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21acc 64 4f 70 34 28 76 2c 20 4f 50 5f 42 6c 6f 62 2c dOp4(v, OP_Blob,
21acd 20 32 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2, rMayHaveNull
21ace 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 , 0, .
21acf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21ad0 20 20 20 6e 75 6c 6c 52 65 63 6f 72 64 2c 20 50 nullRecord, P
21ad1 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 4_STATIC);.
21ad2 20 20 20 20 20 6a 34 20 3d 20 73 71 6c 69 74 65 j4 = sqlite
21ad3 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 3VdbeAddOp3(v, O
21ad4 50 5f 46 6f 75 6e 64 2c 20 70 45 78 70 72 2d 3e P_Found, pExpr->
21ad5 69 54 61 62 6c 65 2c 20 30 2c 20 72 4d 61 79 48 iTable, 0, rMayH
21ad6 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 aveNull);.
21ad7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21ad8 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 ddOp2(v, OP_Inte
21ad9 67 65 72 2c 20 30 2c 20 72 4e 6f 74 46 6f 75 6e ger, 0, rNotFoun
21ada 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 d);. sq
21adb 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 lite3VdbeJumpHer
21adc 65 28 76 2c 20 6a 34 29 3b 0a 20 20 20 20 20 20 e(v, j4);.
21add 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a sqlite3VdbeJ
21ade 75 6d 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a umpHere(v, j3);.
21adf 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f . /* Co
21ae0 70 79 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 py the value of
21ae1 72 65 67 69 73 74 65 72 20 72 4e 6f 74 46 6f 75 register rNotFou
21ae2 6e 64 20 28 77 68 69 63 68 20 69 73 20 65 69 74 nd (which is eit
21ae3 68 65 72 20 4e 55 4c 4c 20 6f 72 20 30 29 0a 20 her NULL or 0).
21ae4 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f ** into
21ae5 20 74 68 65 20 74 61 72 67 65 74 20 72 65 67 69 the target regi
21ae6 73 74 65 72 2e 20 54 68 69 73 20 77 69 6c 6c 20 ster. This will
21ae7 62 65 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 be the result of
21ae8 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a the. *
21ae9 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 20 20 * expression..
21aea 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
21aeb 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21aec 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 AddOp2(v, OP_Cop
21aed 79 2c 20 72 4e 6f 74 46 6f 75 6e 64 2c 20 74 61 y, rNotFound, ta
21aee 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d rget);. }
21aef 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 . }. s
21af0 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
21af1 72 65 28 76 2c 20 6a 32 29 3b 0a 20 20 20 20 20 re(v, j2);.
21af2 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 sqlite3VdbeJump
21af3 48 65 72 65 28 76 2c 20 6a 35 29 3b 0a 20 20 20 Here(v, j5);.
21af4 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
21af5 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 chePop(pParse, 1
21af6 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d );. VdbeCom
21af7 6d 65 6e 74 28 28 76 2c 20 22 65 6e 64 20 49 4e ment((v, "end IN
21af8 20 65 78 70 72 20 72 25 64 22 2c 20 74 61 72 67 expr r%d", targ
21af9 65 74 29 29 3b 0a 20 20 20 20 20 20 62 72 65 61 et));. brea
21afa 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a k;. }.#endif.
21afb 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 20 /*. **
21afc 20 78 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44 x BETWEEN y AND
21afd 20 7a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a z. **. **
21afe 20 54 68 69 73 20 69 73 20 65 71 75 69 76 61 6c This is equival
21aff 65 6e 74 20 74 6f 0a 20 20 20 20 2a 2a 0a 20 20 ent to. **.
21b00 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44 ** x>=y AND
21b01 20 78 3c 3d 7a 0a 20 20 20 20 2a 2a 0a 20 20 20 x<=z. **.
21b02 20 2a 2a 20 58 20 69 73 20 73 74 6f 72 65 64 20 ** X is stored
21b03 69 6e 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2e in pExpr->pLeft.
21b04 0a 20 20 20 20 2a 2a 20 59 20 69 73 20 73 74 6f . ** Y is sto
21b05 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c red in pExpr->pL
21b06 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2e ist->a[0].pExpr.
21b07 0a 20 20 20 20 2a 2a 20 5a 20 69 73 20 73 74 6f . ** Z is sto
21b08 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c red in pExpr->pL
21b09 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 2e ist->a[1].pExpr.
21b0a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 . */. case
21b0b 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20 TK_BETWEEN: {.
21b0c 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 Expr *pLeft
21b0d 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b = pExpr->pLeft;
21b0e 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78 . struct Ex
21b0f 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 49 prList_item *pLI
21b10 74 65 6d 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 tem = pExpr->x.p
21b11 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 45 List->a;. E
21b12 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70 4c xpr *pRight = pL
21b13 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 0a 20 20 Item->pExpr;..
21b14 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f codeCompareO
21b15 70 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 perands(pParse,
21b16 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 67 pLeft, &r1, ®
21b17 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20 20 Free1,.
21b18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21b19 20 20 20 20 20 20 20 20 20 70 52 69 67 68 74 2c pRight,
21b1a 20 26 72 32 2c 20 26 72 65 67 46 72 65 65 32 29 &r2, ®Free2)
21b1b 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21b1c 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b ( regFree1==0 );
21b1d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21b1e 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a regFree2==0 );.
21b1f 20 20 20 20 20 20 72 33 20 3d 20 73 71 6c 69 74 r3 = sqlit
21b20 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 e3GetTempReg(pPa
21b21 72 73 65 29 3b 0a 20 20 20 20 20 20 72 34 20 3d rse);. r4 =
21b22 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 sqlite3GetTempR
21b23 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 eg(pParse);.
21b24 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 codeCompare(pP
21b25 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69 arse, pLeft, pRi
21b26 67 68 74 2c 20 4f 50 5f 47 65 2c 0a 20 20 20 20 ght, OP_Ge,.
21b27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 31 r1
21b28 2c 20 72 32 2c 20 72 33 2c 20 53 51 4c 49 54 45 , r2, r3, SQLITE
21b29 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 _STOREP2);.
21b2a 20 70 4c 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 pLItem++;.
21b2b 20 70 52 69 67 68 74 20 3d 20 70 4c 49 74 65 6d pRight = pLItem
21b2c 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 73 ->pExpr;. s
21b2d 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
21b2e 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 pReg(pParse, reg
21b2f 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 72 32 Free2);. r2
21b30 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
21b31 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 deTemp(pParse, p
21b32 52 69 67 68 74 2c 20 26 72 65 67 46 72 65 65 32 Right, ®Free2
21b33 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21b34 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 e( regFree2==0 )
21b35 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 ;. codeComp
21b36 61 72 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66 are(pParse, pLef
21b37 74 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f 4c 65 t, pRight, OP_Le
21b38 2c 20 72 31 2c 20 72 32 2c 20 72 34 2c 20 53 51 , r1, r2, r4, SQ
21b39 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20 LITE_STOREP2);.
21b3a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21b3b 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 6e 64 AddOp3(v, OP_And
21b3c 2c 20 72 33 2c 20 72 34 2c 20 74 61 72 67 65 74 , r3, r4, target
21b3d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
21b3e 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 ReleaseTempReg(p
21b3f 50 61 72 73 65 2c 20 72 33 29 3b 0a 20 20 20 20 Parse, r3);.
21b40 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
21b41 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 TempReg(pParse,
21b42 72 34 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b r4);. break
21b43 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
21b44 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 20 TK_UPLUS: {.
21b45 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 inReg = sqlit
21b46 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 e3ExprCodeTarget
21b47 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
21b48 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b 0a pLeft, target);.
21b49 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21b4a 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }.. case TK_
21b4b 54 52 49 47 47 45 52 3a 20 7b 0a 20 20 20 20 20 TRIGGER: {.
21b4c 20 2f 2a 20 49 66 20 74 68 65 20 6f 70 63 6f 64 /* If the opcod
21b4d 65 20 69 73 20 54 4b 5f 54 52 49 47 47 45 52 2c e is TK_TRIGGER,
21b4e 20 74 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 then the expres
21b4f 73 69 6f 6e 20 69 73 20 61 20 72 65 66 65 72 65 sion is a refere
21b50 6e 63 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 nce. ** to
21b51 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 a column in the
21b52 6e 65 77 2e 2a 20 6f 72 20 6f 6c 64 2e 2a 20 70 new.* or old.* p
21b53 73 65 75 64 6f 2d 74 61 62 6c 65 73 20 61 76 61 seudo-tables ava
21b54 69 6c 61 62 6c 65 20 74 6f 0a 20 20 20 20 20 20 ilable to.
21b55 2a 2a 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 ** trigger progr
21b56 61 6d 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ams. In this cas
21b57 65 20 45 78 70 72 2e 69 54 61 62 6c 65 20 69 73 e Expr.iTable is
21b58 20 73 65 74 20 74 6f 20 31 20 66 6f 72 20 74 68 set to 1 for th
21b59 65 0a 20 20 20 20 20 20 2a 2a 20 6e 65 77 2e 2a e. ** new.*
21b5a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 6f pseudo-table, o
21b5b 72 20 30 20 66 6f 72 20 74 68 65 20 6f 6c 64 2e r 0 for the old.
21b5c 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 20 * pseudo-table.
21b5d 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 0a 20 20 20 Expr.iColumn.
21b5e 20 20 20 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 ** is set to
21b5f 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 the column of th
21b60 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 74 e pseudo-table t
21b61 6f 20 72 65 61 64 2c 20 6f 72 20 74 6f 20 2d 31 o read, or to -1
21b62 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 to. ** rea
21b63 64 20 74 68 65 20 72 6f 77 69 64 20 66 69 65 6c d the rowid fiel
21b64 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 d.. **.
21b65 20 20 2a 2a 20 54 68 65 20 65 78 70 72 65 73 73 ** The express
21b66 69 6f 6e 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 ion is implement
21b67 65 64 20 75 73 69 6e 67 20 61 6e 20 4f 50 5f 50 ed using an OP_P
21b68 61 72 61 6d 20 6f 70 63 6f 64 65 2e 20 54 68 65 aram opcode. The
21b69 20 70 31 0a 20 20 20 20 20 20 2a 2a 20 70 61 72 p1. ** par
21b6a 61 6d 65 74 65 72 20 69 73 20 73 65 74 20 74 6f ameter is set to
21b6b 20 30 20 66 6f 72 20 61 6e 20 6f 6c 64 2e 72 6f 0 for an old.ro
21b6c 77 69 64 20 72 65 66 65 72 65 6e 63 65 2c 20 6f wid reference, o
21b6d 72 20 74 6f 20 28 69 2b 31 29 0a 20 20 20 20 20 r to (i+1).
21b6e 20 2a 2a 20 74 6f 20 72 65 66 65 72 65 6e 63 65 ** to reference
21b6f 20 61 6e 6f 74 68 65 72 20 63 6f 6c 75 6d 6e 20 another column
21b70 6f 66 20 74 68 65 20 6f 6c 64 2e 2a 20 70 73 65 of the old.* pse
21b71 75 64 6f 2d 74 61 62 6c 65 2c 20 77 68 65 72 65 udo-table, where
21b72 20 0a 20 20 20 20 20 20 2a 2a 20 69 20 69 73 20 . ** i is
21b73 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 the index of the
21b74 20 63 6f 6c 75 6d 6e 2e 20 46 6f 72 20 61 20 6e column. For a n
21b75 65 77 2e 72 6f 77 69 64 20 72 65 66 65 72 65 6e ew.rowid referen
21b76 63 65 2c 20 70 31 20 69 73 0a 20 20 20 20 20 20 ce, p1 is.
21b77 2a 2a 20 73 65 74 20 74 6f 20 28 6e 2b 31 29 2c ** set to (n+1),
21b78 20 77 68 65 72 65 20 6e 20 69 73 20 74 68 65 20 where n is the
21b79 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e number of column
21b7a 73 20 69 6e 20 65 61 63 68 20 70 73 65 75 64 6f s in each pseudo
21b7b 2d 74 61 62 6c 65 2e 0a 20 20 20 20 20 20 2a 2a -table.. **
21b7c 20 46 6f 72 20 61 20 72 65 66 65 72 65 6e 63 65 For a reference
21b7d 20 74 6f 20 61 6e 79 20 6f 74 68 65 72 20 63 6f to any other co
21b7e 6c 75 6d 6e 20 69 6e 20 74 68 65 20 6e 65 77 2e lumn in the new.
21b7f 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 * pseudo-table,
21b80 70 31 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 73 p1. ** is s
21b81 65 74 20 74 6f 20 28 6e 2b 32 2b 69 29 2c 20 77 et to (n+2+i), w
21b82 68 65 72 65 20 6e 20 61 6e 64 20 69 20 61 72 65 here n and i are
21b83 20 61 73 20 64 65 66 69 6e 65 64 20 70 72 65 76 as defined prev
21b84 69 6f 75 73 6c 79 2e 20 46 6f 72 0a 20 20 20 20 iously. For.
21b85 20 20 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 69 66 ** example, if
21b86 20 74 68 65 20 74 61 62 6c 65 20 6f 6e 20 77 68 the table on wh
21b87 69 63 68 20 74 72 69 67 67 65 72 73 20 61 72 65 ich triggers are
21b88 20 62 65 69 6e 67 20 66 69 72 65 64 20 69 73 0a being fired is.
21b89 20 20 20 20 20 20 2a 2a 20 64 65 63 6c 61 72 65 ** declare
21b8a 64 20 61 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 d as:. **.
21b8b 20 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45 ** CREATE
21b8c 20 54 41 42 4c 45 20 74 31 28 61 2c 20 62 29 3b TABLE t1(a, b);
21b8d 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
21b8e 2a 2a 20 54 68 65 6e 20 70 31 20 69 73 20 69 6e ** Then p1 is in
21b8f 74 65 72 70 72 65 74 65 64 20 61 73 20 66 6f 6c terpreted as fol
21b90 6c 6f 77 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 lows:. **.
21b91 20 20 20 20 20 2a 2a 20 20 20 70 31 3d 3d 30 20 ** p1==0
21b92 20 20 2d 3e 20 20 20 20 6f 6c 64 2e 72 6f 77 69 -> old.rowi
21b93 64 20 20 20 20 20 70 31 3d 3d 33 20 20 20 2d 3e d p1==3 ->
21b94 20 20 20 20 6e 65 77 2e 72 6f 77 69 64 0a 20 20 new.rowid.
21b95 20 20 20 20 2a 2a 20 20 20 70 31 3d 3d 31 20 20 ** p1==1
21b96 20 2d 3e 20 20 20 20 6f 6c 64 2e 61 20 20 20 20 -> old.a
21b97 20 20 20 20 20 70 31 3d 3d 34 20 20 20 2d 3e 20 p1==4 ->
21b98 20 20 20 6e 65 77 2e 61 0a 20 20 20 20 20 20 2a new.a. *
21b99 2a 20 20 20 70 31 3d 3d 32 20 20 20 2d 3e 20 20 * p1==2 ->
21b9a 20 20 6f 6c 64 2e 62 20 20 20 20 20 20 20 20 20 old.b
21b9b 70 31 3d 3d 35 20 20 20 2d 3e 20 20 20 20 6e 65 p1==5 -> ne
21b9c 77 2e 62 20 20 20 20 20 20 20 0a 20 20 20 20 20 w.b .
21b9d 20 2a 2f 0a 20 20 20 20 20 20 54 61 62 6c 65 20 */. Table
21b9e 2a 70 54 61 62 20 3d 20 70 45 78 70 72 2d 3e 70 *pTab = pExpr->p
21b9f 54 61 62 3b 0a 20 20 20 20 20 20 69 6e 74 20 70 Tab;. int p
21ba0 31 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 1 = pExpr->iTabl
21ba1 65 20 2a 20 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b e * (pTab->nCol+
21ba2 31 29 20 2b 20 31 20 2b 20 70 45 78 70 72 2d 3e 1) + 1 + pExpr->
21ba3 69 43 6f 6c 75 6d 6e 3b 0a 0a 20 20 20 20 20 20 iColumn;..
21ba4 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 69 assert( pExpr->i
21ba5 54 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 45 78 70 Table==0 || pExp
21ba6 72 2d 3e 69 54 61 62 6c 65 3d 3d 31 20 29 3b 0a r->iTable==1 );.
21ba7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 assert( pE
21ba8 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 2d 31 xpr->iColumn>=-1
21ba9 20 26 26 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 && pExpr->iColu
21baa 6d 6e 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b mn<pTab->nCol );
21bab 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
21bac 54 61 62 2d 3e 69 50 4b 65 79 3c 30 20 7c 7c 20 Tab->iPKey<0 ||
21bad 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 21 3d pExpr->iColumn!=
21bae 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 3b 0a 20 pTab->iPKey );.
21baf 20 20 20 20 20 61 73 73 65 72 74 28 20 70 31 3e assert( p1>
21bb0 3d 30 20 26 26 20 70 31 3c 28 70 54 61 62 2d 3e =0 && p1<(pTab->
21bb1 6e 43 6f 6c 2a 32 2b 32 29 20 29 3b 0a 0a 20 20 nCol*2+2) );..
21bb2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21bb3 64 64 4f 70 32 28 76 2c 20 4f 50 5f 50 61 72 61 ddOp2(v, OP_Para
21bb4 6d 2c 20 70 31 2c 20 74 61 72 67 65 74 29 3b 0a m, p1, target);.
21bb5 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e VdbeCommen
21bb6 74 28 28 76 2c 20 22 25 73 2e 25 73 20 2d 3e 20 t((v, "%s.%s ->
21bb7 24 25 64 22 2c 0a 20 20 20 20 20 20 20 20 28 70 $%d",. (p
21bb8 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3f 20 22 Expr->iTable ? "
21bb9 6e 65 77 22 20 3a 20 22 6f 6c 64 22 29 2c 0a 20 new" : "old"),.
21bba 20 20 20 20 20 20 20 28 70 45 78 70 72 2d 3e 69 (pExpr->i
21bbb 43 6f 6c 75 6d 6e 3c 30 20 3f 20 22 72 6f 77 69 Column<0 ? "rowi
21bbc 64 22 20 3a 20 70 45 78 70 72 2d 3e 70 54 61 62 d" : pExpr->pTab
21bbd 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 43 ->aCol[pExpr->iC
21bbe 6f 6c 75 6d 6e 5d 2e 7a 4e 61 6d 65 29 2c 0a 20 olumn].zName),.
21bbf 20 20 20 20 20 20 20 74 61 72 67 65 74 0a 20 20 target.
21bc0 20 20 20 20 29 29 3b 0a 0a 20 20 20 20 20 20 2f ));.. /
21bc1 2a 20 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 * If the column
21bc2 68 61 73 20 52 45 41 4c 20 61 66 66 69 6e 69 74 has REAL affinit
21bc3 79 2c 20 69 74 20 6d 61 79 20 63 75 72 72 65 6e y, it may curren
21bc4 74 6c 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 tly be stored as
21bc5 20 61 6e 0a 20 20 20 20 20 20 2a 2a 20 69 6e 74 an. ** int
21bc6 65 67 65 72 2e 20 55 73 65 20 4f 50 5f 52 65 61 eger. Use OP_Rea
21bc7 6c 41 66 66 69 6e 69 74 79 20 74 6f 20 6d 61 6b lAffinity to mak
21bc8 65 20 73 75 72 65 20 69 74 20 69 73 20 72 65 61 e sure it is rea
21bc9 6c 6c 79 20 72 65 61 6c 2e 20 20 2a 2f 0a 20 20 lly real. */.
21bca 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 if( pExpr->i
21bcb 43 6f 6c 75 6d 6e 3e 3d 30 20 0a 20 20 20 20 20 Column>=0 .
21bcc 20 20 26 26 20 70 54 61 62 2d 3e 61 43 6f 6c 5b && pTab->aCol[
21bcd 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e pExpr->iColumn].
21bce 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 affinity==SQLITE
21bcf 5f 41 46 46 5f 52 45 41 4c 0a 20 20 20 20 20 20 _AFF_REAL.
21bd0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
21bd1 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 e3VdbeAddOp1(v,
21bd2 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 2c OP_RealAffinity,
21bd3 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 target);.
21bd4 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
21bd5 20 20 20 7d 0a 0a 0a 20 20 20 20 2f 2a 0a 20 20 }... /*.
21bd6 20 20 2a 2a 20 46 6f 72 6d 20 41 3a 0a 20 20 20 ** Form A:.
21bd7 20 2a 2a 20 20 20 43 41 53 45 20 78 20 57 48 45 ** CASE x WHE
21bd8 4e 20 65 31 20 54 48 45 4e 20 72 31 20 57 48 45 N e1 THEN r1 WHE
21bd9 4e 20 65 32 20 54 48 45 4e 20 72 32 20 2e 2e 2e N e2 THEN r2 ...
21bda 20 57 48 45 4e 20 65 4e 20 54 48 45 4e 20 72 4e WHEN eN THEN rN
21bdb 20 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 20 ELSE y END.
21bdc 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 42 **. ** Form B
21bdd 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 :. ** CASE
21bde 57 48 45 4e 20 65 31 20 54 48 45 4e 20 72 31 20 WHEN e1 THEN r1
21bdf 57 48 45 4e 20 65 32 20 54 48 45 4e 20 72 32 20 WHEN e2 THEN r2
21be0 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48 45 4e ... WHEN eN THEN
21be1 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a 20 rN ELSE y END.
21be2 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 **. ** For
21be3 6d 20 41 20 69 73 20 63 61 6e 20 62 65 20 74 72 m A is can be tr
21be4 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f 20 74 ansformed into t
21be5 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20 66 6f he equivalent fo
21be6 72 6d 20 42 20 61 73 20 66 6f 6c 6c 6f 77 73 3a rm B as follows:
21be7 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 57 . ** CASE W
21be8 48 45 4e 20 78 3d 65 31 20 54 48 45 4e 20 72 31 HEN x=e1 THEN r1
21be9 20 57 48 45 4e 20 78 3d 65 32 20 54 48 45 4e 20 WHEN x=e2 THEN
21bea 72 32 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20 r2 .... **
21beb 20 20 20 20 20 57 48 45 4e 20 78 3d 65 4e 20 54 WHEN x=eN T
21bec 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e HEN rN ELSE y EN
21bed 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 D. **. **
21bee 58 20 28 69 66 20 69 74 20 65 78 69 73 74 73 29 X (if it exists)
21bef 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c is in pExpr->pL
21bf0 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73 eft.. ** Y is
21bf1 20 69 6e 20 70 45 78 70 72 2d 3e 70 52 69 67 68 in pExpr->pRigh
21bf2 74 2e 20 20 54 68 65 20 59 20 69 73 20 61 6c 73 t. The Y is als
21bf3 6f 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66 20 o optional. If
21bf4 74 68 65 72 65 20 69 73 20 6e 6f 0a 20 20 20 20 there is no.
21bf5 2a 2a 20 45 4c 53 45 20 63 6c 61 75 73 65 20 61 ** ELSE clause a
21bf6 6e 64 20 6e 6f 20 6f 74 68 65 72 20 74 65 72 6d nd no other term
21bf7 20 6d 61 74 63 68 65 73 2c 20 74 68 65 6e 20 74 matches, then t
21bf8 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 he result of the
21bf9 0a 20 20 20 20 2a 2a 20 65 78 70 72 73 73 69 6f . ** exprssio
21bfa 6e 20 69 73 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a n is NULL.. *
21bfb 2a 20 45 69 20 69 73 20 69 6e 20 70 45 78 70 72 * Ei is in pExpr
21bfc 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 5d 20 ->pList->a[i*2]
21bfd 61 6e 64 20 52 69 20 69 73 20 70 45 78 70 72 2d and Ri is pExpr-
21bfe 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 2b 31 5d >pList->a[i*2+1]
21bff 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
21c00 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 The result of th
21c01 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
21c02 74 68 65 20 52 69 20 66 6f 72 20 74 68 65 20 66 the Ri for the f
21c03 69 72 73 74 20 6d 61 74 63 68 69 6e 67 20 45 69 irst matching Ei
21c04 2c 0a 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 74 ,. ** or if t
21c05 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 here is no match
21c06 69 6e 67 20 45 69 2c 20 74 68 65 20 45 4c 53 45 ing Ei, the ELSE
21c07 20 74 65 72 6d 20 59 2c 20 6f 72 20 69 66 20 74 term Y, or if t
21c08 68 65 72 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e here is. ** n
21c09 6f 20 45 4c 53 45 20 74 65 72 6d 2c 20 4e 55 4c o ELSE term, NUL
21c0a 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 65 L.. */. de
21c0b 66 61 75 6c 74 3a 20 61 73 73 65 72 74 28 20 6f fault: assert( o
21c0c 70 3d 3d 54 4b 5f 43 41 53 45 20 29 3b 20 7b 0a p==TK_CASE ); {.
21c0d 20 20 20 20 20 20 69 6e 74 20 65 6e 64 4c 61 62 int endLab
21c0e 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 el;
21c0f 20 20 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20 /* GOTO
21c10 6c 61 62 65 6c 20 66 6f 72 20 65 6e 64 20 6f 66 label for end of
21c11 20 43 41 53 45 20 73 74 6d 74 20 2a 2f 0a 20 20 CASE stmt */.
21c12 20 20 20 20 69 6e 74 20 6e 65 78 74 43 61 73 65 int nextCase
21c13 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
21c14 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61 /* GOTO la
21c15 62 65 6c 20 66 6f 72 20 6e 65 78 74 20 57 48 45 bel for next WHE
21c16 4e 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 N clause */.
21c17 20 20 69 6e 74 20 6e 45 78 70 72 3b 20 20 20 20 int nExpr;
21c18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c19 20 20 20 20 2f 2a 20 32 78 20 6e 75 6d 62 65 72 /* 2x number
21c1a 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a of WHEN terms *
21c1b 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 /. int i;
21c1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c1d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
21c1e 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 p counter */.
21c1f 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c ExprList *pEL
21c20 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ist;
21c21 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 /* List of
21c22 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 WHEN terms */.
21c23 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c struct ExprL
21c24 69 73 74 5f 69 74 65 6d 20 2a 61 4c 69 73 74 65 ist_item *aListe
21c25 6c 65 6d 3b 20 20 2f 2a 20 41 72 72 61 79 20 6f lem; /* Array o
21c26 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a f WHEN terms */.
21c27 20 20 20 20 20 20 45 78 70 72 20 6f 70 43 6f 6d Expr opCom
21c28 70 61 72 65 3b 20 20 20 20 20 20 20 20 20 20 20 pare;
21c29 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 58 /* The X
21c2a 3d 3d 45 69 20 65 78 70 72 65 73 73 69 6f 6e 20 ==Ei expression
21c2b 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 63 61 */. Expr ca
21c2c 63 68 65 58 3b 20 20 20 20 20 20 20 20 20 20 20 cheX;
21c2d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 /* Ca
21c2e 63 68 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 ched expression
21c2f 58 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 X */. Expr
21c30 2a 70 58 3b 20 20 20 20 20 20 20 20 20 20 20 20 *pX;
21c31 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
21c32 54 68 65 20 58 20 65 78 70 72 65 73 73 69 6f 6e The X expression
21c33 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a */. Expr *
21c34 70 54 65 73 74 20 3d 20 30 3b 20 20 20 20 20 20 pTest = 0;
21c35 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 58 /* X
21c36 3d 3d 45 69 20 28 66 6f 72 6d 20 41 29 20 6f 72 ==Ei (form A) or
21c37 20 6a 75 73 74 20 45 69 20 28 66 6f 72 6d 20 42 just Ei (form B
21c38 29 20 2a 2f 0a 20 20 20 20 20 20 56 56 41 5f 4f ) */. VVA_O
21c39 4e 4c 59 28 20 69 6e 74 20 69 43 61 63 68 65 4c NLY( int iCacheL
21c3a 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 evel = pParse->i
21c3b 43 61 63 68 65 4c 65 76 65 6c 3b 20 29 0a 0a 20 CacheLevel; )..
21c3c 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 assert( !Ex
21c3d 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 prHasProperty(pE
21c3e 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 xpr, EP_xIsSelec
21c3f 74 29 20 26 26 20 70 45 78 70 72 2d 3e 78 2e 70 t) && pExpr->x.p
21c40 4c 69 73 74 20 29 3b 0a 20 20 20 20 20 20 61 73 List );. as
21c41 73 65 72 74 28 28 70 45 78 70 72 2d 3e 78 2e 70 sert((pExpr->x.p
21c42 4c 69 73 74 2d 3e 6e 45 78 70 72 20 25 20 32 29 List->nExpr % 2)
21c43 20 3d 3d 20 30 29 3b 0a 20 20 20 20 20 20 61 73 == 0);. as
21c44 73 65 72 74 28 70 45 78 70 72 2d 3e 78 2e 70 4c sert(pExpr->x.pL
21c45 69 73 74 2d 3e 6e 45 78 70 72 20 3e 20 30 29 3b ist->nExpr > 0);
21c46 0a 20 20 20 20 20 20 70 45 4c 69 73 74 20 3d 20 . pEList =
21c47 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a pExpr->x.pList;.
21c48 20 20 20 20 20 20 61 4c 69 73 74 65 6c 65 6d 20 aListelem
21c49 3d 20 70 45 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 = pEList->a;.
21c4a 20 20 20 6e 45 78 70 72 20 3d 20 70 45 4c 69 73 nExpr = pELis
21c4b 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 t->nExpr;.
21c4c 65 6e 64 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 endLabel = sqlit
21c4d 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 e3VdbeMakeLabel(
21c4e 76 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 v);. if( (p
21c4f 58 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 X = pExpr->pLeft
21c50 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )!=0 ){.
21c51 63 61 63 68 65 58 20 3d 20 2a 70 58 3b 0a 20 20 cacheX = *pX;.
21c52 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21c53 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d pX->op==TK_COLUM
21c54 4e 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 N );. tes
21c55 74 63 61 73 65 28 20 70 58 2d 3e 6f 70 3d 3d 54 tcase( pX->op==T
21c56 4b 5f 52 45 47 49 53 54 45 52 20 29 3b 0a 20 20 K_REGISTER );.
21c57 20 20 20 20 20 20 63 61 63 68 65 58 2e 69 54 61 cacheX.iTa
21c58 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 ble = sqlite3Exp
21c59 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 rCodeTemp(pParse
21c5a 2c 20 70 58 2c 20 26 72 65 67 46 72 65 65 31 29 , pX, ®Free1)
21c5b 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 ;. testca
21c5c 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 se( regFree1==0
21c5d 29 3b 0a 20 20 20 20 20 20 20 20 63 61 63 68 65 );. cache
21c5e 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 X.op = TK_REGIST
21c5f 45 52 3b 0a 20 20 20 20 20 20 20 20 6f 70 43 6f ER;. opCo
21c60 6d 70 61 72 65 2e 6f 70 20 3d 20 54 4b 5f 45 51 mpare.op = TK_EQ
21c61 3b 0a 20 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 ;. opComp
21c62 61 72 65 2e 70 4c 65 66 74 20 3d 20 26 63 61 63 are.pLeft = &cac
21c63 68 65 58 3b 0a 20 20 20 20 20 20 20 20 70 54 65 heX;. pTe
21c64 73 74 20 3d 20 26 6f 70 43 6f 6d 70 61 72 65 3b st = &opCompare;
21c65 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 . }. f
21c66 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 78 70 72 3b or(i=0; i<nExpr;
21c67 20 69 3d 69 2b 32 29 7b 0a 20 20 20 20 20 20 20 i=i+2){.
21c68 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
21c69 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 ePush(pParse);.
21c6a 20 20 20 20 20 20 20 69 66 28 20 70 58 20 29 7b if( pX ){
21c6b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 . asser
21c6c 74 28 20 70 54 65 73 74 21 3d 30 20 29 3b 0a 20 t( pTest!=0 );.
21c6d 20 20 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 61 opCompa
21c6e 72 65 2e 70 52 69 67 68 74 20 3d 20 61 4c 69 73 re.pRight = aLis
21c6f 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b 0a telem[i].pExpr;.
21c70 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
21c71 20 20 20 20 20 20 20 20 20 70 54 65 73 74 20 3d pTest =
21c72 20 61 4c 69 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 aListelem[i].pE
21c73 78 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 xpr;. }.
21c74 20 20 20 20 20 20 20 6e 65 78 74 43 61 73 65 20 nextCase
21c75 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b = sqlite3VdbeMak
21c76 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 eLabel(v);.
21c77 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 testcase( pTe
21c78 73 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d st->op==TK_COLUM
21c79 4e 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c N );. sql
21c7a 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 ite3ExprIfFalse(
21c7b 70 50 61 72 73 65 2c 20 70 54 65 73 74 2c 20 6e pParse, pTest, n
21c7c 65 78 74 43 61 73 65 2c 20 53 51 4c 49 54 45 5f extCase, SQLITE_
21c7d 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 JUMPIFNULL);.
21c7e 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 61 testcase( a
21c7f 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 Listelem[i+1].pE
21c80 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 xpr->op==TK_COLU
21c81 4d 4e 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65 MN );. te
21c82 73 74 63 61 73 65 28 20 61 4c 69 73 74 65 6c 65 stcase( aListele
21c83 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 m[i+1].pExpr->op
21c84 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b ==TK_REGISTER );
21c85 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
21c86 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c ExprCode(pParse,
21c87 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e aListelem[i+1].
21c88 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a pExpr, target);.
21c89 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
21c8a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
21c8b 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4c 61 62 65 Goto, 0, endLabe
21c8c 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 l);. sqli
21c8d 74 65 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 te3ExprCachePop(
21c8e 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20 20 20 pParse, 1);.
21c8f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 sqlite3VdbeR
21c90 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6e esolveLabel(v, n
21c91 65 78 74 43 61 73 65 29 3b 0a 20 20 20 20 20 20 extCase);.
21c92 7d 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 }. if( pExp
21c93 72 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 20 r->pRight ){.
21c94 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
21c95 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65 CachePush(pParse
21c96 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
21c97 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 e3ExprCode(pPars
21c98 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 e, pExpr->pRight
21c99 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
21c9a 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
21c9b 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 chePop(pParse, 1
21c9c 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
21c9d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
21c9e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
21c9f 4e 75 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74 29 Null, 0, target)
21ca0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
21ca1 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c assert( db->mall
21ca2 6f 63 46 61 69 6c 65 64 20 7c 7c 20 70 50 61 72 ocFailed || pPar
21ca3 73 65 2d 3e 6e 45 72 72 3e 30 20 0a 20 20 20 20 se->nErr>0 .
21ca4 20 20 20 20 20 20 20 7c 7c 20 70 50 61 72 73 65 || pParse
21ca5 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3d 3d 69 ->iCacheLevel==i
21ca6 43 61 63 68 65 4c 65 76 65 6c 20 29 3b 0a 20 20 CacheLevel );.
21ca7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 sqlite3VdbeR
21ca8 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65 esolveLabel(v, e
21ca9 6e 64 4c 61 62 65 6c 29 3b 0a 20 20 20 20 20 20 ndLabel);.
21caa 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 break;. }.#if
21cab 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
21cac 5f 54 52 49 47 47 45 52 0a 20 20 20 20 63 61 73 _TRIGGER. cas
21cad 65 20 54 4b 5f 52 41 49 53 45 3a 20 7b 0a 20 20 e TK_RAISE: {.
21cae 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 assert( pExp
21caf 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f r->affinity==OE_
21cb0 52 6f 6c 6c 62 61 63 6b 20 0a 20 20 20 20 20 20 Rollback .
21cb1 20 20 20 20 20 7c 7c 20 70 45 78 70 72 2d 3e 61 || pExpr->a
21cb2 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 41 62 6f 72 ffinity==OE_Abor
21cb3 74 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 t. ||
21cb4 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d pExpr->affinity=
21cb5 3d 4f 45 5f 46 61 69 6c 0a 20 20 20 20 20 20 20 =OE_Fail.
21cb6 20 20 20 20 7c 7c 20 70 45 78 70 72 2d 3e 61 66 || pExpr->af
21cb7 66 69 6e 69 74 79 3d 3d 4f 45 5f 49 67 6e 6f 72 finity==OE_Ignor
21cb8 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 e. );.
21cb9 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 70 54 if( !pParse->pT
21cba 72 69 67 67 65 72 54 61 62 20 29 7b 0a 20 20 20 riggerTab ){.
21cbb 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
21cbc 72 4d 73 67 28 70 50 61 72 73 65 2c 0a 20 20 20 rMsg(pParse,.
21cbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21cbe 20 20 20 20 22 52 41 49 53 45 28 29 20 6d 61 79 "RAISE() may
21cbf 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 77 69 only be used wi
21cc0 74 68 69 6e 20 61 20 74 72 69 67 67 65 72 2d 70 thin a trigger-p
21cc1 72 6f 67 72 61 6d 22 29 3b 0a 20 20 20 20 20 20 rogram");.
21cc2 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
21cc3 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 45 }. if( pE
21cc4 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f xpr->affinity==O
21cc5 45 5f 41 62 6f 72 74 20 29 7b 0a 20 20 20 20 20 E_Abort ){.
21cc6 20 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f sqlite3MayAbo
21cc7 72 74 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 rt(pParse);.
21cc8 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 }. assert
21cc9 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
21cca 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 ty(pExpr, EP_Int
21ccb 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 Value) );.
21ccc 69 66 28 20 70 45 78 70 72 2d 3e 61 66 66 69 6e if( pExpr->affin
21ccd 69 74 79 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 ity==OE_Ignore )
21cce 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
21ccf 33 56 64 62 65 41 64 64 4f 70 34 28 0a 20 20 20 3VdbeAddOp4(.
21cd0 20 20 20 20 20 20 20 20 20 76 2c 20 4f 50 5f 48 v, OP_H
21cd1 61 6c 74 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 alt, SQLITE_OK,
21cd2 4f 45 5f 49 67 6e 6f 72 65 2c 20 30 2c 20 70 45 OE_Ignore, 0, pE
21cd3 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 30 29 xpr->u.zToken,0)
21cd4 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
21cd5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 48 61 sqlite3Ha
21cd6 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 ltConstraint(pPa
21cd7 72 73 65 2c 20 70 45 78 70 72 2d 3e 61 66 66 69 rse, pExpr->affi
21cd8 6e 69 74 79 2c 20 70 45 78 70 72 2d 3e 75 2e 7a nity, pExpr->u.z
21cd9 54 6f 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20 Token, 0);.
21cda 20 7d 0a 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }.. break;
21cdb 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
21cdc 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 }. sqlite3Relea
21cdd 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 seTempReg(pParse
21cde 2c 20 72 65 67 46 72 65 65 31 29 3b 0a 20 20 73 , regFree1);. s
21cdf 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
21ce0 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 pReg(pParse, reg
21ce1 46 72 65 65 32 29 3b 0a 20 20 72 65 74 75 72 6e Free2);. return
21ce2 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a inReg;.}../*.**
21ce3 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 Generate code t
21ce4 6f 20 65 76 61 6c 75 61 74 65 20 61 6e 20 65 78 o evaluate an ex
21ce5 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 73 74 6f pression and sto
21ce6 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a re the results.*
21ce7 2a 20 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65 * into a registe
21ce8 72 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 r. Return the r
21ce9 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77 egister number w
21cea 68 65 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 here the results
21ceb 0a 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 2e 0a .** are stored..
21cec 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 67 **.** If the reg
21ced 69 73 74 65 72 20 69 73 20 61 20 74 65 6d 70 6f ister is a tempo
21cee 72 61 72 79 20 72 65 67 69 73 74 65 72 20 74 68 rary register th
21cef 61 74 20 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f at can be deallo
21cf0 63 61 74 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 77 cated,.** then w
21cf1 72 69 74 65 20 69 74 73 20 6e 75 6d 62 65 72 20 rite its number
21cf2 69 6e 74 6f 20 2a 70 52 65 67 2e 20 20 49 66 20 into *pReg. If
21cf3 74 68 65 20 72 65 73 75 6c 74 20 72 65 67 69 73 the result regis
21cf4 74 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 ter is not.** a
21cf5 74 65 6d 70 6f 72 61 72 79 2c 20 74 68 65 6e 20 temporary, then
21cf6 73 65 74 20 2a 70 52 65 67 20 74 6f 20 7a 65 72 set *pReg to zer
21cf7 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 o..*/.SQLITE_PRI
21cf8 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
21cf9 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61 72 ExprCodeTemp(Par
21cfa 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 se *pParse, Expr
21cfb 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 2a 70 52 *pExpr, int *pR
21cfc 65 67 29 7b 0a 20 20 69 6e 74 20 72 31 20 3d 20 eg){. int r1 =
21cfd 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 sqlite3GetTempRe
21cfe 67 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 g(pParse);. int
21cff 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 r2 = sqlite3Exp
21d00 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 rCodeTarget(pPar
21d01 73 65 2c 20 70 45 78 70 72 2c 20 72 31 29 3b 0a se, pExpr, r1);.
21d02 20 20 69 66 28 20 72 32 3d 3d 72 31 20 29 7b 0a if( r2==r1 ){.
21d03 20 20 20 20 2a 70 52 65 67 20 3d 20 72 31 3b 0a *pReg = r1;.
21d04 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c }else{. sql
21d05 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
21d06 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a eg(pParse, r1);.
21d07 20 20 20 20 2a 70 52 65 67 20 3d 20 30 3b 0a 20 *pReg = 0;.
21d08 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 32 3b 0a }. return r2;.
21d09 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
21d0a 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c e code that will
21d0b 20 65 76 61 6c 75 61 74 65 20 65 78 70 72 65 73 evaluate expres
21d0c 73 69 6f 6e 20 70 45 78 70 72 20 61 6e 64 20 73 sion pExpr and s
21d0d 74 6f 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 tore the.** resu
21d0e 6c 74 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 lts in register
21d0f 74 61 72 67 65 74 2e 20 20 54 68 65 20 72 65 73 target. The res
21d10 75 6c 74 73 20 61 72 65 20 67 75 61 72 61 6e 74 ults are guarant
21d11 65 65 64 20 74 6f 20 61 70 70 65 61 72 0a 2a 2a eed to appear.**
21d12 20 69 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 in register tar
21d13 67 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 get..*/.SQLITE_P
21d14 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
21d15 65 33 45 78 70 72 43 6f 64 65 28 50 61 72 73 65 e3ExprCode(Parse
21d16 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
21d17 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 pExpr, int targe
21d18 74 29 7b 0a 20 20 69 6e 74 20 69 6e 52 65 67 3b t){. int inReg;
21d19 0a 0a 20 20 61 73 73 65 72 74 28 20 74 61 72 67 .. assert( targ
21d1a 65 74 3e 30 20 26 26 20 74 61 72 67 65 74 3c 3d et>0 && target<=
21d1b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a pParse->nMem );.
21d1c 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 inReg = sqlite
21d1d 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 3ExprCodeTarget(
21d1e 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 pParse, pExpr, t
21d1f 61 72 67 65 74 29 3b 0a 20 20 61 73 73 65 72 74 arget);. assert
21d20 28 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 ( pParse->pVdbe
21d21 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d || pParse->db->m
21d22 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 allocFailed );.
21d23 20 69 66 28 20 69 6e 52 65 67 21 3d 74 61 72 67 if( inReg!=targ
21d24 65 74 20 26 26 20 70 50 61 72 73 65 2d 3e 70 56 et && pParse->pV
21d25 64 62 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 dbe ){. sqlit
21d26 65 33 56 64 62 65 41 64 64 4f 70 32 28 70 50 61 e3VdbeAddOp2(pPa
21d27 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 53 rse->pVdbe, OP_S
21d28 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 72 Copy, inReg, tar
21d29 67 65 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 get);. }. retu
21d2a 72 6e 20 74 61 72 67 65 74 3b 0a 7d 0a 0a 2f 2a rn target;.}../*
21d2b 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 .** Generate cod
21d2c 65 20 74 68 61 74 20 65 76 61 6c 75 74 65 73 20 e that evalutes
21d2d 74 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 the given expres
21d2e 73 69 6f 6e 20 61 6e 64 20 70 75 74 73 20 74 68 sion and puts th
21d2f 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 72 e result.** in r
21d30 65 67 69 73 74 65 72 20 74 61 72 67 65 74 2e 0a egister target..
21d31 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 6d 61 6b 65 20 **.** Also make
21d32 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65 78 a copy of the ex
21d33 70 72 65 73 73 69 6f 6e 20 72 65 73 75 6c 74 73 pression results
21d34 20 69 6e 74 6f 20 61 6e 6f 74 68 65 72 20 22 63 into another "c
21d35 61 63 68 65 22 20 72 65 67 69 73 74 65 72 0a 2a ache" register.*
21d36 2a 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 65 * and modify the
21d37 20 65 78 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 expression so t
21d38 68 61 74 20 74 68 65 20 6e 65 78 74 20 74 69 6d hat the next tim
21d39 65 20 69 74 20 69 73 20 65 76 61 6c 75 61 74 65 e it is evaluate
21d3a 64 2c 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 d,.** the result
21d3b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 is a copy of th
21d3c 65 20 63 61 63 68 65 20 72 65 67 69 73 74 65 72 e cache register
21d3d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
21d3e 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 tine is used for
21d3f 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 expressions tha
21d40 74 20 61 72 65 20 75 73 65 64 20 6d 75 6c 74 69 t are used multi
21d41 70 6c 65 20 0a 2a 2a 20 74 69 6d 65 73 2e 20 20 ple .** times.
21d42 54 68 65 79 20 61 72 65 20 65 76 61 6c 75 61 74 They are evaluat
21d43 65 64 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 20 ed once and the
21d44 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 65 results of the e
21d45 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 72 65 xpression.** are
21d46 20 72 65 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 reused..*/.SQLI
21d47 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
21d48 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e qlite3ExprCodeAn
21d49 64 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 dCache(Parse *pP
21d4a 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 arse, Expr *pExp
21d4b 72 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a r, int target){.
21d4c 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 Vdbe *v = pPar
21d4d 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 se->pVdbe;. int
21d4e 20 69 6e 52 65 67 3b 0a 20 20 69 6e 52 65 67 20 inReg;. inReg
21d4f 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 = sqlite3ExprCod
21d50 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c e(pParse, pExpr,
21d51 20 74 61 72 67 65 74 29 3b 0a 20 20 61 73 73 65 target);. asse
21d52 72 74 28 20 74 61 72 67 65 74 3e 30 20 29 3b 0a rt( target>0 );.
21d53 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e /* This routin
21d54 65 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 e is called for
21d55 74 65 72 6d 73 20 74 6f 20 49 4e 53 45 52 54 20 terms to INSERT
21d56 6f 72 20 55 50 44 41 54 45 2e 20 20 41 6e 64 20 or UPDATE. And
21d57 74 68 65 20 6f 6e 6c 79 0a 20 20 2a 2a 20 6f 74 the only. ** ot
21d58 68 65 72 20 70 6c 61 63 65 20 77 68 65 72 65 20 her place where
21d59 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61 6e 20 expressions can
21d5a 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 be converted int
21d5b 6f 20 54 4b 5f 52 45 47 49 53 54 45 52 20 69 73 o TK_REGISTER is
21d5c 0a 20 20 2a 2a 20 69 6e 20 57 48 45 52 45 20 63 . ** in WHERE c
21d5d 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 lause processing
21d5e 2e 20 20 53 6f 20 61 73 20 63 75 72 72 65 6e 74 . So as current
21d5f 6c 79 20 69 6d 70 6c 65 6d 65 6e 74 65 64 2c 20 ly implemented,
21d60 74 68 65 72 65 20 69 73 0a 20 20 2a 2a 20 6e 6f there is. ** no
21d61 20 77 61 79 20 66 6f 72 20 61 20 54 4b 5f 52 45 way for a TK_RE
21d62 47 49 53 54 45 52 20 74 6f 20 65 78 69 73 74 20 GISTER to exist
21d63 68 65 72 65 2e 20 20 42 75 74 20 69 74 20 73 65 here. But it se
21d64 65 6d 73 20 70 72 75 64 65 6e 74 20 74 6f 0a 20 ems prudent to.
21d65 20 2a 2a 20 6b 65 65 70 20 74 68 65 20 41 4c 57 ** keep the ALW
21d66 41 59 53 28 29 20 69 6e 20 63 61 73 65 20 74 68 AYS() in case th
21d67 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62 6f e conditions abo
21d68 76 65 20 63 68 61 6e 67 65 20 77 69 74 68 20 66 ve change with f
21d69 75 74 75 72 65 0a 20 20 2a 2a 20 6d 6f 64 69 66 uture. ** modif
21d6a 69 63 61 74 69 6f 6e 73 20 6f 72 20 65 6e 68 61 ications or enha
21d6b 6e 63 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 69 ncements. */. i
21d6c 66 28 20 41 4c 57 41 59 53 28 70 45 78 70 72 2d f( ALWAYS(pExpr-
21d6d 3e 6f 70 21 3d 54 4b 5f 52 45 47 49 53 54 45 52 >op!=TK_REGISTER
21d6e 29 20 29 7b 20 20 0a 20 20 20 20 69 6e 74 20 69 ) ){ . int i
21d6f 4d 65 6d 3b 0a 20 20 20 20 69 4d 65 6d 20 3d 20 Mem;. iMem =
21d70 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a ++pParse->nMem;.
21d71 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21d72 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 ddOp2(v, OP_Copy
21d73 2c 20 69 6e 52 65 67 2c 20 69 4d 65 6d 29 3b 0a , inReg, iMem);.
21d74 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c pExpr->iTabl
21d75 65 20 3d 20 69 4d 65 6d 3b 0a 20 20 20 20 70 45 e = iMem;. pE
21d76 78 70 72 2d 3e 6f 70 32 20 3d 20 70 45 78 70 72 xpr->op2 = pExpr
21d77 2d 3e 6f 70 3b 0a 20 20 20 20 70 45 78 70 72 2d ->op;. pExpr-
21d78 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 >op = TK_REGISTE
21d79 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 R;. }. return
21d7a 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 inReg;.}../*.**
21d7b 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 70 Return TRUE if p
21d7c 45 78 70 72 20 69 73 20 61 6e 20 63 6f 6e 73 74 Expr is an const
21d7d 61 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 20 74 ant expression t
21d7e 68 61 74 20 69 73 20 61 70 70 72 6f 70 72 69 61 hat is appropria
21d7f 74 65 0a 2a 2a 20 66 6f 72 20 66 61 63 74 6f 72 te.** for factor
21d80 69 6e 67 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f ing out of a loo
21d81 70 2e 20 20 41 70 70 72 6f 70 72 69 61 74 65 20 p. Appropriate
21d82 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 3a expressions are:
21d83 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79 .**.** * Any
21d84 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 expression that
21d85 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 74 77 evaluates to tw
21d86 6f 20 6f 72 20 6d 6f 72 65 20 6f 70 63 6f 64 65 o or more opcode
21d87 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 s..**.** * A
21d88 6e 79 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 4f ny OP_Integer, O
21d89 50 5f 52 65 61 6c 2c 20 4f 50 5f 53 74 72 69 6e P_Real, OP_Strin
21d8a 67 2c 20 4f 50 5f 42 6c 6f 62 2c 20 4f 50 5f 4e g, OP_Blob, OP_N
21d8b 75 6c 6c 2c 20 0a 2a 2a 20 20 20 20 20 20 20 6f ull, .** o
21d8c 72 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 74 68 r OP_Variable th
21d8d 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 at does not need
21d8e 20 74 6f 20 62 65 20 70 6c 61 63 65 64 20 69 6e to be placed in
21d8f 20 61 20 0a 2a 2a 20 20 20 20 20 20 20 73 70 65 a .** spe
21d90 63 69 66 69 63 20 72 65 67 69 73 74 65 72 2e 0a cific register..
21d91 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e **.** There is n
21d92 6f 20 70 6f 69 6e 74 20 69 6e 20 66 61 63 74 6f o point in facto
21d93 72 69 6e 67 20 6f 75 74 20 73 69 6e 67 6c 65 2d ring out single-
21d94 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f 6e 73 instruction cons
21d95 74 61 6e 74 0a 2a 2a 20 65 78 70 72 65 73 73 69 tant.** expressi
21d96 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f ons that need to
21d97 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20 61 20 be placed in a
21d98 70 61 72 74 69 63 75 6c 61 72 20 72 65 67 69 73 particular regis
21d99 74 65 72 2e 20 20 0a 2a 2a 20 57 65 20 63 6f 75 ter. .** We cou
21d9a 6c 64 20 66 61 63 74 6f 72 20 74 68 65 6d 20 6f ld factor them o
21d9b 75 74 2c 20 62 75 74 20 74 68 65 6e 20 77 65 20 ut, but then we
21d9c 77 6f 75 6c 64 20 65 6e 64 20 75 70 20 61 64 64 would end up add
21d9d 69 6e 67 20 61 6e 0a 2a 2a 20 4f 50 5f 53 43 6f ing an.** OP_SCo
21d9e 70 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 py instruction t
21d9f 6f 20 6d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 o move the value
21da0 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 72 65 63 into the correc
21da1 74 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6c 61 t register.** la
21da2 74 65 72 2e 20 20 57 65 20 6d 69 67 68 74 20 61 ter. We might a
21da3 73 20 77 65 6c 6c 20 6a 75 73 74 20 75 73 65 20 s well just use
21da4 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 69 6e 73 the original ins
21da5 74 72 75 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 truction and.**
21da6 61 76 6f 69 64 20 74 68 65 20 4f 50 5f 53 43 6f avoid the OP_SCo
21da7 70 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e py..*/.static in
21da8 74 20 69 73 41 70 70 72 6f 70 72 69 61 74 65 46 t isAppropriateF
21da9 6f 72 46 61 63 74 6f 72 69 6e 67 28 45 78 70 72 orFactoring(Expr
21daa 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 73 71 6c *p){. if( !sql
21dab 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 ite3ExprIsConsta
21dac 6e 74 4e 6f 74 4a 6f 69 6e 28 70 29 20 29 7b 0a ntNotJoin(p) ){.
21dad 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 2f return 0; /
21dae 2a 20 4f 6e 6c 79 20 63 6f 6e 73 74 61 6e 74 20 * Only constant
21daf 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 expressions are
21db0 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 appropriate for
21db1 66 61 63 74 6f 72 69 6e 67 20 2a 2f 0a 20 20 7d factoring */. }
21db2 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 . if( (p->flags
21db3 20 26 20 45 50 5f 46 69 78 65 64 44 65 73 74 29 & EP_FixedDest)
21db4 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
21db5 6e 20 31 3b 20 20 2f 2a 20 41 6e 79 20 63 6f 6e n 1; /* Any con
21db6 73 74 61 6e 74 20 77 69 74 68 6f 75 74 20 61 20 stant without a
21db7 66 69 78 65 64 20 64 65 73 74 69 6e 61 74 69 6f fixed destinatio
21db8 6e 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 n is appropriate
21db9 20 2a 2f 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 */. }. while(
21dba 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 p->op==TK_UPLUS
21dbb 20 29 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b ) p = p->pLeft;
21dbc 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 . switch( p->op
21dbd 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ){.#ifndef SQLI
21dbe 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 TE_OMIT_BLOB_LIT
21dbf 45 52 41 4c 0a 20 20 20 20 63 61 73 65 20 54 4b ERAL. case TK
21dc0 5f 42 4c 4f 42 3a 0a 23 65 6e 64 69 66 0a 20 20 _BLOB:.#endif.
21dc1 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49 41 42 case TK_VARIAB
21dc2 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f LE:. case TK_
21dc3 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 61 73 INTEGER:. cas
21dc4 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a 20 20 20 20 e TK_FLOAT:.
21dc5 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 0a 20 20 case TK_NULL:.
21dc6 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49 4e 47 case TK_STRING
21dc7 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 : {. testca
21dc8 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 42 4c se( p->op==TK_BL
21dc9 4f 42 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 OB );. test
21dca 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f case( p->op==TK_
21dcb 56 41 52 49 41 42 4c 45 20 29 3b 0a 20 20 20 20 VARIABLE );.
21dcc 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f testcase( p->o
21dcd 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 3b p==TK_INTEGER );
21dce 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21dcf 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 p->op==TK_FLOAT
21dd0 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21dd1 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 se( p->op==TK_NU
21dd2 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 LL );. test
21dd3 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f case( p->op==TK_
21dd4 53 54 52 49 4e 47 20 29 3b 0a 20 20 20 20 20 20 STRING );.
21dd5 2f 2a 20 53 69 6e 67 6c 65 2d 69 6e 73 74 72 75 /* Single-instru
21dd6 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 73 20 ction constants
21dd7 77 69 74 68 20 61 20 66 69 78 65 64 20 64 65 73 with a fixed des
21dd8 74 69 6e 61 74 69 6f 6e 20 61 72 65 0a 20 20 20 tination are.
21dd9 20 20 20 2a 2a 20 62 65 74 74 65 72 20 64 6f 6e ** better don
21dda 65 20 69 6e 2d 6c 69 6e 65 2e 20 20 49 66 20 77 e in-line. If w
21ddb 65 20 66 61 63 74 6f 72 20 74 68 65 6d 2c 20 74 e factor them, t
21ddc 68 65 79 20 77 69 6c 6c 20 6a 75 73 74 20 65 6e hey will just en
21ddd 64 0a 20 20 20 20 20 20 2a 2a 20 75 70 20 67 65 d. ** up ge
21dde 6e 65 72 61 74 69 6e 67 20 61 6e 20 4f 50 5f 53 nerating an OP_S
21ddf 43 6f 70 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 Copy to move the
21de0 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20 64 65 value to the de
21de1 73 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20 20 20 stination.
21de2 2a 2a 20 72 65 67 69 73 74 65 72 2e 20 2a 2f 0a ** register. */.
21de3 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
21de4 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
21de5 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 K_UMINUS: {.
21de6 20 20 69 66 28 20 70 2d 3e 70 4c 65 66 74 2d 3e if( p->pLeft->
21de7 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 op==TK_FLOAT ||
21de8 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b p->pLeft->op==TK
21de9 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 _INTEGER ){.
21dea 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
21deb 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
21dec 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 k;. }. def
21ded 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62 72 ault: {. br
21dee 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
21def 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a return 1;.}../*
21df0 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 69 73 20 .** If pExpr is
21df1 61 20 63 6f 6e 73 74 61 6e 74 20 65 78 70 72 65 a constant expre
21df2 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 61 70 ssion that is ap
21df3 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a 2a 2a propriate for.**
21df4 20 66 61 63 74 6f 72 69 6e 67 20 6f 75 74 20 6f factoring out o
21df5 66 20 61 20 6c 6f 6f 70 2c 20 74 68 65 6e 20 65 f a loop, then e
21df6 76 61 6c 75 61 74 65 20 74 68 65 20 65 78 70 72 valuate the expr
21df7 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e 74 6f 20 61 ession.** into a
21df8 20 72 65 67 69 73 74 65 72 20 61 6e 64 20 63 6f register and co
21df9 6e 76 65 72 74 20 74 68 65 20 65 78 70 72 65 73 nvert the expres
21dfa 73 69 6f 6e 20 69 6e 74 6f 20 61 20 54 4b 5f 52 sion into a TK_R
21dfb 45 47 49 53 54 45 52 0a 2a 2a 20 65 78 70 72 65 EGISTER.** expre
21dfc 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ssion..*/.static
21dfd 20 69 6e 74 20 65 76 61 6c 43 6f 6e 73 74 45 78 int evalConstEx
21dfe 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b pr(Walker *pWalk
21dff 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 er, Expr *pExpr)
21e00 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 {. Parse *pPars
21e01 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61 e = pWalker->pPa
21e02 72 73 65 3b 0a 20 20 73 77 69 74 63 68 28 20 70 rse;. switch( p
21e03 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 Expr->op ){.
21e04 63 61 73 65 20 54 4b 5f 49 4e 3a 0a 20 20 20 20 case TK_IN:.
21e05 63 61 73 65 20 54 4b 5f 52 45 47 49 53 54 45 52 case TK_REGISTER
21e06 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e : {. return
21e07 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 WRC_Prune;.
21e08 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 }. case TK_FU
21e09 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 NCTION:. case
21e0a 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e TK_AGG_FUNCTION
21e0b 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f :. case TK_CO
21e0c 4e 53 54 5f 46 55 4e 43 3a 20 7b 0a 20 20 20 20 NST_FUNC: {.
21e0d 20 20 2f 2a 20 54 68 65 20 61 72 67 75 6d 65 6e /* The argumen
21e0e 74 73 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e ts to a function
21e0f 20 68 61 76 65 20 61 20 66 69 78 65 64 20 64 65 have a fixed de
21e10 73 74 69 6e 61 74 69 6f 6e 2e 0a 20 20 20 20 20 stination..
21e11 20 2a 2a 20 4d 61 72 6b 20 74 68 65 6d 20 74 68 ** Mark them th
21e12 69 73 20 77 61 79 20 74 6f 20 61 76 6f 69 64 20 is way to avoid
21e13 67 65 6e 65 72 61 74 65 64 20 75 6e 6e 65 65 64 generated unneed
21e14 65 64 20 4f 50 5f 53 43 6f 70 79 0a 20 20 20 20 ed OP_SCopy.
21e15 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e ** instruction
21e16 73 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 s. . */.
21e17 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 ExprList *pLi
21e18 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c st = pExpr->x.pL
21e19 69 73 74 3b 0a 20 20 20 20 20 20 61 73 73 65 72 ist;. asser
21e1a 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 t( !ExprHasPrope
21e1b 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 rty(pExpr, EP_xI
21e1c 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 sSelect) );.
21e1d 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 if( pList ){.
21e1e 20 20 20 20 20 20 20 69 6e 74 20 69 20 3d 20 70 int i = p
21e1f 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 List->nExpr;.
21e20 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 struct Expr
21e21 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
21e22 20 3d 20 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 = pList->a;.
21e23 20 20 20 20 20 66 6f 72 28 3b 20 69 3e 30 3b 20 for(; i>0;
21e24 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 i--, pItem++){.
21e25 20 20 20 20 20 20 20 20 20 69 66 28 20 41 4c 57 if( ALW
21e26 41 59 53 28 70 49 74 65 6d 2d 3e 70 45 78 70 72 AYS(pItem->pExpr
21e27 29 20 29 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 ) ) pItem->pExpr
21e28 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 46 69 ->flags |= EP_Fi
21e29 78 65 64 44 65 73 74 3b 0a 20 20 20 20 20 20 20 xedDest;.
21e2a 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
21e2b 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
21e2c 7d 0a 20 20 69 66 28 20 69 73 41 70 70 72 6f 70 }. if( isApprop
21e2d 72 69 61 74 65 46 6f 72 46 61 63 74 6f 72 69 6e riateForFactorin
21e2e 67 28 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 g(pExpr) ){.
21e2f 69 6e 74 20 72 31 20 3d 20 2b 2b 70 50 61 72 73 int r1 = ++pPars
21e30 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 69 6e 74 e->nMem;. int
21e31 20 72 32 3b 0a 20 20 20 20 72 32 20 3d 20 73 71 r2;. r2 = sq
21e32 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 lite3ExprCodeTar
21e33 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 get(pParse, pExp
21e34 72 2c 20 72 31 29 3b 0a 20 20 20 20 69 66 28 20 r, r1);. if(
21e35 4e 45 56 45 52 28 72 31 21 3d 72 32 29 20 29 20 NEVER(r1!=r2) )
21e36 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 sqlite3ReleaseTe
21e37 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 mpReg(pParse, r1
21e38 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 );. pExpr->op
21e39 32 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 2 = pExpr->op;.
21e3a 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 pExpr->op = T
21e3b 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20 20 K_REGISTER;.
21e3c 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 pExpr->iTable =
21e3d 72 32 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 r2;. return W
21e3e 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 RC_Prune;. }.
21e3f 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 return WRC_Conti
21e40 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 nue;.}../*.** Pr
21e41 65 65 76 61 6c 75 61 74 65 20 63 6f 6e 73 74 61 eevaluate consta
21e42 6e 74 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e nt subexpression
21e43 73 20 77 69 74 68 69 6e 20 70 45 78 70 72 20 61 s within pExpr a
21e44 6e 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 nd store the.**
21e45 72 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73 results in regis
21e46 74 65 72 73 2e 20 20 4d 6f 64 69 66 79 20 70 45 ters. Modify pE
21e47 78 70 72 20 73 6f 20 74 68 61 74 20 74 68 65 20 xpr so that the
21e48 63 6f 6e 73 74 61 6e 74 20 73 75 62 65 78 70 72 constant subexpr
21e49 65 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 54 4b esions.** are TK
21e4a 5f 52 45 47 49 53 54 45 52 20 6f 70 63 6f 64 65 _REGISTER opcode
21e4b 73 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 s that refer to
21e4c 74 68 65 20 70 72 65 63 6f 6d 70 75 74 65 64 20 the precomputed
21e4d 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 values..*/.SQLIT
21e4e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
21e4f 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f qlite3ExprCodeCo
21e50 6e 73 74 61 6e 74 73 28 50 61 72 73 65 20 2a 70 nstants(Parse *p
21e51 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 Parse, Expr *pEx
21e52 70 72 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b pr){. Walker w;
21e53 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 . w.xExprCallba
21e54 63 6b 20 3d 20 65 76 61 6c 43 6f 6e 73 74 45 78 ck = evalConstEx
21e55 70 72 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 pr;. w.xSelectC
21e56 61 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 77 allback = 0;. w
21e57 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 .pParse = pParse
21e58 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 ;. sqlite3WalkE
21e59 78 70 72 28 26 77 2c 20 70 45 78 70 72 29 3b 0a xpr(&w, pExpr);.
21e5a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 }.../*.** Genera
21e5b 74 65 20 63 6f 64 65 20 74 68 61 74 20 70 75 73 te code that pus
21e5c 68 65 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 hes the value of
21e5d 20 65 76 65 72 79 20 65 6c 65 6d 65 6e 74 20 6f every element o
21e5e 66 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65 f the given.** e
21e5f 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 69 xpression list i
21e60 6e 74 6f 20 61 20 73 65 71 75 65 6e 63 65 20 6f nto a sequence o
21e61 66 20 72 65 67 69 73 74 65 72 73 20 62 65 67 69 f registers begi
21e62 6e 6e 69 6e 67 20 61 74 20 74 61 72 67 65 74 2e nning at target.
21e63 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 .**.** Return th
21e64 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d e number of elem
21e65 65 6e 74 73 20 65 76 61 6c 75 61 74 65 64 2e 0a ents evaluated..
21e66 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
21e67 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
21e68 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 0a 20 rCodeExprList(.
21e69 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
21e6a 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
21e6b 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 ontext */. Expr
21e6c 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 2f List *pList, /
21e6d 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e * The expression
21e6e 20 6c 69 73 74 20 74 6f 20 62 65 20 63 6f 64 65 list to be code
21e6f 64 20 2a 2f 0a 20 20 69 6e 74 20 74 61 72 67 65 d */. int targe
21e70 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 t, /* Whe
21e71 72 65 20 74 6f 20 77 72 69 74 65 20 72 65 73 75 re to write resu
21e72 6c 74 73 20 2a 2f 0a 20 20 69 6e 74 20 64 6f 48 lts */. int doH
21e73 61 72 64 43 6f 70 79 20 20 20 20 20 2f 2a 20 4d ardCopy /* M
21e74 61 6b 65 20 61 20 68 61 72 64 20 63 6f 70 79 20 ake a hard copy
21e75 6f 66 20 65 76 65 72 79 20 65 6c 65 6d 65 6e 74 of every element
21e76 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 */.){. struct
21e77 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 ExprList_item *p
21e78 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 6e Item;. int i, n
21e79 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 ;. assert( pLis
21e7a 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 t!=0 );. assert
21e7b 28 20 74 61 72 67 65 74 3e 30 20 29 3b 0a 20 20 ( target>0 );.
21e7c 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 n = pList->nExpr
21e7d 3b 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c ;. for(pItem=pL
21e7e 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 6e ist->a, i=0; i<n
21e7f 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b ; i++, pItem++){
21e80 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e . if( pItem->
21e81 69 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 20 iAlias ){.
21e82 69 6e 74 20 69 52 65 67 20 3d 20 63 6f 64 65 41 int iReg = codeA
21e83 6c 69 61 73 28 70 50 61 72 73 65 2c 20 70 49 74 lias(pParse, pIt
21e84 65 6d 2d 3e 69 41 6c 69 61 73 2c 20 70 49 74 65 em->iAlias, pIte
21e85 6d 2d 3e 70 45 78 70 72 2c 20 74 61 72 67 65 74 m->